-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPyLLVMFunction.cpp
More file actions
67 lines (53 loc) · 1.74 KB
/
Copy pathPyLLVMFunction.cpp
File metadata and controls
67 lines (53 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "PyLLVMFunction.hpp"
#include "PyString.hpp"
#include "types/api.hpp"
#include "types/builtin.hpp"
namespace py {
PyLLVMFunction::PyLLVMFunction(PyType *type) : PyBaseObject(type) {}
PyLLVMFunction::PyLLVMFunction(std::string &&name, FunctionType &&function)
: PyBaseObject(types::BuiltinTypes::the().llvm_function()), m_name(std::move(name)),
m_function(std::move(function))
{}
std::string PyLLVMFunction::to_string() const
{
return fmt::format("LLVM JIT function {} at {}", m_name, (void *)this);
}
PyResult<PyObject *> PyLLVMFunction::__call__(PyTuple *args, PyDict *kwargs)
{
return (*this)(args, kwargs);
}
PyResult<PyObject *> PyLLVMFunction::__repr__() const { return PyString::create(to_string()); }
void PyLLVMFunction::visit_graph(Visitor &visitor)
{
PyObject::visit_graph(visitor);
for (auto *obj : m_captures) { visitor.visit(*obj); }
}
PyType *PyLLVMFunction::static_type() const { return types::llvm_function(); }
namespace {
std::once_flag llvm_function_flag;
std::unique_ptr<TypePrototype> register_llvm_function()
{
return std::move(klass<PyLLVMFunction>("llvm_function_or_method").type);
}
}// namespace
std::function<std::unique_ptr<TypePrototype>()> PyLLVMFunction::type_factory()
{
return [] {
static std::unique_ptr<TypePrototype> type = nullptr;
std::call_once(llvm_function_flag, []() { type = register_llvm_function(); });
return std::move(type);
};
}
template<> PyLLVMFunction *as(PyObject *node)
{
if (node->type() == types::llvm_function()) { return static_cast<PyLLVMFunction *>(node); }
return nullptr;
}
template<> const PyLLVMFunction *as(const PyObject *node)
{
if (node->type() == types::llvm_function()) {
return static_cast<const PyLLVMFunction *>(node);
}
return nullptr;
}
}// namespace py