10dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
20dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
30dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//                     The LLVM Compiler Infrastructure
40dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
50dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// This file is distributed under the University of Illinois Open Source
60dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// License. See LICENSE.TXT for details.
70dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
80dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//===----------------------------------------------------------------------===//
90dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
100dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// These classes wrap the information about a call or function
110dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// definition used to handle ABI compliancy.
120dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
130dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//===----------------------------------------------------------------------===//
140dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
15176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#ifndef LLVM_CLANG_LIB_CODEGEN_CGCALL_H
16176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#define LLVM_CLANG_LIB_CODEGEN_CGCALL_H
170dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
1855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CGValue.h"
199b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner#include "EHScopeStack.h"
2055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/CanonicalType.h"
2155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/Type.h"
2231777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson#include "llvm/ADT/FoldingSet.h"
233b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Value.h"
2446f45b9bec4a265ad8400a538e5ec3a5683617f1Daniel Dunbar
2588c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar// FIXME: Restructure so we don't have to expose so much stuff.
2688c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar#include "ABIInfo.h"
2788c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar
280dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarnamespace llvm {
29b263bdf2954953cc7cca5d667eb01319ea0f72ecBill Wendling  class AttributeSet;
3088c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  class Function;
3188c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  class Type;
320dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  class Value;
330dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
340dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
350dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarnamespace clang {
360dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  class ASTContext;
370dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  class Decl;
380dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  class FunctionDecl;
390dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  class ObjCMethodDecl;
407c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  class VarDecl;
410dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
420dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarnamespace CodeGen {
43b263bdf2954953cc7cca5d667eb01319ea0f72ecBill Wendling  typedef SmallVector<llvm::AttributeSet, 8> AttributeListType;
440dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
45c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman  struct CallArg {
46c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman    RValue RV;
47c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman    QualType Ty;
4855d484802f3e27930317739efc5f5956b78aac25Eli Friedman    bool NeedsCopy;
4955d484802f3e27930317739efc5f5956b78aac25Eli Friedman    CallArg(RValue rv, QualType ty, bool needscopy)
5055d484802f3e27930317739efc5f5956b78aac25Eli Friedman    : RV(rv), Ty(ty), NeedsCopy(needscopy)
51c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman    { }
52c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman  };
53c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman
540dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  /// CallArgList - Type for representing both the value and type of
550dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  /// arguments in a call.
56d26bc76c98006609002d9930f8840490e88ac5b5John McCall  class CallArgList :
57686775deca8b8685eb90801495880e3abdd844c2Chris Lattner    public SmallVector<CallArg, 16> {
58413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  public:
59a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    CallArgList() : StackBase(nullptr) {}
60651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
61f85e193739c953358c865005855253af4f68a497John McCall    struct Writeback {
62b6a6079449a5275c283982e19b0c38e165833bb2John McCall      /// The original argument.  Note that the argument l-value
63b6a6079449a5275c283982e19b0c38e165833bb2John McCall      /// is potentially null.
64b6a6079449a5275c283982e19b0c38e165833bb2John McCall      LValue Source;
65f85e193739c953358c865005855253af4f68a497John McCall
66f85e193739c953358c865005855253af4f68a497John McCall      /// The temporary alloca.
67a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      Address Temporary;
68b6a6079449a5275c283982e19b0c38e165833bb2John McCall
69b6a6079449a5275c283982e19b0c38e165833bb2John McCall      /// A value to "use" after the writeback, or null.
70b6a6079449a5275c283982e19b0c38e165833bb2John McCall      llvm::Value *ToUse;
71f85e193739c953358c865005855253af4f68a497John McCall    };
72f85e193739c953358c865005855253af4f68a497John McCall
739b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner    struct CallArgCleanup {
749b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner      EHScopeStack::stable_iterator Cleanup;
759b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner
769b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner      /// The "is active" insertion point.  This instruction is temporary and
779b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner      /// will be removed after insertion.
789b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner      llvm::Instruction *IsActiveIP;
799b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner    };
809b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner
8155d484802f3e27930317739efc5f5956b78aac25Eli Friedman    void add(RValue rvalue, QualType type, bool needscopy = false) {
8255d484802f3e27930317739efc5f5956b78aac25Eli Friedman      push_back(CallArg(rvalue, type, needscopy));
83413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    }
84f85e193739c953358c865005855253af4f68a497John McCall
85f85e193739c953358c865005855253af4f68a497John McCall    void addFrom(const CallArgList &other) {
86f85e193739c953358c865005855253af4f68a497John McCall      insert(end(), other.begin(), other.end());
87f85e193739c953358c865005855253af4f68a497John McCall      Writebacks.insert(Writebacks.end(),
88f85e193739c953358c865005855253af4f68a497John McCall                        other.Writebacks.begin(), other.Writebacks.end());
89f85e193739c953358c865005855253af4f68a497John McCall    }
90f85e193739c953358c865005855253af4f68a497John McCall
91a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    void addWriteback(LValue srcLV, Address temporary,
92b6a6079449a5275c283982e19b0c38e165833bb2John McCall                      llvm::Value *toUse) {
93a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      Writeback writeback = { srcLV, temporary, toUse };
94f85e193739c953358c865005855253af4f68a497John McCall      Writebacks.push_back(writeback);
95f85e193739c953358c865005855253af4f68a497John McCall    }
96f85e193739c953358c865005855253af4f68a497John McCall
97f85e193739c953358c865005855253af4f68a497John McCall    bool hasWritebacks() const { return !Writebacks.empty(); }
98f85e193739c953358c865005855253af4f68a497John McCall
99651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
100651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      writeback_const_range;
101651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
102651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    writeback_const_range writebacks() const {
103651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return writeback_const_range(Writebacks.begin(), Writebacks.end());
104651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
105f85e193739c953358c865005855253af4f68a497John McCall
1069b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner    void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
1079b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner                                   llvm::Instruction *IsActiveIP) {
1089b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner      CallArgCleanup ArgCleanup;
1099b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner      ArgCleanup.Cleanup = Cleanup;
1109b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner      ArgCleanup.IsActiveIP = IsActiveIP;
1119b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner      CleanupsToDeactivate.push_back(ArgCleanup);
1129b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner    }
1139b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner
1149b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner    ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
1159b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner      return CleanupsToDeactivate;
1169b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner    }
1179b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner
118651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    void allocateArgumentMemory(CodeGenFunction &CGF);
119651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::Instruction *getStackBase() const { return StackBase; }
120651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    void freeArgumentMemory(CodeGenFunction &CGF) const;
121651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
122651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// \brief Returns if we're using an inalloca struct to pass arguments in
123651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// memory.
124651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    bool isUsingInAlloca() const { return StackBase; }
125651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
126f85e193739c953358c865005855253af4f68a497John McCall  private:
127686775deca8b8685eb90801495880e3abdd844c2Chris Lattner    SmallVector<Writeback, 1> Writebacks;
1289b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner
1299b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner    /// Deactivate these cleanups immediately before making the call.  This
1309b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner    /// is used to cleanup objects that are owned by the callee once the call
1319b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner    /// occurs.
1329b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner    SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
133651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
134651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// The stacksave call.  It dominates all of the argument evaluation.
135651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::CallInst *StackBase;
136651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
137651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// The iterator pointing to the stack restore cleanup.  We manually run and
138651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// deactivate this cleanup after the call in the unexceptional case because
139651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// it doesn't run in the normal order.
140651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    EHScopeStack::stable_iterator StackCleanup;
141d26bc76c98006609002d9930f8840490e88ac5b5John McCall  };
1420dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
1437c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  /// FunctionArgList - Type for representing both the decl and type
1447c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  /// of parameters to a function. The decl must be either a
1457c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  /// ParmVarDecl or ImplicitParamDecl.
146686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  class FunctionArgList : public SmallVector<const VarDecl*, 16> {
147d26bc76c98006609002d9930f8840490e88ac5b5John McCall  };
1481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
149d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson  /// ReturnValueSlot - Contains the address where the return value of a
150d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson  /// function can be stored, and whether the address is volatile or not.
15131777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson  class ReturnValueSlot {
152b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value;
153a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    CharUnits Alignment;
154b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
155b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    // Return value slot flags
156b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    enum Flags {
157b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar      IS_VOLATILE = 0x1,
158b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar      IS_UNUSED = 0x2,
159b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    };
16031777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson
16131777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson  public:
16231777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson    ReturnValueSlot() {}
163a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false)
164a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar      : Value(Addr.isValid() ? Addr.getPointer() : nullptr,
165a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar              (IsVolatile ? IS_VOLATILE : 0) | (IsUnused ? IS_UNUSED : 0)),
166a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar        Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {}
16731777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson
168a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    bool isNull() const { return !getValue().isValid(); }
169b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar
170b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    bool isVolatile() const { return Value.getInt() & IS_VOLATILE; }
171a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    Address getValue() const { return Address(Value.getPointer(), Alignment); }
172b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar    bool isUnused() const { return Value.getInt() & IS_UNUSED; }
17331777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson  };
17431777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson
1750dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}  // end namespace CodeGen
1760dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}  // end namespace clang
1770dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
1780dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#endif
179