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