Fix compile time warning messages
[external/binutils.git] / opcodes / ip2k-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 Free Software Foundation, Inc.
8
9 This file is part of the GNU Binutils and GDB, the GNU debugger.
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software Foundation, Inc.,
23 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
24
25 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
26    Keep that in mind.  */
27
28 #include "sysdep.h"
29 #include <stdio.h>
30 #include "ansidecl.h"
31 #include "bfd.h"
32 #include "symcat.h"
33 #include "ip2k-desc.h"
34 #include "ip2k-opc.h"
35 #include "opintl.h"
36 #include "xregex.h"
37 #include "libiberty.h"
38 #include "safe-ctype.h"
39
40 #undef  min
41 #define min(a,b) ((a) < (b) ? (a) : (b))
42 #undef  max
43 #define max(a,b) ((a) > (b) ? (a) : (b))
44
45 static const char * parse_insn_normal
46   (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
47 \f
48 /* -- assembler routines inserted here.  */
49
50 /* -- asm.c */
51
52 #define PARSE_FUNC_DECL(name) \
53 static const char *name PARAMS ((CGEN_CPU_DESC, const char **, int, long *))
54
55 PARSE_FUNC_DECL (parse_fr);
56 PARSE_FUNC_DECL (parse_addr16);
57 PARSE_FUNC_DECL (parse_addr16_cjp);
58 PARSE_FUNC_DECL (parse_lit8);
59 PARSE_FUNC_DECL (parse_bit3);
60
61
62 static const char *
63 parse_fr (cd, strp, opindex, valuep)
64      CGEN_CPU_DESC cd;
65      const char **strp;
66      int opindex;
67      long *valuep;
68 {
69   const char *errmsg;
70   const char *old_strp;
71   char *afteroffset; 
72   enum cgen_parse_operand_result result_type;
73   bfd_vma value;
74   extern CGEN_KEYWORD ip2k_cgen_opval_register_names;
75   bfd_vma tempvalue;
76
77   old_strp = *strp;
78   afteroffset = NULL; 
79
80
81   /* Check here to see if you're about to try parsing a w as the first arg */
82   /* and return an error if you are.                                       */
83   if ( (strncmp(*strp,"w",1)==0) || (strncmp(*strp,"W",1)==0) )
84     {
85       (*strp)++;
86
87       if ( (strncmp(*strp,",",1)==0) || ISSPACE(**strp) )
88         {
89           /* We've been passed a w.  Return with an error message so that  */
90           /* cgen will try the next parsing option.                        */
91           errmsg = _("W keyword invalid in FR operand slot.");
92           return errmsg;
93         }
94       *strp = old_strp;
95     }
96
97
98   /* Attempt parse as register keyword. */
99   /* old_strp = *strp; */
100
101   errmsg = cgen_parse_keyword (cd, strp, & ip2k_cgen_opval_register_names,
102                                valuep);
103   if ( *strp != NULL )
104     if (errmsg == NULL)
105       return errmsg;
106
107   /* Attempt to parse for "(IP)" */
108   afteroffset = strstr(*strp,"(IP)");
109
110   if ( afteroffset == NULL)
111     {
112       /* Make sure it's not in lower case */
113       afteroffset = strstr(*strp,"(ip)");
114     }
115
116   if ( afteroffset != NULL )
117     {
118       if ( afteroffset != *strp )
119         {
120           /* Invalid offset present.*/
121           errmsg = _("offset(IP) is not a valid form");
122           return errmsg;
123         }
124       else
125         {
126           *strp += 4; 
127           *valuep = 0;
128           errmsg = NULL;
129           return errmsg;
130         }
131     }
132
133   /* Attempt to parse for DP. ex: mov w, offset(DP)  */
134   /*                              mov offset(DP),w   */
135
136   /* Try parsing it as an address and see what comes back */
137
138   afteroffset = strstr(*strp,"(DP)");
139
140   if ( afteroffset == NULL)
141     {
142       /* Maybe it's in lower case */
143       afteroffset = strstr(*strp,"(dp)");
144     }
145
146   if ( afteroffset != NULL )
147     {
148       if ( afteroffset == *strp )
149         {
150           /* No offset present. Use 0 by default. */
151           tempvalue = 0;
152           errmsg = NULL;
153         }
154       else
155         {
156           errmsg = cgen_parse_address (cd, strp, opindex,
157                                        BFD_RELOC_IP2K_FR_OFFSET,
158                                        & result_type, & tempvalue);
159         }
160
161       if (errmsg == NULL)
162         {
163           if (tempvalue <= 127)
164             {
165               /* Value is ok.  Fix up the first 2 bits and return */       
166               *valuep = 0x0100 | tempvalue;
167               *strp += 4; /* skip over the (DP) in *strp */
168               return errmsg;
169             }
170           else
171             {
172               /* Found something there in front of (DP) but it's out
173                  of range. */
174               errmsg = _("(DP) offset out of range.");
175               return errmsg;
176             }
177         }
178     }
179
180
181   /* Attempt to parse for SP. ex: mov w, offset(SP)  */
182   /*                              mov offset(SP), w  */
183
184
185   afteroffset = strstr(*strp,"(SP)");
186
187   if (afteroffset == NULL)
188     {
189       /* Maybe it's in lower case. */
190       afteroffset = strstr(*strp, "(sp)");
191     }
192
193   if ( afteroffset != NULL )
194     {
195       if ( afteroffset ==  *strp )
196         {
197           /* No offset present. Use 0 by default. */
198           tempvalue = 0;
199           errmsg = NULL;
200         }
201       else
202         {
203           errmsg = cgen_parse_address (cd, strp, opindex,
204                                        BFD_RELOC_IP2K_FR_OFFSET,
205                                        & result_type, & tempvalue);
206         }
207       if (errmsg == NULL)
208         {
209           if (tempvalue <= 127)
210             {
211               /* Value is ok.  Fix up the first 2 bits and return */
212               *valuep = 0x0180 | tempvalue;
213               *strp += 4; /* skip over the (SP) in *strp */
214               return errmsg;
215             }
216           else
217             {
218               /* Found something there in front of (SP) but it's out
219                  of range. */
220               errmsg = _("(SP) offset out of range.");
221               return errmsg;
222             }
223         
224         }
225     }
226
227
228   /* Attempt to parse as an address. */
229   *strp = old_strp;
230   errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_IP2K_FR9,
231                                & result_type, & value);
232   if (errmsg == NULL)
233     {
234       *valuep = value;
235
236       /* if a parenthesis is found, warn about invalid form */
237
238       if (**strp == '(')
239         {
240           errmsg = _("illegal use of parentheses");
241         }
242       /* if a numeric value is specified, ensure that it is between
243          1 and 255 */
244       else if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
245         {
246           if (value < 0x1 || value > 0xff)
247             errmsg = _("operand out of range (not between 1 and 255)");
248         }
249     }
250   return errmsg;
251 }
252
253 static const char *
254 parse_addr16 (cd, strp, opindex, valuep)
255      CGEN_CPU_DESC cd;
256      const char **strp;
257      int opindex;
258      long *valuep;
259 {
260   const char *errmsg;
261   enum cgen_parse_operand_result result_type;
262   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
263   bfd_vma value;
264
265   if ( opindex == (CGEN_OPERAND_TYPE)IP2K_OPERAND_ADDR16H )
266     code = BFD_RELOC_IP2K_HI8DATA;
267   else if ( opindex == (CGEN_OPERAND_TYPE)IP2K_OPERAND_ADDR16L )
268     code = BFD_RELOC_IP2K_LO8DATA;
269   else
270     {
271       /* Something is very wrong. opindex has to be one of the above. */
272       errmsg = _("parse_addr16: invalid opindex.");
273       return errmsg;
274     }
275   
276   errmsg = cgen_parse_address (cd, strp, opindex, code,
277                                & result_type, & value);
278   if (errmsg == NULL)
279     {
280       /* We either have a relocation or a number now. */
281       if ( result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER )
282         {
283           /* We got a number back. */
284           if ( code == BFD_RELOC_IP2K_HI8DATA )
285             value >>= 8;
286           else    /* code = BFD_RELOC_IP2K_LOW8DATA */
287             value &= 0x00FF;
288         }   
289       *valuep = value;
290     }
291
292   return errmsg;
293 }
294
295
296 static const char *
297 parse_addr16_cjp (cd, strp, opindex, valuep)
298      CGEN_CPU_DESC cd;
299      const char **strp;
300      int opindex;
301      long *valuep;
302 {
303   const char *errmsg;
304   enum cgen_parse_operand_result result_type;
305   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
306   bfd_vma value;
307  
308   if ( opindex == (CGEN_OPERAND_TYPE)IP2K_OPERAND_ADDR16CJP )
309     code = BFD_RELOC_IP2K_ADDR16CJP;
310   else if ( opindex == (CGEN_OPERAND_TYPE)IP2K_OPERAND_ADDR16P )
311     code = BFD_RELOC_IP2K_PAGE3;
312
313   errmsg = cgen_parse_address (cd, strp, opindex, code,
314                                & result_type, & value);
315   if (errmsg == NULL)
316     {
317       if ( result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER )
318         {
319           if ( (value & 0x1) == 0)  /* If the address is even .... */
320             {
321               if ( opindex == (CGEN_OPERAND_TYPE)IP2K_OPERAND_ADDR16CJP )
322                 *valuep = (value >> 1) & 0x1FFF;  /* Should mask be 1FFF? */
323               else if ( opindex == (CGEN_OPERAND_TYPE)IP2K_OPERAND_ADDR16P )
324                 *valuep = (value >> 14) & 0x7;
325             }
326           else
327             errmsg = _("Byte address required. - must be even.");
328         }
329       else if ( result_type == CGEN_PARSE_OPERAND_RESULT_QUEUED )
330         {
331           /* This will happen for things like (s2-s1) where s2 and s1
332              are labels.  */
333           *valuep = value;
334         }
335       else 
336         errmsg = _("cgen_parse_address returned a symbol. Literal required.");
337     }
338   return errmsg; 
339 }
340
341
342 static const char *
343 parse_lit8 (cd, strp, opindex, valuep)
344      CGEN_CPU_DESC cd;
345      const char **strp;
346      int opindex;
347      long *valuep;
348 {
349   const char *errmsg;
350   enum cgen_parse_operand_result result_type;
351   bfd_reloc_code_real_type code = BFD_RELOC_NONE;
352   bfd_vma value;
353
354   /* Parse %OP relocating operators. */
355   if (strncmp (*strp, "%bank", 5) == 0)
356     {
357       *strp += 5;
358       code = BFD_RELOC_IP2K_BANK;
359     }
360   else if (strncmp (*strp, "%lo8data", 8) == 0)
361     {
362       *strp += 8;
363       code = BFD_RELOC_IP2K_LO8DATA;
364     }
365   else if (strncmp (*strp, "%hi8data", 8) == 0)
366     {
367       *strp += 8;
368       code = BFD_RELOC_IP2K_HI8DATA;
369     }
370   else if (strncmp (*strp, "%ex8data", 8) == 0)
371     {
372       *strp += 8;
373       code = BFD_RELOC_IP2K_EX8DATA;
374     }
375   else if (strncmp (*strp, "%lo8insn", 8) == 0)
376     {
377       *strp += 8;
378       code = BFD_RELOC_IP2K_LO8INSN;
379     }
380   else if (strncmp (*strp, "%hi8insn", 8) == 0)
381     {
382       *strp += 8;
383       code = BFD_RELOC_IP2K_HI8INSN;
384     }
385   
386
387   /* Parse %op operand.  */
388   if (code != BFD_RELOC_NONE)
389     {
390       errmsg = cgen_parse_address (cd, strp, opindex, code, 
391                                    & result_type, & value);
392       if ((errmsg == NULL) &&
393           (result_type != CGEN_PARSE_OPERAND_RESULT_QUEUED))
394         errmsg = _("%operator operand is not a symbol");
395
396       *valuep = value;
397     }
398   /* Parse as a number.  */
399   else
400     {
401       errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep);
402
403       /* Truncate to eight bits to accept both signed and unsigned input. */
404       if (errmsg == NULL)
405         *valuep &= 0xFF;
406     }
407
408   return errmsg;
409 }
410
411 static const char *
412 parse_bit3 (cd, strp, opindex, valuep)
413      CGEN_CPU_DESC cd;
414      const char **strp;
415      int opindex;
416      long *valuep;
417 {
418   const char *errmsg;
419   char mode = 0;
420   long count = 0;
421   unsigned long value;
422
423   if (strncmp (*strp, "%bit", 4) == 0)
424     {
425       *strp += 4;
426       mode = 1;
427     }
428   else if (strncmp (*strp, "%msbbit", 7) == 0)
429     {
430       *strp += 7;
431       mode = 1;
432     }
433   else if (strncmp (*strp, "%lsbbit", 7) == 0)
434     {
435       *strp += 7;
436       mode = 2;
437     }
438
439   errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep);
440   if (errmsg)
441     return errmsg;
442
443   if (mode)
444     {
445       value = (unsigned long) *valuep;
446       if (value == 0)
447         {
448           errmsg = _("Attempt to find bit index of 0");
449           return errmsg;
450         }
451     
452       if (mode == 1)
453         {
454           count = 31;
455           while ((value & 0x80000000) == 0)
456             {
457               count--;
458               value <<= 1;
459             }
460         }
461       else if (mode == 2)
462         {
463           count = 0;
464           while ((value & 0x00000001) == 0)
465             {
466               count++;
467               value >>= 1;
468             }
469         }
470     
471       *valuep = count;
472     }
473
474   return errmsg;
475 }
476
477
478 /* -- dis.c */
479
480 const char * ip2k_cgen_parse_operand
481   PARAMS ((CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *));
482
483 /* Main entry point for operand parsing.
484
485    This function is basically just a big switch statement.  Earlier versions
486    used tables to look up the function to use, but
487    - if the table contains both assembler and disassembler functions then
488      the disassembler contains much of the assembler and vice-versa,
489    - there's a lot of inlining possibilities as things grow,
490    - using a switch statement avoids the function call overhead.
491
492    This function could be moved into `parse_insn_normal', but keeping it
493    separate makes clear the interface between `parse_insn_normal' and each of
494    the handlers.  */
495
496 const char *
497 ip2k_cgen_parse_operand (cd, opindex, strp, fields)
498      CGEN_CPU_DESC cd;
499      int opindex;
500      const char ** strp;
501      CGEN_FIELDS * fields;
502 {
503   const char * errmsg = NULL;
504   /* Used by scalar operands that still need to be parsed.  */
505   long junk ATTRIBUTE_UNUSED;
506
507   switch (opindex)
508     {
509     case IP2K_OPERAND_ADDR16CJP :
510       errmsg = parse_addr16_cjp (cd, strp, IP2K_OPERAND_ADDR16CJP, &fields->f_addr16cjp);
511       break;
512     case IP2K_OPERAND_ADDR16H :
513       errmsg = parse_addr16 (cd, strp, IP2K_OPERAND_ADDR16H, &fields->f_imm8);
514       break;
515     case IP2K_OPERAND_ADDR16L :
516       errmsg = parse_addr16 (cd, strp, IP2K_OPERAND_ADDR16L, &fields->f_imm8);
517       break;
518     case IP2K_OPERAND_ADDR16P :
519       errmsg = parse_addr16_cjp (cd, strp, IP2K_OPERAND_ADDR16P, &fields->f_page3);
520       break;
521     case IP2K_OPERAND_BITNO :
522       errmsg = parse_bit3 (cd, strp, IP2K_OPERAND_BITNO, &fields->f_bitno);
523       break;
524     case IP2K_OPERAND_CBIT :
525       errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_CBIT, &junk);
526       break;
527     case IP2K_OPERAND_DCBIT :
528       errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_DCBIT, &junk);
529       break;
530     case IP2K_OPERAND_FR :
531       errmsg = parse_fr (cd, strp, IP2K_OPERAND_FR, &fields->f_reg);
532       break;
533     case IP2K_OPERAND_LIT8 :
534       errmsg = parse_lit8 (cd, strp, IP2K_OPERAND_LIT8, &fields->f_imm8);
535       break;
536     case IP2K_OPERAND_PABITS :
537       errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_PABITS, &junk);
538       break;
539     case IP2K_OPERAND_RETI3 :
540       errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_RETI3, &fields->f_reti3);
541       break;
542     case IP2K_OPERAND_ZBIT :
543       errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_ZBIT, &junk);
544       break;
545
546     default :
547       /* xgettext:c-format */
548       fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
549       abort ();
550   }
551
552   return errmsg;
553 }
554
555 cgen_parse_fn * const ip2k_cgen_parse_handlers[] = 
556 {
557   parse_insn_normal,
558 };
559
560 void
561 ip2k_cgen_init_asm (cd)
562      CGEN_CPU_DESC cd;
563 {
564   ip2k_cgen_init_opcode_table (cd);
565   ip2k_cgen_init_ibld_table (cd);
566   cd->parse_handlers = & ip2k_cgen_parse_handlers[0];
567   cd->parse_operand = ip2k_cgen_parse_operand;
568 }
569
570 \f
571
572 /* Regex construction routine.
573
574    This translates an opcode syntax string into a regex string,
575    by replacing any non-character syntax element (such as an
576    opcode) with the pattern '.*'
577
578    It then compiles the regex and stores it in the opcode, for
579    later use by ip2k_cgen_assemble_insn
580
581    Returns NULL for success, an error message for failure.  */
582
583 char * 
584 ip2k_cgen_build_insn_regex (CGEN_INSN *insn)
585 {  
586   CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
587   const char *mnem = CGEN_INSN_MNEMONIC (insn);
588   char rxbuf[CGEN_MAX_RX_ELEMENTS];
589   char *rx = rxbuf;
590   const CGEN_SYNTAX_CHAR_TYPE *syn;
591   int reg_err;
592
593   syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
594
595   /* Mnemonics come first in the syntax string.  */
596   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
597     return _("missing mnemonic in syntax string");
598   ++syn;
599
600   /* Generate a case sensitive regular expression that emulates case
601      insensitive matching in the "C" locale.  We cannot generate a case
602      insensitive regular expression because in Turkish locales, 'i' and 'I'
603      are not equal modulo case conversion.  */
604
605   /* Copy the literal mnemonic out of the insn.  */
606   for (; *mnem; mnem++)
607     {
608       char c = *mnem;
609
610       if (ISALPHA (c))
611         {
612           *rx++ = '[';
613           *rx++ = TOLOWER (c);
614           *rx++ = TOUPPER (c);
615           *rx++ = ']';
616         }
617       else
618         *rx++ = c;
619     }
620
621   /* Copy any remaining literals from the syntax string into the rx.  */
622   for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
623     {
624       if (CGEN_SYNTAX_CHAR_P (* syn)) 
625         {
626           char c = CGEN_SYNTAX_CHAR (* syn);
627
628           switch (c) 
629             {
630               /* Escape any regex metacharacters in the syntax.  */
631             case '.': case '[': case '\\': 
632             case '*': case '^': case '$': 
633
634 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
635             case '?': case '{': case '}': 
636             case '(': case ')': case '*':
637             case '|': case '+': case ']':
638 #endif
639               *rx++ = '\\';
640               *rx++ = c;
641               break;
642
643             default:
644               if (ISALPHA (c))
645                 {
646                   *rx++ = '[';
647                   *rx++ = TOLOWER (c);
648                   *rx++ = TOUPPER (c);
649                   *rx++ = ']';
650                 }
651               else
652                 *rx++ = c;
653               break;
654             }
655         }
656       else
657         {
658           /* Replace non-syntax fields with globs.  */
659           *rx++ = '.';
660           *rx++ = '*';
661         }
662     }
663
664   /* Trailing whitespace ok.  */
665   * rx++ = '['; 
666   * rx++ = ' '; 
667   * rx++ = '\t'; 
668   * rx++ = ']'; 
669   * rx++ = '*'; 
670
671   /* But anchor it after that.  */
672   * rx++ = '$'; 
673   * rx = '\0';
674
675   CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
676   reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
677
678   if (reg_err == 0) 
679     return NULL;
680   else
681     {
682       static char msg[80];
683
684       regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
685       regfree ((regex_t *) CGEN_INSN_RX (insn));
686       free (CGEN_INSN_RX (insn));
687       (CGEN_INSN_RX (insn)) = NULL;
688       return msg;
689     }
690 }
691
692 \f
693 /* Default insn parser.
694
695    The syntax string is scanned and operands are parsed and stored in FIELDS.
696    Relocs are queued as we go via other callbacks.
697
698    ??? Note that this is currently an all-or-nothing parser.  If we fail to
699    parse the instruction, we return 0 and the caller will start over from
700    the beginning.  Backtracking will be necessary in parsing subexpressions,
701    but that can be handled there.  Not handling backtracking here may get
702    expensive in the case of the m68k.  Deal with later.
703
704    Returns NULL for success, an error message for failure.  */
705
706 static const char *
707 parse_insn_normal (CGEN_CPU_DESC cd,
708                    const CGEN_INSN *insn,
709                    const char **strp,
710                    CGEN_FIELDS *fields)
711 {
712   /* ??? Runtime added insns not handled yet.  */
713   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
714   const char *str = *strp;
715   const char *errmsg;
716   const char *p;
717   const CGEN_SYNTAX_CHAR_TYPE * syn;
718 #ifdef CGEN_MNEMONIC_OPERANDS
719   /* FIXME: wip */
720   int past_opcode_p;
721 #endif
722
723   /* For now we assume the mnemonic is first (there are no leading operands).
724      We can parse it without needing to set up operand parsing.
725      GAS's input scrubber will ensure mnemonics are lowercase, but we may
726      not be called from GAS.  */
727   p = CGEN_INSN_MNEMONIC (insn);
728   while (*p && TOLOWER (*p) == TOLOWER (*str))
729     ++p, ++str;
730
731   if (* p)
732     return _("unrecognized instruction");
733
734 #ifndef CGEN_MNEMONIC_OPERANDS
735   if (* str && ! ISSPACE (* str))
736     return _("unrecognized instruction");
737 #endif
738
739   CGEN_INIT_PARSE (cd);
740   cgen_init_parse_operand (cd);
741 #ifdef CGEN_MNEMONIC_OPERANDS
742   past_opcode_p = 0;
743 #endif
744
745   /* We don't check for (*str != '\0') here because we want to parse
746      any trailing fake arguments in the syntax string.  */
747   syn = CGEN_SYNTAX_STRING (syntax);
748
749   /* Mnemonics come first for now, ensure valid string.  */
750   if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
751     abort ();
752
753   ++syn;
754
755   while (* syn != 0)
756     {
757       /* Non operand chars must match exactly.  */
758       if (CGEN_SYNTAX_CHAR_P (* syn))
759         {
760           /* FIXME: While we allow for non-GAS callers above, we assume the
761              first char after the mnemonic part is a space.  */
762           /* FIXME: We also take inappropriate advantage of the fact that
763              GAS's input scrubber will remove extraneous blanks.  */
764           if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
765             {
766 #ifdef CGEN_MNEMONIC_OPERANDS
767               if (CGEN_SYNTAX_CHAR(* syn) == ' ')
768                 past_opcode_p = 1;
769 #endif
770               ++ syn;
771               ++ str;
772             }
773           else if (*str)
774             {
775               /* Syntax char didn't match.  Can't be this insn.  */
776               static char msg [80];
777
778               /* xgettext:c-format */
779               sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
780                        CGEN_SYNTAX_CHAR(*syn), *str);
781               return msg;
782             }
783           else
784             {
785               /* Ran out of input.  */
786               static char msg [80];
787
788               /* xgettext:c-format */
789               sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
790                        CGEN_SYNTAX_CHAR(*syn));
791               return msg;
792             }
793           continue;
794         }
795
796       /* We have an operand of some sort.  */
797       errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
798                                           &str, fields);
799       if (errmsg)
800         return errmsg;
801
802       /* Done with this operand, continue with next one.  */
803       ++ syn;
804     }
805
806   /* If we're at the end of the syntax string, we're done.  */
807   if (* syn == 0)
808     {
809       /* FIXME: For the moment we assume a valid `str' can only contain
810          blanks now.  IE: We needn't try again with a longer version of
811          the insn and it is assumed that longer versions of insns appear
812          before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3).  */
813       while (ISSPACE (* str))
814         ++ str;
815
816       if (* str != '\0')
817         return _("junk at end of line"); /* FIXME: would like to include `str' */
818
819       return NULL;
820     }
821
822   /* We couldn't parse it.  */
823   return _("unrecognized instruction");
824 }
825 \f
826 /* Main entry point.
827    This routine is called for each instruction to be assembled.
828    STR points to the insn to be assembled.
829    We assume all necessary tables have been initialized.
830    The assembled instruction, less any fixups, is stored in BUF.
831    Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
832    still needs to be converted to target byte order, otherwise BUF is an array
833    of bytes in target byte order.
834    The result is a pointer to the insn's entry in the opcode table,
835    or NULL if an error occured (an error message will have already been
836    printed).
837
838    Note that when processing (non-alias) macro-insns,
839    this function recurses.
840
841    ??? It's possible to make this cpu-independent.
842    One would have to deal with a few minor things.
843    At this point in time doing so would be more of a curiosity than useful
844    [for example this file isn't _that_ big], but keeping the possibility in
845    mind helps keep the design clean.  */
846
847 const CGEN_INSN *
848 ip2k_cgen_assemble_insn (CGEN_CPU_DESC cd,
849                            const char *str,
850                            CGEN_FIELDS *fields,
851                            CGEN_INSN_BYTES_PTR buf,
852                            char **errmsg)
853 {
854   const char *start;
855   CGEN_INSN_LIST *ilist;
856   const char *parse_errmsg = NULL;
857   const char *insert_errmsg = NULL;
858   int recognized_mnemonic = 0;
859
860   /* Skip leading white space.  */
861   while (ISSPACE (* str))
862     ++ str;
863
864   /* The instructions are stored in hashed lists.
865      Get the first in the list.  */
866   ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
867
868   /* Keep looking until we find a match.  */
869   start = str;
870   for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
871     {
872       const CGEN_INSN *insn = ilist->insn;
873       recognized_mnemonic = 1;
874
875 #ifdef CGEN_VALIDATE_INSN_SUPPORTED 
876       /* Not usually needed as unsupported opcodes
877          shouldn't be in the hash lists.  */
878       /* Is this insn supported by the selected cpu?  */
879       if (! ip2k_cgen_insn_supported (cd, insn))
880         continue;
881 #endif
882       /* If the RELAXED attribute is set, this is an insn that shouldn't be
883          chosen immediately.  Instead, it is used during assembler/linker
884          relaxation if possible.  */
885       if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
886         continue;
887
888       str = start;
889
890       /* Skip this insn if str doesn't look right lexically.  */
891       if (CGEN_INSN_RX (insn) != NULL &&
892           regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
893         continue;
894
895       /* Allow parse/insert handlers to obtain length of insn.  */
896       CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
897
898       parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
899       if (parse_errmsg != NULL)
900         continue;
901
902       /* ??? 0 is passed for `pc'.  */
903       insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
904                                                  (bfd_vma) 0);
905       if (insert_errmsg != NULL)
906         continue;
907
908       /* It is up to the caller to actually output the insn and any
909          queued relocs.  */
910       return insn;
911     }
912
913   {
914     static char errbuf[150];
915 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
916     const char *tmp_errmsg;
917
918     /* If requesting verbose error messages, use insert_errmsg.
919        Failing that, use parse_errmsg.  */
920     tmp_errmsg = (insert_errmsg ? insert_errmsg :
921                   parse_errmsg ? parse_errmsg :
922                   recognized_mnemonic ?
923                   _("unrecognized form of instruction") :
924                   _("unrecognized instruction"));
925
926     if (strlen (start) > 50)
927       /* xgettext:c-format */
928       sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
929     else 
930       /* xgettext:c-format */
931       sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
932 #else
933     if (strlen (start) > 50)
934       /* xgettext:c-format */
935       sprintf (errbuf, _("bad instruction `%.50s...'"), start);
936     else 
937       /* xgettext:c-format */
938       sprintf (errbuf, _("bad instruction `%.50s'"), start);
939 #endif
940       
941     *errmsg = errbuf;
942     return NULL;
943   }
944 }
945 \f
946 #if 0 /* This calls back to GAS which we can't do without care.  */
947
948 /* Record each member of OPVALS in the assembler's symbol table.
949    This lets GAS parse registers for us.
950    ??? Interesting idea but not currently used.  */
951
952 /* Record each member of OPVALS in the assembler's symbol table.
953    FIXME: Not currently used.  */
954
955 void
956 ip2k_cgen_asm_hash_keywords (CGEN_CPU_DESC cd, CGEN_KEYWORD *opvals)
957 {
958   CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL);
959   const CGEN_KEYWORD_ENTRY * ke;
960
961   while ((ke = cgen_keyword_search_next (& search)) != NULL)
962     {
963 #if 0 /* Unnecessary, should be done in the search routine.  */
964       if (! ip2k_cgen_opval_supported (ke))
965         continue;
966 #endif
967       cgen_asm_record_register (cd, ke->name, ke->value);
968     }
969 }
970
971 #endif /* 0 */