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