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