Skip to content

Use Binaryen's function signature naming scheme #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import {
import {
Type,
TypeKind,
TypeFlags
TypeFlags,
Signature
} from "./types";

import {
Expand Down Expand Up @@ -3236,17 +3237,16 @@ export function compileCall(
}
let numOperands = operands.length - 1;
let operandExprs = new Array<ExpressionRef>(numOperands);
let signatureParts = new Array<string>(numOperands + 1);
let nativeReturnType = returnType.toNativeType();
let parameterTypes = new Array<Type>(numOperands);
let nativeParamTypes = new Array<NativeType>(numOperands);
for (let i = 0; i < numOperands; ++i) {
operandExprs[i] = compiler.compileExpressionRetainType(operands[1 + i], Type.i32, WrapMode.NONE);
let operandType = compiler.currentType;
signatureParts[i] = operandType.toSignatureString();
parameterTypes[i] = operandType;
nativeParamTypes[i] = operandType.toNativeType();
}
signatureParts[numOperands] = returnType.toSignatureString();
let typeName = signatureParts.join("");
let typeName = Signature.makeSignatureString(parameterTypes, returnType);
let typeRef = module.getFunctionTypeBySignature(nativeReturnType, nativeParamTypes);
if (!typeRef) typeRef = module.addFunctionType(typeName, nativeReturnType, nativeParamTypes);
compiler.currentType = returnType;
Expand Down Expand Up @@ -5952,7 +5952,7 @@ export function compileIterateRoots(compiler: Compiler): void {
? module.createI64(i64_low(value), i64_high(value))
: module.createI32(i64_low(value))
],
"i_"
"FUNCSIG$vi"
)
);
} else {
Expand All @@ -5965,7 +5965,7 @@ export function compileIterateRoots(compiler: Compiler): void {
compiler.options.nativeSizeType
)
],
"i_"
"FUNCSIG$vi"
)
);
}
Expand Down Expand Up @@ -6049,7 +6049,7 @@ export function ensureGCHook(
[
module.createGetLocal(0, nativeSizeType)
],
nativeSizeType == NativeType.I64 ? "I_" : "i_"
"FUNCSIG$" + (nativeSizeType == NativeType.I64 ? "vj" : "vi")
)
);

