From aa726199528a79069f41136ef14e774b6ae6d848 Mon Sep 17 00:00:00 2001 From: Chad Stearns Date: Sun, 26 Apr 2020 14:38:09 -0400 Subject: [PATCH] Implemenet code gen for int div --- compiler/builtins/src/std.rs | 16 +++++++++++----- compiler/gen/src/llvm/build.rs | 11 +++++++++++ compiler/gen/tests/gen_builtins.rs | 13 +++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/compiler/builtins/src/std.rs b/compiler/builtins/src/std.rs index a4824de997..32e904cae1 100644 --- a/compiler/builtins/src/std.rs +++ b/compiler/builtins/src/std.rs @@ -285,13 +285,19 @@ pub fn types() -> MutMap { Box::new(SolvedType::Wildcard), ); - // div : Int, Int -> Result Int [ DivByZero ]* + // // div : Int, Int -> Result Int [ DivByZero ]* + // add_type( + // Symbol::INT_DIV, + // SolvedType::Func( + // vec![int_type(), int_type()], + // Box::new(result_type(flex(TVAR1), div_by_zero.clone())), + // ), + // ); + + // div : Int, Int -> Int add_type( Symbol::INT_DIV, - SolvedType::Func( - vec![int_type(), int_type()], - Box::new(result_type(flex(TVAR1), div_by_zero.clone())), - ), + SolvedType::Func(vec![int_type(), int_type()], Box::new(int_type())), ); // mod : Int, Int -> Result Int [ DivByZero ]* diff --git a/compiler/gen/src/llvm/build.rs b/compiler/gen/src/llvm/build.rs index 8a9eafe24c..dec36276d8 100644 --- a/compiler/gen/src/llvm/build.rs +++ b/compiler/gen/src/llvm/build.rs @@ -1226,6 +1226,17 @@ fn call_with_args<'a, 'ctx, 'env>( Symbol::FLOAT_ROUND => call_intrinsic(LLVM_LROUND_I64_F64, env, args), Symbol::LIST_SET => list_set(parent, args, env, InPlace::Clone), Symbol::LIST_SET_IN_PLACE => list_set(parent, args, env, InPlace::InPlace), + Symbol::INT_DIV => { + debug_assert!(args.len() == 2); + + let int_val = env.builder.build_int_signed_div( + args[0].0.into_int_value(), + args[1].0.into_int_value(), + "div_i64", + ); + + BasicValueEnum::IntValue(int_val) + } _ => { let fn_val = env .module diff --git a/compiler/gen/tests/gen_builtins.rs b/compiler/gen/tests/gen_builtins.rs index f97dc20c50..1243c60eba 100644 --- a/compiler/gen/tests/gen_builtins.rs +++ b/compiler/gen/tests/gen_builtins.rs @@ -159,6 +159,19 @@ mod gen_builtins { ); } + #[test] + fn gen_div_i64() { + assert_evals_to!( + indoc!( + r#" + 1000 // 100 + "# + ), + 10, + i64 + ); + } + #[test] fn gen_order_of_arithmetic_ops() { assert_evals_to!(