Skip to content

Commit ef9b437

Browse files
committed
Eliminate leftover temporary function types using latest Binaryen
1 parent dae9880 commit ef9b437

46 files changed

Lines changed: 48 additions & 79 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dist/assemblyscript.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"dependencies": {
1414
"@protobufjs/utf8": "^1.1.0",
15-
"binaryen": "47.0.0-nightly.20180507",
15+
"binaryen": "47.0.0-nightly.20180509",
1616
"glob": "^7.1.2",
1717
"long": "^4.0.0",
1818
"minimist": "^1.2.0",

src/compiler.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,21 +2311,10 @@ export class Compiler extends DiagnosticEmitter {
23112311
var module = this.module;
23122312
var type = this.currentType;
23132313
var nativeType = type.toNativeType();
2314-
var typeRef = module.getFunctionTypeBySignature(nativeType, null);
2315-
var typeRefAdded = false;
2316-
if (!typeRef) {
2317-
typeRef = module.addFunctionType(type.toSignatureString(), nativeType, null);
2318-
typeRefAdded = true;
2319-
}
2320-
var funcRef = module.addFunction("__precompute", typeRef, null, expr);
2314+
var funcRef = module.addTemporaryFunction(nativeType, null, expr);
23212315
module.runPasses([ "precompute" ], funcRef);
23222316
var ret = getFunctionBody(funcRef);
2323-
module.removeFunction("__precompute");
2324-
if (typeRefAdded) {
2325-
// TODO: also remove the function type somehow if no longer used or make the C-API accept
2326-
// a `null` typeRef, using an implicit type.
2327-
// module.removeFunctionType(typeRef);
2328-
}
2317+
module.removeTemporaryFunction();
23292318
return ret;
23302319
}
23312320

src/glue/binaryen.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ declare type BinaryenFunctionTypeRef = usize;
358358

359359
declare function _BinaryenAddFunctionType(module: BinaryenModuleRef, name: usize, result: BinaryenType, paramTypes: usize, numParams: BinaryenIndex): BinaryenFunctionTypeRef;
360360
declare function _BinaryenGetFunctionTypeBySignature(module: BinaryenModuleRef, result: BinaryenType, paramTypes: usize, numParams: BinaryenIndex): BinaryenFunctionTypeRef;
361+
declare function _BinaryenRemoveFunctionType(module: BinaryenModuleRef, name: usize): void;
361362

362363
declare function _BinaryenFunctionTypeGetName(ftype: BinaryenFunctionTypeRef): usize;
363364
declare function _BinaryenFunctionTypeGetNumParams(ftype: BinaryenFunctionTypeRef): BinaryenIndex;

src/module.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,15 @@ export class Module {
297297
}
298298
}
299299

300+
removeFunctionType(name: string): void {
301+
var cStr = allocString(name);
302+
try {
303+
_BinaryenRemoveFunctionType(this.ref, cStr);
304+
} finally {
305+
free_memory(cStr);
306+
}
307+
}
308+
300309
// constants
301310

302311
createI32(value: i32): ExpressionRef {
@@ -663,6 +672,28 @@ export class Module {
663672
}
664673
}
665674

675+
private tempName: usize = 0;
676+
private hasTempFunc: bool = false;
677+
678+
addTemporaryFunction(result: NativeType, paramTypes: NativeType[] | null, body: ExpressionRef): FunctionRef {
679+
this.hasTempFunc = assert(!this.hasTempFunc);
680+
if (!this.tempName) this.tempName = allocString(""); // works because strings are interned
681+
var cArr = allocI32Array(paramTypes);
682+
try {
683+
let typeRef = _BinaryenAddFunctionType(this.ref, this.tempName, result, cArr, paramTypes ? paramTypes.length : 0);
684+
return _BinaryenAddFunction(this.ref, this.tempName, typeRef, 0, 0, body);
685+
} finally {
686+
free_memory(cArr);
687+
}
688+
}
689+
690+
removeTemporaryFunction(): void {
691+
this.hasTempFunc = !assert(this.hasTempFunc);
692+
var tempName = assert(this.tempName);
693+
_BinaryenRemoveFunction(this.ref, tempName);
694+
_BinaryenRemoveFunctionType(this.ref, tempName);
695+
}
696+
666697
addFunctionExport(
667698
internalName: string,
668699
externalName: string

tests/compiler/assert.untouched.wat

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
(module
22
(type $iiiiv (func (param i32 i32 i32 i32)))
3-
(type $i (func (result i32)))
43
(type $v (func))
54
(import "env" "abort" (func $abort (param i32 i32 i32 i32)))
65
(global $HEAP_BASE i32 (i32.const 56))

tests/compiler/binary.untouched.wat

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
(module
22
(type $FFF (func (param f64 f64) (result f64)))
3-
(type $F (func (result f64)))
4-
(type $i (func (result i32)))
53
(type $FiF (func (param f64 i32) (result f64)))
64
(type $fff (func (param f32 f32) (result f32)))
75
(type $fi (func (param f32) (result i32)))
8-
(type $f (func (result f32)))
96
(type $fif (func (param f32 i32) (result f32)))
107
(type $Fi (func (param f64) (result i32)))
118
(type $v (func))

tests/compiler/builtins.untouched.wat

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
(module
22
(type $iiiiv (func (param i32 i32 i32 i32)))
3-
(type $F (func (result f64)))
43
(type $fi (func (param f32) (result i32)))
54
(type $Fi (func (param f64) (result i32)))
6-
(type $i (func (result i32)))
75
(type $v (func))
8-
(type $f (func (result f32)))
96
(import "env" "abort" (func $abort (param i32 i32 i32 i32)))
107
(global $builtins/b (mut i32) (i32.const 0))
118
(global $builtins/i (mut i32) (i32.const 0))

0 commit comments

Comments
 (0)