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