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