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:
@ -4,56 +4,59 @@ use inkwell::{values::BasicValueEnum as BVE, AddressSpace};
|
||||
use crate::backend::llvm::compiler::codegen::types;
|
||||
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::ValueId;
|
||||
use crate::mir::{ValueId, function::MirFunction};
|
||||
|
||||
/// Convert a value to i64 handle/int for plugin invoke (ptr->i64, f64->box->i64)
|
||||
pub(super) fn get_i64<'ctx>(
|
||||
pub(super) fn get_i64<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut crate::backend::llvm::compiler::codegen::instructions::builder_cursor::BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::super::Resolver<'ctx>,
|
||||
cur_bid: crate::mir::BasicBlockId,
|
||||
func: &MirFunction,
|
||||
vmap: &HashMap<ValueId, inkwell::values::BasicValueEnum<'ctx>>,
|
||||
vid: ValueId,
|
||||
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<inkwell::values::IntValue<'ctx>, String> {
|
||||
let i64t = codegen.context.i64_type();
|
||||
let bv = *vmap.get(&vid).ok_or("arg missing")?;
|
||||
match bv {
|
||||
BVE::IntValue(iv) => Ok(iv),
|
||||
BVE::PointerValue(pv) => codegen
|
||||
.builder
|
||||
.build_ptr_to_int(pv, i64t, "p2i")
|
||||
.map_err(|e| e.to_string()),
|
||||
BVE::FloatValue(fv) => {
|
||||
match func.metadata.value_types.get(&vid) {
|
||||
Some(crate::mir::MirType::Float) => {
|
||||
// Box f64 then use its handle
|
||||
let fv = resolver.resolve_f64(codegen, cursor, cur_bid, vid, bb_map, preds, block_end_values, vmap)?;
|
||||
let fnty = i64t.fn_type(&[codegen.context.f64_type().into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.box.from_f64")
|
||||
.unwrap_or_else(|| codegen.module.add_function("nyash.box.from_f64", fnty, None));
|
||||
let call = codegen
|
||||
.builder
|
||||
.build_call(callee, &[fv.into()], "arg_f64_to_box")
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_call(callee, &[fv.into()], "arg_f64_to_box"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_f64 returned void".to_string())?;
|
||||
if let BVE::IntValue(h) = rv {
|
||||
Ok(h)
|
||||
} else {
|
||||
Err("from_f64 ret expected i64".to_string())
|
||||
}
|
||||
if let BVE::IntValue(h) = rv { Ok(h) } else { Err("from_f64 ret expected i64".to_string()) }
|
||||
}
|
||||
_ => Err("unsupported arg value".to_string()),
|
||||
_ => resolver.resolve_i64(codegen, cursor, cur_bid, vid, bb_map, preds, block_end_values, vmap),
|
||||
}
|
||||
}
|
||||
|
||||
/// Classify a value into tag constant i64 (uses types::classify_tag)
|
||||
pub(super) fn get_tag_const<'ctx>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
vmap: &HashMap<ValueId, inkwell::values::BasicValueEnum<'ctx>>,
|
||||
func: &MirFunction,
|
||||
vid: ValueId,
|
||||
) -> inkwell::values::IntValue<'ctx> {
|
||||
let i64t = codegen.context.i64_type();
|
||||
let tag = vmap
|
||||
.get(&vid)
|
||||
.map(|v| types::classify_tag(*v))
|
||||
.unwrap_or(3);
|
||||
let tag = match func.metadata.value_types.get(&vid) {
|
||||
Some(crate::mir::MirType::Float) => 5,
|
||||
Some(crate::mir::MirType::String)
|
||||
| Some(crate::mir::MirType::Box(_))
|
||||
| Some(crate::mir::MirType::Array(_))
|
||||
| Some(crate::mir::MirType::Future(_))
|
||||
| Some(crate::mir::MirType::Unknown) => 8,
|
||||
_ => 3,
|
||||
};
|
||||
i64t.const_int(tag as u64, false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user