diff --git a/src/mapleall/bin/dex2mpl b/src/mapleall/bin/dex2mpl index 6f68dc5596479d81c0c9cebb857bd5e61c6d00bc..6dffc10f03b4008f8b020ad25766681cff5c9318 100755 Binary files a/src/mapleall/bin/dex2mpl and b/src/mapleall/bin/dex2mpl differ diff --git a/src/mapleall/bin/dex2mpl_android b/src/mapleall/bin/dex2mpl_android index 22712adc9a6e33d974ae714f66e1dd9233b651c7..6fb359a0361319183a676d3692de0495356b6f75 100755 Binary files a/src/mapleall/bin/dex2mpl_android and b/src/mapleall/bin/dex2mpl_android differ diff --git a/src/mapleall/bin/jbc2mpl b/src/mapleall/bin/jbc2mpl index 94253141a2abf7901d05cfae7f0d8729b8f01dcb..cc787f12eadf1271b95349e77ffbffe04bc270cb 100755 Binary files a/src/mapleall/bin/jbc2mpl and b/src/mapleall/bin/jbc2mpl differ diff --git a/src/mapleall/maple_be/include/cg/aarch64/aarch64_cgfunc.h b/src/mapleall/maple_be/include/cg/aarch64/aarch64_cgfunc.h index d57378872048af92adeabc252d660a470fe15128..23ee8c76ac4ebfceeb4d53b1e687a0653098eb98 100644 --- a/src/mapleall/maple_be/include/cg/aarch64/aarch64_cgfunc.h +++ b/src/mapleall/maple_be/include/cg/aarch64/aarch64_cgfunc.h @@ -327,7 +327,7 @@ class AArch64CGFunc : public CGFunc { return *memPool->New(op, amount, bitLen); } - void SplitMovImmOpndInstruction(int64 immVal, RegOperand &destReg); + void SplitMovImmOpndInstruction(int64 immVal, RegOperand &destReg, Insn *curInsn = nullptr); Operand &GetOrCreateFuncNameOpnd(const MIRSymbol &symbol); void GenerateYieldpoint(BB &bb) override; diff --git a/src/mapleall/maple_be/include/cg/aarch64/aarch64_color_ra.h b/src/mapleall/maple_be/include/cg/aarch64/aarch64_color_ra.h index 8c5a334cbd2f501b87322202c3fcb9c39996467c..c13e62a18ae57664994a09600ff905a9cfa8a81c 100644 --- a/src/mapleall/maple_be/include/cg/aarch64/aarch64_color_ra.h +++ b/src/mapleall/maple_be/include/cg/aarch64/aarch64_color_ra.h @@ -1213,8 +1213,8 @@ class GraphColorRegAllocator : public AArch64RegAllocator { RegOperand *GetReplaceOpndForLRA(Insn &insn, const Operand &opnd, uint32 &spillIdx, uint64 &usedRegMask, bool isDef); RegOperand *GetReplaceOpnd(Insn &insn, const Operand &opnd, uint32 &spillIdx, uint64 &usedRegMask, bool isDef); void MarkCalleeSaveRegs(); - void MarkUsedRegs(Operand &opnd, BBAssignInfo *bbInfo, uint64 &usedRegMask); - uint64 FinalizeRegisterPreprocess(BBAssignInfo *bbInfo, FinalizeRegisterInfo &fInfo, Insn &insn); + void MarkUsedRegs(Operand &opnd, uint64 &usedRegMask); + uint64 FinalizeRegisterPreprocess(FinalizeRegisterInfo &fInfo, Insn &insn); void FinalizeRegisters(); MapleVector::iterator GetHighPriorityLr(MapleVector &lrSet) const; diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp index 41569fff7b1cfbbee8beb846042a8c40ddfcc9a9..0d28aed8e613f09c22f6ab3c7d18a556a142aa60 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp @@ -535,7 +535,7 @@ bool AArch64CGFunc::IsStoreMop(MOperator mOp) const { } } -void AArch64CGFunc::SplitMovImmOpndInstruction(int64 immVal, RegOperand &destReg) { +void AArch64CGFunc::SplitMovImmOpndInstruction(int64 immVal, RegOperand &destReg, Insn *curInsn) { bool useMovz = BetterUseMOVZ(immVal); bool useMovk = false; /* get lower 32 bits of the immediate */ @@ -550,6 +550,7 @@ void AArch64CGFunc::SplitMovImmOpndInstruction(int64 immVal, RegOperand &destReg } uint64 sa = 0; + auto *bb = (curInsn != nullptr) ? curInsn->GetBB() : GetCurBB(); for (int64 i = 0 ; i < maxLoopTime; ++i, sa += k16BitSize) { /* create an imm opereand which represents the i-th 16-bit chunk of the immediate */ uint64 chunkVal = (static_cast(immVal) >> sa) & 0x0000FFFFULL; @@ -558,16 +559,22 @@ void AArch64CGFunc::SplitMovImmOpndInstruction(int64 immVal, RegOperand &destReg } ImmOperand &src16 = CreateImmOperand(chunkVal, k16BitSize, false); LogicalShiftLeftOperand *lslOpnd = GetLogicalShiftLeftOperand(sa, true); + Insn *newInsn = nullptr; if (!useMovk) { /* use movz or movn */ if (!useMovz) { src16.BitwiseNegate(); } MOperator mOpCode = useMovz ? MOP_xmovzri16 : MOP_xmovnri16; - GetCurBB()->AppendInsn(GetCG()->BuildInstruction(mOpCode, destReg, src16, *lslOpnd)); + newInsn = &GetCG()->BuildInstruction(mOpCode, destReg, src16, *lslOpnd); useMovk = true; } else { - GetCurBB()->AppendInsn(GetCG()->BuildInstruction(MOP_xmovkri16, destReg, src16, *lslOpnd)); + newInsn = &GetCG()->BuildInstruction(MOP_xmovkri16, destReg, src16, *lslOpnd); + } + if (curInsn != nullptr) { + bb->InsertInsnBefore(*curInsn, *newInsn); + } else { + bb->AppendInsn(*newInsn); } } @@ -575,7 +582,11 @@ void AArch64CGFunc::SplitMovImmOpndInstruction(int64 immVal, RegOperand &destReg /* copy lower 32 bits to higher 32 bits */ AArch64ImmOperand &immOpnd = CreateImmOperand(k32BitSize, k8BitSize, false); Insn &insn = GetCG()->BuildInstruction(MPO_xbfirri6i6, destReg, destReg, immOpnd, immOpnd); - GetCurBB()->AppendInsn(insn); + if (curInsn != nullptr) { + bb->InsertInsnBefore(*curInsn, insn); + } else { + bb->AppendInsn(insn); + } } } diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_color_ra.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_color_ra.cpp index 30808356af8653e7d4478accedc29543c6e38710..a873e0c38b7ec22100569bb77d73075cf9d0afd3 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_color_ra.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_color_ra.cpp @@ -2017,11 +2017,13 @@ void GraphColorRegAllocator::HandleLocalRegAssignment(regno_t regNO, LocalRegAll localRa.SetPregUsed(assignedReg, isInt); localRa.SetRegAssigned(regNO, isInt); localRa.SetRegAssignmentMap(isInt, regNO, assignedReg); + lr->SetAssignedRegNO(assignedReg); founded = true; break; } if (!founded) { localRa.SetRegSpilled(regNO, isInt); + lr->SetSpilled(true); } } } @@ -2933,24 +2935,25 @@ RegOperand *GraphColorRegAllocator::GetReplaceOpnd(Insn &insn, const Operand &op return &phyOpnd; } -void GraphColorRegAllocator::MarkUsedRegs(Operand &opnd, BBAssignInfo *bbInfo, uint64 &usedRegMask) { +void GraphColorRegAllocator::MarkUsedRegs(Operand &opnd, uint64 &usedRegMask) { auto ®Opnd = static_cast(opnd); uint32 pregInterval = (regOpnd.GetRegisterType() == kRegTyInt) ? 0 : (V0 - R30); uint32 vregNO = regOpnd.GetRegisterNumber(); LiveRange *lr = lrVec[vregNO]; if (lr != nullptr) { + if (lr->IsSpilled()) { + lr->SetAssignedRegNO(0); + } if (lr->GetAssignedRegNO() != 0) { usedRegMask |= (1ULL << (lr->GetAssignedRegNO() - pregInterval)); } if (lr->GetSplitLr() && lr->GetSplitLr()->GetAssignedRegNO()) { usedRegMask |= (1ULL << (lr->GetSplitLr()->GetAssignedRegNO() - pregInterval)); } - } else if (bbInfo != nullptr && bbInfo->HasRegMap(vregNO)) { - usedRegMask |= (1ULL << (bbInfo->GetRegMapElem(vregNO) - pregInterval)); } } -uint64 GraphColorRegAllocator::FinalizeRegisterPreprocess(BBAssignInfo *bbInfo, FinalizeRegisterInfo &fInfo, +uint64 GraphColorRegAllocator::FinalizeRegisterPreprocess(FinalizeRegisterInfo &fInfo, Insn &insn) { uint64 usedRegMask = 0; const AArch64MD *md = &AArch64CG::kMd[static_cast(&insn)->GetMachineOpcode()]; @@ -2966,12 +2969,12 @@ uint64 GraphColorRegAllocator::FinalizeRegisterPreprocess(BBAssignInfo *bbInfo, Operand *base = memOpnd.GetBaseRegister(); if (base != nullptr) { fInfo.SetBaseOperand(opnd, i); - MarkUsedRegs(*base, bbInfo, usedRegMask); + MarkUsedRegs(*base, usedRegMask); } Operand *offset = memOpnd.GetIndexRegister(); if (offset != nullptr) { fInfo.SetOffsetOperand(opnd); - MarkUsedRegs(*offset, bbInfo, usedRegMask); + MarkUsedRegs(*offset, usedRegMask); } } else { bool isDef = md->GetOperand(i)->IsRegDef(); @@ -2982,11 +2985,11 @@ uint64 GraphColorRegAllocator::FinalizeRegisterPreprocess(BBAssignInfo *bbInfo, * Need to exclude def also, since it will clobber the result when the * original value is reloaded. */ - MarkUsedRegs(opnd, bbInfo, usedRegMask); + MarkUsedRegs(opnd, usedRegMask); } else { fInfo.SetUseOperand(opnd, i); if (opnd.IsRegister()) { - MarkUsedRegs(opnd, bbInfo, usedRegMask); + MarkUsedRegs(opnd, usedRegMask); } } } @@ -2997,7 +3000,6 @@ uint64 GraphColorRegAllocator::FinalizeRegisterPreprocess(BBAssignInfo *bbInfo, /* Iterate through all instructions and change the vreg to preg. */ void GraphColorRegAllocator::FinalizeRegisters() { for (auto *bb : sortedBBs) { - BBAssignInfo *bbInfo = bbRegInfo[bb->GetId()]; FOR_BB_INSNS(insn, bb) { if (insn->IsImmaterialInsn()) { continue; @@ -3010,7 +3012,7 @@ void GraphColorRegAllocator::FinalizeRegisters() { } FinalizeRegisterInfo *fInfo = memPool->New(alloc); - uint64 usedRegMask = FinalizeRegisterPreprocess(bbInfo, *fInfo, *insn); + uint64 usedRegMask = FinalizeRegisterPreprocess(*fInfo, *insn); uint32 defSpillIdx = 0; uint32 useSpillIdx = 0; MemOperand *memOpnd = nullptr; diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_ebo.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_ebo.cpp index db582fab242cf9ccb805d34104cb69b4f722e228..b357e94d7cf1be19885fbd839fa6bb7d4636321d 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_ebo.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_ebo.cpp @@ -594,7 +594,7 @@ bool AArch64Ebo::SimplifyBothConst(BB &bb, Insn &insn, const AArch64ImmOperand & AArch64ImmOperand *immOperand = &a64CGFunc->CreateImmOperand(val, opndSize, false); if (!immOperand->IsSingleInstructionMovable()) { ASSERT(res->IsRegister(), " expect a register operand"); - static_cast(cgFunc)->SplitMovImmOpndInstruction(val, *(static_cast(res))); + static_cast(cgFunc)->SplitMovImmOpndInstruction(val, *(static_cast(res)), &insn); bb.RemoveInsn(insn); } else { MOperator newmOp = opndSize == k64BitSize ? MOP_xmovri64 : MOP_xmovri32; diff --git a/src/mapleall/maple_ir/include/global_tables.h b/src/mapleall/maple_ir/include/global_tables.h index 77b6623b1aafa06b134c5e93ba42103895582dbe..b728fc58624fb17e8bf5702af7bd51a44ab98ccf 100644 --- a/src/mapleall/maple_ir/include/global_tables.h +++ b/src/mapleall/maple_ir/include/global_tables.h @@ -64,23 +64,23 @@ class IntConstKey { friend class IntConstHash; friend class IntConstCmp; public: - IntConstKey(int64 v, uint64 id) : val(v), tyidxFieldID(id) {} + IntConstKey(int64 v, TyIdx tyIdx) : val(v), tyIdx(tyIdx) {} private: int64 val; - uint64 tyidxFieldID; + TyIdx tyIdx; }; class IntConstHash { public: std::size_t operator() (const IntConstKey &key) const { - return std::hash{}(key.val) ^ (std::hash{}(key.tyidxFieldID) << 1); + return std::hash{}(key.val) ^ (std::hash{}(key.tyIdx) << 1); } }; class IntConstCmp { public: bool operator() (const IntConstKey &lkey, const IntConstKey &rkey) const { - return lkey.val == rkey.val && lkey.tyidxFieldID == rkey.tyidxFieldID; + return lkey.val == rkey.val && lkey.tyIdx == rkey.tyIdx; } }; diff --git a/src/mapleall/maple_ir/src/global_tables.cpp b/src/mapleall/maple_ir/src/global_tables.cpp index 3ec7a9d9bb5e32e089809b17bf778cda250be985..7703585d73d74252551cd262dda42a121bb9a182 100644 --- a/src/mapleall/maple_ir/src/global_tables.cpp +++ b/src/mapleall/maple_ir/src/global_tables.cpp @@ -305,8 +305,7 @@ MIRIntConst *IntConstTable::GetOrCreateIntConst(int64 val, MIRType &type) { } MIRIntConst *IntConstTable::DoGetOrCreateIntConst(int64 val, MIRType &type) { - uint64 idid = static_cast(type.GetTypeIndex()); // shift bit is 32 - IntConstKey key(val, idid); + IntConstKey key(val, type.GetTypeIndex()); if (intConstTable.find(key) != intConstTable.end()) { return intConstTable[key]; } @@ -315,8 +314,7 @@ MIRIntConst *IntConstTable::DoGetOrCreateIntConst(int64 val, MIRType &type) { } MIRIntConst *IntConstTable::DoGetOrCreateIntConstTreadSafe(int64 val, MIRType &type) { - uint64 idid = static_cast(type.GetTypeIndex()); // shift bit is 32 - IntConstKey key(val, idid); + IntConstKey key(val, type.GetTypeIndex()); { std::shared_lock lock(mtx); if (intConstTable.find(key) != intConstTable.end()) { diff --git a/src/mapleall/maple_me/src/alias_class.cpp b/src/mapleall/maple_me/src/alias_class.cpp index 831679328f6e040db27377bfc0d2b64f0e2344a6..05e0396eb9ab0fc017e31d2dea30f4bee08ce305 100644 --- a/src/mapleall/maple_me/src/alias_class.cpp +++ b/src/mapleall/maple_me/src/alias_class.cpp @@ -65,14 +65,14 @@ static bool OpCanFormAddress(Opcode op) { return false; } -inline bool IsNullOrDummySymbolOst(OriginalSt *ost) { +inline bool IsNullOrDummySymbolOst(const OriginalSt *ost) { if ((ost == nullptr) || (ost && ost->IsSymbolOst() && (ost->GetMIRSymbol()->GetName() == "__nads_dummysym__"))) { return true; } return false; } -inline bool OriginalStIsAuto(OriginalSt *ost) { +inline bool OriginalStIsAuto(const OriginalSt *ost) { if (!ost->IsSymbolOst()) { return false; } @@ -318,7 +318,8 @@ void AliasClass::ApplyUnionForFieldsInAggCopy(const OriginalSt *lhsost, const Or if (!IsPotentialAddress(fieldType->GetPrimType(), &mirModule)) { continue; } - MapleUnorderedMap &mirSt2Ost = ssaTab.GetOriginalStTable().mirSt2Ost; + MapleUnorderedMap &mirSt2Ost = + ssaTab.GetOriginalStTable().mirSt2Ost; auto lhsit = mirSt2Ost.find(SymbolFieldPair(lhsost->GetMIRSymbol()->GetStIdx(), fieldID)); auto rhsit = mirSt2Ost.find(SymbolFieldPair(rhsost->GetMIRSymbol()->GetStIdx(), fieldID)); if (lhsit == mirSt2Ost.end() && rhsit == mirSt2Ost.end()) { @@ -329,7 +330,8 @@ void AliasClass::ApplyUnionForFieldsInAggCopy(const OriginalSt *lhsost, const Or OriginalSt *rhsFieldOst = nullptr; if (lhsit == mirSt2Ost.end()) { // create a new OriginalSt for lhs field - lhsFieldOst = ssaTab.GetOriginalStTable().CreateSymbolOriginalSt(*lhsost->GetMIRSymbol(), lhsost->GetPuIdx(), fieldID); + lhsFieldOst = ssaTab.GetOriginalStTable().CreateSymbolOriginalSt( + *lhsost->GetMIRSymbol(), lhsost->GetPuIdx(), fieldID); osym2Elem.push_back(nullptr); ssaTab.GetVersionStTable().CreateZeroVersionSt(lhsFieldOst); @@ -339,7 +341,8 @@ void AliasClass::ApplyUnionForFieldsInAggCopy(const OriginalSt *lhsost, const Or if (rhsit == mirSt2Ost.end()) { // create a new OriginalSt for rhs field - rhsFieldOst = ssaTab.GetOriginalStTable().CreateSymbolOriginalSt(*rhsost->GetMIRSymbol(), rhsost->GetPuIdx(), fieldID); + rhsFieldOst = ssaTab.GetOriginalStTable().CreateSymbolOriginalSt( + *rhsost->GetMIRSymbol(), rhsost->GetPuIdx(), fieldID); osym2Elem.push_back(nullptr); ssaTab.GetVersionStTable().CreateZeroVersionSt(rhsFieldOst); } else { @@ -360,7 +363,8 @@ void AliasClass::ApplyUnionForFieldsInAggCopy(const OriginalSt *lhsost, const Or if (!IsPotentialAddress(fieldType->GetPrimType(), &mirModule)) { continue; } - MapleUnorderedMap &mirSt2Ost = ssaTab.GetOriginalStTable().mirSt2Ost; + MapleUnorderedMap &mirSt2Ost = + ssaTab.GetOriginalStTable().mirSt2Ost; auto it = mirSt2Ost.find(SymbolFieldPair(lhsost->GetMIRSymbol()->GetStIdx(), fieldID)); if (it == mirSt2Ost.end()) { continue; @@ -379,7 +383,8 @@ void AliasClass::ApplyUnionForFieldsInAggCopy(const OriginalSt *lhsost, const Or if (!IsPotentialAddress(fieldType->GetPrimType(), &mirModule)) { continue; } - MapleUnorderedMap &mirSt2Ost = ssaTab.GetOriginalStTable().mirSt2Ost; + MapleUnorderedMap &mirSt2Ost = + ssaTab.GetOriginalStTable().mirSt2Ost; auto it = mirSt2Ost.find(SymbolFieldPair(rhsost->GetMIRSymbol()->GetStIdx(), fieldID)); if (it == mirSt2Ost.end()) { continue; diff --git a/src/mplfe/ast_input/include/ast_expr.h b/src/mplfe/ast_input/include/ast_expr.h index f049707bc138757facdfca94dee718711d5909da..0f4c4090bc768a5c4e85ca3cf5110a33aa3a0374 100644 --- a/src/mplfe/ast_input/include/ast_expr.h +++ b/src/mplfe/ast_input/include/ast_expr.h @@ -19,6 +19,7 @@ namespace maple { class ASTDecl; +class ASTStmt; class ASTExpr { public: explicit ASTExpr(ASTOp o) : op(o) {} @@ -69,9 +70,36 @@ class ASTImplicitCastExpr : public ASTExpr { return child->GetType(); } + void SetSrcType(MIRType *type) { + src = type; + } + + const MIRType *GetSrcType() const { + return src; + } + + void SetDstType(MIRType *type) { + dst = type; + } + + const MIRType *GetDstType() const { + return dst; + } + + void SetNeededCvt(bool cvt) { + isNeededCvt = cvt; + } + + bool IsNeededCvt(const UniqueFEIRExpr &expr) const; + protected: UniqueFEIRExpr Emit2FEExprImpl(std::list &stmts) const override; + + private: ASTExpr *child = nullptr; + MIRType *src = nullptr; + MIRType *dst = nullptr; + bool isNeededCvt = false; }; class ASTDeclRefExpr : public ASTExpr { @@ -1031,5 +1059,23 @@ class ASTAtomicExpr : public ASTExpr { ASTAtomicOp atomicOp; bool isFromStmt = false; }; + +class ASTExprStmtExpr : public ASTExpr { + public: + ASTExprStmtExpr() : ASTExpr(kASTOpStmtExpr) {} + ~ASTExprStmtExpr() = default; + void SetCompoundStmt(ASTStmt *sub) { + cpdStmt = sub; + } + + ASTStmt *GetSubExpr() const { + return cpdStmt; + } + + private: + UniqueFEIRExpr Emit2FEExprImpl(std::list &stmts) const override; + + ASTStmt *cpdStmt = nullptr; +}; } #endif //MPLFE_AST_INPUT_INCLUDE_AST_EXPR_H diff --git a/src/mplfe/ast_input/include/ast_op.h b/src/mplfe/ast_input/include/ast_op.h index 76c2590ec4f6bca51d86182e9b153ec4f1d46c66..896373e061517a992285fcfb8abefdbd56d6f6d5 100644 --- a/src/mplfe/ast_input/include/ast_op.h +++ b/src/mplfe/ast_input/include/ast_op.h @@ -121,6 +121,7 @@ enum ASTOp { kASTOpSubstNonTypeTemplateParm, kASTOpDependentScopeDeclRef, kASTOpAtomic, + kASTOpStmtExpr, }; enum ASTStmtOp { diff --git a/src/mplfe/ast_input/include/ast_stmt.h b/src/mplfe/ast_input/include/ast_stmt.h index 737753170329adb39a24c0ab90aab4ac349eeed1..6798815f5106a9e19b46f37b5271ba65b801dc92 100644 --- a/src/mplfe/ast_input/include/ast_stmt.h +++ b/src/mplfe/ast_input/include/ast_stmt.h @@ -30,6 +30,14 @@ class ASTStmt { return Emit2FEStmtImpl(); } + ASTStmtOp GetASTStmtOp() const { + return op; + } + + const std::vector &GetExprs() const { + return exprs; + } + protected: virtual std::list Emit2FEStmtImpl() const = 0; ASTStmtOp op; @@ -434,8 +442,14 @@ class ASTStmtExprStmt : public ASTStmt { ASTStmtExprStmt() : ASTStmt(kASTStmtStmtExpr) {} ~ASTStmtExprStmt() override = default; + void SetBodyStmt(ASTStmt *stmt) { + cpdStmt = stmt; + } + private: std::list Emit2FEStmtImpl() const override; + + ASTStmt *cpdStmt = nullptr; }; class ASTCStyleCastExprStmt : public ASTStmt { diff --git a/src/mplfe/ast_input/lib/ast_type.cpp b/src/mplfe/ast_input/lib/ast_type.cpp index b26a041cc36391f3f02c4d6c93c469e130068c18..d1274f42f4ff0b84043dc970d3058349156f6536 100644 --- a/src/mplfe/ast_input/lib/ast_type.cpp +++ b/src/mplfe/ast_input/lib/ast_type.cpp @@ -44,13 +44,8 @@ PrimType LibAstFile::CvtPrimType(const clang::BuiltinType::Kind kind) const { case clang::BuiltinType::UShort: return PTY_u16; case clang::BuiltinType::UInt: -#ifndef AST2MPL_64 - case clang::BuiltinType::ULong: -#endif return PTY_u32; -#ifdef AST2MPL_64 case clang::BuiltinType::ULong: -#endif case clang::BuiltinType::ULongLong: return PTY_u64; case clang::BuiltinType::UInt128: @@ -64,13 +59,8 @@ PrimType LibAstFile::CvtPrimType(const clang::BuiltinType::Kind kind) const { return PTY_i16; case clang::BuiltinType::Char32: case clang::BuiltinType::Int: -#ifndef AST2MPL_64 - case clang::BuiltinType::Long: -#endif return PTY_i32; -#ifdef AST2MPL_64 case clang::BuiltinType::Long: -#endif case clang::BuiltinType::LongLong: case clang::BuiltinType::Int128: // pty = PTY_i128, NOTYETHANDLED return PTY_i64; diff --git a/src/mplfe/ast_input/src/ast_decl.cpp b/src/mplfe/ast_input/src/ast_decl.cpp index d26d0b7011e4fd2e4a123e7937c4ecc8cac4a697..3c2b347ec7144683544ba2a4631b529d05147c4c 100644 --- a/src/mplfe/ast_input/src/ast_decl.cpp +++ b/src/mplfe/ast_input/src/ast_decl.cpp @@ -56,7 +56,14 @@ void ASTVar::GenerateInitStmtImpl(std::list &stmts) { UniqueFEIRVar feirVar = Translate2FEIRVar(); UniqueFEIRExpr expr = GetInitExpr()->Emit2FEExpr(stmts); if (expr != nullptr) { // InitListExpr array not emit here - UniqueFEIRStmt stmt = FEIRBuilder::CreateStmtDAssign(std::move(feirVar), std::move(expr)); + PrimType srcPrimType = expr->GetPrimType(); + UniqueFEIRStmt stmt; + if (srcPrimType != feirVar->GetType()->GetPrimType() && srcPrimType != PTY_agg && srcPrimType != PTY_void) { + UniqueFEIRExpr cvtExpr = FEIRBuilder::CreateExprCvtPrim(std::move(expr), feirVar->GetType()->GetPrimType()); + stmt = FEIRBuilder::CreateStmtDAssign(std::move(feirVar), std::move(cvtExpr)); + } else { + stmt = FEIRBuilder::CreateStmtDAssign(std::move(feirVar), std::move(expr)); + } stmts.emplace_back(std::move(stmt)); } } diff --git a/src/mplfe/ast_input/src/ast_expr.cpp b/src/mplfe/ast_input/src/ast_expr.cpp index 5e8731a537d338c7bf073de4d89ea93df675e44e..83a67d362aee5569ae29659d0dcc31406647581d 100644 --- a/src/mplfe/ast_input/src/ast_expr.cpp +++ b/src/mplfe/ast_input/src/ast_expr.cpp @@ -21,6 +21,7 @@ #include "fe_utils_ast.h" #include "feir_type_helper.h" #include "fe_manager.h" +#include "ast_stmt.h" namespace maple { // ---------- ASTExpr ---------- @@ -103,9 +104,20 @@ UniqueFEIRExpr ASTImplicitCastExpr::Emit2FEExprImpl(std::list &s const ASTExpr *childExpr = child; CHECK_FATAL(childExpr != nullptr, "childExpr is nullptr"); UniqueFEIRExpr feirImplicitCastExpr = childExpr->Emit2FEExpr(stmts); + if (IsNeededCvt(feirImplicitCastExpr)) { + return FEIRBuilder::CreateExprCvtPrim(std::move(feirImplicitCastExpr), dst->GetPrimType()); + } return feirImplicitCastExpr; } +bool ASTImplicitCastExpr::IsNeededCvt(const UniqueFEIRExpr &expr) const { + if (expr == nullptr || dst == nullptr) { + return false; + } + PrimType srcPrimType = expr->GetPrimType(); + return isNeededCvt && srcPrimType != dst->GetPrimType() && srcPrimType != PTY_agg && srcPrimType != PTY_void; +} + // ---------- ASTUnaryOperatorExpr ---------- void ASTUnaryOperatorExpr::SetUOExpr(ASTExpr *astExpr) { expr = astExpr; @@ -833,4 +845,15 @@ UniqueFEIRExpr ASTAtomicExpr::Emit2FEExprImpl(std::list &stmts) } return atomicExpr; } + +// ---------- ASTExprStmtExpr ---------- +UniqueFEIRExpr ASTExprStmtExpr::Emit2FEExprImpl(std::list &stmts) const { + std::list stmts0 = cpdStmt->Emit2FEStmt(); + for (auto &stmt : stmts0) { + stmts.emplace_back(std::move(stmt)); + } + CHECK_FATAL(cpdStmt->GetASTStmtOp() == kASTStmtCompound, "Invalid in ASTExprStmtExpr"); + stmts0.clear(); + return static_cast(cpdStmt)->GetASTStmtList().back()->GetExprs().back()->Emit2FEExpr(stmts); +} } diff --git a/src/mplfe/ast_input/src/ast_parser.cpp b/src/mplfe/ast_input/src/ast_parser.cpp index 55d9793baee7c7c3abfb9a9007f6538895d6c95d..3963b2566a50b49ef2ed33331043981e9c46fd50 100644 --- a/src/mplfe/ast_input/src/ast_parser.cpp +++ b/src/mplfe/ast_input/src/ast_parser.cpp @@ -244,14 +244,11 @@ ASTStmt *ASTParser::ProcessStmtCStyleCastExpr(MapleAllocator &allocator, const c } ASTStmt *ASTParser::ProcessStmtStmtExpr(MapleAllocator &allocator, const clang::StmtExpr &stmtExpr) { - ASTStmtExprStmt *astStmtExprStmt = ASTDeclsBuilder::ASTStmtBuilder(allocator); - CHECK_FATAL(astStmtExprStmt != nullptr, "astStmtExprStmt is nullptr"); - ASTExpr *astExpr = ProcessExpr(allocator, &stmtExpr); - if (astExpr == nullptr) { - return nullptr; - } - astStmtExprStmt->SetASTExpr(astExpr); - return astStmtExprStmt; + ASTStmtExprStmt *astStmt = ASTDeclsBuilder::ASTStmtBuilder(allocator); + const clang::CompoundStmt *cpdStmt = stmtExpr.getSubStmt(); + ASTStmt *astCompoundStmt = ProcessStmtCompoundStmt(allocator, *cpdStmt); + astStmt->SetBodyStmt(astCompoundStmt); + return astStmt; } ASTStmt *ASTParser::ProcessStmtCompoundAssignOperator(MapleAllocator &allocator, @@ -912,8 +909,10 @@ ASTExpr *ASTParser::ProcessExprDesignatedInitUpdateExpr(MapleAllocator &allocato } ASTExpr *ASTParser::ProcessExprStmtExpr(MapleAllocator &allocator, const clang::StmtExpr &expr) { - CHECK_FATAL(false, "NIY"); - return nullptr; + ASTExprStmtExpr *astExpr = ASTDeclsBuilder::ASTExprBuilder(allocator); + ASTStmt *compoundStmt = ProcessStmt(allocator, *expr.getSubStmt()); + astExpr->SetCompoundStmt(compoundStmt); + return astExpr; } ASTExpr *ASTParser::ProcessExprConditionalOperator(MapleAllocator &allocator, const clang::ConditionalOperator &expr) { @@ -1132,20 +1131,24 @@ ASTExpr *ASTParser::ProcessExprImplicitCastExpr(MapleAllocator &allocator, const case clang::CK_NoOp: case clang::CK_ArrayToPointerDecay: case clang::CK_FunctionToPointerDecay: + case clang::CK_LValueToRValue: + break; + case clang::CK_FloatingToIntegral: case clang::CK_FloatingCast: case clang::CK_IntegralCast: - case clang::CK_LValueToRValue: { - ASTExpr *astExpr = ProcessExpr(allocator, expr.getSubExpr()); - if (astExpr == nullptr) { - return nullptr; - } - astImplicitCastExpr->SetASTExpr(astExpr); + astImplicitCastExpr->SetSrcType(astFile->CvtType(expr.getSubExpr()->getType())); + astImplicitCastExpr->SetDstType(astFile->CvtType(expr.getType())); + astImplicitCastExpr->SetNeededCvt(true); break; - } default: CHECK_FATAL(false, "NIY"); return nullptr; } + ASTExpr *astExpr = ProcessExpr(allocator, expr.getSubExpr()); + if (astExpr == nullptr) { + return nullptr; + } + astImplicitCastExpr->SetASTExpr(astExpr); return astImplicitCastExpr; } @@ -1570,7 +1573,11 @@ ASTDecl *ASTParser::ProcessDeclVarDecl(MapleAllocator &allocator, const clang::V if (initExpr->getStmtClass() == clang::Stmt::InitListExprClass) { static_cast(astInitExpr)->SetInitListVarName(astVar->GenerateUniqueVarName()); } - astVar->SetInitExpr(astInitExpr); + if (initExpr->getStmtClass() == clang::Stmt::ImplicitCastExprClass) { + astVar->SetInitExpr(static_cast(astInitExpr)->GetASTExpr()); + } else { + astVar->SetInitExpr(astInitExpr); + } } return astVar; } diff --git a/src/mplfe/ast_input/src/ast_stmt.cpp b/src/mplfe/ast_input/src/ast_stmt.cpp index 1adee71e4f65ced6809e17c16290816d0f5b4c50..96e8da58441809fdf5058a6186c9a80a541c816a 100644 --- a/src/mplfe/ast_input/src/ast_stmt.cpp +++ b/src/mplfe/ast_input/src/ast_stmt.cpp @@ -346,9 +346,7 @@ std::list ASTCharacterLiteralStmt::Emit2FEStmtImpl() const { // ---------- ASTStmtExprStmt ---------- std::list ASTStmtExprStmt::Emit2FEStmtImpl() const { - CHECK_FATAL(false, "NYI"); - std::list stmts; - return stmts; + return cpdStmt->Emit2FEStmt(); } // ---------- ASTCStyleCastExprStmt ---------- diff --git a/src/mplfe/common/include/feir_builder.h b/src/mplfe/common/include/feir_builder.h index 633a8308a07ae4ec785d691b842dd1789538d527..51ea2c25d26bc10ebf089df83196ae27e2f2f9c4 100644 --- a/src/mplfe/common/include/feir_builder.h +++ b/src/mplfe/common/include/feir_builder.h @@ -71,6 +71,7 @@ class FEIRBuilder { static UniqueFEIRExpr CreateExprZExt(UniqueFEIRExpr srcExpr, PrimType dstType); static UniqueFEIRExpr CreateExprCvtPrim(UniqueFEIRVar srcVar, PrimType dstType); static UniqueFEIRExpr CreateExprCvtPrim(UniqueFEIRExpr srcExpr, PrimType dstType); + static UniqueFEIRExpr CreateExprCvtPrim(Opcode argOp, UniqueFEIRExpr srcExpr, PrimType dstType); static UniqueFEIRExpr CreateExprJavaNewInstance(UniqueFEIRType type); static UniqueFEIRExpr CreateExprJavaNewInstance(UniqueFEIRType type, uint32 argTypeID); static UniqueFEIRExpr CreateExprJavaNewInstance(UniqueFEIRType type, uint32 argTypeID, bool isRcPermanent); diff --git a/src/mplfe/common/src/feir_builder.cpp b/src/mplfe/common/src/feir_builder.cpp index 74789e254c3221061b51f9f3745766277d609e5b..dfbbff082f90ce5a6f4e4586be3a012e25c0fcae 100644 --- a/src/mplfe/common/src/feir_builder.cpp +++ b/src/mplfe/common/src/feir_builder.cpp @@ -242,6 +242,14 @@ UniqueFEIRExpr FEIRBuilder::CreateExprCvtPrim(UniqueFEIRExpr srcExpr, PrimType d return expr; } +UniqueFEIRExpr FEIRBuilder::CreateExprCvtPrim(Opcode argOp, UniqueFEIRExpr srcExpr, PrimType dstType) { + UniqueFEIRExpr expr = std::make_unique(argOp, std::move(srcExpr)); + CHECK_NULL_FATAL(expr); + FEIRExprTypeCvt *ptrExpr = static_cast(expr.get()); + ptrExpr->GetType()->SetPrimType(dstType); + return expr; +} + UniqueFEIRExpr FEIRBuilder::CreateExprJavaNewInstance(UniqueFEIRType type) { UniqueFEIRExpr expr = std::make_unique(std::move(type)); CHECK_NULL_FATAL(expr);