Skip to content
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

feat: store local name in debug mode binary #2437

Merged
merged 14 commits into from
Aug 21, 2022
14 changes: 13 additions & 1 deletion src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,9 @@ export class Compiler extends DiagnosticEmitter {
typesToRefs(startFunctionInstance.additionalLocals),
module.flatten(startFunctionBody)
);
if (!options.willOptimize) {
module.setLocalNames(funcRef, startFunctionInstance.localsByIndex.map<string>(local => local.name));
}
startFunctionInstance.finalize(module, funcRef);
if (exportStart == null) module.setStart(funcRef);
else {
Expand Down Expand Up @@ -1019,13 +1022,16 @@ export class Compiler extends DiagnosticEmitter {
let numLocals = locals.length;
let varTypes = new Array<TypeRef>(numLocals);
for (let i = 0; i < numLocals; ++i) varTypes[i] = locals[i].type.toRef();
module.addFunction(
const funcRef = module.addFunction(
startFunction.internalName,
startSignature.paramRefs,
startSignature.resultRefs,
varTypes,
module.flatten(startFunctionBody)
);
if (!this.options.willOptimize) {
HerrCai0907 marked this conversation as resolved.
Show resolved Hide resolved
module.setLocalNames(funcRef, startFunction.localsByIndex.map<string>(local => local.name));
}
previousBody.push(
module.call(startFunction.internalName, null, TypeRef.None)
);
Expand Down Expand Up @@ -1477,6 +1483,9 @@ export class Compiler extends DiagnosticEmitter {
typesToRefs(instance.additionalLocals),
module.flatten(stmts, instance.signature.returnType.toRef())
);
if (!this.options.willOptimize) {
module.setLocalNames(funcRef, instance.localsByIndex.map<string>(local => local.name));
}

// imported function
} else if (instance.is(CommonFlags.AMBIENT)) {
Expand Down Expand Up @@ -8612,6 +8621,9 @@ export class Compiler extends DiagnosticEmitter {
varTypes,
module.flatten(stmts, sizeTypeRef)
);
if (!this.options.willOptimize) {
module.setLocalNames(funcRef, instance.localsByIndex.map<string>(local => local.name));
}
HerrCai0907 marked this conversation as resolved.
Show resolved Hide resolved
instance.finalize(module, funcRef);
}

Expand Down
17 changes: 17 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,23 @@ export class Module {
return ret;
}

setLocalNames(funcRef: FunctionRef, localNames: string[] | null): void {
if (localNames == null) return;
let localNameMap = new Set<string>();
for (let i = 0, k = localNames.length; i < k; i++) {
let localName = localNames[i];
MaxGraey marked this conversation as resolved.
Show resolved Hide resolved
if (localNameMap.has(localName)) {
let repeat = 0;
while (localNameMap.has(`${localName}_${repeat}`)) {
repeat++;
}
localName = `${localName}_${repeat}`;
}
localNameMap.add(localName);
binaryen._BinaryenFunctionSetLocalName(funcRef, i, this.allocStringCached(localName));
}
}

getFunction(
name: string
): FunctionRef {
Expand Down
130 changes: 65 additions & 65 deletions tests/compiler/NonNullable.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,44 @@
(elem $0 (i32.const 1))
(export "memory" (memory $0))
(start $~start)
(func $~lib/string/String#get:length (param $0 i32) (result i32)
local.get $0
(func $~lib/string/String#get:length (param $this i32) (result i32)
local.get $this
i32.const 20
i32.sub
i32.load offset=16
i32.const 1
i32.shr_u
)
(func $~lib/util/string/compareImpl (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (param $4 i32) (result i32)
(local $5 i32)
(local $6 i32)
(local $7 i32)
(local $8 i32)
(local $9 i32)
local.get $0
local.get $1
(func $~lib/util/string/compareImpl (param $str1 i32) (param $index1 i32) (param $str2 i32) (param $index2 i32) (param $len i32) (result i32)
(local $ptr1 i32)
(local $ptr2 i32)
(local $var$7 i32)
(local $a i32)
(local $b i32)
local.get $str1
local.get $index1
i32.const 1
i32.shl
i32.add
local.set $5
local.get $2
local.get $3
local.set $ptr1
local.get $str2
local.get $index2
i32.const 1
i32.shl
i32.add
local.set $6
local.set $ptr2
i32.const 0
i32.const 2
i32.lt_s
drop
local.get $4
local.get $len
i32.const 4
i32.ge_u
if (result i32)
local.get $5
local.get $ptr1
i32.const 7
i32.and
local.get $6
local.get $ptr2
i32.const 7
i32.and
i32.or
Expand All @@ -73,124 +73,124 @@
if
block $do-break|0
loop $do-loop|0
local.get $5
local.get $ptr1
i64.load
local.get $6
local.get $ptr2
i64.load
i64.ne
if
br $do-break|0
end
local.get $5
local.get $ptr1
i32.const 8
i32.add
local.set $5
local.get $6
local.set $ptr1
local.get $ptr2
i32.const 8
i32.add
local.set $6
local.get $4
local.set $ptr2
local.get $len
i32.const 4
i32.sub
local.set $4
local.get $4
local.set $len
local.get $len
i32.const 4
i32.ge_u
br_if $do-loop|0
end
end
end
loop $while-continue|1
local.get $4
local.tee $7
local.get $len
local.tee $var$7
i32.const 1
i32.sub
local.set $4
local.get $7
local.set $7
local.get $7
local.set $len
local.get $var$7
local.set $var$7
local.get $var$7
if
local.get $5
local.get $ptr1
i32.load16_u
local.set $8
local.get $6
local.set $a
local.get $ptr2
i32.load16_u
local.set $9
local.get $8
local.get $9
local.set $b
local.get $a
local.get $b
i32.ne
if
local.get $8
local.get $9
local.get $a
local.get $b
i32.sub
return
end
local.get $5
local.get $ptr1
i32.const 2
i32.add
local.set $5
local.get $6
local.set $ptr1
local.get $ptr2
i32.const 2
i32.add
local.set $6
local.set $ptr2
br $while-continue|1
end
end
i32.const 0
)
(func $~lib/string/String.__eq (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
local.get $0
local.get $1
(func $~lib/string/String.__eq (param $left i32) (param $right i32) (result i32)
(local $leftLength i32)
local.get $left
local.get $right
i32.eq
if
i32.const 1
return
end
local.get $0
local.get $left
i32.const 0
i32.eq
if (result i32)
i32.const 1
else
local.get $1
local.get $right
i32.const 0
i32.eq
end
if
i32.const 0
return
end
local.get $0
local.get $left
call $~lib/string/String#get:length
local.set $2
local.get $2
local.get $1
local.set $leftLength
local.get $leftLength
local.get $right
call $~lib/string/String#get:length
i32.ne
if
i32.const 0
return
end
local.get $0
local.get $left
i32.const 0
local.get $1
local.get $right
i32.const 0
local.get $2
local.get $leftLength
call $~lib/util/string/compareImpl
i32.eqz
)
(func $~lib/string/String.__ne (param $0 i32) (param $1 i32) (result i32)
local.get $0
local.get $1
(func $~lib/string/String.__ne (param $left i32) (param $right i32) (result i32)
local.get $left
local.get $right
call $~lib/string/String.__eq
i32.eqz
)
(func $NonNullable/assertNonNull<~lib/string/String> (param $0 i32)
(func $NonNullable/assertNonNull<~lib/string/String> (param $t i32)
i32.const 0
i32.eqz
drop
local.get $0
local.get $t
i32.const 0
call $~lib/string/String.__ne
i32.eqz
Expand All @@ -203,12 +203,12 @@
unreachable
end
)
(func $NonNullable/safetyCheck<~lib/string/String|null> (param $0 i32)
local.get $0
(func $NonNullable/safetyCheck<~lib/string/String|null> (param $t i32)
local.get $t
i32.const 0
call $~lib/string/String.__ne
if
local.get $0
local.get $t
call $NonNullable/assertNonNull<~lib/string/String>
end
)
Expand Down
Loading