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