Skip to content

Commit 4e1bba3

Browse files
authored
Use Binaryen's function signature naming scheme (AssemblyScript#522)
1 parent 7184db6 commit 4e1bba3

File tree

198 files changed

+3929
-3961
lines changed

Some content is hidden

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

198 files changed

+3929
-3961
lines changed

src/builtins.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import {
2727
import {
2828
Type,
2929
TypeKind,
30-
TypeFlags
30+
TypeFlags,
31+
Signature
3132
} from "./types";
3233

3334
import {
@@ -3236,17 +3237,16 @@ export function compileCall(
32363237
}
32373238
let numOperands = operands.length - 1;
32383239
let operandExprs = new Array<ExpressionRef>(numOperands);
3239-
let signatureParts = new Array<string>(numOperands + 1);
32403240
let nativeReturnType = returnType.toNativeType();
3241+
let parameterTypes = new Array<Type>(numOperands);
32413242
let nativeParamTypes = new Array<NativeType>(numOperands);
32423243
for (let i = 0; i < numOperands; ++i) {
32433244
operandExprs[i] = compiler.compileExpressionRetainType(operands[1 + i], Type.i32, WrapMode.NONE);
32443245
let operandType = compiler.currentType;
3245-
signatureParts[i] = operandType.toSignatureString();
3246+
parameterTypes[i] = operandType;
32463247
nativeParamTypes[i] = operandType.toNativeType();
32473248
}
3248-
signatureParts[numOperands] = returnType.toSignatureString();
3249-
let typeName = signatureParts.join("");
3249+
let typeName = Signature.makeSignatureString(parameterTypes, returnType);
32503250
let typeRef = module.getFunctionTypeBySignature(nativeReturnType, nativeParamTypes);
32513251
if (!typeRef) typeRef = module.addFunctionType(typeName, nativeReturnType, nativeParamTypes);
32523252
compiler.currentType = returnType;
@@ -5952,7 +5952,7 @@ export function compileIterateRoots(compiler: Compiler): void {
59525952
? module.createI64(i64_low(value), i64_high(value))
59535953
: module.createI32(i64_low(value))
59545954
],
5955-
"i_"
5955+
"FUNCSIG$vi"
59565956
)
59575957
);
59585958
} else {
@@ -5965,7 +5965,7 @@ export function compileIterateRoots(compiler: Compiler): void {
59655965
compiler.options.nativeSizeType
59665966
)
59675967
],
5968-
"i_"
5968+
"FUNCSIG$vi"
59695969
)
59705970
);
59715971
}
@@ -6049,7 +6049,7 @@ export function ensureGCHook(
60496049
[
60506050
module.createGetLocal(0, nativeSizeType)
60516051
],
6052-
nativeSizeType == NativeType.I64 ? "I_" : "i_"
6052+
"FUNCSIG$" + (nativeSizeType == NativeType.I64 ? "vj" : "vi")
60536053
)
60546054
);
60556055

