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