From b8127b5d119f580eb52600080e1f3a0e1c102b12 Mon Sep 17 00:00:00 2001 From: Harrand Date: Sun, 26 May 2024 02:13:16 +0100 Subject: [PATCH] [cpp] added trigonometric math functions as builtins (__builtin_sin, __builtin_cos, __builtin_tan) --- cpp/src/builtin.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++ cpp/src/builtin.hpp | 3 +++ cpp/src/codegen.cpp | 23 +++++++++++++++++++ samples/scratchpad.psy | 4 +++- 4 files changed, 80 insertions(+), 1 deletion(-) diff --git a/cpp/src/builtin.cpp b/cpp/src/builtin.cpp index 1422b82..4ff5ab3 100644 --- a/cpp/src/builtin.cpp +++ b/cpp/src/builtin.cpp @@ -77,6 +77,57 @@ std::array 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) diff --git a/cpp/src/builtin.hpp b/cpp/src/builtin.hpp index bf56d81..8652b2d 100644 --- a/cpp/src/builtin.hpp +++ b/cpp/src/builtin.hpp @@ -8,6 +8,9 @@ enum class builtin free, memcpy, debugbreak, + sine, + cosine, + tangent, _count, _undefined, }; diff --git a/cpp/src/codegen.cpp b/cpp/src/codegen.cpp index a84f0f4..49b100f 100644 --- a/cpp/src/codegen.cpp +++ b/cpp/src/codegen.cpp @@ -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; diff --git a/samples/scratchpad.psy b/samples/scratchpad.psy index 9d1604a..5683670 100644 --- a/samples/scratchpad.psy +++ b/samples/scratchpad.psy @@ -1,7 +1,7 @@ main :: () -> i64 { puts("hello world! ;)"); - assert(false, "woopsie!"); + assert(true, "woopsie!"); greeting : string; defer greeting.cleanup(); @@ -10,6 +10,8 @@ main :: () -> i64 greeting.set("hey!"); puts(greeting.str); + myval : f64 := __builtin_tan(0.5); + print(); return 0; }