722c0be876277ebc19575b3187da8ae217b51002
[external/binutils.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 @arch@-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
22 Foundation, 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"
29 #include "symcat.h"
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
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 *));
45 \f
46 /* -- assembler routines inserted here */
47 \f
48 /* Default insertion routine.
49
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).
53
54    The result is an error message or NULL if success.  */
55
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?  */
60
61 static const char *
62 insert_normal (value, attrs, start, length, total_length, buffer)
63      long value;
64      unsigned int attrs;
65      int start;
66      int length;
67      int total_length;
68      char * buffer;
69 {
70   bfd_vma x;
71   static char buf[100];
72
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     }
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:
105       x = * (unsigned char *) buffer;
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
123   x |= ((value & ((1 << length) - 1))
124         << (total_length - (start + length)));
125
126   switch (total_length)
127     {
128     case 8:
129       * buffer = value;
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
147
148   return NULL;
149 }
150 \f
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
165 static const char *
166 parse_insn_normal (insn, strp, fields)
167      const CGEN_INSN * insn;
168      const char ** strp;
169      CGEN_FIELDS * fields;
170 {
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;
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);
183   while (* p && * p == * str)
184     ++ p, ++ str;
185   if (* p || (* str && !isspace (* str)))
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));
197
198   /* Mnemonics come first for now, ensure valid string.  */
199   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
200     abort ();
201
202   ++syn;
203
204   while (* syn != 0)
205     {
206       /* Non operand chars must match exactly.  */
207       /* FIXME: Need to better handle whitespace.  */
208       if (CGEN_SYNTAX_CHAR_P (* syn))
209         {
210           if (*str == CGEN_SYNTAX_CHAR (* syn))
211             {
212 #ifdef CGEN_MNEMONIC_OPERANDS
213               if (* syn == ' ')
214                 past_opcode_p = 1;
215 #endif
216               ++ syn;
217               ++ str;
218             }
219           else
220             {
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";
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.  */
236       ++ syn;
237     }
238
239   /* If we're at the end of the syntax string, we're done.  */
240   if (* syn == '\0')
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).  */
246       while (isspace (* str))
247         ++ str;
248
249       if (* str != '\0')
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).
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 *?  */
263
264 static const char *
265 insert_insn_normal (insn, fields, buffer)
266      const CGEN_INSN * insn;
267      CGEN_FIELDS * fields;
268      cgen_insn_t * buffer;
269 {
270   const CGEN_SYNTAX * syntax = CGEN_INSN_SYNTAX (insn);
271   bfd_vma value;
272   const unsigned char * syn;
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:
287       * buffer = value;
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
309   for (syn = CGEN_SYNTAX_STRING (syntax); * syn != '\0'; ++ syn)
310     {
311       const char *errmsg;
312
313       if (CGEN_SYNTAX_CHAR_P (* syn))
314         continue;
315
316       errmsg = @arch@_cgen_insert_operand (CGEN_SYNTAX_FIELD (*syn), fields,
317                                            (char *) buffer);
318       if (errmsg)
319         return errmsg;
320     }
321
322   return NULL;
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.
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
333    printed).
334
335    Note that when processing (non-alias) macro-insns,
336    this function recurses.  */
337
338 const CGEN_INSN *
339 @arch@_cgen_assemble_insn (str, fields, buf, errmsg)
340      const char * str;
341      CGEN_FIELDS * fields;
342      cgen_insn_t * buf;
343      char ** errmsg;
344 {
345   const char * start;
346   CGEN_INSN_LIST * ilist;
347
348   /* Skip leading white space.  */
349   while (isspace (* str))
350     ++ str;
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
384       if (! CGEN_PARSE_FN (insn) (insn, & str, fields))
385         {
386           if (CGEN_INSERT_FN (insn) (insn, fields, buf) != NULL)
387             continue;
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
416 void
417 @arch@_cgen_asm_hash_keywords (opvals)
418      CGEN_KEYWORD * opvals;
419 {
420   CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL);
421   const CGEN_KEYWORD_ENTRY * ke;
422
423   while ((ke = cgen_keyword_search_next (& search)) != NULL)
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 */