CGCall.h revision 651f13cea278ec967336033dd032faef0e9fc2ec
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 150dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#ifndef CLANG_CODEGEN_CGCALL_H 160dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#define CLANG_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: 59651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines CallArgList() : StackBase(0), StackBaseMem(0) {} 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. 67f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *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 91b6a6079449a5275c283982e19b0c38e165833bb2John McCall void addWriteback(LValue srcLV, llvm::Value *temporary, 92b6a6079449a5275c283982e19b0c38e165833bb2John McCall llvm::Value *toUse) { 93f85e193739c953358c865005855253af4f68a497John McCall Writeback writeback; 94b6a6079449a5275c283982e19b0c38e165833bb2John McCall writeback.Source = srcLV; 95f85e193739c953358c865005855253af4f68a497John McCall writeback.Temporary = temporary; 96b6a6079449a5275c283982e19b0c38e165833bb2John McCall writeback.ToUse = toUse; 97f85e193739c953358c865005855253af4f68a497John McCall Writebacks.push_back(writeback); 98f85e193739c953358c865005855253af4f68a497John McCall } 99f85e193739c953358c865005855253af4f68a497John McCall 100f85e193739c953358c865005855253af4f68a497John McCall bool hasWritebacks() const { return !Writebacks.empty(); } 101f85e193739c953358c865005855253af4f68a497John McCall 102651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator> 103651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines writeback_const_range; 104651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 105651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines writeback_const_range writebacks() const { 106651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines return writeback_const_range(Writebacks.begin(), Writebacks.end()); 107651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines } 108f85e193739c953358c865005855253af4f68a497John McCall 1099b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup, 1109b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner llvm::Instruction *IsActiveIP) { 1119b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner CallArgCleanup ArgCleanup; 1129b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner ArgCleanup.Cleanup = Cleanup; 1139b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner ArgCleanup.IsActiveIP = IsActiveIP; 1149b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner CleanupsToDeactivate.push_back(ArgCleanup); 1159b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner } 1169b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner 1179b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const { 1189b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner return CleanupsToDeactivate; 1199b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner } 1209b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner 121651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void allocateArgumentMemory(CodeGenFunction &CGF); 122651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines llvm::Instruction *getStackBase() const { return StackBase; } 123651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void freeArgumentMemory(CodeGenFunction &CGF) const; 124651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 125651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines /// \brief Returns if we're using an inalloca struct to pass arguments in 126651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines /// memory. 127651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines bool isUsingInAlloca() const { return StackBase; } 128651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 129f85e193739c953358c865005855253af4f68a497John McCall private: 130686775deca8b8685eb90801495880e3abdd844c2Chris Lattner SmallVector<Writeback, 1> Writebacks; 1319b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner 1329b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner /// Deactivate these cleanups immediately before making the call. This 1339b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner /// is used to cleanup objects that are owned by the callee once the call 1349b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner /// occurs. 1359b60195ad4843c9e2e231673a0dbc0d5c8c6eb2bReid Kleckner SmallVector<CallArgCleanup, 1> CleanupsToDeactivate; 136651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 137651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines /// The stacksave call. It dominates all of the argument evaluation. 138651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines llvm::CallInst *StackBase; 139651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 140651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines /// The alloca holding the stackbase. We need it to maintain SSA form. 141651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines llvm::AllocaInst *StackBaseMem; 142651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 143651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines /// The iterator pointing to the stack restore cleanup. We manually run and 144651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines /// deactivate this cleanup after the call in the unexceptional case because 145651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines /// it doesn't run in the normal order. 146651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines EHScopeStack::stable_iterator StackCleanup; 147d26bc76c98006609002d9930f8840490e88ac5b5John McCall }; 1480dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar 1497c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar /// FunctionArgList - Type for representing both the decl and type 1507c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar /// of parameters to a function. The decl must be either a 1517c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar /// ParmVarDecl or ImplicitParamDecl. 152686775deca8b8685eb90801495880e3abdd844c2Chris Lattner class FunctionArgList : public SmallVector<const VarDecl*, 16> { 153d26bc76c98006609002d9930f8840490e88ac5b5John McCall }; 1541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 155d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson /// ReturnValueSlot - Contains the address where the return value of a 156d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson /// function can be stored, and whether the address is volatile or not. 15731777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson class ReturnValueSlot { 15831777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson llvm::PointerIntPair<llvm::Value *, 1, bool> Value; 15931777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson 16031777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson public: 16131777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson ReturnValueSlot() {} 16231777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson ReturnValueSlot(llvm::Value *Value, bool IsVolatile) 16331777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson : Value(Value, IsVolatile) {} 16431777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson 16531777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson bool isNull() const { return !getValue(); } 16631777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson 16731777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson bool isVolatile() const { return Value.getInt(); } 16831777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson llvm::Value *getValue() const { return Value.getPointer(); } 16931777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson }; 17031777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson 1710dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar} // end namespace CodeGen 1720dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar} // end namespace clang 1730dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar 1740dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#endif 175