Skip to content

Commit

Permalink
refactor(compiler): Modernize various compiler-internal helpers (#1341)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGraey authored Jun 16, 2020
1 parent 4ed8acb commit f81250a
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 141 deletions.
4 changes: 2 additions & 2 deletions src/glue/binaryen.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ module.exports = binaryen;

const { Module } = require("../module");

Module.prototype.toText = function() {
Module.prototype.toText = function toText() {
// NOTE: Conversion to StackIR can yield conversion artifacts like sequences
// of unreachable statements not actually emitted by the compiler. Optimizing
// StackIR removes these again, but may also suppress useless code emitted by
// the compiler that's then no longer visible in tests. Both not ideal.
return binaryen.wrapModule(this.ref).emitStackIR(/* optimize-stack-ir */ true);
};

Module.prototype.toAsmjs = function() {
Module.prototype.toAsmjs = function toAsmjs() {
return binaryen.wrapModule(this.ref).emitAsmjs();
};
6 changes: 3 additions & 3 deletions src/glue/js/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* @license Apache-2.0
*/

global.Map_keys = function(map) {
global.Map_keys = function Map_keys(map) {
return Array.from(map.keys());
};

global.Map_values = function(map) {
global.Map_values = function Map_values(map) {
return Array.from(map.values());
};

global.Set_values = function(set) {
global.Set_values = function Set_values(set) {
return Array.from(set.values());
};
8 changes: 4 additions & 4 deletions src/glue/js/float.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ const F64 = new Float64Array(1);
const F32 = new Float32Array(F64.buffer);
const I32 = new Int32Array(F64.buffer);

global.f32_as_i32 = function(value) {
global.f32_as_i32 = function f32_as_i32(value) {
F32[0] = value;
return I32[0];
};

global.i32_as_f32 = function(value) {
global.i32_as_f32 = function i32_as_f32(value) {
I32[0] = value;
return F32[0];
};

global.f64_as_i64 = function(value) {
global.f64_as_i64 = function f64_as_i64(value) {
F64[0] = value;
return i64_new(I32[0], I32[1]);
};

global.i64_as_f64 = function(value) {
global.i64_as_f64 = function i64_as_f64(value) {
I32[0] = i64_low(value);
I32[1] = i64_high(value);
return F64[0];
Expand Down
76 changes: 38 additions & 38 deletions src/glue/js/i64.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ global.i64_zero = Long.ZERO;
global.i64_one = Long.ONE;
global.i64_neg_one = Long.fromInt(-1);

global.i64_new = function(lo, hi) {
global.i64_new = function i64_new(lo, hi) {
return Long.fromBits(lo, hi);
};

global.i64_low = function(value) {
global.i64_low = function i64_low(value) {
return value.low;
};

global.i64_high = function(value) {
global.i64_high = function i64_high(value) {
return value.high;
};

global.i64_add = function(left, right) {
global.i64_add = function i64_add(left, right) {
return left.add(right);
};

global.i64_sub = function(left, right) {
global.i64_sub = function i64_sub(left, right) {
return left.sub(right);
};

global.i64_mul = function(left, right) {
global.i64_mul = function i64_mul(left, right) {
return left.mul(right);
};

global.i64_pow = function(left, right) {
global.i64_pow = function i64_pow(left, right) {
var rightLo = right.low;
var rightHi = right.high;
if (rightHi <= 0) {
Expand All @@ -60,121 +60,121 @@ global.i64_pow = function(left, right) {
return result;
};

global.i64_div = function(left, right) {
global.i64_div = function i64_div(left, right) {
return left.div(right);
};

global.i64_div_u = function(left, right) {
global.i64_div_u = function i64_div_u(left, right) {
return left.toUnsigned().div(right.toUnsigned()).toSigned();
};

global.i64_rem = function(left, right) {
global.i64_rem = function i64_rem(left, right) {
return left.mod(right);
};

global.i64_rem_u = function(left, right) {
global.i64_rem_u = function i64_rem_u(left, right) {
return left.toUnsigned().mod(right.toUnsigned()).toSigned();
};

global.i64_and = function(left, right) {
global.i64_and = function i64_and(left, right) {
return left.and(right);
};

global.i64_or = function(left, right) {
global.i64_or = function i64_or(left, right) {
return left.or(right);
};

global.i64_xor = function(left, right) {
global.i64_xor = function i64_xor(left, right) {
return left.xor(right);
};

global.i64_shl = function(left, right) {
global.i64_shl = function i64_shl(left, right) {
return left.shl(right);
};

global.i64_shr = function(left, right) {
global.i64_shr = function i64_shr(left, right) {
return left.shr(right);
};

global.i64_shr_u = function(left, right) {
global.i64_shr_u = function i64_shr_u(left, right) {
return left.shru(right);
};

global.i64_not = function(value) {
global.i64_not = function i64_not(value) {
return value.not();
};

global.i64_eq = function(left, right) {
global.i64_eq = function i64_eq(left, right) {
return left.eq(right);
};

global.i64_ne = function(left, right) {
global.i64_ne = function i64_ne(left, right) {
return left.ne(right);
};

global.i64_gt = function(left, right) {
global.i64_gt = function i64_gt(left, right) {
return left.gt(right);
};

global.i64_align = function(value, alignment) {
global.i64_align = function i64_align(value, alignment) {
assert(alignment && (alignment & (alignment - 1)) == 0);
var mask = Long.fromInt(alignment - 1);
return value.add(mask).and(mask.not());
};

global.i64_is_i8 = function(value) {
global.i64_is_i8 = function i64_is_i8(value) {
return value.high === 0 && (value.low >= 0 && value.low <= i8.MAX_VALUE)
|| value.high === -1 && (value.low >= i8.MIN_VALUE && value.low < 0);
};

global.i64_is_i16 = function(value) {
global.i64_is_i16 = function i64_is_i16(value) {
return value.high === 0 && (value.low >= 0 && value.low <= i16.MAX_VALUE)
|| value.high === -1 && (value.low >= i16.MIN_VALUE && value.low < 0);
};

global.i64_is_i32 = function(value) {
global.i64_is_i32 = function i64_is_i32(value) {
return (value.high === 0 && value.low >= 0)
|| (value.high === -1 && value.low < 0);
};

global.i64_is_u8 = function(value) {
return value.high === 0 && value.low >= 0 && value.low <= u8.MAX_VALUE;
global.i64_is_u8 = function i64_is_u8(value) {
return value.high === 0 && (value.low >>> 0) <= u8.MAX_VALUE;
};

global.i64_is_u16 = function(value) {
return value.high === 0 && value.low >= 0 && value.low <= u16.MAX_VALUE;
global.i64_is_u16 = function i64_is_u16(value) {
return value.high === 0 && (value.low >>> 0) <= u16.MAX_VALUE;
};

global.i64_is_u32 = function(value) {
global.i64_is_u32 = function i64_is_u32(value) {
return value.high === 0;
};

global.i64_is_bool = function(value) {
return value.high === 0 && (value.low === 0 || value.low === 1);
global.i64_is_bool = function i64_is_bool(value) {
return (value.high | (value.low & ~1)) === 0;
};

const minSafeF32 = Long.fromNumber(f32.MIN_SAFE_INTEGER);
const maxSafeF32 = Long.fromNumber(f32.MAX_SAFE_INTEGER);

global.i64_is_f32 = function(value) {
global.i64_is_f32 = function i64_is_f32(value) {
return value.gte(minSafeF32) && value.lte(maxSafeF32);
};

const minSafeF64 = Long.fromNumber(f64.MIN_SAFE_INTEGER);
const maxSafeF64 = Long.fromNumber(f64.MAX_SAFE_INTEGER);

global.i64_is_f64 = function(value) {
global.i64_is_f64 = function i64_is_f64(value) {
return value.gte(minSafeF64) && value.lte(maxSafeF64);
};

global.i64_to_f32 = function(value) {
global.i64_to_f32 = function i64_to_f32(value) {
return global.Math.fround(value.toNumber());
};

global.i64_to_f64 = function(value) {
global.i64_to_f64 = function i64_to_f64(value) {
return value.toNumber();
};

global.i64_to_string = function(value, unsigned) {
return (unsigned ? value.toUnsigned() : value).toString();
global.i64_to_string = function i64_to_string(value, unsigned) {
return unsigned ? value.toUnsigned().toString() : value.toString();
};
6 changes: 3 additions & 3 deletions src/glue/wasm/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
/* eslint-disable @typescript-eslint/no-unused-vars */

// @ts-ignore: decorator
@global
@global @inline
function Map_keys<K,V>(map: Map<K,V>): K[] {
return map.keys(); // preliminary
}

// @ts-ignore: decorator
@global
@global @inline
function Map_values<K,V>(map: Map<K,V>): V[] {
return map.values(); // preliminary
}

// @ts-ignore: decorator
@global
@global @inline
function Set_values<V>(set: Set<V>): V[] {
return set.values(); // preliminary
}
8 changes: 4 additions & 4 deletions src/glue/wasm/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
/* eslint-disable @typescript-eslint/no-unused-vars */

// @ts-ignore: decorator
@global
@global @inline
function f32_as_i32(value: f32): i32 {
return reinterpret<i32>(value);
}

// @ts-ignore: decorator
@global
@global @inline
function i32_as_f32(value: i32): f32 {
return reinterpret<f32>(value);
}

// @ts-ignore: decorator
@global
@global @inline
function f64_as_i64(value: f64): i64 {
return reinterpret<i64>(value);
}

// @ts-ignore: decorator
@global
@global @inline
function i64_as_f64(value: i64): f64 {
return reinterpret<f64>(value);
}
Loading

0 comments on commit f81250a

Please sign in to comment.