ABIInfo.h revision b53e3e71383233ebb68a6a736cbe8af6d8065700
19eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar//===----- ABIInfo.h - ABI information access & encapsulation ---*- C++ -*-===//
29eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar//
39eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar//                     The LLVM Compiler Infrastructure
49eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar//
59eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar// This file is distributed under the University of Illinois Open Source
69eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar// License. See LICENSE.TXT for details.
79eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar//
89eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar//===----------------------------------------------------------------------===//
99eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar
109eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar#ifndef CLANG_CODEGEN_ABIINFO_H
119eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar#define CLANG_CODEGEN_ABIINFO_H
129eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar
1388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbarnamespace llvm {
1488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  class Type;
1588c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar}
1688c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar
179eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbarnamespace clang {
186bad2658b48264629db595f944a3fbe07f61b4e9Daniel Dunbar  class ASTContext;
196bad2658b48264629db595f944a3fbe07f61b4e9Daniel Dunbar
206bad2658b48264629db595f944a3fbe07f61b4e9Daniel Dunbar  // FIXME: This is a layering issue if we want to move ABIInfo
216bad2658b48264629db595f944a3fbe07f61b4e9Daniel Dunbar  // down. Fortunately CGFunctionInfo has no real tie to CodeGen.
226bad2658b48264629db595f944a3fbe07f61b4e9Daniel Dunbar  namespace CodeGen {
236bad2658b48264629db595f944a3fbe07f61b4e9Daniel Dunbar    class CGFunctionInfo;
24b53e3e71383233ebb68a6a736cbe8af6d8065700Daniel Dunbar    class CodeGenFunction;
256bad2658b48264629db595f944a3fbe07f61b4e9Daniel Dunbar  }
266bad2658b48264629db595f944a3fbe07f61b4e9Daniel Dunbar
279eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar  /* FIXME: All of this stuff should be part of the target interface
289eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar     somehow. It is currently here because it is not clear how to factor
299eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar     the targets to support this, since the Targets currently live in a
309eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar     layer below types n'stuff.
319eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar  */
329eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar
339eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar  /// ABIArgInfo - Helper class to encapsulate information about how a
349eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar  /// specific C type should be passed to or returned from a function.
359eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar  class ABIArgInfo {
369eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar  public:
379eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    enum Kind {
3846327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar      Direct,    /// Pass the argument directly using the normal
3911e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar                 /// converted LLVM type. Complex and structure types
4011e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar                 /// are passed using first class aggregates.
4111e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar
4211e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar      Indirect,  /// Pass the argument indirectly via a hidden pointer
4311e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar                 /// with the specified alignment (0 indicates default
4411e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar                 /// alignment).
459eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar
469eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar      Ignore,    /// Ignore the argument (treat as void). Useful for
479eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar                 /// void and empty structs.
489eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar
499eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar      Coerce,    /// Only valid for aggregate return types, the argument
509eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar                 /// should be accessed by coercion to a provided type.
519eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar
529eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar      Expand,    /// Only valid for aggregate argument types. The
539eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar                 /// structure should be expanded into consecutive
549eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar                 /// arguments for its constituent fields. Currently
559eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar                 /// expand is only allowed on structures whose fields
569eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar                 /// are all scalar types or are themselves expandable
579eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar                 /// types.
589eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar
590bcc52114e37a8d152d9a05095ee7f7687c9aa94Daniel Dunbar      KindFirst=Direct, KindLast=Expand
609eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    };
619eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar
629eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar  private:
639eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    Kind TheKind;
649eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    const llvm::Type *TypeData;
659eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    unsigned UIntData;
669eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar
679eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    ABIArgInfo(Kind K, const llvm::Type *TD=0,
689eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar               unsigned UI=0) : TheKind(K),
699eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar                                TypeData(TD),
70497a85693e8d290c16bcdf5fb97b67bda206aa7dDaniel Dunbar                                UIntData(UI) {}
719eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar  public:
720bcc52114e37a8d152d9a05095ee7f7687c9aa94Daniel Dunbar    ABIArgInfo() : TheKind(Direct), TypeData(0), UIntData(0) {}
7388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar
7446327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar    static ABIArgInfo getDirect() {
7546327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar      return ABIArgInfo(Direct);
7646327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar    }
779eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    static ABIArgInfo getIgnore() {
789eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar      return ABIArgInfo(Ignore);
799eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    }
809eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    static ABIArgInfo getCoerce(const llvm::Type *T) {
819eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar      return ABIArgInfo(Coerce, T);
829eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    }
8311e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar    static ABIArgInfo getIndirect(unsigned Alignment) {
8411e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar      return ABIArgInfo(Indirect, 0, Alignment);
859eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    }
869eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    static ABIArgInfo getExpand() {
879eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar      return ABIArgInfo(Expand);
889eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    }
899eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar
909eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    Kind getKind() const { return TheKind; }
9146327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar    bool isDirect() const { return TheKind == Direct; }
929eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    bool isIgnore() const { return TheKind == Ignore; }
939eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    bool isCoerce() const { return TheKind == Coerce; }
9411e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar    bool isIndirect() const { return TheKind == Indirect; }
959eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    bool isExpand() const { return TheKind == Expand; }
969eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar
979eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    // Coerce accessors
989eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    const llvm::Type *getCoerceToType() const {
999eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar      assert(TheKind == Coerce && "Invalid kind!");
1009eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar      return TypeData;
1019eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    }
1029eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar
1039eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    // ByVal accessors
10411e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar    unsigned getIndirectAlign() const {
10511e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar      assert(TheKind == Indirect && "Invalid kind!");
1069eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar      return UIntData;
1079eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    }
1086f7279bf98e31356306386b2c200820a76b491cfDaniel Dunbar
1096f7279bf98e31356306386b2c200820a76b491cfDaniel Dunbar    void dump() const;
1109eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar  };
1119eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar
1129eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar  /// ABIInfo - Target specific hooks for defining how a type should be
1139eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar  /// passed or returned from functions.
1149eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar  class ABIInfo {
1159eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar  public:
1169eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar    virtual ~ABIInfo();
1176bad2658b48264629db595f944a3fbe07f61b4e9Daniel Dunbar
1186bad2658b48264629db595f944a3fbe07f61b4e9Daniel Dunbar    virtual void computeInfo(CodeGen::CGFunctionInfo &FI,
1196bad2658b48264629db595f944a3fbe07f61b4e9Daniel Dunbar                             ASTContext &Ctx) const = 0;
120b53e3e71383233ebb68a6a736cbe8af6d8065700Daniel Dunbar
121b53e3e71383233ebb68a6a736cbe8af6d8065700Daniel Dunbar    /// EmitVAArg - Emit the target dependent code to load a value of
122b53e3e71383233ebb68a6a736cbe8af6d8065700Daniel Dunbar    /// \arg Ty from the va_list pointed to by \arg VAListAddr.
123b53e3e71383233ebb68a6a736cbe8af6d8065700Daniel Dunbar
124b53e3e71383233ebb68a6a736cbe8af6d8065700Daniel Dunbar    // FIXME: This is a gaping layering violation if we wanted to drop
125b53e3e71383233ebb68a6a736cbe8af6d8065700Daniel Dunbar    // the ABI information any lower than CodeGen. Of course, for
126b53e3e71383233ebb68a6a736cbe8af6d8065700Daniel Dunbar    // VAArg handling it has to be at this level; there is no way to
127b53e3e71383233ebb68a6a736cbe8af6d8065700Daniel Dunbar    // abstract this out.
128b53e3e71383233ebb68a6a736cbe8af6d8065700Daniel Dunbar    virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
129b53e3e71383233ebb68a6a736cbe8af6d8065700Daniel Dunbar                                   CodeGen::CodeGenFunction &CGF) const = 0;
1309eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar  };
1319eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar}  // end namespace clang
1329eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar
1339eb5c6d3242d85359b2d669564c5e2826efb1931Daniel Dunbar#endif
134