From be604cb4941540500d0e6a9eb56184016ca647e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 8 Apr 2024 12:08:17 -0300 Subject: [PATCH 01/35] Add env-macos.sh --- scripts/env-macos.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 scripts/env-macos.sh diff --git a/scripts/env-macos.sh b/scripts/env-macos.sh new file mode 100644 index 0000000..a61e9c0 --- /dev/null +++ b/scripts/env-macos.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +# This script is only useful on macOS using brew. +# It sets the LLVM environment variables. + +MLIR_SYS_170_PREFIX="$(brew --prefix llvm@17)" +LLVM_SYS_170_PREFIX="$(brew --prefix llvm@17)" +TABLEGEN_170_PREFIX="$(brew --prefix llvm@17)" + +export MLIR_SYS_170_PREFIX +export LLVM_SYS_170_PREFIX +export TABLEGEN_170_PREFIX From 7843a55e29d60c33c03acb482b8f75d3075ba7c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 8 Apr 2024 12:08:29 -0300 Subject: [PATCH 02/35] Document the script in README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index c5de262..eac2a94 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,11 @@ export LLVM_SYS_170_PREFIX=/usr/lib/llvm-17 export TABLEGEN_170_PREFIX=/usr/lib/llvm-17 ``` +If you installed llvm with brew, set up the env vars with the following script: +```sh +source scripts/env-macos.sh +``` + ## Table of Contents - [Design](#design) From b9641919a9a370643f6b59dc540657ff231c08e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 8 Apr 2024 12:13:36 -0300 Subject: [PATCH 03/35] Update README message for correctness --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eac2a94..c07dfea 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ export LLVM_SYS_170_PREFIX=/usr/lib/llvm-17 export TABLEGEN_170_PREFIX=/usr/lib/llvm-17 ``` -If you installed llvm with brew, set up the env vars with the following script: +If you installed llvm with brew, source the `env-macos.sh` script to set up the needed env vars: ```sh source scripts/env-macos.sh ``` From 21bdfee43fca4c61163b051f2600e16af9869bd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 14:43:57 -0300 Subject: [PATCH 04/35] Change ForStmt fields to follow C-like syntax --- crates/concrete_ast/src/statements.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/concrete_ast/src/statements.rs b/crates/concrete_ast/src/statements.rs index b3de285..b37f086 100644 --- a/crates/concrete_ast/src/statements.rs +++ b/crates/concrete_ast/src/statements.rs @@ -53,9 +53,9 @@ pub struct Binding { #[derive(Clone, Debug, Eq, PartialEq)] pub struct ForStmt { - pub name: Ident, - pub from: Expression, - pub to: Expression, + pub init: LetStmt, + pub condition: Expression, + pub post: AssignStmt, pub contents: Vec, } From 96e96eda1693dafa53776f8d79ff5802a891fc8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 14:53:44 -0300 Subject: [PATCH 05/35] Add for definition to grammar.lalrpop --- crates/concrete_parser/src/grammar.lalrpop | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index 36a372b..d89a45c 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -500,6 +500,7 @@ pub(crate) Statement: ast::statements::Statement = { ";"? => ast::statements::Statement::Match(<>), ";"? => ast::statements::Statement::If(<>), ";"? => ast::statements::Statement::While(<>), + ":"? => ast::statements::Statement::For(<>), ";" => ast::statements::Statement::Let(<>), ";" => ast::statements::Statement::Assign(<>), ";" => ast::statements::Statement::FnCall(<>), @@ -557,3 +558,15 @@ pub(crate) WhileStmt: ast::statements::WhileStmt = { } } } + + +pub(crate) ForStmt: ast::statements::ForStmt = { + "for" ";" ";" "{" "}" => { + ast::statements::WhileStmt { + init, + condition, + post, + contents, + } + } +} From 645fa2d451f59a3dcce0aed9a33f8f038ef83dd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 16:20:41 -0300 Subject: [PATCH 06/35] Add optional elements in For definition --- crates/concrete_ast/src/statements.rs | 6 +++--- crates/concrete_parser/src/grammar.lalrpop | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/concrete_ast/src/statements.rs b/crates/concrete_ast/src/statements.rs index b37f086..9a8ea6d 100644 --- a/crates/concrete_ast/src/statements.rs +++ b/crates/concrete_ast/src/statements.rs @@ -53,9 +53,9 @@ pub struct Binding { #[derive(Clone, Debug, Eq, PartialEq)] pub struct ForStmt { - pub init: LetStmt, - pub condition: Expression, - pub post: AssignStmt, + pub init: Option, + pub condition: Option, + pub post: Option, pub contents: Vec, } diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index d89a45c..ffc7507 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -561,8 +561,8 @@ pub(crate) WhileStmt: ast::statements::WhileStmt = { pub(crate) ForStmt: ast::statements::ForStmt = { - "for" ";" ";" "{" "}" => { - ast::statements::WhileStmt { + "for" ? ";" ? ";" ? "{" "}" => { + ast::statements::ForStmt { init, condition, post, From d1462dea4f6f628c262a2046f6e3f59858cbd4e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 16:29:10 -0300 Subject: [PATCH 07/35] Fix sintax error --- crates/concrete_parser/src/grammar.lalrpop | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index ffc7507..df825e3 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -561,7 +561,7 @@ pub(crate) WhileStmt: ast::statements::WhileStmt = { pub(crate) ForStmt: ast::statements::ForStmt = { - "for" ? ";" ? ";" ? "{" "}" => { + "for" ";" ";" "{" "}" => { ast::statements::ForStmt { init, condition, From 53b460bda6af1d24bb743fff5691dfc97fe8eca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 16:42:47 -0300 Subject: [PATCH 08/35] Add for example --- examples/for.con | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 examples/for.con diff --git a/examples/for.con b/examples/for.con new file mode 100644 index 0000000..d8155e4 --- /dev/null +++ b/examples/for.con @@ -0,0 +1,15 @@ +mod Example { + fn main() -> i64 { + return sum_to(4); + } + + fn sum_to(limit: i64) -> i64 { + let mut result: i64 = 0; + + for let n: usize = 1; n <= limit; n = n + 1 { + result = result + n; + } + + return result; + } +} From 799e017770d60d60212db4110f40de03dbce30d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 16:52:05 -0300 Subject: [PATCH 09/35] Add temporary parenthesis as a work around --- crates/concrete_parser/src/grammar.lalrpop | 2 +- examples/for.con | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index df825e3..821534f 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -561,7 +561,7 @@ pub(crate) WhileStmt: ast::statements::WhileStmt = { pub(crate) ForStmt: ast::statements::ForStmt = { - "for" ";" ";" "{" "}" => { + "for" "(" ";" ";" ")" "{" "}" => { ast::statements::ForStmt { init, condition, diff --git a/examples/for.con b/examples/for.con index d8155e4..80a0fdb 100644 --- a/examples/for.con +++ b/examples/for.con @@ -6,7 +6,7 @@ mod Example { fn sum_to(limit: i64) -> i64 { let mut result: i64 = 0; - for let n: usize = 1; n <= limit; n = n + 1 { + for (let n: usize = 1; n <= limit; n = n + 1) { result = result + n; } From c8b2e43f277959ed96e0da46ce9715e27760487f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 17:15:14 -0300 Subject: [PATCH 10/35] Add for test --- crates/concrete_parser/src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/concrete_parser/src/lib.rs b/crates/concrete_parser/src/lib.rs index 2881a4a..ff2b940 100644 --- a/crates/concrete_parser/src/lib.rs +++ b/crates/concrete_parser/src/lib.rs @@ -150,6 +150,24 @@ mod ModuleName { fn parse_empty_fn() { let source = r##"mod MyMod { fn hello() {} +}"##; + let lexer = Lexer::new(source); + let parser = grammar::ProgramParser::new(); + parser.parse(lexer).unwrap(); + } + + #[test] + fn parse_for() { + let source = r##"mod MyMod { + fn hello() { + let mut result: i64 = 0; + + for (let n: usize = 1; n <= limit; n = n + 1) { + result = result + n; + } + + return result; + } }"##; let lexer = Lexer::new(source); let parser = grammar::ProgramParser::new(); From 21d34427ca1ae7c81232cb28033e940b3bda7370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 17:25:18 -0300 Subject: [PATCH 11/35] Use i64 for variable type --- examples/for.con | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/for.con b/examples/for.con index 80a0fdb..1f2ddd0 100644 --- a/examples/for.con +++ b/examples/for.con @@ -6,7 +6,7 @@ mod Example { fn sum_to(limit: i64) -> i64 { let mut result: i64 = 0; - for (let n: usize = 1; n <= limit; n = n + 1) { + for (let n: i64 = 1; n <= limit; n = n + 1) { result = result + n; } From 34d5d3b468dfed2c73bccf28267cc26b356f505a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 17:25:30 -0300 Subject: [PATCH 12/35] Add loop and while examples with for loop --- examples/for_loop.con | 17 +++++++++++++++++ examples/for_while.con | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 examples/for_loop.con create mode 100644 examples/for_while.con diff --git a/examples/for_loop.con b/examples/for_loop.con new file mode 100644 index 0000000..90b2236 --- /dev/null +++ b/examples/for_loop.con @@ -0,0 +1,17 @@ +mod Example { + fn main() -> i64 { + return sum_to(4); + } + + fn sum_to(limit: i64) -> i64 { + let mut result: i64 = 0; + + let n: i64 = 1; + for (;;) { + result = result + n; + n = n + 1; + } + + return result; + } +} diff --git a/examples/for_while.con b/examples/for_while.con new file mode 100644 index 0000000..d3b536b --- /dev/null +++ b/examples/for_while.con @@ -0,0 +1,17 @@ +mod Example { + fn main() -> i64 { + return sum_to(4); + } + + fn sum_to(limit: i64) -> i64 { + let mut result: i64 = 0; + + let n: i64 = 1; + for (; n <= limit ;) { + result = result + n; + n = n + 1; + } + + return result; + } +} From 82419dc3350d32091046eb9bc4e59c9ba9069376 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 17:35:28 -0300 Subject: [PATCH 13/35] Add for_while and for_loop tests --- crates/concrete_parser/src/lib.rs | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/crates/concrete_parser/src/lib.rs b/crates/concrete_parser/src/lib.rs index ff2b940..c33c18c 100644 --- a/crates/concrete_parser/src/lib.rs +++ b/crates/concrete_parser/src/lib.rs @@ -173,4 +173,44 @@ mod ModuleName { let parser = grammar::ProgramParser::new(); parser.parse(lexer).unwrap(); } + + #[test] + fn parse_for_while() { + let source = r##"mod MyMod { + fn hello() { + let mut result: i64 = 0; + + let n: i64 = 1; + for (; n <= limit ;) { + result = result + n; + n = n + 1; + } + + return result; + } +}"##; + let lexer = Lexer::new(source); + let parser = grammar::ProgramParser::new(); + parser.parse(lexer).unwrap(); + } + + #[test] + fn parse_for_loop() { + let source = r##"mod MyMod { + fn hello() { + let mut result: i64 = 0; + + let n: i64 = 1; + for (;;) { + result = result + n; + n = n + 1; + } + + return result; + } +}"##; + let lexer = Lexer::new(source); + let parser = grammar::ProgramParser::new(); + parser.parse(lexer).unwrap(); + } } From 628456f3a80743d006b638458e30ecfbc3538041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 18:08:40 -0300 Subject: [PATCH 14/35] Add syntax sugar to "for" as while --- crates/concrete_parser/src/grammar.lalrpop | 8 ++++++++ crates/concrete_parser/src/lib.rs | 6 ++++++ examples/for_while.con | 6 ++++++ 3 files changed, 20 insertions(+) diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index 821534f..a4dca41 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -568,5 +568,13 @@ pub(crate) ForStmt: ast::statements::ForStmt = { post, contents, } + }, + "for" "(" ")" "{" "}" => { + ast::statements::ForStmt { + init: None, + condition: Some(condition), + post: None, + contents, + } } } diff --git a/crates/concrete_parser/src/lib.rs b/crates/concrete_parser/src/lib.rs index c33c18c..22e9123 100644 --- a/crates/concrete_parser/src/lib.rs +++ b/crates/concrete_parser/src/lib.rs @@ -206,6 +206,12 @@ mod ModuleName { n = n + 1; } + n = 1; + for (n <= limit) { + result = result + n; + n = n + 1; + } + return result; } }"##; diff --git a/examples/for_while.con b/examples/for_while.con index d3b536b..80f9d3e 100644 --- a/examples/for_while.con +++ b/examples/for_while.con @@ -12,6 +12,12 @@ mod Example { n = n + 1; } + n = 1; + for (n <= limit) { + result = result + n; + n = n + 1; + } + return result; } } From d52ec32acc2e066f86d4a4c11f15f1f9a0d88269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 18:12:23 -0300 Subject: [PATCH 15/35] Add syntax sugar to "for" loop --- crates/concrete_parser/src/grammar.lalrpop | 8 ++++++++ crates/concrete_parser/src/lib.rs | 9 +++++++-- examples/for_loop.con | 5 +++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index a4dca41..9064787 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -576,5 +576,13 @@ pub(crate) ForStmt: ast::statements::ForStmt = { post: None, contents, } + }, + "for" "{" "}" => { + ast::statements::ForStmt { + init: None, + condition: None, + post: None, + contents, + } } } diff --git a/crates/concrete_parser/src/lib.rs b/crates/concrete_parser/src/lib.rs index 22e9123..c41ed18 100644 --- a/crates/concrete_parser/src/lib.rs +++ b/crates/concrete_parser/src/lib.rs @@ -186,6 +186,12 @@ mod ModuleName { n = n + 1; } + n = 1; + for (n <= limit) { + result = result + n; + n = n + 1; + } + return result; } }"##; @@ -206,8 +212,7 @@ mod ModuleName { n = n + 1; } - n = 1; - for (n <= limit) { + for { result = result + n; n = n + 1; } diff --git a/examples/for_loop.con b/examples/for_loop.con index 90b2236..fb57b29 100644 --- a/examples/for_loop.con +++ b/examples/for_loop.con @@ -11,6 +11,11 @@ mod Example { result = result + n; n = n + 1; } + + for { + result = result + n; + n = n + 1; + } return result; } From 1a98b4e11fbdf65e745b43eed5305ccb98637785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 10 Apr 2024 12:50:09 -0300 Subject: [PATCH 16/35] Fix syntax error --- crates/concrete_parser/src/grammar.lalrpop | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index 9064787..1adc088 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -500,7 +500,7 @@ pub(crate) Statement: ast::statements::Statement = { ";"? => ast::statements::Statement::Match(<>), ";"? => ast::statements::Statement::If(<>), ";"? => ast::statements::Statement::While(<>), - ":"? => ast::statements::Statement::For(<>), + ";"? => ast::statements::Statement::For(<>), ";" => ast::statements::Statement::Let(<>), ";" => ast::statements::Statement::Assign(<>), ";" => ast::statements::Statement::FnCall(<>), From d85898308bb79ae5cd512c29018db04eaaf80f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 10 Apr 2024 14:53:27 -0300 Subject: [PATCH 17/35] Add span to for statement --- crates/concrete_ast/src/statements.rs | 1 + crates/concrete_parser/src/grammar.lalrpop | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/concrete_ast/src/statements.rs b/crates/concrete_ast/src/statements.rs index 9a8ea6d..f836281 100644 --- a/crates/concrete_ast/src/statements.rs +++ b/crates/concrete_ast/src/statements.rs @@ -57,6 +57,7 @@ pub struct ForStmt { pub condition: Option, pub post: Option, pub contents: Vec, + pub span: Span, } #[derive(Clone, Debug, Eq, PartialEq)] diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index 1adc088..4e65311 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -561,28 +561,31 @@ pub(crate) WhileStmt: ast::statements::WhileStmt = { pub(crate) ForStmt: ast::statements::ForStmt = { - "for" "(" ";" ";" ")" "{" "}" => { + "for" "(" ";" ";" ")" "{" "}" => { ast::statements::ForStmt { init, condition, post, contents, + span: Span::new(lo, hi) } }, - "for" "(" ")" "{" "}" => { + "for" "(" ")" "{" "}" => { ast::statements::ForStmt { init: None, condition: Some(condition), post: None, contents, + span: Span::new(lo, hi) } }, - "for" "{" "}" => { + "for" "{" "}" => { ast::statements::ForStmt { init: None, condition: None, post: None, contents, + span: Span::new(lo, hi) } } } From 9ebe3190429e439ed7ff657f96a28499b31e41a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 11 Apr 2024 18:02:05 -0300 Subject: [PATCH 18/35] Fix bug --- crates/concrete_ir/src/lowering.rs | 38 +++++++++--------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index 7663f7f..98357cc 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -428,25 +428,17 @@ fn lower_while(builder: &mut FnBodyBuilder, info: &WhileStmt) -> Result<(), Lowe )?; } - // keet idx to change terminator if there is no return - let last_then_block_idx = if !matches!( - builder.body.basic_blocks.last().unwrap().terminator.kind, - TerminatorKind::Return - ) { - builder.body.basic_blocks.len(); - let statements = std::mem::take(&mut builder.statements); - let idx = builder.body.basic_blocks.len(); - builder.body.basic_blocks.push(BasicBlock { - statements, - terminator: Box::new(Terminator { - span: None, - kind: TerminatorKind::Unreachable, - }), - }); - Some(idx) - } else { - None - }; + builder.body.basic_blocks.len(); + let statements = std::mem::take(&mut builder.statements); + builder.body.basic_blocks.push(BasicBlock { + statements, + terminator: Box::new(Terminator { + span: None, + kind: TerminatorKind::Goto { + target: check_block_idx, + }, + }), + }); let otherwise_block_idx = builder.body.basic_blocks.len(); @@ -461,14 +453,6 @@ fn lower_while(builder: &mut FnBodyBuilder, info: &WhileStmt) -> Result<(), Lowe }; builder.body.basic_blocks[check_block_idx].terminator.kind = kind; - if let Some(last_then_block_idx) = last_then_block_idx { - builder.body.basic_blocks[last_then_block_idx] - .terminator - .kind = TerminatorKind::Goto { - target: check_block_idx, - }; - } - Ok(()) } From 3fd5f5fd5552f79879ba8aba30ecf65275503048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 11 Apr 2024 19:07:57 -0300 Subject: [PATCH 19/35] Add run command to CLI --- crates/concrete_driver/src/lib.rs | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/crates/concrete_driver/src/lib.rs b/crates/concrete_driver/src/lib.rs index 95ba0f4..01692a8 100644 --- a/crates/concrete_driver/src/lib.rs +++ b/crates/concrete_driver/src/lib.rs @@ -59,6 +59,11 @@ enum Commands { #[arg(short, long)] profile: Option, }, + /// Run a concrete file + Run { + #[arg(required = false)] + path: Option, + }, } #[derive(Parser, Debug)] @@ -351,6 +356,36 @@ mod {} {{ } ); } + Commands::Run { path } => { + let input = path.unwrap(); + + let stem = input + .file_stem() + .context("could not get file stem")? + .to_str() + .context("could not convert file stem to string")?; + let output = std::env::current_dir()?.join("build").join(stem); + + let compile_args = CompilerArgs { + input, + output: output.clone(), + release: false, + optlevel: None, + debug_info: None, + library: false, + ast: false, + ir: false, + llvm: true, + asm: false, + object: true, + mlir: true, + }; + + let start = Instant::now(); + let object = compile(&compile_args)?; + + link_binary(&[object], &output)?; + } } Ok(()) From ebbf8a822f3848246d090a9206cd8111424f5189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 12 Apr 2024 11:13:04 -0300 Subject: [PATCH 20/35] Create build_dir if doesn't exist --- crates/concrete_driver/src/lib.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/concrete_driver/src/lib.rs b/crates/concrete_driver/src/lib.rs index 01692a8..ca57e5e 100644 --- a/crates/concrete_driver/src/lib.rs +++ b/crates/concrete_driver/src/lib.rs @@ -364,7 +364,12 @@ mod {} {{ .context("could not get file stem")? .to_str() .context("could not convert file stem to string")?; - let output = std::env::current_dir()?.join("build").join(stem); + + let build_dir = std::env::current_dir()?.join("build"); + if !build_dir.exists() { + std::fs::create_dir_all(&build_dir)?; + } + let output = build_dir.with_file_name(stem); let compile_args = CompilerArgs { input, @@ -380,10 +385,7 @@ mod {} {{ object: true, mlir: true, }; - - let start = Instant::now(); let object = compile(&compile_args)?; - link_binary(&[object], &output)?; } } From 4854b4b275cc5216a7d66f0a02be50e393093c6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 12 Apr 2024 11:13:13 -0300 Subject: [PATCH 21/35] Rename build_artifacts to build in .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6e91045..76f87cf 100644 --- a/.gitignore +++ b/.gitignore @@ -19,7 +19,7 @@ target/ .vscode/ lcov.info -build_artifacts/ +build/ *.so *.a From 1450aba4744b19b5b04c587938c9b6d239e0f892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 12 Apr 2024 11:26:35 -0300 Subject: [PATCH 22/35] Rename variable --- crates/concrete_driver/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/concrete_driver/src/lib.rs b/crates/concrete_driver/src/lib.rs index ca57e5e..3ed24b1 100644 --- a/crates/concrete_driver/src/lib.rs +++ b/crates/concrete_driver/src/lib.rs @@ -359,7 +359,7 @@ mod {} {{ Commands::Run { path } => { let input = path.unwrap(); - let stem = input + let output_stem = input .file_stem() .context("could not get file stem")? .to_str() @@ -369,7 +369,7 @@ mod {} {{ if !build_dir.exists() { std::fs::create_dir_all(&build_dir)?; } - let output = build_dir.with_file_name(stem); + let output = build_dir.join(output_stem); let compile_args = CompilerArgs { input, @@ -386,6 +386,7 @@ mod {} {{ mlir: true, }; let object = compile(&compile_args)?; + link_binary(&[object], &output)?; } } From f7a9119bfef00ffe398c064f4a96462ed8026101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 12 Apr 2024 12:01:20 -0300 Subject: [PATCH 23/35] Simplify command (required path) --- crates/concrete_driver/src/lib.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/crates/concrete_driver/src/lib.rs b/crates/concrete_driver/src/lib.rs index 3ed24b1..c217be2 100644 --- a/crates/concrete_driver/src/lib.rs +++ b/crates/concrete_driver/src/lib.rs @@ -60,10 +60,7 @@ enum Commands { profile: Option, }, /// Run a concrete file - Run { - #[arg(required = false)] - path: Option, - }, + Run { path: PathBuf }, } #[derive(Parser, Debug)] @@ -356,9 +353,7 @@ mod {} {{ } ); } - Commands::Run { path } => { - let input = path.unwrap(); - + Commands::Run { path: input } => { let output_stem = input .file_stem() .context("could not get file stem")? From 01ca0cdbff100cb4d182862076b2cdefa19990c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 12 Apr 2024 12:01:32 -0300 Subject: [PATCH 24/35] Execute binary automatically --- crates/concrete_driver/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/concrete_driver/src/lib.rs b/crates/concrete_driver/src/lib.rs index c217be2..cc5b17c 100644 --- a/crates/concrete_driver/src/lib.rs +++ b/crates/concrete_driver/src/lib.rs @@ -12,6 +12,7 @@ use config::{Package, Profile}; use git2::{IndexAddOption, Repository}; use owo_colors::OwoColorize; use std::io::Read; +use std::os::unix::process::CommandExt; use std::{collections::HashMap, fs::File, path::PathBuf, time::Instant}; use walkdir::WalkDir; @@ -383,6 +384,8 @@ mod {} {{ let object = compile(&compile_args)?; link_binary(&[object], &output)?; + + Err(std::process::Command::new(&output).exec())?; } } From 228ae81e071611d69d755c3df5a11a05efc5f624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 12 Apr 2024 12:52:27 -0300 Subject: [PATCH 25/35] Abstract build_command to function --- crates/concrete_driver/src/lib.rs | 223 ++++++++++++++---------------- 1 file changed, 107 insertions(+), 116 deletions(-) diff --git a/crates/concrete_driver/src/lib.rs b/crates/concrete_driver/src/lib.rs index cc5b17c..27f1923 100644 --- a/crates/concrete_driver/src/lib.rs +++ b/crates/concrete_driver/src/lib.rs @@ -237,122 +237,7 @@ mod {} {{ } } Commands::Build { release, profile } => { - let mut current_dir = std::env::current_dir()?; - let mut config_path = None; - - // Go up to 3 parent folders to find Concrete.toml - for _ in 0..3 { - if !current_dir.join("Concrete.toml").exists() { - current_dir = if let Some(parent) = current_dir.parent() { - parent.to_path_buf() - } else { - bail!("Couldn't find Concrete.toml"); - }; - } else { - config_path = Some(current_dir.join("Concrete.toml")); - break; - } - } - - let config_path = match config_path { - Some(x) => x, - None => bail!("Couldn't find Concrete.toml"), - }; - - let base_dir = config_path - .parent() - .context("couldn't get config parent dir")?; - let mut config = File::open(&config_path).context("Failed to open Concrete.toml")?; - let mut buf = String::new(); - config.read_to_string(&mut buf)?; - - let config: Config = toml::from_str(&buf).context("failed to parse Concrete.toml")?; - - println!( - " {} {} v{} ({})", - "Compiling".green().bold(), - config.package.name, - config.package.version, - base_dir.display() - ); - - let src_dir = base_dir.join("src"); - let target_dir = base_dir.join("build"); - - if !target_dir.exists() { - std::fs::create_dir_all(&target_dir)?; - } - - let has_main = src_dir.join("main.con").exists(); - let output = target_dir.join(config.package.name); - - let (profile, profile_name) = if let Some(profile) = profile { - ( - config - .profile - .get(&profile) - .context("Couldn't get requested profile")?, - profile, - ) - } else if release { - ( - config - .profile - .get("release") - .context("Couldn't get profile: release")?, - "release".to_string(), - ) - } else { - ( - config - .profile - .get("dev") - .context("Couldn't get profile: dev")?, - "dev".to_string(), - ) - }; - - let compile_args = CompilerArgs { - input: src_dir, - output: output.clone(), - release, - optlevel: Some(profile.opt_level), - debug_info: Some(profile.debug_info), - library: !has_main, - ast: false, - ir: false, - llvm: true, - asm: false, - object: true, - mlir: true, - }; - - let start = Instant::now(); - let object = compile(&compile_args)?; - - if !has_main { - link_shared_lib(&[object], &output)?; - } else { - link_binary(&[object], &output)?; - } - - let elapsed = start.elapsed(); - - println!( - " {} {} [{}{}] in {elapsed:?}", - "Finished".green().bold(), - profile_name, - if profile.opt_level > 0 { - "optimized" - } else { - "unoptimized" - }, - if profile.debug_info { - " + debuginfo" - } else { - "" - } - ); + build_command(profile, release)?; } Commands::Run { path: input } => { let output_stem = input @@ -392,6 +277,112 @@ mod {} {{ Ok(()) } +fn build_command(profile: Option, release: bool) -> Result { + let mut current_dir = std::env::current_dir()?; + let mut config_path = None; + for _ in 0..3 { + if !current_dir.join("Concrete.toml").exists() { + current_dir = if let Some(parent) = current_dir.parent() { + parent.to_path_buf() + } else { + bail!("Couldn't find Concrete.toml"); + }; + } else { + config_path = Some(current_dir.join("Concrete.toml")); + break; + } + } + let config_path = match config_path { + Some(x) => x, + None => bail!("Couldn't find Concrete.toml"), + }; + let base_dir = config_path + .parent() + .context("couldn't get config parent dir")?; + let mut config = File::open(&config_path).context("Failed to open Concrete.toml")?; + let mut buf = String::new(); + config.read_to_string(&mut buf)?; + let config: Config = toml::from_str(&buf).context("failed to parse Concrete.toml")?; + println!( + " {} {} v{} ({})", + "Compiling".green().bold(), + config.package.name, + config.package.version, + base_dir.display() + ); + let src_dir = base_dir.join("src"); + let target_dir = base_dir.join("build"); + if !target_dir.exists() { + std::fs::create_dir_all(&target_dir)?; + } + let has_main = src_dir.join("main.con").exists(); + let output = target_dir.join(config.package.name); + let (profile, profile_name) = if let Some(profile) = profile { + ( + config + .profile + .get(&profile) + .context("Couldn't get requested profile")?, + profile, + ) + } else if release { + ( + config + .profile + .get("release") + .context("Couldn't get profile: release")?, + "release".to_string(), + ) + } else { + ( + config + .profile + .get("dev") + .context("Couldn't get profile: dev")?, + "dev".to_string(), + ) + }; + let compile_args = CompilerArgs { + input: src_dir, + output: output.clone(), + release, + optlevel: Some(profile.opt_level), + debug_info: Some(profile.debug_info), + library: !has_main, + ast: false, + ir: false, + llvm: true, + asm: false, + object: true, + mlir: true, + }; + let start = Instant::now(); + let object = compile(&compile_args)?; + if !has_main { + link_shared_lib(&[object], &output)?; + } else { + link_binary(&[object], &output)?; + } + let elapsed = start.elapsed(); + println!( + " {} {} [{}{}] in {elapsed:?}", + "Finished".green().bold(), + profile_name, + if profile.opt_level > 0 { + "optimized" + } else { + "unoptimized" + }, + if profile.debug_info { + " + debuginfo" + } else { + "" + } + ); + + Ok(output) +} + pub fn compile(args: &CompilerArgs) -> Result { let mut files = Vec::new(); for entry in WalkDir::new(&args.input) { From 97bc80d5772fcd73458559a4efab02c5c8e614e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 12 Apr 2024 13:04:00 -0300 Subject: [PATCH 26/35] Integrate build command to run command --- crates/concrete_driver/src/lib.rs | 82 ++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 30 deletions(-) diff --git a/crates/concrete_driver/src/lib.rs b/crates/concrete_driver/src/lib.rs index 27f1923..9444a83 100644 --- a/crates/concrete_driver/src/lib.rs +++ b/crates/concrete_driver/src/lib.rs @@ -61,7 +61,18 @@ enum Commands { profile: Option, }, /// Run a concrete file - Run { path: PathBuf }, + Run { + #[arg(required = false)] + path: Option, + + /// Build for release with all optimizations. + #[arg(short, long, default_value_t = false)] + release: bool, + + /// Override the profile to use. + #[arg(short, long)] + profile: Option, + }, } #[derive(Parser, Debug)] @@ -239,36 +250,47 @@ mod {} {{ Commands::Build { release, profile } => { build_command(profile, release)?; } - Commands::Run { path: input } => { - let output_stem = input - .file_stem() - .context("could not get file stem")? - .to_str() - .context("could not convert file stem to string")?; - - let build_dir = std::env::current_dir()?.join("build"); - if !build_dir.exists() { - std::fs::create_dir_all(&build_dir)?; - } - let output = build_dir.join(output_stem); - - let compile_args = CompilerArgs { - input, - output: output.clone(), - release: false, - optlevel: None, - debug_info: None, - library: false, - ast: false, - ir: false, - llvm: true, - asm: false, - object: true, - mlir: true, + Commands::Run { + path, + release, + profile, + } => { + let output = match path { + Some(input) => { + let output_stem = input + .file_stem() + .context("could not get file stem")? + .to_str() + .context("could not convert file stem to string")?; + + let build_dir = std::env::current_dir()?.join("build"); + if !build_dir.exists() { + std::fs::create_dir_all(&build_dir)?; + } + let output = build_dir.join(output_stem); + + let compile_args = CompilerArgs { + input, + output: output.clone(), + release, + optlevel: None, + debug_info: None, + library: false, + ast: false, + ir: false, + llvm: true, + asm: false, + object: true, + mlir: true, + }; + let object = compile(&compile_args)?; + + link_binary(&[object], &output)?; + + output + } + None => build_command(profile, release)?, }; - let object = compile(&compile_args)?; - - link_binary(&[object], &output)?; Err(std::process::Command::new(&output).exec())?; } From 458405fd052c5101172878389dfef99fd95f77cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 12 Apr 2024 13:50:16 -0300 Subject: [PATCH 27/35] Add example project --- examples/project/Concrete.toml | 14 ++++++++++++++ examples/project/src/main.con | 6 ++++++ 2 files changed, 20 insertions(+) create mode 100644 examples/project/Concrete.toml create mode 100644 examples/project/src/main.con diff --git a/examples/project/Concrete.toml b/examples/project/Concrete.toml new file mode 100644 index 0000000..bd51dae --- /dev/null +++ b/examples/project/Concrete.toml @@ -0,0 +1,14 @@ +[package] +name = "project" +version = "0.1.0" +license = "MIT" + +[profile.release] +release = true +opt_level = 3 +debug_info = false + +[profile.dev] +release = false +opt_level = 0 +debug_info = true diff --git a/examples/project/src/main.con b/examples/project/src/main.con new file mode 100644 index 0000000..9c9881b --- /dev/null +++ b/examples/project/src/main.con @@ -0,0 +1,6 @@ + +mod project { + pub fn main() -> i32 { + return 17; + } +} From 9f1bbb2d54c1b39be7e1138e95462afffb28a726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 12 Apr 2024 14:07:20 -0300 Subject: [PATCH 28/35] Fix clippy --- crates/concrete_driver/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/concrete_driver/src/lib.rs b/crates/concrete_driver/src/lib.rs index 9444a83..fc4429b 100644 --- a/crates/concrete_driver/src/lib.rs +++ b/crates/concrete_driver/src/lib.rs @@ -292,7 +292,7 @@ mod {} {{ None => build_command(profile, release)?, }; - Err(std::process::Command::new(&output).exec())?; + Err(std::process::Command::new(output).exec())?; } } From efabea76e6eb73667807acab4c3dedece6a8c58b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 12 Apr 2024 15:01:06 -0300 Subject: [PATCH 29/35] Add print information --- crates/concrete_driver/src/lib.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/crates/concrete_driver/src/lib.rs b/crates/concrete_driver/src/lib.rs index fc4429b..954604b 100644 --- a/crates/concrete_driver/src/lib.rs +++ b/crates/concrete_driver/src/lib.rs @@ -257,7 +257,7 @@ mod {} {{ } => { let output = match path { Some(input) => { - let output_stem = input + let input_stem = input .file_stem() .context("could not get file stem")? .to_str() @@ -267,10 +267,10 @@ mod {} {{ if !build_dir.exists() { std::fs::create_dir_all(&build_dir)?; } - let output = build_dir.join(output_stem); + let output = build_dir.join(&input_stem); let compile_args = CompilerArgs { - input, + input: input.clone(), output: output.clone(), release, optlevel: None, @@ -283,15 +283,32 @@ mod {} {{ object: true, mlir: true, }; - let object = compile(&compile_args)?; + println!( + " {} {} ({})", + "Compiling".green().bold(), + input_stem, + input.display() + ); + + let start = Instant::now(); + let object = compile(&compile_args)?; link_binary(&[object], &output)?; + let elapsed = start.elapsed(); + + println!( + " {} {} in {elapsed:?}", + "Finished".green().bold(), + if release { "release" } else { "dev" }, + ); output } None => build_command(profile, release)?, }; + println!(" {} {}", "Running".green().bold(), &output.display()); + Err(std::process::Command::new(output).exec())?; } } From 083f94e3fa40bbcd9a23153688b5fefae65d550a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 12 Apr 2024 15:07:33 -0300 Subject: [PATCH 30/35] Fix clippy --- crates/concrete_driver/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/concrete_driver/src/lib.rs b/crates/concrete_driver/src/lib.rs index 954604b..aa2d076 100644 --- a/crates/concrete_driver/src/lib.rs +++ b/crates/concrete_driver/src/lib.rs @@ -267,7 +267,7 @@ mod {} {{ if !build_dir.exists() { std::fs::create_dir_all(&build_dir)?; } - let output = build_dir.join(&input_stem); + let output = build_dir.join(input_stem); let compile_args = CompilerArgs { input: input.clone(), From ef166fadc97d71675c3aab3c8e48e5cbd8435c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 12 Apr 2024 16:10:19 -0300 Subject: [PATCH 31/35] Add assert to enforce rule --- crates/concrete_ir/src/lowering.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index 98357cc..acb8693 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -366,12 +366,18 @@ fn lower_statement( statements::Statement::Assign(info) => lower_assign(builder, info)?, statements::Statement::Match(_) => todo!(), statements::Statement::For(_) => todo!(), - statements::Statement::If(info) => lower_if_statement(builder, info)?, + statements::Statement::If(info) => { + lower_if_statement(builder, info)?; + assert!(builder.statements.is_empty()); + } statements::Statement::Let(info) => lower_let(builder, info)?, statements::Statement::Return(info) => { lower_return(builder, info, ret_type)?; } - statements::Statement::While(info) => lower_while(builder, info)?, + statements::Statement::While(info) => { + lower_while(builder, info)?; + assert!(builder.statements.is_empty()); + } statements::Statement::FnCall(info) => { lower_fn_call(builder, info)?; } From c4058199ed328d0e9ccd11323a43b8586e4516e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 12 Apr 2024 16:52:19 -0300 Subject: [PATCH 32/35] Add test for while_if_false.con --- crates/concrete_driver/tests/examples.rs | 1 + examples/while_if_false.con | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 examples/while_if_false.con diff --git a/crates/concrete_driver/tests/examples.rs b/crates/concrete_driver/tests/examples.rs index 0154563..d5cfba6 100644 --- a/crates/concrete_driver/tests/examples.rs +++ b/crates/concrete_driver/tests/examples.rs @@ -16,6 +16,7 @@ mod common; #[test_case(include_str!("../../../examples/structs.con"), "structs", false, 8 ; "structs.con")] #[test_case(include_str!("../../../examples/casts.con"), "casts", false, 2 ; "casts.con")] #[test_case(include_str!("../../../examples/malloc.con"), "malloc", false, 5 ; "malloc.con")] +#[test_case(include_str!("../../../examples/while_if_false.con"), "while_if_false", false, 7 ; "while_if_false.con")] fn example_tests(source: &str, name: &str, is_library: bool, status_code: i32) { assert_eq!( status_code, diff --git a/examples/while_if_false.con b/examples/while_if_false.con new file mode 100644 index 0000000..46f1ad5 --- /dev/null +++ b/examples/while_if_false.con @@ -0,0 +1,14 @@ +mod Example { + fn main() -> i32 { + let mut result: i32 = 7; + + while false { + if false { + return 0; + } + result = 14; + } + + return result; + } +} From abcd93e5aae70fc6f4cee982a1e8c8ec12d6b006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 12 Apr 2024 18:00:01 -0300 Subject: [PATCH 33/35] Add if_if_false.con example --- crates/concrete_driver/tests/examples.rs | 1 + examples/if_if_false.con | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 examples/if_if_false.con diff --git a/crates/concrete_driver/tests/examples.rs b/crates/concrete_driver/tests/examples.rs index d5cfba6..f26846a 100644 --- a/crates/concrete_driver/tests/examples.rs +++ b/crates/concrete_driver/tests/examples.rs @@ -17,6 +17,7 @@ mod common; #[test_case(include_str!("../../../examples/casts.con"), "casts", false, 2 ; "casts.con")] #[test_case(include_str!("../../../examples/malloc.con"), "malloc", false, 5 ; "malloc.con")] #[test_case(include_str!("../../../examples/while_if_false.con"), "while_if_false", false, 7 ; "while_if_false.con")] +#[test_case(include_str!("../../../examples/if_if_false.con"), "if_if_false", false, 7 ; "if_if_false.con")] fn example_tests(source: &str, name: &str, is_library: bool, status_code: i32) { assert_eq!( status_code, diff --git a/examples/if_if_false.con b/examples/if_if_false.con new file mode 100644 index 0000000..a38c67d --- /dev/null +++ b/examples/if_if_false.con @@ -0,0 +1,15 @@ +mod Example { + fn main() -> i32 { + let foo: i32 = 7; + + if true { + if false { + return 1; + } + } else { + foo = 14; + } + + return foo; + } +} From 13b5bb374bc63fe2b6f612ac7ab915e690cf0793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 12 Apr 2024 18:11:11 -0300 Subject: [PATCH 34/35] Fix if bug by always adding terminator block --- crates/concrete_ir/src/lowering.rs | 66 +++++++++--------------------- 1 file changed, 19 insertions(+), 47 deletions(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index acb8693..ba54eeb 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -309,16 +309,14 @@ fn lower_func( lower_statement(&mut builder, stmt, ret_type.clone())?; } - if !builder.statements.is_empty() { - let statements = std::mem::take(&mut builder.statements); - builder.body.basic_blocks.push(BasicBlock { - statements, - terminator: Box::new(Terminator { - span: None, - kind: TerminatorKind::Return, - }), - }); - } + let statements = std::mem::take(&mut builder.statements); + builder.body.basic_blocks.push(BasicBlock { + statements, + terminator: Box::new(Terminator { + span: None, + kind: TerminatorKind::Return, + }), + }); let (mut ctx, body) = (builder.ctx, builder.body); ctx.unresolved_function_signatures.remove(&body.id); @@ -501,13 +499,9 @@ fn lower_if_statement(builder: &mut FnBodyBuilder, info: &IfExpr) -> Result<(), } // keet idx to change terminator - let last_then_block_idx = if !matches!( - builder.body.basic_blocks.last().unwrap().terminator.kind, - TerminatorKind::Return - ) { + let last_then_block_idx = { builder.body.basic_blocks.len(); let statements = std::mem::take(&mut builder.statements); - let idx = builder.body.basic_blocks.len(); builder.body.basic_blocks.push(BasicBlock { statements, terminator: Box::new(Terminator { @@ -515,9 +509,7 @@ fn lower_if_statement(builder: &mut FnBodyBuilder, info: &IfExpr) -> Result<(), kind: TerminatorKind::Unreachable, }), }); - Some(idx) - } else { - None + builder.body.basic_blocks.len() - 1 }; let first_else_block_idx = builder.body.basic_blocks.len(); @@ -530,32 +522,23 @@ fn lower_if_statement(builder: &mut FnBodyBuilder, info: &IfExpr) -> Result<(), builder.body.locals[builder.ret_local].ty.clone(), )?; } - } - let last_else_block_idx = if !matches!( - builder.body.basic_blocks.last().unwrap().terminator.kind, - TerminatorKind::Return - ) { - builder.body.basic_blocks.len(); let statements = std::mem::take(&mut builder.statements); - let idx = builder.body.basic_blocks.len(); builder.body.basic_blocks.push(BasicBlock { statements, terminator: Box::new(Terminator { span: None, - kind: TerminatorKind::Unreachable, + kind: TerminatorKind::Goto { + target: builder.body.basic_blocks.len() + 1, + }, }), }); - Some(idx) - } else { - None - }; + } let targets = SwitchTargets { values: vec![discriminator_type.kind.get_falsy_value()], targets: vec![first_else_block_idx, first_then_block_idx], }; - let kind = TerminatorKind::SwitchInt { discriminator: Operand::Place(place), targets, @@ -564,22 +547,11 @@ fn lower_if_statement(builder: &mut FnBodyBuilder, info: &IfExpr) -> Result<(), let next_block_idx = builder.body.basic_blocks.len(); - // check if the - if let Some(last_then_block_idx) = last_then_block_idx { - builder.body.basic_blocks[last_then_block_idx] - .terminator - .kind = TerminatorKind::Goto { - target: next_block_idx, - }; - } - - if let Some(last_else_block_idx) = last_else_block_idx { - builder.body.basic_blocks[last_else_block_idx] - .terminator - .kind = TerminatorKind::Goto { - target: next_block_idx, - }; - } + builder.body.basic_blocks[last_then_block_idx] + .terminator + .kind = TerminatorKind::Goto { + target: next_block_idx, + }; Ok(()) } From 5573d2c540de4d09f3d1d5609c88b63a3fdb1ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 17 Apr 2024 15:47:04 -0300 Subject: [PATCH 35/35] Uncapitalize error messages --- crates/concrete_driver/src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/concrete_driver/src/lib.rs b/crates/concrete_driver/src/lib.rs index aa2d076..8db5401 100644 --- a/crates/concrete_driver/src/lib.rs +++ b/crates/concrete_driver/src/lib.rs @@ -139,7 +139,7 @@ pub fn main() -> Result<()> { } => { let name = name.unwrap_or_else(|| { path.file_name() - .context("Failed to get project name") + .context("failed to get project name") .unwrap() .to_string_lossy() .to_string() @@ -324,7 +324,7 @@ fn build_command(profile: Option, release: bool) -> Result { current_dir = if let Some(parent) = current_dir.parent() { parent.to_path_buf() } else { - bail!("Couldn't find Concrete.toml"); + bail!("couldn't find Concrete.toml"); }; } else { config_path = Some(current_dir.join("Concrete.toml")); @@ -333,12 +333,12 @@ fn build_command(profile: Option, release: bool) -> Result { } let config_path = match config_path { Some(x) => x, - None => bail!("Couldn't find Concrete.toml"), + None => bail!("couldn't find Concrete.toml"), }; let base_dir = config_path .parent() .context("couldn't get config parent dir")?; - let mut config = File::open(&config_path).context("Failed to open Concrete.toml")?; + let mut config = File::open(&config_path).context("failed to open Concrete.toml")?; let mut buf = String::new(); config.read_to_string(&mut buf)?; let config: Config = toml::from_str(&buf).context("failed to parse Concrete.toml")?; @@ -361,7 +361,7 @@ fn build_command(profile: Option, release: bool) -> Result { config .profile .get(&profile) - .context("Couldn't get requested profile")?, + .context("couldn't get requested profile")?, profile, ) } else if release { @@ -369,7 +369,7 @@ fn build_command(profile: Option, release: bool) -> Result { config .profile .get("release") - .context("Couldn't get profile: release")?, + .context("couldn't get profile: release")?, "release".to_string(), ) } else { @@ -377,7 +377,7 @@ fn build_command(profile: Option, release: bool) -> Result { config .profile .get("dev") - .context("Couldn't get profile: dev")?, + .context("couldn't get profile: dev")?, "dev".to_string(), ) };