OSDN Git Service

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