* elf32-mips.c (elf_mips_howto_table_rel): Change definition of
[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, 2003
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 /* Return true if ISA supports 64-bit right rotate (dror et al.)
264 instructions. */
265 #define ISA_HAS_DROR(ISA) ( \
266 0 \
267 )
268
269 /* Return true if ISA supports 32-bit right rotate (ror et al.)
270 instructions. */
271 #define ISA_HAS_ROR(ISA) ( \
272 (ISA) == ISA_MIPS32R2 \
273 )
274
275 #define HAVE_32BIT_GPRS \
276 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
277
278 #define HAVE_32BIT_FPRS \
279 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
280
281 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
282 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
283
284 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
285
286 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
287
288 /* We can only have 64bit addresses if the object file format
289 supports it. */
290 #define HAVE_32BIT_ADDRESSES \
291 (HAVE_32BIT_GPRS \
292 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
293 || ! HAVE_64BIT_OBJECTS) \
294 && mips_pic != EMBEDDED_PIC))
295
296 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
297
298 /* Return true if the given CPU supports the MIPS16 ASE. */
299 #define CPU_HAS_MIPS16(cpu) \
300 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
301 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
302
303 /* Return true if the given CPU supports the MIPS3D ASE. */
304 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
305 )
306
307 /* Return true if the given CPU supports the MDMX ASE. */
308 #define CPU_HAS_MDMX(cpu) (FALSE \
309 )
310
311 /* True if CPU has a dror instruction. */
312 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
313
314 /* True if CPU has a ror instruction. */
315 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
316
317 /* Whether the processor uses hardware interlocks to protect
318 reads from the HI and LO registers, and thus does not
319 require nops to be inserted. */
320
321 #define hilo_interlocks (mips_arch == CPU_R4010 \
322 || mips_arch == CPU_VR5500 \
323 || mips_arch == CPU_SB1 \
324 )
325
326 /* Whether the processor uses hardware interlocks to protect reads
327 from the GPRs, and thus does not require nops to be inserted. */
328 #define gpr_interlocks \
329 (mips_opts.isa != ISA_MIPS1 \
330 || mips_arch == CPU_VR5400 \
331 || mips_arch == CPU_VR5500 \
332 || mips_arch == CPU_R3900)
333
334 /* As with other "interlocks" this is used by hardware that has FP
335 (co-processor) interlocks. */
336 /* Itbl support may require additional care here. */
337 #define cop_interlocks (mips_arch == CPU_R4300 \
338 || mips_arch == CPU_VR5400 \
339 || mips_arch == CPU_VR5500 \
340 || mips_arch == CPU_SB1 \
341 )
342
343 /* Is this a mfhi or mflo instruction? */
344 #define MF_HILO_INSN(PINFO) \
345 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
346
347 /* MIPS PIC level. */
348
349 enum mips_pic_level mips_pic;
350
351 /* Warn about all NOPS that the assembler generates. */
352 static int warn_nops = 0;
353
354 /* 1 if we should generate 32 bit offsets from the $gp register in
355 SVR4_PIC mode. Currently has no meaning in other modes. */
356 static int mips_big_got = 0;
357
358 /* 1 if trap instructions should used for overflow rather than break
359 instructions. */
360 static int mips_trap = 0;
361
362 /* 1 if double width floating point constants should not be constructed
363 by assembling two single width halves into two single width floating
364 point registers which just happen to alias the double width destination
365 register. On some architectures this aliasing can be disabled by a bit
366 in the status register, and the setting of this bit cannot be determined
367 automatically at assemble time. */
368 static int mips_disable_float_construction;
369
370 /* Non-zero if any .set noreorder directives were used. */
371
372 static int mips_any_noreorder;
373
374 /* Non-zero if nops should be inserted when the register referenced in
375 an mfhi/mflo instruction is read in the next two instructions. */
376 static int mips_7000_hilo_fix;
377
378 /* The size of the small data section. */
379 static unsigned int g_switch_value = 8;
380 /* Whether the -G option was used. */
381 static int g_switch_seen = 0;
382
383 #define N_RMASK 0xc4
384 #define N_VFP 0xd4
385
386 /* If we can determine in advance that GP optimization won't be
387 possible, we can skip the relaxation stuff that tries to produce
388 GP-relative references. This makes delay slot optimization work
389 better.
390
391 This function can only provide a guess, but it seems to work for
392 gcc output. It needs to guess right for gcc, otherwise gcc
393 will put what it thinks is a GP-relative instruction in a branch
394 delay slot.
395
396 I don't know if a fix is needed for the SVR4_PIC mode. I've only
397 fixed it for the non-PIC mode. KR 95/04/07 */
398 static int nopic_need_relax PARAMS ((symbolS *, int));
399
400 /* handle of the OPCODE hash table */
401 static struct hash_control *op_hash = NULL;
402
403 /* The opcode hash table we use for the mips16. */
404 static struct hash_control *mips16_op_hash = NULL;
405
406 /* This array holds the chars that always start a comment. If the
407 pre-processor is disabled, these aren't very useful */
408 const char comment_chars[] = "#";
409
410 /* This array holds the chars that only start a comment at the beginning of
411 a line. If the line seems to have the form '# 123 filename'
412 .line and .file directives will appear in the pre-processed output */
413 /* Note that input_file.c hand checks for '#' at the beginning of the
414 first line of the input file. This is because the compiler outputs
415 #NO_APP at the beginning of its output. */
416 /* Also note that C style comments are always supported. */
417 const char line_comment_chars[] = "#";
418
419 /* This array holds machine specific line separator characters. */
420 const char line_separator_chars[] = ";";
421
422 /* Chars that can be used to separate mant from exp in floating point nums */
423 const char EXP_CHARS[] = "eE";
424
425 /* Chars that mean this number is a floating point constant */
426 /* As in 0f12.456 */
427 /* or 0d1.2345e12 */
428 const char FLT_CHARS[] = "rRsSfFdDxXpP";
429
430 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
431 changed in read.c . Ideally it shouldn't have to know about it at all,
432 but nothing is ideal around here.
433 */
434
435 static char *insn_error;
436
437 static int auto_align = 1;
438
439 /* When outputting SVR4 PIC code, the assembler needs to know the
440 offset in the stack frame from which to restore the $gp register.
441 This is set by the .cprestore pseudo-op, and saved in this
442 variable. */
443 static offsetT mips_cprestore_offset = -1;
444
445 /* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
446 more optimizations, it can use a register value instead of a memory-saved
447 offset and even an other register than $gp as global pointer. */
448 static offsetT mips_cpreturn_offset = -1;
449 static int mips_cpreturn_register = -1;
450 static int mips_gp_register = GP;
451 static int mips_gprel_offset = 0;
452
453 /* Whether mips_cprestore_offset has been set in the current function
454 (or whether it has already been warned about, if not). */
455 static int mips_cprestore_valid = 0;
456
457 /* This is the register which holds the stack frame, as set by the
458 .frame pseudo-op. This is needed to implement .cprestore. */
459 static int mips_frame_reg = SP;
460
461 /* Whether mips_frame_reg has been set in the current function
462 (or whether it has already been warned about, if not). */
463 static int mips_frame_reg_valid = 0;
464
465 /* To output NOP instructions correctly, we need to keep information
466 about the previous two instructions. */
467
468 /* Whether we are optimizing. The default value of 2 means to remove
469 unneeded NOPs and swap branch instructions when possible. A value
470 of 1 means to not swap branches. A value of 0 means to always
471 insert NOPs. */
472 static int mips_optimize = 2;
473
474 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
475 equivalent to seeing no -g option at all. */
476 static int mips_debug = 0;
477
478 /* The previous instruction. */
479 static struct mips_cl_insn prev_insn;
480
481 /* The instruction before prev_insn. */
482 static struct mips_cl_insn prev_prev_insn;
483
484 /* If we don't want information for prev_insn or prev_prev_insn, we
485 point the insn_mo field at this dummy integer. */
486 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
487
488 /* Non-zero if prev_insn is valid. */
489 static int prev_insn_valid;
490
491 /* The frag for the previous instruction. */
492 static struct frag *prev_insn_frag;
493
494 /* The offset into prev_insn_frag for the previous instruction. */
495 static long prev_insn_where;
496
497 /* The reloc type for the previous instruction, if any. */
498 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
499
500 /* The reloc for the previous instruction, if any. */
501 static fixS *prev_insn_fixp[3];
502
503 /* Non-zero if the previous instruction was in a delay slot. */
504 static int prev_insn_is_delay_slot;
505
506 /* Non-zero if the previous instruction was in a .set noreorder. */
507 static int prev_insn_unreordered;
508
509 /* Non-zero if the previous instruction uses an extend opcode (if
510 mips16). */
511 static int prev_insn_extended;
512
513 /* Non-zero if the previous previous instruction was in a .set
514 noreorder. */
515 static int prev_prev_insn_unreordered;
516
517 /* If this is set, it points to a frag holding nop instructions which
518 were inserted before the start of a noreorder section. If those
519 nops turn out to be unnecessary, the size of the frag can be
520 decreased. */
521 static fragS *prev_nop_frag;
522
523 /* The number of nop instructions we created in prev_nop_frag. */
524 static int prev_nop_frag_holds;
525
526 /* The number of nop instructions that we know we need in
527 prev_nop_frag. */
528 static int prev_nop_frag_required;
529
530 /* The number of instructions we've seen since prev_nop_frag. */
531 static int prev_nop_frag_since;
532
533 /* For ECOFF and ELF, relocations against symbols are done in two
534 parts, with a HI relocation and a LO relocation. Each relocation
535 has only 16 bits of space to store an addend. This means that in
536 order for the linker to handle carries correctly, it must be able
537 to locate both the HI and the LO relocation. This means that the
538 relocations must appear in order in the relocation table.
539
540 In order to implement this, we keep track of each unmatched HI
541 relocation. We then sort them so that they immediately precede the
542 corresponding LO relocation. */
543
544 struct mips_hi_fixup
545 {
546 /* Next HI fixup. */
547 struct mips_hi_fixup *next;
548 /* This fixup. */
549 fixS *fixp;
550 /* The section this fixup is in. */
551 segT seg;
552 };
553
554 /* The list of unmatched HI relocs. */
555
556 static struct mips_hi_fixup *mips_hi_fixup_list;
557
558 /* The frag containing the last explicit relocation operator.
559 Null if explicit relocations have not been used. */
560
561 static fragS *prev_reloc_op_frag;
562
563 /* Map normal MIPS register numbers to mips16 register numbers. */
564
565 #define X ILLEGAL_REG
566 static const int mips32_to_16_reg_map[] =
567 {
568 X, X, 2, 3, 4, 5, 6, 7,
569 X, X, X, X, X, X, X, X,
570 0, 1, X, X, X, X, X, X,
571 X, X, X, X, X, X, X, X
572 };
573 #undef X
574
575 /* Map mips16 register numbers to normal MIPS register numbers. */
576
577 static const unsigned int mips16_to_32_reg_map[] =
578 {
579 16, 17, 2, 3, 4, 5, 6, 7
580 };
581
582 static int mips_fix_4122_bugs;
583
584 /* We don't relax branches by default, since this causes us to expand
585 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
586 fail to compute the offset before expanding the macro to the most
587 efficient expansion. */
588
589 static int mips_relax_branch;
590 \f
591 /* Since the MIPS does not have multiple forms of PC relative
592 instructions, we do not have to do relaxing as is done on other
593 platforms. However, we do have to handle GP relative addressing
594 correctly, which turns out to be a similar problem.
595
596 Every macro that refers to a symbol can occur in (at least) two
597 forms, one with GP relative addressing and one without. For
598 example, loading a global variable into a register generally uses
599 a macro instruction like this:
600 lw $4,i
601 If i can be addressed off the GP register (this is true if it is in
602 the .sbss or .sdata section, or if it is known to be smaller than
603 the -G argument) this will generate the following instruction:
604 lw $4,i($gp)
605 This instruction will use a GPREL reloc. If i can not be addressed
606 off the GP register, the following instruction sequence will be used:
607 lui $at,i
608 lw $4,i($at)
609 In this case the first instruction will have a HI16 reloc, and the
610 second reloc will have a LO16 reloc. Both relocs will be against
611 the symbol i.
612
613 The issue here is that we may not know whether i is GP addressable
614 until after we see the instruction that uses it. Therefore, we
615 want to be able to choose the final instruction sequence only at
616 the end of the assembly. This is similar to the way other
617 platforms choose the size of a PC relative instruction only at the
618 end of assembly.
619
620 When generating position independent code we do not use GP
621 addressing in quite the same way, but the issue still arises as
622 external symbols and local symbols must be handled differently.
623
624 We handle these issues by actually generating both possible
625 instruction sequences. The longer one is put in a frag_var with
626 type rs_machine_dependent. We encode what to do with the frag in
627 the subtype field. We encode (1) the number of existing bytes to
628 replace, (2) the number of new bytes to use, (3) the offset from
629 the start of the existing bytes to the first reloc we must generate
630 (that is, the offset is applied from the start of the existing
631 bytes after they are replaced by the new bytes, if any), (4) the
632 offset from the start of the existing bytes to the second reloc,
633 (5) whether a third reloc is needed (the third reloc is always four
634 bytes after the second reloc), and (6) whether to warn if this
635 variant is used (this is sometimes needed if .set nomacro or .set
636 noat is in effect). All these numbers are reasonably small.
637
638 Generating two instruction sequences must be handled carefully to
639 ensure that delay slots are handled correctly. Fortunately, there
640 are a limited number of cases. When the second instruction
641 sequence is generated, append_insn is directed to maintain the
642 existing delay slot information, so it continues to apply to any
643 code after the second instruction sequence. This means that the
644 second instruction sequence must not impose any requirements not
645 required by the first instruction sequence.
646
647 These variant frags are then handled in functions called by the
648 machine independent code. md_estimate_size_before_relax returns
649 the final size of the frag. md_convert_frag sets up the final form
650 of the frag. tc_gen_reloc adjust the first reloc and adds a second
651 one if needed. */
652 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
653 ((relax_substateT) \
654 (((old) << 23) \
655 | ((new) << 16) \
656 | (((reloc1) + 64) << 9) \
657 | (((reloc2) + 64) << 2) \
658 | ((reloc3) ? (1 << 1) : 0) \
659 | ((warn) ? 1 : 0)))
660 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
661 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
662 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
663 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
664 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
665 #define RELAX_WARN(i) ((i) & 1)
666
667 /* Branch without likely bit. If label is out of range, we turn:
668
669 beq reg1, reg2, label
670 delay slot
671
672 into
673
674 bne reg1, reg2, 0f
675 nop
676 j label
677 0: delay slot
678
679 with the following opcode replacements:
680
681 beq <-> bne
682 blez <-> bgtz
683 bltz <-> bgez
684 bc1f <-> bc1t
685
686 bltzal <-> bgezal (with jal label instead of j label)
687
688 Even though keeping the delay slot instruction in the delay slot of
689 the branch would be more efficient, it would be very tricky to do
690 correctly, because we'd have to introduce a variable frag *after*
691 the delay slot instruction, and expand that instead. Let's do it
692 the easy way for now, even if the branch-not-taken case now costs
693 one additional instruction. Out-of-range branches are not supposed
694 to be common, anyway.
695
696 Branch likely. If label is out of range, we turn:
697
698 beql reg1, reg2, label
699 delay slot (annulled if branch not taken)
700
701 into
702
703 beql reg1, reg2, 1f
704 nop
705 beql $0, $0, 2f
706 nop
707 1: j[al] label
708 delay slot (executed only if branch taken)
709 2:
710
711 It would be possible to generate a shorter sequence by losing the
712 likely bit, generating something like:
713
714 bne reg1, reg2, 0f
715 nop
716 j[al] label
717 delay slot (executed only if branch taken)
718 0:
719
720 beql -> bne
721 bnel -> beq
722 blezl -> bgtz
723 bgtzl -> blez
724 bltzl -> bgez
725 bgezl -> bltz
726 bc1fl -> bc1t
727 bc1tl -> bc1f
728
729 bltzall -> bgezal (with jal label instead of j label)
730 bgezall -> bltzal (ditto)
731
732
733 but it's not clear that it would actually improve performance. */
734 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
735 ((relax_substateT) \
736 (0xc0000000 \
737 | ((toofar) ? 1 : 0) \
738 | ((link) ? 2 : 0) \
739 | ((likely) ? 4 : 0) \
740 | ((uncond) ? 8 : 0)))
741 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
742 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
743 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
744 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
745 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
746
747 /* For mips16 code, we use an entirely different form of relaxation.
748 mips16 supports two versions of most instructions which take
749 immediate values: a small one which takes some small value, and a
750 larger one which takes a 16 bit value. Since branches also follow
751 this pattern, relaxing these values is required.
752
753 We can assemble both mips16 and normal MIPS code in a single
754 object. Therefore, we need to support this type of relaxation at
755 the same time that we support the relaxation described above. We
756 use the high bit of the subtype field to distinguish these cases.
757
758 The information we store for this type of relaxation is the
759 argument code found in the opcode file for this relocation, whether
760 the user explicitly requested a small or extended form, and whether
761 the relocation is in a jump or jal delay slot. That tells us the
762 size of the value, and how it should be stored. We also store
763 whether the fragment is considered to be extended or not. We also
764 store whether this is known to be a branch to a different section,
765 whether we have tried to relax this frag yet, and whether we have
766 ever extended a PC relative fragment because of a shift count. */
767 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
768 (0x80000000 \
769 | ((type) & 0xff) \
770 | ((small) ? 0x100 : 0) \
771 | ((ext) ? 0x200 : 0) \
772 | ((dslot) ? 0x400 : 0) \
773 | ((jal_dslot) ? 0x800 : 0))
774 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
775 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
776 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
777 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
778 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
779 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
780 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
781 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
782 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
783 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
784 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
785 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
786
787 /* Is the given value a sign-extended 32-bit value? */
788 #define IS_SEXT_32BIT_NUM(x) \
789 (((x) &~ (offsetT) 0x7fffffff) == 0 \
790 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
791
792 /* Is the given value a sign-extended 16-bit value? */
793 #define IS_SEXT_16BIT_NUM(x) \
794 (((x) &~ (offsetT) 0x7fff) == 0 \
795 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
796
797 \f
798 /* Prototypes for static functions. */
799
800 #ifdef __STDC__
801 #define internalError() \
802 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
803 #else
804 #define internalError() as_fatal (_("MIPS internal Error"));
805 #endif
806
807 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
808
809 static inline bfd_boolean reloc_needs_lo_p
810 PARAMS ((bfd_reloc_code_real_type));
811 static inline bfd_boolean fixup_has_matching_lo_p
812 PARAMS ((fixS *));
813 static int insn_uses_reg
814 PARAMS ((struct mips_cl_insn *ip, unsigned int reg,
815 enum mips_regclass class));
816 static int reg_needs_delay
817 PARAMS ((unsigned int));
818 static void mips16_mark_labels
819 PARAMS ((void));
820 static void append_insn
821 PARAMS ((char *place, struct mips_cl_insn * ip, expressionS * p,
822 bfd_reloc_code_real_type *r));
823 static void mips_no_prev_insn
824 PARAMS ((int));
825 static void mips_emit_delays
826 PARAMS ((bfd_boolean));
827 #ifdef USE_STDARG
828 static void macro_build
829 PARAMS ((char *place, int *counter, expressionS * ep, const char *name,
830 const char *fmt, ...));
831 #else
832 static void macro_build ();
833 #endif
834 static void mips16_macro_build
835 PARAMS ((char *, int *, expressionS *, const char *, const char *, va_list));
836 static void macro_build_jalr
837 PARAMS ((int, expressionS *));
838 static void macro_build_lui
839 PARAMS ((char *place, int *counter, expressionS * ep, int regnum));
840 static void macro_build_ldst_constoffset
841 PARAMS ((char *place, int *counter, expressionS * ep, const char *op,
842 int valreg, int breg));
843 static void set_at
844 PARAMS ((int *counter, int reg, int unsignedp));
845 static void check_absolute_expr
846 PARAMS ((struct mips_cl_insn * ip, expressionS *));
847 static void load_register
848 PARAMS ((int *, int, expressionS *, int));
849 static void load_address
850 PARAMS ((int *, int, expressionS *, int *));
851 static void move_register
852 PARAMS ((int *, int, int));
853 static void macro
854 PARAMS ((struct mips_cl_insn * ip));
855 static void mips16_macro
856 PARAMS ((struct mips_cl_insn * ip));
857 #ifdef LOSING_COMPILER
858 static void macro2
859 PARAMS ((struct mips_cl_insn * ip));
860 #endif
861 static void mips_ip
862 PARAMS ((char *str, struct mips_cl_insn * ip));
863 static void mips16_ip
864 PARAMS ((char *str, struct mips_cl_insn * ip));
865 static void mips16_immed
866 PARAMS ((char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean,
867 bfd_boolean, unsigned long *, bfd_boolean *, unsigned short *));
868 static bfd_boolean parse_relocation
869 PARAMS ((char **, bfd_reloc_code_real_type *));
870 static size_t my_getSmallExpression
871 PARAMS ((expressionS *, bfd_reloc_code_real_type *, char *));
872 static void my_getExpression
873 PARAMS ((expressionS *, char *));
874 #ifdef OBJ_ELF
875 static int support_64bit_objects
876 PARAMS((void));
877 #endif
878 static void mips_set_option_string
879 PARAMS ((const char **, const char *));
880 static symbolS *get_symbol
881 PARAMS ((void));
882 static void mips_align
883 PARAMS ((int to, int fill, symbolS *label));
884 static void s_align
885 PARAMS ((int));
886 static void s_change_sec
887 PARAMS ((int));
888 static void s_change_section
889 PARAMS ((int));
890 static void s_cons
891 PARAMS ((int));
892 static void s_float_cons
893 PARAMS ((int));
894 static void s_mips_globl
895 PARAMS ((int));
896 static void s_option
897 PARAMS ((int));
898 static void s_mipsset
899 PARAMS ((int));
900 static void s_abicalls
901 PARAMS ((int));
902 static void s_cpload
903 PARAMS ((int));
904 static void s_cpsetup
905 PARAMS ((int));
906 static void s_cplocal
907 PARAMS ((int));
908 static void s_cprestore
909 PARAMS ((int));
910 static void s_cpreturn
911 PARAMS ((int));
912 static void s_gpvalue
913 PARAMS ((int));
914 static void s_gpword
915 PARAMS ((int));
916 static void s_gpdword
917 PARAMS ((int));
918 static void s_cpadd
919 PARAMS ((int));
920 static void s_insn
921 PARAMS ((int));
922 static void md_obj_begin
923 PARAMS ((void));
924 static void md_obj_end
925 PARAMS ((void));
926 static long get_number
927 PARAMS ((void));
928 static void s_mips_ent
929 PARAMS ((int));
930 static void s_mips_end
931 PARAMS ((int));
932 static void s_mips_frame
933 PARAMS ((int));
934 static void s_mips_mask
935 PARAMS ((int));
936 static void s_mips_stab
937 PARAMS ((int));
938 static void s_mips_weakext
939 PARAMS ((int));
940 static void s_mips_file
941 PARAMS ((int));
942 static void s_mips_loc
943 PARAMS ((int));
944 static bfd_boolean pic_need_relax
945 PARAMS ((symbolS *, asection *));
946 static int mips16_extended_frag
947 PARAMS ((fragS *, asection *, long));
948 static int relaxed_branch_length (fragS *, asection *, int);
949 static int validate_mips_insn
950 PARAMS ((const struct mips_opcode *));
951 static void show
952 PARAMS ((FILE *, const char *, int *, int *));
953 #ifdef OBJ_ELF
954 static int mips_need_elf_addend_fixup
955 PARAMS ((fixS *));
956 #endif
957
958 /* Table and functions used to map between CPU/ISA names, and
959 ISA levels, and CPU numbers. */
960
961 struct mips_cpu_info
962 {
963 const char *name; /* CPU or ISA name. */
964 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
965 int isa; /* ISA level. */
966 int cpu; /* CPU number (default CPU if ISA). */
967 };
968
969 static void mips_set_architecture
970 PARAMS ((const struct mips_cpu_info *));
971 static void mips_set_tune
972 PARAMS ((const struct mips_cpu_info *));
973 static bfd_boolean mips_strict_matching_cpu_name_p
974 PARAMS ((const char *, const char *));
975 static bfd_boolean mips_matching_cpu_name_p
976 PARAMS ((const char *, const char *));
977 static const struct mips_cpu_info *mips_parse_cpu
978 PARAMS ((const char *, const char *));
979 static const struct mips_cpu_info *mips_cpu_info_from_isa
980 PARAMS ((int));
981 \f
982 /* Pseudo-op table.
983
984 The following pseudo-ops from the Kane and Heinrich MIPS book
985 should be defined here, but are currently unsupported: .alias,
986 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
987
988 The following pseudo-ops from the Kane and Heinrich MIPS book are
989 specific to the type of debugging information being generated, and
990 should be defined by the object format: .aent, .begin, .bend,
991 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
992 .vreg.
993
994 The following pseudo-ops from the Kane and Heinrich MIPS book are
995 not MIPS CPU specific, but are also not specific to the object file
996 format. This file is probably the best place to define them, but
997 they are not currently supported: .asm0, .endr, .lab, .repeat,
998 .struct. */
999
1000 static const pseudo_typeS mips_pseudo_table[] =
1001 {
1002 /* MIPS specific pseudo-ops. */
1003 {"option", s_option, 0},
1004 {"set", s_mipsset, 0},
1005 {"rdata", s_change_sec, 'r'},
1006 {"sdata", s_change_sec, 's'},
1007 {"livereg", s_ignore, 0},
1008 {"abicalls", s_abicalls, 0},
1009 {"cpload", s_cpload, 0},
1010 {"cpsetup", s_cpsetup, 0},
1011 {"cplocal", s_cplocal, 0},
1012 {"cprestore", s_cprestore, 0},
1013 {"cpreturn", s_cpreturn, 0},
1014 {"gpvalue", s_gpvalue, 0},
1015 {"gpword", s_gpword, 0},
1016 {"gpdword", s_gpdword, 0},
1017 {"cpadd", s_cpadd, 0},
1018 {"insn", s_insn, 0},
1019
1020 /* Relatively generic pseudo-ops that happen to be used on MIPS
1021 chips. */
1022 {"asciiz", stringer, 1},
1023 {"bss", s_change_sec, 'b'},
1024 {"err", s_err, 0},
1025 {"half", s_cons, 1},
1026 {"dword", s_cons, 3},
1027 {"weakext", s_mips_weakext, 0},
1028
1029 /* These pseudo-ops are defined in read.c, but must be overridden
1030 here for one reason or another. */
1031 {"align", s_align, 0},
1032 {"byte", s_cons, 0},
1033 {"data", s_change_sec, 'd'},
1034 {"double", s_float_cons, 'd'},
1035 {"float", s_float_cons, 'f'},
1036 {"globl", s_mips_globl, 0},
1037 {"global", s_mips_globl, 0},
1038 {"hword", s_cons, 1},
1039 {"int", s_cons, 2},
1040 {"long", s_cons, 2},
1041 {"octa", s_cons, 4},
1042 {"quad", s_cons, 3},
1043 {"section", s_change_section, 0},
1044 {"short", s_cons, 1},
1045 {"single", s_float_cons, 'f'},
1046 {"stabn", s_mips_stab, 'n'},
1047 {"text", s_change_sec, 't'},
1048 {"word", s_cons, 2},
1049
1050 { "extern", ecoff_directive_extern, 0},
1051
1052 { NULL, NULL, 0 },
1053 };
1054
1055 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1056 {
1057 /* These pseudo-ops should be defined by the object file format.
1058 However, a.out doesn't support them, so we have versions here. */
1059 {"aent", s_mips_ent, 1},
1060 {"bgnb", s_ignore, 0},
1061 {"end", s_mips_end, 0},
1062 {"endb", s_ignore, 0},
1063 {"ent", s_mips_ent, 0},
1064 {"file", s_mips_file, 0},
1065 {"fmask", s_mips_mask, 'F'},
1066 {"frame", s_mips_frame, 0},
1067 {"loc", s_mips_loc, 0},
1068 {"mask", s_mips_mask, 'R'},
1069 {"verstamp", s_ignore, 0},
1070 { NULL, NULL, 0 },
1071 };
1072
1073 extern void pop_insert PARAMS ((const pseudo_typeS *));
1074
1075 void
1076 mips_pop_insert ()
1077 {
1078 pop_insert (mips_pseudo_table);
1079 if (! ECOFF_DEBUGGING)
1080 pop_insert (mips_nonecoff_pseudo_table);
1081 }
1082 \f
1083 /* Symbols labelling the current insn. */
1084
1085 struct insn_label_list
1086 {
1087 struct insn_label_list *next;
1088 symbolS *label;
1089 };
1090
1091 static struct insn_label_list *insn_labels;
1092 static struct insn_label_list *free_insn_labels;
1093
1094 static void mips_clear_insn_labels PARAMS ((void));
1095
1096 static inline void
1097 mips_clear_insn_labels ()
1098 {
1099 register struct insn_label_list **pl;
1100
1101 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1102 ;
1103 *pl = insn_labels;
1104 insn_labels = NULL;
1105 }
1106 \f
1107 static char *expr_end;
1108
1109 /* Expressions which appear in instructions. These are set by
1110 mips_ip. */
1111
1112 static expressionS imm_expr;
1113 static expressionS offset_expr;
1114
1115 /* Relocs associated with imm_expr and offset_expr. */
1116
1117 static bfd_reloc_code_real_type imm_reloc[3]
1118 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1119 static bfd_reloc_code_real_type offset_reloc[3]
1120 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1121
1122 /* These are set by mips16_ip if an explicit extension is used. */
1123
1124 static bfd_boolean mips16_small, mips16_ext;
1125
1126 #ifdef OBJ_ELF
1127 /* The pdr segment for per procedure frame/regmask info. Not used for
1128 ECOFF debugging. */
1129
1130 static segT pdr_seg;
1131 #endif
1132
1133 /* The default target format to use. */
1134
1135 const char *
1136 mips_target_format ()
1137 {
1138 switch (OUTPUT_FLAVOR)
1139 {
1140 case bfd_target_aout_flavour:
1141 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1142 case bfd_target_ecoff_flavour:
1143 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1144 case bfd_target_coff_flavour:
1145 return "pe-mips";
1146 case bfd_target_elf_flavour:
1147 #ifdef TE_TMIPS
1148 /* This is traditional mips. */
1149 return (target_big_endian
1150 ? (HAVE_64BIT_OBJECTS
1151 ? "elf64-tradbigmips"
1152 : (HAVE_NEWABI
1153 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1154 : (HAVE_64BIT_OBJECTS
1155 ? "elf64-tradlittlemips"
1156 : (HAVE_NEWABI
1157 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1158 #else
1159 return (target_big_endian
1160 ? (HAVE_64BIT_OBJECTS
1161 ? "elf64-bigmips"
1162 : (HAVE_NEWABI
1163 ? "elf32-nbigmips" : "elf32-bigmips"))
1164 : (HAVE_64BIT_OBJECTS
1165 ? "elf64-littlemips"
1166 : (HAVE_NEWABI
1167 ? "elf32-nlittlemips" : "elf32-littlemips")));
1168 #endif
1169 default:
1170 abort ();
1171 return NULL;
1172 }
1173 }
1174
1175 /* This function is called once, at assembler startup time. It should
1176 set up all the tables, etc. that the MD part of the assembler will need. */
1177
1178 void
1179 md_begin ()
1180 {
1181 register const char *retval = NULL;
1182 int i = 0;
1183 int broken = 0;
1184
1185 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1186 as_warn (_("Could not set architecture and machine"));
1187
1188 op_hash = hash_new ();
1189
1190 for (i = 0; i < NUMOPCODES;)
1191 {
1192 const char *name = mips_opcodes[i].name;
1193
1194 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1195 if (retval != NULL)
1196 {
1197 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1198 mips_opcodes[i].name, retval);
1199 /* Probably a memory allocation problem? Give up now. */
1200 as_fatal (_("Broken assembler. No assembly attempted."));
1201 }
1202 do
1203 {
1204 if (mips_opcodes[i].pinfo != INSN_MACRO)
1205 {
1206 if (!validate_mips_insn (&mips_opcodes[i]))
1207 broken = 1;
1208 }
1209 ++i;
1210 }
1211 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1212 }
1213
1214 mips16_op_hash = hash_new ();
1215
1216 i = 0;
1217 while (i < bfd_mips16_num_opcodes)
1218 {
1219 const char *name = mips16_opcodes[i].name;
1220
1221 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1222 if (retval != NULL)
1223 as_fatal (_("internal: can't hash `%s': %s"),
1224 mips16_opcodes[i].name, retval);
1225 do
1226 {
1227 if (mips16_opcodes[i].pinfo != INSN_MACRO
1228 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1229 != mips16_opcodes[i].match))
1230 {
1231 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1232 mips16_opcodes[i].name, mips16_opcodes[i].args);
1233 broken = 1;
1234 }
1235 ++i;
1236 }
1237 while (i < bfd_mips16_num_opcodes
1238 && strcmp (mips16_opcodes[i].name, name) == 0);
1239 }
1240
1241 if (broken)
1242 as_fatal (_("Broken assembler. No assembly attempted."));
1243
1244 /* We add all the general register names to the symbol table. This
1245 helps us detect invalid uses of them. */
1246 for (i = 0; i < 32; i++)
1247 {
1248 char buf[5];
1249
1250 sprintf (buf, "$%d", i);
1251 symbol_table_insert (symbol_new (buf, reg_section, i,
1252 &zero_address_frag));
1253 }
1254 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1255 &zero_address_frag));
1256 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1257 &zero_address_frag));
1258 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1259 &zero_address_frag));
1260 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1261 &zero_address_frag));
1262 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1263 &zero_address_frag));
1264 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1265 &zero_address_frag));
1266 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1267 &zero_address_frag));
1268 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1269 &zero_address_frag));
1270 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1271 &zero_address_frag));
1272
1273 /* If we don't add these register names to the symbol table, they
1274 may end up being added as regular symbols by operand(), and then
1275 make it to the object file as undefined in case they're not
1276 regarded as local symbols. They're local in o32, since `$' is a
1277 local symbol prefix, but not in n32 or n64. */
1278 for (i = 0; i < 8; i++)
1279 {
1280 char buf[6];
1281
1282 sprintf (buf, "$fcc%i", i);
1283 symbol_table_insert (symbol_new (buf, reg_section, -1,
1284 &zero_address_frag));
1285 }
1286
1287 mips_no_prev_insn (FALSE);
1288
1289 mips_gprmask = 0;
1290 mips_cprmask[0] = 0;
1291 mips_cprmask[1] = 0;
1292 mips_cprmask[2] = 0;
1293 mips_cprmask[3] = 0;
1294
1295 /* set the default alignment for the text section (2**2) */
1296 record_alignment (text_section, 2);
1297
1298 if (USE_GLOBAL_POINTER_OPT)
1299 bfd_set_gp_size (stdoutput, g_switch_value);
1300
1301 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1302 {
1303 /* On a native system, sections must be aligned to 16 byte
1304 boundaries. When configured for an embedded ELF target, we
1305 don't bother. */
1306 if (strcmp (TARGET_OS, "elf") != 0)
1307 {
1308 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1309 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1310 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1311 }
1312
1313 /* Create a .reginfo section for register masks and a .mdebug
1314 section for debugging information. */
1315 {
1316 segT seg;
1317 subsegT subseg;
1318 flagword flags;
1319 segT sec;
1320
1321 seg = now_seg;
1322 subseg = now_subseg;
1323
1324 /* The ABI says this section should be loaded so that the
1325 running program can access it. However, we don't load it
1326 if we are configured for an embedded target */
1327 flags = SEC_READONLY | SEC_DATA;
1328 if (strcmp (TARGET_OS, "elf") != 0)
1329 flags |= SEC_ALLOC | SEC_LOAD;
1330
1331 if (mips_abi != N64_ABI)
1332 {
1333 sec = subseg_new (".reginfo", (subsegT) 0);
1334
1335 bfd_set_section_flags (stdoutput, sec, flags);
1336 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1337
1338 #ifdef OBJ_ELF
1339 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1340 #endif
1341 }
1342 else
1343 {
1344 /* The 64-bit ABI uses a .MIPS.options section rather than
1345 .reginfo section. */
1346 sec = subseg_new (".MIPS.options", (subsegT) 0);
1347 bfd_set_section_flags (stdoutput, sec, flags);
1348 bfd_set_section_alignment (stdoutput, sec, 3);
1349
1350 #ifdef OBJ_ELF
1351 /* Set up the option header. */
1352 {
1353 Elf_Internal_Options opthdr;
1354 char *f;
1355
1356 opthdr.kind = ODK_REGINFO;
1357 opthdr.size = (sizeof (Elf_External_Options)
1358 + sizeof (Elf64_External_RegInfo));
1359 opthdr.section = 0;
1360 opthdr.info = 0;
1361 f = frag_more (sizeof (Elf_External_Options));
1362 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1363 (Elf_External_Options *) f);
1364
1365 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1366 }
1367 #endif
1368 }
1369
1370 if (ECOFF_DEBUGGING)
1371 {
1372 sec = subseg_new (".mdebug", (subsegT) 0);
1373 (void) bfd_set_section_flags (stdoutput, sec,
1374 SEC_HAS_CONTENTS | SEC_READONLY);
1375 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1376 }
1377 #ifdef OBJ_ELF
1378 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1379 {
1380 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1381 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1382 SEC_READONLY | SEC_RELOC
1383 | SEC_DEBUGGING);
1384 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1385 }
1386 #endif
1387
1388 subseg_set (seg, subseg);
1389 }
1390 }
1391
1392 if (! ECOFF_DEBUGGING)
1393 md_obj_begin ();
1394 }
1395
1396 void
1397 md_mips_end ()
1398 {
1399 if (! ECOFF_DEBUGGING)
1400 md_obj_end ();
1401 }
1402
1403 void
1404 md_assemble (str)
1405 char *str;
1406 {
1407 struct mips_cl_insn insn;
1408 bfd_reloc_code_real_type unused_reloc[3]
1409 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1410
1411 imm_expr.X_op = O_absent;
1412 offset_expr.X_op = O_absent;
1413 imm_reloc[0] = BFD_RELOC_UNUSED;
1414 imm_reloc[1] = BFD_RELOC_UNUSED;
1415 imm_reloc[2] = BFD_RELOC_UNUSED;
1416 offset_reloc[0] = BFD_RELOC_UNUSED;
1417 offset_reloc[1] = BFD_RELOC_UNUSED;
1418 offset_reloc[2] = BFD_RELOC_UNUSED;
1419
1420 if (mips_opts.mips16)
1421 mips16_ip (str, &insn);
1422 else
1423 {
1424 mips_ip (str, &insn);
1425 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1426 str, insn.insn_opcode));
1427 }
1428
1429 if (insn_error)
1430 {
1431 as_bad ("%s `%s'", insn_error, str);
1432 return;
1433 }
1434
1435 if (insn.insn_mo->pinfo == INSN_MACRO)
1436 {
1437 if (mips_opts.mips16)
1438 mips16_macro (&insn);
1439 else
1440 macro (&insn);
1441 }
1442 else
1443 {
1444 if (imm_expr.X_op != O_absent)
1445 append_insn (NULL, &insn, &imm_expr, imm_reloc);
1446 else if (offset_expr.X_op != O_absent)
1447 append_insn (NULL, &insn, &offset_expr, offset_reloc);
1448 else
1449 append_insn (NULL, &insn, NULL, unused_reloc);
1450 }
1451 }
1452
1453 /* Return true if the given relocation might need a matching %lo().
1454 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1455 applied to local symbols. */
1456
1457 static inline bfd_boolean
1458 reloc_needs_lo_p (reloc)
1459 bfd_reloc_code_real_type reloc;
1460 {
1461 return (reloc == BFD_RELOC_HI16_S
1462 || reloc == BFD_RELOC_MIPS_GOT16);
1463 }
1464
1465 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1466 relocation. */
1467
1468 static inline bfd_boolean
1469 fixup_has_matching_lo_p (fixp)
1470 fixS *fixp;
1471 {
1472 return (fixp->fx_next != NULL
1473 && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1474 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1475 && fixp->fx_offset == fixp->fx_next->fx_offset);
1476 }
1477
1478 /* See whether instruction IP reads register REG. CLASS is the type
1479 of register. */
1480
1481 static int
1482 insn_uses_reg (ip, reg, class)
1483 struct mips_cl_insn *ip;
1484 unsigned int reg;
1485 enum mips_regclass class;
1486 {
1487 if (class == MIPS16_REG)
1488 {
1489 assert (mips_opts.mips16);
1490 reg = mips16_to_32_reg_map[reg];
1491 class = MIPS_GR_REG;
1492 }
1493
1494 /* Don't report on general register ZERO, since it never changes. */
1495 if (class == MIPS_GR_REG && reg == ZERO)
1496 return 0;
1497
1498 if (class == MIPS_FP_REG)
1499 {
1500 assert (! mips_opts.mips16);
1501 /* If we are called with either $f0 or $f1, we must check $f0.
1502 This is not optimal, because it will introduce an unnecessary
1503 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1504 need to distinguish reading both $f0 and $f1 or just one of
1505 them. Note that we don't have to check the other way,
1506 because there is no instruction that sets both $f0 and $f1
1507 and requires a delay. */
1508 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1509 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1510 == (reg &~ (unsigned) 1)))
1511 return 1;
1512 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1513 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1514 == (reg &~ (unsigned) 1)))
1515 return 1;
1516 }
1517 else if (! mips_opts.mips16)
1518 {
1519 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1520 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1521 return 1;
1522 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1523 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1524 return 1;
1525 }
1526 else
1527 {
1528 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1529 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1530 & MIPS16OP_MASK_RX)]
1531 == reg))
1532 return 1;
1533 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1534 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1535 & MIPS16OP_MASK_RY)]
1536 == reg))
1537 return 1;
1538 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1539 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1540 & MIPS16OP_MASK_MOVE32Z)]
1541 == reg))
1542 return 1;
1543 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1544 return 1;
1545 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1546 return 1;
1547 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1548 return 1;
1549 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1550 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1551 & MIPS16OP_MASK_REGR32) == reg)
1552 return 1;
1553 }
1554
1555 return 0;
1556 }
1557
1558 /* This function returns true if modifying a register requires a
1559 delay. */
1560
1561 static int
1562 reg_needs_delay (reg)
1563 unsigned int reg;
1564 {
1565 unsigned long prev_pinfo;
1566
1567 prev_pinfo = prev_insn.insn_mo->pinfo;
1568 if (! mips_opts.noreorder
1569 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1570 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1571 || (! gpr_interlocks
1572 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1573 {
1574 /* A load from a coprocessor or from memory. All load
1575 delays delay the use of general register rt for one
1576 instruction on the r3000. The r6000 and r4000 use
1577 interlocks. */
1578 /* Itbl support may require additional care here. */
1579 know (prev_pinfo & INSN_WRITE_GPR_T);
1580 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1581 return 1;
1582 }
1583
1584 return 0;
1585 }
1586
1587 /* Mark instruction labels in mips16 mode. This permits the linker to
1588 handle them specially, such as generating jalx instructions when
1589 needed. We also make them odd for the duration of the assembly, in
1590 order to generate the right sort of code. We will make them even
1591 in the adjust_symtab routine, while leaving them marked. This is
1592 convenient for the debugger and the disassembler. The linker knows
1593 to make them odd again. */
1594
1595 static void
1596 mips16_mark_labels ()
1597 {
1598 if (mips_opts.mips16)
1599 {
1600 struct insn_label_list *l;
1601 valueT val;
1602
1603 for (l = insn_labels; l != NULL; l = l->next)
1604 {
1605 #ifdef OBJ_ELF
1606 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1607 S_SET_OTHER (l->label, STO_MIPS16);
1608 #endif
1609 val = S_GET_VALUE (l->label);
1610 if ((val & 1) == 0)
1611 S_SET_VALUE (l->label, val + 1);
1612 }
1613 }
1614 }
1615
1616 /* Output an instruction. PLACE is where to put the instruction; if
1617 it is NULL, this uses frag_more to get room. IP is the instruction
1618 information. ADDRESS_EXPR is an operand of the instruction to be
1619 used with RELOC_TYPE. */
1620
1621 static void
1622 append_insn (place, ip, address_expr, reloc_type)
1623 char *place;
1624 struct mips_cl_insn *ip;
1625 expressionS *address_expr;
1626 bfd_reloc_code_real_type *reloc_type;
1627 {
1628 register unsigned long prev_pinfo, pinfo;
1629 char *f;
1630 fixS *fixp[3];
1631 int nops = 0;
1632
1633 /* Mark instruction labels in mips16 mode. */
1634 mips16_mark_labels ();
1635
1636 prev_pinfo = prev_insn.insn_mo->pinfo;
1637 pinfo = ip->insn_mo->pinfo;
1638
1639 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1640 {
1641 int prev_prev_nop;
1642
1643 /* If the previous insn required any delay slots, see if we need
1644 to insert a NOP or two. There are eight kinds of possible
1645 hazards, of which an instruction can have at most one type.
1646 (1) a load from memory delay
1647 (2) a load from a coprocessor delay
1648 (3) an unconditional branch delay
1649 (4) a conditional branch delay
1650 (5) a move to coprocessor register delay
1651 (6) a load coprocessor register from memory delay
1652 (7) a coprocessor condition code delay
1653 (8) a HI/LO special register delay
1654
1655 There are a lot of optimizations we could do that we don't.
1656 In particular, we do not, in general, reorder instructions.
1657 If you use gcc with optimization, it will reorder
1658 instructions and generally do much more optimization then we
1659 do here; repeating all that work in the assembler would only
1660 benefit hand written assembly code, and does not seem worth
1661 it. */
1662
1663 /* This is how a NOP is emitted. */
1664 #define emit_nop() \
1665 (mips_opts.mips16 \
1666 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1667 : md_number_to_chars (frag_more (4), 0, 4))
1668
1669 /* The previous insn might require a delay slot, depending upon
1670 the contents of the current insn. */
1671 if (! mips_opts.mips16
1672 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1673 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1674 && ! cop_interlocks)
1675 || (! gpr_interlocks
1676 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1677 {
1678 /* A load from a coprocessor or from memory. All load
1679 delays delay the use of general register rt for one
1680 instruction on the r3000. The r6000 and r4000 use
1681 interlocks. */
1682 /* Itbl support may require additional care here. */
1683 know (prev_pinfo & INSN_WRITE_GPR_T);
1684 if (mips_optimize == 0
1685 || insn_uses_reg (ip,
1686 ((prev_insn.insn_opcode >> OP_SH_RT)
1687 & OP_MASK_RT),
1688 MIPS_GR_REG))
1689 ++nops;
1690 }
1691 else if (! mips_opts.mips16
1692 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1693 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1694 && ! cop_interlocks)
1695 || (mips_opts.isa == ISA_MIPS1
1696 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1697 {
1698 /* A generic coprocessor delay. The previous instruction
1699 modified a coprocessor general or control register. If
1700 it modified a control register, we need to avoid any
1701 coprocessor instruction (this is probably not always
1702 required, but it sometimes is). If it modified a general
1703 register, we avoid using that register.
1704
1705 On the r6000 and r4000 loading a coprocessor register
1706 from memory is interlocked, and does not require a delay.
1707
1708 This case is not handled very well. There is no special
1709 knowledge of CP0 handling, and the coprocessors other
1710 than the floating point unit are not distinguished at
1711 all. */
1712 /* Itbl support may require additional care here. FIXME!
1713 Need to modify this to include knowledge about
1714 user specified delays! */
1715 if (prev_pinfo & INSN_WRITE_FPR_T)
1716 {
1717 if (mips_optimize == 0
1718 || insn_uses_reg (ip,
1719 ((prev_insn.insn_opcode >> OP_SH_FT)
1720 & OP_MASK_FT),
1721 MIPS_FP_REG))
1722 ++nops;
1723 }
1724 else if (prev_pinfo & INSN_WRITE_FPR_S)
1725 {
1726 if (mips_optimize == 0
1727 || insn_uses_reg (ip,
1728 ((prev_insn.insn_opcode >> OP_SH_FS)
1729 & OP_MASK_FS),
1730 MIPS_FP_REG))
1731 ++nops;
1732 }
1733 else
1734 {
1735 /* We don't know exactly what the previous instruction
1736 does. If the current instruction uses a coprocessor
1737 register, we must insert a NOP. If previous
1738 instruction may set the condition codes, and the
1739 current instruction uses them, we must insert two
1740 NOPS. */
1741 /* Itbl support may require additional care here. */
1742 if (mips_optimize == 0
1743 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1744 && (pinfo & INSN_READ_COND_CODE)))
1745 nops += 2;
1746 else if (pinfo & INSN_COP)
1747 ++nops;
1748 }
1749 }
1750 else if (! mips_opts.mips16
1751 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1752 && (prev_pinfo & INSN_WRITE_COND_CODE)
1753 && ! cop_interlocks)
1754 {
1755 /* The previous instruction sets the coprocessor condition
1756 codes, but does not require a general coprocessor delay
1757 (this means it is a floating point comparison
1758 instruction). If this instruction uses the condition
1759 codes, we need to insert a single NOP. */
1760 /* Itbl support may require additional care here. */
1761 if (mips_optimize == 0
1762 || (pinfo & INSN_READ_COND_CODE))
1763 ++nops;
1764 }
1765
1766 /* If we're fixing up mfhi/mflo for the r7000 and the
1767 previous insn was an mfhi/mflo and the current insn
1768 reads the register that the mfhi/mflo wrote to, then
1769 insert two nops. */
1770
1771 else if (mips_7000_hilo_fix
1772 && MF_HILO_INSN (prev_pinfo)
1773 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1774 & OP_MASK_RD),
1775 MIPS_GR_REG))
1776 {
1777 nops += 2;
1778 }
1779
1780 /* If we're fixing up mfhi/mflo for the r7000 and the
1781 2nd previous insn was an mfhi/mflo and the current insn
1782 reads the register that the mfhi/mflo wrote to, then
1783 insert one nop. */
1784
1785 else if (mips_7000_hilo_fix
1786 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1787 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1788 & OP_MASK_RD),
1789 MIPS_GR_REG))
1790
1791 {
1792 ++nops;
1793 }
1794
1795 else if (prev_pinfo & INSN_READ_LO)
1796 {
1797 /* The previous instruction reads the LO register; if the
1798 current instruction writes to the LO register, we must
1799 insert two NOPS. Some newer processors have interlocks.
1800 Also the tx39's multiply instructions can be exectuted
1801 immediatly after a read from HI/LO (without the delay),
1802 though the tx39's divide insns still do require the
1803 delay. */
1804 if (! (hilo_interlocks
1805 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1806 && (mips_optimize == 0
1807 || (pinfo & INSN_WRITE_LO)))
1808 nops += 2;
1809 /* Most mips16 branch insns don't have a delay slot.
1810 If a read from LO is immediately followed by a branch
1811 to a write to LO we have a read followed by a write
1812 less than 2 insns away. We assume the target of
1813 a branch might be a write to LO, and insert a nop
1814 between a read and an immediately following branch. */
1815 else if (mips_opts.mips16
1816 && (mips_optimize == 0
1817 || (pinfo & MIPS16_INSN_BRANCH)))
1818 ++nops;
1819 }
1820 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1821 {
1822 /* The previous instruction reads the HI register; if the
1823 current instruction writes to the HI register, we must
1824 insert a NOP. Some newer processors have interlocks.
1825 Also the note tx39's multiply above. */
1826 if (! (hilo_interlocks
1827 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1828 && (mips_optimize == 0
1829 || (pinfo & INSN_WRITE_HI)))
1830 nops += 2;
1831 /* Most mips16 branch insns don't have a delay slot.
1832 If a read from HI is immediately followed by a branch
1833 to a write to HI we have a read followed by a write
1834 less than 2 insns away. We assume the target of
1835 a branch might be a write to HI, and insert a nop
1836 between a read and an immediately following branch. */
1837 else if (mips_opts.mips16
1838 && (mips_optimize == 0
1839 || (pinfo & MIPS16_INSN_BRANCH)))
1840 ++nops;
1841 }
1842
1843 /* If the previous instruction was in a noreorder section, then
1844 we don't want to insert the nop after all. */
1845 /* Itbl support may require additional care here. */
1846 if (prev_insn_unreordered)
1847 nops = 0;
1848
1849 /* There are two cases which require two intervening
1850 instructions: 1) setting the condition codes using a move to
1851 coprocessor instruction which requires a general coprocessor
1852 delay and then reading the condition codes 2) reading the HI
1853 or LO register and then writing to it (except on processors
1854 which have interlocks). If we are not already emitting a NOP
1855 instruction, we must check for these cases compared to the
1856 instruction previous to the previous instruction. */
1857 if ((! mips_opts.mips16
1858 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1859 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1860 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1861 && (pinfo & INSN_READ_COND_CODE)
1862 && ! cop_interlocks)
1863 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1864 && (pinfo & INSN_WRITE_LO)
1865 && ! (hilo_interlocks
1866 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1867 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1868 && (pinfo & INSN_WRITE_HI)
1869 && ! (hilo_interlocks
1870 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1871 prev_prev_nop = 1;
1872 else
1873 prev_prev_nop = 0;
1874
1875 if (prev_prev_insn_unreordered)
1876 prev_prev_nop = 0;
1877
1878 if (prev_prev_nop && nops == 0)
1879 ++nops;
1880
1881 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1882 {
1883 /* We're out of bits in pinfo, so we must resort to string
1884 ops here. Shortcuts are selected based on opcodes being
1885 limited to the VR4122 instruction set. */
1886 int min_nops = 0;
1887 const char *pn = prev_insn.insn_mo->name;
1888 const char *tn = ip->insn_mo->name;
1889 if (strncmp(pn, "macc", 4) == 0
1890 || strncmp(pn, "dmacc", 5) == 0)
1891 {
1892 /* Errata 21 - [D]DIV[U] after [D]MACC */
1893 if (strstr (tn, "div"))
1894 {
1895 min_nops = 1;
1896 }
1897
1898 /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1899 if (pn[0] == 'd' /* dmacc */
1900 && (strncmp(tn, "dmult", 5) == 0
1901 || strncmp(tn, "dmacc", 5) == 0))
1902 {
1903 min_nops = 1;
1904 }
1905
1906 /* Errata 24 - MT{LO,HI} after [D]MACC */
1907 if (strcmp (tn, "mtlo") == 0
1908 || strcmp (tn, "mthi") == 0)
1909 {
1910 min_nops = 1;
1911 }
1912
1913 }
1914 else if (strncmp(pn, "dmult", 5) == 0
1915 && (strncmp(tn, "dmult", 5) == 0
1916 || strncmp(tn, "dmacc", 5) == 0))
1917 {
1918 /* Here is the rest of errata 23. */
1919 min_nops = 1;
1920 }
1921 if (nops < min_nops)
1922 nops = min_nops;
1923 }
1924
1925 /* If we are being given a nop instruction, don't bother with
1926 one of the nops we would otherwise output. This will only
1927 happen when a nop instruction is used with mips_optimize set
1928 to 0. */
1929 if (nops > 0
1930 && ! mips_opts.noreorder
1931 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1932 --nops;
1933
1934 /* Now emit the right number of NOP instructions. */
1935 if (nops > 0 && ! mips_opts.noreorder)
1936 {
1937 fragS *old_frag;
1938 unsigned long old_frag_offset;
1939 int i;
1940 struct insn_label_list *l;
1941
1942 old_frag = frag_now;
1943 old_frag_offset = frag_now_fix ();
1944
1945 for (i = 0; i < nops; i++)
1946 emit_nop ();
1947
1948 if (listing)
1949 {
1950 listing_prev_line ();
1951 /* We may be at the start of a variant frag. In case we
1952 are, make sure there is enough space for the frag
1953 after the frags created by listing_prev_line. The
1954 argument to frag_grow here must be at least as large
1955 as the argument to all other calls to frag_grow in
1956 this file. We don't have to worry about being in the
1957 middle of a variant frag, because the variants insert
1958 all needed nop instructions themselves. */
1959 frag_grow (40);
1960 }
1961
1962 for (l = insn_labels; l != NULL; l = l->next)
1963 {
1964 valueT val;
1965
1966 assert (S_GET_SEGMENT (l->label) == now_seg);
1967 symbol_set_frag (l->label, frag_now);
1968 val = (valueT) frag_now_fix ();
1969 /* mips16 text labels are stored as odd. */
1970 if (mips_opts.mips16)
1971 ++val;
1972 S_SET_VALUE (l->label, val);
1973 }
1974
1975 #ifndef NO_ECOFF_DEBUGGING
1976 if (ECOFF_DEBUGGING)
1977 ecoff_fix_loc (old_frag, old_frag_offset);
1978 #endif
1979 }
1980 else if (prev_nop_frag != NULL)
1981 {
1982 /* We have a frag holding nops we may be able to remove. If
1983 we don't need any nops, we can decrease the size of
1984 prev_nop_frag by the size of one instruction. If we do
1985 need some nops, we count them in prev_nops_required. */
1986 if (prev_nop_frag_since == 0)
1987 {
1988 if (nops == 0)
1989 {
1990 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1991 --prev_nop_frag_holds;
1992 }
1993 else
1994 prev_nop_frag_required += nops;
1995 }
1996 else
1997 {
1998 if (prev_prev_nop == 0)
1999 {
2000 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2001 --prev_nop_frag_holds;
2002 }
2003 else
2004 ++prev_nop_frag_required;
2005 }
2006
2007 if (prev_nop_frag_holds <= prev_nop_frag_required)
2008 prev_nop_frag = NULL;
2009
2010 ++prev_nop_frag_since;
2011
2012 /* Sanity check: by the time we reach the second instruction
2013 after prev_nop_frag, we should have used up all the nops
2014 one way or another. */
2015 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2016 }
2017 }
2018
2019 if (place == NULL
2020 && address_expr
2021 && (*reloc_type == BFD_RELOC_16_PCREL_S2
2022 || *reloc_type == BFD_RELOC_MIPSEMB_16_PCREL_S2)
2023 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2024 || pinfo & INSN_COND_BRANCH_LIKELY)
2025 && mips_relax_branch
2026 /* Don't try branch relaxation within .set nomacro, or within
2027 .set noat if we use $at for PIC computations. If it turns
2028 out that the branch was out-of-range, we'll get an error. */
2029 && !mips_opts.warn_about_macros
2030 && !(mips_opts.noat && mips_pic != NO_PIC)
2031 && !mips_opts.mips16)
2032 {
2033 f = frag_var (rs_machine_dependent,
2034 relaxed_branch_length
2035 (NULL, NULL,
2036 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2037 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2038 RELAX_BRANCH_ENCODE
2039 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2040 pinfo & INSN_COND_BRANCH_LIKELY,
2041 pinfo & INSN_WRITE_GPR_31,
2042 0),
2043 address_expr->X_add_symbol,
2044 address_expr->X_add_number,
2045 0);
2046 *reloc_type = BFD_RELOC_UNUSED;
2047 }
2048 else if (*reloc_type > BFD_RELOC_UNUSED)
2049 {
2050 /* We need to set up a variant frag. */
2051 assert (mips_opts.mips16 && address_expr != NULL);
2052 f = frag_var (rs_machine_dependent, 4, 0,
2053 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2054 mips16_small, mips16_ext,
2055 (prev_pinfo
2056 & INSN_UNCOND_BRANCH_DELAY),
2057 (*prev_insn_reloc_type
2058 == BFD_RELOC_MIPS16_JMP)),
2059 make_expr_symbol (address_expr), 0, NULL);
2060 }
2061 else if (place != NULL)
2062 f = place;
2063 else if (mips_opts.mips16
2064 && ! ip->use_extend
2065 && *reloc_type != BFD_RELOC_MIPS16_JMP)
2066 {
2067 /* Make sure there is enough room to swap this instruction with
2068 a following jump instruction. */
2069 frag_grow (6);
2070 f = frag_more (2);
2071 }
2072 else
2073 {
2074 if (mips_opts.mips16
2075 && mips_opts.noreorder
2076 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2077 as_warn (_("extended instruction in delay slot"));
2078
2079 f = frag_more (4);
2080 }
2081
2082 fixp[0] = fixp[1] = fixp[2] = NULL;
2083 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2084 {
2085 if (address_expr->X_op == O_constant)
2086 {
2087 valueT tmp;
2088
2089 switch (*reloc_type)
2090 {
2091 case BFD_RELOC_32:
2092 ip->insn_opcode |= address_expr->X_add_number;
2093 break;
2094
2095 case BFD_RELOC_MIPS_HIGHEST:
2096 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2097 tmp >>= 16;
2098 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2099 break;
2100
2101 case BFD_RELOC_MIPS_HIGHER:
2102 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2103 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2104 break;
2105
2106 case BFD_RELOC_HI16_S:
2107 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2108 >> 16) & 0xffff;
2109 break;
2110
2111 case BFD_RELOC_HI16:
2112 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2113 break;
2114
2115 case BFD_RELOC_LO16:
2116 case BFD_RELOC_MIPS_GOT_DISP:
2117 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2118 break;
2119
2120 case BFD_RELOC_MIPS_JMP:
2121 if ((address_expr->X_add_number & 3) != 0)
2122 as_bad (_("jump to misaligned address (0x%lx)"),
2123 (unsigned long) address_expr->X_add_number);
2124 if (address_expr->X_add_number & ~0xfffffff)
2125 as_bad (_("jump address range overflow (0x%lx)"),
2126 (unsigned long) address_expr->X_add_number);
2127 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2128 break;
2129
2130 case BFD_RELOC_MIPS16_JMP:
2131 if ((address_expr->X_add_number & 3) != 0)
2132 as_bad (_("jump to misaligned address (0x%lx)"),
2133 (unsigned long) address_expr->X_add_number);
2134 if (address_expr->X_add_number & ~0xfffffff)
2135 as_bad (_("jump address range overflow (0x%lx)"),
2136 (unsigned long) address_expr->X_add_number);
2137 ip->insn_opcode |=
2138 (((address_expr->X_add_number & 0x7c0000) << 3)
2139 | ((address_expr->X_add_number & 0xf800000) >> 7)
2140 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2141 break;
2142
2143 case BFD_RELOC_16_PCREL_S2:
2144 if ((address_expr->X_add_number & 3) != 0)
2145 as_bad (_("branch to misaligned address (0x%lx)"),
2146 (unsigned long) address_expr->X_add_number);
2147 if (mips_relax_branch)
2148 goto need_reloc;
2149 if ((address_expr->X_add_number + 0x20000) & ~0x3ffff)
2150 as_bad (_("branch address range overflow (0x%lx)"),
2151 (unsigned long) address_expr->X_add_number);
2152 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0xffff;
2153 break;
2154
2155 case BFD_RELOC_MIPSEMB_16_PCREL_S2:
2156 goto need_reloc;
2157
2158 default:
2159 internalError ();
2160 }
2161 }
2162 else
2163 {
2164 need_reloc:
2165 /* Don't generate a reloc if we are writing into a variant frag. */
2166 if (place == NULL)
2167 {
2168 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2169 address_expr,
2170 (*reloc_type == BFD_RELOC_16_PCREL_S2
2171 || *reloc_type == BFD_RELOC_MIPSEMB_16_PCREL_S2),
2172 reloc_type[0]);
2173
2174 /* These relocations can have an addend that won't fit in
2175 4 octets for 64bit assembly. */
2176 if (HAVE_64BIT_GPRS &&
2177 (*reloc_type == BFD_RELOC_16
2178 || *reloc_type == BFD_RELOC_32
2179 || *reloc_type == BFD_RELOC_MIPS_JMP
2180 || *reloc_type == BFD_RELOC_HI16_S
2181 || *reloc_type == BFD_RELOC_LO16
2182 || *reloc_type == BFD_RELOC_GPREL16
2183 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2184 || *reloc_type == BFD_RELOC_GPREL32
2185 || *reloc_type == BFD_RELOC_64
2186 || *reloc_type == BFD_RELOC_CTOR
2187 || *reloc_type == BFD_RELOC_MIPS_SUB
2188 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2189 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2190 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2191 || *reloc_type == BFD_RELOC_MIPS_REL16
2192 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2193 fixp[0]->fx_no_overflow = 1;
2194
2195 if (reloc_needs_lo_p (*reloc_type))
2196 {
2197 struct mips_hi_fixup *hi_fixup;
2198
2199 /* Reuse the last entry if it already has a matching %lo. */
2200 hi_fixup = mips_hi_fixup_list;
2201 if (hi_fixup == 0
2202 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2203 {
2204 hi_fixup = ((struct mips_hi_fixup *)
2205 xmalloc (sizeof (struct mips_hi_fixup)));
2206 hi_fixup->next = mips_hi_fixup_list;
2207 mips_hi_fixup_list = hi_fixup;
2208 }
2209 hi_fixup->fixp = fixp[0];
2210 hi_fixup->seg = now_seg;
2211 }
2212
2213 if (reloc_type[1] != BFD_RELOC_UNUSED)
2214 {
2215 /* FIXME: This symbol can be one of
2216 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
2217 address_expr->X_op = O_absent;
2218 address_expr->X_add_symbol = 0;
2219 address_expr->X_add_number = 0;
2220
2221 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2222 4, address_expr, FALSE,
2223 reloc_type[1]);
2224
2225 /* These relocations can have an addend that won't fit in
2226 4 octets for 64bit assembly. */
2227 if (HAVE_64BIT_GPRS &&
2228 (*reloc_type == BFD_RELOC_16
2229 || *reloc_type == BFD_RELOC_32
2230 || *reloc_type == BFD_RELOC_MIPS_JMP
2231 || *reloc_type == BFD_RELOC_HI16_S
2232 || *reloc_type == BFD_RELOC_LO16
2233 || *reloc_type == BFD_RELOC_GPREL16
2234 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2235 || *reloc_type == BFD_RELOC_GPREL32
2236 || *reloc_type == BFD_RELOC_64
2237 || *reloc_type == BFD_RELOC_CTOR
2238 || *reloc_type == BFD_RELOC_MIPS_SUB
2239 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2240 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2241 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2242 || *reloc_type == BFD_RELOC_MIPS_REL16
2243 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2244 fixp[1]->fx_no_overflow = 1;
2245
2246 if (reloc_type[2] != BFD_RELOC_UNUSED)
2247 {
2248 address_expr->X_op = O_absent;
2249 address_expr->X_add_symbol = 0;
2250 address_expr->X_add_number = 0;
2251
2252 fixp[2] = fix_new_exp (frag_now,
2253 f - frag_now->fr_literal, 4,
2254 address_expr, FALSE,
2255 reloc_type[2]);
2256
2257 /* These relocations can have an addend that won't fit in
2258 4 octets for 64bit assembly. */
2259 if (HAVE_64BIT_GPRS &&
2260 (*reloc_type == BFD_RELOC_16
2261 || *reloc_type == BFD_RELOC_32
2262 || *reloc_type == BFD_RELOC_MIPS_JMP
2263 || *reloc_type == BFD_RELOC_HI16_S
2264 || *reloc_type == BFD_RELOC_LO16
2265 || *reloc_type == BFD_RELOC_GPREL16
2266 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2267 || *reloc_type == BFD_RELOC_GPREL32
2268 || *reloc_type == BFD_RELOC_64
2269 || *reloc_type == BFD_RELOC_CTOR
2270 || *reloc_type == BFD_RELOC_MIPS_SUB
2271 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2272 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2273 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2274 || *reloc_type == BFD_RELOC_MIPS_REL16
2275 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2276 fixp[2]->fx_no_overflow = 1;
2277 }
2278 }
2279 }
2280 }
2281 }
2282
2283 if (! mips_opts.mips16)
2284 {
2285 md_number_to_chars (f, ip->insn_opcode, 4);
2286 #ifdef OBJ_ELF
2287 dwarf2_emit_insn (4);
2288 #endif
2289 }
2290 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2291 {
2292 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2293 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2294 #ifdef OBJ_ELF
2295 dwarf2_emit_insn (4);
2296 #endif
2297 }
2298 else
2299 {
2300 if (ip->use_extend)
2301 {
2302 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2303 f += 2;
2304 }
2305 md_number_to_chars (f, ip->insn_opcode, 2);
2306 #ifdef OBJ_ELF
2307 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2308 #endif
2309 }
2310
2311 /* Update the register mask information. */
2312 if (! mips_opts.mips16)
2313 {
2314 if (pinfo & INSN_WRITE_GPR_D)
2315 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2316 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2317 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2318 if (pinfo & INSN_READ_GPR_S)
2319 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2320 if (pinfo & INSN_WRITE_GPR_31)
2321 mips_gprmask |= 1 << RA;
2322 if (pinfo & INSN_WRITE_FPR_D)
2323 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2324 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2325 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2326 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2327 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2328 if ((pinfo & INSN_READ_FPR_R) != 0)
2329 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2330 if (pinfo & INSN_COP)
2331 {
2332 /* We don't keep enough information to sort these cases out.
2333 The itbl support does keep this information however, although
2334 we currently don't support itbl fprmats as part of the cop
2335 instruction. May want to add this support in the future. */
2336 }
2337 /* Never set the bit for $0, which is always zero. */
2338 mips_gprmask &= ~1 << 0;
2339 }
2340 else
2341 {
2342 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2343 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2344 & MIPS16OP_MASK_RX);
2345 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2346 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2347 & MIPS16OP_MASK_RY);
2348 if (pinfo & MIPS16_INSN_WRITE_Z)
2349 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2350 & MIPS16OP_MASK_RZ);
2351 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2352 mips_gprmask |= 1 << TREG;
2353 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2354 mips_gprmask |= 1 << SP;
2355 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2356 mips_gprmask |= 1 << RA;
2357 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2358 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2359 if (pinfo & MIPS16_INSN_READ_Z)
2360 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2361 & MIPS16OP_MASK_MOVE32Z);
2362 if (pinfo & MIPS16_INSN_READ_GPR_X)
2363 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2364 & MIPS16OP_MASK_REGR32);
2365 }
2366
2367 if (place == NULL && ! mips_opts.noreorder)
2368 {
2369 /* Filling the branch delay slot is more complex. We try to
2370 switch the branch with the previous instruction, which we can
2371 do if the previous instruction does not set up a condition
2372 that the branch tests and if the branch is not itself the
2373 target of any branch. */
2374 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2375 || (pinfo & INSN_COND_BRANCH_DELAY))
2376 {
2377 if (mips_optimize < 2
2378 /* If we have seen .set volatile or .set nomove, don't
2379 optimize. */
2380 || mips_opts.nomove != 0
2381 /* If we had to emit any NOP instructions, then we
2382 already know we can not swap. */
2383 || nops != 0
2384 /* If we don't even know the previous insn, we can not
2385 swap. */
2386 || ! prev_insn_valid
2387 /* If the previous insn is already in a branch delay
2388 slot, then we can not swap. */
2389 || prev_insn_is_delay_slot
2390 /* If the previous previous insn was in a .set
2391 noreorder, we can't swap. Actually, the MIPS
2392 assembler will swap in this situation. However, gcc
2393 configured -with-gnu-as will generate code like
2394 .set noreorder
2395 lw $4,XXX
2396 .set reorder
2397 INSN
2398 bne $4,$0,foo
2399 in which we can not swap the bne and INSN. If gcc is
2400 not configured -with-gnu-as, it does not output the
2401 .set pseudo-ops. We don't have to check
2402 prev_insn_unreordered, because prev_insn_valid will
2403 be 0 in that case. We don't want to use
2404 prev_prev_insn_valid, because we do want to be able
2405 to swap at the start of a function. */
2406 || prev_prev_insn_unreordered
2407 /* If the branch is itself the target of a branch, we
2408 can not swap. We cheat on this; all we check for is
2409 whether there is a label on this instruction. If
2410 there are any branches to anything other than a
2411 label, users must use .set noreorder. */
2412 || insn_labels != NULL
2413 /* If the previous instruction is in a variant frag, we
2414 can not do the swap. This does not apply to the
2415 mips16, which uses variant frags for different
2416 purposes. */
2417 || (! mips_opts.mips16
2418 && prev_insn_frag->fr_type == rs_machine_dependent)
2419 /* If the branch reads the condition codes, we don't
2420 even try to swap, because in the sequence
2421 ctc1 $X,$31
2422 INSN
2423 INSN
2424 bc1t LABEL
2425 we can not swap, and I don't feel like handling that
2426 case. */
2427 || (! mips_opts.mips16
2428 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2429 && (pinfo & INSN_READ_COND_CODE))
2430 /* We can not swap with an instruction that requires a
2431 delay slot, becase the target of the branch might
2432 interfere with that instruction. */
2433 || (! mips_opts.mips16
2434 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2435 && (prev_pinfo
2436 /* Itbl support may require additional care here. */
2437 & (INSN_LOAD_COPROC_DELAY
2438 | INSN_COPROC_MOVE_DELAY
2439 | INSN_WRITE_COND_CODE)))
2440 || (! (hilo_interlocks
2441 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2442 && (prev_pinfo
2443 & (INSN_READ_LO
2444 | INSN_READ_HI)))
2445 || (! mips_opts.mips16
2446 && ! gpr_interlocks
2447 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2448 || (! mips_opts.mips16
2449 && mips_opts.isa == ISA_MIPS1
2450 /* Itbl support may require additional care here. */
2451 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2452 /* We can not swap with a branch instruction. */
2453 || (prev_pinfo
2454 & (INSN_UNCOND_BRANCH_DELAY
2455 | INSN_COND_BRANCH_DELAY
2456 | INSN_COND_BRANCH_LIKELY))
2457 /* We do not swap with a trap instruction, since it
2458 complicates trap handlers to have the trap
2459 instruction be in a delay slot. */
2460 || (prev_pinfo & INSN_TRAP)
2461 /* If the branch reads a register that the previous
2462 instruction sets, we can not swap. */
2463 || (! mips_opts.mips16
2464 && (prev_pinfo & INSN_WRITE_GPR_T)
2465 && insn_uses_reg (ip,
2466 ((prev_insn.insn_opcode >> OP_SH_RT)
2467 & OP_MASK_RT),
2468 MIPS_GR_REG))
2469 || (! mips_opts.mips16
2470 && (prev_pinfo & INSN_WRITE_GPR_D)
2471 && insn_uses_reg (ip,
2472 ((prev_insn.insn_opcode >> OP_SH_RD)
2473 & OP_MASK_RD),
2474 MIPS_GR_REG))
2475 || (mips_opts.mips16
2476 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2477 && insn_uses_reg (ip,
2478 ((prev_insn.insn_opcode
2479 >> MIPS16OP_SH_RX)
2480 & MIPS16OP_MASK_RX),
2481 MIPS16_REG))
2482 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2483 && insn_uses_reg (ip,
2484 ((prev_insn.insn_opcode
2485 >> MIPS16OP_SH_RY)
2486 & MIPS16OP_MASK_RY),
2487 MIPS16_REG))
2488 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2489 && insn_uses_reg (ip,
2490 ((prev_insn.insn_opcode
2491 >> MIPS16OP_SH_RZ)
2492 & MIPS16OP_MASK_RZ),
2493 MIPS16_REG))
2494 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2495 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2496 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2497 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2498 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2499 && insn_uses_reg (ip,
2500 MIPS16OP_EXTRACT_REG32R (prev_insn.
2501 insn_opcode),
2502 MIPS_GR_REG))))
2503 /* If the branch writes a register that the previous
2504 instruction sets, we can not swap (we know that
2505 branches write only to RD or to $31). */
2506 || (! mips_opts.mips16
2507 && (prev_pinfo & INSN_WRITE_GPR_T)
2508 && (((pinfo & INSN_WRITE_GPR_D)
2509 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2510 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2511 || ((pinfo & INSN_WRITE_GPR_31)
2512 && (((prev_insn.insn_opcode >> OP_SH_RT)
2513 & OP_MASK_RT)
2514 == RA))))
2515 || (! mips_opts.mips16
2516 && (prev_pinfo & INSN_WRITE_GPR_D)
2517 && (((pinfo & INSN_WRITE_GPR_D)
2518 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2519 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2520 || ((pinfo & INSN_WRITE_GPR_31)
2521 && (((prev_insn.insn_opcode >> OP_SH_RD)
2522 & OP_MASK_RD)
2523 == RA))))
2524 || (mips_opts.mips16
2525 && (pinfo & MIPS16_INSN_WRITE_31)
2526 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2527 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2528 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2529 == RA))))
2530 /* If the branch writes a register that the previous
2531 instruction reads, we can not swap (we know that
2532 branches only write to RD or to $31). */
2533 || (! mips_opts.mips16
2534 && (pinfo & INSN_WRITE_GPR_D)
2535 && insn_uses_reg (&prev_insn,
2536 ((ip->insn_opcode >> OP_SH_RD)
2537 & OP_MASK_RD),
2538 MIPS_GR_REG))
2539 || (! mips_opts.mips16
2540 && (pinfo & INSN_WRITE_GPR_31)
2541 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2542 || (mips_opts.mips16
2543 && (pinfo & MIPS16_INSN_WRITE_31)
2544 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2545 /* If we are generating embedded PIC code, the branch
2546 might be expanded into a sequence which uses $at, so
2547 we can't swap with an instruction which reads it. */
2548 || (mips_pic == EMBEDDED_PIC
2549 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2550 /* If the previous previous instruction has a load
2551 delay, and sets a register that the branch reads, we
2552 can not swap. */
2553 || (! mips_opts.mips16
2554 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2555 /* Itbl support may require additional care here. */
2556 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2557 || (! gpr_interlocks
2558 && (prev_prev_insn.insn_mo->pinfo
2559 & INSN_LOAD_MEMORY_DELAY)))
2560 && insn_uses_reg (ip,
2561 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2562 & OP_MASK_RT),
2563 MIPS_GR_REG))
2564 /* If one instruction sets a condition code and the
2565 other one uses a condition code, we can not swap. */
2566 || ((pinfo & INSN_READ_COND_CODE)
2567 && (prev_pinfo & INSN_WRITE_COND_CODE))
2568 || ((pinfo & INSN_WRITE_COND_CODE)
2569 && (prev_pinfo & INSN_READ_COND_CODE))
2570 /* If the previous instruction uses the PC, we can not
2571 swap. */
2572 || (mips_opts.mips16
2573 && (prev_pinfo & MIPS16_INSN_READ_PC))
2574 /* If the previous instruction was extended, we can not
2575 swap. */
2576 || (mips_opts.mips16 && prev_insn_extended)
2577 /* If the previous instruction had a fixup in mips16
2578 mode, we can not swap. This normally means that the
2579 previous instruction was a 4 byte branch anyhow. */
2580 || (mips_opts.mips16 && prev_insn_fixp[0])
2581 /* If the previous instruction is a sync, sync.l, or
2582 sync.p, we can not swap. */
2583 || (prev_pinfo & INSN_SYNC))
2584 {
2585 /* We could do even better for unconditional branches to
2586 portions of this object file; we could pick up the
2587 instruction at the destination, put it in the delay
2588 slot, and bump the destination address. */
2589 emit_nop ();
2590 /* Update the previous insn information. */
2591 prev_prev_insn = *ip;
2592 prev_insn.insn_mo = &dummy_opcode;
2593 }
2594 else
2595 {
2596 /* It looks like we can actually do the swap. */
2597 if (! mips_opts.mips16)
2598 {
2599 char *prev_f;
2600 char temp[4];
2601
2602 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2603 memcpy (temp, prev_f, 4);
2604 memcpy (prev_f, f, 4);
2605 memcpy (f, temp, 4);
2606 if (prev_insn_fixp[0])
2607 {
2608 prev_insn_fixp[0]->fx_frag = frag_now;
2609 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2610 }
2611 if (prev_insn_fixp[1])
2612 {
2613 prev_insn_fixp[1]->fx_frag = frag_now;
2614 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2615 }
2616 if (prev_insn_fixp[2])
2617 {
2618 prev_insn_fixp[2]->fx_frag = frag_now;
2619 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2620 }
2621 if (fixp[0])
2622 {
2623 fixp[0]->fx_frag = prev_insn_frag;
2624 fixp[0]->fx_where = prev_insn_where;
2625 }
2626 if (fixp[1])
2627 {
2628 fixp[1]->fx_frag = prev_insn_frag;
2629 fixp[1]->fx_where = prev_insn_where;
2630 }
2631 if (fixp[2])
2632 {
2633 fixp[2]->fx_frag = prev_insn_frag;
2634 fixp[2]->fx_where = prev_insn_where;
2635 }
2636 }
2637 else
2638 {
2639 char *prev_f;
2640 char temp[2];
2641
2642 assert (prev_insn_fixp[0] == NULL);
2643 assert (prev_insn_fixp[1] == NULL);
2644 assert (prev_insn_fixp[2] == NULL);
2645 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2646 memcpy (temp, prev_f, 2);
2647 memcpy (prev_f, f, 2);
2648 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2649 {
2650 assert (*reloc_type == BFD_RELOC_UNUSED);
2651 memcpy (f, temp, 2);
2652 }
2653 else
2654 {
2655 memcpy (f, f + 2, 2);
2656 memcpy (f + 2, temp, 2);
2657 }
2658 if (fixp[0])
2659 {
2660 fixp[0]->fx_frag = prev_insn_frag;
2661 fixp[0]->fx_where = prev_insn_where;
2662 }
2663 if (fixp[1])
2664 {
2665 fixp[1]->fx_frag = prev_insn_frag;
2666 fixp[1]->fx_where = prev_insn_where;
2667 }
2668 if (fixp[2])
2669 {
2670 fixp[2]->fx_frag = prev_insn_frag;
2671 fixp[2]->fx_where = prev_insn_where;
2672 }
2673 }
2674
2675 /* Update the previous insn information; leave prev_insn
2676 unchanged. */
2677 prev_prev_insn = *ip;
2678 }
2679 prev_insn_is_delay_slot = 1;
2680
2681 /* If that was an unconditional branch, forget the previous
2682 insn information. */
2683 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2684 {
2685 prev_prev_insn.insn_mo = &dummy_opcode;
2686 prev_insn.insn_mo = &dummy_opcode;
2687 }
2688
2689 prev_insn_fixp[0] = NULL;
2690 prev_insn_fixp[1] = NULL;
2691 prev_insn_fixp[2] = NULL;
2692 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2693 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2694 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2695 prev_insn_extended = 0;
2696 }
2697 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2698 {
2699 /* We don't yet optimize a branch likely. What we should do
2700 is look at the target, copy the instruction found there
2701 into the delay slot, and increment the branch to jump to
2702 the next instruction. */
2703 emit_nop ();
2704 /* Update the previous insn information. */
2705 prev_prev_insn = *ip;
2706 prev_insn.insn_mo = &dummy_opcode;
2707 prev_insn_fixp[0] = NULL;
2708 prev_insn_fixp[1] = NULL;
2709 prev_insn_fixp[2] = NULL;
2710 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2711 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2712 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2713 prev_insn_extended = 0;
2714 }
2715 else
2716 {
2717 /* Update the previous insn information. */
2718 if (nops > 0)
2719 prev_prev_insn.insn_mo = &dummy_opcode;
2720 else
2721 prev_prev_insn = prev_insn;
2722 prev_insn = *ip;
2723
2724 /* Any time we see a branch, we always fill the delay slot
2725 immediately; since this insn is not a branch, we know it
2726 is not in a delay slot. */
2727 prev_insn_is_delay_slot = 0;
2728
2729 prev_insn_fixp[0] = fixp[0];
2730 prev_insn_fixp[1] = fixp[1];
2731 prev_insn_fixp[2] = fixp[2];
2732 prev_insn_reloc_type[0] = reloc_type[0];
2733 prev_insn_reloc_type[1] = reloc_type[1];
2734 prev_insn_reloc_type[2] = reloc_type[2];
2735 if (mips_opts.mips16)
2736 prev_insn_extended = (ip->use_extend
2737 || *reloc_type > BFD_RELOC_UNUSED);
2738 }
2739
2740 prev_prev_insn_unreordered = prev_insn_unreordered;
2741 prev_insn_unreordered = 0;
2742 prev_insn_frag = frag_now;
2743 prev_insn_where = f - frag_now->fr_literal;
2744 prev_insn_valid = 1;
2745 }
2746 else if (place == NULL)
2747 {
2748 /* We need to record a bit of information even when we are not
2749 reordering, in order to determine the base address for mips16
2750 PC relative relocs. */
2751 prev_prev_insn = prev_insn;
2752 prev_insn = *ip;
2753 prev_insn_reloc_type[0] = reloc_type[0];
2754 prev_insn_reloc_type[1] = reloc_type[1];
2755 prev_insn_reloc_type[2] = reloc_type[2];
2756 prev_prev_insn_unreordered = prev_insn_unreordered;
2757 prev_insn_unreordered = 1;
2758 }
2759
2760 /* We just output an insn, so the next one doesn't have a label. */
2761 mips_clear_insn_labels ();
2762 }
2763
2764 /* This function forgets that there was any previous instruction or
2765 label. If PRESERVE is non-zero, it remembers enough information to
2766 know whether nops are needed before a noreorder section. */
2767
2768 static void
2769 mips_no_prev_insn (preserve)
2770 int preserve;
2771 {
2772 if (! preserve)
2773 {
2774 prev_insn.insn_mo = &dummy_opcode;
2775 prev_prev_insn.insn_mo = &dummy_opcode;
2776 prev_nop_frag = NULL;
2777 prev_nop_frag_holds = 0;
2778 prev_nop_frag_required = 0;
2779 prev_nop_frag_since = 0;
2780 }
2781 prev_insn_valid = 0;
2782 prev_insn_is_delay_slot = 0;
2783 prev_insn_unreordered = 0;
2784 prev_insn_extended = 0;
2785 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2786 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2787 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2788 prev_prev_insn_unreordered = 0;
2789 mips_clear_insn_labels ();
2790 }
2791
2792 /* This function must be called whenever we turn on noreorder or emit
2793 something other than instructions. It inserts any NOPS which might
2794 be needed by the previous instruction, and clears the information
2795 kept for the previous instructions. The INSNS parameter is true if
2796 instructions are to follow. */
2797
2798 static void
2799 mips_emit_delays (insns)
2800 bfd_boolean insns;
2801 {
2802 if (! mips_opts.noreorder)
2803 {
2804 int nops;
2805
2806 nops = 0;
2807 if ((! mips_opts.mips16
2808 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2809 && (! cop_interlocks
2810 && (prev_insn.insn_mo->pinfo
2811 & (INSN_LOAD_COPROC_DELAY
2812 | INSN_COPROC_MOVE_DELAY
2813 | INSN_WRITE_COND_CODE))))
2814 || (! hilo_interlocks
2815 && (prev_insn.insn_mo->pinfo
2816 & (INSN_READ_LO
2817 | INSN_READ_HI)))
2818 || (! mips_opts.mips16
2819 && ! gpr_interlocks
2820 && (prev_insn.insn_mo->pinfo
2821 & INSN_LOAD_MEMORY_DELAY))
2822 || (! mips_opts.mips16
2823 && mips_opts.isa == ISA_MIPS1
2824 && (prev_insn.insn_mo->pinfo
2825 & INSN_COPROC_MEMORY_DELAY)))
2826 {
2827 /* Itbl support may require additional care here. */
2828 ++nops;
2829 if ((! mips_opts.mips16
2830 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2831 && (! cop_interlocks
2832 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2833 || (! hilo_interlocks
2834 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2835 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2836 ++nops;
2837
2838 if (prev_insn_unreordered)
2839 nops = 0;
2840 }
2841 else if ((! mips_opts.mips16
2842 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2843 && (! cop_interlocks
2844 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2845 || (! hilo_interlocks
2846 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2847 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2848 {
2849 /* Itbl support may require additional care here. */
2850 if (! prev_prev_insn_unreordered)
2851 ++nops;
2852 }
2853
2854 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2855 {
2856 int min_nops = 0;
2857 const char *pn = prev_insn.insn_mo->name;
2858 if (strncmp(pn, "macc", 4) == 0
2859 || strncmp(pn, "dmacc", 5) == 0
2860 || strncmp(pn, "dmult", 5) == 0)
2861 {
2862 min_nops = 1;
2863 }
2864 if (nops < min_nops)
2865 nops = min_nops;
2866 }
2867
2868 if (nops > 0)
2869 {
2870 struct insn_label_list *l;
2871
2872 if (insns)
2873 {
2874 /* Record the frag which holds the nop instructions, so
2875 that we can remove them if we don't need them. */
2876 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2877 prev_nop_frag = frag_now;
2878 prev_nop_frag_holds = nops;
2879 prev_nop_frag_required = 0;
2880 prev_nop_frag_since = 0;
2881 }
2882
2883 for (; nops > 0; --nops)
2884 emit_nop ();
2885
2886 if (insns)
2887 {
2888 /* Move on to a new frag, so that it is safe to simply
2889 decrease the size of prev_nop_frag. */
2890 frag_wane (frag_now);
2891 frag_new (0);
2892 }
2893
2894 for (l = insn_labels; l != NULL; l = l->next)
2895 {
2896 valueT val;
2897
2898 assert (S_GET_SEGMENT (l->label) == now_seg);
2899 symbol_set_frag (l->label, frag_now);
2900 val = (valueT) frag_now_fix ();
2901 /* mips16 text labels are stored as odd. */
2902 if (mips_opts.mips16)
2903 ++val;
2904 S_SET_VALUE (l->label, val);
2905 }
2906 }
2907 }
2908
2909 /* Mark instruction labels in mips16 mode. */
2910 if (insns)
2911 mips16_mark_labels ();
2912
2913 mips_no_prev_insn (insns);
2914 }
2915
2916 /* Build an instruction created by a macro expansion. This is passed
2917 a pointer to the count of instructions created so far, an
2918 expression, the name of the instruction to build, an operand format
2919 string, and corresponding arguments. */
2920
2921 #ifdef USE_STDARG
2922 static void
2923 macro_build (char *place,
2924 int *counter,
2925 expressionS * ep,
2926 const char *name,
2927 const char *fmt,
2928 ...)
2929 #else
2930 static void
2931 macro_build (place, counter, ep, name, fmt, va_alist)
2932 char *place;
2933 int *counter;
2934 expressionS *ep;
2935 const char *name;
2936 const char *fmt;
2937 va_dcl
2938 #endif
2939 {
2940 struct mips_cl_insn insn;
2941 bfd_reloc_code_real_type r[3];
2942 va_list args;
2943
2944 #ifdef USE_STDARG
2945 va_start (args, fmt);
2946 #else
2947 va_start (args);
2948 #endif
2949
2950 /*
2951 * If the macro is about to expand into a second instruction,
2952 * print a warning if needed. We need to pass ip as a parameter
2953 * to generate a better warning message here...
2954 */
2955 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2956 as_warn (_("Macro instruction expanded into multiple instructions"));
2957
2958 /*
2959 * If the macro is about to expand into a second instruction,
2960 * and it is in a delay slot, print a warning.
2961 */
2962 if (place == NULL
2963 && *counter == 1
2964 && mips_opts.noreorder
2965 && (prev_prev_insn.insn_mo->pinfo
2966 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2967 | INSN_COND_BRANCH_LIKELY)) != 0)
2968 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2969
2970 if (place == NULL)
2971 ++*counter; /* bump instruction counter */
2972
2973 if (mips_opts.mips16)
2974 {
2975 mips16_macro_build (place, counter, ep, name, fmt, args);
2976 va_end (args);
2977 return;
2978 }
2979
2980 r[0] = BFD_RELOC_UNUSED;
2981 r[1] = BFD_RELOC_UNUSED;
2982 r[2] = BFD_RELOC_UNUSED;
2983 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2984 assert (insn.insn_mo);
2985 assert (strcmp (name, insn.insn_mo->name) == 0);
2986
2987 /* Search until we get a match for NAME. */
2988 while (1)
2989 {
2990 /* It is assumed here that macros will never generate
2991 MDMX or MIPS-3D instructions. */
2992 if (strcmp (fmt, insn.insn_mo->args) == 0
2993 && insn.insn_mo->pinfo != INSN_MACRO
2994 && OPCODE_IS_MEMBER (insn.insn_mo,
2995 (mips_opts.isa
2996 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2997 mips_arch)
2998 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2999 break;
3000
3001 ++insn.insn_mo;
3002 assert (insn.insn_mo->name);
3003 assert (strcmp (name, insn.insn_mo->name) == 0);
3004 }
3005
3006 insn.insn_opcode = insn.insn_mo->match;
3007 for (;;)
3008 {
3009 switch (*fmt++)
3010 {
3011 case '\0':
3012 break;
3013
3014 case ',':
3015 case '(':
3016 case ')':
3017 continue;
3018
3019 case 't':
3020 case 'w':
3021 case 'E':
3022 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
3023 continue;
3024
3025 case 'c':
3026 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3027 continue;
3028
3029 case 'T':
3030 case 'W':
3031 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
3032 continue;
3033
3034 case 'd':
3035 case 'G':
3036 case 'K':
3037 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
3038 continue;
3039
3040 case 'U':
3041 {
3042 int tmp = va_arg (args, int);
3043
3044 insn.insn_opcode |= tmp << OP_SH_RT;
3045 insn.insn_opcode |= tmp << OP_SH_RD;
3046 continue;
3047 }
3048
3049 case 'V':
3050 case 'S':
3051 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3052 continue;
3053
3054 case 'z':
3055 continue;
3056
3057 case '<':
3058 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3059 continue;
3060
3061 case 'D':
3062 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3063 continue;
3064
3065 case 'B':
3066 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3067 continue;
3068
3069 case 'J':
3070 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3071 continue;
3072
3073 case 'q':
3074 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3075 continue;
3076
3077 case 'b':
3078 case 's':
3079 case 'r':
3080 case 'v':
3081 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3082 continue;
3083
3084 case 'i':
3085 case 'j':
3086 case 'o':
3087 *r = (bfd_reloc_code_real_type) va_arg (args, int);
3088 assert (*r == BFD_RELOC_GPREL16
3089 || *r == BFD_RELOC_MIPS_LITERAL
3090 || *r == BFD_RELOC_MIPS_HIGHER
3091 || *r == BFD_RELOC_HI16_S
3092 || *r == BFD_RELOC_LO16
3093 || *r == BFD_RELOC_MIPS_GOT16
3094 || *r == BFD_RELOC_MIPS_CALL16
3095 || *r == BFD_RELOC_MIPS_GOT_DISP
3096 || *r == BFD_RELOC_MIPS_GOT_PAGE
3097 || *r == BFD_RELOC_MIPS_GOT_OFST
3098 || *r == BFD_RELOC_MIPS_GOT_LO16
3099 || *r == BFD_RELOC_MIPS_CALL_LO16
3100 || (ep->X_op == O_subtract
3101 && *r == BFD_RELOC_PCREL_LO16));
3102 continue;
3103
3104 case 'u':
3105 *r = (bfd_reloc_code_real_type) va_arg (args, int);
3106 assert (ep != NULL
3107 && (ep->X_op == O_constant
3108 || (ep->X_op == O_symbol
3109 && (*r == BFD_RELOC_MIPS_HIGHEST
3110 || *r == BFD_RELOC_HI16_S
3111 || *r == BFD_RELOC_HI16
3112 || *r == BFD_RELOC_GPREL16
3113 || *r == BFD_RELOC_MIPS_GOT_HI16
3114 || *r == BFD_RELOC_MIPS_CALL_HI16))
3115 || (ep->X_op == O_subtract
3116 && *r == BFD_RELOC_PCREL_HI16_S)));
3117 continue;
3118
3119 case 'p':
3120 assert (ep != NULL);
3121
3122 /*
3123 * This allows macro() to pass an immediate expression for
3124 * creating short branches without creating a symbol.
3125 *
3126 * We don't allow branch relaxation for these branches, as
3127 * they should only appear in ".set nomacro" anyway.
3128 */
3129 if (ep->X_op == O_constant)
3130 {
3131 if ((ep->X_add_number & 3) != 0)
3132 as_bad (_("branch to misaligned address (0x%lx)"),
3133 (unsigned long) ep->X_add_number);
3134 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
3135 as_bad (_("branch address range overflow (0x%lx)"),
3136 (unsigned long) ep->X_add_number);
3137 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3138 ep = NULL;
3139 }
3140 else
3141 {
3142 if (mips_pic == EMBEDDED_PIC)
3143 *r = BFD_RELOC_MIPSEMB_16_PCREL_S2;
3144 else
3145 *r = BFD_RELOC_16_PCREL_S2;
3146 }
3147 continue;
3148
3149 case 'a':
3150 assert (ep != NULL);
3151 *r = BFD_RELOC_MIPS_JMP;
3152 continue;
3153
3154 case 'C':
3155 insn.insn_opcode |= va_arg (args, unsigned long);
3156 continue;
3157
3158 default:
3159 internalError ();
3160 }
3161 break;
3162 }
3163 va_end (args);
3164 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3165
3166 append_insn (place, &insn, ep, r);
3167 }
3168
3169 static void
3170 mips16_macro_build (place, counter, ep, name, fmt, args)
3171 char *place;
3172 int *counter ATTRIBUTE_UNUSED;
3173 expressionS *ep;
3174 const char *name;
3175 const char *fmt;
3176 va_list args;
3177 {
3178 struct mips_cl_insn insn;
3179 bfd_reloc_code_real_type r[3]
3180 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3181
3182 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3183 assert (insn.insn_mo);
3184 assert (strcmp (name, insn.insn_mo->name) == 0);
3185
3186 while (strcmp (fmt, insn.insn_mo->args) != 0
3187 || insn.insn_mo->pinfo == INSN_MACRO)
3188 {
3189 ++insn.insn_mo;
3190 assert (insn.insn_mo->name);
3191 assert (strcmp (name, insn.insn_mo->name) == 0);
3192 }
3193
3194 insn.insn_opcode = insn.insn_mo->match;
3195 insn.use_extend = FALSE;
3196
3197 for (;;)
3198 {
3199 int c;
3200
3201 c = *fmt++;
3202 switch (c)
3203 {
3204 case '\0':
3205 break;
3206
3207 case ',':
3208 case '(':
3209 case ')':
3210 continue;
3211
3212 case 'y':
3213 case 'w':
3214 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3215 continue;
3216
3217 case 'x':
3218 case 'v':
3219 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3220 continue;
3221
3222 case 'z':
3223 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3224 continue;
3225
3226 case 'Z':
3227 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3228 continue;
3229
3230 case '0':
3231 case 'S':
3232 case 'P':
3233 case 'R':
3234 continue;
3235
3236 case 'X':
3237 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3238 continue;
3239
3240 case 'Y':
3241 {
3242 int regno;
3243
3244 regno = va_arg (args, int);
3245 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3246 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3247 }
3248 continue;
3249
3250 case '<':
3251 case '>':
3252 case '4':
3253 case '5':
3254 case 'H':
3255 case 'W':
3256 case 'D':
3257 case 'j':
3258 case '8':
3259 case 'V':
3260 case 'C':
3261 case 'U':
3262 case 'k':
3263 case 'K':
3264 case 'p':
3265 case 'q':
3266 {
3267 assert (ep != NULL);
3268
3269 if (ep->X_op != O_constant)
3270 *r = (int) BFD_RELOC_UNUSED + c;
3271 else
3272 {
3273 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3274 FALSE, &insn.insn_opcode, &insn.use_extend,
3275 &insn.extend);
3276 ep = NULL;
3277 *r = BFD_RELOC_UNUSED;
3278 }
3279 }
3280 continue;
3281
3282 case '6':
3283 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3284 continue;
3285 }
3286
3287 break;
3288 }
3289
3290 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3291
3292 append_insn (place, &insn, ep, r);
3293 }
3294
3295 /*
3296 * Generate a "jalr" instruction with a relocation hint to the called
3297 * function. This occurs in NewABI PIC code.
3298 */
3299 static void
3300 macro_build_jalr (icnt, ep)
3301 int icnt;
3302 expressionS *ep;
3303 {
3304 char *f;
3305
3306 if (HAVE_NEWABI)
3307 {
3308 frag_grow (4);
3309 f = frag_more (0);
3310 }
3311 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3312 RA, PIC_CALL_REG);
3313 if (HAVE_NEWABI)
3314 fix_new_exp (frag_now, f - frag_now->fr_literal,
3315 0, ep, FALSE, BFD_RELOC_MIPS_JALR);
3316 }
3317
3318 /*
3319 * Generate a "lui" instruction.
3320 */
3321 static void
3322 macro_build_lui (place, counter, ep, regnum)
3323 char *place;
3324 int *counter;
3325 expressionS *ep;
3326 int regnum;
3327 {
3328 expressionS high_expr;
3329 struct mips_cl_insn insn;
3330 bfd_reloc_code_real_type r[3]
3331 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3332 const char *name = "lui";
3333 const char *fmt = "t,u";
3334
3335 assert (! mips_opts.mips16);
3336
3337 if (place == NULL)
3338 high_expr = *ep;
3339 else
3340 {
3341 high_expr.X_op = O_constant;
3342 high_expr.X_add_number = ep->X_add_number;
3343 }
3344
3345 if (high_expr.X_op == O_constant)
3346 {
3347 /* we can compute the instruction now without a relocation entry */
3348 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3349 >> 16) & 0xffff;
3350 *r = BFD_RELOC_UNUSED;
3351 }
3352 else
3353 {
3354 assert (ep->X_op == O_symbol);
3355 /* _gp_disp is a special case, used from s_cpload. */
3356 assert (mips_pic == NO_PIC
3357 || (! HAVE_NEWABI
3358 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3359 *r = BFD_RELOC_HI16_S;
3360 }
3361
3362 /*
3363 * If the macro is about to expand into a second instruction,
3364 * print a warning if needed. We need to pass ip as a parameter
3365 * to generate a better warning message here...
3366 */
3367 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3368 as_warn (_("Macro instruction expanded into multiple instructions"));
3369
3370 if (place == NULL)
3371 ++*counter; /* bump instruction counter */
3372
3373 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3374 assert (insn.insn_mo);
3375 assert (strcmp (name, insn.insn_mo->name) == 0);
3376 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3377
3378 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3379 if (*r == BFD_RELOC_UNUSED)
3380 {
3381 insn.insn_opcode |= high_expr.X_add_number;
3382 append_insn (place, &insn, NULL, r);
3383 }
3384 else
3385 append_insn (place, &insn, &high_expr, r);
3386 }
3387
3388 /* Generate a sequence of instructions to do a load or store from a constant
3389 offset off of a base register (breg) into/from a target register (treg),
3390 using AT if necessary. */
3391 static void
3392 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3393 char *place;
3394 int *counter;
3395 expressionS *ep;
3396 const char *op;
3397 int treg, breg;
3398 {
3399 assert (ep->X_op == O_constant);
3400
3401 /* Right now, this routine can only handle signed 32-bit contants. */
3402 if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3403 as_warn (_("operand overflow"));
3404
3405 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3406 {
3407 /* Signed 16-bit offset will fit in the op. Easy! */
3408 macro_build (place, counter, ep, op, "t,o(b)", treg,
3409 (int) BFD_RELOC_LO16, breg);
3410 }
3411 else
3412 {
3413 /* 32-bit offset, need multiple instructions and AT, like:
3414 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3415 addu $tempreg,$tempreg,$breg
3416 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3417 to handle the complete offset. */
3418 macro_build_lui (place, counter, ep, AT);
3419 if (place != NULL)
3420 place += 4;
3421 macro_build (place, counter, (expressionS *) NULL,
3422 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3423 "d,v,t", AT, AT, breg);
3424 if (place != NULL)
3425 place += 4;
3426 macro_build (place, counter, ep, op, "t,o(b)", treg,
3427 (int) BFD_RELOC_LO16, AT);
3428
3429 if (mips_opts.noat)
3430 as_warn (_("Macro used $at after \".set noat\""));
3431 }
3432 }
3433
3434 /* set_at()
3435 * Generates code to set the $at register to true (one)
3436 * if reg is less than the immediate expression.
3437 */
3438 static void
3439 set_at (counter, reg, unsignedp)
3440 int *counter;
3441 int reg;
3442 int unsignedp;
3443 {
3444 if (imm_expr.X_op == O_constant
3445 && imm_expr.X_add_number >= -0x8000
3446 && imm_expr.X_add_number < 0x8000)
3447 macro_build ((char *) NULL, counter, &imm_expr,
3448 unsignedp ? "sltiu" : "slti",
3449 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3450 else
3451 {
3452 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3453 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3454 unsignedp ? "sltu" : "slt",
3455 "d,v,t", AT, reg, AT);
3456 }
3457 }
3458
3459 /* Warn if an expression is not a constant. */
3460
3461 static void
3462 check_absolute_expr (ip, ex)
3463 struct mips_cl_insn *ip;
3464 expressionS *ex;
3465 {
3466 if (ex->X_op == O_big)
3467 as_bad (_("unsupported large constant"));
3468 else if (ex->X_op != O_constant)
3469 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3470 }
3471
3472 /* Count the leading zeroes by performing a binary chop. This is a
3473 bulky bit of source, but performance is a LOT better for the
3474 majority of values than a simple loop to count the bits:
3475 for (lcnt = 0; (lcnt < 32); lcnt++)
3476 if ((v) & (1 << (31 - lcnt)))
3477 break;
3478 However it is not code size friendly, and the gain will drop a bit
3479 on certain cached systems.
3480 */
3481 #define COUNT_TOP_ZEROES(v) \
3482 (((v) & ~0xffff) == 0 \
3483 ? ((v) & ~0xff) == 0 \
3484 ? ((v) & ~0xf) == 0 \
3485 ? ((v) & ~0x3) == 0 \
3486 ? ((v) & ~0x1) == 0 \
3487 ? !(v) \
3488 ? 32 \
3489 : 31 \
3490 : 30 \
3491 : ((v) & ~0x7) == 0 \
3492 ? 29 \
3493 : 28 \
3494 : ((v) & ~0x3f) == 0 \
3495 ? ((v) & ~0x1f) == 0 \
3496 ? 27 \
3497 : 26 \
3498 : ((v) & ~0x7f) == 0 \
3499 ? 25 \
3500 : 24 \
3501 : ((v) & ~0xfff) == 0 \
3502 ? ((v) & ~0x3ff) == 0 \
3503 ? ((v) & ~0x1ff) == 0 \
3504 ? 23 \
3505 : 22 \
3506 : ((v) & ~0x7ff) == 0 \
3507 ? 21 \
3508 : 20 \
3509 : ((v) & ~0x3fff) == 0 \
3510 ? ((v) & ~0x1fff) == 0 \
3511 ? 19 \
3512 : 18 \
3513 : ((v) & ~0x7fff) == 0 \
3514 ? 17 \
3515 : 16 \
3516 : ((v) & ~0xffffff) == 0 \
3517 ? ((v) & ~0xfffff) == 0 \
3518 ? ((v) & ~0x3ffff) == 0 \
3519 ? ((v) & ~0x1ffff) == 0 \
3520 ? 15 \
3521 : 14 \
3522 : ((v) & ~0x7ffff) == 0 \
3523 ? 13 \
3524 : 12 \
3525 : ((v) & ~0x3fffff) == 0 \
3526 ? ((v) & ~0x1fffff) == 0 \
3527 ? 11 \
3528 : 10 \
3529 : ((v) & ~0x7fffff) == 0 \
3530 ? 9 \
3531 : 8 \
3532 : ((v) & ~0xfffffff) == 0 \
3533 ? ((v) & ~0x3ffffff) == 0 \
3534 ? ((v) & ~0x1ffffff) == 0 \
3535 ? 7 \
3536 : 6 \
3537 : ((v) & ~0x7ffffff) == 0 \
3538 ? 5 \
3539 : 4 \
3540 : ((v) & ~0x3fffffff) == 0 \
3541 ? ((v) & ~0x1fffffff) == 0 \
3542 ? 3 \
3543 : 2 \
3544 : ((v) & ~0x7fffffff) == 0 \
3545 ? 1 \
3546 : 0)
3547
3548 /* load_register()
3549 * This routine generates the least number of instructions neccessary to load
3550 * an absolute expression value into a register.
3551 */
3552 static void
3553 load_register (counter, reg, ep, dbl)
3554 int *counter;
3555 int reg;
3556 expressionS *ep;
3557 int dbl;
3558 {
3559 int freg;
3560 expressionS hi32, lo32;
3561
3562 if (ep->X_op != O_big)
3563 {
3564 assert (ep->X_op == O_constant);
3565 if (ep->X_add_number < 0x8000
3566 && (ep->X_add_number >= 0
3567 || (ep->X_add_number >= -0x8000
3568 && (! dbl
3569 || ! ep->X_unsigned
3570 || sizeof (ep->X_add_number) > 4))))
3571 {
3572 /* We can handle 16 bit signed values with an addiu to
3573 $zero. No need to ever use daddiu here, since $zero and
3574 the result are always correct in 32 bit mode. */
3575 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3576 (int) BFD_RELOC_LO16);
3577 return;
3578 }
3579 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3580 {
3581 /* We can handle 16 bit unsigned values with an ori to
3582 $zero. */
3583 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3584 (int) BFD_RELOC_LO16);
3585 return;
3586 }
3587 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3588 && (! dbl
3589 || ! ep->X_unsigned
3590 || sizeof (ep->X_add_number) > 4
3591 || (ep->X_add_number & 0x80000000) == 0))
3592 || ((HAVE_32BIT_GPRS || ! dbl)
3593 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3594 || (HAVE_32BIT_GPRS
3595 && ! dbl
3596 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3597 == ~ (offsetT) 0xffffffff)))
3598 {
3599 /* 32 bit values require an lui. */
3600 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3601 (int) BFD_RELOC_HI16);
3602 if ((ep->X_add_number & 0xffff) != 0)
3603 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3604 (int) BFD_RELOC_LO16);
3605 return;
3606 }
3607 }
3608
3609 /* The value is larger than 32 bits. */
3610
3611 if (HAVE_32BIT_GPRS)
3612 {
3613 as_bad (_("Number (0x%lx) larger than 32 bits"),
3614 (unsigned long) ep->X_add_number);
3615 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3616 (int) BFD_RELOC_LO16);
3617 return;
3618 }
3619
3620 if (ep->X_op != O_big)
3621 {
3622 hi32 = *ep;
3623 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3624 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3625 hi32.X_add_number &= 0xffffffff;
3626 lo32 = *ep;
3627 lo32.X_add_number &= 0xffffffff;
3628 }
3629 else
3630 {
3631 assert (ep->X_add_number > 2);
3632 if (ep->X_add_number == 3)
3633 generic_bignum[3] = 0;
3634 else if (ep->X_add_number > 4)
3635 as_bad (_("Number larger than 64 bits"));
3636 lo32.X_op = O_constant;
3637 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3638 hi32.X_op = O_constant;
3639 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3640 }
3641
3642 if (hi32.X_add_number == 0)
3643 freg = 0;
3644 else
3645 {
3646 int shift, bit;
3647 unsigned long hi, lo;
3648
3649 if (hi32.X_add_number == (offsetT) 0xffffffff)
3650 {
3651 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3652 {
3653 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3654 reg, 0, (int) BFD_RELOC_LO16);
3655 return;
3656 }
3657 if (lo32.X_add_number & 0x80000000)
3658 {
3659 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3660 (int) BFD_RELOC_HI16);
3661 if (lo32.X_add_number & 0xffff)
3662 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3663 reg, reg, (int) BFD_RELOC_LO16);
3664 return;
3665 }
3666 }
3667
3668 /* Check for 16bit shifted constant. We know that hi32 is
3669 non-zero, so start the mask on the first bit of the hi32
3670 value. */
3671 shift = 17;
3672 do
3673 {
3674 unsigned long himask, lomask;
3675
3676 if (shift < 32)
3677 {
3678 himask = 0xffff >> (32 - shift);
3679 lomask = (0xffff << shift) & 0xffffffff;
3680 }
3681 else
3682 {
3683 himask = 0xffff << (shift - 32);
3684 lomask = 0;
3685 }
3686 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3687 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3688 {
3689 expressionS tmp;
3690
3691 tmp.X_op = O_constant;
3692 if (shift < 32)
3693 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3694 | (lo32.X_add_number >> shift));
3695 else
3696 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3697 macro_build ((char *) NULL, counter, &tmp,
3698 "ori", "t,r,i", reg, 0,
3699 (int) BFD_RELOC_LO16);
3700 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3701 (shift >= 32) ? "dsll32" : "dsll",
3702 "d,w,<", reg, reg,
3703 (shift >= 32) ? shift - 32 : shift);
3704 return;
3705 }
3706 ++shift;
3707 }
3708 while (shift <= (64 - 16));
3709
3710 /* Find the bit number of the lowest one bit, and store the
3711 shifted value in hi/lo. */
3712 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3713 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3714 if (lo != 0)
3715 {
3716 bit = 0;
3717 while ((lo & 1) == 0)
3718 {
3719 lo >>= 1;
3720 ++bit;
3721 }
3722 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3723 hi >>= bit;
3724 }
3725 else
3726 {
3727 bit = 32;
3728 while ((hi & 1) == 0)
3729 {
3730 hi >>= 1;
3731 ++bit;
3732 }
3733 lo = hi;
3734 hi = 0;
3735 }
3736
3737 /* Optimize if the shifted value is a (power of 2) - 1. */
3738 if ((hi == 0 && ((lo + 1) & lo) == 0)
3739 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3740 {
3741 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3742 if (shift != 0)
3743 {
3744 expressionS tmp;
3745
3746 /* This instruction will set the register to be all
3747 ones. */
3748 tmp.X_op = O_constant;
3749 tmp.X_add_number = (offsetT) -1;
3750 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3751 reg, 0, (int) BFD_RELOC_LO16);
3752 if (bit != 0)
3753 {
3754 bit += shift;
3755 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3756 (bit >= 32) ? "dsll32" : "dsll",
3757 "d,w,<", reg, reg,
3758 (bit >= 32) ? bit - 32 : bit);
3759 }
3760 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3761 (shift >= 32) ? "dsrl32" : "dsrl",
3762 "d,w,<", reg, reg,
3763 (shift >= 32) ? shift - 32 : shift);
3764 return;
3765 }
3766 }
3767
3768 /* Sign extend hi32 before calling load_register, because we can
3769 generally get better code when we load a sign extended value. */
3770 if ((hi32.X_add_number & 0x80000000) != 0)
3771 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3772 load_register (counter, reg, &hi32, 0);
3773 freg = reg;
3774 }
3775 if ((lo32.X_add_number & 0xffff0000) == 0)
3776 {
3777 if (freg != 0)
3778 {
3779 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3780 "dsll32", "d,w,<", reg, freg, 0);
3781 freg = reg;
3782 }
3783 }
3784 else
3785 {
3786 expressionS mid16;
3787
3788 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3789 {
3790 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3791 (int) BFD_RELOC_HI16);
3792 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3793 "dsrl32", "d,w,<", reg, reg, 0);
3794 return;
3795 }
3796
3797 if (freg != 0)
3798 {
3799 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3800 "d,w,<", reg, freg, 16);
3801 freg = reg;
3802 }
3803 mid16 = lo32;
3804 mid16.X_add_number >>= 16;
3805 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3806 freg, (int) BFD_RELOC_LO16);
3807 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3808 "d,w,<", reg, reg, 16);
3809 freg = reg;
3810 }
3811 if ((lo32.X_add_number & 0xffff) != 0)
3812 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3813 (int) BFD_RELOC_LO16);
3814 }
3815
3816 /* Load an address into a register. */
3817
3818 static void
3819 load_address (counter, reg, ep, used_at)
3820 int *counter;
3821 int reg;
3822 expressionS *ep;
3823 int *used_at;
3824 {
3825 char *p = NULL;
3826
3827 if (ep->X_op != O_constant
3828 && ep->X_op != O_symbol)
3829 {
3830 as_bad (_("expression too complex"));
3831 ep->X_op = O_constant;
3832 }
3833
3834 if (ep->X_op == O_constant)
3835 {
3836 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3837 return;
3838 }
3839
3840 if (mips_pic == NO_PIC)
3841 {
3842 /* If this is a reference to a GP relative symbol, we want
3843 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3844 Otherwise we want
3845 lui $reg,<sym> (BFD_RELOC_HI16_S)
3846 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3847 If we have an addend, we always use the latter form.
3848
3849 With 64bit address space and a usable $at we want
3850 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3851 lui $at,<sym> (BFD_RELOC_HI16_S)
3852 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3853 daddiu $at,<sym> (BFD_RELOC_LO16)
3854 dsll32 $reg,0
3855 daddu $reg,$reg,$at
3856
3857 If $at is already in use, we use a path which is suboptimal
3858 on superscalar processors.
3859 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3860 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3861 dsll $reg,16
3862 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3863 dsll $reg,16
3864 daddiu $reg,<sym> (BFD_RELOC_LO16)
3865 */
3866 if (HAVE_64BIT_ADDRESSES)
3867 {
3868 /* We don't do GP optimization for now because RELAX_ENCODE can't
3869 hold the data for such large chunks. */
3870
3871 if (*used_at == 0 && ! mips_opts.noat)
3872 {
3873 macro_build (p, counter, ep, "lui", "t,u",
3874 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3875 macro_build (p, counter, ep, "lui", "t,u",
3876 AT, (int) BFD_RELOC_HI16_S);
3877 macro_build (p, counter, ep, "daddiu", "t,r,j",
3878 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3879 macro_build (p, counter, ep, "daddiu", "t,r,j",
3880 AT, AT, (int) BFD_RELOC_LO16);
3881 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3882 "d,w,<", reg, reg, 0);
3883 macro_build (p, counter, (expressionS *) NULL, "daddu",
3884 "d,v,t", reg, reg, AT);
3885 *used_at = 1;
3886 }
3887 else
3888 {
3889 macro_build (p, counter, ep, "lui", "t,u",
3890 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3891 macro_build (p, counter, ep, "daddiu", "t,r,j",
3892 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3893 macro_build (p, counter, (expressionS *) NULL, "dsll",
3894 "d,w,<", reg, reg, 16);
3895 macro_build (p, counter, ep, "daddiu", "t,r,j",
3896 reg, reg, (int) BFD_RELOC_HI16_S);
3897 macro_build (p, counter, (expressionS *) NULL, "dsll",
3898 "d,w,<", reg, reg, 16);
3899 macro_build (p, counter, ep, "daddiu", "t,r,j",
3900 reg, reg, (int) BFD_RELOC_LO16);
3901 }
3902 }
3903 else
3904 {
3905 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3906 && ! nopic_need_relax (ep->X_add_symbol, 1))
3907 {
3908 frag_grow (20);
3909 macro_build ((char *) NULL, counter, ep,
3910 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3911 reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3912 p = frag_var (rs_machine_dependent, 8, 0,
3913 RELAX_ENCODE (4, 8, 0, 4, 0,
3914 mips_opts.warn_about_macros),
3915 ep->X_add_symbol, 0, NULL);
3916 }
3917 macro_build_lui (p, counter, ep, reg);
3918 if (p != NULL)
3919 p += 4;
3920 macro_build (p, counter, ep,
3921 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3922 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3923 }
3924 }
3925 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3926 {
3927 expressionS ex;
3928
3929 /* If this is a reference to an external symbol, we want
3930 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3931 Otherwise we want
3932 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3933 nop
3934 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3935 If we have NewABI, we want
3936 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3937 If there is a constant, it must be added in after. */
3938 ex.X_add_number = ep->X_add_number;
3939 ep->X_add_number = 0;
3940 frag_grow (20);
3941 if (HAVE_NEWABI)
3942 {
3943 macro_build ((char *) NULL, counter, ep,
3944 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3945 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3946 }
3947 else
3948 {
3949 macro_build ((char *) NULL, counter, ep,
3950 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3951 reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3952 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3953 p = frag_var (rs_machine_dependent, 4, 0,
3954 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3955 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3956 macro_build (p, counter, ep,
3957 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3958 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3959 }
3960
3961 if (ex.X_add_number != 0)
3962 {
3963 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3964 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3965 ex.X_op = O_constant;
3966 macro_build ((char *) NULL, counter, &ex,
3967 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3968 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3969 }
3970 }
3971 else if (mips_pic == SVR4_PIC)
3972 {
3973 expressionS ex;
3974 int off;
3975
3976 /* This is the large GOT case. If this is a reference to an
3977 external symbol, we want
3978 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3979 addu $reg,$reg,$gp
3980 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3981 Otherwise, for a reference to a local symbol, we want
3982 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3983 nop
3984 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3985 If we have NewABI, we want
3986 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3987 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
3988 If there is a constant, it must be added in after. */
3989 ex.X_add_number = ep->X_add_number;
3990 ep->X_add_number = 0;
3991 if (HAVE_NEWABI)
3992 {
3993 macro_build ((char *) NULL, counter, ep,
3994 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3995 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3996 macro_build (p, counter, ep,
3997 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3998 reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3999 }
4000 else
4001 {
4002 if (reg_needs_delay (mips_gp_register))
4003 off = 4;
4004 else
4005 off = 0;
4006 frag_grow (32);
4007 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
4008 (int) BFD_RELOC_MIPS_GOT_HI16);
4009 macro_build ((char *) NULL, counter, (expressionS *) NULL,
4010 HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
4011 reg, mips_gp_register);
4012 macro_build ((char *) NULL, counter, ep,
4013 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4014 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
4015 p = frag_var (rs_machine_dependent, 12 + off, 0,
4016 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
4017 mips_opts.warn_about_macros),
4018 ep->X_add_symbol, 0, NULL);
4019 if (off > 0)
4020 {
4021 /* We need a nop before loading from $gp. This special
4022 check is required because the lui which starts the main
4023 instruction stream does not refer to $gp, and so will not
4024 insert the nop which may be required. */
4025 macro_build (p, counter, (expressionS *) NULL, "nop", "");
4026 p += 4;
4027 }
4028 macro_build (p, counter, ep,
4029 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
4030 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
4031 p += 4;
4032 macro_build (p, counter, (expressionS *) NULL, "nop", "");
4033 p += 4;
4034 macro_build (p, counter, ep,
4035 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4036 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4037 }
4038
4039 if (ex.X_add_number != 0)
4040 {
4041 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4042 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4043 ex.X_op = O_constant;
4044 macro_build ((char *) NULL, counter, &ex,
4045 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4046 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4047 }
4048 }
4049 else if (mips_pic == EMBEDDED_PIC)
4050 {
4051 /* We always do
4052 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
4053 */
4054 macro_build ((char *) NULL, counter, ep,
4055 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4056 "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4057 }
4058 else
4059 abort ();
4060 }
4061
4062 /* Move the contents of register SOURCE into register DEST. */
4063
4064 static void
4065 move_register (counter, dest, source)
4066 int *counter;
4067 int dest;
4068 int source;
4069 {
4070 macro_build ((char *) NULL, counter, (expressionS *) NULL,
4071 HAVE_32BIT_GPRS ? "addu" : "daddu",
4072 "d,v,t", dest, source, 0);
4073 }
4074
4075 /*
4076 * Build macros
4077 * This routine implements the seemingly endless macro or synthesized
4078 * instructions and addressing modes in the mips assembly language. Many
4079 * of these macros are simple and are similar to each other. These could
4080 * probably be handled by some kind of table or grammer aproach instead of
4081 * this verbose method. Others are not simple macros but are more like
4082 * optimizing code generation.
4083 * One interesting optimization is when several store macros appear
4084 * consecutivly that would load AT with the upper half of the same address.
4085 * The ensuing load upper instructions are ommited. This implies some kind
4086 * of global optimization. We currently only optimize within a single macro.
4087 * For many of the load and store macros if the address is specified as a
4088 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4089 * first load register 'at' with zero and use it as the base register. The
4090 * mips assembler simply uses register $zero. Just one tiny optimization
4091 * we're missing.
4092 */
4093 static void
4094 macro (ip)
4095 struct mips_cl_insn *ip;
4096 {
4097 register int treg, sreg, dreg, breg;
4098 int tempreg;
4099 int mask;
4100 int icnt = 0;
4101 int used_at = 0;
4102 expressionS expr1;
4103 const char *s;
4104 const char *s2;
4105 const char *fmt;
4106 int likely = 0;
4107 int dbl = 0;
4108 int coproc = 0;
4109 int lr = 0;
4110 int imm = 0;
4111 offsetT maxnum;
4112 int off;
4113 bfd_reloc_code_real_type r;
4114 int hold_mips_optimize;
4115
4116 assert (! mips_opts.mips16);
4117
4118 treg = (ip->insn_opcode >> 16) & 0x1f;
4119 dreg = (ip->insn_opcode >> 11) & 0x1f;
4120 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4121 mask = ip->insn_mo->mask;
4122
4123 expr1.X_op = O_constant;
4124 expr1.X_op_symbol = NULL;
4125 expr1.X_add_symbol = NULL;
4126 expr1.X_add_number = 1;
4127
4128 /* Umatched fixups should not be put in the same frag as a relaxable
4129 macro. For example, suppose we have:
4130
4131 lui $4,%hi(l1) # 1
4132 la $5,l2 # 2
4133 addiu $4,$4,%lo(l1) # 3
4134
4135 If instructions 1 and 2 were put in the same frag, md_frob_file would
4136 move the fixup for #1 after the fixups for the "unrelaxed" version of
4137 #2. This would confuse tc_gen_reloc, which expects the relocations
4138 for #2 to be the last for that frag.
4139
4140 Also, if tc_gen_reloc sees certain relocations in a variant frag,
4141 it assumes that they belong to a relaxable macro. We mustn't put
4142 other uses of such relocations into a variant frag.
4143
4144 To avoid both problems, finish the current frag it contains a
4145 %reloc() operator. The macro then goes into a new frag. */
4146 if (prev_reloc_op_frag == frag_now)
4147 {
4148 frag_wane (frag_now);
4149 frag_new (0);
4150 }
4151
4152 switch (mask)
4153 {
4154 case M_DABS:
4155 dbl = 1;
4156 case M_ABS:
4157 /* bgez $a0,.+12
4158 move v0,$a0
4159 sub v0,$zero,$a0
4160 */
4161
4162 mips_emit_delays (TRUE);
4163 ++mips_opts.noreorder;
4164 mips_any_noreorder = 1;
4165
4166 expr1.X_add_number = 8;
4167 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4168 if (dreg == sreg)
4169 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4170 0);
4171 else
4172 move_register (&icnt, dreg, sreg);
4173 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4174 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4175
4176 --mips_opts.noreorder;
4177 return;
4178
4179 case M_ADD_I:
4180 s = "addi";
4181 s2 = "add";
4182 goto do_addi;
4183 case M_ADDU_I:
4184 s = "addiu";
4185 s2 = "addu";
4186 goto do_addi;
4187 case M_DADD_I:
4188 dbl = 1;
4189 s = "daddi";
4190 s2 = "dadd";
4191 goto do_addi;
4192 case M_DADDU_I:
4193 dbl = 1;
4194 s = "daddiu";
4195 s2 = "daddu";
4196 do_addi:
4197 if (imm_expr.X_op == O_constant
4198 && imm_expr.X_add_number >= -0x8000
4199 && imm_expr.X_add_number < 0x8000)
4200 {
4201 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4202 (int) BFD_RELOC_LO16);
4203 return;
4204 }
4205 load_register (&icnt, AT, &imm_expr, dbl);
4206 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4207 treg, sreg, AT);
4208 break;
4209
4210 case M_AND_I:
4211 s = "andi";
4212 s2 = "and";
4213 goto do_bit;
4214 case M_OR_I:
4215 s = "ori";
4216 s2 = "or";
4217 goto do_bit;
4218 case M_NOR_I:
4219 s = "";
4220 s2 = "nor";
4221 goto do_bit;
4222 case M_XOR_I:
4223 s = "xori";
4224 s2 = "xor";
4225 do_bit:
4226 if (imm_expr.X_op == O_constant
4227 && imm_expr.X_add_number >= 0
4228 && imm_expr.X_add_number < 0x10000)
4229 {
4230 if (mask != M_NOR_I)
4231 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4232 sreg, (int) BFD_RELOC_LO16);
4233 else
4234 {
4235 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4236 treg, sreg, (int) BFD_RELOC_LO16);
4237 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4238 "d,v,t", treg, treg, 0);
4239 }
4240 return;
4241 }
4242
4243 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4244 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4245 treg, sreg, AT);
4246 break;
4247
4248 case M_BEQ_I:
4249 s = "beq";
4250 goto beq_i;
4251 case M_BEQL_I:
4252 s = "beql";
4253 likely = 1;
4254 goto beq_i;
4255 case M_BNE_I:
4256 s = "bne";
4257 goto beq_i;
4258 case M_BNEL_I:
4259 s = "bnel";
4260 likely = 1;
4261 beq_i:
4262 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4263 {
4264 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4265 0);
4266 return;
4267 }
4268 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4269 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4270 break;
4271
4272 case M_BGEL:
4273 likely = 1;
4274 case M_BGE:
4275 if (treg == 0)
4276 {
4277 macro_build ((char *) NULL, &icnt, &offset_expr,
4278 likely ? "bgezl" : "bgez", "s,p", sreg);
4279 return;
4280 }
4281 if (sreg == 0)
4282 {
4283 macro_build ((char *) NULL, &icnt, &offset_expr,
4284 likely ? "blezl" : "blez", "s,p", treg);
4285 return;
4286 }
4287 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4288 AT, sreg, treg);
4289 macro_build ((char *) NULL, &icnt, &offset_expr,
4290 likely ? "beql" : "beq", "s,t,p", AT, 0);
4291 break;
4292
4293 case M_BGTL_I:
4294 likely = 1;
4295 case M_BGT_I:
4296 /* check for > max integer */
4297 maxnum = 0x7fffffff;
4298 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4299 {
4300 maxnum <<= 16;
4301 maxnum |= 0xffff;
4302 maxnum <<= 16;
4303 maxnum |= 0xffff;
4304 }
4305 if (imm_expr.X_op == O_constant
4306 && imm_expr.X_add_number >= maxnum
4307 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4308 {
4309 do_false:
4310 /* result is always false */
4311 if (! likely)
4312 {
4313 if (warn_nops)
4314 as_warn (_("Branch %s is always false (nop)"),
4315 ip->insn_mo->name);
4316 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4317 "", 0);
4318 }
4319 else
4320 {
4321 if (warn_nops)
4322 as_warn (_("Branch likely %s is always false"),
4323 ip->insn_mo->name);
4324 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4325 "s,t,p", 0, 0);
4326 }
4327 return;
4328 }
4329 if (imm_expr.X_op != O_constant)
4330 as_bad (_("Unsupported large constant"));
4331 ++imm_expr.X_add_number;
4332 /* FALLTHROUGH */
4333 case M_BGE_I:
4334 case M_BGEL_I:
4335 if (mask == M_BGEL_I)
4336 likely = 1;
4337 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4338 {
4339 macro_build ((char *) NULL, &icnt, &offset_expr,
4340 likely ? "bgezl" : "bgez", "s,p", sreg);
4341 return;
4342 }
4343 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4344 {
4345 macro_build ((char *) NULL, &icnt, &offset_expr,
4346 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4347 return;
4348 }
4349 maxnum = 0x7fffffff;
4350 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4351 {
4352 maxnum <<= 16;
4353 maxnum |= 0xffff;
4354 maxnum <<= 16;
4355 maxnum |= 0xffff;
4356 }
4357 maxnum = - maxnum - 1;
4358 if (imm_expr.X_op == O_constant
4359 && imm_expr.X_add_number <= maxnum
4360 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4361 {
4362 do_true:
4363 /* result is always true */
4364 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4365 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4366 return;
4367 }
4368 set_at (&icnt, sreg, 0);
4369 macro_build ((char *) NULL, &icnt, &offset_expr,
4370 likely ? "beql" : "beq", "s,t,p", AT, 0);
4371 break;
4372
4373 case M_BGEUL:
4374 likely = 1;
4375 case M_BGEU:
4376 if (treg == 0)
4377 goto do_true;
4378 if (sreg == 0)
4379 {
4380 macro_build ((char *) NULL, &icnt, &offset_expr,
4381 likely ? "beql" : "beq", "s,t,p", 0, treg);
4382 return;
4383 }
4384 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4385 "d,v,t", AT, sreg, treg);
4386 macro_build ((char *) NULL, &icnt, &offset_expr,
4387 likely ? "beql" : "beq", "s,t,p", AT, 0);
4388 break;
4389
4390 case M_BGTUL_I:
4391 likely = 1;
4392 case M_BGTU_I:
4393 if (sreg == 0
4394 || (HAVE_32BIT_GPRS
4395 && imm_expr.X_op == O_constant
4396 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4397 goto do_false;
4398 if (imm_expr.X_op != O_constant)
4399 as_bad (_("Unsupported large constant"));
4400 ++imm_expr.X_add_number;
4401 /* FALLTHROUGH */
4402 case M_BGEU_I:
4403 case M_BGEUL_I:
4404 if (mask == M_BGEUL_I)
4405 likely = 1;
4406 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4407 goto do_true;
4408 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4409 {
4410 macro_build ((char *) NULL, &icnt, &offset_expr,
4411 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4412 return;
4413 }
4414 set_at (&icnt, sreg, 1);
4415 macro_build ((char *) NULL, &icnt, &offset_expr,
4416 likely ? "beql" : "beq", "s,t,p", AT, 0);
4417 break;
4418
4419 case M_BGTL:
4420 likely = 1;
4421 case M_BGT:
4422 if (treg == 0)
4423 {
4424 macro_build ((char *) NULL, &icnt, &offset_expr,
4425 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4426 return;
4427 }
4428 if (sreg == 0)
4429 {
4430 macro_build ((char *) NULL, &icnt, &offset_expr,
4431 likely ? "bltzl" : "bltz", "s,p", treg);
4432 return;
4433 }
4434 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4435 AT, treg, sreg);
4436 macro_build ((char *) NULL, &icnt, &offset_expr,
4437 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4438 break;
4439
4440 case M_BGTUL:
4441 likely = 1;
4442 case M_BGTU:
4443 if (treg == 0)
4444 {
4445 macro_build ((char *) NULL, &icnt, &offset_expr,
4446 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4447 return;
4448 }
4449 if (sreg == 0)
4450 goto do_false;
4451 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4452 "d,v,t", AT, treg, sreg);
4453 macro_build ((char *) NULL, &icnt, &offset_expr,
4454 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4455 break;
4456
4457 case M_BLEL:
4458 likely = 1;
4459 case M_BLE:
4460 if (treg == 0)
4461 {
4462 macro_build ((char *) NULL, &icnt, &offset_expr,
4463 likely ? "blezl" : "blez", "s,p", sreg);
4464 return;
4465 }
4466 if (sreg == 0)
4467 {
4468 macro_build ((char *) NULL, &icnt, &offset_expr,
4469 likely ? "bgezl" : "bgez", "s,p", treg);
4470 return;
4471 }
4472 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4473 AT, treg, sreg);
4474 macro_build ((char *) NULL, &icnt, &offset_expr,
4475 likely ? "beql" : "beq", "s,t,p", AT, 0);
4476 break;
4477
4478 case M_BLEL_I:
4479 likely = 1;
4480 case M_BLE_I:
4481 maxnum = 0x7fffffff;
4482 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4483 {
4484 maxnum <<= 16;
4485 maxnum |= 0xffff;
4486 maxnum <<= 16;
4487 maxnum |= 0xffff;
4488 }
4489 if (imm_expr.X_op == O_constant
4490 && imm_expr.X_add_number >= maxnum
4491 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4492 goto do_true;
4493 if (imm_expr.X_op != O_constant)
4494 as_bad (_("Unsupported large constant"));
4495 ++imm_expr.X_add_number;
4496 /* FALLTHROUGH */
4497 case M_BLT_I:
4498 case M_BLTL_I:
4499 if (mask == M_BLTL_I)
4500 likely = 1;
4501 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4502 {
4503 macro_build ((char *) NULL, &icnt, &offset_expr,
4504 likely ? "bltzl" : "bltz", "s,p", sreg);
4505 return;
4506 }
4507 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4508 {
4509 macro_build ((char *) NULL, &icnt, &offset_expr,
4510 likely ? "blezl" : "blez", "s,p", sreg);
4511 return;
4512 }
4513 set_at (&icnt, sreg, 0);
4514 macro_build ((char *) NULL, &icnt, &offset_expr,
4515 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4516 break;
4517
4518 case M_BLEUL:
4519 likely = 1;
4520 case M_BLEU:
4521 if (treg == 0)
4522 {
4523 macro_build ((char *) NULL, &icnt, &offset_expr,
4524 likely ? "beql" : "beq", "s,t,p", sreg, 0);
4525 return;
4526 }
4527 if (sreg == 0)
4528 goto do_true;
4529 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4530 "d,v,t", AT, treg, sreg);
4531 macro_build ((char *) NULL, &icnt, &offset_expr,
4532 likely ? "beql" : "beq", "s,t,p", AT, 0);
4533 break;
4534
4535 case M_BLEUL_I:
4536 likely = 1;
4537 case M_BLEU_I:
4538 if (sreg == 0
4539 || (HAVE_32BIT_GPRS
4540 && imm_expr.X_op == O_constant
4541 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4542 goto do_true;
4543 if (imm_expr.X_op != O_constant)
4544 as_bad (_("Unsupported large constant"));
4545 ++imm_expr.X_add_number;
4546 /* FALLTHROUGH */
4547 case M_BLTU_I:
4548 case M_BLTUL_I:
4549 if (mask == M_BLTUL_I)
4550 likely = 1;
4551 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4552 goto do_false;
4553 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4554 {
4555 macro_build ((char *) NULL, &icnt, &offset_expr,
4556 likely ? "beql" : "beq",
4557 "s,t,p", sreg, 0);
4558 return;
4559 }
4560 set_at (&icnt, sreg, 1);
4561 macro_build ((char *) NULL, &icnt, &offset_expr,
4562 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4563 break;
4564
4565 case M_BLTL:
4566 likely = 1;
4567 case M_BLT:
4568 if (treg == 0)
4569 {
4570 macro_build ((char *) NULL, &icnt, &offset_expr,
4571 likely ? "bltzl" : "bltz", "s,p", sreg);
4572 return;
4573 }
4574 if (sreg == 0)
4575 {
4576 macro_build ((char *) NULL, &icnt, &offset_expr,
4577 likely ? "bgtzl" : "bgtz", "s,p", treg);
4578 return;
4579 }
4580 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4581 AT, sreg, treg);
4582 macro_build ((char *) NULL, &icnt, &offset_expr,
4583 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4584 break;
4585
4586 case M_BLTUL:
4587 likely = 1;
4588 case M_BLTU:
4589 if (treg == 0)
4590 goto do_false;
4591 if (sreg == 0)
4592 {
4593 macro_build ((char *) NULL, &icnt, &offset_expr,
4594 likely ? "bnel" : "bne", "s,t,p", 0, treg);
4595 return;
4596 }
4597 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4598 "d,v,t", AT, sreg,
4599 treg);
4600 macro_build ((char *) NULL, &icnt, &offset_expr,
4601 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4602 break;
4603
4604 case M_DDIV_3:
4605 dbl = 1;
4606 case M_DIV_3:
4607 s = "mflo";
4608 goto do_div3;
4609 case M_DREM_3:
4610 dbl = 1;
4611 case M_REM_3:
4612 s = "mfhi";
4613 do_div3:
4614 if (treg == 0)
4615 {
4616 as_warn (_("Divide by zero."));
4617 if (mips_trap)
4618 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4619 "s,t,q", 0, 0, 7);
4620 else
4621 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4622 "c", 7);
4623 return;
4624 }
4625
4626 mips_emit_delays (TRUE);
4627 ++mips_opts.noreorder;
4628 mips_any_noreorder = 1;
4629 if (mips_trap)
4630 {
4631 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4632 "s,t,q", treg, 0, 7);
4633 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4634 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4635 }
4636 else
4637 {
4638 expr1.X_add_number = 8;
4639 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4640 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4641 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4642 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4643 "c", 7);
4644 }
4645 expr1.X_add_number = -1;
4646 macro_build ((char *) NULL, &icnt, &expr1,
4647 dbl ? "daddiu" : "addiu",
4648 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4649 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4650 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4651 if (dbl)
4652 {
4653 expr1.X_add_number = 1;
4654 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4655 (int) BFD_RELOC_LO16);
4656 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4657 "d,w,<", AT, AT, 31);
4658 }
4659 else
4660 {
4661 expr1.X_add_number = 0x80000000;
4662 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4663 (int) BFD_RELOC_HI16);
4664 }
4665 if (mips_trap)
4666 {
4667 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4668 "s,t,q", sreg, AT, 6);
4669 /* We want to close the noreorder block as soon as possible, so
4670 that later insns are available for delay slot filling. */
4671 --mips_opts.noreorder;
4672 }
4673 else
4674 {
4675 expr1.X_add_number = 8;
4676 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4677 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4678 0);
4679
4680 /* We want to close the noreorder block as soon as possible, so
4681 that later insns are available for delay slot filling. */
4682 --mips_opts.noreorder;
4683
4684 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4685 "c", 6);
4686 }
4687 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4688 break;
4689
4690 case M_DIV_3I:
4691 s = "div";
4692 s2 = "mflo";
4693 goto do_divi;
4694 case M_DIVU_3I:
4695 s = "divu";
4696 s2 = "mflo";
4697 goto do_divi;
4698 case M_REM_3I:
4699 s = "div";
4700 s2 = "mfhi";
4701 goto do_divi;
4702 case M_REMU_3I:
4703 s = "divu";
4704 s2 = "mfhi";
4705 goto do_divi;
4706 case M_DDIV_3I:
4707 dbl = 1;
4708 s = "ddiv";
4709 s2 = "mflo";
4710 goto do_divi;
4711 case M_DDIVU_3I:
4712 dbl = 1;
4713 s = "ddivu";
4714 s2 = "mflo";
4715 goto do_divi;
4716 case M_DREM_3I:
4717 dbl = 1;
4718 s = "ddiv";
4719 s2 = "mfhi";
4720 goto do_divi;
4721 case M_DREMU_3I:
4722 dbl = 1;
4723 s = "ddivu";
4724 s2 = "mfhi";
4725 do_divi:
4726 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4727 {
4728 as_warn (_("Divide by zero."));
4729 if (mips_trap)
4730 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4731 "s,t,q", 0, 0, 7);
4732 else
4733 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4734 "c", 7);
4735 return;
4736 }
4737 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4738 {
4739 if (strcmp (s2, "mflo") == 0)
4740 move_register (&icnt, dreg, sreg);
4741 else
4742 move_register (&icnt, dreg, 0);
4743 return;
4744 }
4745 if (imm_expr.X_op == O_constant
4746 && imm_expr.X_add_number == -1
4747 && s[strlen (s) - 1] != 'u')
4748 {
4749 if (strcmp (s2, "mflo") == 0)
4750 {
4751 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4752 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4753 }
4754 else
4755 move_register (&icnt, dreg, 0);
4756 return;
4757 }
4758
4759 load_register (&icnt, AT, &imm_expr, dbl);
4760 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4761 sreg, AT);
4762 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4763 break;
4764
4765 case M_DIVU_3:
4766 s = "divu";
4767 s2 = "mflo";
4768 goto do_divu3;
4769 case M_REMU_3:
4770 s = "divu";
4771 s2 = "mfhi";
4772 goto do_divu3;
4773 case M_DDIVU_3:
4774 s = "ddivu";
4775 s2 = "mflo";
4776 goto do_divu3;
4777 case M_DREMU_3:
4778 s = "ddivu";
4779 s2 = "mfhi";
4780 do_divu3:
4781 mips_emit_delays (TRUE);
4782 ++mips_opts.noreorder;
4783 mips_any_noreorder = 1;
4784 if (mips_trap)
4785 {
4786 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4787 "s,t,q", treg, 0, 7);
4788 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4789 sreg, treg);
4790 /* We want to close the noreorder block as soon as possible, so
4791 that later insns are available for delay slot filling. */
4792 --mips_opts.noreorder;
4793 }
4794 else
4795 {
4796 expr1.X_add_number = 8;
4797 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4798 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4799 sreg, treg);
4800
4801 /* We want to close the noreorder block as soon as possible, so
4802 that later insns are available for delay slot filling. */
4803 --mips_opts.noreorder;
4804 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4805 "c", 7);
4806 }
4807 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4808 return;
4809
4810 case M_DLA_AB:
4811 dbl = 1;
4812 case M_LA_AB:
4813 /* Load the address of a symbol into a register. If breg is not
4814 zero, we then add a base register to it. */
4815
4816 if (dbl && HAVE_32BIT_GPRS)
4817 as_warn (_("dla used to load 32-bit register"));
4818
4819 if (! dbl && HAVE_64BIT_OBJECTS)
4820 as_warn (_("la used to load 64-bit address"));
4821
4822 if (offset_expr.X_op == O_constant
4823 && offset_expr.X_add_number >= -0x8000
4824 && offset_expr.X_add_number < 0x8000)
4825 {
4826 macro_build ((char *) NULL, &icnt, &offset_expr,
4827 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4828 "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4829 return;
4830 }
4831
4832 if (treg == breg)
4833 {
4834 tempreg = AT;
4835 used_at = 1;
4836 }
4837 else
4838 {
4839 tempreg = treg;
4840 used_at = 0;
4841 }
4842
4843 /* When generating embedded PIC code, we permit expressions of
4844 the form
4845 la $treg,foo-bar
4846 la $treg,foo-bar($breg)
4847 where bar is an address in the current section. These are used
4848 when getting the addresses of functions. We don't permit
4849 X_add_number to be non-zero, because if the symbol is
4850 external the relaxing code needs to know that any addend is
4851 purely the offset to X_op_symbol. */
4852 if (mips_pic == EMBEDDED_PIC
4853 && offset_expr.X_op == O_subtract
4854 && (symbol_constant_p (offset_expr.X_op_symbol)
4855 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4856 : (symbol_equated_p (offset_expr.X_op_symbol)
4857 && (S_GET_SEGMENT
4858 (symbol_get_value_expression (offset_expr.X_op_symbol)
4859 ->X_add_symbol)
4860 == now_seg)))
4861 && (offset_expr.X_add_number == 0
4862 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4863 {
4864 if (breg == 0)
4865 {
4866 tempreg = treg;
4867 used_at = 0;
4868 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4869 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4870 }
4871 else
4872 {
4873 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4874 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4875 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4876 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4877 "d,v,t", tempreg, tempreg, breg);
4878 }
4879 macro_build ((char *) NULL, &icnt, &offset_expr,
4880 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4881 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4882 if (! used_at)
4883 return;
4884 break;
4885 }
4886
4887 if (offset_expr.X_op != O_symbol
4888 && offset_expr.X_op != O_constant)
4889 {
4890 as_bad (_("expression too complex"));
4891 offset_expr.X_op = O_constant;
4892 }
4893
4894 if (offset_expr.X_op == O_constant)
4895 load_register (&icnt, tempreg, &offset_expr,
4896 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4897 ? (dbl || HAVE_64BIT_ADDRESSES)
4898 : HAVE_64BIT_ADDRESSES));
4899 else if (mips_pic == NO_PIC)
4900 {
4901 /* If this is a reference to a GP relative symbol, we want
4902 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4903 Otherwise we want
4904 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4905 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4906 If we have a constant, we need two instructions anyhow,
4907 so we may as well always use the latter form.
4908
4909 With 64bit address space and a usable $at we want
4910 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4911 lui $at,<sym> (BFD_RELOC_HI16_S)
4912 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4913 daddiu $at,<sym> (BFD_RELOC_LO16)
4914 dsll32 $tempreg,0
4915 daddu $tempreg,$tempreg,$at
4916
4917 If $at is already in use, we use a path which is suboptimal
4918 on superscalar processors.
4919 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4920 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4921 dsll $tempreg,16
4922 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4923 dsll $tempreg,16
4924 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4925 */
4926 char *p = NULL;
4927 if (HAVE_64BIT_ADDRESSES)
4928 {
4929 /* We don't do GP optimization for now because RELAX_ENCODE can't
4930 hold the data for such large chunks. */
4931
4932 if (used_at == 0 && ! mips_opts.noat)
4933 {
4934 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4935 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4936 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4937 AT, (int) BFD_RELOC_HI16_S);
4938 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4939 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4940 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4941 AT, AT, (int) BFD_RELOC_LO16);
4942 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4943 "d,w,<", tempreg, tempreg, 0);
4944 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4945 "d,v,t", tempreg, tempreg, AT);
4946 used_at = 1;
4947 }
4948 else
4949 {
4950 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4951 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4952 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4953 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4954 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4955 tempreg, tempreg, 16);
4956 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4957 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4958 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4959 tempreg, tempreg, 16);
4960 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4961 tempreg, tempreg, (int) BFD_RELOC_LO16);
4962 }
4963 }
4964 else
4965 {
4966 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4967 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4968 {
4969 frag_grow (20);
4970 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4971 "t,r,j", tempreg, mips_gp_register,
4972 (int) BFD_RELOC_GPREL16);
4973 p = frag_var (rs_machine_dependent, 8, 0,
4974 RELAX_ENCODE (4, 8, 0, 4, 0,
4975 mips_opts.warn_about_macros),
4976 offset_expr.X_add_symbol, 0, NULL);
4977 }
4978 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4979 if (p != NULL)
4980 p += 4;
4981 macro_build (p, &icnt, &offset_expr, "addiu",
4982 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4983 }
4984 }
4985 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4986 {
4987 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4988
4989 /* If this is a reference to an external symbol, and there
4990 is no constant, we want
4991 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4992 or if tempreg is PIC_CALL_REG
4993 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4994 For a local symbol, we want
4995 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4996 nop
4997 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4998
4999 If we have a small constant, and this is a reference to
5000 an external symbol, we want
5001 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5002 nop
5003 addiu $tempreg,$tempreg,<constant>
5004 For a local symbol, we want the same instruction
5005 sequence, but we output a BFD_RELOC_LO16 reloc on the
5006 addiu instruction.
5007
5008 If we have a large constant, and this is a reference to
5009 an external symbol, we want
5010 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5011 lui $at,<hiconstant>
5012 addiu $at,$at,<loconstant>
5013 addu $tempreg,$tempreg,$at
5014 For a local symbol, we want the same instruction
5015 sequence, but we output a BFD_RELOC_LO16 reloc on the
5016 addiu instruction.
5017
5018 For NewABI, we want for local or external data addresses
5019 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5020 For a local function symbol, we want
5021 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5022 nop
5023 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5024 */
5025
5026 expr1.X_add_number = offset_expr.X_add_number;
5027 offset_expr.X_add_number = 0;
5028 frag_grow (32);
5029 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5030 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5031 else if (HAVE_NEWABI)
5032 lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5033 macro_build ((char *) NULL, &icnt, &offset_expr,
5034 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5035 "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
5036 if (expr1.X_add_number == 0)
5037 {
5038 int off;
5039 char *p;
5040
5041 if (breg == 0)
5042 off = 0;
5043 else
5044 {
5045 /* We're going to put in an addu instruction using
5046 tempreg, so we may as well insert the nop right
5047 now. */
5048 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5049 "nop", "");
5050 off = 4;
5051 }
5052 p = frag_var (rs_machine_dependent, 8 - off, 0,
5053 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5054 (breg == 0
5055 ? mips_opts.warn_about_macros
5056 : 0)),
5057 offset_expr.X_add_symbol, 0, NULL);
5058 if (breg == 0)
5059 {
5060 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5061 p += 4;
5062 }
5063 macro_build (p, &icnt, &expr1,
5064 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5065 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5066 /* FIXME: If breg == 0, and the next instruction uses
5067 $tempreg, then if this variant case is used an extra
5068 nop will be generated. */
5069 }
5070 else if (expr1.X_add_number >= -0x8000
5071 && expr1.X_add_number < 0x8000)
5072 {
5073 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5074 "nop", "");
5075 macro_build ((char *) NULL, &icnt, &expr1,
5076 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5077 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5078 frag_var (rs_machine_dependent, 0, 0,
5079 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5080 offset_expr.X_add_symbol, 0, NULL);
5081 }
5082 else
5083 {
5084 int off1;
5085
5086 /* If we are going to add in a base register, and the
5087 target register and the base register are the same,
5088 then we are using AT as a temporary register. Since
5089 we want to load the constant into AT, we add our
5090 current AT (from the global offset table) and the
5091 register into the register now, and pretend we were
5092 not using a base register. */
5093 if (breg != treg)
5094 off1 = 0;
5095 else
5096 {
5097 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5098 "nop", "");
5099 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5100 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5101 "d,v,t", treg, AT, breg);
5102 breg = 0;
5103 tempreg = treg;
5104 off1 = -8;
5105 }
5106
5107 /* Set mips_optimize around the lui instruction to avoid
5108 inserting an unnecessary nop after the lw. */
5109 hold_mips_optimize = mips_optimize;
5110 mips_optimize = 2;
5111 macro_build_lui (NULL, &icnt, &expr1, AT);
5112 mips_optimize = hold_mips_optimize;
5113
5114 macro_build ((char *) NULL, &icnt, &expr1,
5115 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5116 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5117 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5118 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5119 "d,v,t", tempreg, tempreg, AT);
5120 frag_var (rs_machine_dependent, 0, 0,
5121 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5122 offset_expr.X_add_symbol, 0, NULL);
5123 used_at = 1;
5124 }
5125 }
5126 else if (mips_pic == SVR4_PIC)
5127 {
5128 int gpdel;
5129 char *p;
5130 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5131 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5132 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5133
5134 /* This is the large GOT case. If this is a reference to an
5135 external symbol, and there is no constant, we want
5136 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5137 addu $tempreg,$tempreg,$gp
5138 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5139 or if tempreg is PIC_CALL_REG
5140 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5141 addu $tempreg,$tempreg,$gp
5142 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5143 For a local symbol, we want
5144 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5145 nop
5146 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5147
5148 If we have a small constant, and this is a reference to
5149 an external symbol, we want
5150 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5151 addu $tempreg,$tempreg,$gp
5152 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5153 nop
5154 addiu $tempreg,$tempreg,<constant>
5155 For a local symbol, we want
5156 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5157 nop
5158 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5159
5160 If we have a large constant, and this is a reference to
5161 an external symbol, we want
5162 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5163 addu $tempreg,$tempreg,$gp
5164 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5165 lui $at,<hiconstant>
5166 addiu $at,$at,<loconstant>
5167 addu $tempreg,$tempreg,$at
5168 For a local symbol, we want
5169 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5170 lui $at,<hiconstant>
5171 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5172 addu $tempreg,$tempreg,$at
5173
5174 For NewABI, we want for local data addresses
5175 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5176 */
5177
5178 expr1.X_add_number = offset_expr.X_add_number;
5179 offset_expr.X_add_number = 0;
5180 frag_grow (52);
5181 if (reg_needs_delay (mips_gp_register))
5182 gpdel = 4;
5183 else
5184 gpdel = 0;
5185 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5186 {
5187 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5188 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5189 }
5190 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5191 tempreg, lui_reloc_type);
5192 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5193 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5194 "d,v,t", tempreg, tempreg, mips_gp_register);
5195 macro_build ((char *) NULL, &icnt, &offset_expr,
5196 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5197 "t,o(b)", tempreg, lw_reloc_type, tempreg);
5198 if (expr1.X_add_number == 0)
5199 {
5200 int off;
5201
5202 if (breg == 0)
5203 off = 0;
5204 else
5205 {
5206 /* We're going to put in an addu instruction using
5207 tempreg, so we may as well insert the nop right
5208 now. */
5209 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5210 "nop", "");
5211 off = 4;
5212 }
5213
5214 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5215 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5216 8 + gpdel, 0,
5217 (breg == 0
5218 ? mips_opts.warn_about_macros
5219 : 0)),
5220 offset_expr.X_add_symbol, 0, NULL);
5221 }
5222 else if (expr1.X_add_number >= -0x8000
5223 && expr1.X_add_number < 0x8000)
5224 {
5225 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5226 "nop", "");
5227 macro_build ((char *) NULL, &icnt, &expr1,
5228 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5229 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5230
5231 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5232 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5233 (breg == 0
5234 ? mips_opts.warn_about_macros
5235 : 0)),
5236 offset_expr.X_add_symbol, 0, NULL);
5237 }
5238 else
5239 {
5240 int adj, dreg;
5241
5242 /* If we are going to add in a base register, and the
5243 target register and the base register are the same,
5244 then we are using AT as a temporary register. Since
5245 we want to load the constant into AT, we add our
5246 current AT (from the global offset table) and the
5247 register into the register now, and pretend we were
5248 not using a base register. */
5249 if (breg != treg)
5250 {
5251 adj = 0;
5252 dreg = tempreg;
5253 }
5254 else
5255 {
5256 assert (tempreg == AT);
5257 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5258 "nop", "");
5259 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5260 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5261 "d,v,t", treg, AT, breg);
5262 dreg = treg;
5263 adj = 8;
5264 }
5265
5266 /* Set mips_optimize around the lui instruction to avoid
5267 inserting an unnecessary nop after the lw. */
5268 hold_mips_optimize = mips_optimize;
5269 mips_optimize = 2;
5270 macro_build_lui (NULL, &icnt, &expr1, AT);
5271 mips_optimize = hold_mips_optimize;
5272
5273 macro_build ((char *) NULL, &icnt, &expr1,
5274 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5275 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5276 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5277 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5278 "d,v,t", dreg, dreg, AT);
5279
5280 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5281 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5282 8 + gpdel, 0,
5283 (breg == 0
5284 ? mips_opts.warn_about_macros
5285 : 0)),
5286 offset_expr.X_add_symbol, 0, NULL);
5287
5288 used_at = 1;
5289 }
5290
5291 if (gpdel > 0)
5292 {
5293 /* This is needed because this instruction uses $gp, but
5294 the first instruction on the main stream does not. */
5295 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5296 p += 4;
5297 }
5298
5299 if (HAVE_NEWABI)
5300 local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5301 macro_build (p, &icnt, &offset_expr,
5302 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5303 "t,o(b)", tempreg,
5304 local_reloc_type,
5305 mips_gp_register);
5306 p += 4;
5307 if (expr1.X_add_number == 0 && HAVE_NEWABI)
5308 {
5309 /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
5310 }
5311 else
5312 if (expr1.X_add_number >= -0x8000
5313 && expr1.X_add_number < 0x8000)
5314 {
5315 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5316 p += 4;
5317 macro_build (p, &icnt, &expr1,
5318 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5319 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5320 /* FIXME: If add_number is 0, and there was no base
5321 register, the external symbol case ended with a load,
5322 so if the symbol turns out to not be external, and
5323 the next instruction uses tempreg, an unnecessary nop
5324 will be inserted. */
5325 }
5326 else
5327 {
5328 if (breg == treg)
5329 {
5330 /* We must add in the base register now, as in the
5331 external symbol case. */
5332 assert (tempreg == AT);
5333 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5334 p += 4;
5335 macro_build (p, &icnt, (expressionS *) NULL,
5336 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5337 "d,v,t", treg, AT, breg);
5338 p += 4;
5339 tempreg = treg;
5340 /* We set breg to 0 because we have arranged to add
5341 it in in both cases. */
5342 breg = 0;
5343 }
5344
5345 macro_build_lui (p, &icnt, &expr1, AT);
5346 p += 4;
5347 macro_build (p, &icnt, &expr1,
5348 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5349 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5350 p += 4;
5351 macro_build (p, &icnt, (expressionS *) NULL,
5352 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5353 "d,v,t", tempreg, tempreg, AT);
5354 p += 4;
5355 }
5356 }
5357 else if (mips_pic == EMBEDDED_PIC)
5358 {
5359 /* We use
5360 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
5361 */
5362 macro_build ((char *) NULL, &icnt, &offset_expr,
5363 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5364 tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5365 }
5366 else
5367 abort ();
5368
5369 if (breg != 0)
5370 {
5371 char *s;
5372
5373 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5374 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5375 else
5376 s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5377
5378 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5379 "d,v,t", treg, tempreg, breg);
5380 }
5381
5382 if (! used_at)
5383 return;
5384
5385 break;
5386
5387 case M_J_A:
5388 /* The j instruction may not be used in PIC code, since it
5389 requires an absolute address. We convert it to a b
5390 instruction. */
5391 if (mips_pic == NO_PIC)
5392 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5393 else
5394 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5395 return;
5396
5397 /* The jal instructions must be handled as macros because when
5398 generating PIC code they expand to multi-instruction
5399 sequences. Normally they are simple instructions. */
5400 case M_JAL_1:
5401 dreg = RA;
5402 /* Fall through. */
5403 case M_JAL_2:
5404 if (mips_pic == NO_PIC
5405 || mips_pic == EMBEDDED_PIC)
5406 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5407 "d,s", dreg, sreg);
5408 else if (mips_pic == SVR4_PIC)
5409 {
5410 if (sreg != PIC_CALL_REG)
5411 as_warn (_("MIPS PIC call to register other than $25"));
5412
5413 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5414 "d,s", dreg, sreg);
5415 if (! HAVE_NEWABI)
5416 {
5417 if (mips_cprestore_offset < 0)
5418 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5419 else
5420 {
5421 if (! mips_frame_reg_valid)
5422 {
5423 as_warn (_("No .frame pseudo-op used in PIC code"));
5424 /* Quiet this warning. */
5425 mips_frame_reg_valid = 1;
5426 }
5427 if (! mips_cprestore_valid)
5428 {
5429 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5430 /* Quiet this warning. */
5431 mips_cprestore_valid = 1;
5432 }
5433 expr1.X_add_number = mips_cprestore_offset;
5434 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5435 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5436 mips_gp_register, mips_frame_reg);
5437 }
5438 }
5439 }
5440 else
5441 abort ();
5442
5443 return;
5444
5445 case M_JAL_A:
5446 if (mips_pic == NO_PIC)
5447 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5448 else if (mips_pic == SVR4_PIC)
5449 {
5450 char *p;
5451
5452 /* If this is a reference to an external symbol, and we are
5453 using a small GOT, we want
5454 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5455 nop
5456 jalr $ra,$25
5457 nop
5458 lw $gp,cprestore($sp)
5459 The cprestore value is set using the .cprestore
5460 pseudo-op. If we are using a big GOT, we want
5461 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5462 addu $25,$25,$gp
5463 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5464 nop
5465 jalr $ra,$25
5466 nop
5467 lw $gp,cprestore($sp)
5468 If the symbol is not external, we want
5469 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5470 nop
5471 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5472 jalr $ra,$25
5473 nop
5474 lw $gp,cprestore($sp)
5475 For NewABI, we want
5476 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5477 jalr $ra,$25 (BFD_RELOC_MIPS_JALR)
5478 */
5479 if (HAVE_NEWABI)
5480 {
5481 macro_build ((char *) NULL, &icnt, &offset_expr,
5482 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5483 "t,o(b)", PIC_CALL_REG,
5484 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5485 macro_build_jalr (icnt, &offset_expr);
5486 }
5487 else
5488 {
5489 frag_grow (40);
5490 if (! mips_big_got)
5491 {
5492 macro_build ((char *) NULL, &icnt, &offset_expr,
5493 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5494 "t,o(b)", PIC_CALL_REG,
5495 (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5496 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5497 "nop", "");
5498 p = frag_var (rs_machine_dependent, 4, 0,
5499 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5500 offset_expr.X_add_symbol, 0, NULL);
5501 }
5502 else
5503 {
5504 int gpdel;
5505
5506 if (reg_needs_delay (mips_gp_register))
5507 gpdel = 4;
5508 else
5509 gpdel = 0;
5510 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5511 "t,u", PIC_CALL_REG,
5512 (int) BFD_RELOC_MIPS_CALL_HI16);
5513 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5514 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5515 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5516 mips_gp_register);
5517 macro_build ((char *) NULL, &icnt, &offset_expr,
5518 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5519 "t,o(b)", PIC_CALL_REG,
5520 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5521 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5522 "nop", "");
5523 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5524 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5525 8 + gpdel, 0, 0),
5526 offset_expr.X_add_symbol, 0, NULL);
5527 if (gpdel > 0)
5528 {
5529 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5530 p += 4;
5531 }
5532 macro_build (p, &icnt, &offset_expr,
5533 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5534 "t,o(b)", PIC_CALL_REG,
5535 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5536 p += 4;
5537 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5538 p += 4;
5539 }
5540 macro_build (p, &icnt, &offset_expr,
5541 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5542 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5543 (int) BFD_RELOC_LO16);
5544 macro_build_jalr (icnt, &offset_expr);
5545
5546 if (mips_cprestore_offset < 0)
5547 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5548 else
5549 {
5550 if (! mips_frame_reg_valid)
5551 {
5552 as_warn (_("No .frame pseudo-op used in PIC code"));
5553 /* Quiet this warning. */
5554 mips_frame_reg_valid = 1;
5555 }
5556 if (! mips_cprestore_valid)
5557 {
5558 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5559 /* Quiet this warning. */
5560 mips_cprestore_valid = 1;
5561 }
5562 if (mips_opts.noreorder)
5563 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5564 "nop", "");
5565 expr1.X_add_number = mips_cprestore_offset;
5566 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5567 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5568 mips_gp_register, mips_frame_reg);
5569 }
5570 }
5571 }
5572 else if (mips_pic == EMBEDDED_PIC)
5573 {
5574 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5575 /* The linker may expand the call to a longer sequence which
5576 uses $at, so we must break rather than return. */
5577 break;
5578 }
5579 else
5580 abort ();
5581
5582 return;
5583
5584 case M_LB_AB:
5585 s = "lb";
5586 goto ld;
5587 case M_LBU_AB:
5588 s = "lbu";
5589 goto ld;
5590 case M_LH_AB:
5591 s = "lh";
5592 goto ld;
5593 case M_LHU_AB:
5594 s = "lhu";
5595 goto ld;
5596 case M_LW_AB:
5597 s = "lw";
5598 goto ld;
5599 case M_LWC0_AB:
5600 s = "lwc0";
5601 /* Itbl support may require additional care here. */
5602 coproc = 1;
5603 goto ld;
5604 case M_LWC1_AB:
5605 s = "lwc1";
5606 /* Itbl support may require additional care here. */
5607 coproc = 1;
5608 goto ld;
5609 case M_LWC2_AB:
5610 s = "lwc2";
5611 /* Itbl support may require additional care here. */
5612 coproc = 1;
5613 goto ld;
5614 case M_LWC3_AB:
5615 s = "lwc3";
5616 /* Itbl support may require additional care here. */
5617 coproc = 1;
5618 goto ld;
5619 case M_LWL_AB:
5620 s = "lwl";
5621 lr = 1;
5622 goto ld;
5623 case M_LWR_AB:
5624 s = "lwr";
5625 lr = 1;
5626 goto ld;
5627 case M_LDC1_AB:
5628 if (mips_arch == CPU_R4650)
5629 {
5630 as_bad (_("opcode not supported on this processor"));
5631 return;
5632 }
5633 s = "ldc1";
5634 /* Itbl support may require additional care here. */
5635 coproc = 1;
5636 goto ld;
5637 case M_LDC2_AB:
5638 s = "ldc2";
5639 /* Itbl support may require additional care here. */
5640 coproc = 1;
5641 goto ld;
5642 case M_LDC3_AB:
5643 s = "ldc3";
5644 /* Itbl support may require additional care here. */
5645 coproc = 1;
5646 goto ld;
5647 case M_LDL_AB:
5648 s = "ldl";
5649 lr = 1;
5650 goto ld;
5651 case M_LDR_AB:
5652 s = "ldr";
5653 lr = 1;
5654 goto ld;
5655 case M_LL_AB:
5656 s = "ll";
5657 goto ld;
5658 case M_LLD_AB:
5659 s = "lld";
5660 goto ld;
5661 case M_LWU_AB:
5662 s = "lwu";
5663 ld:
5664 if (breg == treg || coproc || lr)
5665 {
5666 tempreg = AT;
5667 used_at = 1;
5668 }
5669 else
5670 {
5671 tempreg = treg;
5672 used_at = 0;
5673 }
5674 goto ld_st;
5675 case M_SB_AB:
5676 s = "sb";
5677 goto st;
5678 case M_SH_AB:
5679 s = "sh";
5680 goto st;
5681 case M_SW_AB:
5682 s = "sw";
5683 goto st;
5684 case M_SWC0_AB:
5685 s = "swc0";
5686 /* Itbl support may require additional care here. */
5687 coproc = 1;
5688 goto st;
5689 case M_SWC1_AB:
5690 s = "swc1";
5691 /* Itbl support may require additional care here. */
5692 coproc = 1;
5693 goto st;
5694 case M_SWC2_AB:
5695 s = "swc2";
5696 /* Itbl support may require additional care here. */
5697 coproc = 1;
5698 goto st;
5699 case M_SWC3_AB:
5700 s = "swc3";
5701 /* Itbl support may require additional care here. */
5702 coproc = 1;
5703 goto st;
5704 case M_SWL_AB:
5705 s = "swl";
5706 goto st;
5707 case M_SWR_AB:
5708 s = "swr";
5709 goto st;
5710 case M_SC_AB:
5711 s = "sc";
5712 goto st;
5713 case M_SCD_AB:
5714 s = "scd";
5715 goto st;
5716 case M_SDC1_AB:
5717 if (mips_arch == CPU_R4650)
5718 {
5719 as_bad (_("opcode not supported on this processor"));
5720 return;
5721 }
5722 s = "sdc1";
5723 coproc = 1;
5724 /* Itbl support may require additional care here. */
5725 goto st;
5726 case M_SDC2_AB:
5727 s = "sdc2";
5728 /* Itbl support may require additional care here. */
5729 coproc = 1;
5730 goto st;
5731 case M_SDC3_AB:
5732 s = "sdc3";
5733 /* Itbl support may require additional care here. */
5734 coproc = 1;
5735 goto st;
5736 case M_SDL_AB:
5737 s = "sdl";
5738 goto st;
5739 case M_SDR_AB:
5740 s = "sdr";
5741 st:
5742 tempreg = AT;
5743 used_at = 1;
5744 ld_st:
5745 /* Itbl support may require additional care here. */
5746 if (mask == M_LWC1_AB
5747 || mask == M_SWC1_AB
5748 || mask == M_LDC1_AB
5749 || mask == M_SDC1_AB
5750 || mask == M_L_DAB
5751 || mask == M_S_DAB)
5752 fmt = "T,o(b)";
5753 else if (coproc)
5754 fmt = "E,o(b)";
5755 else
5756 fmt = "t,o(b)";
5757
5758 /* For embedded PIC, we allow loads where the offset is calculated
5759 by subtracting a symbol in the current segment from an unknown
5760 symbol, relative to a base register, e.g.:
5761 <op> $treg, <sym>-<localsym>($breg)
5762 This is used by the compiler for switch statements. */
5763 if (mips_pic == EMBEDDED_PIC
5764 && offset_expr.X_op == O_subtract
5765 && (symbol_constant_p (offset_expr.X_op_symbol)
5766 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5767 : (symbol_equated_p (offset_expr.X_op_symbol)
5768 && (S_GET_SEGMENT
5769 (symbol_get_value_expression (offset_expr.X_op_symbol)
5770 ->X_add_symbol)
5771 == now_seg)))
5772 && breg != 0
5773 && (offset_expr.X_add_number == 0
5774 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5775 {
5776 /* For this case, we output the instructions:
5777 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5778 addiu $tempreg,$tempreg,$breg
5779 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5780 If the relocation would fit entirely in 16 bits, it would be
5781 nice to emit:
5782 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5783 instead, but that seems quite difficult. */
5784 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5785 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5786 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5787 ((bfd_arch_bits_per_address (stdoutput) == 32
5788 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5789 ? "addu" : "daddu"),
5790 "d,v,t", tempreg, tempreg, breg);
5791 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5792 (int) BFD_RELOC_PCREL_LO16, tempreg);
5793 if (! used_at)
5794 return;
5795 break;
5796 }
5797
5798 if (offset_expr.X_op != O_constant
5799 && offset_expr.X_op != O_symbol)
5800 {
5801 as_bad (_("expression too complex"));
5802 offset_expr.X_op = O_constant;
5803 }
5804
5805 /* A constant expression in PIC code can be handled just as it
5806 is in non PIC code. */
5807 if (mips_pic == NO_PIC
5808 || offset_expr.X_op == O_constant)
5809 {
5810 char *p;
5811
5812 /* If this is a reference to a GP relative symbol, and there
5813 is no base register, we want
5814 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5815 Otherwise, if there is no base register, we want
5816 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5817 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5818 If we have a constant, we need two instructions anyhow,
5819 so we always use the latter form.
5820
5821 If we have a base register, and this is a reference to a
5822 GP relative symbol, we want
5823 addu $tempreg,$breg,$gp
5824 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5825 Otherwise we want
5826 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5827 addu $tempreg,$tempreg,$breg
5828 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5829 With a constant we always use the latter case.
5830
5831 With 64bit address space and no base register and $at usable,
5832 we want
5833 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5834 lui $at,<sym> (BFD_RELOC_HI16_S)
5835 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5836 dsll32 $tempreg,0
5837 daddu $tempreg,$at
5838 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5839 If we have a base register, we want
5840 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5841 lui $at,<sym> (BFD_RELOC_HI16_S)
5842 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5843 daddu $at,$breg
5844 dsll32 $tempreg,0
5845 daddu $tempreg,$at
5846 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5847
5848 Without $at we can't generate the optimal path for superscalar
5849 processors here since this would require two temporary registers.
5850 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5851 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5852 dsll $tempreg,16
5853 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5854 dsll $tempreg,16
5855 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5856 If we have a base register, we want
5857 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5858 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5859 dsll $tempreg,16
5860 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5861 dsll $tempreg,16
5862 daddu $tempreg,$tempreg,$breg
5863 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5864
5865 If we have 64-bit addresses, as an optimization, for
5866 addresses which are 32-bit constants (e.g. kseg0/kseg1
5867 addresses) we fall back to the 32-bit address generation
5868 mechanism since it is more efficient. Note that due to
5869 the signed offset used by memory operations, the 32-bit
5870 range is shifted down by 32768 here. This code should
5871 probably attempt to generate 64-bit constants more
5872 efficiently in general.
5873 */
5874 if (HAVE_64BIT_ADDRESSES
5875 && !(offset_expr.X_op == O_constant
5876 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5877 {
5878 p = NULL;
5879
5880 /* We don't do GP optimization for now because RELAX_ENCODE can't
5881 hold the data for such large chunks. */
5882
5883 if (used_at == 0 && ! mips_opts.noat)
5884 {
5885 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5886 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5887 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5888 AT, (int) BFD_RELOC_HI16_S);
5889 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5890 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5891 if (breg != 0)
5892 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5893 "d,v,t", AT, AT, breg);
5894 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5895 "d,w,<", tempreg, tempreg, 0);
5896 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5897 "d,v,t", tempreg, tempreg, AT);
5898 macro_build (p, &icnt, &offset_expr, s,
5899 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5900 used_at = 1;
5901 }
5902 else
5903 {
5904 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5905 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5906 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5907 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5908 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5909 "d,w,<", tempreg, tempreg, 16);
5910 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5911 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5912 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5913 "d,w,<", tempreg, tempreg, 16);
5914 if (breg != 0)
5915 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5916 "d,v,t", tempreg, tempreg, breg);
5917 macro_build (p, &icnt, &offset_expr, s,
5918 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5919 }
5920
5921 return;
5922 }
5923
5924 if (breg == 0)
5925 {
5926 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5927 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5928 p = NULL;
5929 else
5930 {
5931 frag_grow (20);
5932 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5933 treg, (int) BFD_RELOC_GPREL16,
5934 mips_gp_register);
5935 p = frag_var (rs_machine_dependent, 8, 0,
5936 RELAX_ENCODE (4, 8, 0, 4, 0,
5937 (mips_opts.warn_about_macros
5938 || (used_at
5939 && mips_opts.noat))),
5940 offset_expr.X_add_symbol, 0, NULL);
5941 used_at = 0;
5942 }
5943 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5944 if (p != NULL)
5945 p += 4;
5946 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5947 (int) BFD_RELOC_LO16, tempreg);
5948 }
5949 else
5950 {
5951 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5952 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5953 p = NULL;
5954 else
5955 {
5956 frag_grow (28);
5957 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5958 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5959 "d,v,t", tempreg, breg, mips_gp_register);
5960 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5961 treg, (int) BFD_RELOC_GPREL16, tempreg);
5962 p = frag_var (rs_machine_dependent, 12, 0,
5963 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5964 offset_expr.X_add_symbol, 0, NULL);
5965 }
5966 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5967 if (p != NULL)
5968 p += 4;
5969 macro_build (p, &icnt, (expressionS *) NULL,
5970 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5971 "d,v,t", tempreg, tempreg, breg);
5972 if (p != NULL)
5973 p += 4;
5974 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5975 (int) BFD_RELOC_LO16, tempreg);
5976 }
5977 }
5978 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5979 {
5980 char *p;
5981 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5982
5983 /* If this is a reference to an external symbol, we want
5984 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5985 nop
5986 <op> $treg,0($tempreg)
5987 Otherwise we want
5988 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5989 nop
5990 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5991 <op> $treg,0($tempreg)
5992 If we have NewABI, we want
5993 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5994 If there is a base register, we add it to $tempreg before
5995 the <op>. If there is a constant, we stick it in the
5996 <op> instruction. We don't handle constants larger than
5997 16 bits, because we have no way to load the upper 16 bits
5998 (actually, we could handle them for the subset of cases
5999 in which we are not using $at). */
6000 assert (offset_expr.X_op == O_symbol);
6001 expr1.X_add_number = offset_expr.X_add_number;
6002 offset_expr.X_add_number = 0;
6003 if (HAVE_NEWABI)
6004 lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
6005 if (expr1.X_add_number < -0x8000
6006 || expr1.X_add_number >= 0x8000)
6007 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6008 frag_grow (20);
6009 macro_build ((char *) NULL, &icnt, &offset_expr,
6010 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
6011 (int) lw_reloc_type, mips_gp_register);
6012 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6013 p = frag_var (rs_machine_dependent, 4, 0,
6014 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
6015 offset_expr.X_add_symbol, 0, NULL);
6016 macro_build (p, &icnt, &offset_expr,
6017 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6018 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6019 if (breg != 0)
6020 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6021 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6022 "d,v,t", tempreg, tempreg, breg);
6023 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6024 (int) BFD_RELOC_LO16, tempreg);
6025 }
6026 else if (mips_pic == SVR4_PIC)
6027 {
6028 int gpdel;
6029 char *p;
6030
6031 /* If this is a reference to an external symbol, we want
6032 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6033 addu $tempreg,$tempreg,$gp
6034 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6035 <op> $treg,0($tempreg)
6036 Otherwise we want
6037 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6038 nop
6039 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6040 <op> $treg,0($tempreg)
6041 If there is a base register, we add it to $tempreg before
6042 the <op>. If there is a constant, we stick it in the
6043 <op> instruction. We don't handle constants larger than
6044 16 bits, because we have no way to load the upper 16 bits
6045 (actually, we could handle them for the subset of cases
6046 in which we are not using $at).
6047
6048 For NewABI, we want
6049 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6050 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
6051 <op> $treg,0($tempreg)
6052 */
6053 assert (offset_expr.X_op == O_symbol);
6054 expr1.X_add_number = offset_expr.X_add_number;
6055 offset_expr.X_add_number = 0;
6056 if (expr1.X_add_number < -0x8000
6057 || expr1.X_add_number >= 0x8000)
6058 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6059 if (HAVE_NEWABI)
6060 {
6061 macro_build ((char *) NULL, &icnt, &offset_expr,
6062 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6063 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6064 mips_gp_register);
6065 macro_build ((char *) NULL, &icnt, &offset_expr,
6066 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6067 "t,r,j", tempreg, tempreg,
6068 BFD_RELOC_MIPS_GOT_OFST);
6069 if (breg != 0)
6070 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6071 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6072 "d,v,t", tempreg, tempreg, breg);
6073 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6074 (int) BFD_RELOC_LO16, tempreg);
6075
6076 if (! used_at)
6077 return;
6078
6079 break;
6080 }
6081 if (reg_needs_delay (mips_gp_register))
6082 gpdel = 4;
6083 else
6084 gpdel = 0;
6085 frag_grow (36);
6086 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6087 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6088 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6089 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6090 "d,v,t", tempreg, tempreg, mips_gp_register);
6091 macro_build ((char *) NULL, &icnt, &offset_expr,
6092 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6093 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6094 tempreg);
6095 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6096 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6097 offset_expr.X_add_symbol, 0, NULL);
6098 if (gpdel > 0)
6099 {
6100 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6101 p += 4;
6102 }
6103 macro_build (p, &icnt, &offset_expr,
6104 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6105 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6106 mips_gp_register);
6107 p += 4;
6108 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6109 p += 4;
6110 macro_build (p, &icnt, &offset_expr,
6111 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6112 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6113 if (breg != 0)
6114 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6115 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6116 "d,v,t", tempreg, tempreg, breg);
6117 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6118 (int) BFD_RELOC_LO16, tempreg);
6119 }
6120 else if (mips_pic == EMBEDDED_PIC)
6121 {
6122 /* If there is no base register, we want
6123 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6124 If there is a base register, we want
6125 addu $tempreg,$breg,$gp
6126 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
6127 */
6128 assert (offset_expr.X_op == O_symbol);
6129 if (breg == 0)
6130 {
6131 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6132 treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6133 used_at = 0;
6134 }
6135 else
6136 {
6137 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6138 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6139 "d,v,t", tempreg, breg, mips_gp_register);
6140 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6141 treg, (int) BFD_RELOC_GPREL16, tempreg);
6142 }
6143 }
6144 else
6145 abort ();
6146
6147 if (! used_at)
6148 return;
6149
6150 break;
6151
6152 case M_LI:
6153 case M_LI_S:
6154 load_register (&icnt, treg, &imm_expr, 0);
6155 return;
6156
6157 case M_DLI:
6158 load_register (&icnt, treg, &imm_expr, 1);
6159 return;
6160
6161 case M_LI_SS:
6162 if (imm_expr.X_op == O_constant)
6163 {
6164 load_register (&icnt, AT, &imm_expr, 0);
6165 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6166 "mtc1", "t,G", AT, treg);
6167 break;
6168 }
6169 else
6170 {
6171 assert (offset_expr.X_op == O_symbol
6172 && strcmp (segment_name (S_GET_SEGMENT
6173 (offset_expr.X_add_symbol)),
6174 ".lit4") == 0
6175 && offset_expr.X_add_number == 0);
6176 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6177 treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6178 return;
6179 }
6180
6181 case M_LI_D:
6182 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6183 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6184 order 32 bits of the value and the low order 32 bits are either
6185 zero or in OFFSET_EXPR. */
6186 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6187 {
6188 if (HAVE_64BIT_GPRS)
6189 load_register (&icnt, treg, &imm_expr, 1);
6190 else
6191 {
6192 int hreg, lreg;
6193
6194 if (target_big_endian)
6195 {
6196 hreg = treg;
6197 lreg = treg + 1;
6198 }
6199 else
6200 {
6201 hreg = treg + 1;
6202 lreg = treg;
6203 }
6204
6205 if (hreg <= 31)
6206 load_register (&icnt, hreg, &imm_expr, 0);
6207 if (lreg <= 31)
6208 {
6209 if (offset_expr.X_op == O_absent)
6210 move_register (&icnt, lreg, 0);
6211 else
6212 {
6213 assert (offset_expr.X_op == O_constant);
6214 load_register (&icnt, lreg, &offset_expr, 0);
6215 }
6216 }
6217 }
6218 return;
6219 }
6220
6221 /* We know that sym is in the .rdata section. First we get the
6222 upper 16 bits of the address. */
6223 if (mips_pic == NO_PIC)
6224 {
6225 macro_build_lui (NULL, &icnt, &offset_expr, AT);
6226 }
6227 else if (mips_pic == SVR4_PIC)
6228 {
6229 macro_build ((char *) NULL, &icnt, &offset_expr,
6230 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6231 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6232 mips_gp_register);
6233 }
6234 else if (mips_pic == EMBEDDED_PIC)
6235 {
6236 /* For embedded PIC we pick up the entire address off $gp in
6237 a single instruction. */
6238 macro_build ((char *) NULL, &icnt, &offset_expr,
6239 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6240 mips_gp_register, (int) BFD_RELOC_GPREL16);
6241 offset_expr.X_op = O_constant;
6242 offset_expr.X_add_number = 0;
6243 }
6244 else
6245 abort ();
6246
6247 /* Now we load the register(s). */
6248 if (HAVE_64BIT_GPRS)
6249 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6250 treg, (int) BFD_RELOC_LO16, AT);
6251 else
6252 {
6253 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6254 treg, (int) BFD_RELOC_LO16, AT);
6255 if (treg != RA)
6256 {
6257 /* FIXME: How in the world do we deal with the possible
6258 overflow here? */
6259 offset_expr.X_add_number += 4;
6260 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6261 treg + 1, (int) BFD_RELOC_LO16, AT);
6262 }
6263 }
6264
6265 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6266 does not become a variant frag. */
6267 frag_wane (frag_now);
6268 frag_new (0);
6269
6270 break;
6271
6272 case M_LI_DD:
6273 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6274 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6275 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6276 the value and the low order 32 bits are either zero or in
6277 OFFSET_EXPR. */
6278 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6279 {
6280 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6281 if (HAVE_64BIT_FPRS)
6282 {
6283 assert (HAVE_64BIT_GPRS);
6284 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6285 "dmtc1", "t,S", AT, treg);
6286 }
6287 else
6288 {
6289 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6290 "mtc1", "t,G", AT, treg + 1);
6291 if (offset_expr.X_op == O_absent)
6292 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6293 "mtc1", "t,G", 0, treg);
6294 else
6295 {
6296 assert (offset_expr.X_op == O_constant);
6297 load_register (&icnt, AT, &offset_expr, 0);
6298 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6299 "mtc1", "t,G", AT, treg);
6300 }
6301 }
6302 break;
6303 }
6304
6305 assert (offset_expr.X_op == O_symbol
6306 && offset_expr.X_add_number == 0);
6307 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6308 if (strcmp (s, ".lit8") == 0)
6309 {
6310 if (mips_opts.isa != ISA_MIPS1)
6311 {
6312 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6313 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6314 mips_gp_register);
6315 return;
6316 }
6317 breg = mips_gp_register;
6318 r = BFD_RELOC_MIPS_LITERAL;
6319 goto dob;
6320 }
6321 else
6322 {
6323 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6324 if (mips_pic == SVR4_PIC)
6325 macro_build ((char *) NULL, &icnt, &offset_expr,
6326 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6327 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6328 mips_gp_register);
6329 else
6330 {
6331 /* FIXME: This won't work for a 64 bit address. */
6332 macro_build_lui (NULL, &icnt, &offset_expr, AT);
6333 }
6334
6335 if (mips_opts.isa != ISA_MIPS1)
6336 {
6337 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6338 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6339
6340 /* To avoid confusion in tc_gen_reloc, we must ensure
6341 that this does not become a variant frag. */
6342 frag_wane (frag_now);
6343 frag_new (0);
6344
6345 break;
6346 }
6347 breg = AT;
6348 r = BFD_RELOC_LO16;
6349 goto dob;
6350 }
6351
6352 case M_L_DOB:
6353 if (mips_arch == CPU_R4650)
6354 {
6355 as_bad (_("opcode not supported on this processor"));
6356 return;
6357 }
6358 /* Even on a big endian machine $fn comes before $fn+1. We have
6359 to adjust when loading from memory. */
6360 r = BFD_RELOC_LO16;
6361 dob:
6362 assert (mips_opts.isa == ISA_MIPS1);
6363 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6364 target_big_endian ? treg + 1 : treg,
6365 (int) r, breg);
6366 /* FIXME: A possible overflow which I don't know how to deal
6367 with. */
6368 offset_expr.X_add_number += 4;
6369 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6370 target_big_endian ? treg : treg + 1,
6371 (int) r, breg);
6372
6373 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6374 does not become a variant frag. */
6375 frag_wane (frag_now);
6376 frag_new (0);
6377
6378 if (breg != AT)
6379 return;
6380 break;
6381
6382 case M_L_DAB:
6383 /*
6384 * The MIPS assembler seems to check for X_add_number not
6385 * being double aligned and generating:
6386 * lui at,%hi(foo+1)
6387 * addu at,at,v1
6388 * addiu at,at,%lo(foo+1)
6389 * lwc1 f2,0(at)
6390 * lwc1 f3,4(at)
6391 * But, the resulting address is the same after relocation so why
6392 * generate the extra instruction?
6393 */
6394 if (mips_arch == CPU_R4650)
6395 {
6396 as_bad (_("opcode not supported on this processor"));
6397 return;
6398 }
6399 /* Itbl support may require additional care here. */
6400 coproc = 1;
6401 if (mips_opts.isa != ISA_MIPS1)
6402 {
6403 s = "ldc1";
6404 goto ld;
6405 }
6406
6407 s = "lwc1";
6408 fmt = "T,o(b)";
6409 goto ldd_std;
6410
6411 case M_S_DAB:
6412 if (mips_arch == CPU_R4650)
6413 {
6414 as_bad (_("opcode not supported on this processor"));
6415 return;
6416 }
6417
6418 if (mips_opts.isa != ISA_MIPS1)
6419 {
6420 s = "sdc1";
6421 goto st;
6422 }
6423
6424 s = "swc1";
6425 fmt = "T,o(b)";
6426 /* Itbl support may require additional care here. */
6427 coproc = 1;
6428 goto ldd_std;
6429
6430 case M_LD_AB:
6431 if (HAVE_64BIT_GPRS)
6432 {
6433 s = "ld";
6434 goto ld;
6435 }
6436
6437 s = "lw";
6438 fmt = "t,o(b)";
6439 goto ldd_std;
6440
6441 case M_SD_AB:
6442 if (HAVE_64BIT_GPRS)
6443 {
6444 s = "sd";
6445 goto st;
6446 }
6447
6448 s = "sw";
6449 fmt = "t,o(b)";
6450
6451 ldd_std:
6452 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6453 loads for the case of doing a pair of loads to simulate an 'ld'.
6454 This is not currently done by the compiler, and assembly coders
6455 writing embedded-pic code can cope. */
6456
6457 if (offset_expr.X_op != O_symbol
6458 && offset_expr.X_op != O_constant)
6459 {
6460 as_bad (_("expression too complex"));
6461 offset_expr.X_op = O_constant;
6462 }
6463
6464 /* Even on a big endian machine $fn comes before $fn+1. We have
6465 to adjust when loading from memory. We set coproc if we must
6466 load $fn+1 first. */
6467 /* Itbl support may require additional care here. */
6468 if (! target_big_endian)
6469 coproc = 0;
6470
6471 if (mips_pic == NO_PIC
6472 || offset_expr.X_op == O_constant)
6473 {
6474 char *p;
6475
6476 /* If this is a reference to a GP relative symbol, we want
6477 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6478 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6479 If we have a base register, we use this
6480 addu $at,$breg,$gp
6481 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6482 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6483 If this is not a GP relative symbol, we want
6484 lui $at,<sym> (BFD_RELOC_HI16_S)
6485 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6486 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6487 If there is a base register, we add it to $at after the
6488 lui instruction. If there is a constant, we always use
6489 the last case. */
6490 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6491 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6492 {
6493 p = NULL;
6494 used_at = 1;
6495 }
6496 else
6497 {
6498 int off;
6499
6500 if (breg == 0)
6501 {
6502 frag_grow (28);
6503 tempreg = mips_gp_register;
6504 off = 0;
6505 used_at = 0;
6506 }
6507 else
6508 {
6509 frag_grow (36);
6510 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6511 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6512 "d,v,t", AT, breg, mips_gp_register);
6513 tempreg = AT;
6514 off = 4;
6515 used_at = 1;
6516 }
6517
6518 /* Itbl support may require additional care here. */
6519 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6520 coproc ? treg + 1 : treg,
6521 (int) BFD_RELOC_GPREL16, tempreg);
6522 offset_expr.X_add_number += 4;
6523
6524 /* Set mips_optimize to 2 to avoid inserting an
6525 undesired nop. */
6526 hold_mips_optimize = mips_optimize;
6527 mips_optimize = 2;
6528 /* Itbl support may require additional care here. */
6529 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6530 coproc ? treg : treg + 1,
6531 (int) BFD_RELOC_GPREL16, tempreg);
6532 mips_optimize = hold_mips_optimize;
6533
6534 p = frag_var (rs_machine_dependent, 12 + off, 0,
6535 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6536 used_at && mips_opts.noat),
6537 offset_expr.X_add_symbol, 0, NULL);
6538
6539 /* We just generated two relocs. When tc_gen_reloc
6540 handles this case, it will skip the first reloc and
6541 handle the second. The second reloc already has an
6542 extra addend of 4, which we added above. We must
6543 subtract it out, and then subtract another 4 to make
6544 the first reloc come out right. The second reloc
6545 will come out right because we are going to add 4 to
6546 offset_expr when we build its instruction below.
6547
6548 If we have a symbol, then we don't want to include
6549 the offset, because it will wind up being included
6550 when we generate the reloc. */
6551
6552 if (offset_expr.X_op == O_constant)
6553 offset_expr.X_add_number -= 8;
6554 else
6555 {
6556 offset_expr.X_add_number = -4;
6557 offset_expr.X_op = O_constant;
6558 }
6559 }
6560 macro_build_lui (p, &icnt, &offset_expr, AT);
6561 if (p != NULL)
6562 p += 4;
6563 if (breg != 0)
6564 {
6565 macro_build (p, &icnt, (expressionS *) NULL,
6566 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6567 "d,v,t", AT, breg, AT);
6568 if (p != NULL)
6569 p += 4;
6570 }
6571 /* Itbl support may require additional care here. */
6572 macro_build (p, &icnt, &offset_expr, s, fmt,
6573 coproc ? treg + 1 : treg,
6574 (int) BFD_RELOC_LO16, AT);
6575 if (p != NULL)
6576 p += 4;
6577 /* FIXME: How do we handle overflow here? */
6578 offset_expr.X_add_number += 4;
6579 /* Itbl support may require additional care here. */
6580 macro_build (p, &icnt, &offset_expr, s, fmt,
6581 coproc ? treg : treg + 1,
6582 (int) BFD_RELOC_LO16, AT);
6583 }
6584 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6585 {
6586 int off;
6587
6588 /* If this is a reference to an external symbol, we want
6589 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6590 nop
6591 <op> $treg,0($at)
6592 <op> $treg+1,4($at)
6593 Otherwise we want
6594 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6595 nop
6596 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6597 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6598 If there is a base register we add it to $at before the
6599 lwc1 instructions. If there is a constant we include it
6600 in the lwc1 instructions. */
6601 used_at = 1;
6602 expr1.X_add_number = offset_expr.X_add_number;
6603 offset_expr.X_add_number = 0;
6604 if (expr1.X_add_number < -0x8000
6605 || expr1.X_add_number >= 0x8000 - 4)
6606 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6607 if (breg == 0)
6608 off = 0;
6609 else
6610 off = 4;
6611 frag_grow (24 + off);
6612 macro_build ((char *) NULL, &icnt, &offset_expr,
6613 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6614 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6615 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6616 if (breg != 0)
6617 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6618 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6619 "d,v,t", AT, breg, AT);
6620 /* Itbl support may require additional care here. */
6621 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6622 coproc ? treg + 1 : treg,
6623 (int) BFD_RELOC_LO16, AT);
6624 expr1.X_add_number += 4;
6625
6626 /* Set mips_optimize to 2 to avoid inserting an undesired
6627 nop. */
6628 hold_mips_optimize = mips_optimize;
6629 mips_optimize = 2;
6630 /* Itbl support may require additional care here. */
6631 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6632 coproc ? treg : treg + 1,
6633 (int) BFD_RELOC_LO16, AT);
6634 mips_optimize = hold_mips_optimize;
6635
6636 (void) frag_var (rs_machine_dependent, 0, 0,
6637 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6638 offset_expr.X_add_symbol, 0, NULL);
6639 }
6640 else if (mips_pic == SVR4_PIC)
6641 {
6642 int gpdel, off;
6643 char *p;
6644
6645 /* If this is a reference to an external symbol, we want
6646 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6647 addu $at,$at,$gp
6648 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6649 nop
6650 <op> $treg,0($at)
6651 <op> $treg+1,4($at)
6652 Otherwise we want
6653 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6654 nop
6655 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6656 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6657 If there is a base register we add it to $at before the
6658 lwc1 instructions. If there is a constant we include it
6659 in the lwc1 instructions. */
6660 used_at = 1;
6661 expr1.X_add_number = offset_expr.X_add_number;
6662 offset_expr.X_add_number = 0;
6663 if (expr1.X_add_number < -0x8000
6664 || expr1.X_add_number >= 0x8000 - 4)
6665 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6666 if (reg_needs_delay (mips_gp_register))
6667 gpdel = 4;
6668 else
6669 gpdel = 0;
6670 if (breg == 0)
6671 off = 0;
6672 else
6673 off = 4;
6674 frag_grow (56);
6675 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6676 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6677 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6678 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6679 "d,v,t", AT, AT, mips_gp_register);
6680 macro_build ((char *) NULL, &icnt, &offset_expr,
6681 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6682 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6683 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6684 if (breg != 0)
6685 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6686 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6687 "d,v,t", AT, breg, AT);
6688 /* Itbl support may require additional care here. */
6689 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6690 coproc ? treg + 1 : treg,
6691 (int) BFD_RELOC_LO16, AT);
6692 expr1.X_add_number += 4;
6693
6694 /* Set mips_optimize to 2 to avoid inserting an undesired
6695 nop. */
6696 hold_mips_optimize = mips_optimize;
6697 mips_optimize = 2;
6698 /* Itbl support may require additional care here. */
6699 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6700 coproc ? treg : treg + 1,
6701 (int) BFD_RELOC_LO16, AT);
6702 mips_optimize = hold_mips_optimize;
6703 expr1.X_add_number -= 4;
6704
6705 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6706 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6707 8 + gpdel + off, 1, 0),
6708 offset_expr.X_add_symbol, 0, NULL);
6709 if (gpdel > 0)
6710 {
6711 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6712 p += 4;
6713 }
6714 macro_build (p, &icnt, &offset_expr,
6715 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6716 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6717 mips_gp_register);
6718 p += 4;
6719 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6720 p += 4;
6721 if (breg != 0)
6722 {
6723 macro_build (p, &icnt, (expressionS *) NULL,
6724 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6725 "d,v,t", AT, breg, AT);
6726 p += 4;
6727 }
6728 /* Itbl support may require additional care here. */
6729 macro_build (p, &icnt, &expr1, s, fmt,
6730 coproc ? treg + 1 : treg,
6731 (int) BFD_RELOC_LO16, AT);
6732 p += 4;
6733 expr1.X_add_number += 4;
6734
6735 /* Set mips_optimize to 2 to avoid inserting an undesired
6736 nop. */
6737 hold_mips_optimize = mips_optimize;
6738 mips_optimize = 2;
6739 /* Itbl support may require additional care here. */
6740 macro_build (p, &icnt, &expr1, s, fmt,
6741 coproc ? treg : treg + 1,
6742 (int) BFD_RELOC_LO16, AT);
6743 mips_optimize = hold_mips_optimize;
6744 }
6745 else if (mips_pic == EMBEDDED_PIC)
6746 {
6747 /* If there is no base register, we use
6748 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6749 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6750 If we have a base register, we use
6751 addu $at,$breg,$gp
6752 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6753 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6754 */
6755 if (breg == 0)
6756 {
6757 tempreg = mips_gp_register;
6758 used_at = 0;
6759 }
6760 else
6761 {
6762 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6763 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6764 "d,v,t", AT, breg, mips_gp_register);
6765 tempreg = AT;
6766 used_at = 1;
6767 }
6768
6769 /* Itbl support may require additional care here. */
6770 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6771 coproc ? treg + 1 : treg,
6772 (int) BFD_RELOC_GPREL16, tempreg);
6773 offset_expr.X_add_number += 4;
6774 /* Itbl support may require additional care here. */
6775 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6776 coproc ? treg : treg + 1,
6777 (int) BFD_RELOC_GPREL16, tempreg);
6778 }
6779 else
6780 abort ();
6781
6782 if (! used_at)
6783 return;
6784
6785 break;
6786
6787 case M_LD_OB:
6788 s = "lw";
6789 goto sd_ob;
6790 case M_SD_OB:
6791 s = "sw";
6792 sd_ob:
6793 assert (HAVE_32BIT_ADDRESSES);
6794 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6795 (int) BFD_RELOC_LO16, breg);
6796 offset_expr.X_add_number += 4;
6797 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6798 (int) BFD_RELOC_LO16, breg);
6799 return;
6800
6801 /* New code added to support COPZ instructions.
6802 This code builds table entries out of the macros in mip_opcodes.
6803 R4000 uses interlocks to handle coproc delays.
6804 Other chips (like the R3000) require nops to be inserted for delays.
6805
6806 FIXME: Currently, we require that the user handle delays.
6807 In order to fill delay slots for non-interlocked chips,
6808 we must have a way to specify delays based on the coprocessor.
6809 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6810 What are the side-effects of the cop instruction?
6811 What cache support might we have and what are its effects?
6812 Both coprocessor & memory require delays. how long???
6813 What registers are read/set/modified?
6814
6815 If an itbl is provided to interpret cop instructions,
6816 this knowledge can be encoded in the itbl spec. */
6817
6818 case M_COP0:
6819 s = "c0";
6820 goto copz;
6821 case M_COP1:
6822 s = "c1";
6823 goto copz;
6824 case M_COP2:
6825 s = "c2";
6826 goto copz;
6827 case M_COP3:
6828 s = "c3";
6829 copz:
6830 /* For now we just do C (same as Cz). The parameter will be
6831 stored in insn_opcode by mips_ip. */
6832 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6833 ip->insn_opcode);
6834 return;
6835
6836 case M_MOVE:
6837 move_register (&icnt, dreg, sreg);
6838 return;
6839
6840 #ifdef LOSING_COMPILER
6841 default:
6842 /* Try and see if this is a new itbl instruction.
6843 This code builds table entries out of the macros in mip_opcodes.
6844 FIXME: For now we just assemble the expression and pass it's
6845 value along as a 32-bit immediate.
6846 We may want to have the assembler assemble this value,
6847 so that we gain the assembler's knowledge of delay slots,
6848 symbols, etc.
6849 Would it be more efficient to use mask (id) here? */
6850 if (itbl_have_entries
6851 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6852 {
6853 s = ip->insn_mo->name;
6854 s2 = "cop3";
6855 coproc = ITBL_DECODE_PNUM (immed_expr);;
6856 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6857 return;
6858 }
6859 macro2 (ip);
6860 return;
6861 }
6862 if (mips_opts.noat)
6863 as_warn (_("Macro used $at after \".set noat\""));
6864 }
6865
6866 static void
6867 macro2 (ip)
6868 struct mips_cl_insn *ip;
6869 {
6870 register int treg, sreg, dreg, breg;
6871 int tempreg;
6872 int mask;
6873 int icnt = 0;
6874 int used_at;
6875 expressionS expr1;
6876 const char *s;
6877 const char *s2;
6878 const char *fmt;
6879 int likely = 0;
6880 int dbl = 0;
6881 int coproc = 0;
6882 int lr = 0;
6883 int imm = 0;
6884 int off;
6885 offsetT maxnum;
6886 bfd_reloc_code_real_type r;
6887 char *p;
6888
6889 treg = (ip->insn_opcode >> 16) & 0x1f;
6890 dreg = (ip->insn_opcode >> 11) & 0x1f;
6891 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6892 mask = ip->insn_mo->mask;
6893
6894 expr1.X_op = O_constant;
6895 expr1.X_op_symbol = NULL;
6896 expr1.X_add_symbol = NULL;
6897 expr1.X_add_number = 1;
6898
6899 switch (mask)
6900 {
6901 #endif /* LOSING_COMPILER */
6902
6903 case M_DMUL:
6904 dbl = 1;
6905 case M_MUL:
6906 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6907 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6908 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6909 dreg);
6910 return;
6911
6912 case M_DMUL_I:
6913 dbl = 1;
6914 case M_MUL_I:
6915 /* The MIPS assembler some times generates shifts and adds. I'm
6916 not trying to be that fancy. GCC should do this for us
6917 anyway. */
6918 load_register (&icnt, AT, &imm_expr, dbl);
6919 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6920 dbl ? "dmult" : "mult", "s,t", sreg, AT);
6921 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6922 dreg);
6923 break;
6924
6925 case M_DMULO_I:
6926 dbl = 1;
6927 case M_MULO_I:
6928 imm = 1;
6929 goto do_mulo;
6930
6931 case M_DMULO:
6932 dbl = 1;
6933 case M_MULO:
6934 do_mulo:
6935 mips_emit_delays (TRUE);
6936 ++mips_opts.noreorder;
6937 mips_any_noreorder = 1;
6938 if (imm)
6939 load_register (&icnt, AT, &imm_expr, dbl);
6940 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6941 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6942 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6943 dreg);
6944 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6945 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6946 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6947 AT);
6948 if (mips_trap)
6949 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6950 "s,t,q", dreg, AT, 6);
6951 else
6952 {
6953 expr1.X_add_number = 8;
6954 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6955 AT);
6956 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6957 0);
6958 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6959 "c", 6);
6960 }
6961 --mips_opts.noreorder;
6962 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6963 break;
6964
6965 case M_DMULOU_I:
6966 dbl = 1;
6967 case M_MULOU_I:
6968 imm = 1;
6969 goto do_mulou;
6970
6971 case M_DMULOU:
6972 dbl = 1;
6973 case M_MULOU:
6974 do_mulou:
6975 mips_emit_delays (TRUE);
6976 ++mips_opts.noreorder;
6977 mips_any_noreorder = 1;
6978 if (imm)
6979 load_register (&icnt, AT, &imm_expr, dbl);
6980 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6981 dbl ? "dmultu" : "multu",
6982 "s,t", sreg, imm ? AT : treg);
6983 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6984 AT);
6985 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6986 dreg);
6987 if (mips_trap)
6988 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6989 "s,t,q", AT, 0, 6);
6990 else
6991 {
6992 expr1.X_add_number = 8;
6993 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6994 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6995 0);
6996 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6997 "c", 6);
6998 }
6999 --mips_opts.noreorder;
7000 break;
7001
7002 case M_DROL:
7003 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7004 {
7005 if (dreg == sreg)
7006 {
7007 tempreg = AT;
7008 used_at = 1;
7009 }
7010 else
7011 {
7012 tempreg = dreg;
7013 used_at = 0;
7014 }
7015 macro_build ((char *) NULL, &icnt, NULL, "dnegu",
7016 "d,w", tempreg, treg);
7017 macro_build ((char *) NULL, &icnt, NULL, "drorv",
7018 "d,t,s", dreg, sreg, tempreg);
7019 if (used_at)
7020 break;
7021 return;
7022 }
7023 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7024 "d,v,t", AT, 0, treg);
7025 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7026 "d,t,s", AT, sreg, AT);
7027 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7028 "d,t,s", dreg, sreg, treg);
7029 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7030 "d,v,t", dreg, dreg, AT);
7031 break;
7032
7033 case M_ROL:
7034 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7035 {
7036 if (dreg == sreg)
7037 {
7038 tempreg = AT;
7039 used_at = 1;
7040 }
7041 else
7042 {
7043 tempreg = dreg;
7044 used_at = 0;
7045 }
7046 macro_build ((char *) NULL, &icnt, NULL, "negu",
7047 "d,w", tempreg, treg);
7048 macro_build ((char *) NULL, &icnt, NULL, "rorv",
7049 "d,t,s", dreg, sreg, tempreg);
7050 if (used_at)
7051 break;
7052 return;
7053 }
7054 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7055 "d,v,t", AT, 0, treg);
7056 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7057 "d,t,s", AT, sreg, AT);
7058 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7059 "d,t,s", dreg, sreg, treg);
7060 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7061 "d,v,t", dreg, dreg, AT);
7062 break;
7063
7064 case M_DROL_I:
7065 {
7066 unsigned int rot;
7067 char *l, *r;
7068
7069 if (imm_expr.X_op != O_constant)
7070 as_bad (_("Improper rotate count"));
7071 rot = imm_expr.X_add_number & 0x3f;
7072 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7073 {
7074 rot = (64 - rot) & 0x3f;
7075 if (rot >= 32)
7076 macro_build ((char *) NULL, &icnt, NULL, "dror32",
7077 "d,w,<", dreg, sreg, rot - 32);
7078 else
7079 macro_build ((char *) NULL, &icnt, NULL, "dror",
7080 "d,w,<", dreg, sreg, rot);
7081 return;
7082 }
7083 if (rot == 0)
7084 {
7085 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7086 "d,w,<", dreg, sreg, 0);
7087 return;
7088 }
7089 l = (rot < 0x20) ? "dsll" : "dsll32";
7090 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7091 rot &= 0x1f;
7092 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7093 "d,w,<", AT, sreg, rot);
7094 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7095 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7096 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7097 "d,v,t", dreg, dreg, AT);
7098 }
7099 break;
7100
7101 case M_ROL_I:
7102 {
7103 unsigned int rot;
7104
7105 if (imm_expr.X_op != O_constant)
7106 as_bad (_("Improper rotate count"));
7107 rot = imm_expr.X_add_number & 0x1f;
7108 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7109 {
7110 macro_build ((char *) NULL, &icnt, NULL, "ror",
7111 "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7112 return;
7113 }
7114 if (rot == 0)
7115 {
7116 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7117 "d,w,<", dreg, sreg, 0);
7118 return;
7119 }
7120 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7121 "d,w,<", AT, sreg, rot);
7122 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7123 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7124 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7125 "d,v,t", dreg, dreg, AT);
7126 }
7127 break;
7128
7129 case M_DROR:
7130 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7131 {
7132 macro_build ((char *) NULL, &icnt, NULL, "drorv",
7133 "d,t,s", dreg, sreg, treg);
7134 return;
7135 }
7136 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7137 "d,v,t", AT, 0, treg);
7138 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7139 "d,t,s", AT, sreg, AT);
7140 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7141 "d,t,s", dreg, sreg, treg);
7142 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7143 "d,v,t", dreg, dreg, AT);
7144 break;
7145
7146 case M_ROR:
7147 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7148 {
7149 macro_build ((char *) NULL, &icnt, NULL, "rorv",
7150 "d,t,s", dreg, sreg, treg);
7151 return;
7152 }
7153 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7154 "d,v,t", AT, 0, treg);
7155 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7156 "d,t,s", AT, sreg, AT);
7157 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7158 "d,t,s", dreg, sreg, treg);
7159 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7160 "d,v,t", dreg, dreg, AT);
7161 break;
7162
7163 case M_DROR_I:
7164 {
7165 unsigned int rot;
7166 char *l, *r;
7167
7168 if (imm_expr.X_op != O_constant)
7169 as_bad (_("Improper rotate count"));
7170 rot = imm_expr.X_add_number & 0x3f;
7171 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7172 {
7173 if (rot >= 32)
7174 macro_build ((char *) NULL, &icnt, NULL, "dror32",
7175 "d,w,<", dreg, sreg, rot - 32);
7176 else
7177 macro_build ((char *) NULL, &icnt, NULL, "dror",
7178 "d,w,<", dreg, sreg, rot);
7179 return;
7180 }
7181 if (rot == 0)
7182 {
7183 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7184 "d,w,<", dreg, sreg, 0);
7185 return;
7186 }
7187 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7188 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7189 rot &= 0x1f;
7190 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7191 "d,w,<", AT, sreg, rot);
7192 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7193 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7194 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7195 "d,v,t", dreg, dreg, AT);
7196 }
7197 break;
7198
7199 case M_ROR_I:
7200 {
7201 unsigned int rot;
7202
7203 if (imm_expr.X_op != O_constant)
7204 as_bad (_("Improper rotate count"));
7205 rot = imm_expr.X_add_number & 0x1f;
7206 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7207 {
7208 macro_build ((char *) NULL, &icnt, NULL, "ror",
7209 "d,w,<", dreg, sreg, rot);
7210 return;
7211 }
7212 if (rot == 0)
7213 {
7214 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7215 "d,w,<", dreg, sreg, 0);
7216 return;
7217 }
7218 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7219 "d,w,<", AT, sreg, rot);
7220 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7221 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7222 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7223 "d,v,t", dreg, dreg, AT);
7224 }
7225 break;
7226
7227 case M_S_DOB:
7228 if (mips_arch == CPU_R4650)
7229 {
7230 as_bad (_("opcode not supported on this processor"));
7231 return;
7232 }
7233 assert (mips_opts.isa == ISA_MIPS1);
7234 /* Even on a big endian machine $fn comes before $fn+1. We have
7235 to adjust when storing to memory. */
7236 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7237 target_big_endian ? treg + 1 : treg,
7238 (int) BFD_RELOC_LO16, breg);
7239 offset_expr.X_add_number += 4;
7240 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7241 target_big_endian ? treg : treg + 1,
7242 (int) BFD_RELOC_LO16, breg);
7243 return;
7244
7245 case M_SEQ:
7246 if (sreg == 0)
7247 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7248 treg, (int) BFD_RELOC_LO16);
7249 else if (treg == 0)
7250 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7251 sreg, (int) BFD_RELOC_LO16);
7252 else
7253 {
7254 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7255 "d,v,t", dreg, sreg, treg);
7256 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7257 dreg, (int) BFD_RELOC_LO16);
7258 }
7259 return;
7260
7261 case M_SEQ_I:
7262 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7263 {
7264 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7265 sreg, (int) BFD_RELOC_LO16);
7266 return;
7267 }
7268 if (sreg == 0)
7269 {
7270 as_warn (_("Instruction %s: result is always false"),
7271 ip->insn_mo->name);
7272 move_register (&icnt, dreg, 0);
7273 return;
7274 }
7275 if (imm_expr.X_op == O_constant
7276 && imm_expr.X_add_number >= 0
7277 && imm_expr.X_add_number < 0x10000)
7278 {
7279 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7280 sreg, (int) BFD_RELOC_LO16);
7281 used_at = 0;
7282 }
7283 else if (imm_expr.X_op == O_constant
7284 && imm_expr.X_add_number > -0x8000
7285 && imm_expr.X_add_number < 0)
7286 {
7287 imm_expr.X_add_number = -imm_expr.X_add_number;
7288 macro_build ((char *) NULL, &icnt, &imm_expr,
7289 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7290 "t,r,j", dreg, sreg,
7291 (int) BFD_RELOC_LO16);
7292 used_at = 0;
7293 }
7294 else
7295 {
7296 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7297 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7298 "d,v,t", dreg, sreg, AT);
7299 used_at = 1;
7300 }
7301 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7302 (int) BFD_RELOC_LO16);
7303 if (used_at)
7304 break;
7305 return;
7306
7307 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7308 s = "slt";
7309 goto sge;
7310 case M_SGEU:
7311 s = "sltu";
7312 sge:
7313 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7314 dreg, sreg, treg);
7315 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7316 (int) BFD_RELOC_LO16);
7317 return;
7318
7319 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7320 case M_SGEU_I:
7321 if (imm_expr.X_op == O_constant
7322 && imm_expr.X_add_number >= -0x8000
7323 && imm_expr.X_add_number < 0x8000)
7324 {
7325 macro_build ((char *) NULL, &icnt, &imm_expr,
7326 mask == M_SGE_I ? "slti" : "sltiu",
7327 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7328 used_at = 0;
7329 }
7330 else
7331 {
7332 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7333 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7334 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7335 AT);
7336 used_at = 1;
7337 }
7338 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7339 (int) BFD_RELOC_LO16);
7340 if (used_at)
7341 break;
7342 return;
7343
7344 case M_SGT: /* sreg > treg <==> treg < sreg */
7345 s = "slt";
7346 goto sgt;
7347 case M_SGTU:
7348 s = "sltu";
7349 sgt:
7350 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7351 dreg, treg, sreg);
7352 return;
7353
7354 case M_SGT_I: /* sreg > I <==> I < sreg */
7355 s = "slt";
7356 goto sgti;
7357 case M_SGTU_I:
7358 s = "sltu";
7359 sgti:
7360 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7361 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7362 dreg, AT, sreg);
7363 break;
7364
7365 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7366 s = "slt";
7367 goto sle;
7368 case M_SLEU:
7369 s = "sltu";
7370 sle:
7371 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7372 dreg, treg, sreg);
7373 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7374 (int) BFD_RELOC_LO16);
7375 return;
7376
7377 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7378 s = "slt";
7379 goto slei;
7380 case M_SLEU_I:
7381 s = "sltu";
7382 slei:
7383 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7384 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7385 dreg, AT, sreg);
7386 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7387 (int) BFD_RELOC_LO16);
7388 break;
7389
7390 case M_SLT_I:
7391 if (imm_expr.X_op == O_constant
7392 && imm_expr.X_add_number >= -0x8000
7393 && imm_expr.X_add_number < 0x8000)
7394 {
7395 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7396 dreg, sreg, (int) BFD_RELOC_LO16);
7397 return;
7398 }
7399 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7400 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7401 dreg, sreg, AT);
7402 break;
7403
7404 case M_SLTU_I:
7405 if (imm_expr.X_op == O_constant
7406 && imm_expr.X_add_number >= -0x8000
7407 && imm_expr.X_add_number < 0x8000)
7408 {
7409 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7410 dreg, sreg, (int) BFD_RELOC_LO16);
7411 return;
7412 }
7413 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7414 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7415 "d,v,t", dreg, sreg, AT);
7416 break;
7417
7418 case M_SNE:
7419 if (sreg == 0)
7420 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7421 "d,v,t", dreg, 0, treg);
7422 else if (treg == 0)
7423 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7424 "d,v,t", dreg, 0, sreg);
7425 else
7426 {
7427 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7428 "d,v,t", dreg, sreg, treg);
7429 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7430 "d,v,t", dreg, 0, dreg);
7431 }
7432 return;
7433
7434 case M_SNE_I:
7435 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7436 {
7437 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7438 "d,v,t", dreg, 0, sreg);
7439 return;
7440 }
7441 if (sreg == 0)
7442 {
7443 as_warn (_("Instruction %s: result is always true"),
7444 ip->insn_mo->name);
7445 macro_build ((char *) NULL, &icnt, &expr1,
7446 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7447 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7448 return;
7449 }
7450 if (imm_expr.X_op == O_constant
7451 && imm_expr.X_add_number >= 0
7452 && imm_expr.X_add_number < 0x10000)
7453 {
7454 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7455 dreg, sreg, (int) BFD_RELOC_LO16);
7456 used_at = 0;
7457 }
7458 else if (imm_expr.X_op == O_constant
7459 && imm_expr.X_add_number > -0x8000
7460 && imm_expr.X_add_number < 0)
7461 {
7462 imm_expr.X_add_number = -imm_expr.X_add_number;
7463 macro_build ((char *) NULL, &icnt, &imm_expr,
7464 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7465 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7466 used_at = 0;
7467 }
7468 else
7469 {
7470 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7471 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7472 "d,v,t", dreg, sreg, AT);
7473 used_at = 1;
7474 }
7475 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7476 "d,v,t", dreg, 0, dreg);
7477 if (used_at)
7478 break;
7479 return;
7480
7481 case M_DSUB_I:
7482 dbl = 1;
7483 case M_SUB_I:
7484 if (imm_expr.X_op == O_constant
7485 && imm_expr.X_add_number > -0x8000
7486 && imm_expr.X_add_number <= 0x8000)
7487 {
7488 imm_expr.X_add_number = -imm_expr.X_add_number;
7489 macro_build ((char *) NULL, &icnt, &imm_expr,
7490 dbl ? "daddi" : "addi",
7491 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7492 return;
7493 }
7494 load_register (&icnt, AT, &imm_expr, dbl);
7495 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7496 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7497 break;
7498
7499 case M_DSUBU_I:
7500 dbl = 1;
7501 case M_SUBU_I:
7502 if (imm_expr.X_op == O_constant
7503 && imm_expr.X_add_number > -0x8000
7504 && imm_expr.X_add_number <= 0x8000)
7505 {
7506 imm_expr.X_add_number = -imm_expr.X_add_number;
7507 macro_build ((char *) NULL, &icnt, &imm_expr,
7508 dbl ? "daddiu" : "addiu",
7509 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7510 return;
7511 }
7512 load_register (&icnt, AT, &imm_expr, dbl);
7513 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7514 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7515 break;
7516
7517 case M_TEQ_I:
7518 s = "teq";
7519 goto trap;
7520 case M_TGE_I:
7521 s = "tge";
7522 goto trap;
7523 case M_TGEU_I:
7524 s = "tgeu";
7525 goto trap;
7526 case M_TLT_I:
7527 s = "tlt";
7528 goto trap;
7529 case M_TLTU_I:
7530 s = "tltu";
7531 goto trap;
7532 case M_TNE_I:
7533 s = "tne";
7534 trap:
7535 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7536 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7537 AT);
7538 break;
7539
7540 case M_TRUNCWS:
7541 case M_TRUNCWD:
7542 assert (mips_opts.isa == ISA_MIPS1);
7543 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7544 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7545
7546 /*
7547 * Is the double cfc1 instruction a bug in the mips assembler;
7548 * or is there a reason for it?
7549 */
7550 mips_emit_delays (TRUE);
7551 ++mips_opts.noreorder;
7552 mips_any_noreorder = 1;
7553 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7554 treg, RA);
7555 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7556 treg, RA);
7557 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7558 expr1.X_add_number = 3;
7559 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7560 (int) BFD_RELOC_LO16);
7561 expr1.X_add_number = 2;
7562 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7563 (int) BFD_RELOC_LO16);
7564 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7565 AT, RA);
7566 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7567 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7568 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7569 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7570 treg, RA);
7571 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7572 --mips_opts.noreorder;
7573 break;
7574
7575 case M_ULH:
7576 s = "lb";
7577 goto ulh;
7578 case M_ULHU:
7579 s = "lbu";
7580 ulh:
7581 if (offset_expr.X_add_number >= 0x7fff)
7582 as_bad (_("operand overflow"));
7583 /* avoid load delay */
7584 if (! target_big_endian)
7585 ++offset_expr.X_add_number;
7586 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7587 (int) BFD_RELOC_LO16, breg);
7588 if (! target_big_endian)
7589 --offset_expr.X_add_number;
7590 else
7591 ++offset_expr.X_add_number;
7592 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7593 (int) BFD_RELOC_LO16, breg);
7594 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7595 treg, treg, 8);
7596 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7597 treg, treg, AT);
7598 break;
7599
7600 case M_ULD:
7601 s = "ldl";
7602 s2 = "ldr";
7603 off = 7;
7604 goto ulw;
7605 case M_ULW:
7606 s = "lwl";
7607 s2 = "lwr";
7608 off = 3;
7609 ulw:
7610 if (offset_expr.X_add_number >= 0x8000 - off)
7611 as_bad (_("operand overflow"));
7612 if (! target_big_endian)
7613 offset_expr.X_add_number += off;
7614 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7615 (int) BFD_RELOC_LO16, breg);
7616 if (! target_big_endian)
7617 offset_expr.X_add_number -= off;
7618 else
7619 offset_expr.X_add_number += off;
7620 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7621 (int) BFD_RELOC_LO16, breg);
7622 return;
7623
7624 case M_ULD_A:
7625 s = "ldl";
7626 s2 = "ldr";
7627 off = 7;
7628 goto ulwa;
7629 case M_ULW_A:
7630 s = "lwl";
7631 s2 = "lwr";
7632 off = 3;
7633 ulwa:
7634 used_at = 1;
7635 load_address (&icnt, AT, &offset_expr, &used_at);
7636 if (breg != 0)
7637 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7638 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7639 "d,v,t", AT, AT, breg);
7640 if (! target_big_endian)
7641 expr1.X_add_number = off;
7642 else
7643 expr1.X_add_number = 0;
7644 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7645 (int) BFD_RELOC_LO16, AT);
7646 if (! target_big_endian)
7647 expr1.X_add_number = 0;
7648 else
7649 expr1.X_add_number = off;
7650 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7651 (int) BFD_RELOC_LO16, AT);
7652 break;
7653
7654 case M_ULH_A:
7655 case M_ULHU_A:
7656 used_at = 1;
7657 load_address (&icnt, AT, &offset_expr, &used_at);
7658 if (breg != 0)
7659 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7660 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7661 "d,v,t", AT, AT, breg);
7662 if (target_big_endian)
7663 expr1.X_add_number = 0;
7664 macro_build ((char *) NULL, &icnt, &expr1,
7665 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7666 (int) BFD_RELOC_LO16, AT);
7667 if (target_big_endian)
7668 expr1.X_add_number = 1;
7669 else
7670 expr1.X_add_number = 0;
7671 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7672 (int) BFD_RELOC_LO16, AT);
7673 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7674 treg, treg, 8);
7675 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7676 treg, treg, AT);
7677 break;
7678
7679 case M_USH:
7680 if (offset_expr.X_add_number >= 0x7fff)
7681 as_bad (_("operand overflow"));
7682 if (target_big_endian)
7683 ++offset_expr.X_add_number;
7684 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7685 (int) BFD_RELOC_LO16, breg);
7686 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7687 AT, treg, 8);
7688 if (target_big_endian)
7689 --offset_expr.X_add_number;
7690 else
7691 ++offset_expr.X_add_number;
7692 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7693 (int) BFD_RELOC_LO16, breg);
7694 break;
7695
7696 case M_USD:
7697 s = "sdl";
7698 s2 = "sdr";
7699 off = 7;
7700 goto usw;
7701 case M_USW:
7702 s = "swl";
7703 s2 = "swr";
7704 off = 3;
7705 usw:
7706 if (offset_expr.X_add_number >= 0x8000 - off)
7707 as_bad (_("operand overflow"));
7708 if (! target_big_endian)
7709 offset_expr.X_add_number += off;
7710 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7711 (int) BFD_RELOC_LO16, breg);
7712 if (! target_big_endian)
7713 offset_expr.X_add_number -= off;
7714 else
7715 offset_expr.X_add_number += off;
7716 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7717 (int) BFD_RELOC_LO16, breg);
7718 return;
7719
7720 case M_USD_A:
7721 s = "sdl";
7722 s2 = "sdr";
7723 off = 7;
7724 goto uswa;
7725 case M_USW_A:
7726 s = "swl";
7727 s2 = "swr";
7728 off = 3;
7729 uswa:
7730 used_at = 1;
7731 load_address (&icnt, AT, &offset_expr, &used_at);
7732 if (breg != 0)
7733 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7734 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7735 "d,v,t", AT, AT, breg);
7736 if (! target_big_endian)
7737 expr1.X_add_number = off;
7738 else
7739 expr1.X_add_number = 0;
7740 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7741 (int) BFD_RELOC_LO16, AT);
7742 if (! target_big_endian)
7743 expr1.X_add_number = 0;
7744 else
7745 expr1.X_add_number = off;
7746 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7747 (int) BFD_RELOC_LO16, AT);
7748 break;
7749
7750 case M_USH_A:
7751 used_at = 1;
7752 load_address (&icnt, AT, &offset_expr, &used_at);
7753 if (breg != 0)
7754 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7755 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7756 "d,v,t", AT, AT, breg);
7757 if (! target_big_endian)
7758 expr1.X_add_number = 0;
7759 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7760 (int) BFD_RELOC_LO16, AT);
7761 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7762 treg, treg, 8);
7763 if (! target_big_endian)
7764 expr1.X_add_number = 1;
7765 else
7766 expr1.X_add_number = 0;
7767 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7768 (int) BFD_RELOC_LO16, AT);
7769 if (! target_big_endian)
7770 expr1.X_add_number = 0;
7771 else
7772 expr1.X_add_number = 1;
7773 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7774 (int) BFD_RELOC_LO16, AT);
7775 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7776 treg, treg, 8);
7777 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7778 treg, treg, AT);
7779 break;
7780
7781 default:
7782 /* FIXME: Check if this is one of the itbl macros, since they
7783 are added dynamically. */
7784 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7785 break;
7786 }
7787 if (mips_opts.noat)
7788 as_warn (_("Macro used $at after \".set noat\""));
7789 }
7790
7791 /* Implement macros in mips16 mode. */
7792
7793 static void
7794 mips16_macro (ip)
7795 struct mips_cl_insn *ip;
7796 {
7797 int mask;
7798 int xreg, yreg, zreg, tmp;
7799 int icnt;
7800 expressionS expr1;
7801 int dbl;
7802 const char *s, *s2, *s3;
7803
7804 mask = ip->insn_mo->mask;
7805
7806 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7807 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7808 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7809
7810 icnt = 0;
7811
7812 expr1.X_op = O_constant;
7813 expr1.X_op_symbol = NULL;
7814 expr1.X_add_symbol = NULL;
7815 expr1.X_add_number = 1;
7816
7817 dbl = 0;
7818
7819 switch (mask)
7820 {
7821 default:
7822 internalError ();
7823
7824 case M_DDIV_3:
7825 dbl = 1;
7826 case M_DIV_3:
7827 s = "mflo";
7828 goto do_div3;
7829 case M_DREM_3:
7830 dbl = 1;
7831 case M_REM_3:
7832 s = "mfhi";
7833 do_div3:
7834 mips_emit_delays (TRUE);
7835 ++mips_opts.noreorder;
7836 mips_any_noreorder = 1;
7837 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7838 dbl ? "ddiv" : "div",
7839 "0,x,y", xreg, yreg);
7840 expr1.X_add_number = 2;
7841 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7842 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7843 7);
7844
7845 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7846 since that causes an overflow. We should do that as well,
7847 but I don't see how to do the comparisons without a temporary
7848 register. */
7849 --mips_opts.noreorder;
7850 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7851 break;
7852
7853 case M_DIVU_3:
7854 s = "divu";
7855 s2 = "mflo";
7856 goto do_divu3;
7857 case M_REMU_3:
7858 s = "divu";
7859 s2 = "mfhi";
7860 goto do_divu3;
7861 case M_DDIVU_3:
7862 s = "ddivu";
7863 s2 = "mflo";
7864 goto do_divu3;
7865 case M_DREMU_3:
7866 s = "ddivu";
7867 s2 = "mfhi";
7868 do_divu3:
7869 mips_emit_delays (TRUE);
7870 ++mips_opts.noreorder;
7871 mips_any_noreorder = 1;
7872 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7873 xreg, yreg);
7874 expr1.X_add_number = 2;
7875 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7876 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7877 "6", 7);
7878 --mips_opts.noreorder;
7879 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7880 break;
7881
7882 case M_DMUL:
7883 dbl = 1;
7884 case M_MUL:
7885 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7886 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7887 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7888 zreg);
7889 return;
7890
7891 case M_DSUBU_I:
7892 dbl = 1;
7893 goto do_subu;
7894 case M_SUBU_I:
7895 do_subu:
7896 if (imm_expr.X_op != O_constant)
7897 as_bad (_("Unsupported large constant"));
7898 imm_expr.X_add_number = -imm_expr.X_add_number;
7899 macro_build ((char *) NULL, &icnt, &imm_expr,
7900 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7901 break;
7902
7903 case M_SUBU_I_2:
7904 if (imm_expr.X_op != O_constant)
7905 as_bad (_("Unsupported large constant"));
7906 imm_expr.X_add_number = -imm_expr.X_add_number;
7907 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7908 "x,k", xreg);
7909 break;
7910
7911 case M_DSUBU_I_2:
7912 if (imm_expr.X_op != O_constant)
7913 as_bad (_("Unsupported large constant"));
7914 imm_expr.X_add_number = -imm_expr.X_add_number;
7915 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7916 "y,j", yreg);
7917 break;
7918
7919 case M_BEQ:
7920 s = "cmp";
7921 s2 = "bteqz";
7922 goto do_branch;
7923 case M_BNE:
7924 s = "cmp";
7925 s2 = "btnez";
7926 goto do_branch;
7927 case M_BLT:
7928 s = "slt";
7929 s2 = "btnez";
7930 goto do_branch;
7931 case M_BLTU:
7932 s = "sltu";
7933 s2 = "btnez";
7934 goto do_branch;
7935 case M_BLE:
7936 s = "slt";
7937 s2 = "bteqz";
7938 goto do_reverse_branch;
7939 case M_BLEU:
7940 s = "sltu";
7941 s2 = "bteqz";
7942 goto do_reverse_branch;
7943 case M_BGE:
7944 s = "slt";
7945 s2 = "bteqz";
7946 goto do_branch;
7947 case M_BGEU:
7948 s = "sltu";
7949 s2 = "bteqz";
7950 goto do_branch;
7951 case M_BGT:
7952 s = "slt";
7953 s2 = "btnez";
7954 goto do_reverse_branch;
7955 case M_BGTU:
7956 s = "sltu";
7957 s2 = "btnez";
7958
7959 do_reverse_branch:
7960 tmp = xreg;
7961 xreg = yreg;
7962 yreg = tmp;
7963
7964 do_branch:
7965 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7966 xreg, yreg);
7967 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7968 break;
7969
7970 case M_BEQ_I:
7971 s = "cmpi";
7972 s2 = "bteqz";
7973 s3 = "x,U";
7974 goto do_branch_i;
7975 case M_BNE_I:
7976 s = "cmpi";
7977 s2 = "btnez";
7978 s3 = "x,U";
7979 goto do_branch_i;
7980 case M_BLT_I:
7981 s = "slti";
7982 s2 = "btnez";
7983 s3 = "x,8";
7984 goto do_branch_i;
7985 case M_BLTU_I:
7986 s = "sltiu";
7987 s2 = "btnez";
7988 s3 = "x,8";
7989 goto do_branch_i;
7990 case M_BLE_I:
7991 s = "slti";
7992 s2 = "btnez";
7993 s3 = "x,8";
7994 goto do_addone_branch_i;
7995 case M_BLEU_I:
7996 s = "sltiu";
7997 s2 = "btnez";
7998 s3 = "x,8";
7999 goto do_addone_branch_i;
8000 case M_BGE_I:
8001 s = "slti";
8002 s2 = "bteqz";
8003 s3 = "x,8";
8004 goto do_branch_i;
8005 case M_BGEU_I:
8006 s = "sltiu";
8007 s2 = "bteqz";
8008 s3 = "x,8";
8009 goto do_branch_i;
8010 case M_BGT_I:
8011 s = "slti";
8012 s2 = "bteqz";
8013 s3 = "x,8";
8014 goto do_addone_branch_i;
8015 case M_BGTU_I:
8016 s = "sltiu";
8017 s2 = "bteqz";
8018 s3 = "x,8";
8019
8020 do_addone_branch_i:
8021 if (imm_expr.X_op != O_constant)
8022 as_bad (_("Unsupported large constant"));
8023 ++imm_expr.X_add_number;
8024
8025 do_branch_i:
8026 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
8027 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8028 break;
8029
8030 case M_ABS:
8031 expr1.X_add_number = 0;
8032 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
8033 if (xreg != yreg)
8034 move_register (&icnt, xreg, yreg);
8035 expr1.X_add_number = 2;
8036 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
8037 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8038 "neg", "x,w", xreg, xreg);
8039 }
8040 }
8041
8042 /* For consistency checking, verify that all bits are specified either
8043 by the match/mask part of the instruction definition, or by the
8044 operand list. */
8045 static int
8046 validate_mips_insn (opc)
8047 const struct mips_opcode *opc;
8048 {
8049 const char *p = opc->args;
8050 char c;
8051 unsigned long used_bits = opc->mask;
8052
8053 if ((used_bits & opc->match) != opc->match)
8054 {
8055 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8056 opc->name, opc->args);
8057 return 0;
8058 }
8059 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
8060 while (*p)
8061 switch (c = *p++)
8062 {
8063 case ',': break;
8064 case '(': break;
8065 case ')': break;
8066 case '+':
8067 switch (c = *p++)
8068 {
8069 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8070 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8071 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8072 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
8073 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
8074 default:
8075 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8076 c, opc->name, opc->args);
8077 return 0;
8078 }
8079 break;
8080 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8081 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8082 case 'A': break;
8083 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
8084 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8085 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8086 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8087 case 'F': break;
8088 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8089 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
8090 case 'I': break;
8091 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
8092 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8093 case 'L': break;
8094 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8095 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
8096 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8097 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8098 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8099 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8100 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8101 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8102 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8103 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8104 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8105 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8106 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8107 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8108 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8109 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8110 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8111 case 'f': break;
8112 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8113 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8114 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8115 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8116 case 'l': break;
8117 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8118 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8119 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8120 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8121 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8122 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8123 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8124 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8125 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8126 case 'x': break;
8127 case 'z': break;
8128 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
8129 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8130 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8131 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8132 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8133 case '[': break;
8134 case ']': break;
8135 default:
8136 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8137 c, opc->name, opc->args);
8138 return 0;
8139 }
8140 #undef USE_BITS
8141 if (used_bits != 0xffffffff)
8142 {
8143 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8144 ~used_bits & 0xffffffff, opc->name, opc->args);
8145 return 0;
8146 }
8147 return 1;
8148 }
8149
8150 /* This routine assembles an instruction into its binary format. As a
8151 side effect, it sets one of the global variables imm_reloc or
8152 offset_reloc to the type of relocation to do if one of the operands
8153 is an address expression. */
8154
8155 static void
8156 mips_ip (str, ip)
8157 char *str;
8158 struct mips_cl_insn *ip;
8159 {
8160 char *s;
8161 const char *args;
8162 char c = 0;
8163 struct mips_opcode *insn;
8164 char *argsStart;
8165 unsigned int regno;
8166 unsigned int lastregno = 0;
8167 unsigned int lastpos = 0;
8168 unsigned int limlo, limhi;
8169 char *s_reset;
8170 char save_c = 0;
8171
8172 insn_error = NULL;
8173
8174 /* If the instruction contains a '.', we first try to match an instruction
8175 including the '.'. Then we try again without the '.'. */
8176 insn = NULL;
8177 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8178 continue;
8179
8180 /* If we stopped on whitespace, then replace the whitespace with null for
8181 the call to hash_find. Save the character we replaced just in case we
8182 have to re-parse the instruction. */
8183 if (ISSPACE (*s))
8184 {
8185 save_c = *s;
8186 *s++ = '\0';
8187 }
8188
8189 insn = (struct mips_opcode *) hash_find (op_hash, str);
8190
8191 /* If we didn't find the instruction in the opcode table, try again, but
8192 this time with just the instruction up to, but not including the
8193 first '.'. */
8194 if (insn == NULL)
8195 {
8196 /* Restore the character we overwrite above (if any). */
8197 if (save_c)
8198 *(--s) = save_c;
8199
8200 /* Scan up to the first '.' or whitespace. */
8201 for (s = str;
8202 *s != '\0' && *s != '.' && !ISSPACE (*s);
8203 ++s)
8204 continue;
8205
8206 /* If we did not find a '.', then we can quit now. */
8207 if (*s != '.')
8208 {
8209 insn_error = "unrecognized opcode";
8210 return;
8211 }
8212
8213 /* Lookup the instruction in the hash table. */
8214 *s++ = '\0';
8215 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8216 {
8217 insn_error = "unrecognized opcode";
8218 return;
8219 }
8220 }
8221
8222 argsStart = s;
8223 for (;;)
8224 {
8225 bfd_boolean ok;
8226
8227 assert (strcmp (insn->name, str) == 0);
8228
8229 if (OPCODE_IS_MEMBER (insn,
8230 (mips_opts.isa
8231 | (file_ase_mips16 ? INSN_MIPS16 : 0)
8232 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8233 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8234 mips_arch))
8235 ok = TRUE;
8236 else
8237 ok = FALSE;
8238
8239 if (insn->pinfo != INSN_MACRO)
8240 {
8241 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8242 ok = FALSE;
8243 }
8244
8245 if (! ok)
8246 {
8247 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8248 && strcmp (insn->name, insn[1].name) == 0)
8249 {
8250 ++insn;
8251 continue;
8252 }
8253 else
8254 {
8255 if (!insn_error)
8256 {
8257 static char buf[100];
8258 if (mips_arch_info->is_isa)
8259 sprintf (buf,
8260 _("opcode not supported at this ISA level (%s)"),
8261 mips_cpu_info_from_isa (mips_opts.isa)->name);
8262 else
8263 sprintf (buf,
8264 _("opcode not supported on this processor: %s (%s)"),
8265 mips_arch_info->name,
8266 mips_cpu_info_from_isa (mips_opts.isa)->name);
8267 insn_error = buf;
8268 }
8269 if (save_c)
8270 *(--s) = save_c;
8271 return;
8272 }
8273 }
8274
8275 ip->insn_mo = insn;
8276 ip->insn_opcode = insn->match;
8277 insn_error = NULL;
8278 for (args = insn->args;; ++args)
8279 {
8280 int is_mdmx;
8281
8282 s += strspn (s, " \t");
8283 is_mdmx = 0;
8284 switch (*args)
8285 {
8286 case '\0': /* end of args */
8287 if (*s == '\0')
8288 return;
8289 break;
8290
8291 case ',':
8292 if (*s++ == *args)
8293 continue;
8294 s--;
8295 switch (*++args)
8296 {
8297 case 'r':
8298 case 'v':
8299 ip->insn_opcode |= lastregno << OP_SH_RS;
8300 continue;
8301
8302 case 'w':
8303 ip->insn_opcode |= lastregno << OP_SH_RT;
8304 continue;
8305
8306 case 'W':
8307 ip->insn_opcode |= lastregno << OP_SH_FT;
8308 continue;
8309
8310 case 'V':
8311 ip->insn_opcode |= lastregno << OP_SH_FS;
8312 continue;
8313 }
8314 break;
8315
8316 case '(':
8317 /* Handle optional base register.
8318 Either the base register is omitted or
8319 we must have a left paren. */
8320 /* This is dependent on the next operand specifier
8321 is a base register specification. */
8322 assert (args[1] == 'b' || args[1] == '5'
8323 || args[1] == '-' || args[1] == '4');
8324 if (*s == '\0')
8325 return;
8326
8327 case ')': /* these must match exactly */
8328 case '[':
8329 case ']':
8330 if (*s++ == *args)
8331 continue;
8332 break;
8333
8334 case '+': /* Opcode extension character. */
8335 switch (*++args)
8336 {
8337 case 'A': /* ins/ext position, becomes LSB. */
8338 limlo = 0;
8339 limhi = 31;
8340 my_getExpression (&imm_expr, s);
8341 check_absolute_expr (ip, &imm_expr);
8342 if ((unsigned long) imm_expr.X_add_number < limlo
8343 || (unsigned long) imm_expr.X_add_number > limhi)
8344 {
8345 as_bad (_("Improper position (%lu)"),
8346 (unsigned long) imm_expr.X_add_number);
8347 imm_expr.X_add_number = limlo;
8348 }
8349 lastpos = imm_expr.X_add_number;
8350 ip->insn_opcode |= (imm_expr.X_add_number
8351 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8352 imm_expr.X_op = O_absent;
8353 s = expr_end;
8354 continue;
8355
8356 case 'B': /* ins size, becomes MSB. */
8357 limlo = 1;
8358 limhi = 32;
8359 my_getExpression (&imm_expr, s);
8360 check_absolute_expr (ip, &imm_expr);
8361 /* Check for negative input so that small negative numbers
8362 will not succeed incorrectly. The checks against
8363 (pos+size) transitively check "size" itself,
8364 assuming that "pos" is reasonable. */
8365 if ((long) imm_expr.X_add_number < 0
8366 || ((unsigned long) imm_expr.X_add_number
8367 + lastpos) < limlo
8368 || ((unsigned long) imm_expr.X_add_number
8369 + lastpos) > limhi)
8370 {
8371 as_bad (_("Improper insert size (%lu, position %lu)"),
8372 (unsigned long) imm_expr.X_add_number,
8373 (unsigned long) lastpos);
8374 imm_expr.X_add_number = limlo - lastpos;
8375 }
8376 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8377 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8378 imm_expr.X_op = O_absent;
8379 s = expr_end;
8380 continue;
8381
8382 case 'C': /* ext size, becomes MSBD. */
8383 limlo = 1;
8384 limhi = 32;
8385 my_getExpression (&imm_expr, s);
8386 check_absolute_expr (ip, &imm_expr);
8387 /* Check for negative input so that small negative numbers
8388 will not succeed incorrectly. The checks against
8389 (pos+size) transitively check "size" itself,
8390 assuming that "pos" is reasonable. */
8391 if ((long) imm_expr.X_add_number < 0
8392 || ((unsigned long) imm_expr.X_add_number
8393 + lastpos) < limlo
8394 || ((unsigned long) imm_expr.X_add_number
8395 + lastpos) > limhi)
8396 {
8397 as_bad (_("Improper extract size (%lu, position %lu)"),
8398 (unsigned long) imm_expr.X_add_number,
8399 (unsigned long) lastpos);
8400 imm_expr.X_add_number = limlo - lastpos;
8401 }
8402 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8403 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8404 imm_expr.X_op = O_absent;
8405 s = expr_end;
8406 continue;
8407
8408 case 'D':
8409 /* +D is for disassembly only; never match. */
8410 break;
8411
8412 default:
8413 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8414 *args, insn->name, insn->args);
8415 /* Further processing is fruitless. */
8416 return;
8417 }
8418 break;
8419
8420 case '<': /* must be at least one digit */
8421 /*
8422 * According to the manual, if the shift amount is greater
8423 * than 31 or less than 0, then the shift amount should be
8424 * mod 32. In reality the mips assembler issues an error.
8425 * We issue a warning and mask out all but the low 5 bits.
8426 */
8427 my_getExpression (&imm_expr, s);
8428 check_absolute_expr (ip, &imm_expr);
8429 if ((unsigned long) imm_expr.X_add_number > 31)
8430 {
8431 as_warn (_("Improper shift amount (%lu)"),
8432 (unsigned long) imm_expr.X_add_number);
8433 imm_expr.X_add_number &= OP_MASK_SHAMT;
8434 }
8435 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8436 imm_expr.X_op = O_absent;
8437 s = expr_end;
8438 continue;
8439
8440 case '>': /* shift amount minus 32 */
8441 my_getExpression (&imm_expr, s);
8442 check_absolute_expr (ip, &imm_expr);
8443 if ((unsigned long) imm_expr.X_add_number < 32
8444 || (unsigned long) imm_expr.X_add_number > 63)
8445 break;
8446 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8447 imm_expr.X_op = O_absent;
8448 s = expr_end;
8449 continue;
8450
8451 case 'k': /* cache code */
8452 case 'h': /* prefx code */
8453 my_getExpression (&imm_expr, s);
8454 check_absolute_expr (ip, &imm_expr);
8455 if ((unsigned long) imm_expr.X_add_number > 31)
8456 {
8457 as_warn (_("Invalid value for `%s' (%lu)"),
8458 ip->insn_mo->name,
8459 (unsigned long) imm_expr.X_add_number);
8460 imm_expr.X_add_number &= 0x1f;
8461 }
8462 if (*args == 'k')
8463 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8464 else
8465 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8466 imm_expr.X_op = O_absent;
8467 s = expr_end;
8468 continue;
8469
8470 case 'c': /* break code */
8471 my_getExpression (&imm_expr, s);
8472 check_absolute_expr (ip, &imm_expr);
8473 if ((unsigned long) imm_expr.X_add_number > 1023)
8474 {
8475 as_warn (_("Illegal break code (%lu)"),
8476 (unsigned long) imm_expr.X_add_number);
8477 imm_expr.X_add_number &= OP_MASK_CODE;
8478 }
8479 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8480 imm_expr.X_op = O_absent;
8481 s = expr_end;
8482 continue;
8483
8484 case 'q': /* lower break code */
8485 my_getExpression (&imm_expr, s);
8486 check_absolute_expr (ip, &imm_expr);
8487 if ((unsigned long) imm_expr.X_add_number > 1023)
8488 {
8489 as_warn (_("Illegal lower break code (%lu)"),
8490 (unsigned long) imm_expr.X_add_number);
8491 imm_expr.X_add_number &= OP_MASK_CODE2;
8492 }
8493 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8494 imm_expr.X_op = O_absent;
8495 s = expr_end;
8496 continue;
8497
8498 case 'B': /* 20-bit syscall/break code. */
8499 my_getExpression (&imm_expr, s);
8500 check_absolute_expr (ip, &imm_expr);
8501 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8502 as_warn (_("Illegal 20-bit code (%lu)"),
8503 (unsigned long) imm_expr.X_add_number);
8504 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8505 imm_expr.X_op = O_absent;
8506 s = expr_end;
8507 continue;
8508
8509 case 'C': /* Coprocessor code */
8510 my_getExpression (&imm_expr, s);
8511 check_absolute_expr (ip, &imm_expr);
8512 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8513 {
8514 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8515 (unsigned long) imm_expr.X_add_number);
8516 imm_expr.X_add_number &= ((1 << 25) - 1);
8517 }
8518 ip->insn_opcode |= imm_expr.X_add_number;
8519 imm_expr.X_op = O_absent;
8520 s = expr_end;
8521 continue;
8522
8523 case 'J': /* 19-bit wait code. */
8524 my_getExpression (&imm_expr, s);
8525 check_absolute_expr (ip, &imm_expr);
8526 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8527 as_warn (_("Illegal 19-bit code (%lu)"),
8528 (unsigned long) imm_expr.X_add_number);
8529 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8530 imm_expr.X_op = O_absent;
8531 s = expr_end;
8532 continue;
8533
8534 case 'P': /* Performance register */
8535 my_getExpression (&imm_expr, s);
8536 check_absolute_expr (ip, &imm_expr);
8537 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8538 {
8539 as_warn (_("Invalid performance register (%lu)"),
8540 (unsigned long) imm_expr.X_add_number);
8541 imm_expr.X_add_number &= OP_MASK_PERFREG;
8542 }
8543 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8544 imm_expr.X_op = O_absent;
8545 s = expr_end;
8546 continue;
8547
8548 case 'b': /* base register */
8549 case 'd': /* destination register */
8550 case 's': /* source register */
8551 case 't': /* target register */
8552 case 'r': /* both target and source */
8553 case 'v': /* both dest and source */
8554 case 'w': /* both dest and target */
8555 case 'E': /* coprocessor target register */
8556 case 'G': /* coprocessor destination register */
8557 case 'K': /* 'rdhwr' destination register */
8558 case 'x': /* ignore register name */
8559 case 'z': /* must be zero register */
8560 case 'U': /* destination register (clo/clz). */
8561 s_reset = s;
8562 if (s[0] == '$')
8563 {
8564
8565 if (ISDIGIT (s[1]))
8566 {
8567 ++s;
8568 regno = 0;
8569 do
8570 {
8571 regno *= 10;
8572 regno += *s - '0';
8573 ++s;
8574 }
8575 while (ISDIGIT (*s));
8576 if (regno > 31)
8577 as_bad (_("Invalid register number (%d)"), regno);
8578 }
8579 else if (*args == 'E' || *args == 'G' || *args == 'K')
8580 goto notreg;
8581 else
8582 {
8583 if (s[1] == 'r' && s[2] == 'a')
8584 {
8585 s += 3;
8586 regno = RA;
8587 }
8588 else if (s[1] == 'f' && s[2] == 'p')
8589 {
8590 s += 3;
8591 regno = FP;
8592 }
8593 else if (s[1] == 's' && s[2] == 'p')
8594 {
8595 s += 3;
8596 regno = SP;
8597 }
8598 else if (s[1] == 'g' && s[2] == 'p')
8599 {
8600 s += 3;
8601 regno = GP;
8602 }
8603 else if (s[1] == 'a' && s[2] == 't')
8604 {
8605 s += 3;
8606 regno = AT;
8607 }
8608 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8609 {
8610 s += 4;
8611 regno = KT0;
8612 }
8613 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8614 {
8615 s += 4;
8616 regno = KT1;
8617 }
8618 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8619 {
8620 s += 5;
8621 regno = ZERO;
8622 }
8623 else if (itbl_have_entries)
8624 {
8625 char *p, *n;
8626 unsigned long r;
8627
8628 p = s + 1; /* advance past '$' */
8629 n = itbl_get_field (&p); /* n is name */
8630
8631 /* See if this is a register defined in an
8632 itbl entry. */
8633 if (itbl_get_reg_val (n, &r))
8634 {
8635 /* Get_field advances to the start of
8636 the next field, so we need to back
8637 rack to the end of the last field. */
8638 if (p)
8639 s = p - 1;
8640 else
8641 s = strchr (s, '\0');
8642 regno = r;
8643 }
8644 else
8645 goto notreg;
8646 }
8647 else
8648 goto notreg;
8649 }
8650 if (regno == AT
8651 && ! mips_opts.noat
8652 && *args != 'E'
8653 && *args != 'G'
8654 && *args != 'K')
8655 as_warn (_("Used $at without \".set noat\""));
8656 c = *args;
8657 if (*s == ' ')
8658 ++s;
8659 if (args[1] != *s)
8660 {
8661 if (c == 'r' || c == 'v' || c == 'w')
8662 {
8663 regno = lastregno;
8664 s = s_reset;
8665 ++args;
8666 }
8667 }
8668 /* 'z' only matches $0. */
8669 if (c == 'z' && regno != 0)
8670 break;
8671
8672 /* Now that we have assembled one operand, we use the args string
8673 * to figure out where it goes in the instruction. */
8674 switch (c)
8675 {
8676 case 'r':
8677 case 's':
8678 case 'v':
8679 case 'b':
8680 ip->insn_opcode |= regno << OP_SH_RS;
8681 break;
8682 case 'd':
8683 case 'G':
8684 case 'K':
8685 ip->insn_opcode |= regno << OP_SH_RD;
8686 break;
8687 case 'U':
8688 ip->insn_opcode |= regno << OP_SH_RD;
8689 ip->insn_opcode |= regno << OP_SH_RT;
8690 break;
8691 case 'w':
8692 case 't':
8693 case 'E':
8694 ip->insn_opcode |= regno << OP_SH_RT;
8695 break;
8696 case 'x':
8697 /* This case exists because on the r3000 trunc
8698 expands into a macro which requires a gp
8699 register. On the r6000 or r4000 it is
8700 assembled into a single instruction which
8701 ignores the register. Thus the insn version
8702 is MIPS_ISA2 and uses 'x', and the macro
8703 version is MIPS_ISA1 and uses 't'. */
8704 break;
8705 case 'z':
8706 /* This case is for the div instruction, which
8707 acts differently if the destination argument
8708 is $0. This only matches $0, and is checked
8709 outside the switch. */
8710 break;
8711 case 'D':
8712 /* Itbl operand; not yet implemented. FIXME ?? */
8713 break;
8714 /* What about all other operands like 'i', which
8715 can be specified in the opcode table? */
8716 }
8717 lastregno = regno;
8718 continue;
8719 }
8720 notreg:
8721 switch (*args++)
8722 {
8723 case 'r':
8724 case 'v':
8725 ip->insn_opcode |= lastregno << OP_SH_RS;
8726 continue;
8727 case 'w':
8728 ip->insn_opcode |= lastregno << OP_SH_RT;
8729 continue;
8730 }
8731 break;
8732
8733 case 'O': /* MDMX alignment immediate constant. */
8734 my_getExpression (&imm_expr, s);
8735 check_absolute_expr (ip, &imm_expr);
8736 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8737 {
8738 as_warn ("Improper align amount (%ld), using low bits",
8739 (long) imm_expr.X_add_number);
8740 imm_expr.X_add_number &= OP_MASK_ALN;
8741 }
8742 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8743 imm_expr.X_op = O_absent;
8744 s = expr_end;
8745 continue;
8746
8747 case 'Q': /* MDMX vector, element sel, or const. */
8748 if (s[0] != '$')
8749 {
8750 /* MDMX Immediate. */
8751 my_getExpression (&imm_expr, s);
8752 check_absolute_expr (ip, &imm_expr);
8753 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8754 {
8755 as_warn (_("Invalid MDMX Immediate (%ld)"),
8756 (long) imm_expr.X_add_number);
8757 imm_expr.X_add_number &= OP_MASK_FT;
8758 }
8759 imm_expr.X_add_number &= OP_MASK_FT;
8760 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8761 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8762 else
8763 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8764 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8765 imm_expr.X_op = O_absent;
8766 s = expr_end;
8767 continue;
8768 }
8769 /* Not MDMX Immediate. Fall through. */
8770 case 'X': /* MDMX destination register. */
8771 case 'Y': /* MDMX source register. */
8772 case 'Z': /* MDMX target register. */
8773 is_mdmx = 1;
8774 case 'D': /* floating point destination register */
8775 case 'S': /* floating point source register */
8776 case 'T': /* floating point target register */
8777 case 'R': /* floating point source register */
8778 case 'V':
8779 case 'W':
8780 s_reset = s;
8781 /* Accept $fN for FP and MDMX register numbers, and in
8782 addition accept $vN for MDMX register numbers. */
8783 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8784 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8785 && ISDIGIT (s[2])))
8786 {
8787 s += 2;
8788 regno = 0;
8789 do
8790 {
8791 regno *= 10;
8792 regno += *s - '0';
8793 ++s;
8794 }
8795 while (ISDIGIT (*s));
8796
8797 if (regno > 31)
8798 as_bad (_("Invalid float register number (%d)"), regno);
8799
8800 if ((regno & 1) != 0
8801 && HAVE_32BIT_FPRS
8802 && ! (strcmp (str, "mtc1") == 0
8803 || strcmp (str, "mfc1") == 0
8804 || strcmp (str, "lwc1") == 0
8805 || strcmp (str, "swc1") == 0
8806 || strcmp (str, "l.s") == 0
8807 || strcmp (str, "s.s") == 0))
8808 as_warn (_("Float register should be even, was %d"),
8809 regno);
8810
8811 c = *args;
8812 if (*s == ' ')
8813 ++s;
8814 if (args[1] != *s)
8815 {
8816 if (c == 'V' || c == 'W')
8817 {
8818 regno = lastregno;
8819 s = s_reset;
8820 ++args;
8821 }
8822 }
8823 switch (c)
8824 {
8825 case 'D':
8826 case 'X':
8827 ip->insn_opcode |= regno << OP_SH_FD;
8828 break;
8829 case 'V':
8830 case 'S':
8831 case 'Y':
8832 ip->insn_opcode |= regno << OP_SH_FS;
8833 break;
8834 case 'Q':
8835 /* This is like 'Z', but also needs to fix the MDMX
8836 vector/scalar select bits. Note that the
8837 scalar immediate case is handled above. */
8838 if (*s == '[')
8839 {
8840 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8841 int max_el = (is_qh ? 3 : 7);
8842 s++;
8843 my_getExpression(&imm_expr, s);
8844 check_absolute_expr (ip, &imm_expr);
8845 s = expr_end;
8846 if (imm_expr.X_add_number > max_el)
8847 as_bad(_("Bad element selector %ld"),
8848 (long) imm_expr.X_add_number);
8849 imm_expr.X_add_number &= max_el;
8850 ip->insn_opcode |= (imm_expr.X_add_number
8851 << (OP_SH_VSEL +
8852 (is_qh ? 2 : 1)));
8853 if (*s != ']')
8854 as_warn(_("Expecting ']' found '%s'"), s);
8855 else
8856 s++;
8857 }
8858 else
8859 {
8860 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8861 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8862 << OP_SH_VSEL);
8863 else
8864 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8865 OP_SH_VSEL);
8866 }
8867 /* Fall through */
8868 case 'W':
8869 case 'T':
8870 case 'Z':
8871 ip->insn_opcode |= regno << OP_SH_FT;
8872 break;
8873 case 'R':
8874 ip->insn_opcode |= regno << OP_SH_FR;
8875 break;
8876 }
8877 lastregno = regno;
8878 continue;
8879 }
8880
8881 switch (*args++)
8882 {
8883 case 'V':
8884 ip->insn_opcode |= lastregno << OP_SH_FS;
8885 continue;
8886 case 'W':
8887 ip->insn_opcode |= lastregno << OP_SH_FT;
8888 continue;
8889 }
8890 break;
8891
8892 case 'I':
8893 my_getExpression (&imm_expr, s);
8894 if (imm_expr.X_op != O_big
8895 && imm_expr.X_op != O_constant)
8896 insn_error = _("absolute expression required");
8897 s = expr_end;
8898 continue;
8899
8900 case 'A':
8901 my_getExpression (&offset_expr, s);
8902 *imm_reloc = BFD_RELOC_32;
8903 s = expr_end;
8904 continue;
8905
8906 case 'F':
8907 case 'L':
8908 case 'f':
8909 case 'l':
8910 {
8911 int f64;
8912 int using_gprs;
8913 char *save_in;
8914 char *err;
8915 unsigned char temp[8];
8916 int len;
8917 unsigned int length;
8918 segT seg;
8919 subsegT subseg;
8920 char *p;
8921
8922 /* These only appear as the last operand in an
8923 instruction, and every instruction that accepts
8924 them in any variant accepts them in all variants.
8925 This means we don't have to worry about backing out
8926 any changes if the instruction does not match.
8927
8928 The difference between them is the size of the
8929 floating point constant and where it goes. For 'F'
8930 and 'L' the constant is 64 bits; for 'f' and 'l' it
8931 is 32 bits. Where the constant is placed is based
8932 on how the MIPS assembler does things:
8933 F -- .rdata
8934 L -- .lit8
8935 f -- immediate value
8936 l -- .lit4
8937
8938 The .lit4 and .lit8 sections are only used if
8939 permitted by the -G argument.
8940
8941 When generating embedded PIC code, we use the
8942 .lit8 section but not the .lit4 section (we can do
8943 .lit4 inline easily; we need to put .lit8
8944 somewhere in the data segment, and using .lit8
8945 permits the linker to eventually combine identical
8946 .lit8 entries).
8947
8948 The code below needs to know whether the target register
8949 is 32 or 64 bits wide. It relies on the fact 'f' and
8950 'F' are used with GPR-based instructions and 'l' and
8951 'L' are used with FPR-based instructions. */
8952
8953 f64 = *args == 'F' || *args == 'L';
8954 using_gprs = *args == 'F' || *args == 'f';
8955
8956 save_in = input_line_pointer;
8957 input_line_pointer = s;
8958 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8959 length = len;
8960 s = input_line_pointer;
8961 input_line_pointer = save_in;
8962 if (err != NULL && *err != '\0')
8963 {
8964 as_bad (_("Bad floating point constant: %s"), err);
8965 memset (temp, '\0', sizeof temp);
8966 length = f64 ? 8 : 4;
8967 }
8968
8969 assert (length == (unsigned) (f64 ? 8 : 4));
8970
8971 if (*args == 'f'
8972 || (*args == 'l'
8973 && (! USE_GLOBAL_POINTER_OPT
8974 || mips_pic == EMBEDDED_PIC
8975 || g_switch_value < 4
8976 || (temp[0] == 0 && temp[1] == 0)
8977 || (temp[2] == 0 && temp[3] == 0))))
8978 {
8979 imm_expr.X_op = O_constant;
8980 if (! target_big_endian)
8981 imm_expr.X_add_number = bfd_getl32 (temp);
8982 else
8983 imm_expr.X_add_number = bfd_getb32 (temp);
8984 }
8985 else if (length > 4
8986 && ! mips_disable_float_construction
8987 /* Constants can only be constructed in GPRs and
8988 copied to FPRs if the GPRs are at least as wide
8989 as the FPRs. Force the constant into memory if
8990 we are using 64-bit FPRs but the GPRs are only
8991 32 bits wide. */
8992 && (using_gprs
8993 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8994 && ((temp[0] == 0 && temp[1] == 0)
8995 || (temp[2] == 0 && temp[3] == 0))
8996 && ((temp[4] == 0 && temp[5] == 0)
8997 || (temp[6] == 0 && temp[7] == 0)))
8998 {
8999 /* The value is simple enough to load with a couple of
9000 instructions. If using 32-bit registers, set
9001 imm_expr to the high order 32 bits and offset_expr to
9002 the low order 32 bits. Otherwise, set imm_expr to
9003 the entire 64 bit constant. */
9004 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
9005 {
9006 imm_expr.X_op = O_constant;
9007 offset_expr.X_op = O_constant;
9008 if (! target_big_endian)
9009 {
9010 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9011 offset_expr.X_add_number = bfd_getl32 (temp);
9012 }
9013 else
9014 {
9015 imm_expr.X_add_number = bfd_getb32 (temp);
9016 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9017 }
9018 if (offset_expr.X_add_number == 0)
9019 offset_expr.X_op = O_absent;
9020 }
9021 else if (sizeof (imm_expr.X_add_number) > 4)
9022 {
9023 imm_expr.X_op = O_constant;
9024 if (! target_big_endian)
9025 imm_expr.X_add_number = bfd_getl64 (temp);
9026 else
9027 imm_expr.X_add_number = bfd_getb64 (temp);
9028 }
9029 else
9030 {
9031 imm_expr.X_op = O_big;
9032 imm_expr.X_add_number = 4;
9033 if (! target_big_endian)
9034 {
9035 generic_bignum[0] = bfd_getl16 (temp);
9036 generic_bignum[1] = bfd_getl16 (temp + 2);
9037 generic_bignum[2] = bfd_getl16 (temp + 4);
9038 generic_bignum[3] = bfd_getl16 (temp + 6);
9039 }
9040 else
9041 {
9042 generic_bignum[0] = bfd_getb16 (temp + 6);
9043 generic_bignum[1] = bfd_getb16 (temp + 4);
9044 generic_bignum[2] = bfd_getb16 (temp + 2);
9045 generic_bignum[3] = bfd_getb16 (temp);
9046 }
9047 }
9048 }
9049 else
9050 {
9051 const char *newname;
9052 segT new_seg;
9053
9054 /* Switch to the right section. */
9055 seg = now_seg;
9056 subseg = now_subseg;
9057 switch (*args)
9058 {
9059 default: /* unused default case avoids warnings. */
9060 case 'L':
9061 newname = RDATA_SECTION_NAME;
9062 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9063 || mips_pic == EMBEDDED_PIC)
9064 newname = ".lit8";
9065 break;
9066 case 'F':
9067 if (mips_pic == EMBEDDED_PIC)
9068 newname = ".lit8";
9069 else
9070 newname = RDATA_SECTION_NAME;
9071 break;
9072 case 'l':
9073 assert (!USE_GLOBAL_POINTER_OPT
9074 || g_switch_value >= 4);
9075 newname = ".lit4";
9076 break;
9077 }
9078 new_seg = subseg_new (newname, (subsegT) 0);
9079 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9080 bfd_set_section_flags (stdoutput, new_seg,
9081 (SEC_ALLOC
9082 | SEC_LOAD
9083 | SEC_READONLY
9084 | SEC_DATA));
9085 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9086 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9087 && strcmp (TARGET_OS, "elf") != 0)
9088 record_alignment (new_seg, 4);
9089 else
9090 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9091 if (seg == now_seg)
9092 as_bad (_("Can't use floating point insn in this section"));
9093
9094 /* Set the argument to the current address in the
9095 section. */
9096 offset_expr.X_op = O_symbol;
9097 offset_expr.X_add_symbol =
9098 symbol_new ("L0\001", now_seg,
9099 (valueT) frag_now_fix (), frag_now);
9100 offset_expr.X_add_number = 0;
9101
9102 /* Put the floating point number into the section. */
9103 p = frag_more ((int) length);
9104 memcpy (p, temp, length);
9105
9106 /* Switch back to the original section. */
9107 subseg_set (seg, subseg);
9108 }
9109 }
9110 continue;
9111
9112 case 'i': /* 16 bit unsigned immediate */
9113 case 'j': /* 16 bit signed immediate */
9114 *imm_reloc = BFD_RELOC_LO16;
9115 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9116 {
9117 int more;
9118 offsetT minval, maxval;
9119
9120 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9121 && strcmp (insn->name, insn[1].name) == 0);
9122
9123 /* If the expression was written as an unsigned number,
9124 only treat it as signed if there are no more
9125 alternatives. */
9126 if (more
9127 && *args == 'j'
9128 && sizeof (imm_expr.X_add_number) <= 4
9129 && imm_expr.X_op == O_constant
9130 && imm_expr.X_add_number < 0
9131 && imm_expr.X_unsigned
9132 && HAVE_64BIT_GPRS)
9133 break;
9134
9135 /* For compatibility with older assemblers, we accept
9136 0x8000-0xffff as signed 16-bit numbers when only
9137 signed numbers are allowed. */
9138 if (*args == 'i')
9139 minval = 0, maxval = 0xffff;
9140 else if (more)
9141 minval = -0x8000, maxval = 0x7fff;
9142 else
9143 minval = -0x8000, maxval = 0xffff;
9144
9145 if (imm_expr.X_op != O_constant
9146 || imm_expr.X_add_number < minval
9147 || imm_expr.X_add_number > maxval)
9148 {
9149 if (more)
9150 break;
9151 if (imm_expr.X_op == O_constant
9152 || imm_expr.X_op == O_big)
9153 as_bad (_("expression out of range"));
9154 }
9155 }
9156 s = expr_end;
9157 continue;
9158
9159 case 'o': /* 16 bit offset */
9160 /* Check whether there is only a single bracketed expression
9161 left. If so, it must be the base register and the
9162 constant must be zero. */
9163 if (*s == '(' && strchr (s + 1, '(') == 0)
9164 {
9165 offset_expr.X_op = O_constant;
9166 offset_expr.X_add_number = 0;
9167 continue;
9168 }
9169
9170 /* If this value won't fit into a 16 bit offset, then go
9171 find a macro that will generate the 32 bit offset
9172 code pattern. */
9173 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9174 && (offset_expr.X_op != O_constant
9175 || offset_expr.X_add_number >= 0x8000
9176 || offset_expr.X_add_number < -0x8000))
9177 break;
9178
9179 s = expr_end;
9180 continue;
9181
9182 case 'p': /* pc relative offset */
9183 if (mips_pic == EMBEDDED_PIC)
9184 *offset_reloc = BFD_RELOC_MIPSEMB_16_PCREL_S2;
9185 else
9186 *offset_reloc = BFD_RELOC_16_PCREL_S2;
9187 my_getExpression (&offset_expr, s);
9188 s = expr_end;
9189 continue;
9190
9191 case 'u': /* upper 16 bits */
9192 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9193 && imm_expr.X_op == O_constant
9194 && (imm_expr.X_add_number < 0
9195 || imm_expr.X_add_number >= 0x10000))
9196 as_bad (_("lui expression not in range 0..65535"));
9197 s = expr_end;
9198 continue;
9199
9200 case 'a': /* 26 bit address */
9201 my_getExpression (&offset_expr, s);
9202 s = expr_end;
9203 *offset_reloc = BFD_RELOC_MIPS_JMP;
9204 continue;
9205
9206 case 'N': /* 3 bit branch condition code */
9207 case 'M': /* 3 bit compare condition code */
9208 if (strncmp (s, "$fcc", 4) != 0)
9209 break;
9210 s += 4;
9211 regno = 0;
9212 do
9213 {
9214 regno *= 10;
9215 regno += *s - '0';
9216 ++s;
9217 }
9218 while (ISDIGIT (*s));
9219 if (regno > 7)
9220 as_bad (_("invalid condition code register $fcc%d"), regno);
9221 if (*args == 'N')
9222 ip->insn_opcode |= regno << OP_SH_BCC;
9223 else
9224 ip->insn_opcode |= regno << OP_SH_CCC;
9225 continue;
9226
9227 case 'H':
9228 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9229 s += 2;
9230 if (ISDIGIT (*s))
9231 {
9232 c = 0;
9233 do
9234 {
9235 c *= 10;
9236 c += *s - '0';
9237 ++s;
9238 }
9239 while (ISDIGIT (*s));
9240 }
9241 else
9242 c = 8; /* Invalid sel value. */
9243
9244 if (c > 7)
9245 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9246 ip->insn_opcode |= c;
9247 continue;
9248
9249 case 'e':
9250 /* Must be at least one digit. */
9251 my_getExpression (&imm_expr, s);
9252 check_absolute_expr (ip, &imm_expr);
9253
9254 if ((unsigned long) imm_expr.X_add_number
9255 > (unsigned long) OP_MASK_VECBYTE)
9256 {
9257 as_bad (_("bad byte vector index (%ld)"),
9258 (long) imm_expr.X_add_number);
9259 imm_expr.X_add_number = 0;
9260 }
9261
9262 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9263 imm_expr.X_op = O_absent;
9264 s = expr_end;
9265 continue;
9266
9267 case '%':
9268 my_getExpression (&imm_expr, s);
9269 check_absolute_expr (ip, &imm_expr);
9270
9271 if ((unsigned long) imm_expr.X_add_number
9272 > (unsigned long) OP_MASK_VECALIGN)
9273 {
9274 as_bad (_("bad byte vector index (%ld)"),
9275 (long) imm_expr.X_add_number);
9276 imm_expr.X_add_number = 0;
9277 }
9278
9279 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9280 imm_expr.X_op = O_absent;
9281 s = expr_end;
9282 continue;
9283
9284 default:
9285 as_bad (_("bad char = '%c'\n"), *args);
9286 internalError ();
9287 }
9288 break;
9289 }
9290 /* Args don't match. */
9291 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9292 !strcmp (insn->name, insn[1].name))
9293 {
9294 ++insn;
9295 s = argsStart;
9296 insn_error = _("illegal operands");
9297 continue;
9298 }
9299 if (save_c)
9300 *(--s) = save_c;
9301 insn_error = _("illegal operands");
9302 return;
9303 }
9304 }
9305
9306 /* This routine assembles an instruction into its binary format when
9307 assembling for the mips16. As a side effect, it sets one of the
9308 global variables imm_reloc or offset_reloc to the type of
9309 relocation to do if one of the operands is an address expression.
9310 It also sets mips16_small and mips16_ext if the user explicitly
9311 requested a small or extended instruction. */
9312
9313 static void
9314 mips16_ip (str, ip)
9315 char *str;
9316 struct mips_cl_insn *ip;
9317 {
9318 char *s;
9319 const char *args;
9320 struct mips_opcode *insn;
9321 char *argsstart;
9322 unsigned int regno;
9323 unsigned int lastregno = 0;
9324 char *s_reset;
9325
9326 insn_error = NULL;
9327
9328 mips16_small = FALSE;
9329 mips16_ext = FALSE;
9330
9331 for (s = str; ISLOWER (*s); ++s)
9332 ;
9333 switch (*s)
9334 {
9335 case '\0':
9336 break;
9337
9338 case ' ':
9339 *s++ = '\0';
9340 break;
9341
9342 case '.':
9343 if (s[1] == 't' && s[2] == ' ')
9344 {
9345 *s = '\0';
9346 mips16_small = TRUE;
9347 s += 3;
9348 break;
9349 }
9350 else if (s[1] == 'e' && s[2] == ' ')
9351 {
9352 *s = '\0';
9353 mips16_ext = TRUE;
9354 s += 3;
9355 break;
9356 }
9357 /* Fall through. */
9358 default:
9359 insn_error = _("unknown opcode");
9360 return;
9361 }
9362
9363 if (mips_opts.noautoextend && ! mips16_ext)
9364 mips16_small = TRUE;
9365
9366 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9367 {
9368 insn_error = _("unrecognized opcode");
9369 return;
9370 }
9371
9372 argsstart = s;
9373 for (;;)
9374 {
9375 assert (strcmp (insn->name, str) == 0);
9376
9377 ip->insn_mo = insn;
9378 ip->insn_opcode = insn->match;
9379 ip->use_extend = FALSE;
9380 imm_expr.X_op = O_absent;
9381 imm_reloc[0] = BFD_RELOC_UNUSED;
9382 imm_reloc[1] = BFD_RELOC_UNUSED;
9383 imm_reloc[2] = BFD_RELOC_UNUSED;
9384 offset_expr.X_op = O_absent;
9385 offset_reloc[0] = BFD_RELOC_UNUSED;
9386 offset_reloc[1] = BFD_RELOC_UNUSED;
9387 offset_reloc[2] = BFD_RELOC_UNUSED;
9388 for (args = insn->args; 1; ++args)
9389 {
9390 int c;
9391
9392 if (*s == ' ')
9393 ++s;
9394
9395 /* In this switch statement we call break if we did not find
9396 a match, continue if we did find a match, or return if we
9397 are done. */
9398
9399 c = *args;
9400 switch (c)
9401 {
9402 case '\0':
9403 if (*s == '\0')
9404 {
9405 /* Stuff the immediate value in now, if we can. */
9406 if (imm_expr.X_op == O_constant
9407 && *imm_reloc > BFD_RELOC_UNUSED
9408 && insn->pinfo != INSN_MACRO)
9409 {
9410 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9411 imm_expr.X_add_number, TRUE, mips16_small,
9412 mips16_ext, &ip->insn_opcode,
9413 &ip->use_extend, &ip->extend);
9414 imm_expr.X_op = O_absent;
9415 *imm_reloc = BFD_RELOC_UNUSED;
9416 }
9417
9418 return;
9419 }
9420 break;
9421
9422 case ',':
9423 if (*s++ == c)
9424 continue;
9425 s--;
9426 switch (*++args)
9427 {
9428 case 'v':
9429 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9430 continue;
9431 case 'w':
9432 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9433 continue;
9434 }
9435 break;
9436
9437 case '(':
9438 case ')':
9439 if (*s++ == c)
9440 continue;
9441 break;
9442
9443 case 'v':
9444 case 'w':
9445 if (s[0] != '$')
9446 {
9447 if (c == 'v')
9448 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9449 else
9450 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9451 ++args;
9452 continue;
9453 }
9454 /* Fall through. */
9455 case 'x':
9456 case 'y':
9457 case 'z':
9458 case 'Z':
9459 case '0':
9460 case 'S':
9461 case 'R':
9462 case 'X':
9463 case 'Y':
9464 if (s[0] != '$')
9465 break;
9466 s_reset = s;
9467 if (ISDIGIT (s[1]))
9468 {
9469 ++s;
9470 regno = 0;
9471 do
9472 {
9473 regno *= 10;
9474 regno += *s - '0';
9475 ++s;
9476 }
9477 while (ISDIGIT (*s));
9478 if (regno > 31)
9479 {
9480 as_bad (_("invalid register number (%d)"), regno);
9481 regno = 2;
9482 }
9483 }
9484 else
9485 {
9486 if (s[1] == 'r' && s[2] == 'a')
9487 {
9488 s += 3;
9489 regno = RA;
9490 }
9491 else if (s[1] == 'f' && s[2] == 'p')
9492 {
9493 s += 3;
9494 regno = FP;
9495 }
9496 else if (s[1] == 's' && s[2] == 'p')
9497 {
9498 s += 3;
9499 regno = SP;
9500 }
9501 else if (s[1] == 'g' && s[2] == 'p')
9502 {
9503 s += 3;
9504 regno = GP;
9505 }
9506 else if (s[1] == 'a' && s[2] == 't')
9507 {
9508 s += 3;
9509 regno = AT;
9510 }
9511 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9512 {
9513 s += 4;
9514 regno = KT0;
9515 }
9516 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9517 {
9518 s += 4;
9519 regno = KT1;
9520 }
9521 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9522 {
9523 s += 5;
9524 regno = ZERO;
9525 }
9526 else
9527 break;
9528 }
9529
9530 if (*s == ' ')
9531 ++s;
9532 if (args[1] != *s)
9533 {
9534 if (c == 'v' || c == 'w')
9535 {
9536 regno = mips16_to_32_reg_map[lastregno];
9537 s = s_reset;
9538 ++args;
9539 }
9540 }
9541
9542 switch (c)
9543 {
9544 case 'x':
9545 case 'y':
9546 case 'z':
9547 case 'v':
9548 case 'w':
9549 case 'Z':
9550 regno = mips32_to_16_reg_map[regno];
9551 break;
9552
9553 case '0':
9554 if (regno != 0)
9555 regno = ILLEGAL_REG;
9556 break;
9557
9558 case 'S':
9559 if (regno != SP)
9560 regno = ILLEGAL_REG;
9561 break;
9562
9563 case 'R':
9564 if (regno != RA)
9565 regno = ILLEGAL_REG;
9566 break;
9567
9568 case 'X':
9569 case 'Y':
9570 if (regno == AT && ! mips_opts.noat)
9571 as_warn (_("used $at without \".set noat\""));
9572 break;
9573
9574 default:
9575 internalError ();
9576 }
9577
9578 if (regno == ILLEGAL_REG)
9579 break;
9580
9581 switch (c)
9582 {
9583 case 'x':
9584 case 'v':
9585 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9586 break;
9587 case 'y':
9588 case 'w':
9589 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9590 break;
9591 case 'z':
9592 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9593 break;
9594 case 'Z':
9595 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9596 case '0':
9597 case 'S':
9598 case 'R':
9599 break;
9600 case 'X':
9601 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9602 break;
9603 case 'Y':
9604 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9605 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9606 break;
9607 default:
9608 internalError ();
9609 }
9610
9611 lastregno = regno;
9612 continue;
9613
9614 case 'P':
9615 if (strncmp (s, "$pc", 3) == 0)
9616 {
9617 s += 3;
9618 continue;
9619 }
9620 break;
9621
9622 case '<':
9623 case '>':
9624 case '[':
9625 case ']':
9626 case '4':
9627 case '5':
9628 case 'H':
9629 case 'W':
9630 case 'D':
9631 case 'j':
9632 case '8':
9633 case 'V':
9634 case 'C':
9635 case 'U':
9636 case 'k':
9637 case 'K':
9638 if (s[0] == '%'
9639 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9640 {
9641 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9642 and generate the appropriate reloc. If the text
9643 inside %gprel is not a symbol name with an
9644 optional offset, then we generate a normal reloc
9645 and will probably fail later. */
9646 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9647 if (imm_expr.X_op == O_symbol)
9648 {
9649 mips16_ext = TRUE;
9650 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9651 s = expr_end;
9652 ip->use_extend = TRUE;
9653 ip->extend = 0;
9654 continue;
9655 }
9656 }
9657 else
9658 {
9659 /* Just pick up a normal expression. */
9660 my_getExpression (&imm_expr, s);
9661 }
9662
9663 if (imm_expr.X_op == O_register)
9664 {
9665 /* What we thought was an expression turned out to
9666 be a register. */
9667
9668 if (s[0] == '(' && args[1] == '(')
9669 {
9670 /* It looks like the expression was omitted
9671 before a register indirection, which means
9672 that the expression is implicitly zero. We
9673 still set up imm_expr, so that we handle
9674 explicit extensions correctly. */
9675 imm_expr.X_op = O_constant;
9676 imm_expr.X_add_number = 0;
9677 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9678 continue;
9679 }
9680
9681 break;
9682 }
9683
9684 /* We need to relax this instruction. */
9685 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9686 s = expr_end;
9687 continue;
9688
9689 case 'p':
9690 case 'q':
9691 case 'A':
9692 case 'B':
9693 case 'E':
9694 /* We use offset_reloc rather than imm_reloc for the PC
9695 relative operands. This lets macros with both
9696 immediate and address operands work correctly. */
9697 my_getExpression (&offset_expr, s);
9698
9699 if (offset_expr.X_op == O_register)
9700 break;
9701
9702 /* We need to relax this instruction. */
9703 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9704 s = expr_end;
9705 continue;
9706
9707 case '6': /* break code */
9708 my_getExpression (&imm_expr, s);
9709 check_absolute_expr (ip, &imm_expr);
9710 if ((unsigned long) imm_expr.X_add_number > 63)
9711 {
9712 as_warn (_("Invalid value for `%s' (%lu)"),
9713 ip->insn_mo->name,
9714 (unsigned long) imm_expr.X_add_number);
9715 imm_expr.X_add_number &= 0x3f;
9716 }
9717 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9718 imm_expr.X_op = O_absent;
9719 s = expr_end;
9720 continue;
9721
9722 case 'a': /* 26 bit address */
9723 my_getExpression (&offset_expr, s);
9724 s = expr_end;
9725 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9726 ip->insn_opcode <<= 16;
9727 continue;
9728
9729 case 'l': /* register list for entry macro */
9730 case 'L': /* register list for exit macro */
9731 {
9732 int mask;
9733
9734 if (c == 'l')
9735 mask = 0;
9736 else
9737 mask = 7 << 3;
9738 while (*s != '\0')
9739 {
9740 int freg, reg1, reg2;
9741
9742 while (*s == ' ' || *s == ',')
9743 ++s;
9744 if (*s != '$')
9745 {
9746 as_bad (_("can't parse register list"));
9747 break;
9748 }
9749 ++s;
9750 if (*s != 'f')
9751 freg = 0;
9752 else
9753 {
9754 freg = 1;
9755 ++s;
9756 }
9757 reg1 = 0;
9758 while (ISDIGIT (*s))
9759 {
9760 reg1 *= 10;
9761 reg1 += *s - '0';
9762 ++s;
9763 }
9764 if (*s == ' ')
9765 ++s;
9766 if (*s != '-')
9767 reg2 = reg1;
9768 else
9769 {
9770 ++s;
9771 if (*s != '$')
9772 break;
9773 ++s;
9774 if (freg)
9775 {
9776 if (*s == 'f')
9777 ++s;
9778 else
9779 {
9780 as_bad (_("invalid register list"));
9781 break;
9782 }
9783 }
9784 reg2 = 0;
9785 while (ISDIGIT (*s))
9786 {
9787 reg2 *= 10;
9788 reg2 += *s - '0';
9789 ++s;
9790 }
9791 }
9792 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9793 {
9794 mask &= ~ (7 << 3);
9795 mask |= 5 << 3;
9796 }
9797 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9798 {
9799 mask &= ~ (7 << 3);
9800 mask |= 6 << 3;
9801 }
9802 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9803 mask |= (reg2 - 3) << 3;
9804 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9805 mask |= (reg2 - 15) << 1;
9806 else if (reg1 == RA && reg2 == RA)
9807 mask |= 1;
9808 else
9809 {
9810 as_bad (_("invalid register list"));
9811 break;
9812 }
9813 }
9814 /* The mask is filled in in the opcode table for the
9815 benefit of the disassembler. We remove it before
9816 applying the actual mask. */
9817 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9818 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9819 }
9820 continue;
9821
9822 case 'e': /* extend code */
9823 my_getExpression (&imm_expr, s);
9824 check_absolute_expr (ip, &imm_expr);
9825 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9826 {
9827 as_warn (_("Invalid value for `%s' (%lu)"),
9828 ip->insn_mo->name,
9829 (unsigned long) imm_expr.X_add_number);
9830 imm_expr.X_add_number &= 0x7ff;
9831 }
9832 ip->insn_opcode |= imm_expr.X_add_number;
9833 imm_expr.X_op = O_absent;
9834 s = expr_end;
9835 continue;
9836
9837 default:
9838 internalError ();
9839 }
9840 break;
9841 }
9842
9843 /* Args don't match. */
9844 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9845 strcmp (insn->name, insn[1].name) == 0)
9846 {
9847 ++insn;
9848 s = argsstart;
9849 continue;
9850 }
9851
9852 insn_error = _("illegal operands");
9853
9854 return;
9855 }
9856 }
9857
9858 /* This structure holds information we know about a mips16 immediate
9859 argument type. */
9860
9861 struct mips16_immed_operand
9862 {
9863 /* The type code used in the argument string in the opcode table. */
9864 int type;
9865 /* The number of bits in the short form of the opcode. */
9866 int nbits;
9867 /* The number of bits in the extended form of the opcode. */
9868 int extbits;
9869 /* The amount by which the short form is shifted when it is used;
9870 for example, the sw instruction has a shift count of 2. */
9871 int shift;
9872 /* The amount by which the short form is shifted when it is stored
9873 into the instruction code. */
9874 int op_shift;
9875 /* Non-zero if the short form is unsigned. */
9876 int unsp;
9877 /* Non-zero if the extended form is unsigned. */
9878 int extu;
9879 /* Non-zero if the value is PC relative. */
9880 int pcrel;
9881 };
9882
9883 /* The mips16 immediate operand types. */
9884
9885 static const struct mips16_immed_operand mips16_immed_operands[] =
9886 {
9887 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9888 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9889 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9890 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9891 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9892 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9893 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9894 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9895 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9896 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9897 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9898 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9899 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9900 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9901 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9902 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9903 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9904 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9905 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9906 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9907 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9908 };
9909
9910 #define MIPS16_NUM_IMMED \
9911 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9912
9913 /* Handle a mips16 instruction with an immediate value. This or's the
9914 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9915 whether an extended value is needed; if one is needed, it sets
9916 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9917 If SMALL is true, an unextended opcode was explicitly requested.
9918 If EXT is true, an extended opcode was explicitly requested. If
9919 WARN is true, warn if EXT does not match reality. */
9920
9921 static void
9922 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9923 extend)
9924 char *file;
9925 unsigned int line;
9926 int type;
9927 offsetT val;
9928 bfd_boolean warn;
9929 bfd_boolean small;
9930 bfd_boolean ext;
9931 unsigned long *insn;
9932 bfd_boolean *use_extend;
9933 unsigned short *extend;
9934 {
9935 register const struct mips16_immed_operand *op;
9936 int mintiny, maxtiny;
9937 bfd_boolean needext;
9938
9939 op = mips16_immed_operands;
9940 while (op->type != type)
9941 {
9942 ++op;
9943 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9944 }
9945
9946 if (op->unsp)
9947 {
9948 if (type == '<' || type == '>' || type == '[' || type == ']')
9949 {
9950 mintiny = 1;
9951 maxtiny = 1 << op->nbits;
9952 }
9953 else
9954 {
9955 mintiny = 0;
9956 maxtiny = (1 << op->nbits) - 1;
9957 }
9958 }
9959 else
9960 {
9961 mintiny = - (1 << (op->nbits - 1));
9962 maxtiny = (1 << (op->nbits - 1)) - 1;
9963 }
9964
9965 /* Branch offsets have an implicit 0 in the lowest bit. */
9966 if (type == 'p' || type == 'q')
9967 val /= 2;
9968
9969 if ((val & ((1 << op->shift) - 1)) != 0
9970 || val < (mintiny << op->shift)
9971 || val > (maxtiny << op->shift))
9972 needext = TRUE;
9973 else
9974 needext = FALSE;
9975
9976 if (warn && ext && ! needext)
9977 as_warn_where (file, line,
9978 _("extended operand requested but not required"));
9979 if (small && needext)
9980 as_bad_where (file, line, _("invalid unextended operand value"));
9981
9982 if (small || (! ext && ! needext))
9983 {
9984 int insnval;
9985
9986 *use_extend = FALSE;
9987 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9988 insnval <<= op->op_shift;
9989 *insn |= insnval;
9990 }
9991 else
9992 {
9993 long minext, maxext;
9994 int extval;
9995
9996 if (op->extu)
9997 {
9998 minext = 0;
9999 maxext = (1 << op->extbits) - 1;
10000 }
10001 else
10002 {
10003 minext = - (1 << (op->extbits - 1));
10004 maxext = (1 << (op->extbits - 1)) - 1;
10005 }
10006 if (val < minext || val > maxext)
10007 as_bad_where (file, line,
10008 _("operand value out of range for instruction"));
10009
10010 *use_extend = TRUE;
10011 if (op->extbits == 16)
10012 {
10013 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10014 val &= 0x1f;
10015 }
10016 else if (op->extbits == 15)
10017 {
10018 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10019 val &= 0xf;
10020 }
10021 else
10022 {
10023 extval = ((val & 0x1f) << 6) | (val & 0x20);
10024 val = 0;
10025 }
10026
10027 *extend = (unsigned short) extval;
10028 *insn |= val;
10029 }
10030 }
10031 \f
10032 static const struct percent_op_match
10033 {
10034 const char *str;
10035 bfd_reloc_code_real_type reloc;
10036 } percent_op[] =
10037 {
10038 {"%lo", BFD_RELOC_LO16},
10039 #ifdef OBJ_ELF
10040 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10041 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10042 {"%call16", BFD_RELOC_MIPS_CALL16},
10043 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10044 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10045 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10046 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10047 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10048 {"%got", BFD_RELOC_MIPS_GOT16},
10049 {"%gp_rel", BFD_RELOC_GPREL16},
10050 {"%half", BFD_RELOC_16},
10051 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10052 {"%higher", BFD_RELOC_MIPS_HIGHER},
10053 {"%neg", BFD_RELOC_MIPS_SUB},
10054 #endif
10055 {"%hi", BFD_RELOC_HI16_S}
10056 };
10057
10058
10059 /* Return true if *STR points to a relocation operator. When returning true,
10060 move *STR over the operator and store its relocation code in *RELOC.
10061 Leave both *STR and *RELOC alone when returning false. */
10062
10063 static bfd_boolean
10064 parse_relocation (str, reloc)
10065 char **str;
10066 bfd_reloc_code_real_type *reloc;
10067 {
10068 size_t i;
10069
10070 for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10071 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10072 {
10073 *str += strlen (percent_op[i].str);
10074 *reloc = percent_op[i].reloc;
10075
10076 /* Check whether the output BFD supports this relocation.
10077 If not, issue an error and fall back on something safe. */
10078 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10079 {
10080 as_bad ("relocation %s isn't supported by the current ABI",
10081 percent_op[i].str);
10082 *reloc = BFD_RELOC_LO16;
10083 }
10084 return TRUE;
10085 }
10086 return FALSE;
10087 }
10088
10089
10090 /* Parse string STR as a 16-bit relocatable operand. Store the
10091 expression in *EP and the relocations in the array starting
10092 at RELOC. Return the number of relocation operators used.
10093
10094 On exit, EXPR_END points to the first character after the expression.
10095 If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16. */
10096
10097 static size_t
10098 my_getSmallExpression (ep, reloc, str)
10099 expressionS *ep;
10100 bfd_reloc_code_real_type *reloc;
10101 char *str;
10102 {
10103 bfd_reloc_code_real_type reversed_reloc[3];
10104 size_t reloc_index, i;
10105 int crux_depth, str_depth;
10106 char *crux;
10107
10108 /* Search for the start of the main expression, recoding relocations
10109 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10110 of the main expression and with CRUX_DEPTH containing the number
10111 of open brackets at that point. */
10112 reloc_index = -1;
10113 str_depth = 0;
10114 do
10115 {
10116 reloc_index++;
10117 crux = str;
10118 crux_depth = str_depth;
10119
10120 /* Skip over whitespace and brackets, keeping count of the number
10121 of brackets. */
10122 while (*str == ' ' || *str == '\t' || *str == '(')
10123 if (*str++ == '(')
10124 str_depth++;
10125 }
10126 while (*str == '%'
10127 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10128 && parse_relocation (&str, &reversed_reloc[reloc_index]));
10129
10130 my_getExpression (ep, crux);
10131 str = expr_end;
10132
10133 /* Match every open bracket. */
10134 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10135 if (*str++ == ')')
10136 crux_depth--;
10137
10138 if (crux_depth > 0)
10139 as_bad ("unclosed '('");
10140
10141 expr_end = str;
10142
10143 if (reloc_index == 0)
10144 reloc[0] = BFD_RELOC_LO16;
10145 else
10146 {
10147 prev_reloc_op_frag = frag_now;
10148 for (i = 0; i < reloc_index; i++)
10149 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10150 }
10151
10152 return reloc_index;
10153 }
10154
10155 static void
10156 my_getExpression (ep, str)
10157 expressionS *ep;
10158 char *str;
10159 {
10160 char *save_in;
10161 valueT val;
10162
10163 save_in = input_line_pointer;
10164 input_line_pointer = str;
10165 expression (ep);
10166 expr_end = input_line_pointer;
10167 input_line_pointer = save_in;
10168
10169 /* If we are in mips16 mode, and this is an expression based on `.',
10170 then we bump the value of the symbol by 1 since that is how other
10171 text symbols are handled. We don't bother to handle complex
10172 expressions, just `.' plus or minus a constant. */
10173 if (mips_opts.mips16
10174 && ep->X_op == O_symbol
10175 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10176 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10177 && symbol_get_frag (ep->X_add_symbol) == frag_now
10178 && symbol_constant_p (ep->X_add_symbol)
10179 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10180 S_SET_VALUE (ep->X_add_symbol, val + 1);
10181 }
10182
10183 /* Turn a string in input_line_pointer into a floating point constant
10184 of type TYPE, and store the appropriate bytes in *LITP. The number
10185 of LITTLENUMS emitted is stored in *SIZEP. An error message is
10186 returned, or NULL on OK. */
10187
10188 char *
10189 md_atof (type, litP, sizeP)
10190 int type;
10191 char *litP;
10192 int *sizeP;
10193 {
10194 int prec;
10195 LITTLENUM_TYPE words[4];
10196 char *t;
10197 int i;
10198
10199 switch (type)
10200 {
10201 case 'f':
10202 prec = 2;
10203 break;
10204
10205 case 'd':
10206 prec = 4;
10207 break;
10208
10209 default:
10210 *sizeP = 0;
10211 return _("bad call to md_atof");
10212 }
10213
10214 t = atof_ieee (input_line_pointer, type, words);
10215 if (t)
10216 input_line_pointer = t;
10217
10218 *sizeP = prec * 2;
10219
10220 if (! target_big_endian)
10221 {
10222 for (i = prec - 1; i >= 0; i--)
10223 {
10224 md_number_to_chars (litP, (valueT) words[i], 2);
10225 litP += 2;
10226 }
10227 }
10228 else
10229 {
10230 for (i = 0; i < prec; i++)
10231 {
10232 md_number_to_chars (litP, (valueT) words[i], 2);
10233 litP += 2;
10234 }
10235 }
10236
10237 return NULL;
10238 }
10239
10240 void
10241 md_number_to_chars (buf, val, n)
10242 char *buf;
10243 valueT val;
10244 int n;
10245 {
10246 if (target_big_endian)
10247 number_to_chars_bigendian (buf, val, n);
10248 else
10249 number_to_chars_littleendian (buf, val, n);
10250 }
10251 \f
10252 #ifdef OBJ_ELF
10253 static int support_64bit_objects(void)
10254 {
10255 const char **list, **l;
10256 int yes;
10257
10258 list = bfd_target_list ();
10259 for (l = list; *l != NULL; l++)
10260 #ifdef TE_TMIPS
10261 /* This is traditional mips */
10262 if (strcmp (*l, "elf64-tradbigmips") == 0
10263 || strcmp (*l, "elf64-tradlittlemips") == 0)
10264 #else
10265 if (strcmp (*l, "elf64-bigmips") == 0
10266 || strcmp (*l, "elf64-littlemips") == 0)
10267 #endif
10268 break;
10269 yes = (*l != NULL);
10270 free (list);
10271 return yes;
10272 }
10273 #endif /* OBJ_ELF */
10274
10275 const char *md_shortopts = "nO::g::G:";
10276
10277 struct option md_longopts[] =
10278 {
10279 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10280 {"mips0", no_argument, NULL, OPTION_MIPS1},
10281 {"mips1", no_argument, NULL, OPTION_MIPS1},
10282 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10283 {"mips2", no_argument, NULL, OPTION_MIPS2},
10284 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10285 {"mips3", no_argument, NULL, OPTION_MIPS3},
10286 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10287 {"mips4", no_argument, NULL, OPTION_MIPS4},
10288 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10289 {"mips5", no_argument, NULL, OPTION_MIPS5},
10290 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10291 {"mips32", no_argument, NULL, OPTION_MIPS32},
10292 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10293 {"mips64", no_argument, NULL, OPTION_MIPS64},
10294 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
10295 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10296 #define OPTION_TRAP (OPTION_MD_BASE + 9)
10297 {"trap", no_argument, NULL, OPTION_TRAP},
10298 {"no-break", no_argument, NULL, OPTION_TRAP},
10299 #define OPTION_BREAK (OPTION_MD_BASE + 10)
10300 {"break", no_argument, NULL, OPTION_BREAK},
10301 {"no-trap", no_argument, NULL, OPTION_BREAK},
10302 #define OPTION_EB (OPTION_MD_BASE + 11)
10303 {"EB", no_argument, NULL, OPTION_EB},
10304 #define OPTION_EL (OPTION_MD_BASE + 12)
10305 {"EL", no_argument, NULL, OPTION_EL},
10306 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
10307 {"mips16", no_argument, NULL, OPTION_MIPS16},
10308 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
10309 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10310 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
10311 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10312 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10313 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10314 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10315 #define OPTION_FP32 (OPTION_MD_BASE + 17)
10316 {"mfp32", no_argument, NULL, OPTION_FP32},
10317 #define OPTION_GP32 (OPTION_MD_BASE + 18)
10318 {"mgp32", no_argument, NULL, OPTION_GP32},
10319 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
10320 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10321 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
10322 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10323 #define OPTION_MARCH (OPTION_MD_BASE + 21)
10324 {"march", required_argument, NULL, OPTION_MARCH},
10325 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
10326 {"mtune", required_argument, NULL, OPTION_MTUNE},
10327 #define OPTION_FP64 (OPTION_MD_BASE + 23)
10328 {"mfp64", no_argument, NULL, OPTION_FP64},
10329 #define OPTION_M4650 (OPTION_MD_BASE + 24)
10330 {"m4650", no_argument, NULL, OPTION_M4650},
10331 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10332 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10333 #define OPTION_M4010 (OPTION_MD_BASE + 26)
10334 {"m4010", no_argument, NULL, OPTION_M4010},
10335 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10336 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10337 #define OPTION_M4100 (OPTION_MD_BASE + 28)
10338 {"m4100", no_argument, NULL, OPTION_M4100},
10339 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10340 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10341 #define OPTION_M3900 (OPTION_MD_BASE + 30)
10342 {"m3900", no_argument, NULL, OPTION_M3900},
10343 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10344 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10345 #define OPTION_GP64 (OPTION_MD_BASE + 32)
10346 {"mgp64", no_argument, NULL, OPTION_GP64},
10347 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10348 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10349 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10350 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10351 #define OPTION_MDMX (OPTION_MD_BASE + 35)
10352 {"mdmx", no_argument, NULL, OPTION_MDMX},
10353 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10354 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10355 #define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10356 #define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10357 {"mfix-vr4122-bugs", no_argument, NULL, OPTION_FIX_VR4122},
10358 {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10359 #define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10360 #define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10361 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10362 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10363 #define OPTION_MIPS32R2 (OPTION_MD_BASE + 41)
10364 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10365 #ifdef OBJ_ELF
10366 #define OPTION_ELF_BASE (OPTION_MD_BASE + 42)
10367 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10368 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10369 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10370 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10371 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10372 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10373 {"xgot", no_argument, NULL, OPTION_XGOT},
10374 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10375 {"mabi", required_argument, NULL, OPTION_MABI},
10376 #define OPTION_32 (OPTION_ELF_BASE + 4)
10377 {"32", no_argument, NULL, OPTION_32},
10378 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10379 {"n32", no_argument, NULL, OPTION_N32},
10380 #define OPTION_64 (OPTION_ELF_BASE + 6)
10381 {"64", no_argument, NULL, OPTION_64},
10382 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10383 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10384 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10385 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10386 #endif /* OBJ_ELF */
10387 {NULL, no_argument, NULL, 0}
10388 };
10389 size_t md_longopts_size = sizeof (md_longopts);
10390
10391 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10392 NEW_VALUE. Warn if another value was already specified. Note:
10393 we have to defer parsing the -march and -mtune arguments in order
10394 to handle 'from-abi' correctly, since the ABI might be specified
10395 in a later argument. */
10396
10397 static void
10398 mips_set_option_string (string_ptr, new_value)
10399 const char **string_ptr, *new_value;
10400 {
10401 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10402 as_warn (_("A different %s was already specified, is now %s"),
10403 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10404 new_value);
10405
10406 *string_ptr = new_value;
10407 }
10408
10409 int
10410 md_parse_option (c, arg)
10411 int c;
10412 char *arg;
10413 {
10414 switch (c)
10415 {
10416 case OPTION_CONSTRUCT_FLOATS:
10417 mips_disable_float_construction = 0;
10418 break;
10419
10420 case OPTION_NO_CONSTRUCT_FLOATS:
10421 mips_disable_float_construction = 1;
10422 break;
10423
10424 case OPTION_TRAP:
10425 mips_trap = 1;
10426 break;
10427
10428 case OPTION_BREAK:
10429 mips_trap = 0;
10430 break;
10431
10432 case OPTION_EB:
10433 target_big_endian = 1;
10434 break;
10435
10436 case OPTION_EL:
10437 target_big_endian = 0;
10438 break;
10439
10440 case 'n':
10441 warn_nops = 1;
10442 break;
10443
10444 case 'O':
10445 if (arg && arg[1] == '0')
10446 mips_optimize = 1;
10447 else
10448 mips_optimize = 2;
10449 break;
10450
10451 case 'g':
10452 if (arg == NULL)
10453 mips_debug = 2;
10454 else
10455 mips_debug = atoi (arg);
10456 /* When the MIPS assembler sees -g or -g2, it does not do
10457 optimizations which limit full symbolic debugging. We take
10458 that to be equivalent to -O0. */
10459 if (mips_debug == 2)
10460 mips_optimize = 1;
10461 break;
10462
10463 case OPTION_MIPS1:
10464 file_mips_isa = ISA_MIPS1;
10465 break;
10466
10467 case OPTION_MIPS2:
10468 file_mips_isa = ISA_MIPS2;
10469 break;
10470
10471 case OPTION_MIPS3:
10472 file_mips_isa = ISA_MIPS3;
10473 break;
10474
10475 case OPTION_MIPS4:
10476 file_mips_isa = ISA_MIPS4;
10477 break;
10478
10479 case OPTION_MIPS5:
10480 file_mips_isa = ISA_MIPS5;
10481 break;
10482
10483 case OPTION_MIPS32:
10484 file_mips_isa = ISA_MIPS32;
10485 break;
10486
10487 case OPTION_MIPS32R2:
10488 file_mips_isa = ISA_MIPS32R2;
10489 break;
10490
10491 case OPTION_MIPS64:
10492 file_mips_isa = ISA_MIPS64;
10493 break;
10494
10495 case OPTION_MTUNE:
10496 mips_set_option_string (&mips_tune_string, arg);
10497 break;
10498
10499 case OPTION_MARCH:
10500 mips_set_option_string (&mips_arch_string, arg);
10501 break;
10502
10503 case OPTION_M4650:
10504 mips_set_option_string (&mips_arch_string, "4650");
10505 mips_set_option_string (&mips_tune_string, "4650");
10506 break;
10507
10508 case OPTION_NO_M4650:
10509 break;
10510
10511 case OPTION_M4010:
10512 mips_set_option_string (&mips_arch_string, "4010");
10513 mips_set_option_string (&mips_tune_string, "4010");
10514 break;
10515
10516 case OPTION_NO_M4010:
10517 break;
10518
10519 case OPTION_M4100:
10520 mips_set_option_string (&mips_arch_string, "4100");
10521 mips_set_option_string (&mips_tune_string, "4100");
10522 break;
10523
10524 case OPTION_NO_M4100:
10525 break;
10526
10527 case OPTION_M3900:
10528 mips_set_option_string (&mips_arch_string, "3900");
10529 mips_set_option_string (&mips_tune_string, "3900");
10530 break;
10531
10532 case OPTION_NO_M3900:
10533 break;
10534
10535 case OPTION_MDMX:
10536 mips_opts.ase_mdmx = 1;
10537 break;
10538
10539 case OPTION_NO_MDMX:
10540 mips_opts.ase_mdmx = 0;
10541 break;
10542
10543 case OPTION_MIPS16:
10544 mips_opts.mips16 = 1;
10545 mips_no_prev_insn (FALSE);
10546 break;
10547
10548 case OPTION_NO_MIPS16:
10549 mips_opts.mips16 = 0;
10550 mips_no_prev_insn (FALSE);
10551 break;
10552
10553 case OPTION_MIPS3D:
10554 mips_opts.ase_mips3d = 1;
10555 break;
10556
10557 case OPTION_NO_MIPS3D:
10558 mips_opts.ase_mips3d = 0;
10559 break;
10560
10561 case OPTION_MEMBEDDED_PIC:
10562 mips_pic = EMBEDDED_PIC;
10563 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10564 {
10565 as_bad (_("-G may not be used with embedded PIC code"));
10566 return 0;
10567 }
10568 g_switch_value = 0x7fffffff;
10569 break;
10570
10571 case OPTION_FIX_VR4122:
10572 mips_fix_4122_bugs = 1;
10573 break;
10574
10575 case OPTION_NO_FIX_VR4122:
10576 mips_fix_4122_bugs = 0;
10577 break;
10578
10579 case OPTION_RELAX_BRANCH:
10580 mips_relax_branch = 1;
10581 break;
10582
10583 case OPTION_NO_RELAX_BRANCH:
10584 mips_relax_branch = 0;
10585 break;
10586
10587 #ifdef OBJ_ELF
10588 /* When generating ELF code, we permit -KPIC and -call_shared to
10589 select SVR4_PIC, and -non_shared to select no PIC. This is
10590 intended to be compatible with Irix 5. */
10591 case OPTION_CALL_SHARED:
10592 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10593 {
10594 as_bad (_("-call_shared is supported only for ELF format"));
10595 return 0;
10596 }
10597 mips_pic = SVR4_PIC;
10598 if (g_switch_seen && g_switch_value != 0)
10599 {
10600 as_bad (_("-G may not be used with SVR4 PIC code"));
10601 return 0;
10602 }
10603 g_switch_value = 0;
10604 break;
10605
10606 case OPTION_NON_SHARED:
10607 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10608 {
10609 as_bad (_("-non_shared is supported only for ELF format"));
10610 return 0;
10611 }
10612 mips_pic = NO_PIC;
10613 break;
10614
10615 /* The -xgot option tells the assembler to use 32 offsets when
10616 accessing the got in SVR4_PIC mode. It is for Irix
10617 compatibility. */
10618 case OPTION_XGOT:
10619 mips_big_got = 1;
10620 break;
10621 #endif /* OBJ_ELF */
10622
10623 case 'G':
10624 if (! USE_GLOBAL_POINTER_OPT)
10625 {
10626 as_bad (_("-G is not supported for this configuration"));
10627 return 0;
10628 }
10629 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10630 {
10631 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10632 return 0;
10633 }
10634 else
10635 g_switch_value = atoi (arg);
10636 g_switch_seen = 1;
10637 break;
10638
10639 #ifdef OBJ_ELF
10640 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10641 and -mabi=64. */
10642 case OPTION_32:
10643 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10644 {
10645 as_bad (_("-32 is supported for ELF format only"));
10646 return 0;
10647 }
10648 mips_abi = O32_ABI;
10649 break;
10650
10651 case OPTION_N32:
10652 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10653 {
10654 as_bad (_("-n32 is supported for ELF format only"));
10655 return 0;
10656 }
10657 mips_abi = N32_ABI;
10658 break;
10659
10660 case OPTION_64:
10661 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10662 {
10663 as_bad (_("-64 is supported for ELF format only"));
10664 return 0;
10665 }
10666 mips_abi = N64_ABI;
10667 if (! support_64bit_objects())
10668 as_fatal (_("No compiled in support for 64 bit object file format"));
10669 break;
10670 #endif /* OBJ_ELF */
10671
10672 case OPTION_GP32:
10673 file_mips_gp32 = 1;
10674 break;
10675
10676 case OPTION_GP64:
10677 file_mips_gp32 = 0;
10678 break;
10679
10680 case OPTION_FP32:
10681 file_mips_fp32 = 1;
10682 break;
10683
10684 case OPTION_FP64:
10685 file_mips_fp32 = 0;
10686 break;
10687
10688 #ifdef OBJ_ELF
10689 case OPTION_MABI:
10690 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10691 {
10692 as_bad (_("-mabi is supported for ELF format only"));
10693 return 0;
10694 }
10695 if (strcmp (arg, "32") == 0)
10696 mips_abi = O32_ABI;
10697 else if (strcmp (arg, "o64") == 0)
10698 mips_abi = O64_ABI;
10699 else if (strcmp (arg, "n32") == 0)
10700 mips_abi = N32_ABI;
10701 else if (strcmp (arg, "64") == 0)
10702 {
10703 mips_abi = N64_ABI;
10704 if (! support_64bit_objects())
10705 as_fatal (_("No compiled in support for 64 bit object file "
10706 "format"));
10707 }
10708 else if (strcmp (arg, "eabi") == 0)
10709 mips_abi = EABI_ABI;
10710 else
10711 {
10712 as_fatal (_("invalid abi -mabi=%s"), arg);
10713 return 0;
10714 }
10715 break;
10716 #endif /* OBJ_ELF */
10717
10718 case OPTION_M7000_HILO_FIX:
10719 mips_7000_hilo_fix = TRUE;
10720 break;
10721
10722 case OPTION_MNO_7000_HILO_FIX:
10723 mips_7000_hilo_fix = FALSE;
10724 break;
10725
10726 #ifdef OBJ_ELF
10727 case OPTION_MDEBUG:
10728 mips_flag_mdebug = TRUE;
10729 break;
10730
10731 case OPTION_NO_MDEBUG:
10732 mips_flag_mdebug = FALSE;
10733 break;
10734 #endif /* OBJ_ELF */
10735
10736 default:
10737 return 0;
10738 }
10739
10740 return 1;
10741 }
10742 \f
10743 /* Set up globals to generate code for the ISA or processor
10744 described by INFO. */
10745
10746 static void
10747 mips_set_architecture (info)
10748 const struct mips_cpu_info *info;
10749 {
10750 if (info != 0)
10751 {
10752 mips_arch_info = info;
10753 mips_arch = info->cpu;
10754 mips_opts.isa = info->isa;
10755 }
10756 }
10757
10758
10759 /* Likewise for tuning. */
10760
10761 static void
10762 mips_set_tune (info)
10763 const struct mips_cpu_info *info;
10764 {
10765 if (info != 0)
10766 {
10767 mips_tune_info = info;
10768 mips_tune = info->cpu;
10769 }
10770 }
10771
10772
10773 void
10774 mips_after_parse_args ()
10775 {
10776 /* GP relative stuff not working for PE */
10777 if (strncmp (TARGET_OS, "pe", 2) == 0
10778 && g_switch_value != 0)
10779 {
10780 if (g_switch_seen)
10781 as_bad (_("-G not supported in this configuration."));
10782 g_switch_value = 0;
10783 }
10784
10785 /* The following code determines the architecture and register size.
10786 Similar code was added to GCC 3.3 (see override_options() in
10787 config/mips/mips.c). The GAS and GCC code should be kept in sync
10788 as much as possible. */
10789
10790 if (mips_arch_string != 0)
10791 mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10792
10793 if (mips_tune_string != 0)
10794 mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10795
10796 if (file_mips_isa != ISA_UNKNOWN)
10797 {
10798 /* Handle -mipsN. At this point, file_mips_isa contains the
10799 ISA level specified by -mipsN, while mips_opts.isa contains
10800 the -march selection (if any). */
10801 if (mips_arch_info != 0)
10802 {
10803 /* -march takes precedence over -mipsN, since it is more descriptive.
10804 There's no harm in specifying both as long as the ISA levels
10805 are the same. */
10806 if (file_mips_isa != mips_opts.isa)
10807 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10808 mips_cpu_info_from_isa (file_mips_isa)->name,
10809 mips_cpu_info_from_isa (mips_opts.isa)->name);
10810 }
10811 else
10812 mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10813 }
10814
10815 if (mips_arch_info == 0)
10816 mips_set_architecture (mips_parse_cpu ("default CPU",
10817 MIPS_CPU_STRING_DEFAULT));
10818
10819 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10820 as_bad ("-march=%s is not compatible with the selected ABI",
10821 mips_arch_info->name);
10822
10823 /* Optimize for mips_arch, unless -mtune selects a different processor. */
10824 if (mips_tune_info == 0)
10825 mips_set_tune (mips_arch_info);
10826
10827 if (file_mips_gp32 >= 0)
10828 {
10829 /* The user specified the size of the integer registers. Make sure
10830 it agrees with the ABI and ISA. */
10831 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10832 as_bad (_("-mgp64 used with a 32-bit processor"));
10833 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10834 as_bad (_("-mgp32 used with a 64-bit ABI"));
10835 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10836 as_bad (_("-mgp64 used with a 32-bit ABI"));
10837 }
10838 else
10839 {
10840 /* Infer the integer register size from the ABI and processor.
10841 Restrict ourselves to 32-bit registers if that's all the
10842 processor has, or if the ABI cannot handle 64-bit registers. */
10843 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10844 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10845 }
10846
10847 /* ??? GAS treats single-float processors as though they had 64-bit
10848 float registers (although it complains when double-precision
10849 instructions are used). As things stand, saying they have 32-bit
10850 registers would lead to spurious "register must be even" messages.
10851 So here we assume float registers are always the same size as
10852 integer ones, unless the user says otherwise. */
10853 if (file_mips_fp32 < 0)
10854 file_mips_fp32 = file_mips_gp32;
10855
10856 /* End of GCC-shared inference code. */
10857
10858 /* ??? When do we want this flag to be set? Who uses it? */
10859 if (file_mips_gp32 == 1
10860 && mips_abi == NO_ABI
10861 && ISA_HAS_64BIT_REGS (mips_opts.isa))
10862 mips_32bitmode = 1;
10863
10864 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10865 as_bad (_("trap exception not supported at ISA 1"));
10866
10867 /* If the selected architecture includes support for ASEs, enable
10868 generation of code for them. */
10869 if (mips_opts.mips16 == -1)
10870 mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10871 if (mips_opts.ase_mips3d == -1)
10872 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10873 if (mips_opts.ase_mdmx == -1)
10874 mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10875
10876 file_mips_isa = mips_opts.isa;
10877 file_ase_mips16 = mips_opts.mips16;
10878 file_ase_mips3d = mips_opts.ase_mips3d;
10879 file_ase_mdmx = mips_opts.ase_mdmx;
10880 mips_opts.gp32 = file_mips_gp32;
10881 mips_opts.fp32 = file_mips_fp32;
10882
10883 if (mips_flag_mdebug < 0)
10884 {
10885 #ifdef OBJ_MAYBE_ECOFF
10886 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10887 mips_flag_mdebug = 1;
10888 else
10889 #endif /* OBJ_MAYBE_ECOFF */
10890 mips_flag_mdebug = 0;
10891 }
10892 }
10893 \f
10894 void
10895 mips_init_after_args ()
10896 {
10897 /* initialize opcodes */
10898 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10899 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10900 }
10901
10902 long
10903 md_pcrel_from (fixP)
10904 fixS *fixP;
10905 {
10906 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10907 && fixP->fx_addsy != (symbolS *) NULL
10908 && ! S_IS_DEFINED (fixP->fx_addsy))
10909 return 4;
10910
10911 /* Return the address of the delay slot. */
10912 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10913 }
10914
10915 /* This is called before the symbol table is processed. In order to
10916 work with gcc when using mips-tfile, we must keep all local labels.
10917 However, in other cases, we want to discard them. If we were
10918 called with -g, but we didn't see any debugging information, it may
10919 mean that gcc is smuggling debugging information through to
10920 mips-tfile, in which case we must generate all local labels. */
10921
10922 void
10923 mips_frob_file_before_adjust ()
10924 {
10925 #ifndef NO_ECOFF_DEBUGGING
10926 if (ECOFF_DEBUGGING
10927 && mips_debug != 0
10928 && ! ecoff_debugging_seen)
10929 flag_keep_locals = 1;
10930 #endif
10931 }
10932
10933 /* Sort any unmatched HI16_S relocs so that they immediately precede
10934 the corresponding LO reloc. This is called before md_apply_fix3 and
10935 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10936 explicit use of the %hi modifier. */
10937
10938 void
10939 mips_frob_file ()
10940 {
10941 struct mips_hi_fixup *l;
10942
10943 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10944 {
10945 segment_info_type *seginfo;
10946 int pass;
10947
10948 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10949
10950 /* If a GOT16 relocation turns out to be against a global symbol,
10951 there isn't supposed to be a matching LO. */
10952 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10953 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10954 continue;
10955
10956 /* Check quickly whether the next fixup happens to be a matching %lo. */
10957 if (fixup_has_matching_lo_p (l->fixp))
10958 continue;
10959
10960 /* Look through the fixups for this segment for a matching %lo.
10961 When we find one, move the %hi just in front of it. We do
10962 this in two passes. In the first pass, we try to find a
10963 unique %lo. In the second pass, we permit multiple %hi
10964 relocs for a single %lo (this is a GNU extension). */
10965 seginfo = seg_info (l->seg);
10966 for (pass = 0; pass < 2; pass++)
10967 {
10968 fixS *f, *prev;
10969
10970 prev = NULL;
10971 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10972 {
10973 /* Check whether this is a %lo fixup which matches l->fixp. */
10974 if (f->fx_r_type == BFD_RELOC_LO16
10975 && f->fx_addsy == l->fixp->fx_addsy
10976 && f->fx_offset == l->fixp->fx_offset
10977 && (pass == 1
10978 || prev == NULL
10979 || !reloc_needs_lo_p (prev->fx_r_type)
10980 || !fixup_has_matching_lo_p (prev)))
10981 {
10982 fixS **pf;
10983
10984 /* Move l->fixp before f. */
10985 for (pf = &seginfo->fix_root;
10986 *pf != l->fixp;
10987 pf = &(*pf)->fx_next)
10988 assert (*pf != NULL);
10989
10990 *pf = l->fixp->fx_next;
10991
10992 l->fixp->fx_next = f;
10993 if (prev == NULL)
10994 seginfo->fix_root = l->fixp;
10995 else
10996 prev->fx_next = l->fixp;
10997
10998 break;
10999 }
11000
11001 prev = f;
11002 }
11003
11004 if (f != NULL)
11005 break;
11006
11007 #if 0 /* GCC code motion plus incomplete dead code elimination
11008 can leave a %hi without a %lo. */
11009 if (pass == 1)
11010 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11011 _("Unmatched %%hi reloc"));
11012 #endif
11013 }
11014 }
11015 }
11016
11017 /* When generating embedded PIC code we need to use a special
11018 relocation to represent the difference of two symbols in the .text
11019 section (switch tables use a difference of this sort). See
11020 include/coff/mips.h for details. This macro checks whether this
11021 fixup requires the special reloc. */
11022 #define SWITCH_TABLE(fixp) \
11023 ((fixp)->fx_r_type == BFD_RELOC_32 \
11024 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
11025 && (fixp)->fx_addsy != NULL \
11026 && (fixp)->fx_subsy != NULL \
11027 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11028 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11029
11030 /* When generating embedded PIC code we must keep all PC relative
11031 relocations, in case the linker has to relax a call. We also need
11032 to keep relocations for switch table entries.
11033
11034 We may have combined relocations without symbols in the N32/N64 ABI.
11035 We have to prevent gas from dropping them. */
11036
11037 int
11038 mips_force_relocation (fixp)
11039 fixS *fixp;
11040 {
11041 if (generic_force_reloc (fixp))
11042 return 1;
11043
11044 if (HAVE_NEWABI
11045 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11046 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11047 || fixp->fx_r_type == BFD_RELOC_HI16_S
11048 || fixp->fx_r_type == BFD_RELOC_LO16))
11049 return 1;
11050
11051 return (mips_pic == EMBEDDED_PIC
11052 && (fixp->fx_pcrel
11053 || SWITCH_TABLE (fixp)
11054 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11055 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11056 }
11057
11058 #ifdef OBJ_ELF
11059 static int
11060 mips_need_elf_addend_fixup (fixP)
11061 fixS *fixP;
11062 {
11063 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11064 return 1;
11065 if (mips_pic == EMBEDDED_PIC
11066 && S_IS_WEAK (fixP->fx_addsy))
11067 return 1;
11068 if (mips_pic != EMBEDDED_PIC
11069 && (S_IS_WEAK (fixP->fx_addsy)
11070 || S_IS_EXTERNAL (fixP->fx_addsy))
11071 && !S_IS_COMMON (fixP->fx_addsy))
11072 return 1;
11073 if (symbol_used_in_reloc_p (fixP->fx_addsy)
11074 && (((bfd_get_section_flags (stdoutput,
11075 S_GET_SEGMENT (fixP->fx_addsy))
11076 & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11077 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11078 ".gnu.linkonce",
11079 sizeof (".gnu.linkonce") - 1)))
11080 return 1;
11081 return 0;
11082 }
11083 #endif
11084
11085 /* Apply a fixup to the object file. */
11086
11087 void
11088 md_apply_fix3 (fixP, valP, seg)
11089 fixS *fixP;
11090 valueT *valP;
11091 segT seg ATTRIBUTE_UNUSED;
11092 {
11093 bfd_byte *buf;
11094 long insn;
11095 valueT value;
11096 static int previous_fx_r_type = 0;
11097
11098 /* FIXME: Maybe just return for all reloc types not listed below?
11099 Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
11100 if (fixP->fx_r_type == BFD_RELOC_8)
11101 return;
11102
11103 assert (fixP->fx_size == 4
11104 || fixP->fx_r_type == BFD_RELOC_16
11105 || fixP->fx_r_type == BFD_RELOC_32
11106 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11107 || fixP->fx_r_type == BFD_RELOC_HI16_S
11108 || fixP->fx_r_type == BFD_RELOC_LO16
11109 || fixP->fx_r_type == BFD_RELOC_GPREL16
11110 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
11111 || fixP->fx_r_type == BFD_RELOC_GPREL32
11112 || fixP->fx_r_type == BFD_RELOC_64
11113 || fixP->fx_r_type == BFD_RELOC_CTOR
11114 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11115 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11116 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11117 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11118 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11119 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
11120 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11121 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11122 || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
11123
11124 value = *valP;
11125
11126 /* If we aren't adjusting this fixup to be against the section
11127 symbol, we need to adjust the value. */
11128 #ifdef OBJ_ELF
11129 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11130 {
11131 if (mips_need_elf_addend_fixup (fixP))
11132 {
11133 reloc_howto_type *howto;
11134 valueT symval = S_GET_VALUE (fixP->fx_addsy);
11135
11136 value -= symval;
11137
11138 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11139 if (value != 0 && howto && howto->partial_inplace
11140 && (! fixP->fx_pcrel || howto->pcrel_offset))
11141 {
11142 /* In this case, the bfd_install_relocation routine will
11143 incorrectly add the symbol value back in. We just want
11144 the addend to appear in the object file.
11145
11146 howto->pcrel_offset is added for R_MIPS_PC16, which is
11147 generated for code like
11148
11149 globl g1 .text
11150 .text
11151 .space 20
11152 g1:
11153 x:
11154 bal g1
11155 */
11156 value -= symval;
11157
11158 /* Make sure the addend is still non-zero. If it became zero
11159 after the last operation, set it to a spurious value and
11160 subtract the same value from the object file's contents. */
11161 if (value == 0)
11162 {
11163 value = 8;
11164
11165 /* The in-place addends for LO16 relocations are signed;
11166 leave the matching HI16 in-place addends as zero. */
11167 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11168 {
11169 bfd_vma contents, mask, field;
11170
11171 contents = bfd_get_bits (fixP->fx_frag->fr_literal
11172 + fixP->fx_where,
11173 fixP->fx_size * 8,
11174 target_big_endian);
11175
11176 /* MASK has bits set where the relocation should go.
11177 FIELD is -value, shifted into the appropriate place
11178 for this relocation. */
11179 mask = 1 << (howto->bitsize - 1);
11180 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11181 field = (-value >> howto->rightshift) << howto->bitpos;
11182
11183 bfd_put_bits ((field & mask) | (contents & ~mask),
11184 fixP->fx_frag->fr_literal + fixP->fx_where,
11185 fixP->fx_size * 8,
11186 target_big_endian);
11187 }
11188 }
11189 }
11190 }
11191
11192 /* This code was generated using trial and error and so is
11193 fragile and not trustworthy. If you change it, you should
11194 rerun the elf-rel, elf-rel2, and empic testcases and ensure
11195 they still pass. */
11196 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11197 {
11198 value += fixP->fx_frag->fr_address + fixP->fx_where;
11199
11200 /* BFD's REL handling, for MIPS, is _very_ weird.
11201 This gives the right results, but it can't possibly
11202 be the way things are supposed to work. */
11203 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11204 && fixP->fx_r_type != BFD_RELOC_MIPSEMB_16_PCREL_S2)
11205 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11206 value += fixP->fx_frag->fr_address + fixP->fx_where;
11207 }
11208 }
11209 #endif
11210
11211 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
11212
11213 /* We are not done if this is a composite relocation to set up gp. */
11214 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11215 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11216 || (fixP->fx_r_type == BFD_RELOC_64
11217 && (previous_fx_r_type == BFD_RELOC_GPREL32
11218 || previous_fx_r_type == BFD_RELOC_GPREL16))
11219 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11220 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11221 || fixP->fx_r_type == BFD_RELOC_LO16))))
11222 fixP->fx_done = 1;
11223 previous_fx_r_type = fixP->fx_r_type;
11224
11225 switch (fixP->fx_r_type)
11226 {
11227 case BFD_RELOC_MIPS_JMP:
11228 case BFD_RELOC_MIPS_SHIFT5:
11229 case BFD_RELOC_MIPS_SHIFT6:
11230 case BFD_RELOC_MIPS_GOT_DISP:
11231 case BFD_RELOC_MIPS_GOT_PAGE:
11232 case BFD_RELOC_MIPS_GOT_OFST:
11233 case BFD_RELOC_MIPS_SUB:
11234 case BFD_RELOC_MIPS_INSERT_A:
11235 case BFD_RELOC_MIPS_INSERT_B:
11236 case BFD_RELOC_MIPS_DELETE:
11237 case BFD_RELOC_MIPS_HIGHEST:
11238 case BFD_RELOC_MIPS_HIGHER:
11239 case BFD_RELOC_MIPS_SCN_DISP:
11240 case BFD_RELOC_MIPS_REL16:
11241 case BFD_RELOC_MIPS_RELGOT:
11242 case BFD_RELOC_MIPS_JALR:
11243 case BFD_RELOC_HI16:
11244 case BFD_RELOC_HI16_S:
11245 case BFD_RELOC_GPREL16:
11246 case BFD_RELOC_MIPS_LITERAL:
11247 case BFD_RELOC_MIPS_CALL16:
11248 case BFD_RELOC_MIPS_GOT16:
11249 case BFD_RELOC_GPREL32:
11250 case BFD_RELOC_MIPS_GOT_HI16:
11251 case BFD_RELOC_MIPS_GOT_LO16:
11252 case BFD_RELOC_MIPS_CALL_HI16:
11253 case BFD_RELOC_MIPS_CALL_LO16:
11254 case BFD_RELOC_MIPS16_GPREL:
11255 if (fixP->fx_pcrel)
11256 as_bad_where (fixP->fx_file, fixP->fx_line,
11257 _("Invalid PC relative reloc"));
11258 /* Nothing needed to do. The value comes from the reloc entry */
11259 break;
11260
11261 case BFD_RELOC_MIPS16_JMP:
11262 /* We currently always generate a reloc against a symbol, which
11263 means that we don't want an addend even if the symbol is
11264 defined. */
11265 fixP->fx_addnumber = 0;
11266 break;
11267
11268 case BFD_RELOC_PCREL_HI16_S:
11269 /* The addend for this is tricky if it is internal, so we just
11270 do everything here rather than in bfd_install_relocation. */
11271 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11272 && !fixP->fx_done
11273 && value != 0)
11274 break;
11275 if (fixP->fx_addsy
11276 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11277 {
11278 /* For an external symbol adjust by the address to make it
11279 pcrel_offset. We use the address of the RELLO reloc
11280 which follows this one. */
11281 value += (fixP->fx_next->fx_frag->fr_address
11282 + fixP->fx_next->fx_where);
11283 }
11284 value = ((value + 0x8000) >> 16) & 0xffff;
11285 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11286 if (target_big_endian)
11287 buf += 2;
11288 md_number_to_chars ((char *) buf, value, 2);
11289 break;
11290
11291 case BFD_RELOC_PCREL_LO16:
11292 /* The addend for this is tricky if it is internal, so we just
11293 do everything here rather than in bfd_install_relocation. */
11294 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11295 && !fixP->fx_done
11296 && value != 0)
11297 break;
11298 if (fixP->fx_addsy
11299 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11300 value += fixP->fx_frag->fr_address + fixP->fx_where;
11301 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11302 if (target_big_endian)
11303 buf += 2;
11304 md_number_to_chars ((char *) buf, value, 2);
11305 break;
11306
11307 case BFD_RELOC_64:
11308 /* This is handled like BFD_RELOC_32, but we output a sign
11309 extended value if we are only 32 bits. */
11310 if (fixP->fx_done
11311 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11312 {
11313 if (8 <= sizeof (valueT))
11314 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11315 value, 8);
11316 else
11317 {
11318 long w1, w2;
11319 long hiv;
11320
11321 w1 = w2 = fixP->fx_where;
11322 if (target_big_endian)
11323 w1 += 4;
11324 else
11325 w2 += 4;
11326 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11327 if ((value & 0x80000000) != 0)
11328 hiv = 0xffffffff;
11329 else
11330 hiv = 0;
11331 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11332 }
11333 }
11334 break;
11335
11336 case BFD_RELOC_RVA:
11337 case BFD_RELOC_32:
11338 /* If we are deleting this reloc entry, we must fill in the
11339 value now. This can happen if we have a .word which is not
11340 resolved when it appears but is later defined. We also need
11341 to fill in the value if this is an embedded PIC switch table
11342 entry. */
11343 if (fixP->fx_done
11344 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11345 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11346 value, 4);
11347 break;
11348
11349 case BFD_RELOC_16:
11350 /* If we are deleting this reloc entry, we must fill in the
11351 value now. */
11352 assert (fixP->fx_size == 2);
11353 if (fixP->fx_done)
11354 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11355 value, 2);
11356 break;
11357
11358 case BFD_RELOC_LO16:
11359 /* When handling an embedded PIC switch statement, we can wind
11360 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11361 if (fixP->fx_done)
11362 {
11363 if (value + 0x8000 > 0xffff)
11364 as_bad_where (fixP->fx_file, fixP->fx_line,
11365 _("relocation overflow"));
11366 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11367 if (target_big_endian)
11368 buf += 2;
11369 md_number_to_chars ((char *) buf, value, 2);
11370 }
11371 break;
11372
11373 case BFD_RELOC_16_PCREL_S2:
11374 case BFD_RELOC_MIPSEMB_16_PCREL_S2:
11375 if ((value & 0x3) != 0)
11376 as_bad_where (fixP->fx_file, fixP->fx_line,
11377 _("Branch to misaligned address (%lx)"), (long) value);
11378
11379 /*
11380 * We need to save the bits in the instruction since fixup_segment()
11381 * might be deleting the relocation entry (i.e., a branch within
11382 * the current segment).
11383 */
11384 if (!fixP->fx_done && (value != 0 || HAVE_NEWABI))
11385 break;
11386 /* If 'value' is zero, the remaining reloc code won't actually
11387 do the store, so it must be done here. This is probably
11388 a bug somewhere. */
11389 if (!fixP->fx_done
11390 && (fixP->fx_addsy == NULL /* ??? */
11391 || ! S_IS_DEFINED (fixP->fx_addsy)))
11392 value -= fixP->fx_frag->fr_address + fixP->fx_where;
11393
11394 value = (offsetT) value >> 2;
11395
11396 /* update old instruction data */
11397 buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11398 if (target_big_endian)
11399 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11400 else
11401 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11402
11403 if (value + 0x8000 <= 0xffff)
11404 insn |= value & 0xffff;
11405 else
11406 {
11407 /* The branch offset is too large. If this is an
11408 unconditional branch, and we are not generating PIC code,
11409 we can convert it to an absolute jump instruction. */
11410 if (mips_pic == NO_PIC
11411 && fixP->fx_done
11412 && fixP->fx_frag->fr_address >= text_section->vma
11413 && (fixP->fx_frag->fr_address
11414 < text_section->vma + text_section->_raw_size)
11415 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11416 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11417 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11418 {
11419 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11420 insn = 0x0c000000; /* jal */
11421 else
11422 insn = 0x08000000; /* j */
11423 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11424 fixP->fx_done = 0;
11425 fixP->fx_addsy = section_symbol (text_section);
11426 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11427 }
11428 else
11429 {
11430 /* If we got here, we have branch-relaxation disabled,
11431 and there's nothing we can do to fix this instruction
11432 without turning it into a longer sequence. */
11433 as_bad_where (fixP->fx_file, fixP->fx_line,
11434 _("Branch out of range"));
11435 }
11436 }
11437
11438 md_number_to_chars ((char *) buf, (valueT) insn, 4);
11439 break;
11440
11441 case BFD_RELOC_VTABLE_INHERIT:
11442 fixP->fx_done = 0;
11443 if (fixP->fx_addsy
11444 && !S_IS_DEFINED (fixP->fx_addsy)
11445 && !S_IS_WEAK (fixP->fx_addsy))
11446 S_SET_WEAK (fixP->fx_addsy);
11447 break;
11448
11449 case BFD_RELOC_VTABLE_ENTRY:
11450 fixP->fx_done = 0;
11451 break;
11452
11453 default:
11454 internalError ();
11455 }
11456 }
11457
11458 #if 0
11459 void
11460 printInsn (oc)
11461 unsigned long oc;
11462 {
11463 const struct mips_opcode *p;
11464 int treg, sreg, dreg, shamt;
11465 short imm;
11466 const char *args;
11467 int i;
11468
11469 for (i = 0; i < NUMOPCODES; ++i)
11470 {
11471 p = &mips_opcodes[i];
11472 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11473 {
11474 printf ("%08lx %s\t", oc, p->name);
11475 treg = (oc >> 16) & 0x1f;
11476 sreg = (oc >> 21) & 0x1f;
11477 dreg = (oc >> 11) & 0x1f;
11478 shamt = (oc >> 6) & 0x1f;
11479 imm = oc;
11480 for (args = p->args;; ++args)
11481 {
11482 switch (*args)
11483 {
11484 case '\0':
11485 printf ("\n");
11486 break;
11487
11488 case ',':
11489 case '(':
11490 case ')':
11491 printf ("%c", *args);
11492 continue;
11493
11494 case 'r':
11495 assert (treg == sreg);
11496 printf ("$%d,$%d", treg, sreg);
11497 continue;
11498
11499 case 'd':
11500 case 'G':
11501 printf ("$%d", dreg);
11502 continue;
11503
11504 case 't':
11505 case 'E':
11506 printf ("$%d", treg);
11507 continue;
11508
11509 case 'k':
11510 printf ("0x%x", treg);
11511 continue;
11512
11513 case 'b':
11514 case 's':
11515 printf ("$%d", sreg);
11516 continue;
11517
11518 case 'a':
11519 printf ("0x%08lx", oc & 0x1ffffff);
11520 continue;
11521
11522 case 'i':
11523 case 'j':
11524 case 'o':
11525 case 'u':
11526 printf ("%d", imm);
11527 continue;
11528
11529 case '<':
11530 case '>':
11531 printf ("$%d", shamt);
11532 continue;
11533
11534 default:
11535 internalError ();
11536 }
11537 break;
11538 }
11539 return;
11540 }
11541 }
11542 printf (_("%08lx UNDEFINED\n"), oc);
11543 }
11544 #endif
11545
11546 static symbolS *
11547 get_symbol ()
11548 {
11549 int c;
11550 char *name;
11551 symbolS *p;
11552
11553 name = input_line_pointer;
11554 c = get_symbol_end ();
11555 p = (symbolS *) symbol_find_or_make (name);
11556 *input_line_pointer = c;
11557 return p;
11558 }
11559
11560 /* Align the current frag to a given power of two. The MIPS assembler
11561 also automatically adjusts any preceding label. */
11562
11563 static void
11564 mips_align (to, fill, label)
11565 int to;
11566 int fill;
11567 symbolS *label;
11568 {
11569 mips_emit_delays (FALSE);
11570 frag_align (to, fill, 0);
11571 record_alignment (now_seg, to);
11572 if (label != NULL)
11573 {
11574 assert (S_GET_SEGMENT (label) == now_seg);
11575 symbol_set_frag (label, frag_now);
11576 S_SET_VALUE (label, (valueT) frag_now_fix ());
11577 }
11578 }
11579
11580 /* Align to a given power of two. .align 0 turns off the automatic
11581 alignment used by the data creating pseudo-ops. */
11582
11583 static void
11584 s_align (x)
11585 int x ATTRIBUTE_UNUSED;
11586 {
11587 register int temp;
11588 register long temp_fill;
11589 long max_alignment = 15;
11590
11591 /*
11592
11593 o Note that the assembler pulls down any immediately preceeding label
11594 to the aligned address.
11595 o It's not documented but auto alignment is reinstated by
11596 a .align pseudo instruction.
11597 o Note also that after auto alignment is turned off the mips assembler
11598 issues an error on attempt to assemble an improperly aligned data item.
11599 We don't.
11600
11601 */
11602
11603 temp = get_absolute_expression ();
11604 if (temp > max_alignment)
11605 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11606 else if (temp < 0)
11607 {
11608 as_warn (_("Alignment negative: 0 assumed."));
11609 temp = 0;
11610 }
11611 if (*input_line_pointer == ',')
11612 {
11613 ++input_line_pointer;
11614 temp_fill = get_absolute_expression ();
11615 }
11616 else
11617 temp_fill = 0;
11618 if (temp)
11619 {
11620 auto_align = 1;
11621 mips_align (temp, (int) temp_fill,
11622 insn_labels != NULL ? insn_labels->label : NULL);
11623 }
11624 else
11625 {
11626 auto_align = 0;
11627 }
11628
11629 demand_empty_rest_of_line ();
11630 }
11631
11632 void
11633 mips_flush_pending_output ()
11634 {
11635 mips_emit_delays (FALSE);
11636 mips_clear_insn_labels ();
11637 }
11638
11639 static void
11640 s_change_sec (sec)
11641 int sec;
11642 {
11643 segT seg;
11644
11645 /* When generating embedded PIC code, we only use the .text, .lit8,
11646 .sdata and .sbss sections. We change the .data and .rdata
11647 pseudo-ops to use .sdata. */
11648 if (mips_pic == EMBEDDED_PIC
11649 && (sec == 'd' || sec == 'r'))
11650 sec = 's';
11651
11652 #ifdef OBJ_ELF
11653 /* The ELF backend needs to know that we are changing sections, so
11654 that .previous works correctly. We could do something like check
11655 for an obj_section_change_hook macro, but that might be confusing
11656 as it would not be appropriate to use it in the section changing
11657 functions in read.c, since obj-elf.c intercepts those. FIXME:
11658 This should be cleaner, somehow. */
11659 obj_elf_section_change_hook ();
11660 #endif
11661
11662 mips_emit_delays (FALSE);
11663 switch (sec)
11664 {
11665 case 't':
11666 s_text (0);
11667 break;
11668 case 'd':
11669 s_data (0);
11670 break;
11671 case 'b':
11672 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11673 demand_empty_rest_of_line ();
11674 break;
11675
11676 case 'r':
11677 if (USE_GLOBAL_POINTER_OPT)
11678 {
11679 seg = subseg_new (RDATA_SECTION_NAME,
11680 (subsegT) get_absolute_expression ());
11681 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11682 {
11683 bfd_set_section_flags (stdoutput, seg,
11684 (SEC_ALLOC
11685 | SEC_LOAD
11686 | SEC_READONLY
11687 | SEC_RELOC
11688 | SEC_DATA));
11689 if (strcmp (TARGET_OS, "elf") != 0)
11690 record_alignment (seg, 4);
11691 }
11692 demand_empty_rest_of_line ();
11693 }
11694 else
11695 {
11696 as_bad (_("No read only data section in this object file format"));
11697 demand_empty_rest_of_line ();
11698 return;
11699 }
11700 break;
11701
11702 case 's':
11703 if (USE_GLOBAL_POINTER_OPT)
11704 {
11705 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11706 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11707 {
11708 bfd_set_section_flags (stdoutput, seg,
11709 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11710 | SEC_DATA);
11711 if (strcmp (TARGET_OS, "elf") != 0)
11712 record_alignment (seg, 4);
11713 }
11714 demand_empty_rest_of_line ();
11715 break;
11716 }
11717 else
11718 {
11719 as_bad (_("Global pointers not supported; recompile -G 0"));
11720 demand_empty_rest_of_line ();
11721 return;
11722 }
11723 }
11724
11725 auto_align = 1;
11726 }
11727
11728 void
11729 s_change_section (ignore)
11730 int ignore ATTRIBUTE_UNUSED;
11731 {
11732 #ifdef OBJ_ELF
11733 char *section_name;
11734 char c;
11735 char next_c;
11736 int section_type;
11737 int section_flag;
11738 int section_entry_size;
11739 int section_alignment;
11740
11741 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11742 return;
11743
11744 section_name = input_line_pointer;
11745 c = get_symbol_end ();
11746 if (c)
11747 next_c = *(input_line_pointer + 1);
11748
11749 /* Do we have .section Name<,"flags">? */
11750 if (c != ',' || (c == ',' && next_c == '"'))
11751 {
11752 /* just after name is now '\0'. */
11753 *input_line_pointer = c;
11754 input_line_pointer = section_name;
11755 obj_elf_section (ignore);
11756 return;
11757 }
11758 input_line_pointer++;
11759
11760 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11761 if (c == ',')
11762 section_type = get_absolute_expression ();
11763 else
11764 section_type = 0;
11765 if (*input_line_pointer++ == ',')
11766 section_flag = get_absolute_expression ();
11767 else
11768 section_flag = 0;
11769 if (*input_line_pointer++ == ',')
11770 section_entry_size = get_absolute_expression ();
11771 else
11772 section_entry_size = 0;
11773 if (*input_line_pointer++ == ',')
11774 section_alignment = get_absolute_expression ();
11775 else
11776 section_alignment = 0;
11777
11778 section_name = xstrdup (section_name);
11779
11780 obj_elf_change_section (section_name, section_type, section_flag,
11781 section_entry_size, 0, 0, 0);
11782
11783 if (now_seg->name != section_name)
11784 free (section_name);
11785 #endif /* OBJ_ELF */
11786 }
11787
11788 void
11789 mips_enable_auto_align ()
11790 {
11791 auto_align = 1;
11792 }
11793
11794 static void
11795 s_cons (log_size)
11796 int log_size;
11797 {
11798 symbolS *label;
11799
11800 label = insn_labels != NULL ? insn_labels->label : NULL;
11801 mips_emit_delays (FALSE);
11802 if (log_size > 0 && auto_align)
11803 mips_align (log_size, 0, label);
11804 mips_clear_insn_labels ();
11805 cons (1 << log_size);
11806 }
11807
11808 static void
11809 s_float_cons (type)
11810 int type;
11811 {
11812 symbolS *label;
11813
11814 label = insn_labels != NULL ? insn_labels->label : NULL;
11815
11816 mips_emit_delays (FALSE);
11817
11818 if (auto_align)
11819 {
11820 if (type == 'd')
11821 mips_align (3, 0, label);
11822 else
11823 mips_align (2, 0, label);
11824 }
11825
11826 mips_clear_insn_labels ();
11827
11828 float_cons (type);
11829 }
11830
11831 /* Handle .globl. We need to override it because on Irix 5 you are
11832 permitted to say
11833 .globl foo .text
11834 where foo is an undefined symbol, to mean that foo should be
11835 considered to be the address of a function. */
11836
11837 static void
11838 s_mips_globl (x)
11839 int x ATTRIBUTE_UNUSED;
11840 {
11841 char *name;
11842 int c;
11843 symbolS *symbolP;
11844 flagword flag;
11845
11846 name = input_line_pointer;
11847 c = get_symbol_end ();
11848 symbolP = symbol_find_or_make (name);
11849 *input_line_pointer = c;
11850 SKIP_WHITESPACE ();
11851
11852 /* On Irix 5, every global symbol that is not explicitly labelled as
11853 being a function is apparently labelled as being an object. */
11854 flag = BSF_OBJECT;
11855
11856 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11857 {
11858 char *secname;
11859 asection *sec;
11860
11861 secname = input_line_pointer;
11862 c = get_symbol_end ();
11863 sec = bfd_get_section_by_name (stdoutput, secname);
11864 if (sec == NULL)
11865 as_bad (_("%s: no such section"), secname);
11866 *input_line_pointer = c;
11867
11868 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11869 flag = BSF_FUNCTION;
11870 }
11871
11872 symbol_get_bfdsym (symbolP)->flags |= flag;
11873
11874 S_SET_EXTERNAL (symbolP);
11875 demand_empty_rest_of_line ();
11876 }
11877
11878 static void
11879 s_option (x)
11880 int x ATTRIBUTE_UNUSED;
11881 {
11882 char *opt;
11883 char c;
11884
11885 opt = input_line_pointer;
11886 c = get_symbol_end ();
11887
11888 if (*opt == 'O')
11889 {
11890 /* FIXME: What does this mean? */
11891 }
11892 else if (strncmp (opt, "pic", 3) == 0)
11893 {
11894 int i;
11895
11896 i = atoi (opt + 3);
11897 if (i == 0)
11898 mips_pic = NO_PIC;
11899 else if (i == 2)
11900 mips_pic = SVR4_PIC;
11901 else
11902 as_bad (_(".option pic%d not supported"), i);
11903
11904 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11905 {
11906 if (g_switch_seen && g_switch_value != 0)
11907 as_warn (_("-G may not be used with SVR4 PIC code"));
11908 g_switch_value = 0;
11909 bfd_set_gp_size (stdoutput, 0);
11910 }
11911 }
11912 else
11913 as_warn (_("Unrecognized option \"%s\""), opt);
11914
11915 *input_line_pointer = c;
11916 demand_empty_rest_of_line ();
11917 }
11918
11919 /* This structure is used to hold a stack of .set values. */
11920
11921 struct mips_option_stack
11922 {
11923 struct mips_option_stack *next;
11924 struct mips_set_options options;
11925 };
11926
11927 static struct mips_option_stack *mips_opts_stack;
11928
11929 /* Handle the .set pseudo-op. */
11930
11931 static void
11932 s_mipsset (x)
11933 int x ATTRIBUTE_UNUSED;
11934 {
11935 char *name = input_line_pointer, ch;
11936
11937 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11938 ++input_line_pointer;
11939 ch = *input_line_pointer;
11940 *input_line_pointer = '\0';
11941
11942 if (strcmp (name, "reorder") == 0)
11943 {
11944 if (mips_opts.noreorder && prev_nop_frag != NULL)
11945 {
11946 /* If we still have pending nops, we can discard them. The
11947 usual nop handling will insert any that are still
11948 needed. */
11949 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11950 * (mips_opts.mips16 ? 2 : 4));
11951 prev_nop_frag = NULL;
11952 }
11953 mips_opts.noreorder = 0;
11954 }
11955 else if (strcmp (name, "noreorder") == 0)
11956 {
11957 mips_emit_delays (TRUE);
11958 mips_opts.noreorder = 1;
11959 mips_any_noreorder = 1;
11960 }
11961 else if (strcmp (name, "at") == 0)
11962 {
11963 mips_opts.noat = 0;
11964 }
11965 else if (strcmp (name, "noat") == 0)
11966 {
11967 mips_opts.noat = 1;
11968 }
11969 else if (strcmp (name, "macro") == 0)
11970 {
11971 mips_opts.warn_about_macros = 0;
11972 }
11973 else if (strcmp (name, "nomacro") == 0)
11974 {
11975 if (mips_opts.noreorder == 0)
11976 as_bad (_("`noreorder' must be set before `nomacro'"));
11977 mips_opts.warn_about_macros = 1;
11978 }
11979 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11980 {
11981 mips_opts.nomove = 0;
11982 }
11983 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11984 {
11985 mips_opts.nomove = 1;
11986 }
11987 else if (strcmp (name, "bopt") == 0)
11988 {
11989 mips_opts.nobopt = 0;
11990 }
11991 else if (strcmp (name, "nobopt") == 0)
11992 {
11993 mips_opts.nobopt = 1;
11994 }
11995 else if (strcmp (name, "mips16") == 0
11996 || strcmp (name, "MIPS-16") == 0)
11997 mips_opts.mips16 = 1;
11998 else if (strcmp (name, "nomips16") == 0
11999 || strcmp (name, "noMIPS-16") == 0)
12000 mips_opts.mips16 = 0;
12001 else if (strcmp (name, "mips3d") == 0)
12002 mips_opts.ase_mips3d = 1;
12003 else if (strcmp (name, "nomips3d") == 0)
12004 mips_opts.ase_mips3d = 0;
12005 else if (strcmp (name, "mdmx") == 0)
12006 mips_opts.ase_mdmx = 1;
12007 else if (strcmp (name, "nomdmx") == 0)
12008 mips_opts.ase_mdmx = 0;
12009 else if (strncmp (name, "mips", 4) == 0)
12010 {
12011 int reset = 0;
12012
12013 /* Permit the user to change the ISA on the fly. Needless to
12014 say, misuse can cause serious problems. */
12015 if (strcmp (name, "mips0") == 0)
12016 {
12017 reset = 1;
12018 mips_opts.isa = file_mips_isa;
12019 }
12020 else if (strcmp (name, "mips1") == 0)
12021 mips_opts.isa = ISA_MIPS1;
12022 else if (strcmp (name, "mips2") == 0)
12023 mips_opts.isa = ISA_MIPS2;
12024 else if (strcmp (name, "mips3") == 0)
12025 mips_opts.isa = ISA_MIPS3;
12026 else if (strcmp (name, "mips4") == 0)
12027 mips_opts.isa = ISA_MIPS4;
12028 else if (strcmp (name, "mips5") == 0)
12029 mips_opts.isa = ISA_MIPS5;
12030 else if (strcmp (name, "mips32") == 0)
12031 mips_opts.isa = ISA_MIPS32;
12032 else if (strcmp (name, "mips32r2") == 0)
12033 mips_opts.isa = ISA_MIPS32R2;
12034 else if (strcmp (name, "mips64") == 0)
12035 mips_opts.isa = ISA_MIPS64;
12036 else
12037 as_bad (_("unknown ISA level %s"), name + 4);
12038
12039 switch (mips_opts.isa)
12040 {
12041 case 0:
12042 break;
12043 case ISA_MIPS1:
12044 case ISA_MIPS2:
12045 case ISA_MIPS32:
12046 case ISA_MIPS32R2:
12047 mips_opts.gp32 = 1;
12048 mips_opts.fp32 = 1;
12049 break;
12050 case ISA_MIPS3:
12051 case ISA_MIPS4:
12052 case ISA_MIPS5:
12053 case ISA_MIPS64:
12054 mips_opts.gp32 = 0;
12055 mips_opts.fp32 = 0;
12056 break;
12057 default:
12058 as_bad (_("unknown ISA level %s"), name + 4);
12059 break;
12060 }
12061 if (reset)
12062 {
12063 mips_opts.gp32 = file_mips_gp32;
12064 mips_opts.fp32 = file_mips_fp32;
12065 }
12066 }
12067 else if (strcmp (name, "autoextend") == 0)
12068 mips_opts.noautoextend = 0;
12069 else if (strcmp (name, "noautoextend") == 0)
12070 mips_opts.noautoextend = 1;
12071 else if (strcmp (name, "push") == 0)
12072 {
12073 struct mips_option_stack *s;
12074
12075 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12076 s->next = mips_opts_stack;
12077 s->options = mips_opts;
12078 mips_opts_stack = s;
12079 }
12080 else if (strcmp (name, "pop") == 0)
12081 {
12082 struct mips_option_stack *s;
12083
12084 s = mips_opts_stack;
12085 if (s == NULL)
12086 as_bad (_(".set pop with no .set push"));
12087 else
12088 {
12089 /* If we're changing the reorder mode we need to handle
12090 delay slots correctly. */
12091 if (s->options.noreorder && ! mips_opts.noreorder)
12092 mips_emit_delays (TRUE);
12093 else if (! s->options.noreorder && mips_opts.noreorder)
12094 {
12095 if (prev_nop_frag != NULL)
12096 {
12097 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12098 * (mips_opts.mips16 ? 2 : 4));
12099 prev_nop_frag = NULL;
12100 }
12101 }
12102
12103 mips_opts = s->options;
12104 mips_opts_stack = s->next;
12105 free (s);
12106 }
12107 }
12108 else
12109 {
12110 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12111 }
12112 *input_line_pointer = ch;
12113 demand_empty_rest_of_line ();
12114 }
12115
12116 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
12117 .option pic2. It means to generate SVR4 PIC calls. */
12118
12119 static void
12120 s_abicalls (ignore)
12121 int ignore ATTRIBUTE_UNUSED;
12122 {
12123 mips_pic = SVR4_PIC;
12124 if (USE_GLOBAL_POINTER_OPT)
12125 {
12126 if (g_switch_seen && g_switch_value != 0)
12127 as_warn (_("-G may not be used with SVR4 PIC code"));
12128 g_switch_value = 0;
12129 }
12130 bfd_set_gp_size (stdoutput, 0);
12131 demand_empty_rest_of_line ();
12132 }
12133
12134 /* Handle the .cpload pseudo-op. This is used when generating SVR4
12135 PIC code. It sets the $gp register for the function based on the
12136 function address, which is in the register named in the argument.
12137 This uses a relocation against _gp_disp, which is handled specially
12138 by the linker. The result is:
12139 lui $gp,%hi(_gp_disp)
12140 addiu $gp,$gp,%lo(_gp_disp)
12141 addu $gp,$gp,.cpload argument
12142 The .cpload argument is normally $25 == $t9. */
12143
12144 static void
12145 s_cpload (ignore)
12146 int ignore ATTRIBUTE_UNUSED;
12147 {
12148 expressionS ex;
12149 int icnt = 0;
12150
12151 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12152 .cpload is ignored. */
12153 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12154 {
12155 s_ignore (0);
12156 return;
12157 }
12158
12159 /* .cpload should be in a .set noreorder section. */
12160 if (mips_opts.noreorder == 0)
12161 as_warn (_(".cpload not in noreorder section"));
12162
12163 ex.X_op = O_symbol;
12164 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12165 ex.X_op_symbol = NULL;
12166 ex.X_add_number = 0;
12167
12168 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12169 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12170
12171 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12172 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12173 mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12174
12175 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12176 mips_gp_register, mips_gp_register, tc_get_register (0));
12177
12178 demand_empty_rest_of_line ();
12179 }
12180
12181 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12182 .cpsetup $reg1, offset|$reg2, label
12183
12184 If offset is given, this results in:
12185 sd $gp, offset($sp)
12186 lui $gp, %hi(%neg(%gp_rel(label)))
12187 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12188 daddu $gp, $gp, $reg1
12189
12190 If $reg2 is given, this results in:
12191 daddu $reg2, $gp, $0
12192 lui $gp, %hi(%neg(%gp_rel(label)))
12193 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12194 daddu $gp, $gp, $reg1
12195 $reg1 is normally $25 == $t9. */
12196 static void
12197 s_cpsetup (ignore)
12198 int ignore ATTRIBUTE_UNUSED;
12199 {
12200 expressionS ex_off;
12201 expressionS ex_sym;
12202 int reg1;
12203 int icnt = 0;
12204 char *f;
12205
12206 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12207 We also need NewABI support. */
12208 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12209 {
12210 s_ignore (0);
12211 return;
12212 }
12213
12214 reg1 = tc_get_register (0);
12215 SKIP_WHITESPACE ();
12216 if (*input_line_pointer != ',')
12217 {
12218 as_bad (_("missing argument separator ',' for .cpsetup"));
12219 return;
12220 }
12221 else
12222 ++input_line_pointer;
12223 SKIP_WHITESPACE ();
12224 if (*input_line_pointer == '$')
12225 {
12226 mips_cpreturn_register = tc_get_register (0);
12227 mips_cpreturn_offset = -1;
12228 }
12229 else
12230 {
12231 mips_cpreturn_offset = get_absolute_expression ();
12232 mips_cpreturn_register = -1;
12233 }
12234 SKIP_WHITESPACE ();
12235 if (*input_line_pointer != ',')
12236 {
12237 as_bad (_("missing argument separator ',' for .cpsetup"));
12238 return;
12239 }
12240 else
12241 ++input_line_pointer;
12242 SKIP_WHITESPACE ();
12243 expression (&ex_sym);
12244
12245 if (mips_cpreturn_register == -1)
12246 {
12247 ex_off.X_op = O_constant;
12248 ex_off.X_add_symbol = NULL;
12249 ex_off.X_op_symbol = NULL;
12250 ex_off.X_add_number = mips_cpreturn_offset;
12251
12252 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12253 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12254 }
12255 else
12256 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12257 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12258
12259 /* Ensure there's room for the next two instructions, so that `f'
12260 doesn't end up with an address in the wrong frag. */
12261 frag_grow (8);
12262 f = frag_more (0);
12263 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12264 (int) BFD_RELOC_GPREL16);
12265 fix_new (frag_now, f - frag_now->fr_literal,
12266 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12267 fix_new (frag_now, f - frag_now->fr_literal,
12268 0, NULL, 0, 0, BFD_RELOC_HI16_S);
12269
12270 f = frag_more (0);
12271 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12272 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12273 fix_new (frag_now, f - frag_now->fr_literal,
12274 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12275 fix_new (frag_now, f - frag_now->fr_literal,
12276 0, NULL, 0, 0, BFD_RELOC_LO16);
12277
12278 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12279 HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
12280 mips_gp_register, mips_gp_register, reg1);
12281
12282 demand_empty_rest_of_line ();
12283 }
12284
12285 static void
12286 s_cplocal (ignore)
12287 int ignore ATTRIBUTE_UNUSED;
12288 {
12289 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12290 .cplocal is ignored. */
12291 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12292 {
12293 s_ignore (0);
12294 return;
12295 }
12296
12297 mips_gp_register = tc_get_register (0);
12298 demand_empty_rest_of_line ();
12299 }
12300
12301 /* Handle the .cprestore pseudo-op. This stores $gp into a given
12302 offset from $sp. The offset is remembered, and after making a PIC
12303 call $gp is restored from that location. */
12304
12305 static void
12306 s_cprestore (ignore)
12307 int ignore ATTRIBUTE_UNUSED;
12308 {
12309 expressionS ex;
12310 int icnt = 0;
12311
12312 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12313 .cprestore is ignored. */
12314 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12315 {
12316 s_ignore (0);
12317 return;
12318 }
12319
12320 mips_cprestore_offset = get_absolute_expression ();
12321 mips_cprestore_valid = 1;
12322
12323 ex.X_op = O_constant;
12324 ex.X_add_symbol = NULL;
12325 ex.X_op_symbol = NULL;
12326 ex.X_add_number = mips_cprestore_offset;
12327
12328 macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12329 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12330 mips_gp_register, SP);
12331
12332 demand_empty_rest_of_line ();
12333 }
12334
12335 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12336 was given in the preceeding .gpsetup, it results in:
12337 ld $gp, offset($sp)
12338
12339 If a register $reg2 was given there, it results in:
12340 daddiu $gp, $gp, $reg2
12341 */
12342 static void
12343 s_cpreturn (ignore)
12344 int ignore ATTRIBUTE_UNUSED;
12345 {
12346 expressionS ex;
12347 int icnt = 0;
12348
12349 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12350 We also need NewABI support. */
12351 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12352 {
12353 s_ignore (0);
12354 return;
12355 }
12356
12357 if (mips_cpreturn_register == -1)
12358 {
12359 ex.X_op = O_constant;
12360 ex.X_add_symbol = NULL;
12361 ex.X_op_symbol = NULL;
12362 ex.X_add_number = mips_cpreturn_offset;
12363
12364 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12365 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12366 }
12367 else
12368 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12369 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12370
12371 demand_empty_rest_of_line ();
12372 }
12373
12374 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12375 code. It sets the offset to use in gp_rel relocations. */
12376
12377 static void
12378 s_gpvalue (ignore)
12379 int ignore ATTRIBUTE_UNUSED;
12380 {
12381 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12382 We also need NewABI support. */
12383 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12384 {
12385 s_ignore (0);
12386 return;
12387 }
12388
12389 mips_gprel_offset = get_absolute_expression ();
12390
12391 demand_empty_rest_of_line ();
12392 }
12393
12394 /* Handle the .gpword pseudo-op. This is used when generating PIC
12395 code. It generates a 32 bit GP relative reloc. */
12396
12397 static void
12398 s_gpword (ignore)
12399 int ignore ATTRIBUTE_UNUSED;
12400 {
12401 symbolS *label;
12402 expressionS ex;
12403 char *p;
12404
12405 /* When not generating PIC code, this is treated as .word. */
12406 if (mips_pic != SVR4_PIC)
12407 {
12408 s_cons (2);
12409 return;
12410 }
12411
12412 label = insn_labels != NULL ? insn_labels->label : NULL;
12413 mips_emit_delays (TRUE);
12414 if (auto_align)
12415 mips_align (2, 0, label);
12416 mips_clear_insn_labels ();
12417
12418 expression (&ex);
12419
12420 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12421 {
12422 as_bad (_("Unsupported use of .gpword"));
12423 ignore_rest_of_line ();
12424 }
12425
12426 p = frag_more (4);
12427 md_number_to_chars (p, (valueT) 0, 4);
12428 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12429 BFD_RELOC_GPREL32);
12430
12431 demand_empty_rest_of_line ();
12432 }
12433
12434 static void
12435 s_gpdword (ignore)
12436 int ignore ATTRIBUTE_UNUSED;
12437 {
12438 symbolS *label;
12439 expressionS ex;
12440 char *p;
12441
12442 /* When not generating PIC code, this is treated as .dword. */
12443 if (mips_pic != SVR4_PIC)
12444 {
12445 s_cons (3);
12446 return;
12447 }
12448
12449 label = insn_labels != NULL ? insn_labels->label : NULL;
12450 mips_emit_delays (TRUE);
12451 if (auto_align)
12452 mips_align (3, 0, label);
12453 mips_clear_insn_labels ();
12454
12455 expression (&ex);
12456
12457 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12458 {
12459 as_bad (_("Unsupported use of .gpdword"));
12460 ignore_rest_of_line ();
12461 }
12462
12463 p = frag_more (8);
12464 md_number_to_chars (p, (valueT) 0, 8);
12465 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12466 BFD_RELOC_GPREL32);
12467
12468 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12469 ex.X_op = O_absent;
12470 ex.X_add_symbol = 0;
12471 ex.X_add_number = 0;
12472 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12473 BFD_RELOC_64);
12474
12475 demand_empty_rest_of_line ();
12476 }
12477
12478 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12479 tables in SVR4 PIC code. */
12480
12481 static void
12482 s_cpadd (ignore)
12483 int ignore ATTRIBUTE_UNUSED;
12484 {
12485 int icnt = 0;
12486 int reg;
12487
12488 /* This is ignored when not generating SVR4 PIC code. */
12489 if (mips_pic != SVR4_PIC)
12490 {
12491 s_ignore (0);
12492 return;
12493 }
12494
12495 /* Add $gp to the register named as an argument. */
12496 reg = tc_get_register (0);
12497 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12498 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12499 "d,v,t", reg, reg, mips_gp_register);
12500
12501 demand_empty_rest_of_line ();
12502 }
12503
12504 /* Handle the .insn pseudo-op. This marks instruction labels in
12505 mips16 mode. This permits the linker to handle them specially,
12506 such as generating jalx instructions when needed. We also make
12507 them odd for the duration of the assembly, in order to generate the
12508 right sort of code. We will make them even in the adjust_symtab
12509 routine, while leaving them marked. This is convenient for the
12510 debugger and the disassembler. The linker knows to make them odd
12511 again. */
12512
12513 static void
12514 s_insn (ignore)
12515 int ignore ATTRIBUTE_UNUSED;
12516 {
12517 mips16_mark_labels ();
12518
12519 demand_empty_rest_of_line ();
12520 }
12521
12522 /* Handle a .stabn directive. We need these in order to mark a label
12523 as being a mips16 text label correctly. Sometimes the compiler
12524 will emit a label, followed by a .stabn, and then switch sections.
12525 If the label and .stabn are in mips16 mode, then the label is
12526 really a mips16 text label. */
12527
12528 static void
12529 s_mips_stab (type)
12530 int type;
12531 {
12532 if (type == 'n')
12533 mips16_mark_labels ();
12534
12535 s_stab (type);
12536 }
12537
12538 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12539 */
12540
12541 static void
12542 s_mips_weakext (ignore)
12543 int ignore ATTRIBUTE_UNUSED;
12544 {
12545 char *name;
12546 int c;
12547 symbolS *symbolP;
12548 expressionS exp;
12549
12550 name = input_line_pointer;
12551 c = get_symbol_end ();
12552 symbolP = symbol_find_or_make (name);
12553 S_SET_WEAK (symbolP);
12554 *input_line_pointer = c;
12555
12556 SKIP_WHITESPACE ();
12557
12558 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12559 {
12560 if (S_IS_DEFINED (symbolP))
12561 {
12562 as_bad ("ignoring attempt to redefine symbol %s",
12563 S_GET_NAME (symbolP));
12564 ignore_rest_of_line ();
12565 return;
12566 }
12567
12568 if (*input_line_pointer == ',')
12569 {
12570 ++input_line_pointer;
12571 SKIP_WHITESPACE ();
12572 }
12573
12574 expression (&exp);
12575 if (exp.X_op != O_symbol)
12576 {
12577 as_bad ("bad .weakext directive");
12578 ignore_rest_of_line ();
12579 return;
12580 }
12581 symbol_set_value_expression (symbolP, &exp);
12582 }
12583
12584 demand_empty_rest_of_line ();
12585 }
12586
12587 /* Parse a register string into a number. Called from the ECOFF code
12588 to parse .frame. The argument is non-zero if this is the frame
12589 register, so that we can record it in mips_frame_reg. */
12590
12591 int
12592 tc_get_register (frame)
12593 int frame;
12594 {
12595 int reg;
12596
12597 SKIP_WHITESPACE ();
12598 if (*input_line_pointer++ != '$')
12599 {
12600 as_warn (_("expected `$'"));
12601 reg = ZERO;
12602 }
12603 else if (ISDIGIT (*input_line_pointer))
12604 {
12605 reg = get_absolute_expression ();
12606 if (reg < 0 || reg >= 32)
12607 {
12608 as_warn (_("Bad register number"));
12609 reg = ZERO;
12610 }
12611 }
12612 else
12613 {
12614 if (strncmp (input_line_pointer, "ra", 2) == 0)
12615 {
12616 reg = RA;
12617 input_line_pointer += 2;
12618 }
12619 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12620 {
12621 reg = FP;
12622 input_line_pointer += 2;
12623 }
12624 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12625 {
12626 reg = SP;
12627 input_line_pointer += 2;
12628 }
12629 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12630 {
12631 reg = GP;
12632 input_line_pointer += 2;
12633 }
12634 else if (strncmp (input_line_pointer, "at", 2) == 0)
12635 {
12636 reg = AT;
12637 input_line_pointer += 2;
12638 }
12639 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12640 {
12641 reg = KT0;
12642 input_line_pointer += 3;
12643 }
12644 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12645 {
12646 reg = KT1;
12647 input_line_pointer += 3;
12648 }
12649 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12650 {
12651 reg = ZERO;
12652 input_line_pointer += 4;
12653 }
12654 else
12655 {
12656 as_warn (_("Unrecognized register name"));
12657 reg = ZERO;
12658 while (ISALNUM(*input_line_pointer))
12659 input_line_pointer++;
12660 }
12661 }
12662 if (frame)
12663 {
12664 mips_frame_reg = reg != 0 ? reg : SP;
12665 mips_frame_reg_valid = 1;
12666 mips_cprestore_valid = 0;
12667 }
12668 return reg;
12669 }
12670
12671 valueT
12672 md_section_align (seg, addr)
12673 asection *seg;
12674 valueT addr;
12675 {
12676 int align = bfd_get_section_alignment (stdoutput, seg);
12677
12678 #ifdef OBJ_ELF
12679 /* We don't need to align ELF sections to the full alignment.
12680 However, Irix 5 may prefer that we align them at least to a 16
12681 byte boundary. We don't bother to align the sections if we are
12682 targeted for an embedded system. */
12683 if (strcmp (TARGET_OS, "elf") == 0)
12684 return addr;
12685 if (align > 4)
12686 align = 4;
12687 #endif
12688
12689 return ((addr + (1 << align) - 1) & (-1 << align));
12690 }
12691
12692 /* Utility routine, called from above as well. If called while the
12693 input file is still being read, it's only an approximation. (For
12694 example, a symbol may later become defined which appeared to be
12695 undefined earlier.) */
12696
12697 static int
12698 nopic_need_relax (sym, before_relaxing)
12699 symbolS *sym;
12700 int before_relaxing;
12701 {
12702 if (sym == 0)
12703 return 0;
12704
12705 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12706 {
12707 const char *symname;
12708 int change;
12709
12710 /* Find out whether this symbol can be referenced off the $gp
12711 register. It can be if it is smaller than the -G size or if
12712 it is in the .sdata or .sbss section. Certain symbols can
12713 not be referenced off the $gp, although it appears as though
12714 they can. */
12715 symname = S_GET_NAME (sym);
12716 if (symname != (const char *) NULL
12717 && (strcmp (symname, "eprol") == 0
12718 || strcmp (symname, "etext") == 0
12719 || strcmp (symname, "_gp") == 0
12720 || strcmp (symname, "edata") == 0
12721 || strcmp (symname, "_fbss") == 0
12722 || strcmp (symname, "_fdata") == 0
12723 || strcmp (symname, "_ftext") == 0
12724 || strcmp (symname, "end") == 0
12725 || strcmp (symname, "_gp_disp") == 0))
12726 change = 1;
12727 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12728 && (0
12729 #ifndef NO_ECOFF_DEBUGGING
12730 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12731 && (symbol_get_obj (sym)->ecoff_extern_size
12732 <= g_switch_value))
12733 #endif
12734 /* We must defer this decision until after the whole
12735 file has been read, since there might be a .extern
12736 after the first use of this symbol. */
12737 || (before_relaxing
12738 #ifndef NO_ECOFF_DEBUGGING
12739 && symbol_get_obj (sym)->ecoff_extern_size == 0
12740 #endif
12741 && S_GET_VALUE (sym) == 0)
12742 || (S_GET_VALUE (sym) != 0
12743 && S_GET_VALUE (sym) <= g_switch_value)))
12744 change = 0;
12745 else
12746 {
12747 const char *segname;
12748
12749 segname = segment_name (S_GET_SEGMENT (sym));
12750 assert (strcmp (segname, ".lit8") != 0
12751 && strcmp (segname, ".lit4") != 0);
12752 change = (strcmp (segname, ".sdata") != 0
12753 && strcmp (segname, ".sbss") != 0
12754 && strncmp (segname, ".sdata.", 7) != 0
12755 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12756 }
12757 return change;
12758 }
12759 else
12760 /* We are not optimizing for the $gp register. */
12761 return 1;
12762 }
12763
12764
12765 /* Return true if the given symbol should be considered local for SVR4 PIC. */
12766
12767 static bfd_boolean
12768 pic_need_relax (sym, segtype)
12769 symbolS *sym;
12770 asection *segtype;
12771 {
12772 asection *symsec;
12773 bfd_boolean linkonce;
12774
12775 /* Handle the case of a symbol equated to another symbol. */
12776 while (symbol_equated_reloc_p (sym))
12777 {
12778 symbolS *n;
12779
12780 /* It's possible to get a loop here in a badly written
12781 program. */
12782 n = symbol_get_value_expression (sym)->X_add_symbol;
12783 if (n == sym)
12784 break;
12785 sym = n;
12786 }
12787
12788 symsec = S_GET_SEGMENT (sym);
12789
12790 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12791 linkonce = FALSE;
12792 if (symsec != segtype && ! S_IS_LOCAL (sym))
12793 {
12794 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12795 != 0)
12796 linkonce = TRUE;
12797
12798 /* The GNU toolchain uses an extension for ELF: a section
12799 beginning with the magic string .gnu.linkonce is a linkonce
12800 section. */
12801 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12802 sizeof ".gnu.linkonce" - 1) == 0)
12803 linkonce = TRUE;
12804 }
12805
12806 /* This must duplicate the test in adjust_reloc_syms. */
12807 return (symsec != &bfd_und_section
12808 && symsec != &bfd_abs_section
12809 && ! bfd_is_com_section (symsec)
12810 && !linkonce
12811 #ifdef OBJ_ELF
12812 /* A global or weak symbol is treated as external. */
12813 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12814 || (! S_IS_WEAK (sym)
12815 && (! S_IS_EXTERNAL (sym)
12816 || mips_pic == EMBEDDED_PIC)))
12817 #endif
12818 );
12819 }
12820
12821
12822 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12823 extended opcode. SEC is the section the frag is in. */
12824
12825 static int
12826 mips16_extended_frag (fragp, sec, stretch)
12827 fragS *fragp;
12828 asection *sec;
12829 long stretch;
12830 {
12831 int type;
12832 register const struct mips16_immed_operand *op;
12833 offsetT val;
12834 int mintiny, maxtiny;
12835 segT symsec;
12836 fragS *sym_frag;
12837
12838 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12839 return 0;
12840 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12841 return 1;
12842
12843 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12844 op = mips16_immed_operands;
12845 while (op->type != type)
12846 {
12847 ++op;
12848 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12849 }
12850
12851 if (op->unsp)
12852 {
12853 if (type == '<' || type == '>' || type == '[' || type == ']')
12854 {
12855 mintiny = 1;
12856 maxtiny = 1 << op->nbits;
12857 }
12858 else
12859 {
12860 mintiny = 0;
12861 maxtiny = (1 << op->nbits) - 1;
12862 }
12863 }
12864 else
12865 {
12866 mintiny = - (1 << (op->nbits - 1));
12867 maxtiny = (1 << (op->nbits - 1)) - 1;
12868 }
12869
12870 sym_frag = symbol_get_frag (fragp->fr_symbol);
12871 val = S_GET_VALUE (fragp->fr_symbol);
12872 symsec = S_GET_SEGMENT (fragp->fr_symbol);
12873
12874 if (op->pcrel)
12875 {
12876 addressT addr;
12877
12878 /* We won't have the section when we are called from
12879 mips_relax_frag. However, we will always have been called
12880 from md_estimate_size_before_relax first. If this is a
12881 branch to a different section, we mark it as such. If SEC is
12882 NULL, and the frag is not marked, then it must be a branch to
12883 the same section. */
12884 if (sec == NULL)
12885 {
12886 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12887 return 1;
12888 }
12889 else
12890 {
12891 /* Must have been called from md_estimate_size_before_relax. */
12892 if (symsec != sec)
12893 {
12894 fragp->fr_subtype =
12895 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12896
12897 /* FIXME: We should support this, and let the linker
12898 catch branches and loads that are out of range. */
12899 as_bad_where (fragp->fr_file, fragp->fr_line,
12900 _("unsupported PC relative reference to different section"));
12901
12902 return 1;
12903 }
12904 if (fragp != sym_frag && sym_frag->fr_address == 0)
12905 /* Assume non-extended on the first relaxation pass.
12906 The address we have calculated will be bogus if this is
12907 a forward branch to another frag, as the forward frag
12908 will have fr_address == 0. */
12909 return 0;
12910 }
12911
12912 /* In this case, we know for sure that the symbol fragment is in
12913 the same section. If the relax_marker of the symbol fragment
12914 differs from the relax_marker of this fragment, we have not
12915 yet adjusted the symbol fragment fr_address. We want to add
12916 in STRETCH in order to get a better estimate of the address.
12917 This particularly matters because of the shift bits. */
12918 if (stretch != 0
12919 && sym_frag->relax_marker != fragp->relax_marker)
12920 {
12921 fragS *f;
12922
12923 /* Adjust stretch for any alignment frag. Note that if have
12924 been expanding the earlier code, the symbol may be
12925 defined in what appears to be an earlier frag. FIXME:
12926 This doesn't handle the fr_subtype field, which specifies
12927 a maximum number of bytes to skip when doing an
12928 alignment. */
12929 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12930 {
12931 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12932 {
12933 if (stretch < 0)
12934 stretch = - ((- stretch)
12935 & ~ ((1 << (int) f->fr_offset) - 1));
12936 else
12937 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12938 if (stretch == 0)
12939 break;
12940 }
12941 }
12942 if (f != NULL)
12943 val += stretch;
12944 }
12945
12946 addr = fragp->fr_address + fragp->fr_fix;
12947
12948 /* The base address rules are complicated. The base address of
12949 a branch is the following instruction. The base address of a
12950 PC relative load or add is the instruction itself, but if it
12951 is in a delay slot (in which case it can not be extended) use
12952 the address of the instruction whose delay slot it is in. */
12953 if (type == 'p' || type == 'q')
12954 {
12955 addr += 2;
12956
12957 /* If we are currently assuming that this frag should be
12958 extended, then, the current address is two bytes
12959 higher. */
12960 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12961 addr += 2;
12962
12963 /* Ignore the low bit in the target, since it will be set
12964 for a text label. */
12965 if ((val & 1) != 0)
12966 --val;
12967 }
12968 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12969 addr -= 4;
12970 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12971 addr -= 2;
12972
12973 val -= addr & ~ ((1 << op->shift) - 1);
12974
12975 /* Branch offsets have an implicit 0 in the lowest bit. */
12976 if (type == 'p' || type == 'q')
12977 val /= 2;
12978
12979 /* If any of the shifted bits are set, we must use an extended
12980 opcode. If the address depends on the size of this
12981 instruction, this can lead to a loop, so we arrange to always
12982 use an extended opcode. We only check this when we are in
12983 the main relaxation loop, when SEC is NULL. */
12984 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12985 {
12986 fragp->fr_subtype =
12987 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12988 return 1;
12989 }
12990
12991 /* If we are about to mark a frag as extended because the value
12992 is precisely maxtiny + 1, then there is a chance of an
12993 infinite loop as in the following code:
12994 la $4,foo
12995 .skip 1020
12996 .align 2
12997 foo:
12998 In this case when the la is extended, foo is 0x3fc bytes
12999 away, so the la can be shrunk, but then foo is 0x400 away, so
13000 the la must be extended. To avoid this loop, we mark the
13001 frag as extended if it was small, and is about to become
13002 extended with a value of maxtiny + 1. */
13003 if (val == ((maxtiny + 1) << op->shift)
13004 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13005 && sec == NULL)
13006 {
13007 fragp->fr_subtype =
13008 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13009 return 1;
13010 }
13011 }
13012 else if (symsec != absolute_section && sec != NULL)
13013 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13014
13015 if ((val & ((1 << op->shift) - 1)) != 0
13016 || val < (mintiny << op->shift)
13017 || val > (maxtiny << op->shift))
13018 return 1;
13019 else
13020 return 0;
13021 }
13022
13023 /* Compute the length of a branch sequence, and adjust the
13024 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13025 worst-case length is computed, with UPDATE being used to indicate
13026 whether an unconditional (-1), branch-likely (+1) or regular (0)
13027 branch is to be computed. */
13028 static int
13029 relaxed_branch_length (fragp, sec, update)
13030 fragS *fragp;
13031 asection *sec;
13032 int update;
13033 {
13034 bfd_boolean toofar;
13035 int length;
13036
13037 if (fragp
13038 && S_IS_DEFINED (fragp->fr_symbol)
13039 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13040 {
13041 addressT addr;
13042 offsetT val;
13043
13044 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13045
13046 addr = fragp->fr_address + fragp->fr_fix + 4;
13047
13048 val -= addr;
13049
13050 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13051 }
13052 else if (fragp)
13053 /* If the symbol is not defined or it's in a different segment,
13054 assume the user knows what's going on and emit a short
13055 branch. */
13056 toofar = FALSE;
13057 else
13058 toofar = TRUE;
13059
13060 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13061 fragp->fr_subtype
13062 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13063 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13064 RELAX_BRANCH_LINK (fragp->fr_subtype),
13065 toofar);
13066
13067 length = 4;
13068 if (toofar)
13069 {
13070 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13071 length += 8;
13072
13073 if (mips_pic != NO_PIC)
13074 {
13075 /* Additional space for PIC loading of target address. */
13076 length += 8;
13077 if (mips_opts.isa == ISA_MIPS1)
13078 /* Additional space for $at-stabilizing nop. */
13079 length += 4;
13080 }
13081
13082 /* If branch is conditional. */
13083 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13084 length += 8;
13085 }
13086
13087 return length;
13088 }
13089
13090 /* Estimate the size of a frag before relaxing. Unless this is the
13091 mips16, we are not really relaxing here, and the final size is
13092 encoded in the subtype information. For the mips16, we have to
13093 decide whether we are using an extended opcode or not. */
13094
13095 int
13096 md_estimate_size_before_relax (fragp, segtype)
13097 fragS *fragp;
13098 asection *segtype;
13099 {
13100 int change;
13101
13102 if (RELAX_BRANCH_P (fragp->fr_subtype))
13103 {
13104
13105 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13106
13107 return fragp->fr_var;
13108 }
13109
13110 if (RELAX_MIPS16_P (fragp->fr_subtype))
13111 /* We don't want to modify the EXTENDED bit here; it might get us
13112 into infinite loops. We change it only in mips_relax_frag(). */
13113 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13114
13115 if (mips_pic == NO_PIC)
13116 change = nopic_need_relax (fragp->fr_symbol, 0);
13117 else if (mips_pic == SVR4_PIC)
13118 change = pic_need_relax (fragp->fr_symbol, segtype);
13119 else
13120 abort ();
13121
13122 if (change)
13123 {
13124 /* Record the offset to the first reloc in the fr_opcode field.
13125 This lets md_convert_frag and tc_gen_reloc know that the code
13126 must be expanded. */
13127 fragp->fr_opcode = (fragp->fr_literal
13128 + fragp->fr_fix
13129 - RELAX_OLD (fragp->fr_subtype)
13130 + RELAX_RELOC1 (fragp->fr_subtype));
13131 /* FIXME: This really needs as_warn_where. */
13132 if (RELAX_WARN (fragp->fr_subtype))
13133 as_warn (_("AT used after \".set noat\" or macro used after "
13134 "\".set nomacro\""));
13135
13136 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13137 }
13138
13139 return 0;
13140 }
13141
13142 /* This is called to see whether a reloc against a defined symbol
13143 should be converted into a reloc against a section. Don't adjust
13144 MIPS16 jump relocations, so we don't have to worry about the format
13145 of the offset in the .o file. Don't adjust relocations against
13146 mips16 symbols, so that the linker can find them if it needs to set
13147 up a stub. */
13148
13149 int
13150 mips_fix_adjustable (fixp)
13151 fixS *fixp;
13152 {
13153 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13154 return 0;
13155
13156 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13157 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13158 return 0;
13159
13160 if (fixp->fx_addsy == NULL)
13161 return 1;
13162
13163 #ifdef OBJ_ELF
13164 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13165 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13166 && fixp->fx_subsy == NULL)
13167 return 0;
13168 #endif
13169
13170 return 1;
13171 }
13172
13173 /* Translate internal representation of relocation info to BFD target
13174 format. */
13175
13176 arelent **
13177 tc_gen_reloc (section, fixp)
13178 asection *section ATTRIBUTE_UNUSED;
13179 fixS *fixp;
13180 {
13181 static arelent *retval[4];
13182 arelent *reloc;
13183 bfd_reloc_code_real_type code;
13184
13185 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13186 retval[1] = NULL;
13187
13188 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13189 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13190 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13191
13192 if (mips_pic == EMBEDDED_PIC
13193 && SWITCH_TABLE (fixp))
13194 {
13195 /* For a switch table entry we use a special reloc. The addend
13196 is actually the difference between the reloc address and the
13197 subtrahend. */
13198 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13199 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13200 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13201 fixp->fx_r_type = BFD_RELOC_GPREL32;
13202 }
13203 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13204 {
13205 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13206 reloc->addend = fixp->fx_addnumber;
13207 else
13208 {
13209 /* We use a special addend for an internal RELLO reloc. */
13210 if (symbol_section_p (fixp->fx_addsy))
13211 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13212 else
13213 reloc->addend = fixp->fx_addnumber + reloc->address;
13214 }
13215 }
13216 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13217 {
13218 assert (fixp->fx_next != NULL
13219 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13220
13221 /* The reloc is relative to the RELLO; adjust the addend
13222 accordingly. */
13223 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13224 reloc->addend = fixp->fx_next->fx_addnumber;
13225 else
13226 {
13227 /* We use a special addend for an internal RELHI reloc. */
13228 if (symbol_section_p (fixp->fx_addsy))
13229 reloc->addend = (fixp->fx_next->fx_frag->fr_address
13230 + fixp->fx_next->fx_where
13231 - S_GET_VALUE (fixp->fx_subsy));
13232 else
13233 reloc->addend = (fixp->fx_addnumber
13234 + fixp->fx_next->fx_frag->fr_address
13235 + fixp->fx_next->fx_where);
13236 }
13237 }
13238 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13239 reloc->addend = fixp->fx_addnumber;
13240 else
13241 {
13242 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13243 /* A gruesome hack which is a result of the gruesome gas reloc
13244 handling. */
13245 reloc->addend = reloc->address;
13246 else
13247 reloc->addend = -reloc->address;
13248 }
13249
13250 /* If this is a variant frag, we may need to adjust the existing
13251 reloc and generate a new one. */
13252 if (fixp->fx_frag->fr_opcode != NULL
13253 && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13254 && ! HAVE_NEWABI)
13255 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13256 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13257 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13258 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13259 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13260 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13261 )
13262 {
13263 arelent *reloc2;
13264
13265 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13266
13267 /* If this is not the last reloc in this frag, then we have two
13268 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13269 CALL_HI16/CALL_LO16, both of which are being replaced. Let
13270 the second one handle all of them. */
13271 if (fixp->fx_next != NULL
13272 && fixp->fx_frag == fixp->fx_next->fx_frag)
13273 {
13274 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13275 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13276 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13277 && (fixp->fx_next->fx_r_type
13278 == BFD_RELOC_MIPS_GOT_LO16))
13279 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13280 && (fixp->fx_next->fx_r_type
13281 == BFD_RELOC_MIPS_CALL_LO16)));
13282 retval[0] = NULL;
13283 return retval;
13284 }
13285
13286 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13287 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13288 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13289 retval[2] = NULL;
13290 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13291 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13292 reloc2->address = (reloc->address
13293 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13294 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13295 reloc2->addend = fixp->fx_addnumber;
13296 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13297 assert (reloc2->howto != NULL);
13298
13299 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13300 {
13301 arelent *reloc3;
13302
13303 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13304 retval[3] = NULL;
13305 *reloc3 = *reloc2;
13306 reloc3->address += 4;
13307 }
13308
13309 if (mips_pic == NO_PIC)
13310 {
13311 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13312 fixp->fx_r_type = BFD_RELOC_HI16_S;
13313 }
13314 else if (mips_pic == SVR4_PIC)
13315 {
13316 switch (fixp->fx_r_type)
13317 {
13318 default:
13319 abort ();
13320 case BFD_RELOC_MIPS_GOT16:
13321 break;
13322 case BFD_RELOC_MIPS_GOT_LO16:
13323 case BFD_RELOC_MIPS_CALL_LO16:
13324 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13325 break;
13326 case BFD_RELOC_MIPS_CALL16:
13327 if (HAVE_NEWABI)
13328 {
13329 /* BFD_RELOC_MIPS_GOT16;*/
13330 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13331 reloc2->howto = bfd_reloc_type_lookup
13332 (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13333 }
13334 else
13335 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13336 break;
13337 }
13338 }
13339 else
13340 abort ();
13341
13342 /* newabi uses R_MIPS_GOT_DISP for local symbols */
13343 if (HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16)
13344 {
13345 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13346 retval[1] = NULL;
13347 }
13348 }
13349
13350 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13351 entry to be used in the relocation's section offset. */
13352 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13353 {
13354 reloc->address = reloc->addend;
13355 reloc->addend = 0;
13356 }
13357
13358 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13359 fixup_segment converted a non-PC relative reloc into a PC
13360 relative reloc. In such a case, we need to convert the reloc
13361 code. */
13362 code = fixp->fx_r_type;
13363 if (fixp->fx_pcrel)
13364 {
13365 switch (code)
13366 {
13367 case BFD_RELOC_8:
13368 code = BFD_RELOC_8_PCREL;
13369 break;
13370 case BFD_RELOC_16:
13371 code = BFD_RELOC_16_PCREL;
13372 break;
13373 case BFD_RELOC_32:
13374 code = BFD_RELOC_32_PCREL;
13375 break;
13376 case BFD_RELOC_64:
13377 code = BFD_RELOC_64_PCREL;
13378 break;
13379 case BFD_RELOC_8_PCREL:
13380 case BFD_RELOC_16_PCREL:
13381 case BFD_RELOC_32_PCREL:
13382 case BFD_RELOC_64_PCREL:
13383 case BFD_RELOC_16_PCREL_S2:
13384 case BFD_RELOC_MIPSEMB_16_PCREL_S2:
13385 case BFD_RELOC_PCREL_HI16_S:
13386 case BFD_RELOC_PCREL_LO16:
13387 break;
13388 default:
13389 as_bad_where (fixp->fx_file, fixp->fx_line,
13390 _("Cannot make %s relocation PC relative"),
13391 bfd_get_reloc_code_name (code));
13392 }
13393 }
13394
13395 #ifdef OBJ_ELF
13396 /* md_apply_fix3 has a double-subtraction hack to get
13397 bfd_install_relocation to behave nicely. GPREL relocations are
13398 handled correctly without this hack, so undo it here. We can't
13399 stop md_apply_fix3 from subtracting twice in the first place since
13400 the fake addend is required for variant frags above. */
13401 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
13402 && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
13403 && reloc->addend != 0
13404 && mips_need_elf_addend_fixup (fixp))
13405 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13406 #endif
13407
13408 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13409 if (reloc->howto == NULL)
13410 {
13411 as_bad_where (fixp->fx_file, fixp->fx_line,
13412 _("Can not represent %s relocation in this object file format"),
13413 bfd_get_reloc_code_name (code));
13414 retval[0] = NULL;
13415 }
13416
13417 return retval;
13418 }
13419
13420 /* Relax a machine dependent frag. This returns the amount by which
13421 the current size of the frag should change. */
13422
13423 int
13424 mips_relax_frag (sec, fragp, stretch)
13425 asection *sec;
13426 fragS *fragp;
13427 long stretch;
13428 {
13429 if (RELAX_BRANCH_P (fragp->fr_subtype))
13430 {
13431 offsetT old_var = fragp->fr_var;
13432
13433 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13434
13435 return fragp->fr_var - old_var;
13436 }
13437
13438 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13439 return 0;
13440
13441 if (mips16_extended_frag (fragp, NULL, stretch))
13442 {
13443 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13444 return 0;
13445 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13446 return 2;
13447 }
13448 else
13449 {
13450 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13451 return 0;
13452 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13453 return -2;
13454 }
13455
13456 return 0;
13457 }
13458
13459 /* Convert a machine dependent frag. */
13460
13461 void
13462 md_convert_frag (abfd, asec, fragp)
13463 bfd *abfd ATTRIBUTE_UNUSED;
13464 segT asec;
13465 fragS *fragp;
13466 {
13467 int old, new;
13468 char *fixptr;
13469
13470 if (RELAX_BRANCH_P (fragp->fr_subtype))
13471 {
13472 bfd_byte *buf;
13473 unsigned long insn;
13474 expressionS exp;
13475 fixS *fixp;
13476
13477 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13478
13479 if (target_big_endian)
13480 insn = bfd_getb32 (buf);
13481 else
13482 insn = bfd_getl32 (buf);
13483
13484 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13485 {
13486 /* We generate a fixup instead of applying it right now
13487 because, if there are linker relaxations, we're going to
13488 need the relocations. */
13489 exp.X_op = O_symbol;
13490 exp.X_add_symbol = fragp->fr_symbol;
13491 exp.X_add_number = fragp->fr_offset;
13492
13493 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13494 4, &exp, 1, ((mips_pic == EMBEDDED_PIC)
13495 ? BFD_RELOC_MIPSEMB_16_PCREL_S2
13496 : BFD_RELOC_16_PCREL_S2));
13497 fixp->fx_file = fragp->fr_file;
13498 fixp->fx_line = fragp->fr_line;
13499
13500 md_number_to_chars ((char *)buf, insn, 4);
13501 buf += 4;
13502 }
13503 else
13504 {
13505 int i;
13506
13507 as_warn_where (fragp->fr_file, fragp->fr_line,
13508 _("relaxed out-of-range branch into a jump"));
13509
13510 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13511 goto uncond;
13512
13513 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13514 {
13515 /* Reverse the branch. */
13516 switch ((insn >> 28) & 0xf)
13517 {
13518 case 4:
13519 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13520 have the condition reversed by tweaking a single
13521 bit, and their opcodes all have 0x4???????. */
13522 assert ((insn & 0xf1000000) == 0x41000000);
13523 insn ^= 0x00010000;
13524 break;
13525
13526 case 0:
13527 /* bltz 0x04000000 bgez 0x04010000
13528 bltzal 0x04100000 bgezal 0x04110000 */
13529 assert ((insn & 0xfc0e0000) == 0x04000000);
13530 insn ^= 0x00010000;
13531 break;
13532
13533 case 1:
13534 /* beq 0x10000000 bne 0x14000000
13535 blez 0x18000000 bgtz 0x1c000000 */
13536 insn ^= 0x04000000;
13537 break;
13538
13539 default:
13540 abort ();
13541 }
13542 }
13543
13544 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13545 {
13546 /* Clear the and-link bit. */
13547 assert ((insn & 0xfc1c0000) == 0x04100000);
13548
13549 /* bltzal 0x04100000 bgezal 0x04110000
13550 bltzall 0x04120000 bgezall 0x04130000 */
13551 insn &= ~0x00100000;
13552 }
13553
13554 /* Branch over the branch (if the branch was likely) or the
13555 full jump (not likely case). Compute the offset from the
13556 current instruction to branch to. */
13557 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13558 i = 16;
13559 else
13560 {
13561 /* How many bytes in instructions we've already emitted? */
13562 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13563 /* How many bytes in instructions from here to the end? */
13564 i = fragp->fr_var - i;
13565 }
13566 /* Convert to instruction count. */
13567 i >>= 2;
13568 /* Branch counts from the next instruction. */
13569 i--;
13570 insn |= i;
13571 /* Branch over the jump. */
13572 md_number_to_chars ((char *)buf, insn, 4);
13573 buf += 4;
13574
13575 /* Nop */
13576 md_number_to_chars ((char*)buf, 0, 4);
13577 buf += 4;
13578
13579 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13580 {
13581 /* beql $0, $0, 2f */
13582 insn = 0x50000000;
13583 /* Compute the PC offset from the current instruction to
13584 the end of the variable frag. */
13585 /* How many bytes in instructions we've already emitted? */
13586 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13587 /* How many bytes in instructions from here to the end? */
13588 i = fragp->fr_var - i;
13589 /* Convert to instruction count. */
13590 i >>= 2;
13591 /* Don't decrement i, because we want to branch over the
13592 delay slot. */
13593
13594 insn |= i;
13595 md_number_to_chars ((char *)buf, insn, 4);
13596 buf += 4;
13597
13598 md_number_to_chars ((char *)buf, 0, 4);
13599 buf += 4;
13600 }
13601
13602 uncond:
13603 if (mips_pic == NO_PIC)
13604 {
13605 /* j or jal. */
13606 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13607 ? 0x0c000000 : 0x08000000);
13608 exp.X_op = O_symbol;
13609 exp.X_add_symbol = fragp->fr_symbol;
13610 exp.X_add_number = fragp->fr_offset;
13611
13612 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13613 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13614 fixp->fx_file = fragp->fr_file;
13615 fixp->fx_line = fragp->fr_line;
13616
13617 md_number_to_chars ((char*)buf, insn, 4);
13618 buf += 4;
13619 }
13620 else
13621 {
13622 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13623 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13624 exp.X_op = O_symbol;
13625 exp.X_add_symbol = fragp->fr_symbol;
13626 exp.X_add_number = fragp->fr_offset;
13627
13628 if (fragp->fr_offset)
13629 {
13630 exp.X_add_symbol = make_expr_symbol (&exp);
13631 exp.X_add_number = 0;
13632 }
13633
13634 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13635 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13636 fixp->fx_file = fragp->fr_file;
13637 fixp->fx_line = fragp->fr_line;
13638
13639 md_number_to_chars ((char*)buf, insn, 4);
13640 buf += 4;
13641
13642 if (mips_opts.isa == ISA_MIPS1)
13643 {
13644 /* nop */
13645 md_number_to_chars ((char*)buf, 0, 4);
13646 buf += 4;
13647 }
13648
13649 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13650 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13651
13652 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13653 4, &exp, 0, BFD_RELOC_LO16);
13654 fixp->fx_file = fragp->fr_file;
13655 fixp->fx_line = fragp->fr_line;
13656
13657 md_number_to_chars ((char*)buf, insn, 4);
13658 buf += 4;
13659
13660 /* j(al)r $at. */
13661 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13662 insn = 0x0020f809;
13663 else
13664 insn = 0x00200008;
13665
13666 md_number_to_chars ((char*)buf, insn, 4);
13667 buf += 4;
13668 }
13669 }
13670
13671 assert (buf == (bfd_byte *)fragp->fr_literal
13672 + fragp->fr_fix + fragp->fr_var);
13673
13674 fragp->fr_fix += fragp->fr_var;
13675
13676 return;
13677 }
13678
13679 if (RELAX_MIPS16_P (fragp->fr_subtype))
13680 {
13681 int type;
13682 register const struct mips16_immed_operand *op;
13683 bfd_boolean small, ext;
13684 offsetT val;
13685 bfd_byte *buf;
13686 unsigned long insn;
13687 bfd_boolean use_extend;
13688 unsigned short extend;
13689
13690 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13691 op = mips16_immed_operands;
13692 while (op->type != type)
13693 ++op;
13694
13695 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13696 {
13697 small = FALSE;
13698 ext = TRUE;
13699 }
13700 else
13701 {
13702 small = TRUE;
13703 ext = FALSE;
13704 }
13705
13706 resolve_symbol_value (fragp->fr_symbol);
13707 val = S_GET_VALUE (fragp->fr_symbol);
13708 if (op->pcrel)
13709 {
13710 addressT addr;
13711
13712 addr = fragp->fr_address + fragp->fr_fix;
13713
13714 /* The rules for the base address of a PC relative reloc are
13715 complicated; see mips16_extended_frag. */
13716 if (type == 'p' || type == 'q')
13717 {
13718 addr += 2;
13719 if (ext)
13720 addr += 2;
13721 /* Ignore the low bit in the target, since it will be
13722 set for a text label. */
13723 if ((val & 1) != 0)
13724 --val;
13725 }
13726 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13727 addr -= 4;
13728 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13729 addr -= 2;
13730
13731 addr &= ~ (addressT) ((1 << op->shift) - 1);
13732 val -= addr;
13733
13734 /* Make sure the section winds up with the alignment we have
13735 assumed. */
13736 if (op->shift > 0)
13737 record_alignment (asec, op->shift);
13738 }
13739
13740 if (ext
13741 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13742 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13743 as_warn_where (fragp->fr_file, fragp->fr_line,
13744 _("extended instruction in delay slot"));
13745
13746 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13747
13748 if (target_big_endian)
13749 insn = bfd_getb16 (buf);
13750 else
13751 insn = bfd_getl16 (buf);
13752
13753 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13754 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13755 small, ext, &insn, &use_extend, &extend);
13756
13757 if (use_extend)
13758 {
13759 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13760 fragp->fr_fix += 2;
13761 buf += 2;
13762 }
13763
13764 md_number_to_chars ((char *) buf, insn, 2);
13765 fragp->fr_fix += 2;
13766 buf += 2;
13767 }
13768 else
13769 {
13770 if (fragp->fr_opcode == NULL)
13771 return;
13772
13773 old = RELAX_OLD (fragp->fr_subtype);
13774 new = RELAX_NEW (fragp->fr_subtype);
13775 fixptr = fragp->fr_literal + fragp->fr_fix;
13776
13777 if (new > 0)
13778 memcpy (fixptr - old, fixptr, new);
13779
13780 fragp->fr_fix += new - old;
13781 }
13782 }
13783
13784 #ifdef OBJ_ELF
13785
13786 /* This function is called after the relocs have been generated.
13787 We've been storing mips16 text labels as odd. Here we convert them
13788 back to even for the convenience of the debugger. */
13789
13790 void
13791 mips_frob_file_after_relocs ()
13792 {
13793 asymbol **syms;
13794 unsigned int count, i;
13795
13796 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13797 return;
13798
13799 syms = bfd_get_outsymbols (stdoutput);
13800 count = bfd_get_symcount (stdoutput);
13801 for (i = 0; i < count; i++, syms++)
13802 {
13803 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13804 && ((*syms)->value & 1) != 0)
13805 {
13806 (*syms)->value &= ~1;
13807 /* If the symbol has an odd size, it was probably computed
13808 incorrectly, so adjust that as well. */
13809 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13810 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13811 }
13812 }
13813 }
13814
13815 #endif
13816
13817 /* This function is called whenever a label is defined. It is used
13818 when handling branch delays; if a branch has a label, we assume we
13819 can not move it. */
13820
13821 void
13822 mips_define_label (sym)
13823 symbolS *sym;
13824 {
13825 struct insn_label_list *l;
13826
13827 if (free_insn_labels == NULL)
13828 l = (struct insn_label_list *) xmalloc (sizeof *l);
13829 else
13830 {
13831 l = free_insn_labels;
13832 free_insn_labels = l->next;
13833 }
13834
13835 l->label = sym;
13836 l->next = insn_labels;
13837 insn_labels = l;
13838 }
13839 \f
13840 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13841
13842 /* Some special processing for a MIPS ELF file. */
13843
13844 void
13845 mips_elf_final_processing ()
13846 {
13847 /* Write out the register information. */
13848 if (mips_abi != N64_ABI)
13849 {
13850 Elf32_RegInfo s;
13851
13852 s.ri_gprmask = mips_gprmask;
13853 s.ri_cprmask[0] = mips_cprmask[0];
13854 s.ri_cprmask[1] = mips_cprmask[1];
13855 s.ri_cprmask[2] = mips_cprmask[2];
13856 s.ri_cprmask[3] = mips_cprmask[3];
13857 /* The gp_value field is set by the MIPS ELF backend. */
13858
13859 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13860 ((Elf32_External_RegInfo *)
13861 mips_regmask_frag));
13862 }
13863 else
13864 {
13865 Elf64_Internal_RegInfo s;
13866
13867 s.ri_gprmask = mips_gprmask;
13868 s.ri_pad = 0;
13869 s.ri_cprmask[0] = mips_cprmask[0];
13870 s.ri_cprmask[1] = mips_cprmask[1];
13871 s.ri_cprmask[2] = mips_cprmask[2];
13872 s.ri_cprmask[3] = mips_cprmask[3];
13873 /* The gp_value field is set by the MIPS ELF backend. */
13874
13875 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13876 ((Elf64_External_RegInfo *)
13877 mips_regmask_frag));
13878 }
13879
13880 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13881 sort of BFD interface for this. */
13882 if (mips_any_noreorder)
13883 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13884 if (mips_pic != NO_PIC)
13885 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13886
13887 /* Set MIPS ELF flags for ASEs. */
13888 if (file_ase_mips16)
13889 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13890 #if 0 /* XXX FIXME */
13891 if (file_ase_mips3d)
13892 elf_elfheader (stdoutput)->e_flags |= ???;
13893 #endif
13894 if (file_ase_mdmx)
13895 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13896
13897 /* Set the MIPS ELF ABI flags. */
13898 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13899 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13900 else if (mips_abi == O64_ABI)
13901 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13902 else if (mips_abi == EABI_ABI)
13903 {
13904 if (!file_mips_gp32)
13905 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13906 else
13907 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13908 }
13909 else if (mips_abi == N32_ABI)
13910 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13911
13912 /* Nothing to do for N64_ABI. */
13913
13914 if (mips_32bitmode)
13915 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13916 }
13917
13918 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13919 \f
13920 typedef struct proc {
13921 symbolS *isym;
13922 unsigned long reg_mask;
13923 unsigned long reg_offset;
13924 unsigned long fpreg_mask;
13925 unsigned long fpreg_offset;
13926 unsigned long frame_offset;
13927 unsigned long frame_reg;
13928 unsigned long pc_reg;
13929 } procS;
13930
13931 static procS cur_proc;
13932 static procS *cur_proc_ptr;
13933 static int numprocs;
13934
13935 /* Fill in an rs_align_code fragment. */
13936
13937 void
13938 mips_handle_align (fragp)
13939 fragS *fragp;
13940 {
13941 if (fragp->fr_type != rs_align_code)
13942 return;
13943
13944 if (mips_opts.mips16)
13945 {
13946 static const unsigned char be_nop[] = { 0x65, 0x00 };
13947 static const unsigned char le_nop[] = { 0x00, 0x65 };
13948
13949 int bytes;
13950 char *p;
13951
13952 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13953 p = fragp->fr_literal + fragp->fr_fix;
13954
13955 if (bytes & 1)
13956 {
13957 *p++ = 0;
13958 fragp->fr_fix++;
13959 }
13960
13961 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13962 fragp->fr_var = 2;
13963 }
13964
13965 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
13966 }
13967
13968 static void
13969 md_obj_begin ()
13970 {
13971 }
13972
13973 static void
13974 md_obj_end ()
13975 {
13976 /* check for premature end, nesting errors, etc */
13977 if (cur_proc_ptr)
13978 as_warn (_("missing .end at end of assembly"));
13979 }
13980
13981 static long
13982 get_number ()
13983 {
13984 int negative = 0;
13985 long val = 0;
13986
13987 if (*input_line_pointer == '-')
13988 {
13989 ++input_line_pointer;
13990 negative = 1;
13991 }
13992 if (!ISDIGIT (*input_line_pointer))
13993 as_bad (_("expected simple number"));
13994 if (input_line_pointer[0] == '0')
13995 {
13996 if (input_line_pointer[1] == 'x')
13997 {
13998 input_line_pointer += 2;
13999 while (ISXDIGIT (*input_line_pointer))
14000 {
14001 val <<= 4;
14002 val |= hex_value (*input_line_pointer++);
14003 }
14004 return negative ? -val : val;
14005 }
14006 else
14007 {
14008 ++input_line_pointer;
14009 while (ISDIGIT (*input_line_pointer))
14010 {
14011 val <<= 3;
14012 val |= *input_line_pointer++ - '0';
14013 }
14014 return negative ? -val : val;
14015 }
14016 }
14017 if (!ISDIGIT (*input_line_pointer))
14018 {
14019 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14020 *input_line_pointer, *input_line_pointer);
14021 as_warn (_("invalid number"));
14022 return -1;
14023 }
14024 while (ISDIGIT (*input_line_pointer))
14025 {
14026 val *= 10;
14027 val += *input_line_pointer++ - '0';
14028 }
14029 return negative ? -val : val;
14030 }
14031
14032 /* The .file directive; just like the usual .file directive, but there
14033 is an initial number which is the ECOFF file index. In the non-ECOFF
14034 case .file implies DWARF-2. */
14035
14036 static void
14037 s_mips_file (x)
14038 int x ATTRIBUTE_UNUSED;
14039 {
14040 static int first_file_directive = 0;
14041
14042 if (ECOFF_DEBUGGING)
14043 {
14044 get_number ();
14045 s_app_file (0);
14046 }
14047 else
14048 {
14049 char *filename;
14050
14051 filename = dwarf2_directive_file (0);
14052
14053 /* Versions of GCC up to 3.1 start files with a ".file"
14054 directive even for stabs output. Make sure that this
14055 ".file" is handled. Note that you need a version of GCC
14056 after 3.1 in order to support DWARF-2 on MIPS. */
14057 if (filename != NULL && ! first_file_directive)
14058 {
14059 (void) new_logical_line (filename, -1);
14060 s_app_file_string (filename);
14061 }
14062 first_file_directive = 1;
14063 }
14064 }
14065
14066 /* The .loc directive, implying DWARF-2. */
14067
14068 static void
14069 s_mips_loc (x)
14070 int x ATTRIBUTE_UNUSED;
14071 {
14072 if (!ECOFF_DEBUGGING)
14073 dwarf2_directive_loc (0);
14074 }
14075
14076 /* The .end directive. */
14077
14078 static void
14079 s_mips_end (x)
14080 int x ATTRIBUTE_UNUSED;
14081 {
14082 symbolS *p;
14083 int maybe_text;
14084
14085 /* Following functions need their own .frame and .cprestore directives. */
14086 mips_frame_reg_valid = 0;
14087 mips_cprestore_valid = 0;
14088
14089 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14090 {
14091 p = get_symbol ();
14092 demand_empty_rest_of_line ();
14093 }
14094 else
14095 p = NULL;
14096
14097 #ifdef BFD_ASSEMBLER
14098 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14099 maybe_text = 1;
14100 else
14101 maybe_text = 0;
14102 #else
14103 if (now_seg != data_section && now_seg != bss_section)
14104 maybe_text = 1;
14105 else
14106 maybe_text = 0;
14107 #endif
14108
14109 if (!maybe_text)
14110 as_warn (_(".end not in text section"));
14111
14112 if (!cur_proc_ptr)
14113 {
14114 as_warn (_(".end directive without a preceding .ent directive."));
14115 demand_empty_rest_of_line ();
14116 return;
14117 }
14118
14119 if (p != NULL)
14120 {
14121 assert (S_GET_NAME (p));
14122 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14123 as_warn (_(".end symbol does not match .ent symbol."));
14124
14125 if (debug_type == DEBUG_STABS)
14126 stabs_generate_asm_endfunc (S_GET_NAME (p),
14127 S_GET_NAME (p));
14128 }
14129 else
14130 as_warn (_(".end directive missing or unknown symbol"));
14131
14132 #ifdef OBJ_ELF
14133 /* Generate a .pdr section. */
14134 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14135 {
14136 segT saved_seg = now_seg;
14137 subsegT saved_subseg = now_subseg;
14138 valueT dot;
14139 expressionS exp;
14140 char *fragp;
14141
14142 dot = frag_now_fix ();
14143
14144 #ifdef md_flush_pending_output
14145 md_flush_pending_output ();
14146 #endif
14147
14148 assert (pdr_seg);
14149 subseg_set (pdr_seg, 0);
14150
14151 /* Write the symbol. */
14152 exp.X_op = O_symbol;
14153 exp.X_add_symbol = p;
14154 exp.X_add_number = 0;
14155 emit_expr (&exp, 4);
14156
14157 fragp = frag_more (7 * 4);
14158
14159 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
14160 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
14161 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14162 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14163 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14164 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14165 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14166
14167 subseg_set (saved_seg, saved_subseg);
14168 }
14169 #endif /* OBJ_ELF */
14170
14171 cur_proc_ptr = NULL;
14172 }
14173
14174 /* The .aent and .ent directives. */
14175
14176 static void
14177 s_mips_ent (aent)
14178 int aent;
14179 {
14180 symbolS *symbolP;
14181 int maybe_text;
14182
14183 symbolP = get_symbol ();
14184 if (*input_line_pointer == ',')
14185 ++input_line_pointer;
14186 SKIP_WHITESPACE ();
14187 if (ISDIGIT (*input_line_pointer)
14188 || *input_line_pointer == '-')
14189 get_number ();
14190
14191 #ifdef BFD_ASSEMBLER
14192 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14193 maybe_text = 1;
14194 else
14195 maybe_text = 0;
14196 #else
14197 if (now_seg != data_section && now_seg != bss_section)
14198 maybe_text = 1;
14199 else
14200 maybe_text = 0;
14201 #endif
14202
14203 if (!maybe_text)
14204 as_warn (_(".ent or .aent not in text section."));
14205
14206 if (!aent && cur_proc_ptr)
14207 as_warn (_("missing .end"));
14208
14209 if (!aent)
14210 {
14211 /* This function needs its own .frame and .cprestore directives. */
14212 mips_frame_reg_valid = 0;
14213 mips_cprestore_valid = 0;
14214
14215 cur_proc_ptr = &cur_proc;
14216 memset (cur_proc_ptr, '\0', sizeof (procS));
14217
14218 cur_proc_ptr->isym = symbolP;
14219
14220 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14221
14222 ++numprocs;
14223
14224 if (debug_type == DEBUG_STABS)
14225 stabs_generate_asm_func (S_GET_NAME (symbolP),
14226 S_GET_NAME (symbolP));
14227 }
14228
14229 demand_empty_rest_of_line ();
14230 }
14231
14232 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14233 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14234 s_mips_frame is used so that we can set the PDR information correctly.
14235 We can't use the ecoff routines because they make reference to the ecoff
14236 symbol table (in the mdebug section). */
14237
14238 static void
14239 s_mips_frame (ignore)
14240 int ignore ATTRIBUTE_UNUSED;
14241 {
14242 #ifdef OBJ_ELF
14243 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14244 {
14245 long val;
14246
14247 if (cur_proc_ptr == (procS *) NULL)
14248 {
14249 as_warn (_(".frame outside of .ent"));
14250 demand_empty_rest_of_line ();
14251 return;
14252 }
14253
14254 cur_proc_ptr->frame_reg = tc_get_register (1);
14255
14256 SKIP_WHITESPACE ();
14257 if (*input_line_pointer++ != ','
14258 || get_absolute_expression_and_terminator (&val) != ',')
14259 {
14260 as_warn (_("Bad .frame directive"));
14261 --input_line_pointer;
14262 demand_empty_rest_of_line ();
14263 return;
14264 }
14265
14266 cur_proc_ptr->frame_offset = val;
14267 cur_proc_ptr->pc_reg = tc_get_register (0);
14268
14269 demand_empty_rest_of_line ();
14270 }
14271 else
14272 #endif /* OBJ_ELF */
14273 s_ignore (ignore);
14274 }
14275
14276 /* The .fmask and .mask directives. If the mdebug section is present
14277 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14278 embedded targets, s_mips_mask is used so that we can set the PDR
14279 information correctly. We can't use the ecoff routines because they
14280 make reference to the ecoff symbol table (in the mdebug section). */
14281
14282 static void
14283 s_mips_mask (reg_type)
14284 char reg_type;
14285 {
14286 #ifdef OBJ_ELF
14287 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14288 {
14289 long mask, off;
14290
14291 if (cur_proc_ptr == (procS *) NULL)
14292 {
14293 as_warn (_(".mask/.fmask outside of .ent"));
14294 demand_empty_rest_of_line ();
14295 return;
14296 }
14297
14298 if (get_absolute_expression_and_terminator (&mask) != ',')
14299 {
14300 as_warn (_("Bad .mask/.fmask directive"));
14301 --input_line_pointer;
14302 demand_empty_rest_of_line ();
14303 return;
14304 }
14305
14306 off = get_absolute_expression ();
14307
14308 if (reg_type == 'F')
14309 {
14310 cur_proc_ptr->fpreg_mask = mask;
14311 cur_proc_ptr->fpreg_offset = off;
14312 }
14313 else
14314 {
14315 cur_proc_ptr->reg_mask = mask;
14316 cur_proc_ptr->reg_offset = off;
14317 }
14318
14319 demand_empty_rest_of_line ();
14320 }
14321 else
14322 #endif /* OBJ_ELF */
14323 s_ignore (reg_type);
14324 }
14325
14326 /* The .loc directive. */
14327
14328 #if 0
14329 static void
14330 s_loc (x)
14331 int x;
14332 {
14333 symbolS *symbolP;
14334 int lineno;
14335 int addroff;
14336
14337 assert (now_seg == text_section);
14338
14339 lineno = get_number ();
14340 addroff = frag_now_fix ();
14341
14342 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14343 S_SET_TYPE (symbolP, N_SLINE);
14344 S_SET_OTHER (symbolP, 0);
14345 S_SET_DESC (symbolP, lineno);
14346 symbolP->sy_segment = now_seg;
14347 }
14348 #endif
14349
14350 /* A table describing all the processors gas knows about. Names are
14351 matched in the order listed.
14352
14353 To ease comparison, please keep this table in the same order as
14354 gcc's mips_cpu_info_table[]. */
14355 static const struct mips_cpu_info mips_cpu_info_table[] =
14356 {
14357 /* Entries for generic ISAs */
14358 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14359 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14360 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14361 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14362 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14363 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
14364 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
14365 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
14366
14367 /* MIPS I */
14368 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14369 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14370 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14371
14372 /* MIPS II */
14373 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14374
14375 /* MIPS III */
14376 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14377 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14378 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14379 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
14380 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14381 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14382 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
14383 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14384 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14385 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14386 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14387 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14388
14389 /* MIPS IV */
14390 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14391 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14392 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14393 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
14394 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14395 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
14396 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14397 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14398 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14399 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14400 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14401 { "r7000", 0, ISA_MIPS4, CPU_R5000 },
14402
14403 /* MIPS 32 */
14404 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
14405 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14406 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
14407
14408 /* MIPS 64 */
14409 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14410 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
14411
14412 /* Broadcom SB-1 CPU core */
14413 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
14414
14415 /* End marker */
14416 { NULL, 0, 0, 0 }
14417 };
14418
14419
14420 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14421 with a final "000" replaced by "k". Ignore case.
14422
14423 Note: this function is shared between GCC and GAS. */
14424
14425 static bfd_boolean
14426 mips_strict_matching_cpu_name_p (canonical, given)
14427 const char *canonical, *given;
14428 {
14429 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14430 given++, canonical++;
14431
14432 return ((*given == 0 && *canonical == 0)
14433 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14434 }
14435
14436
14437 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14438 CPU name. We've traditionally allowed a lot of variation here.
14439
14440 Note: this function is shared between GCC and GAS. */
14441
14442 static bfd_boolean
14443 mips_matching_cpu_name_p (canonical, given)
14444 const char *canonical, *given;
14445 {
14446 /* First see if the name matches exactly, or with a final "000"
14447 turned into "k". */
14448 if (mips_strict_matching_cpu_name_p (canonical, given))
14449 return TRUE;
14450
14451 /* If not, try comparing based on numerical designation alone.
14452 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14453 if (TOLOWER (*given) == 'r')
14454 given++;
14455 if (!ISDIGIT (*given))
14456 return FALSE;
14457
14458 /* Skip over some well-known prefixes in the canonical name,
14459 hoping to find a number there too. */
14460 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14461 canonical += 2;
14462 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14463 canonical += 2;
14464 else if (TOLOWER (canonical[0]) == 'r')
14465 canonical += 1;
14466
14467 return mips_strict_matching_cpu_name_p (canonical, given);
14468 }
14469
14470
14471 /* Parse an option that takes the name of a processor as its argument.
14472 OPTION is the name of the option and CPU_STRING is the argument.
14473 Return the corresponding processor enumeration if the CPU_STRING is
14474 recognized, otherwise report an error and return null.
14475
14476 A similar function exists in GCC. */
14477
14478 static const struct mips_cpu_info *
14479 mips_parse_cpu (option, cpu_string)
14480 const char *option, *cpu_string;
14481 {
14482 const struct mips_cpu_info *p;
14483
14484 /* 'from-abi' selects the most compatible architecture for the given
14485 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14486 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14487 version. Look first at the -mgp options, if given, otherwise base
14488 the choice on MIPS_DEFAULT_64BIT.
14489
14490 Treat NO_ABI like the EABIs. One reason to do this is that the
14491 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14492 architecture. This code picks MIPS I for 'mips' and MIPS III for
14493 'mips64', just as we did in the days before 'from-abi'. */
14494 if (strcasecmp (cpu_string, "from-abi") == 0)
14495 {
14496 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14497 return mips_cpu_info_from_isa (ISA_MIPS1);
14498
14499 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14500 return mips_cpu_info_from_isa (ISA_MIPS3);
14501
14502 if (file_mips_gp32 >= 0)
14503 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14504
14505 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14506 ? ISA_MIPS3
14507 : ISA_MIPS1);
14508 }
14509
14510 /* 'default' has traditionally been a no-op. Probably not very useful. */
14511 if (strcasecmp (cpu_string, "default") == 0)
14512 return 0;
14513
14514 for (p = mips_cpu_info_table; p->name != 0; p++)
14515 if (mips_matching_cpu_name_p (p->name, cpu_string))
14516 return p;
14517
14518 as_bad ("Bad value (%s) for %s", cpu_string, option);
14519 return 0;
14520 }
14521
14522 /* Return the canonical processor information for ISA (a member of the
14523 ISA_MIPS* enumeration). */
14524
14525 static const struct mips_cpu_info *
14526 mips_cpu_info_from_isa (isa)
14527 int isa;
14528 {
14529 int i;
14530
14531 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14532 if (mips_cpu_info_table[i].is_isa
14533 && isa == mips_cpu_info_table[i].isa)
14534 return (&mips_cpu_info_table[i]);
14535
14536 return NULL;
14537 }
14538 \f
14539 static void
14540 show (stream, string, col_p, first_p)
14541 FILE *stream;
14542 const char *string;
14543 int *col_p;
14544 int *first_p;
14545 {
14546 if (*first_p)
14547 {
14548 fprintf (stream, "%24s", "");
14549 *col_p = 24;
14550 }
14551 else
14552 {
14553 fprintf (stream, ", ");
14554 *col_p += 2;
14555 }
14556
14557 if (*col_p + strlen (string) > 72)
14558 {
14559 fprintf (stream, "\n%24s", "");
14560 *col_p = 24;
14561 }
14562
14563 fprintf (stream, "%s", string);
14564 *col_p += strlen (string);
14565
14566 *first_p = 0;
14567 }
14568
14569 void
14570 md_show_usage (stream)
14571 FILE *stream;
14572 {
14573 int column, first;
14574 size_t i;
14575
14576 fprintf (stream, _("\
14577 MIPS options:\n\
14578 -membedded-pic generate embedded position independent code\n\
14579 -EB generate big endian output\n\
14580 -EL generate little endian output\n\
14581 -g, -g2 do not remove unneeded NOPs or swap branches\n\
14582 -G NUM allow referencing objects up to NUM bytes\n\
14583 implicitly with the gp register [default 8]\n"));
14584 fprintf (stream, _("\
14585 -mips1 generate MIPS ISA I instructions\n\
14586 -mips2 generate MIPS ISA II instructions\n\
14587 -mips3 generate MIPS ISA III instructions\n\
14588 -mips4 generate MIPS ISA IV instructions\n\
14589 -mips5 generate MIPS ISA V instructions\n\
14590 -mips32 generate MIPS32 ISA instructions\n\
14591 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
14592 -mips64 generate MIPS64 ISA instructions\n\
14593 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14594
14595 first = 1;
14596
14597 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14598 show (stream, mips_cpu_info_table[i].name, &column, &first);
14599 show (stream, "from-abi", &column, &first);
14600 fputc ('\n', stream);
14601
14602 fprintf (stream, _("\
14603 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14604 -no-mCPU don't generate code specific to CPU.\n\
14605 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14606
14607 first = 1;
14608
14609 show (stream, "3900", &column, &first);
14610 show (stream, "4010", &column, &first);
14611 show (stream, "4100", &column, &first);
14612 show (stream, "4650", &column, &first);
14613 fputc ('\n', stream);
14614
14615 fprintf (stream, _("\
14616 -mips16 generate mips16 instructions\n\
14617 -no-mips16 do not generate mips16 instructions\n"));
14618 fprintf (stream, _("\
14619 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14620 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14621 -O0 remove unneeded NOPs, do not swap branches\n\
14622 -O remove unneeded NOPs and swap branches\n\
14623 -n warn about NOPs generated from macros\n\
14624 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14625 --trap, --no-break trap exception on div by 0 and mult overflow\n\
14626 --break, --no-trap break exception on div by 0 and mult overflow\n"));
14627 #ifdef OBJ_ELF
14628 fprintf (stream, _("\
14629 -KPIC, -call_shared generate SVR4 position independent code\n\
14630 -non_shared do not generate position independent code\n\
14631 -xgot assume a 32 bit GOT\n\
14632 -mabi=ABI create ABI conformant object file for:\n"));
14633
14634 first = 1;
14635
14636 show (stream, "32", &column, &first);
14637 show (stream, "o64", &column, &first);
14638 show (stream, "n32", &column, &first);
14639 show (stream, "64", &column, &first);
14640 show (stream, "eabi", &column, &first);
14641
14642 fputc ('\n', stream);
14643
14644 fprintf (stream, _("\
14645 -32 create o32 ABI object file (default)\n\
14646 -n32 create n32 ABI object file\n\
14647 -64 create 64 ABI object file\n"));
14648 #endif
14649 }
14650
14651 enum dwarf2_format
14652 mips_dwarf2_format ()
14653 {
14654 if (mips_abi == N64_ABI)
14655 {
14656 #ifdef TE_IRIX
14657 return dwarf2_format_64bit_irix;
14658 #else
14659 return dwarf2_format_64bit;
14660 #endif
14661 }
14662 else
14663 return dwarf2_format_32bit;
14664 }
This page took 0.336316 seconds and 5 git commands to generate.