* Fix for PR 16567, brought over from d30v branch.
[deliverable/binutils-gdb.git] / opcodes / cgen-asm.in
CommitLineData
bfc10abe
DE
1/* Assembler interface for targets using CGEN. -*- C -*-
2 CGEN: Cpu tools GENerator
3
fbc8134d 4THIS FILE IS USED TO GENERATE @prefix@-asm.c.
bfc10abe 5
833d2990 6Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
bfc10abe
DE
7
8This file is part of the GNU Binutils and GDB, the GNU debugger.
9
10This program is free software; you can redistribute it and/or modify
11it under the terms of the GNU General Public License as published by
12the Free Software Foundation; either version 2, or (at your option)
13any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
fbc8134d
DE
21along with this program; if not, write to the Free Software Foundation, Inc.,
2259 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
bfc10abe
DE
23
24#include "sysdep.h"
25#include <ctype.h>
26#include <stdio.h>
27#include "ansidecl.h"
28#include "bfd.h"
833d2990 29#include "symcat.h"
fbc8134d 30#include "@prefix@-opc.h"
240f5c9f 31#include "opintl.h"
bfc10abe
DE
32
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.
38*/
39
041d7e18
DE
40static const char * insert_normal
41 PARAMS ((long, unsigned int, int, int, int, char *));
833d2990 42static const char * parse_insn_normal
bfc10abe 43 PARAMS ((const CGEN_INSN *, const char **, CGEN_FIELDS *));
833d2990 44static const char * insert_insn_normal
fbc8134d 45 PARAMS ((const CGEN_INSN *, CGEN_FIELDS *, cgen_insn_t *, bfd_vma));
bfc10abe 46\f
041d7e18
DE
47/* -- assembler routines inserted here */
48\f
bfc10abe
DE
49/* Default insertion routine.
50
833d2990
DE
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).
bfc10abe 54
833d2990 55 The result is an error message or NULL if success. */
bfc10abe 56
833d2990 57/* ??? This duplicates functionality with bfd's howto table and
bfc10abe 58 bfd_install_relocation. */
833d2990
DE
59/* ??? For architectures where insns can be representable as ints,
60 store insn in `field' struct and add registers, etc. while parsing? */
bfc10abe 61
833d2990 62static const char *
041d7e18 63insert_normal (value, attrs, start, length, total_length, buffer)
bfc10abe
DE
64 long value;
65 unsigned int attrs;
833d2990
DE
66 int start;
67 int length;
833d2990
DE
68 int total_length;
69 char * buffer;
bfc10abe
DE
70{
71 bfd_vma x;
833d2990 72 static char buf[100];
fbc8134d
DE
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;
76
77 /* If LENGTH is zero, this operand doesn't contribute to the value. */
78 if (length == 0)
79 return NULL;
833d2990 80
833d2990 81 /* Ensure VALUE will fit. */
fbc8134d 82 if ((attrs & CGEN_ATTR_MASK (CGEN_OPERAND_UNSIGNED)) != 0)
833d2990 83 {
fbc8134d 84 unsigned long max = mask;
833d2990
DE
85 if ((unsigned long) value > max)
86 {
fbc8134d 87 /* xgettext:c-format */
240f5c9f
NC
88 sprintf (buf, _("operand out of range (%lu not between 0 and %lu)"),
89 value, max);
833d2990
DE
90 return buf;
91 }
92 }
93 else
94 {
fbc8134d
DE
95 long min = - (1L << (length - 1));
96 long max = (1L << (length - 1)) - 1;
833d2990 97 if (value < min || value > max)
fbc8134d
DE
98 {
99 sprintf
100 /* xgettext:c-format */
101 (buf, _("operand out of range (%ld not between %ld and %ld)"),
102 value, min, max);
103 return buf;
104 }
833d2990 105 }
bfc10abe
DE
106
107#if 0 /*def CGEN_INT_INSN*/
fbc8134d 108 *buffer |= (value & mask) << (total_length - (start + length));
bfc10abe
DE
109#else
110 switch (total_length)
111 {
112 case 8:
833d2990 113 x = * (unsigned char *) buffer;
bfc10abe
DE
114 break;
115 case 16:
116 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
117 x = bfd_getb16 (buffer);
118 else
119 x = bfd_getl16 (buffer);
120 break;
121 case 32:
122 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
123 x = bfd_getb32 (buffer);
124 else
125 x = bfd_getl32 (buffer);
126 break;
127 default :
128 abort ();
129 }
130
fbc8134d 131 x |= (value & mask) << (total_length - (start + length));
bfc10abe
DE
132
133 switch (total_length)
134 {
135 case 8:
833d2990 136 * buffer = value;
bfc10abe
DE
137 break;
138 case 16:
139 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
140 bfd_putb16 (x, buffer);
141 else
142 bfd_putl16 (x, buffer);
143 break;
144 case 32:
145 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
146 bfd_putb32 (x, buffer);
147 else
148 bfd_putl32 (x, buffer);
149 break;
150 default :
151 abort ();
152 }
153#endif
833d2990
DE
154
155 return NULL;
bfc10abe
DE
156}
157\f
bfc10abe
DE
158/* Default insn parser.
159
160 The syntax string is scanned and operands are parsed and stored in FIELDS.
161 Relocs are queued as we go via other callbacks.
162
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.
168
169 Returns NULL for success, an error message for failure.
170*/
171
172static const char *
173parse_insn_normal (insn, strp, fields)
833d2990
DE
174 const CGEN_INSN * insn;
175 const char ** strp;
176 CGEN_FIELDS * fields;
bfc10abe 177{
833d2990
DE
178 const CGEN_SYNTAX * syntax = CGEN_INSN_SYNTAX (insn);
179 const char * str = *strp;
180 const char * errmsg;
181 const char * p;
182 const unsigned char * syn;
bfc10abe 183#ifdef CGEN_MNEMONIC_OPERANDS
fbc8134d 184 /* FIXME: wip */
bfc10abe
DE
185 int past_opcode_p;
186#endif
187
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);
833d2990
DE
191 while (* p && * p == * str)
192 ++ p, ++ str;
240f5c9f 193
833d2990 194 if (* p || (* str && !isspace (* str)))
240f5c9f 195 return _("unrecognized instruction");
bfc10abe
DE
196
197 CGEN_INIT_PARSE ();
198 cgen_init_parse_operand ();
199#ifdef CGEN_MNEMONIC_OPERANDS
200 past_opcode_p = 0;
201#endif
202
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));
833d2990 206
bfc10abe 207 /* Mnemonics come first for now, ensure valid string. */
833d2990 208 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
bfc10abe 209 abort ();
833d2990 210
bfc10abe 211 ++syn;
833d2990
DE
212
213 while (* syn != 0)
bfc10abe
DE
214 {
215 /* Non operand chars must match exactly. */
833d2990 216 if (CGEN_SYNTAX_CHAR_P (* syn))
bfc10abe 217 {
833d2990 218 if (*str == CGEN_SYNTAX_CHAR (* syn))
bfc10abe
DE
219 {
220#ifdef CGEN_MNEMONIC_OPERANDS
833d2990 221 if (* syn == ' ')
bfc10abe
DE
222 past_opcode_p = 1;
223#endif
833d2990
DE
224 ++ syn;
225 ++ str;
bfc10abe
DE
226 }
227 else
228 {
229 /* Syntax char didn't match. Can't be this insn. */
833d2990
DE
230 /* FIXME: would like to return something like
231 "expected char `c'" */
240f5c9f 232 return _("syntax error");
bfc10abe
DE
233 }
234 continue;
235 }
236
237 /* We have an operand of some sort. */
238 errmsg = @arch@_cgen_parse_operand (CGEN_SYNTAX_FIELD (*syn),
239 &str, fields);
240 if (errmsg)
241 return errmsg;
242
243 /* Done with this operand, continue with next one. */
833d2990 244 ++ syn;
bfc10abe
DE
245 }
246
247 /* If we're at the end of the syntax string, we're done. */
833d2990 248 if (* syn == '\0')
bfc10abe
DE
249 {
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). */
833d2990
DE
254 while (isspace (* str))
255 ++ str;
bfc10abe 256
833d2990 257 if (* str != '\0')
240f5c9f 258 return _("junk at end of line"); /* FIXME: would like to include `str' */
bfc10abe
DE
259
260 return NULL;
261 }
262
263 /* We couldn't parse it. */
264 return "unrecognized instruction";
265}
266
267/* Default insn builder (insert handler).
833d2990
DE
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 *? */
bfc10abe 271
833d2990 272static const char *
fbc8134d 273insert_insn_normal (insn, fields, buffer, pc)
833d2990
DE
274 const CGEN_INSN * insn;
275 CGEN_FIELDS * fields;
276 cgen_insn_t * buffer;
fbc8134d 277 bfd_vma pc;
bfc10abe 278{
833d2990 279 const CGEN_SYNTAX * syntax = CGEN_INSN_SYNTAX (insn);
bfc10abe 280 bfd_vma value;
833d2990 281 const unsigned char * syn;
bfc10abe
DE
282
283 CGEN_INIT_INSERT ();
284 value = CGEN_INSN_VALUE (insn);
285
286 /* If we're recording insns as numbers (rather than a string of bytes),
287 target byte order handling is deferred until later. */
288#undef min
289#define min(a,b) ((a) < (b) ? (a) : (b))
290#if 0 /*def CGEN_INT_INSN*/
291 *buffer = value;
292#else
293 switch (min (CGEN_BASE_INSN_BITSIZE, CGEN_FIELDS_BITSIZE (fields)))
294 {
295 case 8:
833d2990 296 * buffer = value;
bfc10abe
DE
297 break;
298 case 16:
299 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
300 bfd_putb16 (value, (char *) buffer);
301 else
302 bfd_putl16 (value, (char *) buffer);
303 break;
304 case 32:
305 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
306 bfd_putb32 (value, (char *) buffer);
307 else
308 bfd_putl32 (value, (char *) buffer);
309 break;
310 default:
311 abort ();
312 }
313#endif
314
315 /* ??? Rather than scanning the syntax string again, we could store
316 in `fields' a null terminated list of the fields that are present. */
317
833d2990 318 for (syn = CGEN_SYNTAX_STRING (syntax); * syn != '\0'; ++ syn)
bfc10abe 319 {
833d2990
DE
320 const char *errmsg;
321
322 if (CGEN_SYNTAX_CHAR_P (* syn))
bfc10abe
DE
323 continue;
324
833d2990 325 errmsg = @arch@_cgen_insert_operand (CGEN_SYNTAX_FIELD (*syn), fields,
fbc8134d 326 (char *) buffer, pc);
833d2990
DE
327 if (errmsg)
328 return errmsg;
bfc10abe 329 }
833d2990
DE
330
331 return NULL;
bfc10abe
DE
332}
333\f
334/* Main entry point.
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.
041d7e18
DE
338 The assembled instruction, less any fixups, is stored in buf.
339 [??? What byte order?]
bfc10abe
DE
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
041d7e18
DE
342 printed).
343
344 Note that when processing (non-alias) macro-insns,
345 this function recurses. */
bfc10abe
DE
346
347const CGEN_INSN *
348@arch@_cgen_assemble_insn (str, fields, buf, errmsg)
833d2990
DE
349 const char * str;
350 CGEN_FIELDS * fields;
351 cgen_insn_t * buf;
352 char ** errmsg;
bfc10abe 353{
833d2990
DE
354 const char * start;
355 CGEN_INSN_LIST * ilist;
bfc10abe
DE
356
357 /* Skip leading white space. */
833d2990
DE
358 while (isspace (* str))
359 ++ str;
bfc10abe
DE
360
361 /* The instructions are stored in hashed lists.
362 Get the first in the list. */
363 ilist = CGEN_ASM_LOOKUP_INSN (str);
364
365 /* Keep looking until we find a match. */
366
367 start = str;
368 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
369 {
370 const CGEN_INSN *insn = ilist->insn;
371
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))
375 continue;
376#endif
377
bfc10abe
DE
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)
382 continue;
bfc10abe
DE
383
384 str = start;
385
386 /* Record a default length for the insn. This will get set to the
387 correct value while parsing. */
388 /* FIXME: wip */
389 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
390
833d2990 391 if (! CGEN_PARSE_FN (insn) (insn, & str, fields))
bfc10abe 392 {
fbc8134d
DE
393 /* ??? 0 is passed for `pc' */
394 if (CGEN_INSERT_FN (insn) (insn, fields, buf, (bfd_vma) 0) != NULL)
833d2990 395 continue;
bfc10abe
DE
396 /* It is up to the caller to actually output the insn and any
397 queued relocs. */
398 return insn;
399 }
400
401 /* Try the next entry. */
402 }
403
404 /* FIXME: We can return a better error message than this.
405 Need to track why it failed and pick the right one. */
406 {
407 static char errbuf[100];
240f5c9f 408 if (strlen (start) > 50)
fbc8134d 409 /* xgettext:c-format */
240f5c9f 410 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
fbc8134d
DE
411 else
412 /* xgettext:c-format */
240f5c9f
NC
413 sprintf (errbuf, _("bad instruction `%.50s'"), start);
414
bfc10abe
DE
415 *errmsg = errbuf;
416 return NULL;
417 }
418}
419\f
420#if 0 /* This calls back to GAS which we can't do without care. */
421
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. */
425
426/* Record each member of OPVALS in the assembler's symbol table.
427 FIXME: Not currently used. */
428
429void
430@arch@_cgen_asm_hash_keywords (opvals)
833d2990 431 CGEN_KEYWORD * opvals;
bfc10abe
DE
432{
433 CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL);
833d2990 434 const CGEN_KEYWORD_ENTRY * ke;
bfc10abe 435
833d2990 436 while ((ke = cgen_keyword_search_next (& search)) != NULL)
bfc10abe
DE
437 {
438#if 0 /* Unnecessary, should be done in the search routine. */
439 if (! @arch@_cgen_opval_supported (ke))
440 continue;
441#endif
442 cgen_asm_record_register (ke->name, ke->value);
443 }
444}
445
446#endif /* 0 */
This page took 0.079155 seconds and 4 git commands to generate.