]>
Commit | Line | Data |
---|---|---|
252b5132 RH |
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 | |
252b5132 | 6 | |
47b0e7ad NC |
7 | Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2005 |
8 | Free Software Foundation, Inc. | |
252b5132 | 9 | |
47b0e7ad | 10 | This file is part of the GNU Binutils and GDB, the GNU debugger. |
252b5132 | 11 | |
47b0e7ad NC |
12 | This program is free software; you can redistribute it and/or modify |
13 | it under the terms of the GNU General Public License as published by | |
14 | the Free Software Foundation; either version 2, or (at your option) | |
15 | any later version. | |
252b5132 | 16 | |
47b0e7ad NC |
17 | This program is distributed in the hope that it will be useful, |
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | GNU General Public License for more details. | |
252b5132 | 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. */ | |
252b5132 RH |
25 | |
26 | /* ??? Eventually more and more of this stuff can go to cpu-independent files. | |
27 | Keep that in mind. */ | |
28 | ||
29 | #include "sysdep.h" | |
252b5132 RH |
30 | #include <stdio.h> |
31 | #include "ansidecl.h" | |
32 | #include "bfd.h" | |
33 | #include "symcat.h" | |
34 | #include "fr30-desc.h" | |
35 | #include "fr30-opc.h" | |
36 | #include "opintl.h" | |
fc7bc883 | 37 | #include "xregex.h" |
d5b2f4d6 | 38 | #include "libiberty.h" |
37111cc7 | 39 | #include "safe-ctype.h" |
252b5132 | 40 | |
37111cc7 | 41 | #undef min |
252b5132 | 42 | #define min(a,b) ((a) < (b) ? (a) : (b)) |
37111cc7 | 43 | #undef max |
252b5132 RH |
44 | #define max(a,b) ((a) > (b) ? (a) : (b)) |
45 | ||
0e2ee3ca | 46 | static const char * parse_insn_normal |
ffead7ae | 47 | (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *); |
252b5132 | 48 | \f |
37111cc7 | 49 | /* -- assembler routines inserted here. */ |
252b5132 RH |
50 | |
51 | /* -- asm.c */ | |
0e2ee3ca NC |
52 | /* Handle register lists for LDMx and STMx. */ |
53 | ||
252b5132 | 54 | static int |
47b0e7ad | 55 | parse_register_number (const char **strp) |
252b5132 RH |
56 | { |
57 | int regno; | |
47b0e7ad | 58 | |
252b5132 | 59 | if (**strp < '0' || **strp > '9') |
47b0e7ad | 60 | return -1; /* Error. */ |
252b5132 RH |
61 | regno = **strp - '0'; |
62 | ++*strp; | |
63 | ||
64 | if (**strp >= '0' && **strp <= '9') | |
65 | { | |
66 | regno = regno * 10 + (**strp - '0'); | |
67 | ++*strp; | |
68 | } | |
69 | ||
70 | return regno; | |
71 | } | |
72 | ||
73 | static const char * | |
47b0e7ad NC |
74 | parse_register_list (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, |
75 | const char **strp, | |
76 | int opindex ATTRIBUTE_UNUSED, | |
77 | unsigned long *valuep, | |
78 | int high_low, /* 0 == high, 1 == low. */ | |
79 | int load_store) /* 0 == load, 1 == store. */ | |
252b5132 | 80 | { |
252b5132 RH |
81 | *valuep = 0; |
82 | while (**strp && **strp != ')') | |
83 | { | |
47b0e7ad NC |
84 | int regno; |
85 | ||
252b5132 RH |
86 | if (**strp != 'R' && **strp != 'r') |
87 | break; | |
88 | ++*strp; | |
89 | ||
90 | regno = parse_register_number (strp); | |
91 | if (regno == -1) | |
47b0e7ad | 92 | return _("Register number is not valid"); |
252b5132 | 93 | if (regno > 7 && !high_low) |
47b0e7ad | 94 | return _("Register must be between r0 and r7"); |
252b5132 | 95 | if (regno < 8 && high_low) |
47b0e7ad | 96 | return _("Register must be between r8 and r15"); |
252b5132 RH |
97 | |
98 | if (high_low) | |
99 | regno -= 8; | |
100 | ||
0e2ee3ca | 101 | if (load_store) /* Mask is reversed for store. */ |
252b5132 RH |
102 | *valuep |= 0x80 >> regno; |
103 | else | |
104 | *valuep |= 1 << regno; | |
105 | ||
106 | if (**strp == ',') | |
107 | { | |
108 | if (*(*strp + 1) == ')') | |
109 | break; | |
110 | ++*strp; | |
111 | } | |
112 | } | |
113 | ||
114 | if (!*strp || **strp != ')') | |
47b0e7ad | 115 | return _("Register list is not valid"); |
252b5132 RH |
116 | |
117 | return NULL; | |
118 | } | |
119 | ||
120 | static const char * | |
47b0e7ad NC |
121 | parse_low_register_list_ld (CGEN_CPU_DESC cd, |
122 | const char **strp, | |
123 | int opindex, | |
124 | unsigned long *valuep) | |
252b5132 | 125 | { |
47b0e7ad NC |
126 | return parse_register_list (cd, strp, opindex, valuep, |
127 | 0 /* Low. */, 0 /* Load. */); | |
252b5132 RH |
128 | } |
129 | ||
130 | static const char * | |
47b0e7ad NC |
131 | parse_hi_register_list_ld (CGEN_CPU_DESC cd, |
132 | const char **strp, | |
133 | int opindex, | |
134 | unsigned long *valuep) | |
252b5132 | 135 | { |
47b0e7ad NC |
136 | return parse_register_list (cd, strp, opindex, valuep, |
137 | 1 /* High. */, 0 /* Load. */); | |
252b5132 RH |
138 | } |
139 | ||
140 | static const char * | |
47b0e7ad NC |
141 | parse_low_register_list_st (CGEN_CPU_DESC cd, |
142 | const char **strp, | |
143 | int opindex, | |
144 | unsigned long *valuep) | |
252b5132 | 145 | { |
47b0e7ad NC |
146 | return parse_register_list (cd, strp, opindex, valuep, |
147 | 0 /* Low. */, 1 /* Store. */); | |
252b5132 RH |
148 | } |
149 | ||
150 | static const char * | |
47b0e7ad NC |
151 | parse_hi_register_list_st (CGEN_CPU_DESC cd, |
152 | const char **strp, | |
153 | int opindex, | |
154 | unsigned long *valuep) | |
252b5132 | 155 | { |
47b0e7ad NC |
156 | return parse_register_list (cd, strp, opindex, valuep, |
157 | 1 /* High. */, 1 /* Store. */); | |
252b5132 RH |
158 | } |
159 | ||
160 | /* -- */ | |
161 | ||
0e2ee3ca | 162 | const char * fr30_cgen_parse_operand |
47b0e7ad | 163 | (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *); |
0e2ee3ca | 164 | |
252b5132 RH |
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. */ |
252b5132 RH |
177 | |
178 | const char * | |
47b0e7ad NC |
179 | fr30_cgen_parse_operand (CGEN_CPU_DESC cd, |
180 | int opindex, | |
181 | const char ** strp, | |
182 | CGEN_FIELDS * fields) | |
252b5132 | 183 | { |
eb1b03df DE |
184 | const char * errmsg = NULL; |
185 | /* Used by scalar operands that still need to be parsed. */ | |
0e2ee3ca | 186 | long junk ATTRIBUTE_UNUSED; |
252b5132 RH |
187 | |
188 | switch (opindex) | |
189 | { | |
190 | case FR30_OPERAND_CRI : | |
191 | errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_cr_names, & fields->f_CRi); | |
192 | break; | |
193 | case FR30_OPERAND_CRJ : | |
194 | errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_cr_names, & fields->f_CRj); | |
195 | break; | |
196 | case FR30_OPERAND_R13 : | |
eb1b03df | 197 | errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r13, & junk); |
252b5132 RH |
198 | break; |
199 | case FR30_OPERAND_R14 : | |
eb1b03df | 200 | errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r14, & junk); |
252b5132 RH |
201 | break; |
202 | case FR30_OPERAND_R15 : | |
eb1b03df | 203 | errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r15, & junk); |
252b5132 RH |
204 | break; |
205 | case FR30_OPERAND_RI : | |
206 | errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Ri); | |
207 | break; | |
208 | case FR30_OPERAND_RIC : | |
209 | errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Ric); | |
210 | break; | |
211 | case FR30_OPERAND_RJ : | |
212 | errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Rj); | |
213 | break; | |
214 | case FR30_OPERAND_RJC : | |
215 | errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Rjc); | |
216 | break; | |
217 | case FR30_OPERAND_RS1 : | |
218 | errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_dr_names, & fields->f_Rs1); | |
219 | break; | |
220 | case FR30_OPERAND_RS2 : | |
221 | errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_dr_names, & fields->f_Rs2); | |
222 | break; | |
223 | case FR30_OPERAND_CC : | |
33b71eeb | 224 | errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_CC, (unsigned long *) (& fields->f_cc)); |
252b5132 RH |
225 | break; |
226 | case FR30_OPERAND_CCC : | |
33b71eeb | 227 | errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_CCC, (unsigned long *) (& fields->f_ccc)); |
252b5132 RH |
228 | break; |
229 | case FR30_OPERAND_DIR10 : | |
33b71eeb | 230 | errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR10, (unsigned long *) (& fields->f_dir10)); |
252b5132 RH |
231 | break; |
232 | case FR30_OPERAND_DIR8 : | |
33b71eeb | 233 | errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR8, (unsigned long *) (& fields->f_dir8)); |
252b5132 RH |
234 | break; |
235 | case FR30_OPERAND_DIR9 : | |
33b71eeb | 236 | errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR9, (unsigned long *) (& fields->f_dir9)); |
252b5132 RH |
237 | break; |
238 | case FR30_OPERAND_DISP10 : | |
33b71eeb | 239 | errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP10, (long *) (& fields->f_disp10)); |
252b5132 RH |
240 | break; |
241 | case FR30_OPERAND_DISP8 : | |
33b71eeb | 242 | errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP8, (long *) (& fields->f_disp8)); |
252b5132 RH |
243 | break; |
244 | case FR30_OPERAND_DISP9 : | |
33b71eeb | 245 | errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP9, (long *) (& fields->f_disp9)); |
252b5132 RH |
246 | break; |
247 | case FR30_OPERAND_I20 : | |
33b71eeb | 248 | errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I20, (unsigned long *) (& fields->f_i20)); |
252b5132 RH |
249 | break; |
250 | case FR30_OPERAND_I32 : | |
33b71eeb | 251 | errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I32, (unsigned long *) (& fields->f_i32)); |
252b5132 RH |
252 | break; |
253 | case FR30_OPERAND_I8 : | |
33b71eeb | 254 | errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I8, (unsigned long *) (& fields->f_i8)); |
252b5132 RH |
255 | break; |
256 | case FR30_OPERAND_LABEL12 : | |
257 | { | |
9494d739 | 258 | bfd_vma value = 0; |
252b5132 RH |
259 | errmsg = cgen_parse_address (cd, strp, FR30_OPERAND_LABEL12, 0, NULL, & value); |
260 | fields->f_rel12 = value; | |
261 | } | |
262 | break; | |
263 | case FR30_OPERAND_LABEL9 : | |
264 | { | |
9494d739 | 265 | bfd_vma value = 0; |
252b5132 RH |
266 | errmsg = cgen_parse_address (cd, strp, FR30_OPERAND_LABEL9, 0, NULL, & value); |
267 | fields->f_rel9 = value; | |
268 | } | |
269 | break; | |
270 | case FR30_OPERAND_M4 : | |
33b71eeb | 271 | errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_M4, (long *) (& fields->f_m4)); |
252b5132 RH |
272 | break; |
273 | case FR30_OPERAND_PS : | |
eb1b03df | 274 | errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_ps, & junk); |
252b5132 RH |
275 | break; |
276 | case FR30_OPERAND_REGLIST_HI_LD : | |
33b71eeb | 277 | errmsg = parse_hi_register_list_ld (cd, strp, FR30_OPERAND_REGLIST_HI_LD, (unsigned long *) (& fields->f_reglist_hi_ld)); |
252b5132 RH |
278 | break; |
279 | case FR30_OPERAND_REGLIST_HI_ST : | |
33b71eeb | 280 | errmsg = parse_hi_register_list_st (cd, strp, FR30_OPERAND_REGLIST_HI_ST, (unsigned long *) (& fields->f_reglist_hi_st)); |
252b5132 RH |
281 | break; |
282 | case FR30_OPERAND_REGLIST_LOW_LD : | |
33b71eeb | 283 | errmsg = parse_low_register_list_ld (cd, strp, FR30_OPERAND_REGLIST_LOW_LD, (unsigned long *) (& fields->f_reglist_low_ld)); |
252b5132 RH |
284 | break; |
285 | case FR30_OPERAND_REGLIST_LOW_ST : | |
33b71eeb | 286 | errmsg = parse_low_register_list_st (cd, strp, FR30_OPERAND_REGLIST_LOW_ST, (unsigned long *) (& fields->f_reglist_low_st)); |
252b5132 RH |
287 | break; |
288 | case FR30_OPERAND_S10 : | |
33b71eeb | 289 | errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_S10, (long *) (& fields->f_s10)); |
252b5132 RH |
290 | break; |
291 | case FR30_OPERAND_U10 : | |
33b71eeb | 292 | errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U10, (unsigned long *) (& fields->f_u10)); |
252b5132 RH |
293 | break; |
294 | case FR30_OPERAND_U4 : | |
33b71eeb | 295 | errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U4, (unsigned long *) (& fields->f_u4)); |
252b5132 RH |
296 | break; |
297 | case FR30_OPERAND_U4C : | |
33b71eeb | 298 | errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U4C, (unsigned long *) (& fields->f_u4c)); |
252b5132 RH |
299 | break; |
300 | case FR30_OPERAND_U8 : | |
33b71eeb | 301 | errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U8, (unsigned long *) (& fields->f_u8)); |
252b5132 RH |
302 | break; |
303 | case FR30_OPERAND_UDISP6 : | |
33b71eeb | 304 | errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_UDISP6, (unsigned long *) (& fields->f_udisp6)); |
252b5132 RH |
305 | break; |
306 | ||
307 | default : | |
308 | /* xgettext:c-format */ | |
309 | fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex); | |
310 | abort (); | |
311 | } | |
312 | ||
313 | return errmsg; | |
314 | } | |
315 | ||
316 | cgen_parse_fn * const fr30_cgen_parse_handlers[] = | |
317 | { | |
318 | parse_insn_normal, | |
319 | }; | |
320 | ||
321 | void | |
47b0e7ad | 322 | fr30_cgen_init_asm (CGEN_CPU_DESC cd) |
252b5132 RH |
323 | { |
324 | fr30_cgen_init_opcode_table (cd); | |
325 | fr30_cgen_init_ibld_table (cd); | |
326 | cd->parse_handlers = & fr30_cgen_parse_handlers[0]; | |
327 | cd->parse_operand = fr30_cgen_parse_operand; | |
1620f33d AM |
328 | #ifdef CGEN_ASM_INIT_HOOK |
329 | CGEN_ASM_INIT_HOOK | |
330 | #endif | |
252b5132 RH |
331 | } |
332 | ||
fc7bc883 RH |
333 | \f |
334 | ||
37111cc7 | 335 | /* Regex construction routine. |
fc7bc883 | 336 | |
37111cc7 NC |
337 | This translates an opcode syntax string into a regex string, |
338 | by replacing any non-character syntax element (such as an | |
339 | opcode) with the pattern '.*' | |
fc7bc883 | 340 | |
37111cc7 NC |
341 | It then compiles the regex and stores it in the opcode, for |
342 | later use by fr30_cgen_assemble_insn | |
fc7bc883 | 343 | |
37111cc7 | 344 | Returns NULL for success, an error message for failure. */ |
fc7bc883 RH |
345 | |
346 | char * | |
ffead7ae | 347 | fr30_cgen_build_insn_regex (CGEN_INSN *insn) |
fc7bc883 | 348 | { |
d5b2f4d6 | 349 | CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn); |
fc7bc883 | 350 | const char *mnem = CGEN_INSN_MNEMONIC (insn); |
fc7bc883 RH |
351 | char rxbuf[CGEN_MAX_RX_ELEMENTS]; |
352 | char *rx = rxbuf; | |
353 | const CGEN_SYNTAX_CHAR_TYPE *syn; | |
354 | int reg_err; | |
355 | ||
356 | syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc)); | |
357 | ||
f3a55c17 NC |
358 | /* Mnemonics come first in the syntax string. */ |
359 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) | |
360 | return _("missing mnemonic in syntax string"); | |
fc7bc883 RH |
361 | ++syn; |
362 | ||
f3a55c17 NC |
363 | /* Generate a case sensitive regular expression that emulates case |
364 | insensitive matching in the "C" locale. We cannot generate a case | |
365 | insensitive regular expression because in Turkish locales, 'i' and 'I' | |
366 | are not equal modulo case conversion. */ | |
fc7bc883 | 367 | |
f3a55c17 NC |
368 | /* Copy the literal mnemonic out of the insn. */ |
369 | for (; *mnem; mnem++) | |
370 | { | |
371 | char c = *mnem; | |
372 | ||
373 | if (ISALPHA (c)) | |
374 | { | |
375 | *rx++ = '['; | |
376 | *rx++ = TOLOWER (c); | |
377 | *rx++ = TOUPPER (c); | |
378 | *rx++ = ']'; | |
379 | } | |
380 | else | |
381 | *rx++ = c; | |
382 | } | |
383 | ||
384 | /* Copy any remaining literals from the syntax string into the rx. */ | |
385 | for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn) | |
fc7bc883 RH |
386 | { |
387 | if (CGEN_SYNTAX_CHAR_P (* syn)) | |
388 | { | |
f3a55c17 NC |
389 | char c = CGEN_SYNTAX_CHAR (* syn); |
390 | ||
391 | switch (c) | |
392 | { | |
393 | /* Escape any regex metacharacters in the syntax. */ | |
394 | case '.': case '[': case '\\': | |
395 | case '*': case '^': case '$': | |
fc7bc883 RH |
396 | |
397 | #ifdef CGEN_ESCAPE_EXTENDED_REGEX | |
f3a55c17 NC |
398 | case '?': case '{': case '}': |
399 | case '(': case ')': case '*': | |
400 | case '|': case '+': case ']': | |
fc7bc883 | 401 | #endif |
f3a55c17 NC |
402 | *rx++ = '\\'; |
403 | *rx++ = c; | |
404 | break; | |
405 | ||
406 | default: | |
407 | if (ISALPHA (c)) | |
408 | { | |
409 | *rx++ = '['; | |
410 | *rx++ = TOLOWER (c); | |
411 | *rx++ = TOUPPER (c); | |
412 | *rx++ = ']'; | |
413 | } | |
414 | else | |
415 | *rx++ = c; | |
416 | break; | |
417 | } | |
fc7bc883 RH |
418 | } |
419 | else | |
420 | { | |
f3a55c17 NC |
421 | /* Replace non-syntax fields with globs. */ |
422 | *rx++ = '.'; | |
423 | *rx++ = '*'; | |
fc7bc883 RH |
424 | } |
425 | } | |
426 | ||
f3a55c17 | 427 | /* Trailing whitespace ok. */ |
fc7bc883 RH |
428 | * rx++ = '['; |
429 | * rx++ = ' '; | |
430 | * rx++ = '\t'; | |
431 | * rx++ = ']'; | |
432 | * rx++ = '*'; | |
433 | ||
f3a55c17 | 434 | /* But anchor it after that. */ |
fc7bc883 RH |
435 | * rx++ = '$'; |
436 | * rx = '\0'; | |
437 | ||
438 | CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t)); | |
f3a55c17 | 439 | reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB); |
fc7bc883 RH |
440 | |
441 | if (reg_err == 0) | |
442 | return NULL; | |
443 | else | |
444 | { | |
445 | static char msg[80]; | |
f3a55c17 | 446 | |
fc7bc883 RH |
447 | regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80); |
448 | regfree ((regex_t *) CGEN_INSN_RX (insn)); | |
449 | free (CGEN_INSN_RX (insn)); | |
450 | (CGEN_INSN_RX (insn)) = NULL; | |
37111cc7 | 451 | return msg; |
fc7bc883 RH |
452 | } |
453 | } | |
454 | ||
252b5132 RH |
455 | \f |
456 | /* Default insn parser. | |
457 | ||
458 | The syntax string is scanned and operands are parsed and stored in FIELDS. | |
459 | Relocs are queued as we go via other callbacks. | |
460 | ||
461 | ??? Note that this is currently an all-or-nothing parser. If we fail to | |
462 | parse the instruction, we return 0 and the caller will start over from | |
463 | the beginning. Backtracking will be necessary in parsing subexpressions, | |
464 | but that can be handled there. Not handling backtracking here may get | |
465 | expensive in the case of the m68k. Deal with later. | |
466 | ||
f3a55c17 | 467 | Returns NULL for success, an error message for failure. */ |
252b5132 RH |
468 | |
469 | static const char * | |
ffead7ae MM |
470 | parse_insn_normal (CGEN_CPU_DESC cd, |
471 | const CGEN_INSN *insn, | |
472 | const char **strp, | |
473 | CGEN_FIELDS *fields) | |
252b5132 RH |
474 | { |
475 | /* ??? Runtime added insns not handled yet. */ | |
476 | const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn); | |
477 | const char *str = *strp; | |
478 | const char *errmsg; | |
479 | const char *p; | |
b3466c39 | 480 | const CGEN_SYNTAX_CHAR_TYPE * syn; |
252b5132 RH |
481 | #ifdef CGEN_MNEMONIC_OPERANDS |
482 | /* FIXME: wip */ | |
483 | int past_opcode_p; | |
484 | #endif | |
485 | ||
486 | /* For now we assume the mnemonic is first (there are no leading operands). | |
487 | We can parse it without needing to set up operand parsing. | |
488 | GAS's input scrubber will ensure mnemonics are lowercase, but we may | |
489 | not be called from GAS. */ | |
490 | p = CGEN_INSN_MNEMONIC (insn); | |
37111cc7 | 491 | while (*p && TOLOWER (*p) == TOLOWER (*str)) |
252b5132 | 492 | ++p, ++str; |
1fa60b5d DE |
493 | |
494 | if (* p) | |
495 | return _("unrecognized instruction"); | |
496 | ||
497 | #ifndef CGEN_MNEMONIC_OPERANDS | |
37111cc7 | 498 | if (* str && ! ISSPACE (* str)) |
252b5132 | 499 | return _("unrecognized instruction"); |
1fa60b5d | 500 | #endif |
252b5132 RH |
501 | |
502 | CGEN_INIT_PARSE (cd); | |
503 | cgen_init_parse_operand (cd); | |
504 | #ifdef CGEN_MNEMONIC_OPERANDS | |
505 | past_opcode_p = 0; | |
506 | #endif | |
507 | ||
508 | /* We don't check for (*str != '\0') here because we want to parse | |
509 | any trailing fake arguments in the syntax string. */ | |
510 | syn = CGEN_SYNTAX_STRING (syntax); | |
511 | ||
512 | /* Mnemonics come first for now, ensure valid string. */ | |
513 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) | |
514 | abort (); | |
515 | ||
516 | ++syn; | |
517 | ||
518 | while (* syn != 0) | |
519 | { | |
520 | /* Non operand chars must match exactly. */ | |
521 | if (CGEN_SYNTAX_CHAR_P (* syn)) | |
522 | { | |
1fa60b5d DE |
523 | /* FIXME: While we allow for non-GAS callers above, we assume the |
524 | first char after the mnemonic part is a space. */ | |
525 | /* FIXME: We also take inappropriate advantage of the fact that | |
526 | GAS's input scrubber will remove extraneous blanks. */ | |
37111cc7 | 527 | if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn))) |
252b5132 RH |
528 | { |
529 | #ifdef CGEN_MNEMONIC_OPERANDS | |
b3466c39 | 530 | if (CGEN_SYNTAX_CHAR(* syn) == ' ') |
252b5132 RH |
531 | past_opcode_p = 1; |
532 | #endif | |
533 | ++ syn; | |
534 | ++ str; | |
535 | } | |
b3466c39 | 536 | else if (*str) |
252b5132 RH |
537 | { |
538 | /* Syntax char didn't match. Can't be this insn. */ | |
6bb95a0f | 539 | static char msg [80]; |
f3a55c17 | 540 | |
6bb95a0f DB |
541 | /* xgettext:c-format */ |
542 | sprintf (msg, _("syntax error (expected char `%c', found `%c')"), | |
b3466c39 DB |
543 | CGEN_SYNTAX_CHAR(*syn), *str); |
544 | return msg; | |
545 | } | |
546 | else | |
547 | { | |
548 | /* Ran out of input. */ | |
549 | static char msg [80]; | |
f3a55c17 | 550 | |
b3466c39 DB |
551 | /* xgettext:c-format */ |
552 | sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"), | |
553 | CGEN_SYNTAX_CHAR(*syn)); | |
6bb95a0f | 554 | return msg; |
252b5132 RH |
555 | } |
556 | continue; | |
557 | } | |
558 | ||
559 | /* We have an operand of some sort. */ | |
a978a3e5 | 560 | errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), |
252b5132 RH |
561 | &str, fields); |
562 | if (errmsg) | |
563 | return errmsg; | |
564 | ||
565 | /* Done with this operand, continue with next one. */ | |
566 | ++ syn; | |
567 | } | |
568 | ||
569 | /* If we're at the end of the syntax string, we're done. */ | |
b3466c39 | 570 | if (* syn == 0) |
252b5132 RH |
571 | { |
572 | /* FIXME: For the moment we assume a valid `str' can only contain | |
573 | blanks now. IE: We needn't try again with a longer version of | |
574 | the insn and it is assumed that longer versions of insns appear | |
575 | before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */ | |
37111cc7 | 576 | while (ISSPACE (* str)) |
252b5132 RH |
577 | ++ str; |
578 | ||
579 | if (* str != '\0') | |
580 | return _("junk at end of line"); /* FIXME: would like to include `str' */ | |
581 | ||
582 | return NULL; | |
583 | } | |
584 | ||
585 | /* We couldn't parse it. */ | |
586 | return _("unrecognized instruction"); | |
587 | } | |
588 | \f | |
589 | /* Main entry point. | |
590 | This routine is called for each instruction to be assembled. | |
591 | STR points to the insn to be assembled. | |
592 | We assume all necessary tables have been initialized. | |
593 | The assembled instruction, less any fixups, is stored in BUF. | |
594 | Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value | |
595 | still needs to be converted to target byte order, otherwise BUF is an array | |
596 | of bytes in target byte order. | |
597 | The result is a pointer to the insn's entry in the opcode table, | |
598 | or NULL if an error occured (an error message will have already been | |
599 | printed). | |
600 | ||
601 | Note that when processing (non-alias) macro-insns, | |
602 | this function recurses. | |
603 | ||
604 | ??? It's possible to make this cpu-independent. | |
605 | One would have to deal with a few minor things. | |
606 | At this point in time doing so would be more of a curiosity than useful | |
607 | [for example this file isn't _that_ big], but keeping the possibility in | |
608 | mind helps keep the design clean. */ | |
609 | ||
610 | const CGEN_INSN * | |
ffead7ae MM |
611 | fr30_cgen_assemble_insn (CGEN_CPU_DESC cd, |
612 | const char *str, | |
613 | CGEN_FIELDS *fields, | |
614 | CGEN_INSN_BYTES_PTR buf, | |
615 | char **errmsg) | |
252b5132 RH |
616 | { |
617 | const char *start; | |
618 | CGEN_INSN_LIST *ilist; | |
b3466c39 DB |
619 | const char *parse_errmsg = NULL; |
620 | const char *insert_errmsg = NULL; | |
fc7bc883 | 621 | int recognized_mnemonic = 0; |
252b5132 RH |
622 | |
623 | /* Skip leading white space. */ | |
37111cc7 | 624 | while (ISSPACE (* str)) |
252b5132 RH |
625 | ++ str; |
626 | ||
627 | /* The instructions are stored in hashed lists. | |
628 | Get the first in the list. */ | |
629 | ilist = CGEN_ASM_LOOKUP_INSN (cd, str); | |
630 | ||
631 | /* Keep looking until we find a match. */ | |
252b5132 RH |
632 | start = str; |
633 | for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist)) | |
634 | { | |
635 | const CGEN_INSN *insn = ilist->insn; | |
fc7bc883 | 636 | recognized_mnemonic = 1; |
252b5132 | 637 | |
6bb95a0f | 638 | #ifdef CGEN_VALIDATE_INSN_SUPPORTED |
f3a55c17 NC |
639 | /* Not usually needed as unsupported opcodes |
640 | shouldn't be in the hash lists. */ | |
252b5132 RH |
641 | /* Is this insn supported by the selected cpu? */ |
642 | if (! fr30_cgen_insn_supported (cd, insn)) | |
643 | continue; | |
644 | #endif | |
b11dcf4e | 645 | /* If the RELAXED attribute is set, this is an insn that shouldn't be |
252b5132 RH |
646 | chosen immediately. Instead, it is used during assembler/linker |
647 | relaxation if possible. */ | |
b11dcf4e | 648 | if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0) |
252b5132 RH |
649 | continue; |
650 | ||
651 | str = start; | |
652 | ||
f3a55c17 | 653 | /* Skip this insn if str doesn't look right lexically. */ |
fc7bc883 RH |
654 | if (CGEN_INSN_RX (insn) != NULL && |
655 | regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH) | |
656 | continue; | |
657 | ||
252b5132 RH |
658 | /* Allow parse/insert handlers to obtain length of insn. */ |
659 | CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn); | |
660 | ||
b3466c39 DB |
661 | parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields); |
662 | if (parse_errmsg != NULL) | |
6bb95a0f | 663 | continue; |
252b5132 | 664 | |
f3a55c17 | 665 | /* ??? 0 is passed for `pc'. */ |
b3466c39 DB |
666 | insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf, |
667 | (bfd_vma) 0); | |
668 | if (insert_errmsg != NULL) | |
6bb95a0f DB |
669 | continue; |
670 | ||
671 | /* It is up to the caller to actually output the insn and any | |
672 | queued relocs. */ | |
673 | return insn; | |
252b5132 RH |
674 | } |
675 | ||
252b5132 | 676 | { |
6bb95a0f | 677 | static char errbuf[150]; |
fc7bc883 | 678 | #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS |
b3466c39 | 679 | const char *tmp_errmsg; |
6bb95a0f | 680 | |
b3466c39 | 681 | /* If requesting verbose error messages, use insert_errmsg. |
f3a55c17 | 682 | Failing that, use parse_errmsg. */ |
b3466c39 DB |
683 | tmp_errmsg = (insert_errmsg ? insert_errmsg : |
684 | parse_errmsg ? parse_errmsg : | |
f3a55c17 NC |
685 | recognized_mnemonic ? |
686 | _("unrecognized form of instruction") : | |
b3466c39 DB |
687 | _("unrecognized instruction")); |
688 | ||
6bb95a0f DB |
689 | if (strlen (start) > 50) |
690 | /* xgettext:c-format */ | |
691 | sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start); | |
692 | else | |
693 | /* xgettext:c-format */ | |
694 | sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start); | |
695 | #else | |
252b5132 RH |
696 | if (strlen (start) > 50) |
697 | /* xgettext:c-format */ | |
698 | sprintf (errbuf, _("bad instruction `%.50s...'"), start); | |
699 | else | |
700 | /* xgettext:c-format */ | |
701 | sprintf (errbuf, _("bad instruction `%.50s'"), start); | |
6bb95a0f | 702 | #endif |
252b5132 RH |
703 | |
704 | *errmsg = errbuf; | |
705 | return NULL; | |
706 | } | |
707 | } |