CGCall.h revision d2490a91341b57df7a7e54f8a707e7ecde2eeb4e
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
1831777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson#include "llvm/ADT/FoldingSet.h"
1931777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson#include "llvm/Value.h"
200dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "clang/AST/Type.h"
210dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
2246f45b9bec4a265ad8400a538e5ec3a5683617f1Daniel Dunbar#include "CGValue.h"
2346f45b9bec4a265ad8400a538e5ec3a5683617f1Daniel Dunbar
2488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar// FIXME: Restructure so we don't have to expose so much stuff.
2588c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar#include "ABIInfo.h"
2688c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar
270dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarnamespace llvm {
28761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  struct AttributeWithIndex;
2988c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  class Function;
3088c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  class Type;
310dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  class Value;
320dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
330dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  template<typename T, unsigned> class SmallVector;
340dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
350dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
360dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarnamespace clang {
370dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  class ASTContext;
380dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  class Decl;
390dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  class FunctionDecl;
400dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  class ObjCMethodDecl;
417c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  class VarDecl;
420dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
430dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarnamespace CodeGen {
44761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  typedef llvm::SmallVector<llvm::AttributeWithIndex, 8> AttributeListType;
450dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
460dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  /// CallArgList - Type for representing both the value and type of
470dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  /// arguments in a call.
4846f45b9bec4a265ad8400a538e5ec3a5683617f1Daniel Dunbar  typedef llvm::SmallVector<std::pair<RValue, QualType>, 16> CallArgList;
490dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
507c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  /// FunctionArgList - Type for representing both the decl and type
517c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  /// of parameters to a function. The decl must be either a
527c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar  /// ParmVarDecl or ImplicitParamDecl.
531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  typedef llvm::SmallVector<std::pair<const VarDecl*, QualType>,
547c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                            16> FunctionArgList;
551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
560dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  /// CGFunctionInfo - Class to encapsulate the information about a
570dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  /// function definition.
5840a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  class CGFunctionInfo : public llvm::FoldingSetNode {
5988c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    struct ArgInfo {
6088c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar      QualType type;
6188c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar      ABIArgInfo info;
6288c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    };
6388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar
64ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar    /// The LLVM::CallingConv to use for this function (as specified by the
65ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar    /// user).
66bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar    unsigned CallingConvention;
67bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar
68ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar    /// The LLVM::CallingConv to actually use for this function, which may
69ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar    /// depend on the ABI.
70ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar    unsigned EffectiveCallingConvention;
71ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar
7288c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    unsigned NumArgs;
7388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    ArgInfo *Args;
740dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
750dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  public:
7688c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    typedef const ArgInfo *const_arg_iterator;
7788c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    typedef ArgInfo *arg_iterator;
78a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar
79bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar    CGFunctionInfo(unsigned CallingConvention,
80bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar                   QualType ResTy,
81541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar                   const llvm::SmallVector<QualType, 16> &ArgTys);
8288c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    ~CGFunctionInfo() { delete[] Args; }
8388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar
8488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    const_arg_iterator arg_begin() const { return Args + 1; }
8588c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    const_arg_iterator arg_end() const { return Args + 1 + NumArgs; }
8688c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    arg_iterator arg_begin() { return Args + 1; }
8788c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    arg_iterator arg_end() { return Args + 1 + NumArgs; }
880dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
894b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar    unsigned  arg_size() const { return NumArgs; }
904b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar
91ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar    /// getCallingConvention - Return the user specified calling
92ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar    /// convention.
93bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar    unsigned getCallingConvention() const { return CallingConvention; }
94bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar
95ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar    /// getEffectiveCallingConvention - Return the actual calling convention to
96ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar    /// use, which may depend on the ABI.
97ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar    unsigned getEffectiveCallingConvention() const {
98ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar      return EffectiveCallingConvention;
99ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar    }
100ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar    void setEffectiveCallingConvention(unsigned Value) {
101ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar      EffectiveCallingConvention = Value;
102ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar    }
103ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar
10488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    QualType getReturnType() const { return Args[0].type; }
105bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar
10688c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    ABIArgInfo &getReturnInfo() { return Args[0].info; }
10788c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    const ABIArgInfo &getReturnInfo() const { return Args[0].info; }
10840a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar
10940a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar    void Profile(llvm::FoldingSetNodeID &ID) {
110bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar      ID.AddInteger(getCallingConvention());
11135e67d4387bbe3e7e17ee6b17eaa42eebb0eb9f1Daniel Dunbar      getReturnType().Profile(ID);
11288c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar      for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
11388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar        it->type.Profile(ID);
11440a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar    }
11540a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar    template<class Iterator>
1161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    static void Profile(llvm::FoldingSetNodeID &ID,
117bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar                        unsigned CallingConvention,
11840a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar                        QualType ResTy,
11940a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar                        Iterator begin,
12040a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar                        Iterator end) {
121bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar      ID.AddInteger(CallingConvention);
12240a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar      ResTy.Profile(ID);
12340a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar      for (; begin != end; ++begin)
12440a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar        begin->Profile(ID);
12540a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar    }
1260dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  };
12731777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson
128d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson  /// ReturnValueSlot - Contains the address where the return value of a
129d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson  /// function can be stored, and whether the address is volatile or not.
13031777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson  class ReturnValueSlot {
13131777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson    llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
13231777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson
13331777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson  public:
13431777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson    ReturnValueSlot() {}
13531777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson    ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
13631777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson      : Value(Value, IsVolatile) {}
13731777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson
13831777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson    bool isNull() const { return !getValue(); }
13931777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson
14031777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson    bool isVolatile() const { return Value.getInt(); }
14131777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson    llvm::Value *getValue() const { return Value.getPointer(); }
14231777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson  };
14331777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson
1440dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}  // end namespace CodeGen
1450dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}  // end namespace clang
1460dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
1470dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#endif
148