* elf64-sparc.c (sparc64_elf_check_relocs): Don't trust reloc_count.
[binutils.git] / opcodes / ia64-opc.c
blobc2ddc8765fef764d5b11f5fe8095fbf33cafc985
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
20 02111-1307, USA. */
22 #include "ansidecl.h"
23 #include "libiberty.h"
24 #include "sysdep.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" },
33 { 0, { 0, }, "-3-" },
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" },
40 { 0, { 0, }, "-a-" },
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 */
43 { 0, { 0, }, "-d-" },
44 { 0, { IA64_UNIT_M, IA64_UNIT_F, IA64_UNIT_B }, "MFB" },
45 { 0, { 0, }, "-f-" },
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. */
53 static void
54 get_opc_prefix (ptr, dest)
55 const char **ptr;
56 char *dest;
58 char *c = strchr (*ptr, '.');
59 if (c != NULL)
61 memcpy (dest, *ptr, c - *ptr);
62 dest[c - *ptr] = '\0';
63 *ptr = c + 1;
65 else
67 int l = strlen (*ptr);
68 memcpy (dest, *ptr, l);
69 dest[l] = '\0';
70 *ptr += l;
74 /* Find the index of the entry in the string table corresponding to
75 STR; return -1 if one does not exist. */
77 static short
78 find_string_ent (str)
79 const char *str;
81 short start = 0;
82 short end = sizeof (ia64_strings) / sizeof (const char *);
83 short i = (start + end) / 2;
85 if (strcmp (str, ia64_strings[end - 1]) > 0)
87 return -1;
89 while (start <= end)
91 int c = strcmp (str, ia64_strings[i]);
92 if (c < 0)
94 end = i - 1;
96 else if (c == 0)
98 return i;
100 else
102 start = i + 1;
104 i = (start + end) / 2;
106 return -1;
109 /* Find the opcode in the main opcode table whose name is STRINGINDEX, or
110 return -1 if one does not exist. */
112 static short
113 find_main_ent (nameindex)
114 short nameindex;
116 short start = 0;
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)
123 return -1;
125 while (start <= end)
127 if (nameindex < main_table[i].name_index)
129 end = i - 1;
131 else if (nameindex == main_table[i].name_index)
133 while (i > 0 && main_table[i - 1].name_index == nameindex)
135 i--;
137 return i;
139 else
141 start = i + 1;
143 i = (start + end) / 2;
145 return -1;
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. */
152 static short
153 find_completer (main_ent, prev_completer, name)
154 short main_ent;
155 short prev_completer;
156 const char *name;
158 short name_index = find_string_ent (name);
160 if (name_index < 0)
162 return -1;
165 if (prev_completer == -1)
167 prev_completer = main_table[main_ent].completers;
169 else
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;
182 return -1;
185 /* Apply the completer referred to by COMPLETER_INDEX to OPCODE, and
186 return the result. */
188 static ia64_insn
189 apply_completer (opcode, completer_index)
190 ia64_insn opcode;
191 int 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;
200 return opcode;
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.) */
208 static int
209 extract_op_bits (op_pointer, bitoffset, bits)
210 int op_pointer;
211 int bitoffset;
212 int bits;
214 int res = 0;
216 op_pointer += (bitoffset / 8);
218 if (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);
227 bitoffset += bata;
228 bits -= bata;
230 while (bits >= 8)
232 res = (res << 8) | (dis_table[op_pointer++] & 255);
233 bits -= 8;
235 if (bits > 0)
237 unsigned int op = (dis_table[op_pointer++] & 255);
238 res = (res << bits) | (op >> (8 - bits));
240 return res;
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. */
247 static int
248 extract_op (op_pointer, opval, op)
249 int op_pointer;
250 int *opval;
251 unsigned int *op;
253 int oplen = 5;
255 *op = dis_table[op_pointer];
257 if ((*op) & 0x40)
259 opval[0] = extract_op_bits (op_pointer, oplen, 5);
260 oplen += 5;
262 switch ((*op) & 0x30)
264 case 0x10:
266 opval[1] = extract_op_bits (op_pointer, oplen, 8);
267 oplen += 8;
268 opval[1] += op_pointer;
269 break;
271 case 0x20:
273 opval[1] = extract_op_bits (op_pointer, oplen, 16);
274 if (! (opval[1] & 32768))
276 opval[1] += op_pointer;
278 oplen += 16;
279 break;
281 case 0x30:
283 oplen--;
284 opval[2] = extract_op_bits (op_pointer, oplen, 12);
285 oplen += 12;
286 opval[2] |= 32768;
287 break;
290 if (((*op) & 0x08) && (((*op) & 0x30) != 0x30))
292 opval[2] = extract_op_bits (op_pointer, oplen, 16);
293 oplen += 16;
294 if (! (opval[2] & 32768))
296 opval[2] += op_pointer;
299 return oplen;
302 /* Returns a non-zero value if the opcode in the main_table list at
303 PLACE matches OPCODE and is of type TYPE. */
305 static int
306 opcode_verify (opcode, place, type)
307 ia64_insn opcode;
308 int place;
309 enum ia64_insn_type type;
311 if (main_table[place].opcode_type != type)
313 return 0;
315 if (main_table[place].flags
316 & (IA64_OPCODE_F2_EQ_F3 | IA64_OPCODE_LEN_EQ_64MCNT))
318 const struct ia64_operand *o1, *o2;
319 ia64_insn f2, f3;
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);
327 if (f2 != f3)
328 return 0;
330 else
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)
340 return 0;
343 return 1;
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
349 not be found. Checks all matches and returns the one with the highest
350 priority. */
352 static int
353 locate_opcode_ent (opcode, type)
354 ia64_insn opcode;
355 enum ia64_insn_type type;
357 int currtest[41];
358 int bitpos[41];
359 int op_ptr[41];
360 int currstatenum = 0;
361 short found_disent = -1;
362 short found_priority = -1;
364 currtest[currstatenum] = 0;
365 op_ptr[currstatenum] = 0;
366 bitpos[currstatenum] = 40;
368 while (1)
370 int op_pointer = op_ptr[currstatenum];
371 unsigned int op;
372 int currbitnum = bitpos[currstatenum];
373 int oplen;
374 int opval[3];
375 int next_op;
376 int currbit;
378 oplen = extract_op (op_pointer, opval, &op);
380 bitpos[currstatenum] = currbitnum;
382 /* Skip opval[0] bits in the instruction. */
383 if (op & 0x40)
385 currbitnum -= opval[0];
388 /* The value of the current bit being tested. */
389 currbit = opcode & (((ia64_insn) 1) << currbitnum) ? 1 : 0;
390 next_op = -1;
392 /* We always perform the tests specified in the current state in
393 a particular order, falling through to the next test if the
394 previous one failed. */
395 switch (currtest[currstatenum])
397 case 0:
398 currtest[currstatenum]++;
399 if (currbit == 0 && (op & 0x80))
401 /* Check for a zero bit. If this test solely checks for
402 a zero bit, we can check for up to 8 consecutive zero
403 bits (the number to check is specified by the lower 3
404 bits in the state code.)
406 If the state instruction matches, we go to the very
407 next state instruction; otherwise, try the next test. */
409 if ((op & 0xf8) == 0x80)
411 int count = op & 0x7;
412 int x;
414 for (x = 0; x <= count; x++)
416 int i =
417 opcode & (((ia64_insn) 1) << (currbitnum - x)) ? 1 : 0;
418 if (i)
420 break;
423 if (x > count)
425 next_op = op_pointer + ((oplen + 7) / 8);
426 currbitnum -= count;
427 break;
430 else if (! currbit)
432 next_op = op_pointer + ((oplen + 7) / 8);
433 break;
436 /* FALLTHROUGH */
437 case 1:
438 /* If the bit in the instruction is one, go to the state
439 instruction specified by opval[1]. */
440 currtest[currstatenum]++;
441 if (currbit && (op & 0x30) != 0 && ((op & 0x30) != 0x30))
443 next_op = opval[1];
444 break;
446 /* FALLTHROUGH */
447 case 2:
448 /* Don't care. Skip the current bit and go to the state
449 instruction specified by opval[2].
451 An encoding of 0x30 is special; this means that a 12-bit
452 offset into the ia64_dis_names[] array is specified. */
453 currtest[currstatenum]++;
454 if ((op & 0x08) || ((op & 0x30) == 0x30))
456 next_op = opval[2];
457 break;
461 /* If bit 15 is set in the address of the next state, an offset
462 in the ia64_dis_names array was specified instead. We then
463 check to see if an entry in the list of opcodes matches the
464 opcode we were given; if so, we have succeeded. */
466 if ((next_op >= 0) && (next_op & 32768))
468 short disent = next_op & 32767;
469 short priority = -1;
471 if (next_op > 65535)
473 abort ();
476 /* Run through the list of opcodes to check, trying to find
477 one that matches. */
478 while (disent >= 0)
480 int place = ia64_dis_names[disent].insn_index;
482 priority = ia64_dis_names[disent].priority;
484 if (opcode_verify (opcode, place, type)
485 && priority > found_priority)
487 break;
489 if (ia64_dis_names[disent].next_flag)
491 disent++;
493 else
495 disent = -1;
499 if (disent >= 0)
501 found_disent = disent;
502 found_priority = priority;
504 /* Try the next test in this state, regardless of whether a match
505 was found. */
506 next_op = -2;
509 /* next_op == -1 is "back up to the previous state".
510 next_op == -2 is "stay in this state and try the next test".
511 Otherwise, transition to the state indicated by next_op. */
513 if (next_op == -1)
515 currstatenum--;
516 if (currstatenum < 0)
518 return found_disent;
521 else if (next_op >= 0)
523 currstatenum++;
524 bitpos[currstatenum] = currbitnum - 1;
525 op_ptr[currstatenum] = next_op;
526 currtest[currstatenum] = 0;
531 /* Construct an ia64_opcode entry based on OPCODE, NAME and PLACE. */
533 static struct ia64_opcode *
534 make_ia64_opcode (opcode, name, place, depind)
535 ia64_insn opcode;
536 const char *name;
537 int place;
538 int depind;
540 struct ia64_opcode *res =
541 (struct ia64_opcode *) xmalloc (sizeof (struct ia64_opcode));
542 res->name = xstrdup (name);
543 res->type = main_table[place].opcode_type;
544 res->num_outputs = main_table[place].num_outputs;
545 res->opcode = opcode;
546 res->mask = main_table[place].mask;
547 res->operands[0] = main_table[place].operands[0];
548 res->operands[1] = main_table[place].operands[1];
549 res->operands[2] = main_table[place].operands[2];
550 res->operands[3] = main_table[place].operands[3];
551 res->operands[4] = main_table[place].operands[4];
552 res->flags = main_table[place].flags;
553 res->ent_index = place;
554 res->dependencies = &op_dependencies[depind];
555 return res;
558 /* Determine the ia64_opcode entry for the opcode specified by INSN
559 and TYPE. If a valid entry is not found, return NULL. */
560 struct ia64_opcode *
561 ia64_dis_opcode (insn, type)
562 ia64_insn insn;
563 enum ia64_insn_type type;
565 int disent = locate_opcode_ent (insn, type);
567 if (disent < 0)
569 return NULL;
571 else
573 unsigned int cb = ia64_dis_names[disent].completer_index;
574 static char name[128];
575 int place = ia64_dis_names[disent].insn_index;
576 int ci = main_table[place].completers;
577 ia64_insn tinsn = main_table[place].opcode;
579 strcpy (name, ia64_strings [main_table[place].name_index]);
581 while (cb)
583 if (cb & 1)
585 int cname = completer_table[ci].name_index;
587 tinsn = apply_completer (tinsn, ci);
589 if (ia64_strings[cname][0] != '\0')
591 strcat (name, ".");
592 strcat (name, ia64_strings[cname]);
594 if (cb != 1)
596 ci = completer_table[ci].subentries;
599 else
601 ci = completer_table[ci].alternative;
603 if (ci < 0)
605 abort ();
607 cb = cb >> 1;
609 if (tinsn != (insn & main_table[place].mask))
611 abort ();
613 return make_ia64_opcode (insn, name, place,
614 completer_table[ci].dependencies);
618 /* Search the main_opcode table starting from PLACE for an opcode that
619 matches NAME. Return NULL if one is not found. */
621 static struct ia64_opcode *
622 ia64_find_matching_opcode (name, place)
623 const char *name;
624 short place;
626 char op[129];
627 const char *suffix;
628 short name_index;
630 if (strlen (name) > 128)
632 return NULL;
634 suffix = name;
635 get_opc_prefix (&suffix, op);
636 name_index = find_string_ent (op);
637 if (name_index < 0)
639 return NULL;
642 while (main_table[place].name_index == name_index)
644 const char *curr_suffix = suffix;
645 ia64_insn curr_insn = main_table[place].opcode;
646 short completer = -1;
648 do {
649 if (suffix[0] == '\0')
651 completer = find_completer (place, completer, suffix);
653 else
655 get_opc_prefix (&curr_suffix, op);
656 completer = find_completer (place, completer, op);
658 if (completer != -1)
660 curr_insn = apply_completer (curr_insn, completer);
662 } while (completer != -1 && curr_suffix[0] != '\0');
664 if (completer != -1 && curr_suffix[0] == '\0'
665 && completer_table[completer].terminal_completer)
667 int depind = completer_table[completer].dependencies;
668 return make_ia64_opcode (curr_insn, name, place, depind);
670 else
672 place++;
675 return NULL;
678 /* Find the next opcode after PREV_ENT that matches PREV_ENT, or return NULL
679 if one does not exist.
681 It is the caller's responsibility to invoke ia64_free_opcode () to
682 release any resources used by the returned entry. */
684 struct ia64_opcode *
685 ia64_find_next_opcode (prev_ent)
686 struct ia64_opcode *prev_ent;
688 return ia64_find_matching_opcode (prev_ent->name,
689 prev_ent->ent_index + 1);
692 /* Find the first opcode that matches NAME, or return NULL if it does
693 not exist.
695 It is the caller's responsibility to invoke ia64_free_opcode () to
696 release any resources used by the returned entry. */
698 struct ia64_opcode *
699 ia64_find_opcode (name)
700 const char *name;
702 char op[129];
703 const char *suffix;
704 short place;
705 short name_index;
707 if (strlen (name) > 128)
709 return NULL;
711 suffix = name;
712 get_opc_prefix (&suffix, op);
713 name_index = find_string_ent (op);
714 if (name_index < 0)
716 return NULL;
719 place = find_main_ent (name_index);
721 if (place < 0)
723 return NULL;
725 return ia64_find_matching_opcode (name, place);
728 /* Free any resources used by ENT. */
729 void
730 ia64_free_opcode (ent)
731 struct ia64_opcode *ent;
733 free ((void *)ent->name);
734 free (ent);
737 const struct ia64_dependency *
738 ia64_find_dependency (index)
739 int index;
741 index = DEP(index);
743 if (index < 0
744 || index >= (int)(sizeof(dependencies) / sizeof(dependencies[0])))
745 return NULL;
747 return &dependencies[index];