]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/tc-d30v.c
Sun Mar 23 18:03: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' || *p==' ' || *p==',')
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 if ((opers[i].X_op == O_constant) && check_range (number, bits, flags))
520 as_bad("operand out of range: %d",number);
521 if (bits < 31)
522 number &= 0x7FFFFFFF >> (31 - bits);
523
524 if (bits == 32)
525 {
526 /* it's a LONG instruction */
527 insn |= (number >> 26); /* top 6 bits */
528 insn <<= 32; /* shift the first word over */
529 insn |= ((number & 0x03FC0000) << 2); /* next 8 bits */
530 insn |= number & 0x0003FFFF; /* bottom 18 bits */
531 }
532 else
533 insn |= number << shift;
534 }
535 return insn;
536 }
537
538
539 /* write out a long form instruction */
540 static void
541 write_long (opcode, insn, fx)
542 struct d30v_insn *opcode;
543 long long insn;
544 Fixups *fx;
545 {
546 int i, where;
547 char *f = frag_more(8);
548
549 insn |= FM11;
550 d30v_number_to_chars (f, insn, 8);
551
552 for (i=0; i < fx->fc; i++)
553 {
554 if (fx->fix[i].reloc)
555 {
556 where = f - frag_now->fr_literal;
557 fix_new_exp (frag_now,
558 where,
559 fx->fix[i].size,
560 &(fx->fix[i].exp),
561 fx->fix[i].pcrel,
562 fx->fix[i].reloc);
563 }
564 }
565 fx->fc = 0;
566 }
567
568
569 /* write out a short form instruction by itself */
570 static void
571 write_1_short (opcode, insn, fx)
572 struct d30v_insn *opcode;
573 long long insn;
574 Fixups *fx;
575 {
576 char *f = frag_more(8);
577 int i, where;
578
579 /* the other container needs to be NOP */
580 /* according to 4.3.1: for FM=00, sub-instructions performed only
581 by IU cannot be encoded in L-container. */
582 if (opcode->op->unit == IU)
583 insn |= FM00 | ((long long)NOP << 32); /* right container */
584 else
585 insn = FM00 | (insn << 32) | (long long)NOP; /* left container */
586
587 d30v_number_to_chars (f, insn, 8);
588
589 for (i=0; i < fx->fc; i++)
590 {
591 if (fx->fix[i].reloc)
592 {
593 where = f - frag_now->fr_literal;
594 fix_new_exp (frag_now,
595 where,
596 fx->fix[i].size,
597 &(fx->fix[i].exp),
598 fx->fix[i].pcrel,
599 fx->fix[i].reloc);
600 }
601 }
602 fx->fc = 0;
603 }
604
605 /* write out a short form instruction if possible */
606 /* return number of instructions not written out */
607 static int
608 write_2_short (opcode1, insn1, opcode2, insn2, exec_type, fx)
609 struct d30v_insn *opcode1, *opcode2;
610 long long insn1, insn2;
611 int exec_type;
612 Fixups *fx;
613 {
614 long long insn;
615 char *f;
616 int i,j, where;
617
618 if(exec_type != 1 && (opcode1->op->flags_used == FLAG_JSR))
619 {
620 /* subroutines must be called from 32-bit boundaries */
621 /* so the return address will be correct */
622 write_1_short (opcode1, insn1, fx->next);
623 return (1);
624 }
625
626 switch (exec_type)
627 {
628 case 0: /* order not specified */
629 if ( Optimizing && parallel_ok (opcode1, insn1, opcode2, insn2, exec_type))
630 {
631 /* parallel */
632 if (opcode1->op->unit == IU)
633 insn = FM00 | (insn2 << 32) | insn1;
634 else if (opcode2->op->unit == MU)
635 insn = FM00 | (insn2 << 32) | insn1;
636 else
637 {
638 insn = FM00 | (insn1 << 32) | insn2;
639 fx = fx->next;
640 }
641 }
642 else if (opcode1->op->unit == IU)
643 {
644 /* reverse sequential */
645 insn = FM10 | (insn2 << 32) | insn1;
646 }
647 else
648 {
649 /* sequential */
650 insn = FM01 | (insn1 << 32) | insn2;
651 fx = fx->next;
652 }
653 break;
654 case 1: /* parallel */
655 if (opcode1->op->unit == IU)
656 {
657 if (opcode2->op->unit == IU)
658 as_fatal ("Two IU instructions may not be executed in parallel");
659 as_warn ("Swapping instruction order");
660 insn = FM00 | (insn2 << 32) | insn1;
661 }
662 else if (opcode2->op->unit == MU)
663 {
664 if (opcode1->op->unit == MU)
665 as_fatal ("Two MU instructions may not be executed in parallel");
666 as_warn ("Swapping instruction order");
667 insn = FM00 | (insn2 << 32) | insn1;
668 }
669 else
670 {
671 insn = FM00 | (insn1 << 32) | insn2;
672 fx = fx->next;
673 }
674 break;
675 case 2: /* sequential */
676 if (opcode1->op->unit == IU)
677 as_fatal ("IU instruction may not be in the left container");
678 insn = FM01 | (insn1 << 32) | insn2;
679 fx = fx->next;
680 break;
681 case 3: /* reverse sequential */
682 if (opcode2->op->unit == MU)
683 as_fatal ("MU instruction may not be in the right container");
684 insn = FM10 | (insn1 << 32) | insn2;
685 fx = fx->next;
686 break;
687 default:
688 as_fatal("unknown execution type passed to write_2_short()");
689 }
690
691 /* printf("writing out %llx\n",insn); */
692 f = frag_more(8);
693 d30v_number_to_chars (f, insn, 8);
694
695 for (j=0; j<2; j++)
696 {
697 for (i=0; i < fx->fc; i++)
698 {
699 if (fx->fix[i].reloc)
700 {
701 where = (f - frag_now->fr_literal) + 4*j;
702
703 fix_new_exp (frag_now,
704 where,
705 fx->fix[i].size,
706 &(fx->fix[i].exp),
707 fx->fix[i].pcrel,
708 fx->fix[i].reloc);
709 }
710 }
711 fx->fc = 0;
712 fx = fx->next;
713 }
714 return (0);
715 }
716
717
718 /* Check 2 instructions and determine if they can be safely */
719 /* executed in parallel. Returns 1 if they can be. */
720 static int
721 parallel_ok (op1, insn1, op2, insn2, exec_type)
722 struct d30v_insn *op1, *op2;
723 unsigned long insn1, insn2;
724 int exec_type;
725 {
726 int i, j, flags, mask, shift, regno, bits;
727 unsigned long ins, mod_reg[2][3], used_reg[2][3];
728 struct d30v_format *f;
729 struct d30v_opcode *op;
730
731 /* section 4.3: both instructions must not be IU or MU only */
732 if ((op1->op->unit == IU && op2->op->unit == IU)
733 || (op1->op->unit == MU && op2->op->unit == MU))
734 return 0;
735
736 /*
737 [0] r0-r31
738 [1] r32-r63
739 [2] a0, a1
740 */
741
742 for (j = 0; j < 2; j++)
743 {
744 if (j == 0)
745 {
746 f = op1->form;
747 op = op1->op;
748 ins = insn1;
749 }
750 else
751 {
752 f = op2->form;
753 op = op2->op;
754 ins = insn2;
755 }
756 mod_reg[j][0] = mod_reg[j][1] = 0;
757 mod_reg[j][2] = op->flags_set;
758 used_reg[j][0] = used_reg[j][1] = 0;
759 used_reg[j][2] = op->flags_used;
760 for (i = 0; f->operands[i]; i++)
761 {
762 flags = d30v_operand_table[f->operands[i]].flags;
763 shift = 12 - d30v_operand_table[f->operands[i]].position;
764 bits = d30v_operand_table[f->operands[i]].bits;
765 if (bits == 32)
766 mask = 0xffffffff;
767 else
768 mask = 0x7FFFFFFF >> (31 - bits);
769 if (flags & OPERAND_REG)
770 {
771 regno = (ins >> shift) & mask;
772 if (flags & OPERAND_DEST)
773 {
774 if (flags & OPERAND_ACC)
775 mod_reg[j][2] = 1 << (regno+16);
776 else if (flags & OPERAND_FLAG)
777 mod_reg[j][2] = 1 << regno;
778 else if (!(flags & OPERAND_CONTROL))
779 {
780 if (regno >= 32)
781 mod_reg[j][1] = 1 << (regno - 32);
782 else
783 mod_reg[j][0] = 1 << regno;
784 }
785 }
786 else
787 {
788 if (flags & OPERAND_ACC)
789 used_reg[j][2] = 1 << (regno+16);
790 else if (flags & OPERAND_FLAG)
791 used_reg[j][2] = 1 << regno;
792 else if (!(flags & OPERAND_CONTROL))
793 {
794 if (regno >= 32)
795 used_reg[j][1] = 1 << (regno - 32);
796 else
797 used_reg[j][0] = 1 << regno;
798 }
799 }
800 }
801 }
802 }
803
804 for(j = 0; j < 3; j++)
805 if ((mod_reg[0][j] & mod_reg[1][j])
806 || (mod_reg[0][j] & used_reg[1][j])
807 || (mod_reg[1][j] & used_reg[0][j]))
808 return 0;
809
810 return 1;
811 }
812
813
814
815 /* This is the main entry point for the machine-dependent assembler. str points to a
816 machine-dependent instruction. This function is supposed to emit the frags/bytes
817 it assembles to. For the D30V, it mostly handles the special VLIW parsing and packing
818 and leaves the difficult stuff to do_assemble().
819 */
820
821 static long long prev_insn = -1;
822 static struct d30v_insn prev_opcode;
823 static subsegT prev_subseg;
824 static segT prev_seg = 0;
825
826 void
827 md_assemble (str)
828 char *str;
829 {
830 struct d30v_insn opcode;
831 long long insn;
832 int extype=0; /* execution type; parallel, etc */
833 static int etype=0; /* saved extype. used for multiline instructions */
834 char *str2;
835
836 if (etype == 0)
837 {
838 /* look for the special multiple instruction separators */
839 str2 = strstr (str, "||");
840 if (str2)
841 extype = 1;
842 else
843 {
844 str2 = strstr (str, "->");
845 if (str2)
846 extype = 2;
847 else
848 {
849 str2 = strstr (str, "<-");
850 if (str2)
851 extype = 3;
852 }
853 }
854 /* str2 points to the separator, if one */
855 if (str2)
856 {
857 *str2 = 0;
858
859 /* if two instructions are present and we already have one saved
860 then first write it out */
861 d30v_cleanup();
862
863 /* assemble first instruction and save it */
864 prev_insn = do_assemble (str, &prev_opcode);
865 if (prev_insn == -1)
866 as_fatal ("cannot assemble instruction ");
867 fixups = fixups->next;
868 str = str2 + 2;
869 }
870 }
871
872 insn = do_assemble (str, &opcode);
873 if (insn == -1)
874 {
875 if (extype)
876 {
877 etype = extype;
878 return;
879 }
880 as_fatal ("cannot assemble instruction ");
881 }
882
883 if (etype)
884 {
885 extype = etype;
886 etype = 0;
887 }
888
889 /* if this is a long instruction, write it and any previous short instruction */
890 if (opcode.form->form >= LONG)
891 {
892 if (extype)
893 as_fatal("Unable to mix instructions as specified");
894 d30v_cleanup();
895 write_long (&opcode, insn, fixups);
896 prev_insn = -1;
897 return;
898 }
899
900 if ( (prev_insn != -1) && prev_seg && ((prev_seg != now_seg) || (prev_subseg != now_subseg)))
901 d30v_cleanup();
902
903 if ( (prev_insn != -1) &&
904 (write_2_short (&prev_opcode, (long)prev_insn, &opcode, (long)insn, extype, fixups) == 0))
905 {
906 /* no instructions saved */
907 prev_insn = -1;
908 }
909 else
910 {
911 if (extype)
912 as_fatal("Unable to mix instructions as specified");
913 /* save off last instruction so it may be packed on next pass */
914 memcpy( &prev_opcode, &opcode, sizeof(prev_opcode));
915 prev_insn = insn;
916 prev_seg = now_seg;
917 prev_subseg = now_subseg;
918 fixups = fixups->next;
919 }
920 }
921
922
923 /* do_assemble assembles a single instruction and returns an opcode */
924 /* it returns -1 (an invalid opcode) on error */
925
926 static long long
927 do_assemble (str, opcode)
928 char *str;
929 struct d30v_insn *opcode;
930 {
931 unsigned char *op_start, *save;
932 unsigned char *op_end;
933 char name[20];
934 int cmp_hack, nlen = 0;
935 expressionS myops[6];
936 long long insn;
937
938 /* Drop leading whitespace */
939 while (*str == ' ')
940 str++;
941
942 /* find the opcode end */
943 for (op_start = op_end = (unsigned char *) (str);
944 *op_end
945 && nlen < 20
946 && *op_end != '/'
947 && !is_end_of_line[*op_end] && *op_end != ' ';
948 op_end++)
949 {
950 name[nlen] = tolower(op_start[nlen]);
951 nlen++;
952 }
953
954 if (nlen == 0)
955 return (-1);
956
957 name[nlen] = 0;
958
959 /* if there is an execution condition code, handle it */
960 if (*op_end == '/')
961 {
962 int i = 0;
963 while ( (i < ECC_MAX) && strncasecmp(d30v_ecc_names[i],op_end+1,2))
964 i++;
965
966 if (i == ECC_MAX)
967 {
968 char tmp[4];
969 strncpy(tmp,op_end+1,2);
970 tmp[2] = 0;
971 as_fatal ("unknown condition code: %s",tmp);
972 return -1;
973 }
974 /* printf("condition code=%d\n",i); */
975 opcode->ecc = i;
976 op_end += 3;
977 }
978 else
979 opcode->ecc = ECC_AL;
980
981
982 /* CMP and CMPU change their name based on condition codes */
983 if (!strncmp(name,"cmp",3))
984 {
985 int p,i;
986 char **str = (char **)d30v_cc_names;
987 if (name[3] == 'u')
988 p = 4;
989 else
990 p = 3;
991
992 for(i=1; *str && strncmp(*str,&name[p],2); i++, *str++)
993 ;
994
995 if (!*str)
996 {
997 name[p+2]=0;
998 as_fatal ("unknown condition code: %s",&name[p]);
999 }
1000
1001 cmp_hack = i;
1002 name[p] = 0;
1003 }
1004 else
1005 cmp_hack = 0;
1006
1007 /* printf("cmp_hack=%d\n",cmp_hack); */
1008
1009 /* find the first opcode with the proper name */
1010 opcode->op = (struct d30v_opcode *)hash_find (d30v_hash, name);
1011 if (opcode->op == NULL)
1012 as_fatal ("unknown opcode: %s",name);
1013
1014 save = input_line_pointer;
1015 input_line_pointer = op_end;
1016 while (!(opcode->form = find_format (opcode->op, myops, cmp_hack)))
1017 {
1018 opcode->op++;
1019 if (strcmp(opcode->op->name,name))
1020 return -1;
1021 }
1022 input_line_pointer = save;
1023
1024 insn = build_insn (opcode, myops);
1025 return (insn);
1026 }
1027
1028
1029 /* find_format() gets a pointer to an entry in the format table. */
1030 /* It must look at all formats for an opcode and use the operands */
1031 /* to choose the correct one. Returns NULL on error. */
1032
1033 static struct d30v_format *
1034 find_format (opcode, myops, cmp_hack)
1035 struct d30v_opcode *opcode;
1036 expressionS myops[];
1037 int cmp_hack;
1038 {
1039 int numops, match, index, i=0, j, k;
1040 struct d30v_format *fm;
1041 struct d30v_operand *op;
1042
1043 /* get all the operands and save them as expressions */
1044 numops = get_operands (myops, cmp_hack);
1045
1046 while (index = opcode->format[i++])
1047 {
1048 fm = (struct d30v_format *)&d30v_format_table[index];
1049 k = index;
1050 while (fm->form == index)
1051 {
1052 match = 1;
1053 /* now check the operands for compatibility */
1054 for (j = 0; match && fm->operands[j]; j++)
1055 {
1056 int flags = d30v_operand_table[fm->operands[j]].flags;
1057 int X_op = myops[j].X_op;
1058 int num = myops[j].X_add_number;
1059
1060 if ( flags & OPERAND_SPECIAL )
1061 break;
1062 else if (X_op == 0)
1063 match = 0;
1064 else if (flags & OPERAND_REG)
1065 {
1066 if ((X_op != O_register) ||
1067 ((flags & OPERAND_ACC) && !(num & OPERAND_ACC)) ||
1068 ((flags & OPERAND_FLAG) && !(num & OPERAND_FLAG)) ||
1069 (flags & OPERAND_CONTROL && !(num & OPERAND_CONTROL | num & OPERAND_FLAG)))
1070 {
1071 match = 0;
1072 }
1073 }
1074 else
1075 if (((flags & OPERAND_MINUS) && ((X_op != O_absent) || (num != OPERAND_MINUS))) ||
1076 ((flags & OPERAND_PLUS) && ((X_op != O_absent) || (num != OPERAND_PLUS))) ||
1077 ((flags & OPERAND_ATMINUS) && ((X_op != O_absent) || (num != OPERAND_ATMINUS))) ||
1078 ((flags & OPERAND_ATPAR) && ((X_op != O_absent) || (num != OPERAND_ATPAR))) ||
1079 ((flags & OPERAND_ATSIGN) && ((X_op != O_absent) || (num != OPERAND_ATSIGN))))
1080 {
1081 match=0;
1082 }
1083 else if (flags & OPERAND_NUM)
1084 {
1085 /* a number can be a constant or symbol expression */
1086 if (fm->form >= LONG)
1087 {
1088 /* If we're testing for a LONG format, either fits */
1089 if (X_op != O_constant && X_op != O_symbol)
1090 match = 0;
1091 }
1092 /* This is the tricky part. Will the constant or symbol */
1093 /* fit into the space in the current format? */
1094 else if (X_op == O_constant)
1095 {
1096 if (check_range (num, d30v_operand_table[fm->operands[j]].bits, flags))
1097 match = 0;
1098 }
1099 else if (X_op == O_symbol && S_IS_DEFINED(myops[j].X_add_symbol) &&
1100 (S_GET_SEGMENT(myops[j].X_add_symbol) == now_seg))
1101 {
1102 /* if the symbol is defined, see if the value will fit */
1103 /* into the form we're considering */
1104 fragS *f;
1105 long value;
1106 /* calculate the current address by running through the previous frags */
1107 /* and adding our current offset */
1108 for (value = 0, f = frchain_now->frch_root; f; f = f->fr_next)
1109 value += f->fr_fix + f->fr_offset;
1110 if (opcode->reloc_flag == RELOC_PCREL)
1111 value = S_GET_VALUE(myops[j].X_add_symbol) - value -
1112 (obstack_next_free(&frchain_now->frch_obstack) - frag_now->fr_literal);
1113 else
1114 value = S_GET_VALUE(myops[j].X_add_symbol);
1115 if (check_range (value, d30v_operand_table[fm->operands[j]].bits, flags))
1116 match = 0;
1117 }
1118 else
1119 match = 0;
1120 }
1121 }
1122 /* printf("through the loop: match=%d\n",match); */
1123 /* we're only done if the operands matched so far AND there
1124 are no more to check */
1125 if (match && myops[j].X_op==0)
1126 return fm;
1127 match = 0;
1128 fm = (struct d30v_format *)&d30v_format_table[++k];
1129 }
1130 /* printf("trying another format: i=%d\n",i); */
1131 }
1132 return NULL;
1133 }
1134
1135 /* if while processing a fixup, a reloc really needs to be created */
1136 /* then it is done here */
1137
1138 arelent *
1139 tc_gen_reloc (seg, fixp)
1140 asection *seg;
1141 fixS *fixp;
1142 {
1143 arelent *reloc;
1144 reloc = (arelent *) xmalloc (sizeof (arelent));
1145 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
1146 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1147 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1148 if (reloc->howto == (reloc_howto_type *) NULL)
1149 {
1150 as_bad_where (fixp->fx_file, fixp->fx_line,
1151 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
1152 return NULL;
1153 }
1154 reloc->addend = fixp->fx_addnumber;
1155 return reloc;
1156 }
1157
1158 int
1159 md_estimate_size_before_relax (fragp, seg)
1160 fragS *fragp;
1161 asection *seg;
1162 {
1163 abort ();
1164 return 0;
1165 }
1166
1167 long
1168 md_pcrel_from_section (fixp, sec)
1169 fixS *fixp;
1170 segT sec;
1171 {
1172 if (fixp->fx_addsy != (symbolS *)NULL && !S_IS_DEFINED (fixp->fx_addsy))
1173 return 0;
1174 return fixp->fx_frag->fr_address + fixp->fx_where;
1175 }
1176
1177 int
1178 md_apply_fix3 (fixp, valuep, seg)
1179 fixS *fixp;
1180 valueT *valuep;
1181 segT seg;
1182 {
1183 char *where;
1184 unsigned long insn, insn2;
1185 long value;
1186 int op_type;
1187 int left=0;
1188
1189 if (fixp->fx_addsy == (symbolS *) NULL)
1190 {
1191 value = *valuep;
1192 fixp->fx_done = 1;
1193 }
1194 else if (!S_IS_DEFINED(fixp->fx_addsy))
1195 return 0;
1196 else if (fixp->fx_pcrel)
1197 {
1198 value = *valuep;
1199 }
1200 else
1201 {
1202 value = fixp->fx_offset;
1203 if (fixp->fx_subsy != (symbolS *) NULL)
1204 {
1205 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
1206 value -= S_GET_VALUE (fixp->fx_subsy);
1207 else
1208 {
1209 /* We don't actually support subtracting a symbol. */
1210 as_bad_where (fixp->fx_file, fixp->fx_line,
1211 "expression too complex");
1212 }
1213 }
1214 }
1215
1216 /* Fetch the instruction, insert the fully resolved operand
1217 value, and stuff the instruction back again. */
1218 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1219 insn = bfd_getb32 ((unsigned char *) where);
1220
1221 switch (fixp->fx_r_type)
1222 {
1223 case BFD_RELOC_D30V_6:
1224 insn |= value & 0x3F;
1225 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1226 break;
1227 case BFD_RELOC_D30V_15:
1228 insn |= (value >> 3) & 0xFFF;
1229 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1230 break;
1231 case BFD_RELOC_D30V_15_PCREL:
1232 if ((long)fixp->fx_where & 0x7)
1233 value += 4;
1234 insn |= (value >> 3) & 0xFFF;
1235 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1236 break;
1237 case BFD_RELOC_D30V_21:
1238 insn |= (value >> 3) & 0x3FFFF;
1239 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1240 break;
1241 case BFD_RELOC_D30V_21_PCREL:
1242 if ((long)fixp->fx_where & 0x7)
1243 value += 4;
1244 insn |= (value >> 3) & 0x3FFFF;
1245 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1246 break;
1247 case BFD_RELOC_D30V_32:
1248 insn2 = bfd_getb32 ((unsigned char *) where + 4);
1249 insn |= (value >> 26) & 0x3F; /* top 6 bits */
1250 insn2 |= ((value & 0x03FC0000) << 2); /* next 8 bits */
1251 insn2 |= value & 0x0003FFFF; /* bottom 18 bits */
1252 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1253 bfd_putb32 ((bfd_vma) insn2, (unsigned char *) where + 4);
1254 break;
1255 case BFD_RELOC_D30V_32_PCREL:
1256 if ((long)fixp->fx_where & 0x7)
1257 value += 4;
1258 insn2 = bfd_getb32 ((unsigned char *) where + 4);
1259 insn |= (value >> 26) & 0x3F; /* top 6 bits */
1260 insn2 |= ((value & 0x03FC0000) << 2); /* next 8 bits */
1261 insn2 |= value & 0x0003FFFF; /* bottom 18 bits */
1262 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1263 bfd_putb32 ((bfd_vma) insn2, (unsigned char *) where + 4);
1264 break;
1265 case BFD_RELOC_32:
1266 bfd_putb32 ((bfd_vma) value, (unsigned char *) where);
1267 break;
1268 default:
1269 as_fatal ("line %d: unknown relocation type: 0x%x",fixp->fx_line,fixp->fx_r_type);
1270 }
1271 fixp->fx_done = 1;
1272 return 0;
1273 }
1274
1275
1276 /* d30v_cleanup() is called after the assembler has finished parsing the input
1277 file or after a label is defined. Because the D30V assembler sometimes saves short
1278 instructions to see if it can package them with the next instruction, there may
1279 be a short instruction that still needs written. */
1280 int
1281 d30v_cleanup ()
1282 {
1283 segT seg;
1284 subsegT subseg;
1285
1286 if (prev_insn != -1)
1287 {
1288 seg = now_seg;
1289 subseg = now_subseg;
1290 subseg_set (prev_seg, prev_subseg);
1291 write_1_short (&prev_opcode, (long)prev_insn, fixups->next);
1292 subseg_set (seg, subseg);
1293 prev_insn = -1;
1294 }
1295 return 1;
1296 }
1297
1298
1299 static void
1300 d30v_number_to_chars (buf, value, n)
1301 char *buf; /* Return 'nbytes' of chars here. */
1302 long long value; /* The value of the bits. */
1303 int n; /* Number of bytes in the output. */
1304 {
1305 while (n--)
1306 {
1307 buf[n] = value & 0xff;
1308 value >>= 8;
1309 }
1310 }