xo-expression: naming: {Primitive,Function}ExprInterface

This commit is contained in:
Roland Conybeare 2025-11-26 13:43:02 -05:00
commit 22670cda3d
9 changed files with 38 additions and 41 deletions

View file

@ -10,14 +10,14 @@
namespace xo { namespace xo {
namespace scm { namespace scm {
class FunctionInterface : public Expression { class FunctionExprInterface : public Expression {
public: public:
FunctionInterface(exprtype extype, TypeDescr fn_type) FunctionExprInterface(exprtype extype, TypeDescr fn_type)
: Expression(extype, fn_type) {} : Expression(extype, fn_type) {}
/** downcast from Expression **/ /** downcast from Expression **/
static bp<FunctionInterface> from(bp<Expression> x) { static bp<FunctionExprInterface> from(bp<Expression> x) {
return bp<FunctionInterface>::from(x); return bp<FunctionExprInterface>::from(x);
} }
virtual const std::string & name() const = 0; virtual const std::string & name() const = 0;

View file

@ -6,13 +6,12 @@
#pragma once #pragma once
#include "Expression.hpp" #include "Expression.hpp"
#include "FunctionInterface.hpp" #include "FunctionExprInterface.hpp"
#include "Variable.hpp" #include "Variable.hpp"
#include "LocalSymtab.hpp" #include "LocalSymtab.hpp"
#include <map> #include <map>
#include <vector> #include <vector>
#include <string> #include <string>
//#include <cstdint>
namespace xo { namespace xo {
namespace scm { namespace scm {
@ -20,7 +19,7 @@ namespace xo {
* @brief abstract syntax tree for a function definition * @brief abstract syntax tree for a function definition
* *
**/ **/
class Lambda : public FunctionInterface { class Lambda : public FunctionExprInterface {
public: public:
/** /**
* @p name. Name for this lambda -- must be unique * @p name. Name for this lambda -- must be unique

View file

@ -5,7 +5,7 @@
#pragma once #pragma once
#include "PrimitiveInterface.hpp" #include "PrimitiveExprInterface.hpp"
#include "pretty_expression.hpp" #include "pretty_expression.hpp"
#include "llvmintrinsic.hpp" #include "llvmintrinsic.hpp"
#include "xo/reflect/Reflect.hpp" #include "xo/reflect/Reflect.hpp"
@ -38,7 +38,7 @@ namespace xo {
* won't work here. * won't work here.
**/ **/
template <typename FunctionPointer> template <typename FunctionPointer>
class Primitive: public PrimitiveInterface { class Primitive: public PrimitiveExprInterface {
public: public:
using Reflect = xo::reflect::Reflect; using Reflect = xo::reflect::Reflect;
using TaggedPtr = xo::reflect::TaggedPtr; using TaggedPtr = xo::reflect::TaggedPtr;
@ -111,7 +111,7 @@ namespace xo {
FunctionPointer fnptr, FunctionPointer fnptr,
bool explicit_symbol_def, bool explicit_symbol_def,
llvmintrinsic intrinsic) llvmintrinsic intrinsic)
: PrimitiveInterface(fn_type), : PrimitiveExprInterface(fn_type),
name_{name}, name_{name},
value_td_{Reflect::require_function<FunctionPointer>()}, value_td_{Reflect::require_function<FunctionPointer>()},
value_{fnptr}, value_{fnptr},

View file

@ -1,29 +1,27 @@
/** @file PrimitiveInterface.hpp /** @file PrimitiveExprInterface.hpp
* *
* Author: Roland Conybeare * Author: Roland Conybeare
**/ **/
#pragma once #pragma once
#include "FunctionInterface.hpp" #include "FunctionExprInterface.hpp"
#include "llvmintrinsic.hpp" #include "llvmintrinsic.hpp"
//#include <cstdint>
#include <type_traits> #include <type_traits>
namespace xo { namespace xo {
namespace scm { namespace scm {
class PrimitiveInterface : public FunctionInterface { class PrimitiveExprInterface : public FunctionExprInterface {
public: public:
using void_function_type = void (*)(); using void_function_type = void (*)();
public: public:
explicit PrimitiveInterface(TypeDescr fn_type) explicit PrimitiveExprInterface(TypeDescr fn_type)
: FunctionInterface(exprtype::primitive, fn_type) {} : FunctionExprInterface(exprtype::primitive, fn_type) {}
/** downcast from Expression **/ /** downcast from Expression **/
static bp<PrimitiveInterface> from(bp<Expression> x) { static bp<PrimitiveExprInterface> from(bp<Expression> x) {
return bp<PrimitiveInterface>::from(x); return bp<PrimitiveExprInterface>::from(x);
} }
/** if true, Jit will try to explicitly symbol for this primitive /** if true, Jit will try to explicitly symbol for this primitive

View file

@ -259,7 +259,7 @@ namespace xo {
TypeDescr lambda_td, TypeDescr lambda_td,
const rp<LocalSymtab> & local_env, const rp<LocalSymtab> & local_env,
const rp<Expression> & body) const rp<Expression> & body)
: FunctionInterface(exprtype::lambda, lambda_td), : FunctionExprInterface(exprtype::lambda, lambda_td),
name_{name}, name_{name},
body_{body}, body_{body},
local_env_{local_env} local_env_{local_env}

View file

@ -15,7 +15,7 @@
#include "xo/expression/Expression.hpp" #include "xo/expression/Expression.hpp"
#include "xo/expression/ConstantInterface.hpp" #include "xo/expression/ConstantInterface.hpp"
#include "xo/expression/PrimitiveInterface.hpp" #include "xo/expression/PrimitiveExprInterface.hpp"
#include "xo/expression/Apply.hpp" #include "xo/expression/Apply.hpp"
#include "xo/expression/Lambda.hpp" #include "xo/expression/Lambda.hpp"
#include "xo/expression/Variable.hpp" #include "xo/expression/Variable.hpp"
@ -110,7 +110,7 @@ namespace xo {
**/ **/
llvm::Type * codegen_type(TypeDescr td); llvm::Type * codegen_type(TypeDescr td);
llvm::Value * codegen_constant(bp<xo::scm::ConstantInterface> expr); llvm::Value * codegen_constant(bp<xo::scm::ConstantInterface> expr);
llvm::Function * codegen_primitive(bp<xo::scm::PrimitiveInterface> expr); llvm::Function * codegen_primitive(bp<xo::scm::PrimitiveExprInterface> expr);
/** like @ref codegen_primitive , but create wrapper function that accepts (and discards) /** like @ref codegen_primitive , but create wrapper function that accepts (and discards)
* environment pointer as first argument. * environment pointer as first argument.
@ -118,7 +118,7 @@ namespace xo {
* Implementation consists of tail call to natural primitive, that skips the unused * Implementation consists of tail call to natural primitive, that skips the unused
* environment pointer * environment pointer
**/ **/
llvm::Function * codegen_primitive_wrapper(bp<xo::scm::PrimitiveInterface> expr, llvm::Function * codegen_primitive_wrapper(bp<xo::scm::PrimitiveExprInterface> expr,
llvm::IRBuilder<> & ir_builder); llvm::IRBuilder<> & ir_builder);
/** Generate closure for invoking a primitive function. /** Generate closure for invoking a primitive function.
@ -126,7 +126,7 @@ namespace xo {
* to support function-pointer-like behavior for a target function * to support function-pointer-like behavior for a target function
* that may resolve to primitive-or-lambda at runtime * that may resolve to primitive-or-lambda at runtime
**/ **/
llvm::Value * codegen_primitive_closure(bp<xo::scm::PrimitiveInterface> expr, llvm::Value * codegen_primitive_closure(bp<xo::scm::PrimitiveExprInterface> expr,
llvm::IRBuilder<> & ir_builder); llvm::IRBuilder<> & ir_builder);
llvm::Value * codegen_apply(bp<xo::scm::Apply> expr, llvm::Value * codegen_apply(bp<xo::scm::Apply> expr,

View file

@ -20,7 +20,7 @@ namespace xo {
**/ **/
struct type2llvm { struct type2llvm {
public: public:
using FunctionInterface = xo::scm::FunctionInterface; using FunctionInterface = xo::scm::FunctionExprInterface;
using Lambda = xo::scm::Lambda; using Lambda = xo::scm::Lambda;
using TypeDescr = xo::reflect::TypeDescr; using TypeDescr = xo::reflect::TypeDescr;

View file

@ -11,7 +11,7 @@ namespace xo {
using xo::scm::Expression; using xo::scm::Expression;
using xo::scm::ConstantInterface; using xo::scm::ConstantInterface;
//using xo::scm::FunctionInterface; //using xo::scm::FunctionInterface;
using xo::scm::PrimitiveInterface; using xo::scm::PrimitiveExprInterface;
using xo::scm::Lambda; using xo::scm::Lambda;
using xo::scm::Variable; using xo::scm::Variable;
using xo::scm::Apply; using xo::scm::Apply;
@ -166,7 +166,7 @@ namespace xo {
} }
llvm::Function * llvm::Function *
MachPipeline::codegen_primitive(bp<PrimitiveInterface> expr) MachPipeline::codegen_primitive(bp<PrimitiveExprInterface> expr)
{ {
constexpr bool c_debug_flag = true; constexpr bool c_debug_flag = true;
@ -251,7 +251,7 @@ namespace xo {
} /*codegen_primitive*/ } /*codegen_primitive*/
llvm::Function * llvm::Function *
MachPipeline::codegen_primitive_wrapper(bp<PrimitiveInterface> expr, MachPipeline::codegen_primitive_wrapper(bp<PrimitiveExprInterface> expr,
llvm::IRBuilder<> & /*ir_builder*/) llvm::IRBuilder<> & /*ir_builder*/)
{ {
constexpr bool c_debug_flag = true; constexpr bool c_debug_flag = true;
@ -369,7 +369,7 @@ namespace xo {
} /*codegen_primitive_wrapper*/ } /*codegen_primitive_wrapper*/
llvm::Value * llvm::Value *
MachPipeline::codegen_primitive_closure(bp<xo::scm::PrimitiveInterface> expr, MachPipeline::codegen_primitive_closure(bp<xo::scm::PrimitiveExprInterface> expr,
llvm::IRBuilder<> & ir_builder) llvm::IRBuilder<> & ir_builder)
{ {
constexpr bool c_debug_flag = true; constexpr bool c_debug_flag = true;
@ -422,7 +422,7 @@ namespace xo {
* allows substituting LLVM intrinsic * allows substituting LLVM intrinsic
*/ */
if (apply->fn()->extype() == exprtype::primitive) { if (apply->fn()->extype() == exprtype::primitive) {
auto pm = PrimitiveInterface::from(apply->fn()); auto pm = PrimitiveExprInterface::from(apply->fn());
if (pm) { if (pm) {
llvm_closure = this->codegen_primitive_closure(pm, ir_builder); llvm_closure = this->codegen_primitive_closure(pm, ir_builder);
@ -979,7 +979,7 @@ namespace xo {
case exprtype::constant: case exprtype::constant:
return this->codegen_constant(ConstantInterface::from(expr)); return this->codegen_constant(ConstantInterface::from(expr));
case exprtype::primitive: case exprtype::primitive:
return this->codegen_primitive_closure(PrimitiveInterface::from(expr), ir_builder); return this->codegen_primitive_closure(PrimitiveExprInterface::from(expr), ir_builder);
case exprtype::apply: case exprtype::apply:
return this->codegen_apply(Apply::from(expr), envptr, ir_builder); return this->codegen_apply(Apply::from(expr), envptr, ir_builder);
case exprtype::lambda: case exprtype::lambda:

View file

@ -4,7 +4,7 @@
#include "xo/pyreflect/pyreflect.hpp" #include "xo/pyreflect/pyreflect.hpp"
#include "xo/expression/Expression.hpp" #include "xo/expression/Expression.hpp"
#include "xo/expression/Apply.hpp" #include "xo/expression/Apply.hpp"
#include "xo/expression/PrimitiveInterface.hpp" #include "xo/expression/PrimitiveExprInterface.hpp"
#include "xo/expression/Primitive.hpp" #include "xo/expression/Primitive.hpp"
#include "xo/expression/ConstantInterface.hpp" #include "xo/expression/ConstantInterface.hpp"
#include "xo/expression/Constant.hpp" #include "xo/expression/Constant.hpp"
@ -21,7 +21,7 @@ namespace xo {
using xo::scm::exprtype; using xo::scm::exprtype;
using xo::scm::Expression; using xo::scm::Expression;
using xo::scm::make_apply; using xo::scm::make_apply;
using xo::scm::PrimitiveInterface; using xo::scm::PrimitiveExprInterface;
using xo::scm::Primitive; using xo::scm::Primitive;
using xo::scm::make_primitive; using xo::scm::make_primitive;
using xo::scm::ConstantInterface; using xo::scm::ConstantInterface;
@ -95,24 +95,24 @@ namespace xo {
// ----- Primitives ----- // ----- Primitives -----
py::class_<PrimitiveInterface, py::class_<PrimitiveExprInterface,
Expression, Expression,
rp<PrimitiveInterface>>(m, "PrimitiveInterface") rp<PrimitiveExprInterface>>(m, "PrimitiveInterface")
.def("name", &PrimitiveInterface::name, .def("name", &PrimitiveExprInterface::name,
py::doc("name of this primitive function; use this name to invoke the function")) py::doc("name of this primitive function; use this name to invoke the function"))
.def("n_arg", &PrimitiveInterface::n_arg, .def("n_arg", &PrimitiveExprInterface::n_arg,
py::doc("number of arguments to this function (not counting return value)")) py::doc("number of arguments to this function (not counting return value)"))
; ;
using int32_t = std::int32_t; using int32_t = std::int32_t;
py::class_<Primitive<int32_t (*)(int32_t, int32_t)>, py::class_<Primitive<int32_t (*)(int32_t, int32_t)>,
PrimitiveInterface, PrimitiveExprInterface,
rp<Primitive<int32_t (*)(int32_t, int32_t)>>>(m, "Primitive_i32_i32") rp<Primitive<int32_t (*)(int32_t, int32_t)>>>(m, "Primitive_i32_i32")
; ;
py::class_<Primitive<double (*)(double)>, py::class_<Primitive<double (*)(double)>,
PrimitiveInterface, PrimitiveExprInterface,
rp<Primitive<double (*)(double)>>>(m, "Primitive_double_double") rp<Primitive<double (*)(double)>>>(m, "Primitive_double_double")
; ;
using Fn_dbl_dbl_type = double (*)(double); using Fn_dbl_dbl_type = double (*)(double);
@ -136,7 +136,7 @@ namespace xo {
py::doc("create primitive representing the ::cos() function")); py::doc("create primitive representing the ::cos() function"));
py::class_<Primitive<double (*)(double, double)>, py::class_<Primitive<double (*)(double, double)>,
PrimitiveInterface, PrimitiveExprInterface,
rp<Primitive<double (*)(double, double)>>>(m, "Primitive_double_double_double") rp<Primitive<double (*)(double, double)>>>(m, "Primitive_double_double_double")
; ;