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