Skip to content

Commit

Permalink
[cpp] added trigonometric math functions as builtins (__builtin_sin, …
Browse files Browse the repository at this point in the history
…__builtin_cos, __builtin_tan)
  • Loading branch information
harrand committed May 26, 2024
1 parent e5a89a8 commit b8127b5
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
51 changes: 51 additions & 0 deletions cpp/src/builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,57 @@ std::array<semal::function_t, (int)builtin::_count> builtin_functions
.is_method = false,
.is_builtin = true
},
semal::function_t
{
.return_ty = type::from_primitive(primitive_type::f64),
.name = "sin",
.params =
{
semal::local_variable_t
{
.ty = type::from_primitive(primitive_type::f64, qualifier_weak),
.name = "num",
.ctx = {},
},
},
.ctx = {},
.is_method = false,
.is_builtin = true
},
semal::function_t
{
.return_ty = type::from_primitive(primitive_type::f64),
.name = "cos",
.params =
{
semal::local_variable_t
{
.ty = type::from_primitive(primitive_type::f64, qualifier_weak),
.name = "num",
.ctx = {},
},
},
.ctx = {},
.is_method = false,
.is_builtin = true
},
semal::function_t
{
.return_ty = type::from_primitive(primitive_type::f64),
.name = "tan",
.params =
{
semal::local_variable_t
{
.ty = type::from_primitive(primitive_type::f64, qualifier_weak),
.name = "num",
.ctx = {},
},
},
.ctx = {},
.is_method = false,
.is_builtin = true
},
};

builtin try_find_builtin(std::string_view funcname)
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/builtin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ enum class builtin
free,
memcpy,
debugbreak,
sine,
cosine,
tangent,
_count,
_undefined,
};
Expand Down
23 changes: 23 additions & 0 deletions cpp/src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,29 @@ namespace code
ret.ty = func.return_ty;
}
break;
case builtin::sine:
{
value num = get_arg(0);
ret.llv = builder->CreateCall(llvm::Intrinsic::getDeclaration(program.get(), llvm::Intrinsic::sin, {as_llvm_type(num.ty, d.state)}), {num.llv});
ret.ty = func.return_ty;
}
break;
case builtin::cosine:
{
value num = get_arg(0);
ret.llv = builder->CreateCall(llvm::Intrinsic::getDeclaration(program.get(), llvm::Intrinsic::sin, {as_llvm_type(num.ty, d.state)}), {num.llv});
ret.ty = func.return_ty;
}
break;
case builtin::tangent:
{
value num = get_arg(0);
llvm::Value* sine = builder->CreateCall(llvm::Intrinsic::getDeclaration(program.get(), llvm::Intrinsic::sin, {as_llvm_type(num.ty, d.state)}), {num.llv});
llvm::Value* cosine = builder->CreateCall(llvm::Intrinsic::getDeclaration(program.get(), llvm::Intrinsic::sin, {as_llvm_type(num.ty, d.state)}), {num.llv});
ret.llv = builder->CreateFDiv(sine, cosine);
ret.ty = func.return_ty;
}
break;
default:
d.ctx.error(error_code::nyi, "missing codegen for builtin \"{}\"", call.function_name);
break;
Expand Down
4 changes: 3 additions & 1 deletion samples/scratchpad.psy
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
main :: () -> i64
{
puts("hello world! ;)");
assert(false, "woopsie!");
assert(true, "woopsie!");
greeting : string;
defer greeting.cleanup();

Expand All @@ -10,6 +10,8 @@ main :: () -> i64
greeting.set("hey!");
puts(greeting.str);

myval : f64 := __builtin_tan(0.5);

print();
return 0;
}
Expand Down

0 comments on commit b8127b5

Please sign in to comment.