diff --git a/es2panda/compiler/core/pandagen.cpp b/es2panda/compiler/core/pandagen.cpp index 605cd7defa887ebcc3235654d5cae487c1a6a263..55a8cfef33e420d129bd8dfb59688177c666d62a 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 5faeeb0a8e65b52a50e3754ed198f18d540cecab..8dbed07d51f62ef2b0ab6577aa760f6364951f4a 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 7f87a6533d1a667f9c8a34f72577c9192c78a6f1..22ae1ca7dbcc873c90b8ea4c1b06042631445ee5 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 29eea7514b10fb206bb0490c2f87422c3d6af368..09c3a4ac27c479b54def86350840c084ea804e72 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 aba261946ab94d1da53213d7102dc0a09e828380..0b9ab61236053577fe8527f70cf3098e0ba4d5fe 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 55d6ed1f4bb6cec4bdcb1bbddb660bcd47742192..708f382e764ac8cc6bd206799ff73e43cb1579ab 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 9014994d5dc568a8948f07315afcf00a1e82c5ab..eee0bb618619b6fba79948f62738e4642545e18b 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 c53f3d386f43df40c265567481927fce95fa6ba8..ac103f52d52659c5e2270eaf7fbab2b5a395f2df 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; });