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