Compare commits

..

3 Commits

Author SHA1 Message Date
ychenfo 7c5e617e90 nac3core: add location debug info 2022-04-14 00:51:48 +08:00
ychenfo 4850df7874 nac3core: add function debug info 2022-04-13 18:08:23 +08:00
ychenfo ee3b18bd83 nac3core: add basic debug info metadata 2022-04-13 16:12:14 +08:00
3 changed files with 159 additions and 18 deletions

View File

@ -781,7 +781,37 @@ impl Nac3 {
let context = inkwell::context::Context::create();
let module = context.create_module("attributes_writeback");
let builder = context.create_builder();
let (_, module, _) = gen_func_impl(&context, &mut generator, &registry, builder, module,
module.add_basic_value_flag(
"Debug Info Version",
inkwell::module::FlagBehavior::Warning,
context.i32_type().const_int(3, false),
);
module.add_basic_value_flag(
"Dwarf Version",
inkwell::module::FlagBehavior::Warning,
context.i32_type().const_int(4, false),
);
let (dibuilder, compile_unit) = module.create_debug_info_builder(
true,
/* language */ inkwell::debug_info::DWARFSourceLanguage::Python,
/* filename */ "_attribute_writeback",
/* directory */ ".",
/* producer */ "NAC3",
/* is_optimized */ false,
/* compiler command line flags */ "",
/* runtime_ver */ 0,
/* split_name */ "",
/* kind */ inkwell::debug_info::DWARFEmissionKind::Full,
/* dwo_id */ 0,
/* split_debug_inling */ false,
/* debug_info_for_profiling */ false,
"",
"",
);
let debug_info_builder = nac3core::codegen::DebugInfoGenerator {dibuilder, compile_unit};
let (_, module, _, _) = gen_func_impl(&context, &mut generator, &registry, builder, debug_info_builder, module,
attributes_writeback_task, |generator, ctx| {
attributes_writeback(ctx, generator, inner_resolver.as_ref(), host_attributes)
}).unwrap();

View File

@ -17,7 +17,8 @@ use inkwell::{
module::Module,
passes::{PassManager, PassManagerBuilder},
types::{AnyType, BasicType, BasicTypeEnum},
values::{BasicValueEnum, FunctionValue, PhiValue, PointerValue}
values::{BasicValueEnum, FunctionValue, PhiValue, PointerValue},
debug_info::{DebugInfoBuilder, DICompileUnit, DISubprogram, AsDIScope, DIFlagsConstants},
};
use itertools::Itertools;
use nac3parser::ast::{Stmt, StrRef, Location};
@ -49,6 +50,12 @@ pub struct StaticValueStore {
pub type VarValue<'ctx> = (PointerValue<'ctx>, Option<Arc<dyn StaticValue + Send + Sync>>, i64);
#[derive(Debug)]
pub struct DebugInfoGenerator<'ctx> {
pub dibuilder: DebugInfoBuilder<'ctx>,
pub compile_unit: DICompileUnit<'ctx>,
}
pub struct CodeGenContext<'ctx, 'a> {
pub ctx: &'ctx Context,
pub builder: Builder<'ctx>,
@ -78,6 +85,7 @@ pub struct CodeGenContext<'ctx, 'a> {
Option<(Vec<Option<BasicValueEnum<'ctx>>>, BasicBlock<'ctx>, PhiValue<'ctx>)>,
pub need_sret: bool,
pub current_loc: Location,
pub debug_info_builder: DebugInfoGenerator<'ctx>,
}
impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
@ -203,6 +211,36 @@ impl WorkerRegistry {
let mut builder = context.create_builder();
let module = context.create_module(generator.get_name());
module.add_basic_value_flag(
"Debug Info Version",
inkwell::module::FlagBehavior::Warning,
context.i32_type().const_int(3, false),
);
module.add_basic_value_flag(
"Dwarf Version",
inkwell::module::FlagBehavior::Warning,
context.i32_type().const_int(4, false),
);
let (dibuilder, compile_unit) = module.create_debug_info_builder(
true,
/* language */ inkwell::debug_info::DWARFSourceLanguage::Python,
/* filename */ generator.get_name(),
/* directory */ ".",
/* producer */ "NAC3",
/* is_optimized */ false,
/* compiler command line flags */ "",
/* runtime_ver */ 0,
/* split_name */ "",
/* kind */ inkwell::debug_info::DWARFEmissionKind::Full,
/* dwo_id */ 0,
/* split_debug_inling */ false,
/* debug_info_for_profiling */ false,
"",
"",
);
let mut debug_info_builder = DebugInfoGenerator {dibuilder, compile_unit};
let pass_builder = PassManagerBuilder::create();
pass_builder.set_optimization_level(OptimizationLevel::Default);
let passes = PassManager::create(&module);
@ -211,14 +249,17 @@ impl WorkerRegistry {
let mut errors = HashSet::new();
while let Some(task) = self.receiver.recv().unwrap() {
let tmp_module = context.create_module("tmp");
match gen_func(&context, generator, self, builder, tmp_module, task) {
match gen_func(&context, generator, self, builder, debug_info_builder, tmp_module, task)
{
Ok(result) => {
builder = result.0;
debug_info_builder = result.3;
passes.run_on(&result.2);
module.link_in_module(result.1).unwrap();
}
Err((old_builder, e)) => {
Err((old_builder, old_di, e)) => {
builder = old_builder;
debug_info_builder = old_di;
errors.insert(e);
}
}
@ -361,15 +402,23 @@ fn need_sret<'ctx>(ctx: &'ctx Context, ty: BasicTypeEnum<'ctx>) -> bool {
need_sret_impl(ctx, ty, true)
}
pub fn gen_func_impl<'ctx, G: CodeGenerator, F: FnOnce(&mut G, &mut CodeGenContext) -> Result<(), String>> (
pub fn gen_func_impl<
'ctx,
G: CodeGenerator,
F: FnOnce(&mut G, &mut CodeGenContext) -> Result<(), String>,
>(
context: &'ctx Context,
generator: &mut G,
registry: &WorkerRegistry,
builder: Builder<'ctx>,
debug_info_builder: DebugInfoGenerator<'ctx>,
module: Module<'ctx>,
task: CodeGenTask,
codegen_function: F
) -> Result<(Builder<'ctx>, Module<'ctx>, FunctionValue<'ctx>), (Builder<'ctx>, String)> {
codegen_function: F,
) -> Result<
(Builder<'ctx>, Module<'ctx>, FunctionValue<'ctx>, DebugInfoGenerator<'ctx>),
(Builder<'ctx>, DebugInfoGenerator<'ctx>, String),
> {
let top_level_ctx = registry.top_level_ctx.clone();
let static_value_store = registry.static_value_store.clone();
let (mut unifier, primitives) = {
@ -508,6 +557,35 @@ pub fn gen_func_impl<'ctx, G: CodeGenerator, F: FnOnce(&mut G, &mut CodeGenConte
context.create_type_attribute(Attribute::get_named_enum_kind_id("sret"),
ret_type.unwrap().as_any_type_enum()));
}
{
let ditype = debug_info_builder.dibuilder.create_basic_type(
"_",
0_u64,
0x00,
inkwell::debug_info::DIFlags::PUBLIC,
).unwrap();
let subroutine_type = debug_info_builder.dibuilder.create_subroutine_type(
debug_info_builder.compile_unit.get_file(),
/* return type */ Some(ditype.as_type()),
/* parameter types */ &[],
inkwell::debug_info::DIFlags::PUBLIC,
);
let func_scope: DISubprogram<'_> = debug_info_builder.dibuilder.create_function(
/* scope */ debug_info_builder.compile_unit.as_debug_info_scope(),
/* func name */ symbol,
/* linkage_name */ None,
/* file */ debug_info_builder.compile_unit.get_file(),
/* line_no */ 0,
/* DIType */ subroutine_type,
/* is_local_to_unit */ true,
/* is_definition */ true,
/* scope_line */ 0,
/* flags */ inkwell::debug_info::DIFlags::PUBLIC,
/* is_optimized */ false,
);
fn_val.set_subprogram(func_scope);
}
let init_bb = context.append_basic_block(fn_val, "init");
builder.position_at_end(init_bb);
@ -568,6 +646,7 @@ pub fn gen_func_impl<'ctx, G: CodeGenerator, F: FnOnce(&mut G, &mut CodeGenConte
primitives,
init_bb,
builder,
debug_info_builder,
module,
unifier,
static_value_store,
@ -582,12 +661,12 @@ pub fn gen_func_impl<'ctx, G: CodeGenerator, F: FnOnce(&mut G, &mut CodeGenConte
code_gen_context.builder.build_return(None);
}
let CodeGenContext { builder, module, .. } = code_gen_context;
let CodeGenContext { builder, module, debug_info_builder, .. } = code_gen_context;
if let Err(e) = result {
return Err((builder, e));
return Err((builder, debug_info_builder, e));
}
Ok((builder, module, fn_val))
debug_info_builder.dibuilder.finalize();
Ok((builder, module, fn_val, debug_info_builder))
}
pub fn gen_func<'ctx, G: CodeGenerator>(
@ -595,14 +674,27 @@ pub fn gen_func<'ctx, G: CodeGenerator>(
generator: &mut G,
registry: &WorkerRegistry,
builder: Builder<'ctx>,
debug_info_builder: DebugInfoGenerator<'ctx>,
module: Module<'ctx>,
task: CodeGenTask,
) -> Result<(Builder<'ctx>, Module<'ctx>, FunctionValue<'ctx>), (Builder<'ctx>, String)> {
) -> Result<
(Builder<'ctx>, Module<'ctx>, FunctionValue<'ctx>, DebugInfoGenerator<'ctx>),
(Builder<'ctx>, DebugInfoGenerator<'ctx>, String),
> {
let body = task.body.clone();
gen_func_impl(context, generator, registry, builder, module, task, |generator, ctx| {
for stmt in body.iter() {
generator.gen_stmt(ctx, stmt)?;
}
Ok(())
})
gen_func_impl(
context,
generator,
registry,
builder,
debug_info_builder,
module,
task,
|generator, ctx| {
for stmt in body.iter() {
generator.gen_stmt(ctx, stmt)?;
}
Ok(())
},
)
}

View File

@ -12,6 +12,7 @@ use crate::{
use inkwell::{
attributes::{Attribute, AttributeLoc},
basic_block::BasicBlock,
debug_info::AsDIScope,
types::BasicTypeEnum,
values::{BasicValue, BasicValueEnum, FunctionValue, PointerValue},
IntPredicate::EQ,
@ -989,6 +990,24 @@ pub fn gen_stmt<'ctx, 'a, G: CodeGenerator>(
stmt: &Stmt<Option<Type>>,
) -> Result<(), String> {
ctx.current_loc = stmt.location;
let current_fn = ctx.builder.get_insert_block().unwrap().get_parent().unwrap();
let lexical_block = ctx.debug_info_builder.dibuilder.create_lexical_block(
/* scope */ current_fn.get_subprogram().unwrap().as_debug_info_scope(),
/* file */ ctx.debug_info_builder.compile_unit.get_file(),
/* line_no */ ctx.current_loc.row as u32,
/* column_no */ ctx.current_loc.column as u32,
);
let loc = ctx.debug_info_builder.dibuilder.create_debug_location(
ctx.ctx,
/* line */ ctx.current_loc.row as u32,
/* column_no */ ctx.current_loc.column as u32,
/* current_scope */ lexical_block.as_debug_info_scope(),
/* inlined_at */ None
);
ctx.builder.set_current_debug_location(ctx.ctx, loc);
match &stmt.node {
StmtKind::Pass { .. } => {}
StmtKind::Expr { value, .. } => {