Eliminate leftover temporary function types using latest Binaryen

This commit is contained in:
dcodeIO
2018-05-09 16:33:42 +02:00
parent dae9880428
commit ef9b43740d
46 changed files with 48 additions and 79 deletions

View File

@ -2311,21 +2311,10 @@ export class Compiler extends DiagnosticEmitter {
var module = this.module;
var type = this.currentType;
var nativeType = type.toNativeType();
var typeRef = module.getFunctionTypeBySignature(nativeType, null);
var typeRefAdded = false;
if (!typeRef) {
typeRef = module.addFunctionType(type.toSignatureString(), nativeType, null);
typeRefAdded = true;
}
var funcRef = module.addFunction("__precompute", typeRef, null, expr);
var funcRef = module.addTemporaryFunction(nativeType, null, expr);
module.runPasses([ "precompute" ], funcRef);
var ret = getFunctionBody(funcRef);
module.removeFunction("__precompute");
if (typeRefAdded) {
// TODO: also remove the function type somehow if no longer used or make the C-API accept
// a `null` typeRef, using an implicit type.
// module.removeFunctionType(typeRef);
}
module.removeTemporaryFunction();
return ret;
}

View File

@ -358,6 +358,7 @@ declare type BinaryenFunctionTypeRef = usize;
declare function _BinaryenAddFunctionType(module: BinaryenModuleRef, name: usize, result: BinaryenType, paramTypes: usize, numParams: BinaryenIndex): BinaryenFunctionTypeRef;
declare function _BinaryenGetFunctionTypeBySignature(module: BinaryenModuleRef, result: BinaryenType, paramTypes: usize, numParams: BinaryenIndex): BinaryenFunctionTypeRef;
declare function _BinaryenRemoveFunctionType(module: BinaryenModuleRef, name: usize): void;
declare function _BinaryenFunctionTypeGetName(ftype: BinaryenFunctionTypeRef): usize;
declare function _BinaryenFunctionTypeGetNumParams(ftype: BinaryenFunctionTypeRef): BinaryenIndex;

View File

@ -297,6 +297,15 @@ export class Module {
}
}
removeFunctionType(name: string): void {
var cStr = allocString(name);
try {
_BinaryenRemoveFunctionType(this.ref, cStr);
} finally {
free_memory(cStr);
}
}
// constants
createI32(value: i32): ExpressionRef {
@ -663,6 +672,28 @@ export class Module {
}
}
private tempName: usize = 0;
private hasTempFunc: bool = false;
addTemporaryFunction(result: NativeType, paramTypes: NativeType[] | null, body: ExpressionRef): FunctionRef {
this.hasTempFunc = assert(!this.hasTempFunc);
if (!this.tempName) this.tempName = allocString(""); // works because strings are interned
var cArr = allocI32Array(paramTypes);
try {
let typeRef = _BinaryenAddFunctionType(this.ref, this.tempName, result, cArr, paramTypes ? paramTypes.length : 0);
return _BinaryenAddFunction(this.ref, this.tempName, typeRef, 0, 0, body);
} finally {
free_memory(cArr);
}
}
removeTemporaryFunction(): void {
this.hasTempFunc = !assert(this.hasTempFunc);
var tempName = assert(this.tempName);
_BinaryenRemoveFunction(this.ref, tempName);
_BinaryenRemoveFunctionType(this.ref, tempName);
}
addFunctionExport(
internalName: string,
externalName: string