* config/tc-sparc.c (md_shortopts): Add "K:" if OBJ_ELF.
[external/binutils.git] / gas / config / tc-sparc.c
1 /* tc-sparc.c -- Assemble for the SPARC
2    Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3
4    This file is part of GAS, the GNU Assembler.
5
6    GAS is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GAS is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GAS; see the file COPYING.  If not, write to
18    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stdio.h>
21 #include <ctype.h>
22
23 #include "as.h"
24 #include "subsegs.h"
25
26 /* careful, this file includes data *declarations* */
27 #include "opcode/sparc.h"
28
29 static void sparc_ip PARAMS ((char *));
30
31 #ifdef sparcv9
32 static enum sparc_architecture current_architecture = v9;
33 #else
34 static enum sparc_architecture current_architecture = v6;
35 #endif
36 static int architecture_requested;
37 static int warn_on_bump;
38
39 extern int target_big_endian;
40
41 /* handle of the OPCODE hash table */
42 static struct hash_control *op_hash;
43
44 static void s_data1 PARAMS ((void));
45 static void s_seg PARAMS ((int));
46 static void s_proc PARAMS ((int));
47 static void s_reserve PARAMS ((int));
48 static void s_common PARAMS ((int));
49
50 const pseudo_typeS md_pseudo_table[] =
51 {
52   {"align", s_align_bytes, 0},  /* Defaulting is invalid (0) */
53   {"common", s_common, 0},
54   {"global", s_globl, 0},
55   {"half", cons, 2},
56   {"optim", s_ignore, 0},
57   {"proc", s_proc, 0},
58   {"reserve", s_reserve, 0},
59   {"seg", s_seg, 0},
60   {"skip", s_space, 0},
61   {"word", cons, 4},
62 #ifndef NO_V9
63   {"xword", cons, 8},
64 #ifdef OBJ_ELF
65   {"uaxword", cons, 8},
66 #endif
67 #endif
68 #ifdef OBJ_ELF
69   /* these are specific to sparc/svr4 */
70   {"pushsection", obj_elf_section, 0},
71   {"popsection", obj_elf_previous, 0},
72   {"uaword", cons, 4},
73   {"uahalf", cons, 2},
74 #endif
75   {NULL, 0, 0},
76 };
77
78 const int md_reloc_size = 12;   /* Size of relocation record */
79
80 /* This array holds the chars that always start a comment.  If the
81    pre-processor is disabled, these aren't very useful */
82 const char comment_chars[] = "!";       /* JF removed '|' from comment_chars */
83
84 /* This array holds the chars that only start a comment at the beginning of
85    a line.  If the line seems to have the form '# 123 filename'
86    .line and .file directives will appear in the pre-processed output */
87 /* Note that input_file.c hand checks for '#' at the beginning of the
88    first line of the input file.  This is because the compiler outputs
89    #NO_APP at the beginning of its output. */
90 /* Also note that comments started like this one will always
91    work if '/' isn't otherwise defined. */
92 const char line_comment_chars[] = "#";
93
94 const char line_separator_chars[] = "";
95
96 /* Chars that can be used to separate mant from exp in floating point nums */
97 const char EXP_CHARS[] = "eE";
98
99 /* Chars that mean this number is a floating point constant */
100 /* As in 0f12.456 */
101 /* or    0d1.2345e12 */
102 const char FLT_CHARS[] = "rRsSfFdDxXpP";
103
104 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
105    changed in read.c.  Ideally it shouldn't have to know about it at all,
106    but nothing is ideal around here.  */
107
108 static unsigned char octal[256];
109 #define isoctal(c)  octal[(unsigned char) (c)]
110 static unsigned char toHex[256];
111
112 struct sparc_it
113   {
114     char *error;
115     unsigned long opcode;
116     struct nlist *nlistp;
117     expressionS exp;
118     int pcrel;
119     bfd_reloc_code_real_type reloc;
120   };
121
122 struct sparc_it the_insn, set_insn;
123
124 static INLINE int
125 in_signed_range (val, max)
126      bfd_signed_vma val, max;
127 {
128   if (max <= 0)
129     abort ();
130   if (val > max)
131     return 0;
132   if (val < ~max)
133     return 0;
134   return 1;
135 }
136
137 #if 0
138 static void print_insn PARAMS ((struct sparc_it *insn));
139 #endif
140 static int getExpression PARAMS ((char *str));
141
142 static char *expr_end;
143 static int special_case;
144
145 /*
146  * Instructions that require wierd handling because they're longer than
147  * 4 bytes.
148  */
149 #define SPECIAL_CASE_SET        1
150 #define SPECIAL_CASE_FDIV       2
151
152 /*
153  * sort of like s_lcomm
154  *
155  */
156 #ifndef OBJ_ELF
157 static int max_alignment = 15;
158 #endif
159
160 static void
161 s_reserve (ignore)
162      int ignore;
163 {
164   char *name;
165   char *p;
166   char c;
167   int align;
168   int size;
169   int temp;
170   symbolS *symbolP;
171
172   name = input_line_pointer;
173   c = get_symbol_end ();
174   p = input_line_pointer;
175   *p = c;
176   SKIP_WHITESPACE ();
177
178   if (*input_line_pointer != ',')
179     {
180       as_bad ("Expected comma after name");
181       ignore_rest_of_line ();
182       return;
183     }
184
185   ++input_line_pointer;
186
187   if ((size = get_absolute_expression ()) < 0)
188     {
189       as_bad ("BSS length (%d.) <0! Ignored.", size);
190       ignore_rest_of_line ();
191       return;
192     }                           /* bad length */
193
194   *p = 0;
195   symbolP = symbol_find_or_make (name);
196   *p = c;
197
198   if (strncmp (input_line_pointer, ",\"bss\"", 6) != 0
199       && strncmp (input_line_pointer, ",\".bss\"", 7) != 0)
200     {
201       as_bad ("bad .reserve segment -- expected BSS segment");
202       return;
203     }
204
205   if (input_line_pointer[2] == '.')
206     input_line_pointer += 7;
207   else
208     input_line_pointer += 6;
209   SKIP_WHITESPACE ();
210
211   if (*input_line_pointer == ',')
212     {
213       ++input_line_pointer;
214
215       SKIP_WHITESPACE ();
216       if (*input_line_pointer == '\n')
217         {
218           as_bad ("Missing alignment");
219           return;
220         }
221
222       align = get_absolute_expression ();
223 #ifndef OBJ_ELF
224       if (align > max_alignment)
225         {
226           align = max_alignment;
227           as_warn ("Alignment too large: %d. assumed.", align);
228         }
229 #endif
230       if (align < 0)
231         {
232           align = 0;
233           as_warn ("Alignment negative. 0 assumed.");
234         }
235
236       record_alignment (bss_section, align);
237
238       /* convert to a power of 2 alignment */
239       for (temp = 0; (align & 1) == 0; align >>= 1, ++temp);;
240
241       if (align != 1)
242         {
243           as_bad ("Alignment not a power of 2");
244           ignore_rest_of_line ();
245           return;
246         }                       /* not a power of two */
247
248       align = temp;
249     }                           /* if has optional alignment */
250   else
251     align = 0;
252
253   if ((S_GET_SEGMENT (symbolP) == bss_section
254        || !S_IS_DEFINED (symbolP))
255 #ifdef OBJ_AOUT
256       && S_GET_OTHER (symbolP) == 0
257       && S_GET_DESC (symbolP) == 0
258 #endif
259       )
260     {
261       if (! need_pass_2)
262         {
263           char *pfrag;
264           segT current_seg = now_seg;
265           subsegT current_subseg = now_subseg;
266
267           subseg_set (bss_section, 1); /* switch to bss */
268
269           if (align)
270             frag_align (align, 0); /* do alignment */
271
272           /* detach from old frag */
273           if (S_GET_SEGMENT(symbolP) == bss_section)
274             symbolP->sy_frag->fr_symbol = NULL;
275
276           symbolP->sy_frag = frag_now;
277           pfrag = frag_var (rs_org, 1, 1, (relax_substateT)0, symbolP,
278                             size, (char *)0);
279           *pfrag = 0;
280
281           S_SET_SEGMENT (symbolP, bss_section);
282
283           subseg_set (current_seg, current_subseg);
284         }
285     }
286   else
287     {
288       as_warn("Ignoring attempt to re-define symbol %s.", name);
289     }                           /* if not redefining */
290
291   demand_empty_rest_of_line ();
292 }
293
294 static void
295 s_common (ignore)
296      int ignore;
297 {
298   char *name;
299   char c;
300   char *p;
301   int temp, size;
302   symbolS *symbolP;
303
304   name = input_line_pointer;
305   c = get_symbol_end ();
306   /* just after name is now '\0' */
307   p = input_line_pointer;
308   *p = c;
309   SKIP_WHITESPACE ();
310   if (*input_line_pointer != ',')
311     {
312       as_bad ("Expected comma after symbol-name");
313       ignore_rest_of_line ();
314       return;
315     }
316   input_line_pointer++;         /* skip ',' */
317   if ((temp = get_absolute_expression ()) < 0)
318     {
319       as_bad (".COMMon length (%d.) <0! Ignored.", temp);
320       ignore_rest_of_line ();
321       return;
322     }
323   size = temp;
324   *p = 0;
325   symbolP = symbol_find_or_make (name);
326   *p = c;
327   if (S_IS_DEFINED (symbolP))
328     {
329       as_bad ("Ignoring attempt to re-define symbol");
330       ignore_rest_of_line ();
331       return;
332     }
333   if (S_GET_VALUE (symbolP) != 0)
334     {
335       if (S_GET_VALUE (symbolP) != size)
336         {
337           as_warn ("Length of .comm \"%s\" is already %ld. Not changed to %d.",
338                    S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
339         }
340     }
341   else
342     {
343 #ifndef OBJ_ELF
344       S_SET_VALUE (symbolP, (valueT) size);
345       S_SET_EXTERNAL (symbolP);
346 #endif
347     }
348   know (symbolP->sy_frag == &zero_address_frag);
349   if (*input_line_pointer != ',')
350     {
351       as_bad ("Expected comma after common length");
352       ignore_rest_of_line ();
353       return;
354     }
355   input_line_pointer++;
356   SKIP_WHITESPACE ();
357   if (*input_line_pointer != '"')
358     {
359       temp = get_absolute_expression ();
360 #ifndef OBJ_ELF
361       if (temp > max_alignment)
362         {
363           temp = max_alignment;
364           as_warn ("Common alignment too large: %d. assumed", temp);
365         }
366 #endif
367       if (temp < 0)
368         {
369           temp = 0;
370           as_warn ("Common alignment negative; 0 assumed");
371         }
372 #ifdef OBJ_ELF
373       if (symbolP->local)
374         {
375           segT old_sec;
376           int old_subsec;
377           char *p;
378           int align;
379
380         allocate_bss:
381           old_sec = now_seg;
382           old_subsec = now_subseg;
383           align = temp;
384           record_alignment (bss_section, align);
385           subseg_set (bss_section, 0);
386           if (align)
387             frag_align (align, 0);
388           if (S_GET_SEGMENT (symbolP) == bss_section)
389             symbolP->sy_frag->fr_symbol = 0;
390           symbolP->sy_frag = frag_now;
391           p = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
392                         (char *) 0);
393           *p = 0;
394           S_SET_SEGMENT (symbolP, bss_section);
395           S_CLEAR_EXTERNAL (symbolP);
396           subseg_set (old_sec, old_subsec);
397         }
398       else
399 #endif
400         {
401         allocate_common:
402           S_SET_VALUE (symbolP, (valueT) size);
403 #ifdef OBJ_ELF
404           S_SET_ALIGN (symbolP, temp);
405 #endif
406           S_SET_EXTERNAL (symbolP);
407           /* should be common, but this is how gas does it for now */
408           S_SET_SEGMENT (symbolP, bfd_und_section_ptr);
409         }
410     }
411   else
412     {
413       input_line_pointer++;
414       /* @@ Some use the dot, some don't.  Can we get some consistency??  */
415       if (*input_line_pointer == '.')
416         input_line_pointer++;
417       /* @@ Some say data, some say bss.  */
418       if (strncmp (input_line_pointer, "bss\"", 4)
419           && strncmp (input_line_pointer, "data\"", 5))
420         {
421           while (*--input_line_pointer != '"')
422             ;
423           input_line_pointer--;
424           goto bad_common_segment;
425         }
426       while (*input_line_pointer++ != '"')
427         ;
428       goto allocate_common;
429     }
430   demand_empty_rest_of_line ();
431   return;
432
433   {
434   bad_common_segment:
435     p = input_line_pointer;
436     while (*p && *p != '\n')
437       p++;
438     c = *p;
439     *p = '\0';
440     as_bad ("bad .common segment %s", input_line_pointer + 1);
441     *p = c;
442     input_line_pointer = p;
443     ignore_rest_of_line ();
444     return;
445   }
446 }
447
448 static void
449 s_seg (ignore)
450      int ignore;
451 {
452
453   if (strncmp (input_line_pointer, "\"text\"", 6) == 0)
454     {
455       input_line_pointer += 6;
456       s_text (0);
457       return;
458     }
459   if (strncmp (input_line_pointer, "\"data\"", 6) == 0)
460     {
461       input_line_pointer += 6;
462       s_data (0);
463       return;
464     }
465   if (strncmp (input_line_pointer, "\"data1\"", 7) == 0)
466     {
467       input_line_pointer += 7;
468       s_data1 ();
469       return;
470     }
471   if (strncmp (input_line_pointer, "\"bss\"", 5) == 0)
472     {
473       input_line_pointer += 5;
474       /* We only support 2 segments -- text and data -- for now, so
475          things in the "bss segment" will have to go into data for now.
476          You can still allocate SEG_BSS stuff with .lcomm or .reserve. */
477       subseg_set (data_section, 255);   /* FIXME-SOMEDAY */
478       return;
479     }
480   as_bad ("Unknown segment type");
481   demand_empty_rest_of_line ();
482 }
483
484 static void
485 s_data1 ()
486 {
487   subseg_set (data_section, 1);
488   demand_empty_rest_of_line ();
489 }
490
491 static void
492 s_proc (ignore)
493      int ignore;
494 {
495   while (!is_end_of_line[(unsigned char) *input_line_pointer])
496     {
497       ++input_line_pointer;
498     }
499   ++input_line_pointer;
500 }
501
502 #ifndef NO_V9
503
504 struct priv_reg_entry
505   {
506     char *name;
507     int regnum;
508   };
509
510 struct priv_reg_entry priv_reg_table[] =
511 {
512   {"tpc", 0},
513   {"tnpc", 1},
514   {"tstate", 2},
515   {"tt", 3},
516   {"tick", 4},
517   {"tba", 5},
518   {"pstate", 6},
519   {"tl", 7},
520   {"pil", 8},
521   {"cwp", 9},
522   {"cansave", 10},
523   {"canrestore", 11},
524   {"cleanwin", 12},
525   {"otherwin", 13},
526   {"wstate", 14},
527   {"fq", 15},
528   {"ver", 31},
529   {"", -1},                     /* end marker */
530 };
531
532 struct membar_masks
533 {
534   char *name;
535   unsigned int len;
536   unsigned int mask;
537 };
538
539 #define MEMBAR_MASKS_SIZE 7
540
541 static const struct membar_masks membar_masks[MEMBAR_MASKS_SIZE] =
542 {
543   {"Sync", 4, 0x40},
544   {"MemIssue", 8, 0x20},
545   {"Lookaside", 9, 0x10},
546   {"StoreStore", 10, 0x08},
547   {"LoadStore", 9, 0x04},
548   {"StoreLoad", 9, 0x02},
549   {"LoadLoad", 8, 0x01},
550 };
551
552 static int
553 cmp_reg_entry (p, q)
554      struct priv_reg_entry *p, *q;
555 {
556   return strcmp (q->name, p->name);
557 }
558
559 #endif
560
561 /* This function is called once, at assembler startup time.  It should
562    set up all the tables, etc. that the MD part of the assembler will need. */
563 void
564 md_begin ()
565 {
566   register const char *retval = NULL;
567   int lose = 0;
568   register unsigned int i = 0;
569
570   op_hash = hash_new ();
571
572   while (i < NUMOPCODES)
573     {
574       const char *name = sparc_opcodes[i].name;
575       retval = hash_insert (op_hash, name, &sparc_opcodes[i]);
576       if (retval != NULL)
577         {
578           fprintf (stderr, "internal error: can't hash `%s': %s\n",
579                    sparc_opcodes[i].name, retval);
580           lose = 1;
581         }
582       do
583         {
584           if (sparc_opcodes[i].match & sparc_opcodes[i].lose)
585             {
586               fprintf (stderr, "internal error: losing opcode: `%s' \"%s\"\n",
587                        sparc_opcodes[i].name, sparc_opcodes[i].args);
588               lose = 1;
589             }
590           ++i;
591         }
592       while (i < NUMOPCODES
593              && !strcmp (sparc_opcodes[i].name, name));
594     }
595
596   if (lose)
597     as_fatal ("Broken assembler.  No assembly attempted.");
598
599   for (i = '0'; i < '8'; ++i)
600     octal[i] = 1;
601   for (i = '0'; i <= '9'; ++i)
602     toHex[i] = i - '0';
603   for (i = 'a'; i <= 'f'; ++i)
604     toHex[i] = i + 10 - 'a';
605   for (i = 'A'; i <= 'F'; ++i)
606     toHex[i] = i + 10 - 'A';
607
608 #ifndef NO_V9
609   qsort (priv_reg_table, sizeof (priv_reg_table) / sizeof (priv_reg_table[0]),
610          sizeof (priv_reg_table[0]), cmp_reg_entry);
611 #endif
612
613   target_big_endian = 1;
614 }
615
616 void
617 md_assemble (str)
618      char *str;
619 {
620   char *toP;
621   int rsd;
622
623   know (str);
624   sparc_ip (str);
625
626   /* See if "set" operand is absolute and small; skip sethi if so. */
627   if (special_case == SPECIAL_CASE_SET
628       && the_insn.exp.X_op == O_constant)
629     {
630       if (the_insn.exp.X_add_number >= -(1 << 12)
631           && the_insn.exp.X_add_number < (1 << 12))
632         {
633           the_insn.opcode = 0x80102000  /* or %g0,imm,... */
634             | (the_insn.opcode & 0x3E000000)    /* dest reg */
635             | (the_insn.exp.X_add_number & 0x1FFF);     /* imm */
636           special_case = 0;     /* No longer special */
637           the_insn.reloc = BFD_RELOC_NONE;      /* No longer relocated */
638         }
639     }
640
641   toP = frag_more (4);
642   /* put out the opcode */
643   md_number_to_chars (toP, (valueT) the_insn.opcode, 4);
644
645   /* put out the symbol-dependent stuff */
646   if (the_insn.reloc != BFD_RELOC_NONE)
647     {
648       fix_new_exp (frag_now,    /* which frag */
649                    (toP - frag_now->fr_literal),        /* where */
650                    4,           /* size */
651                    &the_insn.exp,
652                    the_insn.pcrel,
653                    the_insn.reloc);
654     }
655
656   switch (special_case)
657     {
658     case SPECIAL_CASE_SET:
659       special_case = 0;
660       assert (the_insn.reloc == BFD_RELOC_HI22);
661       /* See if "set" operand has no low-order bits; skip OR if so. */
662       if (the_insn.exp.X_op == O_constant
663           && ((the_insn.exp.X_add_number & 0x3FF) == 0))
664         return;
665       toP = frag_more (4);
666       rsd = (the_insn.opcode >> 25) & 0x1f;
667       the_insn.opcode = 0x80102000 | (rsd << 25) | (rsd << 14);
668       md_number_to_chars (toP, (valueT) the_insn.opcode, 4);
669       fix_new_exp (frag_now,    /* which frag */
670                    (toP - frag_now->fr_literal),        /* where */
671                    4,           /* size */
672                    &the_insn.exp,
673                    the_insn.pcrel,
674                    BFD_RELOC_LO10);
675       return;
676
677     case SPECIAL_CASE_FDIV:
678       /* According to information leaked from Sun, the "fdiv" instructions
679          on early SPARC machines would produce incorrect results sometimes.
680          The workaround is to add an fmovs of the destination register to
681          itself just after the instruction.  This was true on machines
682          with Weitek 1165 float chips, such as the Sun-4/260 and /280. */
683       special_case = 0;
684       assert (the_insn.reloc == BFD_RELOC_NONE);
685       toP = frag_more (4);
686       rsd = (the_insn.opcode >> 25) & 0x1f;
687       the_insn.opcode = 0x81A00020 | (rsd << 25) | rsd; /* fmovs dest,dest */
688       md_number_to_chars (toP, (valueT) the_insn.opcode, 4);
689       return;
690
691     case 0:
692       return;
693
694     default:
695       as_fatal ("failed sanity check.");
696     }
697 }
698
699 /* Implement big shift right.  */
700 static bfd_vma
701 BSR (val, amount)
702      bfd_vma val;
703      int amount;
704 {
705   if (sizeof (bfd_vma) <= 4 && amount >= 32)
706     as_fatal ("Support for 64-bit arithmetic not compiled in.");
707   return val >> amount;
708 }
709
710 static void
711 sparc_ip (str)
712      char *str;
713 {
714   char *error_message = "";
715   char *s;
716   const char *args;
717   char c;
718   const struct sparc_opcode *insn;
719   char *argsStart;
720   unsigned long opcode;
721   unsigned int mask = 0;
722   int match = 0;
723   int comma = 0;
724   long immediate_max = 0;
725
726   for (s = str; islower (*s) || (*s >= '0' && *s <= '3'); ++s)
727     ;
728   switch (*s)
729     {
730
731     case '\0':
732       break;
733
734     case ',':
735       comma = 1;
736
737       /*FALLTHROUGH */
738
739     case ' ':
740       *s++ = '\0';
741       break;
742
743     default:
744       as_fatal ("Unknown opcode: `%s'", str);
745     }
746   insn = (struct sparc_opcode *) hash_find (op_hash, str);
747   if (insn == NULL)
748     {
749       as_bad ("Unknown opcode: `%s'", str);
750       return;
751     }
752   if (comma)
753     {
754       *--s = ',';
755     }
756   argsStart = s;
757   for (;;)
758     {
759       opcode = insn->match;
760       memset (&the_insn, '\0', sizeof (the_insn));
761       the_insn.reloc = BFD_RELOC_NONE;
762
763       /*
764        * Build the opcode, checking as we go to make
765        * sure that the operands match
766        */
767       for (args = insn->args;; ++args)
768         {
769           switch (*args)
770             {
771 #ifndef NO_V9
772             case 'K':
773               {
774                 int kmask = 0;
775                 int i;
776
777                 /* Parse a series of masks.  */
778                 if (*s == '#')
779                   {
780                     while (*s == '#')
781                       {
782                         ++s;
783                         for (i = 0; i < MEMBAR_MASKS_SIZE; i++)
784                           if (!strncmp (s, membar_masks[i].name,
785                                         membar_masks[i].len))
786                             break;
787                         if (i < MEMBAR_MASKS_SIZE)
788                           {
789                             kmask |= membar_masks[i].mask;
790                             s += membar_masks[i].len;
791                           }
792                         else
793                           {
794                             error_message = ": invalid membar mask name";
795                             goto error;
796                           }
797                         if (*s == '|')
798                           ++s;
799                       }
800                   }
801                 else
802                   {
803                     expressionS exp;
804                     char *hold;
805                     char *send;
806
807                     hold = input_line_pointer;
808                     input_line_pointer = s;
809                     expression (&exp);
810                     send = input_line_pointer;
811                     input_line_pointer = hold;
812
813                     kmask = exp.X_add_number;
814                     if (exp.X_op != O_constant
815                         || kmask < 0
816                         || kmask > 127)
817                       {
818                         error_message = ": invalid membar mask number";
819                         goto error;
820                       }
821
822                     s = send;
823                   }
824
825                 opcode |= SIMM13 (kmask);
826                 continue;
827               }
828
829             case '*':
830               {
831                 int prefetch_fcn = 0;
832
833                 /* Parse a prefetch function.  */
834                 if (*s == '#')
835                   {
836                     s += 1;
837                     if (!strncmp (s, "n_reads", 7))
838                       prefetch_fcn = 0, s += 7;
839                     else if (!strncmp (s, "one_read", 8))
840                       prefetch_fcn = 1, s += 8;
841                     else if (!strncmp (s, "n_writes", 8))
842                       prefetch_fcn = 2, s += 8;
843                     else if (!strncmp (s, "one_write", 9))
844                       prefetch_fcn = 3, s += 9;
845                     else if (!strncmp (s, "page", 4))
846                       prefetch_fcn = 4, s += 4;
847                     else
848                       {
849                         error_message = ": invalid prefetch function name";
850                         goto error;
851                       }
852                   }
853                 else if (isdigit (*s))
854                   {
855                     while (isdigit (*s))
856                       {
857                         prefetch_fcn = prefetch_fcn * 10 + *s - '0';
858                         ++s;
859                       }
860
861                     if (prefetch_fcn < 0 || prefetch_fcn > 31)
862                       {
863                         error_message = ": invalid prefetch function number";
864                         goto error;
865                       }
866                   }
867                 else
868                   {
869                     error_message = ": unrecognizable prefetch function";
870                     goto error;
871                   }
872                 opcode |= RD (prefetch_fcn);
873                 continue;
874               }
875
876             case '!':
877             case '?':
878               /* Parse a privileged register.  */
879               if (*s == '%')
880                 {
881                   struct priv_reg_entry *p = priv_reg_table;
882                   unsigned int len = 9999999; /* init to make gcc happy */
883
884                   s += 1;
885                   while (p->name[0] > s[0])
886                     p++;
887                   while (p->name[0] == s[0])
888                     {
889                       len = strlen (p->name);
890                       if (strncmp (p->name, s, len) == 0)
891                         break;
892                       p++;
893                     }
894                   if (p->name[0] != s[0])
895                     {
896                       error_message = ": unrecognizable privileged register";
897                       goto error;
898                     }
899                   if (*args == '?')
900                     opcode |= (p->regnum << 14);
901                   else
902                     opcode |= (p->regnum << 25);
903                   s += len;
904                   continue;
905                 }
906               else
907                 {
908                   error_message = ": unrecognizable privileged register";
909                   goto error;
910                 }
911 #endif
912
913             case 'M':
914             case 'm':
915               if (strncmp (s, "%asr", 4) == 0)
916                 {
917                   s += 4;
918
919                   if (isdigit (*s))
920                     {
921                       long num = 0;
922
923                       while (isdigit (*s))
924                         {
925                           num = num * 10 + *s - '0';
926                           ++s;
927                         }
928
929                       if (num < 16 || 31 < num)
930                         {
931                           error_message = ": asr number must be between 15 and 31";
932                           goto error;
933                         }       /* out of range */
934
935                       opcode |= (*args == 'M' ? RS1 (num) : RD (num));
936                       continue;
937                     }
938                   else
939                     {
940                       error_message = ": expecting %asrN";
941                       goto error;
942                     }           /* if %asr followed by a number. */
943
944                 }               /* if %asr */
945               break;
946
947 #ifndef NO_V9
948             case 'I':
949               the_insn.reloc = BFD_RELOC_SPARC_11;
950               immediate_max = 0x03FF;
951               goto immediate;
952
953             case 'j':
954               the_insn.reloc = BFD_RELOC_SPARC_10;
955               immediate_max = 0x01FF;
956               goto immediate;
957
958             case 'k':
959               the_insn.reloc = /* RELOC_WDISP2_14 */ BFD_RELOC_SPARC_WDISP16;
960               the_insn.pcrel = 1;
961               goto immediate;
962
963             case 'G':
964               the_insn.reloc = BFD_RELOC_SPARC_WDISP19;
965               the_insn.pcrel = 1;
966               goto immediate;
967
968             case 'N':
969               if (*s == 'p' && s[1] == 'n')
970                 {
971                   s += 2;
972                   continue;
973                 }
974               break;
975
976             case 'T':
977               if (*s == 'p' && s[1] == 't')
978                 {
979                   s += 2;
980                   continue;
981                 }
982               break;
983
984             case 'z':
985               if (*s == ' ')
986                 {
987                   ++s;
988                 }
989               if (strncmp (s, "%icc", 4) == 0)
990                 {
991                   s += 4;
992                   continue;
993                 }
994               break;
995
996             case 'Z':
997               if (*s == ' ')
998                 {
999                   ++s;
1000                 }
1001               if (strncmp (s, "%xcc", 4) == 0)
1002                 {
1003                   s += 4;
1004                   continue;
1005                 }
1006               break;
1007
1008             case '6':
1009               if (*s == ' ')
1010                 {
1011                   ++s;
1012                 }
1013               if (strncmp (s, "%fcc0", 5) == 0)
1014                 {
1015                   s += 5;
1016                   continue;
1017                 }
1018               break;
1019
1020             case '7':
1021               if (*s == ' ')
1022                 {
1023                   ++s;
1024                 }
1025               if (strncmp (s, "%fcc1", 5) == 0)
1026                 {
1027                   s += 5;
1028                   continue;
1029                 }
1030               break;
1031
1032             case '8':
1033               if (*s == ' ')
1034                 {
1035                   ++s;
1036                 }
1037               if (strncmp (s, "%fcc2", 5) == 0)
1038                 {
1039                   s += 5;
1040                   continue;
1041                 }
1042               break;
1043
1044             case '9':
1045               if (*s == ' ')
1046                 {
1047                   ++s;
1048                 }
1049               if (strncmp (s, "%fcc3", 5) == 0)
1050                 {
1051                   s += 5;
1052                   continue;
1053                 }
1054               break;
1055
1056             case 'P':
1057               if (strncmp (s, "%pc", 3) == 0)
1058                 {
1059                   s += 3;
1060                   continue;
1061                 }
1062               break;
1063
1064             case 'W':
1065               if (strncmp (s, "%tick", 5) == 0)
1066                 {
1067                   s += 5;
1068                   continue;
1069                 }
1070               break;
1071 #endif /* NO_V9 */
1072
1073             case '\0':          /* end of args */
1074               if (*s == '\0')
1075                 {
1076                   match = 1;
1077                 }
1078               break;
1079
1080             case '+':
1081               if (*s == '+')
1082                 {
1083                   ++s;
1084                   continue;
1085                 }
1086               if (*s == '-')
1087                 {
1088                   continue;
1089                 }
1090               break;
1091
1092             case '[':           /* these must match exactly */
1093             case ']':
1094             case ',':
1095             case ' ':
1096               if (*s++ == *args)
1097                 continue;
1098               break;
1099
1100             case '#':           /* must be at least one digit */
1101               if (isdigit (*s++))
1102                 {
1103                   while (isdigit (*s))
1104                     {
1105                       ++s;
1106                     }
1107                   continue;
1108                 }
1109               break;
1110
1111             case 'C':           /* coprocessor state register */
1112               if (strncmp (s, "%csr", 4) == 0)
1113                 {
1114                   s += 4;
1115                   continue;
1116                 }
1117               break;
1118
1119             case 'b':           /* next operand is a coprocessor register */
1120             case 'c':
1121             case 'D':
1122               if (*s++ == '%' && *s++ == 'c' && isdigit (*s))
1123                 {
1124                   mask = *s++;
1125                   if (isdigit (*s))
1126                     {
1127                       mask = 10 * (mask - '0') + (*s++ - '0');
1128                       if (mask >= 32)
1129                         {
1130                           break;
1131                         }
1132                     }
1133                   else
1134                     {
1135                       mask -= '0';
1136                     }
1137                   switch (*args)
1138                     {
1139
1140                     case 'b':
1141                       opcode |= mask << 14;
1142                       continue;
1143
1144                     case 'c':
1145                       opcode |= mask;
1146                       continue;
1147
1148                     case 'D':
1149                       opcode |= mask << 25;
1150                       continue;
1151                     }
1152                 }
1153               break;
1154
1155             case 'r':           /* next operand must be a register */
1156             case '1':
1157             case '2':
1158             case 'd':
1159               if (*s++ == '%')
1160                 {
1161                   switch (c = *s++)
1162                     {
1163
1164                     case 'f':   /* frame pointer */
1165                       if (*s++ == 'p')
1166                         {
1167                           mask = 0x1e;
1168                           break;
1169                         }
1170                       goto error;
1171
1172                     case 'g':   /* global register */
1173                       if (isoctal (c = *s++))
1174                         {
1175                           mask = c - '0';
1176                           break;
1177                         }
1178                       goto error;
1179
1180                     case 'i':   /* in register */
1181                       if (isoctal (c = *s++))
1182                         {
1183                           mask = c - '0' + 24;
1184                           break;
1185                         }
1186                       goto error;
1187
1188                     case 'l':   /* local register */
1189                       if (isoctal (c = *s++))
1190                         {
1191                           mask = (c - '0' + 16);
1192                           break;
1193                         }
1194                       goto error;
1195
1196                     case 'o':   /* out register */
1197                       if (isoctal (c = *s++))
1198                         {
1199                           mask = (c - '0' + 8);
1200                           break;
1201                         }
1202                       goto error;
1203
1204                     case 's':   /* stack pointer */
1205                       if (*s++ == 'p')
1206                         {
1207                           mask = 0xe;
1208                           break;
1209                         }
1210                       goto error;
1211
1212                     case 'r':   /* any register */
1213                       if (!isdigit (c = *s++))
1214                         {
1215                           goto error;
1216                         }
1217                       /* FALLTHROUGH */
1218                     case '0':
1219                     case '1':
1220                     case '2':
1221                     case '3':
1222                     case '4':
1223                     case '5':
1224                     case '6':
1225                     case '7':
1226                     case '8':
1227                     case '9':
1228                       if (isdigit (*s))
1229                         {
1230                           if ((c = 10 * (c - '0') + (*s++ - '0')) >= 32)
1231                             {
1232                               goto error;
1233                             }
1234                         }
1235                       else
1236                         {
1237                           c -= '0';
1238                         }
1239                       mask = c;
1240                       break;
1241
1242                     default:
1243                       goto error;
1244                     }
1245                   /*
1246                                          * Got the register, now figure out where
1247                                          * it goes in the opcode.
1248                                          */
1249                   switch (*args)
1250                     {
1251
1252                     case '1':
1253                       opcode |= mask << 14;
1254                       continue;
1255
1256                     case '2':
1257                       opcode |= mask;
1258                       continue;
1259
1260                     case 'd':
1261                       opcode |= mask << 25;
1262                       continue;
1263
1264                     case 'r':
1265                       opcode |= (mask << 25) | (mask << 14);
1266                       continue;
1267                     }
1268                 }
1269               break;
1270
1271             case 'e':           /* next operand is a floating point register */
1272             case 'v':
1273             case 'V':
1274
1275             case 'f':
1276             case 'B':
1277             case 'R':
1278
1279             case 'g':
1280             case 'H':
1281             case 'J':
1282               {
1283                 char format;
1284
1285                 if (*s++ == '%'
1286                     && ((format = *s) == 'f')
1287                     && isdigit (*++s))
1288                   {
1289                     for (mask = 0; isdigit (*s); ++s)
1290                       {
1291                         mask = 10 * mask + (*s - '0');
1292                       }         /* read the number */
1293
1294                     if ((*args == 'v'
1295                          || *args == 'B'
1296                          || *args == 'H')
1297                         && (mask & 1))
1298                       {
1299                         break;
1300                       }         /* register must be even numbered */
1301
1302                     if ((*args == 'V'
1303                          || *args == 'R'
1304                          || *args == 'J')
1305                         && (mask & 3))
1306                       {
1307                         break;
1308                       }         /* register must be multiple of 4 */
1309
1310 #ifndef NO_V9
1311                     if (mask >= 64)
1312                       {
1313                         error_message = ": There are only 64 f registers; [0-63]";
1314                         goto error;
1315                       } /* on error */
1316                     if (mask >= 32)
1317                       {
1318                         mask -= 31;
1319                       } /* wrap high bit */
1320 #else
1321                     if (mask >= 32)
1322                       {
1323                         error_message = ": There are only 32 f registers; [0-31]";
1324                         goto error;
1325                       } /* on error */
1326 #endif
1327                   }
1328                 else
1329                   {
1330                     break;
1331                   }     /* if not an 'f' register. */
1332
1333                 switch (*args)
1334                   {
1335
1336                   case 'v':
1337                   case 'V':
1338                   case 'e':
1339                     opcode |= RS1 (mask);
1340                     continue;
1341
1342
1343                   case 'f':
1344                   case 'B':
1345                   case 'R':
1346                     opcode |= RS2 (mask);
1347                     continue;
1348
1349                   case 'g':
1350                   case 'H':
1351                   case 'J':
1352                     opcode |= RD (mask);
1353                     continue;
1354                   }             /* pack it in. */
1355
1356                 know (0);
1357                 break;
1358               }                 /* float arg */
1359
1360             case 'F':
1361               if (strncmp (s, "%fsr", 4) == 0)
1362                 {
1363                   s += 4;
1364                   continue;
1365                 }
1366               break;
1367
1368             case 'h':           /* high 22 bits */
1369               the_insn.reloc = BFD_RELOC_HI22;
1370               goto immediate;
1371
1372             case 'l':           /* 22 bit PC relative immediate */
1373               the_insn.reloc = BFD_RELOC_SPARC_WDISP22;
1374               the_insn.pcrel = 1;
1375               goto immediate;
1376
1377             case 'L':           /* 30 bit immediate */
1378               the_insn.reloc = BFD_RELOC_32_PCREL_S2;
1379               the_insn.pcrel = 1;
1380               goto immediate;
1381
1382             case 'n':           /* 22 bit immediate */
1383               the_insn.reloc = BFD_RELOC_SPARC22;
1384               goto immediate;
1385
1386             case 'i':           /* 13 bit immediate */
1387               the_insn.reloc = BFD_RELOC_SPARC13;
1388               immediate_max = 0x0FFF;
1389
1390               /*FALLTHROUGH */
1391
1392             immediate:
1393               if (*s == ' ')
1394                 s++;
1395               if (*s == '%')
1396                 {
1397                   if ((c = s[1]) == 'h' && s[2] == 'i')
1398                     {
1399                       the_insn.reloc = BFD_RELOC_HI22;
1400                       s += 3;
1401                     }
1402                   else if (c == 'l' && s[2] == 'o')
1403                     {
1404                       the_insn.reloc = BFD_RELOC_LO10;
1405                       s += 3;
1406                     }
1407 #ifndef NO_V9
1408                   else if (c == 'u'
1409                            && s[2] == 'h'
1410                            && s[3] == 'i')
1411                     {
1412                       the_insn.reloc = BFD_RELOC_SPARC_HH22;
1413                       s += 4;
1414                     }
1415                   else if (c == 'u'
1416                            && s[2] == 'l'
1417                            && s[3] == 'o')
1418                     {
1419                       the_insn.reloc = BFD_RELOC_SPARC_HM10;
1420                       s += 4;
1421                     }
1422 #endif /* NO_V9 */
1423                   else
1424                     break;
1425                 }
1426               /* Note that if the getExpression() fails, we will still
1427                  have created U entries in the symbol table for the
1428                  'symbols' in the input string.  Try not to create U
1429                  symbols for registers, etc.  */
1430               {
1431                 /* This stuff checks to see if the expression ends in
1432                    +%reg.  If it does, it removes the register from
1433                    the expression, and re-sets 's' to point to the
1434                    right place.  */
1435
1436                 char *s1;
1437
1438                 for (s1 = s; *s1 && *s1 != ',' && *s1 != ']'; s1++);;
1439
1440                 if (s1 != s && isdigit (s1[-1]))
1441                   {
1442                     if (s1[-2] == '%' && s1[-3] == '+')
1443                       {
1444                         s1 -= 3;
1445                         *s1 = '\0';
1446                         (void) getExpression (s);
1447                         *s1 = '+';
1448                         s = s1;
1449                         continue;
1450                       }
1451                     else if (strchr ("goli0123456789", s1[-2]) && s1[-3] == '%' && s1[-4] == '+')
1452                       {
1453                         s1 -= 4;
1454                         *s1 = '\0';
1455                         (void) getExpression (s);
1456                         *s1 = '+';
1457                         s = s1;
1458                         continue;
1459                       }
1460                   }
1461               }
1462               (void) getExpression (s);
1463               s = expr_end;
1464
1465               if (the_insn.exp.X_op == O_constant
1466                   && the_insn.exp.X_add_symbol == 0
1467                   && the_insn.exp.X_op_symbol == 0)
1468                 {
1469 #ifndef NO_V9
1470                   /* Handle %uhi/%ulo by moving the upper word to the lower
1471                      one and pretending it's %hi/%lo.  We also need to watch
1472                      for %hi/%lo: the top word needs to be zeroed otherwise
1473                      fixup_segment will complain the value is too big.  */
1474                   switch (the_insn.reloc)
1475                     {
1476                     case BFD_RELOC_SPARC_HH22:
1477                       the_insn.reloc = BFD_RELOC_HI22;
1478                       the_insn.exp.X_add_number = BSR (the_insn.exp.X_add_number, 32);
1479                       break;
1480                     case BFD_RELOC_SPARC_HM10:
1481                       the_insn.reloc = BFD_RELOC_LO10;
1482                       the_insn.exp.X_add_number = BSR (the_insn.exp.X_add_number, 32);
1483                       break;
1484                     default:
1485                       break;
1486                     case BFD_RELOC_HI22:
1487                     case BFD_RELOC_LO10:
1488                       the_insn.exp.X_add_number &= 0xffffffff;
1489                       break;
1490                     }
1491 #endif
1492                   /* For pc-relative call instructions, we reject
1493                      constants to get better code.  */
1494                   if (the_insn.pcrel
1495                       && the_insn.reloc == BFD_RELOC_32_PCREL_S2
1496                       && in_signed_range (the_insn.exp.X_add_number, 0x3fff)
1497                       )
1498                     {
1499                       error_message = ": PC-relative operand can't be a constant";
1500                       goto error;
1501                     }
1502                   /* Check for invalid constant values.  Don't warn if
1503                      constant was inside %hi or %lo, since these
1504                      truncate the constant to fit.  */
1505                   if (immediate_max != 0
1506                       && the_insn.reloc != BFD_RELOC_LO10
1507                       && the_insn.reloc != BFD_RELOC_HI22
1508                       && !in_signed_range (the_insn.exp.X_add_number,
1509                                            immediate_max)
1510                       )
1511                     {
1512                       if (the_insn.pcrel)
1513                         /* Who knows?  After relocation, we may be within
1514                            range.  Let the linker figure it out.  */
1515                         {
1516                           the_insn.exp.X_op = O_symbol;
1517                           the_insn.exp.X_add_symbol = section_symbol (absolute_section);
1518                         }
1519                       else
1520                         /* Immediate value is non-pcrel, and out of
1521                            range.  */
1522                         as_bad ("constant value %ld out of range (%ld .. %ld)",
1523                                 the_insn.exp.X_add_number,
1524                                 ~immediate_max, immediate_max);
1525                     }
1526                 }
1527
1528               /* Reset to prevent extraneous range check.  */
1529               immediate_max = 0;
1530
1531               continue;
1532
1533             case 'a':
1534               if (*s++ == 'a')
1535                 {
1536                   opcode |= ANNUL;
1537                   continue;
1538                 }
1539               break;
1540
1541             case 'A':
1542               {
1543                 int asi = 0;
1544
1545                 /* Parse an asi.  */
1546                 if (*s == '#')
1547                   {
1548                     s += 1;
1549                     if (!strncmp (s, "ASI_AIUP", 8))
1550                       asi = 0x10, s += 8;
1551                     else if (!strncmp (s, "ASI_AIUS", 8))
1552                       asi = 0x11, s += 8;
1553                     else if (!strncmp (s, "ASI_PNF", 7))
1554                       asi = 0x82, s += 7;
1555                     else if (!strncmp (s, "ASI_SNF", 7))
1556                       asi = 0x83, s += 7;
1557                     else if (!strncmp (s, "ASI_P", 5))
1558                       asi = 0x80, s += 5;
1559                     else if (!strncmp (s, "ASI_S", 5))
1560                       asi = 0x81, s += 5;
1561                     else
1562                       {
1563                         error_message = ": invalid asi name";
1564                         goto error;
1565                       }
1566                   }
1567                 else
1568                   {
1569                     char *push = input_line_pointer;
1570                     expressionS e;
1571                     input_line_pointer = s;
1572
1573                     expression (&e);
1574                     if (e.X_op != O_constant)
1575                       {
1576                         error_message = ": constant required for ASI";
1577                         goto error;
1578                       }
1579                     asi = e.X_add_number;
1580                     s = input_line_pointer;
1581                     input_line_pointer = push;
1582                     
1583                     if (asi < 0 || asi > 255)
1584                       {
1585                         error_message = ": invalid asi number";
1586                         goto error;
1587                       }
1588                   }
1589                 opcode |= ASI (asi);
1590                 continue;
1591               }                 /* alternate space */
1592
1593             case 'p':
1594               if (strncmp (s, "%psr", 4) == 0)
1595                 {
1596                   s += 4;
1597                   continue;
1598                 }
1599               break;
1600
1601             case 'q':           /* floating point queue */
1602               if (strncmp (s, "%fq", 3) == 0)
1603                 {
1604                   s += 3;
1605                   continue;
1606                 }
1607               break;
1608
1609             case 'Q':           /* coprocessor queue */
1610               if (strncmp (s, "%cq", 3) == 0)
1611                 {
1612                   s += 3;
1613                   continue;
1614                 }
1615               break;
1616
1617             case 'S':
1618               if (strcmp (str, "set") == 0)
1619                 {
1620                   special_case = SPECIAL_CASE_SET;
1621                   continue;
1622                 }
1623               else if (strncmp (str, "fdiv", 4) == 0)
1624                 {
1625                   special_case = SPECIAL_CASE_FDIV;
1626                   continue;
1627                 }
1628               break;
1629
1630 #ifndef NO_V9
1631             case 'o':
1632               if (strncmp (s, "%asi", 4) != 0)
1633                 break;
1634               s += 4;
1635               continue;
1636
1637             case 's':
1638               if (strncmp (s, "%fprs", 5) != 0)
1639                 break;
1640               s += 5;
1641               continue;
1642
1643             case 'E':
1644               if (strncmp (s, "%ccr", 4) != 0)
1645                 break;
1646               s += 4;
1647               continue;
1648 #endif /* NO_V9 */
1649
1650             case 't':
1651               if (strncmp (s, "%tbr", 4) != 0)
1652                 break;
1653               s += 4;
1654               continue;
1655
1656             case 'w':
1657               if (strncmp (s, "%wim", 4) != 0)
1658                 break;
1659               s += 4;
1660               continue;
1661
1662 #ifndef NO_V9
1663             case 'x':
1664               {
1665                 char *push = input_line_pointer;
1666                 expressionS e;
1667
1668                 input_line_pointer = s;
1669                 expression (&e);
1670                 if (e.X_op == O_constant)
1671                   {
1672                     int n = e.X_add_number;
1673                     if (n != e.X_add_number || (n & ~0x1ff) != 0)
1674                       as_bad ("OPF immediate operand out of range (0-0x1ff)");
1675                     else
1676                       opcode |= e.X_add_number << 5;
1677                   }
1678                 else
1679                   as_bad ("non-immediate OPF operand, ignored");
1680                 s = input_line_pointer;
1681                 input_line_pointer = push;
1682                 continue;
1683               }
1684 #endif
1685
1686             case 'y':
1687               if (strncmp (s, "%y", 2) != 0)
1688                 break;
1689               s += 2;
1690               continue;
1691
1692             default:
1693               as_fatal ("failed sanity check.");
1694             }                   /* switch on arg code */
1695           break;
1696         }                       /* for each arg that we expect */
1697     error:
1698       if (match == 0)
1699         {
1700           /* Args don't match. */
1701           if (((unsigned) (&insn[1] - sparc_opcodes)) < NUMOPCODES
1702               && (insn->name == insn[1].name
1703                   || !strcmp (insn->name, insn[1].name)))
1704             {
1705               ++insn;
1706               s = argsStart;
1707               continue;
1708             }
1709           else
1710             {
1711               as_bad ("Illegal operands%s", error_message);
1712               return;
1713             }
1714         }
1715       else
1716         {
1717           if (insn->architecture > current_architecture
1718               || (insn->architecture != current_architecture
1719                   && current_architecture > v8))
1720             {
1721               if ((!architecture_requested || warn_on_bump)
1722                   && !ARCHITECTURES_CONFLICT_P (current_architecture,
1723                                                 insn->architecture)
1724                   && !ARCHITECTURES_CONFLICT_P (insn->architecture,
1725                                                 current_architecture))
1726                 {
1727                   if (warn_on_bump)
1728                     {
1729                       as_warn ("architecture bumped from \"%s\" to \"%s\" on \"%s\"",
1730                                architecture_pname[current_architecture],
1731                                architecture_pname[insn->architecture],
1732                                str);
1733                     }           /* if warning */
1734
1735                   current_architecture = insn->architecture;
1736                 }
1737               else
1738                 {
1739                   as_bad ("Architecture mismatch on \"%s\".", str);
1740                   as_tsktsk (" (Requires %s; current architecture is %s.)",
1741                              architecture_pname[insn->architecture],
1742                              architecture_pname[current_architecture]);
1743                   return;
1744                 }               /* if bump ok else error */
1745             }                   /* if architecture higher */
1746         }                       /* if no match */
1747
1748       break;
1749     }                           /* forever looking for a match */
1750
1751   the_insn.opcode = opcode;
1752 }
1753
1754 static int
1755 getExpression (str)
1756      char *str;
1757 {
1758   char *save_in;
1759   segT seg;
1760
1761   save_in = input_line_pointer;
1762   input_line_pointer = str;
1763   seg = expression (&the_insn.exp);
1764   if (seg != absolute_section
1765       && seg != text_section
1766       && seg != data_section
1767       && seg != bss_section
1768       && seg != undefined_section)
1769     {
1770       the_insn.error = "bad segment";
1771       expr_end = input_line_pointer;
1772       input_line_pointer = save_in;
1773       return 1;
1774     }
1775   expr_end = input_line_pointer;
1776   input_line_pointer = save_in;
1777   return 0;
1778 }                               /* getExpression() */
1779
1780
1781 /*
1782   This is identical to the md_atof in m68k.c.  I think this is right,
1783   but I'm not sure.
1784
1785   Turn a string in input_line_pointer into a floating point constant of type
1786   type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
1787   emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
1788   */
1789
1790 /* Equal to MAX_PRECISION in atof-ieee.c */
1791 #define MAX_LITTLENUMS 6
1792
1793 char *
1794 md_atof (type, litP, sizeP)
1795      char type;
1796      char *litP;
1797      int *sizeP;
1798 {
1799   int prec;
1800   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1801   LITTLENUM_TYPE *wordP;
1802   char *t;
1803   char *atof_ieee ();
1804
1805   switch (type)
1806     {
1807
1808     case 'f':
1809     case 'F':
1810     case 's':
1811     case 'S':
1812       prec = 2;
1813       break;
1814
1815     case 'd':
1816     case 'D':
1817     case 'r':
1818     case 'R':
1819       prec = 4;
1820       break;
1821
1822     case 'x':
1823     case 'X':
1824       prec = 6;
1825       break;
1826
1827     case 'p':
1828     case 'P':
1829       prec = 6;
1830       break;
1831
1832     default:
1833       *sizeP = 0;
1834       return "Bad call to MD_ATOF()";
1835     }
1836   t = atof_ieee (input_line_pointer, type, words);
1837   if (t)
1838     input_line_pointer = t;
1839   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1840   for (wordP = words; prec--;)
1841     {
1842       md_number_to_chars (litP, (valueT) (*wordP++), sizeof (LITTLENUM_TYPE));
1843       litP += sizeof (LITTLENUM_TYPE);
1844     }
1845   return 0;
1846 }
1847
1848 /*
1849  * Write out big-endian.
1850  */
1851 void
1852 md_number_to_chars (buf, val, n)
1853      char *buf;
1854      valueT val;
1855      int n;
1856 {
1857   number_to_chars_bigendian (buf, val, n);
1858 }
1859
1860 /* Apply a fixS to the frags, now that we know the value it ought to
1861    hold. */
1862
1863 int
1864 md_apply_fix (fixP, value)
1865      fixS *fixP;
1866      valueT *value;
1867 {
1868   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1869   offsetT val;
1870
1871   val = *value;
1872
1873   assert (fixP->fx_r_type < BFD_RELOC_UNUSED);
1874
1875   fixP->fx_addnumber = val;     /* Remember value for emit_reloc */
1876
1877 #ifdef OBJ_ELF
1878   /* FIXME: SPARC ELF relocations don't use an addend in the data
1879      field itself.  This whole approach should be somehow combined
1880      with the calls to bfd_perform_relocation.  Also, the value passed
1881      in by fixup_segment includes the value of a defined symbol.  We
1882      don't want to include the value of an externally visible symbol.  */
1883   if (fixP->fx_addsy != NULL)
1884     {
1885       if (S_IS_EXTERN (fixP->fx_addsy)
1886           && S_GET_SEGMENT (fixP->fx_addsy) != absolute_section
1887           && S_GET_SEGMENT (fixP->fx_addsy) != undefined_section
1888           && ! bfd_is_com_section (S_GET_SEGMENT (fixP->fx_addsy)))
1889         fixP->fx_addnumber -= S_GET_VALUE (fixP->fx_addsy);
1890       return 1;
1891     }
1892 #endif
1893
1894   /* This is a hack.  There should be a better way to
1895      handle this.  Probably in terms of howto fields, once
1896      we can look at these fixups in terms of howtos.  */
1897   if (fixP->fx_r_type == BFD_RELOC_32_PCREL_S2 && fixP->fx_addsy)
1898     val += fixP->fx_where + fixP->fx_frag->fr_address;
1899
1900 #ifdef OBJ_AOUT
1901   /* FIXME: More ridiculous gas reloc hacking.  If we are going to
1902      generate a reloc, then we just want to let the reloc addend set
1903      the value.  We do not want to also stuff the addend into the
1904      object file.  Including the addend in the object file works when
1905      doing a static link, because the linker will ignore the object
1906      file contents.  However, the dynamic linker does not ignore the
1907      object file contents.  */
1908   if (fixP->fx_addsy != NULL
1909       && fixP->fx_r_type != BFD_RELOC_32_PCREL_S2)
1910     val = 0;
1911 #endif
1912
1913   switch (fixP->fx_r_type)
1914     {
1915     case BFD_RELOC_16:
1916       buf[0] = val >> 8;
1917       buf[1] = val;
1918       break;
1919
1920     case BFD_RELOC_32:
1921       buf[0] = val >> 24;
1922       buf[1] = val >> 16;
1923       buf[2] = val >> 8;
1924       buf[3] = val;
1925       break;
1926
1927     case BFD_RELOC_32_PCREL_S2:
1928       val = (val >>= 2) + 1;
1929       buf[0] |= (val >> 24) & 0x3f;
1930       buf[1] = (val >> 16);
1931       buf[2] = val >> 8;
1932       buf[3] = val;
1933       break;
1934
1935 #ifndef NO_V9
1936     case BFD_RELOC_64:
1937       {
1938         bfd_vma valh = BSR (val, 32);
1939         buf[0] = valh >> 24;
1940         buf[1] = valh >> 16;
1941         buf[2] = valh >> 8;
1942         buf[3] = valh;
1943         buf[4] = val >> 24;
1944         buf[5] = val >> 16;
1945         buf[6] = val >> 8;
1946         buf[7] = val;
1947       }
1948       break;
1949
1950     case BFD_RELOC_SPARC_11:
1951       if (((val > 0) && (val & ~0x7ff))
1952           || ((val < 0) && (~(val - 1) & ~0x7ff)))
1953         {
1954           as_bad ("relocation overflow.");
1955         }                       /* on overflow */
1956
1957       buf[2] |= (val >> 8) & 0x7;
1958       buf[3] = val & 0xff;
1959       break;
1960
1961     case BFD_RELOC_SPARC_10:
1962       if (((val > 0) && (val & ~0x3ff))
1963           || ((val < 0) && (~(val - 1) & ~0x3ff)))
1964         {
1965           as_bad ("relocation overflow.");
1966         }                       /* on overflow */
1967
1968       buf[2] |= (val >> 8) & 0x3;
1969       buf[3] = val & 0xff;
1970       break;
1971
1972     case BFD_RELOC_SPARC_WDISP16:
1973       if (((val > 0) && (val & ~0x3fffc))
1974           || ((val < 0) && (~(val - 1) & ~0x3fffc)))
1975         {
1976           as_bad ("relocation overflow.");
1977         }                       /* on overflow */
1978
1979       val = (val >>= 2) + 1;
1980       buf[1] |= ((val >> 14) & 0x3) << 4;
1981       buf[2] |= (val >> 8) & 0x3f;
1982       buf[3] = val & 0xff;
1983       break;
1984
1985     case BFD_RELOC_SPARC_WDISP19:
1986       if (((val > 0) && (val & ~0x1ffffc))
1987           || ((val < 0) && (~(val - 1) & ~0x1ffffc)))
1988         {
1989           as_bad ("relocation overflow.");
1990         }                       /* on overflow */
1991
1992       val = (val >>= 2) + 1;
1993       buf[1] |= (val >> 16) & 0x7;
1994       buf[2] = (val >> 8) & 0xff;
1995       buf[3] = val & 0xff;
1996       break;
1997
1998     case BFD_RELOC_SPARC_HH22:
1999       val = BSR (val, 32);
2000       /* intentional fallthrough */
2001 #endif /* NO_V9 */
2002
2003 #ifndef NO_V9
2004     case BFD_RELOC_SPARC_LM22:
2005 #endif
2006     case BFD_RELOC_HI22:
2007       if (!fixP->fx_addsy)
2008         {
2009           buf[1] |= (val >> 26) & 0x3f;
2010           buf[2] = val >> 18;
2011           buf[3] = val >> 10;
2012         }
2013       else
2014         {
2015           buf[2] = 0;
2016           buf[3] = 0;
2017         }
2018       break;
2019
2020     case BFD_RELOC_SPARC22:
2021       if (val & ~0x003fffff)
2022         {
2023           as_bad ("relocation overflow");
2024         }                       /* on overflow */
2025       buf[1] |= (val >> 16) & 0x3f;
2026       buf[2] = val >> 8;
2027       buf[3] = val & 0xff;
2028       break;
2029
2030 #ifndef NO_V9
2031     case BFD_RELOC_SPARC_HM10:
2032       val = BSR (val, 32);
2033       /* intentional fallthrough */
2034 #endif /* NO_V9 */
2035
2036     case BFD_RELOC_LO10:
2037       if (!fixP->fx_addsy)
2038         {
2039           buf[2] |= (val >> 8) & 0x03;
2040           buf[3] = val;
2041         }
2042       else
2043         buf[3] = 0;
2044       break;
2045
2046     case BFD_RELOC_SPARC13:
2047       if (! in_signed_range (val, 0x1fff))
2048         as_bad ("relocation overflow");
2049
2050       buf[2] |= (val >> 8) & 0x1f;
2051       buf[3] = val;
2052       break;
2053
2054     case BFD_RELOC_SPARC_WDISP22:
2055       val = (val >> 2) + 1;
2056       /* FALLTHROUGH */
2057     case BFD_RELOC_SPARC_BASE22:
2058       buf[1] |= (val >> 16) & 0x3f;
2059       buf[2] = val >> 8;
2060       buf[3] = val;
2061       break;
2062
2063     case BFD_RELOC_NONE:
2064     default:
2065       as_bad ("bad or unhandled relocation type: 0x%02x", fixP->fx_r_type);
2066       break;
2067     }
2068
2069   /* Are we finished with this relocation now?  */
2070   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
2071     fixP->fx_done = 1;
2072
2073   return 1;
2074 }
2075
2076 /* Translate internal representation of relocation info to BFD target
2077    format.  */
2078 arelent *
2079 tc_gen_reloc (section, fixp)
2080      asection *section;
2081      fixS *fixp;
2082 {
2083   arelent *reloc;
2084   bfd_reloc_code_real_type code;
2085
2086   reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
2087   assert (reloc != 0);
2088
2089   reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
2090   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2091
2092   switch (fixp->fx_r_type)
2093     {
2094     case BFD_RELOC_16:
2095     case BFD_RELOC_32:
2096     case BFD_RELOC_HI22:
2097     case BFD_RELOC_LO10:
2098     case BFD_RELOC_32_PCREL_S2:
2099     case BFD_RELOC_SPARC13:
2100     case BFD_RELOC_SPARC_BASE13:
2101     case BFD_RELOC_SPARC_WDISP16:
2102     case BFD_RELOC_SPARC_WDISP19:
2103     case BFD_RELOC_SPARC_WDISP22:
2104     case BFD_RELOC_64:
2105     case BFD_RELOC_SPARC_10:
2106     case BFD_RELOC_SPARC_11:
2107     case BFD_RELOC_SPARC_HH22:
2108     case BFD_RELOC_SPARC_HM10:
2109     case BFD_RELOC_SPARC_LM22:
2110     case BFD_RELOC_SPARC_PC_HH22:
2111     case BFD_RELOC_SPARC_PC_HM10:
2112     case BFD_RELOC_SPARC_PC_LM22:
2113       code = fixp->fx_r_type;
2114       break;
2115     default:
2116       abort ();
2117     }
2118   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
2119   if (reloc->howto == 0)
2120     {
2121       as_bad_where (fixp->fx_file, fixp->fx_line,
2122                     "internal error: can't export reloc type %d (`%s')",
2123                     fixp->fx_r_type, bfd_get_reloc_code_name (code));
2124       return 0;
2125     }
2126   assert (!fixp->fx_pcrel == !reloc->howto->pc_relative);
2127
2128   /* @@ Why fx_addnumber sometimes and fx_offset other times?  */
2129 #ifdef OBJ_AOUT
2130
2131   if (reloc->howto->pc_relative == 0)
2132     reloc->addend = fixp->fx_addnumber;
2133   else
2134     reloc->addend = fixp->fx_offset - reloc->address;
2135
2136 #else /* elf or coff */
2137
2138   if (reloc->howto->pc_relative == 0)
2139     reloc->addend = fixp->fx_addnumber;
2140   else if ((fixp->fx_addsy->bsym->flags & BSF_SECTION_SYM) != 0)
2141     reloc->addend = (section->vma
2142                      + fixp->fx_addnumber
2143                      + md_pcrel_from (fixp));
2144   else
2145     reloc->addend = fixp->fx_offset;
2146
2147 #endif
2148
2149   return reloc;
2150 }
2151
2152
2153 #if 0
2154 /* for debugging only */
2155 static void
2156 print_insn (insn)
2157      struct sparc_it *insn;
2158 {
2159   const char *const Reloc[] = {
2160     "RELOC_8",
2161     "RELOC_16",
2162     "RELOC_32",
2163     "RELOC_DISP8",
2164     "RELOC_DISP16",
2165     "RELOC_DISP32",
2166     "RELOC_WDISP30",
2167     "RELOC_WDISP22",
2168     "RELOC_HI22",
2169     "RELOC_22",
2170     "RELOC_13",
2171     "RELOC_LO10",
2172     "RELOC_SFA_BASE",
2173     "RELOC_SFA_OFF13",
2174     "RELOC_BASE10",
2175     "RELOC_BASE13",
2176     "RELOC_BASE22",
2177     "RELOC_PC10",
2178     "RELOC_PC22",
2179     "RELOC_JMP_TBL",
2180     "RELOC_SEGOFF16",
2181     "RELOC_GLOB_DAT",
2182     "RELOC_JMP_SLOT",
2183     "RELOC_RELATIVE",
2184     "NO_RELOC"
2185   };
2186
2187   if (insn->error)
2188     fprintf (stderr, "ERROR: %s\n");
2189   fprintf (stderr, "opcode=0x%08x\n", insn->opcode);
2190   fprintf (stderr, "reloc = %s\n", Reloc[insn->reloc]);
2191   fprintf (stderr, "exp = {\n");
2192   fprintf (stderr, "\t\tX_add_symbol = %s\n",
2193            ((insn->exp.X_add_symbol != NULL)
2194             ? ((S_GET_NAME (insn->exp.X_add_symbol) != NULL)
2195                ? S_GET_NAME (insn->exp.X_add_symbol)
2196                : "???")
2197             : "0"));
2198   fprintf (stderr, "\t\tX_sub_symbol = %s\n",
2199            ((insn->exp.X_op_symbol != NULL)
2200             ? (S_GET_NAME (insn->exp.X_op_symbol)
2201                ? S_GET_NAME (insn->exp.X_op_symbol)
2202                : "???")
2203             : "0"));
2204   fprintf (stderr, "\t\tX_add_number = %d\n",
2205            insn->exp.X_add_number);
2206   fprintf (stderr, "}\n");
2207 }
2208 #endif
2209 \f
2210 /*
2211  * md_parse_option
2212  *      Invocation line includes a switch not recognized by the base assembler.
2213  *      See if it's a processor-specific option.  These are:
2214  *
2215  *      -bump
2216  *              Warn on architecture bumps.  See also -A.
2217  *
2218  *      -Av6, -Av7, -Av8, -Av9, -Asparclite
2219  *              Select the architecture.  Instructions or features not
2220  *              supported by the selected architecture cause fatal errors.
2221  *
2222  *              The default is to start at v6, and bump the architecture up
2223  *              whenever an instruction is seen at a higher level.
2224  *
2225  *              If -bump is specified, a warning is printing when bumping to
2226  *              higher levels.
2227  *
2228  *              If an architecture is specified, all instructions must match
2229  *              that architecture.  Any higher level instructions are flagged
2230  *              as errors.
2231  *
2232  *              if both an architecture and -bump are specified, the
2233  *              architecture starts at the specified level, but bumps are
2234  *              warnings.
2235  *
2236  * Note:
2237  *              Bumping between incompatible architectures is always an
2238  *              error.  For example, from sparclite to v9.
2239  */
2240
2241 #ifdef OBJ_ELF
2242 CONST char *md_shortopts = "A:K:VQ:sq";
2243 #else
2244 CONST char *md_shortopts = "A:";
2245 #endif
2246 struct option md_longopts[] = {
2247 #define OPTION_BUMP (OPTION_MD_BASE)
2248   {"bump", no_argument, NULL, OPTION_BUMP},
2249 #define OPTION_SPARC (OPTION_MD_BASE + 1)
2250   {"sparc", no_argument, NULL, OPTION_SPARC},
2251   {NULL, no_argument, NULL, 0}
2252 };
2253 size_t md_longopts_size = sizeof(md_longopts);
2254
2255 int
2256 md_parse_option (c, arg)
2257      int c;
2258      char *arg;
2259 {
2260   switch (c)
2261     {
2262     case OPTION_BUMP:
2263       warn_on_bump = 1;
2264       break;
2265
2266     case 'A':
2267       {
2268         char *p = arg;
2269         const char **arch;
2270
2271         for (arch = architecture_pname; *arch != NULL; ++arch)
2272           {
2273             if (strcmp (p, *arch) == 0)
2274               break;
2275           }
2276
2277         if (*arch == NULL)
2278           {
2279             as_bad ("invalid architecture -A%s", p);
2280             return 0;
2281           }
2282         else
2283           {
2284             enum sparc_architecture new_arch = arch - architecture_pname;
2285 #ifdef NO_V9
2286             if (new_arch == v9)
2287               {
2288                 as_error ("v9 support not compiled in");
2289                 return 0;
2290               }
2291 #endif
2292             current_architecture = new_arch;
2293             architecture_requested = 1;
2294           }
2295       }
2296       break;
2297
2298     case OPTION_SPARC:
2299       /* Ignore -sparc, used by SunOS make default .s.o rule.  */
2300       break;
2301
2302 #ifdef OBJ_ELF
2303     case 'V':
2304       print_version_id ();
2305       break;
2306
2307     case 'Q':
2308       /* Qy - do emit .comment
2309          Qn - do not emit .comment */
2310       break;
2311
2312     case 's':
2313       /* use .stab instead of .stab.excl */
2314       break;
2315
2316     case 'q':
2317       /* quick -- native assembler does fewer checks */
2318       break;
2319
2320     case 'K':
2321       if (strcmp (arg, "PIC") != 0)
2322         as_warn ("Unrecognized option following -K");
2323       else
2324         {
2325           as_warn ("gas does not currently support PIC code for the SPARC");
2326           as_fatal ("use /usr/ccs/bin/as instead");
2327         }
2328 #endif
2329
2330     default:
2331       return 0;
2332     }
2333
2334  return 1;
2335 }
2336
2337 void
2338 md_show_usage (stream)
2339      FILE *stream;
2340 {
2341   const char **arch;
2342   fprintf(stream, "SPARC options:\n");
2343   for (arch = architecture_pname; *arch; arch++)
2344     {
2345       if (arch != architecture_pname)
2346         fprintf (stream, " | ");
2347       fprintf (stream, "-A%s", *arch);
2348     }
2349   fprintf (stream, "\n\
2350                         specify variant of SPARC architecture\n\
2351 -bump                   warn when assembler switches architectures\n\
2352 -sparc                  ignored\n");
2353 #ifdef OBJ_ELF
2354   fprintf(stream, "\
2355 -V                      print assembler version number\n\
2356 -q                      ignored\n\
2357 -Qy, -Qn                ignored\n\
2358 -s                      ignored\n");
2359 #endif
2360 }
2361 \f
2362 /* We have no need to default values of symbols. */
2363
2364 /* ARGSUSED */
2365 symbolS *
2366 md_undefined_symbol (name)
2367      char *name;
2368 {
2369   return 0;
2370 }                               /* md_undefined_symbol() */
2371
2372 /* Round up a section size to the appropriate boundary. */
2373 valueT
2374 md_section_align (segment, size)
2375      segT segment;
2376      valueT size;
2377 {
2378 #ifndef OBJ_ELF
2379   /* This is not right for ELF; a.out wants it, and COFF will force
2380      the alignment anyways.  */
2381   valueT align = ((valueT) 1
2382                   << (valueT) bfd_get_section_alignment (stdoutput, segment));
2383   valueT newsize;
2384   /* turn alignment value into a mask */
2385   align--;
2386   newsize = (size + align) & ~align;
2387   return newsize;
2388 #else
2389   return size;
2390 #endif
2391 }
2392
2393 /* Exactly what point is a PC-relative offset relative TO?
2394    On the sparc, they're relative to the address of the offset, plus
2395    its size.  This gets us to the following instruction.
2396    (??? Is this right?  FIXME-SOON) */
2397 long 
2398 md_pcrel_from (fixP)
2399      fixS *fixP;
2400 {
2401   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
2402 }
2403
2404 /* end of tc-sparc.c */