1*3d8817e4Smiod /* Assembler interface for targets using CGEN. -*- C -*-
2*3d8817e4Smiod CGEN: Cpu tools GENerator
3*3d8817e4Smiod
4*3d8817e4Smiod THIS FILE IS MACHINE GENERATED WITH CGEN.
5*3d8817e4Smiod - the resultant file is machine generated, cgen-asm.in isn't
6*3d8817e4Smiod
7*3d8817e4Smiod Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2005
8*3d8817e4Smiod Free Software Foundation, Inc.
9*3d8817e4Smiod
10*3d8817e4Smiod This file is part of the GNU Binutils and GDB, the GNU debugger.
11*3d8817e4Smiod
12*3d8817e4Smiod This program is free software; you can redistribute it and/or modify
13*3d8817e4Smiod it under the terms of the GNU General Public License as published by
14*3d8817e4Smiod the Free Software Foundation; either version 2, or (at your option)
15*3d8817e4Smiod any later version.
16*3d8817e4Smiod
17*3d8817e4Smiod This program is distributed in the hope that it will be useful,
18*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
19*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20*3d8817e4Smiod GNU General Public License for more details.
21*3d8817e4Smiod
22*3d8817e4Smiod You should have received a copy of the GNU General Public License
23*3d8817e4Smiod along with this program; if not, write to the Free Software Foundation, Inc.,
24*3d8817e4Smiod 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
25*3d8817e4Smiod
26*3d8817e4Smiod /* ??? Eventually more and more of this stuff can go to cpu-independent files.
27*3d8817e4Smiod Keep that in mind. */
28*3d8817e4Smiod
29*3d8817e4Smiod #include "sysdep.h"
30*3d8817e4Smiod #include <stdio.h>
31*3d8817e4Smiod #include "ansidecl.h"
32*3d8817e4Smiod #include "bfd.h"
33*3d8817e4Smiod #include "symcat.h"
34*3d8817e4Smiod #include "openrisc-desc.h"
35*3d8817e4Smiod #include "openrisc-opc.h"
36*3d8817e4Smiod #include "opintl.h"
37*3d8817e4Smiod #include "xregex.h"
38*3d8817e4Smiod #include "libiberty.h"
39*3d8817e4Smiod #include "safe-ctype.h"
40*3d8817e4Smiod
41*3d8817e4Smiod #undef min
42*3d8817e4Smiod #define min(a,b) ((a) < (b) ? (a) : (b))
43*3d8817e4Smiod #undef max
44*3d8817e4Smiod #define max(a,b) ((a) > (b) ? (a) : (b))
45*3d8817e4Smiod
46*3d8817e4Smiod static const char * parse_insn_normal
47*3d8817e4Smiod (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
48*3d8817e4Smiod
49*3d8817e4Smiod /* -- assembler routines inserted here. */
50*3d8817e4Smiod
51*3d8817e4Smiod /* -- asm.c */
52*3d8817e4Smiod
53*3d8817e4Smiod static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'");
54*3d8817e4Smiod
55*3d8817e4Smiod #define CGEN_VERBOSE_ASSEMBLER_ERRORS
56*3d8817e4Smiod
57*3d8817e4Smiod long
openrisc_sign_extend_16bit(long value)58*3d8817e4Smiod openrisc_sign_extend_16bit (long value)
59*3d8817e4Smiod {
60*3d8817e4Smiod return ((value & 0xffff) ^ 0x8000) - 0x8000;
61*3d8817e4Smiod }
62*3d8817e4Smiod
63*3d8817e4Smiod /* Handle hi(). */
64*3d8817e4Smiod
65*3d8817e4Smiod static const char *
parse_hi16(CGEN_CPU_DESC cd,const char ** strp,int opindex,long * valuep)66*3d8817e4Smiod parse_hi16 (CGEN_CPU_DESC cd, const char ** strp, int opindex, long * valuep)
67*3d8817e4Smiod {
68*3d8817e4Smiod const char *errmsg;
69*3d8817e4Smiod enum cgen_parse_operand_result result_type;
70*3d8817e4Smiod unsigned long ret;
71*3d8817e4Smiod
72*3d8817e4Smiod if (**strp == '#')
73*3d8817e4Smiod ++*strp;
74*3d8817e4Smiod
75*3d8817e4Smiod if (strncasecmp (*strp, "hi(", 3) == 0)
76*3d8817e4Smiod {
77*3d8817e4Smiod bfd_vma value;
78*3d8817e4Smiod
79*3d8817e4Smiod *strp += 3;
80*3d8817e4Smiod errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_HI16,
81*3d8817e4Smiod & result_type, & value);
82*3d8817e4Smiod if (**strp != ')')
83*3d8817e4Smiod return MISSING_CLOSING_PARENTHESIS;
84*3d8817e4Smiod
85*3d8817e4Smiod ++*strp;
86*3d8817e4Smiod if (errmsg == NULL
87*3d8817e4Smiod && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
88*3d8817e4Smiod value >>= 16;
89*3d8817e4Smiod ret = value;
90*3d8817e4Smiod }
91*3d8817e4Smiod else
92*3d8817e4Smiod {
93*3d8817e4Smiod if (**strp == '-')
94*3d8817e4Smiod {
95*3d8817e4Smiod long value;
96*3d8817e4Smiod
97*3d8817e4Smiod errmsg = cgen_parse_signed_integer (cd, strp, opindex, &value);
98*3d8817e4Smiod ret = value;
99*3d8817e4Smiod }
100*3d8817e4Smiod else
101*3d8817e4Smiod {
102*3d8817e4Smiod unsigned long value;
103*3d8817e4Smiod
104*3d8817e4Smiod errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, &value);
105*3d8817e4Smiod ret = value;
106*3d8817e4Smiod }
107*3d8817e4Smiod }
108*3d8817e4Smiod
109*3d8817e4Smiod *valuep = ((ret & 0xffff) ^ 0x8000) - 0x8000;
110*3d8817e4Smiod return errmsg;
111*3d8817e4Smiod }
112*3d8817e4Smiod
113*3d8817e4Smiod /* Handle lo(). */
114*3d8817e4Smiod
115*3d8817e4Smiod static const char *
parse_lo16(CGEN_CPU_DESC cd,const char ** strp,int opindex,long * valuep)116*3d8817e4Smiod parse_lo16 (CGEN_CPU_DESC cd, const char ** strp, int opindex, long * valuep)
117*3d8817e4Smiod {
118*3d8817e4Smiod const char *errmsg;
119*3d8817e4Smiod enum cgen_parse_operand_result result_type;
120*3d8817e4Smiod unsigned long ret;
121*3d8817e4Smiod
122*3d8817e4Smiod if (**strp == '#')
123*3d8817e4Smiod ++*strp;
124*3d8817e4Smiod
125*3d8817e4Smiod if (strncasecmp (*strp, "lo(", 3) == 0)
126*3d8817e4Smiod {
127*3d8817e4Smiod bfd_vma value;
128*3d8817e4Smiod
129*3d8817e4Smiod *strp += 3;
130*3d8817e4Smiod errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LO16,
131*3d8817e4Smiod & result_type, & value);
132*3d8817e4Smiod if (**strp != ')')
133*3d8817e4Smiod return MISSING_CLOSING_PARENTHESIS;
134*3d8817e4Smiod
135*3d8817e4Smiod ++*strp;
136*3d8817e4Smiod ret = value;
137*3d8817e4Smiod }
138*3d8817e4Smiod else
139*3d8817e4Smiod {
140*3d8817e4Smiod if (**strp == '-')
141*3d8817e4Smiod {
142*3d8817e4Smiod long value;
143*3d8817e4Smiod
144*3d8817e4Smiod errmsg = cgen_parse_signed_integer (cd, strp, opindex, &value);
145*3d8817e4Smiod ret = value;
146*3d8817e4Smiod }
147*3d8817e4Smiod else
148*3d8817e4Smiod {
149*3d8817e4Smiod unsigned long value;
150*3d8817e4Smiod
151*3d8817e4Smiod errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, &value);
152*3d8817e4Smiod ret = value;
153*3d8817e4Smiod }
154*3d8817e4Smiod }
155*3d8817e4Smiod
156*3d8817e4Smiod *valuep = ((ret & 0xffff) ^ 0x8000) - 0x8000;
157*3d8817e4Smiod return errmsg;
158*3d8817e4Smiod }
159*3d8817e4Smiod
160*3d8817e4Smiod /* -- */
161*3d8817e4Smiod
162*3d8817e4Smiod const char * openrisc_cgen_parse_operand
163*3d8817e4Smiod (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
164*3d8817e4Smiod
165*3d8817e4Smiod /* Main entry point for operand parsing.
166*3d8817e4Smiod
167*3d8817e4Smiod This function is basically just a big switch statement. Earlier versions
168*3d8817e4Smiod used tables to look up the function to use, but
169*3d8817e4Smiod - if the table contains both assembler and disassembler functions then
170*3d8817e4Smiod the disassembler contains much of the assembler and vice-versa,
171*3d8817e4Smiod - there's a lot of inlining possibilities as things grow,
172*3d8817e4Smiod - using a switch statement avoids the function call overhead.
173*3d8817e4Smiod
174*3d8817e4Smiod This function could be moved into `parse_insn_normal', but keeping it
175*3d8817e4Smiod separate makes clear the interface between `parse_insn_normal' and each of
176*3d8817e4Smiod the handlers. */
177*3d8817e4Smiod
178*3d8817e4Smiod const char *
openrisc_cgen_parse_operand(CGEN_CPU_DESC cd,int opindex,const char ** strp,CGEN_FIELDS * fields)179*3d8817e4Smiod openrisc_cgen_parse_operand (CGEN_CPU_DESC cd,
180*3d8817e4Smiod int opindex,
181*3d8817e4Smiod const char ** strp,
182*3d8817e4Smiod CGEN_FIELDS * fields)
183*3d8817e4Smiod {
184*3d8817e4Smiod const char * errmsg = NULL;
185*3d8817e4Smiod /* Used by scalar operands that still need to be parsed. */
186*3d8817e4Smiod long junk ATTRIBUTE_UNUSED;
187*3d8817e4Smiod
188*3d8817e4Smiod switch (opindex)
189*3d8817e4Smiod {
190*3d8817e4Smiod case OPENRISC_OPERAND_ABS_26 :
191*3d8817e4Smiod {
192*3d8817e4Smiod bfd_vma value = 0;
193*3d8817e4Smiod errmsg = cgen_parse_address (cd, strp, OPENRISC_OPERAND_ABS_26, 0, NULL, & value);
194*3d8817e4Smiod fields->f_abs26 = value;
195*3d8817e4Smiod }
196*3d8817e4Smiod break;
197*3d8817e4Smiod case OPENRISC_OPERAND_DISP_26 :
198*3d8817e4Smiod {
199*3d8817e4Smiod bfd_vma value = 0;
200*3d8817e4Smiod errmsg = cgen_parse_address (cd, strp, OPENRISC_OPERAND_DISP_26, 0, NULL, & value);
201*3d8817e4Smiod fields->f_disp26 = value;
202*3d8817e4Smiod }
203*3d8817e4Smiod break;
204*3d8817e4Smiod case OPENRISC_OPERAND_HI16 :
205*3d8817e4Smiod errmsg = parse_hi16 (cd, strp, OPENRISC_OPERAND_HI16, (long *) (& fields->f_simm16));
206*3d8817e4Smiod break;
207*3d8817e4Smiod case OPENRISC_OPERAND_LO16 :
208*3d8817e4Smiod errmsg = parse_lo16 (cd, strp, OPENRISC_OPERAND_LO16, (long *) (& fields->f_lo16));
209*3d8817e4Smiod break;
210*3d8817e4Smiod case OPENRISC_OPERAND_OP_F_23 :
211*3d8817e4Smiod errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_OP_F_23, (unsigned long *) (& fields->f_op4));
212*3d8817e4Smiod break;
213*3d8817e4Smiod case OPENRISC_OPERAND_OP_F_3 :
214*3d8817e4Smiod errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_OP_F_3, (unsigned long *) (& fields->f_op5));
215*3d8817e4Smiod break;
216*3d8817e4Smiod case OPENRISC_OPERAND_RA :
217*3d8817e4Smiod errmsg = cgen_parse_keyword (cd, strp, & openrisc_cgen_opval_h_gr, & fields->f_r2);
218*3d8817e4Smiod break;
219*3d8817e4Smiod case OPENRISC_OPERAND_RB :
220*3d8817e4Smiod errmsg = cgen_parse_keyword (cd, strp, & openrisc_cgen_opval_h_gr, & fields->f_r3);
221*3d8817e4Smiod break;
222*3d8817e4Smiod case OPENRISC_OPERAND_RD :
223*3d8817e4Smiod errmsg = cgen_parse_keyword (cd, strp, & openrisc_cgen_opval_h_gr, & fields->f_r1);
224*3d8817e4Smiod break;
225*3d8817e4Smiod case OPENRISC_OPERAND_SIMM_16 :
226*3d8817e4Smiod errmsg = cgen_parse_signed_integer (cd, strp, OPENRISC_OPERAND_SIMM_16, (long *) (& fields->f_simm16));
227*3d8817e4Smiod break;
228*3d8817e4Smiod case OPENRISC_OPERAND_UI16NC :
229*3d8817e4Smiod errmsg = parse_lo16 (cd, strp, OPENRISC_OPERAND_UI16NC, (long *) (& fields->f_i16nc));
230*3d8817e4Smiod break;
231*3d8817e4Smiod case OPENRISC_OPERAND_UIMM_16 :
232*3d8817e4Smiod errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_UIMM_16, (unsigned long *) (& fields->f_uimm16));
233*3d8817e4Smiod break;
234*3d8817e4Smiod case OPENRISC_OPERAND_UIMM_5 :
235*3d8817e4Smiod errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_UIMM_5, (unsigned long *) (& fields->f_uimm5));
236*3d8817e4Smiod break;
237*3d8817e4Smiod
238*3d8817e4Smiod default :
239*3d8817e4Smiod /* xgettext:c-format */
240*3d8817e4Smiod fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
241*3d8817e4Smiod abort ();
242*3d8817e4Smiod }
243*3d8817e4Smiod
244*3d8817e4Smiod return errmsg;
245*3d8817e4Smiod }
246*3d8817e4Smiod
247*3d8817e4Smiod cgen_parse_fn * const openrisc_cgen_parse_handlers[] =
248*3d8817e4Smiod {
249*3d8817e4Smiod parse_insn_normal,
250*3d8817e4Smiod };
251*3d8817e4Smiod
252*3d8817e4Smiod void
openrisc_cgen_init_asm(CGEN_CPU_DESC cd)253*3d8817e4Smiod openrisc_cgen_init_asm (CGEN_CPU_DESC cd)
254*3d8817e4Smiod {
255*3d8817e4Smiod openrisc_cgen_init_opcode_table (cd);
256*3d8817e4Smiod openrisc_cgen_init_ibld_table (cd);
257*3d8817e4Smiod cd->parse_handlers = & openrisc_cgen_parse_handlers[0];
258*3d8817e4Smiod cd->parse_operand = openrisc_cgen_parse_operand;
259*3d8817e4Smiod }
260*3d8817e4Smiod
261*3d8817e4Smiod
262*3d8817e4Smiod
263*3d8817e4Smiod /* Regex construction routine.
264*3d8817e4Smiod
265*3d8817e4Smiod This translates an opcode syntax string into a regex string,
266*3d8817e4Smiod by replacing any non-character syntax element (such as an
267*3d8817e4Smiod opcode) with the pattern '.*'
268*3d8817e4Smiod
269*3d8817e4Smiod It then compiles the regex and stores it in the opcode, for
270*3d8817e4Smiod later use by openrisc_cgen_assemble_insn
271*3d8817e4Smiod
272*3d8817e4Smiod Returns NULL for success, an error message for failure. */
273*3d8817e4Smiod
274*3d8817e4Smiod char *
openrisc_cgen_build_insn_regex(CGEN_INSN * insn)275*3d8817e4Smiod openrisc_cgen_build_insn_regex (CGEN_INSN *insn)
276*3d8817e4Smiod {
277*3d8817e4Smiod CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
278*3d8817e4Smiod const char *mnem = CGEN_INSN_MNEMONIC (insn);
279*3d8817e4Smiod char rxbuf[CGEN_MAX_RX_ELEMENTS];
280*3d8817e4Smiod char *rx = rxbuf;
281*3d8817e4Smiod const CGEN_SYNTAX_CHAR_TYPE *syn;
282*3d8817e4Smiod int reg_err;
283*3d8817e4Smiod
284*3d8817e4Smiod syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
285*3d8817e4Smiod
286*3d8817e4Smiod /* Mnemonics come first in the syntax string. */
287*3d8817e4Smiod if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
288*3d8817e4Smiod return _("missing mnemonic in syntax string");
289*3d8817e4Smiod ++syn;
290*3d8817e4Smiod
291*3d8817e4Smiod /* Generate a case sensitive regular expression that emulates case
292*3d8817e4Smiod insensitive matching in the "C" locale. We cannot generate a case
293*3d8817e4Smiod insensitive regular expression because in Turkish locales, 'i' and 'I'
294*3d8817e4Smiod are not equal modulo case conversion. */
295*3d8817e4Smiod
296*3d8817e4Smiod /* Copy the literal mnemonic out of the insn. */
297*3d8817e4Smiod for (; *mnem; mnem++)
298*3d8817e4Smiod {
299*3d8817e4Smiod char c = *mnem;
300*3d8817e4Smiod
301*3d8817e4Smiod if (ISALPHA (c))
302*3d8817e4Smiod {
303*3d8817e4Smiod *rx++ = '[';
304*3d8817e4Smiod *rx++ = TOLOWER (c);
305*3d8817e4Smiod *rx++ = TOUPPER (c);
306*3d8817e4Smiod *rx++ = ']';
307*3d8817e4Smiod }
308*3d8817e4Smiod else
309*3d8817e4Smiod *rx++ = c;
310*3d8817e4Smiod }
311*3d8817e4Smiod
312*3d8817e4Smiod /* Copy any remaining literals from the syntax string into the rx. */
313*3d8817e4Smiod for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
314*3d8817e4Smiod {
315*3d8817e4Smiod if (CGEN_SYNTAX_CHAR_P (* syn))
316*3d8817e4Smiod {
317*3d8817e4Smiod char c = CGEN_SYNTAX_CHAR (* syn);
318*3d8817e4Smiod
319*3d8817e4Smiod switch (c)
320*3d8817e4Smiod {
321*3d8817e4Smiod /* Escape any regex metacharacters in the syntax. */
322*3d8817e4Smiod case '.': case '[': case '\\':
323*3d8817e4Smiod case '*': case '^': case '$':
324*3d8817e4Smiod
325*3d8817e4Smiod #ifdef CGEN_ESCAPE_EXTENDED_REGEX
326*3d8817e4Smiod case '?': case '{': case '}':
327*3d8817e4Smiod case '(': case ')': case '*':
328*3d8817e4Smiod case '|': case '+': case ']':
329*3d8817e4Smiod #endif
330*3d8817e4Smiod *rx++ = '\\';
331*3d8817e4Smiod *rx++ = c;
332*3d8817e4Smiod break;
333*3d8817e4Smiod
334*3d8817e4Smiod default:
335*3d8817e4Smiod if (ISALPHA (c))
336*3d8817e4Smiod {
337*3d8817e4Smiod *rx++ = '[';
338*3d8817e4Smiod *rx++ = TOLOWER (c);
339*3d8817e4Smiod *rx++ = TOUPPER (c);
340*3d8817e4Smiod *rx++ = ']';
341*3d8817e4Smiod }
342*3d8817e4Smiod else
343*3d8817e4Smiod *rx++ = c;
344*3d8817e4Smiod break;
345*3d8817e4Smiod }
346*3d8817e4Smiod }
347*3d8817e4Smiod else
348*3d8817e4Smiod {
349*3d8817e4Smiod /* Replace non-syntax fields with globs. */
350*3d8817e4Smiod *rx++ = '.';
351*3d8817e4Smiod *rx++ = '*';
352*3d8817e4Smiod }
353*3d8817e4Smiod }
354*3d8817e4Smiod
355*3d8817e4Smiod /* Trailing whitespace ok. */
356*3d8817e4Smiod * rx++ = '[';
357*3d8817e4Smiod * rx++ = ' ';
358*3d8817e4Smiod * rx++ = '\t';
359*3d8817e4Smiod * rx++ = ']';
360*3d8817e4Smiod * rx++ = '*';
361*3d8817e4Smiod
362*3d8817e4Smiod /* But anchor it after that. */
363*3d8817e4Smiod * rx++ = '$';
364*3d8817e4Smiod * rx = '\0';
365*3d8817e4Smiod
366*3d8817e4Smiod CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
367*3d8817e4Smiod reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
368*3d8817e4Smiod
369*3d8817e4Smiod if (reg_err == 0)
370*3d8817e4Smiod return NULL;
371*3d8817e4Smiod else
372*3d8817e4Smiod {
373*3d8817e4Smiod static char msg[80];
374*3d8817e4Smiod
375*3d8817e4Smiod regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
376*3d8817e4Smiod regfree ((regex_t *) CGEN_INSN_RX (insn));
377*3d8817e4Smiod free (CGEN_INSN_RX (insn));
378*3d8817e4Smiod (CGEN_INSN_RX (insn)) = NULL;
379*3d8817e4Smiod return msg;
380*3d8817e4Smiod }
381*3d8817e4Smiod }
382*3d8817e4Smiod
383*3d8817e4Smiod
384*3d8817e4Smiod /* Default insn parser.
385*3d8817e4Smiod
386*3d8817e4Smiod The syntax string is scanned and operands are parsed and stored in FIELDS.
387*3d8817e4Smiod Relocs are queued as we go via other callbacks.
388*3d8817e4Smiod
389*3d8817e4Smiod ??? Note that this is currently an all-or-nothing parser. If we fail to
390*3d8817e4Smiod parse the instruction, we return 0 and the caller will start over from
391*3d8817e4Smiod the beginning. Backtracking will be necessary in parsing subexpressions,
392*3d8817e4Smiod but that can be handled there. Not handling backtracking here may get
393*3d8817e4Smiod expensive in the case of the m68k. Deal with later.
394*3d8817e4Smiod
395*3d8817e4Smiod Returns NULL for success, an error message for failure. */
396*3d8817e4Smiod
397*3d8817e4Smiod static const char *
parse_insn_normal(CGEN_CPU_DESC cd,const CGEN_INSN * insn,const char ** strp,CGEN_FIELDS * fields)398*3d8817e4Smiod parse_insn_normal (CGEN_CPU_DESC cd,
399*3d8817e4Smiod const CGEN_INSN *insn,
400*3d8817e4Smiod const char **strp,
401*3d8817e4Smiod CGEN_FIELDS *fields)
402*3d8817e4Smiod {
403*3d8817e4Smiod /* ??? Runtime added insns not handled yet. */
404*3d8817e4Smiod const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
405*3d8817e4Smiod const char *str = *strp;
406*3d8817e4Smiod const char *errmsg;
407*3d8817e4Smiod const char *p;
408*3d8817e4Smiod const CGEN_SYNTAX_CHAR_TYPE * syn;
409*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
410*3d8817e4Smiod /* FIXME: wip */
411*3d8817e4Smiod int past_opcode_p;
412*3d8817e4Smiod #endif
413*3d8817e4Smiod
414*3d8817e4Smiod /* For now we assume the mnemonic is first (there are no leading operands).
415*3d8817e4Smiod We can parse it without needing to set up operand parsing.
416*3d8817e4Smiod GAS's input scrubber will ensure mnemonics are lowercase, but we may
417*3d8817e4Smiod not be called from GAS. */
418*3d8817e4Smiod p = CGEN_INSN_MNEMONIC (insn);
419*3d8817e4Smiod while (*p && TOLOWER (*p) == TOLOWER (*str))
420*3d8817e4Smiod ++p, ++str;
421*3d8817e4Smiod
422*3d8817e4Smiod if (* p)
423*3d8817e4Smiod return _("unrecognized instruction");
424*3d8817e4Smiod
425*3d8817e4Smiod #ifndef CGEN_MNEMONIC_OPERANDS
426*3d8817e4Smiod if (* str && ! ISSPACE (* str))
427*3d8817e4Smiod return _("unrecognized instruction");
428*3d8817e4Smiod #endif
429*3d8817e4Smiod
430*3d8817e4Smiod CGEN_INIT_PARSE (cd);
431*3d8817e4Smiod cgen_init_parse_operand (cd);
432*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
433*3d8817e4Smiod past_opcode_p = 0;
434*3d8817e4Smiod #endif
435*3d8817e4Smiod
436*3d8817e4Smiod /* We don't check for (*str != '\0') here because we want to parse
437*3d8817e4Smiod any trailing fake arguments in the syntax string. */
438*3d8817e4Smiod syn = CGEN_SYNTAX_STRING (syntax);
439*3d8817e4Smiod
440*3d8817e4Smiod /* Mnemonics come first for now, ensure valid string. */
441*3d8817e4Smiod if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
442*3d8817e4Smiod abort ();
443*3d8817e4Smiod
444*3d8817e4Smiod ++syn;
445*3d8817e4Smiod
446*3d8817e4Smiod while (* syn != 0)
447*3d8817e4Smiod {
448*3d8817e4Smiod /* Non operand chars must match exactly. */
449*3d8817e4Smiod if (CGEN_SYNTAX_CHAR_P (* syn))
450*3d8817e4Smiod {
451*3d8817e4Smiod /* FIXME: While we allow for non-GAS callers above, we assume the
452*3d8817e4Smiod first char after the mnemonic part is a space. */
453*3d8817e4Smiod /* FIXME: We also take inappropriate advantage of the fact that
454*3d8817e4Smiod GAS's input scrubber will remove extraneous blanks. */
455*3d8817e4Smiod if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
456*3d8817e4Smiod {
457*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
458*3d8817e4Smiod if (CGEN_SYNTAX_CHAR(* syn) == ' ')
459*3d8817e4Smiod past_opcode_p = 1;
460*3d8817e4Smiod #endif
461*3d8817e4Smiod ++ syn;
462*3d8817e4Smiod ++ str;
463*3d8817e4Smiod }
464*3d8817e4Smiod else if (*str)
465*3d8817e4Smiod {
466*3d8817e4Smiod /* Syntax char didn't match. Can't be this insn. */
467*3d8817e4Smiod static char msg [80];
468*3d8817e4Smiod
469*3d8817e4Smiod /* xgettext:c-format */
470*3d8817e4Smiod sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
471*3d8817e4Smiod CGEN_SYNTAX_CHAR(*syn), *str);
472*3d8817e4Smiod return msg;
473*3d8817e4Smiod }
474*3d8817e4Smiod else
475*3d8817e4Smiod {
476*3d8817e4Smiod /* Ran out of input. */
477*3d8817e4Smiod static char msg [80];
478*3d8817e4Smiod
479*3d8817e4Smiod /* xgettext:c-format */
480*3d8817e4Smiod sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
481*3d8817e4Smiod CGEN_SYNTAX_CHAR(*syn));
482*3d8817e4Smiod return msg;
483*3d8817e4Smiod }
484*3d8817e4Smiod continue;
485*3d8817e4Smiod }
486*3d8817e4Smiod
487*3d8817e4Smiod /* We have an operand of some sort. */
488*3d8817e4Smiod errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
489*3d8817e4Smiod &str, fields);
490*3d8817e4Smiod if (errmsg)
491*3d8817e4Smiod return errmsg;
492*3d8817e4Smiod
493*3d8817e4Smiod /* Done with this operand, continue with next one. */
494*3d8817e4Smiod ++ syn;
495*3d8817e4Smiod }
496*3d8817e4Smiod
497*3d8817e4Smiod /* If we're at the end of the syntax string, we're done. */
498*3d8817e4Smiod if (* syn == 0)
499*3d8817e4Smiod {
500*3d8817e4Smiod /* FIXME: For the moment we assume a valid `str' can only contain
501*3d8817e4Smiod blanks now. IE: We needn't try again with a longer version of
502*3d8817e4Smiod the insn and it is assumed that longer versions of insns appear
503*3d8817e4Smiod before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
504*3d8817e4Smiod while (ISSPACE (* str))
505*3d8817e4Smiod ++ str;
506*3d8817e4Smiod
507*3d8817e4Smiod if (* str != '\0')
508*3d8817e4Smiod return _("junk at end of line"); /* FIXME: would like to include `str' */
509*3d8817e4Smiod
510*3d8817e4Smiod return NULL;
511*3d8817e4Smiod }
512*3d8817e4Smiod
513*3d8817e4Smiod /* We couldn't parse it. */
514*3d8817e4Smiod return _("unrecognized instruction");
515*3d8817e4Smiod }
516*3d8817e4Smiod
517*3d8817e4Smiod /* Main entry point.
518*3d8817e4Smiod This routine is called for each instruction to be assembled.
519*3d8817e4Smiod STR points to the insn to be assembled.
520*3d8817e4Smiod We assume all necessary tables have been initialized.
521*3d8817e4Smiod The assembled instruction, less any fixups, is stored in BUF.
522*3d8817e4Smiod Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
523*3d8817e4Smiod still needs to be converted to target byte order, otherwise BUF is an array
524*3d8817e4Smiod of bytes in target byte order.
525*3d8817e4Smiod The result is a pointer to the insn's entry in the opcode table,
526*3d8817e4Smiod or NULL if an error occured (an error message will have already been
527*3d8817e4Smiod printed).
528*3d8817e4Smiod
529*3d8817e4Smiod Note that when processing (non-alias) macro-insns,
530*3d8817e4Smiod this function recurses.
531*3d8817e4Smiod
532*3d8817e4Smiod ??? It's possible to make this cpu-independent.
533*3d8817e4Smiod One would have to deal with a few minor things.
534*3d8817e4Smiod At this point in time doing so would be more of a curiosity than useful
535*3d8817e4Smiod [for example this file isn't _that_ big], but keeping the possibility in
536*3d8817e4Smiod mind helps keep the design clean. */
537*3d8817e4Smiod
538*3d8817e4Smiod const CGEN_INSN *
openrisc_cgen_assemble_insn(CGEN_CPU_DESC cd,const char * str,CGEN_FIELDS * fields,CGEN_INSN_BYTES_PTR buf,char ** errmsg)539*3d8817e4Smiod openrisc_cgen_assemble_insn (CGEN_CPU_DESC cd,
540*3d8817e4Smiod const char *str,
541*3d8817e4Smiod CGEN_FIELDS *fields,
542*3d8817e4Smiod CGEN_INSN_BYTES_PTR buf,
543*3d8817e4Smiod char **errmsg)
544*3d8817e4Smiod {
545*3d8817e4Smiod const char *start;
546*3d8817e4Smiod CGEN_INSN_LIST *ilist;
547*3d8817e4Smiod const char *parse_errmsg = NULL;
548*3d8817e4Smiod const char *insert_errmsg = NULL;
549*3d8817e4Smiod int recognized_mnemonic = 0;
550*3d8817e4Smiod
551*3d8817e4Smiod /* Skip leading white space. */
552*3d8817e4Smiod while (ISSPACE (* str))
553*3d8817e4Smiod ++ str;
554*3d8817e4Smiod
555*3d8817e4Smiod /* The instructions are stored in hashed lists.
556*3d8817e4Smiod Get the first in the list. */
557*3d8817e4Smiod ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
558*3d8817e4Smiod
559*3d8817e4Smiod /* Keep looking until we find a match. */
560*3d8817e4Smiod start = str;
561*3d8817e4Smiod for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
562*3d8817e4Smiod {
563*3d8817e4Smiod const CGEN_INSN *insn = ilist->insn;
564*3d8817e4Smiod recognized_mnemonic = 1;
565*3d8817e4Smiod
566*3d8817e4Smiod #ifdef CGEN_VALIDATE_INSN_SUPPORTED
567*3d8817e4Smiod /* Not usually needed as unsupported opcodes
568*3d8817e4Smiod shouldn't be in the hash lists. */
569*3d8817e4Smiod /* Is this insn supported by the selected cpu? */
570*3d8817e4Smiod if (! openrisc_cgen_insn_supported (cd, insn))
571*3d8817e4Smiod continue;
572*3d8817e4Smiod #endif
573*3d8817e4Smiod /* If the RELAXED attribute is set, this is an insn that shouldn't be
574*3d8817e4Smiod chosen immediately. Instead, it is used during assembler/linker
575*3d8817e4Smiod relaxation if possible. */
576*3d8817e4Smiod if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
577*3d8817e4Smiod continue;
578*3d8817e4Smiod
579*3d8817e4Smiod str = start;
580*3d8817e4Smiod
581*3d8817e4Smiod /* Skip this insn if str doesn't look right lexically. */
582*3d8817e4Smiod if (CGEN_INSN_RX (insn) != NULL &&
583*3d8817e4Smiod regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
584*3d8817e4Smiod continue;
585*3d8817e4Smiod
586*3d8817e4Smiod /* Allow parse/insert handlers to obtain length of insn. */
587*3d8817e4Smiod CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
588*3d8817e4Smiod
589*3d8817e4Smiod parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
590*3d8817e4Smiod if (parse_errmsg != NULL)
591*3d8817e4Smiod continue;
592*3d8817e4Smiod
593*3d8817e4Smiod /* ??? 0 is passed for `pc'. */
594*3d8817e4Smiod insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
595*3d8817e4Smiod (bfd_vma) 0);
596*3d8817e4Smiod if (insert_errmsg != NULL)
597*3d8817e4Smiod continue;
598*3d8817e4Smiod
599*3d8817e4Smiod /* It is up to the caller to actually output the insn and any
600*3d8817e4Smiod queued relocs. */
601*3d8817e4Smiod return insn;
602*3d8817e4Smiod }
603*3d8817e4Smiod
604*3d8817e4Smiod {
605*3d8817e4Smiod static char errbuf[150];
606*3d8817e4Smiod #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
607*3d8817e4Smiod const char *tmp_errmsg;
608*3d8817e4Smiod
609*3d8817e4Smiod /* If requesting verbose error messages, use insert_errmsg.
610*3d8817e4Smiod Failing that, use parse_errmsg. */
611*3d8817e4Smiod tmp_errmsg = (insert_errmsg ? insert_errmsg :
612*3d8817e4Smiod parse_errmsg ? parse_errmsg :
613*3d8817e4Smiod recognized_mnemonic ?
614*3d8817e4Smiod _("unrecognized form of instruction") :
615*3d8817e4Smiod _("unrecognized instruction"));
616*3d8817e4Smiod
617*3d8817e4Smiod if (strlen (start) > 50)
618*3d8817e4Smiod /* xgettext:c-format */
619*3d8817e4Smiod sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
620*3d8817e4Smiod else
621*3d8817e4Smiod /* xgettext:c-format */
622*3d8817e4Smiod sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
623*3d8817e4Smiod #else
624*3d8817e4Smiod if (strlen (start) > 50)
625*3d8817e4Smiod /* xgettext:c-format */
626*3d8817e4Smiod sprintf (errbuf, _("bad instruction `%.50s...'"), start);
627*3d8817e4Smiod else
628*3d8817e4Smiod /* xgettext:c-format */
629*3d8817e4Smiod sprintf (errbuf, _("bad instruction `%.50s'"), start);
630*3d8817e4Smiod #endif
631*3d8817e4Smiod
632*3d8817e4Smiod *errmsg = errbuf;
633*3d8817e4Smiod return NULL;
634*3d8817e4Smiod }
635*3d8817e4Smiod }
636