From 81e5c5d03a5705408076eaada3c0f6f0a315dc5b Mon Sep 17 00:00:00 2001 From: huangyu Date: Mon, 20 Mar 2023 15:20:15 +0800 Subject: [PATCH] Fix es2abc type extractor recorded wrong type index 1. Fix get wrong type index result caused by unsigned char overflow 2. Fix mismatched binding of type and instruction order Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I6OJP7 Signed-off-by: huangyu Change-Id: Ia4bfcb314bd68d0c8fe89c3d521e51b0970febde --- .../core/emitter/typeExtractorEmitter.cpp | 15 ++++-- .../test-type-bind-instruction-with-branch.ts | 51 +++++++++++++++++++ .../typescript/extractor/typeRecorder.cpp | 15 ++++++ es2panda/typescript/extractor/typeRecorder.h | 2 +- 4 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 es2panda/test/type_extractor/testcases_with_assert/test-type-bind-instruction-with-branch.ts diff --git a/es2panda/compiler/core/emitter/typeExtractorEmitter.cpp b/es2panda/compiler/core/emitter/typeExtractorEmitter.cpp index d42d7a6619..153820c9b8 100644 --- a/es2panda/compiler/core/emitter/typeExtractorEmitter.cpp +++ b/es2panda/compiler/core/emitter/typeExtractorEmitter.cpp @@ -95,16 +95,23 @@ void TypeExtractorEmitter::GenFunctionTypeInfo(panda::pandasm::Program *prog) co auto recorder = pg_->Context()->TypeRecorder(); std::vector typedInsns; typedInsns.reserve(pg_->TypedInsns().size() * 4U); // Expand to 4 pieces of information - size_t index = 0U; + uint32_t index = 0; + uint32_t remove = 0; for (const auto *ins : pg_->Insns()) { + if (func_->ins[index].opcode == panda::pandasm::Opcode::INVALID) { + remove++; + index++; + continue; + } auto t = pg_->TypedInsns().find(ins); if (t != pg_->TypedInsns().end()) { int64_t typeIndex = t->second; uint32_t orderIndex = index; if (typeIndex > extractor::TypeRecorder::PRIMITIVETYPE_ANY) { - GenInsnTypeInfo(recorder, orderIndex, typeIndex, typedInsns); - DCOUT << "[LOG]" << func_->name << ": " << func_->ins[index].ToString("", true, func_->regs_num) << - " | " << orderIndex << " | " << typeIndex << std::endl; + uint32_t realOrderIndex = orderIndex - remove; + GenInsnTypeInfo(recorder, realOrderIndex, typeIndex, typedInsns); + DCOUT << "[LOG] " << func_->name << ": " << func_->ins[index].ToString("", true, func_->regs_num) << + " | " << realOrderIndex << " | " << typeIndex << std::endl; } } index++; diff --git a/es2panda/test/type_extractor/testcases_with_assert/test-type-bind-instruction-with-branch.ts b/es2panda/test/type_extractor/testcases_with_assert/test-type-bind-instruction-with-branch.ts new file mode 100644 index 0000000000..a55c685fd9 --- /dev/null +++ b/es2panda/test/type_extractor/testcases_with_assert/test-type-bind-instruction-with-branch.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +declare function AssertType(value: any, type: string): void; +declare function print(arg0: any, arg?: any): string; + +const target = "ts"; +let extensions: string[] = ["js", "ts", "ets"]; + +for (let i = 0; i < extensions.length; i++) { + AssertType(extensions[i], "string"); +} + +class FileItem { + ext:string; + constructor(extension: string) { + this.ext = extension; + } +} + +function reportFail() { + print("Failed to find target.") +} + +function createFile(ext: string) { + let f = new FileItem(ext); + AssertType(f, "FileItem"); + print("Create file item successfully!"); +} + +let foundExt:string = extensions.find((u) => u == target); + +if (!foundExt) { + AssertType(foundExt, "string"); + reportFail(); +} else { + createFile(foundExt); +} diff --git a/es2panda/typescript/extractor/typeRecorder.cpp b/es2panda/typescript/extractor/typeRecorder.cpp index 4b071d3fa8..051193d5cc 100644 --- a/es2panda/typescript/extractor/typeRecorder.cpp +++ b/es2panda/typescript/extractor/typeRecorder.cpp @@ -144,6 +144,7 @@ void TypeRecorder::SetNodeTypeIndex(const ir::AstNode *node, int64_t index) } nodeTypeIndex_[node] = index; + ASSERT(GetNodeTypeIndex(node) == index); } int64_t TypeRecorder::GetVariableTypeIndex(const binder::Variable *variable) const @@ -158,6 +159,7 @@ void TypeRecorder::SetVariableTypeIndex(const binder::Variable *variable, int64_ } variableTypeIndex_[variable] = index; + ASSERT(GetVariableTypeIndex(variable) == index); } void TypeRecorder::SetIdentifierTypeIndex(const ir::Identifier *identifier, int64_t index) @@ -177,6 +179,7 @@ int64_t TypeRecorder::GetBuiltinInst(const std::vector &allTypes) const void TypeRecorder::SetBuiltinInst(const std::vector &allTypes, int64_t instIndex) { builtinInst_[allTypes] = instIndex; + ASSERT(GetBuiltinInst(allTypes) == instIndex); } int64_t TypeRecorder::GetGenericInst(const std::vector &allTypes) const @@ -187,6 +190,7 @@ int64_t TypeRecorder::GetGenericInst(const std::vector &allTypes) const void TypeRecorder::SetGenericInst(const std::vector &allTypes, int64_t instIndex) { genericInst_[allTypes] = instIndex; + ASSERT(GetGenericInst(allTypes) == instIndex); } int64_t TypeRecorder::GetIndexSig(int64_t refIndex) const @@ -197,6 +201,7 @@ int64_t TypeRecorder::GetIndexSig(int64_t refIndex) const void TypeRecorder::SetIndexSig(int64_t refIndex, int64_t indexSigIndex) { indexSig_[refIndex] = indexSigIndex; + ASSERT(GetIndexSig(refIndex) == indexSigIndex); } int64_t TypeRecorder::GetClassInst(int64_t classIndex) const @@ -210,6 +215,7 @@ void TypeRecorder::SetClassInst(int64_t classIndex, int64_t instIndex) return; } classInst_[classIndex] = instIndex; + ASSERT(GetClassInst(classIndex) == instIndex); } int64_t TypeRecorder::GetClassType(int64_t instIndex) const @@ -225,6 +231,7 @@ void TypeRecorder::SetClassType(int64_t instIndex, int64_t classIndex) return; } classType_[instIndex] = classIndex; + ASSERT(GetClassType(instIndex) == classIndex); } int64_t TypeRecorder::GetArrayType(int64_t contentIndex) const @@ -235,6 +242,7 @@ int64_t TypeRecorder::GetArrayType(int64_t contentIndex) const void TypeRecorder::SetArrayType(int64_t contentIndex, int64_t arrayIndex) { arrayType_[contentIndex] = arrayIndex; + ASSERT(GetArrayType(contentIndex) == arrayIndex); } int64_t TypeRecorder::GetUnionType(const std::string &unionStr) const @@ -245,6 +253,7 @@ int64_t TypeRecorder::GetUnionType(const std::string &unionStr) const void TypeRecorder::SetUnionType(const std::string &unionStr, int64_t unionIndex) { unionType_[unionStr] = unionIndex; + ASSERT(GetUnionType(unionStr) == unionIndex); } int64_t TypeRecorder::GetObjectType(const std::string &objectStr) const @@ -255,6 +264,7 @@ int64_t TypeRecorder::GetObjectType(const std::string &objectStr) const void TypeRecorder::SetObjectType(const std::string &objectStr, int64_t objectIndex) { objectType_[objectStr] = objectIndex; + ASSERT(GetObjectType(objectStr) == objectIndex); } int64_t TypeRecorder::GetFunctionType(const std::string &functionStr) const @@ -265,6 +275,7 @@ int64_t TypeRecorder::GetFunctionType(const std::string &functionStr) const void TypeRecorder::SetFunctionType(const std::string &functionStr, int64_t functionIndex) { functionType_[functionStr] = functionIndex; + ASSERT(GetFunctionType(functionStr) == functionIndex); } int64_t TypeRecorder::GetExportType(const std::string &exportStr) const @@ -275,6 +286,7 @@ int64_t TypeRecorder::GetExportType(const std::string &exportStr) const void TypeRecorder::SetExportType(const std::string &exportStr, int64_t exportIndex) { exportType_[exportStr] = exportIndex; + ASSERT(GetExportType(exportStr) == exportIndex); } int64_t TypeRecorder::GetDeclareType(const std::string &declareStr) const @@ -285,6 +297,7 @@ int64_t TypeRecorder::GetDeclareType(const std::string &declareStr) const void TypeRecorder::SetDeclareType(const std::string &declareStr, int64_t declareIndex) { declareType_[declareStr] = declareIndex; + ASSERT(GetDeclareType(declareStr) == declareIndex); } int64_t TypeRecorder::GetNamespaceType(const std::string &namespaceStr) const @@ -295,6 +308,7 @@ int64_t TypeRecorder::GetNamespaceType(const std::string &namespaceStr) const void TypeRecorder::SetNamespaceType(const std::string &namespaceStr, int64_t namespaceIndex) { namespaceType_[namespaceStr] = namespaceIndex; + ASSERT(GetNamespaceType(namespaceStr) == namespaceIndex); } std::string TypeRecorder::GetNamespacePath(const std::string &namespaceStr) const @@ -305,6 +319,7 @@ std::string TypeRecorder::GetNamespacePath(const std::string &namespaceStr) cons void TypeRecorder::SetNamespacePath(const std::string &namespaceStr, const std::string &filePath) { namespacePath_[namespaceStr] = filePath; + ASSERT(GetNamespacePath(namespaceStr) == filePath); } const std::set &TypeRecorder::GetAnonymousReExport() const diff --git a/es2panda/typescript/extractor/typeRecorder.h b/es2panda/typescript/extractor/typeRecorder.h index 5f83102da4..465b1e51bf 100644 --- a/es2panda/typescript/extractor/typeRecorder.h +++ b/es2panda/typescript/extractor/typeRecorder.h @@ -115,7 +115,7 @@ public: ALWAYS_INLINE void Dump(const parser::Program *program) const; - static constexpr uint8_t PRIMITIVETYPE_ANY = 0U; + static constexpr int64_t PRIMITIVETYPE_ANY = 0; static constexpr int64_t USERTYPEINDEXHEAD = 100; private: -- Gitee