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