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