Expand Down
25 changes: 17 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,16 +368,25 @@ export class Type {
/** Converts this type to its signature string. */
toSignatureString(): string {
switch (this.kind) {
default: return "i";
// same naming scheme as Binaryen
case TypeKind.I8:
case TypeKind.U8:
case TypeKind.I16:
case TypeKind.U16:
case TypeKind.I32:
case TypeKind.U32:
case TypeKind.BOOL: return "i";
case TypeKind.I64:
case TypeKind.U64: return "I";
case TypeKind.U64: return "j";
case TypeKind.ISIZE:
case TypeKind.USIZE: return this.size == 64 ? "I" : "i";
case TypeKind.USIZE: return this.size == 64 ? "j" : "i";
case TypeKind.F32: return "f";
case TypeKind.F64: return "F";
case TypeKind.V128: return "v";
case TypeKind.VOID: return "_";
case TypeKind.F64: return "d";
case TypeKind.V128: return "V";
case TypeKind.VOID: return "v";
default: assert(false);
}
return "i";
}

// Types
Expand Down Expand Up @@ -614,12 +623,12 @@ export class Signature {
/** Converts a signature to a function type string. */
static makeSignatureString(parameterTypes: Type[] | null, returnType: Type, thisType: Type | null = null): string {
var sb = [];
sb.push(returnType.toSignatureString());
if (thisType) sb.push(thisType.toSignatureString());
if (parameterTypes) {
for (let i = 0, k = parameterTypes.length; i < k; ++i) sb.push(parameterTypes[i].toSignatureString());
}
sb.push(returnType.toSignatureString());
return sb.join("");
return "FUNCSIG$" + sb.join("");
}

/** Converts this signature to a function type string. */
Expand Down
12 changes: 6 additions & 6 deletions tests/compiler/abi.optimized.wat
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(module
(type $i (func (result i32)))
(type $iiii_ (func (param i32 i32 i32 i32)))
(type $_ (func))
(type $FUNCSIG$i (func (result i32)))
(type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
(type $FUNCSIG$v (func))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(memory $0 1)
(data (i32.const 8) "\06\00\00\00a\00b\00i\00.\00t\00s")
Expand All @@ -15,10 +15,10 @@
(export "exportedExported" (func $abi/exported))
(export "exportedInternal" (func $abi/exported))
(start $start)
(func $abi/exported (; 1 ;) (type $i) (result i32)
(func $abi/exported (; 1 ;) (type $FUNCSIG$i) (result i32)
i32.const -128
)
(func $start (; 2 ;) (type $_)
(func $start (; 2 ;) (type $FUNCSIG$v)
i32.const 1
global.set $abi/condition
i32.const 0
Expand All @@ -33,7 +33,7 @@
unreachable
end
)
(func $null (; 3 ;) (type $_)
(func $null (; 3 ;) (type $FUNCSIG$v)
nop
)
)
20 changes: 10 additions & 10 deletions tests/compiler/abi.untouched.wat
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(module
(type $i (func (result i32)))
(type $iiii_ (func (param i32 i32 i32 i32)))
(type $_ (func))
(type $FUNCSIG$i (func (result i32)))
(type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
(type $FUNCSIG$v (func))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(memory $0 1)
(data (i32.const 8) "\06\00\00\00a\00b\00i\00.\00t\00s\00")
Expand All @@ -16,10 +16,10 @@
(export "exportedExported" (func $abi/exportedExported))
(export "exportedInternal" (func $abi/exportedInternal))
(start $start)
(func $abi/internal (; 1 ;) (type $i) (result i32)
(func $abi/internal (; 1 ;) (type $FUNCSIG$i) (result i32)
i32.const 128
)
(func $start:abi (; 2 ;) (type $_)
(func $start:abi (; 2 ;) (type $FUNCSIG$v)
(local $0 i32)
(local $1 i32)
call $abi/internal
Expand Down Expand Up @@ -194,26 +194,26 @@
end
end
)
(func $abi/exported (; 3 ;) (type $i) (result i32)
(func $abi/exported (; 3 ;) (type $FUNCSIG$i) (result i32)
i32.const 128
i32.const 24
i32.shl
i32.const 24
i32.shr_s
)
(func $abi/exportedExported (; 4 ;) (type $i) (result i32)
(func $abi/exportedExported (; 4 ;) (type $FUNCSIG$i) (result i32)
call $abi/exported
)
(func $abi/exportedInternal (; 5 ;) (type $i) (result i32)
(func $abi/exportedInternal (; 5 ;) (type $FUNCSIG$i) (result i32)
call $abi/internal
i32.const 24
i32.shl
i32.const 24
i32.shr_s
)
(func $start (; 6 ;) (type $_)
(func $start (; 6 ;) (type $FUNCSIG$v)
call $start:abi
)
(func $null (; 7 ;) (type $_)
(func $null (; 7 ;) (type $FUNCSIG$v)
)
)
4 changes: 2 additions & 2 deletions tests/compiler/asc-constants.optimized.wat
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(module
(type $_ (func))
(type $FUNCSIG$v (func))
(memory $0 0)
(table $0 1 funcref)
(elem (i32.const 0) $start)
(export "memory" (memory $0))
(export "table" (table $0))
(func $start (; 0 ;) (type $_)
(func $start (; 0 ;) (type $FUNCSIG$v)
nop
)
)
8 changes: 4 additions & 4 deletions tests/compiler/asc-constants.untouched.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(module
(type $_ (func))
(type $FUNCSIG$v (func))
(memory $0 0)
(table $0 1 funcref)
(elem (i32.const 0) $null)
Expand All @@ -17,7 +17,7 @@
(export "memory" (memory $0))
(export "table" (table $0))
(start $start)
(func $start:asc-constants (; 0 ;) (type $_)
(func $start:asc-constants (; 0 ;) (type $FUNCSIG$v)
i32.const 1
drop
i32.const 0
Expand All @@ -39,9 +39,9 @@
i32.const 0
drop
)
(func $start (; 1 ;) (type $_)
(func $start (; 1 ;) (type $FUNCSIG$v)
call $start:asc-constants
)
(func $null (; 2 ;) (type $_)
(func $null (; 2 ;) (type $FUNCSIG$v)
)
)
4 changes: 2 additions & 2 deletions tests/compiler/assert.optimized.wat
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(module
(type $_ (func))
(type $FUNCSIG$v (func))
(memory $0 1)
(data (i32.const 8) "\t\00\00\00a\00s\00s\00e\00r\00t\00.\00t\00s")
(data (i32.const 32) "\0c\00\00\00m\00u\00s\00t\00 \00b\00e\00 \00t\00r\00u\00e")
(table $0 1 funcref)
(elem (i32.const 0) $start)
(export "memory" (memory $0))
(export "table" (table $0))
(func $start (; 0 ;) (type $_)
(func $start (; 0 ;) (type $FUNCSIG$v)
nop
)
)
10 changes: 5 additions & 5 deletions tests/compiler/assert.untouched.wat
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(module
(type $iiii_ (func (param i32 i32 i32 i32)))
(type $_ (func))
(type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
(type $FUNCSIG$v (func))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(memory $0 1)
(data (i32.const 8) "\t\00\00\00a\00s\00s\00e\00r\00t\00.\00t\00s\00")
Expand All @@ -11,7 +11,7 @@
(export "memory" (memory $0))
(export "table" (table $0))
(start $start)
(func $start:assert (; 1 ;) (type $_)
(func $start:assert (; 1 ;) (type $FUNCSIG$v)
(local $0 i32)
i32.const 1
i32.eqz
Expand Down Expand Up @@ -107,9 +107,9 @@
unreachable
end
)
(func $start (; 2 ;) (type $_)
(func $start (; 2 ;) (type $FUNCSIG$v)
call $start:assert
)
(func $null (; 3 ;) (type $_)
(func $null (; 3 ;) (type $FUNCSIG$v)
)
)
8 changes: 4 additions & 4 deletions tests/compiler/binary.optimized.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(module
(type $_ (func))
(type $FUNCSIG$v (func))
(type $FUNCSIG$dd (func (param f64) (result f64)))
(type $FUNCSIG$ff (func (param f32) (result f32)))
(memory $0 0)
Expand Down Expand Up @@ -406,7 +406,7 @@
local.get $0
f64.mul
)
(func $start:binary (; 4 ;) (type $_)
(func $start:binary (; 4 ;) (type $FUNCSIG$v)
(local $0 i32)
(local $1 i64)
(local $2 f32)
Expand Down Expand Up @@ -749,10 +749,10 @@
call $~lib/math/NativeMath.pow
global.set $binary/F
)
(func $start (; 5 ;) (type $_)
(func $start (; 5 ;) (type $FUNCSIG$v)
call $start:binary
)
(func $null (; 6 ;) (type $_)
(func $null (; 6 ;) (type $FUNCSIG$v)
nop
)
)
Loading