* remote-utils.c (write_enn): Use "E01" instead of "ENN" for the
[deliverable/binutils-gdb.git] / gas / config / tc-sh.c
CommitLineData
ef230218 1/* tc-sh.c -- Assemble code for the Renesas / SuperH SH
ae6063d4 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
f7e42eb4 3 Free Software Foundation, Inc.
252b5132
RH
4
5 This file is part of GAS, the GNU Assembler.
6
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.
11
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.
16
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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
6b31947e 22/* Written By Steve Chamberlain <sac@cygnus.com> */
252b5132
RH
23
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"
3882b010 30#include "safe-ctype.h"
43841e91 31#include "struc-symbol.h"
d4845d57
JR
32
33#ifdef OBJ_ELF
34#include "elf/sh.h"
35#endif
36
0d10e182 37#include "dwarf2dbg.h"
0d10e182 38
e08ae979
HPN
39typedef struct
40 {
41 sh_arg_type type;
42 int reg;
43 expressionS immediate;
44 }
45sh_operand_info;
46
252b5132
RH
47const char comment_chars[] = "!";
48const char line_separator_chars[] = ";";
49const char line_comment_chars[] = "!#";
50
8edc77b9
KK
51static void s_uses (int);
52static void s_uacons (int);
252b5132 53
a1cc9221 54#ifdef OBJ_ELF
8edc77b9 55static void sh_elf_cons (int);
538cd60f 56
a1cc9221
AO
57symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
58#endif
59
05982cac 60static void
8edc77b9 61big (int ignore ATTRIBUTE_UNUSED)
05982cac
HPN
62{
63 if (! target_big_endian)
64 as_bad (_("directive .big encountered when option -big required"));
65
66 /* Stop further messages. */
67 target_big_endian = 1;
68}
252b5132
RH
69
70static void
8edc77b9 71little (int ignore ATTRIBUTE_UNUSED)
252b5132 72{
05982cac
HPN
73 if (target_big_endian)
74 as_bad (_("directive .little encountered when option -little required"));
75
76 /* Stop further messages. */
252b5132
RH
77 target_big_endian = 0;
78}
79
d4845d57
JR
80/* This table describes all the machine specific pseudo-ops the assembler
81 has to support. The fields are:
82 pseudo-op name without dot
83 function to call to execute this pseudo-op
6b31947e 84 Integer arg to pass to the function. */
d4845d57 85
252b5132
RH
86const pseudo_typeS md_pseudo_table[] =
87{
a1cc9221
AO
88#ifdef OBJ_ELF
89 {"long", sh_elf_cons, 4},
90 {"int", sh_elf_cons, 4},
91 {"word", sh_elf_cons, 2},
92 {"short", sh_elf_cons, 2},
93#else
252b5132
RH
94 {"int", cons, 4},
95 {"word", cons, 2},
a1cc9221 96#endif /* OBJ_ELF */
05982cac 97 {"big", big, 0},
252b5132
RH
98 {"form", listing_psize, 0},
99 {"little", little, 0},
100 {"heading", listing_title, 0},
101 {"import", s_ignore, 0},
102 {"page", listing_eject, 0},
103 {"program", s_ignore, 0},
104 {"uses", s_uses, 0},
105 {"uaword", s_uacons, 2},
106 {"ualong", s_uacons, 4},
de68de20
AO
107 {"uaquad", s_uacons, 8},
108 {"2byte", s_uacons, 2},
109 {"4byte", s_uacons, 4},
110 {"8byte", s_uacons, 8},
324bfcf3
AO
111#ifdef HAVE_SH64
112 {"mode", s_sh64_mode, 0 },
113
114 /* Have the old name too. */
115 {"isa", s_sh64_mode, 0 },
116
117 /* Assert that the right ABI is used. */
118 {"abi", s_sh64_abi, 0 },
119
120 { "vtable_inherit", sh64_vtable_inherit, 0 },
121 { "vtable_entry", sh64_vtable_entry, 0 },
122#endif /* HAVE_SH64 */
252b5132
RH
123 {0, 0, 0}
124};
125
126/*int md_reloc_size; */
127
128int sh_relax; /* set if -relax seen */
129
130/* Whether -small was seen. */
131
132int sh_small;
133
bdfaef52 134/* preset architecture set, if given; zero otherwise. */
d4845d57 135
bdfaef52 136static int preset_target_arch;
d4845d57
JR
137
138/* The bit mask of architectures that could
67c1ffbe 139 accommodate the insns seen so far. */
d4845d57
JR
140static int valid_arch;
141
252b5132
RH
142const char EXP_CHARS[] = "eE";
143
6b31947e 144/* Chars that mean this number is a floating point constant. */
252b5132
RH
145/* As in 0f12.456 */
146/* or 0d1.2345e12 */
147const char FLT_CHARS[] = "rRsSfFdDxXpP";
148
149#define C(a,b) ENCODE_RELAX(a,b)
150
252b5132
RH
151#define ENCODE_RELAX(what,length) (((what) << 4) + (length))
152#define GET_WHAT(x) ((x>>4))
153
67c1ffbe 154/* These are the three types of relaxable instruction. */
324bfcf3
AO
155/* These are the types of relaxable instructions; except for END which is
156 a marker. */
252b5132
RH
157#define COND_JUMP 1
158#define COND_JUMP_DELAY 2
159#define UNCOND_JUMP 3
324bfcf3
AO
160
161#ifdef HAVE_SH64
162
163/* A 16-bit (times four) pc-relative operand, at most expanded to 32 bits. */
164#define SH64PCREL16_32 4
165/* A 16-bit (times four) pc-relative operand, at most expanded to 64 bits. */
166#define SH64PCREL16_64 5
167
168/* Variants of the above for adjusting the insn to PTA or PTB according to
169 the label. */
170#define SH64PCREL16PT_32 6
171#define SH64PCREL16PT_64 7
172
173/* A MOVI expansion, expanding to at most 32 or 64 bits. */
174#define MOVI_IMM_32 8
175#define MOVI_IMM_32_PCREL 9
176#define MOVI_IMM_64 10
177#define MOVI_IMM_64_PCREL 11
178#define END 12
179
180#else /* HAVE_SH64 */
181
252b5132
RH
182#define END 4
183
324bfcf3
AO
184#endif /* HAVE_SH64 */
185
252b5132
RH
186#define UNDEF_DISP 0
187#define COND8 1
188#define COND12 2
189#define COND32 3
252b5132
RH
190#define UNDEF_WORD_DISP 4
191
192#define UNCOND12 1
193#define UNCOND32 2
194
324bfcf3
AO
195#ifdef HAVE_SH64
196#define UNDEF_SH64PCREL 0
197#define SH64PCREL16 1
198#define SH64PCREL32 2
199#define SH64PCREL48 3
200#define SH64PCREL64 4
201#define SH64PCRELPLT 5
202
203#define UNDEF_MOVI 0
204#define MOVI_16 1
205#define MOVI_32 2
206#define MOVI_48 3
207#define MOVI_64 4
208#define MOVI_PLT 5
209#define MOVI_GOTOFF 6
210#define MOVI_GOTPC 7
211#endif /* HAVE_SH64 */
212
252b5132
RH
213/* Branch displacements are from the address of the branch plus
214 four, thus all minimum and maximum values have 4 added to them. */
215#define COND8_F 258
216#define COND8_M -252
217#define COND8_LENGTH 2
218
219/* There is one extra instruction before the branch, so we must add
220 two more bytes to account for it. */
221#define COND12_F 4100
222#define COND12_M -4090
223#define COND12_LENGTH 6
224
225#define COND12_DELAY_LENGTH 4
226
227/* ??? The minimum and maximum values are wrong, but this does not matter
228 since this relocation type is not supported yet. */
229#define COND32_F (1<<30)
230#define COND32_M -(1<<30)
231#define COND32_LENGTH 14
232
233#define UNCOND12_F 4098
234#define UNCOND12_M -4092
235#define UNCOND12_LENGTH 2
236
237/* ??? The minimum and maximum values are wrong, but this does not matter
238 since this relocation type is not supported yet. */
239#define UNCOND32_F (1<<30)
240#define UNCOND32_M -(1<<30)
241#define UNCOND32_LENGTH 14
242
324bfcf3
AO
243#ifdef HAVE_SH64
244/* The trivial expansion of a SH64PCREL16 relaxation is just a "PT label,
245 TRd" as is the current insn, so no extra length. Note that the "reach"
246 is calculated from the address *after* that insn, but the offset in the
247 insn is calculated from the beginning of the insn. We also need to
248 take into account the implicit 1 coded as the "A" in PTA when counting
249 forward. If PTB reaches an odd address, we trap that as an error
250 elsewhere, so we don't have to have different relaxation entries. We
251 don't add a one to the negative range, since PTB would then have the
252 farthest backward-reaching value skipped, not generated at relaxation. */
253#define SH64PCREL16_F (32767 * 4 - 4 + 1)
254#define SH64PCREL16_M (-32768 * 4 - 4)
255#define SH64PCREL16_LENGTH 0
256
257/* The next step is to change that PT insn into
258 MOVI ((label - datalabel Ln) >> 16) & 65535, R25
259 SHORI (label - datalabel Ln) & 65535, R25
260 Ln:
261 PTREL R25,TRd
262 which means two extra insns, 8 extra bytes. This is the limit for the
263 32-bit ABI.
264
265 The expressions look a bit bad since we have to adjust this to avoid overflow on a
266 32-bit host. */
267#define SH64PCREL32_F ((((long) 1 << 30) - 1) * 2 + 1 - 4)
268#define SH64PCREL32_LENGTH (2 * 4)
269
270/* Similarly, we just change the MOVI and add a SHORI for the 48-bit
271 expansion. */
272#if BFD_HOST_64BIT_LONG
273/* The "reach" type is long, so we can only do this for a 64-bit-long
274 host. */
275#define SH64PCREL32_M (((long) -1 << 30) * 2 - 4)
276#define SH64PCREL48_F ((((long) 1 << 47) - 1) - 4)
277#define SH64PCREL48_M (((long) -1 << 47) - 4)
278#define SH64PCREL48_LENGTH (3 * 4)
279#else
280/* If the host does not have 64-bit longs, just make this state identical
281 in reach to the 32-bit state. Note that we have a slightly incorrect
282 reach, but the correct one above will overflow a 32-bit number. */
283#define SH64PCREL32_M (((long) -1 << 30) * 2)
284#define SH64PCREL48_F SH64PCREL32_F
285#define SH64PCREL48_M SH64PCREL32_M
286#define SH64PCREL48_LENGTH (3 * 4)
287#endif /* BFD_HOST_64BIT_LONG */
288
289/* And similarly for the 64-bit expansion; a MOVI + SHORI + SHORI + SHORI
290 + PTREL sequence. */
291#define SH64PCREL64_LENGTH (4 * 4)
292
293/* For MOVI, we make the MOVI + SHORI... expansion you can see in the
294 SH64PCREL expansions. The PCREL one is similar, but the other has no
295 pc-relative reach; it must be fully expanded in
296 shmedia_md_estimate_size_before_relax. */
297#define MOVI_16_LENGTH 0
298#define MOVI_16_F (32767 - 4)
299#define MOVI_16_M (-32768 - 4)
300#define MOVI_32_LENGTH 4
301#define MOVI_32_F ((((long) 1 << 30) - 1) * 2 + 1 - 4)
302#define MOVI_48_LENGTH 8
303
304#if BFD_HOST_64BIT_LONG
305/* The "reach" type is long, so we can only do this for a 64-bit-long
306 host. */
307#define MOVI_32_M (((long) -1 << 30) * 2 - 4)
308#define MOVI_48_F ((((long) 1 << 47) - 1) - 4)
309#define MOVI_48_M (((long) -1 << 47) - 4)
310#else
311/* If the host does not have 64-bit longs, just make this state identical
312 in reach to the 32-bit state. Note that we have a slightly incorrect
313 reach, but the correct one above will overflow a 32-bit number. */
314#define MOVI_32_M (((long) -1 << 30) * 2)
315#define MOVI_48_F MOVI_32_F
316#define MOVI_48_M MOVI_32_M
317#endif /* BFD_HOST_64BIT_LONG */
318
319#define MOVI_64_LENGTH 12
320#endif /* HAVE_SH64 */
321
43841e91
NC
322#define EMPTY { 0, 0, 0, 0 }
323
252b5132 324const relax_typeS md_relax_table[C (END, 0)] = {
43841e91
NC
325 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
326 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
252b5132 327
43841e91 328 EMPTY,
252b5132
RH
329 /* C (COND_JUMP, COND8) */
330 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
331 /* C (COND_JUMP, COND12) */
332 { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
333 /* C (COND_JUMP, COND32) */
334 { COND32_F, COND32_M, COND32_LENGTH, 0, },
e66457fb
AM
335 /* C (COND_JUMP, UNDEF_WORD_DISP) */
336 { 0, 0, COND32_LENGTH, 0, },
337 EMPTY, EMPTY, EMPTY,
43841e91 338 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
252b5132 339
43841e91 340 EMPTY,
252b5132
RH
341 /* C (COND_JUMP_DELAY, COND8) */
342 { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP_DELAY, COND12) },
343 /* C (COND_JUMP_DELAY, COND12) */
344 { COND12_F, COND12_M, COND12_DELAY_LENGTH, C (COND_JUMP_DELAY, COND32), },
345 /* C (COND_JUMP_DELAY, COND32) */
346 { COND32_F, COND32_M, COND32_LENGTH, 0, },
e66457fb
AM
347 /* C (COND_JUMP_DELAY, UNDEF_WORD_DISP) */
348 { 0, 0, COND32_LENGTH, 0, },
349 EMPTY, EMPTY, EMPTY,
43841e91 350 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
252b5132 351
43841e91 352 EMPTY,
252b5132
RH
353 /* C (UNCOND_JUMP, UNCOND12) */
354 { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
355 /* C (UNCOND_JUMP, UNCOND32) */
356 { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
e66457fb
AM
357 EMPTY,
358 /* C (UNCOND_JUMP, UNDEF_WORD_DISP) */
359 { 0, 0, UNCOND32_LENGTH, 0, },
360 EMPTY, EMPTY, EMPTY,
43841e91 361 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
324bfcf3
AO
362
363#ifdef HAVE_SH64
364 /* C (SH64PCREL16_32, SH64PCREL16) */
365 EMPTY,
366 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16_32, SH64PCREL32) },
367 /* C (SH64PCREL16_32, SH64PCREL32) */
368 { 0, 0, SH64PCREL32_LENGTH, 0 },
369 EMPTY, EMPTY,
370 /* C (SH64PCREL16_32, SH64PCRELPLT) */
371 { 0, 0, SH64PCREL32_LENGTH, 0 },
372 EMPTY, EMPTY,
373 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
374
375 /* C (SH64PCREL16_64, SH64PCREL16) */
376 EMPTY,
377 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16_64, SH64PCREL32) },
378 /* C (SH64PCREL16_64, SH64PCREL32) */
379 { SH64PCREL32_F, SH64PCREL32_M, SH64PCREL32_LENGTH, C (SH64PCREL16_64, SH64PCREL48) },
380 /* C (SH64PCREL16_64, SH64PCREL48) */
381 { SH64PCREL48_F, SH64PCREL48_M, SH64PCREL48_LENGTH, C (SH64PCREL16_64, SH64PCREL64) },
382 /* C (SH64PCREL16_64, SH64PCREL64) */
383 { 0, 0, SH64PCREL64_LENGTH, 0 },
384 /* C (SH64PCREL16_64, SH64PCRELPLT) */
385 { 0, 0, SH64PCREL64_LENGTH, 0 },
386 EMPTY, EMPTY,
387 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
388
389 /* C (SH64PCREL16PT_32, SH64PCREL16) */
390 EMPTY,
391 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16PT_32, SH64PCREL32) },
392 /* C (SH64PCREL16PT_32, SH64PCREL32) */
393 { 0, 0, SH64PCREL32_LENGTH, 0 },
394 EMPTY, EMPTY,
395 /* C (SH64PCREL16PT_32, SH64PCRELPLT) */
396 { 0, 0, SH64PCREL32_LENGTH, 0 },
397 EMPTY, EMPTY,
398 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
399
400 /* C (SH64PCREL16PT_64, SH64PCREL16) */
401 EMPTY,
402 { SH64PCREL16_F, SH64PCREL16_M, SH64PCREL16_LENGTH, C (SH64PCREL16PT_64, SH64PCREL32) },
403 /* C (SH64PCREL16PT_64, SH64PCREL32) */
404 { SH64PCREL32_F,
5d6255fe 405 SH64PCREL32_M,
324bfcf3
AO
406 SH64PCREL32_LENGTH,
407 C (SH64PCREL16PT_64, SH64PCREL48) },
408 /* C (SH64PCREL16PT_64, SH64PCREL48) */
409 { SH64PCREL48_F, SH64PCREL48_M, SH64PCREL48_LENGTH, C (SH64PCREL16PT_64, SH64PCREL64) },
410 /* C (SH64PCREL16PT_64, SH64PCREL64) */
411 { 0, 0, SH64PCREL64_LENGTH, 0 },
412 /* C (SH64PCREL16PT_64, SH64PCRELPLT) */
413 { 0, 0, SH64PCREL64_LENGTH, 0},
414 EMPTY, EMPTY,
415 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
416
417 /* C (MOVI_IMM_32, UNDEF_MOVI) */
418 { 0, 0, MOVI_32_LENGTH, 0 },
419 /* C (MOVI_IMM_32, MOVI_16) */
420 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_32, MOVI_32) },
421 /* C (MOVI_IMM_32, MOVI_32) */
422 { MOVI_32_F, MOVI_32_M, MOVI_32_LENGTH, 0 },
423 EMPTY, EMPTY, EMPTY,
424 /* C (MOVI_IMM_32, MOVI_GOTOFF) */
425 { 0, 0, MOVI_32_LENGTH, 0 },
426 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
427
428 /* C (MOVI_IMM_32_PCREL, MOVI_16) */
429 EMPTY,
430 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_32_PCREL, MOVI_32) },
431 /* C (MOVI_IMM_32_PCREL, MOVI_32) */
432 { 0, 0, MOVI_32_LENGTH, 0 },
433 EMPTY, EMPTY,
434 /* C (MOVI_IMM_32_PCREL, MOVI_PLT) */
435 { 0, 0, MOVI_32_LENGTH, 0 },
436 EMPTY,
437 /* C (MOVI_IMM_32_PCREL, MOVI_GOTPC) */
438 { 0, 0, MOVI_32_LENGTH, 0 },
439 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
440
441 /* C (MOVI_IMM_64, UNDEF_MOVI) */
442 { 0, 0, MOVI_64_LENGTH, 0 },
443 /* C (MOVI_IMM_64, MOVI_16) */
444 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_64, MOVI_32) },
445 /* C (MOVI_IMM_64, MOVI_32) */
446 { MOVI_32_F, MOVI_32_M, MOVI_32_LENGTH, C (MOVI_IMM_64, MOVI_48) },
447 /* C (MOVI_IMM_64, MOVI_48) */
448 { MOVI_48_F, MOVI_48_M, MOVI_48_LENGTH, C (MOVI_IMM_64, MOVI_64) },
449 /* C (MOVI_IMM_64, MOVI_64) */
450 { 0, 0, MOVI_64_LENGTH, 0 },
451 EMPTY,
452 /* C (MOVI_IMM_64, MOVI_GOTOFF) */
453 { 0, 0, MOVI_64_LENGTH, 0 },
454 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
455
456 /* C (MOVI_IMM_64_PCREL, MOVI_16) */
457 EMPTY,
458 { MOVI_16_F, MOVI_16_M, MOVI_16_LENGTH, C (MOVI_IMM_64_PCREL, MOVI_32) },
459 /* C (MOVI_IMM_64_PCREL, MOVI_32) */
460 { MOVI_32_F, MOVI_32_M, MOVI_32_LENGTH, C (MOVI_IMM_64_PCREL, MOVI_48) },
461 /* C (MOVI_IMM_64_PCREL, MOVI_48) */
462 { MOVI_48_F, MOVI_48_M, MOVI_48_LENGTH, C (MOVI_IMM_64_PCREL, MOVI_64) },
463 /* C (MOVI_IMM_64_PCREL, MOVI_64) */
464 { 0, 0, MOVI_64_LENGTH, 0 },
465 /* C (MOVI_IMM_64_PCREL, MOVI_PLT) */
466 { 0, 0, MOVI_64_LENGTH, 0 },
467 EMPTY,
468 /* C (MOVI_IMM_64_PCREL, MOVI_GOTPC) */
469 { 0, 0, MOVI_64_LENGTH, 0 },
470 EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
471
472#endif /* HAVE_SH64 */
473
252b5132
RH
474};
475
43841e91
NC
476#undef EMPTY
477
252b5132
RH
478static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
479
a1cc9221
AO
480\f
481#ifdef OBJ_ELF
538cd60f
AO
482/* Determinet whether the symbol needs any kind of PIC relocation. */
483
484inline static int
8edc77b9 485sh_PIC_related_p (symbolS *sym)
a1cc9221 486{
538cd60f 487 expressionS *exp;
a1cc9221 488
538cd60f
AO
489 if (! sym)
490 return 0;
491
492 if (sym == GOT_symbol)
493 return 1;
494
324bfcf3
AO
495#ifdef HAVE_SH64
496 if (sh_PIC_related_p (*symbol_get_tc (sym)))
497 return 1;
498#endif
499
538cd60f
AO
500 exp = symbol_get_value_expression (sym);
501
502 return (exp->X_op == O_PIC_reloc
503 || sh_PIC_related_p (exp->X_add_symbol)
504 || sh_PIC_related_p (exp->X_op_symbol));
505}
506
507/* Determine the relocation type to be used to represent the
508 expression, that may be rearranged. */
509
510static int
8edc77b9 511sh_check_fixup (expressionS *main_exp, bfd_reloc_code_real_type *r_type_p)
538cd60f
AO
512{
513 expressionS *exp = main_exp;
514
515 /* This is here for backward-compatibility only. GCC used to generated:
516
517 f@PLT + . - (.LPCS# + 2)
518
519 but we'd rather be able to handle this as a PIC-related reference
520 plus/minus a symbol. However, gas' parser gives us:
521
522 O_subtract (O_add (f@PLT, .), .LPCS#+2)
5d6255fe 523
538cd60f
AO
524 so we attempt to transform this into:
525
526 O_subtract (f@PLT, O_subtract (.LPCS#+2, .))
527
5d6255fe 528 which we can handle simply below. */
538cd60f
AO
529 if (exp->X_op == O_subtract)
530 {
531 if (sh_PIC_related_p (exp->X_op_symbol))
532 return 1;
533
534 exp = symbol_get_value_expression (exp->X_add_symbol);
535
536 if (exp && sh_PIC_related_p (exp->X_op_symbol))
537 return 1;
538
539 if (exp && exp->X_op == O_add
540 && sh_PIC_related_p (exp->X_add_symbol))
541 {
542 symbolS *sym = exp->X_add_symbol;
543
544 exp->X_op = O_subtract;
545 exp->X_add_symbol = main_exp->X_op_symbol;
546
547 main_exp->X_op_symbol = main_exp->X_add_symbol;
548 main_exp->X_add_symbol = sym;
549
550 main_exp->X_add_number += exp->X_add_number;
551 exp->X_add_number = 0;
552 }
553
554 exp = main_exp;
555 }
556 else if (exp->X_op == O_add && sh_PIC_related_p (exp->X_op_symbol))
557 return 1;
558
559 if (exp->X_op == O_symbol || exp->X_op == O_add || exp->X_op == O_subtract)
560 {
324bfcf3
AO
561#ifdef HAVE_SH64
562 if (exp->X_add_symbol
563 && (exp->X_add_symbol == GOT_symbol
564 || (GOT_symbol
565 && *symbol_get_tc (exp->X_add_symbol) == GOT_symbol)))
566 {
567 switch (*r_type_p)
568 {
569 case BFD_RELOC_SH_IMM_LOW16:
570 *r_type_p = BFD_RELOC_SH_GOTPC_LOW16;
571 break;
572
573 case BFD_RELOC_SH_IMM_MEDLOW16:
574 *r_type_p = BFD_RELOC_SH_GOTPC_MEDLOW16;
575 break;
576
577 case BFD_RELOC_SH_IMM_MEDHI16:
578 *r_type_p = BFD_RELOC_SH_GOTPC_MEDHI16;
579 break;
580
581 case BFD_RELOC_SH_IMM_HI16:
582 *r_type_p = BFD_RELOC_SH_GOTPC_HI16;
583 break;
584
585 case BFD_RELOC_NONE:
586 case BFD_RELOC_UNUSED:
587 *r_type_p = BFD_RELOC_SH_GOTPC;
588 break;
5d6255fe 589
324bfcf3
AO
590 default:
591 abort ();
592 }
593 return 0;
594 }
595#else
538cd60f
AO
596 if (exp->X_add_symbol && exp->X_add_symbol == GOT_symbol)
597 {
598 *r_type_p = BFD_RELOC_SH_GOTPC;
599 return 0;
600 }
324bfcf3 601#endif
538cd60f
AO
602 exp = symbol_get_value_expression (exp->X_add_symbol);
603 if (! exp)
604 return 0;
605 }
606
607 if (exp->X_op == O_PIC_reloc)
608 {
324bfcf3
AO
609#ifdef HAVE_SH64
610 switch (*r_type_p)
611 {
612 case BFD_RELOC_NONE:
613 case BFD_RELOC_UNUSED:
614 *r_type_p = exp->X_md;
615 break;
616
617 case BFD_RELOC_SH_IMM_LOW16:
618 switch (exp->X_md)
619 {
620 case BFD_RELOC_32_GOTOFF:
621 *r_type_p = BFD_RELOC_SH_GOTOFF_LOW16;
622 break;
5d6255fe 623
324bfcf3
AO
624 case BFD_RELOC_SH_GOTPLT32:
625 *r_type_p = BFD_RELOC_SH_GOTPLT_LOW16;
626 break;
5d6255fe 627
324bfcf3
AO
628 case BFD_RELOC_32_GOT_PCREL:
629 *r_type_p = BFD_RELOC_SH_GOT_LOW16;
630 break;
5d6255fe 631
324bfcf3
AO
632 case BFD_RELOC_32_PLT_PCREL:
633 *r_type_p = BFD_RELOC_SH_PLT_LOW16;
634 break;
635
636 default:
637 abort ();
638 }
639 break;
640
641 case BFD_RELOC_SH_IMM_MEDLOW16:
642 switch (exp->X_md)
643 {
644 case BFD_RELOC_32_GOTOFF:
645 *r_type_p = BFD_RELOC_SH_GOTOFF_MEDLOW16;
646 break;
5d6255fe 647
324bfcf3
AO
648 case BFD_RELOC_SH_GOTPLT32:
649 *r_type_p = BFD_RELOC_SH_GOTPLT_MEDLOW16;
650 break;
5d6255fe 651
324bfcf3
AO
652 case BFD_RELOC_32_GOT_PCREL:
653 *r_type_p = BFD_RELOC_SH_GOT_MEDLOW16;
654 break;
5d6255fe 655
324bfcf3
AO
656 case BFD_RELOC_32_PLT_PCREL:
657 *r_type_p = BFD_RELOC_SH_PLT_MEDLOW16;
658 break;
659
660 default:
661 abort ();
662 }
663 break;
664
665 case BFD_RELOC_SH_IMM_MEDHI16:
666 switch (exp->X_md)
667 {
668 case BFD_RELOC_32_GOTOFF:
669 *r_type_p = BFD_RELOC_SH_GOTOFF_MEDHI16;
670 break;
5d6255fe 671
324bfcf3
AO
672 case BFD_RELOC_SH_GOTPLT32:
673 *r_type_p = BFD_RELOC_SH_GOTPLT_MEDHI16;
674 break;
5d6255fe 675
324bfcf3
AO
676 case BFD_RELOC_32_GOT_PCREL:
677 *r_type_p = BFD_RELOC_SH_GOT_MEDHI16;
678 break;
5d6255fe 679
324bfcf3
AO
680 case BFD_RELOC_32_PLT_PCREL:
681 *r_type_p = BFD_RELOC_SH_PLT_MEDHI16;
682 break;
683
684 default:
685 abort ();
686 }
687 break;
688
689 case BFD_RELOC_SH_IMM_HI16:
690 switch (exp->X_md)
691 {
692 case BFD_RELOC_32_GOTOFF:
693 *r_type_p = BFD_RELOC_SH_GOTOFF_HI16;
694 break;
5d6255fe 695
324bfcf3
AO
696 case BFD_RELOC_SH_GOTPLT32:
697 *r_type_p = BFD_RELOC_SH_GOTPLT_HI16;
698 break;
5d6255fe 699
324bfcf3
AO
700 case BFD_RELOC_32_GOT_PCREL:
701 *r_type_p = BFD_RELOC_SH_GOT_HI16;
702 break;
5d6255fe 703
324bfcf3
AO
704 case BFD_RELOC_32_PLT_PCREL:
705 *r_type_p = BFD_RELOC_SH_PLT_HI16;
706 break;
707
708 default:
709 abort ();
710 }
711 break;
712
713 default:
714 abort ();
715 }
716#else
538cd60f 717 *r_type_p = exp->X_md;
324bfcf3 718#endif
538cd60f
AO
719 if (exp == main_exp)
720 exp->X_op = O_symbol;
721 else
722 {
723 main_exp->X_add_symbol = exp->X_add_symbol;
724 main_exp->X_add_number += exp->X_add_number;
725 }
726 }
727 else
728 return (sh_PIC_related_p (exp->X_add_symbol)
729 || sh_PIC_related_p (exp->X_op_symbol));
730
731 return 0;
732}
733
734/* Add expression EXP of SIZE bytes to offset OFF of fragment FRAG. */
735
736void
8edc77b9 737sh_cons_fix_new (fragS *frag, int off, int size, expressionS *exp)
538cd60f
AO
738{
739 bfd_reloc_code_real_type r_type = BFD_RELOC_UNUSED;
740
741 if (sh_check_fixup (exp, &r_type))
742 as_bad (_("Invalid PIC expression."));
743
744 if (r_type == BFD_RELOC_UNUSED)
745 switch (size)
a1cc9221 746 {
538cd60f
AO
747 case 1:
748 r_type = BFD_RELOC_8;
749 break;
a1cc9221 750
538cd60f
AO
751 case 2:
752 r_type = BFD_RELOC_16;
753 break;
754
755 case 4:
756 r_type = BFD_RELOC_32;
757 break;
a1cc9221 758
324bfcf3
AO
759#ifdef HAVE_SH64
760 case 8:
761 r_type = BFD_RELOC_64;
762 break;
763#endif
764
538cd60f
AO
765 default:
766 goto error;
767 }
768 else if (size != 4)
769 {
770 error:
771 as_bad (_("unsupported BFD relocation size %u"), size);
772 r_type = BFD_RELOC_UNUSED;
773 }
5d6255fe 774
538cd60f 775 fix_new_exp (frag, off, size, exp, 0, r_type);
a1cc9221
AO
776}
777
778/* The regular cons() function, that reads constants, doesn't support
779 suffixes such as @GOT, @GOTOFF and @PLT, that generate
780 machine-specific relocation types. So we must define it here. */
781/* Clobbers input_line_pointer, checks end-of-line. */
8edc77b9 782/* NBYTES 1=.byte, 2=.word, 4=.long */
a1cc9221 783static void
8edc77b9 784sh_elf_cons (register int nbytes)
a1cc9221 785{
538cd60f 786 expressionS exp;
a1cc9221 787
324bfcf3
AO
788#ifdef HAVE_SH64
789
790 /* Update existing range to include a previous insn, if there was one. */
b34976b6 791 sh64_update_contents_mark (TRUE);
324bfcf3
AO
792
793 /* We need to make sure the contents type is set to data. */
794 sh64_flag_output ();
795
796#endif /* HAVE_SH64 */
797
a1cc9221
AO
798 if (is_it_end_of_statement ())
799 {
800 demand_empty_rest_of_line ();
801 return;
802 }
803
028f09bd
NC
804#ifdef md_cons_align
805 md_cons_align (nbytes);
806#endif
807
a1cc9221
AO
808 do
809 {
810 expression (&exp);
538cd60f 811 emit_expr (&exp, (unsigned int) nbytes);
a1cc9221
AO
812 }
813 while (*input_line_pointer++ == ',');
814
81d4177b 815 input_line_pointer--; /* Put terminator back into stream. */
a1cc9221
AO
816 if (*input_line_pointer == '#' || *input_line_pointer == '!')
817 {
dda5ecfc 818 while (! is_end_of_line[(unsigned char) *input_line_pointer++]);
a1cc9221
AO
819 }
820 else
821 demand_empty_rest_of_line ();
822}
823#endif /* OBJ_ELF */
824
825\f
6b31947e
NC
826/* This function is called once, at assembler startup time. This should
827 set up all the tables, etc that the MD part of the assembler needs. */
252b5132
RH
828
829void
8edc77b9 830md_begin (void)
252b5132 831{
5ff37431 832 const sh_opcode_info *opcode;
252b5132 833 char *prev_name = "";
d4845d57 834 int target_arch;
252b5132 835
bdfaef52
JR
836 target_arch
837 = preset_target_arch ? preset_target_arch : arch_sh1_up & ~arch_sh_dsp_up;
d4845d57
JR
838 valid_arch = target_arch;
839
324bfcf3
AO
840#ifdef HAVE_SH64
841 shmedia_md_begin ();
842#endif
843
252b5132
RH
844 opcode_hash_control = hash_new ();
845
6b31947e 846 /* Insert unique names into hash table. */
252b5132
RH
847 for (opcode = sh_table; opcode->name; opcode++)
848 {
5ff37431 849 if (strcmp (prev_name, opcode->name) != 0)
252b5132 850 {
a37c8f88
JR
851 if (! (opcode->arch & target_arch))
852 continue;
252b5132
RH
853 prev_name = opcode->name;
854 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
855 }
252b5132
RH
856 }
857}
858
859static int reg_m;
860static int reg_n;
d4845d57
JR
861static int reg_x, reg_y;
862static int reg_efg;
252b5132
RH
863static int reg_b;
864
3882b010 865#define IDENT_CHAR(c) (ISALNUM (c) || (c) == '_')
dead1419 866
6b31947e
NC
867/* Try to parse a reg name. Return the number of chars consumed. */
868
252b5132 869static int
8edc77b9 870parse_reg (char *src, int *mode, int *reg)
252b5132 871{
3882b010
L
872 char l0 = TOLOWER (src[0]);
873 char l1 = l0 ? TOLOWER (src[1]) : 0;
e46fee70 874
dead1419 875 /* We use ! IDENT_CHAR for the next character after the register name, to
252b5132 876 make sure that we won't accidentally recognize a symbol name such as
dead1419 877 'sram' or sr_ram as being a reference to the register 'sr'. */
252b5132 878
e46fee70 879 if (l0 == 'r')
252b5132 880 {
e46fee70 881 if (l1 == '1')
d4845d57
JR
882 {
883 if (src[2] >= '0' && src[2] <= '5'
dead1419 884 && ! IDENT_CHAR ((unsigned char) src[3]))
d4845d57
JR
885 {
886 *mode = A_REG_N;
887 *reg = 10 + src[2] - '0';
888 return 3;
889 }
890 }
e46fee70 891 if (l1 >= '0' && l1 <= '9'
dead1419 892 && ! IDENT_CHAR ((unsigned char) src[2]))
d4845d57
JR
893 {
894 *mode = A_REG_N;
e46fee70 895 *reg = (l1 - '0');
d4845d57
JR
896 return 2;
897 }
e46fee70 898 if (l1 >= '0' && l1 <= '7' && strncasecmp (&src[2], "_bank", 5) == 0
dead1419
JR
899 && ! IDENT_CHAR ((unsigned char) src[7]))
900 {
901 *mode = A_REG_B;
e46fee70 902 *reg = (l1 - '0');
dead1419
JR
903 return 7;
904 }
d4845d57 905
e46fee70 906 if (l1 == 'e' && ! IDENT_CHAR ((unsigned char) src[2]))
d4845d57
JR
907 {
908 *mode = A_RE;
909 return 2;
910 }
e46fee70 911 if (l1 == 's' && ! IDENT_CHAR ((unsigned char) src[2]))
d4845d57
JR
912 {
913 *mode = A_RS;
914 return 2;
915 }
252b5132
RH
916 }
917
e46fee70 918 if (l0 == 'a')
252b5132 919 {
e46fee70 920 if (l1 == '0')
d4845d57 921 {
dead1419 922 if (! IDENT_CHAR ((unsigned char) src[2]))
d4845d57
JR
923 {
924 *mode = DSP_REG_N;
925 *reg = A_A0_NUM;
926 return 2;
927 }
3882b010 928 if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
d4845d57
JR
929 {
930 *mode = DSP_REG_N;
931 *reg = A_A0G_NUM;
932 return 3;
933 }
934 }
e46fee70 935 if (l1 == '1')
252b5132 936 {
dead1419 937 if (! IDENT_CHAR ((unsigned char) src[2]))
252b5132 938 {
d4845d57
JR
939 *mode = DSP_REG_N;
940 *reg = A_A1_NUM;
941 return 2;
942 }
3882b010 943 if (TOLOWER (src[2]) == 'g' && ! IDENT_CHAR ((unsigned char) src[3]))
d4845d57
JR
944 {
945 *mode = DSP_REG_N;
946 *reg = A_A1G_NUM;
252b5132
RH
947 return 3;
948 }
949 }
d4845d57 950
e46fee70 951 if (l1 == 'x' && src[2] >= '0' && src[2] <= '1'
dead1419 952 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
953 {
954 *mode = A_REG_N;
e46fee70 955 *reg = 4 + (l1 - '0');
d4845d57
JR
956 return 3;
957 }
e46fee70 958 if (l1 == 'y' && src[2] >= '0' && src[2] <= '1'
dead1419 959 && ! IDENT_CHAR ((unsigned char) src[3]))
d4845d57
JR
960 {
961 *mode = A_REG_N;
e46fee70 962 *reg = 6 + (l1 - '0');
d4845d57
JR
963 return 3;
964 }
e46fee70 965 if (l1 == 's' && src[2] >= '0' && src[2] <= '3'
dead1419 966 && ! IDENT_CHAR ((unsigned char) src[3]))
d4845d57 967 {
e46fee70 968 int n = l1 - '0';
d4845d57
JR
969
970 *mode = A_REG_N;
971 *reg = n | ((~n & 2) << 1);
972 return 3;
973 }
974 }
975
912a07db 976 if (l0 == 'i' && l1 && ! IDENT_CHAR ((unsigned char) src[2]))
d4845d57 977 {
e46fee70 978 if (l1 == 's')
d4845d57
JR
979 {
980 *mode = A_REG_N;
981 *reg = 8;
252b5132
RH
982 return 2;
983 }
e46fee70 984 if (l1 == 'x')
d4845d57
JR
985 {
986 *mode = A_REG_N;
987 *reg = 8;
988 return 2;
989 }
e46fee70 990 if (l1 == 'y')
d4845d57
JR
991 {
992 *mode = A_REG_N;
993 *reg = 9;
994 return 2;
995 }
996 }
997
e46fee70 998 if (l0 == 'x' && l1 >= '0' && l1 <= '1'
dead1419 999 && ! IDENT_CHAR ((unsigned char) src[2]))
d4845d57
JR
1000 {
1001 *mode = DSP_REG_N;
e46fee70 1002 *reg = A_X0_NUM + l1 - '0';
d4845d57
JR
1003 return 2;
1004 }
1005
e46fee70 1006 if (l0 == 'y' && l1 >= '0' && l1 <= '1'
dead1419 1007 && ! IDENT_CHAR ((unsigned char) src[2]))
d4845d57
JR
1008 {
1009 *mode = DSP_REG_N;
e46fee70 1010 *reg = A_Y0_NUM + l1 - '0';
d4845d57
JR
1011 return 2;
1012 }
1013
e46fee70 1014 if (l0 == 'm' && l1 >= '0' && l1 <= '1'
dead1419 1015 && ! IDENT_CHAR ((unsigned char) src[2]))
d4845d57
JR
1016 {
1017 *mode = DSP_REG_N;
e46fee70 1018 *reg = l1 == '0' ? A_M0_NUM : A_M1_NUM;
d4845d57 1019 return 2;
252b5132
RH
1020 }
1021
e46fee70
HPN
1022 if (l0 == 's'
1023 && l1 == 's'
3882b010 1024 && TOLOWER (src[2]) == 'r' && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1025 {
1026 *mode = A_SSR;
1027 return 3;
1028 }
1029
3882b010 1030 if (l0 == 's' && l1 == 'p' && TOLOWER (src[2]) == 'c'
dead1419 1031 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1032 {
1033 *mode = A_SPC;
1034 return 3;
1035 }
1036
3882b010 1037 if (l0 == 's' && l1 == 'g' && TOLOWER (src[2]) == 'r'
dead1419 1038 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1039 {
1040 *mode = A_SGR;
1041 return 3;
1042 }
1043
3882b010 1044 if (l0 == 'd' && l1 == 's' && TOLOWER (src[2]) == 'r'
dead1419 1045 && ! IDENT_CHAR ((unsigned char) src[3]))
d4845d57
JR
1046 {
1047 *mode = A_DSR;
1048 return 3;
1049 }
1050
3882b010 1051 if (l0 == 'd' && l1 == 'b' && TOLOWER (src[2]) == 'r'
dead1419 1052 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1053 {
1054 *mode = A_DBR;
1055 return 3;
1056 }
1057
e46fee70 1058 if (l0 == 's' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
252b5132
RH
1059 {
1060 *mode = A_SR;
1061 return 2;
1062 }
1063
e46fee70 1064 if (l0 == 's' && l1 == 'p' && ! IDENT_CHAR ((unsigned char) src[2]))
252b5132
RH
1065 {
1066 *mode = A_REG_N;
1067 *reg = 15;
1068 return 2;
1069 }
1070
e46fee70 1071 if (l0 == 'p' && l1 == 'r' && ! IDENT_CHAR ((unsigned char) src[2]))
252b5132
RH
1072 {
1073 *mode = A_PR;
1074 return 2;
1075 }
e46fee70 1076 if (l0 == 'p' && l1 == 'c' && ! IDENT_CHAR ((unsigned char) src[2]))
252b5132 1077 {
015551fc
JR
1078 /* Don't use A_DISP_PC here - that would accept stuff like 'mova pc,r0'
1079 and use an uninitialized immediate. */
1080 *mode = A_PC;
252b5132
RH
1081 return 2;
1082 }
3882b010 1083 if (l0 == 'g' && l1 == 'b' && TOLOWER (src[2]) == 'r'
dead1419 1084 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1085 {
1086 *mode = A_GBR;
1087 return 3;
1088 }
3882b010 1089 if (l0 == 'v' && l1 == 'b' && TOLOWER (src[2]) == 'r'
dead1419 1090 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1091 {
1092 *mode = A_VBR;
1093 return 3;
1094 }
1095
3882b010 1096 if (l0 == 'm' && l1 == 'a' && TOLOWER (src[2]) == 'c'
dead1419 1097 && ! IDENT_CHAR ((unsigned char) src[4]))
252b5132 1098 {
3882b010 1099 if (TOLOWER (src[3]) == 'l')
252b5132
RH
1100 {
1101 *mode = A_MACL;
1102 return 4;
1103 }
3882b010 1104 if (TOLOWER (src[3]) == 'h')
252b5132
RH
1105 {
1106 *mode = A_MACH;
1107 return 4;
1108 }
1109 }
3882b010 1110 if (l0 == 'm' && l1 == 'o' && TOLOWER (src[2]) == 'd'
912a07db 1111 && ! IDENT_CHAR ((unsigned char) src[3]))
d4845d57
JR
1112 {
1113 *mode = A_MOD;
1114 return 3;
1115 }
e46fee70 1116 if (l0 == 'f' && l1 == 'r')
252b5132
RH
1117 {
1118 if (src[2] == '1')
1119 {
1120 if (src[3] >= '0' && src[3] <= '5'
dead1419 1121 && ! IDENT_CHAR ((unsigned char) src[4]))
252b5132
RH
1122 {
1123 *mode = F_REG_N;
1124 *reg = 10 + src[3] - '0';
1125 return 4;
1126 }
1127 }
1128 if (src[2] >= '0' && src[2] <= '9'
dead1419 1129 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1130 {
1131 *mode = F_REG_N;
1132 *reg = (src[2] - '0');
1133 return 3;
1134 }
1135 }
e46fee70 1136 if (l0 == 'd' && l1 == 'r')
252b5132
RH
1137 {
1138 if (src[2] == '1')
1139 {
1140 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
dead1419 1141 && ! IDENT_CHAR ((unsigned char) src[4]))
252b5132
RH
1142 {
1143 *mode = D_REG_N;
1144 *reg = 10 + src[3] - '0';
1145 return 4;
1146 }
1147 }
1148 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
dead1419 1149 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1150 {
1151 *mode = D_REG_N;
1152 *reg = (src[2] - '0');
1153 return 3;
1154 }
1155 }
e46fee70 1156 if (l0 == 'x' && l1 == 'd')
252b5132
RH
1157 {
1158 if (src[2] == '1')
1159 {
1160 if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
dead1419 1161 && ! IDENT_CHAR ((unsigned char) src[4]))
252b5132
RH
1162 {
1163 *mode = X_REG_N;
1164 *reg = 11 + src[3] - '0';
1165 return 4;
1166 }
1167 }
1168 if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
dead1419 1169 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1170 {
1171 *mode = X_REG_N;
1172 *reg = (src[2] - '0') + 1;
1173 return 3;
1174 }
1175 }
e46fee70 1176 if (l0 == 'f' && l1 == 'v')
252b5132 1177 {
dead1419 1178 if (src[2] == '1'&& src[3] == '2' && ! IDENT_CHAR ((unsigned char) src[4]))
252b5132
RH
1179 {
1180 *mode = V_REG_N;
1181 *reg = 12;
1182 return 4;
1183 }
1184 if ((src[2] == '0' || src[2] == '4' || src[2] == '8')
dead1419 1185 && ! IDENT_CHAR ((unsigned char) src[3]))
252b5132
RH
1186 {
1187 *mode = V_REG_N;
1188 *reg = (src[2] - '0');
1189 return 3;
1190 }
1191 }
3882b010
L
1192 if (l0 == 'f' && l1 == 'p' && TOLOWER (src[2]) == 'u'
1193 && TOLOWER (src[3]) == 'l'
dead1419 1194 && ! IDENT_CHAR ((unsigned char) src[4]))
252b5132
RH
1195 {
1196 *mode = FPUL_N;
1197 return 4;
1198 }
1199
3882b010
L
1200 if (l0 == 'f' && l1 == 'p' && TOLOWER (src[2]) == 's'
1201 && TOLOWER (src[3]) == 'c'
1202 && TOLOWER (src[4]) == 'r' && ! IDENT_CHAR ((unsigned char) src[5]))
252b5132
RH
1203 {
1204 *mode = FPSCR_N;
1205 return 5;
1206 }
1207
3882b010
L
1208 if (l0 == 'x' && l1 == 'm' && TOLOWER (src[2]) == 't'
1209 && TOLOWER (src[3]) == 'r'
1210 && TOLOWER (src[4]) == 'x' && ! IDENT_CHAR ((unsigned char) src[5]))
252b5132
RH
1211 {
1212 *mode = XMTRX_M4;
1213 return 5;
1214 }
1215
1216 return 0;
1217}
1218
c4aa876b 1219static char *
8edc77b9 1220parse_exp (char *s, sh_operand_info *op)
252b5132
RH
1221{
1222 char *save;
1223 char *new;
1224
1225 save = input_line_pointer;
1226 input_line_pointer = s;
015551fc
JR
1227 expression (&op->immediate);
1228 if (op->immediate.X_op == O_absent)
252b5132 1229 as_bad (_("missing operand"));
538cd60f
AO
1230#ifdef OBJ_ELF
1231 else if (op->immediate.X_op == O_PIC_reloc
1232 || sh_PIC_related_p (op->immediate.X_add_symbol)
1233 || sh_PIC_related_p (op->immediate.X_op_symbol))
1234 as_bad (_("misplaced PIC operand"));
1235#endif
252b5132
RH
1236 new = input_line_pointer;
1237 input_line_pointer = save;
1238 return new;
1239}
1240
252b5132
RH
1241/* The many forms of operand:
1242
1243 Rn Register direct
1244 @Rn Register indirect
1245 @Rn+ Autoincrement
1246 @-Rn Autodecrement
1247 @(disp:4,Rn)
1248 @(disp:8,GBR)
1249 @(disp:8,PC)
1250
1251 @(R0,Rn)
1252 @(R0,GBR)
1253
1254 disp:8
1255 disp:12
1256 #imm8
1257 pr, gbr, vbr, macl, mach
252b5132
RH
1258 */
1259
c4aa876b 1260static char *
8edc77b9 1261parse_at (char *src, sh_operand_info *op)
252b5132
RH
1262{
1263 int len;
1264 int mode;
1265 src++;
1266 if (src[0] == '-')
1267 {
6b31947e 1268 /* Must be predecrement. */
252b5132
RH
1269 src++;
1270
1271 len = parse_reg (src, &mode, &(op->reg));
1272 if (mode != A_REG_N)
1273 as_bad (_("illegal register after @-"));
1274
1275 op->type = A_DEC_N;
1276 src += len;
1277 }
1278 else if (src[0] == '(')
1279 {
1280 /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
8d4d84c2 1281 @(r0, rn). */
252b5132
RH
1282 src++;
1283 len = parse_reg (src, &mode, &(op->reg));
1284 if (len && mode == A_REG_N)
1285 {
1286 src += len;
1287 if (op->reg != 0)
1288 {
1289 as_bad (_("must be @(r0,...)"));
1290 }
1291 if (src[0] == ',')
252b5132 1292 {
8d4d84c2
AO
1293 src++;
1294 /* Now can be rn or gbr. */
1295 len = parse_reg (src, &mode, &(op->reg));
1296 }
1297 else
1298 {
1299 len = 0;
252b5132 1300 }
8d4d84c2 1301 if (len)
252b5132 1302 {
8d4d84c2
AO
1303 if (mode == A_GBR)
1304 {
1305 op->type = A_R0_GBR;
1306 }
1307 else if (mode == A_REG_N)
1308 {
1309 op->type = A_IND_R0_REG_N;
1310 }
1311 else
1312 {
1313 as_bad (_("syntax error in @(r0,...)"));
1314 }
252b5132
RH
1315 }
1316 else
1317 {
8d4d84c2 1318 as_bad (_("syntax error in @(r0...)"));
252b5132
RH
1319 }
1320 }
1321 else
1322 {
8d4d84c2 1323 /* Must be an @(disp,.. thing). */
015551fc 1324 src = parse_exp (src, op);
252b5132
RH
1325 if (src[0] == ',')
1326 src++;
8d4d84c2 1327 /* Now can be rn, gbr or pc. */
252b5132
RH
1328 len = parse_reg (src, &mode, &op->reg);
1329 if (len)
1330 {
1331 if (mode == A_REG_N)
1332 {
1333 op->type = A_DISP_REG_N;
1334 }
1335 else if (mode == A_GBR)
1336 {
1337 op->type = A_DISP_GBR;
1338 }
015551fc 1339 else if (mode == A_PC)
252b5132 1340 {
dbb4348d
JR
1341 /* We want @(expr, pc) to uniformly address . + expr,
1342 no matter if expr is a constant, or a more complex
1343 expression, e.g. sym-. or sym1-sym2.
1344 However, we also used to accept @(sym,pc)
67c1ffbe 1345 as addressing sym, i.e. meaning the same as plain sym.
dbb4348d
JR
1346 Some existing code does use the @(sym,pc) syntax, so
1347 we give it the old semantics for now, but warn about
1348 its use, so that users have some time to fix their code.
1349
1350 Note that due to this backward compatibility hack,
1351 we'll get unexpected results when @(offset, pc) is used,
1352 and offset is a symbol that is set later to an an address
1353 difference, or an external symbol that is set to an
1354 address difference in another source file, so we want to
1355 eventually remove it. */
9691d64f
JR
1356 if (op->immediate.X_op == O_symbol)
1357 {
1358 op->type = A_DISP_PC;
1359 as_warn (_("Deprecated syntax."));
1360 }
1361 else
1362 {
1363 op->type = A_DISP_PC_ABS;
1364 /* Such operands don't get corrected for PC==.+4, so
1365 make the correction here. */
1366 op->immediate.X_add_number -= 4;
1367 }
252b5132
RH
1368 }
1369 else
1370 {
1371 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
1372 }
1373 }
1374 else
1375 {
1376 as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
1377 }
1378 }
1379 src += len;
1380 if (src[0] != ')')
1381 as_bad (_("expecting )"));
1382 else
1383 src++;
1384 }
1385 else
1386 {
1387 src += parse_reg (src, &mode, &(op->reg));
1388 if (mode != A_REG_N)
006299d3
NC
1389 as_bad (_("illegal register after @"));
1390
252b5132
RH
1391 if (src[0] == '+')
1392 {
1000a02a
NC
1393 char l0, l1;
1394
252b5132 1395 src++;
1000a02a
NC
1396 l0 = TOLOWER (src[0]);
1397 l1 = TOLOWER (src[1]);
1398
1399 if ((l0 == 'r' && l1 == '8')
1400 || (l0 == 'i' && (l1 == 'x' || l1 == 's')))
d4845d57
JR
1401 {
1402 src += 2;
88da98f3 1403 op->type = AX_PMOD_N;
d4845d57 1404 }
006299d3
NC
1405 else if ( (l0 == 'r' && l1 == '9')
1406 || (l0 == 'i' && l1 == 'y'))
d4845d57
JR
1407 {
1408 src += 2;
88da98f3 1409 op->type = AY_PMOD_N;
d4845d57
JR
1410 }
1411 else
1412 op->type = A_INC_N;
252b5132
RH
1413 }
1414 else
006299d3 1415 op->type = A_IND_N;
252b5132
RH
1416 }
1417 return src;
1418}
1419
1420static void
8edc77b9 1421get_operand (char **ptr, sh_operand_info *op)
252b5132
RH
1422{
1423 char *src = *ptr;
1424 int mode = -1;
1425 unsigned int len;
1426
1427 if (src[0] == '#')
1428 {
1429 src++;
015551fc 1430 *ptr = parse_exp (src, op);
252b5132
RH
1431 op->type = A_IMM;
1432 return;
1433 }
1434
1435 else if (src[0] == '@')
1436 {
1437 *ptr = parse_at (src, op);
1438 return;
1439 }
1440 len = parse_reg (src, &mode, &(op->reg));
1441 if (len)
1442 {
1443 *ptr = src + len;
1444 op->type = mode;
1445 return;
1446 }
1447 else
1448 {
6b31947e 1449 /* Not a reg, the only thing left is a displacement. */
015551fc 1450 *ptr = parse_exp (src, op);
252b5132
RH
1451 op->type = A_DISP_PC;
1452 return;
1453 }
1454}
1455
c4aa876b 1456static char *
8edc77b9 1457get_operands (sh_opcode_info *info, char *args, sh_operand_info *operand)
252b5132
RH
1458{
1459 char *ptr = args;
1460 if (info->arg[0])
1461 {
d4845d57
JR
1462 /* The pre-processor will eliminate whitespace in front of '@'
1463 after the first argument; we may be called multiple times
1464 from assemble_ppi, so don't insist on finding whitespace here. */
1465 if (*ptr == ' ')
1466 ptr++;
252b5132
RH
1467
1468 get_operand (&ptr, operand + 0);
1469 if (info->arg[1])
1470 {
1471 if (*ptr == ',')
1472 {
1473 ptr++;
1474 }
1475 get_operand (&ptr, operand + 1);
52ccafd0
JR
1476 /* ??? Hack: psha/pshl have a varying operand number depending on
1477 the type of the first operand. We handle this by having the
1478 three-operand version first and reducing the number of operands
1479 parsed to two if we see that the first operand is an immediate.
1480 This works because no insn with three operands has an immediate
1481 as first operand. */
1482 if (info->arg[2] && operand[0].type != A_IMM)
252b5132
RH
1483 {
1484 if (*ptr == ',')
1485 {
1486 ptr++;
1487 }
1488 get_operand (&ptr, operand + 2);
1489 }
1490 else
1491 {
1492 operand[2].type = 0;
1493 }
1494 }
1495 else
1496 {
1497 operand[1].type = 0;
1498 operand[2].type = 0;
1499 }
1500 }
1501 else
1502 {
1503 operand[0].type = 0;
1504 operand[1].type = 0;
1505 operand[2].type = 0;
1506 }
1507 return ptr;
1508}
1509
1510/* Passed a pointer to a list of opcodes which use different
1511 addressing modes, return the opcode which matches the opcodes
6b31947e 1512 provided. */
252b5132 1513
c4aa876b 1514static sh_opcode_info *
8edc77b9 1515get_specific (sh_opcode_info *opcode, sh_operand_info *operands)
252b5132
RH
1516{
1517 sh_opcode_info *this_try = opcode;
1518 char *name = opcode->name;
1519 int n = 0;
c4aa876b 1520
252b5132
RH
1521 while (opcode->name)
1522 {
1523 this_try = opcode++;
5ff37431 1524 if ((this_try->name != name) && (strcmp (this_try->name, name) != 0))
252b5132
RH
1525 {
1526 /* We've looked so far down the table that we've run out of
6b31947e 1527 opcodes with the same name. */
252b5132
RH
1528 return 0;
1529 }
c4aa876b 1530
6b31947e 1531 /* Look at both operands needed by the opcodes and provided by
252b5132
RH
1532 the user - since an arg test will often fail on the same arg
1533 again and again, we'll try and test the last failing arg the
6b31947e 1534 first on each opcode try. */
252b5132
RH
1535 for (n = 0; this_try->arg[n]; n++)
1536 {
1537 sh_operand_info *user = operands + n;
1538 sh_arg_type arg = this_try->arg[n];
c4aa876b 1539
252b5132
RH
1540 switch (arg)
1541 {
7679ead9
AO
1542 case A_DISP_PC:
1543 if (user->type == A_DISP_PC_ABS)
1544 break;
1545 /* Fall through. */
252b5132
RH
1546 case A_IMM:
1547 case A_BDISP12:
1548 case A_BDISP8:
1549 case A_DISP_GBR:
252b5132
RH
1550 case A_MACH:
1551 case A_PR:
1552 case A_MACL:
1553 if (user->type != arg)
1554 goto fail;
1555 break;
1556 case A_R0:
1557 /* opcode needs r0 */
1558 if (user->type != A_REG_N || user->reg != 0)
1559 goto fail;
1560 break;
1561 case A_R0_GBR:
1562 if (user->type != A_R0_GBR || user->reg != 0)
1563 goto fail;
1564 break;
1565 case F_FR0:
1566 if (user->type != F_REG_N || user->reg != 0)
1567 goto fail;
1568 break;
1569
1570 case A_REG_N:
1571 case A_INC_N:
1572 case A_DEC_N:
1573 case A_IND_N:
1574 case A_IND_R0_REG_N:
1575 case A_DISP_REG_N:
1576 case F_REG_N:
1577 case D_REG_N:
1578 case X_REG_N:
1579 case V_REG_N:
1580 case FPUL_N:
1581 case FPSCR_N:
d4845d57 1582 case DSP_REG_N:
252b5132
RH
1583 /* Opcode needs rn */
1584 if (user->type != arg)
1585 goto fail;
1586 reg_n = user->reg;
1587 break;
252b5132
RH
1588 case DX_REG_N:
1589 if (user->type != D_REG_N && user->type != X_REG_N)
1590 goto fail;
1591 reg_n = user->reg;
1592 break;
1593 case A_GBR:
1594 case A_SR:
1595 case A_VBR:
d4845d57
JR
1596 case A_DSR:
1597 case A_MOD:
1598 case A_RE:
1599 case A_RS:
252b5132
RH
1600 case A_SSR:
1601 case A_SPC:
1602 case A_SGR:
1603 case A_DBR:
1604 if (user->type != arg)
1605 goto fail;
1606 break;
1607
c4aa876b 1608 case A_REG_B:
252b5132
RH
1609 if (user->type != arg)
1610 goto fail;
1611 reg_b = user->reg;
1612 break;
1613
1614 case A_REG_M:
1615 case A_INC_M:
1616 case A_DEC_M:
1617 case A_IND_M:
1618 case A_IND_R0_REG_M:
1619 case A_DISP_REG_M:
d4845d57 1620 case DSP_REG_M:
252b5132
RH
1621 /* Opcode needs rn */
1622 if (user->type != arg - A_REG_M + A_REG_N)
1623 goto fail;
1624 reg_m = user->reg;
1625 break;
1626
88da98f3
MS
1627 case AS_DEC_N:
1628 if (user->type != A_DEC_N)
1629 goto fail;
1630 if (user->reg < 2 || user->reg > 5)
1631 goto fail;
1632 reg_n = user->reg;
1633 break;
1634
1635 case AS_INC_N:
1636 if (user->type != A_INC_N)
1637 goto fail;
1638 if (user->reg < 2 || user->reg > 5)
1639 goto fail;
1640 reg_n = user->reg;
1641 break;
1642
1643 case AS_IND_N:
1644 if (user->type != A_IND_N)
1645 goto fail;
1646 if (user->reg < 2 || user->reg > 5)
1647 goto fail;
1648 reg_n = user->reg;
1649 break;
1650
1651 case AS_PMOD_N:
1652 if (user->type != AX_PMOD_N)
1653 goto fail;
1654 if (user->reg < 2 || user->reg > 5)
1655 goto fail;
1656 reg_n = user->reg;
1657 break;
1658
1659 case AX_INC_N:
1660 if (user->type != A_INC_N)
1661 goto fail;
1662 if (user->reg < 4 || user->reg > 5)
1663 goto fail;
1664 reg_n = user->reg;
1665 break;
1666
1667 case AX_IND_N:
1668 if (user->type != A_IND_N)
1669 goto fail;
1670 if (user->reg < 4 || user->reg > 5)
1671 goto fail;
1672 reg_n = user->reg;
1673 break;
1674
1675 case AX_PMOD_N:
1676 if (user->type != AX_PMOD_N)
1677 goto fail;
1678 if (user->reg < 4 || user->reg > 5)
1679 goto fail;
1680 reg_n = user->reg;
1681 break;
1682
1683 case AXY_INC_N:
1684 if (user->type != A_INC_N)
1685 goto fail;
1686 if ((user->reg < 4 || user->reg > 5)
1687 && (user->reg < 0 || user->reg > 1))
1688 goto fail;
1689 reg_n = user->reg;
1690 break;
1691
1692 case AXY_IND_N:
1693 if (user->type != A_IND_N)
1694 goto fail;
1695 if ((user->reg < 4 || user->reg > 5)
1696 && (user->reg < 0 || user->reg > 1))
1697 goto fail;
1698 reg_n = user->reg;
1699 break;
1700
1701 case AXY_PMOD_N:
1702 if (user->type != AX_PMOD_N)
1703 goto fail;
1704 if ((user->reg < 4 || user->reg > 5)
1705 && (user->reg < 0 || user->reg > 1))
1706 goto fail;
1707 reg_n = user->reg;
1708 break;
1709
1710 case AY_INC_N:
1711 if (user->type != A_INC_N)
1712 goto fail;
1713 if (user->reg < 6 || user->reg > 7)
1714 goto fail;
1715 reg_n = user->reg;
1716 break;
1717
1718 case AY_IND_N:
1719 if (user->type != A_IND_N)
1720 goto fail;
1721 if (user->reg < 6 || user->reg > 7)
1722 goto fail;
1723 reg_n = user->reg;
1724 break;
1725
1726 case AY_PMOD_N:
1727 if (user->type != AY_PMOD_N)
1728 goto fail;
1729 if (user->reg < 6 || user->reg > 7)
1730 goto fail;
1731 reg_n = user->reg;
1732 break;
1733
1734 case AYX_INC_N:
1735 if (user->type != A_INC_N)
1736 goto fail;
1737 if ((user->reg < 6 || user->reg > 7)
1738 && (user->reg < 2 || user->reg > 3))
1739 goto fail;
1740 reg_n = user->reg;
1741 break;
1742
1743 case AYX_IND_N:
1744 if (user->type != A_IND_N)
1745 goto fail;
1746 if ((user->reg < 6 || user->reg > 7)
1747 && (user->reg < 2 || user->reg > 3))
1748 goto fail;
1749 reg_n = user->reg;
1750 break;
1751
1752 case AYX_PMOD_N:
1753 if (user->type != AY_PMOD_N)
1754 goto fail;
1755 if ((user->reg < 6 || user->reg > 7)
1756 && (user->reg < 2 || user->reg > 3))
1757 goto fail;
1758 reg_n = user->reg;
1759 break;
1760
1761 case DSP_REG_A_M:
1762 if (user->type != DSP_REG_N)
1763 goto fail;
1764 if (user->reg != A_A0_NUM
1765 && user->reg != A_A1_NUM)
1766 goto fail;
1767 reg_m = user->reg;
1768 break;
1769
1770 case DSP_REG_AX:
1771 if (user->type != DSP_REG_N)
1772 goto fail;
1773 switch (user->reg)
1774 {
1775 case A_A0_NUM:
1776 reg_x = 0;
1777 break;
1778 case A_A1_NUM:
1779 reg_x = 2;
1780 break;
1781 case A_X0_NUM:
1782 reg_x = 1;
1783 break;
1784 case A_X1_NUM:
1785 reg_x = 3;
1786 break;
1787 default:
1788 goto fail;
1789 }
1790 break;
1791
1792 case DSP_REG_XY:
1793 if (user->type != DSP_REG_N)
1794 goto fail;
1795 switch (user->reg)
1796 {
1797 case A_X0_NUM:
1798 reg_x = 0;
1799 break;
1800 case A_X1_NUM:
1801 reg_x = 2;
1802 break;
1803 case A_Y0_NUM:
1804 reg_x = 1;
1805 break;
1806 case A_Y1_NUM:
1807 reg_x = 3;
1808 break;
1809 default:
1810 goto fail;
1811 }
1812 break;
1813
1814 case DSP_REG_AY:
1815 if (user->type != DSP_REG_N)
1816 goto fail;
1817 switch (user->reg)
1818 {
1819 case A_A0_NUM:
1820 reg_y = 0;
1821 break;
1822 case A_A1_NUM:
1823 reg_y = 1;
1824 break;
1825 case A_Y0_NUM:
1826 reg_y = 2;
1827 break;
1828 case A_Y1_NUM:
1829 reg_y = 3;
1830 break;
1831 default:
1832 goto fail;
1833 }
1834 break;
1835
1836 case DSP_REG_YX:
1837 if (user->type != DSP_REG_N)
1838 goto fail;
1839 switch (user->reg)
1840 {
1841 case A_Y0_NUM:
1842 reg_y = 0;
1843 break;
1844 case A_Y1_NUM:
1845 reg_y = 1;
1846 break;
1847 case A_X0_NUM:
1848 reg_y = 2;
1849 break;
1850 case A_X1_NUM:
1851 reg_y = 3;
1852 break;
1853 default:
1854 goto fail;
1855 }
1856 break;
1857
d4845d57
JR
1858 case DSP_REG_X:
1859 if (user->type != DSP_REG_N)
1860 goto fail;
1861 switch (user->reg)
1862 {
1863 case A_X0_NUM:
1864 reg_x = 0;
1865 break;
1866 case A_X1_NUM:
1867 reg_x = 1;
1868 break;
1869 case A_A0_NUM:
1870 reg_x = 2;
1871 break;
1872 case A_A1_NUM:
1873 reg_x = 3;
1874 break;
1875 default:
1876 goto fail;
1877 }
1878 break;
1879
1880 case DSP_REG_Y:
1881 if (user->type != DSP_REG_N)
1882 goto fail;
1883 switch (user->reg)
1884 {
1885 case A_Y0_NUM:
1886 reg_y = 0;
1887 break;
1888 case A_Y1_NUM:
1889 reg_y = 1;
1890 break;
1891 case A_M0_NUM:
1892 reg_y = 2;
1893 break;
1894 case A_M1_NUM:
1895 reg_y = 3;
1896 break;
1897 default:
1898 goto fail;
1899 }
1900 break;
1901
1902 case DSP_REG_E:
1903 if (user->type != DSP_REG_N)
1904 goto fail;
1905 switch (user->reg)
1906 {
1907 case A_X0_NUM:
1908 reg_efg = 0 << 10;
1909 break;
1910 case A_X1_NUM:
1911 reg_efg = 1 << 10;
1912 break;
1913 case A_Y0_NUM:
1914 reg_efg = 2 << 10;
1915 break;
1916 case A_A1_NUM:
1917 reg_efg = 3 << 10;
1918 break;
1919 default:
1920 goto fail;
1921 }
1922 break;
1923
1924 case DSP_REG_F:
1925 if (user->type != DSP_REG_N)
1926 goto fail;
1927 switch (user->reg)
1928 {
1929 case A_Y0_NUM:
1930 reg_efg |= 0 << 8;
1931 break;
1932 case A_Y1_NUM:
1933 reg_efg |= 1 << 8;
1934 break;
1935 case A_X0_NUM:
1936 reg_efg |= 2 << 8;
1937 break;
1938 case A_A1_NUM:
1939 reg_efg |= 3 << 8;
1940 break;
1941 default:
1942 goto fail;
1943 }
1944 break;
1945
1946 case DSP_REG_G:
1947 if (user->type != DSP_REG_N)
1948 goto fail;
1949 switch (user->reg)
1950 {
1951 case A_M0_NUM:
1952 reg_efg |= 0 << 2;
1953 break;
1954 case A_M1_NUM:
1955 reg_efg |= 1 << 2;
1956 break;
1957 case A_A0_NUM:
1958 reg_efg |= 2 << 2;
1959 break;
1960 case A_A1_NUM:
1961 reg_efg |= 3 << 2;
1962 break;
1963 default:
1964 goto fail;
1965 }
1966 break;
1967
1968 case A_A0:
1969 if (user->type != DSP_REG_N || user->reg != A_A0_NUM)
1970 goto fail;
1971 break;
1972 case A_X0:
1973 if (user->type != DSP_REG_N || user->reg != A_X0_NUM)
1974 goto fail;
1975 break;
1976 case A_X1:
1977 if (user->type != DSP_REG_N || user->reg != A_X1_NUM)
1978 goto fail;
1979 break;
1980 case A_Y0:
1981 if (user->type != DSP_REG_N || user->reg != A_Y0_NUM)
1982 goto fail;
1983 break;
1984 case A_Y1:
1985 if (user->type != DSP_REG_N || user->reg != A_Y1_NUM)
1986 goto fail;
1987 break;
1988
252b5132
RH
1989 case F_REG_M:
1990 case D_REG_M:
1991 case X_REG_M:
1992 case V_REG_M:
1993 case FPUL_M:
1994 case FPSCR_M:
1995 /* Opcode needs rn */
1996 if (user->type != arg - F_REG_M + F_REG_N)
1997 goto fail;
1998 reg_m = user->reg;
1999 break;
2000 case DX_REG_M:
2001 if (user->type != D_REG_N && user->type != X_REG_N)
2002 goto fail;
2003 reg_m = user->reg;
2004 break;
2005 case XMTRX_M4:
2006 if (user->type != XMTRX_M4)
2007 goto fail;
2008 reg_m = 4;
2009 break;
c4aa876b 2010
252b5132
RH
2011 default:
2012 printf (_("unhandled %d\n"), arg);
2013 goto fail;
2014 }
2015 }
a37c8f88
JR
2016 if ( !(valid_arch & this_try->arch))
2017 goto fail;
d4845d57 2018 valid_arch &= this_try->arch;
252b5132 2019 return this_try;
c4aa876b
NC
2020 fail:
2021 ;
252b5132
RH
2022 }
2023
2024 return 0;
2025}
2026
252b5132 2027static void
8edc77b9 2028insert (char *where, int how, int pcrel, sh_operand_info *op)
252b5132
RH
2029{
2030 fix_new_exp (frag_now,
2031 where - frag_now->fr_literal,
2032 2,
015551fc 2033 &op->immediate,
252b5132
RH
2034 pcrel,
2035 how);
2036}
2037
2038static void
8edc77b9 2039build_relax (sh_opcode_info *opcode, sh_operand_info *op)
252b5132
RH
2040{
2041 int high_byte = target_big_endian ? 0 : 1;
2042 char *p;
2043
2044 if (opcode->arg[0] == A_BDISP8)
2045 {
2046 int what = (opcode->nibbles[1] & 4) ? COND_JUMP_DELAY : COND_JUMP;
2047 p = frag_var (rs_machine_dependent,
2048 md_relax_table[C (what, COND32)].rlx_length,
2049 md_relax_table[C (what, COND8)].rlx_length,
2050 C (what, 0),
015551fc
JR
2051 op->immediate.X_add_symbol,
2052 op->immediate.X_add_number,
252b5132
RH
2053 0);
2054 p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
2055 }
2056 else if (opcode->arg[0] == A_BDISP12)
2057 {
2058 p = frag_var (rs_machine_dependent,
2059 md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
2060 md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
2061 C (UNCOND_JUMP, 0),
015551fc
JR
2062 op->immediate.X_add_symbol,
2063 op->immediate.X_add_number,
252b5132
RH
2064 0);
2065 p[high_byte] = (opcode->nibbles[0] << 4);
2066 }
2067
2068}
2069
6b31947e 2070/* Insert ldrs & ldre with fancy relocations that relaxation can recognize. */
d67b5d6d 2071
015551fc 2072static char *
8edc77b9 2073insert_loop_bounds (char *output, sh_operand_info *operand)
015551fc
JR
2074{
2075 char *name;
2076 symbolS *end_sym;
2077
2078 /* Since the low byte of the opcode will be overwritten by the reloc, we
2079 can just stash the high byte into both bytes and ignore endianness. */
2080 output[0] = 0x8c;
2081 output[1] = 0x8c;
2082 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
2083 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
2084
2085 if (sh_relax)
2086 {
2087 static int count = 0;
2088
2089 /* If the last loop insn is a two-byte-insn, it is in danger of being
2090 swapped with the insn after it. To prevent this, create a new
2091 symbol - complete with SH_LABEL reloc - after the last loop insn.
2092 If the last loop insn is four bytes long, the symbol will be
2093 right in the middle, but four byte insns are not swapped anyways. */
2094 /* A REPEAT takes 6 bytes. The SH has a 32 bit address space.
2095 Hence a 9 digit number should be enough to count all REPEATs. */
2096 name = alloca (11);
2097 sprintf (name, "_R%x", count++ & 0x3fffffff);
c4aa876b 2098 end_sym = symbol_new (name, undefined_section, 0, &zero_address_frag);
015551fc
JR
2099 /* Make this a local symbol. */
2100#ifdef OBJ_COFF
2101 SF_SET_LOCAL (end_sym);
2102#endif /* OBJ_COFF */
2103 symbol_table_insert (end_sym);
2104 end_sym->sy_value = operand[1].immediate;
2105 end_sym->sy_value.X_add_number += 2;
2106 fix_new (frag_now, frag_now_fix (), 2, end_sym, 0, 1, BFD_RELOC_SH_LABEL);
2107 }
2108
2109 output = frag_more (2);
2110 output[0] = 0x8e;
2111 output[1] = 0x8e;
2112 insert (output, BFD_RELOC_SH_LOOP_START, 1, operand);
2113 insert (output, BFD_RELOC_SH_LOOP_END, 1, operand + 1);
2114
2115 return frag_more (2);
2116}
2117
d67b5d6d 2118/* Now we know what sort of opcodes it is, let's build the bytes. */
6b31947e 2119
0d10e182 2120static unsigned int
8edc77b9 2121build_Mytes (sh_opcode_info *opcode, sh_operand_info *operand)
252b5132
RH
2122{
2123 int index;
2124 char nbuf[4];
2125 char *output = frag_more (2);
0d10e182 2126 unsigned int size = 2;
252b5132
RH
2127 int low_byte = target_big_endian ? 1 : 0;
2128 nbuf[0] = 0;
2129 nbuf[1] = 0;
2130 nbuf[2] = 0;
2131 nbuf[3] = 0;
2132
2133 for (index = 0; index < 4; index++)
2134 {
2135 sh_nibble_type i = opcode->nibbles[index];
2136 if (i < 16)
2137 {
2138 nbuf[index] = i;
2139 }
2140 else
2141 {
2142 switch (i)
2143 {
2144 case REG_N:
2145 nbuf[index] = reg_n;
2146 break;
2147 case REG_M:
2148 nbuf[index] = reg_m;
2149 break;
d4845d57
JR
2150 case SDT_REG_N:
2151 if (reg_n < 2 || reg_n > 5)
2152 as_bad (_("Invalid register: 'r%d'"), reg_n);
2153 nbuf[index] = (reg_n & 3) | 4;
2154 break;
252b5132
RH
2155 case REG_NM:
2156 nbuf[index] = reg_n | (reg_m >> 2);
2157 break;
c4aa876b 2158 case REG_B:
252b5132
RH
2159 nbuf[index] = reg_b | 0x08;
2160 break;
015551fc
JR
2161 case IMM0_4BY4:
2162 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand);
2163 break;
2164 case IMM0_4BY2:
2165 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand);
2166 break;
2167 case IMM0_4:
2168 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand);
2169 break;
2170 case IMM1_4BY4:
2171 insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0, operand + 1);
2172 break;
2173 case IMM1_4BY2:
2174 insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0, operand + 1);
252b5132 2175 break;
015551fc
JR
2176 case IMM1_4:
2177 insert (output + low_byte, BFD_RELOC_SH_IMM4, 0, operand + 1);
252b5132 2178 break;
015551fc
JR
2179 case IMM0_8BY4:
2180 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand);
252b5132 2181 break;
015551fc
JR
2182 case IMM0_8BY2:
2183 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand);
252b5132 2184 break;
015551fc
JR
2185 case IMM0_8:
2186 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand);
252b5132 2187 break;
015551fc
JR
2188 case IMM1_8BY4:
2189 insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0, operand + 1);
252b5132 2190 break;
015551fc
JR
2191 case IMM1_8BY2:
2192 insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0, operand + 1);
2193 break;
2194 case IMM1_8:
2195 insert (output + low_byte, BFD_RELOC_SH_IMM8, 0, operand + 1);
252b5132
RH
2196 break;
2197 case PCRELIMM_8BY4:
7679ead9
AO
2198 insert (output, BFD_RELOC_SH_PCRELIMM8BY4,
2199 operand->type != A_DISP_PC_ABS, operand);
252b5132
RH
2200 break;
2201 case PCRELIMM_8BY2:
7679ead9
AO
2202 insert (output, BFD_RELOC_SH_PCRELIMM8BY2,
2203 operand->type != A_DISP_PC_ABS, operand);
015551fc
JR
2204 break;
2205 case REPEAT:
2206 output = insert_loop_bounds (output, operand);
2207 nbuf[index] = opcode->nibbles[3];
2208 operand += 2;
252b5132
RH
2209 break;
2210 default:
2211 printf (_("failed for %d\n"), i);
2212 }
2213 }
2214 }
c4aa876b
NC
2215 if (!target_big_endian)
2216 {
2217 output[1] = (nbuf[0] << 4) | (nbuf[1]);
2218 output[0] = (nbuf[2] << 4) | (nbuf[3]);
2219 }
2220 else
2221 {
2222 output[0] = (nbuf[0] << 4) | (nbuf[1]);
2223 output[1] = (nbuf[2] << 4) | (nbuf[3]);
2224 }
0d10e182 2225 return size;
252b5132
RH
2226}
2227
d4845d57
JR
2228/* Find an opcode at the start of *STR_P in the hash table, and set
2229 *STR_P to the first character after the last one read. */
252b5132 2230
d4845d57 2231static sh_opcode_info *
8edc77b9 2232find_cooked_opcode (char **str_p)
252b5132 2233{
d4845d57 2234 char *str = *str_p;
252b5132
RH
2235 unsigned char *op_start;
2236 unsigned char *op_end;
252b5132
RH
2237 char name[20];
2238 int nlen = 0;
c4aa876b 2239
6b31947e 2240 /* Drop leading whitespace. */
252b5132
RH
2241 while (*str == ' ')
2242 str++;
2243
d4845d57
JR
2244 /* Find the op code end.
2245 The pre-processor will eliminate whitespace in front of
2246 any '@' after the first argument; we may be called from
2247 assemble_ppi, so the opcode might be terminated by an '@'. */
252b5132
RH
2248 for (op_start = op_end = (unsigned char *) (str);
2249 *op_end
2250 && nlen < 20
d4845d57 2251 && !is_end_of_line[*op_end] && *op_end != ' ' && *op_end != '@';
252b5132
RH
2252 op_end++)
2253 {
2254 unsigned char c = op_start[nlen];
2255
2256 /* The machine independent code will convert CMP/EQ into cmp/EQ
d4845d57
JR
2257 because it thinks the '/' is the end of the symbol. Moreover,
2258 all but the first sub-insn is a parallel processing insn won't
3882b010 2259 be capitalized. Instead of hacking up the machine independent
d4845d57 2260 code, we just deal with it here. */
3882b010 2261 c = TOLOWER (c);
252b5132
RH
2262 name[nlen] = c;
2263 nlen++;
2264 }
c4aa876b 2265
252b5132 2266 name[nlen] = 0;
d4845d57 2267 *str_p = op_end;
252b5132
RH
2268
2269 if (nlen == 0)
6b31947e 2270 as_bad (_("can't find opcode "));
252b5132 2271
d4845d57
JR
2272 return (sh_opcode_info *) hash_find (opcode_hash_control, name);
2273}
2274
2275/* Assemble a parallel processing insn. */
2276#define DDT_BASE 0xf000 /* Base value for double data transfer insns */
6b31947e 2277
0d10e182 2278static unsigned int
8edc77b9 2279assemble_ppi (char *op_end, sh_opcode_info *opcode)
d4845d57
JR
2280{
2281 int movx = 0;
2282 int movy = 0;
2283 int cond = 0;
2284 int field_b = 0;
2285 char *output;
2286 int move_code;
0d10e182 2287 unsigned int size;
d4845d57 2288
d4845d57
JR
2289 for (;;)
2290 {
2291 sh_operand_info operand[3];
2292
ac62e7a3
JR
2293 /* Some insn ignore one or more register fields, e.g. psts machl,a0.
2294 Make sure we encode a defined insn pattern. */
2295 reg_x = 0;
2296 reg_y = 0;
2297 reg_n = 0;
2298
d4845d57
JR
2299 if (opcode->arg[0] != A_END)
2300 op_end = get_operands (opcode, op_end, operand);
88da98f3 2301 try_another_opcode:
d4845d57
JR
2302 opcode = get_specific (opcode, operand);
2303 if (opcode == 0)
2304 {
6b31947e 2305 /* Couldn't find an opcode which matched the operands. */
d4845d57 2306 char *where = frag_more (2);
0d10e182 2307 size = 2;
d4845d57
JR
2308
2309 where[0] = 0x0;
2310 where[1] = 0x0;
2311 as_bad (_("invalid operands for opcode"));
0d10e182 2312 return size;
d4845d57 2313 }
c4aa876b 2314
d4845d57
JR
2315 if (opcode->nibbles[0] != PPI)
2316 as_bad (_("insn can't be combined with parallel processing insn"));
2317
2318 switch (opcode->nibbles[1])
2319 {
2320
2321 case NOPX:
2322 if (movx)
2323 as_bad (_("multiple movx specifications"));
2324 movx = DDT_BASE;
2325 break;
2326 case NOPY:
2327 if (movy)
2328 as_bad (_("multiple movy specifications"));
2329 movy = DDT_BASE;
2330 break;
2331
88da98f3
MS
2332 case MOVX_NOPY:
2333 if (movx)
2334 as_bad (_("multiple movx specifications"));
2335 if ((reg_n < 4 || reg_n > 5)
2336 && (reg_n < 0 || reg_n > 1))
2337 as_bad (_("invalid movx address register"));
2338 if (movy && movy != DDT_BASE)
2339 as_bad (_("insn cannot be combined with non-nopy"));
2340 movx = ((((reg_n & 1) != 0) << 9)
2341 + (((reg_n & 4) == 0) << 8)
2342 + (reg_x << 6)
2343 + (opcode->nibbles[2] << 4)
2344 + opcode->nibbles[3]
2345 + DDT_BASE);
2346 break;
2347
2348 case MOVY_NOPX:
2349 if (movy)
2350 as_bad (_("multiple movy specifications"));
2351 if ((reg_n < 6 || reg_n > 7)
2352 && (reg_n < 2 || reg_n > 3))
2353 as_bad (_("invalid movy address register"));
2354 if (movx && movx != DDT_BASE)
2355 as_bad (_("insn cannot be combined with non-nopx"));
2356 movy = ((((reg_n & 1) != 0) << 8)
2357 + (((reg_n & 4) == 0) << 9)
2358 + (reg_y << 6)
2359 + (opcode->nibbles[2] << 4)
2360 + opcode->nibbles[3]
2361 + DDT_BASE);
2362 break;
2363
d4845d57
JR
2364 case MOVX:
2365 if (movx)
2366 as_bad (_("multiple movx specifications"));
88da98f3
MS
2367 if (movy & 0x2ac)
2368 as_bad (_("previous movy requires nopx"));
d4845d57
JR
2369 if (reg_n < 4 || reg_n > 5)
2370 as_bad (_("invalid movx address register"));
2371 if (opcode->nibbles[2] & 8)
2372 {
2373 if (reg_m == A_A1_NUM)
2374 movx = 1 << 7;
2375 else if (reg_m != A_A0_NUM)
2376 as_bad (_("invalid movx dsp register"));
2377 }
2378 else
2379 {
2380 if (reg_x > 1)
2381 as_bad (_("invalid movx dsp register"));
2382 movx = reg_x << 7;
2383 }
2384 movx += ((reg_n - 4) << 9) + (opcode->nibbles[2] << 2) + DDT_BASE;
2385 break;
2386
2387 case MOVY:
2388 if (movy)
2389 as_bad (_("multiple movy specifications"));
88da98f3
MS
2390 if (movx & 0x153)
2391 as_bad (_("previous movx requires nopy"));
d4845d57
JR
2392 if (opcode->nibbles[2] & 8)
2393 {
2394 /* Bit 3 in nibbles[2] is intended for bit 4 of the opcode,
2395 so add 8 more. */
2396 movy = 8;
2397 if (reg_m == A_A1_NUM)
2398 movy += 1 << 6;
2399 else if (reg_m != A_A0_NUM)
2400 as_bad (_("invalid movy dsp register"));
2401 }
2402 else
2403 {
2404 if (reg_y > 1)
2405 as_bad (_("invalid movy dsp register"));
2406 movy = reg_y << 6;
2407 }
2408 if (reg_n < 6 || reg_n > 7)
2409 as_bad (_("invalid movy address register"));
2410 movy += ((reg_n - 6) << 8) + opcode->nibbles[2] + DDT_BASE;
2411 break;
2412
2413 case PSH:
015551fc 2414 if (operand[0].immediate.X_op != O_constant)
d4845d57
JR
2415 as_bad (_("dsp immediate shift value not constant"));
2416 field_b = ((opcode->nibbles[2] << 12)
015551fc 2417 | (operand[0].immediate.X_add_number & 127) << 4
d4845d57
JR
2418 | reg_n);
2419 break;
88da98f3
MS
2420 case PPI3NC:
2421 if (cond)
2422 {
2423 opcode++;
2424 goto try_another_opcode;
2425 }
2426 /* Fall through. */
d4845d57
JR
2427 case PPI3:
2428 if (field_b)
2429 as_bad (_("multiple parallel processing specifications"));
2430 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2431 + (reg_x << 6) + (reg_y << 4) + reg_n);
88da98f3
MS
2432 switch (opcode->nibbles[4])
2433 {
2434 case HEX_0:
2435 case HEX_XX00:
2436 case HEX_00YY:
2437 break;
2438 case HEX_1:
2439 case HEX_4:
2440 field_b += opcode->nibbles[4] << 4;
2441 break;
2442 default:
2443 abort ();
2444 }
d4845d57
JR
2445 break;
2446 case PDC:
2447 if (cond)
2448 as_bad (_("multiple condition specifications"));
2449 cond = opcode->nibbles[2] << 8;
2450 if (*op_end)
2451 goto skip_cond_check;
2452 break;
2453 case PPIC:
2454 if (field_b)
2455 as_bad (_("multiple parallel processing specifications"));
2456 field_b = ((opcode->nibbles[2] << 12) + (opcode->nibbles[3] << 8)
2457 + cond + (reg_x << 6) + (reg_y << 4) + reg_n);
2458 cond = 0;
88da98f3
MS
2459 switch (opcode->nibbles[4])
2460 {
2461 case HEX_0:
2462 case HEX_XX00:
2463 case HEX_00YY:
2464 break;
2465 case HEX_1:
2466 case HEX_4:
2467 field_b += opcode->nibbles[4] << 4;
2468 break;
2469 default:
2470 abort ();
2471 }
d4845d57
JR
2472 break;
2473 case PMUL:
2474 if (field_b)
2475 {
88da98f3
MS
2476 if ((field_b & 0xef00) == 0xa100)
2477 field_b -= 0x8100;
2478 /* pclr Dz pmuls Se,Sf,Dg */
2479 else if ((field_b & 0xff00) == 0x8d00
2480 && (valid_arch & arch_sh4al_dsp_up))
2481 {
2482 valid_arch &= arch_sh4al_dsp_up;
2483 field_b -= 0x8cf0;
2484 }
2485 else
d4845d57 2486 as_bad (_("insn cannot be combined with pmuls"));
d4845d57
JR
2487 switch (field_b & 0xf)
2488 {
2489 case A_X0_NUM:
2490 field_b += 0 - A_X0_NUM;
2491 break;
2492 case A_Y0_NUM:
2493 field_b += 1 - A_Y0_NUM;
2494 break;
2495 case A_A0_NUM:
2496 field_b += 2 - A_A0_NUM;
2497 break;
2498 case A_A1_NUM:
2499 field_b += 3 - A_A1_NUM;
2500 break;
2501 default:
88da98f3 2502 as_bad (_("bad combined pmuls output operand"));
d4845d57 2503 }
7dd04abd
JR
2504 /* Generate warning if the destination register for padd / psub
2505 and pmuls is the same ( only for A0 or A1 ).
2506 If the last nibble is 1010 then A0 is used in both
2507 padd / psub and pmuls. If it is 1111 then A1 is used
2508 as destination register in both padd / psub and pmuls. */
5db33d76
JR
2509
2510 if ((((field_b | reg_efg) & 0x000F) == 0x000A)
2511 || (((field_b | reg_efg) & 0x000F) == 0x000F))
2512 as_warn (_("destination register is same for parallel insns"));
d4845d57
JR
2513 }
2514 field_b += 0x4000 + reg_efg;
2515 break;
2516 default:
2517 abort ();
2518 }
2519 if (cond)
2520 {
2521 as_bad (_("condition not followed by conditionalizable insn"));
2522 cond = 0;
2523 }
2524 if (! *op_end)
2525 break;
2526 skip_cond_check:
2527 opcode = find_cooked_opcode (&op_end);
2528 if (opcode == NULL)
2529 {
2530 (as_bad
2531 (_("unrecognized characters at end of parallel processing insn")));
2532 break;
2533 }
2534 }
2535
2536 move_code = movx | movy;
2537 if (field_b)
2538 {
2539 /* Parallel processing insn. */
2540 unsigned long ppi_code = (movx | movy | 0xf800) << 16 | field_b;
2541
2542 output = frag_more (4);
0d10e182 2543 size = 4;
d4845d57
JR
2544 if (! target_big_endian)
2545 {
2546 output[3] = ppi_code >> 8;
2547 output[2] = ppi_code;
2548 }
2549 else
2550 {
2551 output[2] = ppi_code >> 8;
2552 output[3] = ppi_code;
2553 }
2554 move_code |= 0xf800;
2555 }
2556 else
0d10e182
JL
2557 {
2558 /* Just a double data transfer. */
2559 output = frag_more (2);
2560 size = 2;
2561 }
d4845d57
JR
2562 if (! target_big_endian)
2563 {
2564 output[1] = move_code >> 8;
2565 output[0] = move_code;
2566 }
2567 else
2568 {
2569 output[0] = move_code >> 8;
2570 output[1] = move_code;
2571 }
0d10e182 2572 return size;
d4845d57
JR
2573}
2574
2575/* This is the guts of the machine-dependent assembler. STR points to a
2576 machine dependent instruction. This function is supposed to emit
6b31947e 2577 the frags/bytes it assembles to. */
d4845d57
JR
2578
2579void
8edc77b9 2580md_assemble (char *str)
d4845d57
JR
2581{
2582 unsigned char *op_end;
2583 sh_operand_info operand[3];
2584 sh_opcode_info *opcode;
dda5ecfc 2585 unsigned int size = 0;
d4845d57 2586
324bfcf3
AO
2587#ifdef HAVE_SH64
2588 if (sh64_isa_mode == sh64_isa_shmedia)
2589 {
2590 shmedia_md_assemble (str);
2591 return;
2592 }
2593 else
2594 {
2595 /* If we've seen pseudo-directives, make sure any emitted data or
2596 frags are marked as data. */
b34976b6 2597 if (!seen_insn)
324bfcf3 2598 {
b34976b6 2599 sh64_update_contents_mark (TRUE);
324bfcf3
AO
2600 sh64_set_contents_type (CRT_SH5_ISA16);
2601 }
2602
b34976b6 2603 seen_insn = TRUE;
324bfcf3
AO
2604 }
2605#endif /* HAVE_SH64 */
2606
d4845d57
JR
2607 opcode = find_cooked_opcode (&str);
2608 op_end = str;
252b5132
RH
2609
2610 if (opcode == NULL)
2611 {
2612 as_bad (_("unknown opcode"));
2613 return;
2614 }
2615
2616 if (sh_relax
2617 && ! seg_info (now_seg)->tc_segment_info_data.in_code)
2618 {
2619 /* Output a CODE reloc to tell the linker that the following
2620 bytes are instructions, not data. */
2621 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
2622 BFD_RELOC_SH_CODE);
2623 seg_info (now_seg)->tc_segment_info_data.in_code = 1;
2624 }
2625
d4845d57
JR
2626 if (opcode->nibbles[0] == PPI)
2627 {
0d10e182 2628 size = assemble_ppi (op_end, opcode);
252b5132
RH
2629 }
2630 else
2631 {
0d10e182
JL
2632 if (opcode->arg[0] == A_BDISP12
2633 || opcode->arg[0] == A_BDISP8)
252b5132 2634 {
26c9b704
JR
2635 /* Since we skip get_specific here, we have to check & update
2636 valid_arch now. */
2637 if (valid_arch & opcode->arch)
2638 valid_arch &= opcode->arch;
2639 else
2640 as_bad (_("Delayed branches not available on SH1"));
0d10e182
JL
2641 parse_exp (op_end + 1, &operand[0]);
2642 build_relax (opcode, &operand[0]);
5fc44b2d
JR
2643 }
2644 else
2645 {
0d10e182
JL
2646 if (opcode->arg[0] == A_END)
2647 {
2648 /* Ignore trailing whitespace. If there is any, it has already
2649 been compressed to a single space. */
2650 if (*op_end == ' ')
2651 op_end++;
2652 }
2653 else
2654 {
2655 op_end = get_operands (opcode, op_end, operand);
2656 }
2657 opcode = get_specific (opcode, operand);
252b5132 2658
0d10e182
JL
2659 if (opcode == 0)
2660 {
2661 /* Couldn't find an opcode which matched the operands. */
2662 char *where = frag_more (2);
2663 size = 2;
252b5132 2664
0d10e182
JL
2665 where[0] = 0x0;
2666 where[1] = 0x0;
2667 as_bad (_("invalid operands for opcode"));
2668 }
2669 else
2670 {
2671 if (*op_end)
2672 as_bad (_("excess operands: '%s'"), op_end);
2673
2674 size = build_Mytes (opcode, operand);
2675 }
252b5132 2676 }
0d10e182 2677 }
252b5132 2678
2bc0a128 2679#ifdef BFD_ASSEMBLER
4dc7ead9 2680 dwarf2_emit_insn (size);
2bc0a128 2681#endif
252b5132
RH
2682}
2683
2684/* This routine is called each time a label definition is seen. It
2685 emits a BFD_RELOC_SH_LABEL reloc if necessary. */
2686
2687void
8edc77b9 2688sh_frob_label (void)
252b5132
RH
2689{
2690 static fragS *last_label_frag;
2691 static int last_label_offset;
2692
2693 if (sh_relax
2694 && seg_info (now_seg)->tc_segment_info_data.in_code)
2695 {
2696 int offset;
2697
2698 offset = frag_now_fix ();
2699 if (frag_now != last_label_frag
2700 || offset != last_label_offset)
c4aa876b 2701 {
252b5132
RH
2702 fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, BFD_RELOC_SH_LABEL);
2703 last_label_frag = frag_now;
2704 last_label_offset = offset;
2705 }
2706 }
2707}
2708
2709/* This routine is called when the assembler is about to output some
2710 data. It emits a BFD_RELOC_SH_DATA reloc if necessary. */
2711
2712void
8edc77b9 2713sh_flush_pending_output (void)
252b5132
RH
2714{
2715 if (sh_relax
2716 && seg_info (now_seg)->tc_segment_info_data.in_code)
2717 {
2718 fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
2719 BFD_RELOC_SH_DATA);
2720 seg_info (now_seg)->tc_segment_info_data.in_code = 0;
2721 }
2722}
2723
2724symbolS *
8edc77b9 2725md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
252b5132
RH
2726{
2727 return 0;
2728}
2729
2730#ifdef OBJ_COFF
056350c6 2731#ifndef BFD_ASSEMBLER
252b5132
RH
2732
2733void
8edc77b9 2734tc_crawl_symbol_chain (object_headers *headers ATTRIBUTE_UNUSED)
252b5132
RH
2735{
2736 printf (_("call to tc_crawl_symbol_chain \n"));
2737}
2738
2739void
8edc77b9 2740tc_headers_hook (object_headers *headers ATTRIBUTE_UNUSED)
252b5132
RH
2741{
2742 printf (_("call to tc_headers_hook \n"));
2743}
2744
056350c6 2745#endif
252b5132
RH
2746#endif
2747
6b31947e
NC
2748/* Various routines to kill one day. */
2749/* Equal to MAX_PRECISION in atof-ieee.c. */
252b5132
RH
2750#define MAX_LITTLENUMS 6
2751
6b31947e
NC
2752/* Turn a string in input_line_pointer into a floating point constant
2753 of type TYPE, and store the appropriate bytes in *LITP. The number
2754 of LITTLENUMS emitted is stored in *SIZEP . An error message is
2755 returned, or NULL on OK. */
2756
252b5132 2757char *
8edc77b9 2758md_atof (int type, char *litP, int *sizeP)
252b5132
RH
2759{
2760 int prec;
2761 LITTLENUM_TYPE words[4];
2762 char *t;
2763 int i;
2764
2765 switch (type)
2766 {
2767 case 'f':
2768 prec = 2;
2769 break;
2770
2771 case 'd':
2772 prec = 4;
2773 break;
2774
2775 default:
2776 *sizeP = 0;
2777 return _("bad call to md_atof");
2778 }
2779
2780 t = atof_ieee (input_line_pointer, type, words);
2781 if (t)
2782 input_line_pointer = t;
2783
2784 *sizeP = prec * 2;
2785
2786 if (! target_big_endian)
2787 {
2788 for (i = prec - 1; i >= 0; i--)
2789 {
2790 md_number_to_chars (litP, (valueT) words[i], 2);
2791 litP += 2;
2792 }
2793 }
2794 else
2795 {
2796 for (i = 0; i < prec; i++)
2797 {
2798 md_number_to_chars (litP, (valueT) words[i], 2);
2799 litP += 2;
2800 }
2801 }
c4aa876b 2802
252b5132
RH
2803 return NULL;
2804}
2805
2806/* Handle the .uses pseudo-op. This pseudo-op is used just before a
2807 call instruction. It refers to a label of the instruction which
2808 loads the register which the call uses. We use it to generate a
2809 special reloc for the linker. */
2810
2811static void
8edc77b9 2812s_uses (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
2813{
2814 expressionS ex;
2815
2816 if (! sh_relax)
2817 as_warn (_(".uses pseudo-op seen when not relaxing"));
2818
2819 expression (&ex);
2820
2821 if (ex.X_op != O_symbol || ex.X_add_number != 0)
2822 {
2823 as_bad (_("bad .uses format"));
2824 ignore_rest_of_line ();
2825 return;
2826 }
2827
2828 fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, BFD_RELOC_SH_USES);
2829
2830 demand_empty_rest_of_line ();
2831}
2832\f
5a38dc70 2833const char *md_shortopts = "";
6b31947e
NC
2834struct option md_longopts[] =
2835{
252b5132 2836#define OPTION_RELAX (OPTION_MD_BASE)
05982cac
HPN
2837#define OPTION_BIG (OPTION_MD_BASE + 1)
2838#define OPTION_LITTLE (OPTION_BIG + 1)
252b5132 2839#define OPTION_SMALL (OPTION_LITTLE + 1)
d4845d57 2840#define OPTION_DSP (OPTION_SMALL + 1)
bdfaef52 2841#define OPTION_ISA (OPTION_DSP + 1)
252b5132
RH
2842
2843 {"relax", no_argument, NULL, OPTION_RELAX},
05982cac 2844 {"big", no_argument, NULL, OPTION_BIG},
252b5132
RH
2845 {"little", no_argument, NULL, OPTION_LITTLE},
2846 {"small", no_argument, NULL, OPTION_SMALL},
d4845d57 2847 {"dsp", no_argument, NULL, OPTION_DSP},
bdfaef52 2848 {"isa", required_argument, NULL, OPTION_ISA},
324bfcf3 2849#ifdef HAVE_SH64
324bfcf3
AO
2850#define OPTION_ABI (OPTION_ISA + 1)
2851#define OPTION_NO_MIX (OPTION_ABI + 1)
2852#define OPTION_SHCOMPACT_CONST_CRANGE (OPTION_NO_MIX + 1)
2853#define OPTION_NO_EXPAND (OPTION_SHCOMPACT_CONST_CRANGE + 1)
2854#define OPTION_PT32 (OPTION_NO_EXPAND + 1)
324bfcf3
AO
2855 {"abi", required_argument, NULL, OPTION_ABI},
2856 {"no-mix", no_argument, NULL, OPTION_NO_MIX},
2857 {"shcompact-const-crange", no_argument, NULL, OPTION_SHCOMPACT_CONST_CRANGE},
2858 {"no-expand", no_argument, NULL, OPTION_NO_EXPAND},
2859 {"expand-pt32", no_argument, NULL, OPTION_PT32},
2860#endif /* HAVE_SH64 */
2861
252b5132
RH
2862 {NULL, no_argument, NULL, 0}
2863};
c4aa876b 2864size_t md_longopts_size = sizeof (md_longopts);
252b5132
RH
2865
2866int
8edc77b9 2867md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
252b5132
RH
2868{
2869 switch (c)
2870 {
2871 case OPTION_RELAX:
2872 sh_relax = 1;
2873 break;
2874
05982cac
HPN
2875 case OPTION_BIG:
2876 target_big_endian = 1;
2877 break;
2878
252b5132 2879 case OPTION_LITTLE:
252b5132
RH
2880 target_big_endian = 0;
2881 break;
2882
2883 case OPTION_SMALL:
2884 sh_small = 1;
2885 break;
2886
d4845d57 2887 case OPTION_DSP:
88da98f3 2888 preset_target_arch = arch_sh1_up & ~arch_sh2e_up;
d4845d57
JR
2889 break;
2890
324bfcf3 2891 case OPTION_ISA:
bdfaef52
JR
2892 if (strcasecmp (arg, "sh4") == 0)
2893 preset_target_arch = arch_sh4;
88da98f3
MS
2894 else if (strcasecmp (arg, "sh4a") == 0)
2895 preset_target_arch = arch_sh4a;
2896 else if (strcasecmp (arg, "dsp") == 0)
2897 preset_target_arch = arch_sh1_up & ~arch_sh2e_up;
2898 else if (strcasecmp (arg, "fp") == 0)
2899 preset_target_arch = arch_sh2e_up;
bdfaef52
JR
2900 else if (strcasecmp (arg, "any") == 0)
2901 preset_target_arch = arch_sh1_up;
2902#ifdef HAVE_SH64
2903 else if (strcasecmp (arg, "shmedia") == 0)
324bfcf3
AO
2904 {
2905 if (sh64_isa_mode == sh64_isa_shcompact)
2906 as_bad (_("Invalid combination: --isa=SHcompact with --isa=SHmedia"));
2907 sh64_isa_mode = sh64_isa_shmedia;
2908 }
2909 else if (strcasecmp (arg, "shcompact") == 0)
2910 {
2911 if (sh64_isa_mode == sh64_isa_shmedia)
2912 as_bad (_("Invalid combination: --isa=SHmedia with --isa=SHcompact"));
2913 if (sh64_abi == sh64_abi_64)
2914 as_bad (_("Invalid combination: --abi=64 with --isa=SHcompact"));
2915 sh64_isa_mode = sh64_isa_shcompact;
2916 }
bdfaef52 2917#endif /* HAVE_SH64 */
324bfcf3
AO
2918 else
2919 as_bad ("Invalid argument to --isa option: %s", arg);
2920 break;
2921
bdfaef52 2922#ifdef HAVE_SH64
324bfcf3
AO
2923 case OPTION_ABI:
2924 if (strcmp (arg, "32") == 0)
2925 {
2926 if (sh64_abi == sh64_abi_64)
2927 as_bad (_("Invalid combination: --abi=32 with --abi=64"));
2928 sh64_abi = sh64_abi_32;
2929 }
2930 else if (strcmp (arg, "64") == 0)
2931 {
2932 if (sh64_abi == sh64_abi_32)
2933 as_bad (_("Invalid combination: --abi=64 with --abi=32"));
2934 if (sh64_isa_mode == sh64_isa_shcompact)
2935 as_bad (_("Invalid combination: --isa=SHcompact with --abi=64"));
2936 sh64_abi = sh64_abi_64;
2937 }
2938 else
2939 as_bad ("Invalid argument to --abi option: %s", arg);
2940 break;
2941
2942 case OPTION_NO_MIX:
b34976b6 2943 sh64_mix = FALSE;
324bfcf3
AO
2944 break;
2945
2946 case OPTION_SHCOMPACT_CONST_CRANGE:
b34976b6 2947 sh64_shcompact_const_crange = TRUE;
324bfcf3
AO
2948 break;
2949
2950 case OPTION_NO_EXPAND:
b34976b6 2951 sh64_expand = FALSE;
324bfcf3
AO
2952 break;
2953
2954 case OPTION_PT32:
b34976b6 2955 sh64_pt32 = TRUE;
324bfcf3
AO
2956 break;
2957#endif /* HAVE_SH64 */
2958
252b5132
RH
2959 default:
2960 return 0;
2961 }
2962
2963 return 1;
2964}
2965
2966void
8edc77b9 2967md_show_usage (FILE *stream)
252b5132 2968{
c4aa876b 2969 fprintf (stream, _("\
252b5132
RH
2970SH options:\n\
2971-little generate little endian code\n\
05982cac 2972-big generate big endian code\n\
252b5132 2973-relax alter jump instructions for long displacements\n\
5b8274e3 2974-small align sections to 4 byte boundaries, not 16\n\
88da98f3 2975-dsp enable sh-dsp insns, and disable floating-point ISAs.\n"));
2acb89ed 2976#ifdef HAVE_SH64
88da98f3
MS
2977 fprintf (stream, _("\
2978-isa=[sh4\n\
2979 | sh4a\n\
2980 | dsp same as '-dsp'\n\
2acb89ed
NC
2981 | fp\n\
2982 | shmedia set as the default instruction set for SH64\n\
88da98f3
MS
2983 | SHmedia\n\
2984 | shcompact\n\
2acb89ed 2985 | SHcompact\n"));
324bfcf3 2986 fprintf (stream, _("\
324bfcf3
AO
2987-abi=[32|64] set size of expanded SHmedia operands and object\n\
2988 file type\n\
2989-shcompact-const-crange emit code-range descriptors for constants in\n\
2990 SHcompact code sections\n\
2991-no-mix disallow SHmedia code in the same section as\n\
2992 constants and SHcompact code\n\
2993-no-expand do not expand MOVI, PT, PTA or PTB instructions\n\
2994-expand-pt32 with -abi=64, expand PT, PTA and PTB instructions\n\
2acb89ed
NC
2995 to 32 bits only\n"));
2996#else
2997 fprintf (stream, _("\
2998-isa=[sh4\n\
2999 | sh4a\n\
3000 | dsp same as '-dsp'\n\
3001 | fp\n\
3002 | any]\n"));
324bfcf3 3003#endif /* HAVE_SH64 */
252b5132
RH
3004}
3005\f
252b5132
RH
3006/* This struct is used to pass arguments to sh_count_relocs through
3007 bfd_map_over_sections. */
3008
3009struct sh_count_relocs
3010{
3011 /* Symbol we are looking for. */
3012 symbolS *sym;
3013 /* Count of relocs found. */
3014 int count;
3015};
3016
3017/* Count the number of fixups in a section which refer to a particular
3018 symbol. When using BFD_ASSEMBLER, this is called via
3019 bfd_map_over_sections. */
3020
252b5132 3021static void
8edc77b9 3022sh_count_relocs (bfd *abfd ATTRIBUTE_UNUSED, segT sec, void *data)
252b5132
RH
3023{
3024 struct sh_count_relocs *info = (struct sh_count_relocs *) data;
3025 segment_info_type *seginfo;
3026 symbolS *sym;
3027 fixS *fix;
3028
3029 seginfo = seg_info (sec);
3030 if (seginfo == NULL)
3031 return;
3032
3033 sym = info->sym;
3034 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
3035 {
3036 if (fix->fx_addsy == sym)
3037 {
3038 ++info->count;
3039 fix->fx_tcbit = 1;
3040 }
3041 }
3042}
3043
3044/* Handle the count relocs for a particular section. When using
3045 BFD_ASSEMBLER, this is called via bfd_map_over_sections. */
3046
252b5132 3047static void
8edc77b9
KK
3048sh_frob_section (bfd *abfd ATTRIBUTE_UNUSED, segT sec,
3049 void *ignore ATTRIBUTE_UNUSED)
252b5132
RH
3050{
3051 segment_info_type *seginfo;
3052 fixS *fix;
3053
3054 seginfo = seg_info (sec);
3055 if (seginfo == NULL)
3056 return;
3057
3058 for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
3059 {
3060 symbolS *sym;
3061 bfd_vma val;
3062 fixS *fscan;
3063 struct sh_count_relocs info;
3064
3065 if (fix->fx_r_type != BFD_RELOC_SH_USES)
3066 continue;
3067
3068 /* The BFD_RELOC_SH_USES reloc should refer to a defined local
3069 symbol in the same section. */
3070 sym = fix->fx_addsy;
3071 if (sym == NULL
3072 || fix->fx_subsy != NULL
3073 || fix->fx_addnumber != 0
3074 || S_GET_SEGMENT (sym) != sec
3075#if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
3076 || S_GET_STORAGE_CLASS (sym) == C_EXT
3077#endif
3078 || S_IS_EXTERNAL (sym))
3079 {
3080 as_warn_where (fix->fx_file, fix->fx_line,
3081 _(".uses does not refer to a local symbol in the same section"));
3082 continue;
3083 }
3084
3085 /* Look through the fixups again, this time looking for one
3086 at the same location as sym. */
3087 val = S_GET_VALUE (sym);
3088 for (fscan = seginfo->fix_root;
3089 fscan != NULL;
3090 fscan = fscan->fx_next)
3091 if (val == fscan->fx_frag->fr_address + fscan->fx_where
3092 && fscan->fx_r_type != BFD_RELOC_SH_ALIGN
3093 && fscan->fx_r_type != BFD_RELOC_SH_CODE
3094 && fscan->fx_r_type != BFD_RELOC_SH_DATA
3095 && fscan->fx_r_type != BFD_RELOC_SH_LABEL)
3096 break;
3097 if (fscan == NULL)
3098 {
3099 as_warn_where (fix->fx_file, fix->fx_line,
3100 _("can't find fixup pointed to by .uses"));
3101 continue;
3102 }
3103
3104 if (fscan->fx_tcbit)
3105 {
3106 /* We've already done this one. */
3107 continue;
3108 }
3109
6b31947e
NC
3110 /* The variable fscan should also be a fixup to a local symbol
3111 in the same section. */
252b5132
RH
3112 sym = fscan->fx_addsy;
3113 if (sym == NULL
3114 || fscan->fx_subsy != NULL
3115 || fscan->fx_addnumber != 0
3116 || S_GET_SEGMENT (sym) != sec
3117#if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
3118 || S_GET_STORAGE_CLASS (sym) == C_EXT
3119#endif
3120 || S_IS_EXTERNAL (sym))
3121 {
3122 as_warn_where (fix->fx_file, fix->fx_line,
3123 _(".uses target does not refer to a local symbol in the same section"));
3124 continue;
3125 }
3126
3127 /* Now we look through all the fixups of all the sections,
3128 counting the number of times we find a reference to sym. */
3129 info.sym = sym;
3130 info.count = 0;
3131#ifdef BFD_ASSEMBLER
8edc77b9 3132 bfd_map_over_sections (stdoutput, sh_count_relocs, &info);
252b5132
RH
3133#else
3134 {
3135 int iscan;
3136
3137 for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
8edc77b9 3138 sh_count_relocs ((bfd *) NULL, iscan, &info);
252b5132
RH
3139 }
3140#endif
3141
3142 if (info.count < 1)
3143 abort ();
3144
3145 /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
3146 We have already adjusted the value of sym to include the
3147 fragment address, so we undo that adjustment here. */
3148 subseg_change (sec, 0);
7bcad3e5
NC
3149 fix_new (fscan->fx_frag,
3150 S_GET_VALUE (sym) - fscan->fx_frag->fr_address,
252b5132
RH
3151 4, &abs_symbol, info.count, 0, BFD_RELOC_SH_COUNT);
3152 }
3153}
3154
3155/* This function is called after the symbol table has been completed,
3156 but before the relocs or section contents have been written out.
3157 If we have seen any .uses pseudo-ops, they point to an instruction
3158 which loads a register with the address of a function. We look
3159 through the fixups to find where the function address is being
3160 loaded from. We then generate a COUNT reloc giving the number of
3161 times that function address is referred to. The linker uses this
3162 information when doing relaxing, to decide when it can eliminate
3163 the stored function address entirely. */
3164
3165void
8edc77b9 3166sh_frob_file (void)
252b5132 3167{
324bfcf3
AO
3168#ifdef HAVE_SH64
3169 shmedia_frob_file_before_adjust ();
3170#endif
3171
252b5132
RH
3172 if (! sh_relax)
3173 return;
3174
3175#ifdef BFD_ASSEMBLER
8edc77b9 3176 bfd_map_over_sections (stdoutput, sh_frob_section, NULL);
252b5132
RH
3177#else
3178 {
3179 int iseg;
3180
3181 for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
8edc77b9 3182 sh_frob_section ((bfd *) NULL, iseg, NULL);
252b5132
RH
3183 }
3184#endif
3185}
3186
3187/* Called after relaxing. Set the correct sizes of the fragments, and
94f592af 3188 create relocs so that md_apply_fix3 will fill in the correct values. */
252b5132
RH
3189
3190void
252b5132 3191#ifdef BFD_ASSEMBLER
8edc77b9 3192md_convert_frag (bfd *headers ATTRIBUTE_UNUSED, segT seg, fragS *fragP)
252b5132 3193#else
8edc77b9
KK
3194md_convert_frag (object_headers *headers ATTRIBUTE_UNUSED, segT seg,
3195 fragS *fragP)
252b5132 3196#endif
252b5132
RH
3197{
3198 int donerelax = 0;
3199
3200 switch (fragP->fr_subtype)
3201 {
3202 case C (COND_JUMP, COND8):
3203 case C (COND_JUMP_DELAY, COND8):
3204 subseg_change (seg, 0);
3205 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
3206 1, BFD_RELOC_SH_PCDISP8BY2);
3207 fragP->fr_fix += 2;
3208 fragP->fr_var = 0;
3209 break;
3210
3211 case C (UNCOND_JUMP, UNCOND12):
3212 subseg_change (seg, 0);
3213 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
3214 1, BFD_RELOC_SH_PCDISP12BY2);
3215 fragP->fr_fix += 2;
3216 fragP->fr_var = 0;
3217 break;
3218
3219 case C (UNCOND_JUMP, UNCOND32):
3220 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
3221 if (fragP->fr_symbol == NULL)
99b222b4
HPN
3222 as_bad_where (fragP->fr_file, fragP->fr_line,
3223 _("displacement overflows 12-bit field"));
252b5132 3224 else if (S_IS_DEFINED (fragP->fr_symbol))
99b222b4
HPN
3225 as_bad_where (fragP->fr_file, fragP->fr_line,
3226 _("displacement to defined symbol %s overflows 12-bit field"),
3227 S_GET_NAME (fragP->fr_symbol));
252b5132 3228 else
99b222b4
HPN
3229 as_bad_where (fragP->fr_file, fragP->fr_line,
3230 _("displacement to undefined symbol %s overflows 12-bit field"),
3231 S_GET_NAME (fragP->fr_symbol));
3232 /* Stabilize this frag, so we don't trip an assert. */
3233 fragP->fr_fix += fragP->fr_var;
3234 fragP->fr_var = 0;
252b5132
RH
3235 break;
3236
3237 case C (COND_JUMP, COND12):
3238 case C (COND_JUMP_DELAY, COND12):
6b31947e 3239 /* A bcond won't fit, so turn it into a b!cond; bra disp; nop. */
252b5132
RH
3240 /* I found that a relax failure for gcc.c-torture/execute/930628-1.c
3241 was due to gas incorrectly relaxing an out-of-range conditional
3242 branch with delay slot. It turned:
3243 bf.s L6 (slot mov.l r12,@(44,r0))
3244 into:
c4aa876b 3245
252b5132
RH
32462c: 8f 01 a0 8b bf.s 32 <_main+32> (slot bra L6)
324730: 00 09 nop
324832: 10 cb mov.l r12,@(44,r0)
3249 Therefore, branches with delay slots have to be handled
3250 differently from ones without delay slots. */
3251 {
3252 unsigned char *buffer =
3253 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
3254 int highbyte = target_big_endian ? 0 : 1;
3255 int lowbyte = target_big_endian ? 1 : 0;
3256 int delay = fragP->fr_subtype == C (COND_JUMP_DELAY, COND12);
3257
3258 /* Toggle the true/false bit of the bcond. */
3259 buffer[highbyte] ^= 0x2;
3260
d3ecfc59 3261 /* If this is a delayed branch, we may not put the bra in the
252b5132
RH
3262 slot. So we change it to a non-delayed branch, like that:
3263 b! cond slot_label; bra disp; slot_label: slot_insn
3264 ??? We should try if swapping the conditional branch and
3265 its delay-slot insn already makes the branch reach. */
3266
3267 /* Build a relocation to six / four bytes farther on. */
3268 subseg_change (seg, 0);
3269 fix_new (fragP, fragP->fr_fix, 2,
3270#ifdef BFD_ASSEMBLER
3271 section_symbol (seg),
3272#else
3273 seg_info (seg)->dot,
3274#endif
3275 fragP->fr_address + fragP->fr_fix + (delay ? 4 : 6),
3276 1, BFD_RELOC_SH_PCDISP8BY2);
3277
3278 /* Set up a jump instruction. */
3279 buffer[highbyte + 2] = 0xa0;
3280 buffer[lowbyte + 2] = 0;
3281 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
3282 fragP->fr_offset, 1, BFD_RELOC_SH_PCDISP12BY2);
3283
3284 if (delay)
3285 {
3286 buffer[highbyte] &= ~0x4; /* Removes delay slot from branch. */
3287 fragP->fr_fix += 4;
3288 }
3289 else
3290 {
3291 /* Fill in a NOP instruction. */
3292 buffer[highbyte + 4] = 0x0;
3293 buffer[lowbyte + 4] = 0x9;
3294
3295 fragP->fr_fix += 6;
3296 }
3297 fragP->fr_var = 0;
3298 donerelax = 1;
3299 }
3300 break;
3301
3302 case C (COND_JUMP, COND32):
3303 case C (COND_JUMP_DELAY, COND32):
3304 case C (COND_JUMP, UNDEF_WORD_DISP):
3305 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
3306 if (fragP->fr_symbol == NULL)
99b222b4
HPN
3307 as_bad_where (fragP->fr_file, fragP->fr_line,
3308 _("displacement overflows 8-bit field"));
252b5132 3309 else if (S_IS_DEFINED (fragP->fr_symbol))
99b222b4
HPN
3310 as_bad_where (fragP->fr_file, fragP->fr_line,
3311 _("displacement to defined symbol %s overflows 8-bit field"),
3312 S_GET_NAME (fragP->fr_symbol));
252b5132 3313 else
99b222b4
HPN
3314 as_bad_where (fragP->fr_file, fragP->fr_line,
3315 _("displacement to undefined symbol %s overflows 8-bit field "),
3316 S_GET_NAME (fragP->fr_symbol));
3317 /* Stabilize this frag, so we don't trip an assert. */
3318 fragP->fr_fix += fragP->fr_var;
3319 fragP->fr_var = 0;
252b5132
RH
3320 break;
3321
3322 default:
324bfcf3 3323#ifdef HAVE_SH64
b34976b6 3324 shmedia_md_convert_frag (headers, seg, fragP, TRUE);
324bfcf3 3325#else
252b5132 3326 abort ();
324bfcf3 3327#endif
252b5132
RH
3328 }
3329
3330 if (donerelax && !sh_relax)
3331 as_warn_where (fragP->fr_file, fragP->fr_line,
3332 _("overflow in branch to %s; converted into longer instruction sequence"),
3333 (fragP->fr_symbol != NULL
3334 ? S_GET_NAME (fragP->fr_symbol)
3335 : ""));
3336}
3337
3338valueT
8edc77b9 3339md_section_align (segT seg ATTRIBUTE_UNUSED, valueT size)
252b5132
RH
3340{
3341#ifdef BFD_ASSEMBLER
3342#ifdef OBJ_ELF
3343 return size;
3344#else /* ! OBJ_ELF */
3345 return ((size + (1 << bfd_get_section_alignment (stdoutput, seg)) - 1)
3346 & (-1 << bfd_get_section_alignment (stdoutput, seg)));
3347#endif /* ! OBJ_ELF */
3348#else /* ! BFD_ASSEMBLER */
3349 return ((size + (1 << section_alignment[(int) seg]) - 1)
3350 & (-1 << section_alignment[(int) seg]));
3351#endif /* ! BFD_ASSEMBLER */
3352}
3353
3354/* This static variable is set by s_uacons to tell sh_cons_align that
67c1ffbe 3355 the expression does not need to be aligned. */
252b5132
RH
3356
3357static int sh_no_align_cons = 0;
3358
3359/* This handles the unaligned space allocation pseudo-ops, such as
3360 .uaword. .uaword is just like .word, but the value does not need
3361 to be aligned. */
3362
3363static void
8edc77b9 3364s_uacons (int bytes)
252b5132
RH
3365{
3366 /* Tell sh_cons_align not to align this value. */
3367 sh_no_align_cons = 1;
3368 cons (bytes);
3369}
3370
3371/* If a .word, et. al., pseud-op is seen, warn if the value is not
3372 aligned correctly. Note that this can cause warnings to be issued
3373 when assembling initialized structured which were declared with the
3374 packed attribute. FIXME: Perhaps we should require an option to
3375 enable this warning? */
3376
3377void
8edc77b9 3378sh_cons_align (int nbytes)
252b5132
RH
3379{
3380 int nalign;
3381 char *p;
3382
3383 if (sh_no_align_cons)
3384 {
3385 /* This is an unaligned pseudo-op. */
3386 sh_no_align_cons = 0;
3387 return;
3388 }
3389
3390 nalign = 0;
3391 while ((nbytes & 1) == 0)
3392 {
3393 ++nalign;
3394 nbytes >>= 1;
3395 }
3396
3397 if (nalign == 0)
3398 return;
3399
3400 if (now_seg == absolute_section)
3401 {
3402 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
3403 as_warn (_("misaligned data"));
3404 return;
3405 }
3406
0a9ef439 3407 p = frag_var (rs_align_test, 1, 1, (relax_substateT) 0,
252b5132
RH
3408 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
3409
3410 record_alignment (now_seg, nalign);
3411}
3412
3413/* When relaxing, we need to output a reloc for any .align directive
3414 that requests alignment to a four byte boundary or larger. This is
3415 also where we check for misaligned data. */
3416
3417void
8edc77b9 3418sh_handle_align (fragS *frag)
252b5132 3419{
0a9ef439
RH
3420 int bytes = frag->fr_next->fr_address - frag->fr_address - frag->fr_fix;
3421
3422 if (frag->fr_type == rs_align_code)
3423 {
3424 static const unsigned char big_nop_pattern[] = { 0x00, 0x09 };
3425 static const unsigned char little_nop_pattern[] = { 0x09, 0x00 };
3426
3427 char *p = frag->fr_literal + frag->fr_fix;
3428
3429 if (bytes & 1)
3430 {
3431 *p++ = 0;
3432 bytes--;
3433 frag->fr_fix += 1;
3434 }
3435
3436 if (target_big_endian)
3437 {
3438 memcpy (p, big_nop_pattern, sizeof big_nop_pattern);
3439 frag->fr_var = sizeof big_nop_pattern;
3440 }
3441 else
3442 {
3443 memcpy (p, little_nop_pattern, sizeof little_nop_pattern);
3444 frag->fr_var = sizeof little_nop_pattern;
3445 }
3446 }
3447 else if (frag->fr_type == rs_align_test)
3448 {
3449 if (bytes != 0)
3450 as_warn_where (frag->fr_file, frag->fr_line, _("misaligned data"));
3451 }
3452
252b5132 3453 if (sh_relax
0a9ef439
RH
3454 && (frag->fr_type == rs_align
3455 || frag->fr_type == rs_align_code)
252b5132
RH
3456 && frag->fr_address + frag->fr_fix > 0
3457 && frag->fr_offset > 1
3458 && now_seg != bss_section)
3459 fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
3460 BFD_RELOC_SH_ALIGN);
252b5132
RH
3461}
3462
28602ebf
KK
3463/* See whether the relocation should be resolved locally. */
3464
b34976b6 3465static bfd_boolean
8edc77b9 3466sh_local_pcrel (fixS *fix)
28602ebf 3467{
b34976b6
AM
3468 return (! sh_relax
3469 && (fix->fx_r_type == BFD_RELOC_SH_PCDISP8BY2
3470 || fix->fx_r_type == BFD_RELOC_SH_PCDISP12BY2
3471 || fix->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY2
3472 || fix->fx_r_type == BFD_RELOC_SH_PCRELIMM8BY4
3473 || fix->fx_r_type == BFD_RELOC_8_PCREL
3474 || fix->fx_r_type == BFD_RELOC_SH_SWITCH16
3475 || fix->fx_r_type == BFD_RELOC_SH_SWITCH32));
28602ebf
KK
3476}
3477
252b5132
RH
3478/* See whether we need to force a relocation into the output file.
3479 This is used to force out switch and PC relative relocations when
3480 relaxing. */
3481
3482int
8edc77b9 3483sh_force_relocation (fixS *fix)
252b5132 3484{
8ba4dac0
DJ
3485 /* These relocations can't make it into a DSO, so no use forcing
3486 them for global symbols. */
28602ebf 3487 if (sh_local_pcrel (fix))
8ba4dac0
DJ
3488 return 0;
3489
9efb3b7b 3490 /* Make sure some relocations get emitted. */
ae6063d4 3491 if (fix->fx_r_type == BFD_RELOC_SH_LOOP_START
a161fe53 3492 || fix->fx_r_type == BFD_RELOC_SH_LOOP_END
9efb3b7b
KK
3493 || fix->fx_r_type == BFD_RELOC_SH_TLS_GD_32
3494 || fix->fx_r_type == BFD_RELOC_SH_TLS_LD_32
3495 || fix->fx_r_type == BFD_RELOC_SH_TLS_IE_32
3496 || fix->fx_r_type == BFD_RELOC_SH_TLS_LDO_32
3497 || fix->fx_r_type == BFD_RELOC_SH_TLS_LE_32
ae6063d4 3498 || generic_force_reloc (fix))
252b5132
RH
3499 return 1;
3500
3501 if (! sh_relax)
3502 return 0;
3503
3504 return (fix->fx_pcrel
3505 || SWITCH_TABLE (fix)
3506 || fix->fx_r_type == BFD_RELOC_SH_COUNT
3507 || fix->fx_r_type == BFD_RELOC_SH_ALIGN
3508 || fix->fx_r_type == BFD_RELOC_SH_CODE
3509 || fix->fx_r_type == BFD_RELOC_SH_DATA
324bfcf3
AO
3510#ifdef HAVE_SH64
3511 || fix->fx_r_type == BFD_RELOC_SH_SHMEDIA_CODE
3512#endif
252b5132
RH
3513 || fix->fx_r_type == BFD_RELOC_SH_LABEL);
3514}
3515
3516#ifdef OBJ_ELF
b34976b6 3517bfd_boolean
8edc77b9 3518sh_fix_adjustable (fixS *fixP)
252b5132 3519{
a161fe53
AM
3520 if (fixP->fx_r_type == BFD_RELOC_32_PLT_PCREL
3521 || fixP->fx_r_type == BFD_RELOC_32_GOT_PCREL
3522 || fixP->fx_r_type == BFD_RELOC_SH_GOTPC
a1cc9221
AO
3523 || fixP->fx_r_type == BFD_RELOC_RVA)
3524 return 0;
3525
252b5132
RH
3526 /* We need the symbol name for the VTABLE entries */
3527 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3528 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3529 return 0;
3530
3531 return 1;
3532}
d4845d57 3533
6b31947e 3534void
8edc77b9 3535sh_elf_final_processing (void)
d4845d57
JR
3536{
3537 int val;
3538
3539 /* Set file-specific flags to indicate if this code needs
88da98f3 3540 a processor with the sh-dsp / sh2e ISA to execute. */
324bfcf3
AO
3541#ifdef HAVE_SH64
3542 /* SH5 and above don't know about the valid_arch arch_sh* bits defined
3543 in sh-opc.h, so check SH64 mode before checking valid_arch. */
3544 if (sh64_isa_mode != sh64_isa_unspecified)
3545 val = EF_SH5;
3546 else
3547#endif /* HAVE_SH64 */
d4845d57
JR
3548 if (valid_arch & arch_sh1)
3549 val = EF_SH1;
3550 else if (valid_arch & arch_sh2)
3551 val = EF_SH2;
5177500f
NC
3552 else if (valid_arch & arch_sh2e)
3553 val = EF_SH2E;
d4845d57
JR
3554 else if (valid_arch & arch_sh_dsp)
3555 val = EF_SH_DSP;
3556 else if (valid_arch & arch_sh3)
3557 val = EF_SH3;
3558 else if (valid_arch & arch_sh3_dsp)
88da98f3 3559 val = EF_SH3_DSP;
d4845d57
JR
3560 else if (valid_arch & arch_sh3e)
3561 val = EF_SH3E;
88da98f3
MS
3562 else if (valid_arch & arch_sh4_nofpu)
3563 val = EF_SH4_NOFPU;
d4845d57
JR
3564 else if (valid_arch & arch_sh4)
3565 val = EF_SH4;
88da98f3
MS
3566 else if (valid_arch & arch_sh4a_nofpu)
3567 val = EF_SH4A_NOFPU;
3568 else if (valid_arch & arch_sh4a)
3569 val = EF_SH4A;
3570 else if (valid_arch & arch_sh4al_dsp)
3571 val = EF_SH4AL_DSP;
d4845d57
JR
3572 else
3573 abort ();
3574
3575 elf_elfheader (stdoutput)->e_flags &= ~EF_SH_MACH_MASK;
3576 elf_elfheader (stdoutput)->e_flags |= val;
3577}
252b5132
RH
3578#endif
3579
3580/* Apply a fixup to the object file. */
3581
252b5132 3582void
8edc77b9 3583md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132
RH
3584{
3585 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
3586 int lowbyte = target_big_endian ? 1 : 0;
3587 int highbyte = target_big_endian ? 0 : 1;
2ed5f585 3588 long val = (long) *valP;
252b5132
RH
3589 long max, min;
3590 int shift;
3591
3592#ifdef BFD_ASSEMBLER
a1cc9221
AO
3593 /* A difference between two symbols, the second of which is in the
3594 current section, is transformed in a PC-relative relocation to
3595 the other symbol. We have to adjust the relocation type here. */
3596 if (fixP->fx_pcrel)
3597 {
3598 switch (fixP->fx_r_type)
3599 {
3600 default:
3601 break;
3602
3603 case BFD_RELOC_32:
3604 fixP->fx_r_type = BFD_RELOC_32_PCREL;
3605 break;
3606
3607 /* Currently, we only support 32-bit PCREL relocations.
3608 We'd need a new reloc type to handle 16_PCREL, and
3609 8_PCREL is already taken for R_SH_SWITCH8, which
3610 apparently does something completely different than what
3611 we need. FIXME. */
3612 case BFD_RELOC_16:
3613 bfd_set_error (bfd_error_bad_value);
94f592af 3614 return;
81d4177b 3615
a1cc9221
AO
3616 case BFD_RELOC_8:
3617 bfd_set_error (bfd_error_bad_value);
94f592af 3618 return;
a1cc9221
AO
3619 }
3620 }
3621
6b31947e
NC
3622 /* The function adjust_reloc_syms won't convert a reloc against a weak
3623 symbol into a reloc against a section, but bfd_install_relocation
3624 will screw up if the symbol is defined, so we have to adjust val here
1308f14c
HPN
3625 to avoid the screw up later.
3626
3627 For ordinary relocs, this does not happen for ELF, since for ELF,
3628 bfd_install_relocation uses the "special function" field of the
3629 howto, and does not execute the code that needs to be undone, as long
3630 as the special function does not return bfd_reloc_continue.
3631 It can happen for GOT- and PLT-type relocs the way they are
3632 described in elf32-sh.c as they use bfd_elf_generic_reloc, but it
3633 doesn't matter here since those relocs don't use VAL; see below. */
3634 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3635 && fixP->fx_addsy != NULL
252b5132
RH
3636 && S_IS_WEAK (fixP->fx_addsy))
3637 val -= S_GET_VALUE (fixP->fx_addsy);
3638#endif
3639
bdfaef52
JR
3640#ifdef BFD_ASSEMBLER
3641 if (SWITCH_TABLE (fixP))
3642 val -= S_GET_VALUE (fixP->fx_subsy);
3643#else
252b5132
RH
3644 if (fixP->fx_r_type == 0)
3645 {
3646 if (fixP->fx_size == 2)
3647 fixP->fx_r_type = BFD_RELOC_16;
3648 else if (fixP->fx_size == 4)
3649 fixP->fx_r_type = BFD_RELOC_32;
3650 else if (fixP->fx_size == 1)
3651 fixP->fx_r_type = BFD_RELOC_8;
3652 else
3653 abort ();
3654 }
3655#endif
3656
3657 max = min = 0;
3658 shift = 0;
3659 switch (fixP->fx_r_type)
3660 {
3661 case BFD_RELOC_SH_IMM4:
3662 max = 0xf;
3663 *buf = (*buf & 0xf0) | (val & 0xf);
3664 break;
3665
3666 case BFD_RELOC_SH_IMM4BY2:
3667 max = 0xf;
3668 shift = 1;
3669 *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
3670 break;
3671
3672 case BFD_RELOC_SH_IMM4BY4:
3673 max = 0xf;
3674 shift = 2;
3675 *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
3676 break;
3677
3678 case BFD_RELOC_SH_IMM8BY2:
3679 max = 0xff;
3680 shift = 1;
3681 *buf = val >> 1;
3682 break;
3683
3684 case BFD_RELOC_SH_IMM8BY4:
3685 max = 0xff;
3686 shift = 2;
3687 *buf = val >> 2;
3688 break;
3689
3690 case BFD_RELOC_8:
3691 case BFD_RELOC_SH_IMM8:
3692 /* Sometimes the 8 bit value is sign extended (e.g., add) and
3693 sometimes it is not (e.g., and). We permit any 8 bit value.
3694 Note that adding further restrictions may invalidate
3695 reasonable looking assembly code, such as ``and -0x1,r0''. */
3696 max = 0xff;
c4aa876b 3697 min = -0xff;
252b5132
RH
3698 *buf++ = val;
3699 break;
3700
3701 case BFD_RELOC_SH_PCRELIMM8BY4:
3702 /* The lower two bits of the PC are cleared before the
3703 displacement is added in. We can assume that the destination
67c1ffbe 3704 is on a 4 byte boundary. If this instruction is also on a 4
252b5132
RH
3705 byte boundary, then we want
3706 (target - here) / 4
3707 and target - here is a multiple of 4.
3708 Otherwise, we are on a 2 byte boundary, and we want
3709 (target - (here - 2)) / 4
3710 and target - here is not a multiple of 4. Computing
3711 (target - (here - 2)) / 4 == (target - here + 2) / 4
3712 works for both cases, since in the first case the addition of
3713 2 will be removed by the division. target - here is in the
3714 variable val. */
3715 val = (val + 2) / 4;
3716 if (val & ~0xff)
3717 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3718 buf[lowbyte] = val;
3719 break;
3720
3721 case BFD_RELOC_SH_PCRELIMM8BY2:
3722 val /= 2;
3723 if (val & ~0xff)
3724 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3725 buf[lowbyte] = val;
3726 break;
3727
3728 case BFD_RELOC_SH_PCDISP8BY2:
3729 val /= 2;
3730 if (val < -0x80 || val > 0x7f)
3731 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3732 buf[lowbyte] = val;
3733 break;
3734
3735 case BFD_RELOC_SH_PCDISP12BY2:
3736 val /= 2;
8637c045 3737 if (val < -0x800 || val > 0x7ff)
252b5132
RH
3738 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
3739 buf[lowbyte] = val & 0xff;
3740 buf[highbyte] |= (val >> 8) & 0xf;
3741 break;
3742
3743 case BFD_RELOC_32:
a1cc9221 3744 case BFD_RELOC_32_PCREL:
1db77c8e 3745 md_number_to_chars (buf, val, 4);
252b5132
RH
3746 break;
3747
3748 case BFD_RELOC_16:
1db77c8e 3749 md_number_to_chars (buf, val, 2);
252b5132
RH
3750 break;
3751
3752 case BFD_RELOC_SH_USES:
3753 /* Pass the value into sh_coff_reloc_mangle. */
3754 fixP->fx_addnumber = val;
3755 break;
3756
3757 case BFD_RELOC_SH_COUNT:
3758 case BFD_RELOC_SH_ALIGN:
3759 case BFD_RELOC_SH_CODE:
3760 case BFD_RELOC_SH_DATA:
3761 case BFD_RELOC_SH_LABEL:
3762 /* Nothing to do here. */
3763 break;
3764
015551fc
JR
3765 case BFD_RELOC_SH_LOOP_START:
3766 case BFD_RELOC_SH_LOOP_END:
3767
252b5132
RH
3768 case BFD_RELOC_VTABLE_INHERIT:
3769 case BFD_RELOC_VTABLE_ENTRY:
3770 fixP->fx_done = 0;
3771 return;
3772
a1cc9221
AO
3773#ifdef OBJ_ELF
3774 case BFD_RELOC_32_PLT_PCREL:
3775 /* Make the jump instruction point to the address of the operand. At
81d4177b 3776 runtime we merely add the offset to the actual PLT entry. */
94f592af 3777 * valP = 0xfffffffc;
0174e383 3778 val = fixP->fx_offset;
ac3f04d7
AO
3779 if (fixP->fx_subsy)
3780 val -= S_GET_VALUE (fixP->fx_subsy);
a161fe53 3781 fixP->fx_addnumber = val;
538cd60f 3782 md_number_to_chars (buf, val, 4);
a1cc9221
AO
3783 break;
3784
3785 case BFD_RELOC_SH_GOTPC:
3786 /* This is tough to explain. We end up with this one if we have
3787 operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".
3788 The goal here is to obtain the absolute address of the GOT,
3789 and it is strongly preferable from a performance point of
3790 view to avoid using a runtime relocation for this. There are
3791 cases where you have something like:
81d4177b 3792
a1cc9221 3793 .long _GLOBAL_OFFSET_TABLE_+[.-.L66]
81d4177b 3794
a1cc9221
AO
3795 and here no correction would be required. Internally in the
3796 assembler we treat operands of this form as not being pcrel
3797 since the '.' is explicitly mentioned, and I wonder whether
3798 it would simplify matters to do it this way. Who knows. In
3799 earlier versions of the PIC patches, the pcrel_adjust field
3800 was used to store the correction, but since the expression is
3801 not pcrel, I felt it would be confusing to do it this way. */
94f592af 3802 * valP -= 1;
a1cc9221
AO
3803 md_number_to_chars (buf, val, 4);
3804 break;
3805
9efb3b7b
KK
3806 case BFD_RELOC_SH_TLS_GD_32:
3807 case BFD_RELOC_SH_TLS_LD_32:
3808 case BFD_RELOC_SH_TLS_IE_32:
2bba4140
KK
3809 S_SET_THREAD_LOCAL (fixP->fx_addsy);
3810 /* Fallthrough */
3811 case BFD_RELOC_32_GOT_PCREL:
3812 case BFD_RELOC_SH_GOTPLT32:
94f592af 3813 * valP = 0; /* Fully resolved at runtime. No addend. */
a1cc9221
AO
3814 md_number_to_chars (buf, 0, 4);
3815 break;
3816
9efb3b7b
KK
3817 case BFD_RELOC_SH_TLS_LDO_32:
3818 case BFD_RELOC_SH_TLS_LE_32:
2bba4140
KK
3819 S_SET_THREAD_LOCAL (fixP->fx_addsy);
3820 /* Fallthrough */
3821 case BFD_RELOC_32_GOTOFF:
538cd60f 3822 md_number_to_chars (buf, val, 4);
a1cc9221
AO
3823 break;
3824#endif
3825
252b5132 3826 default:
324bfcf3
AO
3827#ifdef HAVE_SH64
3828 shmedia_md_apply_fix3 (fixP, valP);
3829 return;
3830#else
252b5132 3831 abort ();
324bfcf3 3832#endif
252b5132
RH
3833 }
3834
3835 if (shift != 0)
3836 {
3837 if ((val & ((1 << shift) - 1)) != 0)
3838 as_bad_where (fixP->fx_file, fixP->fx_line, _("misaligned offset"));
3839 if (val >= 0)
3840 val >>= shift;
3841 else
3842 val = ((val >> shift)
3843 | ((long) -1 & ~ ((long) -1 >> shift)));
3844 }
3845 if (max != 0 && (val < min || val > max))
3846 as_bad_where (fixP->fx_file, fixP->fx_line, _("offset out of range"));
3847
94f592af
NC
3848 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
3849 fixP->fx_done = 1;
252b5132
RH
3850}
3851
3852/* Called just before address relaxation. Return the length
3853 by which a fragment must grow to reach it's destination. */
3854
3855int
8edc77b9 3856md_estimate_size_before_relax (fragS *fragP, segT segment_type)
252b5132 3857{
e66457fb
AM
3858 int what;
3859
252b5132
RH
3860 switch (fragP->fr_subtype)
3861 {
93c2a809 3862 default:
324bfcf3
AO
3863#ifdef HAVE_SH64
3864 return shmedia_md_estimate_size_before_relax (fragP, segment_type);
3865#else
93c2a809 3866 abort ();
324bfcf3
AO
3867#endif
3868
93c2a809 3869
252b5132 3870 case C (UNCOND_JUMP, UNDEF_DISP):
6b31947e 3871 /* Used to be a branch to somewhere which was unknown. */
252b5132
RH
3872 if (!fragP->fr_symbol)
3873 {
3874 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
252b5132
RH
3875 }
3876 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3877 {
3878 fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
252b5132
RH
3879 }
3880 else
3881 {
3882 fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
252b5132
RH
3883 }
3884 break;
3885
252b5132
RH
3886 case C (COND_JUMP, UNDEF_DISP):
3887 case C (COND_JUMP_DELAY, UNDEF_DISP):
e66457fb 3888 what = GET_WHAT (fragP->fr_subtype);
6b31947e 3889 /* Used to be a branch to somewhere which was unknown. */
252b5132
RH
3890 if (fragP->fr_symbol
3891 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
3892 {
252b5132 3893 /* Got a symbol and it's defined in this segment, become byte
6b31947e 3894 sized - maybe it will fix up. */
252b5132 3895 fragP->fr_subtype = C (what, COND8);
252b5132
RH
3896 }
3897 else if (fragP->fr_symbol)
3898 {
6b31947e 3899 /* Its got a segment, but its not ours, so it will always be long. */
252b5132 3900 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
252b5132
RH
3901 }
3902 else
3903 {
6b31947e 3904 /* We know the abs value. */
252b5132 3905 fragP->fr_subtype = C (what, COND8);
252b5132 3906 }
93c2a809 3907 break;
252b5132 3908
93c2a809 3909 case C (UNCOND_JUMP, UNCOND12):
e66457fb 3910 case C (UNCOND_JUMP, UNCOND32):
93c2a809
AM
3911 case C (UNCOND_JUMP, UNDEF_WORD_DISP):
3912 case C (COND_JUMP, COND8):
e66457fb
AM
3913 case C (COND_JUMP, COND12):
3914 case C (COND_JUMP, COND32):
93c2a809
AM
3915 case C (COND_JUMP, UNDEF_WORD_DISP):
3916 case C (COND_JUMP_DELAY, COND8):
e66457fb
AM
3917 case C (COND_JUMP_DELAY, COND12):
3918 case C (COND_JUMP_DELAY, COND32):
93c2a809
AM
3919 case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
3920 /* When relaxing a section for the second time, we don't need to
e66457fb 3921 do anything besides return the current size. */
252b5132
RH
3922 break;
3923 }
e66457fb
AM
3924
3925 fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
252b5132
RH
3926 return fragP->fr_var;
3927}
3928
6b31947e 3929/* Put number into target byte order. */
252b5132
RH
3930
3931void
8edc77b9 3932md_number_to_chars (char *ptr, valueT use, int nbytes)
252b5132 3933{
324bfcf3
AO
3934#ifdef HAVE_SH64
3935 /* We might need to set the contents type to data. */
3936 sh64_flag_output ();
3937#endif
3938
252b5132
RH
3939 if (! target_big_endian)
3940 number_to_chars_littleendian (ptr, use, nbytes);
3941 else
3942 number_to_chars_bigendian (ptr, use, nbytes);
3943}
3944
cce5a618
NC
3945/* This version is used in obj-coff.c when not using BFD_ASSEMBLER.
3946 eg for the sh-hms target. */
3947
3948long
8edc77b9 3949md_pcrel_from (fixS *fixP)
cce5a618
NC
3950{
3951 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
3952}
3953
252b5132 3954long
8edc77b9 3955md_pcrel_from_section (fixS *fixP, segT sec)
252b5132 3956{
28602ebf
KK
3957 if (! sh_local_pcrel (fixP)
3958 && fixP->fx_addsy != (symbolS *) NULL
ae6063d4 3959 && (generic_force_reloc (fixP)
ef17112f
HPN
3960 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
3961 {
3962 /* The symbol is undefined (or is defined but not in this section,
3963 or we're not sure about it being the final definition). Let the
3964 linker figure it out. We need to adjust the subtraction of a
3965 symbol to the position of the relocated data, though. */
3966 return fixP->fx_subsy ? fixP->fx_where + fixP->fx_frag->fr_address : 0;
3967 }
3968
cce5a618 3969 return md_pcrel_from (fixP);
252b5132
RH
3970}
3971
3972#ifdef OBJ_COFF
3973
3974int
8edc77b9 3975tc_coff_sizemachdep (fragS *frag)
252b5132
RH
3976{
3977 return md_relax_table[frag->fr_subtype].rlx_length;
3978}
3979
3980#endif /* OBJ_COFF */
3981
252b5132
RH
3982#ifndef BFD_ASSEMBLER
3983#ifdef OBJ_COFF
3984
3985/* Map BFD relocs to SH COFF relocs. */
3986
3987struct reloc_map
3988{
3989 bfd_reloc_code_real_type bfd_reloc;
3990 int sh_reloc;
3991};
3992
3993static const struct reloc_map coff_reloc_map[] =
3994{
3995 { BFD_RELOC_32, R_SH_IMM32 },
3996 { BFD_RELOC_16, R_SH_IMM16 },
3997 { BFD_RELOC_8, R_SH_IMM8 },
3998 { BFD_RELOC_SH_PCDISP8BY2, R_SH_PCDISP8BY2 },
3999 { BFD_RELOC_SH_PCDISP12BY2, R_SH_PCDISP },
4000 { BFD_RELOC_SH_IMM4, R_SH_IMM4 },
4001 { BFD_RELOC_SH_IMM4BY2, R_SH_IMM4BY2 },
4002 { BFD_RELOC_SH_IMM4BY4, R_SH_IMM4BY4 },
4003 { BFD_RELOC_SH_IMM8, R_SH_IMM8 },
4004 { BFD_RELOC_SH_IMM8BY2, R_SH_IMM8BY2 },
4005 { BFD_RELOC_SH_IMM8BY4, R_SH_IMM8BY4 },
4006 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_PCRELIMM8BY2 },
4007 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_PCRELIMM8BY4 },
4008 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
4009 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
4010 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
4011 { BFD_RELOC_SH_USES, R_SH_USES },
4012 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
4013 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
4014 { BFD_RELOC_SH_CODE, R_SH_CODE },
4015 { BFD_RELOC_SH_DATA, R_SH_DATA },
4016 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
4017 { BFD_RELOC_UNUSED, 0 }
4018};
4019
4020/* Adjust a reloc for the SH. This is similar to the generic code,
4021 but does some minor tweaking. */
4022
4023void
8edc77b9
KK
4024sh_coff_reloc_mangle (segment_info_type *seg, fixS *fix,
4025 struct internal_reloc *intr, unsigned int paddr)
252b5132
RH
4026{
4027 symbolS *symbol_ptr = fix->fx_addsy;
4028 symbolS *dot;
4029
4030 intr->r_vaddr = paddr + fix->fx_frag->fr_address + fix->fx_where;
4031
4032 if (! SWITCH_TABLE (fix))
4033 {
4034 const struct reloc_map *rm;
4035
4036 for (rm = coff_reloc_map; rm->bfd_reloc != BFD_RELOC_UNUSED; rm++)
4037 if (rm->bfd_reloc == (bfd_reloc_code_real_type) fix->fx_r_type)
4038 break;
4039 if (rm->bfd_reloc == BFD_RELOC_UNUSED)
4040 as_bad_where (fix->fx_file, fix->fx_line,
4041 _("Can not represent %s relocation in this object file format"),
4042 bfd_get_reloc_code_name (fix->fx_r_type));
4043 intr->r_type = rm->sh_reloc;
4044 intr->r_offset = 0;
4045 }
4046 else
4047 {
4048 know (sh_relax);
4049
4050 if (fix->fx_r_type == BFD_RELOC_16)
4051 intr->r_type = R_SH_SWITCH16;
4052 else if (fix->fx_r_type == BFD_RELOC_8)
4053 intr->r_type = R_SH_SWITCH8;
4054 else if (fix->fx_r_type == BFD_RELOC_32)
4055 intr->r_type = R_SH_SWITCH32;
4056 else
4057 abort ();
4058
4059 /* For a switch reloc, we set r_offset to the difference between
4060 the reloc address and the subtrahend. When the linker is
4061 doing relaxing, it can use the determine the starting and
4062 ending points of the switch difference expression. */
4063 intr->r_offset = intr->r_vaddr - S_GET_VALUE (fix->fx_subsy);
4064 }
4065
4066 /* PC relative relocs are always against the current section. */
4067 if (symbol_ptr == NULL)
4068 {
4069 switch (fix->fx_r_type)
4070 {
4071 case BFD_RELOC_SH_PCRELIMM8BY2:
4072 case BFD_RELOC_SH_PCRELIMM8BY4:
4073 case BFD_RELOC_SH_PCDISP8BY2:
4074 case BFD_RELOC_SH_PCDISP12BY2:
4075 case BFD_RELOC_SH_USES:
4076 symbol_ptr = seg->dot;
4077 break;
4078 default:
4079 break;
4080 }
4081 }
4082
4083 if (fix->fx_r_type == BFD_RELOC_SH_USES)
4084 {
4085 /* We can't store the offset in the object file, since this
4086 reloc does not take up any space, so we store it in r_offset.
94f592af 4087 The fx_addnumber field was set in md_apply_fix3. */
252b5132
RH
4088 intr->r_offset = fix->fx_addnumber;
4089 }
4090 else if (fix->fx_r_type == BFD_RELOC_SH_COUNT)
4091 {
4092 /* We can't store the count in the object file, since this reloc
4093 does not take up any space, so we store it in r_offset. The
4094 fx_offset field was set when the fixup was created in
4095 sh_coff_frob_file. */
4096 intr->r_offset = fix->fx_offset;
4097 /* This reloc is always absolute. */
4098 symbol_ptr = NULL;
4099 }
4100 else if (fix->fx_r_type == BFD_RELOC_SH_ALIGN)
4101 {
4102 /* Store the alignment in the r_offset field. */
4103 intr->r_offset = fix->fx_offset;
4104 /* This reloc is always absolute. */
4105 symbol_ptr = NULL;
4106 }
4107 else if (fix->fx_r_type == BFD_RELOC_SH_CODE
4108 || fix->fx_r_type == BFD_RELOC_SH_DATA
4109 || fix->fx_r_type == BFD_RELOC_SH_LABEL)
4110 {
4111 /* These relocs are always absolute. */
4112 symbol_ptr = NULL;
4113 }
4114
4115 /* Turn the segment of the symbol into an offset. */
4116 if (symbol_ptr != NULL)
4117 {
4118 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
4119 if (dot != NULL)
4120 intr->r_symndx = dot->sy_number;
4121 else
4122 intr->r_symndx = symbol_ptr->sy_number;
4123 }
4124 else
4125 intr->r_symndx = -1;
4126}
4127
4128#endif /* OBJ_COFF */
4129#endif /* ! BFD_ASSEMBLER */
4130
4131#ifdef BFD_ASSEMBLER
4132
4133/* Create a reloc. */
4134
4135arelent *
8edc77b9 4136tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
4137{
4138 arelent *rel;
4139 bfd_reloc_code_real_type r_type;
4140
4141 rel = (arelent *) xmalloc (sizeof (arelent));
49309057
ILT
4142 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
4143 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
4144 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
4145
4146 r_type = fixp->fx_r_type;
4147
4148 if (SWITCH_TABLE (fixp))
4149 {
bdfaef52
JR
4150 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_subsy);
4151 rel->addend = 0;
252b5132
RH
4152 if (r_type == BFD_RELOC_16)
4153 r_type = BFD_RELOC_SH_SWITCH16;
4154 else if (r_type == BFD_RELOC_8)
4155 r_type = BFD_RELOC_8_PCREL;
4156 else if (r_type == BFD_RELOC_32)
4157 r_type = BFD_RELOC_SH_SWITCH32;
4158 else
4159 abort ();
4160 }
4161 else if (r_type == BFD_RELOC_SH_USES)
4162 rel->addend = fixp->fx_addnumber;
4163 else if (r_type == BFD_RELOC_SH_COUNT)
4164 rel->addend = fixp->fx_offset;
4165 else if (r_type == BFD_RELOC_SH_ALIGN)
4166 rel->addend = fixp->fx_offset;
4167 else if (r_type == BFD_RELOC_VTABLE_INHERIT
4168 || r_type == BFD_RELOC_VTABLE_ENTRY)
4169 rel->addend = fixp->fx_offset;
015551fc
JR
4170 else if (r_type == BFD_RELOC_SH_LOOP_START
4171 || r_type == BFD_RELOC_SH_LOOP_END)
4172 rel->addend = fixp->fx_offset;
4173 else if (r_type == BFD_RELOC_SH_LABEL && fixp->fx_pcrel)
4174 {
4175 rel->addend = 0;
4176 rel->address = rel->addend = fixp->fx_offset;
4177 }
324bfcf3
AO
4178#ifdef HAVE_SH64
4179 else if (shmedia_init_reloc (rel, fixp))
4180 ;
4181#endif
252b5132
RH
4182 else if (fixp->fx_pcrel)
4183 rel->addend = fixp->fx_addnumber;
a1cc9221
AO
4184 else if (r_type == BFD_RELOC_32 || r_type == BFD_RELOC_32_GOTOFF)
4185 rel->addend = fixp->fx_addnumber;
252b5132
RH
4186 else
4187 rel->addend = 0;
4188
4189 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
c861c80c 4190#ifdef OBJ_ELF
bdfaef52
JR
4191 if (rel->howto->type == R_SH_IND12W)
4192 rel->addend += fixp->fx_offset - 4;
c861c80c 4193#endif
a161fe53 4194 if (rel->howto == NULL)
252b5132
RH
4195 {
4196 as_bad_where (fixp->fx_file, fixp->fx_line,
4197 _("Cannot represent relocation type %s"),
4198 bfd_get_reloc_code_name (r_type));
4199 /* Set howto to a garbage value so that we can keep going. */
4200 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
4201 assert (rel->howto != NULL);
4202 }
4203
4204 return rel;
4205}
4206
538cd60f
AO
4207#ifdef OBJ_ELF
4208inline static char *
8edc77b9 4209sh_end_of_match (char *cont, char *what)
538cd60f
AO
4210{
4211 int len = strlen (what);
4212
4213 if (strncasecmp (cont, what, strlen (what)) == 0
4214 && ! is_part_of_name (cont[len]))
4215 return cont + len;
4216
4217 return NULL;
5d6255fe 4218}
538cd60f
AO
4219
4220int
8edc77b9 4221sh_parse_name (char const *name, expressionS *exprP, char *nextcharP)
538cd60f
AO
4222{
4223 char *next = input_line_pointer;
4224 char *next_end;
4225 int reloc_type;
4226 segT segment;
4227
4228 exprP->X_op_symbol = NULL;
4229
4230 if (strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
4231 {
4232 if (! GOT_symbol)
4233 GOT_symbol = symbol_find_or_make (name);
4234
4235 exprP->X_add_symbol = GOT_symbol;
4236 no_suffix:
4237 /* If we have an absolute symbol or a reg, then we know its
4238 value now. */
4239 segment = S_GET_SEGMENT (exprP->X_add_symbol);
4240 if (segment == absolute_section)
4241 {
4242 exprP->X_op = O_constant;
4243 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
4244 exprP->X_add_symbol = NULL;
4245 }
4246 else if (segment == reg_section)
4247 {
4248 exprP->X_op = O_register;
4249 exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
4250 exprP->X_add_symbol = NULL;
4251 }
4252 else
4253 {
4254 exprP->X_op = O_symbol;
4255 exprP->X_add_number = 0;
4256 }
4257
4258 return 1;
4259 }
4260
4261 exprP->X_add_symbol = symbol_find_or_make (name);
5d6255fe 4262
538cd60f
AO
4263 if (*nextcharP != '@')
4264 goto no_suffix;
4265 else if ((next_end = sh_end_of_match (next + 1, "GOTOFF")))
4266 reloc_type = BFD_RELOC_32_GOTOFF;
324bfcf3
AO
4267 else if ((next_end = sh_end_of_match (next + 1, "GOTPLT")))
4268 reloc_type = BFD_RELOC_SH_GOTPLT32;
538cd60f
AO
4269 else if ((next_end = sh_end_of_match (next + 1, "GOT")))
4270 reloc_type = BFD_RELOC_32_GOT_PCREL;
4271 else if ((next_end = sh_end_of_match (next + 1, "PLT")))
4272 reloc_type = BFD_RELOC_32_PLT_PCREL;
9efb3b7b
KK
4273 else if ((next_end = sh_end_of_match (next + 1, "TLSGD")))
4274 reloc_type = BFD_RELOC_SH_TLS_GD_32;
4275 else if ((next_end = sh_end_of_match (next + 1, "TLSLDM")))
4276 reloc_type = BFD_RELOC_SH_TLS_LD_32;
4277 else if ((next_end = sh_end_of_match (next + 1, "GOTTPOFF")))
4278 reloc_type = BFD_RELOC_SH_TLS_IE_32;
4279 else if ((next_end = sh_end_of_match (next + 1, "TPOFF")))
4280 reloc_type = BFD_RELOC_SH_TLS_LE_32;
4281 else if ((next_end = sh_end_of_match (next + 1, "DTPOFF")))
4282 reloc_type = BFD_RELOC_SH_TLS_LDO_32;
538cd60f
AO
4283 else
4284 goto no_suffix;
4285
4286 *input_line_pointer = *nextcharP;
4287 input_line_pointer = next_end;
4288 *nextcharP = *input_line_pointer;
4289 *input_line_pointer = '\0';
4290
4291 exprP->X_op = O_PIC_reloc;
4292 exprP->X_add_number = 0;
4293 exprP->X_md = reloc_type;
4294
4295 return 1;
4296}
4297#endif
252b5132 4298#endif /* BFD_ASSEMBLER */
This page took 0.502732 seconds and 4 git commands to generate.