blob: 3f600c04e59dfbd5ac951ad7d9faae7da8758596 [file] [log] [blame]
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- 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// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef CLANG_CODEGEN_CGCALL_H
16#define CLANG_CODEGEN_CGCALL_H
17
Anders Carlsson0435ed52009-12-24 19:08:58 +000018#include "llvm/ADT/FoldingSet.h"
19#include "llvm/Value.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000020#include "clang/AST/Type.h"
John McCall2da83a32010-02-26 00:48:12 +000021#include "clang/AST/CanonicalType.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000022
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000023#include "CGValue.h"
24
Daniel Dunbar313321e2009-02-03 05:31:23 +000025// FIXME: Restructure so we don't have to expose so much stuff.
26#include "ABIInfo.h"
27
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000028namespace llvm {
Devang Patel322300d2008-09-25 21:02:23 +000029 struct AttributeWithIndex;
Daniel Dunbar313321e2009-02-03 05:31:23 +000030 class Function;
31 class Type;
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000032 class Value;
33
34 template<typename T, unsigned> class SmallVector;
35}
36
37namespace clang {
38 class ASTContext;
39 class Decl;
40 class FunctionDecl;
41 class ObjCMethodDecl;
Daniel Dunbarbc915f42008-09-09 23:14:03 +000042 class VarDecl;
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000043
44namespace CodeGen {
Devang Patel322300d2008-09-25 21:02:23 +000045 typedef llvm::SmallVector<llvm::AttributeWithIndex, 8> AttributeListType;
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000046
Eli Friedmanf4258eb2011-05-02 18:05:27 +000047 struct CallArg {
48 RValue RV;
49 QualType Ty;
Douglas Gregor9ca546552011-05-07 20:12:26 +000050 CallArg(RValue rv, QualType ty)
51 : RV(rv), Ty(ty)
Eli Friedmanf4258eb2011-05-02 18:05:27 +000052 { }
53 };
54
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000055 /// CallArgList - Type for representing both the value and type of
56 /// arguments in a call.
John McCalla738c252011-03-09 04:27:21 +000057 class CallArgList :
Eli Friedmanf4258eb2011-05-02 18:05:27 +000058 public llvm::SmallVector<CallArg, 16> {
John McCall32ea9692011-03-11 20:59:21 +000059 public:
Douglas Gregor9ca546552011-05-07 20:12:26 +000060 void add(RValue rvalue, QualType type) {
61 push_back(CallArg(rvalue, type));
John McCall32ea9692011-03-11 20:59:21 +000062 }
John McCalla738c252011-03-09 04:27:21 +000063 };
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000064
Daniel Dunbarbc915f42008-09-09 23:14:03 +000065 /// FunctionArgList - Type for representing both the decl and type
66 /// of parameters to a function. The decl must be either a
67 /// ParmVarDecl or ImplicitParamDecl.
John McCalla738c252011-03-09 04:27:21 +000068 class FunctionArgList : public llvm::SmallVector<const VarDecl*, 16> {
69 };
Mike Stump11289f42009-09-09 15:08:12 +000070
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000071 /// CGFunctionInfo - Class to encapsulate the information about a
72 /// function definition.
Daniel Dunbare0be8292009-02-03 00:07:12 +000073 class CGFunctionInfo : public llvm::FoldingSetNode {
Daniel Dunbar313321e2009-02-03 05:31:23 +000074 struct ArgInfo {
John McCall2da83a32010-02-26 00:48:12 +000075 CanQualType type;
Daniel Dunbar313321e2009-02-03 05:31:23 +000076 ABIArgInfo info;
77 };
78
Daniel Dunbar0ef34792009-09-12 00:59:20 +000079 /// The LLVM::CallingConv to use for this function (as specified by the
80 /// user).
Daniel Dunbar7feafc72009-09-11 22:24:53 +000081 unsigned CallingConvention;
82
Daniel Dunbar0ef34792009-09-12 00:59:20 +000083 /// The LLVM::CallingConv to actually use for this function, which may
84 /// depend on the ABI.
85 unsigned EffectiveCallingConvention;
86
John McCallab26cfa2010-02-05 21:31:56 +000087 /// Whether this function is noreturn.
88 bool NoReturn;
89
Daniel Dunbar313321e2009-02-03 05:31:23 +000090 unsigned NumArgs;
91 ArgInfo *Args;
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000092
Rafael Espindola49b85ab2010-03-30 22:15:11 +000093 /// How many arguments to pass inreg.
Eli Friedmanc5b20b52011-04-09 08:18:08 +000094 bool HasRegParm;
Rafael Espindola49b85ab2010-03-30 22:15:11 +000095 unsigned RegParm;
96
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000097 public:
Daniel Dunbar313321e2009-02-03 05:31:23 +000098 typedef const ArgInfo *const_arg_iterator;
99 typedef ArgInfo *arg_iterator;
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000100
Chris Lattner34d62812010-06-29 18:13:52 +0000101 CGFunctionInfo(unsigned CallingConvention, bool NoReturn,
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000102 bool HasRegParm, unsigned RegParm, CanQualType ResTy,
Chris Lattner34d62812010-06-29 18:13:52 +0000103 const CanQualType *ArgTys, unsigned NumArgTys);
Daniel Dunbar313321e2009-02-03 05:31:23 +0000104 ~CGFunctionInfo() { delete[] Args; }
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000105
Daniel Dunbar313321e2009-02-03 05:31:23 +0000106 const_arg_iterator arg_begin() const { return Args + 1; }
107 const_arg_iterator arg_end() const { return Args + 1 + NumArgs; }
108 arg_iterator arg_begin() { return Args + 1; }
109 arg_iterator arg_end() { return Args + 1 + NumArgs; }
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000110
Daniel Dunbara45bdbb2009-02-04 21:17:21 +0000111 unsigned arg_size() const { return NumArgs; }
112
John McCallab26cfa2010-02-05 21:31:56 +0000113 bool isNoReturn() const { return NoReturn; }
114
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000115 /// getCallingConvention - Return the user specified calling
116 /// convention.
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000117 unsigned getCallingConvention() const { return CallingConvention; }
118
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000119 /// getEffectiveCallingConvention - Return the actual calling convention to
120 /// use, which may depend on the ABI.
121 unsigned getEffectiveCallingConvention() const {
122 return EffectiveCallingConvention;
123 }
124 void setEffectiveCallingConvention(unsigned Value) {
125 EffectiveCallingConvention = Value;
126 }
127
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000128 bool getHasRegParm() const { return HasRegParm; }
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000129 unsigned getRegParm() const { return RegParm; }
130
John McCall2da83a32010-02-26 00:48:12 +0000131 CanQualType getReturnType() const { return Args[0].type; }
Daniel Dunbar313321e2009-02-03 05:31:23 +0000132
133 ABIArgInfo &getReturnInfo() { return Args[0].info; }
134 const ABIArgInfo &getReturnInfo() const { return Args[0].info; }
Daniel Dunbare0be8292009-02-03 00:07:12 +0000135
136 void Profile(llvm::FoldingSetNodeID &ID) {
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000137 ID.AddInteger(getCallingConvention());
John McCallab26cfa2010-02-05 21:31:56 +0000138 ID.AddBoolean(NoReturn);
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000139 ID.AddBoolean(HasRegParm);
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000140 ID.AddInteger(RegParm);
Daniel Dunbarfff09f32009-02-05 00:00:23 +0000141 getReturnType().Profile(ID);
Daniel Dunbar313321e2009-02-03 05:31:23 +0000142 for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
143 it->type.Profile(ID);
Daniel Dunbare0be8292009-02-03 00:07:12 +0000144 }
145 template<class Iterator>
Mike Stump11289f42009-09-09 15:08:12 +0000146 static void Profile(llvm::FoldingSetNodeID &ID,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000147 const FunctionType::ExtInfo &Info,
John McCall2da83a32010-02-26 00:48:12 +0000148 CanQualType ResTy,
Daniel Dunbare0be8292009-02-03 00:07:12 +0000149 Iterator begin,
150 Iterator end) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000151 ID.AddInteger(Info.getCC());
152 ID.AddBoolean(Info.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000153 ID.AddBoolean(Info.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000154 ID.AddInteger(Info.getRegParm());
Daniel Dunbare0be8292009-02-03 00:07:12 +0000155 ResTy.Profile(ID);
John McCall2da83a32010-02-26 00:48:12 +0000156 for (; begin != end; ++begin) {
157 CanQualType T = *begin; // force iterator to be over canonical types
158 T.Profile(ID);
159 }
Daniel Dunbare0be8292009-02-03 00:07:12 +0000160 }
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000161 };
Anders Carlsson0435ed52009-12-24 19:08:58 +0000162
Anders Carlsson17490832009-12-24 20:40:36 +0000163 /// ReturnValueSlot - Contains the address where the return value of a
164 /// function can be stored, and whether the address is volatile or not.
Anders Carlsson0435ed52009-12-24 19:08:58 +0000165 class ReturnValueSlot {
166 llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
167
168 public:
169 ReturnValueSlot() {}
170 ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
171 : Value(Value, IsVolatile) {}
172
173 bool isNull() const { return !getValue(); }
174
175 bool isVolatile() const { return Value.getInt(); }
176 llvm::Value *getValue() const { return Value.getPointer(); }
177 };
178
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000179} // end namespace CodeGen
180} // end namespace clang
181
182#endif