/src/binutils-gdb/opcodes/xstormy16-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 "xstormy16-desc.h" |
36 | | #include "xstormy16-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 | | |
54 | | /* The machine-independent code doesn't know how to disambiguate |
55 | | mov (foo),r3 |
56 | | and |
57 | | mov (r2),r3 |
58 | | where 'foo' is a label. This helps it out. */ |
59 | | |
60 | | static const char * |
61 | | parse_mem8 (CGEN_CPU_DESC cd, |
62 | | const char **strp, |
63 | | int opindex, |
64 | | unsigned long *valuep) |
65 | 0 | { |
66 | 0 | if (**strp == '(') |
67 | 0 | { |
68 | 0 | const char *s = *strp; |
69 | |
|
70 | 0 | if (s[1] == '-' && s[2] == '-') |
71 | 0 | return _("Bad register in preincrement"); |
72 | | |
73 | 0 | while (ISALNUM (*++s)) |
74 | 0 | ; |
75 | 0 | if (s[0] == '+' && s[1] == '+' && (s[2] == ')' || s[2] == ',')) |
76 | 0 | return _("Bad register in postincrement"); |
77 | 0 | if (s[0] == ',' || s[0] == ')') |
78 | 0 | return _("Bad register name"); |
79 | 0 | } |
80 | 0 | else if (cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_gr_names, |
81 | 0 | (long *) valuep) == NULL) |
82 | 0 | return _("Label conflicts with register name"); |
83 | 0 | else if (strncasecmp (*strp, "rx,", 3) == 0 |
84 | 0 | || strncasecmp (*strp, "rxl,", 3) == 0 |
85 | 0 | || strncasecmp (*strp, "rxh,", 3) == 0) |
86 | 0 | return _("Label conflicts with `Rx'"); |
87 | 0 | else if (**strp == '#') |
88 | 0 | return _("Bad immediate expression"); |
89 | | |
90 | 0 | return cgen_parse_unsigned_integer (cd, strp, opindex, valuep); |
91 | 0 | } |
92 | | |
93 | | /* For the add and subtract instructions, there are two immediate forms, |
94 | | one for small operands and one for large ones. We want to use |
95 | | the small one when possible, but we do not want to generate relocs |
96 | | of the small size. This is somewhat tricky. */ |
97 | | |
98 | | static const char * |
99 | | parse_small_immediate (CGEN_CPU_DESC cd, |
100 | | const char **strp, |
101 | | int opindex, |
102 | | unsigned long *valuep) |
103 | 0 | { |
104 | 0 | bfd_vma value; |
105 | 0 | enum cgen_parse_operand_result result; |
106 | 0 | const char *errmsg; |
107 | |
|
108 | 0 | if (**strp == '@') |
109 | 0 | return _("No relocation for small immediate"); |
110 | | |
111 | 0 | errmsg = (* cd->parse_operand_fn) |
112 | 0 | (cd, CGEN_PARSE_OPERAND_INTEGER, strp, opindex, BFD_RELOC_NONE, |
113 | 0 | & result, & value); |
114 | |
|
115 | 0 | if (errmsg) |
116 | 0 | return errmsg; |
117 | | |
118 | 0 | if (result != CGEN_PARSE_OPERAND_RESULT_NUMBER) |
119 | 0 | return _("Small operand was not an immediate number"); |
120 | | |
121 | 0 | *valuep = value; |
122 | 0 | return NULL; |
123 | 0 | } |
124 | | |
125 | | /* Literal scan be either a normal literal, a @hi() or @lo relocation. */ |
126 | | |
127 | | static const char * |
128 | | parse_immediate16 (CGEN_CPU_DESC cd, |
129 | | const char **strp, |
130 | | int opindex, |
131 | | unsigned long *valuep) |
132 | 0 | { |
133 | 0 | const char *errmsg; |
134 | 0 | enum cgen_parse_operand_result result; |
135 | 0 | bfd_reloc_code_real_type code = BFD_RELOC_NONE; |
136 | 0 | bfd_vma value; |
137 | |
|
138 | 0 | if (strncmp (*strp, "@hi(", 4) == 0) |
139 | 0 | { |
140 | 0 | *strp += 4; |
141 | 0 | code = BFD_RELOC_HI16; |
142 | 0 | } |
143 | 0 | else |
144 | 0 | if (strncmp (*strp, "@lo(", 4) == 0) |
145 | 0 | { |
146 | 0 | *strp += 4; |
147 | 0 | code = BFD_RELOC_LO16; |
148 | 0 | } |
149 | |
|
150 | 0 | if (code == BFD_RELOC_NONE) |
151 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep); |
152 | 0 | else |
153 | 0 | { |
154 | 0 | errmsg = cgen_parse_address (cd, strp, opindex, code, &result, &value); |
155 | 0 | if ((errmsg == NULL) && |
156 | 0 | (result != CGEN_PARSE_OPERAND_RESULT_QUEUED)) |
157 | 0 | errmsg = _("Operand is not a symbol"); |
158 | |
|
159 | 0 | *valuep = value; |
160 | 0 | if ((code == BFD_RELOC_HI16 || code == BFD_RELOC_LO16) |
161 | 0 | && **strp == ')') |
162 | 0 | *strp += 1; |
163 | 0 | else |
164 | 0 | { |
165 | 0 | errmsg = _("Syntax error: No trailing ')'"); |
166 | 0 | return errmsg; |
167 | 0 | } |
168 | 0 | } |
169 | 0 | return errmsg; |
170 | 0 | } |
171 | | /* -- */ |
172 | | |
173 | | const char * xstormy16_cgen_parse_operand |
174 | | (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *); |
175 | | |
176 | | /* Main entry point for operand parsing. |
177 | | |
178 | | This function is basically just a big switch statement. Earlier versions |
179 | | used tables to look up the function to use, but |
180 | | - if the table contains both assembler and disassembler functions then |
181 | | the disassembler contains much of the assembler and vice-versa, |
182 | | - there's a lot of inlining possibilities as things grow, |
183 | | - using a switch statement avoids the function call overhead. |
184 | | |
185 | | This function could be moved into `parse_insn_normal', but keeping it |
186 | | separate makes clear the interface between `parse_insn_normal' and each of |
187 | | the handlers. */ |
188 | | |
189 | | const char * |
190 | | xstormy16_cgen_parse_operand (CGEN_CPU_DESC cd, |
191 | | int opindex, |
192 | | const char ** strp, |
193 | | CGEN_FIELDS * fields) |
194 | 0 | { |
195 | 0 | const char * errmsg = NULL; |
196 | | /* Used by scalar operands that still need to be parsed. */ |
197 | 0 | long junk ATTRIBUTE_UNUSED; |
198 | |
|
199 | 0 | switch (opindex) |
200 | 0 | { |
201 | 0 | case XSTORMY16_OPERAND_RB : |
202 | 0 | errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_gr_Rb_names, & fields->f_Rb); |
203 | 0 | break; |
204 | 0 | case XSTORMY16_OPERAND_RBJ : |
205 | 0 | errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_gr_Rb_names, & fields->f_Rbj); |
206 | 0 | break; |
207 | 0 | case XSTORMY16_OPERAND_RD : |
208 | 0 | errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_gr_names, & fields->f_Rd); |
209 | 0 | break; |
210 | 0 | case XSTORMY16_OPERAND_RDM : |
211 | 0 | errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_gr_names, & fields->f_Rdm); |
212 | 0 | break; |
213 | 0 | case XSTORMY16_OPERAND_RM : |
214 | 0 | errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_gr_names, & fields->f_Rm); |
215 | 0 | break; |
216 | 0 | case XSTORMY16_OPERAND_RS : |
217 | 0 | errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_gr_names, & fields->f_Rs); |
218 | 0 | break; |
219 | 0 | case XSTORMY16_OPERAND_ABS24 : |
220 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_ABS24, (unsigned long *) (& fields->f_abs24)); |
221 | 0 | break; |
222 | 0 | case XSTORMY16_OPERAND_BCOND2 : |
223 | 0 | errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_h_branchcond, & fields->f_op2); |
224 | 0 | break; |
225 | 0 | case XSTORMY16_OPERAND_BCOND5 : |
226 | 0 | errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_h_branchcond, & fields->f_op5); |
227 | 0 | break; |
228 | 0 | case XSTORMY16_OPERAND_HMEM8 : |
229 | 0 | errmsg = parse_mem8 (cd, strp, XSTORMY16_OPERAND_HMEM8, (unsigned long *) (& fields->f_hmem8)); |
230 | 0 | break; |
231 | 0 | case XSTORMY16_OPERAND_IMM12 : |
232 | 0 | errmsg = cgen_parse_signed_integer (cd, strp, XSTORMY16_OPERAND_IMM12, (long *) (& fields->f_imm12)); |
233 | 0 | break; |
234 | 0 | case XSTORMY16_OPERAND_IMM16 : |
235 | 0 | errmsg = parse_immediate16 (cd, strp, XSTORMY16_OPERAND_IMM16, (unsigned long *) (& fields->f_imm16)); |
236 | 0 | break; |
237 | 0 | case XSTORMY16_OPERAND_IMM2 : |
238 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_IMM2, (unsigned long *) (& fields->f_imm2)); |
239 | 0 | break; |
240 | 0 | case XSTORMY16_OPERAND_IMM3 : |
241 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_IMM3, (unsigned long *) (& fields->f_imm3)); |
242 | 0 | break; |
243 | 0 | case XSTORMY16_OPERAND_IMM3B : |
244 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_IMM3B, (unsigned long *) (& fields->f_imm3b)); |
245 | 0 | break; |
246 | 0 | case XSTORMY16_OPERAND_IMM4 : |
247 | 0 | errmsg = parse_small_immediate (cd, strp, XSTORMY16_OPERAND_IMM4, (unsigned long *) (& fields->f_imm4)); |
248 | 0 | break; |
249 | 0 | case XSTORMY16_OPERAND_IMM8 : |
250 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_IMM8, (unsigned long *) (& fields->f_imm8)); |
251 | 0 | break; |
252 | 0 | case XSTORMY16_OPERAND_IMM8SMALL : |
253 | 0 | errmsg = parse_small_immediate (cd, strp, XSTORMY16_OPERAND_IMM8SMALL, (unsigned long *) (& fields->f_imm8)); |
254 | 0 | break; |
255 | 0 | case XSTORMY16_OPERAND_LMEM8 : |
256 | 0 | errmsg = parse_mem8 (cd, strp, XSTORMY16_OPERAND_LMEM8, (unsigned long *) (& fields->f_lmem8)); |
257 | 0 | break; |
258 | 0 | case XSTORMY16_OPERAND_REL12 : |
259 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_REL12, (unsigned long *) (& fields->f_rel12)); |
260 | 0 | break; |
261 | 0 | case XSTORMY16_OPERAND_REL12A : |
262 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_REL12A, (unsigned long *) (& fields->f_rel12a)); |
263 | 0 | break; |
264 | 0 | case XSTORMY16_OPERAND_REL8_2 : |
265 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_REL8_2, (unsigned long *) (& fields->f_rel8_2)); |
266 | 0 | break; |
267 | 0 | case XSTORMY16_OPERAND_REL8_4 : |
268 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, XSTORMY16_OPERAND_REL8_4, (unsigned long *) (& fields->f_rel8_4)); |
269 | 0 | break; |
270 | 0 | case XSTORMY16_OPERAND_WS2 : |
271 | 0 | errmsg = cgen_parse_keyword (cd, strp, & xstormy16_cgen_opval_h_wordsize, & fields->f_op2m); |
272 | 0 | break; |
273 | | |
274 | 0 | default : |
275 | | /* xgettext:c-format */ |
276 | 0 | opcodes_error_handler |
277 | 0 | (_("internal error: unrecognized field %d while parsing"), |
278 | 0 | opindex); |
279 | 0 | abort (); |
280 | 0 | } |
281 | | |
282 | 0 | return errmsg; |
283 | 0 | } |
284 | | |
285 | | cgen_parse_fn * const xstormy16_cgen_parse_handlers[] = |
286 | | { |
287 | | parse_insn_normal, |
288 | | }; |
289 | | |
290 | | void |
291 | | xstormy16_cgen_init_asm (CGEN_CPU_DESC cd) |
292 | 0 | { |
293 | 0 | xstormy16_cgen_init_opcode_table (cd); |
294 | 0 | xstormy16_cgen_init_ibld_table (cd); |
295 | 0 | cd->parse_handlers = & xstormy16_cgen_parse_handlers[0]; |
296 | 0 | cd->parse_operand = xstormy16_cgen_parse_operand; |
297 | | #ifdef CGEN_ASM_INIT_HOOK |
298 | | CGEN_ASM_INIT_HOOK |
299 | | #endif |
300 | 0 | } |
301 | | |
302 | | |
303 | | |
304 | | /* Regex construction routine. |
305 | | |
306 | | This translates an opcode syntax string into a regex string, |
307 | | by replacing any non-character syntax element (such as an |
308 | | opcode) with the pattern '.*' |
309 | | |
310 | | It then compiles the regex and stores it in the opcode, for |
311 | | later use by xstormy16_cgen_assemble_insn |
312 | | |
313 | | Returns NULL for success, an error message for failure. */ |
314 | | |
315 | | char * |
316 | | xstormy16_cgen_build_insn_regex (CGEN_INSN *insn) |
317 | 268 | { |
318 | 268 | CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn); |
319 | 268 | const char *mnem = CGEN_INSN_MNEMONIC (insn); |
320 | 268 | char rxbuf[CGEN_MAX_RX_ELEMENTS]; |
321 | 268 | char *rx = rxbuf; |
322 | 268 | const CGEN_SYNTAX_CHAR_TYPE *syn; |
323 | 268 | int reg_err; |
324 | | |
325 | 268 | syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc)); |
326 | | |
327 | | /* Mnemonics come first in the syntax string. */ |
328 | 268 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) |
329 | 2 | return _("missing mnemonic in syntax string"); |
330 | 266 | ++syn; |
331 | | |
332 | | /* Generate a case sensitive regular expression that emulates case |
333 | | insensitive matching in the "C" locale. We cannot generate a case |
334 | | insensitive regular expression because in Turkish locales, 'i' and 'I' |
335 | | are not equal modulo case conversion. */ |
336 | | |
337 | | /* Copy the literal mnemonic out of the insn. */ |
338 | 1.16k | for (; *mnem; mnem++) |
339 | 900 | { |
340 | 900 | char c = *mnem; |
341 | | |
342 | 900 | if (ISALPHA (c)) |
343 | 866 | { |
344 | 866 | *rx++ = '['; |
345 | 866 | *rx++ = TOLOWER (c); |
346 | 866 | *rx++ = TOUPPER (c); |
347 | 866 | *rx++ = ']'; |
348 | 866 | } |
349 | 34 | else |
350 | 34 | *rx++ = c; |
351 | 900 | } |
352 | | |
353 | | /* Copy any remaining literals from the syntax string into the rx. */ |
354 | 1.61k | for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn) |
355 | 1.34k | { |
356 | 1.34k | if (CGEN_SYNTAX_CHAR_P (* syn)) |
357 | 806 | { |
358 | 806 | char c = CGEN_SYNTAX_CHAR (* syn); |
359 | | |
360 | 806 | switch (c) |
361 | 806 | { |
362 | | /* Escape any regex metacharacters in the syntax. */ |
363 | 0 | case '.': case '[': case '\\': |
364 | 0 | case '*': case '^': case '$': |
365 | |
|
366 | | #ifdef CGEN_ESCAPE_EXTENDED_REGEX |
367 | | case '?': case '{': case '}': |
368 | | case '(': case ')': case '*': |
369 | | case '|': case '+': case ']': |
370 | | #endif |
371 | 0 | *rx++ = '\\'; |
372 | 0 | *rx++ = c; |
373 | 0 | break; |
374 | | |
375 | 806 | default: |
376 | 806 | if (ISALPHA (c)) |
377 | 52 | { |
378 | 52 | *rx++ = '['; |
379 | 52 | *rx++ = TOLOWER (c); |
380 | 52 | *rx++ = TOUPPER (c); |
381 | 52 | *rx++ = ']'; |
382 | 52 | } |
383 | 754 | else |
384 | 754 | *rx++ = c; |
385 | 806 | break; |
386 | 806 | } |
387 | 806 | } |
388 | 542 | else |
389 | 542 | { |
390 | | /* Replace non-syntax fields with globs. */ |
391 | 542 | *rx++ = '.'; |
392 | 542 | *rx++ = '*'; |
393 | 542 | } |
394 | 1.34k | } |
395 | | |
396 | | /* Trailing whitespace ok. */ |
397 | 266 | * rx++ = '['; |
398 | 266 | * rx++ = ' '; |
399 | 266 | * rx++ = '\t'; |
400 | 266 | * rx++ = ']'; |
401 | 266 | * rx++ = '*'; |
402 | | |
403 | | /* But anchor it after that. */ |
404 | 266 | * rx++ = '$'; |
405 | 266 | * rx = '\0'; |
406 | | |
407 | 266 | CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t)); |
408 | 266 | reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB); |
409 | | |
410 | 266 | if (reg_err == 0) |
411 | 266 | return NULL; |
412 | 0 | else |
413 | 0 | { |
414 | 0 | static char msg[80]; |
415 | |
|
416 | 0 | regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80); |
417 | 0 | regfree ((regex_t *) CGEN_INSN_RX (insn)); |
418 | 0 | free (CGEN_INSN_RX (insn)); |
419 | 0 | (CGEN_INSN_RX (insn)) = NULL; |
420 | 0 | return msg; |
421 | 0 | } |
422 | 266 | } |
423 | | |
424 | | |
425 | | /* Default insn parser. |
426 | | |
427 | | The syntax string is scanned and operands are parsed and stored in FIELDS. |
428 | | Relocs are queued as we go via other callbacks. |
429 | | |
430 | | ??? Note that this is currently an all-or-nothing parser. If we fail to |
431 | | parse the instruction, we return 0 and the caller will start over from |
432 | | the beginning. Backtracking will be necessary in parsing subexpressions, |
433 | | but that can be handled there. Not handling backtracking here may get |
434 | | expensive in the case of the m68k. Deal with later. |
435 | | |
436 | | Returns NULL for success, an error message for failure. */ |
437 | | |
438 | | static const char * |
439 | | parse_insn_normal (CGEN_CPU_DESC cd, |
440 | | const CGEN_INSN *insn, |
441 | | const char **strp, |
442 | | CGEN_FIELDS *fields) |
443 | 0 | { |
444 | | /* ??? Runtime added insns not handled yet. */ |
445 | 0 | const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn); |
446 | 0 | const char *str = *strp; |
447 | 0 | const char *errmsg; |
448 | 0 | const char *p; |
449 | 0 | const CGEN_SYNTAX_CHAR_TYPE * syn; |
450 | 0 | #ifdef CGEN_MNEMONIC_OPERANDS |
451 | | /* FIXME: wip */ |
452 | 0 | int past_opcode_p; |
453 | 0 | #endif |
454 | | |
455 | | /* For now we assume the mnemonic is first (there are no leading operands). |
456 | | We can parse it without needing to set up operand parsing. |
457 | | GAS's input scrubber will ensure mnemonics are lowercase, but we may |
458 | | not be called from GAS. */ |
459 | 0 | p = CGEN_INSN_MNEMONIC (insn); |
460 | 0 | while (*p && TOLOWER (*p) == TOLOWER (*str)) |
461 | 0 | ++p, ++str; |
462 | |
|
463 | 0 | if (* p) |
464 | 0 | return _("unrecognized instruction"); |
465 | | |
466 | | #ifndef CGEN_MNEMONIC_OPERANDS |
467 | | if (* str && ! ISSPACE (* str)) |
468 | | return _("unrecognized instruction"); |
469 | | #endif |
470 | | |
471 | 0 | CGEN_INIT_PARSE (cd); |
472 | 0 | cgen_init_parse_operand (cd); |
473 | 0 | #ifdef CGEN_MNEMONIC_OPERANDS |
474 | 0 | past_opcode_p = 0; |
475 | 0 | #endif |
476 | | |
477 | | /* We don't check for (*str != '\0') here because we want to parse |
478 | | any trailing fake arguments in the syntax string. */ |
479 | 0 | syn = CGEN_SYNTAX_STRING (syntax); |
480 | | |
481 | | /* Mnemonics come first for now, ensure valid string. */ |
482 | 0 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) |
483 | 0 | abort (); |
484 | | |
485 | 0 | ++syn; |
486 | |
|
487 | 0 | while (* syn != 0) |
488 | 0 | { |
489 | | /* Non operand chars must match exactly. */ |
490 | 0 | if (CGEN_SYNTAX_CHAR_P (* syn)) |
491 | 0 | { |
492 | | /* FIXME: While we allow for non-GAS callers above, we assume the |
493 | | first char after the mnemonic part is a space. */ |
494 | | /* FIXME: We also take inappropriate advantage of the fact that |
495 | | GAS's input scrubber will remove extraneous blanks. */ |
496 | 0 | if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn))) |
497 | 0 | { |
498 | 0 | #ifdef CGEN_MNEMONIC_OPERANDS |
499 | 0 | if (CGEN_SYNTAX_CHAR(* syn) == ' ') |
500 | 0 | past_opcode_p = 1; |
501 | 0 | #endif |
502 | 0 | ++ syn; |
503 | 0 | ++ str; |
504 | 0 | } |
505 | 0 | else if (*str) |
506 | 0 | { |
507 | | /* Syntax char didn't match. Can't be this insn. */ |
508 | 0 | static char msg [80]; |
509 | | |
510 | | /* xgettext:c-format */ |
511 | 0 | sprintf (msg, _("syntax error (expected char `%c', found `%c')"), |
512 | 0 | CGEN_SYNTAX_CHAR(*syn), *str); |
513 | 0 | return msg; |
514 | 0 | } |
515 | 0 | else |
516 | 0 | { |
517 | | /* Ran out of input. */ |
518 | 0 | static char msg [80]; |
519 | | |
520 | | /* xgettext:c-format */ |
521 | 0 | sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"), |
522 | 0 | CGEN_SYNTAX_CHAR(*syn)); |
523 | 0 | return msg; |
524 | 0 | } |
525 | 0 | continue; |
526 | 0 | } |
527 | | |
528 | 0 | #ifdef CGEN_MNEMONIC_OPERANDS |
529 | 0 | (void) past_opcode_p; |
530 | 0 | #endif |
531 | | /* We have an operand of some sort. */ |
532 | 0 | errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields); |
533 | 0 | if (errmsg) |
534 | 0 | return errmsg; |
535 | | |
536 | | /* Done with this operand, continue with next one. */ |
537 | 0 | ++ syn; |
538 | 0 | } |
539 | | |
540 | | /* If we're at the end of the syntax string, we're done. */ |
541 | 0 | if (* syn == 0) |
542 | 0 | { |
543 | | /* FIXME: For the moment we assume a valid `str' can only contain |
544 | | blanks now. IE: We needn't try again with a longer version of |
545 | | the insn and it is assumed that longer versions of insns appear |
546 | | before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */ |
547 | 0 | while (ISSPACE (* str)) |
548 | 0 | ++ str; |
549 | |
|
550 | 0 | if (* str != '\0') |
551 | 0 | return _("junk at end of line"); /* FIXME: would like to include `str' */ |
552 | | |
553 | 0 | return NULL; |
554 | 0 | } |
555 | | |
556 | | /* We couldn't parse it. */ |
557 | 0 | return _("unrecognized instruction"); |
558 | 0 | } |
559 | | |
560 | | /* Main entry point. |
561 | | This routine is called for each instruction to be assembled. |
562 | | STR points to the insn to be assembled. |
563 | | We assume all necessary tables have been initialized. |
564 | | The assembled instruction, less any fixups, is stored in BUF. |
565 | | Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value |
566 | | still needs to be converted to target byte order, otherwise BUF is an array |
567 | | of bytes in target byte order. |
568 | | The result is a pointer to the insn's entry in the opcode table, |
569 | | or NULL if an error occured (an error message will have already been |
570 | | printed). |
571 | | |
572 | | Note that when processing (non-alias) macro-insns, |
573 | | this function recurses. |
574 | | |
575 | | ??? It's possible to make this cpu-independent. |
576 | | One would have to deal with a few minor things. |
577 | | At this point in time doing so would be more of a curiosity than useful |
578 | | [for example this file isn't _that_ big], but keeping the possibility in |
579 | | mind helps keep the design clean. */ |
580 | | |
581 | | const CGEN_INSN * |
582 | | xstormy16_cgen_assemble_insn (CGEN_CPU_DESC cd, |
583 | | const char *str, |
584 | | CGEN_FIELDS *fields, |
585 | | CGEN_INSN_BYTES_PTR buf, |
586 | | char **errmsg) |
587 | 0 | { |
588 | 0 | const char *start; |
589 | 0 | CGEN_INSN_LIST *ilist; |
590 | 0 | const char *parse_errmsg = NULL; |
591 | 0 | const char *insert_errmsg = NULL; |
592 | 0 | int recognized_mnemonic = 0; |
593 | | |
594 | | /* Skip leading white space. */ |
595 | 0 | while (ISSPACE (* str)) |
596 | 0 | ++ str; |
597 | | |
598 | | /* The instructions are stored in hashed lists. |
599 | | Get the first in the list. */ |
600 | 0 | ilist = CGEN_ASM_LOOKUP_INSN (cd, str); |
601 | | |
602 | | /* Keep looking until we find a match. */ |
603 | 0 | start = str; |
604 | 0 | for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist)) |
605 | 0 | { |
606 | 0 | const CGEN_INSN *insn = ilist->insn; |
607 | 0 | recognized_mnemonic = 1; |
608 | |
|
609 | | #ifdef CGEN_VALIDATE_INSN_SUPPORTED |
610 | | /* Not usually needed as unsupported opcodes |
611 | | shouldn't be in the hash lists. */ |
612 | | /* Is this insn supported by the selected cpu? */ |
613 | | if (! xstormy16_cgen_insn_supported (cd, insn)) |
614 | | continue; |
615 | | #endif |
616 | | /* If the RELAXED attribute is set, this is an insn that shouldn't be |
617 | | chosen immediately. Instead, it is used during assembler/linker |
618 | | relaxation if possible. */ |
619 | 0 | if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0) |
620 | 0 | continue; |
621 | | |
622 | 0 | str = start; |
623 | | |
624 | | /* Skip this insn if str doesn't look right lexically. */ |
625 | 0 | if (CGEN_INSN_RX (insn) != NULL && |
626 | 0 | regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH) |
627 | 0 | continue; |
628 | | |
629 | | /* Allow parse/insert handlers to obtain length of insn. */ |
630 | 0 | CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn); |
631 | |
|
632 | 0 | parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields); |
633 | 0 | if (parse_errmsg != NULL) |
634 | 0 | continue; |
635 | | |
636 | | /* ??? 0 is passed for `pc'. */ |
637 | 0 | insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf, |
638 | 0 | (bfd_vma) 0); |
639 | 0 | if (insert_errmsg != NULL) |
640 | 0 | continue; |
641 | | |
642 | | /* It is up to the caller to actually output the insn and any |
643 | | queued relocs. */ |
644 | 0 | return insn; |
645 | 0 | } |
646 | | |
647 | 0 | { |
648 | 0 | static char errbuf[150]; |
649 | 0 | const char *tmp_errmsg; |
650 | 0 | #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS |
651 | 0 | #define be_verbose 1 |
652 | | #else |
653 | | #define be_verbose 0 |
654 | | #endif |
655 | |
|
656 | 0 | if (be_verbose) |
657 | 0 | { |
658 | | /* If requesting verbose error messages, use insert_errmsg. |
659 | | Failing that, use parse_errmsg. */ |
660 | 0 | tmp_errmsg = (insert_errmsg ? insert_errmsg : |
661 | 0 | parse_errmsg ? parse_errmsg : |
662 | 0 | recognized_mnemonic ? |
663 | 0 | _("unrecognized form of instruction") : |
664 | 0 | _("unrecognized instruction")); |
665 | |
|
666 | 0 | if (strlen (start) > 50) |
667 | | /* xgettext:c-format */ |
668 | 0 | sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start); |
669 | 0 | else |
670 | | /* xgettext:c-format */ |
671 | 0 | sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start); |
672 | 0 | } |
673 | 0 | else |
674 | 0 | { |
675 | 0 | if (strlen (start) > 50) |
676 | | /* xgettext:c-format */ |
677 | 0 | sprintf (errbuf, _("bad instruction `%.50s...'"), start); |
678 | 0 | else |
679 | | /* xgettext:c-format */ |
680 | 0 | sprintf (errbuf, _("bad instruction `%.50s'"), start); |
681 | 0 | } |
682 | |
|
683 | 0 | *errmsg = errbuf; |
684 | 0 | return NULL; |
685 | 0 | } |
686 | 0 | } |