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