|
| 1 | +// Dynamic dispatcher from generically packaged args to any C or Python function. |
| 2 | +// |
| 3 | +// Note: this particular implementation is based on libffi, presumed to be for |
| 4 | +// now the minimal dependency, but an alternative could be based on JITing |
| 5 | +// using Cling or even Numba's llvmlite. |
| 6 | + |
| 7 | +#include "dyncall.hpp" |
| 8 | +#include <stdexcept> |
| 9 | + |
| 10 | +#include <ffi.h> |
| 11 | + |
| 12 | +using namespace phlex::experimental; |
| 13 | + |
| 14 | +phlex::experimental::dcarg phlex::experimental::dcarg::from_str(std::string const& stype) |
| 15 | +{ |
| 16 | + // only types currently used in modulewrap are added, not all ffi types |
| 17 | + if (stype == "bool") |
| 18 | + return dcarg(false); |
| 19 | + else if (stype == "int32_t") |
| 20 | + return dcarg(static_cast<std::int32_t>(0)); |
| 21 | + else if (stype == "uint32_t") |
| 22 | + return dcarg(static_cast<std::uint32_t>(0)); |
| 23 | + else if (stype == "int64_t") |
| 24 | + return dcarg(static_cast<ph_long_t>(0)); |
| 25 | + else if (stype == "uint64_t") |
| 26 | + return dcarg(static_cast<ph_ulong_t>(0)); |
| 27 | + else if (stype == "float") |
| 28 | + return dcarg(0.0f); |
| 29 | + else if (stype == "double") |
| 30 | + return dcarg(0.0); |
| 31 | + else if (stype == "void") |
| 32 | + return dcarg{}; |
| 33 | + |
| 34 | + throw std::invalid_argument("unknown type string: " + stype); |
| 35 | +} |
| 36 | + |
| 37 | +void* phlex::experimental::dcarg::value_ptr() |
| 38 | +{ |
| 39 | + return std::visit( |
| 40 | + [](auto& val) -> void* { |
| 41 | + using T = std::decay_t<decltype(val)>; |
| 42 | + if constexpr (std::is_same_v<T, std::monostate>) { |
| 43 | + return nullptr; |
| 44 | + } else { |
| 45 | + return static_cast<void*>(&val); |
| 46 | + } |
| 47 | + }, |
| 48 | + m_value); |
| 49 | +} |
| 50 | + |
| 51 | +namespace { |
| 52 | + static ffi_type* get_ffi_type(dcarg const& d) |
| 53 | + { |
| 54 | + return std::visit( |
| 55 | + [](auto&& val) -> ffi_type* { |
| 56 | + using T = std::decay_t<decltype(val)>; |
| 57 | + |
| 58 | + // there are duplicate bodies here b/c bool is represented by uint8, |
| 59 | + // just as uint8 is, there being no bool in C; the code is cleaner |
| 60 | + // with each type on its own line, however, rather than combining the |
| 61 | + // two in a single predicate as a special case |
| 62 | + // NOLINTBEGIN(bugprone-branch-clone) |
| 63 | + if constexpr (std::is_same_v<T, std::monostate>) |
| 64 | + return &ffi_type_void; |
| 65 | + else if constexpr (std::is_same_v<T, void*>) |
| 66 | + return &ffi_type_pointer; |
| 67 | + else if constexpr (std::is_same_v<T, bool>) |
| 68 | + return &ffi_type_uint8; |
| 69 | + else if constexpr (std::is_same_v<T, std::int8_t>) |
| 70 | + return &ffi_type_sint8; |
| 71 | + else if constexpr (std::is_same_v<T, std::uint8_t>) |
| 72 | + return &ffi_type_uint8; |
| 73 | + else if constexpr (std::is_same_v<T, std::int16_t>) |
| 74 | + return &ffi_type_sint16; |
| 75 | + else if constexpr (std::is_same_v<T, std::uint16_t>) |
| 76 | + return &ffi_type_uint16; |
| 77 | + else if constexpr (std::is_same_v<T, std::int32_t>) |
| 78 | + return &ffi_type_sint32; |
| 79 | + else if constexpr (std::is_same_v<T, std::uint32_t>) |
| 80 | + return &ffi_type_uint32; |
| 81 | + else if constexpr (std::is_same_v<T, ph_long_t>) |
| 82 | + return &ffi_type_sint64; |
| 83 | + else if constexpr (std::is_same_v<T, ph_ulong_t>) |
| 84 | + return &ffi_type_uint64; |
| 85 | + else if constexpr (std::is_same_v<T, float>) |
| 86 | + return &ffi_type_float; |
| 87 | + else if constexpr (std::is_same_v<T, double>) |
| 88 | + return &ffi_type_double; |
| 89 | + // NOLINTEND(bugprone-branch-clone) |
| 90 | + }, |
| 91 | + d.m_value); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +void phlex::experimental::dyncall(void* fn, dcarg& result, dcargs_t& args, int var_offset) |
| 96 | +{ |
| 97 | + // Perform a dynamic call of function fn, taking arguments `args` and returning |
| 98 | + // `result`. Set `var_offset` to the appropriate number of positional arguments |
| 99 | + // if the other arguments are variational. |
| 100 | + |
| 101 | + // Except for the memory management unique_ptrs, this code is essentially C, |
| 102 | + // because libffi is, and that yields a plethora of warnings from clang-tidy, |
| 103 | + // none of which warrant actual changes. |
| 104 | + // NOLINTBEGIN |
| 105 | + std::size_t nargs = (std::size_t)args.size(); |
| 106 | + |
| 107 | + auto t = std::make_unique<ffi_type*[]>(nargs); |
| 108 | + auto p = std::make_unique<void*[]>(nargs); |
| 109 | + |
| 110 | + for (dcargs_t::size_type i = 0; i < nargs; ++i) { |
| 111 | + auto& a = args[i]; |
| 112 | + t[i] = get_ffi_type(a); |
| 113 | + p[i] = a.value_ptr(); |
| 114 | + } |
| 115 | + |
| 116 | + ffi_cif cif; |
| 117 | + ffi_status status; |
| 118 | + if (0 < var_offset) |
| 119 | + status = |
| 120 | + ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, var_offset, nargs, get_ffi_type(result), t.get()); |
| 121 | + else |
| 122 | + status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI, nargs, get_ffi_type(result), t.get()); |
| 123 | + |
| 124 | + if (status) |
| 125 | + throw std::runtime_error("ffi prep failed"); |
| 126 | + |
| 127 | + ffi_call(&cif, (void (*)())fn, result.value_ptr(), p.get()); |
| 128 | + // NOLINTEND |
| 129 | +} |
0 commit comments