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