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