clang API Documentation

CGCall.h
Go to the documentation of this file.
00001 //===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // These classes wrap the information about a call or function
00011 // definition used to handle ABI compliancy.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_LIB_CODEGEN_CGCALL_H
00016 #define LLVM_CLANG_LIB_CODEGEN_CGCALL_H
00017 
00018 #include "CGValue.h"
00019 #include "EHScopeStack.h"
00020 #include "clang/AST/CanonicalType.h"
00021 #include "clang/AST/Type.h"
00022 #include "llvm/ADT/FoldingSet.h"
00023 #include "llvm/IR/Value.h"
00024 
00025 // FIXME: Restructure so we don't have to expose so much stuff.
00026 #include "ABIInfo.h"
00027 
00028 namespace llvm {
00029   class AttributeSet;
00030   class Function;
00031   class Type;
00032   class Value;
00033 }
00034 
00035 namespace clang {
00036   class ASTContext;
00037   class Decl;
00038   class FunctionDecl;
00039   class ObjCMethodDecl;
00040   class VarDecl;
00041 
00042 namespace CodeGen {
00043   typedef SmallVector<llvm::AttributeSet, 8> AttributeListType;
00044 
00045   struct CallArg {
00046     RValue RV;
00047     QualType Ty;
00048     bool NeedsCopy;
00049     CallArg(RValue rv, QualType ty, bool needscopy)
00050     : RV(rv), Ty(ty), NeedsCopy(needscopy)
00051     { }
00052   };
00053 
00054   /// CallArgList - Type for representing both the value and type of
00055   /// arguments in a call.
00056   class CallArgList :
00057     public SmallVector<CallArg, 16> {
00058   public:
00059     CallArgList() : StackBase(nullptr), StackBaseMem(nullptr) {}
00060 
00061     struct Writeback {
00062       /// The original argument.  Note that the argument l-value
00063       /// is potentially null.
00064       LValue Source;
00065 
00066       /// The temporary alloca.
00067       llvm::Value *Temporary;
00068 
00069       /// A value to "use" after the writeback, or null.
00070       llvm::Value *ToUse;
00071     };
00072 
00073     struct CallArgCleanup {
00074       EHScopeStack::stable_iterator Cleanup;
00075 
00076       /// The "is active" insertion point.  This instruction is temporary and
00077       /// will be removed after insertion.
00078       llvm::Instruction *IsActiveIP;
00079     };
00080 
00081     void add(RValue rvalue, QualType type, bool needscopy = false) {
00082       push_back(CallArg(rvalue, type, needscopy));
00083     }
00084 
00085     void addFrom(const CallArgList &other) {
00086       insert(end(), other.begin(), other.end());
00087       Writebacks.insert(Writebacks.end(),
00088                         other.Writebacks.begin(), other.Writebacks.end());
00089     }
00090 
00091     void addWriteback(LValue srcLV, llvm::Value *temporary,
00092                       llvm::Value *toUse) {
00093       Writeback writeback;
00094       writeback.Source = srcLV;
00095       writeback.Temporary = temporary;
00096       writeback.ToUse = toUse;
00097       Writebacks.push_back(writeback);
00098     }
00099 
00100     bool hasWritebacks() const { return !Writebacks.empty(); }
00101 
00102     typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
00103       writeback_const_range;
00104 
00105     writeback_const_range writebacks() const {
00106       return writeback_const_range(Writebacks.begin(), Writebacks.end());
00107     }
00108 
00109     void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
00110                                    llvm::Instruction *IsActiveIP) {
00111       CallArgCleanup ArgCleanup;
00112       ArgCleanup.Cleanup = Cleanup;
00113       ArgCleanup.IsActiveIP = IsActiveIP;
00114       CleanupsToDeactivate.push_back(ArgCleanup);
00115     }
00116 
00117     ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
00118       return CleanupsToDeactivate;
00119     }
00120 
00121     void allocateArgumentMemory(CodeGenFunction &CGF);
00122     llvm::Instruction *getStackBase() const { return StackBase; }
00123     void freeArgumentMemory(CodeGenFunction &CGF) const;
00124 
00125     /// \brief Returns if we're using an inalloca struct to pass arguments in
00126     /// memory.
00127     bool isUsingInAlloca() const { return StackBase; }
00128 
00129   private:
00130     SmallVector<Writeback, 1> Writebacks;
00131 
00132     /// Deactivate these cleanups immediately before making the call.  This
00133     /// is used to cleanup objects that are owned by the callee once the call
00134     /// occurs.
00135     SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
00136 
00137     /// The stacksave call.  It dominates all of the argument evaluation.
00138     llvm::CallInst *StackBase;
00139 
00140     /// The alloca holding the stackbase.  We need it to maintain SSA form.
00141     llvm::AllocaInst *StackBaseMem;
00142 
00143     /// The iterator pointing to the stack restore cleanup.  We manually run and
00144     /// deactivate this cleanup after the call in the unexceptional case because
00145     /// it doesn't run in the normal order.
00146     EHScopeStack::stable_iterator StackCleanup;
00147   };
00148 
00149   /// FunctionArgList - Type for representing both the decl and type
00150   /// of parameters to a function. The decl must be either a
00151   /// ParmVarDecl or ImplicitParamDecl.
00152   class FunctionArgList : public SmallVector<const VarDecl*, 16> {
00153   };
00154 
00155   /// ReturnValueSlot - Contains the address where the return value of a 
00156   /// function can be stored, and whether the address is volatile or not.
00157   class ReturnValueSlot {
00158     llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
00159 
00160   public:
00161     ReturnValueSlot() {}
00162     ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
00163       : Value(Value, IsVolatile) {}
00164 
00165     bool isNull() const { return !getValue(); }
00166     
00167     bool isVolatile() const { return Value.getInt(); }
00168     llvm::Value *getValue() const { return Value.getPointer(); }
00169   };
00170   
00171 }  // end namespace CodeGen
00172 }  // end namespace clang
00173 
00174 #endif