home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / opcodes / sparc-dis.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  25KB  |  917 lines

  1. /* Print SPARC instructions.
  2.    Copyright (C) 1989, 91-93, 1995, 1996 Free Software Foundation, Inc.
  3.  
  4. This program 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 2 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  17.  
  18. #include "ansidecl.h"
  19. #include "opcode/sparc.h"
  20. #include "dis-asm.h"
  21. #include "libiberty.h"
  22. #include <string.h>
  23.  
  24. /* Bitmask of v9 architectures.  */
  25. #define MASK_V9 ((1 << SPARC_OPCODE_ARCH_V9) \
  26.          | (1 << SPARC_OPCODE_ARCH_V9A))
  27. /* 1 if INSN is for v9 only.  */
  28. #define V9_ONLY_P(insn) (! ((insn)->architecture & ~MASK_V9))
  29. /* 1 if INSN is for v9.  */
  30. #define V9_P(insn) (((insn)->architecture & MASK_V9) != 0)
  31.  
  32. /* For faster lookup, after insns are sorted they are hashed.  */
  33. /* ??? I think there is room for even more improvement.  */
  34.  
  35. #define HASH_SIZE 256
  36. /* It is important that we only look at insn code bits as that is how the
  37.    opcode table is hashed.  OPCODE_BITS is a table of valid bits for each
  38.    of the main types (0,1,2,3).  */
  39. static int opcode_bits[4] = { 0x01c00000, 0x0, 0x01f80000, 0x01f80000 };
  40. #define HASH_INSN(INSN) \
  41.   ((((INSN) >> 24) & 0xc0) | (((INSN) & opcode_bits[((INSN) >> 30) & 3]) >> 19))
  42. struct opcode_hash {
  43.   struct opcode_hash *next;
  44.   struct sparc_opcode *opcode;
  45. };
  46. static struct opcode_hash *opcode_hash_table[HASH_SIZE];
  47. static void build_hash_table ();
  48.  
  49. /* Sign-extend a value which is N bits long.  */
  50. #define    SEX(value, bits) \
  51.     ((((int)(value)) << ((8 * sizeof (int)) - bits))    \
  52.              >> ((8 * sizeof (int)) - bits) )
  53.  
  54. static  char *reg_names[] =
  55. { "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",    
  56.   "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",    
  57.   "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",    
  58.   "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7",    
  59.   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",    
  60.   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",    
  61.   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
  62.   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
  63.   "f32", "f33", "f34", "f35", "f36", "f37", "f38", "f39",    
  64.   "f40", "f41", "f42", "f43", "f44", "f45", "f46", "f47",    
  65.   "f48", "f49", "f50", "f51", "f52", "f53", "f54", "f55",
  66.   "f56", "f57", "f58", "f59", "f60", "f61", "f62", "f63",
  67. /* psr, wim, tbr, fpsr, cpsr are v8 only.  */
  68.   "y", "psr", "wim", "tbr", "pc", "npc", "fpsr", "cpsr"
  69. };
  70.  
  71. #define    freg_names    (®_names[4 * 8])
  72.  
  73. /* These are ordered according to there register number in
  74.    rdpr and wrpr insns.  */
  75. static char *v9_priv_reg_names[] =
  76. {
  77.   "tpc", "tnpc", "tstate", "tt", "tick", "tba", "pstate", "tl",
  78.   "pil", "cwp", "cansave", "canrestore", "cleanwin", "otherwin",
  79.   "wstate", "fq"
  80.   /* "ver" - special cased */
  81. };
  82.  
  83. /* Macros used to extract instruction fields.  Not all fields have
  84.    macros defined here, only those which are actually used.  */
  85.  
  86. #define X_RD(i) (((i) >> 25) & 0x1f)
  87. #define X_RS1(i) (((i) >> 14) & 0x1f)
  88. #define X_LDST_I(i) (((i) >> 13) & 1)
  89. #define X_ASI(i) (((i) >> 5) & 0xff)
  90. #define X_RS2(i) (((i) >> 0) & 0x1f)
  91. #define X_IMM(i,n) (((i) >> 0) & ((1 << (n)) - 1))
  92. #define X_SIMM(i,n) SEX (X_IMM ((i), (n)), (n))
  93. #define X_DISP22(i) (((i) >> 0) & 0x3fffff)
  94. #define X_IMM22(i) X_DISP22 (i)
  95. #define X_DISP30(i) (((i) >> 0) & 0x3fffffff)
  96.  
  97. /* These are for v9.  */
  98. #define X_DISP16(i) (((((i) >> 20) & 3) << 14) | (((i) >> 0) & 0x3fff))
  99. #define X_DISP19(i) (((i) >> 0) & 0x7ffff)
  100. #define X_MEMBAR(i) ((i) & 0x7f)
  101.  
  102. /* Here is the union which was used to extract instruction fields
  103.    before the shift and mask macros were written.
  104.  
  105.    union sparc_insn
  106.      {
  107.        unsigned long int code;
  108.        struct
  109.      {
  110.        unsigned int anop:2;
  111.        #define    op    ldst.anop
  112.        unsigned int anrd:5;
  113.        #define    rd    ldst.anrd
  114.        unsigned int op3:6;
  115.        unsigned int anrs1:5;
  116.        #define    rs1    ldst.anrs1
  117.        unsigned int i:1;
  118.        unsigned int anasi:8;
  119.        #define    asi    ldst.anasi
  120.        unsigned int anrs2:5;
  121.        #define    rs2    ldst.anrs2
  122.        #define    shcnt    rs2
  123.      } ldst;
  124.        struct
  125.      {
  126.        unsigned int anop:2, anrd:5, op3:6, anrs1:5, i:1;
  127.        unsigned int IMM13:13;
  128.        #define    imm13    IMM13.IMM13
  129.      } IMM13;
  130.        struct
  131.      {
  132.        unsigned int anop:2;
  133.        unsigned int a:1;
  134.        unsigned int cond:4;
  135.        unsigned int op2:3;
  136.        unsigned int DISP22:22;
  137.        #define    disp22    branch.DISP22
  138.        #define    imm22    disp22
  139.      } branch;
  140.        struct
  141.      {
  142.        unsigned int anop:2;
  143.        unsigned int a:1;
  144.        unsigned int z:1;
  145.        unsigned int rcond:3;
  146.        unsigned int op2:3;
  147.        unsigned int DISP16HI:2;
  148.        unsigned int p:1;
  149.        unsigned int _rs1:5;
  150.        unsigned int DISP16LO:14;
  151.      } branch16;
  152.        struct
  153.      {
  154.        unsigned int anop:2;
  155.        unsigned int adisp30:30;
  156.        #define    disp30    call.adisp30
  157.      } call;
  158.      };
  159.  
  160.    */
  161.  
  162. /* Nonzero if INSN is the opcode for a delayed branch.  */
  163. static int
  164. is_delayed_branch (insn)
  165.      unsigned long insn;
  166. {
  167.   struct opcode_hash *op;
  168.  
  169.   for (op = opcode_hash_table[HASH_INSN (insn)]; op; op = op->next)
  170.     {
  171.       CONST struct sparc_opcode *opcode = op->opcode;
  172.       if ((opcode->match & insn) == opcode->match
  173.       && (opcode->lose & insn) == 0)
  174.     return (opcode->flags & F_DELAYED);
  175.     }
  176.   return 0;
  177. }
  178.  
  179. /* extern void qsort (); */
  180. static int compare_opcodes ();
  181.  
  182. /* Records current mask of SPARC_OPCODE_ARCH_FOO values, used to pass value
  183.    to compare_opcodes.  */
  184. static unsigned int current_arch_mask;
  185. static int compute_arch_mask ();
  186.  
  187. int
  188. print_insn_sparclite (memaddr, info)
  189.      bfd_vma memaddr;
  190.      disassemble_info *info;
  191. {
  192.   info->mach = bfd_mach_sparc_sparclite;
  193.  
  194.   return print_insn_sparc (memaddr, info);
  195. }
  196.  
  197. /* Print one instruction from MEMADDR on INFO->STREAM.
  198.  
  199.    We suffix the instruction with a comment that gives the absolute
  200.    address involved, as well as its symbolic form, if the instruction
  201.    is preceded by a findable `sethi' and it either adds an immediate
  202.    displacement to that register, or it is an `add' or `or' instruction
  203.    on that register.  */
  204.  
  205. int
  206. print_insn_sparc (memaddr, info)
  207.      bfd_vma memaddr;
  208.      disassemble_info *info;
  209. {
  210.   FILE *stream = info->stream;
  211.   bfd_byte buffer[4];
  212.   unsigned long insn;
  213.   register struct opcode_hash *op;
  214.   /* Nonzero of opcode table has been initialized.  */
  215.   static int opcodes_initialized = 0;
  216.   /* bfd mach number of last call.  */
  217.   static unsigned long current_mach = 0;
  218.  
  219.   if (!opcodes_initialized
  220.       || info->mach != current_mach)
  221.     {
  222.       current_arch_mask = compute_arch_mask (info->mach);
  223.       qsort ((char *) sparc_opcodes, sparc_num_opcodes,
  224.          sizeof (sparc_opcodes[0]), compare_opcodes);
  225.       build_hash_table (sparc_opcodes, opcode_hash_table, sparc_num_opcodes);
  226.       current_mach = info->mach;
  227.       opcodes_initialized = 1;
  228.     }
  229.  
  230.   {
  231.     int status =
  232.       (*info->read_memory_func) (memaddr, buffer, sizeof (buffer), info);
  233.     if (status != 0)
  234.       {
  235.     (*info->memory_error_func) (status, memaddr, info);
  236.     return -1;
  237.       }
  238.   }
  239.  
  240.   insn = bfd_getb32 (buffer);
  241.  
  242.   info->insn_info_valid = 1;            /* We do return this info */
  243.   info->insn_type = dis_nonbranch;        /* Assume non branch insn */
  244.   info->branch_delay_insns = 0;            /* Assume no delay */
  245.   info->target = 0;                /* Assume no target known */
  246.  
  247.   for (op = opcode_hash_table[HASH_INSN (insn)]; op; op = op->next)
  248.     {
  249.       CONST struct sparc_opcode *opcode = op->opcode;
  250.  
  251.       /* If the insn isn't supported by the current architecture, skip it.  */
  252.       if (! (opcode->architecture & current_arch_mask))
  253.     continue;
  254.  
  255.       if ((opcode->match & insn) == opcode->match
  256.       && (opcode->lose & insn) == 0)
  257.     {
  258.       /* Nonzero means that we have found an instruction which has
  259.          the effect of adding or or'ing the imm13 field to rs1.  */
  260.       int imm_added_to_rs1 = 0;
  261.  
  262.       /* Nonzero means that we have found a plus sign in the args
  263.          field of the opcode table.  */
  264.       int found_plus = 0;
  265.       
  266.       /* Nonzero means we have an annulled branch.  */
  267.       int is_annulled = 0;
  268.  
  269.       /* Do we have an `add' or `or' instruction combining an
  270.              immediate with rs1?  */
  271.       if (opcode->match == 0x80102000 || opcode->match == 0x80002000)
  272.       /*              (or)                 (add)  */
  273.         imm_added_to_rs1 = 1;
  274.  
  275.       if (X_RS1 (insn) != X_RD (insn)
  276.           && strchr (opcode->args, 'r') != 0)
  277.           /* Can't do simple format if source and dest are different.  */
  278.           continue;
  279.       if (X_RS2 (insn) != X_RD (insn)
  280.           && strchr (opcode->args, 'O') != 0)
  281.           /* Can't do simple format if source and dest are different.  */
  282.           continue;
  283.  
  284.       (*info->fprintf_func) (stream, opcode->name);
  285.  
  286.       {
  287.         register CONST char *s;
  288.  
  289.         if (opcode->args[0] != ',')
  290.           (*info->fprintf_func) (stream, " ");
  291.         for (s = opcode->args; *s != '\0'; ++s)
  292.           {
  293.         while (*s == ',')
  294.           {
  295.             (*info->fprintf_func) (stream, ",");
  296.             ++s;
  297.             switch (*s) {
  298.             case 'a':
  299.               (*info->fprintf_func) (stream, "a");
  300.               is_annulled = 1;
  301.               ++s;
  302.               continue;
  303.             case 'N':
  304.               (*info->fprintf_func) (stream, "pn");
  305.               ++s;
  306.               continue;
  307.  
  308.             case 'T':
  309.               (*info->fprintf_func) (stream, "pt");
  310.               ++s;
  311.               continue;
  312.  
  313.             default:
  314.               break;
  315.             }        /* switch on arg */
  316.           }        /* while there are comma started args */
  317.  
  318.         (*info->fprintf_func) (stream, " ");
  319.             
  320.         switch (*s)
  321.           {
  322.           case '+':
  323.             found_plus = 1;
  324.  
  325.             /* note fall-through */
  326.           default:
  327.             (*info->fprintf_func) (stream, "%c", *s);
  328.             break;
  329.  
  330.           case '#':
  331.             (*info->fprintf_func) (stream, "0");
  332.             break;
  333.  
  334. #define    reg(n)    (*info->fprintf_func) (stream, "%%%s", reg_names[n])
  335.           case '1':
  336.           case 'r':
  337.             reg (X_RS1 (insn));
  338.             break;
  339.  
  340.           case '2':
  341.           case 'O':
  342.             reg (X_RS2 (insn));
  343.             break;
  344.  
  345.           case 'd':
  346.             reg (X_RD (insn));
  347.             break;
  348. #undef    reg
  349.  
  350. #define    freg(n)        (*info->fprintf_func) (stream, "%%%s", freg_names[n])
  351. #define    fregx(n)    (*info->fprintf_func) (stream, "%%%s", freg_names[((n) & ~1) | (((n) & 1) << 5)])
  352.           case 'e':
  353.             freg (X_RS1 (insn));
  354.             break;
  355.           case 'v':    /* double/even */
  356.           case 'V':    /* quad/multiple of 4 */
  357.             fregx (X_RS1 (insn));
  358.             break;
  359.  
  360.           case 'f':
  361.             freg (X_RS2 (insn));
  362.             break;
  363.           case 'B':    /* double/even */
  364.           case 'R':    /* quad/multiple of 4 */
  365.             fregx (X_RS2 (insn));
  366.             break;
  367.  
  368.           case 'g':
  369.             freg (X_RD (insn));
  370.             break;
  371.           case 'H':    /* double/even */
  372.           case 'J':    /* quad/multiple of 4 */
  373.             fregx (X_RD (insn));
  374.             break;
  375. #undef    freg
  376. #undef    fregx
  377.  
  378. #define    creg(n)    (*info->fprintf_func) (stream, "%%c%u", (unsigned int) (n))
  379.           case 'b':
  380.             creg (X_RS1 (insn));
  381.             break;
  382.  
  383.           case 'c':
  384.             creg (X_RS2 (insn));
  385.             break;
  386.  
  387.           case 'D':
  388.             creg (X_RD (insn));
  389.             break;
  390. #undef    creg
  391.  
  392.           case 'h':
  393.             (*info->fprintf_func) (stream, "%%hi(%#x)",
  394.                        (0xFFFFFFFF
  395.                         & ((int) X_IMM22 (insn) << 10)));
  396.             break;
  397.  
  398.           case 'i':    /* 13 bit immediate */
  399.           case 'I':    /* 11 bit immediate */
  400.           case 'j':    /* 10 bit immediate */
  401.             {
  402.               int imm;
  403.  
  404.               if (*s == 'i')
  405.                 imm = X_SIMM (insn, 13);
  406.               else if (*s == 'I')
  407.             imm = X_SIMM (insn, 11);
  408.               else
  409.             imm = X_SIMM (insn, 10);
  410.  
  411.               /* Check to see whether we have a 1+i, and take
  412.              note of that fact.
  413.  
  414.              Note: because of the way we sort the table,
  415.              we will be matching 1+i rather than i+1,
  416.              so it is OK to assume that i is after +,
  417.              not before it.  */
  418.               if (found_plus)
  419.             imm_added_to_rs1 = 1;
  420.               
  421.               if (imm <= 9)
  422.             (*info->fprintf_func) (stream, "%d", imm);
  423.               else
  424.             (*info->fprintf_func) (stream, "%#x", imm);
  425.             }
  426.             break;
  427.  
  428.           case 'X':    /* 5 bit unsigned immediate */
  429.           case 'Y':    /* 6 bit unsigned immediate */
  430.             {
  431.               int imm = X_IMM (insn, *s == 'X' ? 5 : 6);
  432.  
  433.               if (imm <= 9)
  434.             (info->fprintf_func) (stream, "%d", imm);
  435.               else
  436.             (info->fprintf_func) (stream, "%#x", (unsigned) imm);
  437.             }
  438.             break;
  439.  
  440.           case 'K':
  441.             {
  442.               int mask = X_MEMBAR (insn);
  443.               int bit = 0x40, printed_one = 0;
  444.               char *name;
  445.  
  446.               if (mask == 0)
  447.             (info->fprintf_func) (stream, "0");
  448.               else
  449.             while (bit)
  450.               {
  451.                 if (mask & bit)
  452.                   {
  453.                 if (printed_one)
  454.                   (info->fprintf_func) (stream, "|");
  455.                 name = sparc_decode_membar (bit);
  456.                 (info->fprintf_func) (stream, "%s", name);
  457.                 printed_one = 1;
  458.                   }
  459.                 bit >>= 1;
  460.               }
  461.               break;
  462.             }
  463.  
  464.           case 'k':
  465.             info->target = memaddr + SEX (X_DISP16 (insn), 16) * 4;
  466.             (*info->print_address_func) (info->target, info);
  467.             break;
  468.  
  469.           case 'G':
  470.             info->target = memaddr + SEX (X_DISP19 (insn), 19) * 4;
  471.             (*info->print_address_func) (info->target, info);
  472.             break;
  473.  
  474.           case '6':
  475.           case '7':
  476.           case '8':
  477.           case '9':
  478.             (*info->fprintf_func) (stream, "%%fcc%c", *s - '6' + '0');
  479.             break;
  480.  
  481.           case 'z':
  482.             (*info->fprintf_func) (stream, "%%icc");
  483.             break;
  484.  
  485.           case 'Z':
  486.             (*info->fprintf_func) (stream, "%%xcc");
  487.             break;
  488.  
  489.           case 'E':
  490.             (*info->fprintf_func) (stream, "%%ccr");
  491.             break;
  492.  
  493.           case 's':
  494.             (*info->fprintf_func) (stream, "%%fprs");
  495.             break;
  496.  
  497.           case 'o':
  498.             (*info->fprintf_func) (stream, "%%asi");
  499.             break;
  500.  
  501.           case 'W':
  502.             (*info->fprintf_func) (stream, "%%tick");
  503.             break;
  504.  
  505.           case 'P':
  506.             (*info->fprintf_func) (stream, "%%pc");
  507.             break;
  508.  
  509.           case '?':
  510.             if (X_RS1 (insn) == 31)
  511.               (*info->fprintf_func) (stream, "%%ver");
  512.             else if ((unsigned) X_RS1 (insn) < 16)
  513.               (*info->fprintf_func) (stream, "%%%s",
  514.                          v9_priv_reg_names[X_RS1 (insn)]);
  515.             else
  516.               (*info->fprintf_func) (stream, "%%reserved");
  517.             break;
  518.  
  519.           case '!':
  520.             if ((unsigned) X_RD (insn) < 15)
  521.               (*info->fprintf_func) (stream, "%%%s",
  522.                          v9_priv_reg_names[X_RD (insn)]);
  523.             else
  524.               (*info->fprintf_func) (stream, "%%reserved");
  525.             break;
  526.  
  527.           case '*':
  528.             {
  529.               char *name = sparc_decode_prefetch (X_RD (insn));
  530.  
  531.               if (name)
  532.             (*info->fprintf_func) (stream, "%s", name);
  533.               else
  534.             (*info->fprintf_func) (stream, "%d", X_RD (insn));
  535.               break;
  536.             }
  537.  
  538.           case 'M':
  539.             (*info->fprintf_func) (stream, "%%asr%d", X_RS1 (insn));
  540.             break;
  541.             
  542.           case 'm':
  543.             (*info->fprintf_func) (stream, "%%asr%d", X_RD (insn));
  544.             break;
  545.             
  546.           case 'L':
  547.             info->target = memaddr + SEX (X_DISP30 (insn), 30) * 4;
  548.             (*info->print_address_func) (info->target, info);
  549.             break;
  550.  
  551.           case 'n':
  552.             (*info->fprintf_func)
  553.               (stream, "%#x", SEX (X_DISP22 (insn), 22));
  554.             break;
  555.  
  556.           case 'l':
  557.             info->target = memaddr + SEX (X_DISP22 (insn), 22) * 4;
  558.             (*info->print_address_func) (info->target, info);
  559.             break;
  560.  
  561.           case 'A':
  562.             {
  563.               char *name = sparc_decode_asi (X_ASI (insn));
  564.  
  565.               if (name)
  566.             (*info->fprintf_func) (stream, "%s", name);
  567.               else
  568.             (*info->fprintf_func) (stream, "(%d)", X_ASI (insn));
  569.               break;
  570.             }
  571.  
  572.           case 'C':
  573.             (*info->fprintf_func) (stream, "%%csr");
  574.             break;
  575.  
  576.           case 'F':
  577.             (*info->fprintf_func) (stream, "%%fsr");
  578.             break;
  579.  
  580.           case 'p':
  581.             (*info->fprintf_func) (stream, "%%psr");
  582.             break;
  583.  
  584.           case 'q':
  585.             (*info->fprintf_func) (stream, "%%fq");
  586.             break;
  587.  
  588.           case 'Q':
  589.             (*info->fprintf_func) (stream, "%%cq");
  590.             break;
  591.  
  592.           case 't':
  593.             (*info->fprintf_func) (stream, "%%tbr");
  594.             break;
  595.  
  596.           case 'w':
  597.             (*info->fprintf_func) (stream, "%%wim");
  598.             break;
  599.  
  600.           case 'x':
  601.             (*info->fprintf_func) (stream, "%d",
  602.                        ((X_LDST_I (insn) << 8)
  603.                         + X_ASI (insn)));
  604.             break;
  605.  
  606.           case 'y':
  607.             (*info->fprintf_func) (stream, "%%y");
  608.             break;
  609.  
  610.           case 'u':
  611.           case 'U':
  612.             {
  613.               int val = *s == 'U' ? X_RS1 (insn) : X_RD (insn);
  614.               char *name = sparc_decode_sparclet_cpreg (val);
  615.  
  616.               if (name)
  617.             (*info->fprintf_func) (stream, "%s", name);
  618.               else
  619.             (*info->fprintf_func) (stream, "%%cpreg(%d)", val);
  620.               break;
  621.             }
  622.           }
  623.           }
  624.       }
  625.  
  626.       /* If we are adding or or'ing something to rs1, then
  627.          check to see whether the previous instruction was
  628.          a sethi to the same register as in the sethi.
  629.          If so, attempt to print the result of the add or
  630.          or (in this context add and or do the same thing)
  631.          and its symbolic value.  */
  632.       if (imm_added_to_rs1)
  633.         {
  634.           unsigned long prev_insn;
  635.           int errcode;
  636.  
  637.           errcode =
  638.         (*info->read_memory_func)
  639.           (memaddr - 4, buffer, sizeof (buffer), info);
  640.           prev_insn = bfd_getb32 (buffer);
  641.  
  642.           if (errcode == 0)
  643.         {
  644.           /* If it is a delayed branch, we need to look at the
  645.              instruction before the delayed branch.  This handles
  646.              sequences such as
  647.  
  648.              sethi %o1, %hi(_foo), %o1
  649.              call _printf
  650.              or %o1, %lo(_foo), %o1
  651.              */
  652.  
  653.           if (is_delayed_branch (prev_insn))
  654.             {
  655.               errcode = (*info->read_memory_func)
  656.             (memaddr - 8, buffer, sizeof (buffer), info);
  657.               prev_insn = bfd_getb32 (buffer);
  658.             }
  659.         }
  660.  
  661.           /* If there was a problem reading memory, then assume
  662.          the previous instruction was not sethi.  */
  663.           if (errcode == 0)
  664.         {
  665.           /* Is it sethi to the same register?  */
  666.           if ((prev_insn & 0xc1c00000) == 0x01000000
  667.               && X_RD (prev_insn) == X_RS1 (insn))
  668.             {
  669.               (*info->fprintf_func) (stream, "\t! ");
  670.               info->target = 
  671.             (0xFFFFFFFF & (int) X_IMM22 (prev_insn) << 10)
  672.             | X_SIMM (insn, 13);
  673.               (*info->print_address_func) (info->target, info);
  674.               info->insn_type = dis_dref;
  675.               info->data_size = 4;  /* FIXME!!! */
  676.             }
  677.         }
  678.         }
  679.  
  680.       if (opcode->flags & (F_UNBR|F_CONDBR|F_JSR))
  681.         {
  682.         /* FIXME -- check is_annulled flag */
  683.           if (opcode->flags & F_UNBR)
  684.         info->insn_type = dis_branch;
  685.           if (opcode->flags & F_CONDBR)
  686.         info->insn_type = dis_condbranch;
  687.           if (opcode->flags & F_JSR)
  688.         info->insn_type = dis_jsr;
  689.           if (opcode->flags & F_DELAYED)
  690.         info->branch_delay_insns = 1;
  691.         }
  692.  
  693.       return sizeof (buffer);
  694.     }
  695.     }
  696.  
  697.   info->insn_type = dis_noninsn;    /* Mark as non-valid instruction */
  698.   (*info->fprintf_func) (stream, "unknown");
  699.   return sizeof (buffer);
  700. }
  701.  
  702. /* Given BFD mach number, return a mask of SPARC_OPCODE_ARCH_FOO values.  */
  703.  
  704. static int
  705. compute_arch_mask (mach)
  706.      unsigned long mach;
  707. {
  708.   switch (mach)
  709.     {
  710.     case 0 :
  711.     case bfd_mach_sparc :
  712.       return SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V8);
  713.     case bfd_mach_sparc_sparclet :
  714.       return SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_SPARCLET);
  715.     case bfd_mach_sparc_sparclite :
  716.       /* sparclites insns are recognized by default (because that's how
  717.      they've always been treated, for better or worse).  Kludge this by
  718.      indicating generic v8 is also selected.  */
  719.       return (SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_SPARCLITE)
  720.           | SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V8));
  721.     case bfd_mach_sparc_v8plus :
  722.     case bfd_mach_sparc_v9 :
  723.       return SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V9);
  724.     case bfd_mach_sparc_v8plusa :
  725.     case bfd_mach_sparc_v9a :
  726.       return SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V9A);
  727.     }
  728.   abort ();
  729. }
  730.  
  731. /* Compare opcodes A and B.  */
  732.  
  733. static int
  734. compare_opcodes (a, b)
  735.      char *a, *b;
  736. {
  737.   struct sparc_opcode *op0 = (struct sparc_opcode *) a;
  738.   struct sparc_opcode *op1 = (struct sparc_opcode *) b;
  739.   unsigned long int match0 = op0->match, match1 = op1->match;
  740.   unsigned long int lose0 = op0->lose, lose1 = op1->lose;
  741.   register unsigned int i;
  742.  
  743.   /* If one (and only one) insn isn't supported by the current architecture,
  744.      prefer the one that is.  If neither are supported, but they're both for
  745.      the same architecture, continue processing.  Otherwise (both unsupported
  746.      and for different architectures), prefer lower numbered arch's (fudged
  747.      by comparing the bitmasks).  */
  748.   if (op0->architecture & current_arch_mask)
  749.     {
  750.       if (! (op1->architecture & current_arch_mask))
  751.     return -1;
  752.     }
  753.   else
  754.     {
  755.       if (op1->architecture & current_arch_mask)
  756.     return 1;
  757.       else if (op0->architecture != op1->architecture)
  758.     return op0->architecture - op1->architecture;
  759.     }
  760.  
  761.   /* If a bit is set in both match and lose, there is something
  762.      wrong with the opcode table.  */
  763.   if (match0 & lose0)
  764.     {
  765.       fprintf (stderr, "Internal error:  bad sparc-opcode.h: \"%s\", %#.8lx, %#.8lx\n",
  766.            op0->name, match0, lose0);
  767.       op0->lose &= ~op0->match;
  768.       lose0 = op0->lose;
  769.     }
  770.  
  771.   if (match1 & lose1)
  772.     {
  773.       fprintf (stderr, "Internal error: bad sparc-opcode.h: \"%s\", %#.8lx, %#.8lx\n",
  774.            op1->name, match1, lose1);
  775.       op1->lose &= ~op1->match;
  776.       lose1 = op1->lose;
  777.     }
  778.  
  779.   /* Because the bits that are variable in one opcode are constant in
  780.      another, it is important to order the opcodes in the right order.  */
  781.   for (i = 0; i < 32; ++i)
  782.     {
  783.       unsigned long int x = 1 << i;
  784.       int x0 = (match0 & x) != 0;
  785.       int x1 = (match1 & x) != 0;
  786.  
  787.       if (x0 != x1)
  788.     return x1 - x0;
  789.     }
  790.  
  791.   for (i = 0; i < 32; ++i)
  792.     {
  793.       unsigned long int x = 1 << i;
  794.       int x0 = (lose0 & x) != 0;
  795.       int x1 = (lose1 & x) != 0;
  796.  
  797.       if (x0 != x1)
  798.     return x1 - x0;
  799.     }
  800.  
  801.   /* They are functionally equal.  So as long as the opcode table is
  802.      valid, we can put whichever one first we want, on aesthetic grounds.  */
  803.  
  804.   /* Our first aesthetic ground is that aliases defer to real insns.  */
  805.   {
  806.     int alias_diff = (op0->flags & F_ALIAS) - (op1->flags & F_ALIAS);
  807.     if (alias_diff != 0)
  808.       /* Put the one that isn't an alias first.  */
  809.       return alias_diff;
  810.   }
  811.  
  812.   /* Except for aliases, two "identical" instructions had
  813.      better have the same opcode.  This is a sanity check on the table.  */
  814.   i = strcmp (op0->name, op1->name);
  815.   if (i)
  816.     {
  817.       if (op0->flags & F_ALIAS) /* If they're both aliases, be arbitrary. */
  818.     return i;
  819.       else
  820.     fprintf (stderr,
  821.          "Internal error: bad sparc-opcode.h: \"%s\" == \"%s\"\n",
  822.          op0->name, op1->name);
  823.     }
  824.  
  825.   /* Fewer arguments are preferred.  */
  826.   {
  827.     int length_diff = strlen (op0->args) - strlen (op1->args);
  828.     if (length_diff != 0)
  829.       /* Put the one with fewer arguments first.  */
  830.       return length_diff;
  831.   }
  832.  
  833.   /* Put 1+i before i+1.  */
  834.   {
  835.     char *p0 = (char *) strchr(op0->args, '+');
  836.     char *p1 = (char *) strchr(op1->args, '+');
  837.  
  838.     if (p0 && p1)
  839.       {
  840.     /* There is a plus in both operands.  Note that a plus
  841.        sign cannot be the first character in args,
  842.        so the following [-1]'s are valid.  */
  843.     if (p0[-1] == 'i' && p1[1] == 'i')
  844.       /* op0 is i+1 and op1 is 1+i, so op1 goes first.  */
  845.       return 1;
  846.     if (p0[1] == 'i' && p1[-1] == 'i')
  847.       /* op0 is 1+i and op1 is i+1, so op0 goes first.  */
  848.       return -1;
  849.       }
  850.   }
  851.  
  852.   /* Put 1,i before i,1.  */
  853.   {
  854.     int i0 = strncmp (op0->args, "i,1", 3) == 0;
  855.     int i1 = strncmp (op1->args, "i,1", 3) == 0;
  856.  
  857.     if (i0 ^ i1)
  858.       return i0 - i1;
  859.   }
  860.  
  861.   /* They are, as far as we can tell, identical.
  862.      Since qsort may have rearranged the table partially, there is
  863.      no way to tell which one was first in the opcode table as
  864.      written, so just say there are equal.  */
  865.   return 0;
  866. }
  867.  
  868. /* Build a hash table from the opcode table.  */
  869.  
  870. static void
  871. build_hash_table (table, hash_table, num_opcodes)
  872.      struct sparc_opcode *table;
  873.      struct opcode_hash **hash_table;
  874.      int num_opcodes;
  875. {
  876.   register int i;
  877.   int hash_count[HASH_SIZE];
  878.   static struct opcode_hash *hash_buf = NULL;
  879.  
  880.   /* Start at the end of the table and work backwards so that each
  881.      chain is sorted.  */
  882.  
  883.   memset (hash_table, 0, HASH_SIZE * sizeof (hash_table[0]));
  884.   memset (hash_count, 0, HASH_SIZE * sizeof (hash_count[0]));
  885.   if (hash_buf != NULL)
  886.     free (hash_buf);
  887.   hash_buf = (struct opcode_hash *) xmalloc (sizeof (struct opcode_hash) * num_opcodes);
  888.   for (i = num_opcodes - 1; i >= 0; --i)
  889.     {
  890.       register int hash = HASH_INSN (sparc_opcodes[i].match);
  891.       register struct opcode_hash *h = &hash_buf[i];
  892.       h->next = hash_table[hash];
  893.       h->opcode = &sparc_opcodes[i];
  894.       hash_table[hash] = h;
  895.       ++hash_count[hash];
  896.     }
  897.  
  898. #if 0 /* for debugging */
  899.   {
  900.     int min_count = num_opcodes, max_count = 0;
  901.     int total;
  902.  
  903.     for (i = 0; i < HASH_SIZE; ++i)
  904.       {
  905.         if (hash_count[i] < min_count)
  906.       min_count = hash_count[i];
  907.     if (hash_count[i] > max_count)
  908.       max_count = hash_count[i];
  909.     total += hash_count[i];
  910.       }
  911.  
  912.     printf ("Opcode hash table stats: min %d, max %d, ave %f\n",
  913.         min_count, max_count, (double) total / HASH_SIZE);
  914.   }
  915. #endif
  916. }
  917.