feat: improved logging

This commit is contained in:
Edgar 2024-02-14 10:23:39 +01:00
parent ce0516651f
commit 1d60bb5482
No known key found for this signature in database
GPG key ID: 70ADAE8F35904387
5 changed files with 38 additions and 7 deletions

View file

@ -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);
}
@ -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);
@ -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])?;

View file

@ -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();

View file

@ -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;
}
}

View file

@ -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<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()
}
},
@ -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)

11
programs/while.ed Normal file
View file

@ -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;
}
}