f61ff5b5c6b052677203ee1639d8aad93cafd451
[deliverable/binutils-gdb.git] / gas / config / tc-d30v.c
1 /* tc-d30v.c -- Assembler code for the Mitsubishi D30V
2
3 Copyright (C) 1997, 1998 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 = "OnNcC";
32 const char EXP_CHARS[] = "eE";
33 const char FLT_CHARS[] = "dD";
34
35 #define NOP_MULTIPLY 1
36 #define NOP_ALL 2
37 static int warn_nops = 0;
38 static int Optimizing = 0;
39 static int warn_register_name_conflicts = 1;
40
41 #define FORCE_SHORT 1
42 #define FORCE_LONG 2
43
44 /* EXEC types. */
45 typedef enum _exec_type
46 {
47 EXEC_UNKNOWN, /* no order specified */
48 EXEC_PARALLEL, /* done in parallel (FM=00) */
49 EXEC_SEQ, /* sequential (FM=01) */
50 EXEC_REVSEQ /* reverse sequential (FM=10) */
51 } exec_type_enum;
52
53 /* fixups */
54 #define MAX_INSN_FIXUPS (5)
55 struct d30v_fixup
56 {
57 expressionS exp;
58 int operand;
59 int pcrel;
60 int size;
61 bfd_reloc_code_real_type reloc;
62 };
63
64 typedef struct _fixups
65 {
66 int fc;
67 struct d30v_fixup fix[MAX_INSN_FIXUPS];
68 struct _fixups *next;
69 } Fixups;
70
71 static Fixups FixUps[2];
72 static Fixups *fixups;
73
74 /* Whether current and previous instruction are word multiply insns. */
75 static int cur_mul32_p = 0;
76 static int prev_mul32_p = 0;
77
78 /* The flag_explicitly_parallel is true iff the instruction being assembled
79 has been explicitly written as a parallel short-instruction pair by the
80 human programmer. It is used in parallel_ok() to distinguish between
81 those dangerous parallelizations attempted by the human, which are to be
82 allowed, and those attempted by the assembler, which are not. It is set
83 from md_assemble(). */
84 static int flag_explicitly_parallel = 0;
85 static int flag_xp_state = 0;
86
87 /* Whether current and previous left sub-instruction disables
88 execution of right sub-instruction. */
89 static int cur_left_kills_right_p = 0;
90 static int prev_left_kills_right_p = 0;
91
92 /* The known current alignment of the current section. */
93 static int d30v_current_align;
94 static segT d30v_current_align_seg;
95
96 /* The last seen label in the current section. This is used to auto-align
97 labels preceeding instructions. */
98 static symbolS *d30v_last_label;
99
100 /* Two nops */
101 #define NOP_LEFT ((long long)NOP << 32)
102 #define NOP_RIGHT ((long long)NOP)
103 #define NOP2 (FM00 | NOP_LEFT | NOP_RIGHT)
104
105 /* local functions */
106 static int reg_name_search PARAMS ((char *name));
107 static int register_name PARAMS ((expressionS *expressionP));
108 static int check_range PARAMS ((unsigned long num, int bits, int flags));
109 static int postfix PARAMS ((char *p));
110 static bfd_reloc_code_real_type get_reloc PARAMS ((struct d30v_operand *op, int rel_flag));
111 static int get_operands PARAMS ((expressionS exp[], int cmp_hack));
112 static struct d30v_format *find_format PARAMS ((struct d30v_opcode *opcode,
113 expressionS ops[],int fsize, int cmp_hack));
114 static long long build_insn PARAMS ((struct d30v_insn *opcode, expressionS *opers));
115 static void write_long PARAMS ((struct d30v_insn *opcode, long long insn, Fixups *fx));
116 static void write_1_short PARAMS ((struct d30v_insn *opcode, long long insn,
117 Fixups *fx, int use_sequential));
118 static int write_2_short PARAMS ((struct d30v_insn *opcode1, long long insn1,
119 struct d30v_insn *opcode2, long long insn2, exec_type_enum exec_type, Fixups *fx));
120 static long long do_assemble PARAMS ((char *str, struct d30v_insn *opcode,
121 int shortp, int is_parallel));
122 static int parallel_ok PARAMS ((struct d30v_insn *opcode1, unsigned long insn1,
123 struct d30v_insn *opcode2, unsigned long insn2,
124 exec_type_enum exec_type));
125 static void d30v_number_to_chars PARAMS ((char *buf, long long value, int nbytes));
126 static void check_size PARAMS ((long value, int bits, char *file, int line));
127 static void d30v_align PARAMS ((int, char *, symbolS *));
128 static void s_d30v_align PARAMS ((int));
129 static void s_d30v_text PARAMS ((int));
130 static void s_d30v_data PARAMS ((int));
131 static void s_d30v_section PARAMS ((int));
132
133 struct option md_longopts[] = {
134 {NULL, no_argument, NULL, 0}
135 };
136 size_t md_longopts_size = sizeof(md_longopts);
137
138
139 /* The target specific pseudo-ops which we support. */
140 const pseudo_typeS md_pseudo_table[] =
141 {
142 { "word", cons, 4 },
143 { "hword", cons, 2 },
144 { "align", s_d30v_align, 0 },
145 { "text", s_d30v_text, 0 },
146 { "data", s_d30v_data, 0 },
147 { "section", s_d30v_section, 0 },
148 { "section.s", s_d30v_section, 0 },
149 { "sect", s_d30v_section, 0 },
150 { "sect.s", s_d30v_section, 0 },
151 { NULL, NULL, 0 }
152 };
153
154 /* Opcode hash table. */
155 static struct hash_control *d30v_hash;
156
157 /* reg_name_search does a binary search of the pre_defined_registers
158 array to see if "name" is a valid regiter name. Returns the register
159 number from the array on success, or -1 on failure. */
160
161 static int
162 reg_name_search (name)
163 char *name;
164 {
165 int middle, low, high;
166 int cmp;
167
168 low = 0;
169 high = reg_name_cnt () - 1;
170
171 if (symbol_find (name) != NULL)
172 {
173 if (warn_register_name_conflicts)
174 as_warn ("Register name %s conflicts with symbol of the same name",
175 name);
176 }
177
178 do
179 {
180 middle = (low + high) / 2;
181 cmp = strcasecmp (name, pre_defined_registers[middle].name);
182 if (cmp < 0)
183 high = middle - 1;
184 else if (cmp > 0)
185 low = middle + 1;
186 else
187 return pre_defined_registers[middle].value;
188 }
189 while (low <= high);
190
191 return -1;
192 }
193
194 /* register_name() checks the string at input_line_pointer
195 to see if it is a valid register name */
196
197 static int
198 register_name (expressionP)
199 expressionS *expressionP;
200 {
201 int reg_number;
202 char c, *p = input_line_pointer;
203
204 while (*p && *p!='\n' && *p!='\r' && *p !=',' && *p!=' ' && *p!=')')
205 p++;
206
207 c = *p;
208 if (c)
209 *p++ = 0;
210
211 /* look to see if it's in the register table */
212 reg_number = reg_name_search (input_line_pointer);
213 if (reg_number >= 0)
214 {
215 expressionP->X_op = O_register;
216 /* temporarily store a pointer to the string here */
217 expressionP->X_op_symbol = (struct symbol *)input_line_pointer;
218 expressionP->X_add_number = reg_number;
219 input_line_pointer = p;
220 return 1;
221 }
222 if (c)
223 *(p-1) = c;
224 return 0;
225 }
226
227
228 static int
229 check_range (num, bits, flags)
230 unsigned long num;
231 int bits;
232 int flags;
233 {
234 long min, max;
235 int retval=0;
236
237 /* don't bother checking 32-bit values */
238 if (bits == 32)
239 return 0;
240
241 if (flags & OPERAND_SHIFT)
242 {
243 /* We know that all shifts are right by three bits.... */
244
245 if (flags & OPERAND_SIGNED)
246 num = (unsigned long) (((/*signed*/ long) num) >> 3);
247 else
248 num >>= 3;
249 }
250
251 if (flags & OPERAND_SIGNED)
252 {
253 max = (1 << (bits - 1))-1;
254 min = - (1 << (bits - 1));
255 if (((long)num > max) || ((long)num < min))
256 retval = 1;
257 }
258 else
259 {
260 max = (1 << bits) - 1;
261 min = 0;
262 if ((num > max) || (num < min))
263 retval = 1;
264 }
265 return retval;
266 }
267
268
269 void
270 md_show_usage (stream)
271 FILE *stream;
272 {
273 fprintf (stream, _("\nD30V options:\n\
274 -O Make adjacent short instructions parallel if possible.\n\
275 -n Warn about all NOPs inserted by the assembler.\n\
276 -N Warn about NOPs inserted after word multiplies.\n\
277 -c Warn about symbols whoes names match register names.\n\
278 -C Opposite of -C. -c is the default.\n"));
279 }
280
281 int
282 md_parse_option (c, arg)
283 int c;
284 char *arg;
285 {
286 switch (c)
287 {
288 /* Optimize. Will attempt to parallelize operations */
289 case 'O':
290 Optimizing = 1;
291 break;
292
293 /* Warn about all NOPS that the assembler inserts. */
294 case 'n':
295 warn_nops = NOP_ALL;
296 break;
297
298 /* Warn about the NOPS that the assembler inserts because of the
299 multiply hazard. */
300 case 'N':
301 warn_nops = NOP_MULTIPLY;
302 break;
303
304 case 'c':
305 warn_register_name_conflicts = 1;
306 break;
307
308 case 'C':
309 warn_register_name_conflicts = 0;
310 break;
311
312 default:
313 return 0;
314 }
315 return 1;
316 }
317
318 symbolS *
319 md_undefined_symbol (name)
320 char *name;
321 {
322 return 0;
323 }
324
325 /* Turn a string in input_line_pointer into a floating point constant of type
326 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
327 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
328 */
329 char *
330 md_atof (type, litP, sizeP)
331 int type;
332 char *litP;
333 int *sizeP;
334 {
335 int prec;
336 LITTLENUM_TYPE words[4];
337 char *t;
338 int i;
339
340 switch (type)
341 {
342 case 'f':
343 prec = 2;
344 break;
345 case 'd':
346 prec = 4;
347 break;
348 default:
349 *sizeP = 0;
350 return _("bad call to md_atof");
351 }
352
353 t = atof_ieee (input_line_pointer, type, words);
354 if (t)
355 input_line_pointer = t;
356
357 *sizeP = prec * 2;
358
359 for (i = 0; i < prec; i++)
360 {
361 md_number_to_chars (litP, (valueT) words[i], 2);
362 litP += 2;
363 }
364 return NULL;
365 }
366
367 void
368 md_convert_frag (abfd, sec, fragP)
369 bfd *abfd;
370 asection *sec;
371 fragS *fragP;
372 {
373 abort ();
374 }
375
376 valueT
377 md_section_align (seg, addr)
378 asection *seg;
379 valueT addr;
380 {
381 int align = bfd_get_section_alignment (stdoutput, seg);
382 return ((addr + (1 << align) - 1) & (-1 << align));
383 }
384
385
386 void
387 md_begin ()
388 {
389 struct d30v_opcode *opcode;
390 d30v_hash = hash_new ();
391
392 /* Insert opcode names into a hash table. */
393 for (opcode = (struct d30v_opcode *)d30v_opcode_table; opcode->name; opcode++)
394 hash_insert (d30v_hash, opcode->name, (char *) opcode);
395
396 fixups = &FixUps[0];
397 FixUps[0].next = &FixUps[1];
398 FixUps[1].next = &FixUps[0];
399
400 d30v_current_align_seg = now_seg;
401 }
402
403
404 /* this function removes the postincrement or postdecrement
405 operator ( '+' or '-' ) from an expression */
406
407 static int postfix (p)
408 char *p;
409 {
410 while (*p != '-' && *p != '+')
411 {
412 if (*p==0 || *p=='\n' || *p=='\r' || *p==' ' || *p==',')
413 break;
414 p++;
415 }
416
417 if (*p == '-')
418 {
419 *p = ' ';
420 return (-1);
421 }
422 if (*p == '+')
423 {
424 *p = ' ';
425 return (1);
426 }
427
428 return (0);
429 }
430
431
432 static bfd_reloc_code_real_type
433 get_reloc (op, rel_flag)
434 struct d30v_operand *op;
435 int rel_flag;
436 {
437 switch (op->bits)
438 {
439 case 6:
440 if (op->flags & OPERAND_SHIFT)
441 return BFD_RELOC_D30V_9_PCREL;
442 else
443 return BFD_RELOC_D30V_6;
444 break;
445 case 12:
446 if (!(op->flags & OPERAND_SHIFT))
447 as_warn (_("unexpected 12-bit reloc type"));
448 if (rel_flag == RELOC_PCREL)
449 return BFD_RELOC_D30V_15_PCREL;
450 else
451 return BFD_RELOC_D30V_15;
452 case 18:
453 if (!(op->flags & OPERAND_SHIFT))
454 as_warn (_("unexpected 18-bit reloc type"));
455 if (rel_flag == RELOC_PCREL)
456 return BFD_RELOC_D30V_21_PCREL;
457 else
458 return BFD_RELOC_D30V_21;
459 case 32:
460 if (rel_flag == RELOC_PCREL)
461 return BFD_RELOC_D30V_32_PCREL;
462 else
463 return BFD_RELOC_D30V_32;
464 default:
465 return 0;
466 }
467 }
468
469 /* get_operands parses a string of operands and returns
470 an array of expressions */
471
472 static int
473 get_operands (exp, cmp_hack)
474 expressionS exp[];
475 int cmp_hack;
476 {
477 char *p = input_line_pointer;
478 int numops = 0;
479 int post = 0;
480
481 if (cmp_hack)
482 {
483 exp[numops].X_op = O_absent;
484 exp[numops++].X_add_number = cmp_hack - 1;
485 }
486
487 while (*p)
488 {
489 while (*p == ' ' || *p == '\t' || *p == ',')
490 p++;
491 if (*p==0 || *p=='\n' || *p=='\r')
492 break;
493
494 if (*p == '@')
495 {
496 p++;
497 exp[numops].X_op = O_absent;
498 if (*p == '(')
499 {
500 p++;
501 exp[numops].X_add_number = OPERAND_ATPAR;
502 post = postfix (p);
503 }
504 else if (*p == '-')
505 {
506 p++;
507 exp[numops].X_add_number = OPERAND_ATMINUS;
508 }
509 else
510 {
511 exp[numops].X_add_number = OPERAND_ATSIGN;
512 post = postfix (p);
513 }
514 numops++;
515 continue;
516 }
517
518 if (*p == ')')
519 {
520 /* just skip the trailing paren */
521 p++;
522 continue;
523 }
524
525 input_line_pointer = p;
526
527 /* check to see if it might be a register name */
528 if (!register_name (&exp[numops]))
529 {
530 /* parse as an expression */
531 expression (&exp[numops]);
532 }
533
534 if (exp[numops].X_op == O_illegal)
535 as_bad (_("illegal operand"));
536 else if (exp[numops].X_op == O_absent)
537 as_bad (_("missing operand"));
538
539 numops++;
540 p = input_line_pointer;
541
542 switch (post)
543 {
544 case -1: /* postdecrement mode */
545 exp[numops].X_op = O_absent;
546 exp[numops++].X_add_number = OPERAND_MINUS;
547 break;
548 case 1: /* postincrement mode */
549 exp[numops].X_op = O_absent;
550 exp[numops++].X_add_number = OPERAND_PLUS;
551 break;
552 }
553 post = 0;
554 }
555
556 exp[numops].X_op = 0;
557 return (numops);
558 }
559
560 /* build_insn generates the instruction. It does everything */
561 /* but write the FM bits. */
562
563 static long long
564 build_insn (opcode, opers)
565 struct d30v_insn *opcode;
566 expressionS *opers;
567 {
568 int i, length, bits, shift, flags;
569 unsigned int number, id=0;
570 long long insn;
571 struct d30v_opcode *op = opcode->op;
572 struct d30v_format *form = opcode->form;
573
574 insn = opcode->ecc << 28 | op->op1 << 25 | op->op2 << 20 | form->modifier << 18;
575
576 for (i=0; form->operands[i]; i++)
577 {
578 flags = d30v_operand_table[form->operands[i]].flags;
579
580 /* must be a register or number */
581 if (!(flags & OPERAND_REG) && !(flags & OPERAND_NUM) &&
582 !(flags & OPERAND_NAME) && !(flags & OPERAND_SPECIAL))
583 continue;
584
585 bits = d30v_operand_table[form->operands[i]].bits;
586 if (flags & OPERAND_SHIFT)
587 bits += 3;
588
589 length = d30v_operand_table[form->operands[i]].length;
590 shift = 12 - d30v_operand_table[form->operands[i]].position;
591 if (opers[i].X_op != O_symbol)
592 number = opers[i].X_add_number;
593 else
594 number = 0;
595 if (flags & OPERAND_REG)
596 {
597 /* check for mvfsys or mvtsys control registers */
598 if (flags & OPERAND_CONTROL && (number & 0x7f) > MAX_CONTROL_REG)
599 {
600 /* PSWL or PSWH */
601 id = (number & 0x7f) - MAX_CONTROL_REG;
602 number = 0;
603 }
604 else if (number & OPERAND_FLAG)
605 {
606 id = 3; /* number is a flag register */
607 }
608 number &= 0x7F;
609 }
610 else if (flags & OPERAND_SPECIAL)
611 {
612 number = id;
613 }
614
615 if (opers[i].X_op != O_register && opers[i].X_op != O_constant && !(flags & OPERAND_NAME))
616 {
617 /* now create a fixup */
618
619 if (fixups->fc >= MAX_INSN_FIXUPS)
620 as_fatal (_("too many fixups"));
621
622 fixups->fix[fixups->fc].reloc =
623 get_reloc ((struct d30v_operand *)&d30v_operand_table[form->operands[i]], op->reloc_flag);
624 fixups->fix[fixups->fc].size = 4;
625 fixups->fix[fixups->fc].exp = opers[i];
626 fixups->fix[fixups->fc].operand = form->operands[i];
627 if (fixups->fix[fixups->fc].reloc == BFD_RELOC_D30V_9_PCREL)
628 fixups->fix[fixups->fc].pcrel = RELOC_PCREL;
629 else
630 fixups->fix[fixups->fc].pcrel = op->reloc_flag;
631 (fixups->fc)++;
632 }
633
634 /* truncate to the proper number of bits */
635 if ((opers[i].X_op == O_constant) && check_range (number, bits, flags))
636 as_bad (_("operand out of range: %d"),number);
637 if (bits < 31)
638 number &= 0x7FFFFFFF >> (31 - bits);
639 if (flags & OPERAND_SHIFT)
640 number >>= 3;
641 if (bits == 32)
642 {
643 /* it's a LONG instruction */
644 insn |= (number >> 26); /* top 6 bits */
645 insn <<= 32; /* shift the first word over */
646 insn |= ((number & 0x03FC0000) << 2); /* next 8 bits */
647 insn |= number & 0x0003FFFF; /* bottom 18 bits */
648 }
649 else
650 insn |= number << shift;
651 }
652 return insn;
653 }
654
655
656 /* write out a long form instruction */
657 static void
658 write_long (opcode, insn, fx)
659 struct d30v_insn *opcode;
660 long long insn;
661 Fixups *fx;
662 {
663 int i, where;
664 char *f = frag_more (8);
665
666 insn |= FM11;
667 d30v_number_to_chars (f, insn, 8);
668
669 for (i=0; i < fx->fc; i++)
670 {
671 if (fx->fix[i].reloc)
672 {
673 where = f - frag_now->fr_literal;
674 fix_new_exp (frag_now,
675 where,
676 fx->fix[i].size,
677 &(fx->fix[i].exp),
678 fx->fix[i].pcrel,
679 fx->fix[i].reloc);
680 }
681 }
682 fx->fc = 0;
683 }
684
685
686 /* Write out a short form instruction by itself. */
687 static void
688 write_1_short (opcode, insn, fx, use_sequential)
689 struct d30v_insn *opcode;
690 long long insn;
691 Fixups *fx;
692 int use_sequential;
693 {
694 char *f = frag_more (8);
695 int i, where;
696
697 if (warn_nops == NOP_ALL)
698 as_warn (_("%s NOP inserted"), use_sequential ?
699 _("sequential") : _("parallel"));
700
701 /* The other container needs to be NOP. */
702 if (use_sequential)
703 {
704 /* Use a sequential NOP rather than a parallel one,
705 as the current instruction is a FLAG_MUL32 type one
706 and the next instruction is a load. */
707
708 /* According to 4.3.1: for FM=01, sub-instructions performed
709 only by IU cannot be encoded in L-container. */
710
711 if (opcode->op->unit == IU)
712 insn |= FM10 | NOP_LEFT; /* right then left */
713 else
714 insn = FM01 | (insn << 32) | NOP_RIGHT; /* left then right */
715 }
716 else
717 {
718 /* According to 4.3.1: for FM=00, sub-instructions performed
719 only by IU cannot be encoded in L-container. */
720
721 if (opcode->op->unit == IU)
722 insn |= FM00 | NOP_LEFT; /* right container */
723 else
724 insn = FM00 | (insn << 32) | NOP_RIGHT; /* left container */
725 }
726
727 d30v_number_to_chars (f, insn, 8);
728
729 for (i=0; i < fx->fc; i++)
730 {
731 if (fx->fix[i].reloc)
732 {
733 where = f - frag_now->fr_literal;
734 fix_new_exp (frag_now,
735 where,
736 fx->fix[i].size,
737 &(fx->fix[i].exp),
738 fx->fix[i].pcrel,
739 fx->fix[i].reloc);
740 }
741 }
742 fx->fc = 0;
743 }
744
745 /* Write out a short form instruction if possible; */
746 /* return number of instructions not written out. */
747 static int
748 write_2_short (opcode1, insn1, opcode2, insn2, exec_type, fx)
749 struct d30v_insn *opcode1, *opcode2;
750 long long insn1, insn2;
751 exec_type_enum exec_type;
752 Fixups *fx;
753 {
754 long long insn = NOP2;
755 char *f;
756 int i,j, where;
757
758 if (exec_type != EXEC_PARALLEL &&
759 ((opcode1->op->flags_used & (FLAG_JSR | FLAG_DELAY)) == FLAG_JSR))
760 {
761 /* subroutines must be called from 32-bit boundaries */
762 /* so the return address will be correct */
763 write_1_short (opcode1, insn1, fx->next, false);
764 return 1;
765 }
766
767 switch (exec_type)
768 {
769 case EXEC_UNKNOWN: /* order not specified */
770 if (Optimizing
771 && parallel_ok (opcode1, insn1, opcode2, insn2, exec_type)
772 && ! ( (opcode1->op->unit == EITHER_BUT_PREFER_MU
773 || opcode1->op->unit == MU)
774 &&
775 ( opcode2->op->unit == EITHER_BUT_PREFER_MU
776 || opcode2->op->unit == MU)))
777 {
778 /* parallel */
779 exec_type = EXEC_PARALLEL;
780
781 if (opcode1->op->unit == IU
782 || opcode2->op->unit == MU
783 || opcode2->op->unit == EITHER_BUT_PREFER_MU)
784 insn = FM00 | (insn2 << 32) | insn1;
785 else
786 {
787 insn = FM00 | (insn1 << 32) | insn2;
788 fx = fx->next;
789 }
790 }
791 else if (opcode1->op->unit == IU
792 || (opcode1->op->unit == EITHER
793 && opcode2->op->unit == EITHER_BUT_PREFER_MU))
794 {
795 /* reverse sequential */
796 insn = FM10 | (insn2 << 32) | insn1;
797 exec_type = EXEC_REVSEQ;
798 }
799 else
800 {
801 /* sequential */
802 insn = FM01 | (insn1 << 32) | insn2;
803 fx = fx->next;
804 exec_type = EXEC_SEQ;
805 }
806 break;
807
808 case EXEC_PARALLEL: /* parallel */
809 flag_explicitly_parallel = flag_xp_state;
810 if (! parallel_ok (opcode1, insn1, opcode2, insn2, exec_type))
811 as_fatal (_("Instructions may not be executed in parallel"));
812 else if (opcode1->op->unit == IU)
813 {
814 if (opcode2->op->unit == IU)
815 as_fatal (_("Two IU instructions may not be executed in parallel"));
816 as_warn (_("Swapping instruction order"));
817 insn = FM00 | (insn2 << 32) | insn1;
818 }
819 else if (opcode2->op->unit == MU)
820 {
821 if (opcode1->op->unit == MU)
822 as_fatal (_("Two MU instructions may not be executed in parallel"));
823 else if (opcode1->op->unit == EITHER_BUT_PREFER_MU)
824 as_warn (_("Executing %s in IU may not work", opcode1->op->name));
825 as_warn (_("Swapping instruction order"));
826 insn = FM00 | (insn2 << 32) | insn1;
827 }
828 else
829 {
830 if (opcode2->op->unit == EITHER_BUT_PREFER_MU)
831 as_warn ("Executing %s in IU may not work", opcode2->op->name);
832
833 insn = FM00 | (insn1 << 32) | insn2;
834 fx = fx->next;
835 }
836 flag_explicitly_parallel = 0;
837 break;
838
839 case EXEC_SEQ: /* sequential */
840 if (opcode1->op->unit == IU)
841 as_fatal (_("IU instruction may not be in the left container"));
842 if (prev_left_kills_right_p)
843 as_warn (_("special left instruction `%s' kills instruction "
844 "`%s' in right container"),
845 opcode1->op->name, opcode2->op->name);
846 if (opcode2->op->unit == EITHER_BUT_PREFER_MU)
847 as_warn (_("Executing %s in IU may not work"), opcode2->op->name);
848 insn = FM01 | (insn1 << 32) | insn2;
849 fx = fx->next;
850 break;
851
852 case EXEC_REVSEQ: /* reverse sequential */
853 if (opcode2->op->unit == MU)
854 as_fatal (_("MU instruction may not be in the right container"));
855 if (opcode2->op->unit == EITHER_BUT_PREFER_MU)
856 as_warn (_("Executing %s in IU may not work"), opcode2->op->name);
857 insn = FM10 | (insn1 << 32) | insn2;
858 fx = fx->next;
859 break;
860
861 default:
862 as_fatal (_("unknown execution type passed to write_2_short()"));
863 }
864
865 /* printf("writing out %llx\n",insn); */
866 f = frag_more (8);
867 d30v_number_to_chars (f, insn, 8);
868
869 /* If the previous instruction was a 32-bit multiply but it is put into a
870 parallel container, mark the current instruction as being a 32-bit
871 multiply. */
872 if (prev_mul32_p && exec_type == EXEC_PARALLEL)
873 cur_mul32_p = 1;
874
875 for (j=0; j<2; j++)
876 {
877 for (i=0; i < fx->fc; i++)
878 {
879 if (fx->fix[i].reloc)
880 {
881 where = (f - frag_now->fr_literal) + 4*j;
882
883 fix_new_exp (frag_now,
884 where,
885 fx->fix[i].size,
886 &(fx->fix[i].exp),
887 fx->fix[i].pcrel,
888 fx->fix[i].reloc);
889 }
890 }
891
892 fx->fc = 0;
893 fx = fx->next;
894 }
895
896 return 0;
897 }
898
899
900 /* Check 2 instructions and determine if they can be safely */
901 /* executed in parallel. Returns 1 if they can be. */
902 static int
903 parallel_ok (op1, insn1, op2, insn2, exec_type)
904 struct d30v_insn *op1, *op2;
905 unsigned long insn1, insn2;
906 exec_type_enum exec_type;
907 {
908 int i, j, shift, regno, bits, ecc;
909 unsigned long flags, mask, flags_set1, flags_set2, flags_used1, flags_used2;
910 unsigned long ins, mod_reg[2][3], used_reg[2][3], flag_reg[2];
911 struct d30v_format *f;
912 struct d30v_opcode *op;
913
914 /* section 4.3: both instructions must not be IU or MU only */
915 if ((op1->op->unit == IU && op2->op->unit == IU)
916 || (op1->op->unit == MU && op2->op->unit == MU))
917 return 0;
918
919 /* first instruction must not be a jump to safely optimize, unless this
920 is an explicit parallel operation. */
921 if (exec_type != EXEC_PARALLEL
922 && (op1->op->flags_used & (FLAG_JMP | FLAG_JSR)))
923 return 0;
924
925 /* If one instruction is /TX or /XT and the other is /FX or /XF respectively,
926 then it is safe to allow the two to be done as parallel ops, since only
927 one will ever be executed at a time. */
928 if ((op1->ecc == ECC_TX && op2->ecc == ECC_FX)
929 || (op1->ecc == ECC_FX && op2->ecc == ECC_TX)
930 || (op1->ecc == ECC_XT && op2->ecc == ECC_XF)
931 || (op1->ecc == ECC_XF && op2->ecc == ECC_XT))
932 return 1;
933
934 /* [0] r0-r31
935 [1] r32-r63
936 [2] a0, a1, flag registers */
937
938 for (j = 0; j < 2; j++)
939 {
940 if (j == 0)
941 {
942 f = op1->form;
943 op = op1->op;
944 ecc = op1->ecc;
945 ins = insn1;
946 }
947 else
948 {
949 f = op2->form;
950 op = op2->op;
951 ecc = op2->ecc;
952 ins = insn2;
953 }
954 flag_reg[j] = 0;
955 mod_reg[j][0] = mod_reg[j][1] = 0;
956 mod_reg[j][2] = (op->flags_set & FLAG_ALL);
957 used_reg[j][0] = used_reg[j][1] = 0;
958 used_reg[j][2] = (op->flags_used & FLAG_ALL);
959
960 /* BSR/JSR always sets R62 */
961 if (op->flags_used & FLAG_JSR)
962 mod_reg[j][1] = (1L << (62-32));
963
964 /* conditional execution affects the flags_used */
965 switch (ecc)
966 {
967 case ECC_TX:
968 case ECC_FX:
969 used_reg[j][2] |= flag_reg[j] = FLAG_0;
970 break;
971
972 case ECC_XT:
973 case ECC_XF:
974 used_reg[j][2] |= flag_reg[j] = FLAG_1;
975 break;
976
977 case ECC_TT:
978 case ECC_TF:
979 used_reg[j][2] |= flag_reg[j] = (FLAG_0 | FLAG_1);
980 break;
981 }
982
983 for (i = 0; f->operands[i]; i++)
984 {
985 flags = d30v_operand_table[f->operands[i]].flags;
986 shift = 12 - d30v_operand_table[f->operands[i]].position;
987 bits = d30v_operand_table[f->operands[i]].bits;
988 if (bits == 32)
989 mask = 0xffffffff;
990 else
991 mask = 0x7FFFFFFF >> (31 - bits);
992
993 if ((flags & OPERAND_PLUS) || (flags & OPERAND_MINUS))
994 {
995 /* this is a post-increment or post-decrement */
996 /* the previous register needs to be marked as modified */
997
998 shift = 12 - d30v_operand_table[f->operands[i-1]].position;
999 regno = (ins >> shift) & 0x3f;
1000 if (regno >= 32)
1001 mod_reg[j][1] |= 1L << (regno - 32);
1002 else
1003 mod_reg[j][0] |= 1L << regno;
1004 }
1005 else if (flags & OPERAND_REG)
1006 {
1007 regno = (ins >> shift) & mask;
1008 /* the memory write functions don't have a destination register */
1009 if ((flags & OPERAND_DEST) && !(op->flags_set & FLAG_MEM))
1010 {
1011 /* MODIFIED registers and flags */
1012 if (flags & OPERAND_ACC)
1013 {
1014 if (regno == 0)
1015 mod_reg[j][2] |= FLAG_A0;
1016 else if (regno == 1)
1017 mod_reg[j][2] |= FLAG_A1;
1018 else
1019 abort ();
1020 }
1021 else if (flags & OPERAND_FLAG)
1022 mod_reg[j][2] |= 1L << regno;
1023 else if (!(flags & OPERAND_CONTROL))
1024 {
1025 int r, z;
1026
1027 /* need to check if there are two destination */
1028 /* registers, for example ld2w */
1029 if (flags & OPERAND_2REG)
1030 z = 1;
1031 else
1032 z = 0;
1033
1034 for (r = regno; r <= regno + z; r++)
1035 {
1036 if (r >= 32)
1037 mod_reg[j][1] |= 1L << (r - 32);
1038 else
1039 mod_reg[j][0] |= 1L << r;
1040 }
1041 }
1042 }
1043 else
1044 {
1045 /* USED, but not modified registers and flags */
1046 if (flags & OPERAND_ACC)
1047 {
1048 if (regno == 0)
1049 used_reg[j][2] |= FLAG_A0;
1050 else if (regno == 1)
1051 used_reg[j][2] |= FLAG_A1;
1052 else
1053 abort ();
1054 }
1055 else if (flags & OPERAND_FLAG)
1056 used_reg[j][2] |= 1L << regno;
1057 else if (!(flags & OPERAND_CONTROL))
1058 {
1059 int r, z;
1060
1061 /* need to check if there are two source */
1062 /* registers, for example st2w */
1063 if (flags & OPERAND_2REG)
1064 z = 1;
1065 else
1066 z = 0;
1067
1068 for (r = regno; r <= regno + z; r++)
1069 {
1070 if (r >= 32)
1071 used_reg[j][1] |= 1L << (r - 32);
1072 else
1073 used_reg[j][0] |= 1L << r;
1074 }
1075 }
1076 }
1077 }
1078 }
1079 }
1080
1081 flags_set1 = op1->op->flags_set;
1082 flags_set2 = op2->op->flags_set;
1083 flags_used1 = op1->op->flags_used;
1084 flags_used2 = op2->op->flags_used;
1085
1086 /* ST2W/ST4HB combined with ADDppp/SUBppp is illegal. */
1087 if (((flags_set1 & (FLAG_MEM | FLAG_2WORD)) == (FLAG_MEM | FLAG_2WORD)
1088 && (flags_used2 & FLAG_ADDSUBppp) != 0)
1089 || ((flags_set2 & (FLAG_MEM | FLAG_2WORD)) == (FLAG_MEM | FLAG_2WORD)
1090 && (flags_used1 & FLAG_ADDSUBppp) != 0))
1091 return 0;
1092
1093 /* Load instruction combined with half-word multiply is illegal. */
1094 if (((flags_used1 & FLAG_MEM) != 0 && (flags_used2 & FLAG_MUL16))
1095 || ((flags_used2 & FLAG_MEM) != 0 && (flags_used1 & FLAG_MUL16)))
1096 return 0;
1097
1098 /* Specifically allow add || add by removing carry, overflow bits dependency.
1099 This is safe, even if an addc follows since the IU takes the argument in
1100 the right container, and it writes its results last.
1101 However, don't paralellize add followed by addc or sub followed by
1102 subb. */
1103
1104 if (mod_reg[0][2] == FLAG_CVVA && mod_reg[1][2] == FLAG_CVVA
1105 && (used_reg[0][2] & ~flag_reg[0]) == 0
1106 && (used_reg[1][2] & ~flag_reg[1]) == 0
1107 && op1->op->unit == EITHER && op2->op->unit == EITHER)
1108 {
1109 mod_reg[0][2] = mod_reg[1][2] = 0;
1110 }
1111
1112 for (j = 0; j < 3; j++)
1113 {
1114 /* If the second instruction depends on the first, we obviously
1115 cannot parallelize. Note, the mod flag implies use, so
1116 check that as well. */
1117 /* If flag_explicitly_parallel is set, then the case of the
1118 second instruction using a register the first instruction
1119 modifies is assumed to be okay; we trust the human. We
1120 don't trust the human if both instructions modify the same
1121 register but we do trust the human if they modify the same
1122 flags. */
1123 if (flag_explicitly_parallel)
1124 {
1125 if ((j < 2) && (mod_reg[0][j] & mod_reg[1][j]) != 0)
1126 return 0;
1127 }
1128 else
1129 if ((mod_reg[0][j] & (mod_reg[1][j] | used_reg[1][j])) != 0)
1130 return 0;
1131 }
1132
1133 return 1;
1134 }
1135
1136
1137 /* This is the main entry point for the machine-dependent assembler. str points to a
1138 machine-dependent instruction. This function is supposed to emit the frags/bytes
1139 it assembles to. For the D30V, it mostly handles the special VLIW parsing and packing
1140 and leaves the difficult stuff to do_assemble(). */
1141
1142 static long long prev_insn = -1;
1143 static struct d30v_insn prev_opcode;
1144 static subsegT prev_subseg;
1145 static segT prev_seg = 0;
1146
1147 void
1148 md_assemble (str)
1149 char *str;
1150 {
1151 struct d30v_insn opcode;
1152 long long insn;
1153 exec_type_enum extype = EXEC_UNKNOWN; /* execution type; parallel, etc */
1154 static exec_type_enum etype = EXEC_UNKNOWN; /* saved extype. used for multiline instructions */
1155 char *str2;
1156
1157 if ((prev_insn != -1) && prev_seg
1158 && ((prev_seg != now_seg) || (prev_subseg != now_subseg)))
1159 d30v_cleanup (false);
1160
1161 if (d30v_current_align < 3)
1162 d30v_align (3, NULL, d30v_last_label);
1163 else if (d30v_current_align > 3)
1164 d30v_current_align = 3;
1165 d30v_last_label = NULL;
1166
1167 flag_explicitly_parallel = 0;
1168 flag_xp_state = 0;
1169 if (etype == EXEC_UNKNOWN)
1170 {
1171 /* look for the special multiple instruction separators */
1172 str2 = strstr (str, "||");
1173 if (str2)
1174 {
1175 extype = EXEC_PARALLEL;
1176 flag_xp_state = 1;
1177 }
1178 else
1179 {
1180 str2 = strstr (str, "->");
1181 if (str2)
1182 extype = EXEC_SEQ;
1183 else
1184 {
1185 str2 = strstr (str, "<-");
1186 if (str2)
1187 extype = EXEC_REVSEQ;
1188 }
1189 }
1190 /* str2 points to the separator, if one */
1191 if (str2)
1192 {
1193 *str2 = 0;
1194
1195 /* if two instructions are present and we already have one saved
1196 then first write it out */
1197 d30v_cleanup (false);
1198
1199 /* Assemble first instruction and save it. */
1200 prev_insn = do_assemble (str, &prev_opcode, 1, 0);
1201 if (prev_insn == -1)
1202 as_fatal (_("Cannot assemble instruction"));
1203 if (prev_opcode.form->form >= LONG)
1204 as_fatal (_("First opcode is long. Unable to mix instructions as specified."));
1205 fixups = fixups->next;
1206 str = str2 + 2;
1207 prev_seg = now_seg;
1208 prev_subseg = now_subseg;
1209 }
1210 }
1211
1212 insn = do_assemble (str, &opcode,
1213 (extype != EXEC_UNKNOWN || etype != EXEC_UNKNOWN),
1214 extype == EXEC_PARALLEL);
1215 if (insn == -1)
1216 {
1217 if (extype != EXEC_UNKNOWN)
1218 {
1219 etype = extype;
1220 return;
1221 }
1222 as_fatal (_("Cannot assemble instruction"));
1223 }
1224
1225 if (etype != EXEC_UNKNOWN)
1226 {
1227 extype = etype;
1228 etype = EXEC_UNKNOWN;
1229 }
1230
1231 /* Word multiply instructions must not be followed by either a load or a
1232 16-bit multiply instruction in the next cycle. */
1233 if ( (extype != EXEC_REVSEQ)
1234 && prev_mul32_p
1235 && (opcode.op->flags_used & (FLAG_MEM | FLAG_MUL16)))
1236 {
1237 /* However, load and multiply should able to be combined in a parallel
1238 operation, so check for that first. */
1239 if (prev_insn != -1
1240 && (opcode.op->flags_used & FLAG_MEM)
1241 && opcode.form->form < LONG
1242 && (extype == EXEC_PARALLEL || (Optimizing && extype == EXEC_UNKNOWN))
1243 && parallel_ok (&prev_opcode, (long)prev_insn,
1244 &opcode, (long)insn, extype)
1245 && write_2_short (&prev_opcode, (long)prev_insn,
1246 &opcode, (long)insn, extype, fixups) == 0)
1247 {
1248 /* no instructions saved */
1249 prev_insn = -1;
1250 return;
1251 }
1252 else
1253 {
1254 /* Can't parallelize, flush previous instruction and emit a word of NOPS,
1255 unless the previous instruction is a NOP, in which case just flush it,
1256 as this will generate a word of NOPs for us. */
1257
1258 if (prev_insn != -1 && (strcmp (prev_opcode.op->name, "nop") == 0))
1259 d30v_cleanup (false);
1260 else
1261 {
1262 char * f;
1263
1264 if (prev_insn != -1)
1265 d30v_cleanup (true);
1266 else
1267 {
1268 f = frag_more (8);
1269 d30v_number_to_chars (f, NOP2, 8);
1270
1271 if (warn_nops == NOP_ALL || warn_nops == NOP_MULTIPLY)
1272 {
1273 if (opcode.op->flags_used & FLAG_MEM)
1274 as_warn (_("word of NOPs added between word multiply and load"));
1275 else
1276 as_warn (_("word of NOPs added between word multiply and 16-bit multiply"));
1277 }
1278 }
1279 }
1280
1281 extype = EXEC_UNKNOWN;
1282 }
1283 }
1284 else if ( (extype == EXEC_REVSEQ)
1285 && cur_mul32_p
1286 && (prev_opcode.op->flags_used & (FLAG_MEM | FLAG_MUL16)))
1287 {
1288 /* Can't parallelize, flush current instruction and add a sequential NOP. */
1289 write_1_short (& opcode, (long) insn, fixups->next->next, true);
1290
1291 /* Make the previous instruction the current one. */
1292 extype = EXEC_UNKNOWN;
1293 insn = prev_insn;
1294 now_seg = prev_seg;
1295 now_subseg = prev_subseg;
1296 prev_insn = -1;
1297 cur_mul32_p = prev_mul32_p;
1298 prev_mul32_p = 0;
1299 memcpy (&opcode, &prev_opcode, sizeof (prev_opcode));
1300 }
1301
1302 /* If this is a long instruction, write it and any previous short instruction. */
1303 if (opcode.form->form >= LONG)
1304 {
1305 if (extype != EXEC_UNKNOWN)
1306 as_fatal (_("Instruction uses long version, so it cannot be mixed as specified"));
1307 d30v_cleanup (false);
1308 write_long (& opcode, insn, fixups);
1309 prev_insn = -1;
1310 }
1311 else if ((prev_insn != -1) &&
1312 (write_2_short
1313 (& prev_opcode, (long) prev_insn, & opcode, (long) insn, extype, fixups) == 0))
1314 {
1315 /* No instructions saved. */
1316 prev_insn = -1;
1317 }
1318 else
1319 {
1320 if (extype != EXEC_UNKNOWN)
1321 as_fatal (_("Unable to mix instructions as specified"));
1322
1323 /* Save off last instruction so it may be packed on next pass. */
1324 memcpy (&prev_opcode, &opcode, sizeof (prev_opcode));
1325 prev_insn = insn;
1326 prev_seg = now_seg;
1327 prev_subseg = now_subseg;
1328 fixups = fixups->next;
1329 prev_mul32_p = cur_mul32_p;
1330 }
1331 }
1332
1333
1334 /* do_assemble assembles a single instruction and returns an opcode */
1335 /* it returns -1 (an invalid opcode) on error */
1336
1337 static long long
1338 do_assemble (str, opcode, shortp, is_parallel)
1339 char *str;
1340 struct d30v_insn *opcode;
1341 int shortp;
1342 int is_parallel;
1343 {
1344 unsigned char *op_start, *save;
1345 unsigned char *op_end;
1346 char name[20];
1347 int cmp_hack, nlen = 0, fsize = (shortp ? FORCE_SHORT : 0);
1348 expressionS myops[6];
1349 long long insn;
1350
1351 /* Drop leading whitespace */
1352 while (*str == ' ')
1353 str++;
1354
1355 /* find the opcode end */
1356 for (op_start = op_end = (unsigned char *) (str);
1357 *op_end
1358 && nlen < 20
1359 && *op_end != '/'
1360 && !is_end_of_line[*op_end] && *op_end != ' ';
1361 op_end++)
1362 {
1363 name[nlen] = tolower (op_start[nlen]);
1364 nlen++;
1365 }
1366
1367 if (nlen == 0)
1368 return (-1);
1369
1370 name[nlen] = 0;
1371
1372 /* if there is an execution condition code, handle it */
1373 if (*op_end == '/')
1374 {
1375 int i = 0;
1376 while ( (i < ECC_MAX) && strncasecmp (d30v_ecc_names[i], op_end + 1, 2))
1377 i++;
1378
1379 if (i == ECC_MAX)
1380 {
1381 char tmp[4];
1382 strncpy (tmp, op_end + 1, 2);
1383 tmp[2] = 0;
1384 as_fatal (_("unknown condition code: %s"),tmp);
1385 return -1;
1386 }
1387 /* printf("condition code=%d\n",i); */
1388 opcode->ecc = i;
1389 op_end += 3;
1390 }
1391 else
1392 opcode->ecc = ECC_AL;
1393
1394
1395 /* CMP and CMPU change their name based on condition codes */
1396 if (!strncmp (name, "cmp", 3))
1397 {
1398 int p,i;
1399 char **str = (char **)d30v_cc_names;
1400 if (name[3] == 'u')
1401 p = 4;
1402 else
1403 p = 3;
1404
1405 for (i=1; *str && strncmp (*str, & name[p], 2); i++, str++)
1406 ;
1407
1408 /* cmpu only supports some condition codes */
1409 if (p == 4)
1410 {
1411 if (i < 3 || i > 6)
1412 {
1413 name[p+2]=0;
1414 as_fatal (_("cmpu doesn't support condition code %s"),&name[p]);
1415 }
1416 }
1417
1418 if (!*str)
1419 {
1420 name[p+2]=0;
1421 as_fatal (_("unknown condition code: %s"),&name[p]);
1422 }
1423
1424 cmp_hack = i;
1425 name[p] = 0;
1426 }
1427 else
1428 cmp_hack = 0;
1429
1430 /* printf("cmp_hack=%d\n",cmp_hack); */
1431
1432 /* need to look for .s or .l */
1433 if (name[nlen-2] == '.')
1434 {
1435 switch (name[nlen-1])
1436 {
1437 case 's':
1438 fsize = FORCE_SHORT;
1439 break;
1440 case 'l':
1441 fsize = FORCE_LONG;
1442 break;
1443 }
1444 name[nlen-2] = 0;
1445 }
1446
1447 /* find the first opcode with the proper name */
1448 opcode->op = (struct d30v_opcode *)hash_find (d30v_hash, name);
1449 if (opcode->op == NULL)
1450 as_fatal (_("unknown opcode: %s"),name);
1451
1452 save = input_line_pointer;
1453 input_line_pointer = op_end;
1454 while (!(opcode->form = find_format (opcode->op, myops, fsize, cmp_hack)))
1455 {
1456 opcode->op++;
1457 if (strcmp (opcode->op->name, name))
1458 as_fatal (_("operands for opcode `%s' do not match any valid format"), name);
1459 }
1460 input_line_pointer = save;
1461
1462 insn = build_insn (opcode, myops);
1463
1464 /* Propigate multiply status */
1465 if (insn != -1)
1466 {
1467 if (is_parallel && prev_mul32_p)
1468 cur_mul32_p = 1;
1469 else
1470 {
1471 prev_mul32_p = cur_mul32_p;
1472 cur_mul32_p = (opcode->op->flags_used & FLAG_MUL32) != 0;
1473 }
1474 }
1475
1476 /* Propagate left_kills_right status */
1477 if (insn != -1)
1478 {
1479 prev_left_kills_right_p = cur_left_kills_right_p;
1480
1481 if (opcode->op->flags_set & FLAG_LKR)
1482 {
1483 cur_left_kills_right_p = 1;
1484
1485 if (strcmp (opcode->op->name, "mvtsys") == 0)
1486 {
1487 /* Left kills right for only mvtsys only for PSW/PSWH/PSWL/flags target. */
1488 if ((myops[0].X_op == O_register) &&
1489 ((myops[0].X_add_number == OPERAND_CONTROL) || /* psw */
1490 (myops[0].X_add_number == OPERAND_CONTROL+MAX_CONTROL_REG+2) || /* pswh */
1491 (myops[0].X_add_number == OPERAND_CONTROL+MAX_CONTROL_REG+1) || /* pswl */
1492 (myops[0].X_add_number == OPERAND_FLAG+0) || /* f0 */
1493 (myops[0].X_add_number == OPERAND_FLAG+1) || /* f1 */
1494 (myops[0].X_add_number == OPERAND_FLAG+2) || /* f2 */
1495 (myops[0].X_add_number == OPERAND_FLAG+3) || /* f3 */
1496 (myops[0].X_add_number == OPERAND_FLAG+4) || /* f4 */
1497 (myops[0].X_add_number == OPERAND_FLAG+5) || /* f5 */
1498 (myops[0].X_add_number == OPERAND_FLAG+6) || /* f6 */
1499 (myops[0].X_add_number == OPERAND_FLAG+7))) /* f7 */
1500 {
1501 cur_left_kills_right_p = 1;
1502 }
1503 else
1504 {
1505 /* Other mvtsys target registers don't kill right instruction. */
1506 cur_left_kills_right_p = 0;
1507 }
1508 } /* mvtsys */
1509 }
1510 else
1511 cur_left_kills_right_p = 0;
1512 }
1513
1514
1515 return insn;
1516 }
1517
1518
1519 /* find_format() gets a pointer to an entry in the format table.
1520 It must look at all formats for an opcode and use the operands
1521 to choose the correct one. Returns NULL on error. */
1522
1523 static struct d30v_format *
1524 find_format (opcode, myops, fsize, cmp_hack)
1525 struct d30v_opcode *opcode;
1526 expressionS myops[];
1527 int fsize;
1528 int cmp_hack;
1529 {
1530 int numops, match, index, i=0, j, k;
1531 struct d30v_format *fm;
1532
1533 /* Get all the operands and save them as expressions. */
1534 numops = get_operands (myops, cmp_hack);
1535
1536 while ((index = opcode->format[i++]) != 0)
1537 {
1538 if (fsize == FORCE_SHORT && index >= LONG)
1539 continue;
1540
1541 if (fsize == FORCE_LONG && index < LONG)
1542 continue;
1543
1544 fm = (struct d30v_format *)&d30v_format_table[index];
1545 k = index;
1546 while (fm->form == index)
1547 {
1548 match = 1;
1549 /* Now check the operands for compatibility. */
1550 for (j = 0; match && fm->operands[j]; j++)
1551 {
1552 int flags = d30v_operand_table[fm->operands[j]].flags;
1553 int bits = d30v_operand_table[fm->operands[j]].bits;
1554 int X_op = myops[j].X_op;
1555 int num = myops[j].X_add_number;
1556
1557 if (flags & OPERAND_SPECIAL)
1558 break;
1559 else if (X_op == O_illegal)
1560 match = 0;
1561 else if (flags & OPERAND_REG)
1562 {
1563 if (X_op != O_register
1564 || ((flags & OPERAND_ACC) && !(num & OPERAND_ACC))
1565 || (!(flags & OPERAND_ACC) && (num & OPERAND_ACC))
1566 || ((flags & OPERAND_FLAG) && !(num & OPERAND_FLAG))
1567 || (!(flags & OPERAND_FLAG) && (num & OPERAND_FLAG))
1568 || ((flags & OPERAND_CONTROL)
1569 && !(num & (OPERAND_CONTROL | OPERAND_FLAG))))
1570 {
1571 match = 0;
1572 }
1573 }
1574 else if (((flags & OPERAND_MINUS)
1575 && (X_op != O_absent || num != OPERAND_MINUS))
1576 || ((flags & OPERAND_PLUS)
1577 && (X_op != O_absent || num != OPERAND_PLUS))
1578 || ((flags & OPERAND_ATMINUS)
1579 && (X_op != O_absent || num != OPERAND_ATMINUS))
1580 || ((flags & OPERAND_ATPAR)
1581 && (X_op != O_absent || num != OPERAND_ATPAR))
1582 || ((flags & OPERAND_ATSIGN)
1583 && (X_op != O_absent || num != OPERAND_ATSIGN)))
1584 {
1585 match=0;
1586 }
1587 else if (flags & OPERAND_NUM)
1588 {
1589 /* A number can be a constant or symbol expression. */
1590
1591 /* If we have found a register name, but that name also
1592 matches a symbol, then re-parse the name as an expression. */
1593 if (X_op == O_register
1594 && symbol_find ((char *) myops[j].X_op_symbol))
1595 {
1596 input_line_pointer = (char *) myops[j].X_op_symbol;
1597 expression (& myops[j]);
1598 }
1599
1600 /* Turn an expression into a symbol for later resolution. */
1601 if (X_op != O_absent && X_op != O_constant
1602 && X_op != O_symbol && X_op != O_register
1603 && X_op != O_big)
1604 {
1605 symbolS *sym = make_expr_symbol (&myops[j]);
1606 myops[j].X_op = X_op = O_symbol;
1607 myops[j].X_add_symbol = sym;
1608 myops[j].X_add_number = num = 0;
1609 }
1610
1611 if (fm->form >= LONG)
1612 {
1613 /* If we're testing for a LONG format, either fits. */
1614 if (X_op != O_constant && X_op != O_symbol)
1615 match = 0;
1616 }
1617 else if (fm->form < LONG
1618 && ((fsize == FORCE_SHORT && X_op == O_symbol)
1619 || (fm->form == SHORT_D2 && j == 0)))
1620 match = 1;
1621 /* This is the tricky part. Will the constant or symbol
1622 fit into the space in the current format? */
1623 else if (X_op == O_constant)
1624 {
1625 if (check_range (num, bits, flags))
1626 match = 0;
1627 }
1628 else if (X_op == O_symbol
1629 && S_IS_DEFINED (myops[j].X_add_symbol)
1630 && S_GET_SEGMENT (myops[j].X_add_symbol) == now_seg
1631 && opcode->reloc_flag == RELOC_PCREL)
1632 {
1633 /* If the symbol is defined, see if the value will fit
1634 into the form we're considering. */
1635 fragS *f;
1636 long value;
1637
1638 /* Calculate the current address by running through the
1639 previous frags and adding our current offset. */
1640 value = 0;
1641 for (f = frchain_now->frch_root; f; f = f->fr_next)
1642 value += f->fr_fix + f->fr_offset;
1643 value = (S_GET_VALUE (myops[j].X_add_symbol) - value
1644 - (obstack_next_free (&frchain_now->frch_obstack)
1645 - frag_now->fr_literal));
1646 if (check_range (value, bits, flags))
1647 match = 0;
1648 }
1649 else
1650 match = 0;
1651 }
1652 }
1653 /* printf("through the loop: match=%d\n",match); */
1654 /* We're only done if the operands matched so far AND there
1655 are no more to check. */
1656 if (match && myops[j].X_op == 0)
1657 return fm;
1658 fm = (struct d30v_format *)&d30v_format_table[++k];
1659 }
1660 /* printf("trying another format: i=%d\n",i); */
1661 }
1662 return NULL;
1663 }
1664
1665 /* if while processing a fixup, a reloc really needs to be created */
1666 /* then it is done here */
1667
1668 arelent *
1669 tc_gen_reloc (seg, fixp)
1670 asection *seg;
1671 fixS *fixp;
1672 {
1673 arelent *reloc;
1674 reloc = (arelent *) xmalloc (sizeof (arelent));
1675 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
1676 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1677 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1678 if (reloc->howto == (reloc_howto_type *) NULL)
1679 {
1680 as_bad_where (fixp->fx_file, fixp->fx_line,
1681 _("reloc %d not supported by object file format"), (int)fixp->fx_r_type);
1682 return NULL;
1683 }
1684 reloc->addend = fixp->fx_addnumber;
1685 return reloc;
1686 }
1687
1688 int
1689 md_estimate_size_before_relax (fragp, seg)
1690 fragS *fragp;
1691 asection *seg;
1692 {
1693 abort ();
1694 return 0;
1695 }
1696
1697 long
1698 md_pcrel_from_section (fixp, sec)
1699 fixS *fixp;
1700 segT sec;
1701 {
1702 if (fixp->fx_addsy != (symbolS *)NULL && (!S_IS_DEFINED (fixp->fx_addsy) ||
1703 (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
1704 return 0;
1705 return fixp->fx_frag->fr_address + fixp->fx_where;
1706 }
1707
1708 int
1709 md_apply_fix3 (fixp, valuep, seg)
1710 fixS *fixp;
1711 valueT *valuep;
1712 segT seg;
1713 {
1714 char *where;
1715 unsigned long insn, insn2;
1716 long value;
1717
1718 if (fixp->fx_addsy == (symbolS *) NULL)
1719 {
1720 value = *valuep;
1721 fixp->fx_done = 1;
1722 }
1723 else if (fixp->fx_pcrel)
1724 {
1725 value = *valuep;
1726 }
1727 else
1728 {
1729 value = fixp->fx_offset;
1730 if (fixp->fx_subsy != (symbolS *) NULL)
1731 {
1732 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
1733 value -= S_GET_VALUE (fixp->fx_subsy);
1734 else
1735 {
1736 /* We don't actually support subtracting a symbol. */
1737 as_bad_where (fixp->fx_file, fixp->fx_line,
1738 _("expression too complex"));
1739 }
1740 }
1741 }
1742
1743 /* Fetch the instruction, insert the fully resolved operand
1744 value, and stuff the instruction back again. */
1745 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1746 insn = bfd_getb32 ((unsigned char *) where);
1747
1748 switch (fixp->fx_r_type)
1749 {
1750 case BFD_RELOC_8:
1751 /* Caused by a bad .byte directive. */
1752 as_fatal (_("line %d: unable to place address of symbol '%s' into a byte"),
1753 fixp->fx_line, S_GET_NAME (fixp->fx_addsy));
1754 break;
1755
1756 case BFD_RELOC_16:
1757 /* Caused by a bad .short directive. */
1758 as_fatal (_("line %d: unable to place address of symbol '%s' into a short"),
1759 fixp->fx_line, S_GET_NAME (fixp->fx_addsy));
1760 break;
1761
1762 case BFD_RELOC_64:
1763 /* Caused by a bad .quad directive. */
1764 as_fatal (_("line %d: unable to place address of symbol '%s' into a .quad"),
1765 fixp->fx_line, S_GET_NAME (fixp->fx_addsy));
1766 break;
1767
1768 case BFD_RELOC_D30V_6:
1769 check_size (value, 6, fixp->fx_file, fixp->fx_line);
1770 insn |= value & 0x3F;
1771 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1772 break;
1773
1774 case BFD_RELOC_D30V_9_PCREL:
1775 if (fixp->fx_where & 0x7)
1776 {
1777 if (fixp->fx_done)
1778 value += 4;
1779 else
1780 fixp->fx_r_type = BFD_RELOC_D30V_9_PCREL_R;
1781 }
1782 check_size (value, 9, fixp->fx_file, fixp->fx_line);
1783 insn |= ((value >> 3) & 0x3F) << 12;
1784 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1785 break;
1786
1787 case BFD_RELOC_D30V_15:
1788 check_size (value, 15, fixp->fx_file, fixp->fx_line);
1789 insn |= (value >> 3) & 0xFFF;
1790 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1791 break;
1792
1793 case BFD_RELOC_D30V_15_PCREL:
1794 if (fixp->fx_where & 0x7)
1795 {
1796 if (fixp->fx_done)
1797 value += 4;
1798 else
1799 fixp->fx_r_type = BFD_RELOC_D30V_15_PCREL_R;
1800 }
1801 check_size (value, 15, fixp->fx_file, fixp->fx_line);
1802 insn |= (value >> 3) & 0xFFF;
1803 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1804 break;
1805
1806 case BFD_RELOC_D30V_21:
1807 check_size (value, 21, fixp->fx_file, fixp->fx_line);
1808 insn |= (value >> 3) & 0x3FFFF;
1809 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1810 break;
1811
1812 case BFD_RELOC_D30V_21_PCREL:
1813 if (fixp->fx_where & 0x7)
1814 {
1815 if (fixp->fx_done)
1816 value += 4;
1817 else
1818 fixp->fx_r_type = BFD_RELOC_D30V_21_PCREL_R;
1819 }
1820 check_size (value, 21, fixp->fx_file, fixp->fx_line);
1821 insn |= (value >> 3) & 0x3FFFF;
1822 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1823 break;
1824
1825 case BFD_RELOC_D30V_32:
1826 insn2 = bfd_getb32 ((unsigned char *) where + 4);
1827 insn |= (value >> 26) & 0x3F; /* top 6 bits */
1828 insn2 |= ((value & 0x03FC0000) << 2); /* next 8 bits */
1829 insn2 |= value & 0x0003FFFF; /* bottom 18 bits */
1830 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1831 bfd_putb32 ((bfd_vma) insn2, (unsigned char *) where + 4);
1832 break;
1833
1834 case BFD_RELOC_D30V_32_PCREL:
1835 insn2 = bfd_getb32 ((unsigned char *) where + 4);
1836 insn |= (value >> 26) & 0x3F; /* top 6 bits */
1837 insn2 |= ((value & 0x03FC0000) << 2); /* next 8 bits */
1838 insn2 |= value & 0x0003FFFF; /* bottom 18 bits */
1839 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1840 bfd_putb32 ((bfd_vma) insn2, (unsigned char *) where + 4);
1841 break;
1842
1843 case BFD_RELOC_32:
1844 bfd_putb32 ((bfd_vma) value, (unsigned char *) where);
1845 break;
1846
1847 default:
1848 as_fatal (_("line %d: unknown relocation type: 0x%x"),fixp->fx_line,fixp->fx_r_type);
1849 }
1850 return 0;
1851 }
1852
1853
1854 /* d30v_cleanup() is called after the assembler has finished parsing the input
1855 file or after a label is defined. Because the D30V assembler sometimes saves short
1856 instructions to see if it can package them with the next instruction, there may
1857 be a short instruction that still needs written. */
1858 int
1859 d30v_cleanup (use_sequential)
1860 int use_sequential;
1861 {
1862 segT seg;
1863 subsegT subseg;
1864
1865 if (prev_insn != -1)
1866 {
1867 seg = now_seg;
1868 subseg = now_subseg;
1869 subseg_set (prev_seg, prev_subseg);
1870 write_1_short (&prev_opcode, (long)prev_insn, fixups->next, use_sequential);
1871 subseg_set (seg, subseg);
1872 prev_insn = -1;
1873 if (use_sequential)
1874 prev_mul32_p = false;
1875 }
1876 return 1;
1877 }
1878
1879 static void
1880 d30v_number_to_chars (buf, value, n)
1881 char *buf; /* Return 'nbytes' of chars here. */
1882 long long value; /* The value of the bits. */
1883 int n; /* Number of bytes in the output. */
1884 {
1885 while (n--)
1886 {
1887 buf[n] = value & 0xff;
1888 value >>= 8;
1889 }
1890 }
1891
1892
1893 /* This function is called at the start of every line. */
1894 /* it checks to see if the first character is a '.' */
1895 /* which indicates the start of a pseudo-op. If it is, */
1896 /* then write out any unwritten instructions */
1897
1898 void
1899 d30v_start_line ()
1900 {
1901 char *c = input_line_pointer;
1902
1903 while (isspace (*c))
1904 c++;
1905
1906 if (*c == '.')
1907 d30v_cleanup (false);
1908 }
1909
1910 static void
1911 check_size (value, bits, file, line)
1912 long value;
1913 int bits;
1914 char *file;
1915 int line;
1916 {
1917 int tmp, max;
1918
1919 if (value < 0)
1920 tmp = ~value;
1921 else
1922 tmp = value;
1923
1924 max = (1 << (bits - 1)) - 1;
1925
1926 if (tmp > max)
1927 as_bad_where (file, line, _("value too large to fit in %d bits"), bits);
1928
1929 return;
1930 }
1931
1932 /* d30v_frob_label() is called when after a label is recognized. */
1933
1934 void
1935 d30v_frob_label (lab)
1936 symbolS *lab;
1937 {
1938 /* Emit any pending instructions. */
1939 d30v_cleanup (false);
1940
1941 /* Update the label's address with the current output pointer. */
1942 lab->sy_frag = frag_now;
1943 S_SET_VALUE (lab, (valueT) frag_now_fix ());
1944
1945 /* Record this label for future adjustment after we find out what
1946 kind of data it references, and the required alignment therewith. */
1947 d30v_last_label = lab;
1948 }
1949
1950 /* Hook into cons for capturing alignment changes. */
1951
1952 void
1953 d30v_cons_align (size)
1954 int size;
1955 {
1956 int log_size;
1957
1958 log_size = 0;
1959 while ((size >>= 1) != 0)
1960 ++log_size;
1961
1962 if (d30v_current_align < log_size)
1963 d30v_align (log_size, (char *) NULL, NULL);
1964 else if (d30v_current_align > log_size)
1965 d30v_current_align = log_size;
1966 d30v_last_label = NULL;
1967 }
1968
1969 /* Called internally to handle all alignment needs. This takes care
1970 of eliding calls to frag_align if'n the cached current alignment
1971 says we've already got it, as well as taking care of the auto-aligning
1972 labels wrt code. */
1973
1974 static void
1975 d30v_align (n, pfill, label)
1976 int n;
1977 char *pfill;
1978 symbolS *label;
1979 {
1980 /* The front end is prone to changing segments out from under us
1981 temporarily when -g is in effect. */
1982 int switched_seg_p = (d30v_current_align_seg != now_seg);
1983
1984 /* Do not assume that if 'd30v_current_align >= n' and
1985 '! switched_seg_p' that it is safe to avoid performing
1986 this alignement request. The alignment of the current frag
1987 can be changed under our feet, for example by a .ascii
1988 directive in the source code. cf testsuite/gas/d30v/reloc.s */
1989
1990 d30v_cleanup (false);
1991
1992 if (pfill == NULL)
1993 {
1994 if (n > 2
1995 && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
1996 {
1997 static char const nop[4] = { 0x00, 0xf0, 0x00, 0x00 };
1998
1999 /* First, make sure we're on a four-byte boundary, in case
2000 someone has been putting .byte values the text section. */
2001 if (d30v_current_align < 2 || switched_seg_p)
2002 frag_align (2, 0, 0);
2003 frag_align_pattern (n, nop, sizeof nop, 0);
2004 }
2005 else
2006 frag_align (n, 0, 0);
2007 }
2008 else
2009 frag_align (n, *pfill, 0);
2010
2011 if (!switched_seg_p)
2012 d30v_current_align = n;
2013
2014 if (label != NULL)
2015 {
2016 symbolS * sym;
2017 int label_seen = false;
2018 struct frag * old_frag;
2019 valueT old_value;
2020 valueT new_value;
2021
2022 assert (S_GET_SEGMENT (label) == now_seg);
2023
2024 old_frag = label->sy_frag;
2025 old_value = S_GET_VALUE (label);
2026 new_value = (valueT) frag_now_fix ();
2027
2028 /* It is possible to have more than one label at a particular
2029 address, especially if debugging is enabled, so we must
2030 take care to adjust all the labels at this address in this
2031 fragment. To save time we search from the end of the symbol
2032 list, backwards, since the symbols we are interested in are
2033 almost certainly the ones that were most recently added.
2034 Also to save time we stop searching once we have seen at least
2035 one matching label, and we encounter a label that is no longer
2036 in the target fragment. Note, this search is guaranteed to
2037 find at least one match when sym == label, so no special case
2038 code is necessary. */
2039 for (sym = symbol_lastP; sym != NULL; sym = sym->sy_previous)
2040 {
2041 if (sym->sy_frag == old_frag && S_GET_VALUE (sym) == old_value)
2042 {
2043 label_seen = true;
2044 sym->sy_frag = frag_now;
2045 S_SET_VALUE (sym, new_value);
2046 }
2047 else if (label_seen && sym->sy_frag != old_frag)
2048 break;
2049 }
2050 }
2051
2052 record_alignment (now_seg, n);
2053 }
2054
2055 /* Handle the .align pseudo-op. This aligns to a power of two. We
2056 hook here to latch the current alignment. */
2057
2058 static void
2059 s_d30v_align (ignore)
2060 int ignore;
2061 {
2062 int align;
2063 char fill, *pfill = NULL;
2064 long max_alignment = 15;
2065
2066 align = get_absolute_expression ();
2067 if (align > max_alignment)
2068 {
2069 align = max_alignment;
2070 as_warn (_("Alignment too large: %d assumed"), align);
2071 }
2072 else if (align < 0)
2073 {
2074 as_warn (_("Alignment negative: 0 assumed"));
2075 align = 0;
2076 }
2077
2078 if (*input_line_pointer == ',')
2079 {
2080 input_line_pointer++;
2081 fill = get_absolute_expression ();
2082 pfill = &fill;
2083 }
2084
2085 d30v_last_label = NULL;
2086 d30v_align (align, pfill, NULL);
2087
2088 demand_empty_rest_of_line ();
2089 }
2090
2091 /* Handle the .text pseudo-op. This is like the usual one, but it
2092 clears the saved last label and resets known alignment. */
2093
2094 static void
2095 s_d30v_text (i)
2096 int i;
2097
2098 {
2099 s_text (i);
2100 d30v_last_label = NULL;
2101 d30v_current_align = 0;
2102 d30v_current_align_seg = now_seg;
2103 }
2104
2105 /* Handle the .data pseudo-op. This is like the usual one, but it
2106 clears the saved last label and resets known alignment. */
2107
2108 static void
2109 s_d30v_data (i)
2110 int i;
2111 {
2112 s_data (i);
2113 d30v_last_label = NULL;
2114 d30v_current_align = 0;
2115 d30v_current_align_seg = now_seg;
2116 }
2117
2118 /* Handle the .section pseudo-op. This is like the usual one, but it
2119 clears the saved last label and resets known alignment. */
2120
2121 static void
2122 s_d30v_section (ignore)
2123 int ignore;
2124 {
2125 obj_elf_section (ignore);
2126 d30v_last_label = NULL;
2127 d30v_current_align = 0;
2128 d30v_current_align_seg = now_seg;
2129 }
This page took 0.092335 seconds and 4 git commands to generate.