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