2004-02-19 Aldy Hernandez <aldyh@redhat.com>
[deliverable/binutils-gdb.git] / gas / itbl-ops.c
CommitLineData
252b5132 1/* itbl-ops.c
3438adb3 2 Copyright 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
252b5132
RH
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21/*======================================================================*/
22/*
23 * Herein lies the support for dynamic specification of processor
24 * instructions and registers. Mnemonics, values, and formats for each
25 * instruction and register are specified in an ascii file consisting of
26 * table entries. The grammar for the table is defined in the document
27 * "Processor instruction table specification".
28 *
29 * Instructions use the gnu assembler syntax, with the addition of
30 * allowing mnemonics for register.
31 * Eg. "func $2,reg3,0x100,symbol ; comment"
32 * func - opcode name
33 * $n - register n
34 * reg3 - mnemonic for processor's register defined in table
35 * 0xddd..d - immediate value
36 * symbol - address of label or external symbol
37 *
38 * First, itbl_parse reads in the table of register and instruction
39 * names and formats, and builds a list of entries for each
40 * processor/type combination. lex and yacc are used to parse
41 * the entries in the table and call functions defined here to
42 * add each entry to our list.
43 *
44 * Then, when assembling or disassembling, these functions are called to
45 * 1) get information on a processor's registers and
46 * 2) assemble/disassemble an instruction.
47 * To assemble(disassemble) an instruction, the function
48 * itbl_assemble(itbl_disassemble) is called to search the list of
49 * instruction entries, and if a match is found, uses the format
50 * described in the instruction entry structure to complete the action.
51 *
52 * Eg. Suppose we have a Mips coprocessor "cop3" with data register "d2"
53 * and we want to define function "pig" which takes two operands.
54 *
55 * Given the table entries:
56 * "p3 insn pig 0x1:24-21 dreg:20-16 immed:15-0"
57 * "p3 dreg d2 0x2"
58 * and that the instruction encoding for coprocessor pz has encoding:
59 * #define MIPS_ENCODE_COP_NUM(z) ((0x21|(z<<1))<<25)
60 * #define ITBL_ENCODE_PNUM(pnum) MIPS_ENCODE_COP_NUM(pnum)
61 *
62 * a structure to describe the instruction might look something like:
63 * struct itbl_entry = {
64 * e_processor processor = e_p3
65 * e_type type = e_insn
66 * char *name = "pig"
67 * uint value = 0x1
68 * uint flags = 0
69 * struct itbl_range range = 24-21
70 * struct itbl_field *field = {
71 * e_type type = e_dreg
72 * struct itbl_range range = 20-16
73 * struct itbl_field *next = {
74 * e_type type = e_immed
75 * struct itbl_range range = 15-0
76 * struct itbl_field *next = 0
77 * };
78 * };
79 * struct itbl_entry *next = 0
80 * };
81 *
82 * And the assembler instructions:
83 * "pig d2,0x100"
84 * "pig $2,0x100"
85 *
86 * would both assemble to the hex value:
87 * "0x4e220100"
88 *
89 */
90
91#include <stdio.h>
92#include <stdlib.h>
93#include <string.h>
94#include "itbl-ops.h"
3d82a647 95#include <itbl-parse.h>
252b5132
RH
96
97/* #define DEBUG */
98
99#ifdef DEBUG
100#include <assert.h>
101#define ASSERT(x) assert(x)
102#define DBG(x) printf x
103#else
104#define ASSERT(x)
105#define DBG(x)
106#endif
107
108#ifndef min
109#define min(a,b) (a<b?a:b)
110#endif
111
112int itbl_have_entries = 0;
113
114/*======================================================================*/
115/* structures for keeping itbl format entries */
116
ef99799a
KH
117struct itbl_range {
118 int sbit; /* mask starting bit position */
119 int ebit; /* mask ending bit position */
120};
121
122struct itbl_field {
123 e_type type; /* dreg/creg/greg/immed/symb */
124 struct itbl_range range; /* field's bitfield range within instruction */
125 unsigned long flags; /* field flags */
126 struct itbl_field *next; /* next field in list */
127};
252b5132 128
252b5132
RH
129/* These structures define the instructions and registers for a processor.
130 * If the type is an instruction, the structure defines the format of an
131 * instruction where the fields are the list of operands.
132 * The flags field below uses the same values as those defined in the
c488923f 133 * gnu assembler and are machine specific. */
ef99799a
KH
134struct itbl_entry {
135 e_processor processor; /* processor number */
136 e_type type; /* dreg/creg/greg/insn */
137 char *name; /* mnemionic name for insn/register */
138 unsigned long value; /* opcode/instruction mask/register number */
139 unsigned long flags; /* effects of the instruction */
140 struct itbl_range range; /* bit range within instruction for value */
141 struct itbl_field *fields; /* list of operand definitions (if any) */
142 struct itbl_entry *next; /* next entry */
143};
252b5132 144
252b5132
RH
145/* local data and structures */
146
147static int itbl_num_opcodes = 0;
148/* Array of entries for each processor and entry type */
ef99799a 149static struct itbl_entry *entries[e_nprocs][e_ntypes] = {
252b5132
RH
150 {0, 0, 0, 0, 0, 0},
151 {0, 0, 0, 0, 0, 0},
152 {0, 0, 0, 0, 0, 0},
153 {0, 0, 0, 0, 0, 0}
154};
155
156/* local prototypes */
b1f1fa96
KH
157static unsigned long build_opcode (struct itbl_entry *e);
158static e_type get_type (int yytype);
159static e_processor get_processor (int yyproc);
160static struct itbl_entry **get_entries (e_processor processor,
161 e_type type);
162static struct itbl_entry *find_entry_byname (e_processor processor,
163 e_type type, char *name);
164static struct itbl_entry *find_entry_byval (e_processor processor,
165 e_type type, unsigned long val, struct itbl_range *r);
166static struct itbl_entry *alloc_entry (e_processor processor,
167 e_type type, char *name, unsigned long value);
168static unsigned long apply_range (unsigned long value, struct itbl_range r);
169static unsigned long extract_range (unsigned long value, struct itbl_range r);
170static struct itbl_field *alloc_field (e_type type, int sbit,
171 int ebit, unsigned long flags);
252b5132 172
252b5132
RH
173/*======================================================================*/
174/* Interfaces to the parser */
175
252b5132
RH
176/* Open the table and use lex and yacc to parse the entries.
177 * Return 1 for failure; 0 for success. */
178
c488923f 179int
252b5132
RH
180itbl_parse (char *insntbl)
181{
182 extern FILE *yyin;
183 extern int yyparse (void);
f740e790
NC
184
185 yyin = fopen (insntbl, FOPEN_RT);
252b5132
RH
186 if (yyin == 0)
187 {
188 printf ("Can't open processor instruction specification file \"%s\"\n",
189 insntbl);
190 return 1;
191 }
f740e790
NC
192
193 while (yyparse ())
194 ;
195
252b5132
RH
196 fclose (yyin);
197 itbl_have_entries = 1;
198 return 0;
199}
200
201/* Add a register entry */
202
203struct itbl_entry *
204itbl_add_reg (int yyprocessor, int yytype, char *regname,
205 int regnum)
206{
c488923f 207#if 0
252b5132
RH
208#include "as.h"
209#include "symbols.h"
210 /* Since register names don't have a prefix, we put them in the symbol table so
211 they can't be used as symbols. This also simplifies argument parsing as
212 we can let gas parse registers for us. The recorded register number is
213 regnum. */
214 /* Use symbol_create here instead of symbol_new so we don't try to
215 output registers into the object file's symbol table. */
216 symbol_table_insert (symbol_create (regname, reg_section,
217 regnum, &zero_address_frag));
218#endif
219 return alloc_entry (get_processor (yyprocessor), get_type (yytype), regname,
220 (unsigned long) regnum);
221}
222
223/* Add an instruction entry */
224
225struct itbl_entry *
226itbl_add_insn (int yyprocessor, char *name, unsigned long value,
227 int sbit, int ebit, unsigned long flags)
228{
229 struct itbl_entry *e;
230 e = alloc_entry (get_processor (yyprocessor), e_insn, name, value);
231 if (e)
232 {
233 e->range.sbit = sbit;
234 e->range.ebit = ebit;
235 e->flags = flags;
236 itbl_num_opcodes++;
237 }
238 return e;
239}
240
241/* Add an operand to an instruction entry */
242
243struct itbl_field *
244itbl_add_operand (struct itbl_entry *e, int yytype, int sbit,
245 int ebit, unsigned long flags)
246{
247 struct itbl_field *f, **last_f;
248 if (!e)
249 return 0;
c488923f 250 /* Add to end of fields' list. */
252b5132
RH
251 f = alloc_field (get_type (yytype), sbit, ebit, flags);
252 if (f)
253 {
254 last_f = &e->fields;
255 while (*last_f)
256 last_f = &(*last_f)->next;
257 *last_f = f;
258 f->next = 0;
259 }
260 return f;
261}
262
252b5132
RH
263/*======================================================================*/
264/* Interfaces for assembler and disassembler */
265
266#ifndef STAND_ALONE
267#include "as.h"
268#include "symbols.h"
269static void append_insns_as_macros (void);
270
ef99799a
KH
271/* Initialize for gas. */
272
c488923f 273void
252b5132
RH
274itbl_init (void)
275{
276 struct itbl_entry *e, **es;
277 e_processor procn;
278 e_type type;
279
280 if (!itbl_have_entries)
ef99799a 281 return;
252b5132
RH
282
283 /* Since register names don't have a prefix, put them in the symbol table so
284 they can't be used as symbols. This simplifies argument parsing as
c488923f 285 we can let gas parse registers for us. */
252b5132
RH
286 /* Use symbol_create instead of symbol_new so we don't try to
287 output registers into the object file's symbol table. */
288
289 for (type = e_regtype0; type < e_nregtypes; type++)
290 for (procn = e_p0; procn < e_nprocs; procn++)
291 {
292 es = get_entries (procn, type);
293 for (e = *es; e; e = e->next)
294 {
295 symbol_table_insert (symbol_create (e->name, reg_section,
ef99799a 296 e->value, &zero_address_frag));
252b5132
RH
297 }
298 }
299 append_insns_as_macros ();
300}
301
c488923f
KH
302/* Append insns to opcodes table and increase number of opcodes
303 * Structure of opcodes table:
252b5132
RH
304 * struct itbl_opcode
305 * {
306 * const char *name;
c488923f
KH
307 * const char *args; - string describing the arguments.
308 * unsigned long match; - opcode, or ISA level if pinfo=INSN_MACRO
309 * unsigned long mask; - opcode mask, or macro id if pinfo=INSN_MACRO
310 * unsigned long pinfo; - insn flags, or INSN_MACRO
252b5132
RH
311 * };
312 * examples:
313 * {"li", "t,i", 0x34000000, 0xffe00000, WR_t },
314 * {"li", "t,I", 0, (int) M_LI, INSN_MACRO },
315 */
316
317static char *form_args (struct itbl_entry *e);
c488923f 318static void
252b5132
RH
319append_insns_as_macros (void)
320{
321 struct ITBL_OPCODE_STRUCT *new_opcodes, *o;
322 struct itbl_entry *e, **es;
323 int n, id, size, new_size, new_num_opcodes;
324
325 if (!itbl_have_entries)
ef99799a 326 return;
252b5132
RH
327
328 if (!itbl_num_opcodes) /* no new instructions to add! */
329 {
330 return;
331 }
332 DBG (("previous num_opcodes=%d\n", ITBL_NUM_OPCODES));
333
334 new_num_opcodes = ITBL_NUM_OPCODES + itbl_num_opcodes;
335 ASSERT (new_num_opcodes >= itbl_num_opcodes);
336
337 size = sizeof (struct ITBL_OPCODE_STRUCT) * ITBL_NUM_OPCODES;
338 ASSERT (size >= 0);
339 DBG (("I get=%d\n", size / sizeof (ITBL_OPCODES[0])));
340
341 new_size = sizeof (struct ITBL_OPCODE_STRUCT) * new_num_opcodes;
342 ASSERT (new_size > size);
343
344 /* FIXME since ITBL_OPCODES culd be a static table,
c488923f 345 we can't realloc or delete the old memory. */
252b5132
RH
346 new_opcodes = (struct ITBL_OPCODE_STRUCT *) malloc (new_size);
347 if (!new_opcodes)
348 {
349 printf (_("Unable to allocate memory for new instructions\n"));
350 return;
351 }
47eebc20 352 if (size) /* copy preexisting opcodes table */
252b5132
RH
353 memcpy (new_opcodes, ITBL_OPCODES, size);
354
355 /* FIXME! some NUMOPCODES are calculated expressions.
c488923f 356 These need to be changed before itbls can be supported. */
252b5132
RH
357
358 id = ITBL_NUM_MACROS; /* begin the next macro id after the last */
359 o = &new_opcodes[ITBL_NUM_OPCODES]; /* append macro to opcodes list */
360 for (n = e_p0; n < e_nprocs; n++)
361 {
362 es = get_entries (n, e_insn);
363 for (e = *es; e; e = e->next)
364 {
365 /* name, args, mask, match, pinfo
366 * {"li", "t,i", 0x34000000, 0xffe00000, WR_t },
367 * {"li", "t,I", 0, (int) M_LI, INSN_MACRO },
368 * Construct args from itbl_fields.
369 */
370 o->name = e->name;
371 o->args = strdup (form_args (e));
372 o->mask = apply_range (e->value, e->range);
47eebc20 373 /* FIXME how to catch during assembly? */
252b5132
RH
374 /* mask to identify this insn */
375 o->match = apply_range (e->value, e->range);
376 o->pinfo = 0;
377
378#ifdef USE_MACROS
47eebc20 379 o->mask = id++; /* FIXME how to catch during assembly? */
252b5132
RH
380 o->match = 0; /* for macros, the insn_isa number */
381 o->pinfo = INSN_MACRO;
382#endif
383
384 /* Don't add instructions which caused an error */
385 if (o->args)
386 o++;
387 else
388 new_num_opcodes--;
389 }
390 }
391 ITBL_OPCODES = new_opcodes;
392 ITBL_NUM_OPCODES = new_num_opcodes;
393
394 /* FIXME
395 At this point, we can free the entries, as they should have
396 been added to the assembler's tables.
397 Don't free name though, since name is being used by the new
398 opcodes table.
399
c488923f 400 Eventually, we should also free the new opcodes table itself
252b5132
RH
401 on exit.
402 */
403}
404
405static char *
406form_args (struct itbl_entry *e)
407{
408 static char s[31];
409 char c = 0, *p = s;
410 struct itbl_field *f;
411
412 ASSERT (e);
413 for (f = e->fields; f; f = f->next)
414 {
415 switch (f->type)
416 {
417 case e_dreg:
418 c = 'd';
419 break;
420 case e_creg:
421 c = 't';
422 break;
423 case e_greg:
424 c = 's';
425 break;
426 case e_immed:
427 c = 'i';
428 break;
429 case e_addr:
430 c = 'a';
431 break;
432 default:
433 c = 0; /* ignore; unknown field type */
434 }
435 if (c)
436 {
437 if (p != s)
438 *p++ = ',';
439 *p++ = c;
440 }
441 }
442 *p = 0;
443 return s;
444}
445#endif /* !STAND_ALONE */
446
252b5132
RH
447/* Get processor's register name from val */
448
d7ba4a77
ILT
449int
450itbl_get_reg_val (char *name, unsigned long *pval)
252b5132
RH
451{
452 e_type t;
453 e_processor p;
d7ba4a77 454
252b5132 455 for (p = e_p0; p < e_nprocs; p++)
d7ba4a77
ILT
456 {
457 for (t = e_regtype0; t < e_nregtypes; t++)
458 {
459 if (itbl_get_val (p, t, name, pval))
460 return 1;
461 }
462 }
252b5132
RH
463 return 0;
464}
465
466char *
467itbl_get_name (e_processor processor, e_type type, unsigned long val)
468{
469 struct itbl_entry *r;
470 /* type depends on instruction passed */
471 r = find_entry_byval (processor, type, val, 0);
472 if (r)
473 return r->name;
474 else
475 return 0; /* error; invalid operand */
476}
477
478/* Get processor's register value from name */
479
d7ba4a77
ILT
480int
481itbl_get_val (e_processor processor, e_type type, char *name,
482 unsigned long *pval)
252b5132
RH
483{
484 struct itbl_entry *r;
485 /* type depends on instruction passed */
486 r = find_entry_byname (processor, type, name);
d7ba4a77
ILT
487 if (r == NULL)
488 return 0;
489 *pval = r->value;
490 return 1;
252b5132
RH
491}
492
252b5132
RH
493/* Assemble instruction "name" with operands "s".
494 * name - name of instruction
495 * s - operands
496 * returns - long word for assembled instruction */
497
c488923f 498unsigned long
252b5132
RH
499itbl_assemble (char *name, char *s)
500{
501 unsigned long opcode;
3438adb3 502 struct itbl_entry *e = NULL;
252b5132
RH
503 struct itbl_field *f;
504 char *n;
505 int processor;
506
507 if (!name || !*name)
3b37fd66 508 return 0; /* error! must have an opcode name/expr */
252b5132
RH
509
510 /* find entry in list of instructions for all processors */
511 for (processor = 0; processor < e_nprocs; processor++)
512 {
513 e = find_entry_byname (processor, e_insn, name);
514 if (e)
515 break;
516 }
517 if (!e)
ef5c4bfc 518 return 0; /* opcode not in table; invalid instruction */
252b5132
RH
519 opcode = build_opcode (e);
520
521 /* parse opcode's args (if any) */
c488923f 522 for (f = e->fields; f; f = f->next) /* for each arg, ... */
252b5132
RH
523 {
524 struct itbl_entry *r;
525 unsigned long value;
526 if (!s || !*s)
527 return 0; /* error - not enough operands */
528 n = itbl_get_field (&s);
529 /* n should be in form $n or 0xhhh (are symbol names valid?? */
530 switch (f->type)
531 {
532 case e_dreg:
533 case e_creg:
534 case e_greg:
535 /* Accept either a string name
536 * or '$' followed by the register number */
537 if (*n == '$')
538 {
539 n++;
540 value = strtol (n, 0, 10);
541 /* FIXME! could have "0l"... then what?? */
542 if (value == 0 && *n != '0')
543 return 0; /* error; invalid operand */
544 }
545 else
546 {
547 r = find_entry_byname (e->processor, f->type, n);
548 if (r)
549 value = r->value;
550 else
551 return 0; /* error; invalid operand */
552 }
553 break;
554 case e_addr:
555 /* use assembler's symbol table to find symbol */
556 /* FIXME!! Do we need this?
557 if so, what about relocs??
558 my_getExpression (&imm_expr, s);
559 return 0; /-* error; invalid operand *-/
560 break;
561 */
562 /* If not a symbol, fall thru to IMMED */
563 case e_immed:
c488923f 564 if (*n == '0' && *(n + 1) == 'x') /* hex begins 0x... */
252b5132
RH
565 {
566 n += 2;
567 value = strtol (n, 0, 16);
568 /* FIXME! could have "0xl"... then what?? */
569 }
570 else
571 {
572 value = strtol (n, 0, 10);
573 /* FIXME! could have "0l"... then what?? */
574 if (value == 0 && *n != '0')
575 return 0; /* error; invalid operand */
576 }
577 break;
578 default:
579 return 0; /* error; invalid field spec */
580 }
581 opcode |= apply_range (value, f->range);
582 }
583 if (s && *s)
584 return 0; /* error - too many operands */
585 return opcode; /* done! */
586}
587
588/* Disassemble instruction "insn".
589 * insn - instruction
590 * s - buffer to hold disassembled instruction
591 * returns - 1 if succeeded; 0 if failed
592 */
593
c488923f 594int
252b5132
RH
595itbl_disassemble (char *s, unsigned long insn)
596{
597 e_processor processor;
598 struct itbl_entry *e;
599 struct itbl_field *f;
600
601 if (!ITBL_IS_INSN (insn))
ef99799a 602 return 0; /* error */
252b5132
RH
603 processor = get_processor (ITBL_DECODE_PNUM (insn));
604
605 /* find entry in list */
606 e = find_entry_byval (processor, e_insn, insn, 0);
607 if (!e)
ef5c4bfc 608 return 0; /* opcode not in table; invalid instruction */
252b5132
RH
609 strcpy (s, e->name);
610
ef99799a 611 /* Parse insn's args (if any). */
c488923f 612 for (f = e->fields; f; f = f->next) /* for each arg, ... */
252b5132
RH
613 {
614 struct itbl_entry *r;
615 unsigned long value;
616
47eebc20 617 if (f == e->fields) /* First operand is preceded by tab. */
252b5132 618 strcat (s, "\t");
ef99799a 619 else /* ','s separate following operands. */
252b5132
RH
620 strcat (s, ",");
621 value = extract_range (insn, f->range);
622 /* n should be in form $n or 0xhhh (are symbol names valid?? */
623 switch (f->type)
624 {
625 case e_dreg:
626 case e_creg:
627 case e_greg:
628 /* Accept either a string name
ef99799a 629 or '$' followed by the register number. */
252b5132
RH
630 r = find_entry_byval (e->processor, f->type, value, &f->range);
631 if (r)
632 strcat (s, r->name);
633 else
41e60a82 634 sprintf (s, "%s$%lu", s, value);
252b5132
RH
635 break;
636 case e_addr:
ef99799a
KH
637 /* Use assembler's symbol table to find symbol. */
638 /* FIXME!! Do we need this? If so, what about relocs?? */
639 /* If not a symbol, fall through to IMMED. */
252b5132 640 case e_immed:
41e60a82 641 sprintf (s, "%s0x%lx", s, value);
252b5132
RH
642 break;
643 default:
644 return 0; /* error; invalid field spec */
645 }
646 }
ef99799a 647 return 1; /* Done! */
252b5132
RH
648}
649
650/*======================================================================*/
651/*
652 * Local functions for manipulating private structures containing
653 * the names and format for the new instructions and registers
654 * for each processor.
655 */
656
657/* Calculate instruction's opcode and function values from entry */
658
c488923f 659static unsigned long
252b5132
RH
660build_opcode (struct itbl_entry *e)
661{
662 unsigned long opcode;
663
664 opcode = apply_range (e->value, e->range);
665 opcode |= ITBL_ENCODE_PNUM (e->processor);
666 return opcode;
667}
668
669/* Calculate absolute value given the relative value and bit position range
670 * within the instruction.
671 * The range is inclusive where 0 is least significant bit.
672 * A range of { 24, 20 } will have a mask of
673 * bit 3 2 1
674 * pos: 1098 7654 3210 9876 5432 1098 7654 3210
675 * bin: 0000 0001 1111 0000 0000 0000 0000 0000
676 * hex: 0 1 f 0 0 0 0 0
677 * mask: 0x01f00000.
678 */
679
c488923f 680static unsigned long
252b5132
RH
681apply_range (unsigned long rval, struct itbl_range r)
682{
683 unsigned long mask;
684 unsigned long aval;
685 int len = MAX_BITPOS - r.sbit;
686
687 ASSERT (r.sbit >= r.ebit);
688 ASSERT (MAX_BITPOS >= r.sbit);
689 ASSERT (r.ebit >= 0);
690
691 /* create mask by truncating 1s by shifting */
692 mask = 0xffffffff << len;
693 mask = mask >> len;
694 mask = mask >> r.ebit;
695 mask = mask << r.ebit;
696
697 aval = (rval << r.ebit) & mask;
698 return aval;
699}
700
701/* Calculate relative value given the absolute value and bit position range
702 * within the instruction. */
703
c488923f 704static unsigned long
252b5132
RH
705extract_range (unsigned long aval, struct itbl_range r)
706{
707 unsigned long mask;
708 unsigned long rval;
709 int len = MAX_BITPOS - r.sbit;
710
711 /* create mask by truncating 1s by shifting */
712 mask = 0xffffffff << len;
713 mask = mask >> len;
714 mask = mask >> r.ebit;
715 mask = mask << r.ebit;
716
717 rval = (aval & mask) >> r.ebit;
718 return rval;
719}
720
721/* Extract processor's assembly instruction field name from s;
722 * forms are "n args" "n,args" or "n" */
723/* Return next argument from string pointer "s" and advance s.
d7ba4a77 724 * delimiters are " ,()" */
252b5132
RH
725
726char *
727itbl_get_field (char **S)
728{
729 static char n[128];
41e60a82 730 char *s;
252b5132
RH
731 int len;
732
733 s = *S;
734 if (!s || !*s)
735 return 0;
d7ba4a77
ILT
736 /* FIXME: This is a weird set of delimiters. */
737 len = strcspn (s, " \t,()");
252b5132
RH
738 ASSERT (128 > len + 1);
739 strncpy (n, s, len);
740 n[len] = 0;
741 if (s[len] == '\0')
742 s = 0; /* no more args */
743 else
744 s += len + 1; /* advance to next arg */
745
746 *S = s;
747 return n;
748}
749
750/* Search entries for a given processor and type
751 * to find one matching the name "n".
752 * Return a pointer to the entry */
753
754static struct itbl_entry *
755find_entry_byname (e_processor processor,
756 e_type type, char *n)
757{
758 struct itbl_entry *e, **es;
759
760 es = get_entries (processor, type);
c488923f 761 for (e = *es; e; e = e->next) /* for each entry, ... */
252b5132
RH
762 {
763 if (!strcmp (e->name, n))
764 return e;
765 }
766 return 0;
767}
768
769/* Search entries for a given processor and type
770 * to find one matching the value "val" for the range "r".
771 * Return a pointer to the entry.
772 * This function is used for disassembling fields of an instruction.
773 */
774
775static struct itbl_entry *
776find_entry_byval (e_processor processor, e_type type,
777 unsigned long val, struct itbl_range *r)
778{
779 struct itbl_entry *e, **es;
780 unsigned long eval;
781
782 es = get_entries (processor, type);
c488923f 783 for (e = *es; e; e = e->next) /* for each entry, ... */
252b5132
RH
784 {
785 if (processor != e->processor)
786 continue;
787 /* For insns, we might not know the range of the opcode,
788 * so a range of 0 will allow this routine to match against
789 * the range of the entry to be compared with.
790 * This could cause ambiguities.
791 * For operands, we get an extracted value and a range.
792 */
c488923f 793 /* if range is 0, mask val against the range of the compared entry. */
252b5132
RH
794 if (r == 0) /* if no range passed, must be whole 32-bits
795 * so create 32-bit value from entry's range */
796 {
797 eval = apply_range (e->value, e->range);
798 val &= apply_range (0xffffffff, e->range);
799 }
41e60a82
ILT
800 else if ((r->sbit == e->range.sbit && r->ebit == e->range.ebit)
801 || (e->range.sbit == 0 && e->range.ebit == 0))
252b5132
RH
802 {
803 eval = apply_range (e->value, *r);
804 val = apply_range (val, *r);
805 }
806 else
807 continue;
808 if (val == eval)
809 return e;
810 }
811 return 0;
812}
813
c488923f 814/* Return a pointer to the list of entries for a given processor and type. */
252b5132
RH
815
816static struct itbl_entry **
817get_entries (e_processor processor, e_type type)
818{
819 return &entries[processor][type];
820}
821
c488923f 822/* Return an integral value for the processor passed from yyparse. */
252b5132 823
c488923f 824static e_processor
252b5132
RH
825get_processor (int yyproc)
826{
827 /* translate from yacc's processor to enum */
828 if (yyproc >= e_p0 && yyproc < e_nprocs)
829 return (e_processor) yyproc;
830 return e_invproc; /* error; invalid processor */
831}
832
c488923f 833/* Return an integral value for the entry type passed from yyparse. */
252b5132 834
c488923f 835static e_type
252b5132
RH
836get_type (int yytype)
837{
838 switch (yytype)
839 {
840 /* translate from yacc's type to enum */
841 case INSN:
842 return e_insn;
843 case DREG:
844 return e_dreg;
845 case CREG:
846 return e_creg;
847 case GREG:
848 return e_greg;
849 case ADDR:
850 return e_addr;
851 case IMMED:
852 return e_immed;
853 default:
854 return e_invtype; /* error; invalid type */
855 }
856}
857
252b5132
RH
858/* Allocate and initialize an entry */
859
860static struct itbl_entry *
861alloc_entry (e_processor processor, e_type type,
862 char *name, unsigned long value)
863{
864 struct itbl_entry *e, **es;
865 if (!name)
866 return 0;
867 e = (struct itbl_entry *) malloc (sizeof (struct itbl_entry));
868 if (e)
869 {
870 memset (e, 0, sizeof (struct itbl_entry));
871 e->name = (char *) malloc (sizeof (strlen (name)) + 1);
872 if (e->name)
873 strcpy (e->name, name);
874 e->processor = processor;
875 e->type = type;
876 e->value = value;
877 es = get_entries (e->processor, e->type);
878 e->next = *es;
879 *es = e;
880 }
881 return e;
882}
883
884/* Allocate and initialize an entry's field */
885
886static struct itbl_field *
887alloc_field (e_type type, int sbit, int ebit,
888 unsigned long flags)
889{
890 struct itbl_field *f;
891 f = (struct itbl_field *) malloc (sizeof (struct itbl_field));
892 if (f)
893 {
894 memset (f, 0, sizeof (struct itbl_field));
895 f->type = type;
896 f->range.sbit = sbit;
897 f->range.ebit = ebit;
898 f->flags = flags;
899 }
900 return f;
901}
This page took 0.286455 seconds and 4 git commands to generate.