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