blob: 91b7742557f562e6e9d275242668b32bd33679ec [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"
Chris Lattner958c53c2010-06-29 19:21:36 +000014#include "llvm/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;
Chris Lattnerea044322010-07-29 02:01:43 +000019 class TargetData;
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
45 /// representation.
46 Direct,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000047
Chris Lattner117e3f42010-07-30 04:02:24 +000048 /// Extend - Valid only for integer argument types. Same as 'direct'
49 /// but also emit a zero/sign extension attribute.
50 Extend,
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000051
Chris Lattner117e3f42010-07-30 04:02:24 +000052 /// Indirect - Pass the argument indirectly via a hidden pointer
53 /// with the specified alignment (0 indicates default alignment).
54 Indirect,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000055
Chris Lattner117e3f42010-07-30 04:02:24 +000056 /// Ignore - Ignore the argument (treat as void). Useful for void and
57 /// empty structs.
58 Ignore,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000059
Chris Lattner117e3f42010-07-30 04:02:24 +000060 /// 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 Korobeynikovc4a59eb2009-06-05 22:08:42 +000065
Daniel Dunbar0bcc5212009-02-03 06:30:17 +000066 KindFirst=Direct, KindLast=Expand
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000067 };
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000068
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000069 private:
70 Kind TheKind;
Chris Lattner958c53c2010-06-29 19:21:36 +000071 llvm::PATypeHolder TypeData;
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000072 unsigned UIntData;
Anders Carlsson0a8f8472009-09-16 15:53:40 +000073 bool BoolData;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000074
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000075 ABIArgInfo(Kind K, const llvm::Type *TD=0,
Anders Carlsson0a8f8472009-09-16 15:53:40 +000076 unsigned UI=0, bool B = false)
77 : TheKind(K), TypeData(TD), UIntData(UI), BoolData(B) {}
78
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000079 public:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +000080 ABIArgInfo() : TheKind(Direct), TypeData(0), UIntData(0) {}
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000081
Chris Lattner117e3f42010-07-30 04:02:24 +000082 static ABIArgInfo getDirect(const llvm::Type *T = 0, unsigned Offset = 0) {
83 return ABIArgInfo(Direct, T, Offset);
Daniel Dunbar46327aa2009-02-03 06:17:37 +000084 }
Chris Lattnereb518b42010-07-29 21:42:50 +000085 static ABIArgInfo getExtend(const llvm::Type *T = 0) {
Chris Lattner117e3f42010-07-30 04:02:24 +000086 return ABIArgInfo(Extend, T, 0);
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000087 }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000088 static ABIArgInfo getIgnore() {
89 return ABIArgInfo(Ignore);
90 }
Anders Carlsson0a8f8472009-09-16 15:53:40 +000091 static ABIArgInfo getIndirect(unsigned Alignment, bool ByVal = true) {
92 return ABIArgInfo(Indirect, 0, Alignment, ByVal);
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000093 }
94 static ABIArgInfo getExpand() {
95 return ABIArgInfo(Expand);
96 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000097
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000098 Kind getKind() const { return TheKind; }
Daniel Dunbar46327aa2009-02-03 06:17:37 +000099 bool isDirect() const { return TheKind == Direct; }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000100 bool isExtend() const { return TheKind == Extend; }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000101 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000102 bool isIndirect() const { return TheKind == Indirect; }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000103 bool isExpand() const { return TheKind == Expand; }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000104
Chris Lattner800588f2010-07-29 06:26:06 +0000105 bool canHaveCoerceToType() const {
106 return TheKind == Direct || TheKind == Extend;
107 }
108
109 // Direct/Extend accessors
Chris Lattner117e3f42010-07-30 04:02:24 +0000110 unsigned getDirectOffset() const {
111 assert((isDirect() || isExtend()) && "Not a direct or extend kind");
112 return UIntData;
113 }
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000114 const llvm::Type *getCoerceToType() const {
Chris Lattner800588f2010-07-29 06:26:06 +0000115 assert(canHaveCoerceToType() && "Invalid kind!");
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000116 return TypeData;
117 }
Chris Lattner800588f2010-07-29 06:26:06 +0000118
119 void setCoerceToType(const llvm::Type *T) {
120 assert(canHaveCoerceToType() && "Invalid kind!");
121 TypeData = T;
122 }
Chris Lattner117e3f42010-07-30 04:02:24 +0000123
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000124 // Indirect accessors
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000125 unsigned getIndirectAlign() const {
126 assert(TheKind == Indirect && "Invalid kind!");
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000127 return UIntData;
128 }
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000129
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000130 bool getIndirectByVal() const {
131 assert(TheKind == Indirect && "Invalid kind!");
132 return BoolData;
133 }
134
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000135 void dump() const;
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000136 };
137
138 /// ABIInfo - Target specific hooks for defining how a type should be
139 /// passed or returned from functions.
140 class ABIInfo {
141 public:
Chris Lattnerea044322010-07-29 02:01:43 +0000142 CodeGen::CodeGenTypes &CGT;
143
144 ABIInfo(CodeGen::CodeGenTypes &cgt) : CGT(cgt) {}
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000145 virtual ~ABIInfo();
Chris Lattnerea044322010-07-29 02:01:43 +0000146
147 ASTContext &getContext() const;
148 llvm::LLVMContext &getVMContext() const;
149 const llvm::TargetData &getTargetData() const;
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000150
Chris Lattneree5dcd02010-07-29 02:31:05 +0000151 virtual void computeInfo(CodeGen::CGFunctionInfo &FI) const = 0;
Daniel Dunbarb53e3e72009-02-10 21:44:36 +0000152
153 /// EmitVAArg - Emit the target dependent code to load a value of
154 /// \arg Ty from the va_list pointed to by \arg VAListAddr.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000155
Daniel Dunbarb53e3e72009-02-10 21:44:36 +0000156 // FIXME: This is a gaping layering violation if we wanted to drop
157 // the ABI information any lower than CodeGen. Of course, for
158 // VAArg handling it has to be at this level; there is no way to
159 // abstract this out.
160 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
161 CodeGen::CodeGenFunction &CGF) const = 0;
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +0000162 };
163} // end namespace clang
164
165#endif