PageRenderTime 81ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/cappuccino_vm.js

https://github.com/hogeki/CappuccinoVM
JavaScript | 3353 lines | 2851 code | 341 blank | 161 comment | 254 complexity | 863a1e3f8076451b855c42e374b253f3 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. new function()
  2. {
  3. var debugPrint = function(str)
  4. {
  5. //document.write(str + "<br>");
  6. console.log(str);
  7. };
  8. var get16BitsSigned = function(value)
  9. {
  10. if(value & 0x8000)
  11. {
  12. value = (0x10000 - value) * -1;
  13. }
  14. return value;
  15. }
  16. var get8BitsSigned = function(value)
  17. {
  18. if(value & 0x80)
  19. {
  20. value = (0x100 - value) * -1;
  21. }
  22. return value;
  23. };
  24. var getParamLength = function(descriptor)
  25. {
  26. var paramLength = 0;
  27. var i = 0;
  28. if(descriptor.charAt(i) != "(")
  29. {
  30. throw Error("Method descriptor broken");
  31. }
  32. i++;
  33. loop: while(true)
  34. {
  35. switch(descriptor.charAt(i))
  36. {
  37. case ")":
  38. break loop;
  39. case "B":
  40. case "C":
  41. case "F":
  42. case "I":
  43. case "S":
  44. case "Z":
  45. paramLength++;
  46. i++;
  47. break;
  48. case "D":
  49. case "J":
  50. paramLength+=2;
  51. //paramLength++;
  52. i+=1;
  53. break;
  54. case "L":
  55. paramLength++;
  56. i++;
  57. while(descriptor.charAt(i) != ";")
  58. {
  59. i++;
  60. }
  61. i++;
  62. break;
  63. case "[":
  64. paramLength++;
  65. i++;
  66. while(true)
  67. {
  68. c = descriptor.charAt(i);
  69. if(c == "B" || c == "C" || c == "F" || c == "I" || c == "S" || c == "Z" || c == "D" || c == "J")
  70. {
  71. i++;
  72. break;
  73. }
  74. else if(c == "L")
  75. {
  76. i++;
  77. while(descriptor.charAt(i) != ";")
  78. {
  79. i++;
  80. }
  81. i++;
  82. break;
  83. }
  84. else
  85. {
  86. //[が続いているはず
  87. i++;
  88. }
  89. }
  90. break;
  91. }
  92. }
  93. return paramLength
  94. }
  95. const CONSTANT_CLASS = 7;
  96. const CONSTANT_FIELDREF = 9;
  97. const CONSTANT_METHODREF = 10;
  98. const CONSTANT_INTERFACEMETHODREF = 11;
  99. const CONSTANT_STRING = 8;
  100. const CONSTANT_INTEGER = 3;
  101. const CONSTANT_FLOAT = 4;
  102. const CONSTANT_LONG = 5;
  103. const CONSTANT_DOUBLE = 6;
  104. const CONSTANT_NAMEANDTYPE = 12;
  105. const CONSTANT_UTF8 = 1;
  106. const TYPE_INTEGER = 0x00;
  107. const TYPE_FLOAT = 0x01;
  108. const TYPE_DOUBLE = 0x02;
  109. const TYPE_LONG = 0x03;
  110. const TYPE_OBJECT = 0x04;
  111. const TYPE_DUMMY = 0x05;
  112. const TYPE_ARRAY = 0x10;
  113. var ConstEmpty = function() {}
  114. ConstEmpty.prototype.resolve = function(cpool) {
  115. this.value = "empty";
  116. }
  117. ConstEmpty.prototype.debugPrint = function()
  118. {
  119. debugPrint("Empty slot");
  120. }
  121. var ConstClass = function(tag, nameIndex)
  122. {
  123. this.tag = tag;
  124. this.nameIndex = nameIndex;
  125. }
  126. ConstClass.prototype.resolve = function(cpool)
  127. {
  128. this.value = cpool[this.nameIndex].value;
  129. }
  130. ConstClass.prototype.debugPrint = function()
  131. {
  132. debugPrint("Class " + this.value);
  133. }
  134. var ConstReferrence = function(tag, classIndex, nameAndTypeIndex)
  135. {
  136. this.tag = tag;
  137. this.classIndex = classIndex;
  138. this.nameAndTypeIndex = nameAndTypeIndex;
  139. }
  140. ConstReferrence.prototype.resolve = function(cpool)
  141. {
  142. var className = cpool[cpool[this.classIndex].nameIndex].value;
  143. var nameAndType = cpool[this.nameAndTypeIndex];
  144. var name = cpool[nameAndType.nameIndex].value;
  145. var descriptor = cpool[nameAndType.descriptorIndex].value;
  146. this.value = {className:className, name:name, descriptor:descriptor};
  147. }
  148. ConstReferrence.prototype.debugPrint = function()
  149. {
  150. debugPrint("Referrence " + this.value.className + " " + this.value.name + " " + this.value.descriptor);
  151. }
  152. var ConstFieldref = function()
  153. {
  154. ConstReferrence.apply(this, arguments);
  155. }
  156. ConstFieldref.prototype = new ConstReferrence();
  157. ConstFieldref.prototype.resolve = function()
  158. {
  159. ConstReferrence.prototype.resolve.apply(this, arguments);
  160. var desc = this.value.descriptor.charAt(0);
  161. switch(desc)
  162. {
  163. case "B":
  164. case "C":
  165. case "I":
  166. case "S":
  167. case "Z":
  168. //this.valueClass = ValueInteger;
  169. this.fieldType = TYPE_INTEGER;
  170. break;
  171. case "F":
  172. //this.valueClass = ValueFloat;
  173. this.fieldType = TYPE_FLOAT;
  174. break;
  175. case "D":
  176. //this.valueClass = ValueDouble;
  177. this.fieldType = TYPE_DOUBLE;
  178. break;
  179. case "J":
  180. //this.valueClass = ValueLong;
  181. this.fieldType = TYPE_LONG;
  182. break;
  183. case "L":
  184. case "[":
  185. //this.valueClass = ValueObject;
  186. this.fieldType = TYPE_OBJECT;
  187. break;
  188. default:
  189. //this.valueClass = ValueInteger;
  190. this.fieldType = TYPE_INTEGER;
  191. }
  192. }
  193. var ConstMethodref = function()
  194. {
  195. ConstReferrence.apply(this, arguments);
  196. }
  197. ConstMethodref.prototype = new ConstReferrence();
  198. ConstMethodref.prototype.resolve = function()
  199. {
  200. ConstReferrence.prototype.resolve.apply(this, arguments);
  201. this.paramLength = getParamLength(this.value.descriptor);
  202. }
  203. var ConstInterfaceMethodref = function()
  204. {
  205. ConstReferrence.apply(this, arguments);
  206. }
  207. ConstInterfaceMethodref.prototype = new ConstReferrence();
  208. var ConstString = function(tag, stringIndex)
  209. {
  210. this.tag = tag;
  211. this.stringIndex = stringIndex;
  212. }
  213. ConstString.prototype.resolve = function(cpool)
  214. {
  215. //this.value = cpool[this.stringIndex].value;
  216. this.value = createJavaString(cpool[this.stringIndex].value);
  217. }
  218. ConstString.prototype.debugPrint = function()
  219. {
  220. debugPrint("String " + this.value);
  221. }
  222. var ConstInteger = function(tag, bytes)
  223. {
  224. //debugPrint("ConstInteger:" + bytes);
  225. this.tag = tag;
  226. //this.value = get32BitsSigned(bytes);
  227. this.value = bytes;
  228. //this.value = new ValueInteger(bytes);
  229. }
  230. ConstInteger.prototype.resolve = function(cpool) {}
  231. ConstInteger.prototype.debugPrint = function()
  232. {
  233. debubPrint("Integer " + this.value);
  234. }
  235. var ConstLong = function(tag, high, low)
  236. {
  237. var tmp;
  238. this.tag = tag;
  239. //32bitずつhighとlowに分けて格納
  240. //符号はhighのbit32に持たせる(2の補数表現ではない)
  241. //オペランドスタック上ではhighが上でlowが下
  242. if(high & 0x80000000)
  243. {
  244. //負の数なので絶対値を取り出す
  245. //ビットを反転して1足す
  246. high = ~high >>> 0;
  247. low = ~low >>> 0;
  248. low = low + 1;
  249. if( low > 0xffffffff)
  250. {
  251. low = 0;
  252. high = high + 1;
  253. }
  254. high += Math.pow(2, 32);
  255. }
  256. this.high = high;
  257. this.low = low >>> 0;
  258. }
  259. ConstLong.prototype.resolve = function(cpool) {}
  260. ConstLong.prototype.debugPrint = function()
  261. {
  262. debugPrint("Long " + this.low);
  263. }
  264. var ConstFloat = function(tag, bytes)
  265. {
  266. this.tag = tag;
  267. var s = ((bytes >>> 31) == 1) ? -1 : 1;
  268. var e = (bytes >>> 23) & 0xff;
  269. var m = (e == 0) ? (bytes & 0x7fffff) << 1 : (bytes & 0x7fffff) | 0x800000;
  270. this.value = s * m * Math.pow(2, e-150);
  271. //this.value = new ValueFloat(s * m * Math.pow(2, e-150));
  272. }
  273. ConstFloat.prototype.resolve = function(cpool) {}
  274. ConstFloat.prototype.debugPrint = function()
  275. {
  276. debugPrint("Float " + this.value);
  277. }
  278. var ConstDouble = function(tag, highBytes, lowBytes)
  279. {
  280. this.tag = tag;
  281. var s = ((highBytes >>> 31) == 1) ? -1 : 1;
  282. var e = (highBytes >>> 20) & 0x7ff;
  283. var m;
  284. if(e == 0)
  285. {
  286. m = (highBytes & 0xfffff) * 8589934592.0 + lowBytes * 2.0;
  287. }
  288. else
  289. {
  290. m = ((highBytes & 0xfffff) | 0x100000) * 4294967296.0 + lowBytes;
  291. }
  292. //debugPrint("s=" + s + " e=" + e + " m=" + m);
  293. this.value = s * m * Math.pow(2, e-1075);
  294. //debugPrint("double value=" + this.value);
  295. //this.value = new ValueDouble(s * m * Math.pow(2, e-1075));
  296. }
  297. ConstDouble.prototype.resolve = function(cpool) {}
  298. ConstDouble.prototype.debugPrint = function()
  299. {
  300. debugPrint("Double " + this.value);
  301. }
  302. var ConstNameAndType = function(tag, nameIndex, descriptorIndex)
  303. {
  304. this.tag = tag;
  305. this.nameIndex = nameIndex;
  306. this.descriptorIndex = descriptorIndex;
  307. }
  308. ConstNameAndType.prototype.resolve = function(cpool)
  309. {
  310. var name = cpool[this.nameIndex].value;
  311. var descriptor = cpool[this.descriptorIndex].value;
  312. this.value = {name:name, descriptor:descriptor};
  313. }
  314. ConstNameAndType.prototype.debugPrint = function()
  315. {
  316. debugPrint("NameAndType " + this.value.name + " " + this.value.descriptor);
  317. }
  318. var ConstUtf8 = function(tag, length, str)
  319. {
  320. this.tag = tag;
  321. this.length = length;
  322. this.value = str;
  323. }
  324. ConstUtf8.prototype.resolve = function(cpool) {}
  325. ConstUtf8.prototype.debugPrint = function()
  326. {
  327. debugPrint("Utf8 " + this.value);
  328. }
  329. var Method = function(accessFlags, name, descriptor, $cappuccino)
  330. {
  331. this.accessFlags = accessFlags;
  332. this.name = name;
  333. this.descriptor = descriptor;
  334. this.paramLength = getParamLength(descriptor);
  335. this.$cappuccino = $cappuccino
  336. }
  337. Method.prototype.isStatic = function()
  338. {
  339. return(this.accessFlags & 0x0008);
  340. }
  341. Method.prototype.debugPrint = function()
  342. {
  343. debugPrint("Method " + this.name + " " + this.descriptor + " paramLength=" + this.paramLength);
  344. }
  345. Method.prototype.getCompiledMethod = function()
  346. {
  347. if(!this.compiledMethod)
  348. {
  349. this.compile();
  350. }
  351. return this.compiledMethod;
  352. }
  353. const NOP = 0x00;
  354. const ACONST_NULL = 0x01;
  355. const ICONST_M1 = 0x02;
  356. const ICONST_0 = 0x03;
  357. const ICONST_1 = 0x04;
  358. const ICONST_2 = 0x05;
  359. const ICONST_3 = 0x06;
  360. const ICONST_4 = 0x07;
  361. const ICONST_5 = 0x08;
  362. const LCONST_0 = 0x09;
  363. const LCONST_1 = 0x0a;
  364. const DCONST_0 = 0xe;
  365. const DCONST_1 = 0xf;
  366. const BIPUSH = 0x10;
  367. const SIPUSH = 0x11;
  368. const LDC = 0x12;
  369. const LDC2_W = 0x14;
  370. const ILOAD = 0x15;
  371. const LLOAD = 0x16;
  372. const FLOAD = 0x17;
  373. const DLOAD = 0x18;
  374. const ALOAD = 0x19;
  375. const ILOAD_0 = 0x1a;
  376. const ILOAD_1 = 0x1b;
  377. const ILOAD_2 = 0x1c;
  378. const ILOAD_3 = 0x1d;
  379. const LLOAD_0 = 0x1e;
  380. const LLOAD_1 = 0x1f;
  381. const LLOAD_2 = 0x20;
  382. const LLOAD_3 = 0x21;
  383. const FLOAD_0 = 0x22;
  384. const FLOAD_1 = 0x23;
  385. const FLOAD_2 = 0x24;
  386. const FLOAD_3 = 0x25;
  387. const DLOAD_0 = 0x26;
  388. const DLOAD_1 = 0x27;
  389. const DLOAD_2 = 0x28;
  390. const DLOAD_3 = 0x29;
  391. const ALOAD_0 = 0x2a;
  392. const ALOAD_1 = 0x2b;
  393. const ALOAD_2 = 0x2c;
  394. const ALOAD_3 = 0x2d;
  395. const IALOAD = 0x2e;
  396. const LALOAD = 0x2f;
  397. const FALOAD = 0x30;
  398. const DALOAD = 0x31;
  399. const AALOAD = 0x32;
  400. const ISTORE = 0x36;
  401. const LSTORE = 0x37;
  402. const FSTORE = 0x38;
  403. const DSTORE = 0x39;
  404. const ASTORE = 0x3a;
  405. const ISTORE_0 = 0x3b;
  406. const ISTORE_1 = 0x3c;
  407. const ISTORE_2 = 0x3d;
  408. const ISTORE_3 = 0x3e;
  409. const LSTORE_0 = 0x3f;
  410. const LSTORE_1 = 0x40;
  411. const LSTORE_2 = 0x41;
  412. const LSTORE_3 = 0x42;
  413. const FSTORE_0 = 0x43;
  414. const FSTORE_1 = 0x44;
  415. const FSTORE_2 = 0x45;
  416. const FSTORE_3 = 0x46;
  417. const DSTORE_0 = 0x47;
  418. const DSTORE_1 = 0x48;
  419. const DSTORE_2 = 0x49;
  420. const DSTORE_3 = 0x4a;
  421. const ASTORE_0 = 0x4b;
  422. const ASTORE_1 = 0x4c;
  423. const ASTORE_2 = 0x4d;
  424. const ASTORE_3 = 0x4e;
  425. const IASTORE = 0x4f;
  426. const LASTORE = 0x50;
  427. const FASTORE = 0x51;
  428. const DASTORE = 0x52;
  429. const AASTORE = 0x53;
  430. const POP = 0x57;
  431. const POP2 = 0x58;
  432. const DUP = 0x59;
  433. const DUP2 = 0x5c;
  434. const SWAP = 0x5f;
  435. const IADD = 0x60;
  436. const LADD = 0x61;
  437. const FADD = 0x62;
  438. const DADD = 0x63;
  439. const ISUB = 0x64;
  440. const LSUB = 0x65;
  441. const FSUB = 0x66;
  442. const DSUB = 0x67;
  443. const IMUL = 0x68;
  444. const LMUL = 0x69;
  445. const FMUL = 0x6a;
  446. const DMUL = 0x6b;
  447. const IDIV = 0x6c;
  448. const LDIV = 0x6d;
  449. const FDIV = 0x6e;
  450. const DDIV = 0x6f;
  451. const IREM = 0x70;
  452. const LREM = 0x71;
  453. const INEG = 0x74;
  454. const LNEG = 0x75;
  455. const FNEG = 0x76;
  456. const DNEG = 0x77;
  457. const ISHL = 0x78;
  458. const LSHL = 0x79;
  459. const ISHR = 0x7a;
  460. const LSHR = 0x7b;
  461. const IUSHR = 0x7c;
  462. const LUSHR = 0x7d;
  463. const IAND = 0x7e;
  464. const LAND = 0x7f;
  465. const IOR = 0x80;
  466. const LOR = 0x81;
  467. const IXOR = 0x82;
  468. const LXOR = 0x83;
  469. const IINC = 0x84;
  470. const I2L = 0x85;
  471. const I2F = 0x86;
  472. const I2D = 0x87;
  473. const L2I = 0x88;
  474. const L2F = 0x89;
  475. const L2D = 0x8a;
  476. const F2I = 0x8b;
  477. const F2L = 0x8c;
  478. const F2D = 0x8d;
  479. const D2I = 0x8e;
  480. const D2L = 0x8f;
  481. const D2F = 0x90;
  482. const I2B = 0x91;
  483. const I2C = 0x92;
  484. const I2S = 0x93;
  485. const LCMP = 0x94;
  486. const DCMPL = 0x97;
  487. const DCMPG = 0x98;
  488. const IFEQ = 0x99;
  489. const IFNE = 0x9a;
  490. const IFLT = 0x9b;
  491. const IFGE = 0x9c;
  492. const IFGT = 0x9d;
  493. const IFLE = 0x9e;
  494. const IF_ICMPEQ = 0x9f;
  495. const IF_ICMPNE = 0xa0;
  496. const IF_ICMPLT = 0xa1;
  497. const IF_ICMPGE = 0xa2;
  498. const IF_ICMPGT = 0xa3;
  499. const IF_ICMPLE = 0xa4;
  500. const IF_ACMPEQ = 0xa5;
  501. const IF_ACMPNE = 0xa6;
  502. const GOTO = 0xa7;
  503. const TABLESWITCH = 0xaa;
  504. const LOOKUPSWITCH = 0xab;
  505. const IRETURN = 0xac;
  506. const LRETURN = 0xad;
  507. const FRETURN = 0xae;
  508. const DRETURN = 0xaf;
  509. const ARETURN = 0xb0;
  510. const RETURN = 0xb1;
  511. const GETSTATIC = 0xb2;
  512. const PUTSTATIC = 0xb3;
  513. const GETFIELD = 0xb4;
  514. const PUTFIELD = 0xb5;
  515. const INVOKEVIRTUAL = 0xb6;
  516. const INVOKESPECIAL = 0xb7;
  517. const INVOKESTATIC = 0xb8;
  518. const INVOKEINTERFACE = 0xb9;
  519. const NEW = 0xbb;
  520. const NEWARRAY = 0xbc;
  521. const ANEWARRAY = 0xbd;
  522. const ARRAYLENGTH = 0xbe;
  523. const ATHROW = 0xbf;
  524. const MULTIANEWARRAY = 0xc5;
  525. const IFNULL = 0xc6;
  526. const IFNONNULL = 0xc7;
  527. Method.instTable = [];
  528. var notImplement = function(code, i, jsCodes, $cappuccino)
  529. {
  530. throw Error("Not implemented pc=" + i + " opcode=" + code[i]);
  531. }
  532. var i;
  533. for(i=0; i < 256; i++)
  534. Method.instTable[i] = notImplement;
  535. Method.instTable[NOP] = function(code, i, jsCodes)
  536. {
  537. return i + 1;
  538. }
  539. Method.instTable[ISTORE] = Method.instTable[ASTORE] = Method.instTable[FSTORE] = function(code, i, jsCodes)
  540. {
  541. jsCodes.push("vmStack[stackBase+" + code[i+1] + "] = vmStack[--thread.stackPtr];");
  542. return i + 2;
  543. }
  544. Method.instTable[DSTORE] = Method.instTable[LSTORE] = function(code, i, jsCodes)
  545. {
  546. jsCodes.push("thread.stackPtr-=2;");
  547. jsCodes.push("vmStack[stackBase+" + code[i+1] + "] = vmStack[thread.stackPtr];");
  548. jsCodes.push("vmStack[stackBase+" + (code[i+1] + 1) + "] = vmStack[thread.stackPtr+1];");
  549. return i + 2;
  550. }
  551. Method.instTable[ISTORE_0] = Method.instTable[ASTORE_0] = Method.instTable[FSTORE_0] = function(code, i, jsCodes)
  552. {
  553. jsCodes.push("vmStack[stackBase] = vmStack[--thread.stackPtr];");
  554. return i + 1;
  555. }
  556. Method.instTable[DSTORE_0] = Method.instTable[LSTORE_0] = function(code, i, jsCodes)
  557. {
  558. jsCodes.push("thread.stackPtr-=2");
  559. jsCodes.push("vmStack[stackBase] = vmStack[thread.stackPtr];");
  560. jsCodes.push("vmStack[stackBase+1] = vmStack[thread.stackPtr+1];");
  561. return i + 1;
  562. }
  563. Method.instTable[ISTORE_1] = Method.instTable[ASTORE_1] = Method.instTable[FSTORE_1] = function(code, i, jsCodes)
  564. {
  565. jsCodes.push("vmStack[stackBase+1] = vmStack[--thread.stackPtr];");
  566. return i + 1;
  567. }
  568. Method.instTable[DSTORE_1] = Method.instTable[LSTORE_1] = function(code, i, jsCodes)
  569. {
  570. jsCodes.push("thread.stackPtr-=2;");
  571. jsCodes.push("vmStack[stackBase+1] = vmStack[thread.stackPtr];");
  572. jsCodes.push("vmStack[stackBase+2] = vmStack[thread.stackPtr+1];");
  573. return i + 1;
  574. }
  575. Method.instTable[ISTORE_2] = Method.instTable[ASTORE_2] = Method.instTable[FSTORE_2] = function(code, i, jsCodes)
  576. {
  577. jsCodes.push("vmStack[stackBase+2] = vmStack[--thread.stackPtr];");
  578. return i + 1;
  579. }
  580. Method.instTable[DSTORE_2] = Method.instTable[LSTORE_2] = function(code, i, jsCodes)
  581. {
  582. jsCodes.push("thread.stackPtr-=2;");
  583. jsCodes.push("vmStack[stackBase+2] = vmStack[thread.stackPtr];");
  584. jsCodes.push("vmStack[stackBase+3] = vmStack[thread.stackPtr+1];");
  585. return i + 1;
  586. }
  587. Method.instTable[ISTORE_3] = Method.instTable[ASTORE_3] = Method.instTable[FSTORE_3] = function(code, i, jsCodes)
  588. {
  589. jsCodes.push("vmStack[stackBase+3] = vmStack[--thread.stackPtr];");
  590. return i + 1;
  591. }
  592. Method.instTable[DSTORE_3] = Method.instTable[LSTORE_3] = function(code, i, jsCodes)
  593. {
  594. jsCodes.push("thread.stackPtr-=2;");
  595. jsCodes.push("vmStack[stackBase+3] = vmStack[thread.stackPtr];");
  596. jsCodes.push("vmStack[stackBase+4] = vmStack[thread.stackPtr+1];");
  597. return i + 1;
  598. }
  599. Method.instTable[ILOAD] = Method.instTable[ALOAD] = Method.instTable[FLOAD] = function(code, i, jsCodes)
  600. {
  601. jsCodes.push("vmStack[thread.stackPtr++] = vmStack[stackBase+" + code[i+1] + "];");
  602. return i + 2;
  603. }
  604. Method.instTable[DLOAD] = Method.instTable[LLOAD] = function(code, i, jsCodes)
  605. {
  606. //jsCodes.push("vmStack.push(vmStack[stackTop+" + code[i+1] +"].duplicate());");
  607. //jsCodes.push("vmStack.push(CappuccinoVM.valueDummy);");
  608. jsCodes.push("vmStack[thread.stackPtr] = vmStack[stackBase+" + code[i+1] + "];");
  609. jsCodes.push("vmStack[thread.stackPtr+1] = vmStack[stackBase+" + (code[i+1]+1)+ "];");
  610. jsCodes.push("thread.stackPtr+=2;");
  611. return i + 2;
  612. }
  613. Method.instTable[ILOAD_0] = Method.instTable[ALOAD_0] = Method.instTable[FLOAD_0] = function(code, i, jsCodes)
  614. {
  615. jsCodes.push("vmStack[thread.stackPtr++] = vmStack[stackBase];");
  616. return i + 1;
  617. }
  618. Method.instTable[DLOAD_0] = Method.instTable[LLOAD_0] = function(code, i, jsCodes)
  619. {
  620. jsCodes.push("vmStack[thread.stackPtr] = vmStack[stackBase];");
  621. jsCodes.push("vmStack[thread.stackPtr+1] = vmStack[stackBase+1];");
  622. jsCodes.push("thread.stackPtr+=2;");
  623. return i + 1;
  624. }
  625. Method.instTable[ILOAD_1] = Method.instTable[ALOAD_1] = Method.instTable[FLOAD_1] = function(code, i, jsCodes)
  626. {
  627. jsCodes.push("vmStack[thread.stackPtr++] = vmStack[stackBase+1];");
  628. return i + 1;
  629. }
  630. Method.instTable[DLOAD_1] = Method.instTable[LLOAD_1] = function(code, i, jsCodes)
  631. {
  632. jsCodes.push("vmStack[thread.stackPtr] = vmStack[stackBase+1];");
  633. jsCodes.push("vmStack[thread.stackPtr+1] = vmStack[stackBase+2];");
  634. jsCodes.push("thread.stackPtr+=2;");
  635. return i + 1;
  636. }
  637. Method.instTable[ILOAD_2] = Method.instTable[ALOAD_2] = Method.instTable[FLOAD_2] = function(code, i, jsCodes)
  638. {
  639. jsCodes.push("vmStack[thread.stackPtr++] = vmStack[stackBase+2];");
  640. return i + 1;
  641. }
  642. Method.instTable[DLOAD_2] = Method.instTable[LLOAD_2] = function(code, i, jsCodes)
  643. {
  644. jsCodes.push("vmStack[thread.stackPtr] = vmStack[stackBase+2];");
  645. jsCodes.push("vmStack[thread.stackPtr+1] = vmStack[stackBase+3];");
  646. jsCodes.push("thread.stackPtr+=2;");
  647. return i + 1;
  648. }
  649. Method.instTable[ILOAD_3] = Method.instTable[ALOAD_3] = Method.instTable[FLOAD_3] = function(code, i, jsCodes)
  650. {
  651. jsCodes.push("vmStack[thread.stackPtr++] = vmStack[stackBase+3];");
  652. return i + 1;
  653. }
  654. Method.instTable[DLOAD_3] = Method.instTable[LLOAD_3] = function(code, i, jsCodes)
  655. {
  656. jsCodes.push("vmStack[thread.stackPtr] = vmStack[stackBase+3];");
  657. jsCodes.push("vmStack[thread.stackPtr+1] = vmStack[stackBase+4];");
  658. jsCodes.push("thread.stackPtr+=2;");
  659. return i + 1;
  660. }
  661. Method.instTable[IASTORE] = Method.instTable[AASTORE] = Method.instTable[FASTORE] = function(code, i, jsCodes)
  662. {
  663. jsCodes.push("operand2 = vmStack[--thread.stackPtr];");
  664. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  665. jsCodes.push("obj = vmStack[--thread.stackPtr];");
  666. jsCodes.push("obj[operand1] = operand2;");
  667. return i + 1;
  668. }
  669. Method.instTable[DASTORE] = function(code, i, jsCodes)
  670. {
  671. jsCodes.push("--thread.stackPtr;");
  672. jsCodes.push("operand2 = vmStack[--thread.stackPtr];");
  673. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  674. jsCodes.push("obj = vmStack[--thread.stackPtr];");
  675. jsCodes.push("obj[operand1] = operand2;");
  676. return i + 1;
  677. }
  678. Method.instTable[LASTORE] = function(code, i, jsCodes)
  679. {
  680. jsCodes.push("operand2 = {high:vmStack[thread.stackPtr-1], low:vmStack[thread.stackPtr-2]};");
  681. jsCodes.push("operand1 = vmStack[thread.stackPtr-3];");
  682. jsCodes.push("obj = vmStack[thread.stackPtr-4];");
  683. jsCodes.push("obj[operand1] = operand2;");
  684. jsCodes.push("thread.stackPtr -= 4;");
  685. return i + 1;
  686. }
  687. Method.instTable[IALOAD] = Method.instTable[AALOAD] = Method.instTable[FALOAD] = function(code, i, jsCodes)
  688. {
  689. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  690. jsCodes.push("obj = vmStack[thread.stackPtr-1];");
  691. jsCodes.push("vmStack[thread.stackPtr-1] = obj[operand1];");
  692. return i + 1;
  693. }
  694. Method.instTable[DALOAD] = function(code, i, jsCodes)
  695. {
  696. jsCodes.push("operand1 = vmStack[thread.stackPtr-1];");
  697. jsCodes.push("obj = vmStack[thread.stackPtr-2];");
  698. jsCodes.push("vmStack[thread.stackPtr-2] = obj[operand1];");
  699. jsCodes.push("vmStack[thread.stackPtr-1] = 0;");
  700. return i + 1;
  701. }
  702. Method.instTable[LALOAD] = function(code, i, jsCodes)
  703. {
  704. jsCodes.push("operand1 = vmStack[thread.stackPtr-1];");
  705. jsCodes.push("obj = vmStack[thread.stackPtr-2];");
  706. jsCodes.push("vmStack[thread.stackPtr-2] = obj[operand1].low;");
  707. jsCodes.push("vmStack[thread.stackPtr-1] = obj[operand1].high;");
  708. return i + 1;
  709. }
  710. Method.instTable[POP] = function(code, i, jsCodes)
  711. {
  712. jsCodes.push("thread.stackPtr--;");
  713. return i + 1;
  714. }
  715. Method.instTable[POP2] = function(code, i, jsCodes)
  716. {
  717. jsCodes.push("thread.stackPtr-=2;");
  718. return i + 1;
  719. }
  720. Method.instTable[DUP] = function(code, i, jsCodes)
  721. {
  722. jsCodes.push("vmStack[thread.stackPtr] = vmStack[thread.stackPtr-1];");
  723. jsCodes.push("thread.stackPtr++;");
  724. return i + 1;
  725. }
  726. Method.instTable[DUP2] = function(code, i, jsCodes)
  727. {
  728. jsCodes.push("vmStack[thread.stackPtr] = vmStack[thread.stackPtr-2];");
  729. jsCodes.push("vmStack[thread.stackPtr+1] = vmStack[thread.stackPtr-1];");
  730. jsCodes.push("thread.stackPtr+=2;");
  731. return i + 1;
  732. }
  733. Method.instTable[SWAP] = function(code, i, jsCodes)
  734. {
  735. jsCodes.push("operand2 = vmStack[thread.stackPtr-1];");
  736. jsCodes.push("operand1 = vmStack[thread.stackPtr-2];");
  737. jsCodes.push("vmStack[thread.stackPtr-2] = operand2;");
  738. jsCodes.push("vmStack[thread.stackPtr-1] = operand1;");
  739. return i + 1;
  740. }
  741. Method.instTable[BIPUSH] = function(code, i, jsCodes)
  742. {
  743. jsCodes.push("vmStack[thread.stackPtr++] = " + get8BitsSigned(code[i+1]) + ";");
  744. return i + 2;
  745. }
  746. Method.instTable[SIPUSH] = function(code, i, jsCodes)
  747. {
  748. jsCodes.push("vmStack[thread.stackPtr++] = " + get16BitsSigned((code[i+1] << 8) + code[i+2]) + ";");
  749. return i + 3;
  750. }
  751. Method.instTable[ACONST_NULL] = function(code, i, jsCodes)
  752. {
  753. jsCodes.push("vmStack[thread.stackPtr++] = null;");
  754. return i + 1;
  755. }
  756. Method.instTable[ICONST_M1] = function(code, i, jsCodes)
  757. {
  758. jsCodes.push("vmStack[thread.stackPtr++] = -1;");
  759. return i + 1;
  760. }
  761. Method.instTable[ICONST_0] = function(code, i, jsCodes)
  762. {
  763. jsCodes.push("vmStack[thread.stackPtr++] = 0;");
  764. return i + 1;
  765. }
  766. Method.instTable[ICONST_1] = function(code, i, jsCodes)
  767. {
  768. jsCodes.push("vmStack[thread.stackPtr++] = 1;");
  769. return i + 1;
  770. }
  771. Method.instTable[ICONST_2] = function(code, i, jsCodes)
  772. {
  773. jsCodes.push("vmStack[thread.stackPtr++] = 2;");
  774. return i + 1;
  775. }
  776. Method.instTable[ICONST_3] = function(code, i, jsCodes)
  777. {
  778. jsCodes.push("vmStack[thread.stackPtr++] = 3;");
  779. return i + 1;
  780. }
  781. Method.instTable[ICONST_4] = function(code, i, jsCodes)
  782. {
  783. jsCodes.push("vmStack[thread.stackPtr++] = 4;");
  784. return i + 1;
  785. }
  786. Method.instTable[LCONST_0] = function(code, i, jsCodes)
  787. {
  788. jsCodes.push("vmStack[thread.stackPtr++] = 0;");
  789. jsCodes.push("vmStack[thread.stackPtr++] = 0;");
  790. return i + 1;
  791. }
  792. Method.instTable[LCONST_1] = function(code, i, jsCodes)
  793. {
  794. jsCodes.push("vmStack[thread.stackPtr++] = 1;");
  795. jsCodes.push("vmStack[thread.stackPtr++] = 0;");
  796. return i + 1;
  797. }
  798. Method.instTable[DCONST_0] = function(code, i, jsCodes)
  799. {
  800. jsCodes.push("vmStack[thread.stackPtr++] = 0;");
  801. jsCodes.push("vmStack[thread.stackPtr++] = 0;");
  802. return i + 1;
  803. }
  804. Method.instTable[DCONST_1] = function(code, i, jsCodes)
  805. {
  806. jsCodes.push("vmStack[thread.stackPtr++] = 1;");
  807. jsCodes.push("vmStack[thread.stackPtr++] = 0;");
  808. return i + 1;
  809. }
  810. Method.instTable[ICONST_5] = function(code, i, jsCodes)
  811. {
  812. jsCodes.push("vmStack[thread.stackPtr++] = 5;");
  813. return i + 1;
  814. }
  815. Method.instTable[IFEQ] = function(code, i, jsCodes)
  816. {
  817. var addr;
  818. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  819. jsCodes.push("if(operand1==0){");
  820. addr = i + get16BitsSigned((code[i+1] << 8) + code[i+2]);
  821. jsCodes.push("pc = " + addr + "; continue;}");
  822. return i + 3;
  823. }
  824. Method.instTable[IFNE] = function(code, i, jsCodes)
  825. {
  826. var addr;
  827. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  828. jsCodes.push("if(operand1!=0){");
  829. addr = i + get16BitsSigned((code[i+1] << 8) + code[i+2]);
  830. jsCodes.push("pc = " + addr + "; continue;}");
  831. return i + 3;
  832. }
  833. Method.instTable[IFLT] = function(code, i, jsCodes)
  834. {
  835. var addr;
  836. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  837. jsCodes.push("if(operand1<0){");
  838. addr = i + get16BitsSigned((code[i+1] << 8) + code[i+2]);
  839. jsCodes.push("pc = " + addr + "; continue;}");
  840. return i + 3;
  841. }
  842. Method.instTable[IFLE] = function(code, i, jsCodes)
  843. {
  844. var addr;
  845. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  846. jsCodes.push("if(operand1<=0){");
  847. addr = i + get16BitsSigned((code[i+1] << 8) + code[i+2]);
  848. jsCodes.push("pc = " + addr + "; continue;}");
  849. return i + 3;
  850. }
  851. Method.instTable[IFGT] = function(code, i, jsCodes)
  852. {
  853. var addr;
  854. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  855. jsCodes.push("if(operand1>0){");
  856. addr = i + get16BitsSigned((code[i+1] << 8) + code[i+2]);
  857. jsCodes.push("pc = " + addr + "; continue;}");
  858. return i + 3;
  859. }
  860. Method.instTable[IFGE] = function(code, i, jsCodes)
  861. {
  862. var addr;
  863. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  864. jsCodes.push("if(operand1>=0){");
  865. addr = i + get16BitsSigned((code[i+1] << 8) + code[i+2]);
  866. jsCodes.push("pc = " + addr + "; continue;}");
  867. return i + 3;
  868. }
  869. Method.instTable[IF_ICMPEQ] = Method.instTable[IF_ACMPEQ] = function(code, i, jsCodes)
  870. {
  871. var addr;
  872. jsCodes.push("operand2 = vmStack[--thread.stackPtr];");
  873. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  874. jsCodes.push("if(operand1==operand2){");
  875. addr = i + get16BitsSigned((code[i+1] << 8) + code[i+2]);
  876. jsCodes.push("pc = " + addr + "; continue;}");
  877. return i + 3;
  878. }
  879. Method.instTable[IF_ICMPNE] = Method.instTable[IF_ACMPNE] = function(code, i, jsCodes)
  880. {
  881. var addr;
  882. jsCodes.push("operand2 = vmStack[--thread.stackPtr];");
  883. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  884. jsCodes.push("if(operand1!=operand2){");
  885. addr = i + get16BitsSigned((code[i+1] << 8) + code[i+2]);
  886. jsCodes.push("pc = " + addr + "; continue;}");
  887. return i + 3;
  888. }
  889. Method.instTable[IF_ICMPLT] = function(code, i, jsCodes)
  890. {
  891. var addr;
  892. jsCodes.push("operand2 = vmStack[--thread.stackPtr];");
  893. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  894. jsCodes.push("if(operand1<operand2){");
  895. addr = i + get16BitsSigned((code[i+1] << 8) + code[i+2]);
  896. jsCodes.push("pc = " + addr + "; continue;}");
  897. return i + 3;
  898. }
  899. Method.instTable[IF_ICMPLE] = function(code, i, jsCodes)
  900. {
  901. var addr;
  902. jsCodes.push("operand2 = vmStack[--thread.stackPtr];");
  903. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  904. jsCodes.push("if(operand1<=operand2){");
  905. addr = i + get16BitsSigned((code[i+1] << 8) + code[i+2]);
  906. jsCodes.push("pc = " + addr + "; continue;}");
  907. return i + 3;
  908. }
  909. Method.instTable[IF_ICMPGT] = function(code, i, jsCodes)
  910. {
  911. var addr;
  912. jsCodes.push("operand2 = vmStack[--thread.stackPtr];");
  913. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  914. jsCodes.push("if(operand1>operand2){");
  915. addr = i + get16BitsSigned((code[i+1] << 8) + code[i+2]);
  916. jsCodes.push("pc = " + addr + "; continue;}");
  917. return i + 3;
  918. }
  919. Method.instTable[IF_ICMPGE] = function(code, i, jsCodes)
  920. {
  921. var addr;
  922. jsCodes.push("operand2 = vmStack[--thread.stackPtr];");
  923. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  924. jsCodes.push("if(operand1>=operand2){");
  925. addr = i + get16BitsSigned((code[i+1] << 8) + code[i+2]);
  926. jsCodes.push("pc = " + addr + "; continue;}");
  927. return i + 3;
  928. }
  929. Method.instTable[IFNULL] = function(code, i, jsCodes)
  930. {
  931. var addr;
  932. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  933. jsCodes.push("if(operand1 == null){");
  934. addr = i + get16BitsSigned((code[i+1] << 8) + code[i+2]);
  935. jsCodes.push("pc = " + addr + "; continue;}");
  936. return i + 3;
  937. }
  938. Method.instTable[IFNONNULL] = function(code, i, jsCodes)
  939. {
  940. var addr;
  941. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  942. jsCodes.push("if(operand1 != null){");
  943. addr = i + get16BitsSigned((code[i+1] << 8) + code[i+2]);
  944. jsCodes.push("pc = " + addr + "; continue;}");
  945. return i + 3;
  946. }
  947. Method.instTable[GOTO] = function(code, i, jsCodes)
  948. {
  949. var addr;
  950. addr = i + get16BitsSigned((code[i+1] << 8) + code[i+2]);
  951. jsCodes.push("pc = " + addr + "; continue;");
  952. return i + 3;
  953. }
  954. Method.instTable[TABLESWITCH] = function(code, i, jsCodes)
  955. {
  956. var _i, j, addr, defaddr, low, high;
  957. _i = i + (0x04 - (i & 0x03));
  958. defaddr = i + ((code[_i] << 24) + (code[_i+1] << 16) + (code[_i+2] << 8) + code[_i+3]);
  959. _i += 4;
  960. low = ((code[_i] << 24) + (code[_i+1] << 16) + (code[_i+2] << 8) + code[_i+3]);
  961. _i += 4;
  962. high = ((code[_i] << 24) + (code[_i+1] << 16) + (code[_i+2] << 8) + code[_i+3]);
  963. _i += 4;
  964. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  965. for(j = low; j <= high; j++)
  966. {
  967. addr = i + ((code[_i] << 24) + (code[_i+1] << 16) + (code[_i+2] << 8) + code[_i+3]);
  968. jsCodes.push("if(operand1 == " + j + "){pc =" + addr + "; continue;}");
  969. _i += 4;
  970. }
  971. jsCodes.push("pc=" + defaddr + "; continue;");
  972. return _i;
  973. }
  974. Method.instTable[LOOKUPSWITCH] = function(code, i, jsCodes)
  975. {
  976. var _i, j, addr, defaddr, npairs, match;
  977. _i = i + (0x04 - (i & 0x03));
  978. defaddr = i + ((code[_i] << 24) + (code[_i+1] << 16) + (code[_i+2] << 8) + code[_i+3]);
  979. _i += 4;
  980. npairs = (code[_i] << 24) + (code[_i+1] << 16) + (code[_i+2] << 8) + code[_i+3];
  981. _i += 4;
  982. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  983. for(j = 0; j < npairs; j++)
  984. {
  985. match = ((code[_i] << 24) + (code[_i+1] << 16) + (code[_i+2] << 8) + code[_i+3]);
  986. _i += 4;
  987. addr = i + ((code[_i] << 24) + (code[_i+1] << 16) + (code[_i+2] << 8) + code[_i+3]);
  988. jsCodes.push("if(operand1==" + match + "){pc=" + addr + "; continue;}");
  989. _i += 4;
  990. }
  991. jsCodes.push("pc=" + defaddr + "; continue;");
  992. return _i;
  993. }
  994. Method.instTable[IINC] = function(code, i, jsCodes)
  995. {
  996. jsCodes.push("vmStack[stackBase+" + code[i+1] + "]+=" + get8BitsSigned(code[i+2]) + ";");
  997. return i + 3;
  998. }
  999. Method.instTable[IADD] = Method.instTable[FADD] = function(code, i, jsCodes)
  1000. {
  1001. jsCodes.push("vmStack[thread.stackPtr-2] += vmStack[thread.stackPtr-1];");
  1002. jsCodes.push("thread.stackPtr--;");
  1003. return i + 1;
  1004. }
  1005. Method.instTable[DADD] = function(code, i, jsCodes)
  1006. {
  1007. jsCodes.push("vmStack[thread.stackPtr-4] += vmStack[thread.stackPtr-2];");
  1008. jsCodes.push("thread.stackPtr-=2;");
  1009. return i + 1;
  1010. }
  1011. Method.instTable[LADD] = function(code, i, jsCodes)
  1012. {
  1013. jsCodes.push("obj=CappuccinoVM.doAddLong(vmStack[thread.stackPtr-3], vmStack[thread.stackPtr-4], vmStack[thread.stackPtr-1], vmStack[thread.stackPtr-2]);");
  1014. jsCodes.push("vmStack[thread.stackPtr-4]=obj.low;");
  1015. jsCodes.push("vmStack[thread.stackPtr-3]=obj.high;");
  1016. jsCodes.push("thread.stackPtr-=2;");
  1017. return i + 1;
  1018. }
  1019. Method.instTable[ISUB] = Method.instTable[FSUB] = function(code, i, jsCodes)
  1020. {
  1021. jsCodes.push("vmStack[thread.stackPtr-2] -= vmStack[thread.stackPtr-1];");
  1022. jsCodes.push("thread.stackPtr--;");
  1023. return i + 1;
  1024. }
  1025. Method.instTable[DSUB] = function(code, i, jsCodes)
  1026. {
  1027. jsCodes.push("vmStack[thread.stackPtr-4] -= vmStack[thread.stackPtr-2];");
  1028. jsCodes.push("thread.stackPtr-=2;");
  1029. return i + 1;
  1030. }
  1031. Method.instTable[LSUB] = function(code, i, jsCodes)
  1032. {
  1033. jsCodes.push("obj=CappuccinoVM.doSubLong(vmStack[thread.stackPtr-3], vmStack[thread.stackPtr-4], vmStack[thread.stackPtr-1], vmStack[thread.stackPtr-2]);");
  1034. jsCodes.push("vmStack[thread.stackPtr-4]=obj.low;");
  1035. jsCodes.push("vmStack[thread.stackPtr-3]=obj.high;");
  1036. jsCodes.push("thread.stackPtr-=2;");
  1037. return i + 1;
  1038. }
  1039. Method.instTable[IMUL] = Method.instTable[FMUL] = function(code, i, jsCodes)
  1040. {
  1041. jsCodes.push("vmStack[thread.stackPtr-2] *= vmStack[thread.stackPtr-1];");
  1042. jsCodes.push("thread.stackPtr--;");
  1043. return i + 1;
  1044. }
  1045. Method.instTable[DMUL] = function(code, i, jsCodes)
  1046. {
  1047. jsCodes.push("vmStack[thread.stackPtr-4] *= vmStack[thread.stackPtr-2];");
  1048. jsCodes.push("thread.stackPtr-=2;");
  1049. return i + 1;
  1050. }
  1051. Method.instTable[LMUL] = function(code, i, jsCodes)
  1052. {
  1053. jsCodes.push("obj=CappuccinoVM.doMulLong(vmStack[thread.stackPtr-3], vmStack[thread.stackPtr-4], vmStack[thread.stackPtr-1], vmStack[thread.stackPtr-2]);");
  1054. jsCodes.push("vmStack[thread.stackPtr-4]=obj.low;");
  1055. jsCodes.push("vmStack[thread.stackPtr-3]=obj.high;");
  1056. jsCodes.push("thread.stackPtr-=2;");
  1057. return i + 1;
  1058. }
  1059. Method.instTable[IDIV] = Method.instTable[FDIV] = function(code, i, jsCodes)
  1060. {
  1061. jsCodes.push("vmStack[thread.stackPtr-2] = Math.floor(vmStack[thread.stackPtr-2]/vmStack[thread.stackPtr-1]);");
  1062. jsCodes.push("thread.stackPtr--;");
  1063. return i + 1;
  1064. }
  1065. Method.instTable[DDIV] = function(code, i, jsCodes)
  1066. {
  1067. jsCodes.push("vmStack[thread.stackPtr-4] /= vmStack[thread.stackPtr-2];");
  1068. jsCodes.push("thread.stackPtr-=2;");
  1069. return i + 1;
  1070. }
  1071. Method.instTable[LDIV] = function(code, i, jsCodes)
  1072. {
  1073. jsCodes.push("obj=CappuccinoVM.doDivLong(vmStack[thread.stackPtr-3], vmStack[thread.stackPtr-4], vmStack[thread.stackPtr-1], vmStack[thread.stackPtr-2]);");
  1074. jsCodes.push("vmStack[thread.stackPtr-4]=obj.lowQuot;");
  1075. jsCodes.push("vmStack[thread.stackPtr-3]=obj.highQuot;");
  1076. jsCodes.push("thread.stackPtr-=2;");
  1077. return i + 1;
  1078. }
  1079. Method.instTable[IREM] = function(code, i, jsCodes)
  1080. {
  1081. jsCodes.push("vmStack[thread.stackPtr-2] %= vmStack[thread.stackPtr-1];");
  1082. jsCodes.push("thread.stackPtr--;");
  1083. return i + 1;
  1084. }
  1085. Method.instTable[LREM] = function(code, i, jsCodes)
  1086. {
  1087. jsCodes.push("obj=CappuccinoVM.doDivLong(vmStack[thread.stackPtr-3], vmStack[thread.stackPtr-4], vmStack[thread.stackPtr-1], vmStack[thread.stackPtr-2]);");
  1088. jsCodes.push("vmStack[thread.stackPtr-4]=obj.lowMod;");
  1089. jsCodes.push("vmStack[thread.stackPtr-3]=obj.highMod;");
  1090. jsCodes.push("thread.stackPtr-=2;");
  1091. return i + 1;
  1092. }
  1093. Method.instTable[INEG] = Method.instTable[FNEG] = function(code, i, jsCodes)
  1094. {
  1095. jsCodes.push("vmStack[thread.stackPtr-1] *= -1;");
  1096. return i + 1;
  1097. }
  1098. Method.instTable[DNEG] = function(code, i, jsCodes)
  1099. {
  1100. jsCodes.push("vmStack[thread.stackPtr-2] *= -1;");
  1101. return i + 1;
  1102. }
  1103. Method.instTable[LNEG] = function(code, i, jsCodes)
  1104. {
  1105. jsCodes.push("obj=CappuccinoVM.doMulLong(vmStack[thread.stackPtr-1], vmStack[thread.stackPtr-2], Math.pow(2, 32), 1);");
  1106. jsCodes.push("vmStack[thread.stackPtr-2]=obj.low;");
  1107. jsCodes.push("vmStack[thread.stackPtr-1]=obj.high;");
  1108. return i + 1;
  1109. }
  1110. Method.instTable[LCMP] = function(code, i, jsCodes)
  1111. {
  1112. jsCodes.push("operand1=CappuccinoVM.doCmpLong(vmStack[thread.stackPtr-3], vmStack[thread.stackPtr-4], vmStack[thread.stackPtr-1], vmStack[thread.stackPtr-2]);");
  1113. jsCodes.push("vmStack[thread.stackPtr-4] = operand1")
  1114. jsCodes.push("thread.stackPtr-=3;");
  1115. return i + 1;
  1116. }
  1117. Method.instTable[DCMPL] = Method.instTable[DCMPG] = function(code, i, jsCodes)
  1118. {
  1119. jsCodes.push("thread.stackPtr--;");
  1120. jsCodes.push("operand2 = vmStack[--thread.stackPtr];");
  1121. jsCodes.push("thread.stackPtr--;");
  1122. jsCodes.push("operand1 = vmStack[--thread.stackPtr];");
  1123. jsCodes.push("if(operand1 > operand2){vmStack[thread.stackPtr++] = 1;}")
  1124. jsCodes.push("else if(operand1 == operand2){vmStack[thread.stackPtr++] = 0;}");
  1125. jsCodes.push("else {vmStack[thread.stackPtr++] = -1;}");
  1126. return i + 1;
  1127. }
  1128. Method.instTable[ISHL] = function(code, i, jsCodes)
  1129. {
  1130. jsCodes.push("vmStack[thread.stackPtr-2] <<= vmStack[thread.stackPtr-1];");
  1131. jsCodes.push("thread.stackPtr--;");
  1132. return i + 1;
  1133. }
  1134. Method.instTable[LSHL] = function(code, i, jsCodes)
  1135. {
  1136. jsCodes.push("operand1 = CappuccinoVM.getOpponentFromSign({high:vmStack[thread.stackPtr-2], low:vmStack[thread.stackPtr-3]});");
  1137. jsCodes.push("operand2 = vmStack[thread.stackPtr-1];");
  1138. jsCodes.push("if(operand2 < 32) {");
  1139. jsCodes.push("mask = (~0 >>> 0) << (32-operand2);");
  1140. jsCodes.push("operand1.high = ((operand1.high << operand2) | ((operand1.low & mask) >>> (32-operand2))) >>> 0;");
  1141. jsCodes.push("operand1.low = (operand1.low << operand2) >>> 0;");
  1142. jsCodes.push("}else{");
  1143. jsCodes.push("operand1.high = (operand1.low << (operand2 - 32)) >>> 0;");
  1144. jsCodes.push("operand1.low = 0;");
  1145. jsCodes.push("}");
  1146. jsCodes.push("operand1 = CappuccinoVM.getSignFromOpponent(operand1);");
  1147. jsCodes.push("vmStack[thread.stackPtr-2] = operand1.high;");
  1148. jsCodes.push("vmStack[thread.stackPtr-3] = operand1.low;");
  1149. jsCodes.push("thread.stackPtr--;");
  1150. return i + 1;
  1151. }
  1152. Method.instTable[ISHR] = function(code, i, jsCodes)
  1153. {
  1154. jsCodes.push("vmStack[thread.stackPtr-2] >>= vmStack[thread.stackPtr-1];");
  1155. jsCodes.push("thread.stackPtr--;");
  1156. return i + 1;
  1157. }
  1158. Method.instTable[LSHR] = function(code, i, jsCodes)
  1159. {
  1160. jsCodes.push("operand1 = CappuccinoVM.getOpponentFromSign({high:vmStack[thread.stackPtr-2], low:vmStack[thread.stackPtr-3]});");
  1161. jsCodes.push("operand2 = vmStack[thread.stackPtr-1];");
  1162. jsCodes.push("if(operand2 < 32) {");
  1163. jsCodes.push("mask = (~0 >>> 0) >>> (32-operand2);");
  1164. jsCodes.push("operand1.low = ((operand1.low >>> operand2) | ((operand1.high & mask) << (32-operand2))) >>> 0;");
  1165. jsCodes.push("operand1.high = (operand1.high >> operand2) >>> 0;");
  1166. jsCodes.push("}else{");
  1167. jsCodes.push("operand1.low = (operand1.high >> (operand2 - 32)) >>> 0;");
  1168. jsCodes.push("if(operand1.high & 0x80000000){operand1.high = 0xffffffff;}");
  1169. jsCodes.push("else{operand1.high = 0;}");
  1170. jsCodes.push("}");
  1171. jsCodes.push("operand1 = CappuccinoVM.getSignFromOpponent(operand1);");
  1172. jsCodes.push("vmStack[thread.stackPtr-2] = operand1.high;");
  1173. jsCodes.push("vmStack[thread.stackPtr-3] = operand1.low;");
  1174. jsCodes.push("thread.stackPtr--;");
  1175. return i + 1;
  1176. }
  1177. Method.instTable[IUSHR] = function(code, i, jsCodes)
  1178. {
  1179. jsCodes.push("vmStack[thread.stackPtr-2] >>>= vmStack[thread.stackPtr-1];");
  1180. jsCodes.push("thread.stackPtr--;");
  1181. return i + 1;
  1182. }
  1183. Method.instTable[LUSHR] = function(code, i, jsCodes)
  1184. {
  1185. jsCodes.push("operand1 = CappuccinoVM.getOpponentFromSign({high:vmStack[thread.stackPtr-2], low:vmStack[thread.stackPtr-3]});");
  1186. jsCodes.push("operand2 = vmStack[thread.stackPtr-1];");
  1187. jsCodes.push("if(operand2 < 32) {");
  1188. jsCodes.push("mask = (~0 >>> 0) >>> (32-operand2);");
  1189. jsCodes.push("operand1.low = ((operand1.low >>> operand2) | ((operand1.high & mask) << (32-operand2))) >>> 0;");
  1190. jsCodes.push("operand1.high = (operand1.high >>> operand2) >>> 0;");
  1191. jsCodes.push("}else{");
  1192. jsCodes.push("operand1.low = (operand1.high >>> (operand2 - 32)) >>> 0;");
  1193. jsCodes.push("operand1.high = 0;");
  1194. jsCodes.push("}");
  1195. jsCodes.push("operand1 = CappuccinoVM.getSignFromOpponent(operand1);");
  1196. jsCodes.push("vmStack[thread.stackPtr-2] = operand1.high;");
  1197. jsCodes.push("vmStack[thread.stackPtr-3] = operand1.low;");
  1198. jsCodes.push("thread.stackPtr--;");
  1199. return i + 1;
  1200. }
  1201. Method.instTable[IAND] = function(code, i, jsCodes)
  1202. {
  1203. jsCodes.push("vmStack[thread.stackPtr-2] &= vmStack[thread.stackPtr-1];");
  1204. jsCodes.push("thread.stackPtr--;");
  1205. return i + 1;
  1206. }
  1207. Method.instTable[LAND] = function(code, i, jsCodes)
  1208. {
  1209. jsCodes.push("operand1 = CappuccinoVM.getOpponentFromSign({high:vmStack[thread.stackPtr-3], low:vmStack[thread.stackPtr-4]});");
  1210. jsCodes.push("operand2 = CappuccinoVM.getOpponentFromSign({high:vmStack[thread.stackPtr-1], low:vmStack[thread.stackPtr-2]});");
  1211. jsCodes.push("operand1.low &= operand2.low;");
  1212. jsCodes.push("operand1.high &= operand2.high;");
  1213. jsCodes.push("operand1 = CappuccinoVM.getSignFromOpponent(operand1);");
  1214. jsCodes.push("vmStack[thread.stackPtr-3] = operand1.high;");
  1215. jsCodes.push("vmStack[thread.stackPtr-4] = operand1.low;");
  1216. jsCodes.push("thread.stackPtr-=2;");
  1217. return i + 1;
  1218. }
  1219. Method.instTable[IOR] = function(code, i, jsCodes)
  1220. {
  1221. jsCodes.push("vmStack[thread.stackPtr-2] |= vmStack[thread.stackPtr-1];");
  1222. jsCodes.push("thread.stackPtr--;");
  1223. return i + 1;
  1224. }
  1225. Method.instTable[LOR] = function(code, i, jsCodes)
  1226. {
  1227. jsCodes.push("operand1 = CappuccinoVM.getOpponentFromSign({high:vmStack[thread.stackPtr-3], low:vmStack[thread.stackPtr-4]});");
  1228. jsCodes.push("operand2 = CappuccinoVM.getOpponentFromSign({high:vmStack[thread.stackPtr-1], low:vmStack[thread.stackPtr-2]});");
  1229. jsCodes.push("operand1.low |= operand2.low;");
  1230. jsCodes.push("operand1.high |= operand2.high;");
  1231. jsCodes.push("operand1 = CappuccinoVM.getSignFromOpponent(operand1);");
  1232. jsCodes.push("vmStack[thread.stackPtr-3] = operand1.high;");
  1233. jsCodes.push("vmStack[thread.stackPtr-4] = operand1.low;");
  1234. jsCodes.push("thread.stackPtr-=2;");
  1235. return i + 1;
  1236. }
  1237. Method.instTable[IXOR] = function(code, i, jsCodes)
  1238. {
  1239. jsCodes.push("vmStack[thread.stackPtr-2] ^= vmStack[thread.stackPtr-1];");
  1240. jsCodes.push("thread.stackPtr--;");
  1241. return i + 1;
  1242. }
  1243. Method.instTable[LXOR] = function(code, i, jsCodes)
  1244. {
  1245. jsCodes.push("operand1 = CappuccinoVM.getOpponentFromSign({high:vmStack[thread.stackPtr-3], low:vmStack[thread.stackPtr-4]});");
  1246. jsCodes.push("operand2 = CappuccinoVM.getOpponentFromSign({high:vmStack[thread.stackPtr-1], low:vmStack[thread.stackPtr-2]});");
  1247. jsCodes.push("operand1.low ^= operand2.low;");
  1248. jsCodes.push("operand1.high ^= operand2.high;");
  1249. jsCodes.push("operand1 = CappuccinoVM.getSignFromOpponent(operand1);");
  1250. jsCodes.push("vmStack[thread.stackPtr-3] = operand1.high;");
  1251. jsCodes.push("vmStack[thread.stackPtr-4] = operand1.low;");
  1252. jsCodes.push("thread.stackPtr-=2;");
  1253. return i + 1;
  1254. }
  1255. Method.instTable[I2C] = function(code, i, jsCodes)
  1256. {
  1257. //jsCodes.push("vmStack.push(vmStack.pop().toChar());");
  1258. jsCodes.push("vmStack[thread.stackPtr-1] &= 0xffff;");
  1259. return i + 1;
  1260. }
  1261. Method.instTable[I2B] = function(code, i, jsCodes)
  1262. {
  1263. //jsCodes.push("vmStack.push(vmStack.pop().toByte());");
  1264. jsCodes.push("vmStack[thread.stackPtr-1] &= 0xff;");
  1265. return i + 1;
  1266. }
  1267. Method.instTable[I2S] = function(code, i, jsCodes)
  1268. {
  1269. jsCodes.push("vmStack[thread.stackPtr-1] &= 0xffff;");
  1270. return i + 1;
  1271. }
  1272. Method.instTable[I2F] = function(code, i, jsCodes)
  1273. {
  1274. return i + 1;
  1275. }
  1276. Method.instTable[I2D] = function(code, i, jsCodes)
  1277. {
  1278. jsCodes.push("vmStack[thread.stackPtr++] = 0;");
  1279. return i + 1;
  1280. }
  1281. Method.instTable[I2L] = function(code, i, jsCodes)
  1282. {
  1283. jsCodes.push("obj = CappuccinoVM.getLongFromInt(vmStack[thread.stackPtr-1]);");
  1284. jsCodes.push("vmStack[thread.stackPtr-1] = obj.low;");
  1285. jsCodes.push("vmStack[thread.stackPtr++] = obj.high;");
  1286. return i + 1;
  1287. }
  1288. Method.instTable[F2I] = function(code, i, jsCodes)
  1289. {
  1290. jsCodes.push("vmStack[thread.stackPtr-1] = Math.floor(vmStack[thread.stackPtr-1]);");
  1291. return i + 1;
  1292. }
  1293. Method.instTable[F2D] = function(code, i, jsCodes)
  1294. {
  1295. jsCodes.push("vmStack[thread.stackPtr++] = 0;");
  1296. return i + 1;
  1297. }
  1298. Method.instTable[F2L] = function(code, i, jsCodes)
  1299. {
  1300. jsCodes.push("obj=CappuccinoVM.getLongFromDouble(vmStack[thread.stackPtr-1]);");
  1301. jsCodes.push("vmStack[thread.stackPtr-1] = obj.low;");
  1302. jsCodes.push("vmStack[thread.stackPtr++] = obj.high;");
  1303. return i + 1;
  1304. }
  1305. Method.instTable[L2I] = function(code, i, jsCodes)
  1306. {
  1307. jsCodes.push("operand1=CappuccinoVM.getDoubleFromLong(vmStack[thread.stackPtr-1], vmStack[thread.stackPtr-2]);");
  1308. jsCodes.push("vmStack[thread.stackPtr-2] = operand1;");
  1309. jsCodes.push("thread.stackPtr--;");
  1310. return i + 1;
  1311. }
  1312. Method.instTable[L2F] = function(code, i, jsCodes)
  1313. {
  1314. jsCodes.push("operand1=CappuccinoVM.getDoubleFromLong(vmStack[thread.stackPtr-1], vmStack[thread.stackPtr-2]);");
  1315. jsCodes.push("vmStack[thread.stackPtr-2] = operand1;");
  1316. jsCodes.push("thread.stackPtr--;");
  1317. return i + 1;
  1318. }
  1319. Method.instTable[L2D] = function(code, i, jsCodes)
  1320. {
  1321. jsCodes.push("operand1=CappuccinoVM.getDoubleFromLong(vmStack[thread.stackPtr-1], vmStack[thread.stackPtr-2]);");
  1322. jsCodes.push("vmStack[thread.stackPtr-2] = operand1;");
  1323. return i + 1;
  1324. }
  1325. Method.instTable[D2I] = function(code, i, jsCodes)
  1326. {
  1327. jsCodes.push("vmStack[thread.stackPtr-2] = Math.floor(vmStack[thread.stackPtr-2]);");
  1328. jsCodes.push("thread.stackPtr--;");
  1329. return i + 1;
  1330. }
  1331. Method.instTable[D2F] = function(code, i, jsCodes)
  1332. {
  1333. jsCodes.push("thread.stackPtr--;");
  1334. return i + 1;
  1335. }
  1336. Method.instTable[D2L] = function(code, i, jsCodes)
  1337. {
  1338. jsCodes.push("obj=CappuccinoVM.getLongFromDouble(vmStack[thread.stackPtr-2]);");
  1339. jsCodes.push("vmStack[thread.stackPtr-2] = obj.low;");
  1340. jsCodes.push("vmStack[thread.stackPtr-1] = obj.high;");
  1341. return i + 1;
  1342. }
  1343. ////Longの実装////
  1344. /*
  1345. memo:
  1346. highのbit32を符号bitとする
  1347. オペランドスタック上ではlowが下,highが上
  1348. */
  1349. var addAbsLong = function(highX, lowX, highY, lowY)
  1350. {
  1351. var highZ, lowZ;
  1352. var rad = Math.pow(2, 32)
  1353. lowZ = lowX + lowY;
  1354. highZ = highX + highY + Math.floor(lowZ / rad);
  1355. lowZ = lowZ % rad;
  1356. return {high:highZ, low:lowZ};
  1357. }
  1358. //X >= Yじゃないとダメ
  1359. var subAbsLong = function(highX, lowX, highY, lowY)
  1360. {
  1361. var highZ, lowZ;
  1362. var rad = Math.pow(2, 32);
  1363. lowX += rad;
  1364. lowZ = lowX - lowY;
  1365. highZ = highX - highY;
  1366. if(lowZ >= rad)
  1367. {
  1368. lowZ -= rad;
  1369. }
  1370. else
  1371. {
  1372. highZ--;
  1373. }
  1374. return {high:highZ, low:lowZ};
  1375. }
  1376. var cmpAbsLong = function(highX, lowX, highY, lowY)
  1377. {
  1378. if(highX > highY)
  1379. {
  1380. return 1;
  1381. }
  1382. else if(highX == highY)
  1383. {
  1384. if(lowX > lowY)
  1385. {
  1386. return 1;
  1387. }
  1388. else if(lowX == lowY)
  1389. {
  1390. return 0;
  1391. }
  1392. else
  1393. {
  1394. return -1;
  1395. }
  1396. }
  1397. else
  1398. {
  1399. return -1;
  1400. }
  1401. }
  1402. var doCmpLong = function(highX, lowX, highY, lowY)
  1403. {
  1404. var signX, signY;
  1405. var rad = Math.pow(2, 32);
  1406. signX = Math.floor(highX / rad) >>> 0;
  1407. highX = highX % rad >>> 0;
  1408. signY = Math.floor(highY / rad) >>> 0;
  1409. highY = highY % rad >>> 0;
  1410. if(signX == 0 && signY == 0)
  1411. {
  1412. return cmpAbsLong(highX, lowX, highY, lowY);
  1413. }
  1414. else if(signX == 0 && signY == 1)
  1415. {
  1416. return 1;
  1417. }
  1418. else if(signX == 1 && signY == 0)
  1419. {
  1420. return -1;
  1421. }
  1422. else
  1423. {
  1424. return cmpAbsLong(highX, lowX, highY, lowY) * -1;
  1425. }
  1426. }
  1427. var doAddLong = function(highX, lowX, highY, lowY)
  1428. {
  1429. var signX, signY;
  1430. var Z;
  1431. var res;
  1432. var rad = Math.pow(2, 32);
  1433. signX = Math.floor(highX / rad) >>> 0;
  1434. highX = highX % rad >>> 0;
  1435. signY = Math.floor(highY / rad) >>> 0;
  1436. highY = highY % rad >>> 0;
  1437. if(signX == signY)
  1438. {
  1439. Z = addAbsLong(highX, lowX, highY, lowY);
  1440. Z.high += rad * signX;
  1441. }
  1442. else
  1443. {
  1444. res = cmpAbsLong(highX, lowX, highY, lowY);
  1445. if(res == 1)
  1446. {
  1447. Z = subAbsLong(highX, lowX, highY, lowY);
  1448. Z.high += rad * signX;
  1449. }
  1450. else if(res == 0)
  1451. {
  1452. //符号が違って絶対値が等しければ答えは0
  1453. Z = {high:0, low:0};
  1454. }
  1455. else
  1456. {
  1457. Z = subAbsLong(highY, lowY, highX, lowX);
  1458. Z.high += rad * signY;
  1459. }
  1460. }
  1461. return Z;
  1462. }
  1463. var doSubLong = function(highX, lowX, highY, lowY)
  1464. {
  1465. var signX, signY;
  1466. var Z;
  1467. var res;
  1468. var rad = Math.pow(2, 32);
  1469. signX = Math.floor(highX / rad) >>> 0;
  1470. highX = highX % rad >>> 0;
  1471. signY = Math.floor(highY / rad) >>> 0;
  1472. highY = highY % rad >>> 0;
  1473. if(signX == 0)
  1474. {
  1475. if(signY == 0)
  1476. {
  1477. res = cmpAbsLong(highX, lowX, highY, lowY);
  1478. if(res == 1)
  1479. {
  1480. Z = subAbsLong(highX, lowX, highY, lowY);
  1481. }
  1482. else if(res == 0)
  1483. {
  1484. Z = {high:0, low:0};
  1485. }
  1486. else
  1487. {
  1488. Z = subAbsLong(highY, lowY, highX, lowX);
  1489. Z.high += rad;
  1490. }
  1491. }
  1492. else
  1493. {
  1494. Z = addAbsLong(highX, lowX, highY, lowY);
  1495. }
  1496. }
  1497. else
  1498. {
  1499. if(signY == 0)
  1500. {
  1501. Z = addAbsLong(highX, lowX, highY, lowY);
  1502. Z.high += rad;
  1503. }
  1504. else
  1505. {
  1506. res = cmpAbsLong(highX, lowX, highY, lowY);
  1507. if(res == 1)
  1508. {
  1509. Z = subAbsLong(highX, lowX, highY, lowY);
  1510. Z.high += rad;
  1511. }
  1512. else if(res == 0)
  1513. {
  1514. Z = {high:0, low:0};
  1515. }
  1516. else
  1517. {
  1518. Z = subAbsLong(highY, lowY, highX, lowX);
  1519. }
  1520. }
  1521. }
  1522. return Z;
  1523. }
  1524. var doMulLong = function(highX, lowX, highY, lowY)
  1525. {
  1526. var signX, signY;
  1527. var buffX = [];
  1528. var buffY = [];
  1529. var buffZ = [];
  1530. var i, j, tmp;
  1531. var Z = {high:0, low:0};
  1532. var rad = Math.pow(2, 32);
  1533. signX = Math.floor(highX / rad) >>> 0;
  1534. highX = highX % rad >>> 0;
  1535. signY = Math.floor(highY / rad) >>> 0;
  1536. highY = highY % rad >>> 0;
  1537. //16bitごとに分割
  1538. buffX[0] = (lowX & 0x0000ffff) >>> 0;
  1539. buffX[1] = lowX >>> 16;
  1540. buffX[2] = (highX & 0x0000ffff) >>> 0;
  1541. buffX[3] = highX >>> 16;
  1542. buffY[0] = (lowY & 0x0000ffff) >>> 0;
  1543. buffY[1] = lowY >>> 16;
  1544. buffY[2] = (highY & 0x0000ffff) >>> 0;
  1545. buffY[3] = highY >>> 16;
  1546. for(i=0; i<8; i++)
  1547. {
  1548. buffZ[i] = 0;
  1549. }
  1550. for(i=0; i<4; i++)
  1551. {
  1552. tmp = 0;
  1553. for(j=0; j<4; j++)
  1554. {
  1555. tmp += buffX[i] * buffY[j];
  1556. tmp += buffZ[i+j];
  1557. buffZ[i+j] = (tmp & 0x0000ffff) >>> 0;
  1558. tmp >>>= 16;
  1559. }
  1560. buffZ[i+j] = (tmp & 0x0000ffff) >>> 0;
  1561. }
  1562. Z.low = (buffZ[0] + (buffZ[1] << 16)) >>> 0;
  1563. Z.high = (buffZ[2] + (buffZ[3] << 16)) >>> 0;
  1564. if(signX != signY && (Z.low != 0 || Z.high != 0))
  1565. {
  1566. Z.high += rad;
  1567. }
  1568. return Z;
  1569. }
  1570. var cmpLongBuff = function(buffX, buffY)
  1571. {
  1572. var i;
  1573. for(i = 3; i >= 0; i--)
  1574. {
  1575. if(buffX[i] > buffY[i])
  1576. {
  1577. return 1;
  1578. }
  1579. else if(buffX[i] == buffY[i])
  1580. {
  1581. continue;
  1582. }
  1583. else
  1584. {
  1585. return -1;
  1586. }
  1587. return 0;
  1588. }
  1589. }
  1590. var subLongBuff = function(buffX, buffY)
  1591. {
  1592. var tmp;
  1593. var rad = Math.pow(2, 16);
  1594. var i;
  1595. tmp = 0;
  1596. for(i = 0; i < 4; i++)
  1597. {
  1598. tmp = rad + buffX[i] - buffY[i] - tmp;
  1599. buffX[i] = (tmp & 0xffff) >>> 0;
  1600. if(tmp & 0x10000)
  1601. {
  1602. tmp = 0;
  1603. }
  1604. else
  1605. {
  1606. tmp = 1;
  1607. }
  1608. }
  1609. }
  1610. var doDivLong = function(highX, lowX, highY, lowY)
  1611. {
  1612. var signX, signY;
  1613. var buffW = [];
  1614. var buffX = [];
  1615. var buffY = [];
  1616. var buffZ = [];
  1617. var sizeX, sizeY, sizeZ;
  1618. var i, j, tmp, q;
  1619. var rad = Math.pow(2, 32);
  1620. var ret = {highQuot:0, lowQuot:0, highMod:0, lowMod:0};
  1621. signX = Math.floor(highX / rad) >>> 0;
  1622. highX = highX % rad >>> 0;
  1623. signY = Math.floor(highY / rad) >>> 0;
  1624. highY = highY % rad >>> 0;
  1625. //16bitごとに分割
  1626. buffX[0] = (lowX & 0x0000ffff) >>> 0;
  1627. buffX[1] = lowX >>> 16;
  1628. buffX[2] = (highX & 0x0000ffff) >>> 0;
  1629. buffX[3] = highX >>> 16;
  1630. buffY[0] = (lowY & 0x0000ffff) >>> 0;
  1631. buffY[1] = lowY >>> 16;
  1632. buffY[2] = (highY & 0x0000ffff) >>> 0;
  1633. buffY[3] = highY >>> 16;
  1634. buffZ[0] = buffZ[1] = buffZ[2] = buffZ[3] = 0;
  1635. sizeX = 0;
  1636. for(i=3; i>=0; i--)
  1637. {
  1638. if(buffX[i] > 0)
  1639. {
  1640. sizeX = i + 1;
  1641. break;
  1642. }
  1643. }
  1644. if(sizeX == 0)
  1645. {
  1646. return {highQuot:0, lowQuot:0, highMod:0, lowMod:0};
  1647. }
  1648. sizeY = 0;
  1649. for(i=3; i>=0; i--)
  1650. {
  1651. if(buffY[i] > 0)
  1652. {
  1653. sizeY = i + 1;
  1654. break;
  1655. }
  1656. }
  1657. if(sizeY == 0)
  1658. {
  1659. throw Error("divide by 0(long)");
  1660. }
  1661. sizeZ = sizeX - sizeY + 1;
  1662. q = Math.floor(buffX[sizeX-1] / buffY[sizeY-1]);
  1663. if(q == 0)
  1664. {
  1665. if(sizeX >= 2)
  1666. {
  1667. q = Math.floor((((buffX[sizeX-1] << 16) + buffX[sizeX-2]) >>> 0) / buffY[sizeY-1]);

Large files files are truncated, but you can click here to view the full file