blob: df6dc7216a47e02c9439a9855a66b4f645774ab6 [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 {
28 class CGFunctionInfo;
Daniel Dunbarb53e3e72009-02-10 21:44:36 +000029 class CodeGenFunction;
Chris Lattnerea044322010-07-29 02:01:43 +000030 class CodeGenTypes;
Daniel Dunbar6bad2652009-02-03 06:51:18 +000031 }
32
Chris Lattner2eb9cdd2010-07-28 23:46:15 +000033 // FIXME: All of this stuff should be part of the target interface
34 // somehow. It is currently here because it is not clear how to factor
35 // the targets to support this, since the Targets currently live in a
36 // layer below types n'stuff.
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000037
38 /// ABIArgInfo - Helper class to encapsulate information about how a
39 /// specific C type should be passed to or returned from a function.
40 class ABIArgInfo {
41 public:
42 enum Kind {
Chris Lattner117e3f42010-07-30 04:02:24 +000043 /// Direct - Pass the argument directly using the normal converted LLVM
44 /// type, or by coercing to another specified type stored in
45 /// 'CoerceToType'). If an offset is specified (in UIntData), then the
46 /// argument passed is offset by some number of bytes in the memory
Akira Hatanakaf0cc2082012-01-07 00:25:33 +000047 /// representation. A dummy argument is emitted before the real argument
48 /// if the specified type stored in "PaddingType" is not zero.
Chris Lattner117e3f42010-07-30 04:02:24 +000049 Direct,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000050
Chris Lattner117e3f42010-07-30 04:02:24 +000051 /// Extend - Valid only for integer argument types. Same as 'direct'
52 /// but also emit a zero/sign extension attribute.
53 Extend,
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000054
Chris Lattner117e3f42010-07-30 04:02:24 +000055 /// Indirect - Pass the argument indirectly via a hidden pointer
56 /// with the specified alignment (0 indicates default alignment).
57 Indirect,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000058
Chris Lattner117e3f42010-07-30 04:02:24 +000059 /// Ignore - Ignore the argument (treat as void). Useful for void and
60 /// empty structs.
61 Ignore,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000062
Chris Lattner117e3f42010-07-30 04:02:24 +000063 /// Expand - Only valid for aggregate argument types. The structure should
64 /// be expanded into consecutive arguments for its constituent fields.
65 /// Currently expand is only allowed on structures whose fields
66 /// are all scalar types or are themselves expandable types.
67 Expand,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000068
Daniel Dunbar0bcc5212009-02-03 06:30:17 +000069 KindFirst=Direct, KindLast=Expand
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000070 };
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000071
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000072 private:
73 Kind TheKind;
Chris Lattner9cbe4f02011-07-09 17:41:47 +000074 llvm::Type *TypeData;
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000075 llvm::Type *PaddingType;
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000076 unsigned UIntData;
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000077 bool BoolData0;
78 bool BoolData1;
Rafael Espindolab48280b2012-07-31 02:44:24 +000079 bool InReg;
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000080 bool PaddingInReg;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000081
Rafael Espindolab48280b2012-07-31 02:44:24 +000082 ABIArgInfo(Kind K, llvm::Type *TD, unsigned UI, bool B0, bool B1, bool IR,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000083 bool PIR, llvm::Type* P)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +000084 : TheKind(K), TypeData(TD), PaddingType(P), UIntData(UI), BoolData0(B0),
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000085 BoolData1(B1), InReg(IR), PaddingInReg(PIR) {}
Anders Carlsson0a8f8472009-09-16 15:53:40 +000086
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000087 public:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +000088 ABIArgInfo() : TheKind(Direct), TypeData(0), UIntData(0) {}
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000089
Akira Hatanakaf0cc2082012-01-07 00:25:33 +000090 static ABIArgInfo getDirect(llvm::Type *T = 0, unsigned Offset = 0,
91 llvm::Type *Padding = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000092 return ABIArgInfo(Direct, T, Offset, false, false, false, false, Padding);
Rafael Espindolab48280b2012-07-31 02:44:24 +000093 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +000094 static ABIArgInfo getDirectInReg(llvm::Type *T = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000095 return ABIArgInfo(Direct, T, 0, false, false, true, false, 0);
Daniel Dunbar46327aa2009-02-03 06:17:37 +000096 }
Chris Lattner9cbe4f02011-07-09 17:41:47 +000097 static ABIArgInfo getExtend(llvm::Type *T = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +000098 return ABIArgInfo(Extend, T, 0, false, false, false, false, 0);
Rafael Espindolab48280b2012-07-31 02:44:24 +000099 }
100 static ABIArgInfo getExtendInReg(llvm::Type *T = 0) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000101 return ABIArgInfo(Extend, T, 0, false, false, true, false, 0);
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000102 }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000103 static ABIArgInfo getIgnore() {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000104 return ABIArgInfo(Ignore, 0, 0, false, false, false, false, 0);
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000105 }
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000106 static ABIArgInfo getIndirect(unsigned Alignment, bool ByVal = true
Tim Northoverc264e162013-01-31 12:13:10 +0000107 , bool Realign = false
108 , llvm::Type *Padding = 0) {
109 return ABIArgInfo(Indirect, 0, Alignment, ByVal, Realign, false, false,
110 Padding);
Rafael Espindolab48280b2012-07-31 02:44:24 +0000111 }
112 static ABIArgInfo getIndirectInReg(unsigned Alignment, bool ByVal = true
113 , bool Realign = false) {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000114 return ABIArgInfo(Indirect, 0, Alignment, ByVal, Realign, true, false, 0);
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000115 }
116 static ABIArgInfo getExpand() {
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000117 return ABIArgInfo(Expand, 0, 0, false, false, false, false, 0);
118 }
119 static ABIArgInfo getExpandWithPadding(bool PaddingInReg,
120 llvm::Type *Padding) {
121 return ABIArgInfo(Expand, 0, 0, false, false, false, PaddingInReg,
122 Padding);
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000123 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000124
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000125 Kind getKind() const { return TheKind; }
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000126 bool isDirect() const { return TheKind == Direct; }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000127 bool isExtend() const { return TheKind == Extend; }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000128 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000129 bool isIndirect() const { return TheKind == Indirect; }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000130 bool isExpand() const { return TheKind == Expand; }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000131
Chris Lattner800588f2010-07-29 06:26:06 +0000132 bool canHaveCoerceToType() const {
133 return TheKind == Direct || TheKind == Extend;
134 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000135
Chris Lattner800588f2010-07-29 06:26:06 +0000136 // Direct/Extend accessors
Chris Lattner117e3f42010-07-30 04:02:24 +0000137 unsigned getDirectOffset() const {
138 assert((isDirect() || isExtend()) && "Not a direct or extend kind");
139 return UIntData;
140 }
Akira Hatanakaf0cc2082012-01-07 00:25:33 +0000141
142 llvm::Type *getPaddingType() const {
143 return PaddingType;
144 }
145
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000146 bool getPaddingInReg() const {
147 return PaddingInReg;
148 }
149
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000150 llvm::Type *getCoerceToType() const {
Chris Lattner800588f2010-07-29 06:26:06 +0000151 assert(canHaveCoerceToType() && "Invalid kind!");
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000152 return TypeData;
153 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000154
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000155 void setCoerceToType(llvm::Type *T) {
Chris Lattner800588f2010-07-29 06:26:06 +0000156 assert(canHaveCoerceToType() && "Invalid kind!");
157 TypeData = T;
158 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000159
Rafael Espindolab48280b2012-07-31 02:44:24 +0000160 bool getInReg() const {
161 assert((isDirect() || isExtend() || isIndirect()) && "Invalid kind!");
162 return InReg;
163 }
164
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000165 // Indirect accessors
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000166 unsigned getIndirectAlign() const {
167 assert(TheKind == Indirect && "Invalid kind!");
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000168 return UIntData;
169 }
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000170
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000171 bool getIndirectByVal() const {
172 assert(TheKind == Indirect && "Invalid kind!");
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000173 return BoolData0;
174 }
175
176 bool getIndirectRealign() const {
177 assert(TheKind == Indirect && "Invalid kind!");
178 return BoolData1;
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000179 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000180
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000181 void dump() const;
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000182 };
183
184 /// ABIInfo - Target specific hooks for defining how a type should be
185 /// passed or returned from functions.
186 class ABIInfo {
187 public:
Chris Lattnerea044322010-07-29 02:01:43 +0000188 CodeGen::CodeGenTypes &CGT;
John McCallbd7370a2013-02-28 19:01:20 +0000189 protected:
190 llvm::CallingConv::ID RuntimeCC;
191 public:
192 ABIInfo(CodeGen::CodeGenTypes &cgt)
193 : CGT(cgt), RuntimeCC(llvm::CallingConv::C) {}
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000194
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000195 virtual ~ABIInfo();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000196
Chris Lattnerea044322010-07-29 02:01:43 +0000197 ASTContext &getContext() const;
198 llvm::LLVMContext &getVMContext() const;
Micah Villmow25a6a842012-10-08 16:25:52 +0000199 const llvm::DataLayout &getDataLayout() const;
John McCall64aa4b32013-04-16 22:48:15 +0000200 const TargetInfo &getTarget() const;
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000201
John McCallbd7370a2013-02-28 19:01:20 +0000202 /// Return the calling convention to use for system runtime
203 /// functions.
204 llvm::CallingConv::ID getRuntimeCC() const {
205 return RuntimeCC;
206 }
207
Chris Lattneree5dcd02010-07-29 02:31:05 +0000208 virtual void computeInfo(CodeGen::CGFunctionInfo &FI) const = 0;
Daniel Dunbarb53e3e72009-02-10 21:44:36 +0000209
210 /// EmitVAArg - Emit the target dependent code to load a value of
211 /// \arg Ty from the va_list pointed to by \arg VAListAddr.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000212
Daniel Dunbarb53e3e72009-02-10 21:44:36 +0000213 // FIXME: This is a gaping layering violation if we wanted to drop
214 // the ABI information any lower than CodeGen. Of course, for
215 // VAArg handling it has to be at this level; there is no way to
216 // abstract this out.
217 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
218 CodeGen::CodeGenFunction &CGF) const = 0;
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000219 };
220} // end namespace clang
221
222#endif