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