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