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