Commit | Line | Data |
---|---|---|
c6aa56bc C |
1 | /* tc-v850.c -- Assembler code for the NEC V850 |
2 | ||
3 | Copyright (C) 1996 Free Software Foundation. | |
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/v850.h" | |
27 | \f | |
0e8f9bd1 JL |
28 | /* Structure to hold information about predefined registers. */ |
29 | struct reg_name | |
30 | { | |
31 | const char *name; | |
32 | int value; | |
33 | }; | |
34 | ||
c6aa56bc C |
35 | /* Generic assembler global variables which must be defined by all targets. */ |
36 | ||
37 | /* Characters which always start a comment. */ | |
38 | const char comment_chars[] = "#"; | |
39 | ||
40 | /* Characters which start a comment at the beginning of a line. */ | |
dcf5d117 | 41 | const char line_comment_chars[] = ";#"; |
c6aa56bc C |
42 | |
43 | /* Characters which may be used to separate multiple commands on a | |
44 | single line. */ | |
45 | const char line_separator_chars[] = ";"; | |
46 | ||
47 | /* Characters which are used to indicate an exponent in a floating | |
48 | point number. */ | |
49 | const char EXP_CHARS[] = "eE"; | |
50 | ||
51 | /* Characters which mean that a number is a floating point constant, | |
52 | as in 0d1.0. */ | |
53 | const char FLT_CHARS[] = "dD"; | |
54 | \f | |
55 | /* local functions */ | |
56 | static unsigned long v850_insert_operand | |
57 | PARAMS ((unsigned long insn, const struct v850_operand *operand, | |
58 | offsetT val, char *file, unsigned int line)); | |
c84615bc | 59 | static int reg_name_search PARAMS ((const struct reg_name *, int, const char *)); |
c6aa56bc | 60 | static boolean register_name PARAMS ((expressionS *expressionP)); |
0e8f9bd1 | 61 | static boolean system_register_name PARAMS ((expressionS *expressionP)); |
c9f1b2d9 | 62 | static boolean cc_name PARAMS ((expressionS *expressionP)); |
c6aa56bc C |
63 | static int postfix PARAMS ((char *p)); |
64 | static bfd_reloc_code_real_type get_reloc PARAMS ((struct v850_operand *op)); | |
65 | static unsigned long build_insn PARAMS ((struct v850_opcode *opcode, expressionS *opers)); | |
66 | ||
67 | ||
68 | /* fixups */ | |
69 | #define MAX_INSN_FIXUPS (5) | |
70 | struct v850_fixup | |
71 | { | |
72 | expressionS exp; | |
73 | int opindex; | |
74 | bfd_reloc_code_real_type reloc; | |
75 | }; | |
76 | struct v850_fixup fixups[MAX_INSN_FIXUPS]; | |
77 | static int fc; | |
78 | \f | |
79 | const char *md_shortopts = ""; | |
80 | struct option md_longopts[] = { | |
81 | {NULL, no_argument, NULL, 0} | |
82 | }; | |
83 | size_t md_longopts_size = sizeof(md_longopts); | |
84 | ||
85 | /* The target specific pseudo-ops which we support. */ | |
86 | const pseudo_typeS md_pseudo_table[] = | |
87 | { | |
88 | /* | |
89 | { "byte", ppc_byte, 0 }, | |
90 | { "long", ppc_elf_cons, 4 }, | |
91 | { "word", ppc_elf_cons, 2 }, | |
92 | { "short", ppc_elf_cons, 2 }, | |
93 | { "rdata", ppc_elf_rdata, 0 }, | |
94 | { "rodata", ppc_elf_rdata, 0 }, | |
95 | { "lcomm", ppc_elf_lcomm, 0 }, | |
96 | */ | |
97 | { NULL, NULL, 0 } | |
98 | }; | |
99 | ||
100 | /* Opcode hash table. */ | |
101 | static struct hash_control *v850_hash; | |
102 | ||
1510cd39 C |
103 | /* This table is sorted. Suitable for searching by a binary search. */ |
104 | static const struct reg_name pre_defined_registers[] = | |
c6aa56bc C |
105 | { |
106 | { "ep", 30 }, /* ep - element ptr */ | |
107 | { "gp", 4 }, /* gp - global ptr */ | |
108 | { "lp", 31 }, /* lp - link ptr */ | |
109 | { "r0", 0 }, | |
110 | { "r1", 1 }, | |
111 | { "r10", 10 }, | |
112 | { "r11", 11 }, | |
113 | { "r12", 12 }, | |
114 | { "r13", 13 }, | |
115 | { "r14", 14 }, | |
116 | { "r15", 15 }, | |
117 | { "r16", 16 }, | |
118 | { "r17", 17 }, | |
119 | { "r18", 18 }, | |
120 | { "r19", 19 }, | |
121 | { "r2", 2 }, | |
122 | { "r20", 20 }, | |
123 | { "r21", 21 }, | |
124 | { "r22", 22 }, | |
125 | { "r23", 23 }, | |
126 | { "r24", 24 }, | |
127 | { "r25", 25 }, | |
128 | { "r26", 26 }, | |
129 | { "r27", 27 }, | |
130 | { "r28", 28 }, | |
131 | { "r29", 29 }, | |
132 | { "r3", 3 }, | |
133 | { "r30", 30 }, | |
134 | { "r31", 31 }, | |
135 | { "r4", 4 }, | |
136 | { "r5", 5 }, | |
137 | { "r6", 6 }, | |
138 | { "r7", 7 }, | |
139 | { "r8", 8 }, | |
140 | { "r9", 9 }, | |
141 | { "sp", 3 }, /* sp - stack ptr */ | |
142 | { "tp", 5 }, /* tp - text ptr */ | |
143 | { "zero", 0 }, | |
144 | }; | |
1510cd39 C |
145 | #define REG_NAME_CNT (sizeof(pre_defined_registers) / sizeof(struct reg_name)) |
146 | ||
147 | ||
148 | static const struct reg_name system_registers[] = | |
149 | { | |
150 | { "eipc", 0 }, | |
151 | { "eipsw", 1 }, | |
152 | { "fepc", 2 }, | |
153 | { "fepsw", 3 }, | |
154 | { "ecr", 4 }, | |
155 | { "psw", 5 }, | |
156 | }; | |
0e8f9bd1 | 157 | #define SYSREG_NAME_CNT (sizeof(system_registers) / sizeof(struct reg_name)) |
1510cd39 C |
158 | |
159 | static const struct reg_name cc_names[] = | |
160 | { | |
161 | { "c", 0x1 }, | |
162 | { "ge", 0xe }, | |
163 | { "gt", 0xf }, | |
164 | { "h", 0xb }, | |
165 | { "l", 0x1 }, | |
166 | { "le", 0x7 }, | |
167 | { "lt", 0x6 }, | |
168 | { "n", 0x4 }, | |
169 | { "nc", 0x9 }, | |
170 | { "nh", 0x3 }, | |
171 | { "nl", 0x9 }, | |
172 | { "ns", 0xc }, | |
173 | { "nv", 0x8 }, | |
174 | { "nz", 0xa }, | |
175 | { "p", 0xc }, | |
176 | { "s", 0x4 }, | |
177 | { "sa", 0xd }, | |
178 | { "t", 0x5 }, | |
179 | { "v", 0x0 }, | |
180 | { "z", 0x2 }, | |
181 | }; | |
c9f1b2d9 | 182 | #define CC_NAME_CNT (sizeof(cc_names) / sizeof(struct reg_name)) |
c6aa56bc | 183 | |
0e8f9bd1 JL |
184 | /* reg_name_search does a binary search of the given register table |
185 | to see if "name" is a valid regiter name. Returns the register | |
c6aa56bc C |
186 | number from the array on success, or -1 on failure. */ |
187 | ||
188 | static int | |
c84615bc C |
189 | reg_name_search (regs, regcount, name) |
190 | const struct reg_name *regs; | |
191 | int regcount; | |
192 | const char *name; | |
c6aa56bc | 193 | { |
c84615bc | 194 | int middle, low, high; |
c6aa56bc C |
195 | int cmp; |
196 | ||
197 | low = 0; | |
c84615bc C |
198 | high = regcount - 1; |
199 | ||
c6aa56bc C |
200 | do |
201 | { | |
202 | middle = (low + high) / 2; | |
c84615bc | 203 | cmp = strcasecmp (name, regs[middle].name); |
c6aa56bc C |
204 | if (cmp < 0) |
205 | high = middle - 1; | |
206 | else if (cmp > 0) | |
207 | low = middle + 1; | |
208 | else | |
c84615bc | 209 | return regs[middle].value; |
c6aa56bc C |
210 | } |
211 | while (low <= high); | |
212 | return -1; | |
213 | } | |
214 | ||
215 | ||
216 | /* Summary of register_name(). | |
217 | * | |
218 | * in: Input_line_pointer points to 1st char of operand. | |
219 | * | |
220 | * out: A expressionS. | |
221 | * The operand may have been a register: in this case, X_op == O_register, | |
222 | * X_add_number is set to the register number, and truth is returned. | |
223 | * Input_line_pointer->(next non-blank) char after operand, or is in | |
224 | * its original state. | |
225 | */ | |
226 | static boolean | |
227 | register_name (expressionP) | |
228 | expressionS *expressionP; | |
229 | { | |
230 | int reg_number; | |
231 | char *name; | |
232 | char *start; | |
233 | char c; | |
234 | ||
235 | /* Find the spelling of the operand */ | |
236 | start = name = input_line_pointer; | |
237 | ||
238 | c = get_symbol_end (); | |
c84615bc | 239 | reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name); |
0e8f9bd1 JL |
240 | |
241 | /* look to see if it's in the register table */ | |
242 | if (reg_number >= 0) | |
243 | { | |
244 | expressionP->X_op = O_register; | |
245 | expressionP->X_add_number = reg_number; | |
246 | ||
247 | /* make the rest nice */ | |
248 | expressionP->X_add_symbol = NULL; | |
249 | expressionP->X_op_symbol = NULL; | |
250 | *input_line_pointer = c; /* put back the delimiting char */ | |
251 | return true; | |
252 | } | |
253 | else | |
254 | { | |
255 | /* reset the line as if we had not done anything */ | |
256 | *input_line_pointer = c; /* put back the delimiting char */ | |
257 | input_line_pointer = start; /* reset input_line pointer */ | |
258 | return false; | |
259 | } | |
260 | } | |
261 | ||
262 | /* Summary of system_register_name(). | |
263 | * | |
264 | * in: Input_line_pointer points to 1st char of operand. | |
265 | * | |
266 | * out: A expressionS. | |
267 | * The operand may have been a register: in this case, X_op == O_register, | |
268 | * X_add_number is set to the register number, and truth is returned. | |
269 | * Input_line_pointer->(next non-blank) char after operand, or is in | |
270 | * its original state. | |
271 | */ | |
272 | static boolean | |
273 | system_register_name (expressionP) | |
274 | expressionS *expressionP; | |
275 | { | |
276 | int reg_number; | |
277 | char *name; | |
278 | char *start; | |
279 | char c; | |
280 | ||
281 | /* Find the spelling of the operand */ | |
282 | start = name = input_line_pointer; | |
283 | ||
284 | c = get_symbol_end (); | |
c84615bc | 285 | reg_number = reg_name_search (system_registers, SYSREG_NAME_CNT, name); |
c6aa56bc C |
286 | |
287 | /* look to see if it's in the register table */ | |
288 | if (reg_number >= 0) | |
289 | { | |
290 | expressionP->X_op = O_register; | |
291 | expressionP->X_add_number = reg_number; | |
292 | ||
293 | /* make the rest nice */ | |
294 | expressionP->X_add_symbol = NULL; | |
295 | expressionP->X_op_symbol = NULL; | |
296 | *input_line_pointer = c; /* put back the delimiting char */ | |
297 | return true; | |
298 | } | |
299 | else | |
300 | { | |
301 | /* reset the line as if we had not done anything */ | |
302 | *input_line_pointer = c; /* put back the delimiting char */ | |
303 | input_line_pointer = start; /* reset input_line pointer */ | |
304 | return false; | |
305 | } | |
306 | } | |
307 | ||
c9f1b2d9 JL |
308 | /* Summary of cc_name(). |
309 | * | |
310 | * in: Input_line_pointer points to 1st char of operand. | |
311 | * | |
312 | * out: A expressionS. | |
313 | * The operand may have been a register: in this case, X_op == O_register, | |
314 | * X_add_number is set to the register number, and truth is returned. | |
315 | * Input_line_pointer->(next non-blank) char after operand, or is in | |
316 | * its original state. | |
317 | */ | |
318 | static boolean | |
319 | cc_name (expressionP) | |
320 | expressionS *expressionP; | |
321 | { | |
322 | int reg_number; | |
323 | char *name; | |
324 | char *start; | |
325 | char c; | |
326 | ||
327 | /* Find the spelling of the operand */ | |
328 | start = name = input_line_pointer; | |
329 | ||
330 | c = get_symbol_end (); | |
c84615bc | 331 | reg_number = reg_name_search (cc_names, CC_NAME_CNT, name); |
c9f1b2d9 JL |
332 | |
333 | /* look to see if it's in the register table */ | |
334 | if (reg_number >= 0) | |
335 | { | |
336 | expressionP->X_op = O_constant; | |
337 | expressionP->X_add_number = reg_number; | |
338 | ||
339 | /* make the rest nice */ | |
340 | expressionP->X_add_symbol = NULL; | |
341 | expressionP->X_op_symbol = NULL; | |
342 | *input_line_pointer = c; /* put back the delimiting char */ | |
343 | return true; | |
344 | } | |
345 | else | |
346 | { | |
347 | /* reset the line as if we had not done anything */ | |
348 | *input_line_pointer = c; /* put back the delimiting char */ | |
349 | input_line_pointer = start; /* reset input_line pointer */ | |
350 | return false; | |
351 | } | |
352 | } | |
353 | ||
c6aa56bc C |
354 | void |
355 | md_show_usage (stream) | |
356 | FILE *stream; | |
357 | { | |
358 | fprintf(stream, "V850 options:\n\ | |
359 | none yet\n"); | |
360 | } | |
361 | ||
362 | int | |
363 | md_parse_option (c, arg) | |
364 | int c; | |
365 | char *arg; | |
366 | { | |
367 | return 0; | |
368 | } | |
369 | ||
370 | symbolS * | |
371 | md_undefined_symbol (name) | |
372 | char *name; | |
373 | { | |
374 | return 0; | |
375 | } | |
376 | ||
377 | char * | |
378 | md_atof (type, litp, sizep) | |
379 | int type; | |
380 | char *litp; | |
381 | int *sizep; | |
382 | { | |
383 | int prec; | |
384 | LITTLENUM_TYPE words[4]; | |
385 | char *t; | |
386 | int i; | |
387 | ||
388 | switch (type) | |
389 | { | |
390 | case 'f': | |
391 | prec = 2; | |
392 | break; | |
393 | ||
394 | case 'd': | |
395 | prec = 4; | |
396 | break; | |
397 | ||
398 | default: | |
399 | *sizep = 0; | |
400 | return "bad call to md_atof"; | |
401 | } | |
402 | ||
403 | t = atof_ieee (input_line_pointer, type, words); | |
404 | if (t) | |
405 | input_line_pointer = t; | |
406 | ||
407 | *sizep = prec * 2; | |
408 | ||
409 | for (i = prec - 1; i >= 0; i--) | |
410 | { | |
411 | md_number_to_chars (litp, (valueT) words[i], 2); | |
412 | litp += 2; | |
413 | } | |
414 | ||
415 | return NULL; | |
416 | } | |
417 | ||
418 | ||
419 | void | |
420 | md_convert_frag (abfd, sec, fragP) | |
421 | bfd *abfd; | |
422 | asection *sec; | |
423 | fragS *fragP; | |
424 | { | |
425 | /* printf ("call to md_convert_frag \n"); */ | |
426 | abort (); | |
427 | } | |
428 | ||
429 | valueT | |
430 | md_section_align (seg, addr) | |
431 | asection *seg; | |
432 | valueT addr; | |
433 | { | |
434 | int align = bfd_get_section_alignment (stdoutput, seg); | |
435 | return ((addr + (1 << align) - 1) & (-1 << align)); | |
436 | } | |
437 | ||
438 | void | |
439 | md_begin () | |
440 | { | |
441 | char *prev_name = ""; | |
442 | register const struct v850_opcode *op; | |
443 | const struct v850_opcode *op_end; | |
444 | ||
445 | v850_hash = hash_new(); | |
446 | ||
447 | /* Insert unique names into hash table. The V850 instruction set | |
448 | has many identical opcode names that have different opcodes based | |
449 | on the operands. This hash table then provides a quick index to | |
450 | the first opcode with a particular name in the opcode table. */ | |
451 | ||
452 | op = v850_opcodes; | |
05631de2 | 453 | while (op->name) |
c6aa56bc C |
454 | { |
455 | if (strcmp (prev_name, op->name)) | |
456 | { | |
457 | prev_name = (char *) op->name; | |
458 | hash_insert (v850_hash, op->name, (char *) op); | |
459 | } | |
05631de2 | 460 | op++; |
c6aa56bc C |
461 | } |
462 | } | |
463 | ||
464 | ||
c84615bc C |
465 | static bfd_reloc_code_real_type |
466 | v850_reloc_prefix() | |
c6aa56bc | 467 | { |
c84615bc C |
468 | if (strncmp(input_line_pointer, "hi0(", 4) == 0) |
469 | { | |
470 | input_line_pointer += 4; | |
471 | return BFD_RELOC_HI16; | |
472 | } | |
473 | if (strncmp(input_line_pointer, "hi(", 3) == 0) | |
474 | { | |
475 | input_line_pointer += 3; | |
476 | return BFD_RELOC_HI16_S; | |
477 | } | |
478 | if (strncmp (input_line_pointer, "lo(", 3) == 0) | |
479 | { | |
480 | input_line_pointer += 3; | |
481 | return BFD_RELOC_LO16; | |
482 | } | |
c6aa56bc | 483 | |
c84615bc C |
484 | /* FIXME: implement sda, tda, zda here */ |
485 | ||
486 | return BFD_RELOC_UNUSED; | |
487 | } | |
c6aa56bc | 488 | |
c6aa56bc C |
489 | void |
490 | md_assemble (str) | |
491 | char *str; | |
492 | { | |
493 | char *s; | |
494 | struct v850_opcode *opcode; | |
495 | struct v850_opcode *next_opcode; | |
496 | const unsigned char *opindex_ptr; | |
497 | int next_opindex; | |
498 | unsigned long insn; | |
499 | char *f; | |
500 | int i; | |
501 | int numops; | |
502 | int match; | |
c84615bc | 503 | bfd_reloc_code_real_type reloc; |
c6aa56bc C |
504 | |
505 | int numopts; | |
506 | expressionS myops[5]; | |
507 | ||
508 | /* Get the opcode. */ | |
509 | for (s = str; *s != '\0' && ! isspace (*s); s++) | |
510 | ; | |
511 | if (*s != '\0') | |
512 | *s++ = '\0'; | |
513 | ||
514 | /* find the first opcode with the proper name */ | |
515 | opcode = (struct v850_opcode *)hash_find (v850_hash, str); | |
516 | if (opcode == NULL) | |
517 | { | |
518 | as_bad ("Unrecognized opcode: `%s'", str); | |
519 | return; | |
520 | } | |
521 | ||
522 | str = s; | |
523 | while (isspace (*str)) | |
524 | ++str; | |
525 | ||
526 | input_line_pointer = str; | |
527 | ||
1510cd39 | 528 | for(;;) |
c6aa56bc | 529 | { |
1510cd39 C |
530 | const char *errmsg = NULL; |
531 | ||
532 | fc = 0; | |
533 | match = 0; | |
534 | next_opindex = 0; | |
d5974c57 | 535 | insn = opcode->opcode; |
1510cd39 | 536 | for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++) |
c6aa56bc | 537 | { |
1510cd39 C |
538 | const struct v850_operand *operand; |
539 | char *hold; | |
540 | expressionS ex; | |
541 | char endc; | |
c6aa56bc | 542 | |
1510cd39 | 543 | if (next_opindex == 0) |
c6aa56bc | 544 | { |
1510cd39 C |
545 | operand = &v850_operands[*opindex_ptr]; |
546 | } | |
547 | else | |
548 | { | |
549 | operand = &v850_operands[next_opindex]; | |
550 | next_opindex = 0; | |
c6aa56bc C |
551 | } |
552 | ||
1510cd39 C |
553 | errmsg = NULL; |
554 | ||
555 | while (*str == ' ' || *str == ',' || *str == '[' || *str == ']') | |
556 | ++str; | |
557 | ||
558 | /* Gather the operand. */ | |
559 | hold = input_line_pointer; | |
560 | input_line_pointer = str; | |
561 | ||
c84615bc C |
562 | |
563 | /* lo(), hi(), hi0(), etc... */ | |
564 | if ((reloc = v850_reloc_prefix()) != BFD_RELOC_UNUSED) | |
565 | { | |
566 | expression(&ex); | |
567 | ||
568 | if (*input_line_pointer++ != ')') | |
569 | { | |
570 | errmsg = "syntax error: expected `)'"; | |
571 | goto error; | |
572 | } | |
573 | ||
574 | if (ex.X_op == O_constant) | |
575 | { | |
576 | switch (reloc) | |
577 | { | |
578 | case BFD_RELOC_LO16: | |
579 | ex.X_add_number &= 0xffff; | |
580 | break; | |
581 | ||
582 | case BFD_RELOC_HI16: | |
583 | ex.X_add_number = ((ex.X_add_number >> 16) & 0xffff); | |
584 | break; | |
585 | ||
586 | case BFD_RELOC_HI16_S: | |
587 | ex.X_add_number = ((ex.X_add_number >> 16) & 0xffff) | |
588 | + ((ex.X_add_number >> 15) & 1); | |
589 | break; | |
590 | } | |
591 | ||
592 | insn = v850_insert_operand (insn, operand, ex.X_add_number, | |
593 | (char *) NULL, 0); | |
594 | } | |
595 | else | |
596 | { | |
597 | if (fc > MAX_INSN_FIXUPS) | |
598 | as_fatal ("too many fixups"); | |
599 | ||
600 | fixups[fc].exp = ex; | |
601 | fixups[fc].opindex = *opindex_ptr; | |
602 | fixups[fc].reloc = reloc; | |
603 | fc++; | |
604 | } | |
605 | } | |
606 | else | |
607 | { | |
1510cd39 | 608 | if ((operand->flags & V850_OPERAND_REG) != 0) |
c6aa56bc | 609 | { |
1510cd39 | 610 | if (!register_name(&ex)) |
c6aa56bc | 611 | { |
1510cd39 C |
612 | errmsg = "invalid register name"; |
613 | goto error; | |
c6aa56bc C |
614 | } |
615 | } | |
0e8f9bd1 JL |
616 | else if ((operand->flags & V850_OPERAND_SRG) != 0) |
617 | { | |
618 | if (!system_register_name(&ex)) | |
619 | { | |
620 | errmsg = "invalid system register name"; | |
621 | goto error; | |
622 | } | |
623 | } | |
88b47a85 JL |
624 | else if ((operand->flags & V850_OPERAND_EP) != 0) |
625 | { | |
626 | char *start = input_line_pointer; | |
627 | char c = get_symbol_end (); | |
c84615bc | 628 | if (strcmp (start, "ep") != 0 && strcmp (start, "r30") != 0) |
88b47a85 JL |
629 | { |
630 | /* Put things back the way we found them. */ | |
631 | *input_line_pointer = c; | |
632 | input_line_pointer = start; | |
633 | errmsg = "expected EP register"; | |
634 | goto error; | |
635 | } | |
636 | *input_line_pointer = c; | |
637 | str = input_line_pointer; | |
638 | input_line_pointer = hold; | |
c84615bc | 639 | |
88b47a85 JL |
640 | while (*str == ' ' || *str == ',' || *str == '[' || *str == ']') |
641 | ++str; | |
642 | continue; | |
643 | } | |
c9f1b2d9 JL |
644 | else if ((operand->flags & V850_OPERAND_CC) != 0) |
645 | { | |
646 | if (!cc_name(&ex)) | |
647 | { | |
648 | errmsg = "invalid condition code name"; | |
649 | goto error; | |
650 | } | |
651 | } | |
c9a32d6c JL |
652 | else if (register_name (&ex) |
653 | && (operand->flags & V850_OPERAND_REG) == 0) | |
654 | { | |
655 | errmsg = "syntax error: register not expected"; | |
656 | goto error; | |
657 | } | |
0e8f9bd1 JL |
658 | else if (system_register_name (&ex) |
659 | && (operand->flags & V850_OPERAND_SRG) == 0) | |
660 | { | |
661 | errmsg = "syntax error: system register not expected"; | |
662 | goto error; | |
663 | } | |
c9f1b2d9 JL |
664 | else if (cc_name (&ex) |
665 | && (operand->flags & V850_OPERAND_CC) == 0) | |
666 | { | |
667 | errmsg = "syntax error: condition code not expected"; | |
668 | goto error; | |
669 | } | |
1510cd39 C |
670 | else |
671 | { | |
c84615bc | 672 | expression(&ex); |
1510cd39 | 673 | } |
c6aa56bc | 674 | |
1510cd39 C |
675 | switch (ex.X_op) |
676 | { | |
677 | case O_illegal: | |
678 | errmsg = "illegal operand"; | |
679 | goto error; | |
680 | case O_absent: | |
681 | errmsg = "missing operand"; | |
682 | goto error; | |
683 | case O_register: | |
0e8f9bd1 | 684 | if ((operand->flags & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0) |
1510cd39 C |
685 | { |
686 | errmsg = "invalid operand"; | |
687 | goto error; | |
688 | } | |
689 | ||
690 | insn = v850_insert_operand (insn, operand, ex.X_add_number, | |
691 | (char *) NULL, 0); | |
692 | break; | |
693 | ||
694 | case O_constant: | |
695 | insn = v850_insert_operand (insn, operand, ex.X_add_number, | |
696 | (char *) NULL, 0); | |
697 | break; | |
698 | ||
699 | default: | |
700 | /* We need to generate a fixup for this expression. */ | |
701 | if (fc >= MAX_INSN_FIXUPS) | |
702 | as_fatal ("too many fixups"); | |
703 | fixups[fc].exp = ex; | |
704 | fixups[fc].opindex = *opindex_ptr; | |
705 | fixups[fc].reloc = BFD_RELOC_UNUSED; | |
706 | ++fc; | |
707 | break; | |
708 | } | |
709 | ||
c84615bc C |
710 | } |
711 | ||
712 | str = input_line_pointer; | |
713 | input_line_pointer = hold; | |
714 | ||
1510cd39 C |
715 | while (*str == ' ' || *str == ',' || *str == '[' || *str == ']') |
716 | ++str; | |
717 | } | |
718 | match = 1; | |
719 | ||
720 | error: | |
721 | if (match == 0) | |
722 | { | |
723 | next_opcode = opcode + 1; | |
724 | if (next_opcode->opcode != 0 && !strcmp(next_opcode->name, opcode->name)) | |
725 | { | |
726 | opcode = next_opcode; | |
727 | continue; | |
728 | } | |
729 | ||
730 | as_bad ("%s", errmsg); | |
731 | return; | |
732 | } | |
733 | break; | |
734 | } | |
735 | ||
c6aa56bc C |
736 | while (isspace (*str)) |
737 | ++str; | |
738 | ||
739 | if (*str != '\0') | |
740 | as_bad ("junk at end of line: `%s'", str); | |
1510cd39 C |
741 | |
742 | input_line_pointer = str; | |
c6aa56bc | 743 | |
c84615bc C |
744 | /* Write out the instruction |
745 | FIXME: we can determine the size of the opcode by the instruction | |
746 | pattern, it does not have to be stored in the opcode table. */ | |
c9a32d6c JL |
747 | f = frag_more (opcode->size); |
748 | md_number_to_chars (f, insn, opcode->size); | |
c84615bc C |
749 | |
750 | ||
751 | /* Create any fixups. At this point we do not use a | |
752 | bfd_reloc_code_real_type, but instead just use the | |
753 | BFD_RELOC_UNUSED plus the operand index. This lets us easily | |
754 | handle fixups for any operand type, although that is admittedly | |
755 | not a very exciting feature. We pick a BFD reloc type in | |
756 | md_apply_fix. */ | |
757 | for (i = 0; i < fc; i++) | |
758 | { | |
759 | const struct v850_operand *operand; | |
760 | ||
761 | operand = &v850_operands[fixups[i].opindex]; | |
762 | if (fixups[i].reloc != BFD_RELOC_UNUSED) | |
763 | { | |
764 | reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc); | |
765 | int size; | |
766 | int offset; | |
767 | fixS *fixP; | |
768 | ||
769 | if (!reloc_howto) | |
770 | abort(); | |
771 | ||
772 | size = bfd_get_reloc_size (reloc_howto); | |
773 | offset = 4 - size; | |
774 | ||
775 | if (size < 1 || size > 4) | |
776 | abort(); | |
777 | ||
778 | fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset, size, | |
779 | &fixups[i].exp, | |
780 | reloc_howto->pc_relative, | |
781 | fixups[i].reloc); | |
782 | } | |
783 | else | |
784 | { | |
785 | fix_new_exp (frag_now, f - frag_now->fr_literal, 4, | |
786 | &fixups[i].exp, | |
787 | 1 /* FIXME: V850_OPERAND_RELATIVE ??? */, | |
788 | ((bfd_reloc_code_real_type) | |
789 | (fixups[i].opindex + (int) BFD_RELOC_UNUSED))); | |
790 | } | |
791 | } | |
c6aa56bc C |
792 | } |
793 | ||
794 | ||
795 | /* if while processing a fixup, a reloc really needs to be created */ | |
796 | /* then it is done here */ | |
797 | ||
798 | arelent * | |
799 | tc_gen_reloc (seg, fixp) | |
800 | asection *seg; | |
801 | fixS *fixp; | |
802 | { | |
803 | arelent *reloc; | |
804 | reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent)); | |
805 | reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym; | |
806 | reloc->address = fixp->fx_frag->fr_address + fixp->fx_where; | |
807 | reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type); | |
808 | if (reloc->howto == (reloc_howto_type *) NULL) | |
809 | { | |
810 | as_bad_where (fixp->fx_file, fixp->fx_line, | |
811 | "reloc %d not supported by object file format", (int)fixp->fx_r_type); | |
812 | return NULL; | |
813 | } | |
814 | reloc->addend = fixp->fx_addnumber; | |
815 | /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */ | |
816 | return reloc; | |
817 | } | |
818 | ||
819 | int | |
820 | md_estimate_size_before_relax (fragp, seg) | |
821 | fragS *fragp; | |
822 | asection *seg; | |
823 | { | |
c6aa56bc C |
824 | return 0; |
825 | } | |
826 | ||
827 | long | |
c84615bc | 828 | md_pcrel_from (fixp) |
c6aa56bc | 829 | fixS *fixp; |
c6aa56bc | 830 | { |
c84615bc C |
831 | if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy)) |
832 | { | |
833 | /* The symbol is undefined. Let the linker figure it out. */ | |
834 | return 0; | |
835 | } | |
836 | return fixp->fx_frag->fr_address + fixp->fx_where; | |
c6aa56bc C |
837 | } |
838 | ||
839 | int | |
840 | md_apply_fix3 (fixp, valuep, seg) | |
841 | fixS *fixp; | |
842 | valueT *valuep; | |
843 | segT seg; | |
844 | { | |
c6aa56bc C |
845 | valueT value; |
846 | char *where; | |
847 | unsigned long insn; | |
848 | int op_type; | |
849 | ||
850 | if (fixp->fx_addsy == (symbolS *) NULL) | |
851 | { | |
852 | value = *valuep; | |
853 | fixp->fx_done = 1; | |
854 | } | |
855 | else if (fixp->fx_pcrel) | |
856 | value = *valuep; | |
857 | else | |
858 | { | |
859 | value = fixp->fx_offset; | |
860 | if (fixp->fx_subsy != (symbolS *) NULL) | |
861 | { | |
862 | if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section) | |
863 | value -= S_GET_VALUE (fixp->fx_subsy); | |
864 | else | |
865 | { | |
866 | /* We don't actually support subtracting a symbol. */ | |
867 | as_bad_where (fixp->fx_file, fixp->fx_line, | |
868 | "expression too complex"); | |
869 | } | |
870 | } | |
871 | } | |
c6aa56bc | 872 | |
c84615bc | 873 | /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */ |
c6aa56bc | 874 | |
c84615bc C |
875 | if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED) |
876 | { | |
877 | int opindex; | |
878 | const struct v850_operand *operand; | |
879 | char *where; | |
880 | unsigned long insn; | |
881 | ||
882 | opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED; | |
883 | operand = &v850_operands[opindex]; | |
884 | ||
885 | /* Fetch the instruction, insert the fully resolved operand | |
886 | value, and stuff the instruction back again. */ | |
887 | where = fixp->fx_frag->fr_literal + fixp->fx_where; | |
888 | insn = bfd_getb32((unsigned char *) where); | |
889 | insn = v850_insert_operand (insn, operand, (offsetT) value, | |
890 | fixp->fx_file, fixp->fx_line); | |
891 | bfd_putb32((bfd_vma) insn, (unsigned char *) where); | |
892 | ||
893 | if (fixp->fx_done) | |
894 | { | |
895 | /* Nothing else to do here. */ | |
896 | return 1; | |
897 | } | |
c6aa56bc | 898 | |
c84615bc C |
899 | /* Determine a BFD reloc value based on the operand information. |
900 | We are only prepared to turn a few of the operands into relocs. */ | |
c6aa56bc | 901 | |
c84615bc C |
902 | if (operand->bits == 22) |
903 | fixp->fx_r_type = BFD_RELOC_V850_22_PCREL; | |
904 | else if (operand->bits == 9) | |
905 | fixp->fx_r_type = BFD_RELOC_V850_9_PCREL; | |
906 | else | |
907 | { | |
908 | as_bad_where(fixp->fx_file, fixp->fx_line, | |
909 | "unresolved expression that must be resolved"); | |
910 | fixp->fx_done = 1; | |
911 | return 1; | |
912 | } | |
913 | } | |
c6aa56bc C |
914 | |
915 | fixp->fx_addnumber = value; | |
916 | return 1; | |
c6aa56bc C |
917 | } |
918 | ||
919 | \f | |
920 | /* Insert an operand value into an instruction. */ | |
921 | ||
922 | static unsigned long | |
923 | v850_insert_operand (insn, operand, val, file, line) | |
924 | unsigned long insn; | |
925 | const struct v850_operand *operand; | |
926 | offsetT val; | |
927 | char *file; | |
928 | unsigned int line; | |
929 | { | |
c84615bc | 930 | if (operand->bits != 16) |
c6aa56bc C |
931 | { |
932 | long min, max; | |
933 | offsetT test; | |
934 | ||
1510cd39 | 935 | if ((operand->flags & V850_OPERAND_SIGNED) != 0) |
c6aa56bc | 936 | { |
1510cd39 | 937 | #if 0 |
c6aa56bc C |
938 | if ((operand->flags & PPC_OPERAND_SIGNOPT) != 0 |
939 | && ppc_size == PPC_OPCODE_32) | |
940 | max = (1 << operand->bits) - 1; | |
941 | else | |
1510cd39 | 942 | #endif |
c6aa56bc C |
943 | max = (1 << (operand->bits - 1)) - 1; |
944 | min = - (1 << (operand->bits - 1)); | |
945 | } | |
946 | else | |
c6aa56bc C |
947 | { |
948 | max = (1 << operand->bits) - 1; | |
949 | min = 0; | |
950 | } | |
951 | ||
952 | #if 0 | |
953 | if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0) | |
954 | test = - val; | |
955 | else | |
956 | #endif | |
957 | test = val; | |
958 | ||
959 | ||
960 | if (test < (offsetT) min || test > (offsetT) max) | |
961 | { | |
962 | const char *err = | |
963 | "operand out of range (%s not between %ld and %ld)"; | |
964 | char buf[100]; | |
965 | ||
966 | sprint_value (buf, test); | |
967 | if (file == (char *) NULL) | |
968 | as_warn (err, buf, min, max); | |
969 | else | |
970 | as_warn_where (file, line, err, buf, min, max); | |
971 | } | |
972 | } | |
973 | ||
974 | insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift); | |
975 | return insn; | |
976 | } |