From 1d60bb5482366b06429cbcea5bf416aae9f27f39 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Wed, 14 Feb 2024 10:23:39 +0100 Subject: [PATCH] feat: improved logging --- lib/edlang_codegen_llvm/src/codegen.rs | 16 ++++++++++------ lib/edlang_driver/tests/programs.rs | 1 + lib/edlang_driver/tests/programs/while.ed | 11 +++++++++++ lib/edlang_lowering/src/lib.rs | 6 +++++- programs/while.ed | 11 +++++++++++ 5 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 lib/edlang_driver/tests/programs/while.ed create mode 100644 programs/while.ed diff --git a/lib/edlang_codegen_llvm/src/codegen.rs b/lib/edlang_codegen_llvm/src/codegen.rs index 9ad6ea93e..670b61792 100644 --- a/lib/edlang_codegen_llvm/src/codegen.rs +++ b/lib/edlang_codegen_llvm/src/codegen.rs @@ -168,7 +168,7 @@ pub fn compile(session: &Session, program: &ProgramBody) -> Result Result<(), BuilderError> ); debug_loc = ctx.set_debug_loc(lexical_block.as_debug_info_scope(), body.fn_span); + trace!("compiling entry block"); + let block = ctx.ctx.context.append_basic_block(fn_value, "entry"); ctx.builder.position_at_end(block); @@ -280,6 +282,7 @@ fn compile_fn(ctx: &ModuleCompileCtx, fn_id: DefId) -> Result<(), BuilderError> let mut arg_counter = 0; for (index, local) in body.locals.iter().enumerate() { + trace!("compiling local {}: {:?}", index, local); if let Some(span) = local.span { debug_loc = ctx.set_debug_loc(debug_loc.get_scope(), span); } @@ -344,6 +347,7 @@ fn compile_fn(ctx: &ModuleCompileCtx, fn_id: DefId) -> Result<(), BuilderError> } } + trace!("creating blocks"); let mut blocks = Vec::with_capacity(body.blocks.len()); for (index, _block) in body.blocks.iter().enumerate() { @@ -356,15 +360,15 @@ fn compile_fn(ctx: &ModuleCompileCtx, fn_id: DefId) -> Result<(), BuilderError> ctx.builder.build_unconditional_branch(blocks[0])?; - for (block, llvm_block) in body.blocks.iter().zip(&blocks) { - trace!("compiling block"); + for (block_idx, (block, llvm_block)) in body.blocks.iter().zip(&blocks).enumerate() { + trace!("compiling block {}", block_idx); ctx.builder.position_at_end(*llvm_block); - for stmt in &block.statements { + for (idx, stmt) in block.statements.iter().enumerate() { if let Some(span) = stmt.span { debug_loc = ctx.set_debug_loc(debug_loc.get_scope(), span); } - trace!("compiling stmt"); + trace!("compiling stmt {}: {:?}", idx, stmt.kind); match &stmt.kind { ir::StatementKind::Assign(place, rvalue) => { let local = &body.locals[place.local]; @@ -423,7 +427,7 @@ fn compile_fn(ctx: &ModuleCompileCtx, fn_id: DefId) -> Result<(), BuilderError> } } - trace!("compiling terminator"); + trace!("compiling terminator: {:?}", block.terminator); match &block.terminator { ir::Terminator::Target(id) => { ctx.builder.build_unconditional_branch(blocks[*id])?; diff --git a/lib/edlang_driver/tests/programs.rs b/lib/edlang_driver/tests/programs.rs index 0166063de..371731caf 100644 --- a/lib/edlang_driver/tests/programs.rs +++ b/lib/edlang_driver/tests/programs.rs @@ -6,6 +6,7 @@ mod common; #[test_case(include_str!("programs/simple.ed"), "simple", false, 0, &["1"] ; "simple.ed 1")] #[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")] fn example_tests(source: &str, name: &str, is_library: bool, status_code: i32, args: &[&str]) { let program = compile_program(source, name, is_library).unwrap(); diff --git a/lib/edlang_driver/tests/programs/while.ed b/lib/edlang_driver/tests/programs/while.ed new file mode 100644 index 000000000..825b4d310 --- /dev/null +++ b/lib/edlang_driver/tests/programs/while.ed @@ -0,0 +1,11 @@ +mod Main { + pub fn main(argc: i64) -> i64 { + let mut a: i64 = 0; + + while a < 10 { + a = a + 1; + } + + return a; + } +} diff --git a/lib/edlang_lowering/src/lib.rs b/lib/edlang_lowering/src/lib.rs index f607b1f6c..dc569f1bd 100644 --- a/lib/edlang_lowering/src/lib.rs +++ b/lib/edlang_lowering/src/lib.rs @@ -8,6 +8,7 @@ use ir::{ BasicBlock, Body, DefId, Local, LocalKind, Operand, Place, ProgramBody, Statement, StatementKind, SwitchTarget, Terminator, TypeInfo, TypeKind, }; +use tracing::trace; mod common; mod prepass; @@ -382,6 +383,7 @@ fn find_expr_type(builder: &mut BodyBuilder, info: &ast::Expression) -> Option todo!(), ast::ValueExpr::Path(path) => { // todo: handle full path + dbg!("found local"); builder.get_local(&path.first.name)?.ty.kind.clone() } }, @@ -447,9 +449,11 @@ fn lower_binary_expr( rhs: &ast::Expression, type_hint: Option<&TypeKind>, ) -> (ir::RValue, TypeKind) { + trace!("lowering binary op: {:?}", op); + let (lhs, lhs_ty) = if type_hint.is_none() { let ty = find_expr_type(builder, lhs) - .unwrap_or(find_expr_type(builder, rhs).expect("cant find type")); + .unwrap_or_else(|| find_expr_type(builder, rhs).expect("cant find type")); lower_expr(builder, lhs, Some(&ty)) } else { lower_expr(builder, lhs, type_hint) diff --git a/programs/while.ed b/programs/while.ed new file mode 100644 index 000000000..825b4d310 --- /dev/null +++ b/programs/while.ed @@ -0,0 +1,11 @@ +mod Main { + pub fn main(argc: i64) -> i64 { + let mut a: i64 = 0; + + while a < 10 { + a = a + 1; + } + + return a; + } +}