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