1 /* ia64-opc.c -- Functions to access the compacted opcode table
2 Copyright (C) 1999 Free Software Foundation, Inc.
3 Written by Bob Manson of Cygnus Solutions, <manson@cygnus.com>
5 This file is part of GDB, GAS, and the GNU binutils.
7 GDB, GAS, and the GNU binutils are free software; you can redistribute
8 them and/or modify them under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either version
10 2, or (at your option) any later version.
12 GDB, GAS, and the GNU binutils are distributed in the hope that they
13 will be useful, but WITHOUT ANY WARRANTY; without even the implied
14 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this file; see the file COPYING. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "libiberty.h"
25 #include "ia64-asmtab.h"
26 #include "ia64-asmtab.c"
28 const struct ia64_templ_desc ia64_templ_desc[16] =
30 { 0, { IA64_UNIT_M, IA64_UNIT_I, IA64_UNIT_I }, "MII" }, /* 0 */
31 { 2, { IA64_UNIT_M, IA64_UNIT_I, IA64_UNIT_I }, "MII" },
32 { 0, { IA64_UNIT_M, IA64_UNIT_L, IA64_UNIT_X }, "MLX" },
34 { 0, { IA64_UNIT_M, IA64_UNIT_M, IA64_UNIT_I }, "MMI" }, /* 4 */
35 { 1, { IA64_UNIT_M, IA64_UNIT_M, IA64_UNIT_I }, "MMI" },
36 { 0, { IA64_UNIT_M, IA64_UNIT_F, IA64_UNIT_I }, "MFI" },
37 { 0, { IA64_UNIT_M, IA64_UNIT_M, IA64_UNIT_F }, "MMF" },
38 { 0, { IA64_UNIT_M, IA64_UNIT_I, IA64_UNIT_B }, "MIB" }, /* 8 */
39 { 0, { IA64_UNIT_M, IA64_UNIT_B, IA64_UNIT_B }, "MBB" },
41 { 0, { IA64_UNIT_B, IA64_UNIT_B, IA64_UNIT_B }, "BBB" },
42 { 0, { IA64_UNIT_M, IA64_UNIT_M, IA64_UNIT_B }, "MMB" }, /* c */
44 { 0, { IA64_UNIT_M, IA64_UNIT_F, IA64_UNIT_B }, "MFB" },
49 /* Copy the prefix contained in *PTR (up to a '.' or a NUL) to DEST.
50 PTR will be adjusted to point to the start of the next portion
51 of the opcode, or at the NUL character. */
54 get_opc_prefix (ptr, dest)
58 char *c = strchr (*ptr, '.');
61 memcpy (dest, *ptr, c - *ptr);
62 dest[c - *ptr] = '\0';
67 int l = strlen (*ptr);
68 memcpy (dest, *ptr, l);
74 /* Find the index of the entry in the string table corresponding to
75 STR; return -1 if one does not exist. */
82 short end = sizeof (ia64_strings) / sizeof (const char *);
83 short i = (start + end) / 2;
85 if (strcmp (str, ia64_strings[end - 1]) > 0)
91 int c = strcmp (str, ia64_strings[i]);
104 i = (start + end) / 2;
109 /* Find the opcode in the main opcode table whose name is STRINGINDEX, or
110 return -1 if one does not exist. */
113 find_main_ent (nameindex)
117 short end = sizeof (main_table) / sizeof (struct ia64_main_table);
118 short i = (start + end) / 2;
120 if (nameindex < main_table[0].name_index
121 || nameindex > main_table[end - 1].name_index)
127 if (nameindex < main_table[i].name_index)
131 else if (nameindex == main_table[i].name_index)
133 while (i > 0 && main_table[i - 1].name_index == nameindex)
143 i = (start + end) / 2;
148 /* Find the index of the entry in the completer table that is part of
149 MAIN_ENT (starting from PREV_COMPLETER) that matches NAME, or
150 return -1 if one does not exist. */
153 find_completer (main_ent, prev_completer, name)
155 short prev_completer;
158 short name_index = find_string_ent (name);
165 if (prev_completer == -1)
167 prev_completer = main_table[main_ent].completers;
171 prev_completer = completer_table[prev_completer].subentries;
174 while (prev_completer != -1)
176 if (completer_table[prev_completer].name_index == name_index)
178 return prev_completer;
180 prev_completer = completer_table[prev_completer].alternative;
185 /* Apply the completer referred to by COMPLETER_INDEX to OPCODE, and
186 return the result. */
189 apply_completer (opcode, completer_index)
193 ia64_insn mask = completer_table[completer_index].mask;
194 ia64_insn bits = completer_table[completer_index].bits;
195 int shiftamt = (completer_table[completer_index].offset & 63);
197 mask = mask << shiftamt;
198 bits = bits << shiftamt;
199 opcode = (opcode & ~mask) | bits;
203 /* Extract BITS number of bits starting from OP_POINTER + BITOFFSET in
204 the dis_table array, and return its value. (BITOFFSET is numbered
205 starting from MSB to LSB, so a BITOFFSET of 0 indicates the MSB of the
206 first byte in OP_POINTER.) */
209 extract_op_bits (op_pointer, bitoffset, bits)
216 op_pointer += (bitoffset / 8);
220 unsigned int op = dis_table[op_pointer++];
221 int numb = 8 - (bitoffset % 8);
222 int mask = (1 << numb) - 1;
223 int bata = (bits < numb) ? bits : numb;
224 int delta = numb - bata;
226 res = (res << bata) | ((op & mask) >> delta);
232 res = (res << 8) | (dis_table[op_pointer++] & 255);
237 unsigned int op = (dis_table[op_pointer++] & 255);
238 res = (res << bits) | (op >> (8 - bits));
243 /* Examine the state machine entry at OP_POINTER in the dis_table
244 array, and extract its values into OPVAL and OP. The length of the
245 state entry in bits is returned. */
248 extract_op (op_pointer, opval, op)
255 *op = dis_table[op_pointer];
259 opval[0] = extract_op_bits (op_pointer, oplen, 5);
262 switch ((*op) & 0x30)
266 opval[1] = extract_op_bits (op_pointer, oplen, 8);
268 opval[1] += op_pointer;
273 opval[1] = extract_op_bits (op_pointer, oplen, 16);
274 if (! (opval[1] & 32768))
276 opval[1] += op_pointer;
284 opval[2] = extract_op_bits (op_pointer, oplen, 12);
290 if (((*op) & 0x08) && (((*op) & 0x30) != 0x30))
292 opval[2] = extract_op_bits (op_pointer, oplen, 16);
294 if (! (opval[2] & 32768))
296 opval[2] += op_pointer;
302 /* Returns a non-zero value if the opcode in the main_table list at
303 PLACE matches OPCODE and is of type TYPE. */
306 opcode_verify (opcode, place, type)
309 enum ia64_insn_type type;
311 if (main_table[place].opcode_type != type)
315 if (main_table[place].flags
316 & (IA64_OPCODE_F2_EQ_F3 | IA64_OPCODE_LEN_EQ_64MCNT))
318 const struct ia64_operand *o1, *o2;
321 if (main_table[place].flags & IA64_OPCODE_F2_EQ_F3)
323 o1 = elf64_ia64_operands + IA64_OPND_F2;
324 o2 = elf64_ia64_operands + IA64_OPND_F3;
325 (*o1->extract) (o1, opcode, &f2);
326 (*o2->extract) (o2, opcode, &f3);
332 ia64_insn len, count;
334 /* length must equal 64-count: */
335 o1 = elf64_ia64_operands + IA64_OPND_LEN6;
336 o2 = elf64_ia64_operands + main_table[place].operands[2];
337 (*o1->extract) (o1, opcode, &len);
338 (*o2->extract) (o2, opcode, &count);
339 if (len != 64 - count)
346 /* Find an instruction entry in the ia64_dis_names array that matches
347 opcode OPCODE and is of type TYPE. Returns either a positive index
348 into the array, or a negative value if an entry for OPCODE could
352 locate_opcode_ent (opcode, type)
354 enum ia64_insn_type type;
359 int currstatenum = 0;
361 currtest[currstatenum] = 0;
362 op_ptr[currstatenum] = 0;
363 bitpos[currstatenum] = 40;
367 int op_pointer = op_ptr[currstatenum];
369 int currbitnum = bitpos[currstatenum];
375 oplen = extract_op (op_pointer, opval, &op);
377 bitpos[currstatenum] = currbitnum;
379 /* Skip opval[0] bits in the instruction. */
382 currbitnum -= opval[0];
385 /* The value of the current bit being tested. */
386 currbit = opcode & (((ia64_insn) 1) << currbitnum) ? 1 : 0;
389 /* We always perform the tests specified in the current state in
390 a particular order, falling through to the next test if the
391 previous one failed. */
392 switch (currtest[currstatenum])
395 currtest[currstatenum]++;
396 if (currbit == 0 && (op & 0x80))
398 /* Check for a zero bit. If this test solely checks for
399 a zero bit, we can check for up to 8 consecutive zero
400 bits (the number to check is specified by the lower 3
401 bits in the state code.)
403 If the state instruction matches, we go to the very
404 next state instruction; otherwise, try the next test. */
406 if ((op & 0xf8) == 0x80)
408 int count = op & 0x7;
411 for (x = 0; x <= count; x++)
414 opcode & (((ia64_insn) 1) << (currbitnum - x)) ? 1 : 0;
422 next_op = op_pointer + ((oplen + 7) / 8);
429 next_op = op_pointer + ((oplen + 7) / 8);
435 /* If the bit in the instruction is one, go to the state
436 instruction specified by opval[1]. */
437 currtest[currstatenum]++;
438 if (currbit && (op & 0x30) != 0 && ((op & 0x30) != 0x30))
445 /* Don't care. Skip the current bit and go to the state
446 instruction specified by opval[2].
448 An encoding of 0x30 is special; this means that a 12-bit
449 offset into the ia64_dis_names[] array is specified. */
450 currtest[currstatenum]++;
451 if ((op & 0x08) || ((op & 0x30) == 0x30))
458 /* If bit 15 is set in the address of the next state, an offset
459 in the ia64_dis_names array was specified instead. We then
460 check to see if an entry in the list of opcodes matches the
461 opcode we were given; if so, we have succeeded. */
463 if ((next_op >= 0) && (next_op & 32768))
465 short disent = next_op & 32767;
472 /* Run through the list of opcodes to check, trying to find
476 int place = ia64_dis_names[disent].insn_index;
478 if (opcode_verify (opcode, place, type))
482 if (ia64_dis_names[disent].next_flag)
498 /* Failed to match; try the next test in this state. */
503 /* next_op == -1 is "back up to the previous state".
504 next_op == -2 is "stay in this state and try the next test".
505 Otherwise, transition to the state indicated by next_op. */
510 if (currstatenum < 0)
515 else if (next_op >= 0)
518 bitpos[currstatenum] = currbitnum - 1;
519 op_ptr[currstatenum] = next_op;
520 currtest[currstatenum] = 0;
525 /* Construct an ia64_opcode entry based on OPCODE, NAME and PLACE. */
527 static struct ia64_opcode *
528 make_ia64_opcode (opcode, name, place, depind)
534 struct ia64_opcode *res =
535 (struct ia64_opcode *) xmalloc (sizeof (struct ia64_opcode));
536 res->name = xstrdup (name);
537 res->type = main_table[place].opcode_type;
538 res->num_outputs = main_table[place].num_outputs;
539 res->opcode = opcode;
540 res->mask = main_table[place].mask;
541 res->operands[0] = main_table[place].operands[0];
542 res->operands[1] = main_table[place].operands[1];
543 res->operands[2] = main_table[place].operands[2];
544 res->operands[3] = main_table[place].operands[3];
545 res->operands[4] = main_table[place].operands[4];
546 res->flags = main_table[place].flags;
547 res->ent_index = place;
548 res->dependencies = &op_dependencies[depind];
552 /* Determine the ia64_opcode entry for the opcode specified by INSN
553 and TYPE. If a valid entry is not found, return NULL. */
555 ia64_dis_opcode (insn, type)
557 enum ia64_insn_type type;
559 int disent = locate_opcode_ent (insn, type);
567 unsigned int cb = ia64_dis_names[disent].completer_index;
568 static char name[128];
569 int place = ia64_dis_names[disent].insn_index;
570 int ci = main_table[place].completers;
571 ia64_insn tinsn = main_table[place].opcode;
573 strcpy (name, ia64_strings [main_table[place].name_index]);
579 int cname = completer_table[ci].name_index;
581 tinsn = apply_completer (tinsn, ci);
583 if (ia64_strings[cname][0] != '\0')
586 strcat (name, ia64_strings[cname]);
590 ci = completer_table[ci].subentries;
595 ci = completer_table[ci].alternative;
603 if (tinsn != (insn & main_table[place].mask))
607 return make_ia64_opcode (insn, name, place,
608 completer_table[ci].dependencies);
612 /* Search the main_opcode table starting from PLACE for an opcode that
613 matches NAME. Return NULL if one is not found. */
615 static struct ia64_opcode *
616 ia64_find_matching_opcode (name, place)
624 if (strlen (name) > 128)
629 get_opc_prefix (&suffix, op);
630 name_index = find_string_ent (op);
636 while (main_table[place].name_index == name_index)
638 const char *curr_suffix = suffix;
639 ia64_insn curr_insn = main_table[place].opcode;
640 short completer = -1;
643 if (suffix[0] == '\0')
645 completer = find_completer (place, completer, suffix);
649 get_opc_prefix (&curr_suffix, op);
650 completer = find_completer (place, completer, op);
654 curr_insn = apply_completer (curr_insn, completer);
656 } while (completer != -1 && curr_suffix[0] != '\0');
658 if (completer != -1 && curr_suffix[0] == '\0'
659 && completer_table[completer].terminal_completer)
661 int depind = completer_table[completer].dependencies;
662 return make_ia64_opcode (curr_insn, name, place, depind);
672 /* Find the next opcode after PREV_ENT that matches PREV_ENT, or return NULL
673 if one does not exist.
675 It is the caller's responsibility to invoke ia64_free_opcode () to
676 release any resources used by the returned entry. */
679 ia64_find_next_opcode (prev_ent)
680 struct ia64_opcode *prev_ent;
682 return ia64_find_matching_opcode (prev_ent->name,
683 prev_ent->ent_index + 1);
686 /* Find the first opcode that matches NAME, or return NULL if it does
689 It is the caller's responsibility to invoke ia64_free_opcode () to
690 release any resources used by the returned entry. */
693 ia64_find_opcode (name)
701 if (strlen (name) > 128)
706 get_opc_prefix (&suffix, op);
707 name_index = find_string_ent (op);
713 place = find_main_ent (name_index);
719 return ia64_find_matching_opcode (name, place);
722 /* Free any resources used by ENT. */
724 ia64_free_opcode (ent)
725 struct ia64_opcode *ent;
727 free ((void *)ent->name);
731 const struct ia64_dependency *
732 ia64_find_dependency (index)
737 if (index < 0 || index >= sizeof(dependencies) / sizeof(dependencies[0]))
740 return &dependencies[index];