From 1bd1e679532af0edf0a7ee410778ded2d56bde70 Mon Sep 17 00:00:00 2001 From: ctw-ian Date: Fri, 16 Dec 2022 10:33:26 +0800 Subject: [PATCH] Apply new ins for report reference error Issue:I6AXND Signed-off-by: ctw-ian Change-Id: I49ef7978a5b7a8f762a44fa656a55a358ff8d8ad --- es2panda/compiler/core/pandagen.cpp | 9 +---- testTs/instype/recordimport-expected.txt | 2 +- ts2panda/src/base/bcGenUtil.ts | 6 +-- ts2panda/src/compiler.ts | 18 ++------- ts2panda/src/lexenv.ts | 32 +++------------ ts2panda/src/pandagen.ts | 4 +- ts2panda/tests/esmodule.test.ts | 18 ++------- ts2panda/tests/lexenv.test.ts | 50 +----------------------- 8 files changed, 20 insertions(+), 119 deletions(-) diff --git a/es2panda/compiler/core/pandagen.cpp b/es2panda/compiler/core/pandagen.cpp index 605cd7defa..55a8cfef33 100644 --- a/es2panda/compiler/core/pandagen.cpp +++ b/es2panda/compiler/core/pandagen.cpp @@ -1848,14 +1848,7 @@ void PandaGen::ThrowIfSuperNotCorrectCall(const ir::AstNode *node, int64_t num) void PandaGen::ThrowUndefinedIfHole(const ir::AstNode *node, const util::StringView &name) { - RegScope rs(this); - VReg holeReg = AllocReg(); - StoreAccumulator(node, holeReg); - LoadAccumulatorString(node, name); - VReg nameReg = AllocReg(); - StoreAccumulator(node, nameReg); - ra_.Emit(node, holeReg, nameReg); - LoadAccumulator(node, holeReg); + ra_.Emit(node, name); strings_.insert(name); } diff --git a/testTs/instype/recordimport-expected.txt b/testTs/instype/recordimport-expected.txt index 5faeeb0a8e..8dbed07d51 100644 --- a/testTs/instype/recordimport-expected.txt +++ b/testTs/instype/recordimport-expected.txt @@ -9,4 +9,4 @@ Handle types for function: minus Handle types for function: func_main_0 (instruction order, type): (8, 101), (10, 1), (12, 103), (14, 104), (16, 1), (18, 1), (20, 1), (22, 4), (27, 102), (59, 105), Handle types for function: func_main_0 -(instruction order, type): (5, 101), (12, 105), (27, 107), (34, 102), (41, 103), (51, 104), (61, 106), (69, 106), +(instruction order, type): (5, 101), (8, 105), (19, 107), (22, 102), (25, 103), (31, 104), (37, 106), (41, 106), diff --git a/ts2panda/src/base/bcGenUtil.ts b/ts2panda/src/base/bcGenUtil.ts index 7f87a6533d..22ae1ca7db 100644 --- a/ts2panda/src/base/bcGenUtil.ts +++ b/ts2panda/src/base/bcGenUtil.ts @@ -84,7 +84,7 @@ import { ThrowIfsupernotcorrectcall, ThrowPatternnoncoercible, ThrowNotexists, - ThrowUndefinedifhole, + ThrowUndefinedifholewithname, Tryldglobalbyname, Trystglobalbyname, Fldai, @@ -165,8 +165,8 @@ export function throwConstAssignment(name: VReg) { return new ThrowConstassignment(name); } -export function throwUndefinedIfHole(hole: VReg, name: VReg) { - return new ThrowUndefinedifhole(hole, name); +export function throwUndefinedIfHole(name: string) { + return new ThrowUndefinedifholewithname(name); } export function throwThrowNotExists() { diff --git a/ts2panda/src/compiler.ts b/ts2panda/src/compiler.ts index 29eea7514b..09c3a4ac27 100644 --- a/ts2panda/src/compiler.ts +++ b/ts2panda/src/compiler.ts @@ -1470,16 +1470,11 @@ export class Compiler { if ((variable.v.isLet() || variable.v.isClass()) && !variable.v.isInitialized()) { let valueReg = this.pandaGen.getTemp(); - let holeReg = this.pandaGen.getTemp(); - let nameReg = this.pandaGen.getTemp(); this.pandaGen.storeAccumulator(node, valueReg); this.pandaGen.loadModuleVariable(node, variable.v, true); - this.pandaGen.storeAccumulator(node, holeReg); - this.pandaGen.loadAccumulatorString(node, variable.v.getName()); - this.pandaGen.storeAccumulator(node, nameReg); - this.pandaGen.throwUndefinedIfHole(node, holeReg, nameReg); + this.pandaGen.throwUndefinedIfHole(node, variable.v.getName()); this.pandaGen.loadAccumulator(node, valueReg); - this.pandaGen.freeTemps(valueReg, holeReg, nameReg); + this.pandaGen.freeTemps(valueReg); } this.pandaGen.storeModuleVariable(node, variable.v); @@ -1516,14 +1511,7 @@ export class Compiler { let isLocal: boolean = variable.v.isExportVar() ? true : false; this.pandaGen.loadModuleVariable(node, variable.v, isLocal); if ((variable.v.isLetOrConst() || variable.v.isClass()) && !variable.v.isInitialized()) { - let valueReg = this.pandaGen.getTemp(); - let nameReg = this.pandaGen.getTemp(); - this.pandaGen.storeAccumulator(node, valueReg); - this.pandaGen.loadAccumulatorString(node, variable.v.getName()); - this.pandaGen.storeAccumulator(node, nameReg); - this.pandaGen.throwUndefinedIfHole(node, valueReg, nameReg); - this.pandaGen.loadAccumulator(node, valueReg); - this.pandaGen.freeTemps(valueReg, nameReg); + this.pandaGen.throwUndefinedIfHole(node, variable.v.getName()); } } else { // Handle the variables from lexical scope diff --git a/ts2panda/src/lexenv.ts b/ts2panda/src/lexenv.ts index aba261946a..0b9ab61236 100644 --- a/ts2panda/src/lexenv.ts +++ b/ts2panda/src/lexenv.ts @@ -84,11 +84,8 @@ export class VariableAccessLoad extends VariableAccessBase { // check TDZ first if (!(v).isInitialized()) { - let holeReg = pandaGen.getTemp(); insns.push(loadAccumulator(getVregisterCache(pandaGen, CacheList.HOLE))); - insns.push(storeAccumulator(holeReg)); - checkTDZ(pandaGen, holeReg, v.getName(), insns); - pandaGen.freeTemps(holeReg); + insns.push(throwUndefinedIfHole(v.getName())); return insns; } insns.push(loadAccumulator(bindVreg)); @@ -105,12 +102,7 @@ export class VariableAccessLoad extends VariableAccessBase { // check TDZ if (v.isLetOrConst() || v.isClass()) { - let tempReg = pandaGen.getTemp(); - - insns.push(storeAccumulator(tempReg)); - checkTDZ(pandaGen, tempReg, v.getName(), insns); - insns.push(loadAccumulator(tempReg)); - pandaGen.freeTemps(tempReg); + insns.push(throwUndefinedIfHole(v.getName())); } return insns; @@ -142,15 +134,12 @@ export class VariableAcessStore extends VariableAccessBase { if (!this.isDeclaration) { // check TDZ first if (!v.isInitialized()) { - let nameReg = pandaGen.getTemp(); let tempReg = pandaGen.getTemp(); - let holeReg = pandaGen.getTemp(); insns.push(storeAccumulator(tempReg)); insns.push(loadAccumulator(getVregisterCache(pandaGen, CacheList.HOLE))); - insns.push(storeAccumulator(holeReg)); - checkTDZ(pandaGen, holeReg, v.getName(), insns); + insns.push(throwUndefinedIfHole(v.getName())); insns.push(loadAccumulator(tempReg)); - pandaGen.freeTemps(nameReg, tempReg, holeReg); + pandaGen.freeTemps(tempReg); } // check const assignment @@ -175,19 +164,16 @@ export class VariableAcessStore extends VariableAccessBase { let slot = v.idxLex; if (v.isLetOrConst() || v.isClass()) { if (!this.isDeclaration) { - let holeReg = pandaGen.getTemp(); /** * check TDZ first * If acc == hole -> throw reference error * else -> execute the next insn */ insns.push(loadLexicalVar(this.level, slot)); - insns.push(storeAccumulator(holeReg)); - checkTDZ(pandaGen, holeReg, v.getName(), insns); + insns.push(throwUndefinedIfHole(v.getName())) // const assignment check need to be down after TDZ check checkConstAssignment(pandaGen, v, insns, this.node); - pandaGen.freeTemps(holeReg); } } @@ -201,14 +187,6 @@ export class VariableAcessStore extends VariableAccessBase { } } -function checkTDZ(pg: PandaGen, holeReg: VReg, name: string, expansion: IRNode[]) { - let nameReg = pg.getTemp(); - expansion.push(loadAccumulatorString(name)); - expansion.push(storeAccumulator(nameReg)); - expansion.push(throwUndefinedIfHole(holeReg, nameReg)); - pg.freeTemps(nameReg); -} - function checkConstAssignment(pg: PandaGen, v: Variable, expansion: IRNode[], node: ts.Node | NodeKind) { let nameReg = pg.getTemp(); if (v.isConst()) { diff --git a/ts2panda/src/pandagen.ts b/ts2panda/src/pandagen.ts index 55d6ed1f4b..708f382e76 100644 --- a/ts2panda/src/pandagen.ts +++ b/ts2panda/src/pandagen.ts @@ -830,10 +830,10 @@ export class PandaGen { this.add(node, creatDebugger()); } - throwUndefinedIfHole(node: ts.Node, hole: VReg, name: VReg) { + throwUndefinedIfHole(node: ts.Node, name: string) { this.add( node, - throwUndefinedIfHole(hole, name) + throwUndefinedIfHole(name) ) } diff --git a/ts2panda/tests/esmodule.test.ts b/ts2panda/tests/esmodule.test.ts index 9014994d5d..eee0bb6186 100644 --- a/ts2panda/tests/esmodule.test.ts +++ b/ts2panda/tests/esmodule.test.ts @@ -23,7 +23,7 @@ import { Defineclasswithbuffer, Returnundefined, Stmodulevar, - ThrowUndefinedifhole, + ThrowUndefinedifholewithname, Imm, Lda, LdaStr, @@ -68,16 +68,10 @@ describe("ExportDeclaration", function () { snippetCompiler.compile(`import a from 'test.js'; let v = a; export {a};`); CmdOptions.isModules = () => {return false}; let funcMainInsns = snippetCompiler.getGlobalInsns(); - let a = new VReg(); let v = new VReg(); - let name = new VReg(); let expected = [ new Ldexternalmodulevar(new Imm(0)), - new Sta(a), - new LdaStr("a"), - new Sta(name), - new ThrowUndefinedifhole(a, name), - new Lda(a), + new ThrowUndefinedifholewithname("a"), new Sta(v), new Returnundefined(), ]; @@ -102,20 +96,14 @@ describe("ExportDeclaration", function () { IRNode.pg = new PandaGen("test", creatAstFromSnippet(""), 0, undefined); let funcMainInsns = snippetCompiler.getGlobalInsns(); let graphicsAssemblerManager = new VReg(); - let Graphics = new VReg(); let v = new VReg(); - let name = new VReg(); let expected = [ new Createobjectwithbuffer(new Imm(0), "snippet_1"), new Sta(graphicsAssemblerManager), new Lda(graphicsAssemblerManager), new Stmodulevar(new Imm(0)), new Ldexternalmodulevar(new Imm(0)), - new Sta(Graphics), - new LdaStr("Graphics"), - new Sta(name), - new ThrowUndefinedifhole(Graphics, name), - new Lda(Graphics), + new ThrowUndefinedifholewithname("Graphics"), new Sta(v), new Mov(graphicsAssemblerManager, v), new Ldlocalmodulevar(new Imm(0)), diff --git a/ts2panda/tests/lexenv.test.ts b/ts2panda/tests/lexenv.test.ts index c53f3d386f..ac103f52d5 100644 --- a/ts2panda/tests/lexenv.test.ts +++ b/ts2panda/tests/lexenv.test.ts @@ -29,15 +29,13 @@ import { Stglobalvar, Sttoglobalrecord, Stlexvar, - ThrowConstassignment, - ThrowUndefinedifhole, + ThrowUndefinedifholewithname, Tonumeric, Trystglobalbyname, Imm, IRNode, Lda, Ldai, - LdaStr, Return, Sta, VReg @@ -54,7 +52,6 @@ import { GlobalVariable, LocalVariable, VarDeclarationKind, - Variable } from "../src/variable"; import { creatAstFromSnippet } from "./utils/asthelper"; import { @@ -63,43 +60,6 @@ import { SnippetCompiler } from "./utils/base"; - - -function MicroStoreLexVar(level: number, slot: number, kind?: VarDeclarationKind, name?: string): IRNode[] { - let insns = []; - - if (kind && name) { - insns.push(new Ldlexvar(new Imm(level), new Imm(slot))); - insns.push(new Sta(new VReg())); - insns.push(new LdaStr(name)); - insns.push(new Sta(new VReg())); - insns.push(new ThrowUndefinedifhole(new VReg(), new VReg())); - if (kind == VarDeclarationKind.CONST) { - insns.push(new ThrowConstassignment(new VReg())); - } - } - insns.push(new Lda(new VReg())); - insns.push(new Stlexvar(new Imm(level), new Imm(slot))); - insns.push(new Lda(new VReg())); - - return insns; -} - -function MicroLoadLexVar(level: number, slot: number, kind?: VarDeclarationKind, name?: string): IRNode[] { - let insns = []; - - insns.push(new Ldlexvar(new Imm(level), new Imm(slot))); - if (kind && name) { - insns.push(new Sta(new VReg())); - insns.push(new LdaStr(name)); - insns.push(new Sta(new VReg())); - insns.push(new ThrowUndefinedifhole(new VReg(), new VReg())); - insns.push(new Lda(new VReg())); - } - - return insns; -} - describe("lexenv-compile-testcase in lexenv.test.ts", function () { it("test CompilerDriver.scanFunctions-with-empty", function () { @@ -311,15 +271,9 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { pass.run(pandaGen); let outInsns = pandaGen.getInsns(); - let tempReg = new VReg(); - let nameReg = new VReg(); let expected = [ new Ldlexvar(new Imm(0), new Imm(0)), - new Sta(tempReg), - new LdaStr("var1"), - new Sta(nameReg), - new ThrowUndefinedifhole(new VReg(), nameReg), - new Lda(tempReg) + new ThrowUndefinedifholewithname("var1"), ]; expect(checkInstructions(outInsns, expected)).to.be.true; }); -- Gitee