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