Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit c42df7f

Browse files
committed
Simplified proxy
1 parent 4d1ebee commit c42df7f

File tree

2 files changed

+44
-60
lines changed

2 files changed

+44
-60
lines changed

src/include/codegen/codegen.h

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
namespace peloton {
2121
namespace codegen {
2222

23-
class CodeGen;
24-
2523
/// A proxy to a member of a class. Users must provide both the physical
2624
/// position of the member in the class, and the C++ type of the member.
2725
template <uint32_t Pos, typename T>
@@ -30,18 +28,6 @@ struct ProxyMember {
3028
virtual ~ProxyMember() = default;
3129
};
3230

33-
/// A proxy to a method in a class. Subclasses must implement GetFunction().
34-
template <typename T>
35-
struct ProxyMethod {
36-
// Virtual destructor
37-
virtual ~ProxyMethod() = default;
38-
39-
// Hand off to the specialized template to define the LLVM function
40-
llvm::Function *GetFunction(CodeGen &codegen) {
41-
return static_cast<T *>(this)->GetFunction(codegen);
42-
}
43-
};
44-
4531
//===----------------------------------------------------------------------===//
4632
// The main wrapper around LLVM's IR Builder to generate IR
4733
//===----------------------------------------------------------------------===//
@@ -95,8 +81,7 @@ class CodeGen {
9581
llvm::Value *CallFunc(llvm::Value *fn,
9682
const std::vector<llvm::Value *> &args);
9783
template <typename T>
98-
llvm::Value *Call(ProxyMethod<T> &proxy,
99-
const std::vector<llvm::Value *> &args) {
84+
llvm::Value *Call(const T &proxy, const std::vector<llvm::Value *> &args) {
10085
return CallFunc(proxy.GetFunction(*this), args);
10186
}
10287

src/include/codegen/proxy/proxy.h

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,35 @@ namespace codegen {
2020
/// This file contains several macros that help in creating proxies to C++
2121
/// classes.
2222

23-
#define PROXY(N) struct N##Proxy
23+
#define PROXY(clazz) struct clazz##Proxy
2424

25-
#define DECLARE_MEMBER(P, T, N) \
26-
static const ::peloton::codegen::ProxyMember<P, T> _##N;
25+
#define DECLARE_MEMBER(pos, type, name) \
26+
static const ::peloton::codegen::ProxyMember<pos, type> name;
2727

2828
#define DECLARE_TYPE \
2929
static ::llvm::Type *GetType(::peloton::codegen::CodeGen &codegen);
3030

31-
#define DECLARE_METHOD(N) \
32-
struct _##N : public ::peloton::codegen::ProxyMethod<_##N> { \
33-
static const char *k##N##FnName; \
31+
#define DECLARE_METHOD(name) \
32+
struct _##name { \
3433
::llvm::Function *GetFunction(::peloton::codegen::CodeGen &codegen) const; \
3534
}; \
36-
static _##N N;
35+
static _##name name;
3736

38-
#define MEMBER(N) decltype(_##N)
37+
#define MEMBER(member_name) decltype(member_name)
3938
#define FIELDS(...) \
4039
(::peloton::codegen::proxy::TypeList<__VA_ARGS__>::GetType(codegen))
4140

42-
#define DEFINE_TYPE(P, N, ...) \
43-
::llvm::Type *P##Proxy::GetType(::peloton::codegen::CodeGen &codegen) { \
44-
static constexpr const char *kTypeName = N; \
45-
/* Check if type has already been registered */ \
46-
::llvm::Type *type = codegen.LookupType(kTypeName); \
47-
if (type != nullptr) { \
48-
return type; \
49-
} \
50-
::std::vector<::llvm::Type *> fields = (FIELDS(__VA_ARGS__)); \
51-
return ::llvm::StructType::create(codegen.GetContext(), fields, \
52-
kTypeName); \
41+
#define DEFINE_TYPE(clazz, str_name, ...) \
42+
::llvm::Type *clazz##Proxy::GetType(::peloton::codegen::CodeGen &codegen) { \
43+
static constexpr const char *kTypeName = str_name; \
44+
/* Check if type has already been registered */ \
45+
::llvm::Type *type = codegen.LookupType(kTypeName); \
46+
if (type != nullptr) { \
47+
return type; \
48+
} \
49+
::std::vector<::llvm::Type *> fields = (FIELDS(__VA_ARGS__)); \
50+
return ::llvm::StructType::create(codegen.GetContext(), fields, \
51+
kTypeName); \
5352
}
5453

5554
namespace proxy {
@@ -195,31 +194,31 @@ struct MemFn<R (*)(Args..., ...), T, F> {
195194

196195
#define STR(x) #x
197196

198-
#define DEFINE_METHOD(NS, C, F) \
199-
C##Proxy::_##F C##Proxy::F = {}; \
200-
const char *C##Proxy::_##F::k##F##FnName = STR(NS::C::F); \
201-
::llvm::Function *C##Proxy::_##F::GetFunction( \
202-
::peloton::codegen::CodeGen &codegen) const { \
203-
/* If the function has already been defined, return it. */ \
204-
if (::llvm::Function *func = codegen.LookupBuiltin(k##F##FnName)) { \
205-
return func; \
206-
} \
207-
\
208-
/* Ensure either a function pointer or a member function pointer */ \
209-
static_assert( \
210-
((::std::is_pointer<decltype(&NS::C::F)>::value && \
211-
::std::is_function<typename ::std::remove_pointer<decltype( \
212-
&NS::C::F)>::type>::value) || \
213-
::std::is_member_function_pointer<decltype(&NS::C::F)>::value), \
214-
"You must provide a pointer to the function you want to proxy"); \
215-
\
216-
/* The function hasn't been registered. Do it now. */ \
217-
auto *func_type_ptr = ::llvm::cast<::llvm::PointerType>( \
218-
::peloton::codegen::proxy::TypeBuilder<decltype(&NS::C::F)>::GetType( \
219-
codegen)); \
220-
auto *func_type = \
221-
::llvm::cast<::llvm::FunctionType>(func_type_ptr->getElementType()); \
222-
return codegen.RegisterBuiltin(k##F##FnName, func_type, MEMFN(&NS::C::F)); \
197+
#define DEFINE_METHOD(NS, C, F) \
198+
C##Proxy::_##F C##Proxy::F = {}; \
199+
::llvm::Function *C##Proxy::_##F::GetFunction( \
200+
::peloton::codegen::CodeGen &codegen) const { \
201+
static constexpr const char *kFnName = STR(NS::C::F); \
202+
/* If the function has already been defined, return it. */ \
203+
if (::llvm::Function *func = codegen.LookupBuiltin(kFnName)) { \
204+
return func; \
205+
} \
206+
\
207+
/* Ensure either a function pointer or a member function pointer */ \
208+
static_assert( \
209+
((::std::is_pointer<decltype(&NS::C::F)>::value && \
210+
::std::is_function<typename ::std::remove_pointer<decltype( \
211+
&NS::C::F)>::type>::value) || \
212+
::std::is_member_function_pointer<decltype(&NS::C::F)>::value), \
213+
"You must provide a pointer to the function you want to proxy"); \
214+
\
215+
/* The function hasn't been registered. Do it now. */ \
216+
auto *func_type_ptr = ::llvm::cast<::llvm::PointerType>( \
217+
::peloton::codegen::proxy::TypeBuilder<decltype(&NS::C::F)>::GetType( \
218+
codegen)); \
219+
auto *func_type = \
220+
::llvm::cast<::llvm::FunctionType>(func_type_ptr->getElementType()); \
221+
return codegen.RegisterBuiltin(kFnName, func_type, MEMFN(&NS::C::F)); \
223222
}
224223

225224
#define TYPE_BUILDER(PROXY, TYPE) \

0 commit comments

Comments
 (0)