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     /// Add all the arguments from another CallArgList to this one. After doing
85     /// this, the old CallArgList retains its list of arguments, but must not
86     /// be used to emit a call.
87     void addFrom(const CallArgList &other) {
88       insert(end(), other.begin(), other.end());
89       Writebacks.insert(Writebacks.end(),
90                         other.Writebacks.begin(), other.Writebacks.end());
91       CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
92                                   other.CleanupsToDeactivate.begin(),
93                                   other.CleanupsToDeactivate.end());
94       assert(!(StackBase && other.StackBase) && "can't merge stackbases");
95       if (!StackBase)
96         StackBase = other.StackBase;
97     }
98 
99     void addWriteback(LValue srcLV, Address temporary,
100                       llvm::Value *toUse) {
101       Writeback writeback = { srcLV, temporary, toUse };
102       Writebacks.push_back(writeback);
103     }
104 
105     bool hasWritebacks() const { return !Writebacks.empty(); }
106 
107     typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
108       writeback_const_range;
109 
110     writeback_const_range writebacks() const {
111       return writeback_const_range(Writebacks.begin(), Writebacks.end());
112     }
113 
114     void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
115                                    llvm::Instruction *IsActiveIP) {
116       CallArgCleanup ArgCleanup;
117       ArgCleanup.Cleanup = Cleanup;
118       ArgCleanup.IsActiveIP = IsActiveIP;
119       CleanupsToDeactivate.push_back(ArgCleanup);
120     }
121 
122     ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
123       return CleanupsToDeactivate;
124     }
125 
126     void allocateArgumentMemory(CodeGenFunction &CGF);
127     llvm::Instruction *getStackBase() const { return StackBase; }
128     void freeArgumentMemory(CodeGenFunction &CGF) const;
129 
130     /// \brief Returns if we're using an inalloca struct to pass arguments in
131     /// memory.
132     bool isUsingInAlloca() const { return StackBase; }
133 
134   private:
135     SmallVector<Writeback, 1> Writebacks;
136 
137     /// Deactivate these cleanups immediately before making the call.  This
138     /// is used to cleanup objects that are owned by the callee once the call
139     /// occurs.
140     SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
141 
142     /// The stacksave call.  It dominates all of the argument evaluation.
143     llvm::CallInst *StackBase;
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 *, 2, unsigned int> Value;
156     CharUnits Alignment;
157 
158     // Return value slot flags
159     enum Flags {
160       IS_VOLATILE = 0x1,
161       IS_UNUSED = 0x2,
162     };
163 
164   public:
165     ReturnValueSlot() {}
166     ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false)
167       : Value(Addr.isValid() ? Addr.getPointer() : nullptr,
168               (IsVolatile ? IS_VOLATILE : 0) | (IsUnused ? IS_UNUSED : 0)),
169         Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {}
170 
171     bool isNull() const { return !getValue().isValid(); }
172 
173     bool isVolatile() const { return Value.getInt() & IS_VOLATILE; }
174     Address getValue() const { return Address(Value.getPointer(), Alignment); }
175     bool isUnused() const { return Value.getInt() & IS_UNUSED; }
176   };
177 
178 }  // end namespace CodeGen
179 }  // end namespace clang
180 
181 #endif
182