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