Skip to content

Commit

Permalink
fix: fix a miscompilation
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Feb 16, 2024
1 parent caf3a94 commit 64a4665
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/edlang_codegen_llvm/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ fn compile_bin_op<'ctx>(
.as_basic_value_enum()
} else {
ctx.builder
.build_int_add(lhs_value.into_int_value(), rhs_value.into_int_value(), "")?
.build_int_mul(lhs_value.into_int_value(), rhs_value.into_int_value(), "")?
.as_basic_value_enum()
};
(value, lhs_ty)
Expand Down
73 changes: 72 additions & 1 deletion lib/edlang_driver/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ mod common;
#[test_case(include_str!("programs/simple.ed"), "simple", false, 1, &["a", "b"] ; "simple.ed 3")]
#[test_case(include_str!("programs/basic_ifs.ed"), "basic_ifs", false, 9, &[] ; "basic_ifs")]
#[test_case(include_str!("programs/while.ed"), "while", false, 10, &[] ; "r#while")]
#[test_case(include_str!("programs/factorial.ed"), "factorial", false, 6, &[] ; "factorial")]
#[test_case(include_str!("programs/factorial.ed"), "factorial", false, 24, &[] ; "factorial")]
#[test_case(TEST_ADD, "TEST_ADD", false, 2, &[] ; "TEST_ADD")]
#[test_case(TEST_SUB, "TEST_SUB", false, 1, &[] ; "TEST_SUB")]
#[test_case(TEST_MUL, "TEST_MUL", false, 4, &[] ; "TEST_MUL")]
#[test_case(TEST_DIV, "TEST_DIV", false, 2, &[] ; "TEST_DIV")]
#[test_case(TEST_REM, "TEST_REM", false, 0, &[] ; "TEST_REM")]
#[test_case(TEST_IF_BOTH, "TEST_IF_BOTH", false, 1, &[] ; "TEST_IF_BOTH")]
#[test_case(TEST_IF_BOTH, "TEST_IF_BOTH", false, 2, &["a"] ; "TEST_IF_BOTH args")]
#[test_case(TEST_IF_NO_ELSE, "TEST_IF_NO_ELSE", false, 1, &[] ; "TEST_IF_NO_ELSE")]
#[test_case(TEST_IF_NO_ELSE, "TEST_IF_NO_ELSE", false, 2, &["a"] ; "TEST_IF_NO_ELSE args")]
fn example_tests(source: &str, name: &str, is_library: bool, status_code: i32, args: &[&str]) {
let program = compile_program(source, name, is_library).unwrap();

Expand All @@ -21,3 +30,65 @@ fn example_tests(source: &str, name: &str, is_library: bool, status_code: i32, a
name
);
}

const TEST_ADD: &str = r#"
mod Main {
pub fn main() -> i32 {
let b: i32 = 1 + 1;
return b;
}
}
"#;
const TEST_SUB: &str = r#"
mod Main {
pub fn main() -> i32 {
let b: i32 = 2 - 1;
return b;
}
}
"#;
const TEST_MUL: &str = r#"
mod Main {
pub fn main() -> i32 {
let b: i32 = 2 * 2;
return b;
}
}
"#;
const TEST_DIV: &str = r#"
mod Main {
pub fn main() -> i32 {
let b: i32 = 4 / 2;
return b;
}
}
"#;
const TEST_REM: &str = r#"
mod Main {
pub fn main() -> i32 {
let b: i32 = 4 % 2;
return b;
}
}
"#;
const TEST_IF_BOTH: &str = r#"
mod Main {
pub fn main(argc: i32) -> i32 {
if argc == 1 {
return 1;
} else {
return 2;
}
}
}
"#;
const TEST_IF_NO_ELSE: &str = r#"
mod Main {
pub fn main(argc: i32) -> i32 {
if argc == 1 {
return 1;
}
return 2;
}
}
"#;
4 changes: 2 additions & 2 deletions lib/edlang_driver/tests/programs/factorial.ed
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
mod Main {
pub fn main() -> i32 {
let b: i32 = factorial(3);
let b: i32 = factorial(4);
return b;
}

fn factorial(n: i32) -> i32 {
if n == 1 {
if n == 0 {
return n;
} else {
return n * factorial(n - 1);
Expand Down
1 change: 1 addition & 0 deletions lib/edlang_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ fn lower_binary_expr(
) -> (ir::RValue, TypeKind) {
trace!("lowering binary op: {:?}", op);

// todo: if lhs or rhs is a simple place, dont make another temporary?
let (lhs, lhs_ty) = if type_hint.is_none() {
let ty = find_expr_type(builder, lhs)
.unwrap_or_else(|| find_expr_type(builder, rhs).expect("cant find type"));
Expand Down
2 changes: 1 addition & 1 deletion programs/factorial.ed
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod Main {
pub fn main() -> i32 {
let b: i32 = factorial(2);
let b: i32 = factorial(4);
return b;
}

Expand Down

0 comments on commit 64a4665

Please sign in to comment.