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