[cgen/cpu]
[external/binutils.git] / opcodes / mep-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, 2007, 2008
8    Free Software Foundation, Inc.
9
10    This file is part of libopcodes.
11
12    This library 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 3, or (at your option)
15    any later version.
16
17    It is distributed in the hope that it will be useful, but WITHOUT
18    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
20    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
27 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
28    Keep that in mind.  */
29
30 #include "sysdep.h"
31 #include <stdio.h>
32 #include "ansidecl.h"
33 #include "bfd.h"
34 #include "symcat.h"
35 #include "mep-desc.h"
36 #include "mep-opc.h"
37 #include "opintl.h"
38 #include "xregex.h"
39 #include "libiberty.h"
40 #include "safe-ctype.h"
41
42 #undef  min
43 #define min(a,b) ((a) < (b) ? (a) : (b))
44 #undef  max
45 #define max(a,b) ((a) > (b) ? (a) : (b))
46
47 static const char * parse_insn_normal
48   (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
49 \f
50 /* -- assembler routines inserted here.  */
51
52 /* -- asm.c */
53
54 #include "elf/mep.h"
55
56 #define CGEN_VALIDATE_INSN_SUPPORTED
57
58        const char * parse_csrn       (CGEN_CPU_DESC, const char **, CGEN_KEYWORD *, long *);
59        const char * parse_tpreg      (CGEN_CPU_DESC, const char **, CGEN_KEYWORD *, long *);
60        const char * parse_spreg      (CGEN_CPU_DESC, const char **, CGEN_KEYWORD *, long *);
61        const char * parse_mep_align  (CGEN_CPU_DESC, const char **, enum cgen_operand_type, long *);
62        const char * parse_mep_alignu (CGEN_CPU_DESC, const char **, enum cgen_operand_type, unsigned long *);
63 static const char * parse_signed16   (CGEN_CPU_DESC, const char **, int, long *);
64 static const char * parse_unsigned16 (CGEN_CPU_DESC, const char **, int, unsigned long *);
65 static const char * parse_lo16       (CGEN_CPU_DESC, const char **, int, long *, long);
66 static const char * parse_unsigned7  (CGEN_CPU_DESC, const char **, enum cgen_operand_type, unsigned long *);
67 static const char * parse_zero       (CGEN_CPU_DESC, const char **, int, long *);
68
69 const char *
70 parse_csrn (CGEN_CPU_DESC cd, const char **strp,
71             CGEN_KEYWORD *keyword_table, long *field)
72 {
73   const char *err;
74   unsigned long value;
75
76   err = cgen_parse_keyword (cd, strp, keyword_table, field);
77   if (!err)
78     return NULL;
79
80   err = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CSRN_IDX, & value);
81   if (err)
82     return err;
83   *field = value;
84   return NULL;
85 }
86
87 /* begin-cop-ip-parse-handlers */
88 static const char *
89 parse_ivc2_cr (CGEN_CPU_DESC,
90         const char **,
91         CGEN_KEYWORD *,
92         long *) ATTRIBUTE_UNUSED;
93 static const char *
94 parse_ivc2_cr (CGEN_CPU_DESC cd,
95         const char **strp,
96         CGEN_KEYWORD *keyword_table  ATTRIBUTE_UNUSED,
97         long *field)
98 {
99   return cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr_ivc2, field);
100 }
101 static const char *
102 parse_ivc2_ccr (CGEN_CPU_DESC,
103         const char **,
104         CGEN_KEYWORD *,
105         long *) ATTRIBUTE_UNUSED;
106 static const char *
107 parse_ivc2_ccr (CGEN_CPU_DESC cd,
108         const char **strp,
109         CGEN_KEYWORD *keyword_table  ATTRIBUTE_UNUSED,
110         long *field)
111 {
112   return cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, field);
113 }
114 /* end-cop-ip-parse-handlers */
115
116 const char *
117 parse_tpreg (CGEN_CPU_DESC cd, const char ** strp,
118              CGEN_KEYWORD *keyword_table, long *field)
119 {
120   const char *err;
121
122   err = cgen_parse_keyword (cd, strp, keyword_table, field);
123   if (err)
124     return err;
125   if (*field != 13)
126     return _("Only $tp or $13 allowed for this opcode");
127   return NULL;
128 }
129
130 const char *
131 parse_spreg (CGEN_CPU_DESC cd, const char ** strp,
132              CGEN_KEYWORD *keyword_table, long *field)
133 {
134   const char *err;
135
136   err = cgen_parse_keyword (cd, strp, keyword_table, field);
137   if (err)
138     return err;
139   if (*field != 15)
140     return _("Only $sp or $15 allowed for this opcode");
141   return NULL;
142 }
143
144 const char *
145 parse_mep_align (CGEN_CPU_DESC cd, const char ** strp,
146                  enum cgen_operand_type type, long *field)
147 {
148   long lsbs = 0;
149   const char *err;
150
151   switch (type)
152     {
153     case MEP_OPERAND_PCREL8A2:
154     case MEP_OPERAND_PCREL12A2:
155     case MEP_OPERAND_PCREL17A2:
156     case MEP_OPERAND_PCREL24A2:
157       err = cgen_parse_signed_integer   (cd, strp, type, field);
158       break;
159     case MEP_OPERAND_PCABS24A2:
160     case MEP_OPERAND_UDISP7:
161     case MEP_OPERAND_UDISP7A2:
162     case MEP_OPERAND_UDISP7A4:
163     case MEP_OPERAND_UIMM7A4:
164     case MEP_OPERAND_ADDR24A4:
165       err = cgen_parse_unsigned_integer (cd, strp, type, (unsigned long *) field);
166       break;
167     default:
168       abort();
169     }
170   if (err)
171     return err;
172   switch (type)
173     {
174     case MEP_OPERAND_UDISP7:
175       lsbs = 0;
176       break;
177     case MEP_OPERAND_PCREL8A2:
178     case MEP_OPERAND_PCREL12A2:
179     case MEP_OPERAND_PCREL17A2:
180     case MEP_OPERAND_PCREL24A2:
181     case MEP_OPERAND_PCABS24A2:
182     case MEP_OPERAND_UDISP7A2:
183       lsbs = *field & 1;
184       break;
185     case MEP_OPERAND_UDISP7A4:
186     case MEP_OPERAND_UIMM7A4:
187     case MEP_OPERAND_ADDR24A4:
188       lsbs = *field & 3;
189       break;
190       lsbs = *field & 7;
191       break;
192     default:
193       /* Safe assumption?  */
194       abort ();
195     }
196   if (lsbs)
197     return "Value is not aligned enough";
198   return NULL;
199 }
200
201 const char *
202 parse_mep_alignu (CGEN_CPU_DESC cd, const char ** strp,
203                  enum cgen_operand_type type, unsigned long *field)
204 {
205   return parse_mep_align (cd, strp, type, (long *) field);
206 }
207
208
209 /* Handle %lo(), %tpoff(), %sdaoff(), %hi(), and other signed
210    constants in a signed context.  */
211
212 static const char *
213 parse_signed16 (CGEN_CPU_DESC cd,
214                 const char **strp,
215                 int opindex,
216                 long *valuep)
217 {
218   return parse_lo16 (cd, strp, opindex, valuep, 1);
219 }
220
221 static const char *
222 parse_lo16 (CGEN_CPU_DESC cd,
223             const char **strp,
224             int opindex,
225             long *valuep,
226             long signedp)
227 {
228   const char *errmsg;
229   enum cgen_parse_operand_result result_type;
230   bfd_vma value;
231
232   if (strncasecmp (*strp, "%lo(", 4) == 0)
233     {
234       *strp += 4;
235       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_LOW16,
236                                    & result_type, & value);
237       if (**strp != ')')
238         return _("missing `)'");
239       ++*strp;
240       if (errmsg == NULL
241           && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
242         value &= 0xffff;
243       if (signedp)
244         *valuep = (long)(short) value;
245       else
246         *valuep = value;
247       return errmsg;
248     }
249
250   if (strncasecmp (*strp, "%hi(", 4) == 0)
251     {
252       *strp += 4;
253       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_HI16S,
254                                    & result_type, & value);
255       if (**strp != ')')
256         return _("missing `)'");
257       ++*strp;
258       if (errmsg == NULL
259           && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
260         value = (value + 0x8000) >> 16;
261       *valuep = value;
262       return errmsg;
263     }
264
265   if (strncasecmp (*strp, "%uhi(", 5) == 0)
266     {
267       *strp += 5;
268       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_HI16U,
269                                    & result_type, & value);
270       if (**strp != ')')
271         return _("missing `)'");
272       ++*strp;
273       if (errmsg == NULL
274           && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
275         value = value >> 16;
276       *valuep = value;
277       return errmsg;
278     }
279
280   if (strncasecmp (*strp, "%sdaoff(", 8) == 0)
281     {
282       *strp += 8;
283       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_GPREL,
284                                    NULL, & value);
285       if (**strp != ')')
286         return _("missing `)'");
287       ++*strp;
288       *valuep = value;
289       return errmsg;
290     }
291
292   if (strncasecmp (*strp, "%tpoff(", 7) == 0)
293     {
294       *strp += 7;
295       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_TPREL,
296                                    NULL, & value);
297       if (**strp != ')')
298         return _("missing `)'");
299       ++*strp;
300       *valuep = value;
301       return errmsg;
302     }
303
304   if (**strp == '%')
305     return _("invalid %function() here");
306
307   return cgen_parse_signed_integer (cd, strp, opindex, valuep);
308 }
309
310 static const char *
311 parse_unsigned16 (CGEN_CPU_DESC cd,
312                   const char **strp,
313                   int opindex,
314                   unsigned long *valuep)
315 {
316   return parse_lo16 (cd, strp, opindex, (long *) valuep, 0);
317 }
318
319 /* A special case of parse_signed16 which accepts only the value zero.  */
320
321 static const char *
322 parse_zero (CGEN_CPU_DESC cd, const char **strp, int opindex, long *valuep)
323 {
324   const char *errmsg;
325   enum cgen_parse_operand_result result_type;
326   bfd_vma value;
327
328   /*fprintf(stderr, "dj: signed parse opindex `%s'\n", *strp);*/
329
330   /* Prevent ($ry) from being attempted as an expression on 'sw $rx,($ry)'.
331      It will fail and cause ry to be listed as an undefined symbol in the
332      listing.  */
333   if (strncmp (*strp, "($", 2) == 0)
334     return "not zero"; /* any string will do -- will never be seen.  */
335
336   if (strncasecmp (*strp, "%lo(", 4) == 0)
337     {
338       *strp += 4;
339       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_LOW16,
340                                    &result_type, &value);
341       if (**strp != ')')
342         return "missing `)'";
343       ++*strp;
344       if (errmsg == NULL
345           && (result_type != CGEN_PARSE_OPERAND_RESULT_NUMBER || value != 0))
346         return "not zero"; /* any string will do -- will never be seen.  */
347       *valuep = value;
348       return errmsg;
349     }
350
351   if (strncasecmp (*strp, "%hi(", 4) == 0)
352     {
353       *strp += 4;
354       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_HI16S,
355                                    &result_type, &value);
356       if (**strp != ')')
357         return "missing `)'";
358       ++*strp;
359       if (errmsg == NULL
360           && (result_type != CGEN_PARSE_OPERAND_RESULT_NUMBER || value != 0))
361         return "not zero"; /* any string will do -- will never be seen.  */
362       *valuep = value;
363       return errmsg;
364     }
365
366   if (strncasecmp (*strp, "%uhi(", 5) == 0)
367     {
368       *strp += 5;
369       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_HI16U,
370                                    &result_type, &value);
371       if (**strp != ')')
372         return "missing `)'";
373       ++*strp;
374       if (errmsg == NULL
375           && (result_type != CGEN_PARSE_OPERAND_RESULT_NUMBER || value != 0))
376         return "not zero"; /* any string will do -- will never be seen.  */
377       *valuep = value;
378       return errmsg;
379     }
380
381   if (strncasecmp (*strp, "%sdaoff(", 8) == 0)
382     {
383       *strp += 8;
384       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_GPREL,
385                                    &result_type, &value);
386       if (**strp != ')')
387         return "missing `)'";
388       ++*strp;
389       if (errmsg == NULL
390           && (result_type != CGEN_PARSE_OPERAND_RESULT_NUMBER || value != 0))
391         return "not zero"; /* any string will do -- will never be seen.  */
392       *valuep = value;
393       return errmsg;
394     }
395
396   if (strncasecmp (*strp, "%tpoff(", 7) == 0)
397     {
398       *strp += 7;
399       errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_MEP_TPREL,
400                                    &result_type, &value);
401       if (**strp != ')')
402         return "missing `)'";
403       ++*strp;
404       if (errmsg == NULL
405           && (result_type != CGEN_PARSE_OPERAND_RESULT_NUMBER || value != 0))
406         return "not zero"; /* any string will do -- will never be seen.  */
407       *valuep = value;
408       return errmsg;
409     }
410
411   if (**strp == '%')
412     return "invalid %function() here";
413
414   errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_NONE,
415                                &result_type, &value);
416   if (errmsg == NULL
417       && (result_type != CGEN_PARSE_OPERAND_RESULT_NUMBER || value != 0))
418     return "not zero"; /* any string will do -- will never be seen.  */
419
420   return errmsg;
421 }
422
423 static const char *
424 parse_unsigned7 (CGEN_CPU_DESC cd, const char **strp,
425                  enum cgen_operand_type opindex, unsigned long *valuep)
426 {
427   const char *errmsg;
428   bfd_vma value;
429
430   /* fprintf(stderr, "dj: unsigned7 parse `%s'\n", *strp); */
431
432   if (strncasecmp (*strp, "%tpoff(", 7) == 0)
433     {
434       int reloc;
435       *strp += 7;
436       switch (opindex)
437         {
438         case MEP_OPERAND_UDISP7:
439           reloc = BFD_RELOC_MEP_TPREL7;
440           break;
441         case MEP_OPERAND_UDISP7A2:
442           reloc = BFD_RELOC_MEP_TPREL7A2;
443           break;
444         case MEP_OPERAND_UDISP7A4:
445           reloc = BFD_RELOC_MEP_TPREL7A4;
446           break;
447         default:
448           /* Safe assumption?  */
449           abort (); 
450         }
451       errmsg = cgen_parse_address (cd, strp, opindex, reloc,
452                                    NULL, &value);
453       if (**strp != ')')
454         return "missing `)'";
455       ++*strp;
456       *valuep = value;
457       return errmsg;
458     }
459
460   if (**strp == '%')
461     return _("invalid %function() here");
462
463   return parse_mep_alignu (cd, strp, opindex, valuep);
464 }
465
466 static ATTRIBUTE_UNUSED const char *
467 parse_cdisp10 (CGEN_CPU_DESC cd,
468                const char **strp,
469                int opindex,
470                long *valuep)
471 {
472   const char *errmsg = 0;
473   signed long value;
474   long have_zero = 0;
475   int wide = 0;
476   int alignment;
477
478   switch (opindex)
479     {
480     case MEP_OPERAND_CDISP10A4:
481       alignment = 2;
482       break;
483     case MEP_OPERAND_CDISP10A2:
484       alignment = 1;
485       break;
486     case MEP_OPERAND_CDISP10:
487     default:
488       alignment = 0;
489       break;
490     }
491
492   if ((MEP_CPU & EF_MEP_CPU_MASK) == EF_MEP_CPU_C5)
493     wide = 1;
494
495   if (strncmp (*strp, "0x0", 3) == 0 
496       || (**strp == '0' && *(*strp + 1) != 'x'))
497     have_zero = 1;
498
499   errmsg = cgen_parse_signed_integer (cd, strp, opindex, & value);
500   if (errmsg)
501     return errmsg;
502
503   if (wide)
504     {
505       if (value < -512 || value > 511)
506         return _("Immediate is out of range -512 to 511");
507     }
508   else
509     {
510       if (value < -128 || value > 127)
511         return _("Immediate is out of range -128 to 127");
512     }
513
514   if (value & ((1<<alignment)-1))
515     return _("Value is not aligned enough");
516
517   /* If this field may require a relocation then use larger dsp16.  */
518   if (! have_zero && value == 0)
519     return (wide ? _("Immediate is out of range -512 to 511")
520             : _("Immediate is out of range -128 to 127"));
521
522   *valuep = value;
523   return 0;
524 }
525
526 /* BEGIN LIGHTWEIGHT MACRO PROCESSOR.  */
527
528 #define MAXARGS 9
529
530 typedef struct
531 {
532   char *name;
533   char *expansion;
534 }  macro;
535
536 typedef struct
537 {
538   const char *start;
539   int len;
540 } arg;
541
542 macro macros[] =
543 {
544   { "sizeof", "(`1.end + (- `1))"},
545   { "startof", "(`1 | 0)" },
546   { "align4", "(`1&(~3))"},
547 /*{ "hi", "(((`1+0x8000)>>16) & 0xffff)" },  */
548 /*{ "lo", "(`1 & 0xffff)" },  */
549 /*{ "sdaoff", "((`1-__sdabase) & 0x7f)"},  */
550 /*{ "tpoff", "((`1-__tpbase) & 0x7f)"},  */
551   { 0,0 }
552 };
553
554 static char  * expand_string    (const char *, int);
555
556 static const char *
557 mep_cgen_expand_macros_and_parse_operand
558   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
559
560 static char *
561 str_append (char *dest, const char *input, int len)
562 {  
563   char *new_dest;
564   int oldlen;
565
566   if (len == 0)
567     return dest;
568   /* printf("str_append: <<%s>>, <<%s>>, %d\n", dest, input, len); */
569   oldlen = (dest ? strlen(dest) : 0);
570   new_dest = realloc (dest, oldlen + len + 1);
571   memset (new_dest + oldlen, 0, len + 1);
572   return strncat (new_dest, input, len);
573 }
574
575 static macro *
576 lookup_macro (const char *name)
577 {
578   macro *m;
579
580   for (m = macros; m->name; ++m)
581     if (strncmp (m->name, name, strlen(m->name)) == 0)
582       return m;
583
584   return 0;
585 }
586
587 static char *
588 expand_macro (arg *args, int narg, macro *mac)
589 {
590   char *result = 0, *rescanned_result = 0;
591   char *e = mac->expansion;
592   char *mark = e;
593   int arg = 0;
594
595   /*  printf("expanding macro %s with %d args\n", mac->name, narg + 1); */
596   while (*e)
597     {
598       if (*e == '`' && 
599           (*e+1) && 
600           ((*(e + 1) - '1') <= MAXARGS) &&
601           ((*(e + 1) - '1') <= narg))
602         {
603           result = str_append (result, mark, e - mark);
604           arg = (*(e + 1) - '1');
605           /* printf("replacing `%d with %s\n", arg+1, args[arg].start); */
606           result = str_append (result, args[arg].start, args[arg].len);
607           ++e;
608           mark = e+1;
609         }
610       ++e;
611     }
612
613   if (mark != e)
614     result = str_append (result, mark, e - mark);
615
616   if (result)
617     {
618       rescanned_result = expand_string (result, 0);
619       free (result);
620       return rescanned_result;
621     }
622   else 
623     return result;
624 }
625
626 #define IN_TEXT 0
627 #define IN_ARGS 1
628
629 static char *
630 expand_string (const char *in, int first_only)
631 {
632   int num_expansions = 0;
633   int depth = 0;
634   int narg = -1;
635   arg args[MAXARGS];
636   int state = IN_TEXT;
637   const char *mark = in;
638   macro *macro = 0;
639
640   char *expansion = 0;
641   char *result = 0;
642
643   while (*in)
644     {
645       switch (state)
646         {
647         case IN_TEXT:
648           if (*in == '%' && *(in + 1) && (!first_only || num_expansions == 0)) 
649             {         
650               macro = lookup_macro (in + 1);
651               if (macro)
652                 {
653                   /* printf("entering state %d at '%s'...\n", state, in); */
654                   result = str_append (result, mark, in - mark);
655                   mark = in;
656                   in += 1 + strlen (macro->name);
657                   while (*in == ' ') ++in;
658                   if (*in != '(')
659                     {
660                       state = IN_TEXT;                
661                       macro = 0;
662                     }
663                   else
664                     {
665                       state = IN_ARGS;
666                       narg = 0;
667                       args[narg].start = in + 1;
668                       args[narg].len = 0;
669                       mark = in + 1;                          
670                     }
671                 }
672             }
673           break;
674         case IN_ARGS:
675           if (depth == 0)
676             {
677               switch (*in)
678                 {
679                 case ',':
680                   narg++;
681                   args[narg].start = (in + 1);
682                   args[narg].len = 0;
683                   break;
684                 case ')':
685                   state = IN_TEXT;
686                   /* printf("entering state %d at '%s'...\n", state, in); */
687                   if (macro)
688                     {
689                       expansion = 0;
690                       expansion = expand_macro (args, narg, macro);
691                       num_expansions++;
692                       if (expansion)
693                         {
694                           result = str_append (result, expansion, strlen (expansion));
695                           free (expansion);
696                         }
697                     }
698                   else
699                     {
700                       result = str_append (result, mark, in - mark);
701                     }
702                   macro = 0;
703                   mark = in + 1;
704                   break;
705                 case '(':
706                   depth++;
707                 default:
708                   args[narg].len++;
709                   break;                  
710                 }
711             } 
712           else
713             {
714               if (*in == ')')
715                 depth--;
716               if (narg > -1)
717                 args[narg].len++;
718             }
719           
720         }
721       ++in;
722     }
723   
724   if (mark != in)
725     result = str_append (result, mark, in - mark);
726   
727   return result;
728 }
729
730 #undef IN_ARGS
731 #undef IN_TEXT
732 #undef MAXARGS
733
734
735 /* END LIGHTWEIGHT MACRO PROCESSOR.  */
736
737 const char * mep_cgen_parse_operand
738   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
739
740 const char *
741 mep_cgen_expand_macros_and_parse_operand (CGEN_CPU_DESC cd, int opindex,
742                                           const char ** strp_in, CGEN_FIELDS * fields)
743 {
744   const char * errmsg = NULL;
745   char *str = 0, *hold = 0;
746   const char **strp = 0;
747
748   /* Set up a new pointer to macro-expanded string.  */
749   str = expand_string (*strp_in, 1);
750   /* fprintf (stderr, " expanded <<%s>> to <<%s>>\n", *strp_in, str); */
751
752   hold = str;
753   strp = (const char **)(&str);
754
755   errmsg = mep_cgen_parse_operand (cd, opindex, strp, fields);
756
757   /* Now work out the advance.  */
758   if (strlen (str) == 0)
759     *strp_in += strlen (*strp_in);
760
761   else
762     {
763       if (strstr (*strp_in, str))
764         /* A macro-expansion was pulled off the front.  */
765         *strp_in = strstr (*strp_in, str);  
766       else
767         /* A non-macro-expansion was pulled off the front.  */
768         *strp_in += (str - hold); 
769     }
770
771   if (hold)
772     free (hold);
773
774   return errmsg;
775 }
776
777 #define CGEN_ASM_INIT_HOOK (cd->parse_operand = mep_cgen_expand_macros_and_parse_operand); 
778
779 /* -- dis.c */
780
781 const char * mep_cgen_parse_operand
782   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
783
784 /* Main entry point for operand parsing.
785
786    This function is basically just a big switch statement.  Earlier versions
787    used tables to look up the function to use, but
788    - if the table contains both assembler and disassembler functions then
789      the disassembler contains much of the assembler and vice-versa,
790    - there's a lot of inlining possibilities as things grow,
791    - using a switch statement avoids the function call overhead.
792
793    This function could be moved into `parse_insn_normal', but keeping it
794    separate makes clear the interface between `parse_insn_normal' and each of
795    the handlers.  */
796
797 const char *
798 mep_cgen_parse_operand (CGEN_CPU_DESC cd,
799                            int opindex,
800                            const char ** strp,
801                            CGEN_FIELDS * fields)
802 {
803   const char * errmsg = NULL;
804   /* Used by scalar operands that still need to be parsed.  */
805   long junk ATTRIBUTE_UNUSED;
806
807   switch (opindex)
808     {
809     case MEP_OPERAND_ADDR24A4 :
810       errmsg = parse_mep_alignu (cd, strp, MEP_OPERAND_ADDR24A4, (unsigned long *) (& fields->f_24u8a4n));
811       break;
812     case MEP_OPERAND_C5RMUIMM20 :
813       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_C5RMUIMM20, (unsigned long *) (& fields->f_c5_rmuimm20));
814       break;
815     case MEP_OPERAND_C5RNMUIMM24 :
816       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_C5RNMUIMM24, (unsigned long *) (& fields->f_c5_rnmuimm24));
817       break;
818     case MEP_OPERAND_CALLNUM :
819       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CALLNUM, (unsigned long *) (& fields->f_callnum));
820       break;
821     case MEP_OPERAND_CCCC :
822       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CCCC, (unsigned long *) (& fields->f_rm));
823       break;
824     case MEP_OPERAND_CCRN :
825       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr, & fields->f_ccrn);
826       break;
827     case MEP_OPERAND_CDISP10 :
828       errmsg = parse_cdisp10 (cd, strp, MEP_OPERAND_CDISP10, (long *) (& fields->f_cdisp10));
829       break;
830     case MEP_OPERAND_CDISP10A2 :
831       errmsg = parse_cdisp10 (cd, strp, MEP_OPERAND_CDISP10A2, (long *) (& fields->f_cdisp10));
832       break;
833     case MEP_OPERAND_CDISP10A4 :
834       errmsg = parse_cdisp10 (cd, strp, MEP_OPERAND_CDISP10A4, (long *) (& fields->f_cdisp10));
835       break;
836     case MEP_OPERAND_CDISP10A8 :
837       errmsg = parse_cdisp10 (cd, strp, MEP_OPERAND_CDISP10A8, (long *) (& fields->f_cdisp10));
838       break;
839     case MEP_OPERAND_CDISP12 :
840       errmsg = cgen_parse_signed_integer (cd, strp, MEP_OPERAND_CDISP12, (long *) (& fields->f_12s20));
841       break;
842     case MEP_OPERAND_CIMM4 :
843       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CIMM4, (unsigned long *) (& fields->f_rn));
844       break;
845     case MEP_OPERAND_CIMM5 :
846       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CIMM5, (unsigned long *) (& fields->f_5u24));
847       break;
848     case MEP_OPERAND_CODE16 :
849       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CODE16, (unsigned long *) (& fields->f_16u16));
850       break;
851     case MEP_OPERAND_CODE24 :
852       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CODE24, (unsigned long *) (& fields->f_24u4n));
853       break;
854     case MEP_OPERAND_CP_FLAG :
855       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr, & junk);
856       break;
857     case MEP_OPERAND_CRN :
858       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr, & fields->f_crn);
859       break;
860     case MEP_OPERAND_CRN64 :
861       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_crn);
862       break;
863     case MEP_OPERAND_CRNX :
864       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr, & fields->f_crnx);
865       break;
866     case MEP_OPERAND_CRNX64 :
867       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_crnx);
868       break;
869     case MEP_OPERAND_CROC :
870       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_ivc2_5u7);
871       break;
872     case MEP_OPERAND_CROP :
873       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_ivc2_5u23);
874       break;
875     case MEP_OPERAND_CRPC :
876       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_ivc2_5u26);
877       break;
878     case MEP_OPERAND_CRPP :
879       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_ivc2_5u18);
880       break;
881     case MEP_OPERAND_CRQC :
882       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_ivc2_5u21);
883       break;
884     case MEP_OPERAND_CRQP :
885       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_ivc2_5u13);
886       break;
887     case MEP_OPERAND_CSRN :
888       errmsg = parse_csrn (cd, strp, & mep_cgen_opval_h_csr, & fields->f_csrn);
889       break;
890     case MEP_OPERAND_CSRN_IDX :
891       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_CSRN_IDX, (unsigned long *) (& fields->f_csrn));
892       break;
893     case MEP_OPERAND_DBG :
894       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
895       break;
896     case MEP_OPERAND_DEPC :
897       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
898       break;
899     case MEP_OPERAND_EPC :
900       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
901       break;
902     case MEP_OPERAND_EXC :
903       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
904       break;
905     case MEP_OPERAND_HI :
906       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
907       break;
908     case MEP_OPERAND_IMM16P0 :
909       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM16P0, (unsigned long *) (& fields->f_ivc2_imm16p0));
910       break;
911     case MEP_OPERAND_IMM3P12 :
912       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM3P12, (unsigned long *) (& fields->f_ivc2_3u12));
913       break;
914     case MEP_OPERAND_IMM3P25 :
915       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM3P25, (unsigned long *) (& fields->f_ivc2_3u25));
916       break;
917     case MEP_OPERAND_IMM3P4 :
918       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM3P4, (unsigned long *) (& fields->f_ivc2_3u4));
919       break;
920     case MEP_OPERAND_IMM3P5 :
921       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM3P5, (unsigned long *) (& fields->f_ivc2_3u5));
922       break;
923     case MEP_OPERAND_IMM3P9 :
924       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM3P9, (unsigned long *) (& fields->f_ivc2_3u9));
925       break;
926     case MEP_OPERAND_IMM4P10 :
927       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM4P10, (unsigned long *) (& fields->f_ivc2_4u10));
928       break;
929     case MEP_OPERAND_IMM4P4 :
930       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM4P4, (unsigned long *) (& fields->f_ivc2_4u4));
931       break;
932     case MEP_OPERAND_IMM4P8 :
933       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM4P8, (unsigned long *) (& fields->f_ivc2_4u8));
934       break;
935     case MEP_OPERAND_IMM5P23 :
936       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM5P23, (unsigned long *) (& fields->f_ivc2_5u23));
937       break;
938     case MEP_OPERAND_IMM5P3 :
939       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM5P3, (unsigned long *) (& fields->f_ivc2_5u3));
940       break;
941     case MEP_OPERAND_IMM5P7 :
942       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM5P7, (unsigned long *) (& fields->f_ivc2_5u7));
943       break;
944     case MEP_OPERAND_IMM5P8 :
945       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM5P8, (unsigned long *) (& fields->f_ivc2_5u8));
946       break;
947     case MEP_OPERAND_IMM6P2 :
948       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM6P2, (unsigned long *) (& fields->f_ivc2_6u2));
949       break;
950     case MEP_OPERAND_IMM6P6 :
951       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM6P6, (unsigned long *) (& fields->f_ivc2_6u6));
952       break;
953     case MEP_OPERAND_IMM8P0 :
954       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM8P0, (unsigned long *) (& fields->f_ivc2_8u0));
955       break;
956     case MEP_OPERAND_IMM8P20 :
957       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM8P20, (unsigned long *) (& fields->f_ivc2_8u20));
958       break;
959     case MEP_OPERAND_IMM8P4 :
960       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IMM8P4, (unsigned long *) (& fields->f_ivc2_8u4));
961       break;
962     case MEP_OPERAND_IVC_X_0_2 :
963       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IVC_X_0_2, (unsigned long *) (& fields->f_ivc2_2u0));
964       break;
965     case MEP_OPERAND_IVC_X_0_3 :
966       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IVC_X_0_3, (unsigned long *) (& fields->f_ivc2_3u0));
967       break;
968     case MEP_OPERAND_IVC_X_0_4 :
969       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IVC_X_0_4, (unsigned long *) (& fields->f_ivc2_4u0));
970       break;
971     case MEP_OPERAND_IVC_X_0_5 :
972       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IVC_X_0_5, (unsigned long *) (& fields->f_ivc2_5u0));
973       break;
974     case MEP_OPERAND_IVC_X_6_1 :
975       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IVC_X_6_1, (unsigned long *) (& fields->f_ivc2_1u6));
976       break;
977     case MEP_OPERAND_IVC_X_6_2 :
978       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IVC_X_6_2, (unsigned long *) (& fields->f_ivc2_2u6));
979       break;
980     case MEP_OPERAND_IVC_X_6_3 :
981       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_IVC_X_6_3, (unsigned long *) (& fields->f_ivc2_3u6));
982       break;
983     case MEP_OPERAND_IVC2C3CCRN :
984       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & fields->f_ivc2_ccrn_c3);
985       break;
986     case MEP_OPERAND_IVC2CCRN :
987       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_ccr_ivc2, & fields->f_ivc2_ccrn);
988       break;
989     case MEP_OPERAND_IVC2CRN :
990       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_cr64, & fields->f_ivc2_crnx);
991       break;
992     case MEP_OPERAND_IVC2RM :
993       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_ivc2_crm);
994       break;
995     case MEP_OPERAND_LO :
996       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
997       break;
998     case MEP_OPERAND_LP :
999       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
1000       break;
1001     case MEP_OPERAND_MB0 :
1002       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
1003       break;
1004     case MEP_OPERAND_MB1 :
1005       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
1006       break;
1007     case MEP_OPERAND_ME0 :
1008       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
1009       break;
1010     case MEP_OPERAND_ME1 :
1011       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
1012       break;
1013     case MEP_OPERAND_NPC :
1014       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
1015       break;
1016     case MEP_OPERAND_OPT :
1017       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
1018       break;
1019     case MEP_OPERAND_PCABS24A2 :
1020       errmsg = parse_mep_alignu (cd, strp, MEP_OPERAND_PCABS24A2, (unsigned long *) (& fields->f_24u5a2n));
1021       break;
1022     case MEP_OPERAND_PCREL12A2 :
1023       errmsg = parse_mep_align (cd, strp, MEP_OPERAND_PCREL12A2, (long *) (& fields->f_12s4a2));
1024       break;
1025     case MEP_OPERAND_PCREL17A2 :
1026       errmsg = parse_mep_align (cd, strp, MEP_OPERAND_PCREL17A2, (long *) (& fields->f_17s16a2));
1027       break;
1028     case MEP_OPERAND_PCREL24A2 :
1029       errmsg = parse_mep_align (cd, strp, MEP_OPERAND_PCREL24A2, (long *) (& fields->f_24s5a2n));
1030       break;
1031     case MEP_OPERAND_PCREL8A2 :
1032       errmsg = parse_mep_align (cd, strp, MEP_OPERAND_PCREL8A2, (long *) (& fields->f_8s8a2));
1033       break;
1034     case MEP_OPERAND_PSW :
1035       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
1036       break;
1037     case MEP_OPERAND_R0 :
1038       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & junk);
1039       break;
1040     case MEP_OPERAND_R1 :
1041       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & junk);
1042       break;
1043     case MEP_OPERAND_RL :
1044       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rl);
1045       break;
1046     case MEP_OPERAND_RL5 :
1047       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rl5);
1048       break;
1049     case MEP_OPERAND_RM :
1050       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rm);
1051       break;
1052     case MEP_OPERAND_RMA :
1053       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rm);
1054       break;
1055     case MEP_OPERAND_RN :
1056       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn);
1057       break;
1058     case MEP_OPERAND_RN3 :
1059       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn3);
1060       break;
1061     case MEP_OPERAND_RN3C :
1062       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn3);
1063       break;
1064     case MEP_OPERAND_RN3L :
1065       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn3);
1066       break;
1067     case MEP_OPERAND_RN3S :
1068       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn3);
1069       break;
1070     case MEP_OPERAND_RN3UC :
1071       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn3);
1072       break;
1073     case MEP_OPERAND_RN3UL :
1074       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn3);
1075       break;
1076     case MEP_OPERAND_RN3US :
1077       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn3);
1078       break;
1079     case MEP_OPERAND_RNC :
1080       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn);
1081       break;
1082     case MEP_OPERAND_RNL :
1083       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn);
1084       break;
1085     case MEP_OPERAND_RNS :
1086       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn);
1087       break;
1088     case MEP_OPERAND_RNUC :
1089       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn);
1090       break;
1091     case MEP_OPERAND_RNUL :
1092       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn);
1093       break;
1094     case MEP_OPERAND_RNUS :
1095       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & fields->f_rn);
1096       break;
1097     case MEP_OPERAND_SAR :
1098       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_csr, & junk);
1099       break;
1100     case MEP_OPERAND_SDISP16 :
1101       errmsg = parse_signed16 (cd, strp, MEP_OPERAND_SDISP16, (long *) (& fields->f_16s16));
1102       break;
1103     case MEP_OPERAND_SIMM16 :
1104       errmsg = parse_signed16 (cd, strp, MEP_OPERAND_SIMM16, (long *) (& fields->f_16s16));
1105       break;
1106     case MEP_OPERAND_SIMM16P0 :
1107       errmsg = cgen_parse_signed_integer (cd, strp, MEP_OPERAND_SIMM16P0, (long *) (& fields->f_ivc2_simm16p0));
1108       break;
1109     case MEP_OPERAND_SIMM6 :
1110       errmsg = cgen_parse_signed_integer (cd, strp, MEP_OPERAND_SIMM6, (long *) (& fields->f_6s8));
1111       break;
1112     case MEP_OPERAND_SIMM8 :
1113       errmsg = cgen_parse_signed_integer (cd, strp, MEP_OPERAND_SIMM8, (long *) (& fields->f_8s8));
1114       break;
1115     case MEP_OPERAND_SIMM8P0 :
1116       errmsg = cgen_parse_signed_integer (cd, strp, MEP_OPERAND_SIMM8P0, (long *) (& fields->f_ivc2_8s0));
1117       break;
1118     case MEP_OPERAND_SIMM8P20 :
1119       errmsg = cgen_parse_signed_integer (cd, strp, MEP_OPERAND_SIMM8P20, (long *) (& fields->f_ivc2_8s20));
1120       break;
1121     case MEP_OPERAND_SIMM8P4 :
1122       errmsg = cgen_parse_signed_integer (cd, strp, MEP_OPERAND_SIMM8P4, (long *) (& fields->f_ivc2_8s4));
1123       break;
1124     case MEP_OPERAND_SP :
1125       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & junk);
1126       break;
1127     case MEP_OPERAND_SPR :
1128       errmsg = parse_spreg (cd, strp, & mep_cgen_opval_h_gpr, & junk);
1129       break;
1130     case MEP_OPERAND_TP :
1131       errmsg = cgen_parse_keyword (cd, strp, & mep_cgen_opval_h_gpr, & junk);
1132       break;
1133     case MEP_OPERAND_TPR :
1134       errmsg = parse_tpreg (cd, strp, & mep_cgen_opval_h_gpr, & junk);
1135       break;
1136     case MEP_OPERAND_UDISP2 :
1137       errmsg = cgen_parse_signed_integer (cd, strp, MEP_OPERAND_UDISP2, (long *) (& fields->f_2u6));
1138       break;
1139     case MEP_OPERAND_UDISP7 :
1140       errmsg = parse_unsigned7 (cd, strp, MEP_OPERAND_UDISP7, (unsigned long *) (& fields->f_7u9));
1141       break;
1142     case MEP_OPERAND_UDISP7A2 :
1143       errmsg = parse_unsigned7 (cd, strp, MEP_OPERAND_UDISP7A2, (unsigned long *) (& fields->f_7u9a2));
1144       break;
1145     case MEP_OPERAND_UDISP7A4 :
1146       errmsg = parse_unsigned7 (cd, strp, MEP_OPERAND_UDISP7A4, (unsigned long *) (& fields->f_7u9a4));
1147       break;
1148     case MEP_OPERAND_UIMM16 :
1149       errmsg = parse_unsigned16 (cd, strp, MEP_OPERAND_UIMM16, (unsigned long *) (& fields->f_16u16));
1150       break;
1151     case MEP_OPERAND_UIMM2 :
1152       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_UIMM2, (unsigned long *) (& fields->f_2u10));
1153       break;
1154     case MEP_OPERAND_UIMM24 :
1155       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_UIMM24, (unsigned long *) (& fields->f_24u8n));
1156       break;
1157     case MEP_OPERAND_UIMM3 :
1158       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_UIMM3, (unsigned long *) (& fields->f_3u5));
1159       break;
1160     case MEP_OPERAND_UIMM4 :
1161       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_UIMM4, (unsigned long *) (& fields->f_4u8));
1162       break;
1163     case MEP_OPERAND_UIMM5 :
1164       errmsg = cgen_parse_unsigned_integer (cd, strp, MEP_OPERAND_UIMM5, (unsigned long *) (& fields->f_5u8));
1165       break;
1166     case MEP_OPERAND_UIMM7A4 :
1167       errmsg = parse_mep_alignu (cd, strp, MEP_OPERAND_UIMM7A4, (unsigned long *) (& fields->f_7u9a4));
1168       break;
1169     case MEP_OPERAND_ZERO :
1170       errmsg = parse_zero (cd, strp, MEP_OPERAND_ZERO, (long *) (& junk));
1171       break;
1172
1173     default :
1174       /* xgettext:c-format */
1175       fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
1176       abort ();
1177   }
1178
1179   return errmsg;
1180 }
1181
1182 cgen_parse_fn * const mep_cgen_parse_handlers[] = 
1183 {
1184   parse_insn_normal,
1185 };
1186
1187 void
1188 mep_cgen_init_asm (CGEN_CPU_DESC cd)
1189 {
1190   mep_cgen_init_opcode_table (cd);
1191   mep_cgen_init_ibld_table (cd);
1192   cd->parse_handlers = & mep_cgen_parse_handlers[0];
1193   cd->parse_operand = mep_cgen_parse_operand;
1194 #ifdef CGEN_ASM_INIT_HOOK
1195 CGEN_ASM_INIT_HOOK
1196 #endif
1197 }
1198
1199 \f
1200
1201 /* Regex construction routine.
1202
1203    This translates an opcode syntax string into a regex string,
1204    by replacing any non-character syntax element (such as an
1205    opcode) with the pattern '.*'
1206
1207    It then compiles the regex and stores it in the opcode, for
1208    later use by mep_cgen_assemble_insn
1209
1210    Returns NULL for success, an error message for failure.  */
1211
1212 char * 
1213 mep_cgen_build_insn_regex (CGEN_INSN *insn)
1214 {  
1215   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
1216   const char *mnem = CGEN_INSN_MNEMONIC (insn);
1217   char rxbuf[CGEN_MAX_RX_ELEMENTS];
1218   char *rx = rxbuf;
1219   const CGEN_SYNTAX_CHAR_TYPE *syn;
1220   int reg_err;
1221
1222   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
1223
1224   /* Mnemonics come first in the syntax string.  */
1225   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
1226     return _("missing mnemonic in syntax string");
1227   ++syn;
1228
1229   /* Generate a case sensitive regular expression that emulates case
1230      insensitive matching in the "C" locale.  We cannot generate a case
1231      insensitive regular expression because in Turkish locales, 'i' and 'I'
1232      are not equal modulo case conversion.  */
1233
1234   /* Copy the literal mnemonic out of the insn.  */
1235   for (; *mnem; mnem++)
1236     {
1237       char c = *mnem;
1238
1239       if (ISALPHA (c))
1240         {
1241           *rx++ = '[';
1242           *rx++ = TOLOWER (c);
1243           *rx++ = TOUPPER (c);
1244           *rx++ = ']';
1245         }
1246       else
1247         *rx++ = c;
1248     }
1249
1250   /* Copy any remaining literals from the syntax string into the rx.  */
1251   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
1252     {
1253       if (CGEN_SYNTAX_CHAR_P (* syn)) 
1254         {
1255           char c = CGEN_SYNTAX_CHAR (* syn);
1256
1257           switch (c) 
1258             {
1259               /* Escape any regex metacharacters in the syntax.  */
1260             case '.': case '[': case '\\': 
1261             case '*': case '^': case '$': 
1262
1263 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
1264             case '?': case '{': case '}': 
1265             case '(': case ')': case '*':
1266             case '|': case '+': case ']':
1267 #endif
1268               *rx++ = '\\';
1269               *rx++ = c;
1270               break;
1271
1272             default:
1273               if (ISALPHA (c))
1274                 {
1275                   *rx++ = '[';
1276                   *rx++ = TOLOWER (c);
1277                   *rx++ = TOUPPER (c);
1278                   *rx++ = ']';
1279                 }
1280               else
1281                 *rx++ = c;
1282               break;
1283             }
1284         }
1285       else
1286         {
1287           /* Replace non-syntax fields with globs.  */
1288           *rx++ = '.';
1289           *rx++ = '*';
1290         }
1291     }
1292
1293   /* Trailing whitespace ok.  */
1294   * rx++ = '['; 
1295   * rx++ = ' '; 
1296   * rx++ = '\t'; 
1297   * rx++ = ']'; 
1298   * rx++ = '*'; 
1299
1300   /* But anchor it after that.  */
1301   * rx++ = '$'; 
1302   * rx = '\0';
1303
1304   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
1305   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
1306
1307   if (reg_err == 0) 
1308     return NULL;
1309   else
1310     {
1311       static char msg[80];
1312
1313       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
1314       regfree ((regex_t *) CGEN_INSN_RX (insn));
1315       free (CGEN_INSN_RX (insn));
1316       (CGEN_INSN_RX (insn)) = NULL;
1317       return msg;
1318     }
1319 }
1320
1321 \f
1322 /* Default insn parser.
1323
1324    The syntax string is scanned and operands are parsed and stored in FIELDS.
1325    Relocs are queued as we go via other callbacks.
1326
1327    ??? Note that this is currently an all-or-nothing parser.  If we fail to
1328    parse the instruction, we return 0 and the caller will start over from
1329    the beginning.  Backtracking will be necessary in parsing subexpressions,
1330    but that can be handled there.  Not handling backtracking here may get
1331    expensive in the case of the m68k.  Deal with later.
1332
1333    Returns NULL for success, an error message for failure.  */
1334
1335 static const char *
1336 parse_insn_normal (CGEN_CPU_DESC cd,
1337                    const CGEN_INSN *insn,
1338                    const char **strp,
1339                    CGEN_FIELDS *fields)
1340 {
1341   /* ??? Runtime added insns not handled yet.  */
1342   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
1343   const char *str = *strp;
1344   const char *errmsg;
1345   const char *p;
1346   const CGEN_SYNTAX_CHAR_TYPE * syn;
1347 #ifdef CGEN_MNEMONIC_OPERANDS
1348   /* FIXME: wip */
1349   int past_opcode_p;
1350 #endif
1351
1352   /* For now we assume the mnemonic is first (there are no leading operands).
1353      We can parse it without needing to set up operand parsing.
1354      GAS's input scrubber will ensure mnemonics are lowercase, but we may
1355      not be called from GAS.  */
1356   p = CGEN_INSN_MNEMONIC (insn);
1357   while (*p && TOLOWER (*p) == TOLOWER (*str))
1358     ++p, ++str;
1359
1360   if (* p)
1361     return _("unrecognized instruction");
1362
1363 #ifndef CGEN_MNEMONIC_OPERANDS
1364   if (* str && ! ISSPACE (* str))
1365     return _("unrecognized instruction");
1366 #endif
1367
1368   CGEN_INIT_PARSE (cd);
1369   cgen_init_parse_operand (cd);
1370 #ifdef CGEN_MNEMONIC_OPERANDS
1371   past_opcode_p = 0;
1372 #endif
1373
1374   /* We don't check for (*str != '\0') here because we want to parse
1375      any trailing fake arguments in the syntax string.  */
1376   syn = CGEN_SYNTAX_STRING (syntax);
1377
1378   /* Mnemonics come first for now, ensure valid string.  */
1379   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
1380     abort ();
1381
1382   ++syn;
1383
1384   while (* syn != 0)
1385     {
1386       /* Non operand chars must match exactly.  */
1387       if (CGEN_SYNTAX_CHAR_P (* syn))
1388         {
1389           /* FIXME: While we allow for non-GAS callers above, we assume the
1390              first char after the mnemonic part is a space.  */
1391           /* FIXME: We also take inappropriate advantage of the fact that
1392              GAS's input scrubber will remove extraneous blanks.  */
1393           if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
1394             {
1395 #ifdef CGEN_MNEMONIC_OPERANDS
1396               if (CGEN_SYNTAX_CHAR(* syn) == ' ')
1397                 past_opcode_p = 1;
1398 #endif
1399               ++ syn;
1400               ++ str;
1401             }
1402           else if (*str)
1403             {
1404               /* Syntax char didn't match.  Can't be this insn.  */
1405               static char msg [80];
1406
1407               /* xgettext:c-format */
1408               sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
1409                        CGEN_SYNTAX_CHAR(*syn), *str);
1410               return msg;
1411             }
1412           else
1413             {
1414               /* Ran out of input.  */
1415               static char msg [80];
1416
1417               /* xgettext:c-format */
1418               sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
1419                        CGEN_SYNTAX_CHAR(*syn));
1420               return msg;
1421             }
1422           continue;
1423         }
1424
1425       /* We have an operand of some sort.  */
1426       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
1427                                           &str, fields);
1428       if (errmsg)
1429         return errmsg;
1430
1431       /* Done with this operand, continue with next one.  */
1432       ++ syn;
1433     }
1434
1435   /* If we're at the end of the syntax string, we're done.  */
1436   if (* syn == 0)
1437     {
1438       /* FIXME: For the moment we assume a valid `str' can only contain
1439          blanks now.  IE: We needn't try again with a longer version of
1440          the insn and it is assumed that longer versions of insns appear
1441          before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
1442       while (ISSPACE (* str))
1443         ++ str;
1444
1445       if (* str != '\0')
1446         return _("junk at end of line"); /* FIXME: would like to include `str' */
1447
1448       return NULL;
1449     }
1450
1451   /* We couldn't parse it.  */
1452   return _("unrecognized instruction");
1453 }
1454 \f
1455 /* Main entry point.
1456    This routine is called for each instruction to be assembled.
1457    STR points to the insn to be assembled.
1458    We assume all necessary tables have been initialized.
1459    The assembled instruction, less any fixups, is stored in BUF.
1460    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
1461    still needs to be converted to target byte order, otherwise BUF is an array
1462    of bytes in target byte order.
1463    The result is a pointer to the insn's entry in the opcode table,
1464    or NULL if an error occured (an error message will have already been
1465    printed).
1466
1467    Note that when processing (non-alias) macro-insns,
1468    this function recurses.
1469
1470    ??? It's possible to make this cpu-independent.
1471    One would have to deal with a few minor things.
1472    At this point in time doing so would be more of a curiosity than useful
1473    [for example this file isn't _that_ big], but keeping the possibility in
1474    mind helps keep the design clean.  */
1475
1476 const CGEN_INSN *
1477 mep_cgen_assemble_insn (CGEN_CPU_DESC cd,
1478                            const char *str,
1479                            CGEN_FIELDS *fields,
1480                            CGEN_INSN_BYTES_PTR buf,
1481                            char **errmsg)
1482 {
1483   const char *start;
1484   CGEN_INSN_LIST *ilist;
1485   const char *parse_errmsg = NULL;
1486   const char *insert_errmsg = NULL;
1487   int recognized_mnemonic = 0;
1488
1489   /* Skip leading white space.  */
1490   while (ISSPACE (* str))
1491     ++ str;
1492
1493   /* The instructions are stored in hashed lists.
1494      Get the first in the list.  */
1495   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
1496
1497   /* Keep looking until we find a match.  */
1498   start = str;
1499   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
1500     {
1501       const CGEN_INSN *insn = ilist->insn;
1502       recognized_mnemonic = 1;
1503
1504 #ifdef CGEN_VALIDATE_INSN_SUPPORTED 
1505       /* Not usually needed as unsupported opcodes
1506          shouldn't be in the hash lists.  */
1507       /* Is this insn supported by the selected cpu?  */
1508       if (! mep_cgen_insn_supported (cd, insn))
1509         continue;
1510 #endif
1511       /* If the RELAXED attribute is set, this is an insn that shouldn't be
1512          chosen immediately.  Instead, it is used during assembler/linker
1513          relaxation if possible.  */
1514       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
1515         continue;
1516
1517       str = start;
1518
1519       /* Skip this insn if str doesn't look right lexically.  */
1520       if (CGEN_INSN_RX (insn) != NULL &&
1521           regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
1522         continue;
1523
1524       /* Allow parse/insert handlers to obtain length of insn.  */
1525       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
1526
1527       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
1528       if (parse_errmsg != NULL)
1529         continue;
1530
1531       /* ??? 0 is passed for `pc'.  */
1532       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
1533                                                  (bfd_vma) 0);
1534       if (insert_errmsg != NULL)
1535         continue;
1536
1537       /* It is up to the caller to actually output the insn and any
1538          queued relocs.  */
1539       return insn;
1540     }
1541
1542   {
1543     static char errbuf[150];
1544 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
1545     const char *tmp_errmsg;
1546
1547     /* If requesting verbose error messages, use insert_errmsg.
1548        Failing that, use parse_errmsg.  */
1549     tmp_errmsg = (insert_errmsg ? insert_errmsg :
1550                   parse_errmsg ? parse_errmsg :
1551                   recognized_mnemonic ?
1552                   _("unrecognized form of instruction") :
1553                   _("unrecognized instruction"));
1554
1555     if (strlen (start) > 50)
1556       /* xgettext:c-format */
1557       sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
1558     else 
1559       /* xgettext:c-format */
1560       sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
1561 #else
1562     if (strlen (start) > 50)
1563       /* xgettext:c-format */
1564       sprintf (errbuf, _("bad instruction `%.50s...'"), start);
1565     else 
1566       /* xgettext:c-format */
1567       sprintf (errbuf, _("bad instruction `%.50s'"), start);
1568 #endif
1569       
1570     *errmsg = errbuf;
1571     return NULL;
1572   }
1573 }