forked from M-Labs/nac3
Compare commits
3 Commits
master
...
debug_info
Author | SHA1 | Date |
---|---|---|
ychenfo | 7c5e617e90 | |
ychenfo | 4850df7874 | |
ychenfo | ee3b18bd83 |
|
@ -781,7 +781,37 @@ impl Nac3 {
|
||||||
let context = inkwell::context::Context::create();
|
let context = inkwell::context::Context::create();
|
||||||
let module = context.create_module("attributes_writeback");
|
let module = context.create_module("attributes_writeback");
|
||||||
let builder = context.create_builder();
|
let builder = context.create_builder();
|
||||||
let (_, module, _) = gen_func_impl(&context, &mut generator, ®istry, 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, ®istry, builder, debug_info_builder, module,
|
||||||
attributes_writeback_task, |generator, ctx| {
|
attributes_writeback_task, |generator, ctx| {
|
||||||
attributes_writeback(ctx, generator, inner_resolver.as_ref(), host_attributes)
|
attributes_writeback(ctx, generator, inner_resolver.as_ref(), host_attributes)
|
||||||
}).unwrap();
|
}).unwrap();
|
||||||
|
|
|
@ -17,7 +17,8 @@ use inkwell::{
|
||||||
module::Module,
|
module::Module,
|
||||||
passes::{PassManager, PassManagerBuilder},
|
passes::{PassManager, PassManagerBuilder},
|
||||||
types::{AnyType, BasicType, BasicTypeEnum},
|
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 itertools::Itertools;
|
||||||
use nac3parser::ast::{Stmt, StrRef, Location};
|
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);
|
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 struct CodeGenContext<'ctx, 'a> {
|
||||||
pub ctx: &'ctx Context,
|
pub ctx: &'ctx Context,
|
||||||
pub builder: Builder<'ctx>,
|
pub builder: Builder<'ctx>,
|
||||||
|
@ -78,6 +85,7 @@ pub struct CodeGenContext<'ctx, 'a> {
|
||||||
Option<(Vec<Option<BasicValueEnum<'ctx>>>, BasicBlock<'ctx>, PhiValue<'ctx>)>,
|
Option<(Vec<Option<BasicValueEnum<'ctx>>>, BasicBlock<'ctx>, PhiValue<'ctx>)>,
|
||||||
pub need_sret: bool,
|
pub need_sret: bool,
|
||||||
pub current_loc: Location,
|
pub current_loc: Location,
|
||||||
|
pub debug_info_builder: DebugInfoGenerator<'ctx>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
||||||
|
@ -203,6 +211,36 @@ impl WorkerRegistry {
|
||||||
let mut builder = context.create_builder();
|
let mut builder = context.create_builder();
|
||||||
let module = context.create_module(generator.get_name());
|
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();
|
let pass_builder = PassManagerBuilder::create();
|
||||||
pass_builder.set_optimization_level(OptimizationLevel::Default);
|
pass_builder.set_optimization_level(OptimizationLevel::Default);
|
||||||
let passes = PassManager::create(&module);
|
let passes = PassManager::create(&module);
|
||||||
|
@ -211,14 +249,17 @@ impl WorkerRegistry {
|
||||||
let mut errors = HashSet::new();
|
let mut errors = HashSet::new();
|
||||||
while let Some(task) = self.receiver.recv().unwrap() {
|
while let Some(task) = self.receiver.recv().unwrap() {
|
||||||
let tmp_module = context.create_module("tmp");
|
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) => {
|
Ok(result) => {
|
||||||
builder = result.0;
|
builder = result.0;
|
||||||
|
debug_info_builder = result.3;
|
||||||
passes.run_on(&result.2);
|
passes.run_on(&result.2);
|
||||||
module.link_in_module(result.1).unwrap();
|
module.link_in_module(result.1).unwrap();
|
||||||
}
|
}
|
||||||
Err((old_builder, e)) => {
|
Err((old_builder, old_di, e)) => {
|
||||||
builder = old_builder;
|
builder = old_builder;
|
||||||
|
debug_info_builder = old_di;
|
||||||
errors.insert(e);
|
errors.insert(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -361,15 +402,23 @@ fn need_sret<'ctx>(ctx: &'ctx Context, ty: BasicTypeEnum<'ctx>) -> bool {
|
||||||
need_sret_impl(ctx, ty, true)
|
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,
|
context: &'ctx Context,
|
||||||
generator: &mut G,
|
generator: &mut G,
|
||||||
registry: &WorkerRegistry,
|
registry: &WorkerRegistry,
|
||||||
builder: Builder<'ctx>,
|
builder: Builder<'ctx>,
|
||||||
|
debug_info_builder: DebugInfoGenerator<'ctx>,
|
||||||
module: Module<'ctx>,
|
module: Module<'ctx>,
|
||||||
task: CodeGenTask,
|
task: CodeGenTask,
|
||||||
codegen_function: F
|
codegen_function: F,
|
||||||
) -> 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 top_level_ctx = registry.top_level_ctx.clone();
|
let top_level_ctx = registry.top_level_ctx.clone();
|
||||||
let static_value_store = registry.static_value_store.clone();
|
let static_value_store = registry.static_value_store.clone();
|
||||||
let (mut unifier, primitives) = {
|
let (mut unifier, primitives) = {
|
||||||
|
@ -509,6 +558,35 @@ pub fn gen_func_impl<'ctx, G: CodeGenerator, F: FnOnce(&mut G, &mut CodeGenConte
|
||||||
ret_type.unwrap().as_any_type_enum()));
|
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");
|
let init_bb = context.append_basic_block(fn_val, "init");
|
||||||
builder.position_at_end(init_bb);
|
builder.position_at_end(init_bb);
|
||||||
let body_bb = context.append_basic_block(fn_val, "body");
|
let body_bb = context.append_basic_block(fn_val, "body");
|
||||||
|
@ -568,6 +646,7 @@ pub fn gen_func_impl<'ctx, G: CodeGenerator, F: FnOnce(&mut G, &mut CodeGenConte
|
||||||
primitives,
|
primitives,
|
||||||
init_bb,
|
init_bb,
|
||||||
builder,
|
builder,
|
||||||
|
debug_info_builder,
|
||||||
module,
|
module,
|
||||||
unifier,
|
unifier,
|
||||||
static_value_store,
|
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);
|
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 {
|
if let Err(e) = result {
|
||||||
return Err((builder, e));
|
return Err((builder, debug_info_builder, e));
|
||||||
}
|
}
|
||||||
|
debug_info_builder.dibuilder.finalize();
|
||||||
Ok((builder, module, fn_val))
|
Ok((builder, module, fn_val, debug_info_builder))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gen_func<'ctx, G: CodeGenerator>(
|
pub fn gen_func<'ctx, G: CodeGenerator>(
|
||||||
|
@ -595,14 +674,27 @@ pub fn gen_func<'ctx, G: CodeGenerator>(
|
||||||
generator: &mut G,
|
generator: &mut G,
|
||||||
registry: &WorkerRegistry,
|
registry: &WorkerRegistry,
|
||||||
builder: Builder<'ctx>,
|
builder: Builder<'ctx>,
|
||||||
|
debug_info_builder: DebugInfoGenerator<'ctx>,
|
||||||
module: Module<'ctx>,
|
module: Module<'ctx>,
|
||||||
task: CodeGenTask,
|
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();
|
let body = task.body.clone();
|
||||||
gen_func_impl(context, generator, registry, builder, module, task, |generator, ctx| {
|
gen_func_impl(
|
||||||
for stmt in body.iter() {
|
context,
|
||||||
generator.gen_stmt(ctx, stmt)?;
|
generator,
|
||||||
}
|
registry,
|
||||||
Ok(())
|
builder,
|
||||||
})
|
debug_info_builder,
|
||||||
|
module,
|
||||||
|
task,
|
||||||
|
|generator, ctx| {
|
||||||
|
for stmt in body.iter() {
|
||||||
|
generator.gen_stmt(ctx, stmt)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ use crate::{
|
||||||
use inkwell::{
|
use inkwell::{
|
||||||
attributes::{Attribute, AttributeLoc},
|
attributes::{Attribute, AttributeLoc},
|
||||||
basic_block::BasicBlock,
|
basic_block::BasicBlock,
|
||||||
|
debug_info::AsDIScope,
|
||||||
types::BasicTypeEnum,
|
types::BasicTypeEnum,
|
||||||
values::{BasicValue, BasicValueEnum, FunctionValue, PointerValue},
|
values::{BasicValue, BasicValueEnum, FunctionValue, PointerValue},
|
||||||
IntPredicate::EQ,
|
IntPredicate::EQ,
|
||||||
|
@ -989,6 +990,24 @@ pub fn gen_stmt<'ctx, 'a, G: CodeGenerator>(
|
||||||
stmt: &Stmt<Option<Type>>,
|
stmt: &Stmt<Option<Type>>,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
ctx.current_loc = stmt.location;
|
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 {
|
match &stmt.node {
|
||||||
StmtKind::Pass { .. } => {}
|
StmtKind::Pass { .. } => {}
|
||||||
StmtKind::Expr { value, .. } => {
|
StmtKind::Expr { value, .. } => {
|
||||||
|
|
Loading…
Reference in New Issue