This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gas / config / tc-sparc.c
1 /* tc-sparc.c -- Assemble for the SPARC
2    Copyright (C) 1989, 90-96, 97, 1998 Free Software Foundation, Inc.
3    This file is part of GAS, the GNU Assembler.
4
5    GAS is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    GAS is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public
16    License along with GAS; see the file COPYING.  If not, write
17    to the Free Software Foundation, 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA. */
19
20 #include <stdio.h>
21 #include <ctype.h>
22
23 #include "as.h"
24 #include "subsegs.h"
25
26 #include "opcode/sparc.h"
27
28 #ifdef OBJ_ELF
29 #include "elf/sparc.h"
30 #endif
31
32 static struct sparc_arch *lookup_arch PARAMS ((char *));
33 static void init_default_arch PARAMS ((void));
34 static void sparc_ip PARAMS ((char *, const struct sparc_opcode **));
35 static int in_signed_range PARAMS ((bfd_signed_vma, bfd_signed_vma));
36 static int in_unsigned_range PARAMS ((bfd_vma, bfd_vma));
37 static int in_bitfield_range PARAMS ((bfd_signed_vma, bfd_signed_vma));
38 static int sparc_ffs PARAMS ((unsigned int));
39 static bfd_vma BSR PARAMS ((bfd_vma, int));
40 static int cmp_reg_entry PARAMS ((const PTR, const PTR));
41 static int parse_keyword_arg PARAMS ((int (*) (const char *), char **, int *));
42 static int parse_const_expr_arg PARAMS ((char **, int *));
43 static int get_expression PARAMS ((char *str));
44
45 /* Default architecture.  */
46 /* ??? The default value should be V8, but sparclite support was added
47    by making it the default.  GCC now passes -Asparclite, so maybe sometime in
48    the future we can set this to V8.  */
49 #ifndef DEFAULT_ARCH
50 #define DEFAULT_ARCH "sparclite"
51 #endif
52 static char *default_arch = DEFAULT_ARCH;
53
54 /* Non-zero if the initial values of `max_architecture' and `sparc_arch_size'
55    have been set.  */
56 static int default_init_p;
57
58 /* Current architecture.  We don't bump up unless necessary.  */
59 static enum sparc_opcode_arch_val current_architecture = SPARC_OPCODE_ARCH_V6;
60
61 /* The maximum architecture level we can bump up to.
62    In a 32 bit environment, don't allow bumping up to v9 by default.
63    The native assembler works this way.  The user is required to pass
64    an explicit argument before we'll create v9 object files.  However, if
65    we don't see any v9 insns, a v8plus object file is not created.  */
66 static enum sparc_opcode_arch_val max_architecture;
67
68 /* Either 32 or 64, selects file format.  */
69 static int sparc_arch_size;
70 /* Initial (default) value, recorded separately in case a user option
71    changes the value before md_show_usage is called.  */
72 static int default_arch_size;
73
74 #ifdef OBJ_ELF
75 /* The currently selected v9 memory model.  Currently only used for
76    ELF.  */
77 static enum { MM_TSO, MM_PSO, MM_RMO } sparc_memory_model = MM_RMO;
78 #endif
79
80 static int architecture_requested;
81 static int warn_on_bump;
82
83 /* If warn_on_bump and the needed architecture is higher than this
84    architecture, issue a warning.  */
85 static enum sparc_opcode_arch_val warn_after_architecture;
86
87 /* Non-zero if we are generating PIC code.  */
88 int sparc_pic_code;
89
90 /* Non-zero if we should give an error when misaligned data is seen.  */
91 static int enforce_aligned_data;
92
93 extern int target_big_endian;
94
95 static int target_little_endian_data;
96
97 /* V9 and 86x have big and little endian data, but instructions are always big
98    endian.  The sparclet has bi-endian support but both data and insns have
99    the same endianness.  Global `target_big_endian' is used for data.
100    The following macro is used for instructions.  */
101 #ifndef INSN_BIG_ENDIAN
102 #define INSN_BIG_ENDIAN (target_big_endian \
103                          || default_arch_type == sparc86x \
104                          || SPARC_OPCODE_ARCH_V9_P (max_architecture))
105 #endif
106
107 /* handle of the OPCODE hash table */
108 static struct hash_control *op_hash;
109
110 static int log2 PARAMS ((int));
111 static void s_data1 PARAMS ((void));
112 static void s_seg PARAMS ((int));
113 static void s_proc PARAMS ((int));
114 static void s_reserve PARAMS ((int));
115 static void s_common PARAMS ((int));
116 static void s_empty PARAMS ((int));
117 static void s_uacons PARAMS ((int));
118
119 const pseudo_typeS md_pseudo_table[] =
120 {
121   {"align", s_align_bytes, 0},  /* Defaulting is invalid (0) */
122   {"common", s_common, 0},
123   {"empty", s_empty, 0},
124   {"global", s_globl, 0},
125   {"half", cons, 2},
126   {"optim", s_ignore, 0},
127   {"proc", s_proc, 0},
128   {"reserve", s_reserve, 0},
129   {"seg", s_seg, 0},
130   {"skip", s_space, 0},
131   {"word", cons, 4},
132   {"xword", cons, 8},
133   {"uahalf", s_uacons, 2},
134   {"uaword", s_uacons, 4},
135   {"uaxword", s_uacons, 8},
136 #ifdef OBJ_ELF
137   /* these are specific to sparc/svr4 */
138   {"pushsection", obj_elf_section, 0},
139   {"popsection", obj_elf_previous, 0},
140   {"2byte", s_uacons, 2},
141   {"4byte", s_uacons, 4},
142   {"8byte", s_uacons, 8},
143 #endif
144   {NULL, 0, 0},
145 };
146
147 const int md_reloc_size = 12;   /* Size of relocation record */
148
149 /* This array holds the chars that always start a comment.  If the
150    pre-processor is disabled, these aren't very useful */
151 const char comment_chars[] = "!";       /* JF removed '|' from comment_chars */
152
153 /* This array holds the chars that only start a comment at the beginning of
154    a line.  If the line seems to have the form '# 123 filename'
155    .line and .file directives will appear in the pre-processed output */
156 /* Note that input_file.c hand checks for '#' at the beginning of the
157    first line of the input file.  This is because the compiler outputs
158    #NO_APP at the beginning of its output. */
159 /* Also note that comments started like this one will always
160    work if '/' isn't otherwise defined. */
161 const char line_comment_chars[] = "#";
162
163 const char line_separator_chars[] = "";
164
165 /* Chars that can be used to separate mant from exp in floating point nums */
166 const char EXP_CHARS[] = "eE";
167
168 /* Chars that mean this number is a floating point constant */
169 /* As in 0f12.456 */
170 /* or    0d1.2345e12 */
171 const char FLT_CHARS[] = "rRsSfFdDxXpP";
172
173 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
174    changed in read.c.  Ideally it shouldn't have to know about it at all,
175    but nothing is ideal around here.  */
176
177 static unsigned char octal[256];
178 #define isoctal(c)  octal[(unsigned char) (c)]
179 static unsigned char toHex[256];
180
181 struct sparc_it
182   {
183     char *error;
184     unsigned long opcode;
185     struct nlist *nlistp;
186     expressionS exp;
187     int pcrel;
188     bfd_reloc_code_real_type reloc;
189   };
190
191 struct sparc_it the_insn, set_insn;
192
193 static void output_insn
194   PARAMS ((const struct sparc_opcode *, struct sparc_it *));
195 \f
196 /* Table of arguments to -A.
197    The sparc_opcode_arch table in sparc-opc.c is insufficient and incorrect
198    for this use.  That table is for opcodes only.  This table is for opcodes
199    and file formats.  */
200
201 enum sparc_arch_types {v6, v7, v8, sparclet, sparclite, sparc86x, v8plus,
202                        v8plusa, v9, v9a, v9_64};
203
204 static struct sparc_arch {
205   char *name;
206   char *opcode_arch;
207   enum sparc_arch_types arch_type;
208   /* Default word size, as specified during configuration.
209      A value of zero means can't be used to specify default architecture.  */
210   int default_arch_size;
211   /* Allowable arg to -A?  */
212   int user_option_p;
213 } sparc_arch_table[] = {
214   { "v6", "v6", v6, 0, 1 },
215   { "v7", "v7", v7, 0, 1 },
216   { "v8", "v8", v8, 32, 1 },
217   { "sparclet", "sparclet", sparclet, 32, 1 },
218   { "sparclite", "sparclite", sparclite, 32, 1 },
219   { "sparc86x", "sparclite", sparc86x, 32, 1 },
220   { "v8plus", "v9", v9, 0, 1 },
221   { "v8plusa", "v9a", v9, 0, 1 },
222   { "v9", "v9", v9, 0, 1 },
223   { "v9a", "v9a", v9, 0, 1 },
224   /* This exists to allow configure.in/Makefile.in to pass one
225      value to specify both the default machine and default word size.  */
226   { "v9-64", "v9", v9, 64, 0 },
227   { NULL, NULL, v8, 0, 0 }
228 };
229
230 /* Variant of default_arch */
231 static enum sparc_arch_types default_arch_type;
232
233 static struct sparc_arch *
234 lookup_arch (name)
235      char *name;
236 {
237   struct sparc_arch *sa;
238
239   for (sa = &sparc_arch_table[0]; sa->name != NULL; sa++)
240     if (strcmp (sa->name, name) == 0)
241       break;
242   if (sa->name == NULL)
243     return NULL;
244   return sa;
245 }
246
247 /* Initialize the default opcode arch and word size from the default
248    architecture name.  */
249
250 static void
251 init_default_arch ()
252 {
253   struct sparc_arch *sa = lookup_arch (default_arch);
254
255   if (sa == NULL
256       || sa->default_arch_size == 0)
257     as_fatal (_("Invalid default architecture, broken assembler."));
258
259   max_architecture = sparc_opcode_lookup_arch (sa->opcode_arch);
260   if (max_architecture == SPARC_OPCODE_ARCH_BAD)
261     as_fatal (_("Bad opcode table, broken assembler."));
262   default_arch_size = sparc_arch_size = sa->default_arch_size;
263   default_init_p = 1;
264   default_arch_type = sa->arch_type;
265 }
266
267 /* Called by TARGET_FORMAT.  */
268
269 const char *
270 sparc_target_format ()
271 {
272   /* We don't get a chance to initialize anything before we're called,
273      so handle that now.  */
274   if (! default_init_p)
275     init_default_arch ();
276
277 #ifdef OBJ_AOUT
278 #ifdef TE_NetBSD
279   return "a.out-sparc-netbsd";
280 #else
281 #ifdef TE_SPARCAOUT
282   if (target_big_endian)
283     return "a.out-sunos-big";
284   else if (default_arch_type == sparc86x && target_little_endian_data)
285     return "a.out-sunos-big";
286   else return "a.out-sparc-little";
287 #else
288   return "a.out-sunos-big";
289 #endif
290 #endif
291 #endif
292
293 #ifdef OBJ_BOUT
294   return "b.out.big";
295 #endif
296
297 #ifdef OBJ_COFF
298 #ifdef TE_LYNX
299   return "coff-sparc-lynx";
300 #else
301   return "coff-sparc";
302 #endif
303 #endif
304
305 #ifdef OBJ_ELF
306   return sparc_arch_size == 64 ? "elf64-sparc" : "elf32-sparc";
307 #endif
308
309   abort ();
310 }
311 \f
312 /*
313  * md_parse_option
314  *      Invocation line includes a switch not recognized by the base assembler.
315  *      See if it's a processor-specific option.  These are:
316  *
317  *      -bump
318  *              Warn on architecture bumps.  See also -A.
319  *
320  *      -Av6, -Av7, -Av8, -Asparclite, -Asparclet
321  *              Standard 32 bit architectures.
322  *      -Av8plus, -Av8plusa
323  *              Sparc64 in a 32 bit world.
324  *      -Av9, -Av9a
325  *              Sparc64 in either a 32 or 64 bit world (-32/-64 says which).
326  *              This used to only mean 64 bits, but properly specifying it
327  *              complicated gcc's ASM_SPECs, so now opcode selection is
328  *              specified orthogonally to word size (except when specifying
329  *              the default, but that is an internal implementation detail).
330  *      -xarch=v8plus, -xarch=v8plusa
331  *              Same as -Av8plus{,a}, for compatibility with Sun's assembler.
332  *
333  *              Select the architecture and possibly the file format.
334  *              Instructions or features not supported by the selected
335  *              architecture cause fatal errors.
336  *
337  *              The default is to start at v6, and bump the architecture up
338  *              whenever an instruction is seen at a higher level.  In 32 bit
339  *              environments, v9 is not bumped up to, the user must pass
340  *              -Av8plus{,a}.
341  *
342  *              If -bump is specified, a warning is printing when bumping to
343  *              higher levels.
344  *
345  *              If an architecture is specified, all instructions must match
346  *              that architecture.  Any higher level instructions are flagged
347  *              as errors.  Note that in the 32 bit environment specifying
348  *              -Av8plus does not automatically create a v8plus object file, a
349  *              v9 insn must be seen.
350  *
351  *              If both an architecture and -bump are specified, the
352  *              architecture starts at the specified level, but bumps are
353  *              warnings.  Note that we can't set `current_architecture' to
354  *              the requested level in this case: in the 32 bit environment,
355  *              we still must avoid creating v8plus object files unless v9
356  *              insns are seen.
357  *
358  * Note:
359  *              Bumping between incompatible architectures is always an
360  *              error.  For example, from sparclite to v9.
361  */
362
363 #ifdef OBJ_ELF
364 CONST char *md_shortopts = "A:K:VQ:sq";
365 #else
366 #ifdef OBJ_AOUT
367 CONST char *md_shortopts = "A:k";
368 #else
369 CONST char *md_shortopts = "A:";
370 #endif
371 #endif
372 struct option md_longopts[] = {
373 #define OPTION_BUMP (OPTION_MD_BASE)
374   {"bump", no_argument, NULL, OPTION_BUMP},
375 #define OPTION_SPARC (OPTION_MD_BASE + 1)
376   {"sparc", no_argument, NULL, OPTION_SPARC},
377 #define OPTION_XARCH (OPTION_MD_BASE + 2)
378   {"xarch", required_argument, NULL, OPTION_XARCH},
379 #ifdef OBJ_ELF
380 #define OPTION_32 (OPTION_MD_BASE + 3)
381   {"32", no_argument, NULL, OPTION_32},
382 #define OPTION_64 (OPTION_MD_BASE + 4)
383   {"64", no_argument, NULL, OPTION_64},
384 #define OPTION_TSO (OPTION_MD_BASE + 5)
385   {"TSO", no_argument, NULL, OPTION_TSO},
386 #define OPTION_PSO (OPTION_MD_BASE + 6)
387   {"PSO", no_argument, NULL, OPTION_PSO},
388 #define OPTION_RMO (OPTION_MD_BASE + 7)
389   {"RMO", no_argument, NULL, OPTION_RMO},
390 #endif
391 #ifdef SPARC_BIENDIAN
392 #define OPTION_LITTLE_ENDIAN (OPTION_MD_BASE + 8)
393   {"EL", no_argument, NULL, OPTION_LITTLE_ENDIAN},
394 #define OPTION_BIG_ENDIAN (OPTION_MD_BASE + 9)
395   {"EB", no_argument, NULL, OPTION_BIG_ENDIAN},
396 #endif
397 #define OPTION_ENFORCE_ALIGNED_DATA (OPTION_MD_BASE + 10)
398   {"enforce-aligned-data", no_argument, NULL, OPTION_ENFORCE_ALIGNED_DATA},
399 #define OPTION_LITTLE_ENDIAN_DATA (OPTION_MD_BASE + 11)
400   {"little-endian-data", no_argument, NULL, OPTION_LITTLE_ENDIAN_DATA},
401   {NULL, no_argument, NULL, 0}
402 };
403 size_t md_longopts_size = sizeof(md_longopts);
404
405 int
406 md_parse_option (c, arg)
407      int c;
408      char *arg;
409 {
410   /* We don't get a chance to initialize anything before we're called,
411      so handle that now.  */
412   if (! default_init_p)
413     init_default_arch ();
414
415   switch (c)
416     {
417     case OPTION_BUMP:
418       warn_on_bump = 1;
419       warn_after_architecture = SPARC_OPCODE_ARCH_V6;
420       break;
421
422     case OPTION_XARCH:
423       /* This is for compatibility with Sun's assembler.  */
424       if (strcmp (arg, "v8plus") != 0
425           && strcmp (arg, "v8plusa") != 0)
426         {
427           as_bad (_("invalid architecture -xarch=%s"), arg);
428           return 0;
429         }
430
431       /* fall through */
432
433     case 'A':
434       {
435         struct sparc_arch *sa;
436         enum sparc_opcode_arch_val opcode_arch;
437
438         sa = lookup_arch (arg);
439         if (sa == NULL
440             || ! sa->user_option_p)
441           {
442             as_bad (_("invalid architecture -A%s"), arg);
443             return 0;
444           }
445
446         opcode_arch = sparc_opcode_lookup_arch (sa->opcode_arch);
447         if (opcode_arch == SPARC_OPCODE_ARCH_BAD)
448           as_fatal (_("Bad opcode table, broken assembler."));
449
450         max_architecture = opcode_arch;
451         architecture_requested = 1;
452       }
453       break;
454
455     case OPTION_SPARC:
456       /* Ignore -sparc, used by SunOS make default .s.o rule.  */
457       break;
458
459     case OPTION_ENFORCE_ALIGNED_DATA:
460       enforce_aligned_data = 1;
461       break;
462
463 #ifdef SPARC_BIENDIAN
464     case OPTION_LITTLE_ENDIAN:
465       target_big_endian = 0;
466       if (default_arch_type != sparclet)
467         as_fatal ("This target does not support -EL");
468       break;
469     case OPTION_LITTLE_ENDIAN_DATA:
470       target_little_endian_data = 1;
471       target_big_endian = 0;
472       if (default_arch_type != sparc86x
473           && default_arch_type != v9)
474         as_fatal ("This target does not support --little-endian-data");
475       break;
476     case OPTION_BIG_ENDIAN:
477       target_big_endian = 1;
478       break;
479 #endif
480
481 #ifdef OBJ_AOUT
482     case 'k':
483       sparc_pic_code = 1;
484       break;
485 #endif
486
487 #ifdef OBJ_ELF
488     case OPTION_32:
489     case OPTION_64:
490       {
491         const char **list, **l;
492
493         sparc_arch_size = c == OPTION_32 ? 32 : 64;
494         list = bfd_target_list ();
495         for (l = list; *l != NULL; l++)
496           {
497             if (sparc_arch_size == 32)
498               {
499                 if (strcmp (*l, "elf32-sparc") == 0)
500                   break;
501               }
502             else
503               {
504                 if (strcmp (*l, "elf64-sparc") == 0)
505                   break;
506               }
507           }
508         if (*l == NULL)
509           as_fatal (_("No compiled in support for %d bit object file format"),
510                     sparc_arch_size);
511         free (list);
512       }
513       break;
514
515     case OPTION_TSO:
516       sparc_memory_model = MM_TSO;
517       break;
518
519     case OPTION_PSO:
520       sparc_memory_model = MM_PSO;
521       break;
522
523     case OPTION_RMO:
524       sparc_memory_model = MM_RMO;
525       break;
526
527     case 'V':
528       print_version_id ();
529       break;
530
531     case 'Q':
532       /* Qy - do emit .comment
533          Qn - do not emit .comment */
534       break;
535
536     case 's':
537       /* use .stab instead of .stab.excl */
538       break;
539
540     case 'q':
541       /* quick -- native assembler does fewer checks */
542       break;
543
544     case 'K':
545       if (strcmp (arg, "PIC") != 0)
546         as_warn (_("Unrecognized option following -K"));
547       else
548         sparc_pic_code = 1;
549       break;
550 #endif
551
552     default:
553       return 0;
554     }
555
556   return 1;
557 }
558
559 void
560 md_show_usage (stream)
561      FILE *stream;
562 {
563   const struct sparc_arch *arch;
564
565   /* We don't get a chance to initialize anything before we're called,
566      so handle that now.  */
567   if (! default_init_p)
568     init_default_arch ();
569
570   fprintf(stream, _("SPARC options:\n"));
571   for (arch = &sparc_arch_table[0]; arch->name; arch++)
572     {
573       if (arch != &sparc_arch_table[0])
574         fprintf (stream, " | ");
575       if (arch->user_option_p)
576         fprintf (stream, "-A%s", arch->name);
577     }
578   fprintf (stream, _("\n-xarch=v8plus | -xarch=v8plusa\n"));
579   fprintf (stream, _("\
580                         specify variant of SPARC architecture\n\
581 -bump                   warn when assembler switches architectures\n\
582 -sparc                  ignored\n\
583 --enforce-aligned-data  force .long, etc., to be aligned correctly\n"));
584 #ifdef OBJ_AOUT
585   fprintf (stream, _("\
586 -k                      generate PIC\n"));
587 #endif
588 #ifdef OBJ_ELF
589   fprintf (stream, _("\
590 -32                     create 32 bit object file\n\
591 -64                     create 64 bit object file\n"));
592   fprintf (stream, _("\
593                         [default is %d]\n"), default_arch_size);
594   fprintf (stream, _("\
595 -TSO                    use Total Store Ordering\n\
596 -PSO                    use Partial Store Ordering\n\
597 -RMO                    use Relaxed Memory Ordering\n"));
598   fprintf (stream, _("\
599                         [default is %s]\n"), (default_arch_size == 64) ? "RMO" : "TSO");
600   fprintf (stream, _("\
601 -KPIC                   generate PIC\n\
602 -V                      print assembler version number\n\
603 -q                      ignored\n\
604 -Qy, -Qn                ignored\n\
605 -s                      ignored\n"));
606 #endif
607 #ifdef SPARC_BIENDIAN
608   fprintf (stream, _("\
609 -EL                     generate code for a little endian machine\n\
610 -EB                     generate code for a big endian machine\n\
611 --little-endian-data    generate code for a machine having big endian\n\
612                         instructions and little endian data."));
613 #endif
614 }
615 \f
616 /* sparc64 priviledged registers */
617
618 struct priv_reg_entry
619   {
620     char *name;
621     int regnum;
622   };
623
624 struct priv_reg_entry priv_reg_table[] =
625 {
626   {"tpc", 0},
627   {"tnpc", 1},
628   {"tstate", 2},
629   {"tt", 3},
630   {"tick", 4},
631   {"tba", 5},
632   {"pstate", 6},
633   {"tl", 7},
634   {"pil", 8},
635   {"cwp", 9},
636   {"cansave", 10},
637   {"canrestore", 11},
638   {"cleanwin", 12},
639   {"otherwin", 13},
640   {"wstate", 14},
641   {"fq", 15},
642   {"ver", 31},
643   {"", -1},                     /* end marker */
644 };
645
646 /* v9a specific asrs */
647
648 struct priv_reg_entry v9a_asr_table[] =
649 {
650   {"tick_cmpr", 23},
651   {"softint", 22},
652   {"set_softint", 20},
653   {"pic", 17},
654   {"pcr", 16},
655   {"gsr", 19},
656   {"dcr", 18},
657   {"clear_softint", 21},
658   {"", -1},                     /* end marker */
659 };
660
661 static int
662 cmp_reg_entry (parg, qarg)
663      const PTR parg;
664      const PTR qarg;
665 {
666   const struct priv_reg_entry *p = (const struct priv_reg_entry *) parg;
667   const struct priv_reg_entry *q = (const struct priv_reg_entry *) qarg;
668
669   return strcmp (q->name, p->name);
670 }
671 \f
672 /* This function is called once, at assembler startup time.  It should
673    set up all the tables, etc. that the MD part of the assembler will need. */
674
675 void
676 md_begin ()
677 {
678   register const char *retval = NULL;
679   int lose = 0;
680   register unsigned int i = 0;
681
682   /* We don't get a chance to initialize anything before md_parse_option
683      is called, and it may not be called, so handle default initialization
684      now if not already done.  */
685   if (! default_init_p)
686     init_default_arch ();
687
688   op_hash = hash_new ();
689
690   while (i < (unsigned int) sparc_num_opcodes)
691     {
692       const char *name = sparc_opcodes[i].name;
693       retval = hash_insert (op_hash, name, (PTR) &sparc_opcodes[i]);
694       if (retval != NULL)
695         {
696           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
697                    sparc_opcodes[i].name, retval);
698           lose = 1;
699         }
700       do
701         {
702           if (sparc_opcodes[i].match & sparc_opcodes[i].lose)
703             {
704               fprintf (stderr, _("internal error: losing opcode: `%s' \"%s\"\n"),
705                        sparc_opcodes[i].name, sparc_opcodes[i].args);
706               lose = 1;
707             }
708           ++i;
709         }
710       while (i < (unsigned int) sparc_num_opcodes
711              && !strcmp (sparc_opcodes[i].name, name));
712     }
713
714   if (lose)
715     as_fatal (_("Broken assembler.  No assembly attempted."));
716
717   for (i = '0'; i < '8'; ++i)
718     octal[i] = 1;
719   for (i = '0'; i <= '9'; ++i)
720     toHex[i] = i - '0';
721   for (i = 'a'; i <= 'f'; ++i)
722     toHex[i] = i + 10 - 'a';
723   for (i = 'A'; i <= 'F'; ++i)
724     toHex[i] = i + 10 - 'A';
725
726   qsort (priv_reg_table, sizeof (priv_reg_table) / sizeof (priv_reg_table[0]),
727          sizeof (priv_reg_table[0]), cmp_reg_entry);
728
729   /* If -bump, record the architecture level at which we start issuing
730      warnings.  The behaviour is different depending upon whether an
731      architecture was explicitly specified.  If it wasn't, we issue warnings
732      for all upwards bumps.  If it was, we don't start issuing warnings until
733      we need to bump beyond the requested architecture or when we bump between
734      conflicting architectures.  */
735
736   if (warn_on_bump
737       && architecture_requested)
738     {
739       /* `max_architecture' records the requested architecture.
740          Issue warnings if we go above it.  */
741       warn_after_architecture = max_architecture;
742
743       /* Find the highest architecture level that doesn't conflict with
744          the requested one.  */
745       for (max_architecture = SPARC_OPCODE_ARCH_MAX;
746            max_architecture > warn_after_architecture;
747            --max_architecture)
748         if (! SPARC_OPCODE_CONFLICT_P (max_architecture,
749                                        warn_after_architecture))
750           break;
751     }
752 }
753
754 /* Called after all assembly has been done.  */
755
756 void
757 sparc_md_end ()
758 {
759   if (sparc_arch_size == 64)
760     {
761       if (current_architecture == SPARC_OPCODE_ARCH_V9A)
762         bfd_set_arch_mach (stdoutput, bfd_arch_sparc, bfd_mach_sparc_v9a);
763       else
764         bfd_set_arch_mach (stdoutput, bfd_arch_sparc, bfd_mach_sparc_v9);
765     }
766   else
767     {
768       if (current_architecture == SPARC_OPCODE_ARCH_V9)
769         bfd_set_arch_mach (stdoutput, bfd_arch_sparc, bfd_mach_sparc_v8plus);
770       else if (current_architecture == SPARC_OPCODE_ARCH_V9A)
771         bfd_set_arch_mach (stdoutput, bfd_arch_sparc, bfd_mach_sparc_v8plusa);
772       else if (current_architecture == SPARC_OPCODE_ARCH_SPARCLET)
773         bfd_set_arch_mach (stdoutput, bfd_arch_sparc, bfd_mach_sparc_sparclet);
774       else if (default_arch_type == sparc86x && target_little_endian_data)
775         bfd_set_arch_mach (stdoutput, bfd_arch_sparc, bfd_mach_sparc_sparclite_le);
776       else
777         {
778           /* The sparclite is treated like a normal sparc.  Perhaps it shouldn't
779              be but for now it is (since that's the way it's always been
780              treated).  */
781           bfd_set_arch_mach (stdoutput, bfd_arch_sparc, bfd_mach_sparc);
782         }
783     }
784 }
785 \f
786 /* Return non-zero if VAL is in the range -(MAX+1) to MAX.  */
787
788 static INLINE int
789 in_signed_range (val, max)
790      bfd_signed_vma val, max;
791 {
792   if (max <= 0)
793     abort ();
794   /* Sign-extend the value from the architecture word size, so that
795      0xffffffff is always considered -1 on sparc32.  */
796   if (sparc_arch_size == 32)
797     {
798       bfd_signed_vma sign = (bfd_signed_vma)1 << 31;
799       val = ((val & 0xffffffff) ^ sign) - sign;
800     }
801   if (val > max)
802     return 0;
803   if (val < ~max)
804     return 0;
805   return 1;
806 }
807
808 /* Return non-zero if VAL is in the range 0 to MAX.  */
809
810 static INLINE int
811 in_unsigned_range (val, max)
812      bfd_vma val, max;
813 {
814   if (val > max)
815     return 0;
816   return 1;
817 }
818
819 /* Return non-zero if VAL is in the range -(MAX/2+1) to MAX.
820    (e.g. -15 to +31).  */
821
822 static INLINE int
823 in_bitfield_range (val, max)
824      bfd_signed_vma val, max;
825 {
826   if (max <= 0)
827     abort ();
828   if (val > max)
829     return 0;
830   if (val < ~(max >> 1))
831     return 0;
832   return 1;
833 }
834
835 static int
836 sparc_ffs (mask)
837      unsigned int mask;
838 {
839   int i;
840
841   if (mask == 0)
842     return -1;
843
844   for (i = 0; (mask & 1) == 0; ++i)
845     mask >>= 1;
846   return i;
847 }
848
849 /* Implement big shift right.  */
850 static bfd_vma
851 BSR (val, amount)
852      bfd_vma val;
853      int amount;
854 {
855   if (sizeof (bfd_vma) <= 4 && amount >= 32)
856     as_fatal (_("Support for 64-bit arithmetic not compiled in."));
857   return val >> amount;
858 }
859 \f
860 /* For communication between sparc_ip and get_expression.  */
861 static char *expr_end;
862
863 /* For communication between md_assemble and sparc_ip.  */
864 static int special_case;
865
866 /* Values for `special_case'.
867    Instructions that require wierd handling because they're longer than
868    4 bytes.  */
869 #define SPECIAL_CASE_NONE       0
870 #define SPECIAL_CASE_SET        1
871 #define SPECIAL_CASE_SETSW      2
872 #define SPECIAL_CASE_SETX       3
873 /* FIXME: sparc-opc.c doesn't have necessary "S" trigger to enable this.  */
874 #define SPECIAL_CASE_FDIV       4
875
876 /* Bit masks of various insns.  */
877 #define NOP_INSN 0x01000000
878 #define OR_INSN 0x80100000
879 #define FMOVS_INSN 0x81A00020
880 #define SETHI_INSN 0x01000000
881 #define SLLX_INSN 0x81281000
882 #define SRA_INSN 0x81380000
883
884 /* The last instruction to be assembled.  */
885 static const struct sparc_opcode *last_insn;
886 /* The assembled opcode of `last_insn'.  */
887 static unsigned long last_opcode;
888 \f
889 /* Main entry point to assemble one instruction.  */
890
891 void
892 md_assemble (str)
893      char *str;
894 {
895   const struct sparc_opcode *insn;
896
897   know (str);
898   special_case = SPECIAL_CASE_NONE;
899   sparc_ip (str, &insn);
900
901   /* We warn about attempts to put a floating point branch in a delay slot,
902      unless the delay slot has been annulled.  */
903   if (insn != NULL
904       && last_insn != NULL
905       && (insn->flags & F_FBR) != 0
906       && (last_insn->flags & F_DELAYED) != 0
907       /* ??? This test isn't completely accurate.  We assume anything with
908          F_{UNBR,CONDBR,FBR} set is annullable.  */
909       && ((last_insn->flags & (F_UNBR | F_CONDBR | F_FBR)) == 0
910           || (last_opcode & ANNUL) == 0))
911     as_warn (_("FP branch in delay slot"));
912
913   /* SPARC before v9 requires a nop instruction between a floating
914      point instruction and a floating point branch.  We insert one
915      automatically, with a warning.  */
916   if (max_architecture < SPARC_OPCODE_ARCH_V9
917       && insn != NULL
918       && last_insn != NULL
919       && (insn->flags & F_FBR) != 0
920       && (last_insn->flags & F_FLOAT) != 0)
921     {
922       struct sparc_it nop_insn;
923
924       nop_insn.opcode = NOP_INSN;
925       nop_insn.reloc = BFD_RELOC_NONE;
926       output_insn (insn, &nop_insn);
927       as_warn (_("FP branch preceded by FP instruction; NOP inserted"));
928     }
929
930   switch (special_case)
931     {
932     case SPECIAL_CASE_NONE:
933       /* normal insn */
934       output_insn (insn, &the_insn);
935       break;
936
937     case SPECIAL_CASE_SET:
938       {
939         int need_hi22_p = 0;
940
941         /* "set" is not defined for negative numbers in v9: it doesn't yield
942            what you expect it to.  */
943         if (SPARC_OPCODE_ARCH_V9_P (max_architecture)
944             && the_insn.exp.X_op == O_constant)
945           {
946             if (the_insn.exp.X_add_number < 0)
947               as_warn (_("set: used with negative number"));
948             else if (the_insn.exp.X_add_number > (offsetT) 0xffffffff)
949               as_warn (_("set: number larger than 4294967295"));
950           }
951
952         /* See if operand is absolute and small; skip sethi if so.  */
953         if (the_insn.exp.X_op != O_constant
954             || the_insn.exp.X_add_number >= (1 << 12)
955             || the_insn.exp.X_add_number < -(1 << 12))
956           {
957             output_insn (insn, &the_insn);
958             need_hi22_p = 1;
959           }
960         /* See if operand has no low-order bits; skip OR if so.  */
961         if (the_insn.exp.X_op != O_constant
962             || (need_hi22_p && (the_insn.exp.X_add_number & 0x3FF) != 0)
963             || ! need_hi22_p)
964           {
965             int rd = (the_insn.opcode & RD (~0)) >> 25;
966             the_insn.opcode = (OR_INSN | (need_hi22_p ? RS1 (rd) : 0)
967                                | RD (rd)
968                                | IMMED
969                                | (the_insn.exp.X_add_number
970                                   & (need_hi22_p ? 0x3ff : 0x1fff)));
971             the_insn.reloc = (the_insn.exp.X_op != O_constant
972                               ? BFD_RELOC_LO10
973                               : BFD_RELOC_NONE);
974             output_insn (insn, &the_insn);
975           }
976         break;
977       }
978
979     case SPECIAL_CASE_SETSW:
980       {
981         /* FIXME: Not finished.  */
982         break;
983       }
984
985     case SPECIAL_CASE_SETX:
986       {
987 #define SIGNEXT32(x) ((((x) & 0xffffffff) ^ 0x80000000) - 0x80000000)
988         int upper32 = SIGNEXT32 (BSR (the_insn.exp.X_add_number, 32));
989         int lower32 = SIGNEXT32 (the_insn.exp.X_add_number);
990 #undef SIGNEXT32
991         int tmpreg = (the_insn.opcode & RS1 (~0)) >> 14;
992         int dstreg = (the_insn.opcode & RD (~0)) >> 25;
993         /* Output directly to dst reg if lower 32 bits are all zero.  */
994         int upper_dstreg = (the_insn.exp.X_op == O_constant
995                             && lower32 == 0) ? dstreg : tmpreg;
996         int need_hh22_p = 0, need_hm10_p = 0, need_hi22_p = 0, need_lo10_p = 0;
997
998         /* The tmp reg should not be the dst reg.  */
999         if (tmpreg == dstreg)
1000           as_warn (_("setx: temporary register same as destination register"));
1001
1002         /* Reset X_add_number, we've extracted it as upper32/lower32.
1003            Otherwise fixup_segment will complain about not being able to
1004            write an 8 byte number in a 4 byte field.  */
1005         the_insn.exp.X_add_number = 0;
1006
1007         /* ??? Obviously there are other optimizations we can do
1008            (e.g. sethi+shift for 0x1f0000000) and perhaps we shouldn't be
1009            doing some of these.  Later.  If you do change things, try to
1010            change all of this to be table driven as well.  */
1011
1012         /* What to output depends on the number if it's constant.
1013            Compute that first, then output what we've decided upon.  */
1014         if (the_insn.exp.X_op != O_constant)
1015           need_hh22_p = need_hm10_p = need_hi22_p = need_lo10_p = 1;
1016         else
1017           {
1018             /* Only need hh22 if `or' insn can't handle constant.  */
1019             if (upper32 < -(1 << 12) || upper32 >= (1 << 12))
1020               need_hh22_p = 1;
1021
1022             /* Does bottom part (after sethi) have bits?  */
1023             if ((need_hh22_p && (upper32 & 0x3ff) != 0)
1024                 /* No hh22, but does upper32 still have bits we can't set
1025                    from lower32?  */
1026                 || (! need_hh22_p
1027                     && upper32 != 0
1028                     && (upper32 != -1 || lower32 >= 0)))
1029               need_hm10_p = 1;
1030
1031             /* If the lower half is all zero, we build the upper half directly
1032                into the dst reg.  */
1033             if (lower32 != 0
1034                 /* Need lower half if number is zero.  */
1035                 || (! need_hh22_p && ! need_hm10_p))
1036               {
1037                 /* No need for sethi if `or' insn can handle constant.  */
1038                 if (lower32 < -(1 << 12) || lower32 >= (1 << 12)
1039                     /* Note that we can't use a negative constant in the `or'
1040                        insn unless the upper 32 bits are all ones.  */
1041                     || (lower32 < 0 && upper32 != -1))
1042                   need_hi22_p = 1;
1043
1044                 /* Does bottom part (after sethi) have bits?  */
1045                 if ((need_hi22_p && (lower32 & 0x3ff) != 0)
1046                     /* No sethi.  */
1047                     || (! need_hi22_p && (lower32 & 0x1fff) != 0)
1048                     /* Need `or' if we didn't set anything else.  */
1049                     || (! need_hi22_p && ! need_hh22_p && ! need_hm10_p))
1050                   need_lo10_p = 1;
1051               }
1052           }
1053
1054         if (need_hh22_p)
1055           {
1056             the_insn.opcode = (SETHI_INSN | RD (upper_dstreg)
1057                                | ((upper32 >> 10) & 0x3fffff));
1058             the_insn.reloc = (the_insn.exp.X_op != O_constant
1059                               ? BFD_RELOC_SPARC_HH22 : BFD_RELOC_NONE);
1060             output_insn (insn, &the_insn);
1061           }
1062
1063         if (need_hm10_p)
1064           {
1065             the_insn.opcode = (OR_INSN
1066                                | (need_hh22_p ? RS1 (upper_dstreg) : 0)
1067                                | RD (upper_dstreg)
1068                                | IMMED
1069                                | (upper32
1070                                   & (need_hh22_p ? 0x3ff : 0x1fff)));
1071             the_insn.reloc = (the_insn.exp.X_op != O_constant
1072                               ? BFD_RELOC_SPARC_HM10 : BFD_RELOC_NONE);
1073             output_insn (insn, &the_insn);
1074           }
1075
1076         if (need_hi22_p)
1077           {
1078             the_insn.opcode = (SETHI_INSN | RD (dstreg)
1079                                | ((lower32 >> 10) & 0x3fffff));
1080             the_insn.reloc = BFD_RELOC_HI22;
1081             output_insn (insn, &the_insn);
1082           }
1083
1084         if (need_lo10_p)
1085           {
1086             /* FIXME: One nice optimization to do here is to OR the low part
1087                with the highpart if hi22 isn't needed and the low part is
1088                positive.  */
1089             the_insn.opcode = (OR_INSN | (need_hi22_p ? RS1 (dstreg) : 0)
1090                                | RD (dstreg)
1091                                | IMMED
1092                                | (lower32
1093                                   & (need_hi22_p ? 0x3ff : 0x1fff)));
1094             the_insn.reloc = BFD_RELOC_LO10;
1095             output_insn (insn, &the_insn);
1096           }
1097
1098         /* If we needed to build the upper part, shift it into place.  */
1099         if (need_hh22_p || need_hm10_p)
1100           {
1101             the_insn.opcode = (SLLX_INSN | RS1 (upper_dstreg) | RD (upper_dstreg)
1102                                | IMMED | 32);
1103             the_insn.reloc = BFD_RELOC_NONE;
1104             output_insn (insn, &the_insn);
1105           }
1106
1107         /* If we needed to build both upper and lower parts, OR them together.  */
1108         if ((need_hh22_p || need_hm10_p)
1109             && (need_hi22_p || need_lo10_p))
1110           {
1111             the_insn.opcode = (OR_INSN | RS1 (dstreg) | RS2 (upper_dstreg)
1112                                | RD (dstreg));
1113             the_insn.reloc = BFD_RELOC_NONE;
1114             output_insn (insn, &the_insn);
1115           }
1116         /* We didn't need both regs, but we may have to sign extend lower32.  */
1117         else if (need_hi22_p && upper32 == -1)
1118           {
1119             the_insn.opcode = (SRA_INSN | RS1 (dstreg) | RD (dstreg)
1120                                | IMMED | 0);
1121             the_insn.reloc = BFD_RELOC_NONE;
1122             output_insn (insn, &the_insn);
1123           }
1124         break;
1125       }
1126
1127     case SPECIAL_CASE_FDIV:
1128       {
1129         int rd = (the_insn.opcode >> 25) & 0x1f;
1130
1131         output_insn (insn, &the_insn);
1132
1133         /* According to information leaked from Sun, the "fdiv" instructions
1134            on early SPARC machines would produce incorrect results sometimes.
1135            The workaround is to add an fmovs of the destination register to
1136            itself just after the instruction.  This was true on machines
1137            with Weitek 1165 float chips, such as the Sun-4/260 and /280. */
1138         assert (the_insn.reloc == BFD_RELOC_NONE);
1139         the_insn.opcode = FMOVS_INSN | rd | RD (rd);
1140         output_insn (insn, &the_insn);
1141         break;
1142       }
1143
1144     default:
1145       as_fatal (_("failed special case insn sanity check"));
1146     }
1147 }
1148
1149 /* Subroutine of md_assemble to do the actual parsing.  */
1150
1151 static void
1152 sparc_ip (str, pinsn)
1153      char *str;
1154      const struct sparc_opcode **pinsn;
1155 {
1156   char *error_message = "";
1157   char *s;
1158   const char *args;
1159   char c;
1160   const struct sparc_opcode *insn;
1161   char *argsStart;
1162   unsigned long opcode;
1163   unsigned int mask = 0;
1164   int match = 0;
1165   int comma = 0;
1166   int v9_arg_p;
1167
1168   s = str;
1169   if (islower ((unsigned char) *s))
1170     {
1171       do
1172         ++s;
1173       while (islower ((unsigned char) *s) || isdigit ((unsigned char) *s));
1174     }
1175
1176   switch (*s)
1177     {
1178     case '\0':
1179       break;
1180
1181     case ',':
1182       comma = 1;
1183
1184       /*FALLTHROUGH */
1185
1186     case ' ':
1187       *s++ = '\0';
1188       break;
1189
1190     default:
1191       as_fatal (_("Unknown opcode: `%s'"), str);
1192     }
1193   insn = (struct sparc_opcode *) hash_find (op_hash, str);
1194   *pinsn = insn;
1195   if (insn == NULL)
1196     {
1197       as_bad (_("Unknown opcode: `%s'"), str);
1198       return;
1199     }
1200   if (comma)
1201     {
1202       *--s = ',';
1203     }
1204
1205   argsStart = s;
1206   for (;;)
1207     {
1208       opcode = insn->match;
1209       memset (&the_insn, '\0', sizeof (the_insn));
1210       the_insn.reloc = BFD_RELOC_NONE;
1211       v9_arg_p = 0;
1212
1213       /*
1214        * Build the opcode, checking as we go to make
1215        * sure that the operands match
1216        */
1217       for (args = insn->args;; ++args)
1218         {
1219           switch (*args)
1220             {
1221             case 'K':
1222               {
1223                 int kmask = 0;
1224
1225                 /* Parse a series of masks.  */
1226                 if (*s == '#')
1227                   {
1228                     while (*s == '#')
1229                       {
1230                         int mask;
1231
1232                         if (! parse_keyword_arg (sparc_encode_membar, &s,
1233                                                  &mask))
1234                           {
1235                             error_message = _(": invalid membar mask name");
1236                             goto error;
1237                           }
1238                         kmask |= mask;
1239                         while (*s == ' ') { ++s; continue; }
1240                         if (*s == '|' || *s == '+')
1241                           ++s;
1242                         while (*s == ' ') { ++s; continue; }
1243                       }
1244                   }
1245                 else
1246                   {
1247                     if (! parse_const_expr_arg (&s, &kmask))
1248                       {
1249                         error_message = _(": invalid membar mask expression");
1250                         goto error;
1251                       }
1252                     if (kmask < 0 || kmask > 127)
1253                       {
1254                         error_message = _(": invalid membar mask number");
1255                         goto error;
1256                       }
1257                   }
1258
1259                 opcode |= MEMBAR (kmask);
1260                 continue;
1261               }
1262
1263             case '*':
1264               {
1265                 int fcn = 0;
1266
1267                 /* Parse a prefetch function.  */
1268                 if (*s == '#')
1269                   {
1270                     if (! parse_keyword_arg (sparc_encode_prefetch, &s, &fcn))
1271                       {
1272                         error_message = _(": invalid prefetch function name");
1273                         goto error;
1274                       }
1275                   }
1276                 else
1277                   {
1278                     if (! parse_const_expr_arg (&s, &fcn))
1279                       {
1280                         error_message = _(": invalid prefetch function expression");
1281                         goto error;
1282                       }
1283                     if (fcn < 0 || fcn > 31)
1284                       {
1285                         error_message = _(": invalid prefetch function number");
1286                         goto error;
1287                       }
1288                   }
1289                 opcode |= RD (fcn);
1290                 continue;
1291               }
1292
1293             case '!':
1294             case '?':
1295               /* Parse a sparc64 privileged register.  */
1296               if (*s == '%')
1297                 {
1298                   struct priv_reg_entry *p = priv_reg_table;
1299                   unsigned int len = 9999999; /* init to make gcc happy */
1300
1301                   s += 1;
1302                   while (p->name[0] > s[0])
1303                     p++;
1304                   while (p->name[0] == s[0])
1305                     {
1306                       len = strlen (p->name);
1307                       if (strncmp (p->name, s, len) == 0)
1308                         break;
1309                       p++;
1310                     }
1311                   if (p->name[0] != s[0])
1312                     {
1313                       error_message = _(": unrecognizable privileged register");
1314                       goto error;
1315                     }
1316                   if (*args == '?')
1317                     opcode |= (p->regnum << 14);
1318                   else
1319                     opcode |= (p->regnum << 25);
1320                   s += len;
1321                   continue;
1322                 }
1323               else
1324                 {
1325                   error_message = _(": unrecognizable privileged register");
1326                   goto error;
1327                 }
1328
1329             case '_':
1330             case '/':
1331               /* Parse a v9a ancillary state register.  */
1332               if (*s == '%')
1333                 {
1334                   struct priv_reg_entry *p = v9a_asr_table;
1335                   unsigned int len = 9999999; /* init to make gcc happy */
1336
1337                   s += 1;
1338                   while (p->name[0] > s[0])
1339                     p++;
1340                   while (p->name[0] == s[0])
1341                     {
1342                       len = strlen (p->name);
1343                       if (strncmp (p->name, s, len) == 0)
1344                         break;
1345                       p++;
1346                     }
1347                   if (p->name[0] != s[0])
1348                     {
1349                       error_message = _(": unrecognizable v9a ancillary state register");
1350                       goto error;
1351                     }
1352                   if (*args == '/' && (p->regnum == 20 || p->regnum == 21))
1353                     {
1354                       error_message = _(": rd on write only ancillary state register");
1355                       goto error;
1356                     }                 
1357                   if (*args == '/')
1358                     opcode |= (p->regnum << 14);
1359                   else
1360                     opcode |= (p->regnum << 25);
1361                   s += len;
1362                   continue;
1363                 }
1364               else
1365                 {
1366                   error_message = _(": unrecognizable v9a ancillary state register");
1367                   goto error;
1368                 }
1369
1370             case 'M':
1371             case 'm':
1372               if (strncmp (s, "%asr", 4) == 0)
1373                 {
1374                   s += 4;
1375
1376                   if (isdigit ((unsigned char) *s))
1377                     {
1378                       long num = 0;
1379
1380                       while (isdigit ((unsigned char) *s))
1381                         {
1382                           num = num * 10 + *s - '0';
1383                           ++s;
1384                         }
1385
1386                       if (current_architecture >= SPARC_OPCODE_ARCH_V9)
1387                         {
1388                           if (num < 16 || 31 < num)
1389                             {
1390                               error_message = _(": asr number must be between 16 and 31");
1391                               goto error;
1392                             }
1393                         }
1394                       else
1395                         {
1396                           if (num < 0 || 31 < num)
1397                             {
1398                               error_message = _(": asr number must be between 0 and 31");
1399                               goto error;
1400                             }
1401                         }
1402
1403                       opcode |= (*args == 'M' ? RS1 (num) : RD (num));
1404                       continue;
1405                     }
1406                   else
1407                     {
1408                       error_message = _(": expecting %asrN");
1409                       goto error;
1410                     }
1411                 } /* if %asr */
1412               break;
1413
1414             case 'I':
1415               the_insn.reloc = BFD_RELOC_SPARC_11;
1416               goto immediate;
1417
1418             case 'j':
1419               the_insn.reloc = BFD_RELOC_SPARC_10;
1420               goto immediate;
1421
1422             case 'X':
1423               /* V8 systems don't understand BFD_RELOC_SPARC_5.  */
1424               if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1425                 the_insn.reloc = BFD_RELOC_SPARC_5;
1426               else
1427                 the_insn.reloc = BFD_RELOC_SPARC13;
1428               /* These fields are unsigned, but for upward compatibility,
1429                  allow negative values as well.  */
1430               goto immediate;
1431
1432             case 'Y':
1433               /* V8 systems don't understand BFD_RELOC_SPARC_6.  */
1434               if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1435                 the_insn.reloc = BFD_RELOC_SPARC_6;
1436               else
1437                 the_insn.reloc = BFD_RELOC_SPARC13;
1438               /* These fields are unsigned, but for upward compatibility,
1439                  allow negative values as well.  */
1440               goto immediate;
1441
1442             case 'k':
1443               the_insn.reloc = /* RELOC_WDISP2_14 */ BFD_RELOC_SPARC_WDISP16;
1444               the_insn.pcrel = 1;
1445               goto immediate;
1446
1447             case 'G':
1448               the_insn.reloc = BFD_RELOC_SPARC_WDISP19;
1449               the_insn.pcrel = 1;
1450               goto immediate;
1451
1452             case 'N':
1453               if (*s == 'p' && s[1] == 'n')
1454                 {
1455                   s += 2;
1456                   continue;
1457                 }
1458               break;
1459
1460             case 'T':
1461               if (*s == 'p' && s[1] == 't')
1462                 {
1463                   s += 2;
1464                   continue;
1465                 }
1466               break;
1467
1468             case 'z':
1469               if (*s == ' ')
1470                 {
1471                   ++s;
1472                 }
1473               if (strncmp (s, "%icc", 4) == 0)
1474                 {
1475                   s += 4;
1476                   continue;
1477                 }
1478               break;
1479
1480             case 'Z':
1481               if (*s == ' ')
1482                 {
1483                   ++s;
1484                 }
1485               if (strncmp (s, "%xcc", 4) == 0)
1486                 {
1487                   s += 4;
1488                   continue;
1489                 }
1490               break;
1491
1492             case '6':
1493               if (*s == ' ')
1494                 {
1495                   ++s;
1496                 }
1497               if (strncmp (s, "%fcc0", 5) == 0)
1498                 {
1499                   s += 5;
1500                   continue;
1501                 }
1502               break;
1503
1504             case '7':
1505               if (*s == ' ')
1506                 {
1507                   ++s;
1508                 }
1509               if (strncmp (s, "%fcc1", 5) == 0)
1510                 {
1511                   s += 5;
1512                   continue;
1513                 }
1514               break;
1515
1516             case '8':
1517               if (*s == ' ')
1518                 {
1519                   ++s;
1520                 }
1521               if (strncmp (s, "%fcc2", 5) == 0)
1522                 {
1523                   s += 5;
1524                   continue;
1525                 }
1526               break;
1527
1528             case '9':
1529               if (*s == ' ')
1530                 {
1531                   ++s;
1532                 }
1533               if (strncmp (s, "%fcc3", 5) == 0)
1534                 {
1535                   s += 5;
1536                   continue;
1537                 }
1538               break;
1539
1540             case 'P':
1541               if (strncmp (s, "%pc", 3) == 0)
1542                 {
1543                   s += 3;
1544                   continue;
1545                 }
1546               break;
1547
1548             case 'W':
1549               if (strncmp (s, "%tick", 5) == 0)
1550                 {
1551                   s += 5;
1552                   continue;
1553                 }
1554               break;
1555
1556             case '\0':          /* end of args */
1557               if (*s == '\0')
1558                 {
1559                   match = 1;
1560                 }
1561               break;
1562
1563             case '+':
1564               if (*s == '+')
1565                 {
1566                   ++s;
1567                   continue;
1568                 }
1569               if (*s == '-')
1570                 {
1571                   continue;
1572                 }
1573               break;
1574
1575             case '[':           /* these must match exactly */
1576             case ']':
1577             case ',':
1578             case ' ':
1579               if (*s++ == *args)
1580                 continue;
1581               break;
1582
1583             case '#':           /* must be at least one digit */
1584               if (isdigit ((unsigned char) *s++))
1585                 {
1586                   while (isdigit ((unsigned char) *s))
1587                     {
1588                       ++s;
1589                     }
1590                   continue;
1591                 }
1592               break;
1593
1594             case 'C':           /* coprocessor state register */
1595               if (strncmp (s, "%csr", 4) == 0)
1596                 {
1597                   s += 4;
1598                   continue;
1599                 }
1600               break;
1601
1602             case 'b':           /* next operand is a coprocessor register */
1603             case 'c':
1604             case 'D':
1605               if (*s++ == '%' && *s++ == 'c' && isdigit ((unsigned char) *s))
1606                 {
1607                   mask = *s++;
1608                   if (isdigit ((unsigned char) *s))
1609                     {
1610                       mask = 10 * (mask - '0') + (*s++ - '0');
1611                       if (mask >= 32)
1612                         {
1613                           break;
1614                         }
1615                     }
1616                   else
1617                     {
1618                       mask -= '0';
1619                     }
1620                   switch (*args)
1621                     {
1622
1623                     case 'b':
1624                       opcode |= mask << 14;
1625                       continue;
1626
1627                     case 'c':
1628                       opcode |= mask;
1629                       continue;
1630
1631                     case 'D':
1632                       opcode |= mask << 25;
1633                       continue;
1634                     }
1635                 }
1636               break;
1637
1638             case 'r':           /* next operand must be a register */
1639             case 'O':
1640             case '1':
1641             case '2':
1642             case 'd':
1643               if (*s++ == '%')
1644                 {
1645                   switch (c = *s++)
1646                     {
1647
1648                     case 'f':   /* frame pointer */
1649                       if (*s++ == 'p')
1650                         {
1651                           mask = 0x1e;
1652                           break;
1653                         }
1654                       goto error;
1655
1656                     case 'g':   /* global register */
1657                       if (isoctal (c = *s++))
1658                         {
1659                           mask = c - '0';
1660                           break;
1661                         }
1662                       goto error;
1663
1664                     case 'i':   /* in register */
1665                       if (isoctal (c = *s++))
1666                         {
1667                           mask = c - '0' + 24;
1668                           break;
1669                         }
1670                       goto error;
1671
1672                     case 'l':   /* local register */
1673                       if (isoctal (c = *s++))
1674                         {
1675                           mask = (c - '0' + 16);
1676                           break;
1677                         }
1678                       goto error;
1679
1680                     case 'o':   /* out register */
1681                       if (isoctal (c = *s++))
1682                         {
1683                           mask = (c - '0' + 8);
1684                           break;
1685                         }
1686                       goto error;
1687
1688                     case 's':   /* stack pointer */
1689                       if (*s++ == 'p')
1690                         {
1691                           mask = 0xe;
1692                           break;
1693                         }
1694                       goto error;
1695
1696                     case 'r':   /* any register */
1697                       if (!isdigit ((unsigned char) (c = *s++)))
1698                         {
1699                           goto error;
1700                         }
1701                       /* FALLTHROUGH */
1702                     case '0':
1703                     case '1':
1704                     case '2':
1705                     case '3':
1706                     case '4':
1707                     case '5':
1708                     case '6':
1709                     case '7':
1710                     case '8':
1711                     case '9':
1712                       if (isdigit ((unsigned char) *s))
1713                         {
1714                           if ((c = 10 * (c - '0') + (*s++ - '0')) >= 32)
1715                             {
1716                               goto error;
1717                             }
1718                         }
1719                       else
1720                         {
1721                           c -= '0';
1722                         }
1723                       mask = c;
1724                       break;
1725
1726                     default:
1727                       goto error;
1728                     }
1729
1730                   /* Got the register, now figure out where
1731                      it goes in the opcode.  */
1732                   switch (*args)
1733                     {
1734                     case '1':
1735                       opcode |= mask << 14;
1736                       continue;
1737
1738                     case '2':
1739                       opcode |= mask;
1740                       continue;
1741
1742                     case 'd':
1743                       opcode |= mask << 25;
1744                       continue;
1745
1746                     case 'r':
1747                       opcode |= (mask << 25) | (mask << 14);
1748                       continue;
1749
1750                     case 'O':
1751                       opcode |= (mask << 25) | (mask << 0);
1752                       continue;
1753                     }
1754                 }
1755               break;
1756
1757             case 'e':           /* next operand is a floating point register */
1758             case 'v':
1759             case 'V':
1760
1761             case 'f':
1762             case 'B':
1763             case 'R':
1764
1765             case 'g':
1766             case 'H':
1767             case 'J':
1768               {
1769                 char format;
1770
1771                 if (*s++ == '%'
1772                     && ((format = *s) == 'f')
1773                     && isdigit ((unsigned char) *++s))
1774                   {
1775                     for (mask = 0; isdigit ((unsigned char) *s); ++s)
1776                       {
1777                         mask = 10 * mask + (*s - '0');
1778                       }         /* read the number */
1779
1780                     if ((*args == 'v'
1781                          || *args == 'B'
1782                          || *args == 'H')
1783                         && (mask & 1))
1784                       {
1785                         break;
1786                       }         /* register must be even numbered */
1787
1788                     if ((*args == 'V'
1789                          || *args == 'R'
1790                          || *args == 'J')
1791                         && (mask & 3))
1792                       {
1793                         break;
1794                       }         /* register must be multiple of 4 */
1795
1796                     if (mask >= 64)
1797                       {
1798                         if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1799                           error_message = _(": There are only 64 f registers; [0-63]");
1800                         else
1801                           error_message = _(": There are only 32 f registers; [0-31]");
1802                         goto error;
1803                       } /* on error */
1804                     else if (mask >= 32)
1805                       {
1806                         if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1807                           {
1808                             v9_arg_p = 1;
1809                             mask -= 31; /* wrap high bit */
1810                           }
1811                         else
1812                           {
1813                             error_message = _(": There are only 32 f registers; [0-31]");
1814                             goto error;
1815                           }
1816                       }
1817                   }
1818                 else
1819                   {
1820                     break;
1821                   }     /* if not an 'f' register. */
1822
1823                 switch (*args)
1824                   {
1825                   case 'v':
1826                   case 'V':
1827                   case 'e':
1828                     opcode |= RS1 (mask);
1829                     continue;
1830
1831
1832                   case 'f':
1833                   case 'B':
1834                   case 'R':
1835                     opcode |= RS2 (mask);
1836                     continue;
1837
1838                   case 'g':
1839                   case 'H':
1840                   case 'J':
1841                     opcode |= RD (mask);
1842                     continue;
1843                   }             /* pack it in. */
1844
1845                 know (0);
1846                 break;
1847               }                 /* float arg */
1848
1849             case 'F':
1850               if (strncmp (s, "%fsr", 4) == 0)
1851                 {
1852                   s += 4;
1853                   continue;
1854                 }
1855               break;
1856
1857             case '0':           /* 64 bit immediate (setx insn) */
1858               the_insn.reloc = BFD_RELOC_NONE; /* reloc handled elsewhere */
1859               goto immediate;
1860
1861             case 'h':           /* high 22 bits */
1862               the_insn.reloc = BFD_RELOC_HI22;
1863               goto immediate;
1864
1865             case 'l':           /* 22 bit PC relative immediate */
1866               the_insn.reloc = BFD_RELOC_SPARC_WDISP22;
1867               the_insn.pcrel = 1;
1868               goto immediate;
1869
1870             case 'L':           /* 30 bit immediate */
1871               the_insn.reloc = BFD_RELOC_32_PCREL_S2;
1872               the_insn.pcrel = 1;
1873               goto immediate;
1874
1875             case 'n':           /* 22 bit immediate */
1876               the_insn.reloc = BFD_RELOC_SPARC22;
1877               goto immediate;
1878
1879             case 'i':           /* 13 bit immediate */
1880               the_insn.reloc = BFD_RELOC_SPARC13;
1881
1882               /* fallthrough */
1883
1884             immediate:
1885               if (*s == ' ')
1886                 s++;
1887
1888               /* Check for %hi, etc.  */
1889               if (*s == '%')
1890                 {
1891                   static struct ops {
1892                     /* The name as it appears in assembler.  */
1893                     char *name;
1894                     /* strlen (name), precomputed for speed */
1895                     int len;
1896                     /* The reloc this pseudo-op translates to.  */
1897                     int reloc;
1898                     /* Non-zero if for v9 only.  */
1899                     int v9_p;
1900                     /* Non-zero if can be used in pc-relative contexts.  */
1901                     int pcrel_p;/*FIXME:wip*/
1902                   } ops[] = {
1903                     /* hix/lox must appear before hi/lo so %hix won't be
1904                        mistaken for %hi.  */
1905                     { "hix", 3, BFD_RELOC_SPARC_HIX22, 1, 0 },
1906                     { "lox", 3, BFD_RELOC_SPARC_LOX10, 1, 0 },
1907                     { "hi", 2, BFD_RELOC_HI22, 0, 1 },
1908                     { "lo", 2, BFD_RELOC_LO10, 0, 1 },
1909                     { "hh", 2, BFD_RELOC_SPARC_HH22, 1, 1 },
1910                     { "hm", 2, BFD_RELOC_SPARC_HM10, 1, 1 },
1911                     { "lm", 2, BFD_RELOC_SPARC_LM22, 1, 1 },
1912                     { "h44", 3, BFD_RELOC_SPARC_H44, 1, 0 },
1913                     { "m44", 3, BFD_RELOC_SPARC_M44, 1, 0 },
1914                     { "l44", 3, BFD_RELOC_SPARC_L44, 1, 0 },
1915                     { "uhi", 3, BFD_RELOC_SPARC_HH22, 1, 0 },
1916                     { "ulo", 3, BFD_RELOC_SPARC_HM10, 1, 0 },
1917                     { NULL }
1918                   };
1919                   struct ops *o;
1920
1921                   for (o = ops; o->name; o++)
1922                     if (strncmp (s + 1, o->name, o->len) == 0)
1923                       break;
1924                   if (o->name == NULL)
1925                     break;
1926
1927                   the_insn.reloc = o->reloc;
1928                   s += o->len + 1;
1929                   v9_arg_p = o->v9_p;
1930                 }
1931
1932               /* Note that if the get_expression() fails, we will still
1933                  have created U entries in the symbol table for the
1934                  'symbols' in the input string.  Try not to create U
1935                  symbols for registers, etc.  */
1936               {
1937                 /* This stuff checks to see if the expression ends in
1938                    +%reg.  If it does, it removes the register from
1939                    the expression, and re-sets 's' to point to the
1940                    right place.  */
1941
1942                 char *s1;
1943
1944                 for (s1 = s; *s1 && *s1 != ',' && *s1 != ']'; s1++) ;
1945
1946                 if (s1 != s && isdigit ((unsigned char) s1[-1]))
1947                   {
1948                     if (s1[-2] == '%' && s1[-3] == '+')
1949                       {
1950                         s1 -= 3;
1951                         *s1 = '\0';
1952                         (void) get_expression (s);
1953                         *s1 = '+';
1954                         s = s1;
1955                         continue;
1956                       }
1957                     else if (strchr ("goli0123456789", s1[-2]) && s1[-3] == '%' && s1[-4] == '+')
1958                       {
1959                         s1 -= 4;
1960                         *s1 = '\0';
1961                         (void) get_expression (s);
1962                         *s1 = '+';
1963                         s = s1;
1964                         continue;
1965                       }
1966                   }
1967               }
1968               (void) get_expression (s);
1969               s = expr_end;
1970
1971               /* Check for constants that don't require emitting a reloc.  */
1972               if (the_insn.exp.X_op == O_constant
1973                   && the_insn.exp.X_add_symbol == 0
1974                   && the_insn.exp.X_op_symbol == 0)
1975                 {
1976                   /* For pc-relative call instructions, we reject
1977                      constants to get better code.  */
1978                   if (the_insn.pcrel
1979                       && the_insn.reloc == BFD_RELOC_32_PCREL_S2
1980                       && in_signed_range (the_insn.exp.X_add_number, 0x3fff))
1981                     {
1982                       error_message = _(": PC-relative operand can't be a constant");
1983                       goto error;
1984                     }
1985
1986                   /* Constants that won't fit are checked in md_apply_fix3
1987                      and bfd_install_relocation.
1988                      ??? It would be preferable to install the constants
1989                      into the insn here and save having to create a fixS
1990                      for each one.  There already exists code to handle
1991                      all the various cases (e.g. in md_apply_fix3 and
1992                      bfd_install_relocation) so duplicating all that code
1993                      here isn't right.  */
1994                 }
1995
1996               continue;
1997
1998             case 'a':
1999               if (*s++ == 'a')
2000                 {
2001                   opcode |= ANNUL;
2002                   continue;
2003                 }
2004               break;
2005
2006             case 'A':
2007               {
2008                 int asi = 0;
2009
2010                 /* Parse an asi.  */
2011                 if (*s == '#')
2012                   {
2013                     if (! parse_keyword_arg (sparc_encode_asi, &s, &asi))
2014                       {
2015                         error_message = _(": invalid ASI name");
2016                         goto error;
2017                       }
2018                   }
2019                 else
2020                   {
2021                     if (! parse_const_expr_arg (&s, &asi))
2022                       {
2023                         error_message = _(": invalid ASI expression");
2024                         goto error;
2025                       }
2026                     if (asi < 0 || asi > 255)
2027                       {
2028                         error_message = _(": invalid ASI number");
2029                         goto error;
2030                       }
2031                   }
2032                 opcode |= ASI (asi);
2033                 continue;
2034               }                 /* alternate space */
2035
2036             case 'p':
2037               if (strncmp (s, "%psr", 4) == 0)
2038                 {
2039                   s += 4;
2040                   continue;
2041                 }
2042               break;
2043
2044             case 'q':           /* floating point queue */
2045               if (strncmp (s, "%fq", 3) == 0)
2046                 {
2047                   s += 3;
2048                   continue;
2049                 }
2050               break;
2051
2052             case 'Q':           /* coprocessor queue */
2053               if (strncmp (s, "%cq", 3) == 0)
2054                 {
2055                   s += 3;
2056                   continue;
2057                 }
2058               break;
2059
2060             case 'S':
2061               if (strcmp (str, "set") == 0
2062                   || strcmp (str, "setuw") == 0)
2063                 {
2064                   special_case = SPECIAL_CASE_SET;
2065                   continue;
2066                 }
2067               else if (strcmp (str, "setsw") == 0)
2068                 {
2069                   special_case = SPECIAL_CASE_SETSW;
2070                   continue;
2071                 }
2072               else if (strcmp (str, "setx") == 0)
2073                 {
2074                   special_case = SPECIAL_CASE_SETX;
2075                   continue;
2076                 }
2077               else if (strncmp (str, "fdiv", 4) == 0)
2078                 {
2079                   special_case = SPECIAL_CASE_FDIV;
2080                   continue;
2081                 }
2082               break;
2083
2084             case 'o':
2085               if (strncmp (s, "%asi", 4) != 0)
2086                 break;
2087               s += 4;
2088               continue;
2089
2090             case 's':
2091               if (strncmp (s, "%fprs", 5) != 0)
2092                 break;
2093               s += 5;
2094               continue;
2095
2096             case 'E':
2097               if (strncmp (s, "%ccr", 4) != 0)
2098                 break;
2099               s += 4;
2100               continue;
2101
2102             case 't':
2103               if (strncmp (s, "%tbr", 4) != 0)
2104                 break;
2105               s += 4;
2106               continue;
2107
2108             case 'w':
2109               if (strncmp (s, "%wim", 4) != 0)
2110                 break;
2111               s += 4;
2112               continue;
2113
2114             case 'x':
2115               {
2116                 char *push = input_line_pointer;
2117                 expressionS e;
2118
2119                 input_line_pointer = s;
2120                 expression (&e);
2121                 if (e.X_op == O_constant)
2122                   {
2123                     int n = e.X_add_number;
2124                     if (n != e.X_add_number || (n & ~0x1ff) != 0)
2125                       as_bad (_("OPF immediate operand out of range (0-0x1ff)"));
2126                     else
2127                       opcode |= e.X_add_number << 5;
2128                   }
2129                 else
2130                   as_bad (_("non-immediate OPF operand, ignored"));
2131                 s = input_line_pointer;
2132                 input_line_pointer = push;
2133                 continue;
2134               }
2135
2136             case 'y':
2137               if (strncmp (s, "%y", 2) != 0)
2138                 break;
2139               s += 2;
2140               continue;
2141
2142             case 'u':
2143             case 'U':
2144               {
2145                 /* Parse a sparclet cpreg.  */
2146                 int cpreg;
2147                 if (! parse_keyword_arg (sparc_encode_sparclet_cpreg, &s, &cpreg))
2148                   {
2149                     error_message = _(": invalid cpreg name");
2150                     goto error;
2151                   }
2152                 opcode |= (*args == 'U' ? RS1 (cpreg) : RD (cpreg));
2153                 continue;
2154               }
2155
2156             default:
2157               as_fatal (_("failed sanity check."));
2158             }                   /* switch on arg code */
2159
2160           /* Break out of for() loop.  */
2161           break;
2162         }                       /* for each arg that we expect */
2163
2164     error:
2165       if (match == 0)
2166         {
2167           /* Args don't match. */
2168           if (&insn[1] - sparc_opcodes < sparc_num_opcodes
2169               && (insn->name == insn[1].name
2170                   || !strcmp (insn->name, insn[1].name)))
2171             {
2172               ++insn;
2173               s = argsStart;
2174               continue;
2175             }
2176           else
2177             {
2178               as_bad (_("Illegal operands%s"), error_message);
2179               return;
2180             }
2181         }
2182       else
2183         {
2184           /* We have a match.  Now see if the architecture is ok.  */
2185           int needed_arch_mask = insn->architecture;
2186
2187           if (v9_arg_p)
2188             {
2189               needed_arch_mask &= ~ ((1 << SPARC_OPCODE_ARCH_V9)
2190                                      | (1 << SPARC_OPCODE_ARCH_V9A));
2191               needed_arch_mask |= (1 << SPARC_OPCODE_ARCH_V9);
2192             }
2193
2194           if (needed_arch_mask & SPARC_OPCODE_SUPPORTED (current_architecture))
2195             ; /* ok */
2196           /* Can we bump up the architecture?  */
2197           else if (needed_arch_mask & SPARC_OPCODE_SUPPORTED (max_architecture))
2198             {
2199               enum sparc_opcode_arch_val needed_architecture =
2200                 sparc_ffs (SPARC_OPCODE_SUPPORTED (max_architecture)
2201                            & needed_arch_mask);
2202
2203               assert (needed_architecture <= SPARC_OPCODE_ARCH_MAX);
2204               if (warn_on_bump
2205                   && needed_architecture > warn_after_architecture)
2206                 {
2207                   as_warn (_("architecture bumped from \"%s\" to \"%s\" on \"%s\""),
2208                            sparc_opcode_archs[current_architecture].name,
2209                            sparc_opcode_archs[needed_architecture].name,
2210                            str);
2211                   warn_after_architecture = needed_architecture;
2212                 }
2213               current_architecture = needed_architecture;
2214             }
2215           /* Conflict.  */
2216           /* ??? This seems to be a bit fragile.  What if the next entry in
2217              the opcode table is the one we want and it is supported?
2218              It is possible to arrange the table today so that this can't
2219              happen but what about tomorrow?  */
2220           else
2221             {
2222               int arch,printed_one_p = 0;
2223               char *p;
2224               char required_archs[SPARC_OPCODE_ARCH_MAX * 16];
2225
2226               /* Create a list of the architectures that support the insn.  */
2227               needed_arch_mask &= ~ SPARC_OPCODE_SUPPORTED (max_architecture);
2228               p = required_archs;
2229               arch = sparc_ffs (needed_arch_mask);
2230               while ((1 << arch) <= needed_arch_mask)
2231                 {
2232                   if ((1 << arch) & needed_arch_mask)
2233                     {
2234                       if (printed_one_p)
2235                         *p++ = '|';
2236                       strcpy (p, sparc_opcode_archs[arch].name);
2237                       p += strlen (p);
2238                       printed_one_p = 1;
2239                     }
2240                   ++arch;
2241                 }
2242
2243               as_bad (_("Architecture mismatch on \"%s\"."), str);
2244               as_tsktsk (_(" (Requires %s; requested architecture is %s.)"),
2245                          required_archs,
2246                          sparc_opcode_archs[max_architecture].name);
2247               return;
2248             }
2249         } /* if no match */
2250
2251       break;
2252     } /* forever looking for a match */
2253
2254   the_insn.opcode = opcode;
2255 }
2256
2257 /* Parse an argument that can be expressed as a keyword.
2258    (eg: #StoreStore or %ccfr).
2259    The result is a boolean indicating success.
2260    If successful, INPUT_POINTER is updated.  */
2261
2262 static int
2263 parse_keyword_arg (lookup_fn, input_pointerP, valueP)
2264      int (*lookup_fn) PARAMS ((const char *));
2265      char **input_pointerP;
2266      int *valueP;
2267 {
2268   int value;
2269   char c, *p, *q;
2270
2271   p = *input_pointerP;
2272   for (q = p + (*p == '#' || *p == '%');
2273        isalnum ((unsigned char) *q) || *q == '_';
2274        ++q)
2275     continue;
2276   c = *q;
2277   *q = 0;
2278   value = (*lookup_fn) (p);
2279   *q = c;
2280   if (value == -1)
2281     return 0;
2282   *valueP = value;
2283   *input_pointerP = q;
2284   return 1;
2285 }
2286
2287 /* Parse an argument that is a constant expression.
2288    The result is a boolean indicating success.  */
2289
2290 static int
2291 parse_const_expr_arg (input_pointerP, valueP)
2292      char **input_pointerP;
2293      int *valueP;
2294 {
2295   char *save = input_line_pointer;
2296   expressionS exp;
2297
2298   input_line_pointer = *input_pointerP;
2299   /* The next expression may be something other than a constant
2300      (say if we're not processing the right variant of the insn).
2301      Don't call expression unless we're sure it will succeed as it will
2302      signal an error (which we want to defer until later).  */
2303   /* FIXME: It might be better to define md_operand and have it recognize
2304      things like %asi, etc. but continuing that route through to the end
2305      is a lot of work.  */
2306   if (*input_line_pointer == '%')
2307     {
2308       input_line_pointer = save;
2309       return 0;
2310     }
2311   expression (&exp);
2312   *input_pointerP = input_line_pointer;
2313   input_line_pointer = save;
2314   if (exp.X_op != O_constant)
2315     return 0;
2316   *valueP = exp.X_add_number;
2317   return 1;
2318 }
2319
2320 /* Subroutine of sparc_ip to parse an expression.  */
2321
2322 static int
2323 get_expression (str)
2324      char *str;
2325 {
2326   char *save_in;
2327   segT seg;
2328
2329   save_in = input_line_pointer;
2330   input_line_pointer = str;
2331   seg = expression (&the_insn.exp);
2332   if (seg != absolute_section
2333       && seg != text_section
2334       && seg != data_section
2335       && seg != bss_section
2336       && seg != undefined_section)
2337     {
2338       the_insn.error = _("bad segment");
2339       expr_end = input_line_pointer;
2340       input_line_pointer = save_in;
2341       return 1;
2342     }
2343   expr_end = input_line_pointer;
2344   input_line_pointer = save_in;
2345   return 0;
2346 }
2347
2348 /* Subroutine of md_assemble to output one insn.  */
2349
2350 static void
2351 output_insn (insn, the_insn)
2352      const struct sparc_opcode *insn;
2353      struct sparc_it *the_insn;
2354 {
2355   char *toP = frag_more (4);
2356
2357   /* put out the opcode */
2358   if (INSN_BIG_ENDIAN)
2359     number_to_chars_bigendian (toP, (valueT) the_insn->opcode, 4);
2360   else
2361     number_to_chars_littleendian (toP, (valueT) the_insn->opcode, 4);
2362
2363   /* put out the symbol-dependent stuff */
2364   if (the_insn->reloc != BFD_RELOC_NONE)
2365     {
2366       fixS *fixP =  fix_new_exp (frag_now,      /* which frag */
2367                                  (toP - frag_now->fr_literal),  /* where */
2368                                  4,             /* size */
2369                                  &the_insn->exp,
2370                                  the_insn->pcrel,
2371                                  the_insn->reloc);
2372       /* Turn off overflow checking in fixup_segment.  We'll do our
2373          own overflow checking in md_apply_fix3.  This is necessary because
2374          the insn size is 4 and fixup_segment will signal an overflow for
2375          large 8 byte quantities.  */
2376       fixP->fx_no_overflow = 1;
2377     }
2378
2379   last_insn = insn;
2380   last_opcode = the_insn->opcode;
2381 }
2382 \f
2383 /*
2384   This is identical to the md_atof in m68k.c.  I think this is right,
2385   but I'm not sure.
2386
2387   Turn a string in input_line_pointer into a floating point constant of type
2388   type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
2389   emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
2390   */
2391
2392 /* Equal to MAX_PRECISION in atof-ieee.c */
2393 #define MAX_LITTLENUMS 6
2394
2395 char *
2396 md_atof (type, litP, sizeP)
2397      char type;
2398      char *litP;
2399      int *sizeP;
2400 {
2401   int i,prec;
2402   LITTLENUM_TYPE words[MAX_LITTLENUMS];
2403   char *t;
2404
2405   switch (type)
2406     {
2407     case 'f':
2408     case 'F':
2409     case 's':
2410     case 'S':
2411       prec = 2;
2412       break;
2413
2414     case 'd':
2415     case 'D':
2416     case 'r':
2417     case 'R':
2418       prec = 4;
2419       break;
2420
2421     case 'x':
2422     case 'X':
2423       prec = 6;
2424       break;
2425
2426     case 'p':
2427     case 'P':
2428       prec = 6;
2429       break;
2430
2431     default:
2432       *sizeP = 0;
2433       return _("Bad call to MD_ATOF()");
2434     }
2435
2436   t = atof_ieee (input_line_pointer, type, words);
2437   if (t)
2438     input_line_pointer = t;
2439   *sizeP = prec * sizeof (LITTLENUM_TYPE);
2440
2441   if (target_big_endian)
2442     {
2443       for (i = 0; i < prec; i++)
2444         {
2445           md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
2446           litP += sizeof (LITTLENUM_TYPE);
2447         }
2448     }
2449   else
2450     {
2451       for (i = prec - 1; i >= 0; i--)
2452         {
2453           md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
2454           litP += sizeof (LITTLENUM_TYPE);
2455         }
2456     }
2457      
2458   return 0;
2459 }
2460
2461 /* Write a value out to the object file, using the appropriate
2462    endianness.  */
2463
2464 void
2465 md_number_to_chars (buf, val, n)
2466      char *buf;
2467      valueT val;
2468      int n;
2469 {
2470   if (target_big_endian)
2471     number_to_chars_bigendian (buf, val, n);
2472   else if (target_little_endian_data
2473            && ((n == 4 || n == 2) && ~now_seg->flags & SEC_ALLOC))
2474     /* Output debug words, which are not in allocated sections, as big endian */
2475     number_to_chars_bigendian (buf, val, n);
2476   else if (target_little_endian_data || ! target_big_endian)
2477     number_to_chars_littleendian (buf, val, n);
2478 }
2479 \f
2480 /* Apply a fixS to the frags, now that we know the value it ought to
2481    hold. */
2482
2483 int
2484 md_apply_fix3 (fixP, value, segment)
2485      fixS *fixP;
2486      valueT *value;
2487      segT segment;
2488 {
2489   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
2490   offsetT val;
2491   long insn;
2492
2493   val = *value;
2494
2495   assert (fixP->fx_r_type < BFD_RELOC_UNUSED);
2496
2497   fixP->fx_addnumber = val;     /* Remember value for emit_reloc */
2498
2499 #ifdef OBJ_ELF
2500   /* FIXME: SPARC ELF relocations don't use an addend in the data
2501      field itself.  This whole approach should be somehow combined
2502      with the calls to bfd_install_relocation.  Also, the value passed
2503      in by fixup_segment includes the value of a defined symbol.  We
2504      don't want to include the value of an externally visible symbol.  */
2505   if (fixP->fx_addsy != NULL)
2506     {
2507       if (fixP->fx_addsy->sy_used_in_reloc
2508           && (S_IS_EXTERNAL (fixP->fx_addsy)
2509               || S_IS_WEAK (fixP->fx_addsy)
2510               || (sparc_pic_code && ! fixP->fx_pcrel)
2511               || (S_GET_SEGMENT (fixP->fx_addsy) != segment
2512                   && ((bfd_get_section_flags (stdoutput,
2513                                               S_GET_SEGMENT (fixP->fx_addsy))
2514                        & SEC_LINK_ONCE) != 0
2515                       || strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
2516                                   ".gnu.linkonce",
2517                                   sizeof ".gnu.linkonce" - 1) == 0)))
2518           && S_GET_SEGMENT (fixP->fx_addsy) != absolute_section
2519           && S_GET_SEGMENT (fixP->fx_addsy) != undefined_section
2520           && ! bfd_is_com_section (S_GET_SEGMENT (fixP->fx_addsy)))
2521         fixP->fx_addnumber -= S_GET_VALUE (fixP->fx_addsy);
2522       return 1;
2523     }
2524 #endif
2525
2526   /* This is a hack.  There should be a better way to
2527      handle this.  Probably in terms of howto fields, once
2528      we can look at these fixups in terms of howtos.  */
2529   if (fixP->fx_r_type == BFD_RELOC_32_PCREL_S2 && fixP->fx_addsy)
2530     val += fixP->fx_where + fixP->fx_frag->fr_address;
2531
2532 #ifdef OBJ_AOUT
2533   /* FIXME: More ridiculous gas reloc hacking.  If we are going to
2534      generate a reloc, then we just want to let the reloc addend set
2535      the value.  We do not want to also stuff the addend into the
2536      object file.  Including the addend in the object file works when
2537      doing a static link, because the linker will ignore the object
2538      file contents.  However, the dynamic linker does not ignore the
2539      object file contents.  */
2540   if (fixP->fx_addsy != NULL
2541       && fixP->fx_r_type != BFD_RELOC_32_PCREL_S2)
2542     val = 0;
2543
2544   /* When generating PIC code, we do not want an addend for a reloc
2545      against a local symbol.  We adjust fx_addnumber to cancel out the
2546      value already included in val, and to also cancel out the
2547      adjustment which bfd_install_relocation will create.  */
2548   if (sparc_pic_code
2549       && fixP->fx_r_type != BFD_RELOC_32_PCREL_S2
2550       && fixP->fx_addsy != NULL
2551       && ! S_IS_COMMON (fixP->fx_addsy)
2552       && (fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) == 0)
2553     fixP->fx_addnumber -= 2 * S_GET_VALUE (fixP->fx_addsy);
2554
2555   /* When generating PIC code, we need to fiddle to get
2556      bfd_install_relocation to do the right thing for a PC relative
2557      reloc against a local symbol which we are going to keep.  */
2558   if (sparc_pic_code
2559       && fixP->fx_r_type == BFD_RELOC_32_PCREL_S2
2560       && fixP->fx_addsy != NULL
2561       && (S_IS_EXTERNAL (fixP->fx_addsy)
2562           || S_IS_WEAK (fixP->fx_addsy))
2563       && S_IS_DEFINED (fixP->fx_addsy)
2564       && ! S_IS_COMMON (fixP->fx_addsy))
2565     {
2566       val = 0;
2567       fixP->fx_addnumber -= 2 * S_GET_VALUE (fixP->fx_addsy);
2568     }
2569 #endif
2570
2571   /* If this is a data relocation, just output VAL.  */
2572
2573   if (fixP->fx_r_type == BFD_RELOC_16)
2574     {
2575       md_number_to_chars (buf, val, 2);
2576     }
2577   else if (fixP->fx_r_type == BFD_RELOC_32
2578            || fixP->fx_r_type == BFD_RELOC_SPARC_REV32)
2579     {
2580       md_number_to_chars (buf, val, 4);
2581     }
2582   else if (fixP->fx_r_type == BFD_RELOC_64)
2583     {
2584       md_number_to_chars (buf, val, 8);
2585     }
2586   else if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT 
2587            || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2588     {
2589       fixP->fx_done = 0;
2590       return 1;
2591     }
2592   else
2593     {
2594       /* It's a relocation against an instruction.  */
2595
2596       if (INSN_BIG_ENDIAN)
2597         insn = bfd_getb32 ((unsigned char *) buf);
2598       else
2599         insn = bfd_getl32 ((unsigned char *) buf);
2600     
2601       switch (fixP->fx_r_type)
2602         {
2603         case BFD_RELOC_32_PCREL_S2:
2604           val = val >> 2;
2605           /* FIXME: This increment-by-one deserves a comment of why it's
2606              being done!  */
2607           if (! sparc_pic_code
2608               || fixP->fx_addsy == NULL
2609               || (fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) != 0)
2610             ++val;
2611           insn |= val & 0x3fffffff;
2612           break;
2613
2614         case BFD_RELOC_SPARC_11:
2615           if (! in_signed_range (val, 0x7ff))
2616             as_bad_where (fixP->fx_file, fixP->fx_line,
2617                           _("relocation overflow"));
2618           insn |= val & 0x7ff;
2619           break;
2620
2621         case BFD_RELOC_SPARC_10:
2622           if (! in_signed_range (val, 0x3ff))
2623             as_bad_where (fixP->fx_file, fixP->fx_line,
2624                           _("relocation overflow"));
2625           insn |= val & 0x3ff;
2626           break;
2627
2628         case BFD_RELOC_SPARC_7:
2629           if (! in_bitfield_range (val, 0x7f))
2630             as_bad_where (fixP->fx_file, fixP->fx_line,
2631                           _("relocation overflow"));
2632           insn |= val & 0x7f;
2633           break;
2634
2635         case BFD_RELOC_SPARC_6:
2636           if (! in_bitfield_range (val, 0x3f))
2637             as_bad_where (fixP->fx_file, fixP->fx_line,
2638                           _("relocation overflow"));
2639           insn |= val & 0x3f;
2640           break;
2641
2642         case BFD_RELOC_SPARC_5:
2643           if (! in_bitfield_range (val, 0x1f))
2644             as_bad_where (fixP->fx_file, fixP->fx_line,
2645                           _("relocation overflow"));
2646           insn |= val & 0x1f;
2647           break;
2648
2649         case BFD_RELOC_SPARC_WDISP16:
2650           /* FIXME: simplify */
2651           if (((val > 0) && (val & ~0x3fffc))
2652               || ((val < 0) && (~(val - 1) & ~0x3fffc)))
2653             as_bad_where (fixP->fx_file, fixP->fx_line,
2654                           _("relocation overflow"));
2655           /* FIXME: The +1 deserves a comment.  */
2656           val = (val >> 2) + 1;
2657           insn |= ((val & 0xc000) << 6) | (val & 0x3fff);
2658           break;
2659
2660         case BFD_RELOC_SPARC_WDISP19:
2661           /* FIXME: simplify */
2662           if (((val > 0) && (val & ~0x1ffffc))
2663               || ((val < 0) && (~(val - 1) & ~0x1ffffc)))
2664             as_bad_where (fixP->fx_file, fixP->fx_line,
2665                           _("relocation overflow"));
2666           /* FIXME: The +1 deserves a comment.  */
2667           val = (val >> 2) + 1;
2668           insn |= val & 0x7ffff;
2669           break;
2670
2671         case BFD_RELOC_SPARC_HH22:
2672           val = BSR (val, 32);
2673           /* intentional fallthrough */
2674
2675         case BFD_RELOC_SPARC_LM22:
2676         case BFD_RELOC_HI22:
2677           if (!fixP->fx_addsy)
2678             {
2679               insn |= (val >> 10) & 0x3fffff;
2680             }
2681           else
2682             {
2683               /* FIXME: Need comment explaining why we do this.  */
2684               insn &= ~0xffff;
2685             }
2686           break;
2687
2688         case BFD_RELOC_SPARC22:
2689           if (val & ~0x003fffff)
2690             as_bad_where (fixP->fx_file, fixP->fx_line,
2691                           _("relocation overflow"));
2692           insn |= (val & 0x3fffff);
2693           break;
2694
2695         case BFD_RELOC_SPARC_HM10:
2696           val = BSR (val, 32);
2697           /* intentional fallthrough */
2698
2699         case BFD_RELOC_LO10:
2700           if (!fixP->fx_addsy)
2701             {
2702               insn |= val & 0x3ff;
2703             }
2704           else
2705             {
2706               /* FIXME: Need comment explaining why we do this.  */
2707               insn &= ~0xff;
2708             }
2709           break;
2710
2711         case BFD_RELOC_SPARC13:
2712           if (! in_signed_range (val, 0x1fff))
2713             as_bad_where (fixP->fx_file, fixP->fx_line,
2714                           _("relocation overflow"));
2715           insn |= val & 0x1fff;
2716           break;
2717
2718         case BFD_RELOC_SPARC_WDISP22:
2719           val = (val >> 2) + 1;
2720           /* FALLTHROUGH */
2721         case BFD_RELOC_SPARC_BASE22:
2722           insn |= val & 0x3fffff;
2723           break;
2724
2725         case BFD_RELOC_SPARC_H44:
2726           if (!fixP->fx_addsy)
2727             {
2728               bfd_vma tval = val;
2729               tval >>= 22;
2730               insn |= tval & 0x3fffff;
2731             }
2732           break;
2733
2734         case BFD_RELOC_SPARC_M44:
2735           if (!fixP->fx_addsy)
2736             insn |= (val >> 12) & 0x3ff;
2737           break;
2738
2739         case BFD_RELOC_SPARC_L44:
2740           if (!fixP->fx_addsy)
2741             insn |= val & 0xfff;
2742           break;
2743
2744         case BFD_RELOC_SPARC_HIX22:
2745           if (!fixP->fx_addsy)
2746             {
2747               val ^= ~ (offsetT) 0;
2748               insn |= (val >> 10) & 0x3fffff;
2749             }
2750           break;
2751
2752         case BFD_RELOC_SPARC_LOX10:
2753           if (!fixP->fx_addsy)
2754             insn |= 0x1c00 | (val & 0x3ff);
2755           break;
2756
2757         case BFD_RELOC_NONE:
2758         default:
2759           as_bad_where (fixP->fx_file, fixP->fx_line,
2760                         _("bad or unhandled relocation type: 0x%02x"),
2761                         fixP->fx_r_type);
2762           break;
2763         }
2764
2765       if (INSN_BIG_ENDIAN)
2766         bfd_putb32 (insn, (unsigned char *) buf);
2767       else
2768         bfd_putl32 (insn, (unsigned char *) buf);
2769     }
2770
2771   /* Are we finished with this relocation now?  */
2772   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
2773     fixP->fx_done = 1;
2774
2775   return 1;
2776 }
2777
2778 /* Translate internal representation of relocation info to BFD target
2779    format.  */
2780 arelent *
2781 tc_gen_reloc (section, fixp)
2782      asection *section;
2783      fixS *fixp;
2784 {
2785   arelent *reloc;
2786   bfd_reloc_code_real_type code;
2787
2788   reloc = (arelent *) xmalloc (sizeof (arelent));
2789
2790   reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
2791   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2792
2793   switch (fixp->fx_r_type)
2794     {
2795     case BFD_RELOC_16:
2796     case BFD_RELOC_32:
2797     case BFD_RELOC_HI22:
2798     case BFD_RELOC_LO10:
2799     case BFD_RELOC_32_PCREL_S2:
2800     case BFD_RELOC_SPARC13:
2801     case BFD_RELOC_SPARC_BASE13:
2802     case BFD_RELOC_SPARC_WDISP16:
2803     case BFD_RELOC_SPARC_WDISP19:
2804     case BFD_RELOC_SPARC_WDISP22:
2805     case BFD_RELOC_64:
2806     case BFD_RELOC_SPARC_5:
2807     case BFD_RELOC_SPARC_6:
2808     case BFD_RELOC_SPARC_7:
2809     case BFD_RELOC_SPARC_10:
2810     case BFD_RELOC_SPARC_11:
2811     case BFD_RELOC_SPARC_HH22:
2812     case BFD_RELOC_SPARC_HM10:
2813     case BFD_RELOC_SPARC_LM22:
2814     case BFD_RELOC_SPARC_PC_HH22:
2815     case BFD_RELOC_SPARC_PC_HM10:
2816     case BFD_RELOC_SPARC_PC_LM22:
2817     case BFD_RELOC_SPARC_H44:
2818     case BFD_RELOC_SPARC_M44:
2819     case BFD_RELOC_SPARC_L44:
2820     case BFD_RELOC_SPARC_HIX22:
2821     case BFD_RELOC_SPARC_LOX10:
2822     case BFD_RELOC_SPARC_REV32:
2823     case BFD_RELOC_VTABLE_ENTRY:
2824     case BFD_RELOC_VTABLE_INHERIT:
2825       code = fixp->fx_r_type;
2826       break;
2827     default:
2828       abort ();
2829       return NULL;
2830     }
2831
2832 #if defined (OBJ_ELF) || defined (OBJ_AOUT)
2833   /* If we are generating PIC code, we need to generate a different
2834      set of relocs.  */
2835
2836 #ifdef OBJ_ELF
2837 #define GOT_NAME "_GLOBAL_OFFSET_TABLE_"
2838 #else
2839 #define GOT_NAME "__GLOBAL_OFFSET_TABLE_"
2840 #endif
2841
2842   if (sparc_pic_code)
2843     {
2844       switch (code)
2845         {
2846         case BFD_RELOC_32_PCREL_S2:
2847           if (! S_IS_DEFINED (fixp->fx_addsy)
2848               || S_IS_COMMON (fixp->fx_addsy)
2849               || S_IS_EXTERNAL (fixp->fx_addsy)
2850               || S_IS_WEAK (fixp->fx_addsy))
2851             code = BFD_RELOC_SPARC_WPLT30;
2852           break;
2853         case BFD_RELOC_HI22:
2854           if (fixp->fx_addsy != NULL
2855               && strcmp (S_GET_NAME (fixp->fx_addsy), GOT_NAME) == 0)
2856             code = BFD_RELOC_SPARC_PC22;
2857           else
2858             code = BFD_RELOC_SPARC_GOT22;
2859           break;
2860         case BFD_RELOC_LO10:
2861           if (fixp->fx_addsy != NULL
2862               && strcmp (S_GET_NAME (fixp->fx_addsy), GOT_NAME) == 0)
2863             code = BFD_RELOC_SPARC_PC10;
2864           else
2865             code = BFD_RELOC_SPARC_GOT10;
2866           break;
2867         case BFD_RELOC_SPARC13:
2868           code = BFD_RELOC_SPARC_GOT13;
2869           break;
2870         default:
2871           break;
2872         }
2873     }
2874 #endif /* defined (OBJ_ELF) || defined (OBJ_AOUT) */
2875
2876   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
2877   if (reloc->howto == 0)
2878     {
2879       as_bad_where (fixp->fx_file, fixp->fx_line,
2880                     _("internal error: can't export reloc type %d (`%s')"),
2881                     fixp->fx_r_type, bfd_get_reloc_code_name (code));
2882       return 0;
2883     }
2884
2885   /* @@ Why fx_addnumber sometimes and fx_offset other times?  */
2886 #ifdef OBJ_AOUT
2887
2888   if (reloc->howto->pc_relative == 0
2889       || code == BFD_RELOC_SPARC_PC10
2890       || code == BFD_RELOC_SPARC_PC22)
2891     reloc->addend = fixp->fx_addnumber;
2892   else if (sparc_pic_code
2893            && fixp->fx_r_type == BFD_RELOC_32_PCREL_S2
2894            && fixp->fx_addsy != NULL
2895            && (S_IS_EXTERNAL (fixp->fx_addsy)
2896                || S_IS_WEAK (fixp->fx_addsy))
2897            && S_IS_DEFINED (fixp->fx_addsy)
2898            && ! S_IS_COMMON (fixp->fx_addsy))
2899     reloc->addend = fixp->fx_addnumber;
2900   else
2901     reloc->addend = fixp->fx_offset - reloc->address;
2902
2903 #else /* elf or coff */
2904
2905   if (reloc->howto->pc_relative == 0
2906       || code == BFD_RELOC_SPARC_PC10
2907       || code == BFD_RELOC_SPARC_PC22)
2908     reloc->addend = fixp->fx_addnumber;
2909   else if ((fixp->fx_addsy->bsym->flags & BSF_SECTION_SYM) != 0)
2910     reloc->addend = (section->vma
2911                      + fixp->fx_addnumber
2912                      + md_pcrel_from (fixp));
2913   else
2914     reloc->addend = fixp->fx_offset;
2915 #endif
2916
2917   return reloc;
2918 }
2919 \f
2920 /* We have no need to default values of symbols. */
2921
2922 /* ARGSUSED */
2923 symbolS *
2924 md_undefined_symbol (name)
2925      char *name;
2926 {
2927   return 0;
2928 }                               /* md_undefined_symbol() */
2929
2930 /* Round up a section size to the appropriate boundary. */
2931 valueT
2932 md_section_align (segment, size)
2933      segT segment;
2934      valueT size;
2935 {
2936 #ifndef OBJ_ELF
2937   /* This is not right for ELF; a.out wants it, and COFF will force
2938      the alignment anyways.  */
2939   valueT align = ((valueT) 1
2940                   << (valueT) bfd_get_section_alignment (stdoutput, segment));
2941   valueT newsize;
2942   /* turn alignment value into a mask */
2943   align--;
2944   newsize = (size + align) & ~align;
2945   return newsize;
2946 #else
2947   return size;
2948 #endif
2949 }
2950
2951 /* Exactly what point is a PC-relative offset relative TO?
2952    On the sparc, they're relative to the address of the offset, plus
2953    its size.  This gets us to the following instruction.
2954    (??? Is this right?  FIXME-SOON) */
2955 long 
2956 md_pcrel_from (fixP)
2957      fixS *fixP;
2958 {
2959   long ret;
2960
2961   ret = fixP->fx_where + fixP->fx_frag->fr_address;
2962   if (! sparc_pic_code
2963       || fixP->fx_addsy == NULL
2964       || (fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) != 0)
2965     ret += fixP->fx_size;
2966   return ret;
2967 }
2968 \f
2969 /* Return log2 (VALUE), or -1 if VALUE is not an exact positive power
2970    of two.  */
2971
2972 static int
2973 log2 (value)
2974      int value;
2975 {
2976   int shift;
2977
2978   if (value <= 0)
2979     return -1;
2980
2981   for (shift = 0; (value & 1) == 0; value >>= 1)
2982     ++shift;
2983
2984   return (value == 1) ? shift : -1;
2985 }
2986
2987 /*
2988  * sort of like s_lcomm
2989  */
2990
2991 #ifndef OBJ_ELF
2992 static int max_alignment = 15;
2993 #endif
2994
2995 static void
2996 s_reserve (ignore)
2997      int ignore;
2998 {
2999   char *name;
3000   char *p;
3001   char c;
3002   int align;
3003   int size;
3004   int temp;
3005   symbolS *symbolP;
3006
3007   name = input_line_pointer;
3008   c = get_symbol_end ();
3009   p = input_line_pointer;
3010   *p = c;
3011   SKIP_WHITESPACE ();
3012
3013   if (*input_line_pointer != ',')
3014     {
3015       as_bad (_("Expected comma after name"));
3016       ignore_rest_of_line ();
3017       return;
3018     }
3019
3020   ++input_line_pointer;
3021
3022   if ((size = get_absolute_expression ()) < 0)
3023     {
3024       as_bad (_("BSS length (%d.) <0! Ignored."), size);
3025       ignore_rest_of_line ();
3026       return;
3027     }                           /* bad length */
3028
3029   *p = 0;
3030   symbolP = symbol_find_or_make (name);
3031   *p = c;
3032
3033   if (strncmp (input_line_pointer, ",\"bss\"", 6) != 0
3034       && strncmp (input_line_pointer, ",\".bss\"", 7) != 0)
3035     {
3036       as_bad (_("bad .reserve segment -- expected BSS segment"));
3037       return;
3038     }
3039
3040   if (input_line_pointer[2] == '.')
3041     input_line_pointer += 7;
3042   else
3043     input_line_pointer += 6;
3044   SKIP_WHITESPACE ();
3045
3046   if (*input_line_pointer == ',')
3047     {
3048       ++input_line_pointer;
3049
3050       SKIP_WHITESPACE ();
3051       if (*input_line_pointer == '\n')
3052         {
3053           as_bad (_("missing alignment"));
3054           ignore_rest_of_line ();
3055           return;
3056         }
3057
3058       align = (int) get_absolute_expression ();
3059
3060 #ifndef OBJ_ELF
3061       if (align > max_alignment)
3062         {
3063           align = max_alignment;
3064           as_warn (_("alignment too large; assuming %d"), align);
3065         }
3066 #endif
3067
3068       if (align < 0)
3069         {
3070           as_bad (_("negative alignment"));
3071           ignore_rest_of_line ();
3072           return;
3073         }
3074
3075       if (align != 0)
3076         {
3077           temp = log2 (align);
3078           if (temp < 0)
3079             {
3080               as_bad (_("alignment not a power of 2"));
3081               ignore_rest_of_line ();
3082               return;
3083             }
3084
3085           align = temp;
3086         }
3087
3088       record_alignment (bss_section, align);
3089     }
3090   else
3091     align = 0;
3092
3093   if (!S_IS_DEFINED (symbolP)
3094 #ifdef OBJ_AOUT
3095       && S_GET_OTHER (symbolP) == 0
3096       && S_GET_DESC (symbolP) == 0
3097 #endif
3098       )
3099     {
3100       if (! need_pass_2)
3101         {
3102           char *pfrag;
3103           segT current_seg = now_seg;
3104           subsegT current_subseg = now_subseg;
3105
3106           subseg_set (bss_section, 1); /* switch to bss */
3107
3108           if (align)
3109             frag_align (align, 0, 0); /* do alignment */
3110
3111           /* detach from old frag */
3112           if (S_GET_SEGMENT(symbolP) == bss_section)
3113             symbolP->sy_frag->fr_symbol = NULL;
3114
3115           symbolP->sy_frag = frag_now;
3116           pfrag = frag_var (rs_org, 1, 1, (relax_substateT)0, symbolP,
3117                             (offsetT) size, (char *)0);
3118           *pfrag = 0;
3119
3120           S_SET_SEGMENT (symbolP, bss_section);
3121
3122           subseg_set (current_seg, current_subseg);
3123
3124 #ifdef OBJ_ELF
3125           S_SET_SIZE (symbolP, size);
3126 #endif
3127         }
3128     }
3129   else
3130     {
3131       as_warn("Ignoring attempt to re-define symbol %s",
3132               S_GET_NAME (symbolP));
3133     }                           /* if not redefining */
3134
3135   demand_empty_rest_of_line ();
3136 }
3137
3138 static void
3139 s_common (ignore)
3140      int ignore;
3141 {
3142   char *name;
3143   char c;
3144   char *p;
3145   int temp, size;
3146   symbolS *symbolP;
3147
3148   name = input_line_pointer;
3149   c = get_symbol_end ();
3150   /* just after name is now '\0' */
3151   p = input_line_pointer;
3152   *p = c;
3153   SKIP_WHITESPACE ();
3154   if (*input_line_pointer != ',')
3155     {
3156       as_bad (_("Expected comma after symbol-name"));
3157       ignore_rest_of_line ();
3158       return;
3159     }
3160   input_line_pointer++;         /* skip ',' */
3161   if ((temp = get_absolute_expression ()) < 0)
3162     {
3163       as_bad (_(".COMMon length (%d.) <0! Ignored."), temp);
3164       ignore_rest_of_line ();
3165       return;
3166     }
3167   size = temp;
3168   *p = 0;
3169   symbolP = symbol_find_or_make (name);
3170   *p = c;
3171   if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
3172     {
3173       as_bad (_("Ignoring attempt to re-define symbol"));
3174       ignore_rest_of_line ();
3175       return;
3176     }
3177   if (S_GET_VALUE (symbolP) != 0)
3178     {
3179       if (S_GET_VALUE (symbolP) != (valueT) size)
3180         {
3181           as_warn (_("Length of .comm \"%s\" is already %ld. Not changed to %d."),
3182                    S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
3183         }
3184     }
3185   else
3186     {
3187 #ifndef OBJ_ELF
3188       S_SET_VALUE (symbolP, (valueT) size);
3189       S_SET_EXTERNAL (symbolP);
3190 #endif
3191     }
3192   know (symbolP->sy_frag == &zero_address_frag);
3193   if (*input_line_pointer != ',')
3194     {
3195       as_bad (_("Expected comma after common length"));
3196       ignore_rest_of_line ();
3197       return;
3198     }
3199   input_line_pointer++;
3200   SKIP_WHITESPACE ();
3201   if (*input_line_pointer != '"')
3202     {
3203       temp = get_absolute_expression ();
3204
3205 #ifndef OBJ_ELF
3206       if (temp > max_alignment)
3207         {
3208           temp = max_alignment;
3209           as_warn (_("alignment too large; assuming %d"), temp);
3210         }
3211 #endif
3212
3213       if (temp < 0)
3214         {
3215           as_bad (_("negative alignment"));
3216           ignore_rest_of_line ();
3217           return;
3218         }
3219
3220 #ifdef OBJ_ELF
3221       if (symbolP->local)
3222         {
3223           segT old_sec;
3224           int old_subsec;
3225           char *p;
3226           int align;
3227
3228           old_sec = now_seg;
3229           old_subsec = now_subseg;
3230
3231           if (temp == 0)
3232             align = 0;
3233           else
3234             align = log2 (temp);
3235
3236           if (align < 0)
3237             {
3238               as_bad (_("alignment not a power of 2"));
3239               ignore_rest_of_line ();
3240               return;
3241             }
3242
3243           record_alignment (bss_section, align);
3244           subseg_set (bss_section, 0);
3245           if (align)
3246             frag_align (align, 0, 0);
3247           if (S_GET_SEGMENT (symbolP) == bss_section)
3248             symbolP->sy_frag->fr_symbol = 0;
3249           symbolP->sy_frag = frag_now;
3250           p = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
3251                         (offsetT) size, (char *) 0);
3252           *p = 0;
3253           S_SET_SEGMENT (symbolP, bss_section);
3254           S_CLEAR_EXTERNAL (symbolP);
3255           S_SET_SIZE (symbolP, size);
3256           subseg_set (old_sec, old_subsec);
3257         }
3258       else
3259 #endif /* OBJ_ELF */
3260         {
3261         allocate_common:
3262           S_SET_VALUE (symbolP, (valueT) size);
3263 #ifdef OBJ_ELF
3264           S_SET_ALIGN (symbolP, temp);
3265           S_SET_SIZE (symbolP, size);
3266 #endif
3267           S_SET_EXTERNAL (symbolP);
3268           S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
3269         }
3270     }
3271   else
3272     {
3273       input_line_pointer++;
3274       /* @@ Some use the dot, some don't.  Can we get some consistency??  */
3275       if (*input_line_pointer == '.')
3276         input_line_pointer++;
3277       /* @@ Some say data, some say bss.  */
3278       if (strncmp (input_line_pointer, "bss\"", 4)
3279           && strncmp (input_line_pointer, "data\"", 5))
3280         {
3281           while (*--input_line_pointer != '"')
3282             ;
3283           input_line_pointer--;
3284           goto bad_common_segment;
3285         }
3286       while (*input_line_pointer++ != '"')
3287         ;
3288       goto allocate_common;
3289     }
3290
3291 #ifdef BFD_ASSEMBLER
3292   symbolP->bsym->flags |= BSF_OBJECT;
3293 #endif
3294
3295   demand_empty_rest_of_line ();
3296   return;
3297
3298   {
3299   bad_common_segment:
3300     p = input_line_pointer;
3301     while (*p && *p != '\n')
3302       p++;
3303     c = *p;
3304     *p = '\0';
3305     as_bad (_("bad .common segment %s"), input_line_pointer + 1);
3306     *p = c;
3307     input_line_pointer = p;
3308     ignore_rest_of_line ();
3309     return;
3310   }
3311 }
3312
3313 /* Handle the .empty pseudo-op.  This supresses the warnings about
3314    invalid delay slot usage.  */
3315
3316 static void
3317 s_empty (ignore)
3318      int ignore;
3319 {
3320   /* The easy way to implement is to just forget about the last
3321      instruction.  */
3322   last_insn = NULL;
3323 }
3324
3325 static void
3326 s_seg (ignore)
3327      int ignore;
3328 {
3329
3330   if (strncmp (input_line_pointer, "\"text\"", 6) == 0)
3331     {
3332       input_line_pointer += 6;
3333       s_text (0);
3334       return;
3335     }
3336   if (strncmp (input_line_pointer, "\"data\"", 6) == 0)
3337     {
3338       input_line_pointer += 6;
3339       s_data (0);
3340       return;
3341     }
3342   if (strncmp (input_line_pointer, "\"data1\"", 7) == 0)
3343     {
3344       input_line_pointer += 7;
3345       s_data1 ();
3346       return;
3347     }
3348   if (strncmp (input_line_pointer, "\"bss\"", 5) == 0)
3349     {
3350       input_line_pointer += 5;
3351       /* We only support 2 segments -- text and data -- for now, so
3352          things in the "bss segment" will have to go into data for now.
3353          You can still allocate SEG_BSS stuff with .lcomm or .reserve. */
3354       subseg_set (data_section, 255);   /* FIXME-SOMEDAY */
3355       return;
3356     }
3357   as_bad (_("Unknown segment type"));
3358   demand_empty_rest_of_line ();
3359 }
3360
3361 static void
3362 s_data1 ()
3363 {
3364   subseg_set (data_section, 1);
3365   demand_empty_rest_of_line ();
3366 }
3367
3368 static void
3369 s_proc (ignore)
3370      int ignore;
3371 {
3372   while (!is_end_of_line[(unsigned char) *input_line_pointer])
3373     {
3374       ++input_line_pointer;
3375     }
3376   ++input_line_pointer;
3377 }
3378
3379 /* This static variable is set by s_uacons to tell sparc_cons_align
3380    that the expession does not need to be aligned.  */
3381
3382 static int sparc_no_align_cons = 0;
3383
3384 /* This handles the unaligned space allocation pseudo-ops, such as
3385    .uaword.  .uaword is just like .word, but the value does not need
3386    to be aligned.  */
3387
3388 static void
3389 s_uacons (bytes)
3390      int bytes;
3391 {
3392   /* Tell sparc_cons_align not to align this value.  */
3393   sparc_no_align_cons = 1;
3394   cons (bytes);
3395 }
3396
3397 /* If the --enforce-aligned-data option is used, we require .word,
3398    et. al., to be aligned correctly.  We do it by setting up an
3399    rs_align_code frag, and checking in HANDLE_ALIGN to make sure that
3400    no unexpected alignment was introduced.
3401
3402    The SunOS and Solaris native assemblers enforce aligned data by
3403    default.  We don't want to do that, because gcc can deliberately
3404    generate misaligned data if the packed attribute is used.  Instead,
3405    we permit misaligned data by default, and permit the user to set an
3406    option to check for it.  */
3407
3408 void
3409 sparc_cons_align (nbytes)
3410      int nbytes;
3411 {
3412   int nalign;
3413   char *p;
3414
3415   /* Only do this if we are enforcing aligned data.  */
3416   if (! enforce_aligned_data)
3417     return;
3418
3419   if (sparc_no_align_cons)
3420     {
3421       /* This is an unaligned pseudo-op.  */
3422       sparc_no_align_cons = 0;
3423       return;
3424     }
3425
3426   nalign = log2 (nbytes);
3427   if (nalign == 0)
3428     return;
3429
3430   assert (nalign > 0);
3431
3432   if (now_seg == absolute_section)
3433     {
3434       if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
3435         as_bad (_("misaligned data"));
3436       return;
3437     }
3438
3439   p = frag_var (rs_align_code, 1, 1, (relax_substateT) 0,
3440                 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
3441
3442   record_alignment (now_seg, nalign);
3443 }
3444
3445 /* This is where we do the unexpected alignment check.
3446    This is called from HANDLE_ALIGN in tc-sparc.h.  */
3447
3448 void
3449 sparc_handle_align (fragp)
3450      fragS *fragp;
3451 {
3452   if (fragp->fr_type == rs_align_code && !fragp->fr_subtype
3453       && fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix != 0)
3454     as_bad_where (fragp->fr_file, fragp->fr_line, _("misaligned data"));
3455   if (fragp->fr_type == rs_align_code && fragp->fr_subtype == 1024)
3456     {
3457       int count = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
3458       
3459       if (count >= 4
3460           && !(count & 3)
3461           && count <= 1024
3462           && !((long)(fragp->fr_literal + fragp->fr_fix) & 3))
3463         {
3464           unsigned *p = (unsigned *)(fragp->fr_literal + fragp->fr_fix);
3465           int i;
3466           
3467           for (i = 0; i < count; i += 4, p++)
3468             if (INSN_BIG_ENDIAN)
3469               number_to_chars_bigendian ((char *)p, 0x01000000, 4); /* emit nops */
3470             else
3471               number_to_chars_littleendian ((char *)p, 0x10000000, 4);
3472
3473           if (SPARC_OPCODE_ARCH_V9_P (max_architecture) && count > 8)
3474             {
3475               char *waddr = &fragp->fr_literal[fragp->fr_fix];
3476               unsigned wval = (0x30680000 | count >> 2); /* ba,a,pt %xcc, 1f */
3477               if (INSN_BIG_ENDIAN)
3478                 number_to_chars_bigendian (waddr, wval, 4);
3479               else
3480                 number_to_chars_littleendian (waddr, wval, 4);
3481             }
3482           fragp->fr_var = count;
3483         }
3484     }
3485 }
3486
3487 #ifdef OBJ_ELF
3488 /* Some special processing for a Sparc ELF file.  */
3489
3490 void
3491 sparc_elf_final_processing ()
3492 {
3493   /* Set the Sparc ELF flag bits.  FIXME: There should probably be some
3494      sort of BFD interface for this.  */
3495   if (sparc_arch_size == 64)
3496     {
3497       switch (sparc_memory_model)
3498         {
3499         case MM_RMO:
3500           elf_elfheader (stdoutput)->e_flags |= EF_SPARCV9_RMO;
3501           break;
3502         case MM_PSO:
3503           elf_elfheader (stdoutput)->e_flags |= EF_SPARCV9_PSO;
3504           break;
3505         default:
3506           break;
3507         }
3508     }
3509   else if (current_architecture >= SPARC_OPCODE_ARCH_V9)
3510     elf_elfheader (stdoutput)->e_flags |= EF_SPARC_32PLUS;
3511   if (current_architecture == SPARC_OPCODE_ARCH_V9A)
3512     elf_elfheader (stdoutput)->e_flags |= EF_SPARC_SUN_US1;
3513 }
3514 #endif
3515
3516 /* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
3517    reloc for a cons.  We could use the definition there, except that
3518    we want to handle little endian relocs specially.  */
3519
3520 void
3521 cons_fix_new_sparc (frag, where, nbytes, exp)
3522      fragS *frag;
3523      int where;
3524      unsigned int nbytes;
3525      expressionS *exp;
3526 {
3527   bfd_reloc_code_real_type r;
3528
3529   r = (nbytes == 1 ? BFD_RELOC_8 :
3530        (nbytes == 2 ? BFD_RELOC_16 :
3531         (nbytes == 4 ? BFD_RELOC_32 : BFD_RELOC_64)));
3532
3533   if (target_little_endian_data && nbytes == 4
3534       && now_seg->flags & SEC_ALLOC)  
3535     r = BFD_RELOC_SPARC_REV32;
3536   fix_new_exp (frag, where, (int) nbytes, exp, 0, r);
3537 }
3538
3539 #ifdef OBJ_ELF
3540 int
3541 elf32_sparc_force_relocation (fixp)
3542       struct fix *fixp;
3543 {
3544   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3545       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3546     return 1;
3547  
3548   return 0;
3549 }
3550 #endif
3551