blob: 1c004bc874c38fa3cc6af1defa357c72fee0b6cb [file] [log] [blame]
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +00001//===----- 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 Korobeynikovc4a59eb2009-06-05 22:08:42 +000013#include "clang/AST/Type.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000014#include "llvm/IR/Type.h"
John McCallbd7370a2013-02-28 19:01:20 +000015#include "llvm/IR/CallingConv.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000017namespace llvm {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000018 class Value;
Benjamin Kramerf21efe92009-08-11 17:46:57 +000019 class LLVMContext;
Micah Villmow25a6a842012-10-08 16:25:52 +000020 class DataLayout;
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000021}
22
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000023namespace clang {
Daniel Dunbar6bad2652009-02-03 06:51:18 +000024 class ASTContext;
John McCall64aa4b32013-04-16 22:48:15 +000025 class TargetInfo;
Daniel Dunbar6bad2652009-02-03 06:51:18 +000026
Daniel Dunbar6bad2652009-02-03 06:51:18 +000027 namespace CodeGen {
Mark Lacey23630722013-10-06 01:33:34 +000028 class CGCXXABI;
Daniel Dunbar6bad2652009-02-03 06:51:18 +000029 class CGFunctionInfo;
Daniel Dunbarb53e3e72009-02-10 21:44:36 +000030 class CodeGenFunction;
Chris Lattnerea044322010-07-29 02:01:43 +000031 class CodeGenTypes;
Daniel Dunbar6bad2652009-02-03 06:51:18 +000032 }
33
Chris Lattner2eb9cdd2010-07-28 23:46:15 +000034 // FIXME: All of this stuff should be part of the target interface
35 // somehow. It is currently here because it is not clear how to factor
36 // the targets to support this, since the Targets currently live in a
37 // layer below types n'stuff.
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000038
39 /// ABIArgInfo - Helper class to encapsulate information about how a
40 /// specific C type should be passed to or returned from a function.
41 class ABIArgInfo {
42 public:
43 enum Kind {
Chris Lattner117e3f42010-07-30 04:02:24 +000044 /// Direct - Pass the argument directly using the normal converted LLVM
45 /// type, or by coercing to another specified type stored in
46 /// 'CoerceToType'). If an offset is specified (in UIntData), then the
47 /// argument passed is offset by some number of bytes in the memory
Akira Hatanakaf0cc2082012-01-07 00:25:33 +000048 /// representation. A dummy argument is emitted before the real argument
49 /// if the specified type stored in "PaddingType" is not zero.
Chris Lattner117e3f42010-07-30 04:02:24 +000050 Direct,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000051
Chris Lattner117e3f42010-07-30 04:02:24 +000052 /// Extend - Valid only for integer argument types. Same as 'direct'
53 /// but also emit a zero/sign extension attribute.
54 Extend,
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000055
Chris Lattner117e3f42010-07-30 04:02:24 +000056 /// Indirect - Pass the argument indirectly via a hidden pointer
57 /// with the specified alignment (0 indicates default alignment).
58 Indirect,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000059
Chris Lattner117e3f42010-07-30 04:02:24 +000060 /// Ignore - Ignore the argument (treat as void). Useful for void and
61 /// empty structs.
62 Ignore,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000063
Chris Lattner117e3f42010-07-30 04:02:24 +000064 /// Expand - Only valid for aggregate argument types. The structure should
65 /// be expanded into consecutive arguments for its constituent fields.
66 /// Currently expand is only allowed on structures whose fields
67 /// are all scalar types or are themselves expandable types.
68 Expand,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000069
Daniel Dunbar0bcc5212009-02-03 06:30:17 +000070 KindFirst=Direct, KindLast=Expand
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000071 };
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000072
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000073 private:
74 Kind TheKind;
Chris Lattner9cbe4f02011-07-09 17:41:47 +000075 llvm::Type *TypeData;
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000076 llvm::Type *PaddingType;
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000077 unsigned UIntData;
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000078 bool BoolData0;
79 bool BoolData1;
Rafael Espindolab48280b2012-07-31 02:44:24 +000080 bool InReg;
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000081 bool PaddingInReg;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000082
Rafael Espindolab48280b2012-07-31 02:44:24 +000083 ABIArgInfo(Kind K, llvm::Type *TD, unsigned UI, bool B0, bool B1, bool IR,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000084 bool PIR, llvm::Type* P)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +000085 : TheKind(K), TypeData(TD), PaddingType(P), UIntData(UI), BoolData0(B0),
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000086 BoolData1(B1), InReg(IR), PaddingInReg(PIR) {}
Anders Carlsson0a8f8472009-09-16 15:53:40 +000087
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000088 public:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +000089 ABIArgInfo() : TheKind(Direct), TypeData(0), UIntData(0) {}
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000090
Akira Hatanakaf0cc2082012-01-07 00:25:33 +000091 static ABIArgInfo getDirect(llvm::Type *T = 0, unsigned Offset = 0,
92 llvm::Type *Padding = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000093 return ABIArgInfo(Direct, T, Offset, false, false, false, false, Padding);
Rafael Espindolab48280b2012-07-31 02:44:24 +000094 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +000095 static ABIArgInfo getDirectInReg(llvm::Type *T = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000096 return ABIArgInfo(Direct, T, 0, false, false, true, false, 0);
Daniel Dunbar46327aa2009-02-03 06:17:37 +000097 }
Chris Lattner9cbe4f02011-07-09 17:41:47 +000098 static ABIArgInfo getExtend(llvm::Type *T = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000099 return ABIArgInfo(Extend, T, 0, false, false, false, false, 0);
Rafael Espindolab48280b2012-07-31 02:44:24 +0000100 }
101 static ABIArgInfo getExtendInReg(llvm::Type *T = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000102 return ABIArgInfo(Extend, T, 0, false, false, true, false, 0);
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000103 }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000104 static ABIArgInfo getIgnore() {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000105 return ABIArgInfo(Ignore, 0, 0, false, false, false, false, 0);
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000106 }
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000107 static ABIArgInfo getIndirect(unsigned Alignment, bool ByVal = true
Tim Northoverc264e162013-01-31 12:13:10 +0000108 , bool Realign = false
109 , llvm::Type *Padding = 0) {
110 return ABIArgInfo(Indirect, 0, Alignment, ByVal, Realign, false, false,
111 Padding);
Rafael Espindolab48280b2012-07-31 02:44:24 +0000112 }
113 static ABIArgInfo getIndirectInReg(unsigned Alignment, bool ByVal = true
114 , bool Realign = false) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000115 return ABIArgInfo(Indirect, 0, Alignment, ByVal, Realign, true, false, 0);
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000116 }
117 static ABIArgInfo getExpand() {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000118 return ABIArgInfo(Expand, 0, 0, false, false, false, false, 0);
119 }
120 static ABIArgInfo getExpandWithPadding(bool PaddingInReg,
121 llvm::Type *Padding) {
122 return ABIArgInfo(Expand, 0, 0, false, false, false, PaddingInReg,
123 Padding);
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000124 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000125
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000126 Kind getKind() const { return TheKind; }
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000127 bool isDirect() const { return TheKind == Direct; }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000128 bool isExtend() const { return TheKind == Extend; }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000129 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000130 bool isIndirect() const { return TheKind == Indirect; }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000131 bool isExpand() const { return TheKind == Expand; }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000132
Chris Lattner800588f2010-07-29 06:26:06 +0000133 bool canHaveCoerceToType() const {
134 return TheKind == Direct || TheKind == Extend;
135 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000136
Chris Lattner800588f2010-07-29 06:26:06 +0000137 // Direct/Extend accessors
Chris Lattner117e3f42010-07-30 04:02:24 +0000138 unsigned getDirectOffset() const {
139 assert((isDirect() || isExtend()) && "Not a direct or extend kind");
140 return UIntData;
141 }
Akira Hatanakaf0cc2082012-01-07 00:25:33 +0000142
143 llvm::Type *getPaddingType() const {
144 return PaddingType;
145 }
146
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000147 bool getPaddingInReg() const {
148 return PaddingInReg;
149 }
150
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000151 llvm::Type *getCoerceToType() const {
Chris Lattner800588f2010-07-29 06:26:06 +0000152 assert(canHaveCoerceToType() && "Invalid kind!");
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000153 return TypeData;
154 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000155
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000156 void setCoerceToType(llvm::Type *T) {
Chris Lattner800588f2010-07-29 06:26:06 +0000157 assert(canHaveCoerceToType() && "Invalid kind!");
158 TypeData = T;
159 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000160
Rafael Espindolab48280b2012-07-31 02:44:24 +0000161 bool getInReg() const {
162 assert((isDirect() || isExtend() || isIndirect()) && "Invalid kind!");
163 return InReg;
164 }
165
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000166 // Indirect accessors
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000167 unsigned getIndirectAlign() const {
168 assert(TheKind == Indirect && "Invalid kind!");
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000169 return UIntData;
170 }
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000171
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000172 bool getIndirectByVal() const {
173 assert(TheKind == Indirect && "Invalid kind!");
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000174 return BoolData0;
175 }
176
177 bool getIndirectRealign() const {
178 assert(TheKind == Indirect && "Invalid kind!");
179 return BoolData1;
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000180 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000181
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000182 void dump() const;
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000183 };
184
185 /// ABIInfo - Target specific hooks for defining how a type should be
186 /// passed or returned from functions.
187 class ABIInfo {
188 public:
Chris Lattnerea044322010-07-29 02:01:43 +0000189 CodeGen::CodeGenTypes &CGT;
John McCallbd7370a2013-02-28 19:01:20 +0000190 protected:
191 llvm::CallingConv::ID RuntimeCC;
192 public:
193 ABIInfo(CodeGen::CodeGenTypes &cgt)
194 : CGT(cgt), RuntimeCC(llvm::CallingConv::C) {}
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000195
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000196 virtual ~ABIInfo();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000197
Mark Lacey23630722013-10-06 01:33:34 +0000198 CodeGen::CGCXXABI &getCXXABI() const;
Chris Lattnerea044322010-07-29 02:01:43 +0000199 ASTContext &getContext() const;
200 llvm::LLVMContext &getVMContext() const;
Micah Villmow25a6a842012-10-08 16:25:52 +0000201 const llvm::DataLayout &getDataLayout() const;
John McCall64aa4b32013-04-16 22:48:15 +0000202 const TargetInfo &getTarget() const;
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000203
John McCallbd7370a2013-02-28 19:01:20 +0000204 /// Return the calling convention to use for system runtime
205 /// functions.
206 llvm::CallingConv::ID getRuntimeCC() const {
207 return RuntimeCC;
208 }
209
Chris Lattneree5dcd02010-07-29 02:31:05 +0000210 virtual void computeInfo(CodeGen::CGFunctionInfo &FI) const = 0;
Daniel Dunbarb53e3e72009-02-10 21:44:36 +0000211
212 /// EmitVAArg - Emit the target dependent code to load a value of
213 /// \arg Ty from the va_list pointed to by \arg VAListAddr.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000214
Daniel Dunbarb53e3e72009-02-10 21:44:36 +0000215 // FIXME: This is a gaping layering violation if we wanted to drop
216 // the ABI information any lower than CodeGen. Of course, for
217 // VAArg handling it has to be at this level; there is no way to
218 // abstract this out.
219 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
220 CodeGen::CodeGenFunction &CGF) const = 0;
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000221 };
222} // end namespace clang
223
224#endif