aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/analyzer.js9
-rw-r--r--src/fastLong.js298
-rw-r--r--src/jsifier.js44
-rw-r--r--src/library.js79
-rw-r--r--src/long.js56
-rw-r--r--src/modules.js2
-rw-r--r--src/parseTools.js17
7 files changed, 404 insertions, 101 deletions
diff --git a/src/analyzer.js b/src/analyzer.js
index c10f18cc..df5a435e 100644
--- a/src/analyzer.js
+++ b/src/analyzer.js
@@ -124,6 +124,10 @@ function analyzer(data, sidePass) {
bits = bits || 32; // things like pointers are all i32, but show up as 0 bits from getBits
if (allowLegal && bits <= 32) return [{ ident: base + ('i' + bits in Runtime.INT_TYPES ? '' : '$0'), bits: bits }];
if (isNumber(base)) return getLegalLiterals(base, bits);
+ if (base[0] == '{') {
+ warnOnce('seeing source of illegal data ' + base + ', likely an inline struct - assuming zeroinit');
+ return getLegalLiterals('0', bits);
+ }
var ret = new Array(Math.ceil(bits/32));
var i = 0;
if (base == 'zeroinitializer' || base == 'undef') base = 0;
@@ -320,12 +324,13 @@ function analyzer(data, sidePass) {
}
// call, return: Return the first 32 bits, the rest are in temp
case 'call': {
- bits = getBits(value.type);
- var elements = getLegalVars(item.assignTo, bits);
var toAdd = [value];
// legalize parameters
legalizeFunctionParameters(value.params);
+ // legalize return value, if any
if (value.assignTo && isIllegalType(item.type)) {
+ bits = getBits(value.type);
+ var elements = getLegalVars(item.assignTo, bits);
// legalize return value
value.assignTo = elements[0].ident;
for (var j = 1; j < elements.length; j++) {
diff --git a/src/fastLong.js b/src/fastLong.js
new file mode 100644
index 00000000..95f398db
--- /dev/null
+++ b/src/fastLong.js
@@ -0,0 +1,298 @@
+// ======== compiled code from system/lib/compiler-rt , see readme therein
+function ___muldsi3($a, $b) {
+ $a = $a | 0;
+ $b = $b | 0;
+ var $1 = 0, $2 = 0, $3 = 0, $6 = 0, $8 = 0, $11 = 0, $12 = 0;
+ $1 = $a & 65535;
+ $2 = $b & 65535;
+ $3 = Math.imul($2, $1);
+ $6 = $a >>> 16;
+ $8 = ($3 >>> 16) + Math.imul($2, $6) | 0;
+ $11 = $b >>> 16;
+ $12 = Math.imul($11, $1);
+ return (tempRet0 = (($8 >>> 16) + Math.imul($11, $6) | 0) + ((($8 & 65535) + $12 | 0) >>> 16) | 0, 0 | ($8 + $12 << 16 | $3 & 65535)) | 0;
+}
+function ___divdi3($a$0, $a$1, $b$0, $b$1) {
+ $a$0 = $a$0 | 0;
+ $a$1 = $a$1 | 0;
+ $b$0 = $b$0 | 0;
+ $b$1 = $b$1 | 0;
+ var $1$0 = 0, $1$1 = 0, $2$0 = 0, $2$1 = 0, $4$0 = 0, $4$1 = 0, $6$0 = 0, $7$0 = 0, $7$1 = 0, $8$0 = 0, $10$0 = 0;
+ $1$0 = $a$1 >> 31 | (($a$1 | 0) < 0 ? -1 : 0) << 1;
+ $1$1 = (($a$1 | 0) < 0 ? -1 : 0) >> 31 | (($a$1 | 0) < 0 ? -1 : 0) << 1;
+ $2$0 = $b$1 >> 31 | (($b$1 | 0) < 0 ? -1 : 0) << 1;
+ $2$1 = (($b$1 | 0) < 0 ? -1 : 0) >> 31 | (($b$1 | 0) < 0 ? -1 : 0) << 1;
+ $4$0 = _i64Subtract($1$0 ^ $a$0, $1$1 ^ $a$1, $1$0, $1$1);
+ $4$1 = tempRet0;
+ $6$0 = _i64Subtract($2$0 ^ $b$0, $2$1 ^ $b$1, $2$0, $2$1);
+ $7$0 = $2$0 ^ $1$0;
+ $7$1 = $2$1 ^ $1$1;
+ $8$0 = ___udivmoddi4($4$0, $4$1, $6$0, tempRet0, 0) | 0;
+ $10$0 = _i64Subtract($8$0 ^ $7$0, tempRet0 ^ $7$1, $7$0, $7$1);
+ return (tempRet0 = tempRet0, $10$0) | 0;
+}
+function ___remdi3($a$0, $a$1, $b$0, $b$1) {
+ $a$0 = $a$0 | 0;
+ $a$1 = $a$1 | 0;
+ $b$0 = $b$0 | 0;
+ $b$1 = $b$1 | 0;
+ var $rem = 0, $1$0 = 0, $1$1 = 0, $2$0 = 0, $2$1 = 0, $4$0 = 0, $4$1 = 0, $6$0 = 0, $10$0 = 0, $10$1 = 0, __stackBase__ = 0;
+ __stackBase__ = STACKTOP;
+ STACKTOP = STACKTOP + 8 | 0;
+ $rem = __stackBase__ | 0;
+ $1$0 = $a$1 >> 31 | (($a$1 | 0) < 0 ? -1 : 0) << 1;
+ $1$1 = (($a$1 | 0) < 0 ? -1 : 0) >> 31 | (($a$1 | 0) < 0 ? -1 : 0) << 1;
+ $2$0 = $b$1 >> 31 | (($b$1 | 0) < 0 ? -1 : 0) << 1;
+ $2$1 = (($b$1 | 0) < 0 ? -1 : 0) >> 31 | (($b$1 | 0) < 0 ? -1 : 0) << 1;
+ $4$0 = _i64Subtract($1$0 ^ $a$0, $1$1 ^ $a$1, $1$0, $1$1);
+ $4$1 = tempRet0;
+ $6$0 = _i64Subtract($2$0 ^ $b$0, $2$1 ^ $b$1, $2$0, $2$1);
+ ___udivmoddi4($4$0, $4$1, $6$0, tempRet0, $rem);
+ $10$0 = _i64Subtract(HEAP32[$rem >> 2] ^ $1$0, HEAP32[$rem + 4 >> 2] ^ $1$1, $1$0, $1$1);
+ $10$1 = tempRet0;
+ STACKTOP = __stackBase__;
+ return (tempRet0 = $10$1, $10$0) | 0;
+}
+function ___muldi3($a$0, $a$1, $b$0, $b$1) {
+ $a$0 = $a$0 | 0;
+ $a$1 = $a$1 | 0;
+ $b$0 = $b$0 | 0;
+ $b$1 = $b$1 | 0;
+ var $x_sroa_0_0_extract_trunc = 0, $y_sroa_0_0_extract_trunc = 0, $1$0 = 0, $1$1 = 0, $2 = 0;
+ $x_sroa_0_0_extract_trunc = $a$0;
+ $y_sroa_0_0_extract_trunc = $b$0;
+ $1$0 = ___muldsi3($x_sroa_0_0_extract_trunc, $y_sroa_0_0_extract_trunc) | 0;
+ $1$1 = tempRet0;
+ $2 = Math.imul($a$1, $y_sroa_0_0_extract_trunc);
+ return (tempRet0 = (Math.imul($b$1, $x_sroa_0_0_extract_trunc) + $2 | 0) + $1$1 | $1$1 & 0, 0 | $1$0 & -1) | 0;
+}
+function ___udivdi3($a$0, $a$1, $b$0, $b$1) {
+ $a$0 = $a$0 | 0;
+ $a$1 = $a$1 | 0;
+ $b$0 = $b$0 | 0;
+ $b$1 = $b$1 | 0;
+ var $1$0 = 0;
+ $1$0 = ___udivmoddi4($a$0, $a$1, $b$0, $b$1, 0) | 0;
+ return (tempRet0 = tempRet0, $1$0) | 0;
+}
+function ___uremdi3($a$0, $a$1, $b$0, $b$1) {
+ $a$0 = $a$0 | 0;
+ $a$1 = $a$1 | 0;
+ $b$0 = $b$0 | 0;
+ $b$1 = $b$1 | 0;
+ var $rem = 0, __stackBase__ = 0;
+ __stackBase__ = STACKTOP;
+ STACKTOP = STACKTOP + 8 | 0;
+ $rem = __stackBase__ | 0;
+ ___udivmoddi4($a$0, $a$1, $b$0, $b$1, $rem);
+ STACKTOP = __stackBase__;
+ return (tempRet0 = HEAP32[$rem + 4 >> 2] | 0, HEAP32[$rem >> 2] | 0) | 0;
+}
+function ___udivmoddi4($a$0, $a$1, $b$0, $b$1, $rem) {
+ $a$0 = $a$0 | 0;
+ $a$1 = $a$1 | 0;
+ $b$0 = $b$0 | 0;
+ $b$1 = $b$1 | 0;
+ $rem = $rem | 0;
+ var $n_sroa_0_0_extract_trunc = 0, $n_sroa_1_4_extract_shift$0 = 0, $n_sroa_1_4_extract_trunc = 0, $d_sroa_0_0_extract_trunc = 0, $d_sroa_1_4_extract_shift$0 = 0, $d_sroa_1_4_extract_trunc = 0, $4 = 0, $17 = 0, $37 = 0, $49 = 0, $51 = 0, $57 = 0, $58 = 0, $66 = 0, $78 = 0, $86 = 0, $88 = 0, $89 = 0, $91 = 0, $92 = 0, $95 = 0, $105 = 0, $117 = 0, $119 = 0, $125 = 0, $126 = 0, $130 = 0, $q_sroa_1_1_ph = 0, $q_sroa_0_1_ph = 0, $r_sroa_1_1_ph = 0, $r_sroa_0_1_ph = 0, $sr_1_ph = 0, $d_sroa_0_0_insert_insert99$0 = 0, $d_sroa_0_0_insert_insert99$1 = 0, $137$0 = 0, $137$1 = 0, $carry_0203 = 0, $sr_1202 = 0, $r_sroa_0_1201 = 0, $r_sroa_1_1200 = 0, $q_sroa_0_1199 = 0, $q_sroa_1_1198 = 0, $147 = 0, $149 = 0, $r_sroa_0_0_insert_insert42$0 = 0, $r_sroa_0_0_insert_insert42$1 = 0, $150$1 = 0, $151$0 = 0, $152 = 0, $154$0 = 0, $r_sroa_0_0_extract_trunc = 0, $r_sroa_1_4_extract_trunc = 0, $155 = 0, $carry_0_lcssa$0 = 0, $carry_0_lcssa$1 = 0, $r_sroa_0_1_lcssa = 0, $r_sroa_1_1_lcssa = 0, $q_sroa_0_1_lcssa = 0, $q_sroa_1_1_lcssa = 0, $q_sroa_0_0_insert_ext75$0 = 0, $q_sroa_0_0_insert_ext75$1 = 0, $q_sroa_0_0_insert_insert77$1 = 0, $_0$0 = 0, $_0$1 = 0;
+ $n_sroa_0_0_extract_trunc = $a$0;
+ $n_sroa_1_4_extract_shift$0 = $a$1;
+ $n_sroa_1_4_extract_trunc = $n_sroa_1_4_extract_shift$0;
+ $d_sroa_0_0_extract_trunc = $b$0;
+ $d_sroa_1_4_extract_shift$0 = $b$1;
+ $d_sroa_1_4_extract_trunc = $d_sroa_1_4_extract_shift$0;
+ if (($n_sroa_1_4_extract_trunc | 0) == 0) {
+ $4 = ($rem | 0) != 0;
+ if (($d_sroa_1_4_extract_trunc | 0) == 0) {
+ if ($4) {
+ HEAP32[$rem >> 2] = ($n_sroa_0_0_extract_trunc >>> 0) % ($d_sroa_0_0_extract_trunc >>> 0);
+ HEAP32[$rem + 4 >> 2] = 0;
+ }
+ $_0$1 = 0;
+ $_0$0 = ($n_sroa_0_0_extract_trunc >>> 0) / ($d_sroa_0_0_extract_trunc >>> 0) >>> 0;
+ return (tempRet0 = $_0$1, $_0$0) | 0;
+ } else {
+ if (!$4) {
+ $_0$1 = 0;
+ $_0$0 = 0;
+ return (tempRet0 = $_0$1, $_0$0) | 0;
+ }
+ HEAP32[$rem >> 2] = $a$0 & -1;
+ HEAP32[$rem + 4 >> 2] = $a$1 & 0;
+ $_0$1 = 0;
+ $_0$0 = 0;
+ return (tempRet0 = $_0$1, $_0$0) | 0;
+ }
+ }
+ $17 = ($d_sroa_1_4_extract_trunc | 0) == 0;
+ do {
+ if (($d_sroa_0_0_extract_trunc | 0) == 0) {
+ if ($17) {
+ if (($rem | 0) != 0) {
+ HEAP32[$rem >> 2] = ($n_sroa_1_4_extract_trunc >>> 0) % ($d_sroa_0_0_extract_trunc >>> 0);
+ HEAP32[$rem + 4 >> 2] = 0;
+ }
+ $_0$1 = 0;
+ $_0$0 = ($n_sroa_1_4_extract_trunc >>> 0) / ($d_sroa_0_0_extract_trunc >>> 0) >>> 0;
+ return (tempRet0 = $_0$1, $_0$0) | 0;
+ }
+ if (($n_sroa_0_0_extract_trunc | 0) == 0) {
+ if (($rem | 0) != 0) {
+ HEAP32[$rem >> 2] = 0;
+ HEAP32[$rem + 4 >> 2] = ($n_sroa_1_4_extract_trunc >>> 0) % ($d_sroa_1_4_extract_trunc >>> 0);
+ }
+ $_0$1 = 0;
+ $_0$0 = ($n_sroa_1_4_extract_trunc >>> 0) / ($d_sroa_1_4_extract_trunc >>> 0) >>> 0;
+ return (tempRet0 = $_0$1, $_0$0) | 0;
+ }
+ $37 = $d_sroa_1_4_extract_trunc - 1 | 0;
+ if (($37 & $d_sroa_1_4_extract_trunc | 0) == 0) {
+ if (($rem | 0) != 0) {
+ HEAP32[$rem >> 2] = 0 | $a$0 & -1;
+ HEAP32[$rem + 4 >> 2] = $37 & $n_sroa_1_4_extract_trunc | $a$1 & 0;
+ }
+ $_0$1 = 0;
+ $_0$0 = $n_sroa_1_4_extract_trunc >>> ((_llvm_cttz_i32($d_sroa_1_4_extract_trunc | 0) | 0) >>> 0);
+ return (tempRet0 = $_0$1, $_0$0) | 0;
+ }
+ $49 = _llvm_ctlz_i32($d_sroa_1_4_extract_trunc | 0) | 0;
+ $51 = $49 - (_llvm_ctlz_i32($n_sroa_1_4_extract_trunc | 0) | 0) | 0;
+ if ($51 >>> 0 <= 30) {
+ $57 = $51 + 1 | 0;
+ $58 = 31 - $51 | 0;
+ $sr_1_ph = $57;
+ $r_sroa_0_1_ph = $n_sroa_1_4_extract_trunc << $58 | $n_sroa_0_0_extract_trunc >>> ($57 >>> 0);
+ $r_sroa_1_1_ph = $n_sroa_1_4_extract_trunc >>> ($57 >>> 0);
+ $q_sroa_0_1_ph = 0;
+ $q_sroa_1_1_ph = $n_sroa_0_0_extract_trunc << $58;
+ break;
+ }
+ if (($rem | 0) == 0) {
+ $_0$1 = 0;
+ $_0$0 = 0;
+ return (tempRet0 = $_0$1, $_0$0) | 0;
+ }
+ HEAP32[$rem >> 2] = 0 | $a$0 & -1;
+ HEAP32[$rem + 4 >> 2] = $n_sroa_1_4_extract_shift$0 | $a$1 & 0;
+ $_0$1 = 0;
+ $_0$0 = 0;
+ return (tempRet0 = $_0$1, $_0$0) | 0;
+ } else {
+ if (!$17) {
+ $117 = _llvm_ctlz_i32($d_sroa_1_4_extract_trunc | 0) | 0;
+ $119 = $117 - (_llvm_ctlz_i32($n_sroa_1_4_extract_trunc | 0) | 0) | 0;
+ if ($119 >>> 0 <= 31) {
+ $125 = $119 + 1 | 0;
+ $126 = 31 - $119 | 0;
+ $130 = $119 - 31 >> 31;
+ $sr_1_ph = $125;
+ $r_sroa_0_1_ph = $n_sroa_0_0_extract_trunc >>> ($125 >>> 0) & $130 | $n_sroa_1_4_extract_trunc << $126;
+ $r_sroa_1_1_ph = $n_sroa_1_4_extract_trunc >>> ($125 >>> 0) & $130;
+ $q_sroa_0_1_ph = 0;
+ $q_sroa_1_1_ph = $n_sroa_0_0_extract_trunc << $126;
+ break;
+ }
+ if (($rem | 0) == 0) {
+ $_0$1 = 0;
+ $_0$0 = 0;
+ return (tempRet0 = $_0$1, $_0$0) | 0;
+ }
+ HEAP32[$rem >> 2] = 0 | $a$0 & -1;
+ HEAP32[$rem + 4 >> 2] = $n_sroa_1_4_extract_shift$0 | $a$1 & 0;
+ $_0$1 = 0;
+ $_0$0 = 0;
+ return (tempRet0 = $_0$1, $_0$0) | 0;
+ }
+ $66 = $d_sroa_0_0_extract_trunc - 1 | 0;
+ if (($66 & $d_sroa_0_0_extract_trunc | 0) != 0) {
+ $86 = (_llvm_ctlz_i32($d_sroa_0_0_extract_trunc | 0) | 0) + 33 | 0;
+ $88 = $86 - (_llvm_ctlz_i32($n_sroa_1_4_extract_trunc | 0) | 0) | 0;
+ $89 = 64 - $88 | 0;
+ $91 = 32 - $88 | 0;
+ $92 = $91 >> 31;
+ $95 = $88 - 32 | 0;
+ $105 = $95 >> 31;
+ $sr_1_ph = $88;
+ $r_sroa_0_1_ph = $91 - 1 >> 31 & $n_sroa_1_4_extract_trunc >>> ($95 >>> 0) | ($n_sroa_1_4_extract_trunc << $91 | $n_sroa_0_0_extract_trunc >>> ($88 >>> 0)) & $105;
+ $r_sroa_1_1_ph = $105 & $n_sroa_1_4_extract_trunc >>> ($88 >>> 0);
+ $q_sroa_0_1_ph = $n_sroa_0_0_extract_trunc << $89 & $92;
+ $q_sroa_1_1_ph = ($n_sroa_1_4_extract_trunc << $89 | $n_sroa_0_0_extract_trunc >>> ($95 >>> 0)) & $92 | $n_sroa_0_0_extract_trunc << $91 & $88 - 33 >> 31;
+ break;
+ }
+ if (($rem | 0) != 0) {
+ HEAP32[$rem >> 2] = $66 & $n_sroa_0_0_extract_trunc;
+ HEAP32[$rem + 4 >> 2] = 0;
+ }
+ if (($d_sroa_0_0_extract_trunc | 0) == 1) {
+ $_0$1 = $n_sroa_1_4_extract_shift$0 | $a$1 & 0;
+ $_0$0 = 0 | $a$0 & -1;
+ return (tempRet0 = $_0$1, $_0$0) | 0;
+ } else {
+ $78 = _llvm_cttz_i32($d_sroa_0_0_extract_trunc | 0) | 0;
+ $_0$1 = 0 | $n_sroa_1_4_extract_trunc >>> ($78 >>> 0);
+ $_0$0 = $n_sroa_1_4_extract_trunc << 32 - $78 | $n_sroa_0_0_extract_trunc >>> ($78 >>> 0) | 0;
+ return (tempRet0 = $_0$1, $_0$0) | 0;
+ }
+ }
+ } while (0);
+ if (($sr_1_ph | 0) == 0) {
+ $q_sroa_1_1_lcssa = $q_sroa_1_1_ph;
+ $q_sroa_0_1_lcssa = $q_sroa_0_1_ph;
+ $r_sroa_1_1_lcssa = $r_sroa_1_1_ph;
+ $r_sroa_0_1_lcssa = $r_sroa_0_1_ph;
+ $carry_0_lcssa$1 = 0;
+ $carry_0_lcssa$0 = 0;
+ } else {
+ $d_sroa_0_0_insert_insert99$0 = 0 | $b$0 & -1;
+ $d_sroa_0_0_insert_insert99$1 = $d_sroa_1_4_extract_shift$0 | $b$1 & 0;
+ $137$0 = _i64Add($d_sroa_0_0_insert_insert99$0, $d_sroa_0_0_insert_insert99$1, -1, -1);
+ $137$1 = tempRet0;
+ $q_sroa_1_1198 = $q_sroa_1_1_ph;
+ $q_sroa_0_1199 = $q_sroa_0_1_ph;
+ $r_sroa_1_1200 = $r_sroa_1_1_ph;
+ $r_sroa_0_1201 = $r_sroa_0_1_ph;
+ $sr_1202 = $sr_1_ph;
+ $carry_0203 = 0;
+ while (1) {
+ $147 = $q_sroa_0_1199 >>> 31 | $q_sroa_1_1198 << 1;
+ $149 = $carry_0203 | $q_sroa_0_1199 << 1;
+ $r_sroa_0_0_insert_insert42$0 = 0 | ($r_sroa_0_1201 << 1 | $q_sroa_1_1198 >>> 31);
+ $r_sroa_0_0_insert_insert42$1 = $r_sroa_0_1201 >>> 31 | $r_sroa_1_1200 << 1 | 0;
+ _i64Subtract($137$0, $137$1, $r_sroa_0_0_insert_insert42$0, $r_sroa_0_0_insert_insert42$1);
+ $150$1 = tempRet0;
+ $151$0 = $150$1 >> 31 | (($150$1 | 0) < 0 ? -1 : 0) << 1;
+ $152 = $151$0 & 1;
+ $154$0 = _i64Subtract($r_sroa_0_0_insert_insert42$0, $r_sroa_0_0_insert_insert42$1, $151$0 & $d_sroa_0_0_insert_insert99$0, ((($150$1 | 0) < 0 ? -1 : 0) >> 31 | (($150$1 | 0) < 0 ? -1 : 0) << 1) & $d_sroa_0_0_insert_insert99$1);
+ $r_sroa_0_0_extract_trunc = $154$0;
+ $r_sroa_1_4_extract_trunc = tempRet0;
+ $155 = $sr_1202 - 1 | 0;
+ if (($155 | 0) == 0) {
+ break;
+ } else {
+ $q_sroa_1_1198 = $147;
+ $q_sroa_0_1199 = $149;
+ $r_sroa_1_1200 = $r_sroa_1_4_extract_trunc;
+ $r_sroa_0_1201 = $r_sroa_0_0_extract_trunc;
+ $sr_1202 = $155;
+ $carry_0203 = $152;
+ }
+ }
+ $q_sroa_1_1_lcssa = $147;
+ $q_sroa_0_1_lcssa = $149;
+ $r_sroa_1_1_lcssa = $r_sroa_1_4_extract_trunc;
+ $r_sroa_0_1_lcssa = $r_sroa_0_0_extract_trunc;
+ $carry_0_lcssa$1 = 0;
+ $carry_0_lcssa$0 = $152;
+ }
+ $q_sroa_0_0_insert_ext75$0 = $q_sroa_0_1_lcssa;
+ $q_sroa_0_0_insert_ext75$1 = 0;
+ $q_sroa_0_0_insert_insert77$1 = $q_sroa_1_1_lcssa | $q_sroa_0_0_insert_ext75$1;
+ if (($rem | 0) != 0) {
+ HEAP32[$rem >> 2] = 0 | $r_sroa_0_1_lcssa;
+ HEAP32[$rem + 4 >> 2] = $r_sroa_1_1_lcssa | 0;
+ }
+ $_0$1 = (0 | $q_sroa_0_0_insert_ext75$0) >>> 31 | $q_sroa_0_0_insert_insert77$1 << 1 | ($q_sroa_0_0_insert_ext75$1 << 1 | $q_sroa_0_0_insert_ext75$0 >>> 31) & 0 | $carry_0_lcssa$1;
+ $_0$0 = ($q_sroa_0_0_insert_ext75$0 << 1 | 0 >>> 31) & -2 | $carry_0_lcssa$0;
+ return (tempRet0 = $_0$1, $_0$0) | 0;
+}
+// =======================================================================
diff --git a/src/jsifier.js b/src/jsifier.js
index 2a9dc5bd..926be71a 100644
--- a/src/jsifier.js
+++ b/src/jsifier.js
@@ -468,7 +468,7 @@ function JSify(data, functionsOnly, givenFunctions) {
asmLibraryFunctions.push(contentText);
contentText = ' ';
EXPORTED_FUNCTIONS[ident] = 1;
- delete Functions.libraryFunctions[ident.substr(1)];
+ Functions.libraryFunctions[ident.substr(1)] = 2;
}
}
if ((!ASM_JS || phase == 'pre') &&
@@ -1149,9 +1149,7 @@ function JSify(data, functionsOnly, givenFunctions) {
}
// If there is no current exception, set this one as it (during a resume, the current exception can be wiped out)
var ptr = makeStructuralAccess(item.ident, 0);
- return (EXCEPTION_DEBUG ? 'Module.print("Resuming exception");' : '') +
- 'if (' + makeGetValue('_llvm_eh_exception.buf', 0, 'void*') + ' == 0) { ' + makeSetValue('_llvm_eh_exception.buf', 0, ptr, 'void*') + ' } ' +
- makeThrow(ptr) + ';';
+ return '___resumeException(' + asmCoercion(ptr, 'i32') + ')';
});
makeFuncLineActor('invoke', function(item) {
// Wrapping in a function lets us easily return values if we are
@@ -1162,8 +1160,10 @@ function JSify(data, functionsOnly, givenFunctions) {
var ret;
- if (disabled || ASM_JS) { // TODO: EXCEPTION_DEBUG for asm.js
+ if (disabled) {
ret = call_ + ';';
+ } else if (ASM_JS) {
+ ret = '(__THREW__ = 0,' + call_ + ');';
} else {
ret = '(function() { try { __THREW__ = 0; return '
+ call_ + ' '
@@ -1210,8 +1210,8 @@ function JSify(data, functionsOnly, givenFunctions) {
}
});
makeFuncLineActor('landingpad', function(item) {
- var catchTypeArray = item.catchables.map(finalizeLLVMParameter).join(',');
- var ret = '___cxa_find_matching_catch('+ makeGetValue('_llvm_eh_exception.buf', '0', 'void*') +',' + makeGetValue('_llvm_eh_exception.buf', QUANTUM_SIZE, 'void*') + (catchTypeArray.length > 0 ? ',' + catchTypeArray : '') +')';
+ var catchTypeArray = item.catchables.map(finalizeLLVMParameter).map(function(element) { return asmCoercion(element, 'i32') }).join(',');
+ var ret = asmCoercion('___cxa_find_matching_catch(-1, -1' + (catchTypeArray.length > 0 ? ',' + catchTypeArray : '') +')', 'i32');
if (USE_TYPED_ARRAYS == 2) {
ret = makeVarDef(item.assignTo) + '$0 = ' + ret + '; ' + item.assignTo + '$1 = tempRet0;';
item.assignTo = null;
@@ -1348,7 +1348,7 @@ function JSify(data, functionsOnly, givenFunctions) {
args = args.map(function(arg, i) { return indexizeFunctions(arg, argsTypes[i]) });
if (ASM_JS) {
- if (shortident in Functions.libraryFunctions || simpleIdent in Functions.libraryFunctions) {
+ if (shortident in Functions.libraryFunctions || simpleIdent in Functions.libraryFunctions || byPointerForced) {
args = args.map(function(arg, i) { return asmCoercion(arg, argsTypes[i]) });
} else {
args = args.map(function(arg, i) { return asmEnsureFloat(arg, argsTypes[i]) });
@@ -1426,10 +1426,12 @@ function JSify(data, functionsOnly, givenFunctions) {
var sig = Functions.getSignature(returnType, argsTypes, hasVarArgs);
if (ASM_JS) {
assert(returnType.search(/\("'\[,/) == -1); // XXX need isFunctionType(type, out)
- if (!forceByPointer) {
+ if (!byPointerForced) {
callIdent = '(' + callIdent + ')&{{{ FTM_' + sig + ' }}}'; // the function table mask is set in emscripten.py
} else {
- // add initial argument for invoke. note: no need to update argsTypes at this point
+ // This is a forced call, through an invoke_*.
+ // note: no need to update argsTypes at this point
+ Functions.unimplementedFunctions[callIdent] = sig;
args.unshift(byPointerForced ? Functions.getIndex(callIdent) : callIdent);
callIdent = 'invoke_' + sig;
}
@@ -1604,10 +1606,26 @@ function JSify(data, functionsOnly, givenFunctions) {
// "Final shape that will be created").
if (PRECISE_I64_MATH && Types.preciseI64MathUsed) {
if (!INCLUDE_FULL_LIBRARY) {
- ['i64Add', 'bitshift64Shl', 'bitshift64Lshr', 'bitshift64Ashr'].forEach(function(func) {
- print(processLibraryFunction(LibraryManager.library[func], func)); // must be first to be close to generated code
- Functions.implementedFunctions['_' + func] = LibraryManager.library[func + '__sig'];
+ // first row are utilities called from generated code, second are needed from fastLong
+ ['i64Add', 'i64Subtract', 'bitshift64Shl', 'bitshift64Lshr', 'bitshift64Ashr',
+ 'llvm_ctlz_i32', 'llvm_cttz_i32'].forEach(function(func) {
+ if (!Functions.libraryFunctions[func]) {
+ print(processLibraryFunction(LibraryManager.library[func], func)); // must be first to be close to generated code
+ Functions.implementedFunctions['_' + func] = LibraryManager.library[func + '__sig'];
+ Functions.libraryFunctions[func] = 1;
+ // limited dependency handling
+ var deps = LibraryManager.library[func + '__deps'];
+ if (deps) {
+ deps.forEach(function(dep) {
+ assert(typeof dep == 'function');
+ var text = dep();
+ assert(text.indexOf('\n') < 0);
+ print('/* PRE_ASM */ ' + text + '\n');
+ });
+ }
+ }
});
+ print(read('fastLong.js'));
}
print('// EMSCRIPTEN_END_FUNCS\n');
print(read('long.js'));
diff --git a/src/library.js b/src/library.js
index f58c7150..11f30a1c 100644
--- a/src/library.js
+++ b/src/library.js
@@ -4913,16 +4913,20 @@ LibraryManager.library = {
}
return 8;
}
- return 'var ctlz_i8 = [' + range(256).map(function(x) { return ctlz(x) }).join(',') + '];';
+ return 'var ctlz_i8 = allocate([' + range(256).map(function(x) { return ctlz(x) }).join(',') + '], "i8", ALLOC_STACK);';
}],
+ llvm_ctlz_i32__asm: true,
+ llvm_ctlz_i32__sig: 'ii',
llvm_ctlz_i32: function(x) {
- var ret = ctlz_i8[x >>> 24];
- if (ret < 8) return ret;
- var ret = ctlz_i8[(x >> 16)&0xff];
- if (ret < 8) return ret + 8;
- var ret = ctlz_i8[(x >> 8)&0xff];
- if (ret < 8) return ret + 16;
- return ctlz_i8[x&0xff] + 24;
+ x = x|0;
+ var ret = 0;
+ ret = {{{ makeGetValueAsm('ctlz_i8', 'x >>> 24', 'i8') }}};
+ if ((ret|0) < 8) return ret|0;
+ var ret = {{{ makeGetValueAsm('ctlz_i8', '(x >> 16)&0xff', 'i8') }}};
+ if ((ret|0) < 8) return (ret + 8)|0;
+ var ret = {{{ makeGetValueAsm('ctlz_i8', '(x >> 8)&0xff', 'i8') }}};
+ if ((ret|0) < 8) return (ret + 16)|0;
+ return ({{{ makeGetValueAsm('ctlz_i8', 'x&0xff', 'i8') }}} + 24)|0;
},
llvm_ctlz_i64__deps: ['llvm_ctlz_i32'],
@@ -4945,16 +4949,20 @@ LibraryManager.library = {
}
return 8;
}
- return 'var cttz_i8 = [' + range(256).map(function(x) { return cttz(x) }).join(',') + '];';
+ return 'var cttz_i8 = allocate([' + range(256).map(function(x) { return cttz(x) }).join(',') + '], "i8", ALLOC_STACK);';
}],
+ llvm_cttz_i32__asm: true,
+ llvm_cttz_i32__sig: 'ii',
llvm_cttz_i32: function(x) {
- var ret = cttz_i8[x & 0xff];
- if (ret < 8) return ret;
- var ret = cttz_i8[(x >> 8)&0xff];
- if (ret < 8) return ret + 8;
- var ret = cttz_i8[(x >> 16)&0xff];
- if (ret < 8) return ret + 16;
- return cttz_i8[x >>> 24] + 24;
+ x = x|0;
+ var ret = 0;
+ ret = {{{ makeGetValueAsm('cttz_i8', 'x & 0xff', 'i8') }}};
+ if ((ret|0) < 8) return ret|0;
+ var ret = {{{ makeGetValueAsm('cttz_i8', '(x >> 8)&0xff', 'i8') }}};
+ if ((ret|0) < 8) return (ret + 8)|0;
+ var ret = {{{ makeGetValueAsm('cttz_i8', '(x >> 16)&0xff', 'i8') }}};
+ if ((ret|0) < 8) return (ret + 16)|0;
+ return ({{{ makeGetValueAsm('cttz_i8', 'x >>> 24', 'i8') }}} + 24)|0;
},
llvm_cttz_i64__deps: ['llvm_cttz_i32'],
@@ -5156,8 +5164,10 @@ LibraryManager.library = {
// functionality boils down to picking a suitable 'catch' block.
// We'll do that here, instead, to keep things simpler.
- __cxa_find_matching_catch__deps: ['__cxa_does_inherit', '__cxa_is_number_type'],
+ __cxa_find_matching_catch__deps: ['__cxa_does_inherit', '__cxa_is_number_type', '__resumeException'],
__cxa_find_matching_catch: function(thrown, throwntype) {
+ if (thrown == -1) thrown = {{{ makeGetValue('_llvm_eh_exception.buf', '0', 'void*') }}};
+ if (throwntype == -1) throwntype = {{{ makeGetValue('_llvm_eh_exception.buf', QUANTUM_SIZE, 'void*') }}};
var typeArray = Array.prototype.slice.call(arguments, 2);
// If throwntype is a pointer, this means a pointer has been
@@ -5183,6 +5193,15 @@ LibraryManager.library = {
{{{ makeStructuralReturn(['thrown', 'throwntype']) }}};
},
+ __resumeException__deps: [function() { Functions.libraryFunctions['__resumeException'] = 1 }], // will be called directly from compiled code
+ __resumeException: function(ptr) {
+#if EXCEPTION_DEBUG
+ Module.print("Resuming exception");
+#endif
+ if ({{{ makeGetValue('_llvm_eh_exception.buf', 0, 'void*') }}} == 0) {{{ makeSetValue('_llvm_eh_exception.buf', 0, 'ptr', 'void*') }}};
+ {{{ makeThrow('ptr') }}};
+ },
+
// Recursively walks up the base types of 'possibilityType'
// to see if any of them match 'definiteType'.
__cxa_does_inherit__deps: ['__cxa_is_number_type'],
@@ -5285,9 +5304,11 @@ LibraryManager.library = {
llvm_umul_with_overflow_i64__deps: [function() { Types.preciseI64MathUsed = 1 }],
llvm_umul_with_overflow_i64: function(xl, xh, yl, yh) {
- i64Math.multiply(xl, xh, yl, yh);
- {{{ makeStructuralReturn([makeGetTempDouble(0, 'i32'), makeGetTempDouble(1, 'i32'), '0']) }}};
- // XXX Need to hack support for second param in long.js
+#if ASSERTIONS
+ Runtime.warnOnce('no overflow support in llvm_umul_with_overflow_i64');
+#endif
+ var low = ___muldi3(xl, xh, yl, yh);
+ {{{ makeStructuralReturn(['low', 'tempRet0', '0']) }}};
},
llvm_stacksave: function() {
@@ -7471,13 +7492,27 @@ LibraryManager.library = {
var l = 0, h = 0, overflow = 0;
l = (a + c)>>>0;
h = (b + d)>>>0;
- if ((l>>>0) < (a>>>0)) { // iff we overflowed
+ if ((h>>>0) < (b>>>0)) overflow = 1;
+ if ((l>>>0) < (a>>>0)) {
h = (h+1)>>>0;
- overflow = 1;
+ if ((h>>>0) == 0) overflow = 1; // two possibilities to overflow here
}
{{{ makeStructuralReturn(['l|0', 'h', 'overflow'], true) }}};
},
+ i64Subtract__asm: true,
+ i64Subtract__sig: 'iiiii',
+ i64Subtract: function(a, b, c, d) {
+ a = a|0; b = b|0; c = c|0; d = d|0;
+ var l = 0, h = 0;
+ l = (a - c)>>>0;
+ h = (b - d)>>>0;
+ if ((l>>>0) > (a>>>0)) { // iff we overflowed
+ h = (h-1)>>>0;
+ }
+ {{{ makeStructuralReturn(['l|0', 'h'], true) }}};
+ },
+
bitshift64Shl__asm: true,
bitshift64Shl__sig: 'iiii',
bitshift64Shl: function(low, high, bits) {
diff --git a/src/long.js b/src/long.js
index 6d0e873d..42c86c23 100644
--- a/src/long.js
+++ b/src/long.js
@@ -1530,20 +1530,6 @@ var i64Math = (function() { // Emscripten wrapper
// Emscripten wrapper
var Wrapper = {
- subtract: function(xl, xh, yl, yh) {
- var x = new goog.math.Long(xl, xh);
- var y = new goog.math.Long(yl, yh);
- var ret = x.subtract(y);
- HEAP32[tempDoublePtr>>2] = ret.low_;
- HEAP32[tempDoublePtr+4>>2] = ret.high_;
- },
- multiply: function(xl, xh, yl, yh) {
- var x = new goog.math.Long(xl, xh);
- var y = new goog.math.Long(yl, yh);
- var ret = x.multiply(y);
- HEAP32[tempDoublePtr>>2] = ret.low_;
- HEAP32[tempDoublePtr+4>>2] = ret.high_;
- },
abs: function(l, h) {
var x = new goog.math.Long(l, h);
var ret;
@@ -1576,48 +1562,6 @@ var i64Math = (function() { // Emscripten wrapper
c.addTo(b, d);
return d;
},
- divide: function(xl, xh, yl, yh, unsigned) {
- Wrapper.ensureTemps();
- if (!unsigned) {
- var x = new goog.math.Long(xl, xh);
- var y = new goog.math.Long(yl, yh);
- var ret = x.div(y);
- HEAP32[tempDoublePtr>>2] = ret.low_;
- HEAP32[tempDoublePtr+4>>2] = ret.high_;
- } else {
- // slow precise bignum division
- var x = Wrapper.lh2bignum(xl >>> 0, xh >>> 0);
- var y = Wrapper.lh2bignum(yl >>> 0, yh >>> 0);
- var z = new BigInteger();
- x.divRemTo(y, z, null);
- var l = new BigInteger();
- var h = new BigInteger();
- z.divRemTo(Wrapper.two32, h, l);
- HEAP32[tempDoublePtr>>2] = parseInt(l.toString()) | 0;
- HEAP32[tempDoublePtr+4>>2] = parseInt(h.toString()) | 0;
- }
- },
- modulo: function(xl, xh, yl, yh, unsigned) {
- Wrapper.ensureTemps();
- if (!unsigned) {
- var x = new goog.math.Long(xl, xh);
- var y = new goog.math.Long(yl, yh);
- var ret = x.modulo(y);
- HEAP32[tempDoublePtr>>2] = ret.low_;
- HEAP32[tempDoublePtr+4>>2] = ret.high_;
- } else {
- // slow precise bignum division
- var x = Wrapper.lh2bignum(xl >>> 0, xh >>> 0);
- var y = Wrapper.lh2bignum(yl >>> 0, yh >>> 0);
- var z = new BigInteger();
- x.divRemTo(y, null, z);
- var l = new BigInteger();
- var h = new BigInteger();
- z.divRemTo(Wrapper.two32, h, l);
- HEAP32[tempDoublePtr>>2] = parseInt(l.toString()) | 0;
- HEAP32[tempDoublePtr+4>>2] = parseInt(h.toString()) | 0;
- }
- },
stringify: function(l, h, unsigned) {
var ret = new goog.math.Long(l, h).toString();
if (unsigned && ret[0] == '-') {
diff --git a/src/modules.js b/src/modules.js
index f2994ada..5b5f7947 100644
--- a/src/modules.js
+++ b/src/modules.js
@@ -230,7 +230,7 @@ var Types = {
var Functions = {
// All functions that will be implemented in this file. Maps id to signature
implementedFunctions: {},
- libraryFunctions: {}, // functions added from the library
+ libraryFunctions: {}, // functions added from the library. value 2 means asmLibraryFunction
unimplementedFunctions: {}, // library etc. functions that we need to index, maps id to signature
indexedFunctions: {},
diff --git a/src/parseTools.js b/src/parseTools.js
index 7dafbebe..2eb456f1 100644
--- a/src/parseTools.js
+++ b/src/parseTools.js
@@ -1756,7 +1756,7 @@ function makeStructuralReturn(values, inAsm) {
return 'return ' + asmCoercion(values.slice(1).map(function(value) {
i++;
return ASM_JS ? (inAsm ? 'tempRet' + i + ' = ' + value : 'asm.setTempRet' + i + '(' + value + ')')
- : 'tempRet' + (i++) + ' = ' + value;
+ : 'tempRet' + i + ' = ' + value;
}).concat([values[0]]).join(','), 'i32');
} else {
var i = 0;
@@ -1998,9 +1998,12 @@ function processMathop(item) {
return finish(['(i64Math' + (ASM_JS ? '_' : '.') + type + '(' + asmCoercion(low1, 'i32') + ',' + asmCoercion(high1, 'i32') + ',' + asmCoercion(low2, 'i32') + ',' + asmCoercion(high2, 'i32') +
(lastArg ? ',' + asmCoercion(+lastArg, 'i32') : '') + '),' + makeGetValue('tempDoublePtr', 0, 'i32') + ')', makeGetValue('tempDoublePtr', Runtime.getNativeTypeSize('i32'), 'i32')]);
}
- function i64PreciseLib(type) {
+ function preciseCall(name) {
Types.preciseI64MathUsed = true;
- return finish(['_i64' + type[0].toUpperCase() + type.substr(1) + '(' + low1 + ',' + high1 + ',' + low2 + ',' + high2 + ')', 'tempRet0']);
+ return finish([name + '(' + low1 + ',' + high1 + ',' + low2 + ',' + high2 + ')', 'tempRet0']);
+ }
+ function i64PreciseLib(type) {
+ return preciseCall('_i64' + type[0].toUpperCase() + type.substr(1));
}
switch (op) {
// basic integer ops
@@ -2062,7 +2065,7 @@ function processMathop(item) {
}
case 'sub': {
if (PRECISE_I64_MATH) {
- return i64PreciseOp('subtract');
+ return i64PreciseLib('subtract');
} else {
warnI64_1();
return finish(splitI64(mergeI64(idents[0]) + '-' + mergeI64(idents[1]), true));
@@ -2070,7 +2073,7 @@ function processMathop(item) {
}
case 'sdiv': case 'udiv': {
if (PRECISE_I64_MATH) {
- return i64PreciseOp('divide', op[0] === 'u');
+ return preciseCall(op[0] === 'u' ? '___udivdi3' : '___divdi3');
} else {
warnI64_1();
return finish(splitI64(makeRounding(mergeI64(idents[0], op[0] === 'u') + '/' + mergeI64(idents[1], op[0] === 'u'), bits, op[0] === 's'), true));
@@ -2078,7 +2081,7 @@ function processMathop(item) {
}
case 'mul': {
if (PRECISE_I64_MATH) {
- return i64PreciseOp('multiply');
+ return preciseCall('___muldi3');
} else {
warnI64_1();
return finish(splitI64(mergeI64(idents[0], op[0] === 'u') + '*' + mergeI64(idents[1], op[0] === 'u'), true));
@@ -2086,7 +2089,7 @@ function processMathop(item) {
}
case 'urem': case 'srem': {
if (PRECISE_I64_MATH) {
- return i64PreciseOp('modulo', op[0] === 'u');
+ return preciseCall(op[0] === 'u' ? '___uremdi3' : '___remdi3');
} else {
warnI64_1();
return finish(splitI64(mergeI64(idents[0], op[0] === 'u') + '%' + mergeI64(idents[1], op[0] === 'u'), true));