| 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 <ctype.h>
|
|---|
| 30 | #include <stdio.h>
|
|---|
| 31 | #include "ansidecl.h"
|
|---|
| 32 | #include "bfd.h"
|
|---|
| 33 | #include "symcat.h"
|
|---|
| 34 | #include "@prefix@-desc.h"
|
|---|
| 35 | #include "@prefix@-opc.h"
|
|---|
| 36 | #include "opintl.h"
|
|---|
| 37 |
|
|---|
| 38 | #undef min
|
|---|
| 39 | #define min(a,b) ((a) < (b) ? (a) : (b))
|
|---|
| 40 | #undef max
|
|---|
| 41 | #define max(a,b) ((a) > (b) ? (a) : (b))
|
|---|
| 42 |
|
|---|
| 43 | static const char * parse_insn_normal
|
|---|
| 44 | PARAMS ((CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *));
|
|---|
| 45 | |
|---|
| 46 |
|
|---|
| 47 | /* -- assembler routines inserted here */
|
|---|
| 48 | |
|---|
| 49 |
|
|---|
| 50 | /* Default insn parser.
|
|---|
| 51 |
|
|---|
| 52 | The syntax string is scanned and operands are parsed and stored in FIELDS.
|
|---|
| 53 | Relocs are queued as we go via other callbacks.
|
|---|
| 54 |
|
|---|
| 55 | ??? Note that this is currently an all-or-nothing parser. If we fail to
|
|---|
| 56 | parse the instruction, we return 0 and the caller will start over from
|
|---|
| 57 | the beginning. Backtracking will be necessary in parsing subexpressions,
|
|---|
| 58 | but that can be handled there. Not handling backtracking here may get
|
|---|
| 59 | expensive in the case of the m68k. Deal with later.
|
|---|
| 60 |
|
|---|
| 61 | Returns NULL for success, an error message for failure.
|
|---|
| 62 | */
|
|---|
| 63 |
|
|---|
| 64 | static const char *
|
|---|
| 65 | parse_insn_normal (cd, insn, strp, fields)
|
|---|
| 66 | CGEN_CPU_DESC cd;
|
|---|
| 67 | const CGEN_INSN *insn;
|
|---|
| 68 | const char **strp;
|
|---|
| 69 | CGEN_FIELDS *fields;
|
|---|
| 70 | {
|
|---|
| 71 | /* ??? Runtime added insns not handled yet. */
|
|---|
| 72 | const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
|
|---|
| 73 | const char *str = *strp;
|
|---|
| 74 | const char *errmsg;
|
|---|
| 75 | const char *p;
|
|---|
| 76 | const CGEN_SYNTAX_CHAR_TYPE * syn;
|
|---|
| 77 | #ifdef CGEN_MNEMONIC_OPERANDS
|
|---|
| 78 | /* FIXME: wip */
|
|---|
| 79 | int past_opcode_p;
|
|---|
| 80 | #endif
|
|---|
| 81 |
|
|---|
| 82 | /* For now we assume the mnemonic is first (there are no leading operands).
|
|---|
| 83 | We can parse it without needing to set up operand parsing.
|
|---|
| 84 | GAS's input scrubber will ensure mnemonics are lowercase, but we may
|
|---|
| 85 | not be called from GAS. */
|
|---|
| 86 | p = CGEN_INSN_MNEMONIC (insn);
|
|---|
| 87 | while (*p && tolower (*p) == tolower (*str))
|
|---|
| 88 | ++p, ++str;
|
|---|
| 89 |
|
|---|
| 90 | if (* p)
|
|---|
| 91 | return _("unrecognized instruction");
|
|---|
| 92 |
|
|---|
| 93 | #ifndef CGEN_MNEMONIC_OPERANDS
|
|---|
| 94 | if (* str && !isspace (* str))
|
|---|
| 95 | return _("unrecognized instruction");
|
|---|
| 96 | #endif
|
|---|
| 97 |
|
|---|
| 98 | CGEN_INIT_PARSE (cd);
|
|---|
| 99 | cgen_init_parse_operand (cd);
|
|---|
| 100 | #ifdef CGEN_MNEMONIC_OPERANDS
|
|---|
| 101 | past_opcode_p = 0;
|
|---|
| 102 | #endif
|
|---|
| 103 |
|
|---|
| 104 | /* We don't check for (*str != '\0') here because we want to parse
|
|---|
| 105 | any trailing fake arguments in the syntax string. */
|
|---|
| 106 | syn = CGEN_SYNTAX_STRING (syntax);
|
|---|
| 107 |
|
|---|
| 108 | /* Mnemonics come first for now, ensure valid string. */
|
|---|
| 109 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
|
|---|
| 110 | abort ();
|
|---|
| 111 |
|
|---|
| 112 | ++syn;
|
|---|
| 113 |
|
|---|
| 114 | while (* syn != 0)
|
|---|
| 115 | {
|
|---|
| 116 | /* Non operand chars must match exactly. */
|
|---|
| 117 | if (CGEN_SYNTAX_CHAR_P (* syn))
|
|---|
| 118 | {
|
|---|
| 119 | /* FIXME: While we allow for non-GAS callers above, we assume the
|
|---|
| 120 | first char after the mnemonic part is a space. */
|
|---|
| 121 | /* FIXME: We also take inappropriate advantage of the fact that
|
|---|
| 122 | GAS's input scrubber will remove extraneous blanks. */
|
|---|
| 123 | if (tolower (*str) == tolower (CGEN_SYNTAX_CHAR (* syn)))
|
|---|
| 124 | {
|
|---|
| 125 | #ifdef CGEN_MNEMONIC_OPERANDS
|
|---|
| 126 | if (CGEN_SYNTAX_CHAR(* syn) == ' ')
|
|---|
| 127 | past_opcode_p = 1;
|
|---|
| 128 | #endif
|
|---|
| 129 | ++ syn;
|
|---|
| 130 | ++ str;
|
|---|
| 131 | }
|
|---|
| 132 | else if (*str)
|
|---|
| 133 | {
|
|---|
| 134 | /* Syntax char didn't match. Can't be this insn. */
|
|---|
| 135 | static char msg [80];
|
|---|
| 136 | /* xgettext:c-format */
|
|---|
| 137 | sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
|
|---|
| 138 | CGEN_SYNTAX_CHAR(*syn), *str);
|
|---|
| 139 | return msg;
|
|---|
| 140 | }
|
|---|
| 141 | else
|
|---|
| 142 | {
|
|---|
| 143 | /* Ran out of input. */
|
|---|
| 144 | static char msg [80];
|
|---|
| 145 | /* xgettext:c-format */
|
|---|
| 146 | sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
|
|---|
| 147 | CGEN_SYNTAX_CHAR(*syn));
|
|---|
| 148 | return msg;
|
|---|
| 149 | }
|
|---|
| 150 | continue;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /* We have an operand of some sort. */
|
|---|
| 154 | errmsg = @arch@_cgen_parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
|
|---|
| 155 | &str, fields);
|
|---|
| 156 | if (errmsg)
|
|---|
| 157 | return errmsg;
|
|---|
| 158 |
|
|---|
| 159 | /* Done with this operand, continue with next one. */
|
|---|
| 160 | ++ syn;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | /* If we're at the end of the syntax string, we're done. */
|
|---|
| 164 | if (* syn == 0)
|
|---|
| 165 | {
|
|---|
| 166 | /* FIXME: For the moment we assume a valid `str' can only contain
|
|---|
| 167 | blanks now. IE: We needn't try again with a longer version of
|
|---|
| 168 | the insn and it is assumed that longer versions of insns appear
|
|---|
| 169 | before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
|
|---|
| 170 | while (isspace (* str))
|
|---|
| 171 | ++ str;
|
|---|
| 172 |
|
|---|
| 173 | if (* str != '\0')
|
|---|
| 174 | return _("junk at end of line"); /* FIXME: would like to include `str' */
|
|---|
| 175 |
|
|---|
| 176 | return NULL;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /* We couldn't parse it. */
|
|---|
| 180 | return _("unrecognized instruction");
|
|---|
| 181 | }
|
|---|
| 182 | |
|---|
| 183 |
|
|---|
| 184 | /* Main entry point.
|
|---|
| 185 | This routine is called for each instruction to be assembled.
|
|---|
| 186 | STR points to the insn to be assembled.
|
|---|
| 187 | We assume all necessary tables have been initialized.
|
|---|
| 188 | The assembled instruction, less any fixups, is stored in BUF.
|
|---|
| 189 | Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
|
|---|
| 190 | still needs to be converted to target byte order, otherwise BUF is an array
|
|---|
| 191 | of bytes in target byte order.
|
|---|
| 192 | The result is a pointer to the insn's entry in the opcode table,
|
|---|
| 193 | or NULL if an error occured (an error message will have already been
|
|---|
| 194 | printed).
|
|---|
| 195 |
|
|---|
| 196 | Note that when processing (non-alias) macro-insns,
|
|---|
| 197 | this function recurses.
|
|---|
| 198 |
|
|---|
| 199 | ??? It's possible to make this cpu-independent.
|
|---|
| 200 | One would have to deal with a few minor things.
|
|---|
| 201 | At this point in time doing so would be more of a curiosity than useful
|
|---|
| 202 | [for example this file isn't _that_ big], but keeping the possibility in
|
|---|
| 203 | mind helps keep the design clean. */
|
|---|
| 204 |
|
|---|
| 205 | const CGEN_INSN *
|
|---|
| 206 | @arch@_cgen_assemble_insn (cd, str, fields, buf, errmsg)
|
|---|
| 207 | CGEN_CPU_DESC cd;
|
|---|
| 208 | const char *str;
|
|---|
| 209 | CGEN_FIELDS *fields;
|
|---|
| 210 | CGEN_INSN_BYTES_PTR buf;
|
|---|
| 211 | char **errmsg;
|
|---|
| 212 | {
|
|---|
| 213 | const char *start;
|
|---|
| 214 | CGEN_INSN_LIST *ilist;
|
|---|
| 215 | const char *parse_errmsg = NULL;
|
|---|
| 216 | const char *insert_errmsg = NULL;
|
|---|
| 217 |
|
|---|
| 218 | /* Skip leading white space. */
|
|---|
| 219 | while (isspace (* str))
|
|---|
| 220 | ++ str;
|
|---|
| 221 |
|
|---|
| 222 | /* The instructions are stored in hashed lists.
|
|---|
| 223 | Get the first in the list. */
|
|---|
| 224 | ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
|
|---|
| 225 |
|
|---|
| 226 | /* Keep looking until we find a match. */
|
|---|
| 227 |
|
|---|
| 228 | start = str;
|
|---|
| 229 | for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
|
|---|
| 230 | {
|
|---|
| 231 | const CGEN_INSN *insn = ilist->insn;
|
|---|
| 232 |
|
|---|
| 233 | #ifdef CGEN_VALIDATE_INSN_SUPPORTED
|
|---|
| 234 | /* not usually needed as unsupported opcodes shouldn't be in the hash lists */
|
|---|
| 235 | /* Is this insn supported by the selected cpu? */
|
|---|
| 236 | if (! @arch@_cgen_insn_supported (cd, insn))
|
|---|
| 237 | continue;
|
|---|
| 238 | #endif
|
|---|
| 239 |
|
|---|
| 240 | /* If the RELAX attribute is set, this is an insn that shouldn't be
|
|---|
| 241 | chosen immediately. Instead, it is used during assembler/linker
|
|---|
| 242 | relaxation if possible. */
|
|---|
| 243 | if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAX) != 0)
|
|---|
| 244 | continue;
|
|---|
| 245 |
|
|---|
| 246 | str = start;
|
|---|
| 247 |
|
|---|
| 248 | /* Allow parse/insert handlers to obtain length of insn. */
|
|---|
| 249 | CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
|
|---|
| 250 |
|
|---|
| 251 | parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
|
|---|
| 252 | if (parse_errmsg != NULL)
|
|---|
| 253 | continue;
|
|---|
| 254 |
|
|---|
| 255 | /* ??? 0 is passed for `pc' */
|
|---|
| 256 | insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
|
|---|
| 257 | (bfd_vma) 0);
|
|---|
| 258 | if (insert_errmsg != NULL)
|
|---|
| 259 | continue;
|
|---|
| 260 |
|
|---|
| 261 | /* It is up to the caller to actually output the insn and any
|
|---|
| 262 | queued relocs. */
|
|---|
| 263 | return insn;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | {
|
|---|
| 267 | static char errbuf[150];
|
|---|
| 268 | const char *tmp_errmsg;
|
|---|
| 269 |
|
|---|
| 270 | #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
|
|---|
| 271 | /* If requesting verbose error messages, use insert_errmsg.
|
|---|
| 272 | Failing that, use parse_errmsg */
|
|---|
| 273 | tmp_errmsg = (insert_errmsg ? insert_errmsg :
|
|---|
| 274 | parse_errmsg ? parse_errmsg :
|
|---|
| 275 | _("unrecognized instruction"));
|
|---|
| 276 |
|
|---|
| 277 | if (strlen (start) > 50)
|
|---|
| 278 | /* xgettext:c-format */
|
|---|
| 279 | sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
|
|---|
| 280 | else
|
|---|
| 281 | /* xgettext:c-format */
|
|---|
| 282 | sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
|
|---|
| 283 | #else
|
|---|
| 284 | if (strlen (start) > 50)
|
|---|
| 285 | /* xgettext:c-format */
|
|---|
| 286 | sprintf (errbuf, _("bad instruction `%.50s...'"), start);
|
|---|
| 287 | else
|
|---|
| 288 | /* xgettext:c-format */
|
|---|
| 289 | sprintf (errbuf, _("bad instruction `%.50s'"), start);
|
|---|
| 290 | #endif
|
|---|
| 291 |
|
|---|
| 292 | *errmsg = errbuf;
|
|---|
| 293 | return NULL;
|
|---|
| 294 | }
|
|---|
| 295 | }
|
|---|
| 296 | |
|---|
| 297 |
|
|---|
| 298 | #if 0 /* This calls back to GAS which we can't do without care. */
|
|---|
| 299 |
|
|---|
| 300 | /* Record each member of OPVALS in the assembler's symbol table.
|
|---|
| 301 | This lets GAS parse registers for us.
|
|---|
| 302 | ??? Interesting idea but not currently used. */
|
|---|
| 303 |
|
|---|
| 304 | /* Record each member of OPVALS in the assembler's symbol table.
|
|---|
| 305 | FIXME: Not currently used. */
|
|---|
| 306 |
|
|---|
| 307 | void
|
|---|
| 308 | @arch@_cgen_asm_hash_keywords (cd, opvals)
|
|---|
| 309 | CGEN_CPU_DESC cd;
|
|---|
| 310 | CGEN_KEYWORD *opvals;
|
|---|
| 311 | {
|
|---|
| 312 | CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL);
|
|---|
| 313 | const CGEN_KEYWORD_ENTRY * ke;
|
|---|
| 314 |
|
|---|
| 315 | while ((ke = cgen_keyword_search_next (& search)) != NULL)
|
|---|
| 316 | {
|
|---|
| 317 | #if 0 /* Unnecessary, should be done in the search routine. */
|
|---|
| 318 | if (! @arch@_cgen_opval_supported (ke))
|
|---|
| 319 | continue;
|
|---|
| 320 | #endif
|
|---|
| 321 | cgen_asm_record_register (cd, ke->name, ke->value);
|
|---|
| 322 | }
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | #endif /* 0 */
|
|---|