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