blob: 160a62eab367afe66f2d1e242fcff91825fe64a0 [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;
Eli Friedmandf968192011-05-26 00:10:27 +000050 bool NeedsCopy;
51 CallArg(RValue rv, QualType ty, bool needscopy)
52 : RV(rv), Ty(ty), NeedsCopy(needscopy)
Eli Friedmanf4258eb2011-05-02 18:05:27 +000053 { }
54 };
55
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000056 /// CallArgList - Type for representing both the value and type of
57 /// arguments in a call.
John McCalla738c252011-03-09 04:27:21 +000058 class CallArgList :
Eli Friedmanf4258eb2011-05-02 18:05:27 +000059 public llvm::SmallVector<CallArg, 16> {
John McCall32ea9692011-03-11 20:59:21 +000060 public:
Eli Friedmandf968192011-05-26 00:10:27 +000061 void add(RValue rvalue, QualType type, bool needscopy = false) {
62 push_back(CallArg(rvalue, type, needscopy));
John McCall32ea9692011-03-11 20:59:21 +000063 }
John McCalla738c252011-03-09 04:27:21 +000064 };
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000065
Daniel Dunbarbc915f42008-09-09 23:14:03 +000066 /// FunctionArgList - Type for representing both the decl and type
67 /// of parameters to a function. The decl must be either a
68 /// ParmVarDecl or ImplicitParamDecl.
John McCalla738c252011-03-09 04:27:21 +000069 class FunctionArgList : public llvm::SmallVector<const VarDecl*, 16> {
70 };
Mike Stump11289f42009-09-09 15:08:12 +000071
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000072 /// CGFunctionInfo - Class to encapsulate the information about a
73 /// function definition.
Daniel Dunbare0be8292009-02-03 00:07:12 +000074 class CGFunctionInfo : public llvm::FoldingSetNode {
Daniel Dunbar313321e2009-02-03 05:31:23 +000075 struct ArgInfo {
John McCall2da83a32010-02-26 00:48:12 +000076 CanQualType type;
Daniel Dunbar313321e2009-02-03 05:31:23 +000077 ABIArgInfo info;
78 };
79
Daniel Dunbar0ef34792009-09-12 00:59:20 +000080 /// The LLVM::CallingConv to use for this function (as specified by the
81 /// user).
Daniel Dunbar7feafc72009-09-11 22:24:53 +000082 unsigned CallingConvention;
83
Daniel Dunbar0ef34792009-09-12 00:59:20 +000084 /// The LLVM::CallingConv to actually use for this function, which may
85 /// depend on the ABI.
86 unsigned EffectiveCallingConvention;
87
John McCallab26cfa2010-02-05 21:31:56 +000088 /// Whether this function is noreturn.
89 bool NoReturn;
90
Daniel Dunbar313321e2009-02-03 05:31:23 +000091 unsigned NumArgs;
92 ArgInfo *Args;
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000093
Rafael Espindola49b85ab2010-03-30 22:15:11 +000094 /// How many arguments to pass inreg.
Eli Friedmanc5b20b52011-04-09 08:18:08 +000095 bool HasRegParm;
Rafael Espindola49b85ab2010-03-30 22:15:11 +000096 unsigned RegParm;
97
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000098 public:
Daniel Dunbar313321e2009-02-03 05:31:23 +000099 typedef const ArgInfo *const_arg_iterator;
100 typedef ArgInfo *arg_iterator;
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000101
Chris Lattner34d62812010-06-29 18:13:52 +0000102 CGFunctionInfo(unsigned CallingConvention, bool NoReturn,
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000103 bool HasRegParm, unsigned RegParm, CanQualType ResTy,
Chris Lattner34d62812010-06-29 18:13:52 +0000104 const CanQualType *ArgTys, unsigned NumArgTys);
Daniel Dunbar313321e2009-02-03 05:31:23 +0000105 ~CGFunctionInfo() { delete[] Args; }
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000106
Daniel Dunbar313321e2009-02-03 05:31:23 +0000107 const_arg_iterator arg_begin() const { return Args + 1; }
108 const_arg_iterator arg_end() const { return Args + 1 + NumArgs; }
109 arg_iterator arg_begin() { return Args + 1; }
110 arg_iterator arg_end() { return Args + 1 + NumArgs; }
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000111
Daniel Dunbara45bdbb2009-02-04 21:17:21 +0000112 unsigned arg_size() const { return NumArgs; }
113
John McCallab26cfa2010-02-05 21:31:56 +0000114 bool isNoReturn() const { return NoReturn; }
115
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000116 /// getCallingConvention - Return the user specified calling
117 /// convention.
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000118 unsigned getCallingConvention() const { return CallingConvention; }
119
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000120 /// getEffectiveCallingConvention - Return the actual calling convention to
121 /// use, which may depend on the ABI.
122 unsigned getEffectiveCallingConvention() const {
123 return EffectiveCallingConvention;
124 }
125 void setEffectiveCallingConvention(unsigned Value) {
126 EffectiveCallingConvention = Value;
127 }
128
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000129 bool getHasRegParm() const { return HasRegParm; }
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000130 unsigned getRegParm() const { return RegParm; }
131
John McCall2da83a32010-02-26 00:48:12 +0000132 CanQualType getReturnType() const { return Args[0].type; }
Daniel Dunbar313321e2009-02-03 05:31:23 +0000133
134 ABIArgInfo &getReturnInfo() { return Args[0].info; }
135 const ABIArgInfo &getReturnInfo() const { return Args[0].info; }
Daniel Dunbare0be8292009-02-03 00:07:12 +0000136
137 void Profile(llvm::FoldingSetNodeID &ID) {
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000138 ID.AddInteger(getCallingConvention());
John McCallab26cfa2010-02-05 21:31:56 +0000139 ID.AddBoolean(NoReturn);
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000140 ID.AddBoolean(HasRegParm);
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000141 ID.AddInteger(RegParm);
Daniel Dunbarfff09f32009-02-05 00:00:23 +0000142 getReturnType().Profile(ID);
Daniel Dunbar313321e2009-02-03 05:31:23 +0000143 for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
144 it->type.Profile(ID);
Daniel Dunbare0be8292009-02-03 00:07:12 +0000145 }
146 template<class Iterator>
Mike Stump11289f42009-09-09 15:08:12 +0000147 static void Profile(llvm::FoldingSetNodeID &ID,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000148 const FunctionType::ExtInfo &Info,
John McCall2da83a32010-02-26 00:48:12 +0000149 CanQualType ResTy,
Daniel Dunbare0be8292009-02-03 00:07:12 +0000150 Iterator begin,
151 Iterator end) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000152 ID.AddInteger(Info.getCC());
153 ID.AddBoolean(Info.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000154 ID.AddBoolean(Info.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000155 ID.AddInteger(Info.getRegParm());
Daniel Dunbare0be8292009-02-03 00:07:12 +0000156 ResTy.Profile(ID);
John McCall2da83a32010-02-26 00:48:12 +0000157 for (; begin != end; ++begin) {
158 CanQualType T = *begin; // force iterator to be over canonical types
159 T.Profile(ID);
160 }
Daniel Dunbare0be8292009-02-03 00:07:12 +0000161 }
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000162 };
Anders Carlsson0435ed52009-12-24 19:08:58 +0000163
Anders Carlsson17490832009-12-24 20:40:36 +0000164 /// ReturnValueSlot - Contains the address where the return value of a
165 /// function can be stored, and whether the address is volatile or not.
Anders Carlsson0435ed52009-12-24 19:08:58 +0000166 class ReturnValueSlot {
167 llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
168
169 public:
170 ReturnValueSlot() {}
171 ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
172 : Value(Value, IsVolatile) {}
173
174 bool isNull() const { return !getValue(); }
175
176 bool isVolatile() const { return Value.getInt(); }
177 llvm::Value *getValue() const { return Value.getPointer(); }
178 };
179
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000180} // end namespace CodeGen
181} // end namespace clang
182
183#endif