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
8 Free Software Foundation, Inc.
10 This file is part of the GNU Binutils and GDB, the GNU debugger.
12 This program 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 2, or (at your option)
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public 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. */
26 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
34 #include "openrisc-desc.h"
35 #include "openrisc-opc.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 static const char * MISSING_CLOSING_PARENTHESIS
= N_("missing `)'");
55 #define CGEN_VERBOSE_ASSEMBLER_ERRORS
58 openrisc_sign_extend_16bit (long value
)
60 return ((value
& 0xffff) ^ 0x8000) - 0x8000;
66 parse_hi16 (CGEN_CPU_DESC cd
, const char ** strp
, int opindex
, long * valuep
)
69 enum cgen_parse_operand_result result_type
;
75 if (strncasecmp (*strp
, "hi(", 3) == 0)
80 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_HI16
,
81 & result_type
, & value
);
83 return MISSING_CLOSING_PARENTHESIS
;
87 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
97 errmsg
= cgen_parse_signed_integer (cd
, strp
, opindex
, &value
);
104 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, opindex
, &value
);
109 *valuep
= ((ret
& 0xffff) ^ 0x8000) - 0x8000;
116 parse_lo16 (CGEN_CPU_DESC cd
, const char ** strp
, int opindex
, long * valuep
)
119 enum cgen_parse_operand_result result_type
;
125 if (strncasecmp (*strp
, "lo(", 3) == 0)
130 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_LO16
,
131 & result_type
, & value
);
133 return MISSING_CLOSING_PARENTHESIS
;
144 errmsg
= cgen_parse_signed_integer (cd
, strp
, opindex
, &value
);
151 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, opindex
, &value
);
156 *valuep
= ((ret
& 0xffff) ^ 0x8000) - 0x8000;
162 const char * openrisc_cgen_parse_operand
163 (CGEN_CPU_DESC
, int, const char **, CGEN_FIELDS
*);
165 /* Main entry point for operand parsing.
167 This function is basically just a big switch statement. Earlier versions
168 used tables to look up the function to use, but
169 - if the table contains both assembler and disassembler functions then
170 the disassembler contains much of the assembler and vice-versa,
171 - there's a lot of inlining possibilities as things grow,
172 - using a switch statement avoids the function call overhead.
174 This function could be moved into `parse_insn_normal', but keeping it
175 separate makes clear the interface between `parse_insn_normal' and each of
179 openrisc_cgen_parse_operand (CGEN_CPU_DESC cd
,
182 CGEN_FIELDS
* fields
)
184 const char * errmsg
= NULL
;
185 /* Used by scalar operands that still need to be parsed. */
186 long junk ATTRIBUTE_UNUSED
;
190 case OPENRISC_OPERAND_ABS_26
:
193 errmsg
= cgen_parse_address (cd
, strp
, OPENRISC_OPERAND_ABS_26
, 0, NULL
, & value
);
194 fields
->f_abs26
= value
;
197 case OPENRISC_OPERAND_DISP_26
:
200 errmsg
= cgen_parse_address (cd
, strp
, OPENRISC_OPERAND_DISP_26
, 0, NULL
, & value
);
201 fields
->f_disp26
= value
;
204 case OPENRISC_OPERAND_HI16
:
205 errmsg
= parse_hi16 (cd
, strp
, OPENRISC_OPERAND_HI16
, (long *) (& fields
->f_simm16
));
207 case OPENRISC_OPERAND_LO16
:
208 errmsg
= parse_lo16 (cd
, strp
, OPENRISC_OPERAND_LO16
, (long *) (& fields
->f_lo16
));
210 case OPENRISC_OPERAND_OP_F_23
:
211 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, OPENRISC_OPERAND_OP_F_23
, (unsigned long *) (& fields
->f_op4
));
213 case OPENRISC_OPERAND_OP_F_3
:
214 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, OPENRISC_OPERAND_OP_F_3
, (unsigned long *) (& fields
->f_op5
));
216 case OPENRISC_OPERAND_RA
:
217 errmsg
= cgen_parse_keyword (cd
, strp
, & openrisc_cgen_opval_h_gr
, & fields
->f_r2
);
219 case OPENRISC_OPERAND_RB
:
220 errmsg
= cgen_parse_keyword (cd
, strp
, & openrisc_cgen_opval_h_gr
, & fields
->f_r3
);
222 case OPENRISC_OPERAND_RD
:
223 errmsg
= cgen_parse_keyword (cd
, strp
, & openrisc_cgen_opval_h_gr
, & fields
->f_r1
);
225 case OPENRISC_OPERAND_SIMM_16
:
226 errmsg
= cgen_parse_signed_integer (cd
, strp
, OPENRISC_OPERAND_SIMM_16
, (long *) (& fields
->f_simm16
));
228 case OPENRISC_OPERAND_UI16NC
:
229 errmsg
= parse_lo16 (cd
, strp
, OPENRISC_OPERAND_UI16NC
, (long *) (& fields
->f_i16nc
));
231 case OPENRISC_OPERAND_UIMM_16
:
232 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, OPENRISC_OPERAND_UIMM_16
, (unsigned long *) (& fields
->f_uimm16
));
234 case OPENRISC_OPERAND_UIMM_5
:
235 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, OPENRISC_OPERAND_UIMM_5
, (unsigned long *) (& fields
->f_uimm5
));
239 /* xgettext:c-format */
240 fprintf (stderr
, _("Unrecognized field %d while parsing.\n"), opindex
);
247 cgen_parse_fn
* const openrisc_cgen_parse_handlers
[] =
253 openrisc_cgen_init_asm (CGEN_CPU_DESC cd
)
255 openrisc_cgen_init_opcode_table (cd
);
256 openrisc_cgen_init_ibld_table (cd
);
257 cd
->parse_handlers
= & openrisc_cgen_parse_handlers
[0];
258 cd
->parse_operand
= openrisc_cgen_parse_operand
;
259 #ifdef CGEN_ASM_INIT_HOOK
266 /* Regex construction routine.
268 This translates an opcode syntax string into a regex string,
269 by replacing any non-character syntax element (such as an
270 opcode) with the pattern '.*'
272 It then compiles the regex and stores it in the opcode, for
273 later use by openrisc_cgen_assemble_insn
275 Returns NULL for success, an error message for failure. */
278 openrisc_cgen_build_insn_regex (CGEN_INSN
*insn
)
280 CGEN_OPCODE
*opc
= (CGEN_OPCODE
*) CGEN_INSN_OPCODE (insn
);
281 const char *mnem
= CGEN_INSN_MNEMONIC (insn
);
282 char rxbuf
[CGEN_MAX_RX_ELEMENTS
];
284 const CGEN_SYNTAX_CHAR_TYPE
*syn
;
287 syn
= CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc
));
289 /* Mnemonics come first in the syntax string. */
290 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
291 return _("missing mnemonic in syntax string");
294 /* Generate a case sensitive regular expression that emulates case
295 insensitive matching in the "C" locale. We cannot generate a case
296 insensitive regular expression because in Turkish locales, 'i' and 'I'
297 are not equal modulo case conversion. */
299 /* Copy the literal mnemonic out of the insn. */
300 for (; *mnem
; mnem
++)
315 /* Copy any remaining literals from the syntax string into the rx. */
316 for(; * syn
!= 0 && rx
<= rxbuf
+ (CGEN_MAX_RX_ELEMENTS
- 7 - 4); ++syn
)
318 if (CGEN_SYNTAX_CHAR_P (* syn
))
320 char c
= CGEN_SYNTAX_CHAR (* syn
);
324 /* Escape any regex metacharacters in the syntax. */
325 case '.': case '[': case '\\':
326 case '*': case '^': case '$':
328 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
329 case '?': case '{': case '}':
330 case '(': case ')': case '*':
331 case '|': case '+': case ']':
352 /* Replace non-syntax fields with globs. */
358 /* Trailing whitespace ok. */
365 /* But anchor it after that. */
369 CGEN_INSN_RX (insn
) = xmalloc (sizeof (regex_t
));
370 reg_err
= regcomp ((regex_t
*) CGEN_INSN_RX (insn
), rxbuf
, REG_NOSUB
);
378 regerror (reg_err
, (regex_t
*) CGEN_INSN_RX (insn
), msg
, 80);
379 regfree ((regex_t
*) CGEN_INSN_RX (insn
));
380 free (CGEN_INSN_RX (insn
));
381 (CGEN_INSN_RX (insn
)) = NULL
;
387 /* Default insn parser.
389 The syntax string is scanned and operands are parsed and stored in FIELDS.
390 Relocs are queued as we go via other callbacks.
392 ??? Note that this is currently an all-or-nothing parser. If we fail to
393 parse the instruction, we return 0 and the caller will start over from
394 the beginning. Backtracking will be necessary in parsing subexpressions,
395 but that can be handled there. Not handling backtracking here may get
396 expensive in the case of the m68k. Deal with later.
398 Returns NULL for success, an error message for failure. */
401 parse_insn_normal (CGEN_CPU_DESC cd
,
402 const CGEN_INSN
*insn
,
406 /* ??? Runtime added insns not handled yet. */
407 const CGEN_SYNTAX
*syntax
= CGEN_INSN_SYNTAX (insn
);
408 const char *str
= *strp
;
411 const CGEN_SYNTAX_CHAR_TYPE
* syn
;
412 #ifdef CGEN_MNEMONIC_OPERANDS
417 /* For now we assume the mnemonic is first (there are no leading operands).
418 We can parse it without needing to set up operand parsing.
419 GAS's input scrubber will ensure mnemonics are lowercase, but we may
420 not be called from GAS. */
421 p
= CGEN_INSN_MNEMONIC (insn
);
422 while (*p
&& TOLOWER (*p
) == TOLOWER (*str
))
426 return _("unrecognized instruction");
428 #ifndef CGEN_MNEMONIC_OPERANDS
429 if (* str
&& ! ISSPACE (* str
))
430 return _("unrecognized instruction");
433 CGEN_INIT_PARSE (cd
);
434 cgen_init_parse_operand (cd
);
435 #ifdef CGEN_MNEMONIC_OPERANDS
439 /* We don't check for (*str != '\0') here because we want to parse
440 any trailing fake arguments in the syntax string. */
441 syn
= CGEN_SYNTAX_STRING (syntax
);
443 /* Mnemonics come first for now, ensure valid string. */
444 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
451 /* Non operand chars must match exactly. */
452 if (CGEN_SYNTAX_CHAR_P (* syn
))
454 /* FIXME: While we allow for non-GAS callers above, we assume the
455 first char after the mnemonic part is a space. */
456 /* FIXME: We also take inappropriate advantage of the fact that
457 GAS's input scrubber will remove extraneous blanks. */
458 if (TOLOWER (*str
) == TOLOWER (CGEN_SYNTAX_CHAR (* syn
)))
460 #ifdef CGEN_MNEMONIC_OPERANDS
461 if (CGEN_SYNTAX_CHAR(* syn
) == ' ')
469 /* Syntax char didn't match. Can't be this insn. */
470 static char msg
[80];
472 /* xgettext:c-format */
473 sprintf (msg
, _("syntax error (expected char `%c', found `%c')"),
474 CGEN_SYNTAX_CHAR(*syn
), *str
);
479 /* Ran out of input. */
480 static char msg
[80];
482 /* xgettext:c-format */
483 sprintf (msg
, _("syntax error (expected char `%c', found end of instruction)"),
484 CGEN_SYNTAX_CHAR(*syn
));
490 /* We have an operand of some sort. */
491 errmsg
= cd
->parse_operand (cd
, CGEN_SYNTAX_FIELD (*syn
),
496 /* Done with this operand, continue with next one. */
500 /* If we're at the end of the syntax string, we're done. */
503 /* FIXME: For the moment we assume a valid `str' can only contain
504 blanks now. IE: We needn't try again with a longer version of
505 the insn and it is assumed that longer versions of insns appear
506 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
507 while (ISSPACE (* str
))
511 return _("junk at end of line"); /* FIXME: would like to include `str' */
516 /* We couldn't parse it. */
517 return _("unrecognized instruction");
521 This routine is called for each instruction to be assembled.
522 STR points to the insn to be assembled.
523 We assume all necessary tables have been initialized.
524 The assembled instruction, less any fixups, is stored in BUF.
525 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
526 still needs to be converted to target byte order, otherwise BUF is an array
527 of bytes in target byte order.
528 The result is a pointer to the insn's entry in the opcode table,
529 or NULL if an error occured (an error message will have already been
532 Note that when processing (non-alias) macro-insns,
533 this function recurses.
535 ??? It's possible to make this cpu-independent.
536 One would have to deal with a few minor things.
537 At this point in time doing so would be more of a curiosity than useful
538 [for example this file isn't _that_ big], but keeping the possibility in
539 mind helps keep the design clean. */
542 openrisc_cgen_assemble_insn (CGEN_CPU_DESC cd
,
545 CGEN_INSN_BYTES_PTR buf
,
549 CGEN_INSN_LIST
*ilist
;
550 const char *parse_errmsg
= NULL
;
551 const char *insert_errmsg
= NULL
;
552 int recognized_mnemonic
= 0;
554 /* Skip leading white space. */
555 while (ISSPACE (* str
))
558 /* The instructions are stored in hashed lists.
559 Get the first in the list. */
560 ilist
= CGEN_ASM_LOOKUP_INSN (cd
, str
);
562 /* Keep looking until we find a match. */
564 for ( ; ilist
!= NULL
; ilist
= CGEN_ASM_NEXT_INSN (ilist
))
566 const CGEN_INSN
*insn
= ilist
->insn
;
567 recognized_mnemonic
= 1;
569 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
570 /* Not usually needed as unsupported opcodes
571 shouldn't be in the hash lists. */
572 /* Is this insn supported by the selected cpu? */
573 if (! openrisc_cgen_insn_supported (cd
, insn
))
576 /* If the RELAXED attribute is set, this is an insn that shouldn't be
577 chosen immediately. Instead, it is used during assembler/linker
578 relaxation if possible. */
579 if (CGEN_INSN_ATTR_VALUE (insn
, CGEN_INSN_RELAXED
) != 0)
584 /* Skip this insn if str doesn't look right lexically. */
585 if (CGEN_INSN_RX (insn
) != NULL
&&
586 regexec ((regex_t
*) CGEN_INSN_RX (insn
), str
, 0, NULL
, 0) == REG_NOMATCH
)
589 /* Allow parse/insert handlers to obtain length of insn. */
590 CGEN_FIELDS_BITSIZE (fields
) = CGEN_INSN_BITSIZE (insn
);
592 parse_errmsg
= CGEN_PARSE_FN (cd
, insn
) (cd
, insn
, & str
, fields
);
593 if (parse_errmsg
!= NULL
)
596 /* ??? 0 is passed for `pc'. */
597 insert_errmsg
= CGEN_INSERT_FN (cd
, insn
) (cd
, insn
, fields
, buf
,
599 if (insert_errmsg
!= NULL
)
602 /* It is up to the caller to actually output the insn and any
608 static char errbuf
[150];
609 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
610 const char *tmp_errmsg
;
612 /* If requesting verbose error messages, use insert_errmsg.
613 Failing that, use parse_errmsg. */
614 tmp_errmsg
= (insert_errmsg
? insert_errmsg
:
615 parse_errmsg
? parse_errmsg
:
616 recognized_mnemonic
?
617 _("unrecognized form of instruction") :
618 _("unrecognized instruction"));
620 if (strlen (start
) > 50)
621 /* xgettext:c-format */
622 sprintf (errbuf
, "%s `%.50s...'", tmp_errmsg
, start
);
624 /* xgettext:c-format */
625 sprintf (errbuf
, "%s `%.50s'", tmp_errmsg
, start
);
627 if (strlen (start
) > 50)
628 /* xgettext:c-format */
629 sprintf (errbuf
, _("bad instruction `%.50s...'"), start
);
631 /* xgettext:c-format */
632 sprintf (errbuf
, _("bad instruction `%.50s'"), start
);