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