Added --nowarnswap arg to suppress "Swapping instructions" warning.
[external/binutils.git] / gas / config / tc-d10v.c
1 /* tc-d10v.c -- Assembler code for the Mitsubishi D10V
2
3    Copyright (C) 1996, 1997 Free Software Foundation.
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 <ctype.h>
24 #include "as.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 (-1)
39
40 /* fixups */
41 #define MAX_INSN_FIXUPS (5)
42 struct d10v_fixup
43 {
44   expressionS exp;
45   int operand;
46   int pcrel;
47   int size;
48   bfd_reloc_code_real_type reloc;
49 };
50
51 typedef struct _fixups
52 {
53   int fc;
54   struct d10v_fixup fix[MAX_INSN_FIXUPS];
55   struct _fixups *next;
56 } Fixups;
57
58 static Fixups FixUps[2];
59 static Fixups *fixups;
60
61 /* local functions */
62 static int reg_name_search PARAMS ((char *name));
63 static int register_name PARAMS ((expressionS *expressionP));
64 static int check_range PARAMS ((unsigned long num, int bits, int flags));
65 static int postfix PARAMS ((char *p));
66 static bfd_reloc_code_real_type get_reloc PARAMS ((struct d10v_operand *op));
67 static int get_operands PARAMS ((expressionS exp[]));
68 static struct d10v_opcode *find_opcode PARAMS ((struct d10v_opcode *opcode, expressionS ops[]));
69 static unsigned long build_insn PARAMS ((struct d10v_opcode *opcode, expressionS *opers, unsigned long insn));
70 static void write_long PARAMS ((struct d10v_opcode *opcode, unsigned long insn, Fixups *fx));
71 static void write_1_short PARAMS ((struct d10v_opcode *opcode, unsigned long insn, Fixups *fx));
72 static int write_2_short PARAMS ((struct d10v_opcode *opcode1, unsigned long insn1, 
73                                   struct d10v_opcode *opcode2, unsigned long insn2, int exec_type, Fixups *fx));
74 static unsigned long do_assemble PARAMS ((char *str, struct d10v_opcode **opcode));
75 static unsigned long d10v_insert_operand PARAMS (( unsigned long insn, int op_type,
76                                                    offsetT value, int left, fixS *fix));
77 static int parallel_ok PARAMS ((struct d10v_opcode *opcode1, unsigned long insn1, 
78                                 struct d10v_opcode *opcode2, unsigned long insn2,
79                                 int exec_type));
80
81 struct option md_longopts[] = {
82   {NULL, no_argument, NULL, 0}
83 };
84 size_t md_longopts_size = sizeof(md_longopts);       
85
86 static void d10v_dot_word PARAMS ((int));
87
88 /* The target specific pseudo-ops which we support.  */
89 const pseudo_typeS md_pseudo_table[] =
90 {
91   { "word",     d10v_dot_word,  2 },
92   { NULL,       NULL,           0 }
93 };
94
95 /* Opcode hash table.  */
96 static struct hash_control *d10v_hash;
97
98 /* reg_name_search does a binary search of the d10v_predefined_registers
99    array to see if "name" is a valid regiter name.  Returns the register
100    number from the array on success, or -1 on failure. */
101
102 static int
103 reg_name_search (name)
104      char *name;
105 {
106   int middle, low, high;
107   int cmp;
108
109   low = 0;
110   high = d10v_reg_name_cnt() - 1;
111
112   do
113     {
114       middle = (low + high) / 2;
115       cmp = strcasecmp (name, d10v_predefined_registers[middle].name);
116       if (cmp < 0)
117         high = middle - 1;
118       else if (cmp > 0)
119         low = middle + 1;
120       else 
121           return d10v_predefined_registers[middle].value;
122     }
123   while (low <= high);
124   return -1;
125 }
126
127 /* register_name() checks the string at input_line_pointer
128    to see if it is a valid register name */
129
130 static int
131 register_name (expressionP)
132      expressionS *expressionP;
133 {
134   int reg_number;
135   char c, *p = input_line_pointer;
136   
137   while (*p && *p!='\n' && *p!='\r' && *p !=',' && *p!=' ' && *p!=')')
138     p++;
139
140   c = *p;
141   if (c)
142     *p++ = 0;
143
144   /* look to see if it's in the register table */
145   reg_number = reg_name_search (input_line_pointer);
146   if (reg_number >= 0) 
147     {
148       expressionP->X_op = O_register;
149       /* temporarily store a pointer to the string here */
150       expressionP->X_op_symbol = (struct symbol *)input_line_pointer;
151       expressionP->X_add_number = reg_number;
152       input_line_pointer = p;
153       return 1;
154     }
155   if (c)
156     *(p-1) = c;
157   return 0;
158 }
159
160
161 static int
162 check_range (num, bits, flags)
163      unsigned long num;
164      int bits;
165      int flags;
166 {
167   long min, max, bit1;
168   int retval=0;
169
170   /* don't bother checking 16-bit values */
171   if (bits == 16)
172     return 0;
173
174   if (flags & OPERAND_SHIFT)
175     {
176       /* all special shift operands are unsigned */
177       /* and <= 16.  We allow 0 for now. */
178       if (num>16)
179         return 1;
180       else
181         return 0;
182     }
183
184   if (flags & OPERAND_SIGNED)
185     {
186       max = (1 << (bits - 1))-1; 
187       min = - (1 << (bits - 1));  
188       if (((long)num > max) || ((long)num < min))
189         retval = 1;
190     }
191   else
192     {
193       max = (1 << bits) - 1;
194       min = 0;
195       if ((num > max) || (num < min))
196         retval = 1;
197     }
198   return retval;
199 }
200
201
202 void
203 md_show_usage (stream)
204   FILE *stream;
205 {
206   fprintf(stream, "D10V options:\n\
207 -O                      optimize.  Will do some operations in parallel.\n");
208
209
210 int
211 md_parse_option (c, arg)
212      int c;
213      char *arg;
214 {
215   switch (c)
216     {
217     case 'O':
218       /* Optimize. Will attempt to parallelize operations */
219       Optimizing = 1;
220       break;
221     default:
222       return 0;
223     }
224   return 1;
225 }
226
227 symbolS *
228 md_undefined_symbol (name)
229   char *name;
230 {
231   return 0;
232 }
233
234 /* Turn a string in input_line_pointer into a floating point constant of type
235    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
236    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
237  */
238 char *
239 md_atof (type, litP, sizeP)
240      int type;
241      char *litP;
242      int *sizeP;
243 {
244   int prec;
245   LITTLENUM_TYPE words[4];
246   char *t;
247   int i;
248   
249   switch (type)
250     {
251     case 'f':
252       prec = 2;
253       break;
254     case 'd':
255       prec = 4;
256       break;
257     default:
258       *sizeP = 0;
259       return "bad call to md_atof";
260     }
261
262   t = atof_ieee (input_line_pointer, type, words);
263   if (t)
264     input_line_pointer = t;
265   
266   *sizeP = prec * 2;
267   
268   for (i = 0; i < prec; i++)
269     {
270       md_number_to_chars (litP, (valueT) words[i], 2);
271           litP += 2;
272     }
273   return NULL;
274 }
275
276 void
277 md_convert_frag (abfd, sec, fragP)
278   bfd *abfd;
279   asection *sec;
280   fragS *fragP;
281 {
282   abort ();
283 }
284
285 valueT
286 md_section_align (seg, addr)
287      asection *seg;
288      valueT addr;
289 {
290   int align = bfd_get_section_alignment (stdoutput, seg);
291   return ((addr + (1 << align) - 1) & (-1 << align));
292 }
293
294
295 void
296 md_begin ()
297 {
298   char *prev_name = "";
299   struct d10v_opcode *opcode;
300   d10v_hash = hash_new();
301
302   /* Insert unique names into hash table.  The D10v instruction set
303      has many identical opcode names that have different opcodes based
304      on the operands.  This hash table then provides a quick index to
305      the first opcode with a particular name in the opcode table.  */
306
307   for (opcode = (struct d10v_opcode *)d10v_opcodes; opcode->name; opcode++)
308     {
309       if (strcmp (prev_name, opcode->name))
310         {
311           prev_name = (char *)opcode->name;
312           hash_insert (d10v_hash, opcode->name, (char *) opcode);
313         }
314     }
315
316   fixups = &FixUps[0];
317   FixUps[0].next = &FixUps[1];
318   FixUps[1].next = &FixUps[0];
319 }
320
321
322 /* this function removes the postincrement or postdecrement
323    operator ( '+' or '-' ) from an expression */
324
325 static int postfix (p) 
326      char *p;
327 {
328   while (*p != '-' && *p != '+') 
329     {
330       if (*p==0 || *p=='\n' || *p=='\r') 
331         break;
332       p++;
333     }
334
335   if (*p == '-') 
336     {
337       *p = ' ';
338       return (-1);
339     }
340   if (*p == '+') 
341     {
342       *p = ' ';
343       return (1);
344     }
345
346   return (0);
347 }
348
349
350 static bfd_reloc_code_real_type 
351 get_reloc (op) 
352      struct d10v_operand *op;
353 {
354   int bits = op->bits;
355
356   if (bits <= 4) 
357     return (0);
358       
359   if (op->flags & OPERAND_ADDR) 
360     {
361       if (bits == 8)
362         return (BFD_RELOC_D10V_10_PCREL_R);
363       else
364         return (BFD_RELOC_D10V_18_PCREL);
365     }
366
367   return (BFD_RELOC_16);
368 }
369
370
371 /* get_operands parses a string of operands and returns
372    an array of expressions */
373
374 static int
375 get_operands (exp) 
376      expressionS exp[];
377 {
378   char *p = input_line_pointer;
379   int numops = 0;
380   int post = 0;
381
382   while (*p)  
383     {
384       while (*p == ' ' || *p == '\t' || *p == ',') 
385         p++;
386       if (*p==0 || *p=='\n' || *p=='\r') 
387         break;
388       
389       if (*p == '@') 
390         {
391           p++;
392           exp[numops].X_op = O_absent;
393           if (*p == '(') 
394             {
395               p++;
396               exp[numops].X_add_number = OPERAND_ATPAR;
397             }
398           else if (*p == '-') 
399             {
400               p++;
401               exp[numops].X_add_number = OPERAND_ATMINUS;
402             }
403           else
404             {
405               exp[numops].X_add_number = OPERAND_ATSIGN;
406               post = postfix (p);
407             }
408           numops++;
409           continue;
410         }
411
412       if (*p == ')') 
413         {
414           /* just skip the trailing paren */
415           p++;
416           continue;
417         }
418
419       input_line_pointer = p;
420
421       /* check to see if it might be a register name */
422       if (!register_name (&exp[numops]))
423         {
424           /* parse as an expression */
425           expression (&exp[numops]);
426         }
427
428       if (!strncasecmp (input_line_pointer, "@word", 5))
429         {
430           if (exp[numops].X_op == O_register)
431             {
432               /* if it looked like a register name but was followed by "@word" */
433               /* then it was really a symbol, so change it to one */
434               exp[numops].X_op = O_symbol;
435               exp[numops].X_add_symbol = symbol_find_or_make ((char *)exp[numops].X_op_symbol);
436             }
437           exp[numops].X_op_symbol = (struct symbol *)-1;
438           exp[numops].X_add_number = AT_WORD;
439           input_line_pointer += 5;
440         }
441       
442       if (exp[numops].X_op == O_illegal) 
443         as_bad ("illegal operand");
444       else if (exp[numops].X_op == O_absent) 
445         as_bad ("missing operand");
446
447       numops++;
448       p = input_line_pointer;
449     }
450
451   switch (post) 
452     {
453     case -1:    /* postdecrement mode */
454       exp[numops].X_op = O_absent;
455       exp[numops++].X_add_number = OPERAND_MINUS;
456       break;
457     case 1:     /* postincrement mode */
458       exp[numops].X_op = O_absent;
459       exp[numops++].X_add_number = OPERAND_PLUS;
460       break;
461     }
462
463   exp[numops].X_op = 0;
464   return (numops);
465 }
466
467 static unsigned long
468 d10v_insert_operand (insn, op_type, value, left, fix) 
469      unsigned long insn;
470      int op_type;
471      offsetT value;
472      int left;
473      fixS *fix;
474 {
475   int shift, bits;
476
477   shift = d10v_operands[op_type].shift;
478   if (left)
479     shift += 15;
480
481   bits = d10v_operands[op_type].bits;
482
483   /* truncate to the proper number of bits */
484   if (check_range (value, bits, d10v_operands[op_type].flags))
485     as_bad_where (fix->fx_file, fix->fx_line, "operand out of range: %d", value);
486
487   value &= 0x7FFFFFFF >> (31 - bits);
488   insn |= (value << shift);
489
490   return insn;
491 }
492
493
494 /* build_insn takes a pointer to the opcode entry in the opcode table
495    and the array of operand expressions and returns the instruction */
496
497 static unsigned long
498 build_insn (opcode, opers, insn) 
499      struct d10v_opcode *opcode;
500      expressionS *opers;
501      unsigned long insn;
502 {
503   int i, bits, shift, flags, format;
504   unsigned long number;
505   
506   /* the insn argument is only used for the DIVS kludge */
507   if (insn)
508     format = LONG_R;
509   else
510     {
511       insn = opcode->opcode;
512       format = opcode->format;
513     }
514   
515   for (i=0;opcode->operands[i];i++) 
516     {
517       flags = d10v_operands[opcode->operands[i]].flags;
518       bits = d10v_operands[opcode->operands[i]].bits;
519       shift = d10v_operands[opcode->operands[i]].shift;
520       number = opers[i].X_add_number;
521
522       if (flags & OPERAND_REG) 
523         {
524           number &= REGISTER_MASK;
525           if (format == LONG_L)
526             shift += 15;
527         }
528
529       if (opers[i].X_op != O_register && opers[i].X_op != O_constant) 
530         {
531           /* now create a fixup */
532
533           if (fixups->fc >= MAX_INSN_FIXUPS)
534             as_fatal ("too many fixups");
535
536           if (opers[i].X_op == O_symbol && number == AT_WORD && 
537              opers[i].X_op_symbol == (struct symbol *)-1 )
538             {
539               number = opers[i].X_add_number = 0;
540               fixups->fix[fixups->fc].reloc = BFD_RELOC_D10V_18;
541             } else
542               fixups->fix[fixups->fc].reloc = 
543                 get_reloc((struct d10v_operand *)&d10v_operands[opcode->operands[i]]);
544
545           if (fixups->fix[fixups->fc].reloc == BFD_RELOC_16 || 
546               fixups->fix[fixups->fc].reloc == BFD_RELOC_D10V_18)
547             fixups->fix[fixups->fc].size = 2;       
548           else
549             fixups->fix[fixups->fc].size = 4;
550             
551           fixups->fix[fixups->fc].exp = opers[i];
552           fixups->fix[fixups->fc].operand = opcode->operands[i];
553           fixups->fix[fixups->fc].pcrel = (flags & OPERAND_ADDR) ? true : false;
554           (fixups->fc)++;
555         }
556
557       /* truncate to the proper number of bits */
558       if ((opers[i].X_op == O_constant) && check_range (number, bits, flags))
559         as_bad("operand out of range: %d",number);
560       number &= 0x7FFFFFFF >> (31 - bits);
561       insn = insn | (number << shift);
562     }
563
564   /* kludge: for DIVS, we need to put the operands in twice */
565   /* on the second pass, format is changed to LONG_R to force */
566   /* the second set of operands to not be shifted over 15 */
567   if ((opcode->opcode == OPCODE_DIVS) && (format==LONG_L))
568     insn = build_insn (opcode, opers, insn);
569       
570   return insn;
571 }
572
573 /* write out a long form instruction */
574 static void
575 write_long (opcode, insn, fx) 
576      struct d10v_opcode *opcode;
577      unsigned long insn;
578      Fixups *fx;
579 {
580   int i, where;
581   char *f = frag_more(4);
582
583   insn |= FM11;
584   number_to_chars_bigendian (f, insn, 4);
585
586   for (i=0; i < fx->fc; i++) 
587     {
588       if (fx->fix[i].reloc)
589         { 
590           where = f - frag_now->fr_literal; 
591           if (fx->fix[i].size == 2)
592             where += 2;
593
594           if (fx->fix[i].reloc == BFD_RELOC_D10V_18)
595             fx->fix[i].operand |= 4096;   
596
597           fix_new_exp (frag_now,
598                        where,
599                        fx->fix[i].size,
600                        &(fx->fix[i].exp),
601                        fx->fix[i].pcrel,
602                        fx->fix[i].operand|2048);
603         }
604     }
605   fx->fc = 0;
606 }
607
608
609 /* write out a short form instruction by itself */
610 static void
611 write_1_short (opcode, insn, fx) 
612      struct d10v_opcode *opcode;
613      unsigned long insn;
614      Fixups *fx;
615 {
616   char *f = frag_more(4);
617   int i, where;
618
619   if (opcode->exec_type & PARONLY)
620     as_fatal ("Instruction must be executed in parallel with another instruction.");
621
622   /* the other container needs to be NOP */
623   /* according to 4.3.1: for FM=00, sub-instructions performed only
624      by IU cannot be encoded in L-container. */
625   if (opcode->unit == IU)
626     insn |= FM00 | (NOP << 15);         /* right container */
627   else
628     insn = FM00 | (insn << 15) | NOP;   /* left container */
629
630   number_to_chars_bigendian (f, insn, 4);
631   for (i=0; i < fx->fc; i++) 
632     {
633       if (fx->fix[i].reloc)
634         { 
635           where = f - frag_now->fr_literal; 
636           if (fx->fix[i].size == 2)
637             where += 2;
638
639           if (fx->fix[i].reloc == BFD_RELOC_D10V_18)
640             fx->fix[i].operand |= 4096;   
641
642           /* if it's an R reloc, we may have to switch it to L */
643           if ( (fx->fix[i].reloc == BFD_RELOC_D10V_10_PCREL_R) && (opcode->unit != IU) )
644             fx->fix[i].operand |= 1024;
645
646           fix_new_exp (frag_now,
647                        where, 
648                        fx->fix[i].size,
649                        &(fx->fix[i].exp),
650                        fx->fix[i].pcrel,
651                        fx->fix[i].operand|2048);
652         }
653     }
654   fx->fc = 0;
655 }
656
657 /* write out a short form instruction if possible */
658 /* return number of instructions not written out */
659 static int
660 write_2_short (opcode1, insn1, opcode2, insn2, exec_type, fx) 
661      struct d10v_opcode *opcode1, *opcode2;
662      unsigned long insn1, insn2;
663      int exec_type;
664      Fixups *fx;
665 {
666   unsigned long insn;
667   char *f;
668   int i,j, where;
669
670   if ( (exec_type != 1) && ((opcode1->exec_type & PARONLY)
671                         || (opcode2->exec_type & PARONLY)))
672     as_fatal("Instruction must be executed in parallel");
673   
674   if ( (opcode1->format & LONG_OPCODE) || (opcode2->format & LONG_OPCODE))
675     as_fatal ("Long instructions may not be combined.");
676
677   if(opcode1->exec_type & BRANCH_LINK && exec_type == 0)
678     {
679       /* Instructions paired with a subroutine call are executed before the
680          subroutine, so don't do these pairings unless explicitly requested.  */
681       write_1_short (opcode1, insn1, fx->next);
682       return (1);
683     }
684
685   switch (exec_type) 
686     {
687     case 0:     /* order not specified */
688       if ( Optimizing && parallel_ok (opcode1, insn1, opcode2, insn2, exec_type))
689         {
690           /* parallel */
691           if (opcode1->unit == IU)
692             insn = FM00 | (insn2 << 15) | insn1;
693           else if (opcode2->unit == MU)
694             insn = FM00 | (insn2 << 15) | insn1;
695           else
696             {
697               insn = FM00 | (insn1 << 15) | insn2;  
698               fx = fx->next;
699             }
700         }
701       else if (opcode1->unit == IU) 
702         {
703           /* reverse sequential */
704           insn = FM10 | (insn2 << 15) | insn1;
705         }
706       else
707         {
708           /* sequential */
709           insn = FM01 | (insn1 << 15) | insn2;
710           fx = fx->next;  
711         }
712       break;
713     case 1:     /* parallel */
714       if (opcode1->exec_type & SEQ || opcode2->exec_type & SEQ)
715         as_fatal ("One of these instructions may not be executed in parallel.");
716
717       if (opcode1->unit == IU)
718         {
719           if (opcode2->unit == IU)
720             as_fatal ("Two IU instructions may not be executed in parallel");
721           if (flag_warn_instructionswap)
722             as_warn ("Swapping instruction order");
723           insn = FM00 | (insn2 << 15) | insn1;
724         }
725       else if (opcode2->unit == MU)
726         {
727           if (opcode1->unit == MU)
728             as_fatal ("Two MU instructions may not be executed in parallel");
729           if (flag_warn_instructionswap)
730             as_warn ("Swapping instruction order");
731           insn = FM00 | (insn2 << 15) | insn1;
732         }
733       else
734         {
735           insn = FM00 | (insn1 << 15) | insn2;  
736           fx = fx->next;
737         }
738       break;
739     case 2:     /* sequential */
740       if (opcode1->unit == IU)
741         as_fatal ("IU instruction may not be in the left container");
742       insn = FM01 | (insn1 << 15) | insn2;  
743       fx = fx->next;
744       break;
745     case 3:     /* reverse sequential */
746       if (opcode2->unit == MU)
747         as_fatal ("MU instruction may not be in the right container");
748       insn = FM10 | (insn1 << 15) | insn2;  
749       fx = fx->next;
750       break;
751     default:
752       as_fatal("unknown execution type passed to write_2_short()");
753     }
754
755   f = frag_more(4);
756   number_to_chars_bigendian (f, insn, 4);
757
758   for (j=0; j<2; j++) 
759     {
760       for (i=0; i < fx->fc; i++) 
761         {
762           if (fx->fix[i].reloc)
763             {
764               where = f - frag_now->fr_literal; 
765               if (fx->fix[i].size == 2)
766                 where += 2;
767               
768               if ( (fx->fix[i].reloc == BFD_RELOC_D10V_10_PCREL_R) && (j == 0) )
769                 fx->fix[i].operand |= 1024;
770               
771               if (fx->fix[i].reloc == BFD_RELOC_D10V_18)
772                 fx->fix[i].operand |= 4096;       
773
774               fix_new_exp (frag_now,
775                            where, 
776                            fx->fix[i].size,
777                            &(fx->fix[i].exp),
778                            fx->fix[i].pcrel,
779                            fx->fix[i].operand|2048);
780             }
781         }
782       fx->fc = 0;
783       fx = fx->next;
784     }
785   return (0);
786 }
787
788
789 /* Check 2 instructions and determine if they can be safely */
790 /* executed in parallel.  Returns 1 if they can be.         */
791 static int
792 parallel_ok (op1, insn1, op2, insn2, exec_type)
793      struct d10v_opcode *op1, *op2;
794      unsigned long insn1, insn2;
795      int exec_type;
796 {
797   int i, j, flags, mask, shift, regno;
798   unsigned long ins, mod[2], used[2];
799   struct d10v_opcode *op;
800
801   if ((op1->exec_type & SEQ) != 0 || (op2->exec_type & SEQ) != 0
802       || (op1->exec_type & PAR) == 0 || (op2->exec_type & PAR) == 0
803       || (op1->unit == BOTH) || (op2->unit == BOTH)
804       || (op1->unit == IU && op2->unit == IU)
805       || (op1->unit == MU && op2->unit == MU))
806     return 0;
807
808   /* If the first instruction is a branch and this is auto parallazation,
809      don't combine with any second instruction.  */
810   if (exec_type == 0 && (op1->exec_type & BRANCH) != 0)
811     return 0;
812
813   /* The idea here is to create two sets of bitmasks (mod and used)
814      which indicate which registers are modified or used by each
815      instruction.  The operation can only be done in parallel if
816      instruction 1 and instruction 2 modify different registers, and
817      the first instruction does not modify registers that the second
818      is using (The second instruction can modify registers that the
819      first is using as they are only written back after the first
820      instruction has completed).  Accesses to control registers, PSW,
821      and memory are treated as accesses to a single register.  So if
822      both instructions write memory or if the first instruction writes
823      memory and the second reads, then they cannot be done in
824      parallel.  Likewise, if the first instruction mucks with the psw
825      and the second reads the PSW (which includes C, F0, and F1), then
826      they cannot operate safely in parallel. */
827
828   /* the bitmasks (mod and used) look like this (bit 31 = MSB) */
829   /* r0-r15       0-15  */
830   /* a0-a1        16-17 */
831   /* cr (not psw) 18    */
832   /* psw          19    */
833   /* mem          20    */
834
835   for (j=0;j<2;j++)
836     {
837       if (j == 0)
838         {
839           op = op1;
840           ins = insn1;
841         }
842       else
843         {
844           op = op2;
845           ins = insn2;
846         }
847       mod[j] = used[j] = 0;
848       if (op->exec_type & BRANCH_LINK)
849         mod[j] |= 1 << 13;
850
851       for (i = 0; op->operands[i]; i++)
852         {
853           flags = d10v_operands[op->operands[i]].flags;
854           shift = d10v_operands[op->operands[i]].shift;
855           mask = 0x7FFFFFFF >> (31 - d10v_operands[op->operands[i]].bits);
856           if (flags & OPERAND_REG)
857             {
858               regno = (ins >> shift) & mask;
859               if (flags & OPERAND_ACC)     
860                 regno += 16;
861               else if (flags & OPERAND_CONTROL) /* mvtc or mvfc */
862                 { 
863                   if (regno == 0)
864                     regno = 19;
865                   else
866                     regno = 18; 
867                 }
868               else if (flags & (OPERAND_FFLAG|OPERAND_CFLAG))  
869                 regno = 19;
870               
871               if ( flags & OPERAND_DEST )
872                 {
873                   mod[j] |= 1 << regno;
874                   if (flags & OPERAND_EVEN)
875                     mod[j] |= 1 << (regno + 1);
876                 }
877               else
878                 {
879                   used[j] |= 1 << regno ;
880                   if (flags & OPERAND_EVEN)
881                     used[j] |= 1 << (regno + 1);
882
883                   /* Auto inc/dec also modifies the register.  */
884                   if (op->operands[i+1] != 0
885                       && (d10v_operands[op->operands[i+1]].flags
886                           & (OPERAND_PLUS | OPERAND_MINUS)) != 0)
887                     mod[j] |= 1 << regno;
888                 }
889             }
890           else if (flags & OPERAND_ATMINUS)
891             {
892               /* SP implicitly used/modified */
893               mod[j] |= 1 << 15;
894               used[j] |= 1 << 15;
895             }
896         }
897       if (op->exec_type & RMEM)
898         used[j] |= 1 << 20;
899       else if (op->exec_type & WMEM)
900         mod[j] |= 1 << 20;
901       else if (op->exec_type & RF0)
902         used[j] |= 1 << 19;
903       else if (op->exec_type & WF0)
904         mod[j] |= 1 << 19;
905       else if (op->exec_type & WCAR)
906         mod[j] |= 1 << 19;
907     }
908   if ((mod[0] & mod[1]) == 0 && (mod[0] & used[1]) == 0)
909     return 1;
910   return 0;
911 }
912
913
914 /* This is the main entry point for the machine-dependent assembler.  str points to a
915    machine-dependent instruction.  This function is supposed to emit the frags/bytes 
916    it assembles to.  For the D10V, it mostly handles the special VLIW parsing and packing
917    and leaves the difficult stuff to do_assemble().
918  */
919
920 static unsigned long prev_insn;
921 static struct d10v_opcode *prev_opcode = 0;
922 static subsegT prev_subseg;
923 static segT prev_seg = 0;;
924
925 void
926 md_assemble (str)
927      char *str;
928 {
929   struct d10v_opcode *opcode;
930   unsigned long insn;
931   int extype=0;                 /* execution type; parallel, etc */
932   static int etype=0;           /* saved extype.  used for multiline instructions */
933   char *str2;
934
935   if (etype == 0)
936     {
937       /* look for the special multiple instruction separators */
938       str2 = strstr (str, "||");
939       if (str2) 
940         extype = 1;
941       else
942         {
943           str2 = strstr (str, "->");
944           if (str2) 
945             extype = 2;
946           else
947             {
948               str2 = strstr (str, "<-");
949               if (str2) 
950                 extype = 3;
951             }
952         }
953       /* str2 points to the separator, if one */
954       if (str2) 
955         {
956           *str2 = 0;
957           
958           /* if two instructions are present and we already have one saved
959              then first write it out */
960           d10v_cleanup();
961           
962           /* assemble first instruction and save it */
963           prev_insn = do_assemble (str, &prev_opcode);
964           if (prev_insn == -1)
965             as_fatal ("can't find opcode ");
966           fixups = fixups->next;
967           str = str2 + 2;
968         }
969     }
970
971   insn = do_assemble (str, &opcode);
972   if (insn == -1)
973     {
974       if (extype)
975         {
976           etype = extype;
977           return;
978         }
979       as_fatal ("can't find opcode ");
980     }
981
982   if (etype)
983     {
984       extype = etype;
985       etype = 0;
986     }
987
988   /* if this is a long instruction, write it and any previous short instruction */
989   if (opcode->format & LONG_OPCODE) 
990     {
991       if (extype) 
992         as_fatal("Unable to mix instructions as specified");
993       d10v_cleanup();
994       write_long (opcode, insn, fixups);
995       prev_opcode = NULL;
996       return;
997     }
998   
999   if (prev_opcode && prev_seg && ((prev_seg != now_seg) || (prev_subseg != now_subseg)))
1000     d10v_cleanup();
1001   
1002   if (prev_opcode && (write_2_short (prev_opcode, prev_insn, opcode, insn, extype, fixups) == 0)) 
1003     {
1004       /* no instructions saved */
1005       prev_opcode = NULL;
1006     }
1007   else
1008     {
1009       if (extype) 
1010         as_fatal("Unable to mix instructions as specified");
1011       /* save off last instruction so it may be packed on next pass */
1012       prev_opcode = opcode;
1013       prev_insn = insn;
1014       prev_seg = now_seg;
1015       prev_subseg = now_subseg;
1016       fixups = fixups->next;
1017     }
1018 }
1019
1020
1021 /* do_assemble assembles a single instruction and returns an opcode */
1022 /* it returns -1 (an invalid opcode) on error */
1023
1024 static unsigned long
1025 do_assemble (str, opcode) 
1026      char *str;
1027      struct d10v_opcode **opcode;
1028 {
1029   unsigned char *op_start, *save;
1030   unsigned char *op_end;
1031   char name[20];
1032   int nlen = 0;
1033   expressionS myops[6];
1034   unsigned long insn;
1035
1036   /* Drop leading whitespace */
1037   while (*str == ' ')
1038     str++;
1039
1040   /* find the opcode end */
1041   for (op_start = op_end = (unsigned char *) (str);
1042        *op_end
1043        && nlen < 20
1044        && !is_end_of_line[*op_end] && *op_end != ' ';
1045        op_end++)
1046     {
1047       name[nlen] = tolower(op_start[nlen]);
1048       nlen++;
1049     }
1050   name[nlen] = 0;
1051
1052   if (nlen == 0)
1053     return (-1);
1054   
1055   /* find the first opcode with the proper name */
1056   *opcode = (struct d10v_opcode *)hash_find (d10v_hash, name);
1057   if (*opcode == NULL)
1058       as_fatal ("unknown opcode: %s",name);
1059
1060   save = input_line_pointer;
1061   input_line_pointer = op_end;
1062   *opcode = find_opcode (*opcode, myops);
1063   if (*opcode == 0)
1064     return -1;
1065   input_line_pointer = save;
1066
1067   insn = build_insn ((*opcode), myops, 0); 
1068   return (insn);
1069 }
1070
1071 /* find_opcode() gets a pointer to an entry in the opcode table.       */
1072 /* It must look at all opcodes with the same name and use the operands */
1073 /* to choose the correct opcode. */
1074
1075 static struct d10v_opcode *
1076 find_opcode (opcode, myops)
1077      struct d10v_opcode *opcode;
1078      expressionS myops[];
1079 {
1080   int i, match, done;
1081   struct d10v_opcode *next_opcode;
1082
1083   /* get all the operands and save them as expressions */
1084   get_operands (myops);
1085
1086   /* now see if the operand is a fake.  If so, find the correct size */
1087   /* instruction, if possible */
1088   if (opcode->format == OPCODE_FAKE)
1089     {
1090       int opnum = opcode->operands[0];
1091       int flags;
1092                          
1093       if (myops[opnum].X_op == O_register)
1094         {
1095           myops[opnum].X_op = O_symbol;
1096           myops[opnum].X_add_symbol = symbol_find_or_make ((char *)myops[opnum].X_op_symbol);
1097           myops[opnum].X_add_number = 0;
1098           myops[opnum].X_op_symbol = NULL;
1099         }
1100
1101       next_opcode=opcode+1;
1102
1103       /* If the first operand is supposed to be a register, make sure
1104          we got a valid one.  */
1105       flags = d10v_operands[next_opcode->operands[0]].flags;
1106       if (flags & OPERAND_REG)
1107         {
1108           int X_op = myops[0].X_op;
1109           int num = myops[0].X_add_number;
1110
1111           if (X_op != O_register
1112               || (flags & OPERAND_ACC) != (num & OPERAND_ACC)
1113               || (flags & OPERAND_FFLAG) != (num & OPERAND_FFLAG)
1114               || (flags & OPERAND_CFLAG) != (num & OPERAND_CFLAG)
1115               || (flags & OPERAND_CONTROL) != (num & OPERAND_CONTROL))
1116             {
1117               as_bad ("bad opcode or operands");
1118               return 0;
1119             }
1120         }
1121
1122       if (myops[opnum].X_op == O_constant || (myops[opnum].X_op == O_symbol &&
1123           S_IS_DEFINED(myops[opnum].X_add_symbol) &&
1124           (S_GET_SEGMENT(myops[opnum].X_add_symbol) == now_seg)))
1125         {
1126           for (i=0; opcode->operands[i+1]; i++)
1127             {
1128               int bits = d10v_operands[next_opcode->operands[opnum]].bits;
1129               int flags = d10v_operands[next_opcode->operands[opnum]].flags;
1130               if (flags & OPERAND_ADDR)
1131                 bits += 2;
1132               if (myops[opnum].X_op == O_constant)
1133                 {
1134                   if (!check_range (myops[opnum].X_add_number, bits, flags))
1135                     return next_opcode;
1136                 }
1137               else
1138                 {
1139                   fragS *f;
1140                   long value;
1141                   /* calculate the current address by running through the previous frags */
1142                   /* and adding our current offset */
1143                   for (value = 0, f = frchain_now->frch_root; f; f = f->fr_next)
1144                     value += f->fr_fix + f->fr_offset;
1145
1146                   if (flags & OPERAND_ADDR)
1147                     value = S_GET_VALUE(myops[opnum].X_add_symbol) - value -
1148                       (obstack_next_free(&frchain_now->frch_obstack) - frag_now->fr_literal);
1149                   else
1150                     value = S_GET_VALUE(myops[opnum].X_add_symbol);
1151
1152                   if (myops[opnum].X_add_number == AT_WORD && 
1153                       myops[opnum].X_op_symbol == (struct symbol *)-1)
1154                     {
1155                       if (bits > 4)
1156                         {
1157                           bits += 2;
1158                           if (!check_range (value, bits, flags))
1159                             return next_opcode;
1160                         }
1161                     }
1162                   else if (!check_range (value, bits, flags))
1163                     return next_opcode;
1164                 }
1165               next_opcode++;
1166             }
1167           as_fatal ("value out of range");
1168         }
1169       else
1170         {
1171           /* not a constant, so use a long instruction */    
1172           return opcode+2;
1173         }
1174     }
1175   else
1176     {
1177       match = 0;
1178       /* now search the opcode table table for one with operands */
1179       /* that matches what we've got */
1180       while (!match)
1181         {
1182           match = 1;
1183           for (i = 0; opcode->operands[i]; i++) 
1184             {
1185               int flags = d10v_operands[opcode->operands[i]].flags;
1186               int X_op = myops[i].X_op;
1187               int num = myops[i].X_add_number;
1188
1189               if (X_op==0)
1190                 {
1191                   match=0;
1192                   break;
1193                 }
1194               
1195               if (flags & OPERAND_REG)
1196                 {
1197                   if ((X_op != O_register) ||
1198                       ((flags & OPERAND_ACC) != (num & OPERAND_ACC)) ||
1199                       ((flags & OPERAND_FFLAG) != (num & OPERAND_FFLAG)) ||
1200                       ((flags & OPERAND_CFLAG) != (num & OPERAND_CFLAG)) ||
1201                       ((flags & OPERAND_CONTROL) != (num & OPERAND_CONTROL)))
1202                     {
1203                       match=0;
1204                       break;
1205                     }
1206                 }
1207               
1208               if (((flags & OPERAND_MINUS) && ((X_op != O_absent) || (num != OPERAND_MINUS))) ||
1209                   ((flags & OPERAND_PLUS) && ((X_op != O_absent) || (num != OPERAND_PLUS))) ||
1210                   ((flags & OPERAND_ATMINUS) && ((X_op != O_absent) || (num != OPERAND_ATMINUS))) ||
1211                   ((flags & OPERAND_ATPAR) && ((X_op != O_absent) || (num != OPERAND_ATPAR))) ||
1212                   ((flags & OPERAND_ATSIGN) && ((X_op != O_absent) || (num != OPERAND_ATSIGN))))
1213                 {
1214                   match=0;
1215                   break;
1216                 }             
1217             }
1218           /* we're only done if the operands matched so far AND there
1219              are no more to check */
1220           if (match && myops[i].X_op==0) 
1221             break;
1222           else
1223             match = 0;
1224
1225           next_opcode = opcode+1;
1226           if (next_opcode->opcode == 0) 
1227             break;
1228           if (strcmp(next_opcode->name, opcode->name))
1229             break;
1230           opcode = next_opcode;
1231         }
1232     }
1233
1234   if (!match)  
1235     {
1236       as_bad ("bad opcode or operands");
1237       return (0);
1238     }
1239
1240   /* Check that all registers that are required to be even are. */
1241   /* Also, if any operands were marked as registers, but were really symbols */
1242   /* fix that here. */
1243   for (i=0; opcode->operands[i]; i++) 
1244     {
1245       if ((d10v_operands[opcode->operands[i]].flags & OPERAND_EVEN) &&
1246           (myops[i].X_add_number & 1)) 
1247         as_fatal("Register number must be EVEN");
1248       if (myops[i].X_op == O_register)
1249         {
1250           if (!(d10v_operands[opcode->operands[i]].flags & OPERAND_REG)) 
1251             {
1252               myops[i].X_op = O_symbol;
1253               myops[i].X_add_symbol = symbol_find_or_make ((char *)myops[i].X_op_symbol);
1254               myops[i].X_add_number = 0;
1255               myops[i].X_op_symbol = NULL;
1256             }
1257         }
1258     }
1259   return opcode;
1260 }
1261
1262 /* if while processing a fixup, a reloc really needs to be created */
1263 /* then it is done here */
1264                  
1265 arelent *
1266 tc_gen_reloc (seg, fixp)
1267      asection *seg;
1268      fixS *fixp;
1269 {
1270   arelent *reloc;
1271   reloc = (arelent *) xmalloc (sizeof (arelent));
1272   reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
1273   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1274   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1275   if (reloc->howto == (reloc_howto_type *) NULL)
1276     {
1277       as_bad_where (fixp->fx_file, fixp->fx_line,
1278                     "reloc %d not supported by object file format", (int)fixp->fx_r_type);
1279       return NULL;
1280     }
1281   reloc->addend = fixp->fx_addnumber;
1282   return reloc;
1283 }
1284
1285 int
1286 md_estimate_size_before_relax (fragp, seg)
1287      fragS *fragp;
1288      asection *seg;
1289 {
1290   abort ();
1291   return 0;
1292
1293
1294 long
1295 md_pcrel_from_section (fixp, sec)
1296      fixS *fixp;
1297      segT sec;
1298 {
1299   if (fixp->fx_addsy != (symbolS *)NULL && (!S_IS_DEFINED (fixp->fx_addsy) ||
1300       (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
1301     return 0;
1302   return fixp->fx_frag->fr_address + fixp->fx_where;
1303 }
1304
1305 int
1306 md_apply_fix3 (fixp, valuep, seg)
1307      fixS *fixp;
1308      valueT *valuep;
1309      segT seg;
1310 {
1311   char *where;
1312   unsigned long insn;
1313   long value;
1314   int op_type;
1315   int left=0;
1316
1317   if (fixp->fx_addsy == (symbolS *) NULL)
1318     {
1319       value = *valuep;
1320       fixp->fx_done = 1;
1321     }
1322   else if (fixp->fx_pcrel)
1323     value = *valuep;
1324   else
1325     {
1326       value = fixp->fx_offset;
1327       if (fixp->fx_subsy != (symbolS *) NULL)
1328         {
1329           if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
1330             value -= S_GET_VALUE (fixp->fx_subsy);
1331           else
1332             {
1333               /* We don't actually support subtracting a symbol.  */
1334               as_bad_where (fixp->fx_file, fixp->fx_line,
1335                             "expression too complex");
1336             }
1337         }
1338     }
1339
1340   op_type = fixp->fx_r_type;
1341   if (op_type & 2048)
1342     {
1343       op_type -= 2048;
1344       if (op_type & 1024)
1345         {
1346           op_type -= 1024;
1347           fixp->fx_r_type = BFD_RELOC_D10V_10_PCREL_L;
1348           left = 1;
1349         }
1350       else if (op_type & 4096)
1351         {
1352           op_type -= 4096;
1353           fixp->fx_r_type = BFD_RELOC_D10V_18;
1354         }
1355       else
1356         fixp->fx_r_type = get_reloc((struct d10v_operand *)&d10v_operands[op_type]); 
1357     }
1358
1359   /* Fetch the instruction, insert the fully resolved operand
1360      value, and stuff the instruction back again.  */
1361   where = fixp->fx_frag->fr_literal + fixp->fx_where;
1362   insn = bfd_getb32 ((unsigned char *) where);
1363
1364   switch (fixp->fx_r_type)
1365     {
1366     case BFD_RELOC_D10V_10_PCREL_L:
1367     case BFD_RELOC_D10V_10_PCREL_R:
1368     case BFD_RELOC_D10V_18_PCREL:
1369     case BFD_RELOC_D10V_18:
1370       /* instruction addresses are always right-shifted by 2 */
1371       value >>= 2;
1372       if (fixp->fx_size == 2)
1373         bfd_putb16 ((bfd_vma) value, (unsigned char *) where);
1374       else
1375         {
1376           insn = d10v_insert_operand (insn, op_type, (offsetT)value, left, fixp);
1377           bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);  
1378         }
1379       break;
1380     case BFD_RELOC_32:
1381       bfd_putb32 ((bfd_vma) value, (unsigned char *) where);
1382       break;
1383     case BFD_RELOC_16:
1384       bfd_putb16 ((bfd_vma) value, (unsigned char *) where);
1385       break;
1386     default:
1387       as_fatal ("line %d: unknown relocation type: 0x%x",fixp->fx_line,fixp->fx_r_type);
1388     }
1389   return 0;
1390 }
1391
1392 /* d10v_cleanup() is called after the assembler has finished parsing the input 
1393    file or after a label is defined.  Because the D10V assembler sometimes saves short 
1394    instructions to see if it can package them with the next instruction, there may
1395    be a short instruction that still needs written.  */
1396 int
1397 d10v_cleanup ()
1398 {
1399   segT seg;
1400   subsegT subseg;
1401
1402   if (prev_opcode)
1403     {
1404       seg = now_seg;
1405       subseg = now_subseg;
1406       subseg_set (prev_seg, prev_subseg);
1407       write_1_short (prev_opcode, prev_insn, fixups->next);
1408       subseg_set (seg, subseg);
1409       prev_opcode = NULL;
1410     }
1411   return 1;
1412 }
1413
1414 /* Like normal .word, except support @word */
1415 /* clobbers input_line_pointer, checks end-of-line. */
1416 static void
1417 d10v_dot_word (nbytes)
1418      register int nbytes;       /* 1=.byte, 2=.word, 4=.long */
1419 {
1420   expressionS exp;
1421   bfd_reloc_code_real_type reloc;
1422   char *p;
1423   int offset;
1424
1425   if (is_it_end_of_statement ())
1426     {
1427       demand_empty_rest_of_line ();
1428       return;
1429     }
1430
1431   do
1432     {
1433       expression (&exp);
1434       if (!strncasecmp (input_line_pointer, "@word", 5))
1435         {
1436           exp.X_add_number = 0;
1437           input_line_pointer += 5;
1438         
1439           p = frag_more (2);
1440           fix_new_exp (frag_now, p - frag_now->fr_literal, 2, 
1441                        &exp, 0, BFD_RELOC_D10V_18);
1442         }
1443       else
1444         emit_expr (&exp, 2);
1445     }
1446   while (*input_line_pointer++ == ',');
1447
1448   input_line_pointer--;         /* Put terminator back into stream. */
1449   demand_empty_rest_of_line ();
1450 }
1451
1452
1453 /* Mitsubishi asked that we support some old syntax that apparently */
1454 /* had immediate operands starting with '#'.  This is in some of their */
1455 /* sample code but is not documented (although it appears in some  */
1456 /* examples in their assembler manual). For now, we'll solve this */
1457 /* compatibility problem by simply ignoring any '#' at the beginning */
1458 /* of an operand. */
1459
1460 /* operands that begin with '#' should fall through to here */
1461 /* from expr.c */
1462
1463 void 
1464 md_operand (expressionP)
1465      expressionS *expressionP;
1466 {
1467   if (*input_line_pointer == '#')
1468     {
1469       input_line_pointer++;
1470       expression (expressionP);
1471     }
1472 }
1473