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
4 changes: 4 additions & 0 deletions src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10198,6 +10198,7 @@ export function compileVisitGlobals(compiler: Compiler): void {
TypeRef.I32, // cookie
TypeRef.None, // => void
[ sizeTypeRef ],
null,
exprs.length
? module.block(null, exprs)
: module.nop()
Expand Down Expand Up @@ -10304,6 +10305,7 @@ function ensureVisitMembersOf(compiler: Compiler, instance: Class): void {
createType([sizeTypeRef, TypeRef.I32]),
TypeRef.None,
needsTempValue ? [ sizeTypeRef ] : null,
null,
module.flatten(body, TypeRef.None)
);

Expand Down Expand Up @@ -10386,6 +10388,7 @@ export function compileVisitMembers(compiler: Compiler): void {
createType([ sizeTypeRef, TypeRef.I32 ]), // this, cookie
TypeRef.None, // => void
null,
null,
module.flatten([
current,
module.unreachable()
Expand Down Expand Up @@ -10522,6 +10525,7 @@ export function compileClassInstanceOf(compiler: Compiler, prototype: ClassProto
sizeTypeRef,
TypeRef.I32,
null,
null,
module.flatten(stmts)
);
}
Expand Down
18 changes: 14 additions & 4 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ export class Compiler extends DiagnosticEmitter {

// expose the arguments length helper if there are varargs exports
if (this.runtimeFeatures & RuntimeFeatures.setArgumentsLength) {
module.addFunction(BuiltinNames.setArgumentsLength, TypeRef.I32, TypeRef.None, null,
module.addFunction(BuiltinNames.setArgumentsLength, TypeRef.I32, TypeRef.None, null, null,
module.global_set(this.ensureArgumentsLength(), module.local_get(0, TypeRef.I32))
);
module.addFunctionExport(BuiltinNames.setArgumentsLength, ExportNames.setArgumentsLength);
Expand Down Expand Up @@ -784,6 +784,7 @@ export class Compiler extends DiagnosticEmitter {
signature.paramRefs,
signature.resultRefs,
typesToRefs(startFunctionInstance.additionalLocals),
startFunctionInstance.localsByIndex.map<string>(local => local.name),
module.flatten(startFunctionBody)
);
startFunctionInstance.finalize(module, funcRef);
Expand Down Expand Up @@ -1024,6 +1025,7 @@ export class Compiler extends DiagnosticEmitter {
startSignature.paramRefs,
startSignature.resultRefs,
varTypes,
startFunction.localsByIndex.map<string>(locals => locals.name),
module.flatten(startFunctionBody)
);
previousBody.push(
Expand Down Expand Up @@ -1475,6 +1477,7 @@ export class Compiler extends DiagnosticEmitter {
signature.paramRefs,
signature.resultRefs,
typesToRefs(instance.additionalLocals),
instance.localsByIndex.map<string>(local => local.name),
module.flatten(stmts, instance.signature.returnType.toRef())
);

Expand Down Expand Up @@ -1515,6 +1518,7 @@ export class Compiler extends DiagnosticEmitter {
signature.paramRefs,
signature.resultRefs,
null,
null,
module.unreachable()
);
} else {
Expand Down Expand Up @@ -1680,7 +1684,7 @@ export class Compiler extends DiagnosticEmitter {
var valueTypeRef = valueType.toRef();
var thisTypeRef = this.options.sizeTypeRef;
// return this.field
instance.getterRef = module.addFunction(instance.internalGetterName, thisTypeRef, valueTypeRef, null,
instance.getterRef = module.addFunction(instance.internalGetterName, thisTypeRef, valueTypeRef, null, null,
module.load(valueType.byteSize, valueType.isSignedIntegerValue,
module.local_get(0, thisTypeRef),
valueTypeRef, instance.memoryOffset
Expand Down Expand Up @@ -1724,7 +1728,10 @@ export class Compiler extends DiagnosticEmitter {
], TypeRef.None);
}
}
instance.setterRef = module.addFunction(instance.internalSetterName, createType([ thisTypeRef, valueTypeRef ]), TypeRef.None, null,
instance.setterRef = module.addFunction(
instance.internalSetterName,
createType([ thisTypeRef, valueTypeRef ]),
TypeRef.None, null, null,
MaxGraey marked this conversation as resolved.
Show resolved Hide resolved
bodyExpr
);
if (instance.getterRef) {
Expand Down Expand Up @@ -6565,6 +6572,7 @@ export class Compiler extends DiagnosticEmitter {
stub.signature.paramRefs,
stub.signature.resultRefs,
typesToRefs(stub.additionalLocals),
null,
module.flatten(stmts, returnType.toRef())
);
stub.set(CommonFlags.COMPILED);
Expand All @@ -6588,7 +6596,7 @@ export class Compiler extends DiagnosticEmitter {
stub.internalName,
stub.signature.paramRefs,
stub.signature.resultRefs,
null,
null, null,
MaxGraey marked this conversation as resolved.
Show resolved Hide resolved
module.unreachable()
);
this.virtualStubs.add(original);
Expand Down Expand Up @@ -6713,6 +6721,7 @@ export class Compiler extends DiagnosticEmitter {
stub.signature.paramRefs,
stub.signature.resultRefs,
[ TypeRef.I32 ],
null,
module.block(null, [
builder.render(tempIndex),
body
Expand Down Expand Up @@ -8610,6 +8619,7 @@ export class Compiler extends DiagnosticEmitter {
signature.paramRefs,
signature.resultRefs,
varTypes,
instance.localsByIndex.map<string>(local => local.name),
module.flatten(stmts, sizeTypeRef)
);
instance.finalize(module, funcRef);
Expand Down
19 changes: 19 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1896,6 +1896,7 @@ export class Module {
params: TypeRef,
results: TypeRef,
varTypes: TypeRef[] | null,
localNames: string[] | null,
body: ExpressionRef
): FunctionRef {
var cStr = this.allocStringCached(name);
Expand All @@ -1910,9 +1911,27 @@ export class Module {
body
);
binaryen._free(cArr);
this.setLocalNames(ret, localNames);
HerrCai0907 marked this conversation as resolved.
Show resolved Hide resolved
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
2 changes: 1 addition & 1 deletion src/passes/ministack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class MiniStack extends Pass {
module.local_get(numParams, results)
);
}
module.addFunction(wrapperName, params, results, vars,
module.addFunction(wrapperName, params, results, vars, null,
module.block(null, stmts, results)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/passes/shadowstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export class ShadowStackPass extends Pass {
var module = this.module;
if (!this.hasStackCheckFunction) {
this.hasStackCheckFunction = true;
module.addFunction("~stack_check", TypeRef.None, TypeRef.None, null,
module.addFunction("~stack_check", TypeRef.None, TypeRef.None, null, null,
module.if(
module.binary(BinaryOp.LtI32,
module.global_get(BuiltinNames.stack_pointer, this.ptrType),
Expand Down
Loading