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