blob: 1da1689a65919446275324fc271da311f29911c2 [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"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000015
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000016namespace llvm {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000017 class Value;
Benjamin Kramerf21efe92009-08-11 17:46:57 +000018 class LLVMContext;
Micah Villmow25a6a842012-10-08 16:25:52 +000019 class DataLayout;
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000020}
21
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000022namespace clang {
Daniel Dunbar6bad2652009-02-03 06:51:18 +000023 class ASTContext;
24
Daniel Dunbar6bad2652009-02-03 06:51:18 +000025 namespace CodeGen {
26 class CGFunctionInfo;
Daniel Dunbarb53e3e72009-02-10 21:44:36 +000027 class CodeGenFunction;
Chris Lattnerea044322010-07-29 02:01:43 +000028 class CodeGenTypes;
Daniel Dunbar6bad2652009-02-03 06:51:18 +000029 }
30
Chris Lattner2eb9cdd2010-07-28 23:46:15 +000031 // 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 Dunbar9eb5c6d2009-02-03 01:05:53 +000035
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 Lattner117e3f42010-07-30 04:02:24 +000041 /// 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
Akira Hatanakaf0cc2082012-01-07 00:25:33 +000045 /// representation. A dummy argument is emitted before the real argument
46 /// if the specified type stored in "PaddingType" is not zero.
Chris Lattner117e3f42010-07-30 04:02:24 +000047 Direct,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000048
Chris Lattner117e3f42010-07-30 04:02:24 +000049 /// Extend - Valid only for integer argument types. Same as 'direct'
50 /// but also emit a zero/sign extension attribute.
51 Extend,
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000052
Chris Lattner117e3f42010-07-30 04:02:24 +000053 /// Indirect - Pass the argument indirectly via a hidden pointer
54 /// with the specified alignment (0 indicates default alignment).
55 Indirect,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000056
Chris Lattner117e3f42010-07-30 04:02:24 +000057 /// Ignore - Ignore the argument (treat as void). Useful for void and
58 /// empty structs.
59 Ignore,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000060
Chris Lattner117e3f42010-07-30 04:02:24 +000061 /// Expand - Only valid for aggregate argument types. The structure should
62 /// be expanded into consecutive arguments for its constituent fields.
63 /// Currently expand is only allowed on structures whose fields
64 /// are all scalar types or are themselves expandable types.
65 Expand,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000066
Daniel Dunbar0bcc5212009-02-03 06:30:17 +000067 KindFirst=Direct, KindLast=Expand
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000068 };
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000069
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000070 private:
71 Kind TheKind;
Chris Lattner9cbe4f02011-07-09 17:41:47 +000072 llvm::Type *TypeData;
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000073 llvm::Type *PaddingType;
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000074 unsigned UIntData;
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000075 bool BoolData0;
76 bool BoolData1;
Rafael Espindolab48280b2012-07-31 02:44:24 +000077 bool InReg;
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000078 bool PaddingInReg;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000079
Rafael Espindolab48280b2012-07-31 02:44:24 +000080 ABIArgInfo(Kind K, llvm::Type *TD, unsigned UI, bool B0, bool B1, bool IR,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000081 bool PIR, llvm::Type* P)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +000082 : TheKind(K), TypeData(TD), PaddingType(P), UIntData(UI), BoolData0(B0),
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000083 BoolData1(B1), InReg(IR), PaddingInReg(PIR) {}
Anders Carlsson0a8f8472009-09-16 15:53:40 +000084
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000085 public:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +000086 ABIArgInfo() : TheKind(Direct), TypeData(0), UIntData(0) {}
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000087
Akira Hatanakaf0cc2082012-01-07 00:25:33 +000088 static ABIArgInfo getDirect(llvm::Type *T = 0, unsigned Offset = 0,
89 llvm::Type *Padding = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000090 return ABIArgInfo(Direct, T, Offset, false, false, false, false, Padding);
Rafael Espindolab48280b2012-07-31 02:44:24 +000091 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +000092 static ABIArgInfo getDirectInReg(llvm::Type *T = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000093 return ABIArgInfo(Direct, T, 0, false, false, true, false, 0);
Daniel Dunbar46327aa2009-02-03 06:17:37 +000094 }
Chris Lattner9cbe4f02011-07-09 17:41:47 +000095 static ABIArgInfo getExtend(llvm::Type *T = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000096 return ABIArgInfo(Extend, T, 0, false, false, false, false, 0);
Rafael Espindolab48280b2012-07-31 02:44:24 +000097 }
98 static ABIArgInfo getExtendInReg(llvm::Type *T = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000099 return ABIArgInfo(Extend, T, 0, false, false, true, false, 0);
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000100 }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000101 static ABIArgInfo getIgnore() {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000102 return ABIArgInfo(Ignore, 0, 0, false, false, false, false, 0);
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000103 }
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000104 static ABIArgInfo getIndirect(unsigned Alignment, bool ByVal = true
105 , bool Realign = false) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000106 return ABIArgInfo(Indirect, 0, Alignment, ByVal, Realign, false, false, 0);
Rafael Espindolab48280b2012-07-31 02:44:24 +0000107 }
108 static ABIArgInfo getIndirectInReg(unsigned Alignment, bool ByVal = true
109 , bool Realign = false) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000110 return ABIArgInfo(Indirect, 0, Alignment, ByVal, Realign, true, false, 0);
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000111 }
112 static ABIArgInfo getExpand() {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000113 return ABIArgInfo(Expand, 0, 0, false, false, false, false, 0);
114 }
115 static ABIArgInfo getExpandWithPadding(bool PaddingInReg,
116 llvm::Type *Padding) {
117 return ABIArgInfo(Expand, 0, 0, false, false, false, PaddingInReg,
118 Padding);
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000119 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000120
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000121 Kind getKind() const { return TheKind; }
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000122 bool isDirect() const { return TheKind == Direct; }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000123 bool isExtend() const { return TheKind == Extend; }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000124 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000125 bool isIndirect() const { return TheKind == Indirect; }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000126 bool isExpand() const { return TheKind == Expand; }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000127
Chris Lattner800588f2010-07-29 06:26:06 +0000128 bool canHaveCoerceToType() const {
129 return TheKind == Direct || TheKind == Extend;
130 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000131
Chris Lattner800588f2010-07-29 06:26:06 +0000132 // Direct/Extend accessors
Chris Lattner117e3f42010-07-30 04:02:24 +0000133 unsigned getDirectOffset() const {
134 assert((isDirect() || isExtend()) && "Not a direct or extend kind");
135 return UIntData;
136 }
Akira Hatanakaf0cc2082012-01-07 00:25:33 +0000137
138 llvm::Type *getPaddingType() const {
139 return PaddingType;
140 }
141
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000142 bool getPaddingInReg() const {
143 return PaddingInReg;
144 }
145
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000146 llvm::Type *getCoerceToType() const {
Chris Lattner800588f2010-07-29 06:26:06 +0000147 assert(canHaveCoerceToType() && "Invalid kind!");
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000148 return TypeData;
149 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000150
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000151 void setCoerceToType(llvm::Type *T) {
Chris Lattner800588f2010-07-29 06:26:06 +0000152 assert(canHaveCoerceToType() && "Invalid kind!");
153 TypeData = T;
154 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000155
Rafael Espindolab48280b2012-07-31 02:44:24 +0000156 bool getInReg() const {
157 assert((isDirect() || isExtend() || isIndirect()) && "Invalid kind!");
158 return InReg;
159 }
160
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000161 // Indirect accessors
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000162 unsigned getIndirectAlign() const {
163 assert(TheKind == Indirect && "Invalid kind!");
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000164 return UIntData;
165 }
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000166
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000167 bool getIndirectByVal() const {
168 assert(TheKind == Indirect && "Invalid kind!");
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000169 return BoolData0;
170 }
171
172 bool getIndirectRealign() const {
173 assert(TheKind == Indirect && "Invalid kind!");
174 return BoolData1;
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000175 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000176
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000177 void dump() const;
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000178 };
179
180 /// ABIInfo - Target specific hooks for defining how a type should be
181 /// passed or returned from functions.
182 class ABIInfo {
183 public:
Chris Lattnerea044322010-07-29 02:01:43 +0000184 CodeGen::CodeGenTypes &CGT;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000185
Chris Lattnerea044322010-07-29 02:01:43 +0000186 ABIInfo(CodeGen::CodeGenTypes &cgt) : CGT(cgt) {}
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000187 virtual ~ABIInfo();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000188
Chris Lattnerea044322010-07-29 02:01:43 +0000189 ASTContext &getContext() const;
190 llvm::LLVMContext &getVMContext() const;
Micah Villmow25a6a842012-10-08 16:25:52 +0000191 const llvm::DataLayout &getDataLayout() const;
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000192
Chris Lattneree5dcd02010-07-29 02:31:05 +0000193 virtual void computeInfo(CodeGen::CGFunctionInfo &FI) const = 0;
Daniel Dunbarb53e3e72009-02-10 21:44:36 +0000194
195 /// EmitVAArg - Emit the target dependent code to load a value of
196 /// \arg Ty from the va_list pointed to by \arg VAListAddr.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000197
Daniel Dunbarb53e3e72009-02-10 21:44:36 +0000198 // FIXME: This is a gaping layering violation if we wanted to drop
199 // the ABI information any lower than CodeGen. Of course, for
200 // VAArg handling it has to be at this level; there is no way to
201 // abstract this out.
202 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
203 CodeGen::CodeGenFunction &CGF) const = 0;
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000204 };
205} // end namespace clang
206
207#endif