Automatic date update in version.in
[deliverable/binutils-gdb.git] / opcodes / or1k-asm.c
CommitLineData
4162bb66 1/* DO NOT EDIT! -*- buffer-read-only: t -*- vi:set ro: */
87e6d782
NC
2/* Assembler interface for targets using CGEN. -*- C -*-
3 CGEN: Cpu tools GENerator
4
47b0e7ad
NC
5 THIS FILE IS MACHINE GENERATED WITH CGEN.
6 - the resultant file is machine generated, cgen-asm.in isn't
87e6d782 7
219d1afa 8 Copyright (C) 1996-2018 Free Software Foundation, Inc.
87e6d782 9
9b201bb5 10 This file is part of libopcodes.
87e6d782 11
9b201bb5 12 This library is free software; you can redistribute it and/or modify
47b0e7ad 13 it under the terms of the GNU General Public License as published by
9b201bb5 14 the Free Software Foundation; either version 3, or (at your option)
47b0e7ad 15 any later version.
87e6d782 16
9b201bb5
NC
17 It is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
20 License for more details.
87e6d782 21
47b0e7ad
NC
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. */
87e6d782 25
9b201bb5 26
87e6d782
NC
27/* ??? Eventually more and more of this stuff can go to cpu-independent files.
28 Keep that in mind. */
29
30#include "sysdep.h"
87e6d782
NC
31#include <stdio.h>
32#include "ansidecl.h"
33#include "bfd.h"
34#include "symcat.h"
73589c9d
CS
35#include "or1k-desc.h"
36#include "or1k-opc.h"
87e6d782 37#include "opintl.h"
fc7bc883 38#include "xregex.h"
fc05c67f 39#include "libiberty.h"
37111cc7 40#include "safe-ctype.h"
87e6d782 41
37111cc7 42#undef min
87e6d782 43#define min(a,b) ((a) < (b) ? (a) : (b))
37111cc7 44#undef max
87e6d782
NC
45#define max(a,b) ((a) > (b) ? (a) : (b))
46
47static const char * parse_insn_normal
ffead7ae 48 (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
87e6d782 49\f
37111cc7 50/* -- assembler routines inserted here. */
87e6d782
NC
51
52/* -- asm.c */
53
47b0e7ad 54static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'");
87e6d782 55
47b0e7ad 56#define CGEN_VERBOSE_ASSEMBLER_ERRORS
0e2ee3ca 57
73589c9d
CS
58static const char *
59parse_disp26 (CGEN_CPU_DESC cd,
c151b1c6
AM
60 const char ** strp,
61 int opindex,
62 int opinfo,
63 enum cgen_parse_operand_result * resultp,
64 bfd_vma * valuep)
87e6d782 65{
73589c9d
CS
66 const char *errmsg = NULL;
67 enum cgen_parse_operand_result result_type;
87e6d782 68
73589c9d
CS
69 if (strncasecmp (*strp, "plt(", 4) == 0)
70 {
71 bfd_vma value;
72
73 *strp += 4;
74 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_OR1K_PLT26,
c151b1c6 75 & result_type, & value);
73589c9d 76 if (**strp != ')')
c151b1c6 77 return MISSING_CLOSING_PARENTHESIS;
73589c9d
CS
78 ++*strp;
79 if (errmsg == NULL
c151b1c6
AM
80 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
81 value = (value >> 2) & 0xffff;
73589c9d
CS
82 *valuep = value;
83 return errmsg;
84 }
85 return cgen_parse_address (cd, strp, opindex, opinfo, resultp, valuep);
86}
87e6d782
NC
87
88static const char *
73589c9d 89parse_simm16 (CGEN_CPU_DESC cd, const char ** strp, int opindex, long * valuep)
87e6d782
NC
90{
91 const char *errmsg;
92 enum cgen_parse_operand_result result_type;
73589c9d 93 long ret;
87e6d782
NC
94
95 if (**strp == '#')
96 ++*strp;
97
98 if (strncasecmp (*strp, "hi(", 3) == 0)
99 {
8f807746 100 bfd_vma value;
87e6d782 101
8f807746 102 *strp += 3;
33b71eeb 103 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_HI16,
c151b1c6 104 & result_type, & value);
73589c9d 105 if (**strp != ')')
c151b1c6 106 errmsg = MISSING_CLOSING_PARENTHESIS;
73589c9d
CS
107 ++*strp;
108
109 ret = value;
110
111 if (errmsg == NULL
112 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
113 {
114 ret >>= 16;
115 ret &= 0xffff;
116 ret = (ret ^ 0x8000) - 0x8000;
117 }
118 }
119 else if (strncasecmp (*strp, "lo(", 3) == 0)
120 {
121 bfd_vma value;
122
123 *strp += 3;
124 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LO16,
c151b1c6 125 & result_type, & value);
87e6d782 126 if (**strp != ')')
c151b1c6 127 return MISSING_CLOSING_PARENTHESIS;
73589c9d 128 ++*strp;
c151b1c6 129
73589c9d
CS
130 ret = value;
131
132 if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
133 {
134 ret &= 0xffff;
135 ret = (ret ^ 0x8000) - 0x8000;
136 }
137 }
138 else if (strncasecmp (*strp, "got(", 4) == 0)
139 {
140 bfd_vma value;
8f807746 141
73589c9d
CS
142 *strp += 4;
143 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_OR1K_GOT16,
c151b1c6 144 & result_type, & value);
73589c9d 145 if (**strp != ')')
c151b1c6 146 return MISSING_CLOSING_PARENTHESIS;
87e6d782
NC
147 ++*strp;
148 if (errmsg == NULL
c151b1c6
AM
149 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
150 value &= 0xffff;
73589c9d
CS
151 *valuep = value;
152 return errmsg;
87e6d782 153 }
73589c9d 154 else if (strncasecmp (*strp, "gotpchi(", 8) == 0)
87e6d782 155 {
73589c9d 156 bfd_vma value;
33b71eeb 157
73589c9d
CS
158 *strp += 8;
159 errmsg = cgen_parse_address (cd, strp, opindex,
c151b1c6
AM
160 BFD_RELOC_OR1K_GOTPC_HI16,
161 & result_type, & value);
73589c9d 162 if (**strp != ')')
c151b1c6 163 return MISSING_CLOSING_PARENTHESIS;
73589c9d
CS
164 ++*strp;
165 if (errmsg == NULL
c151b1c6
AM
166 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
167 value = (value >> 16) & 0xffff;
73589c9d
CS
168 *valuep = value;
169 return errmsg;
170 }
171 else if (strncasecmp (*strp, "gotpclo(", 8) == 0)
172 {
173 bfd_vma value;
33b71eeb 174
73589c9d
CS
175 *strp += 8;
176 errmsg = cgen_parse_address (cd, strp, opindex,
c151b1c6
AM
177 BFD_RELOC_OR1K_GOTPC_LO16,
178 &result_type, &value);
73589c9d 179 if (**strp != ')')
c151b1c6 180 return MISSING_CLOSING_PARENTHESIS;
73589c9d
CS
181 ++*strp;
182 if (errmsg == NULL
c151b1c6
AM
183 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
184 value &= 0xffff;
73589c9d
CS
185 *valuep = value;
186 return errmsg;
87e6d782 187 }
73589c9d
CS
188 else if (strncasecmp (*strp, "gotoffhi(", 9) == 0)
189 {
190 bfd_vma value;
8f807746 191
73589c9d
CS
192 *strp += 9;
193 errmsg = cgen_parse_address (cd, strp, opindex,
c151b1c6
AM
194 BFD_RELOC_OR1K_GOTOFF_HI16,
195 & result_type, & value);
87e6d782 196
73589c9d 197 if (**strp != ')')
c151b1c6 198 return MISSING_CLOSING_PARENTHESIS;
73589c9d
CS
199 ++*strp;
200 if (errmsg == NULL
c151b1c6
AM
201 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
202 value = (value >> 16) & 0xffff;
73589c9d
CS
203 *valuep = value;
204 return errmsg;
205 }
206 else if (strncasecmp (*strp, "gotofflo(", 9) == 0)
207 {
208 bfd_vma value;
87e6d782 209
73589c9d
CS
210 *strp += 9;
211 errmsg = cgen_parse_address (cd, strp, opindex,
c151b1c6
AM
212 BFD_RELOC_OR1K_GOTOFF_LO16,
213 &result_type, &value);
73589c9d 214 if (**strp != ')')
c151b1c6 215 return MISSING_CLOSING_PARENTHESIS;
73589c9d
CS
216 ++*strp;
217 if (errmsg == NULL
c151b1c6
AM
218 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
219 value &= 0xffff;
73589c9d
CS
220 *valuep = value;
221 return errmsg;
222 }
223 else if (strncasecmp (*strp, "tlsgdhi(", 8) == 0)
224 {
225 bfd_vma value;
87e6d782 226
73589c9d
CS
227 *strp += 8;
228 errmsg = cgen_parse_address (cd, strp, opindex,
c151b1c6
AM
229 BFD_RELOC_OR1K_TLS_GD_HI16,
230 & result_type, & value);
87e6d782 231
73589c9d 232 if (**strp != ')')
c151b1c6 233 return MISSING_CLOSING_PARENTHESIS;
73589c9d
CS
234 ++*strp;
235 if (errmsg == NULL
c151b1c6
AM
236 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
237 value = (value >> 16) & 0xffff;
73589c9d
CS
238 *valuep = value;
239 return errmsg;
240 }
241 else if (strncasecmp (*strp, "tlsgdlo(", 8) == 0)
87e6d782 242 {
8f807746 243 bfd_vma value;
87e6d782 244
73589c9d
CS
245 *strp += 8;
246 errmsg = cgen_parse_address (cd, strp, opindex,
c151b1c6
AM
247 BFD_RELOC_OR1K_TLS_GD_LO16,
248 &result_type, &value);
73589c9d 249 if (**strp != ')')
c151b1c6 250 return MISSING_CLOSING_PARENTHESIS;
73589c9d
CS
251 ++*strp;
252 if (errmsg == NULL
c151b1c6
AM
253 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
254 value &= 0xffff;
73589c9d
CS
255 *valuep = value;
256 return errmsg;
257 }
258 else if (strncasecmp (*strp, "tlsldmhi(", 9) == 0)
259 {
260 bfd_vma value;
261
262 *strp += 9;
263 errmsg = cgen_parse_address (cd, strp, opindex,
c151b1c6
AM
264 BFD_RELOC_OR1K_TLS_LDM_HI16,
265 & result_type, & value);
73589c9d 266
87e6d782 267 if (**strp != ')')
c151b1c6 268 return MISSING_CLOSING_PARENTHESIS;
73589c9d
CS
269 ++*strp;
270 if (errmsg == NULL
c151b1c6
AM
271 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
272 value = (value >> 16) & 0xffff;
73589c9d
CS
273 *valuep = value;
274 return errmsg;
275 }
276 else if (strncasecmp (*strp, "tlsldmlo(", 9) == 0)
277 {
278 bfd_vma value;
87e6d782 279
73589c9d
CS
280 *strp += 9;
281 errmsg = cgen_parse_address (cd, strp, opindex,
c151b1c6
AM
282 BFD_RELOC_OR1K_TLS_LDM_LO16,
283 &result_type, &value);
73589c9d 284 if (**strp != ')')
c151b1c6 285 return MISSING_CLOSING_PARENTHESIS;
8f807746 286 ++*strp;
73589c9d 287 if (errmsg == NULL
c151b1c6
AM
288 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
289 value &= 0xffff;
73589c9d
CS
290 *valuep = value;
291 return errmsg;
87e6d782 292 }
73589c9d 293 else if (strncasecmp (*strp, "dtpoffhi(", 9) == 0)
8f807746 294 {
73589c9d 295 bfd_vma value;
33b71eeb 296
73589c9d
CS
297 *strp += 9;
298 errmsg = cgen_parse_address (cd, strp, opindex,
c151b1c6
AM
299 BFD_RELOC_OR1K_TLS_LDO_HI16,
300 & result_type, & value);
33b71eeb 301
73589c9d 302 if (**strp != ')')
c151b1c6 303 return MISSING_CLOSING_PARENTHESIS;
73589c9d
CS
304 ++*strp;
305 if (errmsg == NULL
c151b1c6
AM
306 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
307 value = (value >> 16) & 0xffff;
73589c9d
CS
308 *valuep = value;
309 return errmsg;
310 }
311 else if (strncasecmp (*strp, "dtpofflo(", 9) == 0)
312 {
313 bfd_vma value;
314
315 *strp += 9;
316 errmsg = cgen_parse_address (cd, strp, opindex,
c151b1c6
AM
317 BFD_RELOC_OR1K_TLS_LDO_LO16,
318 &result_type, &value);
73589c9d 319 if (**strp != ')')
c151b1c6 320 return MISSING_CLOSING_PARENTHESIS;
73589c9d
CS
321 ++*strp;
322 if (errmsg == NULL
c151b1c6
AM
323 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
324 value &= 0xffff;
73589c9d
CS
325 *valuep = value;
326 return errmsg;
8f807746 327 }
73589c9d
CS
328 else if (strncasecmp (*strp, "gottpoffhi(", 11) == 0)
329 {
330 bfd_vma value;
8f807746 331
73589c9d
CS
332 *strp += 11;
333 errmsg = cgen_parse_address (cd, strp, opindex,
c151b1c6
AM
334 BFD_RELOC_OR1K_TLS_IE_HI16,
335 & result_type, & value);
73589c9d
CS
336
337 if (**strp != ')')
c151b1c6 338 return MISSING_CLOSING_PARENTHESIS;
73589c9d
CS
339 ++*strp;
340 if (errmsg == NULL
c151b1c6
AM
341 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
342 value = (value >> 16) & 0xffff;
73589c9d
CS
343 *valuep = value;
344 return errmsg;
345 }
346 else if (strncasecmp (*strp, "gottpofflo(", 11) == 0)
347 {
348 bfd_vma value;
349
350 *strp += 11;
351 errmsg = cgen_parse_address (cd, strp, opindex,
c151b1c6
AM
352 BFD_RELOC_OR1K_TLS_IE_LO16,
353 &result_type, &value);
73589c9d 354 if (**strp != ')')
c151b1c6 355 return MISSING_CLOSING_PARENTHESIS;
73589c9d
CS
356 ++*strp;
357 if (errmsg == NULL
c151b1c6
AM
358 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
359 value &= 0xffff;
73589c9d
CS
360 *valuep = value;
361 return errmsg;
362 }
363 else if (strncasecmp (*strp, "tpoffhi(", 8) == 0)
364 {
365 bfd_vma value;
366
367 *strp += 8;
368 errmsg = cgen_parse_address (cd, strp, opindex,
c151b1c6
AM
369 BFD_RELOC_OR1K_TLS_LE_HI16,
370 & result_type, & value);
73589c9d
CS
371
372 if (**strp != ')')
c151b1c6 373 return MISSING_CLOSING_PARENTHESIS;
73589c9d
CS
374 ++*strp;
375 if (errmsg == NULL
c151b1c6
AM
376 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
377 value = (value >> 16) & 0xffff;
73589c9d
CS
378 *valuep = value;
379 return errmsg;
380 }
381 else if (strncasecmp (*strp, "tpofflo(", 8) == 0)
382 {
383 bfd_vma value;
384
385 *strp += 8;
386 errmsg = cgen_parse_address (cd, strp, opindex,
c151b1c6
AM
387 BFD_RELOC_OR1K_TLS_LE_LO16,
388 &result_type, &value);
73589c9d 389 if (**strp != ')')
c151b1c6 390 return MISSING_CLOSING_PARENTHESIS;
73589c9d
CS
391 ++*strp;
392 if (errmsg == NULL
c151b1c6
AM
393 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
394 value &= 0xffff;
73589c9d
CS
395 *valuep = value;
396 return errmsg;
397 }
398 else
399 {
400 long value;
401 errmsg = cgen_parse_signed_integer (cd, strp, opindex, &value);
402 ret = value;
403 }
404
405 if (errmsg == NULL)
406 *valuep = ret;
407
408 return errmsg;
409}
410
411static const char *
412parse_uimm16 (CGEN_CPU_DESC cd, const char ** strp, int opindex, unsigned long * valuep)
413{
414 const char *errmsg = parse_simm16(cd, strp, opindex, (long *) valuep);
c151b1c6 415
73589c9d
CS
416 if (errmsg == NULL)
417 *valuep &= 0xffff;
87e6d782
NC
418 return errmsg;
419}
420
421/* -- */
422
73589c9d 423const char * or1k_cgen_parse_operand
47b0e7ad 424 (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
0e2ee3ca 425
87e6d782
NC
426/* Main entry point for operand parsing.
427
428 This function is basically just a big switch statement. Earlier versions
429 used tables to look up the function to use, but
430 - if the table contains both assembler and disassembler functions then
431 the disassembler contains much of the assembler and vice-versa,
432 - there's a lot of inlining possibilities as things grow,
433 - using a switch statement avoids the function call overhead.
434
435 This function could be moved into `parse_insn_normal', but keeping it
436 separate makes clear the interface between `parse_insn_normal' and each of
9a2e995d 437 the handlers. */
87e6d782
NC
438
439const char *
73589c9d 440or1k_cgen_parse_operand (CGEN_CPU_DESC cd,
47b0e7ad
NC
441 int opindex,
442 const char ** strp,
443 CGEN_FIELDS * fields)
87e6d782
NC
444{
445 const char * errmsg = NULL;
446 /* Used by scalar operands that still need to be parsed. */
fc05c67f 447 long junk ATTRIBUTE_UNUSED;
87e6d782
NC
448
449 switch (opindex)
450 {
73589c9d 451 case OR1K_OPERAND_DISP26 :
87e6d782 452 {
9494d739 453 bfd_vma value = 0;
73589c9d 454 errmsg = parse_disp26 (cd, strp, OR1K_OPERAND_DISP26, 0, NULL, & value);
87e6d782
NC
455 fields->f_disp26 = value;
456 }
457 break;
73589c9d
CS
458 case OR1K_OPERAND_RA :
459 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_gpr, & fields->f_r2);
460 break;
461 case OR1K_OPERAND_RADF :
462 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fdr, & fields->f_r1);
87e6d782 463 break;
73589c9d
CS
464 case OR1K_OPERAND_RASF :
465 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fsr, & fields->f_r2);
87e6d782 466 break;
73589c9d
CS
467 case OR1K_OPERAND_RB :
468 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_gpr, & fields->f_r3);
87e6d782 469 break;
73589c9d
CS
470 case OR1K_OPERAND_RBDF :
471 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fdr, & fields->f_r1);
87e6d782 472 break;
73589c9d
CS
473 case OR1K_OPERAND_RBSF :
474 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fsr, & fields->f_r3);
87e6d782 475 break;
73589c9d
CS
476 case OR1K_OPERAND_RD :
477 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_gpr, & fields->f_r1);
87e6d782 478 break;
73589c9d
CS
479 case OR1K_OPERAND_RDDF :
480 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fdr, & fields->f_r1);
87e6d782 481 break;
73589c9d
CS
482 case OR1K_OPERAND_RDSF :
483 errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fsr, & fields->f_r1);
87e6d782 484 break;
73589c9d
CS
485 case OR1K_OPERAND_SIMM16 :
486 errmsg = parse_simm16 (cd, strp, OR1K_OPERAND_SIMM16, (long *) (& fields->f_simm16));
87e6d782 487 break;
73589c9d
CS
488 case OR1K_OPERAND_SIMM16_SPLIT :
489 errmsg = parse_simm16 (cd, strp, OR1K_OPERAND_SIMM16_SPLIT, (long *) (& fields->f_simm16_split));
87e6d782 490 break;
73589c9d
CS
491 case OR1K_OPERAND_UIMM16 :
492 errmsg = parse_uimm16 (cd, strp, OR1K_OPERAND_UIMM16, (unsigned long *) (& fields->f_uimm16));
493 break;
494 case OR1K_OPERAND_UIMM16_SPLIT :
495 errmsg = parse_uimm16 (cd, strp, OR1K_OPERAND_UIMM16_SPLIT, (unsigned long *) (& fields->f_uimm16_split));
496 break;
497 case OR1K_OPERAND_UIMM6 :
498 errmsg = cgen_parse_unsigned_integer (cd, strp, OR1K_OPERAND_UIMM6, (unsigned long *) (& fields->f_uimm6));
87e6d782
NC
499 break;
500
501 default :
502 /* xgettext:c-format */
503 fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
504 abort ();
505 }
506
507 return errmsg;
508}
509
43e65147 510cgen_parse_fn * const or1k_cgen_parse_handlers[] =
87e6d782
NC
511{
512 parse_insn_normal,
513};
514
515void
73589c9d 516or1k_cgen_init_asm (CGEN_CPU_DESC cd)
87e6d782 517{
73589c9d
CS
518 or1k_cgen_init_opcode_table (cd);
519 or1k_cgen_init_ibld_table (cd);
520 cd->parse_handlers = & or1k_cgen_parse_handlers[0];
521 cd->parse_operand = or1k_cgen_parse_operand;
1620f33d
AM
522#ifdef CGEN_ASM_INIT_HOOK
523CGEN_ASM_INIT_HOOK
524#endif
87e6d782
NC
525}
526
fc7bc883
RH
527\f
528
37111cc7 529/* Regex construction routine.
fc7bc883 530
37111cc7
NC
531 This translates an opcode syntax string into a regex string,
532 by replacing any non-character syntax element (such as an
533 opcode) with the pattern '.*'
fc7bc883 534
37111cc7 535 It then compiles the regex and stores it in the opcode, for
73589c9d 536 later use by or1k_cgen_assemble_insn
fc7bc883 537
37111cc7 538 Returns NULL for success, an error message for failure. */
fc7bc883 539
43e65147 540char *
73589c9d 541or1k_cgen_build_insn_regex (CGEN_INSN *insn)
43e65147 542{
fc05c67f 543 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
fc7bc883 544 const char *mnem = CGEN_INSN_MNEMONIC (insn);
fc7bc883
RH
545 char rxbuf[CGEN_MAX_RX_ELEMENTS];
546 char *rx = rxbuf;
547 const CGEN_SYNTAX_CHAR_TYPE *syn;
548 int reg_err;
549
550 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
551
f3a55c17
NC
552 /* Mnemonics come first in the syntax string. */
553 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
554 return _("missing mnemonic in syntax string");
fc7bc883
RH
555 ++syn;
556
f3a55c17
NC
557 /* Generate a case sensitive regular expression that emulates case
558 insensitive matching in the "C" locale. We cannot generate a case
559 insensitive regular expression because in Turkish locales, 'i' and 'I'
560 are not equal modulo case conversion. */
fc7bc883 561
f3a55c17
NC
562 /* Copy the literal mnemonic out of the insn. */
563 for (; *mnem; mnem++)
564 {
565 char c = *mnem;
566
567 if (ISALPHA (c))
568 {
569 *rx++ = '[';
570 *rx++ = TOLOWER (c);
571 *rx++ = TOUPPER (c);
572 *rx++ = ']';
573 }
574 else
575 *rx++ = c;
576 }
577
578 /* Copy any remaining literals from the syntax string into the rx. */
579 for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
fc7bc883 580 {
43e65147 581 if (CGEN_SYNTAX_CHAR_P (* syn))
fc7bc883 582 {
f3a55c17
NC
583 char c = CGEN_SYNTAX_CHAR (* syn);
584
43e65147 585 switch (c)
f3a55c17
NC
586 {
587 /* Escape any regex metacharacters in the syntax. */
43e65147
L
588 case '.': case '[': case '\\':
589 case '*': case '^': case '$':
fc7bc883
RH
590
591#ifdef CGEN_ESCAPE_EXTENDED_REGEX
43e65147 592 case '?': case '{': case '}':
f3a55c17
NC
593 case '(': case ')': case '*':
594 case '|': case '+': case ']':
fc7bc883 595#endif
f3a55c17
NC
596 *rx++ = '\\';
597 *rx++ = c;
598 break;
599
600 default:
601 if (ISALPHA (c))
602 {
603 *rx++ = '[';
604 *rx++ = TOLOWER (c);
605 *rx++ = TOUPPER (c);
606 *rx++ = ']';
607 }
608 else
609 *rx++ = c;
610 break;
611 }
fc7bc883
RH
612 }
613 else
614 {
f3a55c17
NC
615 /* Replace non-syntax fields with globs. */
616 *rx++ = '.';
617 *rx++ = '*';
fc7bc883
RH
618 }
619 }
620
f3a55c17 621 /* Trailing whitespace ok. */
43e65147
L
622 * rx++ = '[';
623 * rx++ = ' ';
624 * rx++ = '\t';
625 * rx++ = ']';
626 * rx++ = '*';
fc7bc883 627
f3a55c17 628 /* But anchor it after that. */
43e65147 629 * rx++ = '$';
fc7bc883
RH
630 * rx = '\0';
631
632 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
f3a55c17 633 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
fc7bc883 634
43e65147 635 if (reg_err == 0)
fc7bc883
RH
636 return NULL;
637 else
638 {
639 static char msg[80];
f3a55c17 640
fc7bc883
RH
641 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
642 regfree ((regex_t *) CGEN_INSN_RX (insn));
643 free (CGEN_INSN_RX (insn));
644 (CGEN_INSN_RX (insn)) = NULL;
37111cc7 645 return msg;
fc7bc883
RH
646 }
647}
648
87e6d782
NC
649\f
650/* Default insn parser.
651
652 The syntax string is scanned and operands are parsed and stored in FIELDS.
653 Relocs are queued as we go via other callbacks.
654
655 ??? Note that this is currently an all-or-nothing parser. If we fail to
656 parse the instruction, we return 0 and the caller will start over from
657 the beginning. Backtracking will be necessary in parsing subexpressions,
658 but that can be handled there. Not handling backtracking here may get
659 expensive in the case of the m68k. Deal with later.
660
f3a55c17 661 Returns NULL for success, an error message for failure. */
87e6d782
NC
662
663static const char *
ffead7ae
MM
664parse_insn_normal (CGEN_CPU_DESC cd,
665 const CGEN_INSN *insn,
666 const char **strp,
667 CGEN_FIELDS *fields)
87e6d782
NC
668{
669 /* ??? Runtime added insns not handled yet. */
670 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
671 const char *str = *strp;
672 const char *errmsg;
673 const char *p;
674 const CGEN_SYNTAX_CHAR_TYPE * syn;
675#ifdef CGEN_MNEMONIC_OPERANDS
676 /* FIXME: wip */
677 int past_opcode_p;
678#endif
679
680 /* For now we assume the mnemonic is first (there are no leading operands).
681 We can parse it without needing to set up operand parsing.
682 GAS's input scrubber will ensure mnemonics are lowercase, but we may
683 not be called from GAS. */
684 p = CGEN_INSN_MNEMONIC (insn);
37111cc7 685 while (*p && TOLOWER (*p) == TOLOWER (*str))
87e6d782
NC
686 ++p, ++str;
687
688 if (* p)
689 return _("unrecognized instruction");
690
691#ifndef CGEN_MNEMONIC_OPERANDS
37111cc7 692 if (* str && ! ISSPACE (* str))
87e6d782
NC
693 return _("unrecognized instruction");
694#endif
695
696 CGEN_INIT_PARSE (cd);
697 cgen_init_parse_operand (cd);
698#ifdef CGEN_MNEMONIC_OPERANDS
699 past_opcode_p = 0;
700#endif
701
702 /* We don't check for (*str != '\0') here because we want to parse
703 any trailing fake arguments in the syntax string. */
704 syn = CGEN_SYNTAX_STRING (syntax);
705
706 /* Mnemonics come first for now, ensure valid string. */
707 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
708 abort ();
709
710 ++syn;
711
712 while (* syn != 0)
713 {
714 /* Non operand chars must match exactly. */
715 if (CGEN_SYNTAX_CHAR_P (* syn))
716 {
717 /* FIXME: While we allow for non-GAS callers above, we assume the
718 first char after the mnemonic part is a space. */
719 /* FIXME: We also take inappropriate advantage of the fact that
720 GAS's input scrubber will remove extraneous blanks. */
37111cc7 721 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
87e6d782
NC
722 {
723#ifdef CGEN_MNEMONIC_OPERANDS
724 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
725 past_opcode_p = 1;
726#endif
727 ++ syn;
728 ++ str;
729 }
730 else if (*str)
731 {
732 /* Syntax char didn't match. Can't be this insn. */
733 static char msg [80];
f3a55c17 734
87e6d782
NC
735 /* xgettext:c-format */
736 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
737 CGEN_SYNTAX_CHAR(*syn), *str);
738 return msg;
739 }
740 else
741 {
742 /* Ran out of input. */
743 static char msg [80];
f3a55c17 744
87e6d782
NC
745 /* xgettext:c-format */
746 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
747 CGEN_SYNTAX_CHAR(*syn));
748 return msg;
749 }
750 continue;
751 }
752
c7e2358a
AM
753#ifdef CGEN_MNEMONIC_OPERANDS
754 (void) past_opcode_p;
755#endif
87e6d782 756 /* We have an operand of some sort. */
c7e2358a 757 errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
87e6d782
NC
758 if (errmsg)
759 return errmsg;
760
761 /* Done with this operand, continue with next one. */
762 ++ syn;
763 }
764
765 /* If we're at the end of the syntax string, we're done. */
766 if (* syn == 0)
767 {
768 /* FIXME: For the moment we assume a valid `str' can only contain
769 blanks now. IE: We needn't try again with a longer version of
770 the insn and it is assumed that longer versions of insns appear
771 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
37111cc7 772 while (ISSPACE (* str))
87e6d782
NC
773 ++ str;
774
775 if (* str != '\0')
776 return _("junk at end of line"); /* FIXME: would like to include `str' */
777
778 return NULL;
779 }
780
781 /* We couldn't parse it. */
782 return _("unrecognized instruction");
783}
784\f
785/* Main entry point.
786 This routine is called for each instruction to be assembled.
787 STR points to the insn to be assembled.
788 We assume all necessary tables have been initialized.
789 The assembled instruction, less any fixups, is stored in BUF.
790 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
791 still needs to be converted to target byte order, otherwise BUF is an array
792 of bytes in target byte order.
793 The result is a pointer to the insn's entry in the opcode table,
794 or NULL if an error occured (an error message will have already been
795 printed).
796
797 Note that when processing (non-alias) macro-insns,
798 this function recurses.
799
800 ??? It's possible to make this cpu-independent.
801 One would have to deal with a few minor things.
802 At this point in time doing so would be more of a curiosity than useful
803 [for example this file isn't _that_ big], but keeping the possibility in
804 mind helps keep the design clean. */
805
806const CGEN_INSN *
73589c9d 807or1k_cgen_assemble_insn (CGEN_CPU_DESC cd,
ffead7ae
MM
808 const char *str,
809 CGEN_FIELDS *fields,
810 CGEN_INSN_BYTES_PTR buf,
811 char **errmsg)
87e6d782
NC
812{
813 const char *start;
814 CGEN_INSN_LIST *ilist;
815 const char *parse_errmsg = NULL;
816 const char *insert_errmsg = NULL;
fc7bc883 817 int recognized_mnemonic = 0;
87e6d782
NC
818
819 /* Skip leading white space. */
37111cc7 820 while (ISSPACE (* str))
87e6d782
NC
821 ++ str;
822
823 /* The instructions are stored in hashed lists.
824 Get the first in the list. */
825 ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
826
827 /* Keep looking until we find a match. */
87e6d782
NC
828 start = str;
829 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
830 {
831 const CGEN_INSN *insn = ilist->insn;
fc7bc883 832 recognized_mnemonic = 1;
87e6d782 833
43e65147 834#ifdef CGEN_VALIDATE_INSN_SUPPORTED
f3a55c17
NC
835 /* Not usually needed as unsupported opcodes
836 shouldn't be in the hash lists. */
87e6d782 837 /* Is this insn supported by the selected cpu? */
73589c9d 838 if (! or1k_cgen_insn_supported (cd, insn))
87e6d782
NC
839 continue;
840#endif
b11dcf4e 841 /* If the RELAXED attribute is set, this is an insn that shouldn't be
87e6d782
NC
842 chosen immediately. Instead, it is used during assembler/linker
843 relaxation if possible. */
b11dcf4e 844 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
87e6d782
NC
845 continue;
846
847 str = start;
848
f3a55c17 849 /* Skip this insn if str doesn't look right lexically. */
fc7bc883
RH
850 if (CGEN_INSN_RX (insn) != NULL &&
851 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
852 continue;
853
87e6d782
NC
854 /* Allow parse/insert handlers to obtain length of insn. */
855 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
856
857 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
858 if (parse_errmsg != NULL)
859 continue;
860
f3a55c17 861 /* ??? 0 is passed for `pc'. */
87e6d782
NC
862 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
863 (bfd_vma) 0);
864 if (insert_errmsg != NULL)
865 continue;
866
867 /* It is up to the caller to actually output the insn and any
868 queued relocs. */
869 return insn;
870 }
871
872 {
873 static char errbuf[150];
87e6d782 874 const char *tmp_errmsg;
c7e2358a
AM
875#ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
876#define be_verbose 1
87e6d782 877#else
c7e2358a 878#define be_verbose 0
87e6d782 879#endif
c7e2358a
AM
880
881 if (be_verbose)
882 {
883 /* If requesting verbose error messages, use insert_errmsg.
884 Failing that, use parse_errmsg. */
885 tmp_errmsg = (insert_errmsg ? insert_errmsg :
886 parse_errmsg ? parse_errmsg :
887 recognized_mnemonic ?
888 _("unrecognized form of instruction") :
889 _("unrecognized instruction"));
890
891 if (strlen (start) > 50)
892 /* xgettext:c-format */
893 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
43e65147 894 else
c7e2358a
AM
895 /* xgettext:c-format */
896 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
897 }
898 else
899 {
900 if (strlen (start) > 50)
901 /* xgettext:c-format */
902 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
43e65147 903 else
c7e2358a
AM
904 /* xgettext:c-format */
905 sprintf (errbuf, _("bad instruction `%.50s'"), start);
906 }
43e65147 907
87e6d782
NC
908 *errmsg = errbuf;
909 return NULL;
910 }
911}
This page took 1.175174 seconds and 4 git commands to generate.