1*0a6a1f1dSLionel Sambuc //===- ThreadSafetyCommon.cpp ----------------------------------*- C++ --*-===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc // The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc // Implementation of the interfaces declared in ThreadSafetyCommon.h
11*0a6a1f1dSLionel Sambuc //
12*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
13*0a6a1f1dSLionel Sambuc
14*0a6a1f1dSLionel Sambuc #include "clang/Analysis/Analyses/ThreadSafetyCommon.h"
15*0a6a1f1dSLionel Sambuc #include "clang/AST/Attr.h"
16*0a6a1f1dSLionel Sambuc #include "clang/AST/DeclCXX.h"
17*0a6a1f1dSLionel Sambuc #include "clang/AST/DeclObjC.h"
18*0a6a1f1dSLionel Sambuc #include "clang/AST/ExprCXX.h"
19*0a6a1f1dSLionel Sambuc #include "clang/AST/StmtCXX.h"
20*0a6a1f1dSLionel Sambuc #include "clang/Analysis/Analyses/PostOrderCFGView.h"
21*0a6a1f1dSLionel Sambuc #include "clang/Analysis/Analyses/ThreadSafetyTIL.h"
22*0a6a1f1dSLionel Sambuc #include "clang/Analysis/Analyses/ThreadSafetyTraverse.h"
23*0a6a1f1dSLionel Sambuc #include "clang/Analysis/AnalysisContext.h"
24*0a6a1f1dSLionel Sambuc #include "clang/Analysis/CFG.h"
25*0a6a1f1dSLionel Sambuc #include "clang/Basic/OperatorKinds.h"
26*0a6a1f1dSLionel Sambuc #include "clang/Basic/SourceLocation.h"
27*0a6a1f1dSLionel Sambuc #include "clang/Basic/SourceManager.h"
28*0a6a1f1dSLionel Sambuc #include "llvm/ADT/DenseMap.h"
29*0a6a1f1dSLionel Sambuc #include "llvm/ADT/SmallVector.h"
30*0a6a1f1dSLionel Sambuc #include "llvm/ADT/StringRef.h"
31*0a6a1f1dSLionel Sambuc #include <algorithm>
32*0a6a1f1dSLionel Sambuc #include <climits>
33*0a6a1f1dSLionel Sambuc #include <vector>
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc
36*0a6a1f1dSLionel Sambuc namespace clang {
37*0a6a1f1dSLionel Sambuc namespace threadSafety {
38*0a6a1f1dSLionel Sambuc
39*0a6a1f1dSLionel Sambuc // From ThreadSafetyUtil.h
getSourceLiteralString(const clang::Expr * CE)40*0a6a1f1dSLionel Sambuc std::string getSourceLiteralString(const clang::Expr *CE) {
41*0a6a1f1dSLionel Sambuc switch (CE->getStmtClass()) {
42*0a6a1f1dSLionel Sambuc case Stmt::IntegerLiteralClass:
43*0a6a1f1dSLionel Sambuc return cast<IntegerLiteral>(CE)->getValue().toString(10, true);
44*0a6a1f1dSLionel Sambuc case Stmt::StringLiteralClass: {
45*0a6a1f1dSLionel Sambuc std::string ret("\"");
46*0a6a1f1dSLionel Sambuc ret += cast<StringLiteral>(CE)->getString();
47*0a6a1f1dSLionel Sambuc ret += "\"";
48*0a6a1f1dSLionel Sambuc return ret;
49*0a6a1f1dSLionel Sambuc }
50*0a6a1f1dSLionel Sambuc case Stmt::CharacterLiteralClass:
51*0a6a1f1dSLionel Sambuc case Stmt::CXXNullPtrLiteralExprClass:
52*0a6a1f1dSLionel Sambuc case Stmt::GNUNullExprClass:
53*0a6a1f1dSLionel Sambuc case Stmt::CXXBoolLiteralExprClass:
54*0a6a1f1dSLionel Sambuc case Stmt::FloatingLiteralClass:
55*0a6a1f1dSLionel Sambuc case Stmt::ImaginaryLiteralClass:
56*0a6a1f1dSLionel Sambuc case Stmt::ObjCStringLiteralClass:
57*0a6a1f1dSLionel Sambuc default:
58*0a6a1f1dSLionel Sambuc return "#lit";
59*0a6a1f1dSLionel Sambuc }
60*0a6a1f1dSLionel Sambuc }
61*0a6a1f1dSLionel Sambuc
62*0a6a1f1dSLionel Sambuc namespace til {
63*0a6a1f1dSLionel Sambuc
64*0a6a1f1dSLionel Sambuc // Return true if E is a variable that points to an incomplete Phi node.
isIncompletePhi(const SExpr * E)65*0a6a1f1dSLionel Sambuc static bool isIncompletePhi(const SExpr *E) {
66*0a6a1f1dSLionel Sambuc if (const auto *Ph = dyn_cast<Phi>(E))
67*0a6a1f1dSLionel Sambuc return Ph->status() == Phi::PH_Incomplete;
68*0a6a1f1dSLionel Sambuc return false;
69*0a6a1f1dSLionel Sambuc }
70*0a6a1f1dSLionel Sambuc
71*0a6a1f1dSLionel Sambuc } // end namespace til
72*0a6a1f1dSLionel Sambuc
73*0a6a1f1dSLionel Sambuc
74*0a6a1f1dSLionel Sambuc typedef SExprBuilder::CallingContext CallingContext;
75*0a6a1f1dSLionel Sambuc
76*0a6a1f1dSLionel Sambuc
lookupStmt(const Stmt * S)77*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::lookupStmt(const Stmt *S) {
78*0a6a1f1dSLionel Sambuc auto It = SMap.find(S);
79*0a6a1f1dSLionel Sambuc if (It != SMap.end())
80*0a6a1f1dSLionel Sambuc return It->second;
81*0a6a1f1dSLionel Sambuc return nullptr;
82*0a6a1f1dSLionel Sambuc }
83*0a6a1f1dSLionel Sambuc
84*0a6a1f1dSLionel Sambuc
buildCFG(CFGWalker & Walker)85*0a6a1f1dSLionel Sambuc til::SCFG *SExprBuilder::buildCFG(CFGWalker &Walker) {
86*0a6a1f1dSLionel Sambuc Walker.walk(*this);
87*0a6a1f1dSLionel Sambuc return Scfg;
88*0a6a1f1dSLionel Sambuc }
89*0a6a1f1dSLionel Sambuc
90*0a6a1f1dSLionel Sambuc
91*0a6a1f1dSLionel Sambuc
isCalleeArrow(const Expr * E)92*0a6a1f1dSLionel Sambuc inline bool isCalleeArrow(const Expr *E) {
93*0a6a1f1dSLionel Sambuc const MemberExpr *ME = dyn_cast<MemberExpr>(E->IgnoreParenCasts());
94*0a6a1f1dSLionel Sambuc return ME ? ME->isArrow() : false;
95*0a6a1f1dSLionel Sambuc }
96*0a6a1f1dSLionel Sambuc
97*0a6a1f1dSLionel Sambuc
98*0a6a1f1dSLionel Sambuc /// \brief Translate a clang expression in an attribute to a til::SExpr.
99*0a6a1f1dSLionel Sambuc /// Constructs the context from D, DeclExp, and SelfDecl.
100*0a6a1f1dSLionel Sambuc ///
101*0a6a1f1dSLionel Sambuc /// \param AttrExp The expression to translate.
102*0a6a1f1dSLionel Sambuc /// \param D The declaration to which the attribute is attached.
103*0a6a1f1dSLionel Sambuc /// \param DeclExp An expression involving the Decl to which the attribute
104*0a6a1f1dSLionel Sambuc /// is attached. E.g. the call to a function.
translateAttrExpr(const Expr * AttrExp,const NamedDecl * D,const Expr * DeclExp,VarDecl * SelfDecl)105*0a6a1f1dSLionel Sambuc CapabilityExpr SExprBuilder::translateAttrExpr(const Expr *AttrExp,
106*0a6a1f1dSLionel Sambuc const NamedDecl *D,
107*0a6a1f1dSLionel Sambuc const Expr *DeclExp,
108*0a6a1f1dSLionel Sambuc VarDecl *SelfDecl) {
109*0a6a1f1dSLionel Sambuc // If we are processing a raw attribute expression, with no substitutions.
110*0a6a1f1dSLionel Sambuc if (!DeclExp)
111*0a6a1f1dSLionel Sambuc return translateAttrExpr(AttrExp, nullptr);
112*0a6a1f1dSLionel Sambuc
113*0a6a1f1dSLionel Sambuc CallingContext Ctx(nullptr, D);
114*0a6a1f1dSLionel Sambuc
115*0a6a1f1dSLionel Sambuc // Examine DeclExp to find SelfArg and FunArgs, which are used to substitute
116*0a6a1f1dSLionel Sambuc // for formal parameters when we call buildMutexID later.
117*0a6a1f1dSLionel Sambuc if (const MemberExpr *ME = dyn_cast<MemberExpr>(DeclExp)) {
118*0a6a1f1dSLionel Sambuc Ctx.SelfArg = ME->getBase();
119*0a6a1f1dSLionel Sambuc Ctx.SelfArrow = ME->isArrow();
120*0a6a1f1dSLionel Sambuc } else if (const CXXMemberCallExpr *CE =
121*0a6a1f1dSLionel Sambuc dyn_cast<CXXMemberCallExpr>(DeclExp)) {
122*0a6a1f1dSLionel Sambuc Ctx.SelfArg = CE->getImplicitObjectArgument();
123*0a6a1f1dSLionel Sambuc Ctx.SelfArrow = isCalleeArrow(CE->getCallee());
124*0a6a1f1dSLionel Sambuc Ctx.NumArgs = CE->getNumArgs();
125*0a6a1f1dSLionel Sambuc Ctx.FunArgs = CE->getArgs();
126*0a6a1f1dSLionel Sambuc } else if (const CallExpr *CE = dyn_cast<CallExpr>(DeclExp)) {
127*0a6a1f1dSLionel Sambuc Ctx.NumArgs = CE->getNumArgs();
128*0a6a1f1dSLionel Sambuc Ctx.FunArgs = CE->getArgs();
129*0a6a1f1dSLionel Sambuc } else if (const CXXConstructExpr *CE =
130*0a6a1f1dSLionel Sambuc dyn_cast<CXXConstructExpr>(DeclExp)) {
131*0a6a1f1dSLionel Sambuc Ctx.SelfArg = nullptr; // Will be set below
132*0a6a1f1dSLionel Sambuc Ctx.NumArgs = CE->getNumArgs();
133*0a6a1f1dSLionel Sambuc Ctx.FunArgs = CE->getArgs();
134*0a6a1f1dSLionel Sambuc } else if (D && isa<CXXDestructorDecl>(D)) {
135*0a6a1f1dSLionel Sambuc // There's no such thing as a "destructor call" in the AST.
136*0a6a1f1dSLionel Sambuc Ctx.SelfArg = DeclExp;
137*0a6a1f1dSLionel Sambuc }
138*0a6a1f1dSLionel Sambuc
139*0a6a1f1dSLionel Sambuc // Hack to handle constructors, where self cannot be recovered from
140*0a6a1f1dSLionel Sambuc // the expression.
141*0a6a1f1dSLionel Sambuc if (SelfDecl && !Ctx.SelfArg) {
142*0a6a1f1dSLionel Sambuc DeclRefExpr SelfDRE(SelfDecl, false, SelfDecl->getType(), VK_LValue,
143*0a6a1f1dSLionel Sambuc SelfDecl->getLocation());
144*0a6a1f1dSLionel Sambuc Ctx.SelfArg = &SelfDRE;
145*0a6a1f1dSLionel Sambuc
146*0a6a1f1dSLionel Sambuc // If the attribute has no arguments, then assume the argument is "this".
147*0a6a1f1dSLionel Sambuc if (!AttrExp)
148*0a6a1f1dSLionel Sambuc return translateAttrExpr(Ctx.SelfArg, nullptr);
149*0a6a1f1dSLionel Sambuc else // For most attributes.
150*0a6a1f1dSLionel Sambuc return translateAttrExpr(AttrExp, &Ctx);
151*0a6a1f1dSLionel Sambuc }
152*0a6a1f1dSLionel Sambuc
153*0a6a1f1dSLionel Sambuc // If the attribute has no arguments, then assume the argument is "this".
154*0a6a1f1dSLionel Sambuc if (!AttrExp)
155*0a6a1f1dSLionel Sambuc return translateAttrExpr(Ctx.SelfArg, nullptr);
156*0a6a1f1dSLionel Sambuc else // For most attributes.
157*0a6a1f1dSLionel Sambuc return translateAttrExpr(AttrExp, &Ctx);
158*0a6a1f1dSLionel Sambuc }
159*0a6a1f1dSLionel Sambuc
160*0a6a1f1dSLionel Sambuc
161*0a6a1f1dSLionel Sambuc /// \brief Translate a clang expression in an attribute to a til::SExpr.
162*0a6a1f1dSLionel Sambuc // This assumes a CallingContext has already been created.
translateAttrExpr(const Expr * AttrExp,CallingContext * Ctx)163*0a6a1f1dSLionel Sambuc CapabilityExpr SExprBuilder::translateAttrExpr(const Expr *AttrExp,
164*0a6a1f1dSLionel Sambuc CallingContext *Ctx) {
165*0a6a1f1dSLionel Sambuc if (!AttrExp)
166*0a6a1f1dSLionel Sambuc return CapabilityExpr(nullptr, false);
167*0a6a1f1dSLionel Sambuc
168*0a6a1f1dSLionel Sambuc if (auto* SLit = dyn_cast<StringLiteral>(AttrExp)) {
169*0a6a1f1dSLionel Sambuc if (SLit->getString() == StringRef("*"))
170*0a6a1f1dSLionel Sambuc // The "*" expr is a universal lock, which essentially turns off
171*0a6a1f1dSLionel Sambuc // checks until it is removed from the lockset.
172*0a6a1f1dSLionel Sambuc return CapabilityExpr(new (Arena) til::Wildcard(), false);
173*0a6a1f1dSLionel Sambuc else
174*0a6a1f1dSLionel Sambuc // Ignore other string literals for now.
175*0a6a1f1dSLionel Sambuc return CapabilityExpr(nullptr, false);
176*0a6a1f1dSLionel Sambuc }
177*0a6a1f1dSLionel Sambuc
178*0a6a1f1dSLionel Sambuc bool Neg = false;
179*0a6a1f1dSLionel Sambuc if (auto *OE = dyn_cast<CXXOperatorCallExpr>(AttrExp)) {
180*0a6a1f1dSLionel Sambuc if (OE->getOperator() == OO_Exclaim) {
181*0a6a1f1dSLionel Sambuc Neg = true;
182*0a6a1f1dSLionel Sambuc AttrExp = OE->getArg(0);
183*0a6a1f1dSLionel Sambuc }
184*0a6a1f1dSLionel Sambuc }
185*0a6a1f1dSLionel Sambuc else if (auto *UO = dyn_cast<UnaryOperator>(AttrExp)) {
186*0a6a1f1dSLionel Sambuc if (UO->getOpcode() == UO_LNot) {
187*0a6a1f1dSLionel Sambuc Neg = true;
188*0a6a1f1dSLionel Sambuc AttrExp = UO->getSubExpr();
189*0a6a1f1dSLionel Sambuc }
190*0a6a1f1dSLionel Sambuc }
191*0a6a1f1dSLionel Sambuc
192*0a6a1f1dSLionel Sambuc til::SExpr *E = translate(AttrExp, Ctx);
193*0a6a1f1dSLionel Sambuc
194*0a6a1f1dSLionel Sambuc // Trap mutex expressions like nullptr, or 0.
195*0a6a1f1dSLionel Sambuc // Any literal value is nonsense.
196*0a6a1f1dSLionel Sambuc if (!E || isa<til::Literal>(E))
197*0a6a1f1dSLionel Sambuc return CapabilityExpr(nullptr, false);
198*0a6a1f1dSLionel Sambuc
199*0a6a1f1dSLionel Sambuc // Hack to deal with smart pointers -- strip off top-level pointer casts.
200*0a6a1f1dSLionel Sambuc if (auto *CE = dyn_cast_or_null<til::Cast>(E)) {
201*0a6a1f1dSLionel Sambuc if (CE->castOpcode() == til::CAST_objToPtr)
202*0a6a1f1dSLionel Sambuc return CapabilityExpr(CE->expr(), Neg);
203*0a6a1f1dSLionel Sambuc }
204*0a6a1f1dSLionel Sambuc return CapabilityExpr(E, Neg);
205*0a6a1f1dSLionel Sambuc }
206*0a6a1f1dSLionel Sambuc
207*0a6a1f1dSLionel Sambuc
208*0a6a1f1dSLionel Sambuc
209*0a6a1f1dSLionel Sambuc // Translate a clang statement or expression to a TIL expression.
210*0a6a1f1dSLionel Sambuc // Also performs substitution of variables; Ctx provides the context.
211*0a6a1f1dSLionel Sambuc // Dispatches on the type of S.
translate(const Stmt * S,CallingContext * Ctx)212*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::translate(const Stmt *S, CallingContext *Ctx) {
213*0a6a1f1dSLionel Sambuc if (!S)
214*0a6a1f1dSLionel Sambuc return nullptr;
215*0a6a1f1dSLionel Sambuc
216*0a6a1f1dSLionel Sambuc // Check if S has already been translated and cached.
217*0a6a1f1dSLionel Sambuc // This handles the lookup of SSA names for DeclRefExprs here.
218*0a6a1f1dSLionel Sambuc if (til::SExpr *E = lookupStmt(S))
219*0a6a1f1dSLionel Sambuc return E;
220*0a6a1f1dSLionel Sambuc
221*0a6a1f1dSLionel Sambuc switch (S->getStmtClass()) {
222*0a6a1f1dSLionel Sambuc case Stmt::DeclRefExprClass:
223*0a6a1f1dSLionel Sambuc return translateDeclRefExpr(cast<DeclRefExpr>(S), Ctx);
224*0a6a1f1dSLionel Sambuc case Stmt::CXXThisExprClass:
225*0a6a1f1dSLionel Sambuc return translateCXXThisExpr(cast<CXXThisExpr>(S), Ctx);
226*0a6a1f1dSLionel Sambuc case Stmt::MemberExprClass:
227*0a6a1f1dSLionel Sambuc return translateMemberExpr(cast<MemberExpr>(S), Ctx);
228*0a6a1f1dSLionel Sambuc case Stmt::CallExprClass:
229*0a6a1f1dSLionel Sambuc return translateCallExpr(cast<CallExpr>(S), Ctx);
230*0a6a1f1dSLionel Sambuc case Stmt::CXXMemberCallExprClass:
231*0a6a1f1dSLionel Sambuc return translateCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), Ctx);
232*0a6a1f1dSLionel Sambuc case Stmt::CXXOperatorCallExprClass:
233*0a6a1f1dSLionel Sambuc return translateCXXOperatorCallExpr(cast<CXXOperatorCallExpr>(S), Ctx);
234*0a6a1f1dSLionel Sambuc case Stmt::UnaryOperatorClass:
235*0a6a1f1dSLionel Sambuc return translateUnaryOperator(cast<UnaryOperator>(S), Ctx);
236*0a6a1f1dSLionel Sambuc case Stmt::BinaryOperatorClass:
237*0a6a1f1dSLionel Sambuc case Stmt::CompoundAssignOperatorClass:
238*0a6a1f1dSLionel Sambuc return translateBinaryOperator(cast<BinaryOperator>(S), Ctx);
239*0a6a1f1dSLionel Sambuc
240*0a6a1f1dSLionel Sambuc case Stmt::ArraySubscriptExprClass:
241*0a6a1f1dSLionel Sambuc return translateArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Ctx);
242*0a6a1f1dSLionel Sambuc case Stmt::ConditionalOperatorClass:
243*0a6a1f1dSLionel Sambuc return translateAbstractConditionalOperator(
244*0a6a1f1dSLionel Sambuc cast<ConditionalOperator>(S), Ctx);
245*0a6a1f1dSLionel Sambuc case Stmt::BinaryConditionalOperatorClass:
246*0a6a1f1dSLionel Sambuc return translateAbstractConditionalOperator(
247*0a6a1f1dSLionel Sambuc cast<BinaryConditionalOperator>(S), Ctx);
248*0a6a1f1dSLionel Sambuc
249*0a6a1f1dSLionel Sambuc // We treat these as no-ops
250*0a6a1f1dSLionel Sambuc case Stmt::ParenExprClass:
251*0a6a1f1dSLionel Sambuc return translate(cast<ParenExpr>(S)->getSubExpr(), Ctx);
252*0a6a1f1dSLionel Sambuc case Stmt::ExprWithCleanupsClass:
253*0a6a1f1dSLionel Sambuc return translate(cast<ExprWithCleanups>(S)->getSubExpr(), Ctx);
254*0a6a1f1dSLionel Sambuc case Stmt::CXXBindTemporaryExprClass:
255*0a6a1f1dSLionel Sambuc return translate(cast<CXXBindTemporaryExpr>(S)->getSubExpr(), Ctx);
256*0a6a1f1dSLionel Sambuc
257*0a6a1f1dSLionel Sambuc // Collect all literals
258*0a6a1f1dSLionel Sambuc case Stmt::CharacterLiteralClass:
259*0a6a1f1dSLionel Sambuc case Stmt::CXXNullPtrLiteralExprClass:
260*0a6a1f1dSLionel Sambuc case Stmt::GNUNullExprClass:
261*0a6a1f1dSLionel Sambuc case Stmt::CXXBoolLiteralExprClass:
262*0a6a1f1dSLionel Sambuc case Stmt::FloatingLiteralClass:
263*0a6a1f1dSLionel Sambuc case Stmt::ImaginaryLiteralClass:
264*0a6a1f1dSLionel Sambuc case Stmt::IntegerLiteralClass:
265*0a6a1f1dSLionel Sambuc case Stmt::StringLiteralClass:
266*0a6a1f1dSLionel Sambuc case Stmt::ObjCStringLiteralClass:
267*0a6a1f1dSLionel Sambuc return new (Arena) til::Literal(cast<Expr>(S));
268*0a6a1f1dSLionel Sambuc
269*0a6a1f1dSLionel Sambuc case Stmt::DeclStmtClass:
270*0a6a1f1dSLionel Sambuc return translateDeclStmt(cast<DeclStmt>(S), Ctx);
271*0a6a1f1dSLionel Sambuc default:
272*0a6a1f1dSLionel Sambuc break;
273*0a6a1f1dSLionel Sambuc }
274*0a6a1f1dSLionel Sambuc if (const CastExpr *CE = dyn_cast<CastExpr>(S))
275*0a6a1f1dSLionel Sambuc return translateCastExpr(CE, Ctx);
276*0a6a1f1dSLionel Sambuc
277*0a6a1f1dSLionel Sambuc return new (Arena) til::Undefined(S);
278*0a6a1f1dSLionel Sambuc }
279*0a6a1f1dSLionel Sambuc
280*0a6a1f1dSLionel Sambuc
281*0a6a1f1dSLionel Sambuc
translateDeclRefExpr(const DeclRefExpr * DRE,CallingContext * Ctx)282*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::translateDeclRefExpr(const DeclRefExpr *DRE,
283*0a6a1f1dSLionel Sambuc CallingContext *Ctx) {
284*0a6a1f1dSLionel Sambuc const ValueDecl *VD = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl());
285*0a6a1f1dSLionel Sambuc
286*0a6a1f1dSLionel Sambuc // Function parameters require substitution and/or renaming.
287*0a6a1f1dSLionel Sambuc if (const ParmVarDecl *PV = dyn_cast_or_null<ParmVarDecl>(VD)) {
288*0a6a1f1dSLionel Sambuc const FunctionDecl *FD =
289*0a6a1f1dSLionel Sambuc cast<FunctionDecl>(PV->getDeclContext())->getCanonicalDecl();
290*0a6a1f1dSLionel Sambuc unsigned I = PV->getFunctionScopeIndex();
291*0a6a1f1dSLionel Sambuc
292*0a6a1f1dSLionel Sambuc if (Ctx && Ctx->FunArgs && FD == Ctx->AttrDecl->getCanonicalDecl()) {
293*0a6a1f1dSLionel Sambuc // Substitute call arguments for references to function parameters
294*0a6a1f1dSLionel Sambuc assert(I < Ctx->NumArgs);
295*0a6a1f1dSLionel Sambuc return translate(Ctx->FunArgs[I], Ctx->Prev);
296*0a6a1f1dSLionel Sambuc }
297*0a6a1f1dSLionel Sambuc // Map the param back to the param of the original function declaration
298*0a6a1f1dSLionel Sambuc // for consistent comparisons.
299*0a6a1f1dSLionel Sambuc VD = FD->getParamDecl(I);
300*0a6a1f1dSLionel Sambuc }
301*0a6a1f1dSLionel Sambuc
302*0a6a1f1dSLionel Sambuc // For non-local variables, treat it as a referenced to a named object.
303*0a6a1f1dSLionel Sambuc return new (Arena) til::LiteralPtr(VD);
304*0a6a1f1dSLionel Sambuc }
305*0a6a1f1dSLionel Sambuc
306*0a6a1f1dSLionel Sambuc
translateCXXThisExpr(const CXXThisExpr * TE,CallingContext * Ctx)307*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::translateCXXThisExpr(const CXXThisExpr *TE,
308*0a6a1f1dSLionel Sambuc CallingContext *Ctx) {
309*0a6a1f1dSLionel Sambuc // Substitute for 'this'
310*0a6a1f1dSLionel Sambuc if (Ctx && Ctx->SelfArg)
311*0a6a1f1dSLionel Sambuc return translate(Ctx->SelfArg, Ctx->Prev);
312*0a6a1f1dSLionel Sambuc assert(SelfVar && "We have no variable for 'this'!");
313*0a6a1f1dSLionel Sambuc return SelfVar;
314*0a6a1f1dSLionel Sambuc }
315*0a6a1f1dSLionel Sambuc
316*0a6a1f1dSLionel Sambuc
getValueDeclFromSExpr(const til::SExpr * E)317*0a6a1f1dSLionel Sambuc const ValueDecl *getValueDeclFromSExpr(const til::SExpr *E) {
318*0a6a1f1dSLionel Sambuc if (auto *V = dyn_cast<til::Variable>(E))
319*0a6a1f1dSLionel Sambuc return V->clangDecl();
320*0a6a1f1dSLionel Sambuc if (auto *Ph = dyn_cast<til::Phi>(E))
321*0a6a1f1dSLionel Sambuc return Ph->clangDecl();
322*0a6a1f1dSLionel Sambuc if (auto *P = dyn_cast<til::Project>(E))
323*0a6a1f1dSLionel Sambuc return P->clangDecl();
324*0a6a1f1dSLionel Sambuc if (auto *L = dyn_cast<til::LiteralPtr>(E))
325*0a6a1f1dSLionel Sambuc return L->clangDecl();
326*0a6a1f1dSLionel Sambuc return 0;
327*0a6a1f1dSLionel Sambuc }
328*0a6a1f1dSLionel Sambuc
hasCppPointerType(const til::SExpr * E)329*0a6a1f1dSLionel Sambuc bool hasCppPointerType(const til::SExpr *E) {
330*0a6a1f1dSLionel Sambuc auto *VD = getValueDeclFromSExpr(E);
331*0a6a1f1dSLionel Sambuc if (VD && VD->getType()->isPointerType())
332*0a6a1f1dSLionel Sambuc return true;
333*0a6a1f1dSLionel Sambuc if (auto *C = dyn_cast<til::Cast>(E))
334*0a6a1f1dSLionel Sambuc return C->castOpcode() == til::CAST_objToPtr;
335*0a6a1f1dSLionel Sambuc
336*0a6a1f1dSLionel Sambuc return false;
337*0a6a1f1dSLionel Sambuc }
338*0a6a1f1dSLionel Sambuc
339*0a6a1f1dSLionel Sambuc
340*0a6a1f1dSLionel Sambuc // Grab the very first declaration of virtual method D
getFirstVirtualDecl(const CXXMethodDecl * D)341*0a6a1f1dSLionel Sambuc const CXXMethodDecl* getFirstVirtualDecl(const CXXMethodDecl *D) {
342*0a6a1f1dSLionel Sambuc while (true) {
343*0a6a1f1dSLionel Sambuc D = D->getCanonicalDecl();
344*0a6a1f1dSLionel Sambuc CXXMethodDecl::method_iterator I = D->begin_overridden_methods(),
345*0a6a1f1dSLionel Sambuc E = D->end_overridden_methods();
346*0a6a1f1dSLionel Sambuc if (I == E)
347*0a6a1f1dSLionel Sambuc return D; // Method does not override anything
348*0a6a1f1dSLionel Sambuc D = *I; // FIXME: this does not work with multiple inheritance.
349*0a6a1f1dSLionel Sambuc }
350*0a6a1f1dSLionel Sambuc return nullptr;
351*0a6a1f1dSLionel Sambuc }
352*0a6a1f1dSLionel Sambuc
translateMemberExpr(const MemberExpr * ME,CallingContext * Ctx)353*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::translateMemberExpr(const MemberExpr *ME,
354*0a6a1f1dSLionel Sambuc CallingContext *Ctx) {
355*0a6a1f1dSLionel Sambuc til::SExpr *BE = translate(ME->getBase(), Ctx);
356*0a6a1f1dSLionel Sambuc til::SExpr *E = new (Arena) til::SApply(BE);
357*0a6a1f1dSLionel Sambuc
358*0a6a1f1dSLionel Sambuc const ValueDecl *D = ME->getMemberDecl();
359*0a6a1f1dSLionel Sambuc if (auto *VD = dyn_cast<CXXMethodDecl>(D))
360*0a6a1f1dSLionel Sambuc D = getFirstVirtualDecl(VD);
361*0a6a1f1dSLionel Sambuc
362*0a6a1f1dSLionel Sambuc til::Project *P = new (Arena) til::Project(E, D);
363*0a6a1f1dSLionel Sambuc if (hasCppPointerType(BE))
364*0a6a1f1dSLionel Sambuc P->setArrow(true);
365*0a6a1f1dSLionel Sambuc return P;
366*0a6a1f1dSLionel Sambuc }
367*0a6a1f1dSLionel Sambuc
368*0a6a1f1dSLionel Sambuc
translateCallExpr(const CallExpr * CE,CallingContext * Ctx,const Expr * SelfE)369*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::translateCallExpr(const CallExpr *CE,
370*0a6a1f1dSLionel Sambuc CallingContext *Ctx,
371*0a6a1f1dSLionel Sambuc const Expr *SelfE) {
372*0a6a1f1dSLionel Sambuc if (CapabilityExprMode) {
373*0a6a1f1dSLionel Sambuc // Handle LOCK_RETURNED
374*0a6a1f1dSLionel Sambuc const FunctionDecl *FD = CE->getDirectCallee()->getMostRecentDecl();
375*0a6a1f1dSLionel Sambuc if (LockReturnedAttr* At = FD->getAttr<LockReturnedAttr>()) {
376*0a6a1f1dSLionel Sambuc CallingContext LRCallCtx(Ctx);
377*0a6a1f1dSLionel Sambuc LRCallCtx.AttrDecl = CE->getDirectCallee();
378*0a6a1f1dSLionel Sambuc LRCallCtx.SelfArg = SelfE;
379*0a6a1f1dSLionel Sambuc LRCallCtx.NumArgs = CE->getNumArgs();
380*0a6a1f1dSLionel Sambuc LRCallCtx.FunArgs = CE->getArgs();
381*0a6a1f1dSLionel Sambuc return const_cast<til::SExpr*>(
382*0a6a1f1dSLionel Sambuc translateAttrExpr(At->getArg(), &LRCallCtx).sexpr());
383*0a6a1f1dSLionel Sambuc }
384*0a6a1f1dSLionel Sambuc }
385*0a6a1f1dSLionel Sambuc
386*0a6a1f1dSLionel Sambuc til::SExpr *E = translate(CE->getCallee(), Ctx);
387*0a6a1f1dSLionel Sambuc for (const auto *Arg : CE->arguments()) {
388*0a6a1f1dSLionel Sambuc til::SExpr *A = translate(Arg, Ctx);
389*0a6a1f1dSLionel Sambuc E = new (Arena) til::Apply(E, A);
390*0a6a1f1dSLionel Sambuc }
391*0a6a1f1dSLionel Sambuc return new (Arena) til::Call(E, CE);
392*0a6a1f1dSLionel Sambuc }
393*0a6a1f1dSLionel Sambuc
394*0a6a1f1dSLionel Sambuc
translateCXXMemberCallExpr(const CXXMemberCallExpr * ME,CallingContext * Ctx)395*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::translateCXXMemberCallExpr(
396*0a6a1f1dSLionel Sambuc const CXXMemberCallExpr *ME, CallingContext *Ctx) {
397*0a6a1f1dSLionel Sambuc if (CapabilityExprMode) {
398*0a6a1f1dSLionel Sambuc // Ignore calls to get() on smart pointers.
399*0a6a1f1dSLionel Sambuc if (ME->getMethodDecl()->getNameAsString() == "get" &&
400*0a6a1f1dSLionel Sambuc ME->getNumArgs() == 0) {
401*0a6a1f1dSLionel Sambuc auto *E = translate(ME->getImplicitObjectArgument(), Ctx);
402*0a6a1f1dSLionel Sambuc return new (Arena) til::Cast(til::CAST_objToPtr, E);
403*0a6a1f1dSLionel Sambuc // return E;
404*0a6a1f1dSLionel Sambuc }
405*0a6a1f1dSLionel Sambuc }
406*0a6a1f1dSLionel Sambuc return translateCallExpr(cast<CallExpr>(ME), Ctx,
407*0a6a1f1dSLionel Sambuc ME->getImplicitObjectArgument());
408*0a6a1f1dSLionel Sambuc }
409*0a6a1f1dSLionel Sambuc
410*0a6a1f1dSLionel Sambuc
translateCXXOperatorCallExpr(const CXXOperatorCallExpr * OCE,CallingContext * Ctx)411*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::translateCXXOperatorCallExpr(
412*0a6a1f1dSLionel Sambuc const CXXOperatorCallExpr *OCE, CallingContext *Ctx) {
413*0a6a1f1dSLionel Sambuc if (CapabilityExprMode) {
414*0a6a1f1dSLionel Sambuc // Ignore operator * and operator -> on smart pointers.
415*0a6a1f1dSLionel Sambuc OverloadedOperatorKind k = OCE->getOperator();
416*0a6a1f1dSLionel Sambuc if (k == OO_Star || k == OO_Arrow) {
417*0a6a1f1dSLionel Sambuc auto *E = translate(OCE->getArg(0), Ctx);
418*0a6a1f1dSLionel Sambuc return new (Arena) til::Cast(til::CAST_objToPtr, E);
419*0a6a1f1dSLionel Sambuc // return E;
420*0a6a1f1dSLionel Sambuc }
421*0a6a1f1dSLionel Sambuc }
422*0a6a1f1dSLionel Sambuc return translateCallExpr(cast<CallExpr>(OCE), Ctx);
423*0a6a1f1dSLionel Sambuc }
424*0a6a1f1dSLionel Sambuc
425*0a6a1f1dSLionel Sambuc
translateUnaryOperator(const UnaryOperator * UO,CallingContext * Ctx)426*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::translateUnaryOperator(const UnaryOperator *UO,
427*0a6a1f1dSLionel Sambuc CallingContext *Ctx) {
428*0a6a1f1dSLionel Sambuc switch (UO->getOpcode()) {
429*0a6a1f1dSLionel Sambuc case UO_PostInc:
430*0a6a1f1dSLionel Sambuc case UO_PostDec:
431*0a6a1f1dSLionel Sambuc case UO_PreInc:
432*0a6a1f1dSLionel Sambuc case UO_PreDec:
433*0a6a1f1dSLionel Sambuc return new (Arena) til::Undefined(UO);
434*0a6a1f1dSLionel Sambuc
435*0a6a1f1dSLionel Sambuc case UO_AddrOf: {
436*0a6a1f1dSLionel Sambuc if (CapabilityExprMode) {
437*0a6a1f1dSLionel Sambuc // interpret &Graph::mu_ as an existential.
438*0a6a1f1dSLionel Sambuc if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(UO->getSubExpr())) {
439*0a6a1f1dSLionel Sambuc if (DRE->getDecl()->isCXXInstanceMember()) {
440*0a6a1f1dSLionel Sambuc // This is a pointer-to-member expression, e.g. &MyClass::mu_.
441*0a6a1f1dSLionel Sambuc // We interpret this syntax specially, as a wildcard.
442*0a6a1f1dSLionel Sambuc auto *W = new (Arena) til::Wildcard();
443*0a6a1f1dSLionel Sambuc return new (Arena) til::Project(W, DRE->getDecl());
444*0a6a1f1dSLionel Sambuc }
445*0a6a1f1dSLionel Sambuc }
446*0a6a1f1dSLionel Sambuc }
447*0a6a1f1dSLionel Sambuc // otherwise, & is a no-op
448*0a6a1f1dSLionel Sambuc return translate(UO->getSubExpr(), Ctx);
449*0a6a1f1dSLionel Sambuc }
450*0a6a1f1dSLionel Sambuc
451*0a6a1f1dSLionel Sambuc // We treat these as no-ops
452*0a6a1f1dSLionel Sambuc case UO_Deref:
453*0a6a1f1dSLionel Sambuc case UO_Plus:
454*0a6a1f1dSLionel Sambuc return translate(UO->getSubExpr(), Ctx);
455*0a6a1f1dSLionel Sambuc
456*0a6a1f1dSLionel Sambuc case UO_Minus:
457*0a6a1f1dSLionel Sambuc return new (Arena)
458*0a6a1f1dSLionel Sambuc til::UnaryOp(til::UOP_Minus, translate(UO->getSubExpr(), Ctx));
459*0a6a1f1dSLionel Sambuc case UO_Not:
460*0a6a1f1dSLionel Sambuc return new (Arena)
461*0a6a1f1dSLionel Sambuc til::UnaryOp(til::UOP_BitNot, translate(UO->getSubExpr(), Ctx));
462*0a6a1f1dSLionel Sambuc case UO_LNot:
463*0a6a1f1dSLionel Sambuc return new (Arena)
464*0a6a1f1dSLionel Sambuc til::UnaryOp(til::UOP_LogicNot, translate(UO->getSubExpr(), Ctx));
465*0a6a1f1dSLionel Sambuc
466*0a6a1f1dSLionel Sambuc // Currently unsupported
467*0a6a1f1dSLionel Sambuc case UO_Real:
468*0a6a1f1dSLionel Sambuc case UO_Imag:
469*0a6a1f1dSLionel Sambuc case UO_Extension:
470*0a6a1f1dSLionel Sambuc return new (Arena) til::Undefined(UO);
471*0a6a1f1dSLionel Sambuc }
472*0a6a1f1dSLionel Sambuc return new (Arena) til::Undefined(UO);
473*0a6a1f1dSLionel Sambuc }
474*0a6a1f1dSLionel Sambuc
475*0a6a1f1dSLionel Sambuc
translateBinOp(til::TIL_BinaryOpcode Op,const BinaryOperator * BO,CallingContext * Ctx,bool Reverse)476*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::translateBinOp(til::TIL_BinaryOpcode Op,
477*0a6a1f1dSLionel Sambuc const BinaryOperator *BO,
478*0a6a1f1dSLionel Sambuc CallingContext *Ctx, bool Reverse) {
479*0a6a1f1dSLionel Sambuc til::SExpr *E0 = translate(BO->getLHS(), Ctx);
480*0a6a1f1dSLionel Sambuc til::SExpr *E1 = translate(BO->getRHS(), Ctx);
481*0a6a1f1dSLionel Sambuc if (Reverse)
482*0a6a1f1dSLionel Sambuc return new (Arena) til::BinaryOp(Op, E1, E0);
483*0a6a1f1dSLionel Sambuc else
484*0a6a1f1dSLionel Sambuc return new (Arena) til::BinaryOp(Op, E0, E1);
485*0a6a1f1dSLionel Sambuc }
486*0a6a1f1dSLionel Sambuc
487*0a6a1f1dSLionel Sambuc
translateBinAssign(til::TIL_BinaryOpcode Op,const BinaryOperator * BO,CallingContext * Ctx,bool Assign)488*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::translateBinAssign(til::TIL_BinaryOpcode Op,
489*0a6a1f1dSLionel Sambuc const BinaryOperator *BO,
490*0a6a1f1dSLionel Sambuc CallingContext *Ctx,
491*0a6a1f1dSLionel Sambuc bool Assign) {
492*0a6a1f1dSLionel Sambuc const Expr *LHS = BO->getLHS();
493*0a6a1f1dSLionel Sambuc const Expr *RHS = BO->getRHS();
494*0a6a1f1dSLionel Sambuc til::SExpr *E0 = translate(LHS, Ctx);
495*0a6a1f1dSLionel Sambuc til::SExpr *E1 = translate(RHS, Ctx);
496*0a6a1f1dSLionel Sambuc
497*0a6a1f1dSLionel Sambuc const ValueDecl *VD = nullptr;
498*0a6a1f1dSLionel Sambuc til::SExpr *CV = nullptr;
499*0a6a1f1dSLionel Sambuc if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LHS)) {
500*0a6a1f1dSLionel Sambuc VD = DRE->getDecl();
501*0a6a1f1dSLionel Sambuc CV = lookupVarDecl(VD);
502*0a6a1f1dSLionel Sambuc }
503*0a6a1f1dSLionel Sambuc
504*0a6a1f1dSLionel Sambuc if (!Assign) {
505*0a6a1f1dSLionel Sambuc til::SExpr *Arg = CV ? CV : new (Arena) til::Load(E0);
506*0a6a1f1dSLionel Sambuc E1 = new (Arena) til::BinaryOp(Op, Arg, E1);
507*0a6a1f1dSLionel Sambuc E1 = addStatement(E1, nullptr, VD);
508*0a6a1f1dSLionel Sambuc }
509*0a6a1f1dSLionel Sambuc if (VD && CV)
510*0a6a1f1dSLionel Sambuc return updateVarDecl(VD, E1);
511*0a6a1f1dSLionel Sambuc return new (Arena) til::Store(E0, E1);
512*0a6a1f1dSLionel Sambuc }
513*0a6a1f1dSLionel Sambuc
514*0a6a1f1dSLionel Sambuc
translateBinaryOperator(const BinaryOperator * BO,CallingContext * Ctx)515*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::translateBinaryOperator(const BinaryOperator *BO,
516*0a6a1f1dSLionel Sambuc CallingContext *Ctx) {
517*0a6a1f1dSLionel Sambuc switch (BO->getOpcode()) {
518*0a6a1f1dSLionel Sambuc case BO_PtrMemD:
519*0a6a1f1dSLionel Sambuc case BO_PtrMemI:
520*0a6a1f1dSLionel Sambuc return new (Arena) til::Undefined(BO);
521*0a6a1f1dSLionel Sambuc
522*0a6a1f1dSLionel Sambuc case BO_Mul: return translateBinOp(til::BOP_Mul, BO, Ctx);
523*0a6a1f1dSLionel Sambuc case BO_Div: return translateBinOp(til::BOP_Div, BO, Ctx);
524*0a6a1f1dSLionel Sambuc case BO_Rem: return translateBinOp(til::BOP_Rem, BO, Ctx);
525*0a6a1f1dSLionel Sambuc case BO_Add: return translateBinOp(til::BOP_Add, BO, Ctx);
526*0a6a1f1dSLionel Sambuc case BO_Sub: return translateBinOp(til::BOP_Sub, BO, Ctx);
527*0a6a1f1dSLionel Sambuc case BO_Shl: return translateBinOp(til::BOP_Shl, BO, Ctx);
528*0a6a1f1dSLionel Sambuc case BO_Shr: return translateBinOp(til::BOP_Shr, BO, Ctx);
529*0a6a1f1dSLionel Sambuc case BO_LT: return translateBinOp(til::BOP_Lt, BO, Ctx);
530*0a6a1f1dSLionel Sambuc case BO_GT: return translateBinOp(til::BOP_Lt, BO, Ctx, true);
531*0a6a1f1dSLionel Sambuc case BO_LE: return translateBinOp(til::BOP_Leq, BO, Ctx);
532*0a6a1f1dSLionel Sambuc case BO_GE: return translateBinOp(til::BOP_Leq, BO, Ctx, true);
533*0a6a1f1dSLionel Sambuc case BO_EQ: return translateBinOp(til::BOP_Eq, BO, Ctx);
534*0a6a1f1dSLionel Sambuc case BO_NE: return translateBinOp(til::BOP_Neq, BO, Ctx);
535*0a6a1f1dSLionel Sambuc case BO_And: return translateBinOp(til::BOP_BitAnd, BO, Ctx);
536*0a6a1f1dSLionel Sambuc case BO_Xor: return translateBinOp(til::BOP_BitXor, BO, Ctx);
537*0a6a1f1dSLionel Sambuc case BO_Or: return translateBinOp(til::BOP_BitOr, BO, Ctx);
538*0a6a1f1dSLionel Sambuc case BO_LAnd: return translateBinOp(til::BOP_LogicAnd, BO, Ctx);
539*0a6a1f1dSLionel Sambuc case BO_LOr: return translateBinOp(til::BOP_LogicOr, BO, Ctx);
540*0a6a1f1dSLionel Sambuc
541*0a6a1f1dSLionel Sambuc case BO_Assign: return translateBinAssign(til::BOP_Eq, BO, Ctx, true);
542*0a6a1f1dSLionel Sambuc case BO_MulAssign: return translateBinAssign(til::BOP_Mul, BO, Ctx);
543*0a6a1f1dSLionel Sambuc case BO_DivAssign: return translateBinAssign(til::BOP_Div, BO, Ctx);
544*0a6a1f1dSLionel Sambuc case BO_RemAssign: return translateBinAssign(til::BOP_Rem, BO, Ctx);
545*0a6a1f1dSLionel Sambuc case BO_AddAssign: return translateBinAssign(til::BOP_Add, BO, Ctx);
546*0a6a1f1dSLionel Sambuc case BO_SubAssign: return translateBinAssign(til::BOP_Sub, BO, Ctx);
547*0a6a1f1dSLionel Sambuc case BO_ShlAssign: return translateBinAssign(til::BOP_Shl, BO, Ctx);
548*0a6a1f1dSLionel Sambuc case BO_ShrAssign: return translateBinAssign(til::BOP_Shr, BO, Ctx);
549*0a6a1f1dSLionel Sambuc case BO_AndAssign: return translateBinAssign(til::BOP_BitAnd, BO, Ctx);
550*0a6a1f1dSLionel Sambuc case BO_XorAssign: return translateBinAssign(til::BOP_BitXor, BO, Ctx);
551*0a6a1f1dSLionel Sambuc case BO_OrAssign: return translateBinAssign(til::BOP_BitOr, BO, Ctx);
552*0a6a1f1dSLionel Sambuc
553*0a6a1f1dSLionel Sambuc case BO_Comma:
554*0a6a1f1dSLionel Sambuc // The clang CFG should have already processed both sides.
555*0a6a1f1dSLionel Sambuc return translate(BO->getRHS(), Ctx);
556*0a6a1f1dSLionel Sambuc }
557*0a6a1f1dSLionel Sambuc return new (Arena) til::Undefined(BO);
558*0a6a1f1dSLionel Sambuc }
559*0a6a1f1dSLionel Sambuc
560*0a6a1f1dSLionel Sambuc
translateCastExpr(const CastExpr * CE,CallingContext * Ctx)561*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::translateCastExpr(const CastExpr *CE,
562*0a6a1f1dSLionel Sambuc CallingContext *Ctx) {
563*0a6a1f1dSLionel Sambuc clang::CastKind K = CE->getCastKind();
564*0a6a1f1dSLionel Sambuc switch (K) {
565*0a6a1f1dSLionel Sambuc case CK_LValueToRValue: {
566*0a6a1f1dSLionel Sambuc if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
567*0a6a1f1dSLionel Sambuc til::SExpr *E0 = lookupVarDecl(DRE->getDecl());
568*0a6a1f1dSLionel Sambuc if (E0)
569*0a6a1f1dSLionel Sambuc return E0;
570*0a6a1f1dSLionel Sambuc }
571*0a6a1f1dSLionel Sambuc til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
572*0a6a1f1dSLionel Sambuc return E0;
573*0a6a1f1dSLionel Sambuc // FIXME!! -- get Load working properly
574*0a6a1f1dSLionel Sambuc // return new (Arena) til::Load(E0);
575*0a6a1f1dSLionel Sambuc }
576*0a6a1f1dSLionel Sambuc case CK_NoOp:
577*0a6a1f1dSLionel Sambuc case CK_DerivedToBase:
578*0a6a1f1dSLionel Sambuc case CK_UncheckedDerivedToBase:
579*0a6a1f1dSLionel Sambuc case CK_ArrayToPointerDecay:
580*0a6a1f1dSLionel Sambuc case CK_FunctionToPointerDecay: {
581*0a6a1f1dSLionel Sambuc til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
582*0a6a1f1dSLionel Sambuc return E0;
583*0a6a1f1dSLionel Sambuc }
584*0a6a1f1dSLionel Sambuc default: {
585*0a6a1f1dSLionel Sambuc // FIXME: handle different kinds of casts.
586*0a6a1f1dSLionel Sambuc til::SExpr *E0 = translate(CE->getSubExpr(), Ctx);
587*0a6a1f1dSLionel Sambuc if (CapabilityExprMode)
588*0a6a1f1dSLionel Sambuc return E0;
589*0a6a1f1dSLionel Sambuc return new (Arena) til::Cast(til::CAST_none, E0);
590*0a6a1f1dSLionel Sambuc }
591*0a6a1f1dSLionel Sambuc }
592*0a6a1f1dSLionel Sambuc }
593*0a6a1f1dSLionel Sambuc
594*0a6a1f1dSLionel Sambuc
595*0a6a1f1dSLionel Sambuc til::SExpr *
translateArraySubscriptExpr(const ArraySubscriptExpr * E,CallingContext * Ctx)596*0a6a1f1dSLionel Sambuc SExprBuilder::translateArraySubscriptExpr(const ArraySubscriptExpr *E,
597*0a6a1f1dSLionel Sambuc CallingContext *Ctx) {
598*0a6a1f1dSLionel Sambuc til::SExpr *E0 = translate(E->getBase(), Ctx);
599*0a6a1f1dSLionel Sambuc til::SExpr *E1 = translate(E->getIdx(), Ctx);
600*0a6a1f1dSLionel Sambuc return new (Arena) til::ArrayIndex(E0, E1);
601*0a6a1f1dSLionel Sambuc }
602*0a6a1f1dSLionel Sambuc
603*0a6a1f1dSLionel Sambuc
604*0a6a1f1dSLionel Sambuc til::SExpr *
translateAbstractConditionalOperator(const AbstractConditionalOperator * CO,CallingContext * Ctx)605*0a6a1f1dSLionel Sambuc SExprBuilder::translateAbstractConditionalOperator(
606*0a6a1f1dSLionel Sambuc const AbstractConditionalOperator *CO, CallingContext *Ctx) {
607*0a6a1f1dSLionel Sambuc auto *C = translate(CO->getCond(), Ctx);
608*0a6a1f1dSLionel Sambuc auto *T = translate(CO->getTrueExpr(), Ctx);
609*0a6a1f1dSLionel Sambuc auto *E = translate(CO->getFalseExpr(), Ctx);
610*0a6a1f1dSLionel Sambuc return new (Arena) til::IfThenElse(C, T, E);
611*0a6a1f1dSLionel Sambuc }
612*0a6a1f1dSLionel Sambuc
613*0a6a1f1dSLionel Sambuc
614*0a6a1f1dSLionel Sambuc til::SExpr *
translateDeclStmt(const DeclStmt * S,CallingContext * Ctx)615*0a6a1f1dSLionel Sambuc SExprBuilder::translateDeclStmt(const DeclStmt *S, CallingContext *Ctx) {
616*0a6a1f1dSLionel Sambuc DeclGroupRef DGrp = S->getDeclGroup();
617*0a6a1f1dSLionel Sambuc for (DeclGroupRef::iterator I = DGrp.begin(), E = DGrp.end(); I != E; ++I) {
618*0a6a1f1dSLionel Sambuc if (VarDecl *VD = dyn_cast_or_null<VarDecl>(*I)) {
619*0a6a1f1dSLionel Sambuc Expr *E = VD->getInit();
620*0a6a1f1dSLionel Sambuc til::SExpr* SE = translate(E, Ctx);
621*0a6a1f1dSLionel Sambuc
622*0a6a1f1dSLionel Sambuc // Add local variables with trivial type to the variable map
623*0a6a1f1dSLionel Sambuc QualType T = VD->getType();
624*0a6a1f1dSLionel Sambuc if (T.isTrivialType(VD->getASTContext())) {
625*0a6a1f1dSLionel Sambuc return addVarDecl(VD, SE);
626*0a6a1f1dSLionel Sambuc }
627*0a6a1f1dSLionel Sambuc else {
628*0a6a1f1dSLionel Sambuc // TODO: add alloca
629*0a6a1f1dSLionel Sambuc }
630*0a6a1f1dSLionel Sambuc }
631*0a6a1f1dSLionel Sambuc }
632*0a6a1f1dSLionel Sambuc return nullptr;
633*0a6a1f1dSLionel Sambuc }
634*0a6a1f1dSLionel Sambuc
635*0a6a1f1dSLionel Sambuc
636*0a6a1f1dSLionel Sambuc
637*0a6a1f1dSLionel Sambuc // If (E) is non-trivial, then add it to the current basic block, and
638*0a6a1f1dSLionel Sambuc // update the statement map so that S refers to E. Returns a new variable
639*0a6a1f1dSLionel Sambuc // that refers to E.
640*0a6a1f1dSLionel Sambuc // If E is trivial returns E.
addStatement(til::SExpr * E,const Stmt * S,const ValueDecl * VD)641*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::addStatement(til::SExpr* E, const Stmt *S,
642*0a6a1f1dSLionel Sambuc const ValueDecl *VD) {
643*0a6a1f1dSLionel Sambuc if (!E || !CurrentBB || E->block() || til::ThreadSafetyTIL::isTrivial(E))
644*0a6a1f1dSLionel Sambuc return E;
645*0a6a1f1dSLionel Sambuc if (VD)
646*0a6a1f1dSLionel Sambuc E = new (Arena) til::Variable(E, VD);
647*0a6a1f1dSLionel Sambuc CurrentInstructions.push_back(E);
648*0a6a1f1dSLionel Sambuc if (S)
649*0a6a1f1dSLionel Sambuc insertStmt(S, E);
650*0a6a1f1dSLionel Sambuc return E;
651*0a6a1f1dSLionel Sambuc }
652*0a6a1f1dSLionel Sambuc
653*0a6a1f1dSLionel Sambuc
654*0a6a1f1dSLionel Sambuc // Returns the current value of VD, if known, and nullptr otherwise.
lookupVarDecl(const ValueDecl * VD)655*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::lookupVarDecl(const ValueDecl *VD) {
656*0a6a1f1dSLionel Sambuc auto It = LVarIdxMap.find(VD);
657*0a6a1f1dSLionel Sambuc if (It != LVarIdxMap.end()) {
658*0a6a1f1dSLionel Sambuc assert(CurrentLVarMap[It->second].first == VD);
659*0a6a1f1dSLionel Sambuc return CurrentLVarMap[It->second].second;
660*0a6a1f1dSLionel Sambuc }
661*0a6a1f1dSLionel Sambuc return nullptr;
662*0a6a1f1dSLionel Sambuc }
663*0a6a1f1dSLionel Sambuc
664*0a6a1f1dSLionel Sambuc
665*0a6a1f1dSLionel Sambuc // if E is a til::Variable, update its clangDecl.
maybeUpdateVD(til::SExpr * E,const ValueDecl * VD)666*0a6a1f1dSLionel Sambuc inline void maybeUpdateVD(til::SExpr *E, const ValueDecl *VD) {
667*0a6a1f1dSLionel Sambuc if (!E)
668*0a6a1f1dSLionel Sambuc return;
669*0a6a1f1dSLionel Sambuc if (til::Variable *V = dyn_cast<til::Variable>(E)) {
670*0a6a1f1dSLionel Sambuc if (!V->clangDecl())
671*0a6a1f1dSLionel Sambuc V->setClangDecl(VD);
672*0a6a1f1dSLionel Sambuc }
673*0a6a1f1dSLionel Sambuc }
674*0a6a1f1dSLionel Sambuc
675*0a6a1f1dSLionel Sambuc // Adds a new variable declaration.
addVarDecl(const ValueDecl * VD,til::SExpr * E)676*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::addVarDecl(const ValueDecl *VD, til::SExpr *E) {
677*0a6a1f1dSLionel Sambuc maybeUpdateVD(E, VD);
678*0a6a1f1dSLionel Sambuc LVarIdxMap.insert(std::make_pair(VD, CurrentLVarMap.size()));
679*0a6a1f1dSLionel Sambuc CurrentLVarMap.makeWritable();
680*0a6a1f1dSLionel Sambuc CurrentLVarMap.push_back(std::make_pair(VD, E));
681*0a6a1f1dSLionel Sambuc return E;
682*0a6a1f1dSLionel Sambuc }
683*0a6a1f1dSLionel Sambuc
684*0a6a1f1dSLionel Sambuc
685*0a6a1f1dSLionel Sambuc // Updates a current variable declaration. (E.g. by assignment)
updateVarDecl(const ValueDecl * VD,til::SExpr * E)686*0a6a1f1dSLionel Sambuc til::SExpr *SExprBuilder::updateVarDecl(const ValueDecl *VD, til::SExpr *E) {
687*0a6a1f1dSLionel Sambuc maybeUpdateVD(E, VD);
688*0a6a1f1dSLionel Sambuc auto It = LVarIdxMap.find(VD);
689*0a6a1f1dSLionel Sambuc if (It == LVarIdxMap.end()) {
690*0a6a1f1dSLionel Sambuc til::SExpr *Ptr = new (Arena) til::LiteralPtr(VD);
691*0a6a1f1dSLionel Sambuc til::SExpr *St = new (Arena) til::Store(Ptr, E);
692*0a6a1f1dSLionel Sambuc return St;
693*0a6a1f1dSLionel Sambuc }
694*0a6a1f1dSLionel Sambuc CurrentLVarMap.makeWritable();
695*0a6a1f1dSLionel Sambuc CurrentLVarMap.elem(It->second).second = E;
696*0a6a1f1dSLionel Sambuc return E;
697*0a6a1f1dSLionel Sambuc }
698*0a6a1f1dSLionel Sambuc
699*0a6a1f1dSLionel Sambuc
700*0a6a1f1dSLionel Sambuc // Make a Phi node in the current block for the i^th variable in CurrentVarMap.
701*0a6a1f1dSLionel Sambuc // If E != null, sets Phi[CurrentBlockInfo->ArgIndex] = E.
702*0a6a1f1dSLionel Sambuc // If E == null, this is a backedge and will be set later.
makePhiNodeVar(unsigned i,unsigned NPreds,til::SExpr * E)703*0a6a1f1dSLionel Sambuc void SExprBuilder::makePhiNodeVar(unsigned i, unsigned NPreds, til::SExpr *E) {
704*0a6a1f1dSLionel Sambuc unsigned ArgIndex = CurrentBlockInfo->ProcessedPredecessors;
705*0a6a1f1dSLionel Sambuc assert(ArgIndex > 0 && ArgIndex < NPreds);
706*0a6a1f1dSLionel Sambuc
707*0a6a1f1dSLionel Sambuc til::SExpr *CurrE = CurrentLVarMap[i].second;
708*0a6a1f1dSLionel Sambuc if (CurrE->block() == CurrentBB) {
709*0a6a1f1dSLionel Sambuc // We already have a Phi node in the current block,
710*0a6a1f1dSLionel Sambuc // so just add the new variable to the Phi node.
711*0a6a1f1dSLionel Sambuc til::Phi *Ph = dyn_cast<til::Phi>(CurrE);
712*0a6a1f1dSLionel Sambuc assert(Ph && "Expecting Phi node.");
713*0a6a1f1dSLionel Sambuc if (E)
714*0a6a1f1dSLionel Sambuc Ph->values()[ArgIndex] = E;
715*0a6a1f1dSLionel Sambuc return;
716*0a6a1f1dSLionel Sambuc }
717*0a6a1f1dSLionel Sambuc
718*0a6a1f1dSLionel Sambuc // Make a new phi node: phi(..., E)
719*0a6a1f1dSLionel Sambuc // All phi args up to the current index are set to the current value.
720*0a6a1f1dSLionel Sambuc til::Phi *Ph = new (Arena) til::Phi(Arena, NPreds);
721*0a6a1f1dSLionel Sambuc Ph->values().setValues(NPreds, nullptr);
722*0a6a1f1dSLionel Sambuc for (unsigned PIdx = 0; PIdx < ArgIndex; ++PIdx)
723*0a6a1f1dSLionel Sambuc Ph->values()[PIdx] = CurrE;
724*0a6a1f1dSLionel Sambuc if (E)
725*0a6a1f1dSLionel Sambuc Ph->values()[ArgIndex] = E;
726*0a6a1f1dSLionel Sambuc Ph->setClangDecl(CurrentLVarMap[i].first);
727*0a6a1f1dSLionel Sambuc // If E is from a back-edge, or either E or CurrE are incomplete, then
728*0a6a1f1dSLionel Sambuc // mark this node as incomplete; we may need to remove it later.
729*0a6a1f1dSLionel Sambuc if (!E || isIncompletePhi(E) || isIncompletePhi(CurrE)) {
730*0a6a1f1dSLionel Sambuc Ph->setStatus(til::Phi::PH_Incomplete);
731*0a6a1f1dSLionel Sambuc }
732*0a6a1f1dSLionel Sambuc
733*0a6a1f1dSLionel Sambuc // Add Phi node to current block, and update CurrentLVarMap[i]
734*0a6a1f1dSLionel Sambuc CurrentArguments.push_back(Ph);
735*0a6a1f1dSLionel Sambuc if (Ph->status() == til::Phi::PH_Incomplete)
736*0a6a1f1dSLionel Sambuc IncompleteArgs.push_back(Ph);
737*0a6a1f1dSLionel Sambuc
738*0a6a1f1dSLionel Sambuc CurrentLVarMap.makeWritable();
739*0a6a1f1dSLionel Sambuc CurrentLVarMap.elem(i).second = Ph;
740*0a6a1f1dSLionel Sambuc }
741*0a6a1f1dSLionel Sambuc
742*0a6a1f1dSLionel Sambuc
743*0a6a1f1dSLionel Sambuc // Merge values from Map into the current variable map.
744*0a6a1f1dSLionel Sambuc // This will construct Phi nodes in the current basic block as necessary.
mergeEntryMap(LVarDefinitionMap Map)745*0a6a1f1dSLionel Sambuc void SExprBuilder::mergeEntryMap(LVarDefinitionMap Map) {
746*0a6a1f1dSLionel Sambuc assert(CurrentBlockInfo && "Not processing a block!");
747*0a6a1f1dSLionel Sambuc
748*0a6a1f1dSLionel Sambuc if (!CurrentLVarMap.valid()) {
749*0a6a1f1dSLionel Sambuc // Steal Map, using copy-on-write.
750*0a6a1f1dSLionel Sambuc CurrentLVarMap = std::move(Map);
751*0a6a1f1dSLionel Sambuc return;
752*0a6a1f1dSLionel Sambuc }
753*0a6a1f1dSLionel Sambuc if (CurrentLVarMap.sameAs(Map))
754*0a6a1f1dSLionel Sambuc return; // Easy merge: maps from different predecessors are unchanged.
755*0a6a1f1dSLionel Sambuc
756*0a6a1f1dSLionel Sambuc unsigned NPreds = CurrentBB->numPredecessors();
757*0a6a1f1dSLionel Sambuc unsigned ESz = CurrentLVarMap.size();
758*0a6a1f1dSLionel Sambuc unsigned MSz = Map.size();
759*0a6a1f1dSLionel Sambuc unsigned Sz = std::min(ESz, MSz);
760*0a6a1f1dSLionel Sambuc
761*0a6a1f1dSLionel Sambuc for (unsigned i=0; i<Sz; ++i) {
762*0a6a1f1dSLionel Sambuc if (CurrentLVarMap[i].first != Map[i].first) {
763*0a6a1f1dSLionel Sambuc // We've reached the end of variables in common.
764*0a6a1f1dSLionel Sambuc CurrentLVarMap.makeWritable();
765*0a6a1f1dSLionel Sambuc CurrentLVarMap.downsize(i);
766*0a6a1f1dSLionel Sambuc break;
767*0a6a1f1dSLionel Sambuc }
768*0a6a1f1dSLionel Sambuc if (CurrentLVarMap[i].second != Map[i].second)
769*0a6a1f1dSLionel Sambuc makePhiNodeVar(i, NPreds, Map[i].second);
770*0a6a1f1dSLionel Sambuc }
771*0a6a1f1dSLionel Sambuc if (ESz > MSz) {
772*0a6a1f1dSLionel Sambuc CurrentLVarMap.makeWritable();
773*0a6a1f1dSLionel Sambuc CurrentLVarMap.downsize(Map.size());
774*0a6a1f1dSLionel Sambuc }
775*0a6a1f1dSLionel Sambuc }
776*0a6a1f1dSLionel Sambuc
777*0a6a1f1dSLionel Sambuc
778*0a6a1f1dSLionel Sambuc // Merge a back edge into the current variable map.
779*0a6a1f1dSLionel Sambuc // This will create phi nodes for all variables in the variable map.
mergeEntryMapBackEdge()780*0a6a1f1dSLionel Sambuc void SExprBuilder::mergeEntryMapBackEdge() {
781*0a6a1f1dSLionel Sambuc // We don't have definitions for variables on the backedge, because we
782*0a6a1f1dSLionel Sambuc // haven't gotten that far in the CFG. Thus, when encountering a back edge,
783*0a6a1f1dSLionel Sambuc // we conservatively create Phi nodes for all variables. Unnecessary Phi
784*0a6a1f1dSLionel Sambuc // nodes will be marked as incomplete, and stripped out at the end.
785*0a6a1f1dSLionel Sambuc //
786*0a6a1f1dSLionel Sambuc // An Phi node is unnecessary if it only refers to itself and one other
787*0a6a1f1dSLionel Sambuc // variable, e.g. x = Phi(y, y, x) can be reduced to x = y.
788*0a6a1f1dSLionel Sambuc
789*0a6a1f1dSLionel Sambuc assert(CurrentBlockInfo && "Not processing a block!");
790*0a6a1f1dSLionel Sambuc
791*0a6a1f1dSLionel Sambuc if (CurrentBlockInfo->HasBackEdges)
792*0a6a1f1dSLionel Sambuc return;
793*0a6a1f1dSLionel Sambuc CurrentBlockInfo->HasBackEdges = true;
794*0a6a1f1dSLionel Sambuc
795*0a6a1f1dSLionel Sambuc CurrentLVarMap.makeWritable();
796*0a6a1f1dSLionel Sambuc unsigned Sz = CurrentLVarMap.size();
797*0a6a1f1dSLionel Sambuc unsigned NPreds = CurrentBB->numPredecessors();
798*0a6a1f1dSLionel Sambuc
799*0a6a1f1dSLionel Sambuc for (unsigned i=0; i < Sz; ++i) {
800*0a6a1f1dSLionel Sambuc makePhiNodeVar(i, NPreds, nullptr);
801*0a6a1f1dSLionel Sambuc }
802*0a6a1f1dSLionel Sambuc }
803*0a6a1f1dSLionel Sambuc
804*0a6a1f1dSLionel Sambuc
805*0a6a1f1dSLionel Sambuc // Update the phi nodes that were initially created for a back edge
806*0a6a1f1dSLionel Sambuc // once the variable definitions have been computed.
807*0a6a1f1dSLionel Sambuc // I.e., merge the current variable map into the phi nodes for Blk.
mergePhiNodesBackEdge(const CFGBlock * Blk)808*0a6a1f1dSLionel Sambuc void SExprBuilder::mergePhiNodesBackEdge(const CFGBlock *Blk) {
809*0a6a1f1dSLionel Sambuc til::BasicBlock *BB = lookupBlock(Blk);
810*0a6a1f1dSLionel Sambuc unsigned ArgIndex = BBInfo[Blk->getBlockID()].ProcessedPredecessors;
811*0a6a1f1dSLionel Sambuc assert(ArgIndex > 0 && ArgIndex < BB->numPredecessors());
812*0a6a1f1dSLionel Sambuc
813*0a6a1f1dSLionel Sambuc for (til::SExpr *PE : BB->arguments()) {
814*0a6a1f1dSLionel Sambuc til::Phi *Ph = dyn_cast_or_null<til::Phi>(PE);
815*0a6a1f1dSLionel Sambuc assert(Ph && "Expecting Phi Node.");
816*0a6a1f1dSLionel Sambuc assert(Ph->values()[ArgIndex] == nullptr && "Wrong index for back edge.");
817*0a6a1f1dSLionel Sambuc
818*0a6a1f1dSLionel Sambuc til::SExpr *E = lookupVarDecl(Ph->clangDecl());
819*0a6a1f1dSLionel Sambuc assert(E && "Couldn't find local variable for Phi node.");
820*0a6a1f1dSLionel Sambuc Ph->values()[ArgIndex] = E;
821*0a6a1f1dSLionel Sambuc }
822*0a6a1f1dSLionel Sambuc }
823*0a6a1f1dSLionel Sambuc
enterCFG(CFG * Cfg,const NamedDecl * D,const CFGBlock * First)824*0a6a1f1dSLionel Sambuc void SExprBuilder::enterCFG(CFG *Cfg, const NamedDecl *D,
825*0a6a1f1dSLionel Sambuc const CFGBlock *First) {
826*0a6a1f1dSLionel Sambuc // Perform initial setup operations.
827*0a6a1f1dSLionel Sambuc unsigned NBlocks = Cfg->getNumBlockIDs();
828*0a6a1f1dSLionel Sambuc Scfg = new (Arena) til::SCFG(Arena, NBlocks);
829*0a6a1f1dSLionel Sambuc
830*0a6a1f1dSLionel Sambuc // allocate all basic blocks immediately, to handle forward references.
831*0a6a1f1dSLionel Sambuc BBInfo.resize(NBlocks);
832*0a6a1f1dSLionel Sambuc BlockMap.resize(NBlocks, nullptr);
833*0a6a1f1dSLionel Sambuc // create map from clang blockID to til::BasicBlocks
834*0a6a1f1dSLionel Sambuc for (auto *B : *Cfg) {
835*0a6a1f1dSLionel Sambuc auto *BB = new (Arena) til::BasicBlock(Arena);
836*0a6a1f1dSLionel Sambuc BB->reserveInstructions(B->size());
837*0a6a1f1dSLionel Sambuc BlockMap[B->getBlockID()] = BB;
838*0a6a1f1dSLionel Sambuc }
839*0a6a1f1dSLionel Sambuc
840*0a6a1f1dSLionel Sambuc CurrentBB = lookupBlock(&Cfg->getEntry());
841*0a6a1f1dSLionel Sambuc auto Parms = isa<ObjCMethodDecl>(D) ? cast<ObjCMethodDecl>(D)->parameters()
842*0a6a1f1dSLionel Sambuc : cast<FunctionDecl>(D)->parameters();
843*0a6a1f1dSLionel Sambuc for (auto *Pm : Parms) {
844*0a6a1f1dSLionel Sambuc QualType T = Pm->getType();
845*0a6a1f1dSLionel Sambuc if (!T.isTrivialType(Pm->getASTContext()))
846*0a6a1f1dSLionel Sambuc continue;
847*0a6a1f1dSLionel Sambuc
848*0a6a1f1dSLionel Sambuc // Add parameters to local variable map.
849*0a6a1f1dSLionel Sambuc // FIXME: right now we emulate params with loads; that should be fixed.
850*0a6a1f1dSLionel Sambuc til::SExpr *Lp = new (Arena) til::LiteralPtr(Pm);
851*0a6a1f1dSLionel Sambuc til::SExpr *Ld = new (Arena) til::Load(Lp);
852*0a6a1f1dSLionel Sambuc til::SExpr *V = addStatement(Ld, nullptr, Pm);
853*0a6a1f1dSLionel Sambuc addVarDecl(Pm, V);
854*0a6a1f1dSLionel Sambuc }
855*0a6a1f1dSLionel Sambuc }
856*0a6a1f1dSLionel Sambuc
857*0a6a1f1dSLionel Sambuc
enterCFGBlock(const CFGBlock * B)858*0a6a1f1dSLionel Sambuc void SExprBuilder::enterCFGBlock(const CFGBlock *B) {
859*0a6a1f1dSLionel Sambuc // Intialize TIL basic block and add it to the CFG.
860*0a6a1f1dSLionel Sambuc CurrentBB = lookupBlock(B);
861*0a6a1f1dSLionel Sambuc CurrentBB->reservePredecessors(B->pred_size());
862*0a6a1f1dSLionel Sambuc Scfg->add(CurrentBB);
863*0a6a1f1dSLionel Sambuc
864*0a6a1f1dSLionel Sambuc CurrentBlockInfo = &BBInfo[B->getBlockID()];
865*0a6a1f1dSLionel Sambuc
866*0a6a1f1dSLionel Sambuc // CurrentLVarMap is moved to ExitMap on block exit.
867*0a6a1f1dSLionel Sambuc // FIXME: the entry block will hold function parameters.
868*0a6a1f1dSLionel Sambuc // assert(!CurrentLVarMap.valid() && "CurrentLVarMap already initialized.");
869*0a6a1f1dSLionel Sambuc }
870*0a6a1f1dSLionel Sambuc
871*0a6a1f1dSLionel Sambuc
handlePredecessor(const CFGBlock * Pred)872*0a6a1f1dSLionel Sambuc void SExprBuilder::handlePredecessor(const CFGBlock *Pred) {
873*0a6a1f1dSLionel Sambuc // Compute CurrentLVarMap on entry from ExitMaps of predecessors
874*0a6a1f1dSLionel Sambuc
875*0a6a1f1dSLionel Sambuc CurrentBB->addPredecessor(BlockMap[Pred->getBlockID()]);
876*0a6a1f1dSLionel Sambuc BlockInfo *PredInfo = &BBInfo[Pred->getBlockID()];
877*0a6a1f1dSLionel Sambuc assert(PredInfo->UnprocessedSuccessors > 0);
878*0a6a1f1dSLionel Sambuc
879*0a6a1f1dSLionel Sambuc if (--PredInfo->UnprocessedSuccessors == 0)
880*0a6a1f1dSLionel Sambuc mergeEntryMap(std::move(PredInfo->ExitMap));
881*0a6a1f1dSLionel Sambuc else
882*0a6a1f1dSLionel Sambuc mergeEntryMap(PredInfo->ExitMap.clone());
883*0a6a1f1dSLionel Sambuc
884*0a6a1f1dSLionel Sambuc ++CurrentBlockInfo->ProcessedPredecessors;
885*0a6a1f1dSLionel Sambuc }
886*0a6a1f1dSLionel Sambuc
887*0a6a1f1dSLionel Sambuc
handlePredecessorBackEdge(const CFGBlock * Pred)888*0a6a1f1dSLionel Sambuc void SExprBuilder::handlePredecessorBackEdge(const CFGBlock *Pred) {
889*0a6a1f1dSLionel Sambuc mergeEntryMapBackEdge();
890*0a6a1f1dSLionel Sambuc }
891*0a6a1f1dSLionel Sambuc
892*0a6a1f1dSLionel Sambuc
enterCFGBlockBody(const CFGBlock * B)893*0a6a1f1dSLionel Sambuc void SExprBuilder::enterCFGBlockBody(const CFGBlock *B) {
894*0a6a1f1dSLionel Sambuc // The merge*() methods have created arguments.
895*0a6a1f1dSLionel Sambuc // Push those arguments onto the basic block.
896*0a6a1f1dSLionel Sambuc CurrentBB->arguments().reserve(
897*0a6a1f1dSLionel Sambuc static_cast<unsigned>(CurrentArguments.size()), Arena);
898*0a6a1f1dSLionel Sambuc for (auto *A : CurrentArguments)
899*0a6a1f1dSLionel Sambuc CurrentBB->addArgument(A);
900*0a6a1f1dSLionel Sambuc }
901*0a6a1f1dSLionel Sambuc
902*0a6a1f1dSLionel Sambuc
handleStatement(const Stmt * S)903*0a6a1f1dSLionel Sambuc void SExprBuilder::handleStatement(const Stmt *S) {
904*0a6a1f1dSLionel Sambuc til::SExpr *E = translate(S, nullptr);
905*0a6a1f1dSLionel Sambuc addStatement(E, S);
906*0a6a1f1dSLionel Sambuc }
907*0a6a1f1dSLionel Sambuc
908*0a6a1f1dSLionel Sambuc
handleDestructorCall(const VarDecl * VD,const CXXDestructorDecl * DD)909*0a6a1f1dSLionel Sambuc void SExprBuilder::handleDestructorCall(const VarDecl *VD,
910*0a6a1f1dSLionel Sambuc const CXXDestructorDecl *DD) {
911*0a6a1f1dSLionel Sambuc til::SExpr *Sf = new (Arena) til::LiteralPtr(VD);
912*0a6a1f1dSLionel Sambuc til::SExpr *Dr = new (Arena) til::LiteralPtr(DD);
913*0a6a1f1dSLionel Sambuc til::SExpr *Ap = new (Arena) til::Apply(Dr, Sf);
914*0a6a1f1dSLionel Sambuc til::SExpr *E = new (Arena) til::Call(Ap);
915*0a6a1f1dSLionel Sambuc addStatement(E, nullptr);
916*0a6a1f1dSLionel Sambuc }
917*0a6a1f1dSLionel Sambuc
918*0a6a1f1dSLionel Sambuc
919*0a6a1f1dSLionel Sambuc
exitCFGBlockBody(const CFGBlock * B)920*0a6a1f1dSLionel Sambuc void SExprBuilder::exitCFGBlockBody(const CFGBlock *B) {
921*0a6a1f1dSLionel Sambuc CurrentBB->instructions().reserve(
922*0a6a1f1dSLionel Sambuc static_cast<unsigned>(CurrentInstructions.size()), Arena);
923*0a6a1f1dSLionel Sambuc for (auto *V : CurrentInstructions)
924*0a6a1f1dSLionel Sambuc CurrentBB->addInstruction(V);
925*0a6a1f1dSLionel Sambuc
926*0a6a1f1dSLionel Sambuc // Create an appropriate terminator
927*0a6a1f1dSLionel Sambuc unsigned N = B->succ_size();
928*0a6a1f1dSLionel Sambuc auto It = B->succ_begin();
929*0a6a1f1dSLionel Sambuc if (N == 1) {
930*0a6a1f1dSLionel Sambuc til::BasicBlock *BB = *It ? lookupBlock(*It) : nullptr;
931*0a6a1f1dSLionel Sambuc // TODO: set index
932*0a6a1f1dSLionel Sambuc unsigned Idx = BB ? BB->findPredecessorIndex(CurrentBB) : 0;
933*0a6a1f1dSLionel Sambuc auto *Tm = new (Arena) til::Goto(BB, Idx);
934*0a6a1f1dSLionel Sambuc CurrentBB->setTerminator(Tm);
935*0a6a1f1dSLionel Sambuc }
936*0a6a1f1dSLionel Sambuc else if (N == 2) {
937*0a6a1f1dSLionel Sambuc til::SExpr *C = translate(B->getTerminatorCondition(true), nullptr);
938*0a6a1f1dSLionel Sambuc til::BasicBlock *BB1 = *It ? lookupBlock(*It) : nullptr;
939*0a6a1f1dSLionel Sambuc ++It;
940*0a6a1f1dSLionel Sambuc til::BasicBlock *BB2 = *It ? lookupBlock(*It) : nullptr;
941*0a6a1f1dSLionel Sambuc // FIXME: make sure these arent' critical edges.
942*0a6a1f1dSLionel Sambuc auto *Tm = new (Arena) til::Branch(C, BB1, BB2);
943*0a6a1f1dSLionel Sambuc CurrentBB->setTerminator(Tm);
944*0a6a1f1dSLionel Sambuc }
945*0a6a1f1dSLionel Sambuc }
946*0a6a1f1dSLionel Sambuc
947*0a6a1f1dSLionel Sambuc
handleSuccessor(const CFGBlock * Succ)948*0a6a1f1dSLionel Sambuc void SExprBuilder::handleSuccessor(const CFGBlock *Succ) {
949*0a6a1f1dSLionel Sambuc ++CurrentBlockInfo->UnprocessedSuccessors;
950*0a6a1f1dSLionel Sambuc }
951*0a6a1f1dSLionel Sambuc
952*0a6a1f1dSLionel Sambuc
handleSuccessorBackEdge(const CFGBlock * Succ)953*0a6a1f1dSLionel Sambuc void SExprBuilder::handleSuccessorBackEdge(const CFGBlock *Succ) {
954*0a6a1f1dSLionel Sambuc mergePhiNodesBackEdge(Succ);
955*0a6a1f1dSLionel Sambuc ++BBInfo[Succ->getBlockID()].ProcessedPredecessors;
956*0a6a1f1dSLionel Sambuc }
957*0a6a1f1dSLionel Sambuc
958*0a6a1f1dSLionel Sambuc
exitCFGBlock(const CFGBlock * B)959*0a6a1f1dSLionel Sambuc void SExprBuilder::exitCFGBlock(const CFGBlock *B) {
960*0a6a1f1dSLionel Sambuc CurrentArguments.clear();
961*0a6a1f1dSLionel Sambuc CurrentInstructions.clear();
962*0a6a1f1dSLionel Sambuc CurrentBlockInfo->ExitMap = std::move(CurrentLVarMap);
963*0a6a1f1dSLionel Sambuc CurrentBB = nullptr;
964*0a6a1f1dSLionel Sambuc CurrentBlockInfo = nullptr;
965*0a6a1f1dSLionel Sambuc }
966*0a6a1f1dSLionel Sambuc
967*0a6a1f1dSLionel Sambuc
exitCFG(const CFGBlock * Last)968*0a6a1f1dSLionel Sambuc void SExprBuilder::exitCFG(const CFGBlock *Last) {
969*0a6a1f1dSLionel Sambuc for (auto *Ph : IncompleteArgs) {
970*0a6a1f1dSLionel Sambuc if (Ph->status() == til::Phi::PH_Incomplete)
971*0a6a1f1dSLionel Sambuc simplifyIncompleteArg(Ph);
972*0a6a1f1dSLionel Sambuc }
973*0a6a1f1dSLionel Sambuc
974*0a6a1f1dSLionel Sambuc CurrentArguments.clear();
975*0a6a1f1dSLionel Sambuc CurrentInstructions.clear();
976*0a6a1f1dSLionel Sambuc IncompleteArgs.clear();
977*0a6a1f1dSLionel Sambuc }
978*0a6a1f1dSLionel Sambuc
979*0a6a1f1dSLionel Sambuc
980*0a6a1f1dSLionel Sambuc /*
981*0a6a1f1dSLionel Sambuc void printSCFG(CFGWalker &Walker) {
982*0a6a1f1dSLionel Sambuc llvm::BumpPtrAllocator Bpa;
983*0a6a1f1dSLionel Sambuc til::MemRegionRef Arena(&Bpa);
984*0a6a1f1dSLionel Sambuc SExprBuilder SxBuilder(Arena);
985*0a6a1f1dSLionel Sambuc til::SCFG *Scfg = SxBuilder.buildCFG(Walker);
986*0a6a1f1dSLionel Sambuc TILPrinter::print(Scfg, llvm::errs());
987*0a6a1f1dSLionel Sambuc }
988*0a6a1f1dSLionel Sambuc */
989*0a6a1f1dSLionel Sambuc
990*0a6a1f1dSLionel Sambuc
991*0a6a1f1dSLionel Sambuc } // end namespace threadSafety
992*0a6a1f1dSLionel Sambuc
993*0a6a1f1dSLionel Sambuc } // end namespace clang
994