* config/obj-elf.c (special_sections): Use correct types for init
[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, 2002
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 #include "dwarf2dbg.h"
41
42 #ifdef DEBUG
43 #define DBG(x) printf x
44 #else
45 #define DBG(x)
46 #endif
47
48 #ifdef OBJ_MAYBE_ELF
49 /* Clean up namespace so we can include obj-elf.h too. */
50 static int mips_output_flavor PARAMS ((void));
51 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52 #undef OBJ_PROCESS_STAB
53 #undef OUTPUT_FLAVOR
54 #undef S_GET_ALIGN
55 #undef S_GET_SIZE
56 #undef S_SET_ALIGN
57 #undef S_SET_SIZE
58 #undef obj_frob_file
59 #undef obj_frob_file_after_relocs
60 #undef obj_frob_symbol
61 #undef obj_pop_insert
62 #undef obj_sec_sym_ok_for_reloc
63 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65 #include "obj-elf.h"
66 /* Fix any of them that we actually care about. */
67 #undef OUTPUT_FLAVOR
68 #define OUTPUT_FLAVOR mips_output_flavor()
69 #endif
70
71 #if defined (OBJ_ELF)
72 #include "elf/mips.h"
73 #endif
74
75 #ifndef ECOFF_DEBUGGING
76 #define NO_ECOFF_DEBUGGING
77 #define ECOFF_DEBUGGING 0
78 #endif
79
80 int mips_flag_mdebug = -1;
81
82 #include "ecoff.h"
83
84 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
85 static char *mips_regmask_frag;
86 #endif
87
88 #define ZERO 0
89 #define AT 1
90 #define TREG 24
91 #define PIC_CALL_REG 25
92 #define KT0 26
93 #define KT1 27
94 #define GP 28
95 #define SP 29
96 #define FP 30
97 #define RA 31
98
99 #define ILLEGAL_REG (32)
100
101 /* Allow override of standard little-endian ECOFF format. */
102
103 #ifndef ECOFF_LITTLE_FORMAT
104 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
105 #endif
106
107 extern int target_big_endian;
108
109 /* The name of the readonly data section. */
110 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
111 ? ".data" \
112 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
113 ? ".rdata" \
114 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
115 ? ".rdata" \
116 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117 ? ".rodata" \
118 : (abort (), ""))
119
120 /* The ABI to use. */
121 enum mips_abi_level
122 {
123 NO_ABI = 0,
124 O32_ABI,
125 O64_ABI,
126 N32_ABI,
127 N64_ABI,
128 EABI_ABI
129 };
130
131 /* MIPS ABI we are using for this output file. */
132 static enum mips_abi_level mips_abi = NO_ABI;
133
134 /* This is the set of options which may be modified by the .set
135 pseudo-op. We use a struct so that .set push and .set pop are more
136 reliable. */
137
138 struct mips_set_options
139 {
140 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
141 if it has not been initialized. Changed by `.set mipsN', and the
142 -mipsN command line option, and the default CPU. */
143 int isa;
144 /* Enabled Application Specific Extensions (ASEs). These are set to -1
145 if they have not been initialized. Changed by `.set <asename>', by
146 command line options, and based on the default architecture. */
147 int ase_mips3d;
148 int ase_mdmx;
149 /* Whether we are assembling for the mips16 processor. 0 if we are
150 not, 1 if we are, and -1 if the value has not been initialized.
151 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
152 -nomips16 command line options, and the default CPU. */
153 int mips16;
154 /* Non-zero if we should not reorder instructions. Changed by `.set
155 reorder' and `.set noreorder'. */
156 int noreorder;
157 /* Non-zero if we should not permit the $at ($1) register to be used
158 in instructions. Changed by `.set at' and `.set noat'. */
159 int noat;
160 /* Non-zero if we should warn when a macro instruction expands into
161 more than one machine instruction. Changed by `.set nomacro' and
162 `.set macro'. */
163 int warn_about_macros;
164 /* Non-zero if we should not move instructions. Changed by `.set
165 move', `.set volatile', `.set nomove', and `.set novolatile'. */
166 int nomove;
167 /* Non-zero if we should not optimize branches by moving the target
168 of the branch into the delay slot. Actually, we don't perform
169 this optimization anyhow. Changed by `.set bopt' and `.set
170 nobopt'. */
171 int nobopt;
172 /* Non-zero if we should not autoextend mips16 instructions.
173 Changed by `.set autoextend' and `.set noautoextend'. */
174 int noautoextend;
175 /* Restrict general purpose registers and floating point registers
176 to 32 bit. This is initially determined when -mgp32 or -mfp32
177 is passed but can changed if the assembler code uses .set mipsN. */
178 int gp32;
179 int fp32;
180 };
181
182 /* True if -mgp32 was passed. */
183 static int file_mips_gp32 = -1;
184
185 /* True if -mfp32 was passed. */
186 static int file_mips_fp32 = -1;
187
188 /* This is the struct we use to hold the current set of options. Note
189 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
190 -1 to indicate that they have not been initialized. */
191
192 static struct mips_set_options mips_opts =
193 {
194 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
195 };
196
197 /* These variables are filled in with the masks of registers used.
198 The object format code reads them and puts them in the appropriate
199 place. */
200 unsigned long mips_gprmask;
201 unsigned long mips_cprmask[4];
202
203 /* MIPS ISA we are using for this output file. */
204 static int file_mips_isa = ISA_UNKNOWN;
205
206 /* True if -mips16 was passed or implied by arguments passed on the
207 command line (e.g., by -march). */
208 static int file_ase_mips16;
209
210 /* True if -mips3d was passed or implied by arguments passed on the
211 command line (e.g., by -march). */
212 static int file_ase_mips3d;
213
214 /* True if -mdmx was passed or implied by arguments passed on the
215 command line (e.g., by -march). */
216 static int file_ase_mdmx;
217
218 /* The argument of the -march= flag. The architecture we are assembling. */
219 static int mips_arch = CPU_UNKNOWN;
220 static const char *mips_arch_string;
221 static const struct mips_cpu_info *mips_arch_info;
222
223 /* The argument of the -mtune= flag. The architecture for which we
224 are optimizing. */
225 static int mips_tune = CPU_UNKNOWN;
226 static const char *mips_tune_string;
227 static const struct mips_cpu_info *mips_tune_info;
228
229 /* True when generating 32-bit code for a 64-bit processor. */
230 static int mips_32bitmode = 0;
231
232 /* Some ISA's have delay slots for instructions which read or write
233 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
234 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
235 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
236 delay slot in this ISA. The uses of this macro assume that any
237 ISA that has delay slots for one of these, has them for all. They
238 also assume that ISAs which don't have delays for these insns, don't
239 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
240 #define ISA_HAS_COPROC_DELAYS(ISA) ( \
241 (ISA) == ISA_MIPS1 \
242 || (ISA) == ISA_MIPS2 \
243 || (ISA) == ISA_MIPS3 \
244 )
245
246 /* True if the given ABI requires 32-bit registers. */
247 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
248
249 /* Likewise 64-bit registers. */
250 #define ABI_NEEDS_64BIT_REGS(ABI) \
251 ((ABI) == N32_ABI \
252 || (ABI) == N64_ABI \
253 || (ABI) == O64_ABI)
254
255 /* Return true if ISA supports 64 bit gp register instructions. */
256 #define ISA_HAS_64BIT_REGS(ISA) ( \
257 (ISA) == ISA_MIPS3 \
258 || (ISA) == ISA_MIPS4 \
259 || (ISA) == ISA_MIPS5 \
260 || (ISA) == ISA_MIPS64 \
261 )
262
263 #define HAVE_32BIT_GPRS \
264 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
265
266 #define HAVE_32BIT_FPRS \
267 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
268
269 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
270 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
271
272 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
273
274 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
275
276 /* We can only have 64bit addresses if the object file format
277 supports it. */
278 #define HAVE_32BIT_ADDRESSES \
279 (HAVE_32BIT_GPRS \
280 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
281 || ! HAVE_64BIT_OBJECTS) \
282 && mips_pic != EMBEDDED_PIC))
283
284 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
285
286 /* Return true if the given CPU supports the MIPS16 ASE. */
287 #define CPU_HAS_MIPS16(cpu) \
288 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
289 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
290
291 /* Return true if the given CPU supports the MIPS3D ASE. */
292 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
293 )
294
295 /* Return true if the given CPU supports the MDMX ASE. */
296 #define CPU_HAS_MDMX(cpu) (false \
297 )
298
299 /* True if CPU has a dror instruction. */
300 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
301
302 /* True if CPU has a ror instruction. */
303 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
304
305 /* Whether the processor uses hardware interlocks to protect
306 reads from the HI and LO registers, and thus does not
307 require nops to be inserted. */
308
309 #define hilo_interlocks (mips_arch == CPU_R4010 \
310 || mips_arch == CPU_VR5500 \
311 || mips_arch == CPU_SB1 \
312 )
313
314 /* Whether the processor uses hardware interlocks to protect reads
315 from the GPRs, and thus does not require nops to be inserted. */
316 #define gpr_interlocks \
317 (mips_opts.isa != ISA_MIPS1 \
318 || mips_arch == CPU_VR5400 \
319 || mips_arch == CPU_VR5500 \
320 || mips_arch == CPU_R3900)
321
322 /* As with other "interlocks" this is used by hardware that has FP
323 (co-processor) interlocks. */
324 /* Itbl support may require additional care here. */
325 #define cop_interlocks (mips_arch == CPU_R4300 \
326 || mips_arch == CPU_VR5400 \
327 || mips_arch == CPU_VR5500 \
328 || mips_arch == CPU_SB1 \
329 )
330
331 /* Is this a mfhi or mflo instruction? */
332 #define MF_HILO_INSN(PINFO) \
333 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
334
335 /* MIPS PIC level. */
336
337 enum mips_pic_level mips_pic;
338
339 /* Warn about all NOPS that the assembler generates. */
340 static int warn_nops = 0;
341
342 /* 1 if we should generate 32 bit offsets from the $gp register in
343 SVR4_PIC mode. Currently has no meaning in other modes. */
344 static int mips_big_got = 0;
345
346 /* 1 if trap instructions should used for overflow rather than break
347 instructions. */
348 static int mips_trap = 0;
349
350 /* 1 if double width floating point constants should not be constructed
351 by assembling two single width halves into two single width floating
352 point registers which just happen to alias the double width destination
353 register. On some architectures this aliasing can be disabled by a bit
354 in the status register, and the setting of this bit cannot be determined
355 automatically at assemble time. */
356 static int mips_disable_float_construction;
357
358 /* Non-zero if any .set noreorder directives were used. */
359
360 static int mips_any_noreorder;
361
362 /* Non-zero if nops should be inserted when the register referenced in
363 an mfhi/mflo instruction is read in the next two instructions. */
364 static int mips_7000_hilo_fix;
365
366 /* The size of the small data section. */
367 static unsigned int g_switch_value = 8;
368 /* Whether the -G option was used. */
369 static int g_switch_seen = 0;
370
371 #define N_RMASK 0xc4
372 #define N_VFP 0xd4
373
374 /* If we can determine in advance that GP optimization won't be
375 possible, we can skip the relaxation stuff that tries to produce
376 GP-relative references. This makes delay slot optimization work
377 better.
378
379 This function can only provide a guess, but it seems to work for
380 gcc output. It needs to guess right for gcc, otherwise gcc
381 will put what it thinks is a GP-relative instruction in a branch
382 delay slot.
383
384 I don't know if a fix is needed for the SVR4_PIC mode. I've only
385 fixed it for the non-PIC mode. KR 95/04/07 */
386 static int nopic_need_relax PARAMS ((symbolS *, int));
387
388 /* handle of the OPCODE hash table */
389 static struct hash_control *op_hash = NULL;
390
391 /* The opcode hash table we use for the mips16. */
392 static struct hash_control *mips16_op_hash = NULL;
393
394 /* This array holds the chars that always start a comment. If the
395 pre-processor is disabled, these aren't very useful */
396 const char comment_chars[] = "#";
397
398 /* This array holds the chars that only start a comment at the beginning of
399 a line. If the line seems to have the form '# 123 filename'
400 .line and .file directives will appear in the pre-processed output */
401 /* Note that input_file.c hand checks for '#' at the beginning of the
402 first line of the input file. This is because the compiler outputs
403 #NO_APP at the beginning of its output. */
404 /* Also note that C style comments are always supported. */
405 const char line_comment_chars[] = "#";
406
407 /* This array holds machine specific line separator characters. */
408 const char line_separator_chars[] = ";";
409
410 /* Chars that can be used to separate mant from exp in floating point nums */
411 const char EXP_CHARS[] = "eE";
412
413 /* Chars that mean this number is a floating point constant */
414 /* As in 0f12.456 */
415 /* or 0d1.2345e12 */
416 const char FLT_CHARS[] = "rRsSfFdDxXpP";
417
418 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
419 changed in read.c . Ideally it shouldn't have to know about it at all,
420 but nothing is ideal around here.
421 */
422
423 static char *insn_error;
424
425 static int auto_align = 1;
426
427 /* When outputting SVR4 PIC code, the assembler needs to know the
428 offset in the stack frame from which to restore the $gp register.
429 This is set by the .cprestore pseudo-op, and saved in this
430 variable. */
431 static offsetT mips_cprestore_offset = -1;
432
433 /* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
434 more optimizations, it can use a register value instead of a memory-saved
435 offset and even an other register than $gp as global pointer. */
436 static offsetT mips_cpreturn_offset = -1;
437 static int mips_cpreturn_register = -1;
438 static int mips_gp_register = GP;
439 static int mips_gprel_offset = 0;
440
441 /* Whether mips_cprestore_offset has been set in the current function
442 (or whether it has already been warned about, if not). */
443 static int mips_cprestore_valid = 0;
444
445 /* This is the register which holds the stack frame, as set by the
446 .frame pseudo-op. This is needed to implement .cprestore. */
447 static int mips_frame_reg = SP;
448
449 /* Whether mips_frame_reg has been set in the current function
450 (or whether it has already been warned about, if not). */
451 static int mips_frame_reg_valid = 0;
452
453 /* To output NOP instructions correctly, we need to keep information
454 about the previous two instructions. */
455
456 /* Whether we are optimizing. The default value of 2 means to remove
457 unneeded NOPs and swap branch instructions when possible. A value
458 of 1 means to not swap branches. A value of 0 means to always
459 insert NOPs. */
460 static int mips_optimize = 2;
461
462 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
463 equivalent to seeing no -g option at all. */
464 static int mips_debug = 0;
465
466 /* The previous instruction. */
467 static struct mips_cl_insn prev_insn;
468
469 /* The instruction before prev_insn. */
470 static struct mips_cl_insn prev_prev_insn;
471
472 /* If we don't want information for prev_insn or prev_prev_insn, we
473 point the insn_mo field at this dummy integer. */
474 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
475
476 /* Non-zero if prev_insn is valid. */
477 static int prev_insn_valid;
478
479 /* The frag for the previous instruction. */
480 static struct frag *prev_insn_frag;
481
482 /* The offset into prev_insn_frag for the previous instruction. */
483 static long prev_insn_where;
484
485 /* The reloc type for the previous instruction, if any. */
486 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
487
488 /* The reloc for the previous instruction, if any. */
489 static fixS *prev_insn_fixp[3];
490
491 /* Non-zero if the previous instruction was in a delay slot. */
492 static int prev_insn_is_delay_slot;
493
494 /* Non-zero if the previous instruction was in a .set noreorder. */
495 static int prev_insn_unreordered;
496
497 /* Non-zero if the previous instruction uses an extend opcode (if
498 mips16). */
499 static int prev_insn_extended;
500
501 /* Non-zero if the previous previous instruction was in a .set
502 noreorder. */
503 static int prev_prev_insn_unreordered;
504
505 /* If this is set, it points to a frag holding nop instructions which
506 were inserted before the start of a noreorder section. If those
507 nops turn out to be unnecessary, the size of the frag can be
508 decreased. */
509 static fragS *prev_nop_frag;
510
511 /* The number of nop instructions we created in prev_nop_frag. */
512 static int prev_nop_frag_holds;
513
514 /* The number of nop instructions that we know we need in
515 prev_nop_frag. */
516 static int prev_nop_frag_required;
517
518 /* The number of instructions we've seen since prev_nop_frag. */
519 static int prev_nop_frag_since;
520
521 /* For ECOFF and ELF, relocations against symbols are done in two
522 parts, with a HI relocation and a LO relocation. Each relocation
523 has only 16 bits of space to store an addend. This means that in
524 order for the linker to handle carries correctly, it must be able
525 to locate both the HI and the LO relocation. This means that the
526 relocations must appear in order in the relocation table.
527
528 In order to implement this, we keep track of each unmatched HI
529 relocation. We then sort them so that they immediately precede the
530 corresponding LO relocation. */
531
532 struct mips_hi_fixup
533 {
534 /* Next HI fixup. */
535 struct mips_hi_fixup *next;
536 /* This fixup. */
537 fixS *fixp;
538 /* The section this fixup is in. */
539 segT seg;
540 };
541
542 /* The list of unmatched HI relocs. */
543
544 static struct mips_hi_fixup *mips_hi_fixup_list;
545
546 /* Map normal MIPS register numbers to mips16 register numbers. */
547
548 #define X ILLEGAL_REG
549 static const int mips32_to_16_reg_map[] =
550 {
551 X, X, 2, 3, 4, 5, 6, 7,
552 X, X, X, X, X, X, X, X,
553 0, 1, X, X, X, X, X, X,
554 X, X, X, X, X, X, X, X
555 };
556 #undef X
557
558 /* Map mips16 register numbers to normal MIPS register numbers. */
559
560 static const unsigned int mips16_to_32_reg_map[] =
561 {
562 16, 17, 2, 3, 4, 5, 6, 7
563 };
564
565 static int mips_fix_4122_bugs;
566
567 /* We don't relax branches by default, since this causes us to expand
568 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
569 fail to compute the offset before expanding the macro to the most
570 efficient expansion. */
571
572 static int mips_relax_branch;
573 \f
574 /* Since the MIPS does not have multiple forms of PC relative
575 instructions, we do not have to do relaxing as is done on other
576 platforms. However, we do have to handle GP relative addressing
577 correctly, which turns out to be a similar problem.
578
579 Every macro that refers to a symbol can occur in (at least) two
580 forms, one with GP relative addressing and one without. For
581 example, loading a global variable into a register generally uses
582 a macro instruction like this:
583 lw $4,i
584 If i can be addressed off the GP register (this is true if it is in
585 the .sbss or .sdata section, or if it is known to be smaller than
586 the -G argument) this will generate the following instruction:
587 lw $4,i($gp)
588 This instruction will use a GPREL reloc. If i can not be addressed
589 off the GP register, the following instruction sequence will be used:
590 lui $at,i
591 lw $4,i($at)
592 In this case the first instruction will have a HI16 reloc, and the
593 second reloc will have a LO16 reloc. Both relocs will be against
594 the symbol i.
595
596 The issue here is that we may not know whether i is GP addressable
597 until after we see the instruction that uses it. Therefore, we
598 want to be able to choose the final instruction sequence only at
599 the end of the assembly. This is similar to the way other
600 platforms choose the size of a PC relative instruction only at the
601 end of assembly.
602
603 When generating position independent code we do not use GP
604 addressing in quite the same way, but the issue still arises as
605 external symbols and local symbols must be handled differently.
606
607 We handle these issues by actually generating both possible
608 instruction sequences. The longer one is put in a frag_var with
609 type rs_machine_dependent. We encode what to do with the frag in
610 the subtype field. We encode (1) the number of existing bytes to
611 replace, (2) the number of new bytes to use, (3) the offset from
612 the start of the existing bytes to the first reloc we must generate
613 (that is, the offset is applied from the start of the existing
614 bytes after they are replaced by the new bytes, if any), (4) the
615 offset from the start of the existing bytes to the second reloc,
616 (5) whether a third reloc is needed (the third reloc is always four
617 bytes after the second reloc), and (6) whether to warn if this
618 variant is used (this is sometimes needed if .set nomacro or .set
619 noat is in effect). All these numbers are reasonably small.
620
621 Generating two instruction sequences must be handled carefully to
622 ensure that delay slots are handled correctly. Fortunately, there
623 are a limited number of cases. When the second instruction
624 sequence is generated, append_insn is directed to maintain the
625 existing delay slot information, so it continues to apply to any
626 code after the second instruction sequence. This means that the
627 second instruction sequence must not impose any requirements not
628 required by the first instruction sequence.
629
630 These variant frags are then handled in functions called by the
631 machine independent code. md_estimate_size_before_relax returns
632 the final size of the frag. md_convert_frag sets up the final form
633 of the frag. tc_gen_reloc adjust the first reloc and adds a second
634 one if needed. */
635 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
636 ((relax_substateT) \
637 (((old) << 23) \
638 | ((new) << 16) \
639 | (((reloc1) + 64) << 9) \
640 | (((reloc2) + 64) << 2) \
641 | ((reloc3) ? (1 << 1) : 0) \
642 | ((warn) ? 1 : 0)))
643 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
644 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
645 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
646 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
647 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
648 #define RELAX_WARN(i) ((i) & 1)
649
650 /* Branch without likely bit. If label is out of range, we turn:
651
652 beq reg1, reg2, label
653 delay slot
654
655 into
656
657 bne reg1, reg2, 0f
658 nop
659 j label
660 0: delay slot
661
662 with the following opcode replacements:
663
664 beq <-> bne
665 blez <-> bgtz
666 bltz <-> bgez
667 bc1f <-> bc1t
668
669 bltzal <-> bgezal (with jal label instead of j label)
670
671 Even though keeping the delay slot instruction in the delay slot of
672 the branch would be more efficient, it would be very tricky to do
673 correctly, because we'd have to introduce a variable frag *after*
674 the delay slot instruction, and expand that instead. Let's do it
675 the easy way for now, even if the branch-not-taken case now costs
676 one additional instruction. Out-of-range branches are not supposed
677 to be common, anyway.
678
679 Branch likely. If label is out of range, we turn:
680
681 beql reg1, reg2, label
682 delay slot (annulled if branch not taken)
683
684 into
685
686 beql reg1, reg2, 1f
687 nop
688 beql $0, $0, 2f
689 nop
690 1: j[al] label
691 delay slot (executed only if branch taken)
692 2:
693
694 It would be possible to generate a shorter sequence by losing the
695 likely bit, generating something like:
696
697 bne reg1, reg2, 0f
698 nop
699 j[al] label
700 delay slot (executed only if branch taken)
701 0:
702
703 beql -> bne
704 bnel -> beq
705 blezl -> bgtz
706 bgtzl -> blez
707 bltzl -> bgez
708 bgezl -> bltz
709 bc1fl -> bc1t
710 bc1tl -> bc1f
711
712 bltzall -> bgezal (with jal label instead of j label)
713 bgezall -> bltzal (ditto)
714
715
716 but it's not clear that it would actually improve performance. */
717 #define RELAX_BRANCH_ENCODE(reloc_s2, uncond, likely, link, toofar) \
718 ((relax_substateT) \
719 (0xc0000000 \
720 | ((toofar) ? 1 : 0) \
721 | ((link) ? 2 : 0) \
722 | ((likely) ? 4 : 0) \
723 | ((uncond) ? 8 : 0) \
724 | ((reloc_s2) ? 16 : 0)))
725 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
726 #define RELAX_BRANCH_RELOC_S2(i) (((i) & 16) != 0)
727 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
728 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
729 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
730 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1))
731
732 /* For mips16 code, we use an entirely different form of relaxation.
733 mips16 supports two versions of most instructions which take
734 immediate values: a small one which takes some small value, and a
735 larger one which takes a 16 bit value. Since branches also follow
736 this pattern, relaxing these values is required.
737
738 We can assemble both mips16 and normal MIPS code in a single
739 object. Therefore, we need to support this type of relaxation at
740 the same time that we support the relaxation described above. We
741 use the high bit of the subtype field to distinguish these cases.
742
743 The information we store for this type of relaxation is the
744 argument code found in the opcode file for this relocation, whether
745 the user explicitly requested a small or extended form, and whether
746 the relocation is in a jump or jal delay slot. That tells us the
747 size of the value, and how it should be stored. We also store
748 whether the fragment is considered to be extended or not. We also
749 store whether this is known to be a branch to a different section,
750 whether we have tried to relax this frag yet, and whether we have
751 ever extended a PC relative fragment because of a shift count. */
752 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
753 (0x80000000 \
754 | ((type) & 0xff) \
755 | ((small) ? 0x100 : 0) \
756 | ((ext) ? 0x200 : 0) \
757 | ((dslot) ? 0x400 : 0) \
758 | ((jal_dslot) ? 0x800 : 0))
759 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
760 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
761 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
762 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
763 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
764 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
765 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
766 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
767 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
768 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
769 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
770 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
771
772 /* Is the given value a sign-extended 32-bit value? */
773 #define IS_SEXT_32BIT_NUM(x) \
774 (((x) &~ (offsetT) 0x7fffffff) == 0 \
775 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
776
777 /* Is the given value a sign-extended 16-bit value? */
778 #define IS_SEXT_16BIT_NUM(x) \
779 (((x) &~ (offsetT) 0x7fff) == 0 \
780 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
781
782 \f
783 /* Prototypes for static functions. */
784
785 #ifdef __STDC__
786 #define internalError() \
787 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
788 #else
789 #define internalError() as_fatal (_("MIPS internal Error"));
790 #endif
791
792 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
793
794 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
795 unsigned int reg, enum mips_regclass class));
796 static int reg_needs_delay PARAMS ((unsigned int));
797 static void mips16_mark_labels PARAMS ((void));
798 static void append_insn PARAMS ((char *place,
799 struct mips_cl_insn * ip,
800 expressionS * p,
801 bfd_reloc_code_real_type *r,
802 boolean));
803 static void mips_no_prev_insn PARAMS ((int));
804 static void mips_emit_delays PARAMS ((boolean));
805 #ifdef USE_STDARG
806 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
807 const char *name, const char *fmt,
808 ...));
809 #else
810 static void macro_build ();
811 #endif
812 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
813 const char *, const char *,
814 va_list));
815 static void macro_build_jalr PARAMS ((int, expressionS *));
816 static void macro_build_lui PARAMS ((char *place, int *counter,
817 expressionS * ep, int regnum));
818 static void macro_build_ldst_constoffset PARAMS ((char *place, int *counter,
819 expressionS * ep, const char *op,
820 int valreg, int breg));
821 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
822 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
823 expressionS *));
824 static void load_register PARAMS ((int *, int, expressionS *, int));
825 static void load_address PARAMS ((int *, int, expressionS *, int *));
826 static void move_register PARAMS ((int *, int, int));
827 static void macro PARAMS ((struct mips_cl_insn * ip));
828 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
829 #ifdef LOSING_COMPILER
830 static void macro2 PARAMS ((struct mips_cl_insn * ip));
831 #endif
832 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
833 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
834 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
835 boolean, boolean, unsigned long *,
836 boolean *, unsigned short *));
837 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
838 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
839 static int my_getSmallExpression PARAMS ((expressionS *, char *));
840 static void my_getExpression PARAMS ((expressionS *, char *));
841 #ifdef OBJ_ELF
842 static int support_64bit_objects PARAMS((void));
843 #endif
844 static void mips_set_option_string PARAMS ((const char **, const char *));
845 static symbolS *get_symbol PARAMS ((void));
846 static void mips_align PARAMS ((int to, int fill, symbolS *label));
847 static void s_align PARAMS ((int));
848 static void s_change_sec PARAMS ((int));
849 static void s_change_section PARAMS ((int));
850 static void s_cons PARAMS ((int));
851 static void s_float_cons PARAMS ((int));
852 static void s_mips_globl PARAMS ((int));
853 static void s_option PARAMS ((int));
854 static void s_mipsset PARAMS ((int));
855 static void s_abicalls PARAMS ((int));
856 static void s_cpload PARAMS ((int));
857 static void s_cpsetup PARAMS ((int));
858 static void s_cplocal PARAMS ((int));
859 static void s_cprestore PARAMS ((int));
860 static void s_cpreturn PARAMS ((int));
861 static void s_gpvalue PARAMS ((int));
862 static void s_gpword PARAMS ((int));
863 static void s_gpdword PARAMS ((int));
864 static void s_cpadd PARAMS ((int));
865 static void s_insn PARAMS ((int));
866 static void md_obj_begin PARAMS ((void));
867 static void md_obj_end PARAMS ((void));
868 static long get_number PARAMS ((void));
869 static void s_mips_ent PARAMS ((int));
870 static void s_mips_end PARAMS ((int));
871 static void s_mips_frame PARAMS ((int));
872 static void s_mips_mask PARAMS ((int));
873 static void s_mips_stab PARAMS ((int));
874 static void s_mips_weakext PARAMS ((int));
875 static void s_mips_file PARAMS ((int));
876 static void s_mips_loc PARAMS ((int));
877 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
878 static int relaxed_branch_length (fragS *, asection *, int);
879 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
880 static void show PARAMS ((FILE *, const char *, int *, int *));
881 #ifdef OBJ_ELF
882 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
883 #endif
884
885 /* Return values of my_getSmallExpression(). */
886
887 enum small_ex_type
888 {
889 S_EX_NONE = 0,
890 S_EX_REGISTER,
891
892 /* Direct relocation creation by %percent_op(). */
893 S_EX_HALF,
894 S_EX_HI,
895 S_EX_LO,
896 S_EX_GP_REL,
897 S_EX_GOT,
898 S_EX_CALL16,
899 S_EX_GOT_DISP,
900 S_EX_GOT_PAGE,
901 S_EX_GOT_OFST,
902 S_EX_GOT_HI,
903 S_EX_GOT_LO,
904 S_EX_NEG,
905 S_EX_HIGHER,
906 S_EX_HIGHEST,
907 S_EX_CALL_HI,
908 S_EX_CALL_LO
909 };
910
911 /* Table and functions used to map between CPU/ISA names, and
912 ISA levels, and CPU numbers. */
913
914 struct mips_cpu_info
915 {
916 const char *name; /* CPU or ISA name. */
917 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
918 int isa; /* ISA level. */
919 int cpu; /* CPU number (default CPU if ISA). */
920 };
921
922 static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
923 static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
924 static boolean mips_strict_matching_cpu_name_p PARAMS ((const char *,
925 const char *));
926 static boolean mips_matching_cpu_name_p PARAMS ((const char *, const char *));
927 static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
928 const char *));
929 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
930 \f
931 /* Pseudo-op table.
932
933 The following pseudo-ops from the Kane and Heinrich MIPS book
934 should be defined here, but are currently unsupported: .alias,
935 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
936
937 The following pseudo-ops from the Kane and Heinrich MIPS book are
938 specific to the type of debugging information being generated, and
939 should be defined by the object format: .aent, .begin, .bend,
940 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
941 .vreg.
942
943 The following pseudo-ops from the Kane and Heinrich MIPS book are
944 not MIPS CPU specific, but are also not specific to the object file
945 format. This file is probably the best place to define them, but
946 they are not currently supported: .asm0, .endr, .lab, .repeat,
947 .struct. */
948
949 static const pseudo_typeS mips_pseudo_table[] =
950 {
951 /* MIPS specific pseudo-ops. */
952 {"option", s_option, 0},
953 {"set", s_mipsset, 0},
954 {"rdata", s_change_sec, 'r'},
955 {"sdata", s_change_sec, 's'},
956 {"livereg", s_ignore, 0},
957 {"abicalls", s_abicalls, 0},
958 {"cpload", s_cpload, 0},
959 {"cpsetup", s_cpsetup, 0},
960 {"cplocal", s_cplocal, 0},
961 {"cprestore", s_cprestore, 0},
962 {"cpreturn", s_cpreturn, 0},
963 {"gpvalue", s_gpvalue, 0},
964 {"gpword", s_gpword, 0},
965 {"gpdword", s_gpdword, 0},
966 {"cpadd", s_cpadd, 0},
967 {"insn", s_insn, 0},
968
969 /* Relatively generic pseudo-ops that happen to be used on MIPS
970 chips. */
971 {"asciiz", stringer, 1},
972 {"bss", s_change_sec, 'b'},
973 {"err", s_err, 0},
974 {"half", s_cons, 1},
975 {"dword", s_cons, 3},
976 {"weakext", s_mips_weakext, 0},
977
978 /* These pseudo-ops are defined in read.c, but must be overridden
979 here for one reason or another. */
980 {"align", s_align, 0},
981 {"byte", s_cons, 0},
982 {"data", s_change_sec, 'd'},
983 {"double", s_float_cons, 'd'},
984 {"float", s_float_cons, 'f'},
985 {"globl", s_mips_globl, 0},
986 {"global", s_mips_globl, 0},
987 {"hword", s_cons, 1},
988 {"int", s_cons, 2},
989 {"long", s_cons, 2},
990 {"octa", s_cons, 4},
991 {"quad", s_cons, 3},
992 {"section", s_change_section, 0},
993 {"short", s_cons, 1},
994 {"single", s_float_cons, 'f'},
995 {"stabn", s_mips_stab, 'n'},
996 {"text", s_change_sec, 't'},
997 {"word", s_cons, 2},
998
999 { "extern", ecoff_directive_extern, 0},
1000
1001 { NULL, NULL, 0 },
1002 };
1003
1004 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1005 {
1006 /* These pseudo-ops should be defined by the object file format.
1007 However, a.out doesn't support them, so we have versions here. */
1008 {"aent", s_mips_ent, 1},
1009 {"bgnb", s_ignore, 0},
1010 {"end", s_mips_end, 0},
1011 {"endb", s_ignore, 0},
1012 {"ent", s_mips_ent, 0},
1013 {"file", s_mips_file, 0},
1014 {"fmask", s_mips_mask, 'F'},
1015 {"frame", s_mips_frame, 0},
1016 {"loc", s_mips_loc, 0},
1017 {"mask", s_mips_mask, 'R'},
1018 {"verstamp", s_ignore, 0},
1019 { NULL, NULL, 0 },
1020 };
1021
1022 extern void pop_insert PARAMS ((const pseudo_typeS *));
1023
1024 void
1025 mips_pop_insert ()
1026 {
1027 pop_insert (mips_pseudo_table);
1028 if (! ECOFF_DEBUGGING)
1029 pop_insert (mips_nonecoff_pseudo_table);
1030 }
1031 \f
1032 /* Symbols labelling the current insn. */
1033
1034 struct insn_label_list
1035 {
1036 struct insn_label_list *next;
1037 symbolS *label;
1038 };
1039
1040 static struct insn_label_list *insn_labels;
1041 static struct insn_label_list *free_insn_labels;
1042
1043 static void mips_clear_insn_labels PARAMS ((void));
1044
1045 static inline void
1046 mips_clear_insn_labels ()
1047 {
1048 register struct insn_label_list **pl;
1049
1050 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1051 ;
1052 *pl = insn_labels;
1053 insn_labels = NULL;
1054 }
1055 \f
1056 static char *expr_end;
1057
1058 /* Expressions which appear in instructions. These are set by
1059 mips_ip. */
1060
1061 static expressionS imm_expr;
1062 static expressionS offset_expr;
1063
1064 /* Relocs associated with imm_expr and offset_expr. */
1065
1066 static bfd_reloc_code_real_type imm_reloc[3]
1067 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1068 static bfd_reloc_code_real_type offset_reloc[3]
1069 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1070
1071 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
1072
1073 static boolean imm_unmatched_hi;
1074
1075 /* These are set by mips16_ip if an explicit extension is used. */
1076
1077 static boolean mips16_small, mips16_ext;
1078
1079 #ifdef OBJ_ELF
1080 /* The pdr segment for per procedure frame/regmask info. Not used for
1081 ECOFF debugging. */
1082
1083 static segT pdr_seg;
1084 #endif
1085
1086 /* The default target format to use. */
1087
1088 const char *
1089 mips_target_format ()
1090 {
1091 switch (OUTPUT_FLAVOR)
1092 {
1093 case bfd_target_aout_flavour:
1094 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1095 case bfd_target_ecoff_flavour:
1096 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1097 case bfd_target_coff_flavour:
1098 return "pe-mips";
1099 case bfd_target_elf_flavour:
1100 #ifdef TE_TMIPS
1101 /* This is traditional mips. */
1102 return (target_big_endian
1103 ? (HAVE_64BIT_OBJECTS
1104 ? "elf64-tradbigmips"
1105 : (HAVE_NEWABI
1106 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1107 : (HAVE_64BIT_OBJECTS
1108 ? "elf64-tradlittlemips"
1109 : (HAVE_NEWABI
1110 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1111 #else
1112 return (target_big_endian
1113 ? (HAVE_64BIT_OBJECTS
1114 ? "elf64-bigmips"
1115 : (HAVE_NEWABI
1116 ? "elf32-nbigmips" : "elf32-bigmips"))
1117 : (HAVE_64BIT_OBJECTS
1118 ? "elf64-littlemips"
1119 : (HAVE_NEWABI
1120 ? "elf32-nlittlemips" : "elf32-littlemips")));
1121 #endif
1122 default:
1123 abort ();
1124 return NULL;
1125 }
1126 }
1127
1128 /* This function is called once, at assembler startup time. It should
1129 set up all the tables, etc. that the MD part of the assembler will need. */
1130
1131 void
1132 md_begin ()
1133 {
1134 register const char *retval = NULL;
1135 int i = 0;
1136 int broken = 0;
1137
1138 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1139 as_warn (_("Could not set architecture and machine"));
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 ("$ra", reg_section, RA,
1208 &zero_address_frag));
1209 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1210 &zero_address_frag));
1211 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1212 &zero_address_frag));
1213 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1214 &zero_address_frag));
1215 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1216 &zero_address_frag));
1217 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1218 &zero_address_frag));
1219 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1220 &zero_address_frag));
1221 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1222 &zero_address_frag));
1223 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1224 &zero_address_frag));
1225
1226 /* If we don't add these register names to the symbol table, they
1227 may end up being added as regular symbols by operand(), and then
1228 make it to the object file as undefined in case they're not
1229 regarded as local symbols. They're local in o32, since `$' is a
1230 local symbol prefix, but not in n32 or n64. */
1231 for (i = 0; i < 8; i++)
1232 {
1233 char buf[6];
1234
1235 sprintf (buf, "$fcc%i", i);
1236 symbol_table_insert (symbol_new (buf, reg_section, -1,
1237 &zero_address_frag));
1238 }
1239
1240 mips_no_prev_insn (false);
1241
1242 mips_gprmask = 0;
1243 mips_cprmask[0] = 0;
1244 mips_cprmask[1] = 0;
1245 mips_cprmask[2] = 0;
1246 mips_cprmask[3] = 0;
1247
1248 /* set the default alignment for the text section (2**2) */
1249 record_alignment (text_section, 2);
1250
1251 if (USE_GLOBAL_POINTER_OPT)
1252 bfd_set_gp_size (stdoutput, g_switch_value);
1253
1254 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1255 {
1256 /* On a native system, sections must be aligned to 16 byte
1257 boundaries. When configured for an embedded ELF target, we
1258 don't bother. */
1259 if (strcmp (TARGET_OS, "elf") != 0)
1260 {
1261 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1262 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1263 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1264 }
1265
1266 /* Create a .reginfo section for register masks and a .mdebug
1267 section for debugging information. */
1268 {
1269 segT seg;
1270 subsegT subseg;
1271 flagword flags;
1272 segT sec;
1273
1274 seg = now_seg;
1275 subseg = now_subseg;
1276
1277 /* The ABI says this section should be loaded so that the
1278 running program can access it. However, we don't load it
1279 if we are configured for an embedded target */
1280 flags = SEC_READONLY | SEC_DATA;
1281 if (strcmp (TARGET_OS, "elf") != 0)
1282 flags |= SEC_ALLOC | SEC_LOAD;
1283
1284 if (mips_abi != N64_ABI)
1285 {
1286 sec = subseg_new (".reginfo", (subsegT) 0);
1287
1288 bfd_set_section_flags (stdoutput, sec, flags);
1289 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1290
1291 #ifdef OBJ_ELF
1292 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1293 #endif
1294 }
1295 else
1296 {
1297 /* The 64-bit ABI uses a .MIPS.options section rather than
1298 .reginfo section. */
1299 sec = subseg_new (".MIPS.options", (subsegT) 0);
1300 bfd_set_section_flags (stdoutput, sec, flags);
1301 bfd_set_section_alignment (stdoutput, sec, 3);
1302
1303 #ifdef OBJ_ELF
1304 /* Set up the option header. */
1305 {
1306 Elf_Internal_Options opthdr;
1307 char *f;
1308
1309 opthdr.kind = ODK_REGINFO;
1310 opthdr.size = (sizeof (Elf_External_Options)
1311 + sizeof (Elf64_External_RegInfo));
1312 opthdr.section = 0;
1313 opthdr.info = 0;
1314 f = frag_more (sizeof (Elf_External_Options));
1315 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1316 (Elf_External_Options *) f);
1317
1318 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1319 }
1320 #endif
1321 }
1322
1323 if (ECOFF_DEBUGGING)
1324 {
1325 sec = subseg_new (".mdebug", (subsegT) 0);
1326 (void) bfd_set_section_flags (stdoutput, sec,
1327 SEC_HAS_CONTENTS | SEC_READONLY);
1328 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1329 }
1330 #ifdef OBJ_ELF
1331 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1332 {
1333 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1334 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1335 SEC_READONLY | SEC_RELOC
1336 | SEC_DEBUGGING);
1337 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1338 }
1339 #endif
1340
1341 subseg_set (seg, subseg);
1342 }
1343 }
1344
1345 if (! ECOFF_DEBUGGING)
1346 md_obj_begin ();
1347 }
1348
1349 void
1350 md_mips_end ()
1351 {
1352 if (! ECOFF_DEBUGGING)
1353 md_obj_end ();
1354 }
1355
1356 void
1357 md_assemble (str)
1358 char *str;
1359 {
1360 struct mips_cl_insn insn;
1361 bfd_reloc_code_real_type unused_reloc[3]
1362 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1363
1364 imm_expr.X_op = O_absent;
1365 imm_unmatched_hi = false;
1366 offset_expr.X_op = O_absent;
1367 imm_reloc[0] = BFD_RELOC_UNUSED;
1368 imm_reloc[1] = BFD_RELOC_UNUSED;
1369 imm_reloc[2] = BFD_RELOC_UNUSED;
1370 offset_reloc[0] = BFD_RELOC_UNUSED;
1371 offset_reloc[1] = BFD_RELOC_UNUSED;
1372 offset_reloc[2] = BFD_RELOC_UNUSED;
1373
1374 if (mips_opts.mips16)
1375 mips16_ip (str, &insn);
1376 else
1377 {
1378 mips_ip (str, &insn);
1379 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1380 str, insn.insn_opcode));
1381 }
1382
1383 if (insn_error)
1384 {
1385 as_bad ("%s `%s'", insn_error, str);
1386 return;
1387 }
1388
1389 if (insn.insn_mo->pinfo == INSN_MACRO)
1390 {
1391 if (mips_opts.mips16)
1392 mips16_macro (&insn);
1393 else
1394 macro (&insn);
1395 }
1396 else
1397 {
1398 if (imm_expr.X_op != O_absent)
1399 append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1400 else if (offset_expr.X_op != O_absent)
1401 append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1402 else
1403 append_insn (NULL, &insn, NULL, unused_reloc, false);
1404 }
1405 }
1406
1407 /* See whether instruction IP reads register REG. CLASS is the type
1408 of register. */
1409
1410 static int
1411 insn_uses_reg (ip, reg, class)
1412 struct mips_cl_insn *ip;
1413 unsigned int reg;
1414 enum mips_regclass class;
1415 {
1416 if (class == MIPS16_REG)
1417 {
1418 assert (mips_opts.mips16);
1419 reg = mips16_to_32_reg_map[reg];
1420 class = MIPS_GR_REG;
1421 }
1422
1423 /* Don't report on general register ZERO, since it never changes. */
1424 if (class == MIPS_GR_REG && reg == ZERO)
1425 return 0;
1426
1427 if (class == MIPS_FP_REG)
1428 {
1429 assert (! mips_opts.mips16);
1430 /* If we are called with either $f0 or $f1, we must check $f0.
1431 This is not optimal, because it will introduce an unnecessary
1432 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1433 need to distinguish reading both $f0 and $f1 or just one of
1434 them. Note that we don't have to check the other way,
1435 because there is no instruction that sets both $f0 and $f1
1436 and requires a delay. */
1437 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1438 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1439 == (reg &~ (unsigned) 1)))
1440 return 1;
1441 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1442 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1443 == (reg &~ (unsigned) 1)))
1444 return 1;
1445 }
1446 else if (! mips_opts.mips16)
1447 {
1448 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1449 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1450 return 1;
1451 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1452 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1453 return 1;
1454 }
1455 else
1456 {
1457 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1458 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1459 & MIPS16OP_MASK_RX)]
1460 == reg))
1461 return 1;
1462 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1463 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1464 & MIPS16OP_MASK_RY)]
1465 == reg))
1466 return 1;
1467 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1468 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1469 & MIPS16OP_MASK_MOVE32Z)]
1470 == reg))
1471 return 1;
1472 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1473 return 1;
1474 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1475 return 1;
1476 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1477 return 1;
1478 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1479 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1480 & MIPS16OP_MASK_REGR32) == reg)
1481 return 1;
1482 }
1483
1484 return 0;
1485 }
1486
1487 /* This function returns true if modifying a register requires a
1488 delay. */
1489
1490 static int
1491 reg_needs_delay (reg)
1492 unsigned int reg;
1493 {
1494 unsigned long prev_pinfo;
1495
1496 prev_pinfo = prev_insn.insn_mo->pinfo;
1497 if (! mips_opts.noreorder
1498 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1499 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1500 || (! gpr_interlocks
1501 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1502 {
1503 /* A load from a coprocessor or from memory. All load
1504 delays delay the use of general register rt for one
1505 instruction on the r3000. The r6000 and r4000 use
1506 interlocks. */
1507 /* Itbl support may require additional care here. */
1508 know (prev_pinfo & INSN_WRITE_GPR_T);
1509 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1510 return 1;
1511 }
1512
1513 return 0;
1514 }
1515
1516 /* Mark instruction labels in mips16 mode. This permits the linker to
1517 handle them specially, such as generating jalx instructions when
1518 needed. We also make them odd for the duration of the assembly, in
1519 order to generate the right sort of code. We will make them even
1520 in the adjust_symtab routine, while leaving them marked. This is
1521 convenient for the debugger and the disassembler. The linker knows
1522 to make them odd again. */
1523
1524 static void
1525 mips16_mark_labels ()
1526 {
1527 if (mips_opts.mips16)
1528 {
1529 struct insn_label_list *l;
1530 valueT val;
1531
1532 for (l = insn_labels; l != NULL; l = l->next)
1533 {
1534 #ifdef OBJ_ELF
1535 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1536 S_SET_OTHER (l->label, STO_MIPS16);
1537 #endif
1538 val = S_GET_VALUE (l->label);
1539 if ((val & 1) == 0)
1540 S_SET_VALUE (l->label, val + 1);
1541 }
1542 }
1543 }
1544
1545 /* Output an instruction. PLACE is where to put the instruction; if
1546 it is NULL, this uses frag_more to get room. IP is the instruction
1547 information. ADDRESS_EXPR is an operand of the instruction to be
1548 used with RELOC_TYPE. */
1549
1550 static void
1551 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1552 char *place;
1553 struct mips_cl_insn *ip;
1554 expressionS *address_expr;
1555 bfd_reloc_code_real_type *reloc_type;
1556 boolean unmatched_hi;
1557 {
1558 register unsigned long prev_pinfo, pinfo;
1559 char *f;
1560 fixS *fixp[3];
1561 int nops = 0;
1562
1563 /* Mark instruction labels in mips16 mode. */
1564 mips16_mark_labels ();
1565
1566 prev_pinfo = prev_insn.insn_mo->pinfo;
1567 pinfo = ip->insn_mo->pinfo;
1568
1569 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1570 {
1571 int prev_prev_nop;
1572
1573 /* If the previous insn required any delay slots, see if we need
1574 to insert a NOP or two. There are eight kinds of possible
1575 hazards, of which an instruction can have at most one type.
1576 (1) a load from memory delay
1577 (2) a load from a coprocessor delay
1578 (3) an unconditional branch delay
1579 (4) a conditional branch delay
1580 (5) a move to coprocessor register delay
1581 (6) a load coprocessor register from memory delay
1582 (7) a coprocessor condition code delay
1583 (8) a HI/LO special register delay
1584
1585 There are a lot of optimizations we could do that we don't.
1586 In particular, we do not, in general, reorder instructions.
1587 If you use gcc with optimization, it will reorder
1588 instructions and generally do much more optimization then we
1589 do here; repeating all that work in the assembler would only
1590 benefit hand written assembly code, and does not seem worth
1591 it. */
1592
1593 /* This is how a NOP is emitted. */
1594 #define emit_nop() \
1595 (mips_opts.mips16 \
1596 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1597 : md_number_to_chars (frag_more (4), 0, 4))
1598
1599 /* The previous insn might require a delay slot, depending upon
1600 the contents of the current insn. */
1601 if (! mips_opts.mips16
1602 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1603 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1604 && ! cop_interlocks)
1605 || (! gpr_interlocks
1606 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1607 {
1608 /* A load from a coprocessor or from memory. All load
1609 delays delay the use of general register rt for one
1610 instruction on the r3000. The r6000 and r4000 use
1611 interlocks. */
1612 /* Itbl support may require additional care here. */
1613 know (prev_pinfo & INSN_WRITE_GPR_T);
1614 if (mips_optimize == 0
1615 || insn_uses_reg (ip,
1616 ((prev_insn.insn_opcode >> OP_SH_RT)
1617 & OP_MASK_RT),
1618 MIPS_GR_REG))
1619 ++nops;
1620 }
1621 else if (! mips_opts.mips16
1622 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1623 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1624 && ! cop_interlocks)
1625 || (mips_opts.isa == ISA_MIPS1
1626 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1627 {
1628 /* A generic coprocessor delay. The previous instruction
1629 modified a coprocessor general or control register. If
1630 it modified a control register, we need to avoid any
1631 coprocessor instruction (this is probably not always
1632 required, but it sometimes is). If it modified a general
1633 register, we avoid using that register.
1634
1635 On the r6000 and r4000 loading a coprocessor register
1636 from memory is interlocked, and does not require a delay.
1637
1638 This case is not handled very well. There is no special
1639 knowledge of CP0 handling, and the coprocessors other
1640 than the floating point unit are not distinguished at
1641 all. */
1642 /* Itbl support may require additional care here. FIXME!
1643 Need to modify this to include knowledge about
1644 user specified delays! */
1645 if (prev_pinfo & INSN_WRITE_FPR_T)
1646 {
1647 if (mips_optimize == 0
1648 || insn_uses_reg (ip,
1649 ((prev_insn.insn_opcode >> OP_SH_FT)
1650 & OP_MASK_FT),
1651 MIPS_FP_REG))
1652 ++nops;
1653 }
1654 else if (prev_pinfo & INSN_WRITE_FPR_S)
1655 {
1656 if (mips_optimize == 0
1657 || insn_uses_reg (ip,
1658 ((prev_insn.insn_opcode >> OP_SH_FS)
1659 & OP_MASK_FS),
1660 MIPS_FP_REG))
1661 ++nops;
1662 }
1663 else
1664 {
1665 /* We don't know exactly what the previous instruction
1666 does. If the current instruction uses a coprocessor
1667 register, we must insert a NOP. If previous
1668 instruction may set the condition codes, and the
1669 current instruction uses them, we must insert two
1670 NOPS. */
1671 /* Itbl support may require additional care here. */
1672 if (mips_optimize == 0
1673 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1674 && (pinfo & INSN_READ_COND_CODE)))
1675 nops += 2;
1676 else if (pinfo & INSN_COP)
1677 ++nops;
1678 }
1679 }
1680 else if (! mips_opts.mips16
1681 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1682 && (prev_pinfo & INSN_WRITE_COND_CODE)
1683 && ! cop_interlocks)
1684 {
1685 /* The previous instruction sets the coprocessor condition
1686 codes, but does not require a general coprocessor delay
1687 (this means it is a floating point comparison
1688 instruction). If this instruction uses the condition
1689 codes, we need to insert a single NOP. */
1690 /* Itbl support may require additional care here. */
1691 if (mips_optimize == 0
1692 || (pinfo & INSN_READ_COND_CODE))
1693 ++nops;
1694 }
1695
1696 /* If we're fixing up mfhi/mflo for the r7000 and the
1697 previous insn was an mfhi/mflo and the current insn
1698 reads the register that the mfhi/mflo wrote to, then
1699 insert two nops. */
1700
1701 else if (mips_7000_hilo_fix
1702 && MF_HILO_INSN (prev_pinfo)
1703 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1704 & OP_MASK_RD),
1705 MIPS_GR_REG))
1706 {
1707 nops += 2;
1708 }
1709
1710 /* If we're fixing up mfhi/mflo for the r7000 and the
1711 2nd previous insn was an mfhi/mflo and the current insn
1712 reads the register that the mfhi/mflo wrote to, then
1713 insert one nop. */
1714
1715 else if (mips_7000_hilo_fix
1716 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1717 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1718 & OP_MASK_RD),
1719 MIPS_GR_REG))
1720
1721 {
1722 ++nops;
1723 }
1724
1725 else if (prev_pinfo & INSN_READ_LO)
1726 {
1727 /* The previous instruction reads the LO register; if the
1728 current instruction writes to the LO register, we must
1729 insert two NOPS. Some newer processors have interlocks.
1730 Also the tx39's multiply instructions can be exectuted
1731 immediatly after a read from HI/LO (without the delay),
1732 though the tx39's divide insns still do require the
1733 delay. */
1734 if (! (hilo_interlocks
1735 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1736 && (mips_optimize == 0
1737 || (pinfo & INSN_WRITE_LO)))
1738 nops += 2;
1739 /* Most mips16 branch insns don't have a delay slot.
1740 If a read from LO is immediately followed by a branch
1741 to a write to LO we have a read followed by a write
1742 less than 2 insns away. We assume the target of
1743 a branch might be a write to LO, and insert a nop
1744 between a read and an immediately following branch. */
1745 else if (mips_opts.mips16
1746 && (mips_optimize == 0
1747 || (pinfo & MIPS16_INSN_BRANCH)))
1748 ++nops;
1749 }
1750 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1751 {
1752 /* The previous instruction reads the HI register; if the
1753 current instruction writes to the HI register, we must
1754 insert a NOP. Some newer processors have interlocks.
1755 Also the note tx39's multiply above. */
1756 if (! (hilo_interlocks
1757 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1758 && (mips_optimize == 0
1759 || (pinfo & INSN_WRITE_HI)))
1760 nops += 2;
1761 /* Most mips16 branch insns don't have a delay slot.
1762 If a read from HI is immediately followed by a branch
1763 to a write to HI we have a read followed by a write
1764 less than 2 insns away. We assume the target of
1765 a branch might be a write to HI, and insert a nop
1766 between a read and an immediately following branch. */
1767 else if (mips_opts.mips16
1768 && (mips_optimize == 0
1769 || (pinfo & MIPS16_INSN_BRANCH)))
1770 ++nops;
1771 }
1772
1773 /* If the previous instruction was in a noreorder section, then
1774 we don't want to insert the nop after all. */
1775 /* Itbl support may require additional care here. */
1776 if (prev_insn_unreordered)
1777 nops = 0;
1778
1779 /* There are two cases which require two intervening
1780 instructions: 1) setting the condition codes using a move to
1781 coprocessor instruction which requires a general coprocessor
1782 delay and then reading the condition codes 2) reading the HI
1783 or LO register and then writing to it (except on processors
1784 which have interlocks). If we are not already emitting a NOP
1785 instruction, we must check for these cases compared to the
1786 instruction previous to the previous instruction. */
1787 if ((! mips_opts.mips16
1788 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1789 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1790 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1791 && (pinfo & INSN_READ_COND_CODE)
1792 && ! cop_interlocks)
1793 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1794 && (pinfo & INSN_WRITE_LO)
1795 && ! (hilo_interlocks
1796 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1797 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1798 && (pinfo & INSN_WRITE_HI)
1799 && ! (hilo_interlocks
1800 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1801 prev_prev_nop = 1;
1802 else
1803 prev_prev_nop = 0;
1804
1805 if (prev_prev_insn_unreordered)
1806 prev_prev_nop = 0;
1807
1808 if (prev_prev_nop && nops == 0)
1809 ++nops;
1810
1811 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1812 {
1813 /* We're out of bits in pinfo, so we must resort to string
1814 ops here. Shortcuts are selected based on opcodes being
1815 limited to the VR4122 instruction set. */
1816 int min_nops = 0;
1817 const char *pn = prev_insn.insn_mo->name;
1818 const char *tn = ip->insn_mo->name;
1819 if (strncmp(pn, "macc", 4) == 0
1820 || strncmp(pn, "dmacc", 5) == 0)
1821 {
1822 /* Errata 21 - [D]DIV[U] after [D]MACC */
1823 if (strstr (tn, "div"))
1824 {
1825 min_nops = 1;
1826 }
1827
1828 /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1829 if (pn[0] == 'd' /* dmacc */
1830 && (strncmp(tn, "dmult", 5) == 0
1831 || strncmp(tn, "dmacc", 5) == 0))
1832 {
1833 min_nops = 1;
1834 }
1835
1836 /* Errata 24 - MT{LO,HI} after [D]MACC */
1837 if (strcmp (tn, "mtlo") == 0
1838 || strcmp (tn, "mthi") == 0)
1839 {
1840 min_nops = 1;
1841 }
1842
1843 }
1844 else if (strncmp(pn, "dmult", 5) == 0
1845 && (strncmp(tn, "dmult", 5) == 0
1846 || strncmp(tn, "dmacc", 5) == 0))
1847 {
1848 /* Here is the rest of errata 23. */
1849 min_nops = 1;
1850 }
1851 if (nops < min_nops)
1852 nops = min_nops;
1853 }
1854
1855 /* If we are being given a nop instruction, don't bother with
1856 one of the nops we would otherwise output. This will only
1857 happen when a nop instruction is used with mips_optimize set
1858 to 0. */
1859 if (nops > 0
1860 && ! mips_opts.noreorder
1861 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1862 --nops;
1863
1864 /* Now emit the right number of NOP instructions. */
1865 if (nops > 0 && ! mips_opts.noreorder)
1866 {
1867 fragS *old_frag;
1868 unsigned long old_frag_offset;
1869 int i;
1870 struct insn_label_list *l;
1871
1872 old_frag = frag_now;
1873 old_frag_offset = frag_now_fix ();
1874
1875 for (i = 0; i < nops; i++)
1876 emit_nop ();
1877
1878 if (listing)
1879 {
1880 listing_prev_line ();
1881 /* We may be at the start of a variant frag. In case we
1882 are, make sure there is enough space for the frag
1883 after the frags created by listing_prev_line. The
1884 argument to frag_grow here must be at least as large
1885 as the argument to all other calls to frag_grow in
1886 this file. We don't have to worry about being in the
1887 middle of a variant frag, because the variants insert
1888 all needed nop instructions themselves. */
1889 frag_grow (40);
1890 }
1891
1892 for (l = insn_labels; l != NULL; l = l->next)
1893 {
1894 valueT val;
1895
1896 assert (S_GET_SEGMENT (l->label) == now_seg);
1897 symbol_set_frag (l->label, frag_now);
1898 val = (valueT) frag_now_fix ();
1899 /* mips16 text labels are stored as odd. */
1900 if (mips_opts.mips16)
1901 ++val;
1902 S_SET_VALUE (l->label, val);
1903 }
1904
1905 #ifndef NO_ECOFF_DEBUGGING
1906 if (ECOFF_DEBUGGING)
1907 ecoff_fix_loc (old_frag, old_frag_offset);
1908 #endif
1909 }
1910 else if (prev_nop_frag != NULL)
1911 {
1912 /* We have a frag holding nops we may be able to remove. If
1913 we don't need any nops, we can decrease the size of
1914 prev_nop_frag by the size of one instruction. If we do
1915 need some nops, we count them in prev_nops_required. */
1916 if (prev_nop_frag_since == 0)
1917 {
1918 if (nops == 0)
1919 {
1920 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1921 --prev_nop_frag_holds;
1922 }
1923 else
1924 prev_nop_frag_required += nops;
1925 }
1926 else
1927 {
1928 if (prev_prev_nop == 0)
1929 {
1930 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1931 --prev_nop_frag_holds;
1932 }
1933 else
1934 ++prev_nop_frag_required;
1935 }
1936
1937 if (prev_nop_frag_holds <= prev_nop_frag_required)
1938 prev_nop_frag = NULL;
1939
1940 ++prev_nop_frag_since;
1941
1942 /* Sanity check: by the time we reach the second instruction
1943 after prev_nop_frag, we should have used up all the nops
1944 one way or another. */
1945 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1946 }
1947 }
1948
1949 if (place == NULL
1950 && address_expr
1951 && ((*reloc_type == BFD_RELOC_16_PCREL
1952 && address_expr->X_op != O_constant)
1953 || *reloc_type == BFD_RELOC_16_PCREL_S2)
1954 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
1955 || pinfo & INSN_COND_BRANCH_LIKELY)
1956 && mips_relax_branch
1957 /* Don't try branch relaxation within .set nomacro, or within
1958 .set noat if we use $at for PIC computations. If it turns
1959 out that the branch was out-of-range, we'll get an error. */
1960 && !mips_opts.warn_about_macros
1961 && !(mips_opts.noat && mips_pic != NO_PIC)
1962 && !mips_opts.mips16)
1963 {
1964 f = frag_var (rs_machine_dependent,
1965 relaxed_branch_length
1966 (NULL, NULL,
1967 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
1968 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
1969 RELAX_BRANCH_ENCODE
1970 (*reloc_type == BFD_RELOC_16_PCREL_S2,
1971 pinfo & INSN_UNCOND_BRANCH_DELAY,
1972 pinfo & INSN_COND_BRANCH_LIKELY,
1973 pinfo & INSN_WRITE_GPR_31,
1974 0),
1975 address_expr->X_add_symbol,
1976 address_expr->X_add_number,
1977 0);
1978 *reloc_type = BFD_RELOC_UNUSED;
1979 }
1980 else if (*reloc_type > BFD_RELOC_UNUSED)
1981 {
1982 /* We need to set up a variant frag. */
1983 assert (mips_opts.mips16 && address_expr != NULL);
1984 f = frag_var (rs_machine_dependent, 4, 0,
1985 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1986 mips16_small, mips16_ext,
1987 (prev_pinfo
1988 & INSN_UNCOND_BRANCH_DELAY),
1989 (*prev_insn_reloc_type
1990 == BFD_RELOC_MIPS16_JMP)),
1991 make_expr_symbol (address_expr), 0, NULL);
1992 }
1993 else if (place != NULL)
1994 f = place;
1995 else if (mips_opts.mips16
1996 && ! ip->use_extend
1997 && *reloc_type != BFD_RELOC_MIPS16_JMP)
1998 {
1999 /* Make sure there is enough room to swap this instruction with
2000 a following jump instruction. */
2001 frag_grow (6);
2002 f = frag_more (2);
2003 }
2004 else
2005 {
2006 if (mips_opts.mips16
2007 && mips_opts.noreorder
2008 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2009 as_warn (_("extended instruction in delay slot"));
2010
2011 f = frag_more (4);
2012 }
2013
2014 fixp[0] = fixp[1] = fixp[2] = NULL;
2015 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2016 {
2017 if (address_expr->X_op == O_constant)
2018 {
2019 valueT tmp;
2020
2021 switch (*reloc_type)
2022 {
2023 case BFD_RELOC_32:
2024 ip->insn_opcode |= address_expr->X_add_number;
2025 break;
2026
2027 case BFD_RELOC_MIPS_HIGHEST:
2028 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2029 tmp >>= 16;
2030 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2031 break;
2032
2033 case BFD_RELOC_MIPS_HIGHER:
2034 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2035 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2036 break;
2037
2038 case BFD_RELOC_HI16_S:
2039 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2040 >> 16) & 0xffff;
2041 break;
2042
2043 case BFD_RELOC_HI16:
2044 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2045 break;
2046
2047 case BFD_RELOC_LO16:
2048 case BFD_RELOC_MIPS_GOT_DISP:
2049 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2050 break;
2051
2052 case BFD_RELOC_MIPS_JMP:
2053 if ((address_expr->X_add_number & 3) != 0)
2054 as_bad (_("jump to misaligned address (0x%lx)"),
2055 (unsigned long) address_expr->X_add_number);
2056 if (address_expr->X_add_number & ~0xfffffff)
2057 as_bad (_("jump address range overflow (0x%lx)"),
2058 (unsigned long) address_expr->X_add_number);
2059 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2060 break;
2061
2062 case BFD_RELOC_MIPS16_JMP:
2063 if ((address_expr->X_add_number & 3) != 0)
2064 as_bad (_("jump to misaligned address (0x%lx)"),
2065 (unsigned long) address_expr->X_add_number);
2066 if (address_expr->X_add_number & ~0xfffffff)
2067 as_bad (_("jump address range overflow (0x%lx)"),
2068 (unsigned long) address_expr->X_add_number);
2069 ip->insn_opcode |=
2070 (((address_expr->X_add_number & 0x7c0000) << 3)
2071 | ((address_expr->X_add_number & 0xf800000) >> 7)
2072 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2073 break;
2074
2075 case BFD_RELOC_16_PCREL:
2076 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2077 break;
2078
2079 case BFD_RELOC_16_PCREL_S2:
2080 goto need_reloc;
2081
2082 default:
2083 internalError ();
2084 }
2085 }
2086 else
2087 {
2088 need_reloc:
2089 /* Don't generate a reloc if we are writing into a variant frag. */
2090 if (place == NULL)
2091 {
2092 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2093 address_expr,
2094 (*reloc_type == BFD_RELOC_16_PCREL
2095 || *reloc_type == BFD_RELOC_16_PCREL_S2),
2096 reloc_type[0]);
2097
2098 /* These relocations can have an addend that won't fit in
2099 4 octets for 64bit assembly. */
2100 if (HAVE_64BIT_GPRS &&
2101 (*reloc_type == BFD_RELOC_16
2102 || *reloc_type == BFD_RELOC_32
2103 || *reloc_type == BFD_RELOC_MIPS_JMP
2104 || *reloc_type == BFD_RELOC_HI16_S
2105 || *reloc_type == BFD_RELOC_LO16
2106 || *reloc_type == BFD_RELOC_GPREL16
2107 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2108 || *reloc_type == BFD_RELOC_GPREL32
2109 || *reloc_type == BFD_RELOC_64
2110 || *reloc_type == BFD_RELOC_CTOR
2111 || *reloc_type == BFD_RELOC_MIPS_SUB
2112 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2113 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2114 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2115 || *reloc_type == BFD_RELOC_MIPS_REL16
2116 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2117 fixp[0]->fx_no_overflow = 1;
2118
2119 if (unmatched_hi)
2120 {
2121 struct mips_hi_fixup *hi_fixup;
2122
2123 assert (*reloc_type == BFD_RELOC_HI16_S);
2124 hi_fixup = ((struct mips_hi_fixup *)
2125 xmalloc (sizeof (struct mips_hi_fixup)));
2126 hi_fixup->fixp = fixp[0];
2127 hi_fixup->seg = now_seg;
2128 hi_fixup->next = mips_hi_fixup_list;
2129 mips_hi_fixup_list = hi_fixup;
2130 }
2131
2132 if (reloc_type[1] != BFD_RELOC_UNUSED)
2133 {
2134 /* FIXME: This symbol can be one of
2135 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
2136 address_expr->X_op = O_absent;
2137 address_expr->X_add_symbol = 0;
2138 address_expr->X_add_number = 0;
2139
2140 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2141 4, address_expr, false,
2142 reloc_type[1]);
2143
2144 /* These relocations can have an addend that won't fit in
2145 4 octets for 64bit assembly. */
2146 if (HAVE_64BIT_GPRS &&
2147 (*reloc_type == BFD_RELOC_16
2148 || *reloc_type == BFD_RELOC_32
2149 || *reloc_type == BFD_RELOC_MIPS_JMP
2150 || *reloc_type == BFD_RELOC_HI16_S
2151 || *reloc_type == BFD_RELOC_LO16
2152 || *reloc_type == BFD_RELOC_GPREL16
2153 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2154 || *reloc_type == BFD_RELOC_GPREL32
2155 || *reloc_type == BFD_RELOC_64
2156 || *reloc_type == BFD_RELOC_CTOR
2157 || *reloc_type == BFD_RELOC_MIPS_SUB
2158 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2159 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2160 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2161 || *reloc_type == BFD_RELOC_MIPS_REL16
2162 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2163 fixp[1]->fx_no_overflow = 1;
2164
2165 if (reloc_type[2] != BFD_RELOC_UNUSED)
2166 {
2167 address_expr->X_op = O_absent;
2168 address_expr->X_add_symbol = 0;
2169 address_expr->X_add_number = 0;
2170
2171 fixp[2] = fix_new_exp (frag_now,
2172 f - frag_now->fr_literal, 4,
2173 address_expr, false,
2174 reloc_type[2]);
2175
2176 /* These relocations can have an addend that won't fit in
2177 4 octets for 64bit assembly. */
2178 if (HAVE_64BIT_GPRS &&
2179 (*reloc_type == BFD_RELOC_16
2180 || *reloc_type == BFD_RELOC_32
2181 || *reloc_type == BFD_RELOC_MIPS_JMP
2182 || *reloc_type == BFD_RELOC_HI16_S
2183 || *reloc_type == BFD_RELOC_LO16
2184 || *reloc_type == BFD_RELOC_GPREL16
2185 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2186 || *reloc_type == BFD_RELOC_GPREL32
2187 || *reloc_type == BFD_RELOC_64
2188 || *reloc_type == BFD_RELOC_CTOR
2189 || *reloc_type == BFD_RELOC_MIPS_SUB
2190 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2191 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2192 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2193 || *reloc_type == BFD_RELOC_MIPS_REL16
2194 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2195 fixp[2]->fx_no_overflow = 1;
2196 }
2197 }
2198 }
2199 }
2200 }
2201
2202 if (! mips_opts.mips16)
2203 {
2204 md_number_to_chars (f, ip->insn_opcode, 4);
2205 #ifdef OBJ_ELF
2206 dwarf2_emit_insn (4);
2207 #endif
2208 }
2209 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2210 {
2211 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2212 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2213 #ifdef OBJ_ELF
2214 dwarf2_emit_insn (4);
2215 #endif
2216 }
2217 else
2218 {
2219 if (ip->use_extend)
2220 {
2221 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2222 f += 2;
2223 }
2224 md_number_to_chars (f, ip->insn_opcode, 2);
2225 #ifdef OBJ_ELF
2226 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2227 #endif
2228 }
2229
2230 /* Update the register mask information. */
2231 if (! mips_opts.mips16)
2232 {
2233 if (pinfo & INSN_WRITE_GPR_D)
2234 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2235 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2236 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2237 if (pinfo & INSN_READ_GPR_S)
2238 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2239 if (pinfo & INSN_WRITE_GPR_31)
2240 mips_gprmask |= 1 << RA;
2241 if (pinfo & INSN_WRITE_FPR_D)
2242 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2243 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2244 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2245 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2246 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2247 if ((pinfo & INSN_READ_FPR_R) != 0)
2248 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2249 if (pinfo & INSN_COP)
2250 {
2251 /* We don't keep enough information to sort these cases out.
2252 The itbl support does keep this information however, although
2253 we currently don't support itbl fprmats as part of the cop
2254 instruction. May want to add this support in the future. */
2255 }
2256 /* Never set the bit for $0, which is always zero. */
2257 mips_gprmask &= ~1 << 0;
2258 }
2259 else
2260 {
2261 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2262 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2263 & MIPS16OP_MASK_RX);
2264 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2265 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2266 & MIPS16OP_MASK_RY);
2267 if (pinfo & MIPS16_INSN_WRITE_Z)
2268 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2269 & MIPS16OP_MASK_RZ);
2270 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2271 mips_gprmask |= 1 << TREG;
2272 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2273 mips_gprmask |= 1 << SP;
2274 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2275 mips_gprmask |= 1 << RA;
2276 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2277 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2278 if (pinfo & MIPS16_INSN_READ_Z)
2279 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2280 & MIPS16OP_MASK_MOVE32Z);
2281 if (pinfo & MIPS16_INSN_READ_GPR_X)
2282 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2283 & MIPS16OP_MASK_REGR32);
2284 }
2285
2286 if (place == NULL && ! mips_opts.noreorder)
2287 {
2288 /* Filling the branch delay slot is more complex. We try to
2289 switch the branch with the previous instruction, which we can
2290 do if the previous instruction does not set up a condition
2291 that the branch tests and if the branch is not itself the
2292 target of any branch. */
2293 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2294 || (pinfo & INSN_COND_BRANCH_DELAY))
2295 {
2296 if (mips_optimize < 2
2297 /* If we have seen .set volatile or .set nomove, don't
2298 optimize. */
2299 || mips_opts.nomove != 0
2300 /* If we had to emit any NOP instructions, then we
2301 already know we can not swap. */
2302 || nops != 0
2303 /* If we don't even know the previous insn, we can not
2304 swap. */
2305 || ! prev_insn_valid
2306 /* If the previous insn is already in a branch delay
2307 slot, then we can not swap. */
2308 || prev_insn_is_delay_slot
2309 /* If the previous previous insn was in a .set
2310 noreorder, we can't swap. Actually, the MIPS
2311 assembler will swap in this situation. However, gcc
2312 configured -with-gnu-as will generate code like
2313 .set noreorder
2314 lw $4,XXX
2315 .set reorder
2316 INSN
2317 bne $4,$0,foo
2318 in which we can not swap the bne and INSN. If gcc is
2319 not configured -with-gnu-as, it does not output the
2320 .set pseudo-ops. We don't have to check
2321 prev_insn_unreordered, because prev_insn_valid will
2322 be 0 in that case. We don't want to use
2323 prev_prev_insn_valid, because we do want to be able
2324 to swap at the start of a function. */
2325 || prev_prev_insn_unreordered
2326 /* If the branch is itself the target of a branch, we
2327 can not swap. We cheat on this; all we check for is
2328 whether there is a label on this instruction. If
2329 there are any branches to anything other than a
2330 label, users must use .set noreorder. */
2331 || insn_labels != NULL
2332 /* If the previous instruction is in a variant frag, we
2333 can not do the swap. This does not apply to the
2334 mips16, which uses variant frags for different
2335 purposes. */
2336 || (! mips_opts.mips16
2337 && prev_insn_frag->fr_type == rs_machine_dependent)
2338 /* If the branch reads the condition codes, we don't
2339 even try to swap, because in the sequence
2340 ctc1 $X,$31
2341 INSN
2342 INSN
2343 bc1t LABEL
2344 we can not swap, and I don't feel like handling that
2345 case. */
2346 || (! mips_opts.mips16
2347 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2348 && (pinfo & INSN_READ_COND_CODE))
2349 /* We can not swap with an instruction that requires a
2350 delay slot, becase the target of the branch might
2351 interfere with that instruction. */
2352 || (! mips_opts.mips16
2353 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2354 && (prev_pinfo
2355 /* Itbl support may require additional care here. */
2356 & (INSN_LOAD_COPROC_DELAY
2357 | INSN_COPROC_MOVE_DELAY
2358 | INSN_WRITE_COND_CODE)))
2359 || (! (hilo_interlocks
2360 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2361 && (prev_pinfo
2362 & (INSN_READ_LO
2363 | INSN_READ_HI)))
2364 || (! mips_opts.mips16
2365 && ! gpr_interlocks
2366 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2367 || (! mips_opts.mips16
2368 && mips_opts.isa == ISA_MIPS1
2369 /* Itbl support may require additional care here. */
2370 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2371 /* We can not swap with a branch instruction. */
2372 || (prev_pinfo
2373 & (INSN_UNCOND_BRANCH_DELAY
2374 | INSN_COND_BRANCH_DELAY
2375 | INSN_COND_BRANCH_LIKELY))
2376 /* We do not swap with a trap instruction, since it
2377 complicates trap handlers to have the trap
2378 instruction be in a delay slot. */
2379 || (prev_pinfo & INSN_TRAP)
2380 /* If the branch reads a register that the previous
2381 instruction sets, we can not swap. */
2382 || (! mips_opts.mips16
2383 && (prev_pinfo & INSN_WRITE_GPR_T)
2384 && insn_uses_reg (ip,
2385 ((prev_insn.insn_opcode >> OP_SH_RT)
2386 & OP_MASK_RT),
2387 MIPS_GR_REG))
2388 || (! mips_opts.mips16
2389 && (prev_pinfo & INSN_WRITE_GPR_D)
2390 && insn_uses_reg (ip,
2391 ((prev_insn.insn_opcode >> OP_SH_RD)
2392 & OP_MASK_RD),
2393 MIPS_GR_REG))
2394 || (mips_opts.mips16
2395 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2396 && insn_uses_reg (ip,
2397 ((prev_insn.insn_opcode
2398 >> MIPS16OP_SH_RX)
2399 & MIPS16OP_MASK_RX),
2400 MIPS16_REG))
2401 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2402 && insn_uses_reg (ip,
2403 ((prev_insn.insn_opcode
2404 >> MIPS16OP_SH_RY)
2405 & MIPS16OP_MASK_RY),
2406 MIPS16_REG))
2407 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2408 && insn_uses_reg (ip,
2409 ((prev_insn.insn_opcode
2410 >> MIPS16OP_SH_RZ)
2411 & MIPS16OP_MASK_RZ),
2412 MIPS16_REG))
2413 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2414 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2415 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2416 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2417 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2418 && insn_uses_reg (ip,
2419 MIPS16OP_EXTRACT_REG32R (prev_insn.
2420 insn_opcode),
2421 MIPS_GR_REG))))
2422 /* If the branch writes a register that the previous
2423 instruction sets, we can not swap (we know that
2424 branches write only to RD or to $31). */
2425 || (! mips_opts.mips16
2426 && (prev_pinfo & INSN_WRITE_GPR_T)
2427 && (((pinfo & INSN_WRITE_GPR_D)
2428 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2429 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2430 || ((pinfo & INSN_WRITE_GPR_31)
2431 && (((prev_insn.insn_opcode >> OP_SH_RT)
2432 & OP_MASK_RT)
2433 == RA))))
2434 || (! mips_opts.mips16
2435 && (prev_pinfo & INSN_WRITE_GPR_D)
2436 && (((pinfo & INSN_WRITE_GPR_D)
2437 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2438 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2439 || ((pinfo & INSN_WRITE_GPR_31)
2440 && (((prev_insn.insn_opcode >> OP_SH_RD)
2441 & OP_MASK_RD)
2442 == RA))))
2443 || (mips_opts.mips16
2444 && (pinfo & MIPS16_INSN_WRITE_31)
2445 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2446 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2447 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2448 == RA))))
2449 /* If the branch writes a register that the previous
2450 instruction reads, we can not swap (we know that
2451 branches only write to RD or to $31). */
2452 || (! mips_opts.mips16
2453 && (pinfo & INSN_WRITE_GPR_D)
2454 && insn_uses_reg (&prev_insn,
2455 ((ip->insn_opcode >> OP_SH_RD)
2456 & OP_MASK_RD),
2457 MIPS_GR_REG))
2458 || (! mips_opts.mips16
2459 && (pinfo & INSN_WRITE_GPR_31)
2460 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2461 || (mips_opts.mips16
2462 && (pinfo & MIPS16_INSN_WRITE_31)
2463 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2464 /* If we are generating embedded PIC code, the branch
2465 might be expanded into a sequence which uses $at, so
2466 we can't swap with an instruction which reads it. */
2467 || (mips_pic == EMBEDDED_PIC
2468 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2469 /* If the previous previous instruction has a load
2470 delay, and sets a register that the branch reads, we
2471 can not swap. */
2472 || (! mips_opts.mips16
2473 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2474 /* Itbl support may require additional care here. */
2475 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2476 || (! gpr_interlocks
2477 && (prev_prev_insn.insn_mo->pinfo
2478 & INSN_LOAD_MEMORY_DELAY)))
2479 && insn_uses_reg (ip,
2480 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2481 & OP_MASK_RT),
2482 MIPS_GR_REG))
2483 /* If one instruction sets a condition code and the
2484 other one uses a condition code, we can not swap. */
2485 || ((pinfo & INSN_READ_COND_CODE)
2486 && (prev_pinfo & INSN_WRITE_COND_CODE))
2487 || ((pinfo & INSN_WRITE_COND_CODE)
2488 && (prev_pinfo & INSN_READ_COND_CODE))
2489 /* If the previous instruction uses the PC, we can not
2490 swap. */
2491 || (mips_opts.mips16
2492 && (prev_pinfo & MIPS16_INSN_READ_PC))
2493 /* If the previous instruction was extended, we can not
2494 swap. */
2495 || (mips_opts.mips16 && prev_insn_extended)
2496 /* If the previous instruction had a fixup in mips16
2497 mode, we can not swap. This normally means that the
2498 previous instruction was a 4 byte branch anyhow. */
2499 || (mips_opts.mips16 && prev_insn_fixp[0])
2500 /* If the previous instruction is a sync, sync.l, or
2501 sync.p, we can not swap. */
2502 || (prev_pinfo & INSN_SYNC))
2503 {
2504 /* We could do even better for unconditional branches to
2505 portions of this object file; we could pick up the
2506 instruction at the destination, put it in the delay
2507 slot, and bump the destination address. */
2508 emit_nop ();
2509 /* Update the previous insn information. */
2510 prev_prev_insn = *ip;
2511 prev_insn.insn_mo = &dummy_opcode;
2512 }
2513 else
2514 {
2515 /* It looks like we can actually do the swap. */
2516 if (! mips_opts.mips16)
2517 {
2518 char *prev_f;
2519 char temp[4];
2520
2521 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2522 memcpy (temp, prev_f, 4);
2523 memcpy (prev_f, f, 4);
2524 memcpy (f, temp, 4);
2525 if (prev_insn_fixp[0])
2526 {
2527 prev_insn_fixp[0]->fx_frag = frag_now;
2528 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2529 }
2530 if (prev_insn_fixp[1])
2531 {
2532 prev_insn_fixp[1]->fx_frag = frag_now;
2533 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2534 }
2535 if (prev_insn_fixp[2])
2536 {
2537 prev_insn_fixp[2]->fx_frag = frag_now;
2538 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2539 }
2540 if (fixp[0])
2541 {
2542 fixp[0]->fx_frag = prev_insn_frag;
2543 fixp[0]->fx_where = prev_insn_where;
2544 }
2545 if (fixp[1])
2546 {
2547 fixp[1]->fx_frag = prev_insn_frag;
2548 fixp[1]->fx_where = prev_insn_where;
2549 }
2550 if (fixp[2])
2551 {
2552 fixp[2]->fx_frag = prev_insn_frag;
2553 fixp[2]->fx_where = prev_insn_where;
2554 }
2555 }
2556 else
2557 {
2558 char *prev_f;
2559 char temp[2];
2560
2561 assert (prev_insn_fixp[0] == NULL);
2562 assert (prev_insn_fixp[1] == NULL);
2563 assert (prev_insn_fixp[2] == NULL);
2564 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2565 memcpy (temp, prev_f, 2);
2566 memcpy (prev_f, f, 2);
2567 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2568 {
2569 assert (*reloc_type == BFD_RELOC_UNUSED);
2570 memcpy (f, temp, 2);
2571 }
2572 else
2573 {
2574 memcpy (f, f + 2, 2);
2575 memcpy (f + 2, temp, 2);
2576 }
2577 if (fixp[0])
2578 {
2579 fixp[0]->fx_frag = prev_insn_frag;
2580 fixp[0]->fx_where = prev_insn_where;
2581 }
2582 if (fixp[1])
2583 {
2584 fixp[1]->fx_frag = prev_insn_frag;
2585 fixp[1]->fx_where = prev_insn_where;
2586 }
2587 if (fixp[2])
2588 {
2589 fixp[2]->fx_frag = prev_insn_frag;
2590 fixp[2]->fx_where = prev_insn_where;
2591 }
2592 }
2593
2594 /* Update the previous insn information; leave prev_insn
2595 unchanged. */
2596 prev_prev_insn = *ip;
2597 }
2598 prev_insn_is_delay_slot = 1;
2599
2600 /* If that was an unconditional branch, forget the previous
2601 insn information. */
2602 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2603 {
2604 prev_prev_insn.insn_mo = &dummy_opcode;
2605 prev_insn.insn_mo = &dummy_opcode;
2606 }
2607
2608 prev_insn_fixp[0] = NULL;
2609 prev_insn_fixp[1] = NULL;
2610 prev_insn_fixp[2] = NULL;
2611 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2612 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2613 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2614 prev_insn_extended = 0;
2615 }
2616 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2617 {
2618 /* We don't yet optimize a branch likely. What we should do
2619 is look at the target, copy the instruction found there
2620 into the delay slot, and increment the branch to jump to
2621 the next instruction. */
2622 emit_nop ();
2623 /* Update the previous insn information. */
2624 prev_prev_insn = *ip;
2625 prev_insn.insn_mo = &dummy_opcode;
2626 prev_insn_fixp[0] = NULL;
2627 prev_insn_fixp[1] = NULL;
2628 prev_insn_fixp[2] = NULL;
2629 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2630 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2631 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2632 prev_insn_extended = 0;
2633 }
2634 else
2635 {
2636 /* Update the previous insn information. */
2637 if (nops > 0)
2638 prev_prev_insn.insn_mo = &dummy_opcode;
2639 else
2640 prev_prev_insn = prev_insn;
2641 prev_insn = *ip;
2642
2643 /* Any time we see a branch, we always fill the delay slot
2644 immediately; since this insn is not a branch, we know it
2645 is not in a delay slot. */
2646 prev_insn_is_delay_slot = 0;
2647
2648 prev_insn_fixp[0] = fixp[0];
2649 prev_insn_fixp[1] = fixp[1];
2650 prev_insn_fixp[2] = fixp[2];
2651 prev_insn_reloc_type[0] = reloc_type[0];
2652 prev_insn_reloc_type[1] = reloc_type[1];
2653 prev_insn_reloc_type[2] = reloc_type[2];
2654 if (mips_opts.mips16)
2655 prev_insn_extended = (ip->use_extend
2656 || *reloc_type > BFD_RELOC_UNUSED);
2657 }
2658
2659 prev_prev_insn_unreordered = prev_insn_unreordered;
2660 prev_insn_unreordered = 0;
2661 prev_insn_frag = frag_now;
2662 prev_insn_where = f - frag_now->fr_literal;
2663 prev_insn_valid = 1;
2664 }
2665 else if (place == NULL)
2666 {
2667 /* We need to record a bit of information even when we are not
2668 reordering, in order to determine the base address for mips16
2669 PC relative relocs. */
2670 prev_prev_insn = prev_insn;
2671 prev_insn = *ip;
2672 prev_insn_reloc_type[0] = reloc_type[0];
2673 prev_insn_reloc_type[1] = reloc_type[1];
2674 prev_insn_reloc_type[2] = reloc_type[2];
2675 prev_prev_insn_unreordered = prev_insn_unreordered;
2676 prev_insn_unreordered = 1;
2677 }
2678
2679 /* We just output an insn, so the next one doesn't have a label. */
2680 mips_clear_insn_labels ();
2681
2682 /* We must ensure that a fixup associated with an unmatched %hi
2683 reloc does not become a variant frag. Otherwise, the
2684 rearrangement of %hi relocs in frob_file may confuse
2685 tc_gen_reloc. */
2686 if (unmatched_hi)
2687 {
2688 frag_wane (frag_now);
2689 frag_new (0);
2690 }
2691 }
2692
2693 /* This function forgets that there was any previous instruction or
2694 label. If PRESERVE is non-zero, it remembers enough information to
2695 know whether nops are needed before a noreorder section. */
2696
2697 static void
2698 mips_no_prev_insn (preserve)
2699 int preserve;
2700 {
2701 if (! preserve)
2702 {
2703 prev_insn.insn_mo = &dummy_opcode;
2704 prev_prev_insn.insn_mo = &dummy_opcode;
2705 prev_nop_frag = NULL;
2706 prev_nop_frag_holds = 0;
2707 prev_nop_frag_required = 0;
2708 prev_nop_frag_since = 0;
2709 }
2710 prev_insn_valid = 0;
2711 prev_insn_is_delay_slot = 0;
2712 prev_insn_unreordered = 0;
2713 prev_insn_extended = 0;
2714 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2715 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2716 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2717 prev_prev_insn_unreordered = 0;
2718 mips_clear_insn_labels ();
2719 }
2720
2721 /* This function must be called whenever we turn on noreorder or emit
2722 something other than instructions. It inserts any NOPS which might
2723 be needed by the previous instruction, and clears the information
2724 kept for the previous instructions. The INSNS parameter is true if
2725 instructions are to follow. */
2726
2727 static void
2728 mips_emit_delays (insns)
2729 boolean insns;
2730 {
2731 if (! mips_opts.noreorder)
2732 {
2733 int nops;
2734
2735 nops = 0;
2736 if ((! mips_opts.mips16
2737 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2738 && (! cop_interlocks
2739 && (prev_insn.insn_mo->pinfo
2740 & (INSN_LOAD_COPROC_DELAY
2741 | INSN_COPROC_MOVE_DELAY
2742 | INSN_WRITE_COND_CODE))))
2743 || (! hilo_interlocks
2744 && (prev_insn.insn_mo->pinfo
2745 & (INSN_READ_LO
2746 | INSN_READ_HI)))
2747 || (! mips_opts.mips16
2748 && ! gpr_interlocks
2749 && (prev_insn.insn_mo->pinfo
2750 & INSN_LOAD_MEMORY_DELAY))
2751 || (! mips_opts.mips16
2752 && mips_opts.isa == ISA_MIPS1
2753 && (prev_insn.insn_mo->pinfo
2754 & INSN_COPROC_MEMORY_DELAY)))
2755 {
2756 /* Itbl support may require additional care here. */
2757 ++nops;
2758 if ((! mips_opts.mips16
2759 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2760 && (! cop_interlocks
2761 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2762 || (! hilo_interlocks
2763 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2764 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2765 ++nops;
2766
2767 if (prev_insn_unreordered)
2768 nops = 0;
2769 }
2770 else if ((! mips_opts.mips16
2771 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2772 && (! cop_interlocks
2773 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2774 || (! hilo_interlocks
2775 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2776 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2777 {
2778 /* Itbl support may require additional care here. */
2779 if (! prev_prev_insn_unreordered)
2780 ++nops;
2781 }
2782
2783 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2784 {
2785 int min_nops = 0;
2786 const char *pn = prev_insn.insn_mo->name;
2787 if (strncmp(pn, "macc", 4) == 0
2788 || strncmp(pn, "dmacc", 5) == 0
2789 || strncmp(pn, "dmult", 5) == 0)
2790 {
2791 min_nops = 1;
2792 }
2793 if (nops < min_nops)
2794 nops = min_nops;
2795 }
2796
2797 if (nops > 0)
2798 {
2799 struct insn_label_list *l;
2800
2801 if (insns)
2802 {
2803 /* Record the frag which holds the nop instructions, so
2804 that we can remove them if we don't need them. */
2805 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2806 prev_nop_frag = frag_now;
2807 prev_nop_frag_holds = nops;
2808 prev_nop_frag_required = 0;
2809 prev_nop_frag_since = 0;
2810 }
2811
2812 for (; nops > 0; --nops)
2813 emit_nop ();
2814
2815 if (insns)
2816 {
2817 /* Move on to a new frag, so that it is safe to simply
2818 decrease the size of prev_nop_frag. */
2819 frag_wane (frag_now);
2820 frag_new (0);
2821 }
2822
2823 for (l = insn_labels; l != NULL; l = l->next)
2824 {
2825 valueT val;
2826
2827 assert (S_GET_SEGMENT (l->label) == now_seg);
2828 symbol_set_frag (l->label, frag_now);
2829 val = (valueT) frag_now_fix ();
2830 /* mips16 text labels are stored as odd. */
2831 if (mips_opts.mips16)
2832 ++val;
2833 S_SET_VALUE (l->label, val);
2834 }
2835 }
2836 }
2837
2838 /* Mark instruction labels in mips16 mode. */
2839 if (insns)
2840 mips16_mark_labels ();
2841
2842 mips_no_prev_insn (insns);
2843 }
2844
2845 /* Build an instruction created by a macro expansion. This is passed
2846 a pointer to the count of instructions created so far, an
2847 expression, the name of the instruction to build, an operand format
2848 string, and corresponding arguments. */
2849
2850 #ifdef USE_STDARG
2851 static void
2852 macro_build (char *place,
2853 int *counter,
2854 expressionS * ep,
2855 const char *name,
2856 const char *fmt,
2857 ...)
2858 #else
2859 static void
2860 macro_build (place, counter, ep, name, fmt, va_alist)
2861 char *place;
2862 int *counter;
2863 expressionS *ep;
2864 const char *name;
2865 const char *fmt;
2866 va_dcl
2867 #endif
2868 {
2869 struct mips_cl_insn insn;
2870 bfd_reloc_code_real_type r[3];
2871 va_list args;
2872
2873 #ifdef USE_STDARG
2874 va_start (args, fmt);
2875 #else
2876 va_start (args);
2877 #endif
2878
2879 /*
2880 * If the macro is about to expand into a second instruction,
2881 * print a warning if needed. We need to pass ip as a parameter
2882 * to generate a better warning message here...
2883 */
2884 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2885 as_warn (_("Macro instruction expanded into multiple instructions"));
2886
2887 /*
2888 * If the macro is about to expand into a second instruction,
2889 * and it is in a delay slot, print a warning.
2890 */
2891 if (place == NULL
2892 && *counter == 1
2893 && mips_opts.noreorder
2894 && (prev_prev_insn.insn_mo->pinfo
2895 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2896 | INSN_COND_BRANCH_LIKELY)) != 0)
2897 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2898
2899 if (place == NULL)
2900 ++*counter; /* bump instruction counter */
2901
2902 if (mips_opts.mips16)
2903 {
2904 mips16_macro_build (place, counter, ep, name, fmt, args);
2905 va_end (args);
2906 return;
2907 }
2908
2909 r[0] = BFD_RELOC_UNUSED;
2910 r[1] = BFD_RELOC_UNUSED;
2911 r[2] = BFD_RELOC_UNUSED;
2912 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2913 assert (insn.insn_mo);
2914 assert (strcmp (name, insn.insn_mo->name) == 0);
2915
2916 /* Search until we get a match for NAME. */
2917 while (1)
2918 {
2919 /* It is assumed here that macros will never generate
2920 MDMX or MIPS-3D instructions. */
2921 if (strcmp (fmt, insn.insn_mo->args) == 0
2922 && insn.insn_mo->pinfo != INSN_MACRO
2923 && OPCODE_IS_MEMBER (insn.insn_mo,
2924 (mips_opts.isa
2925 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2926 mips_arch)
2927 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2928 break;
2929
2930 ++insn.insn_mo;
2931 assert (insn.insn_mo->name);
2932 assert (strcmp (name, insn.insn_mo->name) == 0);
2933 }
2934
2935 insn.insn_opcode = insn.insn_mo->match;
2936 for (;;)
2937 {
2938 switch (*fmt++)
2939 {
2940 case '\0':
2941 break;
2942
2943 case ',':
2944 case '(':
2945 case ')':
2946 continue;
2947
2948 case 't':
2949 case 'w':
2950 case 'E':
2951 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2952 continue;
2953
2954 case 'c':
2955 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2956 continue;
2957
2958 case 'T':
2959 case 'W':
2960 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2961 continue;
2962
2963 case 'd':
2964 case 'G':
2965 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2966 continue;
2967
2968 case 'U':
2969 {
2970 int tmp = va_arg (args, int);
2971
2972 insn.insn_opcode |= tmp << OP_SH_RT;
2973 insn.insn_opcode |= tmp << OP_SH_RD;
2974 continue;
2975 }
2976
2977 case 'V':
2978 case 'S':
2979 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2980 continue;
2981
2982 case 'z':
2983 continue;
2984
2985 case '<':
2986 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2987 continue;
2988
2989 case 'D':
2990 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2991 continue;
2992
2993 case 'B':
2994 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2995 continue;
2996
2997 case 'J':
2998 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2999 continue;
3000
3001 case 'q':
3002 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3003 continue;
3004
3005 case 'b':
3006 case 's':
3007 case 'r':
3008 case 'v':
3009 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3010 continue;
3011
3012 case 'i':
3013 case 'j':
3014 case 'o':
3015 *r = (bfd_reloc_code_real_type) va_arg (args, int);
3016 assert (*r == BFD_RELOC_GPREL16
3017 || *r == BFD_RELOC_MIPS_LITERAL
3018 || *r == BFD_RELOC_MIPS_HIGHER
3019 || *r == BFD_RELOC_HI16_S
3020 || *r == BFD_RELOC_LO16
3021 || *r == BFD_RELOC_MIPS_GOT16
3022 || *r == BFD_RELOC_MIPS_CALL16
3023 || *r == BFD_RELOC_MIPS_GOT_DISP
3024 || *r == BFD_RELOC_MIPS_GOT_PAGE
3025 || *r == BFD_RELOC_MIPS_GOT_OFST
3026 || *r == BFD_RELOC_MIPS_GOT_LO16
3027 || *r == BFD_RELOC_MIPS_CALL_LO16
3028 || (ep->X_op == O_subtract
3029 && *r == BFD_RELOC_PCREL_LO16));
3030 continue;
3031
3032 case 'u':
3033 *r = (bfd_reloc_code_real_type) va_arg (args, int);
3034 assert (ep != NULL
3035 && (ep->X_op == O_constant
3036 || (ep->X_op == O_symbol
3037 && (*r == BFD_RELOC_MIPS_HIGHEST
3038 || *r == BFD_RELOC_HI16_S
3039 || *r == BFD_RELOC_HI16
3040 || *r == BFD_RELOC_GPREL16
3041 || *r == BFD_RELOC_MIPS_GOT_HI16
3042 || *r == BFD_RELOC_MIPS_CALL_HI16))
3043 || (ep->X_op == O_subtract
3044 && *r == BFD_RELOC_PCREL_HI16_S)));
3045 continue;
3046
3047 case 'p':
3048 assert (ep != NULL);
3049 /*
3050 * This allows macro() to pass an immediate expression for
3051 * creating short branches without creating a symbol.
3052 * Note that the expression still might come from the assembly
3053 * input, in which case the value is not checked for range nor
3054 * is a relocation entry generated (yuck).
3055 */
3056 if (ep->X_op == O_constant)
3057 {
3058 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3059 ep = NULL;
3060 }
3061 else
3062 if (mips_pic == EMBEDDED_PIC)
3063 *r = BFD_RELOC_16_PCREL_S2;
3064 else
3065 *r = BFD_RELOC_16_PCREL;
3066 continue;
3067
3068 case 'a':
3069 assert (ep != NULL);
3070 *r = BFD_RELOC_MIPS_JMP;
3071 continue;
3072
3073 case 'C':
3074 insn.insn_opcode |= va_arg (args, unsigned long);
3075 continue;
3076
3077 default:
3078 internalError ();
3079 }
3080 break;
3081 }
3082 va_end (args);
3083 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3084
3085 append_insn (place, &insn, ep, r, false);
3086 }
3087
3088 static void
3089 mips16_macro_build (place, counter, ep, name, fmt, args)
3090 char *place;
3091 int *counter ATTRIBUTE_UNUSED;
3092 expressionS *ep;
3093 const char *name;
3094 const char *fmt;
3095 va_list args;
3096 {
3097 struct mips_cl_insn insn;
3098 bfd_reloc_code_real_type r[3]
3099 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3100
3101 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3102 assert (insn.insn_mo);
3103 assert (strcmp (name, insn.insn_mo->name) == 0);
3104
3105 while (strcmp (fmt, insn.insn_mo->args) != 0
3106 || insn.insn_mo->pinfo == INSN_MACRO)
3107 {
3108 ++insn.insn_mo;
3109 assert (insn.insn_mo->name);
3110 assert (strcmp (name, insn.insn_mo->name) == 0);
3111 }
3112
3113 insn.insn_opcode = insn.insn_mo->match;
3114 insn.use_extend = false;
3115
3116 for (;;)
3117 {
3118 int c;
3119
3120 c = *fmt++;
3121 switch (c)
3122 {
3123 case '\0':
3124 break;
3125
3126 case ',':
3127 case '(':
3128 case ')':
3129 continue;
3130
3131 case 'y':
3132 case 'w':
3133 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3134 continue;
3135
3136 case 'x':
3137 case 'v':
3138 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3139 continue;
3140
3141 case 'z':
3142 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3143 continue;
3144
3145 case 'Z':
3146 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3147 continue;
3148
3149 case '0':
3150 case 'S':
3151 case 'P':
3152 case 'R':
3153 continue;
3154
3155 case 'X':
3156 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3157 continue;
3158
3159 case 'Y':
3160 {
3161 int regno;
3162
3163 regno = va_arg (args, int);
3164 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3165 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3166 }
3167 continue;
3168
3169 case '<':
3170 case '>':
3171 case '4':
3172 case '5':
3173 case 'H':
3174 case 'W':
3175 case 'D':
3176 case 'j':
3177 case '8':
3178 case 'V':
3179 case 'C':
3180 case 'U':
3181 case 'k':
3182 case 'K':
3183 case 'p':
3184 case 'q':
3185 {
3186 assert (ep != NULL);
3187
3188 if (ep->X_op != O_constant)
3189 *r = (int) BFD_RELOC_UNUSED + c;
3190 else
3191 {
3192 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
3193 false, &insn.insn_opcode, &insn.use_extend,
3194 &insn.extend);
3195 ep = NULL;
3196 *r = BFD_RELOC_UNUSED;
3197 }
3198 }
3199 continue;
3200
3201 case '6':
3202 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3203 continue;
3204 }
3205
3206 break;
3207 }
3208
3209 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3210
3211 append_insn (place, &insn, ep, r, false);
3212 }
3213
3214 /*
3215 * Generate a "jalr" instruction with a relocation hint to the called
3216 * function. This occurs in NewABI PIC code.
3217 */
3218 static void
3219 macro_build_jalr (icnt, ep)
3220 int icnt;
3221 expressionS *ep;
3222 {
3223 char *f;
3224
3225 if (HAVE_NEWABI)
3226 {
3227 frag_grow (4);
3228 f = frag_more (0);
3229 }
3230 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3231 RA, PIC_CALL_REG);
3232 if (HAVE_NEWABI)
3233 fix_new_exp (frag_now, f - frag_now->fr_literal,
3234 0, ep, false, BFD_RELOC_MIPS_JALR);
3235 }
3236
3237 /*
3238 * Generate a "lui" instruction.
3239 */
3240 static void
3241 macro_build_lui (place, counter, ep, regnum)
3242 char *place;
3243 int *counter;
3244 expressionS *ep;
3245 int regnum;
3246 {
3247 expressionS high_expr;
3248 struct mips_cl_insn insn;
3249 bfd_reloc_code_real_type r[3]
3250 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3251 const char *name = "lui";
3252 const char *fmt = "t,u";
3253
3254 assert (! mips_opts.mips16);
3255
3256 if (place == NULL)
3257 high_expr = *ep;
3258 else
3259 {
3260 high_expr.X_op = O_constant;
3261 high_expr.X_add_number = ep->X_add_number;
3262 }
3263
3264 if (high_expr.X_op == O_constant)
3265 {
3266 /* we can compute the instruction now without a relocation entry */
3267 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3268 >> 16) & 0xffff;
3269 *r = BFD_RELOC_UNUSED;
3270 }
3271 else if (! HAVE_NEWABI)
3272 {
3273 assert (ep->X_op == O_symbol);
3274 /* _gp_disp is a special case, used from s_cpload. */
3275 assert (mips_pic == NO_PIC
3276 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3277 *r = BFD_RELOC_HI16_S;
3278 }
3279
3280 /*
3281 * If the macro is about to expand into a second instruction,
3282 * print a warning if needed. We need to pass ip as a parameter
3283 * to generate a better warning message here...
3284 */
3285 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3286 as_warn (_("Macro instruction expanded into multiple instructions"));
3287
3288 if (place == NULL)
3289 ++*counter; /* bump instruction counter */
3290
3291 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3292 assert (insn.insn_mo);
3293 assert (strcmp (name, insn.insn_mo->name) == 0);
3294 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3295
3296 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3297 if (*r == BFD_RELOC_UNUSED)
3298 {
3299 insn.insn_opcode |= high_expr.X_add_number;
3300 append_insn (place, &insn, NULL, r, false);
3301 }
3302 else
3303 append_insn (place, &insn, &high_expr, r, false);
3304 }
3305
3306 /* Generate a sequence of instructions to do a load or store from a constant
3307 offset off of a base register (breg) into/from a target register (treg),
3308 using AT if necessary. */
3309 static void
3310 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3311 char *place;
3312 int *counter;
3313 expressionS *ep;
3314 const char *op;
3315 int treg, breg;
3316 {
3317 assert (ep->X_op == O_constant);
3318
3319 /* Right now, this routine can only handle signed 32-bit contants. */
3320 if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3321 as_warn (_("operand overflow"));
3322
3323 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3324 {
3325 /* Signed 16-bit offset will fit in the op. Easy! */
3326 macro_build (place, counter, ep, op, "t,o(b)", treg,
3327 (int) BFD_RELOC_LO16, breg);
3328 }
3329 else
3330 {
3331 /* 32-bit offset, need multiple instructions and AT, like:
3332 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3333 addu $tempreg,$tempreg,$breg
3334 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3335 to handle the complete offset. */
3336 macro_build_lui (place, counter, ep, AT);
3337 if (place != NULL)
3338 place += 4;
3339 macro_build (place, counter, (expressionS *) NULL,
3340 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3341 "d,v,t", AT, AT, breg);
3342 if (place != NULL)
3343 place += 4;
3344 macro_build (place, counter, ep, op, "t,o(b)", treg,
3345 (int) BFD_RELOC_LO16, AT);
3346
3347 if (mips_opts.noat)
3348 as_warn (_("Macro used $at after \".set noat\""));
3349 }
3350 }
3351
3352 /* set_at()
3353 * Generates code to set the $at register to true (one)
3354 * if reg is less than the immediate expression.
3355 */
3356 static void
3357 set_at (counter, reg, unsignedp)
3358 int *counter;
3359 int reg;
3360 int unsignedp;
3361 {
3362 if (imm_expr.X_op == O_constant
3363 && imm_expr.X_add_number >= -0x8000
3364 && imm_expr.X_add_number < 0x8000)
3365 macro_build ((char *) NULL, counter, &imm_expr,
3366 unsignedp ? "sltiu" : "slti",
3367 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3368 else
3369 {
3370 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3371 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3372 unsignedp ? "sltu" : "slt",
3373 "d,v,t", AT, reg, AT);
3374 }
3375 }
3376
3377 /* Warn if an expression is not a constant. */
3378
3379 static void
3380 check_absolute_expr (ip, ex)
3381 struct mips_cl_insn *ip;
3382 expressionS *ex;
3383 {
3384 if (ex->X_op == O_big)
3385 as_bad (_("unsupported large constant"));
3386 else if (ex->X_op != O_constant)
3387 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3388 }
3389
3390 /* Count the leading zeroes by performing a binary chop. This is a
3391 bulky bit of source, but performance is a LOT better for the
3392 majority of values than a simple loop to count the bits:
3393 for (lcnt = 0; (lcnt < 32); lcnt++)
3394 if ((v) & (1 << (31 - lcnt)))
3395 break;
3396 However it is not code size friendly, and the gain will drop a bit
3397 on certain cached systems.
3398 */
3399 #define COUNT_TOP_ZEROES(v) \
3400 (((v) & ~0xffff) == 0 \
3401 ? ((v) & ~0xff) == 0 \
3402 ? ((v) & ~0xf) == 0 \
3403 ? ((v) & ~0x3) == 0 \
3404 ? ((v) & ~0x1) == 0 \
3405 ? !(v) \
3406 ? 32 \
3407 : 31 \
3408 : 30 \
3409 : ((v) & ~0x7) == 0 \
3410 ? 29 \
3411 : 28 \
3412 : ((v) & ~0x3f) == 0 \
3413 ? ((v) & ~0x1f) == 0 \
3414 ? 27 \
3415 : 26 \
3416 : ((v) & ~0x7f) == 0 \
3417 ? 25 \
3418 : 24 \
3419 : ((v) & ~0xfff) == 0 \
3420 ? ((v) & ~0x3ff) == 0 \
3421 ? ((v) & ~0x1ff) == 0 \
3422 ? 23 \
3423 : 22 \
3424 : ((v) & ~0x7ff) == 0 \
3425 ? 21 \
3426 : 20 \
3427 : ((v) & ~0x3fff) == 0 \
3428 ? ((v) & ~0x1fff) == 0 \
3429 ? 19 \
3430 : 18 \
3431 : ((v) & ~0x7fff) == 0 \
3432 ? 17 \
3433 : 16 \
3434 : ((v) & ~0xffffff) == 0 \
3435 ? ((v) & ~0xfffff) == 0 \
3436 ? ((v) & ~0x3ffff) == 0 \
3437 ? ((v) & ~0x1ffff) == 0 \
3438 ? 15 \
3439 : 14 \
3440 : ((v) & ~0x7ffff) == 0 \
3441 ? 13 \
3442 : 12 \
3443 : ((v) & ~0x3fffff) == 0 \
3444 ? ((v) & ~0x1fffff) == 0 \
3445 ? 11 \
3446 : 10 \
3447 : ((v) & ~0x7fffff) == 0 \
3448 ? 9 \
3449 : 8 \
3450 : ((v) & ~0xfffffff) == 0 \
3451 ? ((v) & ~0x3ffffff) == 0 \
3452 ? ((v) & ~0x1ffffff) == 0 \
3453 ? 7 \
3454 : 6 \
3455 : ((v) & ~0x7ffffff) == 0 \
3456 ? 5 \
3457 : 4 \
3458 : ((v) & ~0x3fffffff) == 0 \
3459 ? ((v) & ~0x1fffffff) == 0 \
3460 ? 3 \
3461 : 2 \
3462 : ((v) & ~0x7fffffff) == 0 \
3463 ? 1 \
3464 : 0)
3465
3466 /* load_register()
3467 * This routine generates the least number of instructions neccessary to load
3468 * an absolute expression value into a register.
3469 */
3470 static void
3471 load_register (counter, reg, ep, dbl)
3472 int *counter;
3473 int reg;
3474 expressionS *ep;
3475 int dbl;
3476 {
3477 int freg;
3478 expressionS hi32, lo32;
3479
3480 if (ep->X_op != O_big)
3481 {
3482 assert (ep->X_op == O_constant);
3483 if (ep->X_add_number < 0x8000
3484 && (ep->X_add_number >= 0
3485 || (ep->X_add_number >= -0x8000
3486 && (! dbl
3487 || ! ep->X_unsigned
3488 || sizeof (ep->X_add_number) > 4))))
3489 {
3490 /* We can handle 16 bit signed values with an addiu to
3491 $zero. No need to ever use daddiu here, since $zero and
3492 the result are always correct in 32 bit mode. */
3493 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3494 (int) BFD_RELOC_LO16);
3495 return;
3496 }
3497 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3498 {
3499 /* We can handle 16 bit unsigned values with an ori to
3500 $zero. */
3501 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3502 (int) BFD_RELOC_LO16);
3503 return;
3504 }
3505 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3506 && (! dbl
3507 || ! ep->X_unsigned
3508 || sizeof (ep->X_add_number) > 4
3509 || (ep->X_add_number & 0x80000000) == 0))
3510 || ((HAVE_32BIT_GPRS || ! dbl)
3511 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3512 || (HAVE_32BIT_GPRS
3513 && ! dbl
3514 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3515 == ~ (offsetT) 0xffffffff)))
3516 {
3517 /* 32 bit values require an lui. */
3518 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3519 (int) BFD_RELOC_HI16);
3520 if ((ep->X_add_number & 0xffff) != 0)
3521 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3522 (int) BFD_RELOC_LO16);
3523 return;
3524 }
3525 }
3526
3527 /* The value is larger than 32 bits. */
3528
3529 if (HAVE_32BIT_GPRS)
3530 {
3531 as_bad (_("Number (0x%lx) larger than 32 bits"),
3532 (unsigned long) ep->X_add_number);
3533 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3534 (int) BFD_RELOC_LO16);
3535 return;
3536 }
3537
3538 if (ep->X_op != O_big)
3539 {
3540 hi32 = *ep;
3541 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3542 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3543 hi32.X_add_number &= 0xffffffff;
3544 lo32 = *ep;
3545 lo32.X_add_number &= 0xffffffff;
3546 }
3547 else
3548 {
3549 assert (ep->X_add_number > 2);
3550 if (ep->X_add_number == 3)
3551 generic_bignum[3] = 0;
3552 else if (ep->X_add_number > 4)
3553 as_bad (_("Number larger than 64 bits"));
3554 lo32.X_op = O_constant;
3555 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3556 hi32.X_op = O_constant;
3557 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3558 }
3559
3560 if (hi32.X_add_number == 0)
3561 freg = 0;
3562 else
3563 {
3564 int shift, bit;
3565 unsigned long hi, lo;
3566
3567 if (hi32.X_add_number == (offsetT) 0xffffffff)
3568 {
3569 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3570 {
3571 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3572 reg, 0, (int) BFD_RELOC_LO16);
3573 return;
3574 }
3575 if (lo32.X_add_number & 0x80000000)
3576 {
3577 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3578 (int) BFD_RELOC_HI16);
3579 if (lo32.X_add_number & 0xffff)
3580 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3581 reg, reg, (int) BFD_RELOC_LO16);
3582 return;
3583 }
3584 }
3585
3586 /* Check for 16bit shifted constant. We know that hi32 is
3587 non-zero, so start the mask on the first bit of the hi32
3588 value. */
3589 shift = 17;
3590 do
3591 {
3592 unsigned long himask, lomask;
3593
3594 if (shift < 32)
3595 {
3596 himask = 0xffff >> (32 - shift);
3597 lomask = (0xffff << shift) & 0xffffffff;
3598 }
3599 else
3600 {
3601 himask = 0xffff << (shift - 32);
3602 lomask = 0;
3603 }
3604 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3605 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3606 {
3607 expressionS tmp;
3608
3609 tmp.X_op = O_constant;
3610 if (shift < 32)
3611 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3612 | (lo32.X_add_number >> shift));
3613 else
3614 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3615 macro_build ((char *) NULL, counter, &tmp,
3616 "ori", "t,r,i", reg, 0,
3617 (int) BFD_RELOC_LO16);
3618 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3619 (shift >= 32) ? "dsll32" : "dsll",
3620 "d,w,<", reg, reg,
3621 (shift >= 32) ? shift - 32 : shift);
3622 return;
3623 }
3624 ++shift;
3625 }
3626 while (shift <= (64 - 16));
3627
3628 /* Find the bit number of the lowest one bit, and store the
3629 shifted value in hi/lo. */
3630 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3631 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3632 if (lo != 0)
3633 {
3634 bit = 0;
3635 while ((lo & 1) == 0)
3636 {
3637 lo >>= 1;
3638 ++bit;
3639 }
3640 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3641 hi >>= bit;
3642 }
3643 else
3644 {
3645 bit = 32;
3646 while ((hi & 1) == 0)
3647 {
3648 hi >>= 1;
3649 ++bit;
3650 }
3651 lo = hi;
3652 hi = 0;
3653 }
3654
3655 /* Optimize if the shifted value is a (power of 2) - 1. */
3656 if ((hi == 0 && ((lo + 1) & lo) == 0)
3657 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3658 {
3659 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3660 if (shift != 0)
3661 {
3662 expressionS tmp;
3663
3664 /* This instruction will set the register to be all
3665 ones. */
3666 tmp.X_op = O_constant;
3667 tmp.X_add_number = (offsetT) -1;
3668 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3669 reg, 0, (int) BFD_RELOC_LO16);
3670 if (bit != 0)
3671 {
3672 bit += shift;
3673 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3674 (bit >= 32) ? "dsll32" : "dsll",
3675 "d,w,<", reg, reg,
3676 (bit >= 32) ? bit - 32 : bit);
3677 }
3678 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3679 (shift >= 32) ? "dsrl32" : "dsrl",
3680 "d,w,<", reg, reg,
3681 (shift >= 32) ? shift - 32 : shift);
3682 return;
3683 }
3684 }
3685
3686 /* Sign extend hi32 before calling load_register, because we can
3687 generally get better code when we load a sign extended value. */
3688 if ((hi32.X_add_number & 0x80000000) != 0)
3689 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3690 load_register (counter, reg, &hi32, 0);
3691 freg = reg;
3692 }
3693 if ((lo32.X_add_number & 0xffff0000) == 0)
3694 {
3695 if (freg != 0)
3696 {
3697 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3698 "dsll32", "d,w,<", reg, freg, 0);
3699 freg = reg;
3700 }
3701 }
3702 else
3703 {
3704 expressionS mid16;
3705
3706 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3707 {
3708 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3709 (int) BFD_RELOC_HI16);
3710 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3711 "dsrl32", "d,w,<", reg, reg, 0);
3712 return;
3713 }
3714
3715 if (freg != 0)
3716 {
3717 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3718 "d,w,<", reg, freg, 16);
3719 freg = reg;
3720 }
3721 mid16 = lo32;
3722 mid16.X_add_number >>= 16;
3723 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3724 freg, (int) BFD_RELOC_LO16);
3725 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3726 "d,w,<", reg, reg, 16);
3727 freg = reg;
3728 }
3729 if ((lo32.X_add_number & 0xffff) != 0)
3730 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3731 (int) BFD_RELOC_LO16);
3732 }
3733
3734 /* Load an address into a register. */
3735
3736 static void
3737 load_address (counter, reg, ep, used_at)
3738 int *counter;
3739 int reg;
3740 expressionS *ep;
3741 int *used_at;
3742 {
3743 char *p = NULL;
3744
3745 if (ep->X_op != O_constant
3746 && ep->X_op != O_symbol)
3747 {
3748 as_bad (_("expression too complex"));
3749 ep->X_op = O_constant;
3750 }
3751
3752 if (ep->X_op == O_constant)
3753 {
3754 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3755 return;
3756 }
3757
3758 if (mips_pic == NO_PIC)
3759 {
3760 /* If this is a reference to a GP relative symbol, we want
3761 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3762 Otherwise we want
3763 lui $reg,<sym> (BFD_RELOC_HI16_S)
3764 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3765 If we have an addend, we always use the latter form.
3766
3767 With 64bit address space and a usable $at we want
3768 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3769 lui $at,<sym> (BFD_RELOC_HI16_S)
3770 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3771 daddiu $at,<sym> (BFD_RELOC_LO16)
3772 dsll32 $reg,0
3773 daddu $reg,$reg,$at
3774
3775 If $at is already in use, we use an path which is suboptimal
3776 on superscalar processors.
3777 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3778 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3779 dsll $reg,16
3780 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3781 dsll $reg,16
3782 daddiu $reg,<sym> (BFD_RELOC_LO16)
3783 */
3784 if (HAVE_64BIT_ADDRESSES)
3785 {
3786 /* We don't do GP optimization for now because RELAX_ENCODE can't
3787 hold the data for such large chunks. */
3788
3789 if (*used_at == 0 && ! mips_opts.noat)
3790 {
3791 macro_build (p, counter, ep, "lui", "t,u",
3792 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3793 macro_build (p, counter, ep, "lui", "t,u",
3794 AT, (int) BFD_RELOC_HI16_S);
3795 macro_build (p, counter, ep, "daddiu", "t,r,j",
3796 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3797 macro_build (p, counter, ep, "daddiu", "t,r,j",
3798 AT, AT, (int) BFD_RELOC_LO16);
3799 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3800 "d,w,<", reg, reg, 0);
3801 macro_build (p, counter, (expressionS *) NULL, "daddu",
3802 "d,v,t", reg, reg, AT);
3803 *used_at = 1;
3804 }
3805 else
3806 {
3807 macro_build (p, counter, ep, "lui", "t,u",
3808 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3809 macro_build (p, counter, ep, "daddiu", "t,r,j",
3810 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3811 macro_build (p, counter, (expressionS *) NULL, "dsll",
3812 "d,w,<", reg, reg, 16);
3813 macro_build (p, counter, ep, "daddiu", "t,r,j",
3814 reg, reg, (int) BFD_RELOC_HI16_S);
3815 macro_build (p, counter, (expressionS *) NULL, "dsll",
3816 "d,w,<", reg, reg, 16);
3817 macro_build (p, counter, ep, "daddiu", "t,r,j",
3818 reg, reg, (int) BFD_RELOC_LO16);
3819 }
3820 }
3821 else
3822 {
3823 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3824 && ! nopic_need_relax (ep->X_add_symbol, 1))
3825 {
3826 frag_grow (20);
3827 macro_build ((char *) NULL, counter, ep,
3828 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3829 reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3830 p = frag_var (rs_machine_dependent, 8, 0,
3831 RELAX_ENCODE (4, 8, 0, 4, 0,
3832 mips_opts.warn_about_macros),
3833 ep->X_add_symbol, 0, NULL);
3834 }
3835 macro_build_lui (p, counter, ep, reg);
3836 if (p != NULL)
3837 p += 4;
3838 macro_build (p, counter, ep,
3839 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3840 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3841 }
3842 }
3843 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3844 {
3845 expressionS ex;
3846
3847 /* If this is a reference to an external symbol, we want
3848 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3849 Otherwise we want
3850 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3851 nop
3852 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3853 If we have NewABI, we want
3854 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3855 If there is a constant, it must be added in after. */
3856 ex.X_add_number = ep->X_add_number;
3857 ep->X_add_number = 0;
3858 frag_grow (20);
3859 if (HAVE_NEWABI)
3860 {
3861 macro_build ((char *) NULL, counter, ep,
3862 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3863 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3864 }
3865 else
3866 {
3867 macro_build ((char *) NULL, counter, ep,
3868 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3869 reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3870 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3871 p = frag_var (rs_machine_dependent, 4, 0,
3872 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3873 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3874 macro_build (p, counter, ep,
3875 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3876 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3877 }
3878
3879 if (ex.X_add_number != 0)
3880 {
3881 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3882 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3883 ex.X_op = O_constant;
3884 macro_build ((char *) NULL, counter, &ex,
3885 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3886 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3887 }
3888 }
3889 else if (mips_pic == SVR4_PIC)
3890 {
3891 expressionS ex;
3892 int off;
3893
3894 /* This is the large GOT case. If this is a reference to an
3895 external symbol, we want
3896 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3897 addu $reg,$reg,$gp
3898 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3899 Otherwise, for a reference to a local symbol, we want
3900 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3901 nop
3902 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3903 If we have NewABI, we want
3904 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3905 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
3906 If there is a constant, it must be added in after. */
3907 ex.X_add_number = ep->X_add_number;
3908 ep->X_add_number = 0;
3909 if (HAVE_NEWABI)
3910 {
3911 macro_build ((char *) NULL, counter, ep,
3912 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3913 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3914 macro_build (p, counter, ep,
3915 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3916 reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3917 }
3918 else
3919 {
3920 if (reg_needs_delay (mips_gp_register))
3921 off = 4;
3922 else
3923 off = 0;
3924 frag_grow (32);
3925 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3926 (int) BFD_RELOC_MIPS_GOT_HI16);
3927 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3928 HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3929 reg, mips_gp_register);
3930 macro_build ((char *) NULL, counter, ep,
3931 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3932 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3933 p = frag_var (rs_machine_dependent, 12 + off, 0,
3934 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3935 mips_opts.warn_about_macros),
3936 ep->X_add_symbol, 0, NULL);
3937 if (off > 0)
3938 {
3939 /* We need a nop before loading from $gp. This special
3940 check is required because the lui which starts the main
3941 instruction stream does not refer to $gp, and so will not
3942 insert the nop which may be required. */
3943 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3944 p += 4;
3945 }
3946 macro_build (p, counter, ep,
3947 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3948 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3949 p += 4;
3950 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3951 p += 4;
3952 macro_build (p, counter, ep,
3953 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3954 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3955 }
3956
3957 if (ex.X_add_number != 0)
3958 {
3959 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3960 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3961 ex.X_op = O_constant;
3962 macro_build ((char *) NULL, counter, &ex,
3963 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3964 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3965 }
3966 }
3967 else if (mips_pic == EMBEDDED_PIC)
3968 {
3969 /* We always do
3970 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3971 */
3972 macro_build ((char *) NULL, counter, ep,
3973 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3974 "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3975 }
3976 else
3977 abort ();
3978 }
3979
3980 /* Move the contents of register SOURCE into register DEST. */
3981
3982 static void
3983 move_register (counter, dest, source)
3984 int *counter;
3985 int dest;
3986 int source;
3987 {
3988 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3989 HAVE_32BIT_GPRS ? "addu" : "daddu",
3990 "d,v,t", dest, source, 0);
3991 }
3992
3993 /*
3994 * Build macros
3995 * This routine implements the seemingly endless macro or synthesized
3996 * instructions and addressing modes in the mips assembly language. Many
3997 * of these macros are simple and are similar to each other. These could
3998 * probably be handled by some kind of table or grammer aproach instead of
3999 * this verbose method. Others are not simple macros but are more like
4000 * optimizing code generation.
4001 * One interesting optimization is when several store macros appear
4002 * consecutivly that would load AT with the upper half of the same address.
4003 * The ensuing load upper instructions are ommited. This implies some kind
4004 * of global optimization. We currently only optimize within a single macro.
4005 * For many of the load and store macros if the address is specified as a
4006 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4007 * first load register 'at' with zero and use it as the base register. The
4008 * mips assembler simply uses register $zero. Just one tiny optimization
4009 * we're missing.
4010 */
4011 static void
4012 macro (ip)
4013 struct mips_cl_insn *ip;
4014 {
4015 register int treg, sreg, dreg, breg;
4016 int tempreg;
4017 int mask;
4018 int icnt = 0;
4019 int used_at = 0;
4020 expressionS expr1;
4021 const char *s;
4022 const char *s2;
4023 const char *fmt;
4024 int likely = 0;
4025 int dbl = 0;
4026 int coproc = 0;
4027 int lr = 0;
4028 int imm = 0;
4029 offsetT maxnum;
4030 int off;
4031 bfd_reloc_code_real_type r;
4032 int hold_mips_optimize;
4033
4034 assert (! mips_opts.mips16);
4035
4036 treg = (ip->insn_opcode >> 16) & 0x1f;
4037 dreg = (ip->insn_opcode >> 11) & 0x1f;
4038 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4039 mask = ip->insn_mo->mask;
4040
4041 expr1.X_op = O_constant;
4042 expr1.X_op_symbol = NULL;
4043 expr1.X_add_symbol = NULL;
4044 expr1.X_add_number = 1;
4045
4046 switch (mask)
4047 {
4048 case M_DABS:
4049 dbl = 1;
4050 case M_ABS:
4051 /* bgez $a0,.+12
4052 move v0,$a0
4053 sub v0,$zero,$a0
4054 */
4055
4056 mips_emit_delays (true);
4057 ++mips_opts.noreorder;
4058 mips_any_noreorder = 1;
4059
4060 expr1.X_add_number = 8;
4061 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4062 if (dreg == sreg)
4063 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4064 0);
4065 else
4066 move_register (&icnt, dreg, sreg);
4067 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4068 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4069
4070 --mips_opts.noreorder;
4071 return;
4072
4073 case M_ADD_I:
4074 s = "addi";
4075 s2 = "add";
4076 goto do_addi;
4077 case M_ADDU_I:
4078 s = "addiu";
4079 s2 = "addu";
4080 goto do_addi;
4081 case M_DADD_I:
4082 dbl = 1;
4083 s = "daddi";
4084 s2 = "dadd";
4085 goto do_addi;
4086 case M_DADDU_I:
4087 dbl = 1;
4088 s = "daddiu";
4089 s2 = "daddu";
4090 do_addi:
4091 if (imm_expr.X_op == O_constant
4092 && imm_expr.X_add_number >= -0x8000
4093 && imm_expr.X_add_number < 0x8000)
4094 {
4095 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4096 (int) BFD_RELOC_LO16);
4097 return;
4098 }
4099 load_register (&icnt, AT, &imm_expr, dbl);
4100 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4101 treg, sreg, AT);
4102 break;
4103
4104 case M_AND_I:
4105 s = "andi";
4106 s2 = "and";
4107 goto do_bit;
4108 case M_OR_I:
4109 s = "ori";
4110 s2 = "or";
4111 goto do_bit;
4112 case M_NOR_I:
4113 s = "";
4114 s2 = "nor";
4115 goto do_bit;
4116 case M_XOR_I:
4117 s = "xori";
4118 s2 = "xor";
4119 do_bit:
4120 if (imm_expr.X_op == O_constant
4121 && imm_expr.X_add_number >= 0
4122 && imm_expr.X_add_number < 0x10000)
4123 {
4124 if (mask != M_NOR_I)
4125 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4126 sreg, (int) BFD_RELOC_LO16);
4127 else
4128 {
4129 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4130 treg, sreg, (int) BFD_RELOC_LO16);
4131 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4132 "d,v,t", treg, treg, 0);
4133 }
4134 return;
4135 }
4136
4137 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4138 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4139 treg, sreg, AT);
4140 break;
4141
4142 case M_BEQ_I:
4143 s = "beq";
4144 goto beq_i;
4145 case M_BEQL_I:
4146 s = "beql";
4147 likely = 1;
4148 goto beq_i;
4149 case M_BNE_I:
4150 s = "bne";
4151 goto beq_i;
4152 case M_BNEL_I:
4153 s = "bnel";
4154 likely = 1;
4155 beq_i:
4156 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4157 {
4158 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4159 0);
4160 return;
4161 }
4162 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4163 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4164 break;
4165
4166 case M_BGEL:
4167 likely = 1;
4168 case M_BGE:
4169 if (treg == 0)
4170 {
4171 macro_build ((char *) NULL, &icnt, &offset_expr,
4172 likely ? "bgezl" : "bgez", "s,p", sreg);
4173 return;
4174 }
4175 if (sreg == 0)
4176 {
4177 macro_build ((char *) NULL, &icnt, &offset_expr,
4178 likely ? "blezl" : "blez", "s,p", treg);
4179 return;
4180 }
4181 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4182 AT, sreg, treg);
4183 macro_build ((char *) NULL, &icnt, &offset_expr,
4184 likely ? "beql" : "beq", "s,t,p", AT, 0);
4185 break;
4186
4187 case M_BGTL_I:
4188 likely = 1;
4189 case M_BGT_I:
4190 /* check for > max integer */
4191 maxnum = 0x7fffffff;
4192 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4193 {
4194 maxnum <<= 16;
4195 maxnum |= 0xffff;
4196 maxnum <<= 16;
4197 maxnum |= 0xffff;
4198 }
4199 if (imm_expr.X_op == O_constant
4200 && imm_expr.X_add_number >= maxnum
4201 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4202 {
4203 do_false:
4204 /* result is always false */
4205 if (! likely)
4206 {
4207 if (warn_nops)
4208 as_warn (_("Branch %s is always false (nop)"),
4209 ip->insn_mo->name);
4210 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4211 "", 0);
4212 }
4213 else
4214 {
4215 if (warn_nops)
4216 as_warn (_("Branch likely %s is always false"),
4217 ip->insn_mo->name);
4218 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4219 "s,t,p", 0, 0);
4220 }
4221 return;
4222 }
4223 if (imm_expr.X_op != O_constant)
4224 as_bad (_("Unsupported large constant"));
4225 ++imm_expr.X_add_number;
4226 /* FALLTHROUGH */
4227 case M_BGE_I:
4228 case M_BGEL_I:
4229 if (mask == M_BGEL_I)
4230 likely = 1;
4231 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4232 {
4233 macro_build ((char *) NULL, &icnt, &offset_expr,
4234 likely ? "bgezl" : "bgez", "s,p", sreg);
4235 return;
4236 }
4237 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4238 {
4239 macro_build ((char *) NULL, &icnt, &offset_expr,
4240 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4241 return;
4242 }
4243 maxnum = 0x7fffffff;
4244 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4245 {
4246 maxnum <<= 16;
4247 maxnum |= 0xffff;
4248 maxnum <<= 16;
4249 maxnum |= 0xffff;
4250 }
4251 maxnum = - maxnum - 1;
4252 if (imm_expr.X_op == O_constant
4253 && imm_expr.X_add_number <= maxnum
4254 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4255 {
4256 do_true:
4257 /* result is always true */
4258 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4259 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4260 return;
4261 }
4262 set_at (&icnt, sreg, 0);
4263 macro_build ((char *) NULL, &icnt, &offset_expr,
4264 likely ? "beql" : "beq", "s,t,p", AT, 0);
4265 break;
4266
4267 case M_BGEUL:
4268 likely = 1;
4269 case M_BGEU:
4270 if (treg == 0)
4271 goto do_true;
4272 if (sreg == 0)
4273 {
4274 macro_build ((char *) NULL, &icnt, &offset_expr,
4275 likely ? "beql" : "beq", "s,t,p", 0, treg);
4276 return;
4277 }
4278 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4279 "d,v,t", AT, sreg, treg);
4280 macro_build ((char *) NULL, &icnt, &offset_expr,
4281 likely ? "beql" : "beq", "s,t,p", AT, 0);
4282 break;
4283
4284 case M_BGTUL_I:
4285 likely = 1;
4286 case M_BGTU_I:
4287 if (sreg == 0
4288 || (HAVE_32BIT_GPRS
4289 && imm_expr.X_op == O_constant
4290 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4291 goto do_false;
4292 if (imm_expr.X_op != O_constant)
4293 as_bad (_("Unsupported large constant"));
4294 ++imm_expr.X_add_number;
4295 /* FALLTHROUGH */
4296 case M_BGEU_I:
4297 case M_BGEUL_I:
4298 if (mask == M_BGEUL_I)
4299 likely = 1;
4300 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4301 goto do_true;
4302 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4303 {
4304 macro_build ((char *) NULL, &icnt, &offset_expr,
4305 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4306 return;
4307 }
4308 set_at (&icnt, sreg, 1);
4309 macro_build ((char *) NULL, &icnt, &offset_expr,
4310 likely ? "beql" : "beq", "s,t,p", AT, 0);
4311 break;
4312
4313 case M_BGTL:
4314 likely = 1;
4315 case M_BGT:
4316 if (treg == 0)
4317 {
4318 macro_build ((char *) NULL, &icnt, &offset_expr,
4319 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4320 return;
4321 }
4322 if (sreg == 0)
4323 {
4324 macro_build ((char *) NULL, &icnt, &offset_expr,
4325 likely ? "bltzl" : "bltz", "s,p", treg);
4326 return;
4327 }
4328 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4329 AT, treg, sreg);
4330 macro_build ((char *) NULL, &icnt, &offset_expr,
4331 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4332 break;
4333
4334 case M_BGTUL:
4335 likely = 1;
4336 case M_BGTU:
4337 if (treg == 0)
4338 {
4339 macro_build ((char *) NULL, &icnt, &offset_expr,
4340 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4341 return;
4342 }
4343 if (sreg == 0)
4344 goto do_false;
4345 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4346 "d,v,t", AT, treg, sreg);
4347 macro_build ((char *) NULL, &icnt, &offset_expr,
4348 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4349 break;
4350
4351 case M_BLEL:
4352 likely = 1;
4353 case M_BLE:
4354 if (treg == 0)
4355 {
4356 macro_build ((char *) NULL, &icnt, &offset_expr,
4357 likely ? "blezl" : "blez", "s,p", sreg);
4358 return;
4359 }
4360 if (sreg == 0)
4361 {
4362 macro_build ((char *) NULL, &icnt, &offset_expr,
4363 likely ? "bgezl" : "bgez", "s,p", treg);
4364 return;
4365 }
4366 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4367 AT, treg, sreg);
4368 macro_build ((char *) NULL, &icnt, &offset_expr,
4369 likely ? "beql" : "beq", "s,t,p", AT, 0);
4370 break;
4371
4372 case M_BLEL_I:
4373 likely = 1;
4374 case M_BLE_I:
4375 maxnum = 0x7fffffff;
4376 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4377 {
4378 maxnum <<= 16;
4379 maxnum |= 0xffff;
4380 maxnum <<= 16;
4381 maxnum |= 0xffff;
4382 }
4383 if (imm_expr.X_op == O_constant
4384 && imm_expr.X_add_number >= maxnum
4385 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4386 goto do_true;
4387 if (imm_expr.X_op != O_constant)
4388 as_bad (_("Unsupported large constant"));
4389 ++imm_expr.X_add_number;
4390 /* FALLTHROUGH */
4391 case M_BLT_I:
4392 case M_BLTL_I:
4393 if (mask == M_BLTL_I)
4394 likely = 1;
4395 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4396 {
4397 macro_build ((char *) NULL, &icnt, &offset_expr,
4398 likely ? "bltzl" : "bltz", "s,p", sreg);
4399 return;
4400 }
4401 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4402 {
4403 macro_build ((char *) NULL, &icnt, &offset_expr,
4404 likely ? "blezl" : "blez", "s,p", sreg);
4405 return;
4406 }
4407 set_at (&icnt, sreg, 0);
4408 macro_build ((char *) NULL, &icnt, &offset_expr,
4409 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4410 break;
4411
4412 case M_BLEUL:
4413 likely = 1;
4414 case M_BLEU:
4415 if (treg == 0)
4416 {
4417 macro_build ((char *) NULL, &icnt, &offset_expr,
4418 likely ? "beql" : "beq", "s,t,p", sreg, 0);
4419 return;
4420 }
4421 if (sreg == 0)
4422 goto do_true;
4423 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4424 "d,v,t", AT, treg, sreg);
4425 macro_build ((char *) NULL, &icnt, &offset_expr,
4426 likely ? "beql" : "beq", "s,t,p", AT, 0);
4427 break;
4428
4429 case M_BLEUL_I:
4430 likely = 1;
4431 case M_BLEU_I:
4432 if (sreg == 0
4433 || (HAVE_32BIT_GPRS
4434 && imm_expr.X_op == O_constant
4435 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4436 goto do_true;
4437 if (imm_expr.X_op != O_constant)
4438 as_bad (_("Unsupported large constant"));
4439 ++imm_expr.X_add_number;
4440 /* FALLTHROUGH */
4441 case M_BLTU_I:
4442 case M_BLTUL_I:
4443 if (mask == M_BLTUL_I)
4444 likely = 1;
4445 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4446 goto do_false;
4447 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4448 {
4449 macro_build ((char *) NULL, &icnt, &offset_expr,
4450 likely ? "beql" : "beq",
4451 "s,t,p", sreg, 0);
4452 return;
4453 }
4454 set_at (&icnt, sreg, 1);
4455 macro_build ((char *) NULL, &icnt, &offset_expr,
4456 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4457 break;
4458
4459 case M_BLTL:
4460 likely = 1;
4461 case M_BLT:
4462 if (treg == 0)
4463 {
4464 macro_build ((char *) NULL, &icnt, &offset_expr,
4465 likely ? "bltzl" : "bltz", "s,p", sreg);
4466 return;
4467 }
4468 if (sreg == 0)
4469 {
4470 macro_build ((char *) NULL, &icnt, &offset_expr,
4471 likely ? "bgtzl" : "bgtz", "s,p", treg);
4472 return;
4473 }
4474 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4475 AT, sreg, treg);
4476 macro_build ((char *) NULL, &icnt, &offset_expr,
4477 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4478 break;
4479
4480 case M_BLTUL:
4481 likely = 1;
4482 case M_BLTU:
4483 if (treg == 0)
4484 goto do_false;
4485 if (sreg == 0)
4486 {
4487 macro_build ((char *) NULL, &icnt, &offset_expr,
4488 likely ? "bnel" : "bne", "s,t,p", 0, treg);
4489 return;
4490 }
4491 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4492 "d,v,t", AT, sreg,
4493 treg);
4494 macro_build ((char *) NULL, &icnt, &offset_expr,
4495 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4496 break;
4497
4498 case M_DDIV_3:
4499 dbl = 1;
4500 case M_DIV_3:
4501 s = "mflo";
4502 goto do_div3;
4503 case M_DREM_3:
4504 dbl = 1;
4505 case M_REM_3:
4506 s = "mfhi";
4507 do_div3:
4508 if (treg == 0)
4509 {
4510 as_warn (_("Divide by zero."));
4511 if (mips_trap)
4512 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4513 "s,t,q", 0, 0, 7);
4514 else
4515 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4516 "c", 7);
4517 return;
4518 }
4519
4520 mips_emit_delays (true);
4521 ++mips_opts.noreorder;
4522 mips_any_noreorder = 1;
4523 if (mips_trap)
4524 {
4525 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4526 "s,t,q", treg, 0, 7);
4527 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4528 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4529 }
4530 else
4531 {
4532 expr1.X_add_number = 8;
4533 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4534 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4535 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4536 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4537 "c", 7);
4538 }
4539 expr1.X_add_number = -1;
4540 macro_build ((char *) NULL, &icnt, &expr1,
4541 dbl ? "daddiu" : "addiu",
4542 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4543 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4544 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4545 if (dbl)
4546 {
4547 expr1.X_add_number = 1;
4548 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4549 (int) BFD_RELOC_LO16);
4550 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4551 "d,w,<", AT, AT, 31);
4552 }
4553 else
4554 {
4555 expr1.X_add_number = 0x80000000;
4556 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4557 (int) BFD_RELOC_HI16);
4558 }
4559 if (mips_trap)
4560 {
4561 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4562 "s,t,q", sreg, AT, 6);
4563 /* We want to close the noreorder block as soon as possible, so
4564 that later insns are available for delay slot filling. */
4565 --mips_opts.noreorder;
4566 }
4567 else
4568 {
4569 expr1.X_add_number = 8;
4570 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4571 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4572 0);
4573
4574 /* We want to close the noreorder block as soon as possible, so
4575 that later insns are available for delay slot filling. */
4576 --mips_opts.noreorder;
4577
4578 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4579 "c", 6);
4580 }
4581 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4582 break;
4583
4584 case M_DIV_3I:
4585 s = "div";
4586 s2 = "mflo";
4587 goto do_divi;
4588 case M_DIVU_3I:
4589 s = "divu";
4590 s2 = "mflo";
4591 goto do_divi;
4592 case M_REM_3I:
4593 s = "div";
4594 s2 = "mfhi";
4595 goto do_divi;
4596 case M_REMU_3I:
4597 s = "divu";
4598 s2 = "mfhi";
4599 goto do_divi;
4600 case M_DDIV_3I:
4601 dbl = 1;
4602 s = "ddiv";
4603 s2 = "mflo";
4604 goto do_divi;
4605 case M_DDIVU_3I:
4606 dbl = 1;
4607 s = "ddivu";
4608 s2 = "mflo";
4609 goto do_divi;
4610 case M_DREM_3I:
4611 dbl = 1;
4612 s = "ddiv";
4613 s2 = "mfhi";
4614 goto do_divi;
4615 case M_DREMU_3I:
4616 dbl = 1;
4617 s = "ddivu";
4618 s2 = "mfhi";
4619 do_divi:
4620 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4621 {
4622 as_warn (_("Divide by zero."));
4623 if (mips_trap)
4624 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4625 "s,t,q", 0, 0, 7);
4626 else
4627 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4628 "c", 7);
4629 return;
4630 }
4631 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4632 {
4633 if (strcmp (s2, "mflo") == 0)
4634 move_register (&icnt, dreg, sreg);
4635 else
4636 move_register (&icnt, dreg, 0);
4637 return;
4638 }
4639 if (imm_expr.X_op == O_constant
4640 && imm_expr.X_add_number == -1
4641 && s[strlen (s) - 1] != 'u')
4642 {
4643 if (strcmp (s2, "mflo") == 0)
4644 {
4645 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4646 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4647 }
4648 else
4649 move_register (&icnt, dreg, 0);
4650 return;
4651 }
4652
4653 load_register (&icnt, AT, &imm_expr, dbl);
4654 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4655 sreg, AT);
4656 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4657 break;
4658
4659 case M_DIVU_3:
4660 s = "divu";
4661 s2 = "mflo";
4662 goto do_divu3;
4663 case M_REMU_3:
4664 s = "divu";
4665 s2 = "mfhi";
4666 goto do_divu3;
4667 case M_DDIVU_3:
4668 s = "ddivu";
4669 s2 = "mflo";
4670 goto do_divu3;
4671 case M_DREMU_3:
4672 s = "ddivu";
4673 s2 = "mfhi";
4674 do_divu3:
4675 mips_emit_delays (true);
4676 ++mips_opts.noreorder;
4677 mips_any_noreorder = 1;
4678 if (mips_trap)
4679 {
4680 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4681 "s,t,q", treg, 0, 7);
4682 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4683 sreg, treg);
4684 /* We want to close the noreorder block as soon as possible, so
4685 that later insns are available for delay slot filling. */
4686 --mips_opts.noreorder;
4687 }
4688 else
4689 {
4690 expr1.X_add_number = 8;
4691 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4692 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4693 sreg, treg);
4694
4695 /* We want to close the noreorder block as soon as possible, so
4696 that later insns are available for delay slot filling. */
4697 --mips_opts.noreorder;
4698 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4699 "c", 7);
4700 }
4701 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4702 return;
4703
4704 case M_DLA_AB:
4705 dbl = 1;
4706 case M_LA_AB:
4707 /* Load the address of a symbol into a register. If breg is not
4708 zero, we then add a base register to it. */
4709
4710 if (dbl && HAVE_32BIT_GPRS)
4711 as_warn (_("dla used to load 32-bit register"));
4712
4713 if (! dbl && HAVE_64BIT_OBJECTS)
4714 as_warn (_("la used to load 64-bit address"));
4715
4716 if (offset_expr.X_op == O_constant
4717 && offset_expr.X_add_number >= -0x8000
4718 && offset_expr.X_add_number < 0x8000)
4719 {
4720 macro_build ((char *) NULL, &icnt, &offset_expr,
4721 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4722 "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4723 return;
4724 }
4725
4726 if (treg == breg)
4727 {
4728 tempreg = AT;
4729 used_at = 1;
4730 }
4731 else
4732 {
4733 tempreg = treg;
4734 used_at = 0;
4735 }
4736
4737 /* When generating embedded PIC code, we permit expressions of
4738 the form
4739 la $treg,foo-bar
4740 la $treg,foo-bar($breg)
4741 where bar is an address in the current section. These are used
4742 when getting the addresses of functions. We don't permit
4743 X_add_number to be non-zero, because if the symbol is
4744 external the relaxing code needs to know that any addend is
4745 purely the offset to X_op_symbol. */
4746 if (mips_pic == EMBEDDED_PIC
4747 && offset_expr.X_op == O_subtract
4748 && (symbol_constant_p (offset_expr.X_op_symbol)
4749 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4750 : (symbol_equated_p (offset_expr.X_op_symbol)
4751 && (S_GET_SEGMENT
4752 (symbol_get_value_expression (offset_expr.X_op_symbol)
4753 ->X_add_symbol)
4754 == now_seg)))
4755 && (offset_expr.X_add_number == 0
4756 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4757 {
4758 if (breg == 0)
4759 {
4760 tempreg = treg;
4761 used_at = 0;
4762 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4763 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4764 }
4765 else
4766 {
4767 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4768 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4769 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4770 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4771 "d,v,t", tempreg, tempreg, breg);
4772 }
4773 macro_build ((char *) NULL, &icnt, &offset_expr,
4774 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4775 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4776 if (! used_at)
4777 return;
4778 break;
4779 }
4780
4781 if (offset_expr.X_op != O_symbol
4782 && offset_expr.X_op != O_constant)
4783 {
4784 as_bad (_("expression too complex"));
4785 offset_expr.X_op = O_constant;
4786 }
4787
4788 if (offset_expr.X_op == O_constant)
4789 load_register (&icnt, tempreg, &offset_expr,
4790 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4791 ? (dbl || HAVE_64BIT_ADDRESSES)
4792 : HAVE_64BIT_ADDRESSES));
4793 else if (mips_pic == NO_PIC)
4794 {
4795 /* If this is a reference to a GP relative symbol, we want
4796 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4797 Otherwise we want
4798 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4799 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4800 If we have a constant, we need two instructions anyhow,
4801 so we may as well always use the latter form.
4802
4803 With 64bit address space and a usable $at we want
4804 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4805 lui $at,<sym> (BFD_RELOC_HI16_S)
4806 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4807 daddiu $at,<sym> (BFD_RELOC_LO16)
4808 dsll32 $tempreg,0
4809 daddu $tempreg,$tempreg,$at
4810
4811 If $at is already in use, we use an path which is suboptimal
4812 on superscalar processors.
4813 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4814 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4815 dsll $tempreg,16
4816 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4817 dsll $tempreg,16
4818 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4819 */
4820 char *p = NULL;
4821 if (HAVE_64BIT_ADDRESSES)
4822 {
4823 /* We don't do GP optimization for now because RELAX_ENCODE can't
4824 hold the data for such large chunks. */
4825
4826 if (used_at == 0 && ! mips_opts.noat)
4827 {
4828 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4829 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4830 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4831 AT, (int) BFD_RELOC_HI16_S);
4832 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4833 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4834 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4835 AT, AT, (int) BFD_RELOC_LO16);
4836 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4837 "d,w,<", tempreg, tempreg, 0);
4838 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4839 "d,v,t", tempreg, tempreg, AT);
4840 used_at = 1;
4841 }
4842 else
4843 {
4844 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4845 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4846 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4847 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4848 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4849 tempreg, tempreg, 16);
4850 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4851 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4852 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4853 tempreg, tempreg, 16);
4854 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4855 tempreg, tempreg, (int) BFD_RELOC_LO16);
4856 }
4857 }
4858 else
4859 {
4860 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4861 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4862 {
4863 frag_grow (20);
4864 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4865 "t,r,j", tempreg, mips_gp_register,
4866 (int) BFD_RELOC_GPREL16);
4867 p = frag_var (rs_machine_dependent, 8, 0,
4868 RELAX_ENCODE (4, 8, 0, 4, 0,
4869 mips_opts.warn_about_macros),
4870 offset_expr.X_add_symbol, 0, NULL);
4871 }
4872 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4873 if (p != NULL)
4874 p += 4;
4875 macro_build (p, &icnt, &offset_expr, "addiu",
4876 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4877 }
4878 }
4879 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4880 {
4881 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4882
4883 /* If this is a reference to an external symbol, and there
4884 is no constant, we want
4885 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4886 or if tempreg is PIC_CALL_REG
4887 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4888 For a local symbol, we want
4889 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4890 nop
4891 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4892
4893 If we have a small constant, and this is a reference to
4894 an external symbol, we want
4895 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4896 nop
4897 addiu $tempreg,$tempreg,<constant>
4898 For a local symbol, we want the same instruction
4899 sequence, but we output a BFD_RELOC_LO16 reloc on the
4900 addiu instruction.
4901
4902 If we have a large constant, and this is a reference to
4903 an external symbol, we want
4904 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4905 lui $at,<hiconstant>
4906 addiu $at,$at,<loconstant>
4907 addu $tempreg,$tempreg,$at
4908 For a local symbol, we want the same instruction
4909 sequence, but we output a BFD_RELOC_LO16 reloc on the
4910 addiu instruction.
4911
4912 For NewABI, we want for local or external data addresses
4913 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4914 For a local function symbol, we want
4915 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4916 nop
4917 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4918 */
4919
4920 expr1.X_add_number = offset_expr.X_add_number;
4921 offset_expr.X_add_number = 0;
4922 frag_grow (32);
4923 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4924 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4925 else if (HAVE_NEWABI)
4926 lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4927 macro_build ((char *) NULL, &icnt, &offset_expr,
4928 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4929 "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4930 if (expr1.X_add_number == 0)
4931 {
4932 int off;
4933 char *p;
4934
4935 if (breg == 0)
4936 off = 0;
4937 else
4938 {
4939 /* We're going to put in an addu instruction using
4940 tempreg, so we may as well insert the nop right
4941 now. */
4942 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4943 "nop", "");
4944 off = 4;
4945 }
4946 p = frag_var (rs_machine_dependent, 8 - off, 0,
4947 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4948 (breg == 0
4949 ? mips_opts.warn_about_macros
4950 : 0)),
4951 offset_expr.X_add_symbol, 0, NULL);
4952 if (breg == 0)
4953 {
4954 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4955 p += 4;
4956 }
4957 macro_build (p, &icnt, &expr1,
4958 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4959 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4960 /* FIXME: If breg == 0, and the next instruction uses
4961 $tempreg, then if this variant case is used an extra
4962 nop will be generated. */
4963 }
4964 else if (expr1.X_add_number >= -0x8000
4965 && expr1.X_add_number < 0x8000)
4966 {
4967 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4968 "nop", "");
4969 macro_build ((char *) NULL, &icnt, &expr1,
4970 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4971 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4972 frag_var (rs_machine_dependent, 0, 0,
4973 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4974 offset_expr.X_add_symbol, 0, NULL);
4975 }
4976 else
4977 {
4978 int off1;
4979
4980 /* If we are going to add in a base register, and the
4981 target register and the base register are the same,
4982 then we are using AT as a temporary register. Since
4983 we want to load the constant into AT, we add our
4984 current AT (from the global offset table) and the
4985 register into the register now, and pretend we were
4986 not using a base register. */
4987 if (breg != treg)
4988 off1 = 0;
4989 else
4990 {
4991 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4992 "nop", "");
4993 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4994 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4995 "d,v,t", treg, AT, breg);
4996 breg = 0;
4997 tempreg = treg;
4998 off1 = -8;
4999 }
5000
5001 /* Set mips_optimize around the lui instruction to avoid
5002 inserting an unnecessary nop after the lw. */
5003 hold_mips_optimize = mips_optimize;
5004 mips_optimize = 2;
5005 macro_build_lui (NULL, &icnt, &expr1, AT);
5006 mips_optimize = hold_mips_optimize;
5007
5008 macro_build ((char *) NULL, &icnt, &expr1,
5009 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5010 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5011 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5012 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5013 "d,v,t", tempreg, tempreg, AT);
5014 frag_var (rs_machine_dependent, 0, 0,
5015 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5016 offset_expr.X_add_symbol, 0, NULL);
5017 used_at = 1;
5018 }
5019 }
5020 else if (mips_pic == SVR4_PIC)
5021 {
5022 int gpdel;
5023 char *p;
5024 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5025 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5026 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5027
5028 /* This is the large GOT case. If this is a reference to an
5029 external symbol, and there is no constant, we want
5030 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5031 addu $tempreg,$tempreg,$gp
5032 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5033 or if tempreg is PIC_CALL_REG
5034 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5035 addu $tempreg,$tempreg,$gp
5036 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5037 For a local symbol, we want
5038 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5039 nop
5040 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5041
5042 If we have a small constant, and this is a reference to
5043 an external symbol, we want
5044 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5045 addu $tempreg,$tempreg,$gp
5046 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5047 nop
5048 addiu $tempreg,$tempreg,<constant>
5049 For a local symbol, we want
5050 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5051 nop
5052 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5053
5054 If we have a large constant, and this is a reference to
5055 an external symbol, we want
5056 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5057 addu $tempreg,$tempreg,$gp
5058 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5059 lui $at,<hiconstant>
5060 addiu $at,$at,<loconstant>
5061 addu $tempreg,$tempreg,$at
5062 For a local symbol, we want
5063 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5064 lui $at,<hiconstant>
5065 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5066 addu $tempreg,$tempreg,$at
5067
5068 For NewABI, we want for local data addresses
5069 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5070 */
5071
5072 expr1.X_add_number = offset_expr.X_add_number;
5073 offset_expr.X_add_number = 0;
5074 frag_grow (52);
5075 if (reg_needs_delay (mips_gp_register))
5076 gpdel = 4;
5077 else
5078 gpdel = 0;
5079 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5080 {
5081 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5082 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5083 }
5084 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5085 tempreg, lui_reloc_type);
5086 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5087 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5088 "d,v,t", tempreg, tempreg, mips_gp_register);
5089 macro_build ((char *) NULL, &icnt, &offset_expr,
5090 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5091 "t,o(b)", tempreg, lw_reloc_type, tempreg);
5092 if (expr1.X_add_number == 0)
5093 {
5094 int off;
5095
5096 if (breg == 0)
5097 off = 0;
5098 else
5099 {
5100 /* We're going to put in an addu instruction using
5101 tempreg, so we may as well insert the nop right
5102 now. */
5103 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5104 "nop", "");
5105 off = 4;
5106 }
5107
5108 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5109 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5110 8 + gpdel, 0,
5111 (breg == 0
5112 ? mips_opts.warn_about_macros
5113 : 0)),
5114 offset_expr.X_add_symbol, 0, NULL);
5115 }
5116 else if (expr1.X_add_number >= -0x8000
5117 && expr1.X_add_number < 0x8000)
5118 {
5119 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5120 "nop", "");
5121 macro_build ((char *) NULL, &icnt, &expr1,
5122 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5123 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5124
5125 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5126 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5127 (breg == 0
5128 ? mips_opts.warn_about_macros
5129 : 0)),
5130 offset_expr.X_add_symbol, 0, NULL);
5131 }
5132 else
5133 {
5134 int adj, dreg;
5135
5136 /* If we are going to add in a base register, and the
5137 target register and the base register are the same,
5138 then we are using AT as a temporary register. Since
5139 we want to load the constant into AT, we add our
5140 current AT (from the global offset table) and the
5141 register into the register now, and pretend we were
5142 not using a base register. */
5143 if (breg != treg)
5144 {
5145 adj = 0;
5146 dreg = tempreg;
5147 }
5148 else
5149 {
5150 assert (tempreg == AT);
5151 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5152 "nop", "");
5153 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5154 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5155 "d,v,t", treg, AT, breg);
5156 dreg = treg;
5157 adj = 8;
5158 }
5159
5160 /* Set mips_optimize around the lui instruction to avoid
5161 inserting an unnecessary nop after the lw. */
5162 hold_mips_optimize = mips_optimize;
5163 mips_optimize = 2;
5164 macro_build_lui (NULL, &icnt, &expr1, AT);
5165 mips_optimize = hold_mips_optimize;
5166
5167 macro_build ((char *) NULL, &icnt, &expr1,
5168 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5169 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5170 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5171 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5172 "d,v,t", dreg, dreg, AT);
5173
5174 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5175 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5176 8 + gpdel, 0,
5177 (breg == 0
5178 ? mips_opts.warn_about_macros
5179 : 0)),
5180 offset_expr.X_add_symbol, 0, NULL);
5181
5182 used_at = 1;
5183 }
5184
5185 if (gpdel > 0)
5186 {
5187 /* This is needed because this instruction uses $gp, but
5188 the first instruction on the main stream does not. */
5189 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5190 p += 4;
5191 }
5192
5193 if (HAVE_NEWABI)
5194 local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5195 macro_build (p, &icnt, &offset_expr,
5196 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5197 "t,o(b)", tempreg,
5198 local_reloc_type,
5199 mips_gp_register);
5200 p += 4;
5201 if (expr1.X_add_number == 0 && HAVE_NEWABI)
5202 {
5203 /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
5204 }
5205 else
5206 if (expr1.X_add_number >= -0x8000
5207 && expr1.X_add_number < 0x8000)
5208 {
5209 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5210 p += 4;
5211 macro_build (p, &icnt, &expr1,
5212 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5213 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5214 /* FIXME: If add_number is 0, and there was no base
5215 register, the external symbol case ended with a load,
5216 so if the symbol turns out to not be external, and
5217 the next instruction uses tempreg, an unnecessary nop
5218 will be inserted. */
5219 }
5220 else
5221 {
5222 if (breg == treg)
5223 {
5224 /* We must add in the base register now, as in the
5225 external symbol case. */
5226 assert (tempreg == AT);
5227 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5228 p += 4;
5229 macro_build (p, &icnt, (expressionS *) NULL,
5230 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5231 "d,v,t", treg, AT, breg);
5232 p += 4;
5233 tempreg = treg;
5234 /* We set breg to 0 because we have arranged to add
5235 it in in both cases. */
5236 breg = 0;
5237 }
5238
5239 macro_build_lui (p, &icnt, &expr1, AT);
5240 p += 4;
5241 macro_build (p, &icnt, &expr1,
5242 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5243 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5244 p += 4;
5245 macro_build (p, &icnt, (expressionS *) NULL,
5246 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5247 "d,v,t", tempreg, tempreg, AT);
5248 p += 4;
5249 }
5250 }
5251 else if (mips_pic == EMBEDDED_PIC)
5252 {
5253 /* We use
5254 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
5255 */
5256 macro_build ((char *) NULL, &icnt, &offset_expr,
5257 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5258 tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5259 }
5260 else
5261 abort ();
5262
5263 if (breg != 0)
5264 {
5265 char *s;
5266
5267 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5268 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5269 else
5270 s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5271
5272 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5273 "d,v,t", treg, tempreg, breg);
5274 }
5275
5276 if (! used_at)
5277 return;
5278
5279 break;
5280
5281 case M_J_A:
5282 /* The j instruction may not be used in PIC code, since it
5283 requires an absolute address. We convert it to a b
5284 instruction. */
5285 if (mips_pic == NO_PIC)
5286 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5287 else
5288 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5289 return;
5290
5291 /* The jal instructions must be handled as macros because when
5292 generating PIC code they expand to multi-instruction
5293 sequences. Normally they are simple instructions. */
5294 case M_JAL_1:
5295 dreg = RA;
5296 /* Fall through. */
5297 case M_JAL_2:
5298 if (mips_pic == NO_PIC
5299 || mips_pic == EMBEDDED_PIC)
5300 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5301 "d,s", dreg, sreg);
5302 else if (mips_pic == SVR4_PIC)
5303 {
5304 if (sreg != PIC_CALL_REG)
5305 as_warn (_("MIPS PIC call to register other than $25"));
5306
5307 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5308 "d,s", dreg, sreg);
5309 if (! HAVE_NEWABI)
5310 {
5311 if (mips_cprestore_offset < 0)
5312 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5313 else
5314 {
5315 if (! mips_frame_reg_valid)
5316 {
5317 as_warn (_("No .frame pseudo-op used in PIC code"));
5318 /* Quiet this warning. */
5319 mips_frame_reg_valid = 1;
5320 }
5321 if (! mips_cprestore_valid)
5322 {
5323 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5324 /* Quiet this warning. */
5325 mips_cprestore_valid = 1;
5326 }
5327 expr1.X_add_number = mips_cprestore_offset;
5328 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5329 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5330 mips_gp_register, mips_frame_reg);
5331 }
5332 }
5333 }
5334 else
5335 abort ();
5336
5337 return;
5338
5339 case M_JAL_A:
5340 if (mips_pic == NO_PIC)
5341 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5342 else if (mips_pic == SVR4_PIC)
5343 {
5344 char *p;
5345
5346 /* If this is a reference to an external symbol, and we are
5347 using a small GOT, we want
5348 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5349 nop
5350 jalr $ra,$25
5351 nop
5352 lw $gp,cprestore($sp)
5353 The cprestore value is set using the .cprestore
5354 pseudo-op. If we are using a big GOT, we want
5355 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5356 addu $25,$25,$gp
5357 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5358 nop
5359 jalr $ra,$25
5360 nop
5361 lw $gp,cprestore($sp)
5362 If the symbol is not external, we want
5363 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5364 nop
5365 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5366 jalr $ra,$25
5367 nop
5368 lw $gp,cprestore($sp)
5369 For NewABI, we want
5370 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5371 jalr $ra,$25 (BFD_RELOC_MIPS_JALR)
5372 */
5373 if (HAVE_NEWABI)
5374 {
5375 macro_build ((char *) NULL, &icnt, &offset_expr,
5376 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5377 "t,o(b)", PIC_CALL_REG,
5378 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5379 macro_build_jalr (icnt, &offset_expr);
5380 }
5381 else
5382 {
5383 frag_grow (40);
5384 if (! mips_big_got)
5385 {
5386 macro_build ((char *) NULL, &icnt, &offset_expr,
5387 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5388 "t,o(b)", PIC_CALL_REG,
5389 (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5390 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5391 "nop", "");
5392 p = frag_var (rs_machine_dependent, 4, 0,
5393 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5394 offset_expr.X_add_symbol, 0, NULL);
5395 }
5396 else
5397 {
5398 int gpdel;
5399
5400 if (reg_needs_delay (mips_gp_register))
5401 gpdel = 4;
5402 else
5403 gpdel = 0;
5404 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5405 "t,u", PIC_CALL_REG,
5406 (int) BFD_RELOC_MIPS_CALL_HI16);
5407 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5408 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5409 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5410 mips_gp_register);
5411 macro_build ((char *) NULL, &icnt, &offset_expr,
5412 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5413 "t,o(b)", PIC_CALL_REG,
5414 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5415 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5416 "nop", "");
5417 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5418 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5419 8 + gpdel, 0, 0),
5420 offset_expr.X_add_symbol, 0, NULL);
5421 if (gpdel > 0)
5422 {
5423 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5424 p += 4;
5425 }
5426 macro_build (p, &icnt, &offset_expr,
5427 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5428 "t,o(b)", PIC_CALL_REG,
5429 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5430 p += 4;
5431 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5432 p += 4;
5433 }
5434 macro_build (p, &icnt, &offset_expr,
5435 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5436 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5437 (int) BFD_RELOC_LO16);
5438 macro_build_jalr (icnt, &offset_expr);
5439
5440 if (mips_cprestore_offset < 0)
5441 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5442 else
5443 {
5444 if (! mips_frame_reg_valid)
5445 {
5446 as_warn (_("No .frame pseudo-op used in PIC code"));
5447 /* Quiet this warning. */
5448 mips_frame_reg_valid = 1;
5449 }
5450 if (! mips_cprestore_valid)
5451 {
5452 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5453 /* Quiet this warning. */
5454 mips_cprestore_valid = 1;
5455 }
5456 if (mips_opts.noreorder)
5457 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5458 "nop", "");
5459 expr1.X_add_number = mips_cprestore_offset;
5460 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5461 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5462 mips_gp_register, mips_frame_reg);
5463 }
5464 }
5465 }
5466 else if (mips_pic == EMBEDDED_PIC)
5467 {
5468 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5469 /* The linker may expand the call to a longer sequence which
5470 uses $at, so we must break rather than return. */
5471 break;
5472 }
5473 else
5474 abort ();
5475
5476 return;
5477
5478 case M_LB_AB:
5479 s = "lb";
5480 goto ld;
5481 case M_LBU_AB:
5482 s = "lbu";
5483 goto ld;
5484 case M_LH_AB:
5485 s = "lh";
5486 goto ld;
5487 case M_LHU_AB:
5488 s = "lhu";
5489 goto ld;
5490 case M_LW_AB:
5491 s = "lw";
5492 goto ld;
5493 case M_LWC0_AB:
5494 s = "lwc0";
5495 /* Itbl support may require additional care here. */
5496 coproc = 1;
5497 goto ld;
5498 case M_LWC1_AB:
5499 s = "lwc1";
5500 /* Itbl support may require additional care here. */
5501 coproc = 1;
5502 goto ld;
5503 case M_LWC2_AB:
5504 s = "lwc2";
5505 /* Itbl support may require additional care here. */
5506 coproc = 1;
5507 goto ld;
5508 case M_LWC3_AB:
5509 s = "lwc3";
5510 /* Itbl support may require additional care here. */
5511 coproc = 1;
5512 goto ld;
5513 case M_LWL_AB:
5514 s = "lwl";
5515 lr = 1;
5516 goto ld;
5517 case M_LWR_AB:
5518 s = "lwr";
5519 lr = 1;
5520 goto ld;
5521 case M_LDC1_AB:
5522 if (mips_arch == CPU_R4650)
5523 {
5524 as_bad (_("opcode not supported on this processor"));
5525 return;
5526 }
5527 s = "ldc1";
5528 /* Itbl support may require additional care here. */
5529 coproc = 1;
5530 goto ld;
5531 case M_LDC2_AB:
5532 s = "ldc2";
5533 /* Itbl support may require additional care here. */
5534 coproc = 1;
5535 goto ld;
5536 case M_LDC3_AB:
5537 s = "ldc3";
5538 /* Itbl support may require additional care here. */
5539 coproc = 1;
5540 goto ld;
5541 case M_LDL_AB:
5542 s = "ldl";
5543 lr = 1;
5544 goto ld;
5545 case M_LDR_AB:
5546 s = "ldr";
5547 lr = 1;
5548 goto ld;
5549 case M_LL_AB:
5550 s = "ll";
5551 goto ld;
5552 case M_LLD_AB:
5553 s = "lld";
5554 goto ld;
5555 case M_LWU_AB:
5556 s = "lwu";
5557 ld:
5558 if (breg == treg || coproc || lr)
5559 {
5560 tempreg = AT;
5561 used_at = 1;
5562 }
5563 else
5564 {
5565 tempreg = treg;
5566 used_at = 0;
5567 }
5568 goto ld_st;
5569 case M_SB_AB:
5570 s = "sb";
5571 goto st;
5572 case M_SH_AB:
5573 s = "sh";
5574 goto st;
5575 case M_SW_AB:
5576 s = "sw";
5577 goto st;
5578 case M_SWC0_AB:
5579 s = "swc0";
5580 /* Itbl support may require additional care here. */
5581 coproc = 1;
5582 goto st;
5583 case M_SWC1_AB:
5584 s = "swc1";
5585 /* Itbl support may require additional care here. */
5586 coproc = 1;
5587 goto st;
5588 case M_SWC2_AB:
5589 s = "swc2";
5590 /* Itbl support may require additional care here. */
5591 coproc = 1;
5592 goto st;
5593 case M_SWC3_AB:
5594 s = "swc3";
5595 /* Itbl support may require additional care here. */
5596 coproc = 1;
5597 goto st;
5598 case M_SWL_AB:
5599 s = "swl";
5600 goto st;
5601 case M_SWR_AB:
5602 s = "swr";
5603 goto st;
5604 case M_SC_AB:
5605 s = "sc";
5606 goto st;
5607 case M_SCD_AB:
5608 s = "scd";
5609 goto st;
5610 case M_SDC1_AB:
5611 if (mips_arch == CPU_R4650)
5612 {
5613 as_bad (_("opcode not supported on this processor"));
5614 return;
5615 }
5616 s = "sdc1";
5617 coproc = 1;
5618 /* Itbl support may require additional care here. */
5619 goto st;
5620 case M_SDC2_AB:
5621 s = "sdc2";
5622 /* Itbl support may require additional care here. */
5623 coproc = 1;
5624 goto st;
5625 case M_SDC3_AB:
5626 s = "sdc3";
5627 /* Itbl support may require additional care here. */
5628 coproc = 1;
5629 goto st;
5630 case M_SDL_AB:
5631 s = "sdl";
5632 goto st;
5633 case M_SDR_AB:
5634 s = "sdr";
5635 st:
5636 tempreg = AT;
5637 used_at = 1;
5638 ld_st:
5639 /* Itbl support may require additional care here. */
5640 if (mask == M_LWC1_AB
5641 || mask == M_SWC1_AB
5642 || mask == M_LDC1_AB
5643 || mask == M_SDC1_AB
5644 || mask == M_L_DAB
5645 || mask == M_S_DAB)
5646 fmt = "T,o(b)";
5647 else if (coproc)
5648 fmt = "E,o(b)";
5649 else
5650 fmt = "t,o(b)";
5651
5652 /* For embedded PIC, we allow loads where the offset is calculated
5653 by subtracting a symbol in the current segment from an unknown
5654 symbol, relative to a base register, e.g.:
5655 <op> $treg, <sym>-<localsym>($breg)
5656 This is used by the compiler for switch statements. */
5657 if (mips_pic == EMBEDDED_PIC
5658 && offset_expr.X_op == O_subtract
5659 && (symbol_constant_p (offset_expr.X_op_symbol)
5660 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5661 : (symbol_equated_p (offset_expr.X_op_symbol)
5662 && (S_GET_SEGMENT
5663 (symbol_get_value_expression (offset_expr.X_op_symbol)
5664 ->X_add_symbol)
5665 == now_seg)))
5666 && breg != 0
5667 && (offset_expr.X_add_number == 0
5668 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5669 {
5670 /* For this case, we output the instructions:
5671 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5672 addiu $tempreg,$tempreg,$breg
5673 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5674 If the relocation would fit entirely in 16 bits, it would be
5675 nice to emit:
5676 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5677 instead, but that seems quite difficult. */
5678 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5679 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5680 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5681 ((bfd_arch_bits_per_address (stdoutput) == 32
5682 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5683 ? "addu" : "daddu"),
5684 "d,v,t", tempreg, tempreg, breg);
5685 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5686 (int) BFD_RELOC_PCREL_LO16, tempreg);
5687 if (! used_at)
5688 return;
5689 break;
5690 }
5691
5692 if (offset_expr.X_op != O_constant
5693 && offset_expr.X_op != O_symbol)
5694 {
5695 as_bad (_("expression too complex"));
5696 offset_expr.X_op = O_constant;
5697 }
5698
5699 /* A constant expression in PIC code can be handled just as it
5700 is in non PIC code. */
5701 if (mips_pic == NO_PIC
5702 || offset_expr.X_op == O_constant)
5703 {
5704 char *p;
5705
5706 /* If this is a reference to a GP relative symbol, and there
5707 is no base register, we want
5708 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5709 Otherwise, if there is no base register, we want
5710 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5711 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5712 If we have a constant, we need two instructions anyhow,
5713 so we always use the latter form.
5714
5715 If we have a base register, and this is a reference to a
5716 GP relative symbol, we want
5717 addu $tempreg,$breg,$gp
5718 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5719 Otherwise we want
5720 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5721 addu $tempreg,$tempreg,$breg
5722 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5723 With a constant we always use the latter case.
5724
5725 With 64bit address space and no base register and $at usable,
5726 we want
5727 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5728 lui $at,<sym> (BFD_RELOC_HI16_S)
5729 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5730 dsll32 $tempreg,0
5731 daddu $tempreg,$at
5732 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5733 If we have a base register, we want
5734 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5735 lui $at,<sym> (BFD_RELOC_HI16_S)
5736 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5737 daddu $at,$breg
5738 dsll32 $tempreg,0
5739 daddu $tempreg,$at
5740 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5741
5742 Without $at we can't generate the optimal path for superscalar
5743 processors here since this would require two temporary registers.
5744 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5745 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5746 dsll $tempreg,16
5747 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5748 dsll $tempreg,16
5749 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5750 If we have a base register, we want
5751 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5752 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5753 dsll $tempreg,16
5754 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5755 dsll $tempreg,16
5756 daddu $tempreg,$tempreg,$breg
5757 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5758
5759 If we have 64-bit addresses, as an optimization, for
5760 addresses which are 32-bit constants (e.g. kseg0/kseg1
5761 addresses) we fall back to the 32-bit address generation
5762 mechanism since it is more efficient. Note that due to
5763 the signed offset used by memory operations, the 32-bit
5764 range is shifted down by 32768 here. This code should
5765 probably attempt to generate 64-bit constants more
5766 efficiently in general.
5767 */
5768 if (HAVE_64BIT_ADDRESSES
5769 && !(offset_expr.X_op == O_constant
5770 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5771 {
5772 p = NULL;
5773
5774 /* We don't do GP optimization for now because RELAX_ENCODE can't
5775 hold the data for such large chunks. */
5776
5777 if (used_at == 0 && ! mips_opts.noat)
5778 {
5779 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5780 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5781 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5782 AT, (int) BFD_RELOC_HI16_S);
5783 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5784 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5785 if (breg != 0)
5786 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5787 "d,v,t", AT, AT, breg);
5788 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5789 "d,w,<", tempreg, tempreg, 0);
5790 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5791 "d,v,t", tempreg, tempreg, AT);
5792 macro_build (p, &icnt, &offset_expr, s,
5793 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5794 used_at = 1;
5795 }
5796 else
5797 {
5798 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5799 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5800 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5801 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5802 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5803 "d,w,<", tempreg, tempreg, 16);
5804 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5805 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5806 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5807 "d,w,<", tempreg, tempreg, 16);
5808 if (breg != 0)
5809 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5810 "d,v,t", tempreg, tempreg, breg);
5811 macro_build (p, &icnt, &offset_expr, s,
5812 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5813 }
5814
5815 return;
5816 }
5817
5818 if (breg == 0)
5819 {
5820 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5821 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5822 p = NULL;
5823 else
5824 {
5825 frag_grow (20);
5826 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5827 treg, (int) BFD_RELOC_GPREL16,
5828 mips_gp_register);
5829 p = frag_var (rs_machine_dependent, 8, 0,
5830 RELAX_ENCODE (4, 8, 0, 4, 0,
5831 (mips_opts.warn_about_macros
5832 || (used_at
5833 && mips_opts.noat))),
5834 offset_expr.X_add_symbol, 0, NULL);
5835 used_at = 0;
5836 }
5837 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5838 if (p != NULL)
5839 p += 4;
5840 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5841 (int) BFD_RELOC_LO16, tempreg);
5842 }
5843 else
5844 {
5845 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5846 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5847 p = NULL;
5848 else
5849 {
5850 frag_grow (28);
5851 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5852 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5853 "d,v,t", tempreg, breg, mips_gp_register);
5854 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5855 treg, (int) BFD_RELOC_GPREL16, tempreg);
5856 p = frag_var (rs_machine_dependent, 12, 0,
5857 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5858 offset_expr.X_add_symbol, 0, NULL);
5859 }
5860 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5861 if (p != NULL)
5862 p += 4;
5863 macro_build (p, &icnt, (expressionS *) NULL,
5864 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5865 "d,v,t", tempreg, tempreg, breg);
5866 if (p != NULL)
5867 p += 4;
5868 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5869 (int) BFD_RELOC_LO16, tempreg);
5870 }
5871 }
5872 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5873 {
5874 char *p;
5875 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5876
5877 /* If this is a reference to an external symbol, we want
5878 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5879 nop
5880 <op> $treg,0($tempreg)
5881 Otherwise we want
5882 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5883 nop
5884 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5885 <op> $treg,0($tempreg)
5886 If we have NewABI, we want
5887 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5888 If there is a base register, we add it to $tempreg before
5889 the <op>. If there is a constant, we stick it in the
5890 <op> instruction. We don't handle constants larger than
5891 16 bits, because we have no way to load the upper 16 bits
5892 (actually, we could handle them for the subset of cases
5893 in which we are not using $at). */
5894 assert (offset_expr.X_op == O_symbol);
5895 expr1.X_add_number = offset_expr.X_add_number;
5896 offset_expr.X_add_number = 0;
5897 if (HAVE_NEWABI)
5898 lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5899 if (expr1.X_add_number < -0x8000
5900 || expr1.X_add_number >= 0x8000)
5901 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5902 frag_grow (20);
5903 macro_build ((char *) NULL, &icnt, &offset_expr,
5904 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5905 (int) lw_reloc_type, mips_gp_register);
5906 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5907 p = frag_var (rs_machine_dependent, 4, 0,
5908 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5909 offset_expr.X_add_symbol, 0, NULL);
5910 macro_build (p, &icnt, &offset_expr,
5911 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5912 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5913 if (breg != 0)
5914 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5915 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5916 "d,v,t", tempreg, tempreg, breg);
5917 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5918 (int) BFD_RELOC_LO16, tempreg);
5919 }
5920 else if (mips_pic == SVR4_PIC)
5921 {
5922 int gpdel;
5923 char *p;
5924
5925 /* If this is a reference to an external symbol, we want
5926 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5927 addu $tempreg,$tempreg,$gp
5928 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5929 <op> $treg,0($tempreg)
5930 Otherwise we want
5931 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5932 nop
5933 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5934 <op> $treg,0($tempreg)
5935 If there is a base register, we add it to $tempreg before
5936 the <op>. If there is a constant, we stick it in the
5937 <op> instruction. We don't handle constants larger than
5938 16 bits, because we have no way to load the upper 16 bits
5939 (actually, we could handle them for the subset of cases
5940 in which we are not using $at).
5941
5942 For NewABI, we want
5943 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5944 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5945 <op> $treg,0($tempreg)
5946 */
5947 assert (offset_expr.X_op == O_symbol);
5948 expr1.X_add_number = offset_expr.X_add_number;
5949 offset_expr.X_add_number = 0;
5950 if (expr1.X_add_number < -0x8000
5951 || expr1.X_add_number >= 0x8000)
5952 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5953 if (HAVE_NEWABI)
5954 {
5955 macro_build ((char *) NULL, &icnt, &offset_expr,
5956 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5957 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5958 mips_gp_register);
5959 macro_build ((char *) NULL, &icnt, &offset_expr,
5960 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5961 "t,r,j", tempreg, tempreg,
5962 BFD_RELOC_MIPS_GOT_OFST);
5963 if (breg != 0)
5964 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5965 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5966 "d,v,t", tempreg, tempreg, breg);
5967 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5968 (int) BFD_RELOC_LO16, tempreg);
5969
5970 if (! used_at)
5971 return;
5972
5973 break;
5974 }
5975 if (reg_needs_delay (mips_gp_register))
5976 gpdel = 4;
5977 else
5978 gpdel = 0;
5979 frag_grow (36);
5980 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5981 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5982 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5983 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5984 "d,v,t", tempreg, tempreg, mips_gp_register);
5985 macro_build ((char *) NULL, &icnt, &offset_expr,
5986 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5987 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5988 tempreg);
5989 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5990 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5991 offset_expr.X_add_symbol, 0, NULL);
5992 if (gpdel > 0)
5993 {
5994 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5995 p += 4;
5996 }
5997 macro_build (p, &icnt, &offset_expr,
5998 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5999 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6000 mips_gp_register);
6001 p += 4;
6002 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6003 p += 4;
6004 macro_build (p, &icnt, &offset_expr,
6005 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6006 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6007 if (breg != 0)
6008 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6009 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6010 "d,v,t", tempreg, tempreg, breg);
6011 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6012 (int) BFD_RELOC_LO16, tempreg);
6013 }
6014 else if (mips_pic == EMBEDDED_PIC)
6015 {
6016 /* If there is no base register, we want
6017 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6018 If there is a base register, we want
6019 addu $tempreg,$breg,$gp
6020 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
6021 */
6022 assert (offset_expr.X_op == O_symbol);
6023 if (breg == 0)
6024 {
6025 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6026 treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6027 used_at = 0;
6028 }
6029 else
6030 {
6031 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6032 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6033 "d,v,t", tempreg, breg, mips_gp_register);
6034 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6035 treg, (int) BFD_RELOC_GPREL16, tempreg);
6036 }
6037 }
6038 else
6039 abort ();
6040
6041 if (! used_at)
6042 return;
6043
6044 break;
6045
6046 case M_LI:
6047 case M_LI_S:
6048 load_register (&icnt, treg, &imm_expr, 0);
6049 return;
6050
6051 case M_DLI:
6052 load_register (&icnt, treg, &imm_expr, 1);
6053 return;
6054
6055 case M_LI_SS:
6056 if (imm_expr.X_op == O_constant)
6057 {
6058 load_register (&icnt, AT, &imm_expr, 0);
6059 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6060 "mtc1", "t,G", AT, treg);
6061 break;
6062 }
6063 else
6064 {
6065 assert (offset_expr.X_op == O_symbol
6066 && strcmp (segment_name (S_GET_SEGMENT
6067 (offset_expr.X_add_symbol)),
6068 ".lit4") == 0
6069 && offset_expr.X_add_number == 0);
6070 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6071 treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6072 return;
6073 }
6074
6075 case M_LI_D:
6076 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6077 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6078 order 32 bits of the value and the low order 32 bits are either
6079 zero or in OFFSET_EXPR. */
6080 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6081 {
6082 if (HAVE_64BIT_GPRS)
6083 load_register (&icnt, treg, &imm_expr, 1);
6084 else
6085 {
6086 int hreg, lreg;
6087
6088 if (target_big_endian)
6089 {
6090 hreg = treg;
6091 lreg = treg + 1;
6092 }
6093 else
6094 {
6095 hreg = treg + 1;
6096 lreg = treg;
6097 }
6098
6099 if (hreg <= 31)
6100 load_register (&icnt, hreg, &imm_expr, 0);
6101 if (lreg <= 31)
6102 {
6103 if (offset_expr.X_op == O_absent)
6104 move_register (&icnt, lreg, 0);
6105 else
6106 {
6107 assert (offset_expr.X_op == O_constant);
6108 load_register (&icnt, lreg, &offset_expr, 0);
6109 }
6110 }
6111 }
6112 return;
6113 }
6114
6115 /* We know that sym is in the .rdata section. First we get the
6116 upper 16 bits of the address. */
6117 if (mips_pic == NO_PIC)
6118 {
6119 macro_build_lui (NULL, &icnt, &offset_expr, AT);
6120 }
6121 else if (mips_pic == SVR4_PIC)
6122 {
6123 macro_build ((char *) NULL, &icnt, &offset_expr,
6124 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6125 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6126 mips_gp_register);
6127 }
6128 else if (mips_pic == EMBEDDED_PIC)
6129 {
6130 /* For embedded PIC we pick up the entire address off $gp in
6131 a single instruction. */
6132 macro_build ((char *) NULL, &icnt, &offset_expr,
6133 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6134 mips_gp_register, (int) BFD_RELOC_GPREL16);
6135 offset_expr.X_op = O_constant;
6136 offset_expr.X_add_number = 0;
6137 }
6138 else
6139 abort ();
6140
6141 /* Now we load the register(s). */
6142 if (HAVE_64BIT_GPRS)
6143 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6144 treg, (int) BFD_RELOC_LO16, AT);
6145 else
6146 {
6147 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6148 treg, (int) BFD_RELOC_LO16, AT);
6149 if (treg != RA)
6150 {
6151 /* FIXME: How in the world do we deal with the possible
6152 overflow here? */
6153 offset_expr.X_add_number += 4;
6154 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6155 treg + 1, (int) BFD_RELOC_LO16, AT);
6156 }
6157 }
6158
6159 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6160 does not become a variant frag. */
6161 frag_wane (frag_now);
6162 frag_new (0);
6163
6164 break;
6165
6166 case M_LI_DD:
6167 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6168 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6169 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6170 the value and the low order 32 bits are either zero or in
6171 OFFSET_EXPR. */
6172 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6173 {
6174 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6175 if (HAVE_64BIT_FPRS)
6176 {
6177 assert (HAVE_64BIT_GPRS);
6178 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6179 "dmtc1", "t,S", AT, treg);
6180 }
6181 else
6182 {
6183 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6184 "mtc1", "t,G", AT, treg + 1);
6185 if (offset_expr.X_op == O_absent)
6186 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6187 "mtc1", "t,G", 0, treg);
6188 else
6189 {
6190 assert (offset_expr.X_op == O_constant);
6191 load_register (&icnt, AT, &offset_expr, 0);
6192 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6193 "mtc1", "t,G", AT, treg);
6194 }
6195 }
6196 break;
6197 }
6198
6199 assert (offset_expr.X_op == O_symbol
6200 && offset_expr.X_add_number == 0);
6201 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6202 if (strcmp (s, ".lit8") == 0)
6203 {
6204 if (mips_opts.isa != ISA_MIPS1)
6205 {
6206 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6207 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6208 mips_gp_register);
6209 return;
6210 }
6211 breg = mips_gp_register;
6212 r = BFD_RELOC_MIPS_LITERAL;
6213 goto dob;
6214 }
6215 else
6216 {
6217 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6218 if (mips_pic == SVR4_PIC)
6219 macro_build ((char *) NULL, &icnt, &offset_expr,
6220 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6221 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6222 mips_gp_register);
6223 else
6224 {
6225 /* FIXME: This won't work for a 64 bit address. */
6226 macro_build_lui (NULL, &icnt, &offset_expr, AT);
6227 }
6228
6229 if (mips_opts.isa != ISA_MIPS1)
6230 {
6231 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6232 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6233
6234 /* To avoid confusion in tc_gen_reloc, we must ensure
6235 that this does not become a variant frag. */
6236 frag_wane (frag_now);
6237 frag_new (0);
6238
6239 break;
6240 }
6241 breg = AT;
6242 r = BFD_RELOC_LO16;
6243 goto dob;
6244 }
6245
6246 case M_L_DOB:
6247 if (mips_arch == CPU_R4650)
6248 {
6249 as_bad (_("opcode not supported on this processor"));
6250 return;
6251 }
6252 /* Even on a big endian machine $fn comes before $fn+1. We have
6253 to adjust when loading from memory. */
6254 r = BFD_RELOC_LO16;
6255 dob:
6256 assert (mips_opts.isa == ISA_MIPS1);
6257 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6258 target_big_endian ? treg + 1 : treg,
6259 (int) r, breg);
6260 /* FIXME: A possible overflow which I don't know how to deal
6261 with. */
6262 offset_expr.X_add_number += 4;
6263 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6264 target_big_endian ? treg : treg + 1,
6265 (int) r, breg);
6266
6267 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6268 does not become a variant frag. */
6269 frag_wane (frag_now);
6270 frag_new (0);
6271
6272 if (breg != AT)
6273 return;
6274 break;
6275
6276 case M_L_DAB:
6277 /*
6278 * The MIPS assembler seems to check for X_add_number not
6279 * being double aligned and generating:
6280 * lui at,%hi(foo+1)
6281 * addu at,at,v1
6282 * addiu at,at,%lo(foo+1)
6283 * lwc1 f2,0(at)
6284 * lwc1 f3,4(at)
6285 * But, the resulting address is the same after relocation so why
6286 * generate the extra instruction?
6287 */
6288 if (mips_arch == CPU_R4650)
6289 {
6290 as_bad (_("opcode not supported on this processor"));
6291 return;
6292 }
6293 /* Itbl support may require additional care here. */
6294 coproc = 1;
6295 if (mips_opts.isa != ISA_MIPS1)
6296 {
6297 s = "ldc1";
6298 goto ld;
6299 }
6300
6301 s = "lwc1";
6302 fmt = "T,o(b)";
6303 goto ldd_std;
6304
6305 case M_S_DAB:
6306 if (mips_arch == CPU_R4650)
6307 {
6308 as_bad (_("opcode not supported on this processor"));
6309 return;
6310 }
6311
6312 if (mips_opts.isa != ISA_MIPS1)
6313 {
6314 s = "sdc1";
6315 goto st;
6316 }
6317
6318 s = "swc1";
6319 fmt = "T,o(b)";
6320 /* Itbl support may require additional care here. */
6321 coproc = 1;
6322 goto ldd_std;
6323
6324 case M_LD_AB:
6325 if (HAVE_64BIT_GPRS)
6326 {
6327 s = "ld";
6328 goto ld;
6329 }
6330
6331 s = "lw";
6332 fmt = "t,o(b)";
6333 goto ldd_std;
6334
6335 case M_SD_AB:
6336 if (HAVE_64BIT_GPRS)
6337 {
6338 s = "sd";
6339 goto st;
6340 }
6341
6342 s = "sw";
6343 fmt = "t,o(b)";
6344
6345 ldd_std:
6346 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6347 loads for the case of doing a pair of loads to simulate an 'ld'.
6348 This is not currently done by the compiler, and assembly coders
6349 writing embedded-pic code can cope. */
6350
6351 if (offset_expr.X_op != O_symbol
6352 && offset_expr.X_op != O_constant)
6353 {
6354 as_bad (_("expression too complex"));
6355 offset_expr.X_op = O_constant;
6356 }
6357
6358 /* Even on a big endian machine $fn comes before $fn+1. We have
6359 to adjust when loading from memory. We set coproc if we must
6360 load $fn+1 first. */
6361 /* Itbl support may require additional care here. */
6362 if (! target_big_endian)
6363 coproc = 0;
6364
6365 if (mips_pic == NO_PIC
6366 || offset_expr.X_op == O_constant)
6367 {
6368 char *p;
6369
6370 /* If this is a reference to a GP relative symbol, we want
6371 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6372 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6373 If we have a base register, we use this
6374 addu $at,$breg,$gp
6375 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6376 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6377 If this is not a GP relative symbol, we want
6378 lui $at,<sym> (BFD_RELOC_HI16_S)
6379 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6380 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6381 If there is a base register, we add it to $at after the
6382 lui instruction. If there is a constant, we always use
6383 the last case. */
6384 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6385 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6386 {
6387 p = NULL;
6388 used_at = 1;
6389 }
6390 else
6391 {
6392 int off;
6393
6394 if (breg == 0)
6395 {
6396 frag_grow (28);
6397 tempreg = mips_gp_register;
6398 off = 0;
6399 used_at = 0;
6400 }
6401 else
6402 {
6403 frag_grow (36);
6404 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6405 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6406 "d,v,t", AT, breg, mips_gp_register);
6407 tempreg = AT;
6408 off = 4;
6409 used_at = 1;
6410 }
6411
6412 /* Itbl support may require additional care here. */
6413 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6414 coproc ? treg + 1 : treg,
6415 (int) BFD_RELOC_GPREL16, tempreg);
6416 offset_expr.X_add_number += 4;
6417
6418 /* Set mips_optimize to 2 to avoid inserting an
6419 undesired nop. */
6420 hold_mips_optimize = mips_optimize;
6421 mips_optimize = 2;
6422 /* Itbl support may require additional care here. */
6423 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6424 coproc ? treg : treg + 1,
6425 (int) BFD_RELOC_GPREL16, tempreg);
6426 mips_optimize = hold_mips_optimize;
6427
6428 p = frag_var (rs_machine_dependent, 12 + off, 0,
6429 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6430 used_at && mips_opts.noat),
6431 offset_expr.X_add_symbol, 0, NULL);
6432
6433 /* We just generated two relocs. When tc_gen_reloc
6434 handles this case, it will skip the first reloc and
6435 handle the second. The second reloc already has an
6436 extra addend of 4, which we added above. We must
6437 subtract it out, and then subtract another 4 to make
6438 the first reloc come out right. The second reloc
6439 will come out right because we are going to add 4 to
6440 offset_expr when we build its instruction below.
6441
6442 If we have a symbol, then we don't want to include
6443 the offset, because it will wind up being included
6444 when we generate the reloc. */
6445
6446 if (offset_expr.X_op == O_constant)
6447 offset_expr.X_add_number -= 8;
6448 else
6449 {
6450 offset_expr.X_add_number = -4;
6451 offset_expr.X_op = O_constant;
6452 }
6453 }
6454 macro_build_lui (p, &icnt, &offset_expr, AT);
6455 if (p != NULL)
6456 p += 4;
6457 if (breg != 0)
6458 {
6459 macro_build (p, &icnt, (expressionS *) NULL,
6460 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6461 "d,v,t", AT, breg, AT);
6462 if (p != NULL)
6463 p += 4;
6464 }
6465 /* Itbl support may require additional care here. */
6466 macro_build (p, &icnt, &offset_expr, s, fmt,
6467 coproc ? treg + 1 : treg,
6468 (int) BFD_RELOC_LO16, AT);
6469 if (p != NULL)
6470 p += 4;
6471 /* FIXME: How do we handle overflow here? */
6472 offset_expr.X_add_number += 4;
6473 /* Itbl support may require additional care here. */
6474 macro_build (p, &icnt, &offset_expr, s, fmt,
6475 coproc ? treg : treg + 1,
6476 (int) BFD_RELOC_LO16, AT);
6477 }
6478 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6479 {
6480 int off;
6481
6482 /* If this is a reference to an external symbol, we want
6483 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6484 nop
6485 <op> $treg,0($at)
6486 <op> $treg+1,4($at)
6487 Otherwise we want
6488 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6489 nop
6490 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6491 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6492 If there is a base register we add it to $at before the
6493 lwc1 instructions. If there is a constant we include it
6494 in the lwc1 instructions. */
6495 used_at = 1;
6496 expr1.X_add_number = offset_expr.X_add_number;
6497 offset_expr.X_add_number = 0;
6498 if (expr1.X_add_number < -0x8000
6499 || expr1.X_add_number >= 0x8000 - 4)
6500 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6501 if (breg == 0)
6502 off = 0;
6503 else
6504 off = 4;
6505 frag_grow (24 + off);
6506 macro_build ((char *) NULL, &icnt, &offset_expr,
6507 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6508 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6509 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6510 if (breg != 0)
6511 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6512 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6513 "d,v,t", AT, breg, AT);
6514 /* Itbl support may require additional care here. */
6515 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6516 coproc ? treg + 1 : treg,
6517 (int) BFD_RELOC_LO16, AT);
6518 expr1.X_add_number += 4;
6519
6520 /* Set mips_optimize to 2 to avoid inserting an undesired
6521 nop. */
6522 hold_mips_optimize = mips_optimize;
6523 mips_optimize = 2;
6524 /* Itbl support may require additional care here. */
6525 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6526 coproc ? treg : treg + 1,
6527 (int) BFD_RELOC_LO16, AT);
6528 mips_optimize = hold_mips_optimize;
6529
6530 (void) frag_var (rs_machine_dependent, 0, 0,
6531 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6532 offset_expr.X_add_symbol, 0, NULL);
6533 }
6534 else if (mips_pic == SVR4_PIC)
6535 {
6536 int gpdel, off;
6537 char *p;
6538
6539 /* If this is a reference to an external symbol, we want
6540 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6541 addu $at,$at,$gp
6542 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6543 nop
6544 <op> $treg,0($at)
6545 <op> $treg+1,4($at)
6546 Otherwise we want
6547 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6548 nop
6549 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6550 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6551 If there is a base register we add it to $at before the
6552 lwc1 instructions. If there is a constant we include it
6553 in the lwc1 instructions. */
6554 used_at = 1;
6555 expr1.X_add_number = offset_expr.X_add_number;
6556 offset_expr.X_add_number = 0;
6557 if (expr1.X_add_number < -0x8000
6558 || expr1.X_add_number >= 0x8000 - 4)
6559 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6560 if (reg_needs_delay (mips_gp_register))
6561 gpdel = 4;
6562 else
6563 gpdel = 0;
6564 if (breg == 0)
6565 off = 0;
6566 else
6567 off = 4;
6568 frag_grow (56);
6569 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6570 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6571 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6572 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6573 "d,v,t", AT, AT, mips_gp_register);
6574 macro_build ((char *) NULL, &icnt, &offset_expr,
6575 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6576 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6577 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6578 if (breg != 0)
6579 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6580 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6581 "d,v,t", AT, breg, AT);
6582 /* Itbl support may require additional care here. */
6583 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6584 coproc ? treg + 1 : treg,
6585 (int) BFD_RELOC_LO16, AT);
6586 expr1.X_add_number += 4;
6587
6588 /* Set mips_optimize to 2 to avoid inserting an undesired
6589 nop. */
6590 hold_mips_optimize = mips_optimize;
6591 mips_optimize = 2;
6592 /* Itbl support may require additional care here. */
6593 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6594 coproc ? treg : treg + 1,
6595 (int) BFD_RELOC_LO16, AT);
6596 mips_optimize = hold_mips_optimize;
6597 expr1.X_add_number -= 4;
6598
6599 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6600 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6601 8 + gpdel + off, 1, 0),
6602 offset_expr.X_add_symbol, 0, NULL);
6603 if (gpdel > 0)
6604 {
6605 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6606 p += 4;
6607 }
6608 macro_build (p, &icnt, &offset_expr,
6609 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6610 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6611 mips_gp_register);
6612 p += 4;
6613 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6614 p += 4;
6615 if (breg != 0)
6616 {
6617 macro_build (p, &icnt, (expressionS *) NULL,
6618 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6619 "d,v,t", AT, breg, AT);
6620 p += 4;
6621 }
6622 /* Itbl support may require additional care here. */
6623 macro_build (p, &icnt, &expr1, s, fmt,
6624 coproc ? treg + 1 : treg,
6625 (int) BFD_RELOC_LO16, AT);
6626 p += 4;
6627 expr1.X_add_number += 4;
6628
6629 /* Set mips_optimize to 2 to avoid inserting an undesired
6630 nop. */
6631 hold_mips_optimize = mips_optimize;
6632 mips_optimize = 2;
6633 /* Itbl support may require additional care here. */
6634 macro_build (p, &icnt, &expr1, s, fmt,
6635 coproc ? treg : treg + 1,
6636 (int) BFD_RELOC_LO16, AT);
6637 mips_optimize = hold_mips_optimize;
6638 }
6639 else if (mips_pic == EMBEDDED_PIC)
6640 {
6641 /* If there is no base register, we use
6642 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6643 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6644 If we have a base register, we use
6645 addu $at,$breg,$gp
6646 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6647 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6648 */
6649 if (breg == 0)
6650 {
6651 tempreg = mips_gp_register;
6652 used_at = 0;
6653 }
6654 else
6655 {
6656 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6657 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6658 "d,v,t", AT, breg, mips_gp_register);
6659 tempreg = AT;
6660 used_at = 1;
6661 }
6662
6663 /* Itbl support may require additional care here. */
6664 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6665 coproc ? treg + 1 : treg,
6666 (int) BFD_RELOC_GPREL16, tempreg);
6667 offset_expr.X_add_number += 4;
6668 /* Itbl support may require additional care here. */
6669 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6670 coproc ? treg : treg + 1,
6671 (int) BFD_RELOC_GPREL16, tempreg);
6672 }
6673 else
6674 abort ();
6675
6676 if (! used_at)
6677 return;
6678
6679 break;
6680
6681 case M_LD_OB:
6682 s = "lw";
6683 goto sd_ob;
6684 case M_SD_OB:
6685 s = "sw";
6686 sd_ob:
6687 assert (HAVE_32BIT_ADDRESSES);
6688 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6689 (int) BFD_RELOC_LO16, breg);
6690 offset_expr.X_add_number += 4;
6691 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6692 (int) BFD_RELOC_LO16, breg);
6693 return;
6694
6695 /* New code added to support COPZ instructions.
6696 This code builds table entries out of the macros in mip_opcodes.
6697 R4000 uses interlocks to handle coproc delays.
6698 Other chips (like the R3000) require nops to be inserted for delays.
6699
6700 FIXME: Currently, we require that the user handle delays.
6701 In order to fill delay slots for non-interlocked chips,
6702 we must have a way to specify delays based on the coprocessor.
6703 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6704 What are the side-effects of the cop instruction?
6705 What cache support might we have and what are its effects?
6706 Both coprocessor & memory require delays. how long???
6707 What registers are read/set/modified?
6708
6709 If an itbl is provided to interpret cop instructions,
6710 this knowledge can be encoded in the itbl spec. */
6711
6712 case M_COP0:
6713 s = "c0";
6714 goto copz;
6715 case M_COP1:
6716 s = "c1";
6717 goto copz;
6718 case M_COP2:
6719 s = "c2";
6720 goto copz;
6721 case M_COP3:
6722 s = "c3";
6723 copz:
6724 /* For now we just do C (same as Cz). The parameter will be
6725 stored in insn_opcode by mips_ip. */
6726 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6727 ip->insn_opcode);
6728 return;
6729
6730 case M_MOVE:
6731 move_register (&icnt, dreg, sreg);
6732 return;
6733
6734 #ifdef LOSING_COMPILER
6735 default:
6736 /* Try and see if this is a new itbl instruction.
6737 This code builds table entries out of the macros in mip_opcodes.
6738 FIXME: For now we just assemble the expression and pass it's
6739 value along as a 32-bit immediate.
6740 We may want to have the assembler assemble this value,
6741 so that we gain the assembler's knowledge of delay slots,
6742 symbols, etc.
6743 Would it be more efficient to use mask (id) here? */
6744 if (itbl_have_entries
6745 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6746 {
6747 s = ip->insn_mo->name;
6748 s2 = "cop3";
6749 coproc = ITBL_DECODE_PNUM (immed_expr);;
6750 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6751 return;
6752 }
6753 macro2 (ip);
6754 return;
6755 }
6756 if (mips_opts.noat)
6757 as_warn (_("Macro used $at after \".set noat\""));
6758 }
6759
6760 static void
6761 macro2 (ip)
6762 struct mips_cl_insn *ip;
6763 {
6764 register int treg, sreg, dreg, breg;
6765 int tempreg;
6766 int mask;
6767 int icnt = 0;
6768 int used_at;
6769 expressionS expr1;
6770 const char *s;
6771 const char *s2;
6772 const char *fmt;
6773 int likely = 0;
6774 int dbl = 0;
6775 int coproc = 0;
6776 int lr = 0;
6777 int imm = 0;
6778 int off;
6779 offsetT maxnum;
6780 bfd_reloc_code_real_type r;
6781 char *p;
6782
6783 treg = (ip->insn_opcode >> 16) & 0x1f;
6784 dreg = (ip->insn_opcode >> 11) & 0x1f;
6785 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6786 mask = ip->insn_mo->mask;
6787
6788 expr1.X_op = O_constant;
6789 expr1.X_op_symbol = NULL;
6790 expr1.X_add_symbol = NULL;
6791 expr1.X_add_number = 1;
6792
6793 switch (mask)
6794 {
6795 #endif /* LOSING_COMPILER */
6796
6797 case M_DMUL:
6798 dbl = 1;
6799 case M_MUL:
6800 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6801 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6802 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6803 dreg);
6804 return;
6805
6806 case M_DMUL_I:
6807 dbl = 1;
6808 case M_MUL_I:
6809 /* The MIPS assembler some times generates shifts and adds. I'm
6810 not trying to be that fancy. GCC should do this for us
6811 anyway. */
6812 load_register (&icnt, AT, &imm_expr, dbl);
6813 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6814 dbl ? "dmult" : "mult", "s,t", sreg, AT);
6815 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6816 dreg);
6817 break;
6818
6819 case M_DMULO_I:
6820 dbl = 1;
6821 case M_MULO_I:
6822 imm = 1;
6823 goto do_mulo;
6824
6825 case M_DMULO:
6826 dbl = 1;
6827 case M_MULO:
6828 do_mulo:
6829 mips_emit_delays (true);
6830 ++mips_opts.noreorder;
6831 mips_any_noreorder = 1;
6832 if (imm)
6833 load_register (&icnt, AT, &imm_expr, dbl);
6834 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6835 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6836 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6837 dreg);
6838 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6839 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6840 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6841 AT);
6842 if (mips_trap)
6843 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6844 "s,t,q", dreg, AT, 6);
6845 else
6846 {
6847 expr1.X_add_number = 8;
6848 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6849 AT);
6850 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6851 0);
6852 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6853 "c", 6);
6854 }
6855 --mips_opts.noreorder;
6856 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6857 break;
6858
6859 case M_DMULOU_I:
6860 dbl = 1;
6861 case M_MULOU_I:
6862 imm = 1;
6863 goto do_mulou;
6864
6865 case M_DMULOU:
6866 dbl = 1;
6867 case M_MULOU:
6868 do_mulou:
6869 mips_emit_delays (true);
6870 ++mips_opts.noreorder;
6871 mips_any_noreorder = 1;
6872 if (imm)
6873 load_register (&icnt, AT, &imm_expr, dbl);
6874 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6875 dbl ? "dmultu" : "multu",
6876 "s,t", sreg, imm ? AT : treg);
6877 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6878 AT);
6879 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6880 dreg);
6881 if (mips_trap)
6882 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6883 "s,t,q", AT, 0, 6);
6884 else
6885 {
6886 expr1.X_add_number = 8;
6887 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6888 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6889 0);
6890 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6891 "c", 6);
6892 }
6893 --mips_opts.noreorder;
6894 break;
6895
6896 case M_DROL:
6897 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6898 "d,v,t", AT, 0, treg);
6899 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6900 "d,t,s", AT, sreg, AT);
6901 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6902 "d,t,s", dreg, sreg, treg);
6903 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6904 "d,v,t", dreg, dreg, AT);
6905 break;
6906
6907 case M_ROL:
6908 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6909 "d,v,t", AT, 0, treg);
6910 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6911 "d,t,s", AT, sreg, AT);
6912 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6913 "d,t,s", dreg, sreg, treg);
6914 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6915 "d,v,t", dreg, dreg, AT);
6916 break;
6917
6918 case M_DROL_I:
6919 {
6920 unsigned int rot;
6921
6922 if (imm_expr.X_op != O_constant)
6923 as_bad (_("rotate count too large"));
6924 rot = imm_expr.X_add_number & 0x3f;
6925 if (CPU_HAS_DROR (mips_arch))
6926 {
6927 rot = (64 - rot) & 0x3f;
6928 if (rot >= 32)
6929 macro_build ((char *) NULL, &icnt, NULL, "dror32",
6930 "d,w,<", dreg, sreg, rot - 32);
6931 else
6932 macro_build ((char *) NULL, &icnt, NULL, "dror",
6933 "d,w,<", dreg, sreg, rot);
6934 break;
6935 }
6936 if (rot == 0)
6937 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6938 "d,w,<", dreg, sreg, 0);
6939 else
6940 {
6941 char *l, *r;
6942
6943 l = (rot < 0x20) ? "dsll" : "dsll32";
6944 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6945 rot &= 0x1f;
6946 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6947 "d,w,<", AT, sreg, rot);
6948 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6949 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6950 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6951 "d,v,t", dreg, dreg, AT);
6952 }
6953 }
6954 break;
6955
6956 case M_ROL_I:
6957 {
6958 unsigned int rot;
6959
6960 if (imm_expr.X_op != O_constant)
6961 as_bad (_("rotate count too large"));
6962 rot = imm_expr.X_add_number & 0x1f;
6963 if (CPU_HAS_ROR (mips_arch))
6964 {
6965 macro_build ((char *) NULL, &icnt, NULL, "ror",
6966 "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
6967 break;
6968 }
6969 if (rot == 0)
6970 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6971 "d,w,<", dreg, sreg, 0);
6972 else
6973 {
6974 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6975 "d,w,<", AT, sreg, rot);
6976 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6977 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6978 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6979 "d,v,t", dreg, dreg, AT);
6980 }
6981 }
6982 break;
6983
6984 case M_DROR:
6985 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6986 "d,v,t", AT, 0, treg);
6987 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6988 "d,t,s", AT, sreg, AT);
6989 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6990 "d,t,s", dreg, sreg, treg);
6991 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6992 "d,v,t", dreg, dreg, AT);
6993 break;
6994
6995 case M_ROR:
6996 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6997 "d,v,t", AT, 0, treg);
6998 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6999 "d,t,s", AT, sreg, AT);
7000 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7001 "d,t,s", dreg, sreg, treg);
7002 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7003 "d,v,t", dreg, dreg, AT);
7004 break;
7005
7006 case M_DROR_I:
7007 {
7008 unsigned int rot;
7009
7010 if (imm_expr.X_op != O_constant)
7011 as_bad (_("rotate count too large"));
7012 rot = imm_expr.X_add_number & 0x3f;
7013 if (rot == 0)
7014 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7015 "d,w,<", dreg, sreg, 0);
7016 else
7017 {
7018 char *l, *r;
7019
7020 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7021 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7022 rot &= 0x1f;
7023 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7024 "d,w,<", AT, sreg, rot);
7025 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7026 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7027 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7028 "d,v,t", dreg, dreg, AT);
7029 }
7030 }
7031 break;
7032
7033 case M_ROR_I:
7034 {
7035 unsigned int rot;
7036
7037 if (imm_expr.X_op != O_constant)
7038 as_bad (_("rotate count too large"));
7039 rot = imm_expr.X_add_number & 0x1f;
7040 if (rot == 0)
7041 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7042 "d,w,<", dreg, sreg, 0);
7043 else
7044 {
7045 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7046 "d,w,<", AT, sreg, rot);
7047 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7048 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7049 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7050 "d,v,t", dreg, dreg, AT);
7051 }
7052 }
7053 break;
7054
7055 case M_S_DOB:
7056 if (mips_arch == CPU_R4650)
7057 {
7058 as_bad (_("opcode not supported on this processor"));
7059 return;
7060 }
7061 assert (mips_opts.isa == ISA_MIPS1);
7062 /* Even on a big endian machine $fn comes before $fn+1. We have
7063 to adjust when storing to memory. */
7064 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7065 target_big_endian ? treg + 1 : treg,
7066 (int) BFD_RELOC_LO16, breg);
7067 offset_expr.X_add_number += 4;
7068 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7069 target_big_endian ? treg : treg + 1,
7070 (int) BFD_RELOC_LO16, breg);
7071 return;
7072
7073 case M_SEQ:
7074 if (sreg == 0)
7075 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7076 treg, (int) BFD_RELOC_LO16);
7077 else if (treg == 0)
7078 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7079 sreg, (int) BFD_RELOC_LO16);
7080 else
7081 {
7082 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7083 "d,v,t", dreg, sreg, treg);
7084 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7085 dreg, (int) BFD_RELOC_LO16);
7086 }
7087 return;
7088
7089 case M_SEQ_I:
7090 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7091 {
7092 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7093 sreg, (int) BFD_RELOC_LO16);
7094 return;
7095 }
7096 if (sreg == 0)
7097 {
7098 as_warn (_("Instruction %s: result is always false"),
7099 ip->insn_mo->name);
7100 move_register (&icnt, dreg, 0);
7101 return;
7102 }
7103 if (imm_expr.X_op == O_constant
7104 && imm_expr.X_add_number >= 0
7105 && imm_expr.X_add_number < 0x10000)
7106 {
7107 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7108 sreg, (int) BFD_RELOC_LO16);
7109 used_at = 0;
7110 }
7111 else if (imm_expr.X_op == O_constant
7112 && imm_expr.X_add_number > -0x8000
7113 && imm_expr.X_add_number < 0)
7114 {
7115 imm_expr.X_add_number = -imm_expr.X_add_number;
7116 macro_build ((char *) NULL, &icnt, &imm_expr,
7117 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7118 "t,r,j", dreg, sreg,
7119 (int) BFD_RELOC_LO16);
7120 used_at = 0;
7121 }
7122 else
7123 {
7124 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7125 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7126 "d,v,t", dreg, sreg, AT);
7127 used_at = 1;
7128 }
7129 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7130 (int) BFD_RELOC_LO16);
7131 if (used_at)
7132 break;
7133 return;
7134
7135 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7136 s = "slt";
7137 goto sge;
7138 case M_SGEU:
7139 s = "sltu";
7140 sge:
7141 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7142 dreg, sreg, treg);
7143 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7144 (int) BFD_RELOC_LO16);
7145 return;
7146
7147 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7148 case M_SGEU_I:
7149 if (imm_expr.X_op == O_constant
7150 && imm_expr.X_add_number >= -0x8000
7151 && imm_expr.X_add_number < 0x8000)
7152 {
7153 macro_build ((char *) NULL, &icnt, &imm_expr,
7154 mask == M_SGE_I ? "slti" : "sltiu",
7155 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7156 used_at = 0;
7157 }
7158 else
7159 {
7160 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7161 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7162 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7163 AT);
7164 used_at = 1;
7165 }
7166 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7167 (int) BFD_RELOC_LO16);
7168 if (used_at)
7169 break;
7170 return;
7171
7172 case M_SGT: /* sreg > treg <==> treg < sreg */
7173 s = "slt";
7174 goto sgt;
7175 case M_SGTU:
7176 s = "sltu";
7177 sgt:
7178 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7179 dreg, treg, sreg);
7180 return;
7181
7182 case M_SGT_I: /* sreg > I <==> I < sreg */
7183 s = "slt";
7184 goto sgti;
7185 case M_SGTU_I:
7186 s = "sltu";
7187 sgti:
7188 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7189 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7190 dreg, AT, sreg);
7191 break;
7192
7193 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7194 s = "slt";
7195 goto sle;
7196 case M_SLEU:
7197 s = "sltu";
7198 sle:
7199 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7200 dreg, treg, sreg);
7201 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7202 (int) BFD_RELOC_LO16);
7203 return;
7204
7205 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7206 s = "slt";
7207 goto slei;
7208 case M_SLEU_I:
7209 s = "sltu";
7210 slei:
7211 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7212 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7213 dreg, AT, sreg);
7214 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7215 (int) BFD_RELOC_LO16);
7216 break;
7217
7218 case M_SLT_I:
7219 if (imm_expr.X_op == O_constant
7220 && imm_expr.X_add_number >= -0x8000
7221 && imm_expr.X_add_number < 0x8000)
7222 {
7223 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7224 dreg, sreg, (int) BFD_RELOC_LO16);
7225 return;
7226 }
7227 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7228 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7229 dreg, sreg, AT);
7230 break;
7231
7232 case M_SLTU_I:
7233 if (imm_expr.X_op == O_constant
7234 && imm_expr.X_add_number >= -0x8000
7235 && imm_expr.X_add_number < 0x8000)
7236 {
7237 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7238 dreg, sreg, (int) BFD_RELOC_LO16);
7239 return;
7240 }
7241 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7242 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7243 "d,v,t", dreg, sreg, AT);
7244 break;
7245
7246 case M_SNE:
7247 if (sreg == 0)
7248 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7249 "d,v,t", dreg, 0, treg);
7250 else if (treg == 0)
7251 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7252 "d,v,t", dreg, 0, sreg);
7253 else
7254 {
7255 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7256 "d,v,t", dreg, sreg, treg);
7257 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7258 "d,v,t", dreg, 0, dreg);
7259 }
7260 return;
7261
7262 case M_SNE_I:
7263 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7264 {
7265 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7266 "d,v,t", dreg, 0, sreg);
7267 return;
7268 }
7269 if (sreg == 0)
7270 {
7271 as_warn (_("Instruction %s: result is always true"),
7272 ip->insn_mo->name);
7273 macro_build ((char *) NULL, &icnt, &expr1,
7274 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7275 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7276 return;
7277 }
7278 if (imm_expr.X_op == O_constant
7279 && imm_expr.X_add_number >= 0
7280 && imm_expr.X_add_number < 0x10000)
7281 {
7282 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7283 dreg, sreg, (int) BFD_RELOC_LO16);
7284 used_at = 0;
7285 }
7286 else if (imm_expr.X_op == O_constant
7287 && imm_expr.X_add_number > -0x8000
7288 && imm_expr.X_add_number < 0)
7289 {
7290 imm_expr.X_add_number = -imm_expr.X_add_number;
7291 macro_build ((char *) NULL, &icnt, &imm_expr,
7292 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7293 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7294 used_at = 0;
7295 }
7296 else
7297 {
7298 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7299 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7300 "d,v,t", dreg, sreg, AT);
7301 used_at = 1;
7302 }
7303 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7304 "d,v,t", dreg, 0, dreg);
7305 if (used_at)
7306 break;
7307 return;
7308
7309 case M_DSUB_I:
7310 dbl = 1;
7311 case M_SUB_I:
7312 if (imm_expr.X_op == O_constant
7313 && imm_expr.X_add_number > -0x8000
7314 && imm_expr.X_add_number <= 0x8000)
7315 {
7316 imm_expr.X_add_number = -imm_expr.X_add_number;
7317 macro_build ((char *) NULL, &icnt, &imm_expr,
7318 dbl ? "daddi" : "addi",
7319 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7320 return;
7321 }
7322 load_register (&icnt, AT, &imm_expr, dbl);
7323 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7324 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7325 break;
7326
7327 case M_DSUBU_I:
7328 dbl = 1;
7329 case M_SUBU_I:
7330 if (imm_expr.X_op == O_constant
7331 && imm_expr.X_add_number > -0x8000
7332 && imm_expr.X_add_number <= 0x8000)
7333 {
7334 imm_expr.X_add_number = -imm_expr.X_add_number;
7335 macro_build ((char *) NULL, &icnt, &imm_expr,
7336 dbl ? "daddiu" : "addiu",
7337 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7338 return;
7339 }
7340 load_register (&icnt, AT, &imm_expr, dbl);
7341 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7342 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7343 break;
7344
7345 case M_TEQ_I:
7346 s = "teq";
7347 goto trap;
7348 case M_TGE_I:
7349 s = "tge";
7350 goto trap;
7351 case M_TGEU_I:
7352 s = "tgeu";
7353 goto trap;
7354 case M_TLT_I:
7355 s = "tlt";
7356 goto trap;
7357 case M_TLTU_I:
7358 s = "tltu";
7359 goto trap;
7360 case M_TNE_I:
7361 s = "tne";
7362 trap:
7363 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7364 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7365 AT);
7366 break;
7367
7368 case M_TRUNCWS:
7369 case M_TRUNCWD:
7370 assert (mips_opts.isa == ISA_MIPS1);
7371 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7372 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7373
7374 /*
7375 * Is the double cfc1 instruction a bug in the mips assembler;
7376 * or is there a reason for it?
7377 */
7378 mips_emit_delays (true);
7379 ++mips_opts.noreorder;
7380 mips_any_noreorder = 1;
7381 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7382 treg, RA);
7383 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7384 treg, RA);
7385 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7386 expr1.X_add_number = 3;
7387 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7388 (int) BFD_RELOC_LO16);
7389 expr1.X_add_number = 2;
7390 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7391 (int) BFD_RELOC_LO16);
7392 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7393 AT, RA);
7394 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7395 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7396 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7397 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7398 treg, RA);
7399 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7400 --mips_opts.noreorder;
7401 break;
7402
7403 case M_ULH:
7404 s = "lb";
7405 goto ulh;
7406 case M_ULHU:
7407 s = "lbu";
7408 ulh:
7409 if (offset_expr.X_add_number >= 0x7fff)
7410 as_bad (_("operand overflow"));
7411 /* avoid load delay */
7412 if (! target_big_endian)
7413 ++offset_expr.X_add_number;
7414 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7415 (int) BFD_RELOC_LO16, breg);
7416 if (! target_big_endian)
7417 --offset_expr.X_add_number;
7418 else
7419 ++offset_expr.X_add_number;
7420 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7421 (int) BFD_RELOC_LO16, breg);
7422 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7423 treg, treg, 8);
7424 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7425 treg, treg, AT);
7426 break;
7427
7428 case M_ULD:
7429 s = "ldl";
7430 s2 = "ldr";
7431 off = 7;
7432 goto ulw;
7433 case M_ULW:
7434 s = "lwl";
7435 s2 = "lwr";
7436 off = 3;
7437 ulw:
7438 if (offset_expr.X_add_number >= 0x8000 - off)
7439 as_bad (_("operand overflow"));
7440 if (! target_big_endian)
7441 offset_expr.X_add_number += off;
7442 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7443 (int) BFD_RELOC_LO16, breg);
7444 if (! target_big_endian)
7445 offset_expr.X_add_number -= off;
7446 else
7447 offset_expr.X_add_number += off;
7448 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7449 (int) BFD_RELOC_LO16, breg);
7450 return;
7451
7452 case M_ULD_A:
7453 s = "ldl";
7454 s2 = "ldr";
7455 off = 7;
7456 goto ulwa;
7457 case M_ULW_A:
7458 s = "lwl";
7459 s2 = "lwr";
7460 off = 3;
7461 ulwa:
7462 used_at = 1;
7463 load_address (&icnt, AT, &offset_expr, &used_at);
7464 if (breg != 0)
7465 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7466 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7467 "d,v,t", AT, AT, breg);
7468 if (! target_big_endian)
7469 expr1.X_add_number = off;
7470 else
7471 expr1.X_add_number = 0;
7472 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7473 (int) BFD_RELOC_LO16, AT);
7474 if (! target_big_endian)
7475 expr1.X_add_number = 0;
7476 else
7477 expr1.X_add_number = off;
7478 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7479 (int) BFD_RELOC_LO16, AT);
7480 break;
7481
7482 case M_ULH_A:
7483 case M_ULHU_A:
7484 used_at = 1;
7485 load_address (&icnt, AT, &offset_expr, &used_at);
7486 if (breg != 0)
7487 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7488 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7489 "d,v,t", AT, AT, breg);
7490 if (target_big_endian)
7491 expr1.X_add_number = 0;
7492 macro_build ((char *) NULL, &icnt, &expr1,
7493 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7494 (int) BFD_RELOC_LO16, AT);
7495 if (target_big_endian)
7496 expr1.X_add_number = 1;
7497 else
7498 expr1.X_add_number = 0;
7499 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7500 (int) BFD_RELOC_LO16, AT);
7501 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7502 treg, treg, 8);
7503 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7504 treg, treg, AT);
7505 break;
7506
7507 case M_USH:
7508 if (offset_expr.X_add_number >= 0x7fff)
7509 as_bad (_("operand overflow"));
7510 if (target_big_endian)
7511 ++offset_expr.X_add_number;
7512 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7513 (int) BFD_RELOC_LO16, breg);
7514 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7515 AT, treg, 8);
7516 if (target_big_endian)
7517 --offset_expr.X_add_number;
7518 else
7519 ++offset_expr.X_add_number;
7520 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7521 (int) BFD_RELOC_LO16, breg);
7522 break;
7523
7524 case M_USD:
7525 s = "sdl";
7526 s2 = "sdr";
7527 off = 7;
7528 goto usw;
7529 case M_USW:
7530 s = "swl";
7531 s2 = "swr";
7532 off = 3;
7533 usw:
7534 if (offset_expr.X_add_number >= 0x8000 - off)
7535 as_bad (_("operand overflow"));
7536 if (! target_big_endian)
7537 offset_expr.X_add_number += off;
7538 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7539 (int) BFD_RELOC_LO16, breg);
7540 if (! target_big_endian)
7541 offset_expr.X_add_number -= off;
7542 else
7543 offset_expr.X_add_number += off;
7544 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7545 (int) BFD_RELOC_LO16, breg);
7546 return;
7547
7548 case M_USD_A:
7549 s = "sdl";
7550 s2 = "sdr";
7551 off = 7;
7552 goto uswa;
7553 case M_USW_A:
7554 s = "swl";
7555 s2 = "swr";
7556 off = 3;
7557 uswa:
7558 used_at = 1;
7559 load_address (&icnt, AT, &offset_expr, &used_at);
7560 if (breg != 0)
7561 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7562 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7563 "d,v,t", AT, AT, breg);
7564 if (! target_big_endian)
7565 expr1.X_add_number = off;
7566 else
7567 expr1.X_add_number = 0;
7568 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7569 (int) BFD_RELOC_LO16, AT);
7570 if (! target_big_endian)
7571 expr1.X_add_number = 0;
7572 else
7573 expr1.X_add_number = off;
7574 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7575 (int) BFD_RELOC_LO16, AT);
7576 break;
7577
7578 case M_USH_A:
7579 used_at = 1;
7580 load_address (&icnt, AT, &offset_expr, &used_at);
7581 if (breg != 0)
7582 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7583 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7584 "d,v,t", AT, AT, breg);
7585 if (! target_big_endian)
7586 expr1.X_add_number = 0;
7587 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7588 (int) BFD_RELOC_LO16, AT);
7589 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7590 treg, treg, 8);
7591 if (! target_big_endian)
7592 expr1.X_add_number = 1;
7593 else
7594 expr1.X_add_number = 0;
7595 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7596 (int) BFD_RELOC_LO16, AT);
7597 if (! target_big_endian)
7598 expr1.X_add_number = 0;
7599 else
7600 expr1.X_add_number = 1;
7601 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7602 (int) BFD_RELOC_LO16, AT);
7603 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7604 treg, treg, 8);
7605 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7606 treg, treg, AT);
7607 break;
7608
7609 default:
7610 /* FIXME: Check if this is one of the itbl macros, since they
7611 are added dynamically. */
7612 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7613 break;
7614 }
7615 if (mips_opts.noat)
7616 as_warn (_("Macro used $at after \".set noat\""));
7617 }
7618
7619 /* Implement macros in mips16 mode. */
7620
7621 static void
7622 mips16_macro (ip)
7623 struct mips_cl_insn *ip;
7624 {
7625 int mask;
7626 int xreg, yreg, zreg, tmp;
7627 int icnt;
7628 expressionS expr1;
7629 int dbl;
7630 const char *s, *s2, *s3;
7631
7632 mask = ip->insn_mo->mask;
7633
7634 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7635 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7636 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7637
7638 icnt = 0;
7639
7640 expr1.X_op = O_constant;
7641 expr1.X_op_symbol = NULL;
7642 expr1.X_add_symbol = NULL;
7643 expr1.X_add_number = 1;
7644
7645 dbl = 0;
7646
7647 switch (mask)
7648 {
7649 default:
7650 internalError ();
7651
7652 case M_DDIV_3:
7653 dbl = 1;
7654 case M_DIV_3:
7655 s = "mflo";
7656 goto do_div3;
7657 case M_DREM_3:
7658 dbl = 1;
7659 case M_REM_3:
7660 s = "mfhi";
7661 do_div3:
7662 mips_emit_delays (true);
7663 ++mips_opts.noreorder;
7664 mips_any_noreorder = 1;
7665 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7666 dbl ? "ddiv" : "div",
7667 "0,x,y", xreg, yreg);
7668 expr1.X_add_number = 2;
7669 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7670 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7671 7);
7672
7673 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7674 since that causes an overflow. We should do that as well,
7675 but I don't see how to do the comparisons without a temporary
7676 register. */
7677 --mips_opts.noreorder;
7678 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7679 break;
7680
7681 case M_DIVU_3:
7682 s = "divu";
7683 s2 = "mflo";
7684 goto do_divu3;
7685 case M_REMU_3:
7686 s = "divu";
7687 s2 = "mfhi";
7688 goto do_divu3;
7689 case M_DDIVU_3:
7690 s = "ddivu";
7691 s2 = "mflo";
7692 goto do_divu3;
7693 case M_DREMU_3:
7694 s = "ddivu";
7695 s2 = "mfhi";
7696 do_divu3:
7697 mips_emit_delays (true);
7698 ++mips_opts.noreorder;
7699 mips_any_noreorder = 1;
7700 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7701 xreg, yreg);
7702 expr1.X_add_number = 2;
7703 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7704 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7705 "6", 7);
7706 --mips_opts.noreorder;
7707 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7708 break;
7709
7710 case M_DMUL:
7711 dbl = 1;
7712 case M_MUL:
7713 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7714 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7715 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7716 zreg);
7717 return;
7718
7719 case M_DSUBU_I:
7720 dbl = 1;
7721 goto do_subu;
7722 case M_SUBU_I:
7723 do_subu:
7724 if (imm_expr.X_op != O_constant)
7725 as_bad (_("Unsupported large constant"));
7726 imm_expr.X_add_number = -imm_expr.X_add_number;
7727 macro_build ((char *) NULL, &icnt, &imm_expr,
7728 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7729 break;
7730
7731 case M_SUBU_I_2:
7732 if (imm_expr.X_op != O_constant)
7733 as_bad (_("Unsupported large constant"));
7734 imm_expr.X_add_number = -imm_expr.X_add_number;
7735 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7736 "x,k", xreg);
7737 break;
7738
7739 case M_DSUBU_I_2:
7740 if (imm_expr.X_op != O_constant)
7741 as_bad (_("Unsupported large constant"));
7742 imm_expr.X_add_number = -imm_expr.X_add_number;
7743 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7744 "y,j", yreg);
7745 break;
7746
7747 case M_BEQ:
7748 s = "cmp";
7749 s2 = "bteqz";
7750 goto do_branch;
7751 case M_BNE:
7752 s = "cmp";
7753 s2 = "btnez";
7754 goto do_branch;
7755 case M_BLT:
7756 s = "slt";
7757 s2 = "btnez";
7758 goto do_branch;
7759 case M_BLTU:
7760 s = "sltu";
7761 s2 = "btnez";
7762 goto do_branch;
7763 case M_BLE:
7764 s = "slt";
7765 s2 = "bteqz";
7766 goto do_reverse_branch;
7767 case M_BLEU:
7768 s = "sltu";
7769 s2 = "bteqz";
7770 goto do_reverse_branch;
7771 case M_BGE:
7772 s = "slt";
7773 s2 = "bteqz";
7774 goto do_branch;
7775 case M_BGEU:
7776 s = "sltu";
7777 s2 = "bteqz";
7778 goto do_branch;
7779 case M_BGT:
7780 s = "slt";
7781 s2 = "btnez";
7782 goto do_reverse_branch;
7783 case M_BGTU:
7784 s = "sltu";
7785 s2 = "btnez";
7786
7787 do_reverse_branch:
7788 tmp = xreg;
7789 xreg = yreg;
7790 yreg = tmp;
7791
7792 do_branch:
7793 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7794 xreg, yreg);
7795 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7796 break;
7797
7798 case M_BEQ_I:
7799 s = "cmpi";
7800 s2 = "bteqz";
7801 s3 = "x,U";
7802 goto do_branch_i;
7803 case M_BNE_I:
7804 s = "cmpi";
7805 s2 = "btnez";
7806 s3 = "x,U";
7807 goto do_branch_i;
7808 case M_BLT_I:
7809 s = "slti";
7810 s2 = "btnez";
7811 s3 = "x,8";
7812 goto do_branch_i;
7813 case M_BLTU_I:
7814 s = "sltiu";
7815 s2 = "btnez";
7816 s3 = "x,8";
7817 goto do_branch_i;
7818 case M_BLE_I:
7819 s = "slti";
7820 s2 = "btnez";
7821 s3 = "x,8";
7822 goto do_addone_branch_i;
7823 case M_BLEU_I:
7824 s = "sltiu";
7825 s2 = "btnez";
7826 s3 = "x,8";
7827 goto do_addone_branch_i;
7828 case M_BGE_I:
7829 s = "slti";
7830 s2 = "bteqz";
7831 s3 = "x,8";
7832 goto do_branch_i;
7833 case M_BGEU_I:
7834 s = "sltiu";
7835 s2 = "bteqz";
7836 s3 = "x,8";
7837 goto do_branch_i;
7838 case M_BGT_I:
7839 s = "slti";
7840 s2 = "bteqz";
7841 s3 = "x,8";
7842 goto do_addone_branch_i;
7843 case M_BGTU_I:
7844 s = "sltiu";
7845 s2 = "bteqz";
7846 s3 = "x,8";
7847
7848 do_addone_branch_i:
7849 if (imm_expr.X_op != O_constant)
7850 as_bad (_("Unsupported large constant"));
7851 ++imm_expr.X_add_number;
7852
7853 do_branch_i:
7854 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7855 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7856 break;
7857
7858 case M_ABS:
7859 expr1.X_add_number = 0;
7860 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7861 if (xreg != yreg)
7862 move_register (&icnt, xreg, yreg);
7863 expr1.X_add_number = 2;
7864 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7865 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7866 "neg", "x,w", xreg, xreg);
7867 }
7868 }
7869
7870 /* For consistency checking, verify that all bits are specified either
7871 by the match/mask part of the instruction definition, or by the
7872 operand list. */
7873 static int
7874 validate_mips_insn (opc)
7875 const struct mips_opcode *opc;
7876 {
7877 const char *p = opc->args;
7878 char c;
7879 unsigned long used_bits = opc->mask;
7880
7881 if ((used_bits & opc->match) != opc->match)
7882 {
7883 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7884 opc->name, opc->args);
7885 return 0;
7886 }
7887 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7888 while (*p)
7889 switch (c = *p++)
7890 {
7891 case ',': break;
7892 case '(': break;
7893 case ')': break;
7894 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7895 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7896 case 'A': break;
7897 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7898 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7899 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7900 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7901 case 'F': break;
7902 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7903 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7904 case 'I': break;
7905 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7906 case 'L': break;
7907 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7908 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7909 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7910 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7911 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7912 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7913 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7914 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7915 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7916 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7917 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7918 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7919 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7920 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7921 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7922 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7923 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7924 case 'f': break;
7925 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7926 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7927 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7928 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7929 case 'l': break;
7930 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7931 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7932 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7933 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7934 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7935 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7936 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7937 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7938 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7939 case 'x': break;
7940 case 'z': break;
7941 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7942 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7943 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7944 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
7945 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
7946 case '[': break;
7947 case ']': break;
7948 default:
7949 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7950 c, opc->name, opc->args);
7951 return 0;
7952 }
7953 #undef USE_BITS
7954 if (used_bits != 0xffffffff)
7955 {
7956 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7957 ~used_bits & 0xffffffff, opc->name, opc->args);
7958 return 0;
7959 }
7960 return 1;
7961 }
7962
7963 /* This routine assembles an instruction into its binary format. As a
7964 side effect, it sets one of the global variables imm_reloc or
7965 offset_reloc to the type of relocation to do if one of the operands
7966 is an address expression. */
7967
7968 static void
7969 mips_ip (str, ip)
7970 char *str;
7971 struct mips_cl_insn *ip;
7972 {
7973 char *s;
7974 const char *args;
7975 char c = 0;
7976 struct mips_opcode *insn;
7977 char *argsStart;
7978 unsigned int regno;
7979 unsigned int lastregno = 0;
7980 char *s_reset;
7981 char save_c = 0;
7982
7983 insn_error = NULL;
7984
7985 /* If the instruction contains a '.', we first try to match an instruction
7986 including the '.'. Then we try again without the '.'. */
7987 insn = NULL;
7988 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7989 continue;
7990
7991 /* If we stopped on whitespace, then replace the whitespace with null for
7992 the call to hash_find. Save the character we replaced just in case we
7993 have to re-parse the instruction. */
7994 if (ISSPACE (*s))
7995 {
7996 save_c = *s;
7997 *s++ = '\0';
7998 }
7999
8000 insn = (struct mips_opcode *) hash_find (op_hash, str);
8001
8002 /* If we didn't find the instruction in the opcode table, try again, but
8003 this time with just the instruction up to, but not including the
8004 first '.'. */
8005 if (insn == NULL)
8006 {
8007 /* Restore the character we overwrite above (if any). */
8008 if (save_c)
8009 *(--s) = save_c;
8010
8011 /* Scan up to the first '.' or whitespace. */
8012 for (s = str;
8013 *s != '\0' && *s != '.' && !ISSPACE (*s);
8014 ++s)
8015 continue;
8016
8017 /* If we did not find a '.', then we can quit now. */
8018 if (*s != '.')
8019 {
8020 insn_error = "unrecognized opcode";
8021 return;
8022 }
8023
8024 /* Lookup the instruction in the hash table. */
8025 *s++ = '\0';
8026 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8027 {
8028 insn_error = "unrecognized opcode";
8029 return;
8030 }
8031 }
8032
8033 argsStart = s;
8034 for (;;)
8035 {
8036 boolean ok;
8037
8038 assert (strcmp (insn->name, str) == 0);
8039
8040 if (OPCODE_IS_MEMBER (insn,
8041 (mips_opts.isa
8042 | (file_ase_mips16 ? INSN_MIPS16 : 0)
8043 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8044 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8045 mips_arch))
8046 ok = true;
8047 else
8048 ok = false;
8049
8050 if (insn->pinfo != INSN_MACRO)
8051 {
8052 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8053 ok = false;
8054 }
8055
8056 if (! ok)
8057 {
8058 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8059 && strcmp (insn->name, insn[1].name) == 0)
8060 {
8061 ++insn;
8062 continue;
8063 }
8064 else
8065 {
8066 if (!insn_error)
8067 {
8068 static char buf[100];
8069 if (mips_arch_info->is_isa)
8070 sprintf (buf,
8071 _("opcode not supported at this ISA level (%s)"),
8072 mips_cpu_info_from_isa (mips_opts.isa)->name);
8073 else
8074 sprintf (buf,
8075 _("opcode not supported on this processor: %s (%s)"),
8076 mips_arch_info->name,
8077 mips_cpu_info_from_isa (mips_opts.isa)->name);
8078 insn_error = buf;
8079 }
8080 if (save_c)
8081 *(--s) = save_c;
8082 return;
8083 }
8084 }
8085
8086 ip->insn_mo = insn;
8087 ip->insn_opcode = insn->match;
8088 insn_error = NULL;
8089 for (args = insn->args;; ++args)
8090 {
8091 int is_mdmx;
8092
8093 s += strspn (s, " \t");
8094 is_mdmx = 0;
8095 switch (*args)
8096 {
8097 case '\0': /* end of args */
8098 if (*s == '\0')
8099 return;
8100 break;
8101
8102 case ',':
8103 if (*s++ == *args)
8104 continue;
8105 s--;
8106 switch (*++args)
8107 {
8108 case 'r':
8109 case 'v':
8110 ip->insn_opcode |= lastregno << OP_SH_RS;
8111 continue;
8112
8113 case 'w':
8114 ip->insn_opcode |= lastregno << OP_SH_RT;
8115 continue;
8116
8117 case 'W':
8118 ip->insn_opcode |= lastregno << OP_SH_FT;
8119 continue;
8120
8121 case 'V':
8122 ip->insn_opcode |= lastregno << OP_SH_FS;
8123 continue;
8124 }
8125 break;
8126
8127 case '(':
8128 /* Handle optional base register.
8129 Either the base register is omitted or
8130 we must have a left paren. */
8131 /* This is dependent on the next operand specifier
8132 is a base register specification. */
8133 assert (args[1] == 'b' || args[1] == '5'
8134 || args[1] == '-' || args[1] == '4');
8135 if (*s == '\0')
8136 return;
8137
8138 case ')': /* these must match exactly */
8139 case '[':
8140 case ']':
8141 if (*s++ == *args)
8142 continue;
8143 break;
8144
8145 case '<': /* must be at least one digit */
8146 /*
8147 * According to the manual, if the shift amount is greater
8148 * than 31 or less than 0, then the shift amount should be
8149 * mod 32. In reality the mips assembler issues an error.
8150 * We issue a warning and mask out all but the low 5 bits.
8151 */
8152 my_getExpression (&imm_expr, s);
8153 check_absolute_expr (ip, &imm_expr);
8154 if ((unsigned long) imm_expr.X_add_number > 31)
8155 {
8156 as_warn (_("Improper shift amount (%lu)"),
8157 (unsigned long) imm_expr.X_add_number);
8158 imm_expr.X_add_number &= OP_MASK_SHAMT;
8159 }
8160 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8161 imm_expr.X_op = O_absent;
8162 s = expr_end;
8163 continue;
8164
8165 case '>': /* shift amount minus 32 */
8166 my_getExpression (&imm_expr, s);
8167 check_absolute_expr (ip, &imm_expr);
8168 if ((unsigned long) imm_expr.X_add_number < 32
8169 || (unsigned long) imm_expr.X_add_number > 63)
8170 break;
8171 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8172 imm_expr.X_op = O_absent;
8173 s = expr_end;
8174 continue;
8175
8176 case 'k': /* cache code */
8177 case 'h': /* prefx code */
8178 my_getExpression (&imm_expr, s);
8179 check_absolute_expr (ip, &imm_expr);
8180 if ((unsigned long) imm_expr.X_add_number > 31)
8181 {
8182 as_warn (_("Invalid value for `%s' (%lu)"),
8183 ip->insn_mo->name,
8184 (unsigned long) imm_expr.X_add_number);
8185 imm_expr.X_add_number &= 0x1f;
8186 }
8187 if (*args == 'k')
8188 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8189 else
8190 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8191 imm_expr.X_op = O_absent;
8192 s = expr_end;
8193 continue;
8194
8195 case 'c': /* break code */
8196 my_getExpression (&imm_expr, s);
8197 check_absolute_expr (ip, &imm_expr);
8198 if ((unsigned long) imm_expr.X_add_number > 1023)
8199 {
8200 as_warn (_("Illegal break code (%lu)"),
8201 (unsigned long) imm_expr.X_add_number);
8202 imm_expr.X_add_number &= OP_MASK_CODE;
8203 }
8204 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8205 imm_expr.X_op = O_absent;
8206 s = expr_end;
8207 continue;
8208
8209 case 'q': /* lower break code */
8210 my_getExpression (&imm_expr, s);
8211 check_absolute_expr (ip, &imm_expr);
8212 if ((unsigned long) imm_expr.X_add_number > 1023)
8213 {
8214 as_warn (_("Illegal lower break code (%lu)"),
8215 (unsigned long) imm_expr.X_add_number);
8216 imm_expr.X_add_number &= OP_MASK_CODE2;
8217 }
8218 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8219 imm_expr.X_op = O_absent;
8220 s = expr_end;
8221 continue;
8222
8223 case 'B': /* 20-bit syscall/break code. */
8224 my_getExpression (&imm_expr, s);
8225 check_absolute_expr (ip, &imm_expr);
8226 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8227 as_warn (_("Illegal 20-bit code (%lu)"),
8228 (unsigned long) imm_expr.X_add_number);
8229 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8230 imm_expr.X_op = O_absent;
8231 s = expr_end;
8232 continue;
8233
8234 case 'C': /* Coprocessor code */
8235 my_getExpression (&imm_expr, s);
8236 check_absolute_expr (ip, &imm_expr);
8237 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8238 {
8239 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8240 (unsigned long) imm_expr.X_add_number);
8241 imm_expr.X_add_number &= ((1 << 25) - 1);
8242 }
8243 ip->insn_opcode |= imm_expr.X_add_number;
8244 imm_expr.X_op = O_absent;
8245 s = expr_end;
8246 continue;
8247
8248 case 'J': /* 19-bit wait code. */
8249 my_getExpression (&imm_expr, s);
8250 check_absolute_expr (ip, &imm_expr);
8251 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8252 as_warn (_("Illegal 19-bit code (%lu)"),
8253 (unsigned long) imm_expr.X_add_number);
8254 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8255 imm_expr.X_op = O_absent;
8256 s = expr_end;
8257 continue;
8258
8259 case 'P': /* Performance register */
8260 my_getExpression (&imm_expr, s);
8261 check_absolute_expr (ip, &imm_expr);
8262 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8263 {
8264 as_warn (_("Invalid performance register (%lu)"),
8265 (unsigned long) imm_expr.X_add_number);
8266 imm_expr.X_add_number &= OP_MASK_PERFREG;
8267 }
8268 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8269 imm_expr.X_op = O_absent;
8270 s = expr_end;
8271 continue;
8272
8273 case 'b': /* base register */
8274 case 'd': /* destination register */
8275 case 's': /* source register */
8276 case 't': /* target register */
8277 case 'r': /* both target and source */
8278 case 'v': /* both dest and source */
8279 case 'w': /* both dest and target */
8280 case 'E': /* coprocessor target register */
8281 case 'G': /* coprocessor destination register */
8282 case 'x': /* ignore register name */
8283 case 'z': /* must be zero register */
8284 case 'U': /* destination register (clo/clz). */
8285 s_reset = s;
8286 if (s[0] == '$')
8287 {
8288
8289 if (ISDIGIT (s[1]))
8290 {
8291 ++s;
8292 regno = 0;
8293 do
8294 {
8295 regno *= 10;
8296 regno += *s - '0';
8297 ++s;
8298 }
8299 while (ISDIGIT (*s));
8300 if (regno > 31)
8301 as_bad (_("Invalid register number (%d)"), regno);
8302 }
8303 else if (*args == 'E' || *args == 'G')
8304 goto notreg;
8305 else
8306 {
8307 if (s[1] == 'r' && s[2] == 'a')
8308 {
8309 s += 3;
8310 regno = RA;
8311 }
8312 else if (s[1] == 'f' && s[2] == 'p')
8313 {
8314 s += 3;
8315 regno = FP;
8316 }
8317 else if (s[1] == 's' && s[2] == 'p')
8318 {
8319 s += 3;
8320 regno = SP;
8321 }
8322 else if (s[1] == 'g' && s[2] == 'p')
8323 {
8324 s += 3;
8325 regno = GP;
8326 }
8327 else if (s[1] == 'a' && s[2] == 't')
8328 {
8329 s += 3;
8330 regno = AT;
8331 }
8332 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8333 {
8334 s += 4;
8335 regno = KT0;
8336 }
8337 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8338 {
8339 s += 4;
8340 regno = KT1;
8341 }
8342 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8343 {
8344 s += 5;
8345 regno = ZERO;
8346 }
8347 else if (itbl_have_entries)
8348 {
8349 char *p, *n;
8350 unsigned long r;
8351
8352 p = s + 1; /* advance past '$' */
8353 n = itbl_get_field (&p); /* n is name */
8354
8355 /* See if this is a register defined in an
8356 itbl entry. */
8357 if (itbl_get_reg_val (n, &r))
8358 {
8359 /* Get_field advances to the start of
8360 the next field, so we need to back
8361 rack to the end of the last field. */
8362 if (p)
8363 s = p - 1;
8364 else
8365 s = strchr (s, '\0');
8366 regno = r;
8367 }
8368 else
8369 goto notreg;
8370 }
8371 else
8372 goto notreg;
8373 }
8374 if (regno == AT
8375 && ! mips_opts.noat
8376 && *args != 'E'
8377 && *args != 'G')
8378 as_warn (_("Used $at without \".set noat\""));
8379 c = *args;
8380 if (*s == ' ')
8381 ++s;
8382 if (args[1] != *s)
8383 {
8384 if (c == 'r' || c == 'v' || c == 'w')
8385 {
8386 regno = lastregno;
8387 s = s_reset;
8388 ++args;
8389 }
8390 }
8391 /* 'z' only matches $0. */
8392 if (c == 'z' && regno != 0)
8393 break;
8394
8395 /* Now that we have assembled one operand, we use the args string
8396 * to figure out where it goes in the instruction. */
8397 switch (c)
8398 {
8399 case 'r':
8400 case 's':
8401 case 'v':
8402 case 'b':
8403 ip->insn_opcode |= regno << OP_SH_RS;
8404 break;
8405 case 'd':
8406 case 'G':
8407 ip->insn_opcode |= regno << OP_SH_RD;
8408 break;
8409 case 'U':
8410 ip->insn_opcode |= regno << OP_SH_RD;
8411 ip->insn_opcode |= regno << OP_SH_RT;
8412 break;
8413 case 'w':
8414 case 't':
8415 case 'E':
8416 ip->insn_opcode |= regno << OP_SH_RT;
8417 break;
8418 case 'x':
8419 /* This case exists because on the r3000 trunc
8420 expands into a macro which requires a gp
8421 register. On the r6000 or r4000 it is
8422 assembled into a single instruction which
8423 ignores the register. Thus the insn version
8424 is MIPS_ISA2 and uses 'x', and the macro
8425 version is MIPS_ISA1 and uses 't'. */
8426 break;
8427 case 'z':
8428 /* This case is for the div instruction, which
8429 acts differently if the destination argument
8430 is $0. This only matches $0, and is checked
8431 outside the switch. */
8432 break;
8433 case 'D':
8434 /* Itbl operand; not yet implemented. FIXME ?? */
8435 break;
8436 /* What about all other operands like 'i', which
8437 can be specified in the opcode table? */
8438 }
8439 lastregno = regno;
8440 continue;
8441 }
8442 notreg:
8443 switch (*args++)
8444 {
8445 case 'r':
8446 case 'v':
8447 ip->insn_opcode |= lastregno << OP_SH_RS;
8448 continue;
8449 case 'w':
8450 ip->insn_opcode |= lastregno << OP_SH_RT;
8451 continue;
8452 }
8453 break;
8454
8455 case 'O': /* MDMX alignment immediate constant. */
8456 my_getExpression (&imm_expr, s);
8457 check_absolute_expr (ip, &imm_expr);
8458 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8459 {
8460 as_warn ("Improper align amount (%ld), using low bits",
8461 (long) imm_expr.X_add_number);
8462 imm_expr.X_add_number &= OP_MASK_ALN;
8463 }
8464 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8465 imm_expr.X_op = O_absent;
8466 s = expr_end;
8467 continue;
8468
8469 case 'Q': /* MDMX vector, element sel, or const. */
8470 if (s[0] != '$')
8471 {
8472 /* MDMX Immediate. */
8473 my_getExpression (&imm_expr, s);
8474 check_absolute_expr (ip, &imm_expr);
8475 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8476 {
8477 as_warn (_("Invalid MDMX Immediate (%ld)"),
8478 (long) imm_expr.X_add_number);
8479 imm_expr.X_add_number &= OP_MASK_FT;
8480 }
8481 imm_expr.X_add_number &= OP_MASK_FT;
8482 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8483 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8484 else
8485 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8486 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8487 imm_expr.X_op = O_absent;
8488 s = expr_end;
8489 continue;
8490 }
8491 /* Not MDMX Immediate. Fall through. */
8492 case 'X': /* MDMX destination register. */
8493 case 'Y': /* MDMX source register. */
8494 case 'Z': /* MDMX target register. */
8495 is_mdmx = 1;
8496 case 'D': /* floating point destination register */
8497 case 'S': /* floating point source register */
8498 case 'T': /* floating point target register */
8499 case 'R': /* floating point source register */
8500 case 'V':
8501 case 'W':
8502 s_reset = s;
8503 /* Accept $fN for FP and MDMX register numbers, and in
8504 addition accept $vN for MDMX register numbers. */
8505 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8506 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8507 && ISDIGIT (s[2])))
8508 {
8509 s += 2;
8510 regno = 0;
8511 do
8512 {
8513 regno *= 10;
8514 regno += *s - '0';
8515 ++s;
8516 }
8517 while (ISDIGIT (*s));
8518
8519 if (regno > 31)
8520 as_bad (_("Invalid float register number (%d)"), regno);
8521
8522 if ((regno & 1) != 0
8523 && HAVE_32BIT_FPRS
8524 && ! (strcmp (str, "mtc1") == 0
8525 || strcmp (str, "mfc1") == 0
8526 || strcmp (str, "lwc1") == 0
8527 || strcmp (str, "swc1") == 0
8528 || strcmp (str, "l.s") == 0
8529 || strcmp (str, "s.s") == 0))
8530 as_warn (_("Float register should be even, was %d"),
8531 regno);
8532
8533 c = *args;
8534 if (*s == ' ')
8535 ++s;
8536 if (args[1] != *s)
8537 {
8538 if (c == 'V' || c == 'W')
8539 {
8540 regno = lastregno;
8541 s = s_reset;
8542 ++args;
8543 }
8544 }
8545 switch (c)
8546 {
8547 case 'D':
8548 case 'X':
8549 ip->insn_opcode |= regno << OP_SH_FD;
8550 break;
8551 case 'V':
8552 case 'S':
8553 case 'Y':
8554 ip->insn_opcode |= regno << OP_SH_FS;
8555 break;
8556 case 'Q':
8557 /* This is like 'Z', but also needs to fix the MDMX
8558 vector/scalar select bits. Note that the
8559 scalar immediate case is handled above. */
8560 if (*s == '[')
8561 {
8562 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8563 int max_el = (is_qh ? 3 : 7);
8564 s++;
8565 my_getExpression(&imm_expr, s);
8566 check_absolute_expr (ip, &imm_expr);
8567 s = expr_end;
8568 if (imm_expr.X_add_number > max_el)
8569 as_bad(_("Bad element selector %ld"),
8570 (long) imm_expr.X_add_number);
8571 imm_expr.X_add_number &= max_el;
8572 ip->insn_opcode |= (imm_expr.X_add_number
8573 << (OP_SH_VSEL +
8574 (is_qh ? 2 : 1)));
8575 if (*s != ']')
8576 as_warn(_("Expecting ']' found '%s'"), s);
8577 else
8578 s++;
8579 }
8580 else
8581 {
8582 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8583 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8584 << OP_SH_VSEL);
8585 else
8586 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8587 OP_SH_VSEL);
8588 }
8589 /* Fall through */
8590 case 'W':
8591 case 'T':
8592 case 'Z':
8593 ip->insn_opcode |= regno << OP_SH_FT;
8594 break;
8595 case 'R':
8596 ip->insn_opcode |= regno << OP_SH_FR;
8597 break;
8598 }
8599 lastregno = regno;
8600 continue;
8601 }
8602
8603 switch (*args++)
8604 {
8605 case 'V':
8606 ip->insn_opcode |= lastregno << OP_SH_FS;
8607 continue;
8608 case 'W':
8609 ip->insn_opcode |= lastregno << OP_SH_FT;
8610 continue;
8611 }
8612 break;
8613
8614 case 'I':
8615 my_getExpression (&imm_expr, s);
8616 if (imm_expr.X_op != O_big
8617 && imm_expr.X_op != O_constant)
8618 insn_error = _("absolute expression required");
8619 s = expr_end;
8620 continue;
8621
8622 case 'A':
8623 my_getExpression (&offset_expr, s);
8624 *imm_reloc = BFD_RELOC_32;
8625 s = expr_end;
8626 continue;
8627
8628 case 'F':
8629 case 'L':
8630 case 'f':
8631 case 'l':
8632 {
8633 int f64;
8634 int using_gprs;
8635 char *save_in;
8636 char *err;
8637 unsigned char temp[8];
8638 int len;
8639 unsigned int length;
8640 segT seg;
8641 subsegT subseg;
8642 char *p;
8643
8644 /* These only appear as the last operand in an
8645 instruction, and every instruction that accepts
8646 them in any variant accepts them in all variants.
8647 This means we don't have to worry about backing out
8648 any changes if the instruction does not match.
8649
8650 The difference between them is the size of the
8651 floating point constant and where it goes. For 'F'
8652 and 'L' the constant is 64 bits; for 'f' and 'l' it
8653 is 32 bits. Where the constant is placed is based
8654 on how the MIPS assembler does things:
8655 F -- .rdata
8656 L -- .lit8
8657 f -- immediate value
8658 l -- .lit4
8659
8660 The .lit4 and .lit8 sections are only used if
8661 permitted by the -G argument.
8662
8663 When generating embedded PIC code, we use the
8664 .lit8 section but not the .lit4 section (we can do
8665 .lit4 inline easily; we need to put .lit8
8666 somewhere in the data segment, and using .lit8
8667 permits the linker to eventually combine identical
8668 .lit8 entries).
8669
8670 The code below needs to know whether the target register
8671 is 32 or 64 bits wide. It relies on the fact 'f' and
8672 'F' are used with GPR-based instructions and 'l' and
8673 'L' are used with FPR-based instructions. */
8674
8675 f64 = *args == 'F' || *args == 'L';
8676 using_gprs = *args == 'F' || *args == 'f';
8677
8678 save_in = input_line_pointer;
8679 input_line_pointer = s;
8680 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8681 length = len;
8682 s = input_line_pointer;
8683 input_line_pointer = save_in;
8684 if (err != NULL && *err != '\0')
8685 {
8686 as_bad (_("Bad floating point constant: %s"), err);
8687 memset (temp, '\0', sizeof temp);
8688 length = f64 ? 8 : 4;
8689 }
8690
8691 assert (length == (unsigned) (f64 ? 8 : 4));
8692
8693 if (*args == 'f'
8694 || (*args == 'l'
8695 && (! USE_GLOBAL_POINTER_OPT
8696 || mips_pic == EMBEDDED_PIC
8697 || g_switch_value < 4
8698 || (temp[0] == 0 && temp[1] == 0)
8699 || (temp[2] == 0 && temp[3] == 0))))
8700 {
8701 imm_expr.X_op = O_constant;
8702 if (! target_big_endian)
8703 imm_expr.X_add_number = bfd_getl32 (temp);
8704 else
8705 imm_expr.X_add_number = bfd_getb32 (temp);
8706 }
8707 else if (length > 4
8708 && ! mips_disable_float_construction
8709 /* Constants can only be constructed in GPRs and
8710 copied to FPRs if the GPRs are at least as wide
8711 as the FPRs. Force the constant into memory if
8712 we are using 64-bit FPRs but the GPRs are only
8713 32 bits wide. */
8714 && (using_gprs
8715 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8716 && ((temp[0] == 0 && temp[1] == 0)
8717 || (temp[2] == 0 && temp[3] == 0))
8718 && ((temp[4] == 0 && temp[5] == 0)
8719 || (temp[6] == 0 && temp[7] == 0)))
8720 {
8721 /* The value is simple enough to load with a couple of
8722 instructions. If using 32-bit registers, set
8723 imm_expr to the high order 32 bits and offset_expr to
8724 the low order 32 bits. Otherwise, set imm_expr to
8725 the entire 64 bit constant. */
8726 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8727 {
8728 imm_expr.X_op = O_constant;
8729 offset_expr.X_op = O_constant;
8730 if (! target_big_endian)
8731 {
8732 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8733 offset_expr.X_add_number = bfd_getl32 (temp);
8734 }
8735 else
8736 {
8737 imm_expr.X_add_number = bfd_getb32 (temp);
8738 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8739 }
8740 if (offset_expr.X_add_number == 0)
8741 offset_expr.X_op = O_absent;
8742 }
8743 else if (sizeof (imm_expr.X_add_number) > 4)
8744 {
8745 imm_expr.X_op = O_constant;
8746 if (! target_big_endian)
8747 imm_expr.X_add_number = bfd_getl64 (temp);
8748 else
8749 imm_expr.X_add_number = bfd_getb64 (temp);
8750 }
8751 else
8752 {
8753 imm_expr.X_op = O_big;
8754 imm_expr.X_add_number = 4;
8755 if (! target_big_endian)
8756 {
8757 generic_bignum[0] = bfd_getl16 (temp);
8758 generic_bignum[1] = bfd_getl16 (temp + 2);
8759 generic_bignum[2] = bfd_getl16 (temp + 4);
8760 generic_bignum[3] = bfd_getl16 (temp + 6);
8761 }
8762 else
8763 {
8764 generic_bignum[0] = bfd_getb16 (temp + 6);
8765 generic_bignum[1] = bfd_getb16 (temp + 4);
8766 generic_bignum[2] = bfd_getb16 (temp + 2);
8767 generic_bignum[3] = bfd_getb16 (temp);
8768 }
8769 }
8770 }
8771 else
8772 {
8773 const char *newname;
8774 segT new_seg;
8775
8776 /* Switch to the right section. */
8777 seg = now_seg;
8778 subseg = now_subseg;
8779 switch (*args)
8780 {
8781 default: /* unused default case avoids warnings. */
8782 case 'L':
8783 newname = RDATA_SECTION_NAME;
8784 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8785 || mips_pic == EMBEDDED_PIC)
8786 newname = ".lit8";
8787 break;
8788 case 'F':
8789 if (mips_pic == EMBEDDED_PIC)
8790 newname = ".lit8";
8791 else
8792 newname = RDATA_SECTION_NAME;
8793 break;
8794 case 'l':
8795 assert (!USE_GLOBAL_POINTER_OPT
8796 || g_switch_value >= 4);
8797 newname = ".lit4";
8798 break;
8799 }
8800 new_seg = subseg_new (newname, (subsegT) 0);
8801 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8802 bfd_set_section_flags (stdoutput, new_seg,
8803 (SEC_ALLOC
8804 | SEC_LOAD
8805 | SEC_READONLY
8806 | SEC_DATA));
8807 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8808 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8809 && strcmp (TARGET_OS, "elf") != 0)
8810 record_alignment (new_seg, 4);
8811 else
8812 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8813 if (seg == now_seg)
8814 as_bad (_("Can't use floating point insn in this section"));
8815
8816 /* Set the argument to the current address in the
8817 section. */
8818 offset_expr.X_op = O_symbol;
8819 offset_expr.X_add_symbol =
8820 symbol_new ("L0\001", now_seg,
8821 (valueT) frag_now_fix (), frag_now);
8822 offset_expr.X_add_number = 0;
8823
8824 /* Put the floating point number into the section. */
8825 p = frag_more ((int) length);
8826 memcpy (p, temp, length);
8827
8828 /* Switch back to the original section. */
8829 subseg_set (seg, subseg);
8830 }
8831 }
8832 continue;
8833
8834 case 'i': /* 16 bit unsigned immediate */
8835 case 'j': /* 16 bit signed immediate */
8836 *imm_reloc = BFD_RELOC_LO16;
8837 c = my_getSmallExpression (&imm_expr, s);
8838 if (c != S_EX_NONE)
8839 {
8840 if (c != S_EX_LO)
8841 {
8842 if (c == S_EX_HI)
8843 {
8844 *imm_reloc = BFD_RELOC_HI16_S;
8845 imm_unmatched_hi = true;
8846 }
8847 #ifdef OBJ_ELF
8848 else if (c == S_EX_HIGHEST)
8849 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8850 else if (c == S_EX_HIGHER)
8851 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8852 else if (c == S_EX_GP_REL)
8853 {
8854 /* This occurs in NewABI only. */
8855 c = my_getSmallExpression (&imm_expr, s);
8856 if (c != S_EX_NEG)
8857 as_bad (_("bad composition of relocations"));
8858 else
8859 {
8860 c = my_getSmallExpression (&imm_expr, s);
8861 if (c != S_EX_LO)
8862 as_bad (_("bad composition of relocations"));
8863 else
8864 {
8865 imm_reloc[0] = BFD_RELOC_GPREL16;
8866 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8867 imm_reloc[2] = BFD_RELOC_LO16;
8868 }
8869 }
8870 }
8871 #endif
8872 else
8873 *imm_reloc = BFD_RELOC_HI16;
8874 }
8875 else if (imm_expr.X_op == O_constant)
8876 imm_expr.X_add_number &= 0xffff;
8877 }
8878 if (*args == 'i')
8879 {
8880 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8881 || ((imm_expr.X_add_number < 0
8882 || imm_expr.X_add_number >= 0x10000)
8883 && imm_expr.X_op == O_constant))
8884 {
8885 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8886 !strcmp (insn->name, insn[1].name))
8887 break;
8888 if (imm_expr.X_op == O_constant
8889 || imm_expr.X_op == O_big)
8890 as_bad (_("16 bit expression not in range 0..65535"));
8891 }
8892 }
8893 else
8894 {
8895 int more;
8896 offsetT max;
8897
8898 /* The upper bound should be 0x8000, but
8899 unfortunately the MIPS assembler accepts numbers
8900 from 0x8000 to 0xffff and sign extends them, and
8901 we want to be compatible. We only permit this
8902 extended range for an instruction which does not
8903 provide any further alternates, since those
8904 alternates may handle other cases. People should
8905 use the numbers they mean, rather than relying on
8906 a mysterious sign extension. */
8907 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8908 strcmp (insn->name, insn[1].name) == 0);
8909 if (more)
8910 max = 0x8000;
8911 else
8912 max = 0x10000;
8913 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8914 || ((imm_expr.X_add_number < -0x8000
8915 || imm_expr.X_add_number >= max)
8916 && imm_expr.X_op == O_constant)
8917 || (more
8918 && imm_expr.X_add_number < 0
8919 && HAVE_64BIT_GPRS
8920 && imm_expr.X_unsigned
8921 && sizeof (imm_expr.X_add_number) <= 4))
8922 {
8923 if (more)
8924 break;
8925 if (imm_expr.X_op == O_constant
8926 || imm_expr.X_op == O_big)
8927 as_bad (_("16 bit expression not in range -32768..32767"));
8928 }
8929 }
8930 s = expr_end;
8931 continue;
8932
8933 case 'o': /* 16 bit offset */
8934 c = my_getSmallExpression (&offset_expr, s);
8935
8936 /* If this value won't fit into a 16 bit offset, then go
8937 find a macro that will generate the 32 bit offset
8938 code pattern. */
8939 if (c == S_EX_NONE
8940 && (offset_expr.X_op != O_constant
8941 || offset_expr.X_add_number >= 0x8000
8942 || offset_expr.X_add_number < -0x8000))
8943 break;
8944
8945 if (c == S_EX_HI)
8946 {
8947 if (offset_expr.X_op != O_constant)
8948 break;
8949 offset_expr.X_add_number =
8950 (offset_expr.X_add_number >> 16) & 0xffff;
8951 }
8952 *offset_reloc = BFD_RELOC_LO16;
8953 s = expr_end;
8954 continue;
8955
8956 case 'p': /* pc relative offset */
8957 if (mips_pic == EMBEDDED_PIC)
8958 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8959 else
8960 *offset_reloc = BFD_RELOC_16_PCREL;
8961 my_getExpression (&offset_expr, s);
8962 s = expr_end;
8963 continue;
8964
8965 case 'u': /* upper 16 bits */
8966 c = my_getSmallExpression (&imm_expr, s);
8967 *imm_reloc = BFD_RELOC_LO16;
8968 if (c != S_EX_NONE)
8969 {
8970 if (c != S_EX_LO)
8971 {
8972 if (c == S_EX_HI)
8973 {
8974 *imm_reloc = BFD_RELOC_HI16_S;
8975 imm_unmatched_hi = true;
8976 }
8977 #ifdef OBJ_ELF
8978 else if (c == S_EX_HIGHEST)
8979 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8980 else if (c == S_EX_GP_REL)
8981 {
8982 /* This occurs in NewABI only. */
8983 c = my_getSmallExpression (&imm_expr, s);
8984 if (c != S_EX_NEG)
8985 as_bad (_("bad composition of relocations"));
8986 else
8987 {
8988 c = my_getSmallExpression (&imm_expr, s);
8989 if (c != S_EX_HI)
8990 as_bad (_("bad composition of relocations"));
8991 else
8992 {
8993 imm_reloc[0] = BFD_RELOC_GPREL16;
8994 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8995 imm_reloc[2] = BFD_RELOC_HI16_S;
8996 }
8997 }
8998 }
8999 #endif
9000 else
9001 *imm_reloc = BFD_RELOC_HI16;
9002 }
9003 else if (imm_expr.X_op == O_constant)
9004 imm_expr.X_add_number &= 0xffff;
9005 }
9006 else if (imm_expr.X_op == O_constant
9007 && (imm_expr.X_add_number < 0
9008 || imm_expr.X_add_number >= 0x10000))
9009 as_bad (_("lui expression not in range 0..65535"));
9010 s = expr_end;
9011 continue;
9012
9013 case 'a': /* 26 bit address */
9014 my_getExpression (&offset_expr, s);
9015 s = expr_end;
9016 *offset_reloc = BFD_RELOC_MIPS_JMP;
9017 continue;
9018
9019 case 'N': /* 3 bit branch condition code */
9020 case 'M': /* 3 bit compare condition code */
9021 if (strncmp (s, "$fcc", 4) != 0)
9022 break;
9023 s += 4;
9024 regno = 0;
9025 do
9026 {
9027 regno *= 10;
9028 regno += *s - '0';
9029 ++s;
9030 }
9031 while (ISDIGIT (*s));
9032 if (regno > 7)
9033 as_bad (_("invalid condition code register $fcc%d"), regno);
9034 if (*args == 'N')
9035 ip->insn_opcode |= regno << OP_SH_BCC;
9036 else
9037 ip->insn_opcode |= regno << OP_SH_CCC;
9038 continue;
9039
9040 case 'H':
9041 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9042 s += 2;
9043 if (ISDIGIT (*s))
9044 {
9045 c = 0;
9046 do
9047 {
9048 c *= 10;
9049 c += *s - '0';
9050 ++s;
9051 }
9052 while (ISDIGIT (*s));
9053 }
9054 else
9055 c = 8; /* Invalid sel value. */
9056
9057 if (c > 7)
9058 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9059 ip->insn_opcode |= c;
9060 continue;
9061
9062 case 'e':
9063 /* Must be at least one digit. */
9064 my_getExpression (&imm_expr, s);
9065 check_absolute_expr (ip, &imm_expr);
9066
9067 if ((unsigned long) imm_expr.X_add_number
9068 > (unsigned long) OP_MASK_VECBYTE)
9069 {
9070 as_bad (_("bad byte vector index (%ld)"),
9071 (long) imm_expr.X_add_number);
9072 imm_expr.X_add_number = 0;
9073 }
9074
9075 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9076 imm_expr.X_op = O_absent;
9077 s = expr_end;
9078 continue;
9079
9080 case '%':
9081 my_getExpression (&imm_expr, s);
9082 check_absolute_expr (ip, &imm_expr);
9083
9084 if ((unsigned long) imm_expr.X_add_number
9085 > (unsigned long) OP_MASK_VECALIGN)
9086 {
9087 as_bad (_("bad byte vector index (%ld)"),
9088 (long) imm_expr.X_add_number);
9089 imm_expr.X_add_number = 0;
9090 }
9091
9092 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9093 imm_expr.X_op = O_absent;
9094 s = expr_end;
9095 continue;
9096
9097 default:
9098 as_bad (_("bad char = '%c'\n"), *args);
9099 internalError ();
9100 }
9101 break;
9102 }
9103 /* Args don't match. */
9104 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9105 !strcmp (insn->name, insn[1].name))
9106 {
9107 ++insn;
9108 s = argsStart;
9109 insn_error = _("illegal operands");
9110 continue;
9111 }
9112 if (save_c)
9113 *(--s) = save_c;
9114 insn_error = _("illegal operands");
9115 return;
9116 }
9117 }
9118
9119 /* This routine assembles an instruction into its binary format when
9120 assembling for the mips16. As a side effect, it sets one of the
9121 global variables imm_reloc or offset_reloc to the type of
9122 relocation to do if one of the operands is an address expression.
9123 It also sets mips16_small and mips16_ext if the user explicitly
9124 requested a small or extended instruction. */
9125
9126 static void
9127 mips16_ip (str, ip)
9128 char *str;
9129 struct mips_cl_insn *ip;
9130 {
9131 char *s;
9132 const char *args;
9133 struct mips_opcode *insn;
9134 char *argsstart;
9135 unsigned int regno;
9136 unsigned int lastregno = 0;
9137 char *s_reset;
9138
9139 insn_error = NULL;
9140
9141 mips16_small = false;
9142 mips16_ext = false;
9143
9144 for (s = str; ISLOWER (*s); ++s)
9145 ;
9146 switch (*s)
9147 {
9148 case '\0':
9149 break;
9150
9151 case ' ':
9152 *s++ = '\0';
9153 break;
9154
9155 case '.':
9156 if (s[1] == 't' && s[2] == ' ')
9157 {
9158 *s = '\0';
9159 mips16_small = true;
9160 s += 3;
9161 break;
9162 }
9163 else if (s[1] == 'e' && s[2] == ' ')
9164 {
9165 *s = '\0';
9166 mips16_ext = true;
9167 s += 3;
9168 break;
9169 }
9170 /* Fall through. */
9171 default:
9172 insn_error = _("unknown opcode");
9173 return;
9174 }
9175
9176 if (mips_opts.noautoextend && ! mips16_ext)
9177 mips16_small = true;
9178
9179 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9180 {
9181 insn_error = _("unrecognized opcode");
9182 return;
9183 }
9184
9185 argsstart = s;
9186 for (;;)
9187 {
9188 assert (strcmp (insn->name, str) == 0);
9189
9190 ip->insn_mo = insn;
9191 ip->insn_opcode = insn->match;
9192 ip->use_extend = false;
9193 imm_expr.X_op = O_absent;
9194 imm_reloc[0] = BFD_RELOC_UNUSED;
9195 imm_reloc[1] = BFD_RELOC_UNUSED;
9196 imm_reloc[2] = BFD_RELOC_UNUSED;
9197 offset_expr.X_op = O_absent;
9198 offset_reloc[0] = BFD_RELOC_UNUSED;
9199 offset_reloc[1] = BFD_RELOC_UNUSED;
9200 offset_reloc[2] = BFD_RELOC_UNUSED;
9201 for (args = insn->args; 1; ++args)
9202 {
9203 int c;
9204
9205 if (*s == ' ')
9206 ++s;
9207
9208 /* In this switch statement we call break if we did not find
9209 a match, continue if we did find a match, or return if we
9210 are done. */
9211
9212 c = *args;
9213 switch (c)
9214 {
9215 case '\0':
9216 if (*s == '\0')
9217 {
9218 /* Stuff the immediate value in now, if we can. */
9219 if (imm_expr.X_op == O_constant
9220 && *imm_reloc > BFD_RELOC_UNUSED
9221 && insn->pinfo != INSN_MACRO)
9222 {
9223 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9224 imm_expr.X_add_number, true, mips16_small,
9225 mips16_ext, &ip->insn_opcode,
9226 &ip->use_extend, &ip->extend);
9227 imm_expr.X_op = O_absent;
9228 *imm_reloc = BFD_RELOC_UNUSED;
9229 }
9230
9231 return;
9232 }
9233 break;
9234
9235 case ',':
9236 if (*s++ == c)
9237 continue;
9238 s--;
9239 switch (*++args)
9240 {
9241 case 'v':
9242 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9243 continue;
9244 case 'w':
9245 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9246 continue;
9247 }
9248 break;
9249
9250 case '(':
9251 case ')':
9252 if (*s++ == c)
9253 continue;
9254 break;
9255
9256 case 'v':
9257 case 'w':
9258 if (s[0] != '$')
9259 {
9260 if (c == 'v')
9261 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9262 else
9263 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9264 ++args;
9265 continue;
9266 }
9267 /* Fall through. */
9268 case 'x':
9269 case 'y':
9270 case 'z':
9271 case 'Z':
9272 case '0':
9273 case 'S':
9274 case 'R':
9275 case 'X':
9276 case 'Y':
9277 if (s[0] != '$')
9278 break;
9279 s_reset = s;
9280 if (ISDIGIT (s[1]))
9281 {
9282 ++s;
9283 regno = 0;
9284 do
9285 {
9286 regno *= 10;
9287 regno += *s - '0';
9288 ++s;
9289 }
9290 while (ISDIGIT (*s));
9291 if (regno > 31)
9292 {
9293 as_bad (_("invalid register number (%d)"), regno);
9294 regno = 2;
9295 }
9296 }
9297 else
9298 {
9299 if (s[1] == 'r' && s[2] == 'a')
9300 {
9301 s += 3;
9302 regno = RA;
9303 }
9304 else if (s[1] == 'f' && s[2] == 'p')
9305 {
9306 s += 3;
9307 regno = FP;
9308 }
9309 else if (s[1] == 's' && s[2] == 'p')
9310 {
9311 s += 3;
9312 regno = SP;
9313 }
9314 else if (s[1] == 'g' && s[2] == 'p')
9315 {
9316 s += 3;
9317 regno = GP;
9318 }
9319 else if (s[1] == 'a' && s[2] == 't')
9320 {
9321 s += 3;
9322 regno = AT;
9323 }
9324 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9325 {
9326 s += 4;
9327 regno = KT0;
9328 }
9329 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9330 {
9331 s += 4;
9332 regno = KT1;
9333 }
9334 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9335 {
9336 s += 5;
9337 regno = ZERO;
9338 }
9339 else
9340 break;
9341 }
9342
9343 if (*s == ' ')
9344 ++s;
9345 if (args[1] != *s)
9346 {
9347 if (c == 'v' || c == 'w')
9348 {
9349 regno = mips16_to_32_reg_map[lastregno];
9350 s = s_reset;
9351 ++args;
9352 }
9353 }
9354
9355 switch (c)
9356 {
9357 case 'x':
9358 case 'y':
9359 case 'z':
9360 case 'v':
9361 case 'w':
9362 case 'Z':
9363 regno = mips32_to_16_reg_map[regno];
9364 break;
9365
9366 case '0':
9367 if (regno != 0)
9368 regno = ILLEGAL_REG;
9369 break;
9370
9371 case 'S':
9372 if (regno != SP)
9373 regno = ILLEGAL_REG;
9374 break;
9375
9376 case 'R':
9377 if (regno != RA)
9378 regno = ILLEGAL_REG;
9379 break;
9380
9381 case 'X':
9382 case 'Y':
9383 if (regno == AT && ! mips_opts.noat)
9384 as_warn (_("used $at without \".set noat\""));
9385 break;
9386
9387 default:
9388 internalError ();
9389 }
9390
9391 if (regno == ILLEGAL_REG)
9392 break;
9393
9394 switch (c)
9395 {
9396 case 'x':
9397 case 'v':
9398 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9399 break;
9400 case 'y':
9401 case 'w':
9402 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9403 break;
9404 case 'z':
9405 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9406 break;
9407 case 'Z':
9408 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9409 case '0':
9410 case 'S':
9411 case 'R':
9412 break;
9413 case 'X':
9414 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9415 break;
9416 case 'Y':
9417 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9418 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9419 break;
9420 default:
9421 internalError ();
9422 }
9423
9424 lastregno = regno;
9425 continue;
9426
9427 case 'P':
9428 if (strncmp (s, "$pc", 3) == 0)
9429 {
9430 s += 3;
9431 continue;
9432 }
9433 break;
9434
9435 case '<':
9436 case '>':
9437 case '[':
9438 case ']':
9439 case '4':
9440 case '5':
9441 case 'H':
9442 case 'W':
9443 case 'D':
9444 case 'j':
9445 case '8':
9446 case 'V':
9447 case 'C':
9448 case 'U':
9449 case 'k':
9450 case 'K':
9451 if (s[0] == '%'
9452 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9453 {
9454 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9455 and generate the appropriate reloc. If the text
9456 inside %gprel is not a symbol name with an
9457 optional offset, then we generate a normal reloc
9458 and will probably fail later. */
9459 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9460 if (imm_expr.X_op == O_symbol)
9461 {
9462 mips16_ext = true;
9463 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9464 s = expr_end;
9465 ip->use_extend = true;
9466 ip->extend = 0;
9467 continue;
9468 }
9469 }
9470 else
9471 {
9472 /* Just pick up a normal expression. */
9473 my_getExpression (&imm_expr, s);
9474 }
9475
9476 if (imm_expr.X_op == O_register)
9477 {
9478 /* What we thought was an expression turned out to
9479 be a register. */
9480
9481 if (s[0] == '(' && args[1] == '(')
9482 {
9483 /* It looks like the expression was omitted
9484 before a register indirection, which means
9485 that the expression is implicitly zero. We
9486 still set up imm_expr, so that we handle
9487 explicit extensions correctly. */
9488 imm_expr.X_op = O_constant;
9489 imm_expr.X_add_number = 0;
9490 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9491 continue;
9492 }
9493
9494 break;
9495 }
9496
9497 /* We need to relax this instruction. */
9498 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9499 s = expr_end;
9500 continue;
9501
9502 case 'p':
9503 case 'q':
9504 case 'A':
9505 case 'B':
9506 case 'E':
9507 /* We use offset_reloc rather than imm_reloc for the PC
9508 relative operands. This lets macros with both
9509 immediate and address operands work correctly. */
9510 my_getExpression (&offset_expr, s);
9511
9512 if (offset_expr.X_op == O_register)
9513 break;
9514
9515 /* We need to relax this instruction. */
9516 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9517 s = expr_end;
9518 continue;
9519
9520 case '6': /* break code */
9521 my_getExpression (&imm_expr, s);
9522 check_absolute_expr (ip, &imm_expr);
9523 if ((unsigned long) imm_expr.X_add_number > 63)
9524 {
9525 as_warn (_("Invalid value for `%s' (%lu)"),
9526 ip->insn_mo->name,
9527 (unsigned long) imm_expr.X_add_number);
9528 imm_expr.X_add_number &= 0x3f;
9529 }
9530 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9531 imm_expr.X_op = O_absent;
9532 s = expr_end;
9533 continue;
9534
9535 case 'a': /* 26 bit address */
9536 my_getExpression (&offset_expr, s);
9537 s = expr_end;
9538 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9539 ip->insn_opcode <<= 16;
9540 continue;
9541
9542 case 'l': /* register list for entry macro */
9543 case 'L': /* register list for exit macro */
9544 {
9545 int mask;
9546
9547 if (c == 'l')
9548 mask = 0;
9549 else
9550 mask = 7 << 3;
9551 while (*s != '\0')
9552 {
9553 int freg, reg1, reg2;
9554
9555 while (*s == ' ' || *s == ',')
9556 ++s;
9557 if (*s != '$')
9558 {
9559 as_bad (_("can't parse register list"));
9560 break;
9561 }
9562 ++s;
9563 if (*s != 'f')
9564 freg = 0;
9565 else
9566 {
9567 freg = 1;
9568 ++s;
9569 }
9570 reg1 = 0;
9571 while (ISDIGIT (*s))
9572 {
9573 reg1 *= 10;
9574 reg1 += *s - '0';
9575 ++s;
9576 }
9577 if (*s == ' ')
9578 ++s;
9579 if (*s != '-')
9580 reg2 = reg1;
9581 else
9582 {
9583 ++s;
9584 if (*s != '$')
9585 break;
9586 ++s;
9587 if (freg)
9588 {
9589 if (*s == 'f')
9590 ++s;
9591 else
9592 {
9593 as_bad (_("invalid register list"));
9594 break;
9595 }
9596 }
9597 reg2 = 0;
9598 while (ISDIGIT (*s))
9599 {
9600 reg2 *= 10;
9601 reg2 += *s - '0';
9602 ++s;
9603 }
9604 }
9605 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9606 {
9607 mask &= ~ (7 << 3);
9608 mask |= 5 << 3;
9609 }
9610 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9611 {
9612 mask &= ~ (7 << 3);
9613 mask |= 6 << 3;
9614 }
9615 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9616 mask |= (reg2 - 3) << 3;
9617 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9618 mask |= (reg2 - 15) << 1;
9619 else if (reg1 == RA && reg2 == RA)
9620 mask |= 1;
9621 else
9622 {
9623 as_bad (_("invalid register list"));
9624 break;
9625 }
9626 }
9627 /* The mask is filled in in the opcode table for the
9628 benefit of the disassembler. We remove it before
9629 applying the actual mask. */
9630 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9631 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9632 }
9633 continue;
9634
9635 case 'e': /* extend code */
9636 my_getExpression (&imm_expr, s);
9637 check_absolute_expr (ip, &imm_expr);
9638 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9639 {
9640 as_warn (_("Invalid value for `%s' (%lu)"),
9641 ip->insn_mo->name,
9642 (unsigned long) imm_expr.X_add_number);
9643 imm_expr.X_add_number &= 0x7ff;
9644 }
9645 ip->insn_opcode |= imm_expr.X_add_number;
9646 imm_expr.X_op = O_absent;
9647 s = expr_end;
9648 continue;
9649
9650 default:
9651 internalError ();
9652 }
9653 break;
9654 }
9655
9656 /* Args don't match. */
9657 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9658 strcmp (insn->name, insn[1].name) == 0)
9659 {
9660 ++insn;
9661 s = argsstart;
9662 continue;
9663 }
9664
9665 insn_error = _("illegal operands");
9666
9667 return;
9668 }
9669 }
9670
9671 /* This structure holds information we know about a mips16 immediate
9672 argument type. */
9673
9674 struct mips16_immed_operand
9675 {
9676 /* The type code used in the argument string in the opcode table. */
9677 int type;
9678 /* The number of bits in the short form of the opcode. */
9679 int nbits;
9680 /* The number of bits in the extended form of the opcode. */
9681 int extbits;
9682 /* The amount by which the short form is shifted when it is used;
9683 for example, the sw instruction has a shift count of 2. */
9684 int shift;
9685 /* The amount by which the short form is shifted when it is stored
9686 into the instruction code. */
9687 int op_shift;
9688 /* Non-zero if the short form is unsigned. */
9689 int unsp;
9690 /* Non-zero if the extended form is unsigned. */
9691 int extu;
9692 /* Non-zero if the value is PC relative. */
9693 int pcrel;
9694 };
9695
9696 /* The mips16 immediate operand types. */
9697
9698 static const struct mips16_immed_operand mips16_immed_operands[] =
9699 {
9700 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9701 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9702 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9703 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9704 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9705 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9706 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9707 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9708 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9709 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9710 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9711 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9712 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9713 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9714 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9715 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9716 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9717 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9718 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9719 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9720 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9721 };
9722
9723 #define MIPS16_NUM_IMMED \
9724 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9725
9726 /* Handle a mips16 instruction with an immediate value. This or's the
9727 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9728 whether an extended value is needed; if one is needed, it sets
9729 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9730 If SMALL is true, an unextended opcode was explicitly requested.
9731 If EXT is true, an extended opcode was explicitly requested. If
9732 WARN is true, warn if EXT does not match reality. */
9733
9734 static void
9735 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9736 extend)
9737 char *file;
9738 unsigned int line;
9739 int type;
9740 offsetT val;
9741 boolean warn;
9742 boolean small;
9743 boolean ext;
9744 unsigned long *insn;
9745 boolean *use_extend;
9746 unsigned short *extend;
9747 {
9748 register const struct mips16_immed_operand *op;
9749 int mintiny, maxtiny;
9750 boolean needext;
9751
9752 op = mips16_immed_operands;
9753 while (op->type != type)
9754 {
9755 ++op;
9756 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9757 }
9758
9759 if (op->unsp)
9760 {
9761 if (type == '<' || type == '>' || type == '[' || type == ']')
9762 {
9763 mintiny = 1;
9764 maxtiny = 1 << op->nbits;
9765 }
9766 else
9767 {
9768 mintiny = 0;
9769 maxtiny = (1 << op->nbits) - 1;
9770 }
9771 }
9772 else
9773 {
9774 mintiny = - (1 << (op->nbits - 1));
9775 maxtiny = (1 << (op->nbits - 1)) - 1;
9776 }
9777
9778 /* Branch offsets have an implicit 0 in the lowest bit. */
9779 if (type == 'p' || type == 'q')
9780 val /= 2;
9781
9782 if ((val & ((1 << op->shift) - 1)) != 0
9783 || val < (mintiny << op->shift)
9784 || val > (maxtiny << op->shift))
9785 needext = true;
9786 else
9787 needext = false;
9788
9789 if (warn && ext && ! needext)
9790 as_warn_where (file, line,
9791 _("extended operand requested but not required"));
9792 if (small && needext)
9793 as_bad_where (file, line, _("invalid unextended operand value"));
9794
9795 if (small || (! ext && ! needext))
9796 {
9797 int insnval;
9798
9799 *use_extend = false;
9800 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9801 insnval <<= op->op_shift;
9802 *insn |= insnval;
9803 }
9804 else
9805 {
9806 long minext, maxext;
9807 int extval;
9808
9809 if (op->extu)
9810 {
9811 minext = 0;
9812 maxext = (1 << op->extbits) - 1;
9813 }
9814 else
9815 {
9816 minext = - (1 << (op->extbits - 1));
9817 maxext = (1 << (op->extbits - 1)) - 1;
9818 }
9819 if (val < minext || val > maxext)
9820 as_bad_where (file, line,
9821 _("operand value out of range for instruction"));
9822
9823 *use_extend = true;
9824 if (op->extbits == 16)
9825 {
9826 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9827 val &= 0x1f;
9828 }
9829 else if (op->extbits == 15)
9830 {
9831 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9832 val &= 0xf;
9833 }
9834 else
9835 {
9836 extval = ((val & 0x1f) << 6) | (val & 0x20);
9837 val = 0;
9838 }
9839
9840 *extend = (unsigned short) extval;
9841 *insn |= val;
9842 }
9843 }
9844 \f
9845 static struct percent_op_match
9846 {
9847 const char *str;
9848 const enum small_ex_type type;
9849 } percent_op[] =
9850 {
9851 {"%lo", S_EX_LO},
9852 #ifdef OBJ_ELF
9853 {"%call_hi", S_EX_CALL_HI},
9854 {"%call_lo", S_EX_CALL_LO},
9855 {"%call16", S_EX_CALL16},
9856 {"%got_disp", S_EX_GOT_DISP},
9857 {"%got_page", S_EX_GOT_PAGE},
9858 {"%got_ofst", S_EX_GOT_OFST},
9859 {"%got_hi", S_EX_GOT_HI},
9860 {"%got_lo", S_EX_GOT_LO},
9861 {"%got", S_EX_GOT},
9862 {"%gp_rel", S_EX_GP_REL},
9863 {"%half", S_EX_HALF},
9864 {"%highest", S_EX_HIGHEST},
9865 {"%higher", S_EX_HIGHER},
9866 {"%neg", S_EX_NEG},
9867 #endif
9868 {"%hi", S_EX_HI}
9869 };
9870
9871 /* Parse small expression input. STR gets adjusted to eat up whitespace.
9872 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9873 can be nested, this is handled by blanking the innermost, parsing the
9874 rest by subsequent calls. */
9875
9876 static int
9877 my_getSmallParser (str, len, nestlevel)
9878 char **str;
9879 unsigned int *len;
9880 int *nestlevel;
9881 {
9882 *len = 0;
9883 *str += strspn (*str, " \t");
9884 /* Check for expression in parentheses. */
9885 if (**str == '(')
9886 {
9887 char *b = *str + 1 + strspn (*str + 1, " \t");
9888 char *e;
9889
9890 /* Check for base register. */
9891 if (b[0] == '$')
9892 {
9893 if (strchr (b, ')')
9894 && (e = b + strcspn (b, ") \t"))
9895 && e - b > 1 && e - b < 4)
9896 {
9897 if ((e - b == 3
9898 && ((b[1] == 'f' && b[2] == 'p')
9899 || (b[1] == 's' && b[2] == 'p')
9900 || (b[1] == 'g' && b[2] == 'p')
9901 || (b[1] == 'a' && b[2] == 't')
9902 || (ISDIGIT (b[1])
9903 && ISDIGIT (b[2]))))
9904 || (ISDIGIT (b[1])))
9905 {
9906 *len = strcspn (*str, ")") + 1;
9907 return S_EX_REGISTER;
9908 }
9909 }
9910 }
9911 /* Check for percent_op (in parentheses). */
9912 else if (b[0] == '%')
9913 {
9914 *str = b;
9915 return my_getPercentOp (str, len, nestlevel);
9916 }
9917
9918 /* Some other expression in the parentheses, which can contain
9919 parentheses itself. Attempt to find the matching one. */
9920 {
9921 int pcnt = 1;
9922 char *s;
9923
9924 *len = 1;
9925 for (s = *str + 1; *s && pcnt; s++, (*len)++)
9926 {
9927 if (*s == '(')
9928 ++pcnt;
9929 else if (*s == ')')
9930 --pcnt;
9931 }
9932 }
9933 }
9934 /* Check for percent_op (outside of parentheses). */
9935 else if (*str[0] == '%')
9936 return my_getPercentOp (str, len, nestlevel);
9937
9938 /* Any other expression. */
9939 return S_EX_NONE;
9940 }
9941
9942 static int
9943 my_getPercentOp (str, len, nestlevel)
9944 char **str;
9945 unsigned int *len;
9946 int *nestlevel;
9947 {
9948 char *tmp = *str + 1;
9949 unsigned int i = 0;
9950
9951 while (ISALPHA (*tmp) || *tmp == '_')
9952 {
9953 *tmp = TOLOWER (*tmp);
9954 tmp++;
9955 }
9956 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9957 {
9958 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9959 i++;
9960 else
9961 {
9962 int type = percent_op[i].type;
9963
9964 /* Only %hi and %lo are allowed for OldABI. */
9965 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9966 return S_EX_NONE;
9967
9968 *len = strlen (percent_op[i].str);
9969 ++(*nestlevel);
9970 return type;
9971 }
9972 }
9973 return S_EX_NONE;
9974 }
9975
9976 static int
9977 my_getSmallExpression (ep, str)
9978 expressionS *ep;
9979 char *str;
9980 {
9981 static char *oldstr = NULL;
9982 int c = S_EX_NONE;
9983 int oldc;
9984 int nestlevel = -1;
9985 unsigned int len;
9986
9987 /* Don't update oldstr if the last call had nested percent_op's. We need
9988 it to parse the outer ones later. */
9989 if (! oldstr)
9990 oldstr = str;
9991
9992 do
9993 {
9994 oldc = c;
9995 c = my_getSmallParser (&str, &len, &nestlevel);
9996 if (c != S_EX_NONE && c != S_EX_REGISTER)
9997 str += len;
9998 }
9999 while (c != S_EX_NONE && c != S_EX_REGISTER);
10000
10001 if (nestlevel >= 0)
10002 {
10003 /* A percent_op was encountered. Don't try to get an expression if
10004 it is already blanked out. */
10005 if (*(str + strspn (str + 1, " )")) != ')')
10006 {
10007 char save;
10008
10009 /* Let my_getExpression() stop at the closing parenthesis. */
10010 save = *(str + len);
10011 *(str + len) = '\0';
10012 my_getExpression (ep, str);
10013 *(str + len) = save;
10014 }
10015 if (nestlevel > 0)
10016 {
10017 /* Blank out including the % sign and the proper matching
10018 parenthesis. */
10019 int pcnt = 1;
10020 char *s = strrchr (oldstr, '%');
10021 char *end;
10022
10023 for (end = strchr (s, '(') + 1; *end && pcnt; end++)
10024 {
10025 if (*end == '(')
10026 ++pcnt;
10027 else if (*end == ')')
10028 --pcnt;
10029 }
10030
10031 memset (s, ' ', end - s);
10032 str = oldstr;
10033 }
10034 else
10035 expr_end = str + len;
10036
10037 c = oldc;
10038 }
10039 else if (c == S_EX_NONE)
10040 {
10041 my_getExpression (ep, str);
10042 }
10043 else if (c == S_EX_REGISTER)
10044 {
10045 ep->X_op = O_constant;
10046 expr_end = str;
10047 ep->X_add_symbol = NULL;
10048 ep->X_op_symbol = NULL;
10049 ep->X_add_number = 0;
10050 }
10051 else
10052 {
10053 as_fatal (_("internal error"));
10054 }
10055
10056 if (nestlevel <= 0)
10057 /* All percent_op's have been handled. */
10058 oldstr = NULL;
10059
10060 return c;
10061 }
10062
10063 static void
10064 my_getExpression (ep, str)
10065 expressionS *ep;
10066 char *str;
10067 {
10068 char *save_in;
10069 valueT val;
10070
10071 save_in = input_line_pointer;
10072 input_line_pointer = str;
10073 expression (ep);
10074 expr_end = input_line_pointer;
10075 input_line_pointer = save_in;
10076
10077 /* If we are in mips16 mode, and this is an expression based on `.',
10078 then we bump the value of the symbol by 1 since that is how other
10079 text symbols are handled. We don't bother to handle complex
10080 expressions, just `.' plus or minus a constant. */
10081 if (mips_opts.mips16
10082 && ep->X_op == O_symbol
10083 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10084 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10085 && symbol_get_frag (ep->X_add_symbol) == frag_now
10086 && symbol_constant_p (ep->X_add_symbol)
10087 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10088 S_SET_VALUE (ep->X_add_symbol, val + 1);
10089 }
10090
10091 /* Turn a string in input_line_pointer into a floating point constant
10092 of type TYPE, and store the appropriate bytes in *LITP. The number
10093 of LITTLENUMS emitted is stored in *SIZEP. An error message is
10094 returned, or NULL on OK. */
10095
10096 char *
10097 md_atof (type, litP, sizeP)
10098 int type;
10099 char *litP;
10100 int *sizeP;
10101 {
10102 int prec;
10103 LITTLENUM_TYPE words[4];
10104 char *t;
10105 int i;
10106
10107 switch (type)
10108 {
10109 case 'f':
10110 prec = 2;
10111 break;
10112
10113 case 'd':
10114 prec = 4;
10115 break;
10116
10117 default:
10118 *sizeP = 0;
10119 return _("bad call to md_atof");
10120 }
10121
10122 t = atof_ieee (input_line_pointer, type, words);
10123 if (t)
10124 input_line_pointer = t;
10125
10126 *sizeP = prec * 2;
10127
10128 if (! target_big_endian)
10129 {
10130 for (i = prec - 1; i >= 0; i--)
10131 {
10132 md_number_to_chars (litP, (valueT) words[i], 2);
10133 litP += 2;
10134 }
10135 }
10136 else
10137 {
10138 for (i = 0; i < prec; i++)
10139 {
10140 md_number_to_chars (litP, (valueT) words[i], 2);
10141 litP += 2;
10142 }
10143 }
10144
10145 return NULL;
10146 }
10147
10148 void
10149 md_number_to_chars (buf, val, n)
10150 char *buf;
10151 valueT val;
10152 int n;
10153 {
10154 if (target_big_endian)
10155 number_to_chars_bigendian (buf, val, n);
10156 else
10157 number_to_chars_littleendian (buf, val, n);
10158 }
10159 \f
10160 #ifdef OBJ_ELF
10161 static int support_64bit_objects(void)
10162 {
10163 const char **list, **l;
10164
10165 list = bfd_target_list ();
10166 for (l = list; *l != NULL; l++)
10167 #ifdef TE_TMIPS
10168 /* This is traditional mips */
10169 if (strcmp (*l, "elf64-tradbigmips") == 0
10170 || strcmp (*l, "elf64-tradlittlemips") == 0)
10171 #else
10172 if (strcmp (*l, "elf64-bigmips") == 0
10173 || strcmp (*l, "elf64-littlemips") == 0)
10174 #endif
10175 break;
10176 free (list);
10177 return (*l != NULL);
10178 }
10179 #endif /* OBJ_ELF */
10180
10181 const char *md_shortopts = "nO::g::G:";
10182
10183 struct option md_longopts[] =
10184 {
10185 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10186 {"mips0", no_argument, NULL, OPTION_MIPS1},
10187 {"mips1", no_argument, NULL, OPTION_MIPS1},
10188 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10189 {"mips2", no_argument, NULL, OPTION_MIPS2},
10190 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10191 {"mips3", no_argument, NULL, OPTION_MIPS3},
10192 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10193 {"mips4", no_argument, NULL, OPTION_MIPS4},
10194 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10195 {"mips5", no_argument, NULL, OPTION_MIPS5},
10196 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10197 {"mips32", no_argument, NULL, OPTION_MIPS32},
10198 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10199 {"mips64", no_argument, NULL, OPTION_MIPS64},
10200 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
10201 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10202 #define OPTION_TRAP (OPTION_MD_BASE + 9)
10203 {"trap", no_argument, NULL, OPTION_TRAP},
10204 {"no-break", no_argument, NULL, OPTION_TRAP},
10205 #define OPTION_BREAK (OPTION_MD_BASE + 10)
10206 {"break", no_argument, NULL, OPTION_BREAK},
10207 {"no-trap", no_argument, NULL, OPTION_BREAK},
10208 #define OPTION_EB (OPTION_MD_BASE + 11)
10209 {"EB", no_argument, NULL, OPTION_EB},
10210 #define OPTION_EL (OPTION_MD_BASE + 12)
10211 {"EL", no_argument, NULL, OPTION_EL},
10212 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
10213 {"mips16", no_argument, NULL, OPTION_MIPS16},
10214 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
10215 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10216 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
10217 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10218 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10219 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10220 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10221 #define OPTION_FP32 (OPTION_MD_BASE + 17)
10222 {"mfp32", no_argument, NULL, OPTION_FP32},
10223 #define OPTION_GP32 (OPTION_MD_BASE + 18)
10224 {"mgp32", no_argument, NULL, OPTION_GP32},
10225 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
10226 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10227 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
10228 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10229 #define OPTION_MARCH (OPTION_MD_BASE + 21)
10230 {"march", required_argument, NULL, OPTION_MARCH},
10231 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
10232 {"mtune", required_argument, NULL, OPTION_MTUNE},
10233 #define OPTION_FP64 (OPTION_MD_BASE + 23)
10234 {"mfp64", no_argument, NULL, OPTION_FP64},
10235 #define OPTION_M4650 (OPTION_MD_BASE + 24)
10236 {"m4650", no_argument, NULL, OPTION_M4650},
10237 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10238 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10239 #define OPTION_M4010 (OPTION_MD_BASE + 26)
10240 {"m4010", no_argument, NULL, OPTION_M4010},
10241 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10242 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10243 #define OPTION_M4100 (OPTION_MD_BASE + 28)
10244 {"m4100", no_argument, NULL, OPTION_M4100},
10245 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10246 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10247 #define OPTION_M3900 (OPTION_MD_BASE + 30)
10248 {"m3900", no_argument, NULL, OPTION_M3900},
10249 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10250 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10251 #define OPTION_GP64 (OPTION_MD_BASE + 32)
10252 {"mgp64", no_argument, NULL, OPTION_GP64},
10253 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10254 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10255 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10256 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10257 #define OPTION_MDMX (OPTION_MD_BASE + 35)
10258 {"mdmx", no_argument, NULL, OPTION_MDMX},
10259 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10260 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10261 #define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10262 #define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10263 {"mfix-vr4122-bugs", no_argument, NULL, OPTION_FIX_VR4122},
10264 {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10265 #define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10266 #define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10267 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10268 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10269 #ifdef OBJ_ELF
10270 #define OPTION_ELF_BASE (OPTION_MD_BASE + 41)
10271 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10272 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10273 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10274 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10275 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10276 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10277 {"xgot", no_argument, NULL, OPTION_XGOT},
10278 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10279 {"mabi", required_argument, NULL, OPTION_MABI},
10280 #define OPTION_32 (OPTION_ELF_BASE + 4)
10281 {"32", no_argument, NULL, OPTION_32},
10282 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10283 {"n32", no_argument, NULL, OPTION_N32},
10284 #define OPTION_64 (OPTION_ELF_BASE + 6)
10285 {"64", no_argument, NULL, OPTION_64},
10286 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10287 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10288 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10289 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10290 #endif /* OBJ_ELF */
10291 {NULL, no_argument, NULL, 0}
10292 };
10293 size_t md_longopts_size = sizeof (md_longopts);
10294
10295 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10296 NEW_VALUE. Warn if another value was already specified. Note:
10297 we have to defer parsing the -march and -mtune arguments in order
10298 to handle 'from-abi' correctly, since the ABI might be specified
10299 in a later argument. */
10300
10301 static void
10302 mips_set_option_string (string_ptr, new_value)
10303 const char **string_ptr, *new_value;
10304 {
10305 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10306 as_warn (_("A different %s was already specified, is now %s"),
10307 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10308 new_value);
10309
10310 *string_ptr = new_value;
10311 }
10312
10313 int
10314 md_parse_option (c, arg)
10315 int c;
10316 char *arg;
10317 {
10318 switch (c)
10319 {
10320 case OPTION_CONSTRUCT_FLOATS:
10321 mips_disable_float_construction = 0;
10322 break;
10323
10324 case OPTION_NO_CONSTRUCT_FLOATS:
10325 mips_disable_float_construction = 1;
10326 break;
10327
10328 case OPTION_TRAP:
10329 mips_trap = 1;
10330 break;
10331
10332 case OPTION_BREAK:
10333 mips_trap = 0;
10334 break;
10335
10336 case OPTION_EB:
10337 target_big_endian = 1;
10338 break;
10339
10340 case OPTION_EL:
10341 target_big_endian = 0;
10342 break;
10343
10344 case 'n':
10345 warn_nops = 1;
10346 break;
10347
10348 case 'O':
10349 if (arg && arg[1] == '0')
10350 mips_optimize = 1;
10351 else
10352 mips_optimize = 2;
10353 break;
10354
10355 case 'g':
10356 if (arg == NULL)
10357 mips_debug = 2;
10358 else
10359 mips_debug = atoi (arg);
10360 /* When the MIPS assembler sees -g or -g2, it does not do
10361 optimizations which limit full symbolic debugging. We take
10362 that to be equivalent to -O0. */
10363 if (mips_debug == 2)
10364 mips_optimize = 1;
10365 break;
10366
10367 case OPTION_MIPS1:
10368 file_mips_isa = ISA_MIPS1;
10369 break;
10370
10371 case OPTION_MIPS2:
10372 file_mips_isa = ISA_MIPS2;
10373 break;
10374
10375 case OPTION_MIPS3:
10376 file_mips_isa = ISA_MIPS3;
10377 break;
10378
10379 case OPTION_MIPS4:
10380 file_mips_isa = ISA_MIPS4;
10381 break;
10382
10383 case OPTION_MIPS5:
10384 file_mips_isa = ISA_MIPS5;
10385 break;
10386
10387 case OPTION_MIPS32:
10388 file_mips_isa = ISA_MIPS32;
10389 break;
10390
10391 case OPTION_MIPS64:
10392 file_mips_isa = ISA_MIPS64;
10393 break;
10394
10395 case OPTION_MTUNE:
10396 mips_set_option_string (&mips_tune_string, arg);
10397 break;
10398
10399 case OPTION_MARCH:
10400 mips_set_option_string (&mips_arch_string, arg);
10401 break;
10402
10403 case OPTION_M4650:
10404 mips_set_option_string (&mips_arch_string, "4650");
10405 mips_set_option_string (&mips_tune_string, "4650");
10406 break;
10407
10408 case OPTION_NO_M4650:
10409 break;
10410
10411 case OPTION_M4010:
10412 mips_set_option_string (&mips_arch_string, "4010");
10413 mips_set_option_string (&mips_tune_string, "4010");
10414 break;
10415
10416 case OPTION_NO_M4010:
10417 break;
10418
10419 case OPTION_M4100:
10420 mips_set_option_string (&mips_arch_string, "4100");
10421 mips_set_option_string (&mips_tune_string, "4100");
10422 break;
10423
10424 case OPTION_NO_M4100:
10425 break;
10426
10427 case OPTION_M3900:
10428 mips_set_option_string (&mips_arch_string, "3900");
10429 mips_set_option_string (&mips_tune_string, "3900");
10430 break;
10431
10432 case OPTION_NO_M3900:
10433 break;
10434
10435 case OPTION_MDMX:
10436 mips_opts.ase_mdmx = 1;
10437 break;
10438
10439 case OPTION_NO_MDMX:
10440 mips_opts.ase_mdmx = 0;
10441 break;
10442
10443 case OPTION_MIPS16:
10444 mips_opts.mips16 = 1;
10445 mips_no_prev_insn (false);
10446 break;
10447
10448 case OPTION_NO_MIPS16:
10449 mips_opts.mips16 = 0;
10450 mips_no_prev_insn (false);
10451 break;
10452
10453 case OPTION_MIPS3D:
10454 mips_opts.ase_mips3d = 1;
10455 break;
10456
10457 case OPTION_NO_MIPS3D:
10458 mips_opts.ase_mips3d = 0;
10459 break;
10460
10461 case OPTION_MEMBEDDED_PIC:
10462 mips_pic = EMBEDDED_PIC;
10463 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10464 {
10465 as_bad (_("-G may not be used with embedded PIC code"));
10466 return 0;
10467 }
10468 g_switch_value = 0x7fffffff;
10469 break;
10470
10471 case OPTION_FIX_VR4122:
10472 mips_fix_4122_bugs = 1;
10473 break;
10474
10475 case OPTION_NO_FIX_VR4122:
10476 mips_fix_4122_bugs = 0;
10477 break;
10478
10479 case OPTION_RELAX_BRANCH:
10480 mips_relax_branch = 1;
10481 break;
10482
10483 case OPTION_NO_RELAX_BRANCH:
10484 mips_relax_branch = 0;
10485 break;
10486
10487 #ifdef OBJ_ELF
10488 /* When generating ELF code, we permit -KPIC and -call_shared to
10489 select SVR4_PIC, and -non_shared to select no PIC. This is
10490 intended to be compatible with Irix 5. */
10491 case OPTION_CALL_SHARED:
10492 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10493 {
10494 as_bad (_("-call_shared is supported only for ELF format"));
10495 return 0;
10496 }
10497 mips_pic = SVR4_PIC;
10498 if (g_switch_seen && g_switch_value != 0)
10499 {
10500 as_bad (_("-G may not be used with SVR4 PIC code"));
10501 return 0;
10502 }
10503 g_switch_value = 0;
10504 break;
10505
10506 case OPTION_NON_SHARED:
10507 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10508 {
10509 as_bad (_("-non_shared is supported only for ELF format"));
10510 return 0;
10511 }
10512 mips_pic = NO_PIC;
10513 break;
10514
10515 /* The -xgot option tells the assembler to use 32 offsets when
10516 accessing the got in SVR4_PIC mode. It is for Irix
10517 compatibility. */
10518 case OPTION_XGOT:
10519 mips_big_got = 1;
10520 break;
10521 #endif /* OBJ_ELF */
10522
10523 case 'G':
10524 if (! USE_GLOBAL_POINTER_OPT)
10525 {
10526 as_bad (_("-G is not supported for this configuration"));
10527 return 0;
10528 }
10529 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10530 {
10531 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10532 return 0;
10533 }
10534 else
10535 g_switch_value = atoi (arg);
10536 g_switch_seen = 1;
10537 break;
10538
10539 #ifdef OBJ_ELF
10540 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10541 and -mabi=64. */
10542 case OPTION_32:
10543 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10544 {
10545 as_bad (_("-32 is supported for ELF format only"));
10546 return 0;
10547 }
10548 mips_abi = O32_ABI;
10549 break;
10550
10551 case OPTION_N32:
10552 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10553 {
10554 as_bad (_("-n32 is supported for ELF format only"));
10555 return 0;
10556 }
10557 mips_abi = N32_ABI;
10558 break;
10559
10560 case OPTION_64:
10561 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10562 {
10563 as_bad (_("-64 is supported for ELF format only"));
10564 return 0;
10565 }
10566 mips_abi = N64_ABI;
10567 if (! support_64bit_objects())
10568 as_fatal (_("No compiled in support for 64 bit object file format"));
10569 break;
10570 #endif /* OBJ_ELF */
10571
10572 case OPTION_GP32:
10573 file_mips_gp32 = 1;
10574 break;
10575
10576 case OPTION_GP64:
10577 file_mips_gp32 = 0;
10578 break;
10579
10580 case OPTION_FP32:
10581 file_mips_fp32 = 1;
10582 break;
10583
10584 case OPTION_FP64:
10585 file_mips_fp32 = 0;
10586 break;
10587
10588 #ifdef OBJ_ELF
10589 case OPTION_MABI:
10590 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10591 {
10592 as_bad (_("-mabi is supported for ELF format only"));
10593 return 0;
10594 }
10595 if (strcmp (arg, "32") == 0)
10596 mips_abi = O32_ABI;
10597 else if (strcmp (arg, "o64") == 0)
10598 mips_abi = O64_ABI;
10599 else if (strcmp (arg, "n32") == 0)
10600 mips_abi = N32_ABI;
10601 else if (strcmp (arg, "64") == 0)
10602 {
10603 mips_abi = N64_ABI;
10604 if (! support_64bit_objects())
10605 as_fatal (_("No compiled in support for 64 bit object file "
10606 "format"));
10607 }
10608 else if (strcmp (arg, "eabi") == 0)
10609 mips_abi = EABI_ABI;
10610 else
10611 {
10612 as_fatal (_("invalid abi -mabi=%s"), arg);
10613 return 0;
10614 }
10615 break;
10616 #endif /* OBJ_ELF */
10617
10618 case OPTION_M7000_HILO_FIX:
10619 mips_7000_hilo_fix = true;
10620 break;
10621
10622 case OPTION_MNO_7000_HILO_FIX:
10623 mips_7000_hilo_fix = false;
10624 break;
10625
10626 #ifdef OBJ_ELF
10627 case OPTION_MDEBUG:
10628 mips_flag_mdebug = true;
10629 break;
10630
10631 case OPTION_NO_MDEBUG:
10632 mips_flag_mdebug = false;
10633 break;
10634 #endif /* OBJ_ELF */
10635
10636 default:
10637 return 0;
10638 }
10639
10640 return 1;
10641 }
10642 \f
10643 /* Set up globals to generate code for the ISA or processor
10644 described by INFO. */
10645
10646 static void
10647 mips_set_architecture (info)
10648 const struct mips_cpu_info *info;
10649 {
10650 if (info != 0)
10651 {
10652 mips_arch_info = info;
10653 mips_arch = info->cpu;
10654 mips_opts.isa = info->isa;
10655 }
10656 }
10657
10658
10659 /* Likewise for tuning. */
10660
10661 static void
10662 mips_set_tune (info)
10663 const struct mips_cpu_info *info;
10664 {
10665 if (info != 0)
10666 {
10667 mips_tune_info = info;
10668 mips_tune = info->cpu;
10669 }
10670 }
10671
10672
10673 void
10674 mips_after_parse_args ()
10675 {
10676 /* GP relative stuff not working for PE */
10677 if (strncmp (TARGET_OS, "pe", 2) == 0
10678 && g_switch_value != 0)
10679 {
10680 if (g_switch_seen)
10681 as_bad (_("-G not supported in this configuration."));
10682 g_switch_value = 0;
10683 }
10684
10685 /* The following code determines the architecture and register size.
10686 Similar code was added to GCC 3.3 (see override_options() in
10687 config/mips/mips.c). The GAS and GCC code should be kept in sync
10688 as much as possible. */
10689
10690 if (mips_arch_string != 0)
10691 mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10692
10693 if (mips_tune_string != 0)
10694 mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10695
10696 if (file_mips_isa != ISA_UNKNOWN)
10697 {
10698 /* Handle -mipsN. At this point, file_mips_isa contains the
10699 ISA level specified by -mipsN, while mips_opts.isa contains
10700 the -march selection (if any). */
10701 if (mips_arch_info != 0)
10702 {
10703 /* -march takes precedence over -mipsN, since it is more descriptive.
10704 There's no harm in specifying both as long as the ISA levels
10705 are the same. */
10706 if (file_mips_isa != mips_opts.isa)
10707 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10708 mips_cpu_info_from_isa (file_mips_isa)->name,
10709 mips_cpu_info_from_isa (mips_opts.isa)->name);
10710 }
10711 else
10712 mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10713 }
10714
10715 if (mips_arch_info == 0)
10716 mips_set_architecture (mips_parse_cpu ("default CPU",
10717 MIPS_CPU_STRING_DEFAULT));
10718
10719 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10720 as_bad ("-march=%s is not compatible with the selected ABI",
10721 mips_arch_info->name);
10722
10723 /* Optimize for mips_arch, unless -mtune selects a different processor. */
10724 if (mips_tune_info == 0)
10725 mips_set_tune (mips_arch_info);
10726
10727 if (file_mips_gp32 >= 0)
10728 {
10729 /* The user specified the size of the integer registers. Make sure
10730 it agrees with the ABI and ISA. */
10731 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10732 as_bad (_("-mgp64 used with a 32-bit processor"));
10733 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10734 as_bad (_("-mgp32 used with a 64-bit ABI"));
10735 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10736 as_bad (_("-mgp64 used with a 32-bit ABI"));
10737 }
10738 else
10739 {
10740 /* Infer the integer register size from the ABI and processor.
10741 Restrict ourselves to 32-bit registers if that's all the
10742 processor has, or if the ABI cannot handle 64-bit registers. */
10743 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10744 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10745 }
10746
10747 /* ??? GAS treats single-float processors as though they had 64-bit
10748 float registers (although it complains when double-precision
10749 instructions are used). As things stand, saying they have 32-bit
10750 registers would lead to spurious "register must be even" messages.
10751 So here we assume float registers are always the same size as
10752 integer ones, unless the user says otherwise. */
10753 if (file_mips_fp32 < 0)
10754 file_mips_fp32 = file_mips_gp32;
10755
10756 /* End of GCC-shared inference code. */
10757
10758 /* ??? When do we want this flag to be set? Who uses it? */
10759 if (file_mips_gp32 == 1
10760 && mips_abi == NO_ABI
10761 && ISA_HAS_64BIT_REGS (mips_opts.isa))
10762 mips_32bitmode = 1;
10763
10764 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10765 as_bad (_("trap exception not supported at ISA 1"));
10766
10767 /* If the selected architecture includes support for ASEs, enable
10768 generation of code for them. */
10769 if (mips_opts.mips16 == -1)
10770 mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10771 if (mips_opts.ase_mips3d == -1)
10772 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10773 if (mips_opts.ase_mdmx == -1)
10774 mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10775
10776 file_mips_isa = mips_opts.isa;
10777 file_ase_mips16 = mips_opts.mips16;
10778 file_ase_mips3d = mips_opts.ase_mips3d;
10779 file_ase_mdmx = mips_opts.ase_mdmx;
10780 mips_opts.gp32 = file_mips_gp32;
10781 mips_opts.fp32 = file_mips_fp32;
10782
10783 if (mips_flag_mdebug < 0)
10784 {
10785 #ifdef OBJ_MAYBE_ECOFF
10786 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10787 mips_flag_mdebug = 1;
10788 else
10789 #endif /* OBJ_MAYBE_ECOFF */
10790 mips_flag_mdebug = 0;
10791 }
10792 }
10793 \f
10794 void
10795 mips_init_after_args ()
10796 {
10797 /* initialize opcodes */
10798 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10799 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10800 }
10801
10802 long
10803 md_pcrel_from (fixP)
10804 fixS *fixP;
10805 {
10806 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10807 && fixP->fx_addsy != (symbolS *) NULL
10808 && ! S_IS_DEFINED (fixP->fx_addsy))
10809 {
10810 /* This makes a branch to an undefined symbol be a branch to the
10811 current location. */
10812 if (mips_pic == EMBEDDED_PIC)
10813 return 4;
10814 else
10815 return 1;
10816 }
10817
10818 /* Return the address of the delay slot. */
10819 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10820 }
10821
10822 /* This is called before the symbol table is processed. In order to
10823 work with gcc when using mips-tfile, we must keep all local labels.
10824 However, in other cases, we want to discard them. If we were
10825 called with -g, but we didn't see any debugging information, it may
10826 mean that gcc is smuggling debugging information through to
10827 mips-tfile, in which case we must generate all local labels. */
10828
10829 void
10830 mips_frob_file_before_adjust ()
10831 {
10832 #ifndef NO_ECOFF_DEBUGGING
10833 if (ECOFF_DEBUGGING
10834 && mips_debug != 0
10835 && ! ecoff_debugging_seen)
10836 flag_keep_locals = 1;
10837 #endif
10838 }
10839
10840 /* Sort any unmatched HI16_S relocs so that they immediately precede
10841 the corresponding LO reloc. This is called before md_apply_fix3 and
10842 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10843 explicit use of the %hi modifier. */
10844
10845 void
10846 mips_frob_file ()
10847 {
10848 struct mips_hi_fixup *l;
10849
10850 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10851 {
10852 segment_info_type *seginfo;
10853 int pass;
10854
10855 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10856
10857 /* Check quickly whether the next fixup happens to be a matching
10858 %lo. */
10859 if (l->fixp->fx_next != NULL
10860 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10861 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10862 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10863 continue;
10864
10865 /* Look through the fixups for this segment for a matching %lo.
10866 When we find one, move the %hi just in front of it. We do
10867 this in two passes. In the first pass, we try to find a
10868 unique %lo. In the second pass, we permit multiple %hi
10869 relocs for a single %lo (this is a GNU extension). */
10870 seginfo = seg_info (l->seg);
10871 for (pass = 0; pass < 2; pass++)
10872 {
10873 fixS *f, *prev;
10874
10875 prev = NULL;
10876 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10877 {
10878 /* Check whether this is a %lo fixup which matches l->fixp. */
10879 if (f->fx_r_type == BFD_RELOC_LO16
10880 && f->fx_addsy == l->fixp->fx_addsy
10881 && f->fx_offset == l->fixp->fx_offset
10882 && (pass == 1
10883 || prev == NULL
10884 || prev->fx_r_type != BFD_RELOC_HI16_S
10885 || prev->fx_addsy != f->fx_addsy
10886 || prev->fx_offset != f->fx_offset))
10887 {
10888 fixS **pf;
10889
10890 /* Move l->fixp before f. */
10891 for (pf = &seginfo->fix_root;
10892 *pf != l->fixp;
10893 pf = &(*pf)->fx_next)
10894 assert (*pf != NULL);
10895
10896 *pf = l->fixp->fx_next;
10897
10898 l->fixp->fx_next = f;
10899 if (prev == NULL)
10900 seginfo->fix_root = l->fixp;
10901 else
10902 prev->fx_next = l->fixp;
10903
10904 break;
10905 }
10906
10907 prev = f;
10908 }
10909
10910 if (f != NULL)
10911 break;
10912
10913 #if 0 /* GCC code motion plus incomplete dead code elimination
10914 can leave a %hi without a %lo. */
10915 if (pass == 1)
10916 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10917 _("Unmatched %%hi reloc"));
10918 #endif
10919 }
10920 }
10921 }
10922
10923 /* When generating embedded PIC code we need to use a special
10924 relocation to represent the difference of two symbols in the .text
10925 section (switch tables use a difference of this sort). See
10926 include/coff/mips.h for details. This macro checks whether this
10927 fixup requires the special reloc. */
10928 #define SWITCH_TABLE(fixp) \
10929 ((fixp)->fx_r_type == BFD_RELOC_32 \
10930 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10931 && (fixp)->fx_addsy != NULL \
10932 && (fixp)->fx_subsy != NULL \
10933 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10934 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10935
10936 /* When generating embedded PIC code we must keep all PC relative
10937 relocations, in case the linker has to relax a call. We also need
10938 to keep relocations for switch table entries.
10939
10940 We may have combined relocations without symbols in the N32/N64 ABI.
10941 We have to prevent gas from dropping them. */
10942
10943 int
10944 mips_force_relocation (fixp)
10945 fixS *fixp;
10946 {
10947 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10948 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10949 || S_FORCE_RELOC (fixp->fx_addsy))
10950 return 1;
10951
10952 if (HAVE_NEWABI
10953 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10954 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10955 || fixp->fx_r_type == BFD_RELOC_HI16_S
10956 || fixp->fx_r_type == BFD_RELOC_LO16))
10957 return 1;
10958
10959 return (mips_pic == EMBEDDED_PIC
10960 && (fixp->fx_pcrel
10961 || SWITCH_TABLE (fixp)
10962 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10963 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10964 }
10965
10966 #ifdef OBJ_ELF
10967 static int
10968 mips_need_elf_addend_fixup (fixP)
10969 fixS *fixP;
10970 {
10971 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10972 return 1;
10973 if (mips_pic == EMBEDDED_PIC
10974 && S_IS_WEAK (fixP->fx_addsy))
10975 return 1;
10976 if (mips_pic != EMBEDDED_PIC
10977 && (S_IS_WEAK (fixP->fx_addsy)
10978 || S_IS_EXTERNAL (fixP->fx_addsy))
10979 && !S_IS_COMMON (fixP->fx_addsy))
10980 return 1;
10981 if (symbol_used_in_reloc_p (fixP->fx_addsy)
10982 && (((bfd_get_section_flags (stdoutput,
10983 S_GET_SEGMENT (fixP->fx_addsy))
10984 & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
10985 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10986 ".gnu.linkonce",
10987 sizeof (".gnu.linkonce") - 1)))
10988 return 1;
10989 return 0;
10990 }
10991 #endif
10992
10993 /* Apply a fixup to the object file. */
10994
10995 void
10996 md_apply_fix3 (fixP, valP, seg)
10997 fixS *fixP;
10998 valueT *valP;
10999 segT seg ATTRIBUTE_UNUSED;
11000 {
11001 bfd_byte *buf;
11002 long insn;
11003 valueT value;
11004 static int previous_fx_r_type = 0;
11005
11006 /* FIXME: Maybe just return for all reloc types not listed below?
11007 Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
11008 if (fixP->fx_r_type == BFD_RELOC_8)
11009 return;
11010
11011 assert (fixP->fx_size == 4
11012 || fixP->fx_r_type == BFD_RELOC_16
11013 || fixP->fx_r_type == BFD_RELOC_32
11014 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11015 || fixP->fx_r_type == BFD_RELOC_HI16_S
11016 || fixP->fx_r_type == BFD_RELOC_LO16
11017 || fixP->fx_r_type == BFD_RELOC_GPREL16
11018 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
11019 || fixP->fx_r_type == BFD_RELOC_GPREL32
11020 || fixP->fx_r_type == BFD_RELOC_64
11021 || fixP->fx_r_type == BFD_RELOC_CTOR
11022 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11023 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11024 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11025 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11026 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11027 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
11028 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11029 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11030 || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
11031
11032 value = *valP;
11033
11034 /* If we aren't adjusting this fixup to be against the section
11035 symbol, we need to adjust the value. */
11036 #ifdef OBJ_ELF
11037 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11038 {
11039 if (mips_need_elf_addend_fixup (fixP))
11040 {
11041 reloc_howto_type *howto;
11042 valueT symval = S_GET_VALUE (fixP->fx_addsy);
11043
11044 value -= symval;
11045
11046 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11047 if (value != 0 && howto->partial_inplace
11048 && (! fixP->fx_pcrel || howto->pcrel_offset))
11049 {
11050 /* In this case, the bfd_install_relocation routine will
11051 incorrectly add the symbol value back in. We just want
11052 the addend to appear in the object file.
11053
11054 howto->pcrel_offset is added for R_MIPS_PC16, which is
11055 generated for code like
11056
11057 globl g1 .text
11058 .text
11059 .space 20
11060 g1:
11061 x:
11062 bal g1
11063 */
11064 value -= symval;
11065
11066 /* Make sure the addend is still non-zero. If it became zero
11067 after the last operation, set it to a spurious value and
11068 subtract the same value from the object file's contents. */
11069 if (value == 0)
11070 {
11071 value = 8;
11072
11073 /* The in-place addends for LO16 relocations are signed;
11074 leave the matching HI16 in-place addends as zero. */
11075 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11076 {
11077 bfd_vma contents, mask, field;
11078
11079 contents = bfd_get_bits (fixP->fx_frag->fr_literal
11080 + fixP->fx_where,
11081 fixP->fx_size * 8,
11082 target_big_endian);
11083
11084 /* MASK has bits set where the relocation should go.
11085 FIELD is -value, shifted into the appropriate place
11086 for this relocation. */
11087 mask = 1 << (howto->bitsize - 1);
11088 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11089 field = (-value >> howto->rightshift) << howto->bitpos;
11090
11091 bfd_put_bits ((field & mask) | (contents & ~mask),
11092 fixP->fx_frag->fr_literal + fixP->fx_where,
11093 fixP->fx_size * 8,
11094 target_big_endian);
11095 }
11096 }
11097 }
11098 }
11099
11100 /* This code was generated using trial and error and so is
11101 fragile and not trustworthy. If you change it, you should
11102 rerun the elf-rel, elf-rel2, and empic testcases and ensure
11103 they still pass. */
11104 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11105 {
11106 value += fixP->fx_frag->fr_address + fixP->fx_where;
11107
11108 /* BFD's REL handling, for MIPS, is _very_ weird.
11109 This gives the right results, but it can't possibly
11110 be the way things are supposed to work. */
11111 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
11112 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
11113 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11114 value += fixP->fx_frag->fr_address + fixP->fx_where;
11115 }
11116 }
11117 #endif
11118
11119 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
11120
11121 /* We are not done if this is a composite relocation to set up gp. */
11122 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11123 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11124 || (fixP->fx_r_type == BFD_RELOC_64
11125 && (previous_fx_r_type == BFD_RELOC_GPREL32
11126 || previous_fx_r_type == BFD_RELOC_GPREL16))
11127 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11128 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11129 || fixP->fx_r_type == BFD_RELOC_LO16))))
11130 fixP->fx_done = 1;
11131 previous_fx_r_type = fixP->fx_r_type;
11132
11133 switch (fixP->fx_r_type)
11134 {
11135 case BFD_RELOC_MIPS_JMP:
11136 case BFD_RELOC_MIPS_SHIFT5:
11137 case BFD_RELOC_MIPS_SHIFT6:
11138 case BFD_RELOC_MIPS_GOT_DISP:
11139 case BFD_RELOC_MIPS_GOT_PAGE:
11140 case BFD_RELOC_MIPS_GOT_OFST:
11141 case BFD_RELOC_MIPS_SUB:
11142 case BFD_RELOC_MIPS_INSERT_A:
11143 case BFD_RELOC_MIPS_INSERT_B:
11144 case BFD_RELOC_MIPS_DELETE:
11145 case BFD_RELOC_MIPS_HIGHEST:
11146 case BFD_RELOC_MIPS_HIGHER:
11147 case BFD_RELOC_MIPS_SCN_DISP:
11148 case BFD_RELOC_MIPS_REL16:
11149 case BFD_RELOC_MIPS_RELGOT:
11150 case BFD_RELOC_MIPS_JALR:
11151 case BFD_RELOC_HI16:
11152 case BFD_RELOC_HI16_S:
11153 case BFD_RELOC_GPREL16:
11154 case BFD_RELOC_MIPS_LITERAL:
11155 case BFD_RELOC_MIPS_CALL16:
11156 case BFD_RELOC_MIPS_GOT16:
11157 case BFD_RELOC_GPREL32:
11158 case BFD_RELOC_MIPS_GOT_HI16:
11159 case BFD_RELOC_MIPS_GOT_LO16:
11160 case BFD_RELOC_MIPS_CALL_HI16:
11161 case BFD_RELOC_MIPS_CALL_LO16:
11162 case BFD_RELOC_MIPS16_GPREL:
11163 if (fixP->fx_pcrel)
11164 as_bad_where (fixP->fx_file, fixP->fx_line,
11165 _("Invalid PC relative reloc"));
11166 /* Nothing needed to do. The value comes from the reloc entry */
11167 break;
11168
11169 case BFD_RELOC_MIPS16_JMP:
11170 /* We currently always generate a reloc against a symbol, which
11171 means that we don't want an addend even if the symbol is
11172 defined. */
11173 fixP->fx_addnumber = 0;
11174 break;
11175
11176 case BFD_RELOC_PCREL_HI16_S:
11177 /* The addend for this is tricky if it is internal, so we just
11178 do everything here rather than in bfd_install_relocation. */
11179 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11180 && !fixP->fx_done
11181 && value != 0)
11182 break;
11183 if (fixP->fx_addsy
11184 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11185 {
11186 /* For an external symbol adjust by the address to make it
11187 pcrel_offset. We use the address of the RELLO reloc
11188 which follows this one. */
11189 value += (fixP->fx_next->fx_frag->fr_address
11190 + fixP->fx_next->fx_where);
11191 }
11192 value = ((value + 0x8000) >> 16) & 0xffff;
11193 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11194 if (target_big_endian)
11195 buf += 2;
11196 md_number_to_chars ((char *) buf, value, 2);
11197 break;
11198
11199 case BFD_RELOC_PCREL_LO16:
11200 /* The addend for this is tricky if it is internal, so we just
11201 do everything here rather than in bfd_install_relocation. */
11202 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11203 && !fixP->fx_done
11204 && value != 0)
11205 break;
11206 if (fixP->fx_addsy
11207 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11208 value += fixP->fx_frag->fr_address + fixP->fx_where;
11209 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11210 if (target_big_endian)
11211 buf += 2;
11212 md_number_to_chars ((char *) buf, value, 2);
11213 break;
11214
11215 case BFD_RELOC_64:
11216 /* This is handled like BFD_RELOC_32, but we output a sign
11217 extended value if we are only 32 bits. */
11218 if (fixP->fx_done
11219 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11220 {
11221 if (8 <= sizeof (valueT))
11222 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11223 value, 8);
11224 else
11225 {
11226 long w1, w2;
11227 long hiv;
11228
11229 w1 = w2 = fixP->fx_where;
11230 if (target_big_endian)
11231 w1 += 4;
11232 else
11233 w2 += 4;
11234 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11235 if ((value & 0x80000000) != 0)
11236 hiv = 0xffffffff;
11237 else
11238 hiv = 0;
11239 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11240 }
11241 }
11242 break;
11243
11244 case BFD_RELOC_RVA:
11245 case BFD_RELOC_32:
11246 /* If we are deleting this reloc entry, we must fill in the
11247 value now. This can happen if we have a .word which is not
11248 resolved when it appears but is later defined. We also need
11249 to fill in the value if this is an embedded PIC switch table
11250 entry. */
11251 if (fixP->fx_done
11252 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11253 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11254 value, 4);
11255 break;
11256
11257 case BFD_RELOC_16:
11258 /* If we are deleting this reloc entry, we must fill in the
11259 value now. */
11260 assert (fixP->fx_size == 2);
11261 if (fixP->fx_done)
11262 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11263 value, 2);
11264 break;
11265
11266 case BFD_RELOC_LO16:
11267 /* When handling an embedded PIC switch statement, we can wind
11268 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11269 if (fixP->fx_done)
11270 {
11271 if (value + 0x8000 > 0xffff)
11272 as_bad_where (fixP->fx_file, fixP->fx_line,
11273 _("relocation overflow"));
11274 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11275 if (target_big_endian)
11276 buf += 2;
11277 md_number_to_chars ((char *) buf, value, 2);
11278 }
11279 break;
11280
11281 case BFD_RELOC_16_PCREL_S2:
11282 if ((value & 0x3) != 0)
11283 as_bad_where (fixP->fx_file, fixP->fx_line,
11284 _("Branch to odd address (%lx)"), (long) value);
11285
11286 /* Fall through. */
11287
11288 case BFD_RELOC_16_PCREL:
11289 /*
11290 * We need to save the bits in the instruction since fixup_segment()
11291 * might be deleting the relocation entry (i.e., a branch within
11292 * the current segment).
11293 */
11294 if (!fixP->fx_done && value != 0)
11295 break;
11296 /* If 'value' is zero, the remaining reloc code won't actually
11297 do the store, so it must be done here. This is probably
11298 a bug somewhere. */
11299 if (!fixP->fx_done
11300 && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11301 || fixP->fx_addsy == NULL /* ??? */
11302 || ! S_IS_DEFINED (fixP->fx_addsy)))
11303 value -= fixP->fx_frag->fr_address + fixP->fx_where;
11304
11305 value = (offsetT) value >> 2;
11306
11307 /* update old instruction data */
11308 buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11309 if (target_big_endian)
11310 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11311 else
11312 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11313
11314 if (value + 0x8000 <= 0xffff)
11315 insn |= value & 0xffff;
11316 else
11317 {
11318 /* The branch offset is too large. If this is an
11319 unconditional branch, and we are not generating PIC code,
11320 we can convert it to an absolute jump instruction. */
11321 if (mips_pic == NO_PIC
11322 && fixP->fx_done
11323 && fixP->fx_frag->fr_address >= text_section->vma
11324 && (fixP->fx_frag->fr_address
11325 < text_section->vma + text_section->_raw_size)
11326 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11327 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11328 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11329 {
11330 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11331 insn = 0x0c000000; /* jal */
11332 else
11333 insn = 0x08000000; /* j */
11334 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11335 fixP->fx_done = 0;
11336 fixP->fx_addsy = section_symbol (text_section);
11337 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11338 }
11339 else
11340 {
11341 /* If we got here, we have branch-relaxation disabled,
11342 and there's nothing we can do to fix this instruction
11343 without turning it into a longer sequence. */
11344 as_bad_where (fixP->fx_file, fixP->fx_line,
11345 _("Branch out of range"));
11346 }
11347 }
11348
11349 md_number_to_chars ((char *) buf, (valueT) insn, 4);
11350 break;
11351
11352 case BFD_RELOC_VTABLE_INHERIT:
11353 fixP->fx_done = 0;
11354 if (fixP->fx_addsy
11355 && !S_IS_DEFINED (fixP->fx_addsy)
11356 && !S_IS_WEAK (fixP->fx_addsy))
11357 S_SET_WEAK (fixP->fx_addsy);
11358 break;
11359
11360 case BFD_RELOC_VTABLE_ENTRY:
11361 fixP->fx_done = 0;
11362 break;
11363
11364 default:
11365 internalError ();
11366 }
11367 }
11368
11369 #if 0
11370 void
11371 printInsn (oc)
11372 unsigned long oc;
11373 {
11374 const struct mips_opcode *p;
11375 int treg, sreg, dreg, shamt;
11376 short imm;
11377 const char *args;
11378 int i;
11379
11380 for (i = 0; i < NUMOPCODES; ++i)
11381 {
11382 p = &mips_opcodes[i];
11383 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11384 {
11385 printf ("%08lx %s\t", oc, p->name);
11386 treg = (oc >> 16) & 0x1f;
11387 sreg = (oc >> 21) & 0x1f;
11388 dreg = (oc >> 11) & 0x1f;
11389 shamt = (oc >> 6) & 0x1f;
11390 imm = oc;
11391 for (args = p->args;; ++args)
11392 {
11393 switch (*args)
11394 {
11395 case '\0':
11396 printf ("\n");
11397 break;
11398
11399 case ',':
11400 case '(':
11401 case ')':
11402 printf ("%c", *args);
11403 continue;
11404
11405 case 'r':
11406 assert (treg == sreg);
11407 printf ("$%d,$%d", treg, sreg);
11408 continue;
11409
11410 case 'd':
11411 case 'G':
11412 printf ("$%d", dreg);
11413 continue;
11414
11415 case 't':
11416 case 'E':
11417 printf ("$%d", treg);
11418 continue;
11419
11420 case 'k':
11421 printf ("0x%x", treg);
11422 continue;
11423
11424 case 'b':
11425 case 's':
11426 printf ("$%d", sreg);
11427 continue;
11428
11429 case 'a':
11430 printf ("0x%08lx", oc & 0x1ffffff);
11431 continue;
11432
11433 case 'i':
11434 case 'j':
11435 case 'o':
11436 case 'u':
11437 printf ("%d", imm);
11438 continue;
11439
11440 case '<':
11441 case '>':
11442 printf ("$%d", shamt);
11443 continue;
11444
11445 default:
11446 internalError ();
11447 }
11448 break;
11449 }
11450 return;
11451 }
11452 }
11453 printf (_("%08lx UNDEFINED\n"), oc);
11454 }
11455 #endif
11456
11457 static symbolS *
11458 get_symbol ()
11459 {
11460 int c;
11461 char *name;
11462 symbolS *p;
11463
11464 name = input_line_pointer;
11465 c = get_symbol_end ();
11466 p = (symbolS *) symbol_find_or_make (name);
11467 *input_line_pointer = c;
11468 return p;
11469 }
11470
11471 /* Align the current frag to a given power of two. The MIPS assembler
11472 also automatically adjusts any preceding label. */
11473
11474 static void
11475 mips_align (to, fill, label)
11476 int to;
11477 int fill;
11478 symbolS *label;
11479 {
11480 mips_emit_delays (false);
11481 frag_align (to, fill, 0);
11482 record_alignment (now_seg, to);
11483 if (label != NULL)
11484 {
11485 assert (S_GET_SEGMENT (label) == now_seg);
11486 symbol_set_frag (label, frag_now);
11487 S_SET_VALUE (label, (valueT) frag_now_fix ());
11488 }
11489 }
11490
11491 /* Align to a given power of two. .align 0 turns off the automatic
11492 alignment used by the data creating pseudo-ops. */
11493
11494 static void
11495 s_align (x)
11496 int x ATTRIBUTE_UNUSED;
11497 {
11498 register int temp;
11499 register long temp_fill;
11500 long max_alignment = 15;
11501
11502 /*
11503
11504 o Note that the assembler pulls down any immediately preceeding label
11505 to the aligned address.
11506 o It's not documented but auto alignment is reinstated by
11507 a .align pseudo instruction.
11508 o Note also that after auto alignment is turned off the mips assembler
11509 issues an error on attempt to assemble an improperly aligned data item.
11510 We don't.
11511
11512 */
11513
11514 temp = get_absolute_expression ();
11515 if (temp > max_alignment)
11516 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11517 else if (temp < 0)
11518 {
11519 as_warn (_("Alignment negative: 0 assumed."));
11520 temp = 0;
11521 }
11522 if (*input_line_pointer == ',')
11523 {
11524 ++input_line_pointer;
11525 temp_fill = get_absolute_expression ();
11526 }
11527 else
11528 temp_fill = 0;
11529 if (temp)
11530 {
11531 auto_align = 1;
11532 mips_align (temp, (int) temp_fill,
11533 insn_labels != NULL ? insn_labels->label : NULL);
11534 }
11535 else
11536 {
11537 auto_align = 0;
11538 }
11539
11540 demand_empty_rest_of_line ();
11541 }
11542
11543 void
11544 mips_flush_pending_output ()
11545 {
11546 mips_emit_delays (false);
11547 mips_clear_insn_labels ();
11548 }
11549
11550 static void
11551 s_change_sec (sec)
11552 int sec;
11553 {
11554 segT seg;
11555
11556 /* When generating embedded PIC code, we only use the .text, .lit8,
11557 .sdata and .sbss sections. We change the .data and .rdata
11558 pseudo-ops to use .sdata. */
11559 if (mips_pic == EMBEDDED_PIC
11560 && (sec == 'd' || sec == 'r'))
11561 sec = 's';
11562
11563 #ifdef OBJ_ELF
11564 /* The ELF backend needs to know that we are changing sections, so
11565 that .previous works correctly. We could do something like check
11566 for an obj_section_change_hook macro, but that might be confusing
11567 as it would not be appropriate to use it in the section changing
11568 functions in read.c, since obj-elf.c intercepts those. FIXME:
11569 This should be cleaner, somehow. */
11570 obj_elf_section_change_hook ();
11571 #endif
11572
11573 mips_emit_delays (false);
11574 switch (sec)
11575 {
11576 case 't':
11577 s_text (0);
11578 break;
11579 case 'd':
11580 s_data (0);
11581 break;
11582 case 'b':
11583 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11584 demand_empty_rest_of_line ();
11585 break;
11586
11587 case 'r':
11588 if (USE_GLOBAL_POINTER_OPT)
11589 {
11590 seg = subseg_new (RDATA_SECTION_NAME,
11591 (subsegT) get_absolute_expression ());
11592 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11593 {
11594 bfd_set_section_flags (stdoutput, seg,
11595 (SEC_ALLOC
11596 | SEC_LOAD
11597 | SEC_READONLY
11598 | SEC_RELOC
11599 | SEC_DATA));
11600 if (strcmp (TARGET_OS, "elf") != 0)
11601 record_alignment (seg, 4);
11602 }
11603 demand_empty_rest_of_line ();
11604 }
11605 else
11606 {
11607 as_bad (_("No read only data section in this object file format"));
11608 demand_empty_rest_of_line ();
11609 return;
11610 }
11611 break;
11612
11613 case 's':
11614 if (USE_GLOBAL_POINTER_OPT)
11615 {
11616 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11617 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11618 {
11619 bfd_set_section_flags (stdoutput, seg,
11620 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11621 | SEC_DATA);
11622 if (strcmp (TARGET_OS, "elf") != 0)
11623 record_alignment (seg, 4);
11624 }
11625 demand_empty_rest_of_line ();
11626 break;
11627 }
11628 else
11629 {
11630 as_bad (_("Global pointers not supported; recompile -G 0"));
11631 demand_empty_rest_of_line ();
11632 return;
11633 }
11634 }
11635
11636 auto_align = 1;
11637 }
11638
11639 void
11640 s_change_section (ignore)
11641 int ignore ATTRIBUTE_UNUSED;
11642 {
11643 #ifdef OBJ_ELF
11644 char *section_name;
11645 char c;
11646 char next_c;
11647 int section_type;
11648 int section_flag;
11649 int section_entry_size;
11650 int section_alignment;
11651
11652 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11653 return;
11654
11655 section_name = input_line_pointer;
11656 c = get_symbol_end ();
11657 next_c = *(input_line_pointer + 1);
11658
11659 /* Do we have .section Name<,"flags">? */
11660 if (c != ',' || (c == ',' && next_c == '"'))
11661 {
11662 /* just after name is now '\0'. */
11663 *input_line_pointer = c;
11664 input_line_pointer = section_name;
11665 obj_elf_section (ignore);
11666 return;
11667 }
11668 input_line_pointer++;
11669
11670 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11671 if (c == ',')
11672 section_type = get_absolute_expression ();
11673 else
11674 section_type = 0;
11675 if (*input_line_pointer++ == ',')
11676 section_flag = get_absolute_expression ();
11677 else
11678 section_flag = 0;
11679 if (*input_line_pointer++ == ',')
11680 section_entry_size = get_absolute_expression ();
11681 else
11682 section_entry_size = 0;
11683 if (*input_line_pointer++ == ',')
11684 section_alignment = get_absolute_expression ();
11685 else
11686 section_alignment = 0;
11687
11688 obj_elf_change_section (section_name, section_type, section_flag,
11689 section_entry_size, 0, 0, 0);
11690 #endif /* OBJ_ELF */
11691 }
11692
11693 void
11694 mips_enable_auto_align ()
11695 {
11696 auto_align = 1;
11697 }
11698
11699 static void
11700 s_cons (log_size)
11701 int log_size;
11702 {
11703 symbolS *label;
11704
11705 label = insn_labels != NULL ? insn_labels->label : NULL;
11706 mips_emit_delays (false);
11707 if (log_size > 0 && auto_align)
11708 mips_align (log_size, 0, label);
11709 mips_clear_insn_labels ();
11710 cons (1 << log_size);
11711 }
11712
11713 static void
11714 s_float_cons (type)
11715 int type;
11716 {
11717 symbolS *label;
11718
11719 label = insn_labels != NULL ? insn_labels->label : NULL;
11720
11721 mips_emit_delays (false);
11722
11723 if (auto_align)
11724 {
11725 if (type == 'd')
11726 mips_align (3, 0, label);
11727 else
11728 mips_align (2, 0, label);
11729 }
11730
11731 mips_clear_insn_labels ();
11732
11733 float_cons (type);
11734 }
11735
11736 /* Handle .globl. We need to override it because on Irix 5 you are
11737 permitted to say
11738 .globl foo .text
11739 where foo is an undefined symbol, to mean that foo should be
11740 considered to be the address of a function. */
11741
11742 static void
11743 s_mips_globl (x)
11744 int x ATTRIBUTE_UNUSED;
11745 {
11746 char *name;
11747 int c;
11748 symbolS *symbolP;
11749 flagword flag;
11750
11751 name = input_line_pointer;
11752 c = get_symbol_end ();
11753 symbolP = symbol_find_or_make (name);
11754 *input_line_pointer = c;
11755 SKIP_WHITESPACE ();
11756
11757 /* On Irix 5, every global symbol that is not explicitly labelled as
11758 being a function is apparently labelled as being an object. */
11759 flag = BSF_OBJECT;
11760
11761 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11762 {
11763 char *secname;
11764 asection *sec;
11765
11766 secname = input_line_pointer;
11767 c = get_symbol_end ();
11768 sec = bfd_get_section_by_name (stdoutput, secname);
11769 if (sec == NULL)
11770 as_bad (_("%s: no such section"), secname);
11771 *input_line_pointer = c;
11772
11773 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11774 flag = BSF_FUNCTION;
11775 }
11776
11777 symbol_get_bfdsym (symbolP)->flags |= flag;
11778
11779 S_SET_EXTERNAL (symbolP);
11780 demand_empty_rest_of_line ();
11781 }
11782
11783 static void
11784 s_option (x)
11785 int x ATTRIBUTE_UNUSED;
11786 {
11787 char *opt;
11788 char c;
11789
11790 opt = input_line_pointer;
11791 c = get_symbol_end ();
11792
11793 if (*opt == 'O')
11794 {
11795 /* FIXME: What does this mean? */
11796 }
11797 else if (strncmp (opt, "pic", 3) == 0)
11798 {
11799 int i;
11800
11801 i = atoi (opt + 3);
11802 if (i == 0)
11803 mips_pic = NO_PIC;
11804 else if (i == 2)
11805 mips_pic = SVR4_PIC;
11806 else
11807 as_bad (_(".option pic%d not supported"), i);
11808
11809 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11810 {
11811 if (g_switch_seen && g_switch_value != 0)
11812 as_warn (_("-G may not be used with SVR4 PIC code"));
11813 g_switch_value = 0;
11814 bfd_set_gp_size (stdoutput, 0);
11815 }
11816 }
11817 else
11818 as_warn (_("Unrecognized option \"%s\""), opt);
11819
11820 *input_line_pointer = c;
11821 demand_empty_rest_of_line ();
11822 }
11823
11824 /* This structure is used to hold a stack of .set values. */
11825
11826 struct mips_option_stack
11827 {
11828 struct mips_option_stack *next;
11829 struct mips_set_options options;
11830 };
11831
11832 static struct mips_option_stack *mips_opts_stack;
11833
11834 /* Handle the .set pseudo-op. */
11835
11836 static void
11837 s_mipsset (x)
11838 int x ATTRIBUTE_UNUSED;
11839 {
11840 char *name = input_line_pointer, ch;
11841
11842 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11843 ++input_line_pointer;
11844 ch = *input_line_pointer;
11845 *input_line_pointer = '\0';
11846
11847 if (strcmp (name, "reorder") == 0)
11848 {
11849 if (mips_opts.noreorder && prev_nop_frag != NULL)
11850 {
11851 /* If we still have pending nops, we can discard them. The
11852 usual nop handling will insert any that are still
11853 needed. */
11854 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11855 * (mips_opts.mips16 ? 2 : 4));
11856 prev_nop_frag = NULL;
11857 }
11858 mips_opts.noreorder = 0;
11859 }
11860 else if (strcmp (name, "noreorder") == 0)
11861 {
11862 mips_emit_delays (true);
11863 mips_opts.noreorder = 1;
11864 mips_any_noreorder = 1;
11865 }
11866 else if (strcmp (name, "at") == 0)
11867 {
11868 mips_opts.noat = 0;
11869 }
11870 else if (strcmp (name, "noat") == 0)
11871 {
11872 mips_opts.noat = 1;
11873 }
11874 else if (strcmp (name, "macro") == 0)
11875 {
11876 mips_opts.warn_about_macros = 0;
11877 }
11878 else if (strcmp (name, "nomacro") == 0)
11879 {
11880 if (mips_opts.noreorder == 0)
11881 as_bad (_("`noreorder' must be set before `nomacro'"));
11882 mips_opts.warn_about_macros = 1;
11883 }
11884 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11885 {
11886 mips_opts.nomove = 0;
11887 }
11888 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11889 {
11890 mips_opts.nomove = 1;
11891 }
11892 else if (strcmp (name, "bopt") == 0)
11893 {
11894 mips_opts.nobopt = 0;
11895 }
11896 else if (strcmp (name, "nobopt") == 0)
11897 {
11898 mips_opts.nobopt = 1;
11899 }
11900 else if (strcmp (name, "mips16") == 0
11901 || strcmp (name, "MIPS-16") == 0)
11902 mips_opts.mips16 = 1;
11903 else if (strcmp (name, "nomips16") == 0
11904 || strcmp (name, "noMIPS-16") == 0)
11905 mips_opts.mips16 = 0;
11906 else if (strcmp (name, "mips3d") == 0)
11907 mips_opts.ase_mips3d = 1;
11908 else if (strcmp (name, "nomips3d") == 0)
11909 mips_opts.ase_mips3d = 0;
11910 else if (strcmp (name, "mdmx") == 0)
11911 mips_opts.ase_mdmx = 1;
11912 else if (strcmp (name, "nomdmx") == 0)
11913 mips_opts.ase_mdmx = 0;
11914 else if (strncmp (name, "mips", 4) == 0)
11915 {
11916 int isa;
11917
11918 /* Permit the user to change the ISA on the fly. Needless to
11919 say, misuse can cause serious problems. */
11920 isa = atoi (name + 4);
11921 switch (isa)
11922 {
11923 case 0:
11924 mips_opts.gp32 = file_mips_gp32;
11925 mips_opts.fp32 = file_mips_fp32;
11926 break;
11927 case 1:
11928 case 2:
11929 case 32:
11930 mips_opts.gp32 = 1;
11931 mips_opts.fp32 = 1;
11932 break;
11933 case 3:
11934 case 4:
11935 case 5:
11936 case 64:
11937 mips_opts.gp32 = 0;
11938 mips_opts.fp32 = 0;
11939 break;
11940 default:
11941 as_bad (_("unknown ISA level %s"), name + 4);
11942 break;
11943 }
11944
11945 switch (isa)
11946 {
11947 case 0: mips_opts.isa = file_mips_isa; break;
11948 case 1: mips_opts.isa = ISA_MIPS1; break;
11949 case 2: mips_opts.isa = ISA_MIPS2; break;
11950 case 3: mips_opts.isa = ISA_MIPS3; break;
11951 case 4: mips_opts.isa = ISA_MIPS4; break;
11952 case 5: mips_opts.isa = ISA_MIPS5; break;
11953 case 32: mips_opts.isa = ISA_MIPS32; break;
11954 case 64: mips_opts.isa = ISA_MIPS64; break;
11955 default: as_bad (_("unknown ISA level %s"), name + 4); break;
11956 }
11957 }
11958 else if (strcmp (name, "autoextend") == 0)
11959 mips_opts.noautoextend = 0;
11960 else if (strcmp (name, "noautoextend") == 0)
11961 mips_opts.noautoextend = 1;
11962 else if (strcmp (name, "push") == 0)
11963 {
11964 struct mips_option_stack *s;
11965
11966 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11967 s->next = mips_opts_stack;
11968 s->options = mips_opts;
11969 mips_opts_stack = s;
11970 }
11971 else if (strcmp (name, "pop") == 0)
11972 {
11973 struct mips_option_stack *s;
11974
11975 s = mips_opts_stack;
11976 if (s == NULL)
11977 as_bad (_(".set pop with no .set push"));
11978 else
11979 {
11980 /* If we're changing the reorder mode we need to handle
11981 delay slots correctly. */
11982 if (s->options.noreorder && ! mips_opts.noreorder)
11983 mips_emit_delays (true);
11984 else if (! s->options.noreorder && mips_opts.noreorder)
11985 {
11986 if (prev_nop_frag != NULL)
11987 {
11988 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11989 * (mips_opts.mips16 ? 2 : 4));
11990 prev_nop_frag = NULL;
11991 }
11992 }
11993
11994 mips_opts = s->options;
11995 mips_opts_stack = s->next;
11996 free (s);
11997 }
11998 }
11999 else
12000 {
12001 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12002 }
12003 *input_line_pointer = ch;
12004 demand_empty_rest_of_line ();
12005 }
12006
12007 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
12008 .option pic2. It means to generate SVR4 PIC calls. */
12009
12010 static void
12011 s_abicalls (ignore)
12012 int ignore ATTRIBUTE_UNUSED;
12013 {
12014 mips_pic = SVR4_PIC;
12015 if (USE_GLOBAL_POINTER_OPT)
12016 {
12017 if (g_switch_seen && g_switch_value != 0)
12018 as_warn (_("-G may not be used with SVR4 PIC code"));
12019 g_switch_value = 0;
12020 }
12021 bfd_set_gp_size (stdoutput, 0);
12022 demand_empty_rest_of_line ();
12023 }
12024
12025 /* Handle the .cpload pseudo-op. This is used when generating SVR4
12026 PIC code. It sets the $gp register for the function based on the
12027 function address, which is in the register named in the argument.
12028 This uses a relocation against _gp_disp, which is handled specially
12029 by the linker. The result is:
12030 lui $gp,%hi(_gp_disp)
12031 addiu $gp,$gp,%lo(_gp_disp)
12032 addu $gp,$gp,.cpload argument
12033 The .cpload argument is normally $25 == $t9. */
12034
12035 static void
12036 s_cpload (ignore)
12037 int ignore ATTRIBUTE_UNUSED;
12038 {
12039 expressionS ex;
12040 int icnt = 0;
12041
12042 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12043 .cpload is ignored. */
12044 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12045 {
12046 s_ignore (0);
12047 return;
12048 }
12049
12050 /* .cpload should be in a .set noreorder section. */
12051 if (mips_opts.noreorder == 0)
12052 as_warn (_(".cpload not in noreorder section"));
12053
12054 ex.X_op = O_symbol;
12055 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12056 ex.X_op_symbol = NULL;
12057 ex.X_add_number = 0;
12058
12059 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12060 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12061
12062 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12063 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12064 mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12065
12066 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12067 mips_gp_register, mips_gp_register, tc_get_register (0));
12068
12069 demand_empty_rest_of_line ();
12070 }
12071
12072 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12073 .cpsetup $reg1, offset|$reg2, label
12074
12075 If offset is given, this results in:
12076 sd $gp, offset($sp)
12077 lui $gp, %hi(%neg(%gp_rel(label)))
12078 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12079 daddu $gp, $gp, $reg1
12080
12081 If $reg2 is given, this results in:
12082 daddu $reg2, $gp, $0
12083 lui $gp, %hi(%neg(%gp_rel(label)))
12084 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12085 daddu $gp, $gp, $reg1
12086 $reg1 is normally $25 == $t9. */
12087 static void
12088 s_cpsetup (ignore)
12089 int ignore ATTRIBUTE_UNUSED;
12090 {
12091 expressionS ex_off;
12092 expressionS ex_sym;
12093 int reg1;
12094 int icnt = 0;
12095 char *f;
12096
12097 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12098 We also need NewABI support. */
12099 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12100 {
12101 s_ignore (0);
12102 return;
12103 }
12104
12105 reg1 = tc_get_register (0);
12106 SKIP_WHITESPACE ();
12107 if (*input_line_pointer != ',')
12108 {
12109 as_bad (_("missing argument separator ',' for .cpsetup"));
12110 return;
12111 }
12112 else
12113 ++input_line_pointer;
12114 SKIP_WHITESPACE ();
12115 if (*input_line_pointer == '$')
12116 {
12117 mips_cpreturn_register = tc_get_register (0);
12118 mips_cpreturn_offset = -1;
12119 }
12120 else
12121 {
12122 mips_cpreturn_offset = get_absolute_expression ();
12123 mips_cpreturn_register = -1;
12124 }
12125 SKIP_WHITESPACE ();
12126 if (*input_line_pointer != ',')
12127 {
12128 as_bad (_("missing argument separator ',' for .cpsetup"));
12129 return;
12130 }
12131 else
12132 ++input_line_pointer;
12133 SKIP_WHITESPACE ();
12134 expression (&ex_sym);
12135
12136 if (mips_cpreturn_register == -1)
12137 {
12138 ex_off.X_op = O_constant;
12139 ex_off.X_add_symbol = NULL;
12140 ex_off.X_op_symbol = NULL;
12141 ex_off.X_add_number = mips_cpreturn_offset;
12142
12143 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12144 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12145 }
12146 else
12147 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12148 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12149
12150 /* Ensure there's room for the next two instructions, so that `f'
12151 doesn't end up with an address in the wrong frag. */
12152 frag_grow (8);
12153 f = frag_more (0);
12154 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12155 (int) BFD_RELOC_GPREL16);
12156 fix_new (frag_now, f - frag_now->fr_literal,
12157 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12158 fix_new (frag_now, f - frag_now->fr_literal,
12159 0, NULL, 0, 0, BFD_RELOC_HI16_S);
12160
12161 f = frag_more (0);
12162 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12163 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12164 fix_new (frag_now, f - frag_now->fr_literal,
12165 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12166 fix_new (frag_now, f - frag_now->fr_literal,
12167 0, NULL, 0, 0, BFD_RELOC_LO16);
12168
12169 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12170 HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
12171 mips_gp_register, mips_gp_register, reg1);
12172
12173 demand_empty_rest_of_line ();
12174 }
12175
12176 static void
12177 s_cplocal (ignore)
12178 int ignore ATTRIBUTE_UNUSED;
12179 {
12180 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12181 .cplocal is ignored. */
12182 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12183 {
12184 s_ignore (0);
12185 return;
12186 }
12187
12188 mips_gp_register = tc_get_register (0);
12189 demand_empty_rest_of_line ();
12190 }
12191
12192 /* Handle the .cprestore pseudo-op. This stores $gp into a given
12193 offset from $sp. The offset is remembered, and after making a PIC
12194 call $gp is restored from that location. */
12195
12196 static void
12197 s_cprestore (ignore)
12198 int ignore ATTRIBUTE_UNUSED;
12199 {
12200 expressionS ex;
12201 int icnt = 0;
12202
12203 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12204 .cprestore is ignored. */
12205 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12206 {
12207 s_ignore (0);
12208 return;
12209 }
12210
12211 mips_cprestore_offset = get_absolute_expression ();
12212 mips_cprestore_valid = 1;
12213
12214 ex.X_op = O_constant;
12215 ex.X_add_symbol = NULL;
12216 ex.X_op_symbol = NULL;
12217 ex.X_add_number = mips_cprestore_offset;
12218
12219 macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12220 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12221 mips_gp_register, SP);
12222
12223 demand_empty_rest_of_line ();
12224 }
12225
12226 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12227 was given in the preceeding .gpsetup, it results in:
12228 ld $gp, offset($sp)
12229
12230 If a register $reg2 was given there, it results in:
12231 daddiu $gp, $gp, $reg2
12232 */
12233 static void
12234 s_cpreturn (ignore)
12235 int ignore ATTRIBUTE_UNUSED;
12236 {
12237 expressionS ex;
12238 int icnt = 0;
12239
12240 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12241 We also need NewABI support. */
12242 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12243 {
12244 s_ignore (0);
12245 return;
12246 }
12247
12248 if (mips_cpreturn_register == -1)
12249 {
12250 ex.X_op = O_constant;
12251 ex.X_add_symbol = NULL;
12252 ex.X_op_symbol = NULL;
12253 ex.X_add_number = mips_cpreturn_offset;
12254
12255 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12256 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12257 }
12258 else
12259 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12260 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12261
12262 demand_empty_rest_of_line ();
12263 }
12264
12265 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12266 code. It sets the offset to use in gp_rel relocations. */
12267
12268 static void
12269 s_gpvalue (ignore)
12270 int ignore ATTRIBUTE_UNUSED;
12271 {
12272 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12273 We also need NewABI support. */
12274 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12275 {
12276 s_ignore (0);
12277 return;
12278 }
12279
12280 mips_gprel_offset = get_absolute_expression ();
12281
12282 demand_empty_rest_of_line ();
12283 }
12284
12285 /* Handle the .gpword pseudo-op. This is used when generating PIC
12286 code. It generates a 32 bit GP relative reloc. */
12287
12288 static void
12289 s_gpword (ignore)
12290 int ignore ATTRIBUTE_UNUSED;
12291 {
12292 symbolS *label;
12293 expressionS ex;
12294 char *p;
12295
12296 /* When not generating PIC code, this is treated as .word. */
12297 if (mips_pic != SVR4_PIC)
12298 {
12299 s_cons (2);
12300 return;
12301 }
12302
12303 label = insn_labels != NULL ? insn_labels->label : NULL;
12304 mips_emit_delays (true);
12305 if (auto_align)
12306 mips_align (2, 0, label);
12307 mips_clear_insn_labels ();
12308
12309 expression (&ex);
12310
12311 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12312 {
12313 as_bad (_("Unsupported use of .gpword"));
12314 ignore_rest_of_line ();
12315 }
12316
12317 p = frag_more (4);
12318 md_number_to_chars (p, (valueT) 0, 4);
12319 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
12320 BFD_RELOC_GPREL32);
12321
12322 demand_empty_rest_of_line ();
12323 }
12324
12325 static void
12326 s_gpdword (ignore)
12327 int ignore ATTRIBUTE_UNUSED;
12328 {
12329 symbolS *label;
12330 expressionS ex;
12331 char *p;
12332
12333 /* When not generating PIC code, this is treated as .dword. */
12334 if (mips_pic != SVR4_PIC)
12335 {
12336 s_cons (3);
12337 return;
12338 }
12339
12340 label = insn_labels != NULL ? insn_labels->label : NULL;
12341 mips_emit_delays (true);
12342 if (auto_align)
12343 mips_align (3, 0, label);
12344 mips_clear_insn_labels ();
12345
12346 expression (&ex);
12347
12348 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12349 {
12350 as_bad (_("Unsupported use of .gpdword"));
12351 ignore_rest_of_line ();
12352 }
12353
12354 p = frag_more (8);
12355 md_number_to_chars (p, (valueT) 0, 8);
12356 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, false,
12357 BFD_RELOC_GPREL32);
12358
12359 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12360 ex.X_op = O_absent;
12361 ex.X_add_symbol = 0;
12362 ex.X_add_number = 0;
12363 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, false,
12364 BFD_RELOC_64);
12365
12366 demand_empty_rest_of_line ();
12367 }
12368
12369 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12370 tables in SVR4 PIC code. */
12371
12372 static void
12373 s_cpadd (ignore)
12374 int ignore ATTRIBUTE_UNUSED;
12375 {
12376 int icnt = 0;
12377 int reg;
12378
12379 /* This is ignored when not generating SVR4 PIC code. */
12380 if (mips_pic != SVR4_PIC)
12381 {
12382 s_ignore (0);
12383 return;
12384 }
12385
12386 /* Add $gp to the register named as an argument. */
12387 reg = tc_get_register (0);
12388 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12389 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12390 "d,v,t", reg, reg, mips_gp_register);
12391
12392 demand_empty_rest_of_line ();
12393 }
12394
12395 /* Handle the .insn pseudo-op. This marks instruction labels in
12396 mips16 mode. This permits the linker to handle them specially,
12397 such as generating jalx instructions when needed. We also make
12398 them odd for the duration of the assembly, in order to generate the
12399 right sort of code. We will make them even in the adjust_symtab
12400 routine, while leaving them marked. This is convenient for the
12401 debugger and the disassembler. The linker knows to make them odd
12402 again. */
12403
12404 static void
12405 s_insn (ignore)
12406 int ignore ATTRIBUTE_UNUSED;
12407 {
12408 mips16_mark_labels ();
12409
12410 demand_empty_rest_of_line ();
12411 }
12412
12413 /* Handle a .stabn directive. We need these in order to mark a label
12414 as being a mips16 text label correctly. Sometimes the compiler
12415 will emit a label, followed by a .stabn, and then switch sections.
12416 If the label and .stabn are in mips16 mode, then the label is
12417 really a mips16 text label. */
12418
12419 static void
12420 s_mips_stab (type)
12421 int type;
12422 {
12423 if (type == 'n')
12424 mips16_mark_labels ();
12425
12426 s_stab (type);
12427 }
12428
12429 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12430 */
12431
12432 static void
12433 s_mips_weakext (ignore)
12434 int ignore ATTRIBUTE_UNUSED;
12435 {
12436 char *name;
12437 int c;
12438 symbolS *symbolP;
12439 expressionS exp;
12440
12441 name = input_line_pointer;
12442 c = get_symbol_end ();
12443 symbolP = symbol_find_or_make (name);
12444 S_SET_WEAK (symbolP);
12445 *input_line_pointer = c;
12446
12447 SKIP_WHITESPACE ();
12448
12449 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12450 {
12451 if (S_IS_DEFINED (symbolP))
12452 {
12453 as_bad ("ignoring attempt to redefine symbol %s",
12454 S_GET_NAME (symbolP));
12455 ignore_rest_of_line ();
12456 return;
12457 }
12458
12459 if (*input_line_pointer == ',')
12460 {
12461 ++input_line_pointer;
12462 SKIP_WHITESPACE ();
12463 }
12464
12465 expression (&exp);
12466 if (exp.X_op != O_symbol)
12467 {
12468 as_bad ("bad .weakext directive");
12469 ignore_rest_of_line ();
12470 return;
12471 }
12472 symbol_set_value_expression (symbolP, &exp);
12473 }
12474
12475 demand_empty_rest_of_line ();
12476 }
12477
12478 /* Parse a register string into a number. Called from the ECOFF code
12479 to parse .frame. The argument is non-zero if this is the frame
12480 register, so that we can record it in mips_frame_reg. */
12481
12482 int
12483 tc_get_register (frame)
12484 int frame;
12485 {
12486 int reg;
12487
12488 SKIP_WHITESPACE ();
12489 if (*input_line_pointer++ != '$')
12490 {
12491 as_warn (_("expected `$'"));
12492 reg = ZERO;
12493 }
12494 else if (ISDIGIT (*input_line_pointer))
12495 {
12496 reg = get_absolute_expression ();
12497 if (reg < 0 || reg >= 32)
12498 {
12499 as_warn (_("Bad register number"));
12500 reg = ZERO;
12501 }
12502 }
12503 else
12504 {
12505 if (strncmp (input_line_pointer, "ra", 2) == 0)
12506 {
12507 reg = RA;
12508 input_line_pointer += 2;
12509 }
12510 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12511 {
12512 reg = FP;
12513 input_line_pointer += 2;
12514 }
12515 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12516 {
12517 reg = SP;
12518 input_line_pointer += 2;
12519 }
12520 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12521 {
12522 reg = GP;
12523 input_line_pointer += 2;
12524 }
12525 else if (strncmp (input_line_pointer, "at", 2) == 0)
12526 {
12527 reg = AT;
12528 input_line_pointer += 2;
12529 }
12530 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12531 {
12532 reg = KT0;
12533 input_line_pointer += 3;
12534 }
12535 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12536 {
12537 reg = KT1;
12538 input_line_pointer += 3;
12539 }
12540 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12541 {
12542 reg = ZERO;
12543 input_line_pointer += 4;
12544 }
12545 else
12546 {
12547 as_warn (_("Unrecognized register name"));
12548 reg = ZERO;
12549 while (ISALNUM(*input_line_pointer))
12550 input_line_pointer++;
12551 }
12552 }
12553 if (frame)
12554 {
12555 mips_frame_reg = reg != 0 ? reg : SP;
12556 mips_frame_reg_valid = 1;
12557 mips_cprestore_valid = 0;
12558 }
12559 return reg;
12560 }
12561
12562 valueT
12563 md_section_align (seg, addr)
12564 asection *seg;
12565 valueT addr;
12566 {
12567 int align = bfd_get_section_alignment (stdoutput, seg);
12568
12569 #ifdef OBJ_ELF
12570 /* We don't need to align ELF sections to the full alignment.
12571 However, Irix 5 may prefer that we align them at least to a 16
12572 byte boundary. We don't bother to align the sections if we are
12573 targeted for an embedded system. */
12574 if (strcmp (TARGET_OS, "elf") == 0)
12575 return addr;
12576 if (align > 4)
12577 align = 4;
12578 #endif
12579
12580 return ((addr + (1 << align) - 1) & (-1 << align));
12581 }
12582
12583 /* Utility routine, called from above as well. If called while the
12584 input file is still being read, it's only an approximation. (For
12585 example, a symbol may later become defined which appeared to be
12586 undefined earlier.) */
12587
12588 static int
12589 nopic_need_relax (sym, before_relaxing)
12590 symbolS *sym;
12591 int before_relaxing;
12592 {
12593 if (sym == 0)
12594 return 0;
12595
12596 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12597 {
12598 const char *symname;
12599 int change;
12600
12601 /* Find out whether this symbol can be referenced off the $gp
12602 register. It can be if it is smaller than the -G size or if
12603 it is in the .sdata or .sbss section. Certain symbols can
12604 not be referenced off the $gp, although it appears as though
12605 they can. */
12606 symname = S_GET_NAME (sym);
12607 if (symname != (const char *) NULL
12608 && (strcmp (symname, "eprol") == 0
12609 || strcmp (symname, "etext") == 0
12610 || strcmp (symname, "_gp") == 0
12611 || strcmp (symname, "edata") == 0
12612 || strcmp (symname, "_fbss") == 0
12613 || strcmp (symname, "_fdata") == 0
12614 || strcmp (symname, "_ftext") == 0
12615 || strcmp (symname, "end") == 0
12616 || strcmp (symname, "_gp_disp") == 0))
12617 change = 1;
12618 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12619 && (0
12620 #ifndef NO_ECOFF_DEBUGGING
12621 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12622 && (symbol_get_obj (sym)->ecoff_extern_size
12623 <= g_switch_value))
12624 #endif
12625 /* We must defer this decision until after the whole
12626 file has been read, since there might be a .extern
12627 after the first use of this symbol. */
12628 || (before_relaxing
12629 #ifndef NO_ECOFF_DEBUGGING
12630 && symbol_get_obj (sym)->ecoff_extern_size == 0
12631 #endif
12632 && S_GET_VALUE (sym) == 0)
12633 || (S_GET_VALUE (sym) != 0
12634 && S_GET_VALUE (sym) <= g_switch_value)))
12635 change = 0;
12636 else
12637 {
12638 const char *segname;
12639
12640 segname = segment_name (S_GET_SEGMENT (sym));
12641 assert (strcmp (segname, ".lit8") != 0
12642 && strcmp (segname, ".lit4") != 0);
12643 change = (strcmp (segname, ".sdata") != 0
12644 && strcmp (segname, ".sbss") != 0
12645 && strncmp (segname, ".sdata.", 7) != 0
12646 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12647 }
12648 return change;
12649 }
12650 else
12651 /* We are not optimizing for the $gp register. */
12652 return 1;
12653 }
12654
12655 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12656 extended opcode. SEC is the section the frag is in. */
12657
12658 static int
12659 mips16_extended_frag (fragp, sec, stretch)
12660 fragS *fragp;
12661 asection *sec;
12662 long stretch;
12663 {
12664 int type;
12665 register const struct mips16_immed_operand *op;
12666 offsetT val;
12667 int mintiny, maxtiny;
12668 segT symsec;
12669 fragS *sym_frag;
12670
12671 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12672 return 0;
12673 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12674 return 1;
12675
12676 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12677 op = mips16_immed_operands;
12678 while (op->type != type)
12679 {
12680 ++op;
12681 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12682 }
12683
12684 if (op->unsp)
12685 {
12686 if (type == '<' || type == '>' || type == '[' || type == ']')
12687 {
12688 mintiny = 1;
12689 maxtiny = 1 << op->nbits;
12690 }
12691 else
12692 {
12693 mintiny = 0;
12694 maxtiny = (1 << op->nbits) - 1;
12695 }
12696 }
12697 else
12698 {
12699 mintiny = - (1 << (op->nbits - 1));
12700 maxtiny = (1 << (op->nbits - 1)) - 1;
12701 }
12702
12703 sym_frag = symbol_get_frag (fragp->fr_symbol);
12704 val = S_GET_VALUE (fragp->fr_symbol);
12705 symsec = S_GET_SEGMENT (fragp->fr_symbol);
12706
12707 if (op->pcrel)
12708 {
12709 addressT addr;
12710
12711 /* We won't have the section when we are called from
12712 mips_relax_frag. However, we will always have been called
12713 from md_estimate_size_before_relax first. If this is a
12714 branch to a different section, we mark it as such. If SEC is
12715 NULL, and the frag is not marked, then it must be a branch to
12716 the same section. */
12717 if (sec == NULL)
12718 {
12719 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12720 return 1;
12721 }
12722 else
12723 {
12724 /* Must have been called from md_estimate_size_before_relax. */
12725 if (symsec != sec)
12726 {
12727 fragp->fr_subtype =
12728 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12729
12730 /* FIXME: We should support this, and let the linker
12731 catch branches and loads that are out of range. */
12732 as_bad_where (fragp->fr_file, fragp->fr_line,
12733 _("unsupported PC relative reference to different section"));
12734
12735 return 1;
12736 }
12737 if (fragp != sym_frag && sym_frag->fr_address == 0)
12738 /* Assume non-extended on the first relaxation pass.
12739 The address we have calculated will be bogus if this is
12740 a forward branch to another frag, as the forward frag
12741 will have fr_address == 0. */
12742 return 0;
12743 }
12744
12745 /* In this case, we know for sure that the symbol fragment is in
12746 the same section. If the relax_marker of the symbol fragment
12747 differs from the relax_marker of this fragment, we have not
12748 yet adjusted the symbol fragment fr_address. We want to add
12749 in STRETCH in order to get a better estimate of the address.
12750 This particularly matters because of the shift bits. */
12751 if (stretch != 0
12752 && sym_frag->relax_marker != fragp->relax_marker)
12753 {
12754 fragS *f;
12755
12756 /* Adjust stretch for any alignment frag. Note that if have
12757 been expanding the earlier code, the symbol may be
12758 defined in what appears to be an earlier frag. FIXME:
12759 This doesn't handle the fr_subtype field, which specifies
12760 a maximum number of bytes to skip when doing an
12761 alignment. */
12762 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12763 {
12764 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12765 {
12766 if (stretch < 0)
12767 stretch = - ((- stretch)
12768 & ~ ((1 << (int) f->fr_offset) - 1));
12769 else
12770 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12771 if (stretch == 0)
12772 break;
12773 }
12774 }
12775 if (f != NULL)
12776 val += stretch;
12777 }
12778
12779 addr = fragp->fr_address + fragp->fr_fix;
12780
12781 /* The base address rules are complicated. The base address of
12782 a branch is the following instruction. The base address of a
12783 PC relative load or add is the instruction itself, but if it
12784 is in a delay slot (in which case it can not be extended) use
12785 the address of the instruction whose delay slot it is in. */
12786 if (type == 'p' || type == 'q')
12787 {
12788 addr += 2;
12789
12790 /* If we are currently assuming that this frag should be
12791 extended, then, the current address is two bytes
12792 higher. */
12793 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12794 addr += 2;
12795
12796 /* Ignore the low bit in the target, since it will be set
12797 for a text label. */
12798 if ((val & 1) != 0)
12799 --val;
12800 }
12801 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12802 addr -= 4;
12803 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12804 addr -= 2;
12805
12806 val -= addr & ~ ((1 << op->shift) - 1);
12807
12808 /* Branch offsets have an implicit 0 in the lowest bit. */
12809 if (type == 'p' || type == 'q')
12810 val /= 2;
12811
12812 /* If any of the shifted bits are set, we must use an extended
12813 opcode. If the address depends on the size of this
12814 instruction, this can lead to a loop, so we arrange to always
12815 use an extended opcode. We only check this when we are in
12816 the main relaxation loop, when SEC is NULL. */
12817 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12818 {
12819 fragp->fr_subtype =
12820 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12821 return 1;
12822 }
12823
12824 /* If we are about to mark a frag as extended because the value
12825 is precisely maxtiny + 1, then there is a chance of an
12826 infinite loop as in the following code:
12827 la $4,foo
12828 .skip 1020
12829 .align 2
12830 foo:
12831 In this case when the la is extended, foo is 0x3fc bytes
12832 away, so the la can be shrunk, but then foo is 0x400 away, so
12833 the la must be extended. To avoid this loop, we mark the
12834 frag as extended if it was small, and is about to become
12835 extended with a value of maxtiny + 1. */
12836 if (val == ((maxtiny + 1) << op->shift)
12837 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12838 && sec == NULL)
12839 {
12840 fragp->fr_subtype =
12841 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12842 return 1;
12843 }
12844 }
12845 else if (symsec != absolute_section && sec != NULL)
12846 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12847
12848 if ((val & ((1 << op->shift) - 1)) != 0
12849 || val < (mintiny << op->shift)
12850 || val > (maxtiny << op->shift))
12851 return 1;
12852 else
12853 return 0;
12854 }
12855
12856 /* Compute the length of a branch sequence, and adjust the
12857 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12858 worst-case length is computed, with UPDATE being used to indicate
12859 whether an unconditional (-1), branch-likely (+1) or regular (0)
12860 branch is to be computed. */
12861 static int
12862 relaxed_branch_length (fragp, sec, update)
12863 fragS *fragp;
12864 asection *sec;
12865 int update;
12866 {
12867 boolean toofar;
12868 int length;
12869
12870 if (fragp
12871 && S_IS_DEFINED (fragp->fr_symbol)
12872 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12873 {
12874 addressT addr;
12875 offsetT val;
12876
12877 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12878
12879 addr = fragp->fr_address + fragp->fr_fix + 4;
12880
12881 val -= addr;
12882
12883 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12884 }
12885 else if (fragp)
12886 /* If the symbol is not defined or it's in a different segment,
12887 assume the user knows what's going on and emit a short
12888 branch. */
12889 toofar = false;
12890 else
12891 toofar = true;
12892
12893 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12894 fragp->fr_subtype
12895 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_RELOC_S2 (fragp->fr_subtype),
12896 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12897 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12898 RELAX_BRANCH_LINK (fragp->fr_subtype),
12899 toofar);
12900
12901 length = 4;
12902 if (toofar)
12903 {
12904 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12905 length += 8;
12906
12907 if (mips_pic != NO_PIC)
12908 {
12909 /* Additional space for PIC loading of target address. */
12910 length += 8;
12911 if (mips_opts.isa == ISA_MIPS1)
12912 /* Additional space for $at-stabilizing nop. */
12913 length += 4;
12914 }
12915
12916 /* If branch is conditional. */
12917 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12918 length += 8;
12919 }
12920
12921 return length;
12922 }
12923
12924 /* Estimate the size of a frag before relaxing. Unless this is the
12925 mips16, we are not really relaxing here, and the final size is
12926 encoded in the subtype information. For the mips16, we have to
12927 decide whether we are using an extended opcode or not. */
12928
12929 int
12930 md_estimate_size_before_relax (fragp, segtype)
12931 fragS *fragp;
12932 asection *segtype;
12933 {
12934 int change = 0;
12935 boolean linkonce = false;
12936
12937 if (RELAX_BRANCH_P (fragp->fr_subtype))
12938 {
12939
12940 fragp->fr_var = relaxed_branch_length (fragp, segtype, false);
12941
12942 return fragp->fr_var;
12943 }
12944
12945 if (RELAX_MIPS16_P (fragp->fr_subtype))
12946 /* We don't want to modify the EXTENDED bit here; it might get us
12947 into infinite loops. We change it only in mips_relax_frag(). */
12948 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12949
12950 if (mips_pic == NO_PIC)
12951 {
12952 change = nopic_need_relax (fragp->fr_symbol, 0);
12953 }
12954 else if (mips_pic == SVR4_PIC)
12955 {
12956 symbolS *sym;
12957 asection *symsec;
12958
12959 sym = fragp->fr_symbol;
12960
12961 /* Handle the case of a symbol equated to another symbol. */
12962 while (symbol_equated_reloc_p (sym))
12963 {
12964 symbolS *n;
12965
12966 /* It's possible to get a loop here in a badly written
12967 program. */
12968 n = symbol_get_value_expression (sym)->X_add_symbol;
12969 if (n == sym)
12970 break;
12971 sym = n;
12972 }
12973
12974 symsec = S_GET_SEGMENT (sym);
12975
12976 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12977 if (symsec != segtype && ! S_IS_LOCAL (sym))
12978 {
12979 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12980 != 0)
12981 linkonce = true;
12982
12983 /* The GNU toolchain uses an extension for ELF: a section
12984 beginning with the magic string .gnu.linkonce is a linkonce
12985 section. */
12986 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12987 sizeof ".gnu.linkonce" - 1) == 0)
12988 linkonce = true;
12989 }
12990
12991 /* This must duplicate the test in adjust_reloc_syms. */
12992 change = (symsec != &bfd_und_section
12993 && symsec != &bfd_abs_section
12994 && ! bfd_is_com_section (symsec)
12995 && !linkonce
12996 #ifdef OBJ_ELF
12997 /* A global or weak symbol is treated as external. */
12998 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12999 || (! S_IS_WEAK (sym)
13000 && (! S_IS_EXTERNAL (sym)
13001 || mips_pic == EMBEDDED_PIC)))
13002 #endif
13003 );
13004 }
13005 else
13006 abort ();
13007
13008 if (change)
13009 {
13010 /* Record the offset to the first reloc in the fr_opcode field.
13011 This lets md_convert_frag and tc_gen_reloc know that the code
13012 must be expanded. */
13013 fragp->fr_opcode = (fragp->fr_literal
13014 + fragp->fr_fix
13015 - RELAX_OLD (fragp->fr_subtype)
13016 + RELAX_RELOC1 (fragp->fr_subtype));
13017 /* FIXME: This really needs as_warn_where. */
13018 if (RELAX_WARN (fragp->fr_subtype))
13019 as_warn (_("AT used after \".set noat\" or macro used after "
13020 "\".set nomacro\""));
13021
13022 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13023 }
13024
13025 return 0;
13026 }
13027
13028 /* This is called to see whether a reloc against a defined symbol
13029 should be converted into a reloc against a section. Don't adjust
13030 MIPS16 jump relocations, so we don't have to worry about the format
13031 of the offset in the .o file. Don't adjust relocations against
13032 mips16 symbols, so that the linker can find them if it needs to set
13033 up a stub. */
13034
13035 int
13036 mips_fix_adjustable (fixp)
13037 fixS *fixp;
13038 {
13039 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13040 return 0;
13041
13042 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13043 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13044 return 0;
13045
13046 if (fixp->fx_addsy == NULL)
13047 return 1;
13048
13049 #ifdef OBJ_ELF
13050 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13051 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13052 && fixp->fx_subsy == NULL)
13053 return 0;
13054 #endif
13055
13056 return 1;
13057 }
13058
13059 /* Translate internal representation of relocation info to BFD target
13060 format. */
13061
13062 arelent **
13063 tc_gen_reloc (section, fixp)
13064 asection *section ATTRIBUTE_UNUSED;
13065 fixS *fixp;
13066 {
13067 static arelent *retval[4];
13068 arelent *reloc;
13069 bfd_reloc_code_real_type code;
13070
13071 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13072 retval[1] = NULL;
13073
13074 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13075 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13076 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13077
13078 if (mips_pic == EMBEDDED_PIC
13079 && SWITCH_TABLE (fixp))
13080 {
13081 /* For a switch table entry we use a special reloc. The addend
13082 is actually the difference between the reloc address and the
13083 subtrahend. */
13084 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13085 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13086 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13087 fixp->fx_r_type = BFD_RELOC_GPREL32;
13088 }
13089 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13090 {
13091 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13092 reloc->addend = fixp->fx_addnumber;
13093 else
13094 {
13095 /* We use a special addend for an internal RELLO reloc. */
13096 if (symbol_section_p (fixp->fx_addsy))
13097 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13098 else
13099 reloc->addend = fixp->fx_addnumber + reloc->address;
13100 }
13101 }
13102 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13103 {
13104 assert (fixp->fx_next != NULL
13105 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13106
13107 /* The reloc is relative to the RELLO; adjust the addend
13108 accordingly. */
13109 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13110 reloc->addend = fixp->fx_next->fx_addnumber;
13111 else
13112 {
13113 /* We use a special addend for an internal RELHI reloc. */
13114 if (symbol_section_p (fixp->fx_addsy))
13115 reloc->addend = (fixp->fx_next->fx_frag->fr_address
13116 + fixp->fx_next->fx_where
13117 - S_GET_VALUE (fixp->fx_subsy));
13118 else
13119 reloc->addend = (fixp->fx_addnumber
13120 + fixp->fx_next->fx_frag->fr_address
13121 + fixp->fx_next->fx_where);
13122 }
13123 }
13124 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13125 reloc->addend = fixp->fx_addnumber;
13126 else
13127 {
13128 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13129 /* A gruesome hack which is a result of the gruesome gas reloc
13130 handling. */
13131 reloc->addend = reloc->address;
13132 else
13133 reloc->addend = -reloc->address;
13134 }
13135
13136 /* If this is a variant frag, we may need to adjust the existing
13137 reloc and generate a new one. */
13138 if (fixp->fx_frag->fr_opcode != NULL
13139 && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13140 && ! HAVE_NEWABI)
13141 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13142 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13143 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13144 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13145 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13146 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13147 )
13148 {
13149 arelent *reloc2;
13150
13151 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13152
13153 /* If this is not the last reloc in this frag, then we have two
13154 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13155 CALL_HI16/CALL_LO16, both of which are being replaced. Let
13156 the second one handle all of them. */
13157 if (fixp->fx_next != NULL
13158 && fixp->fx_frag == fixp->fx_next->fx_frag)
13159 {
13160 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13161 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13162 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13163 && (fixp->fx_next->fx_r_type
13164 == BFD_RELOC_MIPS_GOT_LO16))
13165 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13166 && (fixp->fx_next->fx_r_type
13167 == BFD_RELOC_MIPS_CALL_LO16)));
13168 retval[0] = NULL;
13169 return retval;
13170 }
13171
13172 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13173 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13174 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13175 retval[2] = NULL;
13176 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13177 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13178 reloc2->address = (reloc->address
13179 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13180 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13181 reloc2->addend = fixp->fx_addnumber;
13182 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13183 assert (reloc2->howto != NULL);
13184
13185 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13186 {
13187 arelent *reloc3;
13188
13189 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13190 retval[3] = NULL;
13191 *reloc3 = *reloc2;
13192 reloc3->address += 4;
13193 }
13194
13195 if (mips_pic == NO_PIC)
13196 {
13197 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13198 fixp->fx_r_type = BFD_RELOC_HI16_S;
13199 }
13200 else if (mips_pic == SVR4_PIC)
13201 {
13202 switch (fixp->fx_r_type)
13203 {
13204 default:
13205 abort ();
13206 case BFD_RELOC_MIPS_GOT16:
13207 break;
13208 case BFD_RELOC_MIPS_GOT_LO16:
13209 case BFD_RELOC_MIPS_CALL_LO16:
13210 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13211 break;
13212 case BFD_RELOC_MIPS_CALL16:
13213 if (HAVE_NEWABI)
13214 {
13215 /* BFD_RELOC_MIPS_GOT16;*/
13216 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13217 reloc2->howto = bfd_reloc_type_lookup
13218 (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13219 }
13220 else
13221 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13222 break;
13223 }
13224 }
13225 else
13226 abort ();
13227
13228 /* newabi uses R_MIPS_GOT_DISP for local symbols */
13229 if (HAVE_NEWABI && BFD_RELOC_MIPS_GOT_LO16)
13230 {
13231 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13232 retval[1] = NULL;
13233 }
13234 }
13235
13236 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13237 entry to be used in the relocation's section offset. */
13238 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13239 {
13240 reloc->address = reloc->addend;
13241 reloc->addend = 0;
13242 }
13243
13244 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13245 fixup_segment converted a non-PC relative reloc into a PC
13246 relative reloc. In such a case, we need to convert the reloc
13247 code. */
13248 code = fixp->fx_r_type;
13249 if (fixp->fx_pcrel)
13250 {
13251 switch (code)
13252 {
13253 case BFD_RELOC_8:
13254 code = BFD_RELOC_8_PCREL;
13255 break;
13256 case BFD_RELOC_16:
13257 code = BFD_RELOC_16_PCREL;
13258 break;
13259 case BFD_RELOC_32:
13260 code = BFD_RELOC_32_PCREL;
13261 break;
13262 case BFD_RELOC_64:
13263 code = BFD_RELOC_64_PCREL;
13264 break;
13265 case BFD_RELOC_8_PCREL:
13266 case BFD_RELOC_16_PCREL:
13267 case BFD_RELOC_32_PCREL:
13268 case BFD_RELOC_64_PCREL:
13269 case BFD_RELOC_16_PCREL_S2:
13270 case BFD_RELOC_PCREL_HI16_S:
13271 case BFD_RELOC_PCREL_LO16:
13272 break;
13273 default:
13274 as_bad_where (fixp->fx_file, fixp->fx_line,
13275 _("Cannot make %s relocation PC relative"),
13276 bfd_get_reloc_code_name (code));
13277 }
13278 }
13279
13280 #ifdef OBJ_ELF
13281 /* md_apply_fix3 has a double-subtraction hack to get
13282 bfd_install_relocation to behave nicely. GPREL relocations are
13283 handled correctly without this hack, so undo it here. We can't
13284 stop md_apply_fix3 from subtracting twice in the first place since
13285 the fake addend is required for variant frags above. */
13286 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
13287 && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
13288 && reloc->addend != 0
13289 && mips_need_elf_addend_fixup (fixp))
13290 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13291 #endif
13292
13293 /* To support a PC relative reloc when generating embedded PIC code
13294 for ECOFF, we use a Cygnus extension. We check for that here to
13295 make sure that we don't let such a reloc escape normally. */
13296 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13297 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13298 && code == BFD_RELOC_16_PCREL_S2
13299 && mips_pic != EMBEDDED_PIC)
13300 reloc->howto = NULL;
13301 else
13302 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13303
13304 if (reloc->howto == NULL)
13305 {
13306 as_bad_where (fixp->fx_file, fixp->fx_line,
13307 _("Can not represent %s relocation in this object file format"),
13308 bfd_get_reloc_code_name (code));
13309 retval[0] = NULL;
13310 }
13311
13312 return retval;
13313 }
13314
13315 /* Relax a machine dependent frag. This returns the amount by which
13316 the current size of the frag should change. */
13317
13318 int
13319 mips_relax_frag (sec, fragp, stretch)
13320 asection *sec;
13321 fragS *fragp;
13322 long stretch;
13323 {
13324 if (RELAX_BRANCH_P (fragp->fr_subtype))
13325 {
13326 offsetT old_var = fragp->fr_var;
13327
13328 fragp->fr_var = relaxed_branch_length (fragp, sec, true);
13329
13330 return fragp->fr_var - old_var;
13331 }
13332
13333 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13334 return 0;
13335
13336 if (mips16_extended_frag (fragp, NULL, stretch))
13337 {
13338 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13339 return 0;
13340 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13341 return 2;
13342 }
13343 else
13344 {
13345 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13346 return 0;
13347 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13348 return -2;
13349 }
13350
13351 return 0;
13352 }
13353
13354 /* Convert a machine dependent frag. */
13355
13356 void
13357 md_convert_frag (abfd, asec, fragp)
13358 bfd *abfd ATTRIBUTE_UNUSED;
13359 segT asec;
13360 fragS *fragp;
13361 {
13362 int old, new;
13363 char *fixptr;
13364
13365 if (RELAX_BRANCH_P (fragp->fr_subtype))
13366 {
13367 bfd_byte *buf;
13368 unsigned long insn;
13369 expressionS exp;
13370 fixS *fixp;
13371
13372 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13373
13374 if (target_big_endian)
13375 insn = bfd_getb32 (buf);
13376 else
13377 insn = bfd_getl32 (buf);
13378
13379 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13380 {
13381 /* We generate a fixup instead of applying it right now
13382 because, if there are linker relaxations, we're going to
13383 need the relocations. */
13384 exp.X_op = O_symbol;
13385 exp.X_add_symbol = fragp->fr_symbol;
13386 exp.X_add_number = fragp->fr_offset;
13387
13388 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13389 4, &exp, 1,
13390 RELAX_BRANCH_RELOC_S2 (fragp->fr_subtype)
13391 ? BFD_RELOC_16_PCREL_S2
13392 : BFD_RELOC_16_PCREL);
13393 fixp->fx_file = fragp->fr_file;
13394 fixp->fx_line = fragp->fr_line;
13395
13396 md_number_to_chars ((char *)buf, insn, 4);
13397 buf += 4;
13398 }
13399 else
13400 {
13401 int i;
13402
13403 as_warn_where (fragp->fr_file, fragp->fr_line,
13404 _("relaxed out-of-range branch into a jump"));
13405
13406 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13407 goto uncond;
13408
13409 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13410 {
13411 /* Reverse the branch. */
13412 switch ((insn >> 28) & 0xf)
13413 {
13414 case 4:
13415 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13416 have the condition reversed by tweaking a single
13417 bit, and their opcodes all have 0x4???????. */
13418 assert ((insn & 0xf1000000) == 0x41000000);
13419 insn ^= 0x00010000;
13420 break;
13421
13422 case 0:
13423 /* bltz 0x04000000 bgez 0x04010000
13424 bltzal 0x04100000 bgezal 0x04110000 */
13425 assert ((insn & 0xfc0e0000) == 0x04000000);
13426 insn ^= 0x00010000;
13427 break;
13428
13429 case 1:
13430 /* beq 0x10000000 bne 0x14000000
13431 blez 0x18000000 bgtz 0x1c000000 */
13432 insn ^= 0x04000000;
13433 break;
13434
13435 default:
13436 abort ();
13437 }
13438 }
13439
13440 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13441 {
13442 /* Clear the and-link bit. */
13443 assert ((insn & 0xfc1c0000) == 0x04100000);
13444
13445 /* bltzal 0x04100000 bgezal 0x04110000
13446 bltzall 0x04120000 bgezall 0x04130000 */
13447 insn &= ~0x00100000;
13448 }
13449
13450 /* Branch over the branch (if the branch was likely) or the
13451 full jump (not likely case). Compute the offset from the
13452 current instruction to branch to. */
13453 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13454 i = 16;
13455 else
13456 {
13457 /* How many bytes in instructions we've already emitted? */
13458 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13459 /* How many bytes in instructions from here to the end? */
13460 i = fragp->fr_var - i;
13461 }
13462 /* Convert to instruction count. */
13463 i >>= 2;
13464 /* Branch counts from the next instruction. */
13465 i--;
13466 insn |= i;
13467 /* Branch over the jump. */
13468 md_number_to_chars ((char *)buf, insn, 4);
13469 buf += 4;
13470
13471 /* Nop */
13472 md_number_to_chars ((char*)buf, 0, 4);
13473 buf += 4;
13474
13475 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13476 {
13477 /* beql $0, $0, 2f */
13478 insn = 0x50000000;
13479 /* Compute the PC offset from the current instruction to
13480 the end of the variable frag. */
13481 /* How many bytes in instructions we've already emitted? */
13482 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13483 /* How many bytes in instructions from here to the end? */
13484 i = fragp->fr_var - i;
13485 /* Convert to instruction count. */
13486 i >>= 2;
13487 /* Don't decrement i, because we want to branch over the
13488 delay slot. */
13489
13490 insn |= i;
13491 md_number_to_chars ((char *)buf, insn, 4);
13492 buf += 4;
13493
13494 md_number_to_chars ((char *)buf, 0, 4);
13495 buf += 4;
13496 }
13497
13498 uncond:
13499 if (mips_pic == NO_PIC)
13500 {
13501 /* j or jal. */
13502 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13503 ? 0x0c000000 : 0x08000000);
13504 exp.X_op = O_symbol;
13505 exp.X_add_symbol = fragp->fr_symbol;
13506 exp.X_add_number = fragp->fr_offset;
13507
13508 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13509 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13510 fixp->fx_file = fragp->fr_file;
13511 fixp->fx_line = fragp->fr_line;
13512
13513 md_number_to_chars ((char*)buf, insn, 4);
13514 buf += 4;
13515 }
13516 else
13517 {
13518 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13519 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13520 exp.X_op = O_symbol;
13521 exp.X_add_symbol = fragp->fr_symbol;
13522 exp.X_add_number = fragp->fr_offset;
13523
13524 if (fragp->fr_offset)
13525 {
13526 exp.X_add_symbol = make_expr_symbol (&exp);
13527 exp.X_add_number = 0;
13528 }
13529
13530 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13531 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13532 fixp->fx_file = fragp->fr_file;
13533 fixp->fx_line = fragp->fr_line;
13534
13535 md_number_to_chars ((char*)buf, insn, 4);
13536 buf += 4;
13537
13538 if (mips_opts.isa == ISA_MIPS1)
13539 {
13540 /* nop */
13541 md_number_to_chars ((char*)buf, 0, 4);
13542 buf += 4;
13543 }
13544
13545 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13546 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13547
13548 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13549 4, &exp, 0, BFD_RELOC_LO16);
13550 fixp->fx_file = fragp->fr_file;
13551 fixp->fx_line = fragp->fr_line;
13552
13553 md_number_to_chars ((char*)buf, insn, 4);
13554 buf += 4;
13555
13556 /* j(al)r $at. */
13557 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13558 insn = 0x0020f809;
13559 else
13560 insn = 0x00200008;
13561
13562 md_number_to_chars ((char*)buf, insn, 4);
13563 buf += 4;
13564 }
13565 }
13566
13567 assert (buf == (bfd_byte *)fragp->fr_literal
13568 + fragp->fr_fix + fragp->fr_var);
13569
13570 fragp->fr_fix += fragp->fr_var;
13571
13572 return;
13573 }
13574
13575 if (RELAX_MIPS16_P (fragp->fr_subtype))
13576 {
13577 int type;
13578 register const struct mips16_immed_operand *op;
13579 boolean small, ext;
13580 offsetT val;
13581 bfd_byte *buf;
13582 unsigned long insn;
13583 boolean use_extend;
13584 unsigned short extend;
13585
13586 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13587 op = mips16_immed_operands;
13588 while (op->type != type)
13589 ++op;
13590
13591 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13592 {
13593 small = false;
13594 ext = true;
13595 }
13596 else
13597 {
13598 small = true;
13599 ext = false;
13600 }
13601
13602 resolve_symbol_value (fragp->fr_symbol);
13603 val = S_GET_VALUE (fragp->fr_symbol);
13604 if (op->pcrel)
13605 {
13606 addressT addr;
13607
13608 addr = fragp->fr_address + fragp->fr_fix;
13609
13610 /* The rules for the base address of a PC relative reloc are
13611 complicated; see mips16_extended_frag. */
13612 if (type == 'p' || type == 'q')
13613 {
13614 addr += 2;
13615 if (ext)
13616 addr += 2;
13617 /* Ignore the low bit in the target, since it will be
13618 set for a text label. */
13619 if ((val & 1) != 0)
13620 --val;
13621 }
13622 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13623 addr -= 4;
13624 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13625 addr -= 2;
13626
13627 addr &= ~ (addressT) ((1 << op->shift) - 1);
13628 val -= addr;
13629
13630 /* Make sure the section winds up with the alignment we have
13631 assumed. */
13632 if (op->shift > 0)
13633 record_alignment (asec, op->shift);
13634 }
13635
13636 if (ext
13637 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13638 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13639 as_warn_where (fragp->fr_file, fragp->fr_line,
13640 _("extended instruction in delay slot"));
13641
13642 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13643
13644 if (target_big_endian)
13645 insn = bfd_getb16 (buf);
13646 else
13647 insn = bfd_getl16 (buf);
13648
13649 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13650 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13651 small, ext, &insn, &use_extend, &extend);
13652
13653 if (use_extend)
13654 {
13655 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13656 fragp->fr_fix += 2;
13657 buf += 2;
13658 }
13659
13660 md_number_to_chars ((char *) buf, insn, 2);
13661 fragp->fr_fix += 2;
13662 buf += 2;
13663 }
13664 else
13665 {
13666 if (fragp->fr_opcode == NULL)
13667 return;
13668
13669 old = RELAX_OLD (fragp->fr_subtype);
13670 new = RELAX_NEW (fragp->fr_subtype);
13671 fixptr = fragp->fr_literal + fragp->fr_fix;
13672
13673 if (new > 0)
13674 memcpy (fixptr - old, fixptr, new);
13675
13676 fragp->fr_fix += new - old;
13677 }
13678 }
13679
13680 #ifdef OBJ_ELF
13681
13682 /* This function is called after the relocs have been generated.
13683 We've been storing mips16 text labels as odd. Here we convert them
13684 back to even for the convenience of the debugger. */
13685
13686 void
13687 mips_frob_file_after_relocs ()
13688 {
13689 asymbol **syms;
13690 unsigned int count, i;
13691
13692 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13693 return;
13694
13695 syms = bfd_get_outsymbols (stdoutput);
13696 count = bfd_get_symcount (stdoutput);
13697 for (i = 0; i < count; i++, syms++)
13698 {
13699 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13700 && ((*syms)->value & 1) != 0)
13701 {
13702 (*syms)->value &= ~1;
13703 /* If the symbol has an odd size, it was probably computed
13704 incorrectly, so adjust that as well. */
13705 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13706 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13707 }
13708 }
13709 }
13710
13711 #endif
13712
13713 /* This function is called whenever a label is defined. It is used
13714 when handling branch delays; if a branch has a label, we assume we
13715 can not move it. */
13716
13717 void
13718 mips_define_label (sym)
13719 symbolS *sym;
13720 {
13721 struct insn_label_list *l;
13722
13723 if (free_insn_labels == NULL)
13724 l = (struct insn_label_list *) xmalloc (sizeof *l);
13725 else
13726 {
13727 l = free_insn_labels;
13728 free_insn_labels = l->next;
13729 }
13730
13731 l->label = sym;
13732 l->next = insn_labels;
13733 insn_labels = l;
13734 }
13735 \f
13736 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13737
13738 /* Some special processing for a MIPS ELF file. */
13739
13740 void
13741 mips_elf_final_processing ()
13742 {
13743 /* Write out the register information. */
13744 if (mips_abi != N64_ABI)
13745 {
13746 Elf32_RegInfo s;
13747
13748 s.ri_gprmask = mips_gprmask;
13749 s.ri_cprmask[0] = mips_cprmask[0];
13750 s.ri_cprmask[1] = mips_cprmask[1];
13751 s.ri_cprmask[2] = mips_cprmask[2];
13752 s.ri_cprmask[3] = mips_cprmask[3];
13753 /* The gp_value field is set by the MIPS ELF backend. */
13754
13755 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13756 ((Elf32_External_RegInfo *)
13757 mips_regmask_frag));
13758 }
13759 else
13760 {
13761 Elf64_Internal_RegInfo s;
13762
13763 s.ri_gprmask = mips_gprmask;
13764 s.ri_pad = 0;
13765 s.ri_cprmask[0] = mips_cprmask[0];
13766 s.ri_cprmask[1] = mips_cprmask[1];
13767 s.ri_cprmask[2] = mips_cprmask[2];
13768 s.ri_cprmask[3] = mips_cprmask[3];
13769 /* The gp_value field is set by the MIPS ELF backend. */
13770
13771 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13772 ((Elf64_External_RegInfo *)
13773 mips_regmask_frag));
13774 }
13775
13776 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13777 sort of BFD interface for this. */
13778 if (mips_any_noreorder)
13779 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13780 if (mips_pic != NO_PIC)
13781 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13782
13783 /* Set MIPS ELF flags for ASEs. */
13784 if (file_ase_mips16)
13785 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13786 #if 0 /* XXX FIXME */
13787 if (file_ase_mips3d)
13788 elf_elfheader (stdoutput)->e_flags |= ???;
13789 #endif
13790 if (file_ase_mdmx)
13791 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13792
13793 /* Set the MIPS ELF ABI flags. */
13794 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13795 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13796 else if (mips_abi == O64_ABI)
13797 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13798 else if (mips_abi == EABI_ABI)
13799 {
13800 if (!file_mips_gp32)
13801 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13802 else
13803 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13804 }
13805 else if (mips_abi == N32_ABI)
13806 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13807
13808 /* Nothing to do for N64_ABI. */
13809
13810 if (mips_32bitmode)
13811 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13812 }
13813
13814 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13815 \f
13816 typedef struct proc {
13817 symbolS *isym;
13818 unsigned long reg_mask;
13819 unsigned long reg_offset;
13820 unsigned long fpreg_mask;
13821 unsigned long fpreg_offset;
13822 unsigned long frame_offset;
13823 unsigned long frame_reg;
13824 unsigned long pc_reg;
13825 } procS;
13826
13827 static procS cur_proc;
13828 static procS *cur_proc_ptr;
13829 static int numprocs;
13830
13831 /* Fill in an rs_align_code fragment. */
13832
13833 void
13834 mips_handle_align (fragp)
13835 fragS *fragp;
13836 {
13837 if (fragp->fr_type != rs_align_code)
13838 return;
13839
13840 if (mips_opts.mips16)
13841 {
13842 static const unsigned char be_nop[] = { 0x65, 0x00 };
13843 static const unsigned char le_nop[] = { 0x00, 0x65 };
13844
13845 int bytes;
13846 char *p;
13847
13848 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13849 p = fragp->fr_literal + fragp->fr_fix;
13850
13851 if (bytes & 1)
13852 {
13853 *p++ = 0;
13854 fragp->fr_fix++;
13855 }
13856
13857 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13858 fragp->fr_var = 2;
13859 }
13860
13861 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
13862 }
13863
13864 static void
13865 md_obj_begin ()
13866 {
13867 }
13868
13869 static void
13870 md_obj_end ()
13871 {
13872 /* check for premature end, nesting errors, etc */
13873 if (cur_proc_ptr)
13874 as_warn (_("missing .end at end of assembly"));
13875 }
13876
13877 static long
13878 get_number ()
13879 {
13880 int negative = 0;
13881 long val = 0;
13882
13883 if (*input_line_pointer == '-')
13884 {
13885 ++input_line_pointer;
13886 negative = 1;
13887 }
13888 if (!ISDIGIT (*input_line_pointer))
13889 as_bad (_("expected simple number"));
13890 if (input_line_pointer[0] == '0')
13891 {
13892 if (input_line_pointer[1] == 'x')
13893 {
13894 input_line_pointer += 2;
13895 while (ISXDIGIT (*input_line_pointer))
13896 {
13897 val <<= 4;
13898 val |= hex_value (*input_line_pointer++);
13899 }
13900 return negative ? -val : val;
13901 }
13902 else
13903 {
13904 ++input_line_pointer;
13905 while (ISDIGIT (*input_line_pointer))
13906 {
13907 val <<= 3;
13908 val |= *input_line_pointer++ - '0';
13909 }
13910 return negative ? -val : val;
13911 }
13912 }
13913 if (!ISDIGIT (*input_line_pointer))
13914 {
13915 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13916 *input_line_pointer, *input_line_pointer);
13917 as_warn (_("invalid number"));
13918 return -1;
13919 }
13920 while (ISDIGIT (*input_line_pointer))
13921 {
13922 val *= 10;
13923 val += *input_line_pointer++ - '0';
13924 }
13925 return negative ? -val : val;
13926 }
13927
13928 /* The .file directive; just like the usual .file directive, but there
13929 is an initial number which is the ECOFF file index. In the non-ECOFF
13930 case .file implies DWARF-2. */
13931
13932 static void
13933 s_mips_file (x)
13934 int x ATTRIBUTE_UNUSED;
13935 {
13936 static int first_file_directive = 0;
13937
13938 if (ECOFF_DEBUGGING)
13939 {
13940 get_number ();
13941 s_app_file (0);
13942 }
13943 else
13944 {
13945 char *filename;
13946
13947 filename = dwarf2_directive_file (0);
13948
13949 /* Versions of GCC up to 3.1 start files with a ".file"
13950 directive even for stabs output. Make sure that this
13951 ".file" is handled. Note that you need a version of GCC
13952 after 3.1 in order to support DWARF-2 on MIPS. */
13953 if (filename != NULL && ! first_file_directive)
13954 {
13955 (void) new_logical_line (filename, -1);
13956 s_app_file_string (filename);
13957 }
13958 first_file_directive = 1;
13959 }
13960 }
13961
13962 /* The .loc directive, implying DWARF-2. */
13963
13964 static void
13965 s_mips_loc (x)
13966 int x ATTRIBUTE_UNUSED;
13967 {
13968 if (!ECOFF_DEBUGGING)
13969 dwarf2_directive_loc (0);
13970 }
13971
13972 /* The .end directive. */
13973
13974 static void
13975 s_mips_end (x)
13976 int x ATTRIBUTE_UNUSED;
13977 {
13978 symbolS *p;
13979 int maybe_text;
13980
13981 /* Following functions need their own .frame and .cprestore directives. */
13982 mips_frame_reg_valid = 0;
13983 mips_cprestore_valid = 0;
13984
13985 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13986 {
13987 p = get_symbol ();
13988 demand_empty_rest_of_line ();
13989 }
13990 else
13991 p = NULL;
13992
13993 #ifdef BFD_ASSEMBLER
13994 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13995 maybe_text = 1;
13996 else
13997 maybe_text = 0;
13998 #else
13999 if (now_seg != data_section && now_seg != bss_section)
14000 maybe_text = 1;
14001 else
14002 maybe_text = 0;
14003 #endif
14004
14005 if (!maybe_text)
14006 as_warn (_(".end not in text section"));
14007
14008 if (!cur_proc_ptr)
14009 {
14010 as_warn (_(".end directive without a preceding .ent directive."));
14011 demand_empty_rest_of_line ();
14012 return;
14013 }
14014
14015 if (p != NULL)
14016 {
14017 assert (S_GET_NAME (p));
14018 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14019 as_warn (_(".end symbol does not match .ent symbol."));
14020
14021 if (debug_type == DEBUG_STABS)
14022 stabs_generate_asm_endfunc (S_GET_NAME (p),
14023 S_GET_NAME (p));
14024 }
14025 else
14026 as_warn (_(".end directive missing or unknown symbol"));
14027
14028 #ifdef OBJ_ELF
14029 /* Generate a .pdr section. */
14030 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14031 {
14032 segT saved_seg = now_seg;
14033 subsegT saved_subseg = now_subseg;
14034 valueT dot;
14035 expressionS exp;
14036 char *fragp;
14037
14038 dot = frag_now_fix ();
14039
14040 #ifdef md_flush_pending_output
14041 md_flush_pending_output ();
14042 #endif
14043
14044 assert (pdr_seg);
14045 subseg_set (pdr_seg, 0);
14046
14047 /* Write the symbol. */
14048 exp.X_op = O_symbol;
14049 exp.X_add_symbol = p;
14050 exp.X_add_number = 0;
14051 emit_expr (&exp, 4);
14052
14053 fragp = frag_more (7 * 4);
14054
14055 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
14056 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
14057 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14058 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14059 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14060 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14061 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14062
14063 subseg_set (saved_seg, saved_subseg);
14064 }
14065 #endif /* OBJ_ELF */
14066
14067 cur_proc_ptr = NULL;
14068 }
14069
14070 /* The .aent and .ent directives. */
14071
14072 static void
14073 s_mips_ent (aent)
14074 int aent;
14075 {
14076 symbolS *symbolP;
14077 int maybe_text;
14078
14079 symbolP = get_symbol ();
14080 if (*input_line_pointer == ',')
14081 ++input_line_pointer;
14082 SKIP_WHITESPACE ();
14083 if (ISDIGIT (*input_line_pointer)
14084 || *input_line_pointer == '-')
14085 get_number ();
14086
14087 #ifdef BFD_ASSEMBLER
14088 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14089 maybe_text = 1;
14090 else
14091 maybe_text = 0;
14092 #else
14093 if (now_seg != data_section && now_seg != bss_section)
14094 maybe_text = 1;
14095 else
14096 maybe_text = 0;
14097 #endif
14098
14099 if (!maybe_text)
14100 as_warn (_(".ent or .aent not in text section."));
14101
14102 if (!aent && cur_proc_ptr)
14103 as_warn (_("missing .end"));
14104
14105 if (!aent)
14106 {
14107 /* This function needs its own .frame and .cprestore directives. */
14108 mips_frame_reg_valid = 0;
14109 mips_cprestore_valid = 0;
14110
14111 cur_proc_ptr = &cur_proc;
14112 memset (cur_proc_ptr, '\0', sizeof (procS));
14113
14114 cur_proc_ptr->isym = symbolP;
14115
14116 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14117
14118 ++numprocs;
14119
14120 if (debug_type == DEBUG_STABS)
14121 stabs_generate_asm_func (S_GET_NAME (symbolP),
14122 S_GET_NAME (symbolP));
14123 }
14124
14125 demand_empty_rest_of_line ();
14126 }
14127
14128 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14129 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14130 s_mips_frame is used so that we can set the PDR information correctly.
14131 We can't use the ecoff routines because they make reference to the ecoff
14132 symbol table (in the mdebug section). */
14133
14134 static void
14135 s_mips_frame (ignore)
14136 int ignore ATTRIBUTE_UNUSED;
14137 {
14138 #ifdef OBJ_ELF
14139 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14140 {
14141 long val;
14142
14143 if (cur_proc_ptr == (procS *) NULL)
14144 {
14145 as_warn (_(".frame outside of .ent"));
14146 demand_empty_rest_of_line ();
14147 return;
14148 }
14149
14150 cur_proc_ptr->frame_reg = tc_get_register (1);
14151
14152 SKIP_WHITESPACE ();
14153 if (*input_line_pointer++ != ','
14154 || get_absolute_expression_and_terminator (&val) != ',')
14155 {
14156 as_warn (_("Bad .frame directive"));
14157 --input_line_pointer;
14158 demand_empty_rest_of_line ();
14159 return;
14160 }
14161
14162 cur_proc_ptr->frame_offset = val;
14163 cur_proc_ptr->pc_reg = tc_get_register (0);
14164
14165 demand_empty_rest_of_line ();
14166 }
14167 else
14168 #endif /* OBJ_ELF */
14169 s_ignore (ignore);
14170 }
14171
14172 /* The .fmask and .mask directives. If the mdebug section is present
14173 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14174 embedded targets, s_mips_mask is used so that we can set the PDR
14175 information correctly. We can't use the ecoff routines because they
14176 make reference to the ecoff symbol table (in the mdebug section). */
14177
14178 static void
14179 s_mips_mask (reg_type)
14180 char reg_type;
14181 {
14182 #ifdef OBJ_ELF
14183 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14184 {
14185 long mask, off;
14186
14187 if (cur_proc_ptr == (procS *) NULL)
14188 {
14189 as_warn (_(".mask/.fmask outside of .ent"));
14190 demand_empty_rest_of_line ();
14191 return;
14192 }
14193
14194 if (get_absolute_expression_and_terminator (&mask) != ',')
14195 {
14196 as_warn (_("Bad .mask/.fmask directive"));
14197 --input_line_pointer;
14198 demand_empty_rest_of_line ();
14199 return;
14200 }
14201
14202 off = get_absolute_expression ();
14203
14204 if (reg_type == 'F')
14205 {
14206 cur_proc_ptr->fpreg_mask = mask;
14207 cur_proc_ptr->fpreg_offset = off;
14208 }
14209 else
14210 {
14211 cur_proc_ptr->reg_mask = mask;
14212 cur_proc_ptr->reg_offset = off;
14213 }
14214
14215 demand_empty_rest_of_line ();
14216 }
14217 else
14218 #endif /* OBJ_ELF */
14219 s_ignore (reg_type);
14220 }
14221
14222 /* The .loc directive. */
14223
14224 #if 0
14225 static void
14226 s_loc (x)
14227 int x;
14228 {
14229 symbolS *symbolP;
14230 int lineno;
14231 int addroff;
14232
14233 assert (now_seg == text_section);
14234
14235 lineno = get_number ();
14236 addroff = frag_now_fix ();
14237
14238 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14239 S_SET_TYPE (symbolP, N_SLINE);
14240 S_SET_OTHER (symbolP, 0);
14241 S_SET_DESC (symbolP, lineno);
14242 symbolP->sy_segment = now_seg;
14243 }
14244 #endif
14245
14246 /* A table describing all the processors gas knows about. Names are
14247 matched in the order listed.
14248
14249 To ease comparison, please keep this table in the same order as
14250 gcc's mips_cpu_info_table[]. */
14251 static const struct mips_cpu_info mips_cpu_info_table[] =
14252 {
14253 /* Entries for generic ISAs */
14254 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14255 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14256 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14257 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14258 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14259 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
14260 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
14261
14262 /* MIPS I */
14263 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14264 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14265 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14266
14267 /* MIPS II */
14268 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14269
14270 /* MIPS III */
14271 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14272 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14273 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14274 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
14275 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14276 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14277 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
14278 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14279 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14280 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14281 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14282 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14283
14284 /* MIPS IV */
14285 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14286 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14287 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14288 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
14289 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14290 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
14291 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14292 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14293 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14294 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14295 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14296 { "r7000", 0, ISA_MIPS4, CPU_R5000 },
14297
14298 /* MIPS 32 */
14299 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
14300 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14301 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
14302
14303 /* MIPS 64 */
14304 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14305 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
14306
14307 /* Broadcom SB-1 CPU core */
14308 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
14309
14310 /* End marker */
14311 { NULL, 0, 0, 0 }
14312 };
14313
14314
14315 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14316 with a final "000" replaced by "k". Ignore case.
14317
14318 Note: this function is shared between GCC and GAS. */
14319
14320 static boolean
14321 mips_strict_matching_cpu_name_p (canonical, given)
14322 const char *canonical, *given;
14323 {
14324 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14325 given++, canonical++;
14326
14327 return ((*given == 0 && *canonical == 0)
14328 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14329 }
14330
14331
14332 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14333 CPU name. We've traditionally allowed a lot of variation here.
14334
14335 Note: this function is shared between GCC and GAS. */
14336
14337 static boolean
14338 mips_matching_cpu_name_p (canonical, given)
14339 const char *canonical, *given;
14340 {
14341 /* First see if the name matches exactly, or with a final "000"
14342 turned into "k". */
14343 if (mips_strict_matching_cpu_name_p (canonical, given))
14344 return true;
14345
14346 /* If not, try comparing based on numerical designation alone.
14347 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14348 if (TOLOWER (*given) == 'r')
14349 given++;
14350 if (!ISDIGIT (*given))
14351 return false;
14352
14353 /* Skip over some well-known prefixes in the canonical name,
14354 hoping to find a number there too. */
14355 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14356 canonical += 2;
14357 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14358 canonical += 2;
14359 else if (TOLOWER (canonical[0]) == 'r')
14360 canonical += 1;
14361
14362 return mips_strict_matching_cpu_name_p (canonical, given);
14363 }
14364
14365
14366 /* Parse an option that takes the name of a processor as its argument.
14367 OPTION is the name of the option and CPU_STRING is the argument.
14368 Return the corresponding processor enumeration if the CPU_STRING is
14369 recognized, otherwise report an error and return null.
14370
14371 A similar function exists in GCC. */
14372
14373 static const struct mips_cpu_info *
14374 mips_parse_cpu (option, cpu_string)
14375 const char *option, *cpu_string;
14376 {
14377 const struct mips_cpu_info *p;
14378
14379 /* 'from-abi' selects the most compatible architecture for the given
14380 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14381 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14382 version. Look first at the -mgp options, if given, otherwise base
14383 the choice on MIPS_DEFAULT_64BIT.
14384
14385 Treat NO_ABI like the EABIs. One reason to do this is that the
14386 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14387 architecture. This code picks MIPS I for 'mips' and MIPS III for
14388 'mips64', just as we did in the days before 'from-abi'. */
14389 if (strcasecmp (cpu_string, "from-abi") == 0)
14390 {
14391 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14392 return mips_cpu_info_from_isa (ISA_MIPS1);
14393
14394 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14395 return mips_cpu_info_from_isa (ISA_MIPS3);
14396
14397 if (file_mips_gp32 >= 0)
14398 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14399
14400 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14401 ? ISA_MIPS3
14402 : ISA_MIPS1);
14403 }
14404
14405 /* 'default' has traditionally been a no-op. Probably not very useful. */
14406 if (strcasecmp (cpu_string, "default") == 0)
14407 return 0;
14408
14409 for (p = mips_cpu_info_table; p->name != 0; p++)
14410 if (mips_matching_cpu_name_p (p->name, cpu_string))
14411 return p;
14412
14413 as_bad ("Bad value (%s) for %s", cpu_string, option);
14414 return 0;
14415 }
14416
14417 /* Return the canonical processor information for ISA (a member of the
14418 ISA_MIPS* enumeration). */
14419
14420 static const struct mips_cpu_info *
14421 mips_cpu_info_from_isa (isa)
14422 int isa;
14423 {
14424 int i;
14425
14426 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14427 if (mips_cpu_info_table[i].is_isa
14428 && isa == mips_cpu_info_table[i].isa)
14429 return (&mips_cpu_info_table[i]);
14430
14431 return NULL;
14432 }
14433 \f
14434 static void
14435 show (stream, string, col_p, first_p)
14436 FILE *stream;
14437 const char *string;
14438 int *col_p;
14439 int *first_p;
14440 {
14441 if (*first_p)
14442 {
14443 fprintf (stream, "%24s", "");
14444 *col_p = 24;
14445 }
14446 else
14447 {
14448 fprintf (stream, ", ");
14449 *col_p += 2;
14450 }
14451
14452 if (*col_p + strlen (string) > 72)
14453 {
14454 fprintf (stream, "\n%24s", "");
14455 *col_p = 24;
14456 }
14457
14458 fprintf (stream, "%s", string);
14459 *col_p += strlen (string);
14460
14461 *first_p = 0;
14462 }
14463
14464 void
14465 md_show_usage (stream)
14466 FILE *stream;
14467 {
14468 int column, first;
14469 size_t i;
14470
14471 fprintf (stream, _("\
14472 MIPS options:\n\
14473 -membedded-pic generate embedded position independent code\n\
14474 -EB generate big endian output\n\
14475 -EL generate little endian output\n\
14476 -g, -g2 do not remove unneeded NOPs or swap branches\n\
14477 -G NUM allow referencing objects up to NUM bytes\n\
14478 implicitly with the gp register [default 8]\n"));
14479 fprintf (stream, _("\
14480 -mips1 generate MIPS ISA I instructions\n\
14481 -mips2 generate MIPS ISA II instructions\n\
14482 -mips3 generate MIPS ISA III instructions\n\
14483 -mips4 generate MIPS ISA IV instructions\n\
14484 -mips5 generate MIPS ISA V instructions\n\
14485 -mips32 generate MIPS32 ISA instructions\n\
14486 -mips64 generate MIPS64 ISA instructions\n\
14487 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14488
14489 first = 1;
14490
14491 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14492 show (stream, mips_cpu_info_table[i].name, &column, &first);
14493 show (stream, "from-abi", &column, &first);
14494 fputc ('\n', stream);
14495
14496 fprintf (stream, _("\
14497 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14498 -no-mCPU don't generate code specific to CPU.\n\
14499 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14500
14501 first = 1;
14502
14503 show (stream, "3900", &column, &first);
14504 show (stream, "4010", &column, &first);
14505 show (stream, "4100", &column, &first);
14506 show (stream, "4650", &column, &first);
14507 fputc ('\n', stream);
14508
14509 fprintf (stream, _("\
14510 -mips16 generate mips16 instructions\n\
14511 -no-mips16 do not generate mips16 instructions\n"));
14512 fprintf (stream, _("\
14513 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14514 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14515 -O0 remove unneeded NOPs, do not swap branches\n\
14516 -O remove unneeded NOPs and swap branches\n\
14517 -n warn about NOPs generated from macros\n\
14518 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14519 --trap, --no-break trap exception on div by 0 and mult overflow\n\
14520 --break, --no-trap break exception on div by 0 and mult overflow\n"));
14521 #ifdef OBJ_ELF
14522 fprintf (stream, _("\
14523 -KPIC, -call_shared generate SVR4 position independent code\n\
14524 -non_shared do not generate position independent code\n\
14525 -xgot assume a 32 bit GOT\n\
14526 -mabi=ABI create ABI conformant object file for:\n"));
14527
14528 first = 1;
14529
14530 show (stream, "32", &column, &first);
14531 show (stream, "o64", &column, &first);
14532 show (stream, "n32", &column, &first);
14533 show (stream, "64", &column, &first);
14534 show (stream, "eabi", &column, &first);
14535
14536 fputc ('\n', stream);
14537
14538 fprintf (stream, _("\
14539 -32 create o32 ABI object file (default)\n\
14540 -n32 create n32 ABI object file\n\
14541 -64 create 64 ABI object file\n"));
14542 #endif
14543 }
This page took 0.333551 seconds and 4 git commands to generate.