* cpu.c,model.c,sem-switch.c,sem.c: Regenerated. Mostly comment
[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
4This file is used to generate @arch@-asm.c.
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
21along with this program; if not, write to the Free Software
22Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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"
bfc10abe
DE
30#include "@arch@-opc.h"
31
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.
37*/
38
041d7e18
DE
39static const char * insert_normal
40 PARAMS ((long, unsigned int, int, int, int, char *));
833d2990 41static const char * parse_insn_normal
bfc10abe 42 PARAMS ((const CGEN_INSN *, const char **, CGEN_FIELDS *));
833d2990 43static const char * insert_insn_normal
bfc10abe
DE
44 PARAMS ((const CGEN_INSN *, CGEN_FIELDS *, cgen_insn_t *));
45\f
041d7e18
DE
46/* -- assembler routines inserted here */
47\f
bfc10abe
DE
48/* Default insertion routine.
49
833d2990
DE
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).
bfc10abe 53
833d2990 54 The result is an error message or NULL if success. */
bfc10abe 55
833d2990 56/* ??? This duplicates functionality with bfd's howto table and
bfc10abe 57 bfd_install_relocation. */
833d2990
DE
58/* ??? For architectures where insns can be representable as ints,
59 store insn in `field' struct and add registers, etc. while parsing? */
bfc10abe 60
833d2990 61static const char *
041d7e18 62insert_normal (value, attrs, start, length, total_length, buffer)
bfc10abe
DE
63 long value;
64 unsigned int attrs;
833d2990
DE
65 int start;
66 int length;
833d2990
DE
67 int total_length;
68 char * buffer;
bfc10abe
DE
69{
70 bfd_vma x;
833d2990
DE
71 static char buf[100];
72
833d2990
DE
73 /* Ensure VALUE will fit. */
74 if ((attrs & (1 << CGEN_OPERAND_UNSIGNED)) != 0)
75 {
76 unsigned long max = (1 << length) - 1;
77 if ((unsigned long) value > max)
78 {
79 const char *err = "operand out of range (%lu not between 0 and %lu)";
80
81 sprintf (buf, err, value, max);
82 return buf;
83 }
84 }
85 else
86 {
87 long min = - (1 << (length - 1));
88 long max = (1 << (length - 1)) - 1;
89 if (value < min || value > max)
90 {
91 const char *err = "operand out of range (%ld not between %ld and %ld)";
92
93 sprintf (buf, err, value, min, max);
94 return buf;
95 }
96 }
bfc10abe
DE
97
98#if 0 /*def CGEN_INT_INSN*/
99 *buffer |= ((value & ((1 << length) - 1))
100 << (total_length - (start + length)));
101#else
102 switch (total_length)
103 {
104 case 8:
833d2990 105 x = * (unsigned char *) buffer;
bfc10abe
DE
106 break;
107 case 16:
108 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
109 x = bfd_getb16 (buffer);
110 else
111 x = bfd_getl16 (buffer);
112 break;
113 case 32:
114 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
115 x = bfd_getb32 (buffer);
116 else
117 x = bfd_getl32 (buffer);
118 break;
119 default :
120 abort ();
121 }
122
bfc10abe
DE
123 x |= ((value & ((1 << length) - 1))
124 << (total_length - (start + length)));
125
126 switch (total_length)
127 {
128 case 8:
833d2990 129 * buffer = value;
bfc10abe
DE
130 break;
131 case 16:
132 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
133 bfd_putb16 (x, buffer);
134 else
135 bfd_putl16 (x, buffer);
136 break;
137 case 32:
138 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
139 bfd_putb32 (x, buffer);
140 else
141 bfd_putl32 (x, buffer);
142 break;
143 default :
144 abort ();
145 }
146#endif
833d2990
DE
147
148 return NULL;
bfc10abe
DE
149}
150\f
bfc10abe
DE
151/* Default insn parser.
152
153 The syntax string is scanned and operands are parsed and stored in FIELDS.
154 Relocs are queued as we go via other callbacks.
155
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.
161
162 Returns NULL for success, an error message for failure.
163*/
164
165static const char *
166parse_insn_normal (insn, strp, fields)
833d2990
DE
167 const CGEN_INSN * insn;
168 const char ** strp;
169 CGEN_FIELDS * fields;
bfc10abe 170{
833d2990
DE
171 const CGEN_SYNTAX * syntax = CGEN_INSN_SYNTAX (insn);
172 const char * str = *strp;
173 const char * errmsg;
174 const char * p;
175 const unsigned char * syn;
bfc10abe
DE
176#ifdef CGEN_MNEMONIC_OPERANDS
177 int past_opcode_p;
178#endif
179
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);
833d2990
DE
183 while (* p && * p == * str)
184 ++ p, ++ str;
185 if (* p || (* str && !isspace (* str)))
bfc10abe
DE
186 return "unrecognized instruction";
187
188 CGEN_INIT_PARSE ();
189 cgen_init_parse_operand ();
190#ifdef CGEN_MNEMONIC_OPERANDS
191 past_opcode_p = 0;
192#endif
193
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));
833d2990 197
bfc10abe 198 /* Mnemonics come first for now, ensure valid string. */
833d2990 199 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
bfc10abe 200 abort ();
833d2990 201
bfc10abe 202 ++syn;
833d2990
DE
203
204 while (* syn != 0)
bfc10abe
DE
205 {
206 /* Non operand chars must match exactly. */
207 /* FIXME: Need to better handle whitespace. */
833d2990 208 if (CGEN_SYNTAX_CHAR_P (* syn))
bfc10abe 209 {
833d2990 210 if (*str == CGEN_SYNTAX_CHAR (* syn))
bfc10abe
DE
211 {
212#ifdef CGEN_MNEMONIC_OPERANDS
833d2990 213 if (* syn == ' ')
bfc10abe
DE
214 past_opcode_p = 1;
215#endif
833d2990
DE
216 ++ syn;
217 ++ str;
bfc10abe
DE
218 }
219 else
220 {
221 /* Syntax char didn't match. Can't be this insn. */
833d2990
DE
222 /* FIXME: would like to return something like
223 "expected char `c'" */
bfc10abe
DE
224 return "syntax error";
225 }
226 continue;
227 }
228
229 /* We have an operand of some sort. */
230 errmsg = @arch@_cgen_parse_operand (CGEN_SYNTAX_FIELD (*syn),
231 &str, fields);
232 if (errmsg)
233 return errmsg;
234
235 /* Done with this operand, continue with next one. */
833d2990 236 ++ syn;
bfc10abe
DE
237 }
238
239 /* If we're at the end of the syntax string, we're done. */
833d2990 240 if (* syn == '\0')
bfc10abe
DE
241 {
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). */
833d2990
DE
246 while (isspace (* str))
247 ++ str;
bfc10abe 248
833d2990 249 if (* str != '\0')
bfc10abe
DE
250 return "junk at end of line"; /* FIXME: would like to include `str' */
251
252 return NULL;
253 }
254
255 /* We couldn't parse it. */
256 return "unrecognized instruction";
257}
258
259/* Default insn builder (insert handler).
833d2990
DE
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 *? */
bfc10abe 263
833d2990 264static const char *
bfc10abe 265insert_insn_normal (insn, fields, buffer)
833d2990
DE
266 const CGEN_INSN * insn;
267 CGEN_FIELDS * fields;
268 cgen_insn_t * buffer;
bfc10abe 269{
833d2990 270 const CGEN_SYNTAX * syntax = CGEN_INSN_SYNTAX (insn);
bfc10abe 271 bfd_vma value;
833d2990 272 const unsigned char * syn;
bfc10abe
DE
273
274 CGEN_INIT_INSERT ();
275 value = CGEN_INSN_VALUE (insn);
276
277 /* If we're recording insns as numbers (rather than a string of bytes),
278 target byte order handling is deferred until later. */
279#undef min
280#define min(a,b) ((a) < (b) ? (a) : (b))
281#if 0 /*def CGEN_INT_INSN*/
282 *buffer = value;
283#else
284 switch (min (CGEN_BASE_INSN_BITSIZE, CGEN_FIELDS_BITSIZE (fields)))
285 {
286 case 8:
833d2990 287 * buffer = value;
bfc10abe
DE
288 break;
289 case 16:
290 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
291 bfd_putb16 (value, (char *) buffer);
292 else
293 bfd_putl16 (value, (char *) buffer);
294 break;
295 case 32:
296 if (CGEN_CURRENT_ENDIAN == CGEN_ENDIAN_BIG)
297 bfd_putb32 (value, (char *) buffer);
298 else
299 bfd_putl32 (value, (char *) buffer);
300 break;
301 default:
302 abort ();
303 }
304#endif
305
306 /* ??? Rather than scanning the syntax string again, we could store
307 in `fields' a null terminated list of the fields that are present. */
308
833d2990 309 for (syn = CGEN_SYNTAX_STRING (syntax); * syn != '\0'; ++ syn)
bfc10abe 310 {
833d2990
DE
311 const char *errmsg;
312
313 if (CGEN_SYNTAX_CHAR_P (* syn))
bfc10abe
DE
314 continue;
315
833d2990
DE
316 errmsg = @arch@_cgen_insert_operand (CGEN_SYNTAX_FIELD (*syn), fields,
317 (char *) buffer);
318 if (errmsg)
319 return errmsg;
bfc10abe 320 }
833d2990
DE
321
322 return NULL;
bfc10abe
DE
323}
324\f
325/* Main entry point.
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.
041d7e18
DE
329 The assembled instruction, less any fixups, is stored in buf.
330 [??? What byte order?]
bfc10abe
DE
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
041d7e18
DE
333 printed).
334
335 Note that when processing (non-alias) macro-insns,
336 this function recurses. */
bfc10abe
DE
337
338const CGEN_INSN *
339@arch@_cgen_assemble_insn (str, fields, buf, errmsg)
833d2990
DE
340 const char * str;
341 CGEN_FIELDS * fields;
342 cgen_insn_t * buf;
343 char ** errmsg;
bfc10abe 344{
833d2990
DE
345 const char * start;
346 CGEN_INSN_LIST * ilist;
bfc10abe
DE
347
348 /* Skip leading white space. */
833d2990
DE
349 while (isspace (* str))
350 ++ str;
bfc10abe
DE
351
352 /* The instructions are stored in hashed lists.
353 Get the first in the list. */
354 ilist = CGEN_ASM_LOOKUP_INSN (str);
355
356 /* Keep looking until we find a match. */
357
358 start = str;
359 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
360 {
361 const CGEN_INSN *insn = ilist->insn;
362
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))
366 continue;
367#endif
368
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)
374 continue;
375#endif
376
377 str = start;
378
379 /* Record a default length for the insn. This will get set to the
380 correct value while parsing. */
381 /* FIXME: wip */
382 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
383
833d2990 384 if (! CGEN_PARSE_FN (insn) (insn, & str, fields))
bfc10abe 385 {
833d2990
DE
386 if (CGEN_INSERT_FN (insn) (insn, fields, buf) != NULL)
387 continue;
bfc10abe
DE
388 /* It is up to the caller to actually output the insn and any
389 queued relocs. */
390 return insn;
391 }
392
393 /* Try the next entry. */
394 }
395
396 /* FIXME: We can return a better error message than this.
397 Need to track why it failed and pick the right one. */
398 {
399 static char errbuf[100];
400 sprintf (errbuf, "bad instruction `%.50s%s'",
401 start, strlen (start) > 50 ? "..." : "");
402 *errmsg = errbuf;
403 return NULL;
404 }
405}
406\f
407#if 0 /* This calls back to GAS which we can't do without care. */
408
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. */
412
413/* Record each member of OPVALS in the assembler's symbol table.
414 FIXME: Not currently used. */
415
416void
417@arch@_cgen_asm_hash_keywords (opvals)
833d2990 418 CGEN_KEYWORD * opvals;
bfc10abe
DE
419{
420 CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL);
833d2990 421 const CGEN_KEYWORD_ENTRY * ke;
bfc10abe 422
833d2990 423 while ((ke = cgen_keyword_search_next (& search)) != NULL)
bfc10abe
DE
424 {
425#if 0 /* Unnecessary, should be done in the search routine. */
426 if (! @arch@_cgen_opval_supported (ke))
427 continue;
428#endif
429 cgen_asm_record_register (ke->name, ke->value);
430 }
431}
432
433#endif /* 0 */
This page took 0.054256 seconds and 4 git commands to generate.