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