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