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