From 81185cf7ef4576be0c0a5e31091205e8a79df7b3 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Tue, 13 Sep 2022 10:32:30 +0800 Subject: [PATCH 01/10] Add HowToWriteProtoForAssemblyStuff.md Signed-off-by: zhangrengao Change-Id: Iad80fb375ca1a61bd6ee088fe63b95846812691c --- merge_abc/HowToWriteProtoForAssemblyStuff.md | 281 +++++++++++++++++++ 1 file changed, 281 insertions(+) create mode 100644 merge_abc/HowToWriteProtoForAssemblyStuff.md diff --git a/merge_abc/HowToWriteProtoForAssemblyStuff.md b/merge_abc/HowToWriteProtoForAssemblyStuff.md new file mode 100644 index 0000000000..83f76ebe34 --- /dev/null +++ b/merge_abc/HowToWriteProtoForAssemblyStuff.md @@ -0,0 +1,281 @@ +# How To Write Proto For Assembly + +## 消息格式 + +``` +syntax = "proto3" // 指定版本信息 +message Test // message为关键字,作用为定义一种消息类型 +{ + string test = 1; +} +``` + +消息由字段组合而成 +``` +限定修饰符 数据类型 字段名称 = 唯一编号标签值 +``` + +## 限定修饰符说明 + +### required + +required 在发送消息之前必须设置该字段的值。对于接收方,必须能够识别该字段的意思。发送之前没有设置required字段或者无法识别required字段都会引发编解码异常,导致消息被丢弃。 + +``` +syntax = "proto3" +message Test +{ + required string test = 1; +} +``` + +### repeated + +repeated 代表可重复,我们可以理解为数组 + +``` +syntax = "proto3" +message Test +{ + string test = 1; +} + +message TestArr +{ + repeated Test arr = 1; +} +``` + +### optional + +optional 表示是一个可选字段,可选对于发送方,在发送消息时,可以有选择性的设置或者不设置该字段的值。如果无法识别,则忽略该字段,消息中的其它字段正常处理。 + +## 数据类型 + +| proto类型 | C++类型 | 备注 | +|:-----------|:---------------|:------------| +|double |double | 64位浮点数 +|float |float | 32位浮点数 +|int32 |int32 | 32位整数 +|int64 |int64 | 64位整数 +|uint32 |uint32 | 32位无符号整数 +|uint64 |uint64 | 64位无符号整数 +|sint32 |int32 | 32位整数,处理负数效率比int32更高 +|sint32 |sint64 | 64位整数,处理负数效率比int64更高 +|fixed32 | uint32 | 总是4个字节。如果数值总是比总是比228大的话,这个类型会比uint32高效。 +|fixed64 | uint64 | 总是8个字节。如果数值总是比总是比256大的话,这个类型会比uint64高效。 +|sfixed32 | int32 | 总是4个字节 +|sfixed64 | int64 | 总是8个字节 +|bool | bool | 布尔类型 +|string | string | 一个字符串必须是UTF-8编码或者7-bit ASCII编码的文本 +|bytes | string | 处理多字节的语言字符、如中文 +|enum | enum | 枚举(proto2 从1开始,proto3从0开始) +|message | object of class | 自定义的消息类型 + + +## 字段名称 + +protobuf建议以下划线命名而非驼峰式 + +## 唯一的编号标签 + +代表每个字段的一个唯一的编号标签,在同一个消息里不可以重复。这些编号标签用与在消息二进制格式中标识你的字段,并且消息一旦定义就不能更改。需要说明的是标签在1到15范围的采用一个字节进行编码,所以通常将标签1到15用于频繁发生的消息字段。编号标签大小的范围是1到229。此外不能使用protobuf系统预留的编号标签(19000 ~19999) + +## import + +protobuf 接口文件可以像C语言的h文件一个,分离为多个,在需要的时候通过 import导入需要对文件。其行为和C语言的#include或者java的import的行为大致相同。 + +## enum + +``` +syntax = "proto3" // 指定版本信息 +enum COLOR +{ + RED = 0; + BLUE = 1; + YELLOW = 2; +} + +message Test // message为关键字,作用为定义一种消息类型 +{ + string test = 1; + COLOR type = 2; +} +``` + +## package + +``` +syntax = "proto3" // 指定版本信息 + +package tutorial; + +message Test // message为关键字,作用为定义一种消息类型 +{ + string test = 1; +} +``` + +## oneof + +message 包含许多可选字段,并且最多只能同时设置其中一个字段,则可以使用 oneof 功能强制执行此行为并节省内存 + +# Assembly对应Proto + +## class\struct + +protobuf中将类型信息结构化处理,通过自定义消息message结构对C++中class或struct进行解析处理 + +C++ +``` +struct Ins { + size_t line_number = 0; + uint32_t column_number = 0; + std::string whole_line = ""; // TODO(mbolshov): redundant given file and line_number + size_t bound_left = 0; + size_t bound_right = 0; +} +``` + +proto +``` +message DebuginfoIns { + uint64 lineNumber = 1; + uint32 columnNumber = 2; + bytes wholeLine = 3; + uint64 boundLeft = 4; + uint64 boundRight = 5; +} +``` + + +## enum + +protobuf中存在enum类型,proto2 从1开始,proto3从0开始 + +对于Assembly中的转换,优化序列化速度,通过uint32位直接进行转换: + +C++ +``` +class Value { +public: + enum class Type { + U1, + I8, + U8, + I16, + U16, + I32, + U32, + I64, + U64, + F32, + F64, + STRING, + STRING_NULLPTR, + RECORD, + METHOD, + ENUM, + ANNOTATION, + ARRAY, + VOID, + METHOD_HANDLE, + UNKNOWN + }; + ... +} + +``` + + +proto +``` +message Value { + uint32 type = 1; +} +``` +## inheritance + +protobuf中message无继承信息,开发者自己实现继承信息 + +C++ +``` +class Value { +... +} + +class ArrayValue : public Value { +... +} +``` + +proto +``` + +message Value { + uint32 type = 1; +} + +message ArrayValue { + Value father = 1; +} + +``` + +## std::variant + +通过oneof关键字实现std::variant + +C++ +``` +std::variant value_; +``` + +proto +``` +oneof value { + uint64 valueU64 = 2; + float valueFloat = 3; + double valueDouble = 4; + bytes valueStr = 5; + Type valueType = 6; + AnnotationData valueAnno = 7; +} +``` + +## unique_ptr + +查看源码中的变量关系,通过oneof实现智能指针 + +C++ +``` +class Value { +} + +class ArrayValue { +} + +class ScalarValue { +} + +std::unique_ptr value_; +``` + +proto +``` +message Value { +} + +message ScalarValue { + Value father = 1; +} + +message ArrayValue { + Value father = 1; +} + +oneof value { + ScalarValue scalar = 2; + ArrayValue array = 3; +} +``` -- Gitee From 20add69ff36d8bb79e9bdd7c2f9e423710c236d3 Mon Sep 17 00:00:00 2001 From: gavin1012_hw Date: Tue, 6 Sep 2022 10:58:17 +0800 Subject: [PATCH 02/10] Support serialize proto bin file in ts2abc Signed-off-by: gavin1012_hw Change-Id: I63bab0c084d4b1bbd22dd8cb5f39dc70165266ce --- ts2panda/src/cmdOptions.ts | 7 +++++++ ts2panda/src/ts2panda.ts | 3 ++- ts2panda/ts2abc/ts2abc_options.h | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ts2panda/src/cmdOptions.ts b/ts2panda/src/cmdOptions.ts index bf19d72cd5..b77ae95f86 100644 --- a/ts2panda/src/cmdOptions.ts +++ b/ts2panda/src/cmdOptions.ts @@ -51,6 +51,8 @@ const ts2pandaOptions = [ { name: 'expression-watch-toolchain', type: String, defaultValue: "es2panda", description: "Specify the tool chain used to transform the expression" }, { name: 'source-file', type: String, defaultValue: "", description: "specify the file path info recorded in generated abc" }, { name: 'generate-tmp-file', type: Boolean, defaultValue: false, description: "whether to generate intermediate temporary files"}, + { name: 'record-name', type: String, defaultValue: "", description: "specify the record name." }, + { name: 'output-proto', type: String, defaultValue: "", description: "Compiler proto serialize binary output (.bin)" }, ] @@ -308,11 +310,16 @@ export class CmdOptions { return this.options["source-file"]; } +<<<<<<< HEAD static needGenerateTmpFile(): boolean { if (!this.options) { return false; } return this.options["generate-tmp-file"]; +======= + static getOutputproto(): string { + return this.options["output-proto"]; +>>>>>>> Support serialize proto bin file in ts2abc } // @ts-ignore diff --git a/ts2panda/src/ts2panda.ts b/ts2panda/src/ts2panda.ts index 190871169d..ac20e8f6c7 100644 --- a/ts2panda/src/ts2panda.ts +++ b/ts2panda/src/ts2panda.ts @@ -201,7 +201,8 @@ export class Ts2Panda { "opt_level": CmdOptions.getOptLevel(), "opt_log_level": CmdOptions.getOptLogLevel(), "display_typeinfo": CmdOptions.getDisplayTypeinfo(), - "is_dts_file": isGlobalDeclare() + "is_dts_file": isGlobalDeclare(), + "output-proto": CmdOptions.getOutputproto() }; let jsonOpt = JSON.stringify(options, null, 2); jsonOpt = "$" + jsonOpt.replace(dollarSign, '#$') + "$"; diff --git a/ts2panda/ts2abc/ts2abc_options.h b/ts2panda/ts2abc/ts2abc_options.h index 76f326fe25..9195b1dd7e 100755 --- a/ts2panda/ts2abc/ts2abc_options.h +++ b/ts2panda/ts2abc/ts2abc_options.h @@ -224,7 +224,7 @@ namespace panda::ts2abc { panda::PandArg compile_by_pipe_arg_{ "compile-by-pipe", false, R"(Compile a json file that is passed by pipe)"}; panda::PandArg compiler_output_proto_{ "output-proto", "", - R"(compiler proto serialize binary output (.proto))"}; + R"(compiler proto serialize binary output (.bin))"}; panda::PandArg Tail_Arg1_arg_{ "ARG_1", "", R"(Path to input(json file) or path to output(ark bytecode)" " when 'compile-by-pipe' enabled)"}; -- Gitee From 6fab283258960b32fcf5d45f2f770859a80baaaa Mon Sep 17 00:00:00 2001 From: gavin1012_hw Date: Tue, 6 Sep 2022 14:29:48 +0800 Subject: [PATCH 03/10] Enable merge abc 262 testing approach for ts2abc Signed-off-by: gavin1012_hw Change-Id: Idd16ac5ce9b23fbf1d06fdef687c0a20447c8e11 --- es2panda/aot/options.cpp | 2 +- test262/run_sunspider.py | 10 +++++++--- ts2panda/src/cmdOptions.ts | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/es2panda/aot/options.cpp b/es2panda/aot/options.cpp index 10410d9491..87e6dcf953 100644 --- a/es2panda/aot/options.cpp +++ b/es2panda/aot/options.cpp @@ -148,7 +148,7 @@ bool Options::Parse(int argc, const char **argv) panda::PandArg base64Output("base64Output", false, "output panda file content as base64 to std out"); panda::PandArg sourceFile("source-file", "", "specify the file path info recorded in generated abc"); - panda::PandArg outputProto("outputProto", "", "compiler proto serialize binary output (.proto)"); + panda::PandArg outputProto("outputProto", "", "specify output name of serializd protobuf file (.bin)"); panda::PandArg opCacheFile("cache-file", "", "cache file for incremental compile"); panda::PandArg opNpmModuleEntryList("npm-module-entry-list", "", "entry list file for module compile"); panda::PandArg opMergeAbc("merge-abc", false, "Compile as merge abc"); diff --git a/test262/run_sunspider.py b/test262/run_sunspider.py index f7f6ea757a..4318109091 100755 --- a/test262/run_sunspider.py +++ b/test262/run_sunspider.py @@ -261,8 +261,12 @@ class ArkProgram(): if self.ark_frontend == ARK_FRONTEND_LIST[0]: mod_opt_index = 3 - cmd_args = ['node', '--expose-gc', frontend_tool, - js_file, '-o', out_file] + if merge_abc_mode != "0": + cmd_args = ['node', '--expose-gc', frontend_tool, + js_file, '-output-proto', proto_bin_file] + else: + cmd_args = ['node', '--expose-gc', frontend_tool, + js_file, '-o', out_file] if file_name in self.module_list: cmd_args.insert(mod_opt_index, "-m") self.module = True @@ -294,7 +298,7 @@ class ArkProgram(): self.abc_file += f':{abc_file}' retcode = exec_command(cmd_args) self.abc_cmd = cmd_args - if self.ark_frontend == ARK_FRONTEND_LIST[1] and merge_abc_mode != "0": + if merge_abc_mode != "0": cmd_args = [merge_abc_binary, '--input', proto_bin_file, '--suffix', "bin", '--outputFilePath', file_dir, '--output', proto_abc_file] diff --git a/ts2panda/src/cmdOptions.ts b/ts2panda/src/cmdOptions.ts index b77ae95f86..f20ac1554c 100644 --- a/ts2panda/src/cmdOptions.ts +++ b/ts2panda/src/cmdOptions.ts @@ -52,7 +52,7 @@ const ts2pandaOptions = [ { name: 'source-file', type: String, defaultValue: "", description: "specify the file path info recorded in generated abc" }, { name: 'generate-tmp-file', type: Boolean, defaultValue: false, description: "whether to generate intermediate temporary files"}, { name: 'record-name', type: String, defaultValue: "", description: "specify the record name." }, - { name: 'output-proto', type: String, defaultValue: "", description: "Compiler proto serialize binary output (.bin)" }, + { name: 'output-proto', type: String, defaultValue: "", description: "specify output name of serializd protobuf file (.bin)" }, ] -- Gitee From f452913988e58d95e58e4b0f5abe14c7ca913698 Mon Sep 17 00:00:00 2001 From: gavin1012_hw Date: Tue, 6 Sep 2022 12:01:28 +0800 Subject: [PATCH 04/10] Add record name info in panda file for ts2abc Signed-off-by: gavin1012_hw Change-Id: Ic07dec1e326825ba3704c8a7bd2fdb623336eca4 --- ts2panda/src/base/typeSystem.ts | 28 +++++++------- ts2panda/src/cmdOptions.ts | 7 ++++ ts2panda/src/compilerDriver.ts | 15 +++++--- ts2panda/src/index.ts | 31 +++++++++++----- ts2panda/src/pandasm.ts | 16 +------- ts2panda/src/ts2panda.ts | 18 ++++++++- ts2panda/ts2abc/ts2abc.cpp | 65 +++++++++++---------------------- 7 files changed, 91 insertions(+), 89 deletions(-) diff --git a/ts2panda/src/base/typeSystem.ts b/ts2panda/src/base/typeSystem.ts index 43bdf6e64f..51a1b4d3d4 100644 --- a/ts2panda/src/base/typeSystem.ts +++ b/ts2panda/src/base/typeSystem.ts @@ -425,7 +425,7 @@ export class ClassType extends BaseType { classTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.extendsHeritage)); classTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.implementsHeritages.length)); this.implementsHeritages.forEach(heritage => { - classTypeLiterals.push(new Literal(LiteralTag.INTEGER, heritage)); + classTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, heritage)); }); // record unstatic fields and methods @@ -446,7 +446,7 @@ export class ClassType extends BaseType { classTypeLiterals.push(new Literal(LiteralTag.INTEGER, transferredTarget.size)); transferredTarget.forEach((typeInfo, name) => { classTypeLiterals.push(new Literal(LiteralTag.STRING, name)); - classTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[0])); // typeIndex + classTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, typeInfo[0])); // typeIndex classTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[1])); // accessFlag classTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[2])); // readonly }); @@ -458,7 +458,7 @@ export class ClassType extends BaseType { classTypeLiterals.push(new Literal(LiteralTag.INTEGER, transferredTarget.size)); transferredTarget.forEach((typeInfo, name) => { classTypeLiterals.push(new Literal(LiteralTag.STRING, name)); - classTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo)); + classTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, typeInfo)); }); } } @@ -588,17 +588,17 @@ export class FunctionType extends BaseType { funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.parameters[0])); funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.parameters.length - 1)); for (let i = 1; i < this.parameters.length; i++) { - funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.parameters[i])); + funcTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.parameters[i])); } } else { funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, 0)); // marker for not having 'this' param funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.parameters.length)); for (let i = 0; i < this.parameters.length; i++) { - funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.parameters[i])); + funcTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.parameters[i])); } } - funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.returnType)); + funcTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.returnType)); funcTypeBuf.addLiterals(...funcTypeLiterals); return funcTypeBuf; } @@ -675,7 +675,7 @@ export class UnionType extends BaseType { UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, L2Type.UNION)); UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.unionedTypeArray.length)); for (let type of this.unionedTypeArray) { - UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, type)); + UnionTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, type)); } UnionTypeBuf.addLiterals(...UnionTypeLiterals); return UnionTypeBuf; @@ -720,7 +720,7 @@ export class ArrayType extends BaseType { let arrayBuf = new LiteralBuffer(); let arrayLiterals: Array = new Array(); arrayLiterals.push(new Literal(LiteralTag.INTEGER, L2Type.ARRAY)); - arrayLiterals.push(new Literal(LiteralTag.INTEGER, this.referedTypeIndex)); + arrayLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.referedTypeIndex)); arrayBuf.addLiterals(...arrayLiterals); return arrayBuf; } @@ -755,7 +755,7 @@ export class ObjectType extends BaseType { objLiterals.push(new Literal(LiteralTag.INTEGER, this.properties.size)); this.properties.forEach((typeIndex, name) => { objLiterals.push(new Literal(LiteralTag.STRING, name)); - objLiterals.push(new Literal(LiteralTag.INTEGER, typeIndex)); + objLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, typeIndex)); }); objTypeBuf.addLiterals(...objLiterals); return objTypeBuf; @@ -868,7 +868,7 @@ export class InterfaceType extends BaseType { interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.heritages.length)); this.heritages.forEach(heritage => { - interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, heritage)); + interfaceTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, heritage)); }); // record fields and methods @@ -885,7 +885,7 @@ export class InterfaceType extends BaseType { interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, transferredTarget.size)); transferredTarget.forEach((typeInfo, name) => { interfaceTypeLiterals.push(new Literal(LiteralTag.STRING, name)); - interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[0])); // typeIndex + interfaceTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, typeInfo[0])); // typeIndex interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[1])); // accessFlag interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[2])); // readonly }); @@ -896,7 +896,7 @@ export class InterfaceType extends BaseType { interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, transferredTarget.length)); transferredTarget.forEach(method => { - interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, method)); + interfaceTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, method)); }); } } @@ -925,10 +925,10 @@ export class BuiltinContainerType extends BaseType { let UnionTypeBuf = new LiteralBuffer(); let UnionTypeLiterals: Array = new Array(); UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, L2Type.BUILTINCONTAINER)); - UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.builtinTypeIndex)); + UnionTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.builtinTypeIndex)); UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.containerArray.length)); for (let type of this.containerArray) { - UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, type)); + UnionTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, type)); } UnionTypeBuf.addLiterals(...UnionTypeLiterals); return UnionTypeBuf; diff --git a/ts2panda/src/cmdOptions.ts b/ts2panda/src/cmdOptions.ts index f20ac1554c..f331b6c92f 100644 --- a/ts2panda/src/cmdOptions.ts +++ b/ts2panda/src/cmdOptions.ts @@ -211,6 +211,13 @@ export class CmdOptions { return outputFile; } + static getRecordName(): string { + if (!this.options) { + return ""; + } + return this.options["record-name"]; + } + static getTimeOut(): Number { if (!this.options) { return 0; diff --git a/ts2panda/src/compilerDriver.ts b/ts2panda/src/compilerDriver.ts index 934449b3a2..d676991444 100644 --- a/ts2panda/src/compilerDriver.ts +++ b/ts2panda/src/compilerDriver.ts @@ -61,6 +61,7 @@ export class PendingCompilationUnit { export class CompilerDriver { static isTsFile: boolean = false; private fileName: string; + private recordName: string; private passes: Pass[] = []; private compilationUnits: PandaGen[]; pendingCompilationUnits: PendingCompilationUnit[]; @@ -70,8 +71,9 @@ export class CompilerDriver { private needDumpHeader: boolean = true; private ts2abcProcess: any = undefined; - constructor(fileName: string) { + constructor(fileName: string, recordName: string) { this.fileName = fileName; + this.recordName = recordName; // register passes here this.passes = [ new CacheExpander(), @@ -178,6 +180,7 @@ export class CompilerDriver { listenErrorEvent(ts2abcProc); try { + Ts2Panda.dumpRecord(ts2abcProc, this.recordName); Ts2Panda.dumpCmdOptions(ts2abcProc); for (let i = 0; i < this.pendingCompilationUnits.length; i++) { @@ -363,13 +366,13 @@ export class CompilerDriver { if (name == '') { if ((ts.isFunctionDeclaration(node) && hasExportKeywordModifier(node) && hasDefaultKeywordModifier(node)) || ts.isExportAssignment(findOuterNodeOfParenthesis(node))) { - return 'default'; + return `${this.recordName}.default`; } - return `#${this.getFuncId(funcNode)}#`; + return `${this.recordName}.#${this.getFuncId(funcNode)}#`; } if (name == "func_main_0") { - return `#${this.getFuncId(funcNode)}#${name}`; + return `${this.recordName}.#${this.getFuncId(funcNode)}#${name}`; } let funcNameMap = recorder.getFuncNameMap(); @@ -386,7 +389,7 @@ export class CompilerDriver { name = `#${this.getFuncId(funcNode)}#` } } - return name; + return `${this.recordName}.${name}`; } getInternalNameForCtor(node: ts.ClassLikeDeclaration, ctor: ts.ConstructorDeclaration) { @@ -395,7 +398,7 @@ export class CompilerDriver { if (name.lastIndexOf(".") != -1) { name = `#${this.getFuncId(ctor)}#` } - return name; + return `${this.recordName}.${name}`; } writeBinaryFile(pandaGen: PandaGen) { diff --git a/ts2panda/src/index.ts b/ts2panda/src/index.ts index e243c78b0d..50c9706a7b 100644 --- a/ts2panda/src/index.ts +++ b/ts2panda/src/index.ts @@ -45,7 +45,7 @@ function checkIsGlobalDeclaration(sourceFile: ts.SourceFile) { function generateDTs(node: ts.SourceFile, options: ts.CompilerOptions) { let outputBinName = getOutputBinName(node); - let compilerDriver = new CompilerDriver(outputBinName); + let compilerDriver = new CompilerDriver(outputBinName, getRecordName(node)); setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(node, options)); compilerDriver.compile(node); compilerDriver.showStatistics(); @@ -90,7 +90,7 @@ function main(fileNames: string[], options: ts.CompilerOptions) { (ctx: ts.TransformationContext) => { return (node: ts.SourceFile) => { let outputBinName = getOutputBinName(node); - let compilerDriver = new CompilerDriver(outputBinName); + let compilerDriver = new CompilerDriver(outputBinName, getRecordName(node)); compilerDriver.compileForSyntaxCheck(node); return node; } @@ -118,7 +118,7 @@ function main(fileNames: string[], options: ts.CompilerOptions) { node = transformCommonjsModule(node); } let outputBinName = getOutputBinName(node); - let compilerDriver = new CompilerDriver(outputBinName); + let compilerDriver = new CompilerDriver(outputBinName, getRecordName(node)); setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(node, options)); compilerDriver.compile(node); compilerDriver.showStatistics(); @@ -176,6 +176,17 @@ function getDtsFiles(libDir: string): string[] { return dtsFiles; } +function getRecordName(node: ts.SourceFile): string { + let recordName = CmdOptions.getRecordName(); + + if (recordName == "") { + let outputBinName = getOutputBinName(node); + recordName = path.basename(outputBinName, path.extname(outputBinName)); + } + + return recordName; +} + function specifyCustomLib(customLib) { Compiler.Options.Default["lib"] = customLib; let curFiles = fs.readdirSync(__dirname); @@ -202,6 +213,7 @@ function specifyCustomLib(customLib) { const stopWatchingStr = "####"; const watchAbcFileDefaultTimeOut = 10; const watchFileName = "watch_expressions"; +const watchOutputFileName = "Base64Output"; // this path is only available in sdk const es2abcBinaryPath = path["join"](__dirname, "..", "bin", path.sep); const es2abcBinaryName = /^win/.test(require('os').platform()) ? "es2abc.exe" : "es2abc"; @@ -305,7 +317,7 @@ function compileWatchExpression(jsFileName: string, errorMsgFileName: string, op return (node: ts.SourceFile) => { if (path.basename(node.fileName) == fileName) { node = sourceFile; } let outputBinName = getOutputBinName(node); - let compilerDriver = new CompilerDriver(outputBinName); + let compilerDriver = new CompilerDriver(outputBinName, watchOutputFileName); compilerDriver.compileForSyntaxCheck(node); return node; } @@ -330,7 +342,7 @@ function compileWatchExpression(jsFileName: string, errorMsgFileName: string, op node = ts.factory.updateSourceFile(node, newStatements); } let outputBinName = getOutputBinName(node); - let compilerDriver = new CompilerDriver(outputBinName); + let compilerDriver = new CompilerDriver(outputBinName, watchOutputFileName); setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(node, options)); compilerDriver.compile(node); return node; @@ -346,10 +358,11 @@ function launchWatchEvaluateDeamon(parsed: ts.ParsedCommandLine | undefined) { console.log("startWatchingSuccess supportTimeout"); return; } - let deamonFilePrefix = CmdOptions.getEvaluateDeamonPath() + path.sep + watchFileName; - let jsFileName = deamonFilePrefix + ".js"; - let abcFileName = deamonFilePrefix + ".abc"; - let errorMsgFileName = deamonFilePrefix + ".err"; + let deamonJSFilePrefix = CmdOptions.getEvaluateDeamonPath() + path.sep + watchFileName; + let deamonABCFilePrefix = CmdOptions.getEvaluateDeamonPath() + path.sep + watchOutputFileName; + let jsFileName = deamonJSFilePrefix + ".js"; + let abcFileName = deamonABCFilePrefix + ".abc"; + let errorMsgFileName = deamonJSFilePrefix + ".err"; if (fs.existsSync(jsFileName)) { console.log("watchFileServer has been initialized supportTimeout"); diff --git a/ts2panda/src/pandasm.ts b/ts2panda/src/pandasm.ts index da58e906a6..549c9a6a3d 100644 --- a/ts2panda/src/pandasm.ts +++ b/ts2panda/src/pandasm.ts @@ -109,24 +109,10 @@ export class Function { export class Record { public name: string; - public whole_line: string; - public bound_left: number; - public bound_right: number; - public line_number: number; public metadata: Metadata; - constructor( - name: string, - whole_line: string, - bound_left: number, - bound_right: number, - line_number: number - ) { + constructor(name: string) { this.name = name; - this.whole_line = whole_line; - this.bound_left = bound_left; - this.bound_right = bound_right; - this.line_number = line_number; this.metadata = new Metadata(); } } diff --git a/ts2panda/src/ts2panda.ts b/ts2panda/src/ts2panda.ts index ac20e8f6c7..7620c2dad5 100644 --- a/ts2panda/src/ts2panda.ts +++ b/ts2panda/src/ts2panda.ts @@ -37,7 +37,8 @@ import { ModuleRecord, NamespaceImportEntry, RegularImportEntry, - Signature + Signature, + Record } from "./pandasm"; import { generateCatchTables } from "./statement/tryStatement"; import { @@ -212,6 +213,19 @@ export class Ts2Panda { ts2abc.stdio[3].write(jsonOpt + '\n'); } + static dumpRecord(ts2abc: any, recordName: string): void { + let record = { + "t": JsonType.record, + "rb": new Record(recordName) + } + let jsonRecord = escapeUnicode(JSON.stringify(record, null, 2)); + jsonRecord = "$" + jsonRecord.replace(dollarSign, '#$') + "$"; + if (CmdOptions.isEnableDebugLog()) { + Ts2Panda.jsonString += jsonRecord; + } + ts2abc.stdio[3].write(jsonRecord + '\n'); + } + // @ts-ignore static dumpInstTypeMap(pg: PandaGen): any { let insts = pg.getInsns(); @@ -321,7 +335,7 @@ export class Ts2Panda { } typeInfo = Ts2Panda.dumpInstTypeMap(pg); - if (funcName == "func_main_0") { + if (funcName.indexOf("func_main_0") !== -1) { let exportedTypes = PandaGen.getExportedTypes(); let declareddTypes = PandaGen.getDeclaredTypes(); if (exportedTypes.size != 0) { diff --git a/ts2panda/ts2abc/ts2abc.cpp b/ts2panda/ts2abc/ts2abc.cpp index ebd96d2f48..99e772ce5f 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -54,6 +54,7 @@ constexpr std::size_t BOUND_RIGHT = 0; constexpr std::size_t LINE_NUMBER = 0; constexpr bool IS_DEFINED = true; int g_opCodeIndex = 0; +std::string g_recordName = ""; std::unordered_map g_opcodeMap = { #define OPLIST(opcode, name, optype, width, flags, def_idx, use_idxs) {g_opCodeIndex++, panda::pandasm::Opcode::opcode}, PANDA_INSTRUCTION_LIST(OPLIST) @@ -65,7 +66,6 @@ std::unordered_map g_opcodeMap = { static panda::pandasm::Record MakeRecordDefinition(const std::string &name) { auto record = panda::pandasm::Record(name, LANG_EXT); - return record; } @@ -817,35 +817,27 @@ static void GenerateESTypeAnnotationRecord(panda::pandasm::Program &prog) prog.record_table.emplace(tsTypeAnnotationRecord.name, std::move(tsTypeAnnotationRecord)); } -static void GenerateCommonJsRecord(panda::pandasm::Program &prog, bool isCommonJs) +static void SetCommonjsField(panda::pandasm::Program &prog, bool isCommonjs) { - // when multi-abc file get merged, field should be inserted in abc's own record - auto commonjsRecord = panda::pandasm::Record("_CommonJsRecord", LANG_EXT); - commonjsRecord.metadata->SetAccessFlags(panda::ACC_PUBLIC); - auto isCommonJsField = panda::pandasm::Field(LANG_EXT); - isCommonJsField.name = "isCommonJs"; - isCommonJsField.type = panda::pandasm::Type("u8", 0); - isCommonJsField.metadata->SetValue(panda::pandasm::ScalarValue::Create( - static_cast(isCommonJs))); - commonjsRecord.field_list.emplace_back(std::move(isCommonJsField)); - - prog.record_table.emplace(commonjsRecord.name, std::move(commonjsRecord)); -} - -static void GenerateESModuleRecord(panda::pandasm::Program &prog) -{ - auto ecmaModuleRecord = panda::pandasm::Record("_ESModuleRecord", LANG_EXT); - ecmaModuleRecord.metadata->SetAccessFlags(panda::ACC_PUBLIC); - prog.record_table.emplace(ecmaModuleRecord.name, std::move(ecmaModuleRecord)); + auto iter = prog.record_table.find(g_recordName); + if (iter != prog.record_table.end()) { + auto &rec = iter->second; + auto isCommonJsField = panda::pandasm::Field(LANG_EXT); + isCommonJsField.name = "isCommonjs"; + isCommonJsField.type = panda::pandasm::Type("u8", 0); + isCommonJsField.metadata->SetValue( + panda::pandasm::ScalarValue::Create(static_cast(isCommonjs))); + rec.field_list.emplace_back(std::move(isCommonJsField)); + } } -static void AddModuleRecord(panda::pandasm::Program &prog, const std::string &moduleName, uint32_t moduleIdx) +static void SetModuleRecordIdx(panda::pandasm::Program &prog, uint32_t moduleIdx) { - auto iter = prog.record_table.find("_ESModuleRecord"); + auto iter = prog.record_table.find(g_recordName); if (iter != prog.record_table.end()) { auto &rec = iter->second; auto moduleIdxField = panda::pandasm::Field(LANG_EXT); - moduleIdxField.name = moduleName; + moduleIdxField.name = "moduleRecordIdx"; moduleIdxField.type = panda::pandasm::Type("u32", 0); moduleIdxField.metadata->SetValue(panda::pandasm::ScalarValue::Create( static_cast(moduleIdx))); @@ -874,23 +866,11 @@ int ParseJson(const std::string &data, Json::Value &rootValue) return RETURN_SUCCESS; } -static void ParseModuleMode(const Json::Value &rootValue, panda::pandasm::Program &prog) -{ - Logd("----------------parse module_mode-----------------"); - if (rootValue.isMember("module_mode") && rootValue["module_mode"].isBool()) { - if (rootValue["module_mode"].asBool()) { - GenerateESModuleRecord(prog); - } - } -} - -static void ParseCommonJsModuleMode(const Json::Value &rootValue, panda::pandasm::Program &prog) +static void SetCommonJsModuleMode(const Json::Value &rootValue, panda::pandasm::Program &prog) { Logd("------------parse commonjs_module_mode-------------"); if (rootValue.isMember("commonjs_module") && rootValue["commonjs_module"].isBool()) { - if (rootValue["commonjs_module"].asBool()) { - GenerateCommonJsRecord(prog, true); - } + SetCommonjsField(prog, rootValue["commonjs_module"].asBool()); } } @@ -966,8 +946,7 @@ static void ParseOptions(const Json::Value &rootValue, panda::pandasm::Program & { GenerateESCallTypeAnnotationRecord(prog); GenerateESTypeAnnotationRecord(prog); - ParseModuleMode(rootValue, prog); - ParseCommonJsModuleMode(rootValue, prog); + SetCommonJsModuleMode(rootValue, prog); ParseLogEnable(rootValue); ParseDebugMode(rootValue); ParseOptLevel(rootValue); @@ -983,9 +962,10 @@ static void ParseSingleFunc(const Json::Value &rootValue, panda::pandasm::Progra prog.function_table.emplace(function.name.c_str(), std::move(function)); } -static void ParseSingleRec(const Json::Value &rootValue, panda::pandasm::Program &prog) +static void ParseRec(const Json::Value &rootValue, panda::pandasm::Program &prog) { auto record = ParseRecord(rootValue["rb"]); + g_recordName = record.name; prog.record_table.emplace(record.name.c_str(), std::move(record)); } @@ -1127,8 +1107,7 @@ static void ParseSingleModule(const Json::Value &rootValue, panda::pandasm::Prog ParseIndirectExportEntries(moduleRecord["indirectExportEntries"], moduleLiteralArray); ParseStarExportEntries(moduleRecord["starExportEntries"], moduleLiteralArray); - auto moduleName = ParseString(moduleRecord["moduleName"].asString()); - AddModuleRecord(prog, moduleName, g_literalArrayCount); + SetModuleRecordIdx(prog, g_literalArrayCount); auto moduleLiteralarrayInstance = panda::pandasm::LiteralArray(moduleLiteralArray); prog.literalarray_table.emplace(std::to_string(g_literalArrayCount++), std::move(moduleLiteralarrayInstance)); @@ -1178,7 +1157,7 @@ static int ParseSmallPieceJson(const std::string &subJson, panda::pandasm::Progr } case static_cast(JsonType::RECORD): { if (rootValue.isMember("rb") && rootValue["rb"].isObject()) { - ParseSingleRec(rootValue, prog); + ParseRec(rootValue, prog); } break; } -- Gitee From 29ac8eb9200683e7a274bbc260121f8bbd8c03ee Mon Sep 17 00:00:00 2001 From: gavin1012_hw Date: Tue, 6 Sep 2022 12:06:20 +0800 Subject: [PATCH 05/10] Adapt UT in ts2abc to merged abc Signed-off-by: gavin1012_hw Change-Id: I21e335709d7add56bbe90cf1e78a331ea3392db3 --- ts2panda/tests/builtIns.test.ts | 2 +- ts2panda/tests/commonjs.test.ts | 6 +-- ts2panda/tests/esmodule.test.ts | 2 +- ts2panda/tests/expression/arguments.test.ts | 4 +- ts2panda/tests/expression/commalist.test.ts | 2 +- .../expression/functionExpression.test.ts | 38 +++++++++---------- ts2panda/tests/expression/thisKeyWord.test.ts | 2 +- ts2panda/tests/hoist.test.ts | 19 +++++----- ts2panda/tests/lexenv.test.ts | 31 ++++++++------- .../statements/functionDeclaration.test.ts | 20 +++++----- .../statements/variableDeclaration.test.ts | 8 ++-- ts2panda/tests/types/array.test.ts | 12 +++--- ts2panda/tests/types/class.test.ts | 16 ++++---- ts2panda/tests/types/function.test.ts | 10 ++--- ts2panda/tests/types/object.test.ts | 4 +- ts2panda/tests/types/primitives.test.ts | 14 +++---- ts2panda/tests/types/union.test.ts | 8 ++-- ts2panda/tests/utils/base.ts | 10 ++--- 18 files changed, 103 insertions(+), 105 deletions(-) diff --git a/ts2panda/tests/builtIns.test.ts b/ts2panda/tests/builtIns.test.ts index ace7fc1c21..590a3131b2 100644 --- a/ts2panda/tests/builtIns.test.ts +++ b/ts2panda/tests/builtIns.test.ts @@ -45,7 +45,7 @@ describe("FunctionToStringTest", function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compileAfter(`function foo() {return 123;}\nfunction bar() {return 321;}\n`, 'toStringTest.js'); CmdOptions.needRecordSourceCode = () => {return false}; - let pandaGen = snippetCompiler.getPandaGenByName('foo'); + let pandaGen = snippetCompiler.getPandaGenByName('UnitTest.foo'); let expected = "function foo() {return 123;}"; expect(pandaGen.getSourceCode() == expected).to.be.true; }) diff --git a/ts2panda/tests/commonjs.test.ts b/ts2panda/tests/commonjs.test.ts index f0148848a4..119cce756f 100644 --- a/ts2panda/tests/commonjs.test.ts +++ b/ts2panda/tests/commonjs.test.ts @@ -44,7 +44,7 @@ describe("CommonJsTest", function () { CmdOptions.isCommonJs = () => {return false}; let funcMainInsns = snippetCompiler.getGlobalInsns(); let expected = [ - new EcmaDefinefuncdyn('#1#', new Imm(5), new VReg()), + new EcmaDefinefuncdyn('UnitTest.#1#', new Imm(5), new VReg()), new StaDyn(new VReg()), new LdaDyn(new VReg()), new StaDyn(new VReg()), @@ -67,7 +67,7 @@ describe("CommonJsTest", function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compileCommonjs(`let a = require('a.js')`, 'cjs.js'); CmdOptions.isCommonJs = () => {return false}; - let execInsns = snippetCompiler.getPandaGenByName('#1#')!.getInsns(); + let execInsns = snippetCompiler.getPandaGenByName('UnitTest.#1#')!.getInsns(); let requirePara = new VReg(); let requireReg = new VReg(); let moduleRequest = new VReg(); @@ -88,7 +88,7 @@ describe("CommonJsTest", function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compileCommonjs(`let a = 1; exports.a = a;`, 'cjs.js'); CmdOptions.isCommonJs = () => {return false}; - let execInsns = snippetCompiler.getPandaGenByName('#1#')!.getInsns(); + let execInsns = snippetCompiler.getPandaGenByName('UnitTest.#1#')!.getInsns(); let exportsPara = new VReg(); let exportsReg = new VReg(); let tmpReg = new VReg(); diff --git a/ts2panda/tests/esmodule.test.ts b/ts2panda/tests/esmodule.test.ts index e28b9b6db4..f88272059a 100644 --- a/ts2panda/tests/esmodule.test.ts +++ b/ts2panda/tests/esmodule.test.ts @@ -45,7 +45,7 @@ describe("ExportDeclaration", function () { let classReg = new VReg(); let expected = [ new MovDyn(new VReg(), new VReg()), - new EcmaDefineclasswithbuffer("#1#C", new Imm(0), new Imm(0), new VReg(), new VReg()), + new EcmaDefineclasswithbuffer("UnitTest.#1#C", new Imm(0), new Imm(0), new VReg(), new VReg()), new StaDyn(classReg), new LdaDyn(classReg), new EcmaStmodulevar('C'), diff --git a/ts2panda/tests/expression/arguments.test.ts b/ts2panda/tests/expression/arguments.test.ts index 3dd2fbd706..350836037e 100644 --- a/ts2panda/tests/expression/arguments.test.ts +++ b/ts2panda/tests/expression/arguments.test.ts @@ -43,7 +43,7 @@ describe("arguments Keyword", function () { new EcmaLdobjbyindex(temp1, new Imm(0)), new EcmaReturnundefined() ]; - let functionPg = snippetCompiler.getPandaGenByName("foo"); + let functionPg = snippetCompiler.getPandaGenByName("UnitTest.foo"); let insns = functionPg!.getInsns(); expect(checkInstructions(insns, expected)).to.be.true; @@ -62,7 +62,7 @@ describe("arguments Keyword", function () { new EcmaLdobjbyindex(temp1, new Imm(0)), new EcmaReturnundefined() ]; - let functionPg = snippetCompiler.getPandaGenByName("foo"); + let functionPg = snippetCompiler.getPandaGenByName("UnitTest.foo"); let insns = functionPg!.getInsns(); expect(checkInstructions(insns, expected)).to.be.true; diff --git a/ts2panda/tests/expression/commalist.test.ts b/ts2panda/tests/expression/commalist.test.ts index 5163fded64..0681472412 100644 --- a/ts2panda/tests/expression/commalist.test.ts +++ b/ts2panda/tests/expression/commalist.test.ts @@ -54,7 +54,7 @@ describe("CommaListExpression", function () { let insns = snippetCompiler.getGlobalInsns(); let expected = [ new MovDyn(new VReg(), new VReg()), - new EcmaDefineclasswithbuffer("#1#Test", new Imm(0), new Imm(0), new VReg(), new VReg()), + new EcmaDefineclasswithbuffer("UnitTest.#1#Test", new Imm(0), new Imm(0), new VReg(), new VReg()), new StaDyn(new VReg()), new LdaDyn(new VReg()), new EcmaStclasstoglobalrecord("Test"), diff --git a/ts2panda/tests/expression/functionExpression.test.ts b/ts2panda/tests/expression/functionExpression.test.ts index e04bb8ea3e..f1b0624fd1 100644 --- a/ts2panda/tests/expression/functionExpression.test.ts +++ b/ts2panda/tests/expression/functionExpression.test.ts @@ -72,7 +72,7 @@ describe("compileFunctionExpression", function () { let checkCount = 0; pandaGens.forEach((pg) => { - if (pg.internalName == "test") { + if (pg.internalName == "UnitTest.test") { expect(checkInstructions(pg.getInsns(), expected_func), "check func insns").to.be.true; checkCount++; } @@ -90,15 +90,15 @@ describe("compileFunctionExpression", function () { let checkCount = 0; pandaGens.forEach((pg) => { - if (pg.internalName == "a") { + if (pg.internalName == "UnitTest.a") { checkCount++; } - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { pg.getInsns().forEach((insns) => { if (insns instanceof EcmaDefinefuncdyn) { - expect(insns.operands[0]).to.equal('a'); + expect(insns.operands[0]).to.equal('UnitTest.a'); checkCount++; } }); @@ -118,15 +118,15 @@ describe("compileFunctionExpression", function () { let checkCount = 0; pandaGens.forEach((pg) => { - if (pg.internalName == "a") { + if (pg.internalName == "UnitTest.a") { checkCount++; } - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { pg.getInsns().forEach((insns) => { if (insns instanceof EcmaDefinefuncdyn) { - expect(insns.operands[0]).to.equal('a'); + expect(insns.operands[0]).to.equal('UnitTest.a'); checkCount++; } }); @@ -146,15 +146,15 @@ describe("compileFunctionExpression", function () { let checkCount = 0; pandaGens.forEach((pg) => { - if (pg.internalName == "a") { + if (pg.internalName == "UnitTest.a") { checkCount++; } - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { pg.getInsns().forEach((insns) => { if (insns instanceof EcmaDefinencfuncdyn) { - expect(insns.operands[0]).to.equal('a'); + expect(insns.operands[0]).to.equal('UnitTest.a'); checkCount++; } }); @@ -182,16 +182,16 @@ describe("compileFunctionExpression", function () { ]; pandaGens.forEach((pg) => { - if (pg.internalName == "p") { + if (pg.internalName == "UnitTest.p") { expect(checkInstructions(pg.getInsns(), expected_func), "check arrow func insns").to.be.true; checkCount++; } - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { pg.getInsns().forEach((insns) => { if (insns instanceof EcmaDefinencfuncdyn) { - expect(insns.operands[0]).to.equal('p'); + expect(insns.operands[0]).to.equal('UnitTest.p'); checkCount++; } }); @@ -283,15 +283,15 @@ describe("compileFunctionExpression", function () { let checkCount = 0; pandaGens.forEach((pg) => { - if (pg.internalName == "a") { + if (pg.internalName == "UnitTest.a") { expect(checkInstructions(pg.getInsns(), expected_func), "check generator func insns").to.be.true; checkCount++; } - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { pg.getInsns().forEach((insns) => { if (insns instanceof EcmaDefinegeneratorfunc) { - expect(insns.operands[0]).to.equal('a'); + expect(insns.operands[0]).to.equal('UnitTest.a'); checkCount++; } }); @@ -343,15 +343,15 @@ describe("compileFunctionExpression", function () { let checkCount = 0; pandaGens.forEach((pg) => { - if (pg.internalName == "a") { + if (pg.internalName == "UnitTest.a") { expect(checkInstructions(pg.getInsns(), expected_func), "check async func insns").to.be.true; checkCount++; } - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { pg.getInsns().forEach((insns) => { if (insns instanceof EcmaDefinencfuncdyn) { - expect(insns.operands[0]).to.equal('a'); + expect(insns.operands[0]).to.equal('UnitTest.a'); checkCount++; } }); diff --git a/ts2panda/tests/expression/thisKeyWord.test.ts b/ts2panda/tests/expression/thisKeyWord.test.ts index e88f39146d..486902b458 100644 --- a/ts2panda/tests/expression/thisKeyWord.test.ts +++ b/ts2panda/tests/expression/thisKeyWord.test.ts @@ -50,7 +50,7 @@ describe("ThisKeyword", function () { it("this in function scope", function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile("function a() {this}"); - let functionPg = snippetCompiler.getPandaGenByName("a"); + let functionPg = snippetCompiler.getPandaGenByName("UnitTest.a"); let functionScope = functionPg!.getScope(); let insns = compileMainSnippet("this;", pandaGen, functionScope); let expected = [ diff --git a/ts2panda/tests/hoist.test.ts b/ts2panda/tests/hoist.test.ts index ffd5a7fe4d..e048a0eca7 100644 --- a/ts2panda/tests/hoist.test.ts +++ b/ts2panda/tests/hoist.test.ts @@ -29,7 +29,6 @@ import { LdaDyn, LdaiDyn, LdaStr, - ResultType, StaDyn, VReg } from "../src/irnodes"; @@ -79,7 +78,7 @@ describe("HoistTest", function () { let insns = snippetCompiler.getGlobalInsns(); let expected = [ - new EcmaDefinefuncdyn("a", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.a", new Imm(0), new VReg()), new EcmaStglobalvar("a"), new EcmaReturnundefined() ] @@ -93,7 +92,7 @@ describe("HoistTest", function () { let insns = snippetCompiler.getGlobalInsns(); let expected = [ - new EcmaDefinefuncdyn("#2#a", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.#2#a", new Imm(0), new VReg()), new EcmaStglobalvar("a"), new EcmaReturnundefined() ] @@ -107,7 +106,7 @@ describe("HoistTest", function () { snippetCompiler.compile(`var a = 1; function a() {}`); let insns = snippetCompiler.getGlobalInsns(); let expected = [ - new EcmaDefinefuncdyn("a", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.a", new Imm(0), new VReg()), new EcmaStglobalvar("a"), new LdaiDyn(new Imm(1)), new EcmaStglobalvar("a"), @@ -121,7 +120,7 @@ describe("HoistTest", function () { it('case 6', function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile(`function a() {var a = 1;}`); - let funcPg = snippetCompiler.getPandaGenByName("a"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.a"); let insns = funcPg!.getInsns(); let a = new VReg(); @@ -140,11 +139,11 @@ describe("HoistTest", function () { it('case 7', function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile(`function a() {function b() {}};`); - let funcPg = snippetCompiler.getPandaGenByName("a"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.a"); let insns = funcPg!.getInsns(); let a = new VReg(); let expected = [ - new EcmaDefinefuncdyn("b", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.b", new Imm(0), new VReg()), new StaDyn(a), new EcmaReturnundefined() @@ -158,7 +157,7 @@ describe("HoistTest", function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile(`a = 1; let a;`); - let funcPg = snippetCompiler.getPandaGenByName("func_main_0"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let insns = funcPg!.getInsns(); let idReg = new VReg(); let expected = [ @@ -177,7 +176,7 @@ describe("HoistTest", function () { a = 1; let a; }`); - let funcPg = snippetCompiler.getPandaGenByName("b"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.b"); let insns = funcPg!.getInsns(); let idReg = new VReg(); @@ -197,7 +196,7 @@ describe("HoistTest", function () { a = 1; let a; }`); - let funcPg = snippetCompiler.getPandaGenByName("func_main_0"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let insns = funcPg!.getInsns(); let idReg = new VReg(); diff --git a/ts2panda/tests/lexenv.test.ts b/ts2panda/tests/lexenv.test.ts index a57433b157..d6752672b9 100644 --- a/ts2panda/tests/lexenv.test.ts +++ b/ts2panda/tests/lexenv.test.ts @@ -39,7 +39,6 @@ import { LdaDyn, LdaiDyn, LdaStr, - ResultType, ReturnDyn, StaDyn, VReg @@ -116,7 +115,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { it("test CompilerDriver.scanFunctions-with-empty", function () { let source: string = ``; let sourceFile = creatAstFromSnippet(source); - let compilerDriver = new CompilerDriver('UnitTest'); + let compilerDriver = new CompilerDriver('UnitTest', 'UnitTest'); let globalScope = new GlobalScope(sourceFile); let recorder = new Recorder(sourceFile, globalScope, compilerDriver, false, false, true); recorder.record(); @@ -144,7 +143,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { var funcExpression = function() { } `; let sourceFile = creatAstFromSnippet(source); - let compilerDriver = new CompilerDriver('UnitTest'); + let compilerDriver = new CompilerDriver('UnitTest', 'UnitTest'); let globalScope = new GlobalScope(sourceFile); let recorder = new Recorder(sourceFile, globalScope, compilerDriver, false, false, true); recorder.record(); @@ -191,7 +190,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { let source: string = ` `; let sourceFile = creatAstFromSnippet(source); - let compilerDriver = new CompilerDriver('UnitTest'); + let compilerDriver = new CompilerDriver('UnitTest', 'UnitTest'); let globalScope = new GlobalScope(sourceFile); let recorder = new Recorder(sourceFile, globalScope, compilerDriver, false, false, true); @@ -215,7 +214,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { var funcExt = function() { } `; let sourceFile = creatAstFromSnippet(source); - let compilerDriver = new CompilerDriver('UnitTest'); + let compilerDriver = new CompilerDriver('UnitTest', 'UnitTest'); let globalScope = new GlobalScope(sourceFile); let recorder = new Recorder(sourceFile, globalScope, compilerDriver, false, false, true); @@ -269,7 +268,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { }))) `; let sourceFile = creatAstFromSnippet(source); - let compilerDriver = new CompilerDriver('UnitTest'); + let compilerDriver = new CompilerDriver('UnitTest', 'UnitTest'); let globalScope = new GlobalScope(sourceFile); let recorder = new Recorder(sourceFile, globalScope, compilerDriver, false, false, true); @@ -412,7 +411,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { let expected_main = [ new LdaDyn(new VReg()), new EcmaStglobalvar("outer"), - new EcmaDefinefuncdyn("func", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.func", new Imm(0), new VReg()), new EcmaStglobalvar("func"), new LdaiDyn(new Imm(1)), new EcmaStglobalvar("outer"), @@ -425,9 +424,9 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { ]; pandaGens.forEach((pg) => { - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { expect(checkInstructions(pg.getInsns(), expected_main)).to.be.true; - } else if (pg.internalName == "func") { + } else if (pg.internalName == "UnitTest.func") { expect(checkInstructions(pg.getInsns(), expected_func)).to.be.true; } }) @@ -447,7 +446,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { let pandaGens = compileAllSnippet(source, passes); let expected_main = [ ...insnsCreateLexEnv_main, - new EcmaDefinefuncdyn("func", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.func", new Imm(0), new VReg()), new EcmaStglobalvar("func"), // global.func = func_func_1 new LdaiDyn(new Imm(1)), // value = 1 // ...insnsStoreLexVar_main, @@ -465,11 +464,11 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { pandaGens.forEach((pg) => { let scope = pg.getScope(); - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { expect(checkInstructions(pg.getInsns(), expected_main), "check main insns").to.be.true; expect(scope.getNumLexEnv(), "main scope has 0 lexvar").to.be.equal(0); // expect(scope.hasLexEnv(), "main scope has lexenv").to.be.true; - } else if (pg.internalName == "func") { + } else if (pg.internalName == "UnitTest.func") { expect(checkInstructions(pg.getInsns(), expected_func), "check func insns").to.be.true; expect(scope.getNumLexEnv(), "func scope has 1 lexvar").to.be.equal(0); @@ -505,7 +504,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { new LdaDyn(new VReg()), new StaDyn(new VReg()), ...insnsStoreLexVar_outer_2, - new EcmaDefinefuncdyn("#1#", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.#1#", new Imm(0), new VReg()), // returnStatement new StaDyn(new VReg()), new LdaDyn(new VReg()), @@ -536,13 +535,13 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { snippetCompiler.compile(source, passes); // check compile result! - let outerPg = snippetCompiler.getPandaGenByName("outer"); + let outerPg = snippetCompiler.getPandaGenByName("UnitTest.outer"); let outerScope = outerPg!.getScope(); let outerA = outerScope!.findLocal("a"); expect(outerA instanceof LocalVariable, "a in outer is local variable").to.be.true; // expect((outerScope).hasLexEnv(), "outer scope need to create lex env").to.be.true; expect((outerScope).getNumLexEnv(), "number of lexvar at outer scope").to.be.equal(2); - let anonymousPg = snippetCompiler.getPandaGenByName("#1#"); + let anonymousPg = snippetCompiler.getPandaGenByName("UnitTest.#1#"); let anonymousScope = anonymousPg!.getScope(); let anonymousA = anonymousScope!.findLocal("a"); let searchRlt = anonymousScope!.find("a"); @@ -551,7 +550,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { expect(anonymousA, "no a in anonymous function").to.be.undefined; // expect((anonymousScope).hasLexEnv(), "anonymous scope had lex env").to.be.true; expect((anonymousScope).getNumLexEnv()).to.be.equal(0); - let globalPg = snippetCompiler.getPandaGenByName("func_main_0"); + let globalPg = snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let globalScope = globalPg!.getScope(); let globalA = globalScope!.findLocal("a"); expect(globalA instanceof GlobalVariable, "globalA is GlobalVariable").to.be.true; diff --git a/ts2panda/tests/statements/functionDeclaration.test.ts b/ts2panda/tests/statements/functionDeclaration.test.ts index 3c9b990385..079b05e8e4 100644 --- a/ts2panda/tests/statements/functionDeclaration.test.ts +++ b/ts2panda/tests/statements/functionDeclaration.test.ts @@ -29,7 +29,6 @@ import { Label, LdaDyn, LdaiDyn, - ResultType, StaDyn, VReg } from "../../src/irnodes"; @@ -43,9 +42,10 @@ describe("FunctionDeclarationTest", function () { it('function definition in the global scope', function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile("function foo() {}"); + let funcInternalName = "UnitTest.foo"; let funcName = "foo"; let expected = [ - new EcmaDefinefuncdyn(funcName, new Imm(0), new VReg()), + new EcmaDefinefuncdyn(funcInternalName, new Imm(0), new VReg()), new EcmaStglobalvar(funcName), new EcmaReturnundefined() ]; @@ -64,7 +64,7 @@ describe("FunctionDeclarationTest", function () { function foo() {} `); let expected = [ - new EcmaDefinefuncdyn("#2#foo", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.#2#foo", new Imm(0), new VReg()), new EcmaStglobalvar("foo"), new EcmaReturnundefined() ]; @@ -81,12 +81,12 @@ describe("FunctionDeclarationTest", function () { snippetCompiler.compile(`function out() {function foo() {}}`); let funcReg = new VReg(); let expected = [ - new EcmaDefinefuncdyn("foo", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.foo", new Imm(0), new VReg()), new StaDyn(funcReg), new EcmaReturnundefined() ]; - let functionPg = snippetCompiler.getPandaGenByName("out"); + let functionPg = snippetCompiler.getPandaGenByName("UnitTest.out"); let insns = functionPg!.getInsns(); let functionScope = functionPg!.getScope(); @@ -103,7 +103,7 @@ describe("FunctionDeclarationTest", function () { snippetCompiler.compile("let foo = function() {}"); let insns = snippetCompiler.getGlobalInsns(); let expected = [ - new EcmaDefinefuncdyn("foo", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.foo", new Imm(0), new VReg()), new EcmaStlettoglobalrecord("foo"), new EcmaReturnundefined() ]; @@ -117,7 +117,7 @@ describe("FunctionDeclarationTest", function () { let endLabel = new Label(); let expected_main = [ - new EcmaDefinefuncdyn("test", new Imm(1), new VReg()), + new EcmaDefinefuncdyn("UnitTest.test", new Imm(1), new VReg()), new EcmaStglobalvar("test"), new EcmaReturnundefined() ]; @@ -133,10 +133,10 @@ describe("FunctionDeclarationTest", function () { ]; compilerunit.forEach(element => { - if (element.internalName == "func_main_0") { + if (element.internalName == "UnitTest.func_main_0") { let insns = element.getInsns(); expect(checkInstructions(insns, expected_main)).to.be.true; - } else if (element.internalName == "test") { + } else if (element.internalName == "UnitTest.test") { let insns = element.getInsns(); expect(checkInstructions(insns, expected_func)).to.be.true; let parameterLength = element.getParameterLength(); @@ -158,7 +158,7 @@ describe("FunctionDeclarationTest", function () { new EcmaReturnundefined(), ]; - let functionPg = snippetCompiler.getPandaGenByName("test"); + let functionPg = snippetCompiler.getPandaGenByName("UnitTest.test"); let insns = functionPg!.getInsns(); expect(checkInstructions(insns, expected_func)).to.be.true; diff --git a/ts2panda/tests/statements/variableDeclaration.test.ts b/ts2panda/tests/statements/variableDeclaration.test.ts index 1ce753c2d0..2e64168b4d 100644 --- a/ts2panda/tests/statements/variableDeclaration.test.ts +++ b/ts2panda/tests/statements/variableDeclaration.test.ts @@ -154,7 +154,7 @@ describe("VariableDeclarationTest", function () { it('var i in a function scope', function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile("function a() {var i;}"); - let funcPg = snippetCompiler.getPandaGenByName("a"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.a"); let functionScope = funcPg!.getScope(); let insns = funcPg!.getInsns(); let expected = [ @@ -172,7 +172,7 @@ describe("VariableDeclarationTest", function () { it('let i in a function scope', function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile("function a() {let i;}"); - let funcPg = snippetCompiler.getPandaGenByName("a"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.a"); let functionScope = funcPg!.getScope(); let insns = funcPg!.getInsns(); let expected = [ @@ -188,7 +188,7 @@ describe("VariableDeclarationTest", function () { it('const i in a function scope', function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile("function a() {const i = 5;}"); - let funcPg = snippetCompiler.getPandaGenByName("a"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.a"); let functionScope = funcPg!.getScope(); let insns = funcPg!.getInsns(); let expected = [ @@ -204,7 +204,7 @@ describe("VariableDeclarationTest", function () { it('let i in a local scope', function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile("{let i;}"); - let funcPg = snippetCompiler.getPandaGenByName("func_main_0"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let localScope = funcPg!.getScope(); let insns = funcPg!.getInsns(); diff --git a/ts2panda/tests/types/array.test.ts b/ts2panda/tests/types/array.test.ts index edaf90fdfa..9742c7faed 100644 --- a/ts2panda/tests/types/array.test.ts +++ b/ts2panda/tests/types/array.test.ts @@ -30,7 +30,7 @@ describe("array tests in array.test.ts", function() { it("test array with primitives", function() { let fileNames = 'tests/types/array/array_primitives.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -75,7 +75,7 @@ describe("array tests in array.test.ts", function() { it("test array with class", function() { let fileNames = 'tests/types/array/array_class.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -108,7 +108,7 @@ describe("array tests in array.test.ts", function() { it("test array with multi same primitive", function() { let fileNames = 'tests/types/array/array_multi_same_primi.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -159,7 +159,7 @@ describe("array tests in array.test.ts", function() { it("test array with multi same class", function() { let fileNames = 'tests/types/array/array_multi_same_class.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -193,7 +193,7 @@ describe("array tests in array.test.ts", function() { it("test array with union", function() { let fileNames = 'tests/types/array/array_union.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -222,7 +222,7 @@ describe("array tests in array.test.ts", function() { it("test array with object", function() { let fileNames = 'tests/types/array/array_object.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ diff --git a/ts2panda/tests/types/class.test.ts b/ts2panda/tests/types/class.test.ts index 6f076b68e6..7fa9db6c9b 100644 --- a/ts2panda/tests/types/class.test.ts +++ b/ts2panda/tests/types/class.test.ts @@ -30,7 +30,7 @@ describe("class tests in class.test.ts", function () { it("test class with no parameter in block", function () { let fileNames = 'tests/types/class/class_constr_no_para.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -66,7 +66,7 @@ describe("class tests in class.test.ts", function () { it("test class with parameter in block", function () { let fileNames = 'tests/types/class/class_constr_para.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -102,7 +102,7 @@ describe("class tests in class.test.ts", function () { it("test class fields type", function () { let fileNames = 'tests/types/class/class_fields.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -136,7 +136,7 @@ describe("class tests in class.test.ts", function () { it("test class methods type", function () { let fileNames = 'tests/types/class/class_methods.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -176,7 +176,7 @@ describe("class tests in class.test.ts", function () { it("test class static fields type", function () { let fileNames = 'tests/types/class/class_static_fields.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -210,7 +210,7 @@ describe("class tests in class.test.ts", function () { it("test class static methods type", function () { let fileNames = 'tests/types/class/class_static_methods.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -252,7 +252,7 @@ describe("class tests in class.test.ts", function () { it("test abstract class type", function () { let fileNames = 'tests/types/class/class_abstract.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -297,7 +297,7 @@ describe("class tests in class.test.ts", function () { it("test class implements type", function () { let fileNames = 'tests/types/class/class_implements.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ diff --git a/ts2panda/tests/types/function.test.ts b/ts2panda/tests/types/function.test.ts index 563e2b4c00..a9ebb11ad8 100644 --- a/ts2panda/tests/types/function.test.ts +++ b/ts2panda/tests/types/function.test.ts @@ -30,7 +30,7 @@ describe("function tests in function.test.ts", function () { it("test function with no parameter", function () { let fileNames = 'tests/types/function/function_no_para.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -60,7 +60,7 @@ describe("function tests in function.test.ts", function () { it("test function with muti parameter", function () { let fileNames = 'tests/types/function/function_multi_para.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -92,7 +92,7 @@ describe("function tests in function.test.ts", function () { it("test function with same type of paras and return", function () { let fileNames = 'tests/types/function/function_same_para_and_return.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -131,7 +131,7 @@ describe("function tests in function.test.ts", function () { it("test function with class as parameter", function () { let fileNames = 'tests/types/function/function_class_para.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -172,7 +172,7 @@ describe("function tests in function.test.ts", function () { it("test function with class as return", function () { let fileNames = 'tests/types/function/function_class_return.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ diff --git a/ts2panda/tests/types/object.test.ts b/ts2panda/tests/types/object.test.ts index eae94789e7..9d10d53062 100644 --- a/ts2panda/tests/types/object.test.ts +++ b/ts2panda/tests/types/object.test.ts @@ -30,7 +30,7 @@ describe("object tests in object.test.ts", function() { it("test object with primitives", function() { let fileNames = 'tests/types/object/object_primi.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -56,7 +56,7 @@ describe("object tests in object.test.ts", function() { it("test object with user defined type", function() { let fileNames = 'tests/types/object/object_class.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ diff --git a/ts2panda/tests/types/primitives.test.ts b/ts2panda/tests/types/primitives.test.ts index d05b7c1ba2..9fab6e6a77 100644 --- a/ts2panda/tests/types/primitives.test.ts +++ b/ts2panda/tests/types/primitives.test.ts @@ -30,7 +30,7 @@ describe("primitives tests in primitives.test.ts", function() { it("test primitives in block", function() { let fileNames = 'tests/types/primitives/primitives_in_block.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -60,7 +60,7 @@ describe("primitives tests in primitives.test.ts", function() { it("test number in function", function() { let fileNames = 'tests/types/primitives/primitives_in_function.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("numberFunc"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.numberFunc"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -100,7 +100,7 @@ describe("primitives tests in primitives.test.ts", function() { it("test primitives in for", function() { let fileNames = 'tests/types/primitives/primitives_in_for.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -131,7 +131,7 @@ describe("primitives tests in primitives.test.ts", function() { it("test primitives in if", function() { let fileNames = 'tests/types/primitives/primitives_in_if.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -161,7 +161,7 @@ describe("primitives tests in primitives.test.ts", function() { it("test primitives in class", function() { let fileNames = 'tests/types/primitives/primitives_in_class.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -198,7 +198,7 @@ describe("primitives tests in primitives.test.ts", function() { it("test primitives with only type annotations", function() { let fileNames = 'tests/types/primitives/primitives_only_type_annotation.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -226,7 +226,7 @@ describe("primitives tests in primitives.test.ts", function() { it("test primitives without type annotations", function() { let fileNames = 'tests/types/primitives/primitives_no_type_annotation.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ diff --git a/ts2panda/tests/types/union.test.ts b/ts2panda/tests/types/union.test.ts index a1dc513747..ec68027356 100644 --- a/ts2panda/tests/types/union.test.ts +++ b/ts2panda/tests/types/union.test.ts @@ -30,7 +30,7 @@ describe("union tests in union.test.ts", function () { it("test union with primitives", function () { let fileNames = 'tests/types/union/union_primitives.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -68,7 +68,7 @@ describe("union tests in union.test.ts", function () { it("test union with user defined type", function () { let fileNames = 'tests/types/union/union_userDefinedType.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -104,7 +104,7 @@ describe("union tests in union.test.ts", function () { it("test union with multi same primitives", function () { let fileNames = 'tests/types/union/union_multi_same_primi.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -130,7 +130,7 @@ describe("union tests in union.test.ts", function () { it("test union with multi same user defined type", function () { let fileNames = 'tests/types/union/union_multi_userDefinedType.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ diff --git a/ts2panda/tests/utils/base.ts b/ts2panda/tests/utils/base.ts index 2d9f7ed318..49c7c5b52d 100644 --- a/ts2panda/tests/utils/base.ts +++ b/ts2panda/tests/utils/base.ts @@ -146,7 +146,7 @@ export function compileAllSnippet(snippet: string, passes?: Pass[], literalBuffe jshelpers.bindSourceFile(sourceFile, {}); CmdOptions.isWatchEvaluateExpressionMode() ? setGlobalStrict(true) : setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(sourceFile, compileOptions)); - let compilerDriver = new CompilerDriver('UnitTest'); + let compilerDriver = new CompilerDriver('UnitTest', 'UnitTest'); if (!passes) { passes = []; @@ -166,7 +166,7 @@ export function compileMainSnippet(snippet: string, pandaGen?: PandaGen, scope?: // only return main function if (compileFunc) { compileUnits.filter((pg) => { - return (pg.internalName == "func_main_0"); + return (pg.internalName == "UnitTest.func_main_0"); }) } @@ -191,7 +191,7 @@ export function compileAfterSnippet(snippet: string, name:string, isCommonJs: bo } jshelpers.bindSourceFile(sourceFile, {}); setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(sourceFile, compileOptions)); - let compilerDriver = new CompilerDriver('UnitTest'); + let compilerDriver = new CompilerDriver('UnitTest', 'UnitTest'); compilerDriver.setCustomPasses([]); compilerDriver.compileUnitTest(sourceFile, []); compileUnits = compilerDriver.getCompilationUnits(); @@ -228,7 +228,7 @@ export class SnippetCompiler { } getGlobalInsns(): IRNode[] { - let root = this.getPandaGenByName("func_main_0"); + let root = this.getPandaGenByName("UnitTest.func_main_0"); if (root) { return root.getInsns(); } else { @@ -237,7 +237,7 @@ export class SnippetCompiler { } getGlobalScope(): Scope | undefined { - let globalPandaGen = this.getPandaGenByName("func_main_0"); + let globalPandaGen = this.getPandaGenByName("UnitTest.func_main_0"); return globalPandaGen ? globalPandaGen.getScope()!.getNearestVariableScope() : undefined; } -- Gitee From 73e8d389818cac3297ffa1853f5f87ac88e2c03a Mon Sep 17 00:00:00 2001 From: gavin1012_hw Date: Thu, 8 Sep 2022 21:15:19 +0800 Subject: [PATCH 06/10] Modification for ts2abc merging abc Signed-off-by: gavin1012_hw Change-Id: I4f9742cf5b25820b855706c5b428706e1dccb1b2 --- es2panda/aot/options.cpp | 3 ++- merge_abc/src/assemblyFileLocationProto.cpp | 12 ++++++---- merge_abc/src/assemblyFileLocationProto.h | 3 ++- merge_abc/src/assemblyFunctionProto.cpp | 2 +- merge_abc/src/assemblyLabelProto.cpp | 2 +- merge_abc/src/assemblyRecordProto.cpp | 2 +- test262/run_sunspider.py | 26 +++++++++++++++++---- ts2panda/src/cmdOptions.ts | 7 +++--- ts2panda/src/compilerDriver.ts | 2 +- ts2panda/src/index.ts | 1 + ts2panda/src/ts2panda.ts | 2 +- ts2panda/ts2abc/ts2abc_options.h | 2 +- 12 files changed, 43 insertions(+), 21 deletions(-) diff --git a/es2panda/aot/options.cpp b/es2panda/aot/options.cpp index 87e6dcf953..533164d164 100644 --- a/es2panda/aot/options.cpp +++ b/es2panda/aot/options.cpp @@ -148,7 +148,8 @@ bool Options::Parse(int argc, const char **argv) panda::PandArg base64Output("base64Output", false, "output panda file content as base64 to std out"); panda::PandArg sourceFile("source-file", "", "specify the file path info recorded in generated abc"); - panda::PandArg outputProto("outputProto", "", "specify output name of serializd protobuf file (.bin)"); + panda::PandArg outputProto("outputProto", "", + "specify the output name for serializd protobuf file (.protoBin)"); panda::PandArg opCacheFile("cache-file", "", "cache file for incremental compile"); panda::PandArg opNpmModuleEntryList("npm-module-entry-list", "", "entry list file for module compile"); panda::PandArg opMergeAbc("merge-abc", false, "Compile as merge abc"); diff --git a/merge_abc/src/assemblyFileLocationProto.cpp b/merge_abc/src/assemblyFileLocationProto.cpp index 9f4a0f23bd..e3ef08e976 100644 --- a/merge_abc/src/assemblyFileLocationProto.cpp +++ b/merge_abc/src/assemblyFileLocationProto.cpp @@ -21,14 +21,16 @@ void FileLocation::Serialize(const panda::pandasm::FileLocation &location, proto protoLocation.set_wholeline(location.whole_line); protoLocation.set_boundleft(location.bound_left); protoLocation.set_boundright(location.bound_right); + protoLocation.set_linenumber(location.line_number); protoLocation.set_isdefined(location.is_defined); } -void FileLocation::Deserialize(const protoPanda::FileLocation &protoLocation, panda::pandasm::FileLocation &location) +void FileLocation::Deserialize(const protoPanda::FileLocation &protoLocation, + std::optional &location) { - location.whole_line = protoLocation.wholeline(); - location.bound_left = protoLocation.boundleft(); - location.bound_right = protoLocation.boundright(); - location.is_defined = protoLocation.isdefined(); + std::string wholeLine = protoLocation.wholeline(); + panda::pandasm::FileLocation fileLocation(wholeLine, protoLocation.boundleft(), protoLocation.boundright(), + protoLocation.linenumber(), protoLocation.isdefined()); + location = fileLocation; } } // panda::proto diff --git a/merge_abc/src/assemblyFileLocationProto.h b/merge_abc/src/assemblyFileLocationProto.h index 83afe39022..2c13264974 100644 --- a/merge_abc/src/assemblyFileLocationProto.h +++ b/merge_abc/src/assemblyFileLocationProto.h @@ -23,7 +23,8 @@ namespace panda::proto { class FileLocation { public: static void Serialize(const panda::pandasm::FileLocation &location, protoPanda::FileLocation &protoLocation); - static void Deserialize(const protoPanda::FileLocation &protoLocation, panda::pandasm::FileLocation &location); + static void Deserialize(const protoPanda::FileLocation &protoLocation, + std::optional &location); }; } // panda::proto #endif diff --git a/merge_abc/src/assemblyFunctionProto.cpp b/merge_abc/src/assemblyFunctionProto.cpp index 5b3e638703..97b8d19bdc 100644 --- a/merge_abc/src/assemblyFunctionProto.cpp +++ b/merge_abc/src/assemblyFunctionProto.cpp @@ -159,7 +159,7 @@ void Function::Deserialize(const protoPanda::Function &protoFunction, panda::pan SourceLocation::Deserialize(protoFunction.bodylocation(), function.body_location); if (protoFunction.has_filelocation()) { - FileLocation::Deserialize(protoFunction.filelocation(), function.file_location.value()); + FileLocation::Deserialize(protoFunction.filelocation(), function.file_location); } } } // panda::proto diff --git a/merge_abc/src/assemblyLabelProto.cpp b/merge_abc/src/assemblyLabelProto.cpp index f355a80490..63b18cefc1 100644 --- a/merge_abc/src/assemblyLabelProto.cpp +++ b/merge_abc/src/assemblyLabelProto.cpp @@ -31,7 +31,7 @@ void Label::Deserialize(const protoPanda::Label &protoLabel, panda::pandasm::Lab label.name = protoLabel.name(); if (protoLabel.has_filelocation()) { protoPanda::FileLocation protoLocation = protoLabel.filelocation(); - FileLocation::Deserialize(protoLocation, label.file_location.value()); + FileLocation::Deserialize(protoLocation, label.file_location); } } } // panda::proto diff --git a/merge_abc/src/assemblyRecordProto.cpp b/merge_abc/src/assemblyRecordProto.cpp index cfd7d34d44..9409ff1297 100644 --- a/merge_abc/src/assemblyRecordProto.cpp +++ b/merge_abc/src/assemblyRecordProto.cpp @@ -56,7 +56,7 @@ void Record::Deserialize(const protoPanda::Record &protoRecord, panda::pandasm:: record.source_file = protoRecord.sourcefile(); if (protoRecord.has_filelocation()) { const auto &protoLocation = protoRecord.filelocation(); - FileLocation::Deserialize(protoLocation, record.file_location.value()); + FileLocation::Deserialize(protoLocation, record.file_location); } } } // panda::proto diff --git a/test262/run_sunspider.py b/test262/run_sunspider.py index 4318109091..e7c6eb3d2a 100755 --- a/test262/run_sunspider.py +++ b/test262/run_sunspider.py @@ -230,9 +230,23 @@ class ArkProgram(): output_file = os.path.splitext(os.path.join(BASE_OUT_DIR, os.path.split(dependency)[1]))[0] output_abc = f"{output_file}.abc" + proto_bin_file = f"{output_file}.bin" + output_abc_name = os.path.basename(output_abc) + frontend_tool = self.ark_frontend_binary - cmd_args = [frontend_tool, dependency, '--output', output_abc, - '--module'] + merge_abc_binary = self.args.merge_abc_binary + merge_abc_mode = self.merge_abc_mode + + if merge_abc_mode != "0": + cmd_args = [frontend_tool, dependency, '--outputProto', + proto_bin_file, '--module', '--merge-abc'] + proc = subprocess.call(cmd_args) + cmd_args = [merge_abc_binary, '--input', proto_bin_file, '--suffix', + "bin", '--outputFilePath', BASE_OUT_DIR, '--output', + output_abc_name] + else: + cmd_args = [frontend_tool, dependency, '--output', output_abc, + '--module', '--merge-abc'] proc = subprocess.Popen(cmd_args) proc.wait() @@ -262,8 +276,10 @@ class ArkProgram(): if self.ark_frontend == ARK_FRONTEND_LIST[0]: mod_opt_index = 3 if merge_abc_mode != "0": + # '--opt-level=0' is added due to failure in optimizer, should be removed later cmd_args = ['node', '--expose-gc', frontend_tool, - js_file, '-output-proto', proto_bin_file] + js_file, '--output-proto', proto_bin_file, + '--opt-level=0'] else: cmd_args = ['node', '--expose-gc', frontend_tool, js_file, '-o', out_file] @@ -274,7 +290,7 @@ class ArkProgram(): mod_opt_index = 1 if merge_abc_mode != "0": cmd_args = [frontend_tool, '--outputProto', - proto_bin_file, js_file] + proto_bin_file, js_file, '--merge-abc'] else: cmd_args = [frontend_tool, '--opt-level=' + str(self.opt_level), '--function-threads=' + @@ -391,6 +407,8 @@ class ArkProgram(): cmd_args = [self.ark_tool, ICU_PATH, f'{file_name_pre}.abc'] + record_name = os.path.splitext(os.path.split(self.js_file)[1])[0] + cmd_args.append(f'--entry-point={record_name}') retcode = exec_command(cmd_args) if retcode: print_command(cmd_args) diff --git a/ts2panda/src/cmdOptions.ts b/ts2panda/src/cmdOptions.ts index f331b6c92f..94b151eaff 100644 --- a/ts2panda/src/cmdOptions.ts +++ b/ts2panda/src/cmdOptions.ts @@ -52,7 +52,7 @@ const ts2pandaOptions = [ { name: 'source-file', type: String, defaultValue: "", description: "specify the file path info recorded in generated abc" }, { name: 'generate-tmp-file', type: Boolean, defaultValue: false, description: "whether to generate intermediate temporary files"}, { name: 'record-name', type: String, defaultValue: "", description: "specify the record name." }, - { name: 'output-proto', type: String, defaultValue: "", description: "specify output name of serializd protobuf file (.bin)" }, + { name: 'output-proto', type: String, defaultValue: "", description: "specify the output name for serializd protobuf file (.protoBin)" }, ] @@ -317,16 +317,15 @@ export class CmdOptions { return this.options["source-file"]; } -<<<<<<< HEAD static needGenerateTmpFile(): boolean { if (!this.options) { return false; } return this.options["generate-tmp-file"]; -======= + } + static getOutputproto(): string { return this.options["output-proto"]; ->>>>>>> Support serialize proto bin file in ts2abc } // @ts-ignore diff --git a/ts2panda/src/compilerDriver.ts b/ts2panda/src/compilerDriver.ts index d676991444..e7bbd900cc 100644 --- a/ts2panda/src/compilerDriver.ts +++ b/ts2panda/src/compilerDriver.ts @@ -359,7 +359,7 @@ export class CompilerDriver { name = "func_main_0"; } else if (ts.isConstructorDeclaration(node)) { let classNode = node.parent; - name = this.getInternalNameForCtor(classNode, node); + return this.getInternalNameForCtor(classNode, node); } else { let funcNode = node; name = (recorder.getScopeOfNode(funcNode)).getFuncName(); diff --git a/ts2panda/src/index.ts b/ts2panda/src/index.ts index 50c9706a7b..ec8aff434d 100644 --- a/ts2panda/src/index.ts +++ b/ts2panda/src/index.ts @@ -220,6 +220,7 @@ const es2abcBinaryName = /^win/.test(require('os').platform()) ? "es2abc.exe" : const es2abcBase64Input = "--base64Input"; const es2abcDebuggerEvaluateFlag = "--debugger-evaluate-expression"; const es2abcBase64Output = "--base64Output"; +// need to specify the record name as 'Base64Output' in es2abc's commandline; cancel the opMergeAbc option function callEs2pandaToolChain(ideIputStr: string) { let commandLine = es2abcBinaryPath + es2abcBinaryName + " " + es2abcBase64Input + " \"" + ideIputStr + "\" " + diff --git a/ts2panda/src/ts2panda.ts b/ts2panda/src/ts2panda.ts index 7620c2dad5..786fe57a7c 100644 --- a/ts2panda/src/ts2panda.ts +++ b/ts2panda/src/ts2panda.ts @@ -335,7 +335,7 @@ export class Ts2Panda { } typeInfo = Ts2Panda.dumpInstTypeMap(pg); - if (funcName.indexOf("func_main_0") !== -1) { + if (funcName.endsWith("func_main_0")) { let exportedTypes = PandaGen.getExportedTypes(); let declareddTypes = PandaGen.getDeclaredTypes(); if (exportedTypes.size != 0) { diff --git a/ts2panda/ts2abc/ts2abc_options.h b/ts2panda/ts2abc/ts2abc_options.h index 9195b1dd7e..03d56e78e9 100755 --- a/ts2panda/ts2abc/ts2abc_options.h +++ b/ts2panda/ts2abc/ts2abc_options.h @@ -224,7 +224,7 @@ namespace panda::ts2abc { panda::PandArg compile_by_pipe_arg_{ "compile-by-pipe", false, R"(Compile a json file that is passed by pipe)"}; panda::PandArg compiler_output_proto_{ "output-proto", "", - R"(compiler proto serialize binary output (.bin))"}; + R"(Specify the output name for serializd protobuf file (.protoBin))"}; panda::PandArg Tail_Arg1_arg_{ "ARG_1", "", R"(Path to input(json file) or path to output(ark bytecode)" " when 'compile-by-pipe' enabled)"}; -- Gitee From b0a8c43a2d09fcfdf192309f897a8e4942118b51 Mon Sep 17 00:00:00 2001 From: hufeng Date: Fri, 16 Sep 2022 11:59:48 +0800 Subject: [PATCH 07/10] Fix several typeinfo's literal tag Signed-off-by: hufeng Change-Id: I17557bb8bc4c7465289413539e18777b7aaf2a03 --- ts2panda/src/base/typeSystem.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ts2panda/src/base/typeSystem.ts b/ts2panda/src/base/typeSystem.ts index 51a1b4d3d4..f6503268f7 100644 --- a/ts2panda/src/base/typeSystem.ts +++ b/ts2panda/src/base/typeSystem.ts @@ -238,7 +238,7 @@ export class TypeSummary extends BaseType { if (isGlobalDeclare()) { definedTypeNum += userDefinedTypeStartIndex - BuiltinType._HEAD; } - summaryLiterals.push(new Literal(LiteralTag.INTEGER, definedTypeNum)); + summaryLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, definedTypeNum)); summaryLiterals.push(new Literal(LiteralTag.INTEGER, this.anonymousRedirect.length)); for (let element of this.anonymousRedirect) { summaryLiterals.push(new Literal(LiteralTag.STRING, element)); @@ -422,7 +422,7 @@ export class ClassType extends BaseType { classTypeLiterals.push(new Literal(LiteralTag.INTEGER, L2Type.CLASS)); classTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.modifier)); - classTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.extendsHeritage)); + classTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.extendsHeritage)); classTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.implementsHeritages.length)); this.implementsHeritages.forEach(heritage => { classTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, heritage)); @@ -480,7 +480,7 @@ export class ClassInstType extends BaseType { let classInstLiterals: Array = new Array(); classInstLiterals.push(new Literal(LiteralTag.INTEGER, L2Type.CLASSINST)); - classInstLiterals.push(new Literal(LiteralTag.INTEGER, this.shiftedReferredClassIndex)); + classInstLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.shiftedReferredClassIndex)); classInstBuf.addLiterals(...classInstLiterals); return classInstBuf; -- Gitee From bfc6b8b409519e9e38f9dce642177751edc16f76 Mon Sep 17 00:00:00 2001 From: hufeng Date: Wed, 21 Sep 2022 17:31:24 +0800 Subject: [PATCH 08/10] Set typeinfo's field into file record Signed-off-by: hufeng Change-Id: Ic8f8af8eacdcfc275e6f6565131fdbd2fd0c1835 --- ts2panda/src/compilerDriver.ts | 1 + ts2panda/ts2abc/ts2abc.cpp | 43 ++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/ts2panda/src/compilerDriver.ts b/ts2panda/src/compilerDriver.ts index e7bbd900cc..81e56e82ee 100644 --- a/ts2panda/src/compilerDriver.ts +++ b/ts2panda/src/compilerDriver.ts @@ -180,6 +180,7 @@ export class CompilerDriver { listenErrorEvent(ts2abcProc); try { + // must keep [dumpRecord] at first Ts2Panda.dumpRecord(ts2abcProc, this.recordName); Ts2Panda.dumpCmdOptions(ts2abcProc); diff --git a/ts2panda/ts2abc/ts2abc.cpp b/ts2panda/ts2abc/ts2abc.cpp index 99e772ce5f..df0b719264 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -1115,26 +1115,29 @@ static void ParseSingleModule(const Json::Value &rootValue, panda::pandasm::Prog static void ParseSingleTypeInfo(const Json::Value &rootValue, panda::pandasm::Program &prog) { - auto typeInfoRecord = rootValue["ti"]; - auto typeFlag = typeInfoRecord["tf"].asBool(); - auto typeSummaryIndex = typeInfoRecord["tsi"].asUInt(); - auto ecmaTypeInfoRecord = panda::pandasm::Record("_ESTypeInfoRecord", LANG_EXT); - ecmaTypeInfoRecord.metadata->SetAccessFlags(panda::ACC_PUBLIC); - - auto typeFlagField = panda::pandasm::Field(LANG_EXT); - typeFlagField.name = "typeFlag"; - typeFlagField.type = panda::pandasm::Type("u8", 0); - typeFlagField.metadata->SetValue(panda::pandasm::ScalarValue::Create( - static_cast(typeFlag))); - ecmaTypeInfoRecord.field_list.emplace_back(std::move(typeFlagField)); - auto typeSummaryIndexField = panda::pandasm::Field(LANG_EXT); - typeSummaryIndexField.name = "typeSummaryIndex"; - typeSummaryIndexField.type = panda::pandasm::Type("u32", 0); - typeSummaryIndexField.metadata->SetValue(panda::pandasm::ScalarValue::Create( - static_cast(typeSummaryIndex))); - ecmaTypeInfoRecord.field_list.emplace_back(std::move(typeSummaryIndexField)); - - prog.record_table.emplace(ecmaTypeInfoRecord.name, std::move(ecmaTypeInfoRecord)); + auto iter = prog.record_table.find(g_recordName); + if (iter != prog.record_table.end()) { + auto &rec = iter->second; + + auto typeInfoRecord = rootValue["ti"]; + auto typeFlag = typeInfoRecord["tf"].asBool(); + auto typeSummaryIndex = typeInfoRecord["tsi"].asUInt(); + + auto typeFlagField = panda::pandasm::Field(LANG_EXT); + typeFlagField.name = "typeFlag"; + typeFlagField.type = panda::pandasm::Type("u8", 0); + typeFlagField.metadata->SetValue(panda::pandasm::ScalarValue::Create( + static_cast(typeFlag))); + + auto typeSummaryIndexField = panda::pandasm::Field(LANG_EXT); + typeSummaryIndexField.name = "typeSummaryIndex"; + typeSummaryIndexField.type = panda::pandasm::Type("u32", 0); + typeSummaryIndexField.metadata->SetValue(panda::pandasm::ScalarValue::Create( + static_cast(typeSummaryIndex))); + + rec.field_list.emplace_back(std::move(typeFlagField)); + rec.field_list.emplace_back(std::move(typeSummaryIndexField)); + } } static int ParseSmallPieceJson(const std::string &subJson, panda::pandasm::Program &prog) -- Gitee From 946258a4b94b5f197160b812b247e44a293d7018 Mon Sep 17 00:00:00 2001 From: ctw-ian Date: Thu, 15 Sep 2022 19:59:12 +0800 Subject: [PATCH 09/10] Change literal index to literal id Issue: Signed-off-by: ctw-ian Change-Id: I116caa7857c0c981c2c7b281742589cdbecdcf52 --- es2panda/compiler/core/compilerContext.cpp | 5 +- es2panda/compiler/core/compilerContext.h | 9 +- es2panda/compiler/core/compilerImpl.cpp | 2 +- es2panda/compiler/core/emitter/emitter.cpp | 7 +- ts2panda/src/base/literal.ts | 4 +- ts2panda/src/base/typeSystem.ts | 55 +++-- ts2panda/src/compilerDriver.ts | 6 +- ts2panda/src/index.ts | 5 + ts2panda/src/ts2panda.ts | 22 +- ts2panda/ts2abc/ts2abc.cpp | 236 +++++++++++++-------- ts2panda/ts2abc/ts2abc.h | 3 +- 11 files changed, 231 insertions(+), 123 deletions(-) diff --git a/es2panda/compiler/core/compilerContext.cpp b/es2panda/compiler/core/compilerContext.cpp index 81434e630b..f3549110db 100644 --- a/es2panda/compiler/core/compilerContext.cpp +++ b/es2panda/compiler/core/compilerContext.cpp @@ -20,9 +20,10 @@ namespace panda::es2panda::compiler { CompilerContext::CompilerContext(binder::Binder *binder, bool isDebug, bool isDebuggerEvaluateExpressionMode, - bool isMergeAbc, std::string sourceFile) + bool isMergeAbc, std::string sourceFile, util::StringView recordName) : binder_(binder), isDebug_(isDebug), isDebuggerEvaluateExpressionMode_(isDebuggerEvaluateExpressionMode), - isMergeAbc_(isMergeAbc), sourceFile_(sourceFile), emitter_(std::make_unique(this)) + isMergeAbc_(isMergeAbc), sourceFile_(sourceFile), emitter_(std::make_unique(this)), + recordName_(recordName) { } diff --git a/es2panda/compiler/core/compilerContext.h b/es2panda/compiler/core/compilerContext.h index 6acc7b83fb..5348d48fd3 100644 --- a/es2panda/compiler/core/compilerContext.h +++ b/es2panda/compiler/core/compilerContext.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -35,7 +36,7 @@ class Emitter; class CompilerContext { public: CompilerContext(binder::Binder *binder, bool isDebug, bool isDebuggerEvaluateExpressionMode, - bool isMergeAbc, std::string sourceFile); + bool isMergeAbc, std::string sourceFile, util::StringView recordName); NO_COPY_SEMANTIC(CompilerContext); NO_MOVE_SEMANTIC(CompilerContext); ~CompilerContext() = default; @@ -96,6 +97,11 @@ public: return hotfixHelper_; } + util::StringView RecordName() const + { + return recordName_; + } + private: binder::Binder *binder_; int32_t literalBufferIdx_ {0}; @@ -106,6 +112,7 @@ private: std::string sourceFile_; std::unique_ptr emitter_; util::Hotfix *hotfixHelper_ {nullptr}; + util::StringView recordName_; }; } // namespace panda::es2panda::compiler diff --git a/es2panda/compiler/core/compilerImpl.cpp b/es2panda/compiler/core/compilerImpl.cpp index 5561346b97..368783be96 100644 --- a/es2panda/compiler/core/compilerImpl.cpp +++ b/es2panda/compiler/core/compilerImpl.cpp @@ -39,7 +39,7 @@ panda::pandasm::Program *CompilerImpl::Compile(parser::Program *program, const e const std::string &debugInfoSourceFile) { CompilerContext context(program->Binder(), options.isDebug, options.isDebuggerEvaluateExpressionMode, - options.mergeAbc, debugInfoSourceFile); + options.mergeAbc, debugInfoSourceFile, program->RecordName()); if (hotfixHelper_ != nullptr) { context.AddHotfixHelper(hotfixHelper_); diff --git a/es2panda/compiler/core/emitter/emitter.cpp b/es2panda/compiler/core/emitter/emitter.cpp index 53064fdc1d..c5c9da995c 100644 --- a/es2panda/compiler/core/emitter/emitter.cpp +++ b/es2panda/compiler/core/emitter/emitter.cpp @@ -380,6 +380,7 @@ void Emitter::AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerCon { std::lock_guard lock(m_); + auto moduleLiteral = std::string(context->RecordName()) + "_" + std::to_string(module->Index()); if (context->IsMergeAbc()) { auto moduleIdxField = panda::pandasm::Field(LANG_EXT); moduleIdxField.name = "moduleRecordIdx"; @@ -398,8 +399,8 @@ void Emitter::AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerCon auto moduleIdxField = panda::pandasm::Field(LANG_EXT); moduleIdxField.name = std::string {context->Binder()->Program()->SourceFile()}; moduleIdxField.type = panda::pandasm::Type("u32", 0); - moduleIdxField.metadata->SetValue(panda::pandasm::ScalarValue::Create( - static_cast(module->Index()))); + moduleIdxField.metadata->SetValue(panda::pandasm::ScalarValue::Create( + static_cast(moduleLiteral))); ecmaModuleRecord.field_list.emplace_back(std::move(moduleIdxField)); if (context->HotfixHelper()) { @@ -409,7 +410,7 @@ void Emitter::AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerCon } auto &moduleLiteralsBuffer = module->Buffer(); auto literalArrayInstance = panda::pandasm::LiteralArray(std::move(moduleLiteralsBuffer)); - prog_->literalarray_table.emplace(std::to_string(module->Index()), std::move(literalArrayInstance)); + prog_->literalarray_table.emplace(static_cast(moduleLiteral), std::move(literalArrayInstance)); } void Emitter::DumpAsm(const panda::pandasm::Program *prog) diff --git a/ts2panda/src/base/literal.ts b/ts2panda/src/base/literal.ts index 50d7c9d52c..4af140c9ec 100644 --- a/ts2panda/src/base/literal.ts +++ b/ts2panda/src/base/literal.ts @@ -25,6 +25,8 @@ export enum LiteralTag { // 0x0a - 0x15 for ARRAY_Type ASYNCGENERATOR = 22, LITERALBUFFERINDEX = 23, + LITERALARRAY = 24, + BUILTINTYPEINDEX = 25, NULLVALUE = 255 } @@ -69,4 +71,4 @@ export class LiteralBuffer { } return this.lb[index]; } -} \ No newline at end of file +} diff --git a/ts2panda/src/base/typeSystem.ts b/ts2panda/src/base/typeSystem.ts index f6503268f7..4a7583519f 100644 --- a/ts2panda/src/base/typeSystem.ts +++ b/ts2panda/src/base/typeSystem.ts @@ -26,6 +26,8 @@ import { } from "./literal"; import { hasAbstractModifier } from "./util"; import { CmdOptions } from "../cmdOptions"; +import { getLiteralKey } from "../index"; +import { CompilerDriver } from "../compilerDriver"; export enum PrimitiveType { ANY, @@ -143,7 +145,8 @@ export enum AccessFlag { PROTECTED } -type ClassMemberFunction = ts.MethodDeclaration | ts.ConstructorDeclaration | ts.GetAccessorDeclaration | ts.SetAccessorDeclaration; +type ClassMemberFunction = ts.MethodDeclaration | ts.ConstructorDeclaration | + ts.GetAccessorDeclaration | ts.SetAccessorDeclaration; export abstract class BaseType { @@ -151,6 +154,15 @@ export abstract class BaseType { protected typeChecker = TypeChecker.getInstance(); protected typeRecorder = TypeRecorder.getInstance(); + protected transferType2Literal(type: number, literals: Array) { + if (type >= literalBufferIndexShift) { + let litId = getLiteralKey(CompilerDriver.srcNode, type - literalBufferIndexShift); + literals.push(new Literal(LiteralTag.LITERALARRAY, litId)); + } else { + literals.push(new Literal(LiteralTag.BUILTINTYPEINDEX, type)); + } + } + protected addCurrentType(node: ts.Node, index: number) { this.typeRecorder.addType2Index(node, index); } @@ -233,12 +245,17 @@ export class TypeSummary extends BaseType { transfer2LiteralBuffer(): LiteralBuffer { let countBuf = new LiteralBuffer(); let summaryLiterals: Array = new Array(); - summaryLiterals.push(new Literal(LiteralTag.INTEGER, L2Type._COUNTER)); let definedTypeNum = this.userDefinedClassNum; if (isGlobalDeclare()) { definedTypeNum += userDefinedTypeStartIndex - BuiltinType._HEAD; } - summaryLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, definedTypeNum)); + // summaryLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, definedTypeNum)); + summaryLiterals.push(new Literal(LiteralTag.INTEGER, definedTypeNum)); + let literalIdx = PandaGen.getLiteralArrayBuffer.length; + for (let i = 1; i <= definedTypeNum; i++) { + summaryLiterals.push(new Literal(LiteralTag.LITERALARRAY, + getLiteralKey(CompilerDriver.srcNode, literalIdx + i))); + } summaryLiterals.push(new Literal(LiteralTag.INTEGER, this.anonymousRedirect.length)); for (let element of this.anonymousRedirect) { summaryLiterals.push(new Literal(LiteralTag.STRING, element)); @@ -424,8 +441,8 @@ export class ClassType extends BaseType { classTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.extendsHeritage)); classTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.implementsHeritages.length)); - this.implementsHeritages.forEach(heritage => { - classTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, heritage)); + this.implementsHeritages.forEach(heritage => { // heritage types + this.transferType2Literal(heritage, classTypeLiterals); }); // record unstatic fields and methods @@ -446,7 +463,7 @@ export class ClassType extends BaseType { classTypeLiterals.push(new Literal(LiteralTag.INTEGER, transferredTarget.size)); transferredTarget.forEach((typeInfo, name) => { classTypeLiterals.push(new Literal(LiteralTag.STRING, name)); - classTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, typeInfo[0])); // typeIndex + this.transferType2Literal(typeInfo[0], classTypeLiterals); classTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[1])); // accessFlag classTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[2])); // readonly }); @@ -458,7 +475,7 @@ export class ClassType extends BaseType { classTypeLiterals.push(new Literal(LiteralTag.INTEGER, transferredTarget.size)); transferredTarget.forEach((typeInfo, name) => { classTypeLiterals.push(new Literal(LiteralTag.STRING, name)); - classTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, typeInfo)); + this.transferType2Literal(typeInfo, classTypeLiterals); }); } } @@ -480,7 +497,7 @@ export class ClassInstType extends BaseType { let classInstLiterals: Array = new Array(); classInstLiterals.push(new Literal(LiteralTag.INTEGER, L2Type.CLASSINST)); - classInstLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.shiftedReferredClassIndex)); + this.transferType2Literal(this.shiftedReferredClassIndex, classInstLiterals); classInstBuf.addLiterals(...classInstLiterals); return classInstBuf; @@ -585,16 +602,16 @@ export class FunctionType extends BaseType { funcTypeLiterals.push(new Literal(LiteralTag.STRING, this.name)); if (this.containThisParam) { funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, 1)); // marker for having 'this' param - funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.parameters[0])); + this.transferType2Literal(this.parameters[0], funcTypeLiterals); funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.parameters.length - 1)); - for (let i = 1; i < this.parameters.length; i++) { - funcTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.parameters[i])); + for (let i = 1; i < this.parameters.length; i++) { // normal parameter types + this.transferType2Literal(this.parameters[i], funcTypeLiterals); } } else { funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, 0)); // marker for not having 'this' param funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.parameters.length)); for (let i = 0; i < this.parameters.length; i++) { - funcTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.parameters[i])); + this.transferType2Literal(this.parameters[i], funcTypeLiterals); } } @@ -675,7 +692,7 @@ export class UnionType extends BaseType { UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, L2Type.UNION)); UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.unionedTypeArray.length)); for (let type of this.unionedTypeArray) { - UnionTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, type)); + this.transferType2Literal(type, UnionTypeLiterals); } UnionTypeBuf.addLiterals(...UnionTypeLiterals); return UnionTypeBuf; @@ -720,7 +737,7 @@ export class ArrayType extends BaseType { let arrayBuf = new LiteralBuffer(); let arrayLiterals: Array = new Array(); arrayLiterals.push(new Literal(LiteralTag.INTEGER, L2Type.ARRAY)); - arrayLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.referedTypeIndex)); + this.transferType2Literal(this.referedTypeIndex, arrayLiterals); arrayBuf.addLiterals(...arrayLiterals); return arrayBuf; } @@ -755,7 +772,7 @@ export class ObjectType extends BaseType { objLiterals.push(new Literal(LiteralTag.INTEGER, this.properties.size)); this.properties.forEach((typeIndex, name) => { objLiterals.push(new Literal(LiteralTag.STRING, name)); - objLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, typeIndex)); + this.transferType2Literal(typeIndex, objLiterals); }); objTypeBuf.addLiterals(...objLiterals); return objTypeBuf; @@ -868,7 +885,7 @@ export class InterfaceType extends BaseType { interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.heritages.length)); this.heritages.forEach(heritage => { - interfaceTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, heritage)); + this.transferType2Literal(heritage, interfaceTypeLiterals) }); // record fields and methods @@ -885,7 +902,7 @@ export class InterfaceType extends BaseType { interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, transferredTarget.size)); transferredTarget.forEach((typeInfo, name) => { interfaceTypeLiterals.push(new Literal(LiteralTag.STRING, name)); - interfaceTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, typeInfo[0])); // typeIndex + this.transferType2Literal(typeInfo[0], interfaceTypeLiterals); interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[1])); // accessFlag interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[2])); // readonly }); @@ -896,7 +913,7 @@ export class InterfaceType extends BaseType { interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, transferredTarget.length)); transferredTarget.forEach(method => { - interfaceTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, method)); + this.transferType2Literal(method, interfaceTypeLiterals); }); } } @@ -928,7 +945,7 @@ export class BuiltinContainerType extends BaseType { UnionTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.builtinTypeIndex)); UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.containerArray.length)); for (let type of this.containerArray) { - UnionTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, type)); + this.transferType2Literal(type, UnionTypeLiterals); } UnionTypeBuf.addLiterals(...UnionTypeLiterals); return UnionTypeBuf; diff --git a/ts2panda/src/compilerDriver.ts b/ts2panda/src/compilerDriver.ts index 81e56e82ee..042ba6fafd 100644 --- a/ts2panda/src/compilerDriver.ts +++ b/ts2panda/src/compilerDriver.ts @@ -17,7 +17,8 @@ import { writeFileSync } from "fs"; import * as ts from "typescript"; import { addVariableToScope } from "./addVariable2Scope"; import { AssemblyDumper } from "./assemblyDumper"; -import { hasDefaultKeywordModifier, hasExportKeywordModifier, initiateTs2abc, listenChildExit, listenErrorEvent, terminateWritePipe, getRecordTypeFlag } from "./base/util"; +import { hasDefaultKeywordModifier, hasExportKeywordModifier, initiateTs2abc, listenChildExit, + listenErrorEvent, terminateWritePipe, getRecordTypeFlag } from "./base/util"; import { CmdOptions } from "./cmdOptions"; import { Compiler @@ -45,6 +46,7 @@ import { Ts2Panda } from "./ts2panda"; import { TypeRecorder } from "./typeRecorder"; import { LiteralBuffer } from "./base/literal"; import { findOuterNodeOfParenthesis } from "./expression/parenthesizedExpression"; +import { getRecordName } from "./index"; export class PendingCompilationUnit { constructor( @@ -59,6 +61,7 @@ export class PendingCompilationUnit { * It handles all dependencies and run passes. */ export class CompilerDriver { + static srcNode: ts.SourceFile | undefined = undefined; static isTsFile: boolean = false; private fileName: string; private recordName: string; @@ -183,6 +186,7 @@ export class CompilerDriver { // must keep [dumpRecord] at first Ts2Panda.dumpRecord(ts2abcProc, this.recordName); Ts2Panda.dumpCmdOptions(ts2abcProc); + Ts2Panda.dumpRecordName(ts2abcProc, getRecordName(CompilerDriver.srcNode)); for (let i = 0; i < this.pendingCompilationUnits.length; i++) { let unit: PendingCompilationUnit = this.pendingCompilationUnits[i]; diff --git a/ts2panda/src/index.ts b/ts2panda/src/index.ts index ec8aff434d..704d1b5332 100644 --- a/ts2panda/src/index.ts +++ b/ts2panda/src/index.ts @@ -119,6 +119,7 @@ function main(fileNames: string[], options: ts.CompilerOptions) { } let outputBinName = getOutputBinName(node); let compilerDriver = new CompilerDriver(outputBinName, getRecordName(node)); + CompilerDriver.srcNode = node; setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(node, options)); compilerDriver.compile(node); compilerDriver.showStatistics(); @@ -187,6 +188,10 @@ function getRecordName(node: ts.SourceFile): string { return recordName; } +export function getLiteralKey(node: ts.SourceFile, idx:number): string { + return getRecordName(node) + "_" + idx; +} + function specifyCustomLib(customLib) { Compiler.Options.Default["lib"] = customLib; let curFiles = fs.readdirSync(__dirname); diff --git a/ts2panda/src/ts2panda.ts b/ts2panda/src/ts2panda.ts index 786fe57a7c..18ea35c4df 100644 --- a/ts2panda/src/ts2panda.ts +++ b/ts2panda/src/ts2panda.ts @@ -55,6 +55,7 @@ import { ModuleScope } from "./scope"; import { TypeRecorder } from "./typeRecorder"; import { isGlobalDeclare } from "./strictMode"; import { isFunctionLikeDeclaration } from "./syntaxCheckHelper"; +import { getLiteralKey } from "./index"; const dollarSign: RegExp = /\$/g; @@ -65,7 +66,8 @@ const JsonType = { "literal_arr": 3, "module": 4, "options": 5, - 'type_info': 6 + 'type_info': 6, + 'record_name': 7 }; export class Ts2Panda { static strings: Set = new Set(); @@ -192,6 +194,20 @@ export class Ts2Panda { }); } + static dumpRecordName(ts2abc: any, recordName: string) { + let recordNameObject = { + "t": JsonType.record_name, + "rn": recordName + } + + let jsonRecordName = escapeUnicode(JSON.stringify(recordNameObject, null, 2)); + jsonRecordName = "$" + jsonRecordName.replace(dollarSign, '#$') + "$"; + if (CmdOptions.isEnableDebugLog()) { + Ts2Panda.jsonString += jsonRecordName; + } + ts2abc.stdio[3].write(jsonRecordName + '\n'); + } + static dumpCmdOptions(ts2abc: any): void { let options = { "t": JsonType.options, @@ -437,9 +453,9 @@ export class Ts2Panda { static dumpTypeInfoRecord(ts2abc: any, recordType: boolean): void { let enableTypeRecord = getRecordTypeFlag(recordType); - let typeSummaryIndex = 0; + let typeSummaryIndex = getLiteralKey(CompilerDriver.srcNode, 0); if (enableTypeRecord) { - typeSummaryIndex = TypeRecorder.getInstance().getTypeSummaryIndex(); + typeSummaryIndex = getLiteralKey(CompilerDriver.srcNode, TypeRecorder.getInstance().getTypeSummaryIndex()); } let typeInfo = { 'tf': enableTypeRecord, diff --git a/ts2panda/ts2abc/ts2abc.cpp b/ts2panda/ts2abc/ts2abc.cpp index df0b719264..a478c896ba 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -45,9 +45,12 @@ bool g_displayTypeinfo = false; bool g_isDtsFile = false; std::string g_optLogLevel = "error"; uint32_t g_literalArrayCount = 0; +int32_t g_newLiteralArrayIndex = -1; static constexpr const char* TSTYPE_ANNO_RECORD_NAME = "_ESTypeAnnotation"; static constexpr const char* TSTYPE_ANNO_ELEMENT_NAME = "_TypeOfInstruction"; std::string g_compilerOutputProto = ""; +std::string g_recordName = ""; +constexpr uint32_t LITERALBUFFERINDEXOFFSET = 100; constexpr std::size_t BOUND_LEFT = 0; constexpr std::size_t BOUND_RIGHT = 0; @@ -168,6 +171,11 @@ static std::string ConvertUtf8ToMUtf8(const std::string &data) return ConvertUtf16ToMUtf8(u16Data, u16DataSize); } +static std::string GetLiteralId(int64_t index) +{ + return g_recordName + "_" + std::to_string(index); +} + static std::string ParseUnicodeEscapeString(const std::string &data) { const int unicodeEscapeSymbolLen = 2; @@ -265,8 +273,16 @@ static void ParseLiteral(const Json::Value &literal, std::vector(panda::panda_file::LiteralTag::LITERALBUFFERINDEX): { - valueLiteral.tag_ = panda::panda_file::LiteralTag::LITERALBUFFERINDEX; - valueLiteral.value_ = static_cast(literal["v"].asInt()); + UNREACHABLE(); + } + case static_cast(panda::panda_file::LiteralTag::LITERALARRAY): { + valueLiteral.tag_ = panda::panda_file::LiteralTag::LITERALARRAY; + valueLiteral.value_ = ParseString(literal["v"].asString()); + break; + } + case static_cast(panda::panda_file::LiteralTag::BUILTINTYPEINDEX): { + valueLiteral.tag_ = panda::panda_file::LiteralTag::BUILTINTYPEINDEX; + valueLiteral.value_ = static_cast(literal["v"].asInt()); break; } case static_cast(panda::panda_file::LiteralTag::NULLVALUE): { @@ -649,27 +665,108 @@ static std::vector> GetInstTypeMap(const Json::Valu return instTypeMap; } -static void ParseFunctionTypeInfo(const Json::Value &function, panda::pandasm::Function &pandaFunc) +static void ParseFunctionTypeInfo(const Json::Value &function, panda::pandasm::Function &pandaFunc, + panda::pandasm::Program &prog) { auto instTypeMap = GetInstTypeMap(function, pandaFunc); if (!instTypeMap.empty()) { - std::vector elements; + std::vector literalArray; for (auto &it : instTypeMap) { - elements.emplace_back(panda::pandasm::ScalarValue::Create(it.first)); - elements.emplace_back(panda::pandasm::ScalarValue::Create(it.second)); + panda::pandasm::LiteralArray::Literal instTagLiteral; + panda::pandasm::LiteralArray::Literal instValueLiteral; + instTagLiteral.tag_ = panda::panda_file::LiteralTag::TAGVALUE; + instTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::INTEGER); + literalArray.emplace_back(instTagLiteral); + instValueLiteral.tag_ = panda::panda_file::LiteralTag::INTEGER; + instValueLiteral.value_ = static_cast(it.first); + literalArray.emplace_back(instValueLiteral); + + panda::pandasm::LiteralArray::Literal typeTagLiteral; + panda::pandasm::LiteralArray::Literal typeValueLiteral; + typeTagLiteral.tag_ = panda::panda_file::LiteralTag::TAGVALUE; + auto type = it.second; + if (type < LITERALBUFFERINDEXOFFSET) { + typeTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::INTEGER); + typeValueLiteral.tag_ = panda::panda_file::LiteralTag::BUILTINTYPEINDEX; + typeValueLiteral.value_ = static_cast(type); + } else { + typeTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::LITERALARRAY); + typeValueLiteral.tag_ = panda::panda_file::LiteralTag::LITERALARRAY; + std::string typeId = g_recordName + "_" + std::to_string(type - LITERALBUFFERINDEXOFFSET); + typeValueLiteral.value_ = typeId; + } + literalArray.emplace_back(typeTagLiteral); + literalArray.emplace_back(typeValueLiteral); } + std::string litId = GetLiteralId(g_newLiteralArrayIndex--); + std::cerr << "--------------function typeinfo litId------------------" << litId << std::endl; + auto literalarrayInstance = panda::pandasm::LiteralArray(literalArray); + prog.literalarray_table.emplace(litId, std::move(literalarrayInstance)); + panda::pandasm::AnnotationData funcAnnotation(TSTYPE_ANNO_RECORD_NAME); panda::pandasm::AnnotationElement typeOfVregElement( - TSTYPE_ANNO_ELEMENT_NAME, std::make_unique(panda::pandasm::ArrayValue( - panda::pandasm::Value::Type::U32, elements))); + TSTYPE_ANNO_ELEMENT_NAME, std::make_unique( + panda::pandasm::ScalarValue::Create(litId))); funcAnnotation.AddElement(std::move(typeOfVregElement)); const_cast&>(pandaFunc.metadata->GetAnnotations()).push_back( std::move(funcAnnotation)); } } -static void ParseFunctionExportedType(const Json::Value &function, panda::pandasm::Function &pandaFunc) +static std::string CreateLiteralArrayForType(const Json::Value &types, panda::pandasm::Program &prog) +{ + std::vector literalArray; + for (Json::ArrayIndex i = 0; i < types.size(); i++) { + auto type = types[i]; + if (!type.isObject()) { + continue; + } + + panda::pandasm::LiteralArray::Literal symbolTagLiteral; + panda::pandasm::LiteralArray::Literal symbolValueLiteral; + std::string symbol = ""; + if (type.isMember("symbol") && type["symbol"].isString()) { + symbol = type["symbol"].asString(); + } + symbolTagLiteral.tag_ = panda::panda_file::LiteralTag::TAGVALUE; + symbolTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::STRING); + symbolValueLiteral.tag_ = panda::panda_file::LiteralTag::STRING; + symbolValueLiteral.value_ = symbol; + literalArray.emplace_back(symbolTagLiteral); + literalArray.emplace_back(symbolValueLiteral); + + panda::pandasm::LiteralArray::Literal typeTagLiteral; + panda::pandasm::LiteralArray::Literal typeValueLiteral; + uint32_t typeIndex = 0; + if (type.isMember("type") && type["type"].isInt()) { + typeIndex = type["type"].asUInt(); + } + typeTagLiteral.tag_ = panda::panda_file::LiteralTag::TAGVALUE; + if (typeIndex < LITERALBUFFERINDEXOFFSET) { + typeTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::BUILTINTYPEINDEX); + typeValueLiteral.tag_ = panda::panda_file::LiteralTag::BUILTINTYPEINDEX; + typeValueLiteral.value_ = static_cast(typeIndex); + } else { + typeTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::LITERALARRAY); + typeValueLiteral.tag_ = panda::panda_file::LiteralTag::LITERALARRAY; + std::string litId = g_recordName + "_" + std::to_string(typeIndex); + typeValueLiteral.value_ = litId; + } + + literalArray.emplace_back(typeTagLiteral); + literalArray.emplace_back(typeValueLiteral); + } + + std::string litId = GetLiteralId(g_newLiteralArrayIndex--); + std::cerr << "--------------function literalArray of type litId------------------" << litId << std::endl; + auto literalarrayInstance = panda::pandasm::LiteralArray(literalArray); + prog.literalarray_table.emplace(litId, std::move(literalarrayInstance)); + return litId; +} + +static void ParseFunctionExportedType(const Json::Value &function, panda::pandasm::Function &pandaFunc, + panda::pandasm::Program &prog) { std::string funcName = ""; if (function.isMember("n") && function["n"].isString()) { @@ -682,42 +779,12 @@ static void ParseFunctionExportedType(const Json::Value &function, panda::pandas if (function.isMember("es2t") && function["es2t"].isArray()) { auto exportedTypes = function["es2t"]; panda::pandasm::AnnotationData funcAnnotation(TSTYPE_ANNO_RECORD_NAME); - std::vector symbolElements; - std::vector symbolTypeElements; - for (Json::ArrayIndex i = 0; i < exportedTypes.size(); i++) { - auto exportedType = exportedTypes[i]; - if (!exportedType.isObject()) { - continue; - } - - std::string exportedSymbol = ""; - if (exportedType.isMember("symbol") && exportedType["symbol"].isString()) { - exportedSymbol = exportedType["symbol"].asString(); - } - - uint32_t typeIndex = 0; - if (exportedType.isMember("type") && exportedType["type"].isInt()) { - typeIndex = exportedType["type"].asUInt(); - } - - panda::pandasm::ScalarValue symbol( - panda::pandasm::ScalarValue::Create(exportedSymbol)); - symbolElements.emplace_back(std::move(symbol)); - panda::pandasm::ScalarValue tIndex( - panda::pandasm::ScalarValue::Create(typeIndex)); - symbolTypeElements.emplace_back(std::move(tIndex)); - } - - std::string symbolAnnotationName = "exportedSymbols"; - panda::pandasm::AnnotationElement exportedSymbolsElement(symbolAnnotationName, - std::make_unique(panda::pandasm::ArrayValue( - panda::pandasm::Value::Type::STRING, symbolElements))); - funcAnnotation.AddElement(std::move(exportedSymbolsElement)); + std::string litId = CreateLiteralArrayForType(exportedTypes, prog); std::string symbolTypeAnnotationName = "exportedSymbolTypes"; panda::pandasm::AnnotationElement exportedSymbolTypesElement(symbolTypeAnnotationName, - std::make_unique(panda::pandasm::ArrayValue( - panda::pandasm::Value::Type::U32, symbolTypeElements))); + std::make_unique( + panda::pandasm::ScalarValue::Create(litId))); funcAnnotation.AddElement(std::move(exportedSymbolTypesElement)); const_cast&>( @@ -725,7 +792,8 @@ static void ParseFunctionExportedType(const Json::Value &function, panda::pandas } } -static void ParseFunctionDeclaredType(const Json::Value &function, panda::pandasm::Function &pandaFunc) +static void ParseFunctionDeclaredType(const Json::Value &function, panda::pandasm::Function &pandaFunc, + panda::pandasm::Program &prog) { std::string funcName = ""; if (function.isMember("n") && function["n"].isString()) { @@ -738,42 +806,12 @@ static void ParseFunctionDeclaredType(const Json::Value &function, panda::pandas if (function.isMember("ds2t") && function["ds2t"].isArray()) { auto declaredTypes = function["ds2t"]; panda::pandasm::AnnotationData funcAnnotation(TSTYPE_ANNO_RECORD_NAME); - std::vector symbolElements; - std::vector symbolTypeElements; - for (Json::ArrayIndex i = 0; i < declaredTypes.size(); i++) { - auto declaredType = declaredTypes[i]; - if (!declaredType.isObject()) { - continue; - } - - std::string declaredSymbol = ""; - if (declaredType.isMember("symbol") && declaredType["symbol"].isString()) { - declaredSymbol = declaredType["symbol"].asString(); - } - - uint32_t typeIndex = 0; - if (declaredType.isMember("type") && declaredType["type"].isInt()) { - typeIndex = declaredType["type"].asUInt(); - } - - panda::pandasm::ScalarValue symbol( - panda::pandasm::ScalarValue::Create(declaredSymbol)); - symbolElements.emplace_back(std::move(symbol)); - panda::pandasm::ScalarValue tIndex( - panda::pandasm::ScalarValue::Create(typeIndex)); - symbolTypeElements.emplace_back(std::move(tIndex)); - } - - std::string symbolAnnotationName = "declaredSymbols"; - panda::pandasm::AnnotationElement declaredSymbolsElement(symbolAnnotationName, - std::make_unique(panda::pandasm::ArrayValue( - panda::pandasm::Value::Type::STRING, symbolElements))); - funcAnnotation.AddElement(std::move(declaredSymbolsElement)); + std::string litId = CreateLiteralArrayForType(declaredTypes, prog); std::string symbolTypeAnnotationName = "declaredSymbolTypes"; panda::pandasm::AnnotationElement declaredSymbolTypesElement(symbolTypeAnnotationName, - std::make_unique(panda::pandasm::ArrayValue( - panda::pandasm::Value::Type::U32, symbolTypeElements))); + std::make_unique( + panda::pandasm::ScalarValue::Create(litId))); funcAnnotation.AddElement(std::move(declaredSymbolTypesElement)); const_cast&>(pandaFunc.metadata->GetAnnotations()).push_back( @@ -781,7 +819,7 @@ static void ParseFunctionDeclaredType(const Json::Value &function, panda::pandas } } -static panda::pandasm::Function ParseFunction(const Json::Value &function) +static panda::pandasm::Function ParseFunction(const Json::Value &function, panda::pandasm::Program &prog) { auto pandaFunc = GetFunctionDefintion(function); ParseFunctionInstructions(function, pandaFunc); @@ -791,9 +829,9 @@ static panda::pandasm::Function ParseFunction(const Json::Value &function) ParseFunctionCatchTables(function, pandaFunc); // parsing call opt type ParseFunctionCallType(function, pandaFunc); - ParseFunctionTypeInfo(function, pandaFunc); - ParseFunctionExportedType(function, pandaFunc); - ParseFunctionDeclaredType(function, pandaFunc); + ParseFunctionTypeInfo(function, pandaFunc, prog); + ParseFunctionExportedType(function, pandaFunc, prog); + ParseFunctionDeclaredType(function, pandaFunc, prog); if (g_isDtsFile && pandaFunc.name != "func_main_0") { pandaFunc.metadata->SetAttribute("external"); @@ -831,7 +869,7 @@ static void SetCommonjsField(panda::pandasm::Program &prog, bool isCommonjs) } } -static void SetModuleRecordIdx(panda::pandasm::Program &prog, uint32_t moduleIdx) +static void SetModuleRecordIdx(panda::pandasm::Program &prog) { auto iter = prog.record_table.find(g_recordName); if (iter != prog.record_table.end()) { @@ -839,8 +877,9 @@ static void SetModuleRecordIdx(panda::pandasm::Program &prog, uint32_t moduleIdx auto moduleIdxField = panda::pandasm::Field(LANG_EXT); moduleIdxField.name = "moduleRecordIdx"; moduleIdxField.type = panda::pandasm::Type("u32", 0); - moduleIdxField.metadata->SetValue(panda::pandasm::ScalarValue::Create( - static_cast(moduleIdx))); + std::string moduleId = GetLiteralId(g_newLiteralArrayIndex); + moduleIdxField.metadata->SetValue(panda::pandasm::ScalarValue::Create( + moduleId)); rec.field_list.emplace_back(std::move(moduleIdxField)); } @@ -958,7 +997,7 @@ static void ParseOptions(const Json::Value &rootValue, panda::pandasm::Program & static void ParseSingleFunc(const Json::Value &rootValue, panda::pandasm::Program &prog) { - auto function = ParseFunction(rootValue["fb"]); + auto function = ParseFunction(rootValue["fb"], prog); prog.function_table.emplace(function.name.c_str(), std::move(function)); } @@ -987,7 +1026,12 @@ static void ParseSingleLiteralBuf(const Json::Value &rootValue, panda::pandasm:: } auto literalarrayInstance = panda::pandasm::LiteralArray(literalArray); - prog.literalarray_table.emplace(std::to_string(g_literalArrayCount++), std::move(literalarrayInstance)); + std::string litId = g_recordName + "_" + std::to_string(g_literalArrayCount++); + if (prog.literalarray_table.find(litId) != prog.literalarray_table.end()) { + std::cerr << "----litId is alrerady exist--------" << litId << std::endl; + } + std::cerr << "---------literal id----------" << litId << std::endl; + prog.literalarray_table.emplace(litId, std::move(literalarrayInstance)); } static void ParseModuleRequests(const Json::Value &moduleRequests, @@ -1107,10 +1151,12 @@ static void ParseSingleModule(const Json::Value &rootValue, panda::pandasm::Prog ParseIndirectExportEntries(moduleRecord["indirectExportEntries"], moduleLiteralArray); ParseStarExportEntries(moduleRecord["starExportEntries"], moduleLiteralArray); - SetModuleRecordIdx(prog, g_literalArrayCount); + SetModuleRecordIdx(prog); + std::string moduleId = GetLiteralId(g_newLiteralArrayIndex--); + std::cerr << "--------------moduleId------------------" << moduleId << std::endl; auto moduleLiteralarrayInstance = panda::pandasm::LiteralArray(moduleLiteralArray); - prog.literalarray_table.emplace(std::to_string(g_literalArrayCount++), std::move(moduleLiteralarrayInstance)); + prog.literalarray_table.emplace(moduleId, std::move(moduleLiteralarrayInstance)); } static void ParseSingleTypeInfo(const Json::Value &rootValue, panda::pandasm::Program &prog) @@ -1121,19 +1167,19 @@ static void ParseSingleTypeInfo(const Json::Value &rootValue, panda::pandasm::Pr auto typeInfoRecord = rootValue["ti"]; auto typeFlag = typeInfoRecord["tf"].asBool(); - auto typeSummaryIndex = typeInfoRecord["tsi"].asUInt(); + auto typeSummaryIndex = typeInfoRecord["tsi"].asString(); auto typeFlagField = panda::pandasm::Field(LANG_EXT); typeFlagField.name = "typeFlag"; typeFlagField.type = panda::pandasm::Type("u8", 0); typeFlagField.metadata->SetValue(panda::pandasm::ScalarValue::Create( - static_cast(typeFlag))); + static_cast(typeFlag))); auto typeSummaryIndexField = panda::pandasm::Field(LANG_EXT); typeSummaryIndexField.name = "typeSummaryIndex"; typeSummaryIndexField.type = panda::pandasm::Type("u32", 0); - typeSummaryIndexField.metadata->SetValue(panda::pandasm::ScalarValue::Create( - static_cast(typeSummaryIndex))); + typeSummaryIndexField.metadata->SetValue( + panda::pandasm::ScalarValue::Create(typeSummaryIndex)); rec.field_list.emplace_back(std::move(typeFlagField)); rec.field_list.emplace_back(std::move(typeSummaryIndexField)); @@ -1192,6 +1238,12 @@ static int ParseSmallPieceJson(const std::string &subJson, panda::pandasm::Progr } break; } + case static_cast(JsonType::RECORDNAME): { + if (rootValue.isMember("rn") && rootValue["rn"].isString()) { + g_recordName = rootValue["rn"].asString(); + } + break; + } default: { std::cerr << "Unreachable json type: " << type << std::endl; return RETURN_FAILED; @@ -1331,6 +1383,8 @@ bool GenerateProgram([[maybe_unused]] const std::string &data, const std::string Logd("parsing done, calling pandasm\n"); + std::cerr << "---------num of literal array------------" << prog.literalarray_table.size() << std::endl; + std::string compilerOutputProto = g_compilerOutputProto; if (options.GetCompilerOutputProto().size() > 0) { compilerOutputProto = options.GetCompilerOutputProto(); diff --git a/ts2panda/ts2abc/ts2abc.h b/ts2panda/ts2abc/ts2abc.h index 421205bb35..4b4658dfdb 100644 --- a/ts2panda/ts2abc/ts2abc.h +++ b/ts2panda/ts2abc/ts2abc.h @@ -36,7 +36,8 @@ enum class JsonType { LITERALBUFFER, MODULE, OPTIONS, - TYPEINFO + TYPEINFO, + RECORDNAME }; constexpr int RETURN_SUCCESS = 0; -- Gitee From e415f1cc3ac8904d26d9bd2b644c9f00d5a25761 Mon Sep 17 00:00:00 2001 From: ctw-ian Date: Tue, 20 Sep 2022 21:29:56 +0800 Subject: [PATCH 10/10] Add key to literalbuffer Signed-off-by: ctw-ian Change-Id: I383a799c44d99d92fbf52bede60c83622394745f --- es2panda/compiler/core/compileQueue.cpp | 2 +- es2panda/compiler/core/emitter/emitter.cpp | 11 +-- es2panda/compiler/core/emitter/emitter.h | 2 +- ts2panda/src/base/literal.ts | 89 ++++++++++--------- ts2panda/src/cmdOptions.ts | 2 +- .../src/expression/arrayLiteralExpression.ts | 5 +- .../src/expression/objectLiteralExpression.ts | 5 +- ts2panda/src/index.ts | 4 +- ts2panda/src/pandagen.ts | 16 +++- ts2panda/src/statement/classStatement.ts | 5 +- ts2panda/src/ts2panda.ts | 3 +- ts2panda/ts2abc/ts2abc.cpp | 34 ++++--- 12 files changed, 101 insertions(+), 77 deletions(-) diff --git a/es2panda/compiler/core/compileQueue.cpp b/es2panda/compiler/core/compileQueue.cpp index f8709aa36d..ea11d2bd33 100644 --- a/es2panda/compiler/core/compileQueue.cpp +++ b/es2panda/compiler/core/compileQueue.cpp @@ -78,7 +78,7 @@ void CompileFunctionJob::Run() FunctionEmitter funcEmitter(&allocator, &pg); funcEmitter.Generate(context_->HotfixHelper()); - context_->GetEmitter()->AddFunction(&funcEmitter); + context_->GetEmitter()->AddFunction(&funcEmitter, context_); if (dependant_) { dependant_->Signal(); diff --git a/es2panda/compiler/core/emitter/emitter.cpp b/es2panda/compiler/core/emitter/emitter.cpp index c5c9da995c..b217dca3c4 100644 --- a/es2panda/compiler/core/emitter/emitter.cpp +++ b/es2panda/compiler/core/emitter/emitter.cpp @@ -359,7 +359,7 @@ Emitter::~Emitter() delete prog_; } -void Emitter::AddFunction(FunctionEmitter *func) +void Emitter::AddFunction(FunctionEmitter *func, CompilerContext *context) { std::lock_guard lock(m_); @@ -369,7 +369,8 @@ void Emitter::AddFunction(FunctionEmitter *func) for (auto &[idx, buf] : func->LiteralBuffers()) { auto literalArrayInstance = panda::pandasm::LiteralArray(std::move(buf)); - prog_->literalarray_table.emplace(std::to_string(idx), std::move(literalArrayInstance)); + auto litId = std::string(context->Binder()->Program()->RecordName()) + "_" + std::to_string(idx); + prog_->literalarray_table.emplace(litId, std::move(literalArrayInstance)); } auto *function = func->Function(); @@ -380,13 +381,13 @@ void Emitter::AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerCon { std::lock_guard lock(m_); - auto moduleLiteral = std::string(context->RecordName()) + "_" + std::to_string(module->Index()); + auto moduleLiteral = std::string(context->Binder()->Program()->RecordName()) + "_" + std::to_string(module->Index()); if (context->IsMergeAbc()) { auto moduleIdxField = panda::pandasm::Field(LANG_EXT); moduleIdxField.name = "moduleRecordIdx"; moduleIdxField.type = panda::pandasm::Type("u32", 0); - moduleIdxField.metadata->SetValue(panda::pandasm::ScalarValue::Create( - static_cast(module->Index()))); + moduleIdxField.metadata->SetValue(panda::pandasm::ScalarValue::Create( + static_cast(moduleLiteral))); rec_->field_list.emplace_back(std::move(moduleIdxField)); if (context->HotfixHelper()) { diff --git a/es2panda/compiler/core/emitter/emitter.h b/es2panda/compiler/core/emitter/emitter.h index f689a4e57c..979356faab 100644 --- a/es2panda/compiler/core/emitter/emitter.h +++ b/es2panda/compiler/core/emitter/emitter.h @@ -100,7 +100,7 @@ public: NO_COPY_SEMANTIC(Emitter); NO_MOVE_SEMANTIC(Emitter); - void AddFunction(FunctionEmitter *func); + void AddFunction(FunctionEmitter *func, CompilerContext *context); void AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerContext *context); static void DumpAsm(const panda::pandasm::Program *prog); panda::pandasm::Program *Finalize(bool dumpDebugInfo, util::Hotfix *hotfixHelper); diff --git a/ts2panda/src/base/literal.ts b/ts2panda/src/base/literal.ts index 4af140c9ec..2cfb246236 100644 --- a/ts2panda/src/base/literal.ts +++ b/ts2panda/src/base/literal.ts @@ -14,61 +14,66 @@ */ export enum LiteralTag { - BOOLEAN = 1, - INTEGER = 2, - DOUBLE = 4, - STRING = 5, - METHOD = 6, - GENERATOR = 7, - ACCESSOR = 8, - METHODAFFILIATE = 9, - // 0x0a - 0x15 for ARRAY_Type - ASYNCGENERATOR = 22, - LITERALBUFFERINDEX = 23, - LITERALARRAY = 24, - BUILTINTYPEINDEX = 25, - NULLVALUE = 255 + BOOLEAN = 1, + INTEGER = 2, + DOUBLE = 4, + STRING = 5, + METHOD = 6, + GENERATOR = 7, + ACCESSOR = 8, + METHODAFFILIATE = 9, + // 0x0a - 0x15 for ARRAY_Type + ASYNCGENERATOR = 22, + LITERALBUFFERINDEX = 23, + LITERALARRAY = 24, + BUILTINTYPEINDEX = 25, + NULLVALUE = 255 } export class Literal { - private t: LiteralTag; - private v: any; + private t: LiteralTag; + private v: any; - constructor(t: LiteralTag, v: any) { - this.t = t; - this.v = v; - } + constructor(t: LiteralTag, v: any) { + this.t = t; + this.v = v; + } - getTag() { - return this.t; - } + getTag() { + return this.t; + } - getValue() { - return this.v; - } + getValue() { + return this.v; + } } export class LiteralBuffer { - private lb: Literal[] = []; + private k: string; + private lb: Literal[] = []; + + constructor() {}; - constructor() { }; + addLiterals(...literals: Array) { + this.lb.push(...literals); + } - addLiterals(...literals: Array) { - this.lb.push(...literals); - } + getLiterals() { + return this.lb; + } - getLiterals() { - return this.lb; - } + isEmpty() { + return this.lb.length == 0; + } - isEmpty() { - return this.lb.length == 0; - } + getLiteral(index: number) { + if (index >= this.lb.length || this.lb.length <=0) { + return ; + } + return this.lb[index]; + } - getLiteral(index: number) { - if (index >= this.lb.length || this.lb.length <=0) { - return ; + setKey(key: string) { + this.k = key; } - return this.lb[index]; - } } diff --git a/ts2panda/src/cmdOptions.ts b/ts2panda/src/cmdOptions.ts index 94b151eaff..9cba00c744 100644 --- a/ts2panda/src/cmdOptions.ts +++ b/ts2panda/src/cmdOptions.ts @@ -282,7 +282,7 @@ export class CmdOptions { return false; } - return !this.options["record-type"]; + return this.options["record-type"]; } static needRecordDtsType(): boolean { diff --git a/ts2panda/src/expression/arrayLiteralExpression.ts b/ts2panda/src/expression/arrayLiteralExpression.ts index 1d16860880..3f8b3152a3 100644 --- a/ts2panda/src/expression/arrayLiteralExpression.ts +++ b/ts2panda/src/expression/arrayLiteralExpression.ts @@ -162,9 +162,8 @@ function emitCreateArrayWithBuffer(pandaGen: PandaGen, literalBuffer: LiteralBuf if (literalBuffer.isEmpty()) { pandaGen.createEmptyArray(element); } else { - let literalArrayBuffer = PandaGen.getLiteralArrayBuffer(); - let bufferIdx = literalArrayBuffer.length; - literalArrayBuffer.push(literalBuffer); + let bufferIdx = PandaGen.getLiteralArrayBuffer().length; + PandaGen.appendLiteralArrayBuffer(literalBuffer); pandaGen.createArrayWithBuffer(element, bufferIdx); } } diff --git a/ts2panda/src/expression/objectLiteralExpression.ts b/ts2panda/src/expression/objectLiteralExpression.ts index b7f34f646d..4b4adffe43 100644 --- a/ts2panda/src/expression/objectLiteralExpression.ts +++ b/ts2panda/src/expression/objectLiteralExpression.ts @@ -112,9 +112,8 @@ function createObject(expr: ts.ObjectLiteralExpression, pandaGen: PandaGen, objR if (literalBuffer.isEmpty()) { pandaGen.createEmptyObject(expr); } else { - let literalArrayBuffer = PandaGen.getLiteralArrayBuffer(); - let bufferIdx = literalArrayBuffer.length; - literalArrayBuffer.push(literalBuffer); + let bufferIdx = PandaGen.getLiteralArrayBuffer().length; + PandaGen.appendLiteralArrayBuffer(literalBuffer); if (hasMethod) { let env = compiler.getCurrentEnv(); pandaGen.createObjectHavingMethod(expr, bufferIdx, env); diff --git a/ts2panda/src/index.ts b/ts2panda/src/index.ts index 704d1b5332..9e6b80bdd2 100644 --- a/ts2panda/src/index.ts +++ b/ts2panda/src/index.ts @@ -177,7 +177,7 @@ function getDtsFiles(libDir: string): string[] { return dtsFiles; } -function getRecordName(node: ts.SourceFile): string { +export function getRecordName(node: ts.SourceFile): string { let recordName = CmdOptions.getRecordName(); if (recordName == "") { @@ -189,7 +189,7 @@ function getRecordName(node: ts.SourceFile): string { } export function getLiteralKey(node: ts.SourceFile, idx:number): string { - return getRecordName(node) + "_" + idx; + return `${getRecordName(node)}_${idx}`; } function specifyCustomLib(customLib) { diff --git a/ts2panda/src/pandagen.ts b/ts2panda/src/pandagen.ts index a89c41a6c2..2d16699504 100644 --- a/ts2panda/src/pandagen.ts +++ b/ts2panda/src/pandagen.ts @@ -192,6 +192,8 @@ import { import { CatchTable } from "./statement/tryStatement"; import { TypeRecorder } from "./typeRecorder"; import { Variable } from "./variable"; +import { CompilerDriver } from "./compilerDriver"; +import { getLiteralKey } from "./index"; export class PandaGen { // @ts-ignore @@ -306,14 +308,24 @@ export class PandaGen { } } + static appendLiteralArrayBuffer(litBuf: LiteralBuffer) { + let index = PandaGen.literalArrayBuffer.length; + litBuf.setKey(getLiteralKey(CompilerDriver.srcNode, index)); + PandaGen.literalArrayBuffer.push(litBuf); + } + static appendTypeArrayBuffer(type: BaseType): number { let index = PandaGen.literalArrayBuffer.length; - PandaGen.literalArrayBuffer.push(type.transfer2LiteralBuffer()); + let typeBuf = type.transfer2LiteralBuffer(); + typeBuf.setKey(getLiteralKey(CompilerDriver.srcNode, index)); + PandaGen.literalArrayBuffer.push(typeBuf); return index; } static setTypeArrayBuffer(type: BaseType, index: number) { - PandaGen.literalArrayBuffer[index] = type.transfer2LiteralBuffer(); + let typeBuf = type.transfer2LiteralBuffer(); + typeBuf.setKey(getLiteralKey(CompilerDriver.srcNode, index)); + PandaGen.literalArrayBuffer[index] = typeBuf; } getFirstStmt() { diff --git a/ts2panda/src/statement/classStatement.ts b/ts2panda/src/statement/classStatement.ts index ace78ccba5..c135ac8132 100644 --- a/ts2panda/src/statement/classStatement.ts +++ b/ts2panda/src/statement/classStatement.ts @@ -252,15 +252,14 @@ function compileUnCompiledVariable(compiler: Compiler, prop: Property, classReg: function createClassLiteralBuf(compiler: Compiler, classBuffer: LiteralBuffer, stmt: ts.ClassLikeDeclaration, vregs: VReg[]) { - let classLiteralBuf = PandaGen.getLiteralArrayBuffer(); - classLiteralBuf.push(classBuffer); + PandaGen.appendLiteralArrayBuffer(classBuffer); let ctorNode = compiler.getRecorder().getCtorOfClass(stmt); let internalName = compiler.getCompilerDriver().getInternalNameForCtor(stmt, ctorNode); let pandaGen = compiler.getPandaGen(); let parameterLength = getParameterLength4Ctor(stmt); - let buffIdx = classLiteralBuf.length - 1; + let buffIdx = PandaGen.getLiteralArrayBuffer().length - 1; pandaGen.defineClassWithBuffer(stmt, internalName, buffIdx, parameterLength, vregs[0]); pandaGen.storeAccumulator(stmt, vregs[1]); } diff --git a/ts2panda/src/ts2panda.ts b/ts2panda/src/ts2panda.ts index 18ea35c4df..5e8184f161 100644 --- a/ts2panda/src/ts2panda.ts +++ b/ts2panda/src/ts2panda.ts @@ -219,7 +219,8 @@ export class Ts2Panda { "opt_log_level": CmdOptions.getOptLogLevel(), "display_typeinfo": CmdOptions.getDisplayTypeinfo(), "is_dts_file": isGlobalDeclare(), - "output-proto": CmdOptions.getOutputproto() + "output-proto": CmdOptions.getOutputproto(), + "record_type": CmdOptions.needRecordType() }; let jsonOpt = JSON.stringify(options, null, 2); jsonOpt = "$" + jsonOpt.replace(dollarSign, '#$') + "$"; diff --git a/ts2panda/ts2abc/ts2abc.cpp b/ts2panda/ts2abc/ts2abc.cpp index a478c896ba..1a27f4636b 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -57,7 +57,6 @@ constexpr std::size_t BOUND_RIGHT = 0; constexpr std::size_t LINE_NUMBER = 0; constexpr bool IS_DEFINED = true; int g_opCodeIndex = 0; -std::string g_recordName = ""; std::unordered_map g_opcodeMap = { #define OPLIST(opcode, name, optype, width, flags, def_idx, use_idxs) {g_opCodeIndex++, panda::pandasm::Opcode::opcode}, PANDA_INSTRUCTION_LIST(OPLIST) @@ -750,7 +749,7 @@ static std::string CreateLiteralArrayForType(const Json::Value &types, panda::pa } else { typeTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::LITERALARRAY); typeValueLiteral.tag_ = panda::panda_file::LiteralTag::LITERALARRAY; - std::string litId = g_recordName + "_" + std::to_string(typeIndex); + std::string litId = GetLiteralId(typeIndex - LITERALBUFFERINDEXOFFSET); typeValueLiteral.value_ = litId; } @@ -963,6 +962,14 @@ static void ParseIsDtsFile(const Json::Value &rootValue) } } +static void ParseEnableTypeInfo(const Json::Value &rootValue) +{ + Logd("-----------------parse enable type info-----------------"); + if (rootValue.isMember("record_type") && rootValue["record_type"].isBool()) { + g_enableTypeinfo = rootValue["record_type"].asBool(); + } +} + static void ParseCompilerOutputProto(const Json::Value &rootValue) { Logd("-----------------parse compiler output proto-----------------"); @@ -992,6 +999,7 @@ static void ParseOptions(const Json::Value &rootValue, panda::pandasm::Program & ParseDisplayTypeinfo(rootValue); ParseOptLogLevel(rootValue); ParseIsDtsFile(rootValue); + ParseEnableTypeInfo(rootValue); ParseCompilerOutputProto(rootValue); } @@ -1026,10 +1034,7 @@ static void ParseSingleLiteralBuf(const Json::Value &rootValue, panda::pandasm:: } auto literalarrayInstance = panda::pandasm::LiteralArray(literalArray); - std::string litId = g_recordName + "_" + std::to_string(g_literalArrayCount++); - if (prog.literalarray_table.find(litId) != prog.literalarray_table.end()) { - std::cerr << "----litId is alrerady exist--------" << litId << std::endl; - } + auto litId = literalBuffer["k"].asString(); std::cerr << "---------literal id----------" << litId << std::endl; prog.literalarray_table.emplace(litId, std::move(literalarrayInstance)); } @@ -1174,15 +1179,18 @@ static void ParseSingleTypeInfo(const Json::Value &rootValue, panda::pandasm::Pr typeFlagField.type = panda::pandasm::Type("u8", 0); typeFlagField.metadata->SetValue(panda::pandasm::ScalarValue::Create( static_cast(typeFlag))); + rec.field_list.emplace_back(std::move(typeFlagField)); - auto typeSummaryIndexField = panda::pandasm::Field(LANG_EXT); - typeSummaryIndexField.name = "typeSummaryIndex"; - typeSummaryIndexField.type = panda::pandasm::Type("u32", 0); - typeSummaryIndexField.metadata->SetValue( - panda::pandasm::ScalarValue::Create(typeSummaryIndex)); - rec.field_list.emplace_back(std::move(typeFlagField)); - rec.field_list.emplace_back(std::move(typeSummaryIndexField)); + if (g_enableTypeinfo) { + std::cerr << "--------type summary index---------" << typeSummaryIndex << std::endl; + auto typeSummaryIndexField = panda::pandasm::Field(LANG_EXT); + typeSummaryIndexField.name = "typeSummaryIndex"; + typeSummaryIndexField.type = panda::pandasm::Type("u32", 0); + typeSummaryIndexField.metadata->SetValue( + panda::pandasm::ScalarValue::Create(typeSummaryIndex)); + rec.field_list.emplace_back(std::move(typeSummaryIndexField)); + } } } -- Gitee