blob: 35780f1556dd3d295b4fd9f0fdf6f08a3c1aeb03 [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;
25
Daniel Dunbar6bad2652009-02-03 06:51:18 +000026 namespace CodeGen {
27 class CGFunctionInfo;
Daniel Dunbarb53e3e72009-02-10 21:44:36 +000028 class CodeGenFunction;
Chris Lattnerea044322010-07-29 02:01:43 +000029 class CodeGenTypes;
Daniel Dunbar6bad2652009-02-03 06:51:18 +000030 }
31
Chris Lattner2eb9cdd2010-07-28 23:46:15 +000032 // FIXME: All of this stuff should be part of the target interface
33 // somehow. It is currently here because it is not clear how to factor
34 // the targets to support this, since the Targets currently live in a
35 // layer below types n'stuff.
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000036
37 /// ABIArgInfo - Helper class to encapsulate information about how a
38 /// specific C type should be passed to or returned from a function.
39 class ABIArgInfo {
40 public:
41 enum Kind {
Chris Lattner117e3f42010-07-30 04:02:24 +000042 /// Direct - Pass the argument directly using the normal converted LLVM
43 /// type, or by coercing to another specified type stored in
44 /// 'CoerceToType'). If an offset is specified (in UIntData), then the
45 /// argument passed is offset by some number of bytes in the memory
Akira Hatanakaf0cc2082012-01-07 00:25:33 +000046 /// representation. A dummy argument is emitted before the real argument
47 /// if the specified type stored in "PaddingType" is not zero.
Chris Lattner117e3f42010-07-30 04:02:24 +000048 Direct,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000049
Chris Lattner117e3f42010-07-30 04:02:24 +000050 /// Extend - Valid only for integer argument types. Same as 'direct'
51 /// but also emit a zero/sign extension attribute.
52 Extend,
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000053
Chris Lattner117e3f42010-07-30 04:02:24 +000054 /// Indirect - Pass the argument indirectly via a hidden pointer
55 /// with the specified alignment (0 indicates default alignment).
56 Indirect,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000057
Chris Lattner117e3f42010-07-30 04:02:24 +000058 /// Ignore - Ignore the argument (treat as void). Useful for void and
59 /// empty structs.
60 Ignore,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000061
Chris Lattner117e3f42010-07-30 04:02:24 +000062 /// Expand - Only valid for aggregate argument types. The structure should
63 /// be expanded into consecutive arguments for its constituent fields.
64 /// Currently expand is only allowed on structures whose fields
65 /// are all scalar types or are themselves expandable types.
66 Expand,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000067
Daniel Dunbar0bcc5212009-02-03 06:30:17 +000068 KindFirst=Direct, KindLast=Expand
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000069 };
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000070
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000071 private:
72 Kind TheKind;
Chris Lattner9cbe4f02011-07-09 17:41:47 +000073 llvm::Type *TypeData;
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000074 llvm::Type *PaddingType;
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000075 unsigned UIntData;
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000076 bool BoolData0;
77 bool BoolData1;
Rafael Espindolab48280b2012-07-31 02:44:24 +000078 bool InReg;
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000079 bool PaddingInReg;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080
Rafael Espindolab48280b2012-07-31 02:44:24 +000081 ABIArgInfo(Kind K, llvm::Type *TD, unsigned UI, bool B0, bool B1, bool IR,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000082 bool PIR, llvm::Type* P)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +000083 : TheKind(K), TypeData(TD), PaddingType(P), UIntData(UI), BoolData0(B0),
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000084 BoolData1(B1), InReg(IR), PaddingInReg(PIR) {}
Anders Carlsson0a8f8472009-09-16 15:53:40 +000085
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000086 public:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +000087 ABIArgInfo() : TheKind(Direct), TypeData(0), UIntData(0) {}
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000088
Akira Hatanakaf0cc2082012-01-07 00:25:33 +000089 static ABIArgInfo getDirect(llvm::Type *T = 0, unsigned Offset = 0,
90 llvm::Type *Padding = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000091 return ABIArgInfo(Direct, T, Offset, false, false, false, false, Padding);
Rafael Espindolab48280b2012-07-31 02:44:24 +000092 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +000093 static ABIArgInfo getDirectInReg(llvm::Type *T = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000094 return ABIArgInfo(Direct, T, 0, false, false, true, false, 0);
Daniel Dunbar46327aa2009-02-03 06:17:37 +000095 }
Chris Lattner9cbe4f02011-07-09 17:41:47 +000096 static ABIArgInfo getExtend(llvm::Type *T = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000097 return ABIArgInfo(Extend, T, 0, false, false, false, false, 0);
Rafael Espindolab48280b2012-07-31 02:44:24 +000098 }
99 static ABIArgInfo getExtendInReg(llvm::Type *T = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000100 return ABIArgInfo(Extend, T, 0, false, false, true, false, 0);
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000101 }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000102 static ABIArgInfo getIgnore() {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000103 return ABIArgInfo(Ignore, 0, 0, false, false, false, false, 0);
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000104 }
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000105 static ABIArgInfo getIndirect(unsigned Alignment, bool ByVal = true
Tim Northoverc264e162013-01-31 12:13:10 +0000106 , bool Realign = false
107 , llvm::Type *Padding = 0) {
108 return ABIArgInfo(Indirect, 0, Alignment, ByVal, Realign, false, false,
109 Padding);
Rafael Espindolab48280b2012-07-31 02:44:24 +0000110 }
111 static ABIArgInfo getIndirectInReg(unsigned Alignment, bool ByVal = true
112 , bool Realign = false) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000113 return ABIArgInfo(Indirect, 0, Alignment, ByVal, Realign, true, false, 0);
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000114 }
115 static ABIArgInfo getExpand() {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000116 return ABIArgInfo(Expand, 0, 0, false, false, false, false, 0);
117 }
118 static ABIArgInfo getExpandWithPadding(bool PaddingInReg,
119 llvm::Type *Padding) {
120 return ABIArgInfo(Expand, 0, 0, false, false, false, PaddingInReg,
121 Padding);
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000122 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000123
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000124 Kind getKind() const { return TheKind; }
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000125 bool isDirect() const { return TheKind == Direct; }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000126 bool isExtend() const { return TheKind == Extend; }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000127 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000128 bool isIndirect() const { return TheKind == Indirect; }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000129 bool isExpand() const { return TheKind == Expand; }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000130
Chris Lattner800588f2010-07-29 06:26:06 +0000131 bool canHaveCoerceToType() const {
132 return TheKind == Direct || TheKind == Extend;
133 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000134
Chris Lattner800588f2010-07-29 06:26:06 +0000135 // Direct/Extend accessors
Chris Lattner117e3f42010-07-30 04:02:24 +0000136 unsigned getDirectOffset() const {
137 assert((isDirect() || isExtend()) && "Not a direct or extend kind");
138 return UIntData;
139 }
Akira Hatanakaf0cc2082012-01-07 00:25:33 +0000140
141 llvm::Type *getPaddingType() const {
142 return PaddingType;
143 }
144
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000145 bool getPaddingInReg() const {
146 return PaddingInReg;
147 }
148
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000149 llvm::Type *getCoerceToType() const {
Chris Lattner800588f2010-07-29 06:26:06 +0000150 assert(canHaveCoerceToType() && "Invalid kind!");
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000151 return TypeData;
152 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000153
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000154 void setCoerceToType(llvm::Type *T) {
Chris Lattner800588f2010-07-29 06:26:06 +0000155 assert(canHaveCoerceToType() && "Invalid kind!");
156 TypeData = T;
157 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000158
Rafael Espindolab48280b2012-07-31 02:44:24 +0000159 bool getInReg() const {
160 assert((isDirect() || isExtend() || isIndirect()) && "Invalid kind!");
161 return InReg;
162 }
163
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000164 // Indirect accessors
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000165 unsigned getIndirectAlign() const {
166 assert(TheKind == Indirect && "Invalid kind!");
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000167 return UIntData;
168 }
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000169
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000170 bool getIndirectByVal() const {
171 assert(TheKind == Indirect && "Invalid kind!");
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000172 return BoolData0;
173 }
174
175 bool getIndirectRealign() const {
176 assert(TheKind == Indirect && "Invalid kind!");
177 return BoolData1;
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000178 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000179
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000180 void dump() const;
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000181 };
182
183 /// ABIInfo - Target specific hooks for defining how a type should be
184 /// passed or returned from functions.
185 class ABIInfo {
186 public:
Chris Lattnerea044322010-07-29 02:01:43 +0000187 CodeGen::CodeGenTypes &CGT;
John McCallbd7370a2013-02-28 19:01:20 +0000188 protected:
189 llvm::CallingConv::ID RuntimeCC;
190 public:
191 ABIInfo(CodeGen::CodeGenTypes &cgt)
192 : CGT(cgt), RuntimeCC(llvm::CallingConv::C) {}
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000193
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000194 virtual ~ABIInfo();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000195
Chris Lattnerea044322010-07-29 02:01:43 +0000196 ASTContext &getContext() const;
197 llvm::LLVMContext &getVMContext() const;
Micah Villmow25a6a842012-10-08 16:25:52 +0000198 const llvm::DataLayout &getDataLayout() const;
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000199
John McCallbd7370a2013-02-28 19:01:20 +0000200 /// Return the calling convention to use for system runtime
201 /// functions.
202 llvm::CallingConv::ID getRuntimeCC() const {
203 return RuntimeCC;
204 }
205
Chris Lattneree5dcd02010-07-29 02:31:05 +0000206 virtual void computeInfo(CodeGen::CGFunctionInfo &FI) const = 0;
Daniel Dunbarb53e3e72009-02-10 21:44:36 +0000207
208 /// EmitVAArg - Emit the target dependent code to load a value of
209 /// \arg Ty from the va_list pointed to by \arg VAListAddr.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000210
Daniel Dunbarb53e3e72009-02-10 21:44:36 +0000211 // FIXME: This is a gaping layering violation if we wanted to drop
212 // the ABI information any lower than CodeGen. Of course, for
213 // VAArg handling it has to be at this level; there is no way to
214 // abstract this out.
215 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
216 CodeGen::CodeGenFunction &CGF) const = 0;
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000217 };
218} // end namespace clang
219
220#endif