From 80305b3a20f88015a85c45ba57bca728b90f2fcf Mon Sep 17 00:00:00 2001 From: binaryfz Date: Thu, 20 May 2021 16:27:41 +0800 Subject: [PATCH] [mplfeC] Fix signed/unsigned shr.[mplfec] fix CStyleCast and get real struct/func def --- src/mplfe/ast_input/lib/ast_util.cpp | 4 ++-- src/mplfe/ast_input/lib/ast_util.h | 2 +- src/mplfe/ast_input/src/ast_parser.cpp | 15 +++++++++++++-- src/mplfe/common/src/feir_stmt.cpp | 3 --- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/mplfe/ast_input/lib/ast_util.cpp b/src/mplfe/ast_input/lib/ast_util.cpp index 37399ce187..654cf29d06 100644 --- a/src/mplfe/ast_input/lib/ast_util.cpp +++ b/src/mplfe/ast_input/lib/ast_util.cpp @@ -93,7 +93,7 @@ std::string ASTUtil::GetNameWithSuffix(const std::string &origName, const std::s return ss.str(); } -Opcode ASTUtil::CvtBinaryOpcode(uint32_t opcode) { +Opcode ASTUtil::CvtBinaryOpcode(uint32_t opcode, PrimType pty) { switch (opcode) { case clang::BO_Mul: return OP_mul; // "*" @@ -108,7 +108,7 @@ Opcode ASTUtil::CvtBinaryOpcode(uint32_t opcode) { case clang::BO_Shl: return OP_shl; // "<<" case clang::BO_Shr: - return OP_lshr; // ">>" + return IsUnsignedInteger(pty) ? OP_lshr : OP_ashr; // ">>" case clang::BO_LT: return OP_lt; // "<" case clang::BO_GT: diff --git a/src/mplfe/ast_input/lib/ast_util.h b/src/mplfe/ast_input/lib/ast_util.h index da9997c95a..4c87116f60 100644 --- a/src/mplfe/ast_input/lib/ast_util.h +++ b/src/mplfe/ast_input/lib/ast_util.h @@ -44,7 +44,7 @@ class ASTUtil { static MIRType *CvtPrimType(const clang::QualType type); static Opcode CvtUnaryOpcode(uint32_t opcode); - static Opcode CvtBinaryOpcode(uint32_t opcode); + static Opcode CvtBinaryOpcode(uint32_t opcode, PrimType pty = PTY_begin); static Opcode CvtBinaryAssignOpcode(uint32_t opcode); static bool IsVoidPointerType(const TyIdx &tyIdx); diff --git a/src/mplfe/ast_input/src/ast_parser.cpp b/src/mplfe/ast_input/src/ast_parser.cpp index 21f7d95304..5ae2f6a99a 100644 --- a/src/mplfe/ast_input/src/ast_parser.cpp +++ b/src/mplfe/ast_input/src/ast_parser.cpp @@ -55,7 +55,8 @@ ASTBinaryOperatorExpr *ASTParser::AllocBinaryOperatorExpr(MapleAllocator &alloca if (bo.isPtrMemOp()) { return ASTDeclsBuilder::ASTExprBuilder(allocator); } - Opcode mirOpcode = ASTUtil::CvtBinaryOpcode(bo.getOpcode()); + MIRType *lhTy = astFile->CvtType(bo.getLHS()->getType()); + Opcode mirOpcode = ASTUtil::CvtBinaryOpcode(bo.getOpcode(), lhTy->GetPrimType()); CHECK_FATAL(mirOpcode != OP_undef, "Opcode not support!"); auto *expr = ASTDeclsBuilder::ASTExprBuilder(allocator); expr->SetOpcode(mirOpcode); @@ -1899,7 +1900,12 @@ bool ASTParser::RetrieveStructs(MapleAllocator &allocator) { for (auto &decl : recordDecles) { clang::RecordDecl *recDecl = llvm::cast(decl->getCanonicalDecl()); if (!recDecl->isCompleteDefinition()) { - continue; + clang::RecordDecl *recDeclDef = recDecl->getDefinition(); + if (recDeclDef == nullptr) { + continue; + } else { + recDecl = recDeclDef; + } } ASTStruct *curStructOrUnion = static_cast(ProcessDecl(allocator, *recDecl)); if (curStructOrUnion == nullptr) { @@ -1914,6 +1920,11 @@ bool ASTParser::RetrieveStructs(MapleAllocator &allocator) { bool ASTParser::RetrieveFuncs(MapleAllocator &allocator) { for (auto &func : funcDecles) { clang::FunctionDecl *funcDecl = llvm::cast(func); + if (funcDecl->isDefined()) { + funcDecl = funcDecl->getDefinition(); + } else { + continue; + } ASTFunc *af = static_cast(ProcessDecl(allocator, *funcDecl)); if (af == nullptr) { return false; diff --git a/src/mplfe/common/src/feir_stmt.cpp b/src/mplfe/common/src/feir_stmt.cpp index 84b18e70b0..4573ab0bd8 100644 --- a/src/mplfe/common/src/feir_stmt.cpp +++ b/src/mplfe/common/src/feir_stmt.cpp @@ -3338,9 +3338,6 @@ BaseNode *FEIRExprCStyleCast::GenMIRNodeImpl(MIRBuilder &mirBuilder) const { if (fromNode.EqualTo(toNode)) { return false; } - if (baseNode.GetOpCode() == OP_bnot && baseNode.Opnd(0)->GetOpCode() == OP_constval) { - return false; - } return true; }; if (!isCvtNeeded(*srcType, *destType, *sub)) { -- Gitee