ABIInfo.h revision b53e3e71383233ebb68a6a736cbe8af6d8065700
1//===----- ABIInfo.h - ABI information access & encapsulation ---*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef CLANG_CODEGEN_ABIINFO_H
11#define CLANG_CODEGEN_ABIINFO_H
12
13namespace llvm {
14  class Type;
15}
16
17namespace clang {
18  class ASTContext;
19
20  // FIXME: This is a layering issue if we want to move ABIInfo
21  // down. Fortunately CGFunctionInfo has no real tie to CodeGen.
22  namespace CodeGen {
23    class CGFunctionInfo;
24    class CodeGenFunction;
25  }
26
27  /* FIXME: All of this stuff should be part of the target interface
28     somehow. It is currently here because it is not clear how to factor
29     the targets to support this, since the Targets currently live in a
30     layer below types n'stuff.
31  */
32
33  /// ABIArgInfo - Helper class to encapsulate information about how a
34  /// specific C type should be passed to or returned from a function.
35  class ABIArgInfo {
36  public:
37    enum Kind {
38      Direct,    /// Pass the argument directly using the normal
39                 /// converted LLVM type. Complex and structure types
40                 /// are passed using first class aggregates.
41
42      Indirect,  /// Pass the argument indirectly via a hidden pointer
43                 /// with the specified alignment (0 indicates default
44                 /// alignment).
45
46      Ignore,    /// Ignore the argument (treat as void). Useful for
47                 /// void and empty structs.
48
49      Coerce,    /// Only valid for aggregate return types, the argument
50                 /// should be accessed by coercion to a provided type.
51
52      Expand,    /// Only valid for aggregate argument types. The
53                 /// structure should be expanded into consecutive
54                 /// arguments for its constituent fields. Currently
55                 /// expand is only allowed on structures whose fields
56                 /// are all scalar types or are themselves expandable
57                 /// types.
58
59      KindFirst=Direct, KindLast=Expand
60    };
61
62  private:
63    Kind TheKind;
64    const llvm::Type *TypeData;
65    unsigned UIntData;
66
67    ABIArgInfo(Kind K, const llvm::Type *TD=0,
68               unsigned UI=0) : TheKind(K),
69                                TypeData(TD),
70                                UIntData(UI) {}
71  public:
72    ABIArgInfo() : TheKind(Direct), TypeData(0), UIntData(0) {}
73
74    static ABIArgInfo getDirect() {
75      return ABIArgInfo(Direct);
76    }
77    static ABIArgInfo getIgnore() {
78      return ABIArgInfo(Ignore);
79    }
80    static ABIArgInfo getCoerce(const llvm::Type *T) {
81      return ABIArgInfo(Coerce, T);
82    }
83    static ABIArgInfo getIndirect(unsigned Alignment) {
84      return ABIArgInfo(Indirect, 0, Alignment);
85    }
86    static ABIArgInfo getExpand() {
87      return ABIArgInfo(Expand);
88    }
89
90    Kind getKind() const { return TheKind; }
91    bool isDirect() const { return TheKind == Direct; }
92    bool isIgnore() const { return TheKind == Ignore; }
93    bool isCoerce() const { return TheKind == Coerce; }
94    bool isIndirect() const { return TheKind == Indirect; }
95    bool isExpand() const { return TheKind == Expand; }
96
97    // Coerce accessors
98    const llvm::Type *getCoerceToType() const {
99      assert(TheKind == Coerce && "Invalid kind!");
100      return TypeData;
101    }
102
103    // ByVal accessors
104    unsigned getIndirectAlign() const {
105      assert(TheKind == Indirect && "Invalid kind!");
106      return UIntData;
107    }
108
109    void dump() const;
110  };
111
112  /// ABIInfo - Target specific hooks for defining how a type should be
113  /// passed or returned from functions.
114  class ABIInfo {
115  public:
116    virtual ~ABIInfo();
117
118    virtual void computeInfo(CodeGen::CGFunctionInfo &FI,
119                             ASTContext &Ctx) const = 0;
120
121    /// EmitVAArg - Emit the target dependent code to load a value of
122    /// \arg Ty from the va_list pointed to by \arg VAListAddr.
123
124    // FIXME: This is a gaping layering violation if we wanted to drop
125    // the ABI information any lower than CodeGen. Of course, for
126    // VAArg handling it has to be at this level; there is no way to
127    // abstract this out.
128    virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
129                                   CodeGen::CodeGenFunction &CGF) const = 0;
130  };
131}  // end namespace clang
132
133#endif
134