* symbols.c (S_FORCE_RELOC): Add "strict" param.
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
071742cf 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
82efde3a 3 Free Software Foundation, Inc.
252b5132
RH
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"
3882b010 29#include "safe-ctype.h"
252b5132
RH
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"
c5dd6aab 40#include "dwarf2dbg.h"
252b5132
RH
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. */
50static int mips_output_flavor PARAMS ((void));
51static 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
252b5132
RH
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
ecb4347a
DJ
80int mips_flag_mdebug = -1;
81
252b5132
RH
82#include "ecoff.h"
83
84#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
85static char *mips_regmask_frag;
86#endif
87
85b51719 88#define ZERO 0
252b5132
RH
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
107extern int target_big_endian;
108
252b5132
RH
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" \
056350c6
NC
114 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
115 ? ".rdata" \
252b5132
RH
116 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117 ? ".rodata" \
118 : (abort (), ""))
119
a325df1d
TS
120/* The ABI to use. */
121enum 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. */
316f5878 132static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 133
252b5132
RH
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
e972090a
NC
138struct mips_set_options
139{
252b5132
RH
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;
1f25f5d3
CD
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;
deec1734 148 int ase_mdmx;
252b5132
RH
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;
a325df1d
TS
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;
252b5132
RH
180};
181
a325df1d 182/* True if -mgp32 was passed. */
a8e8e863 183static int file_mips_gp32 = -1;
a325df1d
TS
184
185/* True if -mfp32 was passed. */
a8e8e863 186static int file_mips_fp32 = -1;
a325df1d 187
252b5132 188/* This is the struct we use to hold the current set of options. Note
a4672219 189 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
e7af610e 190 -1 to indicate that they have not been initialized. */
252b5132 191
e972090a
NC
192static struct mips_set_options mips_opts =
193{
316f5878 194 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
e7af610e 195};
252b5132
RH
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. */
200unsigned long mips_gprmask;
201unsigned long mips_cprmask[4];
202
203/* MIPS ISA we are using for this output file. */
e7af610e 204static int file_mips_isa = ISA_UNKNOWN;
252b5132 205
a4672219
TS
206/* True if -mips16 was passed or implied by arguments passed on the
207 command line (e.g., by -march). */
208static int file_ase_mips16;
209
1f25f5d3
CD
210/* True if -mips3d was passed or implied by arguments passed on the
211 command line (e.g., by -march). */
212static int file_ase_mips3d;
213
deec1734
CD
214/* True if -mdmx was passed or implied by arguments passed on the
215 command line (e.g., by -march). */
216static int file_ase_mdmx;
217
ec68c924
EC
218/* The argument of the -march= flag. The architecture we are assembling. */
219static int mips_arch = CPU_UNKNOWN;
316f5878
RS
220static const char *mips_arch_string;
221static const struct mips_cpu_info *mips_arch_info;
ec68c924
EC
222
223/* The argument of the -mtune= flag. The architecture for which we
224 are optimizing. */
225static int mips_tune = CPU_UNKNOWN;
316f5878
RS
226static const char *mips_tune_string;
227static const struct mips_cpu_info *mips_tune_info;
ec68c924 228
316f5878 229/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
230static int mips_32bitmode = 0;
231
9ce8a5dd
GRK
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).
bdaaa2e1 234 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
9ce8a5dd
GRK
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
bdaaa2e1 239 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
9ce8a5dd 240#define ISA_HAS_COPROC_DELAYS(ISA) ( \
e7af610e
NC
241 (ISA) == ISA_MIPS1 \
242 || (ISA) == ISA_MIPS2 \
243 || (ISA) == ISA_MIPS3 \
9ce8a5dd
GRK
244 )
245
316f5878
RS
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
bdaaa2e1 255/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 256#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
257 (ISA) == ISA_MIPS3 \
258 || (ISA) == ISA_MIPS4 \
84ea6cf2 259 || (ISA) == ISA_MIPS5 \
d1cf510e 260 || (ISA) == ISA_MIPS64 \
9ce8a5dd
GRK
261 )
262
af7ee8bf
CD
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
e013f690 275#define HAVE_32BIT_GPRS \
316f5878 276 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 277
e013f690 278#define HAVE_32BIT_FPRS \
316f5878 279 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257
RS
280
281#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
282#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
283
316f5878 284#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 285
316f5878 286#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690
TS
287
288/* We can only have 64bit addresses if the object file format
289 supports it. */
afdbd6d0
CD
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))
e013f690
TS
295
296#define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
ca4e0257 297
a4672219 298/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
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)
a4672219 302
1f25f5d3
CD
303/* Return true if the given CPU supports the MIPS3D ASE. */
304#define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
305 )
306
deec1734 307/* Return true if the given CPU supports the MDMX ASE. */
b34976b6 308#define CPU_HAS_MDMX(cpu) (FALSE \
deec1734
CD
309 )
310
60b63b72
RS
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
bdaaa2e1 317/* Whether the processor uses hardware interlocks to protect
252b5132 318 reads from the HI and LO registers, and thus does not
ec68c924 319 require nops to be inserted. */
252b5132 320
ec68c924 321#define hilo_interlocks (mips_arch == CPU_R4010 \
60b63b72 322 || mips_arch == CPU_VR5500 \
0a758a12 323 || mips_arch == CPU_SB1 \
252b5132
RH
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 \
e7af610e 329 (mips_opts.isa != ISA_MIPS1 \
60b63b72
RS
330 || mips_arch == CPU_VR5400 \
331 || mips_arch == CPU_VR5500 \
ec68c924 332 || mips_arch == CPU_R3900)
252b5132
RH
333
334/* As with other "interlocks" this is used by hardware that has FP
335 (co-processor) interlocks. */
bdaaa2e1 336/* Itbl support may require additional care here. */
ec68c924 337#define cop_interlocks (mips_arch == CPU_R4300 \
60b63b72
RS
338 || mips_arch == CPU_VR5400 \
339 || mips_arch == CPU_VR5500 \
0a758a12 340 || mips_arch == CPU_SB1 \
252b5132
RH
341 )
342
6b76fefe
CM
343/* Is this a mfhi or mflo instruction? */
344#define MF_HILO_INSN(PINFO) \
345 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
346
252b5132
RH
347/* MIPS PIC level. */
348
a161fe53 349enum mips_pic_level mips_pic;
252b5132 350
39c0a331
L
351/* Warn about all NOPS that the assembler generates. */
352static int warn_nops = 0;
353
c9914766 354/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 355 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 356static int mips_big_got = 0;
252b5132
RH
357
358/* 1 if trap instructions should used for overflow rather than break
359 instructions. */
c9914766 360static int mips_trap = 0;
252b5132 361
119d663a 362/* 1 if double width floating point constants should not be constructed
b6ff326e 363 by assembling two single width halves into two single width floating
119d663a
NC
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
d547a75e 366 in the status register, and the setting of this bit cannot be determined
119d663a
NC
367 automatically at assemble time. */
368static int mips_disable_float_construction;
369
252b5132
RH
370/* Non-zero if any .set noreorder directives were used. */
371
372static int mips_any_noreorder;
373
6b76fefe
CM
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. */
376static int mips_7000_hilo_fix;
377
252b5132 378/* The size of the small data section. */
156c2f8b 379static unsigned int g_switch_value = 8;
252b5132
RH
380/* Whether the -G option was used. */
381static 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
fba2b7f9
GK
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.
252b5132
RH
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 */
398static int nopic_need_relax PARAMS ((symbolS *, int));
399
400/* handle of the OPCODE hash table */
401static struct hash_control *op_hash = NULL;
402
403/* The opcode hash table we use for the mips16. */
404static 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 */
408const 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
bdaaa2e1 415 #NO_APP at the beginning of its output. */
252b5132
RH
416/* Also note that C style comments are always supported. */
417const char line_comment_chars[] = "#";
418
bdaaa2e1 419/* This array holds machine specific line separator characters. */
63a0b638 420const char line_separator_chars[] = ";";
252b5132
RH
421
422/* Chars that can be used to separate mant from exp in floating point nums */
423const 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 */
428const 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
435static char *insn_error;
436
437static 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. */
443static offsetT mips_cprestore_offset = -1;
444
6478892d
TS
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
956cd1d6 447 offset and even an other register than $gp as global pointer. */
6478892d
TS
448static offsetT mips_cpreturn_offset = -1;
449static int mips_cpreturn_register = -1;
450static int mips_gp_register = GP;
def2e0dd 451static int mips_gprel_offset = 0;
6478892d 452
7a621144
DJ
453/* Whether mips_cprestore_offset has been set in the current function
454 (or whether it has already been warned about, if not). */
455static int mips_cprestore_valid = 0;
456
252b5132
RH
457/* This is the register which holds the stack frame, as set by the
458 .frame pseudo-op. This is needed to implement .cprestore. */
459static int mips_frame_reg = SP;
460
7a621144
DJ
461/* Whether mips_frame_reg has been set in the current function
462 (or whether it has already been warned about, if not). */
463static int mips_frame_reg_valid = 0;
464
252b5132
RH
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. */
472static 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. */
476static int mips_debug = 0;
477
478/* The previous instruction. */
479static struct mips_cl_insn prev_insn;
480
481/* The instruction before prev_insn. */
482static 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. */
43841e91 486static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
487
488/* Non-zero if prev_insn is valid. */
489static int prev_insn_valid;
490
491/* The frag for the previous instruction. */
492static struct frag *prev_insn_frag;
493
494/* The offset into prev_insn_frag for the previous instruction. */
495static long prev_insn_where;
496
497/* The reloc type for the previous instruction, if any. */
f6688943 498static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
499
500/* The reloc for the previous instruction, if any. */
f6688943 501static fixS *prev_insn_fixp[3];
252b5132
RH
502
503/* Non-zero if the previous instruction was in a delay slot. */
504static int prev_insn_is_delay_slot;
505
506/* Non-zero if the previous instruction was in a .set noreorder. */
507static int prev_insn_unreordered;
508
509/* Non-zero if the previous instruction uses an extend opcode (if
510 mips16). */
511static int prev_insn_extended;
512
513/* Non-zero if the previous previous instruction was in a .set
514 noreorder. */
515static 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. */
521static fragS *prev_nop_frag;
522
523/* The number of nop instructions we created in prev_nop_frag. */
524static int prev_nop_frag_holds;
525
526/* The number of nop instructions that we know we need in
bdaaa2e1 527 prev_nop_frag. */
252b5132
RH
528static int prev_nop_frag_required;
529
530/* The number of instructions we've seen since prev_nop_frag. */
531static 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
bdaaa2e1 542 corresponding LO relocation. */
252b5132 543
e972090a
NC
544struct mips_hi_fixup
545{
252b5132
RH
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
556static struct mips_hi_fixup *mips_hi_fixup_list;
557
558/* Map normal MIPS register numbers to mips16 register numbers. */
559
560#define X ILLEGAL_REG
e972090a
NC
561static const int mips32_to_16_reg_map[] =
562{
252b5132
RH
563 X, X, 2, 3, 4, 5, 6, 7,
564 X, X, X, X, X, X, X, X,
565 0, 1, X, X, X, X, X, X,
566 X, X, X, X, X, X, X, X
567};
568#undef X
569
570/* Map mips16 register numbers to normal MIPS register numbers. */
571
e972090a
NC
572static const unsigned int mips16_to_32_reg_map[] =
573{
252b5132
RH
574 16, 17, 2, 3, 4, 5, 6, 7
575};
60b63b72
RS
576
577static int mips_fix_4122_bugs;
4a6a3df4
AO
578
579/* We don't relax branches by default, since this causes us to expand
580 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
581 fail to compute the offset before expanding the macro to the most
582 efficient expansion. */
583
584static int mips_relax_branch;
252b5132
RH
585\f
586/* Since the MIPS does not have multiple forms of PC relative
587 instructions, we do not have to do relaxing as is done on other
588 platforms. However, we do have to handle GP relative addressing
589 correctly, which turns out to be a similar problem.
590
591 Every macro that refers to a symbol can occur in (at least) two
592 forms, one with GP relative addressing and one without. For
593 example, loading a global variable into a register generally uses
594 a macro instruction like this:
595 lw $4,i
596 If i can be addressed off the GP register (this is true if it is in
597 the .sbss or .sdata section, or if it is known to be smaller than
598 the -G argument) this will generate the following instruction:
599 lw $4,i($gp)
600 This instruction will use a GPREL reloc. If i can not be addressed
601 off the GP register, the following instruction sequence will be used:
602 lui $at,i
603 lw $4,i($at)
604 In this case the first instruction will have a HI16 reloc, and the
605 second reloc will have a LO16 reloc. Both relocs will be against
606 the symbol i.
607
608 The issue here is that we may not know whether i is GP addressable
609 until after we see the instruction that uses it. Therefore, we
610 want to be able to choose the final instruction sequence only at
611 the end of the assembly. This is similar to the way other
612 platforms choose the size of a PC relative instruction only at the
613 end of assembly.
614
615 When generating position independent code we do not use GP
616 addressing in quite the same way, but the issue still arises as
617 external symbols and local symbols must be handled differently.
618
619 We handle these issues by actually generating both possible
620 instruction sequences. The longer one is put in a frag_var with
621 type rs_machine_dependent. We encode what to do with the frag in
622 the subtype field. We encode (1) the number of existing bytes to
623 replace, (2) the number of new bytes to use, (3) the offset from
624 the start of the existing bytes to the first reloc we must generate
625 (that is, the offset is applied from the start of the existing
626 bytes after they are replaced by the new bytes, if any), (4) the
627 offset from the start of the existing bytes to the second reloc,
628 (5) whether a third reloc is needed (the third reloc is always four
629 bytes after the second reloc), and (6) whether to warn if this
630 variant is used (this is sometimes needed if .set nomacro or .set
631 noat is in effect). All these numbers are reasonably small.
632
633 Generating two instruction sequences must be handled carefully to
634 ensure that delay slots are handled correctly. Fortunately, there
635 are a limited number of cases. When the second instruction
636 sequence is generated, append_insn is directed to maintain the
637 existing delay slot information, so it continues to apply to any
638 code after the second instruction sequence. This means that the
639 second instruction sequence must not impose any requirements not
640 required by the first instruction sequence.
641
642 These variant frags are then handled in functions called by the
643 machine independent code. md_estimate_size_before_relax returns
644 the final size of the frag. md_convert_frag sets up the final form
645 of the frag. tc_gen_reloc adjust the first reloc and adds a second
646 one if needed. */
647#define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
648 ((relax_substateT) \
649 (((old) << 23) \
650 | ((new) << 16) \
651 | (((reloc1) + 64) << 9) \
652 | (((reloc2) + 64) << 2) \
653 | ((reloc3) ? (1 << 1) : 0) \
654 | ((warn) ? 1 : 0)))
655#define RELAX_OLD(i) (((i) >> 23) & 0x7f)
656#define RELAX_NEW(i) (((i) >> 16) & 0x7f)
9a41af64
TS
657#define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
658#define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
252b5132
RH
659#define RELAX_RELOC3(i) (((i) >> 1) & 1)
660#define RELAX_WARN(i) ((i) & 1)
661
4a6a3df4
AO
662/* Branch without likely bit. If label is out of range, we turn:
663
664 beq reg1, reg2, label
665 delay slot
666
667 into
668
669 bne reg1, reg2, 0f
670 nop
671 j label
672 0: delay slot
673
674 with the following opcode replacements:
675
676 beq <-> bne
677 blez <-> bgtz
678 bltz <-> bgez
679 bc1f <-> bc1t
680
681 bltzal <-> bgezal (with jal label instead of j label)
682
683 Even though keeping the delay slot instruction in the delay slot of
684 the branch would be more efficient, it would be very tricky to do
685 correctly, because we'd have to introduce a variable frag *after*
686 the delay slot instruction, and expand that instead. Let's do it
687 the easy way for now, even if the branch-not-taken case now costs
688 one additional instruction. Out-of-range branches are not supposed
689 to be common, anyway.
690
691 Branch likely. If label is out of range, we turn:
692
693 beql reg1, reg2, label
694 delay slot (annulled if branch not taken)
695
696 into
697
698 beql reg1, reg2, 1f
699 nop
700 beql $0, $0, 2f
701 nop
702 1: j[al] label
703 delay slot (executed only if branch taken)
704 2:
705
706 It would be possible to generate a shorter sequence by losing the
707 likely bit, generating something like:
b34976b6 708
4a6a3df4
AO
709 bne reg1, reg2, 0f
710 nop
711 j[al] label
712 delay slot (executed only if branch taken)
713 0:
714
715 beql -> bne
716 bnel -> beq
717 blezl -> bgtz
718 bgtzl -> blez
719 bltzl -> bgez
720 bgezl -> bltz
721 bc1fl -> bc1t
722 bc1tl -> bc1f
723
724 bltzall -> bgezal (with jal label instead of j label)
725 bgezall -> bltzal (ditto)
726
727
728 but it's not clear that it would actually improve performance. */
af6ae2ad 729#define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
4a6a3df4
AO
730 ((relax_substateT) \
731 (0xc0000000 \
732 | ((toofar) ? 1 : 0) \
733 | ((link) ? 2 : 0) \
734 | ((likely) ? 4 : 0) \
af6ae2ad 735 | ((uncond) ? 8 : 0)))
4a6a3df4 736#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
4a6a3df4
AO
737#define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
738#define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
739#define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
ae6063d4 740#define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
4a6a3df4 741
252b5132
RH
742/* For mips16 code, we use an entirely different form of relaxation.
743 mips16 supports two versions of most instructions which take
744 immediate values: a small one which takes some small value, and a
745 larger one which takes a 16 bit value. Since branches also follow
746 this pattern, relaxing these values is required.
747
748 We can assemble both mips16 and normal MIPS code in a single
749 object. Therefore, we need to support this type of relaxation at
750 the same time that we support the relaxation described above. We
751 use the high bit of the subtype field to distinguish these cases.
752
753 The information we store for this type of relaxation is the
754 argument code found in the opcode file for this relocation, whether
755 the user explicitly requested a small or extended form, and whether
756 the relocation is in a jump or jal delay slot. That tells us the
757 size of the value, and how it should be stored. We also store
758 whether the fragment is considered to be extended or not. We also
759 store whether this is known to be a branch to a different section,
760 whether we have tried to relax this frag yet, and whether we have
761 ever extended a PC relative fragment because of a shift count. */
762#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
763 (0x80000000 \
764 | ((type) & 0xff) \
765 | ((small) ? 0x100 : 0) \
766 | ((ext) ? 0x200 : 0) \
767 | ((dslot) ? 0x400 : 0) \
768 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 769#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
770#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
771#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
772#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
773#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
774#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
775#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
776#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
777#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
778#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
779#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
780#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95
CD
781
782/* Is the given value a sign-extended 32-bit value? */
783#define IS_SEXT_32BIT_NUM(x) \
784 (((x) &~ (offsetT) 0x7fffffff) == 0 \
785 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
786
787/* Is the given value a sign-extended 16-bit value? */
788#define IS_SEXT_16BIT_NUM(x) \
789 (((x) &~ (offsetT) 0x7fff) == 0 \
790 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
791
252b5132
RH
792\f
793/* Prototypes for static functions. */
794
795#ifdef __STDC__
796#define internalError() \
797 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
798#else
799#define internalError() as_fatal (_("MIPS internal Error"));
800#endif
801
802enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
803
b34976b6
AM
804static int insn_uses_reg
805 PARAMS ((struct mips_cl_insn *ip, unsigned int reg,
806 enum mips_regclass class));
807static int reg_needs_delay
808 PARAMS ((unsigned int));
809static void mips16_mark_labels
810 PARAMS ((void));
811static void append_insn
812 PARAMS ((char *place, struct mips_cl_insn * ip, expressionS * p,
813 bfd_reloc_code_real_type *r, bfd_boolean));
814static void mips_no_prev_insn
815 PARAMS ((int));
816static void mips_emit_delays
817 PARAMS ((bfd_boolean));
252b5132 818#ifdef USE_STDARG
b34976b6
AM
819static void macro_build
820 PARAMS ((char *place, int *counter, expressionS * ep, const char *name,
821 const char *fmt, ...));
252b5132
RH
822#else
823static void macro_build ();
824#endif
b34976b6
AM
825static void mips16_macro_build
826 PARAMS ((char *, int *, expressionS *, const char *, const char *, va_list));
827static void macro_build_jalr
828 PARAMS ((int, expressionS *));
829static void macro_build_lui
830 PARAMS ((char *place, int *counter, expressionS * ep, int regnum));
831static void macro_build_ldst_constoffset
832 PARAMS ((char *place, int *counter, expressionS * ep, const char *op,
833 int valreg, int breg));
834static void set_at
835 PARAMS ((int *counter, int reg, int unsignedp));
836static void check_absolute_expr
837 PARAMS ((struct mips_cl_insn * ip, expressionS *));
838static void load_register
839 PARAMS ((int *, int, expressionS *, int));
840static void load_address
841 PARAMS ((int *, int, expressionS *, int *));
842static void move_register
843 PARAMS ((int *, int, int));
844static void macro
845 PARAMS ((struct mips_cl_insn * ip));
846static void mips16_macro
847 PARAMS ((struct mips_cl_insn * ip));
252b5132 848#ifdef LOSING_COMPILER
b34976b6
AM
849static void macro2
850 PARAMS ((struct mips_cl_insn * ip));
252b5132 851#endif
b34976b6
AM
852static void mips_ip
853 PARAMS ((char *str, struct mips_cl_insn * ip));
854static void mips16_ip
855 PARAMS ((char *str, struct mips_cl_insn * ip));
856static void mips16_immed
857 PARAMS ((char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean,
858 bfd_boolean, unsigned long *, bfd_boolean *, unsigned short *));
859static int my_getPercentOp
860 PARAMS ((char **, unsigned int *, int *));
861static int my_getSmallParser
862 PARAMS ((char **, unsigned int *, int *));
863static int my_getSmallExpression
864 PARAMS ((expressionS *, char *));
865static void my_getExpression
866 PARAMS ((expressionS *, char *));
ae948b86 867#ifdef OBJ_ELF
b34976b6
AM
868static int support_64bit_objects
869 PARAMS((void));
ae948b86 870#endif
b34976b6
AM
871static void mips_set_option_string
872 PARAMS ((const char **, const char *));
873static symbolS *get_symbol
874 PARAMS ((void));
875static void mips_align
876 PARAMS ((int to, int fill, symbolS *label));
877static void s_align
878 PARAMS ((int));
879static void s_change_sec
880 PARAMS ((int));
881static void s_change_section
882 PARAMS ((int));
883static void s_cons
884 PARAMS ((int));
885static void s_float_cons
886 PARAMS ((int));
887static void s_mips_globl
888 PARAMS ((int));
889static void s_option
890 PARAMS ((int));
891static void s_mipsset
892 PARAMS ((int));
893static void s_abicalls
894 PARAMS ((int));
895static void s_cpload
896 PARAMS ((int));
897static void s_cpsetup
898 PARAMS ((int));
899static void s_cplocal
900 PARAMS ((int));
901static void s_cprestore
902 PARAMS ((int));
903static void s_cpreturn
904 PARAMS ((int));
905static void s_gpvalue
906 PARAMS ((int));
907static void s_gpword
908 PARAMS ((int));
909static void s_gpdword
910 PARAMS ((int));
911static void s_cpadd
912 PARAMS ((int));
913static void s_insn
914 PARAMS ((int));
915static void md_obj_begin
916 PARAMS ((void));
917static void md_obj_end
918 PARAMS ((void));
919static long get_number
920 PARAMS ((void));
921static void s_mips_ent
922 PARAMS ((int));
923static void s_mips_end
924 PARAMS ((int));
925static void s_mips_frame
926 PARAMS ((int));
927static void s_mips_mask
928 PARAMS ((int));
929static void s_mips_stab
930 PARAMS ((int));
931static void s_mips_weakext
932 PARAMS ((int));
933static void s_mips_file
934 PARAMS ((int));
935static void s_mips_loc
936 PARAMS ((int));
937static int mips16_extended_frag
938 PARAMS ((fragS *, asection *, long));
4a6a3df4 939static int relaxed_branch_length (fragS *, asection *, int);
b34976b6
AM
940static int validate_mips_insn
941 PARAMS ((const struct mips_opcode *));
942static void show
943 PARAMS ((FILE *, const char *, int *, int *));
add55e1f 944#ifdef OBJ_ELF
b34976b6
AM
945static int mips_need_elf_addend_fixup
946 PARAMS ((fixS *));
add55e1f 947#endif
e7af610e 948
ad8d3bb3 949/* Return values of my_getSmallExpression(). */
fb1b3232 950
ad8d3bb3 951enum small_ex_type
fb1b3232
TS
952{
953 S_EX_NONE = 0,
ad8d3bb3
TS
954 S_EX_REGISTER,
955
956 /* Direct relocation creation by %percent_op(). */
957 S_EX_HALF,
fb1b3232 958 S_EX_HI,
ad8d3bb3
TS
959 S_EX_LO,
960 S_EX_GP_REL,
961 S_EX_GOT,
962 S_EX_CALL16,
963 S_EX_GOT_DISP,
964 S_EX_GOT_PAGE,
965 S_EX_GOT_OFST,
966 S_EX_GOT_HI,
967 S_EX_GOT_LO,
968 S_EX_NEG,
fb1b3232
TS
969 S_EX_HIGHER,
970 S_EX_HIGHEST,
ad8d3bb3
TS
971 S_EX_CALL_HI,
972 S_EX_CALL_LO
fb1b3232
TS
973};
974
e7af610e
NC
975/* Table and functions used to map between CPU/ISA names, and
976 ISA levels, and CPU numbers. */
977
e972090a
NC
978struct mips_cpu_info
979{
e7af610e
NC
980 const char *name; /* CPU or ISA name. */
981 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
982 int isa; /* ISA level. */
983 int cpu; /* CPU number (default CPU if ISA). */
984};
985
b34976b6
AM
986static void mips_set_architecture
987 PARAMS ((const struct mips_cpu_info *));
988static void mips_set_tune
989 PARAMS ((const struct mips_cpu_info *));
990static bfd_boolean mips_strict_matching_cpu_name_p
991 PARAMS ((const char *, const char *));
992static bfd_boolean mips_matching_cpu_name_p
993 PARAMS ((const char *, const char *));
994static const struct mips_cpu_info *mips_parse_cpu
995 PARAMS ((const char *, const char *));
996static const struct mips_cpu_info *mips_cpu_info_from_isa
997 PARAMS ((int));
252b5132
RH
998\f
999/* Pseudo-op table.
1000
1001 The following pseudo-ops from the Kane and Heinrich MIPS book
1002 should be defined here, but are currently unsupported: .alias,
1003 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1004
1005 The following pseudo-ops from the Kane and Heinrich MIPS book are
1006 specific to the type of debugging information being generated, and
1007 should be defined by the object format: .aent, .begin, .bend,
1008 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1009 .vreg.
1010
1011 The following pseudo-ops from the Kane and Heinrich MIPS book are
1012 not MIPS CPU specific, but are also not specific to the object file
1013 format. This file is probably the best place to define them, but
1014 they are not currently supported: .asm0, .endr, .lab, .repeat,
1015 .struct. */
1016
e972090a
NC
1017static const pseudo_typeS mips_pseudo_table[] =
1018{
beae10d5 1019 /* MIPS specific pseudo-ops. */
252b5132
RH
1020 {"option", s_option, 0},
1021 {"set", s_mipsset, 0},
1022 {"rdata", s_change_sec, 'r'},
1023 {"sdata", s_change_sec, 's'},
1024 {"livereg", s_ignore, 0},
1025 {"abicalls", s_abicalls, 0},
1026 {"cpload", s_cpload, 0},
6478892d
TS
1027 {"cpsetup", s_cpsetup, 0},
1028 {"cplocal", s_cplocal, 0},
252b5132 1029 {"cprestore", s_cprestore, 0},
6478892d
TS
1030 {"cpreturn", s_cpreturn, 0},
1031 {"gpvalue", s_gpvalue, 0},
252b5132 1032 {"gpword", s_gpword, 0},
10181a0d 1033 {"gpdword", s_gpdword, 0},
252b5132
RH
1034 {"cpadd", s_cpadd, 0},
1035 {"insn", s_insn, 0},
1036
beae10d5 1037 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
1038 chips. */
1039 {"asciiz", stringer, 1},
1040 {"bss", s_change_sec, 'b'},
1041 {"err", s_err, 0},
1042 {"half", s_cons, 1},
1043 {"dword", s_cons, 3},
1044 {"weakext", s_mips_weakext, 0},
1045
beae10d5 1046 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1047 here for one reason or another. */
1048 {"align", s_align, 0},
1049 {"byte", s_cons, 0},
1050 {"data", s_change_sec, 'd'},
1051 {"double", s_float_cons, 'd'},
1052 {"float", s_float_cons, 'f'},
1053 {"globl", s_mips_globl, 0},
1054 {"global", s_mips_globl, 0},
1055 {"hword", s_cons, 1},
1056 {"int", s_cons, 2},
1057 {"long", s_cons, 2},
1058 {"octa", s_cons, 4},
1059 {"quad", s_cons, 3},
cca86cc8 1060 {"section", s_change_section, 0},
252b5132
RH
1061 {"short", s_cons, 1},
1062 {"single", s_float_cons, 'f'},
1063 {"stabn", s_mips_stab, 'n'},
1064 {"text", s_change_sec, 't'},
1065 {"word", s_cons, 2},
add56521 1066
add56521 1067 { "extern", ecoff_directive_extern, 0},
add56521 1068
43841e91 1069 { NULL, NULL, 0 },
252b5132
RH
1070};
1071
e972090a
NC
1072static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1073{
beae10d5
KH
1074 /* These pseudo-ops should be defined by the object file format.
1075 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1076 {"aent", s_mips_ent, 1},
1077 {"bgnb", s_ignore, 0},
1078 {"end", s_mips_end, 0},
1079 {"endb", s_ignore, 0},
1080 {"ent", s_mips_ent, 0},
c5dd6aab 1081 {"file", s_mips_file, 0},
252b5132
RH
1082 {"fmask", s_mips_mask, 'F'},
1083 {"frame", s_mips_frame, 0},
c5dd6aab 1084 {"loc", s_mips_loc, 0},
252b5132
RH
1085 {"mask", s_mips_mask, 'R'},
1086 {"verstamp", s_ignore, 0},
43841e91 1087 { NULL, NULL, 0 },
252b5132
RH
1088};
1089
1090extern void pop_insert PARAMS ((const pseudo_typeS *));
1091
1092void
1093mips_pop_insert ()
1094{
1095 pop_insert (mips_pseudo_table);
1096 if (! ECOFF_DEBUGGING)
1097 pop_insert (mips_nonecoff_pseudo_table);
1098}
1099\f
1100/* Symbols labelling the current insn. */
1101
e972090a
NC
1102struct insn_label_list
1103{
252b5132
RH
1104 struct insn_label_list *next;
1105 symbolS *label;
1106};
1107
1108static struct insn_label_list *insn_labels;
1109static struct insn_label_list *free_insn_labels;
1110
1111static void mips_clear_insn_labels PARAMS ((void));
1112
1113static inline void
1114mips_clear_insn_labels ()
1115{
1116 register struct insn_label_list **pl;
1117
1118 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1119 ;
1120 *pl = insn_labels;
1121 insn_labels = NULL;
1122}
1123\f
1124static char *expr_end;
1125
1126/* Expressions which appear in instructions. These are set by
1127 mips_ip. */
1128
1129static expressionS imm_expr;
1130static expressionS offset_expr;
1131
1132/* Relocs associated with imm_expr and offset_expr. */
1133
f6688943
TS
1134static bfd_reloc_code_real_type imm_reloc[3]
1135 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1136static bfd_reloc_code_real_type offset_reloc[3]
1137 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1138
1139/* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
1140
b34976b6 1141static bfd_boolean imm_unmatched_hi;
252b5132
RH
1142
1143/* These are set by mips16_ip if an explicit extension is used. */
1144
b34976b6 1145static bfd_boolean mips16_small, mips16_ext;
252b5132 1146
7ed4a06a 1147#ifdef OBJ_ELF
ecb4347a
DJ
1148/* The pdr segment for per procedure frame/regmask info. Not used for
1149 ECOFF debugging. */
252b5132
RH
1150
1151static segT pdr_seg;
7ed4a06a 1152#endif
252b5132 1153
e013f690
TS
1154/* The default target format to use. */
1155
1156const char *
1157mips_target_format ()
1158{
1159 switch (OUTPUT_FLAVOR)
1160 {
1161 case bfd_target_aout_flavour:
1162 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1163 case bfd_target_ecoff_flavour:
1164 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1165 case bfd_target_coff_flavour:
1166 return "pe-mips";
1167 case bfd_target_elf_flavour:
1168#ifdef TE_TMIPS
cfe86eaa 1169 /* This is traditional mips. */
e013f690 1170 return (target_big_endian
cfe86eaa
TS
1171 ? (HAVE_64BIT_OBJECTS
1172 ? "elf64-tradbigmips"
1173 : (HAVE_NEWABI
1174 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1175 : (HAVE_64BIT_OBJECTS
1176 ? "elf64-tradlittlemips"
1177 : (HAVE_NEWABI
1178 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1179#else
1180 return (target_big_endian
cfe86eaa
TS
1181 ? (HAVE_64BIT_OBJECTS
1182 ? "elf64-bigmips"
1183 : (HAVE_NEWABI
1184 ? "elf32-nbigmips" : "elf32-bigmips"))
1185 : (HAVE_64BIT_OBJECTS
1186 ? "elf64-littlemips"
1187 : (HAVE_NEWABI
1188 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1189#endif
1190 default:
1191 abort ();
1192 return NULL;
1193 }
1194}
1195
156c2f8b
NC
1196/* This function is called once, at assembler startup time. It should
1197 set up all the tables, etc. that the MD part of the assembler will need. */
1198
252b5132
RH
1199void
1200md_begin ()
1201{
252b5132 1202 register const char *retval = NULL;
156c2f8b 1203 int i = 0;
252b5132 1204 int broken = 0;
1f25f5d3 1205
ec68c924 1206 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
252b5132
RH
1207 as_warn (_("Could not set architecture and machine"));
1208
252b5132
RH
1209 op_hash = hash_new ();
1210
1211 for (i = 0; i < NUMOPCODES;)
1212 {
1213 const char *name = mips_opcodes[i].name;
1214
1215 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1216 if (retval != NULL)
1217 {
1218 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1219 mips_opcodes[i].name, retval);
1220 /* Probably a memory allocation problem? Give up now. */
1221 as_fatal (_("Broken assembler. No assembly attempted."));
1222 }
1223 do
1224 {
1225 if (mips_opcodes[i].pinfo != INSN_MACRO)
1226 {
1227 if (!validate_mips_insn (&mips_opcodes[i]))
1228 broken = 1;
1229 }
1230 ++i;
1231 }
1232 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1233 }
1234
1235 mips16_op_hash = hash_new ();
1236
1237 i = 0;
1238 while (i < bfd_mips16_num_opcodes)
1239 {
1240 const char *name = mips16_opcodes[i].name;
1241
1242 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1243 if (retval != NULL)
1244 as_fatal (_("internal: can't hash `%s': %s"),
1245 mips16_opcodes[i].name, retval);
1246 do
1247 {
1248 if (mips16_opcodes[i].pinfo != INSN_MACRO
1249 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1250 != mips16_opcodes[i].match))
1251 {
1252 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1253 mips16_opcodes[i].name, mips16_opcodes[i].args);
1254 broken = 1;
1255 }
1256 ++i;
1257 }
1258 while (i < bfd_mips16_num_opcodes
1259 && strcmp (mips16_opcodes[i].name, name) == 0);
1260 }
1261
1262 if (broken)
1263 as_fatal (_("Broken assembler. No assembly attempted."));
1264
1265 /* We add all the general register names to the symbol table. This
1266 helps us detect invalid uses of them. */
1267 for (i = 0; i < 32; i++)
1268 {
1269 char buf[5];
1270
1271 sprintf (buf, "$%d", i);
1272 symbol_table_insert (symbol_new (buf, reg_section, i,
1273 &zero_address_frag));
1274 }
76db943d
TS
1275 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1276 &zero_address_frag));
252b5132
RH
1277 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1278 &zero_address_frag));
1279 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1280 &zero_address_frag));
1281 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1282 &zero_address_frag));
1283 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1284 &zero_address_frag));
1285 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1286 &zero_address_frag));
1287 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1288 &zero_address_frag));
85b51719
TS
1289 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1290 &zero_address_frag));
252b5132
RH
1291 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1292 &zero_address_frag));
1293
6047c971
AO
1294 /* If we don't add these register names to the symbol table, they
1295 may end up being added as regular symbols by operand(), and then
1296 make it to the object file as undefined in case they're not
1297 regarded as local symbols. They're local in o32, since `$' is a
1298 local symbol prefix, but not in n32 or n64. */
1299 for (i = 0; i < 8; i++)
1300 {
1301 char buf[6];
1302
1303 sprintf (buf, "$fcc%i", i);
1304 symbol_table_insert (symbol_new (buf, reg_section, -1,
1305 &zero_address_frag));
1306 }
1307
b34976b6 1308 mips_no_prev_insn (FALSE);
252b5132
RH
1309
1310 mips_gprmask = 0;
1311 mips_cprmask[0] = 0;
1312 mips_cprmask[1] = 0;
1313 mips_cprmask[2] = 0;
1314 mips_cprmask[3] = 0;
1315
1316 /* set the default alignment for the text section (2**2) */
1317 record_alignment (text_section, 2);
1318
1319 if (USE_GLOBAL_POINTER_OPT)
1320 bfd_set_gp_size (stdoutput, g_switch_value);
1321
1322 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1323 {
1324 /* On a native system, sections must be aligned to 16 byte
1325 boundaries. When configured for an embedded ELF target, we
1326 don't bother. */
1327 if (strcmp (TARGET_OS, "elf") != 0)
1328 {
1329 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1330 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1331 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1332 }
1333
1334 /* Create a .reginfo section for register masks and a .mdebug
1335 section for debugging information. */
1336 {
1337 segT seg;
1338 subsegT subseg;
1339 flagword flags;
1340 segT sec;
1341
1342 seg = now_seg;
1343 subseg = now_subseg;
1344
1345 /* The ABI says this section should be loaded so that the
1346 running program can access it. However, we don't load it
1347 if we are configured for an embedded target */
1348 flags = SEC_READONLY | SEC_DATA;
1349 if (strcmp (TARGET_OS, "elf") != 0)
1350 flags |= SEC_ALLOC | SEC_LOAD;
1351
316f5878 1352 if (mips_abi != N64_ABI)
252b5132
RH
1353 {
1354 sec = subseg_new (".reginfo", (subsegT) 0);
1355
195325d2
TS
1356 bfd_set_section_flags (stdoutput, sec, flags);
1357 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1358
252b5132
RH
1359#ifdef OBJ_ELF
1360 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1361#endif
1362 }
1363 else
1364 {
1365 /* The 64-bit ABI uses a .MIPS.options section rather than
1366 .reginfo section. */
1367 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1368 bfd_set_section_flags (stdoutput, sec, flags);
1369 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1370
1371#ifdef OBJ_ELF
1372 /* Set up the option header. */
1373 {
1374 Elf_Internal_Options opthdr;
1375 char *f;
1376
1377 opthdr.kind = ODK_REGINFO;
1378 opthdr.size = (sizeof (Elf_External_Options)
1379 + sizeof (Elf64_External_RegInfo));
1380 opthdr.section = 0;
1381 opthdr.info = 0;
1382 f = frag_more (sizeof (Elf_External_Options));
1383 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1384 (Elf_External_Options *) f);
1385
1386 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1387 }
1388#endif
1389 }
1390
1391 if (ECOFF_DEBUGGING)
1392 {
1393 sec = subseg_new (".mdebug", (subsegT) 0);
1394 (void) bfd_set_section_flags (stdoutput, sec,
1395 SEC_HAS_CONTENTS | SEC_READONLY);
1396 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1397 }
ecb4347a
DJ
1398#ifdef OBJ_ELF
1399 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1400 {
1401 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1402 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1403 SEC_READONLY | SEC_RELOC
1404 | SEC_DEBUGGING);
1405 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1406 }
252b5132
RH
1407#endif
1408
1409 subseg_set (seg, subseg);
1410 }
1411 }
1412
1413 if (! ECOFF_DEBUGGING)
1414 md_obj_begin ();
1415}
1416
1417void
1418md_mips_end ()
1419{
1420 if (! ECOFF_DEBUGGING)
1421 md_obj_end ();
1422}
1423
1424void
1425md_assemble (str)
1426 char *str;
1427{
1428 struct mips_cl_insn insn;
f6688943
TS
1429 bfd_reloc_code_real_type unused_reloc[3]
1430 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1431
1432 imm_expr.X_op = O_absent;
b34976b6 1433 imm_unmatched_hi = FALSE;
252b5132 1434 offset_expr.X_op = O_absent;
f6688943
TS
1435 imm_reloc[0] = BFD_RELOC_UNUSED;
1436 imm_reloc[1] = BFD_RELOC_UNUSED;
1437 imm_reloc[2] = BFD_RELOC_UNUSED;
1438 offset_reloc[0] = BFD_RELOC_UNUSED;
1439 offset_reloc[1] = BFD_RELOC_UNUSED;
1440 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1441
1442 if (mips_opts.mips16)
1443 mips16_ip (str, &insn);
1444 else
1445 {
1446 mips_ip (str, &insn);
beae10d5
KH
1447 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1448 str, insn.insn_opcode));
252b5132
RH
1449 }
1450
1451 if (insn_error)
1452 {
1453 as_bad ("%s `%s'", insn_error, str);
1454 return;
1455 }
1456
1457 if (insn.insn_mo->pinfo == INSN_MACRO)
1458 {
1459 if (mips_opts.mips16)
1460 mips16_macro (&insn);
1461 else
1462 macro (&insn);
1463 }
1464 else
1465 {
1466 if (imm_expr.X_op != O_absent)
c4e7957c 1467 append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
252b5132 1468 else if (offset_expr.X_op != O_absent)
b34976b6 1469 append_insn (NULL, &insn, &offset_expr, offset_reloc, FALSE);
252b5132 1470 else
b34976b6 1471 append_insn (NULL, &insn, NULL, unused_reloc, FALSE);
252b5132
RH
1472 }
1473}
1474
1475/* See whether instruction IP reads register REG. CLASS is the type
1476 of register. */
1477
1478static int
1479insn_uses_reg (ip, reg, class)
1480 struct mips_cl_insn *ip;
1481 unsigned int reg;
1482 enum mips_regclass class;
1483{
1484 if (class == MIPS16_REG)
1485 {
1486 assert (mips_opts.mips16);
1487 reg = mips16_to_32_reg_map[reg];
1488 class = MIPS_GR_REG;
1489 }
1490
85b51719
TS
1491 /* Don't report on general register ZERO, since it never changes. */
1492 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
1493 return 0;
1494
1495 if (class == MIPS_FP_REG)
1496 {
1497 assert (! mips_opts.mips16);
1498 /* If we are called with either $f0 or $f1, we must check $f0.
1499 This is not optimal, because it will introduce an unnecessary
1500 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1501 need to distinguish reading both $f0 and $f1 or just one of
1502 them. Note that we don't have to check the other way,
1503 because there is no instruction that sets both $f0 and $f1
1504 and requires a delay. */
1505 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1506 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1507 == (reg &~ (unsigned) 1)))
1508 return 1;
1509 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1510 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1511 == (reg &~ (unsigned) 1)))
1512 return 1;
1513 }
1514 else if (! mips_opts.mips16)
1515 {
1516 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1517 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1518 return 1;
1519 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1520 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1521 return 1;
1522 }
1523 else
1524 {
1525 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1526 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1527 & MIPS16OP_MASK_RX)]
1528 == reg))
1529 return 1;
1530 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1531 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1532 & MIPS16OP_MASK_RY)]
1533 == reg))
1534 return 1;
1535 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1536 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1537 & MIPS16OP_MASK_MOVE32Z)]
1538 == reg))
1539 return 1;
1540 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1541 return 1;
1542 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1543 return 1;
1544 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1545 return 1;
1546 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1547 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1548 & MIPS16OP_MASK_REGR32) == reg)
1549 return 1;
1550 }
1551
1552 return 0;
1553}
1554
1555/* This function returns true if modifying a register requires a
1556 delay. */
1557
1558static int
1559reg_needs_delay (reg)
156c2f8b 1560 unsigned int reg;
252b5132
RH
1561{
1562 unsigned long prev_pinfo;
1563
1564 prev_pinfo = prev_insn.insn_mo->pinfo;
1565 if (! mips_opts.noreorder
9ce8a5dd 1566 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1567 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1568 || (! gpr_interlocks
1569 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1570 {
1571 /* A load from a coprocessor or from memory. All load
1572 delays delay the use of general register rt for one
1573 instruction on the r3000. The r6000 and r4000 use
1574 interlocks. */
bdaaa2e1 1575 /* Itbl support may require additional care here. */
252b5132
RH
1576 know (prev_pinfo & INSN_WRITE_GPR_T);
1577 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1578 return 1;
1579 }
1580
1581 return 0;
1582}
1583
1584/* Mark instruction labels in mips16 mode. This permits the linker to
1585 handle them specially, such as generating jalx instructions when
1586 needed. We also make them odd for the duration of the assembly, in
1587 order to generate the right sort of code. We will make them even
1588 in the adjust_symtab routine, while leaving them marked. This is
1589 convenient for the debugger and the disassembler. The linker knows
1590 to make them odd again. */
1591
1592static void
1593mips16_mark_labels ()
1594{
1595 if (mips_opts.mips16)
1596 {
1597 struct insn_label_list *l;
98aa84af 1598 valueT val;
252b5132
RH
1599
1600 for (l = insn_labels; l != NULL; l = l->next)
1601 {
1602#ifdef OBJ_ELF
1603 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1604 S_SET_OTHER (l->label, STO_MIPS16);
1605#endif
98aa84af
AM
1606 val = S_GET_VALUE (l->label);
1607 if ((val & 1) == 0)
1608 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1609 }
1610 }
1611}
1612
1613/* Output an instruction. PLACE is where to put the instruction; if
1614 it is NULL, this uses frag_more to get room. IP is the instruction
1615 information. ADDRESS_EXPR is an operand of the instruction to be
1616 used with RELOC_TYPE. */
1617
1618static void
1619append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1620 char *place;
1621 struct mips_cl_insn *ip;
1622 expressionS *address_expr;
f6688943 1623 bfd_reloc_code_real_type *reloc_type;
b34976b6 1624 bfd_boolean unmatched_hi;
252b5132
RH
1625{
1626 register unsigned long prev_pinfo, pinfo;
1627 char *f;
f6688943 1628 fixS *fixp[3];
252b5132
RH
1629 int nops = 0;
1630
1631 /* Mark instruction labels in mips16 mode. */
f9419b05 1632 mips16_mark_labels ();
252b5132
RH
1633
1634 prev_pinfo = prev_insn.insn_mo->pinfo;
1635 pinfo = ip->insn_mo->pinfo;
1636
1637 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1638 {
1639 int prev_prev_nop;
1640
1641 /* If the previous insn required any delay slots, see if we need
1642 to insert a NOP or two. There are eight kinds of possible
1643 hazards, of which an instruction can have at most one type.
1644 (1) a load from memory delay
1645 (2) a load from a coprocessor delay
1646 (3) an unconditional branch delay
1647 (4) a conditional branch delay
1648 (5) a move to coprocessor register delay
1649 (6) a load coprocessor register from memory delay
1650 (7) a coprocessor condition code delay
1651 (8) a HI/LO special register delay
1652
1653 There are a lot of optimizations we could do that we don't.
1654 In particular, we do not, in general, reorder instructions.
1655 If you use gcc with optimization, it will reorder
1656 instructions and generally do much more optimization then we
1657 do here; repeating all that work in the assembler would only
1658 benefit hand written assembly code, and does not seem worth
1659 it. */
1660
1661 /* This is how a NOP is emitted. */
1662#define emit_nop() \
1663 (mips_opts.mips16 \
1664 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1665 : md_number_to_chars (frag_more (4), 0, 4))
1666
1667 /* The previous insn might require a delay slot, depending upon
1668 the contents of the current insn. */
1669 if (! mips_opts.mips16
9ce8a5dd 1670 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1671 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1672 && ! cop_interlocks)
1673 || (! gpr_interlocks
1674 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1675 {
1676 /* A load from a coprocessor or from memory. All load
1677 delays delay the use of general register rt for one
1678 instruction on the r3000. The r6000 and r4000 use
1679 interlocks. */
beae10d5 1680 /* Itbl support may require additional care here. */
252b5132
RH
1681 know (prev_pinfo & INSN_WRITE_GPR_T);
1682 if (mips_optimize == 0
1683 || insn_uses_reg (ip,
1684 ((prev_insn.insn_opcode >> OP_SH_RT)
1685 & OP_MASK_RT),
1686 MIPS_GR_REG))
1687 ++nops;
1688 }
1689 else if (! mips_opts.mips16
9ce8a5dd 1690 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 1691 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1692 && ! cop_interlocks)
e7af610e 1693 || (mips_opts.isa == ISA_MIPS1
252b5132
RH
1694 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1695 {
1696 /* A generic coprocessor delay. The previous instruction
1697 modified a coprocessor general or control register. If
1698 it modified a control register, we need to avoid any
1699 coprocessor instruction (this is probably not always
1700 required, but it sometimes is). If it modified a general
1701 register, we avoid using that register.
1702
1703 On the r6000 and r4000 loading a coprocessor register
1704 from memory is interlocked, and does not require a delay.
1705
1706 This case is not handled very well. There is no special
1707 knowledge of CP0 handling, and the coprocessors other
1708 than the floating point unit are not distinguished at
1709 all. */
1710 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1711 Need to modify this to include knowledge about
252b5132
RH
1712 user specified delays! */
1713 if (prev_pinfo & INSN_WRITE_FPR_T)
1714 {
1715 if (mips_optimize == 0
1716 || insn_uses_reg (ip,
1717 ((prev_insn.insn_opcode >> OP_SH_FT)
1718 & OP_MASK_FT),
1719 MIPS_FP_REG))
1720 ++nops;
1721 }
1722 else if (prev_pinfo & INSN_WRITE_FPR_S)
1723 {
1724 if (mips_optimize == 0
1725 || insn_uses_reg (ip,
1726 ((prev_insn.insn_opcode >> OP_SH_FS)
1727 & OP_MASK_FS),
1728 MIPS_FP_REG))
1729 ++nops;
1730 }
1731 else
1732 {
1733 /* We don't know exactly what the previous instruction
1734 does. If the current instruction uses a coprocessor
1735 register, we must insert a NOP. If previous
1736 instruction may set the condition codes, and the
1737 current instruction uses them, we must insert two
1738 NOPS. */
bdaaa2e1 1739 /* Itbl support may require additional care here. */
252b5132
RH
1740 if (mips_optimize == 0
1741 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1742 && (pinfo & INSN_READ_COND_CODE)))
1743 nops += 2;
1744 else if (pinfo & INSN_COP)
1745 ++nops;
1746 }
1747 }
1748 else if (! mips_opts.mips16
9ce8a5dd 1749 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1750 && (prev_pinfo & INSN_WRITE_COND_CODE)
1751 && ! cop_interlocks)
1752 {
1753 /* The previous instruction sets the coprocessor condition
1754 codes, but does not require a general coprocessor delay
1755 (this means it is a floating point comparison
1756 instruction). If this instruction uses the condition
1757 codes, we need to insert a single NOP. */
beae10d5 1758 /* Itbl support may require additional care here. */
252b5132
RH
1759 if (mips_optimize == 0
1760 || (pinfo & INSN_READ_COND_CODE))
1761 ++nops;
1762 }
6b76fefe
CM
1763
1764 /* If we're fixing up mfhi/mflo for the r7000 and the
1765 previous insn was an mfhi/mflo and the current insn
1766 reads the register that the mfhi/mflo wrote to, then
1767 insert two nops. */
1768
1769 else if (mips_7000_hilo_fix
1770 && MF_HILO_INSN (prev_pinfo)
1771 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1772 & OP_MASK_RD),
1773 MIPS_GR_REG))
6b76fefe
CM
1774 {
1775 nops += 2;
1776 }
1777
1778 /* If we're fixing up mfhi/mflo for the r7000 and the
1779 2nd previous insn was an mfhi/mflo and the current insn
1780 reads the register that the mfhi/mflo wrote to, then
1781 insert one nop. */
1782
1783 else if (mips_7000_hilo_fix
1784 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1785 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1786 & OP_MASK_RD),
1787 MIPS_GR_REG))
bdaaa2e1 1788
6b76fefe 1789 {
f9419b05 1790 ++nops;
6b76fefe 1791 }
bdaaa2e1 1792
252b5132
RH
1793 else if (prev_pinfo & INSN_READ_LO)
1794 {
1795 /* The previous instruction reads the LO register; if the
1796 current instruction writes to the LO register, we must
bdaaa2e1
KH
1797 insert two NOPS. Some newer processors have interlocks.
1798 Also the tx39's multiply instructions can be exectuted
252b5132 1799 immediatly after a read from HI/LO (without the delay),
bdaaa2e1
KH
1800 though the tx39's divide insns still do require the
1801 delay. */
252b5132 1802 if (! (hilo_interlocks
ec68c924 1803 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1804 && (mips_optimize == 0
1805 || (pinfo & INSN_WRITE_LO)))
1806 nops += 2;
1807 /* Most mips16 branch insns don't have a delay slot.
1808 If a read from LO is immediately followed by a branch
1809 to a write to LO we have a read followed by a write
1810 less than 2 insns away. We assume the target of
1811 a branch might be a write to LO, and insert a nop
bdaaa2e1 1812 between a read and an immediately following branch. */
252b5132
RH
1813 else if (mips_opts.mips16
1814 && (mips_optimize == 0
1815 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1816 ++nops;
252b5132
RH
1817 }
1818 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1819 {
1820 /* The previous instruction reads the HI register; if the
1821 current instruction writes to the HI register, we must
1822 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1823 Also the note tx39's multiply above. */
252b5132 1824 if (! (hilo_interlocks
ec68c924 1825 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1826 && (mips_optimize == 0
1827 || (pinfo & INSN_WRITE_HI)))
1828 nops += 2;
1829 /* Most mips16 branch insns don't have a delay slot.
1830 If a read from HI is immediately followed by a branch
1831 to a write to HI we have a read followed by a write
1832 less than 2 insns away. We assume the target of
1833 a branch might be a write to HI, and insert a nop
bdaaa2e1 1834 between a read and an immediately following branch. */
252b5132
RH
1835 else if (mips_opts.mips16
1836 && (mips_optimize == 0
1837 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1838 ++nops;
252b5132
RH
1839 }
1840
1841 /* If the previous instruction was in a noreorder section, then
1842 we don't want to insert the nop after all. */
bdaaa2e1 1843 /* Itbl support may require additional care here. */
252b5132
RH
1844 if (prev_insn_unreordered)
1845 nops = 0;
1846
1847 /* There are two cases which require two intervening
1848 instructions: 1) setting the condition codes using a move to
1849 coprocessor instruction which requires a general coprocessor
1850 delay and then reading the condition codes 2) reading the HI
1851 or LO register and then writing to it (except on processors
1852 which have interlocks). If we are not already emitting a NOP
1853 instruction, we must check for these cases compared to the
1854 instruction previous to the previous instruction. */
1855 if ((! mips_opts.mips16
9ce8a5dd 1856 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1857 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1858 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1859 && (pinfo & INSN_READ_COND_CODE)
1860 && ! cop_interlocks)
1861 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1862 && (pinfo & INSN_WRITE_LO)
1863 && ! (hilo_interlocks
ec68c924 1864 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1865 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1866 && (pinfo & INSN_WRITE_HI)
1867 && ! (hilo_interlocks
ec68c924 1868 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1869 prev_prev_nop = 1;
1870 else
1871 prev_prev_nop = 0;
1872
1873 if (prev_prev_insn_unreordered)
1874 prev_prev_nop = 0;
1875
1876 if (prev_prev_nop && nops == 0)
1877 ++nops;
1878
60b63b72
RS
1879 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1880 {
1881 /* We're out of bits in pinfo, so we must resort to string
1882 ops here. Shortcuts are selected based on opcodes being
1883 limited to the VR4122 instruction set. */
1884 int min_nops = 0;
1885 const char *pn = prev_insn.insn_mo->name;
1886 const char *tn = ip->insn_mo->name;
1887 if (strncmp(pn, "macc", 4) == 0
1888 || strncmp(pn, "dmacc", 5) == 0)
1889 {
1890 /* Errata 21 - [D]DIV[U] after [D]MACC */
1891 if (strstr (tn, "div"))
1892 {
1893 min_nops = 1;
1894 }
1895
1896 /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1897 if (pn[0] == 'd' /* dmacc */
1898 && (strncmp(tn, "dmult", 5) == 0
1899 || strncmp(tn, "dmacc", 5) == 0))
1900 {
1901 min_nops = 1;
1902 }
1903
1904 /* Errata 24 - MT{LO,HI} after [D]MACC */
1905 if (strcmp (tn, "mtlo") == 0
1906 || strcmp (tn, "mthi") == 0)
1907 {
1908 min_nops = 1;
1909 }
1910
1911 }
1912 else if (strncmp(pn, "dmult", 5) == 0
1913 && (strncmp(tn, "dmult", 5) == 0
1914 || strncmp(tn, "dmacc", 5) == 0))
1915 {
1916 /* Here is the rest of errata 23. */
1917 min_nops = 1;
1918 }
1919 if (nops < min_nops)
1920 nops = min_nops;
1921 }
1922
252b5132
RH
1923 /* If we are being given a nop instruction, don't bother with
1924 one of the nops we would otherwise output. This will only
1925 happen when a nop instruction is used with mips_optimize set
1926 to 0. */
1927 if (nops > 0
1928 && ! mips_opts.noreorder
156c2f8b 1929 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1930 --nops;
1931
1932 /* Now emit the right number of NOP instructions. */
1933 if (nops > 0 && ! mips_opts.noreorder)
1934 {
1935 fragS *old_frag;
1936 unsigned long old_frag_offset;
1937 int i;
1938 struct insn_label_list *l;
1939
1940 old_frag = frag_now;
1941 old_frag_offset = frag_now_fix ();
1942
1943 for (i = 0; i < nops; i++)
1944 emit_nop ();
1945
1946 if (listing)
1947 {
1948 listing_prev_line ();
1949 /* We may be at the start of a variant frag. In case we
1950 are, make sure there is enough space for the frag
1951 after the frags created by listing_prev_line. The
1952 argument to frag_grow here must be at least as large
1953 as the argument to all other calls to frag_grow in
1954 this file. We don't have to worry about being in the
1955 middle of a variant frag, because the variants insert
1956 all needed nop instructions themselves. */
1957 frag_grow (40);
1958 }
1959
1960 for (l = insn_labels; l != NULL; l = l->next)
1961 {
98aa84af
AM
1962 valueT val;
1963
252b5132 1964 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1965 symbol_set_frag (l->label, frag_now);
98aa84af 1966 val = (valueT) frag_now_fix ();
252b5132
RH
1967 /* mips16 text labels are stored as odd. */
1968 if (mips_opts.mips16)
f9419b05 1969 ++val;
98aa84af 1970 S_SET_VALUE (l->label, val);
252b5132
RH
1971 }
1972
1973#ifndef NO_ECOFF_DEBUGGING
1974 if (ECOFF_DEBUGGING)
1975 ecoff_fix_loc (old_frag, old_frag_offset);
1976#endif
1977 }
1978 else if (prev_nop_frag != NULL)
1979 {
1980 /* We have a frag holding nops we may be able to remove. If
1981 we don't need any nops, we can decrease the size of
1982 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1983 need some nops, we count them in prev_nops_required. */
252b5132
RH
1984 if (prev_nop_frag_since == 0)
1985 {
1986 if (nops == 0)
1987 {
1988 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1989 --prev_nop_frag_holds;
1990 }
1991 else
1992 prev_nop_frag_required += nops;
1993 }
1994 else
1995 {
1996 if (prev_prev_nop == 0)
1997 {
1998 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1999 --prev_nop_frag_holds;
2000 }
2001 else
2002 ++prev_nop_frag_required;
2003 }
2004
2005 if (prev_nop_frag_holds <= prev_nop_frag_required)
2006 prev_nop_frag = NULL;
2007
2008 ++prev_nop_frag_since;
2009
2010 /* Sanity check: by the time we reach the second instruction
2011 after prev_nop_frag, we should have used up all the nops
2012 one way or another. */
2013 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2014 }
2015 }
2016
4a6a3df4
AO
2017 if (place == NULL
2018 && address_expr
af6ae2ad 2019 && *reloc_type == BFD_RELOC_16_PCREL_S2
4a6a3df4
AO
2020 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2021 || pinfo & INSN_COND_BRANCH_LIKELY)
2022 && mips_relax_branch
2023 /* Don't try branch relaxation within .set nomacro, or within
2024 .set noat if we use $at for PIC computations. If it turns
2025 out that the branch was out-of-range, we'll get an error. */
2026 && !mips_opts.warn_about_macros
2027 && !(mips_opts.noat && mips_pic != NO_PIC)
2028 && !mips_opts.mips16)
2029 {
2030 f = frag_var (rs_machine_dependent,
2031 relaxed_branch_length
2032 (NULL, NULL,
2033 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2034 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2035 RELAX_BRANCH_ENCODE
af6ae2ad 2036 (pinfo & INSN_UNCOND_BRANCH_DELAY,
4a6a3df4
AO
2037 pinfo & INSN_COND_BRANCH_LIKELY,
2038 pinfo & INSN_WRITE_GPR_31,
2039 0),
2040 address_expr->X_add_symbol,
2041 address_expr->X_add_number,
2042 0);
2043 *reloc_type = BFD_RELOC_UNUSED;
2044 }
2045 else if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
2046 {
2047 /* We need to set up a variant frag. */
2048 assert (mips_opts.mips16 && address_expr != NULL);
2049 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 2050 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
2051 mips16_small, mips16_ext,
2052 (prev_pinfo
2053 & INSN_UNCOND_BRANCH_DELAY),
f6688943 2054 (*prev_insn_reloc_type
252b5132 2055 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 2056 make_expr_symbol (address_expr), 0, NULL);
252b5132
RH
2057 }
2058 else if (place != NULL)
2059 f = place;
2060 else if (mips_opts.mips16
2061 && ! ip->use_extend
f6688943 2062 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
2063 {
2064 /* Make sure there is enough room to swap this instruction with
2065 a following jump instruction. */
2066 frag_grow (6);
2067 f = frag_more (2);
2068 }
2069 else
2070 {
2071 if (mips_opts.mips16
2072 && mips_opts.noreorder
2073 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2074 as_warn (_("extended instruction in delay slot"));
2075
2076 f = frag_more (4);
2077 }
2078
f6688943
TS
2079 fixp[0] = fixp[1] = fixp[2] = NULL;
2080 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
2081 {
2082 if (address_expr->X_op == O_constant)
2083 {
4db1a35d 2084 valueT tmp;
f6688943
TS
2085
2086 switch (*reloc_type)
252b5132
RH
2087 {
2088 case BFD_RELOC_32:
2089 ip->insn_opcode |= address_expr->X_add_number;
2090 break;
2091
f6688943
TS
2092 case BFD_RELOC_MIPS_HIGHEST:
2093 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2094 tmp >>= 16;
2095 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2096 break;
2097
2098 case BFD_RELOC_MIPS_HIGHER:
2099 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2100 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2101 break;
2102
2103 case BFD_RELOC_HI16_S:
2104 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2105 >> 16) & 0xffff;
2106 break;
2107
2108 case BFD_RELOC_HI16:
2109 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2110 break;
2111
252b5132 2112 case BFD_RELOC_LO16:
ed6fb7bd 2113 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2114 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2115 break;
2116
2117 case BFD_RELOC_MIPS_JMP:
2118 if ((address_expr->X_add_number & 3) != 0)
2119 as_bad (_("jump to misaligned address (0x%lx)"),
2120 (unsigned long) address_expr->X_add_number);
f3c0ec86 2121 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2122 as_bad (_("jump address range overflow (0x%lx)"),
2123 (unsigned long) address_expr->X_add_number);
252b5132
RH
2124 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2125 break;
2126
2127 case BFD_RELOC_MIPS16_JMP:
2128 if ((address_expr->X_add_number & 3) != 0)
2129 as_bad (_("jump to misaligned address (0x%lx)"),
2130 (unsigned long) address_expr->X_add_number);
f3c0ec86 2131 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2132 as_bad (_("jump address range overflow (0x%lx)"),
2133 (unsigned long) address_expr->X_add_number);
252b5132
RH
2134 ip->insn_opcode |=
2135 (((address_expr->X_add_number & 0x7c0000) << 3)
2136 | ((address_expr->X_add_number & 0xf800000) >> 7)
2137 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2138 break;
2139
252b5132
RH
2140 case BFD_RELOC_16_PCREL_S2:
2141 goto need_reloc;
2142
2143 default:
2144 internalError ();
2145 }
2146 }
2147 else
2148 {
2149 need_reloc:
f6688943 2150 /* Don't generate a reloc if we are writing into a variant frag. */
252b5132
RH
2151 if (place == NULL)
2152 {
f6688943
TS
2153 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2154 address_expr,
af6ae2ad 2155 *reloc_type == BFD_RELOC_16_PCREL_S2,
f6688943
TS
2156 reloc_type[0]);
2157
b6ff326e 2158 /* These relocations can have an addend that won't fit in
f6688943
TS
2159 4 octets for 64bit assembly. */
2160 if (HAVE_64BIT_GPRS &&
2161 (*reloc_type == BFD_RELOC_16
98d3f06f
KH
2162 || *reloc_type == BFD_RELOC_32
2163 || *reloc_type == BFD_RELOC_MIPS_JMP
2164 || *reloc_type == BFD_RELOC_HI16_S
2165 || *reloc_type == BFD_RELOC_LO16
2166 || *reloc_type == BFD_RELOC_GPREL16
2167 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2168 || *reloc_type == BFD_RELOC_GPREL32
2169 || *reloc_type == BFD_RELOC_64
2170 || *reloc_type == BFD_RELOC_CTOR
2171 || *reloc_type == BFD_RELOC_MIPS_SUB
2172 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2173 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2174 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2175 || *reloc_type == BFD_RELOC_MIPS_REL16
2176 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
f6688943
TS
2177 fixp[0]->fx_no_overflow = 1;
2178
252b5132
RH
2179 if (unmatched_hi)
2180 {
2181 struct mips_hi_fixup *hi_fixup;
2182
f6688943 2183 assert (*reloc_type == BFD_RELOC_HI16_S);
252b5132
RH
2184 hi_fixup = ((struct mips_hi_fixup *)
2185 xmalloc (sizeof (struct mips_hi_fixup)));
f6688943 2186 hi_fixup->fixp = fixp[0];
252b5132
RH
2187 hi_fixup->seg = now_seg;
2188 hi_fixup->next = mips_hi_fixup_list;
2189 mips_hi_fixup_list = hi_fixup;
2190 }
f6688943
TS
2191
2192 if (reloc_type[1] != BFD_RELOC_UNUSED)
2193 {
2194 /* FIXME: This symbol can be one of
2195 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
2196 address_expr->X_op = O_absent;
2197 address_expr->X_add_symbol = 0;
2198 address_expr->X_add_number = 0;
2199
2200 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
b34976b6 2201 4, address_expr, FALSE,
f6688943
TS
2202 reloc_type[1]);
2203
b6ff326e 2204 /* These relocations can have an addend that won't fit in
f6688943
TS
2205 4 octets for 64bit assembly. */
2206 if (HAVE_64BIT_GPRS &&
2207 (*reloc_type == BFD_RELOC_16
2208 || *reloc_type == BFD_RELOC_32
2209 || *reloc_type == BFD_RELOC_MIPS_JMP
2210 || *reloc_type == BFD_RELOC_HI16_S
2211 || *reloc_type == BFD_RELOC_LO16
2212 || *reloc_type == BFD_RELOC_GPREL16
2213 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2214 || *reloc_type == BFD_RELOC_GPREL32
2215 || *reloc_type == BFD_RELOC_64
2216 || *reloc_type == BFD_RELOC_CTOR
2217 || *reloc_type == BFD_RELOC_MIPS_SUB
2218 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2219 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2220 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2221 || *reloc_type == BFD_RELOC_MIPS_REL16
2222 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 2223 fixp[1]->fx_no_overflow = 1;
f6688943
TS
2224
2225 if (reloc_type[2] != BFD_RELOC_UNUSED)
2226 {
2227 address_expr->X_op = O_absent;
2228 address_expr->X_add_symbol = 0;
2229 address_expr->X_add_number = 0;
2230
2231 fixp[2] = fix_new_exp (frag_now,
2232 f - frag_now->fr_literal, 4,
b34976b6 2233 address_expr, FALSE,
f6688943
TS
2234 reloc_type[2]);
2235
b6ff326e 2236 /* These relocations can have an addend that won't fit in
f6688943
TS
2237 4 octets for 64bit assembly. */
2238 if (HAVE_64BIT_GPRS &&
2239 (*reloc_type == BFD_RELOC_16
2240 || *reloc_type == BFD_RELOC_32
2241 || *reloc_type == BFD_RELOC_MIPS_JMP
2242 || *reloc_type == BFD_RELOC_HI16_S
2243 || *reloc_type == BFD_RELOC_LO16
2244 || *reloc_type == BFD_RELOC_GPREL16
2245 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2246 || *reloc_type == BFD_RELOC_GPREL32
2247 || *reloc_type == BFD_RELOC_64
2248 || *reloc_type == BFD_RELOC_CTOR
2249 || *reloc_type == BFD_RELOC_MIPS_SUB
2250 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2251 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2252 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2253 || *reloc_type == BFD_RELOC_MIPS_REL16
2254 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 2255 fixp[2]->fx_no_overflow = 1;
f6688943
TS
2256 }
2257 }
252b5132
RH
2258 }
2259 }
2260 }
2261
2262 if (! mips_opts.mips16)
c5dd6aab
DJ
2263 {
2264 md_number_to_chars (f, ip->insn_opcode, 4);
2265#ifdef OBJ_ELF
2266 dwarf2_emit_insn (4);
2267#endif
2268 }
f6688943 2269 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2270 {
2271 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2272 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
c5dd6aab
DJ
2273#ifdef OBJ_ELF
2274 dwarf2_emit_insn (4);
2275#endif
252b5132
RH
2276 }
2277 else
2278 {
2279 if (ip->use_extend)
2280 {
2281 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2282 f += 2;
2283 }
2284 md_number_to_chars (f, ip->insn_opcode, 2);
c5dd6aab
DJ
2285#ifdef OBJ_ELF
2286 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2287#endif
252b5132
RH
2288 }
2289
2290 /* Update the register mask information. */
2291 if (! mips_opts.mips16)
2292 {
2293 if (pinfo & INSN_WRITE_GPR_D)
2294 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2295 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2296 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2297 if (pinfo & INSN_READ_GPR_S)
2298 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2299 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2300 mips_gprmask |= 1 << RA;
252b5132
RH
2301 if (pinfo & INSN_WRITE_FPR_D)
2302 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2303 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2304 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2305 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2306 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2307 if ((pinfo & INSN_READ_FPR_R) != 0)
2308 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2309 if (pinfo & INSN_COP)
2310 {
bdaaa2e1
KH
2311 /* We don't keep enough information to sort these cases out.
2312 The itbl support does keep this information however, although
2313 we currently don't support itbl fprmats as part of the cop
2314 instruction. May want to add this support in the future. */
252b5132
RH
2315 }
2316 /* Never set the bit for $0, which is always zero. */
beae10d5 2317 mips_gprmask &= ~1 << 0;
252b5132
RH
2318 }
2319 else
2320 {
2321 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2322 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2323 & MIPS16OP_MASK_RX);
2324 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2325 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2326 & MIPS16OP_MASK_RY);
2327 if (pinfo & MIPS16_INSN_WRITE_Z)
2328 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2329 & MIPS16OP_MASK_RZ);
2330 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2331 mips_gprmask |= 1 << TREG;
2332 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2333 mips_gprmask |= 1 << SP;
2334 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2335 mips_gprmask |= 1 << RA;
2336 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2337 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2338 if (pinfo & MIPS16_INSN_READ_Z)
2339 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2340 & MIPS16OP_MASK_MOVE32Z);
2341 if (pinfo & MIPS16_INSN_READ_GPR_X)
2342 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2343 & MIPS16OP_MASK_REGR32);
2344 }
2345
2346 if (place == NULL && ! mips_opts.noreorder)
2347 {
2348 /* Filling the branch delay slot is more complex. We try to
2349 switch the branch with the previous instruction, which we can
2350 do if the previous instruction does not set up a condition
2351 that the branch tests and if the branch is not itself the
2352 target of any branch. */
2353 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2354 || (pinfo & INSN_COND_BRANCH_DELAY))
2355 {
2356 if (mips_optimize < 2
2357 /* If we have seen .set volatile or .set nomove, don't
2358 optimize. */
2359 || mips_opts.nomove != 0
2360 /* If we had to emit any NOP instructions, then we
2361 already know we can not swap. */
2362 || nops != 0
2363 /* If we don't even know the previous insn, we can not
bdaaa2e1 2364 swap. */
252b5132
RH
2365 || ! prev_insn_valid
2366 /* If the previous insn is already in a branch delay
2367 slot, then we can not swap. */
2368 || prev_insn_is_delay_slot
2369 /* If the previous previous insn was in a .set
2370 noreorder, we can't swap. Actually, the MIPS
2371 assembler will swap in this situation. However, gcc
2372 configured -with-gnu-as will generate code like
2373 .set noreorder
2374 lw $4,XXX
2375 .set reorder
2376 INSN
2377 bne $4,$0,foo
2378 in which we can not swap the bne and INSN. If gcc is
2379 not configured -with-gnu-as, it does not output the
2380 .set pseudo-ops. We don't have to check
2381 prev_insn_unreordered, because prev_insn_valid will
2382 be 0 in that case. We don't want to use
2383 prev_prev_insn_valid, because we do want to be able
2384 to swap at the start of a function. */
2385 || prev_prev_insn_unreordered
2386 /* If the branch is itself the target of a branch, we
2387 can not swap. We cheat on this; all we check for is
2388 whether there is a label on this instruction. If
2389 there are any branches to anything other than a
2390 label, users must use .set noreorder. */
2391 || insn_labels != NULL
2392 /* If the previous instruction is in a variant frag, we
2393 can not do the swap. This does not apply to the
2394 mips16, which uses variant frags for different
2395 purposes. */
2396 || (! mips_opts.mips16
2397 && prev_insn_frag->fr_type == rs_machine_dependent)
2398 /* If the branch reads the condition codes, we don't
2399 even try to swap, because in the sequence
2400 ctc1 $X,$31
2401 INSN
2402 INSN
2403 bc1t LABEL
2404 we can not swap, and I don't feel like handling that
2405 case. */
2406 || (! mips_opts.mips16
9ce8a5dd 2407 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2408 && (pinfo & INSN_READ_COND_CODE))
2409 /* We can not swap with an instruction that requires a
2410 delay slot, becase the target of the branch might
2411 interfere with that instruction. */
2412 || (! mips_opts.mips16
9ce8a5dd 2413 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 2414 && (prev_pinfo
bdaaa2e1 2415 /* Itbl support may require additional care here. */
252b5132
RH
2416 & (INSN_LOAD_COPROC_DELAY
2417 | INSN_COPROC_MOVE_DELAY
2418 | INSN_WRITE_COND_CODE)))
2419 || (! (hilo_interlocks
ec68c924 2420 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2421 && (prev_pinfo
2422 & (INSN_READ_LO
2423 | INSN_READ_HI)))
2424 || (! mips_opts.mips16
2425 && ! gpr_interlocks
2426 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2427 || (! mips_opts.mips16
e7af610e 2428 && mips_opts.isa == ISA_MIPS1
bdaaa2e1 2429 /* Itbl support may require additional care here. */
252b5132
RH
2430 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2431 /* We can not swap with a branch instruction. */
2432 || (prev_pinfo
2433 & (INSN_UNCOND_BRANCH_DELAY
2434 | INSN_COND_BRANCH_DELAY
2435 | INSN_COND_BRANCH_LIKELY))
2436 /* We do not swap with a trap instruction, since it
2437 complicates trap handlers to have the trap
2438 instruction be in a delay slot. */
2439 || (prev_pinfo & INSN_TRAP)
2440 /* If the branch reads a register that the previous
2441 instruction sets, we can not swap. */
2442 || (! mips_opts.mips16
2443 && (prev_pinfo & INSN_WRITE_GPR_T)
2444 && insn_uses_reg (ip,
2445 ((prev_insn.insn_opcode >> OP_SH_RT)
2446 & OP_MASK_RT),
2447 MIPS_GR_REG))
2448 || (! mips_opts.mips16
2449 && (prev_pinfo & INSN_WRITE_GPR_D)
2450 && insn_uses_reg (ip,
2451 ((prev_insn.insn_opcode >> OP_SH_RD)
2452 & OP_MASK_RD),
2453 MIPS_GR_REG))
2454 || (mips_opts.mips16
2455 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2456 && insn_uses_reg (ip,
2457 ((prev_insn.insn_opcode
2458 >> MIPS16OP_SH_RX)
2459 & MIPS16OP_MASK_RX),
2460 MIPS16_REG))
2461 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2462 && insn_uses_reg (ip,
2463 ((prev_insn.insn_opcode
2464 >> MIPS16OP_SH_RY)
2465 & MIPS16OP_MASK_RY),
2466 MIPS16_REG))
2467 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2468 && insn_uses_reg (ip,
2469 ((prev_insn.insn_opcode
2470 >> MIPS16OP_SH_RZ)
2471 & MIPS16OP_MASK_RZ),
2472 MIPS16_REG))
2473 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2474 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2475 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2476 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2477 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2478 && insn_uses_reg (ip,
2479 MIPS16OP_EXTRACT_REG32R (prev_insn.
2480 insn_opcode),
2481 MIPS_GR_REG))))
2482 /* If the branch writes a register that the previous
2483 instruction sets, we can not swap (we know that
2484 branches write only to RD or to $31). */
2485 || (! mips_opts.mips16
2486 && (prev_pinfo & INSN_WRITE_GPR_T)
2487 && (((pinfo & INSN_WRITE_GPR_D)
2488 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2489 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2490 || ((pinfo & INSN_WRITE_GPR_31)
2491 && (((prev_insn.insn_opcode >> OP_SH_RT)
2492 & OP_MASK_RT)
f9419b05 2493 == RA))))
252b5132
RH
2494 || (! mips_opts.mips16
2495 && (prev_pinfo & INSN_WRITE_GPR_D)
2496 && (((pinfo & INSN_WRITE_GPR_D)
2497 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2498 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2499 || ((pinfo & INSN_WRITE_GPR_31)
2500 && (((prev_insn.insn_opcode >> OP_SH_RD)
2501 & OP_MASK_RD)
f9419b05 2502 == RA))))
252b5132
RH
2503 || (mips_opts.mips16
2504 && (pinfo & MIPS16_INSN_WRITE_31)
2505 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2506 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2507 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2508 == RA))))
2509 /* If the branch writes a register that the previous
2510 instruction reads, we can not swap (we know that
2511 branches only write to RD or to $31). */
2512 || (! mips_opts.mips16
2513 && (pinfo & INSN_WRITE_GPR_D)
2514 && insn_uses_reg (&prev_insn,
2515 ((ip->insn_opcode >> OP_SH_RD)
2516 & OP_MASK_RD),
2517 MIPS_GR_REG))
2518 || (! mips_opts.mips16
2519 && (pinfo & INSN_WRITE_GPR_31)
f9419b05 2520 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2521 || (mips_opts.mips16
2522 && (pinfo & MIPS16_INSN_WRITE_31)
2523 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2524 /* If we are generating embedded PIC code, the branch
2525 might be expanded into a sequence which uses $at, so
2526 we can't swap with an instruction which reads it. */
2527 || (mips_pic == EMBEDDED_PIC
2528 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2529 /* If the previous previous instruction has a load
2530 delay, and sets a register that the branch reads, we
2531 can not swap. */
2532 || (! mips_opts.mips16
9ce8a5dd 2533 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
bdaaa2e1 2534 /* Itbl support may require additional care here. */
252b5132
RH
2535 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2536 || (! gpr_interlocks
2537 && (prev_prev_insn.insn_mo->pinfo
2538 & INSN_LOAD_MEMORY_DELAY)))
2539 && insn_uses_reg (ip,
2540 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2541 & OP_MASK_RT),
2542 MIPS_GR_REG))
2543 /* If one instruction sets a condition code and the
2544 other one uses a condition code, we can not swap. */
2545 || ((pinfo & INSN_READ_COND_CODE)
2546 && (prev_pinfo & INSN_WRITE_COND_CODE))
2547 || ((pinfo & INSN_WRITE_COND_CODE)
2548 && (prev_pinfo & INSN_READ_COND_CODE))
2549 /* If the previous instruction uses the PC, we can not
2550 swap. */
2551 || (mips_opts.mips16
2552 && (prev_pinfo & MIPS16_INSN_READ_PC))
2553 /* If the previous instruction was extended, we can not
2554 swap. */
2555 || (mips_opts.mips16 && prev_insn_extended)
2556 /* If the previous instruction had a fixup in mips16
2557 mode, we can not swap. This normally means that the
2558 previous instruction was a 4 byte branch anyhow. */
f6688943 2559 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2560 /* If the previous instruction is a sync, sync.l, or
2561 sync.p, we can not swap. */
f173e82e 2562 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2563 {
2564 /* We could do even better for unconditional branches to
2565 portions of this object file; we could pick up the
2566 instruction at the destination, put it in the delay
2567 slot, and bump the destination address. */
2568 emit_nop ();
2569 /* Update the previous insn information. */
2570 prev_prev_insn = *ip;
2571 prev_insn.insn_mo = &dummy_opcode;
2572 }
2573 else
2574 {
2575 /* It looks like we can actually do the swap. */
2576 if (! mips_opts.mips16)
2577 {
2578 char *prev_f;
2579 char temp[4];
2580
2581 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2582 memcpy (temp, prev_f, 4);
2583 memcpy (prev_f, f, 4);
2584 memcpy (f, temp, 4);
f6688943
TS
2585 if (prev_insn_fixp[0])
2586 {
2587 prev_insn_fixp[0]->fx_frag = frag_now;
2588 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2589 }
2590 if (prev_insn_fixp[1])
2591 {
2592 prev_insn_fixp[1]->fx_frag = frag_now;
2593 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2594 }
2595 if (prev_insn_fixp[2])
252b5132 2596 {
f6688943
TS
2597 prev_insn_fixp[2]->fx_frag = frag_now;
2598 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2599 }
f6688943 2600 if (fixp[0])
252b5132 2601 {
f6688943
TS
2602 fixp[0]->fx_frag = prev_insn_frag;
2603 fixp[0]->fx_where = prev_insn_where;
2604 }
2605 if (fixp[1])
2606 {
2607 fixp[1]->fx_frag = prev_insn_frag;
2608 fixp[1]->fx_where = prev_insn_where;
2609 }
2610 if (fixp[2])
2611 {
2612 fixp[2]->fx_frag = prev_insn_frag;
2613 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2614 }
2615 }
2616 else
2617 {
2618 char *prev_f;
2619 char temp[2];
2620
f6688943
TS
2621 assert (prev_insn_fixp[0] == NULL);
2622 assert (prev_insn_fixp[1] == NULL);
2623 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2624 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2625 memcpy (temp, prev_f, 2);
2626 memcpy (prev_f, f, 2);
f6688943 2627 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2628 {
f6688943 2629 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2630 memcpy (f, temp, 2);
2631 }
2632 else
2633 {
2634 memcpy (f, f + 2, 2);
2635 memcpy (f + 2, temp, 2);
2636 }
f6688943
TS
2637 if (fixp[0])
2638 {
2639 fixp[0]->fx_frag = prev_insn_frag;
2640 fixp[0]->fx_where = prev_insn_where;
2641 }
2642 if (fixp[1])
2643 {
2644 fixp[1]->fx_frag = prev_insn_frag;
2645 fixp[1]->fx_where = prev_insn_where;
2646 }
2647 if (fixp[2])
252b5132 2648 {
f6688943
TS
2649 fixp[2]->fx_frag = prev_insn_frag;
2650 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2651 }
2652 }
2653
2654 /* Update the previous insn information; leave prev_insn
2655 unchanged. */
2656 prev_prev_insn = *ip;
2657 }
2658 prev_insn_is_delay_slot = 1;
2659
2660 /* If that was an unconditional branch, forget the previous
2661 insn information. */
2662 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2663 {
2664 prev_prev_insn.insn_mo = &dummy_opcode;
2665 prev_insn.insn_mo = &dummy_opcode;
2666 }
2667
f6688943
TS
2668 prev_insn_fixp[0] = NULL;
2669 prev_insn_fixp[1] = NULL;
2670 prev_insn_fixp[2] = NULL;
2671 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2672 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2673 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2674 prev_insn_extended = 0;
2675 }
2676 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2677 {
2678 /* We don't yet optimize a branch likely. What we should do
2679 is look at the target, copy the instruction found there
2680 into the delay slot, and increment the branch to jump to
2681 the next instruction. */
2682 emit_nop ();
2683 /* Update the previous insn information. */
2684 prev_prev_insn = *ip;
2685 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2686 prev_insn_fixp[0] = NULL;
2687 prev_insn_fixp[1] = NULL;
2688 prev_insn_fixp[2] = NULL;
2689 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2690 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2691 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2692 prev_insn_extended = 0;
2693 }
2694 else
2695 {
2696 /* Update the previous insn information. */
2697 if (nops > 0)
2698 prev_prev_insn.insn_mo = &dummy_opcode;
2699 else
2700 prev_prev_insn = prev_insn;
2701 prev_insn = *ip;
2702
2703 /* Any time we see a branch, we always fill the delay slot
2704 immediately; since this insn is not a branch, we know it
2705 is not in a delay slot. */
2706 prev_insn_is_delay_slot = 0;
2707
f6688943
TS
2708 prev_insn_fixp[0] = fixp[0];
2709 prev_insn_fixp[1] = fixp[1];
2710 prev_insn_fixp[2] = fixp[2];
2711 prev_insn_reloc_type[0] = reloc_type[0];
2712 prev_insn_reloc_type[1] = reloc_type[1];
2713 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2714 if (mips_opts.mips16)
2715 prev_insn_extended = (ip->use_extend
f6688943 2716 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2717 }
2718
2719 prev_prev_insn_unreordered = prev_insn_unreordered;
2720 prev_insn_unreordered = 0;
2721 prev_insn_frag = frag_now;
2722 prev_insn_where = f - frag_now->fr_literal;
2723 prev_insn_valid = 1;
2724 }
2725 else if (place == NULL)
2726 {
2727 /* We need to record a bit of information even when we are not
2728 reordering, in order to determine the base address for mips16
2729 PC relative relocs. */
2730 prev_prev_insn = prev_insn;
2731 prev_insn = *ip;
f6688943
TS
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];
252b5132
RH
2735 prev_prev_insn_unreordered = prev_insn_unreordered;
2736 prev_insn_unreordered = 1;
2737 }
2738
2739 /* We just output an insn, so the next one doesn't have a label. */
2740 mips_clear_insn_labels ();
2741
2742 /* We must ensure that a fixup associated with an unmatched %hi
2743 reloc does not become a variant frag. Otherwise, the
2744 rearrangement of %hi relocs in frob_file may confuse
2745 tc_gen_reloc. */
2746 if (unmatched_hi)
2747 {
2748 frag_wane (frag_now);
2749 frag_new (0);
2750 }
2751}
2752
2753/* This function forgets that there was any previous instruction or
2754 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2755 know whether nops are needed before a noreorder section. */
252b5132
RH
2756
2757static void
2758mips_no_prev_insn (preserve)
2759 int preserve;
2760{
2761 if (! preserve)
2762 {
2763 prev_insn.insn_mo = &dummy_opcode;
2764 prev_prev_insn.insn_mo = &dummy_opcode;
2765 prev_nop_frag = NULL;
2766 prev_nop_frag_holds = 0;
2767 prev_nop_frag_required = 0;
2768 prev_nop_frag_since = 0;
2769 }
2770 prev_insn_valid = 0;
2771 prev_insn_is_delay_slot = 0;
2772 prev_insn_unreordered = 0;
2773 prev_insn_extended = 0;
f6688943
TS
2774 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2775 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2776 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2777 prev_prev_insn_unreordered = 0;
2778 mips_clear_insn_labels ();
2779}
2780
2781/* This function must be called whenever we turn on noreorder or emit
2782 something other than instructions. It inserts any NOPS which might
2783 be needed by the previous instruction, and clears the information
2784 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2785 instructions are to follow. */
252b5132
RH
2786
2787static void
2788mips_emit_delays (insns)
b34976b6 2789 bfd_boolean insns;
252b5132
RH
2790{
2791 if (! mips_opts.noreorder)
2792 {
2793 int nops;
2794
2795 nops = 0;
2796 if ((! mips_opts.mips16
9ce8a5dd 2797 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2798 && (! cop_interlocks
2799 && (prev_insn.insn_mo->pinfo
2800 & (INSN_LOAD_COPROC_DELAY
2801 | INSN_COPROC_MOVE_DELAY
2802 | INSN_WRITE_COND_CODE))))
2803 || (! hilo_interlocks
2804 && (prev_insn.insn_mo->pinfo
2805 & (INSN_READ_LO
2806 | INSN_READ_HI)))
2807 || (! mips_opts.mips16
2808 && ! gpr_interlocks
bdaaa2e1 2809 && (prev_insn.insn_mo->pinfo
252b5132
RH
2810 & INSN_LOAD_MEMORY_DELAY))
2811 || (! mips_opts.mips16
e7af610e 2812 && mips_opts.isa == ISA_MIPS1
252b5132
RH
2813 && (prev_insn.insn_mo->pinfo
2814 & INSN_COPROC_MEMORY_DELAY)))
2815 {
beae10d5 2816 /* Itbl support may require additional care here. */
252b5132
RH
2817 ++nops;
2818 if ((! mips_opts.mips16
9ce8a5dd 2819 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2820 && (! cop_interlocks
2821 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2822 || (! hilo_interlocks
2823 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2824 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2825 ++nops;
2826
2827 if (prev_insn_unreordered)
2828 nops = 0;
2829 }
2830 else if ((! mips_opts.mips16
9ce8a5dd 2831 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2832 && (! cop_interlocks
2833 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2834 || (! hilo_interlocks
2835 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2836 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2837 {
beae10d5 2838 /* Itbl support may require additional care here. */
252b5132
RH
2839 if (! prev_prev_insn_unreordered)
2840 ++nops;
2841 }
2842
60b63b72
RS
2843 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2844 {
2845 int min_nops = 0;
2846 const char *pn = prev_insn.insn_mo->name;
2847 if (strncmp(pn, "macc", 4) == 0
2848 || strncmp(pn, "dmacc", 5) == 0
2849 || strncmp(pn, "dmult", 5) == 0)
2850 {
2851 min_nops = 1;
2852 }
2853 if (nops < min_nops)
2854 nops = min_nops;
2855 }
2856
252b5132
RH
2857 if (nops > 0)
2858 {
2859 struct insn_label_list *l;
2860
2861 if (insns)
2862 {
2863 /* Record the frag which holds the nop instructions, so
2864 that we can remove them if we don't need them. */
2865 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2866 prev_nop_frag = frag_now;
2867 prev_nop_frag_holds = nops;
2868 prev_nop_frag_required = 0;
2869 prev_nop_frag_since = 0;
2870 }
2871
2872 for (; nops > 0; --nops)
2873 emit_nop ();
2874
2875 if (insns)
2876 {
2877 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2878 decrease the size of prev_nop_frag. */
252b5132
RH
2879 frag_wane (frag_now);
2880 frag_new (0);
2881 }
2882
2883 for (l = insn_labels; l != NULL; l = l->next)
2884 {
98aa84af
AM
2885 valueT val;
2886
252b5132 2887 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2888 symbol_set_frag (l->label, frag_now);
98aa84af 2889 val = (valueT) frag_now_fix ();
252b5132
RH
2890 /* mips16 text labels are stored as odd. */
2891 if (mips_opts.mips16)
f9419b05 2892 ++val;
98aa84af 2893 S_SET_VALUE (l->label, val);
252b5132
RH
2894 }
2895 }
2896 }
2897
2898 /* Mark instruction labels in mips16 mode. */
f9419b05 2899 if (insns)
252b5132
RH
2900 mips16_mark_labels ();
2901
2902 mips_no_prev_insn (insns);
2903}
2904
2905/* Build an instruction created by a macro expansion. This is passed
2906 a pointer to the count of instructions created so far, an
2907 expression, the name of the instruction to build, an operand format
2908 string, and corresponding arguments. */
2909
2910#ifdef USE_STDARG
2911static void
2912macro_build (char *place,
2913 int *counter,
2914 expressionS * ep,
2915 const char *name,
2916 const char *fmt,
2917 ...)
2918#else
2919static void
2920macro_build (place, counter, ep, name, fmt, va_alist)
2921 char *place;
2922 int *counter;
2923 expressionS *ep;
2924 const char *name;
2925 const char *fmt;
2926 va_dcl
2927#endif
2928{
2929 struct mips_cl_insn insn;
f6688943 2930 bfd_reloc_code_real_type r[3];
252b5132 2931 va_list args;
252b5132
RH
2932
2933#ifdef USE_STDARG
2934 va_start (args, fmt);
2935#else
2936 va_start (args);
2937#endif
2938
2939 /*
2940 * If the macro is about to expand into a second instruction,
2941 * print a warning if needed. We need to pass ip as a parameter
2942 * to generate a better warning message here...
2943 */
2944 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2945 as_warn (_("Macro instruction expanded into multiple instructions"));
2946
80cc45a5
EC
2947 /*
2948 * If the macro is about to expand into a second instruction,
2949 * and it is in a delay slot, print a warning.
2950 */
2951 if (place == NULL
2952 && *counter == 1
2953 && mips_opts.noreorder
2954 && (prev_prev_insn.insn_mo->pinfo
2955 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2b2e39bf 2956 | INSN_COND_BRANCH_LIKELY)) != 0)
80cc45a5
EC
2957 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2958
252b5132 2959 if (place == NULL)
f9419b05 2960 ++*counter; /* bump instruction counter */
252b5132
RH
2961
2962 if (mips_opts.mips16)
2963 {
2964 mips16_macro_build (place, counter, ep, name, fmt, args);
2965 va_end (args);
2966 return;
2967 }
2968
f6688943
TS
2969 r[0] = BFD_RELOC_UNUSED;
2970 r[1] = BFD_RELOC_UNUSED;
2971 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2972 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2973 assert (insn.insn_mo);
2974 assert (strcmp (name, insn.insn_mo->name) == 0);
2975
2976 /* Search until we get a match for NAME. */
2977 while (1)
2978 {
b34976b6 2979 /* It is assumed here that macros will never generate
deec1734 2980 MDMX or MIPS-3D instructions. */
252b5132
RH
2981 if (strcmp (fmt, insn.insn_mo->args) == 0
2982 && insn.insn_mo->pinfo != INSN_MACRO
aec421e0
TS
2983 && OPCODE_IS_MEMBER (insn.insn_mo,
2984 (mips_opts.isa
3396de36 2985 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
aec421e0 2986 mips_arch)
ec68c924 2987 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
2988 break;
2989
2990 ++insn.insn_mo;
2991 assert (insn.insn_mo->name);
2992 assert (strcmp (name, insn.insn_mo->name) == 0);
2993 }
2994
2995 insn.insn_opcode = insn.insn_mo->match;
2996 for (;;)
2997 {
2998 switch (*fmt++)
2999 {
3000 case '\0':
3001 break;
3002
3003 case ',':
3004 case '(':
3005 case ')':
3006 continue;
3007
3008 case 't':
3009 case 'w':
3010 case 'E':
38487616 3011 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
3012 continue;
3013
3014 case 'c':
38487616
TS
3015 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3016 continue;
3017
252b5132
RH
3018 case 'T':
3019 case 'W':
38487616 3020 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
3021 continue;
3022
3023 case 'd':
3024 case 'G':
af7ee8bf 3025 case 'K':
38487616 3026 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
3027 continue;
3028
4372b673
NC
3029 case 'U':
3030 {
3031 int tmp = va_arg (args, int);
3032
38487616
TS
3033 insn.insn_opcode |= tmp << OP_SH_RT;
3034 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 3035 continue;
4372b673
NC
3036 }
3037
252b5132
RH
3038 case 'V':
3039 case 'S':
38487616 3040 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
3041 continue;
3042
3043 case 'z':
3044 continue;
3045
3046 case '<':
38487616 3047 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
3048 continue;
3049
3050 case 'D':
38487616 3051 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
3052 continue;
3053
3054 case 'B':
38487616 3055 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
3056 continue;
3057
4372b673 3058 case 'J':
38487616 3059 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
3060 continue;
3061
252b5132 3062 case 'q':
38487616 3063 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
3064 continue;
3065
3066 case 'b':
3067 case 's':
3068 case 'r':
3069 case 'v':
38487616 3070 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
3071 continue;
3072
3073 case 'i':
3074 case 'j':
3075 case 'o':
f6688943 3076 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 3077 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3078 || *r == BFD_RELOC_MIPS_LITERAL
3079 || *r == BFD_RELOC_MIPS_HIGHER
3080 || *r == BFD_RELOC_HI16_S
3081 || *r == BFD_RELOC_LO16
3082 || *r == BFD_RELOC_MIPS_GOT16
3083 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
3084 || *r == BFD_RELOC_MIPS_GOT_DISP
3085 || *r == BFD_RELOC_MIPS_GOT_PAGE
3086 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943
TS
3087 || *r == BFD_RELOC_MIPS_GOT_LO16
3088 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 3089 || (ep->X_op == O_subtract
f6688943 3090 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
3091 continue;
3092
3093 case 'u':
f6688943 3094 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
3095 assert (ep != NULL
3096 && (ep->X_op == O_constant
3097 || (ep->X_op == O_symbol
f6688943
TS
3098 && (*r == BFD_RELOC_MIPS_HIGHEST
3099 || *r == BFD_RELOC_HI16_S
3100 || *r == BFD_RELOC_HI16
3101 || *r == BFD_RELOC_GPREL16
3102 || *r == BFD_RELOC_MIPS_GOT_HI16
3103 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 3104 || (ep->X_op == O_subtract
f6688943 3105 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
3106 continue;
3107
3108 case 'p':
3109 assert (ep != NULL);
3110 /*
3111 * This allows macro() to pass an immediate expression for
3112 * creating short branches without creating a symbol.
3113 * Note that the expression still might come from the assembly
3114 * input, in which case the value is not checked for range nor
3115 * is a relocation entry generated (yuck).
3116 */
3117 if (ep->X_op == O_constant)
3118 {
3119 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3120 ep = NULL;
3121 }
3122 else
af6ae2ad 3123 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3124 continue;
3125
3126 case 'a':
3127 assert (ep != NULL);
f6688943 3128 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3129 continue;
3130
3131 case 'C':
3132 insn.insn_opcode |= va_arg (args, unsigned long);
3133 continue;
3134
3135 default:
3136 internalError ();
3137 }
3138 break;
3139 }
3140 va_end (args);
f6688943 3141 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3142
b34976b6 3143 append_insn (place, &insn, ep, r, FALSE);
252b5132
RH
3144}
3145
3146static void
3147mips16_macro_build (place, counter, ep, name, fmt, args)
3148 char *place;
43841e91 3149 int *counter ATTRIBUTE_UNUSED;
252b5132
RH
3150 expressionS *ep;
3151 const char *name;
3152 const char *fmt;
3153 va_list args;
3154{
3155 struct mips_cl_insn insn;
f6688943
TS
3156 bfd_reloc_code_real_type r[3]
3157 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3158
252b5132
RH
3159 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3160 assert (insn.insn_mo);
3161 assert (strcmp (name, insn.insn_mo->name) == 0);
3162
3163 while (strcmp (fmt, insn.insn_mo->args) != 0
3164 || insn.insn_mo->pinfo == INSN_MACRO)
3165 {
3166 ++insn.insn_mo;
3167 assert (insn.insn_mo->name);
3168 assert (strcmp (name, insn.insn_mo->name) == 0);
3169 }
3170
3171 insn.insn_opcode = insn.insn_mo->match;
b34976b6 3172 insn.use_extend = FALSE;
252b5132
RH
3173
3174 for (;;)
3175 {
3176 int c;
3177
3178 c = *fmt++;
3179 switch (c)
3180 {
3181 case '\0':
3182 break;
3183
3184 case ',':
3185 case '(':
3186 case ')':
3187 continue;
3188
3189 case 'y':
3190 case 'w':
3191 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3192 continue;
3193
3194 case 'x':
3195 case 'v':
3196 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3197 continue;
3198
3199 case 'z':
3200 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3201 continue;
3202
3203 case 'Z':
3204 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3205 continue;
3206
3207 case '0':
3208 case 'S':
3209 case 'P':
3210 case 'R':
3211 continue;
3212
3213 case 'X':
3214 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3215 continue;
3216
3217 case 'Y':
3218 {
3219 int regno;
3220
3221 regno = va_arg (args, int);
3222 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3223 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3224 }
3225 continue;
3226
3227 case '<':
3228 case '>':
3229 case '4':
3230 case '5':
3231 case 'H':
3232 case 'W':
3233 case 'D':
3234 case 'j':
3235 case '8':
3236 case 'V':
3237 case 'C':
3238 case 'U':
3239 case 'k':
3240 case 'K':
3241 case 'p':
3242 case 'q':
3243 {
3244 assert (ep != NULL);
3245
3246 if (ep->X_op != O_constant)
874e8986 3247 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3248 else
3249 {
b34976b6
AM
3250 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3251 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3252 &insn.extend);
252b5132 3253 ep = NULL;
f6688943 3254 *r = BFD_RELOC_UNUSED;
252b5132
RH
3255 }
3256 }
3257 continue;
3258
3259 case '6':
3260 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3261 continue;
3262 }
3263
3264 break;
3265 }
3266
f6688943 3267 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3268
b34976b6 3269 append_insn (place, &insn, ep, r, FALSE);
252b5132
RH
3270}
3271
438c16b8
TS
3272/*
3273 * Generate a "jalr" instruction with a relocation hint to the called
3274 * function. This occurs in NewABI PIC code.
3275 */
3276static void
3277macro_build_jalr (icnt, ep)
3278 int icnt;
3279 expressionS *ep;
3280{
f21f8242 3281 char *f;
b34976b6 3282
438c16b8 3283 if (HAVE_NEWABI)
f21f8242
AO
3284 {
3285 frag_grow (4);
3286 f = frag_more (0);
3287 }
438c16b8
TS
3288 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3289 RA, PIC_CALL_REG);
3290 if (HAVE_NEWABI)
f21f8242 3291 fix_new_exp (frag_now, f - frag_now->fr_literal,
b34976b6 3292 0, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3293}
3294
252b5132
RH
3295/*
3296 * Generate a "lui" instruction.
3297 */
3298static void
3299macro_build_lui (place, counter, ep, regnum)
3300 char *place;
3301 int *counter;
3302 expressionS *ep;
3303 int regnum;
3304{
3305 expressionS high_expr;
3306 struct mips_cl_insn insn;
f6688943
TS
3307 bfd_reloc_code_real_type r[3]
3308 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3309 const char *name = "lui";
3310 const char *fmt = "t,u";
252b5132
RH
3311
3312 assert (! mips_opts.mips16);
3313
3314 if (place == NULL)
3315 high_expr = *ep;
3316 else
3317 {
3318 high_expr.X_op = O_constant;
3319 high_expr.X_add_number = ep->X_add_number;
3320 }
3321
3322 if (high_expr.X_op == O_constant)
3323 {
3324 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3325 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3326 >> 16) & 0xffff;
f6688943 3327 *r = BFD_RELOC_UNUSED;
252b5132 3328 }
78e1bb40 3329 else
252b5132
RH
3330 {
3331 assert (ep->X_op == O_symbol);
3332 /* _gp_disp is a special case, used from s_cpload. */
3333 assert (mips_pic == NO_PIC
78e1bb40
AO
3334 || (! HAVE_NEWABI
3335 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
f6688943 3336 *r = BFD_RELOC_HI16_S;
252b5132
RH
3337 }
3338
3339 /*
3340 * If the macro is about to expand into a second instruction,
3341 * print a warning if needed. We need to pass ip as a parameter
3342 * to generate a better warning message here...
3343 */
3344 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3345 as_warn (_("Macro instruction expanded into multiple instructions"));
3346
3347 if (place == NULL)
f9419b05 3348 ++*counter; /* bump instruction counter */
252b5132
RH
3349
3350 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3351 assert (insn.insn_mo);
3352 assert (strcmp (name, insn.insn_mo->name) == 0);
3353 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3354
3355 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3356 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3357 {
3358 insn.insn_opcode |= high_expr.X_add_number;
b34976b6 3359 append_insn (place, &insn, NULL, r, FALSE);
252b5132
RH
3360 }
3361 else
b34976b6 3362 append_insn (place, &insn, &high_expr, r, FALSE);
252b5132
RH
3363}
3364
885add95
CD
3365/* Generate a sequence of instructions to do a load or store from a constant
3366 offset off of a base register (breg) into/from a target register (treg),
3367 using AT if necessary. */
3368static void
3369macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3370 char *place;
3371 int *counter;
3372 expressionS *ep;
3373 const char *op;
3374 int treg, breg;
3375{
3376 assert (ep->X_op == O_constant);
3377
3378 /* Right now, this routine can only handle signed 32-bit contants. */
3379 if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3380 as_warn (_("operand overflow"));
3381
3382 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3383 {
3384 /* Signed 16-bit offset will fit in the op. Easy! */
3385 macro_build (place, counter, ep, op, "t,o(b)", treg,
3386 (int) BFD_RELOC_LO16, breg);
3387 }
3388 else
3389 {
3390 /* 32-bit offset, need multiple instructions and AT, like:
3391 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3392 addu $tempreg,$tempreg,$breg
3393 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3394 to handle the complete offset. */
3395 macro_build_lui (place, counter, ep, AT);
3396 if (place != NULL)
3397 place += 4;
3398 macro_build (place, counter, (expressionS *) NULL,
3399 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3400 "d,v,t", AT, AT, breg);
3401 if (place != NULL)
3402 place += 4;
3403 macro_build (place, counter, ep, op, "t,o(b)", treg,
3404 (int) BFD_RELOC_LO16, AT);
3405
3406 if (mips_opts.noat)
3407 as_warn (_("Macro used $at after \".set noat\""));
3408 }
3409}
3410
252b5132
RH
3411/* set_at()
3412 * Generates code to set the $at register to true (one)
3413 * if reg is less than the immediate expression.
3414 */
3415static void
3416set_at (counter, reg, unsignedp)
3417 int *counter;
3418 int reg;
3419 int unsignedp;
3420{
3421 if (imm_expr.X_op == O_constant
3422 && imm_expr.X_add_number >= -0x8000
3423 && imm_expr.X_add_number < 0x8000)
3424 macro_build ((char *) NULL, counter, &imm_expr,
3425 unsignedp ? "sltiu" : "slti",
3426 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3427 else
3428 {
4d34fb5f 3429 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9 3430 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132
RH
3431 unsignedp ? "sltu" : "slt",
3432 "d,v,t", AT, reg, AT);
3433 }
3434}
3435
3436/* Warn if an expression is not a constant. */
3437
3438static void
3439check_absolute_expr (ip, ex)
3440 struct mips_cl_insn *ip;
3441 expressionS *ex;
3442{
3443 if (ex->X_op == O_big)
3444 as_bad (_("unsupported large constant"));
3445 else if (ex->X_op != O_constant)
3446 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3447}
3448
3449/* Count the leading zeroes by performing a binary chop. This is a
3450 bulky bit of source, but performance is a LOT better for the
3451 majority of values than a simple loop to count the bits:
3452 for (lcnt = 0; (lcnt < 32); lcnt++)
3453 if ((v) & (1 << (31 - lcnt)))
3454 break;
3455 However it is not code size friendly, and the gain will drop a bit
3456 on certain cached systems.
3457*/
3458#define COUNT_TOP_ZEROES(v) \
3459 (((v) & ~0xffff) == 0 \
3460 ? ((v) & ~0xff) == 0 \
3461 ? ((v) & ~0xf) == 0 \
3462 ? ((v) & ~0x3) == 0 \
3463 ? ((v) & ~0x1) == 0 \
3464 ? !(v) \
3465 ? 32 \
3466 : 31 \
3467 : 30 \
3468 : ((v) & ~0x7) == 0 \
3469 ? 29 \
3470 : 28 \
3471 : ((v) & ~0x3f) == 0 \
3472 ? ((v) & ~0x1f) == 0 \
3473 ? 27 \
3474 : 26 \
3475 : ((v) & ~0x7f) == 0 \
3476 ? 25 \
3477 : 24 \
3478 : ((v) & ~0xfff) == 0 \
3479 ? ((v) & ~0x3ff) == 0 \
3480 ? ((v) & ~0x1ff) == 0 \
3481 ? 23 \
3482 : 22 \
3483 : ((v) & ~0x7ff) == 0 \
3484 ? 21 \
3485 : 20 \
3486 : ((v) & ~0x3fff) == 0 \
3487 ? ((v) & ~0x1fff) == 0 \
3488 ? 19 \
3489 : 18 \
3490 : ((v) & ~0x7fff) == 0 \
3491 ? 17 \
3492 : 16 \
3493 : ((v) & ~0xffffff) == 0 \
3494 ? ((v) & ~0xfffff) == 0 \
3495 ? ((v) & ~0x3ffff) == 0 \
3496 ? ((v) & ~0x1ffff) == 0 \
3497 ? 15 \
3498 : 14 \
3499 : ((v) & ~0x7ffff) == 0 \
3500 ? 13 \
3501 : 12 \
3502 : ((v) & ~0x3fffff) == 0 \
3503 ? ((v) & ~0x1fffff) == 0 \
3504 ? 11 \
3505 : 10 \
3506 : ((v) & ~0x7fffff) == 0 \
3507 ? 9 \
3508 : 8 \
3509 : ((v) & ~0xfffffff) == 0 \
3510 ? ((v) & ~0x3ffffff) == 0 \
3511 ? ((v) & ~0x1ffffff) == 0 \
3512 ? 7 \
3513 : 6 \
3514 : ((v) & ~0x7ffffff) == 0 \
3515 ? 5 \
3516 : 4 \
3517 : ((v) & ~0x3fffffff) == 0 \
3518 ? ((v) & ~0x1fffffff) == 0 \
3519 ? 3 \
3520 : 2 \
3521 : ((v) & ~0x7fffffff) == 0 \
3522 ? 1 \
3523 : 0)
3524
3525/* load_register()
3526 * This routine generates the least number of instructions neccessary to load
3527 * an absolute expression value into a register.
3528 */
3529static void
3530load_register (counter, reg, ep, dbl)
3531 int *counter;
3532 int reg;
3533 expressionS *ep;
3534 int dbl;
3535{
3536 int freg;
3537 expressionS hi32, lo32;
3538
3539 if (ep->X_op != O_big)
3540 {
3541 assert (ep->X_op == O_constant);
3542 if (ep->X_add_number < 0x8000
3543 && (ep->X_add_number >= 0
3544 || (ep->X_add_number >= -0x8000
3545 && (! dbl
3546 || ! ep->X_unsigned
3547 || sizeof (ep->X_add_number) > 4))))
3548 {
3549 /* We can handle 16 bit signed values with an addiu to
3550 $zero. No need to ever use daddiu here, since $zero and
3551 the result are always correct in 32 bit mode. */
3552 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3553 (int) BFD_RELOC_LO16);
3554 return;
3555 }
3556 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3557 {
3558 /* We can handle 16 bit unsigned values with an ori to
3559 $zero. */
3560 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3561 (int) BFD_RELOC_LO16);
3562 return;
3563 }
6373ee54 3564 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
252b5132
RH
3565 && (! dbl
3566 || ! ep->X_unsigned
3567 || sizeof (ep->X_add_number) > 4
3568 || (ep->X_add_number & 0x80000000) == 0))
ca4e0257 3569 || ((HAVE_32BIT_GPRS || ! dbl)
252b5132 3570 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
ca4e0257 3571 || (HAVE_32BIT_GPRS
252b5132
RH
3572 && ! dbl
3573 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3574 == ~ (offsetT) 0xffffffff)))
3575 {
3576 /* 32 bit values require an lui. */
3577 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3578 (int) BFD_RELOC_HI16);
3579 if ((ep->X_add_number & 0xffff) != 0)
3580 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3581 (int) BFD_RELOC_LO16);
3582 return;
3583 }
3584 }
3585
3586 /* The value is larger than 32 bits. */
3587
ca4e0257 3588 if (HAVE_32BIT_GPRS)
252b5132 3589 {
956cd1d6
TS
3590 as_bad (_("Number (0x%lx) larger than 32 bits"),
3591 (unsigned long) ep->X_add_number);
252b5132
RH
3592 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3593 (int) BFD_RELOC_LO16);
3594 return;
3595 }
3596
3597 if (ep->X_op != O_big)
3598 {
3599 hi32 = *ep;
3600 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3601 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3602 hi32.X_add_number &= 0xffffffff;
3603 lo32 = *ep;
3604 lo32.X_add_number &= 0xffffffff;
3605 }
3606 else
3607 {
3608 assert (ep->X_add_number > 2);
3609 if (ep->X_add_number == 3)
3610 generic_bignum[3] = 0;
3611 else if (ep->X_add_number > 4)
3612 as_bad (_("Number larger than 64 bits"));
3613 lo32.X_op = O_constant;
3614 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3615 hi32.X_op = O_constant;
3616 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3617 }
3618
3619 if (hi32.X_add_number == 0)
3620 freg = 0;
3621 else
3622 {
3623 int shift, bit;
3624 unsigned long hi, lo;
3625
956cd1d6 3626 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3627 {
3628 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3629 {
3630 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
252b5132 3631 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3632 return;
3633 }
3634 if (lo32.X_add_number & 0x80000000)
3635 {
3636 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3637 (int) BFD_RELOC_HI16);
252b5132
RH
3638 if (lo32.X_add_number & 0xffff)
3639 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3640 reg, reg, (int) BFD_RELOC_LO16);
beae10d5
KH
3641 return;
3642 }
3643 }
252b5132
RH
3644
3645 /* Check for 16bit shifted constant. We know that hi32 is
3646 non-zero, so start the mask on the first bit of the hi32
3647 value. */
3648 shift = 17;
3649 do
beae10d5
KH
3650 {
3651 unsigned long himask, lomask;
3652
3653 if (shift < 32)
3654 {
3655 himask = 0xffff >> (32 - shift);
3656 lomask = (0xffff << shift) & 0xffffffff;
3657 }
3658 else
3659 {
3660 himask = 0xffff << (shift - 32);
3661 lomask = 0;
3662 }
3663 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3664 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3665 {
3666 expressionS tmp;
3667
3668 tmp.X_op = O_constant;
3669 if (shift < 32)
3670 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3671 | (lo32.X_add_number >> shift));
3672 else
3673 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3674 macro_build ((char *) NULL, counter, &tmp,
3675 "ori", "t,r,i", reg, 0,
3676 (int) BFD_RELOC_LO16);
2396cfb9 3677 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3678 (shift >= 32) ? "dsll32" : "dsll",
3679 "d,w,<", reg, reg,
3680 (shift >= 32) ? shift - 32 : shift);
3681 return;
3682 }
f9419b05 3683 ++shift;
beae10d5
KH
3684 }
3685 while (shift <= (64 - 16));
252b5132
RH
3686
3687 /* Find the bit number of the lowest one bit, and store the
3688 shifted value in hi/lo. */
3689 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3690 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3691 if (lo != 0)
3692 {
3693 bit = 0;
3694 while ((lo & 1) == 0)
3695 {
3696 lo >>= 1;
3697 ++bit;
3698 }
3699 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3700 hi >>= bit;
3701 }
3702 else
3703 {
3704 bit = 32;
3705 while ((hi & 1) == 0)
3706 {
3707 hi >>= 1;
3708 ++bit;
3709 }
3710 lo = hi;
3711 hi = 0;
3712 }
3713
3714 /* Optimize if the shifted value is a (power of 2) - 1. */
3715 if ((hi == 0 && ((lo + 1) & lo) == 0)
3716 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3717 {
3718 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3719 if (shift != 0)
beae10d5 3720 {
252b5132
RH
3721 expressionS tmp;
3722
3723 /* This instruction will set the register to be all
3724 ones. */
beae10d5
KH
3725 tmp.X_op = O_constant;
3726 tmp.X_add_number = (offsetT) -1;
3727 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
252b5132 3728 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3729 if (bit != 0)
3730 {
3731 bit += shift;
2396cfb9 3732 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3733 (bit >= 32) ? "dsll32" : "dsll",
3734 "d,w,<", reg, reg,
3735 (bit >= 32) ? bit - 32 : bit);
3736 }
2396cfb9 3737 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132 3738 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3739 "d,w,<", reg, reg,
252b5132 3740 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3741 return;
3742 }
3743 }
252b5132
RH
3744
3745 /* Sign extend hi32 before calling load_register, because we can
3746 generally get better code when we load a sign extended value. */
3747 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3748 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3749 load_register (counter, reg, &hi32, 0);
3750 freg = reg;
3751 }
3752 if ((lo32.X_add_number & 0xffff0000) == 0)
3753 {
3754 if (freg != 0)
3755 {
2396cfb9
TS
3756 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3757 "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3758 freg = reg;
3759 }
3760 }
3761 else
3762 {
3763 expressionS mid16;
3764
956cd1d6 3765 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3766 {
252b5132
RH
3767 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3768 (int) BFD_RELOC_HI16);
956cd1d6
TS
3769 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3770 "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3771 return;
3772 }
252b5132
RH
3773
3774 if (freg != 0)
3775 {
956cd1d6
TS
3776 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3777 "d,w,<", reg, freg, 16);
252b5132
RH
3778 freg = reg;
3779 }
3780 mid16 = lo32;
3781 mid16.X_add_number >>= 16;
3782 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3783 freg, (int) BFD_RELOC_LO16);
956cd1d6
TS
3784 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3785 "d,w,<", reg, reg, 16);
252b5132
RH
3786 freg = reg;
3787 }
3788 if ((lo32.X_add_number & 0xffff) != 0)
3789 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3790 (int) BFD_RELOC_LO16);
3791}
3792
3793/* Load an address into a register. */
3794
3795static void
c9914766 3796load_address (counter, reg, ep, used_at)
252b5132
RH
3797 int *counter;
3798 int reg;
3799 expressionS *ep;
d6bc6245 3800 int *used_at;
252b5132 3801{
f9419b05 3802 char *p = NULL;
252b5132
RH
3803
3804 if (ep->X_op != O_constant
3805 && ep->X_op != O_symbol)
3806 {
3807 as_bad (_("expression too complex"));
3808 ep->X_op = O_constant;
3809 }
3810
3811 if (ep->X_op == O_constant)
3812 {
c9914766 3813 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3814 return;
3815 }
3816
3817 if (mips_pic == NO_PIC)
3818 {
3819 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3820 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3821 Otherwise we want
3822 lui $reg,<sym> (BFD_RELOC_HI16_S)
3823 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3824 If we have an addend, we always use the latter form.
76b3015f 3825
d6bc6245
TS
3826 With 64bit address space and a usable $at we want
3827 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3828 lui $at,<sym> (BFD_RELOC_HI16_S)
3829 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3830 daddiu $at,<sym> (BFD_RELOC_LO16)
3831 dsll32 $reg,0
3a482fd5 3832 daddu $reg,$reg,$at
76b3015f 3833
c03099e6 3834 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
3835 on superscalar processors.
3836 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3837 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3838 dsll $reg,16
3839 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3840 dsll $reg,16
3841 daddiu $reg,<sym> (BFD_RELOC_LO16)
3842 */
c9914766 3843 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3844 {
d6bc6245
TS
3845 /* We don't do GP optimization for now because RELAX_ENCODE can't
3846 hold the data for such large chunks. */
3847
460597ba 3848 if (*used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
3849 {
3850 macro_build (p, counter, ep, "lui", "t,u",
3851 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3852 macro_build (p, counter, ep, "lui", "t,u",
3853 AT, (int) BFD_RELOC_HI16_S);
3854 macro_build (p, counter, ep, "daddiu", "t,r,j",
3855 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3856 macro_build (p, counter, ep, "daddiu", "t,r,j",
3857 AT, AT, (int) BFD_RELOC_LO16);
2396cfb9
TS
3858 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3859 "d,w,<", reg, reg, 0);
3a482fd5 3860 macro_build (p, counter, (expressionS *) NULL, "daddu",
2396cfb9 3861 "d,v,t", reg, reg, AT);
d6bc6245
TS
3862 *used_at = 1;
3863 }
3864 else
3865 {
3866 macro_build (p, counter, ep, "lui", "t,u",
3867 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3868 macro_build (p, counter, ep, "daddiu", "t,r,j",
3869 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
3870 macro_build (p, counter, (expressionS *) NULL, "dsll",
3871 "d,w,<", reg, reg, 16);
d6bc6245
TS
3872 macro_build (p, counter, ep, "daddiu", "t,r,j",
3873 reg, reg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
3874 macro_build (p, counter, (expressionS *) NULL, "dsll",
3875 "d,w,<", reg, reg, 16);
d6bc6245
TS
3876 macro_build (p, counter, ep, "daddiu", "t,r,j",
3877 reg, reg, (int) BFD_RELOC_LO16);
3878 }
3879 }
252b5132
RH
3880 else
3881 {
d6bc6245
TS
3882 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3883 && ! nopic_need_relax (ep->X_add_symbol, 1))
3884 {
3885 frag_grow (20);
3886 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3887 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3888 reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
d6bc6245
TS
3889 p = frag_var (rs_machine_dependent, 8, 0,
3890 RELAX_ENCODE (4, 8, 0, 4, 0,
3891 mips_opts.warn_about_macros),
956cd1d6 3892 ep->X_add_symbol, 0, NULL);
d6bc6245
TS
3893 }
3894 macro_build_lui (p, counter, ep, reg);
3895 if (p != NULL)
3896 p += 4;
c9914766
TS
3897 macro_build (p, counter, ep,
3898 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
d6bc6245
TS
3899 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3900 }
252b5132
RH
3901 }
3902 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3903 {
3904 expressionS ex;
3905
3906 /* If this is a reference to an external symbol, we want
3907 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3908 Otherwise we want
3909 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3910 nop
3911 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
ed6fb7bd
SC
3912 If we have NewABI, we want
3913 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
252b5132
RH
3914 If there is a constant, it must be added in after. */
3915 ex.X_add_number = ep->X_add_number;
3916 ep->X_add_number = 0;
3917 frag_grow (20);
ed6fb7bd
SC
3918 if (HAVE_NEWABI)
3919 {
3920 macro_build ((char *) NULL, counter, ep,
3921 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3922 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3923 }
3924 else
3925 {
3926 macro_build ((char *) NULL, counter, ep,
3927 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3928 reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3929 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3930 p = frag_var (rs_machine_dependent, 4, 0,
3931 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3932 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3933 macro_build (p, counter, ep,
3934 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3935 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3936 }
3937
252b5132
RH
3938 if (ex.X_add_number != 0)
3939 {
3940 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3941 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3942 ex.X_op = O_constant;
3943 macro_build ((char *) NULL, counter, &ex,
ca4e0257 3944 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3945 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3946 }
3947 }
3948 else if (mips_pic == SVR4_PIC)
3949 {
3950 expressionS ex;
3951 int off;
3952
3953 /* This is the large GOT case. If this is a reference to an
3954 external symbol, we want
3955 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3956 addu $reg,$reg,$gp
3957 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3958 Otherwise, for a reference to a local symbol, we want
3959 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3960 nop
3961 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
438c16b8
TS
3962 If we have NewABI, we want
3963 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3964 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
252b5132
RH
3965 If there is a constant, it must be added in after. */
3966 ex.X_add_number = ep->X_add_number;
3967 ep->X_add_number = 0;
438c16b8
TS
3968 if (HAVE_NEWABI)
3969 {
3970 macro_build ((char *) NULL, counter, ep,
3971 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3972 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3973 macro_build (p, counter, ep,
3974 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3975 reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3976 }
252b5132 3977 else
438c16b8
TS
3978 {
3979 if (reg_needs_delay (mips_gp_register))
3980 off = 4;
3981 else
3982 off = 0;
3983 frag_grow (32);
3984 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3985 (int) BFD_RELOC_MIPS_GOT_HI16);
3986 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3987 HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3988 reg, mips_gp_register);
3989 macro_build ((char *) NULL, counter, ep,
3990 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3991 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3992 p = frag_var (rs_machine_dependent, 12 + off, 0,
3993 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3994 mips_opts.warn_about_macros),
3995 ep->X_add_symbol, 0, NULL);
3996 if (off > 0)
3997 {
3998 /* We need a nop before loading from $gp. This special
3999 check is required because the lui which starts the main
4000 instruction stream does not refer to $gp, and so will not
4001 insert the nop which may be required. */
4002 macro_build (p, counter, (expressionS *) NULL, "nop", "");
4003 p += 4;
4004 }
4005 macro_build (p, counter, ep,
4006 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
4007 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
4008 p += 4;
252b5132
RH
4009 macro_build (p, counter, (expressionS *) NULL, "nop", "");
4010 p += 4;
438c16b8
TS
4011 macro_build (p, counter, ep,
4012 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4013 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
252b5132 4014 }
438c16b8 4015
252b5132
RH
4016 if (ex.X_add_number != 0)
4017 {
4018 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4019 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4020 ex.X_op = O_constant;
f7ea7ef2
TS
4021 macro_build ((char *) NULL, counter, &ex,
4022 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4023 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4024 }
4025 }
4026 else if (mips_pic == EMBEDDED_PIC)
4027 {
4028 /* We always do
cdf6fd85 4029 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766
TS
4030 */
4031 macro_build ((char *) NULL, counter, ep,
4032 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4033 "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
4034 }
4035 else
4036 abort ();
4037}
4038
ea1fb5dc
RS
4039/* Move the contents of register SOURCE into register DEST. */
4040
4041static void
4042move_register (counter, dest, source)
4043 int *counter;
4044 int dest;
4045 int source;
4046{
4047 macro_build ((char *) NULL, counter, (expressionS *) NULL,
4048 HAVE_32BIT_GPRS ? "addu" : "daddu",
4049 "d,v,t", dest, source, 0);
4050}
4051
252b5132
RH
4052/*
4053 * Build macros
4054 * This routine implements the seemingly endless macro or synthesized
4055 * instructions and addressing modes in the mips assembly language. Many
4056 * of these macros are simple and are similar to each other. These could
4057 * probably be handled by some kind of table or grammer aproach instead of
4058 * this verbose method. Others are not simple macros but are more like
4059 * optimizing code generation.
4060 * One interesting optimization is when several store macros appear
4061 * consecutivly that would load AT with the upper half of the same address.
4062 * The ensuing load upper instructions are ommited. This implies some kind
4063 * of global optimization. We currently only optimize within a single macro.
4064 * For many of the load and store macros if the address is specified as a
4065 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4066 * first load register 'at' with zero and use it as the base register. The
4067 * mips assembler simply uses register $zero. Just one tiny optimization
4068 * we're missing.
4069 */
4070static void
4071macro (ip)
4072 struct mips_cl_insn *ip;
4073{
4074 register int treg, sreg, dreg, breg;
4075 int tempreg;
4076 int mask;
4077 int icnt = 0;
43841e91 4078 int used_at = 0;
252b5132
RH
4079 expressionS expr1;
4080 const char *s;
4081 const char *s2;
4082 const char *fmt;
4083 int likely = 0;
4084 int dbl = 0;
4085 int coproc = 0;
4086 int lr = 0;
4087 int imm = 0;
4088 offsetT maxnum;
4089 int off;
4090 bfd_reloc_code_real_type r;
252b5132
RH
4091 int hold_mips_optimize;
4092
4093 assert (! mips_opts.mips16);
4094
4095 treg = (ip->insn_opcode >> 16) & 0x1f;
4096 dreg = (ip->insn_opcode >> 11) & 0x1f;
4097 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4098 mask = ip->insn_mo->mask;
4099
4100 expr1.X_op = O_constant;
4101 expr1.X_op_symbol = NULL;
4102 expr1.X_add_symbol = NULL;
4103 expr1.X_add_number = 1;
4104
4105 switch (mask)
4106 {
4107 case M_DABS:
4108 dbl = 1;
4109 case M_ABS:
4110 /* bgez $a0,.+12
4111 move v0,$a0
4112 sub v0,$zero,$a0
4113 */
4114
b34976b6 4115 mips_emit_delays (TRUE);
252b5132
RH
4116 ++mips_opts.noreorder;
4117 mips_any_noreorder = 1;
4118
4119 expr1.X_add_number = 8;
4120 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4121 if (dreg == sreg)
2396cfb9
TS
4122 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4123 0);
252b5132 4124 else
ea1fb5dc 4125 move_register (&icnt, dreg, sreg);
2396cfb9 4126 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4127 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
4128
4129 --mips_opts.noreorder;
4130 return;
4131
4132 case M_ADD_I:
4133 s = "addi";
4134 s2 = "add";
4135 goto do_addi;
4136 case M_ADDU_I:
4137 s = "addiu";
4138 s2 = "addu";
4139 goto do_addi;
4140 case M_DADD_I:
4141 dbl = 1;
4142 s = "daddi";
4143 s2 = "dadd";
4144 goto do_addi;
4145 case M_DADDU_I:
4146 dbl = 1;
4147 s = "daddiu";
4148 s2 = "daddu";
4149 do_addi:
4150 if (imm_expr.X_op == O_constant
4151 && imm_expr.X_add_number >= -0x8000
4152 && imm_expr.X_add_number < 0x8000)
4153 {
4154 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4155 (int) BFD_RELOC_LO16);
4156 return;
4157 }
4158 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
4159 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4160 treg, sreg, AT);
252b5132
RH
4161 break;
4162
4163 case M_AND_I:
4164 s = "andi";
4165 s2 = "and";
4166 goto do_bit;
4167 case M_OR_I:
4168 s = "ori";
4169 s2 = "or";
4170 goto do_bit;
4171 case M_NOR_I:
4172 s = "";
4173 s2 = "nor";
4174 goto do_bit;
4175 case M_XOR_I:
4176 s = "xori";
4177 s2 = "xor";
4178 do_bit:
4179 if (imm_expr.X_op == O_constant
4180 && imm_expr.X_add_number >= 0
4181 && imm_expr.X_add_number < 0x10000)
4182 {
4183 if (mask != M_NOR_I)
4184 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4185 sreg, (int) BFD_RELOC_LO16);
4186 else
4187 {
4188 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4189 treg, sreg, (int) BFD_RELOC_LO16);
2396cfb9
TS
4190 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4191 "d,v,t", treg, treg, 0);
252b5132
RH
4192 }
4193 return;
4194 }
4195
d6bc6245 4196 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
4197 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4198 treg, sreg, AT);
252b5132
RH
4199 break;
4200
4201 case M_BEQ_I:
4202 s = "beq";
4203 goto beq_i;
4204 case M_BEQL_I:
4205 s = "beql";
4206 likely = 1;
4207 goto beq_i;
4208 case M_BNE_I:
4209 s = "bne";
4210 goto beq_i;
4211 case M_BNEL_I:
4212 s = "bnel";
4213 likely = 1;
4214 beq_i:
4215 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4216 {
4217 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4218 0);
4219 return;
4220 }
4d34fb5f 4221 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
252b5132
RH
4222 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4223 break;
4224
4225 case M_BGEL:
4226 likely = 1;
4227 case M_BGE:
4228 if (treg == 0)
4229 {
4230 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 4231 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4232 return;
4233 }
4234 if (sreg == 0)
4235 {
4236 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 4237 likely ? "blezl" : "blez", "s,p", treg);
252b5132
RH
4238 return;
4239 }
2396cfb9
TS
4240 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4241 AT, sreg, treg);
252b5132 4242 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4243 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4244 break;
4245
4246 case M_BGTL_I:
4247 likely = 1;
4248 case M_BGT_I:
4249 /* check for > max integer */
4250 maxnum = 0x7fffffff;
ca4e0257 4251 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4252 {
4253 maxnum <<= 16;
4254 maxnum |= 0xffff;
4255 maxnum <<= 16;
4256 maxnum |= 0xffff;
4257 }
4258 if (imm_expr.X_op == O_constant
4259 && imm_expr.X_add_number >= maxnum
ca4e0257 4260 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4261 {
4262 do_false:
4263 /* result is always false */
4264 if (! likely)
4265 {
39c0a331
L
4266 if (warn_nops)
4267 as_warn (_("Branch %s is always false (nop)"),
4268 ip->insn_mo->name);
2396cfb9
TS
4269 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4270 "", 0);
252b5132
RH
4271 }
4272 else
4273 {
39c0a331
L
4274 if (warn_nops)
4275 as_warn (_("Branch likely %s is always false"),
4276 ip->insn_mo->name);
252b5132
RH
4277 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4278 "s,t,p", 0, 0);
4279 }
4280 return;
4281 }
4282 if (imm_expr.X_op != O_constant)
4283 as_bad (_("Unsupported large constant"));
f9419b05 4284 ++imm_expr.X_add_number;
252b5132
RH
4285 /* FALLTHROUGH */
4286 case M_BGE_I:
4287 case M_BGEL_I:
4288 if (mask == M_BGEL_I)
4289 likely = 1;
4290 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4291 {
4292 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4293 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4294 return;
4295 }
4296 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4297 {
4298 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4299 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4300 return;
4301 }
4302 maxnum = 0x7fffffff;
ca4e0257 4303 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4304 {
4305 maxnum <<= 16;
4306 maxnum |= 0xffff;
4307 maxnum <<= 16;
4308 maxnum |= 0xffff;
4309 }
4310 maxnum = - maxnum - 1;
4311 if (imm_expr.X_op == O_constant
4312 && imm_expr.X_add_number <= maxnum
ca4e0257 4313 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4314 {
4315 do_true:
4316 /* result is always true */
4317 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4318 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4319 return;
4320 }
4321 set_at (&icnt, sreg, 0);
4322 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4323 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4324 break;
4325
4326 case M_BGEUL:
4327 likely = 1;
4328 case M_BGEU:
4329 if (treg == 0)
4330 goto do_true;
4331 if (sreg == 0)
4332 {
4333 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4334 likely ? "beql" : "beq", "s,t,p", 0, treg);
252b5132
RH
4335 return;
4336 }
2396cfb9
TS
4337 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4338 "d,v,t", AT, sreg, treg);
252b5132 4339 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4340 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4341 break;
4342
4343 case M_BGTUL_I:
4344 likely = 1;
4345 case M_BGTU_I:
4346 if (sreg == 0
ca4e0257 4347 || (HAVE_32BIT_GPRS
252b5132 4348 && imm_expr.X_op == O_constant
956cd1d6 4349 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4350 goto do_false;
4351 if (imm_expr.X_op != O_constant)
4352 as_bad (_("Unsupported large constant"));
f9419b05 4353 ++imm_expr.X_add_number;
252b5132
RH
4354 /* FALLTHROUGH */
4355 case M_BGEU_I:
4356 case M_BGEUL_I:
4357 if (mask == M_BGEUL_I)
4358 likely = 1;
4359 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4360 goto do_true;
4361 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4362 {
4363 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4364 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4365 return;
4366 }
4367 set_at (&icnt, sreg, 1);
4368 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4369 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4370 break;
4371
4372 case M_BGTL:
4373 likely = 1;
4374 case M_BGT:
4375 if (treg == 0)
4376 {
4377 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4378 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4379 return;
4380 }
4381 if (sreg == 0)
4382 {
4383 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4384 likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
4385 return;
4386 }
2396cfb9
TS
4387 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4388 AT, treg, sreg);
252b5132 4389 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4390 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4391 break;
4392
4393 case M_BGTUL:
4394 likely = 1;
4395 case M_BGTU:
4396 if (treg == 0)
4397 {
4398 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4399 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4400 return;
4401 }
4402 if (sreg == 0)
4403 goto do_false;
2396cfb9
TS
4404 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4405 "d,v,t", AT, treg, sreg);
252b5132 4406 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4407 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4408 break;
4409
4410 case M_BLEL:
4411 likely = 1;
4412 case M_BLE:
4413 if (treg == 0)
4414 {
4415 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4416 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4417 return;
4418 }
4419 if (sreg == 0)
4420 {
4421 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4422 likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4423 return;
4424 }
2396cfb9
TS
4425 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4426 AT, treg, sreg);
252b5132 4427 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4428 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4429 break;
4430
4431 case M_BLEL_I:
4432 likely = 1;
4433 case M_BLE_I:
4434 maxnum = 0x7fffffff;
ca4e0257 4435 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4436 {
4437 maxnum <<= 16;
4438 maxnum |= 0xffff;
4439 maxnum <<= 16;
4440 maxnum |= 0xffff;
4441 }
4442 if (imm_expr.X_op == O_constant
4443 && imm_expr.X_add_number >= maxnum
ca4e0257 4444 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4445 goto do_true;
4446 if (imm_expr.X_op != O_constant)
4447 as_bad (_("Unsupported large constant"));
f9419b05 4448 ++imm_expr.X_add_number;
252b5132
RH
4449 /* FALLTHROUGH */
4450 case M_BLT_I:
4451 case M_BLTL_I:
4452 if (mask == M_BLTL_I)
4453 likely = 1;
4454 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4455 {
4456 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4457 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4458 return;
4459 }
4460 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4461 {
4462 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4463 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4464 return;
4465 }
4466 set_at (&icnt, sreg, 0);
4467 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4468 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4469 break;
4470
4471 case M_BLEUL:
4472 likely = 1;
4473 case M_BLEU:
4474 if (treg == 0)
4475 {
4476 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4477 likely ? "beql" : "beq", "s,t,p", sreg, 0);
252b5132
RH
4478 return;
4479 }
4480 if (sreg == 0)
4481 goto do_true;
2396cfb9
TS
4482 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4483 "d,v,t", AT, treg, sreg);
252b5132 4484 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4485 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4486 break;
4487
4488 case M_BLEUL_I:
4489 likely = 1;
4490 case M_BLEU_I:
4491 if (sreg == 0
ca4e0257 4492 || (HAVE_32BIT_GPRS
252b5132 4493 && imm_expr.X_op == O_constant
956cd1d6 4494 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4495 goto do_true;
4496 if (imm_expr.X_op != O_constant)
4497 as_bad (_("Unsupported large constant"));
f9419b05 4498 ++imm_expr.X_add_number;
252b5132
RH
4499 /* FALLTHROUGH */
4500 case M_BLTU_I:
4501 case M_BLTUL_I:
4502 if (mask == M_BLTUL_I)
4503 likely = 1;
4504 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4505 goto do_false;
4506 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4507 {
4508 macro_build ((char *) NULL, &icnt, &offset_expr,
4509 likely ? "beql" : "beq",
4510 "s,t,p", sreg, 0);
4511 return;
4512 }
4513 set_at (&icnt, sreg, 1);
4514 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4515 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4516 break;
4517
4518 case M_BLTL:
4519 likely = 1;
4520 case M_BLT:
4521 if (treg == 0)
4522 {
4523 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4524 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4525 return;
4526 }
4527 if (sreg == 0)
4528 {
4529 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4530 likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4531 return;
4532 }
2396cfb9
TS
4533 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4534 AT, sreg, treg);
252b5132 4535 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4536 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4537 break;
4538
4539 case M_BLTUL:
4540 likely = 1;
4541 case M_BLTU:
4542 if (treg == 0)
4543 goto do_false;
4544 if (sreg == 0)
4545 {
4546 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4547 likely ? "bnel" : "bne", "s,t,p", 0, treg);
252b5132
RH
4548 return;
4549 }
2396cfb9
TS
4550 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4551 "d,v,t", AT, sreg,
252b5132
RH
4552 treg);
4553 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4554 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4555 break;
4556
4557 case M_DDIV_3:
4558 dbl = 1;
4559 case M_DIV_3:
4560 s = "mflo";
4561 goto do_div3;
4562 case M_DREM_3:
4563 dbl = 1;
4564 case M_REM_3:
4565 s = "mfhi";
4566 do_div3:
4567 if (treg == 0)
4568 {
4569 as_warn (_("Divide by zero."));
4570 if (mips_trap)
2396cfb9 4571 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4572 "s,t,q", 0, 0, 7);
252b5132 4573 else
2396cfb9
TS
4574 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4575 "c", 7);
252b5132
RH
4576 return;
4577 }
4578
b34976b6 4579 mips_emit_delays (TRUE);
252b5132
RH
4580 ++mips_opts.noreorder;
4581 mips_any_noreorder = 1;
4582 if (mips_trap)
4583 {
2396cfb9 4584 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4585 "s,t,q", treg, 0, 7);
2396cfb9 4586 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4587 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4588 }
4589 else
4590 {
4591 expr1.X_add_number = 8;
4592 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9 4593 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4594 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
2396cfb9
TS
4595 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4596 "c", 7);
252b5132
RH
4597 }
4598 expr1.X_add_number = -1;
4599 macro_build ((char *) NULL, &icnt, &expr1,
4600 dbl ? "daddiu" : "addiu",
4601 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4602 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4603 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4604 if (dbl)
4605 {
4606 expr1.X_add_number = 1;
4607 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4608 (int) BFD_RELOC_LO16);
2396cfb9
TS
4609 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4610 "d,w,<", AT, AT, 31);
252b5132
RH
4611 }
4612 else
4613 {
4614 expr1.X_add_number = 0x80000000;
4615 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4616 (int) BFD_RELOC_HI16);
4617 }
4618 if (mips_trap)
4619 {
2396cfb9 4620 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4621 "s,t,q", sreg, AT, 6);
252b5132
RH
4622 /* We want to close the noreorder block as soon as possible, so
4623 that later insns are available for delay slot filling. */
4624 --mips_opts.noreorder;
4625 }
4626 else
4627 {
4628 expr1.X_add_number = 8;
4629 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
2396cfb9
TS
4630 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4631 0);
252b5132
RH
4632
4633 /* We want to close the noreorder block as soon as possible, so
4634 that later insns are available for delay slot filling. */
4635 --mips_opts.noreorder;
4636
2396cfb9
TS
4637 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4638 "c", 6);
252b5132 4639 }
2396cfb9 4640 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
252b5132
RH
4641 break;
4642
4643 case M_DIV_3I:
4644 s = "div";
4645 s2 = "mflo";
4646 goto do_divi;
4647 case M_DIVU_3I:
4648 s = "divu";
4649 s2 = "mflo";
4650 goto do_divi;
4651 case M_REM_3I:
4652 s = "div";
4653 s2 = "mfhi";
4654 goto do_divi;
4655 case M_REMU_3I:
4656 s = "divu";
4657 s2 = "mfhi";
4658 goto do_divi;
4659 case M_DDIV_3I:
4660 dbl = 1;
4661 s = "ddiv";
4662 s2 = "mflo";
4663 goto do_divi;
4664 case M_DDIVU_3I:
4665 dbl = 1;
4666 s = "ddivu";
4667 s2 = "mflo";
4668 goto do_divi;
4669 case M_DREM_3I:
4670 dbl = 1;
4671 s = "ddiv";
4672 s2 = "mfhi";
4673 goto do_divi;
4674 case M_DREMU_3I:
4675 dbl = 1;
4676 s = "ddivu";
4677 s2 = "mfhi";
4678 do_divi:
4679 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4680 {
4681 as_warn (_("Divide by zero."));
4682 if (mips_trap)
2396cfb9 4683 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4684 "s,t,q", 0, 0, 7);
252b5132 4685 else
2396cfb9
TS
4686 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4687 "c", 7);
252b5132
RH
4688 return;
4689 }
4690 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4691 {
4692 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4693 move_register (&icnt, dreg, sreg);
252b5132 4694 else
ea1fb5dc 4695 move_register (&icnt, dreg, 0);
252b5132
RH
4696 return;
4697 }
4698 if (imm_expr.X_op == O_constant
4699 && imm_expr.X_add_number == -1
4700 && s[strlen (s) - 1] != 'u')
4701 {
4702 if (strcmp (s2, "mflo") == 0)
4703 {
2396cfb9
TS
4704 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4705 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4706 }
4707 else
ea1fb5dc 4708 move_register (&icnt, dreg, 0);
252b5132
RH
4709 return;
4710 }
4711
4712 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
4713 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4714 sreg, AT);
4715 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4716 break;
4717
4718 case M_DIVU_3:
4719 s = "divu";
4720 s2 = "mflo";
4721 goto do_divu3;
4722 case M_REMU_3:
4723 s = "divu";
4724 s2 = "mfhi";
4725 goto do_divu3;
4726 case M_DDIVU_3:
4727 s = "ddivu";
4728 s2 = "mflo";
4729 goto do_divu3;
4730 case M_DREMU_3:
4731 s = "ddivu";
4732 s2 = "mfhi";
4733 do_divu3:
b34976b6 4734 mips_emit_delays (TRUE);
252b5132
RH
4735 ++mips_opts.noreorder;
4736 mips_any_noreorder = 1;
4737 if (mips_trap)
4738 {
2396cfb9 4739 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4740 "s,t,q", treg, 0, 7);
2396cfb9
TS
4741 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4742 sreg, treg);
252b5132
RH
4743 /* We want to close the noreorder block as soon as possible, so
4744 that later insns are available for delay slot filling. */
4745 --mips_opts.noreorder;
4746 }
4747 else
4748 {
4749 expr1.X_add_number = 8;
4750 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9
TS
4751 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4752 sreg, treg);
252b5132
RH
4753
4754 /* We want to close the noreorder block as soon as possible, so
4755 that later insns are available for delay slot filling. */
4756 --mips_opts.noreorder;
2396cfb9
TS
4757 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4758 "c", 7);
252b5132 4759 }
2396cfb9 4760 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4761 return;
4762
4763 case M_DLA_AB:
4764 dbl = 1;
4765 case M_LA_AB:
4766 /* Load the address of a symbol into a register. If breg is not
4767 zero, we then add a base register to it. */
4768
3bec30a8
TS
4769 if (dbl && HAVE_32BIT_GPRS)
4770 as_warn (_("dla used to load 32-bit register"));
4771
c90bbe5b 4772 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4773 as_warn (_("la used to load 64-bit address"));
4774
0c11417f
MR
4775 if (offset_expr.X_op == O_constant
4776 && offset_expr.X_add_number >= -0x8000
4777 && offset_expr.X_add_number < 0x8000)
4778 {
4779 macro_build ((char *) NULL, &icnt, &offset_expr,
4780 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4781 "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4782 return;
4783 }
4784
afdbd6d0
CD
4785 if (treg == breg)
4786 {
4787 tempreg = AT;
4788 used_at = 1;
4789 }
4790 else
4791 {
4792 tempreg = treg;
4793 used_at = 0;
4794 }
4795
252b5132
RH
4796 /* When generating embedded PIC code, we permit expressions of
4797 the form
afdbd6d0
CD
4798 la $treg,foo-bar
4799 la $treg,foo-bar($breg)
bb2d6cd7 4800 where bar is an address in the current section. These are used
252b5132
RH
4801 when getting the addresses of functions. We don't permit
4802 X_add_number to be non-zero, because if the symbol is
4803 external the relaxing code needs to know that any addend is
4804 purely the offset to X_op_symbol. */
4805 if (mips_pic == EMBEDDED_PIC
4806 && offset_expr.X_op == O_subtract
49309057 4807 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4808 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4809 : (symbol_equated_p (offset_expr.X_op_symbol)
4810 && (S_GET_SEGMENT
4811 (symbol_get_value_expression (offset_expr.X_op_symbol)
4812 ->X_add_symbol)
bb2d6cd7 4813 == now_seg)))
bb2d6cd7
GK
4814 && (offset_expr.X_add_number == 0
4815 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4816 {
afdbd6d0
CD
4817 if (breg == 0)
4818 {
4819 tempreg = treg;
4820 used_at = 0;
4821 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4822 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4823 }
4824 else
4825 {
4826 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4827 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4828 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4d34fb5f 4829 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4830 "d,v,t", tempreg, tempreg, breg);
4831 }
252b5132 4832 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4833 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
afdbd6d0
CD
4834 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4835 if (! used_at)
4836 return;
4837 break;
252b5132
RH
4838 }
4839
4840 if (offset_expr.X_op != O_symbol
4841 && offset_expr.X_op != O_constant)
4842 {
4843 as_bad (_("expression too complex"));
4844 offset_expr.X_op = O_constant;
4845 }
4846
252b5132 4847 if (offset_expr.X_op == O_constant)
4d34fb5f
TS
4848 load_register (&icnt, tempreg, &offset_expr,
4849 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4850 ? (dbl || HAVE_64BIT_ADDRESSES)
4851 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4852 else if (mips_pic == NO_PIC)
4853 {
d6bc6245 4854 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4855 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4856 Otherwise we want
4857 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4858 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4859 If we have a constant, we need two instructions anyhow,
d6bc6245 4860 so we may as well always use the latter form.
76b3015f 4861
d6bc6245
TS
4862 With 64bit address space and a usable $at we want
4863 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4864 lui $at,<sym> (BFD_RELOC_HI16_S)
4865 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4866 daddiu $at,<sym> (BFD_RELOC_LO16)
4867 dsll32 $tempreg,0
3a482fd5 4868 daddu $tempreg,$tempreg,$at
76b3015f 4869
c03099e6 4870 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
4871 on superscalar processors.
4872 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4873 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4874 dsll $tempreg,16
4875 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4876 dsll $tempreg,16
4877 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4878 */
f9419b05 4879 char *p = NULL;
d6bc6245 4880 if (HAVE_64BIT_ADDRESSES)
252b5132 4881 {
d6bc6245
TS
4882 /* We don't do GP optimization for now because RELAX_ENCODE can't
4883 hold the data for such large chunks. */
4884
460597ba 4885 if (used_at == 0 && ! mips_opts.noat)
98d3f06f
KH
4886 {
4887 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4888 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4889 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4890 AT, (int) BFD_RELOC_HI16_S);
4891 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4892 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4893 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4894 AT, AT, (int) BFD_RELOC_LO16);
4895 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4896 "d,w,<", tempreg, tempreg, 0);
3a482fd5
MR
4897 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4898 "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
4899 used_at = 1;
4900 }
4901 else
4902 {
4903 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4904 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4905 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4906 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4907 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4908 tempreg, tempreg, 16);
4909 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4910 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4911 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4912 tempreg, tempreg, 16);
4913 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4914 tempreg, tempreg, (int) BFD_RELOC_LO16);
4915 }
4916 }
4917 else
4918 {
4919 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4920 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4921 {
4922 frag_grow (20);
4923 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
c9914766
TS
4924 "t,r,j", tempreg, mips_gp_register,
4925 (int) BFD_RELOC_GPREL16);
98d3f06f
KH
4926 p = frag_var (rs_machine_dependent, 8, 0,
4927 RELAX_ENCODE (4, 8, 0, 4, 0,
4928 mips_opts.warn_about_macros),
4929 offset_expr.X_add_symbol, 0, NULL);
4930 }
4931 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4932 if (p != NULL)
4933 p += 4;
4934 macro_build (p, &icnt, &offset_expr, "addiu",
4935 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4936 }
252b5132
RH
4937 }
4938 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4939 {
9117d219
NC
4940 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4941
252b5132
RH
4942 /* If this is a reference to an external symbol, and there
4943 is no constant, we want
4944 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9117d219
NC
4945 or if tempreg is PIC_CALL_REG
4946 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
4947 For a local symbol, we want
4948 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4949 nop
4950 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4951
4952 If we have a small constant, and this is a reference to
4953 an external symbol, we want
4954 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4955 nop
4956 addiu $tempreg,$tempreg,<constant>
4957 For a local symbol, we want the same instruction
4958 sequence, but we output a BFD_RELOC_LO16 reloc on the
4959 addiu instruction.
4960
4961 If we have a large constant, and this is a reference to
4962 an external symbol, we want
4963 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4964 lui $at,<hiconstant>
4965 addiu $at,$at,<loconstant>
4966 addu $tempreg,$tempreg,$at
4967 For a local symbol, we want the same instruction
4968 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd
SC
4969 addiu instruction.
4970
4971 For NewABI, we want for local or external data addresses
4972 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4973 For a local function symbol, we want
4974 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4975 nop
4976 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4977 */
4978
252b5132
RH
4979 expr1.X_add_number = offset_expr.X_add_number;
4980 offset_expr.X_add_number = 0;
4981 frag_grow (32);
9117d219
NC
4982 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4983 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
ed6fb7bd
SC
4984 else if (HAVE_NEWABI)
4985 lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4d34fb5f
TS
4986 macro_build ((char *) NULL, &icnt, &offset_expr,
4987 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766 4988 "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
252b5132
RH
4989 if (expr1.X_add_number == 0)
4990 {
4991 int off;
f9419b05 4992 char *p;
252b5132
RH
4993
4994 if (breg == 0)
4995 off = 0;
4996 else
4997 {
4998 /* We're going to put in an addu instruction using
4999 tempreg, so we may as well insert the nop right
5000 now. */
5001 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5002 "nop", "");
5003 off = 4;
5004 }
5005 p = frag_var (rs_machine_dependent, 8 - off, 0,
5006 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5007 (breg == 0
5008 ? mips_opts.warn_about_macros
5009 : 0)),
c4e7957c 5010 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5011 if (breg == 0)
5012 {
5013 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5014 p += 4;
5015 }
5016 macro_build (p, &icnt, &expr1,
ca4e0257 5017 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5018 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5019 /* FIXME: If breg == 0, and the next instruction uses
5020 $tempreg, then if this variant case is used an extra
5021 nop will be generated. */
5022 }
5023 else if (expr1.X_add_number >= -0x8000
5024 && expr1.X_add_number < 0x8000)
5025 {
5026 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5027 "nop", "");
5028 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 5029 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132 5030 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
c4e7957c
TS
5031 frag_var (rs_machine_dependent, 0, 0,
5032 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5033 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5034 }
5035 else
5036 {
5037 int off1;
5038
5039 /* If we are going to add in a base register, and the
5040 target register and the base register are the same,
5041 then we are using AT as a temporary register. Since
5042 we want to load the constant into AT, we add our
5043 current AT (from the global offset table) and the
5044 register into the register now, and pretend we were
5045 not using a base register. */
5046 if (breg != treg)
5047 off1 = 0;
5048 else
5049 {
5050 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5051 "nop", "");
5052 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5053 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5054 "d,v,t", treg, AT, breg);
5055 breg = 0;
5056 tempreg = treg;
5057 off1 = -8;
5058 }
5059
5060 /* Set mips_optimize around the lui instruction to avoid
5061 inserting an unnecessary nop after the lw. */
5062 hold_mips_optimize = mips_optimize;
5063 mips_optimize = 2;
c4e7957c 5064 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
5065 mips_optimize = hold_mips_optimize;
5066
5067 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 5068 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5069 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5070 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5071 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 5072 "d,v,t", tempreg, tempreg, AT);
c4e7957c
TS
5073 frag_var (rs_machine_dependent, 0, 0,
5074 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5075 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5076 used_at = 1;
5077 }
5078 }
5079 else if (mips_pic == SVR4_PIC)
5080 {
5081 int gpdel;
f9419b05 5082 char *p;
9117d219
NC
5083 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5084 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5085 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5086
5087 /* This is the large GOT case. If this is a reference to an
5088 external symbol, and there is no constant, we want
5089 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5090 addu $tempreg,$tempreg,$gp
5091 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9117d219
NC
5092 or if tempreg is PIC_CALL_REG
5093 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5094 addu $tempreg,$tempreg,$gp
5095 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5096 For a local symbol, we want
5097 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5098 nop
5099 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5100
5101 If we have a small constant, and this is a reference to
5102 an external symbol, we want
5103 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5104 addu $tempreg,$tempreg,$gp
5105 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5106 nop
5107 addiu $tempreg,$tempreg,<constant>
5108 For a local symbol, we want
5109 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5110 nop
5111 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5112
5113 If we have a large constant, and this is a reference to
5114 an external symbol, we want
5115 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5116 addu $tempreg,$tempreg,$gp
5117 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5118 lui $at,<hiconstant>
5119 addiu $at,$at,<loconstant>
5120 addu $tempreg,$tempreg,$at
5121 For a local symbol, we want
5122 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5123 lui $at,<hiconstant>
5124 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5125 addu $tempreg,$tempreg,$at
438c16b8 5126
ed6fb7bd
SC
5127 For NewABI, we want for local data addresses
5128 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
438c16b8 5129 */
438c16b8 5130
252b5132
RH
5131 expr1.X_add_number = offset_expr.X_add_number;
5132 offset_expr.X_add_number = 0;
5133 frag_grow (52);
f7ea7ef2 5134 if (reg_needs_delay (mips_gp_register))
252b5132
RH
5135 gpdel = 4;
5136 else
5137 gpdel = 0;
9117d219
NC
5138 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5139 {
5140 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5141 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5142 }
252b5132 5143 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 5144 tempreg, lui_reloc_type);
252b5132 5145 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5146 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5147 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 5148 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 5149 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
9117d219 5150 "t,o(b)", tempreg, lw_reloc_type, tempreg);
252b5132
RH
5151 if (expr1.X_add_number == 0)
5152 {
5153 int off;
5154
5155 if (breg == 0)
5156 off = 0;
5157 else
5158 {
5159 /* We're going to put in an addu instruction using
5160 tempreg, so we may as well insert the nop right
5161 now. */
5162 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5163 "nop", "");
5164 off = 4;
5165 }
5166
5167 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5168 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5169 8 + gpdel, 0,
5170 (breg == 0
5171 ? mips_opts.warn_about_macros
5172 : 0)),
c4e7957c 5173 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5174 }
5175 else if (expr1.X_add_number >= -0x8000
5176 && expr1.X_add_number < 0x8000)
5177 {
5178 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5179 "nop", "");
5180 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 5181 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5182 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5183
5184 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5185 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5186 (breg == 0
5187 ? mips_opts.warn_about_macros
5188 : 0)),
c4e7957c 5189 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5190 }
5191 else
5192 {
5193 int adj, dreg;
5194
5195 /* If we are going to add in a base register, and the
5196 target register and the base register are the same,
5197 then we are using AT as a temporary register. Since
5198 we want to load the constant into AT, we add our
5199 current AT (from the global offset table) and the
5200 register into the register now, and pretend we were
5201 not using a base register. */
5202 if (breg != treg)
5203 {
5204 adj = 0;
5205 dreg = tempreg;
5206 }
5207 else
5208 {
5209 assert (tempreg == AT);
5210 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5211 "nop", "");
5212 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5213 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5214 "d,v,t", treg, AT, breg);
5215 dreg = treg;
5216 adj = 8;
5217 }
5218
5219 /* Set mips_optimize around the lui instruction to avoid
5220 inserting an unnecessary nop after the lw. */
5221 hold_mips_optimize = mips_optimize;
5222 mips_optimize = 2;
c4e7957c 5223 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
5224 mips_optimize = hold_mips_optimize;
5225
5226 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 5227 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5228 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5229 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5230 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5231 "d,v,t", dreg, dreg, AT);
5232
5233 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5234 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5235 8 + gpdel, 0,
5236 (breg == 0
5237 ? mips_opts.warn_about_macros
5238 : 0)),
c4e7957c 5239 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5240
5241 used_at = 1;
5242 }
5243
5244 if (gpdel > 0)
5245 {
5246 /* This is needed because this instruction uses $gp, but
5247 the first instruction on the main stream does not. */
5248 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5249 p += 4;
5250 }
ed6fb7bd
SC
5251
5252 if (HAVE_NEWABI)
5253 local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
252b5132 5254 macro_build (p, &icnt, &offset_expr,
4d34fb5f 5255 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
ed6fb7bd
SC
5256 "t,o(b)", tempreg,
5257 local_reloc_type,
c9914766 5258 mips_gp_register);
252b5132 5259 p += 4;
ed6fb7bd
SC
5260 if (expr1.X_add_number == 0 && HAVE_NEWABI)
5261 {
5262 /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
5263 }
5264 else
5265 if (expr1.X_add_number >= -0x8000
252b5132
RH
5266 && expr1.X_add_number < 0x8000)
5267 {
5268 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5269 p += 4;
5270 macro_build (p, &icnt, &expr1,
ca4e0257 5271 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5272 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5273 /* FIXME: If add_number is 0, and there was no base
5274 register, the external symbol case ended with a load,
5275 so if the symbol turns out to not be external, and
5276 the next instruction uses tempreg, an unnecessary nop
5277 will be inserted. */
5278 }
5279 else
5280 {
5281 if (breg == treg)
5282 {
5283 /* We must add in the base register now, as in the
5284 external symbol case. */
5285 assert (tempreg == AT);
5286 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5287 p += 4;
5288 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5289 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5290 "d,v,t", treg, AT, breg);
5291 p += 4;
5292 tempreg = treg;
5293 /* We set breg to 0 because we have arranged to add
5294 it in in both cases. */
5295 breg = 0;
5296 }
5297
5298 macro_build_lui (p, &icnt, &expr1, AT);
5299 p += 4;
5300 macro_build (p, &icnt, &expr1,
ca4e0257 5301 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5302 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5303 p += 4;
5304 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5305 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5306 "d,v,t", tempreg, tempreg, AT);
5307 p += 4;
5308 }
5309 }
5310 else if (mips_pic == EMBEDDED_PIC)
5311 {
5312 /* We use
cdf6fd85 5313 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5314 */
5315 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5316 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5317 tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
5318 }
5319 else
5320 abort ();
5321
5322 if (breg != 0)
4d34fb5f
TS
5323 {
5324 char *s;
5325
5326 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5327 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5328 else
5329 s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5330
5331 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5332 "d,v,t", treg, tempreg, breg);
5333 }
252b5132
RH
5334
5335 if (! used_at)
5336 return;
5337
5338 break;
5339
5340 case M_J_A:
5341 /* The j instruction may not be used in PIC code, since it
5342 requires an absolute address. We convert it to a b
5343 instruction. */
5344 if (mips_pic == NO_PIC)
5345 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5346 else
5347 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5348 return;
5349
5350 /* The jal instructions must be handled as macros because when
5351 generating PIC code they expand to multi-instruction
5352 sequences. Normally they are simple instructions. */
5353 case M_JAL_1:
5354 dreg = RA;
5355 /* Fall through. */
5356 case M_JAL_2:
5357 if (mips_pic == NO_PIC
5358 || mips_pic == EMBEDDED_PIC)
5359 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5360 "d,s", dreg, sreg);
5361 else if (mips_pic == SVR4_PIC)
5362 {
5363 if (sreg != PIC_CALL_REG)
5364 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5365
252b5132
RH
5366 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5367 "d,s", dreg, sreg);
6478892d 5368 if (! HAVE_NEWABI)
252b5132 5369 {
6478892d
TS
5370 if (mips_cprestore_offset < 0)
5371 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5372 else
5373 {
7a621144
DJ
5374 if (! mips_frame_reg_valid)
5375 {
5376 as_warn (_("No .frame pseudo-op used in PIC code"));
5377 /* Quiet this warning. */
5378 mips_frame_reg_valid = 1;
5379 }
5380 if (! mips_cprestore_valid)
5381 {
5382 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5383 /* Quiet this warning. */
5384 mips_cprestore_valid = 1;
5385 }
6478892d 5386 expr1.X_add_number = mips_cprestore_offset;
885add95
CD
5387 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5388 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5389 mips_gp_register, mips_frame_reg);
6478892d 5390 }
252b5132
RH
5391 }
5392 }
5393 else
5394 abort ();
5395
5396 return;
5397
5398 case M_JAL_A:
5399 if (mips_pic == NO_PIC)
5400 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5401 else if (mips_pic == SVR4_PIC)
5402 {
f9419b05
TS
5403 char *p;
5404
252b5132
RH
5405 /* If this is a reference to an external symbol, and we are
5406 using a small GOT, we want
5407 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5408 nop
f9419b05 5409 jalr $ra,$25
252b5132
RH
5410 nop
5411 lw $gp,cprestore($sp)
5412 The cprestore value is set using the .cprestore
5413 pseudo-op. If we are using a big GOT, we want
5414 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5415 addu $25,$25,$gp
5416 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5417 nop
f9419b05 5418 jalr $ra,$25
252b5132
RH
5419 nop
5420 lw $gp,cprestore($sp)
5421 If the symbol is not external, we want
5422 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5423 nop
5424 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5425 jalr $ra,$25
252b5132 5426 nop
438c16b8
TS
5427 lw $gp,cprestore($sp)
5428 For NewABI, we want
5429 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5430 jalr $ra,$25 (BFD_RELOC_MIPS_JALR)
5431 */
5432 if (HAVE_NEWABI)
252b5132
RH
5433 {
5434 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5435 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132 5436 "t,o(b)", PIC_CALL_REG,
438c16b8
TS
5437 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5438 macro_build_jalr (icnt, &offset_expr);
252b5132
RH
5439 }
5440 else
5441 {
438c16b8
TS
5442 frag_grow (40);
5443 if (! mips_big_got)
5444 {
5445 macro_build ((char *) NULL, &icnt, &offset_expr,
5446 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5447 "t,o(b)", PIC_CALL_REG,
5448 (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5449 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5450 "nop", "");
5451 p = frag_var (rs_machine_dependent, 4, 0,
5452 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5453 offset_expr.X_add_symbol, 0, NULL);
5454 }
252b5132 5455 else
252b5132 5456 {
438c16b8
TS
5457 int gpdel;
5458
5459 if (reg_needs_delay (mips_gp_register))
5460 gpdel = 4;
5461 else
5462 gpdel = 0;
5463 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5464 "t,u", PIC_CALL_REG,
5465 (int) BFD_RELOC_MIPS_CALL_HI16);
5466 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5467 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5468 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5469 mips_gp_register);
5470 macro_build ((char *) NULL, &icnt, &offset_expr,
5471 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5472 "t,o(b)", PIC_CALL_REG,
5473 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5474 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5475 "nop", "");
5476 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5477 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5478 8 + gpdel, 0, 0),
5479 offset_expr.X_add_symbol, 0, NULL);
5480 if (gpdel > 0)
5481 {
5482 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5483 p += 4;
5484 }
5485 macro_build (p, &icnt, &offset_expr,
5486 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5487 "t,o(b)", PIC_CALL_REG,
5488 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5489 p += 4;
252b5132
RH
5490 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5491 p += 4;
5492 }
5493 macro_build (p, &icnt, &offset_expr,
438c16b8
TS
5494 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5495 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5496 (int) BFD_RELOC_LO16);
5497 macro_build_jalr (icnt, &offset_expr);
5498
6478892d
TS
5499 if (mips_cprestore_offset < 0)
5500 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5501 else
5502 {
7a621144
DJ
5503 if (! mips_frame_reg_valid)
5504 {
5505 as_warn (_("No .frame pseudo-op used in PIC code"));
5506 /* Quiet this warning. */
5507 mips_frame_reg_valid = 1;
5508 }
5509 if (! mips_cprestore_valid)
5510 {
5511 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5512 /* Quiet this warning. */
5513 mips_cprestore_valid = 1;
5514 }
6478892d
TS
5515 if (mips_opts.noreorder)
5516 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
98d3f06f 5517 "nop", "");
6478892d 5518 expr1.X_add_number = mips_cprestore_offset;
885add95
CD
5519 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5520 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5521 mips_gp_register, mips_frame_reg);
6478892d 5522 }
252b5132
RH
5523 }
5524 }
5525 else if (mips_pic == EMBEDDED_PIC)
5526 {
5527 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5528 /* The linker may expand the call to a longer sequence which
5529 uses $at, so we must break rather than return. */
5530 break;
5531 }
5532 else
5533 abort ();
5534
5535 return;
5536
5537 case M_LB_AB:
5538 s = "lb";
5539 goto ld;
5540 case M_LBU_AB:
5541 s = "lbu";
5542 goto ld;
5543 case M_LH_AB:
5544 s = "lh";
5545 goto ld;
5546 case M_LHU_AB:
5547 s = "lhu";
5548 goto ld;
5549 case M_LW_AB:
5550 s = "lw";
5551 goto ld;
5552 case M_LWC0_AB:
5553 s = "lwc0";
bdaaa2e1 5554 /* Itbl support may require additional care here. */
252b5132
RH
5555 coproc = 1;
5556 goto ld;
5557 case M_LWC1_AB:
5558 s = "lwc1";
bdaaa2e1 5559 /* Itbl support may require additional care here. */
252b5132
RH
5560 coproc = 1;
5561 goto ld;
5562 case M_LWC2_AB:
5563 s = "lwc2";
bdaaa2e1 5564 /* Itbl support may require additional care here. */
252b5132
RH
5565 coproc = 1;
5566 goto ld;
5567 case M_LWC3_AB:
5568 s = "lwc3";
bdaaa2e1 5569 /* Itbl support may require additional care here. */
252b5132
RH
5570 coproc = 1;
5571 goto ld;
5572 case M_LWL_AB:
5573 s = "lwl";
5574 lr = 1;
5575 goto ld;
5576 case M_LWR_AB:
5577 s = "lwr";
5578 lr = 1;
5579 goto ld;
5580 case M_LDC1_AB:
ec68c924 5581 if (mips_arch == CPU_R4650)
252b5132
RH
5582 {
5583 as_bad (_("opcode not supported on this processor"));
5584 return;
5585 }
5586 s = "ldc1";
bdaaa2e1 5587 /* Itbl support may require additional care here. */
252b5132
RH
5588 coproc = 1;
5589 goto ld;
5590 case M_LDC2_AB:
5591 s = "ldc2";
bdaaa2e1 5592 /* Itbl support may require additional care here. */
252b5132
RH
5593 coproc = 1;
5594 goto ld;
5595 case M_LDC3_AB:
5596 s = "ldc3";
bdaaa2e1 5597 /* Itbl support may require additional care here. */
252b5132
RH
5598 coproc = 1;
5599 goto ld;
5600 case M_LDL_AB:
5601 s = "ldl";
5602 lr = 1;
5603 goto ld;
5604 case M_LDR_AB:
5605 s = "ldr";
5606 lr = 1;
5607 goto ld;
5608 case M_LL_AB:
5609 s = "ll";
5610 goto ld;
5611 case M_LLD_AB:
5612 s = "lld";
5613 goto ld;
5614 case M_LWU_AB:
5615 s = "lwu";
5616 ld:
5617 if (breg == treg || coproc || lr)
5618 {
5619 tempreg = AT;
5620 used_at = 1;
5621 }
5622 else
5623 {
5624 tempreg = treg;
5625 used_at = 0;
5626 }
5627 goto ld_st;
5628 case M_SB_AB:
5629 s = "sb";
5630 goto st;
5631 case M_SH_AB:
5632 s = "sh";
5633 goto st;
5634 case M_SW_AB:
5635 s = "sw";
5636 goto st;
5637 case M_SWC0_AB:
5638 s = "swc0";
bdaaa2e1 5639 /* Itbl support may require additional care here. */
252b5132
RH
5640 coproc = 1;
5641 goto st;
5642 case M_SWC1_AB:
5643 s = "swc1";
bdaaa2e1 5644 /* Itbl support may require additional care here. */
252b5132
RH
5645 coproc = 1;
5646 goto st;
5647 case M_SWC2_AB:
5648 s = "swc2";
bdaaa2e1 5649 /* Itbl support may require additional care here. */
252b5132
RH
5650 coproc = 1;
5651 goto st;
5652 case M_SWC3_AB:
5653 s = "swc3";
bdaaa2e1 5654 /* Itbl support may require additional care here. */
252b5132
RH
5655 coproc = 1;
5656 goto st;
5657 case M_SWL_AB:
5658 s = "swl";
5659 goto st;
5660 case M_SWR_AB:
5661 s = "swr";
5662 goto st;
5663 case M_SC_AB:
5664 s = "sc";
5665 goto st;
5666 case M_SCD_AB:
5667 s = "scd";
5668 goto st;
5669 case M_SDC1_AB:
ec68c924 5670 if (mips_arch == CPU_R4650)
252b5132
RH
5671 {
5672 as_bad (_("opcode not supported on this processor"));
5673 return;
5674 }
5675 s = "sdc1";
5676 coproc = 1;
bdaaa2e1 5677 /* Itbl support may require additional care here. */
252b5132
RH
5678 goto st;
5679 case M_SDC2_AB:
5680 s = "sdc2";
bdaaa2e1 5681 /* Itbl support may require additional care here. */
252b5132
RH
5682 coproc = 1;
5683 goto st;
5684 case M_SDC3_AB:
5685 s = "sdc3";
bdaaa2e1 5686 /* Itbl support may require additional care here. */
252b5132
RH
5687 coproc = 1;
5688 goto st;
5689 case M_SDL_AB:
5690 s = "sdl";
5691 goto st;
5692 case M_SDR_AB:
5693 s = "sdr";
5694 st:
5695 tempreg = AT;
5696 used_at = 1;
5697 ld_st:
bdaaa2e1 5698 /* Itbl support may require additional care here. */
252b5132
RH
5699 if (mask == M_LWC1_AB
5700 || mask == M_SWC1_AB
5701 || mask == M_LDC1_AB
5702 || mask == M_SDC1_AB
5703 || mask == M_L_DAB
5704 || mask == M_S_DAB)
5705 fmt = "T,o(b)";
5706 else if (coproc)
5707 fmt = "E,o(b)";
5708 else
5709 fmt = "t,o(b)";
5710
afdbd6d0
CD
5711 /* For embedded PIC, we allow loads where the offset is calculated
5712 by subtracting a symbol in the current segment from an unknown
5713 symbol, relative to a base register, e.g.:
5714 <op> $treg, <sym>-<localsym>($breg)
5715 This is used by the compiler for switch statements. */
76b3015f 5716 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5717 && offset_expr.X_op == O_subtract
5718 && (symbol_constant_p (offset_expr.X_op_symbol)
5719 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5720 : (symbol_equated_p (offset_expr.X_op_symbol)
5721 && (S_GET_SEGMENT
5722 (symbol_get_value_expression (offset_expr.X_op_symbol)
5723 ->X_add_symbol)
5724 == now_seg)))
5725 && breg != 0
5726 && (offset_expr.X_add_number == 0
5727 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5728 {
5729 /* For this case, we output the instructions:
5730 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5731 addiu $tempreg,$tempreg,$breg
5732 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5733 If the relocation would fit entirely in 16 bits, it would be
5734 nice to emit:
5735 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5736 instead, but that seems quite difficult. */
5737 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5738 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5739 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5740 ((bfd_arch_bits_per_address (stdoutput) == 32
5741 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5742 ? "addu" : "daddu"),
5743 "d,v,t", tempreg, tempreg, breg);
5744 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5745 (int) BFD_RELOC_PCREL_LO16, tempreg);
5746 if (! used_at)
5747 return;
5748 break;
5749 }
5750
252b5132
RH
5751 if (offset_expr.X_op != O_constant
5752 && offset_expr.X_op != O_symbol)
5753 {
5754 as_bad (_("expression too complex"));
5755 offset_expr.X_op = O_constant;
5756 }
5757
5758 /* A constant expression in PIC code can be handled just as it
5759 is in non PIC code. */
5760 if (mips_pic == NO_PIC
5761 || offset_expr.X_op == O_constant)
5762 {
f9419b05
TS
5763 char *p;
5764
252b5132
RH
5765 /* If this is a reference to a GP relative symbol, and there
5766 is no base register, we want
cdf6fd85 5767 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5768 Otherwise, if there is no base register, we want
5769 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5770 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5771 If we have a constant, we need two instructions anyhow,
5772 so we always use the latter form.
5773
5774 If we have a base register, and this is a reference to a
5775 GP relative symbol, we want
5776 addu $tempreg,$breg,$gp
cdf6fd85 5777 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5778 Otherwise we want
5779 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5780 addu $tempreg,$tempreg,$breg
5781 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5782 With a constant we always use the latter case.
76b3015f 5783
d6bc6245
TS
5784 With 64bit address space and no base register and $at usable,
5785 we want
5786 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5787 lui $at,<sym> (BFD_RELOC_HI16_S)
5788 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5789 dsll32 $tempreg,0
5790 daddu $tempreg,$at
5791 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5792 If we have a base register, we want
5793 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5794 lui $at,<sym> (BFD_RELOC_HI16_S)
5795 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5796 daddu $at,$breg
5797 dsll32 $tempreg,0
5798 daddu $tempreg,$at
5799 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5800
5801 Without $at we can't generate the optimal path for superscalar
5802 processors here since this would require two temporary registers.
5803 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5804 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5805 dsll $tempreg,16
5806 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5807 dsll $tempreg,16
5808 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5809 If we have a base register, we want
5810 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5811 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5812 dsll $tempreg,16
5813 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5814 dsll $tempreg,16
5815 daddu $tempreg,$tempreg,$breg
5816 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
5817
5818 If we have 64-bit addresses, as an optimization, for
5819 addresses which are 32-bit constants (e.g. kseg0/kseg1
5820 addresses) we fall back to the 32-bit address generation
78d32a17
MR
5821 mechanism since it is more efficient. Note that due to
5822 the signed offset used by memory operations, the 32-bit
5823 range is shifted down by 32768 here. This code should
6373ee54
CD
5824 probably attempt to generate 64-bit constants more
5825 efficiently in general.
d6bc6245 5826 */
6373ee54
CD
5827 if (HAVE_64BIT_ADDRESSES
5828 && !(offset_expr.X_op == O_constant
78d32a17 5829 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
d6bc6245
TS
5830 {
5831 p = NULL;
5832
5833 /* We don't do GP optimization for now because RELAX_ENCODE can't
5834 hold the data for such large chunks. */
5835
460597ba 5836 if (used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
5837 {
5838 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5839 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5840 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5841 AT, (int) BFD_RELOC_HI16_S);
5842 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5843 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5844 if (breg != 0)
2396cfb9
TS
5845 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5846 "d,v,t", AT, AT, breg);
5847 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5848 "d,w,<", tempreg, tempreg, 0);
5849 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5850 "d,v,t", tempreg, tempreg, AT);
d6bc6245
TS
5851 macro_build (p, &icnt, &offset_expr, s,
5852 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5853 used_at = 1;
5854 }
5855 else
5856 {
5857 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5858 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5859 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5860 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
5861 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5862 "d,w,<", tempreg, tempreg, 16);
d6bc6245
TS
5863 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5864 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
5865 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5866 "d,w,<", tempreg, tempreg, 16);
d6bc6245 5867 if (breg != 0)
2396cfb9
TS
5868 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5869 "d,v,t", tempreg, tempreg, breg);
d6bc6245
TS
5870 macro_build (p, &icnt, &offset_expr, s,
5871 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5872 }
5873
5874 return;
5875 }
76b3015f 5876
252b5132
RH
5877 if (breg == 0)
5878 {
e7d556df 5879 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5880 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5881 p = NULL;
5882 else
5883 {
5884 frag_grow (20);
5885 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766
TS
5886 treg, (int) BFD_RELOC_GPREL16,
5887 mips_gp_register);
252b5132
RH
5888 p = frag_var (rs_machine_dependent, 8, 0,
5889 RELAX_ENCODE (4, 8, 0, 4, 0,
5890 (mips_opts.warn_about_macros
5891 || (used_at
5892 && mips_opts.noat))),
956cd1d6 5893 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5894 used_at = 0;
5895 }
5896 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5897 if (p != NULL)
5898 p += 4;
5899 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5900 (int) BFD_RELOC_LO16, tempreg);
5901 }
5902 else
5903 {
e7d556df 5904 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5905 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5906 p = NULL;
5907 else
5908 {
5909 frag_grow (28);
5910 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5911 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5912 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5913 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5914 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5915 p = frag_var (rs_machine_dependent, 12, 0,
5916 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
956cd1d6 5917 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5918 }
5919 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5920 if (p != NULL)
5921 p += 4;
5922 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5923 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5924 "d,v,t", tempreg, tempreg, breg);
5925 if (p != NULL)
5926 p += 4;
5927 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5928 (int) BFD_RELOC_LO16, tempreg);
5929 }
5930 }
5931 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5932 {
f9419b05 5933 char *p;
ed6fb7bd 5934 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 5935
252b5132
RH
5936 /* If this is a reference to an external symbol, we want
5937 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5938 nop
5939 <op> $treg,0($tempreg)
5940 Otherwise we want
5941 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5942 nop
5943 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5944 <op> $treg,0($tempreg)
ed6fb7bd
SC
5945 If we have NewABI, we want
5946 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
252b5132
RH
5947 If there is a base register, we add it to $tempreg before
5948 the <op>. If there is a constant, we stick it in the
5949 <op> instruction. We don't handle constants larger than
5950 16 bits, because we have no way to load the upper 16 bits
5951 (actually, we could handle them for the subset of cases
5952 in which we are not using $at). */
5953 assert (offset_expr.X_op == O_symbol);
5954 expr1.X_add_number = offset_expr.X_add_number;
5955 offset_expr.X_add_number = 0;
ed6fb7bd
SC
5956 if (HAVE_NEWABI)
5957 lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
252b5132
RH
5958 if (expr1.X_add_number < -0x8000
5959 || expr1.X_add_number >= 0x8000)
5960 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5961 frag_grow (20);
5962 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766 5963 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
ed6fb7bd 5964 (int) lw_reloc_type, mips_gp_register);
252b5132 5965 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
bdaaa2e1 5966 p = frag_var (rs_machine_dependent, 4, 0,
252b5132 5967 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 5968 offset_expr.X_add_symbol, 0, NULL);
252b5132 5969 macro_build (p, &icnt, &offset_expr,
ca4e0257 5970 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5971 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5972 if (breg != 0)
5973 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5974 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5975 "d,v,t", tempreg, tempreg, breg);
5976 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5977 (int) BFD_RELOC_LO16, tempreg);
5978 }
5979 else if (mips_pic == SVR4_PIC)
5980 {
5981 int gpdel;
f9419b05 5982 char *p;
252b5132
RH
5983
5984 /* If this is a reference to an external symbol, we want
5985 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5986 addu $tempreg,$tempreg,$gp
5987 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5988 <op> $treg,0($tempreg)
5989 Otherwise we want
5990 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5991 nop
5992 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5993 <op> $treg,0($tempreg)
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
438c16b8
TS
5999 in which we are not using $at).
6000
6001 For NewABI, we want
6002 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6003 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
6004 <op> $treg,0($tempreg)
6005 */
252b5132
RH
6006 assert (offset_expr.X_op == O_symbol);
6007 expr1.X_add_number = offset_expr.X_add_number;
6008 offset_expr.X_add_number = 0;
6009 if (expr1.X_add_number < -0x8000
6010 || expr1.X_add_number >= 0x8000)
6011 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
438c16b8
TS
6012 if (HAVE_NEWABI)
6013 {
6014 macro_build ((char *) NULL, &icnt, &offset_expr,
6015 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6016 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6017 mips_gp_register);
6018 macro_build ((char *) NULL, &icnt, &offset_expr,
6019 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6020 "t,r,j", tempreg, tempreg,
6021 BFD_RELOC_MIPS_GOT_OFST);
6022 if (breg != 0)
6023 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6024 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6025 "d,v,t", tempreg, tempreg, breg);
6026 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6027 (int) BFD_RELOC_LO16, tempreg);
6028
6029 if (! used_at)
6030 return;
6031
6032 break;
6033 }
f7ea7ef2 6034 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6035 gpdel = 4;
6036 else
6037 gpdel = 0;
6038 frag_grow (36);
6039 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6040 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6041 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6042 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6043 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 6044 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6045 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6046 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6047 tempreg);
6048 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6049 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
c4e7957c 6050 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6051 if (gpdel > 0)
6052 {
6053 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6054 p += 4;
6055 }
6056 macro_build (p, &icnt, &offset_expr,
ca4e0257 6057 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6058 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6059 mips_gp_register);
252b5132
RH
6060 p += 4;
6061 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6062 p += 4;
6063 macro_build (p, &icnt, &offset_expr,
ca4e0257 6064 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
6065 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6066 if (breg != 0)
6067 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6068 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6069 "d,v,t", tempreg, tempreg, breg);
6070 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6071 (int) BFD_RELOC_LO16, tempreg);
6072 }
6073 else if (mips_pic == EMBEDDED_PIC)
6074 {
6075 /* If there is no base register, we want
cdf6fd85 6076 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6077 If there is a base register, we want
6078 addu $tempreg,$breg,$gp
cdf6fd85 6079 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6080 */
6081 assert (offset_expr.X_op == O_symbol);
6082 if (breg == 0)
6083 {
6084 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766 6085 treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
6086 used_at = 0;
6087 }
6088 else
6089 {
6090 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6091 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6092 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 6093 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 6094 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6095 }
6096 }
6097 else
6098 abort ();
6099
6100 if (! used_at)
6101 return;
6102
6103 break;
6104
6105 case M_LI:
6106 case M_LI_S:
6107 load_register (&icnt, treg, &imm_expr, 0);
6108 return;
6109
6110 case M_DLI:
6111 load_register (&icnt, treg, &imm_expr, 1);
6112 return;
6113
6114 case M_LI_SS:
6115 if (imm_expr.X_op == O_constant)
6116 {
6117 load_register (&icnt, AT, &imm_expr, 0);
6118 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6119 "mtc1", "t,G", AT, treg);
6120 break;
6121 }
6122 else
6123 {
6124 assert (offset_expr.X_op == O_symbol
6125 && strcmp (segment_name (S_GET_SEGMENT
6126 (offset_expr.X_add_symbol)),
6127 ".lit4") == 0
6128 && offset_expr.X_add_number == 0);
6129 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
c9914766 6130 treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6131 return;
6132 }
6133
6134 case M_LI_D:
ca4e0257
RS
6135 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6136 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6137 order 32 bits of the value and the low order 32 bits are either
6138 zero or in OFFSET_EXPR. */
252b5132
RH
6139 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6140 {
ca4e0257 6141 if (HAVE_64BIT_GPRS)
252b5132
RH
6142 load_register (&icnt, treg, &imm_expr, 1);
6143 else
6144 {
6145 int hreg, lreg;
6146
6147 if (target_big_endian)
6148 {
6149 hreg = treg;
6150 lreg = treg + 1;
6151 }
6152 else
6153 {
6154 hreg = treg + 1;
6155 lreg = treg;
6156 }
6157
6158 if (hreg <= 31)
6159 load_register (&icnt, hreg, &imm_expr, 0);
6160 if (lreg <= 31)
6161 {
6162 if (offset_expr.X_op == O_absent)
ea1fb5dc 6163 move_register (&icnt, lreg, 0);
252b5132
RH
6164 else
6165 {
6166 assert (offset_expr.X_op == O_constant);
6167 load_register (&icnt, lreg, &offset_expr, 0);
6168 }
6169 }
6170 }
6171 return;
6172 }
6173
6174 /* We know that sym is in the .rdata section. First we get the
6175 upper 16 bits of the address. */
6176 if (mips_pic == NO_PIC)
6177 {
956cd1d6 6178 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132
RH
6179 }
6180 else if (mips_pic == SVR4_PIC)
6181 {
6182 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6183 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6184 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6185 mips_gp_register);
252b5132
RH
6186 }
6187 else if (mips_pic == EMBEDDED_PIC)
6188 {
6189 /* For embedded PIC we pick up the entire address off $gp in
6190 a single instruction. */
6191 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
6192 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6193 mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
6194 offset_expr.X_op = O_constant;
6195 offset_expr.X_add_number = 0;
6196 }
6197 else
6198 abort ();
bdaaa2e1 6199
252b5132 6200 /* Now we load the register(s). */
ca4e0257 6201 if (HAVE_64BIT_GPRS)
252b5132
RH
6202 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6203 treg, (int) BFD_RELOC_LO16, AT);
6204 else
6205 {
6206 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6207 treg, (int) BFD_RELOC_LO16, AT);
f9419b05 6208 if (treg != RA)
252b5132
RH
6209 {
6210 /* FIXME: How in the world do we deal with the possible
6211 overflow here? */
6212 offset_expr.X_add_number += 4;
6213 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6214 treg + 1, (int) BFD_RELOC_LO16, AT);
6215 }
6216 }
6217
6218 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6219 does not become a variant frag. */
6220 frag_wane (frag_now);
6221 frag_new (0);
6222
6223 break;
6224
6225 case M_LI_DD:
ca4e0257
RS
6226 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6227 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6228 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6229 the value and the low order 32 bits are either zero or in
6230 OFFSET_EXPR. */
252b5132
RH
6231 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6232 {
ca4e0257
RS
6233 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6234 if (HAVE_64BIT_FPRS)
6235 {
6236 assert (HAVE_64BIT_GPRS);
6237 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6238 "dmtc1", "t,S", AT, treg);
6239 }
252b5132
RH
6240 else
6241 {
6242 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6243 "mtc1", "t,G", AT, treg + 1);
6244 if (offset_expr.X_op == O_absent)
6245 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6246 "mtc1", "t,G", 0, treg);
6247 else
6248 {
6249 assert (offset_expr.X_op == O_constant);
6250 load_register (&icnt, AT, &offset_expr, 0);
6251 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6252 "mtc1", "t,G", AT, treg);
6253 }
6254 }
6255 break;
6256 }
6257
6258 assert (offset_expr.X_op == O_symbol
6259 && offset_expr.X_add_number == 0);
6260 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6261 if (strcmp (s, ".lit8") == 0)
6262 {
e7af610e 6263 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6264 {
6265 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
c9914766
TS
6266 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6267 mips_gp_register);
252b5132
RH
6268 return;
6269 }
c9914766 6270 breg = mips_gp_register;
252b5132
RH
6271 r = BFD_RELOC_MIPS_LITERAL;
6272 goto dob;
6273 }
6274 else
6275 {
6276 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6277 if (mips_pic == SVR4_PIC)
6278 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6279 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6280 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6281 mips_gp_register);
252b5132
RH
6282 else
6283 {
6284 /* FIXME: This won't work for a 64 bit address. */
956cd1d6 6285 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132 6286 }
bdaaa2e1 6287
e7af610e 6288 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6289 {
6290 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6291 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6292
6293 /* To avoid confusion in tc_gen_reloc, we must ensure
6294 that this does not become a variant frag. */
6295 frag_wane (frag_now);
6296 frag_new (0);
6297
6298 break;
6299 }
6300 breg = AT;
6301 r = BFD_RELOC_LO16;
6302 goto dob;
6303 }
6304
6305 case M_L_DOB:
ec68c924 6306 if (mips_arch == CPU_R4650)
252b5132
RH
6307 {
6308 as_bad (_("opcode not supported on this processor"));
6309 return;
6310 }
6311 /* Even on a big endian machine $fn comes before $fn+1. We have
6312 to adjust when loading from memory. */
6313 r = BFD_RELOC_LO16;
6314 dob:
e7af610e 6315 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6316 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6317 target_big_endian ? treg + 1 : treg,
6318 (int) r, breg);
6319 /* FIXME: A possible overflow which I don't know how to deal
6320 with. */
6321 offset_expr.X_add_number += 4;
6322 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6323 target_big_endian ? treg : treg + 1,
6324 (int) r, breg);
6325
6326 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6327 does not become a variant frag. */
6328 frag_wane (frag_now);
6329 frag_new (0);
6330
6331 if (breg != AT)
6332 return;
6333 break;
6334
6335 case M_L_DAB:
6336 /*
6337 * The MIPS assembler seems to check for X_add_number not
6338 * being double aligned and generating:
6339 * lui at,%hi(foo+1)
6340 * addu at,at,v1
6341 * addiu at,at,%lo(foo+1)
6342 * lwc1 f2,0(at)
6343 * lwc1 f3,4(at)
6344 * But, the resulting address is the same after relocation so why
6345 * generate the extra instruction?
6346 */
ec68c924 6347 if (mips_arch == CPU_R4650)
252b5132
RH
6348 {
6349 as_bad (_("opcode not supported on this processor"));
6350 return;
6351 }
bdaaa2e1 6352 /* Itbl support may require additional care here. */
252b5132 6353 coproc = 1;
e7af610e 6354 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6355 {
6356 s = "ldc1";
6357 goto ld;
6358 }
6359
6360 s = "lwc1";
6361 fmt = "T,o(b)";
6362 goto ldd_std;
6363
6364 case M_S_DAB:
ec68c924 6365 if (mips_arch == CPU_R4650)
252b5132
RH
6366 {
6367 as_bad (_("opcode not supported on this processor"));
6368 return;
6369 }
6370
e7af610e 6371 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6372 {
6373 s = "sdc1";
6374 goto st;
6375 }
6376
6377 s = "swc1";
6378 fmt = "T,o(b)";
bdaaa2e1 6379 /* Itbl support may require additional care here. */
252b5132
RH
6380 coproc = 1;
6381 goto ldd_std;
6382
6383 case M_LD_AB:
ca4e0257 6384 if (HAVE_64BIT_GPRS)
252b5132
RH
6385 {
6386 s = "ld";
6387 goto ld;
6388 }
6389
6390 s = "lw";
6391 fmt = "t,o(b)";
6392 goto ldd_std;
6393
6394 case M_SD_AB:
ca4e0257 6395 if (HAVE_64BIT_GPRS)
252b5132
RH
6396 {
6397 s = "sd";
6398 goto st;
6399 }
6400
6401 s = "sw";
6402 fmt = "t,o(b)";
6403
6404 ldd_std:
afdbd6d0
CD
6405 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6406 loads for the case of doing a pair of loads to simulate an 'ld'.
6407 This is not currently done by the compiler, and assembly coders
6408 writing embedded-pic code can cope. */
6409
252b5132
RH
6410 if (offset_expr.X_op != O_symbol
6411 && offset_expr.X_op != O_constant)
6412 {
6413 as_bad (_("expression too complex"));
6414 offset_expr.X_op = O_constant;
6415 }
6416
6417 /* Even on a big endian machine $fn comes before $fn+1. We have
6418 to adjust when loading from memory. We set coproc if we must
6419 load $fn+1 first. */
bdaaa2e1 6420 /* Itbl support may require additional care here. */
252b5132
RH
6421 if (! target_big_endian)
6422 coproc = 0;
6423
6424 if (mips_pic == NO_PIC
6425 || offset_expr.X_op == O_constant)
6426 {
f9419b05
TS
6427 char *p;
6428
252b5132 6429 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6430 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6431 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6432 If we have a base register, we use this
6433 addu $at,$breg,$gp
cdf6fd85
TS
6434 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6435 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6436 If this is not a GP relative symbol, we want
6437 lui $at,<sym> (BFD_RELOC_HI16_S)
6438 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6439 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6440 If there is a base register, we add it to $at after the
6441 lui instruction. If there is a constant, we always use
6442 the last case. */
e7d556df 6443 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6444 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6445 {
6446 p = NULL;
6447 used_at = 1;
6448 }
6449 else
6450 {
6451 int off;
6452
6453 if (breg == 0)
6454 {
6455 frag_grow (28);
c9914766 6456 tempreg = mips_gp_register;
252b5132
RH
6457 off = 0;
6458 used_at = 0;
6459 }
6460 else
6461 {
6462 frag_grow (36);
6463 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6464 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6465 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6466 tempreg = AT;
6467 off = 4;
6468 used_at = 1;
6469 }
6470
beae10d5 6471 /* Itbl support may require additional care here. */
252b5132
RH
6472 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6473 coproc ? treg + 1 : treg,
cdf6fd85 6474 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6475 offset_expr.X_add_number += 4;
6476
6477 /* Set mips_optimize to 2 to avoid inserting an
6478 undesired nop. */
6479 hold_mips_optimize = mips_optimize;
6480 mips_optimize = 2;
beae10d5 6481 /* Itbl support may require additional care here. */
252b5132
RH
6482 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6483 coproc ? treg : treg + 1,
cdf6fd85 6484 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6485 mips_optimize = hold_mips_optimize;
6486
6487 p = frag_var (rs_machine_dependent, 12 + off, 0,
6488 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6489 used_at && mips_opts.noat),
956cd1d6 6490 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6491
6492 /* We just generated two relocs. When tc_gen_reloc
6493 handles this case, it will skip the first reloc and
6494 handle the second. The second reloc already has an
6495 extra addend of 4, which we added above. We must
6496 subtract it out, and then subtract another 4 to make
6497 the first reloc come out right. The second reloc
6498 will come out right because we are going to add 4 to
6499 offset_expr when we build its instruction below.
6500
6501 If we have a symbol, then we don't want to include
6502 the offset, because it will wind up being included
6503 when we generate the reloc. */
6504
6505 if (offset_expr.X_op == O_constant)
6506 offset_expr.X_add_number -= 8;
6507 else
6508 {
6509 offset_expr.X_add_number = -4;
6510 offset_expr.X_op = O_constant;
6511 }
6512 }
6513 macro_build_lui (p, &icnt, &offset_expr, AT);
6514 if (p != NULL)
6515 p += 4;
6516 if (breg != 0)
6517 {
6518 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6519 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6520 "d,v,t", AT, breg, AT);
6521 if (p != NULL)
6522 p += 4;
6523 }
beae10d5 6524 /* Itbl support may require additional care here. */
252b5132
RH
6525 macro_build (p, &icnt, &offset_expr, s, fmt,
6526 coproc ? treg + 1 : treg,
6527 (int) BFD_RELOC_LO16, AT);
6528 if (p != NULL)
6529 p += 4;
6530 /* FIXME: How do we handle overflow here? */
6531 offset_expr.X_add_number += 4;
beae10d5 6532 /* Itbl support may require additional care here. */
252b5132
RH
6533 macro_build (p, &icnt, &offset_expr, s, fmt,
6534 coproc ? treg : treg + 1,
6535 (int) BFD_RELOC_LO16, AT);
bdaaa2e1 6536 }
252b5132
RH
6537 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6538 {
6539 int off;
6540
6541 /* If this is a reference to an external symbol, we want
6542 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6543 nop
6544 <op> $treg,0($at)
6545 <op> $treg+1,4($at)
6546 Otherwise we want
6547 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6548 nop
6549 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6550 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6551 If there is a base register we add it to $at before the
6552 lwc1 instructions. If there is a constant we include it
6553 in the lwc1 instructions. */
6554 used_at = 1;
6555 expr1.X_add_number = offset_expr.X_add_number;
6556 offset_expr.X_add_number = 0;
6557 if (expr1.X_add_number < -0x8000
6558 || expr1.X_add_number >= 0x8000 - 4)
6559 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6560 if (breg == 0)
6561 off = 0;
6562 else
6563 off = 4;
6564 frag_grow (24 + off);
6565 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
6566 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6567 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6568 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6569 if (breg != 0)
6570 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6571 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6572 "d,v,t", AT, breg, AT);
beae10d5 6573 /* Itbl support may require additional care here. */
252b5132
RH
6574 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6575 coproc ? treg + 1 : treg,
6576 (int) BFD_RELOC_LO16, AT);
6577 expr1.X_add_number += 4;
6578
6579 /* Set mips_optimize to 2 to avoid inserting an undesired
6580 nop. */
6581 hold_mips_optimize = mips_optimize;
6582 mips_optimize = 2;
beae10d5 6583 /* Itbl support may require additional care here. */
252b5132
RH
6584 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6585 coproc ? treg : treg + 1,
6586 (int) BFD_RELOC_LO16, AT);
6587 mips_optimize = hold_mips_optimize;
6588
6589 (void) frag_var (rs_machine_dependent, 0, 0,
6590 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
c4e7957c 6591 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6592 }
6593 else if (mips_pic == SVR4_PIC)
6594 {
6595 int gpdel, off;
f9419b05 6596 char *p;
252b5132
RH
6597
6598 /* If this is a reference to an external symbol, we want
6599 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6600 addu $at,$at,$gp
6601 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6602 nop
6603 <op> $treg,0($at)
6604 <op> $treg+1,4($at)
6605 Otherwise we want
6606 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6607 nop
6608 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6609 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6610 If there is a base register we add it to $at before the
6611 lwc1 instructions. If there is a constant we include it
6612 in the lwc1 instructions. */
6613 used_at = 1;
6614 expr1.X_add_number = offset_expr.X_add_number;
6615 offset_expr.X_add_number = 0;
6616 if (expr1.X_add_number < -0x8000
6617 || expr1.X_add_number >= 0x8000 - 4)
6618 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
c9914766 6619 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6620 gpdel = 4;
6621 else
6622 gpdel = 0;
6623 if (breg == 0)
6624 off = 0;
6625 else
6626 off = 4;
6627 frag_grow (56);
6628 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6629 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6630 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6631 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6632 "d,v,t", AT, AT, mips_gp_register);
252b5132 6633 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6634 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6635 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6636 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6637 if (breg != 0)
6638 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6639 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6640 "d,v,t", AT, breg, AT);
beae10d5 6641 /* Itbl support may require additional care here. */
252b5132
RH
6642 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6643 coproc ? treg + 1 : treg,
6644 (int) BFD_RELOC_LO16, AT);
6645 expr1.X_add_number += 4;
6646
6647 /* Set mips_optimize to 2 to avoid inserting an undesired
6648 nop. */
6649 hold_mips_optimize = mips_optimize;
6650 mips_optimize = 2;
beae10d5 6651 /* Itbl support may require additional care here. */
252b5132
RH
6652 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6653 coproc ? treg : treg + 1,
6654 (int) BFD_RELOC_LO16, AT);
6655 mips_optimize = hold_mips_optimize;
6656 expr1.X_add_number -= 4;
6657
6658 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6659 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6660 8 + gpdel + off, 1, 0),
c4e7957c 6661 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6662 if (gpdel > 0)
6663 {
6664 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6665 p += 4;
6666 }
6667 macro_build (p, &icnt, &offset_expr,
ca4e0257 6668 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6669 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6670 mips_gp_register);
252b5132
RH
6671 p += 4;
6672 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6673 p += 4;
6674 if (breg != 0)
6675 {
6676 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6677 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6678 "d,v,t", AT, breg, AT);
6679 p += 4;
6680 }
beae10d5 6681 /* Itbl support may require additional care here. */
252b5132
RH
6682 macro_build (p, &icnt, &expr1, s, fmt,
6683 coproc ? treg + 1 : treg,
6684 (int) BFD_RELOC_LO16, AT);
6685 p += 4;
6686 expr1.X_add_number += 4;
6687
6688 /* Set mips_optimize to 2 to avoid inserting an undesired
6689 nop. */
6690 hold_mips_optimize = mips_optimize;
6691 mips_optimize = 2;
beae10d5 6692 /* Itbl support may require additional care here. */
252b5132
RH
6693 macro_build (p, &icnt, &expr1, s, fmt,
6694 coproc ? treg : treg + 1,
6695 (int) BFD_RELOC_LO16, AT);
6696 mips_optimize = hold_mips_optimize;
6697 }
6698 else if (mips_pic == EMBEDDED_PIC)
6699 {
6700 /* If there is no base register, we use
cdf6fd85
TS
6701 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6702 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6703 If we have a base register, we use
6704 addu $at,$breg,$gp
cdf6fd85
TS
6705 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6706 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6707 */
6708 if (breg == 0)
6709 {
c9914766 6710 tempreg = mips_gp_register;
252b5132
RH
6711 used_at = 0;
6712 }
6713 else
6714 {
6715 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6716 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6717 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6718 tempreg = AT;
6719 used_at = 1;
6720 }
6721
beae10d5 6722 /* Itbl support may require additional care here. */
252b5132
RH
6723 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6724 coproc ? treg + 1 : treg,
cdf6fd85 6725 (int) BFD_RELOC_GPREL16, tempreg);
252b5132 6726 offset_expr.X_add_number += 4;
beae10d5 6727 /* Itbl support may require additional care here. */
252b5132
RH
6728 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6729 coproc ? treg : treg + 1,
cdf6fd85 6730 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6731 }
6732 else
6733 abort ();
6734
6735 if (! used_at)
6736 return;
6737
6738 break;
6739
6740 case M_LD_OB:
6741 s = "lw";
6742 goto sd_ob;
6743 case M_SD_OB:
6744 s = "sw";
6745 sd_ob:
ca4e0257 6746 assert (HAVE_32BIT_ADDRESSES);
252b5132
RH
6747 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6748 (int) BFD_RELOC_LO16, breg);
6749 offset_expr.X_add_number += 4;
6750 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6751 (int) BFD_RELOC_LO16, breg);
6752 return;
6753
6754 /* New code added to support COPZ instructions.
6755 This code builds table entries out of the macros in mip_opcodes.
6756 R4000 uses interlocks to handle coproc delays.
6757 Other chips (like the R3000) require nops to be inserted for delays.
6758
f72c8c98 6759 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6760 In order to fill delay slots for non-interlocked chips,
6761 we must have a way to specify delays based on the coprocessor.
6762 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6763 What are the side-effects of the cop instruction?
6764 What cache support might we have and what are its effects?
6765 Both coprocessor & memory require delays. how long???
bdaaa2e1 6766 What registers are read/set/modified?
252b5132
RH
6767
6768 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6769 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6770
6771 case M_COP0:
6772 s = "c0";
6773 goto copz;
6774 case M_COP1:
6775 s = "c1";
6776 goto copz;
6777 case M_COP2:
6778 s = "c2";
6779 goto copz;
6780 case M_COP3:
6781 s = "c3";
6782 copz:
6783 /* For now we just do C (same as Cz). The parameter will be
6784 stored in insn_opcode by mips_ip. */
6785 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6786 ip->insn_opcode);
6787 return;
6788
ea1fb5dc
RS
6789 case M_MOVE:
6790 move_register (&icnt, dreg, sreg);
6791 return;
6792
252b5132
RH
6793#ifdef LOSING_COMPILER
6794 default:
6795 /* Try and see if this is a new itbl instruction.
6796 This code builds table entries out of the macros in mip_opcodes.
6797 FIXME: For now we just assemble the expression and pass it's
6798 value along as a 32-bit immediate.
bdaaa2e1 6799 We may want to have the assembler assemble this value,
252b5132
RH
6800 so that we gain the assembler's knowledge of delay slots,
6801 symbols, etc.
6802 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6803 if (itbl_have_entries
252b5132 6804 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6805 {
252b5132
RH
6806 s = ip->insn_mo->name;
6807 s2 = "cop3";
6808 coproc = ITBL_DECODE_PNUM (immed_expr);;
6809 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6810 return;
beae10d5 6811 }
252b5132
RH
6812 macro2 (ip);
6813 return;
6814 }
6815 if (mips_opts.noat)
6816 as_warn (_("Macro used $at after \".set noat\""));
6817}
bdaaa2e1 6818
252b5132
RH
6819static void
6820macro2 (ip)
6821 struct mips_cl_insn *ip;
6822{
6823 register int treg, sreg, dreg, breg;
6824 int tempreg;
6825 int mask;
6826 int icnt = 0;
6827 int used_at;
6828 expressionS expr1;
6829 const char *s;
6830 const char *s2;
6831 const char *fmt;
6832 int likely = 0;
6833 int dbl = 0;
6834 int coproc = 0;
6835 int lr = 0;
6836 int imm = 0;
6837 int off;
6838 offsetT maxnum;
6839 bfd_reloc_code_real_type r;
6840 char *p;
bdaaa2e1 6841
252b5132
RH
6842 treg = (ip->insn_opcode >> 16) & 0x1f;
6843 dreg = (ip->insn_opcode >> 11) & 0x1f;
6844 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6845 mask = ip->insn_mo->mask;
bdaaa2e1 6846
252b5132
RH
6847 expr1.X_op = O_constant;
6848 expr1.X_op_symbol = NULL;
6849 expr1.X_add_symbol = NULL;
6850 expr1.X_add_number = 1;
bdaaa2e1 6851
252b5132
RH
6852 switch (mask)
6853 {
6854#endif /* LOSING_COMPILER */
6855
6856 case M_DMUL:
6857 dbl = 1;
6858 case M_MUL:
2396cfb9
TS
6859 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6860 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6861 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6862 dreg);
252b5132
RH
6863 return;
6864
6865 case M_DMUL_I:
6866 dbl = 1;
6867 case M_MUL_I:
6868 /* The MIPS assembler some times generates shifts and adds. I'm
6869 not trying to be that fancy. GCC should do this for us
6870 anyway. */
6871 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6872 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6873 dbl ? "dmult" : "mult", "s,t", sreg, AT);
2396cfb9
TS
6874 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6875 dreg);
252b5132
RH
6876 break;
6877
6878 case M_DMULO_I:
6879 dbl = 1;
6880 case M_MULO_I:
6881 imm = 1;
6882 goto do_mulo;
6883
6884 case M_DMULO:
6885 dbl = 1;
6886 case M_MULO:
6887 do_mulo:
b34976b6 6888 mips_emit_delays (TRUE);
252b5132
RH
6889 ++mips_opts.noreorder;
6890 mips_any_noreorder = 1;
6891 if (imm)
6892 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6893 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6894 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6895 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6896 dreg);
6897 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f9419b05 6898 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
2396cfb9
TS
6899 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6900 AT);
252b5132 6901 if (mips_trap)
9bd7d936
MR
6902 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6903 "s,t,q", dreg, AT, 6);
252b5132
RH
6904 else
6905 {
6906 expr1.X_add_number = 8;
2396cfb9
TS
6907 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6908 AT);
6909 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6910 0);
6911 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6912 "c", 6);
252b5132
RH
6913 }
6914 --mips_opts.noreorder;
2396cfb9 6915 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
252b5132
RH
6916 break;
6917
6918 case M_DMULOU_I:
6919 dbl = 1;
6920 case M_MULOU_I:
6921 imm = 1;
6922 goto do_mulou;
6923
6924 case M_DMULOU:
6925 dbl = 1;
6926 case M_MULOU:
6927 do_mulou:
b34976b6 6928 mips_emit_delays (TRUE);
252b5132
RH
6929 ++mips_opts.noreorder;
6930 mips_any_noreorder = 1;
6931 if (imm)
6932 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6933 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
6934 dbl ? "dmultu" : "multu",
6935 "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6936 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6937 AT);
6938 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6939 dreg);
252b5132 6940 if (mips_trap)
9bd7d936
MR
6941 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6942 "s,t,q", AT, 0, 6);
252b5132
RH
6943 else
6944 {
6945 expr1.X_add_number = 8;
6946 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
2396cfb9
TS
6947 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6948 0);
6949 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6950 "c", 6);
252b5132
RH
6951 }
6952 --mips_opts.noreorder;
6953 break;
6954
771c7ce4 6955 case M_DROL:
af7ee8bf 6956 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
82dd0097
CD
6957 {
6958 if (dreg == sreg)
6959 {
6960 tempreg = AT;
6961 used_at = 1;
6962 }
6963 else
6964 {
6965 tempreg = dreg;
6966 used_at = 0;
6967 }
6968 macro_build ((char *) NULL, &icnt, NULL, "dnegu",
6969 "d,w", tempreg, treg);
6970 macro_build ((char *) NULL, &icnt, NULL, "drorv",
6971 "d,t,s", dreg, sreg, tempreg);
6972 if (used_at)
6973 break;
6974 return;
6975 }
771c7ce4
TS
6976 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6977 "d,v,t", AT, 0, treg);
6978 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6979 "d,t,s", AT, sreg, AT);
6980 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6981 "d,t,s", dreg, sreg, treg);
6982 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6983 "d,v,t", dreg, dreg, AT);
6984 break;
6985
252b5132 6986 case M_ROL:
af7ee8bf 6987 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
82dd0097
CD
6988 {
6989 if (dreg == sreg)
6990 {
6991 tempreg = AT;
6992 used_at = 1;
6993 }
6994 else
6995 {
6996 tempreg = dreg;
6997 used_at = 0;
6998 }
6999 macro_build ((char *) NULL, &icnt, NULL, "negu",
7000 "d,w", tempreg, treg);
7001 macro_build ((char *) NULL, &icnt, NULL, "rorv",
7002 "d,t,s", dreg, sreg, tempreg);
7003 if (used_at)
7004 break;
7005 return;
7006 }
2396cfb9
TS
7007 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7008 "d,v,t", AT, 0, treg);
7009 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7010 "d,t,s", AT, sreg, AT);
7011 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7012 "d,t,s", dreg, sreg, treg);
7013 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7014 "d,v,t", dreg, dreg, AT);
252b5132
RH
7015 break;
7016
771c7ce4
TS
7017 case M_DROL_I:
7018 {
7019 unsigned int rot;
82dd0097 7020 char *l, *r;
771c7ce4
TS
7021
7022 if (imm_expr.X_op != O_constant)
82dd0097 7023 as_bad (_("Improper rotate count"));
771c7ce4 7024 rot = imm_expr.X_add_number & 0x3f;
af7ee8bf 7025 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
60b63b72
RS
7026 {
7027 rot = (64 - rot) & 0x3f;
7028 if (rot >= 32)
7029 macro_build ((char *) NULL, &icnt, NULL, "dror32",
7030 "d,w,<", dreg, sreg, rot - 32);
7031 else
7032 macro_build ((char *) NULL, &icnt, NULL, "dror",
7033 "d,w,<", dreg, sreg, rot);
82dd0097 7034 return;
60b63b72 7035 }
483fc7cd 7036 if (rot == 0)
483fc7cd 7037 {
82dd0097
CD
7038 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7039 "d,w,<", dreg, sreg, 0);
7040 return;
483fc7cd 7041 }
82dd0097
CD
7042 l = (rot < 0x20) ? "dsll" : "dsll32";
7043 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7044 rot &= 0x1f;
7045 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7046 "d,w,<", AT, sreg, rot);
7047 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7048 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7049 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7050 "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7051 }
7052 break;
7053
252b5132 7054 case M_ROL_I:
771c7ce4
TS
7055 {
7056 unsigned int rot;
7057
7058 if (imm_expr.X_op != O_constant)
82dd0097 7059 as_bad (_("Improper rotate count"));
771c7ce4 7060 rot = imm_expr.X_add_number & 0x1f;
af7ee8bf 7061 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
60b63b72
RS
7062 {
7063 macro_build ((char *) NULL, &icnt, NULL, "ror",
7064 "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
82dd0097 7065 return;
60b63b72 7066 }
483fc7cd 7067 if (rot == 0)
483fc7cd 7068 {
483fc7cd 7069 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
82dd0097
CD
7070 "d,w,<", dreg, sreg, 0);
7071 return;
483fc7cd 7072 }
82dd0097
CD
7073 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7074 "d,w,<", AT, sreg, rot);
7075 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7076 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7077 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7078 "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7079 }
7080 break;
7081
7082 case M_DROR:
af7ee8bf 7083 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
82dd0097
CD
7084 {
7085 macro_build ((char *) NULL, &icnt, NULL, "drorv",
7086 "d,t,s", dreg, sreg, treg);
7087 return;
7088 }
771c7ce4
TS
7089 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7090 "d,v,t", AT, 0, treg);
7091 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7092 "d,t,s", AT, sreg, AT);
7093 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7094 "d,t,s", dreg, sreg, treg);
7095 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7096 "d,v,t", dreg, dreg, AT);
252b5132
RH
7097 break;
7098
7099 case M_ROR:
af7ee8bf 7100 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
82dd0097
CD
7101 {
7102 macro_build ((char *) NULL, &icnt, NULL, "rorv",
7103 "d,t,s", dreg, sreg, treg);
7104 return;
7105 }
2396cfb9
TS
7106 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7107 "d,v,t", AT, 0, treg);
7108 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7109 "d,t,s", AT, sreg, AT);
7110 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7111 "d,t,s", dreg, sreg, treg);
7112 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7113 "d,v,t", dreg, dreg, AT);
252b5132
RH
7114 break;
7115
771c7ce4
TS
7116 case M_DROR_I:
7117 {
7118 unsigned int rot;
82dd0097 7119 char *l, *r;
771c7ce4
TS
7120
7121 if (imm_expr.X_op != O_constant)
82dd0097 7122 as_bad (_("Improper rotate count"));
771c7ce4 7123 rot = imm_expr.X_add_number & 0x3f;
af7ee8bf 7124 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
82dd0097
CD
7125 {
7126 if (rot >= 32)
7127 macro_build ((char *) NULL, &icnt, NULL, "dror32",
7128 "d,w,<", dreg, sreg, rot - 32);
7129 else
7130 macro_build ((char *) NULL, &icnt, NULL, "dror",
7131 "d,w,<", dreg, sreg, rot);
7132 return;
7133 }
483fc7cd 7134 if (rot == 0)
483fc7cd 7135 {
82dd0097
CD
7136 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7137 "d,w,<", dreg, sreg, 0);
7138 return;
483fc7cd 7139 }
82dd0097
CD
7140 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7141 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7142 rot &= 0x1f;
7143 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7144 "d,w,<", AT, sreg, rot);
7145 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7146 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7147 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7148 "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7149 }
7150 break;
7151
252b5132 7152 case M_ROR_I:
771c7ce4
TS
7153 {
7154 unsigned int rot;
7155
7156 if (imm_expr.X_op != O_constant)
82dd0097 7157 as_bad (_("Improper rotate count"));
771c7ce4 7158 rot = imm_expr.X_add_number & 0x1f;
af7ee8bf 7159 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
82dd0097
CD
7160 {
7161 macro_build ((char *) NULL, &icnt, NULL, "ror",
7162 "d,w,<", dreg, sreg, rot);
7163 return;
7164 }
483fc7cd 7165 if (rot == 0)
483fc7cd
RS
7166 {
7167 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
82dd0097
CD
7168 "d,w,<", dreg, sreg, 0);
7169 return;
483fc7cd 7170 }
82dd0097
CD
7171 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7172 "d,w,<", AT, sreg, rot);
7173 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7174 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7175 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7176 "d,v,t", dreg, dreg, AT);
771c7ce4 7177 }
252b5132
RH
7178 break;
7179
7180 case M_S_DOB:
ec68c924 7181 if (mips_arch == CPU_R4650)
252b5132
RH
7182 {
7183 as_bad (_("opcode not supported on this processor"));
7184 return;
7185 }
e7af610e 7186 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7187 /* Even on a big endian machine $fn comes before $fn+1. We have
7188 to adjust when storing to memory. */
7189 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7190 target_big_endian ? treg + 1 : treg,
7191 (int) BFD_RELOC_LO16, breg);
7192 offset_expr.X_add_number += 4;
7193 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7194 target_big_endian ? treg : treg + 1,
7195 (int) BFD_RELOC_LO16, breg);
7196 return;
7197
7198 case M_SEQ:
7199 if (sreg == 0)
7200 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7201 treg, (int) BFD_RELOC_LO16);
7202 else if (treg == 0)
7203 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7204 sreg, (int) BFD_RELOC_LO16);
7205 else
7206 {
2396cfb9
TS
7207 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7208 "d,v,t", dreg, sreg, treg);
252b5132
RH
7209 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7210 dreg, (int) BFD_RELOC_LO16);
7211 }
7212 return;
7213
7214 case M_SEQ_I:
7215 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7216 {
7217 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7218 sreg, (int) BFD_RELOC_LO16);
7219 return;
7220 }
7221 if (sreg == 0)
7222 {
7223 as_warn (_("Instruction %s: result is always false"),
7224 ip->insn_mo->name);
ea1fb5dc 7225 move_register (&icnt, dreg, 0);
252b5132
RH
7226 return;
7227 }
7228 if (imm_expr.X_op == O_constant
7229 && imm_expr.X_add_number >= 0
7230 && imm_expr.X_add_number < 0x10000)
7231 {
7232 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7233 sreg, (int) BFD_RELOC_LO16);
7234 used_at = 0;
7235 }
7236 else if (imm_expr.X_op == O_constant
7237 && imm_expr.X_add_number > -0x8000
7238 && imm_expr.X_add_number < 0)
7239 {
7240 imm_expr.X_add_number = -imm_expr.X_add_number;
7241 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 7242 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
7243 "t,r,j", dreg, sreg,
7244 (int) BFD_RELOC_LO16);
7245 used_at = 0;
7246 }
7247 else
7248 {
4d34fb5f 7249 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7250 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7251 "d,v,t", dreg, sreg, AT);
252b5132
RH
7252 used_at = 1;
7253 }
7254 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7255 (int) BFD_RELOC_LO16);
7256 if (used_at)
7257 break;
7258 return;
7259
7260 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7261 s = "slt";
7262 goto sge;
7263 case M_SGEU:
7264 s = "sltu";
7265 sge:
2396cfb9
TS
7266 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7267 dreg, sreg, treg);
252b5132
RH
7268 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7269 (int) BFD_RELOC_LO16);
7270 return;
7271
7272 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7273 case M_SGEU_I:
7274 if (imm_expr.X_op == O_constant
7275 && imm_expr.X_add_number >= -0x8000
7276 && imm_expr.X_add_number < 0x8000)
7277 {
7278 macro_build ((char *) NULL, &icnt, &imm_expr,
7279 mask == M_SGE_I ? "slti" : "sltiu",
7280 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7281 used_at = 0;
7282 }
7283 else
7284 {
4d34fb5f 7285 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7286 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7287 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7288 AT);
252b5132
RH
7289 used_at = 1;
7290 }
7291 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7292 (int) BFD_RELOC_LO16);
7293 if (used_at)
7294 break;
7295 return;
7296
7297 case M_SGT: /* sreg > treg <==> treg < sreg */
7298 s = "slt";
7299 goto sgt;
7300 case M_SGTU:
7301 s = "sltu";
7302 sgt:
2396cfb9
TS
7303 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7304 dreg, treg, sreg);
252b5132
RH
7305 return;
7306
7307 case M_SGT_I: /* sreg > I <==> I < sreg */
7308 s = "slt";
7309 goto sgti;
7310 case M_SGTU_I:
7311 s = "sltu";
7312 sgti:
4d34fb5f 7313 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7314 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7315 dreg, AT, sreg);
252b5132
RH
7316 break;
7317
2396cfb9 7318 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7319 s = "slt";
7320 goto sle;
7321 case M_SLEU:
7322 s = "sltu";
7323 sle:
2396cfb9
TS
7324 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7325 dreg, treg, sreg);
252b5132
RH
7326 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7327 (int) BFD_RELOC_LO16);
7328 return;
7329
2396cfb9 7330 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7331 s = "slt";
7332 goto slei;
7333 case M_SLEU_I:
7334 s = "sltu";
7335 slei:
4d34fb5f 7336 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7337 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7338 dreg, AT, sreg);
252b5132
RH
7339 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7340 (int) BFD_RELOC_LO16);
7341 break;
7342
7343 case M_SLT_I:
7344 if (imm_expr.X_op == O_constant
7345 && imm_expr.X_add_number >= -0x8000
7346 && imm_expr.X_add_number < 0x8000)
7347 {
7348 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7349 dreg, sreg, (int) BFD_RELOC_LO16);
7350 return;
7351 }
4d34fb5f 7352 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7353 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7354 dreg, sreg, AT);
252b5132
RH
7355 break;
7356
7357 case M_SLTU_I:
7358 if (imm_expr.X_op == O_constant
7359 && imm_expr.X_add_number >= -0x8000
7360 && imm_expr.X_add_number < 0x8000)
7361 {
7362 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7363 dreg, sreg, (int) BFD_RELOC_LO16);
7364 return;
7365 }
4d34fb5f 7366 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7367 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7368 "d,v,t", dreg, sreg, AT);
252b5132
RH
7369 break;
7370
7371 case M_SNE:
7372 if (sreg == 0)
2396cfb9
TS
7373 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7374 "d,v,t", dreg, 0, treg);
252b5132 7375 else if (treg == 0)
2396cfb9
TS
7376 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7377 "d,v,t", dreg, 0, sreg);
252b5132
RH
7378 else
7379 {
2396cfb9
TS
7380 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7381 "d,v,t", dreg, sreg, treg);
7382 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7383 "d,v,t", dreg, 0, dreg);
252b5132
RH
7384 }
7385 return;
7386
7387 case M_SNE_I:
7388 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7389 {
2396cfb9
TS
7390 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7391 "d,v,t", dreg, 0, sreg);
252b5132
RH
7392 return;
7393 }
7394 if (sreg == 0)
7395 {
7396 as_warn (_("Instruction %s: result is always true"),
7397 ip->insn_mo->name);
7398 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 7399 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
7400 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7401 return;
7402 }
7403 if (imm_expr.X_op == O_constant
7404 && imm_expr.X_add_number >= 0
7405 && imm_expr.X_add_number < 0x10000)
7406 {
7407 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7408 dreg, sreg, (int) BFD_RELOC_LO16);
7409 used_at = 0;
7410 }
7411 else if (imm_expr.X_op == O_constant
7412 && imm_expr.X_add_number > -0x8000
7413 && imm_expr.X_add_number < 0)
7414 {
7415 imm_expr.X_add_number = -imm_expr.X_add_number;
7416 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 7417 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
7418 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7419 used_at = 0;
7420 }
7421 else
7422 {
4d34fb5f 7423 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7424 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7425 "d,v,t", dreg, sreg, AT);
252b5132
RH
7426 used_at = 1;
7427 }
2396cfb9
TS
7428 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7429 "d,v,t", dreg, 0, dreg);
252b5132
RH
7430 if (used_at)
7431 break;
7432 return;
7433
7434 case M_DSUB_I:
7435 dbl = 1;
7436 case M_SUB_I:
7437 if (imm_expr.X_op == O_constant
7438 && imm_expr.X_add_number > -0x8000
7439 && imm_expr.X_add_number <= 0x8000)
7440 {
7441 imm_expr.X_add_number = -imm_expr.X_add_number;
7442 macro_build ((char *) NULL, &icnt, &imm_expr,
7443 dbl ? "daddi" : "addi",
7444 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7445 return;
7446 }
7447 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7448 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7449 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7450 break;
7451
7452 case M_DSUBU_I:
7453 dbl = 1;
7454 case M_SUBU_I:
7455 if (imm_expr.X_op == O_constant
7456 && imm_expr.X_add_number > -0x8000
7457 && imm_expr.X_add_number <= 0x8000)
7458 {
7459 imm_expr.X_add_number = -imm_expr.X_add_number;
7460 macro_build ((char *) NULL, &icnt, &imm_expr,
7461 dbl ? "daddiu" : "addiu",
7462 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7463 return;
7464 }
7465 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7466 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7467 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7468 break;
7469
7470 case M_TEQ_I:
7471 s = "teq";
7472 goto trap;
7473 case M_TGE_I:
7474 s = "tge";
7475 goto trap;
7476 case M_TGEU_I:
7477 s = "tgeu";
7478 goto trap;
7479 case M_TLT_I:
7480 s = "tlt";
7481 goto trap;
7482 case M_TLTU_I:
7483 s = "tltu";
7484 goto trap;
7485 case M_TNE_I:
7486 s = "tne";
7487 trap:
4d34fb5f 7488 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7489 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7490 AT);
252b5132
RH
7491 break;
7492
252b5132 7493 case M_TRUNCWS:
43841e91 7494 case M_TRUNCWD:
e7af610e 7495 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7496 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7497 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7498
7499 /*
7500 * Is the double cfc1 instruction a bug in the mips assembler;
7501 * or is there a reason for it?
7502 */
b34976b6 7503 mips_emit_delays (TRUE);
252b5132
RH
7504 ++mips_opts.noreorder;
7505 mips_any_noreorder = 1;
2396cfb9 7506 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7507 treg, RA);
2396cfb9 7508 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7509 treg, RA);
2396cfb9 7510 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7511 expr1.X_add_number = 3;
7512 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7513 (int) BFD_RELOC_LO16);
7514 expr1.X_add_number = 2;
7515 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7516 (int) BFD_RELOC_LO16);
2396cfb9 7517 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7518 AT, RA);
2396cfb9
TS
7519 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7520 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132 7521 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
2396cfb9 7522 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7523 treg, RA);
2396cfb9 7524 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7525 --mips_opts.noreorder;
7526 break;
7527
7528 case M_ULH:
7529 s = "lb";
7530 goto ulh;
7531 case M_ULHU:
7532 s = "lbu";
7533 ulh:
7534 if (offset_expr.X_add_number >= 0x7fff)
7535 as_bad (_("operand overflow"));
7536 /* avoid load delay */
7537 if (! target_big_endian)
f9419b05 7538 ++offset_expr.X_add_number;
252b5132
RH
7539 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7540 (int) BFD_RELOC_LO16, breg);
7541 if (! target_big_endian)
f9419b05 7542 --offset_expr.X_add_number;
252b5132 7543 else
f9419b05 7544 ++offset_expr.X_add_number;
252b5132
RH
7545 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7546 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7547 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7548 treg, treg, 8);
7549 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7550 treg, treg, AT);
252b5132
RH
7551 break;
7552
7553 case M_ULD:
7554 s = "ldl";
7555 s2 = "ldr";
7556 off = 7;
7557 goto ulw;
7558 case M_ULW:
7559 s = "lwl";
7560 s2 = "lwr";
7561 off = 3;
7562 ulw:
7563 if (offset_expr.X_add_number >= 0x8000 - off)
7564 as_bad (_("operand overflow"));
7565 if (! target_big_endian)
7566 offset_expr.X_add_number += off;
7567 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7568 (int) BFD_RELOC_LO16, breg);
7569 if (! target_big_endian)
7570 offset_expr.X_add_number -= off;
7571 else
7572 offset_expr.X_add_number += off;
7573 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7574 (int) BFD_RELOC_LO16, breg);
7575 return;
7576
7577 case M_ULD_A:
7578 s = "ldl";
7579 s2 = "ldr";
7580 off = 7;
7581 goto ulwa;
7582 case M_ULW_A:
7583 s = "lwl";
7584 s2 = "lwr";
7585 off = 3;
7586 ulwa:
d6bc6245 7587 used_at = 1;
c9914766 7588 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7589 if (breg != 0)
7590 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7591 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7592 "d,v,t", AT, AT, breg);
7593 if (! target_big_endian)
7594 expr1.X_add_number = off;
7595 else
7596 expr1.X_add_number = 0;
7597 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7598 (int) BFD_RELOC_LO16, AT);
7599 if (! target_big_endian)
7600 expr1.X_add_number = 0;
7601 else
7602 expr1.X_add_number = off;
7603 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7604 (int) BFD_RELOC_LO16, AT);
7605 break;
7606
7607 case M_ULH_A:
7608 case M_ULHU_A:
d6bc6245 7609 used_at = 1;
c9914766 7610 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7611 if (breg != 0)
7612 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7613 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7614 "d,v,t", AT, AT, breg);
7615 if (target_big_endian)
7616 expr1.X_add_number = 0;
7617 macro_build ((char *) NULL, &icnt, &expr1,
7618 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7619 (int) BFD_RELOC_LO16, AT);
7620 if (target_big_endian)
7621 expr1.X_add_number = 1;
7622 else
7623 expr1.X_add_number = 0;
7624 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7625 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7626 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7627 treg, treg, 8);
7628 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7629 treg, treg, AT);
252b5132
RH
7630 break;
7631
7632 case M_USH:
7633 if (offset_expr.X_add_number >= 0x7fff)
7634 as_bad (_("operand overflow"));
7635 if (target_big_endian)
f9419b05 7636 ++offset_expr.X_add_number;
252b5132
RH
7637 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7638 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7639 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7640 AT, treg, 8);
252b5132 7641 if (target_big_endian)
f9419b05 7642 --offset_expr.X_add_number;
252b5132 7643 else
f9419b05 7644 ++offset_expr.X_add_number;
252b5132
RH
7645 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7646 (int) BFD_RELOC_LO16, breg);
7647 break;
7648
7649 case M_USD:
7650 s = "sdl";
7651 s2 = "sdr";
7652 off = 7;
7653 goto usw;
7654 case M_USW:
7655 s = "swl";
7656 s2 = "swr";
7657 off = 3;
7658 usw:
7659 if (offset_expr.X_add_number >= 0x8000 - off)
7660 as_bad (_("operand overflow"));
7661 if (! target_big_endian)
7662 offset_expr.X_add_number += off;
7663 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7664 (int) BFD_RELOC_LO16, breg);
7665 if (! target_big_endian)
7666 offset_expr.X_add_number -= off;
7667 else
7668 offset_expr.X_add_number += off;
7669 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7670 (int) BFD_RELOC_LO16, breg);
7671 return;
7672
7673 case M_USD_A:
7674 s = "sdl";
7675 s2 = "sdr";
7676 off = 7;
7677 goto uswa;
7678 case M_USW_A:
7679 s = "swl";
7680 s2 = "swr";
7681 off = 3;
7682 uswa:
d6bc6245 7683 used_at = 1;
c9914766 7684 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7685 if (breg != 0)
7686 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7687 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7688 "d,v,t", AT, AT, breg);
7689 if (! target_big_endian)
7690 expr1.X_add_number = off;
7691 else
7692 expr1.X_add_number = 0;
7693 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7694 (int) BFD_RELOC_LO16, AT);
7695 if (! target_big_endian)
7696 expr1.X_add_number = 0;
7697 else
7698 expr1.X_add_number = off;
7699 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7700 (int) BFD_RELOC_LO16, AT);
7701 break;
7702
7703 case M_USH_A:
d6bc6245 7704 used_at = 1;
c9914766 7705 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7706 if (breg != 0)
7707 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7708 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7709 "d,v,t", AT, AT, breg);
7710 if (! target_big_endian)
7711 expr1.X_add_number = 0;
7712 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7713 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7714 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7715 treg, treg, 8);
252b5132
RH
7716 if (! target_big_endian)
7717 expr1.X_add_number = 1;
7718 else
7719 expr1.X_add_number = 0;
7720 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7721 (int) BFD_RELOC_LO16, AT);
7722 if (! target_big_endian)
7723 expr1.X_add_number = 0;
7724 else
7725 expr1.X_add_number = 1;
7726 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7727 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7728 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7729 treg, treg, 8);
7730 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7731 treg, treg, AT);
252b5132
RH
7732 break;
7733
7734 default:
7735 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7736 are added dynamically. */
252b5132
RH
7737 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7738 break;
7739 }
7740 if (mips_opts.noat)
7741 as_warn (_("Macro used $at after \".set noat\""));
7742}
7743
7744/* Implement macros in mips16 mode. */
7745
7746static void
7747mips16_macro (ip)
7748 struct mips_cl_insn *ip;
7749{
7750 int mask;
7751 int xreg, yreg, zreg, tmp;
7752 int icnt;
7753 expressionS expr1;
7754 int dbl;
7755 const char *s, *s2, *s3;
7756
7757 mask = ip->insn_mo->mask;
7758
7759 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7760 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7761 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7762
7763 icnt = 0;
7764
7765 expr1.X_op = O_constant;
7766 expr1.X_op_symbol = NULL;
7767 expr1.X_add_symbol = NULL;
7768 expr1.X_add_number = 1;
7769
7770 dbl = 0;
7771
7772 switch (mask)
7773 {
7774 default:
7775 internalError ();
7776
7777 case M_DDIV_3:
7778 dbl = 1;
7779 case M_DIV_3:
7780 s = "mflo";
7781 goto do_div3;
7782 case M_DREM_3:
7783 dbl = 1;
7784 case M_REM_3:
7785 s = "mfhi";
7786 do_div3:
b34976b6 7787 mips_emit_delays (TRUE);
252b5132
RH
7788 ++mips_opts.noreorder;
7789 mips_any_noreorder = 1;
2396cfb9 7790 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
7791 dbl ? "ddiv" : "div",
7792 "0,x,y", xreg, yreg);
7793 expr1.X_add_number = 2;
7794 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
2396cfb9
TS
7795 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7796 7);
bdaaa2e1 7797
252b5132
RH
7798 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7799 since that causes an overflow. We should do that as well,
7800 but I don't see how to do the comparisons without a temporary
7801 register. */
7802 --mips_opts.noreorder;
2396cfb9 7803 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
252b5132
RH
7804 break;
7805
7806 case M_DIVU_3:
7807 s = "divu";
7808 s2 = "mflo";
7809 goto do_divu3;
7810 case M_REMU_3:
7811 s = "divu";
7812 s2 = "mfhi";
7813 goto do_divu3;
7814 case M_DDIVU_3:
7815 s = "ddivu";
7816 s2 = "mflo";
7817 goto do_divu3;
7818 case M_DREMU_3:
7819 s = "ddivu";
7820 s2 = "mfhi";
7821 do_divu3:
b34976b6 7822 mips_emit_delays (TRUE);
252b5132
RH
7823 ++mips_opts.noreorder;
7824 mips_any_noreorder = 1;
2396cfb9
TS
7825 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7826 xreg, yreg);
252b5132
RH
7827 expr1.X_add_number = 2;
7828 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
98d3f06f
KH
7829 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7830 "6", 7);
252b5132 7831 --mips_opts.noreorder;
2396cfb9 7832 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
252b5132
RH
7833 break;
7834
7835 case M_DMUL:
7836 dbl = 1;
7837 case M_MUL:
2396cfb9 7838 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7839 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
2396cfb9
TS
7840 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7841 zreg);
252b5132
RH
7842 return;
7843
7844 case M_DSUBU_I:
7845 dbl = 1;
7846 goto do_subu;
7847 case M_SUBU_I:
7848 do_subu:
7849 if (imm_expr.X_op != O_constant)
7850 as_bad (_("Unsupported large constant"));
7851 imm_expr.X_add_number = -imm_expr.X_add_number;
7852 macro_build ((char *) NULL, &icnt, &imm_expr,
9a41af64 7853 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7854 break;
7855
7856 case M_SUBU_I_2:
7857 if (imm_expr.X_op != O_constant)
7858 as_bad (_("Unsupported large constant"));
7859 imm_expr.X_add_number = -imm_expr.X_add_number;
7860 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7861 "x,k", xreg);
7862 break;
7863
7864 case M_DSUBU_I_2:
7865 if (imm_expr.X_op != O_constant)
7866 as_bad (_("Unsupported large constant"));
7867 imm_expr.X_add_number = -imm_expr.X_add_number;
7868 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7869 "y,j", yreg);
7870 break;
7871
7872 case M_BEQ:
7873 s = "cmp";
7874 s2 = "bteqz";
7875 goto do_branch;
7876 case M_BNE:
7877 s = "cmp";
7878 s2 = "btnez";
7879 goto do_branch;
7880 case M_BLT:
7881 s = "slt";
7882 s2 = "btnez";
7883 goto do_branch;
7884 case M_BLTU:
7885 s = "sltu";
7886 s2 = "btnez";
7887 goto do_branch;
7888 case M_BLE:
7889 s = "slt";
7890 s2 = "bteqz";
7891 goto do_reverse_branch;
7892 case M_BLEU:
7893 s = "sltu";
7894 s2 = "bteqz";
7895 goto do_reverse_branch;
7896 case M_BGE:
7897 s = "slt";
7898 s2 = "bteqz";
7899 goto do_branch;
7900 case M_BGEU:
7901 s = "sltu";
7902 s2 = "bteqz";
7903 goto do_branch;
7904 case M_BGT:
7905 s = "slt";
7906 s2 = "btnez";
7907 goto do_reverse_branch;
7908 case M_BGTU:
7909 s = "sltu";
7910 s2 = "btnez";
7911
7912 do_reverse_branch:
7913 tmp = xreg;
7914 xreg = yreg;
7915 yreg = tmp;
7916
7917 do_branch:
7918 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7919 xreg, yreg);
7920 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7921 break;
7922
7923 case M_BEQ_I:
7924 s = "cmpi";
7925 s2 = "bteqz";
7926 s3 = "x,U";
7927 goto do_branch_i;
7928 case M_BNE_I:
7929 s = "cmpi";
7930 s2 = "btnez";
7931 s3 = "x,U";
7932 goto do_branch_i;
7933 case M_BLT_I:
7934 s = "slti";
7935 s2 = "btnez";
7936 s3 = "x,8";
7937 goto do_branch_i;
7938 case M_BLTU_I:
7939 s = "sltiu";
7940 s2 = "btnez";
7941 s3 = "x,8";
7942 goto do_branch_i;
7943 case M_BLE_I:
7944 s = "slti";
7945 s2 = "btnez";
7946 s3 = "x,8";
7947 goto do_addone_branch_i;
7948 case M_BLEU_I:
7949 s = "sltiu";
7950 s2 = "btnez";
7951 s3 = "x,8";
7952 goto do_addone_branch_i;
7953 case M_BGE_I:
7954 s = "slti";
7955 s2 = "bteqz";
7956 s3 = "x,8";
7957 goto do_branch_i;
7958 case M_BGEU_I:
7959 s = "sltiu";
7960 s2 = "bteqz";
7961 s3 = "x,8";
7962 goto do_branch_i;
7963 case M_BGT_I:
7964 s = "slti";
7965 s2 = "bteqz";
7966 s3 = "x,8";
7967 goto do_addone_branch_i;
7968 case M_BGTU_I:
7969 s = "sltiu";
7970 s2 = "bteqz";
7971 s3 = "x,8";
7972
7973 do_addone_branch_i:
7974 if (imm_expr.X_op != O_constant)
7975 as_bad (_("Unsupported large constant"));
7976 ++imm_expr.X_add_number;
7977
7978 do_branch_i:
7979 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7980 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7981 break;
7982
7983 case M_ABS:
7984 expr1.X_add_number = 0;
98d3f06f 7985 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
252b5132 7986 if (xreg != yreg)
ea1fb5dc 7987 move_register (&icnt, xreg, yreg);
252b5132
RH
7988 expr1.X_add_number = 2;
7989 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7990 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7991 "neg", "x,w", xreg, xreg);
7992 }
7993}
7994
7995/* For consistency checking, verify that all bits are specified either
7996 by the match/mask part of the instruction definition, or by the
7997 operand list. */
7998static int
7999validate_mips_insn (opc)
8000 const struct mips_opcode *opc;
8001{
8002 const char *p = opc->args;
8003 char c;
8004 unsigned long used_bits = opc->mask;
8005
8006 if ((used_bits & opc->match) != opc->match)
8007 {
8008 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8009 opc->name, opc->args);
8010 return 0;
8011 }
8012#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
8013 while (*p)
8014 switch (c = *p++)
8015 {
8016 case ',': break;
8017 case '(': break;
8018 case ')': break;
af7ee8bf
CD
8019 case '+':
8020 switch (c = *p++)
8021 {
8022 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8023 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8024 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
8025 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
8026 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
af7ee8bf
CD
8027 default:
8028 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8029 c, opc->name, opc->args);
8030 return 0;
8031 }
8032 break;
252b5132
RH
8033 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8034 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8035 case 'A': break;
4372b673 8036 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
8037 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8038 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8039 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8040 case 'F': break;
8041 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 8042 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 8043 case 'I': break;
e972090a 8044 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 8045 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
8046 case 'L': break;
8047 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8048 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
8049 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8050 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8051 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8052 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8053 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8054 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8055 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8056 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
8057 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8058 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8059 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8060 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8061 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8062 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8063 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8064 case 'f': break;
8065 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8066 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8067 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8068 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8069 case 'l': break;
8070 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8071 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8072 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8073 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8074 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8075 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8076 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8077 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8078 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8079 case 'x': break;
8080 case 'z': break;
8081 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
8082 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8083 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
8084 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8085 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8086 case '[': break;
8087 case ']': break;
252b5132
RH
8088 default:
8089 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8090 c, opc->name, opc->args);
8091 return 0;
8092 }
8093#undef USE_BITS
8094 if (used_bits != 0xffffffff)
8095 {
8096 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8097 ~used_bits & 0xffffffff, opc->name, opc->args);
8098 return 0;
8099 }
8100 return 1;
8101}
8102
8103/* This routine assembles an instruction into its binary format. As a
8104 side effect, it sets one of the global variables imm_reloc or
8105 offset_reloc to the type of relocation to do if one of the operands
8106 is an address expression. */
8107
8108static void
8109mips_ip (str, ip)
8110 char *str;
8111 struct mips_cl_insn *ip;
8112{
8113 char *s;
8114 const char *args;
43841e91 8115 char c = 0;
252b5132
RH
8116 struct mips_opcode *insn;
8117 char *argsStart;
8118 unsigned int regno;
8119 unsigned int lastregno = 0;
af7ee8bf 8120 unsigned int lastpos = 0;
071742cf 8121 unsigned int limlo, limhi;
252b5132
RH
8122 char *s_reset;
8123 char save_c = 0;
252b5132
RH
8124
8125 insn_error = NULL;
8126
8127 /* If the instruction contains a '.', we first try to match an instruction
8128 including the '.'. Then we try again without the '.'. */
8129 insn = NULL;
3882b010 8130 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
8131 continue;
8132
8133 /* If we stopped on whitespace, then replace the whitespace with null for
8134 the call to hash_find. Save the character we replaced just in case we
8135 have to re-parse the instruction. */
3882b010 8136 if (ISSPACE (*s))
252b5132
RH
8137 {
8138 save_c = *s;
8139 *s++ = '\0';
8140 }
bdaaa2e1 8141
252b5132
RH
8142 insn = (struct mips_opcode *) hash_find (op_hash, str);
8143
8144 /* If we didn't find the instruction in the opcode table, try again, but
8145 this time with just the instruction up to, but not including the
8146 first '.'. */
8147 if (insn == NULL)
8148 {
bdaaa2e1 8149 /* Restore the character we overwrite above (if any). */
252b5132
RH
8150 if (save_c)
8151 *(--s) = save_c;
8152
8153 /* Scan up to the first '.' or whitespace. */
3882b010
L
8154 for (s = str;
8155 *s != '\0' && *s != '.' && !ISSPACE (*s);
8156 ++s)
252b5132
RH
8157 continue;
8158
8159 /* If we did not find a '.', then we can quit now. */
8160 if (*s != '.')
8161 {
8162 insn_error = "unrecognized opcode";
8163 return;
8164 }
8165
8166 /* Lookup the instruction in the hash table. */
8167 *s++ = '\0';
8168 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8169 {
8170 insn_error = "unrecognized opcode";
8171 return;
8172 }
252b5132
RH
8173 }
8174
8175 argsStart = s;
8176 for (;;)
8177 {
b34976b6 8178 bfd_boolean ok;
252b5132
RH
8179
8180 assert (strcmp (insn->name, str) == 0);
8181
1f25f5d3
CD
8182 if (OPCODE_IS_MEMBER (insn,
8183 (mips_opts.isa
3396de36 8184 | (file_ase_mips16 ? INSN_MIPS16 : 0)
deec1734 8185 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 8186 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
1f25f5d3 8187 mips_arch))
b34976b6 8188 ok = TRUE;
bdaaa2e1 8189 else
b34976b6 8190 ok = FALSE;
bdaaa2e1 8191
252b5132
RH
8192 if (insn->pinfo != INSN_MACRO)
8193 {
ec68c924 8194 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
b34976b6 8195 ok = FALSE;
252b5132
RH
8196 }
8197
8198 if (! ok)
8199 {
8200 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8201 && strcmp (insn->name, insn[1].name) == 0)
8202 {
8203 ++insn;
8204 continue;
8205 }
252b5132 8206 else
beae10d5 8207 {
268f6bed
L
8208 if (!insn_error)
8209 {
8210 static char buf[100];
316f5878
RS
8211 if (mips_arch_info->is_isa)
8212 sprintf (buf,
8213 _("opcode not supported at this ISA level (%s)"),
8214 mips_cpu_info_from_isa (mips_opts.isa)->name);
8215 else
8216 sprintf (buf,
8217 _("opcode not supported on this processor: %s (%s)"),
8218 mips_arch_info->name,
8219 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8220 insn_error = buf;
8221 }
8222 if (save_c)
8223 *(--s) = save_c;
2bd7f1f3 8224 return;
252b5132 8225 }
252b5132
RH
8226 }
8227
8228 ip->insn_mo = insn;
8229 ip->insn_opcode = insn->match;
268f6bed 8230 insn_error = NULL;
252b5132
RH
8231 for (args = insn->args;; ++args)
8232 {
deec1734
CD
8233 int is_mdmx;
8234
ad8d3bb3 8235 s += strspn (s, " \t");
deec1734 8236 is_mdmx = 0;
252b5132
RH
8237 switch (*args)
8238 {
8239 case '\0': /* end of args */
8240 if (*s == '\0')
8241 return;
8242 break;
8243
8244 case ',':
8245 if (*s++ == *args)
8246 continue;
8247 s--;
8248 switch (*++args)
8249 {
8250 case 'r':
8251 case 'v':
38487616 8252 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8253 continue;
8254
8255 case 'w':
38487616
TS
8256 ip->insn_opcode |= lastregno << OP_SH_RT;
8257 continue;
8258
252b5132 8259 case 'W':
38487616 8260 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8261 continue;
8262
8263 case 'V':
38487616 8264 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8265 continue;
8266 }
8267 break;
8268
8269 case '(':
8270 /* Handle optional base register.
8271 Either the base register is omitted or
bdaaa2e1 8272 we must have a left paren. */
252b5132
RH
8273 /* This is dependent on the next operand specifier
8274 is a base register specification. */
8275 assert (args[1] == 'b' || args[1] == '5'
8276 || args[1] == '-' || args[1] == '4');
8277 if (*s == '\0')
8278 return;
8279
8280 case ')': /* these must match exactly */
60b63b72
RS
8281 case '[':
8282 case ']':
252b5132
RH
8283 if (*s++ == *args)
8284 continue;
8285 break;
8286
af7ee8bf
CD
8287 case '+': /* Opcode extension character. */
8288 switch (*++args)
8289 {
071742cf
CD
8290 case 'A': /* ins/ext position, becomes LSB. */
8291 limlo = 0;
8292 limhi = 31;
8293 my_getExpression (&imm_expr, s);
8294 check_absolute_expr (ip, &imm_expr);
8295 if ((unsigned long) imm_expr.X_add_number < limlo
8296 || (unsigned long) imm_expr.X_add_number > limhi)
8297 {
8298 as_bad (_("Improper position (%lu)"),
8299 (unsigned long) imm_expr.X_add_number);
8300 imm_expr.X_add_number = limlo;
8301 }
8302 lastpos = imm_expr.X_add_number;
8303 ip->insn_opcode |= (imm_expr.X_add_number
8304 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8305 imm_expr.X_op = O_absent;
8306 s = expr_end;
8307 continue;
8308
8309 case 'B': /* ins size, becomes MSB. */
8310 limlo = 1;
8311 limhi = 32;
8312 my_getExpression (&imm_expr, s);
8313 check_absolute_expr (ip, &imm_expr);
8314 /* Check for negative input so that small negative numbers
8315 will not succeed incorrectly. The checks against
8316 (pos+size) transitively check "size" itself,
8317 assuming that "pos" is reasonable. */
8318 if ((long) imm_expr.X_add_number < 0
8319 || ((unsigned long) imm_expr.X_add_number
8320 + lastpos) < limlo
8321 || ((unsigned long) imm_expr.X_add_number
8322 + lastpos) > limhi)
8323 {
8324 as_bad (_("Improper insert size (%lu, position %lu)"),
8325 (unsigned long) imm_expr.X_add_number,
8326 (unsigned long) lastpos);
8327 imm_expr.X_add_number = limlo - lastpos;
8328 }
8329 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8330 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8331 imm_expr.X_op = O_absent;
8332 s = expr_end;
8333 continue;
8334
8335 case 'C': /* ext size, becomes MSBD. */
8336 limlo = 1;
8337 limhi = 32;
8338 my_getExpression (&imm_expr, s);
8339 check_absolute_expr (ip, &imm_expr);
8340 /* Check for negative input so that small negative numbers
8341 will not succeed incorrectly. The checks against
8342 (pos+size) transitively check "size" itself,
8343 assuming that "pos" is reasonable. */
8344 if ((long) imm_expr.X_add_number < 0
8345 || ((unsigned long) imm_expr.X_add_number
8346 + lastpos) < limlo
8347 || ((unsigned long) imm_expr.X_add_number
8348 + lastpos) > limhi)
8349 {
8350 as_bad (_("Improper extract size (%lu, position %lu)"),
8351 (unsigned long) imm_expr.X_add_number,
8352 (unsigned long) lastpos);
8353 imm_expr.X_add_number = limlo - lastpos;
8354 }
8355 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8356 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8357 imm_expr.X_op = O_absent;
8358 s = expr_end;
8359 continue;
af7ee8bf 8360
bbcc0807
CD
8361 case 'D':
8362 /* +D is for disassembly only; never match. */
8363 break;
8364
af7ee8bf
CD
8365 default:
8366 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8367 *args, insn->name, insn->args);
8368 /* Further processing is fruitless. */
8369 return;
8370 }
8371 break;
8372
252b5132
RH
8373 case '<': /* must be at least one digit */
8374 /*
8375 * According to the manual, if the shift amount is greater
b6ff326e
KH
8376 * than 31 or less than 0, then the shift amount should be
8377 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8378 * We issue a warning and mask out all but the low 5 bits.
8379 */
8380 my_getExpression (&imm_expr, s);
8381 check_absolute_expr (ip, &imm_expr);
8382 if ((unsigned long) imm_expr.X_add_number > 31)
8383 {
793b27f4
TS
8384 as_warn (_("Improper shift amount (%lu)"),
8385 (unsigned long) imm_expr.X_add_number);
38487616 8386 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 8387 }
38487616 8388 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
8389 imm_expr.X_op = O_absent;
8390 s = expr_end;
8391 continue;
8392
8393 case '>': /* shift amount minus 32 */
8394 my_getExpression (&imm_expr, s);
8395 check_absolute_expr (ip, &imm_expr);
8396 if ((unsigned long) imm_expr.X_add_number < 32
8397 || (unsigned long) imm_expr.X_add_number > 63)
8398 break;
38487616 8399 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
8400 imm_expr.X_op = O_absent;
8401 s = expr_end;
8402 continue;
8403
252b5132
RH
8404 case 'k': /* cache code */
8405 case 'h': /* prefx code */
8406 my_getExpression (&imm_expr, s);
8407 check_absolute_expr (ip, &imm_expr);
8408 if ((unsigned long) imm_expr.X_add_number > 31)
8409 {
8410 as_warn (_("Invalid value for `%s' (%lu)"),
8411 ip->insn_mo->name,
8412 (unsigned long) imm_expr.X_add_number);
8413 imm_expr.X_add_number &= 0x1f;
8414 }
8415 if (*args == 'k')
8416 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8417 else
8418 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8419 imm_expr.X_op = O_absent;
8420 s = expr_end;
8421 continue;
8422
8423 case 'c': /* break code */
8424 my_getExpression (&imm_expr, s);
8425 check_absolute_expr (ip, &imm_expr);
793b27f4 8426 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8427 {
793b27f4
TS
8428 as_warn (_("Illegal break code (%lu)"),
8429 (unsigned long) imm_expr.X_add_number);
38487616 8430 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 8431 }
38487616 8432 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
8433 imm_expr.X_op = O_absent;
8434 s = expr_end;
8435 continue;
8436
8437 case 'q': /* lower break code */
8438 my_getExpression (&imm_expr, s);
8439 check_absolute_expr (ip, &imm_expr);
793b27f4 8440 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8441 {
793b27f4
TS
8442 as_warn (_("Illegal lower break code (%lu)"),
8443 (unsigned long) imm_expr.X_add_number);
38487616 8444 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 8445 }
38487616 8446 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
8447 imm_expr.X_op = O_absent;
8448 s = expr_end;
8449 continue;
8450
4372b673 8451 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8452 my_getExpression (&imm_expr, s);
156c2f8b 8453 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8454 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8455 as_warn (_("Illegal 20-bit code (%lu)"),
8456 (unsigned long) imm_expr.X_add_number);
38487616 8457 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
8458 imm_expr.X_op = O_absent;
8459 s = expr_end;
8460 continue;
8461
98d3f06f 8462 case 'C': /* Coprocessor code */
beae10d5 8463 my_getExpression (&imm_expr, s);
252b5132 8464 check_absolute_expr (ip, &imm_expr);
98d3f06f 8465 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 8466 {
793b27f4
TS
8467 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8468 (unsigned long) imm_expr.X_add_number);
98d3f06f 8469 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 8470 }
beae10d5
KH
8471 ip->insn_opcode |= imm_expr.X_add_number;
8472 imm_expr.X_op = O_absent;
8473 s = expr_end;
8474 continue;
252b5132 8475
4372b673
NC
8476 case 'J': /* 19-bit wait code. */
8477 my_getExpression (&imm_expr, s);
8478 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8479 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8480 as_warn (_("Illegal 19-bit code (%lu)"),
8481 (unsigned long) imm_expr.X_add_number);
38487616 8482 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
8483 imm_expr.X_op = O_absent;
8484 s = expr_end;
8485 continue;
8486
252b5132 8487 case 'P': /* Performance register */
beae10d5 8488 my_getExpression (&imm_expr, s);
252b5132 8489 check_absolute_expr (ip, &imm_expr);
beae10d5 8490 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 8491 {
793b27f4
TS
8492 as_warn (_("Invalid performance register (%lu)"),
8493 (unsigned long) imm_expr.X_add_number);
38487616 8494 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 8495 }
38487616 8496 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
8497 imm_expr.X_op = O_absent;
8498 s = expr_end;
8499 continue;
252b5132
RH
8500
8501 case 'b': /* base register */
8502 case 'd': /* destination register */
8503 case 's': /* source register */
8504 case 't': /* target register */
8505 case 'r': /* both target and source */
8506 case 'v': /* both dest and source */
8507 case 'w': /* both dest and target */
8508 case 'E': /* coprocessor target register */
8509 case 'G': /* coprocessor destination register */
af7ee8bf 8510 case 'K': /* 'rdhwr' destination register */
252b5132
RH
8511 case 'x': /* ignore register name */
8512 case 'z': /* must be zero register */
4372b673 8513 case 'U': /* destination register (clo/clz). */
252b5132
RH
8514 s_reset = s;
8515 if (s[0] == '$')
8516 {
8517
3882b010 8518 if (ISDIGIT (s[1]))
252b5132
RH
8519 {
8520 ++s;
8521 regno = 0;
8522 do
8523 {
8524 regno *= 10;
8525 regno += *s - '0';
8526 ++s;
8527 }
3882b010 8528 while (ISDIGIT (*s));
252b5132
RH
8529 if (regno > 31)
8530 as_bad (_("Invalid register number (%d)"), regno);
8531 }
af7ee8bf 8532 else if (*args == 'E' || *args == 'G' || *args == 'K')
252b5132
RH
8533 goto notreg;
8534 else
8535 {
76db943d
TS
8536 if (s[1] == 'r' && s[2] == 'a')
8537 {
8538 s += 3;
8539 regno = RA;
8540 }
8541 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8542 {
8543 s += 3;
8544 regno = FP;
8545 }
8546 else if (s[1] == 's' && s[2] == 'p')
8547 {
8548 s += 3;
8549 regno = SP;
8550 }
8551 else if (s[1] == 'g' && s[2] == 'p')
8552 {
8553 s += 3;
8554 regno = GP;
8555 }
8556 else if (s[1] == 'a' && s[2] == 't')
8557 {
8558 s += 3;
8559 regno = AT;
8560 }
8561 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8562 {
8563 s += 4;
8564 regno = KT0;
8565 }
8566 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8567 {
8568 s += 4;
8569 regno = KT1;
8570 }
85b51719
TS
8571 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8572 {
8573 s += 5;
8574 regno = ZERO;
8575 }
252b5132
RH
8576 else if (itbl_have_entries)
8577 {
8578 char *p, *n;
d7ba4a77 8579 unsigned long r;
252b5132 8580
d7ba4a77 8581 p = s + 1; /* advance past '$' */
252b5132
RH
8582 n = itbl_get_field (&p); /* n is name */
8583
d7ba4a77
ILT
8584 /* See if this is a register defined in an
8585 itbl entry. */
8586 if (itbl_get_reg_val (n, &r))
252b5132
RH
8587 {
8588 /* Get_field advances to the start of
8589 the next field, so we need to back
d7ba4a77 8590 rack to the end of the last field. */
bdaaa2e1 8591 if (p)
252b5132 8592 s = p - 1;
bdaaa2e1 8593 else
d7ba4a77 8594 s = strchr (s, '\0');
252b5132
RH
8595 regno = r;
8596 }
8597 else
8598 goto notreg;
beae10d5 8599 }
252b5132
RH
8600 else
8601 goto notreg;
8602 }
8603 if (regno == AT
8604 && ! mips_opts.noat
8605 && *args != 'E'
af7ee8bf
CD
8606 && *args != 'G'
8607 && *args != 'K')
252b5132
RH
8608 as_warn (_("Used $at without \".set noat\""));
8609 c = *args;
8610 if (*s == ' ')
f9419b05 8611 ++s;
252b5132
RH
8612 if (args[1] != *s)
8613 {
8614 if (c == 'r' || c == 'v' || c == 'w')
8615 {
8616 regno = lastregno;
8617 s = s_reset;
f9419b05 8618 ++args;
252b5132
RH
8619 }
8620 }
8621 /* 'z' only matches $0. */
8622 if (c == 'z' && regno != 0)
8623 break;
8624
bdaaa2e1
KH
8625 /* Now that we have assembled one operand, we use the args string
8626 * to figure out where it goes in the instruction. */
252b5132
RH
8627 switch (c)
8628 {
8629 case 'r':
8630 case 's':
8631 case 'v':
8632 case 'b':
38487616 8633 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8634 break;
8635 case 'd':
8636 case 'G':
af7ee8bf 8637 case 'K':
38487616 8638 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8639 break;
4372b673 8640 case 'U':
38487616
TS
8641 ip->insn_opcode |= regno << OP_SH_RD;
8642 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8643 break;
252b5132
RH
8644 case 'w':
8645 case 't':
8646 case 'E':
38487616 8647 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8648 break;
8649 case 'x':
8650 /* This case exists because on the r3000 trunc
8651 expands into a macro which requires a gp
8652 register. On the r6000 or r4000 it is
8653 assembled into a single instruction which
8654 ignores the register. Thus the insn version
8655 is MIPS_ISA2 and uses 'x', and the macro
8656 version is MIPS_ISA1 and uses 't'. */
8657 break;
8658 case 'z':
8659 /* This case is for the div instruction, which
8660 acts differently if the destination argument
8661 is $0. This only matches $0, and is checked
8662 outside the switch. */
8663 break;
8664 case 'D':
8665 /* Itbl operand; not yet implemented. FIXME ?? */
8666 break;
8667 /* What about all other operands like 'i', which
8668 can be specified in the opcode table? */
8669 }
8670 lastregno = regno;
8671 continue;
8672 }
8673 notreg:
8674 switch (*args++)
8675 {
8676 case 'r':
8677 case 'v':
38487616 8678 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8679 continue;
8680 case 'w':
38487616 8681 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8682 continue;
8683 }
8684 break;
8685
deec1734
CD
8686 case 'O': /* MDMX alignment immediate constant. */
8687 my_getExpression (&imm_expr, s);
8688 check_absolute_expr (ip, &imm_expr);
8689 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8690 {
8691 as_warn ("Improper align amount (%ld), using low bits",
8692 (long) imm_expr.X_add_number);
8693 imm_expr.X_add_number &= OP_MASK_ALN;
8694 }
8695 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8696 imm_expr.X_op = O_absent;
8697 s = expr_end;
8698 continue;
8699
8700 case 'Q': /* MDMX vector, element sel, or const. */
8701 if (s[0] != '$')
8702 {
8703 /* MDMX Immediate. */
8704 my_getExpression (&imm_expr, s);
8705 check_absolute_expr (ip, &imm_expr);
8706 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8707 {
8708 as_warn (_("Invalid MDMX Immediate (%ld)"),
8709 (long) imm_expr.X_add_number);
8710 imm_expr.X_add_number &= OP_MASK_FT;
8711 }
8712 imm_expr.X_add_number &= OP_MASK_FT;
8713 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8714 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8715 else
8716 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8717 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8718 imm_expr.X_op = O_absent;
8719 s = expr_end;
8720 continue;
8721 }
8722 /* Not MDMX Immediate. Fall through. */
8723 case 'X': /* MDMX destination register. */
8724 case 'Y': /* MDMX source register. */
8725 case 'Z': /* MDMX target register. */
8726 is_mdmx = 1;
252b5132
RH
8727 case 'D': /* floating point destination register */
8728 case 'S': /* floating point source register */
8729 case 'T': /* floating point target register */
8730 case 'R': /* floating point source register */
8731 case 'V':
8732 case 'W':
8733 s_reset = s;
deec1734
CD
8734 /* Accept $fN for FP and MDMX register numbers, and in
8735 addition accept $vN for MDMX register numbers. */
8736 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8737 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8738 && ISDIGIT (s[2])))
252b5132
RH
8739 {
8740 s += 2;
8741 regno = 0;
8742 do
8743 {
8744 regno *= 10;
8745 regno += *s - '0';
8746 ++s;
8747 }
3882b010 8748 while (ISDIGIT (*s));
252b5132
RH
8749
8750 if (regno > 31)
8751 as_bad (_("Invalid float register number (%d)"), regno);
8752
8753 if ((regno & 1) != 0
ca4e0257 8754 && HAVE_32BIT_FPRS
252b5132
RH
8755 && ! (strcmp (str, "mtc1") == 0
8756 || strcmp (str, "mfc1") == 0
8757 || strcmp (str, "lwc1") == 0
8758 || strcmp (str, "swc1") == 0
8759 || strcmp (str, "l.s") == 0
8760 || strcmp (str, "s.s") == 0))
8761 as_warn (_("Float register should be even, was %d"),
8762 regno);
8763
8764 c = *args;
8765 if (*s == ' ')
f9419b05 8766 ++s;
252b5132
RH
8767 if (args[1] != *s)
8768 {
8769 if (c == 'V' || c == 'W')
8770 {
8771 regno = lastregno;
8772 s = s_reset;
f9419b05 8773 ++args;
252b5132
RH
8774 }
8775 }
8776 switch (c)
8777 {
8778 case 'D':
deec1734 8779 case 'X':
38487616 8780 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8781 break;
8782 case 'V':
8783 case 'S':
deec1734 8784 case 'Y':
38487616 8785 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 8786 break;
deec1734
CD
8787 case 'Q':
8788 /* This is like 'Z', but also needs to fix the MDMX
8789 vector/scalar select bits. Note that the
8790 scalar immediate case is handled above. */
8791 if (*s == '[')
8792 {
8793 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8794 int max_el = (is_qh ? 3 : 7);
8795 s++;
8796 my_getExpression(&imm_expr, s);
8797 check_absolute_expr (ip, &imm_expr);
8798 s = expr_end;
8799 if (imm_expr.X_add_number > max_el)
8800 as_bad(_("Bad element selector %ld"),
8801 (long) imm_expr.X_add_number);
8802 imm_expr.X_add_number &= max_el;
8803 ip->insn_opcode |= (imm_expr.X_add_number
8804 << (OP_SH_VSEL +
8805 (is_qh ? 2 : 1)));
8806 if (*s != ']')
8807 as_warn(_("Expecting ']' found '%s'"), s);
8808 else
8809 s++;
8810 }
8811 else
8812 {
8813 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8814 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8815 << OP_SH_VSEL);
8816 else
8817 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8818 OP_SH_VSEL);
8819 }
8820 /* Fall through */
252b5132
RH
8821 case 'W':
8822 case 'T':
deec1734 8823 case 'Z':
38487616 8824 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
8825 break;
8826 case 'R':
38487616 8827 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
8828 break;
8829 }
8830 lastregno = regno;
8831 continue;
8832 }
8833
252b5132
RH
8834 switch (*args++)
8835 {
8836 case 'V':
38487616 8837 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8838 continue;
8839 case 'W':
38487616 8840 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8841 continue;
8842 }
8843 break;
8844
8845 case 'I':
8846 my_getExpression (&imm_expr, s);
8847 if (imm_expr.X_op != O_big
8848 && imm_expr.X_op != O_constant)
8849 insn_error = _("absolute expression required");
8850 s = expr_end;
8851 continue;
8852
8853 case 'A':
8854 my_getExpression (&offset_expr, s);
f6688943 8855 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8856 s = expr_end;
8857 continue;
8858
8859 case 'F':
8860 case 'L':
8861 case 'f':
8862 case 'l':
8863 {
8864 int f64;
ca4e0257 8865 int using_gprs;
252b5132
RH
8866 char *save_in;
8867 char *err;
8868 unsigned char temp[8];
8869 int len;
8870 unsigned int length;
8871 segT seg;
8872 subsegT subseg;
8873 char *p;
8874
8875 /* These only appear as the last operand in an
8876 instruction, and every instruction that accepts
8877 them in any variant accepts them in all variants.
8878 This means we don't have to worry about backing out
8879 any changes if the instruction does not match.
8880
8881 The difference between them is the size of the
8882 floating point constant and where it goes. For 'F'
8883 and 'L' the constant is 64 bits; for 'f' and 'l' it
8884 is 32 bits. Where the constant is placed is based
8885 on how the MIPS assembler does things:
8886 F -- .rdata
8887 L -- .lit8
8888 f -- immediate value
8889 l -- .lit4
8890
8891 The .lit4 and .lit8 sections are only used if
8892 permitted by the -G argument.
8893
8894 When generating embedded PIC code, we use the
8895 .lit8 section but not the .lit4 section (we can do
8896 .lit4 inline easily; we need to put .lit8
8897 somewhere in the data segment, and using .lit8
8898 permits the linker to eventually combine identical
ca4e0257
RS
8899 .lit8 entries).
8900
8901 The code below needs to know whether the target register
8902 is 32 or 64 bits wide. It relies on the fact 'f' and
8903 'F' are used with GPR-based instructions and 'l' and
8904 'L' are used with FPR-based instructions. */
252b5132
RH
8905
8906 f64 = *args == 'F' || *args == 'L';
ca4e0257 8907 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8908
8909 save_in = input_line_pointer;
8910 input_line_pointer = s;
8911 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8912 length = len;
8913 s = input_line_pointer;
8914 input_line_pointer = save_in;
8915 if (err != NULL && *err != '\0')
8916 {
8917 as_bad (_("Bad floating point constant: %s"), err);
8918 memset (temp, '\0', sizeof temp);
8919 length = f64 ? 8 : 4;
8920 }
8921
156c2f8b 8922 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8923
8924 if (*args == 'f'
8925 || (*args == 'l'
8926 && (! USE_GLOBAL_POINTER_OPT
8927 || mips_pic == EMBEDDED_PIC
8928 || g_switch_value < 4
8929 || (temp[0] == 0 && temp[1] == 0)
8930 || (temp[2] == 0 && temp[3] == 0))))
8931 {
8932 imm_expr.X_op = O_constant;
8933 if (! target_big_endian)
8934 imm_expr.X_add_number = bfd_getl32 (temp);
8935 else
8936 imm_expr.X_add_number = bfd_getb32 (temp);
8937 }
8938 else if (length > 4
119d663a 8939 && ! mips_disable_float_construction
ca4e0257
RS
8940 /* Constants can only be constructed in GPRs and
8941 copied to FPRs if the GPRs are at least as wide
8942 as the FPRs. Force the constant into memory if
8943 we are using 64-bit FPRs but the GPRs are only
8944 32 bits wide. */
8945 && (using_gprs
8946 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
8947 && ((temp[0] == 0 && temp[1] == 0)
8948 || (temp[2] == 0 && temp[3] == 0))
8949 && ((temp[4] == 0 && temp[5] == 0)
8950 || (temp[6] == 0 && temp[7] == 0)))
8951 {
ca4e0257
RS
8952 /* The value is simple enough to load with a couple of
8953 instructions. If using 32-bit registers, set
8954 imm_expr to the high order 32 bits and offset_expr to
8955 the low order 32 bits. Otherwise, set imm_expr to
8956 the entire 64 bit constant. */
8957 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
8958 {
8959 imm_expr.X_op = O_constant;
8960 offset_expr.X_op = O_constant;
8961 if (! target_big_endian)
8962 {
8963 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8964 offset_expr.X_add_number = bfd_getl32 (temp);
8965 }
8966 else
8967 {
8968 imm_expr.X_add_number = bfd_getb32 (temp);
8969 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8970 }
8971 if (offset_expr.X_add_number == 0)
8972 offset_expr.X_op = O_absent;
8973 }
8974 else if (sizeof (imm_expr.X_add_number) > 4)
8975 {
8976 imm_expr.X_op = O_constant;
8977 if (! target_big_endian)
8978 imm_expr.X_add_number = bfd_getl64 (temp);
8979 else
8980 imm_expr.X_add_number = bfd_getb64 (temp);
8981 }
8982 else
8983 {
8984 imm_expr.X_op = O_big;
8985 imm_expr.X_add_number = 4;
8986 if (! target_big_endian)
8987 {
8988 generic_bignum[0] = bfd_getl16 (temp);
8989 generic_bignum[1] = bfd_getl16 (temp + 2);
8990 generic_bignum[2] = bfd_getl16 (temp + 4);
8991 generic_bignum[3] = bfd_getl16 (temp + 6);
8992 }
8993 else
8994 {
8995 generic_bignum[0] = bfd_getb16 (temp + 6);
8996 generic_bignum[1] = bfd_getb16 (temp + 4);
8997 generic_bignum[2] = bfd_getb16 (temp + 2);
8998 generic_bignum[3] = bfd_getb16 (temp);
8999 }
9000 }
9001 }
9002 else
9003 {
9004 const char *newname;
9005 segT new_seg;
9006
9007 /* Switch to the right section. */
9008 seg = now_seg;
9009 subseg = now_subseg;
9010 switch (*args)
9011 {
9012 default: /* unused default case avoids warnings. */
9013 case 'L':
9014 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
9015 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9016 || mips_pic == EMBEDDED_PIC)
252b5132
RH
9017 newname = ".lit8";
9018 break;
9019 case 'F':
bb2d6cd7
GK
9020 if (mips_pic == EMBEDDED_PIC)
9021 newname = ".lit8";
9022 else
9023 newname = RDATA_SECTION_NAME;
252b5132
RH
9024 break;
9025 case 'l':
9026 assert (!USE_GLOBAL_POINTER_OPT
9027 || g_switch_value >= 4);
9028 newname = ".lit4";
9029 break;
9030 }
9031 new_seg = subseg_new (newname, (subsegT) 0);
9032 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9033 bfd_set_section_flags (stdoutput, new_seg,
9034 (SEC_ALLOC
9035 | SEC_LOAD
9036 | SEC_READONLY
9037 | SEC_DATA));
9038 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9039 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9040 && strcmp (TARGET_OS, "elf") != 0)
9041 record_alignment (new_seg, 4);
9042 else
9043 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9044 if (seg == now_seg)
9045 as_bad (_("Can't use floating point insn in this section"));
9046
9047 /* Set the argument to the current address in the
9048 section. */
9049 offset_expr.X_op = O_symbol;
9050 offset_expr.X_add_symbol =
9051 symbol_new ("L0\001", now_seg,
9052 (valueT) frag_now_fix (), frag_now);
9053 offset_expr.X_add_number = 0;
9054
9055 /* Put the floating point number into the section. */
9056 p = frag_more ((int) length);
9057 memcpy (p, temp, length);
9058
9059 /* Switch back to the original section. */
9060 subseg_set (seg, subseg);
9061 }
9062 }
9063 continue;
9064
9065 case 'i': /* 16 bit unsigned immediate */
9066 case 'j': /* 16 bit signed immediate */
f6688943 9067 *imm_reloc = BFD_RELOC_LO16;
252b5132 9068 c = my_getSmallExpression (&imm_expr, s);
fb1b3232 9069 if (c != S_EX_NONE)
252b5132 9070 {
fb1b3232 9071 if (c != S_EX_LO)
252b5132 9072 {
fdb987ee
RS
9073 if (c == S_EX_HI)
9074 {
9075 *imm_reloc = BFD_RELOC_HI16_S;
b34976b6 9076 imm_unmatched_hi = TRUE;
fdb987ee 9077 }
ad8d3bb3 9078#ifdef OBJ_ELF
fb1b3232 9079 else if (c == S_EX_HIGHEST)
98d3f06f 9080 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
fb1b3232 9081 else if (c == S_EX_HIGHER)
98d3f06f 9082 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
ad8d3bb3
TS
9083 else if (c == S_EX_GP_REL)
9084 {
9085 /* This occurs in NewABI only. */
9086 c = my_getSmallExpression (&imm_expr, s);
9087 if (c != S_EX_NEG)
9088 as_bad (_("bad composition of relocations"));
9089 else
9090 {
9091 c = my_getSmallExpression (&imm_expr, s);
9092 if (c != S_EX_LO)
9093 as_bad (_("bad composition of relocations"));
9094 else
9095 {
9096 imm_reloc[0] = BFD_RELOC_GPREL16;
9097 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
9098 imm_reloc[2] = BFD_RELOC_LO16;
9099 }
9100 }
9101 }
9102#endif
252b5132 9103 else
f6688943 9104 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
9105 }
9106 else if (imm_expr.X_op == O_constant)
9107 imm_expr.X_add_number &= 0xffff;
9108 }
9109 if (*args == 'i')
9110 {
fb1b3232 9111 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 9112 || ((imm_expr.X_add_number < 0
beae10d5
KH
9113 || imm_expr.X_add_number >= 0x10000)
9114 && imm_expr.X_op == O_constant))
252b5132
RH
9115 {
9116 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9117 !strcmp (insn->name, insn[1].name))
9118 break;
2ae7e77b
AH
9119 if (imm_expr.X_op == O_constant
9120 || imm_expr.X_op == O_big)
252b5132
RH
9121 as_bad (_("16 bit expression not in range 0..65535"));
9122 }
9123 }
9124 else
9125 {
9126 int more;
9127 offsetT max;
9128
9129 /* The upper bound should be 0x8000, but
9130 unfortunately the MIPS assembler accepts numbers
9131 from 0x8000 to 0xffff and sign extends them, and
9132 we want to be compatible. We only permit this
9133 extended range for an instruction which does not
9134 provide any further alternates, since those
9135 alternates may handle other cases. People should
9136 use the numbers they mean, rather than relying on
9137 a mysterious sign extension. */
9138 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9139 strcmp (insn->name, insn[1].name) == 0);
9140 if (more)
9141 max = 0x8000;
9142 else
9143 max = 0x10000;
fb1b3232 9144 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 9145 || ((imm_expr.X_add_number < -0x8000
beae10d5
KH
9146 || imm_expr.X_add_number >= max)
9147 && imm_expr.X_op == O_constant)
252b5132
RH
9148 || (more
9149 && imm_expr.X_add_number < 0
ca4e0257 9150 && HAVE_64BIT_GPRS
252b5132
RH
9151 && imm_expr.X_unsigned
9152 && sizeof (imm_expr.X_add_number) <= 4))
9153 {
9154 if (more)
9155 break;
2ae7e77b
AH
9156 if (imm_expr.X_op == O_constant
9157 || imm_expr.X_op == O_big)
252b5132
RH
9158 as_bad (_("16 bit expression not in range -32768..32767"));
9159 }
9160 }
9161 s = expr_end;
9162 continue;
9163
9164 case 'o': /* 16 bit offset */
9165 c = my_getSmallExpression (&offset_expr, s);
9166
9167 /* If this value won't fit into a 16 bit offset, then go
9168 find a macro that will generate the 32 bit offset
afdbd6d0 9169 code pattern. */
fb1b3232 9170 if (c == S_EX_NONE
252b5132
RH
9171 && (offset_expr.X_op != O_constant
9172 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9173 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9174 break;
9175
fb1b3232 9176 if (c == S_EX_HI)
252b5132
RH
9177 {
9178 if (offset_expr.X_op != O_constant)
9179 break;
9180 offset_expr.X_add_number =
9181 (offset_expr.X_add_number >> 16) & 0xffff;
9182 }
f6688943 9183 *offset_reloc = BFD_RELOC_LO16;
252b5132
RH
9184 s = expr_end;
9185 continue;
9186
9187 case 'p': /* pc relative offset */
af6ae2ad 9188 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9189 my_getExpression (&offset_expr, s);
9190 s = expr_end;
9191 continue;
9192
9193 case 'u': /* upper 16 bits */
9194 c = my_getSmallExpression (&imm_expr, s);
f6688943 9195 *imm_reloc = BFD_RELOC_LO16;
e7d556df 9196 if (c != S_EX_NONE)
252b5132 9197 {
fb1b3232 9198 if (c != S_EX_LO)
252b5132 9199 {
fdb987ee 9200 if (c == S_EX_HI)
252b5132 9201 {
f6688943 9202 *imm_reloc = BFD_RELOC_HI16_S;
b34976b6 9203 imm_unmatched_hi = TRUE;
252b5132 9204 }
645dc66c
TS
9205#ifdef OBJ_ELF
9206 else if (c == S_EX_HIGHEST)
98d3f06f 9207 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
ad8d3bb3
TS
9208 else if (c == S_EX_GP_REL)
9209 {
9210 /* This occurs in NewABI only. */
9211 c = my_getSmallExpression (&imm_expr, s);
9212 if (c != S_EX_NEG)
9213 as_bad (_("bad composition of relocations"));
9214 else
9215 {
9216 c = my_getSmallExpression (&imm_expr, s);
9217 if (c != S_EX_HI)
9218 as_bad (_("bad composition of relocations"));
9219 else
9220 {
9221 imm_reloc[0] = BFD_RELOC_GPREL16;
9222 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
9223 imm_reloc[2] = BFD_RELOC_HI16_S;
9224 }
9225 }
9226 }
9227#endif
252b5132 9228 else
f6688943 9229 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
9230 }
9231 else if (imm_expr.X_op == O_constant)
9232 imm_expr.X_add_number &= 0xffff;
9233 }
fdb987ee
RS
9234 else if (imm_expr.X_op == O_constant
9235 && (imm_expr.X_add_number < 0
9236 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9237 as_bad (_("lui expression not in range 0..65535"));
9238 s = expr_end;
9239 continue;
9240
9241 case 'a': /* 26 bit address */
9242 my_getExpression (&offset_expr, s);
9243 s = expr_end;
f6688943 9244 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
9245 continue;
9246
9247 case 'N': /* 3 bit branch condition code */
9248 case 'M': /* 3 bit compare condition code */
9249 if (strncmp (s, "$fcc", 4) != 0)
9250 break;
9251 s += 4;
9252 regno = 0;
9253 do
9254 {
9255 regno *= 10;
9256 regno += *s - '0';
9257 ++s;
9258 }
3882b010 9259 while (ISDIGIT (*s));
252b5132
RH
9260 if (regno > 7)
9261 as_bad (_("invalid condition code register $fcc%d"), regno);
9262 if (*args == 'N')
9263 ip->insn_opcode |= regno << OP_SH_BCC;
9264 else
9265 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 9266 continue;
252b5132 9267
156c2f8b
NC
9268 case 'H':
9269 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9270 s += 2;
3882b010 9271 if (ISDIGIT (*s))
156c2f8b
NC
9272 {
9273 c = 0;
9274 do
9275 {
9276 c *= 10;
9277 c += *s - '0';
9278 ++s;
9279 }
3882b010 9280 while (ISDIGIT (*s));
156c2f8b
NC
9281 }
9282 else
9283 c = 8; /* Invalid sel value. */
9284
9285 if (c > 7)
9286 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9287 ip->insn_opcode |= c;
9288 continue;
9289
60b63b72
RS
9290 case 'e':
9291 /* Must be at least one digit. */
9292 my_getExpression (&imm_expr, s);
9293 check_absolute_expr (ip, &imm_expr);
9294
9295 if ((unsigned long) imm_expr.X_add_number
9296 > (unsigned long) OP_MASK_VECBYTE)
9297 {
9298 as_bad (_("bad byte vector index (%ld)"),
9299 (long) imm_expr.X_add_number);
9300 imm_expr.X_add_number = 0;
9301 }
9302
9303 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9304 imm_expr.X_op = O_absent;
9305 s = expr_end;
9306 continue;
9307
9308 case '%':
9309 my_getExpression (&imm_expr, s);
9310 check_absolute_expr (ip, &imm_expr);
9311
9312 if ((unsigned long) imm_expr.X_add_number
9313 > (unsigned long) OP_MASK_VECALIGN)
9314 {
9315 as_bad (_("bad byte vector index (%ld)"),
9316 (long) imm_expr.X_add_number);
9317 imm_expr.X_add_number = 0;
9318 }
9319
9320 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9321 imm_expr.X_op = O_absent;
9322 s = expr_end;
9323 continue;
9324
252b5132
RH
9325 default:
9326 as_bad (_("bad char = '%c'\n"), *args);
9327 internalError ();
9328 }
9329 break;
9330 }
9331 /* Args don't match. */
9332 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9333 !strcmp (insn->name, insn[1].name))
9334 {
9335 ++insn;
9336 s = argsStart;
268f6bed 9337 insn_error = _("illegal operands");
252b5132
RH
9338 continue;
9339 }
268f6bed
L
9340 if (save_c)
9341 *(--s) = save_c;
252b5132
RH
9342 insn_error = _("illegal operands");
9343 return;
9344 }
9345}
9346
9347/* This routine assembles an instruction into its binary format when
9348 assembling for the mips16. As a side effect, it sets one of the
9349 global variables imm_reloc or offset_reloc to the type of
9350 relocation to do if one of the operands is an address expression.
9351 It also sets mips16_small and mips16_ext if the user explicitly
9352 requested a small or extended instruction. */
9353
9354static void
9355mips16_ip (str, ip)
9356 char *str;
9357 struct mips_cl_insn *ip;
9358{
9359 char *s;
9360 const char *args;
9361 struct mips_opcode *insn;
9362 char *argsstart;
9363 unsigned int regno;
9364 unsigned int lastregno = 0;
9365 char *s_reset;
9366
9367 insn_error = NULL;
9368
b34976b6
AM
9369 mips16_small = FALSE;
9370 mips16_ext = FALSE;
252b5132 9371
3882b010 9372 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9373 ;
9374 switch (*s)
9375 {
9376 case '\0':
9377 break;
9378
9379 case ' ':
9380 *s++ = '\0';
9381 break;
9382
9383 case '.':
9384 if (s[1] == 't' && s[2] == ' ')
9385 {
9386 *s = '\0';
b34976b6 9387 mips16_small = TRUE;
252b5132
RH
9388 s += 3;
9389 break;
9390 }
9391 else if (s[1] == 'e' && s[2] == ' ')
9392 {
9393 *s = '\0';
b34976b6 9394 mips16_ext = TRUE;
252b5132
RH
9395 s += 3;
9396 break;
9397 }
9398 /* Fall through. */
9399 default:
9400 insn_error = _("unknown opcode");
9401 return;
9402 }
9403
9404 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 9405 mips16_small = TRUE;
252b5132
RH
9406
9407 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9408 {
9409 insn_error = _("unrecognized opcode");
9410 return;
9411 }
9412
9413 argsstart = s;
9414 for (;;)
9415 {
9416 assert (strcmp (insn->name, str) == 0);
9417
9418 ip->insn_mo = insn;
9419 ip->insn_opcode = insn->match;
b34976b6 9420 ip->use_extend = FALSE;
252b5132 9421 imm_expr.X_op = O_absent;
f6688943
TS
9422 imm_reloc[0] = BFD_RELOC_UNUSED;
9423 imm_reloc[1] = BFD_RELOC_UNUSED;
9424 imm_reloc[2] = BFD_RELOC_UNUSED;
252b5132 9425 offset_expr.X_op = O_absent;
f6688943
TS
9426 offset_reloc[0] = BFD_RELOC_UNUSED;
9427 offset_reloc[1] = BFD_RELOC_UNUSED;
9428 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9429 for (args = insn->args; 1; ++args)
9430 {
9431 int c;
9432
9433 if (*s == ' ')
9434 ++s;
9435
9436 /* In this switch statement we call break if we did not find
9437 a match, continue if we did find a match, or return if we
9438 are done. */
9439
9440 c = *args;
9441 switch (c)
9442 {
9443 case '\0':
9444 if (*s == '\0')
9445 {
9446 /* Stuff the immediate value in now, if we can. */
9447 if (imm_expr.X_op == O_constant
f6688943 9448 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9449 && insn->pinfo != INSN_MACRO)
9450 {
c4e7957c 9451 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b34976b6 9452 imm_expr.X_add_number, TRUE, mips16_small,
252b5132
RH
9453 mips16_ext, &ip->insn_opcode,
9454 &ip->use_extend, &ip->extend);
9455 imm_expr.X_op = O_absent;
f6688943 9456 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9457 }
9458
9459 return;
9460 }
9461 break;
9462
9463 case ',':
9464 if (*s++ == c)
9465 continue;
9466 s--;
9467 switch (*++args)
9468 {
9469 case 'v':
9470 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9471 continue;
9472 case 'w':
9473 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9474 continue;
9475 }
9476 break;
9477
9478 case '(':
9479 case ')':
9480 if (*s++ == c)
9481 continue;
9482 break;
9483
9484 case 'v':
9485 case 'w':
9486 if (s[0] != '$')
9487 {
9488 if (c == 'v')
9489 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9490 else
9491 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9492 ++args;
9493 continue;
9494 }
9495 /* Fall through. */
9496 case 'x':
9497 case 'y':
9498 case 'z':
9499 case 'Z':
9500 case '0':
9501 case 'S':
9502 case 'R':
9503 case 'X':
9504 case 'Y':
9505 if (s[0] != '$')
9506 break;
9507 s_reset = s;
3882b010 9508 if (ISDIGIT (s[1]))
252b5132
RH
9509 {
9510 ++s;
9511 regno = 0;
9512 do
9513 {
9514 regno *= 10;
9515 regno += *s - '0';
9516 ++s;
9517 }
3882b010 9518 while (ISDIGIT (*s));
252b5132
RH
9519 if (regno > 31)
9520 {
9521 as_bad (_("invalid register number (%d)"), regno);
9522 regno = 2;
9523 }
9524 }
9525 else
9526 {
76db943d
TS
9527 if (s[1] == 'r' && s[2] == 'a')
9528 {
9529 s += 3;
9530 regno = RA;
9531 }
9532 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
9533 {
9534 s += 3;
9535 regno = FP;
9536 }
9537 else if (s[1] == 's' && s[2] == 'p')
9538 {
9539 s += 3;
9540 regno = SP;
9541 }
9542 else if (s[1] == 'g' && s[2] == 'p')
9543 {
9544 s += 3;
9545 regno = GP;
9546 }
9547 else if (s[1] == 'a' && s[2] == 't')
9548 {
9549 s += 3;
9550 regno = AT;
9551 }
9552 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9553 {
9554 s += 4;
9555 regno = KT0;
9556 }
9557 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9558 {
9559 s += 4;
9560 regno = KT1;
9561 }
85b51719
TS
9562 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9563 {
9564 s += 5;
9565 regno = ZERO;
9566 }
252b5132
RH
9567 else
9568 break;
9569 }
9570
9571 if (*s == ' ')
9572 ++s;
9573 if (args[1] != *s)
9574 {
9575 if (c == 'v' || c == 'w')
9576 {
9577 regno = mips16_to_32_reg_map[lastregno];
9578 s = s_reset;
f9419b05 9579 ++args;
252b5132
RH
9580 }
9581 }
9582
9583 switch (c)
9584 {
9585 case 'x':
9586 case 'y':
9587 case 'z':
9588 case 'v':
9589 case 'w':
9590 case 'Z':
9591 regno = mips32_to_16_reg_map[regno];
9592 break;
9593
9594 case '0':
9595 if (regno != 0)
9596 regno = ILLEGAL_REG;
9597 break;
9598
9599 case 'S':
9600 if (regno != SP)
9601 regno = ILLEGAL_REG;
9602 break;
9603
9604 case 'R':
9605 if (regno != RA)
9606 regno = ILLEGAL_REG;
9607 break;
9608
9609 case 'X':
9610 case 'Y':
9611 if (regno == AT && ! mips_opts.noat)
9612 as_warn (_("used $at without \".set noat\""));
9613 break;
9614
9615 default:
9616 internalError ();
9617 }
9618
9619 if (regno == ILLEGAL_REG)
9620 break;
9621
9622 switch (c)
9623 {
9624 case 'x':
9625 case 'v':
9626 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9627 break;
9628 case 'y':
9629 case 'w':
9630 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9631 break;
9632 case 'z':
9633 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9634 break;
9635 case 'Z':
9636 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9637 case '0':
9638 case 'S':
9639 case 'R':
9640 break;
9641 case 'X':
9642 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9643 break;
9644 case 'Y':
9645 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9646 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9647 break;
9648 default:
9649 internalError ();
9650 }
9651
9652 lastregno = regno;
9653 continue;
9654
9655 case 'P':
9656 if (strncmp (s, "$pc", 3) == 0)
9657 {
9658 s += 3;
9659 continue;
9660 }
9661 break;
9662
9663 case '<':
9664 case '>':
9665 case '[':
9666 case ']':
9667 case '4':
9668 case '5':
9669 case 'H':
9670 case 'W':
9671 case 'D':
9672 case 'j':
9673 case '8':
9674 case 'V':
9675 case 'C':
9676 case 'U':
9677 case 'k':
9678 case 'K':
9679 if (s[0] == '%'
9680 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9681 {
9682 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9683 and generate the appropriate reloc. If the text
9684 inside %gprel is not a symbol name with an
9685 optional offset, then we generate a normal reloc
9686 and will probably fail later. */
9687 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9688 if (imm_expr.X_op == O_symbol)
9689 {
b34976b6 9690 mips16_ext = TRUE;
f6688943 9691 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132 9692 s = expr_end;
b34976b6 9693 ip->use_extend = TRUE;
252b5132
RH
9694 ip->extend = 0;
9695 continue;
9696 }
9697 }
9698 else
9699 {
9700 /* Just pick up a normal expression. */
9701 my_getExpression (&imm_expr, s);
9702 }
9703
9704 if (imm_expr.X_op == O_register)
9705 {
9706 /* What we thought was an expression turned out to
9707 be a register. */
9708
9709 if (s[0] == '(' && args[1] == '(')
9710 {
9711 /* It looks like the expression was omitted
9712 before a register indirection, which means
9713 that the expression is implicitly zero. We
9714 still set up imm_expr, so that we handle
9715 explicit extensions correctly. */
9716 imm_expr.X_op = O_constant;
9717 imm_expr.X_add_number = 0;
f6688943 9718 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9719 continue;
9720 }
9721
9722 break;
9723 }
9724
9725 /* We need to relax this instruction. */
f6688943 9726 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9727 s = expr_end;
9728 continue;
9729
9730 case 'p':
9731 case 'q':
9732 case 'A':
9733 case 'B':
9734 case 'E':
9735 /* We use offset_reloc rather than imm_reloc for the PC
9736 relative operands. This lets macros with both
9737 immediate and address operands work correctly. */
9738 my_getExpression (&offset_expr, s);
9739
9740 if (offset_expr.X_op == O_register)
9741 break;
9742
9743 /* We need to relax this instruction. */
f6688943 9744 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9745 s = expr_end;
9746 continue;
9747
9748 case '6': /* break code */
9749 my_getExpression (&imm_expr, s);
9750 check_absolute_expr (ip, &imm_expr);
9751 if ((unsigned long) imm_expr.X_add_number > 63)
9752 {
9753 as_warn (_("Invalid value for `%s' (%lu)"),
9754 ip->insn_mo->name,
9755 (unsigned long) imm_expr.X_add_number);
9756 imm_expr.X_add_number &= 0x3f;
9757 }
9758 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9759 imm_expr.X_op = O_absent;
9760 s = expr_end;
9761 continue;
9762
9763 case 'a': /* 26 bit address */
9764 my_getExpression (&offset_expr, s);
9765 s = expr_end;
f6688943 9766 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9767 ip->insn_opcode <<= 16;
9768 continue;
9769
9770 case 'l': /* register list for entry macro */
9771 case 'L': /* register list for exit macro */
9772 {
9773 int mask;
9774
9775 if (c == 'l')
9776 mask = 0;
9777 else
9778 mask = 7 << 3;
9779 while (*s != '\0')
9780 {
9781 int freg, reg1, reg2;
9782
9783 while (*s == ' ' || *s == ',')
9784 ++s;
9785 if (*s != '$')
9786 {
9787 as_bad (_("can't parse register list"));
9788 break;
9789 }
9790 ++s;
9791 if (*s != 'f')
9792 freg = 0;
9793 else
9794 {
9795 freg = 1;
9796 ++s;
9797 }
9798 reg1 = 0;
3882b010 9799 while (ISDIGIT (*s))
252b5132
RH
9800 {
9801 reg1 *= 10;
9802 reg1 += *s - '0';
9803 ++s;
9804 }
9805 if (*s == ' ')
9806 ++s;
9807 if (*s != '-')
9808 reg2 = reg1;
9809 else
9810 {
9811 ++s;
9812 if (*s != '$')
9813 break;
9814 ++s;
9815 if (freg)
9816 {
9817 if (*s == 'f')
9818 ++s;
9819 else
9820 {
9821 as_bad (_("invalid register list"));
9822 break;
9823 }
9824 }
9825 reg2 = 0;
3882b010 9826 while (ISDIGIT (*s))
252b5132
RH
9827 {
9828 reg2 *= 10;
9829 reg2 += *s - '0';
9830 ++s;
9831 }
9832 }
9833 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9834 {
9835 mask &= ~ (7 << 3);
9836 mask |= 5 << 3;
9837 }
9838 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9839 {
9840 mask &= ~ (7 << 3);
9841 mask |= 6 << 3;
9842 }
9843 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9844 mask |= (reg2 - 3) << 3;
9845 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9846 mask |= (reg2 - 15) << 1;
f9419b05 9847 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9848 mask |= 1;
9849 else
9850 {
9851 as_bad (_("invalid register list"));
9852 break;
9853 }
9854 }
9855 /* The mask is filled in in the opcode table for the
9856 benefit of the disassembler. We remove it before
9857 applying the actual mask. */
9858 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9859 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9860 }
9861 continue;
9862
9863 case 'e': /* extend code */
9864 my_getExpression (&imm_expr, s);
9865 check_absolute_expr (ip, &imm_expr);
9866 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9867 {
9868 as_warn (_("Invalid value for `%s' (%lu)"),
9869 ip->insn_mo->name,
9870 (unsigned long) imm_expr.X_add_number);
9871 imm_expr.X_add_number &= 0x7ff;
9872 }
9873 ip->insn_opcode |= imm_expr.X_add_number;
9874 imm_expr.X_op = O_absent;
9875 s = expr_end;
9876 continue;
9877
9878 default:
9879 internalError ();
9880 }
9881 break;
9882 }
9883
9884 /* Args don't match. */
9885 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9886 strcmp (insn->name, insn[1].name) == 0)
9887 {
9888 ++insn;
9889 s = argsstart;
9890 continue;
9891 }
9892
9893 insn_error = _("illegal operands");
9894
9895 return;
9896 }
9897}
9898
9899/* This structure holds information we know about a mips16 immediate
9900 argument type. */
9901
e972090a
NC
9902struct mips16_immed_operand
9903{
252b5132
RH
9904 /* The type code used in the argument string in the opcode table. */
9905 int type;
9906 /* The number of bits in the short form of the opcode. */
9907 int nbits;
9908 /* The number of bits in the extended form of the opcode. */
9909 int extbits;
9910 /* The amount by which the short form is shifted when it is used;
9911 for example, the sw instruction has a shift count of 2. */
9912 int shift;
9913 /* The amount by which the short form is shifted when it is stored
9914 into the instruction code. */
9915 int op_shift;
9916 /* Non-zero if the short form is unsigned. */
9917 int unsp;
9918 /* Non-zero if the extended form is unsigned. */
9919 int extu;
9920 /* Non-zero if the value is PC relative. */
9921 int pcrel;
9922};
9923
9924/* The mips16 immediate operand types. */
9925
9926static const struct mips16_immed_operand mips16_immed_operands[] =
9927{
9928 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9929 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9930 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9931 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9932 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9933 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9934 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9935 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9936 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9937 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9938 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9939 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9940 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9941 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9942 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9943 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9944 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9945 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9946 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9947 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9948 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9949};
9950
9951#define MIPS16_NUM_IMMED \
9952 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9953
9954/* Handle a mips16 instruction with an immediate value. This or's the
9955 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9956 whether an extended value is needed; if one is needed, it sets
9957 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9958 If SMALL is true, an unextended opcode was explicitly requested.
9959 If EXT is true, an extended opcode was explicitly requested. If
9960 WARN is true, warn if EXT does not match reality. */
9961
9962static void
9963mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9964 extend)
9965 char *file;
9966 unsigned int line;
9967 int type;
9968 offsetT val;
b34976b6
AM
9969 bfd_boolean warn;
9970 bfd_boolean small;
9971 bfd_boolean ext;
252b5132 9972 unsigned long *insn;
b34976b6 9973 bfd_boolean *use_extend;
252b5132
RH
9974 unsigned short *extend;
9975{
9976 register const struct mips16_immed_operand *op;
9977 int mintiny, maxtiny;
b34976b6 9978 bfd_boolean needext;
252b5132
RH
9979
9980 op = mips16_immed_operands;
9981 while (op->type != type)
9982 {
9983 ++op;
9984 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9985 }
9986
9987 if (op->unsp)
9988 {
9989 if (type == '<' || type == '>' || type == '[' || type == ']')
9990 {
9991 mintiny = 1;
9992 maxtiny = 1 << op->nbits;
9993 }
9994 else
9995 {
9996 mintiny = 0;
9997 maxtiny = (1 << op->nbits) - 1;
9998 }
9999 }
10000 else
10001 {
10002 mintiny = - (1 << (op->nbits - 1));
10003 maxtiny = (1 << (op->nbits - 1)) - 1;
10004 }
10005
10006 /* Branch offsets have an implicit 0 in the lowest bit. */
10007 if (type == 'p' || type == 'q')
10008 val /= 2;
10009
10010 if ((val & ((1 << op->shift) - 1)) != 0
10011 || val < (mintiny << op->shift)
10012 || val > (maxtiny << op->shift))
b34976b6 10013 needext = TRUE;
252b5132 10014 else
b34976b6 10015 needext = FALSE;
252b5132
RH
10016
10017 if (warn && ext && ! needext)
beae10d5
KH
10018 as_warn_where (file, line,
10019 _("extended operand requested but not required"));
252b5132
RH
10020 if (small && needext)
10021 as_bad_where (file, line, _("invalid unextended operand value"));
10022
10023 if (small || (! ext && ! needext))
10024 {
10025 int insnval;
10026
b34976b6 10027 *use_extend = FALSE;
252b5132
RH
10028 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10029 insnval <<= op->op_shift;
10030 *insn |= insnval;
10031 }
10032 else
10033 {
10034 long minext, maxext;
10035 int extval;
10036
10037 if (op->extu)
10038 {
10039 minext = 0;
10040 maxext = (1 << op->extbits) - 1;
10041 }
10042 else
10043 {
10044 minext = - (1 << (op->extbits - 1));
10045 maxext = (1 << (op->extbits - 1)) - 1;
10046 }
10047 if (val < minext || val > maxext)
10048 as_bad_where (file, line,
10049 _("operand value out of range for instruction"));
10050
b34976b6 10051 *use_extend = TRUE;
252b5132
RH
10052 if (op->extbits == 16)
10053 {
10054 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10055 val &= 0x1f;
10056 }
10057 else if (op->extbits == 15)
10058 {
10059 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10060 val &= 0xf;
10061 }
10062 else
10063 {
10064 extval = ((val & 0x1f) << 6) | (val & 0x20);
10065 val = 0;
10066 }
10067
10068 *extend = (unsigned short) extval;
10069 *insn |= val;
10070 }
10071}
10072\f
ad8d3bb3
TS
10073static struct percent_op_match
10074{
10075 const char *str;
10076 const enum small_ex_type type;
10077} percent_op[] =
10078{
ad8d3bb3
TS
10079 {"%lo", S_EX_LO},
10080#ifdef OBJ_ELF
394f9b3a
TS
10081 {"%call_hi", S_EX_CALL_HI},
10082 {"%call_lo", S_EX_CALL_LO},
ad8d3bb3
TS
10083 {"%call16", S_EX_CALL16},
10084 {"%got_disp", S_EX_GOT_DISP},
10085 {"%got_page", S_EX_GOT_PAGE},
10086 {"%got_ofst", S_EX_GOT_OFST},
10087 {"%got_hi", S_EX_GOT_HI},
10088 {"%got_lo", S_EX_GOT_LO},
394f9b3a
TS
10089 {"%got", S_EX_GOT},
10090 {"%gp_rel", S_EX_GP_REL},
10091 {"%half", S_EX_HALF},
ad8d3bb3 10092 {"%highest", S_EX_HIGHEST},
394f9b3a
TS
10093 {"%higher", S_EX_HIGHER},
10094 {"%neg", S_EX_NEG},
ad8d3bb3 10095#endif
394f9b3a 10096 {"%hi", S_EX_HI}
ad8d3bb3
TS
10097};
10098
10099/* Parse small expression input. STR gets adjusted to eat up whitespace.
10100 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
10101 can be nested, this is handled by blanking the innermost, parsing the
10102 rest by subsequent calls. */
252b5132
RH
10103
10104static int
ad8d3bb3
TS
10105my_getSmallParser (str, len, nestlevel)
10106 char **str;
10107 unsigned int *len;
10108 int *nestlevel;
252b5132 10109{
ad8d3bb3
TS
10110 *len = 0;
10111 *str += strspn (*str, " \t");
394f9b3a 10112 /* Check for expression in parentheses. */
ad8d3bb3 10113 if (**str == '(')
252b5132 10114 {
ad8d3bb3
TS
10115 char *b = *str + 1 + strspn (*str + 1, " \t");
10116 char *e;
10117
10118 /* Check for base register. */
10119 if (b[0] == '$')
10120 {
10121 if (strchr (b, ')')
10122 && (e = b + strcspn (b, ") \t"))
10123 && e - b > 1 && e - b < 4)
10124 {
98d3f06f
KH
10125 if ((e - b == 3
10126 && ((b[1] == 'f' && b[2] == 'p')
10127 || (b[1] == 's' && b[2] == 'p')
10128 || (b[1] == 'g' && b[2] == 'p')
10129 || (b[1] == 'a' && b[2] == 't')
10130 || (ISDIGIT (b[1])
10131 && ISDIGIT (b[2]))))
10132 || (ISDIGIT (b[1])))
10133 {
10134 *len = strcspn (*str, ")") + 1;
10135 return S_EX_REGISTER;
10136 }
ad8d3bb3
TS
10137 }
10138 }
394f9b3a 10139 /* Check for percent_op (in parentheses). */
ad8d3bb3
TS
10140 else if (b[0] == '%')
10141 {
10142 *str = b;
394f9b3a 10143 return my_getPercentOp (str, len, nestlevel);
ad8d3bb3 10144 }
76b3015f 10145
394f9b3a
TS
10146 /* Some other expression in the parentheses, which can contain
10147 parentheses itself. Attempt to find the matching one. */
10148 {
10149 int pcnt = 1;
10150 char *s;
10151
10152 *len = 1;
10153 for (s = *str + 1; *s && pcnt; s++, (*len)++)
10154 {
10155 if (*s == '(')
f9419b05 10156 ++pcnt;
394f9b3a 10157 else if (*s == ')')
f9419b05 10158 --pcnt;
394f9b3a
TS
10159 }
10160 }
fb1b3232 10161 }
394f9b3a 10162 /* Check for percent_op (outside of parentheses). */
ad8d3bb3 10163 else if (*str[0] == '%')
394f9b3a
TS
10164 return my_getPercentOp (str, len, nestlevel);
10165
10166 /* Any other expression. */
10167 return S_EX_NONE;
10168}
ad8d3bb3 10169
394f9b3a
TS
10170static int
10171my_getPercentOp (str, len, nestlevel)
10172 char **str;
10173 unsigned int *len;
10174 int *nestlevel;
10175{
10176 char *tmp = *str + 1;
10177 unsigned int i = 0;
ad8d3bb3 10178
394f9b3a
TS
10179 while (ISALPHA (*tmp) || *tmp == '_')
10180 {
10181 *tmp = TOLOWER (*tmp);
10182 tmp++;
10183 }
10184 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
10185 {
10186 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
98d3f06f 10187 i++;
394f9b3a 10188 else
ad8d3bb3 10189 {
394f9b3a 10190 int type = percent_op[i].type;
ad8d3bb3 10191
394f9b3a
TS
10192 /* Only %hi and %lo are allowed for OldABI. */
10193 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
10194 return S_EX_NONE;
ad8d3bb3 10195
394f9b3a 10196 *len = strlen (percent_op[i].str);
f9419b05 10197 ++(*nestlevel);
394f9b3a 10198 return type;
ad8d3bb3 10199 }
fb1b3232 10200 }
ad8d3bb3
TS
10201 return S_EX_NONE;
10202}
10203
10204static int
10205my_getSmallExpression (ep, str)
10206 expressionS *ep;
10207 char *str;
10208{
10209 static char *oldstr = NULL;
10210 int c = S_EX_NONE;
10211 int oldc;
394f9b3a 10212 int nestlevel = -1;
ad8d3bb3
TS
10213 unsigned int len;
10214
394f9b3a
TS
10215 /* Don't update oldstr if the last call had nested percent_op's. We need
10216 it to parse the outer ones later. */
ad8d3bb3
TS
10217 if (! oldstr)
10218 oldstr = str;
76b3015f 10219
ad8d3bb3 10220 do
fb1b3232 10221 {
ad8d3bb3 10222 oldc = c;
394f9b3a 10223 c = my_getSmallParser (&str, &len, &nestlevel);
ad8d3bb3
TS
10224 if (c != S_EX_NONE && c != S_EX_REGISTER)
10225 str += len;
fb1b3232 10226 }
ad8d3bb3
TS
10227 while (c != S_EX_NONE && c != S_EX_REGISTER);
10228
394f9b3a 10229 if (nestlevel >= 0)
fb1b3232 10230 {
394f9b3a
TS
10231 /* A percent_op was encountered. Don't try to get an expression if
10232 it is already blanked out. */
ad8d3bb3
TS
10233 if (*(str + strspn (str + 1, " )")) != ')')
10234 {
10235 char save;
10236
394f9b3a 10237 /* Let my_getExpression() stop at the closing parenthesis. */
ad8d3bb3
TS
10238 save = *(str + len);
10239 *(str + len) = '\0';
10240 my_getExpression (ep, str);
10241 *(str + len) = save;
10242 }
394f9b3a 10243 if (nestlevel > 0)
ad8d3bb3 10244 {
394f9b3a
TS
10245 /* Blank out including the % sign and the proper matching
10246 parenthesis. */
10247 int pcnt = 1;
10248 char *s = strrchr (oldstr, '%');
10249 char *end;
10250
10251 for (end = strchr (s, '(') + 1; *end && pcnt; end++)
10252 {
10253 if (*end == '(')
f9419b05 10254 ++pcnt;
394f9b3a 10255 else if (*end == ')')
f9419b05 10256 --pcnt;
394f9b3a
TS
10257 }
10258
10259 memset (s, ' ', end - s);
ad8d3bb3
TS
10260 str = oldstr;
10261 }
10262 else
394f9b3a
TS
10263 expr_end = str + len;
10264
ad8d3bb3 10265 c = oldc;
fb1b3232 10266 }
ad8d3bb3 10267 else if (c == S_EX_NONE)
fb1b3232 10268 {
ad8d3bb3 10269 my_getExpression (ep, str);
fb1b3232 10270 }
ad8d3bb3 10271 else if (c == S_EX_REGISTER)
fb1b3232 10272 {
ad8d3bb3
TS
10273 ep->X_op = O_constant;
10274 expr_end = str;
10275 ep->X_add_symbol = NULL;
10276 ep->X_op_symbol = NULL;
10277 ep->X_add_number = 0;
fb1b3232 10278 }
fb1b3232
TS
10279 else
10280 {
98d3f06f 10281 as_fatal (_("internal error"));
fb1b3232 10282 }
252b5132 10283
394f9b3a
TS
10284 if (nestlevel <= 0)
10285 /* All percent_op's have been handled. */
ad8d3bb3 10286 oldstr = NULL;
fb1b3232 10287
fb1b3232 10288 return c;
252b5132
RH
10289}
10290
10291static void
10292my_getExpression (ep, str)
10293 expressionS *ep;
10294 char *str;
10295{
10296 char *save_in;
98aa84af 10297 valueT val;
252b5132
RH
10298
10299 save_in = input_line_pointer;
10300 input_line_pointer = str;
10301 expression (ep);
10302 expr_end = input_line_pointer;
10303 input_line_pointer = save_in;
10304
10305 /* If we are in mips16 mode, and this is an expression based on `.',
10306 then we bump the value of the symbol by 1 since that is how other
10307 text symbols are handled. We don't bother to handle complex
10308 expressions, just `.' plus or minus a constant. */
10309 if (mips_opts.mips16
10310 && ep->X_op == O_symbol
10311 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10312 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
10313 && symbol_get_frag (ep->X_add_symbol) == frag_now
10314 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
10315 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10316 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
10317}
10318
10319/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
10320 of type TYPE, and store the appropriate bytes in *LITP. The number
10321 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
10322 returned, or NULL on OK. */
10323
10324char *
10325md_atof (type, litP, sizeP)
10326 int type;
10327 char *litP;
10328 int *sizeP;
10329{
10330 int prec;
10331 LITTLENUM_TYPE words[4];
10332 char *t;
10333 int i;
10334
10335 switch (type)
10336 {
10337 case 'f':
10338 prec = 2;
10339 break;
10340
10341 case 'd':
10342 prec = 4;
10343 break;
10344
10345 default:
10346 *sizeP = 0;
10347 return _("bad call to md_atof");
10348 }
10349
10350 t = atof_ieee (input_line_pointer, type, words);
10351 if (t)
10352 input_line_pointer = t;
10353
10354 *sizeP = prec * 2;
10355
10356 if (! target_big_endian)
10357 {
10358 for (i = prec - 1; i >= 0; i--)
10359 {
10360 md_number_to_chars (litP, (valueT) words[i], 2);
10361 litP += 2;
10362 }
10363 }
10364 else
10365 {
10366 for (i = 0; i < prec; i++)
10367 {
10368 md_number_to_chars (litP, (valueT) words[i], 2);
10369 litP += 2;
10370 }
10371 }
bdaaa2e1 10372
252b5132
RH
10373 return NULL;
10374}
10375
10376void
10377md_number_to_chars (buf, val, n)
10378 char *buf;
10379 valueT val;
10380 int n;
10381{
10382 if (target_big_endian)
10383 number_to_chars_bigendian (buf, val, n);
10384 else
10385 number_to_chars_littleendian (buf, val, n);
10386}
10387\f
ae948b86 10388#ifdef OBJ_ELF
e013f690
TS
10389static int support_64bit_objects(void)
10390{
10391 const char **list, **l;
aa3d8fdf 10392 int yes;
e013f690
TS
10393
10394 list = bfd_target_list ();
10395 for (l = list; *l != NULL; l++)
10396#ifdef TE_TMIPS
10397 /* This is traditional mips */
10398 if (strcmp (*l, "elf64-tradbigmips") == 0
10399 || strcmp (*l, "elf64-tradlittlemips") == 0)
10400#else
10401 if (strcmp (*l, "elf64-bigmips") == 0
10402 || strcmp (*l, "elf64-littlemips") == 0)
10403#endif
10404 break;
aa3d8fdf 10405 yes = (*l != NULL);
e013f690 10406 free (list);
aa3d8fdf 10407 return yes;
e013f690 10408}
ae948b86 10409#endif /* OBJ_ELF */
e013f690 10410
5a38dc70 10411const char *md_shortopts = "nO::g::G:";
252b5132 10412
e972090a
NC
10413struct option md_longopts[] =
10414{
252b5132
RH
10415#define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10416 {"mips0", no_argument, NULL, OPTION_MIPS1},
10417 {"mips1", no_argument, NULL, OPTION_MIPS1},
10418#define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10419 {"mips2", no_argument, NULL, OPTION_MIPS2},
10420#define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10421 {"mips3", no_argument, NULL, OPTION_MIPS3},
10422#define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10423 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86
TS
10424#define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10425 {"mips5", no_argument, NULL, OPTION_MIPS5},
10426#define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10427 {"mips32", no_argument, NULL, OPTION_MIPS32},
10428#define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10429 {"mips64", no_argument, NULL, OPTION_MIPS64},
10430#define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
252b5132 10431 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
ae948b86 10432#define OPTION_TRAP (OPTION_MD_BASE + 9)
252b5132
RH
10433 {"trap", no_argument, NULL, OPTION_TRAP},
10434 {"no-break", no_argument, NULL, OPTION_TRAP},
ae948b86 10435#define OPTION_BREAK (OPTION_MD_BASE + 10)
252b5132
RH
10436 {"break", no_argument, NULL, OPTION_BREAK},
10437 {"no-trap", no_argument, NULL, OPTION_BREAK},
ae948b86 10438#define OPTION_EB (OPTION_MD_BASE + 11)
252b5132 10439 {"EB", no_argument, NULL, OPTION_EB},
ae948b86 10440#define OPTION_EL (OPTION_MD_BASE + 12)
252b5132 10441 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 10442#define OPTION_MIPS16 (OPTION_MD_BASE + 13)
252b5132 10443 {"mips16", no_argument, NULL, OPTION_MIPS16},
ae948b86 10444#define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
252b5132 10445 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
ae948b86 10446#define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
6b76fefe 10447 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9ee72ff1
TS
10448#define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10449 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10450 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
ae948b86
TS
10451#define OPTION_FP32 (OPTION_MD_BASE + 17)
10452 {"mfp32", no_argument, NULL, OPTION_FP32},
10453#define OPTION_GP32 (OPTION_MD_BASE + 18)
c97ef257 10454 {"mgp32", no_argument, NULL, OPTION_GP32},
ae948b86 10455#define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
119d663a 10456 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
ae948b86 10457#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
119d663a 10458 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
ae948b86 10459#define OPTION_MARCH (OPTION_MD_BASE + 21)
ec68c924 10460 {"march", required_argument, NULL, OPTION_MARCH},
ae948b86 10461#define OPTION_MTUNE (OPTION_MD_BASE + 22)
ec68c924 10462 {"mtune", required_argument, NULL, OPTION_MTUNE},
316f5878
RS
10463#define OPTION_FP64 (OPTION_MD_BASE + 23)
10464 {"mfp64", no_argument, NULL, OPTION_FP64},
ae948b86
TS
10465#define OPTION_M4650 (OPTION_MD_BASE + 24)
10466 {"m4650", no_argument, NULL, OPTION_M4650},
10467#define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10468 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10469#define OPTION_M4010 (OPTION_MD_BASE + 26)
10470 {"m4010", no_argument, NULL, OPTION_M4010},
10471#define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10472 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10473#define OPTION_M4100 (OPTION_MD_BASE + 28)
10474 {"m4100", no_argument, NULL, OPTION_M4100},
10475#define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10476 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10477#define OPTION_M3900 (OPTION_MD_BASE + 30)
10478 {"m3900", no_argument, NULL, OPTION_M3900},
10479#define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10480 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10481#define OPTION_GP64 (OPTION_MD_BASE + 32)
10482 {"mgp64", no_argument, NULL, OPTION_GP64},
1f25f5d3
CD
10483#define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10484 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10485#define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10486 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
deec1734
CD
10487#define OPTION_MDMX (OPTION_MD_BASE + 35)
10488 {"mdmx", no_argument, NULL, OPTION_MDMX},
10489#define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10490 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
60b63b72
RS
10491#define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10492#define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10493 {"mfix-vr4122-bugs", no_argument, NULL, OPTION_FIX_VR4122},
10494 {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
4a6a3df4
AO
10495#define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10496#define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10497 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10498 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
af7ee8bf
CD
10499#define OPTION_MIPS32R2 (OPTION_MD_BASE + 41)
10500 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
156c2f8b 10501#ifdef OBJ_ELF
af7ee8bf 10502#define OPTION_ELF_BASE (OPTION_MD_BASE + 42)
156c2f8b 10503#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10504 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10505 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10506#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10507 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10508#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10509 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10510#define OPTION_MABI (OPTION_ELF_BASE + 3)
10511 {"mabi", required_argument, NULL, OPTION_MABI},
10512#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10513 {"32", no_argument, NULL, OPTION_32},
ae948b86 10514#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10515 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10516#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10517 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
10518#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10519 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10520#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10521 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
ae948b86 10522#endif /* OBJ_ELF */
252b5132
RH
10523 {NULL, no_argument, NULL, 0}
10524};
156c2f8b 10525size_t md_longopts_size = sizeof (md_longopts);
252b5132 10526
316f5878
RS
10527/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10528 NEW_VALUE. Warn if another value was already specified. Note:
10529 we have to defer parsing the -march and -mtune arguments in order
10530 to handle 'from-abi' correctly, since the ABI might be specified
10531 in a later argument. */
10532
10533static void
10534mips_set_option_string (string_ptr, new_value)
10535 const char **string_ptr, *new_value;
10536{
10537 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10538 as_warn (_("A different %s was already specified, is now %s"),
10539 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10540 new_value);
10541
10542 *string_ptr = new_value;
10543}
10544
252b5132
RH
10545int
10546md_parse_option (c, arg)
10547 int c;
10548 char *arg;
10549{
10550 switch (c)
10551 {
119d663a
NC
10552 case OPTION_CONSTRUCT_FLOATS:
10553 mips_disable_float_construction = 0;
10554 break;
bdaaa2e1 10555
119d663a
NC
10556 case OPTION_NO_CONSTRUCT_FLOATS:
10557 mips_disable_float_construction = 1;
10558 break;
bdaaa2e1 10559
252b5132
RH
10560 case OPTION_TRAP:
10561 mips_trap = 1;
10562 break;
10563
10564 case OPTION_BREAK:
10565 mips_trap = 0;
10566 break;
10567
10568 case OPTION_EB:
10569 target_big_endian = 1;
10570 break;
10571
10572 case OPTION_EL:
10573 target_big_endian = 0;
10574 break;
10575
39c0a331
L
10576 case 'n':
10577 warn_nops = 1;
10578 break;
10579
252b5132
RH
10580 case 'O':
10581 if (arg && arg[1] == '0')
10582 mips_optimize = 1;
10583 else
10584 mips_optimize = 2;
10585 break;
10586
10587 case 'g':
10588 if (arg == NULL)
10589 mips_debug = 2;
10590 else
10591 mips_debug = atoi (arg);
10592 /* When the MIPS assembler sees -g or -g2, it does not do
10593 optimizations which limit full symbolic debugging. We take
10594 that to be equivalent to -O0. */
10595 if (mips_debug == 2)
10596 mips_optimize = 1;
10597 break;
10598
10599 case OPTION_MIPS1:
316f5878 10600 file_mips_isa = ISA_MIPS1;
252b5132
RH
10601 break;
10602
10603 case OPTION_MIPS2:
316f5878 10604 file_mips_isa = ISA_MIPS2;
252b5132
RH
10605 break;
10606
10607 case OPTION_MIPS3:
316f5878 10608 file_mips_isa = ISA_MIPS3;
252b5132
RH
10609 break;
10610
10611 case OPTION_MIPS4:
316f5878 10612 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10613 break;
10614
84ea6cf2 10615 case OPTION_MIPS5:
316f5878 10616 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10617 break;
10618
e7af610e 10619 case OPTION_MIPS32:
316f5878 10620 file_mips_isa = ISA_MIPS32;
252b5132
RH
10621 break;
10622
af7ee8bf
CD
10623 case OPTION_MIPS32R2:
10624 file_mips_isa = ISA_MIPS32R2;
10625 break;
10626
84ea6cf2 10627 case OPTION_MIPS64:
316f5878 10628 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10629 break;
10630
ec68c924 10631 case OPTION_MTUNE:
316f5878
RS
10632 mips_set_option_string (&mips_tune_string, arg);
10633 break;
ec68c924 10634
316f5878
RS
10635 case OPTION_MARCH:
10636 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10637 break;
10638
10639 case OPTION_M4650:
316f5878
RS
10640 mips_set_option_string (&mips_arch_string, "4650");
10641 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10642 break;
10643
10644 case OPTION_NO_M4650:
10645 break;
10646
10647 case OPTION_M4010:
316f5878
RS
10648 mips_set_option_string (&mips_arch_string, "4010");
10649 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10650 break;
10651
10652 case OPTION_NO_M4010:
10653 break;
10654
10655 case OPTION_M4100:
316f5878
RS
10656 mips_set_option_string (&mips_arch_string, "4100");
10657 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10658 break;
10659
10660 case OPTION_NO_M4100:
10661 break;
10662
252b5132 10663 case OPTION_M3900:
316f5878
RS
10664 mips_set_option_string (&mips_arch_string, "3900");
10665 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10666 break;
bdaaa2e1 10667
252b5132
RH
10668 case OPTION_NO_M3900:
10669 break;
10670
deec1734
CD
10671 case OPTION_MDMX:
10672 mips_opts.ase_mdmx = 1;
10673 break;
10674
10675 case OPTION_NO_MDMX:
10676 mips_opts.ase_mdmx = 0;
10677 break;
10678
252b5132
RH
10679 case OPTION_MIPS16:
10680 mips_opts.mips16 = 1;
b34976b6 10681 mips_no_prev_insn (FALSE);
252b5132
RH
10682 break;
10683
10684 case OPTION_NO_MIPS16:
10685 mips_opts.mips16 = 0;
b34976b6 10686 mips_no_prev_insn (FALSE);
252b5132
RH
10687 break;
10688
1f25f5d3
CD
10689 case OPTION_MIPS3D:
10690 mips_opts.ase_mips3d = 1;
10691 break;
10692
10693 case OPTION_NO_MIPS3D:
10694 mips_opts.ase_mips3d = 0;
10695 break;
10696
252b5132
RH
10697 case OPTION_MEMBEDDED_PIC:
10698 mips_pic = EMBEDDED_PIC;
10699 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10700 {
10701 as_bad (_("-G may not be used with embedded PIC code"));
10702 return 0;
10703 }
10704 g_switch_value = 0x7fffffff;
10705 break;
10706
60b63b72
RS
10707 case OPTION_FIX_VR4122:
10708 mips_fix_4122_bugs = 1;
10709 break;
10710
10711 case OPTION_NO_FIX_VR4122:
10712 mips_fix_4122_bugs = 0;
10713 break;
10714
4a6a3df4
AO
10715 case OPTION_RELAX_BRANCH:
10716 mips_relax_branch = 1;
10717 break;
10718
10719 case OPTION_NO_RELAX_BRANCH:
10720 mips_relax_branch = 0;
10721 break;
10722
0f074f60 10723#ifdef OBJ_ELF
252b5132
RH
10724 /* When generating ELF code, we permit -KPIC and -call_shared to
10725 select SVR4_PIC, and -non_shared to select no PIC. This is
10726 intended to be compatible with Irix 5. */
10727 case OPTION_CALL_SHARED:
10728 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10729 {
10730 as_bad (_("-call_shared is supported only for ELF format"));
10731 return 0;
10732 }
10733 mips_pic = SVR4_PIC;
10734 if (g_switch_seen && g_switch_value != 0)
10735 {
10736 as_bad (_("-G may not be used with SVR4 PIC code"));
10737 return 0;
10738 }
10739 g_switch_value = 0;
10740 break;
10741
10742 case OPTION_NON_SHARED:
10743 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10744 {
10745 as_bad (_("-non_shared is supported only for ELF format"));
10746 return 0;
10747 }
10748 mips_pic = NO_PIC;
10749 break;
10750
10751 /* The -xgot option tells the assembler to use 32 offsets when
10752 accessing the got in SVR4_PIC mode. It is for Irix
10753 compatibility. */
10754 case OPTION_XGOT:
10755 mips_big_got = 1;
10756 break;
0f074f60 10757#endif /* OBJ_ELF */
252b5132
RH
10758
10759 case 'G':
10760 if (! USE_GLOBAL_POINTER_OPT)
10761 {
10762 as_bad (_("-G is not supported for this configuration"));
10763 return 0;
10764 }
10765 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10766 {
10767 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10768 return 0;
10769 }
10770 else
10771 g_switch_value = atoi (arg);
10772 g_switch_seen = 1;
10773 break;
10774
0f074f60 10775#ifdef OBJ_ELF
34ba82a8
TS
10776 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10777 and -mabi=64. */
252b5132 10778 case OPTION_32:
34ba82a8
TS
10779 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10780 {
10781 as_bad (_("-32 is supported for ELF format only"));
10782 return 0;
10783 }
316f5878 10784 mips_abi = O32_ABI;
252b5132
RH
10785 break;
10786
e013f690 10787 case OPTION_N32:
34ba82a8
TS
10788 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10789 {
10790 as_bad (_("-n32 is supported for ELF format only"));
10791 return 0;
10792 }
316f5878 10793 mips_abi = N32_ABI;
e013f690 10794 break;
252b5132 10795
e013f690 10796 case OPTION_64:
34ba82a8
TS
10797 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10798 {
10799 as_bad (_("-64 is supported for ELF format only"));
10800 return 0;
10801 }
316f5878 10802 mips_abi = N64_ABI;
e013f690
TS
10803 if (! support_64bit_objects())
10804 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10805 break;
ae948b86 10806#endif /* OBJ_ELF */
252b5132 10807
c97ef257 10808 case OPTION_GP32:
a325df1d 10809 file_mips_gp32 = 1;
c97ef257
AH
10810 break;
10811
10812 case OPTION_GP64:
a325df1d 10813 file_mips_gp32 = 0;
c97ef257 10814 break;
252b5132 10815
ca4e0257 10816 case OPTION_FP32:
a325df1d 10817 file_mips_fp32 = 1;
316f5878
RS
10818 break;
10819
10820 case OPTION_FP64:
10821 file_mips_fp32 = 0;
ca4e0257
RS
10822 break;
10823
ae948b86 10824#ifdef OBJ_ELF
252b5132 10825 case OPTION_MABI:
34ba82a8
TS
10826 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10827 {
10828 as_bad (_("-mabi is supported for ELF format only"));
10829 return 0;
10830 }
e013f690 10831 if (strcmp (arg, "32") == 0)
316f5878 10832 mips_abi = O32_ABI;
e013f690 10833 else if (strcmp (arg, "o64") == 0)
316f5878 10834 mips_abi = O64_ABI;
e013f690 10835 else if (strcmp (arg, "n32") == 0)
316f5878 10836 mips_abi = N32_ABI;
e013f690
TS
10837 else if (strcmp (arg, "64") == 0)
10838 {
316f5878 10839 mips_abi = N64_ABI;
e013f690
TS
10840 if (! support_64bit_objects())
10841 as_fatal (_("No compiled in support for 64 bit object file "
10842 "format"));
10843 }
10844 else if (strcmp (arg, "eabi") == 0)
316f5878 10845 mips_abi = EABI_ABI;
e013f690 10846 else
da0e507f
TS
10847 {
10848 as_fatal (_("invalid abi -mabi=%s"), arg);
10849 return 0;
10850 }
252b5132 10851 break;
e013f690 10852#endif /* OBJ_ELF */
252b5132 10853
6b76fefe 10854 case OPTION_M7000_HILO_FIX:
b34976b6 10855 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
10856 break;
10857
9ee72ff1 10858 case OPTION_MNO_7000_HILO_FIX:
b34976b6 10859 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
10860 break;
10861
ecb4347a
DJ
10862#ifdef OBJ_ELF
10863 case OPTION_MDEBUG:
b34976b6 10864 mips_flag_mdebug = TRUE;
ecb4347a
DJ
10865 break;
10866
10867 case OPTION_NO_MDEBUG:
b34976b6 10868 mips_flag_mdebug = FALSE;
ecb4347a
DJ
10869 break;
10870#endif /* OBJ_ELF */
10871
252b5132
RH
10872 default:
10873 return 0;
10874 }
10875
10876 return 1;
10877}
316f5878
RS
10878\f
10879/* Set up globals to generate code for the ISA or processor
10880 described by INFO. */
252b5132 10881
252b5132 10882static void
316f5878
RS
10883mips_set_architecture (info)
10884 const struct mips_cpu_info *info;
252b5132 10885{
316f5878 10886 if (info != 0)
252b5132 10887 {
316f5878
RS
10888 mips_arch_info = info;
10889 mips_arch = info->cpu;
10890 mips_opts.isa = info->isa;
252b5132 10891 }
252b5132
RH
10892}
10893
252b5132 10894
316f5878 10895/* Likewise for tuning. */
252b5132 10896
316f5878
RS
10897static void
10898mips_set_tune (info)
10899 const struct mips_cpu_info *info;
10900{
10901 if (info != 0)
10902 {
10903 mips_tune_info = info;
10904 mips_tune = info->cpu;
10905 }
10906}
80cc45a5 10907
34ba82a8 10908
252b5132 10909void
e9670677
MR
10910mips_after_parse_args ()
10911{
e9670677
MR
10912 /* GP relative stuff not working for PE */
10913 if (strncmp (TARGET_OS, "pe", 2) == 0
10914 && g_switch_value != 0)
10915 {
10916 if (g_switch_seen)
10917 as_bad (_("-G not supported in this configuration."));
10918 g_switch_value = 0;
10919 }
10920
22923709
RS
10921 /* The following code determines the architecture and register size.
10922 Similar code was added to GCC 3.3 (see override_options() in
10923 config/mips/mips.c). The GAS and GCC code should be kept in sync
10924 as much as possible. */
e9670677 10925
316f5878
RS
10926 if (mips_arch_string != 0)
10927 mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
e9670677 10928
316f5878
RS
10929 if (mips_tune_string != 0)
10930 mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
e9670677 10931
316f5878 10932 if (file_mips_isa != ISA_UNKNOWN)
e9670677 10933 {
316f5878
RS
10934 /* Handle -mipsN. At this point, file_mips_isa contains the
10935 ISA level specified by -mipsN, while mips_opts.isa contains
10936 the -march selection (if any). */
10937 if (mips_arch_info != 0)
e9670677 10938 {
316f5878
RS
10939 /* -march takes precedence over -mipsN, since it is more descriptive.
10940 There's no harm in specifying both as long as the ISA levels
10941 are the same. */
10942 if (file_mips_isa != mips_opts.isa)
10943 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10944 mips_cpu_info_from_isa (file_mips_isa)->name,
10945 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 10946 }
316f5878
RS
10947 else
10948 mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
e9670677
MR
10949 }
10950
316f5878
RS
10951 if (mips_arch_info == 0)
10952 mips_set_architecture (mips_parse_cpu ("default CPU",
10953 MIPS_CPU_STRING_DEFAULT));
e9670677 10954
316f5878
RS
10955 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10956 as_bad ("-march=%s is not compatible with the selected ABI",
10957 mips_arch_info->name);
e9670677 10958
316f5878
RS
10959 /* Optimize for mips_arch, unless -mtune selects a different processor. */
10960 if (mips_tune_info == 0)
10961 mips_set_tune (mips_arch_info);
e9670677 10962
316f5878 10963 if (file_mips_gp32 >= 0)
e9670677 10964 {
316f5878
RS
10965 /* The user specified the size of the integer registers. Make sure
10966 it agrees with the ABI and ISA. */
10967 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10968 as_bad (_("-mgp64 used with a 32-bit processor"));
10969 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10970 as_bad (_("-mgp32 used with a 64-bit ABI"));
10971 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10972 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
10973 }
10974 else
10975 {
316f5878
RS
10976 /* Infer the integer register size from the ABI and processor.
10977 Restrict ourselves to 32-bit registers if that's all the
10978 processor has, or if the ABI cannot handle 64-bit registers. */
10979 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10980 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
10981 }
10982
316f5878
RS
10983 /* ??? GAS treats single-float processors as though they had 64-bit
10984 float registers (although it complains when double-precision
10985 instructions are used). As things stand, saying they have 32-bit
10986 registers would lead to spurious "register must be even" messages.
10987 So here we assume float registers are always the same size as
10988 integer ones, unless the user says otherwise. */
10989 if (file_mips_fp32 < 0)
10990 file_mips_fp32 = file_mips_gp32;
e9670677 10991
316f5878 10992 /* End of GCC-shared inference code. */
e9670677 10993
316f5878
RS
10994 /* ??? When do we want this flag to be set? Who uses it? */
10995 if (file_mips_gp32 == 1
10996 && mips_abi == NO_ABI
10997 && ISA_HAS_64BIT_REGS (mips_opts.isa))
10998 mips_32bitmode = 1;
e9670677
MR
10999
11000 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11001 as_bad (_("trap exception not supported at ISA 1"));
11002
e9670677
MR
11003 /* If the selected architecture includes support for ASEs, enable
11004 generation of code for them. */
a4672219
TS
11005 if (mips_opts.mips16 == -1)
11006 mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
ffdefa66 11007 if (mips_opts.ase_mips3d == -1)
a4672219 11008 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
ffdefa66 11009 if (mips_opts.ase_mdmx == -1)
a4672219 11010 mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
e9670677 11011
e9670677 11012 file_mips_isa = mips_opts.isa;
a4672219 11013 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
11014 file_ase_mips3d = mips_opts.ase_mips3d;
11015 file_ase_mdmx = mips_opts.ase_mdmx;
11016 mips_opts.gp32 = file_mips_gp32;
11017 mips_opts.fp32 = file_mips_fp32;
11018
ecb4347a
DJ
11019 if (mips_flag_mdebug < 0)
11020 {
11021#ifdef OBJ_MAYBE_ECOFF
11022 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11023 mips_flag_mdebug = 1;
11024 else
11025#endif /* OBJ_MAYBE_ECOFF */
11026 mips_flag_mdebug = 0;
11027 }
e9670677
MR
11028}
11029\f
11030void
252b5132
RH
11031mips_init_after_args ()
11032{
11033 /* initialize opcodes */
11034 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 11035 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
11036}
11037
11038long
11039md_pcrel_from (fixP)
11040 fixS *fixP;
11041{
11042 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
11043 && fixP->fx_addsy != (symbolS *) NULL
11044 && ! S_IS_DEFINED (fixP->fx_addsy))
af6ae2ad 11045 return 4;
252b5132 11046
c9914766 11047 /* Return the address of the delay slot. */
252b5132
RH
11048 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
11049}
11050
252b5132
RH
11051/* This is called before the symbol table is processed. In order to
11052 work with gcc when using mips-tfile, we must keep all local labels.
11053 However, in other cases, we want to discard them. If we were
11054 called with -g, but we didn't see any debugging information, it may
11055 mean that gcc is smuggling debugging information through to
11056 mips-tfile, in which case we must generate all local labels. */
11057
11058void
11059mips_frob_file_before_adjust ()
11060{
11061#ifndef NO_ECOFF_DEBUGGING
11062 if (ECOFF_DEBUGGING
11063 && mips_debug != 0
11064 && ! ecoff_debugging_seen)
11065 flag_keep_locals = 1;
11066#endif
11067}
11068
11069/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 11070 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
11071 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
11072 explicit use of the %hi modifier. */
11073
11074void
11075mips_frob_file ()
11076{
11077 struct mips_hi_fixup *l;
11078
11079 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11080 {
11081 segment_info_type *seginfo;
11082 int pass;
11083
11084 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
11085
11086 /* Check quickly whether the next fixup happens to be a matching
11087 %lo. */
11088 if (l->fixp->fx_next != NULL
11089 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
11090 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
11091 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
11092 continue;
11093
11094 /* Look through the fixups for this segment for a matching %lo.
11095 When we find one, move the %hi just in front of it. We do
11096 this in two passes. In the first pass, we try to find a
11097 unique %lo. In the second pass, we permit multiple %hi
11098 relocs for a single %lo (this is a GNU extension). */
11099 seginfo = seg_info (l->seg);
11100 for (pass = 0; pass < 2; pass++)
11101 {
11102 fixS *f, *prev;
11103
11104 prev = NULL;
11105 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
11106 {
11107 /* Check whether this is a %lo fixup which matches l->fixp. */
11108 if (f->fx_r_type == BFD_RELOC_LO16
11109 && f->fx_addsy == l->fixp->fx_addsy
11110 && f->fx_offset == l->fixp->fx_offset
11111 && (pass == 1
11112 || prev == NULL
11113 || prev->fx_r_type != BFD_RELOC_HI16_S
11114 || prev->fx_addsy != f->fx_addsy
11115 || prev->fx_offset != f->fx_offset))
11116 {
11117 fixS **pf;
11118
11119 /* Move l->fixp before f. */
11120 for (pf = &seginfo->fix_root;
11121 *pf != l->fixp;
11122 pf = &(*pf)->fx_next)
11123 assert (*pf != NULL);
11124
11125 *pf = l->fixp->fx_next;
11126
11127 l->fixp->fx_next = f;
11128 if (prev == NULL)
11129 seginfo->fix_root = l->fixp;
11130 else
11131 prev->fx_next = l->fixp;
11132
11133 break;
11134 }
11135
11136 prev = f;
11137 }
11138
11139 if (f != NULL)
11140 break;
11141
11142#if 0 /* GCC code motion plus incomplete dead code elimination
11143 can leave a %hi without a %lo. */
11144 if (pass == 1)
11145 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11146 _("Unmatched %%hi reloc"));
11147#endif
11148 }
11149 }
11150}
11151
11152/* When generating embedded PIC code we need to use a special
11153 relocation to represent the difference of two symbols in the .text
11154 section (switch tables use a difference of this sort). See
11155 include/coff/mips.h for details. This macro checks whether this
11156 fixup requires the special reloc. */
11157#define SWITCH_TABLE(fixp) \
11158 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 11159 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
11160 && (fixp)->fx_addsy != NULL \
11161 && (fixp)->fx_subsy != NULL \
11162 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11163 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11164
11165/* When generating embedded PIC code we must keep all PC relative
11166 relocations, in case the linker has to relax a call. We also need
f6688943
TS
11167 to keep relocations for switch table entries.
11168
11169 We may have combined relocations without symbols in the N32/N64 ABI.
11170 We have to prevent gas from dropping them. */
252b5132 11171
252b5132
RH
11172int
11173mips_force_relocation (fixp)
11174 fixS *fixp;
11175{
ae6063d4 11176 if (generic_force_reloc (fixp))
252b5132
RH
11177 return 1;
11178
f6688943
TS
11179 if (HAVE_NEWABI
11180 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11181 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11182 || fixp->fx_r_type == BFD_RELOC_HI16_S
11183 || fixp->fx_r_type == BFD_RELOC_LO16))
11184 return 1;
11185
252b5132
RH
11186 return (mips_pic == EMBEDDED_PIC
11187 && (fixp->fx_pcrel
11188 || SWITCH_TABLE (fixp)
11189 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11190 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11191}
11192
add55e1f
RS
11193#ifdef OBJ_ELF
11194static int
11195mips_need_elf_addend_fixup (fixP)
11196 fixS *fixP;
11197{
2d2bf3e0
CD
11198 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11199 return 1;
b25a253c
CD
11200 if (mips_pic == EMBEDDED_PIC
11201 && S_IS_WEAK (fixP->fx_addsy))
11202 return 1;
11203 if (mips_pic != EMBEDDED_PIC
11204 && (S_IS_WEAK (fixP->fx_addsy)
bad9ca53 11205 || S_IS_EXTERNAL (fixP->fx_addsy))
2d2bf3e0
CD
11206 && !S_IS_COMMON (fixP->fx_addsy))
11207 return 1;
11208 if (symbol_used_in_reloc_p (fixP->fx_addsy)
11209 && (((bfd_get_section_flags (stdoutput,
11210 S_GET_SEGMENT (fixP->fx_addsy))
631cb423 11211 & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
2d2bf3e0
CD
11212 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11213 ".gnu.linkonce",
11214 sizeof (".gnu.linkonce") - 1)))
11215 return 1;
11216 return 0;
add55e1f
RS
11217}
11218#endif
11219
252b5132
RH
11220/* Apply a fixup to the object file. */
11221
94f592af
NC
11222void
11223md_apply_fix3 (fixP, valP, seg)
252b5132 11224 fixS *fixP;
98d3f06f 11225 valueT *valP;
94f592af 11226 segT seg ATTRIBUTE_UNUSED;
252b5132 11227{
874e8986 11228 bfd_byte *buf;
98aa84af
AM
11229 long insn;
11230 valueT value;
ed6fb7bd 11231 static int previous_fx_r_type = 0;
252b5132 11232
65551fa4
CD
11233 /* FIXME: Maybe just return for all reloc types not listed below?
11234 Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
11235 if (fixP->fx_r_type == BFD_RELOC_8)
11236 return;
11237
252b5132
RH
11238 assert (fixP->fx_size == 4
11239 || fixP->fx_r_type == BFD_RELOC_16
f6688943
TS
11240 || fixP->fx_r_type == BFD_RELOC_32
11241 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11242 || fixP->fx_r_type == BFD_RELOC_HI16_S
11243 || fixP->fx_r_type == BFD_RELOC_LO16
11244 || fixP->fx_r_type == BFD_RELOC_GPREL16
76b3015f 11245 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
f6688943 11246 || fixP->fx_r_type == BFD_RELOC_GPREL32
252b5132 11247 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
11248 || fixP->fx_r_type == BFD_RELOC_CTOR
11249 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11250 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11251 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11252 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11253 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11254 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
252b5132 11255 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
438c16b8
TS
11256 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11257 || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
252b5132 11258
98d3f06f 11259 value = *valP;
252b5132
RH
11260
11261 /* If we aren't adjusting this fixup to be against the section
11262 symbol, we need to adjust the value. */
11263#ifdef OBJ_ELF
11264 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 11265 {
add55e1f 11266 if (mips_need_elf_addend_fixup (fixP))
98aa84af 11267 {
d6e9d61a 11268 reloc_howto_type *howto;
98aa84af 11269 valueT symval = S_GET_VALUE (fixP->fx_addsy);
94f592af 11270
98aa84af 11271 value -= symval;
d6e9d61a
MR
11272
11273 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
af6ae2ad 11274 if (value != 0 && howto && howto->partial_inplace
7a49a8c2 11275 && (! fixP->fx_pcrel || howto->pcrel_offset))
98aa84af
AM
11276 {
11277 /* In this case, the bfd_install_relocation routine will
11278 incorrectly add the symbol value back in. We just want
7a49a8c2 11279 the addend to appear in the object file.
b34976b6 11280
7a49a8c2
L
11281 howto->pcrel_offset is added for R_MIPS_PC16, which is
11282 generated for code like
b34976b6 11283
7a49a8c2
L
11284 globl g1 .text
11285 .text
11286 .space 20
11287 g1:
11288 x:
11289 bal g1
11290 */
98aa84af 11291 value -= symval;
7461da6e
RS
11292
11293 /* Make sure the addend is still non-zero. If it became zero
11294 after the last operation, set it to a spurious value and
11295 subtract the same value from the object file's contents. */
11296 if (value == 0)
11297 {
11298 value = 8;
11299
11300 /* The in-place addends for LO16 relocations are signed;
11301 leave the matching HI16 in-place addends as zero. */
11302 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11303 {
7461da6e
RS
11304 bfd_vma contents, mask, field;
11305
7461da6e
RS
11306 contents = bfd_get_bits (fixP->fx_frag->fr_literal
11307 + fixP->fx_where,
11308 fixP->fx_size * 8,
11309 target_big_endian);
11310
11311 /* MASK has bits set where the relocation should go.
11312 FIELD is -value, shifted into the appropriate place
11313 for this relocation. */
11314 mask = 1 << (howto->bitsize - 1);
11315 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11316 field = (-value >> howto->rightshift) << howto->bitpos;
11317
11318 bfd_put_bits ((field & mask) | (contents & ~mask),
11319 fixP->fx_frag->fr_literal + fixP->fx_where,
11320 fixP->fx_size * 8,
11321 target_big_endian);
11322 }
11323 }
98aa84af
AM
11324 }
11325 }
252b5132 11326
bb2d6cd7
GK
11327 /* This code was generated using trial and error and so is
11328 fragile and not trustworthy. If you change it, you should
11329 rerun the elf-rel, elf-rel2, and empic testcases and ensure
11330 they still pass. */
11331 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11332 {
11333 value += fixP->fx_frag->fr_address + fixP->fx_where;
11334
11335 /* BFD's REL handling, for MIPS, is _very_ weird.
11336 This gives the right results, but it can't possibly
11337 be the way things are supposed to work. */
af6ae2ad 11338 if (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
bb2d6cd7
GK
11339 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11340 value += fixP->fx_frag->fr_address + fixP->fx_where;
11341 }
11342 }
11343#endif
252b5132 11344
94f592af 11345 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
252b5132 11346
ed6fb7bd
SC
11347 /* We are not done if this is a composite relocation to set up gp. */
11348 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11349 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10181a0d
AO
11350 || (fixP->fx_r_type == BFD_RELOC_64
11351 && (previous_fx_r_type == BFD_RELOC_GPREL32
11352 || previous_fx_r_type == BFD_RELOC_GPREL16))
ed6fb7bd
SC
11353 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11354 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11355 || fixP->fx_r_type == BFD_RELOC_LO16))))
252b5132 11356 fixP->fx_done = 1;
ed6fb7bd 11357 previous_fx_r_type = fixP->fx_r_type;
252b5132
RH
11358
11359 switch (fixP->fx_r_type)
11360 {
11361 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
11362 case BFD_RELOC_MIPS_SHIFT5:
11363 case BFD_RELOC_MIPS_SHIFT6:
11364 case BFD_RELOC_MIPS_GOT_DISP:
11365 case BFD_RELOC_MIPS_GOT_PAGE:
11366 case BFD_RELOC_MIPS_GOT_OFST:
11367 case BFD_RELOC_MIPS_SUB:
11368 case BFD_RELOC_MIPS_INSERT_A:
11369 case BFD_RELOC_MIPS_INSERT_B:
11370 case BFD_RELOC_MIPS_DELETE:
11371 case BFD_RELOC_MIPS_HIGHEST:
11372 case BFD_RELOC_MIPS_HIGHER:
11373 case BFD_RELOC_MIPS_SCN_DISP:
11374 case BFD_RELOC_MIPS_REL16:
11375 case BFD_RELOC_MIPS_RELGOT:
11376 case BFD_RELOC_MIPS_JALR:
252b5132
RH
11377 case BFD_RELOC_HI16:
11378 case BFD_RELOC_HI16_S:
cdf6fd85 11379 case BFD_RELOC_GPREL16:
252b5132
RH
11380 case BFD_RELOC_MIPS_LITERAL:
11381 case BFD_RELOC_MIPS_CALL16:
11382 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 11383 case BFD_RELOC_GPREL32:
252b5132
RH
11384 case BFD_RELOC_MIPS_GOT_HI16:
11385 case BFD_RELOC_MIPS_GOT_LO16:
11386 case BFD_RELOC_MIPS_CALL_HI16:
11387 case BFD_RELOC_MIPS_CALL_LO16:
11388 case BFD_RELOC_MIPS16_GPREL:
11389 if (fixP->fx_pcrel)
11390 as_bad_where (fixP->fx_file, fixP->fx_line,
11391 _("Invalid PC relative reloc"));
11392 /* Nothing needed to do. The value comes from the reloc entry */
11393 break;
11394
11395 case BFD_RELOC_MIPS16_JMP:
11396 /* We currently always generate a reloc against a symbol, which
11397 means that we don't want an addend even if the symbol is
11398 defined. */
11399 fixP->fx_addnumber = 0;
11400 break;
11401
11402 case BFD_RELOC_PCREL_HI16_S:
11403 /* The addend for this is tricky if it is internal, so we just
11404 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 11405 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
11406 && !fixP->fx_done
11407 && value != 0)
11408 break;
11409 if (fixP->fx_addsy
11410 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
11411 {
11412 /* For an external symbol adjust by the address to make it
11413 pcrel_offset. We use the address of the RELLO reloc
11414 which follows this one. */
11415 value += (fixP->fx_next->fx_frag->fr_address
11416 + fixP->fx_next->fx_where);
11417 }
e7d556df 11418 value = ((value + 0x8000) >> 16) & 0xffff;
874e8986 11419 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
11420 if (target_big_endian)
11421 buf += 2;
874e8986 11422 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
11423 break;
11424
11425 case BFD_RELOC_PCREL_LO16:
11426 /* The addend for this is tricky if it is internal, so we just
11427 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 11428 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
11429 && !fixP->fx_done
11430 && value != 0)
11431 break;
11432 if (fixP->fx_addsy
11433 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132 11434 value += fixP->fx_frag->fr_address + fixP->fx_where;
874e8986 11435 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
11436 if (target_big_endian)
11437 buf += 2;
874e8986 11438 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
11439 break;
11440
11441 case BFD_RELOC_64:
11442 /* This is handled like BFD_RELOC_32, but we output a sign
11443 extended value if we are only 32 bits. */
11444 if (fixP->fx_done
11445 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11446 {
11447 if (8 <= sizeof (valueT))
11448 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11449 value, 8);
11450 else
11451 {
11452 long w1, w2;
11453 long hiv;
11454
11455 w1 = w2 = fixP->fx_where;
11456 if (target_big_endian)
11457 w1 += 4;
11458 else
11459 w2 += 4;
11460 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11461 if ((value & 0x80000000) != 0)
11462 hiv = 0xffffffff;
11463 else
11464 hiv = 0;
11465 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11466 }
11467 }
11468 break;
11469
056350c6 11470 case BFD_RELOC_RVA:
252b5132
RH
11471 case BFD_RELOC_32:
11472 /* If we are deleting this reloc entry, we must fill in the
11473 value now. This can happen if we have a .word which is not
11474 resolved when it appears but is later defined. We also need
11475 to fill in the value if this is an embedded PIC switch table
11476 entry. */
11477 if (fixP->fx_done
11478 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11479 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11480 value, 4);
11481 break;
11482
11483 case BFD_RELOC_16:
11484 /* If we are deleting this reloc entry, we must fill in the
11485 value now. */
11486 assert (fixP->fx_size == 2);
11487 if (fixP->fx_done)
11488 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11489 value, 2);
11490 break;
11491
11492 case BFD_RELOC_LO16:
11493 /* When handling an embedded PIC switch statement, we can wind
11494 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11495 if (fixP->fx_done)
11496 {
98aa84af 11497 if (value + 0x8000 > 0xffff)
252b5132
RH
11498 as_bad_where (fixP->fx_file, fixP->fx_line,
11499 _("relocation overflow"));
874e8986 11500 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
11501 if (target_big_endian)
11502 buf += 2;
874e8986 11503 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
11504 }
11505 break;
11506
11507 case BFD_RELOC_16_PCREL_S2:
cb56d3d3
TS
11508 if ((value & 0x3) != 0)
11509 as_bad_where (fixP->fx_file, fixP->fx_line,
11510 _("Branch to odd address (%lx)"), (long) value);
11511
252b5132
RH
11512 /*
11513 * We need to save the bits in the instruction since fixup_segment()
11514 * might be deleting the relocation entry (i.e., a branch within
11515 * the current segment).
11516 */
af6ae2ad 11517 if (!fixP->fx_done && (value != 0 || HAVE_NEWABI))
bb2d6cd7
GK
11518 break;
11519 /* If 'value' is zero, the remaining reloc code won't actually
11520 do the store, so it must be done here. This is probably
11521 a bug somewhere. */
b25a253c
CD
11522 if (!fixP->fx_done
11523 && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11524 || fixP->fx_addsy == NULL /* ??? */
11525 || ! S_IS_DEFINED (fixP->fx_addsy)))
bb2d6cd7 11526 value -= fixP->fx_frag->fr_address + fixP->fx_where;
bdaaa2e1 11527
98aa84af 11528 value = (offsetT) value >> 2;
252b5132
RH
11529
11530 /* update old instruction data */
874e8986 11531 buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
252b5132
RH
11532 if (target_big_endian)
11533 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11534 else
11535 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11536
98aa84af 11537 if (value + 0x8000 <= 0xffff)
252b5132
RH
11538 insn |= value & 0xffff;
11539 else
11540 {
11541 /* The branch offset is too large. If this is an
11542 unconditional branch, and we are not generating PIC code,
11543 we can convert it to an absolute jump instruction. */
11544 if (mips_pic == NO_PIC
11545 && fixP->fx_done
11546 && fixP->fx_frag->fr_address >= text_section->vma
11547 && (fixP->fx_frag->fr_address
11548 < text_section->vma + text_section->_raw_size)
11549 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11550 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11551 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11552 {
11553 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11554 insn = 0x0c000000; /* jal */
11555 else
11556 insn = 0x08000000; /* j */
11557 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11558 fixP->fx_done = 0;
11559 fixP->fx_addsy = section_symbol (text_section);
11560 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11561 }
11562 else
11563 {
4a6a3df4
AO
11564 /* If we got here, we have branch-relaxation disabled,
11565 and there's nothing we can do to fix this instruction
11566 without turning it into a longer sequence. */
252b5132
RH
11567 as_bad_where (fixP->fx_file, fixP->fx_line,
11568 _("Branch out of range"));
11569 }
11570 }
11571
11572 md_number_to_chars ((char *) buf, (valueT) insn, 4);
11573 break;
11574
11575 case BFD_RELOC_VTABLE_INHERIT:
11576 fixP->fx_done = 0;
11577 if (fixP->fx_addsy
11578 && !S_IS_DEFINED (fixP->fx_addsy)
11579 && !S_IS_WEAK (fixP->fx_addsy))
11580 S_SET_WEAK (fixP->fx_addsy);
11581 break;
11582
11583 case BFD_RELOC_VTABLE_ENTRY:
11584 fixP->fx_done = 0;
11585 break;
11586
11587 default:
11588 internalError ();
11589 }
252b5132
RH
11590}
11591
11592#if 0
11593void
11594printInsn (oc)
11595 unsigned long oc;
11596{
11597 const struct mips_opcode *p;
11598 int treg, sreg, dreg, shamt;
11599 short imm;
11600 const char *args;
11601 int i;
11602
11603 for (i = 0; i < NUMOPCODES; ++i)
11604 {
11605 p = &mips_opcodes[i];
11606 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11607 {
11608 printf ("%08lx %s\t", oc, p->name);
11609 treg = (oc >> 16) & 0x1f;
11610 sreg = (oc >> 21) & 0x1f;
11611 dreg = (oc >> 11) & 0x1f;
11612 shamt = (oc >> 6) & 0x1f;
11613 imm = oc;
11614 for (args = p->args;; ++args)
11615 {
11616 switch (*args)
11617 {
11618 case '\0':
11619 printf ("\n");
11620 break;
11621
11622 case ',':
11623 case '(':
11624 case ')':
11625 printf ("%c", *args);
11626 continue;
11627
11628 case 'r':
11629 assert (treg == sreg);
11630 printf ("$%d,$%d", treg, sreg);
11631 continue;
11632
11633 case 'd':
11634 case 'G':
11635 printf ("$%d", dreg);
11636 continue;
11637
11638 case 't':
11639 case 'E':
11640 printf ("$%d", treg);
11641 continue;
11642
11643 case 'k':
11644 printf ("0x%x", treg);
11645 continue;
11646
11647 case 'b':
11648 case 's':
11649 printf ("$%d", sreg);
11650 continue;
11651
11652 case 'a':
11653 printf ("0x%08lx", oc & 0x1ffffff);
11654 continue;
11655
11656 case 'i':
11657 case 'j':
11658 case 'o':
11659 case 'u':
11660 printf ("%d", imm);
11661 continue;
11662
11663 case '<':
11664 case '>':
11665 printf ("$%d", shamt);
11666 continue;
11667
11668 default:
11669 internalError ();
11670 }
11671 break;
11672 }
11673 return;
11674 }
11675 }
11676 printf (_("%08lx UNDEFINED\n"), oc);
11677}
11678#endif
11679
11680static symbolS *
11681get_symbol ()
11682{
11683 int c;
11684 char *name;
11685 symbolS *p;
11686
11687 name = input_line_pointer;
11688 c = get_symbol_end ();
11689 p = (symbolS *) symbol_find_or_make (name);
11690 *input_line_pointer = c;
11691 return p;
11692}
11693
11694/* Align the current frag to a given power of two. The MIPS assembler
11695 also automatically adjusts any preceding label. */
11696
11697static void
11698mips_align (to, fill, label)
11699 int to;
11700 int fill;
11701 symbolS *label;
11702{
b34976b6 11703 mips_emit_delays (FALSE);
252b5132
RH
11704 frag_align (to, fill, 0);
11705 record_alignment (now_seg, to);
11706 if (label != NULL)
11707 {
11708 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11709 symbol_set_frag (label, frag_now);
252b5132
RH
11710 S_SET_VALUE (label, (valueT) frag_now_fix ());
11711 }
11712}
11713
11714/* Align to a given power of two. .align 0 turns off the automatic
11715 alignment used by the data creating pseudo-ops. */
11716
11717static void
11718s_align (x)
43841e91 11719 int x ATTRIBUTE_UNUSED;
252b5132
RH
11720{
11721 register int temp;
11722 register long temp_fill;
11723 long max_alignment = 15;
11724
11725 /*
11726
11727 o Note that the assembler pulls down any immediately preceeding label
11728 to the aligned address.
11729 o It's not documented but auto alignment is reinstated by
11730 a .align pseudo instruction.
11731 o Note also that after auto alignment is turned off the mips assembler
11732 issues an error on attempt to assemble an improperly aligned data item.
11733 We don't.
11734
11735 */
11736
11737 temp = get_absolute_expression ();
11738 if (temp > max_alignment)
11739 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11740 else if (temp < 0)
11741 {
11742 as_warn (_("Alignment negative: 0 assumed."));
11743 temp = 0;
11744 }
11745 if (*input_line_pointer == ',')
11746 {
f9419b05 11747 ++input_line_pointer;
252b5132
RH
11748 temp_fill = get_absolute_expression ();
11749 }
11750 else
11751 temp_fill = 0;
11752 if (temp)
11753 {
11754 auto_align = 1;
11755 mips_align (temp, (int) temp_fill,
11756 insn_labels != NULL ? insn_labels->label : NULL);
11757 }
11758 else
11759 {
11760 auto_align = 0;
11761 }
11762
11763 demand_empty_rest_of_line ();
11764}
11765
11766void
11767mips_flush_pending_output ()
11768{
b34976b6 11769 mips_emit_delays (FALSE);
252b5132
RH
11770 mips_clear_insn_labels ();
11771}
11772
11773static void
11774s_change_sec (sec)
11775 int sec;
11776{
11777 segT seg;
11778
11779 /* When generating embedded PIC code, we only use the .text, .lit8,
11780 .sdata and .sbss sections. We change the .data and .rdata
11781 pseudo-ops to use .sdata. */
11782 if (mips_pic == EMBEDDED_PIC
11783 && (sec == 'd' || sec == 'r'))
11784 sec = 's';
11785
11786#ifdef OBJ_ELF
11787 /* The ELF backend needs to know that we are changing sections, so
11788 that .previous works correctly. We could do something like check
b6ff326e 11789 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11790 as it would not be appropriate to use it in the section changing
11791 functions in read.c, since obj-elf.c intercepts those. FIXME:
11792 This should be cleaner, somehow. */
11793 obj_elf_section_change_hook ();
11794#endif
11795
b34976b6 11796 mips_emit_delays (FALSE);
252b5132
RH
11797 switch (sec)
11798 {
11799 case 't':
11800 s_text (0);
11801 break;
11802 case 'd':
11803 s_data (0);
11804 break;
11805 case 'b':
11806 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11807 demand_empty_rest_of_line ();
11808 break;
11809
11810 case 'r':
11811 if (USE_GLOBAL_POINTER_OPT)
11812 {
11813 seg = subseg_new (RDATA_SECTION_NAME,
11814 (subsegT) get_absolute_expression ());
11815 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11816 {
11817 bfd_set_section_flags (stdoutput, seg,
11818 (SEC_ALLOC
11819 | SEC_LOAD
11820 | SEC_READONLY
11821 | SEC_RELOC
11822 | SEC_DATA));
11823 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11824 record_alignment (seg, 4);
252b5132
RH
11825 }
11826 demand_empty_rest_of_line ();
11827 }
11828 else
11829 {
11830 as_bad (_("No read only data section in this object file format"));
11831 demand_empty_rest_of_line ();
11832 return;
11833 }
11834 break;
11835
11836 case 's':
11837 if (USE_GLOBAL_POINTER_OPT)
11838 {
11839 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11840 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11841 {
11842 bfd_set_section_flags (stdoutput, seg,
11843 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11844 | SEC_DATA);
11845 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11846 record_alignment (seg, 4);
252b5132
RH
11847 }
11848 demand_empty_rest_of_line ();
11849 break;
11850 }
11851 else
11852 {
11853 as_bad (_("Global pointers not supported; recompile -G 0"));
11854 demand_empty_rest_of_line ();
11855 return;
11856 }
11857 }
11858
11859 auto_align = 1;
11860}
b34976b6 11861
cca86cc8
SC
11862void
11863s_change_section (ignore)
11864 int ignore ATTRIBUTE_UNUSED;
11865{
7ed4a06a 11866#ifdef OBJ_ELF
cca86cc8
SC
11867 char *section_name;
11868 char c;
4cf0dd0d 11869 char next_c;
cca86cc8
SC
11870 int section_type;
11871 int section_flag;
11872 int section_entry_size;
11873 int section_alignment;
b34976b6 11874
7ed4a06a
TS
11875 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11876 return;
11877
cca86cc8
SC
11878 section_name = input_line_pointer;
11879 c = get_symbol_end ();
a816d1ed
AO
11880 if (c)
11881 next_c = *(input_line_pointer + 1);
cca86cc8 11882
4cf0dd0d
TS
11883 /* Do we have .section Name<,"flags">? */
11884 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 11885 {
4cf0dd0d
TS
11886 /* just after name is now '\0'. */
11887 *input_line_pointer = c;
cca86cc8
SC
11888 input_line_pointer = section_name;
11889 obj_elf_section (ignore);
11890 return;
11891 }
11892 input_line_pointer++;
11893
11894 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11895 if (c == ',')
11896 section_type = get_absolute_expression ();
11897 else
11898 section_type = 0;
11899 if (*input_line_pointer++ == ',')
11900 section_flag = get_absolute_expression ();
11901 else
11902 section_flag = 0;
11903 if (*input_line_pointer++ == ',')
11904 section_entry_size = get_absolute_expression ();
11905 else
11906 section_entry_size = 0;
11907 if (*input_line_pointer++ == ',')
11908 section_alignment = get_absolute_expression ();
11909 else
11910 section_alignment = 0;
11911
a816d1ed
AO
11912 section_name = xstrdup (section_name);
11913
cca86cc8
SC
11914 obj_elf_change_section (section_name, section_type, section_flag,
11915 section_entry_size, 0, 0, 0);
a816d1ed
AO
11916
11917 if (now_seg->name != section_name)
11918 free (section_name);
7ed4a06a 11919#endif /* OBJ_ELF */
cca86cc8 11920}
252b5132
RH
11921
11922void
11923mips_enable_auto_align ()
11924{
11925 auto_align = 1;
11926}
11927
11928static void
11929s_cons (log_size)
11930 int log_size;
11931{
11932 symbolS *label;
11933
11934 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 11935 mips_emit_delays (FALSE);
252b5132
RH
11936 if (log_size > 0 && auto_align)
11937 mips_align (log_size, 0, label);
11938 mips_clear_insn_labels ();
11939 cons (1 << log_size);
11940}
11941
11942static void
11943s_float_cons (type)
11944 int type;
11945{
11946 symbolS *label;
11947
11948 label = insn_labels != NULL ? insn_labels->label : NULL;
11949
b34976b6 11950 mips_emit_delays (FALSE);
252b5132
RH
11951
11952 if (auto_align)
49309057
ILT
11953 {
11954 if (type == 'd')
11955 mips_align (3, 0, label);
11956 else
11957 mips_align (2, 0, label);
11958 }
252b5132
RH
11959
11960 mips_clear_insn_labels ();
11961
11962 float_cons (type);
11963}
11964
11965/* Handle .globl. We need to override it because on Irix 5 you are
11966 permitted to say
11967 .globl foo .text
11968 where foo is an undefined symbol, to mean that foo should be
11969 considered to be the address of a function. */
11970
11971static void
11972s_mips_globl (x)
43841e91 11973 int x ATTRIBUTE_UNUSED;
252b5132
RH
11974{
11975 char *name;
11976 int c;
11977 symbolS *symbolP;
11978 flagword flag;
11979
11980 name = input_line_pointer;
11981 c = get_symbol_end ();
11982 symbolP = symbol_find_or_make (name);
11983 *input_line_pointer = c;
11984 SKIP_WHITESPACE ();
11985
11986 /* On Irix 5, every global symbol that is not explicitly labelled as
11987 being a function is apparently labelled as being an object. */
11988 flag = BSF_OBJECT;
11989
11990 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11991 {
11992 char *secname;
11993 asection *sec;
11994
11995 secname = input_line_pointer;
11996 c = get_symbol_end ();
11997 sec = bfd_get_section_by_name (stdoutput, secname);
11998 if (sec == NULL)
11999 as_bad (_("%s: no such section"), secname);
12000 *input_line_pointer = c;
12001
12002 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12003 flag = BSF_FUNCTION;
12004 }
12005
49309057 12006 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
12007
12008 S_SET_EXTERNAL (symbolP);
12009 demand_empty_rest_of_line ();
12010}
12011
12012static void
12013s_option (x)
43841e91 12014 int x ATTRIBUTE_UNUSED;
252b5132
RH
12015{
12016 char *opt;
12017 char c;
12018
12019 opt = input_line_pointer;
12020 c = get_symbol_end ();
12021
12022 if (*opt == 'O')
12023 {
12024 /* FIXME: What does this mean? */
12025 }
12026 else if (strncmp (opt, "pic", 3) == 0)
12027 {
12028 int i;
12029
12030 i = atoi (opt + 3);
12031 if (i == 0)
12032 mips_pic = NO_PIC;
12033 else if (i == 2)
12034 mips_pic = SVR4_PIC;
12035 else
12036 as_bad (_(".option pic%d not supported"), i);
12037
12038 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
12039 {
12040 if (g_switch_seen && g_switch_value != 0)
12041 as_warn (_("-G may not be used with SVR4 PIC code"));
12042 g_switch_value = 0;
12043 bfd_set_gp_size (stdoutput, 0);
12044 }
12045 }
12046 else
12047 as_warn (_("Unrecognized option \"%s\""), opt);
12048
12049 *input_line_pointer = c;
12050 demand_empty_rest_of_line ();
12051}
12052
12053/* This structure is used to hold a stack of .set values. */
12054
e972090a
NC
12055struct mips_option_stack
12056{
252b5132
RH
12057 struct mips_option_stack *next;
12058 struct mips_set_options options;
12059};
12060
12061static struct mips_option_stack *mips_opts_stack;
12062
12063/* Handle the .set pseudo-op. */
12064
12065static void
12066s_mipsset (x)
43841e91 12067 int x ATTRIBUTE_UNUSED;
252b5132
RH
12068{
12069 char *name = input_line_pointer, ch;
12070
12071 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 12072 ++input_line_pointer;
252b5132
RH
12073 ch = *input_line_pointer;
12074 *input_line_pointer = '\0';
12075
12076 if (strcmp (name, "reorder") == 0)
12077 {
12078 if (mips_opts.noreorder && prev_nop_frag != NULL)
12079 {
12080 /* If we still have pending nops, we can discard them. The
12081 usual nop handling will insert any that are still
bdaaa2e1 12082 needed. */
252b5132
RH
12083 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12084 * (mips_opts.mips16 ? 2 : 4));
12085 prev_nop_frag = NULL;
12086 }
12087 mips_opts.noreorder = 0;
12088 }
12089 else if (strcmp (name, "noreorder") == 0)
12090 {
b34976b6 12091 mips_emit_delays (TRUE);
252b5132
RH
12092 mips_opts.noreorder = 1;
12093 mips_any_noreorder = 1;
12094 }
12095 else if (strcmp (name, "at") == 0)
12096 {
12097 mips_opts.noat = 0;
12098 }
12099 else if (strcmp (name, "noat") == 0)
12100 {
12101 mips_opts.noat = 1;
12102 }
12103 else if (strcmp (name, "macro") == 0)
12104 {
12105 mips_opts.warn_about_macros = 0;
12106 }
12107 else if (strcmp (name, "nomacro") == 0)
12108 {
12109 if (mips_opts.noreorder == 0)
12110 as_bad (_("`noreorder' must be set before `nomacro'"));
12111 mips_opts.warn_about_macros = 1;
12112 }
12113 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12114 {
12115 mips_opts.nomove = 0;
12116 }
12117 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12118 {
12119 mips_opts.nomove = 1;
12120 }
12121 else if (strcmp (name, "bopt") == 0)
12122 {
12123 mips_opts.nobopt = 0;
12124 }
12125 else if (strcmp (name, "nobopt") == 0)
12126 {
12127 mips_opts.nobopt = 1;
12128 }
12129 else if (strcmp (name, "mips16") == 0
12130 || strcmp (name, "MIPS-16") == 0)
12131 mips_opts.mips16 = 1;
12132 else if (strcmp (name, "nomips16") == 0
12133 || strcmp (name, "noMIPS-16") == 0)
12134 mips_opts.mips16 = 0;
1f25f5d3
CD
12135 else if (strcmp (name, "mips3d") == 0)
12136 mips_opts.ase_mips3d = 1;
12137 else if (strcmp (name, "nomips3d") == 0)
12138 mips_opts.ase_mips3d = 0;
a4672219
TS
12139 else if (strcmp (name, "mdmx") == 0)
12140 mips_opts.ase_mdmx = 1;
12141 else if (strcmp (name, "nomdmx") == 0)
12142 mips_opts.ase_mdmx = 0;
252b5132
RH
12143 else if (strncmp (name, "mips", 4) == 0)
12144 {
af7ee8bf 12145 int reset = 0;
252b5132
RH
12146
12147 /* Permit the user to change the ISA on the fly. Needless to
12148 say, misuse can cause serious problems. */
af7ee8bf
CD
12149 if (strcmp (name, "mips0") == 0)
12150 {
12151 reset = 1;
12152 mips_opts.isa = file_mips_isa;
12153 }
12154 else if (strcmp (name, "mips1") == 0)
12155 mips_opts.isa = ISA_MIPS1;
12156 else if (strcmp (name, "mips2") == 0)
12157 mips_opts.isa = ISA_MIPS2;
12158 else if (strcmp (name, "mips3") == 0)
12159 mips_opts.isa = ISA_MIPS3;
12160 else if (strcmp (name, "mips4") == 0)
12161 mips_opts.isa = ISA_MIPS4;
12162 else if (strcmp (name, "mips5") == 0)
12163 mips_opts.isa = ISA_MIPS5;
12164 else if (strcmp (name, "mips32") == 0)
12165 mips_opts.isa = ISA_MIPS32;
12166 else if (strcmp (name, "mips32r2") == 0)
12167 mips_opts.isa = ISA_MIPS32R2;
12168 else if (strcmp (name, "mips64") == 0)
12169 mips_opts.isa = ISA_MIPS64;
12170 else
12171 as_bad (_("unknown ISA level %s"), name + 4);
12172
12173 switch (mips_opts.isa)
98d3f06f
KH
12174 {
12175 case 0:
98d3f06f 12176 break;
af7ee8bf
CD
12177 case ISA_MIPS1:
12178 case ISA_MIPS2:
12179 case ISA_MIPS32:
12180 case ISA_MIPS32R2:
98d3f06f
KH
12181 mips_opts.gp32 = 1;
12182 mips_opts.fp32 = 1;
12183 break;
af7ee8bf
CD
12184 case ISA_MIPS3:
12185 case ISA_MIPS4:
12186 case ISA_MIPS5:
12187 case ISA_MIPS64:
98d3f06f
KH
12188 mips_opts.gp32 = 0;
12189 mips_opts.fp32 = 0;
12190 break;
12191 default:
12192 as_bad (_("unknown ISA level %s"), name + 4);
12193 break;
12194 }
af7ee8bf 12195 if (reset)
98d3f06f 12196 {
af7ee8bf
CD
12197 mips_opts.gp32 = file_mips_gp32;
12198 mips_opts.fp32 = file_mips_fp32;
98d3f06f 12199 }
252b5132
RH
12200 }
12201 else if (strcmp (name, "autoextend") == 0)
12202 mips_opts.noautoextend = 0;
12203 else if (strcmp (name, "noautoextend") == 0)
12204 mips_opts.noautoextend = 1;
12205 else if (strcmp (name, "push") == 0)
12206 {
12207 struct mips_option_stack *s;
12208
12209 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12210 s->next = mips_opts_stack;
12211 s->options = mips_opts;
12212 mips_opts_stack = s;
12213 }
12214 else if (strcmp (name, "pop") == 0)
12215 {
12216 struct mips_option_stack *s;
12217
12218 s = mips_opts_stack;
12219 if (s == NULL)
12220 as_bad (_(".set pop with no .set push"));
12221 else
12222 {
12223 /* If we're changing the reorder mode we need to handle
12224 delay slots correctly. */
12225 if (s->options.noreorder && ! mips_opts.noreorder)
b34976b6 12226 mips_emit_delays (TRUE);
252b5132
RH
12227 else if (! s->options.noreorder && mips_opts.noreorder)
12228 {
12229 if (prev_nop_frag != NULL)
12230 {
12231 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12232 * (mips_opts.mips16 ? 2 : 4));
12233 prev_nop_frag = NULL;
12234 }
12235 }
12236
12237 mips_opts = s->options;
12238 mips_opts_stack = s->next;
12239 free (s);
12240 }
12241 }
12242 else
12243 {
12244 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12245 }
12246 *input_line_pointer = ch;
12247 demand_empty_rest_of_line ();
12248}
12249
12250/* Handle the .abicalls pseudo-op. I believe this is equivalent to
12251 .option pic2. It means to generate SVR4 PIC calls. */
12252
12253static void
12254s_abicalls (ignore)
43841e91 12255 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12256{
12257 mips_pic = SVR4_PIC;
12258 if (USE_GLOBAL_POINTER_OPT)
12259 {
12260 if (g_switch_seen && g_switch_value != 0)
12261 as_warn (_("-G may not be used with SVR4 PIC code"));
12262 g_switch_value = 0;
12263 }
12264 bfd_set_gp_size (stdoutput, 0);
12265 demand_empty_rest_of_line ();
12266}
12267
12268/* Handle the .cpload pseudo-op. This is used when generating SVR4
12269 PIC code. It sets the $gp register for the function based on the
12270 function address, which is in the register named in the argument.
12271 This uses a relocation against _gp_disp, which is handled specially
12272 by the linker. The result is:
12273 lui $gp,%hi(_gp_disp)
12274 addiu $gp,$gp,%lo(_gp_disp)
12275 addu $gp,$gp,.cpload argument
12276 The .cpload argument is normally $25 == $t9. */
12277
12278static void
12279s_cpload (ignore)
43841e91 12280 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12281{
12282 expressionS ex;
12283 int icnt = 0;
12284
6478892d
TS
12285 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12286 .cpload is ignored. */
12287 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12288 {
12289 s_ignore (0);
12290 return;
12291 }
12292
d3ecfc59 12293 /* .cpload should be in a .set noreorder section. */
252b5132
RH
12294 if (mips_opts.noreorder == 0)
12295 as_warn (_(".cpload not in noreorder section"));
12296
12297 ex.X_op = O_symbol;
12298 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12299 ex.X_op_symbol = NULL;
12300 ex.X_add_number = 0;
12301
12302 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 12303 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 12304
c9914766
TS
12305 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12306 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12307 mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
252b5132
RH
12308
12309 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
c9914766 12310 mips_gp_register, mips_gp_register, tc_get_register (0));
252b5132
RH
12311
12312 demand_empty_rest_of_line ();
12313}
12314
6478892d
TS
12315/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12316 .cpsetup $reg1, offset|$reg2, label
12317
12318 If offset is given, this results in:
12319 sd $gp, offset($sp)
956cd1d6 12320 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12321 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12322 daddu $gp, $gp, $reg1
6478892d
TS
12323
12324 If $reg2 is given, this results in:
12325 daddu $reg2, $gp, $0
956cd1d6 12326 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12327 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12328 daddu $gp, $gp, $reg1
12329 $reg1 is normally $25 == $t9. */
6478892d
TS
12330static void
12331s_cpsetup (ignore)
12332 int ignore ATTRIBUTE_UNUSED;
12333{
12334 expressionS ex_off;
12335 expressionS ex_sym;
12336 int reg1;
12337 int icnt = 0;
f21f8242 12338 char *f;
6478892d 12339
8586fc66 12340 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
12341 We also need NewABI support. */
12342 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12343 {
12344 s_ignore (0);
12345 return;
12346 }
12347
12348 reg1 = tc_get_register (0);
12349 SKIP_WHITESPACE ();
12350 if (*input_line_pointer != ',')
12351 {
12352 as_bad (_("missing argument separator ',' for .cpsetup"));
12353 return;
12354 }
12355 else
80245285 12356 ++input_line_pointer;
6478892d
TS
12357 SKIP_WHITESPACE ();
12358 if (*input_line_pointer == '$')
80245285
TS
12359 {
12360 mips_cpreturn_register = tc_get_register (0);
12361 mips_cpreturn_offset = -1;
12362 }
6478892d 12363 else
80245285
TS
12364 {
12365 mips_cpreturn_offset = get_absolute_expression ();
12366 mips_cpreturn_register = -1;
12367 }
6478892d
TS
12368 SKIP_WHITESPACE ();
12369 if (*input_line_pointer != ',')
12370 {
12371 as_bad (_("missing argument separator ',' for .cpsetup"));
12372 return;
12373 }
12374 else
f9419b05 12375 ++input_line_pointer;
6478892d 12376 SKIP_WHITESPACE ();
f21f8242 12377 expression (&ex_sym);
6478892d
TS
12378
12379 if (mips_cpreturn_register == -1)
12380 {
12381 ex_off.X_op = O_constant;
12382 ex_off.X_add_symbol = NULL;
12383 ex_off.X_op_symbol = NULL;
12384 ex_off.X_add_number = mips_cpreturn_offset;
12385
12386 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12387 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12388 }
12389 else
12390 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12391 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12392
f21f8242
AO
12393 /* Ensure there's room for the next two instructions, so that `f'
12394 doesn't end up with an address in the wrong frag. */
12395 frag_grow (8);
12396 f = frag_more (0);
6478892d
TS
12397 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12398 (int) BFD_RELOC_GPREL16);
f21f8242
AO
12399 fix_new (frag_now, f - frag_now->fr_literal,
12400 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12401 fix_new (frag_now, f - frag_now->fr_literal,
12402 0, NULL, 0, 0, BFD_RELOC_HI16_S);
12403
12404 f = frag_more (0);
6478892d
TS
12405 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12406 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
f21f8242
AO
12407 fix_new (frag_now, f - frag_now->fr_literal,
12408 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12409 fix_new (frag_now, f - frag_now->fr_literal,
12410 0, NULL, 0, 0, BFD_RELOC_LO16);
12411
8586fc66
TS
12412 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12413 HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
12414 mips_gp_register, mips_gp_register, reg1);
6478892d
TS
12415
12416 demand_empty_rest_of_line ();
12417}
12418
12419static void
12420s_cplocal (ignore)
12421 int ignore ATTRIBUTE_UNUSED;
12422{
12423 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12424 .cplocal is ignored. */
12425 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12426 {
12427 s_ignore (0);
12428 return;
12429 }
12430
12431 mips_gp_register = tc_get_register (0);
85b51719 12432 demand_empty_rest_of_line ();
6478892d
TS
12433}
12434
252b5132
RH
12435/* Handle the .cprestore pseudo-op. This stores $gp into a given
12436 offset from $sp. The offset is remembered, and after making a PIC
12437 call $gp is restored from that location. */
12438
12439static void
12440s_cprestore (ignore)
43841e91 12441 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12442{
12443 expressionS ex;
12444 int icnt = 0;
12445
6478892d 12446 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 12447 .cprestore is ignored. */
6478892d 12448 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12449 {
12450 s_ignore (0);
12451 return;
12452 }
12453
12454 mips_cprestore_offset = get_absolute_expression ();
7a621144 12455 mips_cprestore_valid = 1;
252b5132
RH
12456
12457 ex.X_op = O_constant;
12458 ex.X_add_symbol = NULL;
12459 ex.X_op_symbol = NULL;
12460 ex.X_add_number = mips_cprestore_offset;
12461
885add95
CD
12462 macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12463 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12464 mips_gp_register, SP);
252b5132
RH
12465
12466 demand_empty_rest_of_line ();
12467}
12468
6478892d
TS
12469/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12470 was given in the preceeding .gpsetup, it results in:
12471 ld $gp, offset($sp)
76b3015f 12472
6478892d
TS
12473 If a register $reg2 was given there, it results in:
12474 daddiu $gp, $gp, $reg2
12475 */
12476static void
12477s_cpreturn (ignore)
12478 int ignore ATTRIBUTE_UNUSED;
12479{
12480 expressionS ex;
12481 int icnt = 0;
12482
12483 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12484 We also need NewABI support. */
12485 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12486 {
12487 s_ignore (0);
12488 return;
12489 }
12490
12491 if (mips_cpreturn_register == -1)
12492 {
12493 ex.X_op = O_constant;
12494 ex.X_add_symbol = NULL;
12495 ex.X_op_symbol = NULL;
12496 ex.X_add_number = mips_cpreturn_offset;
12497
12498 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12499 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12500 }
12501 else
12502 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12503 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12504
12505 demand_empty_rest_of_line ();
12506}
12507
12508/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12509 code. It sets the offset to use in gp_rel relocations. */
12510
12511static void
12512s_gpvalue (ignore)
12513 int ignore ATTRIBUTE_UNUSED;
12514{
12515 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12516 We also need NewABI support. */
12517 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12518 {
12519 s_ignore (0);
12520 return;
12521 }
12522
def2e0dd 12523 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12524
12525 demand_empty_rest_of_line ();
12526}
12527
252b5132
RH
12528/* Handle the .gpword pseudo-op. This is used when generating PIC
12529 code. It generates a 32 bit GP relative reloc. */
12530
12531static void
12532s_gpword (ignore)
43841e91 12533 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12534{
12535 symbolS *label;
12536 expressionS ex;
12537 char *p;
12538
12539 /* When not generating PIC code, this is treated as .word. */
12540 if (mips_pic != SVR4_PIC)
12541 {
12542 s_cons (2);
12543 return;
12544 }
12545
12546 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12547 mips_emit_delays (TRUE);
252b5132
RH
12548 if (auto_align)
12549 mips_align (2, 0, label);
12550 mips_clear_insn_labels ();
12551
12552 expression (&ex);
12553
12554 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12555 {
12556 as_bad (_("Unsupported use of .gpword"));
12557 ignore_rest_of_line ();
12558 }
12559
12560 p = frag_more (4);
12561 md_number_to_chars (p, (valueT) 0, 4);
b34976b6 12562 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 12563 BFD_RELOC_GPREL32);
252b5132
RH
12564
12565 demand_empty_rest_of_line ();
12566}
12567
10181a0d
AO
12568static void
12569s_gpdword (ignore)
12570 int ignore ATTRIBUTE_UNUSED;
12571{
12572 symbolS *label;
12573 expressionS ex;
12574 char *p;
12575
12576 /* When not generating PIC code, this is treated as .dword. */
12577 if (mips_pic != SVR4_PIC)
12578 {
12579 s_cons (3);
12580 return;
12581 }
12582
12583 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12584 mips_emit_delays (TRUE);
10181a0d
AO
12585 if (auto_align)
12586 mips_align (3, 0, label);
12587 mips_clear_insn_labels ();
12588
12589 expression (&ex);
12590
12591 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12592 {
12593 as_bad (_("Unsupported use of .gpdword"));
12594 ignore_rest_of_line ();
12595 }
12596
12597 p = frag_more (8);
12598 md_number_to_chars (p, (valueT) 0, 8);
b34976b6 12599 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
10181a0d
AO
12600 BFD_RELOC_GPREL32);
12601
12602 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12603 ex.X_op = O_absent;
12604 ex.X_add_symbol = 0;
12605 ex.X_add_number = 0;
b34976b6 12606 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
10181a0d
AO
12607 BFD_RELOC_64);
12608
12609 demand_empty_rest_of_line ();
12610}
12611
252b5132
RH
12612/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12613 tables in SVR4 PIC code. */
12614
12615static void
12616s_cpadd (ignore)
43841e91 12617 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12618{
12619 int icnt = 0;
12620 int reg;
12621
10181a0d
AO
12622 /* This is ignored when not generating SVR4 PIC code. */
12623 if (mips_pic != SVR4_PIC)
252b5132
RH
12624 {
12625 s_ignore (0);
12626 return;
12627 }
12628
12629 /* Add $gp to the register named as an argument. */
12630 reg = tc_get_register (0);
12631 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 12632 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 12633 "d,v,t", reg, reg, mips_gp_register);
252b5132 12634
bdaaa2e1 12635 demand_empty_rest_of_line ();
252b5132
RH
12636}
12637
12638/* Handle the .insn pseudo-op. This marks instruction labels in
12639 mips16 mode. This permits the linker to handle them specially,
12640 such as generating jalx instructions when needed. We also make
12641 them odd for the duration of the assembly, in order to generate the
12642 right sort of code. We will make them even in the adjust_symtab
12643 routine, while leaving them marked. This is convenient for the
12644 debugger and the disassembler. The linker knows to make them odd
12645 again. */
12646
12647static void
12648s_insn (ignore)
43841e91 12649 int ignore ATTRIBUTE_UNUSED;
252b5132 12650{
f9419b05 12651 mips16_mark_labels ();
252b5132
RH
12652
12653 demand_empty_rest_of_line ();
12654}
12655
12656/* Handle a .stabn directive. We need these in order to mark a label
12657 as being a mips16 text label correctly. Sometimes the compiler
12658 will emit a label, followed by a .stabn, and then switch sections.
12659 If the label and .stabn are in mips16 mode, then the label is
12660 really a mips16 text label. */
12661
12662static void
12663s_mips_stab (type)
12664 int type;
12665{
f9419b05 12666 if (type == 'n')
252b5132
RH
12667 mips16_mark_labels ();
12668
12669 s_stab (type);
12670}
12671
12672/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12673 */
12674
12675static void
12676s_mips_weakext (ignore)
43841e91 12677 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12678{
12679 char *name;
12680 int c;
12681 symbolS *symbolP;
12682 expressionS exp;
12683
12684 name = input_line_pointer;
12685 c = get_symbol_end ();
12686 symbolP = symbol_find_or_make (name);
12687 S_SET_WEAK (symbolP);
12688 *input_line_pointer = c;
12689
12690 SKIP_WHITESPACE ();
12691
12692 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12693 {
12694 if (S_IS_DEFINED (symbolP))
12695 {
956cd1d6 12696 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12697 S_GET_NAME (symbolP));
12698 ignore_rest_of_line ();
12699 return;
12700 }
bdaaa2e1 12701
252b5132
RH
12702 if (*input_line_pointer == ',')
12703 {
12704 ++input_line_pointer;
12705 SKIP_WHITESPACE ();
12706 }
bdaaa2e1 12707
252b5132
RH
12708 expression (&exp);
12709 if (exp.X_op != O_symbol)
12710 {
12711 as_bad ("bad .weakext directive");
98d3f06f 12712 ignore_rest_of_line ();
252b5132
RH
12713 return;
12714 }
49309057 12715 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12716 }
12717
12718 demand_empty_rest_of_line ();
12719}
12720
12721/* Parse a register string into a number. Called from the ECOFF code
12722 to parse .frame. The argument is non-zero if this is the frame
12723 register, so that we can record it in mips_frame_reg. */
12724
12725int
12726tc_get_register (frame)
12727 int frame;
12728{
12729 int reg;
12730
12731 SKIP_WHITESPACE ();
12732 if (*input_line_pointer++ != '$')
12733 {
12734 as_warn (_("expected `$'"));
85b51719 12735 reg = ZERO;
252b5132 12736 }
3882b010 12737 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12738 {
12739 reg = get_absolute_expression ();
12740 if (reg < 0 || reg >= 32)
12741 {
12742 as_warn (_("Bad register number"));
85b51719 12743 reg = ZERO;
252b5132
RH
12744 }
12745 }
12746 else
12747 {
76db943d 12748 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12749 {
12750 reg = RA;
12751 input_line_pointer += 2;
12752 }
76db943d 12753 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12754 {
12755 reg = FP;
12756 input_line_pointer += 2;
12757 }
252b5132 12758 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12759 {
12760 reg = SP;
12761 input_line_pointer += 2;
12762 }
252b5132 12763 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12764 {
12765 reg = GP;
12766 input_line_pointer += 2;
12767 }
252b5132 12768 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12769 {
12770 reg = AT;
12771 input_line_pointer += 2;
12772 }
12773 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12774 {
12775 reg = KT0;
12776 input_line_pointer += 3;
12777 }
12778 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12779 {
12780 reg = KT1;
12781 input_line_pointer += 3;
12782 }
12783 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12784 {
12785 reg = ZERO;
12786 input_line_pointer += 4;
12787 }
252b5132
RH
12788 else
12789 {
12790 as_warn (_("Unrecognized register name"));
85b51719
TS
12791 reg = ZERO;
12792 while (ISALNUM(*input_line_pointer))
12793 input_line_pointer++;
252b5132 12794 }
252b5132
RH
12795 }
12796 if (frame)
7a621144
DJ
12797 {
12798 mips_frame_reg = reg != 0 ? reg : SP;
12799 mips_frame_reg_valid = 1;
12800 mips_cprestore_valid = 0;
12801 }
252b5132
RH
12802 return reg;
12803}
12804
12805valueT
12806md_section_align (seg, addr)
12807 asection *seg;
12808 valueT addr;
12809{
12810 int align = bfd_get_section_alignment (stdoutput, seg);
12811
12812#ifdef OBJ_ELF
12813 /* We don't need to align ELF sections to the full alignment.
12814 However, Irix 5 may prefer that we align them at least to a 16
12815 byte boundary. We don't bother to align the sections if we are
12816 targeted for an embedded system. */
12817 if (strcmp (TARGET_OS, "elf") == 0)
12818 return addr;
12819 if (align > 4)
12820 align = 4;
12821#endif
12822
12823 return ((addr + (1 << align) - 1) & (-1 << align));
12824}
12825
12826/* Utility routine, called from above as well. If called while the
12827 input file is still being read, it's only an approximation. (For
12828 example, a symbol may later become defined which appeared to be
12829 undefined earlier.) */
12830
12831static int
12832nopic_need_relax (sym, before_relaxing)
12833 symbolS *sym;
12834 int before_relaxing;
12835{
12836 if (sym == 0)
12837 return 0;
12838
6478892d 12839 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
252b5132
RH
12840 {
12841 const char *symname;
12842 int change;
12843
c9914766 12844 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12845 register. It can be if it is smaller than the -G size or if
12846 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12847 not be referenced off the $gp, although it appears as though
252b5132
RH
12848 they can. */
12849 symname = S_GET_NAME (sym);
12850 if (symname != (const char *) NULL
12851 && (strcmp (symname, "eprol") == 0
12852 || strcmp (symname, "etext") == 0
12853 || strcmp (symname, "_gp") == 0
12854 || strcmp (symname, "edata") == 0
12855 || strcmp (symname, "_fbss") == 0
12856 || strcmp (symname, "_fdata") == 0
12857 || strcmp (symname, "_ftext") == 0
12858 || strcmp (symname, "end") == 0
12859 || strcmp (symname, "_gp_disp") == 0))
12860 change = 1;
12861 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12862 && (0
12863#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12864 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12865 && (symbol_get_obj (sym)->ecoff_extern_size
12866 <= g_switch_value))
252b5132
RH
12867#endif
12868 /* We must defer this decision until after the whole
12869 file has been read, since there might be a .extern
12870 after the first use of this symbol. */
12871 || (before_relaxing
12872#ifndef NO_ECOFF_DEBUGGING
49309057 12873 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12874#endif
12875 && S_GET_VALUE (sym) == 0)
12876 || (S_GET_VALUE (sym) != 0
12877 && S_GET_VALUE (sym) <= g_switch_value)))
12878 change = 0;
12879 else
12880 {
12881 const char *segname;
12882
12883 segname = segment_name (S_GET_SEGMENT (sym));
12884 assert (strcmp (segname, ".lit8") != 0
12885 && strcmp (segname, ".lit4") != 0);
12886 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12887 && strcmp (segname, ".sbss") != 0
12888 && strncmp (segname, ".sdata.", 7) != 0
12889 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12890 }
12891 return change;
12892 }
12893 else
c9914766 12894 /* We are not optimizing for the $gp register. */
252b5132
RH
12895 return 1;
12896}
12897
12898/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12899 extended opcode. SEC is the section the frag is in. */
12900
12901static int
12902mips16_extended_frag (fragp, sec, stretch)
12903 fragS *fragp;
12904 asection *sec;
12905 long stretch;
12906{
12907 int type;
12908 register const struct mips16_immed_operand *op;
12909 offsetT val;
12910 int mintiny, maxtiny;
12911 segT symsec;
98aa84af 12912 fragS *sym_frag;
252b5132
RH
12913
12914 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12915 return 0;
12916 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12917 return 1;
12918
12919 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12920 op = mips16_immed_operands;
12921 while (op->type != type)
12922 {
12923 ++op;
12924 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12925 }
12926
12927 if (op->unsp)
12928 {
12929 if (type == '<' || type == '>' || type == '[' || type == ']')
12930 {
12931 mintiny = 1;
12932 maxtiny = 1 << op->nbits;
12933 }
12934 else
12935 {
12936 mintiny = 0;
12937 maxtiny = (1 << op->nbits) - 1;
12938 }
12939 }
12940 else
12941 {
12942 mintiny = - (1 << (op->nbits - 1));
12943 maxtiny = (1 << (op->nbits - 1)) - 1;
12944 }
12945
98aa84af 12946 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12947 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12948 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12949
12950 if (op->pcrel)
12951 {
12952 addressT addr;
12953
12954 /* We won't have the section when we are called from
12955 mips_relax_frag. However, we will always have been called
12956 from md_estimate_size_before_relax first. If this is a
12957 branch to a different section, we mark it as such. If SEC is
12958 NULL, and the frag is not marked, then it must be a branch to
12959 the same section. */
12960 if (sec == NULL)
12961 {
12962 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12963 return 1;
12964 }
12965 else
12966 {
98aa84af 12967 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12968 if (symsec != sec)
12969 {
12970 fragp->fr_subtype =
12971 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12972
12973 /* FIXME: We should support this, and let the linker
12974 catch branches and loads that are out of range. */
12975 as_bad_where (fragp->fr_file, fragp->fr_line,
12976 _("unsupported PC relative reference to different section"));
12977
12978 return 1;
12979 }
98aa84af
AM
12980 if (fragp != sym_frag && sym_frag->fr_address == 0)
12981 /* Assume non-extended on the first relaxation pass.
12982 The address we have calculated will be bogus if this is
12983 a forward branch to another frag, as the forward frag
12984 will have fr_address == 0. */
12985 return 0;
252b5132
RH
12986 }
12987
12988 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12989 the same section. If the relax_marker of the symbol fragment
12990 differs from the relax_marker of this fragment, we have not
12991 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12992 in STRETCH in order to get a better estimate of the address.
12993 This particularly matters because of the shift bits. */
12994 if (stretch != 0
98aa84af 12995 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12996 {
12997 fragS *f;
12998
12999 /* Adjust stretch for any alignment frag. Note that if have
13000 been expanding the earlier code, the symbol may be
13001 defined in what appears to be an earlier frag. FIXME:
13002 This doesn't handle the fr_subtype field, which specifies
13003 a maximum number of bytes to skip when doing an
13004 alignment. */
98aa84af 13005 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
13006 {
13007 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13008 {
13009 if (stretch < 0)
13010 stretch = - ((- stretch)
13011 & ~ ((1 << (int) f->fr_offset) - 1));
13012 else
13013 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13014 if (stretch == 0)
13015 break;
13016 }
13017 }
13018 if (f != NULL)
13019 val += stretch;
13020 }
13021
13022 addr = fragp->fr_address + fragp->fr_fix;
13023
13024 /* The base address rules are complicated. The base address of
13025 a branch is the following instruction. The base address of a
13026 PC relative load or add is the instruction itself, but if it
13027 is in a delay slot (in which case it can not be extended) use
13028 the address of the instruction whose delay slot it is in. */
13029 if (type == 'p' || type == 'q')
13030 {
13031 addr += 2;
13032
13033 /* If we are currently assuming that this frag should be
13034 extended, then, the current address is two bytes
bdaaa2e1 13035 higher. */
252b5132
RH
13036 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13037 addr += 2;
13038
13039 /* Ignore the low bit in the target, since it will be set
13040 for a text label. */
13041 if ((val & 1) != 0)
13042 --val;
13043 }
13044 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13045 addr -= 4;
13046 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13047 addr -= 2;
13048
13049 val -= addr & ~ ((1 << op->shift) - 1);
13050
13051 /* Branch offsets have an implicit 0 in the lowest bit. */
13052 if (type == 'p' || type == 'q')
13053 val /= 2;
13054
13055 /* If any of the shifted bits are set, we must use an extended
13056 opcode. If the address depends on the size of this
13057 instruction, this can lead to a loop, so we arrange to always
13058 use an extended opcode. We only check this when we are in
13059 the main relaxation loop, when SEC is NULL. */
13060 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13061 {
13062 fragp->fr_subtype =
13063 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13064 return 1;
13065 }
13066
13067 /* If we are about to mark a frag as extended because the value
13068 is precisely maxtiny + 1, then there is a chance of an
13069 infinite loop as in the following code:
13070 la $4,foo
13071 .skip 1020
13072 .align 2
13073 foo:
13074 In this case when the la is extended, foo is 0x3fc bytes
13075 away, so the la can be shrunk, but then foo is 0x400 away, so
13076 the la must be extended. To avoid this loop, we mark the
13077 frag as extended if it was small, and is about to become
13078 extended with a value of maxtiny + 1. */
13079 if (val == ((maxtiny + 1) << op->shift)
13080 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13081 && sec == NULL)
13082 {
13083 fragp->fr_subtype =
13084 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13085 return 1;
13086 }
13087 }
13088 else if (symsec != absolute_section && sec != NULL)
13089 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13090
13091 if ((val & ((1 << op->shift) - 1)) != 0
13092 || val < (mintiny << op->shift)
13093 || val > (maxtiny << op->shift))
13094 return 1;
13095 else
13096 return 0;
13097}
13098
4a6a3df4
AO
13099/* Compute the length of a branch sequence, and adjust the
13100 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13101 worst-case length is computed, with UPDATE being used to indicate
13102 whether an unconditional (-1), branch-likely (+1) or regular (0)
13103 branch is to be computed. */
13104static int
13105relaxed_branch_length (fragp, sec, update)
13106 fragS *fragp;
13107 asection *sec;
13108 int update;
13109{
b34976b6 13110 bfd_boolean toofar;
4a6a3df4
AO
13111 int length;
13112
13113 if (fragp
13114 && S_IS_DEFINED (fragp->fr_symbol)
13115 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13116 {
13117 addressT addr;
13118 offsetT val;
13119
13120 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13121
13122 addr = fragp->fr_address + fragp->fr_fix + 4;
13123
13124 val -= addr;
13125
13126 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13127 }
13128 else if (fragp)
13129 /* If the symbol is not defined or it's in a different segment,
13130 assume the user knows what's going on and emit a short
13131 branch. */
b34976b6 13132 toofar = FALSE;
4a6a3df4 13133 else
b34976b6 13134 toofar = TRUE;
4a6a3df4
AO
13135
13136 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13137 fragp->fr_subtype
af6ae2ad 13138 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
13139 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13140 RELAX_BRANCH_LINK (fragp->fr_subtype),
13141 toofar);
13142
13143 length = 4;
13144 if (toofar)
13145 {
13146 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13147 length += 8;
13148
13149 if (mips_pic != NO_PIC)
13150 {
13151 /* Additional space for PIC loading of target address. */
13152 length += 8;
13153 if (mips_opts.isa == ISA_MIPS1)
13154 /* Additional space for $at-stabilizing nop. */
13155 length += 4;
13156 }
13157
13158 /* If branch is conditional. */
13159 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13160 length += 8;
13161 }
b34976b6 13162
4a6a3df4
AO
13163 return length;
13164}
13165
252b5132
RH
13166/* Estimate the size of a frag before relaxing. Unless this is the
13167 mips16, we are not really relaxing here, and the final size is
13168 encoded in the subtype information. For the mips16, we have to
13169 decide whether we are using an extended opcode or not. */
13170
252b5132
RH
13171int
13172md_estimate_size_before_relax (fragp, segtype)
13173 fragS *fragp;
13174 asection *segtype;
13175{
43841e91 13176 int change = 0;
b34976b6 13177 bfd_boolean linkonce = FALSE;
252b5132 13178
4a6a3df4
AO
13179 if (RELAX_BRANCH_P (fragp->fr_subtype))
13180 {
13181
b34976b6
AM
13182 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13183
4a6a3df4
AO
13184 return fragp->fr_var;
13185 }
13186
252b5132 13187 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
13188 /* We don't want to modify the EXTENDED bit here; it might get us
13189 into infinite loops. We change it only in mips_relax_frag(). */
13190 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
13191
13192 if (mips_pic == NO_PIC)
13193 {
13194 change = nopic_need_relax (fragp->fr_symbol, 0);
13195 }
13196 else if (mips_pic == SVR4_PIC)
13197 {
13198 symbolS *sym;
13199 asection *symsec;
13200
13201 sym = fragp->fr_symbol;
13202
13203 /* Handle the case of a symbol equated to another symbol. */
e0890092 13204 while (symbol_equated_reloc_p (sym))
252b5132
RH
13205 {
13206 symbolS *n;
13207
13208 /* It's possible to get a loop here in a badly written
13209 program. */
49309057 13210 n = symbol_get_value_expression (sym)->X_add_symbol;
252b5132
RH
13211 if (n == sym)
13212 break;
13213 sym = n;
13214 }
13215
13216 symsec = S_GET_SEGMENT (sym);
13217
8614eeee
UC
13218 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
13219 if (symsec != segtype && ! S_IS_LOCAL (sym))
beae10d5
KH
13220 {
13221 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
13222 != 0)
b34976b6 13223 linkonce = TRUE;
beae10d5
KH
13224
13225 /* The GNU toolchain uses an extension for ELF: a section
13226 beginning with the magic string .gnu.linkonce is a linkonce
13227 section. */
13228 if (strncmp (segment_name (symsec), ".gnu.linkonce",
13229 sizeof ".gnu.linkonce" - 1) == 0)
b34976b6 13230 linkonce = TRUE;
beae10d5 13231 }
8614eeee 13232
252b5132
RH
13233 /* This must duplicate the test in adjust_reloc_syms. */
13234 change = (symsec != &bfd_und_section
13235 && symsec != &bfd_abs_section
426b0403 13236 && ! bfd_is_com_section (symsec)
8614eeee 13237 && !linkonce
426b0403 13238#ifdef OBJ_ELF
ea4ff978 13239 /* A global or weak symbol is treated as external. */
9151e8bf 13240 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
b25a253c 13241 || (! S_IS_WEAK (sym)
bad9ca53
TS
13242 && (! S_IS_EXTERNAL (sym)
13243 || mips_pic == EMBEDDED_PIC)))
426b0403
AM
13244#endif
13245 );
252b5132
RH
13246 }
13247 else
13248 abort ();
13249
13250 if (change)
13251 {
13252 /* Record the offset to the first reloc in the fr_opcode field.
13253 This lets md_convert_frag and tc_gen_reloc know that the code
13254 must be expanded. */
13255 fragp->fr_opcode = (fragp->fr_literal
13256 + fragp->fr_fix
13257 - RELAX_OLD (fragp->fr_subtype)
13258 + RELAX_RELOC1 (fragp->fr_subtype));
13259 /* FIXME: This really needs as_warn_where. */
13260 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
13261 as_warn (_("AT used after \".set noat\" or macro used after "
13262 "\".set nomacro\""));
13263
13264 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
13265 }
13266
9a41af64 13267 return 0;
252b5132
RH
13268}
13269
13270/* This is called to see whether a reloc against a defined symbol
13271 should be converted into a reloc against a section. Don't adjust
13272 MIPS16 jump relocations, so we don't have to worry about the format
13273 of the offset in the .o file. Don't adjust relocations against
13274 mips16 symbols, so that the linker can find them if it needs to set
13275 up a stub. */
13276
13277int
13278mips_fix_adjustable (fixp)
13279 fixS *fixp;
13280{
13281 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13282 return 0;
a161fe53 13283
252b5132
RH
13284 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13285 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13286 return 0;
a161fe53 13287
252b5132
RH
13288 if (fixp->fx_addsy == NULL)
13289 return 1;
a161fe53 13290
252b5132
RH
13291#ifdef OBJ_ELF
13292 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13293 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13294 && fixp->fx_subsy == NULL)
13295 return 0;
13296#endif
a161fe53 13297
252b5132
RH
13298 return 1;
13299}
13300
13301/* Translate internal representation of relocation info to BFD target
13302 format. */
13303
13304arelent **
13305tc_gen_reloc (section, fixp)
43841e91 13306 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
13307 fixS *fixp;
13308{
13309 static arelent *retval[4];
13310 arelent *reloc;
13311 bfd_reloc_code_real_type code;
13312
13313 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13314 retval[1] = NULL;
13315
49309057
ILT
13316 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13317 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13318 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13319
13320 if (mips_pic == EMBEDDED_PIC
13321 && SWITCH_TABLE (fixp))
13322 {
13323 /* For a switch table entry we use a special reloc. The addend
13324 is actually the difference between the reloc address and the
13325 subtrahend. */
13326 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13327 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13328 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13329 fixp->fx_r_type = BFD_RELOC_GPREL32;
13330 }
13331 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13332 {
4514d474
CD
13333 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13334 reloc->addend = fixp->fx_addnumber;
252b5132 13335 else
4514d474
CD
13336 {
13337 /* We use a special addend for an internal RELLO reloc. */
13338 if (symbol_section_p (fixp->fx_addsy))
13339 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13340 else
13341 reloc->addend = fixp->fx_addnumber + reloc->address;
13342 }
252b5132
RH
13343 }
13344 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13345 {
13346 assert (fixp->fx_next != NULL
13347 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
4514d474
CD
13348
13349 /* The reloc is relative to the RELLO; adjust the addend
252b5132 13350 accordingly. */
4514d474
CD
13351 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13352 reloc->addend = fixp->fx_next->fx_addnumber;
252b5132 13353 else
4514d474
CD
13354 {
13355 /* We use a special addend for an internal RELHI reloc. */
13356 if (symbol_section_p (fixp->fx_addsy))
13357 reloc->addend = (fixp->fx_next->fx_frag->fr_address
13358 + fixp->fx_next->fx_where
13359 - S_GET_VALUE (fixp->fx_subsy));
13360 else
13361 reloc->addend = (fixp->fx_addnumber
13362 + fixp->fx_next->fx_frag->fr_address
13363 + fixp->fx_next->fx_where);
13364 }
252b5132 13365 }
4514d474
CD
13366 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13367 reloc->addend = fixp->fx_addnumber;
252b5132
RH
13368 else
13369 {
13370 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13371 /* A gruesome hack which is a result of the gruesome gas reloc
13372 handling. */
13373 reloc->addend = reloc->address;
13374 else
13375 reloc->addend = -reloc->address;
13376 }
13377
13378 /* If this is a variant frag, we may need to adjust the existing
13379 reloc and generate a new one. */
13380 if (fixp->fx_frag->fr_opcode != NULL
ed6fb7bd
SC
13381 && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13382 && ! HAVE_NEWABI)
252b5132
RH
13383 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13384 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13385 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13386 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13387 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
6478892d 13388 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
ed6fb7bd 13389 )
252b5132
RH
13390 {
13391 arelent *reloc2;
13392
13393 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13394
13395 /* If this is not the last reloc in this frag, then we have two
13396 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13397 CALL_HI16/CALL_LO16, both of which are being replaced. Let
13398 the second one handle all of them. */
13399 if (fixp->fx_next != NULL
13400 && fixp->fx_frag == fixp->fx_next->fx_frag)
13401 {
cdf6fd85
TS
13402 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13403 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
252b5132
RH
13404 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13405 && (fixp->fx_next->fx_r_type
13406 == BFD_RELOC_MIPS_GOT_LO16))
13407 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13408 && (fixp->fx_next->fx_r_type
13409 == BFD_RELOC_MIPS_CALL_LO16)));
13410 retval[0] = NULL;
13411 return retval;
13412 }
13413
13414 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13415 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13416 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13417 retval[2] = NULL;
49309057
ILT
13418 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13419 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13420 reloc2->address = (reloc->address
13421 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13422 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13423 reloc2->addend = fixp->fx_addnumber;
13424 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13425 assert (reloc2->howto != NULL);
13426
13427 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13428 {
13429 arelent *reloc3;
13430
13431 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13432 retval[3] = NULL;
13433 *reloc3 = *reloc2;
13434 reloc3->address += 4;
13435 }
13436
13437 if (mips_pic == NO_PIC)
13438 {
cdf6fd85 13439 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
252b5132
RH
13440 fixp->fx_r_type = BFD_RELOC_HI16_S;
13441 }
13442 else if (mips_pic == SVR4_PIC)
13443 {
13444 switch (fixp->fx_r_type)
13445 {
13446 default:
13447 abort ();
13448 case BFD_RELOC_MIPS_GOT16:
13449 break;
252b5132
RH
13450 case BFD_RELOC_MIPS_GOT_LO16:
13451 case BFD_RELOC_MIPS_CALL_LO16:
13452 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13453 break;
ed6fb7bd
SC
13454 case BFD_RELOC_MIPS_CALL16:
13455 if (HAVE_NEWABI)
13456 {
13457 /* BFD_RELOC_MIPS_GOT16;*/
13458 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13459 reloc2->howto = bfd_reloc_type_lookup
13460 (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13461 }
13462 else
13463 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13464 break;
252b5132
RH
13465 }
13466 }
13467 else
13468 abort ();
ed6fb7bd
SC
13469
13470 /* newabi uses R_MIPS_GOT_DISP for local symbols */
6b70243f 13471 if (HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16)
ed6fb7bd
SC
13472 {
13473 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13474 retval[1] = NULL;
13475 }
252b5132
RH
13476 }
13477
438c16b8
TS
13478 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13479 entry to be used in the relocation's section offset. */
13480 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
13481 {
13482 reloc->address = reloc->addend;
13483 reloc->addend = 0;
13484 }
13485
13486 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13487 fixup_segment converted a non-PC relative reloc into a PC
13488 relative reloc. In such a case, we need to convert the reloc
13489 code. */
13490 code = fixp->fx_r_type;
13491 if (fixp->fx_pcrel)
13492 {
13493 switch (code)
13494 {
13495 case BFD_RELOC_8:
13496 code = BFD_RELOC_8_PCREL;
13497 break;
13498 case BFD_RELOC_16:
13499 code = BFD_RELOC_16_PCREL;
13500 break;
13501 case BFD_RELOC_32:
13502 code = BFD_RELOC_32_PCREL;
13503 break;
13504 case BFD_RELOC_64:
13505 code = BFD_RELOC_64_PCREL;
13506 break;
13507 case BFD_RELOC_8_PCREL:
13508 case BFD_RELOC_16_PCREL:
13509 case BFD_RELOC_32_PCREL:
13510 case BFD_RELOC_64_PCREL:
13511 case BFD_RELOC_16_PCREL_S2:
13512 case BFD_RELOC_PCREL_HI16_S:
13513 case BFD_RELOC_PCREL_LO16:
13514 break;
13515 default:
13516 as_bad_where (fixp->fx_file, fixp->fx_line,
13517 _("Cannot make %s relocation PC relative"),
13518 bfd_get_reloc_code_name (code));
13519 }
13520 }
13521
add55e1f
RS
13522#ifdef OBJ_ELF
13523 /* md_apply_fix3 has a double-subtraction hack to get
13524 bfd_install_relocation to behave nicely. GPREL relocations are
13525 handled correctly without this hack, so undo it here. We can't
13526 stop md_apply_fix3 from subtracting twice in the first place since
13527 the fake addend is required for variant frags above. */
13528 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
98605598 13529 && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
add55e1f
RS
13530 && reloc->addend != 0
13531 && mips_need_elf_addend_fixup (fixp))
13532 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13533#endif
13534
252b5132
RH
13535 /* To support a PC relative reloc when generating embedded PIC code
13536 for ECOFF, we use a Cygnus extension. We check for that here to
13537 make sure that we don't let such a reloc escape normally. */
bb2d6cd7
GK
13538 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13539 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132
RH
13540 && code == BFD_RELOC_16_PCREL_S2
13541 && mips_pic != EMBEDDED_PIC)
13542 reloc->howto = NULL;
13543 else
13544 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13545
13546 if (reloc->howto == NULL)
13547 {
13548 as_bad_where (fixp->fx_file, fixp->fx_line,
13549 _("Can not represent %s relocation in this object file format"),
13550 bfd_get_reloc_code_name (code));
13551 retval[0] = NULL;
13552 }
13553
13554 return retval;
13555}
13556
13557/* Relax a machine dependent frag. This returns the amount by which
13558 the current size of the frag should change. */
13559
13560int
4a6a3df4
AO
13561mips_relax_frag (sec, fragp, stretch)
13562 asection *sec;
252b5132
RH
13563 fragS *fragp;
13564 long stretch;
13565{
4a6a3df4
AO
13566 if (RELAX_BRANCH_P (fragp->fr_subtype))
13567 {
13568 offsetT old_var = fragp->fr_var;
b34976b6
AM
13569
13570 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
13571
13572 return fragp->fr_var - old_var;
13573 }
13574
252b5132
RH
13575 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13576 return 0;
13577
c4e7957c 13578 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
13579 {
13580 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13581 return 0;
13582 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13583 return 2;
13584 }
13585 else
13586 {
13587 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13588 return 0;
13589 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13590 return -2;
13591 }
13592
13593 return 0;
13594}
13595
13596/* Convert a machine dependent frag. */
13597
13598void
13599md_convert_frag (abfd, asec, fragp)
43841e91 13600 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
13601 segT asec;
13602 fragS *fragp;
13603{
13604 int old, new;
13605 char *fixptr;
13606
4a6a3df4
AO
13607 if (RELAX_BRANCH_P (fragp->fr_subtype))
13608 {
13609 bfd_byte *buf;
13610 unsigned long insn;
13611 expressionS exp;
13612 fixS *fixp;
b34976b6 13613
4a6a3df4
AO
13614 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13615
13616 if (target_big_endian)
13617 insn = bfd_getb32 (buf);
13618 else
13619 insn = bfd_getl32 (buf);
b34976b6 13620
4a6a3df4
AO
13621 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13622 {
13623 /* We generate a fixup instead of applying it right now
13624 because, if there are linker relaxations, we're going to
13625 need the relocations. */
13626 exp.X_op = O_symbol;
13627 exp.X_add_symbol = fragp->fr_symbol;
13628 exp.X_add_number = fragp->fr_offset;
13629
13630 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13631 4, &exp, 1,
af6ae2ad 13632 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
13633 fixp->fx_file = fragp->fr_file;
13634 fixp->fx_line = fragp->fr_line;
b34976b6 13635
4a6a3df4
AO
13636 md_number_to_chars ((char *)buf, insn, 4);
13637 buf += 4;
13638 }
13639 else
13640 {
13641 int i;
13642
13643 as_warn_where (fragp->fr_file, fragp->fr_line,
13644 _("relaxed out-of-range branch into a jump"));
13645
13646 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13647 goto uncond;
13648
13649 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13650 {
13651 /* Reverse the branch. */
13652 switch ((insn >> 28) & 0xf)
13653 {
13654 case 4:
13655 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13656 have the condition reversed by tweaking a single
13657 bit, and their opcodes all have 0x4???????. */
13658 assert ((insn & 0xf1000000) == 0x41000000);
13659 insn ^= 0x00010000;
13660 break;
13661
13662 case 0:
13663 /* bltz 0x04000000 bgez 0x04010000
13664 bltzal 0x04100000 bgezal 0x04110000 */
13665 assert ((insn & 0xfc0e0000) == 0x04000000);
13666 insn ^= 0x00010000;
13667 break;
b34976b6 13668
4a6a3df4
AO
13669 case 1:
13670 /* beq 0x10000000 bne 0x14000000
13671 blez 0x18000000 bgtz 0x1c000000 */
13672 insn ^= 0x04000000;
13673 break;
13674
13675 default:
13676 abort ();
13677 }
13678 }
13679
13680 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13681 {
13682 /* Clear the and-link bit. */
13683 assert ((insn & 0xfc1c0000) == 0x04100000);
13684
13685 /* bltzal 0x04100000 bgezal 0x04110000
13686 bltzall 0x04120000 bgezall 0x04130000 */
13687 insn &= ~0x00100000;
13688 }
13689
13690 /* Branch over the branch (if the branch was likely) or the
13691 full jump (not likely case). Compute the offset from the
13692 current instruction to branch to. */
13693 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13694 i = 16;
13695 else
13696 {
13697 /* How many bytes in instructions we've already emitted? */
13698 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13699 /* How many bytes in instructions from here to the end? */
13700 i = fragp->fr_var - i;
13701 }
13702 /* Convert to instruction count. */
13703 i >>= 2;
13704 /* Branch counts from the next instruction. */
b34976b6 13705 i--;
4a6a3df4
AO
13706 insn |= i;
13707 /* Branch over the jump. */
13708 md_number_to_chars ((char *)buf, insn, 4);
13709 buf += 4;
13710
13711 /* Nop */
13712 md_number_to_chars ((char*)buf, 0, 4);
13713 buf += 4;
13714
13715 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13716 {
13717 /* beql $0, $0, 2f */
13718 insn = 0x50000000;
13719 /* Compute the PC offset from the current instruction to
13720 the end of the variable frag. */
13721 /* How many bytes in instructions we've already emitted? */
13722 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13723 /* How many bytes in instructions from here to the end? */
13724 i = fragp->fr_var - i;
13725 /* Convert to instruction count. */
13726 i >>= 2;
13727 /* Don't decrement i, because we want to branch over the
13728 delay slot. */
13729
13730 insn |= i;
13731 md_number_to_chars ((char *)buf, insn, 4);
13732 buf += 4;
13733
13734 md_number_to_chars ((char *)buf, 0, 4);
13735 buf += 4;
13736 }
13737
13738 uncond:
13739 if (mips_pic == NO_PIC)
13740 {
13741 /* j or jal. */
13742 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13743 ? 0x0c000000 : 0x08000000);
13744 exp.X_op = O_symbol;
13745 exp.X_add_symbol = fragp->fr_symbol;
13746 exp.X_add_number = fragp->fr_offset;
13747
13748 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13749 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13750 fixp->fx_file = fragp->fr_file;
13751 fixp->fx_line = fragp->fr_line;
13752
13753 md_number_to_chars ((char*)buf, insn, 4);
13754 buf += 4;
13755 }
13756 else
13757 {
13758 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13759 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13760 exp.X_op = O_symbol;
13761 exp.X_add_symbol = fragp->fr_symbol;
13762 exp.X_add_number = fragp->fr_offset;
13763
13764 if (fragp->fr_offset)
13765 {
13766 exp.X_add_symbol = make_expr_symbol (&exp);
13767 exp.X_add_number = 0;
13768 }
13769
13770 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13771 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13772 fixp->fx_file = fragp->fr_file;
13773 fixp->fx_line = fragp->fr_line;
13774
13775 md_number_to_chars ((char*)buf, insn, 4);
13776 buf += 4;
b34976b6 13777
4a6a3df4
AO
13778 if (mips_opts.isa == ISA_MIPS1)
13779 {
13780 /* nop */
13781 md_number_to_chars ((char*)buf, 0, 4);
13782 buf += 4;
13783 }
13784
13785 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13786 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13787
13788 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13789 4, &exp, 0, BFD_RELOC_LO16);
13790 fixp->fx_file = fragp->fr_file;
13791 fixp->fx_line = fragp->fr_line;
b34976b6 13792
4a6a3df4
AO
13793 md_number_to_chars ((char*)buf, insn, 4);
13794 buf += 4;
13795
13796 /* j(al)r $at. */
13797 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13798 insn = 0x0020f809;
13799 else
13800 insn = 0x00200008;
13801
13802 md_number_to_chars ((char*)buf, insn, 4);
13803 buf += 4;
13804 }
13805 }
13806
13807 assert (buf == (bfd_byte *)fragp->fr_literal
13808 + fragp->fr_fix + fragp->fr_var);
13809
13810 fragp->fr_fix += fragp->fr_var;
13811
13812 return;
13813 }
13814
252b5132
RH
13815 if (RELAX_MIPS16_P (fragp->fr_subtype))
13816 {
13817 int type;
13818 register const struct mips16_immed_operand *op;
b34976b6 13819 bfd_boolean small, ext;
252b5132
RH
13820 offsetT val;
13821 bfd_byte *buf;
13822 unsigned long insn;
b34976b6 13823 bfd_boolean use_extend;
252b5132
RH
13824 unsigned short extend;
13825
13826 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13827 op = mips16_immed_operands;
13828 while (op->type != type)
13829 ++op;
13830
13831 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13832 {
b34976b6
AM
13833 small = FALSE;
13834 ext = TRUE;
252b5132
RH
13835 }
13836 else
13837 {
b34976b6
AM
13838 small = TRUE;
13839 ext = FALSE;
252b5132
RH
13840 }
13841
6386f3a7 13842 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
13843 val = S_GET_VALUE (fragp->fr_symbol);
13844 if (op->pcrel)
13845 {
13846 addressT addr;
13847
13848 addr = fragp->fr_address + fragp->fr_fix;
13849
13850 /* The rules for the base address of a PC relative reloc are
13851 complicated; see mips16_extended_frag. */
13852 if (type == 'p' || type == 'q')
13853 {
13854 addr += 2;
13855 if (ext)
13856 addr += 2;
13857 /* Ignore the low bit in the target, since it will be
13858 set for a text label. */
13859 if ((val & 1) != 0)
13860 --val;
13861 }
13862 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13863 addr -= 4;
13864 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13865 addr -= 2;
13866
13867 addr &= ~ (addressT) ((1 << op->shift) - 1);
13868 val -= addr;
13869
13870 /* Make sure the section winds up with the alignment we have
13871 assumed. */
13872 if (op->shift > 0)
13873 record_alignment (asec, op->shift);
13874 }
13875
13876 if (ext
13877 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13878 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13879 as_warn_where (fragp->fr_file, fragp->fr_line,
13880 _("extended instruction in delay slot"));
13881
13882 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13883
13884 if (target_big_endian)
13885 insn = bfd_getb16 (buf);
13886 else
13887 insn = bfd_getl16 (buf);
13888
13889 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13890 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13891 small, ext, &insn, &use_extend, &extend);
13892
13893 if (use_extend)
13894 {
874e8986 13895 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
13896 fragp->fr_fix += 2;
13897 buf += 2;
13898 }
13899
874e8986 13900 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
13901 fragp->fr_fix += 2;
13902 buf += 2;
13903 }
13904 else
13905 {
13906 if (fragp->fr_opcode == NULL)
13907 return;
13908
13909 old = RELAX_OLD (fragp->fr_subtype);
13910 new = RELAX_NEW (fragp->fr_subtype);
13911 fixptr = fragp->fr_literal + fragp->fr_fix;
13912
13913 if (new > 0)
13914 memcpy (fixptr - old, fixptr, new);
13915
13916 fragp->fr_fix += new - old;
13917 }
13918}
13919
13920#ifdef OBJ_ELF
13921
13922/* This function is called after the relocs have been generated.
13923 We've been storing mips16 text labels as odd. Here we convert them
13924 back to even for the convenience of the debugger. */
13925
13926void
13927mips_frob_file_after_relocs ()
13928{
13929 asymbol **syms;
13930 unsigned int count, i;
13931
13932 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13933 return;
13934
13935 syms = bfd_get_outsymbols (stdoutput);
13936 count = bfd_get_symcount (stdoutput);
13937 for (i = 0; i < count; i++, syms++)
13938 {
13939 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13940 && ((*syms)->value & 1) != 0)
13941 {
13942 (*syms)->value &= ~1;
13943 /* If the symbol has an odd size, it was probably computed
13944 incorrectly, so adjust that as well. */
13945 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13946 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13947 }
13948 }
13949}
13950
13951#endif
13952
13953/* This function is called whenever a label is defined. It is used
13954 when handling branch delays; if a branch has a label, we assume we
13955 can not move it. */
13956
13957void
13958mips_define_label (sym)
13959 symbolS *sym;
13960{
13961 struct insn_label_list *l;
13962
13963 if (free_insn_labels == NULL)
13964 l = (struct insn_label_list *) xmalloc (sizeof *l);
13965 else
13966 {
13967 l = free_insn_labels;
13968 free_insn_labels = l->next;
13969 }
13970
13971 l->label = sym;
13972 l->next = insn_labels;
13973 insn_labels = l;
13974}
13975\f
13976#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13977
13978/* Some special processing for a MIPS ELF file. */
13979
13980void
13981mips_elf_final_processing ()
13982{
13983 /* Write out the register information. */
316f5878 13984 if (mips_abi != N64_ABI)
252b5132
RH
13985 {
13986 Elf32_RegInfo s;
13987
13988 s.ri_gprmask = mips_gprmask;
13989 s.ri_cprmask[0] = mips_cprmask[0];
13990 s.ri_cprmask[1] = mips_cprmask[1];
13991 s.ri_cprmask[2] = mips_cprmask[2];
13992 s.ri_cprmask[3] = mips_cprmask[3];
13993 /* The gp_value field is set by the MIPS ELF backend. */
13994
13995 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13996 ((Elf32_External_RegInfo *)
13997 mips_regmask_frag));
13998 }
13999 else
14000 {
14001 Elf64_Internal_RegInfo s;
14002
14003 s.ri_gprmask = mips_gprmask;
14004 s.ri_pad = 0;
14005 s.ri_cprmask[0] = mips_cprmask[0];
14006 s.ri_cprmask[1] = mips_cprmask[1];
14007 s.ri_cprmask[2] = mips_cprmask[2];
14008 s.ri_cprmask[3] = mips_cprmask[3];
14009 /* The gp_value field is set by the MIPS ELF backend. */
14010
14011 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14012 ((Elf64_External_RegInfo *)
14013 mips_regmask_frag));
14014 }
14015
14016 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
14017 sort of BFD interface for this. */
14018 if (mips_any_noreorder)
14019 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14020 if (mips_pic != NO_PIC)
14021 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
14022
98d3f06f 14023 /* Set MIPS ELF flags for ASEs. */
a4672219
TS
14024 if (file_ase_mips16)
14025 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
14026#if 0 /* XXX FIXME */
14027 if (file_ase_mips3d)
14028 elf_elfheader (stdoutput)->e_flags |= ???;
14029#endif
deec1734
CD
14030 if (file_ase_mdmx)
14031 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 14032
bdaaa2e1 14033 /* Set the MIPS ELF ABI flags. */
316f5878 14034 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 14035 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 14036 else if (mips_abi == O64_ABI)
252b5132 14037 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 14038 else if (mips_abi == EABI_ABI)
252b5132 14039 {
316f5878 14040 if (!file_mips_gp32)
252b5132
RH
14041 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14042 else
14043 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14044 }
316f5878 14045 else if (mips_abi == N32_ABI)
be00bddd
TS
14046 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14047
c9914766 14048 /* Nothing to do for N64_ABI. */
252b5132
RH
14049
14050 if (mips_32bitmode)
14051 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14052}
14053
14054#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14055\f
beae10d5
KH
14056typedef struct proc {
14057 symbolS *isym;
14058 unsigned long reg_mask;
14059 unsigned long reg_offset;
14060 unsigned long fpreg_mask;
14061 unsigned long fpreg_offset;
14062 unsigned long frame_offset;
14063 unsigned long frame_reg;
14064 unsigned long pc_reg;
14065} procS;
252b5132
RH
14066
14067static procS cur_proc;
14068static procS *cur_proc_ptr;
14069static int numprocs;
14070
0a9ef439 14071/* Fill in an rs_align_code fragment. */
a19d8eb0 14072
0a9ef439
RH
14073void
14074mips_handle_align (fragp)
14075 fragS *fragp;
a19d8eb0 14076{
0a9ef439
RH
14077 if (fragp->fr_type != rs_align_code)
14078 return;
14079
14080 if (mips_opts.mips16)
a19d8eb0
CP
14081 {
14082 static const unsigned char be_nop[] = { 0x65, 0x00 };
14083 static const unsigned char le_nop[] = { 0x00, 0x65 };
14084
0a9ef439
RH
14085 int bytes;
14086 char *p;
a19d8eb0 14087
0a9ef439
RH
14088 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14089 p = fragp->fr_literal + fragp->fr_fix;
14090
14091 if (bytes & 1)
14092 {
14093 *p++ = 0;
f9419b05 14094 fragp->fr_fix++;
0a9ef439
RH
14095 }
14096
14097 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14098 fragp->fr_var = 2;
a19d8eb0
CP
14099 }
14100
0a9ef439 14101 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
14102}
14103
252b5132
RH
14104static void
14105md_obj_begin ()
14106{
14107}
14108
14109static void
14110md_obj_end ()
14111{
14112 /* check for premature end, nesting errors, etc */
14113 if (cur_proc_ptr)
9a41af64 14114 as_warn (_("missing .end at end of assembly"));
252b5132
RH
14115}
14116
14117static long
14118get_number ()
14119{
14120 int negative = 0;
14121 long val = 0;
14122
14123 if (*input_line_pointer == '-')
14124 {
14125 ++input_line_pointer;
14126 negative = 1;
14127 }
3882b010 14128 if (!ISDIGIT (*input_line_pointer))
956cd1d6 14129 as_bad (_("expected simple number"));
252b5132
RH
14130 if (input_line_pointer[0] == '0')
14131 {
14132 if (input_line_pointer[1] == 'x')
14133 {
14134 input_line_pointer += 2;
3882b010 14135 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
14136 {
14137 val <<= 4;
14138 val |= hex_value (*input_line_pointer++);
14139 }
14140 return negative ? -val : val;
14141 }
14142 else
14143 {
14144 ++input_line_pointer;
3882b010 14145 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14146 {
14147 val <<= 3;
14148 val |= *input_line_pointer++ - '0';
14149 }
14150 return negative ? -val : val;
14151 }
14152 }
3882b010 14153 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
14154 {
14155 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14156 *input_line_pointer, *input_line_pointer);
956cd1d6 14157 as_warn (_("invalid number"));
252b5132
RH
14158 return -1;
14159 }
3882b010 14160 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14161 {
14162 val *= 10;
14163 val += *input_line_pointer++ - '0';
14164 }
14165 return negative ? -val : val;
14166}
14167
14168/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
14169 is an initial number which is the ECOFF file index. In the non-ECOFF
14170 case .file implies DWARF-2. */
14171
14172static void
14173s_mips_file (x)
14174 int x ATTRIBUTE_UNUSED;
14175{
ecb4347a
DJ
14176 static int first_file_directive = 0;
14177
c5dd6aab
DJ
14178 if (ECOFF_DEBUGGING)
14179 {
14180 get_number ();
14181 s_app_file (0);
14182 }
14183 else
ecb4347a
DJ
14184 {
14185 char *filename;
14186
14187 filename = dwarf2_directive_file (0);
14188
14189 /* Versions of GCC up to 3.1 start files with a ".file"
14190 directive even for stabs output. Make sure that this
14191 ".file" is handled. Note that you need a version of GCC
14192 after 3.1 in order to support DWARF-2 on MIPS. */
14193 if (filename != NULL && ! first_file_directive)
14194 {
14195 (void) new_logical_line (filename, -1);
14196 s_app_file_string (filename);
14197 }
14198 first_file_directive = 1;
14199 }
c5dd6aab
DJ
14200}
14201
14202/* The .loc directive, implying DWARF-2. */
252b5132
RH
14203
14204static void
c5dd6aab 14205s_mips_loc (x)
43841e91 14206 int x ATTRIBUTE_UNUSED;
252b5132 14207{
c5dd6aab
DJ
14208 if (!ECOFF_DEBUGGING)
14209 dwarf2_directive_loc (0);
252b5132
RH
14210}
14211
252b5132
RH
14212/* The .end directive. */
14213
14214static void
14215s_mips_end (x)
43841e91 14216 int x ATTRIBUTE_UNUSED;
252b5132
RH
14217{
14218 symbolS *p;
14219 int maybe_text;
14220
7a621144
DJ
14221 /* Following functions need their own .frame and .cprestore directives. */
14222 mips_frame_reg_valid = 0;
14223 mips_cprestore_valid = 0;
14224
252b5132
RH
14225 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14226 {
14227 p = get_symbol ();
14228 demand_empty_rest_of_line ();
14229 }
14230 else
14231 p = NULL;
14232
14233#ifdef BFD_ASSEMBLER
14234 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14235 maybe_text = 1;
14236 else
14237 maybe_text = 0;
14238#else
14239 if (now_seg != data_section && now_seg != bss_section)
14240 maybe_text = 1;
14241 else
14242 maybe_text = 0;
14243#endif
14244
14245 if (!maybe_text)
14246 as_warn (_(".end not in text section"));
14247
14248 if (!cur_proc_ptr)
14249 {
14250 as_warn (_(".end directive without a preceding .ent directive."));
14251 demand_empty_rest_of_line ();
14252 return;
14253 }
14254
14255 if (p != NULL)
14256 {
14257 assert (S_GET_NAME (p));
14258 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14259 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
14260
14261 if (debug_type == DEBUG_STABS)
14262 stabs_generate_asm_endfunc (S_GET_NAME (p),
14263 S_GET_NAME (p));
252b5132
RH
14264 }
14265 else
14266 as_warn (_(".end directive missing or unknown symbol"));
14267
ecb4347a
DJ
14268#ifdef OBJ_ELF
14269 /* Generate a .pdr section. */
14270 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14271 {
14272 segT saved_seg = now_seg;
14273 subsegT saved_subseg = now_subseg;
14274 valueT dot;
14275 expressionS exp;
14276 char *fragp;
252b5132 14277
ecb4347a 14278 dot = frag_now_fix ();
252b5132
RH
14279
14280#ifdef md_flush_pending_output
ecb4347a 14281 md_flush_pending_output ();
252b5132
RH
14282#endif
14283
ecb4347a
DJ
14284 assert (pdr_seg);
14285 subseg_set (pdr_seg, 0);
252b5132 14286
ecb4347a
DJ
14287 /* Write the symbol. */
14288 exp.X_op = O_symbol;
14289 exp.X_add_symbol = p;
14290 exp.X_add_number = 0;
14291 emit_expr (&exp, 4);
252b5132 14292
ecb4347a 14293 fragp = frag_more (7 * 4);
252b5132 14294
ecb4347a
DJ
14295 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
14296 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
14297 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14298 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14299 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14300 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14301 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
252b5132 14302
ecb4347a
DJ
14303 subseg_set (saved_seg, saved_subseg);
14304 }
14305#endif /* OBJ_ELF */
252b5132
RH
14306
14307 cur_proc_ptr = NULL;
14308}
14309
14310/* The .aent and .ent directives. */
14311
14312static void
14313s_mips_ent (aent)
14314 int aent;
14315{
252b5132
RH
14316 symbolS *symbolP;
14317 int maybe_text;
14318
14319 symbolP = get_symbol ();
14320 if (*input_line_pointer == ',')
f9419b05 14321 ++input_line_pointer;
252b5132 14322 SKIP_WHITESPACE ();
3882b010 14323 if (ISDIGIT (*input_line_pointer)
d9a62219 14324 || *input_line_pointer == '-')
874e8986 14325 get_number ();
252b5132
RH
14326
14327#ifdef BFD_ASSEMBLER
14328 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14329 maybe_text = 1;
14330 else
14331 maybe_text = 0;
14332#else
14333 if (now_seg != data_section && now_seg != bss_section)
14334 maybe_text = 1;
14335 else
14336 maybe_text = 0;
14337#endif
14338
14339 if (!maybe_text)
14340 as_warn (_(".ent or .aent not in text section."));
14341
14342 if (!aent && cur_proc_ptr)
9a41af64 14343 as_warn (_("missing .end"));
252b5132
RH
14344
14345 if (!aent)
14346 {
7a621144
DJ
14347 /* This function needs its own .frame and .cprestore directives. */
14348 mips_frame_reg_valid = 0;
14349 mips_cprestore_valid = 0;
14350
252b5132
RH
14351 cur_proc_ptr = &cur_proc;
14352 memset (cur_proc_ptr, '\0', sizeof (procS));
14353
14354 cur_proc_ptr->isym = symbolP;
14355
49309057 14356 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 14357
f9419b05 14358 ++numprocs;
ecb4347a
DJ
14359
14360 if (debug_type == DEBUG_STABS)
14361 stabs_generate_asm_func (S_GET_NAME (symbolP),
14362 S_GET_NAME (symbolP));
252b5132
RH
14363 }
14364
14365 demand_empty_rest_of_line ();
14366}
14367
14368/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 14369 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 14370 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 14371 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
14372 symbol table (in the mdebug section). */
14373
14374static void
14375s_mips_frame (ignore)
2b3c5a5d 14376 int ignore ATTRIBUTE_UNUSED;
252b5132 14377{
ecb4347a
DJ
14378#ifdef OBJ_ELF
14379 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14380 {
14381 long val;
252b5132 14382
ecb4347a
DJ
14383 if (cur_proc_ptr == (procS *) NULL)
14384 {
14385 as_warn (_(".frame outside of .ent"));
14386 demand_empty_rest_of_line ();
14387 return;
14388 }
252b5132 14389
ecb4347a
DJ
14390 cur_proc_ptr->frame_reg = tc_get_register (1);
14391
14392 SKIP_WHITESPACE ();
14393 if (*input_line_pointer++ != ','
14394 || get_absolute_expression_and_terminator (&val) != ',')
14395 {
14396 as_warn (_("Bad .frame directive"));
14397 --input_line_pointer;
14398 demand_empty_rest_of_line ();
14399 return;
14400 }
252b5132 14401
ecb4347a
DJ
14402 cur_proc_ptr->frame_offset = val;
14403 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 14404
252b5132 14405 demand_empty_rest_of_line ();
252b5132 14406 }
ecb4347a
DJ
14407 else
14408#endif /* OBJ_ELF */
14409 s_ignore (ignore);
252b5132
RH
14410}
14411
bdaaa2e1
KH
14412/* The .fmask and .mask directives. If the mdebug section is present
14413 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 14414 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 14415 information correctly. We can't use the ecoff routines because they
252b5132
RH
14416 make reference to the ecoff symbol table (in the mdebug section). */
14417
14418static void
14419s_mips_mask (reg_type)
14420 char reg_type;
14421{
ecb4347a
DJ
14422#ifdef OBJ_ELF
14423 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 14424 {
ecb4347a 14425 long mask, off;
252b5132 14426
ecb4347a
DJ
14427 if (cur_proc_ptr == (procS *) NULL)
14428 {
14429 as_warn (_(".mask/.fmask outside of .ent"));
14430 demand_empty_rest_of_line ();
14431 return;
14432 }
252b5132 14433
ecb4347a
DJ
14434 if (get_absolute_expression_and_terminator (&mask) != ',')
14435 {
14436 as_warn (_("Bad .mask/.fmask directive"));
14437 --input_line_pointer;
14438 demand_empty_rest_of_line ();
14439 return;
14440 }
252b5132 14441
ecb4347a
DJ
14442 off = get_absolute_expression ();
14443
14444 if (reg_type == 'F')
14445 {
14446 cur_proc_ptr->fpreg_mask = mask;
14447 cur_proc_ptr->fpreg_offset = off;
14448 }
14449 else
14450 {
14451 cur_proc_ptr->reg_mask = mask;
14452 cur_proc_ptr->reg_offset = off;
14453 }
14454
14455 demand_empty_rest_of_line ();
252b5132
RH
14456 }
14457 else
ecb4347a
DJ
14458#endif /* OBJ_ELF */
14459 s_ignore (reg_type);
252b5132
RH
14460}
14461
14462/* The .loc directive. */
14463
14464#if 0
14465static void
14466s_loc (x)
14467 int x;
14468{
14469 symbolS *symbolP;
14470 int lineno;
14471 int addroff;
14472
14473 assert (now_seg == text_section);
14474
14475 lineno = get_number ();
14476 addroff = frag_now_fix ();
14477
14478 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14479 S_SET_TYPE (symbolP, N_SLINE);
14480 S_SET_OTHER (symbolP, 0);
14481 S_SET_DESC (symbolP, lineno);
14482 symbolP->sy_segment = now_seg;
14483}
14484#endif
e7af610e 14485
316f5878
RS
14486/* A table describing all the processors gas knows about. Names are
14487 matched in the order listed.
e7af610e 14488
316f5878
RS
14489 To ease comparison, please keep this table in the same order as
14490 gcc's mips_cpu_info_table[]. */
e972090a
NC
14491static const struct mips_cpu_info mips_cpu_info_table[] =
14492{
316f5878
RS
14493 /* Entries for generic ISAs */
14494 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14495 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14496 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14497 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14498 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14499 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
af7ee8bf 14500 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
316f5878
RS
14501 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
14502
14503 /* MIPS I */
14504 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14505 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14506 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14507
14508 /* MIPS II */
14509 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14510
14511 /* MIPS III */
14512 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14513 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14514 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14515 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
60b63b72
RS
14516 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14517 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14518 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
316f5878
RS
14519 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14520 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14521 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14522 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14523 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14524
14525 /* MIPS IV */
14526 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14527 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14528 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14529 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
60b63b72
RS
14530 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14531 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
316f5878
RS
14532 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14533 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14534 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14535 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14536 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14537 { "r7000", 0, ISA_MIPS4, CPU_R5000 },
14538
14539 /* MIPS 32 */
14540 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
14541 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14542 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 14543
316f5878
RS
14544 /* MIPS 64 */
14545 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14546 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 14547
c7a23324 14548 /* Broadcom SB-1 CPU core */
316f5878 14549 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 14550
316f5878
RS
14551 /* End marker */
14552 { NULL, 0, 0, 0 }
14553};
e7af610e 14554
84ea6cf2 14555
316f5878
RS
14556/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14557 with a final "000" replaced by "k". Ignore case.
e7af610e 14558
316f5878 14559 Note: this function is shared between GCC and GAS. */
c6c98b38 14560
b34976b6 14561static bfd_boolean
316f5878
RS
14562mips_strict_matching_cpu_name_p (canonical, given)
14563 const char *canonical, *given;
14564{
14565 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14566 given++, canonical++;
14567
14568 return ((*given == 0 && *canonical == 0)
14569 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14570}
14571
14572
14573/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14574 CPU name. We've traditionally allowed a lot of variation here.
14575
14576 Note: this function is shared between GCC and GAS. */
14577
b34976b6 14578static bfd_boolean
316f5878
RS
14579mips_matching_cpu_name_p (canonical, given)
14580 const char *canonical, *given;
14581{
14582 /* First see if the name matches exactly, or with a final "000"
14583 turned into "k". */
14584 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 14585 return TRUE;
316f5878
RS
14586
14587 /* If not, try comparing based on numerical designation alone.
14588 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14589 if (TOLOWER (*given) == 'r')
14590 given++;
14591 if (!ISDIGIT (*given))
b34976b6 14592 return FALSE;
316f5878
RS
14593
14594 /* Skip over some well-known prefixes in the canonical name,
14595 hoping to find a number there too. */
14596 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14597 canonical += 2;
14598 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14599 canonical += 2;
14600 else if (TOLOWER (canonical[0]) == 'r')
14601 canonical += 1;
14602
14603 return mips_strict_matching_cpu_name_p (canonical, given);
14604}
14605
14606
14607/* Parse an option that takes the name of a processor as its argument.
14608 OPTION is the name of the option and CPU_STRING is the argument.
14609 Return the corresponding processor enumeration if the CPU_STRING is
14610 recognized, otherwise report an error and return null.
14611
14612 A similar function exists in GCC. */
e7af610e
NC
14613
14614static const struct mips_cpu_info *
316f5878
RS
14615mips_parse_cpu (option, cpu_string)
14616 const char *option, *cpu_string;
e7af610e 14617{
316f5878 14618 const struct mips_cpu_info *p;
e7af610e 14619
316f5878
RS
14620 /* 'from-abi' selects the most compatible architecture for the given
14621 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14622 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14623 version. Look first at the -mgp options, if given, otherwise base
14624 the choice on MIPS_DEFAULT_64BIT.
e7af610e 14625
316f5878
RS
14626 Treat NO_ABI like the EABIs. One reason to do this is that the
14627 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14628 architecture. This code picks MIPS I for 'mips' and MIPS III for
14629 'mips64', just as we did in the days before 'from-abi'. */
14630 if (strcasecmp (cpu_string, "from-abi") == 0)
14631 {
14632 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14633 return mips_cpu_info_from_isa (ISA_MIPS1);
14634
14635 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14636 return mips_cpu_info_from_isa (ISA_MIPS3);
14637
14638 if (file_mips_gp32 >= 0)
14639 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14640
14641 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14642 ? ISA_MIPS3
14643 : ISA_MIPS1);
14644 }
14645
14646 /* 'default' has traditionally been a no-op. Probably not very useful. */
14647 if (strcasecmp (cpu_string, "default") == 0)
14648 return 0;
14649
14650 for (p = mips_cpu_info_table; p->name != 0; p++)
14651 if (mips_matching_cpu_name_p (p->name, cpu_string))
14652 return p;
14653
14654 as_bad ("Bad value (%s) for %s", cpu_string, option);
14655 return 0;
e7af610e
NC
14656}
14657
316f5878
RS
14658/* Return the canonical processor information for ISA (a member of the
14659 ISA_MIPS* enumeration). */
14660
e7af610e
NC
14661static const struct mips_cpu_info *
14662mips_cpu_info_from_isa (isa)
14663 int isa;
14664{
14665 int i;
14666
14667 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14668 if (mips_cpu_info_table[i].is_isa
316f5878 14669 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
14670 return (&mips_cpu_info_table[i]);
14671
e972090a 14672 return NULL;
e7af610e 14673}
316f5878
RS
14674\f
14675static void
14676show (stream, string, col_p, first_p)
14677 FILE *stream;
14678 const char *string;
14679 int *col_p;
14680 int *first_p;
14681{
14682 if (*first_p)
14683 {
14684 fprintf (stream, "%24s", "");
14685 *col_p = 24;
14686 }
14687 else
14688 {
14689 fprintf (stream, ", ");
14690 *col_p += 2;
14691 }
e7af610e 14692
316f5878
RS
14693 if (*col_p + strlen (string) > 72)
14694 {
14695 fprintf (stream, "\n%24s", "");
14696 *col_p = 24;
14697 }
14698
14699 fprintf (stream, "%s", string);
14700 *col_p += strlen (string);
14701
14702 *first_p = 0;
14703}
14704
14705void
14706md_show_usage (stream)
14707 FILE *stream;
e7af610e 14708{
316f5878
RS
14709 int column, first;
14710 size_t i;
14711
14712 fprintf (stream, _("\
14713MIPS options:\n\
14714-membedded-pic generate embedded position independent code\n\
14715-EB generate big endian output\n\
14716-EL generate little endian output\n\
14717-g, -g2 do not remove unneeded NOPs or swap branches\n\
14718-G NUM allow referencing objects up to NUM bytes\n\
14719 implicitly with the gp register [default 8]\n"));
14720 fprintf (stream, _("\
14721-mips1 generate MIPS ISA I instructions\n\
14722-mips2 generate MIPS ISA II instructions\n\
14723-mips3 generate MIPS ISA III instructions\n\
14724-mips4 generate MIPS ISA IV instructions\n\
14725-mips5 generate MIPS ISA V instructions\n\
14726-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 14727-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878
RS
14728-mips64 generate MIPS64 ISA instructions\n\
14729-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14730
14731 first = 1;
e7af610e
NC
14732
14733 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
14734 show (stream, mips_cpu_info_table[i].name, &column, &first);
14735 show (stream, "from-abi", &column, &first);
14736 fputc ('\n', stream);
e7af610e 14737
316f5878
RS
14738 fprintf (stream, _("\
14739-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14740-no-mCPU don't generate code specific to CPU.\n\
14741 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14742
14743 first = 1;
14744
14745 show (stream, "3900", &column, &first);
14746 show (stream, "4010", &column, &first);
14747 show (stream, "4100", &column, &first);
14748 show (stream, "4650", &column, &first);
14749 fputc ('\n', stream);
14750
14751 fprintf (stream, _("\
14752-mips16 generate mips16 instructions\n\
14753-no-mips16 do not generate mips16 instructions\n"));
14754 fprintf (stream, _("\
14755-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14756-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14757-O0 remove unneeded NOPs, do not swap branches\n\
14758-O remove unneeded NOPs and swap branches\n\
14759-n warn about NOPs generated from macros\n\
14760--[no-]construct-floats [dis]allow floating point values to be constructed\n\
14761--trap, --no-break trap exception on div by 0 and mult overflow\n\
14762--break, --no-trap break exception on div by 0 and mult overflow\n"));
14763#ifdef OBJ_ELF
14764 fprintf (stream, _("\
14765-KPIC, -call_shared generate SVR4 position independent code\n\
14766-non_shared do not generate position independent code\n\
14767-xgot assume a 32 bit GOT\n\
14768-mabi=ABI create ABI conformant object file for:\n"));
14769
14770 first = 1;
14771
14772 show (stream, "32", &column, &first);
14773 show (stream, "o64", &column, &first);
14774 show (stream, "n32", &column, &first);
14775 show (stream, "64", &column, &first);
14776 show (stream, "eabi", &column, &first);
14777
14778 fputc ('\n', stream);
14779
14780 fprintf (stream, _("\
14781-32 create o32 ABI object file (default)\n\
14782-n32 create n32 ABI object file\n\
14783-64 create 64 ABI object file\n"));
14784#endif
e7af610e 14785}
14e777e0
KB
14786
14787enum dwarf2_format
14788mips_dwarf2_format ()
14789{
14790 if (mips_abi == N64_ABI)
14791 return dwarf2_format_64bit_irix;
14792 else
14793 return dwarf2_format_32bit;
14794}
This page took 1.086226 seconds and 4 git commands to generate.