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