daily update
[binutils.git] / opcodes / openrisc-asm.c
blob8aaf3746119b37ff6b865a6474ce2e92d232602a
1 /* Assembler interface for targets using CGEN. -*- C -*-
2 CGEN: Cpu tools GENerator
4 THIS FILE IS MACHINE GENERATED WITH CGEN.
5 - the resultant file is machine generated, cgen-asm.in isn't
7 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2005, 2007, 2008, 2010
8 Free Software Foundation, Inc.
10 This file is part of libopcodes.
12 This library is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
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.
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. */
27 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
28 Keep that in mind. */
30 #include "sysdep.h"
31 #include <stdio.h>
32 #include "ansidecl.h"
33 #include "bfd.h"
34 #include "symcat.h"
35 #include "openrisc-desc.h"
36 #include "openrisc-opc.h"
37 #include "opintl.h"
38 #include "xregex.h"
39 #include "libiberty.h"
40 #include "safe-ctype.h"
42 #undef min
43 #define min(a,b) ((a) < (b) ? (a) : (b))
44 #undef max
45 #define max(a,b) ((a) > (b) ? (a) : (b))
47 static const char * parse_insn_normal
48 (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
50 /* -- assembler routines inserted here. */
52 /* -- asm.c */
54 static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'");
56 #define CGEN_VERBOSE_ASSEMBLER_ERRORS
58 long
59 openrisc_sign_extend_16bit (long value)
61 return ((value & 0xffff) ^ 0x8000) - 0x8000;
64 /* Handle hi(). */
66 static const char *
67 parse_hi16 (CGEN_CPU_DESC cd, const char ** strp, int opindex, long * valuep)
69 const char *errmsg;
70 enum cgen_parse_operand_result result_type;
71 unsigned long ret;
73 if (**strp == '#')
74 ++*strp;
76 if (strncasecmp (*strp, "hi(", 3) == 0)
78 bfd_vma value;
80 *strp += 3;
81 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_HI16,
82 & result_type, & value);
83 if (**strp != ')')
84 return MISSING_CLOSING_PARENTHESIS;
86 ++*strp;
87 if (errmsg == NULL
88 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
89 value >>= 16;
90 ret = value;
92 else
94 if (**strp == '-')
96 long value;
98 errmsg = cgen_parse_signed_integer (cd, strp, opindex, &value);
99 ret = value;
101 else
103 unsigned long value;
105 errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, &value);
106 ret = value;
110 *valuep = ((ret & 0xffff) ^ 0x8000) - 0x8000;
111 return errmsg;
114 /* Handle lo(). */
116 static const char *
117 parse_lo16 (CGEN_CPU_DESC cd, const char ** strp, int opindex, long * valuep)
119 const char *errmsg;
120 enum cgen_parse_operand_result result_type;
121 unsigned long ret;
123 if (**strp == '#')
124 ++*strp;
126 if (strncasecmp (*strp, "lo(", 3) == 0)
128 bfd_vma value;
130 *strp += 3;
131 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LO16,
132 & result_type, & value);
133 if (**strp != ')')
134 return MISSING_CLOSING_PARENTHESIS;
136 ++*strp;
137 ret = value;
139 else
141 if (**strp == '-')
143 long value;
145 errmsg = cgen_parse_signed_integer (cd, strp, opindex, &value);
146 ret = value;
148 else
150 unsigned long value;
152 errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, &value);
153 ret = value;
157 *valuep = ((ret & 0xffff) ^ 0x8000) - 0x8000;
158 return errmsg;
161 /* -- */
163 const char * openrisc_cgen_parse_operand
164 (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
166 /* Main entry point for operand parsing.
168 This function is basically just a big switch statement. Earlier versions
169 used tables to look up the function to use, but
170 - if the table contains both assembler and disassembler functions then
171 the disassembler contains much of the assembler and vice-versa,
172 - there's a lot of inlining possibilities as things grow,
173 - using a switch statement avoids the function call overhead.
175 This function could be moved into `parse_insn_normal', but keeping it
176 separate makes clear the interface between `parse_insn_normal' and each of
177 the handlers. */
179 const char *
180 openrisc_cgen_parse_operand (CGEN_CPU_DESC cd,
181 int opindex,
182 const char ** strp,
183 CGEN_FIELDS * fields)
185 const char * errmsg = NULL;
186 /* Used by scalar operands that still need to be parsed. */
187 long junk ATTRIBUTE_UNUSED;
189 switch (opindex)
191 case OPENRISC_OPERAND_ABS_26 :
193 bfd_vma value = 0;
194 errmsg = cgen_parse_address (cd, strp, OPENRISC_OPERAND_ABS_26, 0, NULL, & value);
195 fields->f_abs26 = value;
197 break;
198 case OPENRISC_OPERAND_DISP_26 :
200 bfd_vma value = 0;
201 errmsg = cgen_parse_address (cd, strp, OPENRISC_OPERAND_DISP_26, 0, NULL, & value);
202 fields->f_disp26 = value;
204 break;
205 case OPENRISC_OPERAND_HI16 :
206 errmsg = parse_hi16 (cd, strp, OPENRISC_OPERAND_HI16, (long *) (& fields->f_simm16));
207 break;
208 case OPENRISC_OPERAND_LO16 :
209 errmsg = parse_lo16 (cd, strp, OPENRISC_OPERAND_LO16, (long *) (& fields->f_lo16));
210 break;
211 case OPENRISC_OPERAND_OP_F_23 :
212 errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_OP_F_23, (unsigned long *) (& fields->f_op4));
213 break;
214 case OPENRISC_OPERAND_OP_F_3 :
215 errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_OP_F_3, (unsigned long *) (& fields->f_op5));
216 break;
217 case OPENRISC_OPERAND_RA :
218 errmsg = cgen_parse_keyword (cd, strp, & openrisc_cgen_opval_h_gr, & fields->f_r2);
219 break;
220 case OPENRISC_OPERAND_RB :
221 errmsg = cgen_parse_keyword (cd, strp, & openrisc_cgen_opval_h_gr, & fields->f_r3);
222 break;
223 case OPENRISC_OPERAND_RD :
224 errmsg = cgen_parse_keyword (cd, strp, & openrisc_cgen_opval_h_gr, & fields->f_r1);
225 break;
226 case OPENRISC_OPERAND_SIMM_16 :
227 errmsg = cgen_parse_signed_integer (cd, strp, OPENRISC_OPERAND_SIMM_16, (long *) (& fields->f_simm16));
228 break;
229 case OPENRISC_OPERAND_UI16NC :
230 errmsg = parse_lo16 (cd, strp, OPENRISC_OPERAND_UI16NC, (long *) (& fields->f_i16nc));
231 break;
232 case OPENRISC_OPERAND_UIMM_16 :
233 errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_UIMM_16, (unsigned long *) (& fields->f_uimm16));
234 break;
235 case OPENRISC_OPERAND_UIMM_5 :
236 errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_UIMM_5, (unsigned long *) (& fields->f_uimm5));
237 break;
239 default :
240 /* xgettext:c-format */
241 fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
242 abort ();
245 return errmsg;
248 cgen_parse_fn * const openrisc_cgen_parse_handlers[] =
250 parse_insn_normal,
253 void
254 openrisc_cgen_init_asm (CGEN_CPU_DESC cd)
256 openrisc_cgen_init_opcode_table (cd);
257 openrisc_cgen_init_ibld_table (cd);
258 cd->parse_handlers = & openrisc_cgen_parse_handlers[0];
259 cd->parse_operand = openrisc_cgen_parse_operand;
260 #ifdef CGEN_ASM_INIT_HOOK
261 CGEN_ASM_INIT_HOOK
262 #endif
267 /* Regex construction routine.
269 This translates an opcode syntax string into a regex string,
270 by replacing any non-character syntax element (such as an
271 opcode) with the pattern '.*'
273 It then compiles the regex and stores it in the opcode, for
274 later use by openrisc_cgen_assemble_insn
276 Returns NULL for success, an error message for failure. */
278 char *
279 openrisc_cgen_build_insn_regex (CGEN_INSN *insn)
281 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
282 const char *mnem = CGEN_INSN_MNEMONIC (insn);
283 char rxbuf[CGEN_MAX_RX_ELEMENTS];
284 char *rx = rxbuf;
285 const CGEN_SYNTAX_CHAR_TYPE *syn;
286 int reg_err;
288 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
290 /* Mnemonics come first in the syntax string. */
291 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
292 return _("missing mnemonic in syntax string");
293 ++syn;
295 /* Generate a case sensitive regular expression that emulates case
296 insensitive matching in the "C" locale. We cannot generate a case
297 insensitive regular expression because in Turkish locales, 'i' and 'I'
298 are not equal modulo case conversion. */
300 /* Copy the literal mnemonic out of the insn. */
301 for (; *mnem; mnem++)
303 char c = *mnem;
305 if (ISALPHA (c))
307 *rx++ = '[';
308 *rx++ = TOLOWER (c);
309 *rx++ = TOUPPER (c);
310 *rx++ = ']';
312 else
313 *rx++ = c;
316 /* Copy any remaining literals from the syntax string into the rx. */
317 for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
319 if (CGEN_SYNTAX_CHAR_P (* syn))
321 char c = CGEN_SYNTAX_CHAR (* syn);
323 switch (c)
325 /* Escape any regex metacharacters in the syntax. */
326 case '.': case '[': case '\\':
327 case '*': case '^': case '$':
329 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
330 case '?': case '{': case '}':
331 case '(': case ')': case '*':
332 case '|': case '+': case ']':
333 #endif
334 *rx++ = '\\';
335 *rx++ = c;
336 break;
338 default:
339 if (ISALPHA (c))
341 *rx++ = '[';
342 *rx++ = TOLOWER (c);
343 *rx++ = TOUPPER (c);
344 *rx++ = ']';
346 else
347 *rx++ = c;
348 break;
351 else
353 /* Replace non-syntax fields with globs. */
354 *rx++ = '.';
355 *rx++ = '*';
359 /* Trailing whitespace ok. */
360 * rx++ = '[';
361 * rx++ = ' ';
362 * rx++ = '\t';
363 * rx++ = ']';
364 * rx++ = '*';
366 /* But anchor it after that. */
367 * rx++ = '$';
368 * rx = '\0';
370 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
371 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
373 if (reg_err == 0)
374 return NULL;
375 else
377 static char msg[80];
379 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
380 regfree ((regex_t *) CGEN_INSN_RX (insn));
381 free (CGEN_INSN_RX (insn));
382 (CGEN_INSN_RX (insn)) = NULL;
383 return msg;
388 /* Default insn parser.
390 The syntax string is scanned and operands are parsed and stored in FIELDS.
391 Relocs are queued as we go via other callbacks.
393 ??? Note that this is currently an all-or-nothing parser. If we fail to
394 parse the instruction, we return 0 and the caller will start over from
395 the beginning. Backtracking will be necessary in parsing subexpressions,
396 but that can be handled there. Not handling backtracking here may get
397 expensive in the case of the m68k. Deal with later.
399 Returns NULL for success, an error message for failure. */
401 static const char *
402 parse_insn_normal (CGEN_CPU_DESC cd,
403 const CGEN_INSN *insn,
404 const char **strp,
405 CGEN_FIELDS *fields)
407 /* ??? Runtime added insns not handled yet. */
408 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
409 const char *str = *strp;
410 const char *errmsg;
411 const char *p;
412 const CGEN_SYNTAX_CHAR_TYPE * syn;
413 #ifdef CGEN_MNEMONIC_OPERANDS
414 /* FIXME: wip */
415 int past_opcode_p;
416 #endif
418 /* For now we assume the mnemonic is first (there are no leading operands).
419 We can parse it without needing to set up operand parsing.
420 GAS's input scrubber will ensure mnemonics are lowercase, but we may
421 not be called from GAS. */
422 p = CGEN_INSN_MNEMONIC (insn);
423 while (*p && TOLOWER (*p) == TOLOWER (*str))
424 ++p, ++str;
426 if (* p)
427 return _("unrecognized instruction");
429 #ifndef CGEN_MNEMONIC_OPERANDS
430 if (* str && ! ISSPACE (* str))
431 return _("unrecognized instruction");
432 #endif
434 CGEN_INIT_PARSE (cd);
435 cgen_init_parse_operand (cd);
436 #ifdef CGEN_MNEMONIC_OPERANDS
437 past_opcode_p = 0;
438 #endif
440 /* We don't check for (*str != '\0') here because we want to parse
441 any trailing fake arguments in the syntax string. */
442 syn = CGEN_SYNTAX_STRING (syntax);
444 /* Mnemonics come first for now, ensure valid string. */
445 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
446 abort ();
448 ++syn;
450 while (* syn != 0)
452 /* Non operand chars must match exactly. */
453 if (CGEN_SYNTAX_CHAR_P (* syn))
455 /* FIXME: While we allow for non-GAS callers above, we assume the
456 first char after the mnemonic part is a space. */
457 /* FIXME: We also take inappropriate advantage of the fact that
458 GAS's input scrubber will remove extraneous blanks. */
459 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
461 #ifdef CGEN_MNEMONIC_OPERANDS
462 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
463 past_opcode_p = 1;
464 #endif
465 ++ syn;
466 ++ str;
468 else if (*str)
470 /* Syntax char didn't match. Can't be this insn. */
471 static char msg [80];
473 /* xgettext:c-format */
474 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
475 CGEN_SYNTAX_CHAR(*syn), *str);
476 return msg;
478 else
480 /* Ran out of input. */
481 static char msg [80];
483 /* xgettext:c-format */
484 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
485 CGEN_SYNTAX_CHAR(*syn));
486 return msg;
488 continue;
491 #ifdef CGEN_MNEMONIC_OPERANDS
492 (void) past_opcode_p;
493 #endif
494 /* We have an operand of some sort. */
495 errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
496 if (errmsg)
497 return errmsg;
499 /* Done with this operand, continue with next one. */
500 ++ syn;
503 /* If we're at the end of the syntax string, we're done. */
504 if (* syn == 0)
506 /* FIXME: For the moment we assume a valid `str' can only contain
507 blanks now. IE: We needn't try again with a longer version of
508 the insn and it is assumed that longer versions of insns appear
509 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
510 while (ISSPACE (* str))
511 ++ str;
513 if (* str != '\0')
514 return _("junk at end of line"); /* FIXME: would like to include `str' */
516 return NULL;
519 /* We couldn't parse it. */
520 return _("unrecognized instruction");
523 /* Main entry point.
524 This routine is called for each instruction to be assembled.
525 STR points to the insn to be assembled.
526 We assume all necessary tables have been initialized.
527 The assembled instruction, less any fixups, is stored in BUF.
528 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
529 still needs to be converted to target byte order, otherwise BUF is an array
530 of bytes in target byte order.
531 The result is a pointer to the insn's entry in the opcode table,
532 or NULL if an error occured (an error message will have already been
533 printed).
535 Note that when processing (non-alias) macro-insns,
536 this function recurses.
538 ??? It's possible to make this cpu-independent.
539 One would have to deal with a few minor things.
540 At this point in time doing so would be more of a curiosity than useful
541 [for example this file isn't _that_ big], but keeping the possibility in
542 mind helps keep the design clean. */
544 const CGEN_INSN *
545 openrisc_cgen_assemble_insn (CGEN_CPU_DESC cd,
546 const char *str,
547 CGEN_FIELDS *fields,
548 CGEN_INSN_BYTES_PTR buf,
549 char **errmsg)
551 const char *start;
552 CGEN_INSN_LIST *ilist;
553 const char *parse_errmsg = NULL;
554 const char *insert_errmsg = NULL;
555 int recognized_mnemonic = 0;
557 /* Skip leading white space. */
558 while (ISSPACE (* str))
559 ++ str;
561 /* The instructions are stored in hashed lists.
562 Get the first in the list. */
563 ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
565 /* Keep looking until we find a match. */
566 start = str;
567 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
569 const CGEN_INSN *insn = ilist->insn;
570 recognized_mnemonic = 1;
572 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
573 /* Not usually needed as unsupported opcodes
574 shouldn't be in the hash lists. */
575 /* Is this insn supported by the selected cpu? */
576 if (! openrisc_cgen_insn_supported (cd, insn))
577 continue;
578 #endif
579 /* If the RELAXED attribute is set, this is an insn that shouldn't be
580 chosen immediately. Instead, it is used during assembler/linker
581 relaxation if possible. */
582 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
583 continue;
585 str = start;
587 /* Skip this insn if str doesn't look right lexically. */
588 if (CGEN_INSN_RX (insn) != NULL &&
589 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
590 continue;
592 /* Allow parse/insert handlers to obtain length of insn. */
593 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
595 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
596 if (parse_errmsg != NULL)
597 continue;
599 /* ??? 0 is passed for `pc'. */
600 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
601 (bfd_vma) 0);
602 if (insert_errmsg != NULL)
603 continue;
605 /* It is up to the caller to actually output the insn and any
606 queued relocs. */
607 return insn;
611 static char errbuf[150];
612 const char *tmp_errmsg;
613 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
614 #define be_verbose 1
615 #else
616 #define be_verbose 0
617 #endif
619 if (be_verbose)
621 /* If requesting verbose error messages, use insert_errmsg.
622 Failing that, use parse_errmsg. */
623 tmp_errmsg = (insert_errmsg ? insert_errmsg :
624 parse_errmsg ? parse_errmsg :
625 recognized_mnemonic ?
626 _("unrecognized form of instruction") :
627 _("unrecognized instruction"));
629 if (strlen (start) > 50)
630 /* xgettext:c-format */
631 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
632 else
633 /* xgettext:c-format */
634 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
636 else
638 if (strlen (start) > 50)
639 /* xgettext:c-format */
640 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
641 else
642 /* xgettext:c-format */
643 sprintf (errbuf, _("bad instruction `%.50s'"), start);
646 *errmsg = errbuf;
647 return NULL;