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