* config/tc-mips.c (append_insn): Correctly handle mips16 case
[binutils.git] / gas / config / tc-sh.c
blobb61793ded5d609b9e11b1ea170b831143048cf49
1 /* tc-sh.c -- Assemble code for the Renesas / SuperH SH
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* Written By Steve Chamberlain <sac@cygnus.com> */
24 #include <stdio.h>
25 #include "as.h"
26 #include "bfd.h"
27 #include "subsegs.h"
28 #define DEFINE_TABLE
29 #include "opcodes/sh-opc.h"
30 #include "safe-ctype.h"
31 #include "struc-symbol.h"
33 #ifdef OBJ_ELF
34 #include "elf/sh.h"
35 #endif
37 #include "dwarf2dbg.h"
38 #include "dw2gencfi.h"
40 typedef struct
42 sh_arg_type type;
43 int reg;
44 expressionS immediate;
46 sh_operand_info;
48 const char comment_chars[] = "!";
49 const char line_separator_chars[] = ";";
50 const char line_comment_chars[] = "!#";
52 static void s_uses (int);
53 static void s_uacons (int);
55 #ifdef OBJ_ELF
56 static void sh_elf_cons (int);
58 symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
59 #endif
61 static void
62 big (int ignore ATTRIBUTE_UNUSED)
64 if (! target_big_endian)
65 as_bad (_("directive .big encountered when option -big required"));
67 /* Stop further messages. */
68 target_big_endian = 1;
71 static void
72 little (int ignore ATTRIBUTE_UNUSED)
74 if (target_big_endian)
75 as_bad (_("directive .little encountered when option -little required"));
77 /* Stop further messages. */
78 target_big_endian = 0;
81 /* This table describes all the machine specific pseudo-ops the assembler
82 has to support. The fields are:
83 pseudo-op name without dot
84 function to call to execute this pseudo-op
85 Integer arg to pass to the function. */
87 const pseudo_typeS md_pseudo_table[] =
89 #ifdef OBJ_ELF
90 {"long", sh_elf_cons, 4},
91 {"int", sh_elf_cons, 4},
92 {"word", sh_elf_cons, 2},
93 {"short", sh_elf_cons, 2},
94 #else
95 {"int", cons, 4},
96 {"word", cons, 2},
97 #endif /* OBJ_ELF */
98 {"big", big, 0},
99 {"form", listing_psize, 0},
100 {"little", little, 0},
101 {"heading", listing_title, 0},
102 {"import", s_ignore, 0},
103 {"page", listing_eject, 0},
104 {"program", s_ignore, 0},
105 {"uses", s_uses, 0},
106 {"uaword", s_uacons, 2},
107 {"ualong", s_uacons, 4},
108 {"uaquad", s_uacons, 8},
109 {"2byte", s_uacons, 2},
110 {"4byte", s_uacons, 4},
111 {"8byte", s_uacons, 8},
112 #ifdef HAVE_SH64
113 {"mode", s_sh64_mode, 0 },
115 /* Have the old name too. */
116 {"isa", s_sh64_mode, 0 },
118 /* Assert that the right ABI is used. */
119 {"abi", s_sh64_abi, 0 },
121 { "vtable_inherit", sh64_vtable_inherit, 0 },
122 { "vtable_entry", sh64_vtable_entry, 0 },
123 #endif /* HAVE_SH64 */
124 {0, 0, 0}
127 int sh_relax; /* set if -relax seen */
129 /* Whether -small was seen. */
131 int sh_small;
133 /* Flag to generate relocations against symbol values for local symbols. */
135 static int dont_adjust_reloc_32;
137 /* preset architecture set, if given; zero otherwise. */
139 static unsigned int preset_target_arch;
141 /* The bit mask of architectures that could
142 accommodate the insns seen so far. */
143 static unsigned int valid_arch;
145 const char EXP_CHARS[] = "eE";
147 /* Chars that mean this number is a floating point constant. */
148 /* As in 0f12.456 */
149 /* or 0d1.2345e12 */
150 const char FLT_CHARS[] = "rRsSfFdDxXpP";
152 #define C(a,b) ENCODE_RELAX(a,b)
154 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
155 #define GET_WHAT(x) ((x>>4))
157 /* These are the three types of relaxable instruction. */
158 /* These are the types of relaxable instructions; except for END which is
159 a marker. */
160 #define COND_JUMP 1
161 #define COND_JUMP_DELAY 2
162 #define UNCOND_JUMP 3
164 #ifdef HAVE_SH64
166 /* A 16-bit (times four) pc-relative operand, at most expanded to 32 bits. */
167 #define SH64PCREL16_32 4
168 /* A 16-bit (times four) pc-relative operand, at most expanded to 64 bits. */
169 #define SH64PCREL16_64 5
171 /* Variants of the above for adjusting the insn to PTA or PTB according to
172 the label. */
173 #define SH64PCREL16PT_32 6
174 #define SH64PCREL16PT_64 7
176 /* A MOVI expansion, expanding to at most 32 or 64 bits. */
177 #define MOVI_IMM_32 8
178 #define MOVI_IMM_32_PCREL 9
179 #define MOVI_IMM_64 10
180 #define MOVI_IMM_64_PCREL 11
181 #define END 12
183 #else /* HAVE_SH64 */
185 #define END 4
187 #endif /* HAVE_SH64 */
189 #define UNDEF_DISP 0
190 #define COND8 1
191 #define COND12 2
192 #define COND32 3
193 #define UNDEF_WORD_DISP 4
195 #define UNCOND12 1
196 #define UNCOND32 2
198 #ifdef HAVE_SH64
199 #define UNDEF_SH64PCREL 0
200 #define SH64PCREL16 1
201 #define SH64PCREL32 2
202 #define SH64PCREL48 3
203 #define SH64PCREL64 4
204 #define SH64PCRELPLT 5
206 #define UNDEF_MOVI 0
207 #define MOVI_16 1
208 #define MOVI_32 2
209 #define MOVI_48 3
210 #define MOVI_64 4
211 #define MOVI_PLT 5
212 #define MOVI_GOTOFF 6
213 #define MOVI_GOTPC 7
214 #endif /* HAVE_SH64 */
216 /* Branch displacements are from the address of the branch plus
217 four, thus all minimum and maximum values have 4 added to them. */
218 #define COND8_F 258
219 #define COND8_M -252
220 #define COND8_LENGTH 2
222 /* There is one extra instruction before the branch, so we must add
223 two more bytes to account for it. */
224 #define COND12_F 4100
225 #define COND12_M -4090
226 #define COND12_LENGTH 6
228 #define COND12_DELAY_LENGTH 4
230 /* ??? The minimum and maximum values are wrong, but this does not matter
231 since this relocation type is not supported yet. */
232 #define COND32_F (1<<30)
233 #define COND32_M -(1<<30)
234 #define COND32_LENGTH 14
236 #define UNCOND12_F 4098
237 #define UNCOND12_M -4092
238 #define UNCOND12_LENGTH 2
240 /* ??? The minimum and maximum values are wrong, but this does not matter
241 since this relocation type is not supported yet. */
242 #define UNCOND32_F (1<<30)
243 #define UNCOND32_M -(1<<30)
244 #define UNCOND32_LENGTH 14
246 #ifdef HAVE_SH64
247 /* The trivial expansion of a SH64PCREL16 relaxation is just a "PT label,
248 TRd" as is the current insn, so no extra length. Note that the "reach"
249 is calculated from the address *after* that insn, but the offset in the
250 insn is calculated from the beginning of the insn. We also need to
251 take into account the implicit 1 coded as the "A" in PTA when counting
252 forward. If PTB reaches an odd address, we trap that as an error
253 elsewhere, so we don't have to have different relaxation entries. We
254 don't add a one to the negative range, since PTB would then have the
255 farthest backward-reaching value skipped, not generated at relaxation. */
256 #define SH64PCREL16_F (32767 * 4 - 4 + 1)
257 #define SH64PCREL16_M (-32768 * 4 - 4)
258 #define SH64PCREL16_LENGTH 0
260 /* The next step is to change that PT insn into
261 MOVI ((label - datalabel Ln) >> 16) & 65535, R25
262 SHORI (label - datalabel Ln) & 65535, R25
264 PTREL R25,TRd
265 which means two extra insns, 8 extra bytes. This is the limit for the
266 32-bit ABI.
268 The expressions look a bit bad since we have to adjust this to avoid overflow on a
269 32-bit host. */
270 #define SH64PCREL32_F ((((long) 1 << 30) - 1) * 2 + 1 - 4)
271 #define SH64PCREL32_LENGTH (2 * 4)
273 /* Similarly, we just change the MOVI and add a SHORI for the 48-bit
274 expansion. */
275 #if BFD_HOST_64BIT_LONG
276 /* The "reach" type is long, so we can only do this for a 64-bit-long
277 host. */
278 #define SH64PCREL32_M (((long) -1 << 30) * 2 - 4)
279 #define SH64PCREL48_F ((((long) 1 << 47) - 1) - 4)
280 #define SH64PCREL48_M (((long) -1 << 47) - 4)
281 #define SH64PCREL48_LENGTH (3 * 4)
282 #else
283 /* If the host does not have 64-bit longs, just make this state identical
284 in reach to the 32-bit state. Note that we have a slightly incorrect
285 reach, but the correct one above will overflow a 32-bit number. */
286 #define SH64PCREL32_M (((long) -1 << 30) * 2)
287 #define SH64PCREL48_F SH64PCREL32_F
288 #define SH64PCREL48_M SH64PCREL32_M
289 #define SH64PCREL48_LENGTH (3 * 4)
290 #endif /* BFD_HOST_64BIT_LONG */
292 /* And similarly for the 64-bit expansion; a MOVI + SHORI + SHORI + SHORI
293 + PTREL sequence. */
294 #define SH64PCREL64_LENGTH (4 * 4)
296 /* For MOVI, we make the MOVI + SHORI... expansion you can see in the
297 SH64PCREL expansions. The PCREL one is similar, but the other has no
298 pc-relative reach; it must be fully expanded in
299 shmedia_md_estimate_size_before_relax. */
300 #define MOVI_16_LENGTH 0
301 #define MOVI_16_F (32767 - 4)
302 #define MOVI_16_M (-32768 - 4)
303 #define MOVI_32_LENGTH 4
304 #define MOVI_32_F ((((long) 1 << 30) - 1) * 2 + 1 - 4)
305 #define MOVI_48_LENGTH 8
307 #if BFD_HOST_64BIT_LONG
308 /* The "reach" type is long, so we can only do this for a 64-bit-long
309 host. */
310 #define MOVI_32_M (((long) -1 << 30) * 2 - 4)
311 #define MOVI_48_F ((((long) 1 << 47) - 1) - 4)
312 #define MOVI_48_M (((long) -1 << 47) - 4)
313 #else
314 /* If the host does not have 64-bit longs, just make this state identical
315 in reach to the 32-bit state. Note that we have a slightly incorrect
316 reach, but the correct one above will overflow a 32-bit number. */
317 #define MOVI_32_M (((long) -1 << 30) * 2)
318 #define MOVI_48_F MOVI_32_F
319 #define MOVI_48_M MOVI_32_M
320 #endif /* BFD_HOST_64BIT_LONG */
322 #define MOVI_64_LENGTH 12
323 #endif /* HAVE_SH64 */
325 #define EMPTY { 0, 0, 0, 0 }
327 const relax_typeS md_relax_table[C (END, 0)] = {
328 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
329 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
331 EMPTY,
332 /* C (COND_JUMP, COND8) */
333 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
334 /* C (COND_JUMP, COND12) */
335 { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
336 /* C (COND_JUMP, COND32) */
337 { COND32_F, COND32_M, COND32_LENGTH, 0, },
338 /* C (COND_JUMP, UNDEF_WORD_DISP) */
339 { 0, 0, COND32_LENGTH, 0, },
340 EMPTY, EMPTY, EMPTY,
341 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
343 EMPTY,
344 /* C (COND_JUMP_DELAY, COND8) */
345 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP_DELAY, COND12) },
346 /* C (COND_JUMP_DELAY, COND12) */
347 { COND12_F, COND12_M, COND12_DELAY_LENGTH, C (COND_JUMP_DELAY, COND32), },
348 /* C (COND_JUMP_DELAY, COND32) */
349 { COND32_F, COND32_M, COND32_LENGTH, 0, },
350 /* C (COND_JUMP_DELAY, UNDEF_WORD_DISP) */
351 { 0, 0, COND32_LENGTH, 0, },
352 EMPTY, EMPTY, EMPTY,
353 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
355 EMPTY,
356 /* C (UNCOND_JUMP, UNCOND12) */
357 { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
358 /* C (UNCOND_JUMP, UNCOND32) */
359 { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
360 EMPTY,
361 /* C (UNCOND_JUMP, UNDEF_WORD_DISP) */
362 { 0, 0, UNCOND32_LENGTH, 0, },
363 EMPTY, EMPTY, EMPTY,
364 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
366 #ifdef HAVE_SH64
367 /* C (SH64PCREL16_32, SH64PCREL16) */
368 EMPTY,
369 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16_32, SH64PCREL32) },
370 /* C (SH64PCREL16_32, SH64PCREL32) */
371 { 0, 0, SH64PCREL32_LENGTH, 0 },
372 EMPTY, EMPTY,
373 /* C (SH64PCREL16_32, SH64PCRELPLT) */
374 { 0, 0, SH64PCREL32_LENGTH, 0 },
375 EMPTY, EMPTY,
376 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
378 /* C (SH64PCREL16_64, SH64PCREL16) */
379 EMPTY,
380 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16_64, SH64PCREL32) },
381 /* C (SH64PCREL16_64, SH64PCREL32) */
382 { SH64PCREL32_F, SH64PCREL32_M, SH64PCREL32_LENGTH, C (SH64PCREL16_64, SH64PCREL48) },
383 /* C (SH64PCREL16_64, SH64PCREL48) */
384 { SH64PCREL48_F, SH64PCREL48_M, SH64PCREL48_LENGTH, C (SH64PCREL16_64, SH64PCREL64) },
385 /* C (SH64PCREL16_64, SH64PCREL64) */
386 { 0, 0, SH64PCREL64_LENGTH, 0 },
387 /* C (SH64PCREL16_64, SH64PCRELPLT) */
388 { 0, 0, SH64PCREL64_LENGTH, 0 },
389 EMPTY, EMPTY,
390 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
392 /* C (SH64PCREL16PT_32, SH64PCREL16) */
393 EMPTY,
394 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16PT_32, SH64PCREL32) },
395 /* C (SH64PCREL16PT_32, SH64PCREL32) */
396 { 0, 0, SH64PCREL32_LENGTH, 0 },
397 EMPTY, EMPTY,
398 /* C (SH64PCREL16PT_32, SH64PCRELPLT) */
399 { 0, 0, SH64PCREL32_LENGTH, 0 },
400 EMPTY, EMPTY,
401 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
403 /* C (SH64PCREL16PT_64, SH64PCREL16) */
404 EMPTY,
405 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16PT_64, SH64PCREL32) },
406 /* C (SH64PCREL16PT_64, SH64PCREL32) */
407 { SH64PCREL32_F,
408 SH64PCREL32_M,
409 SH64PCREL32_LENGTH,
410 C (SH64PCREL16PT_64, SH64PCREL48) },
411 /* C (SH64PCREL16PT_64, SH64PCREL48) */
412 { SH64PCREL48_F, SH64PCREL48_M, SH64PCREL48_LENGTH, C (SH64PCREL16PT_64, SH64PCREL64) },
413 /* C (SH64PCREL16PT_64, SH64PCREL64) */
414 { 0, 0, SH64PCREL64_LENGTH, 0 },
415 /* C (SH64PCREL16PT_64, SH64PCRELPLT) */
416 { 0, 0, SH64PCREL64_LENGTH, 0},
417 EMPTY, EMPTY,
418 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
420 /* C (MOVI_IMM_32, UNDEF_MOVI) */
421 { 0, 0, MOVI_32_LENGTH, 0 },
422 /* C (MOVI_IMM_32, MOVI_16) */
423 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_32, MOVI_32) },
424 /* C (MOVI_IMM_32, MOVI_32) */
425 { MOVI_32_F, MOVI_32_M, MOVI_32_LENGTH, 0 },
426 EMPTY, EMPTY, EMPTY,
427 /* C (MOVI_IMM_32, MOVI_GOTOFF) */
428 { 0, 0, MOVI_32_LENGTH, 0 },
429 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
431 /* C (MOVI_IMM_32_PCREL, MOVI_16) */
432 EMPTY,
433 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_32_PCREL, MOVI_32) },
434 /* C (MOVI_IMM_32_PCREL, MOVI_32) */
435 { 0, 0, MOVI_32_LENGTH, 0 },
436 EMPTY, EMPTY,
437 /* C (MOVI_IMM_32_PCREL, MOVI_PLT) */
438 { 0, 0, MOVI_32_LENGTH, 0 },
439 EMPTY,
440 /* C (MOVI_IMM_32_PCREL, MOVI_GOTPC) */
441 { 0, 0, MOVI_32_LENGTH, 0 },
442 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
444 /* C (MOVI_IMM_64, UNDEF_MOVI) */
445 { 0, 0, MOVI_64_LENGTH, 0 },
446 /* C (MOVI_IMM_64, MOVI_16) */
447 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_64, MOVI_32) },
448 /* C (MOVI_IMM_64, MOVI_32) */
449 { MOVI_32_F, MOVI_32_M, MOVI_32_LENGTH, C (MOVI_IMM_64, MOVI_48) },
450 /* C (MOVI_IMM_64, MOVI_48) */
451 { MOVI_48_F, MOVI_48_M, MOVI_48_LENGTH, C (MOVI_IMM_64, MOVI_64) },
452 /* C (MOVI_IMM_64, MOVI_64) */
453 { 0, 0, MOVI_64_LENGTH, 0 },
454 EMPTY,
455 /* C (MOVI_IMM_64, MOVI_GOTOFF) */
456 { 0, 0, MOVI_64_LENGTH, 0 },
457 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
459 /* C (MOVI_IMM_64_PCREL, MOVI_16) */
460 EMPTY,
461 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_64_PCREL, MOVI_32) },
462 /* C (MOVI_IMM_64_PCREL, MOVI_32) */
463 { MOVI_32_F, MOVI_32_M, MOVI_32_LENGTH, C (MOVI_IMM_64_PCREL, MOVI_48) },
464 /* C (MOVI_IMM_64_PCREL, MOVI_48) */
465 { MOVI_48_F, MOVI_48_M, MOVI_48_LENGTH, C (MOVI_IMM_64_PCREL, MOVI_64) },
466 /* C (MOVI_IMM_64_PCREL, MOVI_64) */
467 { 0, 0, MOVI_64_LENGTH, 0 },
468 /* C (MOVI_IMM_64_PCREL, MOVI_PLT) */
469 { 0, 0, MOVI_64_LENGTH, 0 },
470 EMPTY,
471 /* C (MOVI_IMM_64_PCREL, MOVI_GOTPC) */
472 { 0, 0, MOVI_64_LENGTH, 0 },
473 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
475 #endif /* HAVE_SH64 */
479 #undef EMPTY
481 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
484 #ifdef OBJ_ELF
485 /* Determinet whether the symbol needs any kind of PIC relocation. */
487 inline static int
488 sh_PIC_related_p (symbolS *sym)
490 expressionS *exp;
492 if (! sym)
493 return 0;
495 if (sym == GOT_symbol)
496 return 1;
498 #ifdef HAVE_SH64
499 if (sh_PIC_related_p (*symbol_get_tc (sym)))
500 return 1;
501 #endif
503 exp = symbol_get_value_expression (sym);
505 return (exp->X_op == O_PIC_reloc
506 || sh_PIC_related_p (exp->X_add_symbol)
507 || sh_PIC_related_p (exp->X_op_symbol));
510 /* Determine the relocation type to be used to represent the
511 expression, that may be rearranged. */
513 static int
514 sh_check_fixup (expressionS *main_exp, bfd_reloc_code_real_type *r_type_p)
516 expressionS *exp = main_exp;
518 /* This is here for backward-compatibility only. GCC used to generated:
520 f@PLT + . - (.LPCS# + 2)
522 but we'd rather be able to handle this as a PIC-related reference
523 plus/minus a symbol. However, gas' parser gives us:
525 O_subtract (O_add (f@PLT, .), .LPCS#+2)
527 so we attempt to transform this into:
529 O_subtract (f@PLT, O_subtract (.LPCS#+2, .))
531 which we can handle simply below. */
532 if (exp->X_op == O_subtract)
534 if (sh_PIC_related_p (exp->X_op_symbol))
535 return 1;
537 exp = symbol_get_value_expression (exp->X_add_symbol);
539 if (exp && sh_PIC_related_p (exp->X_op_symbol))
540 return 1;
542 if (exp && exp->X_op == O_add
543 && sh_PIC_related_p (exp->X_add_symbol))
545 symbolS *sym = exp->X_add_symbol;
547 exp->X_op = O_subtract;
548 exp->X_add_symbol = main_exp->X_op_symbol;
550 main_exp->X_op_symbol = main_exp->X_add_symbol;
551 main_exp->X_add_symbol = sym;
553 main_exp->X_add_number += exp->X_add_number;
554 exp->X_add_number = 0;
557 exp = main_exp;
559 else if (exp->X_op == O_add && sh_PIC_related_p (exp->X_op_symbol))
560 return 1;
562 if (exp->X_op == O_symbol || exp->X_op == O_add || exp->X_op == O_subtract)
564 #ifdef HAVE_SH64
565 if (exp->X_add_symbol
566 && (exp->X_add_symbol == GOT_symbol
567 || (GOT_symbol
568 && *symbol_get_tc (exp->X_add_symbol) == GOT_symbol)))
570 switch (*r_type_p)
572 case BFD_RELOC_SH_IMM_LOW16:
573 *r_type_p = BFD_RELOC_SH_GOTPC_LOW16;
574 break;
576 case BFD_RELOC_SH_IMM_MEDLOW16:
577 *r_type_p = BFD_RELOC_SH_GOTPC_MEDLOW16;
578 break;
580 case BFD_RELOC_SH_IMM_MEDHI16:
581 *r_type_p = BFD_RELOC_SH_GOTPC_MEDHI16;
582 break;
584 case BFD_RELOC_SH_IMM_HI16:
585 *r_type_p = BFD_RELOC_SH_GOTPC_HI16;
586 break;
588 case BFD_RELOC_NONE:
589 case BFD_RELOC_UNUSED:
590 *r_type_p = BFD_RELOC_SH_GOTPC;
591 break;
593 default:
594 abort ();
596 return 0;
598 #else
599 if (exp->X_add_symbol && exp->X_add_symbol == GOT_symbol)
601 *r_type_p = BFD_RELOC_SH_GOTPC;
602 return 0;
604 #endif
605 exp = symbol_get_value_expression (exp->X_add_symbol);
606 if (! exp)
607 return 0;
610 if (exp->X_op == O_PIC_reloc)
612 #ifdef HAVE_SH64
613 switch (*r_type_p)
615 case BFD_RELOC_NONE:
616 case BFD_RELOC_UNUSED:
617 *r_type_p = exp->X_md;
618 break;
620 case BFD_RELOC_SH_IMM_LOW16:
621 switch (exp->X_md)
623 case BFD_RELOC_32_GOTOFF:
624 *r_type_p = BFD_RELOC_SH_GOTOFF_LOW16;
625 break;
627 case BFD_RELOC_SH_GOTPLT32:
628 *r_type_p = BFD_RELOC_SH_GOTPLT_LOW16;
629 break;
631 case BFD_RELOC_32_GOT_PCREL:
632 *r_type_p = BFD_RELOC_SH_GOT_LOW16;
633 break;
635 case BFD_RELOC_32_PLT_PCREL:
636 *r_type_p = BFD_RELOC_SH_PLT_LOW16;
637 break;
639 default:
640 abort ();
642 break;
644 case BFD_RELOC_SH_IMM_MEDLOW16:
645 switch (exp->X_md)
647 case BFD_RELOC_32_GOTOFF:
648 *r_type_p = BFD_RELOC_SH_GOTOFF_MEDLOW16;
649 break;
651 case BFD_RELOC_SH_GOTPLT32:
652 *r_type_p = BFD_RELOC_SH_GOTPLT_MEDLOW16;
653 break;
655 case BFD_RELOC_32_GOT_PCREL:
656 *r_type_p = BFD_RELOC_SH_GOT_MEDLOW16;
657 break;
659 case BFD_RELOC_32_PLT_PCREL:
660 *r_type_p = BFD_RELOC_SH_PLT_MEDLOW16;
661 break;
663 default:
664 abort ();
666 break;
668 case BFD_RELOC_SH_IMM_MEDHI16:
669 switch (exp->X_md)
671 case BFD_RELOC_32_GOTOFF:
672 *r_type_p = BFD_RELOC_SH_GOTOFF_MEDHI16;
673 break;
675 case BFD_RELOC_SH_GOTPLT32:
676 *r_type_p = BFD_RELOC_SH_GOTPLT_MEDHI16;
677 break;
679 case BFD_RELOC_32_GOT_PCREL:
680 *r_type_p = BFD_RELOC_SH_GOT_MEDHI16;
681 break;
683 case BFD_RELOC_32_PLT_PCREL:
684 *r_type_p = BFD_RELOC_SH_PLT_MEDHI16;
685 break;
687 default:
688 abort ();
690 break;
692 case BFD_RELOC_SH_IMM_HI16:
693 switch (exp->X_md)
695 case BFD_RELOC_32_GOTOFF:
696 *r_type_p = BFD_RELOC_SH_GOTOFF_HI16;
697 break;
699 case BFD_RELOC_SH_GOTPLT32:
700 *r_type_p = BFD_RELOC_SH_GOTPLT_HI16;
701 break;
703 case BFD_RELOC_32_GOT_PCREL:
704 *r_type_p = BFD_RELOC_SH_GOT_HI16;
705 break;
707 case BFD_RELOC_32_PLT_PCREL:
708 *r_type_p = BFD_RELOC_SH_PLT_HI16;
709 break;
711 default:
712 abort ();
714 break;
716 default:
717 abort ();
719 #else
720 *r_type_p = exp->X_md;
721 #endif
722 if (exp == main_exp)
723 exp->X_op = O_symbol;
724 else
726 main_exp->X_add_symbol = exp->X_add_symbol;
727 main_exp->X_add_number += exp->X_add_number;
730 else
731 return (sh_PIC_related_p (exp->X_add_symbol)
732 || sh_PIC_related_p (exp->X_op_symbol));
734 return 0;
737 /* Add expression EXP of SIZE bytes to offset OFF of fragment FRAG. */
739 void
740 sh_cons_fix_new (fragS *frag, int off, int size, expressionS *exp)
742 bfd_reloc_code_real_type r_type = BFD_RELOC_UNUSED;
744 if (sh_check_fixup (exp, &r_type))
745 as_bad (_("Invalid PIC expression."));
747 if (r_type == BFD_RELOC_UNUSED)
748 switch (size)
750 case 1:
751 r_type = BFD_RELOC_8;
752 break;
754 case 2:
755 r_type = BFD_RELOC_16;
756 break;
758 case 4:
759 r_type = BFD_RELOC_32;
760 break;
762 #ifdef HAVE_SH64
763 case 8:
764 r_type = BFD_RELOC_64;
765 break;
766 #endif
768 default:
769 goto error;
771 else if (size != 4)
773 error:
774 as_bad (_("unsupported BFD relocation size %u"), size);
775 r_type = BFD_RELOC_UNUSED;
778 fix_new_exp (frag, off, size, exp, 0, r_type);
781 /* The regular cons() function, that reads constants, doesn't support
782 suffixes such as @GOT, @GOTOFF and @PLT, that generate
783 machine-specific relocation types. So we must define it here. */
784 /* Clobbers input_line_pointer, checks end-of-line. */
785 /* NBYTES 1=.byte, 2=.word, 4=.long */
786 static void
787 sh_elf_cons (register int nbytes)
789 expressionS exp;
791 #ifdef HAVE_SH64
793 /* Update existing range to include a previous insn, if there was one. */
794 sh64_update_contents_mark (TRUE);
796 /* We need to make sure the contents type is set to data. */
797 sh64_flag_output ();
799 #endif /* HAVE_SH64 */
801 if (is_it_end_of_statement ())
803 demand_empty_rest_of_line ();
804 return;
807 #ifdef md_cons_align
808 md_cons_align (nbytes);
809 #endif
813 expression (&exp);
814 emit_expr (&exp, (unsigned int) nbytes);
816 while (*input_line_pointer++ == ',');
818 input_line_pointer--; /* Put terminator back into stream. */
819 if (*input_line_pointer == '#' || *input_line_pointer == '!')
821 while (! is_end_of_line[(unsigned char) *input_line_pointer++]);
823 else
824 demand_empty_rest_of_line ();
826 #endif /* OBJ_ELF */
829 /* This function is called once, at assembler startup time. This should
830 set up all the tables, etc that the MD part of the assembler needs. */
832 void
833 md_begin (void)
835 const sh_opcode_info *opcode;
836 char *prev_name = "";
837 unsigned int target_arch;
839 target_arch
840 = preset_target_arch ? preset_target_arch : arch_sh_up & ~arch_sh_has_dsp;
841 valid_arch = target_arch;
843 #ifdef HAVE_SH64
844 shmedia_md_begin ();
845 #endif
847 opcode_hash_control = hash_new ();
849 /* Insert unique names into hash table. */
850 for (opcode = sh_table; opcode->name; opcode++)
852 if (strcmp (prev_name, opcode->name) != 0)
854 if (!SH_MERGE_ARCH_SET_VALID (opcode->arch, target_arch))
855 continue;
856 prev_name = opcode->name;
857 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
862 static int reg_m;
863 static int reg_n;
864 static int reg_x, reg_y;
865 static int reg_efg;
866 static int reg_b;
868 #define IDENT_CHAR(c) (ISALNUM (c) || (c) == '_')
870 /* Try to parse a reg name. Return the number of chars consumed. */
872 static int
873 parse_reg (char *src, int *mode, int *reg)
875 char l0 = TOLOWER (src[0]);
876 char l1 = l0 ? TOLOWER (src[1]) : 0;
878 /* We use ! IDENT_CHAR for the next character after the register name, to
879 make sure that we won't accidentally recognize a symbol name such as
880 'sram' or sr_ram as being a reference to the register 'sr'. */
882 if (l0 == 'r')
884 if (l1 == '1')
886 if (src[2] >= '0' && src[2] <= '5'
887 && ! IDENT_CHAR ((unsigned char) src[3]))
889 *mode = A_REG_N;
890 *reg = 10 + src[2] - '0';
891 return 3;
894 if (l1 >= '0' && l1 <= '9'
895 && ! IDENT_CHAR ((unsigned char) src[2]))
897 *mode = A_REG_N;
898 *reg = (l1 - '0');
899 return 2;
901 if (l1 >= '0' && l1 <= '7' && strncasecmp (&src[2], "_bank", 5) == 0
902 && ! IDENT_CHAR ((unsigned char) src[7]))
904 *mode = A_REG_B;
905 *reg = (l1 - '0');
906 return 7;
909 if (l1 == 'e' && ! IDENT_CHAR ((unsigned char) src[2]))
911 *mode = A_RE;
912 return 2;
914 if (l1 == 's' && ! IDENT_CHAR ((unsigned char) src[2]))
916 *mode = A_RS;
917 return 2;
921 if (l0 == 'a')
923 if (l1 == '0')
925 if (! IDENT_CHAR ((unsigned char) src[2]))
927 *mode = DSP_REG_N;
928 *reg = A_A0_NUM;
929 return 2;
931 if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
933 *mode = DSP_REG_N;
934 *reg = A_A0G_NUM;
935 return 3;
938 if (l1 == '1')
940 if (! IDENT_CHAR ((unsigned char) src[2]))
942 *mode = DSP_REG_N;
943 *reg = A_A1_NUM;
944 return 2;
946 if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
948 *mode = DSP_REG_N;
949 *reg = A_A1G_NUM;
950 return 3;
954 if (l1 == 'x' && src[2] >= '0' && src[2] <= '1'
955 && ! IDENT_CHAR ((unsigned char) src[3]))
957 *mode = A_REG_N;
958 *reg = 4 + (l1 - '0');
959 return 3;
961 if (l1 == 'y' && src[2] >= '0' && src[2] <= '1'
962 && ! IDENT_CHAR ((unsigned char) src[3]))
964 *mode = A_REG_N;
965 *reg = 6 + (l1 - '0');
966 return 3;
968 if (l1 == 's' && src[2] >= '0' && src[2] <= '3'
969 && ! IDENT_CHAR ((unsigned char) src[3]))
971 int n = l1 - '0';
973 *mode = A_REG_N;
974 *reg = n | ((~n & 2) << 1);
975 return 3;
979 if (l0 == 'i' && l1 && ! IDENT_CHAR ((unsigned char) src[2]))
981 if (l1 == 's')
983 *mode = A_REG_N;
984 *reg = 8;
985 return 2;
987 if (l1 == 'x')
989 *mode = A_REG_N;
990 *reg = 8;
991 return 2;
993 if (l1 == 'y')
995 *mode = A_REG_N;
996 *reg = 9;
997 return 2;
1001 if (l0 == 'x' && l1 >= '0' && l1 <= '1'
1002 && ! IDENT_CHAR ((unsigned char) src[2]))
1004 *mode = DSP_REG_N;
1005 *reg = A_X0_NUM + l1 - '0';
1006 return 2;
1009 if (l0 == 'y' && l1 >= '0' && l1 <= '1'
1010 && ! IDENT_CHAR ((unsigned char) src[2]))
1012 *mode = DSP_REG_N;
1013 *reg = A_Y0_NUM + l1 - '0';
1014 return 2;
1017 if (l0 == 'm' && l1 >= '0' && l1 <= '1'
1018 && ! IDENT_CHAR ((unsigned char) src[2]))
1020 *mode = DSP_REG_N;
1021 *reg = l1 == '0' ? A_M0_NUM : A_M1_NUM;
1022 return 2;
1025 if (l0 == 's'
1026 && l1 == 's'
1027 && TOLOWER (src[2]) == 'r' && ! IDENT_CHAR ((unsigned char) src[3]))
1029 *mode = A_SSR;
1030 return 3;
1033 if (l0 == 's' && l1 == 'p' && TOLOWER (src[2]) == 'c'
1034 && ! IDENT_CHAR ((unsigned char) src[3]))
1036 *mode = A_SPC;
1037 return 3;
1040 if (l0 == 's' && l1 == 'g' && TOLOWER (src[2]) == 'r'
1041 && ! IDENT_CHAR ((unsigned char) src[3]))
1043 *mode = A_SGR;
1044 return 3;
1047 if (l0 == 'd' && l1 == 's' && TOLOWER (src[2]) == 'r'
1048 && ! IDENT_CHAR ((unsigned char) src[3]))
1050 *mode = A_DSR;
1051 return 3;
1054 if (l0 == 'd' && l1 == 'b' && TOLOWER (src[2]) == 'r'
1055 && ! IDENT_CHAR ((unsigned char) src[3]))
1057 *mode = A_DBR;
1058 return 3;
1061 if (l0 == 's' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
1063 *mode = A_SR;
1064 return 2;
1067 if (l0 == 's' && l1 == 'p' && ! IDENT_CHAR ((unsigned char) src[2]))
1069 *mode = A_REG_N;
1070 *reg = 15;
1071 return 2;
1074 if (l0 == 'p' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
1076 *mode = A_PR;
1077 return 2;
1079 if (l0 == 'p' && l1 == 'c' && ! IDENT_CHAR ((unsigned char) src[2]))
1081 /* Don't use A_DISP_PC here - that would accept stuff like 'mova pc,r0'
1082 and use an uninitialized immediate. */
1083 *mode = A_PC;
1084 return 2;
1086 if (l0 == 'g' && l1 == 'b' && TOLOWER (src[2]) == 'r'
1087 && ! IDENT_CHAR ((unsigned char) src[3]))
1089 *mode = A_GBR;
1090 return 3;
1092 if (l0 == 'v' && l1 == 'b' && TOLOWER (src[2]) == 'r'
1093 && ! IDENT_CHAR ((unsigned char) src[3]))
1095 *mode = A_VBR;
1096 return 3;
1099 if (l0 == 't' && l1 == 'b' && TOLOWER (src[2]) == 'r'
1100 && ! IDENT_CHAR ((unsigned char) src[3]))
1102 *mode = A_TBR;
1103 return 3;
1105 if (l0 == 'm' && l1 == 'a' && TOLOWER (src[2]) == 'c'
1106 && ! IDENT_CHAR ((unsigned char) src[4]))
1108 if (TOLOWER (src[3]) == 'l')
1110 *mode = A_MACL;
1111 return 4;
1113 if (TOLOWER (src[3]) == 'h')
1115 *mode = A_MACH;
1116 return 4;
1119 if (l0 == 'm' && l1 == 'o' && TOLOWER (src[2]) == 'd'
1120 && ! IDENT_CHAR ((unsigned char) src[3]))
1122 *mode = A_MOD;
1123 return 3;
1125 if (l0 == 'f' && l1 == 'r')
1127 if (src[2] == '1')
1129 if (src[3] >= '0' && src[3] <= '5'
1130 && ! IDENT_CHAR ((unsigned char) src[4]))
1132 *mode = F_REG_N;
1133 *reg = 10 + src[3] - '0';
1134 return 4;
1137 if (src[2] >= '0' && src[2] <= '9'
1138 && ! IDENT_CHAR ((unsigned char) src[3]))
1140 *mode = F_REG_N;
1141 *reg = (src[2] - '0');
1142 return 3;
1145 if (l0 == 'd' && l1 == 'r')
1147 if (src[2] == '1')
1149 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
1150 && ! IDENT_CHAR ((unsigned char) src[4]))
1152 *mode = D_REG_N;
1153 *reg = 10 + src[3] - '0';
1154 return 4;
1157 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
1158 && ! IDENT_CHAR ((unsigned char) src[3]))
1160 *mode = D_REG_N;
1161 *reg = (src[2] - '0');
1162 return 3;
1165 if (l0 == 'x' && l1 == 'd')
1167 if (src[2] == '1')
1169 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
1170 && ! IDENT_CHAR ((unsigned char) src[4]))
1172 *mode = X_REG_N;
1173 *reg = 11 + src[3] - '0';
1174 return 4;
1177 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
1178 && ! IDENT_CHAR ((unsigned char) src[3]))
1180 *mode = X_REG_N;
1181 *reg = (src[2] - '0') + 1;
1182 return 3;
1185 if (l0 == 'f' && l1 == 'v')
1187 if (src[2] == '1'&& src[3] == '2' && ! IDENT_CHAR ((unsigned char) src[4]))
1189 *mode = V_REG_N;
1190 *reg = 12;
1191 return 4;
1193 if ((src[2] == '0' || src[2] == '4' || src[2] == '8')
1194 && ! IDENT_CHAR ((unsigned char) src[3]))
1196 *mode = V_REG_N;
1197 *reg = (src[2] - '0');
1198 return 3;
1201 if (l0 == 'f' && l1 == 'p' && TOLOWER (src[2]) == 'u'
1202 && TOLOWER (src[3]) == 'l'
1203 && ! IDENT_CHAR ((unsigned char) src[4]))
1205 *mode = FPUL_N;
1206 return 4;
1209 if (l0 == 'f' && l1 == 'p' && TOLOWER (src[2]) == 's'
1210 && TOLOWER (src[3]) == 'c'
1211 && TOLOWER (src[4]) == 'r' && ! IDENT_CHAR ((unsigned char) src[5]))
1213 *mode = FPSCR_N;
1214 return 5;
1217 if (l0 == 'x' && l1 == 'm' && TOLOWER (src[2]) == 't'
1218 && TOLOWER (src[3]) == 'r'
1219 && TOLOWER (src[4]) == 'x' && ! IDENT_CHAR ((unsigned char) src[5]))
1221 *mode = XMTRX_M4;
1222 return 5;
1225 return 0;
1228 static char *
1229 parse_exp (char *s, sh_operand_info *op)
1231 char *save;
1232 char *new;
1234 save = input_line_pointer;
1235 input_line_pointer = s;
1236 expression (&op->immediate);
1237 if (op->immediate.X_op == O_absent)
1238 as_bad (_("missing operand"));
1239 #ifdef OBJ_ELF
1240 else if (op->immediate.X_op == O_PIC_reloc
1241 || sh_PIC_related_p (op->immediate.X_add_symbol)
1242 || sh_PIC_related_p (op->immediate.X_op_symbol))
1243 as_bad (_("misplaced PIC operand"));
1244 #endif
1245 new = input_line_pointer;
1246 input_line_pointer = save;
1247 return new;
1250 /* The many forms of operand:
1252 Rn Register direct
1253 @Rn Register indirect
1254 @Rn+ Autoincrement
1255 @-Rn Autodecrement
1256 @(disp:4,Rn)
1257 @(disp:8,GBR)
1258 @(disp:8,PC)
1260 @(R0,Rn)
1261 @(R0,GBR)
1263 disp:8
1264 disp:12
1265 #imm8
1266 pr, gbr, vbr, macl, mach
1269 static char *
1270 parse_at (char *src, sh_operand_info *op)
1272 int len;
1273 int mode;
1274 src++;
1275 if (src[0] == '@')
1277 src = parse_at (src, op);
1278 if (op->type == A_DISP_TBR)
1279 op->type = A_DISP2_TBR;
1280 else
1281 as_bad (_("illegal double indirection"));
1283 else if (src[0] == '-')
1285 /* Must be predecrement. */
1286 src++;
1288 len = parse_reg (src, &mode, &(op->reg));
1289 if (mode != A_REG_N)
1290 as_bad (_("illegal register after @-"));
1292 op->type = A_DEC_N;
1293 src += len;
1295 else if (src[0] == '(')
1297 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
1298 @(r0, rn). */
1299 src++;
1300 len = parse_reg (src, &mode, &(op->reg));
1301 if (len && mode == A_REG_N)
1303 src += len;
1304 if (op->reg != 0)
1306 as_bad (_("must be @(r0,...)"));
1308 if (src[0] == ',')
1310 src++;
1311 /* Now can be rn or gbr. */
1312 len = parse_reg (src, &mode, &(op->reg));
1314 else
1316 len = 0;
1318 if (len)
1320 if (mode == A_GBR)
1322 op->type = A_R0_GBR;
1324 else if (mode == A_REG_N)
1326 op->type = A_IND_R0_REG_N;
1328 else
1330 as_bad (_("syntax error in @(r0,...)"));
1333 else
1335 as_bad (_("syntax error in @(r0...)"));
1338 else
1340 /* Must be an @(disp,.. thing). */
1341 src = parse_exp (src, op);
1342 if (src[0] == ',')
1343 src++;
1344 /* Now can be rn, gbr or pc. */
1345 len = parse_reg (src, &mode, &op->reg);
1346 if (len)
1348 if (mode == A_REG_N)
1350 op->type = A_DISP_REG_N;
1352 else if (mode == A_GBR)
1354 op->type = A_DISP_GBR;
1356 else if (mode == A_TBR)
1358 op->type = A_DISP_TBR;
1360 else if (mode == A_PC)
1362 /* We want @(expr, pc) to uniformly address . + expr,
1363 no matter if expr is a constant, or a more complex
1364 expression, e.g. sym-. or sym1-sym2.
1365 However, we also used to accept @(sym,pc)
1366 as addressing sym, i.e. meaning the same as plain sym.
1367 Some existing code does use the @(sym,pc) syntax, so
1368 we give it the old semantics for now, but warn about
1369 its use, so that users have some time to fix their code.
1371 Note that due to this backward compatibility hack,
1372 we'll get unexpected results when @(offset, pc) is used,
1373 and offset is a symbol that is set later to an an address
1374 difference, or an external symbol that is set to an
1375 address difference in another source file, so we want to
1376 eventually remove it. */
1377 if (op->immediate.X_op == O_symbol)
1379 op->type = A_DISP_PC;
1380 as_warn (_("Deprecated syntax."));
1382 else
1384 op->type = A_DISP_PC_ABS;
1385 /* Such operands don't get corrected for PC==.+4, so
1386 make the correction here. */
1387 op->immediate.X_add_number -= 4;
1390 else
1392 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
1395 else
1397 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
1400 src += len;
1401 if (src[0] != ')')
1402 as_bad (_("expecting )"));
1403 else
1404 src++;
1406 else
1408 src += parse_reg (src, &mode, &(op->reg));
1409 if (mode != A_REG_N)
1410 as_bad (_("illegal register after @"));
1412 if (src[0] == '+')
1414 char l0, l1;
1416 src++;
1417 l0 = TOLOWER (src[0]);
1418 l1 = TOLOWER (src[1]);
1420 if ((l0 == 'r' && l1 == '8')
1421 || (l0 == 'i' && (l1 == 'x' || l1 == 's')))
1423 src += 2;
1424 op->type = AX_PMOD_N;
1426 else if ( (l0 == 'r' && l1 == '9')
1427 || (l0 == 'i' && l1 == 'y'))
1429 src += 2;
1430 op->type = AY_PMOD_N;
1432 else
1433 op->type = A_INC_N;
1435 else
1436 op->type = A_IND_N;
1438 return src;
1441 static void
1442 get_operand (char **ptr, sh_operand_info *op)
1444 char *src = *ptr;
1445 int mode = -1;
1446 unsigned int len;
1448 if (src[0] == '#')
1450 src++;
1451 *ptr = parse_exp (src, op);
1452 op->type = A_IMM;
1453 return;
1456 else if (src[0] == '@')
1458 *ptr = parse_at (src, op);
1459 return;
1461 len = parse_reg (src, &mode, &(op->reg));
1462 if (len)
1464 *ptr = src + len;
1465 op->type = mode;
1466 return;
1468 else
1470 /* Not a reg, the only thing left is a displacement. */
1471 *ptr = parse_exp (src, op);
1472 op->type = A_DISP_PC;
1473 return;
1477 static char *
1478 get_operands (sh_opcode_info *info, char *args, sh_operand_info *operand)
1480 char *ptr = args;
1481 if (info->arg[0])
1483 /* The pre-processor will eliminate whitespace in front of '@'
1484 after the first argument; we may be called multiple times
1485 from assemble_ppi, so don't insist on finding whitespace here. */
1486 if (*ptr == ' ')
1487 ptr++;
1489 get_operand (&ptr, operand + 0);
1490 if (info->arg[1])
1492 if (*ptr == ',')
1494 ptr++;
1496 get_operand (&ptr, operand + 1);
1497 /* ??? Hack: psha/pshl have a varying operand number depending on
1498 the type of the first operand. We handle this by having the
1499 three-operand version first and reducing the number of operands
1500 parsed to two if we see that the first operand is an immediate.
1501 This works because no insn with three operands has an immediate
1502 as first operand. */
1503 if (info->arg[2] && operand[0].type != A_IMM)
1505 if (*ptr == ',')
1507 ptr++;
1509 get_operand (&ptr, operand + 2);
1511 else
1513 operand[2].type = 0;
1516 else
1518 operand[1].type = 0;
1519 operand[2].type = 0;
1522 else
1524 operand[0].type = 0;
1525 operand[1].type = 0;
1526 operand[2].type = 0;
1528 return ptr;
1531 /* Passed a pointer to a list of opcodes which use different
1532 addressing modes, return the opcode which matches the opcodes
1533 provided. */
1535 static sh_opcode_info *
1536 get_specific (sh_opcode_info *opcode, sh_operand_info *operands)
1538 sh_opcode_info *this_try = opcode;
1539 char *name = opcode->name;
1540 int n = 0;
1542 while (opcode->name)
1544 this_try = opcode++;
1545 if ((this_try->name != name) && (strcmp (this_try->name, name) != 0))
1547 /* We've looked so far down the table that we've run out of
1548 opcodes with the same name. */
1549 return 0;
1552 /* Look at both operands needed by the opcodes and provided by
1553 the user - since an arg test will often fail on the same arg
1554 again and again, we'll try and test the last failing arg the
1555 first on each opcode try. */
1556 for (n = 0; this_try->arg[n]; n++)
1558 sh_operand_info *user = operands + n;
1559 sh_arg_type arg = this_try->arg[n];
1561 if (SH_MERGE_ARCH_SET_VALID (valid_arch, arch_sh2a_nofpu_up)
1562 && ( arg == A_DISP_REG_M
1563 || arg == A_DISP_REG_N))
1565 /* Check a few key IMM* fields for overflow. */
1566 int opf;
1567 long val = user->immediate.X_add_number;
1569 for (opf = 0; opf < 4; opf ++)
1570 switch (this_try->nibbles[opf])
1572 case IMM0_4:
1573 case IMM1_4:
1574 if (val < 0 || val > 15)
1575 goto fail;
1576 break;
1577 case IMM0_4BY2:
1578 case IMM1_4BY2:
1579 if (val < 0 || val > 15 * 2)
1580 goto fail;
1581 break;
1582 case IMM0_4BY4:
1583 case IMM1_4BY4:
1584 if (val < 0 || val > 15 * 4)
1585 goto fail;
1586 break;
1587 default:
1588 break;
1591 switch (arg)
1593 case A_DISP_PC:
1594 if (user->type == A_DISP_PC_ABS)
1595 break;
1596 /* Fall through. */
1597 case A_IMM:
1598 case A_BDISP12:
1599 case A_BDISP8:
1600 case A_DISP_GBR:
1601 case A_DISP2_TBR:
1602 case A_MACH:
1603 case A_PR:
1604 case A_MACL:
1605 if (user->type != arg)
1606 goto fail;
1607 break;
1608 case A_R0:
1609 /* opcode needs r0 */
1610 if (user->type != A_REG_N || user->reg != 0)
1611 goto fail;
1612 break;
1613 case A_R0_GBR:
1614 if (user->type != A_R0_GBR || user->reg != 0)
1615 goto fail;
1616 break;
1617 case F_FR0:
1618 if (user->type != F_REG_N || user->reg != 0)
1619 goto fail;
1620 break;
1622 case A_REG_N:
1623 case A_INC_N:
1624 case A_DEC_N:
1625 case A_IND_N:
1626 case A_IND_R0_REG_N:
1627 case A_DISP_REG_N:
1628 case F_REG_N:
1629 case D_REG_N:
1630 case X_REG_N:
1631 case V_REG_N:
1632 case FPUL_N:
1633 case FPSCR_N:
1634 case DSP_REG_N:
1635 /* Opcode needs rn */
1636 if (user->type != arg)
1637 goto fail;
1638 reg_n = user->reg;
1639 break;
1640 case DX_REG_N:
1641 if (user->type != D_REG_N && user->type != X_REG_N)
1642 goto fail;
1643 reg_n = user->reg;
1644 break;
1645 case A_GBR:
1646 case A_TBR:
1647 case A_SR:
1648 case A_VBR:
1649 case A_DSR:
1650 case A_MOD:
1651 case A_RE:
1652 case A_RS:
1653 case A_SSR:
1654 case A_SPC:
1655 case A_SGR:
1656 case A_DBR:
1657 if (user->type != arg)
1658 goto fail;
1659 break;
1661 case A_REG_B:
1662 if (user->type != arg)
1663 goto fail;
1664 reg_b = user->reg;
1665 break;
1667 case A_INC_R15:
1668 if (user->type != A_INC_N)
1669 goto fail;
1670 if (user->reg != 15)
1671 goto fail;
1672 reg_n = user->reg;
1673 break;
1675 case A_DEC_R15:
1676 if (user->type != A_DEC_N)
1677 goto fail;
1678 if (user->reg != 15)
1679 goto fail;
1680 reg_n = user->reg;
1681 break;
1683 case A_REG_M:
1684 case A_INC_M:
1685 case A_DEC_M:
1686 case A_IND_M:
1687 case A_IND_R0_REG_M:
1688 case A_DISP_REG_M:
1689 case DSP_REG_M:
1690 /* Opcode needs rn */
1691 if (user->type != arg - A_REG_M + A_REG_N)
1692 goto fail;
1693 reg_m = user->reg;
1694 break;
1696 case AS_DEC_N:
1697 if (user->type != A_DEC_N)
1698 goto fail;
1699 if (user->reg < 2 || user->reg > 5)
1700 goto fail;
1701 reg_n = user->reg;
1702 break;
1704 case AS_INC_N:
1705 if (user->type != A_INC_N)
1706 goto fail;
1707 if (user->reg < 2 || user->reg > 5)
1708 goto fail;
1709 reg_n = user->reg;
1710 break;
1712 case AS_IND_N:
1713 if (user->type != A_IND_N)
1714 goto fail;
1715 if (user->reg < 2 || user->reg > 5)
1716 goto fail;
1717 reg_n = user->reg;
1718 break;
1720 case AS_PMOD_N:
1721 if (user->type != AX_PMOD_N)
1722 goto fail;
1723 if (user->reg < 2 || user->reg > 5)
1724 goto fail;
1725 reg_n = user->reg;
1726 break;
1728 case AX_INC_N:
1729 if (user->type != A_INC_N)
1730 goto fail;
1731 if (user->reg < 4 || user->reg > 5)
1732 goto fail;
1733 reg_n = user->reg;
1734 break;
1736 case AX_IND_N:
1737 if (user->type != A_IND_N)
1738 goto fail;
1739 if (user->reg < 4 || user->reg > 5)
1740 goto fail;
1741 reg_n = user->reg;
1742 break;
1744 case AX_PMOD_N:
1745 if (user->type != AX_PMOD_N)
1746 goto fail;
1747 if (user->reg < 4 || user->reg > 5)
1748 goto fail;
1749 reg_n = user->reg;
1750 break;
1752 case AXY_INC_N:
1753 if (user->type != A_INC_N)
1754 goto fail;
1755 if ((user->reg < 4 || user->reg > 5)
1756 && (user->reg < 0 || user->reg > 1))
1757 goto fail;
1758 reg_n = user->reg;
1759 break;
1761 case AXY_IND_N:
1762 if (user->type != A_IND_N)
1763 goto fail;
1764 if ((user->reg < 4 || user->reg > 5)
1765 && (user->reg < 0 || user->reg > 1))
1766 goto fail;
1767 reg_n = user->reg;
1768 break;
1770 case AXY_PMOD_N:
1771 if (user->type != AX_PMOD_N)
1772 goto fail;
1773 if ((user->reg < 4 || user->reg > 5)
1774 && (user->reg < 0 || user->reg > 1))
1775 goto fail;
1776 reg_n = user->reg;
1777 break;
1779 case AY_INC_N:
1780 if (user->type != A_INC_N)
1781 goto fail;
1782 if (user->reg < 6 || user->reg > 7)
1783 goto fail;
1784 reg_n = user->reg;
1785 break;
1787 case AY_IND_N:
1788 if (user->type != A_IND_N)
1789 goto fail;
1790 if (user->reg < 6 || user->reg > 7)
1791 goto fail;
1792 reg_n = user->reg;
1793 break;
1795 case AY_PMOD_N:
1796 if (user->type != AY_PMOD_N)
1797 goto fail;
1798 if (user->reg < 6 || user->reg > 7)
1799 goto fail;
1800 reg_n = user->reg;
1801 break;
1803 case AYX_INC_N:
1804 if (user->type != A_INC_N)
1805 goto fail;
1806 if ((user->reg < 6 || user->reg > 7)
1807 && (user->reg < 2 || user->reg > 3))
1808 goto fail;
1809 reg_n = user->reg;
1810 break;
1812 case AYX_IND_N:
1813 if (user->type != A_IND_N)
1814 goto fail;
1815 if ((user->reg < 6 || user->reg > 7)
1816 && (user->reg < 2 || user->reg > 3))
1817 goto fail;
1818 reg_n = user->reg;
1819 break;
1821 case AYX_PMOD_N:
1822 if (user->type != AY_PMOD_N)
1823 goto fail;
1824 if ((user->reg < 6 || user->reg > 7)
1825 && (user->reg < 2 || user->reg > 3))
1826 goto fail;
1827 reg_n = user->reg;
1828 break;
1830 case DSP_REG_A_M:
1831 if (user->type != DSP_REG_N)
1832 goto fail;
1833 if (user->reg != A_A0_NUM
1834 && user->reg != A_A1_NUM)
1835 goto fail;
1836 reg_m = user->reg;
1837 break;
1839 case DSP_REG_AX:
1840 if (user->type != DSP_REG_N)
1841 goto fail;
1842 switch (user->reg)
1844 case A_A0_NUM:
1845 reg_x = 0;
1846 break;
1847 case A_A1_NUM:
1848 reg_x = 2;
1849 break;
1850 case A_X0_NUM:
1851 reg_x = 1;
1852 break;
1853 case A_X1_NUM:
1854 reg_x = 3;
1855 break;
1856 default:
1857 goto fail;
1859 break;
1861 case DSP_REG_XY:
1862 if (user->type != DSP_REG_N)
1863 goto fail;
1864 switch (user->reg)
1866 case A_X0_NUM:
1867 reg_x = 0;
1868 break;
1869 case A_X1_NUM:
1870 reg_x = 2;
1871 break;
1872 case A_Y0_NUM:
1873 reg_x = 1;
1874 break;
1875 case A_Y1_NUM:
1876 reg_x = 3;
1877 break;
1878 default:
1879 goto fail;
1881 break;
1883 case DSP_REG_AY:
1884 if (user->type != DSP_REG_N)
1885 goto fail;
1886 switch (user->reg)
1888 case A_A0_NUM:
1889 reg_y = 0;
1890 break;
1891 case A_A1_NUM:
1892 reg_y = 1;
1893 break;
1894 case A_Y0_NUM:
1895 reg_y = 2;
1896 break;
1897 case A_Y1_NUM:
1898 reg_y = 3;
1899 break;
1900 default:
1901 goto fail;
1903 break;
1905 case DSP_REG_YX:
1906 if (user->type != DSP_REG_N)
1907 goto fail;
1908 switch (user->reg)
1910 case A_Y0_NUM:
1911 reg_y = 0;
1912 break;
1913 case A_Y1_NUM:
1914 reg_y = 1;
1915 break;
1916 case A_X0_NUM:
1917 reg_y = 2;
1918 break;
1919 case A_X1_NUM:
1920 reg_y = 3;
1921 break;
1922 default:
1923 goto fail;
1925 break;
1927 case DSP_REG_X:
1928 if (user->type != DSP_REG_N)
1929 goto fail;
1930 switch (user->reg)
1932 case A_X0_NUM:
1933 reg_x = 0;
1934 break;
1935 case A_X1_NUM:
1936 reg_x = 1;
1937 break;
1938 case A_A0_NUM:
1939 reg_x = 2;
1940 break;
1941 case A_A1_NUM:
1942 reg_x = 3;
1943 break;
1944 default:
1945 goto fail;
1947 break;
1949 case DSP_REG_Y:
1950 if (user->type != DSP_REG_N)
1951 goto fail;
1952 switch (user->reg)
1954 case A_Y0_NUM:
1955 reg_y = 0;
1956 break;
1957 case A_Y1_NUM:
1958 reg_y = 1;
1959 break;
1960 case A_M0_NUM:
1961 reg_y = 2;
1962 break;
1963 case A_M1_NUM:
1964 reg_y = 3;
1965 break;
1966 default:
1967 goto fail;
1969 break;
1971 case DSP_REG_E:
1972 if (user->type != DSP_REG_N)
1973 goto fail;
1974 switch (user->reg)
1976 case A_X0_NUM:
1977 reg_efg = 0 << 10;
1978 break;
1979 case A_X1_NUM:
1980 reg_efg = 1 << 10;
1981 break;
1982 case A_Y0_NUM:
1983 reg_efg = 2 << 10;
1984 break;
1985 case A_A1_NUM:
1986 reg_efg = 3 << 10;
1987 break;
1988 default:
1989 goto fail;
1991 break;
1993 case DSP_REG_F:
1994 if (user->type != DSP_REG_N)
1995 goto fail;
1996 switch (user->reg)
1998 case A_Y0_NUM:
1999 reg_efg |= 0 << 8;
2000 break;
2001 case A_Y1_NUM:
2002 reg_efg |= 1 << 8;
2003 break;
2004 case A_X0_NUM:
2005 reg_efg |= 2 << 8;
2006 break;
2007 case A_A1_NUM:
2008 reg_efg |= 3 << 8;
2009 break;
2010 default:
2011 goto fail;
2013 break;
2015 case DSP_REG_G:
2016 if (user->type != DSP_REG_N)
2017 goto fail;
2018 switch (user->reg)
2020 case A_M0_NUM:
2021 reg_efg |= 0 << 2;
2022 break;
2023 case A_M1_NUM:
2024 reg_efg |= 1 << 2;
2025 break;
2026 case A_A0_NUM:
2027 reg_efg |= 2 << 2;
2028 break;
2029 case A_A1_NUM:
2030 reg_efg |= 3 << 2;
2031 break;
2032 default:
2033 goto fail;
2035 break;
2037 case A_A0:
2038 if (user->type != DSP_REG_N || user->reg != A_A0_NUM)
2039 goto fail;
2040 break;
2041 case A_X0:
2042 if (user->type != DSP_REG_N || user->reg != A_X0_NUM)
2043 goto fail;
2044 break;
2045 case A_X1:
2046 if (user->type != DSP_REG_N || user->reg != A_X1_NUM)
2047 goto fail;
2048 break;
2049 case A_Y0:
2050 if (user->type != DSP_REG_N || user->reg != A_Y0_NUM)
2051 goto fail;
2052 break;
2053 case A_Y1:
2054 if (user->type != DSP_REG_N || user->reg != A_Y1_NUM)
2055 goto fail;
2056 break;
2058 case F_REG_M:
2059 case D_REG_M:
2060 case X_REG_M:
2061 case V_REG_M:
2062 case FPUL_M:
2063 case FPSCR_M:
2064 /* Opcode needs rn */
2065 if (user->type != arg - F_REG_M + F_REG_N)
2066 goto fail;
2067 reg_m = user->reg;
2068 break;
2069 case DX_REG_M:
2070 if (user->type != D_REG_N && user->type != X_REG_N)
2071 goto fail;
2072 reg_m = user->reg;
2073 break;
2074 case XMTRX_M4:
2075 if (user->type != XMTRX_M4)
2076 goto fail;
2077 reg_m = 4;
2078 break;
2080 default:
2081 printf (_("unhandled %d\n"), arg);
2082 goto fail;
2085 if ( !SH_MERGE_ARCH_SET_VALID (valid_arch, this_try->arch))
2086 goto fail;
2087 valid_arch = SH_MERGE_ARCH_SET (valid_arch, this_try->arch);
2088 return this_try;
2089 fail:
2093 return 0;
2096 static void
2097 insert (char *where, int how, int pcrel, sh_operand_info *op)
2099 fix_new_exp (frag_now,
2100 where - frag_now->fr_literal,
2102 &op->immediate,
2103 pcrel,
2104 how);
2107 static void
2108 insert4 (char * where, int how, int pcrel, sh_operand_info * op)
2110 fix_new_exp (frag_now,
2111 where - frag_now->fr_literal,
2113 & op->immediate,
2114 pcrel,
2115 how);
2117 static void
2118 build_relax (sh_opcode_info *opcode, sh_operand_info *op)
2120 int high_byte = target_big_endian ? 0 : 1;
2121 char *p;
2123 if (opcode->arg[0] == A_BDISP8)
2125 int what = (opcode->nibbles[1] & 4) ? COND_JUMP_DELAY : COND_JUMP;
2126 p = frag_var (rs_machine_dependent,
2127 md_relax_table[C (what, COND32)].rlx_length,
2128 md_relax_table[C (what, COND8)].rlx_length,
2129 C (what, 0),
2130 op->immediate.X_add_symbol,
2131 op->immediate.X_add_number,
2133 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
2135 else if (opcode->arg[0] == A_BDISP12)
2137 p = frag_var (rs_machine_dependent,
2138 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
2139 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
2140 C (UNCOND_JUMP, 0),
2141 op->immediate.X_add_symbol,
2142 op->immediate.X_add_number,
2144 p[high_byte] = (opcode->nibbles[0] << 4);
2149 /* Insert ldrs & ldre with fancy relocations that relaxation can recognize. */
2151 static char *
2152 insert_loop_bounds (char *output, sh_operand_info *operand)
2154 char *name;
2155 symbolS *end_sym;
2157 /* Since the low byte of the opcode will be overwritten by the reloc, we
2158 can just stash the high byte into both bytes and ignore endianness. */
2159 output[0] = 0x8c;
2160 output[1] = 0x8c;
2161 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
2162 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
2164 if (sh_relax)
2166 static int count = 0;
2168 /* If the last loop insn is a two-byte-insn, it is in danger of being
2169 swapped with the insn after it. To prevent this, create a new
2170 symbol - complete with SH_LABEL reloc - after the last loop insn.
2171 If the last loop insn is four bytes long, the symbol will be
2172 right in the middle, but four byte insns are not swapped anyways. */
2173 /* A REPEAT takes 6 bytes. The SH has a 32 bit address space.
2174 Hence a 9 digit number should be enough to count all REPEATs. */
2175 name = alloca (11);
2176 sprintf (name, "_R%x", count++ & 0x3fffffff);
2177 end_sym = symbol_new (name, undefined_section, 0, &zero_address_frag);
2178 /* Make this a local symbol. */
2179 #ifdef OBJ_COFF
2180 SF_SET_LOCAL (end_sym);
2181 #endif /* OBJ_COFF */
2182 symbol_table_insert (end_sym);
2183 end_sym->sy_value = operand[1].immediate;
2184 end_sym->sy_value.X_add_number += 2;
2185 fix_new (frag_now, frag_now_fix (), 2, end_sym, 0, 1, BFD_RELOC_SH_LABEL);
2188 output = frag_more (2);
2189 output[0] = 0x8e;
2190 output[1] = 0x8e;
2191 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
2192 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
2194 return frag_more (2);
2197 /* Now we know what sort of opcodes it is, let's build the bytes. */
2199 static unsigned int
2200 build_Mytes (sh_opcode_info *opcode, sh_operand_info *operand)
2202 int index;
2203 char nbuf[8];
2204 char *output;
2205 unsigned int size = 2;
2206 int low_byte = target_big_endian ? 1 : 0;
2207 int max_index = 4;
2209 nbuf[0] = 0;
2210 nbuf[1] = 0;
2211 nbuf[2] = 0;
2212 nbuf[3] = 0;
2213 nbuf[4] = 0;
2214 nbuf[5] = 0;
2215 nbuf[6] = 0;
2216 nbuf[7] = 0;
2218 if (SH_MERGE_ARCH_SET (opcode->arch, arch_op32))
2220 output = frag_more (4);
2221 size = 4;
2222 max_index = 8;
2224 else
2225 output = frag_more (2);
2227 for (index = 0; index < max_index; index++)
2229 sh_nibble_type i = opcode->nibbles[index];
2230 if (i < 16)
2232 nbuf[index] = i;
2234 else
2236 switch (i)
2238 case REG_N:
2239 case REG_N_D:
2240 nbuf[index] = reg_n;
2241 break;
2242 case REG_M:
2243 nbuf[index] = reg_m;
2244 break;
2245 case SDT_REG_N:
2246 if (reg_n < 2 || reg_n > 5)
2247 as_bad (_("Invalid register: 'r%d'"), reg_n);
2248 nbuf[index] = (reg_n & 3) | 4;
2249 break;
2250 case REG_NM:
2251 nbuf[index] = reg_n | (reg_m >> 2);
2252 break;
2253 case REG_B:
2254 nbuf[index] = reg_b | 0x08;
2255 break;
2256 case REG_N_B01:
2257 nbuf[index] = reg_n | 0x01;
2258 break;
2259 case IMM0_3s:
2260 nbuf[index] |= 0x08;
2261 case IMM0_3c:
2262 insert (output + low_byte, BFD_RELOC_SH_IMM3, 0, operand);
2263 break;
2264 case IMM0_3Us:
2265 nbuf[index] |= 0x80;
2266 case IMM0_3Uc:
2267 insert (output + low_byte, BFD_RELOC_SH_IMM3U, 0, operand);
2268 break;
2269 case DISP0_12:
2270 insert (output + 2, BFD_RELOC_SH_DISP12, 0, operand);
2271 break;
2272 case DISP0_12BY2:
2273 insert (output + 2, BFD_RELOC_SH_DISP12BY2, 0, operand);
2274 break;
2275 case DISP0_12BY4:
2276 insert (output + 2, BFD_RELOC_SH_DISP12BY4, 0, operand);
2277 break;
2278 case DISP0_12BY8:
2279 insert (output + 2, BFD_RELOC_SH_DISP12BY8, 0, operand);
2280 break;
2281 case DISP1_12:
2282 insert (output + 2, BFD_RELOC_SH_DISP12, 0, operand+1);
2283 break;
2284 case DISP1_12BY2:
2285 insert (output + 2, BFD_RELOC_SH_DISP12BY2, 0, operand+1);
2286 break;
2287 case DISP1_12BY4:
2288 insert (output + 2, BFD_RELOC_SH_DISP12BY4, 0, operand+1);
2289 break;
2290 case DISP1_12BY8:
2291 insert (output + 2, BFD_RELOC_SH_DISP12BY8, 0, operand+1);
2292 break;
2293 case IMM0_20_4:
2294 break;
2295 case IMM0_20:
2296 insert4 (output, BFD_RELOC_SH_DISP20, 0, operand);
2297 break;
2298 case IMM0_20BY8:
2299 insert4 (output, BFD_RELOC_SH_DISP20BY8, 0, operand);
2300 break;
2301 case IMM0_4BY4:
2302 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand);
2303 break;
2304 case IMM0_4BY2:
2305 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand);
2306 break;
2307 case IMM0_4:
2308 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand);
2309 break;
2310 case IMM1_4BY4:
2311 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand + 1);
2312 break;
2313 case IMM1_4BY2:
2314 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand + 1);
2315 break;
2316 case IMM1_4:
2317 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand + 1);
2318 break;
2319 case IMM0_8BY4:
2320 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand);
2321 break;
2322 case IMM0_8BY2:
2323 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand);
2324 break;
2325 case IMM0_8:
2326 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand);
2327 break;
2328 case IMM1_8BY4:
2329 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand + 1);
2330 break;
2331 case IMM1_8BY2:
2332 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand + 1);
2333 break;
2334 case IMM1_8:
2335 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand + 1);
2336 break;
2337 case PCRELIMM_8BY4:
2338 insert (output, BFD_RELOC_SH_PCRELIMM8BY4,
2339 operand->type != A_DISP_PC_ABS, operand);
2340 break;
2341 case PCRELIMM_8BY2:
2342 insert (output, BFD_RELOC_SH_PCRELIMM8BY2,
2343 operand->type != A_DISP_PC_ABS, operand);
2344 break;
2345 case REPEAT:
2346 output = insert_loop_bounds (output, operand);
2347 nbuf[index] = opcode->nibbles[3];
2348 operand += 2;
2349 break;
2350 default:
2351 printf (_("failed for %d\n"), i);
2355 if (!target_big_endian)
2357 output[1] = (nbuf[0] << 4) | (nbuf[1]);
2358 output[0] = (nbuf[2] << 4) | (nbuf[3]);
2360 else
2362 output[0] = (nbuf[0] << 4) | (nbuf[1]);
2363 output[1] = (nbuf[2] << 4) | (nbuf[3]);
2365 if (SH_MERGE_ARCH_SET (opcode->arch, arch_op32))
2367 if (!target_big_endian)
2369 output[3] = (nbuf[4] << 4) | (nbuf[5]);
2370 output[2] = (nbuf[6] << 4) | (nbuf[7]);
2372 else
2374 output[2] = (nbuf[4] << 4) | (nbuf[5]);
2375 output[3] = (nbuf[6] << 4) | (nbuf[7]);
2378 return size;
2381 /* Find an opcode at the start of *STR_P in the hash table, and set
2382 *STR_P to the first character after the last one read. */
2384 static sh_opcode_info *
2385 find_cooked_opcode (char **str_p)
2387 char *str = *str_p;
2388 unsigned char *op_start;
2389 unsigned char *op_end;
2390 char name[20];
2391 int nlen = 0;
2393 /* Drop leading whitespace. */
2394 while (*str == ' ')
2395 str++;
2397 /* Find the op code end.
2398 The pre-processor will eliminate whitespace in front of
2399 any '@' after the first argument; we may be called from
2400 assemble_ppi, so the opcode might be terminated by an '@'. */
2401 for (op_start = op_end = (unsigned char *) str;
2402 *op_end
2403 && nlen < 20
2404 && !is_end_of_line[*op_end] && *op_end != ' ' && *op_end != '@';
2405 op_end++)
2407 unsigned char c = op_start[nlen];
2409 /* The machine independent code will convert CMP/EQ into cmp/EQ
2410 because it thinks the '/' is the end of the symbol. Moreover,
2411 all but the first sub-insn is a parallel processing insn won't
2412 be capitalized. Instead of hacking up the machine independent
2413 code, we just deal with it here. */
2414 c = TOLOWER (c);
2415 name[nlen] = c;
2416 nlen++;
2419 name[nlen] = 0;
2420 *str_p = (char *) op_end;
2422 if (nlen == 0)
2423 as_bad (_("can't find opcode "));
2425 return (sh_opcode_info *) hash_find (opcode_hash_control, name);
2428 /* Assemble a parallel processing insn. */
2429 #define DDT_BASE 0xf000 /* Base value for double data transfer insns */
2431 static unsigned int
2432 assemble_ppi (char *op_end, sh_opcode_info *opcode)
2434 int movx = 0;
2435 int movy = 0;
2436 int cond = 0;
2437 int field_b = 0;
2438 char *output;
2439 int move_code;
2440 unsigned int size;
2442 for (;;)
2444 sh_operand_info operand[3];
2446 /* Some insn ignore one or more register fields, e.g. psts machl,a0.
2447 Make sure we encode a defined insn pattern. */
2448 reg_x = 0;
2449 reg_y = 0;
2450 reg_n = 0;
2452 if (opcode->arg[0] != A_END)
2453 op_end = get_operands (opcode, op_end, operand);
2454 try_another_opcode:
2455 opcode = get_specific (opcode, operand);
2456 if (opcode == 0)
2458 /* Couldn't find an opcode which matched the operands. */
2459 char *where = frag_more (2);
2460 size = 2;
2462 where[0] = 0x0;
2463 where[1] = 0x0;
2464 as_bad (_("invalid operands for opcode"));
2465 return size;
2468 if (opcode->nibbles[0] != PPI)
2469 as_bad (_("insn can't be combined with parallel processing insn"));
2471 switch (opcode->nibbles[1])
2474 case NOPX:
2475 if (movx)
2476 as_bad (_("multiple movx specifications"));
2477 movx = DDT_BASE;
2478 break;
2479 case NOPY:
2480 if (movy)
2481 as_bad (_("multiple movy specifications"));
2482 movy = DDT_BASE;
2483 break;
2485 case MOVX_NOPY:
2486 if (movx)
2487 as_bad (_("multiple movx specifications"));
2488 if ((reg_n < 4 || reg_n > 5)
2489 && (reg_n < 0 || reg_n > 1))
2490 as_bad (_("invalid movx address register"));
2491 if (movy && movy != DDT_BASE)
2492 as_bad (_("insn cannot be combined with non-nopy"));
2493 movx = ((((reg_n & 1) != 0) << 9)
2494 + (((reg_n & 4) == 0) << 8)
2495 + (reg_x << 6)
2496 + (opcode->nibbles[2] << 4)
2497 + opcode->nibbles[3]
2498 + DDT_BASE);
2499 break;
2501 case MOVY_NOPX:
2502 if (movy)
2503 as_bad (_("multiple movy specifications"));
2504 if ((reg_n < 6 || reg_n > 7)
2505 && (reg_n < 2 || reg_n > 3))
2506 as_bad (_("invalid movy address register"));
2507 if (movx && movx != DDT_BASE)
2508 as_bad (_("insn cannot be combined with non-nopx"));
2509 movy = ((((reg_n & 1) != 0) << 8)
2510 + (((reg_n & 4) == 0) << 9)
2511 + (reg_y << 6)
2512 + (opcode->nibbles[2] << 4)
2513 + opcode->nibbles[3]
2514 + DDT_BASE);
2515 break;
2517 case MOVX:
2518 if (movx)
2519 as_bad (_("multiple movx specifications"));
2520 if (movy & 0x2ac)
2521 as_bad (_("previous movy requires nopx"));
2522 if (reg_n < 4 || reg_n > 5)
2523 as_bad (_("invalid movx address register"));
2524 if (opcode->nibbles[2] & 8)
2526 if (reg_m == A_A1_NUM)
2527 movx = 1 << 7;
2528 else if (reg_m != A_A0_NUM)
2529 as_bad (_("invalid movx dsp register"));
2531 else
2533 if (reg_x > 1)
2534 as_bad (_("invalid movx dsp register"));
2535 movx = reg_x << 7;
2537 movx += ((reg_n - 4) << 9) + (opcode->nibbles[2] << 2) + DDT_BASE;
2538 break;
2540 case MOVY:
2541 if (movy)
2542 as_bad (_("multiple movy specifications"));
2543 if (movx & 0x153)
2544 as_bad (_("previous movx requires nopy"));
2545 if (opcode->nibbles[2] & 8)
2547 /* Bit 3 in nibbles[2] is intended for bit 4 of the opcode,
2548 so add 8 more. */
2549 movy = 8;
2550 if (reg_m == A_A1_NUM)
2551 movy += 1 << 6;
2552 else if (reg_m != A_A0_NUM)
2553 as_bad (_("invalid movy dsp register"));
2555 else
2557 if (reg_y > 1)
2558 as_bad (_("invalid movy dsp register"));
2559 movy = reg_y << 6;
2561 if (reg_n < 6 || reg_n > 7)
2562 as_bad (_("invalid movy address register"));
2563 movy += ((reg_n - 6) << 8) + opcode->nibbles[2] + DDT_BASE;
2564 break;
2566 case PSH:
2567 if (operand[0].immediate.X_op != O_constant)
2568 as_bad (_("dsp immediate shift value not constant"));
2569 field_b = ((opcode->nibbles[2] << 12)
2570 | (operand[0].immediate.X_add_number & 127) << 4
2571 | reg_n);
2572 break;
2573 case PPI3NC:
2574 if (cond)
2576 opcode++;
2577 goto try_another_opcode;
2579 /* Fall through. */
2580 case PPI3:
2581 if (field_b)
2582 as_bad (_("multiple parallel processing specifications"));
2583 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2584 + (reg_x << 6) + (reg_y << 4) + reg_n);
2585 switch (opcode->nibbles[4])
2587 case HEX_0:
2588 case HEX_XX00:
2589 case HEX_00YY:
2590 break;
2591 case HEX_1:
2592 case HEX_4:
2593 field_b += opcode->nibbles[4] << 4;
2594 break;
2595 default:
2596 abort ();
2598 break;
2599 case PDC:
2600 if (cond)
2601 as_bad (_("multiple condition specifications"));
2602 cond = opcode->nibbles[2] << 8;
2603 if (*op_end)
2604 goto skip_cond_check;
2605 break;
2606 case PPIC:
2607 if (field_b)
2608 as_bad (_("multiple parallel processing specifications"));
2609 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2610 + cond + (reg_x << 6) + (reg_y << 4) + reg_n);
2611 cond = 0;
2612 switch (opcode->nibbles[4])
2614 case HEX_0:
2615 case HEX_XX00:
2616 case HEX_00YY:
2617 break;
2618 case HEX_1:
2619 case HEX_4:
2620 field_b += opcode->nibbles[4] << 4;
2621 break;
2622 default:
2623 abort ();
2625 break;
2626 case PMUL:
2627 if (field_b)
2629 if ((field_b & 0xef00) == 0xa100)
2630 field_b -= 0x8100;
2631 /* pclr Dz pmuls Se,Sf,Dg */
2632 else if ((field_b & 0xff00) == 0x8d00
2633 && (SH_MERGE_ARCH_SET_VALID (valid_arch, arch_sh4al_dsp_up)))
2635 valid_arch = SH_MERGE_ARCH_SET (valid_arch, arch_sh4al_dsp_up);
2636 field_b -= 0x8cf0;
2638 else
2639 as_bad (_("insn cannot be combined with pmuls"));
2640 switch (field_b & 0xf)
2642 case A_X0_NUM:
2643 field_b += 0 - A_X0_NUM;
2644 break;
2645 case A_Y0_NUM:
2646 field_b += 1 - A_Y0_NUM;
2647 break;
2648 case A_A0_NUM:
2649 field_b += 2 - A_A0_NUM;
2650 break;
2651 case A_A1_NUM:
2652 field_b += 3 - A_A1_NUM;
2653 break;
2654 default:
2655 as_bad (_("bad combined pmuls output operand"));
2657 /* Generate warning if the destination register for padd / psub
2658 and pmuls is the same ( only for A0 or A1 ).
2659 If the last nibble is 1010 then A0 is used in both
2660 padd / psub and pmuls. If it is 1111 then A1 is used
2661 as destination register in both padd / psub and pmuls. */
2663 if ((((field_b | reg_efg) & 0x000F) == 0x000A)
2664 || (((field_b | reg_efg) & 0x000F) == 0x000F))
2665 as_warn (_("destination register is same for parallel insns"));
2667 field_b += 0x4000 + reg_efg;
2668 break;
2669 default:
2670 abort ();
2672 if (cond)
2674 as_bad (_("condition not followed by conditionalizable insn"));
2675 cond = 0;
2677 if (! *op_end)
2678 break;
2679 skip_cond_check:
2680 opcode = find_cooked_opcode (&op_end);
2681 if (opcode == NULL)
2683 (as_bad
2684 (_("unrecognized characters at end of parallel processing insn")));
2685 break;
2689 move_code = movx | movy;
2690 if (field_b)
2692 /* Parallel processing insn. */
2693 unsigned long ppi_code = (movx | movy | 0xf800) << 16 | field_b;
2695 output = frag_more (4);
2696 size = 4;
2697 if (! target_big_endian)
2699 output[3] = ppi_code >> 8;
2700 output[2] = ppi_code;
2702 else
2704 output[2] = ppi_code >> 8;
2705 output[3] = ppi_code;
2707 move_code |= 0xf800;
2709 else
2711 /* Just a double data transfer. */
2712 output = frag_more (2);
2713 size = 2;
2715 if (! target_big_endian)
2717 output[1] = move_code >> 8;
2718 output[0] = move_code;
2720 else
2722 output[0] = move_code >> 8;
2723 output[1] = move_code;
2725 return size;
2728 /* This is the guts of the machine-dependent assembler. STR points to a
2729 machine dependent instruction. This function is supposed to emit
2730 the frags/bytes it assembles to. */
2732 void
2733 md_assemble (char *str)
2735 char *op_end;
2736 sh_operand_info operand[3];
2737 sh_opcode_info *opcode;
2738 unsigned int size = 0;
2739 char *initial_str = str;
2741 #ifdef HAVE_SH64
2742 if (sh64_isa_mode == sh64_isa_shmedia)
2744 shmedia_md_assemble (str);
2745 return;
2747 else
2749 /* If we've seen pseudo-directives, make sure any emitted data or
2750 frags are marked as data. */
2751 if (!seen_insn)
2753 sh64_update_contents_mark (TRUE);
2754 sh64_set_contents_type (CRT_SH5_ISA16);
2757 seen_insn = TRUE;
2759 #endif /* HAVE_SH64 */
2761 opcode = find_cooked_opcode (&str);
2762 op_end = str;
2764 if (opcode == NULL)
2766 /* The opcode is not in the hash table.
2767 This means we definately have an assembly failure,
2768 but the instruction may be valid in another CPU variant.
2769 In this case emit something better than 'unknown opcode'.
2770 Search the full table in sh-opc.h to check. */
2772 char *name = initial_str;
2773 int name_length = 0;
2774 const sh_opcode_info *op;
2775 int found = 0;
2777 /* identify opcode in string */
2778 while (ISSPACE (*name))
2780 name++;
2782 while (!ISSPACE (name[name_length]))
2784 name_length++;
2787 /* search for opcode in full list */
2788 for (op = sh_table; op->name; op++)
2790 if (strncasecmp (op->name, name, name_length) == 0
2791 && op->name[name_length] == '\0')
2793 found = 1;
2794 break;
2798 if ( found )
2800 as_bad (_("opcode not valid for this cpu variant"));
2802 else
2804 as_bad (_("unknown opcode"));
2806 return;
2809 if (sh_relax
2810 && ! seg_info (now_seg)->tc_segment_info_data.in_code)
2812 /* Output a CODE reloc to tell the linker that the following
2813 bytes are instructions, not data. */
2814 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
2815 BFD_RELOC_SH_CODE);
2816 seg_info (now_seg)->tc_segment_info_data.in_code = 1;
2819 if (opcode->nibbles[0] == PPI)
2821 size = assemble_ppi (op_end, opcode);
2823 else
2825 if (opcode->arg[0] == A_BDISP12
2826 || opcode->arg[0] == A_BDISP8)
2828 /* Since we skip get_specific here, we have to check & update
2829 valid_arch now. */
2830 if (SH_MERGE_ARCH_SET_VALID (valid_arch, opcode->arch))
2831 valid_arch = SH_MERGE_ARCH_SET (valid_arch, opcode->arch);
2832 else
2833 as_bad (_("Delayed branches not available on SH1"));
2834 parse_exp (op_end + 1, &operand[0]);
2835 build_relax (opcode, &operand[0]);
2837 else
2839 if (opcode->arg[0] == A_END)
2841 /* Ignore trailing whitespace. If there is any, it has already
2842 been compressed to a single space. */
2843 if (*op_end == ' ')
2844 op_end++;
2846 else
2848 op_end = get_operands (opcode, op_end, operand);
2850 opcode = get_specific (opcode, operand);
2852 if (opcode == 0)
2854 /* Couldn't find an opcode which matched the operands. */
2855 char *where = frag_more (2);
2856 size = 2;
2858 where[0] = 0x0;
2859 where[1] = 0x0;
2860 as_bad (_("invalid operands for opcode"));
2862 else
2864 if (*op_end)
2865 as_bad (_("excess operands: '%s'"), op_end);
2867 size = build_Mytes (opcode, operand);
2872 dwarf2_emit_insn (size);
2875 /* This routine is called each time a label definition is seen. It
2876 emits a BFD_RELOC_SH_LABEL reloc if necessary. */
2878 void
2879 sh_frob_label (void)
2881 static fragS *last_label_frag;
2882 static int last_label_offset;
2884 if (sh_relax
2885 && seg_info (now_seg)->tc_segment_info_data.in_code)
2887 int offset;
2889 offset = frag_now_fix ();
2890 if (frag_now != last_label_frag
2891 || offset != last_label_offset)
2893 fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, BFD_RELOC_SH_LABEL);
2894 last_label_frag = frag_now;
2895 last_label_offset = offset;
2900 /* This routine is called when the assembler is about to output some
2901 data. It emits a BFD_RELOC_SH_DATA reloc if necessary. */
2903 void
2904 sh_flush_pending_output (void)
2906 if (sh_relax
2907 && seg_info (now_seg)->tc_segment_info_data.in_code)
2909 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
2910 BFD_RELOC_SH_DATA);
2911 seg_info (now_seg)->tc_segment_info_data.in_code = 0;
2915 symbolS *
2916 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
2918 return 0;
2921 /* Various routines to kill one day. */
2922 /* Equal to MAX_PRECISION in atof-ieee.c. */
2923 #define MAX_LITTLENUMS 6
2925 /* Turn a string in input_line_pointer into a floating point constant
2926 of type TYPE, and store the appropriate bytes in *LITP. The number
2927 of LITTLENUMS emitted is stored in *SIZEP . An error message is
2928 returned, or NULL on OK. */
2930 char *
2931 md_atof (int type, char *litP, int *sizeP)
2933 int prec;
2934 LITTLENUM_TYPE words[4];
2935 char *t;
2936 int i;
2938 switch (type)
2940 case 'f':
2941 prec = 2;
2942 break;
2944 case 'd':
2945 prec = 4;
2946 break;
2948 default:
2949 *sizeP = 0;
2950 return _("bad call to md_atof");
2953 t = atof_ieee (input_line_pointer, type, words);
2954 if (t)
2955 input_line_pointer = t;
2957 *sizeP = prec * 2;
2959 if (! target_big_endian)
2961 for (i = prec - 1; i >= 0; i--)
2963 md_number_to_chars (litP, (valueT) words[i], 2);
2964 litP += 2;
2967 else
2969 for (i = 0; i < prec; i++)
2971 md_number_to_chars (litP, (valueT) words[i], 2);
2972 litP += 2;
2976 return NULL;
2979 /* Handle the .uses pseudo-op. This pseudo-op is used just before a
2980 call instruction. It refers to a label of the instruction which
2981 loads the register which the call uses. We use it to generate a
2982 special reloc for the linker. */
2984 static void
2985 s_uses (int ignore ATTRIBUTE_UNUSED)
2987 expressionS ex;
2989 if (! sh_relax)
2990 as_warn (_(".uses pseudo-op seen when not relaxing"));
2992 expression (&ex);
2994 if (ex.X_op != O_symbol || ex.X_add_number != 0)
2996 as_bad (_("bad .uses format"));
2997 ignore_rest_of_line ();
2998 return;
3001 fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, BFD_RELOC_SH_USES);
3003 demand_empty_rest_of_line ();
3006 const char *md_shortopts = "";
3007 struct option md_longopts[] =
3009 #define OPTION_RELAX (OPTION_MD_BASE)
3010 #define OPTION_BIG (OPTION_MD_BASE + 1)
3011 #define OPTION_LITTLE (OPTION_BIG + 1)
3012 #define OPTION_SMALL (OPTION_LITTLE + 1)
3013 #define OPTION_DSP (OPTION_SMALL + 1)
3014 #define OPTION_ISA (OPTION_DSP + 1)
3015 #define OPTION_RENESAS (OPTION_ISA + 1)
3017 {"relax", no_argument, NULL, OPTION_RELAX},
3018 {"big", no_argument, NULL, OPTION_BIG},
3019 {"little", no_argument, NULL, OPTION_LITTLE},
3020 {"small", no_argument, NULL, OPTION_SMALL},
3021 {"dsp", no_argument, NULL, OPTION_DSP},
3022 {"isa", required_argument, NULL, OPTION_ISA},
3023 {"renesas", no_argument, NULL, OPTION_RENESAS},
3025 #ifdef HAVE_SH64
3026 #define OPTION_ABI (OPTION_RENESAS + 1)
3027 #define OPTION_NO_MIX (OPTION_ABI + 1)
3028 #define OPTION_SHCOMPACT_CONST_CRANGE (OPTION_NO_MIX + 1)
3029 #define OPTION_NO_EXPAND (OPTION_SHCOMPACT_CONST_CRANGE + 1)
3030 #define OPTION_PT32 (OPTION_NO_EXPAND + 1)
3031 {"abi", required_argument, NULL, OPTION_ABI},
3032 {"no-mix", no_argument, NULL, OPTION_NO_MIX},
3033 {"shcompact-const-crange", no_argument, NULL, OPTION_SHCOMPACT_CONST_CRANGE},
3034 {"no-expand", no_argument, NULL, OPTION_NO_EXPAND},
3035 {"expand-pt32", no_argument, NULL, OPTION_PT32},
3036 #endif /* HAVE_SH64 */
3038 {NULL, no_argument, NULL, 0}
3040 size_t md_longopts_size = sizeof (md_longopts);
3043 md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
3045 switch (c)
3047 case OPTION_RELAX:
3048 sh_relax = 1;
3049 break;
3051 case OPTION_BIG:
3052 target_big_endian = 1;
3053 break;
3055 case OPTION_LITTLE:
3056 target_big_endian = 0;
3057 break;
3059 case OPTION_SMALL:
3060 sh_small = 1;
3061 break;
3063 case OPTION_DSP:
3064 preset_target_arch = arch_sh_up & ~(arch_sh_sp_fpu|arch_sh_dp_fpu);
3065 break;
3067 case OPTION_RENESAS:
3068 dont_adjust_reloc_32 = 1;
3069 break;
3071 case OPTION_ISA:
3072 if (strcasecmp (arg, "dsp") == 0)
3073 preset_target_arch = arch_sh_up & ~(arch_sh_sp_fpu|arch_sh_dp_fpu);
3074 else if (strcasecmp (arg, "fp") == 0)
3075 preset_target_arch = arch_sh_up & ~arch_sh_has_dsp;
3076 else if (strcasecmp (arg, "any") == 0)
3077 preset_target_arch = arch_sh_up;
3078 #ifdef HAVE_SH64
3079 else if (strcasecmp (arg, "shmedia") == 0)
3081 if (sh64_isa_mode == sh64_isa_shcompact)
3082 as_bad (_("Invalid combination: --isa=SHcompact with --isa=SHmedia"));
3083 sh64_isa_mode = sh64_isa_shmedia;
3085 else if (strcasecmp (arg, "shcompact") == 0)
3087 if (sh64_isa_mode == sh64_isa_shmedia)
3088 as_bad (_("Invalid combination: --isa=SHmedia with --isa=SHcompact"));
3089 if (sh64_abi == sh64_abi_64)
3090 as_bad (_("Invalid combination: --abi=64 with --isa=SHcompact"));
3091 sh64_isa_mode = sh64_isa_shcompact;
3093 #endif /* HAVE_SH64 */
3094 else
3096 extern const bfd_arch_info_type bfd_sh_arch;
3097 bfd_arch_info_type const *bfd_arch = &bfd_sh_arch;
3098 preset_target_arch = 0;
3099 for (; bfd_arch; bfd_arch=bfd_arch->next)
3101 int len = strlen(bfd_arch->printable_name);
3103 if (bfd_arch->mach == bfd_mach_sh5)
3104 continue;
3106 if (strncasecmp (bfd_arch->printable_name, arg, len) != 0)
3107 continue;
3109 if (arg[len] == '\0')
3110 preset_target_arch =
3111 sh_get_arch_from_bfd_mach (bfd_arch->mach);
3112 else if (strcasecmp(&arg[len], "-up") == 0)
3113 preset_target_arch =
3114 sh_get_arch_up_from_bfd_mach (bfd_arch->mach);
3115 else
3116 continue;
3117 break;
3120 if (!preset_target_arch)
3121 as_bad ("Invalid argument to --isa option: %s", arg);
3123 break;
3125 #ifdef HAVE_SH64
3126 case OPTION_ABI:
3127 if (strcmp (arg, "32") == 0)
3129 if (sh64_abi == sh64_abi_64)
3130 as_bad (_("Invalid combination: --abi=32 with --abi=64"));
3131 sh64_abi = sh64_abi_32;
3133 else if (strcmp (arg, "64") == 0)
3135 if (sh64_abi == sh64_abi_32)
3136 as_bad (_("Invalid combination: --abi=64 with --abi=32"));
3137 if (sh64_isa_mode == sh64_isa_shcompact)
3138 as_bad (_("Invalid combination: --isa=SHcompact with --abi=64"));
3139 sh64_abi = sh64_abi_64;
3141 else
3142 as_bad ("Invalid argument to --abi option: %s", arg);
3143 break;
3145 case OPTION_NO_MIX:
3146 sh64_mix = FALSE;
3147 break;
3149 case OPTION_SHCOMPACT_CONST_CRANGE:
3150 sh64_shcompact_const_crange = TRUE;
3151 break;
3153 case OPTION_NO_EXPAND:
3154 sh64_expand = FALSE;
3155 break;
3157 case OPTION_PT32:
3158 sh64_pt32 = TRUE;
3159 break;
3160 #endif /* HAVE_SH64 */
3162 default:
3163 return 0;
3166 return 1;
3169 void
3170 md_show_usage (FILE *stream)
3172 fprintf (stream, _("\
3173 SH options:\n\
3174 -little generate little endian code\n\
3175 -big generate big endian code\n\
3176 -relax alter jump instructions for long displacements\n\
3177 -renesas disable optimization with section symbol for\n\
3178 compatibility with Renesas assembler.\n\
3179 -small align sections to 4 byte boundaries, not 16\n\
3180 -dsp enable sh-dsp insns, and disable floating-point ISAs.\n\
3181 -isa=[any use most appropriate isa\n\
3182 | dsp same as '-dsp'\n\
3183 | fp"));
3185 extern const bfd_arch_info_type bfd_sh_arch;
3186 bfd_arch_info_type const *bfd_arch = &bfd_sh_arch;
3187 for (; bfd_arch; bfd_arch=bfd_arch->next)
3188 if (bfd_arch->mach != bfd_mach_sh5)
3190 fprintf (stream, "\n | %s", bfd_arch->printable_name);
3191 fprintf (stream, "\n | %s-up", bfd_arch->printable_name);
3194 fprintf (stream, "]\n");
3195 #ifdef HAVE_SH64
3196 fprintf (stream, _("\
3197 -isa=[shmedia set as the default instruction set for SH64\n\
3198 | SHmedia\n\
3199 | shcompact\n\
3200 | SHcompact]\n"));
3201 fprintf (stream, _("\
3202 -abi=[32|64] set size of expanded SHmedia operands and object\n\
3203 file type\n\
3204 -shcompact-const-crange emit code-range descriptors for constants in\n\
3205 SHcompact code sections\n\
3206 -no-mix disallow SHmedia code in the same section as\n\
3207 constants and SHcompact code\n\
3208 -no-expand do not expand MOVI, PT, PTA or PTB instructions\n\
3209 -expand-pt32 with -abi=64, expand PT, PTA and PTB instructions\n\
3210 to 32 bits only\n"));
3211 #endif /* HAVE_SH64 */
3214 /* This struct is used to pass arguments to sh_count_relocs through
3215 bfd_map_over_sections. */
3217 struct sh_count_relocs
3219 /* Symbol we are looking for. */
3220 symbolS *sym;
3221 /* Count of relocs found. */
3222 int count;
3225 /* Count the number of fixups in a section which refer to a particular
3226 symbol. This is called via bfd_map_over_sections. */
3228 static void
3229 sh_count_relocs (bfd *abfd ATTRIBUTE_UNUSED, segT sec, void *data)
3231 struct sh_count_relocs *info = (struct sh_count_relocs *) data;
3232 segment_info_type *seginfo;
3233 symbolS *sym;
3234 fixS *fix;
3236 seginfo = seg_info (sec);
3237 if (seginfo == NULL)
3238 return;
3240 sym = info->sym;
3241 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
3243 if (fix->fx_addsy == sym)
3245 ++info->count;
3246 fix->fx_tcbit = 1;
3251 /* Handle the count relocs for a particular section.
3252 This is called via bfd_map_over_sections. */
3254 static void
3255 sh_frob_section (bfd *abfd ATTRIBUTE_UNUSED, segT sec,
3256 void *ignore ATTRIBUTE_UNUSED)
3258 segment_info_type *seginfo;
3259 fixS *fix;
3261 seginfo = seg_info (sec);
3262 if (seginfo == NULL)
3263 return;
3265 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
3267 symbolS *sym;
3268 bfd_vma val;
3269 fixS *fscan;
3270 struct sh_count_relocs info;
3272 if (fix->fx_r_type != BFD_RELOC_SH_USES)
3273 continue;
3275 /* The BFD_RELOC_SH_USES reloc should refer to a defined local
3276 symbol in the same section. */
3277 sym = fix->fx_addsy;
3278 if (sym == NULL
3279 || fix->fx_subsy != NULL
3280 || fix->fx_addnumber != 0
3281 || S_GET_SEGMENT (sym) != sec
3282 || S_IS_EXTERNAL (sym))
3284 as_warn_where (fix->fx_file, fix->fx_line,
3285 _(".uses does not refer to a local symbol in the same section"));
3286 continue;
3289 /* Look through the fixups again, this time looking for one
3290 at the same location as sym. */
3291 val = S_GET_VALUE (sym);
3292 for (fscan = seginfo->fix_root;
3293 fscan != NULL;
3294 fscan = fscan->fx_next)
3295 if (val == fscan->fx_frag->fr_address + fscan->fx_where
3296 && fscan->fx_r_type != BFD_RELOC_SH_ALIGN
3297 && fscan->fx_r_type != BFD_RELOC_SH_CODE
3298 && fscan->fx_r_type != BFD_RELOC_SH_DATA
3299 && fscan->fx_r_type != BFD_RELOC_SH_LABEL)
3300 break;
3301 if (fscan == NULL)
3303 as_warn_where (fix->fx_file, fix->fx_line,
3304 _("can't find fixup pointed to by .uses"));
3305 continue;
3308 if (fscan->fx_tcbit)
3310 /* We've already done this one. */
3311 continue;
3314 /* The variable fscan should also be a fixup to a local symbol
3315 in the same section. */
3316 sym = fscan->fx_addsy;
3317 if (sym == NULL
3318 || fscan->fx_subsy != NULL
3319 || fscan->fx_addnumber != 0
3320 || S_GET_SEGMENT (sym) != sec
3321 || S_IS_EXTERNAL (sym))
3323 as_warn_where (fix->fx_file, fix->fx_line,
3324 _(".uses target does not refer to a local symbol in the same section"));
3325 continue;
3328 /* Now we look through all the fixups of all the sections,
3329 counting the number of times we find a reference to sym. */
3330 info.sym = sym;
3331 info.count = 0;
3332 bfd_map_over_sections (stdoutput, sh_count_relocs, &info);
3334 if (info.count < 1)
3335 abort ();
3337 /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
3338 We have already adjusted the value of sym to include the
3339 fragment address, so we undo that adjustment here. */
3340 subseg_change (sec, 0);
3341 fix_new (fscan->fx_frag,
3342 S_GET_VALUE (sym) - fscan->fx_frag->fr_address,
3343 4, &abs_symbol, info.count, 0, BFD_RELOC_SH_COUNT);
3347 /* This function is called after the symbol table has been completed,
3348 but before the relocs or section contents have been written out.
3349 If we have seen any .uses pseudo-ops, they point to an instruction
3350 which loads a register with the address of a function. We look
3351 through the fixups to find where the function address is being
3352 loaded from. We then generate a COUNT reloc giving the number of
3353 times that function address is referred to. The linker uses this
3354 information when doing relaxing, to decide when it can eliminate
3355 the stored function address entirely. */
3357 void
3358 sh_frob_file (void)
3360 #ifdef HAVE_SH64
3361 shmedia_frob_file_before_adjust ();
3362 #endif
3364 if (! sh_relax)
3365 return;
3367 bfd_map_over_sections (stdoutput, sh_frob_section, NULL);
3370 /* Called after relaxing. Set the correct sizes of the fragments, and
3371 create relocs so that md_apply_fix will fill in the correct values. */
3373 void
3374 md_convert_frag (bfd *headers ATTRIBUTE_UNUSED, segT seg, fragS *fragP)
3376 int donerelax = 0;
3378 switch (fragP->fr_subtype)
3380 case C (COND_JUMP, COND8):
3381 case C (COND_JUMP_DELAY, COND8):
3382 subseg_change (seg, 0);
3383 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
3384 1, BFD_RELOC_SH_PCDISP8BY2);
3385 fragP->fr_fix += 2;
3386 fragP->fr_var = 0;
3387 break;
3389 case C (UNCOND_JUMP, UNCOND12):
3390 subseg_change (seg, 0);
3391 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
3392 1, BFD_RELOC_SH_PCDISP12BY2);
3393 fragP->fr_fix += 2;
3394 fragP->fr_var = 0;
3395 break;
3397 case C (UNCOND_JUMP, UNCOND32):
3398 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
3399 if (fragP->fr_symbol == NULL)
3400 as_bad_where (fragP->fr_file, fragP->fr_line,
3401 _("displacement overflows 12-bit field"));
3402 else if (S_IS_DEFINED (fragP->fr_symbol))
3403 as_bad_where (fragP->fr_file, fragP->fr_line,
3404 _("displacement to defined symbol %s overflows 12-bit field"),
3405 S_GET_NAME (fragP->fr_symbol));
3406 else
3407 as_bad_where (fragP->fr_file, fragP->fr_line,
3408 _("displacement to undefined symbol %s overflows 12-bit field"),
3409 S_GET_NAME (fragP->fr_symbol));
3410 /* Stabilize this frag, so we don't trip an assert. */
3411 fragP->fr_fix += fragP->fr_var;
3412 fragP->fr_var = 0;
3413 break;
3415 case C (COND_JUMP, COND12):
3416 case C (COND_JUMP_DELAY, COND12):
3417 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop. */
3418 /* I found that a relax failure for gcc.c-torture/execute/930628-1.c
3419 was due to gas incorrectly relaxing an out-of-range conditional
3420 branch with delay slot. It turned:
3421 bf.s L6 (slot mov.l r12,@(44,r0))
3422 into:
3424 2c: 8f 01 a0 8b bf.s 32 <_main+32> (slot bra L6)
3425 30: 00 09 nop
3426 32: 10 cb mov.l r12,@(44,r0)
3427 Therefore, branches with delay slots have to be handled
3428 differently from ones without delay slots. */
3430 unsigned char *buffer =
3431 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
3432 int highbyte = target_big_endian ? 0 : 1;
3433 int lowbyte = target_big_endian ? 1 : 0;
3434 int delay = fragP->fr_subtype == C (COND_JUMP_DELAY, COND12);
3436 /* Toggle the true/false bit of the bcond. */
3437 buffer[highbyte] ^= 0x2;
3439 /* If this is a delayed branch, we may not put the bra in the
3440 slot. So we change it to a non-delayed branch, like that:
3441 b! cond slot_label; bra disp; slot_label: slot_insn
3442 ??? We should try if swapping the conditional branch and
3443 its delay-slot insn already makes the branch reach. */
3445 /* Build a relocation to six / four bytes farther on. */
3446 subseg_change (seg, 0);
3447 fix_new (fragP, fragP->fr_fix, 2, section_symbol (seg),
3448 fragP->fr_address + fragP->fr_fix + (delay ? 4 : 6),
3449 1, BFD_RELOC_SH_PCDISP8BY2);
3451 /* Set up a jump instruction. */
3452 buffer[highbyte + 2] = 0xa0;
3453 buffer[lowbyte + 2] = 0;
3454 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
3455 fragP->fr_offset, 1, BFD_RELOC_SH_PCDISP12BY2);
3457 if (delay)
3459 buffer[highbyte] &= ~0x4; /* Removes delay slot from branch. */
3460 fragP->fr_fix += 4;
3462 else
3464 /* Fill in a NOP instruction. */
3465 buffer[highbyte + 4] = 0x0;
3466 buffer[lowbyte + 4] = 0x9;
3468 fragP->fr_fix += 6;
3470 fragP->fr_var = 0;
3471 donerelax = 1;
3473 break;
3475 case C (COND_JUMP, COND32):
3476 case C (COND_JUMP_DELAY, COND32):
3477 case C (COND_JUMP, UNDEF_WORD_DISP):
3478 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
3479 if (fragP->fr_symbol == NULL)
3480 as_bad_where (fragP->fr_file, fragP->fr_line,
3481 _("displacement overflows 8-bit field"));
3482 else if (S_IS_DEFINED (fragP->fr_symbol))
3483 as_bad_where (fragP->fr_file, fragP->fr_line,
3484 _("displacement to defined symbol %s overflows 8-bit field"),
3485 S_GET_NAME (fragP->fr_symbol));
3486 else
3487 as_bad_where (fragP->fr_file, fragP->fr_line,
3488 _("displacement to undefined symbol %s overflows 8-bit field "),
3489 S_GET_NAME (fragP->fr_symbol));
3490 /* Stabilize this frag, so we don't trip an assert. */
3491 fragP->fr_fix += fragP->fr_var;
3492 fragP->fr_var = 0;
3493 break;
3495 default:
3496 #ifdef HAVE_SH64
3497 shmedia_md_convert_frag (headers, seg, fragP, TRUE);
3498 #else
3499 abort ();
3500 #endif
3503 if (donerelax && !sh_relax)
3504 as_warn_where (fragP->fr_file, fragP->fr_line,
3505 _("overflow in branch to %s; converted into longer instruction sequence"),
3506 (fragP->fr_symbol != NULL
3507 ? S_GET_NAME (fragP->fr_symbol)
3508 : ""));
3511 valueT
3512 md_section_align (segT seg ATTRIBUTE_UNUSED, valueT size)
3514 #ifdef OBJ_ELF
3515 return size;
3516 #else /* ! OBJ_ELF */
3517 return ((size + (1 << bfd_get_section_alignment (stdoutput, seg)) - 1)
3518 & (-1 << bfd_get_section_alignment (stdoutput, seg)));
3519 #endif /* ! OBJ_ELF */
3522 /* This static variable is set by s_uacons to tell sh_cons_align that
3523 the expression does not need to be aligned. */
3525 static int sh_no_align_cons = 0;
3527 /* This handles the unaligned space allocation pseudo-ops, such as
3528 .uaword. .uaword is just like .word, but the value does not need
3529 to be aligned. */
3531 static void
3532 s_uacons (int bytes)
3534 /* Tell sh_cons_align not to align this value. */
3535 sh_no_align_cons = 1;
3536 cons (bytes);
3539 /* If a .word, et. al., pseud-op is seen, warn if the value is not
3540 aligned correctly. Note that this can cause warnings to be issued
3541 when assembling initialized structured which were declared with the
3542 packed attribute. FIXME: Perhaps we should require an option to
3543 enable this warning? */
3545 void
3546 sh_cons_align (int nbytes)
3548 int nalign;
3549 char *p;
3551 if (sh_no_align_cons)
3553 /* This is an unaligned pseudo-op. */
3554 sh_no_align_cons = 0;
3555 return;
3558 nalign = 0;
3559 while ((nbytes & 1) == 0)
3561 ++nalign;
3562 nbytes >>= 1;
3565 if (nalign == 0)
3566 return;
3568 if (now_seg == absolute_section)
3570 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
3571 as_warn (_("misaligned data"));
3572 return;
3575 p = frag_var (rs_align_test, 1, 1, (relax_substateT) 0,
3576 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
3578 record_alignment (now_seg, nalign);
3581 /* When relaxing, we need to output a reloc for any .align directive
3582 that requests alignment to a four byte boundary or larger. This is
3583 also where we check for misaligned data. */
3585 void
3586 sh_handle_align (fragS *frag)
3588 int bytes = frag->fr_next->fr_address - frag->fr_address - frag->fr_fix;
3590 if (frag->fr_type == rs_align_code)
3592 static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
3593 static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
3595 char *p = frag->fr_literal + frag->fr_fix;
3597 if (bytes & 1)
3599 *p++ = 0;
3600 bytes--;
3601 frag->fr_fix += 1;
3604 if (target_big_endian)
3606 memcpy (p, big_nop_pattern, sizeof big_nop_pattern);
3607 frag->fr_var = sizeof big_nop_pattern;
3609 else
3611 memcpy (p, little_nop_pattern, sizeof little_nop_pattern);
3612 frag->fr_var = sizeof little_nop_pattern;
3615 else if (frag->fr_type == rs_align_test)
3617 if (bytes != 0)
3618 as_warn_where (frag->fr_file, frag->fr_line, _("misaligned data"));
3621 if (sh_relax
3622 && (frag->fr_type == rs_align
3623 || frag->fr_type == rs_align_code)
3624 && frag->fr_address + frag->fr_fix > 0
3625 && frag->fr_offset > 1
3626 && now_seg != bss_section)
3627 fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
3628 BFD_RELOC_SH_ALIGN);
3631 /* See whether the relocation should be resolved locally. */
3633 static bfd_boolean
3634 sh_local_pcrel (fixS *fix)
3636 return (! sh_relax
3637 && (fix->fx_r_type == BFD_RELOC_SH_PCDISP8BY2
3638 || fix->fx_r_type == BFD_RELOC_SH_PCDISP12BY2
3639 || fix->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY2
3640 || fix->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY4
3641 || fix->fx_r_type == BFD_RELOC_8_PCREL
3642 || fix->fx_r_type == BFD_RELOC_SH_SWITCH16
3643 || fix->fx_r_type == BFD_RELOC_SH_SWITCH32));
3646 /* See whether we need to force a relocation into the output file.
3647 This is used to force out switch and PC relative relocations when
3648 relaxing. */
3651 sh_force_relocation (fixS *fix)
3653 /* These relocations can't make it into a DSO, so no use forcing
3654 them for global symbols. */
3655 if (sh_local_pcrel (fix))
3656 return 0;
3658 /* Make sure some relocations get emitted. */
3659 if (fix->fx_r_type == BFD_RELOC_SH_LOOP_START
3660 || fix->fx_r_type == BFD_RELOC_SH_LOOP_END
3661 || fix->fx_r_type == BFD_RELOC_SH_TLS_GD_32
3662 || fix->fx_r_type == BFD_RELOC_SH_TLS_LD_32
3663 || fix->fx_r_type == BFD_RELOC_SH_TLS_IE_32
3664 || fix->fx_r_type == BFD_RELOC_SH_TLS_LDO_32
3665 || fix->fx_r_type == BFD_RELOC_SH_TLS_LE_32
3666 || generic_force_reloc (fix))
3667 return 1;
3669 if (! sh_relax)
3670 return 0;
3672 return (fix->fx_pcrel
3673 || SWITCH_TABLE (fix)
3674 || fix->fx_r_type == BFD_RELOC_SH_COUNT
3675 || fix->fx_r_type == BFD_RELOC_SH_ALIGN
3676 || fix->fx_r_type == BFD_RELOC_SH_CODE
3677 || fix->fx_r_type == BFD_RELOC_SH_DATA
3678 #ifdef HAVE_SH64
3679 || fix->fx_r_type == BFD_RELOC_SH_SHMEDIA_CODE
3680 #endif
3681 || fix->fx_r_type == BFD_RELOC_SH_LABEL);
3684 #ifdef OBJ_ELF
3685 bfd_boolean
3686 sh_fix_adjustable (fixS *fixP)
3688 if (fixP->fx_r_type == BFD_RELOC_32_PLT_PCREL
3689 || fixP->fx_r_type == BFD_RELOC_32_GOT_PCREL
3690 || fixP->fx_r_type == BFD_RELOC_SH_GOTPC
3691 || ((fixP->fx_r_type == BFD_RELOC_32) && dont_adjust_reloc_32)
3692 || fixP->fx_r_type == BFD_RELOC_RVA)
3693 return 0;
3695 /* We need the symbol name for the VTABLE entries */
3696 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3697 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3698 return 0;
3700 return 1;
3703 void
3704 sh_elf_final_processing (void)
3706 int val;
3708 /* Set file-specific flags to indicate if this code needs
3709 a processor with the sh-dsp / sh2e ISA to execute. */
3710 #ifdef HAVE_SH64
3711 /* SH5 and above don't know about the valid_arch arch_sh* bits defined
3712 in sh-opc.h, so check SH64 mode before checking valid_arch. */
3713 if (sh64_isa_mode != sh64_isa_unspecified)
3714 val = EF_SH5;
3715 else
3716 #elif defined TARGET_SYMBIAN
3717 if (1)
3719 extern int sh_symbian_find_elf_flags (unsigned int);
3721 val = sh_symbian_find_elf_flags (valid_arch);
3723 else
3724 #endif /* HAVE_SH64 */
3725 val = sh_find_elf_flags (valid_arch);
3727 elf_elfheader (stdoutput)->e_flags &= ~EF_SH_MACH_MASK;
3728 elf_elfheader (stdoutput)->e_flags |= val;
3730 #endif
3732 /* Apply a fixup to the object file. */
3734 void
3735 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
3737 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
3738 int lowbyte = target_big_endian ? 1 : 0;
3739 int highbyte = target_big_endian ? 0 : 1;
3740 long val = (long) *valP;
3741 long max, min;
3742 int shift;
3744 /* A difference between two symbols, the second of which is in the
3745 current section, is transformed in a PC-relative relocation to
3746 the other symbol. We have to adjust the relocation type here. */
3747 if (fixP->fx_pcrel)
3749 switch (fixP->fx_r_type)
3751 default:
3752 break;
3754 case BFD_RELOC_32:
3755 fixP->fx_r_type = BFD_RELOC_32_PCREL;
3756 break;
3758 /* Currently, we only support 32-bit PCREL relocations.
3759 We'd need a new reloc type to handle 16_PCREL, and
3760 8_PCREL is already taken for R_SH_SWITCH8, which
3761 apparently does something completely different than what
3762 we need. FIXME. */
3763 case BFD_RELOC_16:
3764 bfd_set_error (bfd_error_bad_value);
3765 return;
3767 case BFD_RELOC_8:
3768 bfd_set_error (bfd_error_bad_value);
3769 return;
3773 /* The function adjust_reloc_syms won't convert a reloc against a weak
3774 symbol into a reloc against a section, but bfd_install_relocation
3775 will screw up if the symbol is defined, so we have to adjust val here
3776 to avoid the screw up later.
3778 For ordinary relocs, this does not happen for ELF, since for ELF,
3779 bfd_install_relocation uses the "special function" field of the
3780 howto, and does not execute the code that needs to be undone, as long
3781 as the special function does not return bfd_reloc_continue.
3782 It can happen for GOT- and PLT-type relocs the way they are
3783 described in elf32-sh.c as they use bfd_elf_generic_reloc, but it
3784 doesn't matter here since those relocs don't use VAL; see below. */
3785 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3786 && fixP->fx_addsy != NULL
3787 && S_IS_WEAK (fixP->fx_addsy))
3788 val -= S_GET_VALUE (fixP->fx_addsy);
3790 if (SWITCH_TABLE (fixP))
3791 val -= S_GET_VALUE (fixP->fx_subsy);
3793 max = min = 0;
3794 shift = 0;
3795 switch (fixP->fx_r_type)
3797 case BFD_RELOC_SH_IMM3:
3798 max = 0x7;
3799 * buf = (* buf & 0xf8) | (val & 0x7);
3800 break;
3801 case BFD_RELOC_SH_IMM3U:
3802 max = 0x7;
3803 * buf = (* buf & 0x8f) | ((val & 0x7) << 4);
3804 break;
3805 case BFD_RELOC_SH_DISP12:
3806 max = 0xfff;
3807 buf[lowbyte] = val & 0xff;
3808 buf[highbyte] |= (val >> 8) & 0x0f;
3809 break;
3810 case BFD_RELOC_SH_DISP12BY2:
3811 max = 0xfff;
3812 shift = 1;
3813 buf[lowbyte] = (val >> 1) & 0xff;
3814 buf[highbyte] |= (val >> 9) & 0x0f;
3815 break;
3816 case BFD_RELOC_SH_DISP12BY4:
3817 max = 0xfff;
3818 shift = 2;
3819 buf[lowbyte] = (val >> 2) & 0xff;
3820 buf[highbyte] |= (val >> 10) & 0x0f;
3821 break;
3822 case BFD_RELOC_SH_DISP12BY8:
3823 max = 0xfff;
3824 shift = 3;
3825 buf[lowbyte] = (val >> 3) & 0xff;
3826 buf[highbyte] |= (val >> 11) & 0x0f;
3827 break;
3828 case BFD_RELOC_SH_DISP20:
3829 if (! target_big_endian)
3830 abort();
3831 max = 0x7ffff;
3832 min = -0x80000;
3833 buf[1] = (buf[1] & 0x0f) | ((val >> 12) & 0xf0);
3834 buf[2] = (val >> 8) & 0xff;
3835 buf[3] = val & 0xff;
3836 break;
3837 case BFD_RELOC_SH_DISP20BY8:
3838 if (!target_big_endian)
3839 abort();
3840 max = 0x7ffff;
3841 min = -0x80000;
3842 shift = 8;
3843 buf[1] = (buf[1] & 0x0f) | ((val >> 20) & 0xf0);
3844 buf[2] = (val >> 16) & 0xff;
3845 buf[3] = (val >> 8) & 0xff;
3846 break;
3848 case BFD_RELOC_SH_IMM4:
3849 max = 0xf;
3850 *buf = (*buf & 0xf0) | (val & 0xf);
3851 break;
3853 case BFD_RELOC_SH_IMM4BY2:
3854 max = 0xf;
3855 shift = 1;
3856 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
3857 break;
3859 case BFD_RELOC_SH_IMM4BY4:
3860 max = 0xf;
3861 shift = 2;
3862 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
3863 break;
3865 case BFD_RELOC_SH_IMM8BY2:
3866 max = 0xff;
3867 shift = 1;
3868 *buf = val >> 1;
3869 break;
3871 case BFD_RELOC_SH_IMM8BY4:
3872 max = 0xff;
3873 shift = 2;
3874 *buf = val >> 2;
3875 break;
3877 case BFD_RELOC_8:
3878 case BFD_RELOC_SH_IMM8:
3879 /* Sometimes the 8 bit value is sign extended (e.g., add) and
3880 sometimes it is not (e.g., and). We permit any 8 bit value.
3881 Note that adding further restrictions may invalidate
3882 reasonable looking assembly code, such as ``and -0x1,r0''. */
3883 max = 0xff;
3884 min = -0xff;
3885 *buf++ = val;
3886 break;
3888 case BFD_RELOC_SH_PCRELIMM8BY4:
3889 /* The lower two bits of the PC are cleared before the
3890 displacement is added in. We can assume that the destination
3891 is on a 4 byte boundary. If this instruction is also on a 4
3892 byte boundary, then we want
3893 (target - here) / 4
3894 and target - here is a multiple of 4.
3895 Otherwise, we are on a 2 byte boundary, and we want
3896 (target - (here - 2)) / 4
3897 and target - here is not a multiple of 4. Computing
3898 (target - (here - 2)) / 4 == (target - here + 2) / 4
3899 works for both cases, since in the first case the addition of
3900 2 will be removed by the division. target - here is in the
3901 variable val. */
3902 val = (val + 2) / 4;
3903 if (val & ~0xff)
3904 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3905 buf[lowbyte] = val;
3906 break;
3908 case BFD_RELOC_SH_PCRELIMM8BY2:
3909 val /= 2;
3910 if (val & ~0xff)
3911 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3912 buf[lowbyte] = val;
3913 break;
3915 case BFD_RELOC_SH_PCDISP8BY2:
3916 val /= 2;
3917 if (val < -0x80 || val > 0x7f)
3918 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3919 buf[lowbyte] = val;
3920 break;
3922 case BFD_RELOC_SH_PCDISP12BY2:
3923 val /= 2;
3924 if (val < -0x800 || val > 0x7ff)
3925 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3926 buf[lowbyte] = val & 0xff;
3927 buf[highbyte] |= (val >> 8) & 0xf;
3928 break;
3930 case BFD_RELOC_32:
3931 case BFD_RELOC_32_PCREL:
3932 md_number_to_chars (buf, val, 4);
3933 break;
3935 case BFD_RELOC_16:
3936 md_number_to_chars (buf, val, 2);
3937 break;
3939 case BFD_RELOC_SH_USES:
3940 /* Pass the value into sh_reloc(). */
3941 fixP->fx_addnumber = val;
3942 break;
3944 case BFD_RELOC_SH_COUNT:
3945 case BFD_RELOC_SH_ALIGN:
3946 case BFD_RELOC_SH_CODE:
3947 case BFD_RELOC_SH_DATA:
3948 case BFD_RELOC_SH_LABEL:
3949 /* Nothing to do here. */
3950 break;
3952 case BFD_RELOC_SH_LOOP_START:
3953 case BFD_RELOC_SH_LOOP_END:
3955 case BFD_RELOC_VTABLE_INHERIT:
3956 case BFD_RELOC_VTABLE_ENTRY:
3957 fixP->fx_done = 0;
3958 return;
3960 #ifdef OBJ_ELF
3961 case BFD_RELOC_32_PLT_PCREL:
3962 /* Make the jump instruction point to the address of the operand. At
3963 runtime we merely add the offset to the actual PLT entry. */
3964 * valP = 0xfffffffc;
3965 val = fixP->fx_offset;
3966 if (fixP->fx_subsy)
3967 val -= S_GET_VALUE (fixP->fx_subsy);
3968 fixP->fx_addnumber = val;
3969 md_number_to_chars (buf, val, 4);
3970 break;
3972 case BFD_RELOC_SH_GOTPC:
3973 /* This is tough to explain. We end up with this one if we have
3974 operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".
3975 The goal here is to obtain the absolute address of the GOT,
3976 and it is strongly preferable from a performance point of
3977 view to avoid using a runtime relocation for this. There are
3978 cases where you have something like:
3980 .long _GLOBAL_OFFSET_TABLE_+[.-.L66]
3982 and here no correction would be required. Internally in the
3983 assembler we treat operands of this form as not being pcrel
3984 since the '.' is explicitly mentioned, and I wonder whether
3985 it would simplify matters to do it this way. Who knows. In
3986 earlier versions of the PIC patches, the pcrel_adjust field
3987 was used to store the correction, but since the expression is
3988 not pcrel, I felt it would be confusing to do it this way. */
3989 * valP -= 1;
3990 md_number_to_chars (buf, val, 4);
3991 break;
3993 case BFD_RELOC_SH_TLS_GD_32:
3994 case BFD_RELOC_SH_TLS_LD_32:
3995 case BFD_RELOC_SH_TLS_IE_32:
3996 S_SET_THREAD_LOCAL (fixP->fx_addsy);
3997 /* Fallthrough */
3998 case BFD_RELOC_32_GOT_PCREL:
3999 case BFD_RELOC_SH_GOTPLT32:
4000 * valP = 0; /* Fully resolved at runtime. No addend. */
4001 md_number_to_chars (buf, 0, 4);
4002 break;
4004 case BFD_RELOC_SH_TLS_LDO_32:
4005 case BFD_RELOC_SH_TLS_LE_32:
4006 S_SET_THREAD_LOCAL (fixP->fx_addsy);
4007 /* Fallthrough */
4008 case BFD_RELOC_32_GOTOFF:
4009 md_number_to_chars (buf, val, 4);
4010 break;
4011 #endif
4013 default:
4014 #ifdef HAVE_SH64
4015 shmedia_md_apply_fix (fixP, valP);
4016 return;
4017 #else
4018 abort ();
4019 #endif
4022 if (shift != 0)
4024 if ((val & ((1 << shift) - 1)) != 0)
4025 as_bad_where (fixP->fx_file, fixP->fx_line, _("misaligned offset"));
4026 if (val >= 0)
4027 val >>= shift;
4028 else
4029 val = ((val >> shift)
4030 | ((long) -1 & ~ ((long) -1 >> shift)));
4032 if (max != 0 && (val < min || val > max))
4033 as_bad_where (fixP->fx_file, fixP->fx_line, _("offset out of range"));
4035 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
4036 fixP->fx_done = 1;
4039 /* Called just before address relaxation. Return the length
4040 by which a fragment must grow to reach it's destination. */
4043 md_estimate_size_before_relax (fragS *fragP, segT segment_type)
4045 int what;
4047 switch (fragP->fr_subtype)
4049 default:
4050 #ifdef HAVE_SH64
4051 return shmedia_md_estimate_size_before_relax (fragP, segment_type);
4052 #else
4053 abort ();
4054 #endif
4057 case C (UNCOND_JUMP, UNDEF_DISP):
4058 /* Used to be a branch to somewhere which was unknown. */
4059 if (!fragP->fr_symbol)
4061 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
4063 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
4065 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
4067 else
4069 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
4071 break;
4073 case C (COND_JUMP, UNDEF_DISP):
4074 case C (COND_JUMP_DELAY, UNDEF_DISP):
4075 what = GET_WHAT (fragP->fr_subtype);
4076 /* Used to be a branch to somewhere which was unknown. */
4077 if (fragP->fr_symbol
4078 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
4080 /* Got a symbol and it's defined in this segment, become byte
4081 sized - maybe it will fix up. */
4082 fragP->fr_subtype = C (what, COND8);
4084 else if (fragP->fr_symbol)
4086 /* Its got a segment, but its not ours, so it will always be long. */
4087 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
4089 else
4091 /* We know the abs value. */
4092 fragP->fr_subtype = C (what, COND8);
4094 break;
4096 case C (UNCOND_JUMP, UNCOND12):
4097 case C (UNCOND_JUMP, UNCOND32):
4098 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
4099 case C (COND_JUMP, COND8):
4100 case C (COND_JUMP, COND12):
4101 case C (COND_JUMP, COND32):
4102 case C (COND_JUMP, UNDEF_WORD_DISP):
4103 case C (COND_JUMP_DELAY, COND8):
4104 case C (COND_JUMP_DELAY, COND12):
4105 case C (COND_JUMP_DELAY, COND32):
4106 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
4107 /* When relaxing a section for the second time, we don't need to
4108 do anything besides return the current size. */
4109 break;
4112 fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
4113 return fragP->fr_var;
4116 /* Put number into target byte order. */
4118 void
4119 md_number_to_chars (char *ptr, valueT use, int nbytes)
4121 #ifdef HAVE_SH64
4122 /* We might need to set the contents type to data. */
4123 sh64_flag_output ();
4124 #endif
4126 if (! target_big_endian)
4127 number_to_chars_littleendian (ptr, use, nbytes);
4128 else
4129 number_to_chars_bigendian (ptr, use, nbytes);
4132 /* This version is used in obj-coff.c eg. for the sh-hms target. */
4134 long
4135 md_pcrel_from (fixS *fixP)
4137 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
4140 long
4141 md_pcrel_from_section (fixS *fixP, segT sec)
4143 if (! sh_local_pcrel (fixP)
4144 && fixP->fx_addsy != (symbolS *) NULL
4145 && (generic_force_reloc (fixP)
4146 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
4148 /* The symbol is undefined (or is defined but not in this section,
4149 or we're not sure about it being the final definition). Let the
4150 linker figure it out. We need to adjust the subtraction of a
4151 symbol to the position of the relocated data, though. */
4152 return fixP->fx_subsy ? fixP->fx_where + fixP->fx_frag->fr_address : 0;
4155 return md_pcrel_from (fixP);
4158 /* Create a reloc. */
4160 arelent *
4161 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
4163 arelent *rel;
4164 bfd_reloc_code_real_type r_type;
4166 rel = (arelent *) xmalloc (sizeof (arelent));
4167 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
4168 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
4169 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
4171 r_type = fixp->fx_r_type;
4173 if (SWITCH_TABLE (fixp))
4175 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_subsy);
4176 rel->addend = 0;
4177 if (r_type == BFD_RELOC_16)
4178 r_type = BFD_RELOC_SH_SWITCH16;
4179 else if (r_type == BFD_RELOC_8)
4180 r_type = BFD_RELOC_8_PCREL;
4181 else if (r_type == BFD_RELOC_32)
4182 r_type = BFD_RELOC_SH_SWITCH32;
4183 else
4184 abort ();
4186 else if (r_type == BFD_RELOC_SH_USES)
4187 rel->addend = fixp->fx_addnumber;
4188 else if (r_type == BFD_RELOC_SH_COUNT)
4189 rel->addend = fixp->fx_offset;
4190 else if (r_type == BFD_RELOC_SH_ALIGN)
4191 rel->addend = fixp->fx_offset;
4192 else if (r_type == BFD_RELOC_VTABLE_INHERIT
4193 || r_type == BFD_RELOC_VTABLE_ENTRY)
4194 rel->addend = fixp->fx_offset;
4195 else if (r_type == BFD_RELOC_SH_LOOP_START
4196 || r_type == BFD_RELOC_SH_LOOP_END)
4197 rel->addend = fixp->fx_offset;
4198 else if (r_type == BFD_RELOC_SH_LABEL && fixp->fx_pcrel)
4200 rel->addend = 0;
4201 rel->address = rel->addend = fixp->fx_offset;
4203 #ifdef HAVE_SH64
4204 else if (shmedia_init_reloc (rel, fixp))
4206 #endif
4207 else if (fixp->fx_pcrel)
4208 rel->addend = fixp->fx_addnumber;
4209 else if (r_type == BFD_RELOC_32 || r_type == BFD_RELOC_32_GOTOFF)
4210 rel->addend = fixp->fx_addnumber;
4211 else
4212 rel->addend = 0;
4214 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
4216 if (rel->howto == NULL)
4218 as_bad_where (fixp->fx_file, fixp->fx_line,
4219 _("Cannot represent relocation type %s"),
4220 bfd_get_reloc_code_name (r_type));
4221 /* Set howto to a garbage value so that we can keep going. */
4222 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
4223 assert (rel->howto != NULL);
4225 #ifdef OBJ_ELF
4226 else if (rel->howto->type == R_SH_IND12W)
4227 rel->addend += fixp->fx_offset - 4;
4228 #endif
4230 return rel;
4233 #ifdef OBJ_ELF
4234 inline static char *
4235 sh_end_of_match (char *cont, char *what)
4237 int len = strlen (what);
4239 if (strncasecmp (cont, what, strlen (what)) == 0
4240 && ! is_part_of_name (cont[len]))
4241 return cont + len;
4243 return NULL;
4247 sh_parse_name (char const *name, expressionS *exprP, char *nextcharP)
4249 char *next = input_line_pointer;
4250 char *next_end;
4251 int reloc_type;
4252 segT segment;
4254 exprP->X_op_symbol = NULL;
4256 if (strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
4258 if (! GOT_symbol)
4259 GOT_symbol = symbol_find_or_make (name);
4261 exprP->X_add_symbol = GOT_symbol;
4262 no_suffix:
4263 /* If we have an absolute symbol or a reg, then we know its
4264 value now. */
4265 segment = S_GET_SEGMENT (exprP->X_add_symbol);
4266 if (segment == absolute_section)
4268 exprP->X_op = O_constant;
4269 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
4270 exprP->X_add_symbol = NULL;
4272 else if (segment == reg_section)
4274 exprP->X_op = O_register;
4275 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
4276 exprP->X_add_symbol = NULL;
4278 else
4280 exprP->X_op = O_symbol;
4281 exprP->X_add_number = 0;
4284 return 1;
4287 exprP->X_add_symbol = symbol_find_or_make (name);
4289 if (*nextcharP != '@')
4290 goto no_suffix;
4291 else if ((next_end = sh_end_of_match (next + 1, "GOTOFF")))
4292 reloc_type = BFD_RELOC_32_GOTOFF;
4293 else if ((next_end = sh_end_of_match (next + 1, "GOTPLT")))
4294 reloc_type = BFD_RELOC_SH_GOTPLT32;
4295 else if ((next_end = sh_end_of_match (next + 1, "GOT")))
4296 reloc_type = BFD_RELOC_32_GOT_PCREL;
4297 else if ((next_end = sh_end_of_match (next + 1, "PLT")))
4298 reloc_type = BFD_RELOC_32_PLT_PCREL;
4299 else if ((next_end = sh_end_of_match (next + 1, "TLSGD")))
4300 reloc_type = BFD_RELOC_SH_TLS_GD_32;
4301 else if ((next_end = sh_end_of_match (next + 1, "TLSLDM")))
4302 reloc_type = BFD_RELOC_SH_TLS_LD_32;
4303 else if ((next_end = sh_end_of_match (next + 1, "GOTTPOFF")))
4304 reloc_type = BFD_RELOC_SH_TLS_IE_32;
4305 else if ((next_end = sh_end_of_match (next + 1, "TPOFF")))
4306 reloc_type = BFD_RELOC_SH_TLS_LE_32;
4307 else if ((next_end = sh_end_of_match (next + 1, "DTPOFF")))
4308 reloc_type = BFD_RELOC_SH_TLS_LDO_32;
4309 else
4310 goto no_suffix;
4312 *input_line_pointer = *nextcharP;
4313 input_line_pointer = next_end;
4314 *nextcharP = *input_line_pointer;
4315 *input_line_pointer = '\0';
4317 exprP->X_op = O_PIC_reloc;
4318 exprP->X_add_number = 0;
4319 exprP->X_md = reloc_type;
4321 return 1;
4324 void
4325 sh_cfi_frame_initial_instructions (void)
4327 cfi_add_CFA_def_cfa (15, 0);
4331 sh_regname_to_dw2regnum (const char *regname)
4333 unsigned int regnum = -1;
4334 unsigned int i;
4335 const char *p;
4336 char *q;
4337 static struct { char *name; int dw2regnum; } regnames[] =
4339 { "pr", 17 }, { "t", 18 }, { "gbr", 19 }, { "mach", 20 },
4340 { "macl", 21 }, { "fpul", 23 }
4343 for (i = 0; i < ARRAY_SIZE (regnames); ++i)
4344 if (strcmp (regnames[i].name, regname) == 0)
4345 return regnames[i].dw2regnum;
4347 if (regname[0] == 'r')
4349 p = regname + 1;
4350 regnum = strtoul (p, &q, 10);
4351 if (p == q || *q || regnum >= 16)
4352 return -1;
4354 else if (regname[0] == 'f' && regname[1] == 'r')
4356 p = regname + 2;
4357 regnum = strtoul (p, &q, 10);
4358 if (p == q || *q || regnum >= 16)
4359 return -1;
4360 regnum += 25;
4362 else if (regname[0] == 'x' && regname[1] == 'd')
4364 p = regname + 2;
4365 regnum = strtoul (p, &q, 10);
4366 if (p == q || *q || regnum >= 8)
4367 return -1;
4368 regnum += 87;
4370 return regnum;
4372 #endif /* OBJ_ELF */