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