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 Free Software Foundation, Inc.
9 This file is part of the GNU Binutils and GDB, the GNU debugger.
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)
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.
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. */
25 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
31 #include "safe-ctype.h"
34 #include "openrisc-desc.h"
35 #include "openrisc-opc.h"
38 #include "libiberty.h"
41 #define min(a,b) ((a) < (b) ? (a) : (b))
43 #define max(a,b) ((a) > (b) ? (a) : (b))
45 static const char * parse_insn_normal
46 PARAMS ((CGEN_CPU_DESC
, const CGEN_INSN
*, const char **, CGEN_FIELDS
*));
47 long openrisc_sign_extend_16bit
49 static const char * parse_hi16
50 PARAMS ((CGEN_CPU_DESC
, const char **, int, unsigned long *));
51 static const char * parse_lo16
52 PARAMS ((CGEN_CPU_DESC
, const char **, int, unsigned long *));
53 const char * openrisc_cgen_parse_operand
54 PARAMS ((CGEN_CPU_DESC
, int, const char **, CGEN_FIELDS
*));
56 /* -- assembler routines inserted here */
60 #define CGEN_VERBOSE_ASSEMBLER_ERRORS
63 openrisc_sign_extend_16bit (value
)
66 return (long) (short) value
;
73 parse_hi16 (cd
, strp
, opindex
, valuep
)
77 unsigned long *valuep
;
80 enum cgen_parse_operand_result result_type
;
86 if (strncasecmp (*strp
, "hi(", 3) == 0)
91 errmsg
= cgen_parse_signed_integer (cd
, strp
, opindex
, valuep
);
93 fprintf (stderr
, "parse_hi: %s\n", errmsg
);
96 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_HI16
,
97 &result_type
, &value
);
102 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
104 *valuep
= (long) (short) value
;
111 errmsg
= cgen_parse_signed_integer (cd
, strp
, opindex
, (long *) &value
);
113 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, opindex
, (unsigned long *) &value
);
115 *valuep
= (long) (short) (value
& 0xffff);
123 parse_lo16 (cd
, strp
, opindex
, valuep
)
127 unsigned long *valuep
;
130 enum cgen_parse_operand_result result_type
;
136 if (strncasecmp (*strp
, "lo(", 3) == 0)
141 errmsg
= cgen_parse_signed_integer (cd
, strp
, opindex
, valuep
);
143 fprintf (stderr
, "parse_lo: %s\n", errmsg
);
147 errmsg
= cgen_parse_address (cd
, strp
, opindex
, BFD_RELOC_LO16
,
148 &result_type
, &value
);
150 return "missing `)'";
153 && result_type
== CGEN_PARSE_OPERAND_RESULT_NUMBER
)
155 *valuep
= (long) (short) value
;
161 errmsg
= cgen_parse_signed_integer (cd
, strp
, opindex
, (long *) &value
);
163 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, opindex
, (unsigned long *) &value
);
164 *valuep
= (long) (short) (value
& 0xffff);
170 /* Main entry point for operand parsing.
172 This function is basically just a big switch statement. Earlier versions
173 used tables to look up the function to use, but
174 - if the table contains both assembler and disassembler functions then
175 the disassembler contains much of the assembler and vice-versa,
176 - there's a lot of inlining possibilities as things grow,
177 - using a switch statement avoids the function call overhead.
179 This function could be moved into `parse_insn_normal', but keeping it
180 separate makes clear the interface between `parse_insn_normal' and each of
185 openrisc_cgen_parse_operand (cd
, opindex
, strp
, fields
)
189 CGEN_FIELDS
* fields
;
191 const char * errmsg
= NULL
;
192 /* Used by scalar operands that still need to be parsed. */
193 long junk ATTRIBUTE_UNUSED
;
197 case OPENRISC_OPERAND_ABS_26
:
200 errmsg
= cgen_parse_address (cd
, strp
, OPENRISC_OPERAND_ABS_26
, 0, NULL
, & value
);
201 fields
->f_abs26
= value
;
204 case OPENRISC_OPERAND_DISP_26
:
207 errmsg
= cgen_parse_address (cd
, strp
, OPENRISC_OPERAND_DISP_26
, 0, NULL
, & value
);
208 fields
->f_disp26
= value
;
211 case OPENRISC_OPERAND_HI16
:
212 errmsg
= parse_hi16 (cd
, strp
, OPENRISC_OPERAND_HI16
, &fields
->f_simm16
);
214 case OPENRISC_OPERAND_LO16
:
215 errmsg
= parse_lo16 (cd
, strp
, OPENRISC_OPERAND_LO16
, &fields
->f_lo16
);
217 case OPENRISC_OPERAND_OP_F_23
:
218 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, OPENRISC_OPERAND_OP_F_23
, &fields
->f_op4
);
220 case OPENRISC_OPERAND_OP_F_3
:
221 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, OPENRISC_OPERAND_OP_F_3
, &fields
->f_op5
);
223 case OPENRISC_OPERAND_RA
:
224 errmsg
= cgen_parse_keyword (cd
, strp
, & openrisc_cgen_opval_h_gr
, & fields
->f_r2
);
226 case OPENRISC_OPERAND_RB
:
227 errmsg
= cgen_parse_keyword (cd
, strp
, & openrisc_cgen_opval_h_gr
, & fields
->f_r3
);
229 case OPENRISC_OPERAND_RD
:
230 errmsg
= cgen_parse_keyword (cd
, strp
, & openrisc_cgen_opval_h_gr
, & fields
->f_r1
);
232 case OPENRISC_OPERAND_SIMM_16
:
233 errmsg
= cgen_parse_signed_integer (cd
, strp
, OPENRISC_OPERAND_SIMM_16
, &fields
->f_simm16
);
235 case OPENRISC_OPERAND_UI16NC
:
236 errmsg
= parse_lo16 (cd
, strp
, OPENRISC_OPERAND_UI16NC
, &fields
->f_i16nc
);
238 case OPENRISC_OPERAND_UIMM_16
:
239 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, OPENRISC_OPERAND_UIMM_16
, &fields
->f_uimm16
);
241 case OPENRISC_OPERAND_UIMM_5
:
242 errmsg
= cgen_parse_unsigned_integer (cd
, strp
, OPENRISC_OPERAND_UIMM_5
, &fields
->f_uimm5
);
246 /* xgettext:c-format */
247 fprintf (stderr
, _("Unrecognized field %d while parsing.\n"), opindex
);
254 cgen_parse_fn
* const openrisc_cgen_parse_handlers
[] =
260 openrisc_cgen_init_asm (cd
)
263 openrisc_cgen_init_opcode_table (cd
);
264 openrisc_cgen_init_ibld_table (cd
);
265 cd
->parse_handlers
= & openrisc_cgen_parse_handlers
[0];
266 cd
->parse_operand
= openrisc_cgen_parse_operand
;
272 Regex construction routine.
274 This translates an opcode syntax string into a regex string,
275 by replacing any non-character syntax element (such as an
276 opcode) with the pattern '.*'
278 It then compiles the regex and stores it in the opcode, for
279 later use by openrisc_cgen_assemble_insn
281 returns NULL for success, an error message for failure
285 openrisc_cgen_build_insn_regex (insn
)
288 CGEN_OPCODE
*opc
= (CGEN_OPCODE
*) CGEN_INSN_OPCODE (insn
);
289 const char *mnem
= CGEN_INSN_MNEMONIC (insn
);
291 char rxbuf
[CGEN_MAX_RX_ELEMENTS
];
293 const CGEN_SYNTAX_CHAR_TYPE
*syn
;
296 syn
= CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc
));
298 /* Mnemonics come first in the syntax string */
299 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
)) return "missing mnemonic in syntax string";
302 /* copy the literal mnemonic out of the insn */
303 memset (rx
, 0, CGEN_MAX_RX_ELEMENTS
);
304 mnem_len
= strlen(mnem
);
305 memcpy (rx
, mnem
, mnem_len
);
308 /* copy any remaining literals from the syntax string into the rx */
309 for(; * syn
!= 0 && rx
< rxbuf
+ (CGEN_MAX_RX_ELEMENTS
- 9); ++syn
, ++rx
)
311 if (CGEN_SYNTAX_CHAR_P (* syn
))
313 char tmp
= CGEN_SYNTAX_CHAR (* syn
);
316 /* escape any regex metacharacters in the syntax */
317 case '.': case '[': case '\\':
318 case '*': case '^': case '$':
320 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
321 case '?': case '{': case '}':
322 case '(': case ')': case '*':
323 case '|': case '+': case ']':
329 /* insert syntax char into rx */
334 /* replace non-syntax fields with globs */
340 /* trailing whitespace ok */
347 /* but anchor it after that */
351 CGEN_INSN_RX (insn
) = xmalloc (sizeof (regex_t
));
352 reg_err
= regcomp ((regex_t
*) CGEN_INSN_RX (insn
), rxbuf
, REG_NOSUB
|REG_ICASE
);
359 regerror (reg_err
, (regex_t
*) CGEN_INSN_RX (insn
), msg
, 80);
360 regfree ((regex_t
*) CGEN_INSN_RX (insn
));
361 free (CGEN_INSN_RX (insn
));
362 (CGEN_INSN_RX (insn
)) = NULL
;
368 /* Default insn parser.
370 The syntax string is scanned and operands are parsed and stored in FIELDS.
371 Relocs are queued as we go via other callbacks.
373 ??? Note that this is currently an all-or-nothing parser. If we fail to
374 parse the instruction, we return 0 and the caller will start over from
375 the beginning. Backtracking will be necessary in parsing subexpressions,
376 but that can be handled there. Not handling backtracking here may get
377 expensive in the case of the m68k. Deal with later.
379 Returns NULL for success, an error message for failure.
383 parse_insn_normal (cd
, insn
, strp
, fields
)
385 const CGEN_INSN
*insn
;
389 /* ??? Runtime added insns not handled yet. */
390 const CGEN_SYNTAX
*syntax
= CGEN_INSN_SYNTAX (insn
);
391 const char *str
= *strp
;
394 const CGEN_SYNTAX_CHAR_TYPE
* syn
;
395 #ifdef CGEN_MNEMONIC_OPERANDS
400 /* For now we assume the mnemonic is first (there are no leading operands).
401 We can parse it without needing to set up operand parsing.
402 GAS's input scrubber will ensure mnemonics are lowercase, but we may
403 not be called from GAS. */
404 p
= CGEN_INSN_MNEMONIC (insn
);
405 while (*p
&& TOLOWER (*p
) == TOLOWER (*str
))
409 return _("unrecognized instruction");
411 #ifndef CGEN_MNEMONIC_OPERANDS
412 if (* str
&& !ISSPACE (* str
))
413 return _("unrecognized instruction");
416 CGEN_INIT_PARSE (cd
);
417 cgen_init_parse_operand (cd
);
418 #ifdef CGEN_MNEMONIC_OPERANDS
422 /* We don't check for (*str != '\0') here because we want to parse
423 any trailing fake arguments in the syntax string. */
424 syn
= CGEN_SYNTAX_STRING (syntax
);
426 /* Mnemonics come first for now, ensure valid string. */
427 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
434 /* Non operand chars must match exactly. */
435 if (CGEN_SYNTAX_CHAR_P (* syn
))
437 /* FIXME: While we allow for non-GAS callers above, we assume the
438 first char after the mnemonic part is a space. */
439 /* FIXME: We also take inappropriate advantage of the fact that
440 GAS's input scrubber will remove extraneous blanks. */
441 if (TOLOWER (*str
) == TOLOWER (CGEN_SYNTAX_CHAR (* syn
)))
443 #ifdef CGEN_MNEMONIC_OPERANDS
444 if (CGEN_SYNTAX_CHAR(* syn
) == ' ')
452 /* Syntax char didn't match. Can't be this insn. */
453 static char msg
[80];
454 /* xgettext:c-format */
455 sprintf (msg
, _("syntax error (expected char `%c', found `%c')"),
456 CGEN_SYNTAX_CHAR(*syn
), *str
);
461 /* Ran out of input. */
462 static char msg
[80];
463 /* xgettext:c-format */
464 sprintf (msg
, _("syntax error (expected char `%c', found end of instruction)"),
465 CGEN_SYNTAX_CHAR(*syn
));
471 /* We have an operand of some sort. */
472 errmsg
= openrisc_cgen_parse_operand (cd
, CGEN_SYNTAX_FIELD (*syn
),
477 /* Done with this operand, continue with next one. */
481 /* If we're at the end of the syntax string, we're done. */
484 /* FIXME: For the moment we assume a valid `str' can only contain
485 blanks now. IE: We needn't try again with a longer version of
486 the insn and it is assumed that longer versions of insns appear
487 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
488 while (ISSPACE (* str
))
492 return _("junk at end of line"); /* FIXME: would like to include `str' */
497 /* We couldn't parse it. */
498 return _("unrecognized instruction");
502 This routine is called for each instruction to be assembled.
503 STR points to the insn to be assembled.
504 We assume all necessary tables have been initialized.
505 The assembled instruction, less any fixups, is stored in BUF.
506 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
507 still needs to be converted to target byte order, otherwise BUF is an array
508 of bytes in target byte order.
509 The result is a pointer to the insn's entry in the opcode table,
510 or NULL if an error occured (an error message will have already been
513 Note that when processing (non-alias) macro-insns,
514 this function recurses.
516 ??? It's possible to make this cpu-independent.
517 One would have to deal with a few minor things.
518 At this point in time doing so would be more of a curiosity than useful
519 [for example this file isn't _that_ big], but keeping the possibility in
520 mind helps keep the design clean. */
523 openrisc_cgen_assemble_insn (cd
, str
, fields
, buf
, errmsg
)
527 CGEN_INSN_BYTES_PTR buf
;
531 CGEN_INSN_LIST
*ilist
;
532 const char *parse_errmsg
= NULL
;
533 const char *insert_errmsg
= NULL
;
534 int recognized_mnemonic
= 0;
536 /* Skip leading white space. */
537 while (ISSPACE (* str
))
540 /* The instructions are stored in hashed lists.
541 Get the first in the list. */
542 ilist
= CGEN_ASM_LOOKUP_INSN (cd
, str
);
544 /* Keep looking until we find a match. */
547 for ( ; ilist
!= NULL
; ilist
= CGEN_ASM_NEXT_INSN (ilist
))
549 const CGEN_INSN
*insn
= ilist
->insn
;
550 recognized_mnemonic
= 1;
552 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
553 /* not usually needed as unsupported opcodes shouldn't be in the hash lists */
554 /* Is this insn supported by the selected cpu? */
555 if (! openrisc_cgen_insn_supported (cd
, insn
))
559 /* If the RELAX attribute is set, this is an insn that shouldn't be
560 chosen immediately. Instead, it is used during assembler/linker
561 relaxation if possible. */
562 if (CGEN_INSN_ATTR_VALUE (insn
, CGEN_INSN_RELAX
) != 0)
567 /* skip this insn if str doesn't look right lexically */
568 if (CGEN_INSN_RX (insn
) != NULL
&&
569 regexec ((regex_t
*) CGEN_INSN_RX (insn
), str
, 0, NULL
, 0) == REG_NOMATCH
)
572 /* Allow parse/insert handlers to obtain length of insn. */
573 CGEN_FIELDS_BITSIZE (fields
) = CGEN_INSN_BITSIZE (insn
);
575 parse_errmsg
= CGEN_PARSE_FN (cd
, insn
) (cd
, insn
, & str
, fields
);
576 if (parse_errmsg
!= NULL
)
579 /* ??? 0 is passed for `pc' */
580 insert_errmsg
= CGEN_INSERT_FN (cd
, insn
) (cd
, insn
, fields
, buf
,
582 if (insert_errmsg
!= NULL
)
585 /* It is up to the caller to actually output the insn and any
591 static char errbuf
[150];
592 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
593 const char *tmp_errmsg
;
595 /* If requesting verbose error messages, use insert_errmsg.
596 Failing that, use parse_errmsg */
597 tmp_errmsg
= (insert_errmsg
? insert_errmsg
:
598 parse_errmsg
? parse_errmsg
:
599 recognized_mnemonic
? _("unrecognized form of instruction") :
600 _("unrecognized instruction"));
602 if (strlen (start
) > 50)
603 /* xgettext:c-format */
604 sprintf (errbuf
, "%s `%.50s...'", tmp_errmsg
, start
);
606 /* xgettext:c-format */
607 sprintf (errbuf
, "%s `%.50s'", tmp_errmsg
, start
);
609 if (strlen (start
) > 50)
610 /* xgettext:c-format */
611 sprintf (errbuf
, _("bad instruction `%.50s...'"), start
);
613 /* xgettext:c-format */
614 sprintf (errbuf
, _("bad instruction `%.50s'"), start
);
622 #if 0 /* This calls back to GAS which we can't do without care. */
624 /* Record each member of OPVALS in the assembler's symbol table.
625 This lets GAS parse registers for us.
626 ??? Interesting idea but not currently used. */
628 /* Record each member of OPVALS in the assembler's symbol table.
629 FIXME: Not currently used. */
632 openrisc_cgen_asm_hash_keywords (cd
, opvals
)
634 CGEN_KEYWORD
*opvals
;
636 CGEN_KEYWORD_SEARCH search
= cgen_keyword_search_init (opvals
, NULL
);
637 const CGEN_KEYWORD_ENTRY
* ke
;
639 while ((ke
= cgen_keyword_search_next (& search
)) != NULL
)
641 #if 0 /* Unnecessary, should be done in the search routine. */
642 if (! openrisc_cgen_opval_supported (ke
))
645 cgen_asm_record_register (cd
, ke
->name
, ke
->value
);