]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/tc-d30v.c
Fri Feb 21 14:34:31 1997 Martin M. Hunt <hunt@pizza.cygnus.com>
[thirdparty/binutils-gdb.git] / gas / config / tc-d30v.c
1 /* tc-d30v.c -- Assembler code for the Mitsubishi D30V
2
3 Copyright (C) 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/d30v.h"
27
28 const char comment_chars[] = ";";
29 const char line_comment_chars[] = "#";
30 const char line_separator_chars[] = "";
31 const char *md_shortopts = "O";
32 const char EXP_CHARS[] = "eE";
33 const char FLT_CHARS[] = "dD";
34
35 int Optimizing = 0;
36
37 /* fixups */
38 #define MAX_INSN_FIXUPS (5)
39 struct d30v_fixup
40 {
41 expressionS exp;
42 int operand;
43 int pcrel;
44 int size;
45 bfd_reloc_code_real_type reloc;
46 };
47
48 typedef struct _fixups
49 {
50 int fc;
51 struct d30v_fixup fix[MAX_INSN_FIXUPS];
52 struct _fixups *next;
53 } Fixups;
54
55 static Fixups FixUps[2];
56 static Fixups *fixups;
57
58 /* local functions */
59 static int reg_name_search PARAMS ((char *name));
60 static int register_name PARAMS ((expressionS *expressionP));
61 static int check_range PARAMS ((unsigned long num, int bits, int flags));
62 static int postfix PARAMS ((char *p));
63 static bfd_reloc_code_real_type get_reloc PARAMS ((struct d30v_operand *op, int rel_flag));
64 static int get_operands PARAMS ((expressionS exp[], int cmp_hack));
65 static struct d30v_format *find_format PARAMS ((struct d30v_opcode *opcode, expressionS ops[],
66 int cmp_hack));
67 static long long build_insn PARAMS ((struct d30v_insn *opcode, expressionS *opers));
68 static void write_long PARAMS ((struct d30v_insn *opcode, long long insn, Fixups *fx));
69 static void write_1_short PARAMS ((struct d30v_insn *opcode, long long insn, Fixups *fx));
70 static int write_2_short PARAMS ((struct d30v_insn *opcode1, long long insn1,
71 struct d30v_insn *opcode2, long long insn2, int exec_type, Fixups *fx));
72 static long long do_assemble PARAMS ((char *str, struct d30v_insn *opcode));
73 static unsigned long d30v_insert_operand PARAMS (( unsigned long insn, int op_type,
74 offsetT value, int left, fixS *fix));
75 static int parallel_ok PARAMS ((struct d30v_insn *opcode1, unsigned long insn1,
76 struct d30v_insn *opcode2, unsigned long insn2,
77 int exec_type));
78 static void d30v_number_to_chars PARAMS ((char *buf, long long value, int nbytes));
79
80 struct option md_longopts[] = {
81 {NULL, no_argument, NULL, 0}
82 };
83 size_t md_longopts_size = sizeof(md_longopts);
84
85
86 /* The target specific pseudo-ops which we support. */
87 const pseudo_typeS md_pseudo_table[] =
88 {
89 { NULL, NULL, 0 }
90 };
91
92 /* Opcode hash table. */
93 static struct hash_control *d30v_hash;
94
95 /* reg_name_search does a binary search of the pre_defined_registers
96 array to see if "name" is a valid regiter name. Returns the register
97 number from the array on success, or -1 on failure. */
98
99 static int
100 reg_name_search (name)
101 char *name;
102 {
103 int middle, low, high;
104 int cmp;
105
106 low = 0;
107 high = reg_name_cnt() - 1;
108
109 do
110 {
111 middle = (low + high) / 2;
112 cmp = strcasecmp (name, pre_defined_registers[middle].name);
113 if (cmp < 0)
114 high = middle - 1;
115 else if (cmp > 0)
116 low = middle + 1;
117 else
118 return pre_defined_registers[middle].value;
119 }
120 while (low <= high);
121 return -1;
122 }
123
124 /* register_name() checks the string at input_line_pointer
125 to see if it is a valid register name */
126
127 static int
128 register_name (expressionP)
129 expressionS *expressionP;
130 {
131 int reg_number;
132 char c, *p = input_line_pointer;
133
134 while (*p && *p!='\n' && *p!='\r' && *p !=',' && *p!=' ' && *p!=')')
135 p++;
136
137 c = *p;
138 if (c)
139 *p++ = 0;
140
141 /* look to see if it's in the register table */
142 reg_number = reg_name_search (input_line_pointer);
143 if (reg_number >= 0)
144 {
145 expressionP->X_op = O_register;
146 /* temporarily store a pointer to the string here */
147 expressionP->X_op_symbol = (struct symbol *)input_line_pointer;
148 expressionP->X_add_number = reg_number;
149 input_line_pointer = p;
150 return 1;
151 }
152 if (c)
153 *(p-1) = c;
154 return 0;
155 }
156
157
158 static int
159 check_range (num, bits, flags)
160 unsigned long num;
161 int bits;
162 int flags;
163 {
164 long min, max, bit1;
165 int retval=0;
166
167 /* don't bother checking 32-bit values */
168 if (bits == 32)
169 return 0;
170
171 if (flags & OPERAND_SIGNED)
172 {
173 max = (1 << (bits - 1))-1;
174 min = - (1 << (bits - 1));
175 if (((long)num > max) || ((long)num < min))
176 retval = 1;
177 }
178 else
179 {
180 max = (1 << bits) - 1;
181 min = 0;
182 if ((num > max) || (num < min))
183 retval = 1;
184 }
185 return retval;
186 }
187
188
189 void
190 md_show_usage (stream)
191 FILE *stream;
192 {
193 fprintf(stream, "D30V options:\n\
194 -O optimize. Will do some operations in parallel.\n");
195 }
196
197 int
198 md_parse_option (c, arg)
199 int c;
200 char *arg;
201 {
202 switch (c)
203 {
204 case 'O':
205 /* Optimize. Will attempt to parallelize operations */
206 Optimizing = 1;
207 break;
208 default:
209 return 0;
210 }
211 return 1;
212 }
213
214 symbolS *
215 md_undefined_symbol (name)
216 char *name;
217 {
218 return 0;
219 }
220
221 /* Turn a string in input_line_pointer into a floating point constant of type
222 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
223 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
224 */
225 char *
226 md_atof (type, litP, sizeP)
227 int type;
228 char *litP;
229 int *sizeP;
230 {
231 int prec;
232 LITTLENUM_TYPE words[4];
233 char *t;
234 int i;
235
236 switch (type)
237 {
238 case 'f':
239 prec = 2;
240 break;
241 case 'd':
242 prec = 4;
243 break;
244 default:
245 *sizeP = 0;
246 return "bad call to md_atof";
247 }
248
249 t = atof_ieee (input_line_pointer, type, words);
250 if (t)
251 input_line_pointer = t;
252
253 *sizeP = prec * 2;
254
255 for (i = 0; i < prec; i++)
256 {
257 md_number_to_chars (litP, (valueT) words[i], 2);
258 litP += 2;
259 }
260 return NULL;
261 }
262
263 void
264 md_convert_frag (abfd, sec, fragP)
265 bfd *abfd;
266 asection *sec;
267 fragS *fragP;
268 {
269 abort ();
270 }
271
272 valueT
273 md_section_align (seg, addr)
274 asection *seg;
275 valueT addr;
276 {
277 int align = bfd_get_section_alignment (stdoutput, seg);
278 return ((addr + (1 << align) - 1) & (-1 << align));
279 }
280
281
282 void
283 md_begin ()
284 {
285 struct d30v_opcode *opcode;
286 d30v_hash = hash_new();
287
288 /* Insert opcode names into a hash table. */
289 for (opcode = (struct d30v_opcode *)d30v_opcode_table; opcode->name; opcode++)
290 hash_insert (d30v_hash, opcode->name, (char *) opcode);
291
292 fixups = &FixUps[0];
293 FixUps[0].next = &FixUps[1];
294 FixUps[1].next = &FixUps[0];
295 }
296
297
298 /* this function removes the postincrement or postdecrement
299 operator ( '+' or '-' ) from an expression */
300
301 static int postfix (p)
302 char *p;
303 {
304 while (*p != '-' && *p != '+')
305 {
306 if (*p==0 || *p=='\n' || *p=='\r')
307 break;
308 p++;
309 }
310
311 if (*p == '-')
312 {
313 *p = ' ';
314 return (-1);
315 }
316 if (*p == '+')
317 {
318 *p = ' ';
319 return (1);
320 }
321
322 return (0);
323 }
324
325
326 static bfd_reloc_code_real_type
327 get_reloc (op, rel_flag)
328 struct d30v_operand *op;
329 int rel_flag;
330 {
331 switch (op->bits)
332 {
333 case 6:
334 return BFD_RELOC_D30V_6;
335 case 12:
336 if (!(op->flags & OPERAND_SHIFT))
337 as_warn("unexpected 12-bit reloc type");
338 if (rel_flag == RELOC_PCREL)
339 return BFD_RELOC_D30V_15_PCREL;
340 else
341 return BFD_RELOC_D30V_15;
342 case 18:
343 if (!(op->flags & OPERAND_SHIFT))
344 as_warn("unexpected 18-bit reloc type");
345 if (rel_flag == RELOC_PCREL)
346 return BFD_RELOC_D30V_21_PCREL;
347 else
348 return BFD_RELOC_D30V_21;
349 case 32:
350 if (rel_flag == RELOC_PCREL)
351 return BFD_RELOC_D30V_32_PCREL;
352 else
353 return BFD_RELOC_D30V_32;
354 default:
355 return 0;
356 }
357 }
358
359 /* get_operands parses a string of operands and returns
360 an array of expressions */
361
362 static int
363 get_operands (exp, cmp_hack)
364 expressionS exp[];
365 int cmp_hack;
366 {
367 char *p = input_line_pointer;
368 int numops = 0;
369 int post = 0;
370
371 if (cmp_hack)
372 {
373 exp[numops].X_op = O_absent;
374 exp[numops++].X_add_number = cmp_hack - 1;
375 }
376
377 while (*p)
378 {
379 while (*p == ' ' || *p == '\t' || *p == ',')
380 p++;
381 if (*p==0 || *p=='\n' || *p=='\r')
382 break;
383
384 if (*p == '@')
385 {
386 p++;
387 exp[numops].X_op = O_absent;
388 if (*p == '(')
389 {
390 p++;
391 exp[numops].X_add_number = OPERAND_ATPAR;
392 post = postfix (p);
393 }
394 else if (*p == '-')
395 {
396 p++;
397 exp[numops].X_add_number = OPERAND_ATMINUS;
398 }
399 else
400 {
401 exp[numops].X_add_number = OPERAND_ATSIGN;
402 post = postfix (p);
403 }
404 numops++;
405 continue;
406 }
407
408 if (*p == ')')
409 {
410 /* just skip the trailing paren */
411 p++;
412 continue;
413 }
414
415 input_line_pointer = p;
416
417 /* check to see if it might be a register name */
418 if (!register_name (&exp[numops]))
419 {
420 /* parse as an expression */
421 expression (&exp[numops]);
422 }
423
424 if (exp[numops].X_op == O_illegal)
425 as_bad ("illegal operand");
426 else if (exp[numops].X_op == O_absent)
427 as_bad ("missing operand");
428
429 numops++;
430 p = input_line_pointer;
431
432 switch (post)
433 {
434 case -1: /* postdecrement mode */
435 exp[numops].X_op = O_absent;
436 exp[numops++].X_add_number = OPERAND_MINUS;
437 break;
438 case 1: /* postincrement mode */
439 exp[numops].X_op = O_absent;
440 exp[numops++].X_add_number = OPERAND_PLUS;
441 break;
442 }
443 post = 0;
444 }
445
446 exp[numops].X_op = 0;
447 return (numops);
448 }
449
450 /* build_insn generates the instruction. It does everything */
451 /* but write the FM bits. */
452
453 static long long
454 build_insn (opcode, opers)
455 struct d30v_insn *opcode;
456 expressionS *opers;
457 {
458 int i, length, bits, shift, flags, format;
459 unsigned int number, id=0;
460 long long insn;
461 struct d30v_opcode *op = opcode->op;
462 struct d30v_format *form = opcode->form;
463
464 /* printf("ecc=%x op1=%x op2=%x mod=%x\n",opcode->ecc,op->op1,op->op2,form->modifier); */
465 insn = opcode->ecc << 28 | op->op1 << 25 | op->op2 << 20 | form->modifier << 18;
466 /* printf("insn=%llx\n",insn); */
467 for (i=0; form->operands[i]; i++)
468 {
469 flags = d30v_operand_table[form->operands[i]].flags;
470
471
472 /* must be a register or number */
473 if (!(flags & OPERAND_REG) && !(flags & OPERAND_NUM) &&
474 !(flags & OPERAND_NAME) && !(flags & OPERAND_SPECIAL))
475 continue;
476
477 bits = d30v_operand_table[form->operands[i]].bits;
478 length = d30v_operand_table[form->operands[i]].length;
479 shift = 12 - d30v_operand_table[form->operands[i]].position;
480 number = opers[i].X_add_number;
481 if (flags & OPERAND_REG)
482 {
483 /* now check for mvfsys or mvtsys control registers */
484 if (flags & OPERAND_CONTROL && (number & 0x3f) > MAX_CONTROL_REG)
485 {
486 /* PSWL or PSWH */
487 id = (number & 0x3f) - MAX_CONTROL_REG;
488 number = 1;
489 }
490 else if (number & OPERAND_FLAG)
491 {
492 id = 3; /* number is a flag register */
493 }
494 number &= 0x3F;
495 }
496 else if (flags & OPERAND_SPECIAL)
497 {
498 number = id;
499 }
500
501
502 if (opers[i].X_op != O_register && opers[i].X_op != O_constant && !(flags & OPERAND_NAME))
503 {
504 /* now create a fixup */
505
506 if (fixups->fc >= MAX_INSN_FIXUPS)
507 as_fatal ("too many fixups");
508
509 fixups->fix[fixups->fc].reloc =
510 get_reloc((struct d30v_operand *)&d30v_operand_table[form->operands[i]], op->reloc_flag);
511 fixups->fix[fixups->fc].size = 4;
512 fixups->fix[fixups->fc].exp = opers[i];
513 fixups->fix[fixups->fc].operand = form->operands[i];
514 fixups->fix[fixups->fc].pcrel = op->reloc_flag;
515 (fixups->fc)++;
516 }
517
518 /* truncate to the proper number of bits */
519 /*
520 if ((opers[i].X_op == O_constant) && check_range (number, bits, flags))
521 as_bad("operand out of range: %d",number);
522 number &= 0x7FFFFFFF >> (31 - bits);
523 */
524
525 if (bits == 32)
526 {
527 /* it's a LONG instruction */
528 insn |= (number >> 26); /* top 6 bits */
529 insn <<= 32; /* shift the first word over */
530 insn |= ((number & 0x03FC0000) << 2); /* next 8 bits */
531 insn |= number & 0x0003FFFF; /* bottom 18 bits */
532 }
533 else
534 insn |= number << shift;
535 }
536 return insn;
537 }
538
539
540 /* write out a long form instruction */
541 static void
542 write_long (opcode, insn, fx)
543 struct d30v_insn *opcode;
544 long long insn;
545 Fixups *fx;
546 {
547 int i, where;
548 char *f = frag_more(8);
549
550 insn |= FM11;
551 d30v_number_to_chars (f, insn, 8);
552
553 for (i=0; i < fx->fc; i++)
554 {
555 if (fx->fix[i].reloc)
556 {
557 where = f - frag_now->fr_literal;
558 fix_new_exp (frag_now,
559 where,
560 fx->fix[i].size,
561 &(fx->fix[i].exp),
562 fx->fix[i].pcrel,
563 fx->fix[i].reloc);
564 }
565 }
566 fx->fc = 0;
567 }
568
569
570 /* write out a short form instruction by itself */
571 static void
572 write_1_short (opcode, insn, fx)
573 struct d30v_insn *opcode;
574 long long insn;
575 Fixups *fx;
576 {
577 char *f = frag_more(8);
578 int i, where;
579
580 /* the other container needs to be NOP */
581 /* according to 4.3.1: for FM=00, sub-instructions performed only
582 by IU cannot be encoded in L-container. */
583 if (opcode->op->unit == IU)
584 insn |= FM00 | ((long long)NOP << 32); /* right container */
585 else
586 insn = FM00 | (insn << 32) | (long long)NOP; /* left container */
587
588 d30v_number_to_chars (f, insn, 8);
589
590 for (i=0; i < fx->fc; i++)
591 {
592 if (fx->fix[i].reloc)
593 {
594 where = f - frag_now->fr_literal;
595 fix_new_exp (frag_now,
596 where,
597 fx->fix[i].size,
598 &(fx->fix[i].exp),
599 fx->fix[i].pcrel,
600 fx->fix[i].reloc);
601 }
602 }
603 fx->fc = 0;
604 }
605
606 /* write out a short form instruction if possible */
607 /* return number of instructions not written out */
608 static int
609 write_2_short (opcode1, insn1, opcode2, insn2, exec_type, fx)
610 struct d30v_insn *opcode1, *opcode2;
611 long long insn1, insn2;
612 int exec_type;
613 Fixups *fx;
614 {
615 long long insn;
616 char *f;
617 int i,j, where;
618
619 if(exec_type != 1 && (opcode1->op->flags_used == FLAG_JSR))
620 {
621 /* subroutines must be called from 32-bit boundaries */
622 /* so the return address will be correct */
623 write_1_short (opcode1, insn1, fx->next);
624 return (1);
625 }
626
627 switch (exec_type)
628 {
629 case 0: /* order not specified */
630 if ( Optimizing && parallel_ok (opcode1, insn1, opcode2, insn2, exec_type))
631 {
632 /* parallel */
633 if (opcode1->op->unit == IU)
634 insn = FM00 | (insn2 << 32) | insn1;
635 else if (opcode2->op->unit == MU)
636 insn = FM00 | (insn2 << 32) | insn1;
637 else
638 {
639 insn = FM00 | (insn1 << 32) | insn2;
640 fx = fx->next;
641 }
642 }
643 else if (opcode1->op->unit == IU)
644 {
645 /* reverse sequential */
646 insn = FM10 | (insn2 << 32) | insn1;
647 }
648 else
649 {
650 /* sequential */
651 insn = FM01 | (insn1 << 32) | insn2;
652 fx = fx->next;
653 }
654 break;
655 case 1: /* parallel */
656 if (opcode1->op->unit == IU)
657 {
658 if (opcode2->op->unit == IU)
659 as_fatal ("Two IU instructions may not be executed in parallel");
660 as_warn ("Swapping instruction order");
661 insn = FM00 | (insn2 << 32) | insn1;
662 }
663 else if (opcode2->op->unit == MU)
664 {
665 if (opcode1->op->unit == MU)
666 as_fatal ("Two MU instructions may not be executed in parallel");
667 as_warn ("Swapping instruction order");
668 insn = FM00 | (insn2 << 32) | insn1;
669 }
670 else
671 {
672 insn = FM00 | (insn1 << 32) | insn2;
673 fx = fx->next;
674 }
675 break;
676 case 2: /* sequential */
677 if (opcode1->op->unit == IU)
678 as_fatal ("IU instruction may not be in the left container");
679 insn = FM01 | (insn1 << 32) | insn2;
680 fx = fx->next;
681 break;
682 case 3: /* reverse sequential */
683 if (opcode2->op->unit == MU)
684 as_fatal ("MU instruction may not be in the right container");
685 insn = FM10 | (insn1 << 32) | insn2;
686 fx = fx->next;
687 break;
688 default:
689 as_fatal("unknown execution type passed to write_2_short()");
690 }
691
692 /* printf("writing out %llx\n",insn); */
693 f = frag_more(8);
694 d30v_number_to_chars (f, insn, 8);
695
696 for (j=0; j<2; j++)
697 {
698 for (i=0; i < fx->fc; i++)
699 {
700 if (fx->fix[i].reloc)
701 {
702 where = (f - frag_now->fr_literal) + 4*j;
703
704 fix_new_exp (frag_now,
705 where,
706 fx->fix[i].size,
707 &(fx->fix[i].exp),
708 fx->fix[i].pcrel,
709 fx->fix[i].reloc);
710 }
711 }
712 fx->fc = 0;
713 fx = fx->next;
714 }
715 return (0);
716 }
717
718
719 /* Check 2 instructions and determine if they can be safely */
720 /* executed in parallel. Returns 1 if they can be. */
721 static int
722 parallel_ok (op1, insn1, op2, insn2, exec_type)
723 struct d30v_insn *op1, *op2;
724 unsigned long insn1, insn2;
725 int exec_type;
726 {
727 int i, j, flags, mask, shift, regno, bits;
728 unsigned long ins, mod_reg[2][3], used_reg[2][3];
729 struct d30v_format *f;
730 struct d30v_opcode *op;
731
732 /* section 4.3: both instructions must not be IU or MU only */
733 if ((op1->op->unit == IU && op2->op->unit == IU)
734 || (op1->op->unit == MU && op2->op->unit == MU))
735 return 0;
736
737 /*
738 [0] r0-r31
739 [1] r32-r63
740 [2] a0, a1
741 */
742
743 for (j = 0; j < 2; j++)
744 {
745 if (j == 0)
746 {
747 f = op1->form;
748 op = op1->op;
749 ins = insn1;
750 }
751 else
752 {
753 f = op2->form;
754 op = op2->op;
755 ins = insn2;
756 }
757 mod_reg[j][0] = mod_reg[j][1] = 0;
758 mod_reg[j][2] = op->flags_set;
759 used_reg[j][0] = used_reg[j][1] = 0;
760 used_reg[j][2] = op->flags_used;
761 for (i = 0; f->operands[i]; i++)
762 {
763 flags = d30v_operand_table[f->operands[i]].flags;
764 shift = 12 - d30v_operand_table[f->operands[i]].position;
765 bits = d30v_operand_table[f->operands[i]].bits;
766 if (bits == 32)
767 mask = 0xffffffff;
768 else
769 mask = 0x7FFFFFFF >> (31 - bits);
770 if (flags & OPERAND_REG)
771 {
772 regno = (ins >> shift) & mask;
773 if (flags & OPERAND_DEST)
774 {
775 if (flags & OPERAND_ACC)
776 mod_reg[j][2] = 1 << (regno+16);
777 else if (flags & OPERAND_FLAG)
778 mod_reg[j][2] = 1 << regno;
779 else if (!(flags & OPERAND_CONTROL))
780 {
781 if (regno >= 32)
782 mod_reg[j][1] = 1 << (regno - 32);
783 else
784 mod_reg[j][0] = 1 << regno;
785 }
786 }
787 else
788 {
789 if (flags & OPERAND_ACC)
790 used_reg[j][2] = 1 << (regno+16);
791 else if (flags & OPERAND_FLAG)
792 used_reg[j][2] = 1 << regno;
793 else if (!(flags & OPERAND_CONTROL))
794 {
795 if (regno >= 32)
796 used_reg[j][1] = 1 << (regno - 32);
797 else
798 used_reg[j][0] = 1 << regno;
799 }
800 }
801 }
802 }
803 }
804
805 for(j = 0; j < 3; j++)
806 if ((mod_reg[0][j] & mod_reg[1][j])
807 || (mod_reg[0][j] & used_reg[1][j])
808 || (mod_reg[1][j] & used_reg[0][j]))
809 return 0;
810
811 return 1;
812 }
813
814
815
816 /* This is the main entry point for the machine-dependent assembler. str points to a
817 machine-dependent instruction. This function is supposed to emit the frags/bytes
818 it assembles to. For the D30V, it mostly handles the special VLIW parsing and packing
819 and leaves the difficult stuff to do_assemble().
820 */
821
822 static long long prev_insn = -1;
823 static struct d30v_insn prev_opcode;
824 static subsegT prev_subseg;
825 static segT prev_seg = 0;
826
827 void
828 md_assemble (str)
829 char *str;
830 {
831 struct d30v_insn opcode;
832 long long insn;
833 int extype=0; /* execution type; parallel, etc */
834 static int etype=0; /* saved extype. used for multiline instructions */
835 char *str2;
836
837 if (etype == 0)
838 {
839 /* look for the special multiple instruction separators */
840 str2 = strstr (str, "||");
841 if (str2)
842 extype = 1;
843 else
844 {
845 str2 = strstr (str, "->");
846 if (str2)
847 extype = 2;
848 else
849 {
850 str2 = strstr (str, "<-");
851 if (str2)
852 extype = 3;
853 }
854 }
855 /* str2 points to the separator, if one */
856 if (str2)
857 {
858 *str2 = 0;
859
860 /* if two instructions are present and we already have one saved
861 then first write it out */
862 d30v_cleanup();
863
864 /* assemble first instruction and save it */
865 prev_insn = do_assemble (str, &prev_opcode);
866 if (prev_insn == -1)
867 as_fatal ("can't find opcode ");
868 fixups = fixups->next;
869 str = str2 + 2;
870 }
871 }
872
873 insn = do_assemble (str, &opcode);
874 if (insn == -1)
875 {
876 if (extype)
877 {
878 etype = extype;
879 return;
880 }
881 as_fatal ("can't find opcode ");
882 }
883
884 if (etype)
885 {
886 extype = etype;
887 etype = 0;
888 }
889
890 /* if this is a long instruction, write it and any previous short instruction */
891 if (opcode.form->form >= LONG)
892 {
893 if (extype)
894 as_fatal("Unable to mix instructions as specified");
895 d30v_cleanup();
896 write_long (&opcode, insn, fixups);
897 prev_insn = -1;
898 return;
899 }
900
901 if ( (prev_insn != -1) && prev_seg && ((prev_seg != now_seg) || (prev_subseg != now_subseg)))
902 d30v_cleanup();
903
904 if ( (prev_insn != -1) &&
905 (write_2_short (&prev_opcode, (long)prev_insn, &opcode, (long)insn, extype, fixups) == 0))
906 {
907 /* no instructions saved */
908 prev_insn = -1;
909 }
910 else
911 {
912 if (extype)
913 as_fatal("Unable to mix instructions as specified");
914 /* save off last instruction so it may be packed on next pass */
915 memcpy( &prev_opcode, &opcode, sizeof(prev_opcode));
916 prev_insn = insn;
917 prev_seg = now_seg;
918 prev_subseg = now_subseg;
919 fixups = fixups->next;
920 }
921 }
922
923
924 /* do_assemble assembles a single instruction and returns an opcode */
925 /* it returns -1 (an invalid opcode) on error */
926
927 static long long
928 do_assemble (str, opcode)
929 char *str;
930 struct d30v_insn *opcode;
931 {
932 unsigned char *op_start, *save;
933 unsigned char *op_end;
934 char name[20];
935 int cmp_hack, nlen = 0;
936 expressionS myops[6];
937 long long insn;
938
939 /* Drop leading whitespace */
940 while (*str == ' ')
941 str++;
942
943 /* find the opcode end */
944 for (op_start = op_end = (unsigned char *) (str);
945 *op_end
946 && nlen < 20
947 && *op_end != '/'
948 && !is_end_of_line[*op_end] && *op_end != ' ';
949 op_end++)
950 {
951 name[nlen] = tolower(op_start[nlen]);
952 nlen++;
953 }
954
955 if (nlen == 0)
956 return (-1);
957
958 name[nlen] = 0;
959
960 /* if there is an execution condition code, handle it */
961 if (*op_end == '/')
962 {
963 int i = 0;
964 while ( (i < ECC_MAX) && strncasecmp(d30v_ecc_names[i],op_end+1,2))
965 i++;
966
967 if (i == ECC_MAX)
968 {
969 char tmp[4];
970 strncpy(tmp,op_end+1,2);
971 tmp[2] = 0;
972 as_fatal ("unknown condition code: %s",tmp);
973 return -1;
974 }
975 /* printf("condition code=%d\n",i); */
976 opcode->ecc = i;
977 op_end += 3;
978 }
979 else
980 opcode->ecc = ECC_AL;
981
982
983 /* CMP and CMPU change their name based on condition codes */
984 if (!strncmp(name,"cmp",3))
985 {
986 int p,i;
987 char **str = (char **)d30v_cc_names;
988 if (name[3] == 'u')
989 p = 4;
990 else
991 p = 3;
992
993 for(i=1; *str && strncmp(*str,&name[p],2); i++, *str++)
994 ;
995
996 if (!*str)
997 {
998 name[p+2]=0;
999 as_fatal ("unknown condition code: %s",&name[p]);
1000 }
1001
1002 cmp_hack = i;
1003 name[p] = 0;
1004 }
1005 else
1006 cmp_hack = 0;
1007
1008 /* printf("cmp_hack=%d\n",cmp_hack); */
1009
1010 /* find the first opcode with the proper name */
1011 opcode->op = (struct d30v_opcode *)hash_find (d30v_hash, name);
1012 if (opcode->op == NULL)
1013 as_fatal ("unknown opcode: %s",name);
1014
1015 save = input_line_pointer;
1016 input_line_pointer = op_end;
1017 while (!(opcode->form = find_format (opcode->op, myops, cmp_hack)))
1018 {
1019 opcode->op++;
1020 if (strcmp(opcode->op->name,name))
1021 return -1;
1022 }
1023 input_line_pointer = save;
1024
1025 insn = build_insn (opcode, myops);
1026 return (insn);
1027 }
1028
1029
1030 /* find_format() gets a pointer to an entry in the format table. */
1031 /* It must look at all formats for an opcode and use the operands */
1032 /* to choose the correct one. Returns NULL on error. */
1033
1034 static struct d30v_format *
1035 find_format (opcode, myops, cmp_hack)
1036 struct d30v_opcode *opcode;
1037 expressionS myops[];
1038 int cmp_hack;
1039 {
1040 int numops, match, index, i=0, j, k;
1041 struct d30v_format *fm;
1042 struct d30v_operand *op;
1043
1044 /* get all the operands and save them as expressions */
1045 numops = get_operands (myops, cmp_hack);
1046
1047 while (index = opcode->format[i++])
1048 {
1049 fm = (struct d30v_format *)&d30v_format_table[index];
1050 k = index;
1051 while (fm->form == index)
1052 {
1053 match = 1;
1054 /* now check the operands for compatibility */
1055 for (j = 0; match && fm->operands[j]; j++)
1056 {
1057 int flags = d30v_operand_table[fm->operands[j]].flags;
1058 int X_op = myops[j].X_op;
1059 int num = myops[j].X_add_number;
1060
1061 if ( flags & OPERAND_SPECIAL )
1062 break;
1063 else if (X_op == 0)
1064 match = 0;
1065 else if (flags & OPERAND_REG)
1066 {
1067 if ((X_op != O_register) ||
1068 ((flags & OPERAND_ACC) && !(num & OPERAND_ACC)) ||
1069 ((flags & OPERAND_FLAG) && !(num & OPERAND_FLAG)) ||
1070 (flags & OPERAND_CONTROL && !(num & OPERAND_CONTROL | num & OPERAND_FLAG)))
1071 {
1072 match = 0;
1073 }
1074 }
1075 else
1076 if (((flags & OPERAND_MINUS) && ((X_op != O_absent) || (num != OPERAND_MINUS))) ||
1077 ((flags & OPERAND_PLUS) && ((X_op != O_absent) || (num != OPERAND_PLUS))) ||
1078 ((flags & OPERAND_ATMINUS) && ((X_op != O_absent) || (num != OPERAND_ATMINUS))) ||
1079 ((flags & OPERAND_ATPAR) && ((X_op != O_absent) || (num != OPERAND_ATPAR))) ||
1080 ((flags & OPERAND_ATSIGN) && ((X_op != O_absent) || (num != OPERAND_ATSIGN))))
1081 {
1082 match=0;
1083 }
1084 else if (flags & OPERAND_NUM)
1085 {
1086 /* a number can be a constant or symbol expression */
1087 if (fm->form >= LONG)
1088 {
1089 /* If we're testing for a LONG format, either fits */
1090 if (X_op != O_constant && X_op != O_symbol)
1091 match = 0;
1092 }
1093 /* This is the tricky part. Will the constant or symbol */
1094 /* fit into the space in the current format? */
1095 else if (X_op == O_constant)
1096 {
1097 if (check_range (num, d30v_operand_table[fm->operands[j]].bits, flags))
1098 match = 0;
1099 }
1100 else if (X_op == O_symbol && S_IS_DEFINED(myops[j].X_add_symbol) &&
1101 (S_GET_SEGMENT(myops[j].X_add_symbol) == now_seg))
1102 {
1103 /* if the symbol is defined, see if the value will fit */
1104 /* into the form we're considering */
1105 fragS *f;
1106 long value;
1107 /* calculate the current address by running through the previous frags */
1108 /* and adding our current offset */
1109 for (value = 0, f = frchain_now->frch_root; f; f = f->fr_next)
1110 value += f->fr_fix + f->fr_offset;
1111 if (opcode->reloc_flag == RELOC_PCREL)
1112 value = S_GET_VALUE(myops[j].X_add_symbol) - value -
1113 (obstack_next_free(&frchain_now->frch_obstack) - frag_now->fr_literal);
1114 else
1115 value = S_GET_VALUE(myops[j].X_add_symbol);
1116 if (check_range (value, d30v_operand_table[fm->operands[j]].bits, flags))
1117 match = 0;
1118 }
1119 else
1120 match = 0;
1121 }
1122 }
1123 /* printf("through the loop: match=%d\n",match); */
1124 /* we're only done if the operands matched so far AND there
1125 are no more to check */
1126 if (match && myops[j].X_op==0)
1127 return fm;
1128 match = 0;
1129 fm = (struct d30v_format *)&d30v_format_table[++k];
1130 }
1131 /* printf("trying another format: i=%d\n",i); */
1132 }
1133 return NULL;
1134 }
1135
1136 /* if while processing a fixup, a reloc really needs to be created */
1137 /* then it is done here */
1138
1139 arelent *
1140 tc_gen_reloc (seg, fixp)
1141 asection *seg;
1142 fixS *fixp;
1143 {
1144 arelent *reloc;
1145 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
1146 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
1147 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1148 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1149 if (reloc->howto == (reloc_howto_type *) NULL)
1150 {
1151 as_bad_where (fixp->fx_file, fixp->fx_line,
1152 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
1153 return NULL;
1154 }
1155 reloc->addend = fixp->fx_addnumber;
1156 return reloc;
1157 }
1158
1159 int
1160 md_estimate_size_before_relax (fragp, seg)
1161 fragS *fragp;
1162 asection *seg;
1163 {
1164 abort ();
1165 return 0;
1166 }
1167
1168 long
1169 md_pcrel_from_section (fixp, sec)
1170 fixS *fixp;
1171 segT sec;
1172 {
1173 if (fixp->fx_addsy != (symbolS *)NULL && !S_IS_DEFINED (fixp->fx_addsy))
1174 return 0;
1175 return fixp->fx_frag->fr_address + fixp->fx_where;
1176 }
1177
1178 int
1179 md_apply_fix3 (fixp, valuep, seg)
1180 fixS *fixp;
1181 valueT *valuep;
1182 segT seg;
1183 {
1184 char *where;
1185 unsigned long insn, insn2;
1186 long value;
1187 int op_type;
1188 int left=0;
1189
1190 if (fixp->fx_addsy == (symbolS *) NULL)
1191 {
1192 value = *valuep;
1193 fixp->fx_done = 1;
1194 }
1195 else if (!S_IS_DEFINED(fixp->fx_addsy))
1196 return 0;
1197 else if (fixp->fx_pcrel)
1198 {
1199 value = *valuep;
1200 }
1201 else
1202 {
1203 value = fixp->fx_offset;
1204 if (fixp->fx_subsy != (symbolS *) NULL)
1205 {
1206 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
1207 value -= S_GET_VALUE (fixp->fx_subsy);
1208 else
1209 {
1210 /* We don't actually support subtracting a symbol. */
1211 as_bad_where (fixp->fx_file, fixp->fx_line,
1212 "expression too complex");
1213 }
1214 }
1215 }
1216
1217 /* Fetch the instruction, insert the fully resolved operand
1218 value, and stuff the instruction back again. */
1219 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1220 insn = bfd_getb32 ((unsigned char *) where);
1221
1222 switch (fixp->fx_r_type)
1223 {
1224 case BFD_RELOC_D30V_6:
1225 insn |= value & 0x3F;
1226 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1227 break;
1228 case BFD_RELOC_D30V_15:
1229 insn |= (value >> 3) & 0xFFF;
1230 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1231 break;
1232 case BFD_RELOC_D30V_15_PCREL:
1233 if ((long)fixp->fx_where & 0x7)
1234 value += 4;
1235 insn |= (value >> 3) & 0xFFF;
1236 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1237 break;
1238 case BFD_RELOC_D30V_21:
1239 insn |= (value >> 3) & 0x3FFFF;
1240 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1241 break;
1242 case BFD_RELOC_D30V_21_PCREL:
1243 if ((long)fixp->fx_where & 0x7)
1244 value += 4;
1245 insn |= (value >> 3) & 0x3FFFF;
1246 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1247 break;
1248 case BFD_RELOC_D30V_32:
1249 insn2 = bfd_getb32 ((unsigned char *) where + 4);
1250 insn |= (value >> 26) & 0x3F; /* top 6 bits */
1251 insn2 |= ((value & 0x03FC0000) << 2); /* next 8 bits */
1252 insn2 |= value & 0x0003FFFF; /* bottom 18 bits */
1253 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1254 bfd_putb32 ((bfd_vma) insn2, (unsigned char *) where + 4);
1255 break;
1256 case BFD_RELOC_D30V_32_PCREL:
1257 if ((long)fixp->fx_where & 0x7)
1258 value += 4;
1259 insn2 = bfd_getb32 ((unsigned char *) where + 4);
1260 insn |= (value >> 26) & 0x3F; /* top 6 bits */
1261 insn2 |= ((value & 0x03FC0000) << 2); /* next 8 bits */
1262 insn2 |= value & 0x0003FFFF; /* bottom 18 bits */
1263 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1264 bfd_putb32 ((bfd_vma) insn2, (unsigned char *) where + 4);
1265 break;
1266 case BFD_RELOC_32:
1267 bfd_putb32 ((bfd_vma) value, (unsigned char *) where);
1268 break;
1269 default:
1270 as_fatal ("line %d: unknown relocation type: 0x%x",fixp->fx_line,fixp->fx_r_type);
1271 }
1272 fixp->fx_done = 1;
1273 return 0;
1274 }
1275
1276
1277 /* d30v_cleanup() is called after the assembler has finished parsing the input
1278 file or after a label is defined. Because the D30V assembler sometimes saves short
1279 instructions to see if it can package them with the next instruction, there may
1280 be a short instruction that still needs written. */
1281 int
1282 d30v_cleanup ()
1283 {
1284 segT seg;
1285 subsegT subseg;
1286
1287 if (prev_insn != -1)
1288 {
1289 seg = now_seg;
1290 subseg = now_subseg;
1291 subseg_set (prev_seg, prev_subseg);
1292 write_1_short (&prev_opcode, (long)prev_insn, fixups->next);
1293 subseg_set (seg, subseg);
1294 prev_insn = -1;
1295 }
1296 return 1;
1297 }
1298
1299
1300 static void
1301 d30v_number_to_chars (buf, value, n)
1302 char *buf; /* Return 'nbytes' of chars here. */
1303 long long value; /* The value of the bits. */
1304 int n; /* Number of bytes in the output. */
1305 {
1306 while (n--)
1307 {
1308 buf[n] = value & 0xff;
1309 value >>= 8;
1310 }
1311 }