feat(llvm): Implement Context Boxing pattern for cleaner APIs

Major improvement to reduce parameter explosion (15+ args → 3-4 contexts):
- Add LowerFnCtx/BlockCtx for grouping related parameters
- Add lightweight StrHandle/StrPtr newtypes for string safety
- Implement boxed API wrappers for boxcall/fields/invoke
- Add dev checks infrastructure (NYASH_DEV_CHECK_DISPATCH_ONLY_PHI)

Key achievements:
- lower_boxcall: 16 args → 7 args via boxed API
- fields/invoke: Similar parameter reduction
- BuilderCursor discipline enforced throughout
- String handle invariant: i64 across blocks, i8* only at call sites

Status:
- Internal migration in progress (fields → invoke → marshal)
- Full cutover pending due to borrow checker constraints
- dep_tree_min_string.o generation successful (sealed=ON)

Next: Complete internal migration before flipping to boxed APIs

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Selfhosting Dev
2025-09-13 00:07:38 +09:00
parent 8b48480844
commit 3bef7e8608
11 changed files with 436 additions and 35 deletions

View File

@ -1,16 +1,16 @@
use std::collections::HashMap;
use inkwell::{values::BasicValueEnum as BVE, AddressSpace};
use crate::backend::llvm::context::CodegenContext;
use crate::mir::ValueId;
use super::super::ctx::{LowerFnCtx, BlockCtx};
/// Handle getField/setField; returns true if handled.
use super::super::builder_cursor::BuilderCursor;
pub(super) fn try_handle_field_method<'ctx, 'b>(
codegen: &CodegenContext<'ctx>,
cursor: &mut BuilderCursor<'ctx, 'b>,
cursor: &mut super::super::builder_cursor::BuilderCursor<'ctx, 'b>,
cur_bid: crate::mir::BasicBlockId,
vmap: &mut HashMap<ValueId, inkwell::values::BasicValueEnum<'ctx>>,
dst: &Option<ValueId>,
@ -49,12 +49,12 @@ pub(super) fn try_handle_field_method<'ctx, 'b>(
} else {
return Err("get_field ret expected i64".to_string());
};
let pty = codegen.context.ptr_type(AddressSpace::from(0));
let ptr = codegen
let pty = codegen.context.ptr_type(AddressSpace::from(0));
let ptr = codegen
.builder
.build_int_to_ptr(h, pty, "gf_handle_to_ptr")
.map_err(|e| e.to_string())?;
vmap.insert(*d, ptr.into());
vmap.insert(*d, ptr.into());
}
Ok(true)
}
@ -80,3 +80,28 @@ pub(super) fn try_handle_field_method<'ctx, 'b>(
_ => Ok(false),
}
}
// Boxed wrapper that delegates to the non-boxed implementation
pub(super) fn try_handle_field_method_boxed<'ctx, 'b>(
ctx: &mut LowerFnCtx<'ctx, 'b>,
blk: &BlockCtx<'ctx>,
dst: &Option<ValueId>,
method: &str,
args: &[ValueId],
recv_h: inkwell::values::IntValue<'ctx>,
) -> Result<bool, String> {
try_handle_field_method(
ctx.codegen,
ctx.cursor,
blk.cur_bid,
ctx.vmap,
dst,
method,
args,
recv_h,
ctx.resolver,
ctx.bb_map,
ctx.preds,
ctx.block_end_values,
)
}