]>
Commit | Line | Data |
---|---|---|
a86481d3 DB |
1 | /* Assembler interface for targets using CGEN. -*- C -*- |
2 | CGEN: Cpu tools GENerator | |
3 | ||
4 | THIS FILE IS USED TO GENERATE fr30-asm.c. | |
5 | ||
6 | Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. | |
7 | ||
8 | This file is part of the GNU Binutils and GDB, the GNU debugger. | |
9 | ||
10 | This program is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
12 | the Free Software Foundation; either version 2, or (at your option) | |
13 | any later version. | |
14 | ||
15 | This program is distributed in the hope that it will be useful, | |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
21 | along with this program; if not, write to the Free Software Foundation, Inc., | |
22 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
23 | ||
24 | #include "sysdep.h" | |
25 | #include <ctype.h> | |
26 | #include <stdio.h> | |
27 | #include "ansidecl.h" | |
28 | #include "bfd.h" | |
29 | #include "symcat.h" | |
30 | #include "fr30-opc.h" | |
31 | #include "opintl.h" | |
32 | ||
33 | #undef min | |
34 | #define min(a,b) ((a) < (b) ? (a) : (b)) | |
35 | #undef max | |
36 | #define max(a,b) ((a) > (b) ? (a) : (b)) | |
37 | ||
38 | #undef INLINE | |
39 | #ifdef __GNUC__ | |
40 | #define INLINE __inline__ | |
41 | #else | |
42 | #define INLINE | |
43 | #endif | |
44 | ||
45 | static const char * insert_normal | |
46 | PARAMS ((CGEN_OPCODE_DESC, long, unsigned int, int, int, int, | |
47 | CGEN_INSN_BYTES_PTR)); | |
48 | static const char * parse_insn_normal | |
49 | PARAMS ((CGEN_OPCODE_DESC, const CGEN_INSN *, | |
50 | const char **, CGEN_FIELDS *)); | |
51 | static const char * insert_insn_normal | |
52 | PARAMS ((CGEN_OPCODE_DESC, const CGEN_INSN *, | |
53 | CGEN_FIELDS *, CGEN_INSN_BYTES_PTR, bfd_vma)); | |
54 | \f | |
55 | /* -- assembler routines inserted here */ | |
56 | ||
57 | /* Main entry point for operand parsing. | |
58 | ||
59 | This function is basically just a big switch statement. Earlier versions | |
60 | used tables to look up the function to use, but | |
61 | - if the table contains both assembler and disassembler functions then | |
62 | the disassembler contains much of the assembler and vice-versa, | |
63 | - there's a lot of inlining possibilities as things grow, | |
64 | - using a switch statement avoids the function call overhead. | |
65 | ||
66 | This function could be moved into `parse_insn_normal', but keeping it | |
67 | separate makes clear the interface between `parse_insn_normal' and each of | |
68 | the handlers. | |
69 | */ | |
70 | ||
71 | const char * | |
72 | fr30_cgen_parse_operand (od, opindex, strp, fields) | |
73 | CGEN_OPCODE_DESC od; | |
74 | int opindex; | |
75 | const char ** strp; | |
76 | CGEN_FIELDS * fields; | |
77 | { | |
78 | const char * errmsg; | |
79 | ||
80 | switch (opindex) | |
81 | { | |
82 | case FR30_OPERAND_RI : | |
83 | errmsg = cgen_parse_keyword (od, strp, & fr30_cgen_opval_h_gr, & fields->f_Ri); | |
84 | break; | |
85 | case FR30_OPERAND_RJ : | |
86 | errmsg = cgen_parse_keyword (od, strp, & fr30_cgen_opval_h_gr, & fields->f_Rj); | |
87 | break; | |
88 | ||
89 | default : | |
90 | /* xgettext:c-format */ | |
91 | fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex); | |
92 | abort (); | |
93 | } | |
94 | ||
95 | return errmsg; | |
96 | } | |
97 | ||
98 | /* Main entry point for operand insertion. | |
99 | ||
100 | This function is basically just a big switch statement. Earlier versions | |
101 | used tables to look up the function to use, but | |
102 | - if the table contains both assembler and disassembler functions then | |
103 | the disassembler contains much of the assembler and vice-versa, | |
104 | - there's a lot of inlining possibilities as things grow, | |
105 | - using a switch statement avoids the function call overhead. | |
106 | ||
107 | This function could be moved into `parse_insn_normal', but keeping it | |
108 | separate makes clear the interface between `parse_insn_normal' and each of | |
109 | the handlers. It's also needed by GAS to insert operands that couldn't be | |
110 | resolved during parsing. | |
111 | */ | |
112 | ||
113 | const char * | |
114 | fr30_cgen_insert_operand (od, opindex, fields, buffer, pc) | |
115 | CGEN_OPCODE_DESC od; | |
116 | int opindex; | |
117 | CGEN_FIELDS * fields; | |
118 | CGEN_INSN_BYTES_PTR buffer; | |
119 | bfd_vma pc; | |
120 | { | |
121 | const char * errmsg; | |
122 | ||
123 | switch (opindex) | |
124 | { | |
125 | case FR30_OPERAND_RI : | |
126 | errmsg = insert_normal (od, fields->f_Ri, 0|(1<<CGEN_OPERAND_UNSIGNED), 12, 4, CGEN_FIELDS_BITSIZE (fields), buffer); | |
127 | break; | |
128 | case FR30_OPERAND_RJ : | |
129 | errmsg = insert_normal (od, fields->f_Rj, 0|(1<<CGEN_OPERAND_UNSIGNED), 8, 4, CGEN_FIELDS_BITSIZE (fields), buffer); | |
130 | break; | |
131 | ||
132 | default : | |
133 | /* xgettext:c-format */ | |
134 | fprintf (stderr, _("Unrecognized field %d while building insn.\n"), | |
135 | opindex); | |
136 | abort (); | |
137 | } | |
138 | ||
139 | return errmsg; | |
140 | } | |
141 | ||
142 | cgen_parse_fn * const fr30_cgen_parse_handlers[] = | |
143 | { | |
144 | 0, /* default */ | |
145 | parse_insn_normal, | |
146 | }; | |
147 | ||
148 | cgen_insert_fn * const fr30_cgen_insert_handlers[] = | |
149 | { | |
150 | 0, /* default */ | |
151 | insert_insn_normal, | |
152 | }; | |
153 | ||
154 | void | |
155 | fr30_cgen_init_asm (od) | |
156 | CGEN_OPCODE_DESC od; | |
157 | { | |
158 | } | |
159 | ||
160 | \f | |
161 | #if ! CGEN_INT_INSN_P | |
162 | ||
163 | /* Subroutine of insert_normal. */ | |
164 | ||
165 | static INLINE void | |
166 | insert_1 (od, value, start, length, word_length, bufp) | |
167 | CGEN_OPCODE_DESC od; | |
168 | unsigned long value; | |
169 | int start,length,word_length; | |
170 | unsigned char *bufp; | |
171 | { | |
172 | unsigned long x,mask; | |
173 | int shift; | |
174 | int big_p = CGEN_OPCODE_INSN_ENDIAN (od) == CGEN_ENDIAN_BIG; | |
175 | ||
176 | switch (word_length) | |
177 | { | |
178 | case 8: | |
179 | x = *bufp; | |
180 | break; | |
181 | case 16: | |
182 | if (big_p) | |
183 | x = bfd_getb16 (bufp); | |
184 | else | |
185 | x = bfd_getl16 (bufp); | |
186 | break; | |
187 | case 24: | |
188 | /* ??? This may need reworking as these cases don't necessarily | |
189 | want the first byte and the last two bytes handled like this. */ | |
190 | if (big_p) | |
191 | x = (bfd_getb8 (bufp) << 16) | bfd_getb16 (bufp + 1); | |
192 | else | |
193 | x = bfd_getl16 (bufp) | (bfd_getb8 (bufp + 2) << 16); | |
194 | break; | |
195 | case 32: | |
196 | if (big_p) | |
197 | x = bfd_getb32 (bufp); | |
198 | else | |
199 | x = bfd_getl32 (bufp); | |
200 | break; | |
201 | default : | |
202 | abort (); | |
203 | } | |
204 | ||
205 | /* Written this way to avoid undefined behaviour. */ | |
206 | mask = (((1L << (length - 1)) - 1) << 1) | 1; | |
207 | if (CGEN_INSN_LSB0_P) | |
208 | shift = start; | |
209 | else | |
210 | shift = (word_length - (start + length)); | |
211 | x = (x & ~(mask << shift)) | ((value & mask) << shift); | |
212 | ||
213 | switch (word_length) | |
214 | { | |
215 | case 8: | |
216 | *bufp = x; | |
217 | break; | |
218 | case 16: | |
219 | if (big_p) | |
220 | bfd_putb16 (x, bufp); | |
221 | else | |
222 | bfd_putl16 (x, bufp); | |
223 | break; | |
224 | case 24: | |
225 | /* ??? This may need reworking as these cases don't necessarily | |
226 | want the first byte and the last two bytes handled like this. */ | |
227 | if (big_p) | |
228 | { | |
229 | bfd_putb8 (x >> 16, bufp); | |
230 | bfd_putb16 (x, bufp + 1); | |
231 | } | |
232 | else | |
233 | { | |
234 | bfd_putl16 (x, bufp); | |
235 | bfd_putb8 (x >> 16, bufp + 2); | |
236 | } | |
237 | break; | |
238 | case 32: | |
239 | if (big_p) | |
240 | bfd_putb32 (x, bufp); | |
241 | else | |
242 | bfd_putl32 (x, bufp); | |
243 | break; | |
244 | default : | |
245 | abort (); | |
246 | } | |
247 | } | |
248 | ||
249 | #endif /* ! CGEN_INT_INSN_P */ | |
250 | ||
251 | /* Default insertion routine. | |
252 | ||
253 | ATTRS is a mask of the boolean attributes. | |
254 | START is the starting bit number, architecture origin. | |
255 | LENGTH is the length of VALUE in bits. | |
256 | TOTAL_LENGTH is the total length of the insn. | |
257 | ||
258 | The result is an error message or NULL if success. */ | |
259 | ||
260 | /* ??? May need to know word length in order to properly place values as | |
261 | an insn may be made of multiple words and the current bit number handling | |
262 | may be insufficient. Word length is an architectural attribute and thus | |
263 | methinks the way to go [if needed] is to fetch this value from OD or | |
264 | define a macro in <arch>-opc.h rather than adding an extra argument - | |
265 | after all that's how endianness is handled. */ | |
266 | /* ??? This duplicates functionality with bfd's howto table and | |
267 | bfd_install_relocation. */ | |
268 | /* ??? For architectures where insns can be representable as ints, | |
269 | store insn in `field' struct and add registers, etc. while parsing? */ | |
270 | /* ??? This doesn't handle bfd_vma's. Create another function when | |
271 | necessary. */ | |
272 | ||
273 | static const char * | |
274 | insert_normal (od, value, attrs, start, length, total_length, buffer) | |
275 | CGEN_OPCODE_DESC od; | |
276 | long value; | |
277 | unsigned int attrs; | |
278 | int start; | |
279 | int length; | |
280 | int total_length; | |
281 | CGEN_INSN_BYTES_PTR buffer; | |
282 | { | |
283 | static char errbuf[100]; | |
284 | /* Written this way to avoid undefined behaviour. */ | |
285 | unsigned long mask = (((1L << (length - 1)) - 1) << 1) | 1; | |
286 | ||
287 | /* If LENGTH is zero, this operand doesn't contribute to the value. */ | |
288 | if (length == 0) | |
289 | return NULL; | |
290 | ||
291 | /* Ensure VALUE will fit. */ | |
292 | if ((attrs & CGEN_ATTR_MASK (CGEN_OPERAND_UNSIGNED)) != 0) | |
293 | { | |
294 | unsigned long maxval = mask; | |
295 | if ((unsigned long) value > maxval) | |
296 | { | |
297 | /* xgettext:c-format */ | |
298 | sprintf (errbuf, | |
299 | _("operand out of range (%lu not between 0 and %lu)"), | |
300 | value, maxval); | |
301 | return errbuf; | |
302 | } | |
303 | } | |
304 | else | |
305 | { | |
306 | long minval = - (1L << (length - 1)); | |
307 | long maxval = (1L << (length - 1)) - 1; | |
308 | if (value < minval || value > maxval) | |
309 | { | |
310 | sprintf | |
311 | /* xgettext:c-format */ | |
312 | (errbuf, _("operand out of range (%ld not between %ld and %ld)"), | |
313 | value, minval, maxval); | |
314 | return errbuf; | |
315 | } | |
316 | } | |
317 | ||
318 | #if CGEN_INT_INSN_P | |
319 | ||
320 | if (total_length > 32) | |
321 | abort (); | |
322 | { | |
323 | int shift; | |
324 | ||
325 | if (CGEN_INSN_LSB0_P) | |
326 | shift = start; | |
327 | else | |
328 | shift = total_length - (start + length); | |
329 | *buffer = (*buffer & ~(mask << shift)) | ((value & mask) << shift); | |
330 | } | |
331 | ||
332 | #else | |
333 | ||
334 | /* FIXME: unfinished and untested */ | |
335 | ||
336 | /* ??? To be defined in <arch>-opc.h as necessary. */ | |
337 | #ifndef CGEN_WORD_ENDIAN | |
338 | #define CGEN_WORD_ENDIAN(od) CGEN_OPCODE_ENDIAN (od) | |
339 | #endif | |
340 | #ifndef CGEN_INSN_WORD_ENDIAN | |
341 | #define CGEN_INSN_WORD_ENDIAN(od) CGEN_WORD_ENDIAN (od) | |
342 | #endif | |
343 | ||
344 | /* The hard case is probably too slow for the normal cases. | |
345 | It's certainly more difficult to understand than the normal case. | |
346 | Thus this is split into two. Keep it that way. The hard case is defined | |
347 | to be when a field straddles a (loosely defined) word boundary | |
348 | (??? which may require target specific help to determine). */ | |
349 | ||
350 | #if 0 /*wip*/ | |
351 | ||
352 | #define HARD_CASE_P 0 /* FIXME:wip */ | |
353 | ||
354 | if (HARD_CASE_P) | |
355 | { | |
356 | unsigned char *bufp = (unsigned char *) buffer; | |
357 | int insn_length_left = total_length; | |
358 | ||
359 | if (CGEN_INSN_LSB0_P) | |
360 | { | |
361 | int word_offset = (CGEN_INSN_WORD_ENDIAN (od) == CGEN_ENDIAN_BIG | |
362 | ? ... | |
363 | : start / CGEN_BASE_INSN_BITSIZE); | |
364 | bufp += word_offset * (CGEN_BASE_INSN_BITSIZE / 8); | |
365 | if (CGEN_INSN_WORD_ENDIAN (od) == CGEN_ENDIAN_BIG) | |
366 | else | |
367 | start -= word_offset * CGEN_BASE_INSN_BITSIZE; | |
368 | } | |
369 | else | |
370 | { | |
371 | int word_offset = (CGEN_INSN_WORD_ENDIAN (od) == CGEN_ENDIAN_BIG | |
372 | ? start / CGEN_BASE_INSN_BITSIZE | |
373 | : ...); | |
374 | bufp += word_offset * (CGEN_BASE_INSN_BITSIZE / 8); | |
375 | if (CGEN_INSN_WORD_ENDIAN (od) == CGEN_ENDIAN_BIG) | |
376 | start -= word_offset * CGEN_BASE_INSN_BITSIZE; | |
377 | else | |
378 | } | |
379 | ||
380 | /* Loop so we handle a field straddling an insn word boundary | |
381 | (remember, "insn word boundary" is loosely defined here). */ | |
382 | ||
383 | while (length > 0) | |
384 | { | |
385 | int this_pass_length = length; | |
386 | int this_pass_start = start; | |
387 | int this_pass_word_length = min (insn_length_left, | |
388 | (CGEN_BASE_INSN_BITSIZE == 8 | |
389 | ? 32 | |
390 | : CGEN_BASE_INSN_BITSIZE)); | |
391 | ||
392 | insert_1 (od, value, attrs, | |
393 | this_pass_start, this_pass_length, this_pass_word_length, | |
394 | bufp); | |
395 | ||
396 | length -= this_pass_length; | |
397 | insn_length_left -= this_pass_word_length; | |
398 | if (???) | |
399 | { | |
400 | value >>= ???; | |
401 | start += ???; | |
402 | } | |
403 | else | |
404 | { | |
405 | value >>= ???; | |
406 | start += ???; | |
407 | } | |
408 | bufp += this_pass_word_length / 8; | |
409 | } | |
410 | } | |
411 | else | |
412 | #endif /* 0 */ | |
413 | { | |
414 | unsigned char *bufp = (unsigned char *) buffer; | |
415 | ||
416 | if (length > 32) | |
417 | abort (); | |
418 | ||
419 | /* Adjust start,total_length,bufp to point to the pseudo-word that holds | |
420 | the value. For example in a 48 bit insn where the value to insert | |
421 | (say an immediate value) is the last 16 bits then word_length here | |
422 | would be 16. To handle a 24 bit insn with an 18 bit immediate, | |
423 | insert_1 handles 24 bits (using a combination of bfd_get8,16). */ | |
424 | ||
425 | if (total_length > 32) | |
426 | { | |
427 | int needed_width = start % 8 + length; | |
428 | int fetch_length = (needed_width <= 8 ? 8 | |
429 | : needed_width <= 16 ? 16 | |
430 | : 32); | |
431 | ||
432 | if (CGEN_INSN_LSB0_P) | |
433 | { | |
434 | if (CGEN_INSN_WORD_ENDIAN (od) == CGEN_ENDIAN_BIG) | |
435 | { | |
436 | abort (); /* wip */ | |
437 | } | |
438 | else | |
439 | { | |
440 | int offset = start & ~7; | |
441 | ||
442 | bufp += offset / 8; | |
443 | start -= offset; | |
444 | total_length -= offset; | |
445 | } | |
446 | } | |
447 | else | |
448 | { | |
449 | if (CGEN_INSN_WORD_ENDIAN (od) == CGEN_ENDIAN_BIG) | |
450 | { | |
451 | int offset = start & ~7; | |
452 | ||
453 | bufp += offset / 8; | |
454 | start -= offset; | |
455 | total_length -= offset; | |
456 | } | |
457 | else | |
458 | { | |
459 | abort (); /* wip */ | |
460 | } | |
461 | } | |
462 | } | |
463 | ||
464 | insert_1 (od, value, start, length, total_length, bufp); | |
465 | } | |
466 | ||
467 | #endif /* ! CGEN_INT_INSN_P */ | |
468 | ||
469 | return NULL; | |
470 | } | |
471 | \f | |
472 | /* Default insn parser. | |
473 | ||
474 | The syntax string is scanned and operands are parsed and stored in FIELDS. | |
475 | Relocs are queued as we go via other callbacks. | |
476 | ||
477 | ??? Note that this is currently an all-or-nothing parser. If we fail to | |
478 | parse the instruction, we return 0 and the caller will start over from | |
479 | the beginning. Backtracking will be necessary in parsing subexpressions, | |
480 | but that can be handled there. Not handling backtracking here may get | |
481 | expensive in the case of the m68k. Deal with later. | |
482 | ||
483 | Returns NULL for success, an error message for failure. | |
484 | */ | |
485 | ||
486 | static const char * | |
487 | parse_insn_normal (od, insn, strp, fields) | |
488 | CGEN_OPCODE_DESC od; | |
489 | const CGEN_INSN * insn; | |
490 | const char ** strp; | |
491 | CGEN_FIELDS * fields; | |
492 | { | |
493 | const CGEN_SYNTAX * syntax = CGEN_INSN_SYNTAX (insn); | |
494 | const char * str = *strp; | |
495 | const char * errmsg; | |
496 | const char * p; | |
497 | const unsigned char * syn; | |
498 | #ifdef CGEN_MNEMONIC_OPERANDS | |
499 | /* FIXME: wip */ | |
500 | int past_opcode_p; | |
501 | #endif | |
502 | ||
503 | /* For now we assume the mnemonic is first (there are no leading operands). | |
504 | We can parse it without needing to set up operand parsing. | |
505 | GAS's input scrubber will ensure mnemonics are lowercase, but we may | |
506 | not be called from GAS. */ | |
507 | p = CGEN_INSN_MNEMONIC (insn); | |
508 | while (*p && tolower (*p) == tolower (*str)) | |
509 | ++p, ++str; | |
510 | ||
511 | if (* p || (* str && !isspace (* str))) | |
512 | return _("unrecognized instruction"); | |
513 | ||
514 | CGEN_INIT_PARSE (od); | |
515 | cgen_init_parse_operand (od); | |
516 | #ifdef CGEN_MNEMONIC_OPERANDS | |
517 | past_opcode_p = 0; | |
518 | #endif | |
519 | ||
520 | /* We don't check for (*str != '\0') here because we want to parse | |
521 | any trailing fake arguments in the syntax string. */ | |
522 | syn = CGEN_SYNTAX_STRING (syntax); | |
523 | ||
524 | /* Mnemonics come first for now, ensure valid string. */ | |
525 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) | |
526 | abort (); | |
527 | ||
528 | ++syn; | |
529 | ||
530 | while (* syn != 0) | |
531 | { | |
532 | /* Non operand chars must match exactly. */ | |
533 | if (CGEN_SYNTAX_CHAR_P (* syn)) | |
534 | { | |
535 | if (*str == CGEN_SYNTAX_CHAR (* syn)) | |
536 | { | |
537 | #ifdef CGEN_MNEMONIC_OPERANDS | |
538 | if (* syn == ' ') | |
539 | past_opcode_p = 1; | |
540 | #endif | |
541 | ++ syn; | |
542 | ++ str; | |
543 | } | |
544 | else | |
545 | { | |
546 | /* Syntax char didn't match. Can't be this insn. */ | |
547 | /* FIXME: would like to return something like | |
548 | "expected char `c'" */ | |
549 | return _("syntax error"); | |
550 | } | |
551 | continue; | |
552 | } | |
553 | ||
554 | /* We have an operand of some sort. */ | |
555 | errmsg = fr30_cgen_parse_operand (od, CGEN_SYNTAX_FIELD (*syn), | |
556 | &str, fields); | |
557 | if (errmsg) | |
558 | return errmsg; | |
559 | ||
560 | /* Done with this operand, continue with next one. */ | |
561 | ++ syn; | |
562 | } | |
563 | ||
564 | /* If we're at the end of the syntax string, we're done. */ | |
565 | if (* syn == '\0') | |
566 | { | |
567 | /* FIXME: For the moment we assume a valid `str' can only contain | |
568 | blanks now. IE: We needn't try again with a longer version of | |
569 | the insn and it is assumed that longer versions of insns appear | |
570 | before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */ | |
571 | while (isspace (* str)) | |
572 | ++ str; | |
573 | ||
574 | if (* str != '\0') | |
575 | return _("junk at end of line"); /* FIXME: would like to include `str' */ | |
576 | ||
577 | return NULL; | |
578 | } | |
579 | ||
580 | /* We couldn't parse it. */ | |
581 | return _("unrecognized instruction"); | |
582 | } | |
583 | ||
584 | /* Default insn builder (insert handler). | |
585 | The instruction is recorded in CGEN_INT_INSN_P byte order | |
586 | (meaning that if CGEN_INT_INSN_P BUFFER is an int * and thus the value is | |
587 | recorded in host byte order, otherwise BUFFER is an array of bytes and the | |
588 | value is recorded in target byte order). | |
589 | The result is an error message or NULL if success. */ | |
590 | ||
591 | static const char * | |
592 | insert_insn_normal (od, insn, fields, buffer, pc) | |
593 | CGEN_OPCODE_DESC od; | |
594 | const CGEN_INSN * insn; | |
595 | CGEN_FIELDS * fields; | |
596 | CGEN_INSN_BYTES_PTR buffer; | |
597 | bfd_vma pc; | |
598 | { | |
599 | const CGEN_SYNTAX * syntax = CGEN_INSN_SYNTAX (insn); | |
600 | unsigned long value; | |
601 | const unsigned char * syn; | |
602 | ||
603 | CGEN_INIT_INSERT (od); | |
604 | value = CGEN_INSN_VALUE (insn); | |
605 | ||
606 | /* If we're recording insns as numbers (rather than a string of bytes), | |
607 | target byte order handling is deferred until later. */ | |
608 | ||
609 | #if CGEN_INT_INSN_P | |
610 | ||
611 | *buffer = value; | |
612 | ||
613 | #else | |
614 | ||
615 | cgen_insn_put_value (od, buffer, min (CGEN_BASE_INSN_BITSIZE, | |
616 | CGEN_FIELDS_BITSIZE (fields)), | |
617 | value); | |
618 | ||
619 | #endif /* ! CGEN_INT_INSN_P */ | |
620 | ||
621 | /* ??? Rather than scanning the syntax string again, we could store | |
622 | in `fields' a null terminated list of the fields that are present. */ | |
623 | ||
624 | for (syn = CGEN_SYNTAX_STRING (syntax); * syn != '\0'; ++ syn) | |
625 | { | |
626 | const char *errmsg; | |
627 | ||
628 | if (CGEN_SYNTAX_CHAR_P (* syn)) | |
629 | continue; | |
630 | ||
631 | errmsg = fr30_cgen_insert_operand (od, CGEN_SYNTAX_FIELD (*syn), | |
632 | fields, buffer, pc); | |
633 | if (errmsg) | |
634 | return errmsg; | |
635 | } | |
636 | ||
637 | return NULL; | |
638 | } | |
639 | \f | |
640 | /* Main entry point. | |
641 | This routine is called for each instruction to be assembled. | |
642 | STR points to the insn to be assembled. | |
643 | We assume all necessary tables have been initialized. | |
644 | The assembled instruction, less any fixups, is stored in BUF. | |
645 | Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value | |
646 | still needs to be converted to target byte order, otherwise BUF is an array | |
647 | of bytes in target byte order. | |
648 | The result is a pointer to the insn's entry in the opcode table, | |
649 | or NULL if an error occured (an error message will have already been | |
650 | printed). | |
651 | ||
652 | Note that when processing (non-alias) macro-insns, | |
653 | this function recurses. */ | |
654 | ||
655 | const CGEN_INSN * | |
656 | fr30_cgen_assemble_insn (od, str, fields, buf, errmsg) | |
657 | CGEN_OPCODE_DESC od; | |
658 | const char * str; | |
659 | CGEN_FIELDS * fields; | |
660 | CGEN_INSN_BYTES_PTR buf; | |
661 | char ** errmsg; | |
662 | { | |
663 | const char * start; | |
664 | CGEN_INSN_LIST * ilist; | |
665 | ||
666 | /* Skip leading white space. */ | |
667 | while (isspace (* str)) | |
668 | ++ str; | |
669 | ||
670 | /* The instructions are stored in hashed lists. | |
671 | Get the first in the list. */ | |
672 | ilist = CGEN_ASM_LOOKUP_INSN (od, str); | |
673 | ||
674 | /* Keep looking until we find a match. */ | |
675 | ||
676 | start = str; | |
677 | for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist)) | |
678 | { | |
679 | const CGEN_INSN *insn = ilist->insn; | |
680 | ||
681 | #if 0 /* not needed as unsupported opcodes shouldn't be in the hash lists */ | |
682 | /* Is this insn supported by the selected cpu? */ | |
683 | if (! fr30_cgen_insn_supported (od, insn)) | |
684 | continue; | |
685 | #endif | |
686 | ||
687 | /* If the RELAX attribute is set, this is an insn that shouldn't be | |
688 | chosen immediately. Instead, it is used during assembler/linker | |
689 | relaxation if possible. */ | |
690 | if (CGEN_INSN_ATTR (insn, CGEN_INSN_RELAX) != 0) | |
691 | continue; | |
692 | ||
693 | str = start; | |
694 | ||
695 | /* Record a default length for the insn. This will get set to the | |
696 | correct value while parsing. */ | |
697 | /* FIXME: wip */ | |
698 | CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn); | |
699 | ||
700 | if (! CGEN_PARSE_FN (insn) (od, insn, & str, fields)) | |
701 | { | |
702 | /* ??? 0 is passed for `pc' */ | |
703 | if (CGEN_INSERT_FN (insn) (od, insn, fields, buf, (bfd_vma) 0) != NULL) | |
704 | continue; | |
705 | /* It is up to the caller to actually output the insn and any | |
706 | queued relocs. */ | |
707 | return insn; | |
708 | } | |
709 | ||
710 | /* Try the next entry. */ | |
711 | } | |
712 | ||
713 | /* FIXME: We can return a better error message than this. | |
714 | Need to track why it failed and pick the right one. */ | |
715 | { | |
716 | static char errbuf[100]; | |
717 | if (strlen (start) > 50) | |
718 | /* xgettext:c-format */ | |
719 | sprintf (errbuf, _("bad instruction `%.50s...'"), start); | |
720 | else | |
721 | /* xgettext:c-format */ | |
722 | sprintf (errbuf, _("bad instruction `%.50s'"), start); | |
723 | ||
724 | *errmsg = errbuf; | |
725 | return NULL; | |
726 | } | |
727 | } | |
728 | \f | |
729 | #if 0 /* This calls back to GAS which we can't do without care. */ | |
730 | ||
731 | /* Record each member of OPVALS in the assembler's symbol table. | |
732 | This lets GAS parse registers for us. | |
733 | ??? Interesting idea but not currently used. */ | |
734 | ||
735 | /* Record each member of OPVALS in the assembler's symbol table. | |
736 | FIXME: Not currently used. */ | |
737 | ||
738 | void | |
739 | fr30_cgen_asm_hash_keywords (od, opvals) | |
740 | CGEN_OPCODE_DESC od; | |
741 | CGEN_KEYWORD * opvals; | |
742 | { | |
743 | CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL); | |
744 | const CGEN_KEYWORD_ENTRY * ke; | |
745 | ||
746 | while ((ke = cgen_keyword_search_next (& search)) != NULL) | |
747 | { | |
748 | #if 0 /* Unnecessary, should be done in the search routine. */ | |
749 | if (! fr30_cgen_opval_supported (ke)) | |
750 | continue; | |
751 | #endif | |
752 | cgen_asm_record_register (od, ke->name, ke->value); | |
753 | } | |
754 | } | |
755 | ||
756 | #endif /* 0 */ |