/src/binutils-gdb/opcodes/m32r-asm.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* DO NOT EDIT! -*- buffer-read-only: t -*- vi:set ro: */ |
2 | | /* Assembler interface for targets using CGEN. -*- C -*- |
3 | | CGEN: Cpu tools GENerator |
4 | | |
5 | | THIS FILE IS MACHINE GENERATED WITH CGEN. |
6 | | - the resultant file is machine generated, cgen-asm.in isn't |
7 | | |
8 | | Copyright (C) 1996-2025 Free Software Foundation, Inc. |
9 | | |
10 | | This file is part of libopcodes. |
11 | | |
12 | | This library 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 3, or (at your option) |
15 | | any later version. |
16 | | |
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. |
21 | | |
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. */ |
25 | | |
26 | | |
27 | | /* ??? Eventually more and more of this stuff can go to cpu-independent files. |
28 | | Keep that in mind. */ |
29 | | |
30 | | #include "sysdep.h" |
31 | | #include <stdio.h> |
32 | | #include "ansidecl.h" |
33 | | #include "bfd.h" |
34 | | #include "symcat.h" |
35 | | #include "m32r-desc.h" |
36 | | #include "m32r-opc.h" |
37 | | #include "opintl.h" |
38 | | #include "xregex.h" |
39 | | #include "libiberty.h" |
40 | | #include "safe-ctype.h" |
41 | | |
42 | | #undef min |
43 | | #define min(a,b) ((a) < (b) ? (a) : (b)) |
44 | | #undef max |
45 | | #define max(a,b) ((a) > (b) ? (a) : (b)) |
46 | | |
47 | | static const char * parse_insn_normal |
48 | | (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *); |
49 | | |
50 | | /* -- assembler routines inserted here. */ |
51 | | |
52 | | /* -- asm.c */ |
53 | | static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'"); |
54 | | |
55 | | /* Handle '#' prefixes (i.e. skip over them). */ |
56 | | |
57 | | static const char * |
58 | | parse_hash (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, |
59 | | const char **strp, |
60 | | int opindex ATTRIBUTE_UNUSED, |
61 | | long *valuep ATTRIBUTE_UNUSED) |
62 | 0 | { |
63 | 0 | if (**strp == '#') |
64 | 0 | ++*strp; |
65 | 0 | return NULL; |
66 | 0 | } |
67 | | |
68 | | /* Handle shigh(), high(). */ |
69 | | |
70 | | static const char * |
71 | | parse_hi16 (CGEN_CPU_DESC cd, |
72 | | const char **strp, |
73 | | int opindex, |
74 | | unsigned long *valuep) |
75 | 0 | { |
76 | 0 | const char *errmsg; |
77 | 0 | enum cgen_parse_operand_result result_type; |
78 | 0 | bfd_vma value; |
79 | |
|
80 | 0 | if (**strp == '#') |
81 | 0 | ++*strp; |
82 | |
|
83 | 0 | if (strncasecmp (*strp, "high(", 5) == 0) |
84 | 0 | { |
85 | 0 | *strp += 5; |
86 | 0 | errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_HI16_ULO, |
87 | 0 | & result_type, & value); |
88 | 0 | if (**strp != ')') |
89 | 0 | return MISSING_CLOSING_PARENTHESIS; |
90 | 0 | ++*strp; |
91 | 0 | if (errmsg == NULL |
92 | 0 | && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) |
93 | 0 | { |
94 | 0 | value >>= 16; |
95 | 0 | value &= 0xffff; |
96 | 0 | } |
97 | 0 | *valuep = value; |
98 | 0 | return errmsg; |
99 | 0 | } |
100 | 0 | else if (strncasecmp (*strp, "shigh(", 6) == 0) |
101 | 0 | { |
102 | 0 | *strp += 6; |
103 | 0 | errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_HI16_SLO, |
104 | 0 | & result_type, & value); |
105 | 0 | if (**strp != ')') |
106 | 0 | return MISSING_CLOSING_PARENTHESIS; |
107 | 0 | ++*strp; |
108 | 0 | if (errmsg == NULL |
109 | 0 | && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) |
110 | 0 | { |
111 | 0 | value += 0x8000; |
112 | 0 | value >>= 16; |
113 | 0 | value &= 0xffff; |
114 | 0 | } |
115 | 0 | *valuep = value; |
116 | 0 | return errmsg; |
117 | 0 | } |
118 | | |
119 | 0 | return cgen_parse_unsigned_integer (cd, strp, opindex, valuep); |
120 | 0 | } |
121 | | |
122 | | /* Handle low() in a signed context. Also handle sda(). |
123 | | The signedness of the value doesn't matter to low(), but this also |
124 | | handles the case where low() isn't present. */ |
125 | | |
126 | | static const char * |
127 | | parse_slo16 (CGEN_CPU_DESC cd, |
128 | | const char ** strp, |
129 | | int opindex, |
130 | | long * valuep) |
131 | 0 | { |
132 | 0 | const char *errmsg; |
133 | 0 | enum cgen_parse_operand_result result_type; |
134 | 0 | bfd_vma value; |
135 | |
|
136 | 0 | if (**strp == '#') |
137 | 0 | ++*strp; |
138 | |
|
139 | 0 | if (strncasecmp (*strp, "low(", 4) == 0) |
140 | 0 | { |
141 | 0 | *strp += 4; |
142 | 0 | errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_LO16, |
143 | 0 | & result_type, & value); |
144 | 0 | if (**strp != ')') |
145 | 0 | return MISSING_CLOSING_PARENTHESIS; |
146 | 0 | ++*strp; |
147 | 0 | if (errmsg == NULL |
148 | 0 | && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) |
149 | 0 | value = ((value & 0xffff) ^ 0x8000) - 0x8000; |
150 | 0 | *valuep = value; |
151 | 0 | return errmsg; |
152 | 0 | } |
153 | | |
154 | 0 | if (strncasecmp (*strp, "sda(", 4) == 0) |
155 | 0 | { |
156 | 0 | *strp += 4; |
157 | 0 | errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_SDA16, |
158 | 0 | NULL, & value); |
159 | 0 | if (**strp != ')') |
160 | 0 | return MISSING_CLOSING_PARENTHESIS; |
161 | 0 | ++*strp; |
162 | 0 | *valuep = value; |
163 | 0 | return errmsg; |
164 | 0 | } |
165 | | |
166 | 0 | return cgen_parse_signed_integer (cd, strp, opindex, valuep); |
167 | 0 | } |
168 | | |
169 | | /* Handle low() in an unsigned context. |
170 | | The signedness of the value doesn't matter to low(), but this also |
171 | | handles the case where low() isn't present. */ |
172 | | |
173 | | static const char * |
174 | | parse_ulo16 (CGEN_CPU_DESC cd, |
175 | | const char **strp, |
176 | | int opindex, |
177 | | unsigned long *valuep) |
178 | 0 | { |
179 | 0 | const char *errmsg; |
180 | 0 | enum cgen_parse_operand_result result_type; |
181 | 0 | bfd_vma value; |
182 | |
|
183 | 0 | if (**strp == '#') |
184 | 0 | ++*strp; |
185 | |
|
186 | 0 | if (strncasecmp (*strp, "low(", 4) == 0) |
187 | 0 | { |
188 | 0 | *strp += 4; |
189 | 0 | errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_LO16, |
190 | 0 | & result_type, & value); |
191 | 0 | if (**strp != ')') |
192 | 0 | return MISSING_CLOSING_PARENTHESIS; |
193 | 0 | ++*strp; |
194 | 0 | if (errmsg == NULL |
195 | 0 | && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) |
196 | 0 | value &= 0xffff; |
197 | 0 | *valuep = value; |
198 | 0 | return errmsg; |
199 | 0 | } |
200 | | |
201 | 0 | return cgen_parse_unsigned_integer (cd, strp, opindex, valuep); |
202 | 0 | } |
203 | | |
204 | | /* -- */ |
205 | | |
206 | | const char * m32r_cgen_parse_operand |
207 | | (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *); |
208 | | |
209 | | /* Main entry point for operand parsing. |
210 | | |
211 | | This function is basically just a big switch statement. Earlier versions |
212 | | used tables to look up the function to use, but |
213 | | - if the table contains both assembler and disassembler functions then |
214 | | the disassembler contains much of the assembler and vice-versa, |
215 | | - there's a lot of inlining possibilities as things grow, |
216 | | - using a switch statement avoids the function call overhead. |
217 | | |
218 | | This function could be moved into `parse_insn_normal', but keeping it |
219 | | separate makes clear the interface between `parse_insn_normal' and each of |
220 | | the handlers. */ |
221 | | |
222 | | const char * |
223 | | m32r_cgen_parse_operand (CGEN_CPU_DESC cd, |
224 | | int opindex, |
225 | | const char ** strp, |
226 | | CGEN_FIELDS * fields) |
227 | 0 | { |
228 | 0 | const char * errmsg = NULL; |
229 | | /* Used by scalar operands that still need to be parsed. */ |
230 | 0 | long junk ATTRIBUTE_UNUSED; |
231 | |
|
232 | 0 | switch (opindex) |
233 | 0 | { |
234 | 0 | case M32R_OPERAND_ACC : |
235 | 0 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_h_accums, & fields->f_acc); |
236 | 0 | break; |
237 | 0 | case M32R_OPERAND_ACCD : |
238 | 0 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_h_accums, & fields->f_accd); |
239 | 0 | break; |
240 | 0 | case M32R_OPERAND_ACCS : |
241 | 0 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_h_accums, & fields->f_accs); |
242 | 0 | break; |
243 | 0 | case M32R_OPERAND_DCR : |
244 | 0 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_cr_names, & fields->f_r1); |
245 | 0 | break; |
246 | 0 | case M32R_OPERAND_DISP16 : |
247 | 0 | { |
248 | 0 | bfd_vma value = 0; |
249 | 0 | errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_DISP16, 0, NULL, & value); |
250 | 0 | fields->f_disp16 = value; |
251 | 0 | } |
252 | 0 | break; |
253 | 0 | case M32R_OPERAND_DISP24 : |
254 | 0 | { |
255 | 0 | bfd_vma value = 0; |
256 | 0 | errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_DISP24, 0, NULL, & value); |
257 | 0 | fields->f_disp24 = value; |
258 | 0 | } |
259 | 0 | break; |
260 | 0 | case M32R_OPERAND_DISP8 : |
261 | 0 | { |
262 | 0 | bfd_vma value = 0; |
263 | 0 | errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_DISP8, 0, NULL, & value); |
264 | 0 | fields->f_disp8 = value; |
265 | 0 | } |
266 | 0 | break; |
267 | 0 | case M32R_OPERAND_DR : |
268 | 0 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r1); |
269 | 0 | break; |
270 | 0 | case M32R_OPERAND_HASH : |
271 | 0 | errmsg = parse_hash (cd, strp, M32R_OPERAND_HASH, (long *) (& junk)); |
272 | 0 | break; |
273 | 0 | case M32R_OPERAND_HI16 : |
274 | 0 | errmsg = parse_hi16 (cd, strp, M32R_OPERAND_HI16, (unsigned long *) (& fields->f_hi16)); |
275 | 0 | break; |
276 | 0 | case M32R_OPERAND_IMM1 : |
277 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_IMM1, (unsigned long *) (& fields->f_imm1)); |
278 | 0 | break; |
279 | 0 | case M32R_OPERAND_SCR : |
280 | 0 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_cr_names, & fields->f_r2); |
281 | 0 | break; |
282 | 0 | case M32R_OPERAND_SIMM16 : |
283 | 0 | errmsg = cgen_parse_signed_integer (cd, strp, M32R_OPERAND_SIMM16, (long *) (& fields->f_simm16)); |
284 | 0 | break; |
285 | 0 | case M32R_OPERAND_SIMM8 : |
286 | 0 | errmsg = cgen_parse_signed_integer (cd, strp, M32R_OPERAND_SIMM8, (long *) (& fields->f_simm8)); |
287 | 0 | break; |
288 | 0 | case M32R_OPERAND_SLO16 : |
289 | 0 | errmsg = parse_slo16 (cd, strp, M32R_OPERAND_SLO16, (long *) (& fields->f_simm16)); |
290 | 0 | break; |
291 | 0 | case M32R_OPERAND_SR : |
292 | 0 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r2); |
293 | 0 | break; |
294 | 0 | case M32R_OPERAND_SRC1 : |
295 | 0 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r1); |
296 | 0 | break; |
297 | 0 | case M32R_OPERAND_SRC2 : |
298 | 0 | errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r2); |
299 | 0 | break; |
300 | 0 | case M32R_OPERAND_UIMM16 : |
301 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM16, (unsigned long *) (& fields->f_uimm16)); |
302 | 0 | break; |
303 | 0 | case M32R_OPERAND_UIMM24 : |
304 | 0 | { |
305 | 0 | bfd_vma value = 0; |
306 | 0 | errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_UIMM24, 0, NULL, & value); |
307 | 0 | fields->f_uimm24 = value; |
308 | 0 | } |
309 | 0 | break; |
310 | 0 | case M32R_OPERAND_UIMM3 : |
311 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM3, (unsigned long *) (& fields->f_uimm3)); |
312 | 0 | break; |
313 | 0 | case M32R_OPERAND_UIMM4 : |
314 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM4, (unsigned long *) (& fields->f_uimm4)); |
315 | 0 | break; |
316 | 0 | case M32R_OPERAND_UIMM5 : |
317 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM5, (unsigned long *) (& fields->f_uimm5)); |
318 | 0 | break; |
319 | 0 | case M32R_OPERAND_UIMM8 : |
320 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM8, (unsigned long *) (& fields->f_uimm8)); |
321 | 0 | break; |
322 | 0 | case M32R_OPERAND_ULO16 : |
323 | 0 | errmsg = parse_ulo16 (cd, strp, M32R_OPERAND_ULO16, (unsigned long *) (& fields->f_uimm16)); |
324 | 0 | break; |
325 | | |
326 | 0 | default : |
327 | | /* xgettext:c-format */ |
328 | 0 | opcodes_error_handler |
329 | 0 | (_("internal error: unrecognized field %d while parsing"), |
330 | 0 | opindex); |
331 | 0 | abort (); |
332 | 0 | } |
333 | | |
334 | 0 | return errmsg; |
335 | 0 | } |
336 | | |
337 | | cgen_parse_fn * const m32r_cgen_parse_handlers[] = |
338 | | { |
339 | | parse_insn_normal, |
340 | | }; |
341 | | |
342 | | void |
343 | | m32r_cgen_init_asm (CGEN_CPU_DESC cd) |
344 | 0 | { |
345 | 0 | m32r_cgen_init_opcode_table (cd); |
346 | 0 | m32r_cgen_init_ibld_table (cd); |
347 | 0 | cd->parse_handlers = & m32r_cgen_parse_handlers[0]; |
348 | 0 | cd->parse_operand = m32r_cgen_parse_operand; |
349 | | #ifdef CGEN_ASM_INIT_HOOK |
350 | | CGEN_ASM_INIT_HOOK |
351 | | #endif |
352 | 0 | } |
353 | | |
354 | | |
355 | | |
356 | | /* Regex construction routine. |
357 | | |
358 | | This translates an opcode syntax string into a regex string, |
359 | | by replacing any non-character syntax element (such as an |
360 | | opcode) with the pattern '.*' |
361 | | |
362 | | It then compiles the regex and stores it in the opcode, for |
363 | | later use by m32r_cgen_assemble_insn |
364 | | |
365 | | Returns NULL for success, an error message for failure. */ |
366 | | |
367 | | char * |
368 | | m32r_cgen_build_insn_regex (CGEN_INSN *insn) |
369 | 925 | { |
370 | 925 | CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn); |
371 | 925 | const char *mnem = CGEN_INSN_MNEMONIC (insn); |
372 | 925 | char rxbuf[CGEN_MAX_RX_ELEMENTS]; |
373 | 925 | char *rx = rxbuf; |
374 | 925 | const CGEN_SYNTAX_CHAR_TYPE *syn; |
375 | 925 | int reg_err; |
376 | | |
377 | 925 | syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc)); |
378 | | |
379 | | /* Mnemonics come first in the syntax string. */ |
380 | 925 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) |
381 | 5 | return _("missing mnemonic in syntax string"); |
382 | 920 | ++syn; |
383 | | |
384 | | /* Generate a case sensitive regular expression that emulates case |
385 | | insensitive matching in the "C" locale. We cannot generate a case |
386 | | insensitive regular expression because in Turkish locales, 'i' and 'I' |
387 | | are not equal modulo case conversion. */ |
388 | | |
389 | | /* Copy the literal mnemonic out of the insn. */ |
390 | 4.57k | for (; *mnem; mnem++) |
391 | 3.65k | { |
392 | 3.65k | char c = *mnem; |
393 | | |
394 | 3.65k | if (ISALPHA (c)) |
395 | 3.51k | { |
396 | 3.51k | *rx++ = '['; |
397 | 3.51k | *rx++ = TOLOWER (c); |
398 | 3.51k | *rx++ = TOUPPER (c); |
399 | 3.51k | *rx++ = ']'; |
400 | 3.51k | } |
401 | 140 | else |
402 | 140 | *rx++ = c; |
403 | 3.65k | } |
404 | | |
405 | | /* Copy any remaining literals from the syntax string into the rx. */ |
406 | 4.93k | for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn) |
407 | 4.01k | { |
408 | 4.01k | if (CGEN_SYNTAX_CHAR_P (* syn)) |
409 | 2.24k | { |
410 | 2.24k | char c = CGEN_SYNTAX_CHAR (* syn); |
411 | | |
412 | 2.24k | switch (c) |
413 | 2.24k | { |
414 | | /* Escape any regex metacharacters in the syntax. */ |
415 | 0 | case '.': case '[': case '\\': |
416 | 0 | case '*': case '^': case '$': |
417 | |
|
418 | | #ifdef CGEN_ESCAPE_EXTENDED_REGEX |
419 | | case '?': case '{': case '}': |
420 | | case '(': case ')': case '*': |
421 | | case '|': case '+': case ']': |
422 | | #endif |
423 | 0 | *rx++ = '\\'; |
424 | 0 | *rx++ = c; |
425 | 0 | break; |
426 | | |
427 | 2.24k | default: |
428 | 2.24k | if (ISALPHA (c)) |
429 | 0 | { |
430 | 0 | *rx++ = '['; |
431 | 0 | *rx++ = TOLOWER (c); |
432 | 0 | *rx++ = TOUPPER (c); |
433 | 0 | *rx++ = ']'; |
434 | 0 | } |
435 | 2.24k | else |
436 | 2.24k | *rx++ = c; |
437 | 2.24k | break; |
438 | 2.24k | } |
439 | 2.24k | } |
440 | 1.77k | else |
441 | 1.77k | { |
442 | | /* Replace non-syntax fields with globs. */ |
443 | 1.77k | *rx++ = '.'; |
444 | 1.77k | *rx++ = '*'; |
445 | 1.77k | } |
446 | 4.01k | } |
447 | | |
448 | | /* Trailing whitespace ok. */ |
449 | 920 | * rx++ = '['; |
450 | 920 | * rx++ = ' '; |
451 | 920 | * rx++ = '\t'; |
452 | 920 | * rx++ = ']'; |
453 | 920 | * rx++ = '*'; |
454 | | |
455 | | /* But anchor it after that. */ |
456 | 920 | * rx++ = '$'; |
457 | 920 | * rx = '\0'; |
458 | | |
459 | 920 | CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t)); |
460 | 920 | reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB); |
461 | | |
462 | 920 | if (reg_err == 0) |
463 | 920 | return NULL; |
464 | 0 | else |
465 | 0 | { |
466 | 0 | static char msg[80]; |
467 | |
|
468 | 0 | regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80); |
469 | 0 | regfree ((regex_t *) CGEN_INSN_RX (insn)); |
470 | 0 | free (CGEN_INSN_RX (insn)); |
471 | 0 | (CGEN_INSN_RX (insn)) = NULL; |
472 | 0 | return msg; |
473 | 0 | } |
474 | 920 | } |
475 | | |
476 | | |
477 | | /* Default insn parser. |
478 | | |
479 | | The syntax string is scanned and operands are parsed and stored in FIELDS. |
480 | | Relocs are queued as we go via other callbacks. |
481 | | |
482 | | ??? Note that this is currently an all-or-nothing parser. If we fail to |
483 | | parse the instruction, we return 0 and the caller will start over from |
484 | | the beginning. Backtracking will be necessary in parsing subexpressions, |
485 | | but that can be handled there. Not handling backtracking here may get |
486 | | expensive in the case of the m68k. Deal with later. |
487 | | |
488 | | Returns NULL for success, an error message for failure. */ |
489 | | |
490 | | static const char * |
491 | | parse_insn_normal (CGEN_CPU_DESC cd, |
492 | | const CGEN_INSN *insn, |
493 | | const char **strp, |
494 | | CGEN_FIELDS *fields) |
495 | 0 | { |
496 | | /* ??? Runtime added insns not handled yet. */ |
497 | 0 | const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn); |
498 | 0 | const char *str = *strp; |
499 | 0 | const char *errmsg; |
500 | 0 | const char *p; |
501 | 0 | const CGEN_SYNTAX_CHAR_TYPE * syn; |
502 | 0 | #ifdef CGEN_MNEMONIC_OPERANDS |
503 | | /* FIXME: wip */ |
504 | 0 | int past_opcode_p; |
505 | 0 | #endif |
506 | | |
507 | | /* For now we assume the mnemonic is first (there are no leading operands). |
508 | | We can parse it without needing to set up operand parsing. |
509 | | GAS's input scrubber will ensure mnemonics are lowercase, but we may |
510 | | not be called from GAS. */ |
511 | 0 | p = CGEN_INSN_MNEMONIC (insn); |
512 | 0 | while (*p && TOLOWER (*p) == TOLOWER (*str)) |
513 | 0 | ++p, ++str; |
514 | |
|
515 | 0 | if (* p) |
516 | 0 | return _("unrecognized instruction"); |
517 | | |
518 | | #ifndef CGEN_MNEMONIC_OPERANDS |
519 | | if (* str && ! ISSPACE (* str)) |
520 | | return _("unrecognized instruction"); |
521 | | #endif |
522 | | |
523 | 0 | CGEN_INIT_PARSE (cd); |
524 | 0 | cgen_init_parse_operand (cd); |
525 | 0 | #ifdef CGEN_MNEMONIC_OPERANDS |
526 | 0 | past_opcode_p = 0; |
527 | 0 | #endif |
528 | | |
529 | | /* We don't check for (*str != '\0') here because we want to parse |
530 | | any trailing fake arguments in the syntax string. */ |
531 | 0 | syn = CGEN_SYNTAX_STRING (syntax); |
532 | | |
533 | | /* Mnemonics come first for now, ensure valid string. */ |
534 | 0 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) |
535 | 0 | abort (); |
536 | | |
537 | 0 | ++syn; |
538 | |
|
539 | 0 | while (* syn != 0) |
540 | 0 | { |
541 | | /* Non operand chars must match exactly. */ |
542 | 0 | if (CGEN_SYNTAX_CHAR_P (* syn)) |
543 | 0 | { |
544 | | /* FIXME: While we allow for non-GAS callers above, we assume the |
545 | | first char after the mnemonic part is a space. */ |
546 | | /* FIXME: We also take inappropriate advantage of the fact that |
547 | | GAS's input scrubber will remove extraneous blanks. */ |
548 | 0 | if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn))) |
549 | 0 | { |
550 | 0 | #ifdef CGEN_MNEMONIC_OPERANDS |
551 | 0 | if (CGEN_SYNTAX_CHAR(* syn) == ' ') |
552 | 0 | past_opcode_p = 1; |
553 | 0 | #endif |
554 | 0 | ++ syn; |
555 | 0 | ++ str; |
556 | 0 | } |
557 | 0 | else if (*str) |
558 | 0 | { |
559 | | /* Syntax char didn't match. Can't be this insn. */ |
560 | 0 | static char msg [80]; |
561 | | |
562 | | /* xgettext:c-format */ |
563 | 0 | sprintf (msg, _("syntax error (expected char `%c', found `%c')"), |
564 | 0 | CGEN_SYNTAX_CHAR(*syn), *str); |
565 | 0 | return msg; |
566 | 0 | } |
567 | 0 | else |
568 | 0 | { |
569 | | /* Ran out of input. */ |
570 | 0 | static char msg [80]; |
571 | | |
572 | | /* xgettext:c-format */ |
573 | 0 | sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"), |
574 | 0 | CGEN_SYNTAX_CHAR(*syn)); |
575 | 0 | return msg; |
576 | 0 | } |
577 | 0 | continue; |
578 | 0 | } |
579 | | |
580 | 0 | #ifdef CGEN_MNEMONIC_OPERANDS |
581 | 0 | (void) past_opcode_p; |
582 | 0 | #endif |
583 | | /* We have an operand of some sort. */ |
584 | 0 | errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields); |
585 | 0 | if (errmsg) |
586 | 0 | return errmsg; |
587 | | |
588 | | /* Done with this operand, continue with next one. */ |
589 | 0 | ++ syn; |
590 | 0 | } |
591 | | |
592 | | /* If we're at the end of the syntax string, we're done. */ |
593 | 0 | if (* syn == 0) |
594 | 0 | { |
595 | | /* FIXME: For the moment we assume a valid `str' can only contain |
596 | | blanks now. IE: We needn't try again with a longer version of |
597 | | the insn and it is assumed that longer versions of insns appear |
598 | | before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */ |
599 | 0 | while (ISSPACE (* str)) |
600 | 0 | ++ str; |
601 | |
|
602 | 0 | if (* str != '\0') |
603 | 0 | return _("junk at end of line"); /* FIXME: would like to include `str' */ |
604 | | |
605 | 0 | return NULL; |
606 | 0 | } |
607 | | |
608 | | /* We couldn't parse it. */ |
609 | 0 | return _("unrecognized instruction"); |
610 | 0 | } |
611 | | |
612 | | /* Main entry point. |
613 | | This routine is called for each instruction to be assembled. |
614 | | STR points to the insn to be assembled. |
615 | | We assume all necessary tables have been initialized. |
616 | | The assembled instruction, less any fixups, is stored in BUF. |
617 | | Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value |
618 | | still needs to be converted to target byte order, otherwise BUF is an array |
619 | | of bytes in target byte order. |
620 | | The result is a pointer to the insn's entry in the opcode table, |
621 | | or NULL if an error occured (an error message will have already been |
622 | | printed). |
623 | | |
624 | | Note that when processing (non-alias) macro-insns, |
625 | | this function recurses. |
626 | | |
627 | | ??? It's possible to make this cpu-independent. |
628 | | One would have to deal with a few minor things. |
629 | | At this point in time doing so would be more of a curiosity than useful |
630 | | [for example this file isn't _that_ big], but keeping the possibility in |
631 | | mind helps keep the design clean. */ |
632 | | |
633 | | const CGEN_INSN * |
634 | | m32r_cgen_assemble_insn (CGEN_CPU_DESC cd, |
635 | | const char *str, |
636 | | CGEN_FIELDS *fields, |
637 | | CGEN_INSN_BYTES_PTR buf, |
638 | | char **errmsg) |
639 | 0 | { |
640 | 0 | const char *start; |
641 | 0 | CGEN_INSN_LIST *ilist; |
642 | 0 | const char *parse_errmsg = NULL; |
643 | 0 | const char *insert_errmsg = NULL; |
644 | 0 | int recognized_mnemonic = 0; |
645 | | |
646 | | /* Skip leading white space. */ |
647 | 0 | while (ISSPACE (* str)) |
648 | 0 | ++ str; |
649 | | |
650 | | /* The instructions are stored in hashed lists. |
651 | | Get the first in the list. */ |
652 | 0 | ilist = CGEN_ASM_LOOKUP_INSN (cd, str); |
653 | | |
654 | | /* Keep looking until we find a match. */ |
655 | 0 | start = str; |
656 | 0 | for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist)) |
657 | 0 | { |
658 | 0 | const CGEN_INSN *insn = ilist->insn; |
659 | 0 | recognized_mnemonic = 1; |
660 | |
|
661 | | #ifdef CGEN_VALIDATE_INSN_SUPPORTED |
662 | | /* Not usually needed as unsupported opcodes |
663 | | shouldn't be in the hash lists. */ |
664 | | /* Is this insn supported by the selected cpu? */ |
665 | | if (! m32r_cgen_insn_supported (cd, insn)) |
666 | | continue; |
667 | | #endif |
668 | | /* If the RELAXED attribute is set, this is an insn that shouldn't be |
669 | | chosen immediately. Instead, it is used during assembler/linker |
670 | | relaxation if possible. */ |
671 | 0 | if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0) |
672 | 0 | continue; |
673 | | |
674 | 0 | str = start; |
675 | | |
676 | | /* Skip this insn if str doesn't look right lexically. */ |
677 | 0 | if (CGEN_INSN_RX (insn) != NULL && |
678 | 0 | regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH) |
679 | 0 | continue; |
680 | | |
681 | | /* Allow parse/insert handlers to obtain length of insn. */ |
682 | 0 | CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn); |
683 | |
|
684 | 0 | parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields); |
685 | 0 | if (parse_errmsg != NULL) |
686 | 0 | continue; |
687 | | |
688 | | /* ??? 0 is passed for `pc'. */ |
689 | 0 | insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf, |
690 | 0 | (bfd_vma) 0); |
691 | 0 | if (insert_errmsg != NULL) |
692 | 0 | continue; |
693 | | |
694 | | /* It is up to the caller to actually output the insn and any |
695 | | queued relocs. */ |
696 | 0 | return insn; |
697 | 0 | } |
698 | | |
699 | 0 | { |
700 | 0 | static char errbuf[150]; |
701 | 0 | const char *tmp_errmsg; |
702 | | #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS |
703 | | #define be_verbose 1 |
704 | | #else |
705 | 0 | #define be_verbose 0 |
706 | 0 | #endif |
707 | |
|
708 | 0 | if (be_verbose) |
709 | 0 | { |
710 | | /* If requesting verbose error messages, use insert_errmsg. |
711 | | Failing that, use parse_errmsg. */ |
712 | 0 | tmp_errmsg = (insert_errmsg ? insert_errmsg : |
713 | 0 | parse_errmsg ? parse_errmsg : |
714 | 0 | recognized_mnemonic ? |
715 | 0 | _("unrecognized form of instruction") : |
716 | 0 | _("unrecognized instruction")); |
717 | |
|
718 | 0 | if (strlen (start) > 50) |
719 | | /* xgettext:c-format */ |
720 | 0 | sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start); |
721 | 0 | else |
722 | | /* xgettext:c-format */ |
723 | 0 | sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start); |
724 | 0 | } |
725 | 0 | else |
726 | 0 | { |
727 | 0 | if (strlen (start) > 50) |
728 | | /* xgettext:c-format */ |
729 | 0 | sprintf (errbuf, _("bad instruction `%.50s...'"), start); |
730 | 0 | else |
731 | | /* xgettext:c-format */ |
732 | 0 | sprintf (errbuf, _("bad instruction `%.50s'"), start); |
733 | 0 | } |
734 | |
|
735 | 0 | *errmsg = errbuf; |
736 | 0 | return NULL; |
737 | 0 | } |
738 | 0 | } |