JADECODE.C 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* jadecode.c: Copyright (C) Codemist Ltd., 1996. */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "machine.h"
  5. #include "tags.h"
  6. #include "cslerror.h"
  7. #include "externs.h"
  8. #include "read.h"
  9. #include "stream.h"
  10. #include "arith.h"
  11. #include "entries.h"
  12. #include "javahost.h"
  13. #include "javaglb.h"
  14. static struct { char *opname; } optable[211] = {
  15. "\x00""nop", /* 0 */
  16. "\x00""aconst_null", /* 1 */
  17. "\x00""iconst_m1", /* 2 */
  18. "\x00""iconst_0", /* 3 */
  19. "\x00""iconst_1", /* 4 */
  20. "\x00""iconst_2", /* 5 */
  21. "\x00""iconst_3", /* 6 */
  22. "\x00""iconst_4", /* 7 */
  23. "\x00""iconst_5", /* 8 */
  24. "\x00""lconst_0", /* 9 */
  25. "\x00""lconst_1", /* 10 */
  26. "\x00""fconst_0", /* 11 */
  27. "\x00""fconst_1", /* 12 */
  28. "\x00""fconst_2", /* 13 */
  29. "\x00""dconst_0", /* 14 */
  30. "\x00""dconst_1", /* 15 */
  31. "\x01""bipush", /* 16 */
  32. "\x02""sipush", /* 17 */
  33. "\x11""ldc1", /* 18 */
  34. "\x12""ldc2", /* 19 */
  35. "\x12""ldc2w", /* 20 */
  36. "\x01""iload", /* 21 */
  37. "\x01""lload", /* 22 */
  38. "\x01""fload", /* 23 */
  39. "\x01""dload", /* 24 */
  40. "\x01""aload", /* 25 */
  41. "\x00""iload_0", /* 26 */
  42. "\x00""iload_1", /* 27 */
  43. "\x00""iload_2", /* 28 */
  44. "\x00""iload_3", /* 29 */
  45. "\x00""lload_0", /* 30 */
  46. "\x00""lload_1", /* 31 */
  47. "\x00""lload_2", /* 32 */
  48. "\x00""lload_3", /* 33 */
  49. "\x00""fload_0", /* 34 */
  50. "\x00""fload_1", /* 35 */
  51. "\x00""fload_2", /* 36 */
  52. "\x00""fload_3", /* 37 */
  53. "\x00""dload_0", /* 38 */
  54. "\x00""dload_1", /* 39 */
  55. "\x00""dload_2", /* 40 */
  56. "\x00""dload_3", /* 41 */
  57. "\x00""aload_0", /* 42 */
  58. "\x00""aload_1", /* 43 */
  59. "\x00""aload_2", /* 44 */
  60. "\x00""aload_3", /* 45 */
  61. "\x00""iaload", /* 46 */
  62. "\x00""laload", /* 47 */
  63. "\x00""faload", /* 48 */
  64. "\x00""daload", /* 49 */
  65. "\x00""aaload", /* 50 */
  66. "\x00""baload", /* 51 */
  67. "\x00""caload", /* 52 */
  68. "\x00""saload", /* 53 */
  69. "\x01""istore", /* 54 */
  70. "\x01""lstore", /* 55 */
  71. "\x01""fstore", /* 56 */
  72. "\x01""dstore", /* 57 */
  73. "\x01""astore", /* 58 */
  74. "\x00""istore_0", /* 59 */
  75. "\x00""istore_1", /* 60 */
  76. "\x00""istore_2", /* 61 */
  77. "\x00""istore_3", /* 62 */
  78. "\x00""lstore_0", /* 63 */
  79. "\x00""lstore_1", /* 64 */
  80. "\x00""lstore_2", /* 65 */
  81. "\x00""lstore_3", /* 66 */
  82. "\x00""fstore_0", /* 67 */
  83. "\x00""fstore_1", /* 68 */
  84. "\x00""fstore_2", /* 69 */
  85. "\x00""fstore_3", /* 70 */
  86. "\x00""dstore_0", /* 71 */
  87. "\x00""dstore_1", /* 72 */
  88. "\x00""dstore_2", /* 73 */
  89. "\x00""dstore_3", /* 74 */
  90. "\x00""astore_0", /* 75 */
  91. "\x00""astore_1", /* 76 */
  92. "\x00""astore_2", /* 77 */
  93. "\x00""astore_3", /* 78 */
  94. "\x00""iastore", /* 79 */
  95. "\x00""lastore", /* 80 */
  96. "\x00""fastore", /* 81 */
  97. "\x00""dastore", /* 82 */
  98. "\x00""aastore", /* 83 */
  99. "\x00""bastore", /* 84 */
  100. "\x00""castore", /* 85 */
  101. "\x00""sastore", /* 86 */
  102. "\x00""pop", /* 87 */
  103. "\x00""pop2", /* 88 */
  104. "\x00""dup", /* 89 */
  105. "\x00""dup_x1", /* 90 */
  106. "\x00""dup_x2", /* 91 */
  107. "\x00""dup2", /* 92 */
  108. "\x00""dup2_x1", /* 93 */
  109. "\x00""dup2_x2", /* 94 */
  110. "\x00""swap", /* 95 */
  111. "\x00""iadd", /* 96 */
  112. "\x00""ladd", /* 97 */
  113. "\x00""fadd", /* 98 */
  114. "\x00""dadd", /* 99 */
  115. "\x00""isub", /* 100 */
  116. "\x00""lsub", /* 101 */
  117. "\x00""fsub", /* 102 */
  118. "\x00""dsub", /* 103 */
  119. "\x00""imul", /* 104 */
  120. "\x00""lmul", /* 105 */
  121. "\x00""fmul", /* 106 */
  122. "\x00""dmul", /* 107 */
  123. "\x00""idiv", /* 108 */
  124. "\x00""ldiv", /* 109 */
  125. "\x00""fdiv", /* 110 */
  126. "\x00""ddiv", /* 111 */
  127. "\x00""irem", /* 112 */
  128. "\x00""lrem", /* 113 */
  129. "\x00""frem", /* 114 */
  130. "\x00""drem", /* 115 */
  131. "\x00""ineg", /* 116 */
  132. "\x00""lneg", /* 117 */
  133. "\x00""fneg", /* 118 */
  134. "\x00""dneg", /* 119 */
  135. "\x00""ishl", /* 120 */
  136. "\x00""ishr", /* 121 */
  137. "\x00""iushr", /* 122 */
  138. "\x00""lshl", /* 123 */
  139. "\x00""lshr", /* 124 */
  140. "\x00""lushr", /* 125 */
  141. "\x00""iand", /* 126 */
  142. "\x00""land", /* 127 */
  143. "\x00""ior", /* 128 */
  144. "\x00""lor", /* 129 */
  145. "\x00""ixor", /* 130 */
  146. "\x00""lxor", /* 131 */
  147. "\x03""iinc", /* 132 */
  148. "\x00""i2l", /* 133 */
  149. "\x00""i2f", /* 134 */
  150. "\x00""i2d", /* 135 */
  151. "\x00""l2i", /* 136 */
  152. "\x00""l2f", /* 137 */
  153. "\x00""l2d", /* 138 */
  154. "\x00""f2i", /* 139 */
  155. "\x00""f2l", /* 140 */
  156. "\x00""f2d", /* 141 */
  157. "\x00""d2i", /* 142 */
  158. "\x00""d2l", /* 143 */
  159. "\x00""d2f", /* 144 */
  160. "\x00""int2byte", /* 145 */
  161. "\x00""int2char", /* 146 */
  162. "\x00""int2short", /* 147 */
  163. "\x00""lcmp", /* 148 */
  164. "\x00""fcmpl", /* 149 */
  165. "\x00""fcmpg", /* 150 */
  166. "\x00""dcmpl", /* 151 */
  167. "\x00""dcmpg", /* 152 */
  168. "\x08""ifeq", /* 153 */
  169. "\x08""ifne", /* 154 */
  170. "\x08""iflt", /* 155 */
  171. "\x08""ifge", /* 156 */
  172. "\x08""ifgt", /* 157 */
  173. "\x08""ifle", /* 158 */
  174. "\x08""if_icmpeq", /* 159 */
  175. "\x08""if_icmpne", /* 160 */
  176. "\x08""if_icmplt", /* 161 */
  177. "\x08""if_icmpge", /* 162 */
  178. "\x08""if_icmpgt", /* 163 */
  179. "\x08""if_icmple", /* 164 */
  180. "\x08""if_acmpeq", /* 165 */
  181. "\x08""if_acmpne", /* 166 */
  182. "\x08""goto", /* 167 */
  183. "\x08""jsr", /* 168 */
  184. "\x01""ret", /* 169 */
  185. "\x06""tableswitch", /* 170 */
  186. "\x07""lookupswitch", /* 171 */
  187. "\x00""ireturn", /* 172 */
  188. "\x00""lreturn", /* 173 */
  189. "\x00""freturn", /* 174 */
  190. "\x00""dreturn", /* 175 */
  191. "\x00""areturn", /* 176 */
  192. "\x00""return", /* 177 */
  193. "\x12""getstatic", /* 178 */
  194. "\x12""putstatic", /* 179 */
  195. "\x12""getfield", /* 180 */
  196. "\x12""putfield", /* 181 */
  197. "\x12""invokevirtual", /* 182 */
  198. "\x12""invokenonvirtual", /* 183 */
  199. "\x12""invokestatic", /* 184 */
  200. "\x14""invokeinterface", /* 185 */
  201. "\x00""<missing186>", /* 186 */
  202. "\x12""new", /* 187 */
  203. "\x01""newarray", /* 188 */
  204. "\x12""anewarray", /* 189 */
  205. "\x00""arraylength", /* 190 */
  206. "\x00""athrow", /* 191 */
  207. "\x12""checkcast", /* 192 */
  208. "\x12""instanceof", /* 193 */
  209. "\x00""monitorenter", /* 194 */
  210. "\x00""monitorexit", /* 195 */
  211. "\x01""wide", /* 196 */
  212. "\x13""multinewarray", /* 197 */
  213. "\x08""ifnull", /* 198 */
  214. "\x08""ifnonnull", /* 199 */
  215. "\x09""goto_w", /* 200 */
  216. "\x09""jsr_w", /* 201 */
  217. "\x00""breakpoint", /* 202 */
  218. "\x00""<missing203>", /* 203 */
  219. "\x00""<missing204>", /* 204 */
  220. "\x00""<missing205>", /* 205 */
  221. "\x00""<missing206>", /* 206 */
  222. "\x00""<missing207>", /* 207 */
  223. "\x00""<missing208>", /* 208 */
  224. "\x02""ret_w", /* 209 */
  225. "\x00""<missing >= 210>" };
  226. static unsigned char *labtab;
  227. static void notelab(unsigned32 l)
  228. { if (l < 0x10000) labtab[l/8] |= 1 << (l%8);
  229. /* err_printf("notelab %d\n", l); */
  230. }
  231. static int islab(unsigned32 l)
  232. { int b = 1 << l%8;
  233. /* err_printf("islab %d\n", l); */
  234. if (l < 0x10000 && labtab[l/8] & b)
  235. { /* labtab[l/8] ^= b; */
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. static int reflab(unsigned32 l)
  241. { int b = 1 << l%8;
  242. if (l < 0x10000 && labtab[l/8] & b)
  243. { err_printf("L%.4x", l);
  244. return 1;
  245. }
  246. return 0;
  247. }
  248. #define ztos16(w) (((unsigned32)(w) ^ 0x8000) - 0x8000)
  249. static void decode_lit(unsigned32 n, Cp_Info *cp, unsigned32 cplen)
  250. { for (;;)
  251. { if (n == 0 || n >= cplen)
  252. { err_printf("<bad literal 0x%x>", n);
  253. return;
  254. }
  255. else switch (cp[n].tag)
  256. {
  257. case CONSTANT_Class: err_printf("Class"); goto redo;
  258. case CONSTANT_FieldRef: err_printf("FieldRef"); goto redo2;
  259. case CONSTANT_MethodRef: err_printf("MethodRef"); goto redo2;
  260. case CONSTANT_InterfaceMethodRef: err_printf("InterfaceMethodRef"); goto redo2;
  261. case CONSTANT_String: err_printf("String"); goto redo;
  262. case CONSTANT_Integer: err_printf("Integer:%d",n); return;
  263. case CONSTANT_Float: err_printf("Float:%d",n); return;
  264. case CONSTANT_Long: err_printf("Long:%d",n); return;
  265. case CONSTANT_Double: err_printf("Double:%d",n); return;
  266. case CONSTANT_Utf8: err_printf("Utf8[%d'%*s']", n,
  267. cp[n].len, cp[n].u.utf8); return;
  268. case CONSTANT_Unicode: err_printf("Unicode:%d",n); return;
  269. case CONSTANT_NameAndType: err_printf("NameAndType"); goto redo2;
  270. default: err_printf("<unknown literal[%.4x] %d>", n, cp[n].tag); return;
  271. }
  272. redo: err_printf("[%d]:", n); n = cp[n].u.val; continue;
  273. redo2: err_printf("[%d]:<", n);
  274. decode_lit(cp[n].u.val, cp, cplen);
  275. err_printf(",");
  276. decode_lit(cp[n].len, cp, cplen);
  277. err_printf(">"); return;
  278. }
  279. }
  280. char *jdecodeopname(unsigned32 op)
  281. {
  282. if (op >= 210) op = 210;
  283. return optable[op].opname+1;
  284. }
  285. void javadecode(unsigned8 *code, unsigned32 len, Cp_Info *cp, unsigned32 cplen)
  286. { unsigned8 *p;
  287. if (labtab == 0) labtab = (unsigned char *)jmalloc(0x10000/8);
  288. memset(labtab, 0, 0x10000/8);
  289. for (p = code; p < code+len;)
  290. { unsigned32 op = *p++;
  291. if (op >= 210) op = 210;
  292. switch (optable[op].opname[0])
  293. {
  294. default: break;
  295. case 0x11:
  296. case 0x01: p++; break;
  297. case 0x12:
  298. case 0x02: p += 2; break;
  299. case 0x08: notelab(p-1-code + ztos16(p[0]<<8 | p[1])); p += 2; break;
  300. case 0x03: p += 2; break;
  301. case 0x13: p += 3; break;
  302. case 0x14:
  303. case 0x04: p += 4; break;
  304. case 0x09: notelab(p-1-code + (p[0]<<24 | p[1]<<16 | p[2]<<8 | p[3])); p += 4;
  305. break;
  306. case 0x06: ("\ttableswitch!"); break;
  307. case 0x07: ("\tlookupswitch!"); break;
  308. }
  309. }
  310. for (p = code; p < code+len;)
  311. { unsigned32 op = *p++;
  312. unsigned32 off;
  313. if (op >= 210) op = 210;
  314. if (islab(p-1 - code)) err_printf("L%.4x:", p-1 - code);
  315. err_printf("\t%s", optable[op].opname+1);
  316. if (optable[op].opname[0] != 0x00 && strlen(optable[op].opname+1) < 8)
  317. err_printf("\t");
  318. switch (optable[op].opname[0])
  319. {
  320. default: break;
  321. case 0x01: err_printf("\t%d", *p++); break;
  322. case 0x11: err_printf("\t"); decode_lit(*p++, cp, cplen); break;
  323. case 0x02: err_printf("\t0x%.4x", p[0]<<8 | p[1]); p += 2; break;
  324. case 0x12: err_printf("\t"); decode_lit(p[0]<<8 | p[1], cp, cplen); p += 2; break;
  325. case 0x08: off = ztos16(p[0]<<8 | p[1]);
  326. err_printf("\t");
  327. if (!reflab(p-1-code+off)) err_printf("$+0x%.4x", off);
  328. p += 2; break;
  329. case 0x03: err_printf("\t%d,%d", p[0], p[1]); p += 2; break;
  330. case 0x13: err_printf("\t"); decode_lit(p[0]<<8 | p[1], cp, cplen);
  331. err_printf(",%d", p[2]); p += 3; break;
  332. case 0x14: err_printf("\t"); decode_lit(p[0]<<8 | p[1], cp, cplen);
  333. err_printf(",%d,%d", p[2], p[3]); p += 4; break;
  334. case 0x04: err_printf("\t0x%.8x", p[0]<<24 | p[1]<<16 | p[2]<<8 | p[3]); p += 4;
  335. break;
  336. case 0x09: off = p[0]<<24 | p[1]<<16 | p[2]<<8 | p[3];
  337. err_printf("\t(big)");
  338. if (!reflab(p-1-code+off)) err_printf("\t$+0x%.8x", off);
  339. p += 4; break;
  340. case 0x06: err_printf("\tswitch!");
  341. break;
  342. case 0x07: err_printf("\tswitch!");
  343. break;
  344. }
  345. err_printf("\n");
  346. }
  347. }
  348. /* end of jadecode.c */
  349.