* config/tc-sparc.c (U0x80000000, U0xffffffff): New constants.
[binutils-gdb.git] / gas / config / tc-d10v.c
1 /* tc-d10v.c -- Assembler code for the Mitsubishi D10V
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
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 "as.h"
24 #include "safe-ctype.h"
25 #include "subsegs.h"
26 #include "opcode/d10v.h"
27 #include "elf/ppc.h"
28 //#include "read.h"
29
30 const char comment_chars[] = ";";
31 const char line_comment_chars[] = "#";
32 const char line_separator_chars[] = "";
33 const char *md_shortopts = "O";
34 const char EXP_CHARS[] = "eE";
35 const char FLT_CHARS[] = "dD";
36
37 int Optimizing = 0;
38
39 #define AT_WORD_P(X) ((X)->X_op == O_right_shift \
40 && (X)->X_op_symbol != NULL \
41 && symbol_constant_p ((X)->X_op_symbol) \
42 && S_GET_VALUE ((X)->X_op_symbol) == AT_WORD_RIGHT_SHIFT)
43 #define AT_WORD_RIGHT_SHIFT 2
44
45 /* Fixups. */
46 #define MAX_INSN_FIXUPS (5)
47 struct d10v_fixup
48 {
49 expressionS exp;
50 int operand;
51 int pcrel;
52 int size;
53 bfd_reloc_code_real_type reloc;
54 };
55
56 typedef struct _fixups
57 {
58 int fc;
59 struct d10v_fixup fix[MAX_INSN_FIXUPS];
60 struct _fixups *next;
61 } Fixups;
62
63 static Fixups FixUps[2];
64 static Fixups *fixups;
65
66 static int do_not_ignore_hash = 0;
67
68 typedef int packing_type;
69 #define PACK_UNSPEC (0) /* Packing order not specified. */
70 #define PACK_PARALLEL (1) /* "||" */
71 #define PACK_LEFT_RIGHT (2) /* "->" */
72 #define PACK_RIGHT_LEFT (3) /* "<-" */
73 static packing_type etype = PACK_UNSPEC; /* Used by d10v_cleanup. */
74
75 /* True if instruction swapping warnings should be inhibited.
76 --nowarnswap. */
77 static boolean flag_warn_suppress_instructionswap;
78
79 /* True if instruction packing should be performed when --gstabs is specified.
80 --gstabs-packing, --no-gstabs-packing. */
81 static boolean flag_allow_gstabs_packing = 1;
82
83 /* Local functions. */
84 static int reg_name_search PARAMS ((char *name));
85 static int register_name PARAMS ((expressionS *expressionP));
86 static int check_range PARAMS ((unsigned long num, int bits, int flags));
87 static int postfix PARAMS ((char *p));
88 static bfd_reloc_code_real_type get_reloc PARAMS ((struct d10v_operand *op));
89 static int get_operands PARAMS ((expressionS exp[]));
90 static struct d10v_opcode *find_opcode PARAMS ((struct d10v_opcode *opcode, expressionS ops[]));
91 static unsigned long build_insn PARAMS ((struct d10v_opcode *opcode, expressionS *opers, unsigned long insn));
92 static void write_long PARAMS ((unsigned long insn, Fixups *fx));
93 static void write_1_short PARAMS ((struct d10v_opcode *opcode, unsigned long insn, Fixups *fx));
94 static int write_2_short PARAMS ((struct d10v_opcode *opcode1, unsigned long insn1,
95 struct d10v_opcode *opcode2, unsigned long insn2, packing_type exec_type, Fixups *fx));
96 static unsigned long do_assemble PARAMS ((char *str, struct d10v_opcode **opcode));
97 static unsigned long d10v_insert_operand PARAMS (( unsigned long insn, int op_type,
98 offsetT value, int left, fixS *fix));
99 static int parallel_ok PARAMS ((struct d10v_opcode *opcode1, unsigned long insn1,
100 struct d10v_opcode *opcode2, unsigned long insn2,
101 packing_type exec_type));
102 static symbolS * find_symbol_matching_register PARAMS ((expressionS *));
103
104 struct option md_longopts[] =
105 {
106 #define OPTION_NOWARNSWAP (OPTION_MD_BASE)
107 {"nowarnswap", no_argument, NULL, OPTION_NOWARNSWAP},
108 #define OPTION_GSTABSPACKING (OPTION_MD_BASE + 1)
109 {"gstabspacking", no_argument, NULL, OPTION_GSTABSPACKING},
110 {"gstabs-packing", no_argument, NULL, OPTION_GSTABSPACKING},
111 #define OPTION_NOGSTABSPACKING (OPTION_MD_BASE + 2)
112 {"nogstabspacking", no_argument, NULL, OPTION_NOGSTABSPACKING},
113 {"no-gstabs-packing", no_argument, NULL, OPTION_NOGSTABSPACKING},
114 {NULL, no_argument, NULL, 0}
115 };
116
117 size_t md_longopts_size = sizeof (md_longopts);
118
119 static void d10v_dot_word PARAMS ((int));
120
121 /* The target specific pseudo-ops which we support. */
122 const pseudo_typeS md_pseudo_table[] =
123 {
124 { "word", d10v_dot_word, 2 },
125 { NULL, NULL, 0 }
126 };
127
128 /* Opcode hash table. */
129 static struct hash_control *d10v_hash;
130
131 /* Do a binary search of the d10v_predefined_registers array to see if
132 NAME is a valid regiter name. Return the register number from the
133 array on success, or -1 on failure. */
134
135 static int
136 reg_name_search (name)
137 char *name;
138 {
139 int middle, low, high;
140 int cmp;
141
142 low = 0;
143 high = d10v_reg_name_cnt () - 1;
144
145 do
146 {
147 middle = (low + high) / 2;
148 cmp = strcasecmp (name, d10v_predefined_registers[middle].name);
149 if (cmp < 0)
150 high = middle - 1;
151 else if (cmp > 0)
152 low = middle + 1;
153 else
154 return d10v_predefined_registers[middle].value;
155 }
156 while (low <= high);
157 return -1;
158 }
159
160 /* Check the string at input_line_pointer
161 to see if it is a valid register name. */
162
163 static int
164 register_name (expressionP)
165 expressionS *expressionP;
166 {
167 int reg_number;
168 char c, *p = input_line_pointer;
169
170 while (*p
171 && *p != '\n' && *p != '\r' && *p != ',' && *p != ' ' && *p != ')')
172 p++;
173
174 c = *p;
175 if (c)
176 *p++ = 0;
177
178 /* Look to see if it's in the register table. */
179 reg_number = reg_name_search (input_line_pointer);
180 if (reg_number >= 0)
181 {
182 expressionP->X_op = O_register;
183 /* Temporarily store a pointer to the string here. */
184 expressionP->X_op_symbol = (symbolS *) input_line_pointer;
185 expressionP->X_add_number = reg_number;
186 input_line_pointer = p;
187 return 1;
188 }
189 if (c)
190 *(p - 1) = c;
191 return 0;
192 }
193
194 static int
195 check_range (num, bits, flags)
196 unsigned long num;
197 int bits;
198 int flags;
199 {
200 long min, max;
201 int retval = 0;
202
203 /* Don't bother checking 16-bit values. */
204 if (bits == 16)
205 return 0;
206
207 if (flags & OPERAND_SHIFT)
208 {
209 /* All special shift operands are unsigned and <= 16.
210 We allow 0 for now. */
211 if (num > 16)
212 return 1;
213 else
214 return 0;
215 }
216
217 if (flags & OPERAND_SIGNED)
218 {
219 /* Signed 3-bit integers are restricted to the (-2, 3) range. */
220 if (flags & RESTRICTED_NUM3)
221 {
222 if ((long) num < -2 || (long) num > 3)
223 retval = 1;
224 }
225 else
226 {
227 max = (1 << (bits - 1)) - 1;
228 min = - (1 << (bits - 1));
229 if (((long) num > max) || ((long) num < min))
230 retval = 1;
231 }
232 }
233 else
234 {
235 max = (1 << bits) - 1;
236 min = 0;
237 if (((long) num > max) || ((long) num < min))
238 retval = 1;
239 }
240 return retval;
241 }
242
243 void
244 md_show_usage (stream)
245 FILE *stream;
246 {
247 fprintf (stream, _("D10V options:\n\
248 -O Optimize. Will do some operations in parallel.\n\
249 --gstabs-packing Pack adjacent short instructions together even\n\
250 when --gstabs is specified. On by default.\n\
251 --no-gstabs-packing If --gstabs is specified, do not pack adjacent\n\
252 instructions together.\n"));
253 }
254
255 int
256 md_parse_option (c, arg)
257 int c;
258 char *arg ATTRIBUTE_UNUSED;
259 {
260 switch (c)
261 {
262 case 'O':
263 /* Optimize. Will attempt to parallelize operations. */
264 Optimizing = 1;
265 break;
266 case OPTION_NOWARNSWAP:
267 flag_warn_suppress_instructionswap = 1;
268 break;
269 case OPTION_GSTABSPACKING:
270 flag_allow_gstabs_packing = 1;
271 break;
272 case OPTION_NOGSTABSPACKING:
273 flag_allow_gstabs_packing = 0;
274 break;
275 default:
276 return 0;
277 }
278 return 1;
279 }
280
281 symbolS *
282 md_undefined_symbol (name)
283 char *name ATTRIBUTE_UNUSED;
284 {
285 return 0;
286 }
287
288 /* Turn a string in input_line_pointer into a floating point constant
289 of type TYPE, and store the appropriate bytes in *LITP. The number
290 of LITTLENUMS emitted is stored in *SIZEP. An error message is
291 returned, or NULL on OK. */
292
293 char *
294 md_atof (type, litP, sizeP)
295 int type;
296 char *litP;
297 int *sizeP;
298 {
299 int prec;
300 LITTLENUM_TYPE words[4];
301 char *t;
302 int i;
303
304 switch (type)
305 {
306 case 'f':
307 prec = 2;
308 break;
309 case 'd':
310 prec = 4;
311 break;
312 default:
313 *sizeP = 0;
314 return _("bad call to md_atof");
315 }
316
317 t = atof_ieee (input_line_pointer, type, words);
318 if (t)
319 input_line_pointer = t;
320
321 *sizeP = prec * 2;
322
323 for (i = 0; i < prec; i++)
324 {
325 md_number_to_chars (litP, (valueT) words[i], 2);
326 litP += 2;
327 }
328 return NULL;
329 }
330
331 void
332 md_convert_frag (abfd, sec, fragP)
333 bfd *abfd ATTRIBUTE_UNUSED;
334 asection *sec ATTRIBUTE_UNUSED;
335 fragS *fragP ATTRIBUTE_UNUSED;
336 {
337 abort ();
338 }
339
340 valueT
341 md_section_align (seg, addr)
342 asection *seg;
343 valueT addr;
344 {
345 int align = bfd_get_section_alignment (stdoutput, seg);
346 return ((addr + (1 << align) - 1) & (-1 << align));
347 }
348
349 void
350 md_begin ()
351 {
352 char *prev_name = "";
353 struct d10v_opcode *opcode;
354 d10v_hash = hash_new ();
355
356 /* Insert unique names into hash table. The D10v instruction set
357 has many identical opcode names that have different opcodes based
358 on the operands. This hash table then provides a quick index to
359 the first opcode with a particular name in the opcode table. */
360
361 for (opcode = (struct d10v_opcode *) d10v_opcodes; opcode->name; opcode++)
362 {
363 if (strcmp (prev_name, opcode->name))
364 {
365 prev_name = (char *) opcode->name;
366 hash_insert (d10v_hash, opcode->name, (char *) opcode);
367 }
368 }
369
370 fixups = &FixUps[0];
371 FixUps[0].next = &FixUps[1];
372 FixUps[1].next = &FixUps[0];
373 }
374
375 /* Remove the postincrement or postdecrement operator ( '+' or '-' )
376 from an expression. */
377
378 static int
379 postfix (p)
380 char *p;
381 {
382 while (*p != '-' && *p != '+')
383 {
384 if (*p == 0 || *p == '\n' || *p == '\r')
385 break;
386 p++;
387 }
388
389 if (*p == '-')
390 {
391 *p = ' ';
392 return (-1);
393 }
394 if (*p == '+')
395 {
396 *p = ' ';
397 return (1);
398 }
399
400 return (0);
401 }
402
403 static bfd_reloc_code_real_type
404 get_reloc (op)
405 struct d10v_operand *op;
406 {
407 int bits = op->bits;
408
409 if (bits <= 4)
410 return (0);
411
412 if (op->flags & OPERAND_ADDR)
413 {
414 if (bits == 8)
415 return (BFD_RELOC_D10V_10_PCREL_R);
416 else
417 return (BFD_RELOC_D10V_18_PCREL);
418 }
419
420 return (BFD_RELOC_16);
421 }
422
423 /* Parse a string of operands. Return an array of expressions. */
424
425 static int
426 get_operands (exp)
427 expressionS exp[];
428 {
429 char *p = input_line_pointer;
430 int numops = 0;
431 int post = 0;
432 int uses_at = 0;
433
434 while (*p)
435 {
436 while (*p == ' ' || *p == '\t' || *p == ',')
437 p++;
438 if (*p == 0 || *p == '\n' || *p == '\r')
439 break;
440
441 if (*p == '@')
442 {
443 uses_at = 1;
444
445 p++;
446 exp[numops].X_op = O_absent;
447 if (*p == '(')
448 {
449 p++;
450 exp[numops].X_add_number = OPERAND_ATPAR;
451 }
452 else if (*p == '-')
453 {
454 p++;
455 exp[numops].X_add_number = OPERAND_ATMINUS;
456 }
457 else
458 {
459 exp[numops].X_add_number = OPERAND_ATSIGN;
460 if (*p == '+')
461 {
462 numops++;
463 exp[numops].X_op = O_absent;
464 exp[numops].X_add_number = OPERAND_PLUS;
465 p++;
466 }
467 post = postfix (p);
468 }
469 numops++;
470 continue;
471 }
472
473 if (*p == ')')
474 {
475 /* Just skip the trailing paren. */
476 p++;
477 continue;
478 }
479
480 input_line_pointer = p;
481
482 /* Check to see if it might be a register name. */
483 if (!register_name (&exp[numops]))
484 {
485 /* Parse as an expression. */
486 if (uses_at)
487 {
488 /* Any expression that involves the indirect addressing
489 cannot also involve immediate addressing. Therefore
490 the use of the hash character is illegal. */
491 int save = do_not_ignore_hash;
492 do_not_ignore_hash = 1;
493
494 expression (&exp[numops]);
495
496 do_not_ignore_hash = save;
497 }
498 else
499 expression (&exp[numops]);
500 }
501
502 if (strncasecmp (input_line_pointer, "@word", 5) == 0)
503 {
504 input_line_pointer += 5;
505 if (exp[numops].X_op == O_register)
506 {
507 /* If it looked like a register name but was followed by
508 "@word" then it was really a symbol, so change it to
509 one. */
510 exp[numops].X_op = O_symbol;
511 exp[numops].X_add_symbol =
512 symbol_find_or_make ((char *) exp[numops].X_op_symbol);
513 }
514
515 /* Check for identifier@word+constant. */
516 if (*input_line_pointer == '-' || *input_line_pointer == '+')
517 {
518 expressionS new_exp;
519 expression (&new_exp);
520 exp[numops].X_add_number = new_exp.X_add_number;
521 }
522
523 /* Convert expr into a right shift by AT_WORD_RIGHT_SHIFT. */
524 {
525 expressionS new_exp;
526 memset (&new_exp, 0, sizeof new_exp);
527 new_exp.X_add_number = AT_WORD_RIGHT_SHIFT;
528 new_exp.X_op = O_constant;
529 new_exp.X_unsigned = 1;
530 exp[numops].X_op_symbol = make_expr_symbol (&new_exp);
531 exp[numops].X_op = O_right_shift;
532 }
533
534 know (AT_WORD_P (&exp[numops]));
535 }
536
537 if (exp[numops].X_op == O_illegal)
538 as_bad (_("illegal operand"));
539 else if (exp[numops].X_op == O_absent)
540 as_bad (_("missing operand"));
541
542 numops++;
543 p = input_line_pointer;
544 }
545
546 switch (post)
547 {
548 case -1: /* Postdecrement mode. */
549 exp[numops].X_op = O_absent;
550 exp[numops++].X_add_number = OPERAND_MINUS;
551 break;
552 case 1: /* Postincrement mode. */
553 exp[numops].X_op = O_absent;
554 exp[numops++].X_add_number = OPERAND_PLUS;
555 break;
556 }
557
558 exp[numops].X_op = 0;
559 return (numops);
560 }
561
562 static unsigned long
563 d10v_insert_operand (insn, op_type, value, left, fix)
564 unsigned long insn;
565 int op_type;
566 offsetT value;
567 int left;
568 fixS *fix;
569 {
570 int shift, bits;
571
572 shift = d10v_operands[op_type].shift;
573 if (left)
574 shift += 15;
575
576 bits = d10v_operands[op_type].bits;
577
578 /* Truncate to the proper number of bits. */
579 if (check_range (value, bits, d10v_operands[op_type].flags))
580 as_bad_where (fix->fx_file, fix->fx_line,
581 _("operand out of range: %d"), value);
582
583 value &= 0x7FFFFFFF >> (31 - bits);
584 insn |= (value << shift);
585
586 return insn;
587 }
588
589 /* Take a pointer to the opcode entry in the opcode table and the
590 array of operand expressions. Return the instruction. */
591
592 static unsigned long
593 build_insn (opcode, opers, insn)
594 struct d10v_opcode *opcode;
595 expressionS *opers;
596 unsigned long insn;
597 {
598 int i, bits, shift, flags, format;
599 unsigned long number;
600
601 /* The insn argument is only used for the DIVS kludge. */
602 if (insn)
603 format = LONG_R;
604 else
605 {
606 insn = opcode->opcode;
607 format = opcode->format;
608 }
609
610 for (i = 0; opcode->operands[i]; i++)
611 {
612 flags = d10v_operands[opcode->operands[i]].flags;
613 bits = d10v_operands[opcode->operands[i]].bits;
614 shift = d10v_operands[opcode->operands[i]].shift;
615 number = opers[i].X_add_number;
616
617 if (flags & OPERAND_REG)
618 {
619 number &= REGISTER_MASK;
620 if (format == LONG_L)
621 shift += 15;
622 }
623
624 if (opers[i].X_op != O_register && opers[i].X_op != O_constant)
625 {
626 /* Now create a fixup. */
627
628 if (fixups->fc >= MAX_INSN_FIXUPS)
629 as_fatal (_("too many fixups"));
630
631 if (AT_WORD_P (&opers[i]))
632 {
633 /* Reconize XXX>>1+N aka XXX@word+N as special (AT_WORD). */
634 fixups->fix[fixups->fc].reloc = BFD_RELOC_D10V_18;
635 opers[i].X_op = O_symbol;
636 opers[i].X_op_symbol = NULL; /* Should free it. */
637 /* number is left shifted by AT_WORD_RIGHT_SHIFT so
638 that, it is aligned with the symbol's value. Later,
639 BFD_RELOC_D10V_18 will right shift (symbol_value +
640 X_add_number). */
641 number <<= AT_WORD_RIGHT_SHIFT;
642 opers[i].X_add_number = number;
643 }
644 else
645 fixups->fix[fixups->fc].reloc =
646 get_reloc ((struct d10v_operand *) &d10v_operands[opcode->operands[i]]);
647
648 if (fixups->fix[fixups->fc].reloc == BFD_RELOC_16 ||
649 fixups->fix[fixups->fc].reloc == BFD_RELOC_D10V_18)
650 fixups->fix[fixups->fc].size = 2;
651 else
652 fixups->fix[fixups->fc].size = 4;
653
654 fixups->fix[fixups->fc].exp = opers[i];
655 fixups->fix[fixups->fc].operand = opcode->operands[i];
656 fixups->fix[fixups->fc].pcrel =
657 (flags & OPERAND_ADDR) ? true : false;
658 (fixups->fc)++;
659 }
660
661 /* Truncate to the proper number of bits. */
662 if ((opers[i].X_op == O_constant) && check_range (number, bits, flags))
663 as_bad (_("operand out of range: %d"), number);
664 number &= 0x7FFFFFFF >> (31 - bits);
665 insn = insn | (number << shift);
666 }
667
668 /* kludge: for DIVS, we need to put the operands in twice */
669 /* on the second pass, format is changed to LONG_R to force
670 the second set of operands to not be shifted over 15. */
671 if ((opcode->opcode == OPCODE_DIVS) && (format == LONG_L))
672 insn = build_insn (opcode, opers, insn);
673
674 return insn;
675 }
676
677 /* Write out a long form instruction. */
678
679 static void
680 write_long (insn, fx)
681 unsigned long insn;
682 Fixups *fx;
683 {
684 int i, where;
685 char *f = frag_more (4);
686
687 insn |= FM11;
688 number_to_chars_bigendian (f, insn, 4);
689
690 for (i = 0; i < fx->fc; i++)
691 {
692 if (fx->fix[i].reloc)
693 {
694 where = f - frag_now->fr_literal;
695 if (fx->fix[i].size == 2)
696 where += 2;
697
698 if (fx->fix[i].reloc == BFD_RELOC_D10V_18)
699 fx->fix[i].operand |= 4096;
700
701 fix_new_exp (frag_now,
702 where,
703 fx->fix[i].size,
704 &(fx->fix[i].exp),
705 fx->fix[i].pcrel,
706 fx->fix[i].operand|2048);
707 }
708 }
709 fx->fc = 0;
710 }
711
712 /* Write out a short form instruction by itself. */
713
714 static void
715 write_1_short (opcode, insn, fx)
716 struct d10v_opcode *opcode;
717 unsigned long insn;
718 Fixups *fx;
719 {
720 char *f = frag_more (4);
721 int i, where;
722
723 if (opcode->exec_type & PARONLY)
724 as_fatal (_("Instruction must be executed in parallel with another instruction."));
725
726 /* The other container needs to be NOP. */
727 /* According to 4.3.1: for FM=00, sub-instructions performed only
728 by IU cannot be encoded in L-container. */
729 if (opcode->unit == IU)
730 insn |= FM00 | (NOP << 15); /* Right container. */
731 else
732 insn = FM00 | (insn << 15) | NOP; /* Left container. */
733
734 number_to_chars_bigendian (f, insn, 4);
735 for (i = 0; i < fx->fc; i++)
736 {
737 if (fx->fix[i].reloc)
738 {
739 where = f - frag_now->fr_literal;
740 if (fx->fix[i].size == 2)
741 where += 2;
742
743 if (fx->fix[i].reloc == BFD_RELOC_D10V_18)
744 fx->fix[i].operand |= 4096;
745
746 /* If it's an R reloc, we may have to switch it to L. */
747 if ((fx->fix[i].reloc == BFD_RELOC_D10V_10_PCREL_R)
748 && (opcode->unit != IU))
749 fx->fix[i].operand |= 1024;
750
751 fix_new_exp (frag_now,
752 where,
753 fx->fix[i].size,
754 &(fx->fix[i].exp),
755 fx->fix[i].pcrel,
756 fx->fix[i].operand|2048);
757 }
758 }
759 fx->fc = 0;
760 }
761
762 /* Expects two short instructions.
763 If possible, writes out both as a single packed instruction.
764 Otherwise, writes out the first one, packed with a NOP.
765 Returns number of instructions not written out. */
766
767 static int
768 write_2_short (opcode1, insn1, opcode2, insn2, exec_type, fx)
769 struct d10v_opcode *opcode1, *opcode2;
770 unsigned long insn1, insn2;
771 packing_type exec_type;
772 Fixups *fx;
773 {
774 unsigned long insn;
775 char *f;
776 int i, j, where;
777
778 if ((exec_type != PACK_PARALLEL)
779 && ((opcode1->exec_type & PARONLY) || (opcode2->exec_type & PARONLY)))
780 as_fatal (_("Instruction must be executed in parallel"));
781
782 if ((opcode1->format & LONG_OPCODE) || (opcode2->format & LONG_OPCODE))
783 as_fatal (_("Long instructions may not be combined."));
784
785 switch (exec_type)
786 {
787 case PACK_UNSPEC: /* Order not specified. */
788 if (opcode1->exec_type & ALONE)
789 {
790 /* Case of a short branch on a separate GAS line.
791 Pack with NOP. */
792 write_1_short (opcode1, insn1, fx->next);
793 return 1;
794 }
795 if (Optimizing
796 && parallel_ok (opcode1, insn1, opcode2, insn2, exec_type))
797 {
798 /* Parallel. */
799 if (opcode1->unit == IU)
800 insn = FM00 | (insn2 << 15) | insn1;
801 else if (opcode2->unit == MU)
802 insn = FM00 | (insn2 << 15) | insn1;
803 else
804 insn = FM00 | (insn1 << 15) | insn2;
805 }
806 else if (opcode1->unit == IU)
807 /* Reverse sequential with IU opcode1 on right and done first. */
808 insn = FM10 | (insn2 << 15) | insn1;
809 else
810 /* Sequential with non-IU opcode1 on left and done first. */
811 insn = FM01 | (insn1 << 15) | insn2;
812 break;
813
814 case PACK_PARALLEL:
815 if (opcode1->exec_type & SEQ || opcode2->exec_type & SEQ)
816 as_fatal
817 (_("One of these instructions may not be executed in parallel."));
818 if (opcode1->unit == IU)
819 {
820 if (opcode2->unit == IU)
821 as_fatal (_("Two IU instructions may not be executed in parallel"));
822 if (!flag_warn_suppress_instructionswap)
823 as_warn (_("Swapping instruction order"));
824 insn = FM00 | (insn2 << 15) | insn1;
825 }
826 else if (opcode2->unit == MU)
827 {
828 if (opcode1->unit == MU)
829 as_fatal (_("Two MU instructions may not be executed in parallel"));
830 if (!flag_warn_suppress_instructionswap)
831 as_warn (_("Swapping instruction order"));
832 insn = FM00 | (insn2 << 15) | insn1;
833 }
834 else
835 insn = FM00 | (insn1 << 15) | insn2;
836 break;
837
838 case PACK_LEFT_RIGHT:
839 if (opcode1->unit != IU)
840 insn = FM01 | (insn1 << 15) | insn2;
841 else if (opcode2->unit == MU || opcode2->unit == EITHER)
842 {
843 if (!flag_warn_suppress_instructionswap)
844 as_warn (_("Swapping instruction order"));
845 insn = FM10 | (insn2 << 15) | insn1;
846 }
847 else
848 as_fatal (_("IU instruction may not be in the left container"));
849 if (opcode1->exec_type & ALONE)
850 as_warn (_("Instruction in R container is squashed by flow control instruction in L container."));
851 break;
852
853 case PACK_RIGHT_LEFT:
854 if (opcode2->unit != MU)
855 insn = FM10 | (insn1 << 15) | insn2;
856 else if (opcode1->unit == IU || opcode1->unit == EITHER)
857 {
858 if (!flag_warn_suppress_instructionswap)
859 as_warn (_("Swapping instruction order"));
860 insn = FM01 | (insn2 << 15) | insn1;
861 }
862 else
863 as_fatal (_("MU instruction may not be in the right container"));
864 if (opcode2->exec_type & ALONE)
865 as_warn (_("Instruction in R container is squashed by flow control instruction in L container."));
866 break;
867
868 default:
869 as_fatal (_("unknown execution type passed to write_2_short()"));
870 }
871
872 f = frag_more (4);
873 number_to_chars_bigendian (f, insn, 4);
874
875 /* Process fixup chains. fx refers to insn2 when j == 0, and to
876 insn1 when j == 1. Yes, it's reversed. */
877
878 for (j = 0; j < 2; j++)
879 {
880 for (i = 0; i < fx->fc; i++)
881 {
882 if (fx->fix[i].reloc)
883 {
884 where = f - frag_now->fr_literal;
885 if (fx->fix[i].size == 2)
886 where += 2;
887
888 if (fx->fix[i].reloc == BFD_RELOC_D10V_10_PCREL_R
889 /* A BFD_RELOC_D10V_10_PCREL_R relocation applied to
890 the instruction in the L container has to be
891 adjusted to BDF_RELOC_D10V_10_PCREL_L. When
892 j==0, we're processing insn2's operands, so we
893 want to mark the operand if insn2 is *not* in the
894 R container. When j==1, we're processing insn1's
895 operands, so we want to mark the operand if insn2
896 *is* in the R container. Note that, if two
897 instructions are identical, we're never going to
898 swap them, so the test is safe. */
899 && j == ((insn & 0x7fff) == insn2))
900 fx->fix[i].operand |= 1024;
901
902 if (fx->fix[i].reloc == BFD_RELOC_D10V_18)
903 fx->fix[i].operand |= 4096;
904
905 fix_new_exp (frag_now,
906 where,
907 fx->fix[i].size,
908 &(fx->fix[i].exp),
909 fx->fix[i].pcrel,
910 fx->fix[i].operand|2048);
911 }
912 }
913 fx->fc = 0;
914 fx = fx->next;
915 }
916 return (0);
917 }
918
919 /* Check 2 instructions and determine if they can be safely
920 executed in parallel. Return 1 if they can be. */
921
922 static int
923 parallel_ok (op1, insn1, op2, insn2, exec_type)
924 struct d10v_opcode *op1, *op2;
925 unsigned long insn1, insn2;
926 packing_type exec_type;
927 {
928 int i, j, flags, mask, shift, regno;
929 unsigned long ins, mod[2], used[2];
930 struct d10v_opcode *op;
931
932 if ((op1->exec_type & SEQ) != 0 || (op2->exec_type & SEQ) != 0
933 || (op1->exec_type & PAR) == 0 || (op2->exec_type & PAR) == 0
934 || (op1->unit == BOTH) || (op2->unit == BOTH)
935 || (op1->unit == IU && op2->unit == IU)
936 || (op1->unit == MU && op2->unit == MU))
937 return 0;
938
939 /* If this is auto parallization, and either instruction is a branch,
940 don't parallel. */
941 if (exec_type == PACK_UNSPEC
942 && (op1->exec_type & ALONE || op2->exec_type & ALONE))
943 return 0;
944
945 /* The idea here is to create two sets of bitmasks (mod and used)
946 which indicate which registers are modified or used by each
947 instruction. The operation can only be done in parallel if
948 instruction 1 and instruction 2 modify different registers, and
949 the first instruction does not modify registers that the second
950 is using (The second instruction can modify registers that the
951 first is using as they are only written back after the first
952 instruction has completed). Accesses to control registers, PSW,
953 and memory are treated as accesses to a single register. So if
954 both instructions write memory or if the first instruction writes
955 memory and the second reads, then they cannot be done in
956 parallel. Likewise, if the first instruction mucks with the psw
957 and the second reads the PSW (which includes C, F0, and F1), then
958 they cannot operate safely in parallel. */
959
960 /* The bitmasks (mod and used) look like this (bit 31 = MSB). */
961 /* r0-r15 0-15 */
962 /* a0-a1 16-17 */
963 /* cr (not psw) 18 */
964 /* psw 19 */
965 /* mem 20 */
966
967 for (j = 0; j < 2; j++)
968 {
969 if (j == 0)
970 {
971 op = op1;
972 ins = insn1;
973 }
974 else
975 {
976 op = op2;
977 ins = insn2;
978 }
979 mod[j] = used[j] = 0;
980 if (op->exec_type & BRANCH_LINK)
981 mod[j] |= 1 << 13;
982
983 for (i = 0; op->operands[i]; i++)
984 {
985 flags = d10v_operands[op->operands[i]].flags;
986 shift = d10v_operands[op->operands[i]].shift;
987 mask = 0x7FFFFFFF >> (31 - d10v_operands[op->operands[i]].bits);
988 if (flags & OPERAND_REG)
989 {
990 regno = (ins >> shift) & mask;
991 if (flags & (OPERAND_ACC0 | OPERAND_ACC1))
992 regno += 16;
993 else if (flags & OPERAND_CONTROL) /* mvtc or mvfc. */
994 {
995 if (regno == 0)
996 regno = 19;
997 else
998 regno = 18;
999 }
1000 else if (flags & (OPERAND_FFLAG | OPERAND_CFLAG))
1001 regno = 19;
1002
1003 if (flags & OPERAND_DEST)
1004 {
1005 mod[j] |= 1 << regno;
1006 if (flags & OPERAND_EVEN)
1007 mod[j] |= 1 << (regno + 1);
1008 }
1009 else
1010 {
1011 used[j] |= 1 << regno;
1012 if (flags & OPERAND_EVEN)
1013 used[j] |= 1 << (regno + 1);
1014
1015 /* Auto inc/dec also modifies the register. */
1016 if (op->operands[i + 1] != 0
1017 && (d10v_operands[op->operands[i + 1]].flags
1018 & (OPERAND_PLUS | OPERAND_MINUS)) != 0)
1019 mod[j] |= 1 << regno;
1020 }
1021 }
1022 else if (flags & OPERAND_ATMINUS)
1023 {
1024 /* SP implicitly used/modified. */
1025 mod[j] |= 1 << 15;
1026 used[j] |= 1 << 15;
1027 }
1028 }
1029 if (op->exec_type & RMEM)
1030 used[j] |= 1 << 20;
1031 else if (op->exec_type & WMEM)
1032 mod[j] |= 1 << 20;
1033 else if (op->exec_type & RF0)
1034 used[j] |= 1 << 19;
1035 else if (op->exec_type & WF0)
1036 mod[j] |= 1 << 19;
1037 else if (op->exec_type & WCAR)
1038 mod[j] |= 1 << 19;
1039 }
1040 if ((mod[0] & mod[1]) == 0 && (mod[0] & used[1]) == 0)
1041 return 1;
1042 return 0;
1043 }
1044
1045 /* This is the main entry point for the machine-dependent assembler.
1046 STR points to a machine-dependent instruction. This function is
1047 supposed to emit the frags/bytes it assembles to. For the D10V, it
1048 mostly handles the special VLIW parsing and packing and leaves the
1049 difficult stuff to do_assemble(). */
1050
1051 static unsigned long prev_insn;
1052 static struct d10v_opcode *prev_opcode = 0;
1053 static subsegT prev_subseg;
1054 static segT prev_seg = 0;;
1055
1056 void
1057 md_assemble (str)
1058 char *str;
1059 {
1060 /* etype is saved extype. For multi-line instructions. */
1061
1062 packing_type extype = PACK_UNSPEC; /* Parallel, etc. */
1063
1064 struct d10v_opcode *opcode;
1065 unsigned long insn;
1066 char *str2;
1067
1068 if (etype == PACK_UNSPEC)
1069 {
1070 /* Look for the special multiple instruction separators. */
1071 str2 = strstr (str, "||");
1072 if (str2)
1073 extype = PACK_PARALLEL;
1074 else
1075 {
1076 str2 = strstr (str, "->");
1077 if (str2)
1078 extype = PACK_LEFT_RIGHT;
1079 else
1080 {
1081 str2 = strstr (str, "<-");
1082 if (str2)
1083 extype = PACK_RIGHT_LEFT;
1084 }
1085 }
1086 /* STR2 points to the separator, if there is one. */
1087 if (str2)
1088 {
1089 *str2 = 0;
1090
1091 /* If two instructions are present and we already have one saved,
1092 then first write out the saved one. */
1093 d10v_cleanup ();
1094
1095 /* Assemble first instruction and save it. */
1096 prev_insn = do_assemble (str, &prev_opcode);
1097 prev_seg = now_seg;
1098 prev_subseg = now_subseg;
1099 if (prev_insn == (unsigned long) -1)
1100 as_fatal (_("can't find opcode "));
1101 fixups = fixups->next;
1102 str = str2 + 2;
1103 }
1104 }
1105
1106 insn = do_assemble (str, &opcode);
1107 if (insn == (unsigned long) -1)
1108 {
1109 if (extype != PACK_UNSPEC)
1110 {
1111 etype = extype;
1112 return;
1113 }
1114 as_fatal (_("can't find opcode "));
1115 }
1116
1117 if (etype != PACK_UNSPEC)
1118 {
1119 extype = etype;
1120 etype = PACK_UNSPEC;
1121 }
1122
1123 /* If this is a long instruction, write it and any previous short
1124 instruction. */
1125 if (opcode->format & LONG_OPCODE)
1126 {
1127 if (extype != PACK_UNSPEC)
1128 as_fatal (_("Unable to mix instructions as specified"));
1129 d10v_cleanup ();
1130 write_long (insn, fixups);
1131 prev_opcode = NULL;
1132 return;
1133 }
1134
1135 if (prev_opcode
1136 && prev_seg
1137 && ((prev_seg != now_seg) || (prev_subseg != now_subseg)))
1138 d10v_cleanup ();
1139
1140 if (prev_opcode
1141 && (write_2_short (prev_opcode, prev_insn, opcode, insn, extype, fixups) == 0))
1142 {
1143 /* No instructions saved. */
1144 prev_opcode = NULL;
1145 }
1146 else
1147 {
1148 if (extype != PACK_UNSPEC)
1149 as_fatal (_("Unable to mix instructions as specified"));
1150 /* Save last instruction so it may be packed on next pass. */
1151 prev_opcode = opcode;
1152 prev_insn = insn;
1153 prev_seg = now_seg;
1154 prev_subseg = now_subseg;
1155 fixups = fixups->next;
1156 }
1157 }
1158
1159 /* Assemble a single instruction.
1160 Return an opcode, or -1 (an invalid opcode) on error. */
1161
1162 static unsigned long
1163 do_assemble (str, opcode)
1164 char *str;
1165 struct d10v_opcode **opcode;
1166 {
1167 unsigned char *op_start, *save;
1168 unsigned char *op_end;
1169 char name[20];
1170 int nlen = 0;
1171 expressionS myops[6];
1172 unsigned long insn;
1173
1174 /* Drop leading whitespace. */
1175 while (*str == ' ')
1176 str++;
1177
1178 /* Find the opcode end. */
1179 for (op_start = op_end = (unsigned char *) (str);
1180 *op_end
1181 && nlen < 20
1182 && !is_end_of_line[*op_end] && *op_end != ' ';
1183 op_end++)
1184 {
1185 name[nlen] = TOLOWER (op_start[nlen]);
1186 nlen++;
1187 }
1188 name[nlen] = 0;
1189
1190 if (nlen == 0)
1191 return -1;
1192
1193 /* Find the first opcode with the proper name. */
1194 *opcode = (struct d10v_opcode *) hash_find (d10v_hash, name);
1195 if (*opcode == NULL)
1196 as_fatal (_("unknown opcode: %s"), name);
1197
1198 save = input_line_pointer;
1199 input_line_pointer = op_end;
1200 *opcode = find_opcode (*opcode, myops);
1201 if (*opcode == 0)
1202 return -1;
1203 input_line_pointer = save;
1204
1205 insn = build_insn ((*opcode), myops, 0);
1206 return (insn);
1207 }
1208
1209 /* Find the symbol which has the same name as the register in EXP. */
1210
1211 static symbolS *
1212 find_symbol_matching_register (exp)
1213 expressionS *exp;
1214 {
1215 int i;
1216
1217 if (exp->X_op != O_register)
1218 return NULL;
1219
1220 /* Find the name of the register. */
1221 for (i = d10v_reg_name_cnt (); i--;)
1222 if (d10v_predefined_registers[i].value == exp->X_add_number)
1223 break;
1224
1225 if (i < 0)
1226 abort ();
1227
1228 /* Now see if a symbol has been defined with the same name. */
1229 return symbol_find (d10v_predefined_registers[i].name);
1230 }
1231
1232 /* Get a pointer to an entry in the opcode table.
1233 The function must look at all opcodes with the same name and use
1234 the operands to choose the correct opcode. */
1235
1236 static struct d10v_opcode *
1237 find_opcode (opcode, myops)
1238 struct d10v_opcode *opcode;
1239 expressionS myops[];
1240 {
1241 int i, match;
1242 struct d10v_opcode *next_opcode;
1243
1244 /* Get all the operands and save them as expressions. */
1245 get_operands (myops);
1246
1247 /* Now see if the operand is a fake. If so, find the correct size
1248 instruction, if possible. */
1249 if (opcode->format == OPCODE_FAKE)
1250 {
1251 int opnum = opcode->operands[0];
1252 int flags;
1253
1254 if (myops[opnum].X_op == O_register)
1255 {
1256 myops[opnum].X_op = O_symbol;
1257 myops[opnum].X_add_symbol =
1258 symbol_find_or_make ((char *) myops[opnum].X_op_symbol);
1259 myops[opnum].X_add_number = 0;
1260 myops[opnum].X_op_symbol = NULL;
1261 }
1262
1263 next_opcode = opcode + 1;
1264
1265 /* If the first operand is supposed to be a register, make sure
1266 we got a valid one. */
1267 flags = d10v_operands[next_opcode->operands[0]].flags;
1268 if (flags & OPERAND_REG)
1269 {
1270 int X_op = myops[0].X_op;
1271 int num = myops[0].X_add_number;
1272
1273 if (X_op != O_register
1274 || (num & ~flags
1275 & (OPERAND_GPR | OPERAND_ACC0 | OPERAND_ACC1
1276 | OPERAND_FFLAG | OPERAND_CFLAG | OPERAND_CONTROL))
1277 || ((flags & OPERAND_SP) && ! (num & OPERAND_SP)))
1278 {
1279 as_bad (_("bad opcode or operands"));
1280 return 0;
1281 }
1282 }
1283
1284 if (myops[opnum].X_op == O_constant
1285 || (myops[opnum].X_op == O_symbol
1286 && S_IS_DEFINED (myops[opnum].X_add_symbol)
1287 && (S_GET_SEGMENT (myops[opnum].X_add_symbol) == now_seg)))
1288 {
1289 for (i = 0; opcode->operands[i + 1]; i++)
1290 {
1291 int bits = d10v_operands[next_opcode->operands[opnum]].bits;
1292 int flags = d10v_operands[next_opcode->operands[opnum]].flags;
1293 if (flags & OPERAND_ADDR)
1294 bits += 2;
1295
1296 if (myops[opnum].X_op == O_constant)
1297 {
1298 if (!check_range (myops[opnum].X_add_number, bits, flags))
1299 return next_opcode;
1300 }
1301 else
1302 {
1303 fragS *sym_frag;
1304 fragS *f;
1305 unsigned long current_position;
1306 unsigned long symbol_position;
1307 unsigned long value;
1308 boolean found_symbol;
1309
1310 /* Calculate the address of the current instruction
1311 and the address of the symbol. Do this by summing
1312 the offsets of previous frags until we reach the
1313 frag containing the symbol, and the current frag. */
1314 sym_frag = symbol_get_frag (myops[opnum].X_add_symbol);
1315 found_symbol = false;
1316
1317 current_position =
1318 obstack_next_free (&frchain_now->frch_obstack)
1319 - frag_now->fr_literal;
1320 symbol_position = S_GET_VALUE (myops[opnum].X_add_symbol);
1321
1322 for (f = frchain_now->frch_root; f; f = f->fr_next)
1323 {
1324 current_position += f->fr_fix + f->fr_offset;
1325
1326 if (f == sym_frag)
1327 found_symbol = true;
1328
1329 if (! found_symbol)
1330 symbol_position += f->fr_fix + f->fr_offset;
1331 }
1332
1333 value = symbol_position;
1334
1335 if (flags & OPERAND_ADDR)
1336 value -= current_position;
1337
1338 if (AT_WORD_P (&myops[opnum]))
1339 {
1340 if (bits > 4)
1341 {
1342 bits += 2;
1343 if (!check_range (value, bits, flags))
1344 return next_opcode;
1345 }
1346 }
1347 else if (!check_range (value, bits, flags))
1348 return next_opcode;
1349 }
1350 next_opcode++;
1351 }
1352 as_fatal (_("value out of range"));
1353 }
1354 else
1355 {
1356 /* Not a constant, so use a long instruction. */
1357 return opcode + 2;
1358 }
1359 }
1360 else
1361 {
1362 match = 0;
1363 /* Now search the opcode table table for one with operands
1364 that matches what we've got. */
1365 while (!match)
1366 {
1367 match = 1;
1368 for (i = 0; opcode->operands[i]; i++)
1369 {
1370 int flags = d10v_operands[opcode->operands[i]].flags;
1371 int X_op = myops[i].X_op;
1372 int num = myops[i].X_add_number;
1373
1374 if (X_op == 0)
1375 {
1376 match = 0;
1377 break;
1378 }
1379
1380 if (flags & OPERAND_REG)
1381 {
1382 if ((X_op != O_register)
1383 || (num & ~flags
1384 & (OPERAND_GPR | OPERAND_ACC0 | OPERAND_ACC1
1385 | OPERAND_FFLAG | OPERAND_CFLAG
1386 | OPERAND_CONTROL))
1387 || ((flags & OPERAND_SP) && ! (num & OPERAND_SP)))
1388 {
1389 match = 0;
1390 break;
1391 }
1392 }
1393
1394 if (((flags & OPERAND_MINUS) && ((X_op != O_absent) || (num != OPERAND_MINUS))) ||
1395 ((flags & OPERAND_PLUS) && ((X_op != O_absent) || (num != OPERAND_PLUS))) ||
1396 ((flags & OPERAND_ATMINUS) && ((X_op != O_absent) || (num != OPERAND_ATMINUS))) ||
1397 ((flags & OPERAND_ATPAR) && ((X_op != O_absent) || (num != OPERAND_ATPAR))) ||
1398 ((flags & OPERAND_ATSIGN) && ((X_op != O_absent) || ((num != OPERAND_ATSIGN) && (num != OPERAND_ATPAR)))))
1399 {
1400 match = 0;
1401 break;
1402 }
1403
1404 /* Unfortunatly, for the indirect operand in
1405 instructions such as ``ldb r1, @(c,r14)'' this
1406 function can be passed X_op == O_register (because
1407 'c' is a valid register name). However we cannot
1408 just ignore the case when X_op == O_register but
1409 flags & OPERAND_REG is null, so we check to see if a
1410 symbol of the same name as the register exists. If
1411 the symbol does exist, then the parser was unable to
1412 distinguish the two cases and we fix things here.
1413 (Ref: PR14826) */
1414
1415 if (!(flags & OPERAND_REG) && (X_op == O_register))
1416 {
1417 symbolS *sym = find_symbol_matching_register (&myops[i]);
1418
1419 if (sym != NULL)
1420 {
1421 myops[i].X_op = X_op = O_symbol;
1422 myops[i].X_add_symbol = sym;
1423 }
1424 else
1425 as_bad
1426 (_("illegal operand - register name found where none expected"));
1427 }
1428 }
1429
1430 /* We're only done if the operands matched so far AND there
1431 are no more to check. */
1432 if (match && myops[i].X_op == 0)
1433 break;
1434 else
1435 match = 0;
1436
1437 next_opcode = opcode + 1;
1438
1439 if (next_opcode->opcode == 0)
1440 break;
1441
1442 if (strcmp (next_opcode->name, opcode->name))
1443 break;
1444
1445 opcode = next_opcode;
1446 }
1447 }
1448
1449 if (!match)
1450 {
1451 as_bad (_("bad opcode or operands"));
1452 return (0);
1453 }
1454
1455 /* Check that all registers that are required to be even are.
1456 Also, if any operands were marked as registers, but were really symbols,
1457 fix that here. */
1458 for (i = 0; opcode->operands[i]; i++)
1459 {
1460 if ((d10v_operands[opcode->operands[i]].flags & OPERAND_EVEN) &&
1461 (myops[i].X_add_number & 1))
1462 as_fatal (_("Register number must be EVEN"));
1463 if ((d10v_operands[opcode->operands[i]].flags & OPERAND_NOSP)
1464 && (myops[i].X_add_number & OPERAND_SP))
1465 as_bad (_("Unsupported use of sp"));
1466 if (myops[i].X_op == O_register)
1467 {
1468 if (!(d10v_operands[opcode->operands[i]].flags & OPERAND_REG))
1469 {
1470 myops[i].X_op = O_symbol;
1471 myops[i].X_add_symbol =
1472 symbol_find_or_make ((char *) myops[i].X_op_symbol);
1473 myops[i].X_add_number = 0;
1474 myops[i].X_op_symbol = NULL;
1475 }
1476 }
1477 }
1478 return opcode;
1479 }
1480
1481 /* If while processing a fixup, a reloc really needs to be created.
1482 Then it is done here. */
1483
1484 arelent *
1485 tc_gen_reloc (seg, fixp)
1486 asection *seg ATTRIBUTE_UNUSED;
1487 fixS *fixp;
1488 {
1489 arelent *reloc;
1490 reloc = (arelent *) xmalloc (sizeof (arelent));
1491 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
1492 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1493 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1494 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1495 if (reloc->howto == (reloc_howto_type *) NULL)
1496 {
1497 as_bad_where (fixp->fx_file, fixp->fx_line,
1498 _("reloc %d not supported by object file format"),
1499 (int) fixp->fx_r_type);
1500 return NULL;
1501 }
1502
1503 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1504 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1505 reloc->address = fixp->fx_offset;
1506
1507 reloc->addend = fixp->fx_addnumber;
1508
1509 return reloc;
1510 }
1511
1512 int
1513 md_estimate_size_before_relax (fragp, seg)
1514 fragS *fragp ATTRIBUTE_UNUSED;
1515 asection *seg ATTRIBUTE_UNUSED;
1516 {
1517 abort ();
1518 return 0;
1519 }
1520
1521 long
1522 md_pcrel_from_section (fixp, sec)
1523 fixS *fixp;
1524 segT sec;
1525 {
1526 if (fixp->fx_addsy != (symbolS *) NULL
1527 && (!S_IS_DEFINED (fixp->fx_addsy)
1528 || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
1529 return 0;
1530 return fixp->fx_frag->fr_address + fixp->fx_where;
1531 }
1532
1533 void
1534 md_apply_fix3 (fixP, valP, seg)
1535 fixS *fixP;
1536 valueT * valP;
1537 segT seg ATTRIBUTE_UNUSED;
1538 {
1539 char *where;
1540 unsigned long insn;
1541 long value = * (long *) valP;
1542 int op_type;
1543 int left = 0;
1544
1545 if (fixP->fx_addsy == (symbolS *) NULL)
1546 fixP->fx_done = 1;
1547
1548 else if (fixP->fx_pcrel)
1549 ;
1550
1551 else
1552 {
1553 value = fixP->fx_offset;
1554
1555 if (fixP->fx_subsy != (symbolS *) NULL)
1556 {
1557 if (S_GET_SEGMENT (fixP->fx_subsy) == absolute_section)
1558 value -= S_GET_VALUE (fixP->fx_subsy);
1559 else
1560 {
1561 /* We don't actually support subtracting a symbol. */
1562 as_bad_where (fixP->fx_file, fixP->fx_line,
1563 _("expression too complex"));
1564 }
1565 }
1566 }
1567
1568 op_type = fixP->fx_r_type;
1569 if (op_type & 2048)
1570 {
1571 op_type -= 2048;
1572 if (op_type & 1024)
1573 {
1574 op_type -= 1024;
1575 fixP->fx_r_type = BFD_RELOC_D10V_10_PCREL_L;
1576 left = 1;
1577 }
1578 else if (op_type & 4096)
1579 {
1580 op_type -= 4096;
1581 fixP->fx_r_type = BFD_RELOC_D10V_18;
1582 }
1583 else
1584 fixP->fx_r_type =
1585 get_reloc ((struct d10v_operand *) &d10v_operands[op_type]);
1586 }
1587
1588 /* Fetch the instruction, insert the fully resolved operand
1589 value, and stuff the instruction back again. */
1590 where = fixP->fx_frag->fr_literal + fixP->fx_where;
1591 insn = bfd_getb32 ((unsigned char *) where);
1592
1593 switch (fixP->fx_r_type)
1594 {
1595 case BFD_RELOC_D10V_10_PCREL_L:
1596 case BFD_RELOC_D10V_10_PCREL_R:
1597 case BFD_RELOC_D10V_18_PCREL:
1598 case BFD_RELOC_D10V_18:
1599 /* Instruction addresses are always right-shifted by 2. */
1600 value >>= AT_WORD_RIGHT_SHIFT;
1601 if (fixP->fx_size == 2)
1602 bfd_putb16 ((bfd_vma) value, (unsigned char *) where);
1603 else
1604 {
1605 struct d10v_opcode *rep, *repi;
1606
1607 rep = (struct d10v_opcode *) hash_find (d10v_hash, "rep");
1608 repi = (struct d10v_opcode *) hash_find (d10v_hash, "repi");
1609 if ((insn & FM11) == FM11
1610 && ( (repi != NULL && (insn & repi->mask) == (unsigned) repi->opcode)
1611 || (rep != NULL && (insn & rep->mask) == (unsigned) rep->opcode))
1612 && value < 4)
1613 as_fatal
1614 (_("line %d: rep or repi must include at least 4 instructions"),
1615 fixP->fx_line);
1616 insn =
1617 d10v_insert_operand (insn, op_type, (offsetT) value, left, fixP);
1618 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1619 }
1620 break;
1621 case BFD_RELOC_32:
1622 bfd_putb32 ((bfd_vma) value, (unsigned char *) where);
1623 break;
1624 case BFD_RELOC_16:
1625 bfd_putb16 ((bfd_vma) value, (unsigned char *) where);
1626 break;
1627
1628 case BFD_RELOC_VTABLE_INHERIT:
1629 case BFD_RELOC_VTABLE_ENTRY:
1630 fixP->fx_done = 0;
1631 return;
1632
1633 default:
1634 as_fatal (_("line %d: unknown relocation type: 0x%x"),
1635 fixP->fx_line, fixP->fx_r_type);
1636 }
1637 }
1638
1639 /* d10v_cleanup() is called after the assembler has finished parsing
1640 the input file, when a label is read from the input file, or when a
1641 stab directive is output. Because the D10V assembler sometimes
1642 saves short instructions to see if it can package them with the
1643 next instruction, there may be a short instruction that still needs
1644 to be written.
1645
1646 NOTE: accesses a global, etype.
1647 NOTE: invoked by various macros such as md_cleanup: see. */
1648
1649 int
1650 d10v_cleanup ()
1651 {
1652 segT seg;
1653 subsegT subseg;
1654
1655 /* If cleanup was invoked because the assembler encountered, e.g., a
1656 user label, we write out the pending instruction, if any. If it
1657 was invoked because the assembler is outputting a piece of line
1658 debugging information, though, we write out the pending
1659 instruction only if the --no-gstabs-packing command line switch
1660 has been specified. */
1661 if (prev_opcode
1662 && etype == PACK_UNSPEC
1663 && (! outputting_stabs_line_debug || ! flag_allow_gstabs_packing))
1664 {
1665 seg = now_seg;
1666 subseg = now_subseg;
1667
1668 if (prev_seg)
1669 subseg_set (prev_seg, prev_subseg);
1670
1671 write_1_short (prev_opcode, prev_insn, fixups->next);
1672
1673 subseg_set (seg, subseg);
1674 prev_opcode = NULL;
1675 }
1676 return 1;
1677 }
1678
1679 /* Like normal .word, except support @word. */
1680 /* Clobbers input_line_pointer, checks end-of-line. */
1681
1682 static void
1683 d10v_dot_word (dummy)
1684 int dummy ATTRIBUTE_UNUSED;
1685 {
1686 expressionS exp;
1687 char *p;
1688
1689 if (is_it_end_of_statement ())
1690 {
1691 demand_empty_rest_of_line ();
1692 return;
1693 }
1694
1695 do
1696 {
1697 expression (&exp);
1698 if (!strncasecmp (input_line_pointer, "@word", 5))
1699 {
1700 exp.X_add_number = 0;
1701 input_line_pointer += 5;
1702
1703 p = frag_more (2);
1704 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
1705 &exp, 0, BFD_RELOC_D10V_18);
1706 }
1707 else
1708 emit_expr (&exp, 2);
1709 }
1710 while (*input_line_pointer++ == ',');
1711
1712 input_line_pointer--; /* Put terminator back into stream. */
1713 demand_empty_rest_of_line ();
1714 }
1715
1716 /* Mitsubishi asked that we support some old syntax that apparently
1717 had immediate operands starting with '#'. This is in some of their
1718 sample code but is not documented (although it appears in some
1719 examples in their assembler manual). For now, we'll solve this
1720 compatibility problem by simply ignoring any '#' at the beginning
1721 of an operand. */
1722
1723 /* Operands that begin with '#' should fall through to here. */
1724 /* From expr.c. */
1725
1726 void
1727 md_operand (expressionP)
1728 expressionS *expressionP;
1729 {
1730 if (*input_line_pointer == '#' && ! do_not_ignore_hash)
1731 {
1732 input_line_pointer++;
1733 expression (expressionP);
1734 }
1735 }
1736
1737 boolean
1738 d10v_fix_adjustable (fixP)
1739 fixS *fixP;
1740 {
1741 if (fixP->fx_addsy == NULL)
1742 return 1;
1743
1744 /* Prevent all adjustments to global symbols. */
1745 if (S_IS_EXTERN (fixP->fx_addsy))
1746 return 0;
1747 if (S_IS_WEAK (fixP->fx_addsy))
1748 return 0;
1749
1750 /* We need the symbol name for the VTABLE entries. */
1751 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1752 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1753 return 0;
1754
1755 return 1;
1756 }
1757
1758 int
1759 d10v_force_relocation (fixp)
1760 fixS *fixp;
1761 {
1762 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1763 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1764 return 1;
1765
1766 return 0;
1767 }