Commit | Line | Data |
---|---|---|
87e6d782 NC |
1 | /* Assembler interface for targets using CGEN. -*- C -*- |
2 | CGEN: Cpu tools GENerator | |
3 | ||
4 | THIS FILE IS MACHINE GENERATED WITH CGEN. | |
5 | - the resultant file is machine generated, cgen-asm.in isn't | |
6 | ||
7 | Copyright 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. | |
8 | ||
9 | This file is part of the GNU Binutils and GDB, the GNU debugger. | |
10 | ||
11 | This program is free software; you can redistribute it and/or modify | |
12 | it under the terms of the GNU General Public License as published by | |
13 | the Free Software Foundation; either version 2, or (at your option) | |
14 | any later version. | |
15 | ||
16 | This program is distributed in the hope that it will be useful, | |
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | GNU General Public License for more details. | |
20 | ||
21 | You should have received a copy of the GNU General Public License | |
22 | along with this program; if not, write to the Free Software Foundation, Inc., | |
23 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
24 | ||
25 | /* ??? Eventually more and more of this stuff can go to cpu-independent files. | |
26 | Keep that in mind. */ | |
27 | ||
28 | #include "sysdep.h" | |
0e2ee3ca | 29 | #include <ctype.h> |
87e6d782 NC |
30 | #include <stdio.h> |
31 | #include "ansidecl.h" | |
32 | #include "bfd.h" | |
33 | #include "symcat.h" | |
34 | #include "openrisc-desc.h" | |
35 | #include "openrisc-opc.h" | |
36 | #include "opintl.h" | |
fc7bc883 | 37 | #include "xregex.h" |
fc05c67f | 38 | #include "libiberty.h" |
87e6d782 NC |
39 | |
40 | #undef min | |
41 | #define min(a,b) ((a) < (b) ? (a) : (b)) | |
42 | #undef max | |
43 | #define max(a,b) ((a) > (b) ? (a) : (b)) | |
44 | ||
45 | static const char * parse_insn_normal | |
46 | PARAMS ((CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *)); | |
47 | \f | |
48 | /* -- assembler routines inserted here */ | |
49 | ||
50 | /* -- asm.c */ | |
51 | ||
52 | #define CGEN_VERBOSE_ASSEMBLER_ERRORS | |
53 | ||
0e2ee3ca NC |
54 | static const char * parse_hi16 |
55 | PARAMS ((CGEN_CPU_DESC, const char **, int, unsigned long *)); | |
56 | static const char * parse_lo16 | |
57 | PARAMS ((CGEN_CPU_DESC, const char **, int, unsigned long *)); | |
58 | ||
87e6d782 NC |
59 | long |
60 | openrisc_sign_extend_16bit (value) | |
61 | long value; | |
62 | { | |
63 | return (long) (short) value; | |
64 | } | |
65 | ||
87e6d782 NC |
66 | /* Handle hi(). */ |
67 | ||
68 | static const char * | |
69 | parse_hi16 (cd, strp, opindex, valuep) | |
70 | CGEN_CPU_DESC cd; | |
71 | const char **strp; | |
72 | int opindex; | |
73 | unsigned long *valuep; | |
74 | { | |
75 | const char *errmsg; | |
76 | enum cgen_parse_operand_result result_type; | |
77 | bfd_vma value; | |
78 | ||
79 | if (**strp == '#') | |
80 | ++*strp; | |
81 | ||
82 | if (strncasecmp (*strp, "hi(", 3) == 0) | |
83 | { | |
84 | *strp += 3; | |
85 | ||
86 | #if 0 | |
87 | errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep); | |
88 | if (errmsg != NULL) | |
89 | fprintf (stderr, "parse_hi: %s\n", errmsg); | |
90 | if (errmsg != NULL) | |
91 | #endif | |
92 | errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_HI16, | |
93 | &result_type, &value); | |
94 | if (**strp != ')') | |
95 | return "missing `)'"; | |
96 | ++*strp; | |
97 | if (errmsg == NULL | |
98 | && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) | |
99 | value >>= 16; | |
100 | *valuep = (long) (short) value; | |
101 | ||
102 | return errmsg; | |
103 | } | |
104 | else | |
105 | { | |
106 | if (**strp == '-') | |
107 | errmsg = cgen_parse_signed_integer (cd, strp, opindex, (long *) &value); | |
108 | else | |
2c8721ec | 109 | errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, (unsigned long *) &value); |
87e6d782 NC |
110 | } |
111 | *valuep = (long) (short) (value & 0xffff); | |
112 | return errmsg; | |
113 | } | |
114 | ||
0e2ee3ca | 115 | /* Handle lo(). */ |
87e6d782 NC |
116 | |
117 | static const char * | |
118 | parse_lo16 (cd, strp, opindex, valuep) | |
119 | CGEN_CPU_DESC cd; | |
120 | const char **strp; | |
121 | int opindex; | |
122 | unsigned long *valuep; | |
123 | { | |
124 | const char *errmsg; | |
125 | enum cgen_parse_operand_result result_type; | |
126 | bfd_vma value; | |
127 | ||
128 | if (**strp == '#') | |
129 | ++*strp; | |
130 | ||
131 | if (strncasecmp (*strp, "lo(", 3) == 0) | |
132 | { | |
133 | *strp += 3; | |
134 | ||
135 | #if 0 | |
136 | errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep); | |
137 | if (errmsg != NULL) | |
138 | fprintf (stderr, "parse_lo: %s\n", errmsg); | |
139 | ||
140 | if (errmsg != NULL) | |
141 | #endif | |
142 | errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LO16, | |
143 | &result_type, &value); | |
144 | if (**strp != ')') | |
145 | return "missing `)'"; | |
146 | ++*strp; | |
147 | if (errmsg == NULL | |
148 | && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) | |
149 | value &= 0xffff; | |
150 | *valuep = (long) (short) value; | |
151 | ||
152 | return errmsg; | |
153 | } | |
154 | ||
155 | if (**strp == '-') | |
156 | errmsg = cgen_parse_signed_integer (cd, strp, opindex, (long *) &value); | |
157 | else | |
2c8721ec | 158 | errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, (unsigned long *) &value); |
87e6d782 NC |
159 | *valuep = (long) (short) (value & 0xffff); |
160 | return errmsg; | |
161 | } | |
162 | ||
163 | /* -- */ | |
164 | ||
0e2ee3ca NC |
165 | const char * openrisc_cgen_parse_operand |
166 | PARAMS ((CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *)); | |
167 | ||
87e6d782 NC |
168 | /* Main entry point for operand parsing. |
169 | ||
170 | This function is basically just a big switch statement. Earlier versions | |
171 | used tables to look up the function to use, but | |
172 | - if the table contains both assembler and disassembler functions then | |
173 | the disassembler contains much of the assembler and vice-versa, | |
174 | - there's a lot of inlining possibilities as things grow, | |
175 | - using a switch statement avoids the function call overhead. | |
176 | ||
177 | This function could be moved into `parse_insn_normal', but keeping it | |
178 | separate makes clear the interface between `parse_insn_normal' and each of | |
179 | the handlers. | |
180 | */ | |
181 | ||
182 | const char * | |
183 | openrisc_cgen_parse_operand (cd, opindex, strp, fields) | |
184 | CGEN_CPU_DESC cd; | |
185 | int opindex; | |
186 | const char ** strp; | |
187 | CGEN_FIELDS * fields; | |
188 | { | |
189 | const char * errmsg = NULL; | |
190 | /* Used by scalar operands that still need to be parsed. */ | |
fc05c67f | 191 | long junk ATTRIBUTE_UNUSED; |
87e6d782 NC |
192 | |
193 | switch (opindex) | |
194 | { | |
195 | case OPENRISC_OPERAND_ABS_26 : | |
196 | { | |
197 | bfd_vma value; | |
198 | errmsg = cgen_parse_address (cd, strp, OPENRISC_OPERAND_ABS_26, 0, NULL, & value); | |
199 | fields->f_abs26 = value; | |
200 | } | |
201 | break; | |
202 | case OPENRISC_OPERAND_DISP_26 : | |
203 | { | |
204 | bfd_vma value; | |
205 | errmsg = cgen_parse_address (cd, strp, OPENRISC_OPERAND_DISP_26, 0, NULL, & value); | |
206 | fields->f_disp26 = value; | |
207 | } | |
208 | break; | |
209 | case OPENRISC_OPERAND_HI16 : | |
210 | errmsg = parse_hi16 (cd, strp, OPENRISC_OPERAND_HI16, &fields->f_simm16); | |
211 | break; | |
212 | case OPENRISC_OPERAND_LO16 : | |
213 | errmsg = parse_lo16 (cd, strp, OPENRISC_OPERAND_LO16, &fields->f_lo16); | |
214 | break; | |
215 | case OPENRISC_OPERAND_OP_F_23 : | |
216 | errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_OP_F_23, &fields->f_op4); | |
217 | break; | |
218 | case OPENRISC_OPERAND_OP_F_3 : | |
219 | errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_OP_F_3, &fields->f_op5); | |
220 | break; | |
221 | case OPENRISC_OPERAND_RA : | |
222 | errmsg = cgen_parse_keyword (cd, strp, & openrisc_cgen_opval_h_gr, & fields->f_r2); | |
223 | break; | |
224 | case OPENRISC_OPERAND_RB : | |
225 | errmsg = cgen_parse_keyword (cd, strp, & openrisc_cgen_opval_h_gr, & fields->f_r3); | |
226 | break; | |
227 | case OPENRISC_OPERAND_RD : | |
228 | errmsg = cgen_parse_keyword (cd, strp, & openrisc_cgen_opval_h_gr, & fields->f_r1); | |
229 | break; | |
230 | case OPENRISC_OPERAND_SIMM_16 : | |
231 | errmsg = cgen_parse_signed_integer (cd, strp, OPENRISC_OPERAND_SIMM_16, &fields->f_simm16); | |
232 | break; | |
233 | case OPENRISC_OPERAND_UI16NC : | |
234 | errmsg = parse_lo16 (cd, strp, OPENRISC_OPERAND_UI16NC, &fields->f_i16nc); | |
235 | break; | |
236 | case OPENRISC_OPERAND_UIMM_16 : | |
237 | errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_UIMM_16, &fields->f_uimm16); | |
238 | break; | |
239 | case OPENRISC_OPERAND_UIMM_5 : | |
240 | errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_UIMM_5, &fields->f_uimm5); | |
241 | break; | |
242 | ||
243 | default : | |
244 | /* xgettext:c-format */ | |
245 | fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex); | |
246 | abort (); | |
247 | } | |
248 | ||
249 | return errmsg; | |
250 | } | |
251 | ||
252 | cgen_parse_fn * const openrisc_cgen_parse_handlers[] = | |
253 | { | |
254 | parse_insn_normal, | |
255 | }; | |
256 | ||
257 | void | |
258 | openrisc_cgen_init_asm (cd) | |
259 | CGEN_CPU_DESC cd; | |
260 | { | |
261 | openrisc_cgen_init_opcode_table (cd); | |
262 | openrisc_cgen_init_ibld_table (cd); | |
263 | cd->parse_handlers = & openrisc_cgen_parse_handlers[0]; | |
264 | cd->parse_operand = openrisc_cgen_parse_operand; | |
265 | } | |
266 | ||
fc7bc883 RH |
267 | \f |
268 | ||
269 | /* | |
270 | Regex construction routine. | |
271 | ||
272 | This translates an opcode syntax string into a regex string, | |
273 | by replacing any non-character syntax element (such as an | |
274 | opcode) with the pattern '.*' | |
275 | ||
276 | It then compiles the regex and stores it in the opcode, for | |
277 | later use by openrisc_cgen_assemble_insn | |
278 | ||
f3a55c17 | 279 | Returns NULL for success, an error message for failure. */ |
fc7bc883 RH |
280 | |
281 | char * | |
282 | openrisc_cgen_build_insn_regex (insn) | |
283 | CGEN_INSN *insn; | |
284 | { | |
fc05c67f | 285 | CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn); |
fc7bc883 RH |
286 | const char *mnem = CGEN_INSN_MNEMONIC (insn); |
287 | int mnem_len; | |
288 | char rxbuf[CGEN_MAX_RX_ELEMENTS]; | |
289 | char *rx = rxbuf; | |
290 | const CGEN_SYNTAX_CHAR_TYPE *syn; | |
291 | int reg_err; | |
292 | ||
293 | syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc)); | |
294 | ||
f3a55c17 NC |
295 | /* Mnemonics come first in the syntax string. */ |
296 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) | |
297 | return _("missing mnemonic in syntax string"); | |
fc7bc883 RH |
298 | ++syn; |
299 | ||
f3a55c17 NC |
300 | /* Generate a case sensitive regular expression that emulates case |
301 | insensitive matching in the "C" locale. We cannot generate a case | |
302 | insensitive regular expression because in Turkish locales, 'i' and 'I' | |
303 | are not equal modulo case conversion. */ | |
fc7bc883 | 304 | |
f3a55c17 NC |
305 | /* Copy the literal mnemonic out of the insn. */ |
306 | for (; *mnem; mnem++) | |
307 | { | |
308 | char c = *mnem; | |
309 | ||
310 | if (ISALPHA (c)) | |
311 | { | |
312 | *rx++ = '['; | |
313 | *rx++ = TOLOWER (c); | |
314 | *rx++ = TOUPPER (c); | |
315 | *rx++ = ']'; | |
316 | } | |
317 | else | |
318 | *rx++ = c; | |
319 | } | |
320 | ||
321 | /* Copy any remaining literals from the syntax string into the rx. */ | |
322 | for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn) | |
fc7bc883 RH |
323 | { |
324 | if (CGEN_SYNTAX_CHAR_P (* syn)) | |
325 | { | |
f3a55c17 NC |
326 | char c = CGEN_SYNTAX_CHAR (* syn); |
327 | ||
328 | switch (c) | |
329 | { | |
330 | /* Escape any regex metacharacters in the syntax. */ | |
331 | case '.': case '[': case '\\': | |
332 | case '*': case '^': case '$': | |
fc7bc883 RH |
333 | |
334 | #ifdef CGEN_ESCAPE_EXTENDED_REGEX | |
f3a55c17 NC |
335 | case '?': case '{': case '}': |
336 | case '(': case ')': case '*': | |
337 | case '|': case '+': case ']': | |
fc7bc883 | 338 | #endif |
f3a55c17 NC |
339 | *rx++ = '\\'; |
340 | *rx++ = c; | |
341 | break; | |
342 | ||
343 | default: | |
344 | if (ISALPHA (c)) | |
345 | { | |
346 | *rx++ = '['; | |
347 | *rx++ = TOLOWER (c); | |
348 | *rx++ = TOUPPER (c); | |
349 | *rx++ = ']'; | |
350 | } | |
351 | else | |
352 | *rx++ = c; | |
353 | break; | |
354 | } | |
fc7bc883 | 355 | |
f3a55c17 NC |
356 | /* Insert syntax char into rx. */ |
357 | *rx++ = c; | |
fc7bc883 RH |
358 | } |
359 | else | |
360 | { | |
f3a55c17 NC |
361 | /* Replace non-syntax fields with globs. */ |
362 | *rx++ = '.'; | |
363 | *rx++ = '*'; | |
fc7bc883 RH |
364 | } |
365 | } | |
366 | ||
f3a55c17 | 367 | /* Trailing whitespace ok. */ |
fc7bc883 RH |
368 | * rx++ = '['; |
369 | * rx++ = ' '; | |
370 | * rx++ = '\t'; | |
371 | * rx++ = ']'; | |
372 | * rx++ = '*'; | |
373 | ||
f3a55c17 | 374 | /* But anchor it after that. */ |
fc7bc883 RH |
375 | * rx++ = '$'; |
376 | * rx = '\0'; | |
377 | ||
378 | CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t)); | |
f3a55c17 | 379 | reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB); |
fc7bc883 RH |
380 | |
381 | if (reg_err == 0) | |
382 | return NULL; | |
383 | else | |
384 | { | |
385 | static char msg[80]; | |
f3a55c17 | 386 | |
fc7bc883 RH |
387 | regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80); |
388 | regfree ((regex_t *) CGEN_INSN_RX (insn)); | |
389 | free (CGEN_INSN_RX (insn)); | |
390 | (CGEN_INSN_RX (insn)) = NULL; | |
391 | return msg; | |
392 | } | |
393 | } | |
394 | ||
87e6d782 NC |
395 | \f |
396 | /* Default insn parser. | |
397 | ||
398 | The syntax string is scanned and operands are parsed and stored in FIELDS. | |
399 | Relocs are queued as we go via other callbacks. | |
400 | ||
401 | ??? Note that this is currently an all-or-nothing parser. If we fail to | |
402 | parse the instruction, we return 0 and the caller will start over from | |
403 | the beginning. Backtracking will be necessary in parsing subexpressions, | |
404 | but that can be handled there. Not handling backtracking here may get | |
405 | expensive in the case of the m68k. Deal with later. | |
406 | ||
f3a55c17 | 407 | Returns NULL for success, an error message for failure. */ |
87e6d782 NC |
408 | |
409 | static const char * | |
410 | parse_insn_normal (cd, insn, strp, fields) | |
411 | CGEN_CPU_DESC cd; | |
412 | const CGEN_INSN *insn; | |
413 | const char **strp; | |
414 | CGEN_FIELDS *fields; | |
415 | { | |
416 | /* ??? Runtime added insns not handled yet. */ | |
417 | const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn); | |
418 | const char *str = *strp; | |
419 | const char *errmsg; | |
420 | const char *p; | |
421 | const CGEN_SYNTAX_CHAR_TYPE * syn; | |
422 | #ifdef CGEN_MNEMONIC_OPERANDS | |
423 | /* FIXME: wip */ | |
424 | int past_opcode_p; | |
425 | #endif | |
426 | ||
427 | /* For now we assume the mnemonic is first (there are no leading operands). | |
428 | We can parse it without needing to set up operand parsing. | |
429 | GAS's input scrubber will ensure mnemonics are lowercase, but we may | |
430 | not be called from GAS. */ | |
431 | p = CGEN_INSN_MNEMONIC (insn); | |
0e2ee3ca | 432 | while (*p && tolower (*p) == tolower (*str)) |
87e6d782 NC |
433 | ++p, ++str; |
434 | ||
435 | if (* p) | |
436 | return _("unrecognized instruction"); | |
437 | ||
438 | #ifndef CGEN_MNEMONIC_OPERANDS | |
0e2ee3ca | 439 | if (* str && !isspace (* str)) |
87e6d782 NC |
440 | return _("unrecognized instruction"); |
441 | #endif | |
442 | ||
443 | CGEN_INIT_PARSE (cd); | |
444 | cgen_init_parse_operand (cd); | |
445 | #ifdef CGEN_MNEMONIC_OPERANDS | |
446 | past_opcode_p = 0; | |
447 | #endif | |
448 | ||
449 | /* We don't check for (*str != '\0') here because we want to parse | |
450 | any trailing fake arguments in the syntax string. */ | |
451 | syn = CGEN_SYNTAX_STRING (syntax); | |
452 | ||
453 | /* Mnemonics come first for now, ensure valid string. */ | |
454 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) | |
455 | abort (); | |
456 | ||
457 | ++syn; | |
458 | ||
459 | while (* syn != 0) | |
460 | { | |
461 | /* Non operand chars must match exactly. */ | |
462 | if (CGEN_SYNTAX_CHAR_P (* syn)) | |
463 | { | |
464 | /* FIXME: While we allow for non-GAS callers above, we assume the | |
465 | first char after the mnemonic part is a space. */ | |
466 | /* FIXME: We also take inappropriate advantage of the fact that | |
467 | GAS's input scrubber will remove extraneous blanks. */ | |
0e2ee3ca | 468 | if (tolower (*str) == tolower (CGEN_SYNTAX_CHAR (* syn))) |
87e6d782 NC |
469 | { |
470 | #ifdef CGEN_MNEMONIC_OPERANDS | |
471 | if (CGEN_SYNTAX_CHAR(* syn) == ' ') | |
472 | past_opcode_p = 1; | |
473 | #endif | |
474 | ++ syn; | |
475 | ++ str; | |
476 | } | |
477 | else if (*str) | |
478 | { | |
479 | /* Syntax char didn't match. Can't be this insn. */ | |
480 | static char msg [80]; | |
f3a55c17 | 481 | |
87e6d782 NC |
482 | /* xgettext:c-format */ |
483 | sprintf (msg, _("syntax error (expected char `%c', found `%c')"), | |
484 | CGEN_SYNTAX_CHAR(*syn), *str); | |
485 | return msg; | |
486 | } | |
487 | else | |
488 | { | |
489 | /* Ran out of input. */ | |
490 | static char msg [80]; | |
f3a55c17 | 491 | |
87e6d782 NC |
492 | /* xgettext:c-format */ |
493 | sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"), | |
494 | CGEN_SYNTAX_CHAR(*syn)); | |
495 | return msg; | |
496 | } | |
497 | continue; | |
498 | } | |
499 | ||
500 | /* We have an operand of some sort. */ | |
501 | errmsg = openrisc_cgen_parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), | |
502 | &str, fields); | |
503 | if (errmsg) | |
504 | return errmsg; | |
505 | ||
506 | /* Done with this operand, continue with next one. */ | |
507 | ++ syn; | |
508 | } | |
509 | ||
510 | /* If we're at the end of the syntax string, we're done. */ | |
511 | if (* syn == 0) | |
512 | { | |
513 | /* FIXME: For the moment we assume a valid `str' can only contain | |
514 | blanks now. IE: We needn't try again with a longer version of | |
515 | the insn and it is assumed that longer versions of insns appear | |
516 | before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */ | |
0e2ee3ca | 517 | while (isspace (* str)) |
87e6d782 NC |
518 | ++ str; |
519 | ||
520 | if (* str != '\0') | |
521 | return _("junk at end of line"); /* FIXME: would like to include `str' */ | |
522 | ||
523 | return NULL; | |
524 | } | |
525 | ||
526 | /* We couldn't parse it. */ | |
527 | return _("unrecognized instruction"); | |
528 | } | |
529 | \f | |
530 | /* Main entry point. | |
531 | This routine is called for each instruction to be assembled. | |
532 | STR points to the insn to be assembled. | |
533 | We assume all necessary tables have been initialized. | |
534 | The assembled instruction, less any fixups, is stored in BUF. | |
535 | Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value | |
536 | still needs to be converted to target byte order, otherwise BUF is an array | |
537 | of bytes in target byte order. | |
538 | The result is a pointer to the insn's entry in the opcode table, | |
539 | or NULL if an error occured (an error message will have already been | |
540 | printed). | |
541 | ||
542 | Note that when processing (non-alias) macro-insns, | |
543 | this function recurses. | |
544 | ||
545 | ??? It's possible to make this cpu-independent. | |
546 | One would have to deal with a few minor things. | |
547 | At this point in time doing so would be more of a curiosity than useful | |
548 | [for example this file isn't _that_ big], but keeping the possibility in | |
549 | mind helps keep the design clean. */ | |
550 | ||
551 | const CGEN_INSN * | |
552 | openrisc_cgen_assemble_insn (cd, str, fields, buf, errmsg) | |
553 | CGEN_CPU_DESC cd; | |
554 | const char *str; | |
555 | CGEN_FIELDS *fields; | |
556 | CGEN_INSN_BYTES_PTR buf; | |
557 | char **errmsg; | |
558 | { | |
559 | const char *start; | |
560 | CGEN_INSN_LIST *ilist; | |
561 | const char *parse_errmsg = NULL; | |
562 | const char *insert_errmsg = NULL; | |
fc7bc883 | 563 | int recognized_mnemonic = 0; |
87e6d782 NC |
564 | |
565 | /* Skip leading white space. */ | |
0e2ee3ca | 566 | while (isspace (* str)) |
87e6d782 NC |
567 | ++ str; |
568 | ||
569 | /* The instructions are stored in hashed lists. | |
570 | Get the first in the list. */ | |
571 | ilist = CGEN_ASM_LOOKUP_INSN (cd, str); | |
572 | ||
573 | /* Keep looking until we find a match. */ | |
87e6d782 NC |
574 | start = str; |
575 | for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist)) | |
576 | { | |
577 | const CGEN_INSN *insn = ilist->insn; | |
fc7bc883 | 578 | recognized_mnemonic = 1; |
87e6d782 NC |
579 | |
580 | #ifdef CGEN_VALIDATE_INSN_SUPPORTED | |
f3a55c17 NC |
581 | /* Not usually needed as unsupported opcodes |
582 | shouldn't be in the hash lists. */ | |
87e6d782 NC |
583 | /* Is this insn supported by the selected cpu? */ |
584 | if (! openrisc_cgen_insn_supported (cd, insn)) | |
585 | continue; | |
586 | #endif | |
87e6d782 NC |
587 | /* If the RELAX attribute is set, this is an insn that shouldn't be |
588 | chosen immediately. Instead, it is used during assembler/linker | |
589 | relaxation if possible. */ | |
590 | if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAX) != 0) | |
591 | continue; | |
592 | ||
593 | str = start; | |
594 | ||
f3a55c17 | 595 | /* Skip this insn if str doesn't look right lexically. */ |
fc7bc883 RH |
596 | if (CGEN_INSN_RX (insn) != NULL && |
597 | regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH) | |
598 | continue; | |
599 | ||
87e6d782 NC |
600 | /* Allow parse/insert handlers to obtain length of insn. */ |
601 | CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn); | |
602 | ||
603 | parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields); | |
604 | if (parse_errmsg != NULL) | |
605 | continue; | |
606 | ||
f3a55c17 | 607 | /* ??? 0 is passed for `pc'. */ |
87e6d782 NC |
608 | insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf, |
609 | (bfd_vma) 0); | |
610 | if (insert_errmsg != NULL) | |
611 | continue; | |
612 | ||
613 | /* It is up to the caller to actually output the insn and any | |
614 | queued relocs. */ | |
615 | return insn; | |
616 | } | |
617 | ||
618 | { | |
619 | static char errbuf[150]; | |
620 | #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS | |
621 | const char *tmp_errmsg; | |
622 | ||
623 | /* If requesting verbose error messages, use insert_errmsg. | |
f3a55c17 | 624 | Failing that, use parse_errmsg. */ |
87e6d782 NC |
625 | tmp_errmsg = (insert_errmsg ? insert_errmsg : |
626 | parse_errmsg ? parse_errmsg : | |
f3a55c17 NC |
627 | recognized_mnemonic ? |
628 | _("unrecognized form of instruction") : | |
87e6d782 NC |
629 | _("unrecognized instruction")); |
630 | ||
631 | if (strlen (start) > 50) | |
632 | /* xgettext:c-format */ | |
633 | sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start); | |
634 | else | |
635 | /* xgettext:c-format */ | |
636 | sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start); | |
637 | #else | |
638 | if (strlen (start) > 50) | |
639 | /* xgettext:c-format */ | |
640 | sprintf (errbuf, _("bad instruction `%.50s...'"), start); | |
641 | else | |
642 | /* xgettext:c-format */ | |
643 | sprintf (errbuf, _("bad instruction `%.50s'"), start); | |
644 | #endif | |
645 | ||
646 | *errmsg = errbuf; | |
647 | return NULL; | |
648 | } | |
649 | } | |
650 | \f | |
651 | #if 0 /* This calls back to GAS which we can't do without care. */ | |
652 | ||
653 | /* Record each member of OPVALS in the assembler's symbol table. | |
654 | This lets GAS parse registers for us. | |
655 | ??? Interesting idea but not currently used. */ | |
656 | ||
657 | /* Record each member of OPVALS in the assembler's symbol table. | |
658 | FIXME: Not currently used. */ | |
659 | ||
660 | void | |
661 | openrisc_cgen_asm_hash_keywords (cd, opvals) | |
662 | CGEN_CPU_DESC cd; | |
663 | CGEN_KEYWORD *opvals; | |
664 | { | |
665 | CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL); | |
666 | const CGEN_KEYWORD_ENTRY * ke; | |
667 | ||
668 | while ((ke = cgen_keyword_search_next (& search)) != NULL) | |
669 | { | |
670 | #if 0 /* Unnecessary, should be done in the search routine. */ | |
671 | if (! openrisc_cgen_opval_supported (ke)) | |
672 | continue; | |
673 | #endif | |
674 | cgen_asm_record_register (cd, ke->name, ke->value); | |
675 | } | |
676 | } | |
677 | ||
678 | #endif /* 0 */ |