Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 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 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 13 | #include "clang/AST/Type.h" |
Chris Lattner | 958c53c | 2010-06-29 19:21:36 +0000 | [diff] [blame] | 14 | #include "llvm/Type.h" |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 15 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 16 | namespace llvm { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 17 | class Value; |
Benjamin Kramer | f21efe9 | 2009-08-11 17:46:57 +0000 | [diff] [blame] | 18 | class LLVMContext; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 19 | class TargetData; |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 20 | } |
| 21 | |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 22 | namespace clang { |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 23 | class ASTContext; |
| 24 | |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 25 | namespace CodeGen { |
| 26 | class CGFunctionInfo; |
Daniel Dunbar | b53e3e7 | 2009-02-10 21:44:36 +0000 | [diff] [blame] | 27 | class CodeGenFunction; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 28 | class CodeGenTypes; |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Chris Lattner | 2eb9cdd | 2010-07-28 23:46:15 +0000 | [diff] [blame] | 31 | // FIXME: All of this stuff should be part of the target interface |
| 32 | // somehow. It is currently here because it is not clear how to factor |
| 33 | // the targets to support this, since the Targets currently live in a |
| 34 | // layer below types n'stuff. |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 35 | |
| 36 | /// ABIArgInfo - Helper class to encapsulate information about how a |
| 37 | /// specific C type should be passed to or returned from a function. |
| 38 | class ABIArgInfo { |
| 39 | public: |
| 40 | enum Kind { |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 41 | /// Direct - Pass the argument directly using the normal converted LLVM |
| 42 | /// type, or by coercing to another specified type stored in |
| 43 | /// 'CoerceToType'). If an offset is specified (in UIntData), then the |
| 44 | /// argument passed is offset by some number of bytes in the memory |
| 45 | /// representation. |
| 46 | Direct, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 48 | /// Extend - Valid only for integer argument types. Same as 'direct' |
| 49 | /// but also emit a zero/sign extension attribute. |
| 50 | Extend, |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 52 | /// Indirect - Pass the argument indirectly via a hidden pointer |
| 53 | /// with the specified alignment (0 indicates default alignment). |
| 54 | Indirect, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 56 | /// Ignore - Ignore the argument (treat as void). Useful for void and |
| 57 | /// empty structs. |
| 58 | Ignore, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 60 | /// Expand - Only valid for aggregate argument types. The structure should |
| 61 | /// be expanded into consecutive arguments for its constituent fields. |
| 62 | /// Currently expand is only allowed on structures whose fields |
| 63 | /// are all scalar types or are themselves expandable types. |
| 64 | Expand, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 65 | |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 66 | KindFirst=Direct, KindLast=Expand |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 67 | }; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 68 | |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 69 | private: |
| 70 | Kind TheKind; |
Chris Lattner | 958c53c | 2010-06-29 19:21:36 +0000 | [diff] [blame] | 71 | llvm::PATypeHolder TypeData; |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 72 | unsigned UIntData; |
Daniel Dunbar | cf3b6f2 | 2010-09-16 20:42:02 +0000 | [diff] [blame^] | 73 | bool BoolData0; |
| 74 | bool BoolData1; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 75 | |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 76 | ABIArgInfo(Kind K, const llvm::Type *TD=0, |
Daniel Dunbar | cf3b6f2 | 2010-09-16 20:42:02 +0000 | [diff] [blame^] | 77 | unsigned UI=0, bool B0 = false, bool B1 = false) |
| 78 | : TheKind(K), TypeData(TD), UIntData(UI), BoolData0(B0), BoolData1(B1) {} |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 79 | |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 80 | public: |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 81 | ABIArgInfo() : TheKind(Direct), TypeData(0), UIntData(0) {} |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 83 | static ABIArgInfo getDirect(const llvm::Type *T = 0, unsigned Offset = 0) { |
| 84 | return ABIArgInfo(Direct, T, Offset); |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 85 | } |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 86 | static ABIArgInfo getExtend(const llvm::Type *T = 0) { |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 87 | return ABIArgInfo(Extend, T, 0); |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 88 | } |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 89 | static ABIArgInfo getIgnore() { |
| 90 | return ABIArgInfo(Ignore); |
| 91 | } |
Daniel Dunbar | cf3b6f2 | 2010-09-16 20:42:02 +0000 | [diff] [blame^] | 92 | static ABIArgInfo getIndirect(unsigned Alignment, bool ByVal = true |
| 93 | , bool Realign = false) { |
| 94 | return ABIArgInfo(Indirect, 0, Alignment, ByVal, Realign); |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 95 | } |
| 96 | static ABIArgInfo getExpand() { |
| 97 | return ABIArgInfo(Expand); |
| 98 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 99 | |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 100 | Kind getKind() const { return TheKind; } |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 101 | bool isDirect() const { return TheKind == Direct; } |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 102 | bool isExtend() const { return TheKind == Extend; } |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 103 | bool isIgnore() const { return TheKind == Ignore; } |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 104 | bool isIndirect() const { return TheKind == Indirect; } |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 105 | bool isExpand() const { return TheKind == Expand; } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 107 | bool canHaveCoerceToType() const { |
| 108 | return TheKind == Direct || TheKind == Extend; |
| 109 | } |
| 110 | |
| 111 | // Direct/Extend accessors |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 112 | unsigned getDirectOffset() const { |
| 113 | assert((isDirect() || isExtend()) && "Not a direct or extend kind"); |
| 114 | return UIntData; |
| 115 | } |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 116 | const llvm::Type *getCoerceToType() const { |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 117 | assert(canHaveCoerceToType() && "Invalid kind!"); |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 118 | return TypeData; |
| 119 | } |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 120 | |
| 121 | void setCoerceToType(const llvm::Type *T) { |
| 122 | assert(canHaveCoerceToType() && "Invalid kind!"); |
| 123 | TypeData = T; |
| 124 | } |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 125 | |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 126 | // Indirect accessors |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 127 | unsigned getIndirectAlign() const { |
| 128 | assert(TheKind == Indirect && "Invalid kind!"); |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 129 | return UIntData; |
| 130 | } |
Daniel Dunbar | 6f7279b | 2009-02-04 23:24:38 +0000 | [diff] [blame] | 131 | |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 132 | bool getIndirectByVal() const { |
| 133 | assert(TheKind == Indirect && "Invalid kind!"); |
Daniel Dunbar | cf3b6f2 | 2010-09-16 20:42:02 +0000 | [diff] [blame^] | 134 | return BoolData0; |
| 135 | } |
| 136 | |
| 137 | bool getIndirectRealign() const { |
| 138 | assert(TheKind == Indirect && "Invalid kind!"); |
| 139 | return BoolData1; |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Daniel Dunbar | 6f7279b | 2009-02-04 23:24:38 +0000 | [diff] [blame] | 142 | void dump() const; |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | /// ABIInfo - Target specific hooks for defining how a type should be |
| 146 | /// passed or returned from functions. |
| 147 | class ABIInfo { |
| 148 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 149 | CodeGen::CodeGenTypes &CGT; |
| 150 | |
| 151 | ABIInfo(CodeGen::CodeGenTypes &cgt) : CGT(cgt) {} |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 152 | virtual ~ABIInfo(); |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 153 | |
| 154 | ASTContext &getContext() const; |
| 155 | llvm::LLVMContext &getVMContext() const; |
| 156 | const llvm::TargetData &getTargetData() const; |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 157 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 158 | virtual void computeInfo(CodeGen::CGFunctionInfo &FI) const = 0; |
Daniel Dunbar | b53e3e7 | 2009-02-10 21:44:36 +0000 | [diff] [blame] | 159 | |
| 160 | /// EmitVAArg - Emit the target dependent code to load a value of |
| 161 | /// \arg Ty from the va_list pointed to by \arg VAListAddr. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 162 | |
Daniel Dunbar | b53e3e7 | 2009-02-10 21:44:36 +0000 | [diff] [blame] | 163 | // FIXME: This is a gaping layering violation if we wanted to drop |
| 164 | // the ABI information any lower than CodeGen. Of course, for |
| 165 | // VAArg handling it has to be at this level; there is no way to |
| 166 | // abstract this out. |
| 167 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 168 | CodeGen::CodeGenFunction &CGF) const = 0; |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 169 | }; |
| 170 | } // end namespace clang |
| 171 | |
| 172 | #endif |