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