]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-sparc.c
gas: sparc: fix collision of registers and pseudo-ops.
[thirdparty/binutils-gdb.git] / gas / config / tc-sparc.c
CommitLineData
252b5132 1/* tc-sparc.c -- Assemble for the SPARC
6f2750fe 2 Copyright (C) 1989-2016 Free Software Foundation, Inc.
252b5132
RH
3 This file is part of GAS, the GNU Assembler.
4
5 GAS is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
ec2655a6 7 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
8 any later version.
9
10 GAS is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with GAS; see the file COPYING. If not, write
4b4da160
NC
17 to the Free Software Foundation, 51 Franklin Street - Fifth Floor,
18 Boston, MA 02110-1301, USA. */
252b5132 19
252b5132 20#include "as.h"
3882b010 21#include "safe-ctype.h"
252b5132
RH
22#include "subsegs.h"
23
24#include "opcode/sparc.h"
364b6d8b 25#include "dw2gencfi.h"
252b5132
RH
26
27#ifdef OBJ_ELF
28#include "elf/sparc.h"
732d96b6 29#include "dwarf2dbg.h"
252b5132
RH
30#endif
31
6c1b24e4
AO
32/* Some ancient Sun C compilers would not take such hex constants as
33 unsigned, and would end up sign-extending them to form an offsetT,
34 so use these constants instead. */
35#define U0xffffffff ((((unsigned long) 1 << 16) << 16) - 1)
36#define U0x80000000 ((((unsigned long) 1 << 16) << 15))
37
5a49b8ac
AM
38static int sparc_ip (char *, const struct sparc_opcode **);
39static int parse_keyword_arg (int (*) (const char *), char **, int *);
40static int parse_const_expr_arg (char **, int *);
41static int get_expression (char *);
252b5132
RH
42
43/* Default architecture. */
44/* ??? The default value should be V8, but sparclite support was added
45 by making it the default. GCC now passes -Asparclite, so maybe sometime in
46 the future we can set this to V8. */
47#ifndef DEFAULT_ARCH
48#define DEFAULT_ARCH "sparclite"
49#endif
e0471c16 50static const char *default_arch = DEFAULT_ARCH;
252b5132
RH
51
52/* Non-zero if the initial values of `max_architecture' and `sparc_arch_size'
53 have been set. */
54static int default_init_p;
55
56/* Current architecture. We don't bump up unless necessary. */
57static enum sparc_opcode_arch_val current_architecture = SPARC_OPCODE_ARCH_V6;
58
59/* The maximum architecture level we can bump up to.
60 In a 32 bit environment, don't allow bumping up to v9 by default.
61 The native assembler works this way. The user is required to pass
62 an explicit argument before we'll create v9 object files. However, if
63 we don't see any v9 insns, a v8plus object file is not created. */
64static enum sparc_opcode_arch_val max_architecture;
65
66/* Either 32 or 64, selects file format. */
67static int sparc_arch_size;
68/* Initial (default) value, recorded separately in case a user option
69 changes the value before md_show_usage is called. */
70static int default_arch_size;
71
72#ifdef OBJ_ELF
73/* The currently selected v9 memory model. Currently only used for
74 ELF. */
75static enum { MM_TSO, MM_PSO, MM_RMO } sparc_memory_model = MM_RMO;
9e8c70f9
DM
76
77#ifndef TE_SOLARIS
78/* Bitmask of instruction types seen so far, used to populate the
79 GNU attributes section with hwcap information. */
3d68f91c 80static bfd_uint64_t hwcap_seen;
9e8c70f9 81#endif
252b5132
RH
82#endif
83
3d68f91c 84static bfd_uint64_t hwcap_allowed;
4bafe00e 85
252b5132
RH
86static int architecture_requested;
87static int warn_on_bump;
88
89/* If warn_on_bump and the needed architecture is higher than this
90 architecture, issue a warning. */
91static enum sparc_opcode_arch_val warn_after_architecture;
92
6d8809aa
RH
93/* Non-zero if as should generate error if an undeclared g[23] register
94 has been used in -64. */
95static int no_undeclared_regs;
96
6faf3d66
JJ
97/* Non-zero if we should try to relax jumps and calls. */
98static int sparc_relax;
99
252b5132
RH
100/* Non-zero if we are generating PIC code. */
101int sparc_pic_code;
102
103/* Non-zero if we should give an error when misaligned data is seen. */
104static int enforce_aligned_data;
105
106extern int target_big_endian;
107
108static int target_little_endian_data;
109
6d8809aa
RH
110/* Symbols for global registers on v9. */
111static symbolS *globals[8];
112
364b6d8b
JJ
113/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
114int sparc_cie_data_alignment;
115
252b5132
RH
116/* V9 and 86x have big and little endian data, but instructions are always big
117 endian. The sparclet has bi-endian support but both data and insns have
118 the same endianness. Global `target_big_endian' is used for data.
119 The following macro is used for instructions. */
120#ifndef INSN_BIG_ENDIAN
121#define INSN_BIG_ENDIAN (target_big_endian \
122 || default_arch_type == sparc86x \
123 || SPARC_OPCODE_ARCH_V9_P (max_architecture))
124#endif
125
e0c6ed95 126/* Handle of the OPCODE hash table. */
252b5132
RH
127static struct hash_control *op_hash;
128
5a49b8ac
AM
129static void s_data1 (void);
130static void s_seg (int);
131static void s_proc (int);
132static void s_reserve (int);
133static void s_common (int);
134static void s_empty (int);
135static void s_uacons (int);
136static void s_ncons (int);
a7982600 137#ifdef OBJ_ELF
5a49b8ac 138static void s_register (int);
a7982600 139#endif
252b5132
RH
140
141const pseudo_typeS md_pseudo_table[] =
142{
e0c6ed95 143 {"align", s_align_bytes, 0}, /* Defaulting is invalid (0). */
252b5132
RH
144 {"common", s_common, 0},
145 {"empty", s_empty, 0},
146 {"global", s_globl, 0},
147 {"half", cons, 2},
cf9a1301 148 {"nword", s_ncons, 0},
252b5132
RH
149 {"optim", s_ignore, 0},
150 {"proc", s_proc, 0},
151 {"reserve", s_reserve, 0},
152 {"seg", s_seg, 0},
153 {"skip", s_space, 0},
154 {"word", cons, 4},
155 {"xword", cons, 8},
156 {"uahalf", s_uacons, 2},
157 {"uaword", s_uacons, 4},
158 {"uaxword", s_uacons, 8},
159#ifdef OBJ_ELF
e0c6ed95 160 /* These are specific to sparc/svr4. */
252b5132
RH
161 {"2byte", s_uacons, 2},
162 {"4byte", s_uacons, 4},
163 {"8byte", s_uacons, 8},
6d8809aa 164 {"register", s_register, 0},
252b5132
RH
165#endif
166 {NULL, 0, 0},
167};
168
252b5132 169/* This array holds the chars that always start a comment. If the
e0c6ed95
AM
170 pre-processor is disabled, these aren't very useful. */
171const char comment_chars[] = "!"; /* JF removed '|' from
172 comment_chars. */
252b5132
RH
173
174/* This array holds the chars that only start a comment at the beginning of
175 a line. If the line seems to have the form '# 123 filename'
e0c6ed95 176 .line and .file directives will appear in the pre-processed output. */
252b5132
RH
177/* Note that input_file.c hand checks for '#' at the beginning of the
178 first line of the input file. This is because the compiler outputs
e0c6ed95 179 #NO_APP at the beginning of its output. */
252b5132 180/* Also note that comments started like this one will always
e0c6ed95 181 work if '/' isn't otherwise defined. */
252b5132
RH
182const char line_comment_chars[] = "#";
183
63a0b638 184const char line_separator_chars[] = ";";
252b5132 185
e0c6ed95
AM
186/* Chars that can be used to separate mant from exp in floating point
187 nums. */
252b5132
RH
188const char EXP_CHARS[] = "eE";
189
e0c6ed95
AM
190/* Chars that mean this number is a floating point constant.
191 As in 0f12.456
192 or 0d1.2345e12 */
252b5132
RH
193const char FLT_CHARS[] = "rRsSfFdDxXpP";
194
195/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
196 changed in read.c. Ideally it shouldn't have to know about it at all,
197 but nothing is ideal around here. */
198
74b56d1f 199#define isoctal(c) ((unsigned) ((c) - '0') < 8)
252b5132
RH
200
201struct sparc_it
202 {
6d4af3c2 203 const char *error;
252b5132
RH
204 unsigned long opcode;
205 struct nlist *nlistp;
206 expressionS exp;
cf9a1301 207 expressionS exp2;
252b5132
RH
208 int pcrel;
209 bfd_reloc_code_real_type reloc;
210 };
211
212struct sparc_it the_insn, set_insn;
213
5a49b8ac 214static void output_insn (const struct sparc_opcode *, struct sparc_it *);
252b5132
RH
215\f
216/* Table of arguments to -A.
217 The sparc_opcode_arch table in sparc-opc.c is insufficient and incorrect
218 for this use. That table is for opcodes only. This table is for opcodes
219 and file formats. */
220
d6787ef9 221enum sparc_arch_types {v6, v7, v8, leon, sparclet, sparclite, sparc86x, v8plus,
19f7b010 222 v8plusa, v9, v9a, v9b, v9_64};
252b5132 223
3d68f91c
JM
224/* Hardware capability sets, used to keep sparc_arch_table easy to
225 read. */
226#define HWS_V8 HWCAP_MUL32 | HWCAP_DIV32 | HWCAP_FSMULD
227#define HWS_V9 HWS_V8 | HWCAP_POPC
228#define HWS_VA HWS_V9 | HWCAP_VIS
229#define HWS_VB HWS_VA | HWCAP_VIS2
230#define HWS_VC HWS_VB | HWCAP_ASI_BLK_INIT
231#define HWS_VD HWS_VC | HWCAP_FMAF | HWCAP_VIS3 | HWCAP_HPC
232#define HWS_VE HWS_VD \
233 | HWCAP_AES | HWCAP_DES | HWCAP_KASUMI | HWCAP_CAMELLIA \
234 | HWCAP_MD5 | HWCAP_SHA1 | HWCAP_SHA256 |HWCAP_SHA512 | HWCAP_MPMUL \
235 | HWCAP_MONT | HWCAP_CRC32C | HWCAP_CBCOND | HWCAP_PAUSE
236#define HWS_VV HWS_VE | HWCAP_FJFMAU | HWCAP_IMA
237#define HWS_VM HWS_VV
238
239#define HWS2_VM \
240 HWCAP2_VIS3B | HWCAP2_ADP | HWCAP2_SPARC5 | HWCAP2_MWAIT \
241 | HWCAP2_XMPMUL | HWCAP2_XMONT
242
252b5132 243static struct sparc_arch {
e0471c16
TS
244 const char *name;
245 const char *opcode_arch;
252b5132
RH
246 enum sparc_arch_types arch_type;
247 /* Default word size, as specified during configuration.
248 A value of zero means can't be used to specify default architecture. */
249 int default_arch_size;
250 /* Allowable arg to -A? */
251 int user_option_p;
4bafe00e 252 int hwcap_allowed;
3d68f91c 253 int hwcap2_allowed;
252b5132 254} sparc_arch_table[] = {
3d68f91c
JM
255 { "v6", "v6", v6, 0, 1, 0, 0 },
256 { "v7", "v7", v7, 0, 1, 0, 0 },
257 { "v8", "v8", v8, 32, 1, HWS_V8, 0 },
258 { "v8a", "v8", v8, 32, 1, HWS_V8, 0 },
259 { "sparc", "v9", v9, 0, 1, HWCAP_V8PLUS|HWS_V9, 0 },
260 { "sparcvis", "v9a", v9, 0, 1, HWS_VA, 0 },
261 { "sparcvis2", "v9b", v9, 0, 1, HWS_VB, 0 },
262 { "sparcfmaf", "v9b", v9, 0, 1, HWS_VB|HWCAP_FMAF, 0 },
263 { "sparcima", "v9b", v9, 0, 1, HWS_VB|HWCAP_FMAF|HWCAP_IMA, 0 },
264 { "sparcvis3", "v9b", v9, 0, 1, HWS_VB|HWCAP_FMAF|HWCAP_VIS3|HWCAP_HPC, 0 },
265 { "sparcvis3r", "v9b", v9, 0, 1, HWS_VB|HWCAP_FMAF|HWCAP_VIS3|HWCAP_HPC|HWCAP_FJFMAU, 0 },
266
267 { "sparc4", "v9b", v9, 0, 1, HWS_VV, 0 },
268 { "sparc5", "v9b", v9, 0, 1, HWS_VM, HWS2_VM },
269
270 { "leon", "leon", leon, 32, 1, HWS_V8, 0 },
271 { "sparclet", "sparclet", sparclet, 32, 1, HWS_V8, 0 },
272 { "sparclite", "sparclite", sparclite, 32, 1, HWS_V8, 0 },
273 { "sparc86x", "sparclite", sparc86x, 32, 1, HWS_V8, 0 },
274
275 { "v8plus", "v9", v9, 0, 1, HWCAP_V8PLUS|HWS_V9, 0 },
276 { "v8plusa", "v9a", v9, 0, 1, HWCAP_V8PLUS|HWS_VA, 0 },
277 { "v8plusb", "v9b", v9, 0, 1, HWCAP_V8PLUS|HWS_VB, 0 },
278 { "v8plusc", "v9b", v9, 0, 1, HWCAP_V8PLUS|HWS_VC, 0 },
279 { "v8plusd", "v9b", v9, 0, 1, HWCAP_V8PLUS|HWS_VD, 0 },
280 { "v8pluse", "v9b", v9, 0, 1, HWCAP_V8PLUS|HWS_VE, 0 },
281 { "v8plusv", "v9b", v9, 0, 1, HWCAP_V8PLUS|HWS_VV, 0 },
282 { "v8plusm", "v9b", v9, 0, 1, HWCAP_V8PLUS|HWS_VM, 0 },
3739860c 283
3d68f91c
JM
284 { "v9", "v9", v9, 0, 1, HWS_V9, 0 },
285 { "v9a", "v9a", v9, 0, 1, HWS_VA, 0 },
286 { "v9b", "v9b", v9, 0, 1, HWS_VB, 0 },
287 { "v9c", "v9b", v9, 0, 1, HWS_VC, 0 },
288 { "v9d", "v9b", v9, 0, 1, HWS_VD, 0 },
289 { "v9e", "v9b", v9, 0, 1, HWS_VE, 0 },
290 { "v9v", "v9b", v9, 0, 1, HWS_VV, 0 },
291 { "v9m", "v9b", v9, 0, 1, HWS_VM, HWS2_VM },
292
1110793a 293 /* This exists to allow configure.tgt to pass one
252b5132 294 value to specify both the default machine and default word size. */
3d68f91c
JM
295 { "v9-64", "v9", v9, 64, 0, HWS_V9, 0 },
296 { NULL, NULL, v8, 0, 0, 0, 0 }
252b5132
RH
297};
298
299/* Variant of default_arch */
300static enum sparc_arch_types default_arch_type;
301
302static struct sparc_arch *
e0471c16 303lookup_arch (const char *name)
252b5132
RH
304{
305 struct sparc_arch *sa;
306
307 for (sa = &sparc_arch_table[0]; sa->name != NULL; sa++)
308 if (strcmp (sa->name, name) == 0)
309 break;
310 if (sa->name == NULL)
311 return NULL;
312 return sa;
313}
314
315/* Initialize the default opcode arch and word size from the default
316 architecture name. */
317
318static void
5a49b8ac 319init_default_arch (void)
252b5132
RH
320{
321 struct sparc_arch *sa = lookup_arch (default_arch);
322
323 if (sa == NULL
324 || sa->default_arch_size == 0)
325 as_fatal (_("Invalid default architecture, broken assembler."));
326
327 max_architecture = sparc_opcode_lookup_arch (sa->opcode_arch);
328 if (max_architecture == SPARC_OPCODE_ARCH_BAD)
329 as_fatal (_("Bad opcode table, broken assembler."));
330 default_arch_size = sparc_arch_size = sa->default_arch_size;
331 default_init_p = 1;
332 default_arch_type = sa->arch_type;
333}
334
335/* Called by TARGET_FORMAT. */
336
337const char *
5a49b8ac 338sparc_target_format (void)
252b5132
RH
339{
340 /* We don't get a chance to initialize anything before we're called,
341 so handle that now. */
342 if (! default_init_p)
343 init_default_arch ();
344
345#ifdef OBJ_AOUT
346#ifdef TE_NetBSD
347 return "a.out-sparc-netbsd";
348#else
349#ifdef TE_SPARCAOUT
350 if (target_big_endian)
351 return "a.out-sunos-big";
352 else if (default_arch_type == sparc86x && target_little_endian_data)
353 return "a.out-sunos-big";
ab3e48dc
KH
354 else
355 return "a.out-sparc-little";
252b5132
RH
356#else
357 return "a.out-sunos-big";
358#endif
359#endif
360#endif
361
362#ifdef OBJ_BOUT
363 return "b.out.big";
364#endif
365
366#ifdef OBJ_COFF
367#ifdef TE_LYNX
368 return "coff-sparc-lynx";
369#else
370 return "coff-sparc";
371#endif
372#endif
373
910600e9
RS
374#ifdef TE_VXWORKS
375 return "elf32-sparc-vxworks";
376#endif
377
252b5132 378#ifdef OBJ_ELF
71a75f6f 379 return sparc_arch_size == 64 ? ELF64_TARGET_FORMAT : ELF_TARGET_FORMAT;
252b5132
RH
380#endif
381
382 abort ();
383}
384\f
e0c6ed95 385/* md_parse_option
252b5132
RH
386 * Invocation line includes a switch not recognized by the base assembler.
387 * See if it's a processor-specific option. These are:
388 *
389 * -bump
390 * Warn on architecture bumps. See also -A.
391 *
d6787ef9 392 * -Av6, -Av7, -Av8, -Aleon, -Asparclite, -Asparclet
252b5132 393 * Standard 32 bit architectures.
19f7b010 394 * -Av9, -Av9a, -Av9b
252b5132
RH
395 * Sparc64 in either a 32 or 64 bit world (-32/-64 says which).
396 * This used to only mean 64 bits, but properly specifying it
397 * complicated gcc's ASM_SPECs, so now opcode selection is
398 * specified orthogonally to word size (except when specifying
399 * the default, but that is an internal implementation detail).
19f7b010
JJ
400 * -Av8plus, -Av8plusa, -Av8plusb
401 * Same as -Av9{,a,b}.
402 * -xarch=v8plus, -xarch=v8plusa, -xarch=v8plusb
403 * Same as -Av8plus{,a,b} -32, for compatibility with Sun's
404 * assembler.
405 * -xarch=v9, -xarch=v9a, -xarch=v9b
406 * Same as -Av9{,a,b} -64, for compatibility with Sun's
c2158c24 407 * assembler.
252b5132
RH
408 *
409 * Select the architecture and possibly the file format.
410 * Instructions or features not supported by the selected
411 * architecture cause fatal errors.
412 *
413 * The default is to start at v6, and bump the architecture up
414 * whenever an instruction is seen at a higher level. In 32 bit
415 * environments, v9 is not bumped up to, the user must pass
19f7b010 416 * -Av8plus{,a,b}.
252b5132
RH
417 *
418 * If -bump is specified, a warning is printing when bumping to
419 * higher levels.
420 *
421 * If an architecture is specified, all instructions must match
422 * that architecture. Any higher level instructions are flagged
423 * as errors. Note that in the 32 bit environment specifying
424 * -Av8plus does not automatically create a v8plus object file, a
425 * v9 insn must be seen.
426 *
427 * If both an architecture and -bump are specified, the
428 * architecture starts at the specified level, but bumps are
429 * warnings. Note that we can't set `current_architecture' to
430 * the requested level in this case: in the 32 bit environment,
431 * we still must avoid creating v8plus object files unless v9
432 * insns are seen.
433 *
434 * Note:
435 * Bumping between incompatible architectures is always an
436 * error. For example, from sparclite to v9.
437 */
438
439#ifdef OBJ_ELF
5a38dc70 440const char *md_shortopts = "A:K:VQ:sq";
252b5132
RH
441#else
442#ifdef OBJ_AOUT
5a38dc70 443const char *md_shortopts = "A:k";
252b5132 444#else
5a38dc70 445const char *md_shortopts = "A:";
252b5132
RH
446#endif
447#endif
448struct option md_longopts[] = {
449#define OPTION_BUMP (OPTION_MD_BASE)
450 {"bump", no_argument, NULL, OPTION_BUMP},
451#define OPTION_SPARC (OPTION_MD_BASE + 1)
452 {"sparc", no_argument, NULL, OPTION_SPARC},
453#define OPTION_XARCH (OPTION_MD_BASE + 2)
454 {"xarch", required_argument, NULL, OPTION_XARCH},
455#ifdef OBJ_ELF
456#define OPTION_32 (OPTION_MD_BASE + 3)
457 {"32", no_argument, NULL, OPTION_32},
458#define OPTION_64 (OPTION_MD_BASE + 4)
459 {"64", no_argument, NULL, OPTION_64},
460#define OPTION_TSO (OPTION_MD_BASE + 5)
461 {"TSO", no_argument, NULL, OPTION_TSO},
462#define OPTION_PSO (OPTION_MD_BASE + 6)
463 {"PSO", no_argument, NULL, OPTION_PSO},
464#define OPTION_RMO (OPTION_MD_BASE + 7)
465 {"RMO", no_argument, NULL, OPTION_RMO},
466#endif
467#ifdef SPARC_BIENDIAN
468#define OPTION_LITTLE_ENDIAN (OPTION_MD_BASE + 8)
469 {"EL", no_argument, NULL, OPTION_LITTLE_ENDIAN},
470#define OPTION_BIG_ENDIAN (OPTION_MD_BASE + 9)
471 {"EB", no_argument, NULL, OPTION_BIG_ENDIAN},
472#endif
473#define OPTION_ENFORCE_ALIGNED_DATA (OPTION_MD_BASE + 10)
474 {"enforce-aligned-data", no_argument, NULL, OPTION_ENFORCE_ALIGNED_DATA},
475#define OPTION_LITTLE_ENDIAN_DATA (OPTION_MD_BASE + 11)
476 {"little-endian-data", no_argument, NULL, OPTION_LITTLE_ENDIAN_DATA},
6d8809aa
RH
477#ifdef OBJ_ELF
478#define OPTION_NO_UNDECLARED_REGS (OPTION_MD_BASE + 12)
479 {"no-undeclared-regs", no_argument, NULL, OPTION_NO_UNDECLARED_REGS},
a25fe906
ILT
480#define OPTION_UNDECLARED_REGS (OPTION_MD_BASE + 13)
481 {"undeclared-regs", no_argument, NULL, OPTION_UNDECLARED_REGS},
6d8809aa 482#endif
6faf3d66
JJ
483#define OPTION_RELAX (OPTION_MD_BASE + 14)
484 {"relax", no_argument, NULL, OPTION_RELAX},
485#define OPTION_NO_RELAX (OPTION_MD_BASE + 15)
486 {"no-relax", no_argument, NULL, OPTION_NO_RELAX},
252b5132
RH
487 {NULL, no_argument, NULL, 0}
488};
e0c6ed95
AM
489
490size_t md_longopts_size = sizeof (md_longopts);
252b5132
RH
491
492int
17b9d67d 493md_parse_option (int c, const char *arg)
252b5132
RH
494{
495 /* We don't get a chance to initialize anything before we're called,
496 so handle that now. */
497 if (! default_init_p)
498 init_default_arch ();
499
500 switch (c)
501 {
502 case OPTION_BUMP:
503 warn_on_bump = 1;
504 warn_after_architecture = SPARC_OPCODE_ARCH_V6;
505 break;
506
507 case OPTION_XARCH:
c2158c24 508#ifdef OBJ_ELF
668b27ea 509 if (!strncmp (arg, "v9", 2))
c2158c24 510 md_parse_option (OPTION_64, NULL);
668b27ea
DM
511 else
512 {
513 if (!strncmp (arg, "v8", 2)
514 || !strncmp (arg, "v7", 2)
515 || !strncmp (arg, "v6", 2)
516 || !strcmp (arg, "sparclet")
517 || !strcmp (arg, "sparclite")
518 || !strcmp (arg, "sparc86x"))
519 md_parse_option (OPTION_32, NULL);
520 }
c2158c24 521#endif
e0c6ed95 522 /* Fall through. */
252b5132
RH
523
524 case 'A':
525 {
526 struct sparc_arch *sa;
527 enum sparc_opcode_arch_val opcode_arch;
528
529 sa = lookup_arch (arg);
530 if (sa == NULL
531 || ! sa->user_option_p)
532 {
c2158c24
JJ
533 if (c == OPTION_XARCH)
534 as_bad (_("invalid architecture -xarch=%s"), arg);
535 else
536 as_bad (_("invalid architecture -A%s"), arg);
252b5132
RH
537 return 0;
538 }
539
540 opcode_arch = sparc_opcode_lookup_arch (sa->opcode_arch);
541 if (opcode_arch == SPARC_OPCODE_ARCH_BAD)
542 as_fatal (_("Bad opcode table, broken assembler."));
543
4bafe00e
DM
544 if (!architecture_requested
545 || opcode_arch > max_architecture)
546 max_architecture = opcode_arch;
3d68f91c
JM
547 hwcap_allowed
548 |= (((bfd_uint64_t) sa->hwcap2_allowed) << 32) | sa->hwcap_allowed;
252b5132
RH
549 architecture_requested = 1;
550 }
551 break;
552
553 case OPTION_SPARC:
554 /* Ignore -sparc, used by SunOS make default .s.o rule. */
555 break;
556
557 case OPTION_ENFORCE_ALIGNED_DATA:
558 enforce_aligned_data = 1;
559 break;
560
561#ifdef SPARC_BIENDIAN
562 case OPTION_LITTLE_ENDIAN:
563 target_big_endian = 0;
564 if (default_arch_type != sparclet)
565 as_fatal ("This target does not support -EL");
566 break;
567 case OPTION_LITTLE_ENDIAN_DATA:
568 target_little_endian_data = 1;
569 target_big_endian = 0;
570 if (default_arch_type != sparc86x
571 && default_arch_type != v9)
572 as_fatal ("This target does not support --little-endian-data");
573 break;
574 case OPTION_BIG_ENDIAN:
575 target_big_endian = 1;
576 break;
577#endif
578
579#ifdef OBJ_AOUT
580 case 'k':
581 sparc_pic_code = 1;
582 break;
583#endif
584
585#ifdef OBJ_ELF
586 case OPTION_32:
587 case OPTION_64:
588 {
589 const char **list, **l;
590
591 sparc_arch_size = c == OPTION_32 ? 32 : 64;
592 list = bfd_target_list ();
593 for (l = list; *l != NULL; l++)
594 {
595 if (sparc_arch_size == 32)
596 {
71a75f6f 597 if (CONST_STRNEQ (*l, "elf32-sparc"))
252b5132
RH
598 break;
599 }
600 else
601 {
71a75f6f 602 if (CONST_STRNEQ (*l, "elf64-sparc"))
252b5132
RH
603 break;
604 }
605 }
606 if (*l == NULL)
607 as_fatal (_("No compiled in support for %d bit object file format"),
608 sparc_arch_size);
609 free (list);
b7cac25f
DM
610
611 if (sparc_arch_size == 64
612 && max_architecture < SPARC_OPCODE_ARCH_V9)
613 max_architecture = SPARC_OPCODE_ARCH_V9;
252b5132
RH
614 }
615 break;
616
617 case OPTION_TSO:
618 sparc_memory_model = MM_TSO;
619 break;
620
621 case OPTION_PSO:
622 sparc_memory_model = MM_PSO;
623 break;
624
625 case OPTION_RMO:
626 sparc_memory_model = MM_RMO;
627 break;
628
629 case 'V':
630 print_version_id ();
631 break;
632
633 case 'Q':
634 /* Qy - do emit .comment
e0c6ed95 635 Qn - do not emit .comment. */
252b5132
RH
636 break;
637
638 case 's':
e0c6ed95 639 /* Use .stab instead of .stab.excl. */
252b5132
RH
640 break;
641
642 case 'q':
e0c6ed95 643 /* quick -- Native assembler does fewer checks. */
252b5132
RH
644 break;
645
646 case 'K':
647 if (strcmp (arg, "PIC") != 0)
648 as_warn (_("Unrecognized option following -K"));
649 else
650 sparc_pic_code = 1;
651 break;
6d8809aa
RH
652
653 case OPTION_NO_UNDECLARED_REGS:
654 no_undeclared_regs = 1;
655 break;
a25fe906
ILT
656
657 case OPTION_UNDECLARED_REGS:
658 no_undeclared_regs = 0;
659 break;
252b5132
RH
660#endif
661
6faf3d66
JJ
662 case OPTION_RELAX:
663 sparc_relax = 1;
664 break;
665
666 case OPTION_NO_RELAX:
667 sparc_relax = 0;
668 break;
669
252b5132
RH
670 default:
671 return 0;
672 }
673
674 return 1;
675}
676
677void
5a49b8ac 678md_show_usage (FILE *stream)
252b5132
RH
679{
680 const struct sparc_arch *arch;
c2158c24 681 int column;
252b5132
RH
682
683 /* We don't get a chance to initialize anything before we're called,
684 so handle that now. */
685 if (! default_init_p)
686 init_default_arch ();
687
e0c6ed95 688 fprintf (stream, _("SPARC options:\n"));
c2158c24 689 column = 0;
252b5132
RH
690 for (arch = &sparc_arch_table[0]; arch->name; arch++)
691 {
c2158c24
JJ
692 if (!arch->user_option_p)
693 continue;
252b5132
RH
694 if (arch != &sparc_arch_table[0])
695 fprintf (stream, " | ");
07726851 696 if (column + strlen (arch->name) > 70)
c2158c24
JJ
697 {
698 column = 0;
699 fputc ('\n', stream);
700 }
07726851 701 column += 5 + 2 + strlen (arch->name);
c2158c24 702 fprintf (stream, "-A%s", arch->name);
252b5132 703 }
c2158c24
JJ
704 for (arch = &sparc_arch_table[0]; arch->name; arch++)
705 {
706 if (!arch->user_option_p)
707 continue;
708 fprintf (stream, " | ");
07726851 709 if (column + strlen (arch->name) > 65)
c2158c24
JJ
710 {
711 column = 0;
712 fputc ('\n', stream);
713 }
07726851 714 column += 5 + 7 + strlen (arch->name);
c2158c24
JJ
715 fprintf (stream, "-xarch=%s", arch->name);
716 }
717 fprintf (stream, _("\n\
252b5132
RH
718 specify variant of SPARC architecture\n\
719-bump warn when assembler switches architectures\n\
720-sparc ignored\n\
6faf3d66
JJ
721--enforce-aligned-data force .long, etc., to be aligned correctly\n\
722-relax relax jumps and branches (default)\n\
723-no-relax avoid changing any jumps and branches\n"));
252b5132
RH
724#ifdef OBJ_AOUT
725 fprintf (stream, _("\
726-k generate PIC\n"));
727#endif
728#ifdef OBJ_ELF
729 fprintf (stream, _("\
730-32 create 32 bit object file\n\
731-64 create 64 bit object file\n"));
732 fprintf (stream, _("\
733 [default is %d]\n"), default_arch_size);
734 fprintf (stream, _("\
735-TSO use Total Store Ordering\n\
736-PSO use Partial Store Ordering\n\
737-RMO use Relaxed Memory Ordering\n"));
738 fprintf (stream, _("\
739 [default is %s]\n"), (default_arch_size == 64) ? "RMO" : "TSO");
740 fprintf (stream, _("\
741-KPIC generate PIC\n\
742-V print assembler version number\n\
a25fe906
ILT
743-undeclared-regs ignore application global register usage without\n\
744 appropriate .register directive (default)\n\
745-no-undeclared-regs force error on application global register usage\n\
746 without appropriate .register directive\n\
252b5132
RH
747-q ignored\n\
748-Qy, -Qn ignored\n\
749-s ignored\n"));
750#endif
751#ifdef SPARC_BIENDIAN
752 fprintf (stream, _("\
753-EL generate code for a little endian machine\n\
754-EB generate code for a big endian machine\n\
755--little-endian-data generate code for a machine having big endian\n\
c20f4f8c 756 instructions and little endian data.\n"));
252b5132
RH
757#endif
758}
759\f
e0c6ed95 760/* Native operand size opcode translation. */
4f2a7b51 761static struct
cf9a1301 762 {
e0471c16
TS
763 const char *name;
764 const char *name32;
765 const char *name64;
cf9a1301
RH
766 } native_op_table[] =
767{
768 {"ldn", "ld", "ldx"},
769 {"ldna", "lda", "ldxa"},
770 {"stn", "st", "stx"},
771 {"stna", "sta", "stxa"},
772 {"slln", "sll", "sllx"},
773 {"srln", "srl", "srlx"},
774 {"sran", "sra", "srax"},
775 {"casn", "cas", "casx"},
776 {"casna", "casa", "casxa"},
777 {"clrn", "clr", "clrx"},
778 {NULL, NULL, NULL},
779};
780\f
10156f83 781/* sparc64 privileged and hyperprivileged registers. */
252b5132
RH
782
783struct priv_reg_entry
ab3e48dc 784{
e0471c16 785 const char *name;
ab3e48dc
KH
786 int regnum;
787};
252b5132
RH
788
789struct priv_reg_entry priv_reg_table[] =
790{
791 {"tpc", 0},
792 {"tnpc", 1},
793 {"tstate", 2},
794 {"tt", 3},
795 {"tick", 4},
796 {"tba", 5},
797 {"pstate", 6},
798 {"tl", 7},
799 {"pil", 8},
800 {"cwp", 9},
801 {"cansave", 10},
802 {"canrestore", 11},
803 {"cleanwin", 12},
804 {"otherwin", 13},
805 {"wstate", 14},
806 {"fq", 15},
10156f83 807 {"gl", 16},
38074311 808 {"pmcdper", 23},
252b5132 809 {"ver", 31},
8b8c7c9f 810 {NULL, -1}, /* End marker. */
252b5132
RH
811};
812
10156f83
DM
813struct priv_reg_entry hpriv_reg_table[] =
814{
815 {"hpstate", 0},
816 {"htstate", 1},
817 {"hintp", 3},
818 {"htba", 5},
819 {"hver", 6},
ec92c392
JM
820 {"hstick_offset", 28},
821 {"hstick_enable", 29},
10156f83 822 {"hstick_cmpr", 31},
8b8c7c9f 823 {NULL, -1}, /* End marker. */
10156f83
DM
824};
825
8b8c7c9f 826/* v9a or later specific ancillary state registers. */
252b5132
RH
827
828struct priv_reg_entry v9a_asr_table[] =
829{
830 {"tick_cmpr", 23},
19f7b010
JJ
831 {"sys_tick_cmpr", 25},
832 {"sys_tick", 24},
1a6b486f
DM
833 {"stick_cmpr", 25},
834 {"stick", 24},
f04d18b7
DM
835 {"softint_clear", 21},
836 {"softint_set", 20},
2b661f3d 837 {"softint", 22},
252b5132 838 {"set_softint", 20},
58004e23 839 {"pause", 27},
252b5132
RH
840 {"pic", 17},
841 {"pcr", 16},
3d68f91c 842 {"mwait", 28},
252b5132
RH
843 {"gsr", 19},
844 {"dcr", 18},
2e52845b 845 {"cfr", 26},
252b5132 846 {"clear_softint", 21},
8b8c7c9f 847 {NULL, -1}, /* End marker. */
252b5132
RH
848};
849
850static int
5a49b8ac 851cmp_reg_entry (const void *parg, const void *qarg)
252b5132
RH
852{
853 const struct priv_reg_entry *p = (const struct priv_reg_entry *) parg;
854 const struct priv_reg_entry *q = (const struct priv_reg_entry *) qarg;
855
8b8c7c9f
JM
856 if (p->name == q->name)
857 return 0;
858 else if (p->name == NULL)
859 return 1;
860 else if (q->name == NULL)
861 return -1;
862 else
863 return strcmp (q->name, p->name);
864}
865\f
866/* sparc %-pseudo-operations. */
867
868
869#define F_POP_V9 0x1 /* The pseudo-op is for v9 only. */
870#define F_POP_PCREL 0x2 /* The pseudo-op can be used in pc-relative
871 contexts. */
872#define F_POP_TLS_CALL 0x4 /* The pseudo-op marks a tls call. */
873#define F_POP_POSTFIX 0x8 /* The pseudo-op should appear after the
874 last operand of an
875 instruction. (Generally they can appear
876 anywhere an immediate operand is
877 expected. */
878struct pop_entry
879{
880 /* The name as it appears in assembler. */
881 const char *name;
882 /* The reloc this pseudo-op translates to. */
883 int reloc;
884 /* Flags. See F_POP_* above. */
885 int flags;
886};
887
888struct pop_entry pop_table[] =
889{
890 { "hix", BFD_RELOC_SPARC_HIX22, F_POP_V9 },
891 { "lox", BFD_RELOC_SPARC_LOX10, F_POP_V9 },
892 { "hi", BFD_RELOC_HI22, F_POP_PCREL },
893 { "lo", BFD_RELOC_LO10, F_POP_PCREL },
894 { "pc22", BFD_RELOC_SPARC_PC22, F_POP_PCREL },
895 { "pc10", BFD_RELOC_SPARC_PC10, F_POP_PCREL },
896 { "hh", BFD_RELOC_SPARC_HH22, F_POP_V9|F_POP_PCREL },
897 { "hm", BFD_RELOC_SPARC_HM10, F_POP_V9|F_POP_PCREL },
898 { "lm", BFD_RELOC_SPARC_LM22, F_POP_V9|F_POP_PCREL },
899 { "h34", BFD_RELOC_SPARC_H34, F_POP_V9 },
900 { "l34", BFD_RELOC_SPARC_L44, F_POP_V9 },
901 { "h44", BFD_RELOC_SPARC_H44, F_POP_V9 },
902 { "m44", BFD_RELOC_SPARC_M44, F_POP_V9 },
903 { "l44", BFD_RELOC_SPARC_L44, F_POP_V9 },
904 { "uhi", BFD_RELOC_SPARC_HH22, F_POP_V9 },
905 { "ulo", BFD_RELOC_SPARC_HM10, F_POP_V9 },
906 { "tgd_hi22", BFD_RELOC_SPARC_TLS_GD_HI22, 0 },
907 { "tgd_lo10", BFD_RELOC_SPARC_TLS_GD_LO10, 0 },
908 { "tldm_hi22", BFD_RELOC_SPARC_TLS_LDM_HI22, 0 },
909 { "tldm_lo10", BFD_RELOC_SPARC_TLS_LDM_LO10, 0 },
910 { "tldo_hix22", BFD_RELOC_SPARC_TLS_LDO_HIX22, 0 },
911 { "tldo_lox10", BFD_RELOC_SPARC_TLS_LDO_LOX10, 0 },
912 { "tie_hi22", BFD_RELOC_SPARC_TLS_IE_HI22, 0 },
913 { "tie_lo10", BFD_RELOC_SPARC_TLS_IE_LO10, 0 },
914 { "tle_hix22", BFD_RELOC_SPARC_TLS_LE_HIX22, 0 },
915 { "tle_lox10", BFD_RELOC_SPARC_TLS_LE_LOX10, 0 },
916 { "gdop_hix22", BFD_RELOC_SPARC_GOTDATA_OP_HIX22, 0 },
917 { "gdop_lox10", BFD_RELOC_SPARC_GOTDATA_OP_LOX10, 0 },
918 { "tgd_add", BFD_RELOC_SPARC_TLS_GD_ADD, F_POP_POSTFIX },
919 { "tgd_call", BFD_RELOC_SPARC_TLS_GD_CALL, F_POP_POSTFIX|F_POP_TLS_CALL },
920 { "tldm_add", BFD_RELOC_SPARC_TLS_LDM_ADD, F_POP_POSTFIX },
921 { "tldm_call", BFD_RELOC_SPARC_TLS_LDM_CALL, F_POP_POSTFIX|F_POP_TLS_CALL },
922 { "tldo_add", BFD_RELOC_SPARC_TLS_LDO_ADD, F_POP_POSTFIX },
923 { "tie_ldx", BFD_RELOC_SPARC_TLS_IE_LDX, F_POP_POSTFIX },
924 { "tie_ld", BFD_RELOC_SPARC_TLS_IE_LD, F_POP_POSTFIX },
925 { "tie_add", BFD_RELOC_SPARC_TLS_IE_ADD, F_POP_POSTFIX },
926 { "gdop", BFD_RELOC_SPARC_GOTDATA_OP, F_POP_POSTFIX },
927 { NULL, 0, 0 },
928};
929\f
930/* Table of %-names that can appear in a sparc assembly program. This
931 table is initialized in md_begin and contains entries for each
932 privileged/hyperprivileged/alternate register and %-pseudo-op. */
933
934enum perc_entry_type
935{
936 perc_entry_none = 0,
937 perc_entry_reg,
938 perc_entry_post_pop,
939 perc_entry_imm_pop
940};
941
942struct perc_entry
943{
944 /* Entry type. */
945 enum perc_entry_type type;
946 /* Name of the %-entity. */
947 const char *name;
948 /* strlen (name). */
949 int len;
950 /* Value. Either a pop or a reg depending on type.*/
951 union
952 {
953 struct pop_entry *pop;
954 struct priv_reg_entry *reg;
955 };
956};
957
958#define NUM_PERC_ENTRIES \
959 (((sizeof (priv_reg_table) / sizeof (priv_reg_table[0])) - 1) \
960 + ((sizeof (hpriv_reg_table) / sizeof (hpriv_reg_table[0])) - 1) \
961 + ((sizeof (v9a_asr_table) / sizeof (v9a_asr_table[0])) - 1) \
962 + ((sizeof (pop_table) / sizeof (pop_table[0])) - 1) \
963 + 1)
964
965struct perc_entry perc_table[NUM_PERC_ENTRIES];
966
967static int
968cmp_perc_entry (const void *parg, const void *qarg)
969{
970 const struct perc_entry *p = (const struct perc_entry *) parg;
971 const struct perc_entry *q = (const struct perc_entry *) qarg;
972
973 if (p->name == q->name)
974 return 0;
975 else if (p->name == NULL)
976 return 1;
977 else if (q->name == NULL)
978 return -1;
979 else
980 return strcmp (q->name, p->name);
252b5132
RH
981}
982\f
983/* This function is called once, at assembler startup time. It should
e0c6ed95
AM
984 set up all the tables, etc. that the MD part of the assembler will
985 need. */
252b5132
RH
986
987void
5a49b8ac 988md_begin (void)
252b5132 989{
ed9e98c2 990 const char *retval = NULL;
252b5132 991 int lose = 0;
ed9e98c2 992 unsigned int i = 0;
252b5132
RH
993
994 /* We don't get a chance to initialize anything before md_parse_option
995 is called, and it may not be called, so handle default initialization
996 now if not already done. */
997 if (! default_init_p)
998 init_default_arch ();
999
364b6d8b 1000 sparc_cie_data_alignment = sparc_arch_size == 64 ? -8 : -4;
252b5132
RH
1001 op_hash = hash_new ();
1002
1003 while (i < (unsigned int) sparc_num_opcodes)
1004 {
1005 const char *name = sparc_opcodes[i].name;
5a49b8ac 1006 retval = hash_insert (op_hash, name, (void *) &sparc_opcodes[i]);
252b5132
RH
1007 if (retval != NULL)
1008 {
cf9a1301
RH
1009 as_bad (_("Internal error: can't hash `%s': %s\n"),
1010 sparc_opcodes[i].name, retval);
252b5132
RH
1011 lose = 1;
1012 }
1013 do
1014 {
1015 if (sparc_opcodes[i].match & sparc_opcodes[i].lose)
1016 {
cf9a1301
RH
1017 as_bad (_("Internal error: losing opcode: `%s' \"%s\"\n"),
1018 sparc_opcodes[i].name, sparc_opcodes[i].args);
252b5132
RH
1019 lose = 1;
1020 }
1021 ++i;
1022 }
1023 while (i < (unsigned int) sparc_num_opcodes
1024 && !strcmp (sparc_opcodes[i].name, name));
1025 }
1026
cf9a1301
RH
1027 for (i = 0; native_op_table[i].name; i++)
1028 {
1029 const struct sparc_opcode *insn;
e0471c16 1030 const char *name = ((sparc_arch_size == 32)
3d4ae3c0
NC
1031 ? native_op_table[i].name32
1032 : native_op_table[i].name64);
e0c6ed95 1033 insn = (struct sparc_opcode *) hash_find (op_hash, name);
cf9a1301 1034 if (insn == NULL)
e0c6ed95
AM
1035 {
1036 as_bad (_("Internal error: can't find opcode `%s' for `%s'\n"),
1037 name, native_op_table[i].name);
1038 lose = 1;
1039 }
cf9a1301
RH
1040 else
1041 {
5a49b8ac
AM
1042 retval = hash_insert (op_hash, native_op_table[i].name,
1043 (void *) insn);
cf9a1301
RH
1044 if (retval != NULL)
1045 {
1046 as_bad (_("Internal error: can't hash `%s': %s\n"),
1047 sparc_opcodes[i].name, retval);
1048 lose = 1;
1049 }
1050 }
1051 }
1052
252b5132
RH
1053 if (lose)
1054 as_fatal (_("Broken assembler. No assembly attempted."));
1055
252b5132
RH
1056 qsort (priv_reg_table, sizeof (priv_reg_table) / sizeof (priv_reg_table[0]),
1057 sizeof (priv_reg_table[0]), cmp_reg_entry);
8b8c7c9f
JM
1058 qsort (hpriv_reg_table, sizeof (hpriv_reg_table) / sizeof (hpriv_reg_table[0]),
1059 sizeof (hpriv_reg_table[0]), cmp_reg_entry);
1060 qsort (v9a_asr_table, sizeof (v9a_asr_table) / sizeof (v9a_asr_table[0]),
1061 sizeof (v9a_asr_table[0]), cmp_reg_entry);
1062
252b5132
RH
1063 /* If -bump, record the architecture level at which we start issuing
1064 warnings. The behaviour is different depending upon whether an
1065 architecture was explicitly specified. If it wasn't, we issue warnings
1066 for all upwards bumps. If it was, we don't start issuing warnings until
1067 we need to bump beyond the requested architecture or when we bump between
1068 conflicting architectures. */
1069
1070 if (warn_on_bump
1071 && architecture_requested)
1072 {
1073 /* `max_architecture' records the requested architecture.
1074 Issue warnings if we go above it. */
1075 warn_after_architecture = max_architecture;
252b5132 1076 }
75ac3a7f
JM
1077
1078 /* Find the highest architecture level that doesn't conflict with
1079 the requested one. */
1080
1081 if (warn_on_bump
1082 || !architecture_requested)
1083 {
1084 enum sparc_opcode_arch_val current_max_architecture
1085 = max_architecture;
1086
1087 for (max_architecture = SPARC_OPCODE_ARCH_MAX;
1088 max_architecture > warn_after_architecture;
1089 --max_architecture)
1090 if (! SPARC_OPCODE_CONFLICT_P (max_architecture,
1091 current_max_architecture))
1092 break;
1093 }
8b8c7c9f
JM
1094
1095 /* Prepare the tables of %-pseudo-ops. */
1096 {
1097 struct priv_reg_entry *reg_tables[]
1098 = {priv_reg_table, hpriv_reg_table, v9a_asr_table, NULL};
1099 struct priv_reg_entry **reg_table;
1100 int entry = 0;
1101
1102 /* Add registers. */
1103 for (reg_table = reg_tables; reg_table[0]; reg_table++)
1104 {
1105 struct priv_reg_entry *reg;
1106 for (reg = *reg_table; reg->name; reg++)
1107 {
1108 struct perc_entry *p = &perc_table[entry++];
1109 p->type = perc_entry_reg;
1110 p->name = reg->name;
1111 p->len = strlen (reg->name);
1112 p->reg = reg;
1113 }
1114 }
1115
1116 /* Add %-pseudo-ops. */
1117 {
1118 struct pop_entry *pop;
1119
1120 for (pop = pop_table; pop->name; pop++)
1121 {
1122 struct perc_entry *p = &perc_table[entry++];
1123 p->type = (pop->flags & F_POP_POSTFIX
1124 ? perc_entry_post_pop : perc_entry_imm_pop);
1125 p->name = pop->name;
1126 p->len = strlen (pop->name);
1127 p->pop = pop;
1128 }
1129 }
1130
1131 /* Last entry is the centinel. */
1132 perc_table[entry].type = perc_entry_none;
1133
1134 qsort (perc_table, sizeof (perc_table) / sizeof (perc_table[0]),
1135 sizeof (perc_table[0]), cmp_perc_entry);
1136
1137 }
252b5132
RH
1138}
1139
1140/* Called after all assembly has been done. */
1141
1142void
5a49b8ac 1143sparc_md_end (void)
252b5132 1144{
19f7b010 1145 unsigned long mach = bfd_mach_sparc;
daf5e10e 1146#if defined(OBJ_ELF) && !defined(TE_SOLARIS)
3d68f91c 1147 int hwcaps, hwcaps2;
daf5e10e 1148#endif
19f7b010 1149
252b5132 1150 if (sparc_arch_size == 64)
19f7b010
JJ
1151 switch (current_architecture)
1152 {
1153 case SPARC_OPCODE_ARCH_V9A: mach = bfd_mach_sparc_v9a; break;
1154 case SPARC_OPCODE_ARCH_V9B: mach = bfd_mach_sparc_v9b; break;
1155 default: mach = bfd_mach_sparc_v9; break;
1156 }
252b5132 1157 else
19f7b010
JJ
1158 switch (current_architecture)
1159 {
1160 case SPARC_OPCODE_ARCH_SPARCLET: mach = bfd_mach_sparc_sparclet; break;
1161 case SPARC_OPCODE_ARCH_V9: mach = bfd_mach_sparc_v8plus; break;
1162 case SPARC_OPCODE_ARCH_V9A: mach = bfd_mach_sparc_v8plusa; break;
1163 case SPARC_OPCODE_ARCH_V9B: mach = bfd_mach_sparc_v8plusb; break;
1164 /* The sparclite is treated like a normal sparc. Perhaps it shouldn't
1165 be but for now it is (since that's the way it's always been
1166 treated). */
1167 default: break;
1168 }
1169 bfd_set_arch_mach (stdoutput, bfd_arch_sparc, mach);
9e8c70f9
DM
1170
1171#if defined(OBJ_ELF) && !defined(TE_SOLARIS)
3d68f91c
JM
1172 hwcaps = hwcap_seen & U0xffffffff;
1173 hwcaps2 = hwcap_seen >> 32;
1174
1175 if (hwcaps)
1176 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU, Tag_GNU_Sparc_HWCAPS, hwcaps);
1177 if (hwcaps2)
1178 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU, Tag_GNU_Sparc_HWCAPS2, hwcaps2);
9e8c70f9 1179#endif
252b5132
RH
1180}
1181\f
1182/* Return non-zero if VAL is in the range -(MAX+1) to MAX. */
1183
5a49b8ac
AM
1184static inline int
1185in_signed_range (bfd_signed_vma val, bfd_signed_vma max)
252b5132
RH
1186{
1187 if (max <= 0)
1188 abort ();
1189 /* Sign-extend the value from the architecture word size, so that
1190 0xffffffff is always considered -1 on sparc32. */
1191 if (sparc_arch_size == 32)
1192 {
e0c6ed95 1193 bfd_signed_vma sign = (bfd_signed_vma) 1 << 31;
6c1b24e4 1194 val = ((val & U0xffffffff) ^ sign) - sign;
252b5132
RH
1195 }
1196 if (val > max)
1197 return 0;
1198 if (val < ~max)
1199 return 0;
1200 return 1;
1201}
1202
1203/* Return non-zero if VAL is in the range 0 to MAX. */
1204
5a49b8ac
AM
1205static inline int
1206in_unsigned_range (bfd_vma val, bfd_vma max)
252b5132
RH
1207{
1208 if (val > max)
1209 return 0;
1210 return 1;
1211}
1212
1213/* Return non-zero if VAL is in the range -(MAX/2+1) to MAX.
1214 (e.g. -15 to +31). */
1215
5a49b8ac
AM
1216static inline int
1217in_bitfield_range (bfd_signed_vma val, bfd_signed_vma max)
252b5132
RH
1218{
1219 if (max <= 0)
1220 abort ();
1221 if (val > max)
1222 return 0;
1223 if (val < ~(max >> 1))
1224 return 0;
1225 return 1;
1226}
1227
1228static int
5a49b8ac 1229sparc_ffs (unsigned int mask)
252b5132
RH
1230{
1231 int i;
1232
1233 if (mask == 0)
1234 return -1;
1235
1236 for (i = 0; (mask & 1) == 0; ++i)
1237 mask >>= 1;
1238 return i;
1239}
1240
1241/* Implement big shift right. */
1242static bfd_vma
5a49b8ac 1243BSR (bfd_vma val, int amount)
252b5132
RH
1244{
1245 if (sizeof (bfd_vma) <= 4 && amount >= 32)
1246 as_fatal (_("Support for 64-bit arithmetic not compiled in."));
1247 return val >> amount;
1248}
1249\f
1250/* For communication between sparc_ip and get_expression. */
1251static char *expr_end;
1252
252b5132
RH
1253/* Values for `special_case'.
1254 Instructions that require wierd handling because they're longer than
1255 4 bytes. */
1256#define SPECIAL_CASE_NONE 0
1257#define SPECIAL_CASE_SET 1
1258#define SPECIAL_CASE_SETSW 2
1259#define SPECIAL_CASE_SETX 3
1260/* FIXME: sparc-opc.c doesn't have necessary "S" trigger to enable this. */
1261#define SPECIAL_CASE_FDIV 4
1262
1263/* Bit masks of various insns. */
1264#define NOP_INSN 0x01000000
1265#define OR_INSN 0x80100000
63fab58c 1266#define XOR_INSN 0x80180000
252b5132
RH
1267#define FMOVS_INSN 0x81A00020
1268#define SETHI_INSN 0x01000000
1269#define SLLX_INSN 0x81281000
1270#define SRA_INSN 0x81380000
1271
1272/* The last instruction to be assembled. */
1273static const struct sparc_opcode *last_insn;
1274/* The assembled opcode of `last_insn'. */
1275static unsigned long last_opcode;
1276\f
a22b281c 1277/* Handle the set and setuw synthetic instructions. */
e0c6ed95 1278
a22b281c 1279static void
5a49b8ac 1280synthetize_setuw (const struct sparc_opcode *insn)
a22b281c
RH
1281{
1282 int need_hi22_p = 0;
1283 int rd = (the_insn.opcode & RD (~0)) >> 25;
1284
1285 if (the_insn.exp.X_op == O_constant)
1286 {
1287 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1288 {
e0c6ed95 1289 if (sizeof (offsetT) > 4
a22b281c 1290 && (the_insn.exp.X_add_number < 0
6c1b24e4 1291 || the_insn.exp.X_add_number > (offsetT) U0xffffffff))
a22b281c
RH
1292 as_warn (_("set: number not in 0..4294967295 range"));
1293 }
1294 else
1295 {
e0c6ed95 1296 if (sizeof (offsetT) > 4
6c1b24e4
AO
1297 && (the_insn.exp.X_add_number < -(offsetT) U0x80000000
1298 || the_insn.exp.X_add_number > (offsetT) U0xffffffff))
a22b281c 1299 as_warn (_("set: number not in -2147483648..4294967295 range"));
e0c6ed95 1300 the_insn.exp.X_add_number = (int) the_insn.exp.X_add_number;
a22b281c
RH
1301 }
1302 }
1303
1304 /* See if operand is absolute and small; skip sethi if so. */
1305 if (the_insn.exp.X_op != O_constant
1306 || the_insn.exp.X_add_number >= (1 << 12)
1307 || the_insn.exp.X_add_number < -(1 << 12))
1308 {
1309 the_insn.opcode = (SETHI_INSN | RD (rd)
1310 | ((the_insn.exp.X_add_number >> 10)
ab3e48dc
KH
1311 & (the_insn.exp.X_op == O_constant
1312 ? 0x3fffff : 0)));
a22b281c 1313 the_insn.reloc = (the_insn.exp.X_op != O_constant
ab3e48dc 1314 ? BFD_RELOC_HI22 : BFD_RELOC_NONE);
a22b281c
RH
1315 output_insn (insn, &the_insn);
1316 need_hi22_p = 1;
1317 }
1318
1319 /* See if operand has no low-order bits; skip OR if so. */
1320 if (the_insn.exp.X_op != O_constant
1321 || (need_hi22_p && (the_insn.exp.X_add_number & 0x3FF) != 0)
1322 || ! need_hi22_p)
1323 {
1324 the_insn.opcode = (OR_INSN | (need_hi22_p ? RS1 (rd) : 0)
1325 | RD (rd) | IMMED
1326 | (the_insn.exp.X_add_number
ab3e48dc
KH
1327 & (the_insn.exp.X_op != O_constant
1328 ? 0 : need_hi22_p ? 0x3ff : 0x1fff)));
a22b281c 1329 the_insn.reloc = (the_insn.exp.X_op != O_constant
ab3e48dc 1330 ? BFD_RELOC_LO10 : BFD_RELOC_NONE);
a22b281c
RH
1331 output_insn (insn, &the_insn);
1332 }
1333}
e0c6ed95 1334
a22b281c 1335/* Handle the setsw synthetic instruction. */
e0c6ed95 1336
a22b281c 1337static void
5a49b8ac 1338synthetize_setsw (const struct sparc_opcode *insn)
a22b281c
RH
1339{
1340 int low32, rd, opc;
1341
1342 rd = (the_insn.opcode & RD (~0)) >> 25;
1343
1344 if (the_insn.exp.X_op != O_constant)
1345 {
1346 synthetize_setuw (insn);
1347
1348 /* Need to sign extend it. */
1349 the_insn.opcode = (SRA_INSN | RS1 (rd) | RD (rd));
1350 the_insn.reloc = BFD_RELOC_NONE;
1351 output_insn (insn, &the_insn);
1352 return;
1353 }
1354
e0c6ed95 1355 if (sizeof (offsetT) > 4
6c1b24e4
AO
1356 && (the_insn.exp.X_add_number < -(offsetT) U0x80000000
1357 || the_insn.exp.X_add_number > (offsetT) U0xffffffff))
a22b281c
RH
1358 as_warn (_("setsw: number not in -2147483648..4294967295 range"));
1359
e0c6ed95
AM
1360 low32 = the_insn.exp.X_add_number;
1361
a22b281c
RH
1362 if (low32 >= 0)
1363 {
1364 synthetize_setuw (insn);
1365 return;
1366 }
1367
1368 opc = OR_INSN;
e0c6ed95 1369
a22b281c
RH
1370 the_insn.reloc = BFD_RELOC_NONE;
1371 /* See if operand is absolute and small; skip sethi if so. */
1372 if (low32 < -(1 << 12))
1373 {
1374 the_insn.opcode = (SETHI_INSN | RD (rd)
1375 | (((~the_insn.exp.X_add_number) >> 10) & 0x3fffff));
1376 output_insn (insn, &the_insn);
1377 low32 = 0x1c00 | (low32 & 0x3ff);
1378 opc = RS1 (rd) | XOR_INSN;
1379 }
1380
1381 the_insn.opcode = (opc | RD (rd) | IMMED
1382 | (low32 & 0x1fff));
1383 output_insn (insn, &the_insn);
1384}
1385
0d495746 1386/* Handle the setx synthetic instruction. */
e0c6ed95 1387
a22b281c 1388static void
5a49b8ac 1389synthetize_setx (const struct sparc_opcode *insn)
a22b281c
RH
1390{
1391 int upper32, lower32;
1392 int tmpreg = (the_insn.opcode & RS1 (~0)) >> 14;
1393 int dstreg = (the_insn.opcode & RD (~0)) >> 25;
1394 int upper_dstreg;
1395 int need_hh22_p = 0, need_hm10_p = 0, need_hi22_p = 0, need_lo10_p = 0;
1396 int need_xor10_p = 0;
e0c6ed95 1397
6c1b24e4 1398#define SIGNEXT32(x) ((((x) & U0xffffffff) ^ U0x80000000) - U0x80000000)
a22b281c
RH
1399 lower32 = SIGNEXT32 (the_insn.exp.X_add_number);
1400 upper32 = SIGNEXT32 (BSR (the_insn.exp.X_add_number, 32));
1401#undef SIGNEXT32
1402
1403 upper_dstreg = tmpreg;
1404 /* The tmp reg should not be the dst reg. */
1405 if (tmpreg == dstreg)
1406 as_warn (_("setx: temporary register same as destination register"));
1407
1408 /* ??? Obviously there are other optimizations we can do
1409 (e.g. sethi+shift for 0x1f0000000) and perhaps we shouldn't be
1410 doing some of these. Later. If you do change things, try to
1411 change all of this to be table driven as well. */
1412 /* What to output depends on the number if it's constant.
1413 Compute that first, then output what we've decided upon. */
1414 if (the_insn.exp.X_op != O_constant)
1415 {
1416 if (sparc_arch_size == 32)
1417 {
1418 /* When arch size is 32, we want setx to be equivalent
1419 to setuw for anything but constants. */
1420 the_insn.exp.X_add_number &= 0xffffffff;
1421 synthetize_setuw (insn);
1422 return;
1423 }
1424 need_hh22_p = need_hm10_p = need_hi22_p = need_lo10_p = 1;
ab3e48dc
KH
1425 lower32 = 0;
1426 upper32 = 0;
a22b281c
RH
1427 }
1428 else
1429 {
1430 /* Reset X_add_number, we've extracted it as upper32/lower32.
1431 Otherwise fixup_segment will complain about not being able to
1432 write an 8 byte number in a 4 byte field. */
1433 the_insn.exp.X_add_number = 0;
e0c6ed95 1434
a22b281c
RH
1435 /* Only need hh22 if `or' insn can't handle constant. */
1436 if (upper32 < -(1 << 12) || upper32 >= (1 << 12))
1437 need_hh22_p = 1;
e0c6ed95 1438
a22b281c
RH
1439 /* Does bottom part (after sethi) have bits? */
1440 if ((need_hh22_p && (upper32 & 0x3ff) != 0)
1441 /* No hh22, but does upper32 still have bits we can't set
1442 from lower32? */
1443 || (! need_hh22_p && upper32 != 0 && upper32 != -1))
1444 need_hm10_p = 1;
e0c6ed95 1445
a22b281c
RH
1446 /* If the lower half is all zero, we build the upper half directly
1447 into the dst reg. */
1448 if (lower32 != 0
1449 /* Need lower half if number is zero or 0xffffffff00000000. */
1450 || (! need_hh22_p && ! need_hm10_p))
1451 {
1452 /* No need for sethi if `or' insn can handle constant. */
1453 if (lower32 < -(1 << 12) || lower32 >= (1 << 12)
1454 /* Note that we can't use a negative constant in the `or'
1455 insn unless the upper 32 bits are all ones. */
1456 || (lower32 < 0 && upper32 != -1)
1457 || (lower32 >= 0 && upper32 == -1))
1458 need_hi22_p = 1;
e0c6ed95 1459
a22b281c
RH
1460 if (need_hi22_p && upper32 == -1)
1461 need_xor10_p = 1;
1462
1463 /* Does bottom part (after sethi) have bits? */
1464 else if ((need_hi22_p && (lower32 & 0x3ff) != 0)
1465 /* No sethi. */
1466 || (! need_hi22_p && (lower32 & 0x1fff) != 0)
1467 /* Need `or' if we didn't set anything else. */
1468 || (! need_hi22_p && ! need_hh22_p && ! need_hm10_p))
1469 need_lo10_p = 1;
1470 }
1471 else
1472 /* Output directly to dst reg if lower 32 bits are all zero. */
1473 upper_dstreg = dstreg;
1474 }
e0c6ed95 1475
a22b281c
RH
1476 if (!upper_dstreg && dstreg)
1477 as_warn (_("setx: illegal temporary register g0"));
1478
1479 if (need_hh22_p)
1480 {
1481 the_insn.opcode = (SETHI_INSN | RD (upper_dstreg)
1482 | ((upper32 >> 10) & 0x3fffff));
1483 the_insn.reloc = (the_insn.exp.X_op != O_constant
1484 ? BFD_RELOC_SPARC_HH22 : BFD_RELOC_NONE);
1485 output_insn (insn, &the_insn);
1486 }
e0c6ed95 1487
a22b281c
RH
1488 if (need_hi22_p)
1489 {
1490 the_insn.opcode = (SETHI_INSN | RD (dstreg)
1491 | (((need_xor10_p ? ~lower32 : lower32)
ab3e48dc 1492 >> 10) & 0x3fffff));
a22b281c
RH
1493 the_insn.reloc = (the_insn.exp.X_op != O_constant
1494 ? BFD_RELOC_SPARC_LM22 : BFD_RELOC_NONE);
1495 output_insn (insn, &the_insn);
1496 }
1497
1498 if (need_hm10_p)
1499 {
1500 the_insn.opcode = (OR_INSN
1501 | (need_hh22_p ? RS1 (upper_dstreg) : 0)
1502 | RD (upper_dstreg)
1503 | IMMED
1504 | (upper32 & (need_hh22_p ? 0x3ff : 0x1fff)));
1505 the_insn.reloc = (the_insn.exp.X_op != O_constant
1506 ? BFD_RELOC_SPARC_HM10 : BFD_RELOC_NONE);
1507 output_insn (insn, &the_insn);
1508 }
e0c6ed95 1509
a22b281c
RH
1510 if (need_lo10_p)
1511 {
1512 /* FIXME: One nice optimization to do here is to OR the low part
1513 with the highpart if hi22 isn't needed and the low part is
1514 positive. */
1515 the_insn.opcode = (OR_INSN | (need_hi22_p ? RS1 (dstreg) : 0)
1516 | RD (dstreg)
1517 | IMMED
1518 | (lower32 & (need_hi22_p ? 0x3ff : 0x1fff)));
1519 the_insn.reloc = (the_insn.exp.X_op != O_constant
1520 ? BFD_RELOC_LO10 : BFD_RELOC_NONE);
1521 output_insn (insn, &the_insn);
1522 }
e0c6ed95 1523
a22b281c
RH
1524 /* If we needed to build the upper part, shift it into place. */
1525 if (need_hh22_p || need_hm10_p)
1526 {
1527 the_insn.opcode = (SLLX_INSN | RS1 (upper_dstreg) | RD (upper_dstreg)
1528 | IMMED | 32);
1529 the_insn.reloc = BFD_RELOC_NONE;
1530 output_insn (insn, &the_insn);
1531 }
e0c6ed95 1532
a22b281c
RH
1533 /* To get -1 in upper32, we do sethi %hi(~x), r; xor r, -0x400 | x, r. */
1534 if (need_xor10_p)
1535 {
1536 the_insn.opcode = (XOR_INSN | RS1 (dstreg) | RD (dstreg) | IMMED
1537 | 0x1c00 | (lower32 & 0x3ff));
1538 the_insn.reloc = BFD_RELOC_NONE;
1539 output_insn (insn, &the_insn);
1540 }
1541
1542 /* If we needed to build both upper and lower parts, OR them together. */
1543 else if ((need_hh22_p || need_hm10_p) && (need_hi22_p || need_lo10_p))
1544 {
1545 the_insn.opcode = (OR_INSN | RS1 (dstreg) | RS2 (upper_dstreg)
1546 | RD (dstreg));
1547 the_insn.reloc = BFD_RELOC_NONE;
1548 output_insn (insn, &the_insn);
1549 }
1550}
1551\f
252b5132
RH
1552/* Main entry point to assemble one instruction. */
1553
1554void
5a49b8ac 1555md_assemble (char *str)
252b5132
RH
1556{
1557 const struct sparc_opcode *insn;
a22b281c 1558 int special_case;
252b5132
RH
1559
1560 know (str);
a22b281c 1561 special_case = sparc_ip (str, &insn);
b0825cc2
DM
1562 if (insn == NULL)
1563 return;
252b5132
RH
1564
1565 /* We warn about attempts to put a floating point branch in a delay slot,
1566 unless the delay slot has been annulled. */
b0825cc2 1567 if (last_insn != NULL
252b5132
RH
1568 && (insn->flags & F_FBR) != 0
1569 && (last_insn->flags & F_DELAYED) != 0
1570 /* ??? This test isn't completely accurate. We assume anything with
1571 F_{UNBR,CONDBR,FBR} set is annullable. */
1572 && ((last_insn->flags & (F_UNBR | F_CONDBR | F_FBR)) == 0
1573 || (last_opcode & ANNUL) == 0))
1574 as_warn (_("FP branch in delay slot"));
1575
1576 /* SPARC before v9 requires a nop instruction between a floating
1577 point instruction and a floating point branch. We insert one
1578 automatically, with a warning. */
1579 if (max_architecture < SPARC_OPCODE_ARCH_V9
252b5132
RH
1580 && last_insn != NULL
1581 && (insn->flags & F_FBR) != 0
1582 && (last_insn->flags & F_FLOAT) != 0)
1583 {
1584 struct sparc_it nop_insn;
1585
1586 nop_insn.opcode = NOP_INSN;
1587 nop_insn.reloc = BFD_RELOC_NONE;
1588 output_insn (insn, &nop_insn);
1589 as_warn (_("FP branch preceded by FP instruction; NOP inserted"));
1590 }
1591
a22b281c
RH
1592 switch (special_case)
1593 {
1594 case SPECIAL_CASE_NONE:
e0c6ed95 1595 /* Normal insn. */
a22b281c
RH
1596 output_insn (insn, &the_insn);
1597 break;
252b5132 1598
a22b281c
RH
1599 case SPECIAL_CASE_SETSW:
1600 synthetize_setsw (insn);
1601 break;
e0c6ed95 1602
a22b281c
RH
1603 case SPECIAL_CASE_SET:
1604 synthetize_setuw (insn);
1605 break;
252b5132 1606
a22b281c
RH
1607 case SPECIAL_CASE_SETX:
1608 synthetize_setx (insn);
1609 break;
e0c6ed95 1610
a22b281c
RH
1611 case SPECIAL_CASE_FDIV:
1612 {
1613 int rd = (the_insn.opcode >> 25) & 0x1f;
e0c6ed95 1614
a22b281c 1615 output_insn (insn, &the_insn);
e0c6ed95 1616
a22b281c
RH
1617 /* According to information leaked from Sun, the "fdiv" instructions
1618 on early SPARC machines would produce incorrect results sometimes.
1619 The workaround is to add an fmovs of the destination register to
1620 itself just after the instruction. This was true on machines
e0c6ed95 1621 with Weitek 1165 float chips, such as the Sun-4/260 and /280. */
9c2799c2 1622 gas_assert (the_insn.reloc == BFD_RELOC_NONE);
a22b281c
RH
1623 the_insn.opcode = FMOVS_INSN | rd | RD (rd);
1624 output_insn (insn, &the_insn);
1625 return;
1626 }
e0c6ed95 1627
a22b281c
RH
1628 default:
1629 as_fatal (_("failed special case insn sanity check"));
252b5132
RH
1630 }
1631}
1632
4bafe00e 1633static const char *
3d68f91c 1634get_hwcap_name (bfd_uint64_t mask)
4bafe00e 1635{
ec668d69 1636 if (mask & HWCAP_MUL32)
4bafe00e 1637 return "mul32";
ec668d69 1638 if (mask & HWCAP_DIV32)
4bafe00e 1639 return "div32";
ec668d69 1640 if (mask & HWCAP_FSMULD)
4bafe00e 1641 return "fsmuld";
ec668d69 1642 if (mask & HWCAP_V8PLUS)
4bafe00e 1643 return "v8plus";
ec668d69 1644 if (mask & HWCAP_POPC)
4bafe00e 1645 return "popc";
ec668d69 1646 if (mask & HWCAP_VIS)
4bafe00e 1647 return "vis";
ec668d69 1648 if (mask & HWCAP_VIS2)
4bafe00e 1649 return "vis2";
ec668d69 1650 if (mask & HWCAP_ASI_BLK_INIT)
4bafe00e 1651 return "ASIBlkInit";
ec668d69 1652 if (mask & HWCAP_FMAF)
4bafe00e 1653 return "fmaf";
ec668d69 1654 if (mask & HWCAP_VIS3)
4bafe00e 1655 return "vis3";
ec668d69 1656 if (mask & HWCAP_HPC)
4bafe00e 1657 return "hpc";
ec668d69 1658 if (mask & HWCAP_RANDOM)
4bafe00e 1659 return "random";
ec668d69 1660 if (mask & HWCAP_TRANS)
4bafe00e 1661 return "trans";
ec668d69 1662 if (mask & HWCAP_FJFMAU)
4bafe00e 1663 return "fjfmau";
ec668d69 1664 if (mask & HWCAP_IMA)
4bafe00e 1665 return "ima";
ec668d69 1666 if (mask & HWCAP_ASI_CACHE_SPARING)
4bafe00e 1667 return "cspare";
ec668d69
DM
1668 if (mask & HWCAP_AES)
1669 return "aes";
1670 if (mask & HWCAP_DES)
1671 return "des";
1672 if (mask & HWCAP_KASUMI)
1673 return "kasumi";
1674 if (mask & HWCAP_CAMELLIA)
1675 return "camellia";
1676 if (mask & HWCAP_MD5)
1677 return "md5";
1678 if (mask & HWCAP_SHA1)
1679 return "sha1";
1680 if (mask & HWCAP_SHA256)
1681 return "sha256";
1682 if (mask & HWCAP_SHA512)
1683 return "sha512";
1684 if (mask & HWCAP_MPMUL)
1685 return "mpmul";
1686 if (mask & HWCAP_MONT)
1687 return "mont";
1688 if (mask & HWCAP_PAUSE)
1689 return "pause";
1690 if (mask & HWCAP_CBCOND)
1691 return "cbcond";
1692 if (mask & HWCAP_CRC32C)
1693 return "crc32c";
3d68f91c
JM
1694
1695 mask = mask >> 32;
1696 if (mask & HWCAP2_FJATHPLUS)
1697 return "fjathplus";
1698 if (mask & HWCAP2_VIS3B)
1699 return "vis3b";
1700 if (mask & HWCAP2_ADP)
1701 return "adp";
1702 if (mask & HWCAP2_SPARC5)
1703 return "sparc5";
1704 if (mask & HWCAP2_MWAIT)
1705 return "mwait";
1706 if (mask & HWCAP2_XMPMUL)
1707 return "xmpmul";
1708 if (mask & HWCAP2_XMONT)
1709 return "xmont";
1710 if (mask & HWCAP2_NSEC)
1711 return "nsec";
1712
4bafe00e
DM
1713 return "UNKNOWN";
1714}
1715
252b5132
RH
1716/* Subroutine of md_assemble to do the actual parsing. */
1717
a22b281c 1718static int
5a49b8ac 1719sparc_ip (char *str, const struct sparc_opcode **pinsn)
252b5132 1720{
e0471c16 1721 const char *error_message = "";
252b5132
RH
1722 char *s;
1723 const char *args;
1724 char c;
1725 const struct sparc_opcode *insn;
1726 char *argsStart;
1727 unsigned long opcode;
1728 unsigned int mask = 0;
1729 int match = 0;
1730 int comma = 0;
1731 int v9_arg_p;
a22b281c 1732 int special_case = SPECIAL_CASE_NONE;
252b5132
RH
1733
1734 s = str;
3882b010 1735 if (ISLOWER (*s))
252b5132
RH
1736 {
1737 do
1738 ++s;
6cda1326 1739 while (ISLOWER (*s) || ISDIGIT (*s) || *s == '_');
252b5132
RH
1740 }
1741
1742 switch (*s)
1743 {
1744 case '\0':
1745 break;
1746
1747 case ',':
1748 comma = 1;
e0c6ed95 1749 /* Fall through. */
252b5132
RH
1750
1751 case ' ':
1752 *s++ = '\0';
1753 break;
1754
1755 default:
b0825cc2
DM
1756 as_bad (_("Unknown opcode: `%s'"), str);
1757 *pinsn = NULL;
1758 return special_case;
252b5132
RH
1759 }
1760 insn = (struct sparc_opcode *) hash_find (op_hash, str);
1761 *pinsn = insn;
1762 if (insn == NULL)
1763 {
1764 as_bad (_("Unknown opcode: `%s'"), str);
a22b281c 1765 return special_case;
252b5132
RH
1766 }
1767 if (comma)
1768 {
1769 *--s = ',';
1770 }
1771
1772 argsStart = s;
1773 for (;;)
1774 {
1775 opcode = insn->match;
1776 memset (&the_insn, '\0', sizeof (the_insn));
1777 the_insn.reloc = BFD_RELOC_NONE;
1778 v9_arg_p = 0;
1779
e0c6ed95
AM
1780 /* Build the opcode, checking as we go to make sure that the
1781 operands match. */
252b5132
RH
1782 for (args = insn->args;; ++args)
1783 {
1784 switch (*args)
1785 {
1786 case 'K':
1787 {
1788 int kmask = 0;
1789
1790 /* Parse a series of masks. */
1791 if (*s == '#')
1792 {
1793 while (*s == '#')
1794 {
91d6fa6a 1795 int jmask;
252b5132
RH
1796
1797 if (! parse_keyword_arg (sparc_encode_membar, &s,
91d6fa6a 1798 &jmask))
252b5132
RH
1799 {
1800 error_message = _(": invalid membar mask name");
1801 goto error;
1802 }
91d6fa6a 1803 kmask |= jmask;
47926f60
KH
1804 while (*s == ' ')
1805 ++s;
252b5132
RH
1806 if (*s == '|' || *s == '+')
1807 ++s;
47926f60
KH
1808 while (*s == ' ')
1809 ++s;
252b5132
RH
1810 }
1811 }
1812 else
1813 {
1814 if (! parse_const_expr_arg (&s, &kmask))
1815 {
1816 error_message = _(": invalid membar mask expression");
1817 goto error;
1818 }
1819 if (kmask < 0 || kmask > 127)
1820 {
1821 error_message = _(": invalid membar mask number");
1822 goto error;
1823 }
1824 }
1825
1826 opcode |= MEMBAR (kmask);
1827 continue;
1828 }
1829
19f7b010
JJ
1830 case '3':
1831 {
1832 int smask = 0;
1833
1834 if (! parse_const_expr_arg (&s, &smask))
1835 {
1836 error_message = _(": invalid siam mode expression");
1837 goto error;
1838 }
1839 if (smask < 0 || smask > 7)
1840 {
1841 error_message = _(": invalid siam mode number");
1842 goto error;
1843 }
1844 opcode |= smask;
1845 continue;
1846 }
1847
252b5132
RH
1848 case '*':
1849 {
1850 int fcn = 0;
1851
1852 /* Parse a prefetch function. */
1853 if (*s == '#')
1854 {
1855 if (! parse_keyword_arg (sparc_encode_prefetch, &s, &fcn))
1856 {
1857 error_message = _(": invalid prefetch function name");
1858 goto error;
1859 }
1860 }
1861 else
1862 {
1863 if (! parse_const_expr_arg (&s, &fcn))
1864 {
1865 error_message = _(": invalid prefetch function expression");
1866 goto error;
1867 }
1868 if (fcn < 0 || fcn > 31)
1869 {
1870 error_message = _(": invalid prefetch function number");
1871 goto error;
1872 }
1873 }
1874 opcode |= RD (fcn);
1875 continue;
1876 }
1877
1878 case '!':
1879 case '?':
1880 /* Parse a sparc64 privileged register. */
1881 if (*s == '%')
1882 {
8b8c7c9f 1883 struct priv_reg_entry *p;
e0c6ed95 1884 unsigned int len = 9999999; /* Init to make gcc happy. */
252b5132
RH
1885
1886 s += 1;
8b8c7c9f
JM
1887 for (p = priv_reg_table; p->name; p++)
1888 if (p->name[0] == s[0])
1889 {
1890 len = strlen (p->name);
1891 if (strncmp (p->name, s, len) == 0)
1892 break;
1893 }
1894
1895 if (!p->name)
252b5132
RH
1896 {
1897 error_message = _(": unrecognizable privileged register");
1898 goto error;
1899 }
1900 if (*args == '?')
1901 opcode |= (p->regnum << 14);
1902 else
1903 opcode |= (p->regnum << 25);
1904 s += len;
1905 continue;
1906 }
1907 else
1908 {
1909 error_message = _(": unrecognizable privileged register");
1910 goto error;
1911 }
1912
10156f83
DM
1913 case '$':
1914 case '%':
1915 /* Parse a sparc64 hyperprivileged register. */
1916 if (*s == '%')
1917 {
8b8c7c9f 1918 struct priv_reg_entry *p;
10156f83
DM
1919 unsigned int len = 9999999; /* Init to make gcc happy. */
1920
1921 s += 1;
8b8c7c9f
JM
1922 for (p = hpriv_reg_table; p->name; p++)
1923 if (p->name[0] == s[0])
1924 {
1925 len = strlen (p->name);
1926 if (strncmp (p->name, s, len) == 0)
1927 break;
1928 }
1929
1930 if (!p->name)
10156f83
DM
1931 {
1932 error_message = _(": unrecognizable hyperprivileged register");
1933 goto error;
1934 }
1935 if (*args == '$')
1936 opcode |= (p->regnum << 14);
1937 else
1938 opcode |= (p->regnum << 25);
1939 s += len;
1940 continue;
1941 }
1942 else
1943 {
1944 error_message = _(": unrecognizable hyperprivileged register");
1945 goto error;
1946 }
1947
252b5132
RH
1948 case '_':
1949 case '/':
19f7b010 1950 /* Parse a v9a/v9b ancillary state register. */
252b5132
RH
1951 if (*s == '%')
1952 {
8b8c7c9f 1953 struct priv_reg_entry *p;
e0c6ed95 1954 unsigned int len = 9999999; /* Init to make gcc happy. */
252b5132
RH
1955
1956 s += 1;
8b8c7c9f
JM
1957 for (p = v9a_asr_table; p->name; p++)
1958 if (p->name[0] == s[0])
1959 {
1960 len = strlen (p->name);
1961 if (strncmp (p->name, s, len) == 0)
1962 break;
1963 }
1964
1965 if (!p->name)
252b5132 1966 {
19f7b010 1967 error_message = _(": unrecognizable v9a or v9b ancillary state register");
252b5132
RH
1968 goto error;
1969 }
1970 if (*args == '/' && (p->regnum == 20 || p->regnum == 21))
1971 {
1972 error_message = _(": rd on write only ancillary state register");
1973 goto error;
e0c6ed95 1974 }
19f7b010
JJ
1975 if (p->regnum >= 24
1976 && (insn->architecture
1977 & SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V9A)))
1978 {
1979 /* %sys_tick and %sys_tick_cmpr are v9bnotv9a */
1980 error_message = _(": unrecognizable v9a ancillary state register");
1981 goto error;
1982 }
252b5132
RH
1983 if (*args == '/')
1984 opcode |= (p->regnum << 14);
1985 else
1986 opcode |= (p->regnum << 25);
1987 s += len;
1988 continue;
1989 }
1990 else
1991 {
19f7b010 1992 error_message = _(": unrecognizable v9a or v9b ancillary state register");
252b5132
RH
1993 goto error;
1994 }
1995
1996 case 'M':
1997 case 'm':
1998 if (strncmp (s, "%asr", 4) == 0)
1999 {
2000 s += 4;
2001
3882b010 2002 if (ISDIGIT (*s))
252b5132
RH
2003 {
2004 long num = 0;
2005
3882b010 2006 while (ISDIGIT (*s))
252b5132
RH
2007 {
2008 num = num * 10 + *s - '0';
2009 ++s;
2010 }
2011
f65c3d1b
JM
2012 /* We used to check here for the asr number to
2013 be between 16 and 31 in V9 and later, as
2014 mandated by the section C.1.1 "Register
2015 Names" in the SPARC spec. However, we
2016 decided to remove this restriction as a) it
2017 introduces problems when new V9 asr registers
2018 are introduced, b) the Solaris assembler
2019 doesn't implement this restriction and c) the
2020 restriction will go away in future revisions
2021 of the Oracle SPARC Architecture. */
2022
2023 if (num < 0 || 31 < num)
2024 {
2025 error_message = _(": asr number must be between 0 and 31");
2026 goto error;
2027 }
252b5132
RH
2028
2029 opcode |= (*args == 'M' ? RS1 (num) : RD (num));
2030 continue;
2031 }
2032 else
2033 {
2034 error_message = _(": expecting %asrN");
2035 goto error;
2036 }
e0c6ed95 2037 } /* if %asr */
252b5132
RH
2038 break;
2039
2040 case 'I':
2041 the_insn.reloc = BFD_RELOC_SPARC_11;
2042 goto immediate;
2043
2044 case 'j':
2045 the_insn.reloc = BFD_RELOC_SPARC_10;
2046 goto immediate;
2047
6cda1326
DM
2048 case ')':
2049 if (*s == ' ')
2050 s++;
2051 if ((s[0] == '0' && s[1] == 'x' && ISXDIGIT (s[2]))
2052 || ISDIGIT (*s))
2053 {
2054 long num = 0;
2055
2056 if (s[0] == '0' && s[1] == 'x')
2057 {
2058 s += 2;
2059 while (ISXDIGIT (*s))
2060 {
2061 num <<= 4;
2062 num |= hex_value (*s);
2063 ++s;
2064 }
2065 }
2066 else
2067 {
2068 while (ISDIGIT (*s))
2069 {
2070 num = num * 10 + *s - '0';
2071 ++s;
2072 }
2073 }
2074 if (num < 0 || num > 31)
2075 {
2076 error_message = _(": crypto immediate must be between 0 and 31");
2077 goto error;
2078 }
2079
2080 opcode |= RS3 (num);
2081 continue;
2082 }
2083 else
2084 {
2085 error_message = _(": expecting crypto immediate");
2086 goto error;
2087 }
2088
252b5132
RH
2089 case 'X':
2090 /* V8 systems don't understand BFD_RELOC_SPARC_5. */
2091 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
2092 the_insn.reloc = BFD_RELOC_SPARC_5;
2093 else
2094 the_insn.reloc = BFD_RELOC_SPARC13;
2095 /* These fields are unsigned, but for upward compatibility,
2096 allow negative values as well. */
2097 goto immediate;
2098
2099 case 'Y':
2100 /* V8 systems don't understand BFD_RELOC_SPARC_6. */
2101 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
2102 the_insn.reloc = BFD_RELOC_SPARC_6;
2103 else
2104 the_insn.reloc = BFD_RELOC_SPARC13;
2105 /* These fields are unsigned, but for upward compatibility,
2106 allow negative values as well. */
2107 goto immediate;
2108
2109 case 'k':
2110 the_insn.reloc = /* RELOC_WDISP2_14 */ BFD_RELOC_SPARC_WDISP16;
2111 the_insn.pcrel = 1;
2112 goto immediate;
2113
2615994e
DM
2114 case '=':
2115 the_insn.reloc = /* RELOC_WDISP2_8 */ BFD_RELOC_SPARC_WDISP10;
2116 the_insn.pcrel = 1;
2117 goto immediate;
2118
252b5132
RH
2119 case 'G':
2120 the_insn.reloc = BFD_RELOC_SPARC_WDISP19;
2121 the_insn.pcrel = 1;
2122 goto immediate;
2123
2124 case 'N':
2125 if (*s == 'p' && s[1] == 'n')
2126 {
2127 s += 2;
2128 continue;
2129 }
2130 break;
2131
2132 case 'T':
2133 if (*s == 'p' && s[1] == 't')
2134 {
2135 s += 2;
2136 continue;
2137 }
2138 break;
2139
2140 case 'z':
2141 if (*s == ' ')
2142 {
2143 ++s;
2144 }
f9911beb
JM
2145 if ((strncmp (s, "%icc", 4) == 0)
2146 || (sparc_arch_size == 32 && strncmp (s, "%ncc", 4) == 0))
252b5132
RH
2147 {
2148 s += 4;
2149 continue;
2150 }
2151 break;
2152
2153 case 'Z':
2154 if (*s == ' ')
2155 {
2156 ++s;
2157 }
f9911beb
JM
2158 if ((strncmp (s, "%xcc", 4) == 0)
2159 || (sparc_arch_size == 64 && strncmp (s, "%ncc", 4) == 0))
252b5132
RH
2160 {
2161 s += 4;
2162 continue;
2163 }
2164 break;
2165
2166 case '6':
2167 if (*s == ' ')
2168 {
2169 ++s;
2170 }
2171 if (strncmp (s, "%fcc0", 5) == 0)
2172 {
2173 s += 5;
2174 continue;
2175 }
2176 break;
2177
2178 case '7':
2179 if (*s == ' ')
2180 {
2181 ++s;
2182 }
2183 if (strncmp (s, "%fcc1", 5) == 0)
2184 {
2185 s += 5;
2186 continue;
2187 }
2188 break;
2189
2190 case '8':
2191 if (*s == ' ')
2192 {
2193 ++s;
2194 }
2195 if (strncmp (s, "%fcc2", 5) == 0)
2196 {
2197 s += 5;
2198 continue;
2199 }
2200 break;
2201
2202 case '9':
2203 if (*s == ' ')
2204 {
2205 ++s;
2206 }
2207 if (strncmp (s, "%fcc3", 5) == 0)
2208 {
2209 s += 5;
2210 continue;
2211 }
2212 break;
2213
2214 case 'P':
2215 if (strncmp (s, "%pc", 3) == 0)
2216 {
2217 s += 3;
2218 continue;
2219 }
2220 break;
2221
2222 case 'W':
2223 if (strncmp (s, "%tick", 5) == 0)
2224 {
2225 s += 5;
2226 continue;
2227 }
2228 break;
2229
e0c6ed95 2230 case '\0': /* End of args. */
b9734f35 2231 if (s[0] == ',' && s[1] == '%')
252b5132 2232 {
b9734f35
JJ
2233 char *s1;
2234 int npar = 0;
8b8c7c9f 2235 const struct perc_entry *p;
b9734f35 2236
8b8c7c9f
JM
2237 for (p = perc_table; p->type != perc_entry_none; p++)
2238 if ((p->type == perc_entry_post_pop || p->type == perc_entry_reg)
2239 && strncmp (s + 2, p->name, p->len) == 0)
2240 break;
2241 if (p->type == perc_entry_none || p->type == perc_entry_reg)
2242 break;
b9734f35 2243
8b8c7c9f 2244 if (s[p->len + 2] != '(')
b9734f35 2245 {
8b8c7c9f 2246 as_bad (_("Illegal operands: %%%s requires arguments in ()"), p->name);
b9734f35
JJ
2247 return special_case;
2248 }
2249
8b8c7c9f
JM
2250 if (! (p->pop->flags & F_POP_TLS_CALL)
2251 && the_insn.reloc != BFD_RELOC_NONE)
b9734f35
JJ
2252 {
2253 as_bad (_("Illegal operands: %%%s cannot be used together with other relocs in the insn ()"),
8b8c7c9f 2254 p->name);
b9734f35
JJ
2255 return special_case;
2256 }
2257
8b8c7c9f 2258 if ((p->pop->flags & F_POP_TLS_CALL)
b9734f35
JJ
2259 && (the_insn.reloc != BFD_RELOC_32_PCREL_S2
2260 || the_insn.exp.X_add_number != 0
2261 || the_insn.exp.X_add_symbol
2262 != symbol_find_or_make ("__tls_get_addr")))
2263 {
2264 as_bad (_("Illegal operands: %%%s can be only used with call __tls_get_addr"),
8b8c7c9f 2265 p->name);
b9734f35
JJ
2266 return special_case;
2267 }
2268
8b8c7c9f 2269 the_insn.reloc = p->pop->reloc;
b9734f35 2270 memset (&the_insn.exp, 0, sizeof (the_insn.exp));
8b8c7c9f 2271 s += p->len + 3;
b9734f35
JJ
2272
2273 for (s1 = s; *s1 && *s1 != ',' && *s1 != ']'; s1++)
2274 if (*s1 == '(')
2275 npar++;
2276 else if (*s1 == ')')
2277 {
2278 if (!npar)
2279 break;
2280 npar--;
2281 }
2282
2283 if (*s1 != ')')
2284 {
8b8c7c9f 2285 as_bad (_("Illegal operands: %%%s requires arguments in ()"), p->name);
b9734f35
JJ
2286 return special_case;
2287 }
2288
2289 *s1 = '\0';
2290 (void) get_expression (s);
2291 *s1 = ')';
2292 s = s1 + 1;
252b5132 2293 }
b9734f35
JJ
2294 if (*s == '\0')
2295 match = 1;
252b5132
RH
2296 break;
2297
2298 case '+':
2299 if (*s == '+')
2300 {
2301 ++s;
2302 continue;
2303 }
2304 if (*s == '-')
2305 {
2306 continue;
2307 }
2308 break;
2309
e0c6ed95 2310 case '[': /* These must match exactly. */
252b5132
RH
2311 case ']':
2312 case ',':
2313 case ' ':
2314 if (*s++ == *args)
2315 continue;
2316 break;
2317
e0c6ed95 2318 case '#': /* Must be at least one digit. */
3882b010 2319 if (ISDIGIT (*s++))
252b5132 2320 {
3882b010 2321 while (ISDIGIT (*s))
252b5132
RH
2322 {
2323 ++s;
2324 }
2325 continue;
2326 }
2327 break;
2328
e0c6ed95 2329 case 'C': /* Coprocessor state register. */
252b5132
RH
2330 if (strncmp (s, "%csr", 4) == 0)
2331 {
2332 s += 4;
2333 continue;
2334 }
2335 break;
2336
e0c6ed95 2337 case 'b': /* Next operand is a coprocessor register. */
252b5132
RH
2338 case 'c':
2339 case 'D':
3882b010 2340 if (*s++ == '%' && *s++ == 'c' && ISDIGIT (*s))
252b5132
RH
2341 {
2342 mask = *s++;
3882b010 2343 if (ISDIGIT (*s))
252b5132
RH
2344 {
2345 mask = 10 * (mask - '0') + (*s++ - '0');
2346 if (mask >= 32)
2347 {
2348 break;
2349 }
2350 }
2351 else
2352 {
2353 mask -= '0';
2354 }
2355 switch (*args)
2356 {
2357
2358 case 'b':
2359 opcode |= mask << 14;
2360 continue;
2361
2362 case 'c':
2363 opcode |= mask;
2364 continue;
2365
2366 case 'D':
2367 opcode |= mask << 25;
2368 continue;
2369 }
2370 }
2371 break;
2372
2373 case 'r': /* next operand must be a register */
2374 case 'O':
2375 case '1':
2376 case '2':
2377 case 'd':
2378 if (*s++ == '%')
2379 {
2380 switch (c = *s++)
2381 {
2382
2383 case 'f': /* frame pointer */
2384 if (*s++ == 'p')
2385 {
2386 mask = 0x1e;
2387 break;
2388 }
2389 goto error;
2390
2391 case 'g': /* global register */
a22b281c
RH
2392 c = *s++;
2393 if (isoctal (c))
252b5132
RH
2394 {
2395 mask = c - '0';
2396 break;
2397 }
2398 goto error;
2399
2400 case 'i': /* in register */
a22b281c
RH
2401 c = *s++;
2402 if (isoctal (c))
252b5132
RH
2403 {
2404 mask = c - '0' + 24;
2405 break;
2406 }
2407 goto error;
2408
2409 case 'l': /* local register */
a22b281c
RH
2410 c = *s++;
2411 if (isoctal (c))
252b5132
RH
2412 {
2413 mask = (c - '0' + 16);
2414 break;
2415 }
2416 goto error;
2417
2418 case 'o': /* out register */
a22b281c
RH
2419 c = *s++;
2420 if (isoctal (c))
252b5132
RH
2421 {
2422 mask = (c - '0' + 8);
2423 break;
2424 }
2425 goto error;
2426
2427 case 's': /* stack pointer */
2428 if (*s++ == 'p')
2429 {
2430 mask = 0xe;
2431 break;
2432 }
2433 goto error;
2434
2435 case 'r': /* any register */
3882b010 2436 if (!ISDIGIT ((c = *s++)))
252b5132
RH
2437 {
2438 goto error;
2439 }
2440 /* FALLTHROUGH */
2441 case '0':
2442 case '1':
2443 case '2':
2444 case '3':
2445 case '4':
2446 case '5':
2447 case '6':
2448 case '7':
2449 case '8':
2450 case '9':
3882b010 2451 if (ISDIGIT (*s))
252b5132
RH
2452 {
2453 if ((c = 10 * (c - '0') + (*s++ - '0')) >= 32)
2454 {
2455 goto error;
2456 }
2457 }
2458 else
2459 {
2460 c -= '0';
2461 }
2462 mask = c;
2463 break;
2464
2465 default:
2466 goto error;
2467 }
2468
6d8809aa 2469 if ((mask & ~1) == 2 && sparc_arch_size == 64
e0c6ed95 2470 && no_undeclared_regs && ! globals[mask])
79bd78be 2471 as_bad (_("detected global register use not covered by .register pseudo-op"));
6d8809aa 2472
252b5132
RH
2473 /* Got the register, now figure out where
2474 it goes in the opcode. */
2475 switch (*args)
2476 {
2477 case '1':
2478 opcode |= mask << 14;
2479 continue;
2480
2481 case '2':
2482 opcode |= mask;
2483 continue;
2484
2485 case 'd':
2486 opcode |= mask << 25;
2487 continue;
2488
2489 case 'r':
2490 opcode |= (mask << 25) | (mask << 14);
2491 continue;
2492
2493 case 'O':
2494 opcode |= (mask << 25) | (mask << 0);
2495 continue;
2496 }
2497 }
2498 break;
2499
2500 case 'e': /* next operand is a floating point register */
2501 case 'v':
2502 case 'V':
2503
2504 case 'f':
2505 case 'B':
2506 case 'R':
2507
ea783ef3
DM
2508 case '4':
2509 case '5':
2510
252b5132
RH
2511 case 'g':
2512 case 'H':
2513 case 'J':
3d68f91c 2514 case '}':
252b5132
RH
2515 {
2516 char format;
2517
2518 if (*s++ == '%'
ec892a07
JM
2519 && ((format = *s) == 'f'
2520 || format == 'd'
2521 || format == 'q')
3882b010 2522 && ISDIGIT (*++s))
252b5132 2523 {
3882b010 2524 for (mask = 0; ISDIGIT (*s); ++s)
252b5132
RH
2525 {
2526 mask = 10 * mask + (*s - '0');
2527 } /* read the number */
2528
2529 if ((*args == 'v'
2530 || *args == 'B'
ea783ef3 2531 || *args == '5'
ec892a07
JM
2532 || *args == 'H'
2533 || format == 'd')
252b5132
RH
2534 && (mask & 1))
2535 {
ec892a07 2536 /* register must be even numbered */
252b5132 2537 break;
ec892a07 2538 }
252b5132
RH
2539
2540 if ((*args == 'V'
2541 || *args == 'R'
ec892a07
JM
2542 || *args == 'J'
2543 || format == 'q')
252b5132
RH
2544 && (mask & 3))
2545 {
ec892a07 2546 /* register must be multiple of 4 */
252b5132 2547 break;
ec892a07 2548 }
252b5132
RH
2549
2550 if (mask >= 64)
2551 {
2552 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
2553 error_message = _(": There are only 64 f registers; [0-63]");
2554 else
2555 error_message = _(": There are only 32 f registers; [0-31]");
2556 goto error;
2557 } /* on error */
2558 else if (mask >= 32)
2559 {
2560 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
2561 {
26664553
JJ
2562 if (*args == 'e' || *args == 'f' || *args == 'g')
2563 {
2564 error_message
2565 = _(": There are only 32 single precision f registers; [0-31]");
2566 goto error;
2567 }
252b5132
RH
2568 v9_arg_p = 1;
2569 mask -= 31; /* wrap high bit */
2570 }
2571 else
2572 {
2573 error_message = _(": There are only 32 f registers; [0-31]");
2574 goto error;
2575 }
2576 }
2577 }
2578 else
2579 {
2580 break;
ab3e48dc 2581 } /* if not an 'f' register. */
252b5132 2582
3d68f91c
JM
2583 if (*args == '}' && mask != RS2 (opcode))
2584 {
2585 error_message
2586 = _(": Instruction requires frs2 and frsd must be the same register");
2587 goto error;
2588 }
2589
252b5132
RH
2590 switch (*args)
2591 {
2592 case 'v':
2593 case 'V':
2594 case 'e':
2595 opcode |= RS1 (mask);
2596 continue;
2597
252b5132
RH
2598 case 'f':
2599 case 'B':
2600 case 'R':
2601 opcode |= RS2 (mask);
2602 continue;
2603
ea783ef3
DM
2604 case '4':
2605 case '5':
2606 opcode |= RS3 (mask);
2607 continue;
2608
252b5132
RH
2609 case 'g':
2610 case 'H':
2611 case 'J':
3d68f91c 2612 case '}':
252b5132
RH
2613 opcode |= RD (mask);
2614 continue;
ab3e48dc 2615 } /* Pack it in. */
252b5132
RH
2616
2617 know (0);
2618 break;
ab3e48dc 2619 } /* float arg */
252b5132
RH
2620
2621 case 'F':
2622 if (strncmp (s, "%fsr", 4) == 0)
2623 {
2624 s += 4;
2625 continue;
2626 }
2627 break;
2628
ea783ef3
DM
2629 case '(':
2630 if (strncmp (s, "%efsr", 5) == 0)
2631 {
2632 s += 5;
2633 continue;
2634 }
2635 break;
2636
ab3e48dc
KH
2637 case '0': /* 64 bit immediate (set, setsw, setx insn) */
2638 the_insn.reloc = BFD_RELOC_NONE; /* reloc handled elsewhere */
252b5132
RH
2639 goto immediate;
2640
ab3e48dc 2641 case 'l': /* 22 bit PC relative immediate */
252b5132
RH
2642 the_insn.reloc = BFD_RELOC_SPARC_WDISP22;
2643 the_insn.pcrel = 1;
2644 goto immediate;
2645
ab3e48dc 2646 case 'L': /* 30 bit immediate */
252b5132
RH
2647 the_insn.reloc = BFD_RELOC_32_PCREL_S2;
2648 the_insn.pcrel = 1;
2649 goto immediate;
2650
63fab58c 2651 case 'h':
ab3e48dc 2652 case 'n': /* 22 bit immediate */
252b5132
RH
2653 the_insn.reloc = BFD_RELOC_SPARC22;
2654 goto immediate;
2655
ab3e48dc 2656 case 'i': /* 13 bit immediate */
252b5132
RH
2657 the_insn.reloc = BFD_RELOC_SPARC13;
2658
2659 /* fallthrough */
2660
2661 immediate:
2662 if (*s == ' ')
2663 s++;
2664
cf9a1301
RH
2665 {
2666 char *s1;
e0471c16 2667 const char *op_arg = NULL;
30eb9c17 2668 static expressionS op_exp;
cf9a1301
RH
2669 bfd_reloc_code_real_type old_reloc = the_insn.reloc;
2670
2671 /* Check for %hi, etc. */
2672 if (*s == '%')
2673 {
8b8c7c9f
JM
2674 const struct perc_entry *p;
2675
2676 for (p = perc_table; p->type != perc_entry_none; p++)
2677 if ((p->type == perc_entry_imm_pop || p->type == perc_entry_reg)
2678 && strncmp (s + 1, p->name, p->len) == 0)
2679 break;
2680 if (p->type == perc_entry_none || p->type == perc_entry_reg)
2681 break;
2682
2683 if (s[p->len + 1] != '(')
cf9a1301 2684 {
8b8c7c9f 2685 as_bad (_("Illegal operands: %%%s requires arguments in ()"), p->name);
a22b281c 2686 return special_case;
cf9a1301 2687 }
252b5132 2688
8b8c7c9f
JM
2689 op_arg = p->name;
2690 the_insn.reloc = p->pop->reloc;
2691 s += p->len + 2;
2692 v9_arg_p = p->pop->flags & F_POP_V9;
cf9a1301
RH
2693 }
2694
2695 /* Note that if the get_expression() fails, we will still
2696 have created U entries in the symbol table for the
2697 'symbols' in the input string. Try not to create U
2698 symbols for registers, etc. */
252b5132 2699
252b5132
RH
2700 /* This stuff checks to see if the expression ends in
2701 +%reg. If it does, it removes the register from
2702 the expression, and re-sets 's' to point to the
2703 right place. */
2704
cf9a1301
RH
2705 if (op_arg)
2706 {
2707 int npar = 0;
2708
2709 for (s1 = s; *s1 && *s1 != ',' && *s1 != ']'; s1++)
2710 if (*s1 == '(')
2711 npar++;
2712 else if (*s1 == ')')
2713 {
2714 if (!npar)
2715 break;
2716 npar--;
2717 }
2718
2719 if (*s1 != ')')
2720 {
2721 as_bad (_("Illegal operands: %%%s requires arguments in ()"), op_arg);
a22b281c 2722 return special_case;
cf9a1301 2723 }
e0c6ed95 2724
cf9a1301
RH
2725 *s1 = '\0';
2726 (void) get_expression (s);
2727 *s1 = ')';
e140100a
NC
2728 if (expr_end != s1)
2729 {
2730 as_bad (_("Expression inside %%%s could not be parsed"), op_arg);
2731 return special_case;
2732 }
cf9a1301
RH
2733 s = s1 + 1;
2734 if (*s == ',' || *s == ']' || !*s)
2735 continue;
2736 if (*s != '+' && *s != '-')
2737 {
2738 as_bad (_("Illegal operands: Can't do arithmetics other than + and - involving %%%s()"), op_arg);
a22b281c 2739 return special_case;
cf9a1301
RH
2740 }
2741 *s1 = '0';
2742 s = s1;
2743 op_exp = the_insn.exp;
e0c6ed95 2744 memset (&the_insn.exp, 0, sizeof (the_insn.exp));
cf9a1301 2745 }
252b5132 2746
e0c6ed95
AM
2747 for (s1 = s; *s1 && *s1 != ',' && *s1 != ']'; s1++)
2748 ;
252b5132 2749
3882b010 2750 if (s1 != s && ISDIGIT (s1[-1]))
252b5132
RH
2751 {
2752 if (s1[-2] == '%' && s1[-3] == '+')
cf9a1301 2753 s1 -= 3;
f124dd4f 2754 else if (strchr ("golir0123456789", s1[-2]) && s1[-3] == '%' && s1[-4] == '+')
cf9a1301 2755 s1 -= 4;
f124dd4f
DM
2756 else if (s1[-3] == 'r' && s1[-4] == '%' && s1[-5] == '+')
2757 s1 -= 5;
cf9a1301
RH
2758 else
2759 s1 = NULL;
2760 if (s1)
252b5132 2761 {
252b5132 2762 *s1 = '\0';
1eb7027c
RH
2763 if (op_arg && s1 == s + 1)
2764 the_insn.exp.X_op = O_absent;
2765 else
2766 (void) get_expression (s);
252b5132 2767 *s1 = '+';
cf9a1301
RH
2768 if (op_arg)
2769 *s = ')';
252b5132 2770 s = s1;
252b5132 2771 }
cf9a1301
RH
2772 }
2773 else
2774 s1 = NULL;
2775
2776 if (!s1)
2777 {
2778 (void) get_expression (s);
2779 if (op_arg)
2780 *s = ')';
2781 s = expr_end;
2782 }
2783
2784 if (op_arg)
2785 {
2786 the_insn.exp2 = the_insn.exp;
2787 the_insn.exp = op_exp;
2788 if (the_insn.exp2.X_op == O_absent)
2789 the_insn.exp2.X_op = O_illegal;
2790 else if (the_insn.exp.X_op == O_absent)
252b5132 2791 {
cf9a1301
RH
2792 the_insn.exp = the_insn.exp2;
2793 the_insn.exp2.X_op = O_illegal;
2794 }
2795 else if (the_insn.exp.X_op == O_constant)
2796 {
2797 valueT val = the_insn.exp.X_add_number;
2798 switch (the_insn.reloc)
2799 {
1b50c718
ILT
2800 default:
2801 break;
2802
cf9a1301
RH
2803 case BFD_RELOC_SPARC_HH22:
2804 val = BSR (val, 32);
e0c6ed95 2805 /* Fall through. */
cf9a1301
RH
2806
2807 case BFD_RELOC_SPARC_LM22:
2808 case BFD_RELOC_HI22:
2809 val = (val >> 10) & 0x3fffff;
2810 break;
2811
2812 case BFD_RELOC_SPARC_HM10:
2813 val = BSR (val, 32);
e0c6ed95 2814 /* Fall through. */
cf9a1301
RH
2815
2816 case BFD_RELOC_LO10:
2817 val &= 0x3ff;
2818 break;
2819
2615994e
DM
2820 case BFD_RELOC_SPARC_H34:
2821 val >>= 12;
2822 val &= 0x3fffff;
2823 break;
2824
cf9a1301
RH
2825 case BFD_RELOC_SPARC_H44:
2826 val >>= 22;
2827 val &= 0x3fffff;
2828 break;
2829
2830 case BFD_RELOC_SPARC_M44:
2831 val >>= 12;
2832 val &= 0x3ff;
2833 break;
2834
2835 case BFD_RELOC_SPARC_L44:
2836 val &= 0xfff;
2837 break;
2838
2839 case BFD_RELOC_SPARC_HIX22:
ab3e48dc 2840 val = ~val;
cf9a1301
RH
2841 val = (val >> 10) & 0x3fffff;
2842 break;
2843
2844 case BFD_RELOC_SPARC_LOX10:
2845 val = (val & 0x3ff) | 0x1c00;
2846 break;
2847 }
2848 the_insn.exp = the_insn.exp2;
2849 the_insn.exp.X_add_number += val;
2850 the_insn.exp2.X_op = O_illegal;
2851 the_insn.reloc = old_reloc;
2852 }
2853 else if (the_insn.exp2.X_op != O_constant)
2854 {
2855 as_bad (_("Illegal operands: Can't add non-constant expression to %%%s()"), op_arg);
a22b281c 2856 return special_case;
cf9a1301
RH
2857 }
2858 else
2859 {
dabe3bbc 2860 if (old_reloc != BFD_RELOC_SPARC13
cf9a1301
RH
2861 || the_insn.reloc != BFD_RELOC_LO10
2862 || sparc_arch_size != 64
2863 || sparc_pic_code)
2864 {
2865 as_bad (_("Illegal operands: Can't do arithmetics involving %%%s() of a relocatable symbol"), op_arg);
a22b281c 2866 return special_case;
cf9a1301
RH
2867 }
2868 the_insn.reloc = BFD_RELOC_SPARC_OLO10;
252b5132
RH
2869 }
2870 }
2871 }
252b5132
RH
2872 /* Check for constants that don't require emitting a reloc. */
2873 if (the_insn.exp.X_op == O_constant
2874 && the_insn.exp.X_add_symbol == 0
2875 && the_insn.exp.X_op_symbol == 0)
2876 {
2877 /* For pc-relative call instructions, we reject
2878 constants to get better code. */
2879 if (the_insn.pcrel
2880 && the_insn.reloc == BFD_RELOC_32_PCREL_S2
2881 && in_signed_range (the_insn.exp.X_add_number, 0x3fff))
2882 {
2883 error_message = _(": PC-relative operand can't be a constant");
2884 goto error;
2885 }
2886
b9734f35
JJ
2887 if (the_insn.reloc >= BFD_RELOC_SPARC_TLS_GD_HI22
2888 && the_insn.reloc <= BFD_RELOC_SPARC_TLS_TPOFF64)
2889 {
2890 error_message = _(": TLS operand can't be a constant");
2891 goto error;
2892 }
2893
55cf6793 2894 /* Constants that won't fit are checked in md_apply_fix
252b5132
RH
2895 and bfd_install_relocation.
2896 ??? It would be preferable to install the constants
2897 into the insn here and save having to create a fixS
2898 for each one. There already exists code to handle
55cf6793 2899 all the various cases (e.g. in md_apply_fix and
252b5132
RH
2900 bfd_install_relocation) so duplicating all that code
2901 here isn't right. */
698544e1
DM
2902
2903 /* This is a special case to handle cbcond instructions
2904 properly, which can need two relocations. The first
2905 one is for the 5-bit immediate field and the latter
2906 is going to be for the WDISP10 branch part. We
2907 handle the R_SPARC_5 immediate directly here so that
2908 we don't need to add support for multiple relocations
2909 in one instruction just yet. */
2910 if (the_insn.reloc == BFD_RELOC_SPARC_5)
2911 {
2912 valueT val = the_insn.exp.X_add_number;
2913
2914 if (! in_bitfield_range (val, 0x1f))
2915 {
2916 error_message = _(": Immediate value in cbcond is out of range.");
2917 goto error;
2918 }
2919 opcode |= val & 0x1f;
2920 the_insn.reloc = BFD_RELOC_NONE;
2921 }
252b5132
RH
2922 }
2923
2924 continue;
2925
2926 case 'a':
2927 if (*s++ == 'a')
2928 {
2929 opcode |= ANNUL;
2930 continue;
2931 }
2932 break;
2933
2934 case 'A':
2935 {
2936 int asi = 0;
2937
2938 /* Parse an asi. */
2939 if (*s == '#')
2940 {
2941 if (! parse_keyword_arg (sparc_encode_asi, &s, &asi))
2942 {
2943 error_message = _(": invalid ASI name");
2944 goto error;
2945 }
2946 }
2947 else
2948 {
2949 if (! parse_const_expr_arg (&s, &asi))
2950 {
2951 error_message = _(": invalid ASI expression");
2952 goto error;
2953 }
2954 if (asi < 0 || asi > 255)
2955 {
2956 error_message = _(": invalid ASI number");
2957 goto error;
2958 }
2959 }
2960 opcode |= ASI (asi);
2961 continue;
e0c6ed95 2962 } /* Alternate space. */
252b5132
RH
2963
2964 case 'p':
2965 if (strncmp (s, "%psr", 4) == 0)
2966 {
2967 s += 4;
2968 continue;
2969 }
2970 break;
2971
e0c6ed95 2972 case 'q': /* Floating point queue. */
252b5132
RH
2973 if (strncmp (s, "%fq", 3) == 0)
2974 {
2975 s += 3;
2976 continue;
2977 }
2978 break;
2979
e0c6ed95 2980 case 'Q': /* Coprocessor queue. */
252b5132
RH
2981 if (strncmp (s, "%cq", 3) == 0)
2982 {
2983 s += 3;
2984 continue;
2985 }
2986 break;
2987
2988 case 'S':
2989 if (strcmp (str, "set") == 0
2990 || strcmp (str, "setuw") == 0)
2991 {
2992 special_case = SPECIAL_CASE_SET;
2993 continue;
2994 }
2995 else if (strcmp (str, "setsw") == 0)
2996 {
2997 special_case = SPECIAL_CASE_SETSW;
2998 continue;
2999 }
3000 else if (strcmp (str, "setx") == 0)
3001 {
3002 special_case = SPECIAL_CASE_SETX;
3003 continue;
3004 }
3005 else if (strncmp (str, "fdiv", 4) == 0)
3006 {
3007 special_case = SPECIAL_CASE_FDIV;
3008 continue;
3009 }
3010 break;
3011
3012 case 'o':
3013 if (strncmp (s, "%asi", 4) != 0)
3014 break;
3015 s += 4;
3016 continue;
3017
3018 case 's':
3019 if (strncmp (s, "%fprs", 5) != 0)
3020 break;
3021 s += 5;
3022 continue;
3023
3d68f91c
JM
3024 case '{':
3025 if (strncmp (s, "%mcdper",7) != 0)
3026 break;
3027 s += 7;
3028 continue;
3029
252b5132
RH
3030 case 'E':
3031 if (strncmp (s, "%ccr", 4) != 0)
3032 break;
3033 s += 4;
3034 continue;
3035
3036 case 't':
3037 if (strncmp (s, "%tbr", 4) != 0)
3038 break;
3039 s += 4;
3040 continue;
3041
3042 case 'w':
3043 if (strncmp (s, "%wim", 4) != 0)
3044 break;
3045 s += 4;
3046 continue;
3047
3048 case 'x':
3049 {
3050 char *push = input_line_pointer;
3051 expressionS e;
3052
3053 input_line_pointer = s;
3054 expression (&e);
3055 if (e.X_op == O_constant)
3056 {
3057 int n = e.X_add_number;
3058 if (n != e.X_add_number || (n & ~0x1ff) != 0)
3059 as_bad (_("OPF immediate operand out of range (0-0x1ff)"));
3060 else
3061 opcode |= e.X_add_number << 5;
3062 }
3063 else
3064 as_bad (_("non-immediate OPF operand, ignored"));
3065 s = input_line_pointer;
3066 input_line_pointer = push;
3067 continue;
3068 }
3069
3070 case 'y':
3071 if (strncmp (s, "%y", 2) != 0)
3072 break;
3073 s += 2;
3074 continue;
3075
3076 case 'u':
3077 case 'U':
3078 {
3079 /* Parse a sparclet cpreg. */
3080 int cpreg;
3081 if (! parse_keyword_arg (sparc_encode_sparclet_cpreg, &s, &cpreg))
3082 {
3083 error_message = _(": invalid cpreg name");
3084 goto error;
3085 }
3086 opcode |= (*args == 'U' ? RS1 (cpreg) : RD (cpreg));
3087 continue;
3088 }
3089
3090 default:
3091 as_fatal (_("failed sanity check."));
e0c6ed95 3092 } /* switch on arg code. */
252b5132
RH
3093
3094 /* Break out of for() loop. */
3095 break;
e0c6ed95 3096 } /* For each arg that we expect. */
252b5132
RH
3097
3098 error:
3099 if (match == 0)
3100 {
e0c6ed95 3101 /* Args don't match. */
252b5132
RH
3102 if (&insn[1] - sparc_opcodes < sparc_num_opcodes
3103 && (insn->name == insn[1].name
3104 || !strcmp (insn->name, insn[1].name)))
3105 {
3106 ++insn;
3107 s = argsStart;
3108 continue;
3109 }
3110 else
3111 {
3112 as_bad (_("Illegal operands%s"), error_message);
a22b281c 3113 return special_case;
252b5132
RH
3114 }
3115 }
3116 else
3117 {
e0c6ed95 3118 /* We have a match. Now see if the architecture is OK. */
252b5132 3119 int needed_arch_mask = insn->architecture;
3d68f91c
JM
3120 bfd_uint64_t hwcaps
3121 = (((bfd_uint64_t) insn->hwcaps2) << 32) | insn->hwcaps;
252b5132 3122
4bafe00e 3123#if defined(OBJ_ELF) && !defined(TE_SOLARIS)
9e8c70f9
DM
3124 if (hwcaps)
3125 hwcap_seen |= hwcaps;
3126#endif
252b5132
RH
3127 if (v9_arg_p)
3128 {
19f7b010
JJ
3129 needed_arch_mask &=
3130 ~(SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V9) - 1);
3131 if (! needed_arch_mask)
3132 needed_arch_mask =
3133 SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V9);
252b5132
RH
3134 }
3135
e0c6ed95
AM
3136 if (needed_arch_mask
3137 & SPARC_OPCODE_SUPPORTED (current_architecture))
3138 /* OK. */
3139 ;
252b5132 3140 /* Can we bump up the architecture? */
e0c6ed95
AM
3141 else if (needed_arch_mask
3142 & SPARC_OPCODE_SUPPORTED (max_architecture))
252b5132
RH
3143 {
3144 enum sparc_opcode_arch_val needed_architecture =
3145 sparc_ffs (SPARC_OPCODE_SUPPORTED (max_architecture)
3146 & needed_arch_mask);
3147
9c2799c2 3148 gas_assert (needed_architecture <= SPARC_OPCODE_ARCH_MAX);
252b5132
RH
3149 if (warn_on_bump
3150 && needed_architecture > warn_after_architecture)
3151 {
3152 as_warn (_("architecture bumped from \"%s\" to \"%s\" on \"%s\""),
3153 sparc_opcode_archs[current_architecture].name,
3154 sparc_opcode_archs[needed_architecture].name,
3155 str);
3156 warn_after_architecture = needed_architecture;
3157 }
3158 current_architecture = needed_architecture;
75ac3a7f 3159 hwcap_allowed |= hwcaps;
252b5132
RH
3160 }
3161 /* Conflict. */
3162 /* ??? This seems to be a bit fragile. What if the next entry in
3163 the opcode table is the one we want and it is supported?
3164 It is possible to arrange the table today so that this can't
3165 happen but what about tomorrow? */
3166 else
3167 {
e0c6ed95 3168 int arch, printed_one_p = 0;
252b5132
RH
3169 char *p;
3170 char required_archs[SPARC_OPCODE_ARCH_MAX * 16];
3171
3172 /* Create a list of the architectures that support the insn. */
e0c6ed95 3173 needed_arch_mask &= ~SPARC_OPCODE_SUPPORTED (max_architecture);
252b5132
RH
3174 p = required_archs;
3175 arch = sparc_ffs (needed_arch_mask);
3176 while ((1 << arch) <= needed_arch_mask)
3177 {
3178 if ((1 << arch) & needed_arch_mask)
3179 {
3180 if (printed_one_p)
3181 *p++ = '|';
3182 strcpy (p, sparc_opcode_archs[arch].name);
3183 p += strlen (p);
3184 printed_one_p = 1;
3185 }
3186 ++arch;
3187 }
3188
3189 as_bad (_("Architecture mismatch on \"%s\"."), str);
3190 as_tsktsk (_(" (Requires %s; requested architecture is %s.)"),
3191 required_archs,
3192 sparc_opcode_archs[max_architecture].name);
a22b281c 3193 return special_case;
252b5132 3194 }
4bafe00e 3195
9aff4b7a 3196 /* Make sure the hwcaps used by the instruction are
4bafe00e
DM
3197 currently enabled. */
3198 if (hwcaps & ~hwcap_allowed)
3199 {
3200 const char *hwcap_name = get_hwcap_name(hwcaps & ~hwcap_allowed);
3201
3202 as_bad (_("Hardware capability \"%s\" not enabled for \"%s\"."),
3203 hwcap_name, str);
3204 return special_case;
3205 }
e0c6ed95 3206 } /* If no match. */
252b5132
RH
3207
3208 break;
e0c6ed95 3209 } /* Forever looking for a match. */
252b5132
RH
3210
3211 the_insn.opcode = opcode;
a22b281c 3212 return special_case;
252b5132
RH
3213}
3214
3215/* Parse an argument that can be expressed as a keyword.
3216 (eg: #StoreStore or %ccfr).
3217 The result is a boolean indicating success.
3218 If successful, INPUT_POINTER is updated. */
3219
3220static int
5a49b8ac
AM
3221parse_keyword_arg (int (*lookup_fn) (const char *),
3222 char **input_pointerP,
3223 int *valueP)
252b5132
RH
3224{
3225 int value;
3226 char c, *p, *q;
3227
3228 p = *input_pointerP;
3229 for (q = p + (*p == '#' || *p == '%');
3882b010 3230 ISALNUM (*q) || *q == '_';
252b5132
RH
3231 ++q)
3232 continue;
3233 c = *q;
3234 *q = 0;
3235 value = (*lookup_fn) (p);
3236 *q = c;
3237 if (value == -1)
3238 return 0;
3239 *valueP = value;
3240 *input_pointerP = q;
3241 return 1;
3242}
3243
3244/* Parse an argument that is a constant expression.
3245 The result is a boolean indicating success. */
3246
3247static int
5a49b8ac 3248parse_const_expr_arg (char **input_pointerP, int *valueP)
252b5132
RH
3249{
3250 char *save = input_line_pointer;
3251 expressionS exp;
3252
3253 input_line_pointer = *input_pointerP;
3254 /* The next expression may be something other than a constant
3255 (say if we're not processing the right variant of the insn).
3256 Don't call expression unless we're sure it will succeed as it will
3257 signal an error (which we want to defer until later). */
3258 /* FIXME: It might be better to define md_operand and have it recognize
3259 things like %asi, etc. but continuing that route through to the end
3260 is a lot of work. */
3261 if (*input_line_pointer == '%')
3262 {
3263 input_line_pointer = save;
3264 return 0;
3265 }
3266 expression (&exp);
3267 *input_pointerP = input_line_pointer;
3268 input_line_pointer = save;
3269 if (exp.X_op != O_constant)
3270 return 0;
3271 *valueP = exp.X_add_number;
3272 return 1;
3273}
3274
3275/* Subroutine of sparc_ip to parse an expression. */
3276
3277static int
5a49b8ac 3278get_expression (char *str)
252b5132
RH
3279{
3280 char *save_in;
3281 segT seg;
3282
3283 save_in = input_line_pointer;
3284 input_line_pointer = str;
3285 seg = expression (&the_insn.exp);
3286 if (seg != absolute_section
3287 && seg != text_section
3288 && seg != data_section
3289 && seg != bss_section
3290 && seg != undefined_section)
3291 {
3292 the_insn.error = _("bad segment");
3293 expr_end = input_line_pointer;
3294 input_line_pointer = save_in;
3295 return 1;
3296 }
3297 expr_end = input_line_pointer;
3298 input_line_pointer = save_in;
3299 return 0;
3300}
3301
3302/* Subroutine of md_assemble to output one insn. */
3303
3304static void
91d6fa6a 3305output_insn (const struct sparc_opcode *insn, struct sparc_it *theinsn)
252b5132
RH
3306{
3307 char *toP = frag_more (4);
3308
e0c6ed95 3309 /* Put out the opcode. */
252b5132 3310 if (INSN_BIG_ENDIAN)
91d6fa6a 3311 number_to_chars_bigendian (toP, (valueT) theinsn->opcode, 4);
252b5132 3312 else
91d6fa6a 3313 number_to_chars_littleendian (toP, (valueT) theinsn->opcode, 4);
252b5132 3314
e0c6ed95 3315 /* Put out the symbol-dependent stuff. */
91d6fa6a 3316 if (theinsn->reloc != BFD_RELOC_NONE)
252b5132 3317 {
e0c6ed95
AM
3318 fixS *fixP = fix_new_exp (frag_now, /* Which frag. */
3319 (toP - frag_now->fr_literal), /* Where. */
3320 4, /* Size. */
91d6fa6a
NC
3321 &theinsn->exp,
3322 theinsn->pcrel,
3323 theinsn->reloc);
252b5132 3324 /* Turn off overflow checking in fixup_segment. We'll do our
55cf6793 3325 own overflow checking in md_apply_fix. This is necessary because
252b5132
RH
3326 the insn size is 4 and fixup_segment will signal an overflow for
3327 large 8 byte quantities. */
3328 fixP->fx_no_overflow = 1;
91d6fa6a
NC
3329 if (theinsn->reloc == BFD_RELOC_SPARC_OLO10)
3330 fixP->tc_fix_data = theinsn->exp2.X_add_number;
252b5132
RH
3331 }
3332
3333 last_insn = insn;
91d6fa6a 3334 last_opcode = theinsn->opcode;
732d96b6
JJ
3335
3336#ifdef OBJ_ELF
3337 dwarf2_emit_insn (4);
3338#endif
252b5132
RH
3339}
3340\f
6d4af3c2 3341const char *
499ac353 3342md_atof (int type, char *litP, int *sizeP)
252b5132 3343{
499ac353 3344 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
3345}
3346
3347/* Write a value out to the object file, using the appropriate
3348 endianness. */
3349
3350void
5a49b8ac 3351md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
3352{
3353 if (target_big_endian)
3354 number_to_chars_bigendian (buf, val, n);
3355 else if (target_little_endian_data
3356 && ((n == 4 || n == 2) && ~now_seg->flags & SEC_ALLOC))
e0c6ed95
AM
3357 /* Output debug words, which are not in allocated sections, as big
3358 endian. */
252b5132
RH
3359 number_to_chars_bigendian (buf, val, n);
3360 else if (target_little_endian_data || ! target_big_endian)
3361 number_to_chars_littleendian (buf, val, n);
3362}
3363\f
3364/* Apply a fixS to the frags, now that we know the value it ought to
81d4177b 3365 hold. */
252b5132 3366
94f592af 3367void
5a49b8ac 3368md_apply_fix (fixS *fixP, valueT *valP, segT segment ATTRIBUTE_UNUSED)
252b5132
RH
3369{
3370 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
94f592af 3371 offsetT val = * (offsetT *) valP;
252b5132
RH
3372 long insn;
3373
9c2799c2 3374 gas_assert (fixP->fx_r_type < BFD_RELOC_UNUSED);
252b5132 3375
e0c6ed95 3376 fixP->fx_addnumber = val; /* Remember value for emit_reloc. */
252b5132
RH
3377
3378#ifdef OBJ_ELF
a161fe53 3379 /* SPARC ELF relocations don't use an addend in the data field. */
252b5132 3380 if (fixP->fx_addsy != NULL)
7c1d0959
L
3381 {
3382 switch (fixP->fx_r_type)
3383 {
3384 case BFD_RELOC_SPARC_TLS_GD_HI22:
3385 case BFD_RELOC_SPARC_TLS_GD_LO10:
3386 case BFD_RELOC_SPARC_TLS_GD_ADD:
3387 case BFD_RELOC_SPARC_TLS_GD_CALL:
3388 case BFD_RELOC_SPARC_TLS_LDM_HI22:
3389 case BFD_RELOC_SPARC_TLS_LDM_LO10:
3390 case BFD_RELOC_SPARC_TLS_LDM_ADD:
3391 case BFD_RELOC_SPARC_TLS_LDM_CALL:
3392 case BFD_RELOC_SPARC_TLS_LDO_HIX22:
3393 case BFD_RELOC_SPARC_TLS_LDO_LOX10:
3394 case BFD_RELOC_SPARC_TLS_LDO_ADD:
3395 case BFD_RELOC_SPARC_TLS_IE_HI22:
3396 case BFD_RELOC_SPARC_TLS_IE_LO10:
3397 case BFD_RELOC_SPARC_TLS_IE_LD:
3398 case BFD_RELOC_SPARC_TLS_IE_LDX:
3399 case BFD_RELOC_SPARC_TLS_IE_ADD:
3400 case BFD_RELOC_SPARC_TLS_LE_HIX22:
3401 case BFD_RELOC_SPARC_TLS_LE_LOX10:
3402 case BFD_RELOC_SPARC_TLS_DTPMOD32:
3403 case BFD_RELOC_SPARC_TLS_DTPMOD64:
3404 case BFD_RELOC_SPARC_TLS_DTPOFF32:
3405 case BFD_RELOC_SPARC_TLS_DTPOFF64:
3406 case BFD_RELOC_SPARC_TLS_TPOFF32:
3407 case BFD_RELOC_SPARC_TLS_TPOFF64:
3408 S_SET_THREAD_LOCAL (fixP->fx_addsy);
3409
3410 default:
3411 break;
3412 }
3413
3414 return;
3415 }
252b5132
RH
3416#endif
3417
3418 /* This is a hack. There should be a better way to
3419 handle this. Probably in terms of howto fields, once
3420 we can look at these fixups in terms of howtos. */
3421 if (fixP->fx_r_type == BFD_RELOC_32_PCREL_S2 && fixP->fx_addsy)
3422 val += fixP->fx_where + fixP->fx_frag->fr_address;
3423
3424#ifdef OBJ_AOUT
3425 /* FIXME: More ridiculous gas reloc hacking. If we are going to
3426 generate a reloc, then we just want to let the reloc addend set
3427 the value. We do not want to also stuff the addend into the
3428 object file. Including the addend in the object file works when
3429 doing a static link, because the linker will ignore the object
3430 file contents. However, the dynamic linker does not ignore the
3431 object file contents. */
3432 if (fixP->fx_addsy != NULL
3433 && fixP->fx_r_type != BFD_RELOC_32_PCREL_S2)
3434 val = 0;
3435
3436 /* When generating PIC code, we do not want an addend for a reloc
3437 against a local symbol. We adjust fx_addnumber to cancel out the
3438 value already included in val, and to also cancel out the
3439 adjustment which bfd_install_relocation will create. */
3440 if (sparc_pic_code
3441 && fixP->fx_r_type != BFD_RELOC_32_PCREL_S2
3442 && fixP->fx_addsy != NULL
3443 && ! S_IS_COMMON (fixP->fx_addsy)
49309057 3444 && symbol_section_p (fixP->fx_addsy))
252b5132
RH
3445 fixP->fx_addnumber -= 2 * S_GET_VALUE (fixP->fx_addsy);
3446
3447 /* When generating PIC code, we need to fiddle to get
3448 bfd_install_relocation to do the right thing for a PC relative
3449 reloc against a local symbol which we are going to keep. */
3450 if (sparc_pic_code
3451 && fixP->fx_r_type == BFD_RELOC_32_PCREL_S2
3452 && fixP->fx_addsy != NULL
3453 && (S_IS_EXTERNAL (fixP->fx_addsy)
3454 || S_IS_WEAK (fixP->fx_addsy))
3455 && S_IS_DEFINED (fixP->fx_addsy)
3456 && ! S_IS_COMMON (fixP->fx_addsy))
3457 {
3458 val = 0;
3459 fixP->fx_addnumber -= 2 * S_GET_VALUE (fixP->fx_addsy);
3460 }
3461#endif
3462
3463 /* If this is a data relocation, just output VAL. */
3464
a7bbf4e9
DM
3465 if (fixP->fx_r_type == BFD_RELOC_8)
3466 {
3467 md_number_to_chars (buf, val, 1);
3468 }
3469 else if (fixP->fx_r_type == BFD_RELOC_16
3470 || fixP->fx_r_type == BFD_RELOC_SPARC_UA16)
252b5132
RH
3471 {
3472 md_number_to_chars (buf, val, 2);
3473 }
3474 else if (fixP->fx_r_type == BFD_RELOC_32
0f2712ed 3475 || fixP->fx_r_type == BFD_RELOC_SPARC_UA32
252b5132
RH
3476 || fixP->fx_r_type == BFD_RELOC_SPARC_REV32)
3477 {
3478 md_number_to_chars (buf, val, 4);
3479 }
0f2712ed
NC
3480 else if (fixP->fx_r_type == BFD_RELOC_64
3481 || fixP->fx_r_type == BFD_RELOC_SPARC_UA64)
252b5132
RH
3482 {
3483 md_number_to_chars (buf, val, 8);
3484 }
e0c6ed95 3485 else if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
252b5132
RH
3486 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3487 {
3488 fixP->fx_done = 0;
94f592af 3489 return;
252b5132
RH
3490 }
3491 else
3492 {
3493 /* It's a relocation against an instruction. */
3494
3495 if (INSN_BIG_ENDIAN)
3496 insn = bfd_getb32 ((unsigned char *) buf);
3497 else
3498 insn = bfd_getl32 ((unsigned char *) buf);
e0c6ed95 3499
252b5132
RH
3500 switch (fixP->fx_r_type)
3501 {
3502 case BFD_RELOC_32_PCREL_S2:
3503 val = val >> 2;
3504 /* FIXME: This increment-by-one deserves a comment of why it's
3505 being done! */
3506 if (! sparc_pic_code
3507 || fixP->fx_addsy == NULL
49309057 3508 || symbol_section_p (fixP->fx_addsy))
252b5132 3509 ++val;
6faf3d66 3510
252b5132 3511 insn |= val & 0x3fffffff;
6faf3d66 3512
e0c6ed95 3513 /* See if we have a delay slot. */
6faf3d66
JJ
3514 if (sparc_relax && fixP->fx_where + 8 <= fixP->fx_frag->fr_fix)
3515 {
3516#define G0 0
3517#define O7 15
3518#define XCC (2 << 20)
3519#define COND(x) (((x)&0xf)<<25)
3520#define CONDA COND(0x8)
3521#define INSN_BPA (F2(0,1) | CONDA | BPRED | XCC)
3522#define INSN_BA (F2(0,2) | CONDA)
3523#define INSN_OR F3(2, 0x2, 0)
3524#define INSN_NOP F2(0,4)
3525
3526 long delay;
3527
3528 /* If the instruction is a call with either:
3529 restore
3530 arithmetic instruction with rd == %o7
3531 where rs1 != %o7 and rs2 if it is register != %o7
3532 then we can optimize if the call destination is near
3533 by changing the call into a branch always. */
3534 if (INSN_BIG_ENDIAN)
3535 delay = bfd_getb32 ((unsigned char *) buf + 4);
3536 else
3537 delay = bfd_getl32 ((unsigned char *) buf + 4);
e0c6ed95 3538 if ((insn & OP (~0)) != OP (1) || (delay & OP (~0)) != OP (2))
6faf3d66 3539 break;
e0c6ed95
AM
3540 if ((delay & OP3 (~0)) != OP3 (0x3d) /* Restore. */
3541 && ((delay & OP3 (0x28)) != 0 /* Arithmetic. */
3542 || ((delay & RD (~0)) != RD (O7))))
6faf3d66 3543 break;
e0c6ed95
AM
3544 if ((delay & RS1 (~0)) == RS1 (O7)
3545 || ((delay & F3I (~0)) == 0
3546 && (delay & RS2 (~0)) == RS2 (O7)))
6faf3d66
JJ
3547 break;
3548 /* Ensure the branch will fit into simm22. */
3549 if ((val & 0x3fe00000)
3550 && (val & 0x3fe00000) != 0x3fe00000)
3551 break;
3552 /* Check if the arch is v9 and branch will fit
3553 into simm19. */
3554 if (((val & 0x3c0000) == 0
3555 || (val & 0x3c0000) == 0x3c0000)
3556 && (sparc_arch_size == 64
3557 || current_architecture >= SPARC_OPCODE_ARCH_V9))
e0c6ed95 3558 /* ba,pt %xcc */
6faf3d66
JJ
3559 insn = INSN_BPA | (val & 0x7ffff);
3560 else
e0c6ed95 3561 /* ba */
6faf3d66
JJ
3562 insn = INSN_BA | (val & 0x3fffff);
3563 if (fixP->fx_where >= 4
e0c6ed95
AM
3564 && ((delay & (0xffffffff ^ RS1 (~0)))
3565 == (INSN_OR | RD (O7) | RS2 (G0))))
6faf3d66
JJ
3566 {
3567 long setter;
3568 int reg;
3569
3570 if (INSN_BIG_ENDIAN)
3571 setter = bfd_getb32 ((unsigned char *) buf - 4);
3572 else
3573 setter = bfd_getl32 ((unsigned char *) buf - 4);
e0c6ed95 3574 if ((setter & (0xffffffff ^ RD (~0)))
ab3e48dc 3575 != (INSN_OR | RS1 (O7) | RS2 (G0)))
6faf3d66
JJ
3576 break;
3577 /* The sequence was
3578 or %o7, %g0, %rN
3579 call foo
3580 or %rN, %g0, %o7
3581
3582 If call foo was replaced with ba, replace
3583 or %rN, %g0, %o7 with nop. */
e0c6ed95
AM
3584 reg = (delay & RS1 (~0)) >> 14;
3585 if (reg != ((setter & RD (~0)) >> 25)
6faf3d66
JJ
3586 || reg == G0 || reg == O7)
3587 break;
3588
3589 if (INSN_BIG_ENDIAN)
3590 bfd_putb32 (INSN_NOP, (unsigned char *) buf + 4);
3591 else
3592 bfd_putl32 (INSN_NOP, (unsigned char *) buf + 4);
3593 }
3594 }
252b5132
RH
3595 break;
3596
3597 case BFD_RELOC_SPARC_11:
3598 if (! in_signed_range (val, 0x7ff))
3599 as_bad_where (fixP->fx_file, fixP->fx_line,
3600 _("relocation overflow"));
3601 insn |= val & 0x7ff;
3602 break;
3603
3604 case BFD_RELOC_SPARC_10:
3605 if (! in_signed_range (val, 0x3ff))
3606 as_bad_where (fixP->fx_file, fixP->fx_line,
3607 _("relocation overflow"));
3608 insn |= val & 0x3ff;
3609 break;
3610
3611 case BFD_RELOC_SPARC_7:
3612 if (! in_bitfield_range (val, 0x7f))
3613 as_bad_where (fixP->fx_file, fixP->fx_line,
3614 _("relocation overflow"));
3615 insn |= val & 0x7f;
3616 break;
3617
3618 case BFD_RELOC_SPARC_6:
3619 if (! in_bitfield_range (val, 0x3f))
3620 as_bad_where (fixP->fx_file, fixP->fx_line,
3621 _("relocation overflow"));
3622 insn |= val & 0x3f;
3623 break;
3624
3625 case BFD_RELOC_SPARC_5:
3626 if (! in_bitfield_range (val, 0x1f))
3627 as_bad_where (fixP->fx_file, fixP->fx_line,
3628 _("relocation overflow"));
3629 insn |= val & 0x1f;
3630 break;
3631
2615994e
DM
3632 case BFD_RELOC_SPARC_WDISP10:
3633 if ((val & 3)
3634 || val >= 0x007fc
3635 || val <= -(offsetT) 0x808)
3636 as_bad_where (fixP->fx_file, fixP->fx_line,
3637 _("relocation overflow"));
3638 /* FIXME: The +1 deserves a comment. */
3639 val = (val >> 2) + 1;
3640 insn |= ((val & 0x300) << 11)
3641 | ((val & 0xff) << 5);
3642 break;
3643
252b5132 3644 case BFD_RELOC_SPARC_WDISP16:
c699f087
JJ
3645 if ((val & 3)
3646 || val >= 0x1fffc
3647 || val <= -(offsetT) 0x20008)
252b5132
RH
3648 as_bad_where (fixP->fx_file, fixP->fx_line,
3649 _("relocation overflow"));
3650 /* FIXME: The +1 deserves a comment. */
3651 val = (val >> 2) + 1;
3652 insn |= ((val & 0xc000) << 6) | (val & 0x3fff);
3653 break;
3654
3655 case BFD_RELOC_SPARC_WDISP19:
c699f087
JJ
3656 if ((val & 3)
3657 || val >= 0xffffc
3658 || val <= -(offsetT) 0x100008)
252b5132
RH
3659 as_bad_where (fixP->fx_file, fixP->fx_line,
3660 _("relocation overflow"));
3661 /* FIXME: The +1 deserves a comment. */
3662 val = (val >> 2) + 1;
3663 insn |= val & 0x7ffff;
3664 break;
3665
3666 case BFD_RELOC_SPARC_HH22:
3667 val = BSR (val, 32);
e0c6ed95 3668 /* Fall through. */
252b5132
RH
3669
3670 case BFD_RELOC_SPARC_LM22:
3671 case BFD_RELOC_HI22:
3672 if (!fixP->fx_addsy)
94f592af 3673 insn |= (val >> 10) & 0x3fffff;
252b5132 3674 else
94f592af
NC
3675 /* FIXME: Need comment explaining why we do this. */
3676 insn &= ~0xffff;
252b5132
RH
3677 break;
3678
3679 case BFD_RELOC_SPARC22:
3680 if (val & ~0x003fffff)
3681 as_bad_where (fixP->fx_file, fixP->fx_line,
3682 _("relocation overflow"));
3683 insn |= (val & 0x3fffff);
3684 break;
3685
3686 case BFD_RELOC_SPARC_HM10:
3687 val = BSR (val, 32);
e0c6ed95 3688 /* Fall through. */
252b5132
RH
3689
3690 case BFD_RELOC_LO10:
3691 if (!fixP->fx_addsy)
94f592af 3692 insn |= val & 0x3ff;
252b5132 3693 else
94f592af
NC
3694 /* FIXME: Need comment explaining why we do this. */
3695 insn &= ~0xff;
252b5132
RH
3696 break;
3697
dabe3bbc
RH
3698 case BFD_RELOC_SPARC_OLO10:
3699 val &= 0x3ff;
3700 val += fixP->tc_fix_data;
e0c6ed95 3701 /* Fall through. */
dabe3bbc 3702
252b5132
RH
3703 case BFD_RELOC_SPARC13:
3704 if (! in_signed_range (val, 0x1fff))
3705 as_bad_where (fixP->fx_file, fixP->fx_line,
3706 _("relocation overflow"));
3707 insn |= val & 0x1fff;
3708 break;
3709
3710 case BFD_RELOC_SPARC_WDISP22:
3711 val = (val >> 2) + 1;
e0c6ed95 3712 /* Fall through. */
252b5132
RH
3713 case BFD_RELOC_SPARC_BASE22:
3714 insn |= val & 0x3fffff;
3715 break;
3716
2615994e
DM
3717 case BFD_RELOC_SPARC_H34:
3718 if (!fixP->fx_addsy)
3719 {
3720 bfd_vma tval = val;
3721 tval >>= 12;
3722 insn |= tval & 0x3fffff;
3723 }
3724 break;
3725
252b5132
RH
3726 case BFD_RELOC_SPARC_H44:
3727 if (!fixP->fx_addsy)
3728 {
3729 bfd_vma tval = val;
3730 tval >>= 22;
3731 insn |= tval & 0x3fffff;
3732 }
3733 break;
3734
3735 case BFD_RELOC_SPARC_M44:
3736 if (!fixP->fx_addsy)
3737 insn |= (val >> 12) & 0x3ff;
3738 break;
3739
3740 case BFD_RELOC_SPARC_L44:
3741 if (!fixP->fx_addsy)
3742 insn |= val & 0xfff;
3743 break;
3744
3745 case BFD_RELOC_SPARC_HIX22:
3746 if (!fixP->fx_addsy)
3747 {
ab3e48dc 3748 val ^= ~(offsetT) 0;
252b5132
RH
3749 insn |= (val >> 10) & 0x3fffff;
3750 }
3751 break;
3752
3753 case BFD_RELOC_SPARC_LOX10:
3754 if (!fixP->fx_addsy)
3755 insn |= 0x1c00 | (val & 0x3ff);
3756 break;
3757
3758 case BFD_RELOC_NONE:
3759 default:
3760 as_bad_where (fixP->fx_file, fixP->fx_line,
3761 _("bad or unhandled relocation type: 0x%02x"),
3762 fixP->fx_r_type);
3763 break;
3764 }
3765
3766 if (INSN_BIG_ENDIAN)
3767 bfd_putb32 (insn, (unsigned char *) buf);
3768 else
3769 bfd_putl32 (insn, (unsigned char *) buf);
3770 }
3771
3772 /* Are we finished with this relocation now? */
3773 if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
3774 fixP->fx_done = 1;
252b5132
RH
3775}
3776
3777/* Translate internal representation of relocation info to BFD target
3778 format. */
e0c6ed95 3779
dabe3bbc 3780arelent **
5a49b8ac 3781tc_gen_reloc (asection *section, fixS *fixp)
252b5132 3782{
dabe3bbc 3783 static arelent *relocs[3];
252b5132
RH
3784 arelent *reloc;
3785 bfd_reloc_code_real_type code;
3786
add39d23 3787 relocs[0] = reloc = XNEW (arelent);
dabe3bbc 3788 relocs[1] = NULL;
252b5132 3789
add39d23 3790 reloc->sym_ptr_ptr = XNEW (asymbol *);
49309057 3791 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
3792 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
3793
3794 switch (fixp->fx_r_type)
3795 {
3796 case BFD_RELOC_16:
3797 case BFD_RELOC_32:
3798 case BFD_RELOC_HI22:
3799 case BFD_RELOC_LO10:
3800 case BFD_RELOC_32_PCREL_S2:
3801 case BFD_RELOC_SPARC13:
63fab58c 3802 case BFD_RELOC_SPARC22:
1a6b486f
DM
3803 case BFD_RELOC_SPARC_PC22:
3804 case BFD_RELOC_SPARC_PC10:
252b5132 3805 case BFD_RELOC_SPARC_BASE13:
2615994e 3806 case BFD_RELOC_SPARC_WDISP10:
252b5132
RH
3807 case BFD_RELOC_SPARC_WDISP16:
3808 case BFD_RELOC_SPARC_WDISP19:
3809 case BFD_RELOC_SPARC_WDISP22:
3810 case BFD_RELOC_64:
3811 case BFD_RELOC_SPARC_5:
3812 case BFD_RELOC_SPARC_6:
3813 case BFD_RELOC_SPARC_7:
3814 case BFD_RELOC_SPARC_10:
3815 case BFD_RELOC_SPARC_11:
3816 case BFD_RELOC_SPARC_HH22:
3817 case BFD_RELOC_SPARC_HM10:
3818 case BFD_RELOC_SPARC_LM22:
3819 case BFD_RELOC_SPARC_PC_HH22:
3820 case BFD_RELOC_SPARC_PC_HM10:
3821 case BFD_RELOC_SPARC_PC_LM22:
2615994e 3822 case BFD_RELOC_SPARC_H34:
252b5132
RH
3823 case BFD_RELOC_SPARC_H44:
3824 case BFD_RELOC_SPARC_M44:
3825 case BFD_RELOC_SPARC_L44:
3826 case BFD_RELOC_SPARC_HIX22:
3827 case BFD_RELOC_SPARC_LOX10:
3828 case BFD_RELOC_SPARC_REV32:
dabe3bbc 3829 case BFD_RELOC_SPARC_OLO10:
0f2712ed
NC
3830 case BFD_RELOC_SPARC_UA16:
3831 case BFD_RELOC_SPARC_UA32:
3832 case BFD_RELOC_SPARC_UA64:
bd5e6e7e
JJ
3833 case BFD_RELOC_8_PCREL:
3834 case BFD_RELOC_16_PCREL:
3835 case BFD_RELOC_32_PCREL:
3836 case BFD_RELOC_64_PCREL:
3837 case BFD_RELOC_SPARC_PLT32:
3838 case BFD_RELOC_SPARC_PLT64:
252b5132
RH
3839 case BFD_RELOC_VTABLE_ENTRY:
3840 case BFD_RELOC_VTABLE_INHERIT:
b9734f35
JJ
3841 case BFD_RELOC_SPARC_TLS_GD_HI22:
3842 case BFD_RELOC_SPARC_TLS_GD_LO10:
3843 case BFD_RELOC_SPARC_TLS_GD_ADD:
3844 case BFD_RELOC_SPARC_TLS_GD_CALL:
3845 case BFD_RELOC_SPARC_TLS_LDM_HI22:
3846 case BFD_RELOC_SPARC_TLS_LDM_LO10:
3847 case BFD_RELOC_SPARC_TLS_LDM_ADD:
3848 case BFD_RELOC_SPARC_TLS_LDM_CALL:
3849 case BFD_RELOC_SPARC_TLS_LDO_HIX22:
3850 case BFD_RELOC_SPARC_TLS_LDO_LOX10:
3851 case BFD_RELOC_SPARC_TLS_LDO_ADD:
3852 case BFD_RELOC_SPARC_TLS_IE_HI22:
3853 case BFD_RELOC_SPARC_TLS_IE_LO10:
3854 case BFD_RELOC_SPARC_TLS_IE_LD:
3855 case BFD_RELOC_SPARC_TLS_IE_LDX:
3856 case BFD_RELOC_SPARC_TLS_IE_ADD:
3857 case BFD_RELOC_SPARC_TLS_LE_HIX22:
3858 case BFD_RELOC_SPARC_TLS_LE_LOX10:
3859 case BFD_RELOC_SPARC_TLS_DTPOFF32:
3860 case BFD_RELOC_SPARC_TLS_DTPOFF64:
739f7f82
DM
3861 case BFD_RELOC_SPARC_GOTDATA_OP_HIX22:
3862 case BFD_RELOC_SPARC_GOTDATA_OP_LOX10:
3863 case BFD_RELOC_SPARC_GOTDATA_OP:
252b5132
RH
3864 code = fixp->fx_r_type;
3865 break;
3866 default:
3867 abort ();
3868 return NULL;
3869 }
3870
3871#if defined (OBJ_ELF) || defined (OBJ_AOUT)
3872 /* If we are generating PIC code, we need to generate a different
3873 set of relocs. */
3874
3875#ifdef OBJ_ELF
3876#define GOT_NAME "_GLOBAL_OFFSET_TABLE_"
3877#else
3878#define GOT_NAME "__GLOBAL_OFFSET_TABLE_"
910600e9
RS
3879#endif
3880#ifdef TE_VXWORKS
3881#define GOTT_BASE "__GOTT_BASE__"
3882#define GOTT_INDEX "__GOTT_INDEX__"
252b5132
RH
3883#endif
3884
153b546a
ILT
3885 /* This code must be parallel to the OBJ_ELF tc_fix_adjustable. */
3886
252b5132
RH
3887 if (sparc_pic_code)
3888 {
3889 switch (code)
3890 {
3891 case BFD_RELOC_32_PCREL_S2:
ae6063d4 3892 if (generic_force_reloc (fixp))
252b5132
RH
3893 code = BFD_RELOC_SPARC_WPLT30;
3894 break;
3895 case BFD_RELOC_HI22:
910600e9
RS
3896 code = BFD_RELOC_SPARC_GOT22;
3897 if (fixp->fx_addsy != NULL)
3898 {
3899 if (strcmp (S_GET_NAME (fixp->fx_addsy), GOT_NAME) == 0)
3900 code = BFD_RELOC_SPARC_PC22;
3901#ifdef TE_VXWORKS
3902 if (strcmp (S_GET_NAME (fixp->fx_addsy), GOTT_BASE) == 0
3903 || strcmp (S_GET_NAME (fixp->fx_addsy), GOTT_INDEX) == 0)
3904 code = BFD_RELOC_HI22; /* Unchanged. */
3905#endif
3906 }
252b5132
RH
3907 break;
3908 case BFD_RELOC_LO10:
910600e9
RS
3909 code = BFD_RELOC_SPARC_GOT10;
3910 if (fixp->fx_addsy != NULL)
3911 {
3912 if (strcmp (S_GET_NAME (fixp->fx_addsy), GOT_NAME) == 0)
3913 code = BFD_RELOC_SPARC_PC10;
3914#ifdef TE_VXWORKS
3915 if (strcmp (S_GET_NAME (fixp->fx_addsy), GOTT_BASE) == 0
3916 || strcmp (S_GET_NAME (fixp->fx_addsy), GOTT_INDEX) == 0)
3917 code = BFD_RELOC_LO10; /* Unchanged. */
3918#endif
3919 }
252b5132
RH
3920 break;
3921 case BFD_RELOC_SPARC13:
3922 code = BFD_RELOC_SPARC_GOT13;
3923 break;
3924 default:
3925 break;
3926 }
3927 }
e0c6ed95 3928#endif /* defined (OBJ_ELF) || defined (OBJ_AOUT) */
252b5132 3929
062cf837
EB
3930 /* Nothing is aligned in DWARF debugging sections. */
3931 if (bfd_get_section_flags (stdoutput, section) & SEC_DEBUGGING)
3932 switch (code)
3933 {
3934 case BFD_RELOC_16: code = BFD_RELOC_SPARC_UA16; break;
3935 case BFD_RELOC_32: code = BFD_RELOC_SPARC_UA32; break;
3936 case BFD_RELOC_64: code = BFD_RELOC_SPARC_UA64; break;
3937 default: break;
3938 }
3939
dabe3bbc
RH
3940 if (code == BFD_RELOC_SPARC_OLO10)
3941 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO10);
3942 else
3943 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
3944 if (reloc->howto == 0)
3945 {
3946 as_bad_where (fixp->fx_file, fixp->fx_line,
3947 _("internal error: can't export reloc type %d (`%s')"),
3948 fixp->fx_r_type, bfd_get_reloc_code_name (code));
dabe3bbc
RH
3949 xfree (reloc);
3950 relocs[0] = NULL;
3951 return relocs;
252b5132
RH
3952 }
3953
3954 /* @@ Why fx_addnumber sometimes and fx_offset other times? */
3955#ifdef OBJ_AOUT
3956
3957 if (reloc->howto->pc_relative == 0
3958 || code == BFD_RELOC_SPARC_PC10
3959 || code == BFD_RELOC_SPARC_PC22)
3960 reloc->addend = fixp->fx_addnumber;
3961 else if (sparc_pic_code
3962 && fixp->fx_r_type == BFD_RELOC_32_PCREL_S2
3963 && fixp->fx_addsy != NULL
3964 && (S_IS_EXTERNAL (fixp->fx_addsy)
3965 || S_IS_WEAK (fixp->fx_addsy))
3966 && S_IS_DEFINED (fixp->fx_addsy)
3967 && ! S_IS_COMMON (fixp->fx_addsy))
3968 reloc->addend = fixp->fx_addnumber;
3969 else
3970 reloc->addend = fixp->fx_offset - reloc->address;
3971
e0c6ed95 3972#else /* elf or coff */
252b5132 3973
bd5e6e7e
JJ
3974 if (code != BFD_RELOC_32_PCREL_S2
3975 && code != BFD_RELOC_SPARC_WDISP22
3976 && code != BFD_RELOC_SPARC_WDISP16
3977 && code != BFD_RELOC_SPARC_WDISP19
2615994e 3978 && code != BFD_RELOC_SPARC_WDISP10
b9734f35
JJ
3979 && code != BFD_RELOC_SPARC_WPLT30
3980 && code != BFD_RELOC_SPARC_TLS_GD_CALL
3981 && code != BFD_RELOC_SPARC_TLS_LDM_CALL)
252b5132 3982 reloc->addend = fixp->fx_addnumber;
49309057 3983 else if (symbol_section_p (fixp->fx_addsy))
252b5132
RH
3984 reloc->addend = (section->vma
3985 + fixp->fx_addnumber
3986 + md_pcrel_from (fixp));
3987 else
3988 reloc->addend = fixp->fx_offset;
3989#endif
3990
dabe3bbc
RH
3991 /* We expand R_SPARC_OLO10 to R_SPARC_LO10 and R_SPARC_13
3992 on the same location. */
3993 if (code == BFD_RELOC_SPARC_OLO10)
3994 {
add39d23 3995 relocs[1] = reloc = XNEW (arelent);
dabe3bbc
RH
3996 relocs[2] = NULL;
3997
add39d23 3998 reloc->sym_ptr_ptr = XNEW (asymbol *);
ab3e48dc
KH
3999 *reloc->sym_ptr_ptr
4000 = symbol_get_bfdsym (section_symbol (absolute_section));
dabe3bbc
RH
4001 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
4002 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_SPARC13);
4003 reloc->addend = fixp->tc_fix_data;
4004 }
4005
4006 return relocs;
252b5132
RH
4007}
4008\f
e0c6ed95 4009/* We have no need to default values of symbols. */
252b5132 4010
252b5132 4011symbolS *
5a49b8ac 4012md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
252b5132
RH
4013{
4014 return 0;
e0c6ed95
AM
4015}
4016
4017/* Round up a section size to the appropriate boundary. */
252b5132 4018
252b5132 4019valueT
5a49b8ac 4020md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132
RH
4021{
4022#ifndef OBJ_ELF
4023 /* This is not right for ELF; a.out wants it, and COFF will force
4024 the alignment anyways. */
4025 valueT align = ((valueT) 1
4026 << (valueT) bfd_get_section_alignment (stdoutput, segment));
4027 valueT newsize;
e0c6ed95
AM
4028
4029 /* Turn alignment value into a mask. */
252b5132
RH
4030 align--;
4031 newsize = (size + align) & ~align;
4032 return newsize;
4033#else
4034 return size;
4035#endif
4036}
4037
4038/* Exactly what point is a PC-relative offset relative TO?
4039 On the sparc, they're relative to the address of the offset, plus
4040 its size. This gets us to the following instruction.
e0c6ed95
AM
4041 (??? Is this right? FIXME-SOON) */
4042long
5a49b8ac 4043md_pcrel_from (fixS *fixP)
252b5132
RH
4044{
4045 long ret;
4046
4047 ret = fixP->fx_where + fixP->fx_frag->fr_address;
4048 if (! sparc_pic_code
4049 || fixP->fx_addsy == NULL
49309057 4050 || symbol_section_p (fixP->fx_addsy))
252b5132
RH
4051 ret += fixP->fx_size;
4052 return ret;
4053}
4054\f
4055/* Return log2 (VALUE), or -1 if VALUE is not an exact positive power
4056 of two. */
4057
4058static int
5a49b8ac 4059mylog2 (int value)
252b5132
RH
4060{
4061 int shift;
4062
4063 if (value <= 0)
4064 return -1;
4065
4066 for (shift = 0; (value & 1) == 0; value >>= 1)
4067 ++shift;
4068
4069 return (value == 1) ? shift : -1;
4070}
4071
e0c6ed95 4072/* Sort of like s_lcomm. */
252b5132
RH
4073
4074#ifndef OBJ_ELF
4075static int max_alignment = 15;
4076#endif
4077
4078static void
5a49b8ac 4079s_reserve (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
4080{
4081 char *name;
4082 char *p;
4083 char c;
4084 int align;
4085 int size;
4086 int temp;
4087 symbolS *symbolP;
4088
d02603dc 4089 c = get_symbol_name (&name);
252b5132
RH
4090 p = input_line_pointer;
4091 *p = c;
d02603dc 4092 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
4093
4094 if (*input_line_pointer != ',')
4095 {
4096 as_bad (_("Expected comma after name"));
4097 ignore_rest_of_line ();
4098 return;
4099 }
4100
4101 ++input_line_pointer;
4102
4103 if ((size = get_absolute_expression ()) < 0)
4104 {
4105 as_bad (_("BSS length (%d.) <0! Ignored."), size);
4106 ignore_rest_of_line ();
4107 return;
e0c6ed95 4108 } /* Bad length. */
252b5132
RH
4109
4110 *p = 0;
4111 symbolP = symbol_find_or_make (name);
4112 *p = c;
4113
4114 if (strncmp (input_line_pointer, ",\"bss\"", 6) != 0
4115 && strncmp (input_line_pointer, ",\".bss\"", 7) != 0)
4116 {
4117 as_bad (_("bad .reserve segment -- expected BSS segment"));
4118 return;
4119 }
4120
4121 if (input_line_pointer[2] == '.')
4122 input_line_pointer += 7;
4123 else
4124 input_line_pointer += 6;
4125 SKIP_WHITESPACE ();
4126
4127 if (*input_line_pointer == ',')
4128 {
4129 ++input_line_pointer;
4130
4131 SKIP_WHITESPACE ();
4132 if (*input_line_pointer == '\n')
4133 {
4134 as_bad (_("missing alignment"));
4135 ignore_rest_of_line ();
4136 return;
4137 }
4138
4139 align = (int) get_absolute_expression ();
4140
4141#ifndef OBJ_ELF
4142 if (align > max_alignment)
4143 {
4144 align = max_alignment;
4145 as_warn (_("alignment too large; assuming %d"), align);
4146 }
4147#endif
4148
4149 if (align < 0)
4150 {
4151 as_bad (_("negative alignment"));
4152 ignore_rest_of_line ();
4153 return;
4154 }
4155
4156 if (align != 0)
4157 {
f17c130b 4158 temp = mylog2 (align);
252b5132
RH
4159 if (temp < 0)
4160 {
4161 as_bad (_("alignment not a power of 2"));
4162 ignore_rest_of_line ();
4163 return;
4164 }
4165
4166 align = temp;
4167 }
4168
4169 record_alignment (bss_section, align);
4170 }
4171 else
4172 align = 0;
4173
4174 if (!S_IS_DEFINED (symbolP)
4175#ifdef OBJ_AOUT
4176 && S_GET_OTHER (symbolP) == 0
4177 && S_GET_DESC (symbolP) == 0
4178#endif
4179 )
4180 {
4181 if (! need_pass_2)
4182 {
4183 char *pfrag;
4184 segT current_seg = now_seg;
4185 subsegT current_subseg = now_subseg;
4186
e0c6ed95
AM
4187 /* Switch to bss. */
4188 subseg_set (bss_section, 1);
252b5132
RH
4189
4190 if (align)
e0c6ed95
AM
4191 /* Do alignment. */
4192 frag_align (align, 0, 0);
252b5132 4193
e0c6ed95 4194 /* Detach from old frag. */
ab3e48dc 4195 if (S_GET_SEGMENT (symbolP) == bss_section)
49309057 4196 symbol_get_frag (symbolP)->fr_symbol = NULL;
252b5132 4197
49309057 4198 symbol_set_frag (symbolP, frag_now);
e0c6ed95
AM
4199 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
4200 (offsetT) size, (char *) 0);
252b5132
RH
4201 *pfrag = 0;
4202
4203 S_SET_SEGMENT (symbolP, bss_section);
4204
4205 subseg_set (current_seg, current_subseg);
4206
4207#ifdef OBJ_ELF
4208 S_SET_SIZE (symbolP, size);
4209#endif
4210 }
4211 }
4212 else
4213 {
20203fb9 4214 as_warn (_("Ignoring attempt to re-define symbol %s"),
ab3e48dc 4215 S_GET_NAME (symbolP));
20203fb9 4216 }
252b5132
RH
4217
4218 demand_empty_rest_of_line ();
4219}
4220
4221static void
5a49b8ac 4222s_common (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
4223{
4224 char *name;
4225 char c;
4226 char *p;
685736be 4227 offsetT temp, size;
252b5132
RH
4228 symbolS *symbolP;
4229
d02603dc 4230 c = get_symbol_name (&name);
e0c6ed95 4231 /* Just after name is now '\0'. */
252b5132
RH
4232 p = input_line_pointer;
4233 *p = c;
d02603dc 4234 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
4235 if (*input_line_pointer != ',')
4236 {
4237 as_bad (_("Expected comma after symbol-name"));
4238 ignore_rest_of_line ();
4239 return;
4240 }
e0c6ed95
AM
4241
4242 /* Skip ','. */
4243 input_line_pointer++;
4244
252b5132
RH
4245 if ((temp = get_absolute_expression ()) < 0)
4246 {
685736be
NC
4247 as_bad (_(".COMMon length (%lu) out of range ignored"),
4248 (unsigned long) temp);
252b5132
RH
4249 ignore_rest_of_line ();
4250 return;
4251 }
4252 size = temp;
4253 *p = 0;
4254 symbolP = symbol_find_or_make (name);
4255 *p = c;
4256 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
4257 {
4258 as_bad (_("Ignoring attempt to re-define symbol"));
4259 ignore_rest_of_line ();
4260 return;
4261 }
4262 if (S_GET_VALUE (symbolP) != 0)
4263 {
4264 if (S_GET_VALUE (symbolP) != (valueT) size)
4265 {
364b6d8b
JJ
4266 as_warn (_("Length of .comm \"%s\" is already %ld. Not changed to %ld."),
4267 S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), (long) size);
252b5132
RH
4268 }
4269 }
4270 else
4271 {
4272#ifndef OBJ_ELF
4273 S_SET_VALUE (symbolP, (valueT) size);
4274 S_SET_EXTERNAL (symbolP);
4275#endif
4276 }
7dcc9865 4277 know (symbol_get_frag (symbolP) == &zero_address_frag);
252b5132
RH
4278 if (*input_line_pointer != ',')
4279 {
4280 as_bad (_("Expected comma after common length"));
4281 ignore_rest_of_line ();
4282 return;
4283 }
4284 input_line_pointer++;
4285 SKIP_WHITESPACE ();
4286 if (*input_line_pointer != '"')
4287 {
4288 temp = get_absolute_expression ();
4289
4290#ifndef OBJ_ELF
4291 if (temp > max_alignment)
4292 {
4293 temp = max_alignment;
f17c130b 4294 as_warn (_("alignment too large; assuming %ld"), (long) temp);
252b5132
RH
4295 }
4296#endif
4297
4298 if (temp < 0)
4299 {
4300 as_bad (_("negative alignment"));
4301 ignore_rest_of_line ();
4302 return;
4303 }
4304
4305#ifdef OBJ_ELF
49309057 4306 if (symbol_get_obj (symbolP)->local)
252b5132
RH
4307 {
4308 segT old_sec;
4309 int old_subsec;
252b5132
RH
4310 int align;
4311
4312 old_sec = now_seg;
4313 old_subsec = now_subseg;
4314
4315 if (temp == 0)
4316 align = 0;
4317 else
f17c130b 4318 align = mylog2 (temp);
252b5132
RH
4319
4320 if (align < 0)
4321 {
4322 as_bad (_("alignment not a power of 2"));
4323 ignore_rest_of_line ();
4324 return;
4325 }
4326
4327 record_alignment (bss_section, align);
4328 subseg_set (bss_section, 0);
4329 if (align)
4330 frag_align (align, 0, 0);
4331 if (S_GET_SEGMENT (symbolP) == bss_section)
49309057
ILT
4332 symbol_get_frag (symbolP)->fr_symbol = 0;
4333 symbol_set_frag (symbolP, frag_now);
252b5132
RH
4334 p = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
4335 (offsetT) size, (char *) 0);
4336 *p = 0;
4337 S_SET_SEGMENT (symbolP, bss_section);
4338 S_CLEAR_EXTERNAL (symbolP);
4339 S_SET_SIZE (symbolP, size);
4340 subseg_set (old_sec, old_subsec);
4341 }
4342 else
e0c6ed95 4343#endif /* OBJ_ELF */
252b5132
RH
4344 {
4345 allocate_common:
4346 S_SET_VALUE (symbolP, (valueT) size);
4347#ifdef OBJ_ELF
4348 S_SET_ALIGN (symbolP, temp);
4349 S_SET_SIZE (symbolP, size);
4350#endif
4351 S_SET_EXTERNAL (symbolP);
4352 S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
4353 }
4354 }
4355 else
4356 {
4357 input_line_pointer++;
4358 /* @@ Some use the dot, some don't. Can we get some consistency?? */
4359 if (*input_line_pointer == '.')
4360 input_line_pointer++;
4361 /* @@ Some say data, some say bss. */
4362 if (strncmp (input_line_pointer, "bss\"", 4)
4363 && strncmp (input_line_pointer, "data\"", 5))
4364 {
4365 while (*--input_line_pointer != '"')
4366 ;
4367 input_line_pointer--;
4368 goto bad_common_segment;
4369 }
4370 while (*input_line_pointer++ != '"')
4371 ;
4372 goto allocate_common;
4373 }
4374
49309057 4375 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
252b5132
RH
4376
4377 demand_empty_rest_of_line ();
4378 return;
4379
4380 {
4381 bad_common_segment:
4382 p = input_line_pointer;
4383 while (*p && *p != '\n')
4384 p++;
4385 c = *p;
4386 *p = '\0';
4387 as_bad (_("bad .common segment %s"), input_line_pointer + 1);
4388 *p = c;
4389 input_line_pointer = p;
4390 ignore_rest_of_line ();
4391 return;
4392 }
4393}
4394
67c1ffbe 4395/* Handle the .empty pseudo-op. This suppresses the warnings about
252b5132
RH
4396 invalid delay slot usage. */
4397
4398static void
5a49b8ac 4399s_empty (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
4400{
4401 /* The easy way to implement is to just forget about the last
4402 instruction. */
4403 last_insn = NULL;
4404}
4405
4406static void
5a49b8ac 4407s_seg (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
4408{
4409
4410 if (strncmp (input_line_pointer, "\"text\"", 6) == 0)
4411 {
4412 input_line_pointer += 6;
4413 s_text (0);
4414 return;
4415 }
4416 if (strncmp (input_line_pointer, "\"data\"", 6) == 0)
4417 {
4418 input_line_pointer += 6;
4419 s_data (0);
4420 return;
4421 }
4422 if (strncmp (input_line_pointer, "\"data1\"", 7) == 0)
4423 {
4424 input_line_pointer += 7;
4425 s_data1 ();
4426 return;
4427 }
4428 if (strncmp (input_line_pointer, "\"bss\"", 5) == 0)
4429 {
4430 input_line_pointer += 5;
4431 /* We only support 2 segments -- text and data -- for now, so
4432 things in the "bss segment" will have to go into data for now.
e0c6ed95
AM
4433 You can still allocate SEG_BSS stuff with .lcomm or .reserve. */
4434 subseg_set (data_section, 255); /* FIXME-SOMEDAY. */
252b5132
RH
4435 return;
4436 }
4437 as_bad (_("Unknown segment type"));
4438 demand_empty_rest_of_line ();
4439}
4440
4441static void
5a49b8ac 4442s_data1 (void)
252b5132
RH
4443{
4444 subseg_set (data_section, 1);
4445 demand_empty_rest_of_line ();
4446}
4447
4448static void
5a49b8ac 4449s_proc (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
4450{
4451 while (!is_end_of_line[(unsigned char) *input_line_pointer])
4452 {
4453 ++input_line_pointer;
4454 }
4455 ++input_line_pointer;
4456}
4457
4458/* This static variable is set by s_uacons to tell sparc_cons_align
67c1ffbe 4459 that the expression does not need to be aligned. */
252b5132
RH
4460
4461static int sparc_no_align_cons = 0;
4462
4463/* This handles the unaligned space allocation pseudo-ops, such as
4464 .uaword. .uaword is just like .word, but the value does not need
4465 to be aligned. */
4466
4467static void
5a49b8ac 4468s_uacons (int bytes)
252b5132
RH
4469{
4470 /* Tell sparc_cons_align not to align this value. */
4471 sparc_no_align_cons = 1;
4472 cons (bytes);
4ffadb11 4473 sparc_no_align_cons = 0;
252b5132
RH
4474}
4475
cf9a1301
RH
4476/* This handles the native word allocation pseudo-op .nword.
4477 For sparc_arch_size 32 it is equivalent to .word, for
4478 sparc_arch_size 64 it is equivalent to .xword. */
4479
4480static void
5a49b8ac 4481s_ncons (int bytes ATTRIBUTE_UNUSED)
cf9a1301
RH
4482{
4483 cons (sparc_arch_size == 32 ? 4 : 8);
4484}
4485
6d8809aa
RH
4486#ifdef OBJ_ELF
4487/* Handle the SPARC ELF .register pseudo-op. This sets the binding of a
4488 global register.
4489 The syntax is:
e0c6ed95 4490
6d8809aa 4491 .register %g[2367],{#scratch|symbolname|#ignore}
e0c6ed95 4492*/
6d8809aa
RH
4493
4494static void
5a49b8ac 4495s_register (int ignore ATTRIBUTE_UNUSED)
6d8809aa
RH
4496{
4497 char c;
4498 int reg;
4499 int flags;
d02603dc 4500 char *regname;
6d8809aa
RH
4501
4502 if (input_line_pointer[0] != '%'
4503 || input_line_pointer[1] != 'g'
4504 || ((input_line_pointer[2] & ~1) != '2'
4505 && (input_line_pointer[2] & ~1) != '6')
4506 || input_line_pointer[3] != ',')
4507 as_bad (_("register syntax is .register %%g[2367],{#scratch|symbolname|#ignore}"));
4508 reg = input_line_pointer[2] - '0';
4509 input_line_pointer += 4;
4510
4511 if (*input_line_pointer == '#')
4512 {
4513 ++input_line_pointer;
d02603dc 4514 c = get_symbol_name (&regname);
6d8809aa
RH
4515 if (strcmp (regname, "scratch") && strcmp (regname, "ignore"))
4516 as_bad (_("register syntax is .register %%g[2367],{#scratch|symbolname|#ignore}"));
ab3e48dc 4517 if (regname[0] == 'i')
6d8809aa
RH
4518 regname = NULL;
4519 else
e87de513 4520 regname = (char *) "";
6d8809aa
RH
4521 }
4522 else
4523 {
d02603dc 4524 c = get_symbol_name (&regname);
6d8809aa 4525 }
d02603dc 4526
6d8809aa
RH
4527 if (sparc_arch_size == 64)
4528 {
e0c6ed95 4529 if (globals[reg])
6d8809aa 4530 {
e0c6ed95
AM
4531 if ((regname && globals[reg] != (symbolS *) 1
4532 && strcmp (S_GET_NAME (globals[reg]), regname))
4533 || ((regname != NULL) ^ (globals[reg] != (symbolS *) 1)))
6d8809aa
RH
4534 as_bad (_("redefinition of global register"));
4535 }
4536 else
4537 {
4538 if (regname == NULL)
e0c6ed95 4539 globals[reg] = (symbolS *) 1;
6d8809aa
RH
4540 else
4541 {
4542 if (*regname)
4543 {
4544 if (symbol_find (regname))
4545 as_bad (_("Register symbol %s already defined."),
4546 regname);
4547 }
e0c6ed95
AM
4548 globals[reg] = symbol_make (regname);
4549 flags = symbol_get_bfdsym (globals[reg])->flags;
6d8809aa
RH
4550 if (! *regname)
4551 flags = flags & ~(BSF_GLOBAL|BSF_LOCAL|BSF_WEAK);
4552 if (! (flags & (BSF_GLOBAL|BSF_LOCAL|BSF_WEAK)))
4553 flags |= BSF_GLOBAL;
e0c6ed95
AM
4554 symbol_get_bfdsym (globals[reg])->flags = flags;
4555 S_SET_VALUE (globals[reg], (valueT) reg);
4556 S_SET_ALIGN (globals[reg], reg);
4557 S_SET_SIZE (globals[reg], 0);
6d8809aa
RH
4558 /* Although we actually want undefined_section here,
4559 we have to use absolute_section, because otherwise
4560 generic as code will make it a COM section.
4561 We fix this up in sparc_adjust_symtab. */
e0c6ed95
AM
4562 S_SET_SEGMENT (globals[reg], absolute_section);
4563 S_SET_OTHER (globals[reg], 0);
4564 elf_symbol (symbol_get_bfdsym (globals[reg]))
6d8809aa
RH
4565 ->internal_elf_sym.st_info =
4566 ELF_ST_INFO(STB_GLOBAL, STT_REGISTER);
e0c6ed95 4567 elf_symbol (symbol_get_bfdsym (globals[reg]))
6d8809aa
RH
4568 ->internal_elf_sym.st_shndx = SHN_UNDEF;
4569 }
4570 }
4571 }
4572
d02603dc 4573 (void) restore_line_pointer (c);
6d8809aa
RH
4574
4575 demand_empty_rest_of_line ();
4576}
4577
4578/* Adjust the symbol table. We set undefined sections for STT_REGISTER
4579 symbols which need it. */
e0c6ed95 4580
6d8809aa 4581void
5a49b8ac 4582sparc_adjust_symtab (void)
6d8809aa
RH
4583{
4584 symbolS *sym;
e0c6ed95 4585
6d8809aa
RH
4586 for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
4587 {
4588 if (ELF_ST_TYPE (elf_symbol (symbol_get_bfdsym (sym))
4589 ->internal_elf_sym.st_info) != STT_REGISTER)
4590 continue;
4591
4592 if (ELF_ST_TYPE (elf_symbol (symbol_get_bfdsym (sym))
4593 ->internal_elf_sym.st_shndx != SHN_UNDEF))
4594 continue;
4595
4596 S_SET_SEGMENT (sym, undefined_section);
4597 }
4598}
4599#endif
4600
252b5132
RH
4601/* If the --enforce-aligned-data option is used, we require .word,
4602 et. al., to be aligned correctly. We do it by setting up an
4603 rs_align_code frag, and checking in HANDLE_ALIGN to make sure that
4604 no unexpected alignment was introduced.
4605
4606 The SunOS and Solaris native assemblers enforce aligned data by
4607 default. We don't want to do that, because gcc can deliberately
4608 generate misaligned data if the packed attribute is used. Instead,
4609 we permit misaligned data by default, and permit the user to set an
4610 option to check for it. */
4611
4612void
5a49b8ac 4613sparc_cons_align (int nbytes)
252b5132
RH
4614{
4615 int nalign;
252b5132
RH
4616
4617 /* Only do this if we are enforcing aligned data. */
4618 if (! enforce_aligned_data)
4619 return;
4620
0f2712ed 4621 /* Don't align if this is an unaligned pseudo-op. */
252b5132 4622 if (sparc_no_align_cons)
0f2712ed 4623 return;
252b5132 4624
f17c130b 4625 nalign = mylog2 (nbytes);
252b5132
RH
4626 if (nalign == 0)
4627 return;
4628
9c2799c2 4629 gas_assert (nalign > 0);
252b5132
RH
4630
4631 if (now_seg == absolute_section)
4632 {
4633 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
4634 as_bad (_("misaligned data"));
4635 return;
4636 }
4637
87975d2a
AM
4638 frag_var (rs_align_test, 1, 1, (relax_substateT) 0,
4639 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
252b5132
RH
4640
4641 record_alignment (now_seg, nalign);
4642}
4643
0a9ef439 4644/* This is called from HANDLE_ALIGN in tc-sparc.h. */
252b5132
RH
4645
4646void
5a49b8ac 4647sparc_handle_align (fragS *fragp)
252b5132 4648{
0a9ef439
RH
4649 int count, fix;
4650 char *p;
4651
4652 count = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
bfb32b52 4653
0a9ef439 4654 switch (fragp->fr_type)
252b5132 4655 {
0a9ef439
RH
4656 case rs_align_test:
4657 if (count != 0)
4658 as_bad_where (fragp->fr_file, fragp->fr_line, _("misaligned data"));
4659 break;
e0c6ed95 4660
0a9ef439
RH
4661 case rs_align_code:
4662 p = fragp->fr_literal + fragp->fr_fix;
4663 fix = 0;
e0c6ed95 4664
0a9ef439
RH
4665 if (count & 3)
4666 {
4667 fix = count & 3;
4668 memset (p, 0, fix);
4669 p += fix;
4670 count -= fix;
4671 }
e0c6ed95 4672
0a9ef439
RH
4673 if (SPARC_OPCODE_ARCH_V9_P (max_architecture) && count > 8)
4674 {
4675 unsigned wval = (0x30680000 | count >> 2); /* ba,a,pt %xcc, 1f */
4676 if (INSN_BIG_ENDIAN)
4677 number_to_chars_bigendian (p, wval, 4);
4678 else
4679 number_to_chars_littleendian (p, wval, 4);
4680 p += 4;
4681 count -= 4;
4682 fix += 4;
e0c6ed95 4683 }
0a9ef439
RH
4684
4685 if (INSN_BIG_ENDIAN)
4686 number_to_chars_bigendian (p, 0x01000000, 4);
4687 else
4688 number_to_chars_littleendian (p, 0x01000000, 4);
4689
4690 fragp->fr_fix += fix;
4691 fragp->fr_var = 4;
4692 break;
4693
4694 default:
4695 break;
252b5132
RH
4696 }
4697}
4698
4699#ifdef OBJ_ELF
4700/* Some special processing for a Sparc ELF file. */
4701
4702void
5a49b8ac 4703sparc_elf_final_processing (void)
252b5132
RH
4704{
4705 /* Set the Sparc ELF flag bits. FIXME: There should probably be some
4706 sort of BFD interface for this. */
4707 if (sparc_arch_size == 64)
4708 {
4709 switch (sparc_memory_model)
4710 {
4711 case MM_RMO:
4712 elf_elfheader (stdoutput)->e_flags |= EF_SPARCV9_RMO;
4713 break;
4714 case MM_PSO:
4715 elf_elfheader (stdoutput)->e_flags |= EF_SPARCV9_PSO;
4716 break;
4717 default:
4718 break;
4719 }
4720 }
4721 else if (current_architecture >= SPARC_OPCODE_ARCH_V9)
4722 elf_elfheader (stdoutput)->e_flags |= EF_SPARC_32PLUS;
4723 if (current_architecture == SPARC_OPCODE_ARCH_V9A)
4724 elf_elfheader (stdoutput)->e_flags |= EF_SPARC_SUN_US1;
19f7b010
JJ
4725 else if (current_architecture == SPARC_OPCODE_ARCH_V9B)
4726 elf_elfheader (stdoutput)->e_flags |= EF_SPARC_SUN_US1|EF_SPARC_SUN_US3;
252b5132 4727}
bd5e6e7e 4728
62ebcb5c 4729const char *
5a49b8ac 4730sparc_cons (expressionS *exp, int size)
bd5e6e7e
JJ
4731{
4732 char *save;
62ebcb5c 4733 const char *sparc_cons_special_reloc = NULL;
bd5e6e7e
JJ
4734
4735 SKIP_WHITESPACE ();
bd5e6e7e
JJ
4736 save = input_line_pointer;
4737 if (input_line_pointer[0] == '%'
4738 && input_line_pointer[1] == 'r'
4739 && input_line_pointer[2] == '_')
4740 {
4741 if (strncmp (input_line_pointer + 3, "disp", 4) == 0)
4742 {
4743 input_line_pointer += 7;
4744 sparc_cons_special_reloc = "disp";
4745 }
4746 else if (strncmp (input_line_pointer + 3, "plt", 3) == 0)
4747 {
4748 if (size != 4 && size != 8)
4749 as_bad (_("Illegal operands: %%r_plt in %d-byte data field"), size);
4750 else
4751 {
4752 input_line_pointer += 6;
4753 sparc_cons_special_reloc = "plt";
4754 }
4755 }
b9734f35
JJ
4756 else if (strncmp (input_line_pointer + 3, "tls_dtpoff", 10) == 0)
4757 {
4758 if (size != 4 && size != 8)
4759 as_bad (_("Illegal operands: %%r_tls_dtpoff in %d-byte data field"), size);
4760 else
4761 {
4762 input_line_pointer += 13;
4763 sparc_cons_special_reloc = "tls_dtpoff";
4764 }
4765 }
bd5e6e7e
JJ
4766 if (sparc_cons_special_reloc)
4767 {
4768 int bad = 0;
4769
4770 switch (size)
4771 {
4772 case 1:
4773 if (*input_line_pointer != '8')
4774 bad = 1;
4775 input_line_pointer--;
4776 break;
4777 case 2:
4778 if (input_line_pointer[0] != '1' || input_line_pointer[1] != '6')
4779 bad = 1;
4780 break;
4781 case 4:
4782 if (input_line_pointer[0] != '3' || input_line_pointer[1] != '2')
4783 bad = 1;
4784 break;
4785 case 8:
4786 if (input_line_pointer[0] != '6' || input_line_pointer[1] != '4')
4787 bad = 1;
4788 break;
4789 default:
4790 bad = 1;
4791 break;
4792 }
4793
4794 if (bad)
4795 {
4796 as_bad (_("Illegal operands: Only %%r_%s%d allowed in %d-byte data fields"),
4797 sparc_cons_special_reloc, size * 8, size);
4798 }
4799 else
4800 {
4801 input_line_pointer += 2;
4802 if (*input_line_pointer != '(')
4803 {
4804 as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
4805 sparc_cons_special_reloc, size * 8);
4806 bad = 1;
4807 }
4808 }
4809
4810 if (bad)
4811 {
4812 input_line_pointer = save;
4813 sparc_cons_special_reloc = NULL;
4814 }
4815 else
4816 {
4817 int c;
4818 char *end = ++input_line_pointer;
4819 int npar = 0;
4820
4821 while (! is_end_of_line[(c = *end)])
4822 {
4823 if (c == '(')
4824 npar++;
4825 else if (c == ')')
4826 {
4827 if (!npar)
4828 break;
4829 npar--;
4830 }
4831 end++;
4832 }
4833
4834 if (c != ')')
4835 as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
4836 sparc_cons_special_reloc, size * 8);
4837 else
4838 {
4839 *end = '\0';
4840 expression (exp);
4841 *end = c;
4842 if (input_line_pointer != end)
4843 {
4844 as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
4845 sparc_cons_special_reloc, size * 8);
4846 }
4847 else
4848 {
4849 input_line_pointer++;
4850 SKIP_WHITESPACE ();
4851 c = *input_line_pointer;
4852 if (! is_end_of_line[c] && c != ',')
4853 as_bad (_("Illegal operands: garbage after %%r_%s%d()"),
4854 sparc_cons_special_reloc, size * 8);
4855 }
4856 }
4857 }
4858 }
4859 }
4860 if (sparc_cons_special_reloc == NULL)
4861 expression (exp);
62ebcb5c 4862 return sparc_cons_special_reloc;
bd5e6e7e
JJ
4863}
4864
252b5132
RH
4865#endif
4866
4867/* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
4868 reloc for a cons. We could use the definition there, except that
4869 we want to handle little endian relocs specially. */
4870
4871void
5a49b8ac
AM
4872cons_fix_new_sparc (fragS *frag,
4873 int where,
4874 unsigned int nbytes,
62ebcb5c
AM
4875 expressionS *exp,
4876 const char *sparc_cons_special_reloc)
252b5132
RH
4877{
4878 bfd_reloc_code_real_type r;
4879
4880 r = (nbytes == 1 ? BFD_RELOC_8 :
4881 (nbytes == 2 ? BFD_RELOC_16 :
4882 (nbytes == 4 ? BFD_RELOC_32 : BFD_RELOC_64)));
4883
0f2712ed
NC
4884 if (target_little_endian_data
4885 && nbytes == 4
e0c6ed95 4886 && now_seg->flags & SEC_ALLOC)
252b5132 4887 r = BFD_RELOC_SPARC_REV32;
0f2712ed 4888
bd5e6e7e
JJ
4889 if (sparc_cons_special_reloc)
4890 {
4891 if (*sparc_cons_special_reloc == 'd')
4892 switch (nbytes)
4893 {
4894 case 1: r = BFD_RELOC_8_PCREL; break;
4895 case 2: r = BFD_RELOC_16_PCREL; break;
4896 case 4: r = BFD_RELOC_32_PCREL; break;
4897 case 8: r = BFD_RELOC_64_PCREL; break;
4898 default: abort ();
4899 }
b9734f35 4900 else if (*sparc_cons_special_reloc == 'p')
bd5e6e7e
JJ
4901 switch (nbytes)
4902 {
4903 case 4: r = BFD_RELOC_SPARC_PLT32; break;
4904 case 8: r = BFD_RELOC_SPARC_PLT64; break;
4905 }
b9734f35
JJ
4906 else
4907 switch (nbytes)
4908 {
4909 case 4: r = BFD_RELOC_SPARC_TLS_DTPOFF32; break;
4910 case 8: r = BFD_RELOC_SPARC_TLS_DTPOFF64; break;
4911 }
bd5e6e7e
JJ
4912 }
4913 else if (sparc_no_align_cons)
0f2712ed
NC
4914 {
4915 switch (nbytes)
4916 {
4917 case 2: r = BFD_RELOC_SPARC_UA16; break;
4918 case 4: r = BFD_RELOC_SPARC_UA32; break;
4919 case 8: r = BFD_RELOC_SPARC_UA64; break;
4920 default: abort ();
4921 }
4ffadb11 4922 }
0f2712ed 4923
252b5132 4924 fix_new_exp (frag, where, (int) nbytes, exp, 0, r);
364b6d8b
JJ
4925}
4926
4927void
5a49b8ac 4928sparc_cfi_frame_initial_instructions (void)
364b6d8b
JJ
4929{
4930 cfi_add_CFA_def_cfa (14, sparc_arch_size == 64 ? 0x7ff : 0);
4931}
4932
4933int
1df69f4f 4934sparc_regname_to_dw2regnum (char *regname)
364b6d8b 4935{
74b4e47a
TS
4936 char *q;
4937 int i;
364b6d8b
JJ
4938
4939 if (!regname[0])
4940 return -1;
4941
74b4e47a
TS
4942 switch (regname[0])
4943 {
4944 case 'g': i = 0; break;
4945 case 'o': i = 1; break;
4946 case 'l': i = 2; break;
4947 case 'i': i = 3; break;
4948 default: i = -1; break;
4949 }
4950 if (i != -1)
364b6d8b
JJ
4951 {
4952 if (regname[1] < '0' || regname[1] > '8' || regname[2])
4953 return -1;
74b4e47a 4954 return i * 8 + regname[1] - '0';
364b6d8b
JJ
4955 }
4956 if (regname[0] == 's' && regname[1] == 'p' && !regname[2])
4957 return 14;
4958 if (regname[0] == 'f' && regname[1] == 'p' && !regname[2])
4959 return 30;
4960 if (regname[0] == 'f' || regname[0] == 'r')
4961 {
4962 unsigned int regnum;
4963
4964 regnum = strtoul (regname + 1, &q, 10);
74b4e47a 4965 if (q == NULL || *q)
364b6d8b
JJ
4966 return -1;
4967 if (regnum >= ((regname[0] == 'f'
4968 && SPARC_OPCODE_ARCH_V9_P (max_architecture))
4969 ? 64 : 32))
4970 return -1;
4971 if (regname[0] == 'f')
4972 {
4973 regnum += 32;
4974 if (regnum >= 64 && (regnum & 1))
4975 return -1;
4976 }
4977 return regnum;
4978 }
4979 return -1;
4980}
4981
4982void
4983sparc_cfi_emit_pcrel_expr (expressionS *exp, unsigned int nbytes)
4984{
364b6d8b 4985 sparc_no_align_cons = 1;
62ebcb5c 4986 emit_expr_with_reloc (exp, nbytes, "disp");
364b6d8b 4987 sparc_no_align_cons = 0;
252b5132 4988}