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