Skip to content

Commit

Permalink
feat: improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Feb 14, 2024
1 parent ce05166 commit 1d60bb5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
16 changes: 10 additions & 6 deletions lib/edlang_codegen_llvm/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub fn compile(session: &Session, program: &ProgramBody) -> Result<PathBuf, Box<

fn compile_module(ctx: &mut ModuleCompileCtx, module_id: DefId) {
let module = ctx.ctx.program.modules.get(&module_id).unwrap();
trace!("compiling module");
trace!("compiling module: {:?}", module_id);
for id in module.functions.iter() {
compile_fn_signature(ctx, *id);
}
Expand Down Expand Up @@ -270,6 +270,8 @@ fn compile_fn(ctx: &ModuleCompileCtx, fn_id: DefId) -> 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);

Expand All @@ -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);
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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];
Expand Down Expand Up @@ -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])?;
Expand Down
1 change: 1 addition & 0 deletions lib/edlang_driver/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
11 changes: 11 additions & 0 deletions lib/edlang_driver/tests/programs/while.ed
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 5 additions & 1 deletion lib/edlang_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -382,6 +383,7 @@ fn find_expr_type(builder: &mut BodyBuilder, info: &ast::Expression) -> Option<T
ast::ValueExpr::Str { .. } => todo!(),
ast::ValueExpr::Path(path) => {
// todo: handle full path
dbg!("found local");
builder.get_local(&path.first.name)?.ty.kind.clone()
}
},
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions programs/while.ed
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 1d60bb5

Please sign in to comment.