Add Dmitry Diky to maintainers list as msp430 maintainer
[deliverable/binutils-gdb.git] / gas / config / tc-d30v.c
CommitLineData
252b5132 1/* tc-d30v.c -- Assembler code for the Mitsubishi D30V
aef6203b 2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005
2132e3a3 3 Free Software Foundation, Inc.
252b5132
RH
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include <stdio.h>
252b5132 23#include "as.h"
3882b010 24#include "safe-ctype.h"
2fd5405a 25#include "subsegs.h"
252b5132
RH
26#include "opcode/d30v.h"
27
28const char comment_chars[] = ";";
29const char line_comment_chars[] = "#";
30const char line_separator_chars[] = "";
31const char *md_shortopts = "OnNcC";
32const char EXP_CHARS[] = "eE";
33const char FLT_CHARS[] = "dD";
34
ba09cd8d
DN
35#if HAVE_LIMITS_H
36#include <limits.h>
37#endif
38
39#ifndef CHAR_BIT
40#define CHAR_BIT 8
41#endif
42
252b5132
RH
43#define NOP_MULTIPLY 1
44#define NOP_ALL 2
45static int warn_nops = 0;
46static int Optimizing = 0;
47static int warn_register_name_conflicts = 1;
48
49#define FORCE_SHORT 1
50#define FORCE_LONG 2
51
52/* EXEC types. */
53typedef enum _exec_type
54{
55 EXEC_UNKNOWN, /* no order specified */
56 EXEC_PARALLEL, /* done in parallel (FM=00) */
57 EXEC_SEQ, /* sequential (FM=01) */
58 EXEC_REVSEQ /* reverse sequential (FM=10) */
59} exec_type_enum;
60
2fd5405a 61/* Fixups. */
252b5132
RH
62#define MAX_INSN_FIXUPS (5)
63struct d30v_fixup
64{
65 expressionS exp;
66 int operand;
67 int pcrel;
68 int size;
69 bfd_reloc_code_real_type reloc;
70};
71
72typedef struct _fixups
73{
74 int fc;
75 struct d30v_fixup fix[MAX_INSN_FIXUPS];
76 struct _fixups *next;
77} Fixups;
78
79static Fixups FixUps[2];
80static Fixups *fixups;
81
82/* Whether current and previous instruction are word multiply insns. */
83static int cur_mul32_p = 0;
84static int prev_mul32_p = 0;
85
86/* The flag_explicitly_parallel is true iff the instruction being assembled
87 has been explicitly written as a parallel short-instruction pair by the
2fd5405a 88 human programmer. It is used in parallel_ok () to distinguish between
252b5132
RH
89 those dangerous parallelizations attempted by the human, which are to be
90 allowed, and those attempted by the assembler, which are not. It is set
2fd5405a
NC
91 from md_assemble (). */
92static int flag_explicitly_parallel = 0;
252b5132
RH
93static int flag_xp_state = 0;
94
95/* Whether current and previous left sub-instruction disables
96 execution of right sub-instruction. */
97static int cur_left_kills_right_p = 0;
98static int prev_left_kills_right_p = 0;
99
100/* The known current alignment of the current section. */
101static int d30v_current_align;
102static segT d30v_current_align_seg;
103
104/* The last seen label in the current section. This is used to auto-align
2d2255b5 105 labels preceding instructions. */
252b5132
RH
106static symbolS *d30v_last_label;
107
2fd5405a 108/* Two nops. */
252b5132
RH
109#define NOP_LEFT ((long long) NOP << 32)
110#define NOP_RIGHT ((long long) NOP)
111#define NOP2 (FM00 | NOP_LEFT | NOP_RIGHT)
112
2fd5405a 113/* Local functions. */
252b5132
RH
114static int reg_name_search PARAMS ((char *name));
115static int register_name PARAMS ((expressionS *expressionP));
116static int check_range PARAMS ((unsigned long num, int bits, int flags));
117static int postfix PARAMS ((char *p));
118static bfd_reloc_code_real_type get_reloc PARAMS ((struct d30v_operand *op, int rel_flag));
119static int get_operands PARAMS ((expressionS exp[], int cmp_hack));
2fd5405a 120static struct d30v_format *find_format PARAMS ((struct d30v_opcode *opcode,
252b5132
RH
121 expressionS ops[],int fsize, int cmp_hack));
122static long long build_insn PARAMS ((struct d30v_insn *opcode, expressionS *opers));
123static void write_long PARAMS ((struct d30v_insn *opcode, long long insn, Fixups *fx));
124static void write_1_short PARAMS ((struct d30v_insn *opcode, long long insn,
125 Fixups *fx, int use_sequential));
2fd5405a 126static int write_2_short PARAMS ((struct d30v_insn *opcode1, long long insn1,
252b5132
RH
127 struct d30v_insn *opcode2, long long insn2, exec_type_enum exec_type, Fixups *fx));
128static long long do_assemble PARAMS ((char *str, struct d30v_insn *opcode,
129 int shortp, int is_parallel));
2fd5405a 130static int parallel_ok PARAMS ((struct d30v_insn *opcode1, unsigned long insn1,
252b5132
RH
131 struct d30v_insn *opcode2, unsigned long insn2,
132 exec_type_enum exec_type));
133static void d30v_number_to_chars PARAMS ((char *buf, long long value, int nbytes));
134static void check_size PARAMS ((long value, int bits, char *file, int line));
135static void d30v_align PARAMS ((int, char *, symbolS *));
136static void s_d30v_align PARAMS ((int));
137static void s_d30v_text PARAMS ((int));
138static void s_d30v_data PARAMS ((int));
139static void s_d30v_section PARAMS ((int));
140
2fd5405a
NC
141struct option md_longopts[] =
142{
252b5132
RH
143 {NULL, no_argument, NULL, 0}
144};
252b5132 145
2fd5405a 146size_t md_longopts_size = sizeof (md_longopts);
252b5132
RH
147
148/* The target specific pseudo-ops which we support. */
149const pseudo_typeS md_pseudo_table[] =
150{
151 { "word", cons, 4 },
152 { "hword", cons, 2 },
153 { "align", s_d30v_align, 0 },
154 { "text", s_d30v_text, 0 },
155 { "data", s_d30v_data, 0 },
156 { "section", s_d30v_section, 0 },
157 { "section.s", s_d30v_section, 0 },
158 { "sect", s_d30v_section, 0 },
159 { "sect.s", s_d30v_section, 0 },
160 { NULL, NULL, 0 }
161};
162
163/* Opcode hash table. */
164static struct hash_control *d30v_hash;
165
2fd5405a
NC
166/* Do a binary search of the pre_defined_registers array to see if
167 NAME is a valid regiter name. Return the register number from the
168 array on success, or -1 on failure. */
252b5132
RH
169
170static int
171reg_name_search (name)
172 char *name;
173{
174 int middle, low, high;
175 int cmp;
176
177 low = 0;
178 high = reg_name_cnt () - 1;
179
180 do
181 {
182 middle = (low + high) / 2;
183 cmp = strcasecmp (name, pre_defined_registers[middle].name);
184 if (cmp < 0)
185 high = middle - 1;
186 else if (cmp > 0)
187 low = middle + 1;
188 else
189 {
190 if (symbol_find (name) != NULL)
191 {
192 if (warn_register_name_conflicts)
193 as_warn (_("Register name %s conflicts with symbol of the same name"),
194 name);
195 }
2fd5405a 196
252b5132
RH
197 return pre_defined_registers[middle].value;
198 }
199 }
200 while (low <= high);
2fd5405a 201
252b5132
RH
202 return -1;
203}
204
2fd5405a
NC
205/* Check the string at input_line_pointer to see if it is a valid
206 register name. */
252b5132
RH
207
208static int
209register_name (expressionP)
210 expressionS *expressionP;
211{
212 int reg_number;
213 char c, *p = input_line_pointer;
2fd5405a
NC
214
215 while (*p && *p != '\n' && *p != '\r' && *p != ',' && *p != ' ' && *p != ')')
252b5132
RH
216 p++;
217
218 c = *p;
219 if (c)
220 *p++ = 0;
221
2fd5405a 222 /* Look to see if it's in the register table. */
252b5132 223 reg_number = reg_name_search (input_line_pointer);
2fd5405a 224 if (reg_number >= 0)
252b5132
RH
225 {
226 expressionP->X_op = O_register;
2fd5405a
NC
227 /* Temporarily store a pointer to the string here. */
228 expressionP->X_op_symbol = (symbolS *) input_line_pointer;
252b5132
RH
229 expressionP->X_add_number = reg_number;
230 input_line_pointer = p;
231 return 1;
232 }
233 if (c)
2fd5405a 234 *(p - 1) = c;
252b5132
RH
235 return 0;
236}
237
252b5132
RH
238static int
239check_range (num, bits, flags)
240 unsigned long num;
241 int bits;
242 int flags;
243{
244 long min, max;
252b5132 245
bf80011a
RH
246 /* Don't bother checking 32-bit values. */
247 if (bits == 32)
248 {
2fd5405a 249 if (sizeof (unsigned long) * CHAR_BIT == 32)
cc8a6dd0 250 return 0;
bf80011a
RH
251
252 /* We don't record signed or unsigned for 32-bit quantities.
253 Allow either. */
2fd5405a
NC
254 min = -((unsigned long) 1 << (bits - 1));
255 max = ((unsigned long) 1 << bits) - 1;
30cdfbed 256 return (long) num < min || (long) num > max;
bf80011a 257 }
252b5132
RH
258
259 if (flags & OPERAND_SHIFT)
260 {
2fd5405a 261 /* We know that all shifts are right by three bits. */
30cdfbed 262 num >>= 3;
2fd5405a 263
252b5132 264 if (flags & OPERAND_SIGNED)
30cdfbed
AM
265 {
266 unsigned long sign_bit = ((unsigned long) -1L >> 4) + 1;
267 num = (num ^ sign_bit) - sign_bit;
268 }
252b5132
RH
269 }
270
271 if (flags & OPERAND_SIGNED)
272 {
2fd5405a
NC
273 max = ((unsigned long) 1 << (bits - 1)) - 1;
274 min = - ((unsigned long) 1 << (bits - 1));
30cdfbed 275 return (long) num > max || (long) num < min;
252b5132
RH
276 }
277 else
278 {
2fd5405a 279 max = ((unsigned long) 1 << bits) - 1;
30cdfbed 280 return num > (unsigned long) max;
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;
ebf19f1b 299 char *arg ATTRIBUTE_UNUSED;
252b5132
RH
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)
ebf19f1b 335 char *name ATTRIBUTE_UNUSED;
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)
ebf19f1b
AM
385 bfd *abfd ATTRIBUTE_UNUSED;
386 asection *sec ATTRIBUTE_UNUSED;
387 fragS *fragP ATTRIBUTE_UNUSED;
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))
ebf19f1b 656 as_bad (_("operand out of range: %ld"), 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)
ebf19f1b 680 struct d30v_insn *opcode ATTRIBUTE_UNUSED;
252b5132
RH
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. */
b34976b6 796 write_1_short (opcode1, insn1, fx->next, FALSE);
252b5132
RH
797 return 1;
798 }
2fd5405a 799
2d2255b5 800 /* Note: we do not have to worry about subroutine calls occurring
252b5132
RH
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. */
b34976b6 836 write_1_short (opcode1, insn1, fx->next, FALSE);
252b5132
RH
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. */
b34976b6 843 write_1_short (opcode1, insn1, fx->next, FALSE);
252b5132
RH
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. */
b34976b6 853 write_1_short (opcode1, insn1, fx->next, FALSE);
6b2539e3
NC
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
252b5132
RH
930 f = frag_more (8);
931 d30v_number_to_chars (f, insn, 8);
932
933 /* If the previous instruction was a 32-bit multiply but it is put into a
934 parallel container, mark the current instruction as being a 32-bit
935 multiply. */
936 if (prev_mul32_p && exec_type == EXEC_PARALLEL)
937 cur_mul32_p = 1;
938
2fd5405a 939 for (j = 0; j < 2; j++)
252b5132 940 {
2fd5405a 941 for (i = 0; i < fx->fc; i++)
252b5132
RH
942 {
943 if (fx->fix[i].reloc)
944 {
2fd5405a 945 where = (f - frag_now->fr_literal) + 4 * j;
252b5132
RH
946
947 fix_new_exp (frag_now,
2fd5405a 948 where,
252b5132
RH
949 fx->fix[i].size,
950 &(fx->fix[i].exp),
951 fx->fix[i].pcrel,
952 fx->fix[i].reloc);
953 }
954 }
2fd5405a 955
252b5132
RH
956 fx->fc = 0;
957 fx = fx->next;
958 }
2fd5405a 959
252b5132
RH
960 return 0;
961}
962
2fd5405a
NC
963/* Check 2 instructions and determine if they can be safely
964 executed in parallel. Return 1 if they can be. */
252b5132 965
252b5132
RH
966static int
967parallel_ok (op1, insn1, op2, insn2, exec_type)
968 struct d30v_insn *op1, *op2;
969 unsigned long insn1, insn2;
970 exec_type_enum exec_type;
971{
972 int i, j, shift, regno, bits, ecc;
973 unsigned long flags, mask, flags_set1, flags_set2, flags_used1, flags_used2;
974 unsigned long ins, mod_reg[2][3], used_reg[2][3], flag_reg[2];
975 struct d30v_format *f;
976 struct d30v_opcode *op;
977
2fd5405a 978 /* Section 4.3: Both instructions must not be IU or MU only. */
252b5132
RH
979 if ((op1->op->unit == IU && op2->op->unit == IU)
980 || (op1->op->unit == MU && op2->op->unit == MU))
981 return 0;
982
2fd5405a 983 /* First instruction must not be a jump to safely optimize, unless this
252b5132
RH
984 is an explicit parallel operation. */
985 if (exec_type != EXEC_PARALLEL
986 && (op1->op->flags_used & (FLAG_JMP | FLAG_JSR)))
987 return 0;
988
989 /* If one instruction is /TX or /XT and the other is /FX or /XF respectively,
990 then it is safe to allow the two to be done as parallel ops, since only
991 one will ever be executed at a time. */
992 if ((op1->ecc == ECC_TX && op2->ecc == ECC_FX)
993 || (op1->ecc == ECC_FX && op2->ecc == ECC_TX)
994 || (op1->ecc == ECC_XT && op2->ecc == ECC_XF)
995 || (op1->ecc == ECC_XF && op2->ecc == ECC_XT))
2fd5405a 996 return 1;
252b5132
RH
997
998 /* [0] r0-r31
999 [1] r32-r63
2fd5405a 1000 [2] a0, a1, flag registers. */
252b5132
RH
1001 for (j = 0; j < 2; j++)
1002 {
1003 if (j == 0)
1004 {
1005 f = op1->form;
1006 op = op1->op;
1007 ecc = op1->ecc;
1008 ins = insn1;
1009 }
1010 else
1011 {
1012 f = op2->form;
1013 op = op2->op;
1014 ecc = op2->ecc;
1015 ins = insn2;
1016 }
81d4177b 1017
252b5132
RH
1018 flag_reg[j] = 0;
1019 mod_reg[j][0] = mod_reg[j][1] = 0;
1020 used_reg[j][0] = used_reg[j][1] = 0;
1021
1022 if (flag_explicitly_parallel)
1023 {
1024 /* For human specified parallel instructions we have been asked
1025 to ignore the possibility that both instructions could modify
1026 bits in the PSW, so we initialise the mod & used arrays to 0.
1027 We have been asked, however, to refuse to allow parallel
1028 instructions which explicitly set the same flag register,
1029 eg "cmpne f0,r1,0x10 || cmpeq f0, r5, 0x2", so further on we test
1030 for the use of a flag register and set a bit in the mod or used
1031 array appropriately. */
252b5132
RH
1032 mod_reg[j][2] = 0;
1033 used_reg[j][2] = 0;
1034 }
1035 else
1036 {
1037 mod_reg[j][2] = (op->flags_set & FLAG_ALL);
1038 used_reg[j][2] = (op->flags_used & FLAG_ALL);
1039 }
2fd5405a
NC
1040
1041 /* BSR/JSR always sets R62. */
252b5132 1042 if (op->flags_used & FLAG_JSR)
2fd5405a 1043 mod_reg[j][1] = (1L << (62 - 32));
252b5132 1044
2fd5405a 1045 /* Conditional execution affects the flags_used. */
252b5132
RH
1046 switch (ecc)
1047 {
1048 case ECC_TX:
1049 case ECC_FX:
1050 used_reg[j][2] |= flag_reg[j] = FLAG_0;
1051 break;
1052
1053 case ECC_XT:
1054 case ECC_XF:
1055 used_reg[j][2] |= flag_reg[j] = FLAG_1;
1056 break;
1057
1058 case ECC_TT:
1059 case ECC_TF:
1060 used_reg[j][2] |= flag_reg[j] = (FLAG_0 | FLAG_1);
1061 break;
1062 }
1063
1064 for (i = 0; f->operands[i]; i++)
1065 {
1066 flags = d30v_operand_table[f->operands[i]].flags;
1067 shift = 12 - d30v_operand_table[f->operands[i]].position;
1068 bits = d30v_operand_table[f->operands[i]].bits;
1069 if (bits == 32)
1070 mask = 0xffffffff;
1071 else
1072 mask = 0x7FFFFFFF >> (31 - bits);
1073
1074 if ((flags & OPERAND_PLUS) || (flags & OPERAND_MINUS))
1075 {
2fd5405a
NC
1076 /* This is a post-increment or post-decrement.
1077 The previous register needs to be marked as modified. */
1078 shift = 12 - d30v_operand_table[f->operands[i - 1]].position;
252b5132
RH
1079 regno = (ins >> shift) & 0x3f;
1080 if (regno >= 32)
1081 mod_reg[j][1] |= 1L << (regno - 32);
1082 else
1083 mod_reg[j][0] |= 1L << regno;
1084 }
1085 else if (flags & OPERAND_REG)
1086 {
1087 regno = (ins >> shift) & mask;
2fd5405a
NC
1088 /* The memory write functions don't have a destination
1089 register. */
252b5132
RH
1090 if ((flags & OPERAND_DEST) && !(op->flags_set & FLAG_MEM))
1091 {
2fd5405a 1092 /* MODIFIED registers and flags. */
252b5132
RH
1093 if (flags & OPERAND_ACC)
1094 {
1095 if (regno == 0)
1096 mod_reg[j][2] |= FLAG_A0;
1097 else if (regno == 1)
1098 mod_reg[j][2] |= FLAG_A1;
1099 else
1100 abort ();
1101 }
1102 else if (flags & OPERAND_FLAG)
1103 mod_reg[j][2] |= 1L << regno;
1104 else if (!(flags & OPERAND_CONTROL))
1105 {
1106 int r, z;
1107
2fd5405a
NC
1108 /* Need to check if there are two destination
1109 registers, for example ld2w. */
252b5132
RH
1110 if (flags & OPERAND_2REG)
1111 z = 1;
1112 else
1113 z = 0;
1114
1115 for (r = regno; r <= regno + z; r++)
2fd5405a 1116 {
252b5132
RH
1117 if (r >= 32)
1118 mod_reg[j][1] |= 1L << (r - 32);
1119 else
1120 mod_reg[j][0] |= 1L << r;
1121 }
1122 }
1123 }
1124 else
1125 {
2fd5405a 1126 /* USED, but not modified registers and flags. */
252b5132
RH
1127 if (flags & OPERAND_ACC)
1128 {
1129 if (regno == 0)
1130 used_reg[j][2] |= FLAG_A0;
1131 else if (regno == 1)
1132 used_reg[j][2] |= FLAG_A1;
1133 else
1134 abort ();
1135 }
1136 else if (flags & OPERAND_FLAG)
1137 used_reg[j][2] |= 1L << regno;
1138 else if (!(flags & OPERAND_CONTROL))
1139 {
1140 int r, z;
1141
2fd5405a
NC
1142 /* Need to check if there are two source
1143 registers, for example st2w. */
252b5132
RH
1144 if (flags & OPERAND_2REG)
1145 z = 1;
1146 else
1147 z = 0;
1148
1149 for (r = regno; r <= regno + z; r++)
2fd5405a 1150 {
252b5132
RH
1151 if (r >= 32)
1152 used_reg[j][1] |= 1L << (r - 32);
1153 else
1154 used_reg[j][0] |= 1L << r;
1155 }
1156 }
1157 }
1158 }
1159 }
1160 }
2fd5405a 1161
252b5132
RH
1162 flags_set1 = op1->op->flags_set;
1163 flags_set2 = op2->op->flags_set;
1164 flags_used1 = op1->op->flags_used;
1165 flags_used2 = op2->op->flags_used;
1166
25f2196d
CC
1167 /* Check for illegal combinations with ADDppp/SUBppp. */
1168 if (((flags_set1 & FLAG_NOT_WITH_ADDSUBppp) != 0
252b5132 1169 && (flags_used2 & FLAG_ADDSUBppp) != 0)
25f2196d 1170 || ((flags_set2 & FLAG_NOT_WITH_ADDSUBppp) != 0
252b5132
RH
1171 && (flags_used1 & FLAG_ADDSUBppp) != 0))
1172 return 0;
1173
1174 /* Load instruction combined with half-word multiply is illegal. */
1175 if (((flags_used1 & FLAG_MEM) != 0 && (flags_used2 & FLAG_MUL16))
1176 || ((flags_used2 & FLAG_MEM) != 0 && (flags_used1 & FLAG_MUL16)))
1177 return 0;
1178
1179 /* Specifically allow add || add by removing carry, overflow bits dependency.
1180 This is safe, even if an addc follows since the IU takes the argument in
1181 the right container, and it writes its results last.
1182 However, don't paralellize add followed by addc or sub followed by
1183 subb. */
252b5132
RH
1184 if (mod_reg[0][2] == FLAG_CVVA && mod_reg[1][2] == FLAG_CVVA
1185 && (used_reg[0][2] & ~flag_reg[0]) == 0
1186 && (used_reg[1][2] & ~flag_reg[1]) == 0
1187 && op1->op->unit == EITHER && op2->op->unit == EITHER)
1188 {
1189 mod_reg[0][2] = mod_reg[1][2] = 0;
1190 }
1191
1192 for (j = 0; j < 3; j++)
1193 {
1194 /* If the second instruction depends on the first, we obviously
1195 cannot parallelize. Note, the mod flag implies use, so
1196 check that as well. */
2fd5405a 1197 /* If flag_explicitly_parallel is set, then the case of the
252b5132
RH
1198 second instruction using a register the first instruction
1199 modifies is assumed to be okay; we trust the human. We
1200 don't trust the human if both instructions modify the same
1201 register but we do trust the human if they modify the same
2fd5405a 1202 flags. */
252b5132
RH
1203 /* We have now been requested not to trust the human if the
1204 instructions modify the same flag registers either. */
1205 if (flag_explicitly_parallel)
1206 {
1207 if ((mod_reg[0][j] & mod_reg[1][j]) != 0)
1208 return 0;
1209 }
1210 else
cc8a6dd0 1211 if ((mod_reg[0][j] & (mod_reg[1][j] | used_reg[1][j])) != 0)
252b5132
RH
1212 return 0;
1213 }
1214
1215 return 1;
1216}
1217
2fd5405a
NC
1218/* This is the main entry point for the machine-dependent assembler.
1219 STR points to a machine-dependent instruction. This function is
1220 supposed to emit the frags/bytes it assembles to. For the D30V, it
1221 mostly handles the special VLIW parsing and packing and leaves the
1222 difficult stuff to do_assemble (). */
252b5132
RH
1223
1224static long long prev_insn = -1;
1225static struct d30v_insn prev_opcode;
1226static subsegT prev_subseg;
1227static segT prev_seg = 0;
1228
1229void
1230md_assemble (str)
1231 char *str;
1232{
1233 struct d30v_insn opcode;
1234 long long insn;
2fd5405a
NC
1235 /* Execution type; parallel, etc. */
1236 exec_type_enum extype = EXEC_UNKNOWN;
1237 /* Saved extype. Used for multiline instructions. */
1238 static exec_type_enum etype = EXEC_UNKNOWN;
252b5132
RH
1239 char *str2;
1240
1241 if ((prev_insn != -1) && prev_seg
2fd5405a 1242 && ((prev_seg != now_seg) || (prev_subseg != now_subseg)))
b34976b6 1243 d30v_cleanup (FALSE);
252b5132
RH
1244
1245 if (d30v_current_align < 3)
1246 d30v_align (3, NULL, d30v_last_label);
1247 else if (d30v_current_align > 3)
1248 d30v_current_align = 3;
1249 d30v_last_label = NULL;
1250
1251 flag_explicitly_parallel = 0;
1252 flag_xp_state = 0;
1253 if (etype == EXEC_UNKNOWN)
1254 {
2fd5405a 1255 /* Look for the special multiple instruction separators. */
252b5132 1256 str2 = strstr (str, "||");
2fd5405a 1257 if (str2)
252b5132
RH
1258 {
1259 extype = EXEC_PARALLEL;
1260 flag_xp_state = 1;
1261 }
1262 else
1263 {
1264 str2 = strstr (str, "->");
2fd5405a 1265 if (str2)
252b5132
RH
1266 extype = EXEC_SEQ;
1267 else
1268 {
1269 str2 = strstr (str, "<-");
2fd5405a 1270 if (str2)
252b5132
RH
1271 extype = EXEC_REVSEQ;
1272 }
1273 }
81d4177b 1274
2fd5405a
NC
1275 /* STR2 points to the separator, if one. */
1276 if (str2)
252b5132
RH
1277 {
1278 *str2 = 0;
2fd5405a
NC
1279
1280 /* If two instructions are present and we already have one saved,
1281 then first write it out. */
b34976b6 1282 d30v_cleanup (FALSE);
2fd5405a 1283
252b5132
RH
1284 /* Assemble first instruction and save it. */
1285 prev_insn = do_assemble (str, &prev_opcode, 1, 0);
1286 if (prev_insn == -1)
1287 as_bad (_("Cannot assemble instruction"));
1288 if (prev_opcode.form != NULL && prev_opcode.form->form >= LONG)
2fd5405a 1289 as_bad (_("First opcode is long. Unable to mix instructions as specified."));
252b5132
RH
1290 fixups = fixups->next;
1291 str = str2 + 2;
1292 prev_seg = now_seg;
1293 prev_subseg = now_subseg;
1294 }
1295 }
1296
1297 insn = do_assemble (str, &opcode,
1298 (extype != EXEC_UNKNOWN || etype != EXEC_UNKNOWN),
1299 extype == EXEC_PARALLEL);
1300 if (insn == -1)
1301 {
1302 if (extype != EXEC_UNKNOWN)
1303 etype = extype;
1304 as_bad (_("Cannot assemble instruction"));
1305 return;
1306 }
1307
1308 if (etype != EXEC_UNKNOWN)
1309 {
1310 extype = etype;
1311 etype = EXEC_UNKNOWN;
1312 }
1313
1314 /* Word multiply instructions must not be followed by either a load or a
1315 16-bit multiply instruction in the next cycle. */
1316 if ( (extype != EXEC_REVSEQ)
1317 && prev_mul32_p
1318 && (opcode.op->flags_used & (FLAG_MEM | FLAG_MUL16)))
1319 {
1320 /* However, load and multiply should able to be combined in a parallel
1321 operation, so check for that first. */
1322 if (prev_insn != -1
1323 && (opcode.op->flags_used & FLAG_MEM)
1324 && opcode.form->form < LONG
1325 && (extype == EXEC_PARALLEL || (Optimizing && extype == EXEC_UNKNOWN))
2fd5405a
NC
1326 && parallel_ok (&prev_opcode, (long) prev_insn,
1327 &opcode, (long) insn, extype)
1328 && write_2_short (&prev_opcode, (long) prev_insn,
1329 &opcode, (long) insn, extype, fixups) == 0)
252b5132 1330 {
2fd5405a 1331 /* No instructions saved. */
252b5132
RH
1332 prev_insn = -1;
1333 return;
1334 }
1335 else
1336 {
2fd5405a
NC
1337 /* Can't parallelize, flush previous instruction and emit a
1338 word of NOPS, unless the previous instruction is a NOP,
1339 in which case just flush it, as this will generate a word
1340 of NOPs for us. */
252b5132
RH
1341
1342 if (prev_insn != -1 && (strcmp (prev_opcode.op->name, "nop") == 0))
b34976b6 1343 d30v_cleanup (FALSE);
252b5132
RH
1344 else
1345 {
2fd5405a
NC
1346 char *f;
1347
252b5132 1348 if (prev_insn != -1)
b34976b6 1349 d30v_cleanup (TRUE);
252b5132
RH
1350 else
1351 {
1352 f = frag_more (8);
1353 d30v_number_to_chars (f, NOP2, 8);
2fd5405a 1354
252b5132
RH
1355 if (warn_nops == NOP_ALL || warn_nops == NOP_MULTIPLY)
1356 {
1357 if (opcode.op->flags_used & FLAG_MEM)
1358 as_warn (_("word of NOPs added between word multiply and load"));
1359 else
1360 as_warn (_("word of NOPs added between word multiply and 16-bit multiply"));
1361 }
1362 }
1363 }
2fd5405a 1364
252b5132
RH
1365 extype = EXEC_UNKNOWN;
1366 }
1367 }
1368 else if ( (extype == EXEC_REVSEQ)
1369 && cur_mul32_p
1370 && (prev_opcode.op->flags_used & (FLAG_MEM | FLAG_MUL16)))
1371 {
2fd5405a
NC
1372 /* Can't parallelize, flush current instruction and add a
1373 sequential NOP. */
b34976b6 1374 write_1_short (&opcode, (long) insn, fixups->next->next, TRUE);
2fd5405a 1375
252b5132
RH
1376 /* Make the previous instruction the current one. */
1377 extype = EXEC_UNKNOWN;
1378 insn = prev_insn;
1379 now_seg = prev_seg;
1380 now_subseg = prev_subseg;
1381 prev_insn = -1;
1382 cur_mul32_p = prev_mul32_p;
1383 prev_mul32_p = 0;
1384 memcpy (&opcode, &prev_opcode, sizeof (prev_opcode));
1385 }
1386
2fd5405a
NC
1387 /* If this is a long instruction, write it and any previous short
1388 instruction. */
1389 if (opcode.form->form >= LONG)
252b5132 1390 {
2fd5405a 1391 if (extype != EXEC_UNKNOWN)
252b5132 1392 as_bad (_("Instruction uses long version, so it cannot be mixed as specified"));
b34976b6 1393 d30v_cleanup (FALSE);
2fd5405a 1394 write_long (&opcode, insn, fixups);
252b5132
RH
1395 prev_insn = -1;
1396 }
1397 else if ((prev_insn != -1)
1398 && (write_2_short
2fd5405a
NC
1399 (&prev_opcode, (long) prev_insn, &opcode,
1400 (long) insn, extype, fixups) == 0))
252b5132
RH
1401 {
1402 /* No instructions saved. */
1403 prev_insn = -1;
1404 }
1405 else
1406 {
2fd5405a 1407 if (extype != EXEC_UNKNOWN)
252b5132 1408 as_bad (_("Unable to mix instructions as specified"));
2fd5405a 1409
252b5132
RH
1410 /* Save off last instruction so it may be packed on next pass. */
1411 memcpy (&prev_opcode, &opcode, sizeof (prev_opcode));
1412 prev_insn = insn;
1413 prev_seg = now_seg;
1414 prev_subseg = now_subseg;
1415 fixups = fixups->next;
1416 prev_mul32_p = cur_mul32_p;
1417 }
1418}
1419
2fd5405a
NC
1420/* Assemble a single instruction and return an opcode.
1421 Return -1 (an invalid opcode) on error. */
252b5132
RH
1422
1423#define NAME_BUF_LEN 20
1424
1425static long long
2fd5405a 1426do_assemble (str, opcode, shortp, is_parallel)
252b5132
RH
1427 char *str;
1428 struct d30v_insn *opcode;
1429 int shortp;
1430 int is_parallel;
1431{
2132e3a3
AM
1432 char *op_start;
1433 char *save;
1434 char *op_end;
2fd5405a
NC
1435 char name[NAME_BUF_LEN];
1436 int cmp_hack;
1437 int nlen = 0;
1438 int fsize = (shortp ? FORCE_SHORT : 0);
1439 expressionS myops[6];
1440 long long insn;
1441
1442 /* Drop leading whitespace. */
1443 while (*str == ' ')
1444 str++;
1445
1446 /* Find the opcode end. */
2132e3a3 1447 for (op_start = op_end = str;
2fd5405a 1448 *op_end
252b5132 1449 && nlen < (NAME_BUF_LEN - 1)
2fd5405a 1450 && *op_end != '/'
2132e3a3 1451 && !is_end_of_line[(unsigned char) *op_end] && *op_end != ' ';
252b5132
RH
1452 op_end++)
1453 {
3882b010 1454 name[nlen] = TOLOWER (op_start[nlen]);
252b5132
RH
1455 nlen++;
1456 }
1457
1458 if (nlen == 0)
1459 return -1;
1460
1461 name[nlen] = 0;
1462
2fd5405a 1463 /* If there is an execution condition code, handle it. */
252b5132
RH
1464 if (*op_end == '/')
1465 {
1466 int i = 0;
2fd5405a 1467 while ((i < ECC_MAX) && strncasecmp (d30v_ecc_names[i], op_end + 1, 2))
252b5132 1468 i++;
2fd5405a 1469
252b5132
RH
1470 if (i == ECC_MAX)
1471 {
1472 char tmp[4];
1473 strncpy (tmp, op_end + 1, 2);
1474 tmp[2] = 0;
2fd5405a 1475 as_bad (_("unknown condition code: %s"), tmp);
252b5132
RH
1476 return -1;
1477 }
252b5132
RH
1478 opcode->ecc = i;
1479 op_end += 3;
1480 }
1481 else
1482 opcode->ecc = ECC_AL;
252b5132 1483
2fd5405a 1484 /* CMP and CMPU change their name based on condition codes. */
252b5132
RH
1485 if (!strncmp (name, "cmp", 3))
1486 {
2fd5405a
NC
1487 int p, i;
1488 char **str = (char **) d30v_cc_names;
252b5132
RH
1489 if (name[3] == 'u')
1490 p = 4;
1491 else
1492 p = 3;
1493
2fd5405a 1494 for (i = 1; *str && strncmp (*str, &name[p], 2); i++, str++)
252b5132
RH
1495 ;
1496
2fd5405a 1497 /* cmpu only supports some condition codes. */
252b5132
RH
1498 if (p == 4)
1499 {
1500 if (i < 3 || i > 6)
1501 {
2fd5405a
NC
1502 name[p + 2] = 0;
1503 as_bad (_("cmpu doesn't support condition code %s"), &name[p]);
252b5132
RH
1504 }
1505 }
1506
1507 if (!*str)
1508 {
2fd5405a
NC
1509 name[p + 2] = 0;
1510 as_bad (_("unknown condition code: %s"), &name[p]);
252b5132 1511 }
2fd5405a 1512
252b5132
RH
1513 cmp_hack = i;
1514 name[p] = 0;
1515 }
1516 else
1517 cmp_hack = 0;
252b5132 1518
2fd5405a
NC
1519 /* Need to look for .s or .l. */
1520 if (name[nlen - 2] == '.')
252b5132 1521 {
2fd5405a 1522 switch (name[nlen - 1])
252b5132
RH
1523 {
1524 case 's':
1525 fsize = FORCE_SHORT;
1526 break;
1527 case 'l':
1528 fsize = FORCE_LONG;
1529 break;
1530 }
2fd5405a 1531 name[nlen - 2] = 0;
252b5132
RH
1532 }
1533
2fd5405a
NC
1534 /* Find the first opcode with the proper name. */
1535 opcode->op = (struct d30v_opcode *) hash_find (d30v_hash, name);
252b5132
RH
1536 if (opcode->op == NULL)
1537 {
2fd5405a 1538 as_bad (_("unknown opcode: %s"), name);
252b5132
RH
1539 return -1;
1540 }
1541
1542 save = input_line_pointer;
1543 input_line_pointer = op_end;
1544 while (!(opcode->form = find_format (opcode->op, myops, fsize, cmp_hack)))
1545 {
1546 opcode->op++;
1547 if (opcode->op->name == NULL || strcmp (opcode->op->name, name))
1548 {
2fd5405a
NC
1549 as_bad (_("operands for opcode `%s' do not match any valid format"),
1550 name);
252b5132
RH
1551 return -1;
1552 }
1553 }
1554 input_line_pointer = save;
1555
2fd5405a 1556 insn = build_insn (opcode, myops);
252b5132 1557
2d2255b5 1558 /* Propagate multiply status. */
252b5132
RH
1559 if (insn != -1)
1560 {
1561 if (is_parallel && prev_mul32_p)
1562 cur_mul32_p = 1;
1563 else
1564 {
1565 prev_mul32_p = cur_mul32_p;
1566 cur_mul32_p = (opcode->op->flags_used & FLAG_MUL32) != 0;
1567 }
1568 }
1569
2fd5405a 1570 /* Propagate left_kills_right status. */
252b5132
RH
1571 if (insn != -1)
1572 {
1573 prev_left_kills_right_p = cur_left_kills_right_p;
1574
1575 if (opcode->op->flags_set & FLAG_LKR)
1576 {
1577 cur_left_kills_right_p = 1;
2fd5405a 1578
252b5132
RH
1579 if (strcmp (opcode->op->name, "mvtsys") == 0)
1580 {
2fd5405a
NC
1581 /* Left kills right for only mvtsys only for
1582 PSW/PSWH/PSWL/flags target. */
252b5132
RH
1583 if ((myops[0].X_op == O_register) &&
1584 ((myops[0].X_add_number == OPERAND_CONTROL) || /* psw */
1585 (myops[0].X_add_number == OPERAND_CONTROL+MAX_CONTROL_REG+2) || /* pswh */
1586 (myops[0].X_add_number == OPERAND_CONTROL+MAX_CONTROL_REG+1) || /* pswl */
1587 (myops[0].X_add_number == OPERAND_FLAG+0) || /* f0 */
1588 (myops[0].X_add_number == OPERAND_FLAG+1) || /* f1 */
1589 (myops[0].X_add_number == OPERAND_FLAG+2) || /* f2 */
1590 (myops[0].X_add_number == OPERAND_FLAG+3) || /* f3 */
1591 (myops[0].X_add_number == OPERAND_FLAG+4) || /* f4 */
1592 (myops[0].X_add_number == OPERAND_FLAG+5) || /* f5 */
1593 (myops[0].X_add_number == OPERAND_FLAG+6) || /* f6 */
1594 (myops[0].X_add_number == OPERAND_FLAG+7))) /* f7 */
1595 {
1596 cur_left_kills_right_p = 1;
1597 }
1598 else
1599 {
2fd5405a
NC
1600 /* Other mvtsys target registers don't kill right
1601 instruction. */
252b5132
RH
1602 cur_left_kills_right_p = 0;
1603 }
1604 } /* mvtsys */
1605 }
1606 else
1607 cur_left_kills_right_p = 0;
1608 }
1609
1610 return insn;
1611}
1612
2fd5405a 1613/* Get a pointer to an entry in the format table.
252b5132 1614 It must look at all formats for an opcode and use the operands
2fd5405a 1615 to choose the correct one. Return NULL on error. */
252b5132
RH
1616
1617static struct d30v_format *
1618find_format (opcode, myops, fsize, cmp_hack)
1619 struct d30v_opcode *opcode;
1620 expressionS myops[];
1621 int fsize;
1622 int cmp_hack;
1623{
2fd5405a 1624 int numops, match, index, i = 0, j, k;
252b5132
RH
1625 struct d30v_format *fm;
1626
1627 if (opcode == NULL)
1628 return NULL;
2fd5405a 1629
252b5132
RH
1630 /* Get all the operands and save them as expressions. */
1631 numops = get_operands (myops, cmp_hack);
1632
1633 while ((index = opcode->format[i++]) != 0)
1634 {
1635 if (fsize == FORCE_SHORT && index >= LONG)
1636 continue;
1637
1638 if (fsize == FORCE_LONG && index < LONG)
1639 continue;
1640
2fd5405a 1641 fm = (struct d30v_format *) &d30v_format_table[index];
252b5132
RH
1642 k = index;
1643 while (fm->form == index)
1644 {
1645 match = 1;
1646 /* Now check the operands for compatibility. */
1647 for (j = 0; match && fm->operands[j]; j++)
1648 {
1649 int flags = d30v_operand_table[fm->operands[j]].flags;
1650 int bits = d30v_operand_table[fm->operands[j]].bits;
1651 int X_op = myops[j].X_op;
1652 int num = myops[j].X_add_number;
2fd5405a 1653
252b5132
RH
1654 if (flags & OPERAND_SPECIAL)
1655 break;
1656 else if (X_op == O_illegal)
1657 match = 0;
1658 else if (flags & OPERAND_REG)
1659 {
1660 if (X_op != O_register
1661 || ((flags & OPERAND_ACC) && !(num & OPERAND_ACC))
1662 || (!(flags & OPERAND_ACC) && (num & OPERAND_ACC))
1663 || ((flags & OPERAND_FLAG) && !(num & OPERAND_FLAG))
1664 || (!(flags & (OPERAND_FLAG | OPERAND_CONTROL)) && (num & OPERAND_FLAG))
1665 || ((flags & OPERAND_CONTROL)
1666 && !(num & (OPERAND_CONTROL | OPERAND_FLAG))))
1667 {
1668 match = 0;
1669 }
1670 }
1671 else if (((flags & OPERAND_MINUS)
2fd5405a 1672 && (X_op != O_absent || num != OPERAND_MINUS))
252b5132
RH
1673 || ((flags & OPERAND_PLUS)
1674 && (X_op != O_absent || num != OPERAND_PLUS))
1675 || ((flags & OPERAND_ATMINUS)
1676 && (X_op != O_absent || num != OPERAND_ATMINUS))
1677 || ((flags & OPERAND_ATPAR)
1678 && (X_op != O_absent || num != OPERAND_ATPAR))
1679 || ((flags & OPERAND_ATSIGN)
1680 && (X_op != O_absent || num != OPERAND_ATSIGN)))
1681 {
2fd5405a 1682 match = 0;
252b5132
RH
1683 }
1684 else if (flags & OPERAND_NUM)
1685 {
1686 /* A number can be a constant or symbol expression. */
1687
2fd5405a
NC
1688 /* If we have found a register name, but that name
1689 also matches a symbol, then re-parse the name as
1690 an expression. */
252b5132
RH
1691 if (X_op == O_register
1692 && symbol_find ((char *) myops[j].X_op_symbol))
1693 {
1694 input_line_pointer = (char *) myops[j].X_op_symbol;
2fd5405a 1695 expression (&myops[j]);
252b5132 1696 }
2fd5405a 1697
252b5132
RH
1698 /* Turn an expression into a symbol for later resolution. */
1699 if (X_op != O_absent && X_op != O_constant
1700 && X_op != O_symbol && X_op != O_register
1701 && X_op != O_big)
1702 {
1703 symbolS *sym = make_expr_symbol (&myops[j]);
1704 myops[j].X_op = X_op = O_symbol;
1705 myops[j].X_add_symbol = sym;
1706 myops[j].X_add_number = num = 0;
1707 }
1708
1709 if (fm->form >= LONG)
1710 {
2fd5405a 1711 /* If we're testing for a LONG format, either fits. */
252b5132
RH
1712 if (X_op != O_constant && X_op != O_symbol)
1713 match = 0;
1714 }
1715 else if (fm->form < LONG
1716 && ((fsize == FORCE_SHORT && X_op == O_symbol)
1717 || (fm->form == SHORT_D2 && j == 0)))
1718 match = 1;
2fd5405a 1719
252b5132 1720 /* This is the tricky part. Will the constant or symbol
2fd5405a 1721 fit into the space in the current format? */
252b5132
RH
1722 else if (X_op == O_constant)
1723 {
1724 if (check_range (num, bits, flags))
1725 match = 0;
1726 }
1727 else if (X_op == O_symbol
1728 && S_IS_DEFINED (myops[j].X_add_symbol)
1729 && S_GET_SEGMENT (myops[j].X_add_symbol) == now_seg
1730 && opcode->reloc_flag == RELOC_PCREL)
1731 {
1732 /* If the symbol is defined, see if the value will fit
2fd5405a 1733 into the form we're considering. */
252b5132
RH
1734 fragS *f;
1735 long value;
1736
1737 /* Calculate the current address by running through the
1738 previous frags and adding our current offset. */
1739 value = 0;
1740 for (f = frchain_now->frch_root; f; f = f->fr_next)
1741 value += f->fr_fix + f->fr_offset;
1742 value = (S_GET_VALUE (myops[j].X_add_symbol) - value
1743 - (obstack_next_free (&frchain_now->frch_obstack)
1744 - frag_now->fr_literal));
2fd5405a 1745 if (check_range (value, bits, flags))
252b5132
RH
1746 match = 0;
1747 }
1748 else
1749 match = 0;
1750 }
1751 }
252b5132
RH
1752 /* We're only done if the operands matched so far AND there
1753 are no more to check. */
1754 if (match && myops[j].X_op == 0)
1755 {
1756 /* Final check - issue a warning if an odd numbered register
1757 is used as the first register in an instruction that reads
1758 or writes 2 registers. */
1759
1760 for (j = 0; fm->operands[j]; j++)
1761 if (myops[j].X_op == O_register
1762 && (myops[j].X_add_number & 1)
1763 && (d30v_operand_table[fm->operands[j]].flags & OPERAND_2REG))
2fd5405a
NC
1764 as_warn (_("Odd numbered register used as target of multi-register instruction"));
1765
252b5132
RH
1766 return fm;
1767 }
2fd5405a 1768 fm = (struct d30v_format *) &d30v_format_table[++k];
252b5132 1769 }
252b5132
RH
1770 }
1771 return NULL;
1772}
1773
2fd5405a
NC
1774/* If while processing a fixup, a reloc really needs to be created,
1775 then it is done here. */
1776
252b5132
RH
1777arelent *
1778tc_gen_reloc (seg, fixp)
ebf19f1b 1779 asection *seg ATTRIBUTE_UNUSED;
252b5132
RH
1780 fixS *fixp;
1781{
1782 arelent *reloc;
1783 reloc = (arelent *) xmalloc (sizeof (arelent));
310b5aa2
ILT
1784 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
1785 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
1786 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1787 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1788 if (reloc->howto == (reloc_howto_type *) NULL)
1789 {
1790 as_bad_where (fixp->fx_file, fixp->fx_line,
2fd5405a
NC
1791 _("reloc %d not supported by object file format"),
1792 (int) fixp->fx_r_type);
252b5132
RH
1793 return NULL;
1794 }
a161fe53
AM
1795
1796 reloc->addend = 0;
252b5132
RH
1797 return reloc;
1798}
1799
1800int
1801md_estimate_size_before_relax (fragp, seg)
ebf19f1b
AM
1802 fragS *fragp ATTRIBUTE_UNUSED;
1803 asection *seg ATTRIBUTE_UNUSED;
252b5132
RH
1804{
1805 abort ();
1806 return 0;
2fd5405a 1807}
252b5132
RH
1808
1809long
1810md_pcrel_from_section (fixp, sec)
1811 fixS *fixp;
1812 segT sec;
1813{
2fd5405a
NC
1814 if (fixp->fx_addsy != (symbolS *) NULL
1815 && (!S_IS_DEFINED (fixp->fx_addsy)
1816 || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
252b5132
RH
1817 return 0;
1818 return fixp->fx_frag->fr_address + fixp->fx_where;
1819}
1820
94f592af
NC
1821void
1822md_apply_fix3 (fixP, valP, seg)
1823 fixS *fixP;
a161fe53 1824 valueT *valP;
ebf19f1b 1825 segT seg ATTRIBUTE_UNUSED;
252b5132 1826{
2fd5405a 1827 char *where;
252b5132 1828 unsigned long insn, insn2;
a161fe53 1829 long value = *valP;
94f592af
NC
1830
1831 if (fixP->fx_addsy == (symbolS *) NULL)
1832 fixP->fx_done = 1;
1833
a161fe53
AM
1834 /* We don't support subtracting a symbol. */
1835 if (fixP->fx_subsy != (symbolS *) NULL)
1836 as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
2fd5405a 1837
252b5132
RH
1838 /* Fetch the instruction, insert the fully resolved operand
1839 value, and stuff the instruction back again. */
94f592af 1840 where = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 1841 insn = bfd_getb32 ((unsigned char *) where);
2fd5405a 1842
94f592af 1843 switch (fixP->fx_r_type)
252b5132
RH
1844 {
1845 case BFD_RELOC_8: /* Check for a bad .byte directive. */
94f592af 1846 if (fixP->fx_addsy != NULL)
252b5132 1847 as_bad (_("line %d: unable to place address of symbol '%s' into a byte"),
94f592af 1848 fixP->fx_line, S_GET_NAME (fixP->fx_addsy));
252b5132 1849 else if (((unsigned)value) > 0xff)
ebf19f1b 1850 as_bad (_("line %d: unable to place value %lx into a byte"),
94f592af 1851 fixP->fx_line, value);
252b5132 1852 else
2fd5405a 1853 *(unsigned char *) where = value;
252b5132 1854 break;
2fd5405a 1855
252b5132 1856 case BFD_RELOC_16: /* Check for a bad .short directive. */
94f592af 1857 if (fixP->fx_addsy != NULL)
252b5132 1858 as_bad (_("line %d: unable to place address of symbol '%s' into a short"),
94f592af 1859 fixP->fx_line, S_GET_NAME (fixP->fx_addsy));
252b5132 1860 else if (((unsigned)value) > 0xffff)
ebf19f1b 1861 as_bad (_("line %d: unable to place value %lx into a short"),
94f592af 1862 fixP->fx_line, value);
252b5132
RH
1863 else
1864 bfd_putb16 ((bfd_vma) value, (unsigned char *) where);
1865 break;
2fd5405a 1866
252b5132 1867 case BFD_RELOC_64: /* Check for a bad .quad directive. */
94f592af 1868 if (fixP->fx_addsy != NULL)
252b5132 1869 as_bad (_("line %d: unable to place address of symbol '%s' into a quad"),
94f592af 1870 fixP->fx_line, S_GET_NAME (fixP->fx_addsy));
252b5132
RH
1871 else
1872 {
1873 bfd_putb32 ((bfd_vma) value, (unsigned char *) where);
1874 bfd_putb32 (0, ((unsigned char *) where) + 4);
1875 }
1876 break;
2fd5405a 1877
252b5132 1878 case BFD_RELOC_D30V_6:
94f592af 1879 check_size (value, 6, fixP->fx_file, fixP->fx_line);
252b5132
RH
1880 insn |= value & 0x3F;
1881 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1882 break;
1883
1884 case BFD_RELOC_D30V_9_PCREL:
94f592af 1885 if (fixP->fx_where & 0x7)
252b5132 1886 {
94f592af 1887 if (fixP->fx_done)
252b5132
RH
1888 value += 4;
1889 else
94f592af 1890 fixP->fx_r_type = BFD_RELOC_D30V_9_PCREL_R;
252b5132 1891 }
94f592af 1892 check_size (value, 9, fixP->fx_file, fixP->fx_line);
252b5132
RH
1893 insn |= ((value >> 3) & 0x3F) << 12;
1894 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1895 break;
1896
1897 case BFD_RELOC_D30V_15:
94f592af 1898 check_size (value, 15, fixP->fx_file, fixP->fx_line);
252b5132
RH
1899 insn |= (value >> 3) & 0xFFF;
1900 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1901 break;
1902
1903 case BFD_RELOC_D30V_15_PCREL:
94f592af 1904 if (fixP->fx_where & 0x7)
252b5132 1905 {
94f592af 1906 if (fixP->fx_done)
252b5132
RH
1907 value += 4;
1908 else
94f592af 1909 fixP->fx_r_type = BFD_RELOC_D30V_15_PCREL_R;
252b5132 1910 }
94f592af 1911 check_size (value, 15, fixP->fx_file, fixP->fx_line);
252b5132
RH
1912 insn |= (value >> 3) & 0xFFF;
1913 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1914 break;
1915
1916 case BFD_RELOC_D30V_21:
94f592af 1917 check_size (value, 21, fixP->fx_file, fixP->fx_line);
252b5132
RH
1918 insn |= (value >> 3) & 0x3FFFF;
1919 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1920 break;
1921
1922 case BFD_RELOC_D30V_21_PCREL:
94f592af 1923 if (fixP->fx_where & 0x7)
252b5132 1924 {
94f592af 1925 if (fixP->fx_done)
252b5132
RH
1926 value += 4;
1927 else
94f592af 1928 fixP->fx_r_type = BFD_RELOC_D30V_21_PCREL_R;
252b5132 1929 }
94f592af 1930 check_size (value, 21, fixP->fx_file, fixP->fx_line);
252b5132
RH
1931 insn |= (value >> 3) & 0x3FFFF;
1932 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1933 break;
1934
1935 case BFD_RELOC_D30V_32:
1936 insn2 = bfd_getb32 ((unsigned char *) where + 4);
2fd5405a
NC
1937 insn |= (value >> 26) & 0x3F; /* Top 6 bits. */
1938 insn2 |= ((value & 0x03FC0000) << 2); /* Next 8 bits. */
1939 insn2 |= value & 0x0003FFFF; /* Bottom 18 bits. */
252b5132
RH
1940 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1941 bfd_putb32 ((bfd_vma) insn2, (unsigned char *) where + 4);
1942 break;
1943
1944 case BFD_RELOC_D30V_32_PCREL:
1945 insn2 = bfd_getb32 ((unsigned char *) where + 4);
81d4177b
KH
1946 insn |= (value >> 26) & 0x3F; /* Top 6 bits. */
1947 insn2 |= ((value & 0x03FC0000) << 2); /* Next 8 bits. */
1948 insn2 |= value & 0x0003FFFF; /* Bottom 18 bits. */
252b5132
RH
1949 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1950 bfd_putb32 ((bfd_vma) insn2, (unsigned char *) where + 4);
1951 break;
1952
1953 case BFD_RELOC_32:
1954 bfd_putb32 ((bfd_vma) value, (unsigned char *) where);
1955 break;
1956
1957 default:
1958 as_bad (_("line %d: unknown relocation type: 0x%x"),
94f592af 1959 fixP->fx_line, fixP->fx_r_type);
252b5132 1960 }
252b5132
RH
1961}
1962
2fd5405a
NC
1963/* Called after the assembler has finished parsing the input file or
1964 after a label is defined. Because the D30V assembler sometimes
1965 saves short instructions to see if it can package them with the
1966 next instruction, there may be a short instruction that still needs
1967 written. */
252b5132 1968
252b5132
RH
1969int
1970d30v_cleanup (use_sequential)
1971 int use_sequential;
1972{
1973 segT seg;
1974 subsegT subseg;
1975
1976 if (prev_insn != -1)
1977 {
1978 seg = now_seg;
1979 subseg = now_subseg;
1980 subseg_set (prev_seg, prev_subseg);
2fd5405a
NC
1981 write_1_short (&prev_opcode, (long) prev_insn, fixups->next,
1982 use_sequential);
252b5132
RH
1983 subseg_set (seg, subseg);
1984 prev_insn = -1;
1985 if (use_sequential)
b34976b6 1986 prev_mul32_p = FALSE;
252b5132 1987 }
81d4177b 1988
252b5132
RH
1989 return 1;
1990}
1991
2fd5405a 1992static void
252b5132 1993d30v_number_to_chars (buf, value, n)
2fd5405a
NC
1994 char *buf; /* Return 'nbytes' of chars here. */
1995 long long value; /* The value of the bits. */
1996 int n; /* Number of bytes in the output. */
252b5132
RH
1997{
1998 while (n--)
1999 {
2000 buf[n] = value & 0xff;
2001 value >>= 8;
2002 }
2003}
2004
2fd5405a
NC
2005/* This function is called at the start of every line. It checks to
2006 see if the first character is a '.', which indicates the start of a
2007 pseudo-op. If it is, then write out any unwritten instructions. */
252b5132 2008
2fd5405a 2009void
252b5132
RH
2010d30v_start_line ()
2011{
2012 char *c = input_line_pointer;
2013
3882b010 2014 while (ISSPACE (*c))
252b5132
RH
2015 c++;
2016
2017 if (*c == '.')
b34976b6 2018 d30v_cleanup (FALSE);
252b5132
RH
2019}
2020
2fd5405a 2021static void
252b5132
RH
2022check_size (value, bits, file, line)
2023 long value;
2024 int bits;
2025 char *file;
2026 int line;
2027{
2028 int tmp, max;
2029
2030 if (value < 0)
2031 tmp = ~value;
2032 else
2033 tmp = value;
2fd5405a 2034
252b5132
RH
2035 max = (1 << (bits - 1)) - 1;
2036
2037 if (tmp > max)
2038 as_bad_where (file, line, _("value too large to fit in %d bits"), bits);
252b5132
RH
2039}
2040
2041/* d30v_frob_label() is called when after a label is recognized. */
2042
2043void
2044d30v_frob_label (lab)
2045 symbolS *lab;
2046{
2047 /* Emit any pending instructions. */
b34976b6 2048 d30v_cleanup (FALSE);
252b5132
RH
2049
2050 /* Update the label's address with the current output pointer. */
7dcc9865 2051 symbol_set_frag (lab, frag_now);
252b5132
RH
2052 S_SET_VALUE (lab, (valueT) frag_now_fix ());
2053
2054 /* Record this label for future adjustment after we find out what
2055 kind of data it references, and the required alignment therewith. */
2056 d30v_last_label = lab;
2057}
2058
2059/* Hook into cons for capturing alignment changes. */
2060
2061void
2062d30v_cons_align (size)
2063 int size;
2064{
2065 int log_size;
2066
2067 log_size = 0;
2068 while ((size >>= 1) != 0)
2069 ++log_size;
2070
2071 if (d30v_current_align < log_size)
2072 d30v_align (log_size, (char *) NULL, NULL);
2073 else if (d30v_current_align > log_size)
2074 d30v_current_align = log_size;
2075 d30v_last_label = NULL;
2076}
2077
2078/* Called internally to handle all alignment needs. This takes care
2079 of eliding calls to frag_align if'n the cached current alignment
2080 says we've already got it, as well as taking care of the auto-aligning
2081 labels wrt code. */
2082
2083static void
2084d30v_align (n, pfill, label)
2085 int n;
2086 char *pfill;
2087 symbolS *label;
2088{
2089 /* The front end is prone to changing segments out from under us
2090 temporarily when -g is in effect. */
2091 int switched_seg_p = (d30v_current_align_seg != now_seg);
2092
2093 /* Do not assume that if 'd30v_current_align >= n' and
2094 '! switched_seg_p' that it is safe to avoid performing
2d2255b5 2095 this alignment request. The alignment of the current frag
252b5132
RH
2096 can be changed under our feet, for example by a .ascii
2097 directive in the source code. cf testsuite/gas/d30v/reloc.s */
b34976b6 2098 d30v_cleanup (FALSE);
252b5132
RH
2099
2100 if (pfill == NULL)
2101 {
2102 if (n > 2
2103 && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
2104 {
2105 static char const nop[4] = { 0x00, 0xf0, 0x00, 0x00 };
2106
2107 /* First, make sure we're on a four-byte boundary, in case
2108 someone has been putting .byte values the text section. */
2109 if (d30v_current_align < 2 || switched_seg_p)
2110 frag_align (2, 0, 0);
2111 frag_align_pattern (n, nop, sizeof nop, 0);
2112 }
2113 else
2114 frag_align (n, 0, 0);
2115 }
2116 else
2117 frag_align (n, *pfill, 0);
2118
2119 if (!switched_seg_p)
2120 d30v_current_align = n;
2121
2122 if (label != NULL)
2123 {
2fd5405a 2124 symbolS *sym;
b34976b6 2125 int label_seen = FALSE;
2fd5405a
NC
2126 struct frag *old_frag;
2127 valueT old_value;
2128 valueT new_value;
2129
252b5132
RH
2130 assert (S_GET_SEGMENT (label) == now_seg);
2131
7dcc9865 2132 old_frag = symbol_get_frag (label);
252b5132
RH
2133 old_value = S_GET_VALUE (label);
2134 new_value = (valueT) frag_now_fix ();
2fd5405a 2135
252b5132
RH
2136 /* It is possible to have more than one label at a particular
2137 address, especially if debugging is enabled, so we must
2138 take care to adjust all the labels at this address in this
2139 fragment. To save time we search from the end of the symbol
2140 list, backwards, since the symbols we are interested in are
2141 almost certainly the ones that were most recently added.
2142 Also to save time we stop searching once we have seen at least
2143 one matching label, and we encounter a label that is no longer
2144 in the target fragment. Note, this search is guaranteed to
2145 find at least one match when sym == label, so no special case
2146 code is necessary. */
7dcc9865 2147 for (sym = symbol_lastP; sym != NULL; sym = symbol_previous (sym))
252b5132 2148 {
7dcc9865
ILT
2149 if (symbol_get_frag (sym) == old_frag
2150 && S_GET_VALUE (sym) == old_value)
252b5132 2151 {
b34976b6 2152 label_seen = TRUE;
7dcc9865 2153 symbol_set_frag (sym, frag_now);
252b5132
RH
2154 S_SET_VALUE (sym, new_value);
2155 }
7dcc9865 2156 else if (label_seen && symbol_get_frag (sym) != old_frag)
252b5132
RH
2157 break;
2158 }
2159 }
2160
2161 record_alignment (now_seg, n);
2162}
2163
2164/* Handle the .align pseudo-op. This aligns to a power of two. We
2165 hook here to latch the current alignment. */
2166
2167static void
2168s_d30v_align (ignore)
ebf19f1b 2169 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
2170{
2171 int align;
2172 char fill, *pfill = NULL;
2173 long max_alignment = 15;
2174
2175 align = get_absolute_expression ();
2176 if (align > max_alignment)
2177 {
2178 align = max_alignment;
2179 as_warn (_("Alignment too large: %d assumed"), align);
2180 }
2181 else if (align < 0)
2182 {
2183 as_warn (_("Alignment negative: 0 assumed"));
2184 align = 0;
2185 }
2186
2187 if (*input_line_pointer == ',')
2188 {
2189 input_line_pointer++;
2190 fill = get_absolute_expression ();
2191 pfill = &fill;
2192 }
2193
2194 d30v_last_label = NULL;
2195 d30v_align (align, pfill, NULL);
2196
2197 demand_empty_rest_of_line ();
2198}
2199
2200/* Handle the .text pseudo-op. This is like the usual one, but it
2201 clears the saved last label and resets known alignment. */
2202
2203static void
2204s_d30v_text (i)
2205 int i;
2206
2207{
2208 s_text (i);
2209 d30v_last_label = NULL;
2210 d30v_current_align = 0;
2211 d30v_current_align_seg = now_seg;
2212}
2213
2214/* Handle the .data pseudo-op. This is like the usual one, but it
2215 clears the saved last label and resets known alignment. */
2216
2217static void
2218s_d30v_data (i)
2219 int i;
2220{
2221 s_data (i);
2222 d30v_last_label = NULL;
2223 d30v_current_align = 0;
2224 d30v_current_align_seg = now_seg;
2225}
2226
2227/* Handle the .section pseudo-op. This is like the usual one, but it
2228 clears the saved last label and resets known alignment. */
2229
2230static void
2231s_d30v_section (ignore)
2232 int ignore;
2233{
2234 obj_elf_section (ignore);
2235 d30v_last_label = NULL;
2236 d30v_current_align = 0;
2237 d30v_current_align_seg = now_seg;
2238}
This page took 0.428875 seconds and 4 git commands to generate.