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