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