itbl-ops.c 23 KB

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