refactor(llvm): Complete Resolver pattern implementation across all instructions

Major structural improvement driven by ChatGPT 5 Pro analysis:
- Replace all direct vmap access with Resolver API calls
- Add proper cursor/bb_map/preds/block_end_values to all instruction handlers
- Ensure dominance safety by localizing values through Resolver
- Fix parameter passing in invoke/fields/extern handlers

Key changes:
- boxcall: Use resolver.resolve_i64/ptr instead of direct vmap access
- strings: Remove unused recv_v parameter, use Resolver throughout
- invoke: Add missing context parameters for proper PHI handling
- fields: Add resolver and block context parameters
- flow/arith/maps: Consistent Resolver usage pattern

This addresses the "structural invariant" requirements:
1. All value fetching goes through Resolver (no direct vmap.get)
2. Localization happens at BB boundaries via Resolver
3. Better preparation for PHI-only-in-dispatch pattern

Next: Consider boxing excessive parameters (15+ args in some functions)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Selfhosting Dev
2025-09-12 22:36:20 +09:00
parent f77bbb5878
commit 8b48480844
16 changed files with 516 additions and 503 deletions

View File

@ -10,13 +10,17 @@ use super::super::builder_cursor::BuilderCursor;
pub(super) fn try_handle_field_method<'ctx, 'b>(
codegen: &CodegenContext<'ctx>,
_cursor: &mut BuilderCursor<'ctx, 'b>,
_cur_bid: crate::mir::BasicBlockId,
cursor: &mut BuilderCursor<'ctx, 'b>,
cur_bid: crate::mir::BasicBlockId,
vmap: &mut HashMap<ValueId, inkwell::values::BasicValueEnum<'ctx>>,
dst: &Option<ValueId>,
method: &str,
args: &[ValueId],
recv_h: inkwell::values::IntValue<'ctx>,
resolver: &mut super::super::Resolver<'ctx>,
bb_map: &std::collections::HashMap<crate::mir::BasicBlockId, inkwell::basic_block::BasicBlock<'ctx>>,
preds: &std::collections::HashMap<crate::mir::BasicBlockId, Vec<crate::mir::BasicBlockId>>,
block_end_values: &std::collections::HashMap<crate::mir::BasicBlockId, std::collections::HashMap<ValueId, inkwell::values::BasicValueEnum<'ctx>>>,
) -> Result<bool, String> {
let i64t = codegen.context.i64_type();
match method {
@ -24,19 +28,16 @@ pub(super) fn try_handle_field_method<'ctx, 'b>(
if args.len() != 1 {
return Err("getField expects 1 arg (name)".to_string());
}
let name_p = match vmap.get(&args[0]).copied() {
Some(BVE::PointerValue(pv)) => pv,
_ => return Err("getField name must be pointer".to_string()),
};
let name_p = resolver
.resolve_ptr(codegen, cursor, cur_bid, args[0], bb_map, preds, block_end_values, vmap)?;
let i8p = codegen.context.ptr_type(AddressSpace::from(0));
let fnty = i64t.fn_type(&[i64t.into(), i8p.into()], false);
let callee = codegen
.module
.get_function("nyash.instance.get_field_h")
.unwrap_or_else(|| codegen.module.add_function("nyash.instance.get_field_h", fnty, None));
let call = codegen
.builder
.build_call(callee, &[recv_h.into(), name_p.into()], "getField")
let call = cursor
.emit_instr(cur_bid, |b| b.build_call(callee, &[recv_h.into(), name_p.into()], "getField"))
.map_err(|e| e.to_string())?;
if let Some(d) = dst {
let rv = call
@ -61,24 +62,18 @@ pub(super) fn try_handle_field_method<'ctx, 'b>(
if args.len() != 2 {
return Err("setField expects 2 args (name, value)".to_string());
}
let name_p = match vmap.get(&args[0]).copied() {
Some(BVE::PointerValue(pv)) => pv,
_ => return Err("setField name must be pointer".to_string()),
};
let val_h = match vmap.get(&args[1]).copied() {
Some(BVE::PointerValue(pv)) => codegen.builder.build_ptr_to_int(pv, i64t, "valp2i").map_err(|e| e.to_string())?,
Some(BVE::IntValue(iv)) => iv,
_ => return Err("setField value must be int/handle".to_string()),
};
let name_p = resolver
.resolve_ptr(codegen, cursor, cur_bid, args[0], bb_map, preds, block_end_values, vmap)?;
let val_h = resolver
.resolve_i64(codegen, cursor, cur_bid, args[1], bb_map, preds, block_end_values, vmap)?;
let i8p = codegen.context.ptr_type(AddressSpace::from(0));
let fnty = i64t.fn_type(&[i64t.into(), i8p.into(), i64t.into()], false);
let callee = codegen
.module
.get_function("nyash.instance.set_field_h")
.unwrap_or_else(|| codegen.module.add_function("nyash.instance.set_field_h", fnty, None));
let _ = codegen
.builder
.build_call(callee, &[recv_h.into(), name_p.into(), val_h.into()], "setField")
let _ = cursor
.emit_instr(cur_bid, |b| b.build_call(callee, &[recv_h.into(), name_p.into(), val_h.into()], "setField"))
.map_err(|e| e.to_string())?;
Ok(true)
}