Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 30th, 2012  |  syntax: None  |  size: 8.71 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <inttypes.h>
  3.  
  4. #include "pretty_print.h"
  5.  
  6. const char *opcodes[256] = {
  7.   "trap","fcmp","fun","feql","fadd","fix","fsub","fixu",              /* 0x0# */
  8.   "flot","flot","flotu","flotu","sflot","sflot","sflotu","sflotu",
  9.   "fmul","fcmpe","fune","feqle","fdiv","fsqrt","frem","fint",         /* 0x1# */
  10.   "mul","mul","mulu","mulu","div","div","divu","divu",
  11.   "add","add","addu","addu","sub","sub","subu","subu",                /* 0x2# */
  12.   "2addu","2addu","4addu","4addu","8addu","8addu","16addu","16addu",
  13.   "cmp","cmp","cmpu","cmpu","neg","neg","negu","negu",                /* 0x3# */
  14.   "sl","sl","slu","slu","sr","sr","sru","sru",
  15.   "bn","bn","bz","bz","bp","bp","bod","bod",                          /* 0x4# */
  16.   "bnn","bnn","bnz","bnz","bnp","bnp","bev","bev",
  17.   "pbn","pbn","pbz","pbz","pbp","pbp","pbod","pbod",                  /* 0x5# */
  18.   "pbnn","pbnn","pbnz","pbnz","pbnp","pbnp","pbev","pbev",
  19.   "csn","csn","csz","csz","csp","csp","csod","csod",                  /* 0x6# */
  20.   "csnn","csnn","csnz","csnz","csnp","csnp","csev","csev",
  21.   "zsn","zsn","zsz","zsz","zsp","zsp","zsod","zsod",                  /* 0x7# */
  22.   "zsnn","zsnn","zsnz","zsnz","zsnp","zsnp","zsev","zsev",
  23.   "ldb","ldb","ldbu","ldbu","ldw","ldw","ldwu","ldwu",                /* 0x8# */
  24.   "ldt","ldt","ldtu","ldtu","ldo","ldo","ldou","ldou",
  25.   "ldsf","ldsf","ldht","ldht","cswap","cswap","ldunc","ldunc",        /* 0x9# */
  26.   "ldvts","ldvts","preld","preld","prego","prego","go","go",
  27.   "stb","stb","stbu","stbu","stw","stw","stwu","stwu",                /* 0xA# */
  28.   "stt","stt","sttu","sttu","sto","sto","stou","stou",
  29.   "stsf","stsf","stht","stht","stco","stco","stunc","stunc",          /* 0xB# */
  30.   "syncd","syncd","prest","prest","syncid","syncid","pushgo","pushgo",
  31.   "or","or","orn","orn","nor","nor","xor","xor",                      /* 0xC# */
  32.   "and","and","andn","andn","nand","nand","nxor","nxor",
  33.   "bdif","bdif","wdif","wdif","tdif","tdif","odif","odif",            /* 0xD# */
  34.   "mux","mux","sadd","sadd","mor","mor","mxor","mxor",
  35.   "seth","setmh","setml","setl","inch","incmh","incml","incl",        /* 0xE# */
  36.   "orh","ormh","orml","orl","andnh","andnmh","andnml","andnl",
  37.   "jmp","jmp","pushj","pushj","geta","geta","put","put",              /* 0xF# */
  38.   "pop","resume","save","unsave","sync","swym","get","trip"
  39. };
  40.  
  41. /* kinds of arguments and format strings
  42.  * S#:  register number, e.g. $123
  43.  * #:   8-bit unsigned,  e.g. #1F
  44.  * ##:  the same, 16 Bit, e.g. #1337
  45.  * ###: the same, 24 Bit, e.g. #1F2E3D
  46.  * F##: relative 16-bit adress, forwards,  e.g.. @+4*#1234
  47.  * B##: the same, backwards, z.B. @-4*#1234
  48.  * F###, B###: the same, 24 Bit
  49.  * R#:  special register, z.B. rH
  50.  * ROUND: rounding mode, string contains comma
  51.  */
  52. enum op_type {
  53.     OP_SX_SY_SZ,
  54.     OP_SX_SY_Z,
  55.     OP_SX_Y_SZ,
  56.     OP_SX_Y_Z,
  57.     OP_X_SY_SZ,
  58.     OP_X_SY_Z,
  59. /*  OP_X_Y_SZ, */
  60. /*  OP_X_Y_Z,  */
  61.     OP_SX_YZ,
  62.     OP_XYZ,
  63. #define IS_SIMPLE_ARGF(x) ((x) <= OP_XYZ)
  64.  
  65. /* complicated argument formats */
  66.     OP_SX_ROUND_SZ,
  67.     OP_SX_ROUND_Z,
  68.     OP_SX_FYZ,
  69.     OP_SX_BYZ,
  70.     OP_FXYZ,
  71.     OP_BXYZ,
  72.     OP_RX_SZ,
  73.     OP_RX_Z,
  74.     OP_SX,
  75.     OP_SZ,
  76.     OP_SX_RZ,
  77.  
  78. /* failback mode, if illegal argument format. Never occurs in argformats */
  79.     OP_SX_Z,
  80.     OP_X_SZ,
  81.     OP_X_Z
  82. };
  83.  
  84. static const char *opfstrings[] = {
  85.     [OP_SX_SY_SZ] = "$%hu,$%hu,$%hu\n",
  86.     [OP_SX_SY_Z]  = "$%hu,$%hu,#%02hx\n",
  87.     [OP_SX_Y_SZ]  = "$%hu,#%02hx,$%hu\n",
  88.     [OP_SX_Y_Z]   = "$%hu,#%02hx,#%02hx\n",
  89.     [OP_X_SY_SZ]  = "#%02hx,$%hu,$%hu\n",
  90.     [OP_X_SY_Z]   = "#%02hx,$%hu,#%02hx\n",
  91. /*  [OP_X_Y_SZ]   = "#%02hx,#%02hx,$%hu\n", */
  92. /*  [OP_X_Y_Z]    = "#%02hx,#%02hx,#%02hx\n", */
  93.     [OP_SX_YZ]    = "$%hu,#%02hx%02hx\n",
  94.     [OP_XYZ]      = "#%02hx%02hx%02hx\n",
  95.  
  96.     [OP_SX_ROUND_SZ] = "$%hu,%s$%hu\n",
  97.     [OP_SX_ROUND_Z]  = "$%hu,%s%02hx\n",
  98. /* x, abs(yz), absolute adress */
  99.     [OP_SX_FYZ]      = "$%hu,@+4*#%04" PRIx64 " <%016" PRIx64 ">\n",
  100.     [OP_SX_BYZ]      = "$%hu,@-4*#%04" PRIx64" <%016" PRIx64 ">\n",
  101.     [OP_FXYZ]        = "@+4*#%06" PRIx64" <%016" PRIx64 ">\n",
  102.     [OP_BXYZ]        = "@-4*#%06" PRIx64" <%016" PRIx64 ">\n",
  103.     [OP_RX_SZ]       = "%s,$%hu\n",
  104.     [OP_RX_Z]        = "%s,#%02hx\n",
  105.     [OP_SX]          = "$%hu\n",
  106.     [OP_SZ]          = "$%hu\n",
  107.     [OP_SX_RZ]       = "$%hu,%s\n",
  108.  
  109.     [OP_SX_Z] = "$%hu,#%02hx\n",
  110.     [OP_X_SZ] = "#%02hx,$%hu\n",
  111.     [OP_X_Z]  = "#%02hx,%02hx\n"
  112. };
  113.  
  114. /* helpful macros */
  115. #define OP_ROUND_PAIR OP_SX_ROUND_SZ, OP_SX_ROUND_Z,
  116. #define OP_STD_PAIR   OP_SX_SY_SZ, OP_SX_SY_Z,
  117. #define OP_STD_ROW    OP_STD_PAIR OP_STD_PAIR OP_STD_PAIR OP_STD_PAIR
  118. #define OP_BR_PAIR    OP_SX_FYZ, OP_SX_BYZ,
  119. #define OP_BR_ROW     OP_BR_PAIR OP_BR_PAIR OP_BR_PAIR OP_BR_PAIR
  120. #define OP_SX_YZ_ROW  OP_SX_YZ, OP_SX_YZ, OP_SX_YZ, OP_SX_YZ, \
  121.                       OP_SX_YZ, OP_SX_YZ, OP_SX_YZ, OP_SX_YZ,
  122.  
  123. /* argument formats to the opcodes, grouped by 32 opcodes */
  124. static const unsigned char argformats[256] = {
  125.   /* trap   */ OP_XYZ,
  126.   /* fcmp   */ OP_SX_SY_SZ,
  127.   /* fun    */ OP_SX_SY_SZ,
  128.   /* feql   */ OP_SX_SY_SZ,
  129.   /* fadd   */ OP_SX_SY_SZ,
  130.   /* fix    */ OP_SX_ROUND_SZ,
  131.   /* fsub   */ OP_SX_SY_SZ,
  132.   /* fixu   */ OP_SX_ROUND_SZ,
  133.   /* flot   */ OP_ROUND_PAIR
  134.   /* flotu  */ OP_ROUND_PAIR
  135.   /* sflot  */ OP_ROUND_PAIR
  136.   /* sflotu */ OP_ROUND_PAIR
  137.   /* fmul   */ OP_SX_SY_SZ,
  138.   /* fcmpe  */ OP_SX_SY_SZ,
  139.   /* fune   */ OP_SX_SY_SZ,
  140.   /* feqle  */ OP_SX_SY_SZ,
  141.   /* fdiv   */ OP_SX_SY_SZ,
  142.   /* fsqrt  */ OP_SX_ROUND_SZ,
  143.   /* frem   */ OP_SX_SY_SZ,
  144.   /* fint   */ OP_SX_ROUND_SZ,
  145.   /* mul  - divu    */ OP_STD_ROW
  146.  
  147.   /* add  - 16addu  */ OP_STD_ROW OP_STD_ROW
  148.   /* cmp    */ OP_STD_PAIR
  149.   /* cmpu   */ OP_STD_PAIR
  150.   /* neg    */ OP_SX_Y_SZ, OP_SX_Y_Z,
  151.   /* negu   */ OP_SX_Y_SZ, OP_SX_Y_Z,
  152.   /* sl   - sru     */ OP_STD_ROW
  153.  
  154.   /* bn   - bev     */ OP_BR_ROW OP_BR_ROW
  155.   /* pbn  - pbev    */ OP_BR_ROW OP_BR_ROW
  156.  
  157.   /* csn  - csev    */ OP_STD_ROW OP_STD_ROW
  158.   /* zsn  - zsev    */ OP_STD_ROW OP_STD_ROW
  159.  
  160.   /* ldb  - ldou    */ OP_STD_ROW OP_STD_ROW
  161.   /* ldsf - go      */ OP_STD_ROW OP_STD_ROW
  162.  
  163.   /* stb  - stou    */ OP_STD_ROW OP_STD_ROW
  164.   /* stsf   */ OP_STD_PAIR
  165.   /* stht   */ OP_STD_PAIR
  166.   /* stco   */ OP_X_SY_SZ, OP_X_SY_Z,
  167.   /* stunc  */ OP_STD_PAIR
  168.   /* syncd - pushgo */ OP_STD_ROW
  169.   /* or    - nxor   */ OP_STD_ROW OP_STD_ROW
  170.   /* bdif  - mxor   */ OP_STD_ROW OP_STD_ROW
  171.  
  172.   /* seth  - andnl  */ OP_SX_YZ_ROW OP_SX_YZ_ROW
  173.   /* jmp    */ OP_FXYZ, OP_BXYZ,
  174.   /* pushj  */ OP_BR_PAIR
  175.   /* geta   */ OP_BR_PAIR
  176.   /* put    */ OP_RX_SZ, OP_RX_Z,
  177.   /* pop    */ OP_SX_YZ,
  178.   /* resume */ OP_XYZ,
  179.   /* save   */ OP_SX,
  180.   /* unsave */ OP_SZ,
  181.   /* sync   */ OP_XYZ,
  182.   /* swym   */ OP_XYZ,
  183.   /* get    */ OP_SX_RZ,
  184.   /* trip   */ OP_XYZ
  185. };
  186.  
  187. const char *special_regs[NUM_SPECIAL_REGS] = {
  188.   "rB","rD","rE","rH","rJ","rM","rR","rBB",
  189.   "rC","rN","rO","rS","rI","rT","rTT","rK",
  190.   "rQ","rU","rV","rG","rL","rA","rF","rP",
  191.   "rW","rX","rY","rZ","rWW","rXX","rYY","rZZ"
  192. };
  193.  
  194. const char *rounding_modes[NUM_ROUNDING_MODES] = {
  195.   ",",
  196.   "ROUND_OFF,",
  197.   "ROUND_UP,",
  198.   "ROUND_DOWN,",
  199.   "ROUND_NEAR,"
  200. };
  201.  
  202. extern void printOp(const char *buffer,uint64_t address) {
  203.   unsigned char opcode = buffer[0],
  204.                 x      = buffer[1],
  205.                 y      = buffer[2],
  206.                 z      = buffer[3],
  207.                 argf   = argformats[opcode];
  208.   int64_t offset;
  209.  
  210.   printf("#%016" PRIx64 " %02hx%02hx%02hx%02hx    %-6s ",
  211.     address,opcode,x,y,z,opcodes[opcode]);
  212.  
  213.   if (IS_SIMPLE_ARGF(argf))
  214.     printf(opfstrings[argf],x,y,z);
  215.  
  216.   else switch (argf) {
  217.     case OP_SX_ROUND_SZ:
  218.     case OP_SX_ROUND_Z:
  219.       if (y >= NUM_ROUNDING_MODES) {
  220.         argf = argf == OP_SX_ROUND_SZ ? OP_SX_Y_SZ : OP_SX_Y_Z;
  221.         printf(opfstrings[argf],x,y,z);
  222.       } else
  223.         printf(opfstrings[argf],x,rounding_modes[y],z);
  224.       break;
  225.  
  226.     case OP_SX:
  227.     case OP_SZ: printf(opfstrings[argf],(argf == OP_SX ? x : z)); break;
  228.  
  229.     case OP_SX_RZ:
  230.       if (z >= NUM_SPECIAL_REGS) printf(opfstrings[OP_SX_Z],x,z);
  231.       else printf(opfstrings[argf],x,special_regs[z]); break;
  232.  
  233.     case OP_RX_SZ:
  234.     case OP_RX_Z:
  235.       if (x >= NUM_SPECIAL_REGS)
  236.         printf(opfstrings[argf == OP_RX_Z ? OP_X_Z : OP_X_SZ],x,z);
  237.       else
  238.         printf(opfstrings[argf],special_regs[x],z);
  239.       break;
  240.  
  241.     case OP_SX_FYZ:
  242.     case OP_SX_BYZ:
  243.       offset = (y << 8) + z;
  244.       offset = argf == OP_SX_FYZ ? offset : (1 << 16) - offset;
  245.       printf(opfstrings[argf],x,
  246.              (uint64_t)offset < 0 ? -offset : offset,
  247.              address + 4*offset);
  248.       break;
  249.  
  250.     case OP_FXYZ:
  251.     case OP_BXYZ:
  252.       offset = (x << 16) + (y << 8) + z;
  253.       offset = argf == OP_FXYZ ? offset : (1 << 24) - offset;
  254.       printf(opfstrings[argf],
  255.         (uint64_t)offset < 0 ? -offset : offset,
  256.         address + 4*offset);
  257.       break;
  258.   }
  259. }
  260.  
  261. extern void printCode(const char* buffer, size_t count,uint64_t address) {
  262.   while(count > 4) {
  263.     printOp(buffer,address);
  264.     buffer += 4;
  265.     address += 4;
  266.     count -= 4;
  267.   }
  268. }