]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - gnu/usr.bin/binutils/as/tc-sparc-fixed.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / gnu / usr.bin / binutils / as / tc-sparc-fixed.c
1 /* tc-sparc.c -- Assemble for the SPARC
2    Copyright 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005
4    Free Software Foundation, Inc.
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public
18    License along with GAS; see the file COPYING.  If not, write
19    to the Free Software Foundation, 51 Franklin Street - Fifth Floor,
20    Boston, MA 02110-1301, USA.  */
21
22 /* $FreeBSD$ */
23
24 #include <stdio.h>
25
26 #include "as.h"
27 #include "safe-ctype.h"
28 #include "subsegs.h"
29
30 #include "opcode/sparc.h"
31 #include "dw2gencfi.h"
32
33 #ifdef OBJ_ELF
34 #include "elf/sparc.h"
35 #include "dwarf2dbg.h"
36 #endif
37
38 /* Some ancient Sun C compilers would not take such hex constants as
39    unsigned, and would end up sign-extending them to form an offsetT,
40    so use these constants instead.  */
41 #define U0xffffffff ((((unsigned long) 1 << 16) << 16) - 1)
42 #define U0x80000000 ((((unsigned long) 1 << 16) << 15))
43
44 static struct sparc_arch *lookup_arch PARAMS ((char *));
45 static void init_default_arch PARAMS ((void));
46 static int sparc_ip PARAMS ((char *, const struct sparc_opcode **));
47 static int in_signed_range PARAMS ((bfd_signed_vma, bfd_signed_vma));
48 static int in_unsigned_range PARAMS ((bfd_vma, bfd_vma));
49 static int in_bitfield_range PARAMS ((bfd_signed_vma, bfd_signed_vma));
50 static int sparc_ffs PARAMS ((unsigned int));
51 static void synthetize_setuw PARAMS ((const struct sparc_opcode *));
52 static void synthetize_setsw PARAMS ((const struct sparc_opcode *));
53 static void synthetize_setx PARAMS ((const struct sparc_opcode *));
54 static bfd_vma BSR PARAMS ((bfd_vma, int));
55 static int cmp_reg_entry PARAMS ((const PTR, const PTR));
56 static int parse_keyword_arg PARAMS ((int (*) (const char *), char **, int *));
57 static int parse_const_expr_arg PARAMS ((char **, int *));
58 static int get_expression PARAMS ((char *str));
59
60 /* Default architecture.  */
61 /* ??? The default value should be V8, but sparclite support was added
62    by making it the default.  GCC now passes -Asparclite, so maybe sometime in
63    the future we can set this to V8.  */
64 #ifndef DEFAULT_ARCH
65 #define DEFAULT_ARCH "sparclite"
66 #endif
67 static char *default_arch = DEFAULT_ARCH;
68
69 /* Non-zero if the initial values of `max_architecture' and `sparc_arch_size'
70    have been set.  */
71 static int default_init_p;
72
73 /* Current architecture.  We don't bump up unless necessary.  */
74 static enum sparc_opcode_arch_val current_architecture = SPARC_OPCODE_ARCH_V6;
75
76 /* The maximum architecture level we can bump up to.
77    In a 32 bit environment, don't allow bumping up to v9 by default.
78    The native assembler works this way.  The user is required to pass
79    an explicit argument before we'll create v9 object files.  However, if
80    we don't see any v9 insns, a v8plus object file is not created.  */
81 static enum sparc_opcode_arch_val max_architecture;
82
83 /* Either 32 or 64, selects file format.  */
84 static int sparc_arch_size;
85 /* Initial (default) value, recorded separately in case a user option
86    changes the value before md_show_usage is called.  */
87 static int default_arch_size;
88
89 #ifdef OBJ_ELF
90 /* The currently selected v9 memory model.  Currently only used for
91    ELF.  */
92 static enum { MM_TSO, MM_PSO, MM_RMO } sparc_memory_model = MM_RMO;
93 #endif
94
95 static int architecture_requested;
96 static int warn_on_bump;
97
98 /* If warn_on_bump and the needed architecture is higher than this
99    architecture, issue a warning.  */
100 static enum sparc_opcode_arch_val warn_after_architecture;
101
102 /* Non-zero if as should generate error if an undeclared g[23] register
103    has been used in -64.  */
104 static int no_undeclared_regs;
105
106 /* Non-zero if we should try to relax jumps and calls.  */
107 static int sparc_relax;
108
109 /* Non-zero if we are generating PIC code.  */
110 int sparc_pic_code;
111
112 /* Non-zero if we should give an error when misaligned data is seen.  */
113 static int enforce_aligned_data;
114
115 extern int target_big_endian;
116
117 static int target_little_endian_data;
118
119 /* Symbols for global registers on v9.  */
120 static symbolS *globals[8];
121
122 /* The dwarf2 data alignment, adjusted for 32 or 64 bit.  */
123 int sparc_cie_data_alignment;
124
125 /* V9 and 86x have big and little endian data, but instructions are always big
126    endian.  The sparclet has bi-endian support but both data and insns have
127    the same endianness.  Global `target_big_endian' is used for data.
128    The following macro is used for instructions.  */
129 #ifndef INSN_BIG_ENDIAN
130 #define INSN_BIG_ENDIAN (target_big_endian \
131                          || default_arch_type == sparc86x \
132                          || SPARC_OPCODE_ARCH_V9_P (max_architecture))
133 #endif
134
135 /* Handle of the OPCODE hash table.  */
136 static struct hash_control *op_hash;
137
138 static int mylog2 PARAMS ((int));
139 static void s_data1 PARAMS ((void));
140 static void s_seg PARAMS ((int));
141 static void s_proc PARAMS ((int));
142 static void s_reserve PARAMS ((int));
143 static void s_common PARAMS ((int));
144 static void s_empty PARAMS ((int));
145 static void s_uacons PARAMS ((int));
146 static void s_ncons PARAMS ((int));
147 #ifdef OBJ_ELF
148 static void s_register PARAMS ((int));
149 #endif
150
151 const pseudo_typeS md_pseudo_table[] =
152 {
153   {"align", s_align_bytes, 0},  /* Defaulting is invalid (0).  */
154   {"common", s_common, 0},
155   {"empty", s_empty, 0},
156   {"global", s_globl, 0},
157   {"half", cons, 2},
158   {"nword", s_ncons, 0},
159   {"optim", s_ignore, 0},
160   {"proc", s_proc, 0},
161   {"reserve", s_reserve, 0},
162   {"seg", s_seg, 0},
163   {"skip", s_space, 0},
164   {"word", cons, 4},
165   {"xword", cons, 8},
166   {"uahalf", s_uacons, 2},
167   {"uaword", s_uacons, 4},
168   {"uaxword", s_uacons, 8},
169 #ifdef OBJ_ELF
170   /* These are specific to sparc/svr4.  */
171   {"2byte", s_uacons, 2},
172   {"4byte", s_uacons, 4},
173   {"8byte", s_uacons, 8},
174   {"register", s_register, 0},
175 #endif
176   {NULL, 0, 0},
177 };
178
179 /* This array holds the chars that always start a comment.  If the
180    pre-processor is disabled, these aren't very useful.  */
181 const char comment_chars[] = "!";       /* JF removed '|' from
182                                            comment_chars.  */
183
184 /* This array holds the chars that only start a comment at the beginning of
185    a line.  If the line seems to have the form '# 123 filename'
186    .line and .file directives will appear in the pre-processed output.  */
187 /* Note that input_file.c hand checks for '#' at the beginning of the
188    first line of the input file.  This is because the compiler outputs
189    #NO_APP at the beginning of its output.  */
190 /* Also note that comments started like this one will always
191    work if '/' isn't otherwise defined.  */
192 const char line_comment_chars[] = "#";
193
194 const char line_separator_chars[] = ";";
195
196 /* Chars that can be used to separate mant from exp in floating point
197    nums.  */
198 const char EXP_CHARS[] = "eE";
199
200 /* Chars that mean this number is a floating point constant.
201    As in 0f12.456
202    or    0d1.2345e12  */
203 const char FLT_CHARS[] = "rRsSfFdDxXpP";
204
205 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
206    changed in read.c.  Ideally it shouldn't have to know about it at all,
207    but nothing is ideal around here.  */
208
209 #define isoctal(c)  ((unsigned) ((c) - '0') < 8)
210
211 struct sparc_it
212   {
213     char *error;
214     unsigned long opcode;
215     struct nlist *nlistp;
216     expressionS exp;
217     expressionS exp2;
218     int pcrel;
219     bfd_reloc_code_real_type reloc;
220   };
221
222 struct sparc_it the_insn, set_insn;
223
224 static void output_insn
225   PARAMS ((const struct sparc_opcode *, struct sparc_it *));
226 \f
227 /* Table of arguments to -A.
228    The sparc_opcode_arch table in sparc-opc.c is insufficient and incorrect
229    for this use.  That table is for opcodes only.  This table is for opcodes
230    and file formats.  */
231
232 enum sparc_arch_types {v6, v7, v8, sparclet, sparclite, sparc86x, v8plus,
233                        v8plusa, v9, v9a, v9b, v9_64};
234
235 static struct sparc_arch {
236   char *name;
237   char *opcode_arch;
238   enum sparc_arch_types arch_type;
239   /* Default word size, as specified during configuration.
240      A value of zero means can't be used to specify default architecture.  */
241   int default_arch_size;
242   /* Allowable arg to -A?  */
243   int user_option_p;
244 } sparc_arch_table[] = {
245   { "v6", "v6", v6, 0, 1 },
246   { "v7", "v7", v7, 0, 1 },
247   { "v8", "v8", v8, 32, 1 },
248   { "sparclet", "sparclet", sparclet, 32, 1 },
249   { "sparclite", "sparclite", sparclite, 32, 1 },
250   { "sparc86x", "sparclite", sparc86x, 32, 1 },
251   { "v8plus", "v9", v9, 0, 1 },
252   { "v8plusa", "v9a", v9, 0, 1 },
253   { "v8plusb", "v9b", v9, 0, 1 },
254   { "v9", "v9", v9, 0, 1 },
255   { "v9a", "v9a", v9, 0, 1 },
256   { "v9b", "v9b", v9, 0, 1 },
257   /* This exists to allow configure.in/Makefile.in to pass one
258      value to specify both the default machine and default word size.  */
259   { "v9-64", "v9", v9, 64, 0 },
260   { NULL, NULL, v8, 0, 0 }
261 };
262
263 /* Variant of default_arch */
264 static enum sparc_arch_types default_arch_type;
265
266 static struct sparc_arch *
267 lookup_arch (name)
268      char *name;
269 {
270   struct sparc_arch *sa;
271
272   for (sa = &sparc_arch_table[0]; sa->name != NULL; sa++)
273     if (strcmp (sa->name, name) == 0)
274       break;
275   if (sa->name == NULL)
276     return NULL;
277   return sa;
278 }
279
280 /* Initialize the default opcode arch and word size from the default
281    architecture name.  */
282
283 static void
284 init_default_arch ()
285 {
286   struct sparc_arch *sa = lookup_arch (default_arch);
287
288   if (sa == NULL
289       || sa->default_arch_size == 0)
290     as_fatal (_("Invalid default architecture, broken assembler."));
291
292   max_architecture = sparc_opcode_lookup_arch (sa->opcode_arch);
293   if (max_architecture == SPARC_OPCODE_ARCH_BAD)
294     as_fatal (_("Bad opcode table, broken assembler."));
295   default_arch_size = sparc_arch_size = sa->default_arch_size;
296   default_init_p = 1;
297   default_arch_type = sa->arch_type;
298 }
299
300 /* Called by TARGET_FORMAT.  */
301
302 const char *
303 sparc_target_format ()
304 {
305   /* We don't get a chance to initialize anything before we're called,
306      so handle that now.  */
307   if (! default_init_p)
308     init_default_arch ();
309
310 #ifdef OBJ_AOUT
311 #ifdef TE_NetBSD
312   return "a.out-sparc-netbsd";
313 #else
314 #ifdef TE_SPARCAOUT
315   if (target_big_endian)
316     return "a.out-sunos-big";
317   else if (default_arch_type == sparc86x && target_little_endian_data)
318     return "a.out-sunos-big";
319   else
320     return "a.out-sparc-little";
321 #else
322   return "a.out-sunos-big";
323 #endif
324 #endif
325 #endif
326
327 #ifdef OBJ_BOUT
328   return "b.out.big";
329 #endif
330
331 #ifdef OBJ_COFF
332 #ifdef TE_LYNX
333   return "coff-sparc-lynx";
334 #else
335   return "coff-sparc";
336 #endif
337 #endif
338
339 #ifdef TE_VXWORKS
340   return "elf32-sparc-vxworks";
341 #endif
342
343 #ifdef OBJ_ELF
344   return sparc_arch_size == 64 ? "elf64-sparc" : "elf32-sparc";
345 #endif
346
347   abort ();
348 }
349 \f
350 /* md_parse_option
351  *      Invocation line includes a switch not recognized by the base assembler.
352  *      See if it's a processor-specific option.  These are:
353  *
354  *      -bump
355  *              Warn on architecture bumps.  See also -A.
356  *
357  *      -Av6, -Av7, -Av8, -Asparclite, -Asparclet
358  *              Standard 32 bit architectures.
359  *      -Av9, -Av9a, -Av9b
360  *              Sparc64 in either a 32 or 64 bit world (-32/-64 says which).
361  *              This used to only mean 64 bits, but properly specifying it
362  *              complicated gcc's ASM_SPECs, so now opcode selection is
363  *              specified orthogonally to word size (except when specifying
364  *              the default, but that is an internal implementation detail).
365  *      -Av8plus, -Av8plusa, -Av8plusb
366  *              Same as -Av9{,a,b}.
367  *      -xarch=v8plus, -xarch=v8plusa, -xarch=v8plusb
368  *              Same as -Av8plus{,a,b} -32, for compatibility with Sun's
369  *              assembler.
370  *      -xarch=v9, -xarch=v9a, -xarch=v9b
371  *              Same as -Av9{,a,b} -64, for compatibility with Sun's
372  *              assembler.
373  *
374  *              Select the architecture and possibly the file format.
375  *              Instructions or features not supported by the selected
376  *              architecture cause fatal errors.
377  *
378  *              The default is to start at v6, and bump the architecture up
379  *              whenever an instruction is seen at a higher level.  In 32 bit
380  *              environments, v9 is not bumped up to, the user must pass
381  *              -Av8plus{,a,b}.
382  *
383  *              If -bump is specified, a warning is printing when bumping to
384  *              higher levels.
385  *
386  *              If an architecture is specified, all instructions must match
387  *              that architecture.  Any higher level instructions are flagged
388  *              as errors.  Note that in the 32 bit environment specifying
389  *              -Av8plus does not automatically create a v8plus object file, a
390  *              v9 insn must be seen.
391  *
392  *              If both an architecture and -bump are specified, the
393  *              architecture starts at the specified level, but bumps are
394  *              warnings.  Note that we can't set `current_architecture' to
395  *              the requested level in this case: in the 32 bit environment,
396  *              we still must avoid creating v8plus object files unless v9
397  *              insns are seen.
398  *
399  * Note:
400  *              Bumping between incompatible architectures is always an
401  *              error.  For example, from sparclite to v9.
402  */
403
404 #ifdef OBJ_ELF
405 const char *md_shortopts = "A:K:VQ:sq";
406 #else
407 #ifdef OBJ_AOUT
408 const char *md_shortopts = "A:k";
409 #else
410 const char *md_shortopts = "A:";
411 #endif
412 #endif
413 struct option md_longopts[] = {
414 #define OPTION_BUMP (OPTION_MD_BASE)
415   {"bump", no_argument, NULL, OPTION_BUMP},
416 #define OPTION_SPARC (OPTION_MD_BASE + 1)
417   {"sparc", no_argument, NULL, OPTION_SPARC},
418 #define OPTION_XARCH (OPTION_MD_BASE + 2)
419   {"xarch", required_argument, NULL, OPTION_XARCH},
420 #ifdef OBJ_ELF
421 #define OPTION_32 (OPTION_MD_BASE + 3)
422   {"32", no_argument, NULL, OPTION_32},
423 #define OPTION_64 (OPTION_MD_BASE + 4)
424   {"64", no_argument, NULL, OPTION_64},
425 #define OPTION_TSO (OPTION_MD_BASE + 5)
426   {"TSO", no_argument, NULL, OPTION_TSO},
427 #define OPTION_PSO (OPTION_MD_BASE + 6)
428   {"PSO", no_argument, NULL, OPTION_PSO},
429 #define OPTION_RMO (OPTION_MD_BASE + 7)
430   {"RMO", no_argument, NULL, OPTION_RMO},
431 #endif
432 #ifdef SPARC_BIENDIAN
433 #define OPTION_LITTLE_ENDIAN (OPTION_MD_BASE + 8)
434   {"EL", no_argument, NULL, OPTION_LITTLE_ENDIAN},
435 #define OPTION_BIG_ENDIAN (OPTION_MD_BASE + 9)
436   {"EB", no_argument, NULL, OPTION_BIG_ENDIAN},
437 #endif
438 #define OPTION_ENFORCE_ALIGNED_DATA (OPTION_MD_BASE + 10)
439   {"enforce-aligned-data", no_argument, NULL, OPTION_ENFORCE_ALIGNED_DATA},
440 #define OPTION_LITTLE_ENDIAN_DATA (OPTION_MD_BASE + 11)
441   {"little-endian-data", no_argument, NULL, OPTION_LITTLE_ENDIAN_DATA},
442 #ifdef OBJ_ELF
443 #define OPTION_NO_UNDECLARED_REGS (OPTION_MD_BASE + 12)
444   {"no-undeclared-regs", no_argument, NULL, OPTION_NO_UNDECLARED_REGS},
445 #define OPTION_UNDECLARED_REGS (OPTION_MD_BASE + 13)
446   {"undeclared-regs", no_argument, NULL, OPTION_UNDECLARED_REGS},
447 #endif
448 #define OPTION_RELAX (OPTION_MD_BASE + 14)
449   {"relax", no_argument, NULL, OPTION_RELAX},
450 #define OPTION_NO_RELAX (OPTION_MD_BASE + 15)
451   {"no-relax", no_argument, NULL, OPTION_NO_RELAX},
452   {NULL, no_argument, NULL, 0}
453 };
454
455 size_t md_longopts_size = sizeof (md_longopts);
456
457 int
458 md_parse_option (c, arg)
459      int c;
460      char *arg;
461 {
462   /* We don't get a chance to initialize anything before we're called,
463      so handle that now.  */
464   if (! default_init_p)
465     init_default_arch ();
466
467   switch (c)
468     {
469     case OPTION_BUMP:
470       warn_on_bump = 1;
471       warn_after_architecture = SPARC_OPCODE_ARCH_V6;
472       break;
473
474     case OPTION_XARCH:
475 #ifdef OBJ_ELF
476       if (strncmp (arg, "v9", 2) != 0)
477         md_parse_option (OPTION_32, NULL);
478       else
479         md_parse_option (OPTION_64, NULL);
480 #endif
481       /* Fall through.  */
482
483     case 'A':
484       {
485         struct sparc_arch *sa;
486         enum sparc_opcode_arch_val opcode_arch;
487
488         sa = lookup_arch (arg);
489         if (sa == NULL
490             || ! sa->user_option_p)
491           {
492             if (c == OPTION_XARCH)
493               as_bad (_("invalid architecture -xarch=%s"), arg);
494             else
495               as_bad (_("invalid architecture -A%s"), arg);
496             return 0;
497           }
498
499         opcode_arch = sparc_opcode_lookup_arch (sa->opcode_arch);
500         if (opcode_arch == SPARC_OPCODE_ARCH_BAD)
501           as_fatal (_("Bad opcode table, broken assembler."));
502
503         max_architecture = opcode_arch;
504         architecture_requested = 1;
505       }
506       break;
507
508     case OPTION_SPARC:
509       /* Ignore -sparc, used by SunOS make default .s.o rule.  */
510       break;
511
512     case OPTION_ENFORCE_ALIGNED_DATA:
513       enforce_aligned_data = 1;
514       break;
515
516 #ifdef SPARC_BIENDIAN
517     case OPTION_LITTLE_ENDIAN:
518       target_big_endian = 0;
519       if (default_arch_type != sparclet)
520         as_fatal ("This target does not support -EL");
521       break;
522     case OPTION_LITTLE_ENDIAN_DATA:
523       target_little_endian_data = 1;
524       target_big_endian = 0;
525       if (default_arch_type != sparc86x
526           && default_arch_type != v9)
527         as_fatal ("This target does not support --little-endian-data");
528       break;
529     case OPTION_BIG_ENDIAN:
530       target_big_endian = 1;
531       break;
532 #endif
533
534 #ifdef OBJ_AOUT
535     case 'k':
536       sparc_pic_code = 1;
537       break;
538 #endif
539
540 #ifdef OBJ_ELF
541     case OPTION_32:
542     case OPTION_64:
543       {
544         const char **list, **l;
545
546         sparc_arch_size = c == OPTION_32 ? 32 : 64;
547         list = bfd_target_list ();
548         for (l = list; *l != NULL; l++)
549           {
550             if (sparc_arch_size == 32)
551               {
552                 if (strcmp (*l, "elf32-sparc") == 0)
553                   break;
554               }
555             else
556               {
557                 if (strcmp (*l, "elf64-sparc") == 0)
558                   break;
559               }
560           }
561         if (*l == NULL)
562           as_fatal (_("No compiled in support for %d bit object file format"),
563                     sparc_arch_size);
564         free (list);
565       }
566       break;
567
568     case OPTION_TSO:
569       sparc_memory_model = MM_TSO;
570       break;
571
572     case OPTION_PSO:
573       sparc_memory_model = MM_PSO;
574       break;
575
576     case OPTION_RMO:
577       sparc_memory_model = MM_RMO;
578       break;
579
580     case 'V':
581       print_version_id ();
582       break;
583
584     case 'Q':
585       /* Qy - do emit .comment
586          Qn - do not emit .comment.  */
587       break;
588
589     case 's':
590       /* Use .stab instead of .stab.excl.  */
591       break;
592
593     case 'q':
594       /* quick -- Native assembler does fewer checks.  */
595       break;
596
597     case 'K':
598       if (strcmp (arg, "PIC") != 0)
599         as_warn (_("Unrecognized option following -K"));
600       else
601         sparc_pic_code = 1;
602       break;
603
604     case OPTION_NO_UNDECLARED_REGS:
605       no_undeclared_regs = 1;
606       break;
607
608     case OPTION_UNDECLARED_REGS:
609       no_undeclared_regs = 0;
610       break;
611 #endif
612
613     case OPTION_RELAX:
614       sparc_relax = 1;
615       break;
616
617     case OPTION_NO_RELAX:
618       sparc_relax = 0;
619       break;
620
621     default:
622       return 0;
623     }
624
625   return 1;
626 }
627
628 void
629 md_show_usage (stream)
630      FILE *stream;
631 {
632   const struct sparc_arch *arch;
633   int column;
634
635   /* We don't get a chance to initialize anything before we're called,
636      so handle that now.  */
637   if (! default_init_p)
638     init_default_arch ();
639
640   fprintf (stream, _("SPARC options:\n"));
641   column = 0;
642   for (arch = &sparc_arch_table[0]; arch->name; arch++)
643     {
644       if (!arch->user_option_p)
645         continue;
646       if (arch != &sparc_arch_table[0])
647         fprintf (stream, " | ");
648       if (column + strlen (arch->name) > 70)
649         {
650           column = 0;
651           fputc ('\n', stream);
652         }
653       column += 5 + 2 + strlen (arch->name);
654       fprintf (stream, "-A%s", arch->name);
655     }
656   for (arch = &sparc_arch_table[0]; arch->name; arch++)
657     {
658       if (!arch->user_option_p)
659         continue;
660       fprintf (stream, " | ");
661       if (column + strlen (arch->name) > 65)
662         {
663           column = 0;
664           fputc ('\n', stream);
665         }
666       column += 5 + 7 + strlen (arch->name);
667       fprintf (stream, "-xarch=%s", arch->name);
668     }
669   fprintf (stream, _("\n\
670                         specify variant of SPARC architecture\n\
671 -bump                   warn when assembler switches architectures\n\
672 -sparc                  ignored\n\
673 --enforce-aligned-data  force .long, etc., to be aligned correctly\n\
674 -relax                  relax jumps and branches (default)\n\
675 -no-relax               avoid changing any jumps and branches\n"));
676 #ifdef OBJ_AOUT
677   fprintf (stream, _("\
678 -k                      generate PIC\n"));
679 #endif
680 #ifdef OBJ_ELF
681   fprintf (stream, _("\
682 -32                     create 32 bit object file\n\
683 -64                     create 64 bit object file\n"));
684   fprintf (stream, _("\
685                         [default is %d]\n"), default_arch_size);
686   fprintf (stream, _("\
687 -TSO                    use Total Store Ordering\n\
688 -PSO                    use Partial Store Ordering\n\
689 -RMO                    use Relaxed Memory Ordering\n"));
690   fprintf (stream, _("\
691                         [default is %s]\n"), (default_arch_size == 64) ? "RMO" : "TSO");
692   fprintf (stream, _("\
693 -KPIC                   generate PIC\n\
694 -V                      print assembler version number\n\
695 -undeclared-regs        ignore application global register usage without\n\
696                         appropriate .register directive (default)\n\
697 -no-undeclared-regs     force error on application global register usage\n\
698                         without appropriate .register directive\n\
699 -q                      ignored\n\
700 -Qy, -Qn                ignored\n\
701 -s                      ignored\n"));
702 #endif
703 #ifdef SPARC_BIENDIAN
704   fprintf (stream, _("\
705 -EL                     generate code for a little endian machine\n\
706 -EB                     generate code for a big endian machine\n\
707 --little-endian-data    generate code for a machine having big endian\n\
708                         instructions and little endian data.\n"));
709 #endif
710 }
711 \f
712 /* Native operand size opcode translation.  */
713 struct
714   {
715     char *name;
716     char *name32;
717     char *name64;
718   } native_op_table[] =
719 {
720   {"ldn", "ld", "ldx"},
721   {"ldna", "lda", "ldxa"},
722   {"stn", "st", "stx"},
723   {"stna", "sta", "stxa"},
724   {"slln", "sll", "sllx"},
725   {"srln", "srl", "srlx"},
726   {"sran", "sra", "srax"},
727   {"casn", "cas", "casx"},
728   {"casna", "casa", "casxa"},
729   {"clrn", "clr", "clrx"},
730   {NULL, NULL, NULL},
731 };
732 \f
733 /* sparc64 privileged and hyperprivileged registers.  */
734
735 struct priv_reg_entry
736 {
737   char *name;
738   int regnum;
739 };
740
741 struct priv_reg_entry priv_reg_table[] =
742 {
743   {"tpc", 0},
744   {"tnpc", 1},
745   {"tstate", 2},
746   {"tt", 3},
747   {"tick", 4},
748   {"tba", 5},
749   {"pstate", 6},
750   {"tl", 7},
751   {"pil", 8},
752   {"cwp", 9},
753   {"cansave", 10},
754   {"canrestore", 11},
755   {"cleanwin", 12},
756   {"otherwin", 13},
757   {"wstate", 14},
758   {"fq", 15},
759   {"gl", 16},
760   {"ver", 31},
761   {"", -1},                     /* End marker.  */
762 };
763
764 struct priv_reg_entry hpriv_reg_table[] =
765 {
766   {"hpstate", 0},
767   {"htstate", 1},
768   {"hintp", 3},
769   {"htba", 5},
770   {"hver", 6},
771   {"hstick_cmpr", 31},
772   {"", -1},                     /* End marker.  */
773 };
774
775 /* v9a specific asrs.  */
776
777 struct priv_reg_entry v9a_asr_table[] =
778 {
779   {"tick_cmpr", 23},
780   {"sys_tick_cmpr", 25},
781   {"sys_tick", 24},
782   {"softint", 22},
783   {"set_softint", 20},
784   {"pic", 17},
785   {"pcr", 16},
786   {"gsr", 19},
787   {"dcr", 18},
788   {"clear_softint", 21},
789   {"", -1},                     /* End marker.  */
790 };
791
792 static int
793 cmp_reg_entry (parg, qarg)
794      const PTR parg;
795      const PTR qarg;
796 {
797   const struct priv_reg_entry *p = (const struct priv_reg_entry *) parg;
798   const struct priv_reg_entry *q = (const struct priv_reg_entry *) qarg;
799
800   return strcmp (q->name, p->name);
801 }
802 \f
803 /* This function is called once, at assembler startup time.  It should
804    set up all the tables, etc. that the MD part of the assembler will
805    need.  */
806
807 void
808 md_begin ()
809 {
810   register const char *retval = NULL;
811   int lose = 0;
812   register unsigned int i = 0;
813
814   /* We don't get a chance to initialize anything before md_parse_option
815      is called, and it may not be called, so handle default initialization
816      now if not already done.  */
817   if (! default_init_p)
818     init_default_arch ();
819
820   sparc_cie_data_alignment = sparc_arch_size == 64 ? -8 : -4;
821   op_hash = hash_new ();
822
823   while (i < (unsigned int) sparc_num_opcodes)
824     {
825       const char *name = sparc_opcodes[i].name;
826       retval = hash_insert (op_hash, name, (PTR) &sparc_opcodes[i]);
827       if (retval != NULL)
828         {
829           as_bad (_("Internal error: can't hash `%s': %s\n"),
830                   sparc_opcodes[i].name, retval);
831           lose = 1;
832         }
833       do
834         {
835           if (sparc_opcodes[i].match & sparc_opcodes[i].lose)
836             {
837               as_bad (_("Internal error: losing opcode: `%s' \"%s\"\n"),
838                       sparc_opcodes[i].name, sparc_opcodes[i].args);
839               lose = 1;
840             }
841           ++i;
842         }
843       while (i < (unsigned int) sparc_num_opcodes
844              && !strcmp (sparc_opcodes[i].name, name));
845     }
846
847   for (i = 0; native_op_table[i].name; i++)
848     {
849       const struct sparc_opcode *insn;
850       char *name = ((sparc_arch_size == 32)
851                     ? native_op_table[i].name32
852                     : native_op_table[i].name64);
853       insn = (struct sparc_opcode *) hash_find (op_hash, name);
854       if (insn == NULL)
855         {
856           as_bad (_("Internal error: can't find opcode `%s' for `%s'\n"),
857                   name, native_op_table[i].name);
858           lose = 1;
859         }
860       else
861         {
862           retval = hash_insert (op_hash, native_op_table[i].name, (PTR) insn);
863           if (retval != NULL)
864             {
865               as_bad (_("Internal error: can't hash `%s': %s\n"),
866                       sparc_opcodes[i].name, retval);
867               lose = 1;
868             }
869         }
870     }
871
872   if (lose)
873     as_fatal (_("Broken assembler.  No assembly attempted."));
874
875   qsort (priv_reg_table, sizeof (priv_reg_table) / sizeof (priv_reg_table[0]),
876          sizeof (priv_reg_table[0]), cmp_reg_entry);
877
878   /* If -bump, record the architecture level at which we start issuing
879      warnings.  The behaviour is different depending upon whether an
880      architecture was explicitly specified.  If it wasn't, we issue warnings
881      for all upwards bumps.  If it was, we don't start issuing warnings until
882      we need to bump beyond the requested architecture or when we bump between
883      conflicting architectures.  */
884
885   if (warn_on_bump
886       && architecture_requested)
887     {
888       /* `max_architecture' records the requested architecture.
889          Issue warnings if we go above it.  */
890       warn_after_architecture = max_architecture;
891
892       /* Find the highest architecture level that doesn't conflict with
893          the requested one.  */
894       for (max_architecture = SPARC_OPCODE_ARCH_MAX;
895            max_architecture > warn_after_architecture;
896            --max_architecture)
897         if (! SPARC_OPCODE_CONFLICT_P (max_architecture,
898                                        warn_after_architecture))
899           break;
900     }
901 }
902
903 /* Called after all assembly has been done.  */
904
905 void
906 sparc_md_end ()
907 {
908   unsigned long mach = bfd_mach_sparc;
909
910   if (sparc_arch_size == 64)
911     switch (current_architecture)
912       {
913       case SPARC_OPCODE_ARCH_V9A: mach = bfd_mach_sparc_v9a; break;
914       case SPARC_OPCODE_ARCH_V9B: mach = bfd_mach_sparc_v9b; break;
915       default: mach = bfd_mach_sparc_v9; break;
916       }
917   else
918     switch (current_architecture)
919       {
920       case SPARC_OPCODE_ARCH_SPARCLET: mach = bfd_mach_sparc_sparclet; break;
921       case SPARC_OPCODE_ARCH_V9: mach = bfd_mach_sparc_v8plus; break;
922       case SPARC_OPCODE_ARCH_V9A: mach = bfd_mach_sparc_v8plusa; break;
923       case SPARC_OPCODE_ARCH_V9B: mach = bfd_mach_sparc_v8plusb; break;
924       /* The sparclite is treated like a normal sparc.  Perhaps it shouldn't
925          be but for now it is (since that's the way it's always been
926          treated).  */
927       default: break;
928       }
929   bfd_set_arch_mach (stdoutput, bfd_arch_sparc, mach);
930 }
931 \f
932 /* Return non-zero if VAL is in the range -(MAX+1) to MAX.  */
933
934 static INLINE int
935 in_signed_range (val, max)
936      bfd_signed_vma val, max;
937 {
938   if (max <= 0)
939     abort ();
940   /* Sign-extend the value from the architecture word size, so that
941      0xffffffff is always considered -1 on sparc32.  */
942   if (sparc_arch_size == 32)
943     {
944       bfd_signed_vma sign = (bfd_signed_vma) 1 << 31;
945       val = ((val & U0xffffffff) ^ sign) - sign;
946     }
947   if (val > max)
948     return 0;
949   if (val < ~max)
950     return 0;
951   return 1;
952 }
953
954 /* Return non-zero if VAL is in the range 0 to MAX.  */
955
956 static INLINE int
957 in_unsigned_range (val, max)
958      bfd_vma val, max;
959 {
960   if (val > max)
961     return 0;
962   return 1;
963 }
964
965 /* Return non-zero if VAL is in the range -(MAX/2+1) to MAX.
966    (e.g. -15 to +31).  */
967
968 static INLINE int
969 in_bitfield_range (val, max)
970      bfd_signed_vma val, max;
971 {
972   if (max <= 0)
973     abort ();
974   if (val > max)
975     return 0;
976   if (val < ~(max >> 1))
977     return 0;
978   return 1;
979 }
980
981 static int
982 sparc_ffs (mask)
983      unsigned int mask;
984 {
985   int i;
986
987   if (mask == 0)
988     return -1;
989
990   for (i = 0; (mask & 1) == 0; ++i)
991     mask >>= 1;
992   return i;
993 }
994
995 /* Implement big shift right.  */
996 static bfd_vma
997 BSR (val, amount)
998      bfd_vma val;
999      int amount;
1000 {
1001   if (sizeof (bfd_vma) <= 4 && amount >= 32)
1002     as_fatal (_("Support for 64-bit arithmetic not compiled in."));
1003   return val >> amount;
1004 }
1005 \f
1006 /* For communication between sparc_ip and get_expression.  */
1007 static char *expr_end;
1008
1009 /* Values for `special_case'.
1010    Instructions that require wierd handling because they're longer than
1011    4 bytes.  */
1012 #define SPECIAL_CASE_NONE       0
1013 #define SPECIAL_CASE_SET        1
1014 #define SPECIAL_CASE_SETSW      2
1015 #define SPECIAL_CASE_SETX       3
1016 /* FIXME: sparc-opc.c doesn't have necessary "S" trigger to enable this.  */
1017 #define SPECIAL_CASE_FDIV       4
1018
1019 /* Bit masks of various insns.  */
1020 #define NOP_INSN 0x01000000
1021 #define OR_INSN 0x80100000
1022 #define XOR_INSN 0x80180000
1023 #define FMOVS_INSN 0x81A00020
1024 #define SETHI_INSN 0x01000000
1025 #define SLLX_INSN 0x81281000
1026 #define SRA_INSN 0x81380000
1027
1028 /* The last instruction to be assembled.  */
1029 static const struct sparc_opcode *last_insn;
1030 /* The assembled opcode of `last_insn'.  */
1031 static unsigned long last_opcode;
1032 \f
1033 /* Handle the set and setuw synthetic instructions.  */
1034
1035 static void
1036 synthetize_setuw (insn)
1037      const struct sparc_opcode *insn;
1038 {
1039   int need_hi22_p = 0;
1040   int rd = (the_insn.opcode & RD (~0)) >> 25;
1041
1042   if (the_insn.exp.X_op == O_constant)
1043     {
1044       if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1045         {
1046           if (sizeof (offsetT) > 4
1047               && (the_insn.exp.X_add_number < 0
1048                   || the_insn.exp.X_add_number > (offsetT) U0xffffffff))
1049             as_warn (_("set: number not in 0..4294967295 range"));
1050         }
1051       else
1052         {
1053           if (sizeof (offsetT) > 4
1054               && (the_insn.exp.X_add_number < -(offsetT) U0x80000000
1055                   || the_insn.exp.X_add_number > (offsetT) U0xffffffff))
1056             as_warn (_("set: number not in -2147483648..4294967295 range"));
1057           the_insn.exp.X_add_number = (int) the_insn.exp.X_add_number;
1058         }
1059     }
1060
1061   /* See if operand is absolute and small; skip sethi if so.  */
1062   if (the_insn.exp.X_op != O_constant
1063       || the_insn.exp.X_add_number >= (1 << 12)
1064       || the_insn.exp.X_add_number < -(1 << 12))
1065     {
1066       the_insn.opcode = (SETHI_INSN | RD (rd)
1067                          | ((the_insn.exp.X_add_number >> 10)
1068                             & (the_insn.exp.X_op == O_constant
1069                                ? 0x3fffff : 0)));
1070       the_insn.reloc = (the_insn.exp.X_op != O_constant
1071                         ? BFD_RELOC_HI22 : BFD_RELOC_NONE);
1072       output_insn (insn, &the_insn);
1073       need_hi22_p = 1;
1074     }
1075
1076   /* See if operand has no low-order bits; skip OR if so.  */
1077   if (the_insn.exp.X_op != O_constant
1078       || (need_hi22_p && (the_insn.exp.X_add_number & 0x3FF) != 0)
1079       || ! need_hi22_p)
1080     {
1081       the_insn.opcode = (OR_INSN | (need_hi22_p ? RS1 (rd) : 0)
1082                          | RD (rd) | IMMED
1083                          | (the_insn.exp.X_add_number
1084                             & (the_insn.exp.X_op != O_constant
1085                                ? 0 : need_hi22_p ? 0x3ff : 0x1fff)));
1086       the_insn.reloc = (the_insn.exp.X_op != O_constant
1087                         ? BFD_RELOC_LO10 : BFD_RELOC_NONE);
1088       output_insn (insn, &the_insn);
1089     }
1090 }
1091
1092 /* Handle the setsw synthetic instruction.  */
1093
1094 static void
1095 synthetize_setsw (insn)
1096      const struct sparc_opcode *insn;
1097 {
1098   int low32, rd, opc;
1099
1100   rd = (the_insn.opcode & RD (~0)) >> 25;
1101
1102   if (the_insn.exp.X_op != O_constant)
1103     {
1104       synthetize_setuw (insn);
1105
1106       /* Need to sign extend it.  */
1107       the_insn.opcode = (SRA_INSN | RS1 (rd) | RD (rd));
1108       the_insn.reloc = BFD_RELOC_NONE;
1109       output_insn (insn, &the_insn);
1110       return;
1111     }
1112
1113   if (sizeof (offsetT) > 4
1114       && (the_insn.exp.X_add_number < -(offsetT) U0x80000000
1115           || the_insn.exp.X_add_number > (offsetT) U0xffffffff))
1116     as_warn (_("setsw: number not in -2147483648..4294967295 range"));
1117
1118   low32 = the_insn.exp.X_add_number;
1119
1120   if (low32 >= 0)
1121     {
1122       synthetize_setuw (insn);
1123       return;
1124     }
1125
1126   opc = OR_INSN;
1127
1128   the_insn.reloc = BFD_RELOC_NONE;
1129   /* See if operand is absolute and small; skip sethi if so.  */
1130   if (low32 < -(1 << 12))
1131     {
1132       the_insn.opcode = (SETHI_INSN | RD (rd)
1133                          | (((~the_insn.exp.X_add_number) >> 10) & 0x3fffff));
1134       output_insn (insn, &the_insn);
1135       low32 = 0x1c00 | (low32 & 0x3ff);
1136       opc = RS1 (rd) | XOR_INSN;
1137     }
1138
1139   the_insn.opcode = (opc | RD (rd) | IMMED
1140                      | (low32 & 0x1fff));
1141   output_insn (insn, &the_insn);
1142 }
1143
1144 /* Handle the setsw synthetic instruction.  */
1145
1146 static void
1147 synthetize_setx (insn)
1148      const struct sparc_opcode *insn;
1149 {
1150   int upper32, lower32;
1151   int tmpreg = (the_insn.opcode & RS1 (~0)) >> 14;
1152   int dstreg = (the_insn.opcode & RD (~0)) >> 25;
1153   int upper_dstreg;
1154   int need_hh22_p = 0, need_hm10_p = 0, need_hi22_p = 0, need_lo10_p = 0;
1155   int need_xor10_p = 0;
1156
1157 #define SIGNEXT32(x) ((((x) & U0xffffffff) ^ U0x80000000) - U0x80000000)
1158   lower32 = SIGNEXT32 (the_insn.exp.X_add_number);
1159   upper32 = SIGNEXT32 (BSR (the_insn.exp.X_add_number, 32));
1160 #undef SIGNEXT32
1161
1162   upper_dstreg = tmpreg;
1163   /* The tmp reg should not be the dst reg.  */
1164   if (tmpreg == dstreg)
1165     as_warn (_("setx: temporary register same as destination register"));
1166
1167   /* ??? Obviously there are other optimizations we can do
1168      (e.g. sethi+shift for 0x1f0000000) and perhaps we shouldn't be
1169      doing some of these.  Later.  If you do change things, try to
1170      change all of this to be table driven as well.  */
1171   /* What to output depends on the number if it's constant.
1172      Compute that first, then output what we've decided upon.  */
1173   if (the_insn.exp.X_op != O_constant)
1174     {
1175       if (sparc_arch_size == 32)
1176         {
1177           /* When arch size is 32, we want setx to be equivalent
1178              to setuw for anything but constants.  */
1179           the_insn.exp.X_add_number &= 0xffffffff;
1180           synthetize_setuw (insn);
1181           return;
1182         }
1183       need_hh22_p = need_hm10_p = need_hi22_p = need_lo10_p = 1;
1184       lower32 = 0;
1185       upper32 = 0;
1186     }
1187   else
1188     {
1189       /* Reset X_add_number, we've extracted it as upper32/lower32.
1190          Otherwise fixup_segment will complain about not being able to
1191          write an 8 byte number in a 4 byte field.  */
1192       the_insn.exp.X_add_number = 0;
1193
1194       /* Only need hh22 if `or' insn can't handle constant.  */
1195       if (upper32 < -(1 << 12) || upper32 >= (1 << 12))
1196         need_hh22_p = 1;
1197
1198       /* Does bottom part (after sethi) have bits?  */
1199       if ((need_hh22_p && (upper32 & 0x3ff) != 0)
1200           /* No hh22, but does upper32 still have bits we can't set
1201              from lower32?  */
1202           || (! need_hh22_p && upper32 != 0 && upper32 != -1))
1203         need_hm10_p = 1;
1204
1205       /* If the lower half is all zero, we build the upper half directly
1206          into the dst reg.  */
1207       if (lower32 != 0
1208           /* Need lower half if number is zero or 0xffffffff00000000.  */
1209           || (! need_hh22_p && ! need_hm10_p))
1210         {
1211           /* No need for sethi if `or' insn can handle constant.  */
1212           if (lower32 < -(1 << 12) || lower32 >= (1 << 12)
1213               /* Note that we can't use a negative constant in the `or'
1214                  insn unless the upper 32 bits are all ones.  */
1215               || (lower32 < 0 && upper32 != -1)
1216               || (lower32 >= 0 && upper32 == -1))
1217             need_hi22_p = 1;
1218
1219           if (need_hi22_p && upper32 == -1)
1220             need_xor10_p = 1;
1221
1222           /* Does bottom part (after sethi) have bits?  */
1223           else if ((need_hi22_p && (lower32 & 0x3ff) != 0)
1224                    /* No sethi.  */
1225                    || (! need_hi22_p && (lower32 & 0x1fff) != 0)
1226                    /* Need `or' if we didn't set anything else.  */
1227                    || (! need_hi22_p && ! need_hh22_p && ! need_hm10_p))
1228             need_lo10_p = 1;
1229         }
1230       else
1231         /* Output directly to dst reg if lower 32 bits are all zero.  */
1232         upper_dstreg = dstreg;
1233     }
1234
1235   if (!upper_dstreg && dstreg)
1236     as_warn (_("setx: illegal temporary register g0"));
1237
1238   if (need_hh22_p)
1239     {
1240       the_insn.opcode = (SETHI_INSN | RD (upper_dstreg)
1241                          | ((upper32 >> 10) & 0x3fffff));
1242       the_insn.reloc = (the_insn.exp.X_op != O_constant
1243                         ? BFD_RELOC_SPARC_HH22 : BFD_RELOC_NONE);
1244       output_insn (insn, &the_insn);
1245     }
1246
1247   if (need_hi22_p)
1248     {
1249       the_insn.opcode = (SETHI_INSN | RD (dstreg)
1250                          | (((need_xor10_p ? ~lower32 : lower32)
1251                              >> 10) & 0x3fffff));
1252       the_insn.reloc = (the_insn.exp.X_op != O_constant
1253                         ? BFD_RELOC_SPARC_LM22 : BFD_RELOC_NONE);
1254       output_insn (insn, &the_insn);
1255     }
1256
1257   if (need_hm10_p)
1258     {
1259       the_insn.opcode = (OR_INSN
1260                          | (need_hh22_p ? RS1 (upper_dstreg) : 0)
1261                          | RD (upper_dstreg)
1262                          | IMMED
1263                          | (upper32 & (need_hh22_p ? 0x3ff : 0x1fff)));
1264       the_insn.reloc = (the_insn.exp.X_op != O_constant
1265                         ? BFD_RELOC_SPARC_HM10 : BFD_RELOC_NONE);
1266       output_insn (insn, &the_insn);
1267     }
1268
1269   if (need_lo10_p)
1270     {
1271       /* FIXME: One nice optimization to do here is to OR the low part
1272          with the highpart if hi22 isn't needed and the low part is
1273          positive.  */
1274       the_insn.opcode = (OR_INSN | (need_hi22_p ? RS1 (dstreg) : 0)
1275                          | RD (dstreg)
1276                          | IMMED
1277                          | (lower32 & (need_hi22_p ? 0x3ff : 0x1fff)));
1278       the_insn.reloc = (the_insn.exp.X_op != O_constant
1279                         ? BFD_RELOC_LO10 : BFD_RELOC_NONE);
1280       output_insn (insn, &the_insn);
1281     }
1282
1283   /* If we needed to build the upper part, shift it into place.  */
1284   if (need_hh22_p || need_hm10_p)
1285     {
1286       the_insn.opcode = (SLLX_INSN | RS1 (upper_dstreg) | RD (upper_dstreg)
1287                          | IMMED | 32);
1288       the_insn.reloc = BFD_RELOC_NONE;
1289       output_insn (insn, &the_insn);
1290     }
1291
1292   /* To get -1 in upper32, we do sethi %hi(~x), r; xor r, -0x400 | x, r.  */
1293   if (need_xor10_p)
1294     {
1295       the_insn.opcode = (XOR_INSN | RS1 (dstreg) | RD (dstreg) | IMMED
1296                          | 0x1c00 | (lower32 & 0x3ff));
1297       the_insn.reloc = BFD_RELOC_NONE;
1298       output_insn (insn, &the_insn);
1299     }
1300
1301   /* If we needed to build both upper and lower parts, OR them together.  */
1302   else if ((need_hh22_p || need_hm10_p) && (need_hi22_p || need_lo10_p))
1303     {
1304       the_insn.opcode = (OR_INSN | RS1 (dstreg) | RS2 (upper_dstreg)
1305                          | RD (dstreg));
1306       the_insn.reloc = BFD_RELOC_NONE;
1307       output_insn (insn, &the_insn);
1308     }
1309 }
1310 \f
1311 /* Main entry point to assemble one instruction.  */
1312
1313 void
1314 md_assemble (str)
1315      char *str;
1316 {
1317   const struct sparc_opcode *insn;
1318   int special_case;
1319
1320   know (str);
1321   special_case = sparc_ip (str, &insn);
1322   if (insn == NULL)
1323     return;
1324
1325   /* We warn about attempts to put a floating point branch in a delay slot,
1326      unless the delay slot has been annulled.  */
1327   if (last_insn != NULL
1328       && (insn->flags & F_FBR) != 0
1329       && (last_insn->flags & F_DELAYED) != 0
1330       /* ??? This test isn't completely accurate.  We assume anything with
1331          F_{UNBR,CONDBR,FBR} set is annullable.  */
1332       && ((last_insn->flags & (F_UNBR | F_CONDBR | F_FBR)) == 0
1333           || (last_opcode & ANNUL) == 0))
1334     as_warn (_("FP branch in delay slot"));
1335
1336   /* SPARC before v9 requires a nop instruction between a floating
1337      point instruction and a floating point branch.  We insert one
1338      automatically, with a warning.  */
1339   if (max_architecture < SPARC_OPCODE_ARCH_V9
1340       && last_insn != NULL
1341       && (insn->flags & F_FBR) != 0
1342       && (last_insn->flags & F_FLOAT) != 0)
1343     {
1344       struct sparc_it nop_insn;
1345
1346       nop_insn.opcode = NOP_INSN;
1347       nop_insn.reloc = BFD_RELOC_NONE;
1348       output_insn (insn, &nop_insn);
1349       as_warn (_("FP branch preceded by FP instruction; NOP inserted"));
1350     }
1351
1352   switch (special_case)
1353     {
1354     case SPECIAL_CASE_NONE:
1355       /* Normal insn.  */
1356       output_insn (insn, &the_insn);
1357       break;
1358
1359     case SPECIAL_CASE_SETSW:
1360       synthetize_setsw (insn);
1361       break;
1362
1363     case SPECIAL_CASE_SET:
1364       synthetize_setuw (insn);
1365       break;
1366
1367     case SPECIAL_CASE_SETX:
1368       synthetize_setx (insn);
1369       break;
1370
1371     case SPECIAL_CASE_FDIV:
1372       {
1373         int rd = (the_insn.opcode >> 25) & 0x1f;
1374
1375         output_insn (insn, &the_insn);
1376
1377         /* According to information leaked from Sun, the "fdiv" instructions
1378            on early SPARC machines would produce incorrect results sometimes.
1379            The workaround is to add an fmovs of the destination register to
1380            itself just after the instruction.  This was true on machines
1381            with Weitek 1165 float chips, such as the Sun-4/260 and /280.  */
1382         assert (the_insn.reloc == BFD_RELOC_NONE);
1383         the_insn.opcode = FMOVS_INSN | rd | RD (rd);
1384         output_insn (insn, &the_insn);
1385         return;
1386       }
1387
1388     default:
1389       as_fatal (_("failed special case insn sanity check"));
1390     }
1391 }
1392
1393 /* Subroutine of md_assemble to do the actual parsing.  */
1394
1395 static int
1396 sparc_ip (str, pinsn)
1397      char *str;
1398      const struct sparc_opcode **pinsn;
1399 {
1400   char *error_message = "";
1401   char *s;
1402   const char *args;
1403   char c;
1404   const struct sparc_opcode *insn;
1405   char *argsStart;
1406   unsigned long opcode;
1407   unsigned int mask = 0;
1408   int match = 0;
1409   int comma = 0;
1410   int v9_arg_p;
1411   int special_case = SPECIAL_CASE_NONE;
1412
1413   s = str;
1414   if (ISLOWER (*s))
1415     {
1416       do
1417         ++s;
1418       while (ISLOWER (*s) || ISDIGIT (*s));
1419     }
1420
1421   switch (*s)
1422     {
1423     case '\0':
1424       break;
1425
1426     case ',':
1427       comma = 1;
1428       /* Fall through.  */
1429
1430     case ' ':
1431       *s++ = '\0';
1432       break;
1433
1434     default:
1435       as_bad (_("Unknown opcode: `%s'"), str);
1436       *pinsn = NULL;
1437       return special_case;
1438     }
1439   insn = (struct sparc_opcode *) hash_find (op_hash, str);
1440   *pinsn = insn;
1441   if (insn == NULL)
1442     {
1443       as_bad (_("Unknown opcode: `%s'"), str);
1444       return special_case;
1445     }
1446   if (comma)
1447     {
1448       *--s = ',';
1449     }
1450
1451   argsStart = s;
1452   for (;;)
1453     {
1454       opcode = insn->match;
1455       memset (&the_insn, '\0', sizeof (the_insn));
1456       the_insn.reloc = BFD_RELOC_NONE;
1457       v9_arg_p = 0;
1458
1459       /* Build the opcode, checking as we go to make sure that the
1460          operands match.  */
1461       for (args = insn->args;; ++args)
1462         {
1463           switch (*args)
1464             {
1465             case 'K':
1466               {
1467                 int kmask = 0;
1468
1469                 /* Parse a series of masks.  */
1470                 if (*s == '#')
1471                   {
1472                     while (*s == '#')
1473                       {
1474                         int mask;
1475
1476                         if (! parse_keyword_arg (sparc_encode_membar, &s,
1477                                                  &mask))
1478                           {
1479                             error_message = _(": invalid membar mask name");
1480                             goto error;
1481                           }
1482                         kmask |= mask;
1483                         while (*s == ' ')
1484                           ++s;
1485                         if (*s == '|' || *s == '+')
1486                           ++s;
1487                         while (*s == ' ')
1488                           ++s;
1489                       }
1490                   }
1491                 else
1492                   {
1493                     if (! parse_const_expr_arg (&s, &kmask))
1494                       {
1495                         error_message = _(": invalid membar mask expression");
1496                         goto error;
1497                       }
1498                     if (kmask < 0 || kmask > 127)
1499                       {
1500                         error_message = _(": invalid membar mask number");
1501                         goto error;
1502                       }
1503                   }
1504
1505                 opcode |= MEMBAR (kmask);
1506                 continue;
1507               }
1508
1509             case '3':
1510               {
1511                 int smask = 0;
1512
1513                 if (! parse_const_expr_arg (&s, &smask))
1514                   {
1515                     error_message = _(": invalid siam mode expression");
1516                     goto error;
1517                   }
1518                 if (smask < 0 || smask > 7)
1519                   {
1520                     error_message = _(": invalid siam mode number");
1521                     goto error;
1522                   }
1523                 opcode |= smask;
1524                 continue;
1525               }
1526
1527             case '*':
1528               {
1529                 int fcn = 0;
1530
1531                 /* Parse a prefetch function.  */
1532                 if (*s == '#')
1533                   {
1534                     if (! parse_keyword_arg (sparc_encode_prefetch, &s, &fcn))
1535                       {
1536                         error_message = _(": invalid prefetch function name");
1537                         goto error;
1538                       }
1539                   }
1540                 else
1541                   {
1542                     if (! parse_const_expr_arg (&s, &fcn))
1543                       {
1544                         error_message = _(": invalid prefetch function expression");
1545                         goto error;
1546                       }
1547                     if (fcn < 0 || fcn > 31)
1548                       {
1549                         error_message = _(": invalid prefetch function number");
1550                         goto error;
1551                       }
1552                   }
1553                 opcode |= RD (fcn);
1554                 continue;
1555               }
1556
1557             case '!':
1558             case '?':
1559               /* Parse a sparc64 privileged register.  */
1560               if (*s == '%')
1561                 {
1562                   struct priv_reg_entry *p = priv_reg_table;
1563                   unsigned int len = 9999999; /* Init to make gcc happy.  */
1564
1565                   s += 1;
1566                   while (p->name[0] > s[0])
1567                     p++;
1568                   while (p->name[0] == s[0])
1569                     {
1570                       len = strlen (p->name);
1571                       if (strncmp (p->name, s, len) == 0)
1572                         break;
1573                       p++;
1574                     }
1575                   if (p->name[0] != s[0])
1576                     {
1577                       error_message = _(": unrecognizable privileged register");
1578                       goto error;
1579                     }
1580                   if (*args == '?')
1581                     opcode |= (p->regnum << 14);
1582                   else
1583                     opcode |= (p->regnum << 25);
1584                   s += len;
1585                   continue;
1586                 }
1587               else
1588                 {
1589                   error_message = _(": unrecognizable privileged register");
1590                   goto error;
1591                 }
1592
1593             case '$':
1594             case '%':
1595               /* Parse a sparc64 hyperprivileged register.  */
1596               if (*s == '%')
1597                 {
1598                   struct priv_reg_entry *p = hpriv_reg_table;
1599                   unsigned int len = 9999999; /* Init to make gcc happy.  */
1600
1601                   s += 1;
1602                   while (p->name[0] > s[0])
1603                     p++;
1604                   while (p->name[0] == s[0])
1605                     {
1606                       len = strlen (p->name);
1607                       if (strncmp (p->name, s, len) == 0)
1608                         break;
1609                       p++;
1610                     }
1611                   if (p->name[0] != s[0])
1612                     {
1613                       error_message = _(": unrecognizable hyperprivileged register");
1614                       goto error;
1615                     }
1616                   if (*args == '$')
1617                     opcode |= (p->regnum << 14);
1618                   else
1619                     opcode |= (p->regnum << 25);
1620                   s += len;
1621                   continue;
1622                 }
1623               else
1624                 {
1625                   error_message = _(": unrecognizable hyperprivileged register");
1626                   goto error;
1627                 }
1628
1629             case '_':
1630             case '/':
1631               /* Parse a v9a/v9b ancillary state register.  */
1632               if (*s == '%')
1633                 {
1634                   struct priv_reg_entry *p = v9a_asr_table;
1635                   unsigned int len = 9999999; /* Init to make gcc happy.  */
1636
1637                   s += 1;
1638                   while (p->name[0] > s[0])
1639                     p++;
1640                   while (p->name[0] == s[0])
1641                     {
1642                       len = strlen (p->name);
1643                       if (strncmp (p->name, s, len) == 0)
1644                         break;
1645                       p++;
1646                     }
1647                   if (p->name[0] != s[0])
1648                     {
1649                       error_message = _(": unrecognizable v9a or v9b ancillary state register");
1650                       goto error;
1651                     }
1652                   if (*args == '/' && (p->regnum == 20 || p->regnum == 21))
1653                     {
1654                       error_message = _(": rd on write only ancillary state register");
1655                       goto error;
1656                     }
1657                   if (p->regnum >= 24
1658                       && (insn->architecture
1659                           & SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V9A)))
1660                     {
1661                       /* %sys_tick and %sys_tick_cmpr are v9bnotv9a */
1662                       error_message = _(": unrecognizable v9a ancillary state register");
1663                       goto error;
1664                     }
1665                   if (*args == '/')
1666                     opcode |= (p->regnum << 14);
1667                   else
1668                     opcode |= (p->regnum << 25);
1669                   s += len;
1670                   continue;
1671                 }
1672               else
1673                 {
1674                   error_message = _(": unrecognizable v9a or v9b ancillary state register");
1675                   goto error;
1676                 }
1677
1678             case 'M':
1679             case 'm':
1680               if (strncmp (s, "%asr", 4) == 0)
1681                 {
1682                   s += 4;
1683
1684                   if (ISDIGIT (*s))
1685                     {
1686                       long num = 0;
1687
1688                       while (ISDIGIT (*s))
1689                         {
1690                           num = num * 10 + *s - '0';
1691                           ++s;
1692                         }
1693
1694                       if (current_architecture >= SPARC_OPCODE_ARCH_V9)
1695                         {
1696                           if (num < 16 || 31 < num)
1697                             {
1698                               error_message = _(": asr number must be between 16 and 31");
1699                               goto error;
1700                             }
1701                         }
1702                       else
1703                         {
1704                           if (num < 0 || 31 < num)
1705                             {
1706                               error_message = _(": asr number must be between 0 and 31");
1707                               goto error;
1708                             }
1709                         }
1710
1711                       opcode |= (*args == 'M' ? RS1 (num) : RD (num));
1712                       continue;
1713                     }
1714                   else
1715                     {
1716                       error_message = _(": expecting %asrN");
1717                       goto error;
1718                     }
1719                 } /* if %asr  */
1720               break;
1721
1722             case 'I':
1723               the_insn.reloc = BFD_RELOC_SPARC_11;
1724               goto immediate;
1725
1726             case 'j':
1727               the_insn.reloc = BFD_RELOC_SPARC_10;
1728               goto immediate;
1729
1730             case 'X':
1731               /* V8 systems don't understand BFD_RELOC_SPARC_5.  */
1732               if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1733                 the_insn.reloc = BFD_RELOC_SPARC_5;
1734               else
1735                 the_insn.reloc = BFD_RELOC_SPARC13;
1736               /* These fields are unsigned, but for upward compatibility,
1737                  allow negative values as well.  */
1738               goto immediate;
1739
1740             case 'Y':
1741               /* V8 systems don't understand BFD_RELOC_SPARC_6.  */
1742               if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1743                 the_insn.reloc = BFD_RELOC_SPARC_6;
1744               else
1745                 the_insn.reloc = BFD_RELOC_SPARC13;
1746               /* These fields are unsigned, but for upward compatibility,
1747                  allow negative values as well.  */
1748               goto immediate;
1749
1750             case 'k':
1751               the_insn.reloc = /* RELOC_WDISP2_14 */ BFD_RELOC_SPARC_WDISP16;
1752               the_insn.pcrel = 1;
1753               goto immediate;
1754
1755             case 'G':
1756               the_insn.reloc = BFD_RELOC_SPARC_WDISP19;
1757               the_insn.pcrel = 1;
1758               goto immediate;
1759
1760             case 'N':
1761               if (*s == 'p' && s[1] == 'n')
1762                 {
1763                   s += 2;
1764                   continue;
1765                 }
1766               break;
1767
1768             case 'T':
1769               if (*s == 'p' && s[1] == 't')
1770                 {
1771                   s += 2;
1772                   continue;
1773                 }
1774               break;
1775
1776             case 'z':
1777               if (*s == ' ')
1778                 {
1779                   ++s;
1780                 }
1781               if (strncmp (s, "%icc", 4) == 0)
1782                 {
1783                   s += 4;
1784                   continue;
1785                 }
1786               break;
1787
1788             case 'Z':
1789               if (*s == ' ')
1790                 {
1791                   ++s;
1792                 }
1793               if (strncmp (s, "%xcc", 4) == 0)
1794                 {
1795                   s += 4;
1796                   continue;
1797                 }
1798               break;
1799
1800             case '6':
1801               if (*s == ' ')
1802                 {
1803                   ++s;
1804                 }
1805               if (strncmp (s, "%fcc0", 5) == 0)
1806                 {
1807                   s += 5;
1808                   continue;
1809                 }
1810               break;
1811
1812             case '7':
1813               if (*s == ' ')
1814                 {
1815                   ++s;
1816                 }
1817               if (strncmp (s, "%fcc1", 5) == 0)
1818                 {
1819                   s += 5;
1820                   continue;
1821                 }
1822               break;
1823
1824             case '8':
1825               if (*s == ' ')
1826                 {
1827                   ++s;
1828                 }
1829               if (strncmp (s, "%fcc2", 5) == 0)
1830                 {
1831                   s += 5;
1832                   continue;
1833                 }
1834               break;
1835
1836             case '9':
1837               if (*s == ' ')
1838                 {
1839                   ++s;
1840                 }
1841               if (strncmp (s, "%fcc3", 5) == 0)
1842                 {
1843                   s += 5;
1844                   continue;
1845                 }
1846               break;
1847
1848             case 'P':
1849               if (strncmp (s, "%pc", 3) == 0)
1850                 {
1851                   s += 3;
1852                   continue;
1853                 }
1854               break;
1855
1856             case 'W':
1857               if (strncmp (s, "%tick", 5) == 0)
1858                 {
1859                   s += 5;
1860                   continue;
1861                 }
1862               break;
1863
1864             case '\0':          /* End of args.  */
1865               if (s[0] == ',' && s[1] == '%')
1866                 {
1867                   static const struct tls_ops {
1868                     /* The name as it appears in assembler.  */
1869                     char *name;
1870                     /* strlen (name), precomputed for speed */
1871                     int len;
1872                     /* The reloc this pseudo-op translates to.  */
1873                     int reloc;
1874                     /* 1 if call.  */
1875                     int call;
1876                   } tls_ops[] = {
1877                     { "tgd_add", 7, BFD_RELOC_SPARC_TLS_GD_ADD, 0 },
1878                     { "tgd_call", 8, BFD_RELOC_SPARC_TLS_GD_CALL, 1 },
1879                     { "tldm_add", 8, BFD_RELOC_SPARC_TLS_LDM_ADD, 0 },
1880                     { "tldm_call", 9, BFD_RELOC_SPARC_TLS_LDM_CALL, 1 },
1881                     { "tldo_add", 8, BFD_RELOC_SPARC_TLS_LDO_ADD, 0 },
1882                     { "tie_ldx", 7, BFD_RELOC_SPARC_TLS_IE_LDX, 0 },
1883                     { "tie_ld", 6, BFD_RELOC_SPARC_TLS_IE_LD, 0 },
1884                     { "tie_add", 7, BFD_RELOC_SPARC_TLS_IE_ADD, 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 /* This is identical to the md_atof in m68k.c.  I think this is right,
2950    but I'm not sure.
2951
2952    Turn a string in input_line_pointer into a floating point constant
2953    of type TYPE, and store the appropriate bytes in *LITP.  The number
2954    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
2955    returned, or NULL on OK.  */
2956
2957 /* Equal to MAX_PRECISION in atof-ieee.c.  */
2958 #define MAX_LITTLENUMS 6
2959
2960 char *
2961 md_atof (type, litP, sizeP)
2962      char type;
2963      char *litP;
2964      int *sizeP;
2965 {
2966   int i, prec;
2967   LITTLENUM_TYPE words[MAX_LITTLENUMS];
2968   char *t;
2969
2970   switch (type)
2971     {
2972     case 'f':
2973     case 'F':
2974     case 's':
2975     case 'S':
2976       prec = 2;
2977       break;
2978
2979     case 'd':
2980     case 'D':
2981     case 'r':
2982     case 'R':
2983       prec = 4;
2984       break;
2985
2986     case 'x':
2987     case 'X':
2988       prec = 6;
2989       break;
2990
2991     case 'p':
2992     case 'P':
2993       prec = 6;
2994       break;
2995
2996     default:
2997       *sizeP = 0;
2998       return _("Bad call to MD_ATOF()");
2999     }
3000
3001   t = atof_ieee (input_line_pointer, type, words);
3002   if (t)
3003     input_line_pointer = t;
3004   *sizeP = prec * sizeof (LITTLENUM_TYPE);
3005
3006   if (target_big_endian)
3007     {
3008       for (i = 0; i < prec; i++)
3009         {
3010           md_number_to_chars (litP, (valueT) words[i],
3011                               sizeof (LITTLENUM_TYPE));
3012           litP += sizeof (LITTLENUM_TYPE);
3013         }
3014     }
3015   else
3016     {
3017       for (i = prec - 1; i >= 0; i--)
3018         {
3019           md_number_to_chars (litP, (valueT) words[i],
3020                               sizeof (LITTLENUM_TYPE));
3021           litP += sizeof (LITTLENUM_TYPE);
3022         }
3023     }
3024
3025   return 0;
3026 }
3027
3028 /* Write a value out to the object file, using the appropriate
3029    endianness.  */
3030
3031 void
3032 md_number_to_chars (buf, val, n)
3033      char *buf;
3034      valueT val;
3035      int n;
3036 {
3037   if (target_big_endian)
3038     number_to_chars_bigendian (buf, val, n);
3039   else if (target_little_endian_data
3040            && ((n == 4 || n == 2) && ~now_seg->flags & SEC_ALLOC))
3041     /* Output debug words, which are not in allocated sections, as big
3042        endian.  */
3043     number_to_chars_bigendian (buf, val, n);
3044   else if (target_little_endian_data || ! target_big_endian)
3045     number_to_chars_littleendian (buf, val, n);
3046 }
3047 \f
3048 /* Apply a fixS to the frags, now that we know the value it ought to
3049    hold.  */
3050
3051 void
3052 md_apply_fix3 (fixP, valP, segment)
3053      fixS *fixP;
3054      valueT *valP;
3055      segT segment ATTRIBUTE_UNUSED;
3056 {
3057   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
3058   offsetT val = * (offsetT *) valP;
3059   long insn;
3060
3061   assert (fixP->fx_r_type < BFD_RELOC_UNUSED);
3062
3063   fixP->fx_addnumber = val;     /* Remember value for emit_reloc.  */
3064
3065 #ifdef OBJ_ELF
3066   /* SPARC ELF relocations don't use an addend in the data field.  */
3067   if (fixP->fx_addsy != NULL)
3068     {
3069       switch (fixP->fx_r_type)
3070         {
3071         case BFD_RELOC_SPARC_TLS_GD_HI22:
3072         case BFD_RELOC_SPARC_TLS_GD_LO10:
3073         case BFD_RELOC_SPARC_TLS_GD_ADD:
3074         case BFD_RELOC_SPARC_TLS_GD_CALL:
3075         case BFD_RELOC_SPARC_TLS_LDM_HI22:
3076         case BFD_RELOC_SPARC_TLS_LDM_LO10:
3077         case BFD_RELOC_SPARC_TLS_LDM_ADD:
3078         case BFD_RELOC_SPARC_TLS_LDM_CALL:
3079         case BFD_RELOC_SPARC_TLS_LDO_HIX22:
3080         case BFD_RELOC_SPARC_TLS_LDO_LOX10:
3081         case BFD_RELOC_SPARC_TLS_LDO_ADD:
3082         case BFD_RELOC_SPARC_TLS_IE_HI22:
3083         case BFD_RELOC_SPARC_TLS_IE_LO10:
3084         case BFD_RELOC_SPARC_TLS_IE_LD:
3085         case BFD_RELOC_SPARC_TLS_IE_LDX:
3086         case BFD_RELOC_SPARC_TLS_IE_ADD:
3087         case BFD_RELOC_SPARC_TLS_LE_HIX22:
3088         case BFD_RELOC_SPARC_TLS_LE_LOX10:
3089         case BFD_RELOC_SPARC_TLS_DTPMOD32:
3090         case BFD_RELOC_SPARC_TLS_DTPMOD64:
3091         case BFD_RELOC_SPARC_TLS_DTPOFF32:
3092         case BFD_RELOC_SPARC_TLS_DTPOFF64:
3093         case BFD_RELOC_SPARC_TLS_TPOFF32:
3094         case BFD_RELOC_SPARC_TLS_TPOFF64:
3095           S_SET_THREAD_LOCAL (fixP->fx_addsy);
3096
3097         default:
3098           break;
3099         }
3100
3101       return;
3102     }
3103 #endif
3104
3105   /* This is a hack.  There should be a better way to
3106      handle this.  Probably in terms of howto fields, once
3107      we can look at these fixups in terms of howtos.  */
3108   if (fixP->fx_r_type == BFD_RELOC_32_PCREL_S2 && fixP->fx_addsy)
3109     val += fixP->fx_where + fixP->fx_frag->fr_address;
3110
3111 #ifdef OBJ_AOUT
3112   /* FIXME: More ridiculous gas reloc hacking.  If we are going to
3113      generate a reloc, then we just want to let the reloc addend set
3114      the value.  We do not want to also stuff the addend into the
3115      object file.  Including the addend in the object file works when
3116      doing a static link, because the linker will ignore the object
3117      file contents.  However, the dynamic linker does not ignore the
3118      object file contents.  */
3119   if (fixP->fx_addsy != NULL
3120       && fixP->fx_r_type != BFD_RELOC_32_PCREL_S2)
3121     val = 0;
3122
3123   /* When generating PIC code, we do not want an addend for a reloc
3124      against a local symbol.  We adjust fx_addnumber to cancel out the
3125      value already included in val, and to also cancel out the
3126      adjustment which bfd_install_relocation will create.  */
3127   if (sparc_pic_code
3128       && fixP->fx_r_type != BFD_RELOC_32_PCREL_S2
3129       && fixP->fx_addsy != NULL
3130       && ! S_IS_COMMON (fixP->fx_addsy)
3131       && symbol_section_p (fixP->fx_addsy))
3132     fixP->fx_addnumber -= 2 * S_GET_VALUE (fixP->fx_addsy);
3133
3134   /* When generating PIC code, we need to fiddle to get
3135      bfd_install_relocation to do the right thing for a PC relative
3136      reloc against a local symbol which we are going to keep.  */
3137   if (sparc_pic_code
3138       && fixP->fx_r_type == BFD_RELOC_32_PCREL_S2
3139       && fixP->fx_addsy != NULL
3140       && (S_IS_EXTERNAL (fixP->fx_addsy)
3141           || S_IS_WEAK (fixP->fx_addsy))
3142       && S_IS_DEFINED (fixP->fx_addsy)
3143       && ! S_IS_COMMON (fixP->fx_addsy))
3144     {
3145       val = 0;
3146       fixP->fx_addnumber -= 2 * S_GET_VALUE (fixP->fx_addsy);
3147     }
3148 #endif
3149
3150   /* If this is a data relocation, just output VAL.  */
3151
3152   if (fixP->fx_r_type == BFD_RELOC_16
3153       || fixP->fx_r_type == BFD_RELOC_SPARC_UA16)
3154     {
3155       md_number_to_chars (buf, val, 2);
3156     }
3157   else if (fixP->fx_r_type == BFD_RELOC_32
3158            || fixP->fx_r_type == BFD_RELOC_SPARC_UA32
3159            || fixP->fx_r_type == BFD_RELOC_SPARC_REV32)
3160     {
3161       md_number_to_chars (buf, val, 4);
3162     }
3163   else if (fixP->fx_r_type == BFD_RELOC_64
3164            || fixP->fx_r_type == BFD_RELOC_SPARC_UA64)
3165     {
3166       md_number_to_chars (buf, val, 8);
3167     }
3168   else if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3169            || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3170     {
3171       fixP->fx_done = 0;
3172       return;
3173     }
3174   else
3175     {
3176       /* It's a relocation against an instruction.  */
3177
3178       if (INSN_BIG_ENDIAN)
3179         insn = bfd_getb32 ((unsigned char *) buf);
3180       else
3181         insn = bfd_getl32 ((unsigned char *) buf);
3182
3183       switch (fixP->fx_r_type)
3184         {
3185         case BFD_RELOC_32_PCREL_S2:
3186           val = val >> 2;
3187           /* FIXME: This increment-by-one deserves a comment of why it's
3188              being done!  */
3189           if (! sparc_pic_code
3190               || fixP->fx_addsy == NULL
3191               || symbol_section_p (fixP->fx_addsy))
3192             ++val;
3193
3194           insn |= val & 0x3fffffff;
3195
3196           /* See if we have a delay slot.  */
3197           if (sparc_relax && fixP->fx_where + 8 <= fixP->fx_frag->fr_fix)
3198             {
3199 #define G0              0
3200 #define O7              15
3201 #define XCC             (2 << 20)
3202 #define COND(x)         (((x)&0xf)<<25)
3203 #define CONDA           COND(0x8)
3204 #define INSN_BPA        (F2(0,1) | CONDA | BPRED | XCC)
3205 #define INSN_BA         (F2(0,2) | CONDA)
3206 #define INSN_OR         F3(2, 0x2, 0)
3207 #define INSN_NOP        F2(0,4)
3208
3209               long delay;
3210
3211               /* If the instruction is a call with either:
3212                  restore
3213                  arithmetic instruction with rd == %o7
3214                  where rs1 != %o7 and rs2 if it is register != %o7
3215                  then we can optimize if the call destination is near
3216                  by changing the call into a branch always.  */
3217               if (INSN_BIG_ENDIAN)
3218                 delay = bfd_getb32 ((unsigned char *) buf + 4);
3219               else
3220                 delay = bfd_getl32 ((unsigned char *) buf + 4);
3221               if ((insn & OP (~0)) != OP (1) || (delay & OP (~0)) != OP (2))
3222                 break;
3223               if ((delay & OP3 (~0)) != OP3 (0x3d) /* Restore.  */
3224                   && ((delay & OP3 (0x28)) != 0 /* Arithmetic.  */
3225                       || ((delay & RD (~0)) != RD (O7))))
3226                 break;
3227               if ((delay & RS1 (~0)) == RS1 (O7)
3228                   || ((delay & F3I (~0)) == 0
3229                       && (delay & RS2 (~0)) == RS2 (O7)))
3230                 break;
3231               /* Ensure the branch will fit into simm22.  */
3232               if ((val & 0x3fe00000)
3233                   && (val & 0x3fe00000) != 0x3fe00000)
3234                 break;
3235               /* Check if the arch is v9 and branch will fit
3236                  into simm19.  */
3237               if (((val & 0x3c0000) == 0
3238                    || (val & 0x3c0000) == 0x3c0000)
3239                   && (sparc_arch_size == 64
3240                       || current_architecture >= SPARC_OPCODE_ARCH_V9))
3241                 /* ba,pt %xcc  */
3242                 insn = INSN_BPA | (val & 0x7ffff);
3243               else
3244                 /* ba  */
3245                 insn = INSN_BA | (val & 0x3fffff);
3246               if (fixP->fx_where >= 4
3247                   && ((delay & (0xffffffff ^ RS1 (~0)))
3248                       == (INSN_OR | RD (O7) | RS2 (G0))))
3249                 {
3250                   long setter;
3251                   int reg;
3252
3253                   if (INSN_BIG_ENDIAN)
3254                     setter = bfd_getb32 ((unsigned char *) buf - 4);
3255                   else
3256                     setter = bfd_getl32 ((unsigned char *) buf - 4);
3257                   if ((setter & (0xffffffff ^ RD (~0)))
3258                       != (INSN_OR | RS1 (O7) | RS2 (G0)))
3259                     break;
3260                   /* The sequence was
3261                      or %o7, %g0, %rN
3262                      call foo
3263                      or %rN, %g0, %o7
3264
3265                      If call foo was replaced with ba, replace
3266                      or %rN, %g0, %o7 with nop.  */
3267                   reg = (delay & RS1 (~0)) >> 14;
3268                   if (reg != ((setter & RD (~0)) >> 25)
3269                       || reg == G0 || reg == O7)
3270                     break;
3271
3272                   if (INSN_BIG_ENDIAN)
3273                     bfd_putb32 (INSN_NOP, (unsigned char *) buf + 4);
3274                   else
3275                     bfd_putl32 (INSN_NOP, (unsigned char *) buf + 4);
3276                 }
3277             }
3278           break;
3279
3280         case BFD_RELOC_SPARC_11:
3281           if (! in_signed_range (val, 0x7ff))
3282             as_bad_where (fixP->fx_file, fixP->fx_line,
3283                           _("relocation overflow"));
3284           insn |= val & 0x7ff;
3285           break;
3286
3287         case BFD_RELOC_SPARC_10:
3288           if (! in_signed_range (val, 0x3ff))
3289             as_bad_where (fixP->fx_file, fixP->fx_line,
3290                           _("relocation overflow"));
3291           insn |= val & 0x3ff;
3292           break;
3293
3294         case BFD_RELOC_SPARC_7:
3295           if (! in_bitfield_range (val, 0x7f))
3296             as_bad_where (fixP->fx_file, fixP->fx_line,
3297                           _("relocation overflow"));
3298           insn |= val & 0x7f;
3299           break;
3300
3301         case BFD_RELOC_SPARC_6:
3302           if (! in_bitfield_range (val, 0x3f))
3303             as_bad_where (fixP->fx_file, fixP->fx_line,
3304                           _("relocation overflow"));
3305           insn |= val & 0x3f;
3306           break;
3307
3308         case BFD_RELOC_SPARC_5:
3309           if (! in_bitfield_range (val, 0x1f))
3310             as_bad_where (fixP->fx_file, fixP->fx_line,
3311                           _("relocation overflow"));
3312           insn |= val & 0x1f;
3313           break;
3314
3315         case BFD_RELOC_SPARC_WDISP16:
3316           /* FIXME: simplify.  */
3317           if (((val > 0) && (val & ~0x3fffc))
3318               || ((val < 0) && (~(val - 1) & ~0x3fffc)))
3319             as_bad_where (fixP->fx_file, fixP->fx_line,
3320                           _("relocation overflow"));
3321           /* FIXME: The +1 deserves a comment.  */
3322           val = (val >> 2) + 1;
3323           insn |= ((val & 0xc000) << 6) | (val & 0x3fff);
3324           break;
3325
3326         case BFD_RELOC_SPARC_WDISP19:
3327           /* FIXME: simplify.  */
3328           if (((val > 0) && (val & ~0x1ffffc))
3329               || ((val < 0) && (~(val - 1) & ~0x1ffffc)))
3330             as_bad_where (fixP->fx_file, fixP->fx_line,
3331                           _("relocation overflow"));
3332           /* FIXME: The +1 deserves a comment.  */
3333           val = (val >> 2) + 1;
3334           insn |= val & 0x7ffff;
3335           break;
3336
3337         case BFD_RELOC_SPARC_HH22:
3338           val = BSR (val, 32);
3339           /* Fall through.  */
3340
3341         case BFD_RELOC_SPARC_LM22:
3342         case BFD_RELOC_HI22:
3343           if (!fixP->fx_addsy)
3344             insn |= (val >> 10) & 0x3fffff;
3345           else
3346             /* FIXME: Need comment explaining why we do this.  */
3347             insn &= ~0xffff;
3348           break;
3349
3350         case BFD_RELOC_SPARC22:
3351           if (val & ~0x003fffff)
3352             as_bad_where (fixP->fx_file, fixP->fx_line,
3353                           _("relocation overflow"));
3354           insn |= (val & 0x3fffff);
3355           break;
3356
3357         case BFD_RELOC_SPARC_HM10:
3358           val = BSR (val, 32);
3359           /* Fall through.  */
3360
3361         case BFD_RELOC_LO10:
3362           if (!fixP->fx_addsy)
3363             insn |= val & 0x3ff;
3364           else
3365             /* FIXME: Need comment explaining why we do this.  */
3366             insn &= ~0xff;
3367           break;
3368
3369         case BFD_RELOC_SPARC_OLO10:
3370           val &= 0x3ff;
3371           val += fixP->tc_fix_data;
3372           /* Fall through.  */
3373
3374         case BFD_RELOC_SPARC13:
3375           if (! in_signed_range (val, 0x1fff))
3376             as_bad_where (fixP->fx_file, fixP->fx_line,
3377                           _("relocation overflow"));
3378           insn |= val & 0x1fff;
3379           break;
3380
3381         case BFD_RELOC_SPARC_WDISP22:
3382           val = (val >> 2) + 1;
3383           /* Fall through.  */
3384         case BFD_RELOC_SPARC_BASE22:
3385           insn |= val & 0x3fffff;
3386           break;
3387
3388         case BFD_RELOC_SPARC_H44:
3389           if (!fixP->fx_addsy)
3390             {
3391               bfd_vma tval = val;
3392               tval >>= 22;
3393               insn |= tval & 0x3fffff;
3394             }
3395           break;
3396
3397         case BFD_RELOC_SPARC_M44:
3398           if (!fixP->fx_addsy)
3399             insn |= (val >> 12) & 0x3ff;
3400           break;
3401
3402         case BFD_RELOC_SPARC_L44:
3403           if (!fixP->fx_addsy)
3404             insn |= val & 0xfff;
3405           break;
3406
3407         case BFD_RELOC_SPARC_HIX22:
3408           if (!fixP->fx_addsy)
3409             {
3410               val ^= ~(offsetT) 0;
3411               insn |= (val >> 10) & 0x3fffff;
3412             }
3413           break;
3414
3415         case BFD_RELOC_SPARC_LOX10:
3416           if (!fixP->fx_addsy)
3417             insn |= 0x1c00 | (val & 0x3ff);
3418           break;
3419
3420         case BFD_RELOC_NONE:
3421         default:
3422           as_bad_where (fixP->fx_file, fixP->fx_line,
3423                         _("bad or unhandled relocation type: 0x%02x"),
3424                         fixP->fx_r_type);
3425           break;
3426         }
3427
3428       if (INSN_BIG_ENDIAN)
3429         bfd_putb32 (insn, (unsigned char *) buf);
3430       else
3431         bfd_putl32 (insn, (unsigned char *) buf);
3432     }
3433
3434   /* Are we finished with this relocation now?  */
3435   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
3436     fixP->fx_done = 1;
3437 }
3438
3439 /* Translate internal representation of relocation info to BFD target
3440    format.  */
3441
3442 arelent **
3443 tc_gen_reloc (section, fixp)
3444      asection *section ATTRIBUTE_UNUSED;
3445      fixS *fixp;
3446 {
3447   static arelent *relocs[3];
3448   arelent *reloc;
3449   bfd_reloc_code_real_type code;
3450
3451   relocs[0] = reloc = (arelent *) xmalloc (sizeof (arelent));
3452   relocs[1] = NULL;
3453
3454   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
3455   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
3456   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
3457
3458   switch (fixp->fx_r_type)
3459     {
3460     case BFD_RELOC_16:
3461     case BFD_RELOC_32:
3462     case BFD_RELOC_HI22:
3463     case BFD_RELOC_LO10:
3464     case BFD_RELOC_32_PCREL_S2:
3465     case BFD_RELOC_SPARC13:
3466     case BFD_RELOC_SPARC22:
3467     case BFD_RELOC_SPARC_BASE13:
3468     case BFD_RELOC_SPARC_WDISP16:
3469     case BFD_RELOC_SPARC_WDISP19:
3470     case BFD_RELOC_SPARC_WDISP22:
3471     case BFD_RELOC_64:
3472     case BFD_RELOC_SPARC_5:
3473     case BFD_RELOC_SPARC_6:
3474     case BFD_RELOC_SPARC_7:
3475     case BFD_RELOC_SPARC_10:
3476     case BFD_RELOC_SPARC_11:
3477     case BFD_RELOC_SPARC_HH22:
3478     case BFD_RELOC_SPARC_HM10:
3479     case BFD_RELOC_SPARC_LM22:
3480     case BFD_RELOC_SPARC_PC_HH22:
3481     case BFD_RELOC_SPARC_PC_HM10:
3482     case BFD_RELOC_SPARC_PC_LM22:
3483     case BFD_RELOC_SPARC_H44:
3484     case BFD_RELOC_SPARC_M44:
3485     case BFD_RELOC_SPARC_L44:
3486     case BFD_RELOC_SPARC_HIX22:
3487     case BFD_RELOC_SPARC_LOX10:
3488     case BFD_RELOC_SPARC_REV32:
3489     case BFD_RELOC_SPARC_OLO10:
3490     case BFD_RELOC_SPARC_UA16:
3491     case BFD_RELOC_SPARC_UA32:
3492     case BFD_RELOC_SPARC_UA64:
3493     case BFD_RELOC_8_PCREL:
3494     case BFD_RELOC_16_PCREL:
3495     case BFD_RELOC_32_PCREL:
3496     case BFD_RELOC_64_PCREL:
3497     case BFD_RELOC_SPARC_PLT32:
3498     case BFD_RELOC_SPARC_PLT64:
3499     case BFD_RELOC_VTABLE_ENTRY:
3500     case BFD_RELOC_VTABLE_INHERIT:
3501     case BFD_RELOC_SPARC_TLS_GD_HI22:
3502     case BFD_RELOC_SPARC_TLS_GD_LO10:
3503     case BFD_RELOC_SPARC_TLS_GD_ADD:
3504     case BFD_RELOC_SPARC_TLS_GD_CALL:
3505     case BFD_RELOC_SPARC_TLS_LDM_HI22:
3506     case BFD_RELOC_SPARC_TLS_LDM_LO10:
3507     case BFD_RELOC_SPARC_TLS_LDM_ADD:
3508     case BFD_RELOC_SPARC_TLS_LDM_CALL:
3509     case BFD_RELOC_SPARC_TLS_LDO_HIX22:
3510     case BFD_RELOC_SPARC_TLS_LDO_LOX10:
3511     case BFD_RELOC_SPARC_TLS_LDO_ADD:
3512     case BFD_RELOC_SPARC_TLS_IE_HI22:
3513     case BFD_RELOC_SPARC_TLS_IE_LO10:
3514     case BFD_RELOC_SPARC_TLS_IE_LD:
3515     case BFD_RELOC_SPARC_TLS_IE_LDX:
3516     case BFD_RELOC_SPARC_TLS_IE_ADD:
3517     case BFD_RELOC_SPARC_TLS_LE_HIX22:
3518     case BFD_RELOC_SPARC_TLS_LE_LOX10:
3519     case BFD_RELOC_SPARC_TLS_DTPOFF32:
3520     case BFD_RELOC_SPARC_TLS_DTPOFF64:
3521       code = fixp->fx_r_type;
3522       break;
3523     default:
3524       abort ();
3525       return NULL;
3526     }
3527
3528 #if defined (OBJ_ELF) || defined (OBJ_AOUT)
3529   /* If we are generating PIC code, we need to generate a different
3530      set of relocs.  */
3531
3532 #ifdef OBJ_ELF
3533 #define GOT_NAME "_GLOBAL_OFFSET_TABLE_"
3534 #else
3535 #define GOT_NAME "__GLOBAL_OFFSET_TABLE_"
3536 #endif
3537 #ifdef TE_VXWORKS
3538 #define GOTT_BASE "__GOTT_BASE__"
3539 #define GOTT_INDEX "__GOTT_INDEX__"
3540 #endif
3541
3542   /* This code must be parallel to the OBJ_ELF tc_fix_adjustable.  */
3543
3544   if (sparc_pic_code)
3545     {
3546       switch (code)
3547         {
3548         case BFD_RELOC_32_PCREL_S2:
3549           if (generic_force_reloc (fixp))
3550             code = BFD_RELOC_SPARC_WPLT30;
3551           break;
3552         case BFD_RELOC_HI22:
3553           code = BFD_RELOC_SPARC_GOT22;
3554           if (fixp->fx_addsy != NULL)
3555             {
3556               if (strcmp (S_GET_NAME (fixp->fx_addsy), GOT_NAME) == 0)
3557                 code = BFD_RELOC_SPARC_PC22;
3558 #ifdef TE_VXWORKS
3559               if (strcmp (S_GET_NAME (fixp->fx_addsy), GOTT_BASE) == 0
3560                   || strcmp (S_GET_NAME (fixp->fx_addsy), GOTT_INDEX) == 0)
3561                 code = BFD_RELOC_HI22; /* Unchanged.  */
3562 #endif
3563             }
3564           break;
3565         case BFD_RELOC_LO10:
3566           code = BFD_RELOC_SPARC_GOT10;
3567           if (fixp->fx_addsy != NULL)
3568             {
3569               if (strcmp (S_GET_NAME (fixp->fx_addsy), GOT_NAME) == 0)
3570                 code = BFD_RELOC_SPARC_PC10;
3571 #ifdef TE_VXWORKS
3572               if (strcmp (S_GET_NAME (fixp->fx_addsy), GOTT_BASE) == 0
3573                   || strcmp (S_GET_NAME (fixp->fx_addsy), GOTT_INDEX) == 0)
3574                 code = BFD_RELOC_LO10; /* Unchanged.  */
3575 #endif
3576             }
3577           break;
3578         case BFD_RELOC_SPARC13:
3579           code = BFD_RELOC_SPARC_GOT13;
3580           break;
3581         default:
3582           break;
3583         }
3584     }
3585 #endif /* defined (OBJ_ELF) || defined (OBJ_AOUT)  */
3586
3587   if (code == BFD_RELOC_SPARC_OLO10)
3588     reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO10);
3589   else
3590     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
3591   if (reloc->howto == 0)
3592     {
3593       as_bad_where (fixp->fx_file, fixp->fx_line,
3594                     _("internal error: can't export reloc type %d (`%s')"),
3595                     fixp->fx_r_type, bfd_get_reloc_code_name (code));
3596       xfree (reloc);
3597       relocs[0] = NULL;
3598       return relocs;
3599     }
3600
3601   /* @@ Why fx_addnumber sometimes and fx_offset other times?  */
3602 #ifdef OBJ_AOUT
3603
3604   if (reloc->howto->pc_relative == 0
3605       || code == BFD_RELOC_SPARC_PC10
3606       || code == BFD_RELOC_SPARC_PC22)
3607     reloc->addend = fixp->fx_addnumber;
3608   else if (sparc_pic_code
3609            && fixp->fx_r_type == BFD_RELOC_32_PCREL_S2
3610            && fixp->fx_addsy != NULL
3611            && (S_IS_EXTERNAL (fixp->fx_addsy)
3612                || S_IS_WEAK (fixp->fx_addsy))
3613            && S_IS_DEFINED (fixp->fx_addsy)
3614            && ! S_IS_COMMON (fixp->fx_addsy))
3615     reloc->addend = fixp->fx_addnumber;
3616   else
3617     reloc->addend = fixp->fx_offset - reloc->address;
3618
3619 #else /* elf or coff  */
3620
3621   if (code != BFD_RELOC_32_PCREL_S2
3622       && code != BFD_RELOC_SPARC_WDISP22
3623       && code != BFD_RELOC_SPARC_WDISP16
3624       && code != BFD_RELOC_SPARC_WDISP19
3625       && code != BFD_RELOC_SPARC_WPLT30
3626       && code != BFD_RELOC_SPARC_TLS_GD_CALL
3627       && code != BFD_RELOC_SPARC_TLS_LDM_CALL)
3628     reloc->addend = fixp->fx_addnumber;
3629   else if (symbol_section_p (fixp->fx_addsy))
3630     reloc->addend = (section->vma
3631                      + fixp->fx_addnumber
3632                      + md_pcrel_from (fixp));
3633   else
3634     reloc->addend = fixp->fx_offset;
3635 #endif
3636
3637   /* We expand R_SPARC_OLO10 to R_SPARC_LO10 and R_SPARC_13
3638      on the same location.  */
3639   if (code == BFD_RELOC_SPARC_OLO10)
3640     {
3641       relocs[1] = reloc = (arelent *) xmalloc (sizeof (arelent));
3642       relocs[2] = NULL;
3643
3644       reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
3645       *reloc->sym_ptr_ptr
3646         = symbol_get_bfdsym (section_symbol (absolute_section));
3647       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
3648       reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_SPARC13);
3649       reloc->addend = fixp->tc_fix_data;
3650     }
3651
3652   return relocs;
3653 }
3654 \f
3655 /* We have no need to default values of symbols.  */
3656
3657 symbolS *
3658 md_undefined_symbol (name)
3659      char *name ATTRIBUTE_UNUSED;
3660 {
3661   return 0;
3662 }
3663
3664 /* Round up a section size to the appropriate boundary.  */
3665
3666 valueT
3667 md_section_align (segment, size)
3668      segT segment ATTRIBUTE_UNUSED;
3669      valueT size;
3670 {
3671 #ifndef OBJ_ELF
3672   /* This is not right for ELF; a.out wants it, and COFF will force
3673      the alignment anyways.  */
3674   valueT align = ((valueT) 1
3675                   << (valueT) bfd_get_section_alignment (stdoutput, segment));
3676   valueT newsize;
3677
3678   /* Turn alignment value into a mask.  */
3679   align--;
3680   newsize = (size + align) & ~align;
3681   return newsize;
3682 #else
3683   return size;
3684 #endif
3685 }
3686
3687 /* Exactly what point is a PC-relative offset relative TO?
3688    On the sparc, they're relative to the address of the offset, plus
3689    its size.  This gets us to the following instruction.
3690    (??? Is this right?  FIXME-SOON)  */
3691 long
3692 md_pcrel_from (fixP)
3693      fixS *fixP;
3694 {
3695   long ret;
3696
3697   ret = fixP->fx_where + fixP->fx_frag->fr_address;
3698   if (! sparc_pic_code
3699       || fixP->fx_addsy == NULL
3700       || symbol_section_p (fixP->fx_addsy))
3701     ret += fixP->fx_size;
3702   return ret;
3703 }
3704 \f
3705 /* Return log2 (VALUE), or -1 if VALUE is not an exact positive power
3706    of two.  */
3707
3708 static int
3709 mylog2 (value)
3710      int value;
3711 {
3712   int shift;
3713
3714   if (value <= 0)
3715     return -1;
3716
3717   for (shift = 0; (value & 1) == 0; value >>= 1)
3718     ++shift;
3719
3720   return (value == 1) ? shift : -1;
3721 }
3722
3723 /* Sort of like s_lcomm.  */
3724
3725 #ifndef OBJ_ELF
3726 static int max_alignment = 15;
3727 #endif
3728
3729 static void
3730 s_reserve (ignore)
3731      int ignore ATTRIBUTE_UNUSED;
3732 {
3733   char *name;
3734   char *p;
3735   char c;
3736   int align;
3737   int size;
3738   int temp;
3739   symbolS *symbolP;
3740
3741   name = input_line_pointer;
3742   c = get_symbol_end ();
3743   p = input_line_pointer;
3744   *p = c;
3745   SKIP_WHITESPACE ();
3746
3747   if (*input_line_pointer != ',')
3748     {
3749       as_bad (_("Expected comma after name"));
3750       ignore_rest_of_line ();
3751       return;
3752     }
3753
3754   ++input_line_pointer;
3755
3756   if ((size = get_absolute_expression ()) < 0)
3757     {
3758       as_bad (_("BSS length (%d.) <0! Ignored."), size);
3759       ignore_rest_of_line ();
3760       return;
3761     }                           /* Bad length.  */
3762
3763   *p = 0;
3764   symbolP = symbol_find_or_make (name);
3765   *p = c;
3766
3767   if (strncmp (input_line_pointer, ",\"bss\"", 6) != 0
3768       && strncmp (input_line_pointer, ",\".bss\"", 7) != 0)
3769     {
3770       as_bad (_("bad .reserve segment -- expected BSS segment"));
3771       return;
3772     }
3773
3774   if (input_line_pointer[2] == '.')
3775     input_line_pointer += 7;
3776   else
3777     input_line_pointer += 6;
3778   SKIP_WHITESPACE ();
3779
3780   if (*input_line_pointer == ',')
3781     {
3782       ++input_line_pointer;
3783
3784       SKIP_WHITESPACE ();
3785       if (*input_line_pointer == '\n')
3786         {
3787           as_bad (_("missing alignment"));
3788           ignore_rest_of_line ();
3789           return;
3790         }
3791
3792       align = (int) get_absolute_expression ();
3793
3794 #ifndef OBJ_ELF
3795       if (align > max_alignment)
3796         {
3797           align = max_alignment;
3798           as_warn (_("alignment too large; assuming %d"), align);
3799         }
3800 #endif
3801
3802       if (align < 0)
3803         {
3804           as_bad (_("negative alignment"));
3805           ignore_rest_of_line ();
3806           return;
3807         }
3808
3809       if (align != 0)
3810         {
3811           temp = mylog2 (align);
3812           if (temp < 0)
3813             {
3814               as_bad (_("alignment not a power of 2"));
3815               ignore_rest_of_line ();
3816               return;
3817             }
3818
3819           align = temp;
3820         }
3821
3822       record_alignment (bss_section, align);
3823     }
3824   else
3825     align = 0;
3826
3827   if (!S_IS_DEFINED (symbolP)
3828 #ifdef OBJ_AOUT
3829       && S_GET_OTHER (symbolP) == 0
3830       && S_GET_DESC (symbolP) == 0
3831 #endif
3832       )
3833     {
3834       if (! need_pass_2)
3835         {
3836           char *pfrag;
3837           segT current_seg = now_seg;
3838           subsegT current_subseg = now_subseg;
3839
3840           /* Switch to bss.  */
3841           subseg_set (bss_section, 1);
3842
3843           if (align)
3844             /* Do alignment.  */
3845             frag_align (align, 0, 0);
3846
3847           /* Detach from old frag.  */
3848           if (S_GET_SEGMENT (symbolP) == bss_section)
3849             symbol_get_frag (symbolP)->fr_symbol = NULL;
3850
3851           symbol_set_frag (symbolP, frag_now);
3852           pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
3853                             (offsetT) size, (char *) 0);
3854           *pfrag = 0;
3855
3856           S_SET_SEGMENT (symbolP, bss_section);
3857
3858           subseg_set (current_seg, current_subseg);
3859
3860 #ifdef OBJ_ELF
3861           S_SET_SIZE (symbolP, size);
3862 #endif
3863         }
3864     }
3865   else
3866     {
3867       as_warn ("Ignoring attempt to re-define symbol %s",
3868                S_GET_NAME (symbolP));
3869     }                           /* if not redefining.  */
3870
3871   demand_empty_rest_of_line ();
3872 }
3873
3874 static void
3875 s_common (ignore)
3876      int ignore ATTRIBUTE_UNUSED;
3877 {
3878   char *name;
3879   char c;
3880   char *p;
3881   offsetT temp, size;
3882   symbolS *symbolP;
3883
3884   name = input_line_pointer;
3885   c = get_symbol_end ();
3886   /* Just after name is now '\0'.  */
3887   p = input_line_pointer;
3888   *p = c;
3889   SKIP_WHITESPACE ();
3890   if (*input_line_pointer != ',')
3891     {
3892       as_bad (_("Expected comma after symbol-name"));
3893       ignore_rest_of_line ();
3894       return;
3895     }
3896
3897   /* Skip ','.  */
3898   input_line_pointer++;
3899
3900   if ((temp = get_absolute_expression ()) < 0)
3901     {
3902       as_bad (_(".COMMon length (%lu) out of range ignored"),
3903               (unsigned long) temp);
3904       ignore_rest_of_line ();
3905       return;
3906     }
3907   size = temp;
3908   *p = 0;
3909   symbolP = symbol_find_or_make (name);
3910   *p = c;
3911   if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
3912     {
3913       as_bad (_("Ignoring attempt to re-define symbol"));
3914       ignore_rest_of_line ();
3915       return;
3916     }
3917   if (S_GET_VALUE (symbolP) != 0)
3918     {
3919       if (S_GET_VALUE (symbolP) != (valueT) size)
3920         {
3921           as_warn (_("Length of .comm \"%s\" is already %ld. Not changed to %ld."),
3922                    S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), (long) size);
3923         }
3924     }
3925   else
3926     {
3927 #ifndef OBJ_ELF
3928       S_SET_VALUE (symbolP, (valueT) size);
3929       S_SET_EXTERNAL (symbolP);
3930 #endif
3931     }
3932   know (symbol_get_frag (symbolP) == &zero_address_frag);
3933   if (*input_line_pointer != ',')
3934     {
3935       as_bad (_("Expected comma after common length"));
3936       ignore_rest_of_line ();
3937       return;
3938     }
3939   input_line_pointer++;
3940   SKIP_WHITESPACE ();
3941   if (*input_line_pointer != '"')
3942     {
3943       temp = get_absolute_expression ();
3944
3945 #ifndef OBJ_ELF
3946       if (temp > max_alignment)
3947         {
3948           temp = max_alignment;
3949           as_warn (_("alignment too large; assuming %ld"), (long) temp);
3950         }
3951 #endif
3952
3953       if (temp < 0)
3954         {
3955           as_bad (_("negative alignment"));
3956           ignore_rest_of_line ();
3957           return;
3958         }
3959
3960 #ifdef OBJ_ELF
3961       if (symbol_get_obj (symbolP)->local)
3962         {
3963           segT old_sec;
3964           int old_subsec;
3965           char *p;
3966           int align;
3967
3968           old_sec = now_seg;
3969           old_subsec = now_subseg;
3970
3971           if (temp == 0)
3972             align = 0;
3973           else
3974             align = mylog2 (temp);
3975
3976           if (align < 0)
3977             {
3978               as_bad (_("alignment not a power of 2"));
3979               ignore_rest_of_line ();
3980               return;
3981             }
3982
3983           record_alignment (bss_section, align);
3984           subseg_set (bss_section, 0);
3985           if (align)
3986             frag_align (align, 0, 0);
3987           if (S_GET_SEGMENT (symbolP) == bss_section)
3988             symbol_get_frag (symbolP)->fr_symbol = 0;
3989           symbol_set_frag (symbolP, frag_now);
3990           p = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
3991                         (offsetT) size, (char *) 0);
3992           *p = 0;
3993           S_SET_SEGMENT (symbolP, bss_section);
3994           S_CLEAR_EXTERNAL (symbolP);
3995           S_SET_SIZE (symbolP, size);
3996           subseg_set (old_sec, old_subsec);
3997         }
3998       else
3999 #endif /* OBJ_ELF  */
4000         {
4001         allocate_common:
4002           S_SET_VALUE (symbolP, (valueT) size);
4003 #ifdef OBJ_ELF
4004           S_SET_ALIGN (symbolP, temp);
4005           S_SET_SIZE (symbolP, size);
4006 #endif
4007           S_SET_EXTERNAL (symbolP);
4008           S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
4009         }
4010     }
4011   else
4012     {
4013       input_line_pointer++;
4014       /* @@ Some use the dot, some don't.  Can we get some consistency??  */
4015       if (*input_line_pointer == '.')
4016         input_line_pointer++;
4017       /* @@ Some say data, some say bss.  */
4018       if (strncmp (input_line_pointer, "bss\"", 4)
4019           && strncmp (input_line_pointer, "data\"", 5))
4020         {
4021           while (*--input_line_pointer != '"')
4022             ;
4023           input_line_pointer--;
4024           goto bad_common_segment;
4025         }
4026       while (*input_line_pointer++ != '"')
4027         ;
4028       goto allocate_common;
4029     }
4030
4031   symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
4032
4033   demand_empty_rest_of_line ();
4034   return;
4035
4036   {
4037   bad_common_segment:
4038     p = input_line_pointer;
4039     while (*p && *p != '\n')
4040       p++;
4041     c = *p;
4042     *p = '\0';
4043     as_bad (_("bad .common segment %s"), input_line_pointer + 1);
4044     *p = c;
4045     input_line_pointer = p;
4046     ignore_rest_of_line ();
4047     return;
4048   }
4049 }
4050
4051 /* Handle the .empty pseudo-op.  This suppresses the warnings about
4052    invalid delay slot usage.  */
4053
4054 static void
4055 s_empty (ignore)
4056      int ignore ATTRIBUTE_UNUSED;
4057 {
4058   /* The easy way to implement is to just forget about the last
4059      instruction.  */
4060   last_insn = NULL;
4061 }
4062
4063 static void
4064 s_seg (ignore)
4065      int ignore ATTRIBUTE_UNUSED;
4066 {
4067
4068   if (strncmp (input_line_pointer, "\"text\"", 6) == 0)
4069     {
4070       input_line_pointer += 6;
4071       s_text (0);
4072       return;
4073     }
4074   if (strncmp (input_line_pointer, "\"data\"", 6) == 0)
4075     {
4076       input_line_pointer += 6;
4077       s_data (0);
4078       return;
4079     }
4080   if (strncmp (input_line_pointer, "\"data1\"", 7) == 0)
4081     {
4082       input_line_pointer += 7;
4083       s_data1 ();
4084       return;
4085     }
4086   if (strncmp (input_line_pointer, "\"bss\"", 5) == 0)
4087     {
4088       input_line_pointer += 5;
4089       /* We only support 2 segments -- text and data -- for now, so
4090          things in the "bss segment" will have to go into data for now.
4091          You can still allocate SEG_BSS stuff with .lcomm or .reserve.  */
4092       subseg_set (data_section, 255);   /* FIXME-SOMEDAY.  */
4093       return;
4094     }
4095   as_bad (_("Unknown segment type"));
4096   demand_empty_rest_of_line ();
4097 }
4098
4099 static void
4100 s_data1 ()
4101 {
4102   subseg_set (data_section, 1);
4103   demand_empty_rest_of_line ();
4104 }
4105
4106 static void
4107 s_proc (ignore)
4108      int ignore ATTRIBUTE_UNUSED;
4109 {
4110   while (!is_end_of_line[(unsigned char) *input_line_pointer])
4111     {
4112       ++input_line_pointer;
4113     }
4114   ++input_line_pointer;
4115 }
4116
4117 /* This static variable is set by s_uacons to tell sparc_cons_align
4118    that the expression does not need to be aligned.  */
4119
4120 static int sparc_no_align_cons = 0;
4121
4122 /* This static variable is set by sparc_cons to emit requested types
4123    of relocations in cons_fix_new_sparc.  */
4124
4125 static const char *sparc_cons_special_reloc;
4126
4127 /* This handles the unaligned space allocation pseudo-ops, such as
4128    .uaword.  .uaword is just like .word, but the value does not need
4129    to be aligned.  */
4130
4131 static void
4132 s_uacons (bytes)
4133      int bytes;
4134 {
4135   /* Tell sparc_cons_align not to align this value.  */
4136   sparc_no_align_cons = 1;
4137   cons (bytes);
4138   sparc_no_align_cons = 0;
4139 }
4140
4141 /* This handles the native word allocation pseudo-op .nword.
4142    For sparc_arch_size 32 it is equivalent to .word,  for
4143    sparc_arch_size 64 it is equivalent to .xword.  */
4144
4145 static void
4146 s_ncons (bytes)
4147      int bytes ATTRIBUTE_UNUSED;
4148 {
4149   cons (sparc_arch_size == 32 ? 4 : 8);
4150 }
4151
4152 #ifdef OBJ_ELF
4153 /* Handle the SPARC ELF .register pseudo-op.  This sets the binding of a
4154    global register.
4155    The syntax is:
4156
4157    .register %g[2367],{#scratch|symbolname|#ignore}
4158 */
4159
4160 static void
4161 s_register (ignore)
4162      int ignore ATTRIBUTE_UNUSED;
4163 {
4164   char c;
4165   int reg;
4166   int flags;
4167   const char *regname;
4168
4169   if (input_line_pointer[0] != '%'
4170       || input_line_pointer[1] != 'g'
4171       || ((input_line_pointer[2] & ~1) != '2'
4172           && (input_line_pointer[2] & ~1) != '6')
4173       || input_line_pointer[3] != ',')
4174     as_bad (_("register syntax is .register %%g[2367],{#scratch|symbolname|#ignore}"));
4175   reg = input_line_pointer[2] - '0';
4176   input_line_pointer += 4;
4177
4178   if (*input_line_pointer == '#')
4179     {
4180       ++input_line_pointer;
4181       regname = input_line_pointer;
4182       c = get_symbol_end ();
4183       if (strcmp (regname, "scratch") && strcmp (regname, "ignore"))
4184         as_bad (_("register syntax is .register %%g[2367],{#scratch|symbolname|#ignore}"));
4185       if (regname[0] == 'i')
4186         regname = NULL;
4187       else
4188         regname = "";
4189     }
4190   else
4191     {
4192       regname = input_line_pointer;
4193       c = get_symbol_end ();
4194     }
4195   if (sparc_arch_size == 64)
4196     {
4197       if (globals[reg])
4198         {
4199           if ((regname && globals[reg] != (symbolS *) 1
4200                && strcmp (S_GET_NAME (globals[reg]), regname))
4201               || ((regname != NULL) ^ (globals[reg] != (symbolS *) 1)))
4202             as_bad (_("redefinition of global register"));
4203         }
4204       else
4205         {
4206           if (regname == NULL)
4207             globals[reg] = (symbolS *) 1;
4208           else
4209             {
4210               if (*regname)
4211                 {
4212                   if (symbol_find (regname))
4213                     as_bad (_("Register symbol %s already defined."),
4214                             regname);
4215                 }
4216               globals[reg] = symbol_make (regname);
4217               flags = symbol_get_bfdsym (globals[reg])->flags;
4218               if (! *regname)
4219                 flags = flags & ~(BSF_GLOBAL|BSF_LOCAL|BSF_WEAK);
4220               if (! (flags & (BSF_GLOBAL|BSF_LOCAL|BSF_WEAK)))
4221                 flags |= BSF_GLOBAL;
4222               symbol_get_bfdsym (globals[reg])->flags = flags;
4223               S_SET_VALUE (globals[reg], (valueT) reg);
4224               S_SET_ALIGN (globals[reg], reg);
4225               S_SET_SIZE (globals[reg], 0);
4226               /* Although we actually want undefined_section here,
4227                  we have to use absolute_section, because otherwise
4228                  generic as code will make it a COM section.
4229                  We fix this up in sparc_adjust_symtab.  */
4230               S_SET_SEGMENT (globals[reg], absolute_section);
4231               S_SET_OTHER (globals[reg], 0);
4232               elf_symbol (symbol_get_bfdsym (globals[reg]))
4233                 ->internal_elf_sym.st_info =
4234                   ELF_ST_INFO(STB_GLOBAL, STT_REGISTER);
4235               elf_symbol (symbol_get_bfdsym (globals[reg]))
4236                 ->internal_elf_sym.st_shndx = SHN_UNDEF;
4237             }
4238         }
4239     }
4240
4241   *input_line_pointer = c;
4242
4243   demand_empty_rest_of_line ();
4244 }
4245
4246 /* Adjust the symbol table.  We set undefined sections for STT_REGISTER
4247    symbols which need it.  */
4248
4249 void
4250 sparc_adjust_symtab ()
4251 {
4252   symbolS *sym;
4253
4254   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
4255     {
4256       if (ELF_ST_TYPE (elf_symbol (symbol_get_bfdsym (sym))
4257                        ->internal_elf_sym.st_info) != STT_REGISTER)
4258         continue;
4259
4260       if (ELF_ST_TYPE (elf_symbol (symbol_get_bfdsym (sym))
4261                        ->internal_elf_sym.st_shndx != SHN_UNDEF))
4262         continue;
4263
4264       S_SET_SEGMENT (sym, undefined_section);
4265     }
4266 }
4267 #endif
4268
4269 /* If the --enforce-aligned-data option is used, we require .word,
4270    et. al., to be aligned correctly.  We do it by setting up an
4271    rs_align_code frag, and checking in HANDLE_ALIGN to make sure that
4272    no unexpected alignment was introduced.
4273
4274    The SunOS and Solaris native assemblers enforce aligned data by
4275    default.  We don't want to do that, because gcc can deliberately
4276    generate misaligned data if the packed attribute is used.  Instead,
4277    we permit misaligned data by default, and permit the user to set an
4278    option to check for it.  */
4279
4280 void
4281 sparc_cons_align (nbytes)
4282      int nbytes;
4283 {
4284   int nalign;
4285   char *p;
4286
4287   /* Only do this if we are enforcing aligned data.  */
4288   if (! enforce_aligned_data)
4289     return;
4290
4291   /* Don't align if this is an unaligned pseudo-op.  */
4292   if (sparc_no_align_cons)
4293     return;
4294
4295   nalign = mylog2 (nbytes);
4296   if (nalign == 0)
4297     return;
4298
4299   assert (nalign > 0);
4300
4301   if (now_seg == absolute_section)
4302     {
4303       if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
4304         as_bad (_("misaligned data"));
4305       return;
4306     }
4307
4308   p = frag_var (rs_align_test, 1, 1, (relax_substateT) 0,
4309                 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
4310
4311   record_alignment (now_seg, nalign);
4312 }
4313
4314 /* This is called from HANDLE_ALIGN in tc-sparc.h.  */
4315
4316 void
4317 sparc_handle_align (fragp)
4318      fragS *fragp;
4319 {
4320   int count, fix;
4321   char *p;
4322
4323   count = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
4324
4325   switch (fragp->fr_type)
4326     {
4327     case rs_align_test:
4328       if (count != 0)
4329         as_bad_where (fragp->fr_file, fragp->fr_line, _("misaligned data"));
4330       break;
4331
4332     case rs_align_code:
4333       p = fragp->fr_literal + fragp->fr_fix;
4334       fix = 0;
4335
4336       if (count & 3)
4337         {
4338           fix = count & 3;
4339           memset (p, 0, fix);
4340           p += fix;
4341           count -= fix;
4342         }
4343
4344       if (SPARC_OPCODE_ARCH_V9_P (max_architecture) && count > 8)
4345         {
4346           unsigned wval = (0x30680000 | count >> 2); /* ba,a,pt %xcc, 1f  */
4347           if (INSN_BIG_ENDIAN)
4348             number_to_chars_bigendian (p, wval, 4);
4349           else
4350             number_to_chars_littleendian (p, wval, 4);
4351           p += 4;
4352           count -= 4;
4353           fix += 4;
4354         }
4355
4356       if (INSN_BIG_ENDIAN)
4357         number_to_chars_bigendian (p, 0x01000000, 4);
4358       else
4359         number_to_chars_littleendian (p, 0x01000000, 4);
4360
4361       fragp->fr_fix += fix;
4362       fragp->fr_var = 4;
4363       break;
4364
4365     default:
4366       break;
4367     }
4368 }
4369
4370 #ifdef OBJ_ELF
4371 /* Some special processing for a Sparc ELF file.  */
4372
4373 void
4374 sparc_elf_final_processing ()
4375 {
4376   /* Set the Sparc ELF flag bits.  FIXME: There should probably be some
4377      sort of BFD interface for this.  */
4378   if (sparc_arch_size == 64)
4379     {
4380       switch (sparc_memory_model)
4381         {
4382         case MM_RMO:
4383           elf_elfheader (stdoutput)->e_flags |= EF_SPARCV9_RMO;
4384           break;
4385         case MM_PSO:
4386           elf_elfheader (stdoutput)->e_flags |= EF_SPARCV9_PSO;
4387           break;
4388         default:
4389           break;
4390         }
4391     }
4392   else if (current_architecture >= SPARC_OPCODE_ARCH_V9)
4393     elf_elfheader (stdoutput)->e_flags |= EF_SPARC_32PLUS;
4394   if (current_architecture == SPARC_OPCODE_ARCH_V9A)
4395     elf_elfheader (stdoutput)->e_flags |= EF_SPARC_SUN_US1;
4396   else if (current_architecture == SPARC_OPCODE_ARCH_V9B)
4397     elf_elfheader (stdoutput)->e_flags |= EF_SPARC_SUN_US1|EF_SPARC_SUN_US3;
4398 }
4399
4400 void
4401 sparc_cons (exp, size)
4402      expressionS *exp;
4403      int size;
4404 {
4405   char *save;
4406
4407   SKIP_WHITESPACE ();
4408   sparc_cons_special_reloc = NULL;
4409   save = input_line_pointer;
4410   if (input_line_pointer[0] == '%'
4411       && input_line_pointer[1] == 'r'
4412       && input_line_pointer[2] == '_')
4413     {
4414       if (strncmp (input_line_pointer + 3, "disp", 4) == 0)
4415         {
4416           input_line_pointer += 7;
4417           sparc_cons_special_reloc = "disp";
4418         }
4419       else if (strncmp (input_line_pointer + 3, "plt", 3) == 0)
4420         {
4421           if (size != 4 && size != 8)
4422             as_bad (_("Illegal operands: %%r_plt in %d-byte data field"), size);
4423           else
4424             {
4425               input_line_pointer += 6;
4426               sparc_cons_special_reloc = "plt";
4427             }
4428         }
4429       else if (strncmp (input_line_pointer + 3, "tls_dtpoff", 10) == 0)
4430         {
4431           if (size != 4 && size != 8)
4432             as_bad (_("Illegal operands: %%r_tls_dtpoff in %d-byte data field"), size);
4433           else
4434             {
4435               input_line_pointer += 13;
4436               sparc_cons_special_reloc = "tls_dtpoff";
4437             }
4438         }
4439       if (sparc_cons_special_reloc)
4440         {
4441           int bad = 0;
4442
4443           switch (size)
4444             {
4445             case 1:
4446               if (*input_line_pointer != '8')
4447                 bad = 1;
4448               input_line_pointer--;
4449               break;
4450             case 2:
4451               if (input_line_pointer[0] != '1' || input_line_pointer[1] != '6')
4452                 bad = 1;
4453               break;
4454             case 4:
4455               if (input_line_pointer[0] != '3' || input_line_pointer[1] != '2')
4456                 bad = 1;
4457               break;
4458             case 8:
4459               if (input_line_pointer[0] != '6' || input_line_pointer[1] != '4')
4460                 bad = 1;
4461               break;
4462             default:
4463               bad = 1;
4464               break;
4465             }
4466
4467           if (bad)
4468             {
4469               as_bad (_("Illegal operands: Only %%r_%s%d allowed in %d-byte data fields"),
4470                       sparc_cons_special_reloc, size * 8, size);
4471             }
4472           else
4473             {
4474               input_line_pointer += 2;
4475               if (*input_line_pointer != '(')
4476                 {
4477                   as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
4478                           sparc_cons_special_reloc, size * 8);
4479                   bad = 1;
4480                 }
4481             }
4482
4483           if (bad)
4484             {
4485               input_line_pointer = save;
4486               sparc_cons_special_reloc = NULL;
4487             }
4488           else
4489             {
4490               int c;
4491               char *end = ++input_line_pointer;
4492               int npar = 0;
4493
4494               while (! is_end_of_line[(c = *end)])
4495                 {
4496                   if (c == '(')
4497                     npar++;
4498                   else if (c == ')')
4499                     {
4500                       if (!npar)
4501                         break;
4502                       npar--;
4503                     }
4504                   end++;
4505                 }
4506
4507               if (c != ')')
4508                 as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
4509                         sparc_cons_special_reloc, size * 8);
4510               else
4511                 {
4512                   *end = '\0';
4513                   expression (exp);
4514                   *end = c;
4515                   if (input_line_pointer != end)
4516                     {
4517                       as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
4518                               sparc_cons_special_reloc, size * 8);
4519                     }
4520                   else
4521                     {
4522                       input_line_pointer++;
4523                       SKIP_WHITESPACE ();
4524                       c = *input_line_pointer;
4525                       if (! is_end_of_line[c] && c != ',')
4526                         as_bad (_("Illegal operands: garbage after %%r_%s%d()"),
4527                                 sparc_cons_special_reloc, size * 8);
4528                     }
4529                 }
4530             }
4531         }
4532     }
4533   if (sparc_cons_special_reloc == NULL)
4534     expression (exp);
4535 }
4536
4537 #endif
4538
4539 /* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
4540    reloc for a cons.  We could use the definition there, except that
4541    we want to handle little endian relocs specially.  */
4542
4543 void
4544 cons_fix_new_sparc (frag, where, nbytes, exp)
4545      fragS *frag;
4546      int where;
4547      unsigned int nbytes;
4548      expressionS *exp;
4549 {
4550   bfd_reloc_code_real_type r;
4551
4552   r = (nbytes == 1 ? BFD_RELOC_8 :
4553        (nbytes == 2 ? BFD_RELOC_16 :
4554         (nbytes == 4 ? BFD_RELOC_32 : BFD_RELOC_64)));
4555
4556   if (target_little_endian_data
4557       && nbytes == 4
4558       && now_seg->flags & SEC_ALLOC)
4559     r = BFD_RELOC_SPARC_REV32;
4560
4561   if (sparc_cons_special_reloc)
4562     {
4563       if (*sparc_cons_special_reloc == 'd')
4564         switch (nbytes)
4565           {
4566           case 1: r = BFD_RELOC_8_PCREL; break;
4567           case 2: r = BFD_RELOC_16_PCREL; break;
4568           case 4: r = BFD_RELOC_32_PCREL; break;
4569           case 8: r = BFD_RELOC_64_PCREL; break;
4570           default: abort ();
4571           }
4572       else if (*sparc_cons_special_reloc == 'p')
4573         switch (nbytes)
4574           {
4575           case 4: r = BFD_RELOC_SPARC_PLT32; break;
4576           case 8: r = BFD_RELOC_SPARC_PLT64; break;
4577           }
4578       else
4579         switch (nbytes)
4580           {
4581           case 4: r = BFD_RELOC_SPARC_TLS_DTPOFF32; break;
4582           case 8: r = BFD_RELOC_SPARC_TLS_DTPOFF64; break;
4583           }
4584     }
4585   else if (sparc_no_align_cons)
4586     {
4587       switch (nbytes)
4588         {
4589         case 2: r = BFD_RELOC_SPARC_UA16; break;
4590         case 4: r = BFD_RELOC_SPARC_UA32; break;
4591         case 8: r = BFD_RELOC_SPARC_UA64; break;
4592         default: abort ();
4593         }
4594    }
4595
4596   fix_new_exp (frag, where, (int) nbytes, exp, 0, r);
4597   sparc_cons_special_reloc = NULL;
4598 }
4599
4600 void
4601 sparc_cfi_frame_initial_instructions ()
4602 {
4603   cfi_add_CFA_def_cfa (14, sparc_arch_size == 64 ? 0x7ff : 0);
4604 }
4605
4606 int
4607 sparc_regname_to_dw2regnum (const char *regname)
4608 {
4609   char *p, *q;
4610
4611   if (!regname[0])
4612     return -1;
4613
4614   q = "goli";
4615   p = strchr (q, regname[0]);
4616   if (p)
4617     {
4618       if (regname[1] < '0' || regname[1] > '8' || regname[2])
4619         return -1;
4620       return (p - q) * 8 + regname[1] - '0';
4621     }
4622   if (regname[0] == 's' && regname[1] == 'p' && !regname[2])
4623     return 14;
4624   if (regname[0] == 'f' && regname[1] == 'p' && !regname[2])
4625     return 30;
4626   if (regname[0] == 'f' || regname[0] == 'r')
4627     {
4628       unsigned int regnum;
4629
4630       regnum = strtoul (regname + 1, &q, 10);
4631       if (p == q || *q)
4632         return -1;
4633       if (regnum >= ((regname[0] == 'f'
4634                       && SPARC_OPCODE_ARCH_V9_P (max_architecture))
4635                      ? 64 : 32))
4636         return -1;
4637       if (regname[0] == 'f')
4638         {
4639           regnum += 32;
4640           if (regnum >= 64 && (regnum & 1))
4641             return -1;
4642         }
4643       return regnum;
4644     }
4645   return -1;
4646 }
4647
4648 void
4649 sparc_cfi_emit_pcrel_expr (expressionS *exp, unsigned int nbytes)
4650 {
4651   sparc_cons_special_reloc = "disp";
4652   sparc_no_align_cons = 1;
4653   emit_expr (exp, nbytes);
4654   sparc_no_align_cons = 0;
4655   sparc_cons_special_reloc = NULL;
4656 }