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 LLVM_CLANG_LIB_CODEGEN_CGCALL_H
16 #define LLVM_CLANG_LIB_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/IR/Value.h"
23 
24 // FIXME: Restructure so we don't have to expose so much stuff.
25 #include "ABIInfo.h"
26 
27 namespace llvm {
28   class AttributeSet;
29   class Function;
30   class Type;
31   class Value;
32 }
33 
34 namespace clang {
35   class ASTContext;
36   class Decl;
37   class FunctionDecl;
38   class ObjCMethodDecl;
39   class VarDecl;
40 
41 namespace CodeGen {
42   typedef SmallVector<llvm::AttributeSet, 8> AttributeListType;
43 
44   struct CallArg {
45     RValue RV;
46     QualType Ty;
47     bool NeedsCopy;
48     CallArg(RValue rv, QualType ty, bool needscopy)
49     : RV(rv), Ty(ty), NeedsCopy(needscopy)
50     { }
51   };
52 
53   /// CallArgList - Type for representing both the value and type of
54   /// arguments in a call.
55   class CallArgList :
56     public SmallVector<CallArg, 16> {
57   public:
58     CallArgList() : StackBase(nullptr) {}
59 
60     struct Writeback {
61       /// The original argument.  Note that the argument l-value
62       /// is potentially null.
63       LValue Source;
64 
65       /// The temporary alloca.
66       Address Temporary;
67 
68       /// A value to "use" after the writeback, or null.
69       llvm::Value *ToUse;
70     };
71 
72     struct CallArgCleanup {
73       EHScopeStack::stable_iterator Cleanup;
74 
75       /// The "is active" insertion point.  This instruction is temporary and
76       /// will be removed after insertion.
77       llvm::Instruction *IsActiveIP;
78     };
79 
80     void add(RValue rvalue, QualType type, bool needscopy = false) {
81       push_back(CallArg(rvalue, type, needscopy));
82     }
83 
84     void addFrom(const CallArgList &other) {
85       insert(end(), other.begin(), other.end());
86       Writebacks.insert(Writebacks.end(),
87                         other.Writebacks.begin(), other.Writebacks.end());
88     }
89 
90     void addWriteback(LValue srcLV, Address temporary,
91                       llvm::Value *toUse) {
92       Writeback writeback = { srcLV, temporary, toUse };
93       Writebacks.push_back(writeback);
94     }
95 
96     bool hasWritebacks() const { return !Writebacks.empty(); }
97 
98     typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
99       writeback_const_range;
100 
101     writeback_const_range writebacks() const {
102       return writeback_const_range(Writebacks.begin(), Writebacks.end());
103     }
104 
105     void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
106                                    llvm::Instruction *IsActiveIP) {
107       CallArgCleanup ArgCleanup;
108       ArgCleanup.Cleanup = Cleanup;
109       ArgCleanup.IsActiveIP = IsActiveIP;
110       CleanupsToDeactivate.push_back(ArgCleanup);
111     }
112 
113     ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
114       return CleanupsToDeactivate;
115     }
116 
117     void allocateArgumentMemory(CodeGenFunction &CGF);
118     llvm::Instruction *getStackBase() const { return StackBase; }
119     void freeArgumentMemory(CodeGenFunction &CGF) const;
120 
121     /// \brief Returns if we're using an inalloca struct to pass arguments in
122     /// memory.
123     bool isUsingInAlloca() const { return StackBase; }
124 
125   private:
126     SmallVector<Writeback, 1> Writebacks;
127 
128     /// Deactivate these cleanups immediately before making the call.  This
129     /// is used to cleanup objects that are owned by the callee once the call
130     /// occurs.
131     SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
132 
133     /// The stacksave call.  It dominates all of the argument evaluation.
134     llvm::CallInst *StackBase;
135 
136     /// The iterator pointing to the stack restore cleanup.  We manually run and
137     /// deactivate this cleanup after the call in the unexceptional case because
138     /// it doesn't run in the normal order.
139     EHScopeStack::stable_iterator StackCleanup;
140   };
141 
142   /// FunctionArgList - Type for representing both the decl and type
143   /// of parameters to a function. The decl must be either a
144   /// ParmVarDecl or ImplicitParamDecl.
145   class FunctionArgList : public SmallVector<const VarDecl*, 16> {
146   };
147 
148   /// ReturnValueSlot - Contains the address where the return value of a
149   /// function can be stored, and whether the address is volatile or not.
150   class ReturnValueSlot {
151     llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value;
152     CharUnits Alignment;
153 
154     // Return value slot flags
155     enum Flags {
156       IS_VOLATILE = 0x1,
157       IS_UNUSED = 0x2,
158     };
159 
160   public:
161     ReturnValueSlot() {}
162     ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false)
163       : Value(Addr.isValid() ? Addr.getPointer() : nullptr,
164               (IsVolatile ? IS_VOLATILE : 0) | (IsUnused ? IS_UNUSED : 0)),
165         Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {}
166 
167     bool isNull() const { return !getValue().isValid(); }
168 
169     bool isVolatile() const { return Value.getInt() & IS_VOLATILE; }
170     Address getValue() const { return Address(Value.getPointer(), Alignment); }
171     bool isUnused() const { return Value.getInt() & IS_UNUSED; }
172   };
173 
174 }  // end namespace CodeGen
175 }  // end namespace clang
176 
177 #endif
178