1 //===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // These classes wrap the information about a call or function
11 // definition used to handle ABI compliancy.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef CLANG_CODEGEN_CGCALL_H
16 #define CLANG_CODEGEN_CGCALL_H
17 
18 #include "CGValue.h"
19 #include "EHScopeStack.h"
20 #include "clang/AST/CanonicalType.h"
21 #include "clang/AST/Type.h"
22 #include "llvm/ADT/FoldingSet.h"
23 #include "llvm/IR/Value.h"
24 
25 // FIXME: Restructure so we don't have to expose so much stuff.
26 #include "ABIInfo.h"
27 
28 namespace llvm {
29   class AttributeSet;
30   class Function;
31   class Type;
32   class Value;
33 }
34 
35 namespace clang {
36   class ASTContext;
37   class Decl;
38   class FunctionDecl;
39   class ObjCMethodDecl;
40   class VarDecl;
41 
42 namespace CodeGen {
43   typedef SmallVector<llvm::AttributeSet, 8> AttributeListType;
44 
45   struct CallArg {
46     RValue RV;
47     QualType Ty;
48     bool NeedsCopy;
49     CallArg(RValue rv, QualType ty, bool needscopy)
50     : RV(rv), Ty(ty), NeedsCopy(needscopy)
51     { }
52   };
53 
54   /// CallArgList - Type for representing both the value and type of
55   /// arguments in a call.
56   class CallArgList :
57     public SmallVector<CallArg, 16> {
58   public:
59     CallArgList() : StackBase(0), StackBaseMem(0) {}
60 
61     struct Writeback {
62       /// The original argument.  Note that the argument l-value
63       /// is potentially null.
64       LValue Source;
65 
66       /// The temporary alloca.
67       llvm::Value *Temporary;
68 
69       /// A value to "use" after the writeback, or null.
70       llvm::Value *ToUse;
71     };
72 
73     struct CallArgCleanup {
74       EHScopeStack::stable_iterator Cleanup;
75 
76       /// The "is active" insertion point.  This instruction is temporary and
77       /// will be removed after insertion.
78       llvm::Instruction *IsActiveIP;
79     };
80 
81     void add(RValue rvalue, QualType type, bool needscopy = false) {
82       push_back(CallArg(rvalue, type, needscopy));
83     }
84 
85     void addFrom(const CallArgList &other) {
86       insert(end(), other.begin(), other.end());
87       Writebacks.insert(Writebacks.end(),
88                         other.Writebacks.begin(), other.Writebacks.end());
89     }
90 
91     void addWriteback(LValue srcLV, llvm::Value *temporary,
92                       llvm::Value *toUse) {
93       Writeback writeback;
94       writeback.Source = srcLV;
95       writeback.Temporary = temporary;
96       writeback.ToUse = toUse;
97       Writebacks.push_back(writeback);
98     }
99 
100     bool hasWritebacks() const { return !Writebacks.empty(); }
101 
102     typedef SmallVectorImpl<Writeback>::const_iterator writeback_iterator;
103     writeback_iterator writeback_begin() const { return Writebacks.begin(); }
104     writeback_iterator writeback_end() const { return Writebacks.end(); }
105 
106     void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
107                                    llvm::Instruction *IsActiveIP) {
108       CallArgCleanup ArgCleanup;
109       ArgCleanup.Cleanup = Cleanup;
110       ArgCleanup.IsActiveIP = IsActiveIP;
111       CleanupsToDeactivate.push_back(ArgCleanup);
112     }
113 
114     ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
115       return CleanupsToDeactivate;
116     }
117 
118     void allocateArgumentMemory(CodeGenFunction &CGF);
119     llvm::Instruction *getStackBase() const { return StackBase; }
120     void freeArgumentMemory(CodeGenFunction &CGF) const;
121 
122     /// \brief Returns if we're using an inalloca struct to pass arguments in
123     /// memory.
124     bool isUsingInAlloca() const { return StackBase; }
125 
126   private:
127     SmallVector<Writeback, 1> Writebacks;
128 
129     /// Deactivate these cleanups immediately before making the call.  This
130     /// is used to cleanup objects that are owned by the callee once the call
131     /// occurs.
132     SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
133 
134     /// The stacksave call.  It dominates all of the argument evaluation.
135     llvm::CallInst *StackBase;
136 
137     /// The alloca holding the stackbase.  We need it to maintain SSA form.
138     llvm::AllocaInst *StackBaseMem;
139 
140     /// The iterator pointing to the stack restore cleanup.  We manually run and
141     /// deactivate this cleanup after the call in the unexceptional case because
142     /// it doesn't run in the normal order.
143     EHScopeStack::stable_iterator StackCleanup;
144   };
145 
146   /// FunctionArgList - Type for representing both the decl and type
147   /// of parameters to a function. The decl must be either a
148   /// ParmVarDecl or ImplicitParamDecl.
149   class FunctionArgList : public SmallVector<const VarDecl*, 16> {
150   };
151 
152   /// ReturnValueSlot - Contains the address where the return value of a
153   /// function can be stored, and whether the address is volatile or not.
154   class ReturnValueSlot {
155     llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
156 
157   public:
158     ReturnValueSlot() {}
159     ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
160       : Value(Value, IsVolatile) {}
161 
162     bool isNull() const { return !getValue(); }
163 
164     bool isVolatile() const { return Value.getInt(); }
165     llvm::Value *getValue() const { return Value.getPointer(); }
166   };
167 
168 }  // end namespace CodeGen
169 }  // end namespace clang
170 
171 #endif
172