Commit | Line | Data |
---|---|---|
f6e6b40f BE |
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. |
f6e6b40f BE |
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" | |
f6e6b40f BE |
29 | #include <stdio.h> |
30 | #include "ansidecl.h" | |
31 | #include "bfd.h" | |
32 | #include "symcat.h" | |
33 | #include "@prefix@-desc.h" | |
34 | #include "@prefix@-opc.h" | |
35 | #include "opintl.h" | |
23969580 | 36 | #include "xregex.h" |
0e2ee3ca | 37 | #include "libiberty.h" |
37111cc7 | 38 | #include "safe-ctype.h" |
f6e6b40f | 39 | |
37111cc7 | 40 | #undef min |
f6e6b40f | 41 | #define min(a,b) ((a) < (b) ? (a) : (b)) |
37111cc7 | 42 | #undef max |
f6e6b40f BE |
43 | #define max(a,b) ((a) > (b) ? (a) : (b)) |
44 | ||
45 | static const char * parse_insn_normal | |
10e05405 | 46 | (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *); |
f6e6b40f | 47 | \f |
37111cc7 | 48 | /* -- assembler routines inserted here. */ |
23969580 JJ |
49 | \f |
50 | ||
37111cc7 | 51 | /* Regex construction routine. |
23969580 | 52 | |
37111cc7 NC |
53 | This translates an opcode syntax string into a regex string, |
54 | by replacing any non-character syntax element (such as an | |
55 | opcode) with the pattern '.*' | |
23969580 | 56 | |
37111cc7 NC |
57 | It then compiles the regex and stores it in the opcode, for |
58 | later use by @arch@_cgen_assemble_insn | |
23969580 | 59 | |
37111cc7 | 60 | Returns NULL for success, an error message for failure. */ |
23969580 JJ |
61 | |
62 | char * | |
10e05405 | 63 | @arch@_cgen_build_insn_regex (CGEN_INSN *insn) |
23969580 | 64 | { |
0e2ee3ca | 65 | CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn); |
23969580 | 66 | const char *mnem = CGEN_INSN_MNEMONIC (insn); |
23969580 JJ |
67 | char rxbuf[CGEN_MAX_RX_ELEMENTS]; |
68 | char *rx = rxbuf; | |
69 | const CGEN_SYNTAX_CHAR_TYPE *syn; | |
70 | int reg_err; | |
71 | ||
72 | syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc)); | |
73 | ||
f3a55c17 NC |
74 | /* Mnemonics come first in the syntax string. */ |
75 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) | |
76 | return _("missing mnemonic in syntax string"); | |
23969580 JJ |
77 | ++syn; |
78 | ||
f3a55c17 NC |
79 | /* Generate a case sensitive regular expression that emulates case |
80 | insensitive matching in the "C" locale. We cannot generate a case | |
81 | insensitive regular expression because in Turkish locales, 'i' and 'I' | |
82 | are not equal modulo case conversion. */ | |
83 | ||
84 | /* Copy the literal mnemonic out of the insn. */ | |
85 | for (; *mnem; mnem++) | |
86 | { | |
87 | char c = *mnem; | |
88 | ||
89 | if (ISALPHA (c)) | |
90 | { | |
91 | *rx++ = '['; | |
92 | *rx++ = TOLOWER (c); | |
93 | *rx++ = TOUPPER (c); | |
94 | *rx++ = ']'; | |
95 | } | |
96 | else | |
97 | *rx++ = c; | |
98 | } | |
23969580 | 99 | |
f3a55c17 NC |
100 | /* Copy any remaining literals from the syntax string into the rx. */ |
101 | for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn) | |
23969580 JJ |
102 | { |
103 | if (CGEN_SYNTAX_CHAR_P (* syn)) | |
104 | { | |
f3a55c17 NC |
105 | char c = CGEN_SYNTAX_CHAR (* syn); |
106 | ||
107 | switch (c) | |
108 | { | |
109 | /* Escape any regex metacharacters in the syntax. */ | |
110 | case '.': case '[': case '\\': | |
111 | case '*': case '^': case '$': | |
23969580 JJ |
112 | |
113 | #ifdef CGEN_ESCAPE_EXTENDED_REGEX | |
f3a55c17 NC |
114 | case '?': case '{': case '}': |
115 | case '(': case ')': case '*': | |
116 | case '|': case '+': case ']': | |
23969580 | 117 | #endif |
f3a55c17 NC |
118 | *rx++ = '\\'; |
119 | *rx++ = c; | |
120 | break; | |
121 | ||
122 | default: | |
123 | if (ISALPHA (c)) | |
124 | { | |
125 | *rx++ = '['; | |
126 | *rx++ = TOLOWER (c); | |
127 | *rx++ = TOUPPER (c); | |
128 | *rx++ = ']'; | |
129 | } | |
130 | else | |
131 | *rx++ = c; | |
132 | break; | |
133 | } | |
23969580 JJ |
134 | } |
135 | else | |
136 | { | |
f3a55c17 NC |
137 | /* Replace non-syntax fields with globs. */ |
138 | *rx++ = '.'; | |
139 | *rx++ = '*'; | |
23969580 JJ |
140 | } |
141 | } | |
142 | ||
f3a55c17 | 143 | /* Trailing whitespace ok. */ |
23969580 JJ |
144 | * rx++ = '['; |
145 | * rx++ = ' '; | |
146 | * rx++ = '\t'; | |
147 | * rx++ = ']'; | |
148 | * rx++ = '*'; | |
149 | ||
f3a55c17 | 150 | /* But anchor it after that. */ |
23969580 JJ |
151 | * rx++ = '$'; |
152 | * rx = '\0'; | |
153 | ||
154 | CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t)); | |
f3a55c17 | 155 | reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB); |
23969580 JJ |
156 | |
157 | if (reg_err == 0) | |
158 | return NULL; | |
159 | else | |
160 | { | |
161 | static char msg[80]; | |
f3a55c17 | 162 | |
23969580 JJ |
163 | regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80); |
164 | regfree ((regex_t *) CGEN_INSN_RX (insn)); | |
165 | free (CGEN_INSN_RX (insn)); | |
166 | (CGEN_INSN_RX (insn)) = NULL; | |
37111cc7 | 167 | return msg; |
23969580 JJ |
168 | } |
169 | } | |
170 | ||
f6e6b40f BE |
171 | \f |
172 | /* Default insn parser. | |
173 | ||
174 | The syntax string is scanned and operands are parsed and stored in FIELDS. | |
175 | Relocs are queued as we go via other callbacks. | |
176 | ||
177 | ??? Note that this is currently an all-or-nothing parser. If we fail to | |
178 | parse the instruction, we return 0 and the caller will start over from | |
179 | the beginning. Backtracking will be necessary in parsing subexpressions, | |
180 | but that can be handled there. Not handling backtracking here may get | |
181 | expensive in the case of the m68k. Deal with later. | |
182 | ||
f3a55c17 | 183 | Returns NULL for success, an error message for failure. */ |
f6e6b40f BE |
184 | |
185 | static const char * | |
10e05405 MM |
186 | parse_insn_normal (CGEN_CPU_DESC cd, |
187 | const CGEN_INSN *insn, | |
188 | const char **strp, | |
189 | CGEN_FIELDS *fields) | |
f6e6b40f BE |
190 | { |
191 | /* ??? Runtime added insns not handled yet. */ | |
192 | const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn); | |
193 | const char *str = *strp; | |
194 | const char *errmsg; | |
195 | const char *p; | |
4a9f416d | 196 | const CGEN_SYNTAX_CHAR_TYPE * syn; |
f6e6b40f BE |
197 | #ifdef CGEN_MNEMONIC_OPERANDS |
198 | /* FIXME: wip */ | |
199 | int past_opcode_p; | |
200 | #endif | |
201 | ||
202 | /* For now we assume the mnemonic is first (there are no leading operands). | |
203 | We can parse it without needing to set up operand parsing. | |
204 | GAS's input scrubber will ensure mnemonics are lowercase, but we may | |
205 | not be called from GAS. */ | |
206 | p = CGEN_INSN_MNEMONIC (insn); | |
37111cc7 | 207 | while (*p && TOLOWER (*p) == TOLOWER (*str)) |
f6e6b40f BE |
208 | ++p, ++str; |
209 | ||
210 | if (* p) | |
211 | return _("unrecognized instruction"); | |
212 | ||
213 | #ifndef CGEN_MNEMONIC_OPERANDS | |
37111cc7 | 214 | if (* str && ! ISSPACE (* str)) |
f6e6b40f BE |
215 | return _("unrecognized instruction"); |
216 | #endif | |
217 | ||
218 | CGEN_INIT_PARSE (cd); | |
219 | cgen_init_parse_operand (cd); | |
220 | #ifdef CGEN_MNEMONIC_OPERANDS | |
221 | past_opcode_p = 0; | |
222 | #endif | |
223 | ||
224 | /* We don't check for (*str != '\0') here because we want to parse | |
225 | any trailing fake arguments in the syntax string. */ | |
226 | syn = CGEN_SYNTAX_STRING (syntax); | |
227 | ||
228 | /* Mnemonics come first for now, ensure valid string. */ | |
229 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) | |
230 | abort (); | |
231 | ||
232 | ++syn; | |
233 | ||
234 | while (* syn != 0) | |
235 | { | |
236 | /* Non operand chars must match exactly. */ | |
237 | if (CGEN_SYNTAX_CHAR_P (* syn)) | |
238 | { | |
239 | /* FIXME: While we allow for non-GAS callers above, we assume the | |
240 | first char after the mnemonic part is a space. */ | |
241 | /* FIXME: We also take inappropriate advantage of the fact that | |
242 | GAS's input scrubber will remove extraneous blanks. */ | |
37111cc7 | 243 | if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn))) |
f6e6b40f BE |
244 | { |
245 | #ifdef CGEN_MNEMONIC_OPERANDS | |
4a9f416d | 246 | if (CGEN_SYNTAX_CHAR(* syn) == ' ') |
f6e6b40f BE |
247 | past_opcode_p = 1; |
248 | #endif | |
249 | ++ syn; | |
250 | ++ str; | |
251 | } | |
149fe25e | 252 | else if (*str) |
f6e6b40f BE |
253 | { |
254 | /* Syntax char didn't match. Can't be this insn. */ | |
255 | static char msg [80]; | |
f3a55c17 | 256 | |
f6e6b40f BE |
257 | /* xgettext:c-format */ |
258 | sprintf (msg, _("syntax error (expected char `%c', found `%c')"), | |
4a9f416d | 259 | CGEN_SYNTAX_CHAR(*syn), *str); |
f6e6b40f BE |
260 | return msg; |
261 | } | |
149fe25e FCE |
262 | else |
263 | { | |
264 | /* Ran out of input. */ | |
265 | static char msg [80]; | |
f3a55c17 | 266 | |
149fe25e FCE |
267 | /* xgettext:c-format */ |
268 | sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"), | |
4a9f416d | 269 | CGEN_SYNTAX_CHAR(*syn)); |
149fe25e FCE |
270 | return msg; |
271 | } | |
f6e6b40f BE |
272 | continue; |
273 | } | |
274 | ||
275 | /* We have an operand of some sort. */ | |
d7de8249 | 276 | errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), |
f6e6b40f BE |
277 | &str, fields); |
278 | if (errmsg) | |
279 | return errmsg; | |
280 | ||
281 | /* Done with this operand, continue with next one. */ | |
282 | ++ syn; | |
283 | } | |
284 | ||
285 | /* If we're at the end of the syntax string, we're done. */ | |
4a9f416d | 286 | if (* syn == 0) |
f6e6b40f BE |
287 | { |
288 | /* FIXME: For the moment we assume a valid `str' can only contain | |
289 | blanks now. IE: We needn't try again with a longer version of | |
290 | the insn and it is assumed that longer versions of insns appear | |
291 | before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */ | |
37111cc7 | 292 | while (ISSPACE (* str)) |
f6e6b40f BE |
293 | ++ str; |
294 | ||
295 | if (* str != '\0') | |
296 | return _("junk at end of line"); /* FIXME: would like to include `str' */ | |
297 | ||
298 | return NULL; | |
299 | } | |
300 | ||
301 | /* We couldn't parse it. */ | |
302 | return _("unrecognized instruction"); | |
303 | } | |
304 | \f | |
305 | /* Main entry point. | |
306 | This routine is called for each instruction to be assembled. | |
307 | STR points to the insn to be assembled. | |
308 | We assume all necessary tables have been initialized. | |
309 | The assembled instruction, less any fixups, is stored in BUF. | |
310 | Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value | |
311 | still needs to be converted to target byte order, otherwise BUF is an array | |
312 | of bytes in target byte order. | |
313 | The result is a pointer to the insn's entry in the opcode table, | |
314 | or NULL if an error occured (an error message will have already been | |
315 | printed). | |
316 | ||
317 | Note that when processing (non-alias) macro-insns, | |
318 | this function recurses. | |
319 | ||
320 | ??? It's possible to make this cpu-independent. | |
321 | One would have to deal with a few minor things. | |
322 | At this point in time doing so would be more of a curiosity than useful | |
323 | [for example this file isn't _that_ big], but keeping the possibility in | |
324 | mind helps keep the design clean. */ | |
325 | ||
326 | const CGEN_INSN * | |
10e05405 MM |
327 | @arch@_cgen_assemble_insn (CGEN_CPU_DESC cd, |
328 | const char *str, | |
329 | CGEN_FIELDS *fields, | |
330 | CGEN_INSN_BYTES_PTR buf, | |
331 | char **errmsg) | |
f6e6b40f BE |
332 | { |
333 | const char *start; | |
334 | CGEN_INSN_LIST *ilist; | |
606d55bc FCE |
335 | const char *parse_errmsg = NULL; |
336 | const char *insert_errmsg = NULL; | |
23969580 | 337 | int recognized_mnemonic = 0; |
f6e6b40f BE |
338 | |
339 | /* Skip leading white space. */ | |
37111cc7 | 340 | while (ISSPACE (* str)) |
f6e6b40f BE |
341 | ++ str; |
342 | ||
343 | /* The instructions are stored in hashed lists. | |
344 | Get the first in the list. */ | |
345 | ilist = CGEN_ASM_LOOKUP_INSN (cd, str); | |
346 | ||
347 | /* Keep looking until we find a match. */ | |
f6e6b40f BE |
348 | start = str; |
349 | for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist)) | |
350 | { | |
351 | const CGEN_INSN *insn = ilist->insn; | |
23969580 | 352 | recognized_mnemonic = 1; |
f6e6b40f BE |
353 | |
354 | #ifdef CGEN_VALIDATE_INSN_SUPPORTED | |
f3a55c17 NC |
355 | /* Not usually needed as unsupported opcodes |
356 | shouldn't be in the hash lists. */ | |
f6e6b40f BE |
357 | /* Is this insn supported by the selected cpu? */ |
358 | if (! @arch@_cgen_insn_supported (cd, insn)) | |
359 | continue; | |
360 | #endif | |
b11dcf4e | 361 | /* If the RELAXED attribute is set, this is an insn that shouldn't be |
f6e6b40f BE |
362 | chosen immediately. Instead, it is used during assembler/linker |
363 | relaxation if possible. */ | |
b11dcf4e | 364 | if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0) |
f6e6b40f BE |
365 | continue; |
366 | ||
367 | str = start; | |
368 | ||
f3a55c17 | 369 | /* Skip this insn if str doesn't look right lexically. */ |
23969580 JJ |
370 | if (CGEN_INSN_RX (insn) != NULL && |
371 | regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH) | |
372 | continue; | |
373 | ||
f6e6b40f BE |
374 | /* Allow parse/insert handlers to obtain length of insn. */ |
375 | CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn); | |
376 | ||
606d55bc FCE |
377 | parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields); |
378 | if (parse_errmsg != NULL) | |
f6e6b40f BE |
379 | continue; |
380 | ||
f3a55c17 | 381 | /* ??? 0 is passed for `pc'. */ |
606d55bc FCE |
382 | insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf, |
383 | (bfd_vma) 0); | |
384 | if (insert_errmsg != NULL) | |
f6e6b40f BE |
385 | continue; |
386 | ||
387 | /* It is up to the caller to actually output the insn and any | |
388 | queued relocs. */ | |
389 | return insn; | |
390 | } | |
391 | ||
f6e6b40f BE |
392 | { |
393 | static char errbuf[150]; | |
fca2040b | 394 | #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS |
606d55bc | 395 | const char *tmp_errmsg; |
f6e6b40f | 396 | |
606d55bc | 397 | /* If requesting verbose error messages, use insert_errmsg. |
f3a55c17 | 398 | Failing that, use parse_errmsg. */ |
606d55bc FCE |
399 | tmp_errmsg = (insert_errmsg ? insert_errmsg : |
400 | parse_errmsg ? parse_errmsg : | |
f3a55c17 NC |
401 | recognized_mnemonic ? |
402 | _("unrecognized form of instruction") : | |
606d55bc FCE |
403 | _("unrecognized instruction")); |
404 | ||
f6e6b40f BE |
405 | if (strlen (start) > 50) |
406 | /* xgettext:c-format */ | |
407 | sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start); | |
408 | else | |
409 | /* xgettext:c-format */ | |
410 | sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start); | |
411 | #else | |
412 | if (strlen (start) > 50) | |
413 | /* xgettext:c-format */ | |
414 | sprintf (errbuf, _("bad instruction `%.50s...'"), start); | |
415 | else | |
416 | /* xgettext:c-format */ | |
417 | sprintf (errbuf, _("bad instruction `%.50s'"), start); | |
418 | #endif | |
419 | ||
420 | *errmsg = errbuf; | |
421 | return NULL; | |
422 | } | |
423 | } | |
424 | \f | |
425 | #if 0 /* This calls back to GAS which we can't do without care. */ | |
426 | ||
427 | /* Record each member of OPVALS in the assembler's symbol table. | |
428 | This lets GAS parse registers for us. | |
429 | ??? Interesting idea but not currently used. */ | |
430 | ||
431 | /* Record each member of OPVALS in the assembler's symbol table. | |
432 | FIXME: Not currently used. */ | |
433 | ||
434 | void | |
10e05405 | 435 | @arch@_cgen_asm_hash_keywords (CGEN_CPU_DESC cd, CGEN_KEYWORD *opvals) |
f6e6b40f BE |
436 | { |
437 | CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL); | |
438 | const CGEN_KEYWORD_ENTRY * ke; | |
439 | ||
440 | while ((ke = cgen_keyword_search_next (& search)) != NULL) | |
441 | { | |
442 | #if 0 /* Unnecessary, should be done in the search routine. */ | |
443 | if (! @arch@_cgen_opval_supported (ke)) | |
444 | continue; | |
445 | #endif | |
446 | cgen_asm_record_register (cd, ke->name, ke->value); | |
447 | } | |
448 | } | |
449 | ||
450 | #endif /* 0 */ |