From 08bac19b8a837f3d5d33ed11b9818f402d6ddc1d Mon Sep 17 00:00:00 2001 From: hufeng Date: Wed, 2 Mar 2022 16:33:16 +0800 Subject: [PATCH] opt debuginfo Signed-off-by: hufeng Change-Id: I72f9023bb7db08fbaed1d798bf1ae541ec0f151b --- ts2panda/src/debuginfo.ts | 126 ++++++++--------------- ts2panda/src/scope.ts | 21 ++-- ts2panda/src/statement/classStatement.ts | 2 +- ts2panda/src/ts2panda.ts | 12 +-- ts2panda/templates/irnodes.ts.erb | 30 +++++- 5 files changed, 84 insertions(+), 107 deletions(-) diff --git a/ts2panda/src/debuginfo.ts b/ts2panda/src/debuginfo.ts index 0519b3ba20..67f03b09b2 100644 --- a/ts2panda/src/debuginfo.ts +++ b/ts2panda/src/debuginfo.ts @@ -18,7 +18,8 @@ import { CmdOptions } from "./cmdOptions"; import { CalliDynRange, CallRange, - DebugInsPlaceHolder, + DebugInsStartPlaceHolder, + DebugInsEndPlaceHolder, IRNode, Label, VReg @@ -31,11 +32,10 @@ import { } from "./variable"; export class DebugPosInfo { - private bl: number | undefined = 0; // bound left - private br: number | undefined = 0; // bound right + private bl: number | undefined; // bound left + private br: number | undefined; // bound right private l: number = -1; // line number private c: number = -1; // column number - private w: string | undefined = ""; // whole line private nodeKind: NodeKind | undefined = NodeKind.FirstNodeOfFunction; constructor() { } @@ -84,22 +84,7 @@ export class DebugPosInfo { return this.c; } - public setWholeLine(wholeLine: string): void { - this.w = wholeLine; - } - - public getWholeLine(): string | undefined { - return this.w; - } - - public ClearMembersForReleaseBuild(): void { - this.ClearMembersForDebugBuild(); - this.bl = undefined; - this.br = undefined; - } - - public ClearMembersForDebugBuild(): void { - this.w = undefined; + public ClearNodeKind(): void { this.nodeKind = undefined; } } @@ -185,17 +170,14 @@ export class DebugInfo { loc: { line : -1, character : -1 - }, - wholeLineText: "" + } } } pos = node.getStart(); let loc = file.getLineAndCharacterOfPosition(pos); - let wholeLineText = node.getText(); return { - loc: loc, - wholeLineText: wholeLineText + loc: loc } } @@ -208,7 +190,6 @@ export class DebugInfo { } posInfo.setSourecLineNum(res.loc.line); posInfo.setSourecColumnNum(res.loc.character); - posInfo.setWholeLine(res.wholeLineText); } } @@ -229,27 +210,21 @@ export class DebugInfo { let lineNumber = -1; let columnNumber = -1; - let wholeLineText = ""; if (DebugInfo.isNode(node)) { let tsNode = (node); let res = this.searchForPos(tsNode); if (!res) { return; } - wholeLineText = res.wholeLineText; lineNumber = res.loc.line; columnNumber = res.loc.character; } - for (let i = 0; i < insns.length; i++) { - let pos = new DebugPosInfo(); - pos.setSourecLineNum(lineNumber); - pos.setSourecColumnNum(columnNumber); - pos.setWholeLine(wholeLineText); - pos.setDebugPosInfoNodeState(node); - - insns[i].debugPosInfo = pos; - } + insns.forEach(insn => { + insn.debugPosInfo.setSourecLineNum(lineNumber); + insn.debugPosInfo.setSourecColumnNum(columnNumber); + insn.debugPosInfo.setDebugPosInfoNodeState(node); + }) } private static matchFormat(irnode: IRNode): number { @@ -270,7 +245,9 @@ export class DebugInfo { } private static getIRNodeWholeLength(irnode: IRNode): number { - if (irnode instanceof Label || irnode instanceof DebugInsPlaceHolder) { + if (irnode instanceof Label || + irnode instanceof DebugInsStartPlaceHolder || + irnode instanceof DebugInsEndPlaceHolder) { return 0; } let length = 1; @@ -293,49 +270,20 @@ export class DebugInfo { return length; } - private static setVariablesDebugInfoInternal(pandaGen: PandaGen, scope: Scope) { - let insns = pandaGen.getInsns(); - // count variables offset - let startIdx = 0; - let startIns = scope.getScopeStartIns(); - let endIns = scope.getScopeEndIns(); - - for (let i = 0; i < insns.length; i++) { - if (startIns == insns[i]) { - startIdx = i; - } - - if (endIns == insns[i]) { - let name2variable = scope.getName2variable(); - name2variable.forEach((value, key) => { - if (!value.hasAlreadyBinded()) { - return; - } - let variableInfo = new VariableDebugInfo(key, "any", "any", (value.getVreg().num)); - variableInfo.setStart(startIdx); - variableInfo.setLength(i - startIdx + 1); - pandaGen.addDebugVariableInfo(variableInfo); - }); - } - } - } - private static setPosDebugInfo(pandaGen: PandaGen) { - let insns = pandaGen.getInsns(); + let insns: IRNode[] = pandaGen.getInsns(); let offset = 0; + // count pos offset for (let i = 0; i < insns.length; i++) { if (insns[i].debugPosInfo.getDebugPosInfoNodeState() == NodeKind.FirstNodeOfFunction) { DebugInfo.setPosInfoForUninitializeIns(insns[i].debugPosInfo, pandaGen); } - } - // count pos offset - for (let i = 0; i < insns.length; i++) { let insLength = DebugInfo.getIRNodeWholeLength(insns[i]); let insnsDebugPosInfo = insns[i].debugPosInfo; - if (insnsDebugPosInfo) { + if (insnsDebugPosInfo && CmdOptions.isDebugMode()) { insnsDebugPosInfo.setBoundLeft(offset); insnsDebugPosInfo.setBoundRight(offset + insLength); } @@ -348,20 +296,36 @@ export class DebugInfo { } } - private static removeDebugIns(pandaGen: PandaGen) { + private static setVariablesDebugInfo(pandaGen: PandaGen) { let insns = pandaGen.getInsns(); + for (let i = 0; i < insns.length; i++) { - if (insns[i] instanceof DebugInsPlaceHolder) { + if (insns[i] instanceof DebugInsStartPlaceHolder) { + ( insns[i]).getScope().setScopeStartInsIdx(i); + // delete ins placeholder + insns.splice(i, 1); + i--; + } + if (insns[i] instanceof DebugInsEndPlaceHolder) { + ( insns[i]).getScope().setScopeEndInsIdx(i); + // delete ins placeholder insns.splice(i, 1); i--; } } - } - private static setVariablesDebugInfo(pandaGen: PandaGen) { let recordArray = DebugInfo.getScopeArray(); recordArray.forEach(scope => { - DebugInfo.setVariablesDebugInfoInternal(pandaGen, scope); + let name2variable = scope.getName2variable(); + name2variable.forEach((value, key) => { + if (!value.hasAlreadyBinded()) { + return; + } + let variableInfo = new VariableDebugInfo(key, "any", "any", (value.getVreg().num)); + variableInfo.setStart(scope.getScopeStartInsIdx()); + variableInfo.setLength(scope.getScopeEndInsIdx() - scope.getScopeStartInsIdx()); + pandaGen.addDebugVariableInfo(variableInfo); + }); }); } @@ -372,9 +336,6 @@ export class DebugInfo { // set variable debug info DebugInfo.setVariablesDebugInfo(pandaGen); - // delete ins placeholder - DebugInfo.removeDebugIns(pandaGen) - // clear scope array DebugInfo.clearScopeArray(); return; @@ -405,14 +366,15 @@ export class DebugInfo { if (!CmdOptions.isDebugMode()) { return; } + let insns = pandaGen.getInsns(); - let placeHolder = new DebugInsPlaceHolder(); - insns.push(placeHolder); + let placeHolder: IRNode; if (isStart) { - scope.setScopeStartIns(placeHolder); + placeHolder = new DebugInsStartPlaceHolder(scope); DebugInfo.addScope(scope); } else { - scope.setScopeEndIns(placeHolder); + placeHolder = new DebugInsEndPlaceHolder(scope); } + insns.push(placeHolder); } } diff --git a/ts2panda/src/scope.ts b/ts2panda/src/scope.ts index 79a59c0486..209c246c5c 100644 --- a/ts2panda/src/scope.ts +++ b/ts2panda/src/scope.ts @@ -14,7 +14,6 @@ */ import * as ts from "typescript"; -import { DebugInsPlaceHolder } from "./irnodes"; import { LOGD, LOGE } from "./log"; import { GlobalVariable, @@ -86,8 +85,8 @@ export abstract class Scope { protected decls: Decl[] = []; protected parent: Scope | undefined = undefined; // for debuginfo - protected startIns: DebugInsPlaceHolder = new DebugInsPlaceHolder(); - protected endIns: DebugInsPlaceHolder = new DebugInsPlaceHolder(); + protected startInsIdx: number | undefined; + protected endInsIdx: number | undefined; constructor() { } abstract add(name: string, declKind: VarDeclarationKind, status?: InitStatus): Variable | undefined; @@ -96,20 +95,20 @@ export abstract class Scope { return this.name2variable; } - getScopeStartIns() { - return this.startIns; + getScopeStartInsIdx() { + return this.startInsIdx; } - setScopeStartIns(startIns: DebugInsPlaceHolder) { - this.startIns = startIns; + setScopeStartInsIdx(startInsIdx: number) { + this.startInsIdx = startInsIdx; } - setScopeEndIns(endIns: DebugInsPlaceHolder) { - this.endIns = endIns; + setScopeEndInsIdx(endInsIdx: number) { + this.endInsIdx = endInsIdx; } - getScopeEndIns() { - return this.endIns; + getScopeEndInsIdx() { + return this.endInsIdx; } setParent(parentScope: Scope | undefined) { diff --git a/ts2panda/src/statement/classStatement.ts b/ts2panda/src/statement/classStatement.ts index 26d86846d4..880f4a5f64 100644 --- a/ts2panda/src/statement/classStatement.ts +++ b/ts2panda/src/statement/classStatement.ts @@ -608,7 +608,7 @@ function generatePropertyFromExpr(node: ts.ClassLikeDeclaration, classFields: Ar let staticItems = properties.slice(properties.length - staticNum) properties = properties.slice(0, properties.length - staticNum); properties = properties.reverse(); - properties = properties.concat(staticItems); + properties.push(...staticItems); if (constructNode) { defineClassMember("constructor", constructNode, PropertyKind.Variable, properties, namedPropertyMap); diff --git a/ts2panda/src/ts2panda.ts b/ts2panda/src/ts2panda.ts index 34069c20e3..dd1555e4fd 100644 --- a/ts2panda/src/ts2panda.ts +++ b/ts2panda/src/ts2panda.ts @@ -14,7 +14,6 @@ */ import { CmdOptions } from "./cmdOptions"; -import { DebugPosInfo } from "./debuginfo"; import { Imm, IRNode, @@ -74,7 +73,6 @@ export class Ts2Panda { let insIds: Array = []; let insImms: Array = []; let insLabel: string = ""; - let insDebugInfo: DebugPosInfo = new DebugPosInfo(); if (insn instanceof Label) { insLabel = Ts2Panda.labelPrefix + insn.id; @@ -104,12 +102,8 @@ export class Ts2Panda { } }); } - insDebugInfo = insn.debugPosInfo; - if (CmdOptions.isDebugMode()) { - insDebugInfo.ClearMembersForDebugBuild(); - } else { - insDebugInfo.ClearMembersForReleaseBuild(); - } + + insn.debugPosInfo.ClearNodeKind(); insns.push(new Ins( insOpcode, @@ -117,7 +111,7 @@ export class Ts2Panda { insIds.length == 0 ? undefined : insIds, insImms.length == 0 ? undefined : insImms, insLabel === "" ? undefined : insLabel, - insDebugInfo, + insn.debugPosInfo, )); }); diff --git a/ts2panda/templates/irnodes.ts.erb b/ts2panda/templates/irnodes.ts.erb index 19b5f1029a..40cbc33f37 100755 --- a/ts2panda/templates/irnodes.ts.erb +++ b/ts2panda/templates/irnodes.ts.erb @@ -173,6 +173,7 @@ import { DebugPosInfo, NodeKind } from "./debuginfo"; +import { Scope } from "./scope"; export enum IRNodeKind { % Panda::instructions.group_by(&:mnemonic).each do |mnemonic, group| @@ -181,7 +182,8 @@ export enum IRNodeKind { VREG, IMM, LABEL, - VIRTUALINS_DYN, + VIRTUALSTARTINS_DYN, + VIRTUALENDINS_DYN, DEFINE_GLOBAL_VAR, } @@ -396,9 +398,29 @@ export class Label extends IRNode { } } -export class DebugInsPlaceHolder extends IRNode { - constructor() { - super(IRNodeKind.VIRTUALINS_DYN, []); +export class DebugInsStartPlaceHolder extends IRNode { + private scope: Scope; + + constructor(scope: Scope) { + super(IRNodeKind.VIRTUALSTARTINS_DYN, []); + this.scope = scope; + } + + getScope() { + return this.scope; + } +} + +export class DebugInsEndPlaceHolder extends IRNode { + private scope: Scope; + + constructor(scope: Scope) { + super(IRNodeKind.VIRTUALENDINS_DYN, []); + this.scope = scope; + } + + getScope() { + return this.scope; } } -- Gitee