xo-expression: arithmetic expression support
This commit is contained in:
parent
fce0fea1cb
commit
d4fd55b8ed
7 changed files with 181 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
|||
/* @file Apply.cpp */
|
||||
|
||||
#include "Apply.hpp"
|
||||
#include "Primitive.hpp"
|
||||
#include "xo/indentlog/print/vector.hpp"
|
||||
|
||||
namespace xo {
|
||||
|
|
@ -25,6 +26,38 @@ namespace xo {
|
|||
return new Apply(fn_retval_type, fn, argv);
|
||||
}
|
||||
|
||||
rp<Apply>
|
||||
Apply::make_add2_f64(const rp<Expression> & lhs,
|
||||
const rp<Expression> & rhs)
|
||||
{
|
||||
return Apply::make(Primitive_f64::make_add2_f64(),
|
||||
{lhs, rhs});
|
||||
}
|
||||
|
||||
rp<Apply>
|
||||
Apply::make_sub2_f64(const rp<Expression> & lhs,
|
||||
const rp<Expression> & rhs)
|
||||
{
|
||||
return Apply::make(Primitive_f64::make_sub2_f64(),
|
||||
{lhs, rhs});
|
||||
}
|
||||
|
||||
rp<Apply>
|
||||
Apply::make_mul2_f64(const rp<Expression> & lhs,
|
||||
const rp<Expression> & rhs)
|
||||
{
|
||||
return Apply::make(Primitive_f64::make_mul2_f64(),
|
||||
{lhs, rhs});
|
||||
}
|
||||
|
||||
rp<Apply>
|
||||
Apply::make_div2_f64(const rp<Expression> & lhs,
|
||||
const rp<Expression> & rhs)
|
||||
{
|
||||
return Apply::make(Primitive_f64::make_div2_f64(),
|
||||
{lhs, rhs});
|
||||
}
|
||||
|
||||
void
|
||||
Apply::display(std::ostream & os) const {
|
||||
os << "<Apply"
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ set(SELF_SRCS
|
|||
IfExpr.cpp
|
||||
LocalEnv.cpp
|
||||
ConvertExpr.cpp
|
||||
Primitive.cpp
|
||||
)
|
||||
|
||||
xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS})
|
||||
|
|
|
|||
89
src/expression/Primitive.cpp
Normal file
89
src/expression/Primitive.cpp
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
/* @file Primitive.cpp */
|
||||
|
||||
#include "Primitive.hpp"
|
||||
|
||||
extern "C" {
|
||||
double
|
||||
add2_f64(double x, double y) {
|
||||
return x + y;
|
||||
}
|
||||
|
||||
double
|
||||
sub2_f64(double x, double y) {
|
||||
return x - y;
|
||||
}
|
||||
|
||||
double
|
||||
mul2_f64(double x, double y) {
|
||||
return x * y;
|
||||
}
|
||||
|
||||
double
|
||||
div2_f64(double x, double y) {
|
||||
return x / y;
|
||||
}
|
||||
}
|
||||
|
||||
namespace xo {
|
||||
namespace ast {
|
||||
auto
|
||||
Primitive_f64::make_add2_f64() -> rp<PrimitiveType>
|
||||
{
|
||||
static rp<PrimitiveType> s_retval;
|
||||
|
||||
if (!s_retval)
|
||||
s_retval = Primitive::make("add2_f64",
|
||||
&add2_f64,
|
||||
true /*explicit_symbol_def*/,
|
||||
llvmintrinsic::fp_add);
|
||||
|
||||
return s_retval;
|
||||
}
|
||||
|
||||
auto
|
||||
Primitive_f64::make_sub2_f64() -> rp<PrimitiveType>
|
||||
{
|
||||
static rp<PrimitiveType> s_retval;
|
||||
|
||||
if (!s_retval)
|
||||
s_retval = Primitive::make("sub2_f64",
|
||||
&sub2_f64,
|
||||
true /*explicit_symbol_def*/,
|
||||
llvmintrinsic::fp_sub);
|
||||
|
||||
return s_retval;
|
||||
}
|
||||
|
||||
auto
|
||||
Primitive_f64::make_mul2_f64() -> rp<PrimitiveType>
|
||||
{
|
||||
static rp<PrimitiveType> s_retval;
|
||||
|
||||
if (!s_retval)
|
||||
s_retval = Primitive::make("mul2_f64",
|
||||
&mul2_f64,
|
||||
true /*explicit_symbol_def*/,
|
||||
llvmintrinsic::fp_mul);
|
||||
|
||||
return s_retval;
|
||||
}
|
||||
|
||||
auto
|
||||
Primitive_f64::make_div2_f64() -> rp<PrimitiveType>
|
||||
{
|
||||
static rp<PrimitiveType> s_retval;
|
||||
|
||||
if (!s_retval)
|
||||
s_retval = Primitive::make("div2_f64",
|
||||
&div2_f64,
|
||||
true /*explicit_symbol_def*/,
|
||||
llvmintrinsic::fp_div);
|
||||
|
||||
return s_retval;
|
||||
}
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
||||
/* end Primitive.cpp */
|
||||
14
src/expression/intrinsics.cpp
Normal file
14
src/expression/intrinsics.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/* @file intrinsics.cpp */
|
||||
|
||||
#include "intrinsics.hpp"
|
||||
|
||||
/* FIXME: don't know how to mangle symbols yet,
|
||||
* so putting functions invoked from jit into global namespace
|
||||
*/
|
||||
extern "C"
|
||||
int32_t
|
||||
mul_i32(int32_t x, int32_t y) {
|
||||
return x * y;
|
||||
}
|
||||
|
||||
/* end intrinsics.cpp */
|
||||
Loading…
Add table
Add a link
Reference in a new issue