6eecede995fc032d6b094eb5c0459659f1fca217
[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 if (mips_opts.isa >= 3)
4980 load_register (&icnt, treg, &imm_expr, 1);
4981 else
4982 {
4983 int hreg, lreg;
4984
4985 if (target_big_endian)
4986 {
4987 hreg = treg;
4988 lreg = treg + 1;
4989 }
4990 else
4991 {
4992 hreg = treg + 1;
4993 lreg = treg;
4994 }
4995
4996 if (hreg <= 31)
4997 load_register (&icnt, hreg, &imm_expr, 0);
4998 if (lreg <= 31)
4999 {
5000 if (offset_expr.X_op == O_absent)
5001 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s",
5002 lreg, 0);
5003 else
5004 {
5005 assert (offset_expr.X_op == O_constant);
5006 load_register (&icnt, lreg, &offset_expr, 0);
5007 }
5008 }
5009 }
5010 return;
5011 }
5012
5013 /* We know that sym is in the .rdata section. First we get the
5014 upper 16 bits of the address. */
5015 if (mips_pic == NO_PIC)
5016 {
5017 /* FIXME: This won't work for a 64 bit address. */
5018 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
5019 }
5020 else if (mips_pic == SVR4_PIC)
5021 {
5022 macro_build ((char *) NULL, &icnt, &offset_expr,
5023 mips_opts.isa < 3 ? "lw" : "ld",
5024 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5025 }
5026 else if (mips_pic == EMBEDDED_PIC)
5027 {
5028 /* For embedded PIC we pick up the entire address off $gp in
5029 a single instruction. */
5030 macro_build ((char *) NULL, &icnt, &offset_expr,
5031 mips_opts.isa < 3 ? "addiu" : "daddiu",
5032 "t,r,j", AT, GP, (int) BFD_RELOC_MIPS_GPREL);
5033 offset_expr.X_op = O_constant;
5034 offset_expr.X_add_number = 0;
5035 }
5036 else
5037 abort ();
5038
5039 /* Now we load the register(s). */
5040 if (mips_opts.isa >= 3)
5041 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5042 treg, (int) BFD_RELOC_LO16, AT);
5043 else
5044 {
5045 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5046 treg, (int) BFD_RELOC_LO16, AT);
5047 if (treg != 31)
5048 {
5049 /* FIXME: How in the world do we deal with the possible
5050 overflow here? */
5051 offset_expr.X_add_number += 4;
5052 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5053 treg + 1, (int) BFD_RELOC_LO16, AT);
5054 }
5055 }
5056
5057 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5058 does not become a variant frag. */
5059 frag_wane (frag_now);
5060 frag_new (0);
5061
5062 break;
5063
5064 case M_LI_DD:
5065 /* If we have a constant in IMM_EXPR, then in mips3 mode it is
5066 the entire value, and in mips1 mode it is the high order 32
5067 bits of the value and the low order 32 bits are either zero
5068 or in offset_expr. */
5069 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5070 {
5071 load_register (&icnt, AT, &imm_expr, mips_opts.isa >= 3);
5072 if (mips_opts.isa >= 3)
5073 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5074 "dmtc1", "t,S", AT, treg);
5075 else
5076 {
5077 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5078 "mtc1", "t,G", AT, treg + 1);
5079 if (offset_expr.X_op == O_absent)
5080 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5081 "mtc1", "t,G", 0, treg);
5082 else
5083 {
5084 assert (offset_expr.X_op == O_constant);
5085 load_register (&icnt, AT, &offset_expr, 0);
5086 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5087 "mtc1", "t,G", AT, treg);
5088 }
5089 }
5090 break;
5091 }
5092
5093 assert (offset_expr.X_op == O_symbol
5094 && offset_expr.X_add_number == 0);
5095 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5096 if (strcmp (s, ".lit8") == 0)
5097 {
5098 if (mips_opts.isa >= 2)
5099 {
5100 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5101 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5102 return;
5103 }
5104 breg = GP;
5105 r = BFD_RELOC_MIPS_LITERAL;
5106 goto dob;
5107 }
5108 else
5109 {
5110 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5111 if (mips_pic == SVR4_PIC)
5112 macro_build ((char *) NULL, &icnt, &offset_expr,
5113 mips_opts.isa < 3 ? "lw" : "ld",
5114 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5115 else
5116 {
5117 /* FIXME: This won't work for a 64 bit address. */
5118 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
5119 }
5120
5121 if (mips_opts.isa >= 2)
5122 {
5123 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5124 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5125
5126 /* To avoid confusion in tc_gen_reloc, we must ensure
5127 that this does not become a variant frag. */
5128 frag_wane (frag_now);
5129 frag_new (0);
5130
5131 break;
5132 }
5133 breg = AT;
5134 r = BFD_RELOC_LO16;
5135 goto dob;
5136 }
5137
5138 case M_L_DOB:
5139 /* Even on a big endian machine $fn comes before $fn+1. We have
5140 to adjust when loading from memory. */
5141 r = BFD_RELOC_LO16;
5142 dob:
5143 assert (mips_opts.isa < 2);
5144 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5145 target_big_endian ? treg + 1 : treg,
5146 (int) r, breg);
5147 /* FIXME: A possible overflow which I don't know how to deal
5148 with. */
5149 offset_expr.X_add_number += 4;
5150 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5151 target_big_endian ? treg : treg + 1,
5152 (int) r, breg);
5153
5154 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5155 does not become a variant frag. */
5156 frag_wane (frag_now);
5157 frag_new (0);
5158
5159 if (breg != AT)
5160 return;
5161 break;
5162
5163 case M_L_DAB:
5164 /*
5165 * The MIPS assembler seems to check for X_add_number not
5166 * being double aligned and generating:
5167 * lui at,%hi(foo+1)
5168 * addu at,at,v1
5169 * addiu at,at,%lo(foo+1)
5170 * lwc1 f2,0(at)
5171 * lwc1 f3,4(at)
5172 * But, the resulting address is the same after relocation so why
5173 * generate the extra instruction?
5174 */
5175 /* Itbl support may require additional care here. */
5176 coproc = 1;
5177 if (mips_opts.isa >= 2)
5178 {
5179 s = "ldc1";
5180 goto ld;
5181 }
5182
5183 s = "lwc1";
5184 fmt = "T,o(b)";
5185 goto ldd_std;
5186
5187 case M_S_DAB:
5188 if (mips_opts.isa >= 2)
5189 {
5190 s = "sdc1";
5191 goto st;
5192 }
5193
5194 s = "swc1";
5195 fmt = "T,o(b)";
5196 /* Itbl support may require additional care here. */
5197 coproc = 1;
5198 goto ldd_std;
5199
5200 case M_LD_AB:
5201 if (mips_opts.isa >= 3)
5202 {
5203 s = "ld";
5204 goto ld;
5205 }
5206
5207 s = "lw";
5208 fmt = "t,o(b)";
5209 goto ldd_std;
5210
5211 case M_SD_AB:
5212 if (mips_opts.isa >= 3)
5213 {
5214 s = "sd";
5215 goto st;
5216 }
5217
5218 s = "sw";
5219 fmt = "t,o(b)";
5220
5221 ldd_std:
5222 if (offset_expr.X_op != O_symbol
5223 && offset_expr.X_op != O_constant)
5224 {
5225 as_bad ("expression too complex");
5226 offset_expr.X_op = O_constant;
5227 }
5228
5229 /* Even on a big endian machine $fn comes before $fn+1. We have
5230 to adjust when loading from memory. We set coproc if we must
5231 load $fn+1 first. */
5232 /* Itbl support may require additional care here. */
5233 if (! target_big_endian)
5234 coproc = 0;
5235
5236 if (mips_pic == NO_PIC
5237 || offset_expr.X_op == O_constant)
5238 {
5239 /* If this is a reference to a GP relative symbol, we want
5240 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5241 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
5242 If we have a base register, we use this
5243 addu $at,$breg,$gp
5244 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
5245 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
5246 If this is not a GP relative symbol, we want
5247 lui $at,<sym> (BFD_RELOC_HI16_S)
5248 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5249 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5250 If there is a base register, we add it to $at after the
5251 lui instruction. If there is a constant, we always use
5252 the last case. */
5253 if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
5254 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5255 {
5256 p = NULL;
5257 used_at = 1;
5258 }
5259 else
5260 {
5261 int off;
5262
5263 if (breg == 0)
5264 {
5265 frag_grow (28);
5266 tempreg = GP;
5267 off = 0;
5268 used_at = 0;
5269 }
5270 else
5271 {
5272 frag_grow (36);
5273 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5274 mips_opts.isa < 3 ? "addu" : "daddu",
5275 "d,v,t", AT, breg, GP);
5276 tempreg = AT;
5277 off = 4;
5278 used_at = 1;
5279 }
5280
5281 /* Itbl support may require additional care here. */
5282 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5283 coproc ? treg + 1 : treg,
5284 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5285 offset_expr.X_add_number += 4;
5286
5287 /* Set mips_optimize to 2 to avoid inserting an
5288 undesired nop. */
5289 hold_mips_optimize = mips_optimize;
5290 mips_optimize = 2;
5291 /* Itbl support may require additional care here. */
5292 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5293 coproc ? treg : treg + 1,
5294 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5295 mips_optimize = hold_mips_optimize;
5296
5297 p = frag_var (rs_machine_dependent, 12 + off, 0,
5298 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
5299 used_at && mips_opts.noat),
5300 offset_expr.X_add_symbol, (offsetT) 0,
5301 (char *) NULL);
5302
5303 /* We just generated two relocs. When tc_gen_reloc
5304 handles this case, it will skip the first reloc and
5305 handle the second. The second reloc already has an
5306 extra addend of 4, which we added above. We must
5307 subtract it out, and then subtract another 4 to make
5308 the first reloc come out right. The second reloc
5309 will come out right because we are going to add 4 to
5310 offset_expr when we build its instruction below.
5311
5312 If we have a symbol, then we don't want to include
5313 the offset, because it will wind up being included
5314 when we generate the reloc. */
5315
5316 if (offset_expr.X_op == O_constant)
5317 offset_expr.X_add_number -= 8;
5318 else
5319 {
5320 offset_expr.X_add_number = -4;
5321 offset_expr.X_op = O_constant;
5322 }
5323 }
5324 macro_build_lui (p, &icnt, &offset_expr, AT);
5325 if (p != NULL)
5326 p += 4;
5327 if (breg != 0)
5328 {
5329 macro_build (p, &icnt, (expressionS *) NULL,
5330 mips_opts.isa < 3 ? "addu" : "daddu",
5331 "d,v,t", AT, breg, AT);
5332 if (p != NULL)
5333 p += 4;
5334 }
5335 /* Itbl support may require additional care here. */
5336 macro_build (p, &icnt, &offset_expr, s, fmt,
5337 coproc ? treg + 1 : treg,
5338 (int) BFD_RELOC_LO16, AT);
5339 if (p != NULL)
5340 p += 4;
5341 /* FIXME: How do we handle overflow here? */
5342 offset_expr.X_add_number += 4;
5343 /* Itbl support may require additional care here. */
5344 macro_build (p, &icnt, &offset_expr, s, fmt,
5345 coproc ? treg : treg + 1,
5346 (int) BFD_RELOC_LO16, AT);
5347 }
5348 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5349 {
5350 int off;
5351
5352 /* If this is a reference to an external symbol, we want
5353 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5354 nop
5355 <op> $treg,0($at)
5356 <op> $treg+1,4($at)
5357 Otherwise we want
5358 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5359 nop
5360 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5361 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5362 If there is a base register we add it to $at before the
5363 lwc1 instructions. If there is a constant we include it
5364 in the lwc1 instructions. */
5365 used_at = 1;
5366 expr1.X_add_number = offset_expr.X_add_number;
5367 offset_expr.X_add_number = 0;
5368 if (expr1.X_add_number < -0x8000
5369 || expr1.X_add_number >= 0x8000 - 4)
5370 as_bad ("PIC code offset overflow (max 16 signed bits)");
5371 if (breg == 0)
5372 off = 0;
5373 else
5374 off = 4;
5375 frag_grow (24 + off);
5376 macro_build ((char *) NULL, &icnt, &offset_expr,
5377 mips_opts.isa < 3 ? "lw" : "ld",
5378 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5379 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5380 if (breg != 0)
5381 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5382 mips_opts.isa < 3 ? "addu" : "daddu",
5383 "d,v,t", AT, breg, AT);
5384 /* Itbl support may require additional care here. */
5385 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5386 coproc ? treg + 1 : treg,
5387 (int) BFD_RELOC_LO16, AT);
5388 expr1.X_add_number += 4;
5389
5390 /* Set mips_optimize to 2 to avoid inserting an undesired
5391 nop. */
5392 hold_mips_optimize = mips_optimize;
5393 mips_optimize = 2;
5394 /* Itbl support may require additional care here. */
5395 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5396 coproc ? treg : treg + 1,
5397 (int) BFD_RELOC_LO16, AT);
5398 mips_optimize = hold_mips_optimize;
5399
5400 (void) frag_var (rs_machine_dependent, 0, 0,
5401 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
5402 offset_expr.X_add_symbol, (offsetT) 0,
5403 (char *) NULL);
5404 }
5405 else if (mips_pic == SVR4_PIC)
5406 {
5407 int gpdel, off;
5408
5409 /* If this is a reference to an external symbol, we want
5410 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5411 addu $at,$at,$gp
5412 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
5413 nop
5414 <op> $treg,0($at)
5415 <op> $treg+1,4($at)
5416 Otherwise we want
5417 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5418 nop
5419 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5420 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5421 If there is a base register we add it to $at before the
5422 lwc1 instructions. If there is a constant we include it
5423 in the lwc1 instructions. */
5424 used_at = 1;
5425 expr1.X_add_number = offset_expr.X_add_number;
5426 offset_expr.X_add_number = 0;
5427 if (expr1.X_add_number < -0x8000
5428 || expr1.X_add_number >= 0x8000 - 4)
5429 as_bad ("PIC code offset overflow (max 16 signed bits)");
5430 if (reg_needs_delay (GP))
5431 gpdel = 4;
5432 else
5433 gpdel = 0;
5434 if (breg == 0)
5435 off = 0;
5436 else
5437 off = 4;
5438 frag_grow (56);
5439 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5440 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
5441 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5442 mips_opts.isa < 3 ? "addu" : "daddu",
5443 "d,v,t", AT, AT, GP);
5444 macro_build ((char *) NULL, &icnt, &offset_expr,
5445 mips_opts.isa < 3 ? "lw" : "ld",
5446 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
5447 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5448 if (breg != 0)
5449 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5450 mips_opts.isa < 3 ? "addu" : "daddu",
5451 "d,v,t", AT, breg, AT);
5452 /* Itbl support may require additional care here. */
5453 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5454 coproc ? treg + 1 : treg,
5455 (int) BFD_RELOC_LO16, AT);
5456 expr1.X_add_number += 4;
5457
5458 /* Set mips_optimize to 2 to avoid inserting an undesired
5459 nop. */
5460 hold_mips_optimize = mips_optimize;
5461 mips_optimize = 2;
5462 /* Itbl support may require additional care here. */
5463 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5464 coproc ? treg : treg + 1,
5465 (int) BFD_RELOC_LO16, AT);
5466 mips_optimize = hold_mips_optimize;
5467 expr1.X_add_number -= 4;
5468
5469 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
5470 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
5471 8 + gpdel + off, 1, 0),
5472 offset_expr.X_add_symbol, (offsetT) 0,
5473 (char *) NULL);
5474 if (gpdel > 0)
5475 {
5476 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5477 p += 4;
5478 }
5479 macro_build (p, &icnt, &offset_expr,
5480 mips_opts.isa < 3 ? "lw" : "ld",
5481 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5482 p += 4;
5483 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5484 p += 4;
5485 if (breg != 0)
5486 {
5487 macro_build (p, &icnt, (expressionS *) NULL,
5488 mips_opts.isa < 3 ? "addu" : "daddu",
5489 "d,v,t", AT, breg, AT);
5490 p += 4;
5491 }
5492 /* Itbl support may require additional care here. */
5493 macro_build (p, &icnt, &expr1, s, fmt,
5494 coproc ? treg + 1 : treg,
5495 (int) BFD_RELOC_LO16, AT);
5496 p += 4;
5497 expr1.X_add_number += 4;
5498
5499 /* Set mips_optimize to 2 to avoid inserting an undesired
5500 nop. */
5501 hold_mips_optimize = mips_optimize;
5502 mips_optimize = 2;
5503 /* Itbl support may require additional care here. */
5504 macro_build (p, &icnt, &expr1, s, fmt,
5505 coproc ? treg : treg + 1,
5506 (int) BFD_RELOC_LO16, AT);
5507 mips_optimize = hold_mips_optimize;
5508 }
5509 else if (mips_pic == EMBEDDED_PIC)
5510 {
5511 /* If there is no base register, we use
5512 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5513 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
5514 If we have a base register, we use
5515 addu $at,$breg,$gp
5516 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
5517 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
5518 */
5519 if (breg == 0)
5520 {
5521 tempreg = GP;
5522 used_at = 0;
5523 }
5524 else
5525 {
5526 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5527 mips_opts.isa < 3 ? "addu" : "daddu",
5528 "d,v,t", AT, breg, GP);
5529 tempreg = AT;
5530 used_at = 1;
5531 }
5532
5533 /* Itbl support may require additional care here. */
5534 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5535 coproc ? treg + 1 : treg,
5536 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5537 offset_expr.X_add_number += 4;
5538 /* Itbl support may require additional care here. */
5539 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5540 coproc ? treg : treg + 1,
5541 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5542 }
5543 else
5544 abort ();
5545
5546 if (! used_at)
5547 return;
5548
5549 break;
5550
5551 case M_LD_OB:
5552 s = "lw";
5553 goto sd_ob;
5554 case M_SD_OB:
5555 s = "sw";
5556 sd_ob:
5557 assert (mips_opts.isa < 3);
5558 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
5559 (int) BFD_RELOC_LO16, breg);
5560 offset_expr.X_add_number += 4;
5561 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
5562 (int) BFD_RELOC_LO16, breg);
5563 return;
5564
5565 /* New code added to support COPZ instructions.
5566 This code builds table entries out of the macros in mip_opcodes.
5567 R4000 uses interlocks to handle coproc delays.
5568 Other chips (like the R3000) require nops to be inserted for delays.
5569
5570 FIXME: Currently, we require that the user handle delays.
5571 In order to fill delay slots for non-interlocked chips,
5572 we must have a way to specify delays based on the coprocessor.
5573 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
5574 What are the side-effects of the cop instruction?
5575 What cache support might we have and what are its effects?
5576 Both coprocessor & memory require delays. how long???
5577 What registers are read/set/modified?
5578
5579 If an itbl is provided to interpret cop instructions,
5580 this knowledge can be encoded in the itbl spec. */
5581
5582 case M_COP0:
5583 s = "cop0";
5584 goto copz;
5585 case M_COP1:
5586 s = "cop1";
5587 goto copz;
5588 case M_COP2:
5589 s = "cop2";
5590 goto copz;
5591 case M_COP3:
5592 s = "cop3";
5593 copz:
5594 /* For now we just do C (same as Cz). */
5595 macro_build ((char *) NULL, &icnt, &offset_expr, s, "C");
5596 return;
5597
5598 #ifdef LOSING_COMPILER
5599 default:
5600 /* Try and see if this is a new itbl instruction.
5601 This code builds table entries out of the macros in mip_opcodes.
5602 FIXME: For now we just assemble the expression and pass it's
5603 value along as a 32-bit immediate.
5604 We may want to have the assembler assemble this value,
5605 so that we gain the assembler's knowledge of delay slots,
5606 symbols, etc.
5607 Would it be more efficient to use mask (id) here? */
5608 if (itbl_have_entries
5609 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
5610 {
5611 s = ip->insn_mo->name;
5612 s2 = "cop3";
5613 coproc = ITBL_DECODE_PNUM (immed_expr);;
5614 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
5615 return;
5616 }
5617 macro2 (ip);
5618 return;
5619 }
5620 if (mips_opts.noat)
5621 as_warn ("Macro used $at after \".set noat\"");
5622 }
5623
5624 static void
5625 macro2 (ip)
5626 struct mips_cl_insn *ip;
5627 {
5628 register int treg, sreg, dreg, breg;
5629 int tempreg;
5630 int mask;
5631 int icnt = 0;
5632 int used_at;
5633 expressionS expr1;
5634 const char *s;
5635 const char *s2;
5636 const char *fmt;
5637 int likely = 0;
5638 int dbl = 0;
5639 int coproc = 0;
5640 int lr = 0;
5641 int off;
5642 offsetT maxnum;
5643 bfd_reloc_code_real_type r;
5644 char *p;
5645
5646 treg = (ip->insn_opcode >> 16) & 0x1f;
5647 dreg = (ip->insn_opcode >> 11) & 0x1f;
5648 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
5649 mask = ip->insn_mo->mask;
5650
5651 expr1.X_op = O_constant;
5652 expr1.X_op_symbol = NULL;
5653 expr1.X_add_symbol = NULL;
5654 expr1.X_add_number = 1;
5655
5656 switch (mask)
5657 {
5658 #endif /* LOSING_COMPILER */
5659
5660 case M_DMUL:
5661 dbl = 1;
5662 case M_MUL:
5663 macro_build ((char *) NULL, &icnt, NULL,
5664 dbl ? "dmultu" : "multu",
5665 "s,t", sreg, treg);
5666 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5667 return;
5668
5669 case M_DMUL_I:
5670 dbl = 1;
5671 case M_MUL_I:
5672 /* The MIPS assembler some times generates shifts and adds. I'm
5673 not trying to be that fancy. GCC should do this for us
5674 anyway. */
5675 load_register (&icnt, AT, &imm_expr, dbl);
5676 macro_build ((char *) NULL, &icnt, NULL,
5677 dbl ? "dmult" : "mult",
5678 "s,t", sreg, AT);
5679 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5680 break;
5681
5682 case M_DMULO:
5683 dbl = 1;
5684 case M_MULO:
5685 mips_emit_delays (true);
5686 ++mips_opts.noreorder;
5687 mips_any_noreorder = 1;
5688 macro_build ((char *) NULL, &icnt, NULL,
5689 dbl ? "dmult" : "mult",
5690 "s,t", sreg, treg);
5691 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5692 macro_build ((char *) NULL, &icnt, NULL,
5693 dbl ? "dsra32" : "sra",
5694 "d,w,<", dreg, dreg, 31);
5695 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
5696 if (mips_trap)
5697 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", dreg, AT);
5698 else
5699 {
5700 expr1.X_add_number = 8;
5701 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
5702 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
5703 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
5704 }
5705 --mips_opts.noreorder;
5706 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5707 break;
5708
5709 case M_DMULOU:
5710 dbl = 1;
5711 case M_MULOU:
5712 mips_emit_delays (true);
5713 ++mips_opts.noreorder;
5714 mips_any_noreorder = 1;
5715 macro_build ((char *) NULL, &icnt, NULL,
5716 dbl ? "dmultu" : "multu",
5717 "s,t", sreg, treg);
5718 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
5719 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5720 if (mips_trap)
5721 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", AT, 0);
5722 else
5723 {
5724 expr1.X_add_number = 8;
5725 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
5726 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
5727 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
5728 }
5729 --mips_opts.noreorder;
5730 break;
5731
5732 case M_ROL:
5733 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
5734 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
5735 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg,
5736 treg);
5737 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
5738 break;
5739
5740 case M_ROL_I:
5741 if (imm_expr.X_op != O_constant)
5742 as_bad ("rotate count too large");
5743 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg,
5744 (int) (imm_expr.X_add_number & 0x1f));
5745 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
5746 (int) ((0 - imm_expr.X_add_number) & 0x1f));
5747 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
5748 break;
5749
5750 case M_ROR:
5751 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
5752 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
5753 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg,
5754 treg);
5755 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
5756 break;
5757
5758 case M_ROR_I:
5759 if (imm_expr.X_op != O_constant)
5760 as_bad ("rotate count too large");
5761 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg,
5762 (int) (imm_expr.X_add_number & 0x1f));
5763 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
5764 (int) ((0 - imm_expr.X_add_number) & 0x1f));
5765 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
5766 break;
5767
5768 case M_S_DOB:
5769 assert (mips_opts.isa < 2);
5770 /* Even on a big endian machine $fn comes before $fn+1. We have
5771 to adjust when storing to memory. */
5772 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
5773 target_big_endian ? treg + 1 : treg,
5774 (int) BFD_RELOC_LO16, breg);
5775 offset_expr.X_add_number += 4;
5776 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
5777 target_big_endian ? treg : treg + 1,
5778 (int) BFD_RELOC_LO16, breg);
5779 return;
5780
5781 case M_SEQ:
5782 if (sreg == 0)
5783 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
5784 treg, (int) BFD_RELOC_LO16);
5785 else if (treg == 0)
5786 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
5787 sreg, (int) BFD_RELOC_LO16);
5788 else
5789 {
5790 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
5791 sreg, treg);
5792 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
5793 dreg, (int) BFD_RELOC_LO16);
5794 }
5795 return;
5796
5797 case M_SEQ_I:
5798 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5799 {
5800 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
5801 sreg, (int) BFD_RELOC_LO16);
5802 return;
5803 }
5804 if (sreg == 0)
5805 {
5806 as_warn ("Instruction %s: result is always false",
5807 ip->insn_mo->name);
5808 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
5809 return;
5810 }
5811 if (imm_expr.X_op == O_constant
5812 && imm_expr.X_add_number >= 0
5813 && imm_expr.X_add_number < 0x10000)
5814 {
5815 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
5816 sreg, (int) BFD_RELOC_LO16);
5817 used_at = 0;
5818 }
5819 else if (imm_expr.X_op == O_constant
5820 && imm_expr.X_add_number > -0x8000
5821 && imm_expr.X_add_number < 0)
5822 {
5823 imm_expr.X_add_number = -imm_expr.X_add_number;
5824 macro_build ((char *) NULL, &icnt, &imm_expr,
5825 mips_opts.isa < 3 ? "addiu" : "daddiu",
5826 "t,r,j", dreg, sreg,
5827 (int) BFD_RELOC_LO16);
5828 used_at = 0;
5829 }
5830 else
5831 {
5832 load_register (&icnt, AT, &imm_expr, 0);
5833 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
5834 sreg, AT);
5835 used_at = 1;
5836 }
5837 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
5838 (int) BFD_RELOC_LO16);
5839 if (used_at)
5840 break;
5841 return;
5842
5843 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
5844 s = "slt";
5845 goto sge;
5846 case M_SGEU:
5847 s = "sltu";
5848 sge:
5849 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
5850 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
5851 (int) BFD_RELOC_LO16);
5852 return;
5853
5854 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
5855 case M_SGEU_I:
5856 if (imm_expr.X_op == O_constant
5857 && imm_expr.X_add_number >= -0x8000
5858 && imm_expr.X_add_number < 0x8000)
5859 {
5860 macro_build ((char *) NULL, &icnt, &expr1,
5861 mask == M_SGE_I ? "slti" : "sltiu",
5862 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
5863 used_at = 0;
5864 }
5865 else
5866 {
5867 load_register (&icnt, AT, &imm_expr, 0);
5868 macro_build ((char *) NULL, &icnt, NULL,
5869 mask == M_SGE_I ? "slt" : "sltu",
5870 "d,v,t", dreg, sreg, AT);
5871 used_at = 1;
5872 }
5873 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
5874 (int) BFD_RELOC_LO16);
5875 if (used_at)
5876 break;
5877 return;
5878
5879 case M_SGT: /* sreg > treg <==> treg < sreg */
5880 s = "slt";
5881 goto sgt;
5882 case M_SGTU:
5883 s = "sltu";
5884 sgt:
5885 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
5886 return;
5887
5888 case M_SGT_I: /* sreg > I <==> I < sreg */
5889 s = "slt";
5890 goto sgti;
5891 case M_SGTU_I:
5892 s = "sltu";
5893 sgti:
5894 load_register (&icnt, AT, &imm_expr, 0);
5895 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
5896 break;
5897
5898 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
5899 s = "slt";
5900 goto sle;
5901 case M_SLEU:
5902 s = "sltu";
5903 sle:
5904 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
5905 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
5906 (int) BFD_RELOC_LO16);
5907 return;
5908
5909 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
5910 s = "slt";
5911 goto slei;
5912 case M_SLEU_I:
5913 s = "sltu";
5914 slei:
5915 load_register (&icnt, AT, &imm_expr, 0);
5916 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
5917 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
5918 (int) BFD_RELOC_LO16);
5919 break;
5920
5921 case M_SLT_I:
5922 if (imm_expr.X_op == O_constant
5923 && imm_expr.X_add_number >= -0x8000
5924 && imm_expr.X_add_number < 0x8000)
5925 {
5926 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
5927 dreg, sreg, (int) BFD_RELOC_LO16);
5928 return;
5929 }
5930 load_register (&icnt, AT, &imm_expr, 0);
5931 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
5932 break;
5933
5934 case M_SLTU_I:
5935 if (imm_expr.X_op == O_constant
5936 && imm_expr.X_add_number >= -0x8000
5937 && imm_expr.X_add_number < 0x8000)
5938 {
5939 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
5940 dreg, sreg, (int) BFD_RELOC_LO16);
5941 return;
5942 }
5943 load_register (&icnt, AT, &imm_expr, 0);
5944 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg,
5945 AT);
5946 break;
5947
5948 case M_SNE:
5949 if (sreg == 0)
5950 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
5951 treg);
5952 else if (treg == 0)
5953 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
5954 sreg);
5955 else
5956 {
5957 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
5958 sreg, treg);
5959 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
5960 dreg);
5961 }
5962 return;
5963
5964 case M_SNE_I:
5965 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5966 {
5967 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
5968 sreg);
5969 return;
5970 }
5971 if (sreg == 0)
5972 {
5973 as_warn ("Instruction %s: result is always true",
5974 ip->insn_mo->name);
5975 macro_build ((char *) NULL, &icnt, &expr1,
5976 mips_opts.isa < 3 ? "addiu" : "daddiu",
5977 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
5978 return;
5979 }
5980 if (imm_expr.X_op == O_constant
5981 && imm_expr.X_add_number >= 0
5982 && imm_expr.X_add_number < 0x10000)
5983 {
5984 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
5985 dreg, sreg, (int) BFD_RELOC_LO16);
5986 used_at = 0;
5987 }
5988 else if (imm_expr.X_op == O_constant
5989 && imm_expr.X_add_number > -0x8000
5990 && imm_expr.X_add_number < 0)
5991 {
5992 imm_expr.X_add_number = -imm_expr.X_add_number;
5993 macro_build ((char *) NULL, &icnt, &imm_expr,
5994 mips_opts.isa < 3 ? "addiu" : "daddiu",
5995 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
5996 used_at = 0;
5997 }
5998 else
5999 {
6000 load_register (&icnt, AT, &imm_expr, 0);
6001 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6002 sreg, AT);
6003 used_at = 1;
6004 }
6005 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
6006 if (used_at)
6007 break;
6008 return;
6009
6010 case M_DSUB_I:
6011 dbl = 1;
6012 case M_SUB_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 ? "daddi" : "addi",
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 ? "dsub" : "sub",
6026 "d,v,t", dreg, sreg, AT);
6027 break;
6028
6029 case M_DSUBU_I:
6030 dbl = 1;
6031 case M_SUBU_I:
6032 if (imm_expr.X_op == O_constant
6033 && imm_expr.X_add_number > -0x8000
6034 && imm_expr.X_add_number <= 0x8000)
6035 {
6036 imm_expr.X_add_number = -imm_expr.X_add_number;
6037 macro_build ((char *) NULL, &icnt, &imm_expr,
6038 dbl ? "daddiu" : "addiu",
6039 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6040 return;
6041 }
6042 load_register (&icnt, AT, &imm_expr, dbl);
6043 macro_build ((char *) NULL, &icnt, NULL,
6044 dbl ? "dsubu" : "subu",
6045 "d,v,t", dreg, sreg, AT);
6046 break;
6047
6048 case M_TEQ_I:
6049 s = "teq";
6050 goto trap;
6051 case M_TGE_I:
6052 s = "tge";
6053 goto trap;
6054 case M_TGEU_I:
6055 s = "tgeu";
6056 goto trap;
6057 case M_TLT_I:
6058 s = "tlt";
6059 goto trap;
6060 case M_TLTU_I:
6061 s = "tltu";
6062 goto trap;
6063 case M_TNE_I:
6064 s = "tne";
6065 trap:
6066 load_register (&icnt, AT, &imm_expr, 0);
6067 macro_build ((char *) NULL, &icnt, NULL, s, "s,t", sreg, AT);
6068 break;
6069
6070 case M_TRUNCWD:
6071 case M_TRUNCWS:
6072 assert (mips_opts.isa < 2);
6073 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
6074 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
6075
6076 /*
6077 * Is the double cfc1 instruction a bug in the mips assembler;
6078 * or is there a reason for it?
6079 */
6080 mips_emit_delays (true);
6081 ++mips_opts.noreorder;
6082 mips_any_noreorder = 1;
6083 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
6084 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
6085 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6086 expr1.X_add_number = 3;
6087 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
6088 (int) BFD_RELOC_LO16);
6089 expr1.X_add_number = 2;
6090 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
6091 (int) BFD_RELOC_LO16);
6092 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", AT, 31);
6093 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6094 macro_build ((char *) NULL, &icnt, NULL,
6095 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
6096 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", treg, 31);
6097 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6098 --mips_opts.noreorder;
6099 break;
6100
6101 case M_ULH:
6102 s = "lb";
6103 goto ulh;
6104 case M_ULHU:
6105 s = "lbu";
6106 ulh:
6107 if (offset_expr.X_add_number >= 0x7fff)
6108 as_bad ("operand overflow");
6109 /* avoid load delay */
6110 if (! target_big_endian)
6111 offset_expr.X_add_number += 1;
6112 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6113 (int) BFD_RELOC_LO16, breg);
6114 if (! target_big_endian)
6115 offset_expr.X_add_number -= 1;
6116 else
6117 offset_expr.X_add_number += 1;
6118 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
6119 (int) BFD_RELOC_LO16, breg);
6120 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
6121 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
6122 break;
6123
6124 case M_ULD:
6125 s = "ldl";
6126 s2 = "ldr";
6127 off = 7;
6128 goto ulw;
6129 case M_ULW:
6130 s = "lwl";
6131 s2 = "lwr";
6132 off = 3;
6133 ulw:
6134 if (offset_expr.X_add_number >= 0x8000 - off)
6135 as_bad ("operand overflow");
6136 if (! target_big_endian)
6137 offset_expr.X_add_number += off;
6138 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6139 (int) BFD_RELOC_LO16, breg);
6140 if (! target_big_endian)
6141 offset_expr.X_add_number -= off;
6142 else
6143 offset_expr.X_add_number += off;
6144 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
6145 (int) BFD_RELOC_LO16, breg);
6146 return;
6147
6148 case M_ULD_A:
6149 s = "ldl";
6150 s2 = "ldr";
6151 off = 7;
6152 goto ulwa;
6153 case M_ULW_A:
6154 s = "lwl";
6155 s2 = "lwr";
6156 off = 3;
6157 ulwa:
6158 load_address (&icnt, AT, &offset_expr);
6159 if (breg != 0)
6160 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6161 mips_opts.isa < 3 ? "addu" : "daddu",
6162 "d,v,t", AT, AT, breg);
6163 if (! target_big_endian)
6164 expr1.X_add_number = off;
6165 else
6166 expr1.X_add_number = 0;
6167 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
6168 (int) BFD_RELOC_LO16, AT);
6169 if (! target_big_endian)
6170 expr1.X_add_number = 0;
6171 else
6172 expr1.X_add_number = off;
6173 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
6174 (int) BFD_RELOC_LO16, AT);
6175 break;
6176
6177 case M_ULH_A:
6178 case M_ULHU_A:
6179 load_address (&icnt, AT, &offset_expr);
6180 if (breg != 0)
6181 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6182 mips_opts.isa < 3 ? "addu" : "daddu",
6183 "d,v,t", AT, AT, breg);
6184 if (target_big_endian)
6185 expr1.X_add_number = 0;
6186 macro_build ((char *) NULL, &icnt, &expr1,
6187 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
6188 (int) BFD_RELOC_LO16, AT);
6189 if (target_big_endian)
6190 expr1.X_add_number = 1;
6191 else
6192 expr1.X_add_number = 0;
6193 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
6194 (int) BFD_RELOC_LO16, AT);
6195 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
6196 treg, 8);
6197 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
6198 treg, AT);
6199 break;
6200
6201 case M_USH:
6202 if (offset_expr.X_add_number >= 0x7fff)
6203 as_bad ("operand overflow");
6204 if (target_big_endian)
6205 offset_expr.X_add_number += 1;
6206 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
6207 (int) BFD_RELOC_LO16, breg);
6208 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
6209 if (target_big_endian)
6210 offset_expr.X_add_number -= 1;
6211 else
6212 offset_expr.X_add_number += 1;
6213 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
6214 (int) BFD_RELOC_LO16, breg);
6215 break;
6216
6217 case M_USD:
6218 s = "sdl";
6219 s2 = "sdr";
6220 off = 7;
6221 goto usw;
6222 case M_USW:
6223 s = "swl";
6224 s2 = "swr";
6225 off = 3;
6226 usw:
6227 if (offset_expr.X_add_number >= 0x8000 - off)
6228 as_bad ("operand overflow");
6229 if (! target_big_endian)
6230 offset_expr.X_add_number += off;
6231 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6232 (int) BFD_RELOC_LO16, breg);
6233 if (! target_big_endian)
6234 offset_expr.X_add_number -= off;
6235 else
6236 offset_expr.X_add_number += off;
6237 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
6238 (int) BFD_RELOC_LO16, breg);
6239 return;
6240
6241 case M_USD_A:
6242 s = "sdl";
6243 s2 = "sdr";
6244 off = 7;
6245 goto uswa;
6246 case M_USW_A:
6247 s = "swl";
6248 s2 = "swr";
6249 off = 3;
6250 uswa:
6251 load_address (&icnt, AT, &offset_expr);
6252 if (breg != 0)
6253 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6254 mips_opts.isa < 3 ? "addu" : "daddu",
6255 "d,v,t", AT, AT, breg);
6256 if (! target_big_endian)
6257 expr1.X_add_number = off;
6258 else
6259 expr1.X_add_number = 0;
6260 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
6261 (int) BFD_RELOC_LO16, AT);
6262 if (! target_big_endian)
6263 expr1.X_add_number = 0;
6264 else
6265 expr1.X_add_number = off;
6266 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
6267 (int) BFD_RELOC_LO16, AT);
6268 break;
6269
6270 case M_USH_A:
6271 load_address (&icnt, AT, &offset_expr);
6272 if (breg != 0)
6273 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6274 mips_opts.isa < 3 ? "addu" : "daddu",
6275 "d,v,t", AT, AT, breg);
6276 if (! target_big_endian)
6277 expr1.X_add_number = 0;
6278 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
6279 (int) BFD_RELOC_LO16, AT);
6280 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", treg,
6281 treg, 8);
6282 if (! target_big_endian)
6283 expr1.X_add_number = 1;
6284 else
6285 expr1.X_add_number = 0;
6286 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
6287 (int) BFD_RELOC_LO16, AT);
6288 if (! target_big_endian)
6289 expr1.X_add_number = 0;
6290 else
6291 expr1.X_add_number = 1;
6292 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
6293 (int) BFD_RELOC_LO16, AT);
6294 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
6295 treg, 8);
6296 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
6297 treg, AT);
6298 break;
6299
6300 default:
6301 /* FIXME: Check if this is one of the itbl macros, since they
6302 are added dynamically. */
6303 as_bad ("Macro %s not implemented yet", ip->insn_mo->name);
6304 break;
6305 }
6306 if (mips_opts.noat)
6307 as_warn ("Macro used $at after \".set noat\"");
6308 }
6309
6310 /* Implement macros in mips16 mode. */
6311
6312 static void
6313 mips16_macro (ip)
6314 struct mips_cl_insn *ip;
6315 {
6316 int mask;
6317 int xreg, yreg, zreg, tmp;
6318 int icnt;
6319 expressionS expr1;
6320 int dbl;
6321 const char *s, *s2, *s3;
6322
6323 mask = ip->insn_mo->mask;
6324
6325 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
6326 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
6327 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
6328
6329 icnt = 0;
6330
6331 expr1.X_op = O_constant;
6332 expr1.X_op_symbol = NULL;
6333 expr1.X_add_symbol = NULL;
6334 expr1.X_add_number = 1;
6335
6336 dbl = 0;
6337
6338 switch (mask)
6339 {
6340 default:
6341 internalError ();
6342
6343 case M_DDIV_3:
6344 dbl = 1;
6345 case M_DIV_3:
6346 s = "mflo";
6347 goto do_div3;
6348 case M_DREM_3:
6349 dbl = 1;
6350 case M_REM_3:
6351 s = "mfhi";
6352 do_div3:
6353 mips_emit_delays (true);
6354 ++mips_opts.noreorder;
6355 mips_any_noreorder = 1;
6356 macro_build ((char *) NULL, &icnt, NULL,
6357 dbl ? "ddiv" : "div",
6358 "0,x,y", xreg, yreg);
6359 expr1.X_add_number = 2;
6360 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
6361 macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
6362 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
6363 since that causes an overflow. We should do that as well,
6364 but I don't see how to do the comparisons without a temporary
6365 register. */
6366 --mips_opts.noreorder;
6367 macro_build ((char *) NULL, &icnt, NULL, s, "x", zreg);
6368 break;
6369
6370 case M_DIVU_3:
6371 s = "divu";
6372 s2 = "mflo";
6373 goto do_divu3;
6374 case M_REMU_3:
6375 s = "divu";
6376 s2 = "mfhi";
6377 goto do_divu3;
6378 case M_DDIVU_3:
6379 s = "ddivu";
6380 s2 = "mflo";
6381 goto do_divu3;
6382 case M_DREMU_3:
6383 s = "ddivu";
6384 s2 = "mfhi";
6385 do_divu3:
6386 mips_emit_delays (true);
6387 ++mips_opts.noreorder;
6388 mips_any_noreorder = 1;
6389 macro_build ((char *) NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
6390 expr1.X_add_number = 2;
6391 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
6392 macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
6393 --mips_opts.noreorder;
6394 macro_build ((char *) NULL, &icnt, NULL, s2, "x", zreg);
6395 break;
6396
6397 case M_DMUL:
6398 dbl = 1;
6399 case M_MUL:
6400 macro_build ((char *) NULL, &icnt, NULL,
6401 dbl ? "dmultu" : "multu",
6402 "x,y", xreg, yreg);
6403 macro_build ((char *) NULL, &icnt, NULL, "mflo", "x", zreg);
6404 return;
6405
6406 case M_DSUBU_I:
6407 dbl = 1;
6408 goto do_subu;
6409 case M_SUBU_I:
6410 do_subu:
6411 if (imm_expr.X_op != O_constant)
6412 as_bad ("Unsupported large constant");
6413 imm_expr.X_add_number = -imm_expr.X_add_number;
6414 macro_build ((char *) NULL, &icnt, &imm_expr,
6415 dbl ? "daddiu" : "addiu",
6416 "y,x,4", yreg, xreg);
6417 break;
6418
6419 case M_SUBU_I_2:
6420 if (imm_expr.X_op != O_constant)
6421 as_bad ("Unsupported large constant");
6422 imm_expr.X_add_number = -imm_expr.X_add_number;
6423 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
6424 "x,k", xreg);
6425 break;
6426
6427 case M_DSUBU_I_2:
6428 if (imm_expr.X_op != O_constant)
6429 as_bad ("Unsupported large constant");
6430 imm_expr.X_add_number = -imm_expr.X_add_number;
6431 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
6432 "y,j", yreg);
6433 break;
6434
6435 case M_BEQ:
6436 s = "cmp";
6437 s2 = "bteqz";
6438 goto do_branch;
6439 case M_BNE:
6440 s = "cmp";
6441 s2 = "btnez";
6442 goto do_branch;
6443 case M_BLT:
6444 s = "slt";
6445 s2 = "btnez";
6446 goto do_branch;
6447 case M_BLTU:
6448 s = "sltu";
6449 s2 = "btnez";
6450 goto do_branch;
6451 case M_BLE:
6452 s = "slt";
6453 s2 = "bteqz";
6454 goto do_reverse_branch;
6455 case M_BLEU:
6456 s = "sltu";
6457 s2 = "bteqz";
6458 goto do_reverse_branch;
6459 case M_BGE:
6460 s = "slt";
6461 s2 = "bteqz";
6462 goto do_branch;
6463 case M_BGEU:
6464 s = "sltu";
6465 s2 = "bteqz";
6466 goto do_branch;
6467 case M_BGT:
6468 s = "slt";
6469 s2 = "btnez";
6470 goto do_reverse_branch;
6471 case M_BGTU:
6472 s = "sltu";
6473 s2 = "btnez";
6474
6475 do_reverse_branch:
6476 tmp = xreg;
6477 xreg = yreg;
6478 yreg = tmp;
6479
6480 do_branch:
6481 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
6482 xreg, yreg);
6483 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
6484 break;
6485
6486 case M_BEQ_I:
6487 s = "cmpi";
6488 s2 = "bteqz";
6489 s3 = "x,U";
6490 goto do_branch_i;
6491 case M_BNE_I:
6492 s = "cmpi";
6493 s2 = "btnez";
6494 s3 = "x,U";
6495 goto do_branch_i;
6496 case M_BLT_I:
6497 s = "slti";
6498 s2 = "btnez";
6499 s3 = "x,8";
6500 goto do_branch_i;
6501 case M_BLTU_I:
6502 s = "sltiu";
6503 s2 = "btnez";
6504 s3 = "x,8";
6505 goto do_branch_i;
6506 case M_BLE_I:
6507 s = "slti";
6508 s2 = "btnez";
6509 s3 = "x,8";
6510 goto do_addone_branch_i;
6511 case M_BLEU_I:
6512 s = "sltiu";
6513 s2 = "btnez";
6514 s3 = "x,8";
6515 goto do_addone_branch_i;
6516 case M_BGE_I:
6517 s = "slti";
6518 s2 = "bteqz";
6519 s3 = "x,8";
6520 goto do_branch_i;
6521 case M_BGEU_I:
6522 s = "sltiu";
6523 s2 = "bteqz";
6524 s3 = "x,8";
6525 goto do_branch_i;
6526 case M_BGT_I:
6527 s = "slti";
6528 s2 = "bteqz";
6529 s3 = "x,8";
6530 goto do_addone_branch_i;
6531 case M_BGTU_I:
6532 s = "sltiu";
6533 s2 = "bteqz";
6534 s3 = "x,8";
6535
6536 do_addone_branch_i:
6537 if (imm_expr.X_op != O_constant)
6538 as_bad ("Unsupported large constant");
6539 ++imm_expr.X_add_number;
6540
6541 do_branch_i:
6542 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
6543 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
6544 break;
6545
6546 case M_ABS:
6547 expr1.X_add_number = 0;
6548 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
6549 if (xreg != yreg)
6550 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6551 "move", "y,X", xreg, yreg);
6552 expr1.X_add_number = 2;
6553 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
6554 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6555 "neg", "x,w", xreg, xreg);
6556 }
6557 }
6558
6559 /* This routine assembles an instruction into its binary format. As a
6560 side effect, it sets one of the global variables imm_reloc or
6561 offset_reloc to the type of relocation to do if one of the operands
6562 is an address expression. */
6563
6564 static void
6565 mips_ip (str, ip)
6566 char *str;
6567 struct mips_cl_insn *ip;
6568 {
6569 char *s;
6570 const char *args;
6571 char c;
6572 struct mips_opcode *insn;
6573 char *argsStart;
6574 unsigned int regno;
6575 unsigned int lastregno = 0;
6576 char *s_reset;
6577
6578 insn_error = NULL;
6579
6580 for (s = str; *s != '\0' && !isspace(*s); ++s)
6581 continue;
6582 if (isspace (*s))
6583 *s++ = '\0';
6584
6585 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
6586 {
6587 insn_error = "unrecognized opcode";
6588 return;
6589 }
6590 argsStart = s;
6591 for (;;)
6592 {
6593 int insn_isa;
6594 boolean ok;
6595
6596 assert (strcmp (insn->name, str) == 0);
6597
6598 if (insn->pinfo == INSN_MACRO)
6599 insn_isa = insn->match;
6600 else if ((insn->membership & INSN_ISA) == INSN_ISA1)
6601 insn_isa = 1;
6602 else if ((insn->membership & INSN_ISA) == INSN_ISA2)
6603 insn_isa = 2;
6604 else if ((insn->membership & INSN_ISA) == INSN_ISA3)
6605 insn_isa = 3;
6606 else if ((insn->membership & INSN_ISA) == INSN_ISA4)
6607 insn_isa = 4;
6608 else
6609 insn_isa = 15;
6610
6611 if (insn_isa <= mips_opts.isa)
6612 ok = true;
6613 else if (insn->pinfo == INSN_MACRO)
6614 ok = false;
6615 else if ((mips_4650 && (insn->membership & INSN_4650) != 0)
6616 || (mips_4010 && (insn->membership & INSN_4010) != 0)
6617 || (mips_4100 && (insn->membership & INSN_4100) != 0)
6618 /* start-sanitize-r5900 */
6619 || (mips_5900 && (insn->membership & INSN_5900) != 0)
6620 /* end-sanitize-r5900 */
6621 || (mips_3900 && (insn->membership & INSN_3900) != 0))
6622 {
6623 ok = true;
6624 if (mips_4650 && (insn->pinfo & FP_D) != 0)
6625 ok = false;
6626 /* start-sanitize-r5900 */
6627 if (mips_5900 && (insn->pinfo & FP_D) != 0)
6628 ok = false;
6629 /* end-sanitize-r5900 */
6630 }
6631 else
6632 ok = false;
6633
6634 if (! ok)
6635 {
6636 if (insn + 1 < &mips_opcodes[NUMOPCODES]
6637 && strcmp (insn->name, insn[1].name) == 0)
6638 {
6639 ++insn;
6640 continue;
6641 }
6642 if (insn_isa == 15
6643 || insn_isa <= mips_opts.isa)
6644 insn_error = "opcode not supported on this processor";
6645 else
6646 {
6647 static char buf[100];
6648
6649 sprintf (buf, "opcode requires -mips%d or greater", insn_isa);
6650 insn_error = buf;
6651 }
6652 return;
6653 }
6654
6655 ip->insn_mo = insn;
6656 ip->insn_opcode = insn->match;
6657 for (args = insn->args;; ++args)
6658 {
6659 if (*s == ' ')
6660 ++s;
6661 switch (*args)
6662 {
6663 case '\0': /* end of args */
6664 if (*s == '\0')
6665 return;
6666 break;
6667
6668 case ',':
6669 if (*s++ == *args)
6670 continue;
6671 s--;
6672 switch (*++args)
6673 {
6674 case 'r':
6675 case 'v':
6676 ip->insn_opcode |= lastregno << 21;
6677 continue;
6678
6679 case 'w':
6680 case 'W':
6681 ip->insn_opcode |= lastregno << 16;
6682 continue;
6683
6684 case 'V':
6685 ip->insn_opcode |= lastregno << 11;
6686 continue;
6687 }
6688 break;
6689
6690 case '(':
6691 /* handle optional base register.
6692 Either the base register is omitted or
6693 we must have a left paren. */
6694 /* this is dependent on the next operand specifier
6695 is a 'b' for base register */
6696 assert (args[1] == 'b');
6697 if (*s == '\0')
6698 return;
6699
6700 case ')': /* these must match exactly */
6701 if (*s++ == *args)
6702 continue;
6703 break;
6704
6705 case '<': /* must be at least one digit */
6706 /*
6707 * According to the manual, if the shift amount is greater
6708 * than 31 or less than 0 the the shift amount should be
6709 * mod 32. In reality the mips assembler issues an error.
6710 * We issue a warning and mask out all but the low 5 bits.
6711 */
6712 my_getExpression (&imm_expr, s);
6713 check_absolute_expr (ip, &imm_expr);
6714 if ((unsigned long) imm_expr.X_add_number > 31)
6715 {
6716 as_warn ("Improper shift amount (%ld)",
6717 (long) imm_expr.X_add_number);
6718 imm_expr.X_add_number = imm_expr.X_add_number & 0x1f;
6719 }
6720 ip->insn_opcode |= imm_expr.X_add_number << 6;
6721 imm_expr.X_op = O_absent;
6722 s = expr_end;
6723 continue;
6724
6725 case '>': /* shift amount minus 32 */
6726 my_getExpression (&imm_expr, s);
6727 check_absolute_expr (ip, &imm_expr);
6728 if ((unsigned long) imm_expr.X_add_number < 32
6729 || (unsigned long) imm_expr.X_add_number > 63)
6730 break;
6731 ip->insn_opcode |= (imm_expr.X_add_number - 32) << 6;
6732 imm_expr.X_op = O_absent;
6733 s = expr_end;
6734 continue;
6735
6736 case 'k': /* cache code */
6737 case 'h': /* prefx code */
6738 my_getExpression (&imm_expr, s);
6739 check_absolute_expr (ip, &imm_expr);
6740 if ((unsigned long) imm_expr.X_add_number > 31)
6741 {
6742 as_warn ("Invalid value for `%s' (%lu)",
6743 ip->insn_mo->name,
6744 (unsigned long) imm_expr.X_add_number);
6745 imm_expr.X_add_number &= 0x1f;
6746 }
6747 if (*args == 'k')
6748 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
6749 else
6750 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
6751 imm_expr.X_op = O_absent;
6752 s = expr_end;
6753 continue;
6754
6755 case 'c': /* break code */
6756 my_getExpression (&imm_expr, s);
6757 check_absolute_expr (ip, &imm_expr);
6758 if ((unsigned) imm_expr.X_add_number > 1023)
6759 as_warn ("Illegal break code (%ld)",
6760 (long) imm_expr.X_add_number);
6761 ip->insn_opcode |= imm_expr.X_add_number << 16;
6762 imm_expr.X_op = O_absent;
6763 s = expr_end;
6764 continue;
6765
6766 case 'B': /* syscall code */
6767 my_getExpression (&imm_expr, s);
6768 check_absolute_expr (ip, &imm_expr);
6769 if ((unsigned) imm_expr.X_add_number > 0xfffff)
6770 as_warn ("Illegal syscall code (%ld)",
6771 (long) imm_expr.X_add_number);
6772 ip->insn_opcode |= imm_expr.X_add_number << 6;
6773 imm_expr.X_op = O_absent;
6774 s = expr_end;
6775 continue;
6776
6777 case 'C': /* Coprocessor code */
6778 my_getExpression (&imm_expr, s);
6779 check_absolute_expr (ip, &imm_expr);
6780 if ((unsigned long) imm_expr.X_add_number >= (1<<25))
6781 {
6782 as_warn ("Coproccesor code > 25 bits (%ld)",
6783 (long) imm_expr.X_add_number);
6784 imm_expr.X_add_number &= ((1<<25) - 1);
6785 }
6786 ip->insn_opcode |= imm_expr.X_add_number;
6787 imm_expr.X_op = O_absent;
6788 s = expr_end;
6789 continue;
6790
6791 case 'b': /* base register */
6792 case 'd': /* destination register */
6793 case 's': /* source register */
6794 case 't': /* target register */
6795 case 'r': /* both target and source */
6796 case 'v': /* both dest and source */
6797 case 'w': /* both dest and target */
6798 case 'E': /* coprocessor target register */
6799 case 'G': /* coprocessor destination register */
6800 case 'x': /* ignore register name */
6801 case 'z': /* must be zero register */
6802 s_reset = s;
6803 if (s[0] == '$')
6804 {
6805 if (isdigit (s[1]))
6806 {
6807 ++s;
6808 regno = 0;
6809 do
6810 {
6811 regno *= 10;
6812 regno += *s - '0';
6813 ++s;
6814 }
6815 while (isdigit (*s));
6816 if (regno > 31)
6817 as_bad ("Invalid register number (%d)", regno);
6818 }
6819 else if (*args == 'E' || *args == 'G')
6820 goto notreg;
6821 else
6822 {
6823 if (s[1] == 'f' && s[2] == 'p')
6824 {
6825 s += 3;
6826 regno = FP;
6827 }
6828 else if (s[1] == 's' && s[2] == 'p')
6829 {
6830 s += 3;
6831 regno = SP;
6832 }
6833 else if (s[1] == 'g' && s[2] == 'p')
6834 {
6835 s += 3;
6836 regno = GP;
6837 }
6838 else if (s[1] == 'a' && s[2] == 't')
6839 {
6840 s += 3;
6841 regno = AT;
6842 }
6843 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
6844 {
6845 s += 4;
6846 regno = KT0;
6847 }
6848 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
6849 {
6850 s += 4;
6851 regno = KT1;
6852 }
6853 else if (itbl_have_entries)
6854 {
6855 char *p, *n;
6856 int r;
6857
6858 p = s+1; /* advance past '$' */
6859 n = itbl_get_field (&p); /* n is name */
6860
6861 /* See if this is a register defined in an
6862 itbl entry */
6863 r = itbl_get_reg_val (n);
6864 if (r)
6865 {
6866 /* Get_field advances to the start of
6867 the next field, so we need to back
6868 rack to the end of the last field. */
6869 if (p)
6870 s = p - 1;
6871 else
6872 s = strchr (s,'\0');
6873 regno = r;
6874 }
6875 else
6876 goto notreg;
6877 }
6878 else
6879 goto notreg;
6880 }
6881 if (regno == AT
6882 && ! mips_opts.noat
6883 && *args != 'E'
6884 && *args != 'G')
6885 as_warn ("Used $at without \".set noat\"");
6886 c = *args;
6887 if (*s == ' ')
6888 s++;
6889 if (args[1] != *s)
6890 {
6891 if (c == 'r' || c == 'v' || c == 'w')
6892 {
6893 regno = lastregno;
6894 s = s_reset;
6895 args++;
6896 }
6897 }
6898 /* 'z' only matches $0. */
6899 if (c == 'z' && regno != 0)
6900 break;
6901
6902 /* Now that we have assembled one operand, we use the args string
6903 * to figure out where it goes in the instruction. */
6904 switch (c)
6905 {
6906 case 'r':
6907 case 's':
6908 case 'v':
6909 case 'b':
6910 ip->insn_opcode |= regno << 21;
6911 break;
6912 case 'd':
6913 case 'G':
6914 ip->insn_opcode |= regno << 11;
6915 break;
6916 case 'w':
6917 case 't':
6918 case 'E':
6919 ip->insn_opcode |= regno << 16;
6920 break;
6921 case 'x':
6922 /* This case exists because on the r3000 trunc
6923 expands into a macro which requires a gp
6924 register. On the r6000 or r4000 it is
6925 assembled into a single instruction which
6926 ignores the register. Thus the insn version
6927 is MIPS_ISA2 and uses 'x', and the macro
6928 version is MIPS_ISA1 and uses 't'. */
6929 break;
6930 case 'z':
6931 /* This case is for the div instruction, which
6932 acts differently if the destination argument
6933 is $0. This only matches $0, and is checked
6934 outside the switch. */
6935 break;
6936 case 'D':
6937 /* Itbl operand; not yet implemented. FIXME ?? */
6938 break;
6939 /* What about all other operands like 'i', which
6940 can be specified in the opcode table? */
6941 }
6942 lastregno = regno;
6943 continue;
6944 }
6945 notreg:
6946 switch (*args++)
6947 {
6948 case 'r':
6949 case 'v':
6950 ip->insn_opcode |= lastregno << 21;
6951 continue;
6952 case 'w':
6953 ip->insn_opcode |= lastregno << 16;
6954 continue;
6955 }
6956 break;
6957
6958 case 'D': /* floating point destination register */
6959 case 'S': /* floating point source register */
6960 case 'T': /* floating point target register */
6961 case 'R': /* floating point source register */
6962 case 'V':
6963 case 'W':
6964 s_reset = s;
6965 if (s[0] == '$' && s[1] == 'f' && isdigit (s[2]))
6966 {
6967 s += 2;
6968 regno = 0;
6969 do
6970 {
6971 regno *= 10;
6972 regno += *s - '0';
6973 ++s;
6974 }
6975 while (isdigit (*s));
6976
6977 if (regno > 31)
6978 as_bad ("Invalid float register number (%d)", regno);
6979
6980 if ((regno & 1) != 0
6981 && mips_opts.isa < 3
6982 && ! (strcmp (str, "mtc1") == 0
6983 || strcmp (str, "mfc1") == 0
6984 || strcmp (str, "lwc1") == 0
6985 || strcmp (str, "swc1") == 0
6986 || strcmp (str, "l.s") == 0
6987 || strcmp (str, "s.s") == 0))
6988 as_warn ("Float register should be even, was %d",
6989 regno);
6990
6991 c = *args;
6992 if (*s == ' ')
6993 s++;
6994 if (args[1] != *s)
6995 {
6996 if (c == 'V' || c == 'W')
6997 {
6998 regno = lastregno;
6999 s = s_reset;
7000 args++;
7001 }
7002 }
7003 switch (c)
7004 {
7005 case 'D':
7006 ip->insn_opcode |= regno << 6;
7007 break;
7008 case 'V':
7009 case 'S':
7010 ip->insn_opcode |= regno << 11;
7011 break;
7012 case 'W':
7013 case 'T':
7014 ip->insn_opcode |= regno << 16;
7015 break;
7016 case 'R':
7017 ip->insn_opcode |= regno << 21;
7018 break;
7019 }
7020 lastregno = regno;
7021 continue;
7022 }
7023 switch (*args++)
7024 {
7025 case 'V':
7026 ip->insn_opcode |= lastregno << 11;
7027 continue;
7028 case 'W':
7029 ip->insn_opcode |= lastregno << 16;
7030 continue;
7031 }
7032 break;
7033
7034 case 'I':
7035 my_getExpression (&imm_expr, s);
7036 if (imm_expr.X_op != O_big
7037 && imm_expr.X_op != O_constant)
7038 insn_error = "absolute expression required";
7039 s = expr_end;
7040 continue;
7041
7042 case 'A':
7043 my_getExpression (&offset_expr, s);
7044 imm_reloc = BFD_RELOC_32;
7045 s = expr_end;
7046 continue;
7047
7048 case 'F':
7049 case 'L':
7050 case 'f':
7051 case 'l':
7052 {
7053 int f64;
7054 char *save_in;
7055 char *err;
7056 unsigned char temp[8];
7057 int len;
7058 unsigned int length;
7059 segT seg;
7060 subsegT subseg;
7061 char *p;
7062
7063 /* These only appear as the last operand in an
7064 instruction, and every instruction that accepts
7065 them in any variant accepts them in all variants.
7066 This means we don't have to worry about backing out
7067 any changes if the instruction does not match.
7068
7069 The difference between them is the size of the
7070 floating point constant and where it goes. For 'F'
7071 and 'L' the constant is 64 bits; for 'f' and 'l' it
7072 is 32 bits. Where the constant is placed is based
7073 on how the MIPS assembler does things:
7074 F -- .rdata
7075 L -- .lit8
7076 f -- immediate value
7077 l -- .lit4
7078
7079 The .lit4 and .lit8 sections are only used if
7080 permitted by the -G argument.
7081
7082 When generating embedded PIC code, we use the
7083 .lit8 section but not the .lit4 section (we can do
7084 .lit4 inline easily; we need to put .lit8
7085 somewhere in the data segment, and using .lit8
7086 permits the linker to eventually combine identical
7087 .lit8 entries). */
7088
7089 f64 = *args == 'F' || *args == 'L';
7090
7091 save_in = input_line_pointer;
7092 input_line_pointer = s;
7093 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
7094 length = len;
7095 s = input_line_pointer;
7096 input_line_pointer = save_in;
7097 if (err != NULL && *err != '\0')
7098 {
7099 as_bad ("Bad floating point constant: %s", err);
7100 memset (temp, '\0', sizeof temp);
7101 length = f64 ? 8 : 4;
7102 }
7103
7104 assert (length == (f64 ? 8 : 4));
7105
7106 if (*args == 'f'
7107 || (*args == 'l'
7108 && (! USE_GLOBAL_POINTER_OPT
7109 || mips_pic == EMBEDDED_PIC
7110 || g_switch_value < 4
7111 || (temp[0] == 0 && temp[1] == 0)
7112 || (temp[2] == 0 && temp[3] == 0))))
7113 {
7114 imm_expr.X_op = O_constant;
7115 if (! target_big_endian)
7116 imm_expr.X_add_number = bfd_getl32 (temp);
7117 else
7118 imm_expr.X_add_number = bfd_getb32 (temp);
7119 }
7120 else if (length > 4
7121 && ((temp[0] == 0 && temp[1] == 0)
7122 || (temp[2] == 0 && temp[3] == 0))
7123 && ((temp[4] == 0 && temp[5] == 0)
7124 || (temp[6] == 0 && temp[7] == 0)))
7125 {
7126 /* The value is simple enough to load with a
7127 couple of instructions. In mips1 mode, set
7128 imm_expr to the high order 32 bits and
7129 offset_expr to the low order 32 bits.
7130 Otherwise, set imm_expr to the entire 64 bit
7131 constant. */
7132 if (mips_opts.isa < 3)
7133 {
7134 imm_expr.X_op = O_constant;
7135 offset_expr.X_op = O_constant;
7136 if (! target_big_endian)
7137 {
7138 imm_expr.X_add_number = bfd_getl32 (temp + 4);
7139 offset_expr.X_add_number = bfd_getl32 (temp);
7140 }
7141 else
7142 {
7143 imm_expr.X_add_number = bfd_getb32 (temp);
7144 offset_expr.X_add_number = bfd_getb32 (temp + 4);
7145 }
7146 if (offset_expr.X_add_number == 0)
7147 offset_expr.X_op = O_absent;
7148 }
7149 else if (sizeof (imm_expr.X_add_number) > 4)
7150 {
7151 imm_expr.X_op = O_constant;
7152 if (! target_big_endian)
7153 imm_expr.X_add_number = bfd_getl64 (temp);
7154 else
7155 imm_expr.X_add_number = bfd_getb64 (temp);
7156 }
7157 else
7158 {
7159 imm_expr.X_op = O_big;
7160 imm_expr.X_add_number = 4;
7161 if (! target_big_endian)
7162 {
7163 generic_bignum[0] = bfd_getl16 (temp);
7164 generic_bignum[1] = bfd_getl16 (temp + 2);
7165 generic_bignum[2] = bfd_getl16 (temp + 4);
7166 generic_bignum[3] = bfd_getl16 (temp + 6);
7167 }
7168 else
7169 {
7170 generic_bignum[0] = bfd_getb16 (temp + 6);
7171 generic_bignum[1] = bfd_getb16 (temp + 4);
7172 generic_bignum[2] = bfd_getb16 (temp + 2);
7173 generic_bignum[3] = bfd_getb16 (temp);
7174 }
7175 }
7176 }
7177 else
7178 {
7179 const char *newname;
7180 segT new_seg;
7181
7182 /* Switch to the right section. */
7183 seg = now_seg;
7184 subseg = now_subseg;
7185 switch (*args)
7186 {
7187 default: /* unused default case avoids warnings. */
7188 case 'L':
7189 newname = RDATA_SECTION_NAME;
7190 if (USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
7191 newname = ".lit8";
7192 break;
7193 case 'F':
7194 newname = RDATA_SECTION_NAME;
7195 break;
7196 case 'l':
7197 assert (!USE_GLOBAL_POINTER_OPT
7198 || g_switch_value >= 4);
7199 newname = ".lit4";
7200 break;
7201 }
7202 new_seg = subseg_new (newname, (subsegT) 0);
7203 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
7204 bfd_set_section_flags (stdoutput, new_seg,
7205 (SEC_ALLOC
7206 | SEC_LOAD
7207 | SEC_READONLY
7208 | SEC_DATA));
7209 frag_align (*args == 'l' ? 2 : 3, 0, 0);
7210 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
7211 && strcmp (TARGET_OS, "elf") != 0)
7212 record_alignment (new_seg, 4);
7213 else
7214 record_alignment (new_seg, *args == 'l' ? 2 : 3);
7215 if (seg == now_seg)
7216 as_bad ("Can't use floating point insn in this section");
7217
7218 /* Set the argument to the current address in the
7219 section. */
7220 offset_expr.X_op = O_symbol;
7221 offset_expr.X_add_symbol =
7222 symbol_new ("L0\001", now_seg,
7223 (valueT) frag_now_fix (), frag_now);
7224 offset_expr.X_add_number = 0;
7225
7226 /* Put the floating point number into the section. */
7227 p = frag_more ((int) length);
7228 memcpy (p, temp, length);
7229
7230 /* Switch back to the original section. */
7231 subseg_set (seg, subseg);
7232 }
7233 }
7234 continue;
7235
7236 case 'i': /* 16 bit unsigned immediate */
7237 case 'j': /* 16 bit signed immediate */
7238 imm_reloc = BFD_RELOC_LO16;
7239 c = my_getSmallExpression (&imm_expr, s);
7240 if (c != '\0')
7241 {
7242 if (c != 'l')
7243 {
7244 if (imm_expr.X_op == O_constant)
7245 imm_expr.X_add_number =
7246 (imm_expr.X_add_number >> 16) & 0xffff;
7247 else if (c == 'h')
7248 {
7249 imm_reloc = BFD_RELOC_HI16_S;
7250 imm_unmatched_hi = true;
7251 }
7252 else
7253 imm_reloc = BFD_RELOC_HI16;
7254 }
7255 }
7256 if (*args == 'i')
7257 {
7258 if ((c == '\0' && imm_expr.X_op != O_constant)
7259 || ((imm_expr.X_add_number < 0
7260 || imm_expr.X_add_number >= 0x10000)
7261 && imm_expr.X_op == O_constant))
7262 {
7263 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7264 !strcmp (insn->name, insn[1].name))
7265 break;
7266 if (imm_expr.X_op != O_constant
7267 && imm_expr.X_op != O_big)
7268 insn_error = "absolute expression required";
7269 else
7270 as_bad ("16 bit expression not in range 0..65535");
7271 }
7272 }
7273 else
7274 {
7275 int more;
7276 offsetT max;
7277
7278 /* The upper bound should be 0x8000, but
7279 unfortunately the MIPS assembler accepts numbers
7280 from 0x8000 to 0xffff and sign extends them, and
7281 we want to be compatible. We only permit this
7282 extended range for an instruction which does not
7283 provide any further alternates, since those
7284 alternates may handle other cases. People should
7285 use the numbers they mean, rather than relying on
7286 a mysterious sign extension. */
7287 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7288 strcmp (insn->name, insn[1].name) == 0);
7289 if (more)
7290 max = 0x8000;
7291 else
7292 max = 0x10000;
7293 if ((c == '\0' && imm_expr.X_op != O_constant)
7294 || ((imm_expr.X_add_number < -0x8000
7295 || imm_expr.X_add_number >= max)
7296 && imm_expr.X_op == O_constant)
7297 || (more
7298 && imm_expr.X_add_number < 0
7299 && mips_opts.isa >= 3
7300 && imm_expr.X_unsigned
7301 && sizeof (imm_expr.X_add_number) <= 4))
7302 {
7303 if (more)
7304 break;
7305 if (imm_expr.X_op != O_constant
7306 && imm_expr.X_op != O_big)
7307 insn_error = "absolute expression required";
7308 else
7309 as_bad ("16 bit expression not in range -32768..32767");
7310 }
7311 }
7312 s = expr_end;
7313 continue;
7314
7315 case 'o': /* 16 bit offset */
7316 c = my_getSmallExpression (&offset_expr, s);
7317
7318 /* If this value won't fit into a 16 bit offset, then go
7319 find a macro that will generate the 32 bit offset
7320 code pattern. As a special hack, we accept the
7321 difference of two local symbols as a constant. This
7322 is required to suppose embedded PIC switches, which
7323 use an instruction which looks like
7324 lw $4,$L12-$LS12($4)
7325 The problem with handling this in a more general
7326 fashion is that the macro function doesn't expect to
7327 see anything which can be handled in a single
7328 constant instruction. */
7329 if (c == 0
7330 && (offset_expr.X_op != O_constant
7331 || offset_expr.X_add_number >= 0x8000
7332 || offset_expr.X_add_number < -0x8000)
7333 && (mips_pic != EMBEDDED_PIC
7334 || offset_expr.X_op != O_subtract
7335 || now_seg != text_section
7336 || (S_GET_SEGMENT (offset_expr.X_op_symbol)
7337 != text_section)))
7338 break;
7339
7340 offset_reloc = BFD_RELOC_LO16;
7341 if (c == 'h' || c == 'H')
7342 {
7343 assert (offset_expr.X_op == O_constant);
7344 offset_expr.X_add_number =
7345 (offset_expr.X_add_number >> 16) & 0xffff;
7346 }
7347 s = expr_end;
7348 continue;
7349
7350 case 'p': /* pc relative offset */
7351 offset_reloc = BFD_RELOC_16_PCREL_S2;
7352 my_getExpression (&offset_expr, s);
7353 s = expr_end;
7354 continue;
7355
7356 case 'u': /* upper 16 bits */
7357 c = my_getSmallExpression (&imm_expr, s);
7358 if (imm_expr.X_op == O_constant
7359 && (imm_expr.X_add_number < 0
7360 || imm_expr.X_add_number >= 0x10000))
7361 as_bad ("lui expression not in range 0..65535");
7362 imm_reloc = BFD_RELOC_LO16;
7363 if (c)
7364 {
7365 if (c != 'l')
7366 {
7367 if (imm_expr.X_op == O_constant)
7368 imm_expr.X_add_number =
7369 (imm_expr.X_add_number >> 16) & 0xffff;
7370 else if (c == 'h')
7371 {
7372 imm_reloc = BFD_RELOC_HI16_S;
7373 imm_unmatched_hi = true;
7374 }
7375 else
7376 imm_reloc = BFD_RELOC_HI16;
7377 }
7378 }
7379 s = expr_end;
7380 continue;
7381
7382 case 'a': /* 26 bit address */
7383 my_getExpression (&offset_expr, s);
7384 s = expr_end;
7385 offset_reloc = BFD_RELOC_MIPS_JMP;
7386 continue;
7387
7388 case 'N': /* 3 bit branch condition code */
7389 case 'M': /* 3 bit compare condition code */
7390 if (strncmp (s, "$fcc", 4) != 0)
7391 break;
7392 s += 4;
7393 regno = 0;
7394 do
7395 {
7396 regno *= 10;
7397 regno += *s - '0';
7398 ++s;
7399 }
7400 while (isdigit (*s));
7401 if (regno > 7)
7402 as_bad ("invalid condition code register $fcc%d", regno);
7403 if (*args == 'N')
7404 ip->insn_opcode |= regno << OP_SH_BCC;
7405 else
7406 ip->insn_opcode |= regno << OP_SH_CCC;
7407 continue;
7408
7409 default:
7410 fprintf (stderr, "bad char = '%c'\n", *args);
7411 internalError ();
7412 }
7413 break;
7414 }
7415 /* Args don't match. */
7416 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7417 !strcmp (insn->name, insn[1].name))
7418 {
7419 ++insn;
7420 s = argsStart;
7421 continue;
7422 }
7423 insn_error = "illegal operands";
7424 return;
7425 }
7426 }
7427
7428 /* This routine assembles an instruction into its binary format when
7429 assembling for the mips16. As a side effect, it sets one of the
7430 global variables imm_reloc or offset_reloc to the type of
7431 relocation to do if one of the operands is an address expression.
7432 It also sets mips16_small and mips16_ext if the user explicitly
7433 requested a small or extended instruction. */
7434
7435 static void
7436 mips16_ip (str, ip)
7437 char *str;
7438 struct mips_cl_insn *ip;
7439 {
7440 char *s;
7441 const char *args;
7442 struct mips_opcode *insn;
7443 char *argsstart;
7444 unsigned int regno;
7445 unsigned int lastregno = 0;
7446 char *s_reset;
7447
7448 insn_error = NULL;
7449
7450 mips16_small = false;
7451 mips16_ext = false;
7452
7453 for (s = str; islower (*s); ++s)
7454 ;
7455 switch (*s)
7456 {
7457 case '\0':
7458 break;
7459
7460 case ' ':
7461 *s++ = '\0';
7462 break;
7463
7464 case '.':
7465 if (s[1] == 't' && s[2] == ' ')
7466 {
7467 *s = '\0';
7468 mips16_small = true;
7469 s += 3;
7470 break;
7471 }
7472 else if (s[1] == 'e' && s[2] == ' ')
7473 {
7474 *s = '\0';
7475 mips16_ext = true;
7476 s += 3;
7477 break;
7478 }
7479 /* Fall through. */
7480 default:
7481 insn_error = "unknown opcode";
7482 return;
7483 }
7484
7485 if (mips_opts.noautoextend && ! mips16_ext)
7486 mips16_small = true;
7487
7488 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
7489 {
7490 insn_error = "unrecognized opcode";
7491 return;
7492 }
7493
7494 argsstart = s;
7495 for (;;)
7496 {
7497 assert (strcmp (insn->name, str) == 0);
7498
7499 ip->insn_mo = insn;
7500 ip->insn_opcode = insn->match;
7501 ip->use_extend = false;
7502 imm_expr.X_op = O_absent;
7503 imm_reloc = BFD_RELOC_UNUSED;
7504 offset_expr.X_op = O_absent;
7505 offset_reloc = BFD_RELOC_UNUSED;
7506 for (args = insn->args; 1; ++args)
7507 {
7508 int c;
7509
7510 if (*s == ' ')
7511 ++s;
7512
7513 /* In this switch statement we call break if we did not find
7514 a match, continue if we did find a match, or return if we
7515 are done. */
7516
7517 c = *args;
7518 switch (c)
7519 {
7520 case '\0':
7521 if (*s == '\0')
7522 {
7523 /* Stuff the immediate value in now, if we can. */
7524 if (imm_expr.X_op == O_constant
7525 && imm_reloc > BFD_RELOC_UNUSED
7526 && insn->pinfo != INSN_MACRO)
7527 {
7528 mips16_immed ((char *) NULL, 0,
7529 imm_reloc - BFD_RELOC_UNUSED,
7530 imm_expr.X_add_number, true, mips16_small,
7531 mips16_ext, &ip->insn_opcode,
7532 &ip->use_extend, &ip->extend);
7533 imm_expr.X_op = O_absent;
7534 imm_reloc = BFD_RELOC_UNUSED;
7535 }
7536
7537 return;
7538 }
7539 break;
7540
7541 case ',':
7542 if (*s++ == c)
7543 continue;
7544 s--;
7545 switch (*++args)
7546 {
7547 case 'v':
7548 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
7549 continue;
7550 case 'w':
7551 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
7552 continue;
7553 }
7554 break;
7555
7556 case '(':
7557 case ')':
7558 if (*s++ == c)
7559 continue;
7560 break;
7561
7562 case 'v':
7563 case 'w':
7564 if (s[0] != '$')
7565 {
7566 if (c == 'v')
7567 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
7568 else
7569 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
7570 ++args;
7571 continue;
7572 }
7573 /* Fall through. */
7574 case 'x':
7575 case 'y':
7576 case 'z':
7577 case 'Z':
7578 case '0':
7579 case 'S':
7580 case 'R':
7581 case 'X':
7582 case 'Y':
7583 if (s[0] != '$')
7584 break;
7585 s_reset = s;
7586 if (isdigit (s[1]))
7587 {
7588 ++s;
7589 regno = 0;
7590 do
7591 {
7592 regno *= 10;
7593 regno += *s - '0';
7594 ++s;
7595 }
7596 while (isdigit (*s));
7597 if (regno > 31)
7598 {
7599 as_bad ("invalid register number (%d)", regno);
7600 regno = 2;
7601 }
7602 }
7603 else
7604 {
7605 if (s[1] == 'f' && s[2] == 'p')
7606 {
7607 s += 3;
7608 regno = FP;
7609 }
7610 else if (s[1] == 's' && s[2] == 'p')
7611 {
7612 s += 3;
7613 regno = SP;
7614 }
7615 else if (s[1] == 'g' && s[2] == 'p')
7616 {
7617 s += 3;
7618 regno = GP;
7619 }
7620 else if (s[1] == 'a' && s[2] == 't')
7621 {
7622 s += 3;
7623 regno = AT;
7624 }
7625 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7626 {
7627 s += 4;
7628 regno = KT0;
7629 }
7630 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7631 {
7632 s += 4;
7633 regno = KT1;
7634 }
7635 else
7636 break;
7637 }
7638
7639 if (*s == ' ')
7640 ++s;
7641 if (args[1] != *s)
7642 {
7643 if (c == 'v' || c == 'w')
7644 {
7645 regno = mips16_to_32_reg_map[lastregno];
7646 s = s_reset;
7647 args++;
7648 }
7649 }
7650
7651 switch (c)
7652 {
7653 case 'x':
7654 case 'y':
7655 case 'z':
7656 case 'v':
7657 case 'w':
7658 case 'Z':
7659 regno = mips32_to_16_reg_map[regno];
7660 break;
7661
7662 case '0':
7663 if (regno != 0)
7664 regno = ILLEGAL_REG;
7665 break;
7666
7667 case 'S':
7668 if (regno != SP)
7669 regno = ILLEGAL_REG;
7670 break;
7671
7672 case 'R':
7673 if (regno != RA)
7674 regno = ILLEGAL_REG;
7675 break;
7676
7677 case 'X':
7678 case 'Y':
7679 if (regno == AT && ! mips_opts.noat)
7680 as_warn ("used $at without \".set noat\"");
7681 break;
7682
7683 default:
7684 internalError ();
7685 }
7686
7687 if (regno == ILLEGAL_REG)
7688 break;
7689
7690 switch (c)
7691 {
7692 case 'x':
7693 case 'v':
7694 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
7695 break;
7696 case 'y':
7697 case 'w':
7698 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
7699 break;
7700 case 'z':
7701 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
7702 break;
7703 case 'Z':
7704 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
7705 case '0':
7706 case 'S':
7707 case 'R':
7708 break;
7709 case 'X':
7710 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
7711 break;
7712 case 'Y':
7713 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
7714 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
7715 break;
7716 default:
7717 internalError ();
7718 }
7719
7720 lastregno = regno;
7721 continue;
7722
7723 case 'P':
7724 if (strncmp (s, "$pc", 3) == 0)
7725 {
7726 s += 3;
7727 continue;
7728 }
7729 break;
7730
7731 case '<':
7732 case '>':
7733 case '[':
7734 case ']':
7735 case '4':
7736 case '5':
7737 case 'H':
7738 case 'W':
7739 case 'D':
7740 case 'j':
7741 case '8':
7742 case 'V':
7743 case 'C':
7744 case 'U':
7745 case 'k':
7746 case 'K':
7747 if (s[0] == '%'
7748 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
7749 {
7750 /* This is %gprel(SYMBOL). We need to read SYMBOL,
7751 and generate the appropriate reloc. If the text
7752 inside %gprel is not a symbol name with an
7753 optional offset, then we generate a normal reloc
7754 and will probably fail later. */
7755 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
7756 if (imm_expr.X_op == O_symbol)
7757 {
7758 mips16_ext = true;
7759 imm_reloc = BFD_RELOC_MIPS16_GPREL;
7760 s = expr_end;
7761 ip->use_extend = true;
7762 ip->extend = 0;
7763 continue;
7764 }
7765 }
7766 else
7767 {
7768 /* Just pick up a normal expression. */
7769 my_getExpression (&imm_expr, s);
7770 }
7771
7772 if (imm_expr.X_op == O_register)
7773 {
7774 /* What we thought was an expression turned out to
7775 be a register. */
7776
7777 if (s[0] == '(' && args[1] == '(')
7778 {
7779 /* It looks like the expression was omitted
7780 before a register indirection, which means
7781 that the expression is implicitly zero. We
7782 still set up imm_expr, so that we handle
7783 explicit extensions correctly. */
7784 imm_expr.X_op = O_constant;
7785 imm_expr.X_add_number = 0;
7786 imm_reloc = (int) BFD_RELOC_UNUSED + c;
7787 continue;
7788 }
7789
7790 break;
7791 }
7792
7793 /* We need to relax this instruction. */
7794 imm_reloc = (int) BFD_RELOC_UNUSED + c;
7795 s = expr_end;
7796 continue;
7797
7798 case 'p':
7799 case 'q':
7800 case 'A':
7801 case 'B':
7802 case 'E':
7803 /* We use offset_reloc rather than imm_reloc for the PC
7804 relative operands. This lets macros with both
7805 immediate and address operands work correctly. */
7806 my_getExpression (&offset_expr, s);
7807
7808 if (offset_expr.X_op == O_register)
7809 break;
7810
7811 /* We need to relax this instruction. */
7812 offset_reloc = (int) BFD_RELOC_UNUSED + c;
7813 s = expr_end;
7814 continue;
7815
7816 case '6': /* break code */
7817 my_getExpression (&imm_expr, s);
7818 check_absolute_expr (ip, &imm_expr);
7819 if ((unsigned long) imm_expr.X_add_number > 63)
7820 {
7821 as_warn ("Invalid value for `%s' (%lu)",
7822 ip->insn_mo->name,
7823 (unsigned long) imm_expr.X_add_number);
7824 imm_expr.X_add_number &= 0x3f;
7825 }
7826 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
7827 imm_expr.X_op = O_absent;
7828 s = expr_end;
7829 continue;
7830
7831 case 'a': /* 26 bit address */
7832 my_getExpression (&offset_expr, s);
7833 s = expr_end;
7834 offset_reloc = BFD_RELOC_MIPS16_JMP;
7835 ip->insn_opcode <<= 16;
7836 continue;
7837
7838 case 'l': /* register list for entry macro */
7839 case 'L': /* register list for exit macro */
7840 {
7841 int mask;
7842
7843 if (c == 'l')
7844 mask = 0;
7845 else
7846 mask = 7 << 3;
7847 while (*s != '\0')
7848 {
7849 int freg, reg1, reg2;
7850
7851 while (*s == ' ' || *s == ',')
7852 ++s;
7853 if (*s != '$')
7854 {
7855 as_bad ("can't parse register list");
7856 break;
7857 }
7858 ++s;
7859 if (*s != 'f')
7860 freg = 0;
7861 else
7862 {
7863 freg = 1;
7864 ++s;
7865 }
7866 reg1 = 0;
7867 while (isdigit (*s))
7868 {
7869 reg1 *= 10;
7870 reg1 += *s - '0';
7871 ++s;
7872 }
7873 if (*s == ' ')
7874 ++s;
7875 if (*s != '-')
7876 reg2 = reg1;
7877 else
7878 {
7879 ++s;
7880 if (*s != '$')
7881 break;
7882 ++s;
7883 if (freg)
7884 {
7885 if (*s == 'f')
7886 ++s;
7887 else
7888 {
7889 as_bad ("invalid register list");
7890 break;
7891 }
7892 }
7893 reg2 = 0;
7894 while (isdigit (*s))
7895 {
7896 reg2 *= 10;
7897 reg2 += *s - '0';
7898 ++s;
7899 }
7900 }
7901 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
7902 {
7903 mask &= ~ (7 << 3);
7904 mask |= 5 << 3;
7905 }
7906 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
7907 {
7908 mask &= ~ (7 << 3);
7909 mask |= 6 << 3;
7910 }
7911 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
7912 mask |= (reg2 - 3) << 3;
7913 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
7914 mask |= (reg2 - 15) << 1;
7915 else if (reg1 == 31 && reg2 == 31)
7916 mask |= 1;
7917 else
7918 {
7919 as_bad ("invalid register list");
7920 break;
7921 }
7922 }
7923 /* The mask is filled in in the opcode table for the
7924 benefit of the disassembler. We remove it before
7925 applying the actual mask. */
7926 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
7927 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
7928 }
7929 continue;
7930
7931 case 'e': /* extend code */
7932 my_getExpression (&imm_expr, s);
7933 check_absolute_expr (ip, &imm_expr);
7934 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
7935 {
7936 as_warn ("Invalid value for `%s' (%lu)",
7937 ip->insn_mo->name,
7938 (unsigned long) imm_expr.X_add_number);
7939 imm_expr.X_add_number &= 0x7ff;
7940 }
7941 ip->insn_opcode |= imm_expr.X_add_number;
7942 imm_expr.X_op = O_absent;
7943 s = expr_end;
7944 continue;
7945
7946 default:
7947 internalError ();
7948 }
7949 break;
7950 }
7951
7952 /* Args don't match. */
7953 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
7954 strcmp (insn->name, insn[1].name) == 0)
7955 {
7956 ++insn;
7957 s = argsstart;
7958 continue;
7959 }
7960
7961 insn_error = "illegal operands";
7962
7963 return;
7964 }
7965 }
7966
7967 /* This structure holds information we know about a mips16 immediate
7968 argument type. */
7969
7970 struct mips16_immed_operand
7971 {
7972 /* The type code used in the argument string in the opcode table. */
7973 int type;
7974 /* The number of bits in the short form of the opcode. */
7975 int nbits;
7976 /* The number of bits in the extended form of the opcode. */
7977 int extbits;
7978 /* The amount by which the short form is shifted when it is used;
7979 for example, the sw instruction has a shift count of 2. */
7980 int shift;
7981 /* The amount by which the short form is shifted when it is stored
7982 into the instruction code. */
7983 int op_shift;
7984 /* Non-zero if the short form is unsigned. */
7985 int unsp;
7986 /* Non-zero if the extended form is unsigned. */
7987 int extu;
7988 /* Non-zero if the value is PC relative. */
7989 int pcrel;
7990 };
7991
7992 /* The mips16 immediate operand types. */
7993
7994 static const struct mips16_immed_operand mips16_immed_operands[] =
7995 {
7996 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
7997 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
7998 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
7999 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
8000 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
8001 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
8002 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
8003 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
8004 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
8005 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
8006 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
8007 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
8008 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
8009 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
8010 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
8011 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
8012 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
8013 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
8014 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
8015 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
8016 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
8017 };
8018
8019 #define MIPS16_NUM_IMMED \
8020 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
8021
8022 /* Handle a mips16 instruction with an immediate value. This or's the
8023 small immediate value into *INSN. It sets *USE_EXTEND to indicate
8024 whether an extended value is needed; if one is needed, it sets
8025 *EXTEND to the value. The argument type is TYPE. The value is VAL.
8026 If SMALL is true, an unextended opcode was explicitly requested.
8027 If EXT is true, an extended opcode was explicitly requested. If
8028 WARN is true, warn if EXT does not match reality. */
8029
8030 static void
8031 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
8032 extend)
8033 char *file;
8034 unsigned int line;
8035 int type;
8036 offsetT val;
8037 boolean warn;
8038 boolean small;
8039 boolean ext;
8040 unsigned long *insn;
8041 boolean *use_extend;
8042 unsigned short *extend;
8043 {
8044 register const struct mips16_immed_operand *op;
8045 int mintiny, maxtiny;
8046 boolean needext;
8047
8048 op = mips16_immed_operands;
8049 while (op->type != type)
8050 {
8051 ++op;
8052 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
8053 }
8054
8055 if (op->unsp)
8056 {
8057 if (type == '<' || type == '>' || type == '[' || type == ']')
8058 {
8059 mintiny = 1;
8060 maxtiny = 1 << op->nbits;
8061 }
8062 else
8063 {
8064 mintiny = 0;
8065 maxtiny = (1 << op->nbits) - 1;
8066 }
8067 }
8068 else
8069 {
8070 mintiny = - (1 << (op->nbits - 1));
8071 maxtiny = (1 << (op->nbits - 1)) - 1;
8072 }
8073
8074 /* Branch offsets have an implicit 0 in the lowest bit. */
8075 if (type == 'p' || type == 'q')
8076 val /= 2;
8077
8078 if ((val & ((1 << op->shift) - 1)) != 0
8079 || val < (mintiny << op->shift)
8080 || val > (maxtiny << op->shift))
8081 needext = true;
8082 else
8083 needext = false;
8084
8085 if (warn && ext && ! needext)
8086 as_warn_where (file, line, "extended operand requested but not required");
8087 if (small && needext)
8088 as_bad_where (file, line, "invalid unextended operand value");
8089
8090 if (small || (! ext && ! needext))
8091 {
8092 int insnval;
8093
8094 *use_extend = false;
8095 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
8096 insnval <<= op->op_shift;
8097 *insn |= insnval;
8098 }
8099 else
8100 {
8101 long minext, maxext;
8102 int extval;
8103
8104 if (op->extu)
8105 {
8106 minext = 0;
8107 maxext = (1 << op->extbits) - 1;
8108 }
8109 else
8110 {
8111 minext = - (1 << (op->extbits - 1));
8112 maxext = (1 << (op->extbits - 1)) - 1;
8113 }
8114 if (val < minext || val > maxext)
8115 as_bad_where (file, line,
8116 "operand value out of range for instruction");
8117
8118 *use_extend = true;
8119 if (op->extbits == 16)
8120 {
8121 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
8122 val &= 0x1f;
8123 }
8124 else if (op->extbits == 15)
8125 {
8126 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
8127 val &= 0xf;
8128 }
8129 else
8130 {
8131 extval = ((val & 0x1f) << 6) | (val & 0x20);
8132 val = 0;
8133 }
8134
8135 *extend = (unsigned short) extval;
8136 *insn |= val;
8137 }
8138 }
8139 \f
8140 #define LP '('
8141 #define RP ')'
8142
8143 static int
8144 my_getSmallExpression (ep, str)
8145 expressionS *ep;
8146 char *str;
8147 {
8148 char *sp;
8149 int c = 0;
8150
8151 if (*str == ' ')
8152 str++;
8153 if (*str == LP
8154 || (*str == '%' &&
8155 ((str[1] == 'h' && str[2] == 'i')
8156 || (str[1] == 'H' && str[2] == 'I')
8157 || (str[1] == 'l' && str[2] == 'o'))
8158 && str[3] == LP))
8159 {
8160 if (*str == LP)
8161 c = 0;
8162 else
8163 {
8164 c = str[1];
8165 str += 3;
8166 }
8167
8168 /*
8169 * A small expression may be followed by a base register.
8170 * Scan to the end of this operand, and then back over a possible
8171 * base register. Then scan the small expression up to that
8172 * point. (Based on code in sparc.c...)
8173 */
8174 for (sp = str; *sp && *sp != ','; sp++)
8175 ;
8176 if (sp - 4 >= str && sp[-1] == RP)
8177 {
8178 if (isdigit (sp[-2]))
8179 {
8180 for (sp -= 3; sp >= str && isdigit (*sp); sp--)
8181 ;
8182 if (*sp == '$' && sp > str && sp[-1] == LP)
8183 {
8184 sp--;
8185 goto do_it;
8186 }
8187 }
8188 else if (sp - 5 >= str
8189 && sp[-5] == LP
8190 && sp[-4] == '$'
8191 && ((sp[-3] == 'f' && sp[-2] == 'p')
8192 || (sp[-3] == 's' && sp[-2] == 'p')
8193 || (sp[-3] == 'g' && sp[-2] == 'p')
8194 || (sp[-3] == 'a' && sp[-2] == 't')))
8195 {
8196 sp -= 5;
8197 do_it:
8198 if (sp == str)
8199 {
8200 /* no expression means zero offset */
8201 if (c)
8202 {
8203 /* %xx(reg) is an error */
8204 ep->X_op = O_absent;
8205 expr_end = str - 3;
8206 }
8207 else
8208 {
8209 ep->X_op = O_constant;
8210 expr_end = sp;
8211 }
8212 ep->X_add_symbol = NULL;
8213 ep->X_op_symbol = NULL;
8214 ep->X_add_number = 0;
8215 }
8216 else
8217 {
8218 *sp = '\0';
8219 my_getExpression (ep, str);
8220 *sp = LP;
8221 }
8222 return c;
8223 }
8224 }
8225 }
8226 my_getExpression (ep, str);
8227 return c; /* => %hi or %lo encountered */
8228 }
8229
8230 static void
8231 my_getExpression (ep, str)
8232 expressionS *ep;
8233 char *str;
8234 {
8235 char *save_in;
8236
8237 save_in = input_line_pointer;
8238 input_line_pointer = str;
8239 expression (ep);
8240 expr_end = input_line_pointer;
8241 input_line_pointer = save_in;
8242
8243 /* If we are in mips16 mode, and this is an expression based on `.',
8244 then we bump the value of the symbol by 1 since that is how other
8245 text symbols are handled. We don't bother to handle complex
8246 expressions, just `.' plus or minus a constant. */
8247 if (mips_opts.mips16
8248 && ep->X_op == O_symbol
8249 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
8250 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
8251 && ep->X_add_symbol->sy_frag == frag_now
8252 && ep->X_add_symbol->sy_value.X_op == O_constant
8253 && ep->X_add_symbol->sy_value.X_add_number == frag_now_fix ())
8254 ++ep->X_add_symbol->sy_value.X_add_number;
8255 }
8256
8257 /* Turn a string in input_line_pointer into a floating point constant
8258 of type type, and store the appropriate bytes in *litP. The number
8259 of LITTLENUMS emitted is stored in *sizeP . An error message is
8260 returned, or NULL on OK. */
8261
8262 char *
8263 md_atof (type, litP, sizeP)
8264 int type;
8265 char *litP;
8266 int *sizeP;
8267 {
8268 int prec;
8269 LITTLENUM_TYPE words[4];
8270 char *t;
8271 int i;
8272
8273 switch (type)
8274 {
8275 case 'f':
8276 prec = 2;
8277 break;
8278
8279 case 'd':
8280 prec = 4;
8281 break;
8282
8283 default:
8284 *sizeP = 0;
8285 return "bad call to md_atof";
8286 }
8287
8288 t = atof_ieee (input_line_pointer, type, words);
8289 if (t)
8290 input_line_pointer = t;
8291
8292 *sizeP = prec * 2;
8293
8294 if (! target_big_endian)
8295 {
8296 for (i = prec - 1; i >= 0; i--)
8297 {
8298 md_number_to_chars (litP, (valueT) words[i], 2);
8299 litP += 2;
8300 }
8301 }
8302 else
8303 {
8304 for (i = 0; i < prec; i++)
8305 {
8306 md_number_to_chars (litP, (valueT) words[i], 2);
8307 litP += 2;
8308 }
8309 }
8310
8311 return NULL;
8312 }
8313
8314 void
8315 md_number_to_chars (buf, val, n)
8316 char *buf;
8317 valueT val;
8318 int n;
8319 {
8320 if (target_big_endian)
8321 number_to_chars_bigendian (buf, val, n);
8322 else
8323 number_to_chars_littleendian (buf, val, n);
8324 }
8325 \f
8326 CONST char *md_shortopts = "O::g::G:";
8327
8328 struct option md_longopts[] = {
8329 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
8330 {"mips0", no_argument, NULL, OPTION_MIPS1},
8331 {"mips1", no_argument, NULL, OPTION_MIPS1},
8332 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
8333 {"mips2", no_argument, NULL, OPTION_MIPS2},
8334 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
8335 {"mips3", no_argument, NULL, OPTION_MIPS3},
8336 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
8337 {"mips4", no_argument, NULL, OPTION_MIPS4},
8338 #define OPTION_MCPU (OPTION_MD_BASE + 5)
8339 {"mcpu", required_argument, NULL, OPTION_MCPU},
8340 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 6)
8341 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
8342 #define OPTION_TRAP (OPTION_MD_BASE + 9)
8343 {"trap", no_argument, NULL, OPTION_TRAP},
8344 {"no-break", no_argument, NULL, OPTION_TRAP},
8345 #define OPTION_BREAK (OPTION_MD_BASE + 10)
8346 {"break", no_argument, NULL, OPTION_BREAK},
8347 {"no-trap", no_argument, NULL, OPTION_BREAK},
8348 #define OPTION_EB (OPTION_MD_BASE + 11)
8349 {"EB", no_argument, NULL, OPTION_EB},
8350 #define OPTION_EL (OPTION_MD_BASE + 12)
8351 {"EL", no_argument, NULL, OPTION_EL},
8352 #define OPTION_M4650 (OPTION_MD_BASE + 13)
8353 {"m4650", no_argument, NULL, OPTION_M4650},
8354 #define OPTION_NO_M4650 (OPTION_MD_BASE + 14)
8355 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
8356 #define OPTION_M4010 (OPTION_MD_BASE + 15)
8357 {"m4010", no_argument, NULL, OPTION_M4010},
8358 #define OPTION_NO_M4010 (OPTION_MD_BASE + 16)
8359 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
8360 #define OPTION_M4100 (OPTION_MD_BASE + 17)
8361 {"m4100", no_argument, NULL, OPTION_M4100},
8362 #define OPTION_NO_M4100 (OPTION_MD_BASE + 18)
8363 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
8364 #define OPTION_MIPS16 (OPTION_MD_BASE + 22)
8365 {"mips16", no_argument, NULL, OPTION_MIPS16},
8366 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 23)
8367 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
8368 /* start-sanitize-r5900 */
8369 #define OPTION_M5900 (OPTION_MD_BASE + 24)
8370 {"m5900", no_argument, NULL, OPTION_M5900},
8371 #define OPTION_NO_M5900 (OPTION_MD_BASE + 25)
8372 {"no-m5900", no_argument, NULL, OPTION_NO_M5900},
8373 /* end-sanitize-r5900 */
8374 #define OPTION_M3900 (OPTION_MD_BASE + 26)
8375 {"m3900", no_argument, NULL, OPTION_M3900},
8376 #define OPTION_NO_M3900 (OPTION_MD_BASE + 27)
8377 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
8378
8379 /* start-sanitize-tx19 */
8380 {"m1900", no_argument, NULL, OPTION_M3900},
8381 {"no-m1900", no_argument, NULL, OPTION_NO_M3900},
8382 /* end-sanitize-tx19 */
8383
8384 #define OPTION_CALL_SHARED (OPTION_MD_BASE + 7)
8385 #define OPTION_NON_SHARED (OPTION_MD_BASE + 8)
8386 #define OPTION_XGOT (OPTION_MD_BASE + 19)
8387 #define OPTION_32 (OPTION_MD_BASE + 20)
8388 #define OPTION_64 (OPTION_MD_BASE + 21)
8389 #ifdef OBJ_ELF
8390 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
8391 {"xgot", no_argument, NULL, OPTION_XGOT},
8392 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
8393 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
8394 {"32", no_argument, NULL, OPTION_32},
8395 {"64", no_argument, NULL, OPTION_64},
8396 #endif
8397
8398 {NULL, no_argument, NULL, 0}
8399 };
8400 size_t md_longopts_size = sizeof(md_longopts);
8401
8402 int
8403 md_parse_option (c, arg)
8404 int c;
8405 char *arg;
8406 {
8407 switch (c)
8408 {
8409 case OPTION_TRAP:
8410 mips_trap = 1;
8411 break;
8412
8413 case OPTION_BREAK:
8414 mips_trap = 0;
8415 break;
8416
8417 case OPTION_EB:
8418 target_big_endian = 1;
8419 break;
8420
8421 case OPTION_EL:
8422 target_big_endian = 0;
8423 break;
8424
8425 case 'O':
8426 if (arg && arg[1] == '0')
8427 mips_optimize = 1;
8428 else
8429 mips_optimize = 2;
8430 break;
8431
8432 case 'g':
8433 if (arg == NULL)
8434 mips_debug = 2;
8435 else
8436 mips_debug = atoi (arg);
8437 /* When the MIPS assembler sees -g or -g2, it does not do
8438 optimizations which limit full symbolic debugging. We take
8439 that to be equivalent to -O0. */
8440 if (mips_debug == 2)
8441 mips_optimize = 1;
8442 break;
8443
8444 case OPTION_MIPS1:
8445 mips_opts.isa = 1;
8446 if (mips_cpu == -1)
8447 mips_cpu = 3000;
8448 break;
8449
8450 case OPTION_MIPS2:
8451 mips_opts.isa = 2;
8452 if (mips_cpu == -1)
8453 mips_cpu = 6000;
8454 break;
8455
8456 case OPTION_MIPS3:
8457 mips_opts.isa = 3;
8458 if (mips_cpu == -1)
8459 mips_cpu = 4000;
8460 break;
8461
8462 case OPTION_MIPS4:
8463 mips_opts.isa = 4;
8464 if (mips_cpu == -1)
8465 mips_cpu = 8000;
8466 break;
8467
8468 case OPTION_MCPU:
8469 {
8470 char *p;
8471
8472 /* Identify the processor type */
8473 p = arg;
8474 if (strcmp (p, "default") == 0
8475 || strcmp (p, "DEFAULT") == 0)
8476 mips_cpu = -1;
8477 else
8478 {
8479 int sv = 0;
8480
8481 /* We need to cope with the various "vr" prefixes for the 4300
8482 processor. */
8483 if (*p == 'v' || *p == 'V')
8484 {
8485 sv = 1;
8486 p++;
8487 }
8488
8489 if (*p == 'r' || *p == 'R')
8490 p++;
8491
8492 mips_cpu = -1;
8493 switch (*p)
8494 {
8495 case '1':
8496 if (strcmp (p, "10000") == 0
8497 || strcmp (p, "10k") == 0
8498 || strcmp (p, "10K") == 0)
8499 mips_cpu = 10000;
8500 /* start-sanitize-tx19 */
8501 else if (strcmp (p, "1900") == 0)
8502 mips_cpu = 3900;
8503 /* end-sanitize-tx19 */
8504 break;
8505
8506 case '2':
8507 if (strcmp (p, "2000") == 0
8508 || strcmp (p, "2k") == 0
8509 || strcmp (p, "2K") == 0)
8510 mips_cpu = 2000;
8511 break;
8512
8513 case '3':
8514 if (strcmp (p, "3000") == 0
8515 || strcmp (p, "3k") == 0
8516 || strcmp (p, "3K") == 0)
8517 mips_cpu = 3000;
8518 else if (strcmp (p, "3900") == 0)
8519 mips_cpu = 3900;
8520 break;
8521
8522 case '4':
8523 if (strcmp (p, "4000") == 0
8524 || strcmp (p, "4k") == 0
8525 || strcmp (p, "4K") == 0)
8526 mips_cpu = 4000;
8527 else if (strcmp (p, "4100") == 0)
8528 {
8529 mips_cpu = 4100;
8530 if (mips_4100 < 0)
8531 mips_4100 = 1;
8532 }
8533 else if (strcmp (p, "4300") == 0)
8534 mips_cpu = 4300;
8535 else if (strcmp (p, "4400") == 0)
8536 mips_cpu = 4400;
8537 else if (strcmp (p, "4600") == 0)
8538 mips_cpu = 4600;
8539 else if (strcmp (p, "4650") == 0)
8540 {
8541 mips_cpu = 4650;
8542 if (mips_4650 < 0)
8543 mips_4650 = 1;
8544 }
8545 else if (strcmp (p, "4010") == 0)
8546 {
8547 mips_cpu = 4010;
8548 if (mips_4010 < 0)
8549 mips_4010 = 1;
8550 }
8551 break;
8552
8553 case '5':
8554 if (strcmp (p, "5000") == 0
8555 || strcmp (p, "5k") == 0
8556 || strcmp (p, "5K") == 0)
8557 mips_cpu = 5000;
8558 /* start-sanitize-r5900 */
8559 else if (strcmp (p, "5900") == 0)
8560 mips_cpu = 5900;
8561 /* end-sanitize-r5900 */
8562 break;
8563
8564 case '6':
8565 if (strcmp (p, "6000") == 0
8566 || strcmp (p, "6k") == 0
8567 || strcmp (p, "6K") == 0)
8568 mips_cpu = 6000;
8569 break;
8570
8571 case '8':
8572 if (strcmp (p, "8000") == 0
8573 || strcmp (p, "8k") == 0
8574 || strcmp (p, "8K") == 0)
8575 mips_cpu = 8000;
8576 break;
8577
8578 case 'o':
8579 if (strcmp (p, "orion") == 0)
8580 mips_cpu = 4600;
8581 break;
8582 }
8583
8584 if (sv && mips_cpu != 4300 && mips_cpu != 4100 && mips_cpu != 5000)
8585 {
8586 as_bad ("ignoring invalid leading 'v' in -mcpu=%s switch", arg);
8587 return 0;
8588 }
8589
8590 if (mips_cpu == -1)
8591 {
8592 as_bad ("invalid architecture -mcpu=%s", arg);
8593 return 0;
8594 }
8595 }
8596 }
8597 break;
8598
8599 case OPTION_M4650:
8600 mips_4650 = 1;
8601 break;
8602
8603 case OPTION_NO_M4650:
8604 mips_4650 = 0;
8605 break;
8606
8607 case OPTION_M4010:
8608 mips_4010 = 1;
8609 break;
8610
8611 case OPTION_NO_M4010:
8612 mips_4010 = 0;
8613 break;
8614
8615 case OPTION_M4100:
8616 mips_4100 = 1;
8617 break;
8618
8619 case OPTION_NO_M4100:
8620 mips_4100 = 0;
8621 break;
8622
8623 /* start-sanitize-r5900 */
8624 case OPTION_M5900:
8625 mips_5900 = 1;
8626 break;
8627
8628 case OPTION_NO_M5900:
8629 mips_5900 = 0;
8630 break;
8631 /* end-sanitize-r5900 */
8632
8633 case OPTION_M3900:
8634 mips_3900 = 1;
8635 break;
8636
8637 case OPTION_NO_M3900:
8638 mips_3900 = 0;
8639 break;
8640
8641 case OPTION_MIPS16:
8642 mips_opts.mips16 = 1;
8643 mips_no_prev_insn (false);
8644 break;
8645
8646 case OPTION_NO_MIPS16:
8647 mips_opts.mips16 = 0;
8648 mips_no_prev_insn (false);
8649 break;
8650
8651 case OPTION_MEMBEDDED_PIC:
8652 mips_pic = EMBEDDED_PIC;
8653 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
8654 {
8655 as_bad ("-G may not be used with embedded PIC code");
8656 return 0;
8657 }
8658 g_switch_value = 0x7fffffff;
8659 break;
8660
8661 /* When generating ELF code, we permit -KPIC and -call_shared to
8662 select SVR4_PIC, and -non_shared to select no PIC. This is
8663 intended to be compatible with Irix 5. */
8664 case OPTION_CALL_SHARED:
8665 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
8666 {
8667 as_bad ("-call_shared is supported only for ELF format");
8668 return 0;
8669 }
8670 mips_pic = SVR4_PIC;
8671 if (g_switch_seen && g_switch_value != 0)
8672 {
8673 as_bad ("-G may not be used with SVR4 PIC code");
8674 return 0;
8675 }
8676 g_switch_value = 0;
8677 break;
8678
8679 case OPTION_NON_SHARED:
8680 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
8681 {
8682 as_bad ("-non_shared is supported only for ELF format");
8683 return 0;
8684 }
8685 mips_pic = NO_PIC;
8686 break;
8687
8688 /* The -xgot option tells the assembler to use 32 offsets when
8689 accessing the got in SVR4_PIC mode. It is for Irix
8690 compatibility. */
8691 case OPTION_XGOT:
8692 mips_big_got = 1;
8693 break;
8694
8695 case 'G':
8696 if (! USE_GLOBAL_POINTER_OPT)
8697 {
8698 as_bad ("-G is not supported for this configuration");
8699 return 0;
8700 }
8701 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
8702 {
8703 as_bad ("-G may not be used with SVR4 or embedded PIC code");
8704 return 0;
8705 }
8706 else
8707 g_switch_value = atoi (arg);
8708 g_switch_seen = 1;
8709 break;
8710
8711 /* The -32 and -64 options tell the assembler to output the 32
8712 bit or the 64 bit MIPS ELF format. */
8713 case OPTION_32:
8714 mips_64 = 0;
8715 break;
8716
8717 case OPTION_64:
8718 {
8719 const char **list, **l;
8720
8721 list = bfd_target_list ();
8722 for (l = list; *l != NULL; l++)
8723 if (strcmp (*l, "elf64-bigmips") == 0
8724 || strcmp (*l, "elf64-littlemips") == 0)
8725 break;
8726 if (*l == NULL)
8727 as_fatal ("No compiled in support for 64 bit object file format");
8728 free (list);
8729 mips_64 = 1;
8730 }
8731 break;
8732
8733 default:
8734 return 0;
8735 }
8736
8737 return 1;
8738 }
8739
8740 void
8741 md_show_usage (stream)
8742 FILE *stream;
8743 {
8744 fprintf(stream, "\
8745 MIPS options:\n\
8746 -membedded-pic generate embedded position independent code\n\
8747 -EB generate big endian output\n\
8748 -EL generate little endian output\n\
8749 -g, -g2 do not remove uneeded NOPs or swap branches\n\
8750 -G NUM allow referencing objects up to NUM bytes\n\
8751 implicitly with the gp register [default 8]\n");
8752 fprintf(stream, "\
8753 -mips1, -mcpu=r{2,3}000 generate code for r2000 and r3000\n\
8754 -mips2, -mcpu=r6000 generate code for r6000\n\
8755 -mips3, -mcpu=r4000 generate code for r4000\n\
8756 -mips4, -mcpu=r8000 generate code for r8000\n\
8757 -mcpu=vr4300 generate code for vr4300\n\
8758 -mcpu=vr4100 generate code for vr4100\n\
8759 -m4650 permit R4650 instructions\n\
8760 -no-m4650 do not permit R4650 instructions\n\
8761 -m4010 permit R4010 instructions\n\
8762 -no-m4010 do not permit R4010 instructions\n\
8763 -m4100 permit VR4100 instructions\n\
8764 -no-m4100 do not permit VR4100 instructions\n");
8765 fprintf(stream, "\
8766 -mips16 generate mips16 instructions\n\
8767 -no-mips16 do not generate mips16 instructions\n");
8768 fprintf(stream, "\
8769 -O0 remove unneeded NOPs, do not swap branches\n\
8770 -O remove unneeded NOPs and swap branches\n\
8771 --trap, --no-break trap exception on div by 0 and mult overflow\n\
8772 --break, --no-trap break exception on div by 0 and mult overflow\n");
8773 #ifdef OBJ_ELF
8774 fprintf(stream, "\
8775 -KPIC, -call_shared generate SVR4 position independent code\n\
8776 -non_shared do not generate position independent code\n\
8777 -xgot assume a 32 bit GOT\n\
8778 -32 create 32 bit object file (default)\n\
8779 -64 create 64 bit object file\n");
8780 #endif
8781 }
8782 \f
8783 void
8784 mips_init_after_args ()
8785 {
8786 /* initialize opcodes */
8787 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
8788 mips_opcodes = (struct mips_opcode*) mips_builtin_opcodes;
8789 }
8790
8791 long
8792 md_pcrel_from (fixP)
8793 fixS *fixP;
8794 {
8795 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
8796 && fixP->fx_addsy != (symbolS *) NULL
8797 && ! S_IS_DEFINED (fixP->fx_addsy))
8798 {
8799 /* This makes a branch to an undefined symbol be a branch to the
8800 current location. */
8801 return 4;
8802 }
8803
8804 /* return the address of the delay slot */
8805 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
8806 }
8807
8808 /* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
8809 reloc for a cons. We could use the definition there, except that
8810 we want to handle 64 bit relocs specially. */
8811
8812 void
8813 cons_fix_new_mips (frag, where, nbytes, exp)
8814 fragS *frag;
8815 int where;
8816 unsigned int nbytes;
8817 expressionS *exp;
8818 {
8819 #ifndef OBJ_ELF
8820 /* If we are assembling in 32 bit mode, turn an 8 byte reloc into a
8821 4 byte reloc. */
8822 if (nbytes == 8 && ! mips_64)
8823 {
8824 if (target_big_endian)
8825 where += 4;
8826 nbytes = 4;
8827 }
8828 #endif
8829
8830 if (nbytes != 2 && nbytes != 4 && nbytes != 8)
8831 as_bad ("Unsupported reloc size %d", nbytes);
8832
8833 fix_new_exp (frag_now, where, (int) nbytes, exp, 0,
8834 (nbytes == 2
8835 ? BFD_RELOC_16
8836 : (nbytes == 4 ? BFD_RELOC_32 : BFD_RELOC_64)));
8837 }
8838
8839 /* This is called before the symbol table is processed. In order to
8840 work with gcc when using mips-tfile, we must keep all local labels.
8841 However, in other cases, we want to discard them. If we were
8842 called with -g, but we didn't see any debugging information, it may
8843 mean that gcc is smuggling debugging information through to
8844 mips-tfile, in which case we must generate all local labels. */
8845
8846 void
8847 mips_frob_file_before_adjust ()
8848 {
8849 #ifndef NO_ECOFF_DEBUGGING
8850 if (ECOFF_DEBUGGING
8851 && mips_debug != 0
8852 && ! ecoff_debugging_seen)
8853 flag_keep_locals = 1;
8854 #endif
8855 }
8856
8857 /* Sort any unmatched HI16_S relocs so that they immediately precede
8858 the corresponding LO reloc. This is called before md_apply_fix and
8859 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
8860 explicit use of the %hi modifier. */
8861
8862 void
8863 mips_frob_file ()
8864 {
8865 struct mips_hi_fixup *l;
8866
8867 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
8868 {
8869 segment_info_type *seginfo;
8870 int pass;
8871
8872 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
8873
8874 /* Check quickly whether the next fixup happens to be a matching
8875 %lo. */
8876 if (l->fixp->fx_next != NULL
8877 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
8878 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
8879 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
8880 continue;
8881
8882 /* Look through the fixups for this segment for a matching %lo.
8883 When we find one, move the %hi just in front of it. We do
8884 this in two passes. In the first pass, we try to find a
8885 unique %lo. In the second pass, we permit multiple %hi
8886 relocs for a single %lo (this is a GNU extension). */
8887 seginfo = seg_info (l->seg);
8888 for (pass = 0; pass < 2; pass++)
8889 {
8890 fixS *f, *prev;
8891
8892 prev = NULL;
8893 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
8894 {
8895 /* Check whether this is a %lo fixup which matches l->fixp. */
8896 if (f->fx_r_type == BFD_RELOC_LO16
8897 && f->fx_addsy == l->fixp->fx_addsy
8898 && f->fx_offset == l->fixp->fx_offset
8899 && (pass == 1
8900 || prev == NULL
8901 || prev->fx_r_type != BFD_RELOC_HI16_S
8902 || prev->fx_addsy != f->fx_addsy
8903 || prev->fx_offset != f->fx_offset))
8904 {
8905 fixS **pf;
8906
8907 /* Move l->fixp before f. */
8908 for (pf = &seginfo->fix_root;
8909 *pf != l->fixp;
8910 pf = &(*pf)->fx_next)
8911 assert (*pf != NULL);
8912
8913 *pf = l->fixp->fx_next;
8914
8915 l->fixp->fx_next = f;
8916 if (prev == NULL)
8917 seginfo->fix_root = l->fixp;
8918 else
8919 prev->fx_next = l->fixp;
8920
8921 break;
8922 }
8923
8924 prev = f;
8925 }
8926
8927 if (f != NULL)
8928 break;
8929
8930 if (pass == 1)
8931 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
8932 "Unmatched %%hi reloc");
8933 }
8934 }
8935 }
8936
8937 /* When generating embedded PIC code we need to use a special
8938 relocation to represent the difference of two symbols in the .text
8939 section (switch tables use a difference of this sort). See
8940 include/coff/mips.h for details. This macro checks whether this
8941 fixup requires the special reloc. */
8942 #define SWITCH_TABLE(fixp) \
8943 ((fixp)->fx_r_type == BFD_RELOC_32 \
8944 && (fixp)->fx_addsy != NULL \
8945 && (fixp)->fx_subsy != NULL \
8946 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
8947 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
8948
8949 /* When generating embedded PIC code we must keep all PC relative
8950 relocations, in case the linker has to relax a call. We also need
8951 to keep relocations for switch table entries. */
8952
8953 /*ARGSUSED*/
8954 int
8955 mips_force_relocation (fixp)
8956 fixS *fixp;
8957 {
8958 return (mips_pic == EMBEDDED_PIC
8959 && (fixp->fx_pcrel
8960 || SWITCH_TABLE (fixp)
8961 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
8962 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
8963 }
8964
8965 /* Apply a fixup to the object file. */
8966
8967 int
8968 md_apply_fix (fixP, valueP)
8969 fixS *fixP;
8970 valueT *valueP;
8971 {
8972 unsigned char *buf;
8973 long insn, value;
8974
8975 assert (fixP->fx_size == 4
8976 || fixP->fx_r_type == BFD_RELOC_16
8977 || fixP->fx_r_type == BFD_RELOC_64);
8978
8979 value = *valueP;
8980
8981 /* If we aren't adjusting this fixup to be against the section
8982 symbol, we need to adjust the value. */
8983 #ifdef OBJ_ELF
8984 if (fixP->fx_addsy != NULL
8985 && OUTPUT_FLAVOR == bfd_target_elf_flavour
8986 && S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
8987 {
8988 value -= S_GET_VALUE (fixP->fx_addsy);
8989 if (value != 0 && ! fixP->fx_pcrel)
8990 {
8991 /* In this case, the bfd_install_relocation routine will
8992 incorrectly add the symbol value back in. We just want
8993 the addend to appear in the object file. */
8994 value -= S_GET_VALUE (fixP->fx_addsy);
8995 }
8996 }
8997 #endif
8998
8999 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc */
9000
9001 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
9002 fixP->fx_done = 1;
9003
9004 switch (fixP->fx_r_type)
9005 {
9006 case BFD_RELOC_MIPS_JMP:
9007 case BFD_RELOC_HI16:
9008 case BFD_RELOC_HI16_S:
9009 case BFD_RELOC_MIPS_GPREL:
9010 case BFD_RELOC_MIPS_LITERAL:
9011 case BFD_RELOC_MIPS_CALL16:
9012 case BFD_RELOC_MIPS_GOT16:
9013 case BFD_RELOC_MIPS_GPREL32:
9014 case BFD_RELOC_MIPS_GOT_HI16:
9015 case BFD_RELOC_MIPS_GOT_LO16:
9016 case BFD_RELOC_MIPS_CALL_HI16:
9017 case BFD_RELOC_MIPS_CALL_LO16:
9018 case BFD_RELOC_MIPS16_GPREL:
9019 if (fixP->fx_pcrel)
9020 as_bad_where (fixP->fx_file, fixP->fx_line,
9021 "Invalid PC relative reloc");
9022 /* Nothing needed to do. The value comes from the reloc entry */
9023 break;
9024
9025 case BFD_RELOC_MIPS16_JMP:
9026 /* We currently always generate a reloc against a symbol, which
9027 means that we don't want an addend even if the symbol is
9028 defined. */
9029 fixP->fx_addnumber = 0;
9030 break;
9031
9032 case BFD_RELOC_PCREL_HI16_S:
9033 /* The addend for this is tricky if it is internal, so we just
9034 do everything here rather than in bfd_install_relocation. */
9035 if ((fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) == 0)
9036 {
9037 /* For an external symbol adjust by the address to make it
9038 pcrel_offset. We use the address of the RELLO reloc
9039 which follows this one. */
9040 value += (fixP->fx_next->fx_frag->fr_address
9041 + fixP->fx_next->fx_where);
9042 }
9043 if (value & 0x8000)
9044 value += 0x10000;
9045 value >>= 16;
9046 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9047 if (target_big_endian)
9048 buf += 2;
9049 md_number_to_chars (buf, value, 2);
9050 break;
9051
9052 case BFD_RELOC_PCREL_LO16:
9053 /* The addend for this is tricky if it is internal, so we just
9054 do everything here rather than in bfd_install_relocation. */
9055 if ((fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) == 0)
9056 value += fixP->fx_frag->fr_address + fixP->fx_where;
9057 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9058 if (target_big_endian)
9059 buf += 2;
9060 md_number_to_chars (buf, value, 2);
9061 break;
9062
9063 case BFD_RELOC_64:
9064 /* This is handled like BFD_RELOC_32, but we output a sign
9065 extended value if we are only 32 bits. */
9066 if (fixP->fx_done
9067 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
9068 {
9069 if (8 <= sizeof (valueT))
9070 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9071 value, 8);
9072 else
9073 {
9074 long w1, w2;
9075 long hiv;
9076
9077 w1 = w2 = fixP->fx_where;
9078 if (target_big_endian)
9079 w1 += 4;
9080 else
9081 w2 += 4;
9082 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
9083 if ((value & 0x80000000) != 0)
9084 hiv = 0xffffffff;
9085 else
9086 hiv = 0;
9087 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
9088 }
9089 }
9090 break;
9091
9092 case BFD_RELOC_32:
9093 /* If we are deleting this reloc entry, we must fill in the
9094 value now. This can happen if we have a .word which is not
9095 resolved when it appears but is later defined. We also need
9096 to fill in the value if this is an embedded PIC switch table
9097 entry. */
9098 if (fixP->fx_done
9099 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
9100 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9101 value, 4);
9102 break;
9103
9104 case BFD_RELOC_16:
9105 /* If we are deleting this reloc entry, we must fill in the
9106 value now. */
9107 assert (fixP->fx_size == 2);
9108 if (fixP->fx_done)
9109 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9110 value, 2);
9111 break;
9112
9113 case BFD_RELOC_LO16:
9114 /* When handling an embedded PIC switch statement, we can wind
9115 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
9116 if (fixP->fx_done)
9117 {
9118 if (value < -0x8000 || value > 0x7fff)
9119 as_bad_where (fixP->fx_file, fixP->fx_line,
9120 "relocation overflow");
9121 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9122 if (target_big_endian)
9123 buf += 2;
9124 md_number_to_chars (buf, value, 2);
9125 }
9126 break;
9127
9128 case BFD_RELOC_16_PCREL_S2:
9129 /*
9130 * We need to save the bits in the instruction since fixup_segment()
9131 * might be deleting the relocation entry (i.e., a branch within
9132 * the current segment).
9133 */
9134 if ((value & 0x3) != 0)
9135 as_bad_where (fixP->fx_file, fixP->fx_line,
9136 "Branch to odd address (%lx)", value);
9137 value >>= 2;
9138
9139 /* update old instruction data */
9140 buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
9141 if (target_big_endian)
9142 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
9143 else
9144 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
9145
9146 if (value >= -0x8000 && value < 0x8000)
9147 insn |= value & 0xffff;
9148 else
9149 {
9150 /* The branch offset is too large. If this is an
9151 unconditional branch, and we are not generating PIC code,
9152 we can convert it to an absolute jump instruction. */
9153 if (mips_pic == NO_PIC
9154 && fixP->fx_done
9155 && fixP->fx_frag->fr_address >= text_section->vma
9156 && (fixP->fx_frag->fr_address
9157 < text_section->vma + text_section->_raw_size)
9158 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
9159 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
9160 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
9161 {
9162 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
9163 insn = 0x0c000000; /* jal */
9164 else
9165 insn = 0x08000000; /* j */
9166 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
9167 fixP->fx_done = 0;
9168 fixP->fx_addsy = section_symbol (text_section);
9169 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
9170 }
9171 else
9172 {
9173 /* FIXME. It would be possible in principle to handle
9174 conditional branches which overflow. They could be
9175 transformed into a branch around a jump. This would
9176 require setting up variant frags for each different
9177 branch type. The native MIPS assembler attempts to
9178 handle these cases, but it appears to do it
9179 incorrectly. */
9180 as_bad_where (fixP->fx_file, fixP->fx_line,
9181 "Branch out of range");
9182 }
9183 }
9184
9185 md_number_to_chars ((char *) buf, (valueT) insn, 4);
9186 break;
9187
9188 default:
9189 internalError ();
9190 }
9191
9192 return 1;
9193 }
9194
9195 #if 0
9196 void
9197 printInsn (oc)
9198 unsigned long oc;
9199 {
9200 const struct mips_opcode *p;
9201 int treg, sreg, dreg, shamt;
9202 short imm;
9203 const char *args;
9204 int i;
9205
9206 for (i = 0; i < NUMOPCODES; ++i)
9207 {
9208 p = &mips_opcodes[i];
9209 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
9210 {
9211 printf ("%08lx %s\t", oc, p->name);
9212 treg = (oc >> 16) & 0x1f;
9213 sreg = (oc >> 21) & 0x1f;
9214 dreg = (oc >> 11) & 0x1f;
9215 shamt = (oc >> 6) & 0x1f;
9216 imm = oc;
9217 for (args = p->args;; ++args)
9218 {
9219 switch (*args)
9220 {
9221 case '\0':
9222 printf ("\n");
9223 break;
9224
9225 case ',':
9226 case '(':
9227 case ')':
9228 printf ("%c", *args);
9229 continue;
9230
9231 case 'r':
9232 assert (treg == sreg);
9233 printf ("$%d,$%d", treg, sreg);
9234 continue;
9235
9236 case 'd':
9237 case 'G':
9238 printf ("$%d", dreg);
9239 continue;
9240
9241 case 't':
9242 case 'E':
9243 printf ("$%d", treg);
9244 continue;
9245
9246 case 'k':
9247 printf ("0x%x", treg);
9248 continue;
9249
9250 case 'b':
9251 case 's':
9252 printf ("$%d", sreg);
9253 continue;
9254
9255 case 'a':
9256 printf ("0x%08lx", oc & 0x1ffffff);
9257 continue;
9258
9259 case 'i':
9260 case 'j':
9261 case 'o':
9262 case 'u':
9263 printf ("%d", imm);
9264 continue;
9265
9266 case '<':
9267 case '>':
9268 printf ("$%d", shamt);
9269 continue;
9270
9271 default:
9272 internalError ();
9273 }
9274 break;
9275 }
9276 return;
9277 }
9278 }
9279 printf ("%08lx UNDEFINED\n", oc);
9280 }
9281 #endif
9282
9283 static symbolS *
9284 get_symbol ()
9285 {
9286 int c;
9287 char *name;
9288 symbolS *p;
9289
9290 name = input_line_pointer;
9291 c = get_symbol_end ();
9292 p = (symbolS *) symbol_find_or_make (name);
9293 *input_line_pointer = c;
9294 return p;
9295 }
9296
9297 /* Align the current frag to a given power of two. The MIPS assembler
9298 also automatically adjusts any preceding label. */
9299
9300 static void
9301 mips_align (to, fill, label)
9302 int to;
9303 int fill;
9304 symbolS *label;
9305 {
9306 mips_emit_delays (false);
9307 frag_align (to, fill, 0);
9308 record_alignment (now_seg, to);
9309 if (label != NULL)
9310 {
9311 assert (S_GET_SEGMENT (label) == now_seg);
9312 label->sy_frag = frag_now;
9313 S_SET_VALUE (label, (valueT) frag_now_fix ());
9314 }
9315 }
9316
9317 /* Align to a given power of two. .align 0 turns off the automatic
9318 alignment used by the data creating pseudo-ops. */
9319
9320 static void
9321 s_align (x)
9322 int x;
9323 {
9324 register int temp;
9325 register long temp_fill;
9326 long max_alignment = 15;
9327
9328 /*
9329
9330 o Note that the assembler pulls down any immediately preceeding label
9331 to the aligned address.
9332 o It's not documented but auto alignment is reinstated by
9333 a .align pseudo instruction.
9334 o Note also that after auto alignment is turned off the mips assembler
9335 issues an error on attempt to assemble an improperly aligned data item.
9336 We don't.
9337
9338 */
9339
9340 temp = get_absolute_expression ();
9341 if (temp > max_alignment)
9342 as_bad ("Alignment too large: %d. assumed.", temp = max_alignment);
9343 else if (temp < 0)
9344 {
9345 as_warn ("Alignment negative: 0 assumed.");
9346 temp = 0;
9347 }
9348 if (*input_line_pointer == ',')
9349 {
9350 input_line_pointer++;
9351 temp_fill = get_absolute_expression ();
9352 }
9353 else
9354 temp_fill = 0;
9355 if (temp)
9356 {
9357 auto_align = 1;
9358 mips_align (temp, (int) temp_fill,
9359 insn_labels != NULL ? insn_labels->label : NULL);
9360 }
9361 else
9362 {
9363 auto_align = 0;
9364 }
9365
9366 demand_empty_rest_of_line ();
9367 }
9368
9369 void
9370 mips_flush_pending_output ()
9371 {
9372 mips_emit_delays (false);
9373 mips_clear_insn_labels ();
9374 }
9375
9376 static void
9377 s_change_sec (sec)
9378 int sec;
9379 {
9380 segT seg;
9381
9382 /* When generating embedded PIC code, we only use the .text, .lit8,
9383 .sdata and .sbss sections. We change the .data and .rdata
9384 pseudo-ops to use .sdata. */
9385 if (mips_pic == EMBEDDED_PIC
9386 && (sec == 'd' || sec == 'r'))
9387 sec = 's';
9388
9389 #ifdef OBJ_ELF
9390 /* The ELF backend needs to know that we are changing sections, so
9391 that .previous works correctly. We could do something like check
9392 for a obj_section_change_hook macro, but that might be confusing
9393 as it would not be appropriate to use it in the section changing
9394 functions in read.c, since obj-elf.c intercepts those. FIXME:
9395 This should be cleaner, somehow. */
9396 obj_elf_section_change_hook ();
9397 #endif
9398
9399 mips_emit_delays (false);
9400 switch (sec)
9401 {
9402 case 't':
9403 s_text (0);
9404 break;
9405 case 'd':
9406 s_data (0);
9407 break;
9408 case 'b':
9409 subseg_set (bss_section, (subsegT) get_absolute_expression ());
9410 demand_empty_rest_of_line ();
9411 break;
9412
9413 case 'r':
9414 if (USE_GLOBAL_POINTER_OPT)
9415 {
9416 seg = subseg_new (RDATA_SECTION_NAME,
9417 (subsegT) get_absolute_expression ());
9418 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9419 {
9420 bfd_set_section_flags (stdoutput, seg,
9421 (SEC_ALLOC
9422 | SEC_LOAD
9423 | SEC_READONLY
9424 | SEC_RELOC
9425 | SEC_DATA));
9426 if (strcmp (TARGET_OS, "elf") != 0)
9427 bfd_set_section_alignment (stdoutput, seg, 4);
9428 }
9429 demand_empty_rest_of_line ();
9430 }
9431 else
9432 {
9433 as_bad ("No read only data section in this object file format");
9434 demand_empty_rest_of_line ();
9435 return;
9436 }
9437 break;
9438
9439 case 's':
9440 if (USE_GLOBAL_POINTER_OPT)
9441 {
9442 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
9443 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9444 {
9445 bfd_set_section_flags (stdoutput, seg,
9446 SEC_ALLOC | SEC_LOAD | SEC_RELOC
9447 | SEC_DATA);
9448 if (strcmp (TARGET_OS, "elf") != 0)
9449 bfd_set_section_alignment (stdoutput, seg, 4);
9450 }
9451 demand_empty_rest_of_line ();
9452 break;
9453 }
9454 else
9455 {
9456 as_bad ("Global pointers not supported; recompile -G 0");
9457 demand_empty_rest_of_line ();
9458 return;
9459 }
9460 }
9461
9462 auto_align = 1;
9463 }
9464
9465 void
9466 mips_enable_auto_align ()
9467 {
9468 auto_align = 1;
9469 }
9470
9471 static void
9472 s_cons (log_size)
9473 int log_size;
9474 {
9475 symbolS *label;
9476
9477 label = insn_labels != NULL ? insn_labels->label : NULL;
9478 mips_emit_delays (false);
9479 if (log_size > 0 && auto_align)
9480 mips_align (log_size, 0, label);
9481 mips_clear_insn_labels ();
9482 cons (1 << log_size);
9483 }
9484
9485 static void
9486 s_float_cons (type)
9487 int type;
9488 {
9489 symbolS *label;
9490
9491 label = insn_labels != NULL ? insn_labels->label : NULL;
9492
9493 mips_emit_delays (false);
9494
9495 if (auto_align)
9496 if (type == 'd')
9497 mips_align (3, 0, label);
9498 else
9499 mips_align (2, 0, label);
9500
9501 mips_clear_insn_labels ();
9502
9503 float_cons (type);
9504 }
9505
9506 /* Handle .globl. We need to override it because on Irix 5 you are
9507 permitted to say
9508 .globl foo .text
9509 where foo is an undefined symbol, to mean that foo should be
9510 considered to be the address of a function. */
9511
9512 static void
9513 s_mips_globl (x)
9514 int x;
9515 {
9516 char *name;
9517 int c;
9518 symbolS *symbolP;
9519 flagword flag;
9520
9521 name = input_line_pointer;
9522 c = get_symbol_end ();
9523 symbolP = symbol_find_or_make (name);
9524 *input_line_pointer = c;
9525 SKIP_WHITESPACE ();
9526
9527 /* On Irix 5, every global symbol that is not explicitly labelled as
9528 being a function is apparently labelled as being an object. */
9529 flag = BSF_OBJECT;
9530
9531 if (! is_end_of_line[(unsigned char) *input_line_pointer])
9532 {
9533 char *secname;
9534 asection *sec;
9535
9536 secname = input_line_pointer;
9537 c = get_symbol_end ();
9538 sec = bfd_get_section_by_name (stdoutput, secname);
9539 if (sec == NULL)
9540 as_bad ("%s: no such section", secname);
9541 *input_line_pointer = c;
9542
9543 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
9544 flag = BSF_FUNCTION;
9545 }
9546
9547 symbolP->bsym->flags |= flag;
9548
9549 S_SET_EXTERNAL (symbolP);
9550 demand_empty_rest_of_line ();
9551 }
9552
9553 static void
9554 s_option (x)
9555 int x;
9556 {
9557 char *opt;
9558 char c;
9559
9560 opt = input_line_pointer;
9561 c = get_symbol_end ();
9562
9563 if (*opt == 'O')
9564 {
9565 /* FIXME: What does this mean? */
9566 }
9567 else if (strncmp (opt, "pic", 3) == 0)
9568 {
9569 int i;
9570
9571 i = atoi (opt + 3);
9572 if (i == 0)
9573 mips_pic = NO_PIC;
9574 else if (i == 2)
9575 mips_pic = SVR4_PIC;
9576 else
9577 as_bad (".option pic%d not supported", i);
9578
9579 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
9580 {
9581 if (g_switch_seen && g_switch_value != 0)
9582 as_warn ("-G may not be used with SVR4 PIC code");
9583 g_switch_value = 0;
9584 bfd_set_gp_size (stdoutput, 0);
9585 }
9586 }
9587 else
9588 as_warn ("Unrecognized option \"%s\"", opt);
9589
9590 *input_line_pointer = c;
9591 demand_empty_rest_of_line ();
9592 }
9593
9594 /* This structure is used to hold a stack of .set values. */
9595
9596 struct mips_option_stack
9597 {
9598 struct mips_option_stack *next;
9599 struct mips_set_options options;
9600 };
9601
9602 static struct mips_option_stack *mips_opts_stack;
9603
9604 /* Handle the .set pseudo-op. */
9605
9606 static void
9607 s_mipsset (x)
9608 int x;
9609 {
9610 char *name = input_line_pointer, ch;
9611
9612 while (!is_end_of_line[(unsigned char) *input_line_pointer])
9613 input_line_pointer++;
9614 ch = *input_line_pointer;
9615 *input_line_pointer = '\0';
9616
9617 if (strcmp (name, "reorder") == 0)
9618 {
9619 if (mips_opts.noreorder && prev_nop_frag != NULL)
9620 {
9621 /* If we still have pending nops, we can discard them. The
9622 usual nop handling will insert any that are still
9623 needed. */
9624 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
9625 * (mips_opts.mips16 ? 2 : 4));
9626 prev_nop_frag = NULL;
9627 }
9628 mips_opts.noreorder = 0;
9629 }
9630 else if (strcmp (name, "noreorder") == 0)
9631 {
9632 mips_emit_delays (true);
9633 mips_opts.noreorder = 1;
9634 mips_any_noreorder = 1;
9635 }
9636 else if (strcmp (name, "at") == 0)
9637 {
9638 mips_opts.noat = 0;
9639 }
9640 else if (strcmp (name, "noat") == 0)
9641 {
9642 mips_opts.noat = 1;
9643 }
9644 else if (strcmp (name, "macro") == 0)
9645 {
9646 mips_opts.warn_about_macros = 0;
9647 }
9648 else if (strcmp (name, "nomacro") == 0)
9649 {
9650 if (mips_opts.noreorder == 0)
9651 as_bad ("`noreorder' must be set before `nomacro'");
9652 mips_opts.warn_about_macros = 1;
9653 }
9654 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
9655 {
9656 mips_opts.nomove = 0;
9657 }
9658 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
9659 {
9660 mips_opts.nomove = 1;
9661 }
9662 else if (strcmp (name, "bopt") == 0)
9663 {
9664 mips_opts.nobopt = 0;
9665 }
9666 else if (strcmp (name, "nobopt") == 0)
9667 {
9668 mips_opts.nobopt = 1;
9669 }
9670 else if (strcmp (name, "mips16") == 0
9671 || strcmp (name, "MIPS-16") == 0)
9672 mips_opts.mips16 = 1;
9673 else if (strcmp (name, "nomips16") == 0
9674 || strcmp (name, "noMIPS-16") == 0)
9675 mips_opts.mips16 = 0;
9676 else if (strncmp (name, "mips", 4) == 0)
9677 {
9678 int isa;
9679
9680 /* Permit the user to change the ISA on the fly. Needless to
9681 say, misuse can cause serious problems. */
9682 isa = atoi (name + 4);
9683 if (isa == 0)
9684 mips_opts.isa = file_mips_isa;
9685 else if (isa < 1 || isa > 4)
9686 as_bad ("unknown ISA level");
9687 else
9688 mips_opts.isa = isa;
9689 }
9690 else if (strcmp (name, "autoextend") == 0)
9691 mips_opts.noautoextend = 0;
9692 else if (strcmp (name, "noautoextend") == 0)
9693 mips_opts.noautoextend = 1;
9694 else if (strcmp (name, "push") == 0)
9695 {
9696 struct mips_option_stack *s;
9697
9698 s = (struct mips_option_stack *) xmalloc (sizeof *s);
9699 s->next = mips_opts_stack;
9700 s->options = mips_opts;
9701 mips_opts_stack = s;
9702 }
9703 else if (strcmp (name, "pop") == 0)
9704 {
9705 struct mips_option_stack *s;
9706
9707 s = mips_opts_stack;
9708 if (s == NULL)
9709 as_bad (".set pop with no .set push");
9710 else
9711 {
9712 /* If we're changing the reorder mode we need to handle
9713 delay slots correctly. */
9714 if (s->options.noreorder && ! mips_opts.noreorder)
9715 mips_emit_delays (true);
9716 else if (! s->options.noreorder && mips_opts.noreorder)
9717 {
9718 if (prev_nop_frag != NULL)
9719 {
9720 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
9721 * (mips_opts.mips16 ? 2 : 4));
9722 prev_nop_frag = NULL;
9723 }
9724 }
9725
9726 mips_opts = s->options;
9727 mips_opts_stack = s->next;
9728 free (s);
9729 }
9730 }
9731 else
9732 {
9733 as_warn ("Tried to set unrecognized symbol: %s\n", name);
9734 }
9735 *input_line_pointer = ch;
9736 demand_empty_rest_of_line ();
9737 }
9738
9739 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
9740 .option pic2. It means to generate SVR4 PIC calls. */
9741
9742 static void
9743 s_abicalls (ignore)
9744 int ignore;
9745 {
9746 mips_pic = SVR4_PIC;
9747 if (USE_GLOBAL_POINTER_OPT)
9748 {
9749 if (g_switch_seen && g_switch_value != 0)
9750 as_warn ("-G may not be used with SVR4 PIC code");
9751 g_switch_value = 0;
9752 }
9753 bfd_set_gp_size (stdoutput, 0);
9754 demand_empty_rest_of_line ();
9755 }
9756
9757 /* Handle the .cpload pseudo-op. This is used when generating SVR4
9758 PIC code. It sets the $gp register for the function based on the
9759 function address, which is in the register named in the argument.
9760 This uses a relocation against _gp_disp, which is handled specially
9761 by the linker. The result is:
9762 lui $gp,%hi(_gp_disp)
9763 addiu $gp,$gp,%lo(_gp_disp)
9764 addu $gp,$gp,.cpload argument
9765 The .cpload argument is normally $25 == $t9. */
9766
9767 static void
9768 s_cpload (ignore)
9769 int ignore;
9770 {
9771 expressionS ex;
9772 int icnt = 0;
9773
9774 /* If we are not generating SVR4 PIC code, .cpload is ignored. */
9775 if (mips_pic != SVR4_PIC)
9776 {
9777 s_ignore (0);
9778 return;
9779 }
9780
9781 /* .cpload should be a in .set noreorder section. */
9782 if (mips_opts.noreorder == 0)
9783 as_warn (".cpload not in noreorder section");
9784
9785 ex.X_op = O_symbol;
9786 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
9787 ex.X_op_symbol = NULL;
9788 ex.X_add_number = 0;
9789
9790 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
9791 ex.X_add_symbol->bsym->flags |= BSF_OBJECT;
9792
9793 macro_build_lui ((char *) NULL, &icnt, &ex, GP);
9794 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
9795 (int) BFD_RELOC_LO16);
9796
9797 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
9798 GP, GP, tc_get_register (0));
9799
9800 demand_empty_rest_of_line ();
9801 }
9802
9803 /* Handle the .cprestore pseudo-op. This stores $gp into a given
9804 offset from $sp. The offset is remembered, and after making a PIC
9805 call $gp is restored from that location. */
9806
9807 static void
9808 s_cprestore (ignore)
9809 int ignore;
9810 {
9811 expressionS ex;
9812 int icnt = 0;
9813
9814 /* If we are not generating SVR4 PIC code, .cprestore is ignored. */
9815 if (mips_pic != SVR4_PIC)
9816 {
9817 s_ignore (0);
9818 return;
9819 }
9820
9821 mips_cprestore_offset = get_absolute_expression ();
9822
9823 ex.X_op = O_constant;
9824 ex.X_add_symbol = NULL;
9825 ex.X_op_symbol = NULL;
9826 ex.X_add_number = mips_cprestore_offset;
9827
9828 macro_build ((char *) NULL, &icnt, &ex,
9829 mips_opts.isa < 3 ? "sw" : "sd",
9830 "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
9831
9832 demand_empty_rest_of_line ();
9833 }
9834
9835 /* Handle the .gpword pseudo-op. This is used when generating PIC
9836 code. It generates a 32 bit GP relative reloc. */
9837
9838 static void
9839 s_gpword (ignore)
9840 int ignore;
9841 {
9842 symbolS *label;
9843 expressionS ex;
9844 char *p;
9845
9846 /* When not generating PIC code, this is treated as .word. */
9847 if (mips_pic != SVR4_PIC)
9848 {
9849 s_cons (2);
9850 return;
9851 }
9852
9853 label = insn_labels != NULL ? insn_labels->label : NULL;
9854 mips_emit_delays (true);
9855 if (auto_align)
9856 mips_align (2, 0, label);
9857 mips_clear_insn_labels ();
9858
9859 expression (&ex);
9860
9861 if (ex.X_op != O_symbol || ex.X_add_number != 0)
9862 {
9863 as_bad ("Unsupported use of .gpword");
9864 ignore_rest_of_line ();
9865 }
9866
9867 p = frag_more (4);
9868 md_number_to_chars (p, (valueT) 0, 4);
9869 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
9870 BFD_RELOC_MIPS_GPREL32);
9871
9872 demand_empty_rest_of_line ();
9873 }
9874
9875 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
9876 tables in SVR4 PIC code. */
9877
9878 static void
9879 s_cpadd (ignore)
9880 int ignore;
9881 {
9882 int icnt = 0;
9883 int reg;
9884
9885 /* This is ignored when not generating SVR4 PIC code. */
9886 if (mips_pic != SVR4_PIC)
9887 {
9888 s_ignore (0);
9889 return;
9890 }
9891
9892 /* Add $gp to the register named as an argument. */
9893 reg = tc_get_register (0);
9894 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9895 mips_opts.isa < 3 ? "addu" : "daddu",
9896 "d,v,t", reg, reg, GP);
9897
9898 demand_empty_rest_of_line ();
9899 }
9900
9901 /* Handle the .insn pseudo-op. This marks instruction labels in
9902 mips16 mode. This permits the linker to handle them specially,
9903 such as generating jalx instructions when needed. We also make
9904 them odd for the duration of the assembly, in order to generate the
9905 right sort of code. We will make them even in the adjust_symtab
9906 routine, while leaving them marked. This is convenient for the
9907 debugger and the disassembler. The linker knows to make them odd
9908 again. */
9909
9910 static void
9911 s_insn (ignore)
9912 int ignore;
9913 {
9914 if (mips_opts.mips16)
9915 mips16_mark_labels ();
9916
9917 demand_empty_rest_of_line ();
9918 }
9919
9920 /* Handle a .stabn directive. We need these in order to mark a label
9921 as being a mips16 text label correctly. Sometimes the compiler
9922 will emit a label, followed by a .stabn, and then switch sections.
9923 If the label and .stabn are in mips16 mode, then the label is
9924 really a mips16 text label. */
9925
9926 static void
9927 s_mips_stab (type)
9928 int type;
9929 {
9930 if (type == 'n' && mips_opts.mips16)
9931 mips16_mark_labels ();
9932
9933 s_stab (type);
9934 }
9935
9936 /* Parse a register string into a number. Called from the ECOFF code
9937 to parse .frame. The argument is non-zero if this is the frame
9938 register, so that we can record it in mips_frame_reg. */
9939
9940 int
9941 tc_get_register (frame)
9942 int frame;
9943 {
9944 int reg;
9945
9946 SKIP_WHITESPACE ();
9947 if (*input_line_pointer++ != '$')
9948 {
9949 as_warn ("expected `$'");
9950 reg = 0;
9951 }
9952 else if (isdigit ((unsigned char) *input_line_pointer))
9953 {
9954 reg = get_absolute_expression ();
9955 if (reg < 0 || reg >= 32)
9956 {
9957 as_warn ("Bad register number");
9958 reg = 0;
9959 }
9960 }
9961 else
9962 {
9963 if (strncmp (input_line_pointer, "fp", 2) == 0)
9964 reg = FP;
9965 else if (strncmp (input_line_pointer, "sp", 2) == 0)
9966 reg = SP;
9967 else if (strncmp (input_line_pointer, "gp", 2) == 0)
9968 reg = GP;
9969 else if (strncmp (input_line_pointer, "at", 2) == 0)
9970 reg = AT;
9971 else
9972 {
9973 as_warn ("Unrecognized register name");
9974 reg = 0;
9975 }
9976 input_line_pointer += 2;
9977 }
9978 if (frame)
9979 mips_frame_reg = reg != 0 ? reg : SP;
9980 return reg;
9981 }
9982
9983 valueT
9984 md_section_align (seg, addr)
9985 asection *seg;
9986 valueT addr;
9987 {
9988 int align = bfd_get_section_alignment (stdoutput, seg);
9989
9990 #ifdef OBJ_ELF
9991 /* We don't need to align ELF sections to the full alignment.
9992 However, Irix 5 may prefer that we align them at least to a 16
9993 byte boundary. We don't bother to align the sections if we are
9994 targeted for an embedded system. */
9995 if (strcmp (TARGET_OS, "elf") == 0)
9996 return addr;
9997 if (align > 4)
9998 align = 4;
9999 #endif
10000
10001 return ((addr + (1 << align) - 1) & (-1 << align));
10002 }
10003
10004 /* Utility routine, called from above as well. If called while the
10005 input file is still being read, it's only an approximation. (For
10006 example, a symbol may later become defined which appeared to be
10007 undefined earlier.) */
10008
10009 static int
10010 nopic_need_relax (sym, before_relaxing)
10011 symbolS *sym;
10012 int before_relaxing;
10013 {
10014 if (sym == 0)
10015 return 0;
10016
10017 if (USE_GLOBAL_POINTER_OPT)
10018 {
10019 const char *symname;
10020 int change;
10021
10022 /* Find out whether this symbol can be referenced off the GP
10023 register. It can be if it is smaller than the -G size or if
10024 it is in the .sdata or .sbss section. Certain symbols can
10025 not be referenced off the GP, although it appears as though
10026 they can. */
10027 symname = S_GET_NAME (sym);
10028 if (symname != (const char *) NULL
10029 && (strcmp (symname, "eprol") == 0
10030 || strcmp (symname, "etext") == 0
10031 || strcmp (symname, "_gp") == 0
10032 || strcmp (symname, "edata") == 0
10033 || strcmp (symname, "_fbss") == 0
10034 || strcmp (symname, "_fdata") == 0
10035 || strcmp (symname, "_ftext") == 0
10036 || strcmp (symname, "end") == 0
10037 || strcmp (symname, "_gp_disp") == 0))
10038 change = 1;
10039 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
10040 && (0
10041 #ifndef NO_ECOFF_DEBUGGING
10042 || (sym->ecoff_extern_size != 0
10043 && sym->ecoff_extern_size <= g_switch_value)
10044 #endif
10045 /* We must defer this decision until after the whole
10046 file has been read, since there might be a .extern
10047 after the first use of this symbol. */
10048 || (before_relaxing
10049 #ifndef NO_ECOFF_DEBUGGING
10050 && sym->ecoff_extern_size == 0
10051 #endif
10052 && S_GET_VALUE (sym) == 0)
10053 || (S_GET_VALUE (sym) != 0
10054 && S_GET_VALUE (sym) <= g_switch_value)))
10055 change = 0;
10056 else
10057 {
10058 const char *segname;
10059
10060 segname = segment_name (S_GET_SEGMENT (sym));
10061 assert (strcmp (segname, ".lit8") != 0
10062 && strcmp (segname, ".lit4") != 0);
10063 change = (strcmp (segname, ".sdata") != 0
10064 && strcmp (segname, ".sbss") != 0);
10065 }
10066 return change;
10067 }
10068 else
10069 /* We are not optimizing for the GP register. */
10070 return 1;
10071 }
10072
10073 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
10074 extended opcode. SEC is the section the frag is in. */
10075
10076 static int
10077 mips16_extended_frag (fragp, sec, stretch)
10078 fragS *fragp;
10079 asection *sec;
10080 long stretch;
10081 {
10082 int type;
10083 register const struct mips16_immed_operand *op;
10084 offsetT val;
10085 int mintiny, maxtiny;
10086 segT symsec;
10087
10088 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
10089 return 0;
10090 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
10091 return 1;
10092
10093 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
10094 op = mips16_immed_operands;
10095 while (op->type != type)
10096 {
10097 ++op;
10098 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10099 }
10100
10101 if (op->unsp)
10102 {
10103 if (type == '<' || type == '>' || type == '[' || type == ']')
10104 {
10105 mintiny = 1;
10106 maxtiny = 1 << op->nbits;
10107 }
10108 else
10109 {
10110 mintiny = 0;
10111 maxtiny = (1 << op->nbits) - 1;
10112 }
10113 }
10114 else
10115 {
10116 mintiny = - (1 << (op->nbits - 1));
10117 maxtiny = (1 << (op->nbits - 1)) - 1;
10118 }
10119
10120 /* We can't call S_GET_VALUE here, because we don't want to lock in
10121 a particular frag address. */
10122 if (fragp->fr_symbol->sy_value.X_op == O_constant)
10123 {
10124 val = (fragp->fr_symbol->sy_value.X_add_number
10125 + fragp->fr_symbol->sy_frag->fr_address);
10126 symsec = S_GET_SEGMENT (fragp->fr_symbol);
10127 }
10128 else if (fragp->fr_symbol->sy_value.X_op == O_symbol
10129 && (fragp->fr_symbol->sy_value.X_add_symbol->sy_value.X_op
10130 == O_constant))
10131 {
10132 val = (fragp->fr_symbol->sy_value.X_add_symbol->sy_value.X_add_number
10133 + fragp->fr_symbol->sy_value.X_add_symbol->sy_frag->fr_address
10134 + fragp->fr_symbol->sy_value.X_add_number
10135 + fragp->fr_symbol->sy_frag->fr_address);
10136 symsec = S_GET_SEGMENT (fragp->fr_symbol->sy_value.X_add_symbol);
10137 }
10138 else
10139 return 1;
10140
10141 if (op->pcrel)
10142 {
10143 addressT addr;
10144
10145 /* We won't have the section when we are called from
10146 mips_relax_frag. However, we will always have been called
10147 from md_estimate_size_before_relax first. If this is a
10148 branch to a different section, we mark it as such. If SEC is
10149 NULL, and the frag is not marked, then it must be a branch to
10150 the same section. */
10151 if (sec == NULL)
10152 {
10153 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
10154 return 1;
10155 }
10156 else
10157 {
10158 if (symsec != sec)
10159 {
10160 fragp->fr_subtype =
10161 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
10162
10163 /* FIXME: We should support this, and let the linker
10164 catch branches and loads that are out of range. */
10165 as_bad_where (fragp->fr_file, fragp->fr_line,
10166 "unsupported PC relative reference to different section");
10167
10168 return 1;
10169 }
10170 }
10171
10172 /* In this case, we know for sure that the symbol fragment is in
10173 the same section. If the fr_address of the symbol fragment
10174 is greater then the address of this fragment we want to add
10175 in STRETCH in order to get a better estimate of the address.
10176 This particularly matters because of the shift bits. */
10177 if (stretch != 0
10178 && fragp->fr_symbol->sy_frag->fr_address >= fragp->fr_address)
10179 {
10180 fragS *f;
10181
10182 /* Adjust stretch for any alignment frag. Note that if have
10183 been expanding the earlier code, the symbol may be
10184 defined in what appears to be an earlier frag. FIXME:
10185 This doesn't handle the fr_subtype field, which specifies
10186 a maximum number of bytes to skip when doing an
10187 alignment. */
10188 for (f = fragp;
10189 f != NULL && f != fragp->fr_symbol->sy_frag;
10190 f = f->fr_next)
10191 {
10192 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
10193 {
10194 if (stretch < 0)
10195 stretch = - ((- stretch)
10196 & ~ ((1 << (int) f->fr_offset) - 1));
10197 else
10198 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
10199 if (stretch == 0)
10200 break;
10201 }
10202 }
10203 if (f != NULL)
10204 val += stretch;
10205 }
10206
10207 addr = fragp->fr_address + fragp->fr_fix;
10208
10209 /* The base address rules are complicated. The base address of
10210 a branch is the following instruction. The base address of a
10211 PC relative load or add is the instruction itself, but if it
10212 is in a delay slot (in which case it can not be extended) use
10213 the address of the instruction whose delay slot it is in. */
10214 if (type == 'p' || type == 'q')
10215 {
10216 addr += 2;
10217
10218 /* If we are currently assuming that this frag should be
10219 extended, then, the current address is two bytes
10220 higher. */
10221 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
10222 addr += 2;
10223
10224 /* Ignore the low bit in the target, since it will be set
10225 for a text label. */
10226 if ((val & 1) != 0)
10227 --val;
10228 }
10229 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
10230 addr -= 4;
10231 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
10232 addr -= 2;
10233
10234 val -= addr & ~ ((1 << op->shift) - 1);
10235
10236 /* Branch offsets have an implicit 0 in the lowest bit. */
10237 if (type == 'p' || type == 'q')
10238 val /= 2;
10239
10240 /* If any of the shifted bits are set, we must use an extended
10241 opcode. If the address depends on the size of this
10242 instruction, this can lead to a loop, so we arrange to always
10243 use an extended opcode. We only check this when we are in
10244 the main relaxation loop, when SEC is NULL. */
10245 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
10246 {
10247 fragp->fr_subtype =
10248 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
10249 return 1;
10250 }
10251
10252 /* If we are about to mark a frag as extended because the value
10253 is precisely maxtiny + 1, then there is a chance of an
10254 infinite loop as in the following code:
10255 la $4,foo
10256 .skip 1020
10257 .align 2
10258 foo:
10259 In this case when the la is extended, foo is 0x3fc bytes
10260 away, so the la can be shrunk, but then foo is 0x400 away, so
10261 the la must be extended. To avoid this loop, we mark the
10262 frag as extended if it was small, and is about to become
10263 extended with a value of maxtiny + 1. */
10264 if (val == ((maxtiny + 1) << op->shift)
10265 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
10266 && sec == NULL)
10267 {
10268 fragp->fr_subtype =
10269 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
10270 return 1;
10271 }
10272 }
10273 else if (symsec != absolute_section && sec != NULL)
10274 as_bad_where (fragp->fr_file, fragp->fr_line, "unsupported relocation");
10275
10276 if ((val & ((1 << op->shift) - 1)) != 0
10277 || val < (mintiny << op->shift)
10278 || val > (maxtiny << op->shift))
10279 return 1;
10280 else
10281 return 0;
10282 }
10283
10284 /* Estimate the size of a frag before relaxing. Unless this is the
10285 mips16, we are not really relaxing here, and the final size is
10286 encoded in the subtype information. For the mips16, we have to
10287 decide whether we are using an extended opcode or not. */
10288
10289 /*ARGSUSED*/
10290 int
10291 md_estimate_size_before_relax (fragp, segtype)
10292 fragS *fragp;
10293 asection *segtype;
10294 {
10295 int change;
10296
10297 if (RELAX_MIPS16_P (fragp->fr_subtype))
10298 {
10299 if (mips16_extended_frag (fragp, segtype, 0))
10300 {
10301 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
10302 return 4;
10303 }
10304 else
10305 {
10306 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
10307 return 2;
10308 }
10309 }
10310
10311 if (mips_pic == NO_PIC)
10312 {
10313 change = nopic_need_relax (fragp->fr_symbol, 0);
10314 }
10315 else if (mips_pic == SVR4_PIC)
10316 {
10317 symbolS *sym;
10318 asection *symsec;
10319
10320 sym = fragp->fr_symbol;
10321
10322 /* Handle the case of a symbol equated to another symbol. */
10323 while (sym->sy_value.X_op == O_symbol
10324 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
10325 {
10326 symbolS *n;
10327
10328 /* It's possible to get a loop here in a badly written
10329 program. */
10330 n = sym->sy_value.X_add_symbol;
10331 if (n == sym)
10332 break;
10333 sym = n;
10334 }
10335
10336 symsec = S_GET_SEGMENT (sym);
10337
10338 /* This must duplicate the test in adjust_reloc_syms. */
10339 change = (symsec != &bfd_und_section
10340 && symsec != &bfd_abs_section
10341 && ! bfd_is_com_section (symsec));
10342 }
10343 else
10344 abort ();
10345
10346 if (change)
10347 {
10348 /* Record the offset to the first reloc in the fr_opcode field.
10349 This lets md_convert_frag and tc_gen_reloc know that the code
10350 must be expanded. */
10351 fragp->fr_opcode = (fragp->fr_literal
10352 + fragp->fr_fix
10353 - RELAX_OLD (fragp->fr_subtype)
10354 + RELAX_RELOC1 (fragp->fr_subtype));
10355 /* FIXME: This really needs as_warn_where. */
10356 if (RELAX_WARN (fragp->fr_subtype))
10357 as_warn ("AT used after \".set noat\" or macro used after \".set nomacro\"");
10358 }
10359
10360 if (! change)
10361 return 0;
10362 else
10363 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
10364 }
10365
10366 /* This is called to see whether a reloc against a defined symbol
10367 should be converted into a reloc against a section. Don't adjust
10368 MIPS16 jump relocations, so we don't have to worry about the format
10369 of the offset in the .o file. Don't adjust relocations against
10370 mips16 symbols, so that the linker can find them if it needs to set
10371 up a stub. */
10372
10373 int
10374 mips_fix_adjustable (fixp)
10375 fixS *fixp;
10376 {
10377 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
10378 return 0;
10379 if (fixp->fx_addsy == NULL)
10380 return 1;
10381 #ifdef OBJ_ELF
10382 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10383 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
10384 && fixp->fx_subsy == NULL)
10385 return 0;
10386 #endif
10387 return 1;
10388 }
10389
10390 /* Translate internal representation of relocation info to BFD target
10391 format. */
10392
10393 arelent **
10394 tc_gen_reloc (section, fixp)
10395 asection *section;
10396 fixS *fixp;
10397 {
10398 static arelent *retval[4];
10399 arelent *reloc;
10400 bfd_reloc_code_real_type code;
10401
10402 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
10403 retval[1] = NULL;
10404
10405 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
10406 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
10407
10408 if (mips_pic == EMBEDDED_PIC
10409 && SWITCH_TABLE (fixp))
10410 {
10411 /* For a switch table entry we use a special reloc. The addend
10412 is actually the difference between the reloc address and the
10413 subtrahend. */
10414 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
10415 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
10416 as_fatal ("Double check fx_r_type in tc-mips.c:tc_gen_reloc");
10417 fixp->fx_r_type = BFD_RELOC_GPREL32;
10418 }
10419 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
10420 {
10421 /* We use a special addend for an internal RELLO reloc. */
10422 if (fixp->fx_addsy->bsym->flags & BSF_SECTION_SYM)
10423 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
10424 else
10425 reloc->addend = fixp->fx_addnumber + reloc->address;
10426 }
10427 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
10428 {
10429 assert (fixp->fx_next != NULL
10430 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
10431 /* We use a special addend for an internal RELHI reloc. The
10432 reloc is relative to the RELLO; adjust the addend
10433 accordingly. */
10434 if (fixp->fx_addsy->bsym->flags & BSF_SECTION_SYM)
10435 reloc->addend = (fixp->fx_next->fx_frag->fr_address
10436 + fixp->fx_next->fx_where
10437 - S_GET_VALUE (fixp->fx_subsy));
10438 else
10439 reloc->addend = (fixp->fx_addnumber
10440 + fixp->fx_next->fx_frag->fr_address
10441 + fixp->fx_next->fx_where);
10442 }
10443 else if (fixp->fx_pcrel == 0)
10444 reloc->addend = fixp->fx_addnumber;
10445 else
10446 {
10447 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
10448 /* A gruesome hack which is a result of the gruesome gas reloc
10449 handling. */
10450 reloc->addend = reloc->address;
10451 else
10452 reloc->addend = -reloc->address;
10453 }
10454
10455 /* If this is a variant frag, we may need to adjust the existing
10456 reloc and generate a new one. */
10457 if (fixp->fx_frag->fr_opcode != NULL
10458 && (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
10459 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10460 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
10461 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
10462 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
10463 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
10464 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16))
10465 {
10466 arelent *reloc2;
10467
10468 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
10469
10470 /* If this is not the last reloc in this frag, then we have two
10471 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
10472 CALL_HI16/CALL_LO16, both of which are being replaced. Let
10473 the second one handle all of them. */
10474 if (fixp->fx_next != NULL
10475 && fixp->fx_frag == fixp->fx_next->fx_frag)
10476 {
10477 assert ((fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
10478 && fixp->fx_next->fx_r_type == BFD_RELOC_MIPS_GPREL)
10479 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
10480 && (fixp->fx_next->fx_r_type
10481 == BFD_RELOC_MIPS_GOT_LO16))
10482 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
10483 && (fixp->fx_next->fx_r_type
10484 == BFD_RELOC_MIPS_CALL_LO16)));
10485 retval[0] = NULL;
10486 return retval;
10487 }
10488
10489 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
10490 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
10491 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
10492 retval[2] = NULL;
10493 reloc2->sym_ptr_ptr = &fixp->fx_addsy->bsym;
10494 reloc2->address = (reloc->address
10495 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
10496 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
10497 reloc2->addend = fixp->fx_addnumber;
10498 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
10499 assert (reloc2->howto != NULL);
10500
10501 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
10502 {
10503 arelent *reloc3;
10504
10505 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
10506 retval[3] = NULL;
10507 *reloc3 = *reloc2;
10508 reloc3->address += 4;
10509 }
10510
10511 if (mips_pic == NO_PIC)
10512 {
10513 assert (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL);
10514 fixp->fx_r_type = BFD_RELOC_HI16_S;
10515 }
10516 else if (mips_pic == SVR4_PIC)
10517 {
10518 switch (fixp->fx_r_type)
10519 {
10520 default:
10521 abort ();
10522 case BFD_RELOC_MIPS_GOT16:
10523 break;
10524 case BFD_RELOC_MIPS_CALL16:
10525 case BFD_RELOC_MIPS_GOT_LO16:
10526 case BFD_RELOC_MIPS_CALL_LO16:
10527 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
10528 break;
10529 }
10530 }
10531 else
10532 abort ();
10533 }
10534
10535 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
10536 fixup_segment converted a non-PC relative reloc into a PC
10537 relative reloc. In such a case, we need to convert the reloc
10538 code. */
10539 code = fixp->fx_r_type;
10540 if (fixp->fx_pcrel)
10541 {
10542 switch (code)
10543 {
10544 case BFD_RELOC_8:
10545 code = BFD_RELOC_8_PCREL;
10546 break;
10547 case BFD_RELOC_16:
10548 code = BFD_RELOC_16_PCREL;
10549 break;
10550 case BFD_RELOC_32:
10551 code = BFD_RELOC_32_PCREL;
10552 break;
10553 case BFD_RELOC_64:
10554 code = BFD_RELOC_64_PCREL;
10555 break;
10556 case BFD_RELOC_8_PCREL:
10557 case BFD_RELOC_16_PCREL:
10558 case BFD_RELOC_32_PCREL:
10559 case BFD_RELOC_64_PCREL:
10560 case BFD_RELOC_16_PCREL_S2:
10561 case BFD_RELOC_PCREL_HI16_S:
10562 case BFD_RELOC_PCREL_LO16:
10563 break;
10564 default:
10565 as_bad_where (fixp->fx_file, fixp->fx_line,
10566 "Cannot make %s relocation PC relative",
10567 bfd_get_reloc_code_name (code));
10568 }
10569 }
10570
10571 /* To support a PC relative reloc when generating embedded PIC code
10572 for ECOFF, we use a Cygnus extension. We check for that here to
10573 make sure that we don't let such a reloc escape normally. */
10574 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour
10575 && code == BFD_RELOC_16_PCREL_S2
10576 && mips_pic != EMBEDDED_PIC)
10577 reloc->howto = NULL;
10578 else
10579 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
10580
10581 if (reloc->howto == NULL)
10582 {
10583 as_bad_where (fixp->fx_file, fixp->fx_line,
10584 "Can not represent %s relocation in this object file format",
10585 bfd_get_reloc_code_name (code));
10586 retval[0] = NULL;
10587 }
10588
10589 return retval;
10590 }
10591
10592 /* Relax a machine dependent frag. This returns the amount by which
10593 the current size of the frag should change. */
10594
10595 int
10596 mips_relax_frag (fragp, stretch)
10597 fragS *fragp;
10598 long stretch;
10599 {
10600 if (! RELAX_MIPS16_P (fragp->fr_subtype))
10601 return 0;
10602
10603 if (mips16_extended_frag (fragp, (asection *) NULL, stretch))
10604 {
10605 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
10606 return 0;
10607 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
10608 return 2;
10609 }
10610 else
10611 {
10612 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
10613 return 0;
10614 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
10615 return -2;
10616 }
10617
10618 return 0;
10619 }
10620
10621 /* Convert a machine dependent frag. */
10622
10623 void
10624 md_convert_frag (abfd, asec, fragp)
10625 bfd *abfd;
10626 segT asec;
10627 fragS *fragp;
10628 {
10629 int old, new;
10630 char *fixptr;
10631
10632 if (RELAX_MIPS16_P (fragp->fr_subtype))
10633 {
10634 int type;
10635 register const struct mips16_immed_operand *op;
10636 boolean small, ext;
10637 offsetT val;
10638 bfd_byte *buf;
10639 unsigned long insn;
10640 boolean use_extend;
10641 unsigned short extend;
10642
10643 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
10644 op = mips16_immed_operands;
10645 while (op->type != type)
10646 ++op;
10647
10648 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
10649 {
10650 small = false;
10651 ext = true;
10652 }
10653 else
10654 {
10655 small = true;
10656 ext = false;
10657 }
10658
10659 resolve_symbol_value (fragp->fr_symbol, 1);
10660 val = S_GET_VALUE (fragp->fr_symbol);
10661 if (op->pcrel)
10662 {
10663 addressT addr;
10664
10665 addr = fragp->fr_address + fragp->fr_fix;
10666
10667 /* The rules for the base address of a PC relative reloc are
10668 complicated; see mips16_extended_frag. */
10669 if (type == 'p' || type == 'q')
10670 {
10671 addr += 2;
10672 if (ext)
10673 addr += 2;
10674 /* Ignore the low bit in the target, since it will be
10675 set for a text label. */
10676 if ((val & 1) != 0)
10677 --val;
10678 }
10679 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
10680 addr -= 4;
10681 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
10682 addr -= 2;
10683
10684 addr &= ~ (addressT) ((1 << op->shift) - 1);
10685 val -= addr;
10686
10687 /* Make sure the section winds up with the alignment we have
10688 assumed. */
10689 if (op->shift > 0)
10690 record_alignment (asec, op->shift);
10691 }
10692
10693 if (ext
10694 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
10695 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
10696 as_warn_where (fragp->fr_file, fragp->fr_line,
10697 "extended instruction in delay slot");
10698
10699 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
10700
10701 if (target_big_endian)
10702 insn = bfd_getb16 (buf);
10703 else
10704 insn = bfd_getl16 (buf);
10705
10706 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
10707 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
10708 small, ext, &insn, &use_extend, &extend);
10709
10710 if (use_extend)
10711 {
10712 md_number_to_chars (buf, 0xf000 | extend, 2);
10713 fragp->fr_fix += 2;
10714 buf += 2;
10715 }
10716
10717 md_number_to_chars (buf, insn, 2);
10718 fragp->fr_fix += 2;
10719 buf += 2;
10720 }
10721 else
10722 {
10723 if (fragp->fr_opcode == NULL)
10724 return;
10725
10726 old = RELAX_OLD (fragp->fr_subtype);
10727 new = RELAX_NEW (fragp->fr_subtype);
10728 fixptr = fragp->fr_literal + fragp->fr_fix;
10729
10730 if (new > 0)
10731 memcpy (fixptr - old, fixptr, new);
10732
10733 fragp->fr_fix += new - old;
10734 }
10735 }
10736
10737 #ifdef OBJ_ELF
10738
10739 /* This function is called after the relocs have been generated.
10740 We've been storing mips16 text labels as odd. Here we convert them
10741 back to even for the convenience of the debugger. */
10742
10743 void
10744 mips_frob_file_after_relocs ()
10745 {
10746 asymbol **syms;
10747 unsigned int count, i;
10748
10749 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10750 return;
10751
10752 syms = bfd_get_outsymbols (stdoutput);
10753 count = bfd_get_symcount (stdoutput);
10754 for (i = 0; i < count; i++, syms++)
10755 {
10756 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
10757 && ((*syms)->value & 1) != 0)
10758 {
10759 (*syms)->value &= ~1;
10760 /* If the symbol has an odd size, it was probably computed
10761 incorrectly, so adjust that as well. */
10762 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
10763 ++elf_symbol (*syms)->internal_elf_sym.st_size;
10764 }
10765 }
10766 }
10767
10768 #endif
10769
10770 /* This function is called whenever a label is defined. It is used
10771 when handling branch delays; if a branch has a label, we assume we
10772 can not move it. */
10773
10774 void
10775 mips_define_label (sym)
10776 symbolS *sym;
10777 {
10778 struct insn_label_list *l;
10779
10780 if (free_insn_labels == NULL)
10781 l = (struct insn_label_list *) xmalloc (sizeof *l);
10782 else
10783 {
10784 l = free_insn_labels;
10785 free_insn_labels = l->next;
10786 }
10787
10788 l->label = sym;
10789 l->next = insn_labels;
10790 insn_labels = l;
10791 }
10792 \f
10793 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10794
10795 /* Some special processing for a MIPS ELF file. */
10796
10797 void
10798 mips_elf_final_processing ()
10799 {
10800 /* Write out the register information. */
10801 if (! mips_64)
10802 {
10803 Elf32_RegInfo s;
10804
10805 s.ri_gprmask = mips_gprmask;
10806 s.ri_cprmask[0] = mips_cprmask[0];
10807 s.ri_cprmask[1] = mips_cprmask[1];
10808 s.ri_cprmask[2] = mips_cprmask[2];
10809 s.ri_cprmask[3] = mips_cprmask[3];
10810 /* The gp_value field is set by the MIPS ELF backend. */
10811
10812 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
10813 ((Elf32_External_RegInfo *)
10814 mips_regmask_frag));
10815 }
10816 else
10817 {
10818 Elf64_Internal_RegInfo s;
10819
10820 s.ri_gprmask = mips_gprmask;
10821 s.ri_pad = 0;
10822 s.ri_cprmask[0] = mips_cprmask[0];
10823 s.ri_cprmask[1] = mips_cprmask[1];
10824 s.ri_cprmask[2] = mips_cprmask[2];
10825 s.ri_cprmask[3] = mips_cprmask[3];
10826 /* The gp_value field is set by the MIPS ELF backend. */
10827
10828 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
10829 ((Elf64_External_RegInfo *)
10830 mips_regmask_frag));
10831 }
10832
10833 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
10834 sort of BFD interface for this. */
10835 if (mips_any_noreorder)
10836 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
10837 if (mips_pic != NO_PIC)
10838 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
10839 }
10840
10841 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
10842 \f
10843 /* These functions should really be defined by the object file format,
10844 since they are related to debugging information. However, this
10845 code has to work for the a.out format, which does not define them,
10846 so we provide simple versions here. These don't actually generate
10847 any debugging information, but they do simple checking and someday
10848 somebody may make them useful. */
10849
10850 typedef struct loc
10851 {
10852 struct loc *loc_next;
10853 unsigned long loc_fileno;
10854 unsigned long loc_lineno;
10855 unsigned long loc_offset;
10856 unsigned short loc_delta;
10857 unsigned short loc_count;
10858 #if 0
10859 fragS *loc_frag;
10860 #endif
10861 }
10862 locS;
10863
10864 typedef struct proc
10865 {
10866 struct proc *proc_next;
10867 struct symbol *proc_isym;
10868 struct symbol *proc_end;
10869 unsigned long proc_reg_mask;
10870 unsigned long proc_reg_offset;
10871 unsigned long proc_fpreg_mask;
10872 unsigned long proc_fpreg_offset;
10873 unsigned long proc_frameoffset;
10874 unsigned long proc_framereg;
10875 unsigned long proc_pcreg;
10876 locS *proc_iline;
10877 struct file *proc_file;
10878 int proc_index;
10879 }
10880 procS;
10881
10882 typedef struct file
10883 {
10884 struct file *file_next;
10885 unsigned long file_fileno;
10886 struct symbol *file_symbol;
10887 struct symbol *file_end;
10888 struct proc *file_proc;
10889 int file_numprocs;
10890 }
10891 fileS;
10892
10893 static struct obstack proc_frags;
10894 static procS *proc_lastP;
10895 static procS *proc_rootP;
10896 static int numprocs;
10897
10898 static void
10899 md_obj_begin ()
10900 {
10901 obstack_begin (&proc_frags, 0x2000);
10902 }
10903
10904 static void
10905 md_obj_end ()
10906 {
10907 /* check for premature end, nesting errors, etc */
10908 if (proc_lastP && proc_lastP->proc_end == NULL)
10909 as_warn ("missing `.end' at end of assembly");
10910 }
10911
10912 static long
10913 get_number ()
10914 {
10915 int negative = 0;
10916 long val = 0;
10917
10918 if (*input_line_pointer == '-')
10919 {
10920 ++input_line_pointer;
10921 negative = 1;
10922 }
10923 if (!isdigit (*input_line_pointer))
10924 as_bad ("Expected simple number.");
10925 if (input_line_pointer[0] == '0')
10926 {
10927 if (input_line_pointer[1] == 'x')
10928 {
10929 input_line_pointer += 2;
10930 while (isxdigit (*input_line_pointer))
10931 {
10932 val <<= 4;
10933 val |= hex_value (*input_line_pointer++);
10934 }
10935 return negative ? -val : val;
10936 }
10937 else
10938 {
10939 ++input_line_pointer;
10940 while (isdigit (*input_line_pointer))
10941 {
10942 val <<= 3;
10943 val |= *input_line_pointer++ - '0';
10944 }
10945 return negative ? -val : val;
10946 }
10947 }
10948 if (!isdigit (*input_line_pointer))
10949 {
10950 printf (" *input_line_pointer == '%c' 0x%02x\n",
10951 *input_line_pointer, *input_line_pointer);
10952 as_warn ("Invalid number");
10953 return -1;
10954 }
10955 while (isdigit (*input_line_pointer))
10956 {
10957 val *= 10;
10958 val += *input_line_pointer++ - '0';
10959 }
10960 return negative ? -val : val;
10961 }
10962
10963 /* The .file directive; just like the usual .file directive, but there
10964 is an initial number which is the ECOFF file index. */
10965
10966 static void
10967 s_file (x)
10968 int x;
10969 {
10970 int line;
10971
10972 line = get_number ();
10973 s_app_file (0);
10974 }
10975
10976
10977 /* The .end directive. */
10978
10979 static void
10980 s_mipsend (x)
10981 int x;
10982 {
10983 symbolS *p;
10984
10985 if (!is_end_of_line[(unsigned char) *input_line_pointer])
10986 {
10987 p = get_symbol ();
10988 demand_empty_rest_of_line ();
10989 }
10990 else
10991 p = NULL;
10992 if (now_seg != text_section)
10993 as_warn (".end not in text section");
10994 if (!proc_lastP)
10995 {
10996 as_warn (".end and no .ent seen yet.");
10997 return;
10998 }
10999
11000 if (p != NULL)
11001 {
11002 assert (S_GET_NAME (p));
11003 if (strcmp (S_GET_NAME (p), S_GET_NAME (proc_lastP->proc_isym)))
11004 as_warn (".end symbol does not match .ent symbol.");
11005 }
11006
11007 proc_lastP->proc_end = (symbolS *) 1;
11008 }
11009
11010 /* The .aent and .ent directives. */
11011
11012 static void
11013 s_ent (aent)
11014 int aent;
11015 {
11016 int number = 0;
11017 procS *procP;
11018 symbolS *symbolP;
11019
11020 symbolP = get_symbol ();
11021 if (*input_line_pointer == ',')
11022 input_line_pointer++;
11023 SKIP_WHITESPACE ();
11024 if (isdigit (*input_line_pointer) || *input_line_pointer == '-')
11025 number = get_number ();
11026 if (now_seg != text_section)
11027 as_warn (".ent or .aent not in text section.");
11028
11029 if (!aent && proc_lastP && proc_lastP->proc_end == NULL)
11030 as_warn ("missing `.end'");
11031
11032 if (!aent)
11033 {
11034 procP = (procS *) obstack_alloc (&proc_frags, sizeof (*procP));
11035 procP->proc_isym = symbolP;
11036 procP->proc_reg_mask = 0;
11037 procP->proc_reg_offset = 0;
11038 procP->proc_fpreg_mask = 0;
11039 procP->proc_fpreg_offset = 0;
11040 procP->proc_frameoffset = 0;
11041 procP->proc_framereg = 0;
11042 procP->proc_pcreg = 0;
11043 procP->proc_end = NULL;
11044 procP->proc_next = NULL;
11045 if (proc_lastP)
11046 proc_lastP->proc_next = procP;
11047 else
11048 proc_rootP = procP;
11049 proc_lastP = procP;
11050 numprocs++;
11051 }
11052 demand_empty_rest_of_line ();
11053 }
11054
11055 /* The .frame directive. */
11056
11057 #if 0
11058 static void
11059 s_frame (x)
11060 int x;
11061 {
11062 char str[100];
11063 symbolS *symP;
11064 int frame_reg;
11065 int frame_off;
11066 int pcreg;
11067
11068 frame_reg = tc_get_register (1);
11069 if (*input_line_pointer == ',')
11070 input_line_pointer++;
11071 frame_off = get_absolute_expression ();
11072 if (*input_line_pointer == ',')
11073 input_line_pointer++;
11074 pcreg = tc_get_register (0);
11075
11076 /* bob third eye */
11077 assert (proc_rootP);
11078 proc_rootP->proc_framereg = frame_reg;
11079 proc_rootP->proc_frameoffset = frame_off;
11080 proc_rootP->proc_pcreg = pcreg;
11081 /* bob macho .frame */
11082
11083 /* We don't have to write out a frame stab for unoptimized code. */
11084 if (!(frame_reg == FP && frame_off == 0))
11085 {
11086 if (!proc_lastP)
11087 as_warn ("No .ent for .frame to use.");
11088 (void) sprintf (str, "R%d;%d", frame_reg, frame_off);
11089 symP = symbol_new (str, N_VFP, 0, frag_now);
11090 S_SET_TYPE (symP, N_RMASK);
11091 S_SET_OTHER (symP, 0);
11092 S_SET_DESC (symP, 0);
11093 symP->sy_forward = proc_lastP->proc_isym;
11094 /* bob perhaps I should have used pseudo set */
11095 }
11096 demand_empty_rest_of_line ();
11097 }
11098 #endif
11099
11100 /* The .fmask and .mask directives. */
11101
11102 #if 0
11103 static void
11104 s_mask (reg_type)
11105 char reg_type;
11106 {
11107 char str[100], *strP;
11108 symbolS *symP;
11109 int i;
11110 unsigned int mask;
11111 int off;
11112
11113 mask = get_number ();
11114 if (*input_line_pointer == ',')
11115 input_line_pointer++;
11116 off = get_absolute_expression ();
11117
11118 /* bob only for coff */
11119 assert (proc_rootP);
11120 if (reg_type == 'F')
11121 {
11122 proc_rootP->proc_fpreg_mask = mask;
11123 proc_rootP->proc_fpreg_offset = off;
11124 }
11125 else
11126 {
11127 proc_rootP->proc_reg_mask = mask;
11128 proc_rootP->proc_reg_offset = off;
11129 }
11130
11131 /* bob macho .mask + .fmask */
11132
11133 /* We don't have to write out a mask stab if no saved regs. */
11134 if (!(mask == 0))
11135 {
11136 if (!proc_lastP)
11137 as_warn ("No .ent for .mask to use.");
11138 strP = str;
11139 for (i = 0; i < 32; i++)
11140 {
11141 if (mask % 2)
11142 {
11143 sprintf (strP, "%c%d,", reg_type, i);
11144 strP += strlen (strP);
11145 }
11146 mask /= 2;
11147 }
11148 sprintf (strP, ";%d,", off);
11149 symP = symbol_new (str, N_RMASK, 0, frag_now);
11150 S_SET_TYPE (symP, N_RMASK);
11151 S_SET_OTHER (symP, 0);
11152 S_SET_DESC (symP, 0);
11153 symP->sy_forward = proc_lastP->proc_isym;
11154 /* bob perhaps I should have used pseudo set */
11155 }
11156 }
11157 #endif
11158
11159 /* The .loc directive. */
11160
11161 #if 0
11162 static void
11163 s_loc (x)
11164 int x;
11165 {
11166 symbolS *symbolP;
11167 int lineno;
11168 int addroff;
11169
11170 assert (now_seg == text_section);
11171
11172 lineno = get_number ();
11173 addroff = frag_now_fix ();
11174
11175 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
11176 S_SET_TYPE (symbolP, N_SLINE);
11177 S_SET_OTHER (symbolP, 0);
11178 S_SET_DESC (symbolP, lineno);
11179 symbolP->sy_segment = now_seg;
11180 }
11181 #endif