itbl-ops.c revision 33965
1193323Sed/* itbl-ops.c 2193323Sed Copyright (C) 1997 Free Software Foundation, Inc. 3193323Sed 4193323Sed This file is part of GAS, the GNU Assembler. 5193323Sed 6193323Sed GAS is free software; you can redistribute it and/or modify 7193323Sed it under the terms of the GNU General Public License as published by 8193323Sed the Free Software Foundation; either version 2, or (at your option) 9193323Sed any later version. 10193323Sed 11193323Sed GAS is distributed in the hope that it will be useful, 12193323Sed but WITHOUT ANY WARRANTY; without even the implied warranty of 13193323Sed MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14193323Sed GNU General Public License for more details. 15193323Sed 16193323Sed You should have received a copy of the GNU General Public License 17203954Srdivacky along with GAS; see the file COPYING. If not, write to the Free 18203954Srdivacky Software Foundation, 59 Temple Place - Suite 330, Boston, MA 19193323Sed 02111-1307, USA. */ 20193323Sed 21193323Sed/*======================================================================*/ 22199481Srdivacky/* 23193323Sed * Herein lies the support for dynamic specification of processor 24203954Srdivacky * instructions and registers. Mnemonics, values, and formats for each 25203954Srdivacky * instruction and register are specified in an ascii file consisting of 26206083Srdivacky * table entries. The grammar for the table is defined in the document 27202375Srdivacky * "Processor instruction table specification". 28193323Sed * 29198090Srdivacky * Instructions use the gnu assembler syntax, with the addition of 30193323Sed * allowing mnemonics for register. 31193323Sed * Eg. "func $2,reg3,0x100,symbol ; comment" 32193323Sed * func - opcode name 33193323Sed * $n - register n 34198892Srdivacky * reg3 - mnemonic for processor's register defined in table 35198892Srdivacky * 0xddd..d - immediate value 36193323Sed * symbol - address of label or external symbol 37193323Sed * 38193323Sed * First, itbl_parse reads in the table of register and instruction 39193323Sed * names and formats, and builds a list of entries for each 40193323Sed * processor/type combination. lex and yacc are used to parse 41193323Sed * the entries in the table and call functions defined here to 42193323Sed * add each entry to our list. 43203954Srdivacky * 44203954Srdivacky * Then, when assembling or disassembling, these functions are called to 45205218Srdivacky * 1) get information on a processor's registers and 46203954Srdivacky * 2) assemble/disassemble an instruction. 47205218Srdivacky * To assemble(disassemble) an instruction, the function 48205218Srdivacky * itbl_assemble(itbl_disassemble) is called to search the list of 49206083Srdivacky * instruction entries, and if a match is found, uses the format 50206083Srdivacky * described in the instruction entry structure to complete the action. 51206083Srdivacky * 52203954Srdivacky * Eg. Suppose we have a Mips coprocessor "cop3" with data register "d2" 53203954Srdivacky * and we want to define function "pig" which takes two operands. 54203954Srdivacky * 55198090Srdivacky * Given the table entries: 56193323Sed * "p3 insn pig 0x1:24-21 dreg:20-16 immed:15-0" 57193323Sed * "p3 dreg d2 0x2" 58193323Sed * and that the instruction encoding for coprocessor pz has encoding: 59193323Sed * #define MIPS_ENCODE_COP_NUM(z) ((0x21|(z<<1))<<25) 60193323Sed * #define ITBL_ENCODE_PNUM(pnum) MIPS_ENCODE_COP_NUM(pnum) 61193323Sed * 62193323Sed * a structure to describe the instruction might look something like: 63193323Sed * struct itbl_entry = { 64193323Sed * e_processor processor = e_p3 65193323Sed * e_type type = e_insn 66193323Sed * char *name = "pig" 67198090Srdivacky * uint value = 0x1 68193323Sed * uint flags = 0 69193323Sed * struct itbl_range range = 24-21 70193323Sed * struct itbl_field *field = { 71193323Sed * e_type type = e_dreg 72193323Sed * struct itbl_range range = 20-16 73193323Sed * struct itbl_field *next = { 74193323Sed * e_type type = e_immed 75193323Sed * struct itbl_range range = 15-0 76193323Sed * struct itbl_field *next = 0 77193323Sed * }; 78193323Sed * }; 79198090Srdivacky * struct itbl_entry *next = 0 80193323Sed * }; 81193323Sed * 82193323Sed * And the assembler instructions: 83193323Sed * "pig d2,0x100" 84193323Sed * "pig $2,0x100" 85193323Sed * 86193323Sed * would both assemble to the hex value: 87193323Sed * "0x4e220100" 88193323Sed * 89198090Srdivacky */ 90193323Sed 91193323Sed#include <stdio.h> 92193323Sed#include <stdlib.h> 93193323Sed#include <string.h> 94193323Sed#include "itbl-ops.h" 95193323Sed#include "itbl-parse.h" 96193323Sed 97193323Sed/* #define DEBUG */ 98193323Sed 99193323Sed#ifdef DEBUG 100193323Sed#include <assert.h> 101193323Sed#define ASSERT(x) assert(x) 102193323Sed#define DBG(x) printf x 103193323Sed#else 104198090Srdivacky#define ASSERT(x) 105193323Sed#define DBG(x) 106193323Sed#endif 107193323Sed 108193323Sed#ifndef min 109193323Sed#define min(a,b) (a<b?a:b) 110193323Sed#endif 111193323Sed 112193323Sedint itbl_have_entries = 0; 113193323Sed 114193323Sed/*======================================================================*/ 115193323Sed/* structures for keeping itbl format entries */ 116193323Sed 117193323Sedstruct itbl_range 118198090Srdivacky { 119198090Srdivacky int sbit; /* mask starting bit position */ 120198090Srdivacky int ebit; /* mask ending bit position */ 121198090Srdivacky }; 122193323Sed 123193323Sedstruct itbl_field 124193323Sed { 125193323Sed e_type type; /* dreg/creg/greg/immed/symb */ 126193323Sed struct itbl_range range; /* field's bitfield range within instruction */ 127193323Sed unsigned long flags; /* field flags */ 128193323Sed struct itbl_field *next; /* next field in list */ 129193323Sed }; 130193323Sed 131193323Sed 132193323Sed/* These structures define the instructions and registers for a processor. 133193323Sed * If the type is an instruction, the structure defines the format of an 134193323Sed * instruction where the fields are the list of operands. 135193323Sed * The flags field below uses the same values as those defined in the 136193323Sed * gnu assembler and are machine specific. */ 137193323Sedstruct itbl_entry 138193323Sed { 139193323Sed e_processor processor; /* processor number */ 140193323Sed e_type type; /* dreg/creg/greg/insn */ 141193323Sed char *name; /* mnemionic name for insn/register */ 142193323Sed unsigned long value; /* opcode/instruction mask/register number */ 143193323Sed unsigned long flags; /* effects of the instruction */ 144193323Sed struct itbl_range range; /* bit range within instruction for value */ 145193323Sed struct itbl_field *fields; /* list of operand definitions (if any) */ 146193323Sed struct itbl_entry *next; /* next entry */ 147193323Sed }; 148202375Srdivacky 149193323Sed 150193323Sed/* local data and structures */ 151198090Srdivacky 152193323Sedstatic int itbl_num_opcodes = 0; 153198892Srdivacky/* Array of entries for each processor and entry type */ 154193323Sedstatic struct itbl_entry *entries[e_nprocs][e_ntypes] = 155193323Sed{ 156193323Sed {0, 0, 0, 0, 0, 0}, 157198892Srdivacky {0, 0, 0, 0, 0, 0}, 158193323Sed {0, 0, 0, 0, 0, 0}, 159193323Sed {0, 0, 0, 0, 0, 0} 160193323Sed}; 161193323Sed 162199989Srdivacky/* local prototypes */ 163199989Srdivackystatic unsigned long build_opcode PARAMS ((struct itbl_entry *e)); 164199989Srdivackystatic e_type get_type PARAMS ((int yytype)); 165199989Srdivackystatic e_processor get_processor PARAMS ((int yyproc)); 166199989Srdivackystatic struct itbl_entry **get_entries PARAMS ((e_processor processor, 167199989Srdivacky e_type type)); 168199989Srdivackystatic struct itbl_entry *find_entry_byname PARAMS ((e_processor processor, 169198090Srdivacky e_type type, char *name)); 170193323Sedstatic struct itbl_entry *find_entry_byval PARAMS ((e_processor processor, 171198090Srdivacky e_type type, unsigned long val, struct itbl_range *r)); 172193323Sedstatic struct itbl_entry *alloc_entry PARAMS ((e_processor processor, 173193323Sed e_type type, char *name, unsigned long value)); 174193323Sedstatic unsigned long apply_range PARAMS ((unsigned long value, 175193323Sed struct itbl_range r)); 176193323Sedstatic unsigned long extract_range PARAMS ((unsigned long value, 177198892Srdivacky struct itbl_range r)); 178198892Srdivackystatic struct itbl_field *alloc_field PARAMS ((e_type type, int sbit, 179198892Srdivacky int ebit, unsigned long flags)); 180198892Srdivacky 181198892Srdivacky 182198892Srdivacky/*======================================================================*/ 183198892Srdivacky/* Interfaces to the parser */ 184198892Srdivacky 185198892Srdivacky 186198892Srdivacky/* Open the table and use lex and yacc to parse the entries. 187198892Srdivacky * Return 1 for failure; 0 for success. */ 188198892Srdivacky 189198090Srdivackyint 190193323Seditbl_parse (char *insntbl) 191193323Sed{ 192193323Sed extern FILE *yyin; 193198892Srdivacky extern int yyparse (void); 194193323Sed yyin = fopen (insntbl, "r"); 195193323Sed if (yyin == 0) 196198090Srdivacky { 197193323Sed printf ("Can't open processor instruction specification file \"%s\"\n", 198193323Sed insntbl); 199193323Sed return 1; 200193323Sed } 201193323Sed else 202198892Srdivacky { 203198090Srdivacky while (yyparse ()); 204193323Sed } 205193323Sed fclose (yyin); 206193323Sed itbl_have_entries = 1; 207198090Srdivacky return 0; 208193323Sed} 209193323Sed 210193323Sed/* Add a register entry */ 211193323Sed 212193323Sedstruct itbl_entry * 213193323Seditbl_add_reg (int yyprocessor, int yytype, char *regname, 214193323Sed int regnum) 215198892Srdivacky{ 216198090Srdivacky#if 0 217193323Sed#include "as.h" 218193323Sed#include "symbols.h" 219193323Sed /* Since register names don't have a prefix, we put them in the symbol table so 220193323Sed they can't be used as symbols. This also simplifies argument parsing as 221193323Sed we can let gas parse registers for us. The recorded register number is 222193323Sed regnum. */ 223193323Sed /* Use symbol_create here instead of symbol_new so we don't try to 224193323Sed output registers into the object file's symbol table. */ 225193323Sed symbol_table_insert (symbol_create (regname, reg_section, 226193323Sed regnum, &zero_address_frag)); 227193323Sed#endif 228193323Sed return alloc_entry (get_processor (yyprocessor), get_type (yytype), regname, 229193323Sed (unsigned long) regnum); 230193323Sed} 231193323Sed 232193323Sed/* Add an instruction entry */ 233193323Sed 234193323Sedstruct itbl_entry * 235193323Seditbl_add_insn (int yyprocessor, char *name, unsigned long value, 236193323Sed int sbit, int ebit, unsigned long flags) 237193323Sed{ 238193323Sed struct itbl_entry *e; 239193323Sed e = alloc_entry (get_processor (yyprocessor), e_insn, name, value); 240199481Srdivacky if (e) 241199481Srdivacky { 242199481Srdivacky e->range.sbit = sbit; 243199481Srdivacky e->range.ebit = ebit; 244193323Sed e->flags = flags; 245199481Srdivacky itbl_num_opcodes++; 246199481Srdivacky } 247199481Srdivacky return e; 248199481Srdivacky} 249199481Srdivacky 250199481Srdivacky/* Add an operand to an instruction entry */ 251199481Srdivacky 252199481Srdivackystruct itbl_field * 253199481Srdivackyitbl_add_operand (struct itbl_entry *e, int yytype, int sbit, 254199481Srdivacky int ebit, unsigned long flags) 255199481Srdivacky{ 256199481Srdivacky struct itbl_field *f, **last_f; 257199481Srdivacky if (!e) 258199481Srdivacky return 0; 259199481Srdivacky /* Add to end of fields' list. */ 260199481Srdivacky f = alloc_field (get_type (yytype), sbit, ebit, flags); 261199481Srdivacky if (f) 262199481Srdivacky { 263199481Srdivacky last_f = &e->fields; 264199481Srdivacky while (*last_f) 265199481Srdivacky last_f = &(*last_f)->next; 266199481Srdivacky *last_f = f; 267199481Srdivacky f->next = 0; 268199481Srdivacky } 269199989Srdivacky return f; 270199989Srdivacky} 271199481Srdivacky 272199481Srdivacky 273199481Srdivacky/*======================================================================*/ 274199481Srdivacky/* Interfaces for assembler and disassembler */ 275199481Srdivacky 276199481Srdivacky#ifndef STAND_ALONE 277199481Srdivacky#include "as.h" 278199481Srdivacky#include "symbols.h" 279199481Srdivackystatic void append_insns_as_macros (void); 280200581Srdivacky 281199481Srdivacky/* initialize for gas */ 282199481Srdivackyvoid 283199989Srdivackyitbl_init (void) 284199989Srdivacky{ 285199989Srdivacky struct itbl_entry *e, **es; 286199989Srdivacky e_processor procn; 287199989Srdivacky e_type type; 288199989Srdivacky 289199481Srdivacky if (!itbl_have_entries) 290199481Srdivacky return; 291199481Srdivacky 292199481Srdivacky /* Since register names don't have a prefix, put them in the symbol table so 293199481Srdivacky they can't be used as symbols. This simplifies argument parsing as 294199481Srdivacky we can let gas parse registers for us. */ 295199481Srdivacky /* Use symbol_create instead of symbol_new so we don't try to 296199481Srdivacky output registers into the object file's symbol table. */ 297199481Srdivacky 298199481Srdivacky for (type = e_regtype0; type < e_nregtypes; type++) 299193323Sed for (procn = e_p0; procn < e_nprocs; procn++) 300193323Sed { 301193323Sed es = get_entries (procn, type); 302193323Sed for (e = *es; e; e = e->next) 303193323Sed { 304193323Sed symbol_table_insert (symbol_create (e->name, reg_section, 305193323Sed e->value, &zero_address_frag)); 306193323Sed } 307193323Sed } 308193323Sed append_insns_as_macros (); 309193323Sed} 310193323Sed 311193323Sed 312193323Sed/* Append insns to opcodes table and increase number of opcodes 313193323Sed * Structure of opcodes table: 314193323Sed * struct itbl_opcode 315193323Sed * { 316193323Sed * const char *name; 317193323Sed * const char *args; - string describing the arguments. 318193323Sed * unsigned long match; - opcode, or ISA level if pinfo=INSN_MACRO 319193323Sed * unsigned long mask; - opcode mask, or macro id if pinfo=INSN_MACRO 320193323Sed * unsigned long pinfo; - insn flags, or INSN_MACRO 321193323Sed * }; 322193323Sed * examples: 323193323Sed * {"li", "t,i", 0x34000000, 0xffe00000, WR_t }, 324193323Sed * {"li", "t,I", 0, (int) M_LI, INSN_MACRO }, 325193323Sed */ 326193323Sed 327193323Sedstatic char *form_args (struct itbl_entry *e); 328193323Sedstatic void 329198090Srdivackyappend_insns_as_macros (void) 330193323Sed{ 331193323Sed struct ITBL_OPCODE_STRUCT *new_opcodes, *o; 332193323Sed struct itbl_entry *e, **es; 333198090Srdivacky int n, id, size, new_size, new_num_opcodes; 334198090Srdivacky 335198090Srdivacky if (!itbl_have_entries) 336198090Srdivacky return; 337198090Srdivacky 338193323Sed if (!itbl_num_opcodes) /* no new instructions to add! */ 339193323Sed { 340193323Sed return; 341193323Sed } 342193323Sed DBG (("previous num_opcodes=%d\n", ITBL_NUM_OPCODES)); 343193323Sed 344193323Sed new_num_opcodes = ITBL_NUM_OPCODES + itbl_num_opcodes; 345193323Sed ASSERT (new_num_opcodes >= itbl_num_opcodes); 346193323Sed 347193323Sed size = sizeof (struct ITBL_OPCODE_STRUCT) * ITBL_NUM_OPCODES; 348193323Sed ASSERT (size >= 0); 349200581Srdivacky DBG (("I get=%d\n", size / sizeof (ITBL_OPCODES[0]))); 350193323Sed 351193323Sed new_size = sizeof (struct ITBL_OPCODE_STRUCT) * new_num_opcodes; 352199989Srdivacky ASSERT (new_size > size); 353199989Srdivacky 354199989Srdivacky /* FIXME since ITBL_OPCODES culd be a static table, 355199989Srdivacky we can't realloc or delete the old memory. */ 356199989Srdivacky new_opcodes = (struct ITBL_OPCODE_STRUCT *) malloc (new_size); 357199989Srdivacky if (!new_opcodes) 358199989Srdivacky { 359199989Srdivacky printf ("Unable to allocate memory for new instructions\n"); 360199989Srdivacky return; 361199989Srdivacky } 362199989Srdivacky if (size) /* copy prexisting opcodes table */ 363200581Srdivacky memcpy (new_opcodes, ITBL_OPCODES, size); 364200581Srdivacky 365200581Srdivacky /* FIXME! some NUMOPCODES are calculated expressions. 366200581Srdivacky These need to be changed before itbls can be supported. */ 367202878Srdivacky 368200581Srdivacky id = ITBL_NUM_MACROS; /* begin the next macro id after the last */ 369200581Srdivacky o = &new_opcodes[ITBL_NUM_OPCODES]; /* append macro to opcodes list */ 370200581Srdivacky for (n = e_p0; n < e_nprocs; n++) 371200581Srdivacky { 372200581Srdivacky es = get_entries (n, e_insn); 373200581Srdivacky for (e = *es; e; e = e->next) 374200581Srdivacky { 375199989Srdivacky /* name, args, mask, match, pinfo 376199989Srdivacky * {"li", "t,i", 0x34000000, 0xffe00000, WR_t }, 377200581Srdivacky * {"li", "t,I", 0, (int) M_LI, INSN_MACRO }, 378199989Srdivacky * Construct args from itbl_fields. 379199989Srdivacky */ 380199989Srdivacky o->name = e->name; 381199989Srdivacky o->args = strdup (form_args (e)); 382199989Srdivacky o->mask = apply_range (e->value, e->range); 383199989Srdivacky /* FIXME how to catch durring assembly? */ 384199989Srdivacky /* mask to identify this insn */ 385199989Srdivacky o->match = apply_range (e->value, e->range); 386199989Srdivacky o->pinfo = 0; 387199989Srdivacky 388199989Srdivacky#ifdef USE_MACROS 389199989Srdivacky o->mask = id++; /* FIXME how to catch durring assembly? */ 390199989Srdivacky o->match = 0; /* for macros, the insn_isa number */ 391199989Srdivacky o->pinfo = INSN_MACRO; 392199989Srdivacky#endif 393199989Srdivacky 394199989Srdivacky /* Don't add instructions which caused an error */ 395199989Srdivacky if (o->args) 396199989Srdivacky o++; 397193323Sed else 398193323Sed new_num_opcodes--; 399193323Sed } 400193323Sed } 401193323Sed ITBL_OPCODES = new_opcodes; 402193323Sed ITBL_NUM_OPCODES = new_num_opcodes; 403193323Sed 404193323Sed /* FIXME 405193323Sed At this point, we can free the entries, as they should have 406193323Sed been added to the assembler's tables. 407193323Sed Don't free name though, since name is being used by the new 408193323Sed opcodes table. 409193323Sed 410193323Sed Eventually, we should also free the new opcodes table itself 411193323Sed on exit. 412193323Sed */ 413193323Sed} 414193323Sed 415193323Sedstatic char * 416193323Sedform_args (struct itbl_entry *e) 417193323Sed{ 418193323Sed static char s[31]; 419193323Sed char c = 0, *p = s; 420193323Sed struct itbl_field *f; 421193323Sed 422193323Sed ASSERT (e); 423193323Sed for (f = e->fields; f; f = f->next) 424193323Sed { 425193323Sed switch (f->type) 426193323Sed { 427193323Sed case e_dreg: 428193323Sed c = 'd'; 429193323Sed break; 430193323Sed case e_creg: 431193323Sed c = 't'; 432193323Sed break; 433193323Sed case e_greg: 434193323Sed c = 's'; 435193323Sed break; 436193323Sed case e_immed: 437193323Sed c = 'i'; 438193323Sed break; 439193323Sed case e_addr: 440201360Srdivacky c = 'a'; 441201360Srdivacky break; 442201360Srdivacky default: 443193323Sed c = 0; /* ignore; unknown field type */ 444193323Sed } 445193323Sed if (c) 446193323Sed { 447193323Sed if (p != s) 448193323Sed *p++ = ','; 449201360Srdivacky *p++ = c; 450201360Srdivacky } 451201360Srdivacky } 452201360Srdivacky *p = 0; 453201360Srdivacky return s; 454201360Srdivacky} 455201360Srdivacky#endif /* !STAND_ALONE */ 456201360Srdivacky 457201360Srdivacky 458201360Srdivacky/* Get processor's register name from val */ 459201360Srdivacky 460201360Srdivackyunsigned long 461201360Srdivackyitbl_get_reg_val (char *name) 462201360Srdivacky{ 463206083Srdivacky e_type t; 464193323Sed e_processor p; 465200581Srdivacky int r = 0; 466200581Srdivacky for (p = e_p0; p < e_nprocs; p++) 467206083Srdivacky for (t = e_regtype0; t < e_nregtypes; t++) 468206083Srdivacky { 469206083Srdivacky if (r = itbl_get_val (p, t, name), r) 470206083Srdivacky return r; 471206083Srdivacky } 472206083Srdivacky return 0; 473206083Srdivacky} 474206083Srdivacky 475193323Sedchar * 476193323Seditbl_get_name (e_processor processor, e_type type, unsigned long val) 477206083Srdivacky{ 478206083Srdivacky struct itbl_entry *r; 479193323Sed /* type depends on instruction passed */ 480206083Srdivacky r = find_entry_byval (processor, type, val, 0); 481206083Srdivacky if (r) 482206083Srdivacky return r->name; 483206083Srdivacky else 484193323Sed return 0; /* error; invalid operand */ 485193323Sed} 486201360Srdivacky 487206083Srdivacky/* Get processor's register value from name */ 488206083Srdivacky 489206083Srdivackyunsigned long 490206083Srdivackyitbl_get_val (e_processor processor, e_type type, char *name) 491206083Srdivacky{ 492206083Srdivacky struct itbl_entry *r; 493193323Sed /* type depends on instruction passed */ 494193323Sed r = find_entry_byname (processor, type, name); 495193323Sed if (r) 496201360Srdivacky return r->value; 497206083Srdivacky else 498193323Sed return 0; /* error; invalid operand */ 499199481Srdivacky} 500202878Srdivacky 501203954Srdivacky 502202878Srdivacky/* Assemble instruction "name" with operands "s". 503202878Srdivacky * name - name of instruction 504202878Srdivacky * s - operands 505202878Srdivacky * returns - long word for assembled instruction */ 506202878Srdivacky 507202878Srdivackyunsigned long 508202878Srdivackyitbl_assemble (char *name, char *s) 509203954Srdivacky{ 510202878Srdivacky unsigned long opcode; 511202878Srdivacky struct itbl_entry *e; 512202878Srdivacky struct itbl_field *f; 513202878Srdivacky char *n; 514202878Srdivacky int processor; 515202878Srdivacky 516202878Srdivacky if (!name || !*name) 517199481Srdivacky return 0; /* error! must have a opcode name/expr */ 518199481Srdivacky 519199481Srdivacky /* find entry in list of instructions for all processors */ 520199481Srdivacky for (processor = 0; processor < e_nprocs; processor++) 521202878Srdivacky { 522 e = find_entry_byname (processor, e_insn, name); 523 if (e) 524 break; 525 } 526 if (!e) 527 return 0; /* opcode not in table; invalid instrustion */ 528 opcode = build_opcode (e); 529 530 /* parse opcode's args (if any) */ 531 for (f = e->fields; f; f = f->next) /* for each arg, ... */ 532 { 533 struct itbl_entry *r; 534 unsigned long value; 535 if (!s || !*s) 536 return 0; /* error - not enough operands */ 537 n = itbl_get_field (&s); 538 /* n should be in form $n or 0xhhh (are symbol names valid?? */ 539 switch (f->type) 540 { 541 case e_dreg: 542 case e_creg: 543 case e_greg: 544 /* Accept either a string name 545 * or '$' followed by the register number */ 546 if (*n == '$') 547 { 548 n++; 549 value = strtol (n, 0, 10); 550 /* FIXME! could have "0l"... then what?? */ 551 if (value == 0 && *n != '0') 552 return 0; /* error; invalid operand */ 553 } 554 else 555 { 556 r = find_entry_byname (e->processor, f->type, n); 557 if (r) 558 value = r->value; 559 else 560 return 0; /* error; invalid operand */ 561 } 562 break; 563 case e_addr: 564 /* use assembler's symbol table to find symbol */ 565 /* FIXME!! Do we need this? 566 if so, what about relocs?? 567 my_getExpression (&imm_expr, s); 568 return 0; /-* error; invalid operand *-/ 569 break; 570 */ 571 /* If not a symbol, fall thru to IMMED */ 572 case e_immed: 573 if (*n == '0' && *(n + 1) == 'x') /* hex begins 0x... */ 574 { 575 n += 2; 576 value = strtol (n, 0, 16); 577 /* FIXME! could have "0xl"... then what?? */ 578 } 579 else 580 { 581 value = strtol (n, 0, 10); 582 /* FIXME! could have "0l"... then what?? */ 583 if (value == 0 && *n != '0') 584 return 0; /* error; invalid operand */ 585 } 586 break; 587 default: 588 return 0; /* error; invalid field spec */ 589 } 590 opcode |= apply_range (value, f->range); 591 } 592 if (s && *s) 593 return 0; /* error - too many operands */ 594 return opcode; /* done! */ 595} 596 597/* Disassemble instruction "insn". 598 * insn - instruction 599 * s - buffer to hold disassembled instruction 600 * returns - 1 if succeeded; 0 if failed 601 */ 602 603int 604itbl_disassemble (char *s, unsigned long insn) 605{ 606 e_processor processor; 607 struct itbl_entry *e; 608 struct itbl_field *f; 609 610 if (!ITBL_IS_INSN (insn)) 611 return 0; /* error*/ 612 processor = get_processor (ITBL_DECODE_PNUM (insn)); 613 614 /* find entry in list */ 615 e = find_entry_byval (processor, e_insn, insn, 0); 616 if (!e) 617 return 0; /* opcode not in table; invalid instrustion */ 618 strcpy (s, e->name); 619 620 /* parse insn's args (if any) */ 621 for (f = e->fields; f; f = f->next) /* for each arg, ... */ 622 { 623 struct itbl_entry *r; 624 unsigned long value; 625 626 if (f == e->fields) /* first operand is preceeded by tab */ 627 strcat (s, "\t"); 628 else /* ','s separate following operands */ 629 strcat (s, ","); 630 value = extract_range (insn, f->range); 631 /* n should be in form $n or 0xhhh (are symbol names valid?? */ 632 switch (f->type) 633 { 634 case e_dreg: 635 case e_creg: 636 case e_greg: 637 /* Accept either a string name 638 * or '$' followed by the register number */ 639 r = find_entry_byval (e->processor, f->type, value, &f->range); 640 if (r) 641 strcat (s, r->name); 642 else 643 sprintf (s, "%s$%d", s, value); 644 break; 645 case e_addr: 646 /* use assembler's symbol table to find symbol */ 647 /* FIXME!! Do we need this? 648 * if so, what about relocs?? 649 */ 650 /* If not a symbol, fall thru to IMMED */ 651 case e_immed: 652 sprintf (s, "%s0x%x", s, value); 653 break; 654 default: 655 return 0; /* error; invalid field spec */ 656 } 657 } 658 return 1; /* done! */ 659} 660 661/*======================================================================*/ 662/* 663 * Local functions for manipulating private structures containing 664 * the names and format for the new instructions and registers 665 * for each processor. 666 */ 667 668/* Calculate instruction's opcode and function values from entry */ 669 670static unsigned long 671build_opcode (struct itbl_entry *e) 672{ 673 unsigned long opcode; 674 675 opcode = apply_range (e->value, e->range); 676 opcode |= ITBL_ENCODE_PNUM (e->processor); 677 return opcode; 678} 679 680/* Calculate absolute value given the relative value and bit position range 681 * within the instruction. 682 * The range is inclusive where 0 is least significant bit. 683 * A range of { 24, 20 } will have a mask of 684 * bit 3 2 1 685 * pos: 1098 7654 3210 9876 5432 1098 7654 3210 686 * bin: 0000 0001 1111 0000 0000 0000 0000 0000 687 * hex: 0 1 f 0 0 0 0 0 688 * mask: 0x01f00000. 689 */ 690 691static unsigned long 692apply_range (unsigned long rval, struct itbl_range r) 693{ 694 unsigned long mask; 695 unsigned long aval; 696 int len = MAX_BITPOS - r.sbit; 697 698 ASSERT (r.sbit >= r.ebit); 699 ASSERT (MAX_BITPOS >= r.sbit); 700 ASSERT (r.ebit >= 0); 701 702 /* create mask by truncating 1s by shifting */ 703 mask = 0xffffffff << len; 704 mask = mask >> len; 705 mask = mask >> r.ebit; 706 mask = mask << r.ebit; 707 708 aval = (rval << r.ebit) & mask; 709 return aval; 710} 711 712/* Calculate relative value given the absolute value and bit position range 713 * within the instruction. */ 714 715static unsigned long 716extract_range (unsigned long aval, struct itbl_range r) 717{ 718 unsigned long mask; 719 unsigned long rval; 720 int len = MAX_BITPOS - r.sbit; 721 722 /* create mask by truncating 1s by shifting */ 723 mask = 0xffffffff << len; 724 mask = mask >> len; 725 mask = mask >> r.ebit; 726 mask = mask << r.ebit; 727 728 rval = (aval & mask) >> r.ebit; 729 return rval; 730} 731 732/* Extract processor's assembly instruction field name from s; 733 * forms are "n args" "n,args" or "n" */ 734/* Return next argument from string pointer "s" and advance s. 735 * delimiters are " ,\0" */ 736 737char * 738itbl_get_field (char **S) 739{ 740 static char n[128]; 741 char *p, *ps, *s; 742 int len; 743 744 s = *S; 745 if (!s || !*s) 746 return 0; 747 p = s + strlen (s); 748 if (ps = strchr (s, ','), ps) 749 p = ps; 750 if (ps = strchr (s, ' '), ps) 751 p = min (p, ps); 752 if (ps = strchr (s, '\0'), ps) 753 p = min (p, ps); 754 if (p == 0) 755 return 0; /* error! */ 756 len = p - s; 757 ASSERT (128 > len + 1); 758 strncpy (n, s, len); 759 n[len] = 0; 760 if (s[len] == '\0') 761 s = 0; /* no more args */ 762 else 763 s += len + 1; /* advance to next arg */ 764 765 *S = s; 766 return n; 767} 768 769/* Search entries for a given processor and type 770 * to find one matching the name "n". 771 * Return a pointer to the entry */ 772 773static struct itbl_entry * 774find_entry_byname (e_processor processor, 775 e_type type, char *n) 776{ 777 struct itbl_entry *e, **es; 778 779 es = get_entries (processor, type); 780 for (e = *es; e; e = e->next) /* for each entry, ... */ 781 { 782 if (!strcmp (e->name, n)) 783 return e; 784 } 785 return 0; 786} 787 788/* Search entries for a given processor and type 789 * to find one matching the value "val" for the range "r". 790 * Return a pointer to the entry. 791 * This function is used for disassembling fields of an instruction. 792 */ 793 794static struct itbl_entry * 795find_entry_byval (e_processor processor, e_type type, 796 unsigned long val, struct itbl_range *r) 797{ 798 struct itbl_entry *e, **es; 799 unsigned long eval; 800 801 es = get_entries (processor, type); 802 for (e = *es; e; e = e->next) /* for each entry, ... */ 803 { 804 if (processor != e->processor) 805 continue; 806 /* For insns, we might not know the range of the opcode, 807 * so a range of 0 will allow this routine to match against 808 * the range of the entry to be compared with. 809 * This could cause ambiguities. 810 * For operands, we get an extracted value and a range. 811 */ 812 /* if range is 0, mask val against the range of the compared entry. */ 813 if (r == 0) /* if no range passed, must be whole 32-bits 814 * so create 32-bit value from entry's range */ 815 { 816 eval = apply_range (e->value, e->range); 817 val &= apply_range (0xffffffff, e->range); 818 } 819 else if (r->sbit == e->range.sbit && r->ebit == e->range.ebit 820 || e->range.sbit == 0 && e->range.ebit == 0) 821 { 822 eval = apply_range (e->value, *r); 823 val = apply_range (val, *r); 824 } 825 else 826 continue; 827 if (val == eval) 828 return e; 829 } 830 return 0; 831} 832 833/* Return a pointer to the list of entries for a given processor and type. */ 834 835static struct itbl_entry ** 836get_entries (e_processor processor, e_type type) 837{ 838 return &entries[processor][type]; 839} 840 841/* Return an integral value for the processor passed from yyparse. */ 842 843static e_processor 844get_processor (int yyproc) 845{ 846 /* translate from yacc's processor to enum */ 847 if (yyproc >= e_p0 && yyproc < e_nprocs) 848 return (e_processor) yyproc; 849 return e_invproc; /* error; invalid processor */ 850} 851 852/* Return an integral value for the entry type passed from yyparse. */ 853 854static e_type 855get_type (int yytype) 856{ 857 switch (yytype) 858 { 859 /* translate from yacc's type to enum */ 860 case INSN: 861 return e_insn; 862 case DREG: 863 return e_dreg; 864 case CREG: 865 return e_creg; 866 case GREG: 867 return e_greg; 868 case ADDR: 869 return e_addr; 870 case IMMED: 871 return e_immed; 872 default: 873 return e_invtype; /* error; invalid type */ 874 } 875} 876 877 878/* Allocate and initialize an entry */ 879 880static struct itbl_entry * 881alloc_entry (e_processor processor, e_type type, 882 char *name, unsigned long value) 883{ 884 struct itbl_entry *e, **es; 885 if (!name) 886 return 0; 887 e = (struct itbl_entry *) malloc (sizeof (struct itbl_entry)); 888 if (e) 889 { 890 memset (e, 0, sizeof (struct itbl_entry)); 891 e->name = (char *) malloc (sizeof (strlen (name)) + 1); 892 if (e->name) 893 strcpy (e->name, name); 894 e->processor = processor; 895 e->type = type; 896 e->value = value; 897 es = get_entries (e->processor, e->type); 898 e->next = *es; 899 *es = e; 900 } 901 return e; 902} 903 904/* Allocate and initialize an entry's field */ 905 906static struct itbl_field * 907alloc_field (e_type type, int sbit, int ebit, 908 unsigned long flags) 909{ 910 struct itbl_field *f; 911 f = (struct itbl_field *) malloc (sizeof (struct itbl_field)); 912 if (f) 913 { 914 memset (f, 0, sizeof (struct itbl_field)); 915 f->type = type; 916 f->range.sbit = sbit; 917 f->range.ebit = ebit; 918 f->flags = flags; 919 } 920 return f; 921} 922