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