Compare commits
2 Commits
7c5e617e90
...
9bfcf36016
Author | SHA1 | Date | |
---|---|---|---|
9bfcf36016 | |||
|
879c66cccf |
@ -148,7 +148,7 @@
|
||||
};
|
||||
|
||||
nixConfig = {
|
||||
binaryCachePublicKeys = ["nixbld.m-labs.hk-1:5aSRVA5b320xbNvu30tqxVPXpld73bhtOeH6uAjRyHc="];
|
||||
binaryCaches = ["https://nixbld.m-labs.hk" "https://cache.nixos.org"];
|
||||
extra-trusted-public-keys = "nixbld.m-labs.hk-1:5aSRVA5b320xbNvu30tqxVPXpld73bhtOeH6uAjRyHc=";
|
||||
extra-substituters = "https://nixbld.m-labs.hk";
|
||||
};
|
||||
}
|
||||
|
@ -781,37 +781,7 @@ impl Nac3 {
|
||||
let context = inkwell::context::Context::create();
|
||||
let module = context.create_module("attributes_writeback");
|
||||
let builder = context.create_builder();
|
||||
|
||||
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,
|
||||
let (_, module, _) = gen_func_impl(&context, &mut generator, ®istry, builder, module,
|
||||
attributes_writeback_task, |generator, ctx| {
|
||||
attributes_writeback(ctx, generator, inner_resolver.as_ref(), host_attributes)
|
||||
}).unwrap();
|
||||
|
@ -14,6 +14,7 @@ use crate::{
|
||||
};
|
||||
use inkwell::{
|
||||
AddressSpace,
|
||||
debug_info::AsDIScope,
|
||||
attributes::{Attribute, AttributeLoc},
|
||||
types::{AnyType, BasicType, BasicTypeEnum},
|
||||
values::{BasicValueEnum, FunctionValue, IntValue, PointerValue}
|
||||
@ -969,6 +970,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.as_debug_info_scope(),
|
||||
None,
|
||||
);
|
||||
ctx.builder.set_current_debug_location(ctx.ctx, loc);
|
||||
|
||||
Ok(Some(match &expr.node {
|
||||
ExprKind::Constant { value, .. } => {
|
||||
let ty = expr.custom.unwrap();
|
||||
|
@ -18,7 +18,9 @@ use inkwell::{
|
||||
passes::{PassManager, PassManagerBuilder},
|
||||
types::{AnyType, BasicType, BasicTypeEnum},
|
||||
values::{BasicValueEnum, FunctionValue, PhiValue, PointerValue},
|
||||
debug_info::{DebugInfoBuilder, DICompileUnit, DISubprogram, AsDIScope, DIFlagsConstants},
|
||||
debug_info::{
|
||||
DebugInfoBuilder, DICompileUnit, DISubprogram, AsDIScope, DIFlagsConstants, DILexicalBlock
|
||||
},
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use nac3parser::ast::{Stmt, StrRef, Location};
|
||||
@ -50,15 +52,10 @@ 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>,
|
||||
pub debug_info: (DebugInfoBuilder<'ctx>, DICompileUnit<'ctx>, DILexicalBlock<'ctx>),
|
||||
pub module: Module<'ctx>,
|
||||
pub top_level: &'a TopLevelContext,
|
||||
pub unifier: Unifier,
|
||||
@ -85,7 +82,6 @@ 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> {
|
||||
@ -221,25 +217,6 @@ impl WorkerRegistry {
|
||||
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);
|
||||
@ -249,17 +226,14 @@ 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, debug_info_builder, tmp_module, task)
|
||||
{
|
||||
match gen_func(&context, generator, self, 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, old_di, e)) => {
|
||||
Err((old_builder, e)) => {
|
||||
builder = old_builder;
|
||||
debug_info_builder = old_di;
|
||||
errors.insert(e);
|
||||
}
|
||||
}
|
||||
@ -402,23 +376,15 @@ 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>, DebugInfoGenerator<'ctx>),
|
||||
(Builder<'ctx>, DebugInfoGenerator<'ctx>, String),
|
||||
> {
|
||||
codegen_function: F
|
||||
) -> Result<(Builder<'ctx>, Module<'ctx>, FunctionValue<'ctx>), (Builder<'ctx>, String)> {
|
||||
let top_level_ctx = registry.top_level_ctx.clone();
|
||||
let static_value_store = registry.static_value_store.clone();
|
||||
let (mut unifier, primitives) = {
|
||||
@ -557,35 +523,6 @@ pub fn gen_func_impl<
|
||||
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);
|
||||
@ -629,6 +566,61 @@ pub fn gen_func_impl<
|
||||
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| f.location.file.0.to_string()),
|
||||
/* directory */ ".",
|
||||
/* producer */ "NAC3",
|
||||
/* is_optimized */ false,
|
||||
/* compiler command line flags */ "",
|
||||
/* runtime_ver */ 0,
|
||||
/* split_name */ "",
|
||||
/* kind */ inkwell::debug_info::DWARFEmissionKind::LineTablesOnly,
|
||||
/* 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 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 */ 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 (row, col) =
|
||||
task.body.get(0).map_or_else(|| (0, 0), |b| (b.location.row, b.location.column));
|
||||
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,
|
||||
@ -646,14 +638,23 @@ pub fn gen_func_impl<
|
||||
primitives,
|
||||
init_bb,
|
||||
builder,
|
||||
debug_info_builder,
|
||||
module,
|
||||
unifier,
|
||||
static_value_store,
|
||||
need_sret: has_sret,
|
||||
current_loc: Default::default(),
|
||||
debug_info: (dibuilder, compile_unit, lexical_block),
|
||||
};
|
||||
|
||||
let loc = code_gen_context.debug_info.0.create_debug_location(
|
||||
context,
|
||||
row as u32,
|
||||
col as u32,
|
||||
lexical_block.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.
|
||||
@ -661,12 +662,15 @@ pub fn gen_func_impl<
|
||||
code_gen_context.builder.build_return(None);
|
||||
}
|
||||
|
||||
let CodeGenContext { builder, module, debug_info_builder, .. } = code_gen_context;
|
||||
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, debug_info_builder, e));
|
||||
return Err((builder, e));
|
||||
}
|
||||
debug_info_builder.dibuilder.finalize();
|
||||
Ok((builder, module, fn_val, debug_info_builder))
|
||||
|
||||
Ok((builder, module, fn_val))
|
||||
}
|
||||
|
||||
pub fn gen_func<'ctx, G: CodeGenerator>(
|
||||
@ -674,27 +678,14 @@ 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>, DebugInfoGenerator<'ctx>),
|
||||
(Builder<'ctx>, DebugInfoGenerator<'ctx>, String),
|
||||
> {
|
||||
) -> Result<(Builder<'ctx>, Module<'ctx>, FunctionValue<'ctx>), (Builder<'ctx>, String)> {
|
||||
let body = task.body.clone();
|
||||
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(())
|
||||
},
|
||||
)
|
||||
gen_func_impl(context, generator, registry, builder, module, task, |generator, ctx| {
|
||||
for stmt in body.iter() {
|
||||
generator.gen_stmt(ctx, stmt)?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
@ -11,8 +11,8 @@ use crate::{
|
||||
};
|
||||
use inkwell::{
|
||||
attributes::{Attribute, AttributeLoc},
|
||||
basic_block::BasicBlock,
|
||||
debug_info::AsDIScope,
|
||||
basic_block::BasicBlock,
|
||||
types::BasicTypeEnum,
|
||||
values::{BasicValue, BasicValueEnum, FunctionValue, PointerValue},
|
||||
IntPredicate::EQ,
|
||||
@ -990,21 +990,13 @@ 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(
|
||||
let loc = ctx.debug_info.0.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.current_loc.row as u32,
|
||||
ctx.current_loc.column as u32,
|
||||
ctx.debug_info.2.as_debug_info_scope(),
|
||||
None,
|
||||
);
|
||||
ctx.builder.set_current_debug_location(ctx.ctx, loc);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user