blob: 642fddb5c8dd8651bd94a036dda4b25cee1942fb [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;
Chris Lattner2b037972010-07-29 02:01:43 +000019 class TargetData;
Daniel Dunbar313321e2009-02-03 05:31:23 +000020}
21
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000022namespace clang {
Daniel Dunbar32931eb2009-02-03 06:51:18 +000023 class ASTContext;
24
Daniel Dunbar32931eb2009-02-03 06:51:18 +000025 namespace CodeGen {
26 class CGFunctionInfo;
Daniel Dunbare46506e2009-02-10 21:44:36 +000027 class CodeGenFunction;
Chris Lattner2b037972010-07-29 02:01:43 +000028 class CodeGenTypes;
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 {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +000041 Direct, /// Pass the argument directly using the normal converted LLVM
42 /// type, or by coercing to another specified type
43 /// (stored in 'CoerceToType').
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
Chris Lattner2b037972010-07-29 02:01:43 +000055 Expand, /// Only valid for aggregate argument types. The
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000056 /// structure should be expanded into consecutive
57 /// arguments for its constituent fields. Currently
58 /// expand is only allowed on structures whose fields
59 /// are all scalar types or are themselves expandable
60 /// types.
Anton Korobeynikov244360d2009-06-05 22:08:42 +000061
Daniel Dunbar01362822009-02-03 06:30:17 +000062 KindFirst=Direct, KindLast=Expand
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000063 };
Anton Korobeynikov244360d2009-06-05 22:08:42 +000064
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000065 private:
66 Kind TheKind;
Chris Lattnercccaad92010-06-29 19:21:36 +000067 llvm::PATypeHolder TypeData;
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000068 unsigned UIntData;
Anders Carlsson20759ad2009-09-16 15:53:40 +000069 bool BoolData;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000070
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000071 ABIArgInfo(Kind K, const llvm::Type *TD=0,
Anders Carlsson20759ad2009-09-16 15:53:40 +000072 unsigned UI=0, bool B = false)
73 : TheKind(K), TypeData(TD), UIntData(UI), BoolData(B) {}
74
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000075 public:
Daniel Dunbar01362822009-02-03 06:30:17 +000076 ABIArgInfo() : TheKind(Direct), TypeData(0), UIntData(0) {}
Daniel Dunbar313321e2009-02-03 05:31:23 +000077
Chris Lattnerfe34c1d2010-07-29 06:26:06 +000078 static ABIArgInfo getDirect(const llvm::Type *T = 0) {
79 return ABIArgInfo(Direct, T);
Daniel Dunbar67dace892009-02-03 06:17:37 +000080 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000081 static ABIArgInfo getExtend() {
82 return ABIArgInfo(Extend);
83 }
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000084 static ABIArgInfo getIgnore() {
85 return ABIArgInfo(Ignore);
86 }
Anders Carlsson20759ad2009-09-16 15:53:40 +000087 static ABIArgInfo getIndirect(unsigned Alignment, bool ByVal = true) {
88 return ABIArgInfo(Indirect, 0, Alignment, ByVal);
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000089 }
90 static ABIArgInfo getExpand() {
91 return ABIArgInfo(Expand);
92 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +000093
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000094 Kind getKind() const { return TheKind; }
Daniel Dunbar67dace892009-02-03 06:17:37 +000095 bool isDirect() const { return TheKind == Direct; }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000096 bool isExtend() const { return TheKind == Extend; }
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000097 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbarb8b1c672009-02-05 08:00:50 +000098 bool isIndirect() const { return TheKind == Indirect; }
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000099 bool isExpand() const { return TheKind == Expand; }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000100
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000101 bool canHaveCoerceToType() const {
102 return TheKind == Direct || TheKind == Extend;
103 }
104
105 // Direct/Extend accessors
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +0000106 const llvm::Type *getCoerceToType() const {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000107 assert(canHaveCoerceToType() && "Invalid kind!");
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +0000108 return TypeData;
109 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000110
111 void setCoerceToType(const llvm::Type *T) {
112 assert(canHaveCoerceToType() && "Invalid kind!");
113 TypeData = T;
114 }
Anders Carlsson20759ad2009-09-16 15:53:40 +0000115 // Indirect accessors
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000116 unsigned getIndirectAlign() const {
117 assert(TheKind == Indirect && "Invalid kind!");
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +0000118 return UIntData;
119 }
Daniel Dunbar56e75522009-02-04 23:24:38 +0000120
Anders Carlsson20759ad2009-09-16 15:53:40 +0000121 bool getIndirectByVal() const {
122 assert(TheKind == Indirect && "Invalid kind!");
123 return BoolData;
124 }
125
Daniel Dunbar56e75522009-02-04 23:24:38 +0000126 void dump() const;
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +0000127 };
128
129 /// ABIInfo - Target specific hooks for defining how a type should be
130 /// passed or returned from functions.
131 class ABIInfo {
132 public:
Chris Lattner2b037972010-07-29 02:01:43 +0000133 CodeGen::CodeGenTypes &CGT;
134
135 ABIInfo(CodeGen::CodeGenTypes &cgt) : CGT(cgt) {}
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +0000136 virtual ~ABIInfo();
Chris Lattner2b037972010-07-29 02:01:43 +0000137
138 ASTContext &getContext() const;
139 llvm::LLVMContext &getVMContext() const;
140 const llvm::TargetData &getTargetData() const;
Daniel Dunbar32931eb2009-02-03 06:51:18 +0000141
Chris Lattner22326a12010-07-29 02:31:05 +0000142 virtual void computeInfo(CodeGen::CGFunctionInfo &FI) const = 0;
Daniel Dunbare46506e2009-02-10 21:44:36 +0000143
144 /// EmitVAArg - Emit the target dependent code to load a value of
145 /// \arg Ty from the va_list pointed to by \arg VAListAddr.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000146
Daniel Dunbare46506e2009-02-10 21:44:36 +0000147 // FIXME: This is a gaping layering violation if we wanted to drop
148 // the ABI information any lower than CodeGen. Of course, for
149 // VAArg handling it has to be at this level; there is no way to
150 // abstract this out.
151 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
152 CodeGen::CodeGenFunction &CGF) const = 0;
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +0000153 };
154} // end namespace clang
155
156#endif