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