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/* relocation type for internal assembler use only */
21#define SPARC_RELOC_13 (127)
22#define SPARC_RELOC_22 (126)
23#define SPARC_RELOC_NONE (125)
24
25#define cypress 1234
26
27#undef DEBUGINSN
28
29#include <stdio.h>
30#include <ctype.h>
31#include "as.h"
32#include "libc.h"
33#include "md.h"
34#include "messages.h"
35#include "symbols.h"
36#include "sections.h"
37
38/* careful, this file includes data *declarations* */
39#include "sparc-opcode.h"
40#include <mach-o/sparc/reloc.h>
41
42/* From GNU ansidecl.h */
43#define PARAMS(paramlist)		paramlist
44
45/*
46 * These are the default cputype and cpusubtype for the Sparc architecture.
47 */
48const cpu_type_t md_cputype = CPU_TYPE_SPARC;
49cpu_subtype_t md_cpusubtype = CPU_SUBTYPE_SPARC_ALL;
50
51/* This is the byte sex for the Sparc architecture */
52const enum byte_sex md_target_byte_sex = BIG_ENDIAN_BYTE_SEX;
53
54/* These characters start a comment anywhere on the line */
55const char md_comment_chars[] = ";!";
56
57/* These characters only start a comment at the beginning of a line */
58const char md_line_comment_chars[] = "#";
59
60/*
61 * These characters can be used to separate mantissa decimal digits from
62 * exponent decimal digits in floating point numbers.
63 */
64const char md_EXP_CHARS[] = "eE";
65
66/*
67 * The characters after a leading 0 that means this number is a floating point
68 * constant as in 0f123.456 or 0d1.234E-12 (see md_EXP_CHARS above).
69 */
70const char md_FLT_CHARS[] = "dDfF";
71
72static void sparc_ip PARAMS ((char *));
73
74static enum sparc_architecture current_architecture = v6;
75static int architecture_requested;
76static int warn_on_bump;
77
78const relax_typeS md_relax_table[1];
79
80/* handle of the OPCODE hash table */
81static struct hash_control *op_hash = NULL;
82
83#ifdef	NeXT_MOD
84static void s_proc PARAMS ((uintptr_t));
85extern void s_seg PARAMS ((int));
86#else	/* NeXT_MOD */
87static void s_data1 PARAMS ((void));
88static void s_seg PARAMS ((int));
89static void s_proc PARAMS ((int));
90static void s_reserve PARAMS ((int));
91static void s_common PARAMS ((int));
92#endif	/* NeXT_MOD */
93
94const pseudo_typeS md_pseudo_table[] =
95{
96#ifdef	NeXT_MOD
97  {"global", s_globl, 0},	/* Maybe we should fix compiler to use globl */
98  {"proc", s_proc, 0},		/* nop??? */
99  /* These are to handle SUN assembler files and point to existing handlers */
100  {"empty", s_ignore, 0},
101  {"ident", s_ignore, 0},
102  {"optim", s_ignore, 0},
103  {"skip", s_space, 0},
104  {"type", s_ignore, 0},
105  {"word", cons, 4},
106  {"half", cons, 2},
107  /* these are custom handlers for SUN SPARC assembler only */
108
109#else	/* NeXT_MOD */
110  {"seg", s_seg, 0},
111  {"align", s_align_bytes, 0},	/* Defaulting is invalid (0) */
112  {"common", s_common, 0},
113  {"global", s_globl, 0},
114  {"half", cons, 2},
115  {"optim", s_ignore, 0},
116  {"proc", s_proc, 0},
117  {"reserve", s_reserve, 0},
118  {"seg", s_seg, 0},
119  {"skip", s_space, 0},
120  {"word", cons, 4},
121#endif	/* NeXT_MOD */
122  {NULL, 0, 0},
123};
124
125const int md_short_jump_size = 4;
126const int md_long_jump_size = 4;
127const int md_reloc_size = 12;	/* Size of relocation record */
128
129/* This array holds the chars that always start a comment.  If the
130   pre-processor is disabled, these aren't very useful */
131const char comment_chars[] = "!";	/* JF removed '|' from comment_chars */
132
133/* This array holds the chars that only start a comment at the beginning of
134   a line.  If the line seems to have the form '# 123 filename'
135   .line and .file directives will appear in the pre-processed output */
136/* Note that input_file.c hand checks for '#' at the beginning of the
137   first line of the input file.  This is because the compiler outputs
138   #NO_APP at the beginning of its output. */
139/* Also note that comments started like this one will always
140   work if '/' isn't otherwise defined. */
141const char line_comment_chars[] = "#";
142
143const char line_separator_chars[] = "";
144
145/* Chars that can be used to separate mant from exp in floating point nums */
146const char EXP_CHARS[] = "eE";
147
148/* Chars that mean this number is a floating point constant */
149/* As in 0f12.456 */
150/* or    0d1.2345e12 */
151const char FLT_CHARS[] = "rRsSfFdDxXpP";
152
153/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
154   changed in read.c.  Ideally it shouldn't have to know about it at all,
155   but nothing is ideal around here.  */
156
157static unsigned char octal[256];
158#define isoctal(c)  octal[(unsigned char) (c)]
159static unsigned char toHex[256];
160
161struct sparc_it
162  {
163    char *error;
164    uint32_t opcode;
165    nlist_t *nlistp;
166    expressionS exp;
167    int pcrel;
168    char pcrel_reloc;	/* do relocation? */
169    int reloc;
170  };
171
172struct sparc_it the_insn;
173
174#ifdef DEBUGINSN
175static void print_insn PARAMS ((struct sparc_it *insn));
176#endif
177static int getExpression PARAMS ((char *str));
178
179static char *expr_end;
180
181
182/*
183 * Indicates a 'set' instruction which may require a either
184 * of the following instructions depending on the size of the
185 * value argument:
186 *
187 * sethi %hi(value),reg
188 *
189 * or    %g0,value,reg
190 *
191 * sethi %hi(value),reg
192 * or    reg,%lo(value),reg
193 *
194 */
195static int special_case_set = 0;
196
197/* s_proc and s_ignore are included for rudimentary
198   compatibility with the Sun assembler only */
199
200static void
201s_proc (
202uintptr_t ignore)
203{
204  totally_ignore_line();
205}
206
207/* This function is called once, at assembler startup time.  It should
208   set up all the tables, etc. that the MD part of the assembler will need. */
209void
210md_begin ()
211{
212  register const char *retval = NULL;
213  int lose = 0;
214  register int i = 0;
215
216  op_hash = hash_new ();
217
218  while (i < NUMOPCODES)
219    {
220      const char *name = sparc_opcodes[i].name;
221      retval = hash_insert (op_hash, (char *)name, (char *)&sparc_opcodes[i]);
222
223      if(retval != NULL && *retval != '\0') {
224	fprintf (stderr, "internal error: can't hash `%s': %s\n",
225		 sparc_opcodes[i].name, retval);
226	lose = 1;
227      } do {
228	if (sparc_opcodes[i].match & sparc_opcodes[i].lose)
229	  {
230	    fprintf (stderr, "internal error: losing opcode: `%s' \"%s\"\n",
231		     sparc_opcodes[i].name, sparc_opcodes[i].args);
232	    lose = 1;
233	  }
234	++i;
235      }
236      while (i < NUMOPCODES
237	     && !strcmp (sparc_opcodes[i].name, name));
238    }
239
240  if (lose)
241    as_fatal ("Broken assembler.  No assembly attempted.");
242
243  for (i = '0'; i < '8'; ++i)
244    octal[i] = 1;
245  for (i = '0'; i <= '9'; ++i)
246    toHex[i] = i - '0';
247  for (i = 'a'; i <= 'f'; ++i)
248    toHex[i] = i + 10 - 'a';
249  for (i = 'A'; i <= 'F'; ++i)
250    toHex[i] = i + 10 - 'A';
251}
252
253void
254md_end(
255void)
256{
257	return;
258}
259
260void
261md_assemble (str)
262     char *str;
263{
264  char *toP;
265  int rsd;
266
267  know (str);
268  sparc_ip (str);
269
270#ifdef DEBUGINSN
271  print_insn(&the_insn);
272#endif
273
274  /* See if "set" operand is absolute and small; skip sethi if so. */
275  if (special_case_set && the_insn.exp.X_seg == SEG_ABSOLUTE)
276    {
277      if (the_insn.exp.X_add_number >= -(1 << 12)
278	  && the_insn.exp.X_add_number < (1 << 12))
279	{
280	  the_insn.opcode = 0x80102000	/* or %g0,imm,... */
281	    | (the_insn.opcode & 0x3E000000)	/* dest reg */
282	    | (the_insn.exp.X_add_number & 0x1FFF);	/* imm */
283	  special_case_set = 0;	/* No longer special */
284	  the_insn.reloc = SPARC_RELOC_NONE;	/* No longer relocated */
285	}
286    }
287
288#ifdef NeXT_MOD	/* mark sections containing instructions */
289  /*
290   * We are putting a machine instruction in this section so mark it as
291   * containg some machine instructions.
292   */
293  frchain_now->frch_section.flags |= S_ATTR_SOME_INSTRUCTIONS;
294#endif /* NeXT_MOD */
295
296  toP = frag_more (4);
297  /* put out the opcode */
298  md_number_to_chars (toP, (valueT) the_insn.opcode, 4);
299
300  /* put out the symbol-dependent stuff */
301  if (the_insn.reloc != SPARC_RELOC_NONE)
302    {
303	fix_new(frag_now,
304		(toP - frag_now->fr_literal),
305		4,
306		the_insn.exp.X_add_symbol,
307		the_insn.exp.X_subtract_symbol,
308		the_insn.exp.X_add_number,
309		the_insn.pcrel,
310		the_insn.pcrel_reloc,	/* 1 for local labels due to scatter loading */
311		the_insn.reloc);
312    }
313
314  if (special_case_set) {
315    special_case_set = 0;
316    assert (the_insn.reloc == SPARC_RELOC_HI22);
317    /* See if "set" operand has no low-order bits; skip OR if so. */
318    if ((the_insn.exp.X_seg == SEG_ABSOLUTE) &&
319	((the_insn.exp.X_add_number & 0x3FF) == 0))
320      return;
321
322    toP = frag_more (4);
323    rsd = (the_insn.opcode >> 25) & 0x1f;
324    the_insn.opcode = 0x80102000 | (rsd << 25) | (rsd << 14);
325    md_number_to_chars (toP, (valueT) the_insn.opcode, 4);
326    the_insn.pcrel_reloc = 0;
327
328    fix_new(frag_now,
329	(toP - frag_now->fr_literal),
330	4,
331	the_insn.exp.X_add_symbol,
332	the_insn.exp.X_subtract_symbol,
333	the_insn.exp.X_add_number,
334	the_insn.pcrel,
335	the_insn.pcrel_reloc,
336	SPARC_RELOC_LO10);
337    return;
338  }
339}
340
341static void
342sparc_ip (str)
343     char *str;
344{
345  char *error_message = "";
346  char *s;
347  const char *args;
348  char c;
349  struct sparc_opcode *insn;
350  char *argsStart;
351  uint32_t opcode;
352  unsigned int mask = 0;
353  int match = 0;
354  int comma = 0;
355  int32_t immediate_max = 0;
356
357  for (s = str; islower (*s) || (*s >= '0' && *s <= '3'); ++s)
358    ;
359  switch (*s)
360    {
361
362    case '\0':
363      break;
364
365    case ',':
366      comma = 1;
367
368      /*FALLTHROUGH */
369
370    case ' ':
371      *s++ = '\0';
372      break;
373
374    default:
375      as_bad ("Unknown opcode: `%s'", str);
376      exit (1);
377    }
378  if ((insn = (struct sparc_opcode *) hash_find (op_hash, str)) == NULL)
379    {
380      as_bad ("Unknown opcode: `%s'", str);
381      return;
382    }
383  if (comma)
384    {
385      *--s = ',';
386    }
387  argsStart = s;
388  for (;;)
389    {
390      opcode = insn->match;
391      memset (&the_insn, '\0', sizeof (the_insn));
392      the_insn.reloc = SPARC_RELOC_NONE;
393      the_insn.pcrel_reloc = 1; /* default, reloc, for scatter loading */
394
395      /*
396       * Build the opcode, checking as we go to make
397       * sure that the operands match
398       */
399      for (args = insn->args;; ++args)
400	{
401	  switch (*args)
402	    {
403	    case 'M':
404	    case 'm':
405	      if (strncmp (s, "%asr", 4) == 0)
406		{
407		  s += 4;
408
409		  if (isdigit (*s))
410		    {
411		      int32_t num = 0;
412
413		      while (isdigit (*s))
414			{
415			  num = num * 10 + *s - '0';
416			  ++s;
417			}
418
419		      if (num < 16 || 31 < num)
420			{
421			  error_message = ": asr number must be between 15 and 31";
422			  goto error;
423			}	/* out of range */
424
425		      opcode |= (*args == 'M' ? RS1 (num) : RD (num));
426		      continue;
427		    }
428		  else
429		    {
430		      error_message = ": expecting %asrN";
431		      goto error;
432		    }		/* if %asr followed by a number. */
433
434		}		/* if %asr */
435	      break;
436
437
438	    case '\0':		/* end of args */
439	      if (*s == '\0')
440		{
441		  match = 1;
442		}
443	      break;
444
445	    case '+':
446	      if (*s == '+')
447		{
448		  ++s;
449		  continue;
450		}
451	      if (*s == '-')
452		{
453		  continue;
454		}
455	      break;
456
457	    case '[':		/* these must match exactly */
458	    case ']':
459	    case ',':
460	    case ' ':
461	      if (*s++ == *args)
462		continue;
463	      break;
464
465	    case '#':		/* must be at least one digit */
466	      if (isdigit (*s++))
467		{
468		  while (isdigit (*s))
469		    {
470		      ++s;
471		    }
472		  continue;
473		}
474	      break;
475
476	    case 'C':		/* coprocessor state register */
477	      if (strncmp (s, "%csr", 4) == 0)
478		{
479		  s += 4;
480		  continue;
481		}
482	      break;
483
484	    case 'b':		/* next operand is a coprocessor register */
485	    case 'c':
486	    case 'D':
487	      if (*s++ == '%' && *s++ == 'c' && isdigit (*s))
488		{
489		  mask = *s++;
490		  if (isdigit (*s))
491		    {
492		      mask = 10 * (mask - '0') + (*s++ - '0');
493		      if (mask >= 32)
494			{
495			  break;
496			}
497		    }
498		  else
499		    {
500		      mask -= '0';
501		    }
502		  switch (*args)
503		    {
504
505		    case 'b':
506		      opcode |= mask << 14;
507		      continue;
508
509		    case 'c':
510		      opcode |= mask;
511		      continue;
512
513		    case 'D':
514		      opcode |= mask << 25;
515		      continue;
516		    }
517		}
518	      break;
519
520	    case 'r':		/* next operand must be a register */
521	    case 'u':
522	    case '1':
523	    case '2':
524	    case 'd':
525	      if (*s++ == '%')
526		{
527		  switch (c = *s++)
528		    {
529
530		    case 'f':	/* frame pointer */
531		      if (*s++ == 'p')
532			{
533			  mask = 0x1e;
534			  break;
535			}
536		      error_message = ": register not fp";
537		      goto error;
538
539		    case 'g':	/* global register */
540		      if (isoctal (c = *s++))
541			{
542			  mask = c - '0';
543			  break;
544			}
545		      error_message = ": invalid global register";
546		      goto error;
547
548		    case 'i':	/* in register */
549		      if (isoctal (c = *s++))
550			{
551			  mask = c - '0' + 24;
552			  break;
553			}
554		      error_message = ": invalid in register";
555		      goto error;
556
557		    case 'l':	/* local register */
558		      if (isoctal (c = *s++))
559			{
560			  mask = (c - '0' + 16);
561			  break;
562			}
563		      error_message = ": invalid local register";
564		      goto error;
565
566		    case 'o':	/* out register */
567		      if (isoctal (c = *s++))
568			{
569			  mask = (c - '0' + 8);
570			  break;
571			}
572		      error_message = ": invalid out register";
573		      goto error;
574
575		    case 's':	/* stack pointer */
576		      if (*s++ == 'p')
577			{
578			  mask = 0xe;
579			  break;
580			}
581		      error_message = ": register is not sp";
582		      goto error;
583
584		    case 'r':	/* any register */
585		      if (!isdigit (c = *s++))
586			{
587			  error_message = ": invalid register";
588			  goto error;
589			}
590		      /* FALLTHROUGH */
591		    case '0':
592		    case '1':
593		    case '2':
594		    case '3':
595		    case '4':
596		    case '5':
597		    case '6':
598		    case '7':
599		    case '8':
600		    case '9':
601		      if (isdigit (*s))
602			{
603			  if ((c = 10 * (c - '0') + (*s++ - '0')) >= 32)
604			    {
605			      error_message = ": register # out of range";
606			      goto error;
607			    }
608			}
609		      else
610			{
611			  c -= '0';
612			}
613		      mask = c;
614		      break;
615
616		    default:
617		      error_message = ": invalid resgiter #";
618		      goto error;
619		    }
620		 /*
621		 * Got the register, now figure out where
622		 * it goes in the opcode.
623		 */
624		  switch (*args)
625		    {
626
627		    case '1':
628		      opcode |= mask << 14;
629		      continue;
630
631		    case '2':
632		      opcode |= mask;
633		      continue;
634
635		    case 'd':
636		      opcode |= mask << 25;
637		      continue;
638
639		    case 'r':
640		      opcode |= (mask << 25) | (mask << 14);
641		      continue;
642
643		    case 'u':
644		      opcode |= (mask << 25) | mask;
645		      continue;
646		    }
647		}
648	      break;
649
650	    case 'e':		/* next operand is a floating point register */
651	    case 'v':
652	    case 'V':
653
654	    case 'f':
655	    case 'B':
656	    case 'R':
657
658	    case 'g':
659	    case 'H':
660	    case 'J':
661	      {
662		char format;
663
664		if (*s++ == '%'
665		    && ((format = *s) == 'f')
666		    && isdigit (*++s))
667		  {
668		    for (mask = 0; isdigit (*s); ++s)
669		      {
670			mask = 10 * mask + (*s - '0');
671		      }		/* read the number */
672
673		    if ((*args == 'v'
674			 || *args == 'B'
675			 || *args == 'H')
676			&& (mask & 1))
677		      {
678			break;
679		      }		/* register must be even numbered */
680
681		    if ((*args == 'V'
682			 || *args == 'R'
683			 || *args == 'J')
684			&& (mask & 3))
685		      {
686			break;
687		      }		/* register must be multiple of 4 */
688
689		    if (mask >= 32)
690		      {
691			error_message = ": There are only 32 f registers; [0-31]";
692			goto error;
693		      }	/* on error */
694		  }
695		else
696		  {
697		    break;
698		  }	/* if not an 'f' register. */
699
700		switch (*args)
701		  {
702
703		  case 'v':
704		  case 'V':
705		  case 'e':
706		    opcode |= RS1 (mask);
707		    continue;
708
709
710		  case 'f':
711		  case 'B':
712		  case 'R':
713		    opcode |= RS2 (mask);
714		    continue;
715
716		  case 'g':
717		  case 'H':
718		  case 'J':
719		    opcode |= RD (mask);
720		    continue;
721		  }		/* pack it in. */
722
723		know (0);
724		break;
725	      }			/* float arg */
726
727	    case 'F':
728	      if (strncmp (s, "%fsr", 4) == 0)
729		{
730		  s += 4;
731		  continue;
732		}
733	      break;
734
735	    case 'h':		/* high 22 bits */
736	      the_insn.reloc = SPARC_RELOC_HI22;
737	      goto immediate;
738
739	    case 'l':		/* 22 bit PC relative immediate */
740	      the_insn.reloc = SPARC_RELOC_WDISP22;
741	      the_insn.pcrel = 1;
742	      goto immediate;
743
744	    case 'L':		/* 30 bit immediate for call insn */
745	      the_insn.reloc = SPARC_RELOC_WDISP30;
746	      the_insn.pcrel = 1;
747	      goto immediate;
748
749	    case 'n':		/* 22 bit immediate */
750	      the_insn.reloc = SPARC_RELOC_22;
751	      goto immediate;
752
753	    case 'i':		/* 13 bit immediate */
754	      /* What's the difference between base13 and 13?
755	         13-bit immediate and 13-bit immediate+register */
756	      the_insn.reloc = SPARC_RELOC_13;
757	      immediate_max = 0x1FFF;
758
759	      /*FALLTHROUGH */
760
761	    immediate:
762	      if (*s == ' ')
763		s++;
764	      if (*s == '%')
765		{
766		  if ((c = s[1]) == 'h' && s[2] == 'i')
767		    {
768		      the_insn.reloc = SPARC_RELOC_HI22;
769		      s += 3;
770		    }
771		  else if (c == 'l' && s[2] == 'o')
772		    {
773		      the_insn.reloc = SPARC_RELOC_LO10;
774		      s += 3;
775		    }
776		  else
777		    break;
778		}
779	      /* Note that if the getExpression() fails, we will still
780		 have created U entries in the symbol table for the
781		 'symbols' in the input string.  Try not to create U
782		 symbols for registers, etc.  */
783	      {
784		/* This stuff checks to see if the expression ends in
785		   +%reg.  If it does, it removes the register from
786		   the expression, and re-sets 's' to point to the
787		   right place.  */
788
789		char *s1;
790
791		for (s1 = s; *s1 && *s1 != ',' && *s1 != ']'; s1++);
792
793		if (s1 != s && isdigit (s1[-1]))
794		  {
795		    if (s1[-2] == '%' && s1[-3] == '+')
796		      {
797			s1 -= 3;
798			*s1 = '\0';
799			(void) getExpression (s);
800			*s1 = '+';
801			s = s1;
802			continue;
803		      }
804		    else if (strchr ("goli0123456789",
805				     s1[-2]) && s1[-3] == '%' && s1[-4] == '+')
806		      {
807			s1 -= 4;
808			*s1 = '\0';
809			(void) getExpression (s);
810			*s1 = '+';
811			s = s1;
812			continue;
813		      }
814		  }
815	      }
816	      (void) getExpression (s);
817	      s = expr_end;
818	/* The Next linker has the ability to scatter blocks of sections between
819	 * labels.  This requires that branches to labels that survive to the
820	 * link phase be relocatable.  These labels are those that are not L*
821	 */
822	     if (the_insn.exp.X_add_symbol != NULL && !flagseen['L']
823		&& the_insn.exp.X_add_symbol->sy_name[0] == 'L') {
824		/* local symbol which will be thrown away.  Don't bother
825		 * to reloc it.
826		 */
827		the_insn.pcrel_reloc = 0;
828	     }
829
830	      if ((the_insn.exp.X_seg == SEG_ABSOLUTE ||
831		   the_insn.exp.X_seg == SEG_BIG)
832		  && the_insn.exp.X_add_symbol == 0)
833		{
834
835		  /* Check for invalid constant values.  Don't warn if
836		     constant was inside %hi or %lo, since these
837		     truncate the constant to fit.  */
838		  if (immediate_max != 0
839		      && the_insn.reloc != SPARC_RELOC_LO10
840		      && the_insn.reloc != SPARC_RELOC_HI22
841		      && (the_insn.exp.X_add_number > immediate_max
842			  || the_insn.exp.X_add_number < ~immediate_max))
843		    as_bad ("constant value must be between %d and %d",
844			    ~immediate_max, immediate_max);
845
846		  if ((the_insn.reloc == SPARC_RELOC_WDISP22 ||
847		      the_insn.reloc == SPARC_RELOC_WDISP30) &&
848		      the_insn.exp.X_add_number & 3)
849		    as_bad ("displacement is not long aligned");
850
851 		  /* plug absolutes directly into opcode */
852
853		  switch(the_insn.reloc) {
854		  case SPARC_RELOC_HI22:
855		    /* extract upper 22 bits from constant */
856		    opcode |= (the_insn.exp.X_add_number >> 10) & 0x3fffff;
857		    the_insn.reloc = SPARC_RELOC_NONE;
858		    break;
859		  case SPARC_RELOC_LO10:
860		    opcode |= the_insn.exp.X_add_number & 0x3ff;
861		    break;
862
863		    /* the PC relative displacements are plugged in
864		       if the argument is absolute, but retain
865		       relocatability */
866		  case SPARC_RELOC_WDISP22:
867		    opcode |= (the_insn.exp.X_add_number >> 2) & 0x3fffff;
868		    break;
869		  case SPARC_RELOC_WDISP30:
870		    opcode |= (the_insn.exp.X_add_number >> 2) & 0x3fffffff;
871		    break;
872		  default:
873		    printf("Unknown reloc entry\n");
874		  }
875		}
876
877	      /* Reset to prevent extraneous range check.  */
878	      immediate_max = 0;
879
880	      continue;
881
882	    case 'a':
883	      if (*s++ == 'a')
884		{
885		  opcode |= ANNUL;
886		  continue;
887		}
888	      break;
889
890	    case 'A':
891	      {
892		char *push = input_line_pointer;
893		expressionS e;
894
895		input_line_pointer = s;
896
897		expression (&e);
898
899		if (e.X_seg == SEG_ABSOLUTE)
900		  {
901		    opcode |= e.X_add_number << 5;
902		    s = input_line_pointer;
903		    input_line_pointer = push;
904		    continue;
905		  }		/* if absolute */
906
907		break;
908	      }			/* alternate space */
909
910	    case 'p':
911	      if (strncmp (s, "%psr", 4) == 0)
912		{
913		  s += 4;
914		  continue;
915		}
916	      break;
917
918	    case 'q':		/* floating point queue */
919	      if (strncmp (s, "%fq", 3) == 0)
920		{
921		  s += 3;
922		  continue;
923		}
924	      break;
925
926	    case 'Q':		/* coprocessor queue */
927	      if (strncmp (s, "%cq", 3) == 0)
928		{
929		  s += 3;
930		  continue;
931		}
932	      break;
933
934	    case 'S':
935	      if (strcmp (str, "set") == 0)
936		{
937		  special_case_set = 1;
938		  continue;
939		}
940	      break;
941
942
943	    case 't':
944	      if (strncmp (s, "%tbr", 4) != 0)
945		break;
946	      s += 4;
947	      continue;
948
949	    case 'w':
950	      if (strncmp (s, "%wim", 4) != 0)
951		break;
952	      s += 4;
953	      continue;
954
955	    case 'y':
956	      if (strncmp (s, "%y", 2) != 0)
957		break;
958	      s += 2;
959	      continue;
960
961	    default:
962	      as_fatal ("failed sanity check.");
963	    }			/* switch on arg code */
964	  break;
965	}			/* for each arg that we expect */
966    error:
967      if (match == 0)
968	{
969	  /* Args don't match. */
970	  if (((int) (&insn[1] - sparc_opcodes)) < NUMOPCODES
971	      && !strcmp (insn->name, insn[1].name))
972	    {
973	      ++insn;
974	      s = argsStart;
975	      continue;
976	    }
977	  else
978	    {
979	      as_bad ("Illegal operands%s", error_message);
980	      return;
981	    }
982	}
983      else
984	{
985	  if (insn->architecture > current_architecture)
986	    {
987	      if ((!architecture_requested || warn_on_bump)
988		  &&
989		  1
990		)
991		{
992		  if (warn_on_bump)
993		    {
994		      as_warn ("architecture bumped from \"%s\" to \"%s\" on \"%s\"",
995			       architecture_pname[current_architecture],
996			       architecture_pname[insn->architecture],
997			       str);
998		    }		/* if warning */
999
1000		  current_architecture = insn->architecture;
1001		}
1002	      else
1003		{
1004		  as_bad ("architecture mismatch on \"%s\" (\"%s\").  current architecture is \"%s\"",
1005			  str,
1006			  architecture_pname[insn->architecture],
1007			  architecture_pname[current_architecture]);
1008		  return;
1009		}		/* if bump ok else error */
1010	    }			/* if architecture higher */
1011	}			/* if no match */
1012
1013      break;
1014    }				/* forever looking for a match */
1015
1016  the_insn.opcode = opcode;
1017}
1018
1019static int
1020getExpression (str)
1021     char *str;
1022{
1023  char *save_in;
1024  segT seg;
1025
1026  save_in = input_line_pointer;
1027  input_line_pointer = str;
1028  seg = expression (&the_insn.exp);
1029
1030  if (seg != SEG_ABSOLUTE
1031      && seg != SEG_SECT
1032      && seg != SEG_DIFFSECT
1033      && seg != SEG_UNKNOWN
1034      && seg != SEG_NONE
1035      && seg != SEG_BIG) {
1036    the_insn.error = "bad segment";
1037    expr_end = input_line_pointer;
1038    input_line_pointer = save_in;
1039    return 1;
1040  }
1041  expr_end = input_line_pointer;
1042  input_line_pointer = save_in;
1043  return 0;
1044}				/* getExpression() */
1045
1046
1047/*
1048  This is identical to the md_atof in m68k.c.  I think this is right,
1049  but I'm not sure.
1050
1051  Turn a string in input_line_pointer into a floating point constant of type
1052  type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
1053  emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
1054  */
1055
1056/* Equal to MAX_PRECISION in atof-ieee.c */
1057#define MAX_LITTLENUMS 6
1058
1059char *
1060md_atof (type, litP, sizeP)
1061     char type;
1062     char *litP;
1063     int *sizeP;
1064{
1065  int prec;
1066  LITTLENUM_TYPE words[MAX_LITTLENUMS];
1067  LITTLENUM_TYPE *wordP;
1068  char *t;
1069  char *atof_ieee ();
1070
1071  switch (type)
1072    {
1073    case 'f':
1074    case 'F':
1075    case 's':
1076    case 'S':
1077      prec = 2;
1078      break;
1079
1080    case 'd':
1081    case 'D':
1082    case 'r':
1083    case 'R':
1084      prec = 4;
1085      break;
1086
1087    case 'x':
1088    case 'X':
1089      prec = 6;
1090      break;
1091
1092    case 'p':
1093    case 'P':
1094      prec = 6;
1095      break;
1096
1097    default:
1098      *sizeP = 0;
1099      return "Bad call to MD_ATOF()";
1100    }
1101  t = atof_ieee (input_line_pointer, type, words);
1102  if (t)
1103    input_line_pointer = t;
1104  *sizeP = prec * sizeof (LITTLENUM_TYPE);
1105  for (wordP = words; prec--;)
1106    {
1107      md_number_to_chars (litP, (valueT) (*wordP++), sizeof (LITTLENUM_TYPE));
1108      litP += sizeof (LITTLENUM_TYPE);
1109    }
1110  return "";
1111}
1112
1113/*
1114 * Write out big-endian.
1115 */
1116void
1117md_number_to_chars (buf, val, n)
1118     char *buf;
1119     signed_expr_t val;
1120     int n;
1121{
1122  // sigh, all architectures do this..,
1123  switch(n) {
1124
1125  case 4:
1126    *buf++ = val >> 24;
1127    *buf++ = val >> 16;
1128  case 2:
1129    *buf++ = val >> 8;
1130  case 1:
1131    *buf = val;
1132    break;
1133
1134  default:
1135    abort();
1136  }
1137}
1138
1139/* Apply a fixS to the frags, now that we know the value it ought to
1140   hold. */
1141
1142
1143void
1144md_number_to_imm(unsigned char *buf, signed_expr_t val, int size, fixS *fixP, int nsect)
1145{
1146
1147  /* handle the most common case quickly */
1148  if ((fixP->fx_r_type == NO_RELOC) ||
1149      (fixP->fx_r_type == SPARC_RELOC_NONE) ||
1150      (fixP->fx_r_type == SPARC_RELOC_VANILLA)) {
1151    switch(size){
1152    case 4:
1153      *buf++ = val >> 24;
1154      *buf++ = val >> 16;
1155    case 2:
1156      *buf++ = val >> 8;
1157    case 1:
1158      *buf = val;
1159      break;
1160    default:
1161      abort();
1162    }
1163    return;
1164  }
1165
1166  switch (fixP->fx_r_type) {
1167  case SPARC_RELOC_WDISP30:
1168    val = (val >> 2) + 1;	/* adjust for word displacement */
1169    buf[0] |= (val >> 24) & 0x3f;
1170    buf[1] = (val >> 16);
1171    buf[2] = val >> 8;
1172    buf[3] = val;
1173    break;
1174  case SPARC_RELOC_WDISP22:
1175    val = (val >> 2) + 1;
1176    buf[1] |= (val >> 16) & 0x3f;
1177    buf[2] = val >> 8;
1178    buf[3] = val;
1179    break;
1180  case SPARC_RELOC_HI22:
1181    buf[1] |= (val >> 26) & 0x3f;
1182    buf[2] = val >> 18;
1183    buf[3] = val >> 10;
1184    break;
1185  case SPARC_RELOC_LO10:
1186    buf[2] |= (val >> 8) & 0x03;
1187    buf[3] = val;
1188    break;
1189
1190  /* special cases that need to be handled internally by the as */
1191  case SPARC_RELOC_22:
1192    if (!fixP->fx_addsy) {
1193      if (val & ~0x003fffff) {
1194	as_bad ("relocation overflow");
1195      }			/* on overflow */
1196      buf[1] |= (val >> 16) & 0x3f;
1197      buf[2] = val >> 8;
1198      buf[3] = val & 0xff;
1199    } else
1200      as_bad ("Undefined symbolic 22-bit immediate reference: %s",
1201	      fixP->fx_addsy->sy_name);
1202    break;
1203  case SPARC_RELOC_13:
1204    if (!fixP->fx_addsy) {
1205      if (((val > 0) && (val & ~(offsetT)0x00001fff))
1206	  || ((val < 0) && (~(val - 1) & ~(offsetT)0x00001fff))) {
1207	as_bad ("relocation overflow");
1208      }
1209      buf[2] |= (val >> 8) & 0x1f;
1210      buf[3] = val;
1211    } else
1212      as_bad ("Undefined symbolic 13-bit immediate reference: %s",
1213	      fixP->fx_addsy->sy_name);
1214    break;
1215  case SPARC_RELOC_NONE:
1216  default:
1217    as_bad ("bad or unhandled relocation type: 0x%02x", fixP->fx_r_type);
1218    break;
1219  }
1220}
1221
1222
1223/*
1224 * md_parse_option
1225 *	Invocation line includes a switch not recognized by the base assembler.
1226 *	See if it's a processor-specific option.  These are:
1227 *
1228 *	-bump
1229 *		Warn on architecture bumps.  See also -A.
1230 *
1231 *	-Av6, -Av7, -Av8, -Asparclite
1232 *		Select the architecture.  Instructions or features not
1233 *		supported by the selected architecture cause fatal errors.
1234 *
1235 *		The default is to start at v6, and bump the architecture up
1236 *		whenever an instruction is seen at a higher level.
1237 *
1238 *		If -bump is specified, a warning is printing when bumping to
1239 *		higher levels.
1240 *
1241 *		If an architecture is specified, all instructions must match
1242 *		that architecture.  Any higher level instructions are flagged
1243 *		as errors.
1244 *
1245 *		if both an architecture and -bump are specified, the
1246 *		architecture starts at the specified level, but bumps are
1247 *		warnings.
1248 *
1249 */
1250
1251int
1252md_parse_option (argP, cntP, vecP)
1253     char **argP;
1254     int *cntP;
1255     char ***vecP;
1256{
1257  char *p;
1258  const char **arch;
1259
1260  if (!strcmp (*argP, "bump"))
1261    {
1262      warn_on_bump = 1;
1263    }
1264  else if (**argP == 'A')
1265    {
1266      p = (*argP) + 1;
1267
1268      for (arch = architecture_pname; *arch != NULL; ++arch)
1269	{
1270	  if (strcmp (p, *arch) == 0)
1271	    {
1272	      break;
1273	    }			/* found a match */
1274	}			/* walk the pname table */
1275
1276      if (*arch == NULL)
1277	{
1278	  as_bad ("unknown architecture: %s", p);
1279	}
1280      else
1281	{
1282	  current_architecture = (enum sparc_architecture) (arch - architecture_pname);
1283	  architecture_requested = 1;
1284	}
1285    }
1286#ifndef NeXT_MOD
1287#ifdef OBJ_ELF
1288  else if (**argP == 'V')
1289    {
1290      print_version_id ();
1291    }
1292  else if (**argP == 'Q')
1293    {
1294      /* Qy - do emit .comment
1295	 Qn - do not emit .comment */
1296    }
1297  else if (**argP == 's')
1298    {
1299      /* use .stab instead of .stab.excl */
1300    }
1301#endif
1302  else if (strcmp (*argP, "sparc") == 0)
1303    {
1304      /* Ignore -sparc, used by SunOS make default .s.o rule.  */
1305    }
1306#endif /* NeXT_MOD */
1307  else
1308    {
1309      /* Unknown option */
1310      (*argP)++;
1311      return 0;
1312    }
1313  **argP = '\0';		/* Done parsing this switch */
1314  return 1;
1315}				/* md_parse_option() */
1316
1317
1318int
1319md_estimate_size_before_relax(
1320fragS *fragP,
1321int segment_type)
1322{
1323	as_fatal("internal error: Relaxation should never occur");
1324	return(0);
1325}
1326
1327void
1328md_convert_frag(
1329fragS *fragP)
1330{
1331	as_fatal("internal error: Relaxation should never occur");
1332}
1333
1334
1335#ifdef DEBUGINSN
1336
1337char *
1338S_GET_NAME(sym)
1339     symbolS *sym;
1340{
1341  return (sym->sy_nlist.n_un.n_name);
1342}
1343
1344/* for debugging only */
1345static void
1346print_insn (insn)
1347     struct sparc_it *insn;
1348{
1349  const char *const Reloc[] = {
1350    "VANILLA",
1351    "PAIR",
1352    "HI22",
1353    "LO10",
1354    "DISP22",
1355    "PCREL",
1356    "22",
1357    "13",
1358    "SECTDIFF",
1359    "HI22_SECTDIFF",
1360    "LO10_SECTDIFF",
1361    "NONE",
1362    "UNUSED"
1363  };
1364
1365  const char *const InternalReloc[] = {
1366    "13",
1367    "22"
1368  };
1369
1370  if (insn->error)
1371    fprintf (stderr, "ERROR: %s\n", insn->error);
1372  fprintf (stderr, "opcode=0x%08x\n", (unsigned int)insn->opcode);
1373  if (insn->reloc >= 127)
1374    fprintf (stderr, "internal reloc = %s\n", InternalReloc[insn->reloc-127]);
1375  else
1376    fprintf (stderr, "reloc = %s\n", Reloc[insn->reloc]);
1377  fprintf (stderr, "X_add_number = 0x%x\n",
1378	   insn->exp.X_add_number);
1379
1380  if (insn->exp.X_add_symbol != NULL)
1381    fprintf(stderr, "Add symbol: %s\n", S_GET_NAME(insn->exp.X_add_symbol));
1382  if (insn->exp.X_subtract_symbol != NULL)
1383    fprintf(stderr, "Subtract symbol: %s\n", S_GET_NAME(insn->exp.X_subtract_symbol));
1384}
1385#endif
1386