xo-expression: + explicit_symbol_def feature in PrimitiveInterface

This commit is contained in:
Roland Conybeare 2024-06-19 18:14:02 -04:00
commit 37b2bc85a4
3 changed files with 39 additions and 7 deletions

View file

@ -17,7 +17,9 @@ main() {
} }
{ {
auto expr = make_primitive("sqrt", &::sqrt); auto expr = make_primitive("sqrt",
&::sqrt,
false /*!explicit_symbol_def*/);
auto expr_td = expr->value_td(); auto expr_td = expr->value_td();

View file

@ -35,10 +35,11 @@ namespace xo {
public: public:
static ref::rp<Primitive> make(const std::string & name, static ref::rp<Primitive> make(const std::string & name,
FunctionPointer fnptr) { FunctionPointer fnptr,
bool explicit_symbol_def) {
TypeDescr fn_type = Reflect::require<FunctionPointer>(); TypeDescr fn_type = Reflect::require<FunctionPointer>();
return new Primitive(fn_type, name, fnptr); return new Primitive(fn_type, name, fnptr, explicit_symbol_def);
} }
FunctionPointer value() const { return value_; } FunctionPointer value() const { return value_; }
@ -54,6 +55,9 @@ namespace xo {
// ----- PrimitiveInterface ----- // ----- PrimitiveInterface -----
virtual bool explicit_symbol_def() const override { return explicit_symbol_def_; }
virtual void_function_type function_address() const override { return reinterpret_cast<void_function_type>(value_); }
// ----- FunctionInterface ----- // ----- FunctionInterface -----
virtual std::string const & name() const override { return name_; } virtual std::string const & name() const override { return name_; }
@ -74,11 +78,13 @@ namespace xo {
private: private:
Primitive(TypeDescr fn_type, Primitive(TypeDescr fn_type,
const std::string & name, const std::string & name,
FunctionPointer fnptr) FunctionPointer fnptr,
bool explicit_symbol_def)
: PrimitiveInterface(fn_type), : PrimitiveInterface(fn_type),
name_{name}, name_{name},
value_td_{Reflect::require_function<FunctionPointer>()}, value_td_{Reflect::require_function<FunctionPointer>()},
value_{fnptr} value_{fnptr},
explicit_symbol_def_{explicit_symbol_def}
{ {
if (!value_td_->is_function()) if (!value_td_->is_function())
throw std::runtime_error("Primitive: expected function pointer"); throw std::runtime_error("Primitive: expected function pointer");
@ -97,13 +103,21 @@ namespace xo {
TypeDescr value_td_; TypeDescr value_td_;
/** address of executable function **/ /** address of executable function **/
FunctionPointer value_; FunctionPointer value_;
/** if true, use Jit.intern_symbol() to provide explicit binding.
* Currently mystified as to what's distinguishes functions like ::sin(), ::sqrt()
* (which work do not require this) from symbols like ::mul_i32(), which do)
**/
bool explicit_symbol_def_ = false;
}; /*Primitive*/ }; /*Primitive*/
/** adopt function @p x as a callable primitive function named @p name **/ /** adopt function @p x as a callable primitive function named @p name **/
template <typename FunctionPointer> template <typename FunctionPointer>
ref::rp<Primitive<FunctionPointer>> ref::rp<Primitive<FunctionPointer>>
make_primitive(const std::string & name, FunctionPointer x) { make_primitive(const std::string & name,
return Primitive<FunctionPointer>::make(name, x); FunctionPointer x,
bool explicit_symbol_def)
{
return Primitive<FunctionPointer>::make(name, x, explicit_symbol_def);
} }
} /*namespace ast*/ } /*namespace ast*/
} /*namespace xo*/ } /*namespace xo*/

View file

@ -13,6 +13,9 @@
namespace xo { namespace xo {
namespace ast { namespace ast {
class PrimitiveInterface : public FunctionInterface { class PrimitiveInterface : public FunctionInterface {
public:
using void_function_type = void (*)();
public: public:
PrimitiveInterface(TypeDescr fn_type) PrimitiveInterface(TypeDescr fn_type)
: FunctionInterface(exprtype::primitive, fn_type) {} : FunctionInterface(exprtype::primitive, fn_type) {}
@ -22,6 +25,19 @@ namespace xo {
return ref::brw<PrimitiveInterface>::from(x); return ref::brw<PrimitiveInterface>::from(x);
} }
/** if true, Jit will try to explicitly symbol for this primitive
* (instead of looking it up in host process).
* Don't know if this works.
* Do know it's not needed for ::sin(), ::sqrt().
* Do know that my extern "C" functions like ::mul_i32(), ::mul_f64()
* need something else.
**/
virtual bool explicit_symbol_def() const = 0;
/** function address for this primitive
**/
virtual void_function_type function_address() const = 0;
// virtual const std::string & name() const; // virtual const std::string & name() const;
// virtual int n_arg() const; // virtual int n_arg() const;
// virtual TypeDescr fn_retval() const; // virtual TypeDescr fn_retval() const;