src/types.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,25 @@ export class Type {
368368
/** Converts this type to its signature string. */
369369
toSignatureString(): string {
370370
switch (this.kind) {
371-
default: return "i";
371+
// same naming scheme as Binaryen
372+
case TypeKind.I8:
373+
case TypeKind.U8:
374+
case TypeKind.I16:
375+
case TypeKind.U16:
376+
case TypeKind.I32:
377+
case TypeKind.U32:
378+
case TypeKind.BOOL: return "i";
372379
case TypeKind.I64:
373-
case TypeKind.U64: return "I";
380+
case TypeKind.U64: return "j";
374381
case TypeKind.ISIZE:
375-
case TypeKind.USIZE: return this.size == 64 ? "I" : "i";
382+
case TypeKind.USIZE: return this.size == 64 ? "j" : "i";
376383
case TypeKind.F32: return "f";
377-
case TypeKind.F64: return "F";
378-
case TypeKind.V128: return "v";
379-
case TypeKind.VOID: return "_";
384+
case TypeKind.F64: return "d";
385+
case TypeKind.V128: return "V";
386+
case TypeKind.VOID: return "v";
387+
default: assert(false);
380388
}
389+
return "i";
381390
}
382391

383392
// Types
@@ -614,12 +623,12 @@ export class Signature {
614623
/** Converts a signature to a function type string. */
615624
static makeSignatureString(parameterTypes: Type[] | null, returnType: Type, thisType: Type | null = null): string {
616625
var sb = [];
626+
sb.push(returnType.toSignatureString());
617627
if (thisType) sb.push(thisType.toSignatureString());
618628
if (parameterTypes) {
619629
for (let i = 0, k = parameterTypes.length; i < k; ++i) sb.push(parameterTypes[i].toSignatureString());
620630
}
621-
sb.push(returnType.toSignatureString());
622-
return sb.join("");
631+
return "FUNCSIG$" + sb.join("");
623632
}
624633

625634
/** Converts this signature to a function type string. */

tests/compiler/abi.optimized.wat

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(module
2-
(type $i (func (result i32)))
3-
(type $iiii_ (func (param i32 i32 i32 i32)))
4-
(type $_ (func))
2+
(type $FUNCSIG$i (func (result i32)))
3+
(type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
4+
(type $FUNCSIG$v (func))
55
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
66
(memory $0 1)
77
(data (i32.const 8) "\06\00\00\00a\00b\00i\00.\00t\00s")
@@ -15,10 +15,10 @@
1515
(export "exportedExported" (func $abi/exported))
1616
(export "exportedInternal" (func $abi/exported))
1717
(start $start)
18-
(func $abi/exported (; 1 ;) (type $i) (result i32)
18+
(func $abi/exported (; 1 ;) (type $FUNCSIG$i) (result i32)
1919
i32.const -128
2020
)
21-
(func $start (; 2 ;) (type $_)
21+
(func $start (; 2 ;) (type $FUNCSIG$v)
2222
i32.const 1
2323
global.set $abi/condition
2424
i32.const 0
@@ -33,7 +33,7 @@
3333
unreachable
3434
end
3535
)
36-
(func $null (; 3 ;) (type $_)
36+
(func $null (; 3 ;) (type $FUNCSIG$v)
3737
nop
3838
)
3939
)

tests/compiler/abi.untouched.wat

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(module
2-
(type $i (func (result i32)))
3-
(type $iiii_ (func (param i32 i32 i32 i32)))
4-
(type $_ (func))
2+
(type $FUNCSIG$i (func (result i32)))
3+
(type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
4+
(type $FUNCSIG$v (func))
55
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
66
(memory $0 1)
77
(data (i32.const 8) "\06\00\00\00a\00b\00i\00.\00t\00s\00")
@@ -16,10 +16,10 @@
1616
(export "exportedExported" (func $abi/exportedExported))
1717
(export "exportedInternal" (func $abi/exportedInternal))
1818
(start $start)
19-
(func $abi/internal (; 1 ;) (type $i) (result i32)
19+
(func $abi/internal (; 1 ;) (type $FUNCSIG$i) (result i32)
2020
i32.const 128
2121
)
22-
(func $start:abi (; 2 ;) (type $_)
22+
(func $start:abi (; 2 ;) (type $FUNCSIG$v)
2323
(local $0 i32)
2424
(local $1 i32)
2525
call $abi/internal
@@ -194,26 +194,26 @@
194194
end
195195
end
196196
)
197-
(func $abi/exported (; 3 ;) (type $i) (result i32)
197+
(func $abi/exported (; 3 ;) (type $FUNCSIG$i) (result i32)
198198
i32.const 128
199199
i32.const 24
200200
i32.shl
201201
i32.const 24
202202
i32.shr_s
203203
)
204-
(func $abi/exportedExported (; 4 ;) (type $i) (result i32)
204+
(func $abi/exportedExported (; 4 ;) (type $FUNCSIG$i) (result i32)
205205
call $abi/exported
206206
)
207-
(func $abi/exportedInternal (; 5 ;) (type $i) (result i32)
207+
(func $abi/exportedInternal (; 5 ;) (type $FUNCSIG$i) (result i32)
208208
call $abi/internal
209209
i32.const 24
210210
i32.shl
211211
i32.const 24
212212
i32.shr_s
213213
)
214-
(func $start (; 6 ;) (type $_)
214+
(func $start (; 6 ;) (type $FUNCSIG$v)
215215
call $start:abi
216216
)
217-
(func $null (; 7 ;) (type $_)
217+
(func $null (; 7 ;) (type $FUNCSIG$v)
218218
)
219219
)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
(module
2-
(type $_ (func))
2+
(type $FUNCSIG$v (func))
33
(memory $0 0)
44
(table $0 1 funcref)
55
(elem (i32.const 0) $start)
66
(export "memory" (memory $0))
77
(export "table" (table $0))
8-
(func $start (; 0 ;) (type $_)
8+
(func $start (; 0 ;) (type $FUNCSIG$v)
99
nop
1010
)
1111
)

tests/compiler/asc-constants.untouched.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(module
2-
(type $_ (func))
2+
(type $FUNCSIG$v (func))
33
(memory $0 0)
44
(table $0 1 funcref)
55
(elem (i32.const 0) $null)
@@ -17,7 +17,7 @@
1717
(export "memory" (memory $0))
1818
(export "table" (table $0))
1919
(start $start)
20-
(func $start:asc-constants (; 0 ;) (type $_)
20+
(func $start:asc-constants (; 0 ;) (type $FUNCSIG$v)
2121
i32.const 1
2222
drop
2323
i32.const 0
@@ -39,9 +39,9 @@
3939
i32.const 0
4040
drop
4141
)
42-
(func $start (; 1 ;) (type $_)
42+
(func $start (; 1 ;) (type $FUNCSIG$v)
4343
call $start:asc-constants
4444
)
45-
(func $null (; 2 ;) (type $_)
45+
(func $null (; 2 ;) (type $FUNCSIG$v)
4646
)
4747
)

tests/compiler/assert.optimized.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
(module
2-
(type $_ (func))
2+
(type $FUNCSIG$v (func))
33
(memory $0 1)
44
(data (i32.const 8) "\t\00\00\00a\00s\00s\00e\00r\00t\00.\00t\00s")
55
(data (i32.const 32) "\0c\00\00\00m\00u\00s\00t\00 \00b\00e\00 \00t\00r\00u\00e")
66
(table $0 1 funcref)
77
(elem (i32.const 0) $start)
88
(export "memory" (memory $0))
99
(export "table" (table $0))
10-
(func $start (; 0 ;) (type $_)
10+
(func $start (; 0 ;) (type $FUNCSIG$v)
1111
nop
1212
)
1313
)

tests/compiler/assert.untouched.wat

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
2-
(type $iiii_ (func (param i32 i32 i32 i32)))
3-
(type $_ (func))
2+
(type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
3+
(type $FUNCSIG$v (func))
44
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
55
(memory $0 1)
66
(data (i32.const 8) "\t\00\00\00a\00s\00s\00e\00r\00t\00.\00t\00s\00")
@@ -11,7 +11,7 @@
1111
(export "memory" (memory $0))
1212
(export "table" (table $0))
1313
(start $start)
14-
(func $start:assert (; 1 ;) (type $_)
14+
(func $start:assert (; 1 ;) (type $FUNCSIG$v)
1515
(local $0 i32)
1616
i32.const 1
1717
i32.eqz
@@ -107,9 +107,9 @@
107107
unreachable
108108
end
109109
)
110-
(func $start (; 2 ;) (type $_)
110+
(func $start (; 2 ;) (type $FUNCSIG$v)
111111
call $start:assert
112112
)
113-
(func $null (; 3 ;) (type $_)
113+
(func $null (; 3 ;) (type $FUNCSIG$v)
114114
)
115115
)

tests/compiler/binary.optimized.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(module
2-
(type $_ (func))
2+
(type $FUNCSIG$v (func))
33
(type $FUNCSIG$dd (func (param f64) (result f64)))
44
(type $FUNCSIG$ff (func (param f32) (result f32)))
55
(memory $0 0)
@@ -406,7 +406,7 @@
406406
local.get $0
407407
f64.mul
408408
)
409-
(func $start:binary (; 4 ;) (type $_)
409+
(func $start:binary (; 4 ;) (type $FUNCSIG$v)
410410
(local $0 i32)
411411
(local $1 i64)
412412
(local $2 f32)
@@ -749,10 +749,10 @@
749749
call $~lib/math/NativeMath.pow
750750
global.set $binary/F
751751
)
752-
(func $start (; 5 ;) (type $_)
752+
(func $start (; 5 ;) (type $FUNCSIG$v)
753753
call $start:binary
754754
)
755-
(func $null (; 6 ;) (type $_)
755+
(func $null (; 6 ;) (type $FUNCSIG$v)
756756
nop
757757
)
758758
)

0 commit comments

Comments
 (0)