1 /* tc-d10v.c -- Assembler code for the Mitsubishi D10V
2    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005
3    Free Software Foundation, Inc.
4 
5    This file is part of GAS, the GNU Assembler.
6 
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11 
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to
19    the Free Software Foundation, 51 Franklin Street - Fifth Floor,
20    Boston, MA 02110-1301, USA.  */
21 
22 #include <stdio.h>
23 #include "as.h"
24 #include "safe-ctype.h"
25 #include "subsegs.h"
26 #include "opcode/d10v.h"
27 #include "elf/ppc.h"
28 
29 const char comment_chars[]        = ";";
30 const char line_comment_chars[]   = "#";
31 const char line_separator_chars[] = "";
32 const char *md_shortopts          = "O";
33 const char EXP_CHARS[]            = "eE";
34 const char FLT_CHARS[]            = "dD";
35 
36 int Optimizing = 0;
37 
38 #define AT_WORD_P(X) ((X)->X_op == O_right_shift \
39 		      && (X)->X_op_symbol != NULL \
40 		      && symbol_constant_p ((X)->X_op_symbol) \
41 		      && S_GET_VALUE ((X)->X_op_symbol) == AT_WORD_RIGHT_SHIFT)
42 #define AT_WORD_RIGHT_SHIFT 2
43 
44 /* Fixups.  */
45 #define MAX_INSN_FIXUPS  5
46 
47 struct d10v_fixup
48 {
49   expressionS exp;
50   int operand;
51   int pcrel;
52   int size;
53   bfd_reloc_code_real_type reloc;
54 };
55 
56 typedef struct _fixups
57 {
58   int fc;
59   struct d10v_fixup fix[MAX_INSN_FIXUPS];
60   struct _fixups *next;
61 } Fixups;
62 
63 static Fixups FixUps[2];
64 static Fixups *fixups;
65 
66 static int do_not_ignore_hash = 0;
67 
68 typedef int packing_type;
69 #define PACK_UNSPEC 	(0)	/* Packing order not specified.  */
70 #define PACK_PARALLEL	(1)	/* "||"  */
71 #define PACK_LEFT_RIGHT (2)	/* "->"  */
72 #define PACK_RIGHT_LEFT (3)	/* "<-"  */
73 static packing_type etype = PACK_UNSPEC; /* Used by d10v_cleanup.  */
74 
75 /* TRUE if instruction swapping warnings should be inhibited.
76    --nowarnswap.  */
77 static bfd_boolean flag_warn_suppress_instructionswap;
78 
79 /* TRUE if instruction packing should be performed when --gstabs is specified.
80    --gstabs-packing, --no-gstabs-packing.  */
81 static bfd_boolean flag_allow_gstabs_packing = 1;
82 
83 /* Local functions.  */
84 
85 enum options
86 {
87   OPTION_NOWARNSWAP = OPTION_MD_BASE,
88   OPTION_GSTABSPACKING,
89   OPTION_NOGSTABSPACKING
90 };
91 
92 struct option md_longopts[] =
93 {
94   {"nowarnswap", no_argument, NULL, OPTION_NOWARNSWAP},
95   {"gstabspacking",  no_argument, NULL, OPTION_GSTABSPACKING},
96   {"gstabs-packing", no_argument, NULL, OPTION_GSTABSPACKING},
97   {"nogstabspacking",   no_argument, NULL, OPTION_NOGSTABSPACKING},
98   {"no-gstabs-packing", no_argument, NULL, OPTION_NOGSTABSPACKING},
99   {NULL, no_argument, NULL, 0}
100 };
101 
102 size_t md_longopts_size = sizeof (md_longopts);
103 
104 /* Opcode hash table.  */
105 static struct hash_control *d10v_hash;
106 
107 /* Do a binary search of the d10v_predefined_registers array to see if
108    NAME is a valid regiter name.  Return the register number from the
109    array on success, or -1 on failure.  */
110 
111 static int
reg_name_search(char * name)112 reg_name_search (char *name)
113 {
114   int middle, low, high;
115   int cmp;
116 
117   low = 0;
118   high = d10v_reg_name_cnt () - 1;
119 
120   do
121     {
122       middle = (low + high) / 2;
123       cmp = strcasecmp (name, d10v_predefined_registers[middle].name);
124       if (cmp < 0)
125 	high = middle - 1;
126       else if (cmp > 0)
127 	low = middle + 1;
128       else
129 	return d10v_predefined_registers[middle].value;
130     }
131   while (low <= high);
132   return -1;
133 }
134 
135 /* Check the string at input_line_pointer
136    to see if it is a valid register name.  */
137 
138 static int
register_name(expressionS * expressionP)139 register_name (expressionS *expressionP)
140 {
141   int reg_number;
142   char c, *p = input_line_pointer;
143 
144   while (*p
145 	 && *p != '\n' && *p != '\r' && *p != ',' && *p != ' ' && *p != ')')
146     p++;
147 
148   c = *p;
149   if (c)
150     *p++ = 0;
151 
152   /* Look to see if it's in the register table.  */
153   reg_number = reg_name_search (input_line_pointer);
154   if (reg_number >= 0)
155     {
156       expressionP->X_op = O_register;
157       /* Temporarily store a pointer to the string here.  */
158       expressionP->X_op_symbol = (symbolS *) input_line_pointer;
159       expressionP->X_add_number = reg_number;
160       input_line_pointer = p;
161       return 1;
162     }
163   if (c)
164     *(p - 1) = c;
165   return 0;
166 }
167 
168 static int
check_range(unsigned long num,int bits,int flags)169 check_range (unsigned long num, int bits, int flags)
170 {
171   long min, max;
172   int retval = 0;
173 
174   /* Don't bother checking 16-bit values.  */
175   if (bits == 16)
176     return 0;
177 
178   if (flags & OPERAND_SHIFT)
179     {
180       /* All special shift operands are unsigned and <= 16.
181 	 We allow 0 for now.  */
182       if (num > 16)
183 	return 1;
184       else
185 	return 0;
186     }
187 
188   if (flags & OPERAND_SIGNED)
189     {
190       /* Signed 3-bit integers are restricted to the (-2, 3) range.  */
191       if (flags & RESTRICTED_NUM3)
192 	{
193 	  if ((long) num < -2 || (long) num > 3)
194 	    retval = 1;
195 	}
196       else
197 	{
198 	  max = (1 << (bits - 1)) - 1;
199 	  min = - (1 << (bits - 1));
200 	  if (((long) num > max) || ((long) num < min))
201 	    retval = 1;
202 	}
203     }
204   else
205     {
206       max = (1 << bits) - 1;
207       min = 0;
208       if (((long) num > max) || ((long) num < min))
209 	retval = 1;
210     }
211   return retval;
212 }
213 
214 void
md_show_usage(FILE * stream)215 md_show_usage (FILE *stream)
216 {
217   fprintf (stream, _("D10V options:\n\
218 -O                      Optimize.  Will do some operations in parallel.\n\
219 --gstabs-packing        Pack adjacent short instructions together even\n\
220                         when --gstabs is specified.  On by default.\n\
221 --no-gstabs-packing     If --gstabs is specified, do not pack adjacent\n\
222                         instructions together.\n"));
223 }
224 
225 int
md_parse_option(int c,char * arg ATTRIBUTE_UNUSED)226 md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
227 {
228   switch (c)
229     {
230     case 'O':
231       /* Optimize. Will attempt to parallelize operations.  */
232       Optimizing = 1;
233       break;
234     case OPTION_NOWARNSWAP:
235       flag_warn_suppress_instructionswap = 1;
236       break;
237     case OPTION_GSTABSPACKING:
238       flag_allow_gstabs_packing = 1;
239       break;
240     case OPTION_NOGSTABSPACKING:
241       flag_allow_gstabs_packing = 0;
242       break;
243     default:
244       return 0;
245     }
246   return 1;
247 }
248 
249 symbolS *
md_undefined_symbol(char * name ATTRIBUTE_UNUSED)250 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
251 {
252   return 0;
253 }
254 
255 /* Turn a string in input_line_pointer into a floating point constant
256    of type TYPE, and store the appropriate bytes in *LITP.  The number
257    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
258    returned, or NULL on OK.  */
259 
260 char *
md_atof(int type,char * litP,int * sizeP)261 md_atof (int type, char *litP, int *sizeP)
262 {
263   int prec;
264   LITTLENUM_TYPE words[4];
265   char *t;
266   int i;
267 
268   switch (type)
269     {
270     case 'f':
271       prec = 2;
272       break;
273     case 'd':
274       prec = 4;
275       break;
276     default:
277       *sizeP = 0;
278       return _("bad call to md_atof");
279     }
280 
281   t = atof_ieee (input_line_pointer, type, words);
282   if (t)
283     input_line_pointer = t;
284 
285   *sizeP = prec * 2;
286 
287   for (i = 0; i < prec; i++)
288     {
289       md_number_to_chars (litP, (valueT) words[i], 2);
290       litP += 2;
291     }
292   return NULL;
293 }
294 
295 void
md_convert_frag(bfd * abfd ATTRIBUTE_UNUSED,asection * sec ATTRIBUTE_UNUSED,fragS * fragP ATTRIBUTE_UNUSED)296 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
297 		 asection *sec ATTRIBUTE_UNUSED,
298 		 fragS *fragP ATTRIBUTE_UNUSED)
299 {
300   abort ();
301 }
302 
303 valueT
md_section_align(asection * seg,valueT addr)304 md_section_align (asection *seg, valueT addr)
305 {
306   int align = bfd_get_section_alignment (stdoutput, seg);
307   return ((addr + (1 << align) - 1) & (-1 << align));
308 }
309 
310 void
md_begin(void)311 md_begin (void)
312 {
313   char *prev_name = "";
314   struct d10v_opcode *opcode;
315   d10v_hash = hash_new ();
316 
317   /* Insert unique names into hash table.  The D10v instruction set
318      has many identical opcode names that have different opcodes based
319      on the operands.  This hash table then provides a quick index to
320      the first opcode with a particular name in the opcode table.  */
321 
322   for (opcode = (struct d10v_opcode *) d10v_opcodes; opcode->name; opcode++)
323     {
324       if (strcmp (prev_name, opcode->name))
325 	{
326 	  prev_name = (char *) opcode->name;
327 	  hash_insert (d10v_hash, opcode->name, (char *) opcode);
328 	}
329     }
330 
331   fixups = &FixUps[0];
332   FixUps[0].next = &FixUps[1];
333   FixUps[1].next = &FixUps[0];
334 }
335 
336 /* Remove the postincrement or postdecrement operator ( '+' or '-' )
337    from an expression.  */
338 
339 static int
postfix(char * p)340 postfix (char *p)
341 {
342   while (*p != '-' && *p != '+')
343     {
344       if (*p == 0 || *p == '\n' || *p == '\r')
345 	break;
346       p++;
347     }
348 
349   if (*p == '-')
350     {
351       *p = ' ';
352       return -1;
353     }
354   if (*p == '+')
355     {
356       *p = ' ';
357       return 1;
358     }
359 
360   return 0;
361 }
362 
363 static bfd_reloc_code_real_type
get_reloc(struct d10v_operand * op)364 get_reloc (struct d10v_operand *op)
365 {
366   int bits = op->bits;
367 
368   if (bits <= 4)
369     return 0;
370 
371   if (op->flags & OPERAND_ADDR)
372     {
373       if (bits == 8)
374 	return BFD_RELOC_D10V_10_PCREL_R;
375       else
376 	return BFD_RELOC_D10V_18_PCREL;
377     }
378 
379   return BFD_RELOC_16;
380 }
381 
382 /* Parse a string of operands.  Return an array of expressions.  */
383 
384 static int
get_operands(expressionS exp[])385 get_operands (expressionS exp[])
386 {
387   char *p = input_line_pointer;
388   int numops = 0;
389   int post = 0;
390   int uses_at = 0;
391 
392   while (*p)
393     {
394       while (*p == ' ' || *p == '\t' || *p == ',')
395 	p++;
396       if (*p == 0 || *p == '\n' || *p == '\r')
397 	break;
398 
399       if (*p == '@')
400 	{
401 	  uses_at = 1;
402 
403 	  p++;
404 	  exp[numops].X_op = O_absent;
405 	  if (*p == '(')
406 	    {
407 	      p++;
408 	      exp[numops].X_add_number = OPERAND_ATPAR;
409 	    }
410 	  else if (*p == '-')
411 	    {
412 	      p++;
413 	      exp[numops].X_add_number = OPERAND_ATMINUS;
414 	    }
415 	  else
416 	    {
417 	      exp[numops].X_add_number = OPERAND_ATSIGN;
418 	      if (*p == '+')
419 		{
420 		  numops++;
421 		  exp[numops].X_op = O_absent;
422 		  exp[numops].X_add_number = OPERAND_PLUS;
423 		  p++;
424 		}
425 	      post = postfix (p);
426 	    }
427 	  numops++;
428 	  continue;
429 	}
430 
431       if (*p == ')')
432 	{
433 	  /* Just skip the trailing paren.  */
434 	  p++;
435 	  continue;
436 	}
437 
438       input_line_pointer = p;
439 
440       /* Check to see if it might be a register name.  */
441       if (!register_name (&exp[numops]))
442 	{
443 	  /* Parse as an expression.  */
444 	  if (uses_at)
445 	    {
446 	      /* Any expression that involves the indirect addressing
447 		 cannot also involve immediate addressing.  Therefore
448 		 the use of the hash character is illegal.  */
449 	      int save = do_not_ignore_hash;
450 	      do_not_ignore_hash = 1;
451 
452 	      expression (&exp[numops]);
453 
454 	      do_not_ignore_hash = save;
455 	    }
456 	  else
457 	    expression (&exp[numops]);
458 	}
459 
460       if (strncasecmp (input_line_pointer, "@word", 5) == 0)
461 	{
462 	  input_line_pointer += 5;
463 	  if (exp[numops].X_op == O_register)
464 	    {
465 	      /* If it looked like a register name but was followed by
466                  "@word" then it was really a symbol, so change it to
467                  one.  */
468 	      exp[numops].X_op = O_symbol;
469 	      exp[numops].X_add_symbol =
470 		symbol_find_or_make ((char *) exp[numops].X_op_symbol);
471 	    }
472 
473 	  /* Check for identifier@word+constant.  */
474 	  if (*input_line_pointer == '-' || *input_line_pointer == '+')
475 	    {
476 	      expressionS new_exp;
477 	      expression (&new_exp);
478 	      exp[numops].X_add_number = new_exp.X_add_number;
479 	    }
480 
481 	  /* Convert expr into a right shift by AT_WORD_RIGHT_SHIFT.  */
482 	  {
483 	    expressionS new_exp;
484 	    memset (&new_exp, 0, sizeof new_exp);
485 	    new_exp.X_add_number = AT_WORD_RIGHT_SHIFT;
486 	    new_exp.X_op = O_constant;
487 	    new_exp.X_unsigned = 1;
488 	    exp[numops].X_op_symbol = make_expr_symbol (&new_exp);
489 	    exp[numops].X_op = O_right_shift;
490 	  }
491 
492 	  know (AT_WORD_P (&exp[numops]));
493 	}
494 
495       if (exp[numops].X_op == O_illegal)
496 	as_bad (_("illegal operand"));
497       else if (exp[numops].X_op == O_absent)
498 	as_bad (_("missing operand"));
499 
500       numops++;
501       p = input_line_pointer;
502     }
503 
504   switch (post)
505     {
506     case -1:	/* Postdecrement mode.  */
507       exp[numops].X_op = O_absent;
508       exp[numops++].X_add_number = OPERAND_MINUS;
509       break;
510     case 1:	/* Postincrement mode.  */
511       exp[numops].X_op = O_absent;
512       exp[numops++].X_add_number = OPERAND_PLUS;
513       break;
514     }
515 
516   exp[numops].X_op = 0;
517   return numops;
518 }
519 
520 static unsigned long
d10v_insert_operand(unsigned long insn,int op_type,offsetT value,int left,fixS * fix)521 d10v_insert_operand (unsigned long insn,
522 		     int op_type,
523 		     offsetT value,
524 		     int left,
525 		     fixS *fix)
526 {
527   int shift, bits;
528 
529   shift = d10v_operands[op_type].shift;
530   if (left)
531     shift += 15;
532 
533   bits = d10v_operands[op_type].bits;
534 
535   /* Truncate to the proper number of bits.  */
536   if (check_range (value, bits, d10v_operands[op_type].flags))
537     as_bad_where (fix->fx_file, fix->fx_line,
538 		  _("operand out of range: %ld"), (long) value);
539 
540   value &= 0x7FFFFFFF >> (31 - bits);
541   insn |= (value << shift);
542 
543   return insn;
544 }
545 
546 /* Take a pointer to the opcode entry in the opcode table and the
547    array of operand expressions.  Return the instruction.  */
548 
549 static unsigned long
build_insn(struct d10v_opcode * opcode,expressionS * opers,unsigned long insn)550 build_insn (struct d10v_opcode *opcode,
551 	    expressionS *opers,
552 	    unsigned long insn)
553 {
554   int i, bits, shift, flags, format;
555   unsigned long number;
556 
557   /* The insn argument is only used for the DIVS kludge.  */
558   if (insn)
559     format = LONG_R;
560   else
561     {
562       insn = opcode->opcode;
563       format = opcode->format;
564     }
565 
566   for (i = 0; opcode->operands[i]; i++)
567     {
568       flags = d10v_operands[opcode->operands[i]].flags;
569       bits = d10v_operands[opcode->operands[i]].bits;
570       shift = d10v_operands[opcode->operands[i]].shift;
571       number = opers[i].X_add_number;
572 
573       if (flags & OPERAND_REG)
574 	{
575 	  number &= REGISTER_MASK;
576 	  if (format == LONG_L)
577 	    shift += 15;
578 	}
579 
580       if (opers[i].X_op != O_register && opers[i].X_op != O_constant)
581 	{
582 	  /* Now create a fixup.  */
583 
584 	  if (fixups->fc >= MAX_INSN_FIXUPS)
585 	    as_fatal (_("too many fixups"));
586 
587 	  if (AT_WORD_P (&opers[i]))
588 	    {
589 	      /* Recognize XXX>>1+N aka XXX@word+N as special (AT_WORD).  */
590 	      fixups->fix[fixups->fc].reloc = BFD_RELOC_D10V_18;
591 	      opers[i].X_op = O_symbol;
592 	      opers[i].X_op_symbol = NULL; /* Should free it.  */
593 	      /* number is left shifted by AT_WORD_RIGHT_SHIFT so
594                  that, it is aligned with the symbol's value.  Later,
595                  BFD_RELOC_D10V_18 will right shift (symbol_value +
596                  X_add_number).  */
597 	      number <<= AT_WORD_RIGHT_SHIFT;
598 	      opers[i].X_add_number = number;
599 	    }
600 	  else
601 	    {
602 	      fixups->fix[fixups->fc].reloc =
603 		get_reloc ((struct d10v_operand *) &d10v_operands[opcode->operands[i]]);
604 
605 	      /* Check that an immediate was passed to ops that expect one.  */
606 	      if ((flags & OPERAND_NUM)
607 		  && (fixups->fix[fixups->fc].reloc == 0))
608 		as_bad (_("operand is not an immediate"));
609 	    }
610 
611 	  if (fixups->fix[fixups->fc].reloc == BFD_RELOC_16 ||
612 	      fixups->fix[fixups->fc].reloc == BFD_RELOC_D10V_18)
613 	    fixups->fix[fixups->fc].size = 2;
614 	  else
615 	    fixups->fix[fixups->fc].size = 4;
616 
617 	  fixups->fix[fixups->fc].exp = opers[i];
618 	  fixups->fix[fixups->fc].operand = opcode->operands[i];
619 	  fixups->fix[fixups->fc].pcrel =
620 	    (flags & OPERAND_ADDR) ? TRUE : FALSE;
621 	  (fixups->fc)++;
622 	}
623 
624       /* Truncate to the proper number of bits.  */
625       if ((opers[i].X_op == O_constant) && check_range (number, bits, flags))
626 	as_bad (_("operand out of range: %lu"), number);
627       number &= 0x7FFFFFFF >> (31 - bits);
628       insn = insn | (number << shift);
629     }
630 
631   /* kludge: for DIVS, we need to put the operands in twice on the second
632      pass, format is changed to LONG_R to force the second set of operands
633      to not be shifted over 15.  */
634   if ((opcode->opcode == OPCODE_DIVS) && (format == LONG_L))
635     insn = build_insn (opcode, opers, insn);
636 
637   return insn;
638 }
639 
640 /* Write out a long form instruction.  */
641 
642 static void
write_long(unsigned long insn,Fixups * fx)643 write_long (unsigned long insn, Fixups *fx)
644 {
645   int i, where;
646   char *f = frag_more (4);
647 
648   insn |= FM11;
649   number_to_chars_bigendian (f, insn, 4);
650 
651   for (i = 0; i < fx->fc; i++)
652     {
653       if (fx->fix[i].reloc)
654 	{
655 	  where = f - frag_now->fr_literal;
656 	  if (fx->fix[i].size == 2)
657 	    where += 2;
658 
659 	  if (fx->fix[i].reloc == BFD_RELOC_D10V_18)
660 	    fx->fix[i].operand |= 4096;
661 
662 	  fix_new_exp (frag_now,
663 		       where,
664 		       fx->fix[i].size,
665 		       &(fx->fix[i].exp),
666 		       fx->fix[i].pcrel,
667 		       fx->fix[i].operand|2048);
668 	}
669     }
670   fx->fc = 0;
671 }
672 
673 /* Write out a short form instruction by itself.  */
674 
675 static void
write_1_short(struct d10v_opcode * opcode,unsigned long insn,Fixups * fx)676 write_1_short (struct d10v_opcode *opcode,
677 	       unsigned long insn,
678 	       Fixups *fx)
679 {
680   char *f = frag_more (4);
681   int i, where;
682 
683   if (opcode->exec_type & PARONLY)
684     as_fatal (_("Instruction must be executed in parallel with another instruction."));
685 
686   /* The other container needs to be NOP.
687      According to 4.3.1: for FM=00, sub-instructions performed only by IU
688      cannot be encoded in L-container.  */
689   if (opcode->unit == IU)
690     insn |= FM00 | (NOP << 15);		/* Right container.  */
691   else
692     insn = FM00 | (insn << 15) | NOP;	/* Left container.  */
693 
694   number_to_chars_bigendian (f, insn, 4);
695   for (i = 0; i < fx->fc; i++)
696     {
697       if (fx->fix[i].reloc)
698 	{
699 	  where = f - frag_now->fr_literal;
700 	  if (fx->fix[i].size == 2)
701 	    where += 2;
702 
703 	  if (fx->fix[i].reloc == BFD_RELOC_D10V_18)
704 	    fx->fix[i].operand |= 4096;
705 
706 	  /* If it's an R reloc, we may have to switch it to L.  */
707 	  if ((fx->fix[i].reloc == BFD_RELOC_D10V_10_PCREL_R)
708 	      && (opcode->unit != IU))
709 	    fx->fix[i].operand |= 1024;
710 
711 	  fix_new_exp (frag_now,
712 		       where,
713 		       fx->fix[i].size,
714 		       &(fx->fix[i].exp),
715 		       fx->fix[i].pcrel,
716 		       fx->fix[i].operand|2048);
717 	}
718     }
719   fx->fc = 0;
720 }
721 
722 /* Determine if there are any resource conflicts among two manually
723    parallelized instructions.  Some of this was lifted from parallel_ok.  */
724 
725 static void
check_resource_conflict(struct d10v_opcode * op1,unsigned long insn1,struct d10v_opcode * op2,unsigned long insn2)726 check_resource_conflict (struct d10v_opcode *op1,
727 			 unsigned long insn1,
728 			 struct d10v_opcode *op2,
729 			 unsigned long insn2)
730 {
731   int i, j, flags, mask, shift, regno;
732   unsigned long ins, mod[2];
733   struct d10v_opcode *op;
734 
735   if ((op1->exec_type & SEQ)
736       || ! ((op1->exec_type & PAR) || (op1->exec_type & PARONLY)))
737     {
738       as_warn (_("packing conflict: %s must dispatch sequentially"),
739 	      op1->name);
740       return;
741     }
742 
743   if ((op2->exec_type & SEQ)
744       || ! ((op2->exec_type & PAR) || (op2->exec_type & PARONLY)))
745     {
746       as_warn (_("packing conflict: %s must dispatch sequentially"),
747 	      op2->name);
748       return;
749     }
750 
751    /* See if both instructions write to the same resource.
752 
753       The idea here is to create two sets of bitmasks (mod and used) which
754       indicate which registers are modified or used by each instruction.
755       The operation can only be done in parallel if neither instruction
756       modifies the same register. Accesses to control registers and memory
757       are treated as accesses to a single register. So if both instructions
758       write memory or if the first instruction writes memory and the second
759       reads, then they cannot be done in parallel. We treat reads to the PSW
760       (which includes C, F0, and F1) in isolation. So simultaneously writing
761       C and F0 in two different sub-instructions is permitted.  */
762 
763   /* The bitmasks (mod and used) look like this (bit 31 = MSB).
764      r0-r15	  0-15
765      a0-a1	  16-17
766      cr (not psw) 18
767      psw(other)   19
768      mem	  20
769      psw(C flag)  21
770      psw(F0 flag) 22  */
771 
772   for (j = 0; j < 2; j++)
773     {
774       if (j == 0)
775 	{
776 	  op = op1;
777 	  ins = insn1;
778 	}
779       else
780 	{
781 	  op = op2;
782 	  ins = insn2;
783 	}
784       mod[j] = 0;
785       if (op->exec_type & BRANCH_LINK)
786 	mod[j] |= 1 << 13;
787 
788       for (i = 0; op->operands[i]; i++)
789 	{
790 	  flags = d10v_operands[op->operands[i]].flags;
791 	  shift = d10v_operands[op->operands[i]].shift;
792 	  mask = 0x7FFFFFFF >> (31 - d10v_operands[op->operands[i]].bits);
793 	  if (flags & OPERAND_REG)
794 	    {
795 	      regno = (ins >> shift) & mask;
796 	      if (flags & (OPERAND_ACC0 | OPERAND_ACC1))
797 		regno += 16;
798 	      else if (flags & OPERAND_CONTROL)	/* mvtc or mvfc */
799 		{
800 		  if (regno == 0)
801 		    regno = 19;
802 		  else
803 		    regno = 18;
804 		}
805 	      else if (flags & OPERAND_FFLAG)
806 		regno = 22;
807 	      else if (flags & OPERAND_CFLAG)
808 		regno = 21;
809 
810 	      if (flags & OPERAND_DEST
811 		  /* Auto inc/dec also modifies the register.  */
812 		  || (op->operands[i + 1] != 0
813 		      && (d10v_operands[op->operands[i + 1]].flags
814 			  & (OPERAND_PLUS | OPERAND_MINUS)) != 0))
815 		{
816 		  mod[j] |= 1 << regno;
817 		  if (flags & OPERAND_EVEN)
818 		    mod[j] |= 1 << (regno + 1);
819 		}
820 	    }
821 	  else if (flags & OPERAND_ATMINUS)
822 	    {
823 	      /* SP implicitly used/modified.  */
824 	      mod[j] |= 1 << 15;
825 	    }
826 	}
827 
828       if (op->exec_type & WMEM)
829 	mod[j] |= 1 << 20;
830       else if (op->exec_type & WF0)
831 	mod[j] |= 1 << 22;
832       else if (op->exec_type & WCAR)
833 	mod[j] |= 1 << 21;
834     }
835 
836   if ((mod[0] & mod[1]) == 0)
837     return;
838   else
839     {
840       unsigned long x;
841       x = mod[0] & mod[1];
842 
843       for (j = 0; j <= 15; j++)
844 	if (x & (1 << j))
845 	  as_warn (_("resource conflict (R%d)"), j);
846       for (j = 16; j <= 17; j++)
847 	if (x & (1 << j))
848 	  as_warn (_("resource conflict (A%d)"), j - 16);
849       if (x & (1 << 19))
850 	as_warn (_("resource conflict (PSW)"));
851       if (x & (1 << 21))
852 	as_warn (_("resource conflict (C flag)"));
853       if (x & (1 << 22))
854 	as_warn (_("resource conflict (F flag)"));
855     }
856 }
857 
858 /* Check 2 instructions and determine if they can be safely
859    executed in parallel.  Return 1 if they can be.  */
860 
861 static int
parallel_ok(struct d10v_opcode * op1,unsigned long insn1,struct d10v_opcode * op2,unsigned long insn2,packing_type exec_type)862 parallel_ok (struct d10v_opcode *op1,
863 	     unsigned long insn1,
864 	     struct d10v_opcode *op2,
865 	     unsigned long insn2,
866 	     packing_type exec_type)
867 {
868   int i, j, flags, mask, shift, regno;
869   unsigned long ins, mod[2], used[2];
870   struct d10v_opcode *op;
871 
872   if ((op1->exec_type & SEQ) != 0 || (op2->exec_type & SEQ) != 0
873       || (op1->exec_type & PAR) == 0 || (op2->exec_type & PAR) == 0
874       || (op1->unit == BOTH) || (op2->unit == BOTH)
875       || (op1->unit == IU && op2->unit == IU)
876       || (op1->unit == MU && op2->unit == MU))
877     return 0;
878 
879   /* If this is auto parallelization, and the first instruction is a
880      branch or should not be packed, then don't parallelize.  */
881   if (exec_type == PACK_UNSPEC
882       && (op1->exec_type & (ALONE | BRANCH)))
883     return 0;
884 
885   /* The idea here is to create two sets of bitmasks (mod and used)
886      which indicate which registers are modified or used by each
887      instruction.  The operation can only be done in parallel if
888      instruction 1 and instruction 2 modify different registers, and
889      the first instruction does not modify registers that the second
890      is using (The second instruction can modify registers that the
891      first is using as they are only written back after the first
892      instruction has completed).  Accesses to control registers, PSW,
893      and memory are treated as accesses to a single register.  So if
894      both instructions write memory or if the first instruction writes
895      memory and the second reads, then they cannot be done in
896      parallel.  Likewise, if the first instruction mucks with the psw
897      and the second reads the PSW (which includes C, F0, and F1), then
898      they cannot operate safely in parallel.  */
899 
900   /* The bitmasks (mod and used) look like this (bit 31 = MSB).
901      r0-r15	  0-15
902      a0-a1	  16-17
903      cr (not psw) 18
904      psw	  19
905      mem	  20  */
906 
907   for (j = 0; j < 2; j++)
908     {
909       if (j == 0)
910 	{
911 	  op = op1;
912 	  ins = insn1;
913 	}
914       else
915 	{
916 	  op = op2;
917 	  ins = insn2;
918 	}
919       mod[j] = used[j] = 0;
920       if (op->exec_type & BRANCH_LINK)
921 	mod[j] |= 1 << 13;
922 
923       for (i = 0; op->operands[i]; i++)
924 	{
925 	  flags = d10v_operands[op->operands[i]].flags;
926 	  shift = d10v_operands[op->operands[i]].shift;
927 	  mask = 0x7FFFFFFF >> (31 - d10v_operands[op->operands[i]].bits);
928 	  if (flags & OPERAND_REG)
929 	    {
930 	      regno = (ins >> shift) & mask;
931 	      if (flags & (OPERAND_ACC0 | OPERAND_ACC1))
932 		regno += 16;
933 	      else if (flags & OPERAND_CONTROL)	/* mvtc or mvfc.  */
934 		{
935 		  if (regno == 0)
936 		    regno = 19;
937 		  else
938 		    regno = 18;
939 		}
940 	      else if (flags & (OPERAND_FFLAG | OPERAND_CFLAG))
941 		regno = 19;
942 
943 	      if (flags & OPERAND_DEST)
944 		{
945 		  mod[j] |= 1 << regno;
946 		  if (flags & OPERAND_EVEN)
947 		    mod[j] |= 1 << (regno + 1);
948 		}
949 	      else
950 		{
951 		  used[j] |= 1 << regno;
952 		  if (flags & OPERAND_EVEN)
953 		    used[j] |= 1 << (regno + 1);
954 
955 		  /* Auto inc/dec also modifies the register.  */
956 		  if (op->operands[i + 1] != 0
957 		      && (d10v_operands[op->operands[i + 1]].flags
958 			  & (OPERAND_PLUS | OPERAND_MINUS)) != 0)
959 		    mod[j] |= 1 << regno;
960 		}
961 	    }
962 	  else if (flags & OPERAND_ATMINUS)
963 	    {
964 	      /* SP implicitly used/modified.  */
965 	      mod[j] |= 1 << 15;
966 	      used[j] |= 1 << 15;
967 	    }
968 	}
969       if (op->exec_type & RMEM)
970 	used[j] |= 1 << 20;
971       else if (op->exec_type & WMEM)
972 	mod[j] |= 1 << 20;
973       else if (op->exec_type & RF0)
974 	used[j] |= 1 << 19;
975       else if (op->exec_type & WF0)
976 	mod[j] |= 1 << 19;
977       else if (op->exec_type & WCAR)
978 	mod[j] |= 1 << 19;
979     }
980   if ((mod[0] & mod[1]) == 0 && (mod[0] & used[1]) == 0)
981     return 1;
982   return 0;
983 }
984 
985 /* Expects two short instructions.
986    If possible, writes out both as a single packed instruction.
987    Otherwise, writes out the first one, packed with a NOP.
988    Returns number of instructions not written out.  */
989 
990 static int
write_2_short(struct d10v_opcode * opcode1,unsigned long insn1,struct d10v_opcode * opcode2,unsigned long insn2,packing_type exec_type,Fixups * fx)991 write_2_short (struct d10v_opcode *opcode1,
992 	       unsigned long insn1,
993 	       struct d10v_opcode *opcode2,
994 	       unsigned long insn2,
995 	       packing_type exec_type,
996 	       Fixups *fx)
997 {
998   unsigned long insn;
999   char *f;
1000   int i, j, where;
1001 
1002   if ((exec_type != PACK_PARALLEL)
1003       && ((opcode1->exec_type & PARONLY) || (opcode2->exec_type & PARONLY)))
1004     as_fatal (_("Instruction must be executed in parallel"));
1005 
1006   if ((opcode1->format & LONG_OPCODE) || (opcode2->format & LONG_OPCODE))
1007     as_fatal (_("Long instructions may not be combined."));
1008 
1009   switch (exec_type)
1010     {
1011     case PACK_UNSPEC:	/* Order not specified.  */
1012       if (opcode1->exec_type & ALONE)
1013 	{
1014 	  /* Case of a short branch on a separate GAS line.  Pack with NOP.  */
1015 	  write_1_short (opcode1, insn1, fx->next);
1016 	  return 1;
1017 	}
1018       if (Optimizing
1019 	  && parallel_ok (opcode1, insn1, opcode2, insn2, exec_type))
1020 	{
1021 	  /* Parallel.  */
1022 	  if (opcode1->unit == IU)
1023 	    insn = FM00 | (insn2 << 15) | insn1;
1024 	  else if (opcode2->unit == MU)
1025 	    insn = FM00 | (insn2 << 15) | insn1;
1026 	  else
1027 	    insn = FM00 | (insn1 << 15) | insn2;
1028 	}
1029       else if (opcode1->unit == IU)
1030 	/* Reverse sequential with IU opcode1 on right and done first.  */
1031 	insn = FM10 | (insn2 << 15) | insn1;
1032       else
1033 	/* Sequential with non-IU opcode1 on left and done first.  */
1034 	insn = FM01 | (insn1 << 15) | insn2;
1035       break;
1036 
1037     case PACK_PARALLEL:
1038       if (opcode1->exec_type & SEQ || opcode2->exec_type & SEQ)
1039 	as_fatal
1040 	  (_("One of these instructions may not be executed in parallel."));
1041       if (opcode1->unit == IU)
1042 	{
1043 	  if (opcode2->unit == IU)
1044 	    as_fatal (_("Two IU instructions may not be executed in parallel"));
1045 	  if (!flag_warn_suppress_instructionswap)
1046 	    as_warn (_("Swapping instruction order"));
1047 	  insn = FM00 | (insn2 << 15) | insn1;
1048 	}
1049       else if (opcode2->unit == MU)
1050 	{
1051 	  if (opcode1->unit == MU)
1052 	    as_fatal (_("Two MU instructions may not be executed in parallel"));
1053 	  if (!flag_warn_suppress_instructionswap)
1054 	    as_warn (_("Swapping instruction order"));
1055 	  insn = FM00 | (insn2 << 15) | insn1;
1056 	}
1057       else
1058 	insn = FM00 | (insn1 << 15) | insn2;
1059       check_resource_conflict (opcode1, insn1, opcode2, insn2);
1060       break;
1061 
1062     case PACK_LEFT_RIGHT:
1063       if (opcode1->unit != IU)
1064 	insn = FM01 | (insn1 << 15) | insn2;
1065       else if (opcode2->unit == MU || opcode2->unit == EITHER)
1066 	{
1067 	  if (!flag_warn_suppress_instructionswap)
1068 	    as_warn (_("Swapping instruction order"));
1069 	  insn = FM10 | (insn2 << 15) | insn1;
1070 	}
1071       else
1072 	as_fatal (_("IU instruction may not be in the left container"));
1073       if (opcode1->exec_type & ALONE)
1074 	as_warn (_("Instruction in R container is squashed by flow control instruction in L container."));
1075       break;
1076 
1077     case PACK_RIGHT_LEFT:
1078       if (opcode2->unit != MU)
1079 	insn = FM10 | (insn1 << 15) | insn2;
1080       else if (opcode1->unit == IU || opcode1->unit == EITHER)
1081 	{
1082 	  if (!flag_warn_suppress_instructionswap)
1083 	    as_warn (_("Swapping instruction order"));
1084 	  insn = FM01 | (insn2 << 15) | insn1;
1085 	}
1086       else
1087 	as_fatal (_("MU instruction may not be in the right container"));
1088       if (opcode2->exec_type & ALONE)
1089 	as_warn (_("Instruction in R container is squashed by flow control instruction in L container."));
1090       break;
1091 
1092     default:
1093       as_fatal (_("unknown execution type passed to write_2_short()"));
1094     }
1095 
1096   f = frag_more (4);
1097   number_to_chars_bigendian (f, insn, 4);
1098 
1099   /* Process fixup chains.  fx refers to insn2 when j == 0, and to
1100      insn1 when j == 1.  Yes, it's reversed.  */
1101 
1102   for (j = 0; j < 2; j++)
1103     {
1104       for (i = 0; i < fx->fc; i++)
1105 	{
1106 	  if (fx->fix[i].reloc)
1107 	    {
1108 	      where = f - frag_now->fr_literal;
1109 	      if (fx->fix[i].size == 2)
1110 		where += 2;
1111 
1112 	      if (fx->fix[i].reloc == BFD_RELOC_D10V_10_PCREL_R
1113 		  /* A BFD_RELOC_D10V_10_PCREL_R relocation applied to
1114 		     the instruction in the L container has to be
1115 		     adjusted to BDF_RELOC_D10V_10_PCREL_L.  When
1116 		     j==0, we're processing insn2's operands, so we
1117 		     want to mark the operand if insn2 is *not* in the
1118 		     R container.  When j==1, we're processing insn1's
1119 		     operands, so we want to mark the operand if insn2
1120 		     *is* in the R container.  Note that, if two
1121 		     instructions are identical, we're never going to
1122 		     swap them, so the test is safe.  */
1123 		  && j == ((insn & 0x7fff) == insn2))
1124 		fx->fix[i].operand |= 1024;
1125 
1126 	      if (fx->fix[i].reloc == BFD_RELOC_D10V_18)
1127 		fx->fix[i].operand |= 4096;
1128 
1129 	      fix_new_exp (frag_now,
1130 			   where,
1131 			   fx->fix[i].size,
1132 			   &(fx->fix[i].exp),
1133 			   fx->fix[i].pcrel,
1134 			   fx->fix[i].operand|2048);
1135 	    }
1136 	}
1137       fx->fc = 0;
1138       fx = fx->next;
1139     }
1140   return 0;
1141 }
1142 
1143 /* This is the main entry point for the machine-dependent assembler.
1144    str points to a machine-dependent instruction.  This function is
1145    supposed to emit the frags/bytes it assembles to.  For the D10V, it
1146    mostly handles the special VLIW parsing and packing and leaves the
1147    difficult stuff to do_assemble().  */
1148 
1149 static unsigned long prev_insn;
1150 static struct d10v_opcode *prev_opcode = 0;
1151 static subsegT prev_subseg;
1152 static segT prev_seg = 0;;
1153 
1154 /* Find the symbol which has the same name as the register in exp.  */
1155 
1156 static symbolS *
find_symbol_matching_register(expressionS * exp)1157 find_symbol_matching_register (expressionS *exp)
1158 {
1159   int i;
1160 
1161   if (exp->X_op != O_register)
1162     return NULL;
1163 
1164   /* Find the name of the register.  */
1165   for (i = d10v_reg_name_cnt (); i--;)
1166     if (d10v_predefined_registers[i].value == exp->X_add_number)
1167       break;
1168 
1169   if (i < 0)
1170     abort ();
1171 
1172   /* Now see if a symbol has been defined with the same name.  */
1173   return symbol_find (d10v_predefined_registers[i].name);
1174 }
1175 
1176 /* Get a pointer to an entry in the opcode table.
1177    The function must look at all opcodes with the same name and use
1178    the operands to choose the correct opcode.  */
1179 
1180 static struct d10v_opcode *
find_opcode(struct d10v_opcode * opcode,expressionS myops[])1181 find_opcode (struct d10v_opcode *opcode, expressionS myops[])
1182 {
1183   int i, match;
1184   struct d10v_opcode *next_opcode;
1185 
1186   /* Get all the operands and save them as expressions.  */
1187   get_operands (myops);
1188 
1189   /* Now see if the operand is a fake.  If so, find the correct size
1190      instruction, if possible.  */
1191   if (opcode->format == OPCODE_FAKE)
1192     {
1193       int opnum = opcode->operands[0];
1194       int flags;
1195 
1196       if (myops[opnum].X_op == O_register)
1197 	{
1198 	  myops[opnum].X_op = O_symbol;
1199 	  myops[opnum].X_add_symbol =
1200 	    symbol_find_or_make ((char *) myops[opnum].X_op_symbol);
1201 	  myops[opnum].X_add_number = 0;
1202 	  myops[opnum].X_op_symbol = NULL;
1203 	}
1204 
1205       next_opcode = opcode + 1;
1206 
1207       /* If the first operand is supposed to be a register, make sure
1208 	 we got a valid one.  */
1209       flags = d10v_operands[next_opcode->operands[0]].flags;
1210       if (flags & OPERAND_REG)
1211 	{
1212 	  int X_op = myops[0].X_op;
1213 	  int num = myops[0].X_add_number;
1214 
1215 	  if (X_op != O_register
1216 	      || (num & ~flags
1217 		  & (OPERAND_GPR | OPERAND_ACC0 | OPERAND_ACC1
1218 		     | OPERAND_FFLAG | OPERAND_CFLAG | OPERAND_CONTROL))
1219 	      || ((flags & OPERAND_SP) && ! (num & OPERAND_SP)))
1220 	    {
1221 	      as_bad (_("bad opcode or operands"));
1222 	      return 0;
1223 	    }
1224 	}
1225 
1226       if (myops[opnum].X_op == O_constant
1227 	  || (myops[opnum].X_op == O_symbol
1228 	      && S_IS_DEFINED (myops[opnum].X_add_symbol)
1229 	      && (S_GET_SEGMENT (myops[opnum].X_add_symbol) == now_seg)))
1230 	{
1231 	  for (i = 0; opcode->operands[i + 1]; i++)
1232 	    {
1233 	      int bits = d10v_operands[next_opcode->operands[opnum]].bits;
1234 	      int flags = d10v_operands[next_opcode->operands[opnum]].flags;
1235 	      if (flags & OPERAND_ADDR)
1236 		bits += 2;
1237 
1238 	      if (myops[opnum].X_op == O_constant)
1239 		{
1240 		  if (!check_range (myops[opnum].X_add_number, bits, flags))
1241 		    break;
1242 		}
1243 	      else
1244 		{
1245 		  fragS *sym_frag;
1246 		  fragS *f;
1247 		  unsigned long current_position;
1248 		  unsigned long symbol_position;
1249 		  unsigned long value;
1250 		  bfd_boolean found_symbol;
1251 
1252 		  /* Calculate the address of the current instruction
1253 		     and the address of the symbol.  Do this by summing
1254 		     the offsets of previous frags until we reach the
1255 		     frag containing the symbol, and the current frag.  */
1256 		  sym_frag = symbol_get_frag (myops[opnum].X_add_symbol);
1257 		  found_symbol = FALSE;
1258 
1259 		  current_position =
1260 		    obstack_next_free (&frchain_now->frch_obstack)
1261 		    - frag_now->fr_literal;
1262 		  symbol_position = S_GET_VALUE (myops[opnum].X_add_symbol);
1263 
1264 		  for (f = frchain_now->frch_root; f; f = f->fr_next)
1265 		    {
1266 		      current_position += f->fr_fix + f->fr_offset;
1267 
1268 		      if (f == sym_frag)
1269 			found_symbol = TRUE;
1270 
1271 		      if (! found_symbol)
1272 			symbol_position += f->fr_fix + f->fr_offset;
1273 		    }
1274 
1275 		  value = symbol_position;
1276 
1277 		  if (flags & OPERAND_ADDR)
1278 		    value -= current_position;
1279 
1280 		  if (AT_WORD_P (&myops[opnum]))
1281 		    {
1282 		      if (bits > 4)
1283 			{
1284 			  bits += 2;
1285 			  if (!check_range (value, bits, flags))
1286 			    break;
1287 			}
1288 		    }
1289 		  else if (!check_range (value, bits, flags))
1290 		    break;
1291 		}
1292 	      next_opcode++;
1293 	    }
1294 
1295 	  if (opcode->operands [i + 1] == 0)
1296 	    as_fatal (_("value out of range"));
1297 	  else
1298 	    opcode = next_opcode;
1299 	}
1300       else
1301 	/* Not a constant, so use a long instruction.  */
1302 	opcode += 2;
1303     }
1304 
1305   match = 0;
1306 
1307   /* Now search the opcode table table for one with operands
1308      that matches what we've got.  */
1309   while (!match)
1310     {
1311       match = 1;
1312       for (i = 0; opcode->operands[i]; i++)
1313 	{
1314 	  int flags = d10v_operands[opcode->operands[i]].flags;
1315 	  int X_op = myops[i].X_op;
1316 	  int num = myops[i].X_add_number;
1317 
1318 	  if (X_op == 0)
1319 	    {
1320 	      match = 0;
1321 	      break;
1322 	    }
1323 
1324 	  if (flags & OPERAND_REG)
1325 	    {
1326 	      if ((X_op != O_register)
1327 		  || (num & ~flags
1328 		      & (OPERAND_GPR | OPERAND_ACC0 | OPERAND_ACC1
1329 			 | OPERAND_FFLAG | OPERAND_CFLAG
1330 			 | OPERAND_CONTROL))
1331 		  || ((flags & OPERAND_SP) && ! (num & OPERAND_SP)))
1332 		{
1333 		  match = 0;
1334 		  break;
1335 		}
1336 	    }
1337 
1338 	  if (((flags & OPERAND_MINUS)   && ((X_op != O_absent) || (num != OPERAND_MINUS))) ||
1339 	      ((flags & OPERAND_PLUS)    && ((X_op != O_absent) || (num != OPERAND_PLUS))) ||
1340 	      ((flags & OPERAND_ATMINUS) && ((X_op != O_absent) || (num != OPERAND_ATMINUS))) ||
1341 	      ((flags & OPERAND_ATPAR)   && ((X_op != O_absent) || (num != OPERAND_ATPAR))) ||
1342 	      ((flags & OPERAND_ATSIGN)  && ((X_op != O_absent) || ((num != OPERAND_ATSIGN) && (num != OPERAND_ATPAR)))))
1343 	    {
1344 	      match = 0;
1345 	      break;
1346 	    }
1347 
1348 	  /* Unfortunately, for the indirect operand in instructions such
1349 	     as ``ldb r1, @(c,r14)'' this function can be passed
1350 	     X_op == O_register (because 'c' is a valid register name).
1351 	     However we cannot just ignore the case when X_op == O_register
1352 	     but flags & OPERAND_REG is null, so we check to see if a symbol
1353 	     of the same name as the register exists.  If the symbol does
1354 	     exist, then the parser was unable to distinguish the two cases
1355 	     and we fix things here. (Ref: PR14826)  */
1356 
1357 	  if (!(flags & OPERAND_REG) && (X_op == O_register))
1358 	    {
1359 	      symbolS * sym;
1360 
1361 	      sym = find_symbol_matching_register (& myops[i]);
1362 
1363 	      if (sym != NULL)
1364 		{
1365 		  myops[i].X_op = X_op = O_symbol;
1366 		  myops[i].X_add_symbol = sym;
1367 		}
1368 	      else
1369 		as_bad
1370 		  (_("illegal operand - register name found where none expected"));
1371 	    }
1372 	}
1373 
1374       /* We're only done if the operands matched so far AND there
1375 	     are no more to check.  */
1376       if (match && myops[i].X_op == 0)
1377 	break;
1378       else
1379 	match = 0;
1380 
1381       next_opcode = opcode + 1;
1382 
1383       if (next_opcode->opcode == 0)
1384 	break;
1385 
1386       if (strcmp (next_opcode->name, opcode->name))
1387 	break;
1388 
1389       opcode = next_opcode;
1390     }
1391 
1392   if (!match)
1393     {
1394       as_bad (_("bad opcode or operands"));
1395       return 0;
1396     }
1397 
1398   /* Check that all registers that are required to be even are.
1399      Also, if any operands were marked as registers, but were really symbols,
1400      fix that here.  */
1401   for (i = 0; opcode->operands[i]; i++)
1402     {
1403       if ((d10v_operands[opcode->operands[i]].flags & OPERAND_EVEN) &&
1404 	  (myops[i].X_add_number & 1))
1405 	as_fatal (_("Register number must be EVEN"));
1406       if ((d10v_operands[opcode->operands[i]].flags & OPERAND_NOSP)
1407 	  && (myops[i].X_add_number & OPERAND_SP))
1408 	as_bad (_("Unsupported use of sp"));
1409       if (myops[i].X_op == O_register)
1410 	{
1411 	  if (!(d10v_operands[opcode->operands[i]].flags & OPERAND_REG))
1412 	    {
1413 	      myops[i].X_op = O_symbol;
1414 	      myops[i].X_add_symbol =
1415 		symbol_find_or_make ((char *) myops[i].X_op_symbol);
1416 	      myops[i].X_add_number = 0;
1417 	      myops[i].X_op_symbol = NULL;
1418 	    }
1419 	}
1420       if ((d10v_operands[opcode->operands[i]].flags & OPERAND_CONTROL)
1421 	  && (myops[i].X_add_number == OPERAND_CONTROL + 4
1422 	      || myops[i].X_add_number == OPERAND_CONTROL + 5
1423 	      || myops[i].X_add_number == OPERAND_CONTROL + 6
1424 	      || myops[i].X_add_number == OPERAND_CONTROL + 12
1425 	      || myops[i].X_add_number == OPERAND_CONTROL + 13
1426 	      || myops[i].X_add_number == OPERAND_CONTROL + 15))
1427 	as_warn (_("cr%ld is a reserved control register"),
1428 		 myops[i].X_add_number - OPERAND_CONTROL);
1429     }
1430   return opcode;
1431 }
1432 
1433 /* Assemble a single instruction.
1434    Return an opcode, or -1 (an invalid opcode) on error.  */
1435 
1436 static unsigned long
do_assemble(char * str,struct d10v_opcode ** opcode)1437 do_assemble (char *str, struct d10v_opcode **opcode)
1438 {
1439   unsigned char *op_start, *op_end;
1440   char *save;
1441   char name[20];
1442   int nlen = 0;
1443   expressionS myops[6];
1444   unsigned long insn;
1445 
1446   /* Drop leading whitespace.  */
1447   while (*str == ' ')
1448     str++;
1449 
1450   /* Find the opcode end.  */
1451   for (op_start = op_end = (unsigned char *) str;
1452        *op_end && nlen < 20 && !is_end_of_line[*op_end] && *op_end != ' ';
1453        op_end++)
1454     {
1455       name[nlen] = TOLOWER (op_start[nlen]);
1456       nlen++;
1457     }
1458   name[nlen] = 0;
1459 
1460   if (nlen == 0)
1461     return -1;
1462 
1463   /* Find the first opcode with the proper name.  */
1464   *opcode = (struct d10v_opcode *) hash_find (d10v_hash, name);
1465   if (*opcode == NULL)
1466     as_fatal (_("unknown opcode: %s"), name);
1467 
1468   save = input_line_pointer;
1469   input_line_pointer = (char *) op_end;
1470   *opcode = find_opcode (*opcode, myops);
1471   if (*opcode == 0)
1472     return -1;
1473   input_line_pointer = save;
1474 
1475   insn = build_insn ((*opcode), myops, 0);
1476   return insn;
1477 }
1478 
1479 /* If while processing a fixup, a reloc really needs to be created.
1480    Then it is done here.  */
1481 
1482 arelent *
tc_gen_reloc(asection * seg ATTRIBUTE_UNUSED,fixS * fixp)1483 tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
1484 {
1485   arelent *reloc;
1486   reloc = xmalloc (sizeof (arelent));
1487   reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
1488   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1489   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1490   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1491   if (reloc->howto == (reloc_howto_type *) NULL)
1492     {
1493       as_bad_where (fixp->fx_file, fixp->fx_line,
1494 		    _("reloc %d not supported by object file format"),
1495 		    (int) fixp->fx_r_type);
1496       return NULL;
1497     }
1498 
1499   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1500     reloc->address = fixp->fx_offset;
1501 
1502   reloc->addend = 0;
1503 
1504   return reloc;
1505 }
1506 
1507 int
md_estimate_size_before_relax(fragS * fragp ATTRIBUTE_UNUSED,asection * seg ATTRIBUTE_UNUSED)1508 md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
1509 			       asection *seg ATTRIBUTE_UNUSED)
1510 {
1511   abort ();
1512   return 0;
1513 }
1514 
1515 long
md_pcrel_from_section(fixS * fixp,segT sec)1516 md_pcrel_from_section (fixS *fixp, segT sec)
1517 {
1518   if (fixp->fx_addsy != (symbolS *) NULL
1519       && (!S_IS_DEFINED (fixp->fx_addsy)
1520 	  || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
1521     return 0;
1522   return fixp->fx_frag->fr_address + fixp->fx_where;
1523 }
1524 
1525 void
md_apply_fix(fixS * fixP,valueT * valP,segT seg ATTRIBUTE_UNUSED)1526 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
1527 {
1528   char *where;
1529   unsigned long insn;
1530   long value = *valP;
1531   int op_type;
1532   int left = 0;
1533 
1534   if (fixP->fx_addsy == (symbolS *) NULL)
1535     fixP->fx_done = 1;
1536 
1537   /* We don't actually support subtracting a symbol.  */
1538   if (fixP->fx_subsy != (symbolS *) NULL)
1539     as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
1540 
1541   op_type = fixP->fx_r_type;
1542   if (op_type & 2048)
1543     {
1544       op_type -= 2048;
1545       if (op_type & 1024)
1546 	{
1547 	  op_type -= 1024;
1548 	  fixP->fx_r_type = BFD_RELOC_D10V_10_PCREL_L;
1549 	  left = 1;
1550 	}
1551       else if (op_type & 4096)
1552 	{
1553 	  op_type -= 4096;
1554 	  fixP->fx_r_type = BFD_RELOC_D10V_18;
1555 	}
1556       else
1557 	fixP->fx_r_type =
1558 	  get_reloc ((struct d10v_operand *) &d10v_operands[op_type]);
1559     }
1560 
1561   /* Fetch the instruction, insert the fully resolved operand
1562      value, and stuff the instruction back again.  */
1563   where = fixP->fx_frag->fr_literal + fixP->fx_where;
1564   insn = bfd_getb32 ((unsigned char *) where);
1565 
1566   switch (fixP->fx_r_type)
1567     {
1568     case BFD_RELOC_D10V_10_PCREL_L:
1569     case BFD_RELOC_D10V_10_PCREL_R:
1570     case BFD_RELOC_D10V_18_PCREL:
1571       /* If the fix is relative to a global symbol, not a section
1572 	 symbol, then ignore the offset.
1573          XXX - Do we have to worry about branches to a symbol + offset ?  */
1574       if (fixP->fx_addsy != NULL
1575 	  && S_IS_EXTERNAL (fixP->fx_addsy) )
1576         {
1577           segT fseg = S_GET_SEGMENT (fixP->fx_addsy);
1578           segment_info_type *segf = seg_info(fseg);
1579 
1580 	  if ( segf && segf->sym != fixP->fx_addsy)
1581 	    value = 0;
1582         }
1583       /* Drop through.  */
1584     case BFD_RELOC_D10V_18:
1585       /* Instruction addresses are always right-shifted by 2.  */
1586       value >>= AT_WORD_RIGHT_SHIFT;
1587       if (fixP->fx_size == 2)
1588 	bfd_putb16 ((bfd_vma) value, (unsigned char *) where);
1589       else
1590 	{
1591 	  struct d10v_opcode *rep, *repi;
1592 
1593 	  rep = (struct d10v_opcode *) hash_find (d10v_hash, "rep");
1594 	  repi = (struct d10v_opcode *) hash_find (d10v_hash, "repi");
1595 	  if ((insn & FM11) == FM11
1596 	      && ((repi != NULL
1597 		   && (insn & repi->mask) == (unsigned) repi->opcode)
1598 		  || (rep != NULL
1599 		      && (insn & rep->mask) == (unsigned) rep->opcode))
1600 	      && value < 4)
1601 	    as_fatal
1602 	      (_("line %d: rep or repi must include at least 4 instructions"),
1603 	       fixP->fx_line);
1604 	  insn =
1605 	    d10v_insert_operand (insn, op_type, (offsetT) value, left, fixP);
1606 	  bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1607 	}
1608       break;
1609     case BFD_RELOC_32:
1610       bfd_putb32 ((bfd_vma) value, (unsigned char *) where);
1611       break;
1612     case BFD_RELOC_16:
1613       bfd_putb16 ((bfd_vma) value, (unsigned char *) where);
1614       break;
1615 
1616     case BFD_RELOC_VTABLE_INHERIT:
1617     case BFD_RELOC_VTABLE_ENTRY:
1618       fixP->fx_done = 0;
1619       return;
1620 
1621     default:
1622       as_fatal (_("line %d: unknown relocation type: 0x%x"),
1623 		fixP->fx_line, fixP->fx_r_type);
1624     }
1625 }
1626 
1627 /* d10v_cleanup() is called after the assembler has finished parsing
1628    the input file, when a label is read from the input file, or when a
1629    stab directive is output.  Because the D10V assembler sometimes
1630    saves short instructions to see if it can package them with the
1631    next instruction, there may be a short instruction that still needs
1632    to be written.
1633 
1634    NOTE: accesses a global, etype.
1635    NOTE: invoked by various macros such as md_cleanup: see.  */
1636 
1637 int
d10v_cleanup(void)1638 d10v_cleanup (void)
1639 {
1640   segT seg;
1641   subsegT subseg;
1642 
1643   /* If cleanup was invoked because the assembler encountered, e.g., a
1644      user label, we write out the pending instruction, if any.  If it
1645      was invoked because the assembler is outputting a piece of line
1646      debugging information, though, we write out the pending
1647      instruction only if the --no-gstabs-packing command line switch
1648      has been specified.  */
1649   if (prev_opcode
1650       && etype == PACK_UNSPEC
1651       && (! outputting_stabs_line_debug || ! flag_allow_gstabs_packing))
1652     {
1653       seg = now_seg;
1654       subseg = now_subseg;
1655 
1656       if (prev_seg)
1657 	subseg_set (prev_seg, prev_subseg);
1658 
1659       write_1_short (prev_opcode, prev_insn, fixups->next);
1660       subseg_set (seg, subseg);
1661       prev_opcode = NULL;
1662     }
1663   return 1;
1664 }
1665 
1666 /* Like normal .word, except support @word.
1667    Clobbers input_line_pointer, checks end-of-line.  */
1668 
1669 static void
d10v_dot_word(int dummy ATTRIBUTE_UNUSED)1670 d10v_dot_word (int dummy ATTRIBUTE_UNUSED)
1671 {
1672   expressionS exp;
1673   char *p;
1674 
1675   if (is_it_end_of_statement ())
1676     {
1677       demand_empty_rest_of_line ();
1678       return;
1679     }
1680 
1681   do
1682     {
1683       expression (&exp);
1684       if (!strncasecmp (input_line_pointer, "@word", 5))
1685 	{
1686 	  exp.X_add_number = 0;
1687 	  input_line_pointer += 5;
1688 
1689 	  p = frag_more (2);
1690 	  fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
1691 		       &exp, 0, BFD_RELOC_D10V_18);
1692 	}
1693       else
1694 	emit_expr (&exp, 2);
1695     }
1696   while (*input_line_pointer++ == ',');
1697 
1698   input_line_pointer--;		/* Put terminator back into stream.  */
1699   demand_empty_rest_of_line ();
1700 }
1701 
1702 /* Mitsubishi asked that we support some old syntax that apparently
1703    had immediate operands starting with '#'.  This is in some of their
1704    sample code but is not documented (although it appears in some
1705    examples in their assembler manual). For now, we'll solve this
1706    compatibility problem by simply ignoring any '#' at the beginning
1707    of an operand.  */
1708 
1709 /* Operands that begin with '#' should fall through to here.
1710    From expr.c.  */
1711 
1712 void
md_operand(expressionS * expressionP)1713 md_operand (expressionS *expressionP)
1714 {
1715   if (*input_line_pointer == '#' && ! do_not_ignore_hash)
1716     {
1717       input_line_pointer++;
1718       expression (expressionP);
1719     }
1720 }
1721 
1722 bfd_boolean
d10v_fix_adjustable(fixS * fixP)1723 d10v_fix_adjustable (fixS *fixP)
1724 {
1725   /* We need the symbol name for the VTABLE entries.  */
1726   if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1727       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1728     return 0;
1729 
1730   return 1;
1731 }
1732 
1733 /* The target specific pseudo-ops which we support.  */
1734 const pseudo_typeS md_pseudo_table[] =
1735 {
1736   { "word",	d10v_dot_word,	2 },
1737   { NULL,       NULL,           0 }
1738 };
1739 
1740 void
md_assemble(char * str)1741 md_assemble (char *str)
1742 {
1743   /* etype is saved extype.  For multi-line instructions.  */
1744   packing_type extype = PACK_UNSPEC;		/* Parallel, etc.  */
1745   struct d10v_opcode *opcode;
1746   unsigned long insn;
1747   char *str2;
1748 
1749   if (etype == PACK_UNSPEC)
1750     {
1751       /* Look for the special multiple instruction separators.  */
1752       str2 = strstr (str, "||");
1753       if (str2)
1754 	extype = PACK_PARALLEL;
1755       else
1756 	{
1757 	  str2 = strstr (str, "->");
1758 	  if (str2)
1759 	    extype = PACK_LEFT_RIGHT;
1760 	  else
1761 	    {
1762 	      str2 = strstr (str, "<-");
1763 	      if (str2)
1764 		extype = PACK_RIGHT_LEFT;
1765 	    }
1766 	}
1767 
1768       /* str2 points to the separator, if there is one.  */
1769       if (str2)
1770 	{
1771 	  *str2 = 0;
1772 
1773 	  /* If two instructions are present and we already have one saved,
1774 	     then first write out the saved one.  */
1775 	  d10v_cleanup ();
1776 
1777 	  /* Assemble first instruction and save it.  */
1778 	  prev_insn = do_assemble (str, &prev_opcode);
1779 	  prev_seg = now_seg;
1780 	  prev_subseg = now_subseg;
1781 	  if (prev_insn == (unsigned long) -1)
1782 	    as_fatal (_("can't find opcode "));
1783 	  fixups = fixups->next;
1784 	  str = str2 + 2;
1785 	}
1786     }
1787 
1788   insn = do_assemble (str, &opcode);
1789   if (insn == (unsigned long) -1)
1790     {
1791       if (extype != PACK_UNSPEC)
1792 	{
1793 	  etype = extype;
1794 	  return;
1795 	}
1796       as_fatal (_("can't find opcode "));
1797     }
1798 
1799   if (etype != PACK_UNSPEC)
1800     {
1801       extype = etype;
1802       etype = PACK_UNSPEC;
1803     }
1804 
1805   /* If this is a long instruction, write it and any previous short
1806      instruction.  */
1807   if (opcode->format & LONG_OPCODE)
1808     {
1809       if (extype != PACK_UNSPEC)
1810 	as_fatal (_("Unable to mix instructions as specified"));
1811       d10v_cleanup ();
1812       write_long (insn, fixups);
1813       prev_opcode = NULL;
1814       return;
1815     }
1816 
1817   if (prev_opcode
1818       && prev_seg
1819       && ((prev_seg != now_seg) || (prev_subseg != now_subseg)))
1820     d10v_cleanup ();
1821 
1822   if (prev_opcode
1823       && (0 == write_2_short (prev_opcode, prev_insn, opcode, insn, extype,
1824 			      fixups)))
1825     {
1826       /* No instructions saved.  */
1827       prev_opcode = NULL;
1828     }
1829   else
1830     {
1831       if (extype != PACK_UNSPEC)
1832 	as_fatal (_("Unable to mix instructions as specified"));
1833       /* Save last instruction so it may be packed on next pass.  */
1834       prev_opcode = opcode;
1835       prev_insn = insn;
1836       prev_seg = now_seg;
1837       prev_subseg = now_subseg;
1838       fixups = fixups->next;
1839     }
1840 }
1841 
1842