* elf32-ppc.c (SYMBOL_REFERENCES_LOCAL): Expand comment.
[deliverable/binutils-gdb.git] / opcodes / cgen-asm.in
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
7 Copyright 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
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 <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"
36 #include "xregex.h"
37 #include "libiberty.h"
38 #include "safe-ctype.h"
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. */
49 \f
50
51 /* Regex construction routine.
52
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 '.*'
56
57 It then compiles the regex and stores it in the opcode, for
58 later use by @arch@_cgen_assemble_insn
59
60 Returns NULL for success, an error message for failure. */
61
62 char *
63 @arch@_cgen_build_insn_regex (insn)
64 CGEN_INSN *insn;
65 {
66 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
67 const char *mnem = CGEN_INSN_MNEMONIC (insn);
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
75 /* Mnemonics come first in the syntax string. */
76 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
77 return _("missing mnemonic in syntax string");
78 ++syn;
79
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 }
100
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)
103 {
104 if (CGEN_SYNTAX_CHAR_P (* syn))
105 {
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 '$':
113
114 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
115 case '?': case '{': case '}':
116 case '(': case ')': case '*':
117 case '|': case '+': case ']':
118 #endif
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 }
135 }
136 else
137 {
138 /* Replace non-syntax fields with globs. */
139 *rx++ = '.';
140 *rx++ = '*';
141 }
142 }
143
144 /* Trailing whitespace ok. */
145 * rx++ = '[';
146 * rx++ = ' ';
147 * rx++ = '\t';
148 * rx++ = ']';
149 * rx++ = '*';
150
151 /* But anchor it after that. */
152 * rx++ = '$';
153 * rx = '\0';
154
155 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
156 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
157
158 if (reg_err == 0)
159 return NULL;
160 else
161 {
162 static char msg[80];
163
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;
168 return msg;
169 }
170 }
171
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
184 Returns NULL for success, an error message for failure. */
185
186 static const char *
187 parse_insn_normal (cd, insn, strp, fields)
188 CGEN_CPU_DESC cd;
189 const CGEN_INSN *insn;
190 const char **strp;
191 CGEN_FIELDS *fields;
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;
198 const CGEN_SYNTAX_CHAR_TYPE * syn;
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);
209 while (*p && TOLOWER (*p) == TOLOWER (*str))
210 ++p, ++str;
211
212 if (* p)
213 return _("unrecognized instruction");
214
215 #ifndef CGEN_MNEMONIC_OPERANDS
216 if (* str && ! ISSPACE (* str))
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. */
245 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
246 {
247 #ifdef CGEN_MNEMONIC_OPERANDS
248 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
249 past_opcode_p = 1;
250 #endif
251 ++ syn;
252 ++ str;
253 }
254 else if (*str)
255 {
256 /* Syntax char didn't match. Can't be this insn. */
257 static char msg [80];
258
259 /* xgettext:c-format */
260 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
261 CGEN_SYNTAX_CHAR(*syn), *str);
262 return msg;
263 }
264 else
265 {
266 /* Ran out of input. */
267 static char msg [80];
268
269 /* xgettext:c-format */
270 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
271 CGEN_SYNTAX_CHAR(*syn));
272 return msg;
273 }
274 continue;
275 }
276
277 /* We have an operand of some sort. */
278 errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
279 &str, fields);
280 if (errmsg)
281 return errmsg;
282
283 /* Done with this operand, continue with next one. */
284 ++ syn;
285 }
286
287 /* If we're at the end of the syntax string, we're done. */
288 if (* syn == 0)
289 {
290 /* FIXME: For the moment we assume a valid `str' can only contain
291 blanks now. IE: We needn't try again with a longer version of
292 the insn and it is assumed that longer versions of insns appear
293 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
294 while (ISSPACE (* str))
295 ++ str;
296
297 if (* str != '\0')
298 return _("junk at end of line"); /* FIXME: would like to include `str' */
299
300 return NULL;
301 }
302
303 /* We couldn't parse it. */
304 return _("unrecognized instruction");
305 }
306 \f
307 /* Main entry point.
308 This routine is called for each instruction to be assembled.
309 STR points to the insn to be assembled.
310 We assume all necessary tables have been initialized.
311 The assembled instruction, less any fixups, is stored in BUF.
312 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
313 still needs to be converted to target byte order, otherwise BUF is an array
314 of bytes in target byte order.
315 The result is a pointer to the insn's entry in the opcode table,
316 or NULL if an error occured (an error message will have already been
317 printed).
318
319 Note that when processing (non-alias) macro-insns,
320 this function recurses.
321
322 ??? It's possible to make this cpu-independent.
323 One would have to deal with a few minor things.
324 At this point in time doing so would be more of a curiosity than useful
325 [for example this file isn't _that_ big], but keeping the possibility in
326 mind helps keep the design clean. */
327
328 const CGEN_INSN *
329 @arch@_cgen_assemble_insn (cd, str, fields, buf, errmsg)
330 CGEN_CPU_DESC cd;
331 const char *str;
332 CGEN_FIELDS *fields;
333 CGEN_INSN_BYTES_PTR buf;
334 char **errmsg;
335 {
336 const char *start;
337 CGEN_INSN_LIST *ilist;
338 const char *parse_errmsg = NULL;
339 const char *insert_errmsg = NULL;
340 int recognized_mnemonic = 0;
341
342 /* Skip leading white space. */
343 while (ISSPACE (* str))
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. */
351 start = str;
352 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
353 {
354 const CGEN_INSN *insn = ilist->insn;
355 recognized_mnemonic = 1;
356
357 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
358 /* Not usually needed as unsupported opcodes
359 shouldn't be in the hash lists. */
360 /* Is this insn supported by the selected cpu? */
361 if (! @arch@_cgen_insn_supported (cd, insn))
362 continue;
363 #endif
364 /* If the RELAX attribute is set, this is an insn that shouldn't be
365 chosen immediately. Instead, it is used during assembler/linker
366 relaxation if possible. */
367 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAX) != 0)
368 continue;
369
370 str = start;
371
372 /* Skip this insn if str doesn't look right lexically. */
373 if (CGEN_INSN_RX (insn) != NULL &&
374 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
375 continue;
376
377 /* Allow parse/insert handlers to obtain length of insn. */
378 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
379
380 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
381 if (parse_errmsg != NULL)
382 continue;
383
384 /* ??? 0 is passed for `pc'. */
385 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
386 (bfd_vma) 0);
387 if (insert_errmsg != NULL)
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
395 {
396 static char errbuf[150];
397 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
398 const char *tmp_errmsg;
399
400 /* If requesting verbose error messages, use insert_errmsg.
401 Failing that, use parse_errmsg. */
402 tmp_errmsg = (insert_errmsg ? insert_errmsg :
403 parse_errmsg ? parse_errmsg :
404 recognized_mnemonic ?
405 _("unrecognized form of instruction") :
406 _("unrecognized instruction"));
407
408 if (strlen (start) > 50)
409 /* xgettext:c-format */
410 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
411 else
412 /* xgettext:c-format */
413 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
414 #else
415 if (strlen (start) > 50)
416 /* xgettext:c-format */
417 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
418 else
419 /* xgettext:c-format */
420 sprintf (errbuf, _("bad instruction `%.50s'"), start);
421 #endif
422
423 *errmsg = errbuf;
424 return NULL;
425 }
426 }
427 \f
428 #if 0 /* This calls back to GAS which we can't do without care. */
429
430 /* Record each member of OPVALS in the assembler's symbol table.
431 This lets GAS parse registers for us.
432 ??? Interesting idea but not currently used. */
433
434 /* Record each member of OPVALS in the assembler's symbol table.
435 FIXME: Not currently used. */
436
437 void
438 @arch@_cgen_asm_hash_keywords (cd, opvals)
439 CGEN_CPU_DESC cd;
440 CGEN_KEYWORD *opvals;
441 {
442 CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL);
443 const CGEN_KEYWORD_ENTRY * ke;
444
445 while ((ke = cgen_keyword_search_next (& search)) != NULL)
446 {
447 #if 0 /* Unnecessary, should be done in the search routine. */
448 if (! @arch@_cgen_opval_supported (ke))
449 continue;
450 #endif
451 cgen_asm_record_register (cd, ke->name, ke->value);
452 }
453 }
454
455 #endif /* 0 */
This page took 0.040188 seconds and 4 git commands to generate.