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