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