diff --git a/es2panda/aot/options.cpp b/es2panda/aot/options.cpp index eb03d55c07e4bd47d52bf59e7e463169dbb2dac9..e905e7850b70ffda03f508c03ab8e4438c846443 100644 --- a/es2panda/aot/options.cpp +++ b/es2panda/aot/options.cpp @@ -38,11 +38,6 @@ T RemoveExtension(T const &filename) return P > 0 && P != T::npos ? filename.substr(0, P) : filename; } -static std::string FormatRecordName(const std::string &recordName) -{ - return recordName + "."; -} - static std::vector GetStringItems(std::string &input, const std::string &delimiter) { std::vector items; @@ -81,7 +76,7 @@ bool Options::CollectInputFilesFromFileList(const std::string &input) } // itemList: [filePath, recordName, moduleKind, sourceFile] std::string fileName = itemList[0]; - std::string recordName = FormatRecordName(itemList[1]); + std::string recordName = itemList[1]; parser::ScriptKind scriptKind; if (itemList[2] == "script") { scriptKind = parser::ScriptKind::SCRIPT; @@ -292,7 +287,6 @@ bool Options::Parse(int argc, const char **argv) recordName_ = compilerOutput_.empty() ? "Base64Output" : RemoveExtension(util::Helpers::BaseName(compilerOutput_)); } - recordName_ = FormatRecordName(recordName_); } if (!inputIsEmpty) { diff --git a/es2panda/binder/binder.cpp b/es2panda/binder/binder.cpp index 1a56f6cdc0bbce6209c0a9fd80bb441c54173c46..0903ded97cfb9bd22d55ea34d77fd56627c7ec35 100644 --- a/es2panda/binder/binder.cpp +++ b/es2panda/binder/binder.cpp @@ -216,13 +216,13 @@ void Binder::BuildFunction(FunctionScope *funcScope, util::StringView name) bool funcNameWithoutDot = (name.Find(".") == std::string::npos); bool funcNameWithoutBackslash = (name.Find("\\") == std::string::npos); if (name != ANONYMOUS_FUNC_NAME && funcNameWithoutDot && funcNameWithoutBackslash && !functionNames_.count(name)) { - auto internalName = std::string(program_->RecordName()) + std::string(name); + auto internalName = std::string(program_->FormatedRecordName()) + std::string(name); functionNames_.insert(name); funcScope->BindName(name, util::UString(internalName, Allocator()).View()); return; } std::stringstream ss; - ss << std::string(program_->RecordName()); + ss << std::string(program_->FormatedRecordName()); uint32_t idx = functionNameIndex_++; ss << "#" << std::to_string(idx) << "#"; if (funcNameWithoutDot && funcNameWithoutBackslash) { diff --git a/es2panda/compiler/core/emitter/emitter.cpp b/es2panda/compiler/core/emitter/emitter.cpp index 8d1b08456de1cd2dc067a6d97d5bbd245745f17c..50487465e69c49d973e2d3b43af3da063fd05312 100644 --- a/es2panda/compiler/core/emitter/emitter.cpp +++ b/es2panda/compiler/core/emitter/emitter.cpp @@ -355,7 +355,7 @@ Emitter::Emitter(const CompilerContext *context) prog_->lang = LANG_EXT; if (context->IsMergeAbc()) { - auto recordName = context->Binder()->Program()->RecordName().Mutf8(); + auto recordName = context->Binder()->Program()->FormatedRecordName().Mutf8(); rec_ = new panda::pandasm::Record(recordName.substr(0, recordName.find_last_of('.')), LANG_EXT); SetCommonjsField(context->Binder()->Program()->Kind() == parser::ScriptKind::COMMONJS); } else { @@ -381,8 +381,8 @@ void Emitter::AddFunction(FunctionEmitter *func, CompilerContext *context) for (auto &[idx, buf] : func->LiteralBuffers()) { auto literalArrayInstance = panda::pandasm::LiteralArray(std::move(buf)); - prog_->literalarray_table.emplace(std::string(context->RecordName()) + 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(); @@ -393,12 +393,15 @@ void Emitter::AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerCon { std::lock_guard lock(m_); + 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()) { @@ -411,8 +414,9 @@ 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()) { @@ -422,7 +426,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/es2panda/compiler/core/pandagen.cpp b/es2panda/compiler/core/pandagen.cpp index 7dcf427b52304bdb7e32952dcba69efa2a79f074..8c040db141dd04b1349ded7785293ee91f393784 100644 --- a/es2panda/compiler/core/pandagen.cpp +++ b/es2panda/compiler/core/pandagen.cpp @@ -1408,7 +1408,7 @@ void PandaGen::CreateEmptyObject(const ir::AstNode *node) void PandaGen::CreateObjectWithBuffer(const ir::AstNode *node, uint32_t idx) { ASSERT(util::Helpers::IsInteger(idx)); - std::string idxStr = std::string(context_->RecordName()) + std::to_string(idx); + std::string idxStr = std::string(context_->Binder()->Program()->RecordName()) + "_" + std::to_string(idx); util::UString litId(idxStr, allocator_); sa_.Emit(node, 0, litId.View()); } @@ -1439,7 +1439,7 @@ void PandaGen::CreateEmptyArray(const ir::AstNode *node) void PandaGen::CreateArrayWithBuffer(const ir::AstNode *node, uint32_t idx) { ASSERT(util::Helpers::IsInteger(idx)); - std::string idxStr = std::string(context_->RecordName()) + std::to_string(idx); + std::string idxStr = std::string(context_->Binder()->Program()->RecordName()) + "_" + std::to_string(idx); util::UString litId(idxStr, allocator_); sa_.Emit(node, 0, litId.View()); } @@ -1601,7 +1601,7 @@ void PandaGen::CloseIterator(const ir::AstNode *node, VReg iter) void PandaGen::DefineClassWithBuffer(const ir::AstNode *node, const util::StringView &ctorId, int32_t litIdx, VReg base) { auto formalParamCnt = node->AsClassDefinition()->Ctor()->Function()->FormalParamsLength(); - std::string idxStr = std::string(context_->RecordName()) + std::to_string(litIdx); + std::string idxStr = std::string(context_->Binder()->Program()->RecordName()) + "_" + std::to_string(litIdx); util::UString litId(idxStr, allocator_); ra_.Emit(node, 0, ctorId, litId.View(), static_cast(formalParamCnt), base); strings_.insert(ctorId); @@ -1824,7 +1824,7 @@ void PandaGen::NewLexEnv(const ir::AstNode *node, uint32_t num) void PandaGen::NewLexEnvWithScopeInfo(const ir::AstNode *node, uint32_t num, int32_t scopeInfoIdx) { - std::string idxStr = std::string(context_->RecordName()) + std::to_string(scopeInfoIdx); + std::string idxStr = std::string(context_->Binder()->Program()->RecordName()) + "_" + std::to_string(scopeInfoIdx); util::UString litId(idxStr, allocator_); num <= util::Helpers::MAX_INT8 ? sa_.Emit(node, num, litId.View()) : sa_.Emit(node, num, litId.View()); diff --git a/es2panda/parser/program/program.cpp b/es2panda/parser/program/program.cpp index c184046e3a2864f07346513b04acf5938ad422c3..8cd8d97df3c319a1a7a1179d3471dd21a7c0e641 100644 --- a/es2panda/parser/program/program.cpp +++ b/es2panda/parser/program/program.cpp @@ -36,6 +36,7 @@ Program::Program(Program &&other) sourceCode_(other.sourceCode_), sourceFile_(other.sourceFile_), recordName_(other.recordName_), + formatedRecordName_(other.formatedRecordName_), kind_(other.kind_), extension_(other.extension_), lineIndex_(other.lineIndex_), diff --git a/es2panda/parser/program/program.h b/es2panda/parser/program/program.h index 15cf51474dd0757547aa5da64df1b6964a02f1b7..650ce1978f21870f577caa32a081a93c0b03000c 100644 --- a/es2panda/parser/program/program.h +++ b/es2panda/parser/program/program.h @@ -90,6 +90,11 @@ public: return recordName_.View(); } + util::StringView FormatedRecordName() const + { + return formatedRecordName_.View(); + } + const lexer::LineIndex &GetLineIndex() const { return lineIndex_; @@ -120,6 +125,8 @@ public: void SetRecordName(const std::string &recordName) { recordName_ = util::UString(recordName, Allocator()); + std::string formatedRecordName = recordName + "."; + formatedRecordName_ = util::UString(formatedRecordName, Allocator()); } void AddHotfixHelper(util::Hotfix *hotfixHelper) @@ -142,6 +149,7 @@ private: util::UString sourceCode_ {}; util::UString sourceFile_ {}; util::UString recordName_ {}; + util::UString formatedRecordName_ {}; ScriptKind kind_ {}; ScriptExtension extension_ {}; lexer::LineIndex lineIndex_ {}; diff --git a/merge_abc/src/annotationProto.cpp b/merge_abc/src/annotationProto.cpp index 85d13681f20707e70d5fee92c42453318d12329c..cc8d592519c36cf9465159cd8e19c3cedb0b8c47 100644 --- a/merge_abc/src/annotationProto.cpp +++ b/merge_abc/src/annotationProto.cpp @@ -107,6 +107,7 @@ void ScalarValue::Serialize(const panda::pandasm::ScalarValue &scalar, protoPand case panda::pandasm::Value::Type::STRING: case panda::pandasm::Value::Type::METHOD: case panda::pandasm::Value::Type::ENUM: + case panda::pandasm::Value::Type::LITERALARRAY: type = protoPanda::ScalarValue_VariantValueType::ScalarValue_VariantValueType_STRING; protoScalar.set_valuestr(scalar.GetValue()); break; @@ -123,6 +124,7 @@ void ScalarValue::Serialize(const panda::pandasm::ScalarValue &scalar, protoPand break; } default: + std::cerr << "unknown panda::pandasm::Value::Type" << std::endl; UNREACHABLE(); } protoScalar.set_type(type); @@ -237,6 +239,10 @@ panda::pandasm::ScalarValue ScalarValue::CreateScalarValue(const panda::pandasm: return panda::pandasm::ScalarValue::Create( std::get(value)); } + case panda::pandasm::Value::Type::LITERALARRAY: { + return panda::pandasm::ScalarValue::Create( + std::get(value)); + } default: UNREACHABLE(); } diff --git a/ts2panda/src/base/bcGenUtil.ts b/ts2panda/src/base/bcGenUtil.ts index 6123d2cd8f3e0550c1597db241ceaf7ede3e75fa..4be0a9afa6cf2f3c9fdab7ce29c460aad7ad202a 100644 --- a/ts2panda/src/base/bcGenUtil.ts +++ b/ts2panda/src/base/bcGenUtil.ts @@ -346,9 +346,8 @@ export function createEmptyObject() { return new Createemptyobject(); } -export function createObjectWithBuffer(idx: number) { - let litId: string = idx.toString(); - return new Createobjectwithbuffer(new Imm(0), litId); +export function createObjectWithBuffer(bufferId: string) { + return new Createobjectwithbuffer(new Imm(0), bufferId); } export function setObjectWithProto(proto: VReg) { @@ -367,17 +366,15 @@ export function createEmptyArray() { return new Createemptyarray(new Imm(0)); } -export function createArrayWithBuffer(idx: number) { - let litId: string = idx.toString(); - return new Createarraywithbuffer(new Imm(0), litId); +export function createArrayWithBuffer(bufferId: string) { + return new Createarraywithbuffer(new Imm(0), bufferId); } export function storeArraySpread(array: VReg, index: VReg) { return new Starrayspread(array, index); } -export function defineClassWithBuffer(id: string, idx: number, parameterLength: number, base: VReg) { - let litId: string = idx.toString(); +export function defineClassWithBuffer(id: string, litId: string, parameterLength: number, base: VReg) { return new Defineclasswithbuffer(new Imm(0), id, litId, new Imm(parameterLength), base); } diff --git a/ts2panda/src/base/literal.ts b/ts2panda/src/base/literal.ts index 50d7c9d52c9f9d91839eef7a44b90885d4af03f0..2cfb24623694f2d639513a3d2b40c1ad4ee24fe8 100644 --- a/ts2panda/src/base/literal.ts +++ b/ts2panda/src/base/literal.ts @@ -14,59 +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, - 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]; - } -} \ No newline at end of file +} diff --git a/ts2panda/src/base/typeSystem.ts b/ts2panda/src/base/typeSystem.ts index 43bdf6e64f06046a31eae1783f476faf7d2c5820..898b7f2665c4addc6462fa1fab93271c766e8b09 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 "./util"; +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,16 @@ 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.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)); @@ -422,10 +438,10 @@ 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)); + this.transferType2Literal(this.extendsHeritage, classTypeLiterals); classTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.implementsHeritages.length)); - this.implementsHeritages.forEach(heritage => { - classTypeLiterals.push(new Literal(LiteralTag.INTEGER, heritage)); + this.implementsHeritages.forEach(heritage => { // heritage types + this.transferType2Literal(heritage, classTypeLiterals); }); // record unstatic fields and methods @@ -446,7 +462,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 + 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 +474,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)); + this.transferType2Literal(typeInfo, classTypeLiterals); }); } } @@ -480,7 +496,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)); + this.transferType2Literal(this.shiftedReferredClassIndex, classInstLiterals); classInstBuf.addLiterals(...classInstLiterals); return classInstBuf; @@ -585,20 +601,20 @@ 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.INTEGER, 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.INTEGER, this.parameters[i])); + this.transferType2Literal(this.parameters[i], funcTypeLiterals); } } - funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.returnType)); + this.transferType2Literal(this.returnType, funcTypeLiterals); funcTypeBuf.addLiterals(...funcTypeLiterals); return funcTypeBuf; } @@ -675,7 +691,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)); + this.transferType2Literal(type, UnionTypeLiterals); } UnionTypeBuf.addLiterals(...UnionTypeLiterals); return UnionTypeBuf; @@ -720,7 +736,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)); + this.transferType2Literal(this.referedTypeIndex, arrayLiterals); arrayBuf.addLiterals(...arrayLiterals); return arrayBuf; } @@ -755,7 +771,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)); + this.transferType2Literal(typeIndex, objLiterals); }); objTypeBuf.addLiterals(...objLiterals); return objTypeBuf; @@ -868,7 +884,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)); + this.transferType2Literal(heritage, interfaceTypeLiterals) }); // record fields and methods @@ -885,7 +901,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 + 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 +912,7 @@ export class InterfaceType extends BaseType { interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, transferredTarget.length)); transferredTarget.forEach(method => { - interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, method)); + this.transferType2Literal(method, interfaceTypeLiterals); }); } } @@ -925,10 +941,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)); + this.transferType2Literal(this.builtinTypeIndex, UnionTypeLiterals); UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.containerArray.length)); for (let type of this.containerArray) { - UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, type)); + this.transferType2Literal(type, UnionTypeLiterals); } UnionTypeBuf.addLiterals(...UnionTypeLiterals); return UnionTypeBuf; diff --git a/ts2panda/src/base/util.ts b/ts2panda/src/base/util.ts index 0e0fcf46aab967f05e80c1bcf4c1e18058c5b8d3..9b60cb6d457f9cc9de76c546581f8c1dde383345 100644 --- a/ts2panda/src/base/util.ts +++ b/ts2panda/src/base/util.ts @@ -390,4 +390,12 @@ export function hasAbstractModifier(node: ts.Node): boolean { } export const MAX_INT8 = 127; -export const MAX_INT16 = 32767; \ No newline at end of file +export const MAX_INT16 = 32767; + +export function getRecordName(node: ts.SourceFile): string { + return ""; // need to be fixed later +} + +export function getLiteralKey(node: ts.SourceFile, idx:number): string { + return `${getRecordName(node)}_${idx}`; +} \ No newline at end of file diff --git a/ts2panda/src/compilerDriver.ts b/ts2panda/src/compilerDriver.ts index 99ab04dc64d9de2fe217fc5a043654afa6fac64f..b4c4413c6bc67650755347ae5a4442f53b42923f 100644 --- a/ts2panda/src/compilerDriver.ts +++ b/ts2panda/src/compilerDriver.ts @@ -49,6 +49,7 @@ import { TypeRecorder } from "./typeRecorder"; import { LiteralBuffer } from "./base/literal"; import { findOuterNodeOfParenthesis } from "./expression/parenthesizedExpression"; import { IRNode } from "./irnodes"; +import { getRecordName } from "./base/util"; export class PendingCompilationUnit { constructor( @@ -63,6 +64,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 passes: Pass[] = []; @@ -183,6 +185,7 @@ export class CompilerDriver { try { 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/expression/arrayLiteralExpression.ts b/ts2panda/src/expression/arrayLiteralExpression.ts index 1d16860880ccd9101984bc037477ab55d1e0d2f8..3ce4a7df271c73ecd05f8bce0510b1a296c981ef 100644 --- a/ts2panda/src/expression/arrayLiteralExpression.ts +++ b/ts2panda/src/expression/arrayLiteralExpression.ts @@ -162,10 +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); - pandaGen.createArrayWithBuffer(element, bufferIdx); + let bufferId = PandaGen.appendLiteralArrayBuffer(literalBuffer); + pandaGen.createArrayWithBuffer(element, bufferId); } } diff --git a/ts2panda/src/expression/objectLiteralExpression.ts b/ts2panda/src/expression/objectLiteralExpression.ts index aec9baf441e34f9c7286cd5684ad1d3f6be5b408..1d6af9384c4f132e56561f2e59e14493848057c9 100644 --- a/ts2panda/src/expression/objectLiteralExpression.ts +++ b/ts2panda/src/expression/objectLiteralExpression.ts @@ -112,10 +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); - pandaGen.createObjectWithBuffer(expr, bufferIdx); + let bufferId = PandaGen.appendLiteralArrayBuffer(literalBuffer); + pandaGen.createObjectWithBuffer(expr, bufferId); } pandaGen.storeAccumulator(expr, objReg); } diff --git a/ts2panda/src/index.ts b/ts2panda/src/index.ts index e243c78b0d2e4615206e0435985e8a5be7ff6307..b4e7c8aad739596c17b5764dd9ff96e020a53c2e 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); + CompilerDriver.srcNode = node; setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(node, options)); compilerDriver.compile(node); compilerDriver.showStatistics(); diff --git a/ts2panda/src/pandagen.ts b/ts2panda/src/pandagen.ts index ce824c54a7d26e13be6159910e9a9fa855f33a15..2ffc0c0c2acd551e2c67de4834d981a00edf4aa9 100644 --- a/ts2panda/src/pandagen.ts +++ b/ts2panda/src/pandagen.ts @@ -188,6 +188,8 @@ import { CatchTable } from "./statement/tryStatement"; import { TypeRecorder } from "./typeRecorder"; import { Variable } from "./variable"; import * as jshelpers from "./jshelpers"; +import { CompilerDriver } from "./compilerDriver"; +import { getLiteralKey } from "./base/util"; export enum FunctionKind { NONE = 0, // represent method for now @@ -368,14 +370,25 @@ export class PandaGen { } } + static appendLiteralArrayBuffer(litBuf: LiteralBuffer): string { + let litId = getLiteralKey(CompilerDriver.srcNode, PandaGen.literalArrayBuffer.length); + litBuf.setKey(litId); + PandaGen.literalArrayBuffer.push(litBuf); + return litId; + } + 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() { @@ -1186,8 +1199,8 @@ export class PandaGen { this.add(node, createEmptyObject()); } - createObjectWithBuffer(node: ts.Node, idx: number) { - this.add(node, createObjectWithBuffer(idx)); + createObjectWithBuffer(node: ts.Node, bufferId: string) { + this.add(node, createObjectWithBuffer(bufferId)); } setObjectWithProto(node: ts.Node, proto: VReg, object: VReg) { @@ -1214,8 +1227,8 @@ export class PandaGen { this.add(node, createEmptyArray()); } - createArrayWithBuffer(node: ts.Node, idx: number) { - this.add(node, createArrayWithBuffer(idx)); + createArrayWithBuffer(node: ts.Node, bufferId: string) { + this.add(node, createArrayWithBuffer(bufferId)); } storeArraySpreadElement(node: ts.Node, array: VReg, index: VReg) { @@ -1257,10 +1270,10 @@ export class PandaGen { this.add(node, dynamicImport()); } - defineClassWithBuffer(node: ts.Node, name: string, idx: number, parameterLength: number, base: VReg) { + defineClassWithBuffer(node: ts.Node, name: string, litId: string, parameterLength: number, base: VReg) { this.add( node, - defineClassWithBuffer(name, idx, parameterLength, base) + defineClassWithBuffer(name, litId, parameterLength, base) ) } diff --git a/ts2panda/src/statement/classStatement.ts b/ts2panda/src/statement/classStatement.ts index 19c2c6f7fc92823a2dae421894e9338e1545ac0e..56a29cbcaef00674bcdbcdc86dea44e9092a2f73 100644 --- a/ts2panda/src/statement/classStatement.ts +++ b/ts2panda/src/statement/classStatement.ts @@ -252,16 +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); + let litId: string = 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; - pandaGen.defineClassWithBuffer(stmt, internalName, buffIdx, parameterLength, vregs[0]); + pandaGen.defineClassWithBuffer(stmt, internalName, litId, parameterLength, vregs[0]); pandaGen.storeAccumulator(stmt, vregs[1]); } diff --git a/ts2panda/src/ts2panda.ts b/ts2panda/src/ts2panda.ts index 3d14a0d221bdf923e409d65defc904954088a2b9..d1d8b4af4977e4c0fc6c2f99d284f6c3fba065b4 100644 --- a/ts2panda/src/ts2panda.ts +++ b/ts2panda/src/ts2panda.ts @@ -54,6 +54,7 @@ import { ModuleScope } from "./scope"; import { TypeRecorder } from "./typeRecorder"; import { isGlobalDeclare } from "./strictMode"; import { isFunctionLikeDeclaration } from "./syntaxCheckHelper"; +import { getLiteralKey } from "./base/util"; const dollarSign: RegExp = /\$/g; @@ -64,7 +65,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(); @@ -209,7 +211,22 @@ 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 enableRecordType: boolean = CmdOptions.needRecordType() && CompilerDriver.isTsFile; let options = { "t": JsonType.options, "module_mode": CmdOptions.isModules(), @@ -219,7 +236,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(), + "record_type": enableRecordType }; let jsonOpt = JSON.stringify(options, null, 2); jsonOpt = "$" + jsonOpt.replace(dollarSign, '#$') + "$"; @@ -442,9 +460,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/tests/esmodule.test.ts b/ts2panda/tests/esmodule.test.ts index d8dc4b5524b0fc77e197191ab2cfcf032136b1ce..ff7e32c2442d3556047e05087f5ba06af9200b6b 100644 --- a/ts2panda/tests/esmodule.test.ts +++ b/ts2panda/tests/esmodule.test.ts @@ -49,7 +49,7 @@ describe("ExportDeclaration", function () { let classReg = new VReg(); let expected = [ new Mov(new VReg(), new VReg()), - new Defineclasswithbuffer(new Imm(0), "#1#C", "0", new Imm(0), new VReg()), + new Defineclasswithbuffer(new Imm(0), "#1#C", "_0", new Imm(0), new VReg()), new Sta(classReg), new Lda(classReg), new Stmodulevar(new Imm(0)), diff --git a/ts2panda/tests/expression/call.test.ts b/ts2panda/tests/expression/call.test.ts index 08d1281c78634b270bcc526fb985037b462cdd6c..a2b6f7bb1d5abc63521941a548025c94af6bf9fe 100644 --- a/ts2panda/tests/expression/call.test.ts +++ b/ts2panda/tests/expression/call.test.ts @@ -21,7 +21,6 @@ import { Dynamicimport, Callarg0, Callarg1, - Callthisrange, Apply, Createarraywithbuffer, Createemptyarray, @@ -114,7 +113,7 @@ describe("CallTest", function () { let arrayInstance = new VReg(); let expected = [ - new Createarraywithbuffer(new Imm(0), "0"), + new Createarraywithbuffer(new Imm(0), "_0"), new Sta(arrayInstance), new Lda(arrayInstance), new Stconsttoglobalrecord(new Imm(1), 'args'), diff --git a/ts2panda/tests/expression/commalist.test.ts b/ts2panda/tests/expression/commalist.test.ts index ccd5f5582535d178a7d3ae9b098ad02723deace6..41d44eb25bcc47960bcc8cb922c2c8c9054f70c8 100644 --- a/ts2panda/tests/expression/commalist.test.ts +++ b/ts2panda/tests/expression/commalist.test.ts @@ -70,10 +70,9 @@ describe("CommaListExpression", function () { } \ "), 0, undefined); let insns = snippetCompiler.getGlobalInsns(); - console.log(insns); let expected = [ new Mov(new VReg(), new VReg()), - new Defineclasswithbuffer(new Imm(0), "#1#Test", "0", new Imm(0), new VReg()), + new Defineclasswithbuffer(new Imm(0), "#1#Test", "_0", new Imm(0), new VReg()), new Sta(new VReg()), new Lda(new VReg()), new Sttoglobalrecord(new Imm(1), "Test"), diff --git a/ts2panda/tests/expression/literal.test.ts b/ts2panda/tests/expression/literal.test.ts index 918ab2c387935066227e1528037b77da05488cbc..832ba0b657aeb33463c9ff3b9f01686b48f830ee 100644 --- a/ts2panda/tests/expression/literal.test.ts +++ b/ts2panda/tests/expression/literal.test.ts @@ -96,7 +96,7 @@ describe("LiteralTest", function () { let arrayInstance = new VReg(); let expected = [ - new Createarraywithbuffer(new Imm(0), "0"), + new Createarraywithbuffer(new Imm(0), "_0"), new Sta(arrayInstance), new Lda(arrayInstance), new Sttoglobalrecord(new Imm(1), 'arr'), @@ -125,7 +125,7 @@ describe("LiteralTest", function () { let arrayInstance = new VReg(); let expected = [ - new Createarraywithbuffer(new Imm(0), "0"), + new Createarraywithbuffer(new Imm(0), "_0"), new Sta(arrayInstance), new Lda(arrayInstance), new Sttoglobalrecord(new Imm(1), 'arr'), @@ -159,7 +159,7 @@ describe("LiteralTest", function () { let arrayInstance = new VReg(); let expected = [ - new Createarraywithbuffer(new Imm(0), "0"), + new Createarraywithbuffer(new Imm(0), "_0"), new Sta(arrayInstance), new Ldai(new Imm(3)), new Stownbyindex(new Imm(1), arrayInstance, new Imm(2)), @@ -180,12 +180,12 @@ describe("LiteralTest", function () { let arrayInstance = new VReg(); let expected = [ - new Createarraywithbuffer(new Imm(0), "0"), + new Createarraywithbuffer(new Imm(0), "_0"), new Sta(arrayInstance), new Lda(arrayInstance), new Sttoglobalrecord(new Imm(1), 'arr1'), - new Createarraywithbuffer(new Imm(2), "1"), + new Createarraywithbuffer(new Imm(2), "_1"), new Sta(arrayInstance), new Ldai(new Imm(1)), new Sta(elemIdxReg), @@ -224,7 +224,7 @@ describe("LiteralTest", function () { IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined); let objInstance = new VReg(); let expected = [ - new Createobjectwithbuffer(new Imm(0), "0"), + new Createobjectwithbuffer(new Imm(0), "_0"), new Sta(objInstance), new Lda(objInstance), new Sttoglobalrecord(new Imm(1), 'obj'), @@ -240,7 +240,7 @@ describe("LiteralTest", function () { let lhs = new VReg(); let expected = [ - new Createobjectwithbuffer(new Imm(0), "0"), + new Createobjectwithbuffer(new Imm(0), "_0"), new Sta(objInstance), new Ldai(new Imm(1)), new Sta(lhs), @@ -260,7 +260,7 @@ describe("LiteralTest", function () { let objInstance = new VReg(); let expected = [ - new Createobjectwithbuffer(new Imm(0), "0"), + new Createobjectwithbuffer(new Imm(0), "_0"), new Sta(objInstance), new Lda(objInstance), new Sttoglobalrecord(new Imm(1), 'obj'), @@ -276,7 +276,7 @@ describe("LiteralTest", function () { let objInstance = new VReg(); let expected = [ - new Createobjectwithbuffer(new Imm(0), "0"), + new Createobjectwithbuffer(new Imm(0), "_0"), new Sta(objInstance), new Tryldglobalbyname(new Imm(1), 'a'), new Stownbyname(new Imm(2), "a", objInstance), diff --git a/ts2panda/tests/expression/propertyAccess.test.ts b/ts2panda/tests/expression/propertyAccess.test.ts index 3fbf1ada004ef75a330e6f9aca28bbf4d29be0cf..2c57fcc0cdcda5365aa39ba88c636debdbc946aa 100644 --- a/ts2panda/tests/expression/propertyAccess.test.ts +++ b/ts2panda/tests/expression/propertyAccess.test.ts @@ -98,7 +98,7 @@ describe("PropertyAccess", function () { let propReg = new VReg(); let expected = [ - new Createobjectwithbuffer(new Imm(0), "0"), + new Createobjectwithbuffer(new Imm(0), "_0"), new Sta(objInstance), new Lda(new VReg()), new Definemethod(new Imm(1), "myMethod", new Imm(1)), @@ -140,7 +140,7 @@ describe("PropertyAccess", function () { let propReg = new VReg(); let expected = [ - new Createobjectwithbuffer(new Imm(0), "0"), + new Createobjectwithbuffer(new Imm(0), "_0"), new Sta(objInstance), new Lda(new VReg()), new Definemethod(new Imm(1), "a", new Imm(0)), @@ -178,7 +178,7 @@ describe("PropertyAccess", function () { let propReg = new VReg(); let expected = [ - new Createobjectwithbuffer(new Imm(0), "0"), + new Createobjectwithbuffer(new Imm(0), "_0"), new Sta(objInstance), new Lda(new VReg()), new Definemethod(new Imm(1), "#1#a", new Imm(0)), diff --git a/ts2panda/tests/types/array.test.ts b/ts2panda/tests/types/array.test.ts index edaf90fdfa928ace5bac50f93c05acbf4c9932ed..6070aa1dfe66a66fe7dab541b7dd5feaa091ec8e 100644 --- a/ts2panda/tests/types/array.test.ts +++ b/ts2panda/tests/types/array.test.ts @@ -47,7 +47,7 @@ describe("array tests in array.test.ts", function() { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 6], [2, 0] + [2, 6], [24, "_1"], [24, "_2"], [24, "_3"], [24, "_4"], [24, "_5"], [24, "_6"], [2, 0] ], [ [2, 5], [2, 1] @@ -88,7 +88,7 @@ describe("array tests in array.test.ts", function() { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 3], [2, 0] + [2, 3], [24, "_1"], [24, "_2"], [24, "_3"], [2, 0] ], [ [2, 1], [2, 0], [2, 0], [2, 0], @@ -131,7 +131,7 @@ describe("array tests in array.test.ts", function() { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 6], [2, 0] + [2, 6], [24, "_1"], [24, "_2"], [24, "_3"], [24, "_4"], [24, "_5"], [24, "_6"], [2, 0] ], [ [2, 5], [2, 1] @@ -173,7 +173,7 @@ describe("array tests in array.test.ts", function() { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 3], [2, 0] + [2, 3], [24, "_1"], [24, "_2"], [24, "_3"], [2, 0] ], [ [2, 1], [2, 0], [2, 0], [2, 0], @@ -206,7 +206,7 @@ describe("array tests in array.test.ts", function() { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 2], [2, 0] + [2, 2], [24, "_1"], [24, "_2"], [2, 0] ], [ [2, 4], [2, 2], [2, 4], [2, 1], @@ -234,7 +234,7 @@ describe("array tests in array.test.ts", function() { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 2], [2, 0] + [2, 2], [24, "_1"], [24, "_2"], [2, 0] ], [ [2, 6], [2, 2], [5, "element1"], [2, 1], diff --git a/ts2panda/tests/types/class.test.ts b/ts2panda/tests/types/class.test.ts index 6f076b68e6efdfa6bc276e402851135cb06acbfc..5ff4d6b3dfdd96270a1f902140eee74272c38927 100644 --- a/ts2panda/tests/types/class.test.ts +++ b/ts2panda/tests/types/class.test.ts @@ -43,7 +43,7 @@ describe("class tests in class.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 3], [2, 0] + [2, 3], [24, "_1"], [24, "_2"], [24, "_3"], [2, 0] ], [ [2, 1], [2, 0], [2, 0], @@ -79,7 +79,7 @@ describe("class tests in class.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 3], [2, 0] + [2, 3], [24, "_1"], [24, "_2"], [24, "_3"], [2, 0] ], [ [2, 1], [2, 0], [2, 0], [2, 0], @@ -115,7 +115,7 @@ describe("class tests in class.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 2], [2, 0] + [2, 2], [24, "_1"], [24, "_2"], [2, 0] ], [ [2, 1], [2, 0], [2, 0], [2, 0], @@ -149,7 +149,7 @@ describe("class tests in class.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 4], [2, 0] + [2, 4], [24, "_1"], [24, "_2"], [24, "_3"], [24, "_4"], [2, 0] ], [ [2, 1], [2, 0], [2, 0], [2, 0], @@ -189,7 +189,7 @@ describe("class tests in class.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 2], [2, 0] + [2, 2], [24, "_1"], [24, "_2"], [2, 0] ], [ [2, 1], [2, 0], [2, 0], [2, 0], @@ -224,7 +224,7 @@ describe("class tests in class.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 4], [2, 0] + [2, 4], [24, "_1"], [24, "_2"], [24, "_3"], [24, "_4"], [2, 0] ], [ [2, 1], [2, 0], [2, 0], [2, 0], @@ -266,7 +266,7 @@ describe("class tests in class.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 5], [2, 0] + [2, 5], [24, "_1"], [24, "_2"], [24, "_3"], [24, "_4"], [24, "_5"], [2, 0] ], [ [2, 1], [2, 1], [2, 0], [2, 0], @@ -312,7 +312,7 @@ describe("class tests in class.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 4], [2, 0] + [2, 4], [24, "_1"], [24, "_2"], [24, "_3"], [24, "_4"], [2, 0] ], [ [2, 1], [2, 0], [2, 0], [2, 0], diff --git a/ts2panda/tests/types/function.test.ts b/ts2panda/tests/types/function.test.ts index 563e2b4c0061209aea968ebd536cca51cda5655d..637b897f40ec4b91629111a928ad6db1835753fb 100644 --- a/ts2panda/tests/types/function.test.ts +++ b/ts2panda/tests/types/function.test.ts @@ -42,7 +42,7 @@ describe("function tests in function.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 1], [2, 0] + [2, 2], [24, "_1"], [24, "_2"], [2, 0] ], [ [2, 3], [2, 0], [2, 0], [5, 'local'], @@ -74,7 +74,7 @@ describe("function tests in function.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 2], [2, 0] + [2, 2], [24, "_1"], [24, "_2"], [2, 0] ], [ [2, 3], [2, 0], [2, 0], [5, 'local'], @@ -109,7 +109,7 @@ describe("function tests in function.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 3], [2, 0] + [2, 3], [24, "_1"], [24, "_2"], [24, "_3"], [2, 0] ], [ [2, 3],[2, 0],[2, 0],[5, 'twoFunctions'], @@ -146,7 +146,7 @@ describe("function tests in function.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 4], [2, 0] + [2, 4], [24, "_1"], [24, "_2"], [24, "_3"], [24, "_4"], [2, 0] ], [ [2, 3], [2, 0], [2, 0], [5, 'localClass'], @@ -184,7 +184,7 @@ describe("function tests in function.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 4], [2, 0] + [2, 4], [24, "_1"], [24, "_2"], [24, "_3"], [24, "_4"], [2, 0] ], [ [2, 3], [2, 0], [5, 'localClassRet'], diff --git a/ts2panda/tests/types/object.test.ts b/ts2panda/tests/types/object.test.ts index eae94789e7537a7db90d785a20d536c614dada98..fd4bc3338de81a6272573db9d227a29a7c80b9a0 100644 --- a/ts2panda/tests/types/object.test.ts +++ b/ts2panda/tests/types/object.test.ts @@ -42,7 +42,7 @@ describe("object tests in object.test.ts", function() { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 1], [2, 0] + [2, 1], [24, "_1"], [2, 0] ], [ [2, 6], [2, 2], [5, 'a'], [2, 1], @@ -69,7 +69,7 @@ describe("object tests in object.test.ts", function() { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 4], [2, 0] + [2, 4], [24, "_1"], [24, "_2"], [24, "_3"], [24, "_4"], [2, 0] ], [ [2, 1],[2, 0],[2, 0],[2, 0], diff --git a/ts2panda/tests/types/primitives.test.ts b/ts2panda/tests/types/primitives.test.ts index d05b7c1ba2b25665ec77173c11029d61673b0af0..96df6c0684d308e56784a69169c2f88975345ff5 100644 --- a/ts2panda/tests/types/primitives.test.ts +++ b/ts2panda/tests/types/primitives.test.ts @@ -48,7 +48,6 @@ describe("primitives tests in primitives.test.ts", function() { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 0], [2, 0] ] @@ -119,7 +118,6 @@ describe("primitives tests in primitives.test.ts", function() { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 0], [2, 0] ] @@ -149,7 +147,6 @@ describe("primitives tests in primitives.test.ts", function() { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 0], [2, 0] ] @@ -174,7 +171,7 @@ describe("primitives tests in primitives.test.ts", function() { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 2], [2, 0] + [2, 2], [24, "_1"], [24, "_2"], [2, 0] ], [ [2, 1], [2, 0], [2, 0], [2, 0], @@ -216,7 +213,7 @@ describe("primitives tests in primitives.test.ts", function() { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 0], [2, 0] + [2, 0], [2, 0] ] ] let buff = createLiteralBufferArray(expectedBuffValues); @@ -244,7 +241,7 @@ describe("primitives tests in primitives.test.ts", function() { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 0], [2, 0] + [2, 0], [2, 0] ] ] let buff = createLiteralBufferArray(expectedBuffValues); diff --git a/ts2panda/tests/types/typeUtils.ts b/ts2panda/tests/types/typeUtils.ts index 35600310006013d7a73cf357e6ea8e73616058dc..e0fca9cc5cf209e70b66df026e7b4c6f5566fc36 100644 --- a/ts2panda/tests/types/typeUtils.ts +++ b/ts2panda/tests/types/typeUtils.ts @@ -94,12 +94,14 @@ export function compareVReg2Type(expectedMap: Map, generated: VR export function createLiteralBufferArray(input: any) { let literalBufferArray: Array = new Array(); - for (let buff of input) { + for (let i = 0; i < input.length; i++) { + let buff = input[i]; let literalBuffer: LiteralBuffer = new LiteralBuffer(); for (let rol of buff) { let literal = new Literal(rol[0], rol[1]); literalBuffer.addLiterals(literal); } + literalBuffer.setKey(`_${i}`); literalBufferArray.push(literalBuffer); } return literalBufferArray; diff --git a/ts2panda/tests/types/union.test.ts b/ts2panda/tests/types/union.test.ts index a1dc513747e159af529ef78b482d171433a9eefd..c60234ccd4dd490b1a1010732af094a5447afe2a 100644 --- a/ts2panda/tests/types/union.test.ts +++ b/ts2panda/tests/types/union.test.ts @@ -45,7 +45,7 @@ describe("union tests in union.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 4], [2, 0] + [2, 4], [24, "_1"], [24, "_2"], [24, "_3"], [24, "_4"], [2, 0] ], [ [2, 4], [2, 2], [2, 1], [2, 2] @@ -81,7 +81,7 @@ describe("union tests in union.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 4], [2, 0] + [2, 4], [24, "_1"], [24, "_2"], [24, "_3"], [24, "_4"], [2, 0] ], [ [2, 1], [2, 0], [2, 0], [2, 0], @@ -117,7 +117,7 @@ describe("union tests in union.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 1], [2, 0] + [2, 1], [24, "_1"], [2, 0] ], [ [2, 4], [2, 2], [2, 1], [2, 2] @@ -144,7 +144,7 @@ describe("union tests in union.test.ts", function () { // check liberalBuffer let expectedBuffValues = [ [ - [2, 0], [2, 4], [2, 0] + [2, 4], [24, "_1"], [24, "_2"], [24, "_3"], [24, "_4"], [2, 0] ], [ [2, 1], [2, 0], [2, 0], [2, 0], diff --git a/ts2panda/tests/watch_expression/addWatch.test.ts b/ts2panda/tests/watch_expression/addWatch.test.ts index e56643c18fec704c03bfc65a3d4c56cc161aabce..7d00f48a7065d3fe23072c05b09732d5e2eaf564 100644 --- a/ts2panda/tests/watch_expression/addWatch.test.ts +++ b/ts2panda/tests/watch_expression/addWatch.test.ts @@ -370,7 +370,7 @@ describe("WatchExpressions", function () { IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined); let expected = [ - new Createarraywithbuffer(new Imm(0), "1"), + new Createarraywithbuffer(new Imm(0), "_1"), new Sta(new VReg()), new Lda(new VReg()), @@ -388,7 +388,7 @@ describe("WatchExpressions", function () { IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined); let expected = [ - new Createobjectwithbuffer(new Imm(0), "1"), + new Createobjectwithbuffer(new Imm(0), "_1"), new Sta(new VReg()), new Lda(new VReg()), new Sta(new VReg()), @@ -946,7 +946,7 @@ describe("WatchExpressions", function () { let expected = [ new Mov(new VReg(), new VReg()), - new Defineclasswithbuffer(new Imm(0), "#1#", "1", new Imm(0), new VReg()), + new Defineclasswithbuffer(new Imm(0), "#1#", "_1", new Imm(0), new VReg()), new Sta(new VReg()), new Lda(new VReg()), new Sta(new VReg()), diff --git a/ts2panda/ts2abc/ts2abc.cpp b/ts2panda/ts2abc/ts2abc.cpp index db81b9f50ebf67944edac2334c3037c2a45f9aad..57c350067eea1a9a4c3b2750f21c54640296a751 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -45,9 +45,13 @@ 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; +uint32_t MAX_UINT8 = static_cast(std::numeric_limits::max()); constexpr std::size_t BOUND_LEFT = 0; constexpr std::size_t BOUND_RIGHT = 0; @@ -168,6 +172,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 +274,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 +666,114 @@ 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; + ASSERT(static_cast(panda::panda_file::LiteralTag::INTEGER) <= MAX_UINT8); + 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) { + ASSERT(static_cast(panda::panda_file::LiteralTag::BUILTINTYPEINDEX) <= MAX_UINT8); + typeTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::BUILTINTYPEINDEX); + typeValueLiteral.tag_ = panda::panda_file::LiteralTag::BUILTINTYPEINDEX; + ASSERT(type <= MAX_UINT8); + typeValueLiteral.value_ = static_cast(type); + } else { + ASSERT(static_cast(panda::panda_file::LiteralTag::LITERALARRAY) <= MAX_UINT8); + typeTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::LITERALARRAY); + typeValueLiteral.tag_ = panda::panda_file::LiteralTag::LITERALARRAY; + std::string typeId = GetLiteralId(type - LITERALBUFFERINDEXOFFSET); + typeValueLiteral.value_ = typeId; + } + literalArray.emplace_back(typeTagLiteral); + literalArray.emplace_back(typeValueLiteral); } + std::string litId = GetLiteralId(g_newLiteralArrayIndex--); + 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; + ASSERT(static_cast(panda::panda_file::LiteralTag::STRING) <= MAX_UINT8); + 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) { + ASSERT(static_cast(panda::panda_file::LiteralTag::BUILTINTYPEINDEX) <= MAX_UINT8); + typeTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::BUILTINTYPEINDEX); + typeValueLiteral.tag_ = panda::panda_file::LiteralTag::BUILTINTYPEINDEX; + ASSERT(typeIndex <= MAX_UINT8); + typeValueLiteral.value_ = static_cast(typeIndex); + } else { + ASSERT(static_cast(panda::panda_file::LiteralTag::LITERALARRAY) <= MAX_UINT8); + typeTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::LITERALARRAY); + typeValueLiteral.tag_ = panda::panda_file::LiteralTag::LITERALARRAY; + std::string litId = GetLiteralId(typeIndex - LITERALBUFFERINDEXOFFSET); + typeValueLiteral.value_ = litId; + } + + literalArray.emplace_back(typeTagLiteral); + literalArray.emplace_back(typeValueLiteral); + } + + std::string litId = GetLiteralId(g_newLiteralArrayIndex--); + 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 +786,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 +799,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 +813,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( @@ -796,7 +841,7 @@ static void ParseFunctionIcSize(const Json::Value &function, panda::pandasm::Fun } } -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); @@ -806,9 +851,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); ParseFunctionKind(function, pandaFunc); ParseFunctionIcSize(function, pandaFunc); @@ -856,7 +901,7 @@ static void GenerateESModuleRecord(panda::pandasm::Program &prog) prog.record_table.emplace(ecmaModuleRecord.name, std::move(ecmaModuleRecord)); } -static void AddModuleRecord(panda::pandasm::Program &prog, const std::string &moduleName, uint32_t moduleIdx) +static void AddModuleRecord(panda::pandasm::Program &prog, const std::string &moduleName) { auto iter = prog.record_table.find("_ESModuleRecord"); if (iter != prog.record_table.end()) { @@ -864,8 +909,9 @@ static void AddModuleRecord(panda::pandasm::Program &prog, const std::string &mo auto moduleIdxField = panda::pandasm::Field(LANG_EXT); moduleIdxField.name = moduleName; 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)); } @@ -961,6 +1007,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-----------------"); @@ -991,12 +1045,13 @@ static void ParseOptions(const Json::Value &rootValue, panda::pandasm::Program & ParseDisplayTypeinfo(rootValue); ParseOptLogLevel(rootValue); ParseIsDtsFile(rootValue); + ParseEnableTypeInfo(rootValue); ParseCompilerOutputProto(rootValue); } 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)); } @@ -1024,7 +1079,8 @@ 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)); + auto litId = literalBuffer["k"].asString(); + prog.literalarray_table.emplace(litId, std::move(literalarrayInstance)); } static void ParseModuleRequests(const Json::Value &moduleRequests, @@ -1144,18 +1200,19 @@ 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); + std::string moduleName = ParseString(moduleRecord["moduleName"].asString()); + AddModuleRecord(prog, moduleName); + std::string moduleId = GetLiteralId(g_newLiteralArrayIndex--); 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) { auto typeInfoRecord = rootValue["ti"]; auto typeFlag = typeInfoRecord["tf"].asBool(); - auto typeSummaryIndex = typeInfoRecord["tsi"].asUInt(); + auto typeSummaryIndex = typeInfoRecord["tsi"].asString(); auto ecmaTypeInfoRecord = panda::pandasm::Record("_ESTypeInfoRecord", LANG_EXT); ecmaTypeInfoRecord.metadata->SetAccessFlags(panda::ACC_PUBLIC); @@ -1165,12 +1222,15 @@ static void ParseSingleTypeInfo(const Json::Value &rootValue, panda::pandasm::Pr 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)); + + if (g_enableTypeinfo) { + auto typeSummaryIndexField = panda::pandasm::Field(LANG_EXT); + typeSummaryIndexField.name = "typeSummaryOffset"; + typeSummaryIndexField.type = panda::pandasm::Type("u32", 0); + typeSummaryIndexField.metadata->SetValue( + panda::pandasm::ScalarValue::Create(typeSummaryIndex)); + ecmaTypeInfoRecord.field_list.emplace_back(std::move(typeSummaryIndexField)); + } prog.record_table.emplace(ecmaTypeInfoRecord.name, std::move(ecmaTypeInfoRecord)); } @@ -1227,6 +1287,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; diff --git a/ts2panda/ts2abc/ts2abc.h b/ts2panda/ts2abc/ts2abc.h index 421205bb35905ff34455f39acfdbcb1e7b5dbb8b..4b4658dfdb431b8337ffa9a6a5be827e7d8af10d 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;