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 (C) 1996-2016 Free Software Foundation, Inc.
9 This file is part of libopcodes.
11 This library 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 3, or (at your option)
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.
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. */
26 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
34 #include "lm32-desc.h"
38 #include "libiberty.h"
39 #include "safe-ctype.h"
42 #define min(a,b) ((a) < (b) ? (a) : (b))
44 #define max(a,b) ((a) > (b) ? (a) : (b))
46 static const char * parse_insn_normal
47 (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
49 /* -- assembler routines inserted here. */
53 /* Handle signed/unsigned literal. */
56 parse_imm (CGEN_CPU_DESC cd,
59 unsigned long *valuep)
64 errmsg = cgen_parse_signed_integer (cd, strp, opindex, & value);
67 unsigned long x = value & 0xFFFF0000;
68 if (x != 0 && x != 0xFFFF0000)
69 errmsg = _("immediate value out of range");
71 *valuep = (value & 0xFFFF);
79 parse_hi16 (CGEN_CPU_DESC cd,
82 unsigned long *valuep)
84 if (strncasecmp (*strp, "hi(", 3) == 0)
86 enum cgen_parse_operand_result result_type;
91 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_HI16,
92 &result_type, &value);
94 return _("missing `)'");
98 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
99 value = (value >> 16) & 0xffff;
105 return parse_imm (cd, strp, opindex, valuep);
111 parse_lo16 (CGEN_CPU_DESC cd,
114 unsigned long *valuep)
116 if (strncasecmp (*strp, "lo(", 3) == 0)
119 enum cgen_parse_operand_result result_type;
123 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LO16,
124 &result_type, &value);
126 return _("missing `)'");
129 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
135 return parse_imm (cd, strp, opindex, valuep);
141 parse_gp16 (CGEN_CPU_DESC cd,
146 if (strncasecmp (*strp, "gp(", 3) == 0)
149 enum cgen_parse_operand_result result_type;
153 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_GPREL16,
154 & result_type, & value);
156 return _("missing `)'");
159 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
165 return _("expecting gp relative address: gp(symbol)");
171 parse_got16 (CGEN_CPU_DESC cd,
176 if (strncasecmp (*strp, "got(", 4) == 0)
179 enum cgen_parse_operand_result result_type;
183 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LM32_16_GOT,
184 & result_type, & value);
186 return _("missing `)'");
189 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
195 return _("expecting got relative address: got(symbol)");
198 /* Handle gotoffhi16() */
201 parse_gotoff_hi16 (CGEN_CPU_DESC cd,
206 if (strncasecmp (*strp, "gotoffhi16(", 11) == 0)
209 enum cgen_parse_operand_result result_type;
213 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LM32_GOTOFF_HI16,
214 & result_type, & value);
216 return _("missing `)'");
219 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
225 return _("expecting got relative address: gotoffhi16(symbol)");
228 /* Handle gotofflo16() */
231 parse_gotoff_lo16 (CGEN_CPU_DESC cd,
236 if (strncasecmp (*strp, "gotofflo16(", 11) == 0)
239 enum cgen_parse_operand_result result_type;
243 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LM32_GOTOFF_LO16,
244 &result_type, &value);
246 return _("missing `)'");
249 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
255 return _("expecting got relative address: gotofflo16(symbol)");
258 const char * lm32_cgen_parse_operand
259 (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
261 /* Main entry point for operand parsing.
263 This function is basically just a big switch statement. Earlier versions
264 used tables to look up the function to use, but
265 - if the table contains both assembler and disassembler functions then
266 the disassembler contains much of the assembler and vice-versa,
267 - there's a lot of inlining possibilities as things grow,
268 - using a switch statement avoids the function call overhead.
270 This function could be moved into `parse_insn_normal', but keeping it
271 separate makes clear the interface between `parse_insn_normal' and each of
275 lm32_cgen_parse_operand (CGEN_CPU_DESC cd,
278 CGEN_FIELDS * fields)
280 const char * errmsg = NULL;
281 /* Used by scalar operands that still need to be parsed. */
282 long junk ATTRIBUTE_UNUSED;
286 case LM32_OPERAND_BRANCH :
289 errmsg = cgen_parse_address (cd, strp, LM32_OPERAND_BRANCH, 0, NULL, & value);
290 fields->f_branch = value;
293 case LM32_OPERAND_CALL :
296 errmsg = cgen_parse_address (cd, strp, LM32_OPERAND_CALL, 0, NULL, & value);
297 fields->f_call = value;
300 case LM32_OPERAND_CSR :
301 errmsg = cgen_parse_keyword (cd, strp, & lm32_cgen_opval_h_csr, & fields->f_csr);
303 case LM32_OPERAND_EXCEPTION :
304 errmsg = cgen_parse_unsigned_integer (cd, strp, LM32_OPERAND_EXCEPTION, (unsigned long *) (& fields->f_exception));
306 case LM32_OPERAND_GOT16 :
307 errmsg = parse_got16 (cd, strp, LM32_OPERAND_GOT16, (long *) (& fields->f_imm));
309 case LM32_OPERAND_GOTOFFHI16 :
310 errmsg = parse_gotoff_hi16 (cd, strp, LM32_OPERAND_GOTOFFHI16, (long *) (& fields->f_imm));
312 case LM32_OPERAND_GOTOFFLO16 :
313 errmsg = parse_gotoff_lo16 (cd, strp, LM32_OPERAND_GOTOFFLO16, (long *) (& fields->f_imm));
315 case LM32_OPERAND_GP16 :
316 errmsg = parse_gp16 (cd, strp, LM32_OPERAND_GP16, (long *) (& fields->f_imm));
318 case LM32_OPERAND_HI16 :
319 errmsg = parse_hi16 (cd, strp, LM32_OPERAND_HI16, (unsigned long *) (& fields->f_uimm));
321 case LM32_OPERAND_IMM :
322 errmsg = cgen_parse_signed_integer (cd, strp, LM32_OPERAND_IMM, (long *) (& fields->f_imm));
324 case LM32_OPERAND_LO16 :
325 errmsg = parse_lo16 (cd, strp, LM32_OPERAND_LO16, (unsigned long *) (& fields->f_uimm));
327 case LM32_OPERAND_R0 :
328 errmsg = cgen_parse_keyword (cd, strp, & lm32_cgen_opval_h_gr, & fields->f_r0);
330 case LM32_OPERAND_R1 :
331 errmsg = cgen_parse_keyword (cd, strp, & lm32_cgen_opval_h_gr, & fields->f_r1);
333 case LM32_OPERAND_R2 :
334 errmsg = cgen_parse_keyword (cd, strp, & lm32_cgen_opval_h_gr, & fields->f_r2);
336 case LM32_OPERAND_SHIFT :
337 errmsg = cgen_parse_unsigned_integer (cd, strp, LM32_OPERAND_SHIFT, (unsigned long *) (& fields->f_shift));
339 case LM32_OPERAND_UIMM :
340 errmsg = cgen_parse_unsigned_integer (cd, strp, LM32_OPERAND_UIMM, (unsigned long *) (& fields->f_uimm));
342 case LM32_OPERAND_USER :
343 errmsg = cgen_parse_unsigned_integer (cd, strp, LM32_OPERAND_USER, (unsigned long *) (& fields->f_user));
347 /* xgettext:c-format */
348 fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
355 cgen_parse_fn * const lm32_cgen_parse_handlers[] =
361 lm32_cgen_init_asm (CGEN_CPU_DESC cd)
363 lm32_cgen_init_opcode_table (cd);
364 lm32_cgen_init_ibld_table (cd);
365 cd->parse_handlers = & lm32_cgen_parse_handlers[0];
366 cd->parse_operand = lm32_cgen_parse_operand;
367 #ifdef CGEN_ASM_INIT_HOOK
374 /* Regex construction routine.
376 This translates an opcode syntax string into a regex string,
377 by replacing any non-character syntax element (such as an
378 opcode) with the pattern '.*'
380 It then compiles the regex and stores it in the opcode, for
381 later use by lm32_cgen_assemble_insn
383 Returns NULL for success, an error message for failure. */
386 lm32_cgen_build_insn_regex (CGEN_INSN *insn)
388 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
389 const char *mnem = CGEN_INSN_MNEMONIC (insn);
390 char rxbuf[CGEN_MAX_RX_ELEMENTS];
392 const CGEN_SYNTAX_CHAR_TYPE *syn;
395 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
397 /* Mnemonics come first in the syntax string. */
398 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
399 return _("missing mnemonic in syntax string");
402 /* Generate a case sensitive regular expression that emulates case
403 insensitive matching in the "C" locale. We cannot generate a case
404 insensitive regular expression because in Turkish locales, 'i' and 'I'
405 are not equal modulo case conversion. */
407 /* Copy the literal mnemonic out of the insn. */
408 for (; *mnem; mnem++)
423 /* Copy any remaining literals from the syntax string into the rx. */
424 for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
426 if (CGEN_SYNTAX_CHAR_P (* syn))
428 char c = CGEN_SYNTAX_CHAR (* syn);
432 /* Escape any regex metacharacters in the syntax. */
433 case '.': case '[': case '\\':
434 case '*': case '^': case '$':
436 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
437 case '?': case '{': case '}':
438 case '(': case ')': case '*':
439 case '|': case '+': case ']':
460 /* Replace non-syntax fields with globs. */
466 /* Trailing whitespace ok. */
473 /* But anchor it after that. */
477 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
478 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
486 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
487 regfree ((regex_t *) CGEN_INSN_RX (insn));
488 free (CGEN_INSN_RX (insn));
489 (CGEN_INSN_RX (insn)) = NULL;
495 /* Default insn parser.
497 The syntax string is scanned and operands are parsed and stored in FIELDS.
498 Relocs are queued as we go via other callbacks.
500 ??? Note that this is currently an all-or-nothing parser. If we fail to
501 parse the instruction, we return 0 and the caller will start over from
502 the beginning. Backtracking will be necessary in parsing subexpressions,
503 but that can be handled there. Not handling backtracking here may get
504 expensive in the case of the m68k. Deal with later.
506 Returns NULL for success, an error message for failure. */
509 parse_insn_normal (CGEN_CPU_DESC cd,
510 const CGEN_INSN *insn,
514 /* ??? Runtime added insns not handled yet. */
515 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
516 const char *str = *strp;
519 const CGEN_SYNTAX_CHAR_TYPE * syn;
520 #ifdef CGEN_MNEMONIC_OPERANDS
525 /* For now we assume the mnemonic is first (there are no leading operands).
526 We can parse it without needing to set up operand parsing.
527 GAS's input scrubber will ensure mnemonics are lowercase, but we may
528 not be called from GAS. */
529 p = CGEN_INSN_MNEMONIC (insn);
530 while (*p && TOLOWER (*p) == TOLOWER (*str))
534 return _("unrecognized instruction");
536 #ifndef CGEN_MNEMONIC_OPERANDS
537 if (* str && ! ISSPACE (* str))
538 return _("unrecognized instruction");
541 CGEN_INIT_PARSE (cd);
542 cgen_init_parse_operand (cd);
543 #ifdef CGEN_MNEMONIC_OPERANDS
547 /* We don't check for (*str != '\0') here because we want to parse
548 any trailing fake arguments in the syntax string. */
549 syn = CGEN_SYNTAX_STRING (syntax);
551 /* Mnemonics come first for now, ensure valid string. */
552 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
559 /* Non operand chars must match exactly. */
560 if (CGEN_SYNTAX_CHAR_P (* syn))
562 /* FIXME: While we allow for non-GAS callers above, we assume the
563 first char after the mnemonic part is a space. */
564 /* FIXME: We also take inappropriate advantage of the fact that
565 GAS's input scrubber will remove extraneous blanks. */
566 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
568 #ifdef CGEN_MNEMONIC_OPERANDS
569 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
577 /* Syntax char didn't match. Can't be this insn. */
578 static char msg [80];
580 /* xgettext:c-format */
581 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
582 CGEN_SYNTAX_CHAR(*syn), *str);
587 /* Ran out of input. */
588 static char msg [80];
590 /* xgettext:c-format */
591 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
592 CGEN_SYNTAX_CHAR(*syn));
598 #ifdef CGEN_MNEMONIC_OPERANDS
599 (void) past_opcode_p;
601 /* We have an operand of some sort. */
602 errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
606 /* Done with this operand, continue with next one. */
610 /* If we're at the end of the syntax string, we're done. */
613 /* FIXME: For the moment we assume a valid `str' can only contain
614 blanks now. IE: We needn't try again with a longer version of
615 the insn and it is assumed that longer versions of insns appear
616 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
617 while (ISSPACE (* str))
621 return _("junk at end of line"); /* FIXME: would like to include `str' */
626 /* We couldn't parse it. */
627 return _("unrecognized instruction");
631 This routine is called for each instruction to be assembled.
632 STR points to the insn to be assembled.
633 We assume all necessary tables have been initialized.
634 The assembled instruction, less any fixups, is stored in BUF.
635 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
636 still needs to be converted to target byte order, otherwise BUF is an array
637 of bytes in target byte order.
638 The result is a pointer to the insn's entry in the opcode table,
639 or NULL if an error occured (an error message will have already been
642 Note that when processing (non-alias) macro-insns,
643 this function recurses.
645 ??? It's possible to make this cpu-independent.
646 One would have to deal with a few minor things.
647 At this point in time doing so would be more of a curiosity than useful
648 [for example this file isn't _that_ big], but keeping the possibility in
649 mind helps keep the design clean. */
652 lm32_cgen_assemble_insn (CGEN_CPU_DESC cd,
655 CGEN_INSN_BYTES_PTR buf,
659 CGEN_INSN_LIST *ilist;
660 const char *parse_errmsg = NULL;
661 const char *insert_errmsg = NULL;
662 int recognized_mnemonic = 0;
664 /* Skip leading white space. */
665 while (ISSPACE (* str))
668 /* The instructions are stored in hashed lists.
669 Get the first in the list. */
670 ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
672 /* Keep looking until we find a match. */
674 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
676 const CGEN_INSN *insn = ilist->insn;
677 recognized_mnemonic = 1;
679 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
680 /* Not usually needed as unsupported opcodes
681 shouldn't be in the hash lists. */
682 /* Is this insn supported by the selected cpu? */
683 if (! lm32_cgen_insn_supported (cd, insn))
686 /* If the RELAXED attribute is set, this is an insn that shouldn't be
687 chosen immediately. Instead, it is used during assembler/linker
688 relaxation if possible. */
689 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
694 /* Skip this insn if str doesn't look right lexically. */
695 if (CGEN_INSN_RX (insn) != NULL &&
696 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
699 /* Allow parse/insert handlers to obtain length of insn. */
700 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
702 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
703 if (parse_errmsg != NULL)
706 /* ??? 0 is passed for `pc'. */
707 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
709 if (insert_errmsg != NULL)
712 /* It is up to the caller to actually output the insn and any
718 static char errbuf[150];
719 const char *tmp_errmsg;
720 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
728 /* If requesting verbose error messages, use insert_errmsg.
729 Failing that, use parse_errmsg. */
730 tmp_errmsg = (insert_errmsg ? insert_errmsg :
731 parse_errmsg ? parse_errmsg :
732 recognized_mnemonic ?
733 _("unrecognized form of instruction") :
734 _("unrecognized instruction"));
736 if (strlen (start) > 50)
737 /* xgettext:c-format */
738 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
740 /* xgettext:c-format */
741 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
745 if (strlen (start) > 50)
746 /* xgettext:c-format */
747 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
749 /* xgettext:c-format */
750 sprintf (errbuf, _("bad instruction `%.50s'"), start);