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