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