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