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