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