2001-11-06 Thiemo Seufer <seufer@csv.ica.uni-stuttgart.de>
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
37
38 #include "opcode/mips.h"
39 #include "itbl-ops.h"
40
41 #ifdef DEBUG
42 #define DBG(x) printf x
43 #else
44 #define DBG(x)
45 #endif
46
47 #ifdef OBJ_MAYBE_ELF
48 /* Clean up namespace so we can include obj-elf.h too. */
49 static int mips_output_flavor PARAMS ((void));
50 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
51 #undef OBJ_PROCESS_STAB
52 #undef OUTPUT_FLAVOR
53 #undef S_GET_ALIGN
54 #undef S_GET_SIZE
55 #undef S_SET_ALIGN
56 #undef S_SET_SIZE
57 #undef obj_frob_file
58 #undef obj_frob_file_after_relocs
59 #undef obj_frob_symbol
60 #undef obj_pop_insert
61 #undef obj_sec_sym_ok_for_reloc
62 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
63
64 #include "obj-elf.h"
65 /* Fix any of them that we actually care about. */
66 #undef OUTPUT_FLAVOR
67 #define OUTPUT_FLAVOR mips_output_flavor()
68 #endif
69
70 #if defined (OBJ_ELF)
71 #include "elf/mips.h"
72 #endif
73
74 #ifndef ECOFF_DEBUGGING
75 #define NO_ECOFF_DEBUGGING
76 #define ECOFF_DEBUGGING 0
77 #endif
78
79 #include "ecoff.h"
80
81 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
82 static char *mips_regmask_frag;
83 #endif
84
85 #define AT 1
86 #define TREG 24
87 #define PIC_CALL_REG 25
88 #define KT0 26
89 #define KT1 27
90 #define GP 28
91 #define SP 29
92 #define FP 30
93 #define RA 31
94
95 #define ILLEGAL_REG (32)
96
97 /* Allow override of standard little-endian ECOFF format. */
98
99 #ifndef ECOFF_LITTLE_FORMAT
100 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
101 #endif
102
103 extern int target_big_endian;
104
105 /* The name of the readonly data section. */
106 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
107 ? ".data" \
108 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
109 ? ".rdata" \
110 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
111 ? ".rdata" \
112 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
113 ? ".rodata" \
114 : (abort (), ""))
115
116 /* This is the set of options which may be modified by the .set
117 pseudo-op. We use a struct so that .set push and .set pop are more
118 reliable. */
119
120 struct mips_set_options
121 {
122 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
123 if it has not been initialized. Changed by `.set mipsN', and the
124 -mipsN command line option, and the default CPU. */
125 int isa;
126 /* Whether we are assembling for the mips16 processor. 0 if we are
127 not, 1 if we are, and -1 if the value has not been initialized.
128 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
129 -nomips16 command line options, and the default CPU. */
130 int mips16;
131 /* Non-zero if we should not reorder instructions. Changed by `.set
132 reorder' and `.set noreorder'. */
133 int noreorder;
134 /* Non-zero if we should not permit the $at ($1) register to be used
135 in instructions. Changed by `.set at' and `.set noat'. */
136 int noat;
137 /* Non-zero if we should warn when a macro instruction expands into
138 more than one machine instruction. Changed by `.set nomacro' and
139 `.set macro'. */
140 int warn_about_macros;
141 /* Non-zero if we should not move instructions. Changed by `.set
142 move', `.set volatile', `.set nomove', and `.set novolatile'. */
143 int nomove;
144 /* Non-zero if we should not optimize branches by moving the target
145 of the branch into the delay slot. Actually, we don't perform
146 this optimization anyhow. Changed by `.set bopt' and `.set
147 nobopt'. */
148 int nobopt;
149 /* Non-zero if we should not autoextend mips16 instructions.
150 Changed by `.set autoextend' and `.set noautoextend'. */
151 int noautoextend;
152 };
153
154 /* This is the struct we use to hold the current set of options. Note
155 that we must set the isa field to ISA_UNKNOWN and the mips16 field to
156 -1 to indicate that they have not been initialized. */
157
158 static struct mips_set_options mips_opts =
159 {
160 ISA_UNKNOWN, -1, 0, 0, 0, 0, 0, 0
161 };
162
163 /* These variables are filled in with the masks of registers used.
164 The object format code reads them and puts them in the appropriate
165 place. */
166 unsigned long mips_gprmask;
167 unsigned long mips_cprmask[4];
168
169 /* MIPS ISA we are using for this output file. */
170 static int file_mips_isa = ISA_UNKNOWN;
171
172 /* The argument of the -mcpu= flag. Historical for code generation. */
173 static int mips_cpu = CPU_UNKNOWN;
174
175 /* The argument of the -march= flag. The architecture we are assembling. */
176 static int mips_arch = CPU_UNKNOWN;
177
178 /* The argument of the -mtune= flag. The architecture for which we
179 are optimizing. */
180 static int mips_tune = CPU_UNKNOWN;
181
182 /* The ABI to use. */
183 enum mips_abi_level
184 {
185 NO_ABI = 0,
186 O32_ABI,
187 O64_ABI,
188 N32_ABI,
189 N64_ABI,
190 EABI_ABI
191 };
192
193 static enum mips_abi_level mips_abi = NO_ABI;
194
195 /* Whether we should mark the file EABI64 or EABI32. */
196 static int mips_eabi64 = 0;
197
198 /* If they asked for mips1 or mips2 and a cpu that is
199 mips3 or greater, then mark the object file 32BITMODE. */
200 static int mips_32bitmode = 0;
201
202 /* True if -mgp32 was passed. */
203 static int mips_gp32 = 0;
204
205 /* True if -mfp32 was passed. */
206 static int mips_fp32 = 0;
207
208 /* Some ISA's have delay slots for instructions which read or write
209 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
210 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
211 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
212 delay slot in this ISA. The uses of this macro assume that any
213 ISA that has delay slots for one of these, has them for all. They
214 also assume that ISAs which don't have delays for these insns, don't
215 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
216 #define ISA_HAS_COPROC_DELAYS(ISA) ( \
217 (ISA) == ISA_MIPS1 \
218 || (ISA) == ISA_MIPS2 \
219 || (ISA) == ISA_MIPS3 \
220 )
221
222 /* Return true if ISA supports 64 bit gp register instructions. */
223 #define ISA_HAS_64BIT_REGS(ISA) ( \
224 (ISA) == ISA_MIPS3 \
225 || (ISA) == ISA_MIPS4 \
226 || (ISA) == ISA_MIPS5 \
227 || (ISA) == ISA_MIPS64 \
228 )
229
230 #define HAVE_32BIT_GPRS \
231 (mips_gp32 \
232 || mips_abi == O32_ABI \
233 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
234
235 #define HAVE_32BIT_FPRS \
236 (mips_fp32 \
237 || mips_abi == O32_ABI \
238 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
239
240 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
241 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
242
243 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
244
245 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
246
247 /* We can only have 64bit addresses if the object file format
248 supports it. */
249 #define HAVE_32BIT_ADDRESSES \
250 (HAVE_32BIT_GPRS \
251 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
252 || ! HAVE_64BIT_OBJECTS) \
253 && mips_pic != EMBEDDED_PIC))
254
255 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
256
257 /* Whether the processor uses hardware interlocks to protect
258 reads from the HI and LO registers, and thus does not
259 require nops to be inserted. */
260
261 #define hilo_interlocks (mips_arch == CPU_R4010 \
262 || mips_arch == CPU_SB1 \
263 )
264
265 /* Whether the processor uses hardware interlocks to protect reads
266 from the GPRs, and thus does not require nops to be inserted. */
267 #define gpr_interlocks \
268 (mips_opts.isa != ISA_MIPS1 \
269 || mips_arch == CPU_R3900)
270
271 /* As with other "interlocks" this is used by hardware that has FP
272 (co-processor) interlocks. */
273 /* Itbl support may require additional care here. */
274 #define cop_interlocks (mips_arch == CPU_R4300 \
275 || mips_arch == CPU_SB1 \
276 )
277
278 /* Is this a mfhi or mflo instruction? */
279 #define MF_HILO_INSN(PINFO) \
280 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
281
282 /* MIPS PIC level. */
283
284 enum mips_pic_level
285 {
286 /* Do not generate PIC code. */
287 NO_PIC,
288
289 /* Generate PIC code as in Irix 4. This is not implemented, and I'm
290 not sure what it is supposed to do. */
291 IRIX4_PIC,
292
293 /* Generate PIC code as in the SVR4 MIPS ABI. */
294 SVR4_PIC,
295
296 /* Generate PIC code without using a global offset table: the data
297 segment has a maximum size of 64K, all data references are off
298 the $gp register, and all text references are PC relative. This
299 is used on some embedded systems. */
300 EMBEDDED_PIC
301 };
302
303 static enum mips_pic_level mips_pic;
304
305 /* Warn about all NOPS that the assembler generates. */
306 static int warn_nops = 0;
307
308 /* 1 if we should generate 32 bit offsets from the GP register in
309 SVR4_PIC mode. Currently has no meaning in other modes. */
310 static int mips_big_got;
311
312 /* 1 if trap instructions should used for overflow rather than break
313 instructions. */
314 static int mips_trap;
315
316 /* 1 if double width floating point constants should not be constructed
317 by assembling two single width halves into two single width floating
318 point registers which just happen to alias the double width destination
319 register. On some architectures this aliasing can be disabled by a bit
320 in the status register, and the setting of this bit cannot be determined
321 automatically at assemble time. */
322 static int mips_disable_float_construction;
323
324 /* Non-zero if any .set noreorder directives were used. */
325
326 static int mips_any_noreorder;
327
328 /* Non-zero if nops should be inserted when the register referenced in
329 an mfhi/mflo instruction is read in the next two instructions. */
330 static int mips_7000_hilo_fix;
331
332 /* The size of the small data section. */
333 static unsigned int g_switch_value = 8;
334 /* Whether the -G option was used. */
335 static int g_switch_seen = 0;
336
337 #define N_RMASK 0xc4
338 #define N_VFP 0xd4
339
340 /* If we can determine in advance that GP optimization won't be
341 possible, we can skip the relaxation stuff that tries to produce
342 GP-relative references. This makes delay slot optimization work
343 better.
344
345 This function can only provide a guess, but it seems to work for
346 gcc output. It needs to guess right for gcc, otherwise gcc
347 will put what it thinks is a GP-relative instruction in a branch
348 delay slot.
349
350 I don't know if a fix is needed for the SVR4_PIC mode. I've only
351 fixed it for the non-PIC mode. KR 95/04/07 */
352 static int nopic_need_relax PARAMS ((symbolS *, int));
353
354 /* handle of the OPCODE hash table */
355 static struct hash_control *op_hash = NULL;
356
357 /* The opcode hash table we use for the mips16. */
358 static struct hash_control *mips16_op_hash = NULL;
359
360 /* This array holds the chars that always start a comment. If the
361 pre-processor is disabled, these aren't very useful */
362 const char comment_chars[] = "#";
363
364 /* This array holds the chars that only start a comment at the beginning of
365 a line. If the line seems to have the form '# 123 filename'
366 .line and .file directives will appear in the pre-processed output */
367 /* Note that input_file.c hand checks for '#' at the beginning of the
368 first line of the input file. This is because the compiler outputs
369 #NO_APP at the beginning of its output. */
370 /* Also note that C style comments are always supported. */
371 const char line_comment_chars[] = "#";
372
373 /* This array holds machine specific line separator characters. */
374 const char line_separator_chars[] = ";";
375
376 /* Chars that can be used to separate mant from exp in floating point nums */
377 const char EXP_CHARS[] = "eE";
378
379 /* Chars that mean this number is a floating point constant */
380 /* As in 0f12.456 */
381 /* or 0d1.2345e12 */
382 const char FLT_CHARS[] = "rRsSfFdDxXpP";
383
384 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
385 changed in read.c . Ideally it shouldn't have to know about it at all,
386 but nothing is ideal around here.
387 */
388
389 static char *insn_error;
390
391 static int auto_align = 1;
392
393 /* When outputting SVR4 PIC code, the assembler needs to know the
394 offset in the stack frame from which to restore the $gp register.
395 This is set by the .cprestore pseudo-op, and saved in this
396 variable. */
397 static offsetT mips_cprestore_offset = -1;
398
399 /* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
400 more optimizations, it can use a register value instead of a memory-saved
401 offset and even an other than GP as global pointer. */
402 static offsetT mips_cpreturn_offset = -1;
403 static int mips_cpreturn_register = -1;
404 static int mips_gp_register = GP;
405
406 /* This is the register which holds the stack frame, as set by the
407 .frame pseudo-op. This is needed to implement .cprestore. */
408 static int mips_frame_reg = SP;
409
410 /* To output NOP instructions correctly, we need to keep information
411 about the previous two instructions. */
412
413 /* Whether we are optimizing. The default value of 2 means to remove
414 unneeded NOPs and swap branch instructions when possible. A value
415 of 1 means to not swap branches. A value of 0 means to always
416 insert NOPs. */
417 static int mips_optimize = 2;
418
419 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
420 equivalent to seeing no -g option at all. */
421 static int mips_debug = 0;
422
423 /* The previous instruction. */
424 static struct mips_cl_insn prev_insn;
425
426 /* The instruction before prev_insn. */
427 static struct mips_cl_insn prev_prev_insn;
428
429 /* If we don't want information for prev_insn or prev_prev_insn, we
430 point the insn_mo field at this dummy integer. */
431 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
432
433 /* Non-zero if prev_insn is valid. */
434 static int prev_insn_valid;
435
436 /* The frag for the previous instruction. */
437 static struct frag *prev_insn_frag;
438
439 /* The offset into prev_insn_frag for the previous instruction. */
440 static long prev_insn_where;
441
442 /* The reloc type for the previous instruction, if any. */
443 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
444
445 /* The reloc for the previous instruction, if any. */
446 static fixS *prev_insn_fixp[3];
447
448 /* Non-zero if the previous instruction was in a delay slot. */
449 static int prev_insn_is_delay_slot;
450
451 /* Non-zero if the previous instruction was in a .set noreorder. */
452 static int prev_insn_unreordered;
453
454 /* Non-zero if the previous instruction uses an extend opcode (if
455 mips16). */
456 static int prev_insn_extended;
457
458 /* Non-zero if the previous previous instruction was in a .set
459 noreorder. */
460 static int prev_prev_insn_unreordered;
461
462 /* If this is set, it points to a frag holding nop instructions which
463 were inserted before the start of a noreorder section. If those
464 nops turn out to be unnecessary, the size of the frag can be
465 decreased. */
466 static fragS *prev_nop_frag;
467
468 /* The number of nop instructions we created in prev_nop_frag. */
469 static int prev_nop_frag_holds;
470
471 /* The number of nop instructions that we know we need in
472 prev_nop_frag. */
473 static int prev_nop_frag_required;
474
475 /* The number of instructions we've seen since prev_nop_frag. */
476 static int prev_nop_frag_since;
477
478 /* For ECOFF and ELF, relocations against symbols are done in two
479 parts, with a HI relocation and a LO relocation. Each relocation
480 has only 16 bits of space to store an addend. This means that in
481 order for the linker to handle carries correctly, it must be able
482 to locate both the HI and the LO relocation. This means that the
483 relocations must appear in order in the relocation table.
484
485 In order to implement this, we keep track of each unmatched HI
486 relocation. We then sort them so that they immediately precede the
487 corresponding LO relocation. */
488
489 struct mips_hi_fixup
490 {
491 /* Next HI fixup. */
492 struct mips_hi_fixup *next;
493 /* This fixup. */
494 fixS *fixp;
495 /* The section this fixup is in. */
496 segT seg;
497 };
498
499 /* The list of unmatched HI relocs. */
500
501 static struct mips_hi_fixup *mips_hi_fixup_list;
502
503 /* Map normal MIPS register numbers to mips16 register numbers. */
504
505 #define X ILLEGAL_REG
506 static const int mips32_to_16_reg_map[] =
507 {
508 X, X, 2, 3, 4, 5, 6, 7,
509 X, X, X, X, X, X, X, X,
510 0, 1, X, X, X, X, X, X,
511 X, X, X, X, X, X, X, X
512 };
513 #undef X
514
515 /* Map mips16 register numbers to normal MIPS register numbers. */
516
517 static const unsigned int mips16_to_32_reg_map[] =
518 {
519 16, 17, 2, 3, 4, 5, 6, 7
520 };
521 \f
522 /* Since the MIPS does not have multiple forms of PC relative
523 instructions, we do not have to do relaxing as is done on other
524 platforms. However, we do have to handle GP relative addressing
525 correctly, which turns out to be a similar problem.
526
527 Every macro that refers to a symbol can occur in (at least) two
528 forms, one with GP relative addressing and one without. For
529 example, loading a global variable into a register generally uses
530 a macro instruction like this:
531 lw $4,i
532 If i can be addressed off the GP register (this is true if it is in
533 the .sbss or .sdata section, or if it is known to be smaller than
534 the -G argument) this will generate the following instruction:
535 lw $4,i($gp)
536 This instruction will use a GPREL reloc. If i can not be addressed
537 off the GP register, the following instruction sequence will be used:
538 lui $at,i
539 lw $4,i($at)
540 In this case the first instruction will have a HI16 reloc, and the
541 second reloc will have a LO16 reloc. Both relocs will be against
542 the symbol i.
543
544 The issue here is that we may not know whether i is GP addressable
545 until after we see the instruction that uses it. Therefore, we
546 want to be able to choose the final instruction sequence only at
547 the end of the assembly. This is similar to the way other
548 platforms choose the size of a PC relative instruction only at the
549 end of assembly.
550
551 When generating position independent code we do not use GP
552 addressing in quite the same way, but the issue still arises as
553 external symbols and local symbols must be handled differently.
554
555 We handle these issues by actually generating both possible
556 instruction sequences. The longer one is put in a frag_var with
557 type rs_machine_dependent. We encode what to do with the frag in
558 the subtype field. We encode (1) the number of existing bytes to
559 replace, (2) the number of new bytes to use, (3) the offset from
560 the start of the existing bytes to the first reloc we must generate
561 (that is, the offset is applied from the start of the existing
562 bytes after they are replaced by the new bytes, if any), (4) the
563 offset from the start of the existing bytes to the second reloc,
564 (5) whether a third reloc is needed (the third reloc is always four
565 bytes after the second reloc), and (6) whether to warn if this
566 variant is used (this is sometimes needed if .set nomacro or .set
567 noat is in effect). All these numbers are reasonably small.
568
569 Generating two instruction sequences must be handled carefully to
570 ensure that delay slots are handled correctly. Fortunately, there
571 are a limited number of cases. When the second instruction
572 sequence is generated, append_insn is directed to maintain the
573 existing delay slot information, so it continues to apply to any
574 code after the second instruction sequence. This means that the
575 second instruction sequence must not impose any requirements not
576 required by the first instruction sequence.
577
578 These variant frags are then handled in functions called by the
579 machine independent code. md_estimate_size_before_relax returns
580 the final size of the frag. md_convert_frag sets up the final form
581 of the frag. tc_gen_reloc adjust the first reloc and adds a second
582 one if needed. */
583 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
584 ((relax_substateT) \
585 (((old) << 23) \
586 | ((new) << 16) \
587 | (((reloc1) + 64) << 9) \
588 | (((reloc2) + 64) << 2) \
589 | ((reloc3) ? (1 << 1) : 0) \
590 | ((warn) ? 1 : 0)))
591 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
592 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
593 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
594 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
595 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
596 #define RELAX_WARN(i) ((i) & 1)
597
598 /* For mips16 code, we use an entirely different form of relaxation.
599 mips16 supports two versions of most instructions which take
600 immediate values: a small one which takes some small value, and a
601 larger one which takes a 16 bit value. Since branches also follow
602 this pattern, relaxing these values is required.
603
604 We can assemble both mips16 and normal MIPS code in a single
605 object. Therefore, we need to support this type of relaxation at
606 the same time that we support the relaxation described above. We
607 use the high bit of the subtype field to distinguish these cases.
608
609 The information we store for this type of relaxation is the
610 argument code found in the opcode file for this relocation, whether
611 the user explicitly requested a small or extended form, and whether
612 the relocation is in a jump or jal delay slot. That tells us the
613 size of the value, and how it should be stored. We also store
614 whether the fragment is considered to be extended or not. We also
615 store whether this is known to be a branch to a different section,
616 whether we have tried to relax this frag yet, and whether we have
617 ever extended a PC relative fragment because of a shift count. */
618 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
619 (0x80000000 \
620 | ((type) & 0xff) \
621 | ((small) ? 0x100 : 0) \
622 | ((ext) ? 0x200 : 0) \
623 | ((dslot) ? 0x400 : 0) \
624 | ((jal_dslot) ? 0x800 : 0))
625 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
626 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
627 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
628 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
629 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
630 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
631 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
632 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
633 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
634 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
635 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
636 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
637 \f
638 /* Prototypes for static functions. */
639
640 #ifdef __STDC__
641 #define internalError() \
642 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
643 #else
644 #define internalError() as_fatal (_("MIPS internal Error"));
645 #endif
646
647 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
648
649 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
650 unsigned int reg, enum mips_regclass class));
651 static int reg_needs_delay PARAMS ((unsigned int));
652 static void mips16_mark_labels PARAMS ((void));
653 static void append_insn PARAMS ((char *place,
654 struct mips_cl_insn * ip,
655 expressionS * p,
656 bfd_reloc_code_real_type *r,
657 boolean));
658 static void mips_no_prev_insn PARAMS ((int));
659 static void mips_emit_delays PARAMS ((boolean));
660 #ifdef USE_STDARG
661 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
662 const char *name, const char *fmt,
663 ...));
664 #else
665 static void macro_build ();
666 #endif
667 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
668 const char *, const char *,
669 va_list));
670 static void macro_build_lui PARAMS ((char *place, int *counter,
671 expressionS * ep, int regnum));
672 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
673 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
674 expressionS *));
675 static void load_register PARAMS ((int *, int, expressionS *, int));
676 static void load_address PARAMS ((int *, int, expressionS *, int, int *));
677 static void move_register PARAMS ((int *, int, int));
678 static void macro PARAMS ((struct mips_cl_insn * ip));
679 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
680 #ifdef LOSING_COMPILER
681 static void macro2 PARAMS ((struct mips_cl_insn * ip));
682 #endif
683 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
684 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
685 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
686 boolean, boolean, unsigned long *,
687 boolean *, unsigned short *));
688 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
689 static int my_getSmallExpression PARAMS ((expressionS *, char *));
690 static void my_getExpression PARAMS ((expressionS *, char *));
691 static int support_64bit_objects PARAMS((void));
692 static symbolS *get_symbol PARAMS ((void));
693 static void mips_align PARAMS ((int to, int fill, symbolS *label));
694 static void s_align PARAMS ((int));
695 static void s_change_sec PARAMS ((int));
696 static void s_cons PARAMS ((int));
697 static void s_float_cons PARAMS ((int));
698 static void s_mips_globl PARAMS ((int));
699 static void s_option PARAMS ((int));
700 static void s_mipsset PARAMS ((int));
701 static void s_abicalls PARAMS ((int));
702 static void s_cpload PARAMS ((int));
703 static void s_cpsetup PARAMS ((int));
704 static void s_cplocal PARAMS ((int));
705 static void s_cprestore PARAMS ((int));
706 static void s_cpreturn PARAMS ((int));
707 static void s_gpvalue PARAMS ((int));
708 static void s_gpword PARAMS ((int));
709 static void s_cpadd PARAMS ((int));
710 static void s_insn PARAMS ((int));
711 static void md_obj_begin PARAMS ((void));
712 static void md_obj_end PARAMS ((void));
713 static long get_number PARAMS ((void));
714 static void s_mips_ent PARAMS ((int));
715 static void s_mips_end PARAMS ((int));
716 static void s_mips_frame PARAMS ((int));
717 static void s_mips_mask PARAMS ((int));
718 static void s_mips_stab PARAMS ((int));
719 static void s_mips_weakext PARAMS ((int));
720 static void s_file PARAMS ((int));
721 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
722 static const char *mips_isa_to_str PARAMS ((int));
723 static const char *mips_cpu_to_str PARAMS ((int));
724 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
725 static void show PARAMS ((FILE *, char *, int *, int *));
726
727 /* Return values of my_getSmallExpression(). */
728
729 enum small_ex_type
730 {
731 S_EX_NONE = 0,
732 S_EX_REGISTER,
733
734 /* Direct relocation creation by %percent_op(). */
735 S_EX_HALF,
736 S_EX_HI,
737 S_EX_LO,
738 S_EX_GP_REL,
739 S_EX_GOT,
740 S_EX_CALL16,
741 S_EX_GOT_DISP,
742 S_EX_GOT_PAGE,
743 S_EX_GOT_OFST,
744 S_EX_GOT_HI,
745 S_EX_GOT_LO,
746 S_EX_NEG,
747 S_EX_HIGHER,
748 S_EX_HIGHEST,
749 S_EX_CALL_HI,
750 S_EX_CALL_LO
751 };
752
753 /* Table and functions used to map between CPU/ISA names, and
754 ISA levels, and CPU numbers. */
755
756 struct mips_cpu_info
757 {
758 const char *name; /* CPU or ISA name. */
759 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
760 int isa; /* ISA level. */
761 int cpu; /* CPU number (default CPU if ISA). */
762 };
763
764 static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
765 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
766 static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
767 \f
768 /* Pseudo-op table.
769
770 The following pseudo-ops from the Kane and Heinrich MIPS book
771 should be defined here, but are currently unsupported: .alias,
772 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
773
774 The following pseudo-ops from the Kane and Heinrich MIPS book are
775 specific to the type of debugging information being generated, and
776 should be defined by the object format: .aent, .begin, .bend,
777 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
778 .vreg.
779
780 The following pseudo-ops from the Kane and Heinrich MIPS book are
781 not MIPS CPU specific, but are also not specific to the object file
782 format. This file is probably the best place to define them, but
783 they are not currently supported: .asm0, .endr, .lab, .repeat,
784 .struct. */
785
786 static const pseudo_typeS mips_pseudo_table[] =
787 {
788 /* MIPS specific pseudo-ops. */
789 {"option", s_option, 0},
790 {"set", s_mipsset, 0},
791 {"rdata", s_change_sec, 'r'},
792 {"sdata", s_change_sec, 's'},
793 {"livereg", s_ignore, 0},
794 {"abicalls", s_abicalls, 0},
795 {"cpload", s_cpload, 0},
796 {"cpsetup", s_cpsetup, 0},
797 {"cplocal", s_cplocal, 0},
798 {"cprestore", s_cprestore, 0},
799 {"cpreturn", s_cpreturn, 0},
800 {"gpvalue", s_gpvalue, 0},
801 {"gpword", s_gpword, 0},
802 {"cpadd", s_cpadd, 0},
803 {"insn", s_insn, 0},
804
805 /* Relatively generic pseudo-ops that happen to be used on MIPS
806 chips. */
807 {"asciiz", stringer, 1},
808 {"bss", s_change_sec, 'b'},
809 {"err", s_err, 0},
810 {"half", s_cons, 1},
811 {"dword", s_cons, 3},
812 {"weakext", s_mips_weakext, 0},
813
814 /* These pseudo-ops are defined in read.c, but must be overridden
815 here for one reason or another. */
816 {"align", s_align, 0},
817 {"byte", s_cons, 0},
818 {"data", s_change_sec, 'd'},
819 {"double", s_float_cons, 'd'},
820 {"float", s_float_cons, 'f'},
821 {"globl", s_mips_globl, 0},
822 {"global", s_mips_globl, 0},
823 {"hword", s_cons, 1},
824 {"int", s_cons, 2},
825 {"long", s_cons, 2},
826 {"octa", s_cons, 4},
827 {"quad", s_cons, 3},
828 {"short", s_cons, 1},
829 {"single", s_float_cons, 'f'},
830 {"stabn", s_mips_stab, 'n'},
831 {"text", s_change_sec, 't'},
832 {"word", s_cons, 2},
833
834 #ifdef MIPS_STABS_ELF
835 { "extern", ecoff_directive_extern, 0},
836 #endif
837
838 { NULL, NULL, 0 },
839 };
840
841 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
842 {
843 /* These pseudo-ops should be defined by the object file format.
844 However, a.out doesn't support them, so we have versions here. */
845 {"aent", s_mips_ent, 1},
846 {"bgnb", s_ignore, 0},
847 {"end", s_mips_end, 0},
848 {"endb", s_ignore, 0},
849 {"ent", s_mips_ent, 0},
850 {"file", s_file, 0},
851 {"fmask", s_mips_mask, 'F'},
852 {"frame", s_mips_frame, 0},
853 {"loc", s_ignore, 0},
854 {"mask", s_mips_mask, 'R'},
855 {"verstamp", s_ignore, 0},
856 { NULL, NULL, 0 },
857 };
858
859 extern void pop_insert PARAMS ((const pseudo_typeS *));
860
861 void
862 mips_pop_insert ()
863 {
864 pop_insert (mips_pseudo_table);
865 if (! ECOFF_DEBUGGING)
866 pop_insert (mips_nonecoff_pseudo_table);
867 }
868 \f
869 /* Symbols labelling the current insn. */
870
871 struct insn_label_list
872 {
873 struct insn_label_list *next;
874 symbolS *label;
875 };
876
877 static struct insn_label_list *insn_labels;
878 static struct insn_label_list *free_insn_labels;
879
880 static void mips_clear_insn_labels PARAMS ((void));
881
882 static inline void
883 mips_clear_insn_labels ()
884 {
885 register struct insn_label_list **pl;
886
887 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
888 ;
889 *pl = insn_labels;
890 insn_labels = NULL;
891 }
892 \f
893 static char *expr_end;
894
895 /* Expressions which appear in instructions. These are set by
896 mips_ip. */
897
898 static expressionS imm_expr;
899 static expressionS offset_expr;
900
901 /* Relocs associated with imm_expr and offset_expr. */
902
903 static bfd_reloc_code_real_type imm_reloc[3]
904 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
905 static bfd_reloc_code_real_type offset_reloc[3]
906 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
907
908 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
909
910 static boolean imm_unmatched_hi;
911
912 /* These are set by mips16_ip if an explicit extension is used. */
913
914 static boolean mips16_small, mips16_ext;
915
916 #ifdef MIPS_STABS_ELF
917 /* The pdr segment for per procedure frame/regmask info */
918
919 static segT pdr_seg;
920 #endif
921
922 static const char *
923 mips_isa_to_str (isa)
924 int isa;
925 {
926 const struct mips_cpu_info *ci;
927 static char s[20];
928
929 ci = mips_cpu_info_from_isa (isa);
930 if (ci != NULL)
931 return (ci->name);
932
933 sprintf (s, "ISA#%d", isa);
934 return s;
935 }
936
937 static const char *
938 mips_cpu_to_str (cpu)
939 int cpu;
940 {
941 const struct mips_cpu_info *ci;
942 static char s[16];
943
944 ci = mips_cpu_info_from_cpu (cpu);
945 if (ci != NULL)
946 return (ci->name);
947
948 sprintf (s, "CPU#%d", cpu);
949 return s;
950 }
951
952 /* The default target format to use. */
953
954 const char *
955 mips_target_format ()
956 {
957 switch (OUTPUT_FLAVOR)
958 {
959 case bfd_target_aout_flavour:
960 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
961 case bfd_target_ecoff_flavour:
962 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
963 case bfd_target_coff_flavour:
964 return "pe-mips";
965 case bfd_target_elf_flavour:
966 #ifdef TE_TMIPS
967 /* This is traditional mips */
968 return (target_big_endian
969 ? (HAVE_64BIT_OBJECTS ? "elf64-tradbigmips"
970 : "elf32-tradbigmips")
971 : (HAVE_64BIT_OBJECTS ? "elf64-tradlittlemips"
972 : "elf32-tradlittlemips"));
973 #else
974 return (target_big_endian
975 ? (HAVE_64BIT_OBJECTS ? "elf64-bigmips" : "elf32-bigmips")
976 : (HAVE_64BIT_OBJECTS ? "elf64-littlemips"
977 : "elf32-littlemips"));
978 #endif
979 default:
980 abort ();
981 return NULL;
982 }
983 }
984
985 /* This function is called once, at assembler startup time. It should
986 set up all the tables, etc. that the MD part of the assembler will need. */
987
988 void
989 md_begin ()
990 {
991 register const char *retval = NULL;
992 int i = 0;
993 const char *cpu;
994 char *a = NULL;
995 int broken = 0;
996 int mips_isa_from_cpu;
997 int target_cpu_had_mips16 = 0;
998 const struct mips_cpu_info *ci;
999
1000 /* GP relative stuff not working for PE */
1001 if (strncmp (TARGET_OS, "pe", 2) == 0
1002 && g_switch_value != 0)
1003 {
1004 if (g_switch_seen)
1005 as_bad (_("-G not supported in this configuration."));
1006 g_switch_value = 0;
1007 }
1008
1009 cpu = TARGET_CPU;
1010 if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
1011 {
1012 a = xmalloc (sizeof TARGET_CPU);
1013 strcpy (a, TARGET_CPU);
1014 a[(sizeof TARGET_CPU) - 3] = '\0';
1015 cpu = a;
1016 }
1017
1018 if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
1019 {
1020 target_cpu_had_mips16 = 1;
1021 cpu += sizeof "mips16" - 1;
1022 }
1023
1024 if (mips_opts.mips16 < 0)
1025 mips_opts.mips16 = target_cpu_had_mips16;
1026
1027 /* Backward compatibility for historic -mcpu= option. Check for
1028 incompatible options, warn if -mcpu is used. */
1029 if (mips_cpu != CPU_UNKNOWN
1030 && mips_arch != CPU_UNKNOWN
1031 && mips_cpu != mips_arch)
1032 {
1033 as_fatal (_("The -mcpu option can't be used together with -march. "
1034 "Use -mtune instead of -mcpu."));
1035 }
1036
1037 if (mips_cpu != CPU_UNKNOWN
1038 && mips_tune != CPU_UNKNOWN
1039 && mips_cpu != mips_tune)
1040 {
1041 as_fatal (_("The -mcpu option can't be used together with -mtune. "
1042 "Use -march instead of -mcpu."));
1043 }
1044
1045 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1046 {
1047 ci = mips_cpu_info_from_cpu (mips_cpu);
1048 assert (ci != NULL);
1049 mips_arch = ci->cpu;
1050 as_warn (_("The -mcpu option is deprecated. Please use -march and "
1051 "-mtune instead."));
1052 }
1053
1054 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1055 specified on the command line, or some other value if one was.
1056 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1057 the command line, or will be set otherwise if one was. */
1058 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1059 {
1060 /* We have to check if the isa is the default isa of arch. Otherwise
1061 we'll get invalid object file headers. */
1062 ci = mips_cpu_info_from_cpu (mips_arch);
1063 assert (ci != NULL);
1064 if (mips_opts.isa != ci->isa)
1065 {
1066 /* This really should be an error instead of a warning, but old
1067 compilers only have -mcpu which sets both arch and tune. For
1068 now, we discard arch and preserve tune. */
1069 as_warn (_("The -march option is incompatible to -mipsN and "
1070 "therefore ignored."));
1071 if (mips_tune == CPU_UNKNOWN)
1072 mips_tune = mips_arch;
1073 ci = mips_cpu_info_from_isa (mips_opts.isa);
1074 assert (ci != NULL);
1075 mips_arch = ci->cpu;
1076 }
1077 }
1078 else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
1079 {
1080 /* We have ARCH, we need ISA. */
1081 ci = mips_cpu_info_from_cpu (mips_arch);
1082 assert (ci != NULL);
1083 mips_opts.isa = ci->isa;
1084 }
1085 else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1086 {
1087 /* We have ISA, we need default ARCH. */
1088 ci = mips_cpu_info_from_isa (mips_opts.isa);
1089 assert (ci != NULL);
1090 mips_arch = ci->cpu;
1091 }
1092 else
1093 {
1094 /* We need to set both ISA and ARCH from target cpu. */
1095 ci = mips_cpu_info_from_name (cpu);
1096 if (ci == NULL)
1097 ci = mips_cpu_info_from_cpu (CPU_R3000);
1098 assert (ci != NULL);
1099 mips_opts.isa = ci->isa;
1100 mips_arch = ci->cpu;
1101 }
1102
1103 if (mips_tune == CPU_UNKNOWN)
1104 mips_tune = mips_arch;
1105
1106 ci = mips_cpu_info_from_cpu (mips_arch);
1107 assert (ci != NULL);
1108 mips_isa_from_cpu = ci->isa;
1109
1110 /* End of TARGET_CPU processing, get rid of malloced memory
1111 if necessary. */
1112 cpu = NULL;
1113 if (a != NULL)
1114 {
1115 free (a);
1116 a = NULL;
1117 }
1118
1119 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
1120 as_bad (_("trap exception not supported at ISA 1"));
1121
1122 /* Set the EABI kind based on the ISA before the user gets
1123 to change the ISA with directives. This isn't really
1124 the best, but then neither is basing the abi on the isa. */
1125 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
1126 && mips_abi == EABI_ABI)
1127 mips_eabi64 = 1;
1128
1129 /* If they asked for mips1 or mips2 and a cpu that is
1130 mips3 or greater, then mark the object file 32BITMODE. */
1131 if (mips_isa_from_cpu != ISA_UNKNOWN
1132 && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
1133 && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
1134 mips_32bitmode = 1;
1135
1136 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1137 as_warn (_("Could not set architecture and machine"));
1138
1139 file_mips_isa = mips_opts.isa;
1140
1141 op_hash = hash_new ();
1142
1143 for (i = 0; i < NUMOPCODES;)
1144 {
1145 const char *name = mips_opcodes[i].name;
1146
1147 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1148 if (retval != NULL)
1149 {
1150 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1151 mips_opcodes[i].name, retval);
1152 /* Probably a memory allocation problem? Give up now. */
1153 as_fatal (_("Broken assembler. No assembly attempted."));
1154 }
1155 do
1156 {
1157 if (mips_opcodes[i].pinfo != INSN_MACRO)
1158 {
1159 if (!validate_mips_insn (&mips_opcodes[i]))
1160 broken = 1;
1161 }
1162 ++i;
1163 }
1164 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1165 }
1166
1167 mips16_op_hash = hash_new ();
1168
1169 i = 0;
1170 while (i < bfd_mips16_num_opcodes)
1171 {
1172 const char *name = mips16_opcodes[i].name;
1173
1174 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1175 if (retval != NULL)
1176 as_fatal (_("internal: can't hash `%s': %s"),
1177 mips16_opcodes[i].name, retval);
1178 do
1179 {
1180 if (mips16_opcodes[i].pinfo != INSN_MACRO
1181 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1182 != mips16_opcodes[i].match))
1183 {
1184 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1185 mips16_opcodes[i].name, mips16_opcodes[i].args);
1186 broken = 1;
1187 }
1188 ++i;
1189 }
1190 while (i < bfd_mips16_num_opcodes
1191 && strcmp (mips16_opcodes[i].name, name) == 0);
1192 }
1193
1194 if (broken)
1195 as_fatal (_("Broken assembler. No assembly attempted."));
1196
1197 /* We add all the general register names to the symbol table. This
1198 helps us detect invalid uses of them. */
1199 for (i = 0; i < 32; i++)
1200 {
1201 char buf[5];
1202
1203 sprintf (buf, "$%d", i);
1204 symbol_table_insert (symbol_new (buf, reg_section, i,
1205 &zero_address_frag));
1206 }
1207 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1208 &zero_address_frag));
1209 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1210 &zero_address_frag));
1211 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1212 &zero_address_frag));
1213 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1214 &zero_address_frag));
1215 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1216 &zero_address_frag));
1217 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1218 &zero_address_frag));
1219 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1220 &zero_address_frag));
1221
1222 mips_no_prev_insn (false);
1223
1224 mips_gprmask = 0;
1225 mips_cprmask[0] = 0;
1226 mips_cprmask[1] = 0;
1227 mips_cprmask[2] = 0;
1228 mips_cprmask[3] = 0;
1229
1230 /* set the default alignment for the text section (2**2) */
1231 record_alignment (text_section, 2);
1232
1233 if (USE_GLOBAL_POINTER_OPT)
1234 bfd_set_gp_size (stdoutput, g_switch_value);
1235
1236 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1237 {
1238 /* On a native system, sections must be aligned to 16 byte
1239 boundaries. When configured for an embedded ELF target, we
1240 don't bother. */
1241 if (strcmp (TARGET_OS, "elf") != 0)
1242 {
1243 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1244 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1245 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1246 }
1247
1248 /* Create a .reginfo section for register masks and a .mdebug
1249 section for debugging information. */
1250 {
1251 segT seg;
1252 subsegT subseg;
1253 flagword flags;
1254 segT sec;
1255
1256 seg = now_seg;
1257 subseg = now_subseg;
1258
1259 /* The ABI says this section should be loaded so that the
1260 running program can access it. However, we don't load it
1261 if we are configured for an embedded target */
1262 flags = SEC_READONLY | SEC_DATA;
1263 if (strcmp (TARGET_OS, "elf") != 0)
1264 flags |= SEC_ALLOC | SEC_LOAD;
1265
1266 if (! HAVE_NEWABI)
1267 {
1268 sec = subseg_new (".reginfo", (subsegT) 0);
1269
1270 (void) bfd_set_section_flags (stdoutput, sec, flags);
1271 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1272
1273 #ifdef OBJ_ELF
1274 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1275 #endif
1276 }
1277 else
1278 {
1279 /* The 64-bit ABI uses a .MIPS.options section rather than
1280 .reginfo section. */
1281 sec = subseg_new (".MIPS.options", (subsegT) 0);
1282 (void) bfd_set_section_flags (stdoutput, sec, flags);
1283 (void) bfd_set_section_alignment (stdoutput, sec, 3);
1284
1285 #ifdef OBJ_ELF
1286 /* Set up the option header. */
1287 {
1288 Elf_Internal_Options opthdr;
1289 char *f;
1290
1291 opthdr.kind = ODK_REGINFO;
1292 opthdr.size = (sizeof (Elf_External_Options)
1293 + sizeof (Elf64_External_RegInfo));
1294 opthdr.section = 0;
1295 opthdr.info = 0;
1296 f = frag_more (sizeof (Elf_External_Options));
1297 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1298 (Elf_External_Options *) f);
1299
1300 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1301 }
1302 #endif
1303 }
1304
1305 if (ECOFF_DEBUGGING)
1306 {
1307 sec = subseg_new (".mdebug", (subsegT) 0);
1308 (void) bfd_set_section_flags (stdoutput, sec,
1309 SEC_HAS_CONTENTS | SEC_READONLY);
1310 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1311 }
1312
1313 #ifdef MIPS_STABS_ELF
1314 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1315 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1316 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1317 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1318 #endif
1319
1320 subseg_set (seg, subseg);
1321 }
1322 }
1323
1324 if (! ECOFF_DEBUGGING)
1325 md_obj_begin ();
1326 }
1327
1328 void
1329 md_mips_end ()
1330 {
1331 if (! ECOFF_DEBUGGING)
1332 md_obj_end ();
1333 }
1334
1335 void
1336 md_assemble (str)
1337 char *str;
1338 {
1339 struct mips_cl_insn insn;
1340 bfd_reloc_code_real_type unused_reloc[3]
1341 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1342
1343 imm_expr.X_op = O_absent;
1344 imm_unmatched_hi = false;
1345 offset_expr.X_op = O_absent;
1346 imm_reloc[0] = BFD_RELOC_UNUSED;
1347 imm_reloc[1] = BFD_RELOC_UNUSED;
1348 imm_reloc[2] = BFD_RELOC_UNUSED;
1349 offset_reloc[0] = BFD_RELOC_UNUSED;
1350 offset_reloc[1] = BFD_RELOC_UNUSED;
1351 offset_reloc[2] = BFD_RELOC_UNUSED;
1352
1353 if (mips_opts.mips16)
1354 mips16_ip (str, &insn);
1355 else
1356 {
1357 mips_ip (str, &insn);
1358 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1359 str, insn.insn_opcode));
1360 }
1361
1362 if (insn_error)
1363 {
1364 as_bad ("%s `%s'", insn_error, str);
1365 return;
1366 }
1367
1368 if (insn.insn_mo->pinfo == INSN_MACRO)
1369 {
1370 if (mips_opts.mips16)
1371 mips16_macro (&insn);
1372 else
1373 macro (&insn);
1374 }
1375 else
1376 {
1377 if (imm_expr.X_op != O_absent)
1378 append_insn ((char *) NULL, &insn, &imm_expr, imm_reloc,
1379 imm_unmatched_hi);
1380 else if (offset_expr.X_op != O_absent)
1381 append_insn ((char *) NULL, &insn, &offset_expr, offset_reloc, false);
1382 else
1383 append_insn ((char *) NULL, &insn, NULL, unused_reloc, false);
1384 }
1385 }
1386
1387 /* See whether instruction IP reads register REG. CLASS is the type
1388 of register. */
1389
1390 static int
1391 insn_uses_reg (ip, reg, class)
1392 struct mips_cl_insn *ip;
1393 unsigned int reg;
1394 enum mips_regclass class;
1395 {
1396 if (class == MIPS16_REG)
1397 {
1398 assert (mips_opts.mips16);
1399 reg = mips16_to_32_reg_map[reg];
1400 class = MIPS_GR_REG;
1401 }
1402
1403 /* Don't report on general register 0, since it never changes. */
1404 if (class == MIPS_GR_REG && reg == 0)
1405 return 0;
1406
1407 if (class == MIPS_FP_REG)
1408 {
1409 assert (! mips_opts.mips16);
1410 /* If we are called with either $f0 or $f1, we must check $f0.
1411 This is not optimal, because it will introduce an unnecessary
1412 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1413 need to distinguish reading both $f0 and $f1 or just one of
1414 them. Note that we don't have to check the other way,
1415 because there is no instruction that sets both $f0 and $f1
1416 and requires a delay. */
1417 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1418 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1419 == (reg &~ (unsigned) 1)))
1420 return 1;
1421 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1422 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1423 == (reg &~ (unsigned) 1)))
1424 return 1;
1425 }
1426 else if (! mips_opts.mips16)
1427 {
1428 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1429 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1430 return 1;
1431 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1432 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1433 return 1;
1434 }
1435 else
1436 {
1437 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1438 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1439 & MIPS16OP_MASK_RX)]
1440 == reg))
1441 return 1;
1442 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1443 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1444 & MIPS16OP_MASK_RY)]
1445 == reg))
1446 return 1;
1447 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1448 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1449 & MIPS16OP_MASK_MOVE32Z)]
1450 == reg))
1451 return 1;
1452 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1453 return 1;
1454 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1455 return 1;
1456 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1457 return 1;
1458 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1459 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1460 & MIPS16OP_MASK_REGR32) == reg)
1461 return 1;
1462 }
1463
1464 return 0;
1465 }
1466
1467 /* This function returns true if modifying a register requires a
1468 delay. */
1469
1470 static int
1471 reg_needs_delay (reg)
1472 unsigned int reg;
1473 {
1474 unsigned long prev_pinfo;
1475
1476 prev_pinfo = prev_insn.insn_mo->pinfo;
1477 if (! mips_opts.noreorder
1478 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1479 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1480 || (! gpr_interlocks
1481 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1482 {
1483 /* A load from a coprocessor or from memory. All load
1484 delays delay the use of general register rt for one
1485 instruction on the r3000. The r6000 and r4000 use
1486 interlocks. */
1487 /* Itbl support may require additional care here. */
1488 know (prev_pinfo & INSN_WRITE_GPR_T);
1489 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1490 return 1;
1491 }
1492
1493 return 0;
1494 }
1495
1496 /* Mark instruction labels in mips16 mode. This permits the linker to
1497 handle them specially, such as generating jalx instructions when
1498 needed. We also make them odd for the duration of the assembly, in
1499 order to generate the right sort of code. We will make them even
1500 in the adjust_symtab routine, while leaving them marked. This is
1501 convenient for the debugger and the disassembler. The linker knows
1502 to make them odd again. */
1503
1504 static void
1505 mips16_mark_labels ()
1506 {
1507 if (mips_opts.mips16)
1508 {
1509 struct insn_label_list *l;
1510 valueT val;
1511
1512 for (l = insn_labels; l != NULL; l = l->next)
1513 {
1514 #ifdef OBJ_ELF
1515 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1516 S_SET_OTHER (l->label, STO_MIPS16);
1517 #endif
1518 val = S_GET_VALUE (l->label);
1519 if ((val & 1) == 0)
1520 S_SET_VALUE (l->label, val + 1);
1521 }
1522 }
1523 }
1524
1525 /* Output an instruction. PLACE is where to put the instruction; if
1526 it is NULL, this uses frag_more to get room. IP is the instruction
1527 information. ADDRESS_EXPR is an operand of the instruction to be
1528 used with RELOC_TYPE. */
1529
1530 static void
1531 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1532 char *place;
1533 struct mips_cl_insn *ip;
1534 expressionS *address_expr;
1535 bfd_reloc_code_real_type *reloc_type;
1536 boolean unmatched_hi;
1537 {
1538 register unsigned long prev_pinfo, pinfo;
1539 char *f;
1540 fixS *fixp[3];
1541 int nops = 0;
1542
1543 /* Mark instruction labels in mips16 mode. */
1544 if (mips_opts.mips16)
1545 mips16_mark_labels ();
1546
1547 prev_pinfo = prev_insn.insn_mo->pinfo;
1548 pinfo = ip->insn_mo->pinfo;
1549
1550 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1551 {
1552 int prev_prev_nop;
1553
1554 /* If the previous insn required any delay slots, see if we need
1555 to insert a NOP or two. There are eight kinds of possible
1556 hazards, of which an instruction can have at most one type.
1557 (1) a load from memory delay
1558 (2) a load from a coprocessor delay
1559 (3) an unconditional branch delay
1560 (4) a conditional branch delay
1561 (5) a move to coprocessor register delay
1562 (6) a load coprocessor register from memory delay
1563 (7) a coprocessor condition code delay
1564 (8) a HI/LO special register delay
1565
1566 There are a lot of optimizations we could do that we don't.
1567 In particular, we do not, in general, reorder instructions.
1568 If you use gcc with optimization, it will reorder
1569 instructions and generally do much more optimization then we
1570 do here; repeating all that work in the assembler would only
1571 benefit hand written assembly code, and does not seem worth
1572 it. */
1573
1574 /* This is how a NOP is emitted. */
1575 #define emit_nop() \
1576 (mips_opts.mips16 \
1577 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1578 : md_number_to_chars (frag_more (4), 0, 4))
1579
1580 /* The previous insn might require a delay slot, depending upon
1581 the contents of the current insn. */
1582 if (! mips_opts.mips16
1583 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1584 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1585 && ! cop_interlocks)
1586 || (! gpr_interlocks
1587 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1588 {
1589 /* A load from a coprocessor or from memory. All load
1590 delays delay the use of general register rt for one
1591 instruction on the r3000. The r6000 and r4000 use
1592 interlocks. */
1593 /* Itbl support may require additional care here. */
1594 know (prev_pinfo & INSN_WRITE_GPR_T);
1595 if (mips_optimize == 0
1596 || insn_uses_reg (ip,
1597 ((prev_insn.insn_opcode >> OP_SH_RT)
1598 & OP_MASK_RT),
1599 MIPS_GR_REG))
1600 ++nops;
1601 }
1602 else if (! mips_opts.mips16
1603 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1604 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1605 && ! cop_interlocks)
1606 || (mips_opts.isa == ISA_MIPS1
1607 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1608 {
1609 /* A generic coprocessor delay. The previous instruction
1610 modified a coprocessor general or control register. If
1611 it modified a control register, we need to avoid any
1612 coprocessor instruction (this is probably not always
1613 required, but it sometimes is). If it modified a general
1614 register, we avoid using that register.
1615
1616 On the r6000 and r4000 loading a coprocessor register
1617 from memory is interlocked, and does not require a delay.
1618
1619 This case is not handled very well. There is no special
1620 knowledge of CP0 handling, and the coprocessors other
1621 than the floating point unit are not distinguished at
1622 all. */
1623 /* Itbl support may require additional care here. FIXME!
1624 Need to modify this to include knowledge about
1625 user specified delays! */
1626 if (prev_pinfo & INSN_WRITE_FPR_T)
1627 {
1628 if (mips_optimize == 0
1629 || insn_uses_reg (ip,
1630 ((prev_insn.insn_opcode >> OP_SH_FT)
1631 & OP_MASK_FT),
1632 MIPS_FP_REG))
1633 ++nops;
1634 }
1635 else if (prev_pinfo & INSN_WRITE_FPR_S)
1636 {
1637 if (mips_optimize == 0
1638 || insn_uses_reg (ip,
1639 ((prev_insn.insn_opcode >> OP_SH_FS)
1640 & OP_MASK_FS),
1641 MIPS_FP_REG))
1642 ++nops;
1643 }
1644 else
1645 {
1646 /* We don't know exactly what the previous instruction
1647 does. If the current instruction uses a coprocessor
1648 register, we must insert a NOP. If previous
1649 instruction may set the condition codes, and the
1650 current instruction uses them, we must insert two
1651 NOPS. */
1652 /* Itbl support may require additional care here. */
1653 if (mips_optimize == 0
1654 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1655 && (pinfo & INSN_READ_COND_CODE)))
1656 nops += 2;
1657 else if (pinfo & INSN_COP)
1658 ++nops;
1659 }
1660 }
1661 else if (! mips_opts.mips16
1662 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1663 && (prev_pinfo & INSN_WRITE_COND_CODE)
1664 && ! cop_interlocks)
1665 {
1666 /* The previous instruction sets the coprocessor condition
1667 codes, but does not require a general coprocessor delay
1668 (this means it is a floating point comparison
1669 instruction). If this instruction uses the condition
1670 codes, we need to insert a single NOP. */
1671 /* Itbl support may require additional care here. */
1672 if (mips_optimize == 0
1673 || (pinfo & INSN_READ_COND_CODE))
1674 ++nops;
1675 }
1676
1677 /* If we're fixing up mfhi/mflo for the r7000 and the
1678 previous insn was an mfhi/mflo and the current insn
1679 reads the register that the mfhi/mflo wrote to, then
1680 insert two nops. */
1681
1682 else if (mips_7000_hilo_fix
1683 && MF_HILO_INSN (prev_pinfo)
1684 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1685 & OP_MASK_RD),
1686 MIPS_GR_REG))
1687 {
1688 nops += 2;
1689 }
1690
1691 /* If we're fixing up mfhi/mflo for the r7000 and the
1692 2nd previous insn was an mfhi/mflo and the current insn
1693 reads the register that the mfhi/mflo wrote to, then
1694 insert one nop. */
1695
1696 else if (mips_7000_hilo_fix
1697 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1698 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1699 & OP_MASK_RD),
1700 MIPS_GR_REG))
1701
1702 {
1703 nops += 1;
1704 }
1705
1706 else if (prev_pinfo & INSN_READ_LO)
1707 {
1708 /* The previous instruction reads the LO register; if the
1709 current instruction writes to the LO register, we must
1710 insert two NOPS. Some newer processors have interlocks.
1711 Also the tx39's multiply instructions can be exectuted
1712 immediatly after a read from HI/LO (without the delay),
1713 though the tx39's divide insns still do require the
1714 delay. */
1715 if (! (hilo_interlocks
1716 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1717 && (mips_optimize == 0
1718 || (pinfo & INSN_WRITE_LO)))
1719 nops += 2;
1720 /* Most mips16 branch insns don't have a delay slot.
1721 If a read from LO is immediately followed by a branch
1722 to a write to LO we have a read followed by a write
1723 less than 2 insns away. We assume the target of
1724 a branch might be a write to LO, and insert a nop
1725 between a read and an immediately following branch. */
1726 else if (mips_opts.mips16
1727 && (mips_optimize == 0
1728 || (pinfo & MIPS16_INSN_BRANCH)))
1729 nops += 1;
1730 }
1731 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1732 {
1733 /* The previous instruction reads the HI register; if the
1734 current instruction writes to the HI register, we must
1735 insert a NOP. Some newer processors have interlocks.
1736 Also the note tx39's multiply above. */
1737 if (! (hilo_interlocks
1738 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1739 && (mips_optimize == 0
1740 || (pinfo & INSN_WRITE_HI)))
1741 nops += 2;
1742 /* Most mips16 branch insns don't have a delay slot.
1743 If a read from HI is immediately followed by a branch
1744 to a write to HI we have a read followed by a write
1745 less than 2 insns away. We assume the target of
1746 a branch might be a write to HI, and insert a nop
1747 between a read and an immediately following branch. */
1748 else if (mips_opts.mips16
1749 && (mips_optimize == 0
1750 || (pinfo & MIPS16_INSN_BRANCH)))
1751 nops += 1;
1752 }
1753
1754 /* If the previous instruction was in a noreorder section, then
1755 we don't want to insert the nop after all. */
1756 /* Itbl support may require additional care here. */
1757 if (prev_insn_unreordered)
1758 nops = 0;
1759
1760 /* There are two cases which require two intervening
1761 instructions: 1) setting the condition codes using a move to
1762 coprocessor instruction which requires a general coprocessor
1763 delay and then reading the condition codes 2) reading the HI
1764 or LO register and then writing to it (except on processors
1765 which have interlocks). If we are not already emitting a NOP
1766 instruction, we must check for these cases compared to the
1767 instruction previous to the previous instruction. */
1768 if ((! mips_opts.mips16
1769 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1770 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1771 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1772 && (pinfo & INSN_READ_COND_CODE)
1773 && ! cop_interlocks)
1774 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1775 && (pinfo & INSN_WRITE_LO)
1776 && ! (hilo_interlocks
1777 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1778 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1779 && (pinfo & INSN_WRITE_HI)
1780 && ! (hilo_interlocks
1781 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1782 prev_prev_nop = 1;
1783 else
1784 prev_prev_nop = 0;
1785
1786 if (prev_prev_insn_unreordered)
1787 prev_prev_nop = 0;
1788
1789 if (prev_prev_nop && nops == 0)
1790 ++nops;
1791
1792 /* If we are being given a nop instruction, don't bother with
1793 one of the nops we would otherwise output. This will only
1794 happen when a nop instruction is used with mips_optimize set
1795 to 0. */
1796 if (nops > 0
1797 && ! mips_opts.noreorder
1798 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1799 --nops;
1800
1801 /* Now emit the right number of NOP instructions. */
1802 if (nops > 0 && ! mips_opts.noreorder)
1803 {
1804 fragS *old_frag;
1805 unsigned long old_frag_offset;
1806 int i;
1807 struct insn_label_list *l;
1808
1809 old_frag = frag_now;
1810 old_frag_offset = frag_now_fix ();
1811
1812 for (i = 0; i < nops; i++)
1813 emit_nop ();
1814
1815 if (listing)
1816 {
1817 listing_prev_line ();
1818 /* We may be at the start of a variant frag. In case we
1819 are, make sure there is enough space for the frag
1820 after the frags created by listing_prev_line. The
1821 argument to frag_grow here must be at least as large
1822 as the argument to all other calls to frag_grow in
1823 this file. We don't have to worry about being in the
1824 middle of a variant frag, because the variants insert
1825 all needed nop instructions themselves. */
1826 frag_grow (40);
1827 }
1828
1829 for (l = insn_labels; l != NULL; l = l->next)
1830 {
1831 valueT val;
1832
1833 assert (S_GET_SEGMENT (l->label) == now_seg);
1834 symbol_set_frag (l->label, frag_now);
1835 val = (valueT) frag_now_fix ();
1836 /* mips16 text labels are stored as odd. */
1837 if (mips_opts.mips16)
1838 val += 1;
1839 S_SET_VALUE (l->label, val);
1840 }
1841
1842 #ifndef NO_ECOFF_DEBUGGING
1843 if (ECOFF_DEBUGGING)
1844 ecoff_fix_loc (old_frag, old_frag_offset);
1845 #endif
1846 }
1847 else if (prev_nop_frag != NULL)
1848 {
1849 /* We have a frag holding nops we may be able to remove. If
1850 we don't need any nops, we can decrease the size of
1851 prev_nop_frag by the size of one instruction. If we do
1852 need some nops, we count them in prev_nops_required. */
1853 if (prev_nop_frag_since == 0)
1854 {
1855 if (nops == 0)
1856 {
1857 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1858 --prev_nop_frag_holds;
1859 }
1860 else
1861 prev_nop_frag_required += nops;
1862 }
1863 else
1864 {
1865 if (prev_prev_nop == 0)
1866 {
1867 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1868 --prev_nop_frag_holds;
1869 }
1870 else
1871 ++prev_nop_frag_required;
1872 }
1873
1874 if (prev_nop_frag_holds <= prev_nop_frag_required)
1875 prev_nop_frag = NULL;
1876
1877 ++prev_nop_frag_since;
1878
1879 /* Sanity check: by the time we reach the second instruction
1880 after prev_nop_frag, we should have used up all the nops
1881 one way or another. */
1882 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1883 }
1884 }
1885
1886 if (*reloc_type > BFD_RELOC_UNUSED)
1887 {
1888 /* We need to set up a variant frag. */
1889 assert (mips_opts.mips16 && address_expr != NULL);
1890 f = frag_var (rs_machine_dependent, 4, 0,
1891 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1892 mips16_small, mips16_ext,
1893 (prev_pinfo
1894 & INSN_UNCOND_BRANCH_DELAY),
1895 (*prev_insn_reloc_type
1896 == BFD_RELOC_MIPS16_JMP)),
1897 make_expr_symbol (address_expr), (offsetT) 0,
1898 (char *) NULL);
1899 }
1900 else if (place != NULL)
1901 f = place;
1902 else if (mips_opts.mips16
1903 && ! ip->use_extend
1904 && *reloc_type != BFD_RELOC_MIPS16_JMP)
1905 {
1906 /* Make sure there is enough room to swap this instruction with
1907 a following jump instruction. */
1908 frag_grow (6);
1909 f = frag_more (2);
1910 }
1911 else
1912 {
1913 if (mips_opts.mips16
1914 && mips_opts.noreorder
1915 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1916 as_warn (_("extended instruction in delay slot"));
1917
1918 f = frag_more (4);
1919 }
1920
1921 fixp[0] = fixp[1] = fixp[2] = NULL;
1922 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1923 {
1924 if (address_expr->X_op == O_constant)
1925 {
1926 unsigned long tmp;
1927
1928 switch (*reloc_type)
1929 {
1930 case BFD_RELOC_32:
1931 ip->insn_opcode |= address_expr->X_add_number;
1932 break;
1933
1934 case BFD_RELOC_MIPS_HIGHEST:
1935 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1936 tmp >>= 16;
1937 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1938 break;
1939
1940 case BFD_RELOC_MIPS_HIGHER:
1941 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1942 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1943 break;
1944
1945 case BFD_RELOC_HI16_S:
1946 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1947 >> 16) & 0xffff;
1948 break;
1949
1950 case BFD_RELOC_HI16:
1951 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1952 break;
1953
1954 case BFD_RELOC_LO16:
1955 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1956 break;
1957
1958 case BFD_RELOC_MIPS_JMP:
1959 if ((address_expr->X_add_number & 3) != 0)
1960 as_bad (_("jump to misaligned address (0x%lx)"),
1961 (unsigned long) address_expr->X_add_number);
1962 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1963 break;
1964
1965 case BFD_RELOC_MIPS16_JMP:
1966 if ((address_expr->X_add_number & 3) != 0)
1967 as_bad (_("jump to misaligned address (0x%lx)"),
1968 (unsigned long) address_expr->X_add_number);
1969 ip->insn_opcode |=
1970 (((address_expr->X_add_number & 0x7c0000) << 3)
1971 | ((address_expr->X_add_number & 0xf800000) >> 7)
1972 | ((address_expr->X_add_number & 0x3fffc) >> 2));
1973 break;
1974
1975 case BFD_RELOC_16_PCREL:
1976 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1977 break;
1978
1979 case BFD_RELOC_16_PCREL_S2:
1980 goto need_reloc;
1981
1982 default:
1983 internalError ();
1984 }
1985 }
1986 else
1987 {
1988 need_reloc:
1989 /* Don't generate a reloc if we are writing into a variant frag. */
1990 if (place == NULL)
1991 {
1992 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1993 address_expr,
1994 (*reloc_type == BFD_RELOC_16_PCREL
1995 || *reloc_type == BFD_RELOC_16_PCREL_S2),
1996 reloc_type[0]);
1997
1998 /* These relocations can have an addend that won't fit in
1999 4 octets for 64bit assembly. */
2000 if (HAVE_64BIT_GPRS &&
2001 (*reloc_type == BFD_RELOC_16
2002 || *reloc_type == BFD_RELOC_32
2003 || *reloc_type == BFD_RELOC_MIPS_JMP
2004 || *reloc_type == BFD_RELOC_HI16_S
2005 || *reloc_type == BFD_RELOC_LO16
2006 || *reloc_type == BFD_RELOC_GPREL16
2007 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2008 || *reloc_type == BFD_RELOC_GPREL32
2009 || *reloc_type == BFD_RELOC_64
2010 || *reloc_type == BFD_RELOC_CTOR
2011 || *reloc_type == BFD_RELOC_MIPS_SUB
2012 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2013 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2014 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2015 || *reloc_type == BFD_RELOC_MIPS_REL16
2016 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2017 fixp[0]->fx_no_overflow = 1;
2018
2019 if (unmatched_hi)
2020 {
2021 struct mips_hi_fixup *hi_fixup;
2022
2023 assert (*reloc_type == BFD_RELOC_HI16_S);
2024 hi_fixup = ((struct mips_hi_fixup *)
2025 xmalloc (sizeof (struct mips_hi_fixup)));
2026 hi_fixup->fixp = fixp[0];
2027 hi_fixup->seg = now_seg;
2028 hi_fixup->next = mips_hi_fixup_list;
2029 mips_hi_fixup_list = hi_fixup;
2030 }
2031
2032 if (reloc_type[1] != BFD_RELOC_UNUSED)
2033 {
2034 /* FIXME: This symbol can be one of
2035 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
2036 address_expr->X_op = O_absent;
2037 address_expr->X_add_symbol = 0;
2038 address_expr->X_add_number = 0;
2039
2040 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2041 4, address_expr, false,
2042 reloc_type[1]);
2043
2044 /* These relocations can have an addend that won't fit in
2045 4 octets for 64bit assembly. */
2046 if (HAVE_64BIT_GPRS &&
2047 (*reloc_type == BFD_RELOC_16
2048 || *reloc_type == BFD_RELOC_32
2049 || *reloc_type == BFD_RELOC_MIPS_JMP
2050 || *reloc_type == BFD_RELOC_HI16_S
2051 || *reloc_type == BFD_RELOC_LO16
2052 || *reloc_type == BFD_RELOC_GPREL16
2053 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2054 || *reloc_type == BFD_RELOC_GPREL32
2055 || *reloc_type == BFD_RELOC_64
2056 || *reloc_type == BFD_RELOC_CTOR
2057 || *reloc_type == BFD_RELOC_MIPS_SUB
2058 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2059 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2060 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2061 || *reloc_type == BFD_RELOC_MIPS_REL16
2062 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2063 fixp[1]->fx_no_overflow = 1;
2064
2065 if (reloc_type[2] != BFD_RELOC_UNUSED)
2066 {
2067 address_expr->X_op = O_absent;
2068 address_expr->X_add_symbol = 0;
2069 address_expr->X_add_number = 0;
2070
2071 fixp[2] = fix_new_exp (frag_now,
2072 f - frag_now->fr_literal, 4,
2073 address_expr, false,
2074 reloc_type[2]);
2075
2076 /* These relocations can have an addend that won't fit in
2077 4 octets for 64bit assembly. */
2078 if (HAVE_64BIT_GPRS &&
2079 (*reloc_type == BFD_RELOC_16
2080 || *reloc_type == BFD_RELOC_32
2081 || *reloc_type == BFD_RELOC_MIPS_JMP
2082 || *reloc_type == BFD_RELOC_HI16_S
2083 || *reloc_type == BFD_RELOC_LO16
2084 || *reloc_type == BFD_RELOC_GPREL16
2085 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2086 || *reloc_type == BFD_RELOC_GPREL32
2087 || *reloc_type == BFD_RELOC_64
2088 || *reloc_type == BFD_RELOC_CTOR
2089 || *reloc_type == BFD_RELOC_MIPS_SUB
2090 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2091 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2092 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2093 || *reloc_type == BFD_RELOC_MIPS_REL16
2094 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2095 fixp[2]->fx_no_overflow = 1;
2096 }
2097 }
2098 }
2099 }
2100 }
2101
2102 if (! mips_opts.mips16)
2103 md_number_to_chars (f, ip->insn_opcode, 4);
2104 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2105 {
2106 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2107 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2108 }
2109 else
2110 {
2111 if (ip->use_extend)
2112 {
2113 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2114 f += 2;
2115 }
2116 md_number_to_chars (f, ip->insn_opcode, 2);
2117 }
2118
2119 /* Update the register mask information. */
2120 if (! mips_opts.mips16)
2121 {
2122 if (pinfo & INSN_WRITE_GPR_D)
2123 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2124 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2125 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2126 if (pinfo & INSN_READ_GPR_S)
2127 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2128 if (pinfo & INSN_WRITE_GPR_31)
2129 mips_gprmask |= 1 << 31;
2130 if (pinfo & INSN_WRITE_FPR_D)
2131 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2132 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2133 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2134 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2135 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2136 if ((pinfo & INSN_READ_FPR_R) != 0)
2137 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2138 if (pinfo & INSN_COP)
2139 {
2140 /* We don't keep enough information to sort these cases out.
2141 The itbl support does keep this information however, although
2142 we currently don't support itbl fprmats as part of the cop
2143 instruction. May want to add this support in the future. */
2144 }
2145 /* Never set the bit for $0, which is always zero. */
2146 mips_gprmask &= ~1 << 0;
2147 }
2148 else
2149 {
2150 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2151 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2152 & MIPS16OP_MASK_RX);
2153 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2154 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2155 & MIPS16OP_MASK_RY);
2156 if (pinfo & MIPS16_INSN_WRITE_Z)
2157 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2158 & MIPS16OP_MASK_RZ);
2159 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2160 mips_gprmask |= 1 << TREG;
2161 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2162 mips_gprmask |= 1 << SP;
2163 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2164 mips_gprmask |= 1 << RA;
2165 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2166 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2167 if (pinfo & MIPS16_INSN_READ_Z)
2168 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2169 & MIPS16OP_MASK_MOVE32Z);
2170 if (pinfo & MIPS16_INSN_READ_GPR_X)
2171 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2172 & MIPS16OP_MASK_REGR32);
2173 }
2174
2175 if (place == NULL && ! mips_opts.noreorder)
2176 {
2177 /* Filling the branch delay slot is more complex. We try to
2178 switch the branch with the previous instruction, which we can
2179 do if the previous instruction does not set up a condition
2180 that the branch tests and if the branch is not itself the
2181 target of any branch. */
2182 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2183 || (pinfo & INSN_COND_BRANCH_DELAY))
2184 {
2185 if (mips_optimize < 2
2186 /* If we have seen .set volatile or .set nomove, don't
2187 optimize. */
2188 || mips_opts.nomove != 0
2189 /* If we had to emit any NOP instructions, then we
2190 already know we can not swap. */
2191 || nops != 0
2192 /* If we don't even know the previous insn, we can not
2193 swap. */
2194 || ! prev_insn_valid
2195 /* If the previous insn is already in a branch delay
2196 slot, then we can not swap. */
2197 || prev_insn_is_delay_slot
2198 /* If the previous previous insn was in a .set
2199 noreorder, we can't swap. Actually, the MIPS
2200 assembler will swap in this situation. However, gcc
2201 configured -with-gnu-as will generate code like
2202 .set noreorder
2203 lw $4,XXX
2204 .set reorder
2205 INSN
2206 bne $4,$0,foo
2207 in which we can not swap the bne and INSN. If gcc is
2208 not configured -with-gnu-as, it does not output the
2209 .set pseudo-ops. We don't have to check
2210 prev_insn_unreordered, because prev_insn_valid will
2211 be 0 in that case. We don't want to use
2212 prev_prev_insn_valid, because we do want to be able
2213 to swap at the start of a function. */
2214 || prev_prev_insn_unreordered
2215 /* If the branch is itself the target of a branch, we
2216 can not swap. We cheat on this; all we check for is
2217 whether there is a label on this instruction. If
2218 there are any branches to anything other than a
2219 label, users must use .set noreorder. */
2220 || insn_labels != NULL
2221 /* If the previous instruction is in a variant frag, we
2222 can not do the swap. This does not apply to the
2223 mips16, which uses variant frags for different
2224 purposes. */
2225 || (! mips_opts.mips16
2226 && prev_insn_frag->fr_type == rs_machine_dependent)
2227 /* If the branch reads the condition codes, we don't
2228 even try to swap, because in the sequence
2229 ctc1 $X,$31
2230 INSN
2231 INSN
2232 bc1t LABEL
2233 we can not swap, and I don't feel like handling that
2234 case. */
2235 || (! mips_opts.mips16
2236 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2237 && (pinfo & INSN_READ_COND_CODE))
2238 /* We can not swap with an instruction that requires a
2239 delay slot, becase the target of the branch might
2240 interfere with that instruction. */
2241 || (! mips_opts.mips16
2242 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2243 && (prev_pinfo
2244 /* Itbl support may require additional care here. */
2245 & (INSN_LOAD_COPROC_DELAY
2246 | INSN_COPROC_MOVE_DELAY
2247 | INSN_WRITE_COND_CODE)))
2248 || (! (hilo_interlocks
2249 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2250 && (prev_pinfo
2251 & (INSN_READ_LO
2252 | INSN_READ_HI)))
2253 || (! mips_opts.mips16
2254 && ! gpr_interlocks
2255 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2256 || (! mips_opts.mips16
2257 && mips_opts.isa == ISA_MIPS1
2258 /* Itbl support may require additional care here. */
2259 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2260 /* We can not swap with a branch instruction. */
2261 || (prev_pinfo
2262 & (INSN_UNCOND_BRANCH_DELAY
2263 | INSN_COND_BRANCH_DELAY
2264 | INSN_COND_BRANCH_LIKELY))
2265 /* We do not swap with a trap instruction, since it
2266 complicates trap handlers to have the trap
2267 instruction be in a delay slot. */
2268 || (prev_pinfo & INSN_TRAP)
2269 /* If the branch reads a register that the previous
2270 instruction sets, we can not swap. */
2271 || (! mips_opts.mips16
2272 && (prev_pinfo & INSN_WRITE_GPR_T)
2273 && insn_uses_reg (ip,
2274 ((prev_insn.insn_opcode >> OP_SH_RT)
2275 & OP_MASK_RT),
2276 MIPS_GR_REG))
2277 || (! mips_opts.mips16
2278 && (prev_pinfo & INSN_WRITE_GPR_D)
2279 && insn_uses_reg (ip,
2280 ((prev_insn.insn_opcode >> OP_SH_RD)
2281 & OP_MASK_RD),
2282 MIPS_GR_REG))
2283 || (mips_opts.mips16
2284 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2285 && insn_uses_reg (ip,
2286 ((prev_insn.insn_opcode
2287 >> MIPS16OP_SH_RX)
2288 & MIPS16OP_MASK_RX),
2289 MIPS16_REG))
2290 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2291 && insn_uses_reg (ip,
2292 ((prev_insn.insn_opcode
2293 >> MIPS16OP_SH_RY)
2294 & MIPS16OP_MASK_RY),
2295 MIPS16_REG))
2296 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2297 && insn_uses_reg (ip,
2298 ((prev_insn.insn_opcode
2299 >> MIPS16OP_SH_RZ)
2300 & MIPS16OP_MASK_RZ),
2301 MIPS16_REG))
2302 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2303 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2304 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2305 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2306 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2307 && insn_uses_reg (ip,
2308 MIPS16OP_EXTRACT_REG32R (prev_insn.
2309 insn_opcode),
2310 MIPS_GR_REG))))
2311 /* If the branch writes a register that the previous
2312 instruction sets, we can not swap (we know that
2313 branches write only to RD or to $31). */
2314 || (! mips_opts.mips16
2315 && (prev_pinfo & INSN_WRITE_GPR_T)
2316 && (((pinfo & INSN_WRITE_GPR_D)
2317 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2318 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2319 || ((pinfo & INSN_WRITE_GPR_31)
2320 && (((prev_insn.insn_opcode >> OP_SH_RT)
2321 & OP_MASK_RT)
2322 == 31))))
2323 || (! mips_opts.mips16
2324 && (prev_pinfo & INSN_WRITE_GPR_D)
2325 && (((pinfo & INSN_WRITE_GPR_D)
2326 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2327 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2328 || ((pinfo & INSN_WRITE_GPR_31)
2329 && (((prev_insn.insn_opcode >> OP_SH_RD)
2330 & OP_MASK_RD)
2331 == 31))))
2332 || (mips_opts.mips16
2333 && (pinfo & MIPS16_INSN_WRITE_31)
2334 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2335 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2336 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2337 == RA))))
2338 /* If the branch writes a register that the previous
2339 instruction reads, we can not swap (we know that
2340 branches only write to RD or to $31). */
2341 || (! mips_opts.mips16
2342 && (pinfo & INSN_WRITE_GPR_D)
2343 && insn_uses_reg (&prev_insn,
2344 ((ip->insn_opcode >> OP_SH_RD)
2345 & OP_MASK_RD),
2346 MIPS_GR_REG))
2347 || (! mips_opts.mips16
2348 && (pinfo & INSN_WRITE_GPR_31)
2349 && insn_uses_reg (&prev_insn, 31, MIPS_GR_REG))
2350 || (mips_opts.mips16
2351 && (pinfo & MIPS16_INSN_WRITE_31)
2352 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2353 /* If we are generating embedded PIC code, the branch
2354 might be expanded into a sequence which uses $at, so
2355 we can't swap with an instruction which reads it. */
2356 || (mips_pic == EMBEDDED_PIC
2357 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2358 /* If the previous previous instruction has a load
2359 delay, and sets a register that the branch reads, we
2360 can not swap. */
2361 || (! mips_opts.mips16
2362 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2363 /* Itbl support may require additional care here. */
2364 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2365 || (! gpr_interlocks
2366 && (prev_prev_insn.insn_mo->pinfo
2367 & INSN_LOAD_MEMORY_DELAY)))
2368 && insn_uses_reg (ip,
2369 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2370 & OP_MASK_RT),
2371 MIPS_GR_REG))
2372 /* If one instruction sets a condition code and the
2373 other one uses a condition code, we can not swap. */
2374 || ((pinfo & INSN_READ_COND_CODE)
2375 && (prev_pinfo & INSN_WRITE_COND_CODE))
2376 || ((pinfo & INSN_WRITE_COND_CODE)
2377 && (prev_pinfo & INSN_READ_COND_CODE))
2378 /* If the previous instruction uses the PC, we can not
2379 swap. */
2380 || (mips_opts.mips16
2381 && (prev_pinfo & MIPS16_INSN_READ_PC))
2382 /* If the previous instruction was extended, we can not
2383 swap. */
2384 || (mips_opts.mips16 && prev_insn_extended)
2385 /* If the previous instruction had a fixup in mips16
2386 mode, we can not swap. This normally means that the
2387 previous instruction was a 4 byte branch anyhow. */
2388 || (mips_opts.mips16 && prev_insn_fixp[0])
2389 /* If the previous instruction is a sync, sync.l, or
2390 sync.p, we can not swap. */
2391 || (prev_pinfo & INSN_SYNC))
2392 {
2393 /* We could do even better for unconditional branches to
2394 portions of this object file; we could pick up the
2395 instruction at the destination, put it in the delay
2396 slot, and bump the destination address. */
2397 emit_nop ();
2398 /* Update the previous insn information. */
2399 prev_prev_insn = *ip;
2400 prev_insn.insn_mo = &dummy_opcode;
2401 }
2402 else
2403 {
2404 /* It looks like we can actually do the swap. */
2405 if (! mips_opts.mips16)
2406 {
2407 char *prev_f;
2408 char temp[4];
2409
2410 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2411 memcpy (temp, prev_f, 4);
2412 memcpy (prev_f, f, 4);
2413 memcpy (f, temp, 4);
2414 if (prev_insn_fixp[0])
2415 {
2416 prev_insn_fixp[0]->fx_frag = frag_now;
2417 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2418 }
2419 if (prev_insn_fixp[1])
2420 {
2421 prev_insn_fixp[1]->fx_frag = frag_now;
2422 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2423 }
2424 if (prev_insn_fixp[2])
2425 {
2426 prev_insn_fixp[2]->fx_frag = frag_now;
2427 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2428 }
2429 if (fixp[0])
2430 {
2431 fixp[0]->fx_frag = prev_insn_frag;
2432 fixp[0]->fx_where = prev_insn_where;
2433 }
2434 if (fixp[1])
2435 {
2436 fixp[1]->fx_frag = prev_insn_frag;
2437 fixp[1]->fx_where = prev_insn_where;
2438 }
2439 if (fixp[2])
2440 {
2441 fixp[2]->fx_frag = prev_insn_frag;
2442 fixp[2]->fx_where = prev_insn_where;
2443 }
2444 }
2445 else
2446 {
2447 char *prev_f;
2448 char temp[2];
2449
2450 assert (prev_insn_fixp[0] == NULL);
2451 assert (prev_insn_fixp[1] == NULL);
2452 assert (prev_insn_fixp[2] == NULL);
2453 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2454 memcpy (temp, prev_f, 2);
2455 memcpy (prev_f, f, 2);
2456 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2457 {
2458 assert (*reloc_type == BFD_RELOC_UNUSED);
2459 memcpy (f, temp, 2);
2460 }
2461 else
2462 {
2463 memcpy (f, f + 2, 2);
2464 memcpy (f + 2, temp, 2);
2465 }
2466 if (fixp[0])
2467 {
2468 fixp[0]->fx_frag = prev_insn_frag;
2469 fixp[0]->fx_where = prev_insn_where;
2470 }
2471 if (fixp[1])
2472 {
2473 fixp[1]->fx_frag = prev_insn_frag;
2474 fixp[1]->fx_where = prev_insn_where;
2475 }
2476 if (fixp[2])
2477 {
2478 fixp[2]->fx_frag = prev_insn_frag;
2479 fixp[2]->fx_where = prev_insn_where;
2480 }
2481 }
2482
2483 /* Update the previous insn information; leave prev_insn
2484 unchanged. */
2485 prev_prev_insn = *ip;
2486 }
2487 prev_insn_is_delay_slot = 1;
2488
2489 /* If that was an unconditional branch, forget the previous
2490 insn information. */
2491 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2492 {
2493 prev_prev_insn.insn_mo = &dummy_opcode;
2494 prev_insn.insn_mo = &dummy_opcode;
2495 }
2496
2497 prev_insn_fixp[0] = NULL;
2498 prev_insn_fixp[1] = NULL;
2499 prev_insn_fixp[2] = NULL;
2500 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2501 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2502 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2503 prev_insn_extended = 0;
2504 }
2505 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2506 {
2507 /* We don't yet optimize a branch likely. What we should do
2508 is look at the target, copy the instruction found there
2509 into the delay slot, and increment the branch to jump to
2510 the next instruction. */
2511 emit_nop ();
2512 /* Update the previous insn information. */
2513 prev_prev_insn = *ip;
2514 prev_insn.insn_mo = &dummy_opcode;
2515 prev_insn_fixp[0] = NULL;
2516 prev_insn_fixp[1] = NULL;
2517 prev_insn_fixp[2] = NULL;
2518 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2519 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2520 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2521 prev_insn_extended = 0;
2522 }
2523 else
2524 {
2525 /* Update the previous insn information. */
2526 if (nops > 0)
2527 prev_prev_insn.insn_mo = &dummy_opcode;
2528 else
2529 prev_prev_insn = prev_insn;
2530 prev_insn = *ip;
2531
2532 /* Any time we see a branch, we always fill the delay slot
2533 immediately; since this insn is not a branch, we know it
2534 is not in a delay slot. */
2535 prev_insn_is_delay_slot = 0;
2536
2537 prev_insn_fixp[0] = fixp[0];
2538 prev_insn_fixp[1] = fixp[1];
2539 prev_insn_fixp[2] = fixp[2];
2540 prev_insn_reloc_type[0] = reloc_type[0];
2541 prev_insn_reloc_type[1] = reloc_type[1];
2542 prev_insn_reloc_type[2] = reloc_type[2];
2543 if (mips_opts.mips16)
2544 prev_insn_extended = (ip->use_extend
2545 || *reloc_type > BFD_RELOC_UNUSED);
2546 }
2547
2548 prev_prev_insn_unreordered = prev_insn_unreordered;
2549 prev_insn_unreordered = 0;
2550 prev_insn_frag = frag_now;
2551 prev_insn_where = f - frag_now->fr_literal;
2552 prev_insn_valid = 1;
2553 }
2554 else if (place == NULL)
2555 {
2556 /* We need to record a bit of information even when we are not
2557 reordering, in order to determine the base address for mips16
2558 PC relative relocs. */
2559 prev_prev_insn = prev_insn;
2560 prev_insn = *ip;
2561 prev_insn_reloc_type[0] = reloc_type[0];
2562 prev_insn_reloc_type[1] = reloc_type[1];
2563 prev_insn_reloc_type[2] = reloc_type[2];
2564 prev_prev_insn_unreordered = prev_insn_unreordered;
2565 prev_insn_unreordered = 1;
2566 }
2567
2568 /* We just output an insn, so the next one doesn't have a label. */
2569 mips_clear_insn_labels ();
2570
2571 /* We must ensure that a fixup associated with an unmatched %hi
2572 reloc does not become a variant frag. Otherwise, the
2573 rearrangement of %hi relocs in frob_file may confuse
2574 tc_gen_reloc. */
2575 if (unmatched_hi)
2576 {
2577 frag_wane (frag_now);
2578 frag_new (0);
2579 }
2580 }
2581
2582 /* This function forgets that there was any previous instruction or
2583 label. If PRESERVE is non-zero, it remembers enough information to
2584 know whether nops are needed before a noreorder section. */
2585
2586 static void
2587 mips_no_prev_insn (preserve)
2588 int preserve;
2589 {
2590 if (! preserve)
2591 {
2592 prev_insn.insn_mo = &dummy_opcode;
2593 prev_prev_insn.insn_mo = &dummy_opcode;
2594 prev_nop_frag = NULL;
2595 prev_nop_frag_holds = 0;
2596 prev_nop_frag_required = 0;
2597 prev_nop_frag_since = 0;
2598 }
2599 prev_insn_valid = 0;
2600 prev_insn_is_delay_slot = 0;
2601 prev_insn_unreordered = 0;
2602 prev_insn_extended = 0;
2603 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2604 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2605 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2606 prev_prev_insn_unreordered = 0;
2607 mips_clear_insn_labels ();
2608 }
2609
2610 /* This function must be called whenever we turn on noreorder or emit
2611 something other than instructions. It inserts any NOPS which might
2612 be needed by the previous instruction, and clears the information
2613 kept for the previous instructions. The INSNS parameter is true if
2614 instructions are to follow. */
2615
2616 static void
2617 mips_emit_delays (insns)
2618 boolean insns;
2619 {
2620 if (! mips_opts.noreorder)
2621 {
2622 int nops;
2623
2624 nops = 0;
2625 if ((! mips_opts.mips16
2626 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2627 && (! cop_interlocks
2628 && (prev_insn.insn_mo->pinfo
2629 & (INSN_LOAD_COPROC_DELAY
2630 | INSN_COPROC_MOVE_DELAY
2631 | INSN_WRITE_COND_CODE))))
2632 || (! hilo_interlocks
2633 && (prev_insn.insn_mo->pinfo
2634 & (INSN_READ_LO
2635 | INSN_READ_HI)))
2636 || (! mips_opts.mips16
2637 && ! gpr_interlocks
2638 && (prev_insn.insn_mo->pinfo
2639 & INSN_LOAD_MEMORY_DELAY))
2640 || (! mips_opts.mips16
2641 && mips_opts.isa == ISA_MIPS1
2642 && (prev_insn.insn_mo->pinfo
2643 & INSN_COPROC_MEMORY_DELAY)))
2644 {
2645 /* Itbl support may require additional care here. */
2646 ++nops;
2647 if ((! mips_opts.mips16
2648 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2649 && (! cop_interlocks
2650 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2651 || (! hilo_interlocks
2652 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2653 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2654 ++nops;
2655
2656 if (prev_insn_unreordered)
2657 nops = 0;
2658 }
2659 else if ((! mips_opts.mips16
2660 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2661 && (! cop_interlocks
2662 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2663 || (! hilo_interlocks
2664 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2665 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2666 {
2667 /* Itbl support may require additional care here. */
2668 if (! prev_prev_insn_unreordered)
2669 ++nops;
2670 }
2671
2672 if (nops > 0)
2673 {
2674 struct insn_label_list *l;
2675
2676 if (insns)
2677 {
2678 /* Record the frag which holds the nop instructions, so
2679 that we can remove them if we don't need them. */
2680 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2681 prev_nop_frag = frag_now;
2682 prev_nop_frag_holds = nops;
2683 prev_nop_frag_required = 0;
2684 prev_nop_frag_since = 0;
2685 }
2686
2687 for (; nops > 0; --nops)
2688 emit_nop ();
2689
2690 if (insns)
2691 {
2692 /* Move on to a new frag, so that it is safe to simply
2693 decrease the size of prev_nop_frag. */
2694 frag_wane (frag_now);
2695 frag_new (0);
2696 }
2697
2698 for (l = insn_labels; l != NULL; l = l->next)
2699 {
2700 valueT val;
2701
2702 assert (S_GET_SEGMENT (l->label) == now_seg);
2703 symbol_set_frag (l->label, frag_now);
2704 val = (valueT) frag_now_fix ();
2705 /* mips16 text labels are stored as odd. */
2706 if (mips_opts.mips16)
2707 val += 1;
2708 S_SET_VALUE (l->label, val);
2709 }
2710 }
2711 }
2712
2713 /* Mark instruction labels in mips16 mode. */
2714 if (mips_opts.mips16 && insns)
2715 mips16_mark_labels ();
2716
2717 mips_no_prev_insn (insns);
2718 }
2719
2720 /* Build an instruction created by a macro expansion. This is passed
2721 a pointer to the count of instructions created so far, an
2722 expression, the name of the instruction to build, an operand format
2723 string, and corresponding arguments. */
2724
2725 #ifdef USE_STDARG
2726 static void
2727 macro_build (char *place,
2728 int *counter,
2729 expressionS * ep,
2730 const char *name,
2731 const char *fmt,
2732 ...)
2733 #else
2734 static void
2735 macro_build (place, counter, ep, name, fmt, va_alist)
2736 char *place;
2737 int *counter;
2738 expressionS *ep;
2739 const char *name;
2740 const char *fmt;
2741 va_dcl
2742 #endif
2743 {
2744 struct mips_cl_insn insn;
2745 bfd_reloc_code_real_type r[3];
2746 va_list args;
2747
2748 #ifdef USE_STDARG
2749 va_start (args, fmt);
2750 #else
2751 va_start (args);
2752 #endif
2753
2754 /*
2755 * If the macro is about to expand into a second instruction,
2756 * print a warning if needed. We need to pass ip as a parameter
2757 * to generate a better warning message here...
2758 */
2759 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2760 as_warn (_("Macro instruction expanded into multiple instructions"));
2761
2762 if (place == NULL)
2763 *counter += 1; /* bump instruction counter */
2764
2765 if (mips_opts.mips16)
2766 {
2767 mips16_macro_build (place, counter, ep, name, fmt, args);
2768 va_end (args);
2769 return;
2770 }
2771
2772 r[0] = BFD_RELOC_UNUSED;
2773 r[1] = BFD_RELOC_UNUSED;
2774 r[2] = BFD_RELOC_UNUSED;
2775 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2776 assert (insn.insn_mo);
2777 assert (strcmp (name, insn.insn_mo->name) == 0);
2778
2779 /* Search until we get a match for NAME. */
2780 while (1)
2781 {
2782 if (strcmp (fmt, insn.insn_mo->args) == 0
2783 && insn.insn_mo->pinfo != INSN_MACRO
2784 && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
2785 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2786 break;
2787
2788 ++insn.insn_mo;
2789 assert (insn.insn_mo->name);
2790 assert (strcmp (name, insn.insn_mo->name) == 0);
2791 }
2792
2793 insn.insn_opcode = insn.insn_mo->match;
2794 for (;;)
2795 {
2796 switch (*fmt++)
2797 {
2798 case '\0':
2799 break;
2800
2801 case ',':
2802 case '(':
2803 case ')':
2804 continue;
2805
2806 case 't':
2807 case 'w':
2808 case 'E':
2809 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2810 continue;
2811
2812 case 'c':
2813 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2814 continue;
2815
2816 case 'T':
2817 case 'W':
2818 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2819 continue;
2820
2821 case 'd':
2822 case 'G':
2823 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2824 continue;
2825
2826 case 'U':
2827 {
2828 int tmp = va_arg (args, int);
2829
2830 insn.insn_opcode |= tmp << OP_SH_RT;
2831 insn.insn_opcode |= tmp << OP_SH_RD;
2832 continue;
2833 }
2834
2835 case 'V':
2836 case 'S':
2837 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2838 continue;
2839
2840 case 'z':
2841 continue;
2842
2843 case '<':
2844 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2845 continue;
2846
2847 case 'D':
2848 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2849 continue;
2850
2851 case 'B':
2852 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2853 continue;
2854
2855 case 'J':
2856 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2857 continue;
2858
2859 case 'q':
2860 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2861 continue;
2862
2863 case 'b':
2864 case 's':
2865 case 'r':
2866 case 'v':
2867 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2868 continue;
2869
2870 case 'i':
2871 case 'j':
2872 case 'o':
2873 *r = (bfd_reloc_code_real_type) va_arg (args, int);
2874 assert (*r == BFD_RELOC_MIPS_GPREL
2875 || *r == BFD_RELOC_MIPS_LITERAL
2876 || *r == BFD_RELOC_MIPS_HIGHER
2877 || *r == BFD_RELOC_HI16_S
2878 || *r == BFD_RELOC_LO16
2879 || *r == BFD_RELOC_MIPS_GOT16
2880 || *r == BFD_RELOC_MIPS_CALL16
2881 || *r == BFD_RELOC_MIPS_GOT_LO16
2882 || *r == BFD_RELOC_MIPS_CALL_LO16
2883 || (ep->X_op == O_subtract
2884 && *r == BFD_RELOC_PCREL_LO16));
2885 continue;
2886
2887 case 'u':
2888 *r = (bfd_reloc_code_real_type) va_arg (args, int);
2889 assert (ep != NULL
2890 && (ep->X_op == O_constant
2891 || (ep->X_op == O_symbol
2892 && (*r == BFD_RELOC_MIPS_HIGHEST
2893 || *r == BFD_RELOC_HI16_S
2894 || *r == BFD_RELOC_HI16
2895 || *r == BFD_RELOC_GPREL16
2896 || *r == BFD_RELOC_MIPS_GOT_HI16
2897 || *r == BFD_RELOC_MIPS_CALL_HI16))
2898 || (ep->X_op == O_subtract
2899 && *r == BFD_RELOC_PCREL_HI16_S)));
2900 continue;
2901
2902 case 'p':
2903 assert (ep != NULL);
2904 /*
2905 * This allows macro() to pass an immediate expression for
2906 * creating short branches without creating a symbol.
2907 * Note that the expression still might come from the assembly
2908 * input, in which case the value is not checked for range nor
2909 * is a relocation entry generated (yuck).
2910 */
2911 if (ep->X_op == O_constant)
2912 {
2913 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2914 ep = NULL;
2915 }
2916 else
2917 if (mips_pic == EMBEDDED_PIC)
2918 *r = BFD_RELOC_16_PCREL_S2;
2919 else
2920 *r = BFD_RELOC_16_PCREL;
2921 continue;
2922
2923 case 'a':
2924 assert (ep != NULL);
2925 *r = BFD_RELOC_MIPS_JMP;
2926 continue;
2927
2928 case 'C':
2929 insn.insn_opcode |= va_arg (args, unsigned long);
2930 continue;
2931
2932 default:
2933 internalError ();
2934 }
2935 break;
2936 }
2937 va_end (args);
2938 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2939
2940 append_insn (place, &insn, ep, r, false);
2941 }
2942
2943 static void
2944 mips16_macro_build (place, counter, ep, name, fmt, args)
2945 char *place;
2946 int *counter ATTRIBUTE_UNUSED;
2947 expressionS *ep;
2948 const char *name;
2949 const char *fmt;
2950 va_list args;
2951 {
2952 struct mips_cl_insn insn;
2953 bfd_reloc_code_real_type r[3]
2954 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2955
2956 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2957 assert (insn.insn_mo);
2958 assert (strcmp (name, insn.insn_mo->name) == 0);
2959
2960 while (strcmp (fmt, insn.insn_mo->args) != 0
2961 || insn.insn_mo->pinfo == INSN_MACRO)
2962 {
2963 ++insn.insn_mo;
2964 assert (insn.insn_mo->name);
2965 assert (strcmp (name, insn.insn_mo->name) == 0);
2966 }
2967
2968 insn.insn_opcode = insn.insn_mo->match;
2969 insn.use_extend = false;
2970
2971 for (;;)
2972 {
2973 int c;
2974
2975 c = *fmt++;
2976 switch (c)
2977 {
2978 case '\0':
2979 break;
2980
2981 case ',':
2982 case '(':
2983 case ')':
2984 continue;
2985
2986 case 'y':
2987 case 'w':
2988 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2989 continue;
2990
2991 case 'x':
2992 case 'v':
2993 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2994 continue;
2995
2996 case 'z':
2997 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2998 continue;
2999
3000 case 'Z':
3001 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3002 continue;
3003
3004 case '0':
3005 case 'S':
3006 case 'P':
3007 case 'R':
3008 continue;
3009
3010 case 'X':
3011 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3012 continue;
3013
3014 case 'Y':
3015 {
3016 int regno;
3017
3018 regno = va_arg (args, int);
3019 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3020 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3021 }
3022 continue;
3023
3024 case '<':
3025 case '>':
3026 case '4':
3027 case '5':
3028 case 'H':
3029 case 'W':
3030 case 'D':
3031 case 'j':
3032 case '8':
3033 case 'V':
3034 case 'C':
3035 case 'U':
3036 case 'k':
3037 case 'K':
3038 case 'p':
3039 case 'q':
3040 {
3041 assert (ep != NULL);
3042
3043 if (ep->X_op != O_constant)
3044 *r = BFD_RELOC_UNUSED + c;
3045 else
3046 {
3047 mips16_immed ((char *) NULL, 0, c, ep->X_add_number, false,
3048 false, false, &insn.insn_opcode,
3049 &insn.use_extend, &insn.extend);
3050 ep = NULL;
3051 *r = BFD_RELOC_UNUSED;
3052 }
3053 }
3054 continue;
3055
3056 case '6':
3057 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3058 continue;
3059 }
3060
3061 break;
3062 }
3063
3064 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3065
3066 append_insn (place, &insn, ep, r, false);
3067 }
3068
3069 /*
3070 * Generate a "lui" instruction.
3071 */
3072 static void
3073 macro_build_lui (place, counter, ep, regnum)
3074 char *place;
3075 int *counter;
3076 expressionS *ep;
3077 int regnum;
3078 {
3079 expressionS high_expr;
3080 struct mips_cl_insn insn;
3081 bfd_reloc_code_real_type r[3]
3082 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3083 CONST char *name = "lui";
3084 CONST char *fmt = "t,u";
3085
3086 assert (! mips_opts.mips16);
3087
3088 if (place == NULL)
3089 high_expr = *ep;
3090 else
3091 {
3092 high_expr.X_op = O_constant;
3093 high_expr.X_add_number = ep->X_add_number;
3094 }
3095
3096 if (high_expr.X_op == O_constant)
3097 {
3098 /* we can compute the instruction now without a relocation entry */
3099 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3100 >> 16) & 0xffff;
3101 *r = BFD_RELOC_UNUSED;
3102 }
3103 else if (! HAVE_NEWABI)
3104 {
3105 assert (ep->X_op == O_symbol);
3106 /* _gp_disp is a special case, used from s_cpload. */
3107 assert (mips_pic == NO_PIC
3108 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3109 *r = BFD_RELOC_HI16_S;
3110 }
3111
3112 /*
3113 * If the macro is about to expand into a second instruction,
3114 * print a warning if needed. We need to pass ip as a parameter
3115 * to generate a better warning message here...
3116 */
3117 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3118 as_warn (_("Macro instruction expanded into multiple instructions"));
3119
3120 if (place == NULL)
3121 *counter += 1; /* bump instruction counter */
3122
3123 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3124 assert (insn.insn_mo);
3125 assert (strcmp (name, insn.insn_mo->name) == 0);
3126 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3127
3128 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3129 if (*r == BFD_RELOC_UNUSED)
3130 {
3131 insn.insn_opcode |= high_expr.X_add_number;
3132 append_insn (place, &insn, NULL, r, false);
3133 }
3134 else
3135 append_insn (place, &insn, &high_expr, r, false);
3136 }
3137
3138 /* set_at()
3139 * Generates code to set the $at register to true (one)
3140 * if reg is less than the immediate expression.
3141 */
3142 static void
3143 set_at (counter, reg, unsignedp)
3144 int *counter;
3145 int reg;
3146 int unsignedp;
3147 {
3148 if (imm_expr.X_op == O_constant
3149 && imm_expr.X_add_number >= -0x8000
3150 && imm_expr.X_add_number < 0x8000)
3151 macro_build ((char *) NULL, counter, &imm_expr,
3152 unsignedp ? "sltiu" : "slti",
3153 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3154 else
3155 {
3156 load_register (counter, AT, &imm_expr, 0);
3157 macro_build ((char *) NULL, counter, NULL,
3158 unsignedp ? "sltu" : "slt",
3159 "d,v,t", AT, reg, AT);
3160 }
3161 }
3162
3163 /* Warn if an expression is not a constant. */
3164
3165 static void
3166 check_absolute_expr (ip, ex)
3167 struct mips_cl_insn *ip;
3168 expressionS *ex;
3169 {
3170 if (ex->X_op == O_big)
3171 as_bad (_("unsupported large constant"));
3172 else if (ex->X_op != O_constant)
3173 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3174 }
3175
3176 /* Count the leading zeroes by performing a binary chop. This is a
3177 bulky bit of source, but performance is a LOT better for the
3178 majority of values than a simple loop to count the bits:
3179 for (lcnt = 0; (lcnt < 32); lcnt++)
3180 if ((v) & (1 << (31 - lcnt)))
3181 break;
3182 However it is not code size friendly, and the gain will drop a bit
3183 on certain cached systems.
3184 */
3185 #define COUNT_TOP_ZEROES(v) \
3186 (((v) & ~0xffff) == 0 \
3187 ? ((v) & ~0xff) == 0 \
3188 ? ((v) & ~0xf) == 0 \
3189 ? ((v) & ~0x3) == 0 \
3190 ? ((v) & ~0x1) == 0 \
3191 ? !(v) \
3192 ? 32 \
3193 : 31 \
3194 : 30 \
3195 : ((v) & ~0x7) == 0 \
3196 ? 29 \
3197 : 28 \
3198 : ((v) & ~0x3f) == 0 \
3199 ? ((v) & ~0x1f) == 0 \
3200 ? 27 \
3201 : 26 \
3202 : ((v) & ~0x7f) == 0 \
3203 ? 25 \
3204 : 24 \
3205 : ((v) & ~0xfff) == 0 \
3206 ? ((v) & ~0x3ff) == 0 \
3207 ? ((v) & ~0x1ff) == 0 \
3208 ? 23 \
3209 : 22 \
3210 : ((v) & ~0x7ff) == 0 \
3211 ? 21 \
3212 : 20 \
3213 : ((v) & ~0x3fff) == 0 \
3214 ? ((v) & ~0x1fff) == 0 \
3215 ? 19 \
3216 : 18 \
3217 : ((v) & ~0x7fff) == 0 \
3218 ? 17 \
3219 : 16 \
3220 : ((v) & ~0xffffff) == 0 \
3221 ? ((v) & ~0xfffff) == 0 \
3222 ? ((v) & ~0x3ffff) == 0 \
3223 ? ((v) & ~0x1ffff) == 0 \
3224 ? 15 \
3225 : 14 \
3226 : ((v) & ~0x7ffff) == 0 \
3227 ? 13 \
3228 : 12 \
3229 : ((v) & ~0x3fffff) == 0 \
3230 ? ((v) & ~0x1fffff) == 0 \
3231 ? 11 \
3232 : 10 \
3233 : ((v) & ~0x7fffff) == 0 \
3234 ? 9 \
3235 : 8 \
3236 : ((v) & ~0xfffffff) == 0 \
3237 ? ((v) & ~0x3ffffff) == 0 \
3238 ? ((v) & ~0x1ffffff) == 0 \
3239 ? 7 \
3240 : 6 \
3241 : ((v) & ~0x7ffffff) == 0 \
3242 ? 5 \
3243 : 4 \
3244 : ((v) & ~0x3fffffff) == 0 \
3245 ? ((v) & ~0x1fffffff) == 0 \
3246 ? 3 \
3247 : 2 \
3248 : ((v) & ~0x7fffffff) == 0 \
3249 ? 1 \
3250 : 0)
3251
3252 /* load_register()
3253 * This routine generates the least number of instructions neccessary to load
3254 * an absolute expression value into a register.
3255 */
3256 static void
3257 load_register (counter, reg, ep, dbl)
3258 int *counter;
3259 int reg;
3260 expressionS *ep;
3261 int dbl;
3262 {
3263 int freg;
3264 expressionS hi32, lo32;
3265
3266 if (ep->X_op != O_big)
3267 {
3268 assert (ep->X_op == O_constant);
3269 if (ep->X_add_number < 0x8000
3270 && (ep->X_add_number >= 0
3271 || (ep->X_add_number >= -0x8000
3272 && (! dbl
3273 || ! ep->X_unsigned
3274 || sizeof (ep->X_add_number) > 4))))
3275 {
3276 /* We can handle 16 bit signed values with an addiu to
3277 $zero. No need to ever use daddiu here, since $zero and
3278 the result are always correct in 32 bit mode. */
3279 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3280 (int) BFD_RELOC_LO16);
3281 return;
3282 }
3283 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3284 {
3285 /* We can handle 16 bit unsigned values with an ori to
3286 $zero. */
3287 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3288 (int) BFD_RELOC_LO16);
3289 return;
3290 }
3291 else if ((((ep->X_add_number &~ (offsetT) 0x7fffffff) == 0
3292 || ((ep->X_add_number &~ (offsetT) 0x7fffffff)
3293 == ~ (offsetT) 0x7fffffff))
3294 && (! dbl
3295 || ! ep->X_unsigned
3296 || sizeof (ep->X_add_number) > 4
3297 || (ep->X_add_number & 0x80000000) == 0))
3298 || ((HAVE_32BIT_GPRS || ! dbl)
3299 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3300 || (HAVE_32BIT_GPRS
3301 && ! dbl
3302 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3303 == ~ (offsetT) 0xffffffff)))
3304 {
3305 /* 32 bit values require an lui. */
3306 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3307 (int) BFD_RELOC_HI16);
3308 if ((ep->X_add_number & 0xffff) != 0)
3309 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3310 (int) BFD_RELOC_LO16);
3311 return;
3312 }
3313 }
3314
3315 /* The value is larger than 32 bits. */
3316
3317 if (HAVE_32BIT_GPRS)
3318 {
3319 as_bad (_("Number larger than 32 bits"));
3320 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3321 (int) BFD_RELOC_LO16);
3322 return;
3323 }
3324
3325 if (ep->X_op != O_big)
3326 {
3327 hi32 = *ep;
3328 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3329 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3330 hi32.X_add_number &= 0xffffffff;
3331 lo32 = *ep;
3332 lo32.X_add_number &= 0xffffffff;
3333 }
3334 else
3335 {
3336 assert (ep->X_add_number > 2);
3337 if (ep->X_add_number == 3)
3338 generic_bignum[3] = 0;
3339 else if (ep->X_add_number > 4)
3340 as_bad (_("Number larger than 64 bits"));
3341 lo32.X_op = O_constant;
3342 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3343 hi32.X_op = O_constant;
3344 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3345 }
3346
3347 if (hi32.X_add_number == 0)
3348 freg = 0;
3349 else
3350 {
3351 int shift, bit;
3352 unsigned long hi, lo;
3353
3354 if (hi32.X_add_number == 0xffffffff)
3355 {
3356 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3357 {
3358 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3359 reg, 0, (int) BFD_RELOC_LO16);
3360 return;
3361 }
3362 if (lo32.X_add_number & 0x80000000)
3363 {
3364 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3365 (int) BFD_RELOC_HI16);
3366 if (lo32.X_add_number & 0xffff)
3367 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3368 reg, reg, (int) BFD_RELOC_LO16);
3369 return;
3370 }
3371 }
3372
3373 /* Check for 16bit shifted constant. We know that hi32 is
3374 non-zero, so start the mask on the first bit of the hi32
3375 value. */
3376 shift = 17;
3377 do
3378 {
3379 unsigned long himask, lomask;
3380
3381 if (shift < 32)
3382 {
3383 himask = 0xffff >> (32 - shift);
3384 lomask = (0xffff << shift) & 0xffffffff;
3385 }
3386 else
3387 {
3388 himask = 0xffff << (shift - 32);
3389 lomask = 0;
3390 }
3391 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3392 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3393 {
3394 expressionS tmp;
3395
3396 tmp.X_op = O_constant;
3397 if (shift < 32)
3398 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3399 | (lo32.X_add_number >> shift));
3400 else
3401 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3402 macro_build ((char *) NULL, counter, &tmp,
3403 "ori", "t,r,i", reg, 0,
3404 (int) BFD_RELOC_LO16);
3405 macro_build ((char *) NULL, counter, NULL,
3406 (shift >= 32) ? "dsll32" : "dsll",
3407 "d,w,<", reg, reg,
3408 (shift >= 32) ? shift - 32 : shift);
3409 return;
3410 }
3411 shift++;
3412 }
3413 while (shift <= (64 - 16));
3414
3415 /* Find the bit number of the lowest one bit, and store the
3416 shifted value in hi/lo. */
3417 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3418 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3419 if (lo != 0)
3420 {
3421 bit = 0;
3422 while ((lo & 1) == 0)
3423 {
3424 lo >>= 1;
3425 ++bit;
3426 }
3427 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3428 hi >>= bit;
3429 }
3430 else
3431 {
3432 bit = 32;
3433 while ((hi & 1) == 0)
3434 {
3435 hi >>= 1;
3436 ++bit;
3437 }
3438 lo = hi;
3439 hi = 0;
3440 }
3441
3442 /* Optimize if the shifted value is a (power of 2) - 1. */
3443 if ((hi == 0 && ((lo + 1) & lo) == 0)
3444 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3445 {
3446 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3447 if (shift != 0)
3448 {
3449 expressionS tmp;
3450
3451 /* This instruction will set the register to be all
3452 ones. */
3453 tmp.X_op = O_constant;
3454 tmp.X_add_number = (offsetT) -1;
3455 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3456 reg, 0, (int) BFD_RELOC_LO16);
3457 if (bit != 0)
3458 {
3459 bit += shift;
3460 macro_build ((char *) NULL, counter, NULL,
3461 (bit >= 32) ? "dsll32" : "dsll",
3462 "d,w,<", reg, reg,
3463 (bit >= 32) ? bit - 32 : bit);
3464 }
3465 macro_build ((char *) NULL, counter, NULL,
3466 (shift >= 32) ? "dsrl32" : "dsrl",
3467 "d,w,<", reg, reg,
3468 (shift >= 32) ? shift - 32 : shift);
3469 return;
3470 }
3471 }
3472
3473 /* Sign extend hi32 before calling load_register, because we can
3474 generally get better code when we load a sign extended value. */
3475 if ((hi32.X_add_number & 0x80000000) != 0)
3476 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3477 load_register (counter, reg, &hi32, 0);
3478 freg = reg;
3479 }
3480 if ((lo32.X_add_number & 0xffff0000) == 0)
3481 {
3482 if (freg != 0)
3483 {
3484 macro_build ((char *) NULL, counter, NULL, "dsll32", "d,w,<", reg,
3485 freg, 0);
3486 freg = reg;
3487 }
3488 }
3489 else
3490 {
3491 expressionS mid16;
3492
3493 if ((freg == 0) && (lo32.X_add_number == 0xffffffff))
3494 {
3495 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3496 (int) BFD_RELOC_HI16);
3497 macro_build ((char *) NULL, counter, NULL, "dsrl32", "d,w,<", reg,
3498 reg, 0);
3499 return;
3500 }
3501
3502 if (freg != 0)
3503 {
3504 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
3505 freg, 16);
3506 freg = reg;
3507 }
3508 mid16 = lo32;
3509 mid16.X_add_number >>= 16;
3510 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3511 freg, (int) BFD_RELOC_LO16);
3512 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
3513 reg, 16);
3514 freg = reg;
3515 }
3516 if ((lo32.X_add_number & 0xffff) != 0)
3517 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3518 (int) BFD_RELOC_LO16);
3519 }
3520
3521 /* Load an address into a register. */
3522
3523 static void
3524 load_address (counter, reg, ep, dbl, used_at)
3525 int *counter;
3526 int reg;
3527 expressionS *ep;
3528 int dbl;
3529 int *used_at;
3530 {
3531 char *p;
3532
3533 if (ep->X_op != O_constant
3534 && ep->X_op != O_symbol)
3535 {
3536 as_bad (_("expression too complex"));
3537 ep->X_op = O_constant;
3538 }
3539
3540 if (ep->X_op == O_constant)
3541 {
3542 load_register (counter, reg, ep, dbl);
3543 return;
3544 }
3545
3546 if (mips_pic == NO_PIC)
3547 {
3548 /* If this is a reference to a GP relative symbol, we want
3549 addiu $reg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
3550 Otherwise we want
3551 lui $reg,<sym> (BFD_RELOC_HI16_S)
3552 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3553 If we have an addend, we always use the latter form.
3554
3555 With 64bit address space and a usable $at we want
3556 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3557 lui $at,<sym> (BFD_RELOC_HI16_S)
3558 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3559 daddiu $at,<sym> (BFD_RELOC_LO16)
3560 dsll32 $reg,0
3561 dadd $reg,$reg,$at
3562
3563 If $at is already in use, we use an path which is suboptimal
3564 on superscalar processors.
3565 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3566 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3567 dsll $reg,16
3568 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3569 dsll $reg,16
3570 daddiu $reg,<sym> (BFD_RELOC_LO16)
3571 */
3572 if (HAVE_64BIT_ADDRESSES)
3573 {
3574 p = NULL;
3575
3576 /* We don't do GP optimization for now because RELAX_ENCODE can't
3577 hold the data for such large chunks. */
3578
3579 if (*used_at == 0)
3580 {
3581 macro_build (p, counter, ep, "lui", "t,u",
3582 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3583 macro_build (p, counter, ep, "lui", "t,u",
3584 AT, (int) BFD_RELOC_HI16_S);
3585 macro_build (p, counter, ep, "daddiu", "t,r,j",
3586 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3587 macro_build (p, counter, ep, "daddiu", "t,r,j",
3588 AT, AT, (int) BFD_RELOC_LO16);
3589 macro_build (p, counter, NULL, "dsll32", "d,w,<",
3590 reg, reg, 0);
3591 macro_build (p, counter, NULL, "dadd", "d,v,t",
3592 reg, reg, AT);
3593 *used_at = 1;
3594 }
3595 else
3596 {
3597 macro_build (p, counter, ep, "lui", "t,u",
3598 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3599 macro_build (p, counter, ep, "daddiu", "t,r,j",
3600 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3601 macro_build (p, counter, NULL, "dsll", "d,w,<",
3602 reg, reg, 16);
3603 macro_build (p, counter, ep, "daddiu", "t,r,j",
3604 reg, reg, (int) BFD_RELOC_HI16_S);
3605 macro_build (p, counter, NULL, "dsll", "d,w,<",
3606 reg, reg, 16);
3607 macro_build (p, counter, ep, "daddiu", "t,r,j",
3608 reg, reg, (int) BFD_RELOC_LO16);
3609 }
3610 }
3611 else
3612 {
3613 p = NULL;
3614 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3615 && ! nopic_need_relax (ep->X_add_symbol, 1))
3616 {
3617 frag_grow (20);
3618 macro_build ((char *) NULL, counter, ep,
3619 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3620 "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
3621 p = frag_var (rs_machine_dependent, 8, 0,
3622 RELAX_ENCODE (4, 8, 0, 4, 0,
3623 mips_opts.warn_about_macros),
3624 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3625 }
3626 macro_build_lui (p, counter, ep, reg);
3627 if (p != NULL)
3628 p += 4;
3629 macro_build (p, counter, ep,
3630 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3631 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3632 }
3633 }
3634 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3635 {
3636 expressionS ex;
3637
3638 /* If this is a reference to an external symbol, we want
3639 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3640 Otherwise we want
3641 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3642 nop
3643 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3644 If there is a constant, it must be added in after. */
3645 ex.X_add_number = ep->X_add_number;
3646 ep->X_add_number = 0;
3647 frag_grow (20);
3648 macro_build ((char *) NULL, counter, ep,
3649 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3650 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3651 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3652 p = frag_var (rs_machine_dependent, 4, 0,
3653 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3654 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3655 macro_build (p, counter, ep,
3656 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3657 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3658 if (ex.X_add_number != 0)
3659 {
3660 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3661 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3662 ex.X_op = O_constant;
3663 macro_build ((char *) NULL, counter, &ex,
3664 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3665 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3666 }
3667 }
3668 else if (mips_pic == SVR4_PIC)
3669 {
3670 expressionS ex;
3671 int off;
3672
3673 /* This is the large GOT case. If this is a reference to an
3674 external symbol, we want
3675 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3676 addu $reg,$reg,$gp
3677 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3678 Otherwise, for a reference to a local symbol, we want
3679 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3680 nop
3681 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3682 If there is a constant, it must be added in after. */
3683 ex.X_add_number = ep->X_add_number;
3684 ep->X_add_number = 0;
3685 if (reg_needs_delay (GP))
3686 off = 4;
3687 else
3688 off = 0;
3689 frag_grow (32);
3690 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3691 (int) BFD_RELOC_MIPS_GOT_HI16);
3692 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3693 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3694 "d,v,t", reg, reg, GP);
3695 macro_build ((char *) NULL, counter, ep,
3696 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3697 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3698 p = frag_var (rs_machine_dependent, 12 + off, 0,
3699 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3700 mips_opts.warn_about_macros),
3701 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3702 if (off > 0)
3703 {
3704 /* We need a nop before loading from $gp. This special
3705 check is required because the lui which starts the main
3706 instruction stream does not refer to $gp, and so will not
3707 insert the nop which may be required. */
3708 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3709 p += 4;
3710 }
3711 macro_build (p, counter, ep, HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3712 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3713 p += 4;
3714 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3715 p += 4;
3716 macro_build (p, counter, ep, HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3717 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3718 if (ex.X_add_number != 0)
3719 {
3720 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3721 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3722 ex.X_op = O_constant;
3723 macro_build ((char *) NULL, counter, &ex,
3724 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3725 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3726 }
3727 }
3728 else if (mips_pic == EMBEDDED_PIC)
3729 {
3730 /* We always do
3731 addiu $reg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
3732 */
3733 macro_build ((char *) NULL, counter, ep,
3734 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3735 "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
3736 }
3737 else
3738 abort ();
3739 }
3740
3741 /* Move the contents of register SOURCE into register DEST. */
3742
3743 static void
3744 move_register (counter, dest, source)
3745 int *counter;
3746 int dest;
3747 int source;
3748 {
3749 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3750 HAVE_32BIT_GPRS ? "addu" : "daddu",
3751 "d,v,t", dest, source, 0);
3752 }
3753
3754 /*
3755 * Build macros
3756 * This routine implements the seemingly endless macro or synthesized
3757 * instructions and addressing modes in the mips assembly language. Many
3758 * of these macros are simple and are similar to each other. These could
3759 * probably be handled by some kind of table or grammer aproach instead of
3760 * this verbose method. Others are not simple macros but are more like
3761 * optimizing code generation.
3762 * One interesting optimization is when several store macros appear
3763 * consecutivly that would load AT with the upper half of the same address.
3764 * The ensuing load upper instructions are ommited. This implies some kind
3765 * of global optimization. We currently only optimize within a single macro.
3766 * For many of the load and store macros if the address is specified as a
3767 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3768 * first load register 'at' with zero and use it as the base register. The
3769 * mips assembler simply uses register $zero. Just one tiny optimization
3770 * we're missing.
3771 */
3772 static void
3773 macro (ip)
3774 struct mips_cl_insn *ip;
3775 {
3776 register int treg, sreg, dreg, breg;
3777 int tempreg;
3778 int mask;
3779 int icnt = 0;
3780 int used_at = 0;
3781 expressionS expr1;
3782 const char *s;
3783 const char *s2;
3784 const char *fmt;
3785 int likely = 0;
3786 int dbl = 0;
3787 int coproc = 0;
3788 int lr = 0;
3789 int imm = 0;
3790 offsetT maxnum;
3791 int off;
3792 bfd_reloc_code_real_type r;
3793 char *p;
3794 int hold_mips_optimize;
3795
3796 assert (! mips_opts.mips16);
3797
3798 treg = (ip->insn_opcode >> 16) & 0x1f;
3799 dreg = (ip->insn_opcode >> 11) & 0x1f;
3800 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3801 mask = ip->insn_mo->mask;
3802
3803 expr1.X_op = O_constant;
3804 expr1.X_op_symbol = NULL;
3805 expr1.X_add_symbol = NULL;
3806 expr1.X_add_number = 1;
3807
3808 switch (mask)
3809 {
3810 case M_DABS:
3811 dbl = 1;
3812 case M_ABS:
3813 /* bgez $a0,.+12
3814 move v0,$a0
3815 sub v0,$zero,$a0
3816 */
3817
3818 mips_emit_delays (true);
3819 ++mips_opts.noreorder;
3820 mips_any_noreorder = 1;
3821
3822 expr1.X_add_number = 8;
3823 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3824 if (dreg == sreg)
3825 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3826 else
3827 move_register (&icnt, dreg, sreg);
3828 macro_build ((char *) NULL, &icnt, NULL,
3829 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3830
3831 --mips_opts.noreorder;
3832 return;
3833
3834 case M_ADD_I:
3835 s = "addi";
3836 s2 = "add";
3837 goto do_addi;
3838 case M_ADDU_I:
3839 s = "addiu";
3840 s2 = "addu";
3841 goto do_addi;
3842 case M_DADD_I:
3843 dbl = 1;
3844 s = "daddi";
3845 s2 = "dadd";
3846 goto do_addi;
3847 case M_DADDU_I:
3848 dbl = 1;
3849 s = "daddiu";
3850 s2 = "daddu";
3851 do_addi:
3852 if (imm_expr.X_op == O_constant
3853 && imm_expr.X_add_number >= -0x8000
3854 && imm_expr.X_add_number < 0x8000)
3855 {
3856 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3857 (int) BFD_RELOC_LO16);
3858 return;
3859 }
3860 load_register (&icnt, AT, &imm_expr, dbl);
3861 macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
3862 break;
3863
3864 case M_AND_I:
3865 s = "andi";
3866 s2 = "and";
3867 goto do_bit;
3868 case M_OR_I:
3869 s = "ori";
3870 s2 = "or";
3871 goto do_bit;
3872 case M_NOR_I:
3873 s = "";
3874 s2 = "nor";
3875 goto do_bit;
3876 case M_XOR_I:
3877 s = "xori";
3878 s2 = "xor";
3879 do_bit:
3880 if (imm_expr.X_op == O_constant
3881 && imm_expr.X_add_number >= 0
3882 && imm_expr.X_add_number < 0x10000)
3883 {
3884 if (mask != M_NOR_I)
3885 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3886 sreg, (int) BFD_RELOC_LO16);
3887 else
3888 {
3889 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3890 treg, sreg, (int) BFD_RELOC_LO16);
3891 macro_build ((char *) NULL, &icnt, NULL, "nor", "d,v,t",
3892 treg, treg, 0);
3893 }
3894 return;
3895 }
3896
3897 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3898 macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
3899 break;
3900
3901 case M_BEQ_I:
3902 s = "beq";
3903 goto beq_i;
3904 case M_BEQL_I:
3905 s = "beql";
3906 likely = 1;
3907 goto beq_i;
3908 case M_BNE_I:
3909 s = "bne";
3910 goto beq_i;
3911 case M_BNEL_I:
3912 s = "bnel";
3913 likely = 1;
3914 beq_i:
3915 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3916 {
3917 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3918 0);
3919 return;
3920 }
3921 load_register (&icnt, AT, &imm_expr, 0);
3922 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3923 break;
3924
3925 case M_BGEL:
3926 likely = 1;
3927 case M_BGE:
3928 if (treg == 0)
3929 {
3930 macro_build ((char *) NULL, &icnt, &offset_expr,
3931 likely ? "bgezl" : "bgez",
3932 "s,p", sreg);
3933 return;
3934 }
3935 if (sreg == 0)
3936 {
3937 macro_build ((char *) NULL, &icnt, &offset_expr,
3938 likely ? "blezl" : "blez",
3939 "s,p", treg);
3940 return;
3941 }
3942 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
3943 macro_build ((char *) NULL, &icnt, &offset_expr,
3944 likely ? "beql" : "beq", "s,t,p", AT, 0);
3945 break;
3946
3947 case M_BGTL_I:
3948 likely = 1;
3949 case M_BGT_I:
3950 /* check for > max integer */
3951 maxnum = 0x7fffffff;
3952 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3953 {
3954 maxnum <<= 16;
3955 maxnum |= 0xffff;
3956 maxnum <<= 16;
3957 maxnum |= 0xffff;
3958 }
3959 if (imm_expr.X_op == O_constant
3960 && imm_expr.X_add_number >= maxnum
3961 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3962 {
3963 do_false:
3964 /* result is always false */
3965 if (! likely)
3966 {
3967 if (warn_nops)
3968 as_warn (_("Branch %s is always false (nop)"),
3969 ip->insn_mo->name);
3970 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3971 }
3972 else
3973 {
3974 if (warn_nops)
3975 as_warn (_("Branch likely %s is always false"),
3976 ip->insn_mo->name);
3977 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3978 "s,t,p", 0, 0);
3979 }
3980 return;
3981 }
3982 if (imm_expr.X_op != O_constant)
3983 as_bad (_("Unsupported large constant"));
3984 imm_expr.X_add_number++;
3985 /* FALLTHROUGH */
3986 case M_BGE_I:
3987 case M_BGEL_I:
3988 if (mask == M_BGEL_I)
3989 likely = 1;
3990 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3991 {
3992 macro_build ((char *) NULL, &icnt, &offset_expr,
3993 likely ? "bgezl" : "bgez", "s,p", sreg);
3994 return;
3995 }
3996 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3997 {
3998 macro_build ((char *) NULL, &icnt, &offset_expr,
3999 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4000 return;
4001 }
4002 maxnum = 0x7fffffff;
4003 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4004 {
4005 maxnum <<= 16;
4006 maxnum |= 0xffff;
4007 maxnum <<= 16;
4008 maxnum |= 0xffff;
4009 }
4010 maxnum = - maxnum - 1;
4011 if (imm_expr.X_op == O_constant
4012 && imm_expr.X_add_number <= maxnum
4013 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4014 {
4015 do_true:
4016 /* result is always true */
4017 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4018 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4019 return;
4020 }
4021 set_at (&icnt, sreg, 0);
4022 macro_build ((char *) NULL, &icnt, &offset_expr,
4023 likely ? "beql" : "beq", "s,t,p", AT, 0);
4024 break;
4025
4026 case M_BGEUL:
4027 likely = 1;
4028 case M_BGEU:
4029 if (treg == 0)
4030 goto do_true;
4031 if (sreg == 0)
4032 {
4033 macro_build ((char *) NULL, &icnt, &offset_expr,
4034 likely ? "beql" : "beq", "s,t,p", 0, treg);
4035 return;
4036 }
4037 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
4038 treg);
4039 macro_build ((char *) NULL, &icnt, &offset_expr,
4040 likely ? "beql" : "beq", "s,t,p", AT, 0);
4041 break;
4042
4043 case M_BGTUL_I:
4044 likely = 1;
4045 case M_BGTU_I:
4046 if (sreg == 0
4047 || (HAVE_32BIT_GPRS
4048 && imm_expr.X_op == O_constant
4049 && imm_expr.X_add_number == 0xffffffff))
4050 goto do_false;
4051 if (imm_expr.X_op != O_constant)
4052 as_bad (_("Unsupported large constant"));
4053 imm_expr.X_add_number++;
4054 /* FALLTHROUGH */
4055 case M_BGEU_I:
4056 case M_BGEUL_I:
4057 if (mask == M_BGEUL_I)
4058 likely = 1;
4059 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4060 goto do_true;
4061 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4062 {
4063 macro_build ((char *) NULL, &icnt, &offset_expr,
4064 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4065 return;
4066 }
4067 set_at (&icnt, sreg, 1);
4068 macro_build ((char *) NULL, &icnt, &offset_expr,
4069 likely ? "beql" : "beq", "s,t,p", AT, 0);
4070 break;
4071
4072 case M_BGTL:
4073 likely = 1;
4074 case M_BGT:
4075 if (treg == 0)
4076 {
4077 macro_build ((char *) NULL, &icnt, &offset_expr,
4078 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4079 return;
4080 }
4081 if (sreg == 0)
4082 {
4083 macro_build ((char *) NULL, &icnt, &offset_expr,
4084 likely ? "bltzl" : "bltz", "s,p", treg);
4085 return;
4086 }
4087 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4088 macro_build ((char *) NULL, &icnt, &offset_expr,
4089 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4090 break;
4091
4092 case M_BGTUL:
4093 likely = 1;
4094 case M_BGTU:
4095 if (treg == 0)
4096 {
4097 macro_build ((char *) NULL, &icnt, &offset_expr,
4098 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4099 return;
4100 }
4101 if (sreg == 0)
4102 goto do_false;
4103 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
4104 sreg);
4105 macro_build ((char *) NULL, &icnt, &offset_expr,
4106 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4107 break;
4108
4109 case M_BLEL:
4110 likely = 1;
4111 case M_BLE:
4112 if (treg == 0)
4113 {
4114 macro_build ((char *) NULL, &icnt, &offset_expr,
4115 likely ? "blezl" : "blez", "s,p", sreg);
4116 return;
4117 }
4118 if (sreg == 0)
4119 {
4120 macro_build ((char *) NULL, &icnt, &offset_expr,
4121 likely ? "bgezl" : "bgez", "s,p", treg);
4122 return;
4123 }
4124 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4125 macro_build ((char *) NULL, &icnt, &offset_expr,
4126 likely ? "beql" : "beq", "s,t,p", AT, 0);
4127 break;
4128
4129 case M_BLEL_I:
4130 likely = 1;
4131 case M_BLE_I:
4132 maxnum = 0x7fffffff;
4133 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4134 {
4135 maxnum <<= 16;
4136 maxnum |= 0xffff;
4137 maxnum <<= 16;
4138 maxnum |= 0xffff;
4139 }
4140 if (imm_expr.X_op == O_constant
4141 && imm_expr.X_add_number >= maxnum
4142 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4143 goto do_true;
4144 if (imm_expr.X_op != O_constant)
4145 as_bad (_("Unsupported large constant"));
4146 imm_expr.X_add_number++;
4147 /* FALLTHROUGH */
4148 case M_BLT_I:
4149 case M_BLTL_I:
4150 if (mask == M_BLTL_I)
4151 likely = 1;
4152 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4153 {
4154 macro_build ((char *) NULL, &icnt, &offset_expr,
4155 likely ? "bltzl" : "bltz", "s,p", sreg);
4156 return;
4157 }
4158 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4159 {
4160 macro_build ((char *) NULL, &icnt, &offset_expr,
4161 likely ? "blezl" : "blez", "s,p", sreg);
4162 return;
4163 }
4164 set_at (&icnt, sreg, 0);
4165 macro_build ((char *) NULL, &icnt, &offset_expr,
4166 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4167 break;
4168
4169 case M_BLEUL:
4170 likely = 1;
4171 case M_BLEU:
4172 if (treg == 0)
4173 {
4174 macro_build ((char *) NULL, &icnt, &offset_expr,
4175 likely ? "beql" : "beq", "s,t,p", sreg, 0);
4176 return;
4177 }
4178 if (sreg == 0)
4179 goto do_true;
4180 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
4181 sreg);
4182 macro_build ((char *) NULL, &icnt, &offset_expr,
4183 likely ? "beql" : "beq", "s,t,p", AT, 0);
4184 break;
4185
4186 case M_BLEUL_I:
4187 likely = 1;
4188 case M_BLEU_I:
4189 if (sreg == 0
4190 || (HAVE_32BIT_GPRS
4191 && imm_expr.X_op == O_constant
4192 && imm_expr.X_add_number == 0xffffffff))
4193 goto do_true;
4194 if (imm_expr.X_op != O_constant)
4195 as_bad (_("Unsupported large constant"));
4196 imm_expr.X_add_number++;
4197 /* FALLTHROUGH */
4198 case M_BLTU_I:
4199 case M_BLTUL_I:
4200 if (mask == M_BLTUL_I)
4201 likely = 1;
4202 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4203 goto do_false;
4204 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4205 {
4206 macro_build ((char *) NULL, &icnt, &offset_expr,
4207 likely ? "beql" : "beq",
4208 "s,t,p", sreg, 0);
4209 return;
4210 }
4211 set_at (&icnt, sreg, 1);
4212 macro_build ((char *) NULL, &icnt, &offset_expr,
4213 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4214 break;
4215
4216 case M_BLTL:
4217 likely = 1;
4218 case M_BLT:
4219 if (treg == 0)
4220 {
4221 macro_build ((char *) NULL, &icnt, &offset_expr,
4222 likely ? "bltzl" : "bltz", "s,p", sreg);
4223 return;
4224 }
4225 if (sreg == 0)
4226 {
4227 macro_build ((char *) NULL, &icnt, &offset_expr,
4228 likely ? "bgtzl" : "bgtz", "s,p", treg);
4229 return;
4230 }
4231 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4232 macro_build ((char *) NULL, &icnt, &offset_expr,
4233 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4234 break;
4235
4236 case M_BLTUL:
4237 likely = 1;
4238 case M_BLTU:
4239 if (treg == 0)
4240 goto do_false;
4241 if (sreg == 0)
4242 {
4243 macro_build ((char *) NULL, &icnt, &offset_expr,
4244 likely ? "bnel" : "bne", "s,t,p", 0, treg);
4245 return;
4246 }
4247 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
4248 treg);
4249 macro_build ((char *) NULL, &icnt, &offset_expr,
4250 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4251 break;
4252
4253 case M_DDIV_3:
4254 dbl = 1;
4255 case M_DIV_3:
4256 s = "mflo";
4257 goto do_div3;
4258 case M_DREM_3:
4259 dbl = 1;
4260 case M_REM_3:
4261 s = "mfhi";
4262 do_div3:
4263 if (treg == 0)
4264 {
4265 as_warn (_("Divide by zero."));
4266 if (mips_trap)
4267 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
4268 else
4269 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
4270 return;
4271 }
4272
4273 mips_emit_delays (true);
4274 ++mips_opts.noreorder;
4275 mips_any_noreorder = 1;
4276 if (mips_trap)
4277 {
4278 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
4279 macro_build ((char *) NULL, &icnt, NULL,
4280 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4281 }
4282 else
4283 {
4284 expr1.X_add_number = 8;
4285 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4286 macro_build ((char *) NULL, &icnt, NULL,
4287 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4288 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
4289 }
4290 expr1.X_add_number = -1;
4291 macro_build ((char *) NULL, &icnt, &expr1,
4292 dbl ? "daddiu" : "addiu",
4293 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4294 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4295 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4296 if (dbl)
4297 {
4298 expr1.X_add_number = 1;
4299 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4300 (int) BFD_RELOC_LO16);
4301 macro_build ((char *) NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT,
4302 31);
4303 }
4304 else
4305 {
4306 expr1.X_add_number = 0x80000000;
4307 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4308 (int) BFD_RELOC_HI16);
4309 }
4310 if (mips_trap)
4311 {
4312 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", sreg, AT);
4313 /* We want to close the noreorder block as soon as possible, so
4314 that later insns are available for delay slot filling. */
4315 --mips_opts.noreorder;
4316 }
4317 else
4318 {
4319 expr1.X_add_number = 8;
4320 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4321 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
4322
4323 /* We want to close the noreorder block as soon as possible, so
4324 that later insns are available for delay slot filling. */
4325 --mips_opts.noreorder;
4326
4327 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
4328 }
4329 macro_build ((char *) NULL, &icnt, NULL, s, "d", dreg);
4330 break;
4331
4332 case M_DIV_3I:
4333 s = "div";
4334 s2 = "mflo";
4335 goto do_divi;
4336 case M_DIVU_3I:
4337 s = "divu";
4338 s2 = "mflo";
4339 goto do_divi;
4340 case M_REM_3I:
4341 s = "div";
4342 s2 = "mfhi";
4343 goto do_divi;
4344 case M_REMU_3I:
4345 s = "divu";
4346 s2 = "mfhi";
4347 goto do_divi;
4348 case M_DDIV_3I:
4349 dbl = 1;
4350 s = "ddiv";
4351 s2 = "mflo";
4352 goto do_divi;
4353 case M_DDIVU_3I:
4354 dbl = 1;
4355 s = "ddivu";
4356 s2 = "mflo";
4357 goto do_divi;
4358 case M_DREM_3I:
4359 dbl = 1;
4360 s = "ddiv";
4361 s2 = "mfhi";
4362 goto do_divi;
4363 case M_DREMU_3I:
4364 dbl = 1;
4365 s = "ddivu";
4366 s2 = "mfhi";
4367 do_divi:
4368 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4369 {
4370 as_warn (_("Divide by zero."));
4371 if (mips_trap)
4372 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
4373 else
4374 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
4375 return;
4376 }
4377 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4378 {
4379 if (strcmp (s2, "mflo") == 0)
4380 move_register (&icnt, dreg, sreg);
4381 else
4382 move_register (&icnt, dreg, 0);
4383 return;
4384 }
4385 if (imm_expr.X_op == O_constant
4386 && imm_expr.X_add_number == -1
4387 && s[strlen (s) - 1] != 'u')
4388 {
4389 if (strcmp (s2, "mflo") == 0)
4390 {
4391 macro_build ((char *) NULL, &icnt, NULL, dbl ? "dneg" : "neg",
4392 "d,w", dreg, sreg);
4393 }
4394 else
4395 move_register (&icnt, dreg, 0);
4396 return;
4397 }
4398
4399 load_register (&icnt, AT, &imm_expr, dbl);
4400 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
4401 macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
4402 break;
4403
4404 case M_DIVU_3:
4405 s = "divu";
4406 s2 = "mflo";
4407 goto do_divu3;
4408 case M_REMU_3:
4409 s = "divu";
4410 s2 = "mfhi";
4411 goto do_divu3;
4412 case M_DDIVU_3:
4413 s = "ddivu";
4414 s2 = "mflo";
4415 goto do_divu3;
4416 case M_DREMU_3:
4417 s = "ddivu";
4418 s2 = "mfhi";
4419 do_divu3:
4420 mips_emit_delays (true);
4421 ++mips_opts.noreorder;
4422 mips_any_noreorder = 1;
4423 if (mips_trap)
4424 {
4425 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
4426 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4427 /* We want to close the noreorder block as soon as possible, so
4428 that later insns are available for delay slot filling. */
4429 --mips_opts.noreorder;
4430 }
4431 else
4432 {
4433 expr1.X_add_number = 8;
4434 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4435 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4436
4437 /* We want to close the noreorder block as soon as possible, so
4438 that later insns are available for delay slot filling. */
4439 --mips_opts.noreorder;
4440 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
4441 }
4442 macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
4443 return;
4444
4445 case M_DLA_AB:
4446 dbl = 1;
4447 case M_LA_AB:
4448 /* Load the address of a symbol into a register. If breg is not
4449 zero, we then add a base register to it. */
4450
4451 if (treg == breg)
4452 {
4453 tempreg = AT;
4454 used_at = 1;
4455 }
4456 else
4457 {
4458 tempreg = treg;
4459 used_at = 0;
4460 }
4461
4462 /* When generating embedded PIC code, we permit expressions of
4463 the form
4464 la $treg,foo-bar
4465 la $treg,foo-bar($breg)
4466 where bar is an address in the current section. These are used
4467 when getting the addresses of functions. We don't permit
4468 X_add_number to be non-zero, because if the symbol is
4469 external the relaxing code needs to know that any addend is
4470 purely the offset to X_op_symbol. */
4471 if (mips_pic == EMBEDDED_PIC
4472 && offset_expr.X_op == O_subtract
4473 && (symbol_constant_p (offset_expr.X_op_symbol)
4474 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4475 : (symbol_equated_p (offset_expr.X_op_symbol)
4476 && (S_GET_SEGMENT
4477 (symbol_get_value_expression (offset_expr.X_op_symbol)
4478 ->X_add_symbol)
4479 == now_seg)))
4480 && (offset_expr.X_add_number == 0
4481 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4482 {
4483 if (breg == 0)
4484 {
4485 tempreg = treg;
4486 used_at = 0;
4487 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4488 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4489 }
4490 else
4491 {
4492 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4493 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4494 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4495 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4496 "d,v,t", tempreg, tempreg, breg);
4497 }
4498 macro_build ((char *) NULL, &icnt, &offset_expr,
4499 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4500 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4501 if (! used_at)
4502 return;
4503 break;
4504 }
4505
4506 if (offset_expr.X_op != O_symbol
4507 && offset_expr.X_op != O_constant)
4508 {
4509 as_bad (_("expression too complex"));
4510 offset_expr.X_op = O_constant;
4511 }
4512
4513 if (offset_expr.X_op == O_constant)
4514 load_register (&icnt, tempreg, &offset_expr, dbl);
4515 else if (mips_pic == NO_PIC)
4516 {
4517 /* If this is a reference to a GP relative symbol, we want
4518 addiu $tempreg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
4519 Otherwise we want
4520 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4521 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4522 If we have a constant, we need two instructions anyhow,
4523 so we may as well always use the latter form.
4524
4525 With 64bit address space and a usable $at we want
4526 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4527 lui $at,<sym> (BFD_RELOC_HI16_S)
4528 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4529 daddiu $at,<sym> (BFD_RELOC_LO16)
4530 dsll32 $tempreg,0
4531 dadd $tempreg,$tempreg,$at
4532
4533 If $at is already in use, we use an path which is suboptimal
4534 on superscalar processors.
4535 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4536 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4537 dsll $tempreg,16
4538 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4539 dsll $tempreg,16
4540 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4541 */
4542 p = NULL;
4543 if (HAVE_64BIT_ADDRESSES)
4544 {
4545 /* We don't do GP optimization for now because RELAX_ENCODE can't
4546 hold the data for such large chunks. */
4547
4548 if (used_at == 0)
4549 {
4550 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4551 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4552 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4553 AT, (int) BFD_RELOC_HI16_S);
4554 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4555 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4556 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4557 AT, AT, (int) BFD_RELOC_LO16);
4558 macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
4559 tempreg, tempreg, 0);
4560 macro_build (p, &icnt, NULL, "dadd", "d,v,t",
4561 tempreg, tempreg, AT);
4562 used_at = 1;
4563 }
4564 else
4565 {
4566 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4567 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4568 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4569 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4570 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
4571 tempreg, tempreg, 16);
4572 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4573 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4574 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
4575 tempreg, tempreg, 16);
4576 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4577 tempreg, tempreg, (int) BFD_RELOC_LO16);
4578 }
4579 }
4580 else
4581 {
4582 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4583 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4584 {
4585 frag_grow (20);
4586 macro_build ((char *) NULL, &icnt, &offset_expr,
4587 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4588 "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
4589 p = frag_var (rs_machine_dependent, 8, 0,
4590 RELAX_ENCODE (4, 8, 0, 4, 0,
4591 mips_opts.warn_about_macros),
4592 offset_expr.X_add_symbol, (offsetT) 0,
4593 (char *) NULL);
4594 }
4595 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4596 if (p != NULL)
4597 p += 4;
4598 macro_build (p, &icnt, &offset_expr,
4599 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4600 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4601 }
4602 }
4603 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4604 {
4605 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4606
4607 /* If this is a reference to an external symbol, and there
4608 is no constant, we want
4609 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4610 or if tempreg is PIC_CALL_REG
4611 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4612 For a local symbol, we want
4613 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4614 nop
4615 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4616
4617 If we have a small constant, and this is a reference to
4618 an external symbol, we want
4619 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4620 nop
4621 addiu $tempreg,$tempreg,<constant>
4622 For a local symbol, we want the same instruction
4623 sequence, but we output a BFD_RELOC_LO16 reloc on the
4624 addiu instruction.
4625
4626 If we have a large constant, and this is a reference to
4627 an external symbol, we want
4628 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4629 lui $at,<hiconstant>
4630 addiu $at,$at,<loconstant>
4631 addu $tempreg,$tempreg,$at
4632 For a local symbol, we want the same instruction
4633 sequence, but we output a BFD_RELOC_LO16 reloc on the
4634 addiu instruction. */
4635 expr1.X_add_number = offset_expr.X_add_number;
4636 offset_expr.X_add_number = 0;
4637 frag_grow (32);
4638 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4639 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4640 macro_build ((char *) NULL, &icnt, &offset_expr,
4641 dbl ? "ld" : "lw",
4642 "t,o(b)", tempreg, lw_reloc_type, GP);
4643 if (expr1.X_add_number == 0)
4644 {
4645 int off;
4646
4647 if (breg == 0)
4648 off = 0;
4649 else
4650 {
4651 /* We're going to put in an addu instruction using
4652 tempreg, so we may as well insert the nop right
4653 now. */
4654 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4655 "nop", "");
4656 off = 4;
4657 }
4658 p = frag_var (rs_machine_dependent, 8 - off, 0,
4659 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4660 (breg == 0
4661 ? mips_opts.warn_about_macros
4662 : 0)),
4663 offset_expr.X_add_symbol, (offsetT) 0,
4664 (char *) NULL);
4665 if (breg == 0)
4666 {
4667 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4668 p += 4;
4669 }
4670 macro_build (p, &icnt, &expr1,
4671 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4672 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4673 /* FIXME: If breg == 0, and the next instruction uses
4674 $tempreg, then if this variant case is used an extra
4675 nop will be generated. */
4676 }
4677 else if (expr1.X_add_number >= -0x8000
4678 && expr1.X_add_number < 0x8000)
4679 {
4680 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4681 "nop", "");
4682 macro_build ((char *) NULL, &icnt, &expr1,
4683 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4684 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4685 (void) frag_var (rs_machine_dependent, 0, 0,
4686 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4687 offset_expr.X_add_symbol, (offsetT) 0,
4688 (char *) NULL);
4689 }
4690 else
4691 {
4692 int off1;
4693
4694 /* If we are going to add in a base register, and the
4695 target register and the base register are the same,
4696 then we are using AT as a temporary register. Since
4697 we want to load the constant into AT, we add our
4698 current AT (from the global offset table) and the
4699 register into the register now, and pretend we were
4700 not using a base register. */
4701 if (breg != treg)
4702 off1 = 0;
4703 else
4704 {
4705 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4706 "nop", "");
4707 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4708 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4709 "d,v,t", treg, AT, breg);
4710 breg = 0;
4711 tempreg = treg;
4712 off1 = -8;
4713 }
4714
4715 /* Set mips_optimize around the lui instruction to avoid
4716 inserting an unnecessary nop after the lw. */
4717 hold_mips_optimize = mips_optimize;
4718 mips_optimize = 2;
4719 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
4720 mips_optimize = hold_mips_optimize;
4721
4722 macro_build ((char *) NULL, &icnt, &expr1,
4723 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4724 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4725 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4726 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4727 "d,v,t", tempreg, tempreg, AT);
4728 (void) frag_var (rs_machine_dependent, 0, 0,
4729 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4730 offset_expr.X_add_symbol, (offsetT) 0,
4731 (char *) NULL);
4732 used_at = 1;
4733 }
4734 }
4735 else if (mips_pic == SVR4_PIC)
4736 {
4737 int gpdel;
4738 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4739 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4740
4741 /* This is the large GOT case. If this is a reference to an
4742 external symbol, and there is no constant, we want
4743 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4744 addu $tempreg,$tempreg,$gp
4745 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4746 or if tempreg is PIC_CALL_REG
4747 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4748 addu $tempreg,$tempreg,$gp
4749 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4750 For a local symbol, we want
4751 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4752 nop
4753 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4754
4755 If we have a small constant, and this is a reference to
4756 an external symbol, we want
4757 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4758 addu $tempreg,$tempreg,$gp
4759 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4760 nop
4761 addiu $tempreg,$tempreg,<constant>
4762 For a local symbol, we want
4763 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4764 nop
4765 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4766
4767 If we have a large constant, and this is a reference to
4768 an external symbol, we want
4769 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4770 addu $tempreg,$tempreg,$gp
4771 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4772 lui $at,<hiconstant>
4773 addiu $at,$at,<loconstant>
4774 addu $tempreg,$tempreg,$at
4775 For a local symbol, we want
4776 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4777 lui $at,<hiconstant>
4778 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4779 addu $tempreg,$tempreg,$at
4780 */
4781 expr1.X_add_number = offset_expr.X_add_number;
4782 offset_expr.X_add_number = 0;
4783 frag_grow (52);
4784 if (reg_needs_delay (GP))
4785 gpdel = 4;
4786 else
4787 gpdel = 0;
4788 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4789 {
4790 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4791 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4792 }
4793 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4794 tempreg, lui_reloc_type);
4795 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4796 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4797 "d,v,t", tempreg, tempreg, GP);
4798 macro_build ((char *) NULL, &icnt, &offset_expr,
4799 dbl ? "ld" : "lw",
4800 "t,o(b)", tempreg, lw_reloc_type, tempreg);
4801 if (expr1.X_add_number == 0)
4802 {
4803 int off;
4804
4805 if (breg == 0)
4806 off = 0;
4807 else
4808 {
4809 /* We're going to put in an addu instruction using
4810 tempreg, so we may as well insert the nop right
4811 now. */
4812 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4813 "nop", "");
4814 off = 4;
4815 }
4816
4817 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4818 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4819 8 + gpdel, 0,
4820 (breg == 0
4821 ? mips_opts.warn_about_macros
4822 : 0)),
4823 offset_expr.X_add_symbol, (offsetT) 0,
4824 (char *) NULL);
4825 }
4826 else if (expr1.X_add_number >= -0x8000
4827 && expr1.X_add_number < 0x8000)
4828 {
4829 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4830 "nop", "");
4831 macro_build ((char *) NULL, &icnt, &expr1,
4832 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4833 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4834
4835 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4836 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4837 (breg == 0
4838 ? mips_opts.warn_about_macros
4839 : 0)),
4840 offset_expr.X_add_symbol, (offsetT) 0,
4841 (char *) NULL);
4842 }
4843 else
4844 {
4845 int adj, dreg;
4846
4847 /* If we are going to add in a base register, and the
4848 target register and the base register are the same,
4849 then we are using AT as a temporary register. Since
4850 we want to load the constant into AT, we add our
4851 current AT (from the global offset table) and the
4852 register into the register now, and pretend we were
4853 not using a base register. */
4854 if (breg != treg)
4855 {
4856 adj = 0;
4857 dreg = tempreg;
4858 }
4859 else
4860 {
4861 assert (tempreg == AT);
4862 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4863 "nop", "");
4864 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4865 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4866 "d,v,t", treg, AT, breg);
4867 dreg = treg;
4868 adj = 8;
4869 }
4870
4871 /* Set mips_optimize around the lui instruction to avoid
4872 inserting an unnecessary nop after the lw. */
4873 hold_mips_optimize = mips_optimize;
4874 mips_optimize = 2;
4875 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
4876 mips_optimize = hold_mips_optimize;
4877
4878 macro_build ((char *) NULL, &icnt, &expr1,
4879 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4880 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4881 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4882 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4883 "d,v,t", dreg, dreg, AT);
4884
4885 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4886 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4887 8 + gpdel, 0,
4888 (breg == 0
4889 ? mips_opts.warn_about_macros
4890 : 0)),
4891 offset_expr.X_add_symbol, (offsetT) 0,
4892 (char *) NULL);
4893
4894 used_at = 1;
4895 }
4896
4897 if (gpdel > 0)
4898 {
4899 /* This is needed because this instruction uses $gp, but
4900 the first instruction on the main stream does not. */
4901 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4902 p += 4;
4903 }
4904 macro_build (p, &icnt, &offset_expr,
4905 dbl ? "ld" : "lw",
4906 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
4907 p += 4;
4908 if (expr1.X_add_number >= -0x8000
4909 && expr1.X_add_number < 0x8000)
4910 {
4911 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4912 p += 4;
4913 macro_build (p, &icnt, &expr1,
4914 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4915 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4916 /* FIXME: If add_number is 0, and there was no base
4917 register, the external symbol case ended with a load,
4918 so if the symbol turns out to not be external, and
4919 the next instruction uses tempreg, an unnecessary nop
4920 will be inserted. */
4921 }
4922 else
4923 {
4924 if (breg == treg)
4925 {
4926 /* We must add in the base register now, as in the
4927 external symbol case. */
4928 assert (tempreg == AT);
4929 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4930 p += 4;
4931 macro_build (p, &icnt, (expressionS *) NULL,
4932 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4933 "d,v,t", treg, AT, breg);
4934 p += 4;
4935 tempreg = treg;
4936 /* We set breg to 0 because we have arranged to add
4937 it in in both cases. */
4938 breg = 0;
4939 }
4940
4941 macro_build_lui (p, &icnt, &expr1, AT);
4942 p += 4;
4943 macro_build (p, &icnt, &expr1,
4944 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4945 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4946 p += 4;
4947 macro_build (p, &icnt, (expressionS *) NULL,
4948 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4949 "d,v,t", tempreg, tempreg, AT);
4950 p += 4;
4951 }
4952 }
4953 else if (mips_pic == EMBEDDED_PIC)
4954 {
4955 /* We use
4956 addiu $tempreg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
4957 */
4958 macro_build ((char *) NULL, &icnt, &offset_expr,
4959 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4960 "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
4961 }
4962 else
4963 abort ();
4964
4965 if (breg != 0)
4966 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4967 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4968 "d,v,t", treg, tempreg, breg);
4969
4970 if (! used_at)
4971 return;
4972
4973 break;
4974
4975 case M_J_A:
4976 /* The j instruction may not be used in PIC code, since it
4977 requires an absolute address. We convert it to a b
4978 instruction. */
4979 if (mips_pic == NO_PIC)
4980 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
4981 else
4982 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4983 return;
4984
4985 /* The jal instructions must be handled as macros because when
4986 generating PIC code they expand to multi-instruction
4987 sequences. Normally they are simple instructions. */
4988 case M_JAL_1:
4989 dreg = RA;
4990 /* Fall through. */
4991 case M_JAL_2:
4992 if (mips_pic == NO_PIC
4993 || mips_pic == EMBEDDED_PIC)
4994 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
4995 "d,s", dreg, sreg);
4996 else if (mips_pic == SVR4_PIC)
4997 {
4998 if (sreg != PIC_CALL_REG)
4999 as_warn (_("MIPS PIC call to register other than $25"));
5000
5001 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5002 "d,s", dreg, sreg);
5003 if (! HAVE_NEWABI)
5004 {
5005 if (mips_cprestore_offset < 0)
5006 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5007 else
5008 {
5009 expr1.X_add_number = mips_cprestore_offset;
5010 macro_build ((char *) NULL, &icnt, &expr1,
5011 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5012 GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5013 }
5014 }
5015 }
5016 else
5017 abort ();
5018
5019 return;
5020
5021 case M_JAL_A:
5022 if (mips_pic == NO_PIC)
5023 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5024 else if (mips_pic == SVR4_PIC)
5025 {
5026 /* If this is a reference to an external symbol, and we are
5027 using a small GOT, we want
5028 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5029 nop
5030 jalr $25
5031 nop
5032 lw $gp,cprestore($sp)
5033 The cprestore value is set using the .cprestore
5034 pseudo-op. If we are using a big GOT, we want
5035 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5036 addu $25,$25,$gp
5037 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5038 nop
5039 jalr $25
5040 nop
5041 lw $gp,cprestore($sp)
5042 If the symbol is not external, we want
5043 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5044 nop
5045 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5046 jalr $25
5047 nop
5048 lw $gp,cprestore($sp) */
5049 frag_grow (40);
5050 if (! mips_big_got)
5051 {
5052 macro_build ((char *) NULL, &icnt, &offset_expr,
5053 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5054 "t,o(b)", PIC_CALL_REG,
5055 (int) BFD_RELOC_MIPS_CALL16, GP);
5056 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5057 "nop", "");
5058 p = frag_var (rs_machine_dependent, 4, 0,
5059 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5060 offset_expr.X_add_symbol, (offsetT) 0,
5061 (char *) NULL);
5062 }
5063 else
5064 {
5065 int gpdel;
5066
5067 if (reg_needs_delay (GP))
5068 gpdel = 4;
5069 else
5070 gpdel = 0;
5071 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5072 PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
5073 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5074 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5075 "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
5076 macro_build ((char *) NULL, &icnt, &offset_expr,
5077 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5078 "t,o(b)", PIC_CALL_REG,
5079 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5080 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5081 "nop", "");
5082 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5083 RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
5084 0, 0),
5085 offset_expr.X_add_symbol, (offsetT) 0,
5086 (char *) NULL);
5087 if (gpdel > 0)
5088 {
5089 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5090 p += 4;
5091 }
5092 macro_build (p, &icnt, &offset_expr,
5093 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5094 "t,o(b)", PIC_CALL_REG,
5095 (int) BFD_RELOC_MIPS_GOT16, GP);
5096 p += 4;
5097 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5098 p += 4;
5099 }
5100 macro_build (p, &icnt, &offset_expr,
5101 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5102 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5103 (int) BFD_RELOC_LO16);
5104 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5105 "jalr", "s", PIC_CALL_REG);
5106 if (! HAVE_NEWABI)
5107 {
5108 if (mips_cprestore_offset < 0)
5109 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5110 else
5111 {
5112 if (mips_opts.noreorder)
5113 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5114 "nop", "");
5115 expr1.X_add_number = mips_cprestore_offset;
5116 macro_build ((char *) NULL, &icnt, &expr1,
5117 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5118 GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5119 }
5120 }
5121 }
5122 else if (mips_pic == EMBEDDED_PIC)
5123 {
5124 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5125 /* The linker may expand the call to a longer sequence which
5126 uses $at, so we must break rather than return. */
5127 break;
5128 }
5129 else
5130 abort ();
5131
5132 return;
5133
5134 case M_LB_AB:
5135 s = "lb";
5136 goto ld;
5137 case M_LBU_AB:
5138 s = "lbu";
5139 goto ld;
5140 case M_LH_AB:
5141 s = "lh";
5142 goto ld;
5143 case M_LHU_AB:
5144 s = "lhu";
5145 goto ld;
5146 case M_LW_AB:
5147 s = "lw";
5148 goto ld;
5149 case M_LWC0_AB:
5150 s = "lwc0";
5151 /* Itbl support may require additional care here. */
5152 coproc = 1;
5153 goto ld;
5154 case M_LWC1_AB:
5155 s = "lwc1";
5156 /* Itbl support may require additional care here. */
5157 coproc = 1;
5158 goto ld;
5159 case M_LWC2_AB:
5160 s = "lwc2";
5161 /* Itbl support may require additional care here. */
5162 coproc = 1;
5163 goto ld;
5164 case M_LWC3_AB:
5165 s = "lwc3";
5166 /* Itbl support may require additional care here. */
5167 coproc = 1;
5168 goto ld;
5169 case M_LWL_AB:
5170 s = "lwl";
5171 lr = 1;
5172 goto ld;
5173 case M_LWR_AB:
5174 s = "lwr";
5175 lr = 1;
5176 goto ld;
5177 case M_LDC1_AB:
5178 if (mips_arch == CPU_R4650)
5179 {
5180 as_bad (_("opcode not supported on this processor"));
5181 return;
5182 }
5183 s = "ldc1";
5184 /* Itbl support may require additional care here. */
5185 coproc = 1;
5186 goto ld;
5187 case M_LDC2_AB:
5188 s = "ldc2";
5189 /* Itbl support may require additional care here. */
5190 coproc = 1;
5191 goto ld;
5192 case M_LDC3_AB:
5193 s = "ldc3";
5194 /* Itbl support may require additional care here. */
5195 coproc = 1;
5196 goto ld;
5197 case M_LDL_AB:
5198 s = "ldl";
5199 lr = 1;
5200 goto ld;
5201 case M_LDR_AB:
5202 s = "ldr";
5203 lr = 1;
5204 goto ld;
5205 case M_LL_AB:
5206 s = "ll";
5207 goto ld;
5208 case M_LLD_AB:
5209 s = "lld";
5210 goto ld;
5211 case M_LWU_AB:
5212 s = "lwu";
5213 ld:
5214 if (breg == treg || coproc || lr)
5215 {
5216 tempreg = AT;
5217 used_at = 1;
5218 }
5219 else
5220 {
5221 tempreg = treg;
5222 used_at = 0;
5223 }
5224 goto ld_st;
5225 case M_SB_AB:
5226 s = "sb";
5227 goto st;
5228 case M_SH_AB:
5229 s = "sh";
5230 goto st;
5231 case M_SW_AB:
5232 s = "sw";
5233 goto st;
5234 case M_SWC0_AB:
5235 s = "swc0";
5236 /* Itbl support may require additional care here. */
5237 coproc = 1;
5238 goto st;
5239 case M_SWC1_AB:
5240 s = "swc1";
5241 /* Itbl support may require additional care here. */
5242 coproc = 1;
5243 goto st;
5244 case M_SWC2_AB:
5245 s = "swc2";
5246 /* Itbl support may require additional care here. */
5247 coproc = 1;
5248 goto st;
5249 case M_SWC3_AB:
5250 s = "swc3";
5251 /* Itbl support may require additional care here. */
5252 coproc = 1;
5253 goto st;
5254 case M_SWL_AB:
5255 s = "swl";
5256 goto st;
5257 case M_SWR_AB:
5258 s = "swr";
5259 goto st;
5260 case M_SC_AB:
5261 s = "sc";
5262 goto st;
5263 case M_SCD_AB:
5264 s = "scd";
5265 goto st;
5266 case M_SDC1_AB:
5267 if (mips_arch == CPU_R4650)
5268 {
5269 as_bad (_("opcode not supported on this processor"));
5270 return;
5271 }
5272 s = "sdc1";
5273 coproc = 1;
5274 /* Itbl support may require additional care here. */
5275 goto st;
5276 case M_SDC2_AB:
5277 s = "sdc2";
5278 /* Itbl support may require additional care here. */
5279 coproc = 1;
5280 goto st;
5281 case M_SDC3_AB:
5282 s = "sdc3";
5283 /* Itbl support may require additional care here. */
5284 coproc = 1;
5285 goto st;
5286 case M_SDL_AB:
5287 s = "sdl";
5288 goto st;
5289 case M_SDR_AB:
5290 s = "sdr";
5291 st:
5292 tempreg = AT;
5293 used_at = 1;
5294 ld_st:
5295 /* Itbl support may require additional care here. */
5296 if (mask == M_LWC1_AB
5297 || mask == M_SWC1_AB
5298 || mask == M_LDC1_AB
5299 || mask == M_SDC1_AB
5300 || mask == M_L_DAB
5301 || mask == M_S_DAB)
5302 fmt = "T,o(b)";
5303 else if (coproc)
5304 fmt = "E,o(b)";
5305 else
5306 fmt = "t,o(b)";
5307
5308 /* For embedded PIC, we allow loads where the offset is calculated
5309 by subtracting a symbol in the current segment from an unknown
5310 symbol, relative to a base register, e.g.:
5311 <op> $treg, <sym>-<localsym>($breg)
5312 This is used by the compiler for switch statements. */
5313 if (mips_pic == EMBEDDED_PIC
5314 && offset_expr.X_op == O_subtract
5315 && (symbol_constant_p (offset_expr.X_op_symbol)
5316 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5317 : (symbol_equated_p (offset_expr.X_op_symbol)
5318 && (S_GET_SEGMENT
5319 (symbol_get_value_expression (offset_expr.X_op_symbol)
5320 ->X_add_symbol)
5321 == now_seg)))
5322 && breg != 0
5323 && (offset_expr.X_add_number == 0
5324 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5325 {
5326 /* For this case, we output the instructions:
5327 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5328 addiu $tempreg,$tempreg,$breg
5329 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5330 If the relocation would fit entirely in 16 bits, it would be
5331 nice to emit:
5332 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5333 instead, but that seems quite difficult. */
5334 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5335 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5336 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5337 ((bfd_arch_bits_per_address (stdoutput) == 32
5338 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5339 ? "addu" : "daddu"),
5340 "d,v,t", tempreg, tempreg, breg);
5341 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5342 (int) BFD_RELOC_PCREL_LO16, tempreg);
5343 if (! used_at)
5344 return;
5345 break;
5346 }
5347
5348 if (offset_expr.X_op != O_constant
5349 && offset_expr.X_op != O_symbol)
5350 {
5351 as_bad (_("expression too complex"));
5352 offset_expr.X_op = O_constant;
5353 }
5354
5355 /* A constant expression in PIC code can be handled just as it
5356 is in non PIC code. */
5357 if (mips_pic == NO_PIC
5358 || offset_expr.X_op == O_constant)
5359 {
5360 /* If this is a reference to a GP relative symbol, and there
5361 is no base register, we want
5362 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5363 Otherwise, if there is no base register, we want
5364 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5365 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5366 If we have a constant, we need two instructions anyhow,
5367 so we always use the latter form.
5368
5369 If we have a base register, and this is a reference to a
5370 GP relative symbol, we want
5371 addu $tempreg,$breg,$gp
5372 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GPREL)
5373 Otherwise we want
5374 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5375 addu $tempreg,$tempreg,$breg
5376 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5377 With a constant we always use the latter case.
5378
5379 With 64bit address space and no base register and $at usable,
5380 we want
5381 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5382 lui $at,<sym> (BFD_RELOC_HI16_S)
5383 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5384 dsll32 $tempreg,0
5385 daddu $tempreg,$at
5386 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5387 If we have a base register, we want
5388 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5389 lui $at,<sym> (BFD_RELOC_HI16_S)
5390 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5391 daddu $at,$breg
5392 dsll32 $tempreg,0
5393 daddu $tempreg,$at
5394 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5395
5396 Without $at we can't generate the optimal path for superscalar
5397 processors here since this would require two temporary registers.
5398 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5399 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5400 dsll $tempreg,16
5401 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5402 dsll $tempreg,16
5403 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5404 If we have a base register, we want
5405 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5406 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5407 dsll $tempreg,16
5408 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5409 dsll $tempreg,16
5410 daddu $tempreg,$tempreg,$breg
5411 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5412 */
5413 if (HAVE_64BIT_ADDRESSES)
5414 {
5415 p = NULL;
5416
5417 /* We don't do GP optimization for now because RELAX_ENCODE can't
5418 hold the data for such large chunks. */
5419
5420 if (used_at == 0)
5421 {
5422 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5423 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5424 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5425 AT, (int) BFD_RELOC_HI16_S);
5426 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5427 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5428 if (breg != 0)
5429 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
5430 AT, AT, breg);
5431 macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
5432 tempreg, tempreg, 0);
5433 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
5434 tempreg, tempreg, AT);
5435 macro_build (p, &icnt, &offset_expr, s,
5436 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5437 used_at = 1;
5438 }
5439 else
5440 {
5441 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5442 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5443 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5444 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5445 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
5446 tempreg, tempreg, 16);
5447 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5448 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5449 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
5450 tempreg, tempreg, 16);
5451 if (breg != 0)
5452 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
5453 tempreg, tempreg, breg);
5454 macro_build (p, &icnt, &offset_expr, s,
5455 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5456 }
5457
5458 return;
5459 }
5460
5461 if (breg == 0)
5462 {
5463 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5464 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5465 p = NULL;
5466 else
5467 {
5468 frag_grow (20);
5469 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5470 treg, (int) BFD_RELOC_MIPS_GPREL, GP);
5471 p = frag_var (rs_machine_dependent, 8, 0,
5472 RELAX_ENCODE (4, 8, 0, 4, 0,
5473 (mips_opts.warn_about_macros
5474 || (used_at
5475 && mips_opts.noat))),
5476 offset_expr.X_add_symbol, (offsetT) 0,
5477 (char *) NULL);
5478 used_at = 0;
5479 }
5480 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5481 if (p != NULL)
5482 p += 4;
5483 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5484 (int) BFD_RELOC_LO16, tempreg);
5485 }
5486 else
5487 {
5488 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5489 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5490 p = NULL;
5491 else
5492 {
5493 frag_grow (28);
5494 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5495 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5496 "d,v,t", tempreg, breg, GP);
5497 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5498 treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
5499 p = frag_var (rs_machine_dependent, 12, 0,
5500 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5501 offset_expr.X_add_symbol, (offsetT) 0,
5502 (char *) NULL);
5503 }
5504 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5505 if (p != NULL)
5506 p += 4;
5507 macro_build (p, &icnt, (expressionS *) NULL,
5508 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5509 "d,v,t", tempreg, tempreg, breg);
5510 if (p != NULL)
5511 p += 4;
5512 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5513 (int) BFD_RELOC_LO16, tempreg);
5514 }
5515 }
5516 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5517 {
5518 /* If this is a reference to an external symbol, we want
5519 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5520 nop
5521 <op> $treg,0($tempreg)
5522 Otherwise we want
5523 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5524 nop
5525 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5526 <op> $treg,0($tempreg)
5527 If there is a base register, we add it to $tempreg before
5528 the <op>. If there is a constant, we stick it in the
5529 <op> instruction. We don't handle constants larger than
5530 16 bits, because we have no way to load the upper 16 bits
5531 (actually, we could handle them for the subset of cases
5532 in which we are not using $at). */
5533 assert (offset_expr.X_op == O_symbol);
5534 expr1.X_add_number = offset_expr.X_add_number;
5535 offset_expr.X_add_number = 0;
5536 if (expr1.X_add_number < -0x8000
5537 || expr1.X_add_number >= 0x8000)
5538 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5539 frag_grow (20);
5540 macro_build ((char *) NULL, &icnt, &offset_expr,
5541 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5542 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5543 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5544 p = frag_var (rs_machine_dependent, 4, 0,
5545 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5546 offset_expr.X_add_symbol, (offsetT) 0,
5547 (char *) NULL);
5548 macro_build (p, &icnt, &offset_expr,
5549 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5550 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5551 if (breg != 0)
5552 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5553 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5554 "d,v,t", tempreg, tempreg, breg);
5555 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5556 (int) BFD_RELOC_LO16, tempreg);
5557 }
5558 else if (mips_pic == SVR4_PIC)
5559 {
5560 int gpdel;
5561
5562 /* If this is a reference to an external symbol, we want
5563 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5564 addu $tempreg,$tempreg,$gp
5565 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5566 <op> $treg,0($tempreg)
5567 Otherwise we want
5568 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5569 nop
5570 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5571 <op> $treg,0($tempreg)
5572 If there is a base register, we add it to $tempreg before
5573 the <op>. If there is a constant, we stick it in the
5574 <op> instruction. We don't handle constants larger than
5575 16 bits, because we have no way to load the upper 16 bits
5576 (actually, we could handle them for the subset of cases
5577 in which we are not using $at). */
5578 assert (offset_expr.X_op == O_symbol);
5579 expr1.X_add_number = offset_expr.X_add_number;
5580 offset_expr.X_add_number = 0;
5581 if (expr1.X_add_number < -0x8000
5582 || expr1.X_add_number >= 0x8000)
5583 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5584 if (reg_needs_delay (GP))
5585 gpdel = 4;
5586 else
5587 gpdel = 0;
5588 frag_grow (36);
5589 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5590 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5591 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5592 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5593 "d,v,t", tempreg, tempreg, GP);
5594 macro_build ((char *) NULL, &icnt, &offset_expr,
5595 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5596 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5597 tempreg);
5598 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5599 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5600 offset_expr.X_add_symbol, (offsetT) 0, (char *) NULL);
5601 if (gpdel > 0)
5602 {
5603 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5604 p += 4;
5605 }
5606 macro_build (p, &icnt, &offset_expr,
5607 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5608 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5609 p += 4;
5610 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5611 p += 4;
5612 macro_build (p, &icnt, &offset_expr,
5613 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5614 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5615 if (breg != 0)
5616 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5617 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5618 "d,v,t", tempreg, tempreg, breg);
5619 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5620 (int) BFD_RELOC_LO16, tempreg);
5621 }
5622 else if (mips_pic == EMBEDDED_PIC)
5623 {
5624 /* If there is no base register, we want
5625 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5626 If there is a base register, we want
5627 addu $tempreg,$breg,$gp
5628 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GPREL)
5629 */
5630 assert (offset_expr.X_op == O_symbol);
5631 if (breg == 0)
5632 {
5633 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5634 treg, (int) BFD_RELOC_MIPS_GPREL, GP);
5635 used_at = 0;
5636 }
5637 else
5638 {
5639 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5640 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5641 "d,v,t", tempreg, breg, GP);
5642 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5643 treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
5644 }
5645 }
5646 else
5647 abort ();
5648
5649 if (! used_at)
5650 return;
5651
5652 break;
5653
5654 case M_LI:
5655 case M_LI_S:
5656 load_register (&icnt, treg, &imm_expr, 0);
5657 return;
5658
5659 case M_DLI:
5660 load_register (&icnt, treg, &imm_expr, 1);
5661 return;
5662
5663 case M_LI_SS:
5664 if (imm_expr.X_op == O_constant)
5665 {
5666 load_register (&icnt, AT, &imm_expr, 0);
5667 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5668 "mtc1", "t,G", AT, treg);
5669 break;
5670 }
5671 else
5672 {
5673 assert (offset_expr.X_op == O_symbol
5674 && strcmp (segment_name (S_GET_SEGMENT
5675 (offset_expr.X_add_symbol)),
5676 ".lit4") == 0
5677 && offset_expr.X_add_number == 0);
5678 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5679 treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5680 return;
5681 }
5682
5683 case M_LI_D:
5684 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5685 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5686 order 32 bits of the value and the low order 32 bits are either
5687 zero or in OFFSET_EXPR. */
5688 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5689 {
5690 if (HAVE_64BIT_GPRS)
5691 load_register (&icnt, treg, &imm_expr, 1);
5692 else
5693 {
5694 int hreg, lreg;
5695
5696 if (target_big_endian)
5697 {
5698 hreg = treg;
5699 lreg = treg + 1;
5700 }
5701 else
5702 {
5703 hreg = treg + 1;
5704 lreg = treg;
5705 }
5706
5707 if (hreg <= 31)
5708 load_register (&icnt, hreg, &imm_expr, 0);
5709 if (lreg <= 31)
5710 {
5711 if (offset_expr.X_op == O_absent)
5712 move_register (&icnt, lreg, 0);
5713 else
5714 {
5715 assert (offset_expr.X_op == O_constant);
5716 load_register (&icnt, lreg, &offset_expr, 0);
5717 }
5718 }
5719 }
5720 return;
5721 }
5722
5723 /* We know that sym is in the .rdata section. First we get the
5724 upper 16 bits of the address. */
5725 if (mips_pic == NO_PIC)
5726 {
5727 /* FIXME: This won't work for a 64 bit address. */
5728 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
5729 }
5730 else if (mips_pic == SVR4_PIC)
5731 {
5732 macro_build ((char *) NULL, &icnt, &offset_expr,
5733 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5734 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5735 }
5736 else if (mips_pic == EMBEDDED_PIC)
5737 {
5738 /* For embedded PIC we pick up the entire address off $gp in
5739 a single instruction. */
5740 macro_build ((char *) NULL, &icnt, &offset_expr,
5741 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5742 "t,r,j", AT, GP, (int) BFD_RELOC_MIPS_GPREL);
5743 offset_expr.X_op = O_constant;
5744 offset_expr.X_add_number = 0;
5745 }
5746 else
5747 abort ();
5748
5749 /* Now we load the register(s). */
5750 if (HAVE_64BIT_GPRS)
5751 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5752 treg, (int) BFD_RELOC_LO16, AT);
5753 else
5754 {
5755 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5756 treg, (int) BFD_RELOC_LO16, AT);
5757 if (treg != 31)
5758 {
5759 /* FIXME: How in the world do we deal with the possible
5760 overflow here? */
5761 offset_expr.X_add_number += 4;
5762 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5763 treg + 1, (int) BFD_RELOC_LO16, AT);
5764 }
5765 }
5766
5767 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5768 does not become a variant frag. */
5769 frag_wane (frag_now);
5770 frag_new (0);
5771
5772 break;
5773
5774 case M_LI_DD:
5775 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5776 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5777 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5778 the value and the low order 32 bits are either zero or in
5779 OFFSET_EXPR. */
5780 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5781 {
5782 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5783 if (HAVE_64BIT_FPRS)
5784 {
5785 assert (HAVE_64BIT_GPRS);
5786 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5787 "dmtc1", "t,S", AT, treg);
5788 }
5789 else
5790 {
5791 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5792 "mtc1", "t,G", AT, treg + 1);
5793 if (offset_expr.X_op == O_absent)
5794 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5795 "mtc1", "t,G", 0, treg);
5796 else
5797 {
5798 assert (offset_expr.X_op == O_constant);
5799 load_register (&icnt, AT, &offset_expr, 0);
5800 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5801 "mtc1", "t,G", AT, treg);
5802 }
5803 }
5804 break;
5805 }
5806
5807 assert (offset_expr.X_op == O_symbol
5808 && offset_expr.X_add_number == 0);
5809 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5810 if (strcmp (s, ".lit8") == 0)
5811 {
5812 if (mips_opts.isa != ISA_MIPS1)
5813 {
5814 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5815 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5816 return;
5817 }
5818 breg = GP;
5819 r = BFD_RELOC_MIPS_LITERAL;
5820 goto dob;
5821 }
5822 else
5823 {
5824 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5825 if (mips_pic == SVR4_PIC)
5826 macro_build ((char *) NULL, &icnt, &offset_expr,
5827 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5828 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5829 else
5830 {
5831 /* FIXME: This won't work for a 64 bit address. */
5832 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
5833 }
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_LO16, AT);
5839
5840 /* To avoid confusion in tc_gen_reloc, we must ensure
5841 that this does not become a variant frag. */
5842 frag_wane (frag_now);
5843 frag_new (0);
5844
5845 break;
5846 }
5847 breg = AT;
5848 r = BFD_RELOC_LO16;
5849 goto dob;
5850 }
5851
5852 case M_L_DOB:
5853 if (mips_arch == CPU_R4650)
5854 {
5855 as_bad (_("opcode not supported on this processor"));
5856 return;
5857 }
5858 /* Even on a big endian machine $fn comes before $fn+1. We have
5859 to adjust when loading from memory. */
5860 r = BFD_RELOC_LO16;
5861 dob:
5862 assert (mips_opts.isa == ISA_MIPS1);
5863 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5864 target_big_endian ? treg + 1 : treg,
5865 (int) r, breg);
5866 /* FIXME: A possible overflow which I don't know how to deal
5867 with. */
5868 offset_expr.X_add_number += 4;
5869 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5870 target_big_endian ? treg : treg + 1,
5871 (int) r, breg);
5872
5873 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5874 does not become a variant frag. */
5875 frag_wane (frag_now);
5876 frag_new (0);
5877
5878 if (breg != AT)
5879 return;
5880 break;
5881
5882 case M_L_DAB:
5883 /*
5884 * The MIPS assembler seems to check for X_add_number not
5885 * being double aligned and generating:
5886 * lui at,%hi(foo+1)
5887 * addu at,at,v1
5888 * addiu at,at,%lo(foo+1)
5889 * lwc1 f2,0(at)
5890 * lwc1 f3,4(at)
5891 * But, the resulting address is the same after relocation so why
5892 * generate the extra instruction?
5893 */
5894 if (mips_arch == CPU_R4650)
5895 {
5896 as_bad (_("opcode not supported on this processor"));
5897 return;
5898 }
5899 /* Itbl support may require additional care here. */
5900 coproc = 1;
5901 if (mips_opts.isa != ISA_MIPS1)
5902 {
5903 s = "ldc1";
5904 goto ld;
5905 }
5906
5907 s = "lwc1";
5908 fmt = "T,o(b)";
5909 goto ldd_std;
5910
5911 case M_S_DAB:
5912 if (mips_arch == CPU_R4650)
5913 {
5914 as_bad (_("opcode not supported on this processor"));
5915 return;
5916 }
5917
5918 if (mips_opts.isa != ISA_MIPS1)
5919 {
5920 s = "sdc1";
5921 goto st;
5922 }
5923
5924 s = "swc1";
5925 fmt = "T,o(b)";
5926 /* Itbl support may require additional care here. */
5927 coproc = 1;
5928 goto ldd_std;
5929
5930 case M_LD_AB:
5931 if (HAVE_64BIT_GPRS)
5932 {
5933 s = "ld";
5934 goto ld;
5935 }
5936
5937 s = "lw";
5938 fmt = "t,o(b)";
5939 goto ldd_std;
5940
5941 case M_SD_AB:
5942 if (HAVE_64BIT_GPRS)
5943 {
5944 s = "sd";
5945 goto st;
5946 }
5947
5948 s = "sw";
5949 fmt = "t,o(b)";
5950
5951 ldd_std:
5952 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
5953 loads for the case of doing a pair of loads to simulate an 'ld'.
5954 This is not currently done by the compiler, and assembly coders
5955 writing embedded-pic code can cope. */
5956
5957 if (offset_expr.X_op != O_symbol
5958 && offset_expr.X_op != O_constant)
5959 {
5960 as_bad (_("expression too complex"));
5961 offset_expr.X_op = O_constant;
5962 }
5963
5964 /* Even on a big endian machine $fn comes before $fn+1. We have
5965 to adjust when loading from memory. We set coproc if we must
5966 load $fn+1 first. */
5967 /* Itbl support may require additional care here. */
5968 if (! target_big_endian)
5969 coproc = 0;
5970
5971 if (mips_pic == NO_PIC
5972 || offset_expr.X_op == O_constant)
5973 {
5974 /* If this is a reference to a GP relative symbol, we want
5975 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5976 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
5977 If we have a base register, we use this
5978 addu $at,$breg,$gp
5979 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
5980 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
5981 If this is not a GP relative symbol, we want
5982 lui $at,<sym> (BFD_RELOC_HI16_S)
5983 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5984 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5985 If there is a base register, we add it to $at after the
5986 lui instruction. If there is a constant, we always use
5987 the last case. */
5988 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5989 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5990 {
5991 p = NULL;
5992 used_at = 1;
5993 }
5994 else
5995 {
5996 int off;
5997
5998 if (breg == 0)
5999 {
6000 frag_grow (28);
6001 tempreg = GP;
6002 off = 0;
6003 used_at = 0;
6004 }
6005 else
6006 {
6007 frag_grow (36);
6008 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6009 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6010 "d,v,t", AT, breg, GP);
6011 tempreg = AT;
6012 off = 4;
6013 used_at = 1;
6014 }
6015
6016 /* Itbl support may require additional care here. */
6017 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6018 coproc ? treg + 1 : treg,
6019 (int) BFD_RELOC_MIPS_GPREL, tempreg);
6020 offset_expr.X_add_number += 4;
6021
6022 /* Set mips_optimize to 2 to avoid inserting an
6023 undesired nop. */
6024 hold_mips_optimize = mips_optimize;
6025 mips_optimize = 2;
6026 /* Itbl support may require additional care here. */
6027 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6028 coproc ? treg : treg + 1,
6029 (int) BFD_RELOC_MIPS_GPREL, tempreg);
6030 mips_optimize = hold_mips_optimize;
6031
6032 p = frag_var (rs_machine_dependent, 12 + off, 0,
6033 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6034 used_at && mips_opts.noat),
6035 offset_expr.X_add_symbol, (offsetT) 0,
6036 (char *) NULL);
6037
6038 /* We just generated two relocs. When tc_gen_reloc
6039 handles this case, it will skip the first reloc and
6040 handle the second. The second reloc already has an
6041 extra addend of 4, which we added above. We must
6042 subtract it out, and then subtract another 4 to make
6043 the first reloc come out right. The second reloc
6044 will come out right because we are going to add 4 to
6045 offset_expr when we build its instruction below.
6046
6047 If we have a symbol, then we don't want to include
6048 the offset, because it will wind up being included
6049 when we generate the reloc. */
6050
6051 if (offset_expr.X_op == O_constant)
6052 offset_expr.X_add_number -= 8;
6053 else
6054 {
6055 offset_expr.X_add_number = -4;
6056 offset_expr.X_op = O_constant;
6057 }
6058 }
6059 macro_build_lui (p, &icnt, &offset_expr, AT);
6060 if (p != NULL)
6061 p += 4;
6062 if (breg != 0)
6063 {
6064 macro_build (p, &icnt, (expressionS *) NULL,
6065 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6066 "d,v,t", AT, breg, AT);
6067 if (p != NULL)
6068 p += 4;
6069 }
6070 /* Itbl support may require additional care here. */
6071 macro_build (p, &icnt, &offset_expr, s, fmt,
6072 coproc ? treg + 1 : treg,
6073 (int) BFD_RELOC_LO16, AT);
6074 if (p != NULL)
6075 p += 4;
6076 /* FIXME: How do we handle overflow here? */
6077 offset_expr.X_add_number += 4;
6078 /* Itbl support may require additional care here. */
6079 macro_build (p, &icnt, &offset_expr, s, fmt,
6080 coproc ? treg : treg + 1,
6081 (int) BFD_RELOC_LO16, AT);
6082 }
6083 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6084 {
6085 int off;
6086
6087 /* If this is a reference to an external symbol, we want
6088 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6089 nop
6090 <op> $treg,0($at)
6091 <op> $treg+1,4($at)
6092 Otherwise we want
6093 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6094 nop
6095 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6096 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6097 If there is a base register we add it to $at before the
6098 lwc1 instructions. If there is a constant we include it
6099 in the lwc1 instructions. */
6100 used_at = 1;
6101 expr1.X_add_number = offset_expr.X_add_number;
6102 offset_expr.X_add_number = 0;
6103 if (expr1.X_add_number < -0x8000
6104 || expr1.X_add_number >= 0x8000 - 4)
6105 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6106 if (breg == 0)
6107 off = 0;
6108 else
6109 off = 4;
6110 frag_grow (24 + off);
6111 macro_build ((char *) NULL, &icnt, &offset_expr,
6112 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6113 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6114 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6115 if (breg != 0)
6116 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6117 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6118 "d,v,t", AT, breg, AT);
6119 /* Itbl support may require additional care here. */
6120 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6121 coproc ? treg + 1 : treg,
6122 (int) BFD_RELOC_LO16, AT);
6123 expr1.X_add_number += 4;
6124
6125 /* Set mips_optimize to 2 to avoid inserting an undesired
6126 nop. */
6127 hold_mips_optimize = mips_optimize;
6128 mips_optimize = 2;
6129 /* Itbl support may require additional care here. */
6130 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6131 coproc ? treg : treg + 1,
6132 (int) BFD_RELOC_LO16, AT);
6133 mips_optimize = hold_mips_optimize;
6134
6135 (void) frag_var (rs_machine_dependent, 0, 0,
6136 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6137 offset_expr.X_add_symbol, (offsetT) 0,
6138 (char *) NULL);
6139 }
6140 else if (mips_pic == SVR4_PIC)
6141 {
6142 int gpdel, off;
6143
6144 /* If this is a reference to an external symbol, we want
6145 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6146 addu $at,$at,$gp
6147 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6148 nop
6149 <op> $treg,0($at)
6150 <op> $treg+1,4($at)
6151 Otherwise we want
6152 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6153 nop
6154 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6155 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6156 If there is a base register we add it to $at before the
6157 lwc1 instructions. If there is a constant we include it
6158 in the lwc1 instructions. */
6159 used_at = 1;
6160 expr1.X_add_number = offset_expr.X_add_number;
6161 offset_expr.X_add_number = 0;
6162 if (expr1.X_add_number < -0x8000
6163 || expr1.X_add_number >= 0x8000 - 4)
6164 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6165 if (reg_needs_delay (GP))
6166 gpdel = 4;
6167 else
6168 gpdel = 0;
6169 if (breg == 0)
6170 off = 0;
6171 else
6172 off = 4;
6173 frag_grow (56);
6174 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6175 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6176 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6177 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6178 "d,v,t", AT, AT, GP);
6179 macro_build ((char *) NULL, &icnt, &offset_expr,
6180 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6181 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6182 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6183 if (breg != 0)
6184 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6185 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6186 "d,v,t", AT, breg, AT);
6187 /* Itbl support may require additional care here. */
6188 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6189 coproc ? treg + 1 : treg,
6190 (int) BFD_RELOC_LO16, AT);
6191 expr1.X_add_number += 4;
6192
6193 /* Set mips_optimize to 2 to avoid inserting an undesired
6194 nop. */
6195 hold_mips_optimize = mips_optimize;
6196 mips_optimize = 2;
6197 /* Itbl support may require additional care here. */
6198 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6199 coproc ? treg : treg + 1,
6200 (int) BFD_RELOC_LO16, AT);
6201 mips_optimize = hold_mips_optimize;
6202 expr1.X_add_number -= 4;
6203
6204 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6205 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6206 8 + gpdel + off, 1, 0),
6207 offset_expr.X_add_symbol, (offsetT) 0,
6208 (char *) NULL);
6209 if (gpdel > 0)
6210 {
6211 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6212 p += 4;
6213 }
6214 macro_build (p, &icnt, &offset_expr,
6215 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6216 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6217 p += 4;
6218 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6219 p += 4;
6220 if (breg != 0)
6221 {
6222 macro_build (p, &icnt, (expressionS *) NULL,
6223 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6224 "d,v,t", AT, breg, AT);
6225 p += 4;
6226 }
6227 /* Itbl support may require additional care here. */
6228 macro_build (p, &icnt, &expr1, s, fmt,
6229 coproc ? treg + 1 : treg,
6230 (int) BFD_RELOC_LO16, AT);
6231 p += 4;
6232 expr1.X_add_number += 4;
6233
6234 /* Set mips_optimize to 2 to avoid inserting an undesired
6235 nop. */
6236 hold_mips_optimize = mips_optimize;
6237 mips_optimize = 2;
6238 /* Itbl support may require additional care here. */
6239 macro_build (p, &icnt, &expr1, s, fmt,
6240 coproc ? treg : treg + 1,
6241 (int) BFD_RELOC_LO16, AT);
6242 mips_optimize = hold_mips_optimize;
6243 }
6244 else if (mips_pic == EMBEDDED_PIC)
6245 {
6246 /* If there is no base register, we use
6247 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
6248 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
6249 If we have a base register, we use
6250 addu $at,$breg,$gp
6251 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
6252 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
6253 */
6254 if (breg == 0)
6255 {
6256 tempreg = GP;
6257 used_at = 0;
6258 }
6259 else
6260 {
6261 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6262 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6263 "d,v,t", AT, breg, GP);
6264 tempreg = AT;
6265 used_at = 1;
6266 }
6267
6268 /* Itbl support may require additional care here. */
6269 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6270 coproc ? treg + 1 : treg,
6271 (int) BFD_RELOC_MIPS_GPREL, tempreg);
6272 offset_expr.X_add_number += 4;
6273 /* Itbl support may require additional care here. */
6274 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6275 coproc ? treg : treg + 1,
6276 (int) BFD_RELOC_MIPS_GPREL, tempreg);
6277 }
6278 else
6279 abort ();
6280
6281 if (! used_at)
6282 return;
6283
6284 break;
6285
6286 case M_LD_OB:
6287 s = "lw";
6288 goto sd_ob;
6289 case M_SD_OB:
6290 s = "sw";
6291 sd_ob:
6292 assert (HAVE_32BIT_ADDRESSES);
6293 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6294 (int) BFD_RELOC_LO16, breg);
6295 offset_expr.X_add_number += 4;
6296 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6297 (int) BFD_RELOC_LO16, breg);
6298 return;
6299
6300 /* New code added to support COPZ instructions.
6301 This code builds table entries out of the macros in mip_opcodes.
6302 R4000 uses interlocks to handle coproc delays.
6303 Other chips (like the R3000) require nops to be inserted for delays.
6304
6305 FIXME: Currently, we require that the user handle delays.
6306 In order to fill delay slots for non-interlocked chips,
6307 we must have a way to specify delays based on the coprocessor.
6308 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6309 What are the side-effects of the cop instruction?
6310 What cache support might we have and what are its effects?
6311 Both coprocessor & memory require delays. how long???
6312 What registers are read/set/modified?
6313
6314 If an itbl is provided to interpret cop instructions,
6315 this knowledge can be encoded in the itbl spec. */
6316
6317 case M_COP0:
6318 s = "c0";
6319 goto copz;
6320 case M_COP1:
6321 s = "c1";
6322 goto copz;
6323 case M_COP2:
6324 s = "c2";
6325 goto copz;
6326 case M_COP3:
6327 s = "c3";
6328 copz:
6329 /* For now we just do C (same as Cz). The parameter will be
6330 stored in insn_opcode by mips_ip. */
6331 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6332 ip->insn_opcode);
6333 return;
6334
6335 case M_MOVE:
6336 move_register (&icnt, dreg, sreg);
6337 return;
6338
6339 #ifdef LOSING_COMPILER
6340 default:
6341 /* Try and see if this is a new itbl instruction.
6342 This code builds table entries out of the macros in mip_opcodes.
6343 FIXME: For now we just assemble the expression and pass it's
6344 value along as a 32-bit immediate.
6345 We may want to have the assembler assemble this value,
6346 so that we gain the assembler's knowledge of delay slots,
6347 symbols, etc.
6348 Would it be more efficient to use mask (id) here? */
6349 if (itbl_have_entries
6350 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6351 {
6352 s = ip->insn_mo->name;
6353 s2 = "cop3";
6354 coproc = ITBL_DECODE_PNUM (immed_expr);;
6355 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6356 return;
6357 }
6358 macro2 (ip);
6359 return;
6360 }
6361 if (mips_opts.noat)
6362 as_warn (_("Macro used $at after \".set noat\""));
6363 }
6364
6365 static void
6366 macro2 (ip)
6367 struct mips_cl_insn *ip;
6368 {
6369 register int treg, sreg, dreg, breg;
6370 int tempreg;
6371 int mask;
6372 int icnt = 0;
6373 int used_at;
6374 expressionS expr1;
6375 const char *s;
6376 const char *s2;
6377 const char *fmt;
6378 int likely = 0;
6379 int dbl = 0;
6380 int coproc = 0;
6381 int lr = 0;
6382 int imm = 0;
6383 int off;
6384 offsetT maxnum;
6385 bfd_reloc_code_real_type r;
6386 char *p;
6387
6388 treg = (ip->insn_opcode >> 16) & 0x1f;
6389 dreg = (ip->insn_opcode >> 11) & 0x1f;
6390 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6391 mask = ip->insn_mo->mask;
6392
6393 expr1.X_op = O_constant;
6394 expr1.X_op_symbol = NULL;
6395 expr1.X_add_symbol = NULL;
6396 expr1.X_add_number = 1;
6397
6398 switch (mask)
6399 {
6400 #endif /* LOSING_COMPILER */
6401
6402 case M_DMUL:
6403 dbl = 1;
6404 case M_MUL:
6405 macro_build ((char *) NULL, &icnt, NULL,
6406 dbl ? "dmultu" : "multu",
6407 "s,t", sreg, treg);
6408 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
6409 return;
6410
6411 case M_DMUL_I:
6412 dbl = 1;
6413 case M_MUL_I:
6414 /* The MIPS assembler some times generates shifts and adds. I'm
6415 not trying to be that fancy. GCC should do this for us
6416 anyway. */
6417 load_register (&icnt, AT, &imm_expr, dbl);
6418 macro_build ((char *) NULL, &icnt, NULL,
6419 dbl ? "dmult" : "mult", "s,t", sreg, AT);
6420 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
6421 break;
6422
6423 case M_DMULO_I:
6424 dbl = 1;
6425 case M_MULO_I:
6426 imm = 1;
6427 goto do_mulo;
6428
6429 case M_DMULO:
6430 dbl = 1;
6431 case M_MULO:
6432 do_mulo:
6433 mips_emit_delays (true);
6434 ++mips_opts.noreorder;
6435 mips_any_noreorder = 1;
6436 if (imm)
6437 load_register (&icnt, AT, &imm_expr, dbl);
6438 macro_build ((char *) NULL, &icnt, NULL,
6439 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6440 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
6441 macro_build ((char *) NULL, &icnt, NULL,
6442 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, 31);
6443 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
6444 if (mips_trap)
6445 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", dreg, AT);
6446 else
6447 {
6448 expr1.X_add_number = 8;
6449 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
6450 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
6451 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
6452 }
6453 --mips_opts.noreorder;
6454 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
6455 break;
6456
6457 case M_DMULOU_I:
6458 dbl = 1;
6459 case M_MULOU_I:
6460 imm = 1;
6461 goto do_mulou;
6462
6463 case M_DMULOU:
6464 dbl = 1;
6465 case M_MULOU:
6466 do_mulou:
6467 mips_emit_delays (true);
6468 ++mips_opts.noreorder;
6469 mips_any_noreorder = 1;
6470 if (imm)
6471 load_register (&icnt, AT, &imm_expr, dbl);
6472 macro_build ((char *) NULL, &icnt, NULL,
6473 dbl ? "dmultu" : "multu",
6474 "s,t", sreg, imm ? AT : treg);
6475 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
6476 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
6477 if (mips_trap)
6478 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", AT, 0);
6479 else
6480 {
6481 expr1.X_add_number = 8;
6482 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6483 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
6484 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
6485 }
6486 --mips_opts.noreorder;
6487 break;
6488
6489 case M_ROL:
6490 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
6491 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
6492 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg,
6493 treg);
6494 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6495 break;
6496
6497 case M_ROL_I:
6498 if (imm_expr.X_op != O_constant)
6499 as_bad (_("rotate count too large"));
6500 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg,
6501 (int) (imm_expr.X_add_number & 0x1f));
6502 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
6503 (int) ((0 - imm_expr.X_add_number) & 0x1f));
6504 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6505 break;
6506
6507 case M_ROR:
6508 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
6509 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
6510 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg,
6511 treg);
6512 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6513 break;
6514
6515 case M_ROR_I:
6516 if (imm_expr.X_op != O_constant)
6517 as_bad (_("rotate count too large"));
6518 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg,
6519 (int) (imm_expr.X_add_number & 0x1f));
6520 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
6521 (int) ((0 - imm_expr.X_add_number) & 0x1f));
6522 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6523 break;
6524
6525 case M_S_DOB:
6526 if (mips_arch == CPU_R4650)
6527 {
6528 as_bad (_("opcode not supported on this processor"));
6529 return;
6530 }
6531 assert (mips_opts.isa == ISA_MIPS1);
6532 /* Even on a big endian machine $fn comes before $fn+1. We have
6533 to adjust when storing to memory. */
6534 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6535 target_big_endian ? treg + 1 : treg,
6536 (int) BFD_RELOC_LO16, breg);
6537 offset_expr.X_add_number += 4;
6538 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6539 target_big_endian ? treg : treg + 1,
6540 (int) BFD_RELOC_LO16, breg);
6541 return;
6542
6543 case M_SEQ:
6544 if (sreg == 0)
6545 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6546 treg, (int) BFD_RELOC_LO16);
6547 else if (treg == 0)
6548 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6549 sreg, (int) BFD_RELOC_LO16);
6550 else
6551 {
6552 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6553 sreg, treg);
6554 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6555 dreg, (int) BFD_RELOC_LO16);
6556 }
6557 return;
6558
6559 case M_SEQ_I:
6560 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6561 {
6562 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6563 sreg, (int) BFD_RELOC_LO16);
6564 return;
6565 }
6566 if (sreg == 0)
6567 {
6568 as_warn (_("Instruction %s: result is always false"),
6569 ip->insn_mo->name);
6570 move_register (&icnt, dreg, 0);
6571 return;
6572 }
6573 if (imm_expr.X_op == O_constant
6574 && imm_expr.X_add_number >= 0
6575 && imm_expr.X_add_number < 0x10000)
6576 {
6577 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6578 sreg, (int) BFD_RELOC_LO16);
6579 used_at = 0;
6580 }
6581 else if (imm_expr.X_op == O_constant
6582 && imm_expr.X_add_number > -0x8000
6583 && imm_expr.X_add_number < 0)
6584 {
6585 imm_expr.X_add_number = -imm_expr.X_add_number;
6586 macro_build ((char *) NULL, &icnt, &imm_expr,
6587 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6588 "t,r,j", dreg, sreg,
6589 (int) BFD_RELOC_LO16);
6590 used_at = 0;
6591 }
6592 else
6593 {
6594 load_register (&icnt, AT, &imm_expr, 0);
6595 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6596 sreg, AT);
6597 used_at = 1;
6598 }
6599 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6600 (int) BFD_RELOC_LO16);
6601 if (used_at)
6602 break;
6603 return;
6604
6605 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6606 s = "slt";
6607 goto sge;
6608 case M_SGEU:
6609 s = "sltu";
6610 sge:
6611 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
6612 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6613 (int) BFD_RELOC_LO16);
6614 return;
6615
6616 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6617 case M_SGEU_I:
6618 if (imm_expr.X_op == O_constant
6619 && imm_expr.X_add_number >= -0x8000
6620 && imm_expr.X_add_number < 0x8000)
6621 {
6622 macro_build ((char *) NULL, &icnt, &imm_expr,
6623 mask == M_SGE_I ? "slti" : "sltiu",
6624 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6625 used_at = 0;
6626 }
6627 else
6628 {
6629 load_register (&icnt, AT, &imm_expr, 0);
6630 macro_build ((char *) NULL, &icnt, NULL,
6631 mask == M_SGE_I ? "slt" : "sltu",
6632 "d,v,t", dreg, sreg, AT);
6633 used_at = 1;
6634 }
6635 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6636 (int) BFD_RELOC_LO16);
6637 if (used_at)
6638 break;
6639 return;
6640
6641 case M_SGT: /* sreg > treg <==> treg < sreg */
6642 s = "slt";
6643 goto sgt;
6644 case M_SGTU:
6645 s = "sltu";
6646 sgt:
6647 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
6648 return;
6649
6650 case M_SGT_I: /* sreg > I <==> I < sreg */
6651 s = "slt";
6652 goto sgti;
6653 case M_SGTU_I:
6654 s = "sltu";
6655 sgti:
6656 load_register (&icnt, AT, &imm_expr, 0);
6657 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
6658 break;
6659
6660 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
6661 s = "slt";
6662 goto sle;
6663 case M_SLEU:
6664 s = "sltu";
6665 sle:
6666 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
6667 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6668 (int) BFD_RELOC_LO16);
6669 return;
6670
6671 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6672 s = "slt";
6673 goto slei;
6674 case M_SLEU_I:
6675 s = "sltu";
6676 slei:
6677 load_register (&icnt, AT, &imm_expr, 0);
6678 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
6679 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6680 (int) BFD_RELOC_LO16);
6681 break;
6682
6683 case M_SLT_I:
6684 if (imm_expr.X_op == O_constant
6685 && imm_expr.X_add_number >= -0x8000
6686 && imm_expr.X_add_number < 0x8000)
6687 {
6688 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6689 dreg, sreg, (int) BFD_RELOC_LO16);
6690 return;
6691 }
6692 load_register (&icnt, AT, &imm_expr, 0);
6693 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
6694 break;
6695
6696 case M_SLTU_I:
6697 if (imm_expr.X_op == O_constant
6698 && imm_expr.X_add_number >= -0x8000
6699 && imm_expr.X_add_number < 0x8000)
6700 {
6701 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6702 dreg, sreg, (int) BFD_RELOC_LO16);
6703 return;
6704 }
6705 load_register (&icnt, AT, &imm_expr, 0);
6706 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg,
6707 AT);
6708 break;
6709
6710 case M_SNE:
6711 if (sreg == 0)
6712 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6713 treg);
6714 else if (treg == 0)
6715 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6716 sreg);
6717 else
6718 {
6719 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6720 sreg, treg);
6721 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6722 dreg);
6723 }
6724 return;
6725
6726 case M_SNE_I:
6727 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6728 {
6729 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6730 sreg);
6731 return;
6732 }
6733 if (sreg == 0)
6734 {
6735 as_warn (_("Instruction %s: result is always true"),
6736 ip->insn_mo->name);
6737 macro_build ((char *) NULL, &icnt, &expr1,
6738 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6739 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6740 return;
6741 }
6742 if (imm_expr.X_op == O_constant
6743 && imm_expr.X_add_number >= 0
6744 && imm_expr.X_add_number < 0x10000)
6745 {
6746 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6747 dreg, sreg, (int) BFD_RELOC_LO16);
6748 used_at = 0;
6749 }
6750 else if (imm_expr.X_op == O_constant
6751 && imm_expr.X_add_number > -0x8000
6752 && imm_expr.X_add_number < 0)
6753 {
6754 imm_expr.X_add_number = -imm_expr.X_add_number;
6755 macro_build ((char *) NULL, &icnt, &imm_expr,
6756 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6757 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6758 used_at = 0;
6759 }
6760 else
6761 {
6762 load_register (&icnt, AT, &imm_expr, 0);
6763 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6764 sreg, AT);
6765 used_at = 1;
6766 }
6767 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
6768 if (used_at)
6769 break;
6770 return;
6771
6772 case M_DSUB_I:
6773 dbl = 1;
6774 case M_SUB_I:
6775 if (imm_expr.X_op == O_constant
6776 && imm_expr.X_add_number > -0x8000
6777 && imm_expr.X_add_number <= 0x8000)
6778 {
6779 imm_expr.X_add_number = -imm_expr.X_add_number;
6780 macro_build ((char *) NULL, &icnt, &imm_expr,
6781 dbl ? "daddi" : "addi",
6782 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6783 return;
6784 }
6785 load_register (&icnt, AT, &imm_expr, dbl);
6786 macro_build ((char *) NULL, &icnt, NULL,
6787 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
6788 break;
6789
6790 case M_DSUBU_I:
6791 dbl = 1;
6792 case M_SUBU_I:
6793 if (imm_expr.X_op == O_constant
6794 && imm_expr.X_add_number > -0x8000
6795 && imm_expr.X_add_number <= 0x8000)
6796 {
6797 imm_expr.X_add_number = -imm_expr.X_add_number;
6798 macro_build ((char *) NULL, &icnt, &imm_expr,
6799 dbl ? "daddiu" : "addiu",
6800 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6801 return;
6802 }
6803 load_register (&icnt, AT, &imm_expr, dbl);
6804 macro_build ((char *) NULL, &icnt, NULL,
6805 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
6806 break;
6807
6808 case M_TEQ_I:
6809 s = "teq";
6810 goto trap;
6811 case M_TGE_I:
6812 s = "tge";
6813 goto trap;
6814 case M_TGEU_I:
6815 s = "tgeu";
6816 goto trap;
6817 case M_TLT_I:
6818 s = "tlt";
6819 goto trap;
6820 case M_TLTU_I:
6821 s = "tltu";
6822 goto trap;
6823 case M_TNE_I:
6824 s = "tne";
6825 trap:
6826 load_register (&icnt, AT, &imm_expr, 0);
6827 macro_build ((char *) NULL, &icnt, NULL, s, "s,t", sreg, AT);
6828 break;
6829
6830 case M_TRUNCWS:
6831 case M_TRUNCWD:
6832 assert (mips_opts.isa == ISA_MIPS1);
6833 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
6834 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
6835
6836 /*
6837 * Is the double cfc1 instruction a bug in the mips assembler;
6838 * or is there a reason for it?
6839 */
6840 mips_emit_delays (true);
6841 ++mips_opts.noreorder;
6842 mips_any_noreorder = 1;
6843 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
6844 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
6845 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6846 expr1.X_add_number = 3;
6847 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
6848 (int) BFD_RELOC_LO16);
6849 expr1.X_add_number = 2;
6850 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
6851 (int) BFD_RELOC_LO16);
6852 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", AT, 31);
6853 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6854 macro_build ((char *) NULL, &icnt, NULL,
6855 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
6856 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", treg, 31);
6857 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6858 --mips_opts.noreorder;
6859 break;
6860
6861 case M_ULH:
6862 s = "lb";
6863 goto ulh;
6864 case M_ULHU:
6865 s = "lbu";
6866 ulh:
6867 if (offset_expr.X_add_number >= 0x7fff)
6868 as_bad (_("operand overflow"));
6869 /* avoid load delay */
6870 if (! target_big_endian)
6871 offset_expr.X_add_number += 1;
6872 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6873 (int) BFD_RELOC_LO16, breg);
6874 if (! target_big_endian)
6875 offset_expr.X_add_number -= 1;
6876 else
6877 offset_expr.X_add_number += 1;
6878 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
6879 (int) BFD_RELOC_LO16, breg);
6880 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
6881 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
6882 break;
6883
6884 case M_ULD:
6885 s = "ldl";
6886 s2 = "ldr";
6887 off = 7;
6888 goto ulw;
6889 case M_ULW:
6890 s = "lwl";
6891 s2 = "lwr";
6892 off = 3;
6893 ulw:
6894 if (offset_expr.X_add_number >= 0x8000 - off)
6895 as_bad (_("operand overflow"));
6896 if (! target_big_endian)
6897 offset_expr.X_add_number += off;
6898 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6899 (int) BFD_RELOC_LO16, breg);
6900 if (! target_big_endian)
6901 offset_expr.X_add_number -= off;
6902 else
6903 offset_expr.X_add_number += off;
6904 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
6905 (int) BFD_RELOC_LO16, breg);
6906 return;
6907
6908 case M_ULD_A:
6909 s = "ldl";
6910 s2 = "ldr";
6911 off = 7;
6912 goto ulwa;
6913 case M_ULW_A:
6914 s = "lwl";
6915 s2 = "lwr";
6916 off = 3;
6917 ulwa:
6918 used_at = 1;
6919 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
6920 if (breg != 0)
6921 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6922 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6923 "d,v,t", AT, AT, breg);
6924 if (! target_big_endian)
6925 expr1.X_add_number = off;
6926 else
6927 expr1.X_add_number = 0;
6928 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
6929 (int) BFD_RELOC_LO16, AT);
6930 if (! target_big_endian)
6931 expr1.X_add_number = 0;
6932 else
6933 expr1.X_add_number = off;
6934 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
6935 (int) BFD_RELOC_LO16, AT);
6936 break;
6937
6938 case M_ULH_A:
6939 case M_ULHU_A:
6940 used_at = 1;
6941 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
6942 if (breg != 0)
6943 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6944 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6945 "d,v,t", AT, AT, breg);
6946 if (target_big_endian)
6947 expr1.X_add_number = 0;
6948 macro_build ((char *) NULL, &icnt, &expr1,
6949 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
6950 (int) BFD_RELOC_LO16, AT);
6951 if (target_big_endian)
6952 expr1.X_add_number = 1;
6953 else
6954 expr1.X_add_number = 0;
6955 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
6956 (int) BFD_RELOC_LO16, AT);
6957 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
6958 treg, 8);
6959 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
6960 treg, AT);
6961 break;
6962
6963 case M_USH:
6964 if (offset_expr.X_add_number >= 0x7fff)
6965 as_bad (_("operand overflow"));
6966 if (target_big_endian)
6967 offset_expr.X_add_number += 1;
6968 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
6969 (int) BFD_RELOC_LO16, breg);
6970 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
6971 if (target_big_endian)
6972 offset_expr.X_add_number -= 1;
6973 else
6974 offset_expr.X_add_number += 1;
6975 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
6976 (int) BFD_RELOC_LO16, breg);
6977 break;
6978
6979 case M_USD:
6980 s = "sdl";
6981 s2 = "sdr";
6982 off = 7;
6983 goto usw;
6984 case M_USW:
6985 s = "swl";
6986 s2 = "swr";
6987 off = 3;
6988 usw:
6989 if (offset_expr.X_add_number >= 0x8000 - off)
6990 as_bad (_("operand overflow"));
6991 if (! target_big_endian)
6992 offset_expr.X_add_number += off;
6993 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6994 (int) BFD_RELOC_LO16, breg);
6995 if (! target_big_endian)
6996 offset_expr.X_add_number -= off;
6997 else
6998 offset_expr.X_add_number += off;
6999 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7000 (int) BFD_RELOC_LO16, breg);
7001 return;
7002
7003 case M_USD_A:
7004 s = "sdl";
7005 s2 = "sdr";
7006 off = 7;
7007 goto uswa;
7008 case M_USW_A:
7009 s = "swl";
7010 s2 = "swr";
7011 off = 3;
7012 uswa:
7013 used_at = 1;
7014 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7015 if (breg != 0)
7016 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7017 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7018 "d,v,t", AT, AT, breg);
7019 if (! target_big_endian)
7020 expr1.X_add_number = off;
7021 else
7022 expr1.X_add_number = 0;
7023 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7024 (int) BFD_RELOC_LO16, AT);
7025 if (! target_big_endian)
7026 expr1.X_add_number = 0;
7027 else
7028 expr1.X_add_number = off;
7029 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7030 (int) BFD_RELOC_LO16, AT);
7031 break;
7032
7033 case M_USH_A:
7034 used_at = 1;
7035 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7036 if (breg != 0)
7037 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7038 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7039 "d,v,t", AT, AT, breg);
7040 if (! target_big_endian)
7041 expr1.X_add_number = 0;
7042 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7043 (int) BFD_RELOC_LO16, AT);
7044 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", treg,
7045 treg, 8);
7046 if (! target_big_endian)
7047 expr1.X_add_number = 1;
7048 else
7049 expr1.X_add_number = 0;
7050 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7051 (int) BFD_RELOC_LO16, AT);
7052 if (! target_big_endian)
7053 expr1.X_add_number = 0;
7054 else
7055 expr1.X_add_number = 1;
7056 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7057 (int) BFD_RELOC_LO16, AT);
7058 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
7059 treg, 8);
7060 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
7061 treg, AT);
7062 break;
7063
7064 default:
7065 /* FIXME: Check if this is one of the itbl macros, since they
7066 are added dynamically. */
7067 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7068 break;
7069 }
7070 if (mips_opts.noat)
7071 as_warn (_("Macro used $at after \".set noat\""));
7072 }
7073
7074 /* Implement macros in mips16 mode. */
7075
7076 static void
7077 mips16_macro (ip)
7078 struct mips_cl_insn *ip;
7079 {
7080 int mask;
7081 int xreg, yreg, zreg, tmp;
7082 int icnt;
7083 expressionS expr1;
7084 int dbl;
7085 const char *s, *s2, *s3;
7086
7087 mask = ip->insn_mo->mask;
7088
7089 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7090 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7091 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7092
7093 icnt = 0;
7094
7095 expr1.X_op = O_constant;
7096 expr1.X_op_symbol = NULL;
7097 expr1.X_add_symbol = NULL;
7098 expr1.X_add_number = 1;
7099
7100 dbl = 0;
7101
7102 switch (mask)
7103 {
7104 default:
7105 internalError ();
7106
7107 case M_DDIV_3:
7108 dbl = 1;
7109 case M_DIV_3:
7110 s = "mflo";
7111 goto do_div3;
7112 case M_DREM_3:
7113 dbl = 1;
7114 case M_REM_3:
7115 s = "mfhi";
7116 do_div3:
7117 mips_emit_delays (true);
7118 ++mips_opts.noreorder;
7119 mips_any_noreorder = 1;
7120 macro_build ((char *) NULL, &icnt, NULL,
7121 dbl ? "ddiv" : "div",
7122 "0,x,y", xreg, yreg);
7123 expr1.X_add_number = 2;
7124 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7125 macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
7126
7127 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7128 since that causes an overflow. We should do that as well,
7129 but I don't see how to do the comparisons without a temporary
7130 register. */
7131 --mips_opts.noreorder;
7132 macro_build ((char *) NULL, &icnt, NULL, s, "x", zreg);
7133 break;
7134
7135 case M_DIVU_3:
7136 s = "divu";
7137 s2 = "mflo";
7138 goto do_divu3;
7139 case M_REMU_3:
7140 s = "divu";
7141 s2 = "mfhi";
7142 goto do_divu3;
7143 case M_DDIVU_3:
7144 s = "ddivu";
7145 s2 = "mflo";
7146 goto do_divu3;
7147 case M_DREMU_3:
7148 s = "ddivu";
7149 s2 = "mfhi";
7150 do_divu3:
7151 mips_emit_delays (true);
7152 ++mips_opts.noreorder;
7153 mips_any_noreorder = 1;
7154 macro_build ((char *) NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
7155 expr1.X_add_number = 2;
7156 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7157 macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
7158 --mips_opts.noreorder;
7159 macro_build ((char *) NULL, &icnt, NULL, s2, "x", zreg);
7160 break;
7161
7162 case M_DMUL:
7163 dbl = 1;
7164 case M_MUL:
7165 macro_build ((char *) NULL, &icnt, NULL,
7166 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7167 macro_build ((char *) NULL, &icnt, NULL, "mflo", "x", zreg);
7168 return;
7169
7170 case M_DSUBU_I:
7171 dbl = 1;
7172 goto do_subu;
7173 case M_SUBU_I:
7174 do_subu:
7175 if (imm_expr.X_op != O_constant)
7176 as_bad (_("Unsupported large constant"));
7177 imm_expr.X_add_number = -imm_expr.X_add_number;
7178 macro_build ((char *) NULL, &icnt, &imm_expr,
7179 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7180 break;
7181
7182 case M_SUBU_I_2:
7183 if (imm_expr.X_op != O_constant)
7184 as_bad (_("Unsupported large constant"));
7185 imm_expr.X_add_number = -imm_expr.X_add_number;
7186 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7187 "x,k", xreg);
7188 break;
7189
7190 case M_DSUBU_I_2:
7191 if (imm_expr.X_op != O_constant)
7192 as_bad (_("Unsupported large constant"));
7193 imm_expr.X_add_number = -imm_expr.X_add_number;
7194 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7195 "y,j", yreg);
7196 break;
7197
7198 case M_BEQ:
7199 s = "cmp";
7200 s2 = "bteqz";
7201 goto do_branch;
7202 case M_BNE:
7203 s = "cmp";
7204 s2 = "btnez";
7205 goto do_branch;
7206 case M_BLT:
7207 s = "slt";
7208 s2 = "btnez";
7209 goto do_branch;
7210 case M_BLTU:
7211 s = "sltu";
7212 s2 = "btnez";
7213 goto do_branch;
7214 case M_BLE:
7215 s = "slt";
7216 s2 = "bteqz";
7217 goto do_reverse_branch;
7218 case M_BLEU:
7219 s = "sltu";
7220 s2 = "bteqz";
7221 goto do_reverse_branch;
7222 case M_BGE:
7223 s = "slt";
7224 s2 = "bteqz";
7225 goto do_branch;
7226 case M_BGEU:
7227 s = "sltu";
7228 s2 = "bteqz";
7229 goto do_branch;
7230 case M_BGT:
7231 s = "slt";
7232 s2 = "btnez";
7233 goto do_reverse_branch;
7234 case M_BGTU:
7235 s = "sltu";
7236 s2 = "btnez";
7237
7238 do_reverse_branch:
7239 tmp = xreg;
7240 xreg = yreg;
7241 yreg = tmp;
7242
7243 do_branch:
7244 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7245 xreg, yreg);
7246 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7247 break;
7248
7249 case M_BEQ_I:
7250 s = "cmpi";
7251 s2 = "bteqz";
7252 s3 = "x,U";
7253 goto do_branch_i;
7254 case M_BNE_I:
7255 s = "cmpi";
7256 s2 = "btnez";
7257 s3 = "x,U";
7258 goto do_branch_i;
7259 case M_BLT_I:
7260 s = "slti";
7261 s2 = "btnez";
7262 s3 = "x,8";
7263 goto do_branch_i;
7264 case M_BLTU_I:
7265 s = "sltiu";
7266 s2 = "btnez";
7267 s3 = "x,8";
7268 goto do_branch_i;
7269 case M_BLE_I:
7270 s = "slti";
7271 s2 = "btnez";
7272 s3 = "x,8";
7273 goto do_addone_branch_i;
7274 case M_BLEU_I:
7275 s = "sltiu";
7276 s2 = "btnez";
7277 s3 = "x,8";
7278 goto do_addone_branch_i;
7279 case M_BGE_I:
7280 s = "slti";
7281 s2 = "bteqz";
7282 s3 = "x,8";
7283 goto do_branch_i;
7284 case M_BGEU_I:
7285 s = "sltiu";
7286 s2 = "bteqz";
7287 s3 = "x,8";
7288 goto do_branch_i;
7289 case M_BGT_I:
7290 s = "slti";
7291 s2 = "bteqz";
7292 s3 = "x,8";
7293 goto do_addone_branch_i;
7294 case M_BGTU_I:
7295 s = "sltiu";
7296 s2 = "bteqz";
7297 s3 = "x,8";
7298
7299 do_addone_branch_i:
7300 if (imm_expr.X_op != O_constant)
7301 as_bad (_("Unsupported large constant"));
7302 ++imm_expr.X_add_number;
7303
7304 do_branch_i:
7305 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7306 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7307 break;
7308
7309 case M_ABS:
7310 expr1.X_add_number = 0;
7311 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7312 if (xreg != yreg)
7313 move_register (&icnt, xreg, yreg);
7314 expr1.X_add_number = 2;
7315 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7316 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7317 "neg", "x,w", xreg, xreg);
7318 }
7319 }
7320
7321 /* For consistency checking, verify that all bits are specified either
7322 by the match/mask part of the instruction definition, or by the
7323 operand list. */
7324 static int
7325 validate_mips_insn (opc)
7326 const struct mips_opcode *opc;
7327 {
7328 const char *p = opc->args;
7329 char c;
7330 unsigned long used_bits = opc->mask;
7331
7332 if ((used_bits & opc->match) != opc->match)
7333 {
7334 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7335 opc->name, opc->args);
7336 return 0;
7337 }
7338 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7339 while (*p)
7340 switch (c = *p++)
7341 {
7342 case ',': break;
7343 case '(': break;
7344 case ')': break;
7345 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7346 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7347 case 'A': break;
7348 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7349 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7350 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7351 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7352 case 'F': break;
7353 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7354 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7355 case 'I': break;
7356 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7357 case 'L': break;
7358 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7359 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7360 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7361 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7362 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7363 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7364 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7365 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7366 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7367 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7368 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7369 case 'f': break;
7370 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7371 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7372 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7373 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7374 case 'l': break;
7375 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7376 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7377 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7378 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7379 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7380 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7381 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7382 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7383 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7384 case 'x': break;
7385 case 'z': break;
7386 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7387 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7388 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7389 default:
7390 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7391 c, opc->name, opc->args);
7392 return 0;
7393 }
7394 #undef USE_BITS
7395 if (used_bits != 0xffffffff)
7396 {
7397 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7398 ~used_bits & 0xffffffff, opc->name, opc->args);
7399 return 0;
7400 }
7401 return 1;
7402 }
7403
7404 /* This routine assembles an instruction into its binary format. As a
7405 side effect, it sets one of the global variables imm_reloc or
7406 offset_reloc to the type of relocation to do if one of the operands
7407 is an address expression. */
7408
7409 static void
7410 mips_ip (str, ip)
7411 char *str;
7412 struct mips_cl_insn *ip;
7413 {
7414 char *s;
7415 const char *args;
7416 char c = 0;
7417 struct mips_opcode *insn;
7418 char *argsStart;
7419 unsigned int regno;
7420 unsigned int lastregno = 0;
7421 char *s_reset;
7422 char save_c = 0;
7423 int full_opcode_match = 1;
7424
7425 insn_error = NULL;
7426
7427 /* If the instruction contains a '.', we first try to match an instruction
7428 including the '.'. Then we try again without the '.'. */
7429 insn = NULL;
7430 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7431 continue;
7432
7433 /* If we stopped on whitespace, then replace the whitespace with null for
7434 the call to hash_find. Save the character we replaced just in case we
7435 have to re-parse the instruction. */
7436 if (ISSPACE (*s))
7437 {
7438 save_c = *s;
7439 *s++ = '\0';
7440 }
7441
7442 insn = (struct mips_opcode *) hash_find (op_hash, str);
7443
7444 /* If we didn't find the instruction in the opcode table, try again, but
7445 this time with just the instruction up to, but not including the
7446 first '.'. */
7447 if (insn == NULL)
7448 {
7449 /* Restore the character we overwrite above (if any). */
7450 if (save_c)
7451 *(--s) = save_c;
7452
7453 /* Scan up to the first '.' or whitespace. */
7454 for (s = str;
7455 *s != '\0' && *s != '.' && !ISSPACE (*s);
7456 ++s)
7457 continue;
7458
7459 /* If we did not find a '.', then we can quit now. */
7460 if (*s != '.')
7461 {
7462 insn_error = "unrecognized opcode";
7463 return;
7464 }
7465
7466 /* Lookup the instruction in the hash table. */
7467 *s++ = '\0';
7468 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7469 {
7470 insn_error = "unrecognized opcode";
7471 return;
7472 }
7473
7474 full_opcode_match = 0;
7475 }
7476
7477 argsStart = s;
7478 for (;;)
7479 {
7480 boolean ok;
7481
7482 assert (strcmp (insn->name, str) == 0);
7483
7484 if (OPCODE_IS_MEMBER (insn, mips_opts.isa, mips_arch))
7485 ok = true;
7486 else
7487 ok = false;
7488
7489 if (insn->pinfo != INSN_MACRO)
7490 {
7491 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7492 ok = false;
7493 }
7494
7495 if (! ok)
7496 {
7497 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7498 && strcmp (insn->name, insn[1].name) == 0)
7499 {
7500 ++insn;
7501 continue;
7502 }
7503 else
7504 {
7505 if (!insn_error)
7506 {
7507 static char buf[100];
7508 sprintf (buf,
7509 _("opcode not supported on this processor: %s (%s)"),
7510 mips_cpu_to_str (mips_arch),
7511 mips_isa_to_str (mips_opts.isa));
7512
7513 insn_error = buf;
7514 }
7515 if (save_c)
7516 *(--s) = save_c;
7517 return;
7518 }
7519 }
7520
7521 ip->insn_mo = insn;
7522 ip->insn_opcode = insn->match;
7523 insn_error = NULL;
7524 for (args = insn->args;; ++args)
7525 {
7526 s += strspn (s, " \t");
7527 switch (*args)
7528 {
7529 case '\0': /* end of args */
7530 if (*s == '\0')
7531 return;
7532 break;
7533
7534 case ',':
7535 if (*s++ == *args)
7536 continue;
7537 s--;
7538 switch (*++args)
7539 {
7540 case 'r':
7541 case 'v':
7542 ip->insn_opcode |= lastregno << OP_SH_RS;
7543 continue;
7544
7545 case 'w':
7546 ip->insn_opcode |= lastregno << OP_SH_RT;
7547 continue;
7548
7549 case 'W':
7550 ip->insn_opcode |= lastregno << OP_SH_FT;
7551 continue;
7552
7553 case 'V':
7554 ip->insn_opcode |= lastregno << OP_SH_FS;
7555 continue;
7556 }
7557 break;
7558
7559 case '(':
7560 /* Handle optional base register.
7561 Either the base register is omitted or
7562 we must have a left paren. */
7563 /* This is dependent on the next operand specifier
7564 is a base register specification. */
7565 assert (args[1] == 'b' || args[1] == '5'
7566 || args[1] == '-' || args[1] == '4');
7567 if (*s == '\0')
7568 return;
7569
7570 case ')': /* these must match exactly */
7571 if (*s++ == *args)
7572 continue;
7573 break;
7574
7575 case '<': /* must be at least one digit */
7576 /*
7577 * According to the manual, if the shift amount is greater
7578 * than 31 or less than 0, then the shift amount should be
7579 * mod 32. In reality the mips assembler issues an error.
7580 * We issue a warning and mask out all but the low 5 bits.
7581 */
7582 my_getExpression (&imm_expr, s);
7583 check_absolute_expr (ip, &imm_expr);
7584 if ((unsigned long) imm_expr.X_add_number > 31)
7585 {
7586 as_warn (_("Improper shift amount (%ld)"),
7587 (long) imm_expr.X_add_number);
7588 imm_expr.X_add_number &= OP_MASK_SHAMT;
7589 }
7590 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7591 imm_expr.X_op = O_absent;
7592 s = expr_end;
7593 continue;
7594
7595 case '>': /* shift amount minus 32 */
7596 my_getExpression (&imm_expr, s);
7597 check_absolute_expr (ip, &imm_expr);
7598 if ((unsigned long) imm_expr.X_add_number < 32
7599 || (unsigned long) imm_expr.X_add_number > 63)
7600 break;
7601 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7602 imm_expr.X_op = O_absent;
7603 s = expr_end;
7604 continue;
7605
7606 case 'k': /* cache code */
7607 case 'h': /* prefx code */
7608 my_getExpression (&imm_expr, s);
7609 check_absolute_expr (ip, &imm_expr);
7610 if ((unsigned long) imm_expr.X_add_number > 31)
7611 {
7612 as_warn (_("Invalid value for `%s' (%lu)"),
7613 ip->insn_mo->name,
7614 (unsigned long) imm_expr.X_add_number);
7615 imm_expr.X_add_number &= 0x1f;
7616 }
7617 if (*args == 'k')
7618 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7619 else
7620 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7621 imm_expr.X_op = O_absent;
7622 s = expr_end;
7623 continue;
7624
7625 case 'c': /* break code */
7626 my_getExpression (&imm_expr, s);
7627 check_absolute_expr (ip, &imm_expr);
7628 if ((unsigned) imm_expr.X_add_number > 1023)
7629 {
7630 as_warn (_("Illegal break code (%ld)"),
7631 (long) imm_expr.X_add_number);
7632 imm_expr.X_add_number &= OP_MASK_CODE;
7633 }
7634 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7635 imm_expr.X_op = O_absent;
7636 s = expr_end;
7637 continue;
7638
7639 case 'q': /* lower break code */
7640 my_getExpression (&imm_expr, s);
7641 check_absolute_expr (ip, &imm_expr);
7642 if ((unsigned) imm_expr.X_add_number > 1023)
7643 {
7644 as_warn (_("Illegal lower break code (%ld)"),
7645 (long) imm_expr.X_add_number);
7646 imm_expr.X_add_number &= OP_MASK_CODE2;
7647 }
7648 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7649 imm_expr.X_op = O_absent;
7650 s = expr_end;
7651 continue;
7652
7653 case 'B': /* 20-bit syscall/break code. */
7654 my_getExpression (&imm_expr, s);
7655 check_absolute_expr (ip, &imm_expr);
7656 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE20)
7657 as_warn (_("Illegal 20-bit code (%ld)"),
7658 (long) imm_expr.X_add_number);
7659 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7660 imm_expr.X_op = O_absent;
7661 s = expr_end;
7662 continue;
7663
7664 case 'C': /* Coprocessor code */
7665 my_getExpression (&imm_expr, s);
7666 check_absolute_expr (ip, &imm_expr);
7667 if ((unsigned long) imm_expr.X_add_number >= (1<<25))
7668 {
7669 as_warn (_("Coproccesor code > 25 bits (%ld)"),
7670 (long) imm_expr.X_add_number);
7671 imm_expr.X_add_number &= ((1<<25) - 1);
7672 }
7673 ip->insn_opcode |= imm_expr.X_add_number;
7674 imm_expr.X_op = O_absent;
7675 s = expr_end;
7676 continue;
7677
7678 case 'J': /* 19-bit wait code. */
7679 my_getExpression (&imm_expr, s);
7680 check_absolute_expr (ip, &imm_expr);
7681 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE19)
7682 as_warn (_("Illegal 19-bit code (%ld)"),
7683 (long) imm_expr.X_add_number);
7684 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
7685 imm_expr.X_op = O_absent;
7686 s = expr_end;
7687 continue;
7688
7689 case 'P': /* Performance register */
7690 my_getExpression (&imm_expr, s);
7691 check_absolute_expr (ip, &imm_expr);
7692 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
7693 {
7694 as_warn (_("Invalid performance register (%ld)"),
7695 (long) imm_expr.X_add_number);
7696 imm_expr.X_add_number &= OP_MASK_PERFREG;
7697 }
7698 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
7699 imm_expr.X_op = O_absent;
7700 s = expr_end;
7701 continue;
7702
7703 case 'b': /* base register */
7704 case 'd': /* destination register */
7705 case 's': /* source register */
7706 case 't': /* target register */
7707 case 'r': /* both target and source */
7708 case 'v': /* both dest and source */
7709 case 'w': /* both dest and target */
7710 case 'E': /* coprocessor target register */
7711 case 'G': /* coprocessor destination register */
7712 case 'x': /* ignore register name */
7713 case 'z': /* must be zero register */
7714 case 'U': /* destination register (clo/clz). */
7715 s_reset = s;
7716 if (s[0] == '$')
7717 {
7718
7719 if (ISDIGIT (s[1]))
7720 {
7721 ++s;
7722 regno = 0;
7723 do
7724 {
7725 regno *= 10;
7726 regno += *s - '0';
7727 ++s;
7728 }
7729 while (ISDIGIT (*s));
7730 if (regno > 31)
7731 as_bad (_("Invalid register number (%d)"), regno);
7732 }
7733 else if (*args == 'E' || *args == 'G')
7734 goto notreg;
7735 else
7736 {
7737 if (s[1] == 'f' && s[2] == 'p')
7738 {
7739 s += 3;
7740 regno = FP;
7741 }
7742 else if (s[1] == 's' && s[2] == 'p')
7743 {
7744 s += 3;
7745 regno = SP;
7746 }
7747 else if (s[1] == 'g' && s[2] == 'p')
7748 {
7749 s += 3;
7750 regno = GP;
7751 }
7752 else if (s[1] == 'a' && s[2] == 't')
7753 {
7754 s += 3;
7755 regno = AT;
7756 }
7757 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7758 {
7759 s += 4;
7760 regno = KT0;
7761 }
7762 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7763 {
7764 s += 4;
7765 regno = KT1;
7766 }
7767 else if (itbl_have_entries)
7768 {
7769 char *p, *n;
7770 unsigned long r;
7771
7772 p = s + 1; /* advance past '$' */
7773 n = itbl_get_field (&p); /* n is name */
7774
7775 /* See if this is a register defined in an
7776 itbl entry. */
7777 if (itbl_get_reg_val (n, &r))
7778 {
7779 /* Get_field advances to the start of
7780 the next field, so we need to back
7781 rack to the end of the last field. */
7782 if (p)
7783 s = p - 1;
7784 else
7785 s = strchr (s, '\0');
7786 regno = r;
7787 }
7788 else
7789 goto notreg;
7790 }
7791 else
7792 goto notreg;
7793 }
7794 if (regno == AT
7795 && ! mips_opts.noat
7796 && *args != 'E'
7797 && *args != 'G')
7798 as_warn (_("Used $at without \".set noat\""));
7799 c = *args;
7800 if (*s == ' ')
7801 s++;
7802 if (args[1] != *s)
7803 {
7804 if (c == 'r' || c == 'v' || c == 'w')
7805 {
7806 regno = lastregno;
7807 s = s_reset;
7808 args++;
7809 }
7810 }
7811 /* 'z' only matches $0. */
7812 if (c == 'z' && regno != 0)
7813 break;
7814
7815 /* Now that we have assembled one operand, we use the args string
7816 * to figure out where it goes in the instruction. */
7817 switch (c)
7818 {
7819 case 'r':
7820 case 's':
7821 case 'v':
7822 case 'b':
7823 ip->insn_opcode |= regno << OP_SH_RS;
7824 break;
7825 case 'd':
7826 case 'G':
7827 ip->insn_opcode |= regno << OP_SH_RD;
7828 break;
7829 case 'U':
7830 ip->insn_opcode |= regno << OP_SH_RD;
7831 ip->insn_opcode |= regno << OP_SH_RT;
7832 break;
7833 case 'w':
7834 case 't':
7835 case 'E':
7836 ip->insn_opcode |= regno << OP_SH_RT;
7837 break;
7838 case 'x':
7839 /* This case exists because on the r3000 trunc
7840 expands into a macro which requires a gp
7841 register. On the r6000 or r4000 it is
7842 assembled into a single instruction which
7843 ignores the register. Thus the insn version
7844 is MIPS_ISA2 and uses 'x', and the macro
7845 version is MIPS_ISA1 and uses 't'. */
7846 break;
7847 case 'z':
7848 /* This case is for the div instruction, which
7849 acts differently if the destination argument
7850 is $0. This only matches $0, and is checked
7851 outside the switch. */
7852 break;
7853 case 'D':
7854 /* Itbl operand; not yet implemented. FIXME ?? */
7855 break;
7856 /* What about all other operands like 'i', which
7857 can be specified in the opcode table? */
7858 }
7859 lastregno = regno;
7860 continue;
7861 }
7862 notreg:
7863 switch (*args++)
7864 {
7865 case 'r':
7866 case 'v':
7867 ip->insn_opcode |= lastregno << OP_SH_RS;
7868 continue;
7869 case 'w':
7870 ip->insn_opcode |= lastregno << OP_SH_RT;
7871 continue;
7872 }
7873 break;
7874
7875 case 'D': /* floating point destination register */
7876 case 'S': /* floating point source register */
7877 case 'T': /* floating point target register */
7878 case 'R': /* floating point source register */
7879 case 'V':
7880 case 'W':
7881 s_reset = s;
7882 if (s[0] == '$' && s[1] == 'f'
7883 && ISDIGIT (s[2]))
7884 {
7885 s += 2;
7886 regno = 0;
7887 do
7888 {
7889 regno *= 10;
7890 regno += *s - '0';
7891 ++s;
7892 }
7893 while (ISDIGIT (*s));
7894
7895 if (regno > 31)
7896 as_bad (_("Invalid float register number (%d)"), regno);
7897
7898 if ((regno & 1) != 0
7899 && HAVE_32BIT_FPRS
7900 && ! (strcmp (str, "mtc1") == 0
7901 || strcmp (str, "mfc1") == 0
7902 || strcmp (str, "lwc1") == 0
7903 || strcmp (str, "swc1") == 0
7904 || strcmp (str, "l.s") == 0
7905 || strcmp (str, "s.s") == 0))
7906 as_warn (_("Float register should be even, was %d"),
7907 regno);
7908
7909 c = *args;
7910 if (*s == ' ')
7911 s++;
7912 if (args[1] != *s)
7913 {
7914 if (c == 'V' || c == 'W')
7915 {
7916 regno = lastregno;
7917 s = s_reset;
7918 args++;
7919 }
7920 }
7921 switch (c)
7922 {
7923 case 'D':
7924 ip->insn_opcode |= regno << OP_SH_FD;
7925 break;
7926 case 'V':
7927 case 'S':
7928 ip->insn_opcode |= regno << OP_SH_FS;
7929 break;
7930 case 'W':
7931 case 'T':
7932 ip->insn_opcode |= regno << OP_SH_FT;
7933 break;
7934 case 'R':
7935 ip->insn_opcode |= regno << OP_SH_FR;
7936 break;
7937 }
7938 lastregno = regno;
7939 continue;
7940 }
7941
7942 switch (*args++)
7943 {
7944 case 'V':
7945 ip->insn_opcode |= lastregno << OP_SH_FS;
7946 continue;
7947 case 'W':
7948 ip->insn_opcode |= lastregno << OP_SH_FT;
7949 continue;
7950 }
7951 break;
7952
7953 case 'I':
7954 my_getExpression (&imm_expr, s);
7955 if (imm_expr.X_op != O_big
7956 && imm_expr.X_op != O_constant)
7957 insn_error = _("absolute expression required");
7958 s = expr_end;
7959 continue;
7960
7961 case 'A':
7962 my_getExpression (&offset_expr, s);
7963 *imm_reloc = BFD_RELOC_32;
7964 s = expr_end;
7965 continue;
7966
7967 case 'F':
7968 case 'L':
7969 case 'f':
7970 case 'l':
7971 {
7972 int f64;
7973 int using_gprs;
7974 char *save_in;
7975 char *err;
7976 unsigned char temp[8];
7977 int len;
7978 unsigned int length;
7979 segT seg;
7980 subsegT subseg;
7981 char *p;
7982
7983 /* These only appear as the last operand in an
7984 instruction, and every instruction that accepts
7985 them in any variant accepts them in all variants.
7986 This means we don't have to worry about backing out
7987 any changes if the instruction does not match.
7988
7989 The difference between them is the size of the
7990 floating point constant and where it goes. For 'F'
7991 and 'L' the constant is 64 bits; for 'f' and 'l' it
7992 is 32 bits. Where the constant is placed is based
7993 on how the MIPS assembler does things:
7994 F -- .rdata
7995 L -- .lit8
7996 f -- immediate value
7997 l -- .lit4
7998
7999 The .lit4 and .lit8 sections are only used if
8000 permitted by the -G argument.
8001
8002 When generating embedded PIC code, we use the
8003 .lit8 section but not the .lit4 section (we can do
8004 .lit4 inline easily; we need to put .lit8
8005 somewhere in the data segment, and using .lit8
8006 permits the linker to eventually combine identical
8007 .lit8 entries).
8008
8009 The code below needs to know whether the target register
8010 is 32 or 64 bits wide. It relies on the fact 'f' and
8011 'F' are used with GPR-based instructions and 'l' and
8012 'L' are used with FPR-based instructions. */
8013
8014 f64 = *args == 'F' || *args == 'L';
8015 using_gprs = *args == 'F' || *args == 'f';
8016
8017 save_in = input_line_pointer;
8018 input_line_pointer = s;
8019 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8020 length = len;
8021 s = input_line_pointer;
8022 input_line_pointer = save_in;
8023 if (err != NULL && *err != '\0')
8024 {
8025 as_bad (_("Bad floating point constant: %s"), err);
8026 memset (temp, '\0', sizeof temp);
8027 length = f64 ? 8 : 4;
8028 }
8029
8030 assert (length == (unsigned) (f64 ? 8 : 4));
8031
8032 if (*args == 'f'
8033 || (*args == 'l'
8034 && (! USE_GLOBAL_POINTER_OPT
8035 || mips_pic == EMBEDDED_PIC
8036 || g_switch_value < 4
8037 || (temp[0] == 0 && temp[1] == 0)
8038 || (temp[2] == 0 && temp[3] == 0))))
8039 {
8040 imm_expr.X_op = O_constant;
8041 if (! target_big_endian)
8042 imm_expr.X_add_number = bfd_getl32 (temp);
8043 else
8044 imm_expr.X_add_number = bfd_getb32 (temp);
8045 }
8046 else if (length > 4
8047 && ! mips_disable_float_construction
8048 /* Constants can only be constructed in GPRs and
8049 copied to FPRs if the GPRs are at least as wide
8050 as the FPRs. Force the constant into memory if
8051 we are using 64-bit FPRs but the GPRs are only
8052 32 bits wide. */
8053 && (using_gprs
8054 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8055 && ((temp[0] == 0 && temp[1] == 0)
8056 || (temp[2] == 0 && temp[3] == 0))
8057 && ((temp[4] == 0 && temp[5] == 0)
8058 || (temp[6] == 0 && temp[7] == 0)))
8059 {
8060 /* The value is simple enough to load with a couple of
8061 instructions. If using 32-bit registers, set
8062 imm_expr to the high order 32 bits and offset_expr to
8063 the low order 32 bits. Otherwise, set imm_expr to
8064 the entire 64 bit constant. */
8065 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8066 {
8067 imm_expr.X_op = O_constant;
8068 offset_expr.X_op = O_constant;
8069 if (! target_big_endian)
8070 {
8071 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8072 offset_expr.X_add_number = bfd_getl32 (temp);
8073 }
8074 else
8075 {
8076 imm_expr.X_add_number = bfd_getb32 (temp);
8077 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8078 }
8079 if (offset_expr.X_add_number == 0)
8080 offset_expr.X_op = O_absent;
8081 }
8082 else if (sizeof (imm_expr.X_add_number) > 4)
8083 {
8084 imm_expr.X_op = O_constant;
8085 if (! target_big_endian)
8086 imm_expr.X_add_number = bfd_getl64 (temp);
8087 else
8088 imm_expr.X_add_number = bfd_getb64 (temp);
8089 }
8090 else
8091 {
8092 imm_expr.X_op = O_big;
8093 imm_expr.X_add_number = 4;
8094 if (! target_big_endian)
8095 {
8096 generic_bignum[0] = bfd_getl16 (temp);
8097 generic_bignum[1] = bfd_getl16 (temp + 2);
8098 generic_bignum[2] = bfd_getl16 (temp + 4);
8099 generic_bignum[3] = bfd_getl16 (temp + 6);
8100 }
8101 else
8102 {
8103 generic_bignum[0] = bfd_getb16 (temp + 6);
8104 generic_bignum[1] = bfd_getb16 (temp + 4);
8105 generic_bignum[2] = bfd_getb16 (temp + 2);
8106 generic_bignum[3] = bfd_getb16 (temp);
8107 }
8108 }
8109 }
8110 else
8111 {
8112 const char *newname;
8113 segT new_seg;
8114
8115 /* Switch to the right section. */
8116 seg = now_seg;
8117 subseg = now_subseg;
8118 switch (*args)
8119 {
8120 default: /* unused default case avoids warnings. */
8121 case 'L':
8122 newname = RDATA_SECTION_NAME;
8123 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8124 || mips_pic == EMBEDDED_PIC)
8125 newname = ".lit8";
8126 break;
8127 case 'F':
8128 if (mips_pic == EMBEDDED_PIC)
8129 newname = ".lit8";
8130 else
8131 newname = RDATA_SECTION_NAME;
8132 break;
8133 case 'l':
8134 assert (!USE_GLOBAL_POINTER_OPT
8135 || g_switch_value >= 4);
8136 newname = ".lit4";
8137 break;
8138 }
8139 new_seg = subseg_new (newname, (subsegT) 0);
8140 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8141 bfd_set_section_flags (stdoutput, new_seg,
8142 (SEC_ALLOC
8143 | SEC_LOAD
8144 | SEC_READONLY
8145 | SEC_DATA));
8146 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8147 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8148 && strcmp (TARGET_OS, "elf") != 0)
8149 record_alignment (new_seg, 4);
8150 else
8151 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8152 if (seg == now_seg)
8153 as_bad (_("Can't use floating point insn in this section"));
8154
8155 /* Set the argument to the current address in the
8156 section. */
8157 offset_expr.X_op = O_symbol;
8158 offset_expr.X_add_symbol =
8159 symbol_new ("L0\001", now_seg,
8160 (valueT) frag_now_fix (), frag_now);
8161 offset_expr.X_add_number = 0;
8162
8163 /* Put the floating point number into the section. */
8164 p = frag_more ((int) length);
8165 memcpy (p, temp, length);
8166
8167 /* Switch back to the original section. */
8168 subseg_set (seg, subseg);
8169 }
8170 }
8171 continue;
8172
8173 case 'i': /* 16 bit unsigned immediate */
8174 case 'j': /* 16 bit signed immediate */
8175 *imm_reloc = BFD_RELOC_LO16;
8176 c = my_getSmallExpression (&imm_expr, s);
8177 if (c != S_EX_NONE)
8178 {
8179 if (c != S_EX_LO)
8180 {
8181 if (imm_expr.X_op == O_constant)
8182 imm_expr.X_add_number =
8183 (imm_expr.X_add_number >> 16) & 0xffff;
8184 #ifdef OBJ_ELF
8185 else if (c == S_EX_HIGHEST)
8186 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8187 else if (c == S_EX_HIGHER)
8188 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8189 else if (c == S_EX_GP_REL)
8190 {
8191 /* This occurs in NewABI only. */
8192 c = my_getSmallExpression (&imm_expr, s);
8193 if (c != S_EX_NEG)
8194 as_bad (_("bad composition of relocations"));
8195 else
8196 {
8197 c = my_getSmallExpression (&imm_expr, s);
8198 if (c != S_EX_LO)
8199 as_bad (_("bad composition of relocations"));
8200 else
8201 {
8202 imm_reloc[0] = BFD_RELOC_GPREL16;
8203 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8204 imm_reloc[2] = BFD_RELOC_LO16;
8205 }
8206 }
8207 }
8208 #endif
8209 else if (c == S_EX_HI)
8210 {
8211 *imm_reloc = BFD_RELOC_HI16_S;
8212 imm_unmatched_hi = true;
8213 }
8214 else
8215 *imm_reloc = BFD_RELOC_HI16;
8216 }
8217 else if (imm_expr.X_op == O_constant)
8218 imm_expr.X_add_number &= 0xffff;
8219 }
8220 if (*args == 'i')
8221 {
8222 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8223 || ((imm_expr.X_add_number < 0
8224 || imm_expr.X_add_number >= 0x10000)
8225 && imm_expr.X_op == O_constant))
8226 {
8227 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8228 !strcmp (insn->name, insn[1].name))
8229 break;
8230 if (imm_expr.X_op == O_constant
8231 || imm_expr.X_op == O_big)
8232 as_bad (_("16 bit expression not in range 0..65535"));
8233 }
8234 }
8235 else
8236 {
8237 int more;
8238 offsetT max;
8239
8240 /* The upper bound should be 0x8000, but
8241 unfortunately the MIPS assembler accepts numbers
8242 from 0x8000 to 0xffff and sign extends them, and
8243 we want to be compatible. We only permit this
8244 extended range for an instruction which does not
8245 provide any further alternates, since those
8246 alternates may handle other cases. People should
8247 use the numbers they mean, rather than relying on
8248 a mysterious sign extension. */
8249 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8250 strcmp (insn->name, insn[1].name) == 0);
8251 if (more)
8252 max = 0x8000;
8253 else
8254 max = 0x10000;
8255 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8256 || ((imm_expr.X_add_number < -0x8000
8257 || imm_expr.X_add_number >= max)
8258 && imm_expr.X_op == O_constant)
8259 || (more
8260 && imm_expr.X_add_number < 0
8261 && HAVE_64BIT_GPRS
8262 && imm_expr.X_unsigned
8263 && sizeof (imm_expr.X_add_number) <= 4))
8264 {
8265 if (more)
8266 break;
8267 if (imm_expr.X_op == O_constant
8268 || imm_expr.X_op == O_big)
8269 as_bad (_("16 bit expression not in range -32768..32767"));
8270 }
8271 }
8272 s = expr_end;
8273 continue;
8274
8275 case 'o': /* 16 bit offset */
8276 c = my_getSmallExpression (&offset_expr, s);
8277
8278 /* If this value won't fit into a 16 bit offset, then go
8279 find a macro that will generate the 32 bit offset
8280 code pattern. */
8281 if (c == S_EX_NONE
8282 && (offset_expr.X_op != O_constant
8283 || offset_expr.X_add_number >= 0x8000
8284 || offset_expr.X_add_number < -0x8000))
8285 break;
8286
8287 if (c == S_EX_HI)
8288 {
8289 if (offset_expr.X_op != O_constant)
8290 break;
8291 offset_expr.X_add_number =
8292 (offset_expr.X_add_number >> 16) & 0xffff;
8293 }
8294 *offset_reloc = BFD_RELOC_LO16;
8295 s = expr_end;
8296 continue;
8297
8298 case 'p': /* pc relative offset */
8299 if (mips_pic == EMBEDDED_PIC)
8300 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8301 else
8302 *offset_reloc = BFD_RELOC_16_PCREL;
8303 my_getExpression (&offset_expr, s);
8304 s = expr_end;
8305 continue;
8306
8307 case 'u': /* upper 16 bits */
8308 c = my_getSmallExpression (&imm_expr, s);
8309 *imm_reloc = BFD_RELOC_LO16;
8310 if (c != S_EX_NONE)
8311 {
8312 if (c != S_EX_LO)
8313 {
8314 if (imm_expr.X_op == O_constant)
8315 imm_expr.X_add_number =
8316 (imm_expr.X_add_number >> 16) & 0xffff;
8317 #ifdef OBJ_ELF
8318 else if (c == S_EX_HIGHEST)
8319 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8320 else if (c == S_EX_HI)
8321 {
8322 *imm_reloc = BFD_RELOC_HI16_S;
8323 imm_unmatched_hi = true;
8324 }
8325 else if (c == S_EX_GP_REL)
8326 {
8327 /* This occurs in NewABI only. */
8328 c = my_getSmallExpression (&imm_expr, s);
8329 if (c != S_EX_NEG)
8330 as_bad (_("bad composition of relocations"));
8331 else
8332 {
8333 c = my_getSmallExpression (&imm_expr, s);
8334 if (c != S_EX_HI)
8335 as_bad (_("bad composition of relocations"));
8336 else
8337 {
8338 imm_reloc[0] = BFD_RELOC_GPREL16;
8339 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8340 imm_reloc[2] = BFD_RELOC_HI16_S;
8341 }
8342 }
8343 }
8344 #endif
8345 else
8346 *imm_reloc = BFD_RELOC_HI16;
8347 }
8348 else if (imm_expr.X_op == O_constant)
8349 imm_expr.X_add_number &= 0xffff;
8350 }
8351 if (imm_expr.X_op == O_constant
8352 && (imm_expr.X_add_number < 0
8353 || imm_expr.X_add_number >= 0x10000))
8354 as_bad (_("lui expression not in range 0..65535"));
8355 s = expr_end;
8356 continue;
8357
8358 case 'a': /* 26 bit address */
8359 my_getExpression (&offset_expr, s);
8360 s = expr_end;
8361 *offset_reloc = BFD_RELOC_MIPS_JMP;
8362 continue;
8363
8364 case 'N': /* 3 bit branch condition code */
8365 case 'M': /* 3 bit compare condition code */
8366 if (strncmp (s, "$fcc", 4) != 0)
8367 break;
8368 s += 4;
8369 regno = 0;
8370 do
8371 {
8372 regno *= 10;
8373 regno += *s - '0';
8374 ++s;
8375 }
8376 while (ISDIGIT (*s));
8377 if (regno > 7)
8378 as_bad (_("invalid condition code register $fcc%d"), regno);
8379 if (*args == 'N')
8380 ip->insn_opcode |= regno << OP_SH_BCC;
8381 else
8382 ip->insn_opcode |= regno << OP_SH_CCC;
8383 continue;
8384
8385 case 'H':
8386 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8387 s += 2;
8388 if (ISDIGIT (*s))
8389 {
8390 c = 0;
8391 do
8392 {
8393 c *= 10;
8394 c += *s - '0';
8395 ++s;
8396 }
8397 while (ISDIGIT (*s));
8398 }
8399 else
8400 c = 8; /* Invalid sel value. */
8401
8402 if (c > 7)
8403 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8404 ip->insn_opcode |= c;
8405 continue;
8406
8407 default:
8408 as_bad (_("bad char = '%c'\n"), *args);
8409 internalError ();
8410 }
8411 break;
8412 }
8413 /* Args don't match. */
8414 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8415 !strcmp (insn->name, insn[1].name))
8416 {
8417 ++insn;
8418 s = argsStart;
8419 insn_error = _("illegal operands");
8420 continue;
8421 }
8422 if (save_c)
8423 *(--s) = save_c;
8424 insn_error = _("illegal operands");
8425 return;
8426 }
8427 }
8428
8429 /* This routine assembles an instruction into its binary format when
8430 assembling for the mips16. As a side effect, it sets one of the
8431 global variables imm_reloc or offset_reloc to the type of
8432 relocation to do if one of the operands is an address expression.
8433 It also sets mips16_small and mips16_ext if the user explicitly
8434 requested a small or extended instruction. */
8435
8436 static void
8437 mips16_ip (str, ip)
8438 char *str;
8439 struct mips_cl_insn *ip;
8440 {
8441 char *s;
8442 const char *args;
8443 struct mips_opcode *insn;
8444 char *argsstart;
8445 unsigned int regno;
8446 unsigned int lastregno = 0;
8447 char *s_reset;
8448
8449 insn_error = NULL;
8450
8451 mips16_small = false;
8452 mips16_ext = false;
8453
8454 for (s = str; ISLOWER (*s); ++s)
8455 ;
8456 switch (*s)
8457 {
8458 case '\0':
8459 break;
8460
8461 case ' ':
8462 *s++ = '\0';
8463 break;
8464
8465 case '.':
8466 if (s[1] == 't' && s[2] == ' ')
8467 {
8468 *s = '\0';
8469 mips16_small = true;
8470 s += 3;
8471 break;
8472 }
8473 else if (s[1] == 'e' && s[2] == ' ')
8474 {
8475 *s = '\0';
8476 mips16_ext = true;
8477 s += 3;
8478 break;
8479 }
8480 /* Fall through. */
8481 default:
8482 insn_error = _("unknown opcode");
8483 return;
8484 }
8485
8486 if (mips_opts.noautoextend && ! mips16_ext)
8487 mips16_small = true;
8488
8489 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8490 {
8491 insn_error = _("unrecognized opcode");
8492 return;
8493 }
8494
8495 argsstart = s;
8496 for (;;)
8497 {
8498 assert (strcmp (insn->name, str) == 0);
8499
8500 ip->insn_mo = insn;
8501 ip->insn_opcode = insn->match;
8502 ip->use_extend = false;
8503 imm_expr.X_op = O_absent;
8504 imm_reloc[0] = BFD_RELOC_UNUSED;
8505 imm_reloc[1] = BFD_RELOC_UNUSED;
8506 imm_reloc[2] = BFD_RELOC_UNUSED;
8507 offset_expr.X_op = O_absent;
8508 offset_reloc[0] = BFD_RELOC_UNUSED;
8509 offset_reloc[1] = BFD_RELOC_UNUSED;
8510 offset_reloc[2] = BFD_RELOC_UNUSED;
8511 for (args = insn->args; 1; ++args)
8512 {
8513 int c;
8514
8515 if (*s == ' ')
8516 ++s;
8517
8518 /* In this switch statement we call break if we did not find
8519 a match, continue if we did find a match, or return if we
8520 are done. */
8521
8522 c = *args;
8523 switch (c)
8524 {
8525 case '\0':
8526 if (*s == '\0')
8527 {
8528 /* Stuff the immediate value in now, if we can. */
8529 if (imm_expr.X_op == O_constant
8530 && *imm_reloc > BFD_RELOC_UNUSED
8531 && insn->pinfo != INSN_MACRO)
8532 {
8533 mips16_immed ((char *) NULL, 0,
8534 *imm_reloc - BFD_RELOC_UNUSED,
8535 imm_expr.X_add_number, true, mips16_small,
8536 mips16_ext, &ip->insn_opcode,
8537 &ip->use_extend, &ip->extend);
8538 imm_expr.X_op = O_absent;
8539 *imm_reloc = BFD_RELOC_UNUSED;
8540 }
8541
8542 return;
8543 }
8544 break;
8545
8546 case ',':
8547 if (*s++ == c)
8548 continue;
8549 s--;
8550 switch (*++args)
8551 {
8552 case 'v':
8553 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8554 continue;
8555 case 'w':
8556 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8557 continue;
8558 }
8559 break;
8560
8561 case '(':
8562 case ')':
8563 if (*s++ == c)
8564 continue;
8565 break;
8566
8567 case 'v':
8568 case 'w':
8569 if (s[0] != '$')
8570 {
8571 if (c == 'v')
8572 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8573 else
8574 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8575 ++args;
8576 continue;
8577 }
8578 /* Fall through. */
8579 case 'x':
8580 case 'y':
8581 case 'z':
8582 case 'Z':
8583 case '0':
8584 case 'S':
8585 case 'R':
8586 case 'X':
8587 case 'Y':
8588 if (s[0] != '$')
8589 break;
8590 s_reset = s;
8591 if (ISDIGIT (s[1]))
8592 {
8593 ++s;
8594 regno = 0;
8595 do
8596 {
8597 regno *= 10;
8598 regno += *s - '0';
8599 ++s;
8600 }
8601 while (ISDIGIT (*s));
8602 if (regno > 31)
8603 {
8604 as_bad (_("invalid register number (%d)"), regno);
8605 regno = 2;
8606 }
8607 }
8608 else
8609 {
8610 if (s[1] == 'f' && s[2] == 'p')
8611 {
8612 s += 3;
8613 regno = FP;
8614 }
8615 else if (s[1] == 's' && s[2] == 'p')
8616 {
8617 s += 3;
8618 regno = SP;
8619 }
8620 else if (s[1] == 'g' && s[2] == 'p')
8621 {
8622 s += 3;
8623 regno = GP;
8624 }
8625 else if (s[1] == 'a' && s[2] == 't')
8626 {
8627 s += 3;
8628 regno = AT;
8629 }
8630 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8631 {
8632 s += 4;
8633 regno = KT0;
8634 }
8635 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8636 {
8637 s += 4;
8638 regno = KT1;
8639 }
8640 else
8641 break;
8642 }
8643
8644 if (*s == ' ')
8645 ++s;
8646 if (args[1] != *s)
8647 {
8648 if (c == 'v' || c == 'w')
8649 {
8650 regno = mips16_to_32_reg_map[lastregno];
8651 s = s_reset;
8652 args++;
8653 }
8654 }
8655
8656 switch (c)
8657 {
8658 case 'x':
8659 case 'y':
8660 case 'z':
8661 case 'v':
8662 case 'w':
8663 case 'Z':
8664 regno = mips32_to_16_reg_map[regno];
8665 break;
8666
8667 case '0':
8668 if (regno != 0)
8669 regno = ILLEGAL_REG;
8670 break;
8671
8672 case 'S':
8673 if (regno != SP)
8674 regno = ILLEGAL_REG;
8675 break;
8676
8677 case 'R':
8678 if (regno != RA)
8679 regno = ILLEGAL_REG;
8680 break;
8681
8682 case 'X':
8683 case 'Y':
8684 if (regno == AT && ! mips_opts.noat)
8685 as_warn (_("used $at without \".set noat\""));
8686 break;
8687
8688 default:
8689 internalError ();
8690 }
8691
8692 if (regno == ILLEGAL_REG)
8693 break;
8694
8695 switch (c)
8696 {
8697 case 'x':
8698 case 'v':
8699 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
8700 break;
8701 case 'y':
8702 case 'w':
8703 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
8704 break;
8705 case 'z':
8706 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
8707 break;
8708 case 'Z':
8709 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
8710 case '0':
8711 case 'S':
8712 case 'R':
8713 break;
8714 case 'X':
8715 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
8716 break;
8717 case 'Y':
8718 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
8719 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
8720 break;
8721 default:
8722 internalError ();
8723 }
8724
8725 lastregno = regno;
8726 continue;
8727
8728 case 'P':
8729 if (strncmp (s, "$pc", 3) == 0)
8730 {
8731 s += 3;
8732 continue;
8733 }
8734 break;
8735
8736 case '<':
8737 case '>':
8738 case '[':
8739 case ']':
8740 case '4':
8741 case '5':
8742 case 'H':
8743 case 'W':
8744 case 'D':
8745 case 'j':
8746 case '8':
8747 case 'V':
8748 case 'C':
8749 case 'U':
8750 case 'k':
8751 case 'K':
8752 if (s[0] == '%'
8753 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
8754 {
8755 /* This is %gprel(SYMBOL). We need to read SYMBOL,
8756 and generate the appropriate reloc. If the text
8757 inside %gprel is not a symbol name with an
8758 optional offset, then we generate a normal reloc
8759 and will probably fail later. */
8760 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
8761 if (imm_expr.X_op == O_symbol)
8762 {
8763 mips16_ext = true;
8764 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
8765 s = expr_end;
8766 ip->use_extend = true;
8767 ip->extend = 0;
8768 continue;
8769 }
8770 }
8771 else
8772 {
8773 /* Just pick up a normal expression. */
8774 my_getExpression (&imm_expr, s);
8775 }
8776
8777 if (imm_expr.X_op == O_register)
8778 {
8779 /* What we thought was an expression turned out to
8780 be a register. */
8781
8782 if (s[0] == '(' && args[1] == '(')
8783 {
8784 /* It looks like the expression was omitted
8785 before a register indirection, which means
8786 that the expression is implicitly zero. We
8787 still set up imm_expr, so that we handle
8788 explicit extensions correctly. */
8789 imm_expr.X_op = O_constant;
8790 imm_expr.X_add_number = 0;
8791 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
8792 continue;
8793 }
8794
8795 break;
8796 }
8797
8798 /* We need to relax this instruction. */
8799 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
8800 s = expr_end;
8801 continue;
8802
8803 case 'p':
8804 case 'q':
8805 case 'A':
8806 case 'B':
8807 case 'E':
8808 /* We use offset_reloc rather than imm_reloc for the PC
8809 relative operands. This lets macros with both
8810 immediate and address operands work correctly. */
8811 my_getExpression (&offset_expr, s);
8812
8813 if (offset_expr.X_op == O_register)
8814 break;
8815
8816 /* We need to relax this instruction. */
8817 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8818 s = expr_end;
8819 continue;
8820
8821 case '6': /* break code */
8822 my_getExpression (&imm_expr, s);
8823 check_absolute_expr (ip, &imm_expr);
8824 if ((unsigned long) imm_expr.X_add_number > 63)
8825 {
8826 as_warn (_("Invalid value for `%s' (%lu)"),
8827 ip->insn_mo->name,
8828 (unsigned long) imm_expr.X_add_number);
8829 imm_expr.X_add_number &= 0x3f;
8830 }
8831 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
8832 imm_expr.X_op = O_absent;
8833 s = expr_end;
8834 continue;
8835
8836 case 'a': /* 26 bit address */
8837 my_getExpression (&offset_expr, s);
8838 s = expr_end;
8839 *offset_reloc = BFD_RELOC_MIPS16_JMP;
8840 ip->insn_opcode <<= 16;
8841 continue;
8842
8843 case 'l': /* register list for entry macro */
8844 case 'L': /* register list for exit macro */
8845 {
8846 int mask;
8847
8848 if (c == 'l')
8849 mask = 0;
8850 else
8851 mask = 7 << 3;
8852 while (*s != '\0')
8853 {
8854 int freg, reg1, reg2;
8855
8856 while (*s == ' ' || *s == ',')
8857 ++s;
8858 if (*s != '$')
8859 {
8860 as_bad (_("can't parse register list"));
8861 break;
8862 }
8863 ++s;
8864 if (*s != 'f')
8865 freg = 0;
8866 else
8867 {
8868 freg = 1;
8869 ++s;
8870 }
8871 reg1 = 0;
8872 while (ISDIGIT (*s))
8873 {
8874 reg1 *= 10;
8875 reg1 += *s - '0';
8876 ++s;
8877 }
8878 if (*s == ' ')
8879 ++s;
8880 if (*s != '-')
8881 reg2 = reg1;
8882 else
8883 {
8884 ++s;
8885 if (*s != '$')
8886 break;
8887 ++s;
8888 if (freg)
8889 {
8890 if (*s == 'f')
8891 ++s;
8892 else
8893 {
8894 as_bad (_("invalid register list"));
8895 break;
8896 }
8897 }
8898 reg2 = 0;
8899 while (ISDIGIT (*s))
8900 {
8901 reg2 *= 10;
8902 reg2 += *s - '0';
8903 ++s;
8904 }
8905 }
8906 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
8907 {
8908 mask &= ~ (7 << 3);
8909 mask |= 5 << 3;
8910 }
8911 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
8912 {
8913 mask &= ~ (7 << 3);
8914 mask |= 6 << 3;
8915 }
8916 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
8917 mask |= (reg2 - 3) << 3;
8918 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
8919 mask |= (reg2 - 15) << 1;
8920 else if (reg1 == 31 && reg2 == 31)
8921 mask |= 1;
8922 else
8923 {
8924 as_bad (_("invalid register list"));
8925 break;
8926 }
8927 }
8928 /* The mask is filled in in the opcode table for the
8929 benefit of the disassembler. We remove it before
8930 applying the actual mask. */
8931 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
8932 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
8933 }
8934 continue;
8935
8936 case 'e': /* extend code */
8937 my_getExpression (&imm_expr, s);
8938 check_absolute_expr (ip, &imm_expr);
8939 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
8940 {
8941 as_warn (_("Invalid value for `%s' (%lu)"),
8942 ip->insn_mo->name,
8943 (unsigned long) imm_expr.X_add_number);
8944 imm_expr.X_add_number &= 0x7ff;
8945 }
8946 ip->insn_opcode |= imm_expr.X_add_number;
8947 imm_expr.X_op = O_absent;
8948 s = expr_end;
8949 continue;
8950
8951 default:
8952 internalError ();
8953 }
8954 break;
8955 }
8956
8957 /* Args don't match. */
8958 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
8959 strcmp (insn->name, insn[1].name) == 0)
8960 {
8961 ++insn;
8962 s = argsstart;
8963 continue;
8964 }
8965
8966 insn_error = _("illegal operands");
8967
8968 return;
8969 }
8970 }
8971
8972 /* This structure holds information we know about a mips16 immediate
8973 argument type. */
8974
8975 struct mips16_immed_operand
8976 {
8977 /* The type code used in the argument string in the opcode table. */
8978 int type;
8979 /* The number of bits in the short form of the opcode. */
8980 int nbits;
8981 /* The number of bits in the extended form of the opcode. */
8982 int extbits;
8983 /* The amount by which the short form is shifted when it is used;
8984 for example, the sw instruction has a shift count of 2. */
8985 int shift;
8986 /* The amount by which the short form is shifted when it is stored
8987 into the instruction code. */
8988 int op_shift;
8989 /* Non-zero if the short form is unsigned. */
8990 int unsp;
8991 /* Non-zero if the extended form is unsigned. */
8992 int extu;
8993 /* Non-zero if the value is PC relative. */
8994 int pcrel;
8995 };
8996
8997 /* The mips16 immediate operand types. */
8998
8999 static const struct mips16_immed_operand mips16_immed_operands[] =
9000 {
9001 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9002 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9003 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9004 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9005 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9006 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9007 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9008 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9009 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9010 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9011 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9012 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9013 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9014 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9015 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9016 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9017 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9018 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9019 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9020 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9021 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9022 };
9023
9024 #define MIPS16_NUM_IMMED \
9025 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9026
9027 /* Handle a mips16 instruction with an immediate value. This or's the
9028 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9029 whether an extended value is needed; if one is needed, it sets
9030 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9031 If SMALL is true, an unextended opcode was explicitly requested.
9032 If EXT is true, an extended opcode was explicitly requested. If
9033 WARN is true, warn if EXT does not match reality. */
9034
9035 static void
9036 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9037 extend)
9038 char *file;
9039 unsigned int line;
9040 int type;
9041 offsetT val;
9042 boolean warn;
9043 boolean small;
9044 boolean ext;
9045 unsigned long *insn;
9046 boolean *use_extend;
9047 unsigned short *extend;
9048 {
9049 register const struct mips16_immed_operand *op;
9050 int mintiny, maxtiny;
9051 boolean needext;
9052
9053 op = mips16_immed_operands;
9054 while (op->type != type)
9055 {
9056 ++op;
9057 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9058 }
9059
9060 if (op->unsp)
9061 {
9062 if (type == '<' || type == '>' || type == '[' || type == ']')
9063 {
9064 mintiny = 1;
9065 maxtiny = 1 << op->nbits;
9066 }
9067 else
9068 {
9069 mintiny = 0;
9070 maxtiny = (1 << op->nbits) - 1;
9071 }
9072 }
9073 else
9074 {
9075 mintiny = - (1 << (op->nbits - 1));
9076 maxtiny = (1 << (op->nbits - 1)) - 1;
9077 }
9078
9079 /* Branch offsets have an implicit 0 in the lowest bit. */
9080 if (type == 'p' || type == 'q')
9081 val /= 2;
9082
9083 if ((val & ((1 << op->shift) - 1)) != 0
9084 || val < (mintiny << op->shift)
9085 || val > (maxtiny << op->shift))
9086 needext = true;
9087 else
9088 needext = false;
9089
9090 if (warn && ext && ! needext)
9091 as_warn_where (file, line,
9092 _("extended operand requested but not required"));
9093 if (small && needext)
9094 as_bad_where (file, line, _("invalid unextended operand value"));
9095
9096 if (small || (! ext && ! needext))
9097 {
9098 int insnval;
9099
9100 *use_extend = false;
9101 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9102 insnval <<= op->op_shift;
9103 *insn |= insnval;
9104 }
9105 else
9106 {
9107 long minext, maxext;
9108 int extval;
9109
9110 if (op->extu)
9111 {
9112 minext = 0;
9113 maxext = (1 << op->extbits) - 1;
9114 }
9115 else
9116 {
9117 minext = - (1 << (op->extbits - 1));
9118 maxext = (1 << (op->extbits - 1)) - 1;
9119 }
9120 if (val < minext || val > maxext)
9121 as_bad_where (file, line,
9122 _("operand value out of range for instruction"));
9123
9124 *use_extend = true;
9125 if (op->extbits == 16)
9126 {
9127 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9128 val &= 0x1f;
9129 }
9130 else if (op->extbits == 15)
9131 {
9132 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9133 val &= 0xf;
9134 }
9135 else
9136 {
9137 extval = ((val & 0x1f) << 6) | (val & 0x20);
9138 val = 0;
9139 }
9140
9141 *extend = (unsigned short) extval;
9142 *insn |= val;
9143 }
9144 }
9145 \f
9146 static struct percent_op_match
9147 {
9148 const char *str;
9149 const enum small_ex_type type;
9150 } percent_op[] =
9151 {
9152 #ifdef OBJ_ELF
9153 {"%half", S_EX_HALF},
9154 #endif
9155 {"%hi", S_EX_HI},
9156 {"%lo", S_EX_LO},
9157 #ifdef OBJ_ELF
9158 {"%gp_rel", S_EX_GP_REL},
9159 {"%got", S_EX_GOT},
9160 {"%call16", S_EX_CALL16},
9161 {"%got_disp", S_EX_GOT_DISP},
9162 {"%got_page", S_EX_GOT_PAGE},
9163 {"%got_ofst", S_EX_GOT_OFST},
9164 {"%got_hi", S_EX_GOT_HI},
9165 {"%got_lo", S_EX_GOT_LO},
9166 {"%neg", S_EX_NEG},
9167 {"%higher", S_EX_HIGHER},
9168 {"%highest", S_EX_HIGHEST},
9169 {"%call_hi", S_EX_CALL_HI},
9170 {"%call_lo", S_EX_CALL_LO}
9171 #endif
9172 };
9173
9174 /* Parse small expression input. STR gets adjusted to eat up whitespace.
9175 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9176 can be nested, this is handled by blanking the innermost, parsing the
9177 rest by subsequent calls. */
9178
9179 static int
9180 my_getSmallParser (str, len, nestlevel)
9181 char **str;
9182 unsigned int *len;
9183 int *nestlevel;
9184 {
9185 int type = S_EX_NONE;
9186
9187 *len = 0;
9188 *str += strspn (*str, " \t");
9189 if (**str == '(')
9190 {
9191 char *b = *str + 1 + strspn (*str + 1, " \t");
9192 char *e;
9193
9194 /* Check for base register. */
9195 if (b[0] == '$')
9196 {
9197 if (strchr (b, ')')
9198 && (e = b + strcspn (b, ") \t"))
9199 && e - b > 1 && e - b < 4)
9200 {
9201 if ((e - b == 3
9202 && ((b[1] == 'f' && b[2] == 'p')
9203 || (b[1] == 's' && b[2] == 'p')
9204 || (b[1] == 'g' && b[2] == 'p')
9205 || (b[1] == 'a' && b[2] == 't')
9206 || (ISDIGIT (b[1])
9207 && ISDIGIT (b[2]))))
9208 || (ISDIGIT (b[1])))
9209 {
9210 *len = strcspn (*str, ")") + 1;
9211 return S_EX_REGISTER;
9212 }
9213 }
9214 }
9215 else if (b[0] == '%')
9216 {
9217 *str = b;
9218 goto percent_op;
9219 }
9220
9221 /* Some other expression in the braces. */
9222 *len = strcspn (*str, ")") + 1;
9223 }
9224 /* Check for percent_op. */
9225 else if (*str[0] == '%')
9226 {
9227 char *tmp;
9228 unsigned int i;
9229
9230 percent_op:
9231 tmp = *str + 1;
9232 i = 0;
9233
9234 while (ISALPHA (*tmp) || *tmp == '_')
9235 {
9236 *tmp = TOLOWER (*tmp);
9237 tmp++;
9238 }
9239 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9240 {
9241 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9242 i++;
9243 else
9244 {
9245 type = percent_op[i].type;
9246
9247 /* Only %hi and %lo are allowed for OldABI. */
9248 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9249 return S_EX_NONE;
9250
9251 *len = strlen (percent_op[i].str);
9252 (*nestlevel)++;
9253 return type;
9254 }
9255 }
9256 }
9257
9258 /* Any other expression. */
9259 return S_EX_NONE;
9260 }
9261
9262 static int
9263 my_getSmallExpression (ep, str)
9264 expressionS *ep;
9265 char *str;
9266 {
9267 static char *oldstr = NULL;
9268 int c = S_EX_NONE;
9269 int oldc;
9270 int nest_level = 0;
9271 unsigned int len;
9272
9273 /* Don't update oldstr if the last call had nested percent_op's. */
9274 if (! oldstr)
9275 oldstr = str;
9276
9277 do
9278 {
9279 oldc = c;
9280 c = my_getSmallParser (&str, &len, &nest_level);
9281 if (c != S_EX_NONE && c != S_EX_REGISTER)
9282 str += len;
9283 }
9284 while (c != S_EX_NONE && c != S_EX_REGISTER);
9285
9286 /* A percent_op was encountered. */
9287 if (nest_level)
9288 {
9289 /* Don't try to get an expression if it is already blanked out. */
9290 if (*(str + strspn (str + 1, " )")) != ')')
9291 {
9292 char save;
9293
9294 save = *(str + len);
9295 *(str + len) = '\0';
9296 my_getExpression (ep, str);
9297 *(str + len) = save;
9298 }
9299 if (nest_level > 1)
9300 {
9301 /* blank out including the % sign. */
9302 char *p = strrchr (oldstr, '%');
9303 memset (p, ' ', str - p + len);
9304 str = oldstr;
9305 }
9306 else
9307 {
9308 expr_end = strchr (str, ')') + 1;
9309 }
9310 c = oldc;
9311 }
9312 else if (c == S_EX_NONE)
9313 {
9314 my_getExpression (ep, str);
9315 }
9316 else if (c == S_EX_REGISTER)
9317 {
9318 ep->X_op = O_constant;
9319 expr_end = str;
9320 ep->X_add_symbol = NULL;
9321 ep->X_op_symbol = NULL;
9322 ep->X_add_number = 0;
9323 }
9324 else
9325 {
9326 as_fatal(_("internal error"));
9327 }
9328
9329 if (nest_level <= 1)
9330 oldstr = NULL;
9331
9332 return c;
9333 }
9334
9335 static void
9336 my_getExpression (ep, str)
9337 expressionS *ep;
9338 char *str;
9339 {
9340 char *save_in;
9341 valueT val;
9342
9343 save_in = input_line_pointer;
9344 input_line_pointer = str;
9345 expression (ep);
9346 expr_end = input_line_pointer;
9347 input_line_pointer = save_in;
9348
9349 /* If we are in mips16 mode, and this is an expression based on `.',
9350 then we bump the value of the symbol by 1 since that is how other
9351 text symbols are handled. We don't bother to handle complex
9352 expressions, just `.' plus or minus a constant. */
9353 if (mips_opts.mips16
9354 && ep->X_op == O_symbol
9355 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9356 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9357 && symbol_get_frag (ep->X_add_symbol) == frag_now
9358 && symbol_constant_p (ep->X_add_symbol)
9359 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9360 S_SET_VALUE (ep->X_add_symbol, val + 1);
9361 }
9362
9363 /* Turn a string in input_line_pointer into a floating point constant
9364 of type TYPE, and store the appropriate bytes in *LITP. The number
9365 of LITTLENUMS emitted is stored in *SIZEP. An error message is
9366 returned, or NULL on OK. */
9367
9368 char *
9369 md_atof (type, litP, sizeP)
9370 int type;
9371 char *litP;
9372 int *sizeP;
9373 {
9374 int prec;
9375 LITTLENUM_TYPE words[4];
9376 char *t;
9377 int i;
9378
9379 switch (type)
9380 {
9381 case 'f':
9382 prec = 2;
9383 break;
9384
9385 case 'd':
9386 prec = 4;
9387 break;
9388
9389 default:
9390 *sizeP = 0;
9391 return _("bad call to md_atof");
9392 }
9393
9394 t = atof_ieee (input_line_pointer, type, words);
9395 if (t)
9396 input_line_pointer = t;
9397
9398 *sizeP = prec * 2;
9399
9400 if (! target_big_endian)
9401 {
9402 for (i = prec - 1; i >= 0; i--)
9403 {
9404 md_number_to_chars (litP, (valueT) words[i], 2);
9405 litP += 2;
9406 }
9407 }
9408 else
9409 {
9410 for (i = 0; i < prec; i++)
9411 {
9412 md_number_to_chars (litP, (valueT) words[i], 2);
9413 litP += 2;
9414 }
9415 }
9416
9417 return NULL;
9418 }
9419
9420 void
9421 md_number_to_chars (buf, val, n)
9422 char *buf;
9423 valueT val;
9424 int n;
9425 {
9426 if (target_big_endian)
9427 number_to_chars_bigendian (buf, val, n);
9428 else
9429 number_to_chars_littleendian (buf, val, n);
9430 }
9431 \f
9432 static int support_64bit_objects(void)
9433 {
9434 const char **list, **l;
9435
9436 list = bfd_target_list ();
9437 for (l = list; *l != NULL; l++)
9438 #ifdef TE_TMIPS
9439 /* This is traditional mips */
9440 if (strcmp (*l, "elf64-tradbigmips") == 0
9441 || strcmp (*l, "elf64-tradlittlemips") == 0)
9442 #else
9443 if (strcmp (*l, "elf64-bigmips") == 0
9444 || strcmp (*l, "elf64-littlemips") == 0)
9445 #endif
9446 break;
9447 free (list);
9448 return (*l != NULL);
9449 }
9450
9451 CONST char *md_shortopts = "nO::g::G:";
9452
9453 struct option md_longopts[] =
9454 {
9455 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9456 {"mips0", no_argument, NULL, OPTION_MIPS1},
9457 {"mips1", no_argument, NULL, OPTION_MIPS1},
9458 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9459 {"mips2", no_argument, NULL, OPTION_MIPS2},
9460 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9461 {"mips3", no_argument, NULL, OPTION_MIPS3},
9462 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9463 {"mips4", no_argument, NULL, OPTION_MIPS4},
9464 #define OPTION_MCPU (OPTION_MD_BASE + 5)
9465 {"mcpu", required_argument, NULL, OPTION_MCPU},
9466 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 6)
9467 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9468 #define OPTION_TRAP (OPTION_MD_BASE + 7)
9469 {"trap", no_argument, NULL, OPTION_TRAP},
9470 {"no-break", no_argument, NULL, OPTION_TRAP},
9471 #define OPTION_BREAK (OPTION_MD_BASE + 8)
9472 {"break", no_argument, NULL, OPTION_BREAK},
9473 {"no-trap", no_argument, NULL, OPTION_BREAK},
9474 #define OPTION_EB (OPTION_MD_BASE + 9)
9475 {"EB", no_argument, NULL, OPTION_EB},
9476 #define OPTION_EL (OPTION_MD_BASE + 10)
9477 {"EL", no_argument, NULL, OPTION_EL},
9478 #define OPTION_M4650 (OPTION_MD_BASE + 11)
9479 {"m4650", no_argument, NULL, OPTION_M4650},
9480 #define OPTION_NO_M4650 (OPTION_MD_BASE + 12)
9481 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9482 #define OPTION_M4010 (OPTION_MD_BASE + 13)
9483 {"m4010", no_argument, NULL, OPTION_M4010},
9484 #define OPTION_NO_M4010 (OPTION_MD_BASE + 14)
9485 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9486 #define OPTION_M4100 (OPTION_MD_BASE + 15)
9487 {"m4100", no_argument, NULL, OPTION_M4100},
9488 #define OPTION_NO_M4100 (OPTION_MD_BASE + 16)
9489 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9490 #define OPTION_MIPS16 (OPTION_MD_BASE + 17)
9491 {"mips16", no_argument, NULL, OPTION_MIPS16},
9492 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 18)
9493 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9494 #define OPTION_M3900 (OPTION_MD_BASE + 19)
9495 {"m3900", no_argument, NULL, OPTION_M3900},
9496 #define OPTION_NO_M3900 (OPTION_MD_BASE + 20)
9497 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9498 #define OPTION_MABI (OPTION_MD_BASE + 21)
9499 {"mabi", required_argument, NULL, OPTION_MABI},
9500 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 22)
9501 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9502 #define OPTION_NO_M7000_HILO_FIX (OPTION_MD_BASE + 23)
9503 {"no-fix-7000", no_argument, NULL, OPTION_NO_M7000_HILO_FIX},
9504 #define OPTION_GP32 (OPTION_MD_BASE + 24)
9505 {"mgp32", no_argument, NULL, OPTION_GP32},
9506 #define OPTION_GP64 (OPTION_MD_BASE + 25)
9507 {"mgp64", no_argument, NULL, OPTION_GP64},
9508 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 26)
9509 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9510 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 27)
9511 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9512 #define OPTION_MIPS32 (OPTION_MD_BASE + 28)
9513 {"mips32", no_argument, NULL, OPTION_MIPS32},
9514 #define OPTION_MIPS5 (OPTION_MD_BASE + 29)
9515 {"mips5", no_argument, NULL, OPTION_MIPS5},
9516 #define OPTION_MIPS64 (OPTION_MD_BASE + 30)
9517 {"mips64", no_argument, NULL, OPTION_MIPS64},
9518 #define OPTION_MARCH (OPTION_MD_BASE + 31)
9519 {"march", required_argument, NULL, OPTION_MARCH},
9520 #define OPTION_MTUNE (OPTION_MD_BASE + 32)
9521 {"mtune", required_argument, NULL, OPTION_MTUNE},
9522 #define OPTION_FP32 (OPTION_MD_BASE + 33)
9523 {"mfp32", no_argument, NULL, OPTION_FP32},
9524 #ifdef OBJ_ELF
9525 #define OPTION_ELF_BASE (OPTION_MD_BASE + 35)
9526 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9527 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
9528 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
9529 #define OPTION_32 (OPTION_ELF_BASE + 3)
9530 #define OPTION_N32 (OPTION_ELF_BASE + 4)
9531 #define OPTION_64 (OPTION_ELF_BASE + 5)
9532 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
9533 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9534 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
9535 {"xgot", no_argument, NULL, OPTION_XGOT},
9536 {"32", no_argument, NULL, OPTION_32},
9537 {"n32", no_argument, NULL, OPTION_N32},
9538 {"64", no_argument, NULL, OPTION_64},
9539 #endif
9540
9541 {NULL, no_argument, NULL, 0}
9542 };
9543 size_t md_longopts_size = sizeof (md_longopts);
9544
9545 int
9546 md_parse_option (c, arg)
9547 int c;
9548 char *arg;
9549 {
9550 switch (c)
9551 {
9552 case OPTION_CONSTRUCT_FLOATS:
9553 mips_disable_float_construction = 0;
9554 break;
9555
9556 case OPTION_NO_CONSTRUCT_FLOATS:
9557 mips_disable_float_construction = 1;
9558 break;
9559
9560 case OPTION_TRAP:
9561 mips_trap = 1;
9562 break;
9563
9564 case OPTION_BREAK:
9565 mips_trap = 0;
9566 break;
9567
9568 case OPTION_EB:
9569 target_big_endian = 1;
9570 break;
9571
9572 case OPTION_EL:
9573 target_big_endian = 0;
9574 break;
9575
9576 case 'n':
9577 warn_nops = 1;
9578 break;
9579
9580 case 'O':
9581 if (arg && arg[1] == '0')
9582 mips_optimize = 1;
9583 else
9584 mips_optimize = 2;
9585 break;
9586
9587 case 'g':
9588 if (arg == NULL)
9589 mips_debug = 2;
9590 else
9591 mips_debug = atoi (arg);
9592 /* When the MIPS assembler sees -g or -g2, it does not do
9593 optimizations which limit full symbolic debugging. We take
9594 that to be equivalent to -O0. */
9595 if (mips_debug == 2)
9596 mips_optimize = 1;
9597 break;
9598
9599 case OPTION_MIPS1:
9600 mips_opts.isa = ISA_MIPS1;
9601 break;
9602
9603 case OPTION_MIPS2:
9604 mips_opts.isa = ISA_MIPS2;
9605 break;
9606
9607 case OPTION_MIPS3:
9608 mips_opts.isa = ISA_MIPS3;
9609 break;
9610
9611 case OPTION_MIPS4:
9612 mips_opts.isa = ISA_MIPS4;
9613 break;
9614
9615 case OPTION_MIPS5:
9616 mips_opts.isa = ISA_MIPS5;
9617 break;
9618
9619 case OPTION_MIPS32:
9620 mips_opts.isa = ISA_MIPS32;
9621 break;
9622
9623 case OPTION_MIPS64:
9624 mips_opts.isa = ISA_MIPS64;
9625 break;
9626
9627 case OPTION_MTUNE:
9628 case OPTION_MARCH:
9629 case OPTION_MCPU:
9630 {
9631 int cpu = CPU_UNKNOWN;
9632
9633 /* Identify the processor type. */
9634 if (strcasecmp (arg, "default") != 0)
9635 {
9636 const struct mips_cpu_info *ci;
9637
9638 ci = mips_cpu_info_from_name (arg);
9639 if (ci == NULL || ci->is_isa)
9640 {
9641 switch (c)
9642 {
9643 case OPTION_MTUNE:
9644 as_fatal (_("invalid architecture -mtune=%s"), arg);
9645 break;
9646 case OPTION_MARCH:
9647 as_fatal (_("invalid architecture -march=%s"), arg);
9648 break;
9649 case OPTION_MCPU:
9650 as_fatal (_("invalid architecture -mcpu=%s"), arg);
9651 break;
9652 }
9653 }
9654 else
9655 cpu = ci->cpu;
9656 }
9657
9658 switch (c)
9659 {
9660 case OPTION_MTUNE:
9661 if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
9662 as_warn(_("A different -mtune= was already specified, is now "
9663 "-mtune=%s"), arg);
9664 mips_tune = cpu;
9665 break;
9666 case OPTION_MARCH:
9667 if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
9668 as_warn(_("A different -march= was already specified, is now "
9669 "-march=%s"), arg);
9670 mips_arch = cpu;
9671 break;
9672 case OPTION_MCPU:
9673 if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
9674 as_warn(_("A different -mcpu= was already specified, is now "
9675 "-mcpu=%s"), arg);
9676 mips_cpu = cpu;
9677 }
9678 }
9679 break;
9680
9681 case OPTION_M4650:
9682 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
9683 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
9684 as_warn(_("A different -march= or -mtune= was already specified, "
9685 "is now -m4650"));
9686 mips_arch = CPU_R4650;
9687 mips_tune = CPU_R4650;
9688 break;
9689
9690 case OPTION_NO_M4650:
9691 break;
9692
9693 case OPTION_M4010:
9694 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
9695 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
9696 as_warn(_("A different -march= or -mtune= was already specified, "
9697 "is now -m4010"));
9698 mips_arch = CPU_R4010;
9699 mips_tune = CPU_R4010;
9700 break;
9701
9702 case OPTION_NO_M4010:
9703 break;
9704
9705 case OPTION_M4100:
9706 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
9707 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
9708 as_warn(_("A different -march= or -mtune= was already specified, "
9709 "is now -m4100"));
9710 mips_arch = CPU_VR4100;
9711 mips_tune = CPU_VR4100;
9712 break;
9713
9714 case OPTION_NO_M4100:
9715 break;
9716
9717 case OPTION_M3900:
9718 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
9719 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
9720 as_warn(_("A different -march= or -mtune= was already specified, "
9721 "is now -m3900"));
9722 mips_arch = CPU_R3900;
9723 mips_tune = CPU_R3900;
9724 break;
9725
9726 case OPTION_NO_M3900:
9727 break;
9728
9729 case OPTION_MIPS16:
9730 mips_opts.mips16 = 1;
9731 mips_no_prev_insn (false);
9732 break;
9733
9734 case OPTION_NO_MIPS16:
9735 mips_opts.mips16 = 0;
9736 mips_no_prev_insn (false);
9737 break;
9738
9739 case OPTION_MEMBEDDED_PIC:
9740 mips_pic = EMBEDDED_PIC;
9741 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
9742 {
9743 as_bad (_("-G may not be used with embedded PIC code"));
9744 return 0;
9745 }
9746 g_switch_value = 0x7fffffff;
9747 break;
9748
9749 #ifdef OBJ_ELF
9750 /* When generating ELF code, we permit -KPIC and -call_shared to
9751 select SVR4_PIC, and -non_shared to select no PIC. This is
9752 intended to be compatible with Irix 5. */
9753 case OPTION_CALL_SHARED:
9754 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9755 {
9756 as_bad (_("-call_shared is supported only for ELF format"));
9757 return 0;
9758 }
9759 mips_pic = SVR4_PIC;
9760 if (g_switch_seen && g_switch_value != 0)
9761 {
9762 as_bad (_("-G may not be used with SVR4 PIC code"));
9763 return 0;
9764 }
9765 g_switch_value = 0;
9766 break;
9767
9768 case OPTION_NON_SHARED:
9769 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9770 {
9771 as_bad (_("-non_shared is supported only for ELF format"));
9772 return 0;
9773 }
9774 mips_pic = NO_PIC;
9775 break;
9776
9777 /* The -xgot option tells the assembler to use 32 offsets when
9778 accessing the got in SVR4_PIC mode. It is for Irix
9779 compatibility. */
9780 case OPTION_XGOT:
9781 mips_big_got = 1;
9782 break;
9783 #endif /* OBJ_ELF */
9784
9785 case 'G':
9786 if (! USE_GLOBAL_POINTER_OPT)
9787 {
9788 as_bad (_("-G is not supported for this configuration"));
9789 return 0;
9790 }
9791 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
9792 {
9793 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
9794 return 0;
9795 }
9796 else
9797 g_switch_value = atoi (arg);
9798 g_switch_seen = 1;
9799 break;
9800
9801 #ifdef OBJ_ELF
9802 /* The -32 and -64 options tell the assembler to output the 32
9803 bit or the 64 bit MIPS ELF format. */
9804 case OPTION_32:
9805 mips_abi = O32_ABI;
9806 break;
9807
9808 case OPTION_N32:
9809 mips_abi = N32_ABI;
9810 break;
9811
9812 case OPTION_64:
9813 mips_abi = N64_ABI;
9814 if (! support_64bit_objects())
9815 as_fatal (_("No compiled in support for 64 bit object file format"));
9816 break;
9817
9818 case OPTION_GP32:
9819 mips_gp32 = 1;
9820 if (mips_abi != O32_ABI)
9821 mips_abi = NO_ABI;
9822 break;
9823
9824 case OPTION_GP64:
9825 mips_gp32 = 0;
9826 if (mips_abi == O32_ABI)
9827 mips_abi = NO_ABI;
9828 break;
9829
9830 case OPTION_FP32:
9831 mips_fp32 = 1;
9832 if (mips_abi != O32_ABI)
9833 mips_abi = NO_ABI;
9834 break;
9835
9836 case OPTION_MABI:
9837 if (strcmp (arg, "32") == 0)
9838 mips_abi = O32_ABI;
9839 else if (strcmp (arg, "o64") == 0)
9840 mips_abi = O64_ABI;
9841 else if (strcmp (arg, "n32") == 0)
9842 mips_abi = N32_ABI;
9843 else if (strcmp (arg, "64") == 0)
9844 {
9845 mips_abi = N64_ABI;
9846 if (! support_64bit_objects())
9847 as_fatal (_("No compiled in support for 64 bit object file "
9848 "format"));
9849 }
9850 else if (strcmp (arg, "eabi") == 0)
9851 mips_abi = EABI_ABI;
9852 else
9853 mips_abi = NO_ABI;
9854 break;
9855 #endif /* OBJ_ELF */
9856
9857 case OPTION_M7000_HILO_FIX:
9858 mips_7000_hilo_fix = true;
9859 break;
9860
9861 case OPTION_NO_M7000_HILO_FIX:
9862 mips_7000_hilo_fix = false;
9863 break;
9864
9865 default:
9866 return 0;
9867 }
9868
9869 return 1;
9870 }
9871
9872 static void
9873 show (stream, string, col_p, first_p)
9874 FILE *stream;
9875 char *string;
9876 int *col_p;
9877 int *first_p;
9878 {
9879 if (*first_p)
9880 {
9881 fprintf (stream, "%24s", "");
9882 *col_p = 24;
9883 }
9884 else
9885 {
9886 fprintf (stream, ", ");
9887 *col_p += 2;
9888 }
9889
9890 if (*col_p + strlen (string) > 72)
9891 {
9892 fprintf (stream, "\n%24s", "");
9893 *col_p = 24;
9894 }
9895
9896 fprintf (stream, "%s", string);
9897 *col_p += strlen (string);
9898
9899 *first_p = 0;
9900 }
9901
9902 void
9903 md_show_usage (stream)
9904 FILE *stream;
9905 {
9906 int column, first;
9907
9908 fprintf (stream, _("\
9909 MIPS options:\n\
9910 -membedded-pic generate embedded position independent code\n\
9911 -EB generate big endian output\n\
9912 -EL generate little endian output\n\
9913 -g, -g2 do not remove unneeded NOPs or swap branches\n\
9914 -G NUM allow referencing objects up to NUM bytes\n\
9915 implicitly with the gp register [default 8]\n"));
9916 fprintf (stream, _("\
9917 -mips1 generate MIPS ISA I instructions\n\
9918 -mips2 generate MIPS ISA II instructions\n\
9919 -mips3 generate MIPS ISA III instructions\n\
9920 -mips4 generate MIPS ISA IV instructions\n\
9921 -mips5 generate MIPS ISA V instructions\n\
9922 -mips32 generate MIPS32 ISA instructions\n\
9923 -mips64 generate MIPS64 ISA instructions\n\
9924 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
9925
9926 first = 1;
9927
9928 show (stream, "2000", &column, &first);
9929 show (stream, "3000", &column, &first);
9930 show (stream, "3900", &column, &first);
9931 show (stream, "4000", &column, &first);
9932 show (stream, "4010", &column, &first);
9933 show (stream, "4100", &column, &first);
9934 show (stream, "4111", &column, &first);
9935 show (stream, "4300", &column, &first);
9936 show (stream, "4400", &column, &first);
9937 show (stream, "4600", &column, &first);
9938 show (stream, "4650", &column, &first);
9939 show (stream, "5000", &column, &first);
9940 show (stream, "5200", &column, &first);
9941 show (stream, "5230", &column, &first);
9942 show (stream, "5231", &column, &first);
9943 show (stream, "5261", &column, &first);
9944 show (stream, "5721", &column, &first);
9945 show (stream, "6000", &column, &first);
9946 show (stream, "8000", &column, &first);
9947 show (stream, "10000", &column, &first);
9948 show (stream, "12000", &column, &first);
9949 show (stream, "sb1", &column, &first);
9950 fputc ('\n', stream);
9951
9952 fprintf (stream, _("\
9953 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
9954 -no-mCPU don't generate code specific to CPU.\n\
9955 For -mCPU and -no-mCPU, CPU must be one of:\n"));
9956
9957 first = 1;
9958
9959 show (stream, "3900", &column, &first);
9960 show (stream, "4010", &column, &first);
9961 show (stream, "4100", &column, &first);
9962 show (stream, "4650", &column, &first);
9963 fputc ('\n', stream);
9964
9965 fprintf (stream, _("\
9966 -mips16 generate mips16 instructions\n\
9967 -no-mips16 do not generate mips16 instructions\n"));
9968 fprintf (stream, _("\
9969 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
9970 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
9971 -O0 remove unneeded NOPs, do not swap branches\n\
9972 -O remove unneeded NOPs and swap branches\n\
9973 -n warn about NOPs generated from macros\n\
9974 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
9975 --trap, --no-break trap exception on div by 0 and mult overflow\n\
9976 --break, --no-trap break exception on div by 0 and mult overflow\n"));
9977 #ifdef OBJ_ELF
9978 fprintf (stream, _("\
9979 -KPIC, -call_shared generate SVR4 position independent code\n\
9980 -non_shared do not generate position independent code\n\
9981 -xgot assume a 32 bit GOT\n\
9982 -32 create o32 ABI object file (default)\n\
9983 -n32 create n32 ABI object file\n\
9984 -64 create 64 ABI object file\n"));
9985 #endif
9986 }
9987 \f
9988 void
9989 mips_init_after_args ()
9990 {
9991 /* initialize opcodes */
9992 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
9993 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
9994 }
9995
9996 long
9997 md_pcrel_from (fixP)
9998 fixS *fixP;
9999 {
10000 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10001 && fixP->fx_addsy != (symbolS *) NULL
10002 && ! S_IS_DEFINED (fixP->fx_addsy))
10003 {
10004 /* This makes a branch to an undefined symbol be a branch to the
10005 current location. */
10006 if (mips_pic == EMBEDDED_PIC)
10007 return 4;
10008 else
10009 return 1;
10010 }
10011
10012 /* return the address of the delay slot */
10013 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10014 }
10015
10016 /* This is called before the symbol table is processed. In order to
10017 work with gcc when using mips-tfile, we must keep all local labels.
10018 However, in other cases, we want to discard them. If we were
10019 called with -g, but we didn't see any debugging information, it may
10020 mean that gcc is smuggling debugging information through to
10021 mips-tfile, in which case we must generate all local labels. */
10022
10023 void
10024 mips_frob_file_before_adjust ()
10025 {
10026 #ifndef NO_ECOFF_DEBUGGING
10027 if (ECOFF_DEBUGGING
10028 && mips_debug != 0
10029 && ! ecoff_debugging_seen)
10030 flag_keep_locals = 1;
10031 #endif
10032 }
10033
10034 /* Sort any unmatched HI16_S relocs so that they immediately precede
10035 the corresponding LO reloc. This is called before md_apply_fix and
10036 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10037 explicit use of the %hi modifier. */
10038
10039 void
10040 mips_frob_file ()
10041 {
10042 struct mips_hi_fixup *l;
10043
10044 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10045 {
10046 segment_info_type *seginfo;
10047 int pass;
10048
10049 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10050
10051 /* Check quickly whether the next fixup happens to be a matching
10052 %lo. */
10053 if (l->fixp->fx_next != NULL
10054 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10055 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10056 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10057 continue;
10058
10059 /* Look through the fixups for this segment for a matching %lo.
10060 When we find one, move the %hi just in front of it. We do
10061 this in two passes. In the first pass, we try to find a
10062 unique %lo. In the second pass, we permit multiple %hi
10063 relocs for a single %lo (this is a GNU extension). */
10064 seginfo = seg_info (l->seg);
10065 for (pass = 0; pass < 2; pass++)
10066 {
10067 fixS *f, *prev;
10068
10069 prev = NULL;
10070 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10071 {
10072 /* Check whether this is a %lo fixup which matches l->fixp. */
10073 if (f->fx_r_type == BFD_RELOC_LO16
10074 && f->fx_addsy == l->fixp->fx_addsy
10075 && f->fx_offset == l->fixp->fx_offset
10076 && (pass == 1
10077 || prev == NULL
10078 || prev->fx_r_type != BFD_RELOC_HI16_S
10079 || prev->fx_addsy != f->fx_addsy
10080 || prev->fx_offset != f->fx_offset))
10081 {
10082 fixS **pf;
10083
10084 /* Move l->fixp before f. */
10085 for (pf = &seginfo->fix_root;
10086 *pf != l->fixp;
10087 pf = &(*pf)->fx_next)
10088 assert (*pf != NULL);
10089
10090 *pf = l->fixp->fx_next;
10091
10092 l->fixp->fx_next = f;
10093 if (prev == NULL)
10094 seginfo->fix_root = l->fixp;
10095 else
10096 prev->fx_next = l->fixp;
10097
10098 break;
10099 }
10100
10101 prev = f;
10102 }
10103
10104 if (f != NULL)
10105 break;
10106
10107 #if 0 /* GCC code motion plus incomplete dead code elimination
10108 can leave a %hi without a %lo. */
10109 if (pass == 1)
10110 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10111 _("Unmatched %%hi reloc"));
10112 #endif
10113 }
10114 }
10115 }
10116
10117 /* When generating embedded PIC code we need to use a special
10118 relocation to represent the difference of two symbols in the .text
10119 section (switch tables use a difference of this sort). See
10120 include/coff/mips.h for details. This macro checks whether this
10121 fixup requires the special reloc. */
10122 #define SWITCH_TABLE(fixp) \
10123 ((fixp)->fx_r_type == BFD_RELOC_32 \
10124 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10125 && (fixp)->fx_addsy != NULL \
10126 && (fixp)->fx_subsy != NULL \
10127 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10128 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10129
10130 /* When generating embedded PIC code we must keep all PC relative
10131 relocations, in case the linker has to relax a call. We also need
10132 to keep relocations for switch table entries.
10133
10134 We may have combined relocations without symbols in the N32/N64 ABI.
10135 We have to prevent gas from dropping them. */
10136
10137 int
10138 mips_force_relocation (fixp)
10139 fixS *fixp;
10140 {
10141 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10142 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10143 return 1;
10144
10145 if (HAVE_NEWABI
10146 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10147 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10148 || fixp->fx_r_type == BFD_RELOC_HI16_S
10149 || fixp->fx_r_type == BFD_RELOC_LO16))
10150 return 1;
10151
10152 if (HAVE_NEWABI
10153 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10154 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10155 || fixp->fx_r_type == BFD_RELOC_HI16_S
10156 || fixp->fx_r_type == BFD_RELOC_LO16))
10157 return 1;
10158
10159 return (mips_pic == EMBEDDED_PIC
10160 && (fixp->fx_pcrel
10161 || SWITCH_TABLE (fixp)
10162 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10163 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10164 }
10165
10166 /* Apply a fixup to the object file. */
10167
10168 int
10169 md_apply_fix (fixP, valueP)
10170 fixS *fixP;
10171 valueT *valueP;
10172 {
10173 unsigned char *buf;
10174 long insn;
10175 valueT value;
10176
10177 assert (fixP->fx_size == 4
10178 || fixP->fx_r_type == BFD_RELOC_16
10179 || fixP->fx_r_type == BFD_RELOC_32
10180 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10181 || fixP->fx_r_type == BFD_RELOC_HI16_S
10182 || fixP->fx_r_type == BFD_RELOC_LO16
10183 || fixP->fx_r_type == BFD_RELOC_GPREL16
10184 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10185 || fixP->fx_r_type == BFD_RELOC_GPREL32
10186 || fixP->fx_r_type == BFD_RELOC_64
10187 || fixP->fx_r_type == BFD_RELOC_CTOR
10188 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10189 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10190 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10191 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10192 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10193 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10194 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10195 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
10196
10197 value = *valueP;
10198
10199 /* If we aren't adjusting this fixup to be against the section
10200 symbol, we need to adjust the value. */
10201 #ifdef OBJ_ELF
10202 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10203 {
10204 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16
10205 || ((S_IS_WEAK (fixP->fx_addsy)
10206 || S_IS_EXTERN (fixP->fx_addsy))
10207 && !S_IS_COMMON (fixP->fx_addsy))
10208 || (symbol_used_in_reloc_p (fixP->fx_addsy)
10209 && (((bfd_get_section_flags (stdoutput,
10210 S_GET_SEGMENT (fixP->fx_addsy))
10211 & SEC_LINK_ONCE) != 0)
10212 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10213 ".gnu.linkonce",
10214 sizeof (".gnu.linkonce") - 1))))
10215
10216 {
10217 valueT symval = S_GET_VALUE (fixP->fx_addsy);
10218 value -= symval;
10219 if (value != 0
10220 && ! fixP->fx_pcrel
10221 && fixP->fx_r_type != BFD_RELOC_MIPS_GPREL)
10222 {
10223 /* In this case, the bfd_install_relocation routine will
10224 incorrectly add the symbol value back in. We just want
10225 the addend to appear in the object file. */
10226 value -= symval;
10227
10228 /* Make sure the addend is still non-zero. If it became zero
10229 after the last operation, set it to a spurious value and
10230 subtract the same value from the object file's contents. */
10231 if (value == 0)
10232 {
10233 value = 8;
10234
10235 /* The in-place addends for LO16 relocations are signed;
10236 leave the matching HI16 in-place addends as zero. */
10237 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10238 {
10239 reloc_howto_type *howto;
10240 bfd_vma contents, mask, field;
10241
10242 howto = bfd_reloc_type_lookup (stdoutput,
10243 fixP->fx_r_type);
10244
10245 contents = bfd_get_bits (fixP->fx_frag->fr_literal
10246 + fixP->fx_where,
10247 fixP->fx_size * 8,
10248 target_big_endian);
10249
10250 /* MASK has bits set where the relocation should go.
10251 FIELD is -value, shifted into the appropriate place
10252 for this relocation. */
10253 mask = 1 << (howto->bitsize - 1);
10254 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10255 field = (-value >> howto->rightshift) << howto->bitpos;
10256
10257 bfd_put_bits ((field & mask) | (contents & ~mask),
10258 fixP->fx_frag->fr_literal + fixP->fx_where,
10259 fixP->fx_size * 8,
10260 target_big_endian);
10261 }
10262 }
10263 }
10264 }
10265
10266 /* This code was generated using trial and error and so is
10267 fragile and not trustworthy. If you change it, you should
10268 rerun the elf-rel, elf-rel2, and empic testcases and ensure
10269 they still pass. */
10270 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10271 {
10272 value += fixP->fx_frag->fr_address + fixP->fx_where;
10273
10274 /* BFD's REL handling, for MIPS, is _very_ weird.
10275 This gives the right results, but it can't possibly
10276 be the way things are supposed to work. */
10277 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10278 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10279 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10280 value += fixP->fx_frag->fr_address + fixP->fx_where;
10281 }
10282 }
10283 #endif
10284
10285 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc */
10286
10287 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10288 fixP->fx_done = 1;
10289
10290 switch (fixP->fx_r_type)
10291 {
10292 case BFD_RELOC_MIPS_JMP:
10293 case BFD_RELOC_MIPS_SHIFT5:
10294 case BFD_RELOC_MIPS_SHIFT6:
10295 case BFD_RELOC_MIPS_GOT_DISP:
10296 case BFD_RELOC_MIPS_GOT_PAGE:
10297 case BFD_RELOC_MIPS_GOT_OFST:
10298 case BFD_RELOC_MIPS_SUB:
10299 case BFD_RELOC_MIPS_INSERT_A:
10300 case BFD_RELOC_MIPS_INSERT_B:
10301 case BFD_RELOC_MIPS_DELETE:
10302 case BFD_RELOC_MIPS_HIGHEST:
10303 case BFD_RELOC_MIPS_HIGHER:
10304 case BFD_RELOC_MIPS_SCN_DISP:
10305 case BFD_RELOC_MIPS_REL16:
10306 case BFD_RELOC_MIPS_RELGOT:
10307 case BFD_RELOC_MIPS_JALR:
10308 case BFD_RELOC_HI16:
10309 case BFD_RELOC_HI16_S:
10310 case BFD_RELOC_MIPS_GPREL:
10311 case BFD_RELOC_MIPS_LITERAL:
10312 case BFD_RELOC_MIPS_CALL16:
10313 case BFD_RELOC_MIPS_GOT16:
10314 case BFD_RELOC_MIPS_GPREL32:
10315 case BFD_RELOC_MIPS_GOT_HI16:
10316 case BFD_RELOC_MIPS_GOT_LO16:
10317 case BFD_RELOC_MIPS_CALL_HI16:
10318 case BFD_RELOC_MIPS_CALL_LO16:
10319 case BFD_RELOC_MIPS16_GPREL:
10320 if (fixP->fx_pcrel)
10321 as_bad_where (fixP->fx_file, fixP->fx_line,
10322 _("Invalid PC relative reloc"));
10323 /* Nothing needed to do. The value comes from the reloc entry */
10324 break;
10325
10326 case BFD_RELOC_MIPS16_JMP:
10327 /* We currently always generate a reloc against a symbol, which
10328 means that we don't want an addend even if the symbol is
10329 defined. */
10330 fixP->fx_addnumber = 0;
10331 break;
10332
10333 case BFD_RELOC_PCREL_HI16_S:
10334 /* The addend for this is tricky if it is internal, so we just
10335 do everything here rather than in bfd_install_relocation. */
10336 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10337 && !fixP->fx_done
10338 && value != 0)
10339 break;
10340 if (fixP->fx_addsy
10341 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10342 {
10343 /* For an external symbol adjust by the address to make it
10344 pcrel_offset. We use the address of the RELLO reloc
10345 which follows this one. */
10346 value += (fixP->fx_next->fx_frag->fr_address
10347 + fixP->fx_next->fx_where);
10348 }
10349 value = ((value + 0x8000) >> 16) & 0xffff;
10350 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10351 if (target_big_endian)
10352 buf += 2;
10353 md_number_to_chars (buf, value, 2);
10354 break;
10355
10356 case BFD_RELOC_PCREL_LO16:
10357 /* The addend for this is tricky if it is internal, so we just
10358 do everything here rather than in bfd_install_relocation. */
10359 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10360 && !fixP->fx_done
10361 && value != 0)
10362 break;
10363 if (fixP->fx_addsy
10364 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10365 value += fixP->fx_frag->fr_address + fixP->fx_where;
10366 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10367 if (target_big_endian)
10368 buf += 2;
10369 md_number_to_chars (buf, value, 2);
10370 break;
10371
10372 case BFD_RELOC_64:
10373 /* This is handled like BFD_RELOC_32, but we output a sign
10374 extended value if we are only 32 bits. */
10375 if (fixP->fx_done
10376 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10377 {
10378 if (8 <= sizeof (valueT))
10379 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10380 value, 8);
10381 else
10382 {
10383 long w1, w2;
10384 long hiv;
10385
10386 w1 = w2 = fixP->fx_where;
10387 if (target_big_endian)
10388 w1 += 4;
10389 else
10390 w2 += 4;
10391 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10392 if ((value & 0x80000000) != 0)
10393 hiv = 0xffffffff;
10394 else
10395 hiv = 0;
10396 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10397 }
10398 }
10399 break;
10400
10401 case BFD_RELOC_RVA:
10402 case BFD_RELOC_32:
10403 /* If we are deleting this reloc entry, we must fill in the
10404 value now. This can happen if we have a .word which is not
10405 resolved when it appears but is later defined. We also need
10406 to fill in the value if this is an embedded PIC switch table
10407 entry. */
10408 if (fixP->fx_done
10409 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10410 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10411 value, 4);
10412 break;
10413
10414 case BFD_RELOC_16:
10415 /* If we are deleting this reloc entry, we must fill in the
10416 value now. */
10417 assert (fixP->fx_size == 2);
10418 if (fixP->fx_done)
10419 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10420 value, 2);
10421 break;
10422
10423 case BFD_RELOC_LO16:
10424 /* When handling an embedded PIC switch statement, we can wind
10425 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
10426 if (fixP->fx_done)
10427 {
10428 if (value + 0x8000 > 0xffff)
10429 as_bad_where (fixP->fx_file, fixP->fx_line,
10430 _("relocation overflow"));
10431 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10432 if (target_big_endian)
10433 buf += 2;
10434 md_number_to_chars (buf, value, 2);
10435 }
10436 break;
10437
10438 case BFD_RELOC_16_PCREL_S2:
10439 if ((value & 0x3) != 0)
10440 as_bad_where (fixP->fx_file, fixP->fx_line,
10441 _("Branch to odd address (%lx)"), (long) value);
10442
10443 /* Fall through. */
10444
10445 case BFD_RELOC_16_PCREL:
10446 /*
10447 * We need to save the bits in the instruction since fixup_segment()
10448 * might be deleting the relocation entry (i.e., a branch within
10449 * the current segment).
10450 */
10451 if (!fixP->fx_done && value != 0)
10452 break;
10453 /* If 'value' is zero, the remaining reloc code won't actually
10454 do the store, so it must be done here. This is probably
10455 a bug somewhere. */
10456 if (!fixP->fx_done)
10457 value -= fixP->fx_frag->fr_address + fixP->fx_where;
10458
10459 value = (offsetT) value >> 2;
10460
10461 /* update old instruction data */
10462 buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
10463 if (target_big_endian)
10464 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10465 else
10466 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10467
10468 if (value + 0x8000 <= 0xffff)
10469 insn |= value & 0xffff;
10470 else
10471 {
10472 /* The branch offset is too large. If this is an
10473 unconditional branch, and we are not generating PIC code,
10474 we can convert it to an absolute jump instruction. */
10475 if (mips_pic == NO_PIC
10476 && fixP->fx_done
10477 && fixP->fx_frag->fr_address >= text_section->vma
10478 && (fixP->fx_frag->fr_address
10479 < text_section->vma + text_section->_raw_size)
10480 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
10481 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
10482 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10483 {
10484 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
10485 insn = 0x0c000000; /* jal */
10486 else
10487 insn = 0x08000000; /* j */
10488 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10489 fixP->fx_done = 0;
10490 fixP->fx_addsy = section_symbol (text_section);
10491 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10492 }
10493 else
10494 {
10495 /* FIXME. It would be possible in principle to handle
10496 conditional branches which overflow. They could be
10497 transformed into a branch around a jump. This would
10498 require setting up variant frags for each different
10499 branch type. The native MIPS assembler attempts to
10500 handle these cases, but it appears to do it
10501 incorrectly. */
10502 as_bad_where (fixP->fx_file, fixP->fx_line,
10503 _("Branch out of range"));
10504 }
10505 }
10506
10507 md_number_to_chars ((char *) buf, (valueT) insn, 4);
10508 break;
10509
10510 case BFD_RELOC_VTABLE_INHERIT:
10511 fixP->fx_done = 0;
10512 if (fixP->fx_addsy
10513 && !S_IS_DEFINED (fixP->fx_addsy)
10514 && !S_IS_WEAK (fixP->fx_addsy))
10515 S_SET_WEAK (fixP->fx_addsy);
10516 break;
10517
10518 case BFD_RELOC_VTABLE_ENTRY:
10519 fixP->fx_done = 0;
10520 break;
10521
10522 default:
10523 internalError ();
10524 }
10525
10526 return 1;
10527 }
10528
10529 #if 0
10530 void
10531 printInsn (oc)
10532 unsigned long oc;
10533 {
10534 const struct mips_opcode *p;
10535 int treg, sreg, dreg, shamt;
10536 short imm;
10537 const char *args;
10538 int i;
10539
10540 for (i = 0; i < NUMOPCODES; ++i)
10541 {
10542 p = &mips_opcodes[i];
10543 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
10544 {
10545 printf ("%08lx %s\t", oc, p->name);
10546 treg = (oc >> 16) & 0x1f;
10547 sreg = (oc >> 21) & 0x1f;
10548 dreg = (oc >> 11) & 0x1f;
10549 shamt = (oc >> 6) & 0x1f;
10550 imm = oc;
10551 for (args = p->args;; ++args)
10552 {
10553 switch (*args)
10554 {
10555 case '\0':
10556 printf ("\n");
10557 break;
10558
10559 case ',':
10560 case '(':
10561 case ')':
10562 printf ("%c", *args);
10563 continue;
10564
10565 case 'r':
10566 assert (treg == sreg);
10567 printf ("$%d,$%d", treg, sreg);
10568 continue;
10569
10570 case 'd':
10571 case 'G':
10572 printf ("$%d", dreg);
10573 continue;
10574
10575 case 't':
10576 case 'E':
10577 printf ("$%d", treg);
10578 continue;
10579
10580 case 'k':
10581 printf ("0x%x", treg);
10582 continue;
10583
10584 case 'b':
10585 case 's':
10586 printf ("$%d", sreg);
10587 continue;
10588
10589 case 'a':
10590 printf ("0x%08lx", oc & 0x1ffffff);
10591 continue;
10592
10593 case 'i':
10594 case 'j':
10595 case 'o':
10596 case 'u':
10597 printf ("%d", imm);
10598 continue;
10599
10600 case '<':
10601 case '>':
10602 printf ("$%d", shamt);
10603 continue;
10604
10605 default:
10606 internalError ();
10607 }
10608 break;
10609 }
10610 return;
10611 }
10612 }
10613 printf (_("%08lx UNDEFINED\n"), oc);
10614 }
10615 #endif
10616
10617 static symbolS *
10618 get_symbol ()
10619 {
10620 int c;
10621 char *name;
10622 symbolS *p;
10623
10624 name = input_line_pointer;
10625 c = get_symbol_end ();
10626 p = (symbolS *) symbol_find_or_make (name);
10627 *input_line_pointer = c;
10628 return p;
10629 }
10630
10631 /* Align the current frag to a given power of two. The MIPS assembler
10632 also automatically adjusts any preceding label. */
10633
10634 static void
10635 mips_align (to, fill, label)
10636 int to;
10637 int fill;
10638 symbolS *label;
10639 {
10640 mips_emit_delays (false);
10641 frag_align (to, fill, 0);
10642 record_alignment (now_seg, to);
10643 if (label != NULL)
10644 {
10645 assert (S_GET_SEGMENT (label) == now_seg);
10646 symbol_set_frag (label, frag_now);
10647 S_SET_VALUE (label, (valueT) frag_now_fix ());
10648 }
10649 }
10650
10651 /* Align to a given power of two. .align 0 turns off the automatic
10652 alignment used by the data creating pseudo-ops. */
10653
10654 static void
10655 s_align (x)
10656 int x ATTRIBUTE_UNUSED;
10657 {
10658 register int temp;
10659 register long temp_fill;
10660 long max_alignment = 15;
10661
10662 /*
10663
10664 o Note that the assembler pulls down any immediately preceeding label
10665 to the aligned address.
10666 o It's not documented but auto alignment is reinstated by
10667 a .align pseudo instruction.
10668 o Note also that after auto alignment is turned off the mips assembler
10669 issues an error on attempt to assemble an improperly aligned data item.
10670 We don't.
10671
10672 */
10673
10674 temp = get_absolute_expression ();
10675 if (temp > max_alignment)
10676 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
10677 else if (temp < 0)
10678 {
10679 as_warn (_("Alignment negative: 0 assumed."));
10680 temp = 0;
10681 }
10682 if (*input_line_pointer == ',')
10683 {
10684 input_line_pointer++;
10685 temp_fill = get_absolute_expression ();
10686 }
10687 else
10688 temp_fill = 0;
10689 if (temp)
10690 {
10691 auto_align = 1;
10692 mips_align (temp, (int) temp_fill,
10693 insn_labels != NULL ? insn_labels->label : NULL);
10694 }
10695 else
10696 {
10697 auto_align = 0;
10698 }
10699
10700 demand_empty_rest_of_line ();
10701 }
10702
10703 void
10704 mips_flush_pending_output ()
10705 {
10706 mips_emit_delays (false);
10707 mips_clear_insn_labels ();
10708 }
10709
10710 static void
10711 s_change_sec (sec)
10712 int sec;
10713 {
10714 segT seg;
10715
10716 /* When generating embedded PIC code, we only use the .text, .lit8,
10717 .sdata and .sbss sections. We change the .data and .rdata
10718 pseudo-ops to use .sdata. */
10719 if (mips_pic == EMBEDDED_PIC
10720 && (sec == 'd' || sec == 'r'))
10721 sec = 's';
10722
10723 #ifdef OBJ_ELF
10724 /* The ELF backend needs to know that we are changing sections, so
10725 that .previous works correctly. We could do something like check
10726 for an obj_section_change_hook macro, but that might be confusing
10727 as it would not be appropriate to use it in the section changing
10728 functions in read.c, since obj-elf.c intercepts those. FIXME:
10729 This should be cleaner, somehow. */
10730 obj_elf_section_change_hook ();
10731 #endif
10732
10733 mips_emit_delays (false);
10734 switch (sec)
10735 {
10736 case 't':
10737 s_text (0);
10738 break;
10739 case 'd':
10740 s_data (0);
10741 break;
10742 case 'b':
10743 subseg_set (bss_section, (subsegT) get_absolute_expression ());
10744 demand_empty_rest_of_line ();
10745 break;
10746
10747 case 'r':
10748 if (USE_GLOBAL_POINTER_OPT)
10749 {
10750 seg = subseg_new (RDATA_SECTION_NAME,
10751 (subsegT) get_absolute_expression ());
10752 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
10753 {
10754 bfd_set_section_flags (stdoutput, seg,
10755 (SEC_ALLOC
10756 | SEC_LOAD
10757 | SEC_READONLY
10758 | SEC_RELOC
10759 | SEC_DATA));
10760 if (strcmp (TARGET_OS, "elf") != 0)
10761 record_alignment (seg, 4);
10762 }
10763 demand_empty_rest_of_line ();
10764 }
10765 else
10766 {
10767 as_bad (_("No read only data section in this object file format"));
10768 demand_empty_rest_of_line ();
10769 return;
10770 }
10771 break;
10772
10773 case 's':
10774 if (USE_GLOBAL_POINTER_OPT)
10775 {
10776 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
10777 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
10778 {
10779 bfd_set_section_flags (stdoutput, seg,
10780 SEC_ALLOC | SEC_LOAD | SEC_RELOC
10781 | SEC_DATA);
10782 if (strcmp (TARGET_OS, "elf") != 0)
10783 record_alignment (seg, 4);
10784 }
10785 demand_empty_rest_of_line ();
10786 break;
10787 }
10788 else
10789 {
10790 as_bad (_("Global pointers not supported; recompile -G 0"));
10791 demand_empty_rest_of_line ();
10792 return;
10793 }
10794 }
10795
10796 auto_align = 1;
10797 }
10798
10799 void
10800 mips_enable_auto_align ()
10801 {
10802 auto_align = 1;
10803 }
10804
10805 static void
10806 s_cons (log_size)
10807 int log_size;
10808 {
10809 symbolS *label;
10810
10811 label = insn_labels != NULL ? insn_labels->label : NULL;
10812 mips_emit_delays (false);
10813 if (log_size > 0 && auto_align)
10814 mips_align (log_size, 0, label);
10815 mips_clear_insn_labels ();
10816 cons (1 << log_size);
10817 }
10818
10819 static void
10820 s_float_cons (type)
10821 int type;
10822 {
10823 symbolS *label;
10824
10825 label = insn_labels != NULL ? insn_labels->label : NULL;
10826
10827 mips_emit_delays (false);
10828
10829 if (auto_align)
10830 {
10831 if (type == 'd')
10832 mips_align (3, 0, label);
10833 else
10834 mips_align (2, 0, label);
10835 }
10836
10837 mips_clear_insn_labels ();
10838
10839 float_cons (type);
10840 }
10841
10842 /* Handle .globl. We need to override it because on Irix 5 you are
10843 permitted to say
10844 .globl foo .text
10845 where foo is an undefined symbol, to mean that foo should be
10846 considered to be the address of a function. */
10847
10848 static void
10849 s_mips_globl (x)
10850 int x ATTRIBUTE_UNUSED;
10851 {
10852 char *name;
10853 int c;
10854 symbolS *symbolP;
10855 flagword flag;
10856
10857 name = input_line_pointer;
10858 c = get_symbol_end ();
10859 symbolP = symbol_find_or_make (name);
10860 *input_line_pointer = c;
10861 SKIP_WHITESPACE ();
10862
10863 /* On Irix 5, every global symbol that is not explicitly labelled as
10864 being a function is apparently labelled as being an object. */
10865 flag = BSF_OBJECT;
10866
10867 if (! is_end_of_line[(unsigned char) *input_line_pointer])
10868 {
10869 char *secname;
10870 asection *sec;
10871
10872 secname = input_line_pointer;
10873 c = get_symbol_end ();
10874 sec = bfd_get_section_by_name (stdoutput, secname);
10875 if (sec == NULL)
10876 as_bad (_("%s: no such section"), secname);
10877 *input_line_pointer = c;
10878
10879 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
10880 flag = BSF_FUNCTION;
10881 }
10882
10883 symbol_get_bfdsym (symbolP)->flags |= flag;
10884
10885 S_SET_EXTERNAL (symbolP);
10886 demand_empty_rest_of_line ();
10887 }
10888
10889 static void
10890 s_option (x)
10891 int x ATTRIBUTE_UNUSED;
10892 {
10893 char *opt;
10894 char c;
10895
10896 opt = input_line_pointer;
10897 c = get_symbol_end ();
10898
10899 if (*opt == 'O')
10900 {
10901 /* FIXME: What does this mean? */
10902 }
10903 else if (strncmp (opt, "pic", 3) == 0)
10904 {
10905 int i;
10906
10907 i = atoi (opt + 3);
10908 if (i == 0)
10909 mips_pic = NO_PIC;
10910 else if (i == 2)
10911 mips_pic = SVR4_PIC;
10912 else
10913 as_bad (_(".option pic%d not supported"), i);
10914
10915 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
10916 {
10917 if (g_switch_seen && g_switch_value != 0)
10918 as_warn (_("-G may not be used with SVR4 PIC code"));
10919 g_switch_value = 0;
10920 bfd_set_gp_size (stdoutput, 0);
10921 }
10922 }
10923 else
10924 as_warn (_("Unrecognized option \"%s\""), opt);
10925
10926 *input_line_pointer = c;
10927 demand_empty_rest_of_line ();
10928 }
10929
10930 /* This structure is used to hold a stack of .set values. */
10931
10932 struct mips_option_stack
10933 {
10934 struct mips_option_stack *next;
10935 struct mips_set_options options;
10936 };
10937
10938 static struct mips_option_stack *mips_opts_stack;
10939
10940 /* Handle the .set pseudo-op. */
10941
10942 static void
10943 s_mipsset (x)
10944 int x ATTRIBUTE_UNUSED;
10945 {
10946 char *name = input_line_pointer, ch;
10947
10948 while (!is_end_of_line[(unsigned char) *input_line_pointer])
10949 input_line_pointer++;
10950 ch = *input_line_pointer;
10951 *input_line_pointer = '\0';
10952
10953 if (strcmp (name, "reorder") == 0)
10954 {
10955 if (mips_opts.noreorder && prev_nop_frag != NULL)
10956 {
10957 /* If we still have pending nops, we can discard them. The
10958 usual nop handling will insert any that are still
10959 needed. */
10960 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
10961 * (mips_opts.mips16 ? 2 : 4));
10962 prev_nop_frag = NULL;
10963 }
10964 mips_opts.noreorder = 0;
10965 }
10966 else if (strcmp (name, "noreorder") == 0)
10967 {
10968 mips_emit_delays (true);
10969 mips_opts.noreorder = 1;
10970 mips_any_noreorder = 1;
10971 }
10972 else if (strcmp (name, "at") == 0)
10973 {
10974 mips_opts.noat = 0;
10975 }
10976 else if (strcmp (name, "noat") == 0)
10977 {
10978 mips_opts.noat = 1;
10979 }
10980 else if (strcmp (name, "macro") == 0)
10981 {
10982 mips_opts.warn_about_macros = 0;
10983 }
10984 else if (strcmp (name, "nomacro") == 0)
10985 {
10986 if (mips_opts.noreorder == 0)
10987 as_bad (_("`noreorder' must be set before `nomacro'"));
10988 mips_opts.warn_about_macros = 1;
10989 }
10990 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
10991 {
10992 mips_opts.nomove = 0;
10993 }
10994 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
10995 {
10996 mips_opts.nomove = 1;
10997 }
10998 else if (strcmp (name, "bopt") == 0)
10999 {
11000 mips_opts.nobopt = 0;
11001 }
11002 else if (strcmp (name, "nobopt") == 0)
11003 {
11004 mips_opts.nobopt = 1;
11005 }
11006 else if (strcmp (name, "mips16") == 0
11007 || strcmp (name, "MIPS-16") == 0)
11008 mips_opts.mips16 = 1;
11009 else if (strcmp (name, "nomips16") == 0
11010 || strcmp (name, "noMIPS-16") == 0)
11011 mips_opts.mips16 = 0;
11012 else if (strncmp (name, "mips", 4) == 0)
11013 {
11014 int isa;
11015 static int saved_mips_gp32;
11016 static int saved_mips_fp32;
11017 static enum mips_abi_level saved_mips_abi;
11018 static int is_saved;
11019
11020 /* Permit the user to change the ISA on the fly. Needless to
11021 say, misuse can cause serious problems. */
11022 isa = atoi (name + 4);
11023 switch (isa)
11024 {
11025 case 0:
11026 mips_gp32 = saved_mips_gp32;
11027 mips_fp32 = saved_mips_fp32;
11028 mips_abi = saved_mips_abi;
11029 is_saved = 0;
11030 break;
11031 case 1:
11032 case 2:
11033 case 32:
11034 if (! is_saved)
11035 {
11036 saved_mips_gp32 = mips_gp32;
11037 saved_mips_fp32 = mips_fp32;
11038 saved_mips_abi = mips_abi;
11039 }
11040 mips_gp32 = 1;
11041 mips_fp32 = 1;
11042 is_saved = 1;
11043 break;
11044 case 3:
11045 case 4:
11046 case 5:
11047 case 64:
11048 if (! is_saved)
11049 {
11050 saved_mips_gp32 = mips_gp32;
11051 saved_mips_fp32 = mips_fp32;
11052 saved_mips_abi = mips_abi;
11053 }
11054 mips_gp32 = 0;
11055 mips_fp32 = 0;
11056 mips_abi = NO_ABI;
11057 is_saved = 1;
11058 break;
11059 default:
11060 as_bad (_("unknown ISA level"));
11061 break;
11062 }
11063
11064 switch (isa)
11065 {
11066 case 0: mips_opts.isa = file_mips_isa; break;
11067 case 1: mips_opts.isa = ISA_MIPS1; break;
11068 case 2: mips_opts.isa = ISA_MIPS2; break;
11069 case 3: mips_opts.isa = ISA_MIPS3; break;
11070 case 4: mips_opts.isa = ISA_MIPS4; break;
11071 case 5: mips_opts.isa = ISA_MIPS5; break;
11072 case 32: mips_opts.isa = ISA_MIPS32; break;
11073 case 64: mips_opts.isa = ISA_MIPS64; break;
11074 default: as_bad (_("unknown ISA level")); break;
11075 }
11076 }
11077 else if (strcmp (name, "autoextend") == 0)
11078 mips_opts.noautoextend = 0;
11079 else if (strcmp (name, "noautoextend") == 0)
11080 mips_opts.noautoextend = 1;
11081 else if (strcmp (name, "push") == 0)
11082 {
11083 struct mips_option_stack *s;
11084
11085 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11086 s->next = mips_opts_stack;
11087 s->options = mips_opts;
11088 mips_opts_stack = s;
11089 }
11090 else if (strcmp (name, "pop") == 0)
11091 {
11092 struct mips_option_stack *s;
11093
11094 s = mips_opts_stack;
11095 if (s == NULL)
11096 as_bad (_(".set pop with no .set push"));
11097 else
11098 {
11099 /* If we're changing the reorder mode we need to handle
11100 delay slots correctly. */
11101 if (s->options.noreorder && ! mips_opts.noreorder)
11102 mips_emit_delays (true);
11103 else if (! s->options.noreorder && mips_opts.noreorder)
11104 {
11105 if (prev_nop_frag != NULL)
11106 {
11107 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11108 * (mips_opts.mips16 ? 2 : 4));
11109 prev_nop_frag = NULL;
11110 }
11111 }
11112
11113 mips_opts = s->options;
11114 mips_opts_stack = s->next;
11115 free (s);
11116 }
11117 }
11118 else
11119 {
11120 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11121 }
11122 *input_line_pointer = ch;
11123 demand_empty_rest_of_line ();
11124 }
11125
11126 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11127 .option pic2. It means to generate SVR4 PIC calls. */
11128
11129 static void
11130 s_abicalls (ignore)
11131 int ignore ATTRIBUTE_UNUSED;
11132 {
11133 mips_pic = SVR4_PIC;
11134 if (USE_GLOBAL_POINTER_OPT)
11135 {
11136 if (g_switch_seen && g_switch_value != 0)
11137 as_warn (_("-G may not be used with SVR4 PIC code"));
11138 g_switch_value = 0;
11139 }
11140 bfd_set_gp_size (stdoutput, 0);
11141 demand_empty_rest_of_line ();
11142 }
11143
11144 /* Handle the .cpload pseudo-op. This is used when generating SVR4
11145 PIC code. It sets the $gp register for the function based on the
11146 function address, which is in the register named in the argument.
11147 This uses a relocation against _gp_disp, which is handled specially
11148 by the linker. The result is:
11149 lui $gp,%hi(_gp_disp)
11150 addiu $gp,$gp,%lo(_gp_disp)
11151 addu $gp,$gp,.cpload argument
11152 The .cpload argument is normally $25 == $t9. */
11153
11154 static void
11155 s_cpload (ignore)
11156 int ignore ATTRIBUTE_UNUSED;
11157 {
11158 expressionS ex;
11159 int icnt = 0;
11160
11161 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11162 .cpload is ignored. */
11163 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11164 {
11165 s_ignore (0);
11166 return;
11167 }
11168
11169 /* .cpload should be in a .set noreorder section. */
11170 if (mips_opts.noreorder == 0)
11171 as_warn (_(".cpload not in noreorder section"));
11172
11173 ex.X_op = O_symbol;
11174 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11175 ex.X_op_symbol = NULL;
11176 ex.X_add_number = 0;
11177
11178 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11179 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11180
11181 macro_build_lui ((char *) NULL, &icnt, &ex, GP);
11182 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
11183 (int) BFD_RELOC_LO16);
11184
11185 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11186 GP, GP, tc_get_register (0));
11187
11188 demand_empty_rest_of_line ();
11189 }
11190
11191 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11192 .cpsetup $reg1, offset|$reg2, label
11193
11194 If offset is given, this results in:
11195 sd $gp, offset($sp)
11196 lui $gp, %gp_rel(%neg(%hi(label)))
11197 daddiu $gp, $gp, %gp_rel(%neg(%lo(label)))
11198 addu $gp, $gp, $reg1
11199
11200 If $reg2 is given, this results in:
11201 daddu $reg2, $gp, $0
11202 lui $gp, %gp_rel(%neg(%hi(label)))
11203 daddiu $gp, $gp, %gp_rel(%neg(%lo(label)))
11204 addu $gp, $gp, $reg1
11205 */
11206 static void
11207 s_cpsetup (ignore)
11208 int ignore ATTRIBUTE_UNUSED;
11209 {
11210 expressionS ex_off;
11211 expressionS ex_sym;
11212 int reg1;
11213 int icnt = 0;
11214 char *sym;
11215
11216 /* If we are not generating SVR4 PIC code, .cpload is ignored.
11217 We also need NewABI support. */
11218 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11219 {
11220 s_ignore (0);
11221 return;
11222 }
11223
11224 reg1 = tc_get_register (0);
11225 SKIP_WHITESPACE ();
11226 if (*input_line_pointer != ',')
11227 {
11228 as_bad (_("missing argument separator ',' for .cpsetup"));
11229 return;
11230 }
11231 else
11232 input_line_pointer++;
11233 SKIP_WHITESPACE ();
11234 if (*input_line_pointer == '$')
11235 mips_cpreturn_register = tc_get_register (0);
11236 else
11237 mips_cpreturn_offset = get_absolute_expression ();
11238 SKIP_WHITESPACE ();
11239 if (*input_line_pointer != ',')
11240 {
11241 as_bad (_("missing argument separator ',' for .cpsetup"));
11242 return;
11243 }
11244 else
11245 input_line_pointer++;
11246 SKIP_WHITESPACE ();
11247 sym = input_line_pointer;
11248 while (ISALNUM (*input_line_pointer))
11249 input_line_pointer++;
11250 *input_line_pointer = 0;
11251
11252 ex_sym.X_op = O_symbol;
11253 ex_sym.X_add_symbol = symbol_find_or_make (sym);
11254 ex_sym.X_op_symbol = NULL;
11255 ex_sym.X_add_number = 0;
11256
11257 if (mips_cpreturn_register == -1)
11258 {
11259 ex_off.X_op = O_constant;
11260 ex_off.X_add_symbol = NULL;
11261 ex_off.X_op_symbol = NULL;
11262 ex_off.X_add_number = mips_cpreturn_offset;
11263
11264 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11265 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11266 }
11267 else
11268 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11269 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11270
11271 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11272 (int) BFD_RELOC_GPREL16);
11273 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11274 NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11275 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11276 NULL, 0, 0, BFD_RELOC_HI16_S);
11277 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11278 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11279 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11280 NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11281 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11282 NULL, 0, 0, BFD_RELOC_LO16);
11283 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11284 "d,v,t", mips_gp_register, mips_gp_register, reg1);
11285
11286 demand_empty_rest_of_line ();
11287 }
11288
11289 static void
11290 s_cplocal (ignore)
11291 int ignore ATTRIBUTE_UNUSED;
11292 {
11293 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11294 .cplocal is ignored. */
11295 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11296 {
11297 s_ignore (0);
11298 return;
11299 }
11300
11301 mips_gp_register = tc_get_register (0);
11302 }
11303
11304 /* Handle the .cprestore pseudo-op. This stores $gp into a given
11305 offset from $sp. The offset is remembered, and after making a PIC
11306 call $gp is restored from that location. */
11307
11308 static void
11309 s_cprestore (ignore)
11310 int ignore ATTRIBUTE_UNUSED;
11311 {
11312 expressionS ex;
11313 int icnt = 0;
11314
11315 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11316 .cprestore is ignored. */
11317 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11318 {
11319 s_ignore (0);
11320 return;
11321 }
11322
11323 mips_cprestore_offset = get_absolute_expression ();
11324
11325 ex.X_op = O_constant;
11326 ex.X_add_symbol = NULL;
11327 ex.X_op_symbol = NULL;
11328 ex.X_add_number = mips_cprestore_offset;
11329
11330 macro_build ((char *) NULL, &icnt, &ex,
11331 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11332 "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
11333
11334 demand_empty_rest_of_line ();
11335 }
11336
11337 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11338 was given in the preceeding .gpsetup, it results in:
11339 ld $gp, offset($sp)
11340
11341 If a register $reg2 was given there, it results in:
11342 daddiu $gp, $gp, $reg2
11343 */
11344 static void
11345 s_cpreturn (ignore)
11346 int ignore ATTRIBUTE_UNUSED;
11347 {
11348 expressionS ex;
11349 int icnt = 0;
11350
11351 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11352 We also need NewABI support. */
11353 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11354 {
11355 s_ignore (0);
11356 return;
11357 }
11358
11359 if (mips_cpreturn_register == -1)
11360 {
11361 ex.X_op = O_constant;
11362 ex.X_add_symbol = NULL;
11363 ex.X_op_symbol = NULL;
11364 ex.X_add_number = mips_cpreturn_offset;
11365
11366 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11367 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11368 }
11369 else
11370 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11371 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11372
11373 demand_empty_rest_of_line ();
11374 }
11375
11376 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
11377 code. It sets the offset to use in gp_rel relocations. */
11378
11379 static void
11380 s_gpvalue (ignore)
11381 int ignore ATTRIBUTE_UNUSED;
11382 {
11383 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11384 We also need NewABI support. */
11385 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11386 {
11387 s_ignore (0);
11388 return;
11389 }
11390
11391 mips_cpreturn_offset = get_absolute_expression ();
11392
11393 demand_empty_rest_of_line ();
11394 }
11395
11396 /* Handle the .gpword pseudo-op. This is used when generating PIC
11397 code. It generates a 32 bit GP relative reloc. */
11398
11399 static void
11400 s_gpword (ignore)
11401 int ignore ATTRIBUTE_UNUSED;
11402 {
11403 symbolS *label;
11404 expressionS ex;
11405 char *p;
11406
11407 /* When not generating PIC code, this is treated as .word. */
11408 if (mips_pic != SVR4_PIC)
11409 {
11410 s_cons (2);
11411 return;
11412 }
11413
11414 label = insn_labels != NULL ? insn_labels->label : NULL;
11415 mips_emit_delays (true);
11416 if (auto_align)
11417 mips_align (2, 0, label);
11418 mips_clear_insn_labels ();
11419
11420 expression (&ex);
11421
11422 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11423 {
11424 as_bad (_("Unsupported use of .gpword"));
11425 ignore_rest_of_line ();
11426 }
11427
11428 p = frag_more (4);
11429 md_number_to_chars (p, (valueT) 0, 4);
11430 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
11431 BFD_RELOC_MIPS_GPREL32);
11432
11433 demand_empty_rest_of_line ();
11434 }
11435
11436 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
11437 tables in SVR4 PIC code. */
11438
11439 static void
11440 s_cpadd (ignore)
11441 int ignore ATTRIBUTE_UNUSED;
11442 {
11443 int icnt = 0;
11444 int reg;
11445
11446 /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11447 code. */
11448 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11449 {
11450 s_ignore (0);
11451 return;
11452 }
11453
11454 /* Add $gp to the register named as an argument. */
11455 reg = tc_get_register (0);
11456 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11457 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
11458 "d,v,t", reg, reg, GP);
11459
11460 demand_empty_rest_of_line ();
11461 }
11462
11463 /* Handle the .insn pseudo-op. This marks instruction labels in
11464 mips16 mode. This permits the linker to handle them specially,
11465 such as generating jalx instructions when needed. We also make
11466 them odd for the duration of the assembly, in order to generate the
11467 right sort of code. We will make them even in the adjust_symtab
11468 routine, while leaving them marked. This is convenient for the
11469 debugger and the disassembler. The linker knows to make them odd
11470 again. */
11471
11472 static void
11473 s_insn (ignore)
11474 int ignore ATTRIBUTE_UNUSED;
11475 {
11476 if (mips_opts.mips16)
11477 mips16_mark_labels ();
11478
11479 demand_empty_rest_of_line ();
11480 }
11481
11482 /* Handle a .stabn directive. We need these in order to mark a label
11483 as being a mips16 text label correctly. Sometimes the compiler
11484 will emit a label, followed by a .stabn, and then switch sections.
11485 If the label and .stabn are in mips16 mode, then the label is
11486 really a mips16 text label. */
11487
11488 static void
11489 s_mips_stab (type)
11490 int type;
11491 {
11492 if (type == 'n' && mips_opts.mips16)
11493 mips16_mark_labels ();
11494
11495 s_stab (type);
11496 }
11497
11498 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
11499 */
11500
11501 static void
11502 s_mips_weakext (ignore)
11503 int ignore ATTRIBUTE_UNUSED;
11504 {
11505 char *name;
11506 int c;
11507 symbolS *symbolP;
11508 expressionS exp;
11509
11510 name = input_line_pointer;
11511 c = get_symbol_end ();
11512 symbolP = symbol_find_or_make (name);
11513 S_SET_WEAK (symbolP);
11514 *input_line_pointer = c;
11515
11516 SKIP_WHITESPACE ();
11517
11518 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11519 {
11520 if (S_IS_DEFINED (symbolP))
11521 {
11522 as_bad ("Ignoring attempt to redefine symbol `%s'.",
11523 S_GET_NAME (symbolP));
11524 ignore_rest_of_line ();
11525 return;
11526 }
11527
11528 if (*input_line_pointer == ',')
11529 {
11530 ++input_line_pointer;
11531 SKIP_WHITESPACE ();
11532 }
11533
11534 expression (&exp);
11535 if (exp.X_op != O_symbol)
11536 {
11537 as_bad ("bad .weakext directive");
11538 ignore_rest_of_line();
11539 return;
11540 }
11541 symbol_set_value_expression (symbolP, &exp);
11542 }
11543
11544 demand_empty_rest_of_line ();
11545 }
11546
11547 /* Parse a register string into a number. Called from the ECOFF code
11548 to parse .frame. The argument is non-zero if this is the frame
11549 register, so that we can record it in mips_frame_reg. */
11550
11551 int
11552 tc_get_register (frame)
11553 int frame;
11554 {
11555 int reg;
11556
11557 SKIP_WHITESPACE ();
11558 if (*input_line_pointer++ != '$')
11559 {
11560 as_warn (_("expected `$'"));
11561 reg = 0;
11562 }
11563 else if (ISDIGIT (*input_line_pointer))
11564 {
11565 reg = get_absolute_expression ();
11566 if (reg < 0 || reg >= 32)
11567 {
11568 as_warn (_("Bad register number"));
11569 reg = 0;
11570 }
11571 }
11572 else
11573 {
11574 if (strncmp (input_line_pointer, "fp", 2) == 0)
11575 reg = FP;
11576 else if (strncmp (input_line_pointer, "sp", 2) == 0)
11577 reg = SP;
11578 else if (strncmp (input_line_pointer, "gp", 2) == 0)
11579 reg = GP;
11580 else if (strncmp (input_line_pointer, "at", 2) == 0)
11581 reg = AT;
11582 else
11583 {
11584 as_warn (_("Unrecognized register name"));
11585 reg = 0;
11586 }
11587 input_line_pointer += 2;
11588 }
11589 if (frame)
11590 mips_frame_reg = reg != 0 ? reg : SP;
11591 return reg;
11592 }
11593
11594 valueT
11595 md_section_align (seg, addr)
11596 asection *seg;
11597 valueT addr;
11598 {
11599 int align = bfd_get_section_alignment (stdoutput, seg);
11600
11601 #ifdef OBJ_ELF
11602 /* We don't need to align ELF sections to the full alignment.
11603 However, Irix 5 may prefer that we align them at least to a 16
11604 byte boundary. We don't bother to align the sections if we are
11605 targeted for an embedded system. */
11606 if (strcmp (TARGET_OS, "elf") == 0)
11607 return addr;
11608 if (align > 4)
11609 align = 4;
11610 #endif
11611
11612 return ((addr + (1 << align) - 1) & (-1 << align));
11613 }
11614
11615 /* Utility routine, called from above as well. If called while the
11616 input file is still being read, it's only an approximation. (For
11617 example, a symbol may later become defined which appeared to be
11618 undefined earlier.) */
11619
11620 static int
11621 nopic_need_relax (sym, before_relaxing)
11622 symbolS *sym;
11623 int before_relaxing;
11624 {
11625 if (sym == 0)
11626 return 0;
11627
11628 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
11629 {
11630 const char *symname;
11631 int change;
11632
11633 /* Find out whether this symbol can be referenced off the GP
11634 register. It can be if it is smaller than the -G size or if
11635 it is in the .sdata or .sbss section. Certain symbols can
11636 not be referenced off the GP, although it appears as though
11637 they can. */
11638 symname = S_GET_NAME (sym);
11639 if (symname != (const char *) NULL
11640 && (strcmp (symname, "eprol") == 0
11641 || strcmp (symname, "etext") == 0
11642 || strcmp (symname, "_gp") == 0
11643 || strcmp (symname, "edata") == 0
11644 || strcmp (symname, "_fbss") == 0
11645 || strcmp (symname, "_fdata") == 0
11646 || strcmp (symname, "_ftext") == 0
11647 || strcmp (symname, "end") == 0
11648 || strcmp (symname, "_gp_disp") == 0))
11649 change = 1;
11650 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
11651 && (0
11652 #ifndef NO_ECOFF_DEBUGGING
11653 || (symbol_get_obj (sym)->ecoff_extern_size != 0
11654 && (symbol_get_obj (sym)->ecoff_extern_size
11655 <= g_switch_value))
11656 #endif
11657 /* We must defer this decision until after the whole
11658 file has been read, since there might be a .extern
11659 after the first use of this symbol. */
11660 || (before_relaxing
11661 #ifndef NO_ECOFF_DEBUGGING
11662 && symbol_get_obj (sym)->ecoff_extern_size == 0
11663 #endif
11664 && S_GET_VALUE (sym) == 0)
11665 || (S_GET_VALUE (sym) != 0
11666 && S_GET_VALUE (sym) <= g_switch_value)))
11667 change = 0;
11668 else
11669 {
11670 const char *segname;
11671
11672 segname = segment_name (S_GET_SEGMENT (sym));
11673 assert (strcmp (segname, ".lit8") != 0
11674 && strcmp (segname, ".lit4") != 0);
11675 change = (strcmp (segname, ".sdata") != 0
11676 && strcmp (segname, ".sbss") != 0
11677 && strncmp (segname, ".sdata.", 7) != 0
11678 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
11679 }
11680 return change;
11681 }
11682 else
11683 /* We are not optimizing for the GP register. */
11684 return 1;
11685 }
11686
11687 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
11688 extended opcode. SEC is the section the frag is in. */
11689
11690 static int
11691 mips16_extended_frag (fragp, sec, stretch)
11692 fragS *fragp;
11693 asection *sec;
11694 long stretch;
11695 {
11696 int type;
11697 register const struct mips16_immed_operand *op;
11698 offsetT val;
11699 int mintiny, maxtiny;
11700 segT symsec;
11701 fragS *sym_frag;
11702
11703 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
11704 return 0;
11705 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
11706 return 1;
11707
11708 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
11709 op = mips16_immed_operands;
11710 while (op->type != type)
11711 {
11712 ++op;
11713 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
11714 }
11715
11716 if (op->unsp)
11717 {
11718 if (type == '<' || type == '>' || type == '[' || type == ']')
11719 {
11720 mintiny = 1;
11721 maxtiny = 1 << op->nbits;
11722 }
11723 else
11724 {
11725 mintiny = 0;
11726 maxtiny = (1 << op->nbits) - 1;
11727 }
11728 }
11729 else
11730 {
11731 mintiny = - (1 << (op->nbits - 1));
11732 maxtiny = (1 << (op->nbits - 1)) - 1;
11733 }
11734
11735 sym_frag = symbol_get_frag (fragp->fr_symbol);
11736 val = S_GET_VALUE (fragp->fr_symbol);
11737 symsec = S_GET_SEGMENT (fragp->fr_symbol);
11738
11739 if (op->pcrel)
11740 {
11741 addressT addr;
11742
11743 /* We won't have the section when we are called from
11744 mips_relax_frag. However, we will always have been called
11745 from md_estimate_size_before_relax first. If this is a
11746 branch to a different section, we mark it as such. If SEC is
11747 NULL, and the frag is not marked, then it must be a branch to
11748 the same section. */
11749 if (sec == NULL)
11750 {
11751 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
11752 return 1;
11753 }
11754 else
11755 {
11756 /* Must have been called from md_estimate_size_before_relax. */
11757 if (symsec != sec)
11758 {
11759 fragp->fr_subtype =
11760 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11761
11762 /* FIXME: We should support this, and let the linker
11763 catch branches and loads that are out of range. */
11764 as_bad_where (fragp->fr_file, fragp->fr_line,
11765 _("unsupported PC relative reference to different section"));
11766
11767 return 1;
11768 }
11769 if (fragp != sym_frag && sym_frag->fr_address == 0)
11770 /* Assume non-extended on the first relaxation pass.
11771 The address we have calculated will be bogus if this is
11772 a forward branch to another frag, as the forward frag
11773 will have fr_address == 0. */
11774 return 0;
11775 }
11776
11777 /* In this case, we know for sure that the symbol fragment is in
11778 the same section. If the relax_marker of the symbol fragment
11779 differs from the relax_marker of this fragment, we have not
11780 yet adjusted the symbol fragment fr_address. We want to add
11781 in STRETCH in order to get a better estimate of the address.
11782 This particularly matters because of the shift bits. */
11783 if (stretch != 0
11784 && sym_frag->relax_marker != fragp->relax_marker)
11785 {
11786 fragS *f;
11787
11788 /* Adjust stretch for any alignment frag. Note that if have
11789 been expanding the earlier code, the symbol may be
11790 defined in what appears to be an earlier frag. FIXME:
11791 This doesn't handle the fr_subtype field, which specifies
11792 a maximum number of bytes to skip when doing an
11793 alignment. */
11794 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
11795 {
11796 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
11797 {
11798 if (stretch < 0)
11799 stretch = - ((- stretch)
11800 & ~ ((1 << (int) f->fr_offset) - 1));
11801 else
11802 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
11803 if (stretch == 0)
11804 break;
11805 }
11806 }
11807 if (f != NULL)
11808 val += stretch;
11809 }
11810
11811 addr = fragp->fr_address + fragp->fr_fix;
11812
11813 /* The base address rules are complicated. The base address of
11814 a branch is the following instruction. The base address of a
11815 PC relative load or add is the instruction itself, but if it
11816 is in a delay slot (in which case it can not be extended) use
11817 the address of the instruction whose delay slot it is in. */
11818 if (type == 'p' || type == 'q')
11819 {
11820 addr += 2;
11821
11822 /* If we are currently assuming that this frag should be
11823 extended, then, the current address is two bytes
11824 higher. */
11825 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11826 addr += 2;
11827
11828 /* Ignore the low bit in the target, since it will be set
11829 for a text label. */
11830 if ((val & 1) != 0)
11831 --val;
11832 }
11833 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
11834 addr -= 4;
11835 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
11836 addr -= 2;
11837
11838 val -= addr & ~ ((1 << op->shift) - 1);
11839
11840 /* Branch offsets have an implicit 0 in the lowest bit. */
11841 if (type == 'p' || type == 'q')
11842 val /= 2;
11843
11844 /* If any of the shifted bits are set, we must use an extended
11845 opcode. If the address depends on the size of this
11846 instruction, this can lead to a loop, so we arrange to always
11847 use an extended opcode. We only check this when we are in
11848 the main relaxation loop, when SEC is NULL. */
11849 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
11850 {
11851 fragp->fr_subtype =
11852 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11853 return 1;
11854 }
11855
11856 /* If we are about to mark a frag as extended because the value
11857 is precisely maxtiny + 1, then there is a chance of an
11858 infinite loop as in the following code:
11859 la $4,foo
11860 .skip 1020
11861 .align 2
11862 foo:
11863 In this case when the la is extended, foo is 0x3fc bytes
11864 away, so the la can be shrunk, but then foo is 0x400 away, so
11865 the la must be extended. To avoid this loop, we mark the
11866 frag as extended if it was small, and is about to become
11867 extended with a value of maxtiny + 1. */
11868 if (val == ((maxtiny + 1) << op->shift)
11869 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
11870 && sec == NULL)
11871 {
11872 fragp->fr_subtype =
11873 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11874 return 1;
11875 }
11876 }
11877 else if (symsec != absolute_section && sec != NULL)
11878 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
11879
11880 if ((val & ((1 << op->shift) - 1)) != 0
11881 || val < (mintiny << op->shift)
11882 || val > (maxtiny << op->shift))
11883 return 1;
11884 else
11885 return 0;
11886 }
11887
11888 /* Estimate the size of a frag before relaxing. Unless this is the
11889 mips16, we are not really relaxing here, and the final size is
11890 encoded in the subtype information. For the mips16, we have to
11891 decide whether we are using an extended opcode or not. */
11892
11893 int
11894 md_estimate_size_before_relax (fragp, segtype)
11895 fragS *fragp;
11896 asection *segtype;
11897 {
11898 int change = 0;
11899 boolean linkonce = false;
11900
11901 if (RELAX_MIPS16_P (fragp->fr_subtype))
11902 {
11903 if (mips16_extended_frag (fragp, segtype, 0))
11904 {
11905 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
11906 return 4;
11907 }
11908 else
11909 {
11910 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
11911 return 2;
11912 }
11913 }
11914
11915 if (mips_pic == NO_PIC)
11916 {
11917 change = nopic_need_relax (fragp->fr_symbol, 0);
11918 }
11919 else if (mips_pic == SVR4_PIC)
11920 {
11921 symbolS *sym;
11922 asection *symsec;
11923
11924 sym = fragp->fr_symbol;
11925
11926 /* Handle the case of a symbol equated to another symbol. */
11927 while (symbol_equated_reloc_p (sym))
11928 {
11929 symbolS *n;
11930
11931 /* It's possible to get a loop here in a badly written
11932 program. */
11933 n = symbol_get_value_expression (sym)->X_add_symbol;
11934 if (n == sym)
11935 break;
11936 sym = n;
11937 }
11938
11939 symsec = S_GET_SEGMENT (sym);
11940
11941 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
11942 if (symsec != segtype && ! S_IS_LOCAL (sym))
11943 {
11944 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
11945 != 0)
11946 linkonce = true;
11947
11948 /* The GNU toolchain uses an extension for ELF: a section
11949 beginning with the magic string .gnu.linkonce is a linkonce
11950 section. */
11951 if (strncmp (segment_name (symsec), ".gnu.linkonce",
11952 sizeof ".gnu.linkonce" - 1) == 0)
11953 linkonce = true;
11954 }
11955
11956 /* This must duplicate the test in adjust_reloc_syms. */
11957 change = (symsec != &bfd_und_section
11958 && symsec != &bfd_abs_section
11959 && ! bfd_is_com_section (symsec)
11960 && !linkonce
11961 #ifdef OBJ_ELF
11962 /* A global or weak symbol is treated as external. */
11963 && (OUTPUT_FLAVOR == bfd_target_elf_flavour
11964 && ! (S_IS_EXTERN (sym) || S_IS_WEAK (sym)))
11965 #endif
11966 );
11967 }
11968 else
11969 abort ();
11970
11971 if (change)
11972 {
11973 /* Record the offset to the first reloc in the fr_opcode field.
11974 This lets md_convert_frag and tc_gen_reloc know that the code
11975 must be expanded. */
11976 fragp->fr_opcode = (fragp->fr_literal
11977 + fragp->fr_fix
11978 - RELAX_OLD (fragp->fr_subtype)
11979 + RELAX_RELOC1 (fragp->fr_subtype));
11980 /* FIXME: This really needs as_warn_where. */
11981 if (RELAX_WARN (fragp->fr_subtype))
11982 as_warn (_("AT used after \".set noat\" or macro used after "
11983 "\".set nomacro\""));
11984
11985 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
11986 }
11987
11988 return 0;
11989 }
11990
11991 /* This is called to see whether a reloc against a defined symbol
11992 should be converted into a reloc against a section. Don't adjust
11993 MIPS16 jump relocations, so we don't have to worry about the format
11994 of the offset in the .o file. Don't adjust relocations against
11995 mips16 symbols, so that the linker can find them if it needs to set
11996 up a stub. */
11997
11998 int
11999 mips_fix_adjustable (fixp)
12000 fixS *fixp;
12001 {
12002 #ifdef OBJ_ELF
12003 /* Prevent all adjustments to global symbols. */
12004 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12005 && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
12006 return 0;
12007 #endif
12008 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12009 return 0;
12010 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12011 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12012 return 0;
12013 if (fixp->fx_addsy == NULL)
12014 return 1;
12015 #ifdef OBJ_ELF
12016 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12017 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12018 && fixp->fx_subsy == NULL)
12019 return 0;
12020 #endif
12021 return 1;
12022 }
12023
12024 /* Translate internal representation of relocation info to BFD target
12025 format. */
12026
12027 arelent **
12028 tc_gen_reloc (section, fixp)
12029 asection *section ATTRIBUTE_UNUSED;
12030 fixS *fixp;
12031 {
12032 static arelent *retval[4];
12033 arelent *reloc;
12034 bfd_reloc_code_real_type code;
12035
12036 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12037 retval[1] = NULL;
12038
12039 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12040 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12041 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12042
12043 if (mips_pic == EMBEDDED_PIC
12044 && SWITCH_TABLE (fixp))
12045 {
12046 /* For a switch table entry we use a special reloc. The addend
12047 is actually the difference between the reloc address and the
12048 subtrahend. */
12049 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12050 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12051 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12052 fixp->fx_r_type = BFD_RELOC_GPREL32;
12053 }
12054 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12055 reloc->addend = fixp->fx_addnumber;
12056 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12057 {
12058 /* We use a special addend for an internal RELLO reloc. */
12059 if (symbol_section_p (fixp->fx_addsy))
12060 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12061 else
12062 reloc->addend = fixp->fx_addnumber + reloc->address;
12063 }
12064 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12065 {
12066 assert (fixp->fx_next != NULL
12067 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12068 /* We use a special addend for an internal RELHI reloc. The
12069 reloc is relative to the RELLO; adjust the addend
12070 accordingly. */
12071 if (symbol_section_p (fixp->fx_addsy))
12072 reloc->addend = (fixp->fx_next->fx_frag->fr_address
12073 + fixp->fx_next->fx_where
12074 - S_GET_VALUE (fixp->fx_subsy));
12075 else
12076 reloc->addend = (fixp->fx_addnumber
12077 + fixp->fx_next->fx_frag->fr_address
12078 + fixp->fx_next->fx_where);
12079 }
12080 else
12081 {
12082 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12083 /* A gruesome hack which is a result of the gruesome gas reloc
12084 handling. */
12085 reloc->addend = reloc->address;
12086 else
12087 reloc->addend = -reloc->address;
12088 }
12089
12090 /* If this is a variant frag, we may need to adjust the existing
12091 reloc and generate a new one. */
12092 if (fixp->fx_frag->fr_opcode != NULL
12093 && (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
12094 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12095 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12096 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12097 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12098 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12099 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12100 && ! HAVE_NEWABI)
12101 {
12102 arelent *reloc2;
12103
12104 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12105
12106 /* If this is not the last reloc in this frag, then we have two
12107 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12108 CALL_HI16/CALL_LO16, both of which are being replaced. Let
12109 the second one handle all of them. */
12110 if (fixp->fx_next != NULL
12111 && fixp->fx_frag == fixp->fx_next->fx_frag)
12112 {
12113 assert ((fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
12114 && fixp->fx_next->fx_r_type == BFD_RELOC_MIPS_GPREL)
12115 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12116 && (fixp->fx_next->fx_r_type
12117 == BFD_RELOC_MIPS_GOT_LO16))
12118 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12119 && (fixp->fx_next->fx_r_type
12120 == BFD_RELOC_MIPS_CALL_LO16)));
12121 retval[0] = NULL;
12122 return retval;
12123 }
12124
12125 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12126 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12127 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12128 retval[2] = NULL;
12129 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12130 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12131 reloc2->address = (reloc->address
12132 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12133 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12134 reloc2->addend = fixp->fx_addnumber;
12135 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12136 assert (reloc2->howto != NULL);
12137
12138 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12139 {
12140 arelent *reloc3;
12141
12142 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12143 retval[3] = NULL;
12144 *reloc3 = *reloc2;
12145 reloc3->address += 4;
12146 }
12147
12148 if (mips_pic == NO_PIC)
12149 {
12150 assert (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL);
12151 fixp->fx_r_type = BFD_RELOC_HI16_S;
12152 }
12153 else if (mips_pic == SVR4_PIC)
12154 {
12155 switch (fixp->fx_r_type)
12156 {
12157 default:
12158 abort ();
12159 case BFD_RELOC_MIPS_GOT16:
12160 break;
12161 case BFD_RELOC_MIPS_CALL16:
12162 case BFD_RELOC_MIPS_GOT_LO16:
12163 case BFD_RELOC_MIPS_CALL_LO16:
12164 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12165 break;
12166 }
12167 }
12168 else
12169 abort ();
12170 }
12171
12172 /* Since MIPS ELF uses Rel instead of Rela, encode the vtable entry
12173 to be used in the relocation's section offset. */
12174 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12175 {
12176 reloc->address = reloc->addend;
12177 reloc->addend = 0;
12178 }
12179
12180 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12181 fixup_segment converted a non-PC relative reloc into a PC
12182 relative reloc. In such a case, we need to convert the reloc
12183 code. */
12184 code = fixp->fx_r_type;
12185 if (fixp->fx_pcrel)
12186 {
12187 switch (code)
12188 {
12189 case BFD_RELOC_8:
12190 code = BFD_RELOC_8_PCREL;
12191 break;
12192 case BFD_RELOC_16:
12193 code = BFD_RELOC_16_PCREL;
12194 break;
12195 case BFD_RELOC_32:
12196 code = BFD_RELOC_32_PCREL;
12197 break;
12198 case BFD_RELOC_64:
12199 code = BFD_RELOC_64_PCREL;
12200 break;
12201 case BFD_RELOC_8_PCREL:
12202 case BFD_RELOC_16_PCREL:
12203 case BFD_RELOC_32_PCREL:
12204 case BFD_RELOC_64_PCREL:
12205 case BFD_RELOC_16_PCREL_S2:
12206 case BFD_RELOC_PCREL_HI16_S:
12207 case BFD_RELOC_PCREL_LO16:
12208 break;
12209 default:
12210 as_bad_where (fixp->fx_file, fixp->fx_line,
12211 _("Cannot make %s relocation PC relative"),
12212 bfd_get_reloc_code_name (code));
12213 }
12214 }
12215
12216 /* To support a PC relative reloc when generating embedded PIC code
12217 for ECOFF, we use a Cygnus extension. We check for that here to
12218 make sure that we don't let such a reloc escape normally. */
12219 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12220 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12221 && code == BFD_RELOC_16_PCREL_S2
12222 && mips_pic != EMBEDDED_PIC)
12223 reloc->howto = NULL;
12224 else
12225 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12226
12227 if (reloc->howto == NULL)
12228 {
12229 as_bad_where (fixp->fx_file, fixp->fx_line,
12230 _("Can not represent %s relocation in this object file format"),
12231 bfd_get_reloc_code_name (code));
12232 retval[0] = NULL;
12233 }
12234
12235 return retval;
12236 }
12237
12238 /* Relax a machine dependent frag. This returns the amount by which
12239 the current size of the frag should change. */
12240
12241 int
12242 mips_relax_frag (fragp, stretch)
12243 fragS *fragp;
12244 long stretch;
12245 {
12246 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12247 return 0;
12248
12249 if (mips16_extended_frag (fragp, (asection *) NULL, stretch))
12250 {
12251 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12252 return 0;
12253 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12254 return 2;
12255 }
12256 else
12257 {
12258 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12259 return 0;
12260 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12261 return -2;
12262 }
12263
12264 return 0;
12265 }
12266
12267 /* Convert a machine dependent frag. */
12268
12269 void
12270 md_convert_frag (abfd, asec, fragp)
12271 bfd *abfd ATTRIBUTE_UNUSED;
12272 segT asec;
12273 fragS *fragp;
12274 {
12275 int old, new;
12276 char *fixptr;
12277
12278 if (RELAX_MIPS16_P (fragp->fr_subtype))
12279 {
12280 int type;
12281 register const struct mips16_immed_operand *op;
12282 boolean small, ext;
12283 offsetT val;
12284 bfd_byte *buf;
12285 unsigned long insn;
12286 boolean use_extend;
12287 unsigned short extend;
12288
12289 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12290 op = mips16_immed_operands;
12291 while (op->type != type)
12292 ++op;
12293
12294 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12295 {
12296 small = false;
12297 ext = true;
12298 }
12299 else
12300 {
12301 small = true;
12302 ext = false;
12303 }
12304
12305 resolve_symbol_value (fragp->fr_symbol);
12306 val = S_GET_VALUE (fragp->fr_symbol);
12307 if (op->pcrel)
12308 {
12309 addressT addr;
12310
12311 addr = fragp->fr_address + fragp->fr_fix;
12312
12313 /* The rules for the base address of a PC relative reloc are
12314 complicated; see mips16_extended_frag. */
12315 if (type == 'p' || type == 'q')
12316 {
12317 addr += 2;
12318 if (ext)
12319 addr += 2;
12320 /* Ignore the low bit in the target, since it will be
12321 set for a text label. */
12322 if ((val & 1) != 0)
12323 --val;
12324 }
12325 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12326 addr -= 4;
12327 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12328 addr -= 2;
12329
12330 addr &= ~ (addressT) ((1 << op->shift) - 1);
12331 val -= addr;
12332
12333 /* Make sure the section winds up with the alignment we have
12334 assumed. */
12335 if (op->shift > 0)
12336 record_alignment (asec, op->shift);
12337 }
12338
12339 if (ext
12340 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12341 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12342 as_warn_where (fragp->fr_file, fragp->fr_line,
12343 _("extended instruction in delay slot"));
12344
12345 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12346
12347 if (target_big_endian)
12348 insn = bfd_getb16 (buf);
12349 else
12350 insn = bfd_getl16 (buf);
12351
12352 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12353 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12354 small, ext, &insn, &use_extend, &extend);
12355
12356 if (use_extend)
12357 {
12358 md_number_to_chars (buf, 0xf000 | extend, 2);
12359 fragp->fr_fix += 2;
12360 buf += 2;
12361 }
12362
12363 md_number_to_chars (buf, insn, 2);
12364 fragp->fr_fix += 2;
12365 buf += 2;
12366 }
12367 else
12368 {
12369 if (fragp->fr_opcode == NULL)
12370 return;
12371
12372 old = RELAX_OLD (fragp->fr_subtype);
12373 new = RELAX_NEW (fragp->fr_subtype);
12374 fixptr = fragp->fr_literal + fragp->fr_fix;
12375
12376 if (new > 0)
12377 memcpy (fixptr - old, fixptr, new);
12378
12379 fragp->fr_fix += new - old;
12380 }
12381 }
12382
12383 #ifdef OBJ_ELF
12384
12385 /* This function is called after the relocs have been generated.
12386 We've been storing mips16 text labels as odd. Here we convert them
12387 back to even for the convenience of the debugger. */
12388
12389 void
12390 mips_frob_file_after_relocs ()
12391 {
12392 asymbol **syms;
12393 unsigned int count, i;
12394
12395 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12396 return;
12397
12398 syms = bfd_get_outsymbols (stdoutput);
12399 count = bfd_get_symcount (stdoutput);
12400 for (i = 0; i < count; i++, syms++)
12401 {
12402 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12403 && ((*syms)->value & 1) != 0)
12404 {
12405 (*syms)->value &= ~1;
12406 /* If the symbol has an odd size, it was probably computed
12407 incorrectly, so adjust that as well. */
12408 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12409 ++elf_symbol (*syms)->internal_elf_sym.st_size;
12410 }
12411 }
12412 }
12413
12414 #endif
12415
12416 /* This function is called whenever a label is defined. It is used
12417 when handling branch delays; if a branch has a label, we assume we
12418 can not move it. */
12419
12420 void
12421 mips_define_label (sym)
12422 symbolS *sym;
12423 {
12424 struct insn_label_list *l;
12425
12426 if (free_insn_labels == NULL)
12427 l = (struct insn_label_list *) xmalloc (sizeof *l);
12428 else
12429 {
12430 l = free_insn_labels;
12431 free_insn_labels = l->next;
12432 }
12433
12434 l->label = sym;
12435 l->next = insn_labels;
12436 insn_labels = l;
12437 }
12438 \f
12439 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12440
12441 /* Some special processing for a MIPS ELF file. */
12442
12443 void
12444 mips_elf_final_processing ()
12445 {
12446 /* Write out the register information. */
12447 if (! HAVE_NEWABI)
12448 {
12449 Elf32_RegInfo s;
12450
12451 s.ri_gprmask = mips_gprmask;
12452 s.ri_cprmask[0] = mips_cprmask[0];
12453 s.ri_cprmask[1] = mips_cprmask[1];
12454 s.ri_cprmask[2] = mips_cprmask[2];
12455 s.ri_cprmask[3] = mips_cprmask[3];
12456 /* The gp_value field is set by the MIPS ELF backend. */
12457
12458 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
12459 ((Elf32_External_RegInfo *)
12460 mips_regmask_frag));
12461 }
12462 else
12463 {
12464 Elf64_Internal_RegInfo s;
12465
12466 s.ri_gprmask = mips_gprmask;
12467 s.ri_pad = 0;
12468 s.ri_cprmask[0] = mips_cprmask[0];
12469 s.ri_cprmask[1] = mips_cprmask[1];
12470 s.ri_cprmask[2] = mips_cprmask[2];
12471 s.ri_cprmask[3] = mips_cprmask[3];
12472 /* The gp_value field is set by the MIPS ELF backend. */
12473
12474 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
12475 ((Elf64_External_RegInfo *)
12476 mips_regmask_frag));
12477 }
12478
12479 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
12480 sort of BFD interface for this. */
12481 if (mips_any_noreorder)
12482 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
12483 if (mips_pic != NO_PIC)
12484 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
12485
12486 /* Set the MIPS ELF ABI flags. */
12487 if (mips_abi == NO_ABI)
12488 ;
12489 else if (mips_abi == O32_ABI)
12490 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
12491 else if (mips_abi == O64_ABI)
12492 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
12493 else if (mips_abi == EABI_ABI)
12494 {
12495 if (mips_eabi64)
12496 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
12497 else
12498 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
12499 }
12500 else if (mips_abi == N32_ABI)
12501 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
12502
12503 /* Nothing to do for "64". */
12504
12505 if (mips_32bitmode)
12506 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
12507 }
12508
12509 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
12510 \f
12511 typedef struct proc {
12512 symbolS *isym;
12513 unsigned long reg_mask;
12514 unsigned long reg_offset;
12515 unsigned long fpreg_mask;
12516 unsigned long fpreg_offset;
12517 unsigned long frame_offset;
12518 unsigned long frame_reg;
12519 unsigned long pc_reg;
12520 } procS;
12521
12522 static procS cur_proc;
12523 static procS *cur_proc_ptr;
12524 static int numprocs;
12525
12526 /* Fill in an rs_align_code fragment. */
12527
12528 void
12529 mips_handle_align (fragp)
12530 fragS *fragp;
12531 {
12532 if (fragp->fr_type != rs_align_code)
12533 return;
12534
12535 if (mips_opts.mips16)
12536 {
12537 static const unsigned char be_nop[] = { 0x65, 0x00 };
12538 static const unsigned char le_nop[] = { 0x00, 0x65 };
12539
12540 int bytes;
12541 char *p;
12542
12543 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
12544 p = fragp->fr_literal + fragp->fr_fix;
12545
12546 if (bytes & 1)
12547 {
12548 *p++ = 0;
12549 fragp->fr_fix += 1;
12550 }
12551
12552 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
12553 fragp->fr_var = 2;
12554 }
12555
12556 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
12557 }
12558
12559 static void
12560 md_obj_begin ()
12561 {
12562 }
12563
12564 static void
12565 md_obj_end ()
12566 {
12567 /* check for premature end, nesting errors, etc */
12568 if (cur_proc_ptr)
12569 as_warn (_("missing .end at end of assembly"));
12570 }
12571
12572 static long
12573 get_number ()
12574 {
12575 int negative = 0;
12576 long val = 0;
12577
12578 if (*input_line_pointer == '-')
12579 {
12580 ++input_line_pointer;
12581 negative = 1;
12582 }
12583 if (!ISDIGIT (*input_line_pointer))
12584 as_bad (_("Expected simple number."));
12585 if (input_line_pointer[0] == '0')
12586 {
12587 if (input_line_pointer[1] == 'x')
12588 {
12589 input_line_pointer += 2;
12590 while (ISXDIGIT (*input_line_pointer))
12591 {
12592 val <<= 4;
12593 val |= hex_value (*input_line_pointer++);
12594 }
12595 return negative ? -val : val;
12596 }
12597 else
12598 {
12599 ++input_line_pointer;
12600 while (ISDIGIT (*input_line_pointer))
12601 {
12602 val <<= 3;
12603 val |= *input_line_pointer++ - '0';
12604 }
12605 return negative ? -val : val;
12606 }
12607 }
12608 if (!ISDIGIT (*input_line_pointer))
12609 {
12610 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
12611 *input_line_pointer, *input_line_pointer);
12612 as_warn (_("Invalid number"));
12613 return -1;
12614 }
12615 while (ISDIGIT (*input_line_pointer))
12616 {
12617 val *= 10;
12618 val += *input_line_pointer++ - '0';
12619 }
12620 return negative ? -val : val;
12621 }
12622
12623 /* The .file directive; just like the usual .file directive, but there
12624 is an initial number which is the ECOFF file index. */
12625
12626 static void
12627 s_file (x)
12628 int x ATTRIBUTE_UNUSED;
12629 {
12630 int line;
12631
12632 line = get_number ();
12633 s_app_file (0);
12634 }
12635
12636 /* The .end directive. */
12637
12638 static void
12639 s_mips_end (x)
12640 int x ATTRIBUTE_UNUSED;
12641 {
12642 symbolS *p;
12643 int maybe_text;
12644
12645 if (!is_end_of_line[(unsigned char) *input_line_pointer])
12646 {
12647 p = get_symbol ();
12648 demand_empty_rest_of_line ();
12649 }
12650 else
12651 p = NULL;
12652
12653 #ifdef BFD_ASSEMBLER
12654 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
12655 maybe_text = 1;
12656 else
12657 maybe_text = 0;
12658 #else
12659 if (now_seg != data_section && now_seg != bss_section)
12660 maybe_text = 1;
12661 else
12662 maybe_text = 0;
12663 #endif
12664
12665 if (!maybe_text)
12666 as_warn (_(".end not in text section"));
12667
12668 if (!cur_proc_ptr)
12669 {
12670 as_warn (_(".end directive without a preceding .ent directive."));
12671 demand_empty_rest_of_line ();
12672 return;
12673 }
12674
12675 if (p != NULL)
12676 {
12677 assert (S_GET_NAME (p));
12678 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
12679 as_warn (_(".end symbol does not match .ent symbol."));
12680 }
12681 else
12682 as_warn (_(".end directive missing or unknown symbol"));
12683
12684 #ifdef MIPS_STABS_ELF
12685 {
12686 segT saved_seg = now_seg;
12687 subsegT saved_subseg = now_subseg;
12688 valueT dot;
12689 expressionS exp;
12690 char *fragp;
12691
12692 dot = frag_now_fix ();
12693
12694 #ifdef md_flush_pending_output
12695 md_flush_pending_output ();
12696 #endif
12697
12698 assert (pdr_seg);
12699 subseg_set (pdr_seg, 0);
12700
12701 /* Write the symbol. */
12702 exp.X_op = O_symbol;
12703 exp.X_add_symbol = p;
12704 exp.X_add_number = 0;
12705 emit_expr (&exp, 4);
12706
12707 fragp = frag_more (7 * 4);
12708
12709 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
12710 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
12711 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
12712 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
12713 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
12714 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
12715 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
12716
12717 subseg_set (saved_seg, saved_subseg);
12718 }
12719 #endif
12720
12721 cur_proc_ptr = NULL;
12722 }
12723
12724 /* The .aent and .ent directives. */
12725
12726 static void
12727 s_mips_ent (aent)
12728 int aent;
12729 {
12730 int number = 0;
12731 symbolS *symbolP;
12732 int maybe_text;
12733
12734 symbolP = get_symbol ();
12735 if (*input_line_pointer == ',')
12736 input_line_pointer++;
12737 SKIP_WHITESPACE ();
12738 if (ISDIGIT (*input_line_pointer)
12739 || *input_line_pointer == '-')
12740 number = get_number ();
12741
12742 #ifdef BFD_ASSEMBLER
12743 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
12744 maybe_text = 1;
12745 else
12746 maybe_text = 0;
12747 #else
12748 if (now_seg != data_section && now_seg != bss_section)
12749 maybe_text = 1;
12750 else
12751 maybe_text = 0;
12752 #endif
12753
12754 if (!maybe_text)
12755 as_warn (_(".ent or .aent not in text section."));
12756
12757 if (!aent && cur_proc_ptr)
12758 as_warn (_("missing .end"));
12759
12760 if (!aent)
12761 {
12762 cur_proc_ptr = &cur_proc;
12763 memset (cur_proc_ptr, '\0', sizeof (procS));
12764
12765 cur_proc_ptr->isym = symbolP;
12766
12767 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
12768
12769 numprocs++;
12770 }
12771
12772 demand_empty_rest_of_line ();
12773 }
12774
12775 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
12776 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
12777 s_mips_frame is used so that we can set the PDR information correctly.
12778 We can't use the ecoff routines because they make reference to the ecoff
12779 symbol table (in the mdebug section). */
12780
12781 static void
12782 s_mips_frame (ignore)
12783 int ignore ATTRIBUTE_UNUSED;
12784 {
12785 #ifdef MIPS_STABS_ELF
12786
12787 long val;
12788
12789 if (cur_proc_ptr == (procS *) NULL)
12790 {
12791 as_warn (_(".frame outside of .ent"));
12792 demand_empty_rest_of_line ();
12793 return;
12794 }
12795
12796 cur_proc_ptr->frame_reg = tc_get_register (1);
12797
12798 SKIP_WHITESPACE ();
12799 if (*input_line_pointer++ != ','
12800 || get_absolute_expression_and_terminator (&val) != ',')
12801 {
12802 as_warn (_("Bad .frame directive"));
12803 --input_line_pointer;
12804 demand_empty_rest_of_line ();
12805 return;
12806 }
12807
12808 cur_proc_ptr->frame_offset = val;
12809 cur_proc_ptr->pc_reg = tc_get_register (0);
12810
12811 demand_empty_rest_of_line ();
12812 #else
12813 s_ignore (ignore);
12814 #endif /* MIPS_STABS_ELF */
12815 }
12816
12817 /* The .fmask and .mask directives. If the mdebug section is present
12818 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
12819 embedded targets, s_mips_mask is used so that we can set the PDR
12820 information correctly. We can't use the ecoff routines because they
12821 make reference to the ecoff symbol table (in the mdebug section). */
12822
12823 static void
12824 s_mips_mask (reg_type)
12825 char reg_type;
12826 {
12827 #ifdef MIPS_STABS_ELF
12828 long mask, off;
12829
12830 if (cur_proc_ptr == (procS *) NULL)
12831 {
12832 as_warn (_(".mask/.fmask outside of .ent"));
12833 demand_empty_rest_of_line ();
12834 return;
12835 }
12836
12837 if (get_absolute_expression_and_terminator (&mask) != ',')
12838 {
12839 as_warn (_("Bad .mask/.fmask directive"));
12840 --input_line_pointer;
12841 demand_empty_rest_of_line ();
12842 return;
12843 }
12844
12845 off = get_absolute_expression ();
12846
12847 if (reg_type == 'F')
12848 {
12849 cur_proc_ptr->fpreg_mask = mask;
12850 cur_proc_ptr->fpreg_offset = off;
12851 }
12852 else
12853 {
12854 cur_proc_ptr->reg_mask = mask;
12855 cur_proc_ptr->reg_offset = off;
12856 }
12857
12858 demand_empty_rest_of_line ();
12859 #else
12860 s_ignore (reg_type);
12861 #endif /* MIPS_STABS_ELF */
12862 }
12863
12864 /* The .loc directive. */
12865
12866 #if 0
12867 static void
12868 s_loc (x)
12869 int x;
12870 {
12871 symbolS *symbolP;
12872 int lineno;
12873 int addroff;
12874
12875 assert (now_seg == text_section);
12876
12877 lineno = get_number ();
12878 addroff = frag_now_fix ();
12879
12880 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
12881 S_SET_TYPE (symbolP, N_SLINE);
12882 S_SET_OTHER (symbolP, 0);
12883 S_SET_DESC (symbolP, lineno);
12884 symbolP->sy_segment = now_seg;
12885 }
12886 #endif
12887
12888 /* CPU name/ISA/number mapping table.
12889
12890 Entries are grouped by type. The first matching CPU or ISA entry
12891 gets chosen by CPU or ISA, so it should be the 'canonical' name
12892 for that type. Entries after that within the type are sorted
12893 alphabetically.
12894
12895 Case is ignored in comparison, so put the canonical entry in the
12896 appropriate case but everything else in lower case to ease eye pain. */
12897 static const struct mips_cpu_info mips_cpu_info_table[] =
12898 {
12899 /* MIPS1 ISA */
12900 { "MIPS1", 1, ISA_MIPS1, CPU_R3000, },
12901 { "mips", 1, ISA_MIPS1, CPU_R3000, },
12902
12903 /* MIPS2 ISA */
12904 { "MIPS2", 1, ISA_MIPS2, CPU_R6000, },
12905
12906 /* MIPS3 ISA */
12907 { "MIPS3", 1, ISA_MIPS3, CPU_R4000, },
12908
12909 /* MIPS4 ISA */
12910 { "MIPS4", 1, ISA_MIPS4, CPU_R8000, },
12911
12912 /* MIPS5 ISA */
12913 { "MIPS5", 1, ISA_MIPS5, CPU_MIPS5, },
12914 { "Generic-MIPS5", 0, ISA_MIPS5, CPU_MIPS5, },
12915
12916 /* MIPS32 ISA */
12917 { "MIPS32", 1, ISA_MIPS32, CPU_MIPS32, },
12918 { "mipsisa32", 0, ISA_MIPS32, CPU_MIPS32, },
12919 { "Generic-MIPS32", 0, ISA_MIPS32, CPU_MIPS32, },
12920 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
12921 { "4km", 0, ISA_MIPS32, CPU_MIPS32, },
12922 { "4kp", 0, ISA_MIPS32, CPU_MIPS32, },
12923
12924 /* For historical reasons. */
12925 { "MIPS64", 1, ISA_MIPS3, CPU_R4000, },
12926
12927 /* MIPS64 ISA */
12928 { "mipsisa64", 1, ISA_MIPS64, CPU_MIPS64, },
12929 { "Generic-MIPS64", 0, ISA_MIPS64, CPU_MIPS64, },
12930 { "5kc", 0, ISA_MIPS64, CPU_MIPS64, },
12931 { "20kc", 0, ISA_MIPS64, CPU_MIPS64, },
12932
12933 /* R2000 CPU */
12934 { "R2000", 0, ISA_MIPS1, CPU_R2000, },
12935 { "2000", 0, ISA_MIPS1, CPU_R2000, },
12936 { "2k", 0, ISA_MIPS1, CPU_R2000, },
12937 { "r2k", 0, ISA_MIPS1, CPU_R2000, },
12938
12939 /* R3000 CPU */
12940 { "R3000", 0, ISA_MIPS1, CPU_R3000, },
12941 { "3000", 0, ISA_MIPS1, CPU_R3000, },
12942 { "3k", 0, ISA_MIPS1, CPU_R3000, },
12943 { "r3k", 0, ISA_MIPS1, CPU_R3000, },
12944
12945 /* TX3900 CPU */
12946 { "R3900", 0, ISA_MIPS1, CPU_R3900, },
12947 { "3900", 0, ISA_MIPS1, CPU_R3900, },
12948 { "mipstx39", 0, ISA_MIPS1, CPU_R3900, },
12949
12950 /* R4000 CPU */
12951 { "R4000", 0, ISA_MIPS3, CPU_R4000, },
12952 { "4000", 0, ISA_MIPS3, CPU_R4000, },
12953 { "4k", 0, ISA_MIPS3, CPU_R4000, }, /* beware */
12954 { "r4k", 0, ISA_MIPS3, CPU_R4000, },
12955
12956 /* R4010 CPU */
12957 { "R4010", 0, ISA_MIPS2, CPU_R4010, },
12958 { "4010", 0, ISA_MIPS2, CPU_R4010, },
12959
12960 /* R4400 CPU */
12961 { "R4400", 0, ISA_MIPS3, CPU_R4400, },
12962 { "4400", 0, ISA_MIPS3, CPU_R4400, },
12963
12964 /* R4600 CPU */
12965 { "R4600", 0, ISA_MIPS3, CPU_R4600, },
12966 { "4600", 0, ISA_MIPS3, CPU_R4600, },
12967 { "mips64orion", 0, ISA_MIPS3, CPU_R4600, },
12968 { "orion", 0, ISA_MIPS3, CPU_R4600, },
12969
12970 /* R4650 CPU */
12971 { "R4650", 0, ISA_MIPS3, CPU_R4650, },
12972 { "4650", 0, ISA_MIPS3, CPU_R4650, },
12973
12974 /* R6000 CPU */
12975 { "R6000", 0, ISA_MIPS2, CPU_R6000, },
12976 { "6000", 0, ISA_MIPS2, CPU_R6000, },
12977 { "6k", 0, ISA_MIPS2, CPU_R6000, },
12978 { "r6k", 0, ISA_MIPS2, CPU_R6000, },
12979
12980 /* R8000 CPU */
12981 { "R8000", 0, ISA_MIPS4, CPU_R8000, },
12982 { "8000", 0, ISA_MIPS4, CPU_R8000, },
12983 { "8k", 0, ISA_MIPS4, CPU_R8000, },
12984 { "r8k", 0, ISA_MIPS4, CPU_R8000, },
12985
12986 /* R10000 CPU */
12987 { "R10000", 0, ISA_MIPS4, CPU_R10000, },
12988 { "10000", 0, ISA_MIPS4, CPU_R10000, },
12989 { "10k", 0, ISA_MIPS4, CPU_R10000, },
12990 { "r10k", 0, ISA_MIPS4, CPU_R10000, },
12991
12992 /* R12000 CPU */
12993 { "R12000", 0, ISA_MIPS4, CPU_R12000, },
12994 { "12000", 0, ISA_MIPS4, CPU_R12000, },
12995 { "12k", 0, ISA_MIPS4, CPU_R12000, },
12996 { "r12k", 0, ISA_MIPS4, CPU_R12000, },
12997
12998 /* VR4100 CPU */
12999 { "VR4100", 0, ISA_MIPS3, CPU_VR4100, },
13000 { "4100", 0, ISA_MIPS3, CPU_VR4100, },
13001 { "mips64vr4100", 0, ISA_MIPS3, CPU_VR4100, },
13002 { "r4100", 0, ISA_MIPS3, CPU_VR4100, },
13003
13004 /* VR4111 CPU */
13005 { "VR4111", 0, ISA_MIPS3, CPU_R4111, },
13006 { "4111", 0, ISA_MIPS3, CPU_R4111, },
13007 { "mips64vr4111", 0, ISA_MIPS3, CPU_R4111, },
13008 { "r4111", 0, ISA_MIPS3, CPU_R4111, },
13009
13010 /* VR4300 CPU */
13011 { "VR4300", 0, ISA_MIPS3, CPU_R4300, },
13012 { "4300", 0, ISA_MIPS3, CPU_R4300, },
13013 { "mips64vr4300", 0, ISA_MIPS3, CPU_R4300, },
13014 { "r4300", 0, ISA_MIPS3, CPU_R4300, },
13015
13016 /* VR5000 CPU */
13017 { "VR5000", 0, ISA_MIPS4, CPU_R5000, },
13018 { "5000", 0, ISA_MIPS4, CPU_R5000, },
13019 { "5k", 0, ISA_MIPS4, CPU_R5000, },
13020 { "mips64vr5000", 0, ISA_MIPS4, CPU_R5000, },
13021 { "r5000", 0, ISA_MIPS4, CPU_R5000, },
13022 { "r5200", 0, ISA_MIPS4, CPU_R5000, },
13023 { "rm5200", 0, ISA_MIPS4, CPU_R5000, },
13024 { "r5230", 0, ISA_MIPS4, CPU_R5000, },
13025 { "rm5230", 0, ISA_MIPS4, CPU_R5000, },
13026 { "r5231", 0, ISA_MIPS4, CPU_R5000, },
13027 { "rm5231", 0, ISA_MIPS4, CPU_R5000, },
13028 { "r5261", 0, ISA_MIPS4, CPU_R5000, },
13029 { "rm5261", 0, ISA_MIPS4, CPU_R5000, },
13030 { "r5721", 0, ISA_MIPS4, CPU_R5000, },
13031 { "rm5721", 0, ISA_MIPS4, CPU_R5000, },
13032 { "r5k", 0, ISA_MIPS4, CPU_R5000, },
13033 { "r7000", 0, ISA_MIPS4, CPU_R5000, },
13034
13035 /* Broadcom SB-1 CPU */
13036 { "SB-1", 0, ISA_MIPS64, CPU_SB1, },
13037 { "sb-1250", 0, ISA_MIPS64, CPU_SB1, },
13038 { "sb1", 0, ISA_MIPS64, CPU_SB1, },
13039 { "sb1250", 0, ISA_MIPS64, CPU_SB1, },
13040
13041 /* End marker. */
13042 { NULL, 0, 0, 0, },
13043 };
13044
13045 static const struct mips_cpu_info *
13046 mips_cpu_info_from_name (name)
13047 const char *name;
13048 {
13049 int i;
13050
13051 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13052 if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
13053 return (&mips_cpu_info_table[i]);
13054
13055 return NULL;
13056 }
13057
13058 static const struct mips_cpu_info *
13059 mips_cpu_info_from_isa (isa)
13060 int isa;
13061 {
13062 int i;
13063
13064 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13065 if (mips_cpu_info_table[i].is_isa
13066 && isa == mips_cpu_info_table[i].isa)
13067 return (&mips_cpu_info_table[i]);
13068
13069 return NULL;
13070 }
13071
13072 static const struct mips_cpu_info *
13073 mips_cpu_info_from_cpu (cpu)
13074 int cpu;
13075 {
13076 int i;
13077
13078 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13079 if (!mips_cpu_info_table[i].is_isa
13080 && cpu == mips_cpu_info_table[i].cpu)
13081 return (&mips_cpu_info_table[i]);
13082
13083 return NULL;
13084 }
This page took 0.338736 seconds and 5 git commands to generate.