blob: 41e707a204caf73b50b22f22cd12ae34bee122fe [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
47 /// CallArgList - Type for representing both the value and type of
48 /// arguments in a call.
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000049 typedef llvm::SmallVector<std::pair<RValue, QualType>, 16> CallArgList;
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000050
Daniel Dunbarbc915f42008-09-09 23:14:03 +000051 /// FunctionArgList - Type for representing both the decl and type
52 /// of parameters to a function. The decl must be either a
53 /// ParmVarDecl or ImplicitParamDecl.
Mike Stump11289f42009-09-09 15:08:12 +000054 typedef llvm::SmallVector<std::pair<const VarDecl*, QualType>,
Daniel Dunbarbc915f42008-09-09 23:14:03 +000055 16> FunctionArgList;
Mike Stump11289f42009-09-09 15:08:12 +000056
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000057 /// CGFunctionInfo - Class to encapsulate the information about a
58 /// function definition.
Daniel Dunbare0be8292009-02-03 00:07:12 +000059 class CGFunctionInfo : public llvm::FoldingSetNode {
Daniel Dunbar313321e2009-02-03 05:31:23 +000060 struct ArgInfo {
John McCall2da83a32010-02-26 00:48:12 +000061 CanQualType type;
Daniel Dunbar313321e2009-02-03 05:31:23 +000062 ABIArgInfo info;
63 };
64
Daniel Dunbar0ef34792009-09-12 00:59:20 +000065 /// The LLVM::CallingConv to use for this function (as specified by the
66 /// user).
Daniel Dunbar7feafc72009-09-11 22:24:53 +000067 unsigned CallingConvention;
68
Daniel Dunbar0ef34792009-09-12 00:59:20 +000069 /// The LLVM::CallingConv to actually use for this function, which may
70 /// depend on the ABI.
71 unsigned EffectiveCallingConvention;
72
John McCallab26cfa2010-02-05 21:31:56 +000073 /// Whether this function is noreturn.
74 bool NoReturn;
75
Daniel Dunbar313321e2009-02-03 05:31:23 +000076 unsigned NumArgs;
77 ArgInfo *Args;
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000078
Rafael Espindola49b85ab2010-03-30 22:15:11 +000079 /// How many arguments to pass inreg.
80 unsigned RegParm;
81
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000082 public:
Daniel Dunbar313321e2009-02-03 05:31:23 +000083 typedef const ArgInfo *const_arg_iterator;
84 typedef ArgInfo *arg_iterator;
Daniel Dunbar3668cb22009-02-02 23:43:58 +000085
Chris Lattner34d62812010-06-29 18:13:52 +000086 CGFunctionInfo(unsigned CallingConvention, bool NoReturn,
87 unsigned RegParm, CanQualType ResTy,
88 const CanQualType *ArgTys, unsigned NumArgTys);
Daniel Dunbar313321e2009-02-03 05:31:23 +000089 ~CGFunctionInfo() { delete[] Args; }
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000090
Daniel Dunbar313321e2009-02-03 05:31:23 +000091 const_arg_iterator arg_begin() const { return Args + 1; }
92 const_arg_iterator arg_end() const { return Args + 1 + NumArgs; }
93 arg_iterator arg_begin() { return Args + 1; }
94 arg_iterator arg_end() { return Args + 1 + NumArgs; }
Daniel Dunbar7633cbf2009-02-02 21:43:58 +000095
Daniel Dunbara45bdbb2009-02-04 21:17:21 +000096 unsigned arg_size() const { return NumArgs; }
97
John McCallab26cfa2010-02-05 21:31:56 +000098 bool isNoReturn() const { return NoReturn; }
99
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000100 /// getCallingConvention - Return the user specified calling
101 /// convention.
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000102 unsigned getCallingConvention() const { return CallingConvention; }
103
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000104 /// getEffectiveCallingConvention - Return the actual calling convention to
105 /// use, which may depend on the ABI.
106 unsigned getEffectiveCallingConvention() const {
107 return EffectiveCallingConvention;
108 }
109 void setEffectiveCallingConvention(unsigned Value) {
110 EffectiveCallingConvention = Value;
111 }
112
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000113 unsigned getRegParm() const { return RegParm; }
114
John McCall2da83a32010-02-26 00:48:12 +0000115 CanQualType getReturnType() const { return Args[0].type; }
Daniel Dunbar313321e2009-02-03 05:31:23 +0000116
117 ABIArgInfo &getReturnInfo() { return Args[0].info; }
118 const ABIArgInfo &getReturnInfo() const { return Args[0].info; }
Daniel Dunbare0be8292009-02-03 00:07:12 +0000119
120 void Profile(llvm::FoldingSetNodeID &ID) {
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000121 ID.AddInteger(getCallingConvention());
John McCallab26cfa2010-02-05 21:31:56 +0000122 ID.AddBoolean(NoReturn);
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000123 ID.AddInteger(RegParm);
Daniel Dunbarfff09f32009-02-05 00:00:23 +0000124 getReturnType().Profile(ID);
Daniel Dunbar313321e2009-02-03 05:31:23 +0000125 for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
126 it->type.Profile(ID);
Daniel Dunbare0be8292009-02-03 00:07:12 +0000127 }
128 template<class Iterator>
Mike Stump11289f42009-09-09 15:08:12 +0000129 static void Profile(llvm::FoldingSetNodeID &ID,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000130 const FunctionType::ExtInfo &Info,
John McCall2da83a32010-02-26 00:48:12 +0000131 CanQualType ResTy,
Daniel Dunbare0be8292009-02-03 00:07:12 +0000132 Iterator begin,
133 Iterator end) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000134 ID.AddInteger(Info.getCC());
135 ID.AddBoolean(Info.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000136 ID.AddInteger(Info.getRegParm());
Daniel Dunbare0be8292009-02-03 00:07:12 +0000137 ResTy.Profile(ID);
John McCall2da83a32010-02-26 00:48:12 +0000138 for (; begin != end; ++begin) {
139 CanQualType T = *begin; // force iterator to be over canonical types
140 T.Profile(ID);
141 }
Daniel Dunbare0be8292009-02-03 00:07:12 +0000142 }
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000143 };
Anders Carlsson0435ed52009-12-24 19:08:58 +0000144
Anders Carlsson17490832009-12-24 20:40:36 +0000145 /// ReturnValueSlot - Contains the address where the return value of a
146 /// function can be stored, and whether the address is volatile or not.
Anders Carlsson0435ed52009-12-24 19:08:58 +0000147 class ReturnValueSlot {
148 llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
149
150 public:
151 ReturnValueSlot() {}
152 ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
153 : Value(Value, IsVolatile) {}
154
155 bool isNull() const { return !getValue(); }
156
157 bool isVolatile() const { return Value.getInt(); }
158 llvm::Value *getValue() const { return Value.getPointer(); }
159 };
160
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000161} // end namespace CodeGen
162} // end namespace clang
163
164#endif