Merge pull request 'nac3core: do not get llvm value too eagerly for kernel invariant' (#253) from kernel_invariant_fix into master

Reviewed-on: M-Labs/nac3#253
This commit is contained in:
pca006132 2022-03-31 12:48:49 +08:00
commit 4f66bdeda9
1 changed files with 13 additions and 12 deletions

View File

@ -1002,21 +1002,22 @@ pub fn gen_expr<'ctx, 'a, G: CodeGenerator>(
let resolver = ctx.resolver.clone(); let resolver = ctx.resolver.clone();
let val = resolver.get_symbol_value(*id, ctx).unwrap(); let val = resolver.get_symbol_value(*id, ctx).unwrap();
// if is option, need to cast pointer to handle None // if is option, need to cast pointer to handle None
match ( match &*ctx.unifier.get_ty(expr.custom.unwrap()) {
&*ctx.unifier.get_ty(expr.custom.unwrap()), TypeEnum::TObj { obj_id, params, .. }
val.to_basic_value_enum(ctx, generator)?
) {
(TypeEnum::TObj { obj_id, params, .. }, BasicValueEnum::PointerValue(ptr))
if *obj_id == ctx.primitives.option.get_obj_id(&ctx.unifier) => if *obj_id == ctx.primitives.option.get_obj_id(&ctx.unifier) =>
{ {
let actual_ptr_ty = ctx.get_llvm_type( if let BasicValueEnum::PointerValue(ptr) = val.to_basic_value_enum(ctx, generator)? {
generator, let actual_ptr_ty = ctx.get_llvm_type(
*params.iter().next().unwrap().1, generator,
) *params.iter().next().unwrap().1,
.ptr_type(AddressSpace::Generic); )
ctx.builder.build_bitcast(ptr, actual_ptr_ty, "option_ptr_cast").into() .ptr_type(AddressSpace::Generic);
ctx.builder.build_bitcast(ptr, actual_ptr_ty, "option_ptr_cast").into()
} else {
unreachable!("option obj must be ptr")
}
} }
val => val.1.into(), _ => val,
} }
} }
}, },