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