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