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