fix: fix a miscompilation

This commit is contained in:
Edgar 2024-02-16 11:09:49 +01:00
parent caf3a94eee
commit 64a4665dfd
No known key found for this signature in database
GPG key ID: 70ADAE8F35904387
5 changed files with 77 additions and 5 deletions

View file

@ -570,7 +570,7 @@ fn compile_bin_op<'ctx>(
.as_basic_value_enum() .as_basic_value_enum()
} else { } else {
ctx.builder 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() .as_basic_value_enum()
}; };
(value, lhs_ty) (value, lhs_ty)

View file

@ -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/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/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/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]) { fn example_tests(source: &str, name: &str, is_library: bool, status_code: i32, args: &[&str]) {
let program = compile_program(source, name, is_library).unwrap(); let program = compile_program(source, name, is_library).unwrap();
@ -21,3 +30,65 @@ fn example_tests(source: &str, name: &str, is_library: bool, status_code: i32, a
name 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;
}
}
"#;

View file

@ -1,11 +1,11 @@
mod Main { mod Main {
pub fn main() -> i32 { pub fn main() -> i32 {
let b: i32 = factorial(3); let b: i32 = factorial(4);
return b; return b;
} }
fn factorial(n: i32) -> i32 { fn factorial(n: i32) -> i32 {
if n == 1 { if n == 0 {
return n; return n;
} else { } else {
return n * factorial(n - 1); return n * factorial(n - 1);

View file

@ -459,6 +459,7 @@ fn lower_binary_expr(
) -> (ir::RValue, TypeKind) { ) -> (ir::RValue, TypeKind) {
trace!("lowering binary op: {:?}", op); 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 (lhs, lhs_ty) = if type_hint.is_none() {
let ty = find_expr_type(builder, lhs) let ty = find_expr_type(builder, lhs)
.unwrap_or_else(|| find_expr_type(builder, rhs).expect("cant find type")); .unwrap_or_else(|| find_expr_type(builder, rhs).expect("cant find type"));

View file

@ -1,6 +1,6 @@
mod Main { mod Main {
pub fn main() -> i32 { pub fn main() -> i32 {
let b: i32 = factorial(2); let b: i32 = factorial(4);
return b; return b;
} }