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 "m32r-desc.h"
35*3d8817e4Smiod #include "m32r-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 static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'");
53*3d8817e4Smiod
54*3d8817e4Smiod /* Handle '#' prefixes (i.e. skip over them). */
55*3d8817e4Smiod
56*3d8817e4Smiod static const char *
parse_hash(CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,const char ** strp,int opindex ATTRIBUTE_UNUSED,long * valuep ATTRIBUTE_UNUSED)57*3d8817e4Smiod parse_hash (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
58*3d8817e4Smiod const char **strp,
59*3d8817e4Smiod int opindex ATTRIBUTE_UNUSED,
60*3d8817e4Smiod long *valuep ATTRIBUTE_UNUSED)
61*3d8817e4Smiod {
62*3d8817e4Smiod if (**strp == '#')
63*3d8817e4Smiod ++*strp;
64*3d8817e4Smiod return NULL;
65*3d8817e4Smiod }
66*3d8817e4Smiod
67*3d8817e4Smiod /* Handle shigh(), high(). */
68*3d8817e4Smiod
69*3d8817e4Smiod static const char *
parse_hi16(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)70*3d8817e4Smiod parse_hi16 (CGEN_CPU_DESC cd,
71*3d8817e4Smiod const char **strp,
72*3d8817e4Smiod int opindex,
73*3d8817e4Smiod unsigned long *valuep)
74*3d8817e4Smiod {
75*3d8817e4Smiod const char *errmsg;
76*3d8817e4Smiod enum cgen_parse_operand_result result_type;
77*3d8817e4Smiod bfd_vma value;
78*3d8817e4Smiod
79*3d8817e4Smiod if (**strp == '#')
80*3d8817e4Smiod ++*strp;
81*3d8817e4Smiod
82*3d8817e4Smiod if (strncasecmp (*strp, "high(", 5) == 0)
83*3d8817e4Smiod {
84*3d8817e4Smiod *strp += 5;
85*3d8817e4Smiod errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_HI16_ULO,
86*3d8817e4Smiod & result_type, & value);
87*3d8817e4Smiod if (**strp != ')')
88*3d8817e4Smiod return MISSING_CLOSING_PARENTHESIS;
89*3d8817e4Smiod ++*strp;
90*3d8817e4Smiod if (errmsg == NULL
91*3d8817e4Smiod && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
92*3d8817e4Smiod {
93*3d8817e4Smiod value >>= 16;
94*3d8817e4Smiod value &= 0xffff;
95*3d8817e4Smiod }
96*3d8817e4Smiod *valuep = value;
97*3d8817e4Smiod return errmsg;
98*3d8817e4Smiod }
99*3d8817e4Smiod else if (strncasecmp (*strp, "shigh(", 6) == 0)
100*3d8817e4Smiod {
101*3d8817e4Smiod *strp += 6;
102*3d8817e4Smiod errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_HI16_SLO,
103*3d8817e4Smiod & result_type, & value);
104*3d8817e4Smiod if (**strp != ')')
105*3d8817e4Smiod return MISSING_CLOSING_PARENTHESIS;
106*3d8817e4Smiod ++*strp;
107*3d8817e4Smiod if (errmsg == NULL
108*3d8817e4Smiod && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
109*3d8817e4Smiod {
110*3d8817e4Smiod value += 0x8000;
111*3d8817e4Smiod value >>= 16;
112*3d8817e4Smiod value &= 0xffff;
113*3d8817e4Smiod }
114*3d8817e4Smiod *valuep = value;
115*3d8817e4Smiod return errmsg;
116*3d8817e4Smiod }
117*3d8817e4Smiod
118*3d8817e4Smiod return cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
119*3d8817e4Smiod }
120*3d8817e4Smiod
121*3d8817e4Smiod /* Handle low() in a signed context. Also handle sda().
122*3d8817e4Smiod The signedness of the value doesn't matter to low(), but this also
123*3d8817e4Smiod handles the case where low() isn't present. */
124*3d8817e4Smiod
125*3d8817e4Smiod static const char *
parse_slo16(CGEN_CPU_DESC cd,const char ** strp,int opindex,long * valuep)126*3d8817e4Smiod parse_slo16 (CGEN_CPU_DESC cd,
127*3d8817e4Smiod const char ** strp,
128*3d8817e4Smiod int opindex,
129*3d8817e4Smiod long * valuep)
130*3d8817e4Smiod {
131*3d8817e4Smiod const char *errmsg;
132*3d8817e4Smiod enum cgen_parse_operand_result result_type;
133*3d8817e4Smiod bfd_vma value;
134*3d8817e4Smiod
135*3d8817e4Smiod if (**strp == '#')
136*3d8817e4Smiod ++*strp;
137*3d8817e4Smiod
138*3d8817e4Smiod if (strncasecmp (*strp, "low(", 4) == 0)
139*3d8817e4Smiod {
140*3d8817e4Smiod *strp += 4;
141*3d8817e4Smiod errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_LO16,
142*3d8817e4Smiod & result_type, & value);
143*3d8817e4Smiod if (**strp != ')')
144*3d8817e4Smiod return MISSING_CLOSING_PARENTHESIS;
145*3d8817e4Smiod ++*strp;
146*3d8817e4Smiod if (errmsg == NULL
147*3d8817e4Smiod && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
148*3d8817e4Smiod value = ((value & 0xffff) ^ 0x8000) - 0x8000;
149*3d8817e4Smiod *valuep = value;
150*3d8817e4Smiod return errmsg;
151*3d8817e4Smiod }
152*3d8817e4Smiod
153*3d8817e4Smiod if (strncasecmp (*strp, "sda(", 4) == 0)
154*3d8817e4Smiod {
155*3d8817e4Smiod *strp += 4;
156*3d8817e4Smiod errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_SDA16,
157*3d8817e4Smiod NULL, & value);
158*3d8817e4Smiod if (**strp != ')')
159*3d8817e4Smiod return MISSING_CLOSING_PARENTHESIS;
160*3d8817e4Smiod ++*strp;
161*3d8817e4Smiod *valuep = value;
162*3d8817e4Smiod return errmsg;
163*3d8817e4Smiod }
164*3d8817e4Smiod
165*3d8817e4Smiod return cgen_parse_signed_integer (cd, strp, opindex, valuep);
166*3d8817e4Smiod }
167*3d8817e4Smiod
168*3d8817e4Smiod /* Handle low() in an unsigned context.
169*3d8817e4Smiod The signedness of the value doesn't matter to low(), but this also
170*3d8817e4Smiod handles the case where low() isn't present. */
171*3d8817e4Smiod
172*3d8817e4Smiod static const char *
parse_ulo16(CGEN_CPU_DESC cd,const char ** strp,int opindex,unsigned long * valuep)173*3d8817e4Smiod parse_ulo16 (CGEN_CPU_DESC cd,
174*3d8817e4Smiod const char **strp,
175*3d8817e4Smiod int opindex,
176*3d8817e4Smiod unsigned long *valuep)
177*3d8817e4Smiod {
178*3d8817e4Smiod const char *errmsg;
179*3d8817e4Smiod enum cgen_parse_operand_result result_type;
180*3d8817e4Smiod bfd_vma value;
181*3d8817e4Smiod
182*3d8817e4Smiod if (**strp == '#')
183*3d8817e4Smiod ++*strp;
184*3d8817e4Smiod
185*3d8817e4Smiod if (strncasecmp (*strp, "low(", 4) == 0)
186*3d8817e4Smiod {
187*3d8817e4Smiod *strp += 4;
188*3d8817e4Smiod errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_LO16,
189*3d8817e4Smiod & result_type, & value);
190*3d8817e4Smiod if (**strp != ')')
191*3d8817e4Smiod return MISSING_CLOSING_PARENTHESIS;
192*3d8817e4Smiod ++*strp;
193*3d8817e4Smiod if (errmsg == NULL
194*3d8817e4Smiod && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
195*3d8817e4Smiod value &= 0xffff;
196*3d8817e4Smiod *valuep = value;
197*3d8817e4Smiod return errmsg;
198*3d8817e4Smiod }
199*3d8817e4Smiod
200*3d8817e4Smiod return cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
201*3d8817e4Smiod }
202*3d8817e4Smiod
203*3d8817e4Smiod /* -- */
204*3d8817e4Smiod
205*3d8817e4Smiod const char * m32r_cgen_parse_operand
206*3d8817e4Smiod (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
207*3d8817e4Smiod
208*3d8817e4Smiod /* Main entry point for operand parsing.
209*3d8817e4Smiod
210*3d8817e4Smiod This function is basically just a big switch statement. Earlier versions
211*3d8817e4Smiod used tables to look up the function to use, but
212*3d8817e4Smiod - if the table contains both assembler and disassembler functions then
213*3d8817e4Smiod the disassembler contains much of the assembler and vice-versa,
214*3d8817e4Smiod - there's a lot of inlining possibilities as things grow,
215*3d8817e4Smiod - using a switch statement avoids the function call overhead.
216*3d8817e4Smiod
217*3d8817e4Smiod This function could be moved into `parse_insn_normal', but keeping it
218*3d8817e4Smiod separate makes clear the interface between `parse_insn_normal' and each of
219*3d8817e4Smiod the handlers. */
220*3d8817e4Smiod
221*3d8817e4Smiod const char *
m32r_cgen_parse_operand(CGEN_CPU_DESC cd,int opindex,const char ** strp,CGEN_FIELDS * fields)222*3d8817e4Smiod m32r_cgen_parse_operand (CGEN_CPU_DESC cd,
223*3d8817e4Smiod int opindex,
224*3d8817e4Smiod const char ** strp,
225*3d8817e4Smiod CGEN_FIELDS * fields)
226*3d8817e4Smiod {
227*3d8817e4Smiod const char * errmsg = NULL;
228*3d8817e4Smiod /* Used by scalar operands that still need to be parsed. */
229*3d8817e4Smiod long junk ATTRIBUTE_UNUSED;
230*3d8817e4Smiod
231*3d8817e4Smiod switch (opindex)
232*3d8817e4Smiod {
233*3d8817e4Smiod case M32R_OPERAND_ACC :
234*3d8817e4Smiod errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_h_accums, & fields->f_acc);
235*3d8817e4Smiod break;
236*3d8817e4Smiod case M32R_OPERAND_ACCD :
237*3d8817e4Smiod errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_h_accums, & fields->f_accd);
238*3d8817e4Smiod break;
239*3d8817e4Smiod case M32R_OPERAND_ACCS :
240*3d8817e4Smiod errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_h_accums, & fields->f_accs);
241*3d8817e4Smiod break;
242*3d8817e4Smiod case M32R_OPERAND_DCR :
243*3d8817e4Smiod errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_cr_names, & fields->f_r1);
244*3d8817e4Smiod break;
245*3d8817e4Smiod case M32R_OPERAND_DISP16 :
246*3d8817e4Smiod {
247*3d8817e4Smiod bfd_vma value = 0;
248*3d8817e4Smiod errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_DISP16, 0, NULL, & value);
249*3d8817e4Smiod fields->f_disp16 = value;
250*3d8817e4Smiod }
251*3d8817e4Smiod break;
252*3d8817e4Smiod case M32R_OPERAND_DISP24 :
253*3d8817e4Smiod {
254*3d8817e4Smiod bfd_vma value = 0;
255*3d8817e4Smiod errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_DISP24, 0, NULL, & value);
256*3d8817e4Smiod fields->f_disp24 = value;
257*3d8817e4Smiod }
258*3d8817e4Smiod break;
259*3d8817e4Smiod case M32R_OPERAND_DISP8 :
260*3d8817e4Smiod {
261*3d8817e4Smiod bfd_vma value = 0;
262*3d8817e4Smiod errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_DISP8, 0, NULL, & value);
263*3d8817e4Smiod fields->f_disp8 = value;
264*3d8817e4Smiod }
265*3d8817e4Smiod break;
266*3d8817e4Smiod case M32R_OPERAND_DR :
267*3d8817e4Smiod errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r1);
268*3d8817e4Smiod break;
269*3d8817e4Smiod case M32R_OPERAND_HASH :
270*3d8817e4Smiod errmsg = parse_hash (cd, strp, M32R_OPERAND_HASH, (long *) (& junk));
271*3d8817e4Smiod break;
272*3d8817e4Smiod case M32R_OPERAND_HI16 :
273*3d8817e4Smiod errmsg = parse_hi16 (cd, strp, M32R_OPERAND_HI16, (unsigned long *) (& fields->f_hi16));
274*3d8817e4Smiod break;
275*3d8817e4Smiod case M32R_OPERAND_IMM1 :
276*3d8817e4Smiod errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_IMM1, (unsigned long *) (& fields->f_imm1));
277*3d8817e4Smiod break;
278*3d8817e4Smiod case M32R_OPERAND_SCR :
279*3d8817e4Smiod errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_cr_names, & fields->f_r2);
280*3d8817e4Smiod break;
281*3d8817e4Smiod case M32R_OPERAND_SIMM16 :
282*3d8817e4Smiod errmsg = cgen_parse_signed_integer (cd, strp, M32R_OPERAND_SIMM16, (long *) (& fields->f_simm16));
283*3d8817e4Smiod break;
284*3d8817e4Smiod case M32R_OPERAND_SIMM8 :
285*3d8817e4Smiod errmsg = cgen_parse_signed_integer (cd, strp, M32R_OPERAND_SIMM8, (long *) (& fields->f_simm8));
286*3d8817e4Smiod break;
287*3d8817e4Smiod case M32R_OPERAND_SLO16 :
288*3d8817e4Smiod errmsg = parse_slo16 (cd, strp, M32R_OPERAND_SLO16, (long *) (& fields->f_simm16));
289*3d8817e4Smiod break;
290*3d8817e4Smiod case M32R_OPERAND_SR :
291*3d8817e4Smiod errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r2);
292*3d8817e4Smiod break;
293*3d8817e4Smiod case M32R_OPERAND_SRC1 :
294*3d8817e4Smiod errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r1);
295*3d8817e4Smiod break;
296*3d8817e4Smiod case M32R_OPERAND_SRC2 :
297*3d8817e4Smiod errmsg = cgen_parse_keyword (cd, strp, & m32r_cgen_opval_gr_names, & fields->f_r2);
298*3d8817e4Smiod break;
299*3d8817e4Smiod case M32R_OPERAND_UIMM16 :
300*3d8817e4Smiod errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM16, (unsigned long *) (& fields->f_uimm16));
301*3d8817e4Smiod break;
302*3d8817e4Smiod case M32R_OPERAND_UIMM24 :
303*3d8817e4Smiod {
304*3d8817e4Smiod bfd_vma value = 0;
305*3d8817e4Smiod errmsg = cgen_parse_address (cd, strp, M32R_OPERAND_UIMM24, 0, NULL, & value);
306*3d8817e4Smiod fields->f_uimm24 = value;
307*3d8817e4Smiod }
308*3d8817e4Smiod break;
309*3d8817e4Smiod case M32R_OPERAND_UIMM3 :
310*3d8817e4Smiod errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM3, (unsigned long *) (& fields->f_uimm3));
311*3d8817e4Smiod break;
312*3d8817e4Smiod case M32R_OPERAND_UIMM4 :
313*3d8817e4Smiod errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM4, (unsigned long *) (& fields->f_uimm4));
314*3d8817e4Smiod break;
315*3d8817e4Smiod case M32R_OPERAND_UIMM5 :
316*3d8817e4Smiod errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM5, (unsigned long *) (& fields->f_uimm5));
317*3d8817e4Smiod break;
318*3d8817e4Smiod case M32R_OPERAND_UIMM8 :
319*3d8817e4Smiod errmsg = cgen_parse_unsigned_integer (cd, strp, M32R_OPERAND_UIMM8, (unsigned long *) (& fields->f_uimm8));
320*3d8817e4Smiod break;
321*3d8817e4Smiod case M32R_OPERAND_ULO16 :
322*3d8817e4Smiod errmsg = parse_ulo16 (cd, strp, M32R_OPERAND_ULO16, (unsigned long *) (& fields->f_uimm16));
323*3d8817e4Smiod break;
324*3d8817e4Smiod
325*3d8817e4Smiod default :
326*3d8817e4Smiod /* xgettext:c-format */
327*3d8817e4Smiod fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
328*3d8817e4Smiod abort ();
329*3d8817e4Smiod }
330*3d8817e4Smiod
331*3d8817e4Smiod return errmsg;
332*3d8817e4Smiod }
333*3d8817e4Smiod
334*3d8817e4Smiod cgen_parse_fn * const m32r_cgen_parse_handlers[] =
335*3d8817e4Smiod {
336*3d8817e4Smiod parse_insn_normal,
337*3d8817e4Smiod };
338*3d8817e4Smiod
339*3d8817e4Smiod void
m32r_cgen_init_asm(CGEN_CPU_DESC cd)340*3d8817e4Smiod m32r_cgen_init_asm (CGEN_CPU_DESC cd)
341*3d8817e4Smiod {
342*3d8817e4Smiod m32r_cgen_init_opcode_table (cd);
343*3d8817e4Smiod m32r_cgen_init_ibld_table (cd);
344*3d8817e4Smiod cd->parse_handlers = & m32r_cgen_parse_handlers[0];
345*3d8817e4Smiod cd->parse_operand = m32r_cgen_parse_operand;
346*3d8817e4Smiod }
347*3d8817e4Smiod
348*3d8817e4Smiod
349*3d8817e4Smiod
350*3d8817e4Smiod /* Regex construction routine.
351*3d8817e4Smiod
352*3d8817e4Smiod This translates an opcode syntax string into a regex string,
353*3d8817e4Smiod by replacing any non-character syntax element (such as an
354*3d8817e4Smiod opcode) with the pattern '.*'
355*3d8817e4Smiod
356*3d8817e4Smiod It then compiles the regex and stores it in the opcode, for
357*3d8817e4Smiod later use by m32r_cgen_assemble_insn
358*3d8817e4Smiod
359*3d8817e4Smiod Returns NULL for success, an error message for failure. */
360*3d8817e4Smiod
361*3d8817e4Smiod char *
m32r_cgen_build_insn_regex(CGEN_INSN * insn)362*3d8817e4Smiod m32r_cgen_build_insn_regex (CGEN_INSN *insn)
363*3d8817e4Smiod {
364*3d8817e4Smiod CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
365*3d8817e4Smiod const char *mnem = CGEN_INSN_MNEMONIC (insn);
366*3d8817e4Smiod char rxbuf[CGEN_MAX_RX_ELEMENTS];
367*3d8817e4Smiod char *rx = rxbuf;
368*3d8817e4Smiod const CGEN_SYNTAX_CHAR_TYPE *syn;
369*3d8817e4Smiod int reg_err;
370*3d8817e4Smiod
371*3d8817e4Smiod syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
372*3d8817e4Smiod
373*3d8817e4Smiod /* Mnemonics come first in the syntax string. */
374*3d8817e4Smiod if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
375*3d8817e4Smiod return _("missing mnemonic in syntax string");
376*3d8817e4Smiod ++syn;
377*3d8817e4Smiod
378*3d8817e4Smiod /* Generate a case sensitive regular expression that emulates case
379*3d8817e4Smiod insensitive matching in the "C" locale. We cannot generate a case
380*3d8817e4Smiod insensitive regular expression because in Turkish locales, 'i' and 'I'
381*3d8817e4Smiod are not equal modulo case conversion. */
382*3d8817e4Smiod
383*3d8817e4Smiod /* Copy the literal mnemonic out of the insn. */
384*3d8817e4Smiod for (; *mnem; mnem++)
385*3d8817e4Smiod {
386*3d8817e4Smiod char c = *mnem;
387*3d8817e4Smiod
388*3d8817e4Smiod if (ISALPHA (c))
389*3d8817e4Smiod {
390*3d8817e4Smiod *rx++ = '[';
391*3d8817e4Smiod *rx++ = TOLOWER (c);
392*3d8817e4Smiod *rx++ = TOUPPER (c);
393*3d8817e4Smiod *rx++ = ']';
394*3d8817e4Smiod }
395*3d8817e4Smiod else
396*3d8817e4Smiod *rx++ = c;
397*3d8817e4Smiod }
398*3d8817e4Smiod
399*3d8817e4Smiod /* Copy any remaining literals from the syntax string into the rx. */
400*3d8817e4Smiod for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
401*3d8817e4Smiod {
402*3d8817e4Smiod if (CGEN_SYNTAX_CHAR_P (* syn))
403*3d8817e4Smiod {
404*3d8817e4Smiod char c = CGEN_SYNTAX_CHAR (* syn);
405*3d8817e4Smiod
406*3d8817e4Smiod switch (c)
407*3d8817e4Smiod {
408*3d8817e4Smiod /* Escape any regex metacharacters in the syntax. */
409*3d8817e4Smiod case '.': case '[': case '\\':
410*3d8817e4Smiod case '*': case '^': case '$':
411*3d8817e4Smiod
412*3d8817e4Smiod #ifdef CGEN_ESCAPE_EXTENDED_REGEX
413*3d8817e4Smiod case '?': case '{': case '}':
414*3d8817e4Smiod case '(': case ')': case '*':
415*3d8817e4Smiod case '|': case '+': case ']':
416*3d8817e4Smiod #endif
417*3d8817e4Smiod *rx++ = '\\';
418*3d8817e4Smiod *rx++ = c;
419*3d8817e4Smiod break;
420*3d8817e4Smiod
421*3d8817e4Smiod default:
422*3d8817e4Smiod if (ISALPHA (c))
423*3d8817e4Smiod {
424*3d8817e4Smiod *rx++ = '[';
425*3d8817e4Smiod *rx++ = TOLOWER (c);
426*3d8817e4Smiod *rx++ = TOUPPER (c);
427*3d8817e4Smiod *rx++ = ']';
428*3d8817e4Smiod }
429*3d8817e4Smiod else
430*3d8817e4Smiod *rx++ = c;
431*3d8817e4Smiod break;
432*3d8817e4Smiod }
433*3d8817e4Smiod }
434*3d8817e4Smiod else
435*3d8817e4Smiod {
436*3d8817e4Smiod /* Replace non-syntax fields with globs. */
437*3d8817e4Smiod *rx++ = '.';
438*3d8817e4Smiod *rx++ = '*';
439*3d8817e4Smiod }
440*3d8817e4Smiod }
441*3d8817e4Smiod
442*3d8817e4Smiod /* Trailing whitespace ok. */
443*3d8817e4Smiod * rx++ = '[';
444*3d8817e4Smiod * rx++ = ' ';
445*3d8817e4Smiod * rx++ = '\t';
446*3d8817e4Smiod * rx++ = ']';
447*3d8817e4Smiod * rx++ = '*';
448*3d8817e4Smiod
449*3d8817e4Smiod /* But anchor it after that. */
450*3d8817e4Smiod * rx++ = '$';
451*3d8817e4Smiod * rx = '\0';
452*3d8817e4Smiod
453*3d8817e4Smiod CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
454*3d8817e4Smiod reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
455*3d8817e4Smiod
456*3d8817e4Smiod if (reg_err == 0)
457*3d8817e4Smiod return NULL;
458*3d8817e4Smiod else
459*3d8817e4Smiod {
460*3d8817e4Smiod static char msg[80];
461*3d8817e4Smiod
462*3d8817e4Smiod regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
463*3d8817e4Smiod regfree ((regex_t *) CGEN_INSN_RX (insn));
464*3d8817e4Smiod free (CGEN_INSN_RX (insn));
465*3d8817e4Smiod (CGEN_INSN_RX (insn)) = NULL;
466*3d8817e4Smiod return msg;
467*3d8817e4Smiod }
468*3d8817e4Smiod }
469*3d8817e4Smiod
470*3d8817e4Smiod
471*3d8817e4Smiod /* Default insn parser.
472*3d8817e4Smiod
473*3d8817e4Smiod The syntax string is scanned and operands are parsed and stored in FIELDS.
474*3d8817e4Smiod Relocs are queued as we go via other callbacks.
475*3d8817e4Smiod
476*3d8817e4Smiod ??? Note that this is currently an all-or-nothing parser. If we fail to
477*3d8817e4Smiod parse the instruction, we return 0 and the caller will start over from
478*3d8817e4Smiod the beginning. Backtracking will be necessary in parsing subexpressions,
479*3d8817e4Smiod but that can be handled there. Not handling backtracking here may get
480*3d8817e4Smiod expensive in the case of the m68k. Deal with later.
481*3d8817e4Smiod
482*3d8817e4Smiod Returns NULL for success, an error message for failure. */
483*3d8817e4Smiod
484*3d8817e4Smiod static const char *
parse_insn_normal(CGEN_CPU_DESC cd,const CGEN_INSN * insn,const char ** strp,CGEN_FIELDS * fields)485*3d8817e4Smiod parse_insn_normal (CGEN_CPU_DESC cd,
486*3d8817e4Smiod const CGEN_INSN *insn,
487*3d8817e4Smiod const char **strp,
488*3d8817e4Smiod CGEN_FIELDS *fields)
489*3d8817e4Smiod {
490*3d8817e4Smiod /* ??? Runtime added insns not handled yet. */
491*3d8817e4Smiod const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
492*3d8817e4Smiod const char *str = *strp;
493*3d8817e4Smiod const char *errmsg;
494*3d8817e4Smiod const char *p;
495*3d8817e4Smiod const CGEN_SYNTAX_CHAR_TYPE * syn;
496*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
497*3d8817e4Smiod /* FIXME: wip */
498*3d8817e4Smiod int past_opcode_p;
499*3d8817e4Smiod #endif
500*3d8817e4Smiod
501*3d8817e4Smiod /* For now we assume the mnemonic is first (there are no leading operands).
502*3d8817e4Smiod We can parse it without needing to set up operand parsing.
503*3d8817e4Smiod GAS's input scrubber will ensure mnemonics are lowercase, but we may
504*3d8817e4Smiod not be called from GAS. */
505*3d8817e4Smiod p = CGEN_INSN_MNEMONIC (insn);
506*3d8817e4Smiod while (*p && TOLOWER (*p) == TOLOWER (*str))
507*3d8817e4Smiod ++p, ++str;
508*3d8817e4Smiod
509*3d8817e4Smiod if (* p)
510*3d8817e4Smiod return _("unrecognized instruction");
511*3d8817e4Smiod
512*3d8817e4Smiod #ifndef CGEN_MNEMONIC_OPERANDS
513*3d8817e4Smiod if (* str && ! ISSPACE (* str))
514*3d8817e4Smiod return _("unrecognized instruction");
515*3d8817e4Smiod #endif
516*3d8817e4Smiod
517*3d8817e4Smiod CGEN_INIT_PARSE (cd);
518*3d8817e4Smiod cgen_init_parse_operand (cd);
519*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
520*3d8817e4Smiod past_opcode_p = 0;
521*3d8817e4Smiod #endif
522*3d8817e4Smiod
523*3d8817e4Smiod /* We don't check for (*str != '\0') here because we want to parse
524*3d8817e4Smiod any trailing fake arguments in the syntax string. */
525*3d8817e4Smiod syn = CGEN_SYNTAX_STRING (syntax);
526*3d8817e4Smiod
527*3d8817e4Smiod /* Mnemonics come first for now, ensure valid string. */
528*3d8817e4Smiod if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
529*3d8817e4Smiod abort ();
530*3d8817e4Smiod
531*3d8817e4Smiod ++syn;
532*3d8817e4Smiod
533*3d8817e4Smiod while (* syn != 0)
534*3d8817e4Smiod {
535*3d8817e4Smiod /* Non operand chars must match exactly. */
536*3d8817e4Smiod if (CGEN_SYNTAX_CHAR_P (* syn))
537*3d8817e4Smiod {
538*3d8817e4Smiod /* FIXME: While we allow for non-GAS callers above, we assume the
539*3d8817e4Smiod first char after the mnemonic part is a space. */
540*3d8817e4Smiod /* FIXME: We also take inappropriate advantage of the fact that
541*3d8817e4Smiod GAS's input scrubber will remove extraneous blanks. */
542*3d8817e4Smiod if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
543*3d8817e4Smiod {
544*3d8817e4Smiod #ifdef CGEN_MNEMONIC_OPERANDS
545*3d8817e4Smiod if (CGEN_SYNTAX_CHAR(* syn) == ' ')
546*3d8817e4Smiod past_opcode_p = 1;
547*3d8817e4Smiod #endif
548*3d8817e4Smiod ++ syn;
549*3d8817e4Smiod ++ str;
550*3d8817e4Smiod }
551*3d8817e4Smiod else if (*str)
552*3d8817e4Smiod {
553*3d8817e4Smiod /* Syntax char didn't match. Can't be this insn. */
554*3d8817e4Smiod static char msg [80];
555*3d8817e4Smiod
556*3d8817e4Smiod /* xgettext:c-format */
557*3d8817e4Smiod sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
558*3d8817e4Smiod CGEN_SYNTAX_CHAR(*syn), *str);
559*3d8817e4Smiod return msg;
560*3d8817e4Smiod }
561*3d8817e4Smiod else
562*3d8817e4Smiod {
563*3d8817e4Smiod /* Ran out of input. */
564*3d8817e4Smiod static char msg [80];
565*3d8817e4Smiod
566*3d8817e4Smiod /* xgettext:c-format */
567*3d8817e4Smiod sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
568*3d8817e4Smiod CGEN_SYNTAX_CHAR(*syn));
569*3d8817e4Smiod return msg;
570*3d8817e4Smiod }
571*3d8817e4Smiod continue;
572*3d8817e4Smiod }
573*3d8817e4Smiod
574*3d8817e4Smiod /* We have an operand of some sort. */
575*3d8817e4Smiod errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
576*3d8817e4Smiod &str, fields);
577*3d8817e4Smiod if (errmsg)
578*3d8817e4Smiod return errmsg;
579*3d8817e4Smiod
580*3d8817e4Smiod /* Done with this operand, continue with next one. */
581*3d8817e4Smiod ++ syn;
582*3d8817e4Smiod }
583*3d8817e4Smiod
584*3d8817e4Smiod /* If we're at the end of the syntax string, we're done. */
585*3d8817e4Smiod if (* syn == 0)
586*3d8817e4Smiod {
587*3d8817e4Smiod /* FIXME: For the moment we assume a valid `str' can only contain
588*3d8817e4Smiod blanks now. IE: We needn't try again with a longer version of
589*3d8817e4Smiod the insn and it is assumed that longer versions of insns appear
590*3d8817e4Smiod before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
591*3d8817e4Smiod while (ISSPACE (* str))
592*3d8817e4Smiod ++ str;
593*3d8817e4Smiod
594*3d8817e4Smiod if (* str != '\0')
595*3d8817e4Smiod return _("junk at end of line"); /* FIXME: would like to include `str' */
596*3d8817e4Smiod
597*3d8817e4Smiod return NULL;
598*3d8817e4Smiod }
599*3d8817e4Smiod
600*3d8817e4Smiod /* We couldn't parse it. */
601*3d8817e4Smiod return _("unrecognized instruction");
602*3d8817e4Smiod }
603*3d8817e4Smiod
604*3d8817e4Smiod /* Main entry point.
605*3d8817e4Smiod This routine is called for each instruction to be assembled.
606*3d8817e4Smiod STR points to the insn to be assembled.
607*3d8817e4Smiod We assume all necessary tables have been initialized.
608*3d8817e4Smiod The assembled instruction, less any fixups, is stored in BUF.
609*3d8817e4Smiod Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
610*3d8817e4Smiod still needs to be converted to target byte order, otherwise BUF is an array
611*3d8817e4Smiod of bytes in target byte order.
612*3d8817e4Smiod The result is a pointer to the insn's entry in the opcode table,
613*3d8817e4Smiod or NULL if an error occured (an error message will have already been
614*3d8817e4Smiod printed).
615*3d8817e4Smiod
616*3d8817e4Smiod Note that when processing (non-alias) macro-insns,
617*3d8817e4Smiod this function recurses.
618*3d8817e4Smiod
619*3d8817e4Smiod ??? It's possible to make this cpu-independent.
620*3d8817e4Smiod One would have to deal with a few minor things.
621*3d8817e4Smiod At this point in time doing so would be more of a curiosity than useful
622*3d8817e4Smiod [for example this file isn't _that_ big], but keeping the possibility in
623*3d8817e4Smiod mind helps keep the design clean. */
624*3d8817e4Smiod
625*3d8817e4Smiod const CGEN_INSN *
m32r_cgen_assemble_insn(CGEN_CPU_DESC cd,const char * str,CGEN_FIELDS * fields,CGEN_INSN_BYTES_PTR buf,char ** errmsg)626*3d8817e4Smiod m32r_cgen_assemble_insn (CGEN_CPU_DESC cd,
627*3d8817e4Smiod const char *str,
628*3d8817e4Smiod CGEN_FIELDS *fields,
629*3d8817e4Smiod CGEN_INSN_BYTES_PTR buf,
630*3d8817e4Smiod char **errmsg)
631*3d8817e4Smiod {
632*3d8817e4Smiod const char *start;
633*3d8817e4Smiod CGEN_INSN_LIST *ilist;
634*3d8817e4Smiod const char *parse_errmsg = NULL;
635*3d8817e4Smiod const char *insert_errmsg = NULL;
636*3d8817e4Smiod int recognized_mnemonic = 0;
637*3d8817e4Smiod
638*3d8817e4Smiod /* Skip leading white space. */
639*3d8817e4Smiod while (ISSPACE (* str))
640*3d8817e4Smiod ++ str;
641*3d8817e4Smiod
642*3d8817e4Smiod /* The instructions are stored in hashed lists.
643*3d8817e4Smiod Get the first in the list. */
644*3d8817e4Smiod ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
645*3d8817e4Smiod
646*3d8817e4Smiod /* Keep looking until we find a match. */
647*3d8817e4Smiod start = str;
648*3d8817e4Smiod for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
649*3d8817e4Smiod {
650*3d8817e4Smiod const CGEN_INSN *insn = ilist->insn;
651*3d8817e4Smiod recognized_mnemonic = 1;
652*3d8817e4Smiod
653*3d8817e4Smiod #ifdef CGEN_VALIDATE_INSN_SUPPORTED
654*3d8817e4Smiod /* Not usually needed as unsupported opcodes
655*3d8817e4Smiod shouldn't be in the hash lists. */
656*3d8817e4Smiod /* Is this insn supported by the selected cpu? */
657*3d8817e4Smiod if (! m32r_cgen_insn_supported (cd, insn))
658*3d8817e4Smiod continue;
659*3d8817e4Smiod #endif
660*3d8817e4Smiod /* If the RELAXED attribute is set, this is an insn that shouldn't be
661*3d8817e4Smiod chosen immediately. Instead, it is used during assembler/linker
662*3d8817e4Smiod relaxation if possible. */
663*3d8817e4Smiod if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
664*3d8817e4Smiod continue;
665*3d8817e4Smiod
666*3d8817e4Smiod str = start;
667*3d8817e4Smiod
668*3d8817e4Smiod /* Skip this insn if str doesn't look right lexically. */
669*3d8817e4Smiod if (CGEN_INSN_RX (insn) != NULL &&
670*3d8817e4Smiod regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
671*3d8817e4Smiod continue;
672*3d8817e4Smiod
673*3d8817e4Smiod /* Allow parse/insert handlers to obtain length of insn. */
674*3d8817e4Smiod CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
675*3d8817e4Smiod
676*3d8817e4Smiod parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
677*3d8817e4Smiod if (parse_errmsg != NULL)
678*3d8817e4Smiod continue;
679*3d8817e4Smiod
680*3d8817e4Smiod /* ??? 0 is passed for `pc'. */
681*3d8817e4Smiod insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
682*3d8817e4Smiod (bfd_vma) 0);
683*3d8817e4Smiod if (insert_errmsg != NULL)
684*3d8817e4Smiod continue;
685*3d8817e4Smiod
686*3d8817e4Smiod /* It is up to the caller to actually output the insn and any
687*3d8817e4Smiod queued relocs. */
688*3d8817e4Smiod return insn;
689*3d8817e4Smiod }
690*3d8817e4Smiod
691*3d8817e4Smiod {
692*3d8817e4Smiod static char errbuf[150];
693*3d8817e4Smiod #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
694*3d8817e4Smiod const char *tmp_errmsg;
695*3d8817e4Smiod
696*3d8817e4Smiod /* If requesting verbose error messages, use insert_errmsg.
697*3d8817e4Smiod Failing that, use parse_errmsg. */
698*3d8817e4Smiod tmp_errmsg = (insert_errmsg ? insert_errmsg :
699*3d8817e4Smiod parse_errmsg ? parse_errmsg :
700*3d8817e4Smiod recognized_mnemonic ?
701*3d8817e4Smiod _("unrecognized form of instruction") :
702*3d8817e4Smiod _("unrecognized instruction"));
703*3d8817e4Smiod
704*3d8817e4Smiod if (strlen (start) > 50)
705*3d8817e4Smiod /* xgettext:c-format */
706*3d8817e4Smiod sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
707*3d8817e4Smiod else
708*3d8817e4Smiod /* xgettext:c-format */
709*3d8817e4Smiod sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
710*3d8817e4Smiod #else
711*3d8817e4Smiod if (strlen (start) > 50)
712*3d8817e4Smiod /* xgettext:c-format */
713*3d8817e4Smiod sprintf (errbuf, _("bad instruction `%.50s...'"), start);
714*3d8817e4Smiod else
715*3d8817e4Smiod /* xgettext:c-format */
716*3d8817e4Smiod sprintf (errbuf, _("bad instruction `%.50s'"), start);
717*3d8817e4Smiod #endif
718*3d8817e4Smiod
719*3d8817e4Smiod *errmsg = errbuf;
720*3d8817e4Smiod return NULL;
721*3d8817e4Smiod }
722*3d8817e4Smiod }
723