OSDN Git Service

528a0d5eebe6185727e60a32013a7ebc3ef12645
[pf3gnuchains/pf3gnuchains4x.git] / opcodes / ms1-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 "ms1-desc.h"
35 #include "ms1-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 /* Range checking for signed numbers.  Returns 0 if acceptable
53    and 1 if the value is out of bounds for a signed quantity.  */
54
55 static int 
56 signed_out_of_bounds (long val)
57 {
58   if ((val < -32768) || (val > 32767))
59     return 1;
60   return 0;
61 }
62
63 static const char *
64 parse_imm16 (CGEN_CPU_DESC cd,
65              const char **strp,
66              int opindex,
67              void *arg)
68 {
69   signed long * valuep = (signed long *) arg;
70   const char *errmsg;
71   enum cgen_parse_operand_result result_type;
72   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
73   bfd_vma value;
74
75   /* Is it a control transfer instructions?  */ 
76   if (opindex == (CGEN_OPERAND_TYPE) MS1_OPERAND_IMM16O)
77     {
78       code = BFD_RELOC_16_PCREL;
79       errmsg = cgen_parse_address (cd, strp, opindex, code,
80                                    & result_type, & value);
81       if (errmsg == NULL)
82         {
83           if (signed_out_of_bounds (value))
84             errmsg = _("Operand out of range. Must be between -32768 and 32767.");
85         }
86       *valuep = value;
87       return errmsg;
88     }
89
90   /* If it's not a control transfer instruction, then
91      we have to check for %OP relocating operators.  */
92   if (strncmp (*strp, "%hi16", 5) == 0)
93     {
94       *strp += 5;
95       code = BFD_RELOC_HI16;
96     }
97   else if (strncmp (*strp, "%lo16", 5) == 0)
98     {
99       *strp += 5;
100       code = BFD_RELOC_LO16;
101     }
102
103   /* If we found a %OP relocating operator, then parse it as an address.
104      If not, we need to parse it as an integer, either signed or unsigned
105      depending on which operand type we have.  */
106   if (code != BFD_RELOC_NONE)
107     {
108        /* %OP relocating operator found.  */
109        errmsg = cgen_parse_address (cd, strp, opindex, code,
110                                    & result_type, & value);
111        if (errmsg == NULL)
112          {
113            switch (result_type)
114              {
115              case (CGEN_PARSE_OPERAND_RESULT_NUMBER):
116                if (code == BFD_RELOC_HI16)
117                  value = (value >> 16) & 0xFFFF;
118                else if (code == BFD_RELOC_LO16)
119                  value = value  & 0xFFFF;
120                else 
121                  errmsg = _("Biiiig Trouble in parse_imm16!");
122                break;
123
124              case (CGEN_PARSE_OPERAND_RESULT_QUEUED):
125                /* No special processing for this case.  */
126                break;
127
128              default:
129                errmsg = _("%operator operand is not a symbol");
130                break;
131              }
132          }
133        *valuep = value;
134     }
135   else
136     {
137       /* Parse hex values like 0xffff as unsigned, and sign extend
138          them manually.  */
139       int parse_signed = (opindex == (CGEN_OPERAND_TYPE)MS1_OPERAND_IMM16);
140
141       if ((*strp)[0] == '0'
142           && ((*strp)[1] == 'x' || (*strp)[1] == 'X'))
143         parse_signed = 0;
144
145       /* No relocating operator.  Parse as an number.  */
146       if (parse_signed)
147         {
148           /* Parse as as signed integer.  */
149  
150           errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep);
151
152           if (errmsg == NULL) 
153             {
154 #if 0
155               /* Manual range checking is needed for the signed case.  */
156               if (*valuep & 0x8000)
157                 value = 0xffff0000 | *valuep;
158               else 
159                 value = *valuep;
160
161               if (signed_out_of_bounds (value))
162                 errmsg = _("Operand out of range. Must be between -32768 and 32767.");
163               /* Truncate to 16 bits. This is necessary
164                  because cgen will have sign extended *valuep.  */
165               *valuep &= 0xFFFF; 
166 #endif
167             }
168         }
169       else  
170         {
171           /* MS1_OPERAND_IMM16Z.  Parse as an unsigned integer.  */
172           errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, (unsigned long *) valuep);
173
174           if (opindex == (CGEN_OPERAND_TYPE) MS1_OPERAND_IMM16
175               && *valuep >= 0x8000
176               && *valuep <= 0xffff)
177             *valuep -= 0x10000;
178         }
179     }
180
181   return errmsg;
182 }
183
184
185 static const char *
186 parse_dup (CGEN_CPU_DESC cd,
187            const char **strp,
188            int opindex,
189            unsigned long *valuep)
190 {
191   const char *errmsg = NULL;
192
193   if (strncmp (*strp, "dup", 3) == 0 || strncmp (*strp, "DUP", 3) == 0)
194     {
195       *strp += 3;
196       *valuep = 1;
197     }
198   else if (strncmp (*strp, "xx", 2) == 0 || strncmp (*strp, "XX", 2) == 0)
199     {
200       *strp += 2;
201       *valuep = 0;
202     }
203   else
204     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
205
206   return errmsg;
207 }
208
209
210 static const char *
211 parse_ball (CGEN_CPU_DESC cd,
212             const char **strp,
213             int opindex,
214             unsigned long *valuep)
215 {
216   const char *errmsg = NULL;
217
218   if (strncmp (*strp, "all", 3) == 0 || strncmp (*strp, "ALL", 3) == 0)
219     {
220       *strp += 3;
221       *valuep = 1;
222     }
223   else if (strncmp (*strp, "one", 3) == 0 || strncmp (*strp, "ONE", 3) == 0)
224     {
225       *strp += 3;
226       *valuep = 0;
227     }
228   else
229     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
230
231   return errmsg;
232 }
233
234 static const char *
235 parse_xmode (CGEN_CPU_DESC cd,
236              const char **strp,
237              int opindex,
238              unsigned long *valuep)
239 {
240   const char *errmsg = NULL;
241
242   if (strncmp (*strp, "pm", 2) == 0 || strncmp (*strp, "PM", 2) == 0)
243     {
244       *strp += 2;
245       *valuep = 1;
246     }
247   else if (strncmp (*strp, "xm", 2) == 0 || strncmp (*strp, "XM", 2) == 0)
248     {
249       *strp += 2;
250       *valuep = 0;
251     }
252   else
253     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
254
255   return errmsg;
256 }
257
258 static const char *
259 parse_rc (CGEN_CPU_DESC cd,
260           const char **strp,
261           int opindex,
262           unsigned long *valuep)
263 {
264   const char *errmsg = NULL;
265
266   if (strncmp (*strp, "r", 1) == 0 || strncmp (*strp, "R", 1) == 0)
267     {
268       *strp += 1;
269       *valuep = 1;
270     }
271   else if (strncmp (*strp, "c", 1) == 0 || strncmp (*strp, "C", 1) == 0)
272     {
273       *strp += 1;
274       *valuep = 0;
275     }
276   else
277     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
278
279   return errmsg;
280 }
281
282 static const char *
283 parse_cbrb (CGEN_CPU_DESC cd,
284             const char **strp,
285             int opindex,
286             unsigned long *valuep)
287 {
288   const char *errmsg = NULL;
289
290   if (strncmp (*strp, "rb", 2) == 0 || strncmp (*strp, "RB", 2) == 0)
291     {
292       *strp += 2;
293       *valuep = 1;
294     }
295   else if (strncmp (*strp, "cb", 2) == 0 || strncmp (*strp, "CB", 2) == 0)
296     {
297       *strp += 2;
298       *valuep = 0;
299     }
300   else
301     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
302
303   return errmsg;
304 }
305
306 static const char *
307 parse_rbbc (CGEN_CPU_DESC cd,
308             const char **strp,
309             int opindex,
310             unsigned long *valuep)
311 {
312   const char *errmsg = NULL;
313
314   if (strncmp (*strp, "rt", 2) == 0 || strncmp (*strp, "RT", 2) == 0)
315     {
316       *strp += 2;
317       *valuep = 0;
318     }
319   else if (strncmp (*strp, "br1", 3) == 0 || strncmp (*strp, "BR1", 3) == 0)
320     {
321       *strp += 3;
322       *valuep = 1;
323     }
324   else if (strncmp (*strp, "br2", 3) == 0 || strncmp (*strp, "BR2", 3) == 0)
325     {
326       *strp += 3;
327       *valuep = 2;
328     }
329   else if (strncmp (*strp, "cs", 2) == 0 || strncmp (*strp, "CS", 2) == 0)
330     {
331       *strp += 2;
332       *valuep = 3;
333     }
334   else
335     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
336
337   return errmsg;
338 }
339
340 static const char *
341 parse_type (CGEN_CPU_DESC cd,
342             const char **strp,
343             int opindex,
344             unsigned long *valuep)
345 {
346   const char *errmsg = NULL;
347
348   if (strncmp (*strp, "odd", 3) == 0 || strncmp (*strp, "ODD", 3) == 0)
349     {
350       *strp += 3;
351       *valuep = 0;
352     }
353   else if (strncmp (*strp, "even", 4) == 0 || strncmp (*strp, "EVEN", 4) == 0)
354     {
355       *strp += 4;
356       *valuep = 1;
357     }
358   else if (strncmp (*strp, "oe", 2) == 0 || strncmp (*strp, "OE", 2) == 0)
359     {
360       *strp += 2;
361       *valuep = 2;
362     }
363   else
364     errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
365
366  if ((errmsg == NULL) && (*valuep == 3))
367     errmsg = _("invalid operand.  type may have values 0,1,2 only.");
368
369   return errmsg;
370 }
371
372 /* -- dis.c */
373
374 const char * ms1_cgen_parse_operand
375   (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
376
377 /* Main entry point for operand parsing.
378
379    This function is basically just a big switch statement.  Earlier versions
380    used tables to look up the function to use, but
381    - if the table contains both assembler and disassembler functions then
382      the disassembler contains much of the assembler and vice-versa,
383    - there's a lot of inlining possibilities as things grow,
384    - using a switch statement avoids the function call overhead.
385
386    This function could be moved into `parse_insn_normal', but keeping it
387    separate makes clear the interface between `parse_insn_normal' and each of
388    the handlers.  */
389
390 const char *
391 ms1_cgen_parse_operand (CGEN_CPU_DESC cd,
392                            int opindex,
393                            const char ** strp,
394                            CGEN_FIELDS * fields)
395 {
396   const char * errmsg = NULL;
397   /* Used by scalar operands that still need to be parsed.  */
398   long junk ATTRIBUTE_UNUSED;
399
400   switch (opindex)
401     {
402     case MS1_OPERAND_A23 :
403       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_A23, (unsigned long *) (& fields->f_a23));
404       break;
405     case MS1_OPERAND_BALL :
406       errmsg = parse_ball (cd, strp, MS1_OPERAND_BALL, (unsigned long *) (& fields->f_ball));
407       break;
408     case MS1_OPERAND_BALL2 :
409       errmsg = parse_ball (cd, strp, MS1_OPERAND_BALL2, (unsigned long *) (& fields->f_ball2));
410       break;
411     case MS1_OPERAND_BANKADDR :
412       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_BANKADDR, (unsigned long *) (& fields->f_bankaddr));
413       break;
414     case MS1_OPERAND_BRC :
415       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_BRC, (unsigned long *) (& fields->f_brc));
416       break;
417     case MS1_OPERAND_BRC2 :
418       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_BRC2, (unsigned long *) (& fields->f_brc2));
419       break;
420     case MS1_OPERAND_CBRB :
421       errmsg = parse_cbrb (cd, strp, MS1_OPERAND_CBRB, (unsigned long *) (& fields->f_cbrb));
422       break;
423     case MS1_OPERAND_CBS :
424       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_CBS, (unsigned long *) (& fields->f_cbs));
425       break;
426     case MS1_OPERAND_CBX :
427       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_CBX, (unsigned long *) (& fields->f_cbx));
428       break;
429     case MS1_OPERAND_CCB :
430       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_CCB, (unsigned long *) (& fields->f_ccb));
431       break;
432     case MS1_OPERAND_CDB :
433       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_CDB, (unsigned long *) (& fields->f_cdb));
434       break;
435     case MS1_OPERAND_CELL :
436       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_CELL, (unsigned long *) (& fields->f_cell));
437       break;
438     case MS1_OPERAND_COLNUM :
439       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_COLNUM, (unsigned long *) (& fields->f_colnum));
440       break;
441     case MS1_OPERAND_CONTNUM :
442       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_CONTNUM, (unsigned long *) (& fields->f_contnum));
443       break;
444     case MS1_OPERAND_CR :
445       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_CR, (unsigned long *) (& fields->f_cr));
446       break;
447     case MS1_OPERAND_CTXDISP :
448       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_CTXDISP, (unsigned long *) (& fields->f_ctxdisp));
449       break;
450     case MS1_OPERAND_DUP :
451       errmsg = parse_dup (cd, strp, MS1_OPERAND_DUP, (unsigned long *) (& fields->f_dup));
452       break;
453     case MS1_OPERAND_FBDISP :
454       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_FBDISP, (unsigned long *) (& fields->f_fbdisp));
455       break;
456     case MS1_OPERAND_FBINCR :
457       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_FBINCR, (unsigned long *) (& fields->f_fbincr));
458       break;
459     case MS1_OPERAND_FRDR :
460       errmsg = cgen_parse_keyword (cd, strp, & ms1_cgen_opval_h_spr, & fields->f_dr);
461       break;
462     case MS1_OPERAND_FRDRRR :
463       errmsg = cgen_parse_keyword (cd, strp, & ms1_cgen_opval_h_spr, & fields->f_drrr);
464       break;
465     case MS1_OPERAND_FRSR1 :
466       errmsg = cgen_parse_keyword (cd, strp, & ms1_cgen_opval_h_spr, & fields->f_sr1);
467       break;
468     case MS1_OPERAND_FRSR2 :
469       errmsg = cgen_parse_keyword (cd, strp, & ms1_cgen_opval_h_spr, & fields->f_sr2);
470       break;
471     case MS1_OPERAND_ID :
472       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_ID, (unsigned long *) (& fields->f_id));
473       break;
474     case MS1_OPERAND_IMM16 :
475       errmsg = parse_imm16 (cd, strp, MS1_OPERAND_IMM16, (long *) (& fields->f_imm16s));
476       break;
477     case MS1_OPERAND_IMM16O :
478       errmsg = parse_imm16 (cd, strp, MS1_OPERAND_IMM16O, (unsigned long *) (& fields->f_imm16s));
479       break;
480     case MS1_OPERAND_IMM16Z :
481       errmsg = parse_imm16 (cd, strp, MS1_OPERAND_IMM16Z, (unsigned long *) (& fields->f_imm16u));
482       break;
483     case MS1_OPERAND_INCAMT :
484       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_INCAMT, (unsigned long *) (& fields->f_incamt));
485       break;
486     case MS1_OPERAND_INCR :
487       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_INCR, (unsigned long *) (& fields->f_incr));
488       break;
489     case MS1_OPERAND_LENGTH :
490       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_LENGTH, (unsigned long *) (& fields->f_length));
491       break;
492     case MS1_OPERAND_MASK :
493       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_MASK, (unsigned long *) (& fields->f_mask));
494       break;
495     case MS1_OPERAND_MASK1 :
496       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_MASK1, (unsigned long *) (& fields->f_mask1));
497       break;
498     case MS1_OPERAND_MODE :
499       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_MODE, (unsigned long *) (& fields->f_mode));
500       break;
501     case MS1_OPERAND_PERM :
502       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_PERM, (unsigned long *) (& fields->f_perm));
503       break;
504     case MS1_OPERAND_RBBC :
505       errmsg = parse_rbbc (cd, strp, MS1_OPERAND_RBBC, (unsigned long *) (& fields->f_rbbc));
506       break;
507     case MS1_OPERAND_RC :
508       errmsg = parse_rc (cd, strp, MS1_OPERAND_RC, (unsigned long *) (& fields->f_rc));
509       break;
510     case MS1_OPERAND_RC1 :
511       errmsg = parse_rc (cd, strp, MS1_OPERAND_RC1, (unsigned long *) (& fields->f_rc1));
512       break;
513     case MS1_OPERAND_RC2 :
514       errmsg = parse_rc (cd, strp, MS1_OPERAND_RC2, (unsigned long *) (& fields->f_rc2));
515       break;
516     case MS1_OPERAND_RCNUM :
517       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_RCNUM, (unsigned long *) (& fields->f_rcnum));
518       break;
519     case MS1_OPERAND_RDA :
520       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_RDA, (unsigned long *) (& fields->f_rda));
521       break;
522     case MS1_OPERAND_ROWNUM :
523       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_ROWNUM, (unsigned long *) (& fields->f_rownum));
524       break;
525     case MS1_OPERAND_ROWNUM1 :
526       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_ROWNUM1, (unsigned long *) (& fields->f_rownum1));
527       break;
528     case MS1_OPERAND_ROWNUM2 :
529       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_ROWNUM2, (unsigned long *) (& fields->f_rownum2));
530       break;
531     case MS1_OPERAND_SIZE :
532       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_SIZE, (unsigned long *) (& fields->f_size));
533       break;
534     case MS1_OPERAND_TYPE :
535       errmsg = parse_type (cd, strp, MS1_OPERAND_TYPE, (unsigned long *) (& fields->f_type));
536       break;
537     case MS1_OPERAND_WR :
538       errmsg = cgen_parse_unsigned_integer (cd, strp, MS1_OPERAND_WR, (unsigned long *) (& fields->f_wr));
539       break;
540     case MS1_OPERAND_XMODE :
541       errmsg = parse_xmode (cd, strp, MS1_OPERAND_XMODE, (unsigned long *) (& fields->f_xmode));
542       break;
543
544     default :
545       /* xgettext:c-format */
546       fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
547       abort ();
548   }
549
550   return errmsg;
551 }
552
553 cgen_parse_fn * const ms1_cgen_parse_handlers[] = 
554 {
555   parse_insn_normal,
556 };
557
558 void
559 ms1_cgen_init_asm (CGEN_CPU_DESC cd)
560 {
561   ms1_cgen_init_opcode_table (cd);
562   ms1_cgen_init_ibld_table (cd);
563   cd->parse_handlers = & ms1_cgen_parse_handlers[0];
564   cd->parse_operand = ms1_cgen_parse_operand;
565 }
566
567 \f
568
569 /* Regex construction routine.
570
571    This translates an opcode syntax string into a regex string,
572    by replacing any non-character syntax element (such as an
573    opcode) with the pattern '.*'
574
575    It then compiles the regex and stores it in the opcode, for
576    later use by ms1_cgen_assemble_insn
577
578    Returns NULL for success, an error message for failure.  */
579
580 char * 
581 ms1_cgen_build_insn_regex (CGEN_INSN *insn)
582 {  
583   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
584   const char *mnem = CGEN_INSN_MNEMONIC (insn);
585   char rxbuf[CGEN_MAX_RX_ELEMENTS];
586   char *rx = rxbuf;
587   const CGEN_SYNTAX_CHAR_TYPE *syn;
588   int reg_err;
589
590   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
591
592   /* Mnemonics come first in the syntax string.  */
593   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
594     return _("missing mnemonic in syntax string");
595   ++syn;
596
597   /* Generate a case sensitive regular expression that emulates case
598      insensitive matching in the "C" locale.  We cannot generate a case
599      insensitive regular expression because in Turkish locales, 'i' and 'I'
600      are not equal modulo case conversion.  */
601
602   /* Copy the literal mnemonic out of the insn.  */
603   for (; *mnem; mnem++)
604     {
605       char c = *mnem;
606
607       if (ISALPHA (c))
608         {
609           *rx++ = '[';
610           *rx++ = TOLOWER (c);
611           *rx++ = TOUPPER (c);
612           *rx++ = ']';
613         }
614       else
615         *rx++ = c;
616     }
617
618   /* Copy any remaining literals from the syntax string into the rx.  */
619   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
620     {
621       if (CGEN_SYNTAX_CHAR_P (* syn)) 
622         {
623           char c = CGEN_SYNTAX_CHAR (* syn);
624
625           switch (c) 
626             {
627               /* Escape any regex metacharacters in the syntax.  */
628             case '.': case '[': case '\\': 
629             case '*': case '^': case '$': 
630
631 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
632             case '?': case '{': case '}': 
633             case '(': case ')': case '*':
634             case '|': case '+': case ']':
635 #endif
636               *rx++ = '\\';
637               *rx++ = c;
638               break;
639
640             default:
641               if (ISALPHA (c))
642                 {
643                   *rx++ = '[';
644                   *rx++ = TOLOWER (c);
645                   *rx++ = TOUPPER (c);
646                   *rx++ = ']';
647                 }
648               else
649                 *rx++ = c;
650               break;
651             }
652         }
653       else
654         {
655           /* Replace non-syntax fields with globs.  */
656           *rx++ = '.';
657           *rx++ = '*';
658         }
659     }
660
661   /* Trailing whitespace ok.  */
662   * rx++ = '['; 
663   * rx++ = ' '; 
664   * rx++ = '\t'; 
665   * rx++ = ']'; 
666   * rx++ = '*'; 
667
668   /* But anchor it after that.  */
669   * rx++ = '$'; 
670   * rx = '\0';
671
672   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
673   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
674
675   if (reg_err == 0) 
676     return NULL;
677   else
678     {
679       static char msg[80];
680
681       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
682       regfree ((regex_t *) CGEN_INSN_RX (insn));
683       free (CGEN_INSN_RX (insn));
684       (CGEN_INSN_RX (insn)) = NULL;
685       return msg;
686     }
687 }
688
689 \f
690 /* Default insn parser.
691
692    The syntax string is scanned and operands are parsed and stored in FIELDS.
693    Relocs are queued as we go via other callbacks.
694
695    ??? Note that this is currently an all-or-nothing parser.  If we fail to
696    parse the instruction, we return 0 and the caller will start over from
697    the beginning.  Backtracking will be necessary in parsing subexpressions,
698    but that can be handled there.  Not handling backtracking here may get
699    expensive in the case of the m68k.  Deal with later.
700
701    Returns NULL for success, an error message for failure.  */
702
703 static const char *
704 parse_insn_normal (CGEN_CPU_DESC cd,
705                    const CGEN_INSN *insn,
706                    const char **strp,
707                    CGEN_FIELDS *fields)
708 {
709   /* ??? Runtime added insns not handled yet.  */
710   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
711   const char *str = *strp;
712   const char *errmsg;
713   const char *p;
714   const CGEN_SYNTAX_CHAR_TYPE * syn;
715 #ifdef CGEN_MNEMONIC_OPERANDS
716   /* FIXME: wip */
717   int past_opcode_p;
718 #endif
719
720   /* For now we assume the mnemonic is first (there are no leading operands).
721      We can parse it without needing to set up operand parsing.
722      GAS's input scrubber will ensure mnemonics are lowercase, but we may
723      not be called from GAS.  */
724   p = CGEN_INSN_MNEMONIC (insn);
725   while (*p && TOLOWER (*p) == TOLOWER (*str))
726     ++p, ++str;
727
728   if (* p)
729     return _("unrecognized instruction");
730
731 #ifndef CGEN_MNEMONIC_OPERANDS
732   if (* str && ! ISSPACE (* str))
733     return _("unrecognized instruction");
734 #endif
735
736   CGEN_INIT_PARSE (cd);
737   cgen_init_parse_operand (cd);
738 #ifdef CGEN_MNEMONIC_OPERANDS
739   past_opcode_p = 0;
740 #endif
741
742   /* We don't check for (*str != '\0') here because we want to parse
743      any trailing fake arguments in the syntax string.  */
744   syn = CGEN_SYNTAX_STRING (syntax);
745
746   /* Mnemonics come first for now, ensure valid string.  */
747   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
748     abort ();
749
750   ++syn;
751
752   while (* syn != 0)
753     {
754       /* Non operand chars must match exactly.  */
755       if (CGEN_SYNTAX_CHAR_P (* syn))
756         {
757           /* FIXME: While we allow for non-GAS callers above, we assume the
758              first char after the mnemonic part is a space.  */
759           /* FIXME: We also take inappropriate advantage of the fact that
760              GAS's input scrubber will remove extraneous blanks.  */
761           if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
762             {
763 #ifdef CGEN_MNEMONIC_OPERANDS
764               if (CGEN_SYNTAX_CHAR(* syn) == ' ')
765                 past_opcode_p = 1;
766 #endif
767               ++ syn;
768               ++ str;
769             }
770           else if (*str)
771             {
772               /* Syntax char didn't match.  Can't be this insn.  */
773               static char msg [80];
774
775               /* xgettext:c-format */
776               sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
777                        CGEN_SYNTAX_CHAR(*syn), *str);
778               return msg;
779             }
780           else
781             {
782               /* Ran out of input.  */
783               static char msg [80];
784
785               /* xgettext:c-format */
786               sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
787                        CGEN_SYNTAX_CHAR(*syn));
788               return msg;
789             }
790           continue;
791         }
792
793       /* We have an operand of some sort.  */
794       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
795                                           &str, fields);
796       if (errmsg)
797         return errmsg;
798
799       /* Done with this operand, continue with next one.  */
800       ++ syn;
801     }
802
803   /* If we're at the end of the syntax string, we're done.  */
804   if (* syn == 0)
805     {
806       /* FIXME: For the moment we assume a valid `str' can only contain
807          blanks now.  IE: We needn't try again with a longer version of
808          the insn and it is assumed that longer versions of insns appear
809          before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
810       while (ISSPACE (* str))
811         ++ str;
812
813       if (* str != '\0')
814         return _("junk at end of line"); /* FIXME: would like to include `str' */
815
816       return NULL;
817     }
818
819   /* We couldn't parse it.  */
820   return _("unrecognized instruction");
821 }
822 \f
823 /* Main entry point.
824    This routine is called for each instruction to be assembled.
825    STR points to the insn to be assembled.
826    We assume all necessary tables have been initialized.
827    The assembled instruction, less any fixups, is stored in BUF.
828    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
829    still needs to be converted to target byte order, otherwise BUF is an array
830    of bytes in target byte order.
831    The result is a pointer to the insn's entry in the opcode table,
832    or NULL if an error occured (an error message will have already been
833    printed).
834
835    Note that when processing (non-alias) macro-insns,
836    this function recurses.
837
838    ??? It's possible to make this cpu-independent.
839    One would have to deal with a few minor things.
840    At this point in time doing so would be more of a curiosity than useful
841    [for example this file isn't _that_ big], but keeping the possibility in
842    mind helps keep the design clean.  */
843
844 const CGEN_INSN *
845 ms1_cgen_assemble_insn (CGEN_CPU_DESC cd,
846                            const char *str,
847                            CGEN_FIELDS *fields,
848                            CGEN_INSN_BYTES_PTR buf,
849                            char **errmsg)
850 {
851   const char *start;
852   CGEN_INSN_LIST *ilist;
853   const char *parse_errmsg = NULL;
854   const char *insert_errmsg = NULL;
855   int recognized_mnemonic = 0;
856
857   /* Skip leading white space.  */
858   while (ISSPACE (* str))
859     ++ str;
860
861   /* The instructions are stored in hashed lists.
862      Get the first in the list.  */
863   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
864
865   /* Keep looking until we find a match.  */
866   start = str;
867   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
868     {
869       const CGEN_INSN *insn = ilist->insn;
870       recognized_mnemonic = 1;
871
872 #ifdef CGEN_VALIDATE_INSN_SUPPORTED 
873       /* Not usually needed as unsupported opcodes
874          shouldn't be in the hash lists.  */
875       /* Is this insn supported by the selected cpu?  */
876       if (! ms1_cgen_insn_supported (cd, insn))
877         continue;
878 #endif
879       /* If the RELAXED attribute is set, this is an insn that shouldn't be
880          chosen immediately.  Instead, it is used during assembler/linker
881          relaxation if possible.  */
882       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
883         continue;
884
885       str = start;
886
887       /* Skip this insn if str doesn't look right lexically.  */
888       if (CGEN_INSN_RX (insn) != NULL &&
889           regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
890         continue;
891
892       /* Allow parse/insert handlers to obtain length of insn.  */
893       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
894
895       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
896       if (parse_errmsg != NULL)
897         continue;
898
899       /* ??? 0 is passed for `pc'.  */
900       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
901                                                  (bfd_vma) 0);
902       if (insert_errmsg != NULL)
903         continue;
904
905       /* It is up to the caller to actually output the insn and any
906          queued relocs.  */
907       return insn;
908     }
909
910   {
911     static char errbuf[150];
912 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
913     const char *tmp_errmsg;
914
915     /* If requesting verbose error messages, use insert_errmsg.
916        Failing that, use parse_errmsg.  */
917     tmp_errmsg = (insert_errmsg ? insert_errmsg :
918                   parse_errmsg ? parse_errmsg :
919                   recognized_mnemonic ?
920                   _("unrecognized form of instruction") :
921                   _("unrecognized instruction"));
922
923     if (strlen (start) > 50)
924       /* xgettext:c-format */
925       sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
926     else 
927       /* xgettext:c-format */
928       sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
929 #else
930     if (strlen (start) > 50)
931       /* xgettext:c-format */
932       sprintf (errbuf, _("bad instruction `%.50s...'"), start);
933     else 
934       /* xgettext:c-format */
935       sprintf (errbuf, _("bad instruction `%.50s'"), start);
936 #endif
937       
938     *errmsg = errbuf;
939     return NULL;
940   }
941 }