1 /* Assembler interface for targets using CGEN. -*- C -*-
2 CGEN: Cpu tools GENerator
4 This file is used to generate @arch@-asm.c.
6 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
8 This file is part of the GNU Binutils and GDB, the GNU debugger.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
30 #include "@arch@-opc.h"
32 /* ??? The layout of this stuff is still work in progress.
33 For speed in assembly/disassembly, we use inline functions. That of course
34 will only work for GCC. When this stuff is finished, we can decide whether
35 to keep the inline functions (and only get the performance increase when
36 compiled with GCC), or switch to macros, or use something else.
39 static const char * insert_normal
40 PARAMS ((long, unsigned int, int, int, int, char *));
41 static const char * parse_insn_normal
42 PARAMS ((const CGEN_INSN *, const char **, CGEN_FIELDS *));
43 static const char * insert_insn_normal
44 PARAMS ((const CGEN_INSN *, CGEN_FIELDS *, cgen_insn_t *));
46 /* -- assembler routines inserted here */
48 /* Default insertion routine.
50 ATTRS is a mask of the boolean attributes.
51 LENGTH is the length of VALUE in bits.
52 TOTAL_LENGTH is the total length of the insn (currently 8,16,32).
54 The result is an error message or NULL if success. */
56 /* ??? This duplicates functionality with bfd's howto table and
57 bfd_install_relocation. */
58 /* ??? For architectures where insns can be representable as ints,
59 store insn in `field' struct and add registers, etc. while parsing? */
62 insert_normal (value, attrs, start, length, total_length, buffer)
73 /* Ensure VALUE will fit. */
74 if ((attrs & (1 << CGEN_OPERAND_UNSIGNED)) != 0)
76 unsigned long max = (1 << length) - 1;
77 if ((unsigned long) value > max)
79 const char *err = "operand out of range (%lu not between 0 and %lu)";
81 sprintf (buf, err, value, max);
87 long min = - (1 << (length - 1));
88 long max = (1 << (length - 1)) - 1;
89 if (value < min || value > max)
91 const char *err = "operand out of range (%ld not between %ld and %ld)";
93 sprintf (buf, err, value, min, max);
98 #if 0 /*def CGEN_INT_INSN*/
99 *buffer |= ((value & ((1 << length) - 1))
100 << (total_length - (start + length)));
102 switch (total_length)
105 x = * (unsigned char *) buffer;
108 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
109 x = bfd_getb16 (buffer);
111 x = bfd_getl16 (buffer);
114 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
115 x = bfd_getb32 (buffer);
117 x = bfd_getl32 (buffer);
123 x |= ((value & ((1 << length) - 1))
124 << (total_length - (start + length)));
126 switch (total_length)
132 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
133 bfd_putb16 (x, buffer);
135 bfd_putl16 (x, buffer);
138 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
139 bfd_putb32 (x, buffer);
141 bfd_putl32 (x, buffer);
151 /* Default insn parser.
153 The syntax string is scanned and operands are parsed and stored in FIELDS.
154 Relocs are queued as we go via other callbacks.
156 ??? Note that this is currently an all-or-nothing parser. If we fail to
157 parse the instruction, we return 0 and the caller will start over from
158 the beginning. Backtracking will be necessary in parsing subexpressions,
159 but that can be handled there. Not handling backtracking here may get
160 expensive in the case of the m68k. Deal with later.
162 Returns NULL for success, an error message for failure.
166 parse_insn_normal (insn, strp, fields)
167 const CGEN_INSN * insn;
169 CGEN_FIELDS * fields;
171 const CGEN_SYNTAX * syntax = CGEN_INSN_SYNTAX (insn);
172 const char * str = *strp;
175 const unsigned char * syn;
176 #ifdef CGEN_MNEMONIC_OPERANDS
180 /* For now we assume the mnemonic is first (there are no leading operands).
181 We can parse it without needing to set up operand parsing. */
182 p = CGEN_INSN_MNEMONIC (insn);
183 while (* p && * p == * str)
185 if (* p || (* str && !isspace (* str)))
186 return "unrecognized instruction";
189 cgen_init_parse_operand ();
190 #ifdef CGEN_MNEMONIC_OPERANDS
194 /* We don't check for (*str != '\0') here because we want to parse
195 any trailing fake arguments in the syntax string. */
196 syn = CGEN_SYNTAX_STRING (CGEN_INSN_SYNTAX (insn));
198 /* Mnemonics come first for now, ensure valid string. */
199 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
206 /* Non operand chars must match exactly. */
207 /* FIXME: Need to better handle whitespace. */
208 if (CGEN_SYNTAX_CHAR_P (* syn))
210 if (*str == CGEN_SYNTAX_CHAR (* syn))
212 #ifdef CGEN_MNEMONIC_OPERANDS
221 /* Syntax char didn't match. Can't be this insn. */
222 /* FIXME: would like to return something like
223 "expected char `c'" */
224 return "syntax error";
229 /* We have an operand of some sort. */
230 errmsg = @arch@_cgen_parse_operand (CGEN_SYNTAX_FIELD (*syn),
235 /* Done with this operand, continue with next one. */
239 /* If we're at the end of the syntax string, we're done. */
242 /* FIXME: For the moment we assume a valid `str' can only contain
243 blanks now. IE: We needn't try again with a longer version of
244 the insn and it is assumed that longer versions of insns appear
245 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
246 while (isspace (* str))
250 return "junk at end of line"; /* FIXME: would like to include `str' */
255 /* We couldn't parse it. */
256 return "unrecognized instruction";
259 /* Default insn builder (insert handler).
260 The instruction is recorded in target byte order.
261 The result is an error message or NULL if success. */
262 /* FIXME: change buffer to char *? */
265 insert_insn_normal (insn, fields, buffer)
266 const CGEN_INSN * insn;
267 CGEN_FIELDS * fields;
268 cgen_insn_t * buffer;
270 const CGEN_SYNTAX * syntax = CGEN_INSN_SYNTAX (insn);
272 const unsigned char * syn;
275 value = CGEN_INSN_VALUE (insn);
277 /* If we're recording insns as numbers (rather than a string of bytes),
278 target byte order handling is deferred until later. */
280 #define min(a,b) ((a) < (b) ? (a) : (b))
281 #if 0 /*def CGEN_INT_INSN*/
284 switch (min (CGEN_BASE_INSN_BITSIZE, CGEN_FIELDS_BITSIZE (fields)))
290 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
291 bfd_putb16 (value, (char *) buffer);
293 bfd_putl16 (value, (char *) buffer);
296 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
297 bfd_putb32 (value, (char *) buffer);
299 bfd_putl32 (value, (char *) buffer);
306 /* ??? Rather than scanning the syntax string again, we could store
307 in `fields' a null terminated list of the fields that are present. */
309 for (syn = CGEN_SYNTAX_STRING (syntax); * syn != '\0'; ++ syn)
313 if (CGEN_SYNTAX_CHAR_P (* syn))
316 errmsg = @arch@_cgen_insert_operand (CGEN_SYNTAX_FIELD (*syn), fields,
326 This routine is called for each instruction to be assembled.
327 STR points to the insn to be assembled.
328 We assume all necessary tables have been initialized.
329 The assembled instruction, less any fixups, is stored in buf.
330 [??? What byte order?]
331 The result is a pointer to the insn's entry in the opcode table,
332 or NULL if an error occured (an error message will have already been
335 Note that when processing (non-alias) macro-insns,
336 this function recurses. */
339 @arch@_cgen_assemble_insn (str, fields, buf, errmsg)
341 CGEN_FIELDS * fields;
346 CGEN_INSN_LIST * ilist;
348 /* Skip leading white space. */
349 while (isspace (* str))
352 /* The instructions are stored in hashed lists.
353 Get the first in the list. */
354 ilist = CGEN_ASM_LOOKUP_INSN (str);
356 /* Keep looking until we find a match. */
359 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
361 const CGEN_INSN *insn = ilist->insn;
363 #if 0 /* not needed as unsupported opcodes shouldn't be in the hash lists */
364 /* Is this insn supported by the selected cpu? */
365 if (! @arch@_cgen_insn_supported (insn))
369 #if 1 /* FIXME: wip */
370 /* If the RELAX attribute is set, this is an insn that shouldn't be
371 chosen immediately. Instead, it is used during assembler/linker
372 relaxation if possible. */
373 if (CGEN_INSN_ATTR (insn, CGEN_INSN_RELAX) != 0)
379 /* Record a default length for the insn. This will get set to the
380 correct value while parsing. */
382 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
384 if (! CGEN_PARSE_FN (insn) (insn, & str, fields))
386 if (CGEN_INSERT_FN (insn) (insn, fields, buf) != NULL)
388 /* It is up to the caller to actually output the insn and any
393 /* Try the next entry. */
396 /* FIXME: We can return a better error message than this.
397 Need to track why it failed and pick the right one. */
399 static char errbuf[100];
400 sprintf (errbuf, "bad instruction `%.50s%s'",
401 start, strlen (start) > 50 ? "..." : "");
407 #if 0 /* This calls back to GAS which we can't do without care. */
409 /* Record each member of OPVALS in the assembler's symbol table.
410 This lets GAS parse registers for us.
411 ??? Interesting idea but not currently used. */
413 /* Record each member of OPVALS in the assembler's symbol table.
414 FIXME: Not currently used. */
417 @arch@_cgen_asm_hash_keywords (opvals)
418 CGEN_KEYWORD * opvals;
420 CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL);
421 const CGEN_KEYWORD_ENTRY * ke;
423 while ((ke = cgen_keyword_search_next (& search)) != NULL)
425 #if 0 /* Unnecessary, should be done in the search routine. */
426 if (! @arch@_cgen_opval_supported (ke))
429 cgen_asm_record_register (ke->name, ke->value);