Compare commits

...

3 Commits

Author SHA1 Message Date
ychenfo 90e4c21f35 temp 2022-04-22 15:57:53 +08:00
ychenfo fed4bbe52b nac3core: fix broken tests 2022-04-22 15:57:53 +08:00
ychenfo ce6054c88b nac3core: add debug info 2022-04-22 15:57:53 +08:00
4 changed files with 176 additions and 20 deletions

View File

@ -17,6 +17,7 @@ use crate::{
};
use inkwell::{
AddressSpace,
debug_info::AsDIScope,
attributes::{Attribute, AttributeLoc},
types::{AnyType, BasicType, BasicTypeEnum},
values::{BasicValueEnum, FunctionValue, IntValue, PointerValue}
@ -1023,6 +1024,16 @@ pub fn gen_expr<'ctx, 'a, G: CodeGenerator>(
ctx.current_loc = expr.location;
let int32 = ctx.ctx.i32_type();
let zero = int32.const_int(0, false);
let loc = ctx.debug_info.0.create_debug_location(
ctx.ctx,
ctx.current_loc.row as u32,
ctx.current_loc.column as u32,
ctx.debug_info.2,
None,
);
ctx.builder.set_current_debug_location(ctx.ctx, loc);
Ok(Some(match &expr.node {
ExprKind::Constant { value, .. } => {
let ty = expr.custom.unwrap();

View File

@ -17,7 +17,10 @@ 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, DILexicalBlock, DIScope
},
};
use itertools::Itertools;
use nac3parser::ast::{Stmt, StrRef, Location};
@ -52,6 +55,7 @@ pub type VarValue<'ctx> = (PointerValue<'ctx>, Option<Arc<dyn StaticValue + Send
pub struct CodeGenContext<'ctx, 'a> {
pub ctx: &'ctx Context,
pub builder: Builder<'ctx>,
pub debug_info: (DebugInfoBuilder<'ctx>, DICompileUnit<'ctx>, DIScope<'ctx>),
pub module: Module<'ctx>,
pub top_level: &'a TopLevelContext,
pub unifier: Unifier,
@ -203,6 +207,17 @@ 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 pass_builder = PassManagerBuilder::create();
pass_builder.set_optimization_level(OptimizationLevel::Default);
let passes = PassManager::create(&module);
@ -551,6 +566,72 @@ pub fn gen_func_impl<'ctx, G: CodeGenerator, F: FnOnce(&mut G, &mut CodeGenConte
builder.build_unconditional_branch(body_bb);
builder.position_at_end(body_bb);
let (dibuilder, compile_unit) = module.create_debug_info_builder(
/* allow_unresolved */ true,
/* language */ inkwell::debug_info::DWARFSourceLanguage::Python,
/* filename */
&task
.body
.get(0)
.map_or_else(
|| "<nac3_internal>".to_string(),
|f| {
let name = f.location.file.0.to_string();
if name.contains("__nac3_synthesized_modinit__") {
"<nac3_synthesized_modinit>".to_string()
} else {
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,
/* sysroot */ "",
/* sdk */ "",
);
let subroutine_type = dibuilder.create_subroutine_type(
compile_unit.get_file(),
Some(
dibuilder
.create_basic_type("_", 0_u64, 0x00, inkwell::debug_info::DIFlags::PUBLIC)
.unwrap()
.as_type(),
),
&[],
inkwell::debug_info::DIFlags::PUBLIC,
);
let (row, col) =
task.body.get(0).map_or_else(|| (0, 0), |b| (b.location.row, b.location.column));
let func_scope: DISubprogram<'_> = dibuilder.create_function(
/* scope */ compile_unit.as_debug_info_scope(),
/* func name */ symbol,
/* linkage_name */ None,
/* file */ compile_unit.get_file(),
/* line_no */ row as u32,
/* DIType */ subroutine_type,
/* is_local_to_unit */ true,
/* is_definition */ true,
/* scope_line */ row as u32,
/* flags */ inkwell::debug_info::DIFlags::PUBLIC,
/* is_optimized */ false,
);
fn_val.set_subprogram(func_scope);
// let lexical_block = dibuilder.create_lexical_block(
// /* scope */ func_scope.as_debug_info_scope(),
// /* file */ compile_unit.get_file(),
// /* line_no */ row as u32,
// /* column_no */ col as u32,
// );
let mut code_gen_context = CodeGenContext {
ctx: context,
resolver: task.resolver,
@ -573,8 +654,18 @@ pub fn gen_func_impl<'ctx, G: CodeGenerator, F: FnOnce(&mut G, &mut CodeGenConte
static_value_store,
need_sret: has_sret,
current_loc: Default::default(),
debug_info: (dibuilder, compile_unit, func_scope.as_debug_info_scope()),
};
let loc = code_gen_context.debug_info.0.create_debug_location(
context,
row as u32,
col as u32,
func_scope.as_debug_info_scope(),
None
);
code_gen_context.builder.set_current_debug_location(context, loc);
let result = codegen_function(generator, &mut code_gen_context);
// after static analysis, only void functions can have no return at the end.
@ -582,6 +673,9 @@ 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.unset_current_debug_location();
code_gen_context.debug_info.0.finalize();
let CodeGenContext { builder, module, .. } = code_gen_context;
if let Err(e) = result {
return Err((builder, e));

View File

@ -11,6 +11,7 @@ use crate::{
};
use inkwell::{
attributes::{Attribute, AttributeLoc},
debug_info::AsDIScope,
basic_block::BasicBlock,
types::BasicTypeEnum,
values::{BasicValue, BasicValueEnum, FunctionValue, PointerValue},
@ -989,6 +990,16 @@ pub fn gen_stmt<'ctx, 'a, G: CodeGenerator>(
stmt: &Stmt<Option<Type>>,
) -> Result<(), String> {
ctx.current_loc = stmt.location;
let loc = ctx.debug_info.0.create_debug_location(
ctx.ctx,
ctx.current_loc.row as u32,
ctx.current_loc.column as u32,
ctx.debug_info.2,
None,
);
ctx.builder.set_current_debug_location(ctx.ctx, loc);
match &stmt.node {
StmtKind::Pass { .. } => {}
StmtKind::Expr { value, .. } => {

View File

@ -181,23 +181,43 @@ fn test_primitives() {
; ModuleID = 'test'
source_filename = \"test\"
define i32 @testing(i32 %0, i32 %1) {
define i32 @testing(i32 %0, i32 %1) !dbg !4 {
init:
%add = add i32 %0, %1
%cmp = icmp eq i32 %add, 1
br i1 %cmp, label %then, label %else
%add = add i32 %0, %1, !dbg !9
%cmp = icmp eq i32 %add, 1, !dbg !11
br i1 %cmp, label %then, label %else, !dbg !11
then: ; preds = %init
br label %cont
br label %cont, !dbg !12
else: ; preds = %init
br label %cont
br label %cont, !dbg !13
cont: ; preds = %else, %then
%if_exp_result.0 = phi i32 [ %0, %then ], [ 0, %else ]
ret i32 %if_exp_result.0
%if_exp_result.0 = phi i32 [ %0, %then ], [ 0, %else ], !dbg !14
ret i32 %if_exp_result.0, !dbg !15
}
"}
!llvm.module.flags = !{!0, !1}
!llvm.dbg.cu = !{!2}
!0 = !{i32 2, !\"Debug Info Version\", i32 3}
!1 = !{i32 2, !\"Dwarf Version\", i32 4}
!2 = distinct !DICompileUnit(language: DW_LANG_Python, file: !3, producer: \"NAC3\", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false)
!3 = !DIFile(filename: \"unknown\", directory: \"\")
!4 = distinct !DISubprogram(name: \"testing\", linkageName: \"testing\", scope: null, file: !3, type: !5, flags: DIFlagPublic, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition, unit: !2, retainedNodes: !8)
!5 = !DISubroutineType(flags: DIFlagPublic, types: !6)
!6 = !{!7}
!7 = !DIBasicType(name: \"_\", flags: DIFlagPublic)
!8 = !{}
!9 = !DILocation(line: 1, column: 9, scope: !10)
!10 = distinct !DILexicalBlock(scope: !4, file: !3, line: 1, column: 1)
!11 = !DILocation(line: 2, column: 15, scope: !10)
!12 = !DILocation(line: 2, column: 5, scope: !10)
!13 = !DILocation(line: 2, column: 22, scope: !10)
!14 = !DILocation(line: 0, scope: !10)
!15 = !DILocation(line: 3, column: 8, scope: !10)
"}
.trim();
assert_eq!(expected, module.print_to_string().to_str().unwrap().trim());
})));
@ -341,20 +361,40 @@ fn test_simple_call() {
let expected = indoc! {"
; ModuleID = 'test'
source_filename = \"test\"
define i32 @testing(i32 %0) {
define i32 @testing(i32 %0) !dbg !5 {
init:
%call = call i32 @foo.0(i32 %0)
%mul = mul i32 %call, 2
ret i32 %mul
%call = call i32 @foo.0(i32 %0), !dbg !10
%mul = mul i32 %call, 2, !dbg !12
ret i32 %mul, !dbg !12
}
define i32 @foo.0(i32 %0) {
define i32 @foo.0(i32 %0) !dbg !13 {
init:
%add = add i32 %0, 1
ret i32 %add
%add = add i32 %0, 1, !dbg !14
ret i32 %add, !dbg !14
}
"}
!llvm.module.flags = !{!0, !1}
!llvm.dbg.cu = !{!2, !4}
!0 = !{i32 2, !\"Debug Info Version\", i32 3}
!1 = !{i32 2, !\"Dwarf Version\", i32 4}
!2 = distinct !DICompileUnit(language: DW_LANG_Python, file: !3, producer: \"NAC3\", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false)
!3 = !DIFile(filename: \"unknown\", directory: \"\")
!4 = distinct !DICompileUnit(language: DW_LANG_Python, file: !3, producer: \"NAC3\", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false)
!5 = distinct !DISubprogram(name: \"testing\", linkageName: \"testing\", scope: null, file: !3, type: !6, flags: DIFlagPublic, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition, unit: !2, retainedNodes: !9)
!6 = !DISubroutineType(flags: DIFlagPublic, types: !7)
!7 = !{!8}
!8 = !DIBasicType(name: \"_\", flags: DIFlagPublic)
!9 = !{}
!10 = !DILocation(line: 1, column: 9, scope: !11)
!11 = distinct !DILexicalBlock(scope: !5, file: !3, line: 1, column: 1)
!12 = !DILocation(line: 2, column: 12, scope: !11)
!13 = distinct !DISubprogram(name: \"foo.0\", linkageName: \"foo.0\", scope: null, file: !3, type: !6, flags: DIFlagPublic, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition, unit: !4, retainedNodes: !9)
!14 = !DILocation(line: 1, column: 12, scope: !15)
!15 = distinct !DILexicalBlock(scope: !13, file: !3, line: 1, column: 1)
"}
.trim();
assert_eq!(expected, module.print_to_string().to_str().unwrap().trim());
})));