1 /* Assembler interface for targets using CGEN. -*- C -*-
2 CGEN: Cpu tools GENerator
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 Foundation, Inc.,
22 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 /* ??? The layout of this stuff is still work in progress.
34 For speed in assembly/disassembly, we use inline functions. That of course
35 will only work for GCC. When this stuff is finished, we can decide whether
36 to keep the inline functions (and only get the performance increase when
37 compiled with GCC), or switch to macros, or use something else.
40 static const char * insert_normal
41 PARAMS ((long, unsigned int, int, int, int, char *));
42 static const char * parse_insn_normal
43 PARAMS ((const CGEN_INSN *, const char **, CGEN_FIELDS *));
44 static const char * insert_insn_normal
45 PARAMS ((const CGEN_INSN *, CGEN_FIELDS *, cgen_insn_t *, bfd_vma));
47 /* -- assembler routines inserted here */
49 /* Default insertion routine.
51 ATTRS is a mask of the boolean attributes.
52 LENGTH is the length of VALUE in bits.
53 TOTAL_LENGTH is the total length of the insn (currently 8,16,32).
55 The result is an error message or NULL if success. */
57 /* ??? This duplicates functionality with bfd's howto table and
58 bfd_install_relocation. */
59 /* ??? For architectures where insns can be representable as ints,
60 store insn in `field' struct and add registers, etc. while parsing? */
63 insert_normal (value, attrs, start, length, total_length, buffer)
73 /* Written this way to avoid undefined behaviour.
74 Yes, `long' will be bfd_vma but not yet. */
75 long mask = (((1L << (length - 1)) - 1) << 1) | 1;
77 /* If LENGTH is zero, this operand doesn't contribute to the value. */
81 /* Ensure VALUE will fit. */
82 if ((attrs & CGEN_ATTR_MASK (CGEN_OPERAND_UNSIGNED)) != 0)
84 unsigned long max = mask;
85 if ((unsigned long) value > max)
87 /* xgettext:c-format */
88 sprintf (buf, _("operand out of range (%lu not between 0 and %lu)"),
95 long min = - (1L << (length - 1));
96 long max = (1L << (length - 1)) - 1;
97 if (value < min || value > max)
100 /* xgettext:c-format */
101 (buf, _("operand out of range (%ld not between %ld and %ld)"),
107 #if 0 /*def CGEN_INT_INSN*/
108 *buffer |= (value & mask) << (total_length - (start + length));
110 switch (total_length)
113 x = * (unsigned char *) buffer;
116 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
117 x = bfd_getb16 (buffer);
119 x = bfd_getl16 (buffer);
122 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
123 x = bfd_getb32 (buffer);
125 x = bfd_getl32 (buffer);
131 x |= (value & mask) << (total_length - (start + length));
133 switch (total_length)
139 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
140 bfd_putb16 (x, buffer);
142 bfd_putl16 (x, buffer);
145 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
146 bfd_putb32 (x, buffer);
148 bfd_putl32 (x, buffer);
158 /* Default insn parser.
160 The syntax string is scanned and operands are parsed and stored in FIELDS.
161 Relocs are queued as we go via other callbacks.
163 ??? Note that this is currently an all-or-nothing parser. If we fail to
164 parse the instruction, we return 0 and the caller will start over from
165 the beginning. Backtracking will be necessary in parsing subexpressions,
166 but that can be handled there. Not handling backtracking here may get
167 expensive in the case of the m68k. Deal with later.
169 Returns NULL for success, an error message for failure.
173 parse_insn_normal (insn, strp, fields)
174 const CGEN_INSN * insn;
176 CGEN_FIELDS * fields;
178 const CGEN_SYNTAX * syntax = CGEN_INSN_SYNTAX (insn);
179 const char * str = *strp;
182 const unsigned char * syn;
183 #ifdef CGEN_MNEMONIC_OPERANDS
188 /* For now we assume the mnemonic is first (there are no leading operands).
189 We can parse it without needing to set up operand parsing. */
190 p = CGEN_INSN_MNEMONIC (insn);
191 while (* p && * p == * str)
194 if (* p || (* str && !isspace (* str)))
195 return _("unrecognized instruction");
198 cgen_init_parse_operand ();
199 #ifdef CGEN_MNEMONIC_OPERANDS
203 /* We don't check for (*str != '\0') here because we want to parse
204 any trailing fake arguments in the syntax string. */
205 syn = CGEN_SYNTAX_STRING (CGEN_INSN_SYNTAX (insn));
207 /* Mnemonics come first for now, ensure valid string. */
208 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
215 /* Non operand chars must match exactly. */
216 if (CGEN_SYNTAX_CHAR_P (* syn))
218 if (*str == CGEN_SYNTAX_CHAR (* syn))
220 #ifdef CGEN_MNEMONIC_OPERANDS
229 /* Syntax char didn't match. Can't be this insn. */
230 /* FIXME: would like to return something like
231 "expected char `c'" */
232 return _("syntax error");
237 /* We have an operand of some sort. */
238 errmsg = @arch@_cgen_parse_operand (CGEN_SYNTAX_FIELD (*syn),
243 /* Done with this operand, continue with next one. */
247 /* If we're at the end of the syntax string, we're done. */
250 /* FIXME: For the moment we assume a valid `str' can only contain
251 blanks now. IE: We needn't try again with a longer version of
252 the insn and it is assumed that longer versions of insns appear
253 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
254 while (isspace (* str))
258 return _("junk at end of line"); /* FIXME: would like to include `str' */
263 /* We couldn't parse it. */
264 return "unrecognized instruction";
267 /* Default insn builder (insert handler).
268 The instruction is recorded in target byte order.
269 The result is an error message or NULL if success. */
270 /* FIXME: change buffer to char *? */
273 insert_insn_normal (insn, fields, buffer, pc)
274 const CGEN_INSN * insn;
275 CGEN_FIELDS * fields;
276 cgen_insn_t * buffer;
279 const CGEN_SYNTAX * syntax = CGEN_INSN_SYNTAX (insn);
281 const unsigned char * syn;
284 value = CGEN_INSN_VALUE (insn);
286 /* If we're recording insns as numbers (rather than a string of bytes),
287 target byte order handling is deferred until later. */
289 #define min(a,b) ((a) < (b) ? (a) : (b))
290 #if 0 /*def CGEN_INT_INSN*/
293 switch (min (CGEN_BASE_INSN_BITSIZE, CGEN_FIELDS_BITSIZE (fields)))
299 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
300 bfd_putb16 (value, (char *) buffer);
302 bfd_putl16 (value, (char *) buffer);
305 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
306 bfd_putb32 (value, (char *) buffer);
308 bfd_putl32 (value, (char *) buffer);
315 /* ??? Rather than scanning the syntax string again, we could store
316 in `fields' a null terminated list of the fields that are present. */
318 for (syn = CGEN_SYNTAX_STRING (syntax); * syn != '\0'; ++ syn)
322 if (CGEN_SYNTAX_CHAR_P (* syn))
325 errmsg = @arch@_cgen_insert_operand (CGEN_SYNTAX_FIELD (*syn), fields,
326 (char *) buffer, pc);
335 This routine is called for each instruction to be assembled.
336 STR points to the insn to be assembled.
337 We assume all necessary tables have been initialized.
338 The assembled instruction, less any fixups, is stored in buf.
339 [??? What byte order?]
340 The result is a pointer to the insn's entry in the opcode table,
341 or NULL if an error occured (an error message will have already been
344 Note that when processing (non-alias) macro-insns,
345 this function recurses. */
348 @arch@_cgen_assemble_insn (str, fields, buf, errmsg)
350 CGEN_FIELDS * fields;
355 CGEN_INSN_LIST * ilist;
357 /* Skip leading white space. */
358 while (isspace (* str))
361 /* The instructions are stored in hashed lists.
362 Get the first in the list. */
363 ilist = CGEN_ASM_LOOKUP_INSN (str);
365 /* Keep looking until we find a match. */
368 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
370 const CGEN_INSN *insn = ilist->insn;
372 #if 0 /* not needed as unsupported opcodes shouldn't be in the hash lists */
373 /* Is this insn supported by the selected cpu? */
374 if (! @arch@_cgen_insn_supported (insn))
378 /* If the RELAX attribute is set, this is an insn that shouldn't be
379 chosen immediately. Instead, it is used during assembler/linker
380 relaxation if possible. */
381 if (CGEN_INSN_ATTR (insn, CGEN_INSN_RELAX) != 0)
386 /* Record a default length for the insn. This will get set to the
387 correct value while parsing. */
389 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
391 if (! CGEN_PARSE_FN (insn) (insn, & str, fields))
393 /* ??? 0 is passed for `pc' */
394 if (CGEN_INSERT_FN (insn) (insn, fields, buf, (bfd_vma) 0) != NULL)
396 /* It is up to the caller to actually output the insn and any
401 /* Try the next entry. */
404 /* FIXME: We can return a better error message than this.
405 Need to track why it failed and pick the right one. */
407 static char errbuf[100];
408 if (strlen (start) > 50)
409 /* xgettext:c-format */
410 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
412 /* xgettext:c-format */
413 sprintf (errbuf, _("bad instruction `%.50s'"), start);
420 #if 0 /* This calls back to GAS which we can't do without care. */
422 /* Record each member of OPVALS in the assembler's symbol table.
423 This lets GAS parse registers for us.
424 ??? Interesting idea but not currently used. */
426 /* Record each member of OPVALS in the assembler's symbol table.
427 FIXME: Not currently used. */
430 @arch@_cgen_asm_hash_keywords (opvals)
431 CGEN_KEYWORD * opvals;
433 CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL);
434 const CGEN_KEYWORD_ENTRY * ke;
436 while ((ke = cgen_keyword_search_next (& search)) != NULL)
438 #if 0 /* Unnecessary, should be done in the search routine. */
439 if (! @arch@_cgen_opval_supported (ke))
442 cgen_asm_record_register (ke->name, ke->value);