blob: f5a84ec99f4be56ce518710c928fba02e510190e [file] [log] [blame]
Daniel Dunbar0dbe2272008-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 Carlsson31777a22009-12-24 19:08:58 +000018#include "llvm/ADT/FoldingSet.h"
19#include "llvm/Value.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000020#include "clang/AST/Type.h"
John McCallead608a2010-02-26 00:48:12 +000021#include "clang/AST/CanonicalType.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000022
Daniel Dunbar46f45b92008-09-09 01:06:48 +000023#include "CGValue.h"
24
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000025// FIXME: Restructure so we don't have to expose so much stuff.
26#include "ABIInfo.h"
27
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000028namespace llvm {
Devang Patel761d7f72008-09-25 21:02:23 +000029 struct AttributeWithIndex;
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000030 class Function;
31 class Type;
Daniel Dunbar0dbe2272008-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 Dunbar7c086512008-09-09 23:14:03 +000042 class VarDecl;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000043
44namespace CodeGen {
Devang Patel761d7f72008-09-25 21:02:23 +000045 typedef llvm::SmallVector<llvm::AttributeWithIndex, 8> AttributeListType;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000046
47 /// CallArgList - Type for representing both the value and type of
48 /// arguments in a call.
John McCalld26bc762011-03-09 04:27:21 +000049 class CallArgList :
50 public llvm::SmallVector<std::pair<RValue, QualType>, 16> {
John McCall413ebdb2011-03-11 20:59:21 +000051 public:
52 void add(RValue rvalue, QualType type) {
53 push_back(std::pair<RValue,QualType>(rvalue,type));
54 }
John McCalld26bc762011-03-09 04:27:21 +000055 };
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000056
Daniel Dunbar7c086512008-09-09 23:14:03 +000057 /// FunctionArgList - Type for representing both the decl and type
58 /// of parameters to a function. The decl must be either a
59 /// ParmVarDecl or ImplicitParamDecl.
John McCalld26bc762011-03-09 04:27:21 +000060 class FunctionArgList : public llvm::SmallVector<const VarDecl*, 16> {
61 };
Mike Stump1eb44332009-09-09 15:08:12 +000062
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000063 /// CGFunctionInfo - Class to encapsulate the information about a
64 /// function definition.
Daniel Dunbar40a6be62009-02-03 00:07:12 +000065 class CGFunctionInfo : public llvm::FoldingSetNode {
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000066 struct ArgInfo {
John McCallead608a2010-02-26 00:48:12 +000067 CanQualType type;
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000068 ABIArgInfo info;
69 };
70
Daniel Dunbarca6408c2009-09-12 00:59:20 +000071 /// The LLVM::CallingConv to use for this function (as specified by the
72 /// user).
Daniel Dunbarbac7c252009-09-11 22:24:53 +000073 unsigned CallingConvention;
74
Daniel Dunbarca6408c2009-09-12 00:59:20 +000075 /// The LLVM::CallingConv to actually use for this function, which may
76 /// depend on the ABI.
77 unsigned EffectiveCallingConvention;
78
John McCall04a67a62010-02-05 21:31:56 +000079 /// Whether this function is noreturn.
80 bool NoReturn;
81
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000082 unsigned NumArgs;
83 ArgInfo *Args;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000084
Rafael Espindola425ef722010-03-30 22:15:11 +000085 /// How many arguments to pass inreg.
Eli Friedmana49218e2011-04-09 08:18:08 +000086 bool HasRegParm;
Rafael Espindola425ef722010-03-30 22:15:11 +000087 unsigned RegParm;
88
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000089 public:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000090 typedef const ArgInfo *const_arg_iterator;
91 typedef ArgInfo *arg_iterator;
Daniel Dunbara0a99e02009-02-02 23:43:58 +000092
Chris Lattnerbb521142010-06-29 18:13:52 +000093 CGFunctionInfo(unsigned CallingConvention, bool NoReturn,
Eli Friedmana49218e2011-04-09 08:18:08 +000094 bool HasRegParm, unsigned RegParm, CanQualType ResTy,
Chris Lattnerbb521142010-06-29 18:13:52 +000095 const CanQualType *ArgTys, unsigned NumArgTys);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000096 ~CGFunctionInfo() { delete[] Args; }
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000097
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000098 const_arg_iterator arg_begin() const { return Args + 1; }
99 const_arg_iterator arg_end() const { return Args + 1 + NumArgs; }
100 arg_iterator arg_begin() { return Args + 1; }
101 arg_iterator arg_end() { return Args + 1 + NumArgs; }
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000102
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000103 unsigned arg_size() const { return NumArgs; }
104
John McCall04a67a62010-02-05 21:31:56 +0000105 bool isNoReturn() const { return NoReturn; }
106
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000107 /// getCallingConvention - Return the user specified calling
108 /// convention.
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000109 unsigned getCallingConvention() const { return CallingConvention; }
110
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000111 /// getEffectiveCallingConvention - Return the actual calling convention to
112 /// use, which may depend on the ABI.
113 unsigned getEffectiveCallingConvention() const {
114 return EffectiveCallingConvention;
115 }
116 void setEffectiveCallingConvention(unsigned Value) {
117 EffectiveCallingConvention = Value;
118 }
119
Eli Friedmana49218e2011-04-09 08:18:08 +0000120 bool getHasRegParm() const { return HasRegParm; }
Rafael Espindola425ef722010-03-30 22:15:11 +0000121 unsigned getRegParm() const { return RegParm; }
122
John McCallead608a2010-02-26 00:48:12 +0000123 CanQualType getReturnType() const { return Args[0].type; }
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000124
125 ABIArgInfo &getReturnInfo() { return Args[0].info; }
126 const ABIArgInfo &getReturnInfo() const { return Args[0].info; }
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000127
128 void Profile(llvm::FoldingSetNodeID &ID) {
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000129 ID.AddInteger(getCallingConvention());
John McCall04a67a62010-02-05 21:31:56 +0000130 ID.AddBoolean(NoReturn);
Eli Friedmana49218e2011-04-09 08:18:08 +0000131 ID.AddBoolean(HasRegParm);
Rafael Espindola425ef722010-03-30 22:15:11 +0000132 ID.AddInteger(RegParm);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000133 getReturnType().Profile(ID);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000134 for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
135 it->type.Profile(ID);
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000136 }
137 template<class Iterator>
Mike Stump1eb44332009-09-09 15:08:12 +0000138 static void Profile(llvm::FoldingSetNodeID &ID,
Rafael Espindola264ba482010-03-30 20:24:48 +0000139 const FunctionType::ExtInfo &Info,
John McCallead608a2010-02-26 00:48:12 +0000140 CanQualType ResTy,
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000141 Iterator begin,
142 Iterator end) {
Rafael Espindola264ba482010-03-30 20:24:48 +0000143 ID.AddInteger(Info.getCC());
144 ID.AddBoolean(Info.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000145 ID.AddBoolean(Info.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000146 ID.AddInteger(Info.getRegParm());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000147 ResTy.Profile(ID);
John McCallead608a2010-02-26 00:48:12 +0000148 for (; begin != end; ++begin) {
149 CanQualType T = *begin; // force iterator to be over canonical types
150 T.Profile(ID);
151 }
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000152 }
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000153 };
Anders Carlsson31777a22009-12-24 19:08:58 +0000154
Anders Carlssond2490a92009-12-24 20:40:36 +0000155 /// ReturnValueSlot - Contains the address where the return value of a
156 /// function can be stored, and whether the address is volatile or not.
Anders Carlsson31777a22009-12-24 19:08:58 +0000157 class ReturnValueSlot {
158 llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
159
160 public:
161 ReturnValueSlot() {}
162 ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
163 : Value(Value, IsVolatile) {}
164
165 bool isNull() const { return !getValue(); }
166
167 bool isVolatile() const { return Value.getInt(); }
168 llvm::Value *getValue() const { return Value.getPointer(); }
169 };
170
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000171} // end namespace CodeGen
172} // end namespace clang
173
174#endif