assemblyscript/src/module.ts

1558 lines
42 KiB
TypeScript
Raw Normal View History

2018-03-17 23:41:48 +01:00
/**
2018-03-19 01:12:18 +01:00
* A thin wrapper around Binaryen's C-API.
* @module module
*//***/
2018-03-17 23:41:48 +01:00
2017-12-24 03:19:47 +01:00
import {
Target
} from "./compiler";
export type ModuleRef = usize;
export type FunctionTypeRef = usize;
export type FunctionRef = usize;
export type ExpressionRef = usize;
export type GlobalRef = usize;
export type ImportRef = usize;
export type ExportRef = usize;
export type Index = u32;
2017-11-26 04:03:28 +01:00
export enum NativeType {
None = _BinaryenTypeNone(),
I32 = _BinaryenTypeInt32(),
I64 = _BinaryenTypeInt64(),
F32 = _BinaryenTypeFloat32(),
F64 = _BinaryenTypeFloat64(),
Unreachable = _BinaryenTypeUnreachable(),
Auto = _BinaryenTypeAuto()
2017-09-28 13:08:25 +02:00
}
2017-11-17 14:33:51 +01:00
export enum ExpressionId {
Invalid = _BinaryenInvalidId(),
Block = _BinaryenBlockId(),
If = _BinaryenIfId(),
Loop = _BinaryenLoopId(),
Break = _BinaryenBreakId(),
Switch = _BinaryenSwitchId(),
Call = _BinaryenCallId(),
CallImport = _BinaryenCallImportId(),
CallIndirect = _BinaryenCallIndirectId(),
GetLocal = _BinaryenGetLocalId(),
SetLocal = _BinaryenSetLocalId(),
GetGlobal = _BinaryenGetGlobalId(),
SetGlobal = _BinaryenSetGlobalId(),
Load = _BinaryenLoadId(),
Store = _BinaryenStoreId(),
Const = _BinaryenConstId(),
Unary = _BinaryenUnaryId(),
Binary = _BinaryenBinaryId(),
Select = _BinaryenSelectId(),
Drop = _BinaryenDropId(),
Return = _BinaryenReturnId(),
Host = _BinaryenHostId(),
Nop = _BinaryenNopId(),
Unreachable = _BinaryenUnreachableId(),
AtomicCmpxchg = _BinaryenAtomicCmpxchgId(),
AtomicRMW = _BinaryenAtomicRMWId(),
AtomicWait = _BinaryenAtomicWaitId(),
AtomicWake = _BinaryenAtomicWakeId()
}
2017-09-28 13:08:25 +02:00
export enum UnaryOp {
ClzI32 = _BinaryenClzInt32(),
CtzI32 = _BinaryenCtzInt32(),
PopcntI32 = _BinaryenPopcntInt32(),
NegF32 = _BinaryenNegFloat32(),
AbsF32 = _BinaryenAbsFloat32(),
CeilF32 = _BinaryenCeilFloat32(),
FloorF32 = _BinaryenFloorFloat32(),
TruncF32 = _BinaryenTruncFloat32(),
NearestF32 = _BinaryenNearestFloat32(),
SqrtF32 = _BinaryenSqrtFloat32(),
EqzI32 = _BinaryenEqZInt32(),
ClzI64 = _BinaryenClzInt64(),
CtzI64 = _BinaryenCtzInt64(),
PopcntI64 = _BinaryenPopcntInt64(),
NegF64 = _BinaryenNegFloat64(),
AbsF64 = _BinaryenAbsFloat64(),
CeilF64 = _BinaryenCeilFloat64(),
FloorF64 = _BinaryenFloorFloat64(),
TruncF64 = _BinaryenTruncFloat64(),
NearestF64 = _BinaryenNearestFloat64(),
SqrtF64 = _BinaryenSqrtFloat64(),
EqzI64 = _BinaryenEqZInt64(),
ExtendI32 = _BinaryenExtendSInt32(),
ExtendU32 = _BinaryenExtendUInt32(),
WrapI64 = _BinaryenWrapInt64(),
TruncF32ToI32 = _BinaryenTruncSFloat32ToInt32(),
TruncF32ToI64 = _BinaryenTruncSFloat32ToInt64(),
TruncF32ToU32 = _BinaryenTruncUFloat32ToInt32(),
TruncF32ToU64 = _BinaryenTruncUFloat32ToInt64(),
TruncF64ToI32 = _BinaryenTruncSFloat64ToInt32(),
TruncF64ToI64 = _BinaryenTruncSFloat64ToInt64(),
TruncF64ToU32 = _BinaryenTruncUFloat64ToInt32(),
TruncF64ToU64 = _BinaryenTruncUFloat64ToInt64(),
2017-09-28 13:08:25 +02:00
ReinterpretF32 = _BinaryenReinterpretFloat32(),
ReinterpretF64 = _BinaryenReinterpretFloat64(),
ConvertI32ToF32 = _BinaryenConvertSInt32ToFloat32(),
ConvertI32ToF64 = _BinaryenConvertSInt32ToFloat64(),
ConvertU32ToF32 = _BinaryenConvertUInt32ToFloat32(),
ConvertU32ToF64 = _BinaryenConvertUInt32ToFloat64(),
ConvertI64ToF32 = _BinaryenConvertSInt64ToFloat32(),
ConvertI64ToF64 = _BinaryenConvertSInt64ToFloat64(),
ConvertU64ToF32 = _BinaryenConvertUInt64ToFloat32(),
ConvertU64ToF64 = _BinaryenConvertUInt64ToFloat64(),
2017-09-28 13:08:25 +02:00
PromoteF32 = _BinaryenPromoteFloat32(),
DemoteF64 = _BinaryenDemoteFloat64(),
ReinterpretI32 = _BinaryenReinterpretInt32(),
ReinterpretI64 = _BinaryenReinterpretInt64(),
// see: https://github.com/WebAssembly/sign-extension-ops
ExtendI8ToI32 = _BinaryenExtendS8Int32(),
ExtendI16ToI32 = _BinaryenExtendS16Int32(),
ExtendI8ToI64 = _BinaryenExtendS8Int64(),
ExtendI16ToI64 = _BinaryenExtendS16Int64(),
ExtendI32ToI64 = _BinaryenExtendS32Int64()
2018-02-25 00:13:39 +01:00
// see: https://github.com/WebAssembly/nontrapping-float-to-int-conversions
// TruncF32ToI32Sat
// TruncF32ToU32Sat
// TruncF64ToI32Sat
// TruncF64ToU32Sat
// TruncF32ToI64Sat
// TruncF32ToU64Sat
// TruncF64ToI64Sat
// TruncF64ToU64Sat
2017-09-28 13:08:25 +02:00
}
export enum BinaryOp {
AddI32 = _BinaryenAddInt32(),
SubI32 = _BinaryenSubInt32(),
MulI32 = _BinaryenMulInt32(),
DivI32 = _BinaryenDivSInt32(),
DivU32 = _BinaryenDivUInt32(),
RemI32 = _BinaryenRemSInt32(),
RemU32 = _BinaryenRemUInt32(),
AndI32 = _BinaryenAndInt32(),
OrI32 = _BinaryenOrInt32(),
XorI32 = _BinaryenXorInt32(),
ShlI32 = _BinaryenShlInt32(),
ShrU32 = _BinaryenShrUInt32(),
ShrI32 = _BinaryenShrSInt32(),
RotlI32 = _BinaryenRotLInt32(),
RotrI32 = _BinaryenRotRInt32(),
EqI32 = _BinaryenEqInt32(),
NeI32 = _BinaryenNeInt32(),
LtI32 = _BinaryenLtSInt32(),
LtU32 = _BinaryenLtUInt32(),
LeI32 = _BinaryenLeSInt32(),
LeU32 = _BinaryenLeUInt32(),
GtI32 = _BinaryenGtSInt32(),
GtU32 = _BinaryenGtUInt32(),
GeI32 = _BinaryenGeSInt32(),
GeU32 = _BinaryenGeUInt32(),
AddI64 = _BinaryenAddInt64(),
SubI64 = _BinaryenSubInt64(),
MulI64 = _BinaryenMulInt64(),
DivI64 = _BinaryenDivSInt64(),
DivU64 = _BinaryenDivUInt64(),
RemI64 = _BinaryenRemSInt64(),
RemU64 = _BinaryenRemUInt64(),
AndI64 = _BinaryenAndInt64(),
OrI64 = _BinaryenOrInt64(),
XorI64 = _BinaryenXorInt64(),
ShlI64 = _BinaryenShlInt64(),
ShrU64 = _BinaryenShrUInt64(),
ShrI64 = _BinaryenShrSInt64(),
RotlI64 = _BinaryenRotLInt64(),
RotrI64 = _BinaryenRotRInt64(),
EqI64 = _BinaryenEqInt64(),
NeI64 = _BinaryenNeInt64(),
LtI64 = _BinaryenLtSInt64(),
LtU64 = _BinaryenLtUInt64(),
LeI64 = _BinaryenLeSInt64(),
LeU64 = _BinaryenLeUInt64(),
GtI64 = _BinaryenGtSInt64(),
GtU64 = _BinaryenGtUInt64(),
GeI64 = _BinaryenGeSInt64(),
GeU64 = _BinaryenGeUInt64(),
AddF32 = _BinaryenAddFloat32(),
SubF32 = _BinaryenSubFloat32(),
MulF32 = _BinaryenMulFloat32(),
DivF32 = _BinaryenDivFloat32(),
CopysignF32 = _BinaryenCopySignFloat32(),
MinF32 = _BinaryenMinFloat32(),
MaxF32 = _BinaryenMaxFloat32(),
EqF32 = _BinaryenEqFloat32(),
NeF32 = _BinaryenNeFloat32(),
LtF32 = _BinaryenLtFloat32(),
LeF32 = _BinaryenLeFloat32(),
GtF32 = _BinaryenGtFloat32(),
GeF32 = _BinaryenGeFloat32(),
AddF64 = _BinaryenAddFloat64(),
SubF64 = _BinaryenSubFloat64(),
MulF64 = _BinaryenMulFloat64(),
DivF64 = _BinaryenDivFloat64(),
CopysignF64 = _BinaryenCopySignFloat64(),
MinF64 = _BinaryenMinFloat64(),
MaxF64 = _BinaryenMaxFloat64(),
EqF64 = _BinaryenEqFloat64(),
NeF64 = _BinaryenNeFloat64(),
LtF64 = _BinaryenLtFloat64(),
LeF64 = _BinaryenLeFloat64(),
GtF64 = _BinaryenGtFloat64(),
GeF64 = _BinaryenGeFloat64()
}
export enum HostOp {
PageSize = _BinaryenPageSize(),
CurrentMemory = _BinaryenCurrentMemory(),
GrowMemory = _BinaryenGrowMemory(),
HasFeature = _BinaryenHasFeature(),
2018-02-25 00:13:39 +01:00
// see: https://github.com/WebAssembly/bulk-memory-operations
// MoveMemory
// SetMemory
2017-09-28 13:08:25 +02:00
}
2017-11-17 14:33:51 +01:00
export enum AtomicRMWOp {
Add = _BinaryenAtomicRMWAdd(),
Sub = _BinaryenAtomicRMWSub(),
And = _BinaryenAtomicRMWAnd(),
Or = _BinaryenAtomicRMWOr(),
Xor = _BinaryenAtomicRMWXor(),
Xchg = _BinaryenAtomicRMWXchg()
2017-09-28 13:08:25 +02:00
}
export class MemorySegment {
buffer: Uint8Array;
offset: I64;
2017-09-28 13:08:25 +02:00
2018-03-13 14:03:57 +01:00
static create(buffer: Uint8Array, offset: I64): MemorySegment {
var segment = new MemorySegment();
2017-09-28 13:08:25 +02:00
segment.buffer = buffer;
segment.offset = offset;
return segment;
}
}
export class Module {
2017-11-26 04:03:28 +01:00
ref: ModuleRef;
private cachedByValue: usize;
2017-10-07 14:29:43 +02:00
2018-03-19 01:12:18 +01:00
/** Maximum number of pages when targeting WASM32. */
static readonly MAX_MEMORY_WASM32: Index = 0xffff;
2018-03-19 01:12:18 +01:00
/** Maximum number of pages when targeting WASM64. */
static readonly MAX_MEMORY_WASM64: Index = 0xffff; // TODO
2017-09-28 13:08:25 +02:00
static create(): Module {
var module = new Module();
2017-09-28 13:08:25 +02:00
module.ref = _BinaryenModuleCreate();
module.cachedByValue = allocate_memory(16);
2017-09-28 13:08:25 +02:00
return module;
}
static createFrom(buffer: Uint8Array): Module {
var cArr = allocU8Array(buffer);
2017-09-28 13:08:25 +02:00
try {
let module = new Module();
2017-09-28 13:08:25 +02:00
module.ref = _BinaryenModuleRead(cArr, buffer.length);
module.cachedByValue = allocate_memory(3 * 8); // LLVM C-ABI, max used is 3 * usize
return module;
2017-09-28 13:08:25 +02:00
} finally {
free_memory(changetype<usize>(cArr));
2017-09-28 13:08:25 +02:00
}
}
2017-10-07 14:29:43 +02:00
private constructor() { }
2017-09-28 13:08:25 +02:00
// types
2018-02-25 00:13:39 +01:00
addFunctionType(
name: string,
result: NativeType,
paramTypes: NativeType[] | null
2018-02-25 00:13:39 +01:00
): FunctionRef {
var cStr = allocString(name);
var cArr = allocI32Array(paramTypes);
2017-09-28 13:08:25 +02:00
try {
return _BinaryenAddFunctionType(this.ref, cStr, result, cArr, paramTypes ? paramTypes.length : 0);
2017-09-28 13:08:25 +02:00
} finally {
free_memory(cArr);
free_memory(cStr);
2017-09-28 13:08:25 +02:00
}
}
2018-02-25 00:13:39 +01:00
getFunctionTypeBySignature(
result: NativeType,
paramTypes: NativeType[] | null
2018-02-25 00:13:39 +01:00
): FunctionTypeRef {
var cArr = allocI32Array(paramTypes);
2017-09-28 13:08:25 +02:00
try {
return _BinaryenGetFunctionTypeBySignature(this.ref, result, cArr, paramTypes ? paramTypes.length : 0);
2017-09-28 13:08:25 +02:00
} finally {
free_memory(cArr);
2017-09-28 13:08:25 +02:00
}
}
removeFunctionType(name: string): void {
var cStr = allocString(name);
try {
_BinaryenRemoveFunctionType(this.ref, cStr);
} finally {
free_memory(cStr);
}
}
2018-02-25 00:13:39 +01:00
// constants
2017-09-28 13:08:25 +02:00
2017-11-26 04:03:28 +01:00
createI32(value: i32): ExpressionRef {
var out = this.cachedByValue;
_BinaryenLiteralInt32(out, value);
return _BinaryenConst(this.ref, out);
2017-09-28 13:08:25 +02:00
}
2018-02-25 00:13:39 +01:00
createI64(valueLow: i32, valueHigh: i32 = 0): ExpressionRef {
var out = this.cachedByValue;
2018-02-25 00:13:39 +01:00
_BinaryenLiteralInt64(out, valueLow, valueHigh);
return _BinaryenConst(this.ref, out);
2017-09-28 13:08:25 +02:00
}
2017-11-26 04:03:28 +01:00
createF32(value: f32): ExpressionRef {
var out = this.cachedByValue;
_BinaryenLiteralFloat32(out, value);
return _BinaryenConst(this.ref, out);
2017-09-28 13:08:25 +02:00
}
2017-11-26 04:03:28 +01:00
createF64(value: f64): ExpressionRef {
var out = this.cachedByValue;
_BinaryenLiteralFloat64(out, value);
return _BinaryenConst(this.ref, out);
2017-09-28 13:08:25 +02:00
}
2018-02-25 00:13:39 +01:00
// expressions
createUnary(
op: UnaryOp,
expr: ExpressionRef
): ExpressionRef {
2017-09-28 13:08:25 +02:00
return _BinaryenUnary(this.ref, op, expr);
}
2018-02-25 00:13:39 +01:00
createBinary(
op: BinaryOp,
left: ExpressionRef,
right: ExpressionRef
): ExpressionRef {
2017-09-28 13:08:25 +02:00
return _BinaryenBinary(this.ref, op, left, right);
}
2018-02-25 00:13:39 +01:00
createHost(
op: HostOp,
name: string | null = null,
operands: ExpressionRef[] | null = null
): ExpressionRef {
var cStr = allocString(name);
var cArr = allocPtrArray(operands);
2017-09-28 13:08:25 +02:00
try {
2017-11-26 04:03:28 +01:00
return _BinaryenHost(this.ref, op, cStr, cArr, operands ? (<ExpressionRef[]>operands).length : 0);
2017-09-28 13:08:25 +02:00
} finally {
free_memory(cArr);
free_memory(cStr);
2017-09-28 13:08:25 +02:00
}
}
2018-02-25 00:13:39 +01:00
createGetLocal(
index: i32,
type: NativeType
): ExpressionRef {
2017-09-28 13:08:25 +02:00
return _BinaryenGetLocal(this.ref, index, type);
}
2018-02-25 00:13:39 +01:00
createTeeLocal(
index: i32,
value: ExpressionRef
): ExpressionRef {
2017-09-28 13:08:25 +02:00
return _BinaryenTeeLocal(this.ref, index, value);
}
2018-02-25 00:13:39 +01:00
createGetGlobal(
name: string,
type: NativeType
): ExpressionRef {
var cStr = allocString(name);
2017-09-28 13:08:25 +02:00
try {
return _BinaryenGetGlobal(this.ref, cStr, type);
} finally {
free_memory(cStr);
2017-09-28 13:08:25 +02:00
}
}
2018-02-25 00:13:39 +01:00
createLoad(
bytes: Index,
signed: bool,
ptr: ExpressionRef,
type: NativeType,
offset: Index = 0
): ExpressionRef {
2017-11-26 04:03:28 +01:00
return _BinaryenLoad(this.ref, bytes, signed ? 1 : 0, offset, /* always aligned */ bytes, type, ptr);
}
2018-02-25 00:13:39 +01:00
createStore(
bytes: Index,
ptr: ExpressionRef,
value: ExpressionRef,
type: NativeType,
offset: Index = 0
): ExpressionRef {
2017-11-26 04:03:28 +01:00
return _BinaryenStore(this.ref, bytes, offset, /* always aligned */ bytes, ptr, value, type);
}
2018-02-25 00:13:39 +01:00
createAtomicLoad(
bytes: Index,
ptr: ExpressionRef,
type: NativeType,
offset: Index = 0
): ExpressionRef {
2017-11-26 04:03:28 +01:00
return _BinaryenAtomicLoad(this.ref, bytes, offset, type, ptr);
}
2018-02-25 00:13:39 +01:00
createAtomicStore(
bytes: Index,
ptr: ExpressionRef,
value: ExpressionRef,
type: NativeType,
offset: Index = 0
): ExpressionRef {
2017-11-26 04:03:28 +01:00
return _BinaryenAtomicStore(this.ref, bytes, offset, ptr, value, type);
}
2018-02-25 00:13:39 +01:00
createAtomicRMW(
op: AtomicRMWOp,
bytes: Index,
offset: Index,
ptr: ExpressionRef,
value: ExpressionRef,
type: NativeType
): ExpressionRef {
2017-11-17 14:33:51 +01:00
return _BinaryenAtomicRMW(this.ref, op, bytes, offset, ptr, value, type);
}
2018-02-25 00:13:39 +01:00
createAtomicCmpxchg(
bytes: Index,
offset: Index,
ptr: ExpressionRef,
expected: ExpressionRef,
replacement: ExpressionRef,
type: NativeType
): ExpressionRef {
2017-11-17 14:33:51 +01:00
return _BinaryenAtomicCmpxchg(this.ref, bytes, offset, ptr, expected, replacement, type);
}
2018-02-25 00:13:39 +01:00
createAtomicWait(
ptr: ExpressionRef,
expected: ExpressionRef,
timeout: ExpressionRef,
expectedType: NativeType
): ExpressionRef {
2017-11-17 14:33:51 +01:00
return _BinaryenAtomicWait(this.ref, ptr, expected, timeout, expectedType);
}
2018-02-25 00:13:39 +01:00
createAtomicWake(
ptr: ExpressionRef,
wakeCount: ExpressionRef
): ExpressionRef {
2017-11-17 14:33:51 +01:00
return _BinaryenAtomicWake(this.ref, ptr, wakeCount);
}
2017-09-28 13:08:25 +02:00
// statements
2018-02-25 00:13:39 +01:00
createSetLocal(
index: Index,
value: ExpressionRef
): ExpressionRef {
2017-09-28 13:08:25 +02:00
return _BinaryenSetLocal(this.ref, index, value);
}
2018-02-25 00:13:39 +01:00
createSetGlobal(
name: string,
value: ExpressionRef
): ExpressionRef {
var cStr = allocString(name);
2017-09-28 13:08:25 +02:00
try {
return _BinaryenSetGlobal(this.ref, cStr, value);
} finally {
free_memory(cStr);
2017-09-28 13:08:25 +02:00
}
}
2018-02-25 00:13:39 +01:00
createBlock(
label: string | null,
children: ExpressionRef[],
type: NativeType = NativeType.None
): ExpressionRef {
var cStr = allocString(label);
var cArr = allocPtrArray(children);
2017-09-28 13:08:25 +02:00
try {
return _BinaryenBlock(this.ref, cStr, cArr, children.length, type);
} finally {
free_memory(cArr);
free_memory(cStr);
2017-09-28 13:08:25 +02:00
}
}
2018-02-25 00:13:39 +01:00
createBreak(
label: string | null,
condition: ExpressionRef = 0,
value: ExpressionRef = 0
): ExpressionRef {
var cStr = allocString(label);
2017-09-28 13:08:25 +02:00
try {
return _BinaryenBreak(this.ref, cStr, condition, value);
} finally {
free_memory(cStr);
2017-09-28 13:08:25 +02:00
}
}
2018-02-25 00:13:39 +01:00
createDrop(
expression: ExpressionRef
): ExpressionRef {
2017-09-28 13:08:25 +02:00
return _BinaryenDrop(this.ref, expression);
}
2018-02-25 00:13:39 +01:00
createLoop(
label: string | null,
body: ExpressionRef
): ExpressionRef {
var cStr = allocString(label);
2017-09-28 13:08:25 +02:00
try {
return _BinaryenLoop(this.ref, cStr, body);
} finally {
free_memory(cStr);
2017-09-28 13:08:25 +02:00
}
}
2018-02-25 00:13:39 +01:00
createIf(
condition: ExpressionRef,
ifTrue: ExpressionRef,
ifFalse: ExpressionRef = 0
): ExpressionRef {
2017-09-28 13:08:25 +02:00
return _BinaryenIf(this.ref, condition, ifTrue, ifFalse);
}
2017-11-26 04:03:28 +01:00
createNop(): ExpressionRef {
2017-09-28 13:08:25 +02:00
return _BinaryenNop(this.ref);
}
2018-02-25 00:13:39 +01:00
createReturn(
expression: ExpressionRef = 0
): ExpressionRef {
2017-09-28 13:08:25 +02:00
return _BinaryenReturn(this.ref, expression);
}
2018-02-25 00:13:39 +01:00
createSelect(
ifTrue: ExpressionRef,
ifFalse: ExpressionRef,
condition: ExpressionRef
): ExpressionRef {
2017-09-28 13:08:25 +02:00
return _BinaryenSelect(this.ref, condition, ifTrue, ifFalse);
}
2018-02-25 00:13:39 +01:00
createSwitch(
names: string[],
defaultName: string | null,
condition: ExpressionRef,
value: ExpressionRef = 0
): ExpressionRef {
var numNames = names.length;
var strs = new Array<usize>(numNames);
for (let i = 0; i < numNames; ++i) {
strs[i] = allocString(names[i]);
2018-02-25 00:13:39 +01:00
}
var cArr = allocI32Array(strs);
var cStr = allocString(defaultName);
2017-09-28 13:08:25 +02:00
try {
return _BinaryenSwitch(this.ref, cArr, numNames, cStr, condition, value);
2017-09-28 13:08:25 +02:00
} finally {
free_memory(cStr);
free_memory(cArr);
for (let i = numNames - 1; i >= 0; --i) free_memory(strs[i]);
2017-09-28 13:08:25 +02:00
}
}
2018-02-25 00:13:39 +01:00
createCall(
target: string,
operands: ExpressionRef[] | null,
returnType: NativeType
): ExpressionRef {
var cStr = allocString(target);
var cArr = allocPtrArray(operands);
2017-09-28 13:08:25 +02:00
try {
return _BinaryenCall(this.ref, cStr, cArr, operands && operands.length || 0, returnType);
2017-09-28 13:08:25 +02:00
} finally {
free_memory(cArr);
free_memory(cStr);
2017-09-28 13:08:25 +02:00
}
}
2018-02-25 00:13:39 +01:00
createCallImport(
target: string,
operands: ExpressionRef[] | null,
returnType: NativeType
): ExpressionRef {
var cStr = allocString(target);
var cArr = allocPtrArray(operands);
2017-09-28 13:08:25 +02:00
try {
return _BinaryenCallImport(this.ref, cStr, cArr, operands && operands.length || 0, returnType);
2017-09-28 13:08:25 +02:00
} finally {
free_memory(cArr);
free_memory(cStr);
2017-09-28 13:08:25 +02:00
}
}
createCallIndirect(
index: ExpressionRef,
operands: ExpressionRef[] | null,
typeName: string
): ExpressionRef {
var cArr = allocPtrArray(operands);
var cStr = allocString(typeName);
try {
return _BinaryenCallIndirect(this.ref, index, cArr, operands && operands.length || 0, cStr);
} finally {
free_memory(cStr);
free_memory(cArr);
}
}
2017-11-26 04:03:28 +01:00
createUnreachable(): ExpressionRef {
2017-09-28 13:08:25 +02:00
return _BinaryenUnreachable(this.ref);
}
// meta
2018-02-25 00:13:39 +01:00
addGlobal(
name: string,
type: NativeType,
mutable: bool,
initializer: ExpressionRef
): GlobalRef {
var cStr = allocString(name);
2017-09-28 13:08:25 +02:00
try {
return _BinaryenAddGlobal(this.ref, cStr, type, mutable ? 1 : 0, initializer);
} finally {
free_memory(cStr);
2017-09-28 13:08:25 +02:00
}
}
2018-02-25 00:13:39 +01:00
addFunction(
name: string,
type: FunctionTypeRef,
varTypes: NativeType[] | null,
2018-02-25 00:13:39 +01:00
body: ExpressionRef
): FunctionRef {
var cStr = allocString(name);
var cArr = allocI32Array(varTypes);
2017-09-28 13:08:25 +02:00
try {
return _BinaryenAddFunction(this.ref, cStr, type, cArr, varTypes ? varTypes.length : 0, body);
2017-09-28 13:08:25 +02:00
} finally {
free_memory(cArr);
free_memory(cStr);
2017-09-28 13:08:25 +02:00
}
}
removeFunction(name: string): void {
var cStr = allocString(name);
try {
_BinaryenRemoveFunction(this.ref, cStr);
} finally {
free_memory(cStr);
}
}
private cachedTemporaryName: usize = 0;
private hasTemporaryFunction: bool = false;
addTemporaryFunction(result: NativeType, paramTypes: NativeType[] | null, body: ExpressionRef): FunctionRef {
this.hasTemporaryFunction = assert(!this.hasTemporaryFunction);
var tempName = this.cachedTemporaryName;
if (!tempName) this.cachedTemporaryName = tempName = allocString(""); // works because strings are interned
var cArr = allocI32Array(paramTypes);
try {
let typeRef = _BinaryenAddFunctionType(this.ref, tempName, result, cArr, paramTypes ? paramTypes.length : 0);
return _BinaryenAddFunction(this.ref, tempName, typeRef, 0, 0, body);
} finally {
free_memory(cArr);
}
}
removeTemporaryFunction(): void {
this.hasTemporaryFunction = !assert(this.hasTemporaryFunction);
var tempName = assert(this.cachedTemporaryName);
_BinaryenRemoveFunction(this.ref, tempName);
_BinaryenRemoveFunctionType(this.ref, tempName);
}
2018-02-25 00:13:39 +01:00
addFunctionExport(
internalName: string,
externalName: string
): ExportRef {
var cStr1 = allocString(internalName);
var cStr2 = allocString(externalName);
2017-09-28 13:08:25 +02:00
try {
2017-12-05 15:28:01 +01:00
return _BinaryenAddFunctionExport(this.ref, cStr1, cStr2);
} finally {
free_memory(cStr2);
free_memory(cStr1);
2017-12-05 15:28:01 +01:00
}
}
2018-02-25 00:13:39 +01:00
addTableExport(
internalName: string,
externalName: string
): ExportRef {
var cStr1 = allocString(internalName);
var cStr2 = allocString(externalName);
2017-12-05 15:28:01 +01:00
try {
return _BinaryenAddTableExport(this.ref, cStr1, cStr2);
} finally {
free_memory(cStr2);
free_memory(cStr1);
2017-12-05 15:28:01 +01:00
}
}
2018-02-25 00:13:39 +01:00
addMemoryExport(
internalName: string,
externalName: string
): ExportRef {
var cStr1 = allocString(internalName);
var cStr2 = allocString(externalName);
2017-12-05 15:28:01 +01:00
try {
return _BinaryenAddMemoryExport(this.ref, cStr1, cStr2);
} finally {
free_memory(cStr2);
free_memory(cStr1);
2017-12-05 15:28:01 +01:00
}
}
2018-02-25 00:13:39 +01:00
addGlobalExport(
internalName: string,
externalName: string
): ExportRef {
var cStr1 = allocString(internalName);
var cStr2 = allocString(externalName);
2017-12-05 15:28:01 +01:00
try {
return _BinaryenAddGlobalExport(this.ref, cStr1, cStr2);
2017-09-28 13:08:25 +02:00
} finally {
free_memory(cStr2);
free_memory(cStr1);
2017-09-28 13:08:25 +02:00
}
}
removeExport(externalName: string): void {
var cStr = allocString(externalName);
2017-09-28 13:08:25 +02:00
try {
_BinaryenRemoveExport(this.ref, cStr);
} finally {
free_memory(cStr);
2017-09-28 13:08:25 +02:00
}
}
2018-02-25 00:13:39 +01:00
addFunctionImport(
internalName: string,
externalModuleName: string,
externalBaseName: string,
functionType: FunctionTypeRef
): ImportRef {
var cStr1 = allocString(internalName);
var cStr2 = allocString(externalModuleName);
var cStr3 = allocString(externalBaseName);
2017-09-28 13:08:25 +02:00
try {
2017-12-05 15:28:01 +01:00
return _BinaryenAddFunctionImport(this.ref, cStr1, cStr2, cStr3, functionType);
} finally {
free_memory(cStr3);
free_memory(cStr2);
free_memory(cStr1);
2017-12-05 15:28:01 +01:00
}
}
2018-02-25 00:13:39 +01:00
addTableImport(
internalName: string,
externalModuleName: string,
externalBaseName: string
): ImportRef {
var cStr1 = allocString(internalName);
var cStr2 = allocString(externalModuleName);
var cStr3 = allocString(externalBaseName);
2017-12-05 15:28:01 +01:00
try {
return _BinaryenAddTableImport(this.ref, cStr1, cStr2, cStr3);
} finally {
free_memory(cStr3);
free_memory(cStr2);
free_memory(cStr1);
2017-12-05 15:28:01 +01:00
}
}
2018-02-25 00:13:39 +01:00
addMemoryImport(
internalName: string,
externalModuleName: string,
externalBaseName: string
): ImportRef {
var cStr1 = allocString(internalName);
var cStr2 = allocString(externalModuleName);
var cStr3 = allocString(externalBaseName);
2017-12-05 15:28:01 +01:00
try {
return _BinaryenAddMemoryImport(this.ref, cStr1, cStr2, cStr3);
} finally {
free_memory(cStr3);
free_memory(cStr2);
free_memory(cStr1);
2017-12-05 15:28:01 +01:00
}
}
2018-02-25 00:13:39 +01:00
addGlobalImport(
internalName: string,
externalModuleName: string,
externalBaseName: string,
globalType: NativeType
): ImportRef {
var cStr1 = allocString(internalName);
var cStr2 = allocString(externalModuleName);
var cStr3 = allocString(externalBaseName);
2017-12-05 15:28:01 +01:00
try {
return _BinaryenAddGlobalImport(this.ref, cStr1, cStr2, cStr3, globalType);
2017-09-28 13:08:25 +02:00
} finally {
free_memory(cStr3);
free_memory(cStr2);
free_memory(cStr1);
2017-09-28 13:08:25 +02:00
}
}
removeImport(internalName: string): void {
var cStr = allocString(internalName);
2017-09-28 13:08:25 +02:00
try {
_BinaryenRemoveImport(this.ref, cStr);
} finally {
free_memory(cStr);
2017-09-28 13:08:25 +02:00
}
}
2018-02-25 00:13:39 +01:00
setMemory(
initial: Index,
maximum: Index,
segments: MemorySegment[],
target: Target,
exportName: string | null = null
): void {
var cStr = allocString(exportName);
var k = segments.length;
var segs = new Array<usize>(k);
var offs = new Array<ExpressionRef>(k);
var sizs = new Array<Index>(k);
for (let i = 0; i < k; ++i) {
let buffer = segments[i].buffer;
let offset = segments[i].offset;
2017-09-28 13:08:25 +02:00
segs[i] = allocU8Array(buffer);
offs[i] = target == Target.WASM64
? this.createI64(i64_low(offset), i64_high(offset))
: this.createI32(i64_low(offset));
2017-09-28 13:08:25 +02:00
sizs[i] = buffer.length;
}
var cArr1 = allocI32Array(segs);
var cArr2 = allocI32Array(offs);
var cArr3 = allocI32Array(sizs);
2017-09-28 13:08:25 +02:00
try {
_BinaryenSetMemory(this.ref, initial, maximum, cStr, cArr1, cArr2, cArr3, k);
} finally {
free_memory(cArr3);
free_memory(cArr2);
free_memory(cArr1);
for (let i = k - 1; i >= 0; --i) free_memory(segs[i]);
free_memory(cStr);
2017-09-28 13:08:25 +02:00
}
}
setFunctionTable(funcs: FunctionRef[]): void {
var cArr = allocPtrArray(funcs);
2017-12-05 15:28:01 +01:00
try {
_BinaryenSetFunctionTable(this.ref, cArr, funcs.length);
} finally {
free_memory(cArr);
2017-12-05 15:28:01 +01:00
}
}
2017-11-26 04:03:28 +01:00
setStart(func: FunctionRef): void {
2017-09-28 13:08:25 +02:00
_BinaryenSetStart(this.ref, func);
}
setOptimizeLevel(level: i32 = 2): void {
_BinaryenSetOptimizeLevel(level);
}
setShrinkLevel(level: i32 = 1): void {
_BinaryenSetShrinkLevel(level);
}
setDebugInfo(on: bool = false): void {
_BinaryenSetDebugInfo(on);
}
optimize(func: FunctionRef = 0): void {
if (func) {
_BinaryenFunctionOptimize(func, this.ref);
} else {
_BinaryenModuleOptimize(this.ref);
}
}
runPasses(passes: string[], func: FunctionRef = 0): void {
var numNames = passes.length;
var names = new Array<usize>(numNames);
for (let i = 0; i < numNames; ++i) {
names[i] = allocString(passes[i]);
2018-02-25 00:13:39 +01:00
}
var cArr = allocI32Array(names);
try {
2018-02-25 00:13:39 +01:00
if (func) {
_BinaryenFunctionRunPasses(func, this.ref, cArr, numNames);
2018-02-25 00:13:39 +01:00
} else {
_BinaryenModuleRunPasses(this.ref, cArr, numNames);
2018-02-25 00:13:39 +01:00
}
} finally {
free_memory(cArr);
for (let i = numNames; i >= 0; --i) {
free_memory(names[i]);
}
}
2017-09-28 13:08:25 +02:00
}
private cachedPrecomputeName: usize = 0;
private cachedPrecomputeNames: usize = 0;
precomputeFunction(func: FunctionRef): void {
var names = this.cachedPrecomputeNames;
if (!names) {
let name = allocString("precompute");
this.cachedPrecomputeName = name;
this.cachedPrecomputeNames = names = allocI32Array([ name ]);
}
_BinaryenFunctionRunPasses(func, this.ref, names, 1);
}
2017-09-28 13:08:25 +02:00
validate(): bool {
return _BinaryenModuleValidate(this.ref) == 1;
}
interpret(): void {
_BinaryenModuleInterpret(this.ref);
}
2018-03-19 01:12:18 +01:00
toBinary(sourceMapUrl: string | null): BinaryModule {
var out = this.cachedByValue;
var cStr = allocString(sourceMapUrl);
var binaryPtr: usize = 0;
var sourceMapPtr: usize = 0;
try {
_BinaryenModuleAllocateAndWrite(out, this.ref, cStr);
binaryPtr = readInt(out);
let binaryBytes = readInt(out + 4);
sourceMapPtr = readInt(out + 4 * 2);
2018-03-19 01:12:18 +01:00
let ret = new BinaryModule();
ret.output = readBuffer(binaryPtr, binaryBytes);
ret.sourceMap = readString(sourceMapPtr);
return ret;
} finally {
if (cStr) free_memory(cStr);
if (binaryPtr) free_memory(binaryPtr);
if (sourceMapPtr) free_memory(sourceMapPtr);
}
}
toText(): string {
throw new Error("not implemented"); // JS glue overrides this
}
toAsmjs(): string {
throw new Error("not implemented"); // JS glue overrides this
}
2017-09-28 13:08:25 +02:00
dispose(): void {
assert(this.ref);
free_memory(this.cachedByValue);
free_memory(this.cachedTemporaryName);
free_memory(this.cachedPrecomputeName);
free_memory(this.cachedPrecomputeNames);
2017-09-28 13:08:25 +02:00
_BinaryenModuleDispose(this.ref);
this.ref = 0;
2017-09-28 13:08:25 +02:00
}
createRelooper(): Relooper {
return Relooper.create(this);
}
2017-11-17 14:33:51 +01:00
cloneExpression(
expr: ExpressionRef,
2018-02-25 00:13:39 +01:00
noSideEffects: bool = false,
maxDepth: i32 = i32.MAX_VALUE
): ExpressionRef { // currently supports side effect free expressions only
if (maxDepth < 0) return 0;
maxDepth -= 1;
2017-11-17 14:33:51 +01:00
var nested1: ExpressionRef,
nested2: ExpressionRef;
2017-11-17 14:33:51 +01:00
switch (_BinaryenExpressionGetId(expr)) {
case ExpressionId.Const: {
switch (_BinaryenExpressionGetType(expr)) {
case NativeType.I32: {
2018-02-25 00:13:39 +01:00
return this.createI32(_BinaryenConstGetValueI32(expr));
}
case NativeType.I64: {
2018-02-25 00:13:39 +01:00
return this.createI64(
_BinaryenConstGetValueI64Low(expr),
_BinaryenConstGetValueI64High(expr)
);
}
case NativeType.F32: {
2018-02-25 00:13:39 +01:00
return this.createF32(_BinaryenConstGetValueF32(expr));
}
case NativeType.F64: {
2018-02-25 00:13:39 +01:00
return this.createF64(_BinaryenConstGetValueF64(expr));
}
default: {
2018-02-25 00:13:39 +01:00
throw new Error("concrete type expected");
}
}
}
case ExpressionId.GetLocal: {
2018-02-25 00:13:39 +01:00
return _BinaryenGetLocal(this.ref,
_BinaryenGetLocalGetIndex(expr),
_BinaryenExpressionGetType(expr)
);
}
case ExpressionId.GetGlobal: {
let globalName = _BinaryenGetGlobalGetName(expr);
2018-02-25 00:13:39 +01:00
if (!globalName) break;
return _BinaryenGetGlobal(this.ref, globalName, _BinaryenExpressionGetType(expr));
}
case ExpressionId.Load: {
2018-02-25 00:13:39 +01:00
if (!(nested1 = this.cloneExpression(_BinaryenLoadGetPtr(expr), noSideEffects, maxDepth))) {
break;
2018-02-25 00:13:39 +01:00
}
return (
_BinaryenLoadIsAtomic(expr)
? _BinaryenAtomicLoad(this.ref,
_BinaryenLoadGetBytes(expr),
_BinaryenLoadGetOffset(expr),
_BinaryenExpressionGetType(expr),
nested1
)
: _BinaryenLoad(this.ref,
_BinaryenLoadGetBytes(expr),
_BinaryenLoadIsSigned(expr) ? 1 : 0,
_BinaryenLoadGetOffset(expr),
_BinaryenLoadGetAlign(expr),
_BinaryenExpressionGetType(expr),
nested1
)
);
}
case ExpressionId.Unary: {
2018-02-25 00:13:39 +01:00
if (!(nested1 = this.cloneExpression(_BinaryenUnaryGetValue(expr), noSideEffects, maxDepth))) {
break;
2018-02-25 00:13:39 +01:00
}
return _BinaryenUnary(this.ref, _BinaryenUnaryGetOp(expr), nested1);
}
case ExpressionId.Binary: {
2018-02-25 00:13:39 +01:00
if (!(nested1 = this.cloneExpression(_BinaryenBinaryGetLeft(expr), noSideEffects, maxDepth))) {
break;
2018-02-25 00:13:39 +01:00
}
if (!(nested2 = this.cloneExpression(_BinaryenBinaryGetRight(expr), noSideEffects, maxDepth))) {
break;
2018-02-25 00:13:39 +01:00
}
return _BinaryenBinary(this.ref, _BinaryenBinaryGetOp(expr), nested1, nested2);
}
}
return 0;
}
// source map generation
addDebugInfoFile(name: string): Index {
var cStr = allocString(name);
try {
return _BinaryenModuleAddDebugInfoFileName(this.ref, cStr);
} finally {
free_memory(cStr);
}
}
getDebugInfoFile(index: Index): string | null {
return readString(_BinaryenModuleGetDebugInfoFileName(this.ref, index));
}
2018-02-25 00:13:39 +01:00
setDebugLocation(
func: FunctionRef,
expr: ExpressionRef,
fileIndex: Index,
lineNumber: Index,
columnNumber: Index
): void {
_BinaryenFunctionSetDebugLocation(func, expr, fileIndex, lineNumber, columnNumber);
}
}
// expressions
export function getExpressionId(expr: ExpressionRef): ExpressionId {
return _BinaryenExpressionGetId(expr);
}
export function getExpressionType(expr: ExpressionRef): NativeType {
return _BinaryenExpressionGetType(expr);
}
export function getConstValueI32(expr: ExpressionRef): i32 {
return _BinaryenConstGetValueI32(expr);
}
export function getConstValueI64Low(expr: ExpressionRef): i32 {
return _BinaryenConstGetValueI64Low(expr);
}
export function getConstValueI64High(expr: ExpressionRef): i32 {
return _BinaryenConstGetValueI64High(expr);
}
export function getConstValueF32(expr: ExpressionRef): f32 {
return _BinaryenConstGetValueF32(expr);
}
export function getConstValueF64(expr: ExpressionRef): f32 {
return _BinaryenConstGetValueF64(expr);
}
export function getGetLocalIndex(expr: ExpressionRef): Index {
return _BinaryenGetLocalGetIndex(expr);
}
export function getSetLocalIndex(expr: ExpressionRef): Index {
return _BinaryenSetLocalGetIndex(expr);
}
export function getSetLocalValue(expr: ExpressionRef): ExpressionRef {
return _BinaryenSetLocalGetValue(expr);
}
export function isTeeLocal(expr: ExpressionRef): bool {
return _BinaryenSetLocalIsTee(expr);
}
export function getBinaryOp(expr: ExpressionRef): BinaryOp {
return _BinaryenBinaryGetOp(expr);
}
export function getBinaryLeft(expr: ExpressionRef): ExpressionRef {
return _BinaryenBinaryGetLeft(expr);
}
export function getBinaryRight(expr: ExpressionRef): ExpressionRef {
return _BinaryenBinaryGetRight(expr);
}
export function getUnaryOp(expr: ExpressionRef): UnaryOp {
return _BinaryenUnaryGetOp(expr);
}
export function getUnaryValue(expr: ExpressionRef): ExpressionRef {
return _BinaryenUnaryGetValue(expr);
}
export function getLoadBytes(expr: ExpressionRef): u32 {
return _BinaryenLoadGetBytes(expr);
}
export function getLoadOffset(expr: ExpressionRef): u32 {
return _BinaryenLoadGetOffset(expr);
}
export function getLoadPtr(expr: ExpressionRef): ExpressionRef {
return _BinaryenLoadGetPtr(expr);
}
export function isLoadSigned(expr: ExpressionRef): bool {
return _BinaryenLoadIsSigned(expr);
}
export function getStoreBytes(expr: ExpressionRef): u32 {
return _BinaryenStoreGetBytes(expr);
}
export function getStoreOffset(expr: ExpressionRef): u32 {
return _BinaryenStoreGetOffset(expr);
}
export function getStorePtr(expr: ExpressionRef): ExpressionRef {
return _BinaryenStoreGetPtr(expr);
}
export function getStoreValue(expr: ExpressionRef): ExpressionRef {
return _BinaryenStoreGetValue(expr);
}
export function getBlockName(expr: ExpressionRef): string | null {
return readString(_BinaryenBlockGetName(expr));
}
export function getBlockChildCount(expr: ExpressionRef): Index {
return _BinaryenBlockGetNumChildren(expr);
}
export function getBlockChild(expr: ExpressionRef, index: Index): ExpressionRef {
return _BinaryenBlockGetChild(expr, index);
}
export function getIfCondition(expr: ExpressionRef): ExpressionRef {
return _BinaryenIfGetCondition(expr);
}
export function getIfTrue(expr: ExpressionRef): ExpressionRef {
return _BinaryenIfGetIfTrue(expr);
}
export function getIfFalse(expr: ExpressionRef): ExpressionRef {
return _BinaryenIfGetIfFalse(expr);
}
export function getLoopName(expr: ExpressionRef): string | null {
return readString(_BinaryenLoopGetName(expr));
}
export function getLoopBody(expr: ExpressionRef): ExpressionRef {
return _BinaryenLoopGetBody(expr);
}
export function getBreakName(expr: ExpressionRef): string | null {
return readString(_BinaryenBreakGetName(expr));
}
export function getBreakCondition(expr: ExpressionRef): ExpressionRef {
return _BinaryenBreakGetCondition(expr);
}
export function getSelectThen(expr: ExpressionRef): ExpressionRef {
return _BinaryenSelectGetIfTrue(expr);
}
export function getSelectElse(expr: ExpressionRef): ExpressionRef {
return _BinaryenSelectGetIfFalse(expr);
}
export function getSelectCondition(expr: ExpressionRef): ExpressionRef {
return _BinaryenSelectGetCondition(expr);
}
export function getDropValue(expr: ExpressionRef): ExpressionRef {
return _BinaryenDropGetValue(expr);
}
export function getReturnValue(expr: ExpressionRef): ExpressionRef {
return _BinaryenReturnGetValue(expr);
}
export function getCallTarget(expr: ExpressionRef): string | null {
return readString(_BinaryenCallGetTarget(expr));
}
export function getHostOp(expr: ExpressionRef): ExpressionRef {
return _BinaryenHostGetOp(expr);
}
export function getHostOperandCount(expr: ExpressionRef): Index {
return _BinaryenHostGetNumOperands(expr);
}
export function getHostOperand(expr: ExpressionRef, index: Index): ExpressionRef {
return _BinaryenHostGetOperand(expr, index);
}
export function getHostName(expr: ExpressionRef): string | null {
return readString(_BinaryenHostGetNameOperand(expr));
}
// functions
export function getFunctionBody(func: FunctionRef): ExpressionRef {
return _BinaryenFunctionGetBody(func);
}
export function getFunctionName(func: FunctionRef): string | null {
return readString(_BinaryenFunctionGetName(func));
}
export function getFunctionParamCount(func: FunctionRef): Index {
return _BinaryenFunctionGetNumParams(func);
}
export function getFunctionParamType(func: FunctionRef, index: Index): NativeType {
return _BinaryenFunctionGetParam(func, index);
}
export function getFunctionResultType(func: FunctionRef): NativeType {
return _BinaryenFunctionGetResult(func);
}
export class Relooper {
module: Module;
ref: RelooperRef;
static create(module: Module): Relooper {
var relooper = new Relooper();
relooper.module = module;
relooper.ref = _RelooperCreate();
return relooper;
}
private constructor() {}
2017-11-26 04:03:28 +01:00
addBlock(code: ExpressionRef): RelooperBlockRef {
return _RelooperAddBlock(this.ref, code);
}
2017-11-26 04:03:28 +01:00
addBranch(from: RelooperBlockRef, to: RelooperBlockRef, condition: ExpressionRef = 0, code: ExpressionRef = 0): void {
_RelooperAddBranch(from, to, condition, code);
}
2017-11-26 04:03:28 +01:00
addBlockWithSwitch(code: ExpressionRef, condition: ExpressionRef): RelooperBlockRef {
return _RelooperAddBlockWithSwitch(this.ref, code, condition);
}
2017-11-26 04:03:28 +01:00
addBranchForSwitch(from: RelooperBlockRef, to: RelooperBlockRef, indexes: i32[], code: ExpressionRef = 0): void {
var cArr = allocI32Array(indexes);
try {
_RelooperAddBranchForSwitch(from, to, cArr, indexes.length, code);
} finally {
free_memory(cArr);
}
}
2017-11-26 04:03:28 +01:00
renderAndDispose(entry: RelooperBlockRef, labelHelper: Index): ExpressionRef {
return _RelooperRenderAndDispose(this.ref, entry, labelHelper, this.module.ref);
}
2017-09-28 13:08:25 +02:00
}
// export function hasSideEffects(expr: ExpressionRef): bool {
// switch (_BinaryenExpressionGetId(expr = getPtr(expr))) {
// case ExpressionId.GetLocal:
// case ExpressionId.GetGlobal:
// case ExpressionId.Const:
// case ExpressionId.Nop:
// case ExpressionId.Unreachable: {
// return false;
// }
// case ExpressionId.Block: {
// for (let i = 0, k = _BinaryenBlockGetNumChildren(expr); i < k; ++i) {
// if (hasSideEffects(_BinaryenBlockGetChild(expr, i))) return true;
// }
// return false;
// }
// case ExpressionId.If: {
// return hasSideEffects(_BinaryenIfGetCondition(expr))
// || hasSideEffects(_BinaryenIfGetIfTrue(expr))
// || hasSideEffects(_BinaryenIfGetIfFalse(expr));
// }
// case ExpressionId.Unary: {
// return hasSideEffects(_BinaryenUnaryGetValue(expr));
// }
// case ExpressionId.Binary: {
// return hasSideEffects(_BinaryenBinaryGetLeft(expr))
// || hasSideEffects(_BinaryenBinaryGetRight(expr));
// }
// case ExpressionId.Drop: {
// return hasSideEffects(_BinaryenDropGetValue(expr));
// }
// case ExpressionId.Select: {
// return hasSideEffects(_BinaryenSelectGetIfTrue(expr))
// || hasSideEffects(_BinaryenSelectGetIfFalse(expr))
// || hasSideEffects(_BinaryenSelectGetCondition(expr));
// }
// }
// return true;
// }
2017-09-28 13:08:25 +02:00
// helpers
2017-11-17 14:33:51 +01:00
// can't do stack allocation here: STACKTOP is a global in WASM but a hidden variable in asm.js
2017-09-28 13:08:25 +02:00
function allocU8Array(u8s: Uint8Array | null): usize {
2017-09-28 13:08:25 +02:00
if (!u8s) return 0;
var numValues = u8s.length;
var ptr = allocate_memory(numValues);
var idx = ptr;
for (let i = 0; i < numValues; ++i) {
store<u8>(idx++, u8s[i]);
2018-02-25 00:13:39 +01:00
}
2017-09-28 13:08:25 +02:00
return ptr;
}
function allocI32Array(i32s: i32[] | null): usize {
2017-09-28 13:08:25 +02:00
if (!i32s) return 0;
var ptr = allocate_memory(i32s.length << 2);
var idx = ptr;
for (let i = 0, k = i32s.length; i < k; ++i) {
let val = i32s[i];
// store<i32>(idx, val) is not portable
2017-09-28 13:08:25 +02:00
store<u8>(idx , ( val & 0xff) as u8);
store<u8>(idx + 1, ((val >> 8) & 0xff) as u8);
store<u8>(idx + 2, ((val >> 16) & 0xff) as u8);
store<u8>(idx + 3, ( val >>> 24 ) as u8);
idx += 4;
}
return ptr;
}
function allocPtrArray(ptrs: usize[] | null): usize {
return allocI32Array(ptrs); // TODO: WASM64 one day
}
2017-09-28 13:08:25 +02:00
function stringLengthUTF8(str: string): usize {
var len = 0;
for (let i = 0, k = str.length; i < k; ++i) {
let u = str.charCodeAt(i);
2018-02-25 00:13:39 +01:00
if (u >= 0xD800 && u <= 0xDFFF && i + 1 < k) {
2017-09-28 13:08:25 +02:00
u = 0x10000 + ((u & 0x3FF) << 10) | (str.charCodeAt(++i) & 0x3FF);
2018-02-25 00:13:39 +01:00
}
if (u <= 0x7F) {
2017-09-28 13:08:25 +02:00
++len;
2018-02-25 00:13:39 +01:00
} else if (u <= 0x7FF) {
2017-09-28 13:08:25 +02:00
len += 2;
2018-02-25 00:13:39 +01:00
} else if (u <= 0xFFFF) {
2017-09-28 13:08:25 +02:00
len += 3;
2018-02-25 00:13:39 +01:00
} else if (u <= 0x1FFFFF) {
2017-09-28 13:08:25 +02:00
len += 4;
2018-02-25 00:13:39 +01:00
} else if (u <= 0x3FFFFFF) {
2017-09-28 13:08:25 +02:00
len += 5;
2018-02-25 00:13:39 +01:00
} else {
2017-09-28 13:08:25 +02:00
len += 6;
2018-02-25 00:13:39 +01:00
}
2017-09-28 13:08:25 +02:00
}
return len;
}
function allocString(str: string | null): usize {
if (str == null) return 0;
var ptr = allocate_memory(stringLengthUTF8(str) + 1);
// the following is based on Emscripten's stringToUTF8Array
var idx = ptr;
for (let i = 0, k = str.length; i < k; ++i) {
let u = str.charCodeAt(i);
2018-02-25 00:13:39 +01:00
if (u >= 0xD800 && u <= 0xDFFF && i + 1 < k) {
u = 0x10000 + ((u & 0x3FF) << 10) | (str.charCodeAt(++i) & 0x3FF);
2018-02-25 00:13:39 +01:00
}
if (u <= 0x7F) {
2017-09-28 13:08:25 +02:00
store<u8>(idx++, u as u8);
2018-02-25 00:13:39 +01:00
} else if (u <= 0x7FF) {
2017-09-28 13:08:25 +02:00
store<u8>(idx++, (0xC0 | (u >>> 6) ) as u8);
store<u8>(idx++, (0x80 | ( u & 63)) as u8);
} else if (u <= 0xFFFF) {
store<u8>(idx++, (0xE0 | (u >>> 12) ) as u8);
store<u8>(idx++, (0x80 | ((u >>> 6) & 63)) as u8);
store<u8>(idx++, (0x80 | ( u & 63)) as u8);
} else if (u <= 0x1FFFFF) {
store<u8>(idx++, (0xF0 | (u >>> 18) ) as u8);
store<u8>(idx++, (0x80 | ((u >>> 12) & 63)) as u8);
store<u8>(idx++, (0x80 | ((u >>> 6) & 63)) as u8);
store<u8>(idx++, (0x80 | ( u & 63)) as u8);
} else if (u <= 0x3FFFFFF) {
store<u8>(idx++, (0xF8 | (u >>> 24) ) as u8);
store<u8>(idx++, (0x80 | ((u >>> 18) & 63)) as u8);
store<u8>(idx++, (0x80 | ((u >>> 12) & 63)) as u8);
store<u8>(idx++, (0x80 | ((u >>> 6) & 63)) as u8);
store<u8>(idx++, (0x80 | ( u & 63)) as u8);
} else {
store<u8>(idx++, (0xFC | (u >>> 30) ) as u8);
store<u8>(idx++, (0x80 | ((u >>> 24) & 63)) as u8);
store<u8>(idx++, (0x80 | ((u >>> 18) & 63)) as u8);
store<u8>(idx++, (0x80 | ((u >>> 12) & 63)) as u8);
store<u8>(idx++, (0x80 | ((u >>> 6) & 63)) as u8);
store<u8>(idx++, (0x80 | ( u & 63)) as u8);
}
}
store<u8>(idx, 0);
return ptr;
}
2018-03-19 01:12:18 +01:00
function readInt(ptr: usize): i32 {
return (
load<u8>(ptr ) |
(load<u8>(ptr + 1) << 8) |
(load<u8>(ptr + 2) << 16) |
(load<u8>(ptr + 3) << 24)
);
}
2018-03-19 01:12:18 +01:00
function readBuffer(ptr: usize, length: usize): Uint8Array {
var ret = new Uint8Array(length);
for (let i: usize = 0; i < length; ++i) {
ret[i] = load<u8>(ptr + i);
2018-02-25 00:13:39 +01:00
}
return ret;
}
2017-12-12 04:35:30 +01:00
export function readString(ptr: usize): string | null {
if (!ptr) return null;
var arr = new Array<i32>();
// the following is based on Emscripten's UTF8ArrayToString
var cp: u32;
var u1: u32, u2: u32, u3: u32, u4: u32, u5: u32;
while (cp = load<u8>(ptr++)) {
if (!(cp & 0x80)) {
arr.push(cp);
continue;
}
u1 = load<u8>(ptr++) & 63;
if ((cp & 0xE0) == 0xC0) {
arr.push(((cp & 31) << 6) | u1);
continue;
}
u2 = load<u8>(ptr++) & 63;
if ((cp & 0xF0) == 0xE0) {
cp = ((cp & 15) << 12) | (u1 << 6) | u2;
} else {
u3 = load<u8>(ptr++) & 63;
if ((cp & 0xF8) == 0xF0) {
cp = ((cp & 7) << 18) | (u1 << 12) | (u2 << 6) | u3;
} else {
u4 = load<u8>(ptr++) & 63;
if ((cp & 0xFC) == 0xF8) {
cp = ((cp & 3) << 24) | (u1 << 18) | (u2 << 12) | (u3 << 6) | u4;
} else {
u5 = load<u8>(ptr++) & 63;
cp = ((cp & 1) << 30) | (u1 << 24) | (u2 << 18) | (u3 << 12) | (u4 << 6) | u5;
}
}
}
arr.push(cp);
// if (cp < 0x10000) {
// arr.push(cp);
// } else {
// var ch = cp - 0x10000;
// arr.push(0xD800 | (ch >> 10));
// arr.push(0xDC00 | (ch & 0x3FF));
// }
}
// return String.fromCharCodes(arr);
return String.fromCodePoints(arr);
}
/** Result structure of {@link Module#toBinary}. */
2018-03-19 01:12:18 +01:00
export class BinaryModule {
/** WebAssembly binary. */
output: Uint8Array;
/** Source map, if generated. */
sourceMap: string | null;
}