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