This commit was generated by cvs2svn to track changes on a CVS vendor
[deliverable/binutils-gdb.git] / opcodes / cgen-asm.in
1 /* Assembler interface for targets using CGEN. -*- C -*-
2 CGEN: Cpu tools GENerator
3
4 THIS FILE IS USED TO GENERATE @prefix@-asm.c.
5
6 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
7
8 This file is part of the GNU Binutils and GDB, the GNU debugger.
9
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)
13 any later version.
14
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.
19
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. */
23
24 #include "sysdep.h"
25 #include <ctype.h>
26 #include <stdio.h>
27 #include "ansidecl.h"
28 #include "bfd.h"
29 #include "symcat.h"
30 #include "@prefix@-opc.h"
31 #include "opintl.h"
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
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));
46 \f
47 /* -- assembler routines inserted here */
48 \f
49 /* Default insertion routine.
50
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).
54
55 The result is an error message or NULL if success. */
56
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? */
61
62 static const char *
63 insert_normal (value, attrs, start, length, total_length, buffer)
64 long value;
65 unsigned int attrs;
66 int start;
67 int length;
68 int total_length;
69 char * buffer;
70 {
71 bfd_vma x;
72 static char buf[100];
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;
80
81 /* Ensure VALUE will fit. */
82 if ((attrs & CGEN_ATTR_MASK (CGEN_OPERAND_UNSIGNED)) != 0)
83 {
84 unsigned long max = mask;
85 if ((unsigned long) value > max)
86 {
87 /* xgettext:c-format */
88 sprintf (buf, _("operand out of range (%lu not between 0 and %lu)"),
89 value, max);
90 return buf;
91 }
92 }
93 else
94 {
95 long min = - (1L << (length - 1));
96 long max = (1L << (length - 1)) - 1;
97 if (value < min || value > max)
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 }
105 }
106
107 #if 0 /*def CGEN_INT_INSN*/
108 *buffer |= (value & mask) << (total_length - (start + length));
109 #else
110 switch (total_length)
111 {
112 case 8:
113 x = * (unsigned char *) buffer;
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
131 x |= (value & mask) << (total_length - (start + length));
132
133 switch (total_length)
134 {
135 case 8:
136 * buffer = value;
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
154
155 return NULL;
156 }
157 \f
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
172 static const char *
173 parse_insn_normal (insn, strp, fields)
174 const CGEN_INSN * insn;
175 const char ** strp;
176 CGEN_FIELDS * fields;
177 {
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;
183 #ifdef CGEN_MNEMONIC_OPERANDS
184 /* FIXME: wip */
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);
191 while (* p && * p == * str)
192 ++ p, ++ str;
193
194 if (* p || (* str && !isspace (* str)))
195 return _("unrecognized instruction");
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));
206
207 /* Mnemonics come first for now, ensure valid string. */
208 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
209 abort ();
210
211 ++syn;
212
213 while (* syn != 0)
214 {
215 /* Non operand chars must match exactly. */
216 if (CGEN_SYNTAX_CHAR_P (* syn))
217 {
218 if (*str == CGEN_SYNTAX_CHAR (* syn))
219 {
220 #ifdef CGEN_MNEMONIC_OPERANDS
221 if (* syn == ' ')
222 past_opcode_p = 1;
223 #endif
224 ++ syn;
225 ++ str;
226 }
227 else
228 {
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");
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. */
244 ++ syn;
245 }
246
247 /* If we're at the end of the syntax string, we're done. */
248 if (* syn == '\0')
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). */
254 while (isspace (* str))
255 ++ str;
256
257 if (* str != '\0')
258 return _("junk at end of line"); /* FIXME: would like to include `str' */
259
260 return NULL;
261 }
262
263 /* We couldn't parse it. */
264 return "unrecognized instruction";
265 }
266
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 *? */
271
272 static const char *
273 insert_insn_normal (insn, fields, buffer, pc)
274 const CGEN_INSN * insn;
275 CGEN_FIELDS * fields;
276 cgen_insn_t * buffer;
277 bfd_vma pc;
278 {
279 const CGEN_SYNTAX * syntax = CGEN_INSN_SYNTAX (insn);
280 bfd_vma value;
281 const unsigned char * syn;
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:
296 * buffer = value;
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
318 for (syn = CGEN_SYNTAX_STRING (syntax); * syn != '\0'; ++ syn)
319 {
320 const char *errmsg;
321
322 if (CGEN_SYNTAX_CHAR_P (* syn))
323 continue;
324
325 errmsg = @arch@_cgen_insert_operand (CGEN_SYNTAX_FIELD (*syn), fields,
326 (char *) buffer, pc);
327 if (errmsg)
328 return errmsg;
329 }
330
331 return NULL;
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.
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
342 printed).
343
344 Note that when processing (non-alias) macro-insns,
345 this function recurses. */
346
347 const CGEN_INSN *
348 @arch@_cgen_assemble_insn (str, fields, buf, errmsg)
349 const char * str;
350 CGEN_FIELDS * fields;
351 cgen_insn_t * buf;
352 char ** errmsg;
353 {
354 const char * start;
355 CGEN_INSN_LIST * ilist;
356
357 /* Skip leading white space. */
358 while (isspace (* str))
359 ++ str;
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
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;
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
391 if (! CGEN_PARSE_FN (insn) (insn, & str, fields))
392 {
393 /* ??? 0 is passed for `pc' */
394 if (CGEN_INSERT_FN (insn) (insn, fields, buf, (bfd_vma) 0) != NULL)
395 continue;
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];
408 if (strlen (start) > 50)
409 /* xgettext:c-format */
410 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
411 else
412 /* xgettext:c-format */
413 sprintf (errbuf, _("bad instruction `%.50s'"), start);
414
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
429 void
430 @arch@_cgen_asm_hash_keywords (opvals)
431 CGEN_KEYWORD * opvals;
432 {
433 CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL);
434 const CGEN_KEYWORD_ENTRY * ke;
435
436 while ((ke = cgen_keyword_search_next (& search)) != NULL)
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.06634 seconds and 5 git commands to generate.