blob: e22ab8eb7a28144aad6b7b4749a0056c2481f4cd [file] [log] [blame]
Daniel Dunbar6d6b0d32009-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 Korobeynikov244360d2009-06-05 22:08:42 +000013#include "clang/AST/Type.h"
Chris Lattnercccaad92010-06-29 19:21:36 +000014#include "llvm/Type.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000015
Daniel Dunbar313321e2009-02-03 05:31:23 +000016namespace llvm {
Anton Korobeynikov244360d2009-06-05 22:08:42 +000017 class Value;
Benjamin Kramer9cd050a2009-08-11 17:46:57 +000018 class LLVMContext;
Daniel Dunbar313321e2009-02-03 05:31:23 +000019}
20
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000021namespace clang {
Daniel Dunbar32931eb2009-02-03 06:51:18 +000022 class ASTContext;
23
24 // FIXME: This is a layering issue if we want to move ABIInfo
25 // down. Fortunately CGFunctionInfo has no real tie to CodeGen.
26 namespace CodeGen {
27 class CGFunctionInfo;
Daniel Dunbare46506e2009-02-10 21:44:36 +000028 class CodeGenFunction;
Daniel Dunbar32931eb2009-02-03 06:51:18 +000029 }
30
Chris Lattner4b8585e2010-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 Dunbar6d6b0d32009-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 {
Daniel Dunbar67dace892009-02-03 06:17:37 +000041 Direct, /// Pass the argument directly using the normal
Daniel Dunbarb8b1c672009-02-05 08:00:50 +000042 /// converted LLVM type. Complex and structure types
43 /// are passed using first class aggregates.
Anton Korobeynikov244360d2009-06-05 22:08:42 +000044
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000045 Extend, /// Valid only for integer argument types. Same as 'direct'
46 /// but also emit a zero/sign extension attribute.
47
Daniel Dunbarb8b1c672009-02-05 08:00:50 +000048 Indirect, /// Pass the argument indirectly via a hidden pointer
49 /// with the specified alignment (0 indicates default
50 /// alignment).
Anton Korobeynikov244360d2009-06-05 22:08:42 +000051
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000052 Ignore, /// Ignore the argument (treat as void). Useful for
53 /// void and empty structs.
Anton Korobeynikov244360d2009-06-05 22:08:42 +000054
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000055 Coerce, /// Only valid for aggregate return types, the argument
56 /// should be accessed by coercion to a provided type.
Anton Korobeynikov244360d2009-06-05 22:08:42 +000057
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000058 Expand, /// Only valid for aggregate argument types. The
59 /// structure should be expanded into consecutive
60 /// arguments for its constituent fields. Currently
61 /// expand is only allowed on structures whose fields
62 /// are all scalar types or are themselves expandable
63 /// types.
Anton Korobeynikov244360d2009-06-05 22:08:42 +000064
Daniel Dunbar01362822009-02-03 06:30:17 +000065 KindFirst=Direct, KindLast=Expand
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000066 };
Anton Korobeynikov244360d2009-06-05 22:08:42 +000067
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000068 private:
69 Kind TheKind;
Chris Lattnercccaad92010-06-29 19:21:36 +000070 llvm::PATypeHolder TypeData;
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000071 unsigned UIntData;
Anders Carlsson20759ad2009-09-16 15:53:40 +000072 bool BoolData;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000073
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000074 ABIArgInfo(Kind K, const llvm::Type *TD=0,
Anders Carlsson20759ad2009-09-16 15:53:40 +000075 unsigned UI=0, bool B = false)
76 : TheKind(K), TypeData(TD), UIntData(UI), BoolData(B) {}
77
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000078 public:
Daniel Dunbar01362822009-02-03 06:30:17 +000079 ABIArgInfo() : TheKind(Direct), TypeData(0), UIntData(0) {}
Daniel Dunbar313321e2009-02-03 05:31:23 +000080
Anton Korobeynikov244360d2009-06-05 22:08:42 +000081 static ABIArgInfo getDirect() {
82 return ABIArgInfo(Direct);
Daniel Dunbar67dace892009-02-03 06:17:37 +000083 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000084 static ABIArgInfo getExtend() {
85 return ABIArgInfo(Extend);
86 }
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000087 static ABIArgInfo getIgnore() {
88 return ABIArgInfo(Ignore);
89 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +000090 static ABIArgInfo getCoerce(const llvm::Type *T) {
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000091 return ABIArgInfo(Coerce, T);
92 }
Anders Carlsson20759ad2009-09-16 15:53:40 +000093 static ABIArgInfo getIndirect(unsigned Alignment, bool ByVal = true) {
94 return ABIArgInfo(Indirect, 0, Alignment, ByVal);
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000095 }
96 static ABIArgInfo getExpand() {
97 return ABIArgInfo(Expand);
98 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +000099
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +0000100 Kind getKind() const { return TheKind; }
Daniel Dunbar67dace892009-02-03 06:17:37 +0000101 bool isDirect() const { return TheKind == Direct; }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000102 bool isExtend() const { return TheKind == Extend; }
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +0000103 bool isIgnore() const { return TheKind == Ignore; }
104 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000105 bool isIndirect() const { return TheKind == Indirect; }
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +0000106 bool isExpand() const { return TheKind == Expand; }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000107
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +0000108 // Coerce accessors
109 const llvm::Type *getCoerceToType() const {
110 assert(TheKind == Coerce && "Invalid kind!");
111 return TypeData;
112 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000113
Anders Carlsson20759ad2009-09-16 15:53:40 +0000114 // Indirect accessors
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000115 unsigned getIndirectAlign() const {
116 assert(TheKind == Indirect && "Invalid kind!");
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +0000117 return UIntData;
118 }
Daniel Dunbar56e75522009-02-04 23:24:38 +0000119
Anders Carlsson20759ad2009-09-16 15:53:40 +0000120 bool getIndirectByVal() const {
121 assert(TheKind == Indirect && "Invalid kind!");
122 return BoolData;
123 }
124
Daniel Dunbar56e75522009-02-04 23:24:38 +0000125 void dump() const;
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +0000126 };
127
128 /// ABIInfo - Target specific hooks for defining how a type should be
129 /// passed or returned from functions.
130 class ABIInfo {
131 public:
132 virtual ~ABIInfo();
Daniel Dunbar32931eb2009-02-03 06:51:18 +0000133
134 virtual void computeInfo(CodeGen::CGFunctionInfo &FI,
Owen Anderson170229f2009-07-14 23:10:40 +0000135 ASTContext &Ctx,
Chris Lattner1d7c9f72010-06-29 01:08:48 +0000136 llvm::LLVMContext &VMContext,
137 // This is the preferred type for argument lowering
138 // which can be used to generate better IR.
139 const llvm::Type *const *PrefTypes = 0,
140 unsigned NumPrefTypes = 0) const = 0;
Daniel Dunbare46506e2009-02-10 21:44:36 +0000141
142 /// EmitVAArg - Emit the target dependent code to load a value of
143 /// \arg Ty from the va_list pointed to by \arg VAListAddr.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000144
Daniel Dunbare46506e2009-02-10 21:44:36 +0000145 // FIXME: This is a gaping layering violation if we wanted to drop
146 // the ABI information any lower than CodeGen. Of course, for
147 // VAArg handling it has to be at this level; there is no way to
148 // abstract this out.
149 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
150 CodeGen::CodeGenFunction &CGF) const = 0;
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +0000151 };
152} // end namespace clang
153
154#endif