Add support for the Infineon XC16X.
[external/binutils.git] / opcodes / xc16x-asm.c
1 /* Assembler interface for targets using CGEN. -*- C -*-
2    CGEN: Cpu tools GENerator
3
4    THIS FILE IS MACHINE GENERATED WITH CGEN.
5    - the resultant file is machine generated, cgen-asm.in isn't
6
7    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2005
8    Free Software Foundation, Inc.
9
10    This file is part of the GNU Binutils and GDB, the GNU debugger.
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 2, or (at your option)
15    any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software Foundation, Inc.,
24    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
25
26 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
27    Keep that in mind.  */
28
29 #include "sysdep.h"
30 #include <stdio.h>
31 #include "ansidecl.h"
32 #include "bfd.h"
33 #include "symcat.h"
34 #include "xc16x-desc.h"
35 #include "xc16x-opc.h"
36 #include "opintl.h"
37 #include "xregex.h"
38 #include "libiberty.h"
39 #include "safe-ctype.h"
40
41 #undef  min
42 #define min(a,b) ((a) < (b) ? (a) : (b))
43 #undef  max
44 #define max(a,b) ((a) > (b) ? (a) : (b))
45
46 static const char * parse_insn_normal
47   (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
48 \f
49 /* -- assembler routines inserted here.  */
50
51 /* -- asm.c */
52 /* Handle '#' prefixes (i.e. skip over them).  */
53
54 static const char *
55 parse_hash (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
56             const char **strp,
57             int opindex ATTRIBUTE_UNUSED,
58             long *valuep ATTRIBUTE_UNUSED)
59 {
60   if (**strp == '#')
61     ++*strp;
62   return NULL;
63 }
64
65 /* Handle '.' prefixes (i.e. skip over them).  */
66
67 static const char *
68 parse_dot (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
69            const char **strp,
70            int opindex ATTRIBUTE_UNUSED,
71            long *valuep ATTRIBUTE_UNUSED)
72 {
73   if (**strp == '.')
74     ++*strp;
75   return NULL;
76 }
77
78 /* Handle '.' prefixes (i.e. skip over them).  */
79
80 static const char *
81 parse_pof (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
82            const char **strp,
83            int opindex ATTRIBUTE_UNUSED,
84            long *valuep ATTRIBUTE_UNUSED)
85 {
86   if (!strncasecmp (*strp, "pof:", 4))
87     *strp += 4;
88   return NULL;
89 }
90
91 /* Handle '.' prefixes (i.e. skip over them).  */
92
93 static const char *
94 parse_pag (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
95            const char **strp,
96            int opindex ATTRIBUTE_UNUSED,
97            long *valuep ATTRIBUTE_UNUSED)
98 {
99   if (!strncasecmp (*strp, "pag:", 4))
100     *strp += 4;
101   return NULL;
102 }
103
104 /* Handle 'sof' prefixes (i.e. skip over them).  */
105 static const char *
106 parse_sof (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
107            const char **strp,
108            int opindex ATTRIBUTE_UNUSED,
109            long *valuep ATTRIBUTE_UNUSED)
110 {
111   if (!strncasecmp (*strp, "sof:", 4))
112     *strp += 4;
113   return NULL;
114 }
115
116 /* Handle 'seg' prefixes (i.e. skip over them).  */
117 static const char *
118 parse_seg (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
119            const char **strp,
120            int opindex ATTRIBUTE_UNUSED,
121            long *valuep ATTRIBUTE_UNUSED)
122 {
123   if (!strncasecmp (*strp, "seg:", 4))
124     *strp += 4;
125   return NULL;
126 }
127 /* -- */
128
129 const char * xc16x_cgen_parse_operand
130   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
131
132 /* Main entry point for operand parsing.
133
134    This function is basically just a big switch statement.  Earlier versions
135    used tables to look up the function to use, but
136    - if the table contains both assembler and disassembler functions then
137      the disassembler contains much of the assembler and vice-versa,
138    - there's a lot of inlining possibilities as things grow,
139    - using a switch statement avoids the function call overhead.
140
141    This function could be moved into `parse_insn_normal', but keeping it
142    separate makes clear the interface between `parse_insn_normal' and each of
143    the handlers.  */
144
145 const char *
146 xc16x_cgen_parse_operand (CGEN_CPU_DESC cd,
147                            int opindex,
148                            const char ** strp,
149                            CGEN_FIELDS * fields)
150 {
151   const char * errmsg = NULL;
152   /* Used by scalar operands that still need to be parsed.  */
153   long junk ATTRIBUTE_UNUSED;
154
155   switch (opindex)
156     {
157     case XC16X_OPERAND_REGNAM :
158       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_psw_names, & fields->f_reg8);
159       break;
160     case XC16X_OPERAND_BIT01 :
161       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_BIT01, (unsigned long *) (& fields->f_op_1bit));
162       break;
163     case XC16X_OPERAND_BIT1 :
164       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_BIT1, (unsigned long *) (& fields->f_op_bit1));
165       break;
166     case XC16X_OPERAND_BIT2 :
167       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_BIT2, (unsigned long *) (& fields->f_op_bit2));
168       break;
169     case XC16X_OPERAND_BIT4 :
170       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_BIT4, (unsigned long *) (& fields->f_op_bit4));
171       break;
172     case XC16X_OPERAND_BIT8 :
173       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_BIT8, (unsigned long *) (& fields->f_op_bit8));
174       break;
175     case XC16X_OPERAND_BITONE :
176       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_BITONE, (unsigned long *) (& fields->f_op_onebit));
177       break;
178     case XC16X_OPERAND_CADDR :
179       {
180         bfd_vma value = 0;
181         errmsg = cgen_parse_address (cd, strp, XC16X_OPERAND_CADDR, 0, NULL,  & value);
182         fields->f_offset16 = value;
183       }
184       break;
185     case XC16X_OPERAND_COND :
186       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_conditioncode_names, & fields->f_condcode);
187       break;
188     case XC16X_OPERAND_DATA8 :
189       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_DATA8, (unsigned long *) (& fields->f_data8));
190       break;
191     case XC16X_OPERAND_DATAHI8 :
192       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_DATAHI8, (unsigned long *) (& fields->f_datahi8));
193       break;
194     case XC16X_OPERAND_DOT :
195       errmsg = parse_dot (cd, strp, XC16X_OPERAND_DOT, (long *) (& junk));
196       break;
197     case XC16X_OPERAND_DR :
198       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_gr_names, & fields->f_r1);
199       break;
200     case XC16X_OPERAND_DRB :
201       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_grb_names, & fields->f_r1);
202       break;
203     case XC16X_OPERAND_DRI :
204       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_gr_names, & fields->f_r4);
205       break;
206     case XC16X_OPERAND_EXTCOND :
207       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_extconditioncode_names, & fields->f_extccode);
208       break;
209     case XC16X_OPERAND_GENREG :
210       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_r8_names, & fields->f_regb8);
211       break;
212     case XC16X_OPERAND_HASH :
213       errmsg = parse_hash (cd, strp, XC16X_OPERAND_HASH, (long *) (& junk));
214       break;
215     case XC16X_OPERAND_ICOND :
216       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_conditioncode_names, & fields->f_icondcode);
217       break;
218     case XC16X_OPERAND_LBIT2 :
219       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_LBIT2, (unsigned long *) (& fields->f_op_lbit2));
220       break;
221     case XC16X_OPERAND_LBIT4 :
222       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_LBIT4, (unsigned long *) (& fields->f_op_lbit4));
223       break;
224     case XC16X_OPERAND_MASK8 :
225       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_MASK8, (unsigned long *) (& fields->f_mask8));
226       break;
227     case XC16X_OPERAND_MASKLO8 :
228       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_MASKLO8, (unsigned long *) (& fields->f_datahi8));
229       break;
230     case XC16X_OPERAND_MEMGR8 :
231       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_memgr8_names, & fields->f_memgr8);
232       break;
233     case XC16X_OPERAND_MEMORY :
234       {
235         bfd_vma value = 0;
236         errmsg = cgen_parse_address (cd, strp, XC16X_OPERAND_MEMORY, 0, NULL,  & value);
237         fields->f_memory = value;
238       }
239       break;
240     case XC16X_OPERAND_PAG :
241       errmsg = parse_pag (cd, strp, XC16X_OPERAND_PAG, (long *) (& junk));
242       break;
243     case XC16X_OPERAND_PAGENUM :
244       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_PAGENUM, (unsigned long *) (& fields->f_pagenum));
245       break;
246     case XC16X_OPERAND_POF :
247       errmsg = parse_pof (cd, strp, XC16X_OPERAND_POF, (long *) (& junk));
248       break;
249     case XC16X_OPERAND_QBIT :
250       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_QBIT, (unsigned long *) (& fields->f_qbit));
251       break;
252     case XC16X_OPERAND_QHIBIT :
253       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_QHIBIT, (unsigned long *) (& fields->f_qhibit));
254       break;
255     case XC16X_OPERAND_QLOBIT :
256       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_QLOBIT, (unsigned long *) (& fields->f_qlobit));
257       break;
258     case XC16X_OPERAND_REG8 :
259       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_r8_names, & fields->f_reg8);
260       break;
261     case XC16X_OPERAND_REGB8 :
262       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_grb8_names, & fields->f_regb8);
263       break;
264     case XC16X_OPERAND_REGBMEM8 :
265       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_regbmem8_names, & fields->f_regmem8);
266       break;
267     case XC16X_OPERAND_REGHI8 :
268       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_r8_names, & fields->f_reghi8);
269       break;
270     case XC16X_OPERAND_REGMEM8 :
271       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_regmem8_names, & fields->f_regmem8);
272       break;
273     case XC16X_OPERAND_REGOFF8 :
274       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_r8_names, & fields->f_regoff8);
275       break;
276     case XC16X_OPERAND_REL :
277       errmsg = cgen_parse_signed_integer (cd, strp, XC16X_OPERAND_REL, (long *) (& fields->f_rel8));
278       break;
279     case XC16X_OPERAND_RELHI :
280       errmsg = cgen_parse_signed_integer (cd, strp, XC16X_OPERAND_RELHI, (long *) (& fields->f_relhi8));
281       break;
282     case XC16X_OPERAND_SEG :
283       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_SEG, (unsigned long *) (& fields->f_seg8));
284       break;
285     case XC16X_OPERAND_SEGHI8 :
286       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_SEGHI8, (unsigned long *) (& fields->f_segnum8));
287       break;
288     case XC16X_OPERAND_SEGM :
289       errmsg = parse_seg (cd, strp, XC16X_OPERAND_SEGM, (long *) (& junk));
290       break;
291     case XC16X_OPERAND_SOF :
292       errmsg = parse_sof (cd, strp, XC16X_OPERAND_SOF, (long *) (& junk));
293       break;
294     case XC16X_OPERAND_SR :
295       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_gr_names, & fields->f_r2);
296       break;
297     case XC16X_OPERAND_SR2 :
298       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_gr_names, & fields->f_r0);
299       break;
300     case XC16X_OPERAND_SRB :
301       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_grb_names, & fields->f_r2);
302       break;
303     case XC16X_OPERAND_SRC1 :
304       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_gr_names, & fields->f_r1);
305       break;
306     case XC16X_OPERAND_SRC2 :
307       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_gr_names, & fields->f_r2);
308       break;
309     case XC16X_OPERAND_SRDIV :
310       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_regdiv8_names, & fields->f_reg8);
311       break;
312     case XC16X_OPERAND_U4 :
313       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_reg0_name, & fields->f_uimm4);
314       break;
315     case XC16X_OPERAND_UIMM16 :
316       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_UIMM16, (unsigned long *) (& fields->f_uimm16));
317       break;
318     case XC16X_OPERAND_UIMM2 :
319       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_ext_names, & fields->f_uimm2);
320       break;
321     case XC16X_OPERAND_UIMM3 :
322       errmsg = cgen_parse_keyword (cd, strp, & xc16x_cgen_opval_reg0_name1, & fields->f_uimm3);
323       break;
324     case XC16X_OPERAND_UIMM4 :
325       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_UIMM4, (unsigned long *) (& fields->f_uimm4));
326       break;
327     case XC16X_OPERAND_UIMM7 :
328       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_UIMM7, (unsigned long *) (& fields->f_uimm7));
329       break;
330     case XC16X_OPERAND_UIMM8 :
331       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_UIMM8, (unsigned long *) (& fields->f_uimm8));
332       break;
333     case XC16X_OPERAND_UPAG16 :
334       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_UPAG16, (unsigned long *) (& fields->f_uimm16));
335       break;
336     case XC16X_OPERAND_UPOF16 :
337       {
338         bfd_vma value = 0;
339         errmsg = cgen_parse_address (cd, strp, XC16X_OPERAND_UPOF16, 0, NULL,  & value);
340         fields->f_memory = value;
341       }
342       break;
343     case XC16X_OPERAND_USEG16 :
344       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_USEG16, (unsigned long *) (& fields->f_offset16));
345       break;
346     case XC16X_OPERAND_USEG8 :
347       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_USEG8, (unsigned long *) (& fields->f_seg8));
348       break;
349     case XC16X_OPERAND_USOF16 :
350       errmsg = cgen_parse_unsigned_integer (cd, strp, XC16X_OPERAND_USOF16, (unsigned long *) (& fields->f_offset16));
351       break;
352
353     default :
354       /* xgettext:c-format */
355       fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
356       abort ();
357   }
358
359   return errmsg;
360 }
361
362 cgen_parse_fn * const xc16x_cgen_parse_handlers[] = 
363 {
364   parse_insn_normal,
365 };
366
367 void
368 xc16x_cgen_init_asm (CGEN_CPU_DESC cd)
369 {
370   xc16x_cgen_init_opcode_table (cd);
371   xc16x_cgen_init_ibld_table (cd);
372   cd->parse_handlers = & xc16x_cgen_parse_handlers[0];
373   cd->parse_operand = xc16x_cgen_parse_operand;
374 }
375
376 \f
377
378 /* Regex construction routine.
379
380    This translates an opcode syntax string into a regex string,
381    by replacing any non-character syntax element (such as an
382    opcode) with the pattern '.*'
383
384    It then compiles the regex and stores it in the opcode, for
385    later use by xc16x_cgen_assemble_insn
386
387    Returns NULL for success, an error message for failure.  */
388
389 char * 
390 xc16x_cgen_build_insn_regex (CGEN_INSN *insn)
391 {  
392   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
393   const char *mnem = CGEN_INSN_MNEMONIC (insn);
394   char rxbuf[CGEN_MAX_RX_ELEMENTS];
395   char *rx = rxbuf;
396   const CGEN_SYNTAX_CHAR_TYPE *syn;
397   int reg_err;
398
399   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
400
401   /* Mnemonics come first in the syntax string.  */
402   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
403     return _("missing mnemonic in syntax string");
404   ++syn;
405
406   /* Generate a case sensitive regular expression that emulates case
407      insensitive matching in the "C" locale.  We cannot generate a case
408      insensitive regular expression because in Turkish locales, 'i' and 'I'
409      are not equal modulo case conversion.  */
410
411   /* Copy the literal mnemonic out of the insn.  */
412   for (; *mnem; mnem++)
413     {
414       char c = *mnem;
415
416       if (ISALPHA (c))
417         {
418           *rx++ = '[';
419           *rx++ = TOLOWER (c);
420           *rx++ = TOUPPER (c);
421           *rx++ = ']';
422         }
423       else
424         *rx++ = c;
425     }
426
427   /* Copy any remaining literals from the syntax string into the rx.  */
428   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
429     {
430       if (CGEN_SYNTAX_CHAR_P (* syn)) 
431         {
432           char c = CGEN_SYNTAX_CHAR (* syn);
433
434           switch (c) 
435             {
436               /* Escape any regex metacharacters in the syntax.  */
437             case '.': case '[': case '\\': 
438             case '*': case '^': case '$': 
439
440 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
441             case '?': case '{': case '}': 
442             case '(': case ')': case '*':
443             case '|': case '+': case ']':
444 #endif
445               *rx++ = '\\';
446               *rx++ = c;
447               break;
448
449             default:
450               if (ISALPHA (c))
451                 {
452                   *rx++ = '[';
453                   *rx++ = TOLOWER (c);
454                   *rx++ = TOUPPER (c);
455                   *rx++ = ']';
456                 }
457               else
458                 *rx++ = c;
459               break;
460             }
461         }
462       else
463         {
464           /* Replace non-syntax fields with globs.  */
465           *rx++ = '.';
466           *rx++ = '*';
467         }
468     }
469
470   /* Trailing whitespace ok.  */
471   * rx++ = '['; 
472   * rx++ = ' '; 
473   * rx++ = '\t'; 
474   * rx++ = ']'; 
475   * rx++ = '*'; 
476
477   /* But anchor it after that.  */
478   * rx++ = '$'; 
479   * rx = '\0';
480
481   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
482   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
483
484   if (reg_err == 0) 
485     return NULL;
486   else
487     {
488       static char msg[80];
489
490       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
491       regfree ((regex_t *) CGEN_INSN_RX (insn));
492       free (CGEN_INSN_RX (insn));
493       (CGEN_INSN_RX (insn)) = NULL;
494       return msg;
495     }
496 }
497
498 \f
499 /* Default insn parser.
500
501    The syntax string is scanned and operands are parsed and stored in FIELDS.
502    Relocs are queued as we go via other callbacks.
503
504    ??? Note that this is currently an all-or-nothing parser.  If we fail to
505    parse the instruction, we return 0 and the caller will start over from
506    the beginning.  Backtracking will be necessary in parsing subexpressions,
507    but that can be handled there.  Not handling backtracking here may get
508    expensive in the case of the m68k.  Deal with later.
509
510    Returns NULL for success, an error message for failure.  */
511
512 static const char *
513 parse_insn_normal (CGEN_CPU_DESC cd,
514                    const CGEN_INSN *insn,
515                    const char **strp,
516                    CGEN_FIELDS *fields)
517 {
518   /* ??? Runtime added insns not handled yet.  */
519   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
520   const char *str = *strp;
521   const char *errmsg;
522   const char *p;
523   const CGEN_SYNTAX_CHAR_TYPE * syn;
524 #ifdef CGEN_MNEMONIC_OPERANDS
525   /* FIXME: wip */
526   int past_opcode_p;
527 #endif
528
529   /* For now we assume the mnemonic is first (there are no leading operands).
530      We can parse it without needing to set up operand parsing.
531      GAS's input scrubber will ensure mnemonics are lowercase, but we may
532      not be called from GAS.  */
533   p = CGEN_INSN_MNEMONIC (insn);
534   while (*p && TOLOWER (*p) == TOLOWER (*str))
535     ++p, ++str;
536
537   if (* p)
538     return _("unrecognized instruction");
539
540 #ifndef CGEN_MNEMONIC_OPERANDS
541   if (* str && ! ISSPACE (* str))
542     return _("unrecognized instruction");
543 #endif
544
545   CGEN_INIT_PARSE (cd);
546   cgen_init_parse_operand (cd);
547 #ifdef CGEN_MNEMONIC_OPERANDS
548   past_opcode_p = 0;
549 #endif
550
551   /* We don't check for (*str != '\0') here because we want to parse
552      any trailing fake arguments in the syntax string.  */
553   syn = CGEN_SYNTAX_STRING (syntax);
554
555   /* Mnemonics come first for now, ensure valid string.  */
556   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
557     abort ();
558
559   ++syn;
560
561   while (* syn != 0)
562     {
563       /* Non operand chars must match exactly.  */
564       if (CGEN_SYNTAX_CHAR_P (* syn))
565         {
566           /* FIXME: While we allow for non-GAS callers above, we assume the
567              first char after the mnemonic part is a space.  */
568           /* FIXME: We also take inappropriate advantage of the fact that
569              GAS's input scrubber will remove extraneous blanks.  */
570           if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
571             {
572 #ifdef CGEN_MNEMONIC_OPERANDS
573               if (CGEN_SYNTAX_CHAR(* syn) == ' ')
574                 past_opcode_p = 1;
575 #endif
576               ++ syn;
577               ++ str;
578             }
579           else if (*str)
580             {
581               /* Syntax char didn't match.  Can't be this insn.  */
582               static char msg [80];
583
584               /* xgettext:c-format */
585               sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
586                        CGEN_SYNTAX_CHAR(*syn), *str);
587               return msg;
588             }
589           else
590             {
591               /* Ran out of input.  */
592               static char msg [80];
593
594               /* xgettext:c-format */
595               sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
596                        CGEN_SYNTAX_CHAR(*syn));
597               return msg;
598             }
599           continue;
600         }
601
602       /* We have an operand of some sort.  */
603       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
604                                           &str, fields);
605       if (errmsg)
606         return errmsg;
607
608       /* Done with this operand, continue with next one.  */
609       ++ syn;
610     }
611
612   /* If we're at the end of the syntax string, we're done.  */
613   if (* syn == 0)
614     {
615       /* FIXME: For the moment we assume a valid `str' can only contain
616          blanks now.  IE: We needn't try again with a longer version of
617          the insn and it is assumed that longer versions of insns appear
618          before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
619       while (ISSPACE (* str))
620         ++ str;
621
622       if (* str != '\0')
623         return _("junk at end of line"); /* FIXME: would like to include `str' */
624
625       return NULL;
626     }
627
628   /* We couldn't parse it.  */
629   return _("unrecognized instruction");
630 }
631 \f
632 /* Main entry point.
633    This routine is called for each instruction to be assembled.
634    STR points to the insn to be assembled.
635    We assume all necessary tables have been initialized.
636    The assembled instruction, less any fixups, is stored in BUF.
637    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
638    still needs to be converted to target byte order, otherwise BUF is an array
639    of bytes in target byte order.
640    The result is a pointer to the insn's entry in the opcode table,
641    or NULL if an error occured (an error message will have already been
642    printed).
643
644    Note that when processing (non-alias) macro-insns,
645    this function recurses.
646
647    ??? It's possible to make this cpu-independent.
648    One would have to deal with a few minor things.
649    At this point in time doing so would be more of a curiosity than useful
650    [for example this file isn't _that_ big], but keeping the possibility in
651    mind helps keep the design clean.  */
652
653 const CGEN_INSN *
654 xc16x_cgen_assemble_insn (CGEN_CPU_DESC cd,
655                            const char *str,
656                            CGEN_FIELDS *fields,
657                            CGEN_INSN_BYTES_PTR buf,
658                            char **errmsg)
659 {
660   const char *start;
661   CGEN_INSN_LIST *ilist;
662   const char *parse_errmsg = NULL;
663   const char *insert_errmsg = NULL;
664   int recognized_mnemonic = 0;
665
666   /* Skip leading white space.  */
667   while (ISSPACE (* str))
668     ++ str;
669
670   /* The instructions are stored in hashed lists.
671      Get the first in the list.  */
672   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
673
674   /* Keep looking until we find a match.  */
675   start = str;
676   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
677     {
678       const CGEN_INSN *insn = ilist->insn;
679       recognized_mnemonic = 1;
680
681 #ifdef CGEN_VALIDATE_INSN_SUPPORTED 
682       /* Not usually needed as unsupported opcodes
683          shouldn't be in the hash lists.  */
684       /* Is this insn supported by the selected cpu?  */
685       if (! xc16x_cgen_insn_supported (cd, insn))
686         continue;
687 #endif
688       /* If the RELAXED attribute is set, this is an insn that shouldn't be
689          chosen immediately.  Instead, it is used during assembler/linker
690          relaxation if possible.  */
691       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
692         continue;
693
694       str = start;
695
696       /* Skip this insn if str doesn't look right lexically.  */
697       if (CGEN_INSN_RX (insn) != NULL &&
698           regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
699         continue;
700
701       /* Allow parse/insert handlers to obtain length of insn.  */
702       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
703
704       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
705       if (parse_errmsg != NULL)
706         continue;
707
708       /* ??? 0 is passed for `pc'.  */
709       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
710                                                  (bfd_vma) 0);
711       if (insert_errmsg != NULL)
712         continue;
713
714       /* It is up to the caller to actually output the insn and any
715          queued relocs.  */
716       return insn;
717     }
718
719   {
720     static char errbuf[150];
721 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
722     const char *tmp_errmsg;
723
724     /* If requesting verbose error messages, use insert_errmsg.
725        Failing that, use parse_errmsg.  */
726     tmp_errmsg = (insert_errmsg ? insert_errmsg :
727                   parse_errmsg ? parse_errmsg :
728                   recognized_mnemonic ?
729                   _("unrecognized form of instruction") :
730                   _("unrecognized instruction"));
731
732     if (strlen (start) > 50)
733       /* xgettext:c-format */
734       sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
735     else 
736       /* xgettext:c-format */
737       sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
738 #else
739     if (strlen (start) > 50)
740       /* xgettext:c-format */
741       sprintf (errbuf, _("bad instruction `%.50s...'"), start);
742     else 
743       /* xgettext:c-format */
744       sprintf (errbuf, _("bad instruction `%.50s'"), start);
745 #endif
746       
747     *errmsg = errbuf;
748     return NULL;
749   }
750 }