Home | History | Annotate | Download | only in bfd
      1 /* Configurable Xtensa ISA support.
      2    Copyright (C) 2003-2014 Free Software Foundation, Inc.
      3 
      4    This file is part of BFD, the Binary File Descriptor library.
      5 
      6    This program 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 3 of the License, or
      9    (at your option) any later version.
     10 
     11    This program 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 this program; if not, write to the Free Software
     18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     19    MA 02110-1301, USA.  */
     20 
     21 #include "sysdep.h"
     22 #include "bfd.h"
     23 #include "libbfd.h"
     24 #include "xtensa-isa.h"
     25 #include "xtensa-isa-internal.h"
     26 
     27 xtensa_isa_status xtisa_errno;
     28 char xtisa_error_msg[1024];
     29 
     30 
     31 xtensa_isa_status
     32 xtensa_isa_errno (xtensa_isa isa __attribute__ ((unused)))
     33 {
     34   return xtisa_errno;
     35 }
     36 
     37 
     38 char *
     39 xtensa_isa_error_msg (xtensa_isa isa __attribute__ ((unused)))
     40 {
     41   return xtisa_error_msg;
     42 }
     43 
     44 
     45 #define CHECK_ALLOC(MEM,ERRVAL) \
     46   do { \
     47     if ((MEM) == 0) \
     48       { \
     49 	xtisa_errno = xtensa_isa_out_of_memory; \
     50 	strcpy (xtisa_error_msg, "out of memory"); \
     51 	return (ERRVAL); \
     52       } \
     53   } while (0)
     54 
     55 #define CHECK_ALLOC_FOR_INIT(MEM,ERRVAL,ERRNO_P,ERROR_MSG_P) \
     56   do { \
     57     if ((MEM) == 0) \
     58       { \
     59 	xtisa_errno = xtensa_isa_out_of_memory; \
     60 	strcpy (xtisa_error_msg, "out of memory"); \
     61 	if (ERRNO_P) *(ERRNO_P) = xtisa_errno; \
     62 	if (ERROR_MSG_P) *(ERROR_MSG_P) = xtisa_error_msg; \
     63 	return (ERRVAL); \
     64       } \
     65   } while (0)
     66 
     67 
     68 
     69 /* Instruction buffers.  */
     71 
     72 int
     73 xtensa_insnbuf_size (xtensa_isa isa)
     74 {
     75   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
     76   return intisa->insnbuf_size;
     77 }
     78 
     79 
     80 xtensa_insnbuf
     81 xtensa_insnbuf_alloc (xtensa_isa isa)
     82 {
     83   xtensa_insnbuf result = (xtensa_insnbuf)
     84     malloc (xtensa_insnbuf_size (isa) * sizeof (xtensa_insnbuf_word));
     85   CHECK_ALLOC (result, 0);
     86   return result;
     87 }
     88 
     89 
     90 void
     91 xtensa_insnbuf_free (xtensa_isa isa __attribute__ ((unused)),
     92 		     xtensa_insnbuf buf)
     93 {
     94   free (buf);
     95 }
     96 
     97 
     98 /* Given <byte_index>, the index of a byte in a xtensa_insnbuf, our
     99    internal representation of a xtensa instruction word, return the index of
    100    its word and the bit index of its low order byte in the xtensa_insnbuf.  */
    101 
    102 static inline int
    103 byte_to_word_index (int byte_index)
    104 {
    105   return byte_index / sizeof (xtensa_insnbuf_word);
    106 }
    107 
    108 
    109 static inline int
    110 byte_to_bit_index (int byte_index)
    111 {
    112   return (byte_index & 0x3) * 8;
    113 }
    114 
    115 
    116 /* Copy an instruction in the 32-bit words pointed at by "insn" to
    117    characters pointed at by "cp".  This is more complicated than you
    118    might think because we want 16-bit instructions in bytes 2 & 3 for
    119    big-endian configurations.  This function allows us to specify
    120    which byte in "insn" to start with and which way to increment,
    121    allowing trivial implementation for both big- and little-endian
    122    configurations....and it seems to make pretty good code for
    123    both.  */
    124 
    125 int
    126 xtensa_insnbuf_to_chars (xtensa_isa isa,
    127 			 const xtensa_insnbuf insn,
    128 			 unsigned char *cp,
    129 			 int num_chars)
    130 {
    131   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    132   int insn_size = xtensa_isa_maxlength (isa);
    133   int fence_post, start, increment, i, byte_count;
    134   xtensa_format fmt;
    135 
    136   if (num_chars == 0)
    137     num_chars = insn_size;
    138 
    139   if (intisa->is_big_endian)
    140     {
    141       start = insn_size - 1;
    142       increment = -1;
    143     }
    144   else
    145     {
    146       start = 0;
    147       increment = 1;
    148     }
    149 
    150   /* Find the instruction format.  Do nothing if the buffer does not contain
    151      a valid instruction since we need to know how many bytes to copy.  */
    152   fmt = xtensa_format_decode (isa, insn);
    153   if (fmt == XTENSA_UNDEFINED)
    154     return XTENSA_UNDEFINED;
    155 
    156   byte_count = xtensa_format_length (isa, fmt);
    157   if (byte_count == XTENSA_UNDEFINED)
    158     return XTENSA_UNDEFINED;
    159 
    160   if (byte_count > num_chars)
    161     {
    162       xtisa_errno = xtensa_isa_buffer_overflow;
    163       strcpy (xtisa_error_msg, "output buffer too small for instruction");
    164       return XTENSA_UNDEFINED;
    165     }
    166 
    167   fence_post = start + (byte_count * increment);
    168 
    169   for (i = start; i != fence_post; i += increment, ++cp)
    170     {
    171       int word_inx = byte_to_word_index (i);
    172       int bit_inx = byte_to_bit_index (i);
    173 
    174       *cp = (insn[word_inx] >> bit_inx) & 0xff;
    175     }
    176 
    177   return byte_count;
    178 }
    179 
    180 
    181 /* Inward conversion from byte stream to xtensa_insnbuf.  See
    182    xtensa_insnbuf_to_chars for a discussion of why this is complicated
    183    by endianness.  */
    184 
    185 void
    186 xtensa_insnbuf_from_chars (xtensa_isa isa,
    187 			   xtensa_insnbuf insn,
    188 			   const unsigned char *cp,
    189 			   int num_chars)
    190 {
    191   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    192   int max_size, insn_size, fence_post, start, increment, i;
    193 
    194   max_size = xtensa_isa_maxlength (isa);
    195 
    196   /* Decode the instruction length so we know how many bytes to read.  */
    197   insn_size = (intisa->length_decode_fn) (cp);
    198   if (insn_size == XTENSA_UNDEFINED)
    199     {
    200       /* This should never happen when the byte stream contains a
    201 	 valid instruction.  Just read the maximum number of bytes....  */
    202       insn_size = max_size;
    203     }
    204 
    205   if (num_chars == 0 || num_chars > insn_size)
    206     num_chars = insn_size;
    207 
    208   if (intisa->is_big_endian)
    209     {
    210       start = max_size - 1;
    211       increment = -1;
    212     }
    213   else
    214     {
    215       start = 0;
    216       increment = 1;
    217     }
    218 
    219   fence_post = start + (num_chars * increment);
    220   memset (insn, 0, xtensa_insnbuf_size (isa) * sizeof (xtensa_insnbuf_word));
    221 
    222   for (i = start; i != fence_post; i += increment, ++cp)
    223     {
    224       int word_inx = byte_to_word_index (i);
    225       int bit_inx = byte_to_bit_index (i);
    226 
    227       insn[word_inx] |= (*cp & 0xff) << bit_inx;
    228     }
    229 }
    230 
    231 
    232 
    233 /* ISA information.  */
    235 
    236 extern xtensa_isa_internal xtensa_modules;
    237 
    238 xtensa_isa
    239 xtensa_isa_init (xtensa_isa_status *errno_p, char **error_msg_p)
    240 {
    241   xtensa_isa_internal *isa = &xtensa_modules;
    242   int n, is_user;
    243 
    244   /* Set up the opcode name lookup table.  */
    245   isa->opname_lookup_table =
    246     bfd_malloc (isa->num_opcodes * sizeof (xtensa_lookup_entry));
    247   CHECK_ALLOC_FOR_INIT (isa->opname_lookup_table, NULL, errno_p, error_msg_p);
    248   for (n = 0; n < isa->num_opcodes; n++)
    249     {
    250       isa->opname_lookup_table[n].key = isa->opcodes[n].name;
    251       isa->opname_lookup_table[n].u.opcode = n;
    252     }
    253   qsort (isa->opname_lookup_table, isa->num_opcodes,
    254 	 sizeof (xtensa_lookup_entry), xtensa_isa_name_compare);
    255 
    256   /* Set up the state name lookup table.  */
    257   isa->state_lookup_table =
    258     bfd_malloc (isa->num_states * sizeof (xtensa_lookup_entry));
    259   CHECK_ALLOC_FOR_INIT (isa->state_lookup_table, NULL, errno_p, error_msg_p);
    260   for (n = 0; n < isa->num_states; n++)
    261     {
    262       isa->state_lookup_table[n].key = isa->states[n].name;
    263       isa->state_lookup_table[n].u.state = n;
    264     }
    265   qsort (isa->state_lookup_table, isa->num_states,
    266 	 sizeof (xtensa_lookup_entry), xtensa_isa_name_compare);
    267 
    268   /* Set up the sysreg name lookup table.  */
    269   isa->sysreg_lookup_table =
    270     bfd_malloc (isa->num_sysregs * sizeof (xtensa_lookup_entry));
    271   CHECK_ALLOC_FOR_INIT (isa->sysreg_lookup_table, NULL, errno_p, error_msg_p);
    272   for (n = 0; n < isa->num_sysregs; n++)
    273     {
    274       isa->sysreg_lookup_table[n].key = isa->sysregs[n].name;
    275       isa->sysreg_lookup_table[n].u.sysreg = n;
    276     }
    277   qsort (isa->sysreg_lookup_table, isa->num_sysregs,
    278 	 sizeof (xtensa_lookup_entry), xtensa_isa_name_compare);
    279 
    280   /* Set up the user & system sysreg number tables.  */
    281   for (is_user = 0; is_user < 2; is_user++)
    282     {
    283       isa->sysreg_table[is_user] =
    284 	bfd_malloc ((isa->max_sysreg_num[is_user] + 1)
    285 		    * sizeof (xtensa_sysreg));
    286       CHECK_ALLOC_FOR_INIT (isa->sysreg_table[is_user], NULL,
    287 			    errno_p, error_msg_p);
    288 
    289       for (n = 0; n <= isa->max_sysreg_num[is_user]; n++)
    290 	isa->sysreg_table[is_user][n] = XTENSA_UNDEFINED;
    291     }
    292   for (n = 0; n < isa->num_sysregs; n++)
    293     {
    294       xtensa_sysreg_internal *sreg = &isa->sysregs[n];
    295       is_user = sreg->is_user;
    296 
    297       isa->sysreg_table[is_user][sreg->number] = n;
    298     }
    299 
    300   /* Set up the interface lookup table.  */
    301   isa->interface_lookup_table =
    302     bfd_malloc (isa->num_interfaces * sizeof (xtensa_lookup_entry));
    303   CHECK_ALLOC_FOR_INIT (isa->interface_lookup_table, NULL, errno_p,
    304 			error_msg_p);
    305   for (n = 0; n < isa->num_interfaces; n++)
    306     {
    307       isa->interface_lookup_table[n].key = isa->interfaces[n].name;
    308       isa->interface_lookup_table[n].u.intf = n;
    309     }
    310   qsort (isa->interface_lookup_table, isa->num_interfaces,
    311 	 sizeof (xtensa_lookup_entry), xtensa_isa_name_compare);
    312 
    313   /* Set up the funcUnit lookup table.  */
    314   isa->funcUnit_lookup_table =
    315     bfd_malloc (isa->num_funcUnits * sizeof (xtensa_lookup_entry));
    316   CHECK_ALLOC_FOR_INIT (isa->funcUnit_lookup_table, NULL, errno_p,
    317 			error_msg_p);
    318   for (n = 0; n < isa->num_funcUnits; n++)
    319     {
    320       isa->funcUnit_lookup_table[n].key = isa->funcUnits[n].name;
    321       isa->funcUnit_lookup_table[n].u.fun = n;
    322     }
    323   qsort (isa->funcUnit_lookup_table, isa->num_funcUnits,
    324 	 sizeof (xtensa_lookup_entry), xtensa_isa_name_compare);
    325 
    326   isa->insnbuf_size = ((isa->insn_size + sizeof (xtensa_insnbuf_word) - 1) /
    327 		       sizeof (xtensa_insnbuf_word));
    328 
    329   return (xtensa_isa) isa;
    330 }
    331 
    332 
    333 void
    334 xtensa_isa_free (xtensa_isa isa)
    335 {
    336   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    337   int n;
    338 
    339   /* With this version of the code, the xtensa_isa structure is not
    340      dynamically allocated, so this function is not essential.  Free
    341      the memory allocated by xtensa_isa_init and restore the xtensa_isa
    342      structure to its initial state.  */
    343 
    344   if (intisa->opname_lookup_table)
    345     {
    346       free (intisa->opname_lookup_table);
    347       intisa->opname_lookup_table = 0;
    348     }
    349 
    350   if (intisa->state_lookup_table)
    351     {
    352       free (intisa->state_lookup_table);
    353       intisa->state_lookup_table = 0;
    354     }
    355 
    356   if (intisa->sysreg_lookup_table)
    357     {
    358       free (intisa->sysreg_lookup_table);
    359       intisa->sysreg_lookup_table = 0;
    360     }
    361   for (n = 0; n < 2; n++)
    362     {
    363       if (intisa->sysreg_table[n])
    364 	{
    365 	  free (intisa->sysreg_table[n]);
    366 	  intisa->sysreg_table[n] = 0;
    367 	}
    368     }
    369 
    370   if (intisa->interface_lookup_table)
    371     {
    372       free (intisa->interface_lookup_table);
    373       intisa->interface_lookup_table = 0;
    374     }
    375 
    376   if (intisa->funcUnit_lookup_table)
    377     {
    378       free (intisa->funcUnit_lookup_table);
    379       intisa->funcUnit_lookup_table = 0;
    380     }
    381 }
    382 
    383 
    384 int
    385 xtensa_isa_name_compare (const void *v1, const void *v2)
    386 {
    387   xtensa_lookup_entry *e1 = (xtensa_lookup_entry *) v1;
    388   xtensa_lookup_entry *e2 = (xtensa_lookup_entry *) v2;
    389 
    390   return strcasecmp (e1->key, e2->key);
    391 }
    392 
    393 
    394 int
    395 xtensa_isa_maxlength (xtensa_isa isa)
    396 {
    397   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    398   return intisa->insn_size;
    399 }
    400 
    401 
    402 int
    403 xtensa_isa_length_from_chars (xtensa_isa isa, const unsigned char *cp)
    404 {
    405   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    406   return (intisa->length_decode_fn) (cp);
    407 }
    408 
    409 
    410 int
    411 xtensa_isa_num_pipe_stages (xtensa_isa isa)
    412 {
    413   xtensa_opcode opcode;
    414   xtensa_funcUnit_use *use;
    415   int num_opcodes, num_uses;
    416   int i, stage;
    417   static int max_stage = XTENSA_UNDEFINED;
    418 
    419   /* Only compute the value once.  */
    420   if (max_stage != XTENSA_UNDEFINED)
    421     return max_stage + 1;
    422 
    423   num_opcodes = xtensa_isa_num_opcodes (isa);
    424   for (opcode = 0; opcode < num_opcodes; opcode++)
    425     {
    426       num_uses = xtensa_opcode_num_funcUnit_uses (isa, opcode);
    427       for (i = 0; i < num_uses; i++)
    428 	{
    429 	  use = xtensa_opcode_funcUnit_use (isa, opcode, i);
    430 	  stage = use->stage;
    431 	  if (stage > max_stage)
    432 	    max_stage = stage;
    433 	}
    434     }
    435 
    436   return max_stage + 1;
    437 }
    438 
    439 
    440 int
    441 xtensa_isa_num_formats (xtensa_isa isa)
    442 {
    443   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    444   return intisa->num_formats;
    445 }
    446 
    447 
    448 int
    449 xtensa_isa_num_opcodes (xtensa_isa isa)
    450 {
    451   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    452   return intisa->num_opcodes;
    453 }
    454 
    455 
    456 int
    457 xtensa_isa_num_regfiles (xtensa_isa isa)
    458 {
    459   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    460   return intisa->num_regfiles;
    461 }
    462 
    463 
    464 int
    465 xtensa_isa_num_states (xtensa_isa isa)
    466 {
    467   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    468   return intisa->num_states;
    469 }
    470 
    471 
    472 int
    473 xtensa_isa_num_sysregs (xtensa_isa isa)
    474 {
    475   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    476   return intisa->num_sysregs;
    477 }
    478 
    479 
    480 int
    481 xtensa_isa_num_interfaces (xtensa_isa isa)
    482 {
    483   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    484   return intisa->num_interfaces;
    485 }
    486 
    487 
    488 int
    489 xtensa_isa_num_funcUnits (xtensa_isa isa)
    490 {
    491   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    492   return intisa->num_funcUnits;
    493 }
    494 
    495 
    496 
    497 /* Instruction formats.  */
    499 
    500 
    501 #define CHECK_FORMAT(INTISA,FMT,ERRVAL) \
    502   do { \
    503     if ((FMT) < 0 || (FMT) >= (INTISA)->num_formats) \
    504       { \
    505 	xtisa_errno = xtensa_isa_bad_format; \
    506 	strcpy (xtisa_error_msg, "invalid format specifier"); \
    507 	return (ERRVAL); \
    508       } \
    509   } while (0)
    510 
    511 
    512 #define CHECK_SLOT(INTISA,FMT,SLOT,ERRVAL) \
    513   do { \
    514     if ((SLOT) < 0 || (SLOT) >= (INTISA)->formats[FMT].num_slots) \
    515       { \
    516 	xtisa_errno = xtensa_isa_bad_slot; \
    517 	strcpy (xtisa_error_msg, "invalid slot specifier"); \
    518 	return (ERRVAL); \
    519       } \
    520   } while (0)
    521 
    522 
    523 const char *
    524 xtensa_format_name (xtensa_isa isa, xtensa_format fmt)
    525 {
    526   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    527   CHECK_FORMAT (intisa, fmt, NULL);
    528   return intisa->formats[fmt].name;
    529 }
    530 
    531 
    532 xtensa_format
    533 xtensa_format_lookup (xtensa_isa isa, const char *fmtname)
    534 {
    535   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    536   int fmt;
    537 
    538   if (!fmtname || !*fmtname)
    539     {
    540       xtisa_errno = xtensa_isa_bad_format;
    541       strcpy (xtisa_error_msg, "invalid format name");
    542       return XTENSA_UNDEFINED;
    543     }
    544 
    545   for (fmt = 0; fmt < intisa->num_formats; fmt++)
    546     {
    547       if (strcasecmp (fmtname, intisa->formats[fmt].name) == 0)
    548 	return fmt;
    549     }
    550 
    551   xtisa_errno = xtensa_isa_bad_format;
    552   sprintf (xtisa_error_msg, "format \"%s\" not recognized", fmtname);
    553   return XTENSA_UNDEFINED;
    554 }
    555 
    556 
    557 xtensa_format
    558 xtensa_format_decode (xtensa_isa isa, const xtensa_insnbuf insn)
    559 {
    560   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    561   xtensa_format fmt;
    562 
    563   fmt = (intisa->format_decode_fn) (insn);
    564   if (fmt != XTENSA_UNDEFINED)
    565     return fmt;
    566 
    567   xtisa_errno = xtensa_isa_bad_format;
    568   strcpy (xtisa_error_msg, "cannot decode instruction format");
    569   return XTENSA_UNDEFINED;
    570 }
    571 
    572 
    573 int
    574 xtensa_format_encode (xtensa_isa isa, xtensa_format fmt, xtensa_insnbuf insn)
    575 {
    576   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    577   CHECK_FORMAT (intisa, fmt, -1);
    578   (*intisa->formats[fmt].encode_fn) (insn);
    579   return 0;
    580 }
    581 
    582 
    583 int
    584 xtensa_format_length (xtensa_isa isa, xtensa_format fmt)
    585 {
    586   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    587   CHECK_FORMAT (intisa, fmt, XTENSA_UNDEFINED);
    588   return intisa->formats[fmt].length;
    589 }
    590 
    591 
    592 int
    593 xtensa_format_num_slots (xtensa_isa isa, xtensa_format fmt)
    594 {
    595   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    596   CHECK_FORMAT (intisa, fmt, XTENSA_UNDEFINED);
    597   return intisa->formats[fmt].num_slots;
    598 }
    599 
    600 
    601 xtensa_opcode
    602 xtensa_format_slot_nop_opcode (xtensa_isa isa, xtensa_format fmt, int slot)
    603 {
    604   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    605   int slot_id;
    606 
    607   CHECK_FORMAT (intisa, fmt, XTENSA_UNDEFINED);
    608   CHECK_SLOT (intisa, fmt, slot, XTENSA_UNDEFINED);
    609 
    610   slot_id = intisa->formats[fmt].slot_id[slot];
    611   return xtensa_opcode_lookup (isa, intisa->slots[slot_id].nop_name);
    612 }
    613 
    614 
    615 int
    616 xtensa_format_get_slot (xtensa_isa isa, xtensa_format fmt, int slot,
    617 			const xtensa_insnbuf insn, xtensa_insnbuf slotbuf)
    618 {
    619   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    620   int slot_id;
    621 
    622   CHECK_FORMAT (intisa, fmt, -1);
    623   CHECK_SLOT (intisa, fmt, slot, -1);
    624 
    625   slot_id = intisa->formats[fmt].slot_id[slot];
    626   (*intisa->slots[slot_id].get_fn) (insn, slotbuf);
    627   return 0;
    628 }
    629 
    630 
    631 int
    632 xtensa_format_set_slot (xtensa_isa isa, xtensa_format fmt, int slot,
    633 			xtensa_insnbuf insn, const xtensa_insnbuf slotbuf)
    634 {
    635   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    636   int slot_id;
    637 
    638   CHECK_FORMAT (intisa, fmt, -1);
    639   CHECK_SLOT (intisa, fmt, slot, -1);
    640 
    641   slot_id = intisa->formats[fmt].slot_id[slot];
    642   (*intisa->slots[slot_id].set_fn) (insn, slotbuf);
    643   return 0;
    644 }
    645 
    646 
    647 
    648 /* Opcode information.  */
    650 
    651 
    652 #define CHECK_OPCODE(INTISA,OPC,ERRVAL) \
    653   do { \
    654     if ((OPC) < 0 || (OPC) >= (INTISA)->num_opcodes) \
    655       { \
    656 	xtisa_errno = xtensa_isa_bad_opcode; \
    657 	strcpy (xtisa_error_msg, "invalid opcode specifier"); \
    658 	return (ERRVAL); \
    659       } \
    660   } while (0)
    661 
    662 
    663 xtensa_opcode
    664 xtensa_opcode_lookup (xtensa_isa isa, const char *opname)
    665 {
    666   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    667   xtensa_lookup_entry entry, *result = 0;
    668 
    669   if (!opname || !*opname)
    670     {
    671       xtisa_errno = xtensa_isa_bad_opcode;
    672       strcpy (xtisa_error_msg, "invalid opcode name");
    673       return XTENSA_UNDEFINED;
    674     }
    675 
    676   if (intisa->num_opcodes != 0)
    677     {
    678       entry.key = opname;
    679       result = bsearch (&entry, intisa->opname_lookup_table,
    680 			intisa->num_opcodes, sizeof (xtensa_lookup_entry),
    681 			xtensa_isa_name_compare);
    682     }
    683 
    684   if (!result)
    685     {
    686       xtisa_errno = xtensa_isa_bad_opcode;
    687       sprintf (xtisa_error_msg, "opcode \"%s\" not recognized", opname);
    688       return XTENSA_UNDEFINED;
    689     }
    690 
    691   return result->u.opcode;
    692 }
    693 
    694 
    695 xtensa_opcode
    696 xtensa_opcode_decode (xtensa_isa isa, xtensa_format fmt, int slot,
    697 		      const xtensa_insnbuf slotbuf)
    698 {
    699   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    700   int slot_id;
    701   xtensa_opcode opc;
    702 
    703   CHECK_FORMAT (intisa, fmt, XTENSA_UNDEFINED);
    704   CHECK_SLOT (intisa, fmt, slot, XTENSA_UNDEFINED);
    705 
    706   slot_id = intisa->formats[fmt].slot_id[slot];
    707 
    708   opc = (intisa->slots[slot_id].opcode_decode_fn) (slotbuf);
    709   if (opc != XTENSA_UNDEFINED)
    710     return opc;
    711 
    712   xtisa_errno = xtensa_isa_bad_opcode;
    713   strcpy (xtisa_error_msg, "cannot decode opcode");
    714   return XTENSA_UNDEFINED;
    715 }
    716 
    717 
    718 int
    719 xtensa_opcode_encode (xtensa_isa isa, xtensa_format fmt, int slot,
    720 		      xtensa_insnbuf slotbuf, xtensa_opcode opc)
    721 {
    722   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    723   int slot_id;
    724   xtensa_opcode_encode_fn encode_fn;
    725 
    726   CHECK_FORMAT (intisa, fmt, -1);
    727   CHECK_SLOT (intisa, fmt, slot, -1);
    728   CHECK_OPCODE (intisa, opc, -1);
    729 
    730   slot_id = intisa->formats[fmt].slot_id[slot];
    731   encode_fn = intisa->opcodes[opc].encode_fns[slot_id];
    732   if (!encode_fn)
    733     {
    734       xtisa_errno = xtensa_isa_wrong_slot;
    735       sprintf (xtisa_error_msg,
    736 	       "opcode \"%s\" is not allowed in slot %d of format \"%s\"",
    737 	       intisa->opcodes[opc].name, slot, intisa->formats[fmt].name);
    738       return -1;
    739     }
    740   (*encode_fn) (slotbuf);
    741   return 0;
    742 }
    743 
    744 
    745 const char *
    746 xtensa_opcode_name (xtensa_isa isa, xtensa_opcode opc)
    747 {
    748   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    749   CHECK_OPCODE (intisa, opc, NULL);
    750   return intisa->opcodes[opc].name;
    751 }
    752 
    753 
    754 int
    755 xtensa_opcode_is_branch (xtensa_isa isa, xtensa_opcode opc)
    756 {
    757   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    758   CHECK_OPCODE (intisa, opc, XTENSA_UNDEFINED);
    759   if ((intisa->opcodes[opc].flags & XTENSA_OPCODE_IS_BRANCH) != 0)
    760     return 1;
    761   return 0;
    762 }
    763 
    764 
    765 int
    766 xtensa_opcode_is_jump (xtensa_isa isa, xtensa_opcode opc)
    767 {
    768   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    769   CHECK_OPCODE (intisa, opc, XTENSA_UNDEFINED);
    770   if ((intisa->opcodes[opc].flags & XTENSA_OPCODE_IS_JUMP) != 0)
    771     return 1;
    772   return 0;
    773 }
    774 
    775 
    776 int
    777 xtensa_opcode_is_loop (xtensa_isa isa, xtensa_opcode opc)
    778 {
    779   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    780   CHECK_OPCODE (intisa, opc, XTENSA_UNDEFINED);
    781   if ((intisa->opcodes[opc].flags & XTENSA_OPCODE_IS_LOOP) != 0)
    782     return 1;
    783   return 0;
    784 }
    785 
    786 
    787 int
    788 xtensa_opcode_is_call (xtensa_isa isa, xtensa_opcode opc)
    789 {
    790   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    791   CHECK_OPCODE (intisa, opc, XTENSA_UNDEFINED);
    792   if ((intisa->opcodes[opc].flags & XTENSA_OPCODE_IS_CALL) != 0)
    793     return 1;
    794   return 0;
    795 }
    796 
    797 
    798 int
    799 xtensa_opcode_num_operands (xtensa_isa isa, xtensa_opcode opc)
    800 {
    801   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    802   int iclass_id;
    803 
    804   CHECK_OPCODE (intisa, opc, XTENSA_UNDEFINED);
    805   iclass_id = intisa->opcodes[opc].iclass_id;
    806   return intisa->iclasses[iclass_id].num_operands;
    807 }
    808 
    809 
    810 int
    811 xtensa_opcode_num_stateOperands (xtensa_isa isa, xtensa_opcode opc)
    812 {
    813   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    814   int iclass_id;
    815 
    816   CHECK_OPCODE (intisa, opc, XTENSA_UNDEFINED);
    817   iclass_id = intisa->opcodes[opc].iclass_id;
    818   return intisa->iclasses[iclass_id].num_stateOperands;
    819 }
    820 
    821 
    822 int
    823 xtensa_opcode_num_interfaceOperands (xtensa_isa isa, xtensa_opcode opc)
    824 {
    825   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    826   int iclass_id;
    827 
    828   CHECK_OPCODE (intisa, opc, XTENSA_UNDEFINED);
    829   iclass_id = intisa->opcodes[opc].iclass_id;
    830   return intisa->iclasses[iclass_id].num_interfaceOperands;
    831 }
    832 
    833 
    834 int
    835 xtensa_opcode_num_funcUnit_uses (xtensa_isa isa, xtensa_opcode opc)
    836 {
    837   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    838   CHECK_OPCODE (intisa, opc, XTENSA_UNDEFINED);
    839   return intisa->opcodes[opc].num_funcUnit_uses;
    840 }
    841 
    842 
    843 xtensa_funcUnit_use *
    844 xtensa_opcode_funcUnit_use (xtensa_isa isa, xtensa_opcode opc, int u)
    845 {
    846   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    847   CHECK_OPCODE (intisa, opc, NULL);
    848   if (u < 0 || u >= intisa->opcodes[opc].num_funcUnit_uses)
    849     {
    850       xtisa_errno = xtensa_isa_bad_funcUnit;
    851       sprintf (xtisa_error_msg, "invalid functional unit use number (%d); "
    852 	       "opcode \"%s\" has %d", u, intisa->opcodes[opc].name,
    853 	       intisa->opcodes[opc].num_funcUnit_uses);
    854       return NULL;
    855     }
    856   return &intisa->opcodes[opc].funcUnit_uses[u];
    857 }
    858 
    859 
    860 
    861 /* Operand information.  */
    863 
    864 
    865 #define CHECK_OPERAND(INTISA,OPC,ICLASS,OPND,ERRVAL) \
    866   do { \
    867     if ((OPND) < 0 || (OPND) >= (ICLASS)->num_operands) \
    868       { \
    869 	xtisa_errno = xtensa_isa_bad_operand; \
    870 	sprintf (xtisa_error_msg, "invalid operand number (%d); " \
    871 		 "opcode \"%s\" has %d operands", (OPND), \
    872 		 (INTISA)->opcodes[(OPC)].name, (ICLASS)->num_operands); \
    873 	return (ERRVAL); \
    874       } \
    875   } while (0)
    876 
    877 
    878 static xtensa_operand_internal *
    879 get_operand (xtensa_isa_internal *intisa, xtensa_opcode opc, int opnd)
    880 {
    881   xtensa_iclass_internal *iclass;
    882   int iclass_id, operand_id;
    883 
    884   CHECK_OPCODE (intisa, opc, NULL);
    885   iclass_id = intisa->opcodes[opc].iclass_id;
    886   iclass = &intisa->iclasses[iclass_id];
    887   CHECK_OPERAND (intisa, opc, iclass, opnd, NULL);
    888   operand_id = iclass->operands[opnd].u.operand_id;
    889   return &intisa->operands[operand_id];
    890 }
    891 
    892 
    893 const char *
    894 xtensa_operand_name (xtensa_isa isa, xtensa_opcode opc, int opnd)
    895 {
    896   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    897   xtensa_operand_internal *intop;
    898 
    899   intop = get_operand (intisa, opc, opnd);
    900   if (!intop) return NULL;
    901   return intop->name;
    902 }
    903 
    904 
    905 int
    906 xtensa_operand_is_visible (xtensa_isa isa, xtensa_opcode opc, int opnd)
    907 {
    908   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    909   xtensa_iclass_internal *iclass;
    910   int iclass_id, operand_id;
    911   xtensa_operand_internal *intop;
    912 
    913   CHECK_OPCODE (intisa, opc, XTENSA_UNDEFINED);
    914   iclass_id = intisa->opcodes[opc].iclass_id;
    915   iclass = &intisa->iclasses[iclass_id];
    916   CHECK_OPERAND (intisa, opc, iclass, opnd, XTENSA_UNDEFINED);
    917 
    918   /* Special case for "sout" operands.  */
    919   if (iclass->operands[opnd].inout == 's')
    920     return 0;
    921 
    922   operand_id = iclass->operands[opnd].u.operand_id;
    923   intop = &intisa->operands[operand_id];
    924 
    925   if ((intop->flags & XTENSA_OPERAND_IS_INVISIBLE) == 0)
    926     return 1;
    927   return 0;
    928 }
    929 
    930 
    931 char
    932 xtensa_operand_inout (xtensa_isa isa, xtensa_opcode opc, int opnd)
    933 {
    934   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    935   xtensa_iclass_internal *iclass;
    936   int iclass_id;
    937   char inout;
    938 
    939   CHECK_OPCODE (intisa, opc, 0);
    940   iclass_id = intisa->opcodes[opc].iclass_id;
    941   iclass = &intisa->iclasses[iclass_id];
    942   CHECK_OPERAND (intisa, opc, iclass, opnd, 0);
    943   inout = iclass->operands[opnd].inout;
    944 
    945   /* Special case for "sout" operands.  */
    946   if (inout == 's')
    947     return 'o';
    948 
    949   return inout;
    950 }
    951 
    952 
    953 int
    954 xtensa_operand_get_field (xtensa_isa isa, xtensa_opcode opc, int opnd,
    955 			  xtensa_format fmt, int slot,
    956 			  const xtensa_insnbuf slotbuf, uint32 *valp)
    957 {
    958   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    959   xtensa_operand_internal *intop;
    960   int slot_id;
    961   xtensa_get_field_fn get_fn;
    962 
    963   intop = get_operand (intisa, opc, opnd);
    964   if (!intop) return -1;
    965 
    966   CHECK_FORMAT (intisa, fmt, -1);
    967   CHECK_SLOT (intisa, fmt, slot, -1);
    968 
    969   slot_id = intisa->formats[fmt].slot_id[slot];
    970   if (intop->field_id == XTENSA_UNDEFINED)
    971     {
    972       xtisa_errno = xtensa_isa_no_field;
    973       strcpy (xtisa_error_msg, "implicit operand has no field");
    974       return -1;
    975     }
    976   get_fn = intisa->slots[slot_id].get_field_fns[intop->field_id];
    977   if (!get_fn)
    978     {
    979       xtisa_errno = xtensa_isa_wrong_slot;
    980       sprintf (xtisa_error_msg,
    981 	       "operand \"%s\" does not exist in slot %d of format \"%s\"",
    982 	       intop->name, slot, intisa->formats[fmt].name);
    983       return -1;
    984     }
    985   *valp = (*get_fn) (slotbuf);
    986   return 0;
    987 }
    988 
    989 
    990 int
    991 xtensa_operand_set_field (xtensa_isa isa, xtensa_opcode opc, int opnd,
    992 			  xtensa_format fmt, int slot,
    993 			  xtensa_insnbuf slotbuf, uint32 val)
    994 {
    995   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
    996   xtensa_operand_internal *intop;
    997   int slot_id;
    998   xtensa_set_field_fn set_fn;
    999 
   1000   intop = get_operand (intisa, opc, opnd);
   1001   if (!intop) return -1;
   1002 
   1003   CHECK_FORMAT (intisa, fmt, -1);
   1004   CHECK_SLOT (intisa, fmt, slot, -1);
   1005 
   1006   slot_id = intisa->formats[fmt].slot_id[slot];
   1007   if (intop->field_id == XTENSA_UNDEFINED)
   1008     {
   1009       xtisa_errno = xtensa_isa_no_field;
   1010       strcpy (xtisa_error_msg, "implicit operand has no field");
   1011       return -1;
   1012     }
   1013   set_fn = intisa->slots[slot_id].set_field_fns[intop->field_id];
   1014   if (!set_fn)
   1015     {
   1016       xtisa_errno = xtensa_isa_wrong_slot;
   1017       sprintf (xtisa_error_msg,
   1018 	       "operand \"%s\" does not exist in slot %d of format \"%s\"",
   1019 	       intop->name, slot, intisa->formats[fmt].name);
   1020       return -1;
   1021     }
   1022   (*set_fn) (slotbuf, val);
   1023   return 0;
   1024 }
   1025 
   1026 
   1027 int
   1028 xtensa_operand_encode (xtensa_isa isa, xtensa_opcode opc, int opnd,
   1029 		       uint32 *valp)
   1030 {
   1031   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1032   xtensa_operand_internal *intop;
   1033   uint32 test_val, orig_val;
   1034 
   1035   intop = get_operand (intisa, opc, opnd);
   1036   if (!intop)
   1037     return -1;
   1038 
   1039   if (!intop->encode)
   1040     {
   1041       /* This is a default operand for a field.  How can we tell if the
   1042 	 value fits in the field?  Write the value into the field,
   1043 	 read it back, and then make sure we get the same value.  */
   1044       static xtensa_insnbuf tmpbuf = 0;
   1045       int slot_id;
   1046 
   1047       if (!tmpbuf)
   1048 	{
   1049 	  tmpbuf = xtensa_insnbuf_alloc (isa);
   1050 	  CHECK_ALLOC (tmpbuf, -1);
   1051 	}
   1052 
   1053       /* A default operand is always associated with a field,
   1054 	 but check just to be sure....  */
   1055       if (intop->field_id == XTENSA_UNDEFINED)
   1056 	{
   1057 	  xtisa_errno = xtensa_isa_internal_error;
   1058 	  strcpy (xtisa_error_msg, "operand has no field");
   1059 	  return -1;
   1060 	}
   1061 
   1062       /* Find some slot that includes the field.  */
   1063       for (slot_id = 0; slot_id < intisa->num_slots; slot_id++)
   1064 	{
   1065 	  xtensa_get_field_fn get_fn =
   1066 	    intisa->slots[slot_id].get_field_fns[intop->field_id];
   1067 	  xtensa_set_field_fn set_fn =
   1068 	    intisa->slots[slot_id].set_field_fns[intop->field_id];
   1069 
   1070 	  if (get_fn && set_fn)
   1071 	    {
   1072 	      (*set_fn) (tmpbuf, *valp);
   1073 	      return ((*get_fn) (tmpbuf) != *valp);
   1074 	    }
   1075 	}
   1076 
   1077       /* Couldn't find any slot containing the field....  */
   1078       xtisa_errno = xtensa_isa_no_field;
   1079       strcpy (xtisa_error_msg, "field does not exist in any slot");
   1080       return -1;
   1081     }
   1082 
   1083   /* Encode the value.  In some cases, the encoding function may detect
   1084      errors, but most of the time the only way to determine if the value
   1085      was successfully encoded is to decode it and check if it matches
   1086      the original value.  */
   1087   orig_val = *valp;
   1088   if ((*intop->encode) (valp)
   1089       || (test_val = *valp, (*intop->decode) (&test_val))
   1090       || test_val != orig_val)
   1091     {
   1092       xtisa_errno = xtensa_isa_bad_value;
   1093       sprintf (xtisa_error_msg, "cannot encode operand value 0x%08x", *valp);
   1094       return -1;
   1095     }
   1096 
   1097   return 0;
   1098 }
   1099 
   1100 
   1101 int
   1102 xtensa_operand_decode (xtensa_isa isa, xtensa_opcode opc, int opnd,
   1103 		       uint32 *valp)
   1104 {
   1105   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1106   xtensa_operand_internal *intop;
   1107 
   1108   intop = get_operand (intisa, opc, opnd);
   1109   if (!intop) return -1;
   1110 
   1111   /* Use identity function for "default" operands.  */
   1112   if (!intop->decode)
   1113     return 0;
   1114 
   1115   if ((*intop->decode) (valp))
   1116     {
   1117       xtisa_errno = xtensa_isa_bad_value;
   1118       sprintf (xtisa_error_msg, "cannot decode operand value 0x%08x", *valp);
   1119       return -1;
   1120     }
   1121   return 0;
   1122 }
   1123 
   1124 
   1125 int
   1126 xtensa_operand_is_register (xtensa_isa isa, xtensa_opcode opc, int opnd)
   1127 {
   1128   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1129   xtensa_operand_internal *intop;
   1130 
   1131   intop = get_operand (intisa, opc, opnd);
   1132   if (!intop) return XTENSA_UNDEFINED;
   1133 
   1134   if ((intop->flags & XTENSA_OPERAND_IS_REGISTER) != 0)
   1135     return 1;
   1136   return 0;
   1137 }
   1138 
   1139 
   1140 xtensa_regfile
   1141 xtensa_operand_regfile (xtensa_isa isa, xtensa_opcode opc, int opnd)
   1142 {
   1143   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1144   xtensa_operand_internal *intop;
   1145 
   1146   intop = get_operand (intisa, opc, opnd);
   1147   if (!intop) return XTENSA_UNDEFINED;
   1148 
   1149   return intop->regfile;
   1150 }
   1151 
   1152 
   1153 int
   1154 xtensa_operand_num_regs (xtensa_isa isa, xtensa_opcode opc, int opnd)
   1155 {
   1156   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1157   xtensa_operand_internal *intop;
   1158 
   1159   intop = get_operand (intisa, opc, opnd);
   1160   if (!intop) return XTENSA_UNDEFINED;
   1161 
   1162   return intop->num_regs;
   1163 }
   1164 
   1165 
   1166 int
   1167 xtensa_operand_is_known_reg (xtensa_isa isa, xtensa_opcode opc, int opnd)
   1168 {
   1169   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1170   xtensa_operand_internal *intop;
   1171 
   1172   intop = get_operand (intisa, opc, opnd);
   1173   if (!intop) return XTENSA_UNDEFINED;
   1174 
   1175   if ((intop->flags & XTENSA_OPERAND_IS_UNKNOWN) == 0)
   1176     return 1;
   1177   return 0;
   1178 }
   1179 
   1180 
   1181 int
   1182 xtensa_operand_is_PCrelative (xtensa_isa isa, xtensa_opcode opc, int opnd)
   1183 {
   1184   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1185   xtensa_operand_internal *intop;
   1186 
   1187   intop = get_operand (intisa, opc, opnd);
   1188   if (!intop) return XTENSA_UNDEFINED;
   1189 
   1190   if ((intop->flags & XTENSA_OPERAND_IS_PCRELATIVE) != 0)
   1191     return 1;
   1192   return 0;
   1193 }
   1194 
   1195 
   1196 int
   1197 xtensa_operand_do_reloc (xtensa_isa isa, xtensa_opcode opc, int opnd,
   1198 			 uint32 *valp, uint32 pc)
   1199 {
   1200   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1201   xtensa_operand_internal *intop;
   1202 
   1203   intop = get_operand (intisa, opc, opnd);
   1204   if (!intop) return -1;
   1205 
   1206   if ((intop->flags & XTENSA_OPERAND_IS_PCRELATIVE) == 0)
   1207     return 0;
   1208 
   1209   if (!intop->do_reloc)
   1210     {
   1211       xtisa_errno = xtensa_isa_internal_error;
   1212       strcpy (xtisa_error_msg, "operand missing do_reloc function");
   1213       return -1;
   1214     }
   1215 
   1216   if ((*intop->do_reloc) (valp, pc))
   1217     {
   1218       xtisa_errno = xtensa_isa_bad_value;
   1219       sprintf (xtisa_error_msg,
   1220 	       "do_reloc failed for value 0x%08x at PC 0x%08x", *valp, pc);
   1221       return -1;
   1222     }
   1223 
   1224   return 0;
   1225 }
   1226 
   1227 
   1228 int
   1229 xtensa_operand_undo_reloc (xtensa_isa isa, xtensa_opcode opc, int opnd,
   1230 			   uint32 *valp, uint32 pc)
   1231 {
   1232   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1233   xtensa_operand_internal *intop;
   1234 
   1235   intop = get_operand (intisa, opc, opnd);
   1236   if (!intop) return -1;
   1237 
   1238   if ((intop->flags & XTENSA_OPERAND_IS_PCRELATIVE) == 0)
   1239     return 0;
   1240 
   1241   if (!intop->undo_reloc)
   1242     {
   1243       xtisa_errno = xtensa_isa_internal_error;
   1244       strcpy (xtisa_error_msg, "operand missing undo_reloc function");
   1245       return -1;
   1246     }
   1247 
   1248   if ((*intop->undo_reloc) (valp, pc))
   1249     {
   1250       xtisa_errno = xtensa_isa_bad_value;
   1251       sprintf (xtisa_error_msg,
   1252 	       "undo_reloc failed for value 0x%08x at PC 0x%08x", *valp, pc);
   1253       return -1;
   1254     }
   1255 
   1256   return 0;
   1257 }
   1258 
   1259 
   1260 
   1261 /* State Operands.  */
   1263 
   1264 
   1265 #define CHECK_STATE_OPERAND(INTISA,OPC,ICLASS,STOP,ERRVAL) \
   1266   do { \
   1267     if ((STOP) < 0 || (STOP) >= (ICLASS)->num_stateOperands) \
   1268       { \
   1269 	xtisa_errno = xtensa_isa_bad_operand; \
   1270 	sprintf (xtisa_error_msg, "invalid state operand number (%d); " \
   1271 		 "opcode \"%s\" has %d state operands", (STOP), \
   1272 		 (INTISA)->opcodes[(OPC)].name, (ICLASS)->num_stateOperands); \
   1273 	return (ERRVAL); \
   1274       } \
   1275   } while (0)
   1276 
   1277 
   1278 xtensa_state
   1279 xtensa_stateOperand_state (xtensa_isa isa, xtensa_opcode opc, int stOp)
   1280 {
   1281   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1282   xtensa_iclass_internal *iclass;
   1283   int iclass_id;
   1284 
   1285   CHECK_OPCODE (intisa, opc, XTENSA_UNDEFINED);
   1286   iclass_id = intisa->opcodes[opc].iclass_id;
   1287   iclass = &intisa->iclasses[iclass_id];
   1288   CHECK_STATE_OPERAND (intisa, opc, iclass, stOp, XTENSA_UNDEFINED);
   1289   return iclass->stateOperands[stOp].u.state;
   1290 }
   1291 
   1292 
   1293 char
   1294 xtensa_stateOperand_inout (xtensa_isa isa, xtensa_opcode opc, int stOp)
   1295 {
   1296   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1297   xtensa_iclass_internal *iclass;
   1298   int iclass_id;
   1299 
   1300   CHECK_OPCODE (intisa, opc, 0);
   1301   iclass_id = intisa->opcodes[opc].iclass_id;
   1302   iclass = &intisa->iclasses[iclass_id];
   1303   CHECK_STATE_OPERAND (intisa, opc, iclass, stOp, 0);
   1304   return iclass->stateOperands[stOp].inout;
   1305 }
   1306 
   1307 
   1308 
   1309 /* Interface Operands.  */
   1311 
   1312 
   1313 #define CHECK_INTERFACE_OPERAND(INTISA,OPC,ICLASS,IFOP,ERRVAL) \
   1314   do { \
   1315     if ((IFOP) < 0 || (IFOP) >= (ICLASS)->num_interfaceOperands) \
   1316       { \
   1317 	xtisa_errno = xtensa_isa_bad_operand; \
   1318 	sprintf (xtisa_error_msg, "invalid interface operand number (%d); " \
   1319 		 "opcode \"%s\" has %d interface operands", (IFOP), \
   1320 		 (INTISA)->opcodes[(OPC)].name, \
   1321 		 (ICLASS)->num_interfaceOperands); \
   1322 	return (ERRVAL); \
   1323       } \
   1324   } while (0)
   1325 
   1326 
   1327 xtensa_interface
   1328 xtensa_interfaceOperand_interface (xtensa_isa isa, xtensa_opcode opc,
   1329 				   int ifOp)
   1330 {
   1331   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1332   xtensa_iclass_internal *iclass;
   1333   int iclass_id;
   1334 
   1335   CHECK_OPCODE (intisa, opc, XTENSA_UNDEFINED);
   1336   iclass_id = intisa->opcodes[opc].iclass_id;
   1337   iclass = &intisa->iclasses[iclass_id];
   1338   CHECK_INTERFACE_OPERAND (intisa, opc, iclass, ifOp, XTENSA_UNDEFINED);
   1339   return iclass->interfaceOperands[ifOp];
   1340 }
   1341 
   1342 
   1343 
   1344 /* Register Files.  */
   1346 
   1347 
   1348 #define CHECK_REGFILE(INTISA,RF,ERRVAL) \
   1349   do { \
   1350     if ((RF) < 0 || (RF) >= (INTISA)->num_regfiles) \
   1351       { \
   1352 	xtisa_errno = xtensa_isa_bad_regfile; \
   1353 	strcpy (xtisa_error_msg, "invalid regfile specifier"); \
   1354 	return (ERRVAL); \
   1355       } \
   1356   } while (0)
   1357 
   1358 
   1359 xtensa_regfile
   1360 xtensa_regfile_lookup (xtensa_isa isa, const char *name)
   1361 {
   1362   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1363   int n;
   1364 
   1365   if (!name || !*name)
   1366     {
   1367       xtisa_errno = xtensa_isa_bad_regfile;
   1368       strcpy (xtisa_error_msg, "invalid regfile name");
   1369       return XTENSA_UNDEFINED;
   1370     }
   1371 
   1372   /* The expected number of regfiles is small; use a linear search.  */
   1373   for (n = 0; n < intisa->num_regfiles; n++)
   1374     {
   1375       if (!filename_cmp (intisa->regfiles[n].name, name))
   1376 	return n;
   1377     }
   1378 
   1379   xtisa_errno = xtensa_isa_bad_regfile;
   1380   sprintf (xtisa_error_msg, "regfile \"%s\" not recognized", name);
   1381   return XTENSA_UNDEFINED;
   1382 }
   1383 
   1384 
   1385 xtensa_regfile
   1386 xtensa_regfile_lookup_shortname (xtensa_isa isa, const char *shortname)
   1387 {
   1388   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1389   int n;
   1390 
   1391   if (!shortname || !*shortname)
   1392     {
   1393       xtisa_errno = xtensa_isa_bad_regfile;
   1394       strcpy (xtisa_error_msg, "invalid regfile shortname");
   1395       return XTENSA_UNDEFINED;
   1396     }
   1397 
   1398   /* The expected number of regfiles is small; use a linear search.  */
   1399   for (n = 0; n < intisa->num_regfiles; n++)
   1400     {
   1401       /* Ignore regfile views since they always have the same shortnames
   1402 	 as their parents.  */
   1403       if (intisa->regfiles[n].parent != n)
   1404 	continue;
   1405       if (!filename_cmp (intisa->regfiles[n].shortname, shortname))
   1406 	return n;
   1407     }
   1408 
   1409   xtisa_errno = xtensa_isa_bad_regfile;
   1410   sprintf (xtisa_error_msg, "regfile shortname \"%s\" not recognized",
   1411 	   shortname);
   1412   return XTENSA_UNDEFINED;
   1413 }
   1414 
   1415 
   1416 const char *
   1417 xtensa_regfile_name (xtensa_isa isa, xtensa_regfile rf)
   1418 {
   1419   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1420   CHECK_REGFILE (intisa, rf, NULL);
   1421   return intisa->regfiles[rf].name;
   1422 }
   1423 
   1424 
   1425 const char *
   1426 xtensa_regfile_shortname (xtensa_isa isa, xtensa_regfile rf)
   1427 {
   1428   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1429   CHECK_REGFILE (intisa, rf, NULL);
   1430   return intisa->regfiles[rf].shortname;
   1431 }
   1432 
   1433 
   1434 xtensa_regfile
   1435 xtensa_regfile_view_parent (xtensa_isa isa, xtensa_regfile rf)
   1436 {
   1437   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1438   CHECK_REGFILE (intisa, rf, XTENSA_UNDEFINED);
   1439   return intisa->regfiles[rf].parent;
   1440 }
   1441 
   1442 
   1443 int
   1444 xtensa_regfile_num_bits (xtensa_isa isa, xtensa_regfile rf)
   1445 {
   1446   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1447   CHECK_REGFILE (intisa, rf, XTENSA_UNDEFINED);
   1448   return intisa->regfiles[rf].num_bits;
   1449 }
   1450 
   1451 
   1452 int
   1453 xtensa_regfile_num_entries (xtensa_isa isa, xtensa_regfile rf)
   1454 {
   1455   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1456   CHECK_REGFILE (intisa, rf, XTENSA_UNDEFINED);
   1457   return intisa->regfiles[rf].num_entries;
   1458 }
   1459 
   1460 
   1461 
   1462 /* Processor States.  */
   1464 
   1465 
   1466 #define CHECK_STATE(INTISA,ST,ERRVAL) \
   1467   do { \
   1468     if ((ST) < 0 || (ST) >= (INTISA)->num_states) \
   1469       { \
   1470 	xtisa_errno = xtensa_isa_bad_state; \
   1471 	strcpy (xtisa_error_msg, "invalid state specifier"); \
   1472 	return (ERRVAL); \
   1473       } \
   1474   } while (0)
   1475 
   1476 
   1477 xtensa_state
   1478 xtensa_state_lookup (xtensa_isa isa, const char *name)
   1479 {
   1480   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1481   xtensa_lookup_entry entry, *result = 0;
   1482 
   1483   if (!name || !*name)
   1484     {
   1485       xtisa_errno = xtensa_isa_bad_state;
   1486       strcpy (xtisa_error_msg, "invalid state name");
   1487       return XTENSA_UNDEFINED;
   1488     }
   1489 
   1490   if (intisa->num_states != 0)
   1491     {
   1492       entry.key = name;
   1493       result = bsearch (&entry, intisa->state_lookup_table, intisa->num_states,
   1494 			sizeof (xtensa_lookup_entry), xtensa_isa_name_compare);
   1495     }
   1496 
   1497   if (!result)
   1498     {
   1499       xtisa_errno = xtensa_isa_bad_state;
   1500       sprintf (xtisa_error_msg, "state \"%s\" not recognized", name);
   1501       return XTENSA_UNDEFINED;
   1502     }
   1503 
   1504   return result->u.state;
   1505 }
   1506 
   1507 
   1508 const char *
   1509 xtensa_state_name (xtensa_isa isa, xtensa_state st)
   1510 {
   1511   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1512   CHECK_STATE (intisa, st, NULL);
   1513   return intisa->states[st].name;
   1514 }
   1515 
   1516 
   1517 int
   1518 xtensa_state_num_bits (xtensa_isa isa, xtensa_state st)
   1519 {
   1520   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1521   CHECK_STATE (intisa, st, XTENSA_UNDEFINED);
   1522   return intisa->states[st].num_bits;
   1523 }
   1524 
   1525 
   1526 int
   1527 xtensa_state_is_exported (xtensa_isa isa, xtensa_state st)
   1528 {
   1529   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1530   CHECK_STATE (intisa, st, XTENSA_UNDEFINED);
   1531   if ((intisa->states[st].flags & XTENSA_STATE_IS_EXPORTED) != 0)
   1532     return 1;
   1533   return 0;
   1534 }
   1535 
   1536 
   1537 int
   1538 xtensa_state_is_shared_or (xtensa_isa isa, xtensa_state st)
   1539 {
   1540   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1541   CHECK_STATE (intisa, st, XTENSA_UNDEFINED);
   1542   if ((intisa->states[st].flags & XTENSA_STATE_IS_SHARED_OR) != 0)
   1543     return 1;
   1544   return 0;
   1545 }
   1546 
   1547 
   1548 
   1549 /* Sysregs.  */
   1551 
   1552 
   1553 #define CHECK_SYSREG(INTISA,SYSREG,ERRVAL) \
   1554   do { \
   1555     if ((SYSREG) < 0 || (SYSREG) >= (INTISA)->num_sysregs) \
   1556       { \
   1557 	xtisa_errno = xtensa_isa_bad_sysreg; \
   1558 	strcpy (xtisa_error_msg, "invalid sysreg specifier"); \
   1559 	return (ERRVAL); \
   1560       } \
   1561   } while (0)
   1562 
   1563 
   1564 xtensa_sysreg
   1565 xtensa_sysreg_lookup (xtensa_isa isa, int num, int is_user)
   1566 {
   1567   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1568 
   1569   if (is_user != 0)
   1570     is_user = 1;
   1571 
   1572   if (num < 0 || num > intisa->max_sysreg_num[is_user]
   1573       || intisa->sysreg_table[is_user][num] == XTENSA_UNDEFINED)
   1574     {
   1575       xtisa_errno = xtensa_isa_bad_sysreg;
   1576       strcpy (xtisa_error_msg, "sysreg not recognized");
   1577       return XTENSA_UNDEFINED;
   1578     }
   1579 
   1580   return intisa->sysreg_table[is_user][num];
   1581 }
   1582 
   1583 
   1584 xtensa_sysreg
   1585 xtensa_sysreg_lookup_name (xtensa_isa isa, const char *name)
   1586 {
   1587   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1588   xtensa_lookup_entry entry, *result = 0;
   1589 
   1590   if (!name || !*name)
   1591     {
   1592       xtisa_errno = xtensa_isa_bad_sysreg;
   1593       strcpy (xtisa_error_msg, "invalid sysreg name");
   1594       return XTENSA_UNDEFINED;
   1595     }
   1596 
   1597   if (intisa->num_sysregs != 0)
   1598     {
   1599       entry.key = name;
   1600       result = bsearch (&entry, intisa->sysreg_lookup_table,
   1601 			intisa->num_sysregs, sizeof (xtensa_lookup_entry),
   1602 			xtensa_isa_name_compare);
   1603     }
   1604 
   1605   if (!result)
   1606     {
   1607       xtisa_errno = xtensa_isa_bad_sysreg;
   1608       sprintf (xtisa_error_msg, "sysreg \"%s\" not recognized", name);
   1609       return XTENSA_UNDEFINED;
   1610     }
   1611 
   1612   return result->u.sysreg;
   1613 }
   1614 
   1615 
   1616 const char *
   1617 xtensa_sysreg_name (xtensa_isa isa, xtensa_sysreg sysreg)
   1618 {
   1619   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1620   CHECK_SYSREG (intisa, sysreg, NULL);
   1621   return intisa->sysregs[sysreg].name;
   1622 }
   1623 
   1624 
   1625 int
   1626 xtensa_sysreg_number (xtensa_isa isa, xtensa_sysreg sysreg)
   1627 {
   1628   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1629   CHECK_SYSREG (intisa, sysreg, XTENSA_UNDEFINED);
   1630   return intisa->sysregs[sysreg].number;
   1631 }
   1632 
   1633 
   1634 int
   1635 xtensa_sysreg_is_user (xtensa_isa isa, xtensa_sysreg sysreg)
   1636 {
   1637   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1638   CHECK_SYSREG (intisa, sysreg, XTENSA_UNDEFINED);
   1639   if (intisa->sysregs[sysreg].is_user)
   1640     return 1;
   1641   return 0;
   1642 }
   1643 
   1644 
   1645 
   1646 /* Interfaces.  */
   1648 
   1649 
   1650 #define CHECK_INTERFACE(INTISA,INTF,ERRVAL) \
   1651   do { \
   1652     if ((INTF) < 0 || (INTF) >= (INTISA)->num_interfaces) \
   1653       { \
   1654 	xtisa_errno = xtensa_isa_bad_interface; \
   1655 	strcpy (xtisa_error_msg, "invalid interface specifier"); \
   1656 	return (ERRVAL); \
   1657       } \
   1658   } while (0)
   1659 
   1660 
   1661 xtensa_interface
   1662 xtensa_interface_lookup (xtensa_isa isa, const char *ifname)
   1663 {
   1664   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1665   xtensa_lookup_entry entry, *result = 0;
   1666 
   1667   if (!ifname || !*ifname)
   1668     {
   1669       xtisa_errno = xtensa_isa_bad_interface;
   1670       strcpy (xtisa_error_msg, "invalid interface name");
   1671       return XTENSA_UNDEFINED;
   1672     }
   1673 
   1674   if (intisa->num_interfaces != 0)
   1675     {
   1676       entry.key = ifname;
   1677       result = bsearch (&entry, intisa->interface_lookup_table,
   1678 			intisa->num_interfaces, sizeof (xtensa_lookup_entry),
   1679 			xtensa_isa_name_compare);
   1680     }
   1681 
   1682   if (!result)
   1683     {
   1684       xtisa_errno = xtensa_isa_bad_interface;
   1685       sprintf (xtisa_error_msg, "interface \"%s\" not recognized", ifname);
   1686       return XTENSA_UNDEFINED;
   1687     }
   1688 
   1689   return result->u.intf;
   1690 }
   1691 
   1692 
   1693 const char *
   1694 xtensa_interface_name (xtensa_isa isa, xtensa_interface intf)
   1695 {
   1696   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1697   CHECK_INTERFACE (intisa, intf, NULL);
   1698   return intisa->interfaces[intf].name;
   1699 }
   1700 
   1701 
   1702 int
   1703 xtensa_interface_num_bits (xtensa_isa isa, xtensa_interface intf)
   1704 {
   1705   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1706   CHECK_INTERFACE (intisa, intf, XTENSA_UNDEFINED);
   1707   return intisa->interfaces[intf].num_bits;
   1708 }
   1709 
   1710 
   1711 char
   1712 xtensa_interface_inout (xtensa_isa isa, xtensa_interface intf)
   1713 {
   1714   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1715   CHECK_INTERFACE (intisa, intf, 0);
   1716   return intisa->interfaces[intf].inout;
   1717 }
   1718 
   1719 
   1720 int
   1721 xtensa_interface_has_side_effect (xtensa_isa isa, xtensa_interface intf)
   1722 {
   1723   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1724   CHECK_INTERFACE (intisa, intf, XTENSA_UNDEFINED);
   1725   if ((intisa->interfaces[intf].flags & XTENSA_INTERFACE_HAS_SIDE_EFFECT) != 0)
   1726     return 1;
   1727   return 0;
   1728 }
   1729 
   1730 
   1731 int
   1732 xtensa_interface_class_id (xtensa_isa isa, xtensa_interface intf)
   1733 {
   1734   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1735   CHECK_INTERFACE (intisa, intf, XTENSA_UNDEFINED);
   1736   return intisa->interfaces[intf].class_id;
   1737 }
   1738 
   1739 
   1740 
   1741 /* Functional Units.  */
   1743 
   1744 
   1745 #define CHECK_FUNCUNIT(INTISA,FUN,ERRVAL) \
   1746   do { \
   1747     if ((FUN) < 0 || (FUN) >= (INTISA)->num_funcUnits) \
   1748       { \
   1749 	xtisa_errno = xtensa_isa_bad_funcUnit; \
   1750 	strcpy (xtisa_error_msg, "invalid functional unit specifier"); \
   1751 	return (ERRVAL); \
   1752       } \
   1753   } while (0)
   1754 
   1755 
   1756 xtensa_funcUnit
   1757 xtensa_funcUnit_lookup (xtensa_isa isa, const char *fname)
   1758 {
   1759   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1760   xtensa_lookup_entry entry, *result = 0;
   1761 
   1762   if (!fname || !*fname)
   1763     {
   1764       xtisa_errno = xtensa_isa_bad_funcUnit;
   1765       strcpy (xtisa_error_msg, "invalid functional unit name");
   1766       return XTENSA_UNDEFINED;
   1767     }
   1768 
   1769   if (intisa->num_funcUnits != 0)
   1770     {
   1771       entry.key = fname;
   1772       result = bsearch (&entry, intisa->funcUnit_lookup_table,
   1773 			intisa->num_funcUnits, sizeof (xtensa_lookup_entry),
   1774 			xtensa_isa_name_compare);
   1775     }
   1776 
   1777   if (!result)
   1778     {
   1779       xtisa_errno = xtensa_isa_bad_funcUnit;
   1780       sprintf (xtisa_error_msg,
   1781 	       "functional unit \"%s\" not recognized", fname);
   1782       return XTENSA_UNDEFINED;
   1783     }
   1784 
   1785   return result->u.fun;
   1786 }
   1787 
   1788 
   1789 const char *
   1790 xtensa_funcUnit_name (xtensa_isa isa, xtensa_funcUnit fun)
   1791 {
   1792   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1793   CHECK_FUNCUNIT (intisa, fun, NULL);
   1794   return intisa->funcUnits[fun].name;
   1795 }
   1796 
   1797 
   1798 int
   1799 xtensa_funcUnit_num_copies (xtensa_isa isa, xtensa_funcUnit fun)
   1800 {
   1801   xtensa_isa_internal *intisa = (xtensa_isa_internal *) isa;
   1802   CHECK_FUNCUNIT (intisa, fun, XTENSA_UNDEFINED);
   1803   return intisa->funcUnits[fun].num_copies;
   1804 }
   1805 
   1806