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