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