Home | History | Annotate | Download | only in config
      1 /* ns32k.c  -- Assemble on the National Semiconductor 32k series
      2    Copyright (C) 1987-2016 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 3, 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 the Free
     18    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
     19    02110-1301, USA.  */
     20 
     21 /*#define SHOW_NUM 1*//* Uncomment for debugging.  */
     22 
     23 #include "as.h"
     24 #include "opcode/ns32k.h"
     25 
     26 #include "obstack.h"
     27 
     28 /* Macros.  */
     29 #define IIF_ENTRIES 13		/* Number of entries in iif.  */
     30 #define PRIVATE_SIZE 256	/* Size of my garbage memory.  */
     31 #define MAX_ARGS 4
     32 #define DEFAULT	-1		/* addr_mode returns this value when
     33                                    plain constant or label is
     34                                    encountered.  */
     35 
     36 #define IIF(ptr,a1,c1,e1,g1,i1,k1,m1,o1,q1,s1,u1)	\
     37     iif.iifP[ptr].type = a1;				\
     38     iif.iifP[ptr].size = c1;				\
     39     iif.iifP[ptr].object = e1;				\
     40     iif.iifP[ptr].object_adjust = g1;			\
     41     iif.iifP[ptr].pcrel = i1;				\
     42     iif.iifP[ptr].pcrel_adjust = k1;			\
     43     iif.iifP[ptr].im_disp = m1;				\
     44     iif.iifP[ptr].relax_substate = o1;			\
     45     iif.iifP[ptr].bit_fixP = q1;			\
     46     iif.iifP[ptr].addr_mode = s1;			\
     47     iif.iifP[ptr].bsr = u1;
     48 
     49 #ifdef SEQUENT_COMPATABILITY
     50 #define LINE_COMMENT_CHARS "|"
     51 #define ABSOLUTE_PREFIX '@'
     52 #define IMMEDIATE_PREFIX '#'
     53 #endif
     54 
     55 #ifndef LINE_COMMENT_CHARS
     56 #define LINE_COMMENT_CHARS "#"
     57 #endif
     58 
     59 const char comment_chars[] = "#";
     60 const char line_comment_chars[] = LINE_COMMENT_CHARS;
     61 const char line_separator_chars[] = ";";
     62 static int default_disp_size = 4; /* Displacement size for external refs.  */
     63 
     64 #if !defined(ABSOLUTE_PREFIX) && !defined(IMMEDIATE_PREFIX)
     65 #define ABSOLUTE_PREFIX '@'	/* One or the other MUST be defined.  */
     66 #endif
     67 
     68 struct addr_mode
     69 {
     70   signed char mode;		/* Addressing mode of operand (0-31).  */
     71   signed char scaled_mode;	/* Mode combined with scaled mode.  */
     72   char scaled_reg;		/* Register used in scaled+1 (1-8).  */
     73   char float_flag;		/* Set if R0..R7 was F0..F7 ie a
     74 				   floating-point-register.  */
     75   char am_size;			/* Estimated max size of general addr-mode
     76 				   parts.  */
     77   char im_disp;			/* If im_disp==1 we have a displacement.  */
     78   char pcrel;			/* 1 if pcrel, this is really redundant info.  */
     79   char disp_suffix[2];		/* Length of displacement(s), 0=undefined.  */
     80   char *disp[2];		/* Pointer(s) at displacement(s)
     81 				   or immediates(s)     (ascii).  */
     82   char index_byte;		/* Index byte.  */
     83 };
     84 typedef struct addr_mode addr_modeS;
     85 
     86 char *freeptr, *freeptr_static;	/* Points at some number of free bytes.  */
     87 struct hash_control *inst_hash_handle;
     88 
     89 struct ns32k_opcode *desc;	/* Pointer at description of instruction.  */
     90 addr_modeS addr_modeP;
     91 const char EXP_CHARS[] = "eE";
     92 const char FLT_CHARS[] = "fd";	/* We don't want to support lowercase,
     93                                    do we?  */
     94 
     95 /* UPPERCASE denotes live names when an instruction is built, IIF is
     96    used as an intermediate form to store the actual parts of the
     97    instruction. A ns32k machine instruction can be divided into a
     98    couple of sub PARTs. When an instruction is assembled the
     99    appropriate PART get an assignment. When an IIF has been completed
    100    it is converted to a FRAGment as specified in AS.H.  */
    101 
    102 /* Internal structs.  */
    103 struct ns32k_option
    104 {
    105   const char *pattern;
    106   unsigned long or;
    107   unsigned long and;
    108 };
    109 
    110 typedef struct
    111 {
    112   int type;			/* How to interpret object.  */
    113   int size;			/* Estimated max size of object.  */
    114   unsigned long object;		/* Binary data.  */
    115   int object_adjust;		/* Number added to object.  */
    116   int pcrel;			/* True if object is pcrel.  */
    117   int pcrel_adjust;		/* Length in bytes from the instruction
    118 				   start to the	displacement.  */
    119   int im_disp;			/* True if the object is a displacement.  */
    120   relax_substateT relax_substate;/*Initial relaxsubstate.  */
    121   bit_fixS *bit_fixP;		/* Pointer at bit_fix struct.  */
    122   int addr_mode;		/* What addrmode do we associate with this
    123 				   iif-entry.  */
    124   char bsr;			/* Sequent hack.  */
    125 } iif_entryT;			/* Internal Instruction Format.  */
    126 
    127 struct int_ins_form
    128 {
    129   int instr_size;		/* Max size of instruction in bytes.  */
    130   iif_entryT iifP[IIF_ENTRIES + 1];
    131 };
    132 
    133 struct int_ins_form iif;
    134 expressionS exprP;
    135 
    136 /* Description of the PARTs in IIF
    137   object[n]:
    138    0	total length in bytes of entries in iif
    139    1	opcode
    140    2	index_byte_a
    141    3	index_byte_b
    142    4	disp_a_1
    143    5	disp_a_2
    144    6	disp_b_1
    145    7	disp_b_2
    146    8	imm_a
    147    9	imm_b
    148    10	implied1
    149    11	implied2
    150 
    151    For every entry there is a datalength in bytes. This is stored in size[n].
    152   	 0,	the objectlength is not explicitly given by the instruction
    153   		and the operand is undefined. This is a case for relaxation.
    154   		Reserve 4 bytes for the final object.
    155 
    156   	 1,	the entry contains one byte
    157   	 2,	the entry contains two bytes
    158   	 3,	the entry contains three bytes
    159   	 4,	the entry contains four bytes
    160   	etc
    161 
    162    Furthermore, every entry has a data type identifier in type[n].
    163 
    164    	 0,	the entry is void, ignore it.
    165    	 1,	the entry is a binary number.
    166   	 2,	the entry is a pointer at an expression.
    167   		Where expression may be as simple as a single '1',
    168   		and as complicated as  foo-bar+12,
    169    		foo and bar may be undefined but suffixed by :{b|w|d} to
    170   		control the length of the object.
    171 
    172   	 3,	the entry is a pointer at a bignum struct
    173 
    174    The low-order-byte corresponds to low physical memory.
    175    Obviously a FRAGment must be created for each valid disp in PART whose
    176    datalength is undefined (to bad) .
    177    The case where just the expression is undefined is less severe and is
    178    handled by fix. Here the number of bytes in the objectfile is known.
    179    With this representation we simplify the assembly and separates the
    180    machine dependent/independent parts in a more clean way (said OE).  */
    181 
    182 struct ns32k_option opt1[] =		/* restore, exit.  */
    184 {
    185   {"r0", 0x80, 0xff},
    186   {"r1", 0x40, 0xff},
    187   {"r2", 0x20, 0xff},
    188   {"r3", 0x10, 0xff},
    189   {"r4", 0x08, 0xff},
    190   {"r5", 0x04, 0xff},
    191   {"r6", 0x02, 0xff},
    192   {"r7", 0x01, 0xff},
    193   {0, 0x00, 0xff}
    194 };
    195 struct ns32k_option opt2[] =		/* save, enter.  */
    196 {
    197   {"r0", 0x01, 0xff},
    198   {"r1", 0x02, 0xff},
    199   {"r2", 0x04, 0xff},
    200   {"r3", 0x08, 0xff},
    201   {"r4", 0x10, 0xff},
    202   {"r5", 0x20, 0xff},
    203   {"r6", 0x40, 0xff},
    204   {"r7", 0x80, 0xff},
    205   {0, 0x00, 0xff}
    206 };
    207 struct ns32k_option opt3[] =		/* setcfg.  */
    208 {
    209   {"c", 0x8, 0xff},
    210   {"m", 0x4, 0xff},
    211   {"f", 0x2, 0xff},
    212   {"i", 0x1, 0xff},
    213   {0, 0x0, 0xff}
    214 };
    215 struct ns32k_option opt4[] =		/* cinv.  */
    216 {
    217   {"a", 0x4, 0xff},
    218   {"i", 0x2, 0xff},
    219   {"d", 0x1, 0xff},
    220   {0, 0x0, 0xff}
    221 };
    222 struct ns32k_option opt5[] =		/* String inst.  */
    223 {
    224   {"b", 0x2, 0xff},
    225   {"u", 0xc, 0xff},
    226   {"w", 0x4, 0xff},
    227   {0, 0x0, 0xff}
    228 };
    229 struct ns32k_option opt6[] =		/* Plain reg ext,cvtp etc.  */
    230 {
    231   {"r0", 0x00, 0xff},
    232   {"r1", 0x01, 0xff},
    233   {"r2", 0x02, 0xff},
    234   {"r3", 0x03, 0xff},
    235   {"r4", 0x04, 0xff},
    236   {"r5", 0x05, 0xff},
    237   {"r6", 0x06, 0xff},
    238   {"r7", 0x07, 0xff},
    239   {0, 0x00, 0xff}
    240 };
    241 
    242 #if !defined(NS32032) && !defined(NS32532)
    243 #define NS32532
    244 #endif
    245 
    246 struct ns32k_option cpureg_532[] =	/* lpr spr.  */
    247 {
    248   {"us", 0x0, 0xff},
    249   {"dcr", 0x1, 0xff},
    250   {"bpc", 0x2, 0xff},
    251   {"dsr", 0x3, 0xff},
    252   {"car", 0x4, 0xff},
    253   {"fp", 0x8, 0xff},
    254   {"sp", 0x9, 0xff},
    255   {"sb", 0xa, 0xff},
    256   {"usp", 0xb, 0xff},
    257   {"cfg", 0xc, 0xff},
    258   {"psr", 0xd, 0xff},
    259   {"intbase", 0xe, 0xff},
    260   {"mod", 0xf, 0xff},
    261   {0, 0x00, 0xff}
    262 };
    263 struct ns32k_option mmureg_532[] =	/* lmr smr.  */
    264 {
    265   {"mcr", 0x9, 0xff},
    266   {"msr", 0xa, 0xff},
    267   {"tear", 0xb, 0xff},
    268   {"ptb0", 0xc, 0xff},
    269   {"ptb1", 0xd, 0xff},
    270   {"ivar0", 0xe, 0xff},
    271   {"ivar1", 0xf, 0xff},
    272   {0, 0x0, 0xff}
    273 };
    274 
    275 struct ns32k_option cpureg_032[] =	/* lpr spr.  */
    276 {
    277   {"upsr", 0x0, 0xff},
    278   {"fp", 0x8, 0xff},
    279   {"sp", 0x9, 0xff},
    280   {"sb", 0xa, 0xff},
    281   {"psr", 0xd, 0xff},
    282   {"intbase", 0xe, 0xff},
    283   {"mod", 0xf, 0xff},
    284   {0, 0x0, 0xff}
    285 };
    286 struct ns32k_option mmureg_032[] =	/* lmr smr.  */
    287 {
    288   {"bpr0", 0x0, 0xff},
    289   {"bpr1", 0x1, 0xff},
    290   {"pf0", 0x4, 0xff},
    291   {"pf1", 0x5, 0xff},
    292   {"sc", 0x8, 0xff},
    293   {"msr", 0xa, 0xff},
    294   {"bcnt", 0xb, 0xff},
    295   {"ptb0", 0xc, 0xff},
    296   {"ptb1", 0xd, 0xff},
    297   {"eia", 0xf, 0xff},
    298   {0, 0x0, 0xff}
    299 };
    300 
    301 #if defined(NS32532)
    302 struct ns32k_option *cpureg = cpureg_532;
    303 struct ns32k_option *mmureg = mmureg_532;
    304 #else
    305 struct ns32k_option *cpureg = cpureg_032;
    306 struct ns32k_option *mmureg = mmureg_032;
    307 #endif
    308 
    309 
    311 const pseudo_typeS md_pseudo_table[] =
    312 {					/* So far empty.  */
    313   {0, 0, 0}
    314 };
    315 
    316 #define IND(x,y)	(((x)<<2)+(y))
    317 
    318 /* Those are index's to relax groups in md_relax_table ie it must be
    319    multiplied by 4 to point at a group start. Viz IND(x,y) Se function
    320    relax_segment in write.c for more info.  */
    321 
    322 #define BRANCH		1
    323 #define PCREL		2
    324 
    325 /* Those are index's to entries in a relax group.  */
    326 
    327 #define BYTE		0
    328 #define WORD		1
    329 #define DOUBLE		2
    330 #define UNDEF           3
    331 /* Those limits are calculated from the displacement start in memory.
    332    The ns32k uses the beginning of the instruction as displacement
    333    base.  This type of displacements could be handled here by moving
    334    the limit window up or down. I choose to use an internal
    335    displacement base-adjust as there are other routines that must
    336    consider this. Also, as we have two various offset-adjusts in the
    337    ns32k (acb versus br/brs/jsr/bcond), two set of limits would have
    338    had to be used.  Now we dont have to think about that.  */
    339 
    340 const relax_typeS md_relax_table[] =
    341 {
    342   {1, 1, 0, 0},
    343   {1, 1, 0, 0},
    344   {1, 1, 0, 0},
    345   {1, 1, 0, 0},
    346 
    347   {(63), (-64), 1, IND (BRANCH, WORD)},
    348   {(8192), (-8192), 2, IND (BRANCH, DOUBLE)},
    349   {0, 0, 4, 0},
    350   {1, 1, 0, 0}
    351 };
    352 
    353 /* Array used to test if mode contains displacements.
    354    Value is true if mode contains displacement.  */
    355 
    356 char disp_test[] =
    357 {0, 0, 0, 0, 0, 0, 0, 0,
    358  1, 1, 1, 1, 1, 1, 1, 1,
    359  1, 1, 1, 0, 0, 1, 1, 0,
    360  1, 1, 1, 1, 1, 1, 1, 1};
    361 
    362 /* Array used to calculate max size of displacements.  */
    363 
    364 char disp_size[] =
    365 {4, 1, 2, 0, 4};
    366 
    367 /* Parse a general operand into an addressingmode struct
    369 
    370    In:  pointer at operand in ascii form
    371         pointer at addr_mode struct for result
    372         the level of recursion. (always 0 or 1)
    373 
    374    Out: data in addr_mode struct.  */
    375 
    376 static int
    377 addr_mode (char *operand,
    378 	   addr_modeS *addrmodeP,
    379 	   int recursive_level)
    380 {
    381   char *str;
    382   int i;
    383   int strl;
    384   int mode;
    385   int j;
    386 
    387   mode = DEFAULT;		/* Default.  */
    388   addrmodeP->scaled_mode = 0;	/* Why not.  */
    389   addrmodeP->scaled_reg = 0;	/* If 0, not scaled index.  */
    390   addrmodeP->float_flag = 0;
    391   addrmodeP->am_size = 0;
    392   addrmodeP->im_disp = 0;
    393   addrmodeP->pcrel = 0;	/* Not set in this function.  */
    394   addrmodeP->disp_suffix[0] = 0;
    395   addrmodeP->disp_suffix[1] = 0;
    396   addrmodeP->disp[0] = NULL;
    397   addrmodeP->disp[1] = NULL;
    398   str = operand;
    399 
    400   if (str[0] == 0)
    401     return 0;
    402 
    403   strl = strlen (str);
    404 
    405   switch (str[0])
    406     {
    407       /* The following three case statements controls the mode-chars
    408 	 this is the place to ed if you want to change them.  */
    409 #ifdef ABSOLUTE_PREFIX
    410     case ABSOLUTE_PREFIX:
    411       if (str[strl - 1] == ']')
    412 	break;
    413       addrmodeP->mode = 21;	/* absolute */
    414       addrmodeP->disp[0] = str + 1;
    415       return -1;
    416 #endif
    417 #ifdef IMMEDIATE_PREFIX
    418     case IMMEDIATE_PREFIX:
    419       if (str[strl - 1] == ']')
    420 	break;
    421       addrmodeP->mode = 20;	/* immediate */
    422       addrmodeP->disp[0] = str + 1;
    423       return -1;
    424 #endif
    425     case '.':
    426       if (str[strl - 1] != ']')
    427 	{
    428 	  switch (str[1])
    429 	    {
    430 	    case '-':
    431 	    case '+':
    432 	      if (str[2] != '\000')
    433 		{
    434 		  addrmodeP->mode = 27;	/* pc-relative */
    435 		  addrmodeP->disp[0] = str + 2;
    436 		  return -1;
    437 		}
    438 	    default:
    439 	      as_bad (_("Invalid syntax in PC-relative addressing mode"));
    440 	      return 0;
    441 	    }
    442 	}
    443       break;
    444     case 'e':
    445       if (str[strl - 1] != ']')
    446 	{
    447 	  if ((!strncmp (str, "ext(", 4)) && strl > 7)
    448 	    {				/* external */
    449 	      addrmodeP->disp[0] = str + 4;
    450 	      i = 0;
    451 	      j = 2;
    452 	      do
    453 		{			/* disp[0]'s termination point.  */
    454 		  j += 1;
    455 		  if (str[j] == '(')
    456 		    i++;
    457 		  if (str[j] == ')')
    458 		    i--;
    459 		}
    460 	      while (j < strl && i != 0);
    461 	      if (i != 0 || !(str[j + 1] == '-' || str[j + 1] == '+'))
    462 		{
    463 		  as_bad (_("Invalid syntax in External addressing mode"));
    464 		  return (0);
    465 		}
    466 	      str[j] = '\000';		/* null terminate disp[0] */
    467 	      addrmodeP->disp[1] = str + j + 2;
    468 	      addrmodeP->mode = 22;
    469 	      return -1;
    470 	    }
    471 	}
    472       break;
    473 
    474     default:
    475       ;
    476     }
    477 
    478   strl = strlen (str);
    479 
    480   switch (strl)
    481     {
    482     case 2:
    483       switch (str[0])
    484 	{
    485 	case 'f':
    486 	  addrmodeP->float_flag = 1;
    487 	  /* Drop through.  */
    488 	case 'r':
    489 	  if (str[1] >= '0' && str[1] < '8')
    490 	    {
    491 	      addrmodeP->mode = str[1] - '0';
    492 	      return -1;
    493 	    }
    494 	  break;
    495 	default:
    496 	  break;
    497 	}
    498       /* Drop through.  */
    499 
    500     case 3:
    501       if (!strncmp (str, "tos", 3))
    502 	{
    503 	  addrmodeP->mode = 23;	/* TopOfStack */
    504 	  return -1;
    505 	}
    506       break;
    507 
    508     default:
    509       break;
    510     }
    511 
    512   if (strl > 4)
    513     {
    514       if (str[strl - 1] == ')')
    515 	{
    516 	  if (str[strl - 2] == ')')
    517 	    {
    518 	      if (!strncmp (&str[strl - 5], "(fp", 3))
    519 		mode = 16;		/* Memory Relative.  */
    520 	      else if (!strncmp (&str[strl - 5], "(sp", 3))
    521 		mode = 17;
    522 	      else if (!strncmp (&str[strl - 5], "(sb", 3))
    523 		mode = 18;
    524 
    525 	      if (mode != DEFAULT)
    526 		{
    527 		  /* Memory relative.  */
    528 		  addrmodeP->mode = mode;
    529 		  j = strl - 5;		/* Temp for end of disp[0].  */
    530 		  i = 0;
    531 
    532 		  do
    533 		    {
    534 		      strl -= 1;
    535 		      if (str[strl] == ')')
    536 			i++;
    537 		      if (str[strl] == '(')
    538 			i--;
    539 		    }
    540 		  while (strl > -1 && i != 0);
    541 
    542 		  if (i != 0)
    543 		    {
    544 		      as_bad (_("Invalid syntax in Memory Relative addressing mode"));
    545 		      return (0);
    546 		    }
    547 
    548 		  addrmodeP->disp[1] = str;
    549 		  addrmodeP->disp[0] = str + strl + 1;
    550 		  str[j] = '\000';	/* Null terminate disp[0] .  */
    551 		  str[strl] = '\000';	/* Null terminate disp[1].  */
    552 
    553 		  return -1;
    554 		}
    555 	    }
    556 
    557 	  switch (str[strl - 3])
    558 	    {
    559 	    case 'r':
    560 	    case 'R':
    561 	      if (str[strl - 2] >= '0'
    562 		  && str[strl - 2] < '8'
    563 		  && str[strl - 4] == '(')
    564 		{
    565 		  addrmodeP->mode = str[strl - 2] - '0' + 8;
    566 		  addrmodeP->disp[0] = str;
    567 		  str[strl - 4] = 0;
    568 		  return -1;		/* reg rel */
    569 		}
    570 	      /* Drop through.  */
    571 
    572 	    default:
    573 	      if (!strncmp (&str[strl - 4], "(fp", 3))
    574 		mode = 24;
    575 	      else if (!strncmp (&str[strl - 4], "(sp", 3))
    576 		mode = 25;
    577 	      else if (!strncmp (&str[strl - 4], "(sb", 3))
    578 		mode = 26;
    579 	      else if (!strncmp (&str[strl - 4], "(pc", 3))
    580 		mode = 27;
    581 
    582 	      if (mode != DEFAULT)
    583 		{
    584 		  addrmodeP->mode = mode;
    585 		  addrmodeP->disp[0] = str;
    586 		  str[strl - 4] = '\0';
    587 
    588 		  return -1;		/* Memory space.  */
    589 		}
    590 	    }
    591 	}
    592 
    593       /* No trailing ')' do we have a ']' ?  */
    594       if (str[strl - 1] == ']')
    595 	{
    596 	  switch (str[strl - 2])
    597 	    {
    598 	    case 'b':
    599 	      mode = 28;
    600 	      break;
    601 	    case 'w':
    602 	      mode = 29;
    603 	      break;
    604 	    case 'd':
    605 	      mode = 30;
    606 	      break;
    607 	    case 'q':
    608 	      mode = 31;
    609 	      break;
    610 	    default:
    611 	      as_bad (_("Invalid scaled-indexed mode, use (b,w,d,q)"));
    612 
    613 	      if (str[strl - 3] != ':' || str[strl - 6] != '['
    614 		  || str[strl - 5] == 'r' || str[strl - 4] < '0'
    615 		  || str[strl - 4] > '7')
    616 		as_bad (_("Syntax in scaled-indexed mode, use [Rn:m] where n=[0..7] m={b,w,d,q}"));
    617 	    } /* Scaled index.  */
    618 
    619 	  if (recursive_level > 0)
    620 	    {
    621 	      as_bad (_("Scaled-indexed addressing mode combined with scaled-index"));
    622 	      return 0;
    623 	    }
    624 
    625 	  addrmodeP->am_size += 1;	/* scaled index byte.  */
    626 	  j = str[strl - 4] - '0';	/* store temporary.  */
    627 	  str[strl - 6] = '\000';	/* nullterminate for recursive call.  */
    628 	  i = addr_mode (str, addrmodeP, 1);
    629 
    630 	  if (!i || addrmodeP->mode == 20)
    631 	    {
    632 	      as_bad (_("Invalid or illegal addressing mode combined with scaled-index"));
    633 	      return 0;
    634 	    }
    635 
    636 	  addrmodeP->scaled_mode = addrmodeP->mode;	/* Store the inferior mode.  */
    637 	  addrmodeP->mode = mode;
    638 	  addrmodeP->scaled_reg = j + 1;
    639 
    640 	  return -1;
    641 	}
    642     }
    643 
    644   addrmodeP->mode = DEFAULT;	/* Default to whatever.  */
    645   addrmodeP->disp[0] = str;
    646 
    647   return -1;
    648 }
    649 
    650 static void
    652 evaluate_expr (expressionS *resultP, char *ptr)
    653 {
    654   char *tmp_line;
    655 
    656   tmp_line = input_line_pointer;
    657   input_line_pointer = ptr;
    658   expression (resultP);
    659   input_line_pointer = tmp_line;
    660 }
    661 
    662 /* ptr points at string addr_modeP points at struct with result This
    663    routine calls addr_mode to determine the general addr.mode of the
    664    operand. When this is ready it parses the displacements for size
    665    specifying suffixes and determines size of immediate mode via
    666    ns32k-opcode.  Also builds index bytes if needed.  */
    667 
    668 static int
    669 get_addr_mode (char *ptr, addr_modeS *addrmodeP)
    670 {
    671   int tmp;
    672 
    673   addr_mode (ptr, addrmodeP, 0);
    674 
    675   if (addrmodeP->mode == DEFAULT || addrmodeP->scaled_mode == -1)
    676     {
    677       /* Resolve ambiguous operands, this shouldn't be necessary if
    678 	 one uses standard NSC operand syntax. But the sequent
    679 	 compiler doesn't!!!  This finds a proper addressing mode
    680 	 if it is implicitly stated. See ns32k-opcode.h.  */
    681       (void) evaluate_expr (&exprP, ptr); /* This call takes time Sigh!  */
    682 
    683       if (addrmodeP->mode == DEFAULT)
    684 	{
    685 	  if (exprP.X_add_symbol || exprP.X_op_symbol)
    686 	    addrmodeP->mode = desc->default_model; /* We have a label.  */
    687 	  else
    688 	    addrmodeP->mode = desc->default_modec; /* We have a constant.  */
    689 	}
    690       else
    691 	{
    692 	  if (exprP.X_add_symbol || exprP.X_op_symbol)
    693 	    addrmodeP->scaled_mode = desc->default_model;
    694 	  else
    695 	    addrmodeP->scaled_mode = desc->default_modec;
    696 	}
    697 
    698       /* Must put this mess down in addr_mode to handle the scaled
    699          case better.  */
    700     }
    701 
    702   /* It appears as the sequent compiler wants an absolute when we have
    703      a label without @. Constants becomes immediates besides the addr
    704      case.  Think it does so with local labels too, not optimum, pcrel
    705      is better.  When I have time I will make gas check this and
    706      select pcrel when possible Actually that is trivial.  */
    707   if ((tmp = addrmodeP->scaled_reg))
    708     {				/* Build indexbyte.  */
    709       tmp--;			/* Remember regnumber comes incremented for
    710 				   flagpurpose.  */
    711       tmp |= addrmodeP->scaled_mode << 3;
    712       addrmodeP->index_byte = (char) tmp;
    713       addrmodeP->am_size += 1;
    714     }
    715 
    716   gas_assert (addrmodeP->mode >= 0);
    717   if (disp_test[(unsigned int) addrmodeP->mode])
    718     {
    719       char c;
    720       char suffix;
    721       char suffix_sub;
    722       int i;
    723       char *toP;
    724       char *fromP;
    725 
    726       /* There was a displacement, probe for length  specifying suffix.  */
    727       addrmodeP->pcrel = 0;
    728 
    729       gas_assert (addrmodeP->mode >= 0);
    730       if (disp_test[(unsigned int) addrmodeP->mode])
    731 	{
    732 	  /* There is a displacement.  */
    733 	  if (addrmodeP->mode == 27 || addrmodeP->scaled_mode == 27)
    734 	    /* Do we have pcrel. mode.  */
    735 	    addrmodeP->pcrel = 1;
    736 
    737 	  addrmodeP->im_disp = 1;
    738 
    739 	  for (i = 0; i < 2; i++)
    740 	    {
    741 	      suffix_sub = suffix = 0;
    742 
    743 	      if ((toP = addrmodeP->disp[i]))
    744 		{
    745 		  /* Suffix of expression, the largest size rules.  */
    746 		  fromP = toP;
    747 
    748 		  while ((c = *fromP++))
    749 		    {
    750 		      *toP++ = c;
    751 		      if (c == ':')
    752 			{
    753 			  switch (*fromP)
    754 			    {
    755 			    case '\0':
    756 			      as_warn (_("Premature end of suffix -- Defaulting to d"));
    757 			      suffix = 4;
    758 			      continue;
    759 			    case 'b':
    760 			      suffix_sub = 1;
    761 			      break;
    762 			    case 'w':
    763 			      suffix_sub = 2;
    764 			      break;
    765 			    case 'd':
    766 			      suffix_sub = 4;
    767 			      break;
    768 			    default:
    769 			      as_warn (_("Bad suffix after ':' use {b|w|d} Defaulting to d"));
    770 			      suffix = 4;
    771 			    }
    772 
    773 			  fromP ++;
    774 			  toP --;	/* So we write over the ':' */
    775 
    776 			  if (suffix < suffix_sub)
    777 			    suffix = suffix_sub;
    778 			}
    779 		    }
    780 
    781 		  *toP = '\0'; /* Terminate properly.  */
    782 		  addrmodeP->disp_suffix[i] = suffix;
    783 		  addrmodeP->am_size += suffix ? suffix : 4;
    784 		}
    785 	    }
    786 	}
    787     }
    788   else
    789     {
    790       if (addrmodeP->mode == 20)
    791 	{
    792 	  /* Look in ns32k_opcode for size.  */
    793 	  addrmodeP->disp_suffix[0] = addrmodeP->am_size = desc->im_size;
    794 	  addrmodeP->im_disp = 0;
    795 	}
    796     }
    797 
    798   return addrmodeP->mode;
    799 }
    800 
    801 /* Read an optionlist.  */
    802 
    803 static void
    804 optlist (char *str,			/* The string to extract options from.  */
    805 	 struct ns32k_option *optionP,	/* How to search the string.  */
    806 	 unsigned long *default_map)	/* Default pattern and output.  */
    807 {
    808   int i, j, k, strlen1, strlen2;
    809   const char *patternP, *strP;
    810 
    811   strlen1 = strlen (str);
    812 
    813   if (strlen1 < 1)
    814     as_fatal (_("Very short instr to option, ie you can't do it on a NULLstr"));
    815 
    816   for (i = 0; optionP[i].pattern != 0; i++)
    817     {
    818       strlen2 = strlen (optionP[i].pattern);
    819 
    820       for (j = 0; j < strlen1; j++)
    821 	{
    822 	  patternP = optionP[i].pattern;
    823 	  strP = &str[j];
    824 
    825 	  for (k = 0; k < strlen2; k++)
    826 	    {
    827 	      if (*(strP++) != *(patternP++))
    828 		break;
    829 	    }
    830 
    831 	  if (k == strlen2)
    832 	    {			/* match */
    833 	      *default_map |= optionP[i].or;
    834 	      *default_map &= optionP[i].and;
    835 	    }
    836 	}
    837     }
    838 }
    839 
    840 /* Search struct for symbols.
    841    This function is used to get the short integer form of reg names in
    842    the instructions lmr, smr, lpr, spr return true if str is found in
    843    list.  */
    844 
    845 static int
    846 list_search (char *str,				/* The string to match.  */
    847 	     struct ns32k_option *optionP,	/* List to search.  */
    848 	     unsigned long *default_map)	/* Default pattern and output.  */
    849 {
    850   int i;
    851 
    852   for (i = 0; optionP[i].pattern != 0; i++)
    853     {
    854       if (!strncmp (optionP[i].pattern, str, 20))
    855 	{
    856 	  /* Use strncmp to be safe.  */
    857 	  *default_map |= optionP[i].or;
    858 	  *default_map &= optionP[i].and;
    859 
    860 	  return -1;
    861 	}
    862     }
    863 
    864   as_bad (_("No such entry in list. (cpu/mmu register)"));
    865   return 0;
    866 }
    867 
    868 /* Create a bit_fixS in obstack 'notes'.
    870    This struct is used to profile the normal fix. If the bit_fixP is a
    871    valid pointer (not NULL) the bit_fix data will be used to format
    872    the fix.  */
    873 
    874 static bit_fixS *
    875 bit_fix_new (int size,		/* Length of bitfield.  */
    876 	     int offset,	/* Bit offset to bitfield.  */
    877 	     long min,		/* Signextended min for bitfield.  */
    878 	     long max,		/* Signextended max for bitfield.  */
    879 	     long add,		/* Add mask, used for huffman prefix.  */
    880 	     long base_type,	/* 0 or 1, if 1 it's exploded to opcode ptr.  */
    881 	     long base_adj)
    882 {
    883   bit_fixS *bit_fixP;
    884 
    885   bit_fixP = XOBNEW (&notes, bit_fixS);
    886 
    887   bit_fixP->fx_bit_size = size;
    888   bit_fixP->fx_bit_offset = offset;
    889   bit_fixP->fx_bit_base = base_type;
    890   bit_fixP->fx_bit_base_adj = base_adj;
    891   bit_fixP->fx_bit_max = max;
    892   bit_fixP->fx_bit_min = min;
    893   bit_fixP->fx_bit_add = add;
    894 
    895   return bit_fixP;
    896 }
    897 
    898 /* Convert operands to iif-format and adds bitfields to the opcode.
    899    Operands are parsed in such an order that the opcode is updated from
    900    its most significant bit, that is when the operand need to alter the
    901    opcode.
    902    Be careful not to put to objects in the same iif-slot.  */
    903 
    904 static void
    905 encode_operand (int argc,
    906 		char **argv,
    907 		const char *operandsP,
    908 		const char *suffixP,
    909 		char im_size ATTRIBUTE_UNUSED,
    910 		char opcode_bit_ptr)
    911 {
    912   int i, j;
    913   char d;
    914   int pcrel, b, loop, pcrel_adjust;
    915   unsigned long tmp;
    916 
    917   for (loop = 0; loop < argc; loop++)
    918     {
    919       /* What operand are we supposed to work on.  */
    920       i = operandsP[loop << 1] - '1';
    921       if (i > 3)
    922 	as_fatal (_("Internal consistency error.  check ns32k-opcode.h"));
    923 
    924       pcrel = 0;
    925       pcrel_adjust = 0;
    926       tmp = 0;
    927 
    928       switch ((d = operandsP[(loop << 1) + 1]))
    929 	{
    930 	case 'f':		/* Operand of sfsr turns out to be a nasty
    931 				   specialcase.  */
    932 	  opcode_bit_ptr -= 5;
    933 	case 'Z':		/* Float not immediate.  */
    934 	case 'F':		/* 32 bit float	general form.  */
    935 	case 'L':		/* 64 bit float.  */
    936 	case 'I':		/* Integer not immediate.  */
    937 	case 'B':		/* Byte	 */
    938 	case 'W':		/* Word	 */
    939 	case 'D':		/* Double-word.  */
    940 	case 'A':		/* Double-word	gen-address-form ie no regs
    941 				   allowed.  */
    942 	  get_addr_mode (argv[i], &addr_modeP);
    943 
    944 	  if ((addr_modeP.mode == 20) &&
    945 	     (d == 'I' || d == 'Z' || d == 'A'))
    946 	    as_fatal (d == 'A'? _("Address of immediate operand"):
    947 			_("Invalid immediate write operand."));
    948 
    949 	  if (opcode_bit_ptr == desc->opcode_size)
    950 	    b = 4;
    951 	  else
    952 	    b = 6;
    953 
    954 	  for (j = b; j < (b + 2); j++)
    955 	    {
    956 	      if (addr_modeP.disp[j - b])
    957 		{
    958 		  IIF (j,
    959 		       2,
    960 		       addr_modeP.disp_suffix[j - b],
    961 		       (unsigned long) addr_modeP.disp[j - b],
    962 		       0,
    963 		       addr_modeP.pcrel,
    964 		       iif.instr_size,
    965 		       addr_modeP.im_disp,
    966 		       IND (BRANCH, BYTE),
    967 		       NULL,
    968 		       (addr_modeP.scaled_reg ? addr_modeP.scaled_mode
    969 			: addr_modeP.mode),
    970 		       0);
    971 		}
    972 	    }
    973 
    974 	  opcode_bit_ptr -= 5;
    975 	  iif.iifP[1].object |= ((long) addr_modeP.mode) << opcode_bit_ptr;
    976 
    977 	  if (addr_modeP.scaled_reg)
    978 	    {
    979 	      j = b / 2;
    980 	      IIF (j, 1, 1, (unsigned long) addr_modeP.index_byte,
    981 		   0, 0, 0, 0, 0, NULL, -1, 0);
    982 	    }
    983 	  break;
    984 
    985 	case 'b':		/* Multiple instruction disp.  */
    986 	  freeptr++;		/* OVE:this is an useful hack.  */
    987 	  sprintf (freeptr, "((%s-1)*%d)", argv[i], desc->im_size);
    988 	  argv[i] = freeptr;
    989 	  pcrel -= 1;		/* Make pcrel 0 in spite of what case 'p':
    990 				   wants.  */
    991 	  /* fall thru */
    992 	case 'p':		/* Displacement - pc relative addressing.  */
    993 	  pcrel += 1;
    994 	  /* fall thru */
    995 	case 'd':		/* Displacement.  */
    996 	  iif.instr_size += suffixP[i] ? suffixP[i] : 4;
    997 	  IIF (12, 2, suffixP[i], (unsigned long) argv[i], 0,
    998 	       pcrel, pcrel_adjust, 1, IND (BRANCH, BYTE), NULL, -1, 0);
    999 	  break;
   1000 	case 'H':		/* Sequent-hack: the linker wants a bit set
   1001 				   when bsr.  */
   1002 	  pcrel = 1;
   1003 	  iif.instr_size += suffixP[i] ? suffixP[i] : 4;
   1004 	  IIF (12, 2, suffixP[i], (unsigned long) argv[i], 0,
   1005 	       pcrel, pcrel_adjust, 1, IND (BRANCH, BYTE), NULL, -1, 1);
   1006 	  break;
   1007 	case 'q':		/* quick */
   1008 	  opcode_bit_ptr -= 4;
   1009 	  IIF (11, 2, 42, (unsigned long) argv[i], 0, 0, 0, 0, 0,
   1010 	       bit_fix_new (4, opcode_bit_ptr, -8, 7, 0, 1, 0), -1, 0);
   1011 	  break;
   1012 	case 'r':		/* Register number (3 bits).  */
   1013 	  list_search (argv[i], opt6, &tmp);
   1014 	  opcode_bit_ptr -= 3;
   1015 	  iif.iifP[1].object |= tmp << opcode_bit_ptr;
   1016 	  break;
   1017 	case 'O':		/* Setcfg instruction optionslist.  */
   1018 	  optlist (argv[i], opt3, &tmp);
   1019 	  opcode_bit_ptr -= 4;
   1020 	  iif.iifP[1].object |= tmp << 15;
   1021 	  break;
   1022 	case 'C':		/* Cinv instruction optionslist.  */
   1023 	  optlist (argv[i], opt4, &tmp);
   1024 	  opcode_bit_ptr -= 4;
   1025 	  iif.iifP[1].object |= tmp << 15; /* Insert the regtype in opcode.  */
   1026 	  break;
   1027 	case 'S':		/* String instruction options list.  */
   1028 	  optlist (argv[i], opt5, &tmp);
   1029 	  opcode_bit_ptr -= 4;
   1030 	  iif.iifP[1].object |= tmp << 15;
   1031 	  break;
   1032 	case 'u':
   1033 	case 'U':		/* Register list.  */
   1034 	  IIF (10, 1, 1, 0, 0, 0, 0, 0, 0, NULL, -1, 0);
   1035 	  switch (operandsP[(i << 1) + 1])
   1036 	    {
   1037 	    case 'u':		/* Restore, exit.  */
   1038 	      optlist (argv[i], opt1, &iif.iifP[10].object);
   1039 	      break;
   1040 	    case 'U':		/* Save, enter.  */
   1041 	      optlist (argv[i], opt2, &iif.iifP[10].object);
   1042 	      break;
   1043 	    }
   1044 	  iif.instr_size += 1;
   1045 	  break;
   1046 	case 'M':		/* MMU register.  */
   1047 	  list_search (argv[i], mmureg, &tmp);
   1048 	  opcode_bit_ptr -= 4;
   1049 	  iif.iifP[1].object |= tmp << opcode_bit_ptr;
   1050 	  break;
   1051 	case 'P':		/* CPU register.  */
   1052 	  list_search (argv[i], cpureg, &tmp);
   1053 	  opcode_bit_ptr -= 4;
   1054 	  iif.iifP[1].object |= tmp << opcode_bit_ptr;
   1055 	  break;
   1056 	case 'g':		/* Inss exts.  */
   1057 	  iif.instr_size += 1;	/* 1 byte is allocated after the opcode.  */
   1058 	  IIF (10, 2, 1,
   1059 	       (unsigned long) argv[i],	/* i always 2 here.  */
   1060 	       0, 0, 0, 0, 0,
   1061 	       bit_fix_new (3, 5, 0, 7, 0, 0, 0), /* A bit_fix is targeted to
   1062 						     the byte.  */
   1063 	       -1, 0);
   1064 	  break;
   1065 	case 'G':
   1066 	  IIF (11, 2, 42,
   1067 	       (unsigned long) argv[i],	/* i always 3 here.  */
   1068 	       0, 0, 0, 0, 0,
   1069 	       bit_fix_new (5, 0, 1, 32, -1, 0, -1), -1, 0);
   1070 	  break;
   1071 	case 'i':
   1072 	  iif.instr_size += 1;
   1073 	  b = 2 + i;		/* Put the extension byte after opcode.  */
   1074 	  IIF (b, 2, 1, 0, 0, 0, 0, 0, 0, 0, -1, 0);
   1075 	  break;
   1076 	default:
   1077 	  as_fatal (_("Bad opcode-table-option, check in file ns32k-opcode.h"));
   1078 	}
   1079     }
   1080 }
   1081 
   1082 /* in:  instruction line
   1084    out: internal structure of instruction
   1085    that has been prepared for direct conversion to fragment(s) and
   1086    fixes in a systematical fashion
   1087    Return-value = recursive_level.  */
   1088 /* Build iif of one assembly text line.  */
   1089 
   1090 static int
   1091 parse (const char *line, int recursive_level)
   1092 {
   1093   const char *lineptr;
   1094   char c, suffix_separator;
   1095   int i;
   1096   unsigned int argc;
   1097   int arg_type;
   1098   char sqr, sep;
   1099   char suffix[MAX_ARGS], *argv[MAX_ARGS];	/* No more than 4 operands.  */
   1100 
   1101   if (recursive_level <= 0)
   1102     {
   1103       /* Called from md_assemble.  */
   1104       for (lineptr = line; (*lineptr) != '\0' && (*lineptr) != ' '; lineptr++)
   1105 	continue;
   1106 
   1107       c = *lineptr;
   1108       *(char *) lineptr = '\0';
   1109 
   1110       if (!(desc = (struct ns32k_opcode *) hash_find (inst_hash_handle, line)))
   1111 	as_fatal (_("No such opcode"));
   1112 
   1113       *(char *) lineptr = c;
   1114     }
   1115   else
   1116     lineptr = line;
   1117 
   1118   argc = 0;
   1119 
   1120   if (*desc->operands)
   1121     {
   1122       if (*lineptr++ != '\0')
   1123 	{
   1124 	  sqr = '[';
   1125 	  sep = ',';
   1126 
   1127 	  while (*lineptr != '\0')
   1128 	    {
   1129 	      if (desc->operands[argc << 1])
   1130 		{
   1131 		  suffix[argc] = 0;
   1132 		  arg_type = desc->operands[(argc << 1) + 1];
   1133 
   1134 		  switch (arg_type)
   1135 		    {
   1136 		    case 'd':
   1137 		    case 'b':
   1138 		    case 'p':
   1139 		    case 'H':
   1140 		      /* The operand is supposed to be a displacement.  */
   1141 		      /* Hackwarning: do not forget to update the 4
   1142                          cases above when editing ns32k-opcode.h.  */
   1143 		      suffix_separator = ':';
   1144 		      break;
   1145 		    default:
   1146 		      /* If this char occurs we loose.  */
   1147 		      suffix_separator = '\255';
   1148 		      break;
   1149 		    }
   1150 
   1151 		  suffix[argc] = 0; /* 0 when no ':' is encountered.  */
   1152 		  argv[argc] = freeptr;
   1153 		  *freeptr = '\0';
   1154 
   1155 		  while ((c = *lineptr) != '\0' && c != sep)
   1156 		    {
   1157 		      if (c == sqr)
   1158 			{
   1159 			  if (sqr == '[')
   1160 			    {
   1161 			      sqr = ']';
   1162 			      sep = '\0';
   1163 			    }
   1164 			  else
   1165 			    {
   1166 			      sqr = '[';
   1167 			      sep = ',';
   1168 			    }
   1169 			}
   1170 
   1171 		      if (c == suffix_separator)
   1172 			{
   1173 			  /* ':' - label/suffix separator.  */
   1174 			  switch (lineptr[1])
   1175 			    {
   1176 			    case 'b':
   1177 			      suffix[argc] = 1;
   1178 			      break;
   1179 			    case 'w':
   1180 			      suffix[argc] = 2;
   1181 			      break;
   1182 			    case 'd':
   1183 			      suffix[argc] = 4;
   1184 			      break;
   1185 			    default:
   1186 			      as_warn (_("Bad suffix, defaulting to d"));
   1187 			      suffix[argc] = 4;
   1188 			      if (lineptr[1] == '\0' || lineptr[1] == sep)
   1189 				{
   1190 				  lineptr += 1;
   1191 				  continue;
   1192 				}
   1193 			      break;
   1194 			    }
   1195 
   1196 			  lineptr += 2;
   1197 			  continue;
   1198 			}
   1199 
   1200 		      *freeptr++ = c;
   1201 		      lineptr++;
   1202 		    }
   1203 
   1204 		  *freeptr++ = '\0';
   1205 		  argc += 1;
   1206 
   1207 		  if (*lineptr == '\0')
   1208 		    continue;
   1209 
   1210 		  lineptr += 1;
   1211 		}
   1212 	      else
   1213 		as_fatal (_("Too many operands passed to instruction"));
   1214 	    }
   1215 	}
   1216     }
   1217 
   1218   if (argc != strlen (desc->operands) / 2)
   1219     {
   1220       if (strlen (desc->default_args))
   1221 	{
   1222 	  /* We can apply default, don't goof.  */
   1223 	  if (parse (desc->default_args, 1) != 1)
   1224 	    /* Check error in default.  */
   1225 	    as_fatal (_("Wrong numbers of operands in default, check ns32k-opcodes.h"));
   1226 	}
   1227       else
   1228 	as_fatal (_("Wrong number of operands"));
   1229     }
   1230 
   1231   for (i = 0; i < IIF_ENTRIES; i++)
   1232     /* Mark all entries as void.  */
   1233     iif.iifP[i].type = 0;
   1234 
   1235   /* Build opcode iif-entry.  */
   1236   iif.instr_size = desc->opcode_size / 8;
   1237   IIF (1, 1, iif.instr_size, desc->opcode_seed, 0, 0, 0, 0, 0, 0, -1, 0);
   1238 
   1239   /* This call encodes operands to iif format.  */
   1240   if (argc)
   1241     encode_operand (argc, argv, &desc->operands[0],
   1242 		    &suffix[0], desc->im_size, desc->opcode_size);
   1243 
   1244   return recursive_level;
   1245 }
   1246 
   1247 /* This functionality should really be in the bfd library.  */
   1249 
   1250 static bfd_reloc_code_real_type
   1251 reloc (int size, int pcrel, int type)
   1252 {
   1253   int length, rel_index;
   1254   bfd_reloc_code_real_type relocs[] =
   1255   {
   1256     BFD_RELOC_NS32K_IMM_8,
   1257     BFD_RELOC_NS32K_IMM_16,
   1258     BFD_RELOC_NS32K_IMM_32,
   1259     BFD_RELOC_NS32K_IMM_8_PCREL,
   1260     BFD_RELOC_NS32K_IMM_16_PCREL,
   1261     BFD_RELOC_NS32K_IMM_32_PCREL,
   1262 
   1263     /* ns32k displacements.  */
   1264     BFD_RELOC_NS32K_DISP_8,
   1265     BFD_RELOC_NS32K_DISP_16,
   1266     BFD_RELOC_NS32K_DISP_32,
   1267     BFD_RELOC_NS32K_DISP_8_PCREL,
   1268     BFD_RELOC_NS32K_DISP_16_PCREL,
   1269     BFD_RELOC_NS32K_DISP_32_PCREL,
   1270 
   1271     /* Normal 2's complement.  */
   1272     BFD_RELOC_8,
   1273     BFD_RELOC_16,
   1274     BFD_RELOC_32,
   1275     BFD_RELOC_8_PCREL,
   1276     BFD_RELOC_16_PCREL,
   1277     BFD_RELOC_32_PCREL
   1278   };
   1279 
   1280   switch (size)
   1281     {
   1282     case 1:
   1283       length = 0;
   1284       break;
   1285     case 2:
   1286       length = 1;
   1287       break;
   1288     case 4:
   1289       length = 2;
   1290       break;
   1291     default:
   1292       length = -1;
   1293       break;
   1294     }
   1295 
   1296   rel_index = length + 3 * pcrel + 6 * type;
   1297 
   1298   if (rel_index >= 0 && (unsigned int) rel_index < sizeof (relocs) / sizeof (relocs[0]))
   1299     return relocs[rel_index];
   1300 
   1301   if (pcrel)
   1302     as_bad (_("Can not do %d byte pc-relative relocation for storage type %d"),
   1303 	    size, type);
   1304   else
   1305     as_bad (_("Can not do %d byte relocation for storage type %d"),
   1306 	    size, type);
   1307 
   1308   return BFD_RELOC_NONE;
   1309 
   1310 }
   1311 
   1312 static void
   1313 fix_new_ns32k (fragS *frag,		/* Which frag? */
   1314 	       int where,		/* Where in that frag? */
   1315 	       int size,		/* 1, 2  or 4 usually.  */
   1316 	       symbolS *add_symbol,	/* X_add_symbol.  */
   1317 	       long offset,		/* X_add_number.  */
   1318 	       int pcrel,		/* True if PC-relative relocation.  */
   1319 	       char im_disp,		/* True if the value to write is a
   1320 					   displacement.  */
   1321 	       bit_fixS *bit_fixP,	/* Pointer at struct of bit_fix's, ignored if
   1322 					   NULL.  */
   1323 	       char bsr,		/* Sequent-linker-hack: 1 when relocobject is
   1324 					   a bsr.  */
   1325 	       fragS *opcode_frag,
   1326 	       unsigned int opcode_offset)
   1327 {
   1328   fixS *fixP = fix_new (frag, where, size, add_symbol,
   1329 			offset, pcrel,
   1330 			bit_fixP ? NO_RELOC : reloc (size, pcrel, im_disp)
   1331 			);
   1332 
   1333   fix_opcode_frag (fixP) = opcode_frag;
   1334   fix_opcode_offset (fixP) = opcode_offset;
   1335   fix_im_disp (fixP) = im_disp;
   1336   fix_bsr (fixP) = bsr;
   1337   fix_bit_fixP (fixP) = bit_fixP;
   1338   /* We have a MD overflow check for displacements.  */
   1339   fixP->fx_no_overflow = (im_disp != 0);
   1340 }
   1341 
   1342 static void
   1343 fix_new_ns32k_exp (fragS *frag,		/* Which frag? */
   1344 		   int where,		/* Where in that frag? */
   1345 		   int size,		/* 1, 2  or 4 usually.  */
   1346 		   expressionS *exp,	/* Expression.  */
   1347 		   int pcrel,		/* True if PC-relative relocation.  */
   1348 		   char im_disp,	/* True if the value to write is a
   1349 					   displacement.  */
   1350 		   bit_fixS *bit_fixP,	/* Pointer at struct of bit_fix's, ignored if
   1351 					   NULL.  */
   1352 		   char bsr,		/* Sequent-linker-hack: 1 when relocobject is
   1353 					   a bsr.  */
   1354 		   fragS *opcode_frag,
   1355 		   unsigned int opcode_offset)
   1356 {
   1357   fixS *fixP = fix_new_exp (frag, where, size, exp, pcrel,
   1358 			    bit_fixP ? NO_RELOC : reloc (size, pcrel, im_disp)
   1359 			    );
   1360 
   1361   fix_opcode_frag (fixP) = opcode_frag;
   1362   fix_opcode_offset (fixP) = opcode_offset;
   1363   fix_im_disp (fixP) = im_disp;
   1364   fix_bsr (fixP) = bsr;
   1365   fix_bit_fixP (fixP) = bit_fixP;
   1366   /* We have a MD overflow check for displacements.  */
   1367   fixP->fx_no_overflow = (im_disp != 0);
   1368 }
   1369 
   1370 /* Convert number to chars in correct order.  */
   1371 
   1372 void
   1373 md_number_to_chars (char *buf, valueT value, int nbytes)
   1374 {
   1375   number_to_chars_littleendian (buf, value, nbytes);
   1376 }
   1377 
   1378 /* This is a variant of md_numbers_to_chars. The reason for its'
   1379    existence is the fact that ns32k uses Huffman coded
   1380    displacements. This implies that the bit order is reversed in
   1381    displacements and that they are prefixed with a size-tag.
   1382 
   1383    binary: msb -> lsb
   1384    0xxxxxxx				byte
   1385    10xxxxxx xxxxxxxx			word
   1386    11xxxxxx xxxxxxxx xxxxxxxx xxxxxxxx	double word
   1387 
   1388    This must be taken care of and we do it here!  */
   1389 
   1390 static void
   1391 md_number_to_disp (char *buf, long val, int n)
   1392 {
   1393   switch (n)
   1394     {
   1395     case 1:
   1396       if (val < -64 || val > 63)
   1397 	as_bad (_("value of %ld out of byte displacement range."), val);
   1398       val &= 0x7f;
   1399 #ifdef SHOW_NUM
   1400       printf ("%x ", val & 0xff);
   1401 #endif
   1402       *buf++ = val;
   1403       break;
   1404 
   1405     case 2:
   1406       if (val < -8192 || val > 8191)
   1407 	as_bad (_("value of %ld out of word displacement range."), val);
   1408       val &= 0x3fff;
   1409       val |= 0x8000;
   1410 #ifdef SHOW_NUM
   1411       printf ("%x ", val >> 8 & 0xff);
   1412 #endif
   1413       *buf++ = (val >> 8);
   1414 #ifdef SHOW_NUM
   1415       printf ("%x ", val & 0xff);
   1416 #endif
   1417       *buf++ = val;
   1418       break;
   1419 
   1420     case 4:
   1421       if (val < -0x20000000 || val >= 0x20000000)
   1422 	as_bad (_("value of %ld out of double word displacement range."), val);
   1423       val |= 0xc0000000;
   1424 #ifdef SHOW_NUM
   1425       printf ("%x ", val >> 24 & 0xff);
   1426 #endif
   1427       *buf++ = (val >> 24);
   1428 #ifdef SHOW_NUM
   1429       printf ("%x ", val >> 16 & 0xff);
   1430 #endif
   1431       *buf++ = (val >> 16);
   1432 #ifdef SHOW_NUM
   1433       printf ("%x ", val >> 8 & 0xff);
   1434 #endif
   1435       *buf++ = (val >> 8);
   1436 #ifdef SHOW_NUM
   1437       printf ("%x ", val & 0xff);
   1438 #endif
   1439       *buf++ = val;
   1440       break;
   1441 
   1442     default:
   1443       as_fatal (_("Internal logic error.  line %d, file \"%s\""),
   1444 		__LINE__, __FILE__);
   1445     }
   1446 }
   1447 
   1448 static void
   1449 md_number_to_imm (char *buf, long val, int n)
   1450 {
   1451   switch (n)
   1452     {
   1453     case 1:
   1454 #ifdef SHOW_NUM
   1455       printf ("%x ", val & 0xff);
   1456 #endif
   1457       *buf++ = val;
   1458       break;
   1459 
   1460     case 2:
   1461 #ifdef SHOW_NUM
   1462       printf ("%x ", val >> 8 & 0xff);
   1463 #endif
   1464       *buf++ = (val >> 8);
   1465 #ifdef SHOW_NUM
   1466       printf ("%x ", val & 0xff);
   1467 #endif
   1468       *buf++ = val;
   1469       break;
   1470 
   1471     case 4:
   1472 #ifdef SHOW_NUM
   1473       printf ("%x ", val >> 24 & 0xff);
   1474 #endif
   1475       *buf++ = (val >> 24);
   1476 #ifdef SHOW_NUM
   1477       printf ("%x ", val >> 16 & 0xff);
   1478 #endif
   1479       *buf++ = (val >> 16);
   1480 #ifdef SHOW_NUM
   1481       printf ("%x ", val >> 8 & 0xff);
   1482 #endif
   1483       *buf++ = (val >> 8);
   1484 #ifdef SHOW_NUM
   1485       printf ("%x ", val & 0xff);
   1486 #endif
   1487       *buf++ = val;
   1488       break;
   1489 
   1490     default:
   1491       as_fatal (_("Internal logic error. line %d, file \"%s\""),
   1492 		__LINE__, __FILE__);
   1493     }
   1494 }
   1495 
   1496 /* Fast bitfiddling support.  */
   1497 /* Mask used to zero bitfield before oring in the true field.  */
   1498 
   1499 static unsigned long l_mask[] =
   1500 {
   1501   0xffffffff, 0xfffffffe, 0xfffffffc, 0xfffffff8,
   1502   0xfffffff0, 0xffffffe0, 0xffffffc0, 0xffffff80,
   1503   0xffffff00, 0xfffffe00, 0xfffffc00, 0xfffff800,
   1504   0xfffff000, 0xffffe000, 0xffffc000, 0xffff8000,
   1505   0xffff0000, 0xfffe0000, 0xfffc0000, 0xfff80000,
   1506   0xfff00000, 0xffe00000, 0xffc00000, 0xff800000,
   1507   0xff000000, 0xfe000000, 0xfc000000, 0xf8000000,
   1508   0xf0000000, 0xe0000000, 0xc0000000, 0x80000000,
   1509 };
   1510 static unsigned long r_mask[] =
   1511 {
   1512   0x00000000, 0x00000001, 0x00000003, 0x00000007,
   1513   0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
   1514   0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
   1515   0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
   1516   0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
   1517   0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
   1518   0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
   1519   0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
   1520 };
   1521 #define MASK_BITS 31
   1522 /* Insert bitfield described by field_ptr and val at buf
   1523    This routine is written for modification of the first 4 bytes pointed
   1524    to by buf, to yield speed.
   1525    The ifdef stuff is for selection between a ns32k-dependent routine
   1526    and a general version. (My advice: use the general version!).  */
   1527 
   1528 static void
   1529 md_number_to_field (char *buf, long val, bit_fixS *field_ptr)
   1530 {
   1531   unsigned long object;
   1532   unsigned long mask;
   1533   /* Define ENDIAN on a ns32k machine.  */
   1534 #ifdef ENDIAN
   1535   unsigned long *mem_ptr;
   1536 #else
   1537   char *mem_ptr;
   1538 #endif
   1539 
   1540   if (field_ptr->fx_bit_min <= val && val <= field_ptr->fx_bit_max)
   1541     {
   1542 #ifdef ENDIAN
   1543       if (field_ptr->fx_bit_base)
   1544 	/* Override buf.  */
   1545 	mem_ptr = (unsigned long *) field_ptr->fx_bit_base;
   1546       else
   1547 	mem_ptr = (unsigned long *) buf;
   1548 
   1549       mem_ptr = ((unsigned long *)
   1550 		 ((char *) mem_ptr + field_ptr->fx_bit_base_adj));
   1551 #else
   1552       if (field_ptr->fx_bit_base)
   1553 	mem_ptr = (char *) field_ptr->fx_bit_base;
   1554       else
   1555 	mem_ptr = buf;
   1556 
   1557       mem_ptr += field_ptr->fx_bit_base_adj;
   1558 #endif
   1559 #ifdef ENDIAN
   1560       /* We have a nice ns32k machine with lowbyte at low-physical mem.  */
   1561       object = *mem_ptr;	/* get some bytes */
   1562 #else /* OVE Goof! the machine is a m68k or dito.  */
   1563       /* That takes more byte fiddling.  */
   1564       object = 0;
   1565       object |= mem_ptr[3] & 0xff;
   1566       object <<= 8;
   1567       object |= mem_ptr[2] & 0xff;
   1568       object <<= 8;
   1569       object |= mem_ptr[1] & 0xff;
   1570       object <<= 8;
   1571       object |= mem_ptr[0] & 0xff;
   1572 #endif
   1573       mask = 0;
   1574       mask |= (r_mask[field_ptr->fx_bit_offset]);
   1575       mask |= (l_mask[field_ptr->fx_bit_offset + field_ptr->fx_bit_size]);
   1576       object &= mask;
   1577       val += field_ptr->fx_bit_add;
   1578       object |= ((val << field_ptr->fx_bit_offset) & (mask ^ 0xffffffff));
   1579 #ifdef ENDIAN
   1580       *mem_ptr = object;
   1581 #else
   1582       mem_ptr[0] = (char) object;
   1583       object >>= 8;
   1584       mem_ptr[1] = (char) object;
   1585       object >>= 8;
   1586       mem_ptr[2] = (char) object;
   1587       object >>= 8;
   1588       mem_ptr[3] = (char) object;
   1589 #endif
   1590     }
   1591   else
   1592     as_bad (_("Bit field out of range"));
   1593 }
   1594 
   1595 /* Convert iif to fragments.  From this point we start to dribble with
   1596    functions in other files than this one.(Except hash.c) So, if it's
   1597    possible to make an iif for an other CPU, you don't need to know
   1598    what frags, relax, obstacks, etc is in order to port this
   1599    assembler. You only need to know if it's possible to reduce your
   1600    cpu-instruction to iif-format (takes some work) and adopt the other
   1601    md_? parts according to given instructions Note that iif was
   1602    invented for the clean ns32k`s architecture.  */
   1603 
   1604 /* GAS for the ns32k has a problem. PC relative displacements are
   1605    relative to the address of the opcode, not the address of the
   1606    operand. We used to keep track of the offset between the operand
   1607    and the opcode in pcrel_adjust for each frag and each fix. However,
   1608    we get into trouble where there are two or more pc-relative
   1609    operands and the size of the first one can't be determined. Then in
   1610    the relax phase, the size of the first operand will change and
   1611    pcrel_adjust will no longer be correct.  The current solution is
   1612    keep a pointer to the frag with the opcode in it and the offset in
   1613    that frag for each frag and each fix. Then, when needed, we can
   1614    always figure out how far it is between the opcode and the pcrel
   1615    object.  See also md_pcrel_adjust and md_fix_pcrel_adjust.  For
   1616    objects not part of an instruction, the pointer to the opcode frag
   1617    is always zero.  */
   1618 
   1619 static void
   1620 convert_iif (void)
   1621 {
   1622   int i;
   1623   bit_fixS *j;
   1624   fragS *inst_frag;
   1625   unsigned int inst_offset;
   1626   char *inst_opcode;
   1627   char *memP;
   1628   int l;
   1629   int k;
   1630   char type;
   1631   char size = 0;
   1632 
   1633   frag_grow (iif.instr_size);	/* This is important.  */
   1634   memP = frag_more (0);
   1635   inst_opcode = memP;
   1636   inst_offset = (memP - frag_now->fr_literal);
   1637   inst_frag = frag_now;
   1638 
   1639   for (i = 0; i < IIF_ENTRIES; i++)
   1640     {
   1641       if ((type = iif.iifP[i].type))
   1642 	{
   1643 	  /* The object exist, so handle it.  */
   1644 	  switch (size = iif.iifP[i].size)
   1645 	    {
   1646 	    case 42:
   1647 	      size = 0;
   1648 	      /* It's a bitfix that operates on an existing object.  */
   1649 	      if (iif.iifP[i].bit_fixP->fx_bit_base)
   1650 		/* Expand fx_bit_base to point at opcode.  */
   1651 		iif.iifP[i].bit_fixP->fx_bit_base = (long) inst_opcode;
   1652 	      /* Fall through.  */
   1653 
   1654 	    case 8:		/* bignum or doublefloat.  */
   1655 	    case 1:
   1656 	    case 2:
   1657 	    case 3:
   1658 	    case 4:
   1659 	      /* The final size in objectmemory is known.  */
   1660 	      memP = frag_more (size);
   1661 	      j = iif.iifP[i].bit_fixP;
   1662 
   1663 	      switch (type)
   1664 		{
   1665 		case 1:	/* The object is pure binary.  */
   1666 		  if (j)
   1667 		    md_number_to_field (memP, exprP.X_add_number, j);
   1668 
   1669 		  else if (iif.iifP[i].pcrel)
   1670 		    fix_new_ns32k (frag_now,
   1671 				   (long) (memP - frag_now->fr_literal),
   1672 				   size,
   1673 				   0,
   1674 				   iif.iifP[i].object,
   1675 				   iif.iifP[i].pcrel,
   1676 				   iif.iifP[i].im_disp,
   1677 				   0,
   1678 				   iif.iifP[i].bsr,	/* Sequent hack.  */
   1679 				   inst_frag, inst_offset);
   1680 		  else
   1681 		    {
   1682 		      /* Good, just put them bytes out.  */
   1683 		      switch (iif.iifP[i].im_disp)
   1684 			{
   1685 			case 0:
   1686 			  md_number_to_chars (memP, iif.iifP[i].object, size);
   1687 			  break;
   1688 			case 1:
   1689 			  md_number_to_disp (memP, iif.iifP[i].object, size);
   1690 			  break;
   1691 			default:
   1692 			  as_fatal (_("iif convert internal pcrel/binary"));
   1693 			}
   1694 		    }
   1695 		  break;
   1696 
   1697 		case 2:
   1698 		  /* The object is a pointer at an expression, so
   1699                      unpack it, note that bignums may result from the
   1700                      expression.  */
   1701 		  evaluate_expr (&exprP, (char *) iif.iifP[i].object);
   1702 		  if (exprP.X_op == O_big || size == 8)
   1703 		    {
   1704 		      if ((k = exprP.X_add_number) > 0)
   1705 			{
   1706 			  /* We have a bignum ie a quad. This can only
   1707                              happens in a long suffixed instruction.  */
   1708 			  if (k * 2 > size)
   1709 			    as_bad (_("Bignum too big for long"));
   1710 
   1711 			  if (k == 3)
   1712 			    memP += 2;
   1713 
   1714 			  for (l = 0; k > 0; k--, l += 2)
   1715 			    md_number_to_chars (memP + l,
   1716 						generic_bignum[l >> 1],
   1717 						sizeof (LITTLENUM_TYPE));
   1718 			}
   1719 		      else
   1720 			{
   1721 			  /* flonum.  */
   1722 			  LITTLENUM_TYPE words[4];
   1723 
   1724 			  switch (size)
   1725 			    {
   1726 			    case 4:
   1727 			      gen_to_words (words, 2, 8);
   1728 			      md_number_to_imm (memP, (long) words[0],
   1729 						sizeof (LITTLENUM_TYPE));
   1730 			      md_number_to_imm (memP + sizeof (LITTLENUM_TYPE),
   1731 						(long) words[1],
   1732 						sizeof (LITTLENUM_TYPE));
   1733 			      break;
   1734 			    case 8:
   1735 			      gen_to_words (words, 4, 11);
   1736 			      md_number_to_imm (memP, (long) words[0],
   1737 						sizeof (LITTLENUM_TYPE));
   1738 			      md_number_to_imm (memP + sizeof (LITTLENUM_TYPE),
   1739 						(long) words[1],
   1740 						sizeof (LITTLENUM_TYPE));
   1741 			      md_number_to_imm ((memP + 2
   1742 						 * sizeof (LITTLENUM_TYPE)),
   1743 						(long) words[2],
   1744 						sizeof (LITTLENUM_TYPE));
   1745 			      md_number_to_imm ((memP + 3
   1746 						 * sizeof (LITTLENUM_TYPE)),
   1747 						(long) words[3],
   1748 						sizeof (LITTLENUM_TYPE));
   1749 			      break;
   1750 			    }
   1751 			}
   1752 		      break;
   1753 		    }
   1754 		  if (exprP.X_add_symbol ||
   1755 		      exprP.X_op_symbol ||
   1756 		      iif.iifP[i].pcrel)
   1757 		    {
   1758 		      /* The expression was undefined due to an
   1759                          undefined label. Create a fix so we can fix
   1760                          the object later.  */
   1761 		      exprP.X_add_number += iif.iifP[i].object_adjust;
   1762 		      fix_new_ns32k_exp (frag_now,
   1763 					 (long) (memP - frag_now->fr_literal),
   1764 					 size,
   1765 					 &exprP,
   1766 					 iif.iifP[i].pcrel,
   1767 					 iif.iifP[i].im_disp,
   1768 					 j,
   1769 					 iif.iifP[i].bsr,
   1770 					 inst_frag, inst_offset);
   1771 		    }
   1772 		  else if (j)
   1773 		    md_number_to_field (memP, exprP.X_add_number, j);
   1774 		  else
   1775 		    {
   1776 		      /* Good, just put them bytes out.  */
   1777 		      switch (iif.iifP[i].im_disp)
   1778 			{
   1779 			case 0:
   1780 			  md_number_to_imm (memP, exprP.X_add_number, size);
   1781 			  break;
   1782 			case 1:
   1783 			  md_number_to_disp (memP, exprP.X_add_number, size);
   1784 			  break;
   1785 			default:
   1786 			  as_fatal (_("iif convert internal pcrel/pointer"));
   1787 			}
   1788 		    }
   1789 		  break;
   1790 		default:
   1791 		  as_fatal (_("Internal logic error in iif.iifP[n].type"));
   1792 		}
   1793 	      break;
   1794 
   1795 	    case 0:
   1796 	      /* Too bad, the object may be undefined as far as its
   1797 		 final nsize in object memory is concerned.  The size
   1798 		 of the object in objectmemory is not explicitly
   1799 		 given.  If the object is defined its length can be
   1800 		 determined and a fix can replace the frag.  */
   1801 	      {
   1802 		evaluate_expr (&exprP, (char *) iif.iifP[i].object);
   1803 
   1804 		if ((exprP.X_add_symbol || exprP.X_op_symbol) &&
   1805 		    !iif.iifP[i].pcrel)
   1806 		  {
   1807 		    /* Size is unknown until link time so have to default.  */
   1808 		    size = default_disp_size; /* Normally 4 bytes.  */
   1809 		    memP = frag_more (size);
   1810 		    fix_new_ns32k_exp (frag_now,
   1811 				       (long) (memP - frag_now->fr_literal),
   1812 				       size,
   1813 				       &exprP,
   1814 				       0, /* never iif.iifP[i].pcrel, */
   1815 				       1, /* always iif.iifP[i].im_disp */
   1816 				       (bit_fixS *) 0, 0,
   1817 				       inst_frag,
   1818 				       inst_offset);
   1819 		    break;		/* Exit this absolute hack.  */
   1820 		  }
   1821 
   1822 		if (exprP.X_add_symbol || exprP.X_op_symbol)
   1823 		  {
   1824 		    /* Frag it.  */
   1825 		    if (exprP.X_op_symbol)
   1826 		      /* We cant relax this case.  */
   1827 		      as_fatal (_("Can't relax difference"));
   1828 		    else
   1829 		      {
   1830 			/* Size is not important.  This gets fixed by
   1831 			   relax, but we assume 0 in what follows.  */
   1832 			memP = frag_more (4); /* Max size.  */
   1833 			size = 0;
   1834 
   1835 			{
   1836 			  fragS *old_frag = frag_now;
   1837 			  frag_variant (rs_machine_dependent,
   1838 					4, /* Max size.  */
   1839 					0, /* Size.  */
   1840 					IND (BRANCH, UNDEF), /* Expecting
   1841                                                                 the worst.  */
   1842 					exprP.X_add_symbol,
   1843 					exprP.X_add_number,
   1844 					inst_opcode);
   1845 			  frag_opcode_frag (old_frag) = inst_frag;
   1846 			  frag_opcode_offset (old_frag) = inst_offset;
   1847 			  frag_bsr (old_frag) = iif.iifP[i].bsr;
   1848 			}
   1849 		      }
   1850 		  }
   1851 		else
   1852 		  {
   1853 		    /* This duplicates code in md_number_to_disp.  */
   1854 		    if (-64 <= exprP.X_add_number && exprP.X_add_number <= 63)
   1855 		      size = 1;
   1856 		    else
   1857 		      {
   1858 			if (-8192 <= exprP.X_add_number
   1859 			    && exprP.X_add_number <= 8191)
   1860 			  size = 2;
   1861 			else
   1862 			  {
   1863 			    if (-0x20000000 <= exprP.X_add_number
   1864 				&& exprP.X_add_number<=0x1fffffff)
   1865 			      size = 4;
   1866 			    else
   1867 			      {
   1868 				as_bad (_("Displacement too large for :d"));
   1869 				size = 4;
   1870 			      }
   1871 			  }
   1872 		      }
   1873 
   1874 		    memP = frag_more (size);
   1875 		    md_number_to_disp (memP, exprP.X_add_number, size);
   1876 		  }
   1877 	      }
   1878 	      break;
   1879 
   1880 	    default:
   1881 	      as_fatal (_("Internal logic error in iif.iifP[].type"));
   1882 	    }
   1883 	}
   1884     }
   1885 }
   1886 
   1887 void
   1889 md_assemble (char *line)
   1890 {
   1891   freeptr = freeptr_static;
   1892   parse (line, 0);		/* Explode line to more fix form in iif.  */
   1893   convert_iif ();		/* Convert iif to frags, fix's etc.  */
   1894 #ifdef SHOW_NUM
   1895   printf (" \t\t\t%s\n", line);
   1896 #endif
   1897 }
   1898 
   1899 void
   1900 md_begin (void)
   1901 {
   1902   /* Build a hashtable of the instructions.  */
   1903   const struct ns32k_opcode *ptr;
   1904   const char *status;
   1905   const struct ns32k_opcode *endop;
   1906 
   1907   inst_hash_handle = hash_new ();
   1908 
   1909   endop = ns32k_opcodes + sizeof (ns32k_opcodes) / sizeof (ns32k_opcodes[0]);
   1910   for (ptr = ns32k_opcodes; ptr < endop; ptr++)
   1911     {
   1912       if ((status = hash_insert (inst_hash_handle, ptr->name, (char *) ptr)))
   1913 	/* Fatal.  */
   1914 	as_fatal (_("Can't hash %s: %s"), ptr->name, status);
   1915     }
   1916 
   1917   /* Some private space please!  */
   1918   freeptr_static = XNEWVEC (char, PRIVATE_SIZE);
   1919 }
   1920 
   1921 /* Turn the string pointed to by litP into a floating point constant
   1922    of type TYPE, and emit the appropriate bytes.  The number of
   1923    LITTLENUMS emitted is stored in *SIZEP.  An error message is
   1924    returned, or NULL on OK.  */
   1925 
   1926 const char *
   1927 md_atof (int type, char *litP, int *sizeP)
   1928 {
   1929   return ieee_md_atof (type, litP, sizeP, FALSE);
   1930 }
   1931 
   1932 int
   1934 md_pcrel_adjust (fragS *fragP)
   1935 {
   1936   fragS *opcode_frag;
   1937   addressT opcode_address;
   1938   unsigned int offset;
   1939 
   1940   opcode_frag = frag_opcode_frag (fragP);
   1941   if (opcode_frag == 0)
   1942     return 0;
   1943 
   1944   offset = frag_opcode_offset (fragP);
   1945   opcode_address = offset + opcode_frag->fr_address;
   1946 
   1947   return fragP->fr_address + fragP->fr_fix - opcode_address;
   1948 }
   1949 
   1950 static int
   1951 md_fix_pcrel_adjust (fixS *fixP)
   1952 {
   1953   fragS *opcode_frag;
   1954   addressT opcode_address;
   1955   unsigned int offset;
   1956 
   1957   opcode_frag = fix_opcode_frag (fixP);
   1958   if (opcode_frag == 0)
   1959     return 0;
   1960 
   1961   offset = fix_opcode_offset (fixP);
   1962   opcode_address = offset + opcode_frag->fr_address;
   1963 
   1964   return fixP->fx_where + fixP->fx_frag->fr_address - opcode_address;
   1965 }
   1966 
   1967 /* Apply a fixS (fixup of an instruction or data that we didn't have
   1968    enough info to complete immediately) to the data in a frag.
   1969 
   1970    On the ns32k, everything is in a different format, so we have broken
   1971    out separate functions for each kind of thing we could be fixing.
   1972    They all get called from here.  */
   1973 
   1974 void
   1975 md_apply_fix (fixS *fixP, valueT * valP, segT seg ATTRIBUTE_UNUSED)
   1976 {
   1977   long val = * (long *) valP;
   1978   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
   1979 
   1980   if (fix_bit_fixP (fixP))
   1981     /* Bitfields to fix, sigh.  */
   1982     md_number_to_field (buf, val, fix_bit_fixP (fixP));
   1983   else switch (fix_im_disp (fixP))
   1984     {
   1985     case 0:
   1986       /* Immediate field.  */
   1987       md_number_to_imm (buf, val, fixP->fx_size);
   1988       break;
   1989 
   1990     case 1:
   1991       /* Displacement field.  */
   1992       /* Calculate offset.  */
   1993       md_number_to_disp (buf,
   1994 			 (fixP->fx_pcrel ? val + md_fix_pcrel_adjust (fixP)
   1995 			  : val), fixP->fx_size);
   1996       break;
   1997 
   1998     case 2:
   1999       /* Pointer in a data object.  */
   2000       md_number_to_chars (buf, val, fixP->fx_size);
   2001       break;
   2002     }
   2003 
   2004   if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
   2005     fixP->fx_done = 1;
   2006 }
   2007 
   2008 /* Convert a relaxed displacement to ditto in final output.  */
   2010 
   2011 void
   2012 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
   2013 		 segT sec ATTRIBUTE_UNUSED,
   2014 		 fragS *fragP)
   2015 {
   2016   long disp;
   2017   long ext = 0;
   2018   /* Address in gas core of the place to store the displacement.  */
   2019   char *buffer_address = fragP->fr_fix + fragP->fr_literal;
   2020   /* Address in object code of the displacement.  */
   2021   int object_address;
   2022 
   2023   switch (fragP->fr_subtype)
   2024     {
   2025     case IND (BRANCH, BYTE):
   2026       ext = 1;
   2027       break;
   2028     case IND (BRANCH, WORD):
   2029       ext = 2;
   2030       break;
   2031     case IND (BRANCH, DOUBLE):
   2032       ext = 4;
   2033       break;
   2034     }
   2035 
   2036   if (ext == 0)
   2037     return;
   2038 
   2039   know (fragP->fr_symbol);
   2040 
   2041   object_address = fragP->fr_fix + fragP->fr_address;
   2042 
   2043   /* The displacement of the address, from current location.  */
   2044   disp = (S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset) - object_address;
   2045   disp += md_pcrel_adjust (fragP);
   2046 
   2047   md_number_to_disp (buffer_address, (long) disp, (int) ext);
   2048   fragP->fr_fix += ext;
   2049 }
   2050 
   2051 /* This function returns the estimated size a variable object will occupy,
   2052    one can say that we tries to guess the size of the objects before we
   2053    actually know it.  */
   2054 
   2055 int
   2056 md_estimate_size_before_relax (fragS *fragP, segT segment)
   2057 {
   2058   if (fragP->fr_subtype == IND (BRANCH, UNDEF))
   2059     {
   2060       if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
   2061 	{
   2062 	  /* We don't relax symbols defined in another segment.  The
   2063 	     thing to do is to assume the object will occupy 4 bytes.  */
   2064 	  fix_new_ns32k (fragP,
   2065 			 (int) (fragP->fr_fix),
   2066 			 4,
   2067 			 fragP->fr_symbol,
   2068 			 fragP->fr_offset,
   2069 			 1,
   2070 			 1,
   2071 			 0,
   2072 			 frag_bsr(fragP), /* Sequent hack.  */
   2073 			 frag_opcode_frag (fragP),
   2074 			 frag_opcode_offset (fragP));
   2075 	  fragP->fr_fix += 4;
   2076 	  frag_wane (fragP);
   2077 	  return 4;
   2078 	}
   2079 
   2080       /* Relaxable case.  Set up the initial guess for the variable
   2081 	 part of the frag.  */
   2082       fragP->fr_subtype = IND (BRANCH, BYTE);
   2083     }
   2084 
   2085   if (fragP->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
   2086     abort ();
   2087 
   2088   /* Return the size of the variable part of the frag.  */
   2089   return md_relax_table[fragP->fr_subtype].rlx_length;
   2090 }
   2091 
   2092 int md_short_jump_size = 3;
   2093 int md_long_jump_size = 5;
   2094 
   2095 void
   2096 md_create_short_jump (char *ptr,
   2097 		      addressT from_addr,
   2098 		      addressT to_addr,
   2099 		      fragS *frag ATTRIBUTE_UNUSED,
   2100 		      symbolS *to_symbol ATTRIBUTE_UNUSED)
   2101 {
   2102   valueT offset;
   2103 
   2104   offset = to_addr - from_addr;
   2105   md_number_to_chars (ptr, (valueT) 0xEA, 1);
   2106   md_number_to_disp (ptr + 1, (valueT) offset, 2);
   2107 }
   2108 
   2109 void
   2110 md_create_long_jump (char *ptr,
   2111 		     addressT from_addr,
   2112 		     addressT to_addr,
   2113 		     fragS *frag ATTRIBUTE_UNUSED,
   2114 		     symbolS *to_symbol ATTRIBUTE_UNUSED)
   2115 {
   2116   valueT offset;
   2117 
   2118   offset = to_addr - from_addr;
   2119   md_number_to_chars (ptr, (valueT) 0xEA, 1);
   2120   md_number_to_disp (ptr + 1, (valueT) offset, 4);
   2121 }
   2122 
   2123 const char *md_shortopts = "m:";
   2125 
   2126 struct option md_longopts[] =
   2127 {
   2128 #define OPTION_DISP_SIZE (OPTION_MD_BASE)
   2129   {"disp-size-default", required_argument , NULL, OPTION_DISP_SIZE},
   2130   {NULL, no_argument, NULL, 0}
   2131 };
   2132 
   2133 size_t md_longopts_size = sizeof (md_longopts);
   2134 
   2135 int
   2136 md_parse_option (int c, const char *arg)
   2137 {
   2138   switch (c)
   2139     {
   2140     case 'm':
   2141       if (!strcmp (arg, "32032"))
   2142 	{
   2143 	  cpureg = cpureg_032;
   2144 	  mmureg = mmureg_032;
   2145 	}
   2146       else if (!strcmp (arg, "32532"))
   2147 	{
   2148 	  cpureg = cpureg_532;
   2149 	  mmureg = mmureg_532;
   2150 	}
   2151       else
   2152 	{
   2153 	  as_warn (_("invalid architecture option -m%s, ignored"), arg);
   2154 	  return 0;
   2155 	}
   2156       break;
   2157     case OPTION_DISP_SIZE:
   2158       {
   2159 	int size = atoi(arg);
   2160 	switch (size)
   2161 	  {
   2162 	  case 1: case 2: case 4:
   2163 	    default_disp_size = size;
   2164 	    break;
   2165 	  default:
   2166 	    as_warn (_("invalid default displacement size \"%s\". Defaulting to %d."),
   2167 		     arg, default_disp_size);
   2168 	  }
   2169 	break;
   2170       }
   2171 
   2172     default:
   2173       return 0;
   2174     }
   2175 
   2176   return 1;
   2177 }
   2178 
   2179 void
   2180 md_show_usage (FILE *stream)
   2181 {
   2182   fprintf (stream, _("\
   2183 NS32K options:\n\
   2184 -m32032 | -m32532	select variant of NS32K architecture\n\
   2185 --disp-size-default=<1|2|4>\n"));
   2186 }
   2187 
   2188 /* This is TC_CONS_FIX_NEW, called by emit_expr in read.c.  */
   2190 
   2191 void
   2192 cons_fix_new_ns32k (fragS *frag,	/* Which frag? */
   2193 		    int where,		/* Where in that frag? */
   2194 		    int size,		/* 1, 2  or 4 usually.  */
   2195 		    expressionS *exp,	/* Expression.  */
   2196 		    bfd_reloc_code_real_type r ATTRIBUTE_UNUSED)
   2197 {
   2198   fix_new_ns32k_exp (frag, where, size, exp,
   2199 		     0, 2, 0, 0, 0, 0);
   2200 }
   2201 
   2202 /* We have no need to default values of symbols.  */
   2203 
   2204 symbolS *
   2205 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
   2206 {
   2207   return 0;
   2208 }
   2209 
   2210 /* Round up a section size to the appropriate boundary.  */
   2211 
   2212 valueT
   2213 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
   2214 {
   2215   return size;			/* Byte alignment is fine.  */
   2216 }
   2217 
   2218 /* Exactly what point is a PC-relative offset relative TO?  On the
   2219    ns32k, they're relative to the start of the instruction.  */
   2220 
   2221 long
   2222 md_pcrel_from (fixS *fixP)
   2223 {
   2224   long res;
   2225 
   2226   res = fixP->fx_where + fixP->fx_frag->fr_address;
   2227 #ifdef SEQUENT_COMPATABILITY
   2228   if (frag_bsr (fixP->fx_frag))
   2229     res += 0x12			/* FOO Kludge alert!  */
   2230 #endif
   2231       return res;
   2232 }
   2233 
   2234 arelent *
   2235 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
   2236 {
   2237   arelent *rel;
   2238   bfd_reloc_code_real_type code;
   2239 
   2240   code = reloc (fixp->fx_size, fixp->fx_pcrel, fix_im_disp (fixp));
   2241 
   2242   rel = XNEW (arelent);
   2243   rel->sym_ptr_ptr = XNEW (asymbol *);
   2244   *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
   2245   rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
   2246   if (fixp->fx_pcrel)
   2247     rel->addend = fixp->fx_addnumber;
   2248   else
   2249     rel->addend = 0;
   2250 
   2251   rel->howto = bfd_reloc_type_lookup (stdoutput, code);
   2252   if (!rel->howto)
   2253     {
   2254       const char *name;
   2255 
   2256       name = S_GET_NAME (fixp->fx_addsy);
   2257       if (name == NULL)
   2258 	name = _("<unknown>");
   2259       as_fatal (_("Cannot find relocation type for symbol %s, code %d"),
   2260 		name, (int) code);
   2261     }
   2262 
   2263   return rel;
   2264 }
   2265