de-rust phase-0: archive Rust LLVM backend to archive/rust-llvm-backend (RESTORE documented); defaults unaffected
This commit is contained in:
670
archive/rust-llvm-backend/llvm/compiler/codegen/function.rs
Normal file
670
archive/rust-llvm-backend/llvm/compiler/codegen/function.rs
Normal file
@ -0,0 +1,670 @@
|
||||
use super::*;
|
||||
|
||||
pub(super) fn lower_one_function<'ctx>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
llvm_func: FunctionValue<'ctx>,
|
||||
func: &crate::mir::function::MirFunction,
|
||||
name: &str,
|
||||
box_type_ids: &HashMap<String, i64>,
|
||||
llvm_funcs: &HashMap<String, FunctionValue<'ctx>>,
|
||||
) -> Result<(), String> {
|
||||
// Create basic blocks (prefix names with function label to avoid any ambiguity)
|
||||
let fn_label = sanitize_symbol(name);
|
||||
let (mut bb_map, entry_bb) =
|
||||
instructions::create_basic_blocks(codegen, llvm_func, func, &fn_label);
|
||||
let mut cursor = instructions::builder_cursor::BuilderCursor::new(&codegen.builder);
|
||||
cursor.at_end(func.entry_block, entry_bb);
|
||||
let mut vmap: HashMap<ValueId, BasicValueEnum> = HashMap::new();
|
||||
let mut allocas: HashMap<ValueId, PointerValue> = HashMap::new();
|
||||
let entry_builder = codegen.context.create_builder();
|
||||
entry_builder.position_at_end(entry_bb);
|
||||
let mut alloca_elem_types: HashMap<ValueId, BasicTypeEnum> = HashMap::new();
|
||||
let mut phis_by_block: HashMap<
|
||||
crate::mir::BasicBlockId,
|
||||
Vec<(ValueId, PhiValue, Vec<(crate::mir::BasicBlockId, ValueId)>)>,
|
||||
> = HashMap::new();
|
||||
// Snapshot of values at the end of each basic block (for sealed-SSA PHI wiring)
|
||||
let mut block_end_values: HashMap<crate::mir::BasicBlockId, HashMap<ValueId, BasicValueEnum>> =
|
||||
HashMap::new();
|
||||
// Build successors and predecessors map (for optional sealed-SSA PHI wiring)
|
||||
let mut succs: HashMap<crate::mir::BasicBlockId, Vec<crate::mir::BasicBlockId>> =
|
||||
HashMap::new();
|
||||
for (bid, block) in &func.blocks {
|
||||
let v: Vec<crate::mir::BasicBlockId> = block.successors.iter().copied().collect();
|
||||
succs.insert(*bid, v);
|
||||
}
|
||||
let mut preds: HashMap<crate::mir::BasicBlockId, Vec<crate::mir::BasicBlockId>> =
|
||||
HashMap::new();
|
||||
for (b, ss) in &succs {
|
||||
for s in ss {
|
||||
preds.entry(*s).or_default().push(*b);
|
||||
}
|
||||
}
|
||||
// Track sealed blocks to know when all preds of a successor are sealed
|
||||
let mut sealed_blocks: std::collections::HashSet<crate::mir::BasicBlockId> =
|
||||
std::collections::HashSet::new();
|
||||
// Bind parameters
|
||||
for (i, pid) in func.params.iter().enumerate() {
|
||||
if let Some(av) = llvm_func.get_nth_param(i as u32) {
|
||||
vmap.insert(*pid, av);
|
||||
}
|
||||
}
|
||||
// Gather block order once for fallthrough handling
|
||||
let block_ids: Vec<crate::mir::BasicBlockId> = func.block_ids().into_iter().collect();
|
||||
|
||||
// Precreate phis
|
||||
for bid in &block_ids {
|
||||
let bb = *bb_map.get(bid).ok_or("missing bb in map")?;
|
||||
codegen.builder.position_at_end(bb);
|
||||
let block = func.blocks.get(bid).unwrap();
|
||||
for inst in block
|
||||
.instructions
|
||||
.iter()
|
||||
.take_while(|i| matches!(i, MirInstruction::Phi { .. }))
|
||||
{
|
||||
if let MirInstruction::Phi { dst, inputs } = inst {
|
||||
let mut phi_ty: Option<BasicTypeEnum> = None;
|
||||
if let Some(mt) = func.metadata.value_types.get(dst) {
|
||||
phi_ty = Some(map_mirtype_to_basic(codegen.context, mt));
|
||||
} else if let Some((_, iv)) = inputs.first() {
|
||||
if let Some(mt) = func.metadata.value_types.get(iv) {
|
||||
phi_ty = Some(map_mirtype_to_basic(codegen.context, mt));
|
||||
}
|
||||
}
|
||||
let phi_ty = phi_ty.unwrap_or_else(|| codegen.context.i64_type().into());
|
||||
let phi = codegen
|
||||
.builder
|
||||
.build_phi(phi_ty, &format!("phi_{}", dst.as_u32()))
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(*dst, phi.as_basic_value());
|
||||
phis_by_block
|
||||
.entry(*bid)
|
||||
.or_default()
|
||||
.push((*dst, phi, inputs.clone()));
|
||||
if std::env::var("NYASH_LLVM_TRACE_PHI").ok().as_deref() == Some("1") {
|
||||
let ty_str = phi
|
||||
.as_basic_value()
|
||||
.get_type()
|
||||
.print_to_string()
|
||||
.to_string();
|
||||
let mut pairs: Vec<String> = Vec::new();
|
||||
for (pb, vid) in inputs {
|
||||
pairs.push(format!("({}->{})", pb.as_u32(), vid.as_u32()));
|
||||
}
|
||||
eprintln!(
|
||||
"[PHI:new] fn={} bb={} dst={} ty={} inputs={}",
|
||||
fn_label,
|
||||
bid.as_u32(),
|
||||
dst.as_u32(),
|
||||
ty_str,
|
||||
pairs.join(",")
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Map of const strings for Call resolution
|
||||
let const_strs = utils::build_const_str_map(func);
|
||||
|
||||
// Lower body
|
||||
let mut loopform_loop_id: u32 = 0;
|
||||
// Default sealed-SSA ON unless explicitly disabled with NYASH_LLVM_PHI_SEALED=0
|
||||
let sealed_mode = std::env::var("NYASH_LLVM_PHI_SEALED").ok().as_deref() != Some("0");
|
||||
// LoopForm registry (per-function lowering; gated)
|
||||
let mut loopform_registry: HashMap<
|
||||
crate::mir::BasicBlockId,
|
||||
(
|
||||
inkwell::basic_block::BasicBlock,
|
||||
PhiValue,
|
||||
PhiValue,
|
||||
inkwell::basic_block::BasicBlock,
|
||||
),
|
||||
> = HashMap::new();
|
||||
let mut loopform_body_to_header: HashMap<crate::mir::BasicBlockId, crate::mir::BasicBlockId> =
|
||||
HashMap::new();
|
||||
// Per-function Resolver for dominance-safe value access (i64 minimal)
|
||||
let mut resolver = instructions::Resolver::new();
|
||||
for (bi, bid) in block_ids.iter().enumerate() {
|
||||
let bb = *bb_map.get(bid).unwrap();
|
||||
// Use cursor to position at BB start for lowering
|
||||
cursor.at_end(*bid, bb);
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!("[LLVM] lowering bb={}", bid.as_u32());
|
||||
}
|
||||
let block = func.blocks.get(bid).unwrap();
|
||||
let mut defined_in_block: std::collections::HashSet<ValueId> =
|
||||
std::collections::HashSet::new();
|
||||
for inst in &block.instructions {
|
||||
match inst {
|
||||
MirInstruction::NewBox {
|
||||
dst,
|
||||
box_type,
|
||||
args,
|
||||
} => {
|
||||
instructions::lower_newbox(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
&mut resolver,
|
||||
*bid,
|
||||
&mut vmap,
|
||||
*dst,
|
||||
box_type,
|
||||
args,
|
||||
box_type_ids,
|
||||
&bb_map,
|
||||
&preds,
|
||||
&block_end_values,
|
||||
)?;
|
||||
defined_in_block.insert(*dst);
|
||||
}
|
||||
MirInstruction::Const { dst, value } => {
|
||||
let bval = match value {
|
||||
ConstValue::Integer(i) => {
|
||||
codegen.context.i64_type().const_int(*i as u64, true).into()
|
||||
}
|
||||
ConstValue::Float(f) => codegen.context.f64_type().const_float(*f).into(),
|
||||
ConstValue::Bool(b) => codegen
|
||||
.context
|
||||
.bool_type()
|
||||
.const_int(*b as u64, false)
|
||||
.into(),
|
||||
ConstValue::String(s) => {
|
||||
// Hoist string creation to entry block to dominate all uses.
|
||||
let entry_term = unsafe { entry_bb.get_terminator() };
|
||||
if let Some(t) = entry_term {
|
||||
entry_builder.position_before(&t);
|
||||
} else {
|
||||
entry_builder.position_at_end(entry_bb);
|
||||
}
|
||||
let gv = entry_builder
|
||||
.build_global_string_ptr(s, "str")
|
||||
.map_err(|e| e.to_string())?;
|
||||
let len = codegen.context.i32_type().const_int(s.len() as u64, false);
|
||||
let rt = codegen.context.ptr_type(inkwell::AddressSpace::from(0));
|
||||
let fn_ty = rt.fn_type(
|
||||
&[
|
||||
codegen
|
||||
.context
|
||||
.ptr_type(inkwell::AddressSpace::from(0))
|
||||
.into(),
|
||||
codegen.context.i32_type().into(),
|
||||
],
|
||||
false,
|
||||
);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash_string_new")
|
||||
.unwrap_or_else(|| {
|
||||
codegen.module.add_function("nyash_string_new", fn_ty, None)
|
||||
});
|
||||
let call = entry_builder
|
||||
.build_call(
|
||||
callee,
|
||||
&[gv.as_pointer_value().into(), len.into()],
|
||||
"strnew",
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
call.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("nyash_string_new returned void".to_string())?
|
||||
}
|
||||
ConstValue::Null => codegen
|
||||
.context
|
||||
.ptr_type(inkwell::AddressSpace::from(0))
|
||||
.const_zero()
|
||||
.into(),
|
||||
ConstValue::Void => codegen.context.i64_type().const_zero().into(),
|
||||
};
|
||||
vmap.insert(*dst, bval);
|
||||
defined_in_block.insert(*dst);
|
||||
}
|
||||
MirInstruction::Call {
|
||||
dst,
|
||||
func: callee,
|
||||
args,
|
||||
..
|
||||
} => {
|
||||
instructions::lower_call(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
&mut resolver,
|
||||
*bid,
|
||||
func,
|
||||
&mut vmap,
|
||||
dst,
|
||||
callee,
|
||||
args,
|
||||
&const_strs,
|
||||
llvm_funcs,
|
||||
&bb_map,
|
||||
&preds,
|
||||
&block_end_values,
|
||||
)?;
|
||||
if let Some(d) = dst {
|
||||
defined_in_block.insert(*d);
|
||||
}
|
||||
}
|
||||
MirInstruction::BoxCall {
|
||||
dst,
|
||||
box_val,
|
||||
method,
|
||||
method_id,
|
||||
args,
|
||||
effects: _,
|
||||
} => {
|
||||
instructions::lower_boxcall(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
&mut resolver,
|
||||
*bid,
|
||||
func,
|
||||
&mut vmap,
|
||||
dst,
|
||||
box_val,
|
||||
method,
|
||||
method_id,
|
||||
args,
|
||||
box_type_ids,
|
||||
&entry_builder,
|
||||
&bb_map,
|
||||
&preds,
|
||||
&block_end_values,
|
||||
)?;
|
||||
if let Some(d) = dst {
|
||||
defined_in_block.insert(*d);
|
||||
}
|
||||
}
|
||||
MirInstruction::ExternCall {
|
||||
dst,
|
||||
iface_name,
|
||||
method_name,
|
||||
args,
|
||||
effects: _,
|
||||
} => {
|
||||
instructions::lower_externcall(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
&mut resolver,
|
||||
*bid,
|
||||
func,
|
||||
&mut vmap,
|
||||
dst,
|
||||
iface_name,
|
||||
method_name,
|
||||
args,
|
||||
&bb_map,
|
||||
&preds,
|
||||
&block_end_values,
|
||||
)?;
|
||||
if let Some(d) = dst {
|
||||
defined_in_block.insert(*d);
|
||||
}
|
||||
}
|
||||
MirInstruction::UnaryOp { dst, op, operand } => {
|
||||
instructions::lower_unary(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
&mut resolver,
|
||||
*bid,
|
||||
func,
|
||||
&mut vmap,
|
||||
*dst,
|
||||
op,
|
||||
operand,
|
||||
&bb_map,
|
||||
&preds,
|
||||
&block_end_values,
|
||||
)?;
|
||||
defined_in_block.insert(*dst);
|
||||
}
|
||||
MirInstruction::BinOp { dst, op, lhs, rhs } => {
|
||||
instructions::lower_binop(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
&mut resolver,
|
||||
*bid,
|
||||
func,
|
||||
&mut vmap,
|
||||
*dst,
|
||||
op,
|
||||
lhs,
|
||||
rhs,
|
||||
&bb_map,
|
||||
&preds,
|
||||
&block_end_values,
|
||||
)?;
|
||||
defined_in_block.insert(*dst);
|
||||
}
|
||||
MirInstruction::Compare { dst, op, lhs, rhs } => {
|
||||
let out = instructions::lower_compare(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
&mut resolver,
|
||||
*bid,
|
||||
func,
|
||||
&vmap,
|
||||
op,
|
||||
lhs,
|
||||
rhs,
|
||||
&bb_map,
|
||||
&preds,
|
||||
&block_end_values,
|
||||
)?;
|
||||
vmap.insert(*dst, out);
|
||||
defined_in_block.insert(*dst);
|
||||
}
|
||||
MirInstruction::Store { value, ptr } => {
|
||||
instructions::lower_store(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
&mut resolver,
|
||||
*bid,
|
||||
&vmap,
|
||||
&mut allocas,
|
||||
&mut alloca_elem_types,
|
||||
value,
|
||||
ptr,
|
||||
&bb_map,
|
||||
&preds,
|
||||
&block_end_values,
|
||||
)?;
|
||||
}
|
||||
MirInstruction::Load { dst, ptr } => {
|
||||
instructions::lower_load(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
*bid,
|
||||
&mut vmap,
|
||||
&mut allocas,
|
||||
&mut alloca_elem_types,
|
||||
dst,
|
||||
ptr,
|
||||
)?;
|
||||
defined_in_block.insert(*dst);
|
||||
}
|
||||
MirInstruction::Phi { .. } => { /* precreated */ }
|
||||
_ => { /* ignore others */ }
|
||||
}
|
||||
// Snapshot end-of-block values
|
||||
let mut snap: HashMap<ValueId, BasicValueEnum> = HashMap::new();
|
||||
for vid in &defined_in_block {
|
||||
if let Some(v) = vmap.get(vid).copied() {
|
||||
snap.insert(*vid, v);
|
||||
}
|
||||
}
|
||||
block_end_values.insert(*bid, snap);
|
||||
}
|
||||
// Terminator handling
|
||||
if let Some(term) = &block.terminator {
|
||||
cursor.at_end(*bid, bb);
|
||||
match term {
|
||||
MirInstruction::Return { value } => {
|
||||
instructions::term_emit_return(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
&mut resolver,
|
||||
*bid,
|
||||
func,
|
||||
&vmap,
|
||||
value,
|
||||
&bb_map,
|
||||
&preds,
|
||||
&block_end_values,
|
||||
)?;
|
||||
}
|
||||
MirInstruction::Jump { target } => {
|
||||
let mut handled = false;
|
||||
if std::env::var("NYASH_ENABLE_LOOPFORM").ok().as_deref() == Some("1")
|
||||
&& std::env::var("NYASH_LOOPFORM_BODY2DISPATCH")
|
||||
.ok()
|
||||
.as_deref()
|
||||
== Some("1")
|
||||
{
|
||||
if let Some(hdr) = loopform_body_to_header.get(bid) {
|
||||
if hdr == target {
|
||||
if let Some((dispatch_bb, tag_phi, payload_phi, _latch_bb)) =
|
||||
loopform_registry.get(hdr)
|
||||
{
|
||||
let i8t = codegen.context.i8_type();
|
||||
let i64t = codegen.context.i64_type();
|
||||
let pred_llbb =
|
||||
*bb_map.get(bid).ok_or("loopform: body llbb missing")?;
|
||||
let z = i8t.const_zero();
|
||||
let pz = i64t.const_zero();
|
||||
tag_phi.add_incoming(&[(&z, pred_llbb)]);
|
||||
payload_phi.add_incoming(&[(&pz, pred_llbb)]);
|
||||
cursor.emit_term(*bid, |b| {
|
||||
b.build_unconditional_branch(*dispatch_bb).unwrap();
|
||||
});
|
||||
handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !handled {
|
||||
instructions::term_emit_jump(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
*bid,
|
||||
target,
|
||||
&bb_map,
|
||||
&phis_by_block,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
MirInstruction::Branch {
|
||||
condition,
|
||||
then_bb,
|
||||
else_bb,
|
||||
} => {
|
||||
let mut handled_by_loopform = false;
|
||||
if std::env::var("NYASH_ENABLE_LOOPFORM").ok().as_deref() == Some("1") {
|
||||
let mut is_back = |start: crate::mir::BasicBlockId| -> u8 {
|
||||
if let Some(b) = func.blocks.get(&start) {
|
||||
if let Some(crate::mir::instruction::MirInstruction::Jump {
|
||||
target,
|
||||
}) = &b.terminator
|
||||
{
|
||||
if target == bid {
|
||||
return 1;
|
||||
}
|
||||
if let Some(b2) = func.blocks.get(target) {
|
||||
if let Some(
|
||||
crate::mir::instruction::MirInstruction::Jump {
|
||||
target: t2,
|
||||
},
|
||||
) = &b2.terminator
|
||||
{
|
||||
if t2 == bid {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
0
|
||||
};
|
||||
let d_then = is_back(*then_bb);
|
||||
let d_else = is_back(*else_bb);
|
||||
let choose_body = if d_then > 0 && d_else == 0 {
|
||||
Some((*then_bb, *else_bb))
|
||||
} else if d_else > 0 && d_then == 0 {
|
||||
Some((*else_bb, *then_bb))
|
||||
} else if d_then > 0 && d_else > 0 {
|
||||
if d_then <= d_else {
|
||||
Some((*then_bb, *else_bb))
|
||||
} else {
|
||||
Some((*else_bb, *then_bb))
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if let Some((body_sel, after_sel)) = choose_body {
|
||||
let body_block = func.blocks.get(&body_sel).unwrap();
|
||||
handled_by_loopform = instructions::lower_while_loopform(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
&mut resolver,
|
||||
func,
|
||||
llvm_func,
|
||||
condition,
|
||||
&body_block.instructions,
|
||||
loopform_loop_id,
|
||||
&fn_label,
|
||||
*bid,
|
||||
body_sel,
|
||||
after_sel,
|
||||
&bb_map,
|
||||
&vmap,
|
||||
&preds,
|
||||
&block_end_values,
|
||||
&mut loopform_registry,
|
||||
&mut loopform_body_to_header,
|
||||
)?;
|
||||
loopform_loop_id = loopform_loop_id.wrapping_add(1);
|
||||
}
|
||||
}
|
||||
if !handled_by_loopform {
|
||||
let cond_norm = instructions::normalize_branch_condition(func, condition);
|
||||
instructions::term_emit_branch(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
&mut resolver,
|
||||
*bid,
|
||||
&cond_norm,
|
||||
then_bb,
|
||||
else_bb,
|
||||
&bb_map,
|
||||
&phis_by_block,
|
||||
&vmap,
|
||||
&preds,
|
||||
&block_end_values,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
cursor.at_end(*bid, bb);
|
||||
if let Some(next_bid) = block_ids.get(bi + 1) {
|
||||
instructions::term_emit_jump(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
*bid,
|
||||
next_bid,
|
||||
&bb_map,
|
||||
&phis_by_block,
|
||||
)?;
|
||||
} else {
|
||||
let entry_first = func.entry_block;
|
||||
instructions::term_emit_jump(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
*bid,
|
||||
&entry_first,
|
||||
&bb_map,
|
||||
&phis_by_block,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!(
|
||||
"[LLVM] no terminator in MIR for bb={} (fallback)",
|
||||
bid.as_u32()
|
||||
);
|
||||
}
|
||||
cursor.at_end(*bid, bb);
|
||||
if let Some(next_bid) = block_ids.get(bi + 1) {
|
||||
instructions::term_emit_jump(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
*bid,
|
||||
next_bid,
|
||||
&bb_map,
|
||||
&phis_by_block,
|
||||
)?;
|
||||
} else {
|
||||
let entry_first = func.entry_block;
|
||||
instructions::term_emit_jump(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
*bid,
|
||||
&entry_first,
|
||||
&bb_map,
|
||||
&phis_by_block,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
if unsafe { bb.get_terminator() }.is_none() {
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!(
|
||||
"[LLVM] extra guard inserting fallback for bb={}",
|
||||
bid.as_u32()
|
||||
);
|
||||
}
|
||||
cursor.at_end(*bid, bb);
|
||||
if let Some(next_bid) = block_ids.get(bi + 1) {
|
||||
instructions::term_emit_jump(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
*bid,
|
||||
next_bid,
|
||||
&bb_map,
|
||||
&phis_by_block,
|
||||
)?;
|
||||
} else {
|
||||
let entry_first = func.entry_block;
|
||||
instructions::term_emit_jump(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
*bid,
|
||||
&entry_first,
|
||||
&bb_map,
|
||||
&phis_by_block,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
if sealed_mode {
|
||||
instructions::flow::seal_block(
|
||||
codegen,
|
||||
&mut cursor,
|
||||
func,
|
||||
*bid,
|
||||
&succs,
|
||||
&bb_map,
|
||||
&phis_by_block,
|
||||
&block_end_values,
|
||||
)?;
|
||||
sealed_blocks.insert(*bid);
|
||||
}
|
||||
}
|
||||
if std::env::var("NYASH_ENABLE_LOOPFORM").ok().as_deref() == Some("1")
|
||||
&& std::env::var("NYASH_LOOPFORM_LATCH2HEADER").ok().as_deref() == Some("1")
|
||||
{
|
||||
for (hdr_bid, (_dispatch_bb, _tag_phi, _payload_phi, latch_bb)) in &loopform_registry {
|
||||
if let Some(phis) = phis_by_block.get(hdr_bid) {
|
||||
instructions::normalize_header_phis_for_latch(codegen, *hdr_bid, *latch_bb, phis)?;
|
||||
}
|
||||
}
|
||||
instructions::dev_check_dispatch_only_phi(&phis_by_block, &loopform_registry);
|
||||
}
|
||||
for bb in llvm_func.get_basic_blocks() {
|
||||
if unsafe { bb.get_terminator() }.is_none() {
|
||||
codegen.builder.position_at_end(bb);
|
||||
let _ = codegen.builder.build_unreachable();
|
||||
}
|
||||
}
|
||||
if !llvm_func.verify(true) {
|
||||
if std::env::var("NYASH_LLVM_DUMP_ON_FAIL").ok().as_deref() == Some("1") {
|
||||
let ir = codegen.module.print_to_string().to_string();
|
||||
let dump_dir = std::path::Path::new("tmp");
|
||||
let _ = std::fs::create_dir_all(dump_dir);
|
||||
let dump_path = dump_dir.join(format!("llvm_fail_{}.ll", sanitize_symbol(name)));
|
||||
let _ = std::fs::write(&dump_path, ir);
|
||||
eprintln!("[LLVM] wrote IR dump: {}", dump_path.display());
|
||||
}
|
||||
return Err(format!("Function verification failed: {}", name));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -0,0 +1,358 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use inkwell::values::BasicValueEnum;
|
||||
|
||||
use super::builder_cursor::BuilderCursor;
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{function::MirFunction, BasicBlockId, CompareOp, ValueId};
|
||||
|
||||
/// Compare lowering: return the resulting BasicValueEnum (i1)
|
||||
pub(in super::super) fn lower_compare<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
func: &MirFunction,
|
||||
vmap: &HashMap<ValueId, BasicValueEnum<'ctx>>,
|
||||
op: &CompareOp,
|
||||
lhs: &ValueId,
|
||||
rhs: &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, BasicValueEnum<'ctx>>,
|
||||
>,
|
||||
) -> Result<BasicValueEnum<'ctx>, String> {
|
||||
use crate::backend::llvm::compiler::helpers::{as_float, as_int};
|
||||
// Synthesize proxy values via Resolver according to metadata
|
||||
let lv: BasicValueEnum<'ctx> = match func.metadata.value_types.get(lhs) {
|
||||
Some(crate::mir::MirType::Float) => resolver
|
||||
.resolve_f64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*lhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?
|
||||
.into(),
|
||||
Some(crate::mir::MirType::String) | Some(crate::mir::MirType::Box(_)) => resolver
|
||||
.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*lhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?
|
||||
.into(),
|
||||
_ => resolver
|
||||
.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*lhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?
|
||||
.into(),
|
||||
};
|
||||
let rv: BasicValueEnum<'ctx> = match func.metadata.value_types.get(rhs) {
|
||||
Some(crate::mir::MirType::Float) => resolver
|
||||
.resolve_f64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*rhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?
|
||||
.into(),
|
||||
Some(crate::mir::MirType::String) | Some(crate::mir::MirType::Box(_)) => resolver
|
||||
.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*rhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?
|
||||
.into(),
|
||||
_ => resolver
|
||||
.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*rhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?
|
||||
.into(),
|
||||
};
|
||||
// String equality/inequality by content when annotated as String/StringBox
|
||||
if matches!(op, CompareOp::Eq | CompareOp::Ne) {
|
||||
let l_is_str = match func.metadata.value_types.get(lhs) {
|
||||
Some(crate::mir::MirType::String) => true,
|
||||
Some(crate::mir::MirType::Box(b)) if b == "StringBox" => true,
|
||||
_ => false,
|
||||
};
|
||||
let r_is_str = match func.metadata.value_types.get(rhs) {
|
||||
Some(crate::mir::MirType::String) => true,
|
||||
Some(crate::mir::MirType::Box(b)) if b == "StringBox" => true,
|
||||
_ => false,
|
||||
};
|
||||
if l_is_str && r_is_str {
|
||||
let i64t = codegen.context.i64_type();
|
||||
// Convert both sides to handles if needed
|
||||
let mut to_handle =
|
||||
|v: BasicValueEnum<'ctx>| -> Result<inkwell::values::IntValue<'ctx>, String> {
|
||||
match v {
|
||||
BasicValueEnum::IntValue(iv) => {
|
||||
if iv.get_type() == i64t {
|
||||
Ok(iv)
|
||||
} else {
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_int_s_extend(iv, i64t, "i2i64")
|
||||
})
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
}
|
||||
BasicValueEnum::PointerValue(pv) => {
|
||||
let fnty = i64t.fn_type(
|
||||
&[codegen
|
||||
.context
|
||||
.ptr_type(inkwell::AddressSpace::from(0))
|
||||
.into()],
|
||||
false,
|
||||
);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.box.from_i8_string")
|
||||
.unwrap_or_else(|| {
|
||||
codegen.module.add_function(
|
||||
"nyash.box.from_i8_string",
|
||||
fnty,
|
||||
None,
|
||||
)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[pv.into()], "str_ptr_to_handle_cmp")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_i8_string returned void".to_string())?;
|
||||
Ok(rv.into_int_value())
|
||||
}
|
||||
_ => Err("unsupported value for string compare".to_string()),
|
||||
}
|
||||
};
|
||||
let lh = to_handle(lv)?;
|
||||
let rh = to_handle(rv)?;
|
||||
let fnty = i64t.fn_type(&[i64t.into(), i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.string.eq_hh")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.string.eq_hh", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[lh.into(), rh.into()], "str_eq_hh")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let iv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("eq_hh returned void".to_string())?
|
||||
.into_int_value();
|
||||
let zero = i64t.const_zero();
|
||||
let pred = if matches!(op, CompareOp::Eq) {
|
||||
inkwell::IntPredicate::NE
|
||||
} else {
|
||||
inkwell::IntPredicate::EQ
|
||||
};
|
||||
let b = cursor
|
||||
.emit_instr(cur_bid, |bd| {
|
||||
bd.build_int_compare(pred, iv, zero, "str_eq_to_bool")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
return Ok(b.into());
|
||||
}
|
||||
}
|
||||
let out = if let (Some(_li0), Some(_ri0)) = (as_int(lv), as_int(rv)) {
|
||||
// Localize integer operands into current block to satisfy dominance
|
||||
let mut li = resolver
|
||||
.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*lhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)
|
||||
.unwrap_or_else(|_| as_int(lv).unwrap());
|
||||
let mut ri = resolver
|
||||
.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*rhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)
|
||||
.unwrap_or_else(|_| as_int(rv).unwrap());
|
||||
// Normalize integer widths: extend the narrower to match the wider to satisfy LLVM
|
||||
let lw = li.get_type().get_bit_width();
|
||||
let rw = ri.get_type().get_bit_width();
|
||||
if lw != rw {
|
||||
if lw < rw {
|
||||
li = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_int_z_extend(li, ri.get_type(), "icmp_zext_l")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
} else {
|
||||
ri = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_int_z_extend(ri, li.get_type(), "icmp_zext_r")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
}
|
||||
use CompareOp as C;
|
||||
let pred = match op {
|
||||
C::Eq => inkwell::IntPredicate::EQ,
|
||||
C::Ne => inkwell::IntPredicate::NE,
|
||||
C::Lt => inkwell::IntPredicate::SLT,
|
||||
C::Le => inkwell::IntPredicate::SLE,
|
||||
C::Gt => inkwell::IntPredicate::SGT,
|
||||
C::Ge => inkwell::IntPredicate::SGE,
|
||||
};
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_compare(pred, li, ri, "icmp"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
} else if let (Some(lf), Some(rf)) = (as_float(lv), as_float(rv)) {
|
||||
use CompareOp as C;
|
||||
let pred = match op {
|
||||
C::Eq => inkwell::FloatPredicate::OEQ,
|
||||
C::Ne => inkwell::FloatPredicate::ONE,
|
||||
C::Lt => inkwell::FloatPredicate::OLT,
|
||||
C::Le => inkwell::FloatPredicate::OLE,
|
||||
C::Gt => inkwell::FloatPredicate::OGT,
|
||||
C::Ge => inkwell::FloatPredicate::OGE,
|
||||
};
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_float_compare(pred, lf, rf, "fcmp"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
} else if let (BasicValueEnum::PointerValue(lp), BasicValueEnum::PointerValue(rp)) = (lv, rv) {
|
||||
// Support pointer equality/inequality comparisons
|
||||
use CompareOp as C;
|
||||
match op {
|
||||
C::Eq | C::Ne => {
|
||||
let i64t = codegen.context.i64_type();
|
||||
let li = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_ptr_to_int(lp, i64t, "pi_l"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
let ri = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_ptr_to_int(rp, i64t, "pi_r"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
let pred = if matches!(op, C::Eq) {
|
||||
inkwell::IntPredicate::EQ
|
||||
} else {
|
||||
inkwell::IntPredicate::NE
|
||||
};
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_compare(pred, li, ri, "pcmp"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
}
|
||||
_ => return Err("unsupported pointer comparison (only Eq/Ne)".to_string()),
|
||||
}
|
||||
} else if let (BasicValueEnum::PointerValue(lp), BasicValueEnum::IntValue(ri)) = (lv, rv) {
|
||||
use CompareOp as C;
|
||||
let i64t = codegen.context.i64_type();
|
||||
let li = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_ptr_to_int(lp, i64t, "pi_l"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
let pred = match op {
|
||||
C::Eq => inkwell::IntPredicate::EQ,
|
||||
C::Ne => inkwell::IntPredicate::NE,
|
||||
C::Lt => inkwell::IntPredicate::SLT,
|
||||
C::Le => inkwell::IntPredicate::SLE,
|
||||
C::Gt => inkwell::IntPredicate::SGT,
|
||||
C::Ge => inkwell::IntPredicate::SGE,
|
||||
};
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_compare(pred, li, ri, "pcmpi"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
} else if let (BasicValueEnum::IntValue(li), BasicValueEnum::PointerValue(rp)) = (lv, rv) {
|
||||
use CompareOp as C;
|
||||
let i64t = codegen.context.i64_type();
|
||||
let ri = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_ptr_to_int(rp, i64t, "pi_r"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
let pred = match op {
|
||||
C::Eq => inkwell::IntPredicate::EQ,
|
||||
C::Ne => inkwell::IntPredicate::NE,
|
||||
C::Lt => inkwell::IntPredicate::SLT,
|
||||
C::Le => inkwell::IntPredicate::SLE,
|
||||
C::Gt => inkwell::IntPredicate::SGT,
|
||||
C::Ge => inkwell::IntPredicate::SGE,
|
||||
};
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_compare(pred, li, ri, "pcmpi"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
} else {
|
||||
return Err("compare type mismatch".to_string());
|
||||
};
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn guessed_zero<'ctx>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
func: &MirFunction,
|
||||
vid: &crate::mir::ValueId,
|
||||
) -> BasicValueEnum<'ctx> {
|
||||
use crate::mir::MirType as MT;
|
||||
match func.metadata.value_types.get(vid) {
|
||||
Some(MT::Bool) => codegen.context.bool_type().const_zero().into(),
|
||||
Some(MT::Integer) => codegen.context.i64_type().const_zero().into(),
|
||||
Some(MT::Float) => codegen.context.f64_type().const_zero().into(),
|
||||
Some(MT::String) | Some(MT::Box(_)) | Some(MT::Array(_)) | Some(MT::Future(_))
|
||||
| Some(MT::Unknown) | Some(MT::Void) | None => codegen
|
||||
.context
|
||||
.ptr_type(inkwell::AddressSpace::from(0))
|
||||
.const_zero()
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,512 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::backend::llvm::compiler::codegen::types;
|
||||
use inkwell::{values::BasicValueEnum, AddressSpace};
|
||||
|
||||
use super::builder_cursor::BuilderCursor;
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{function::MirFunction, instruction::UnaryOp, BasicBlockId, BinaryOp, ValueId};
|
||||
|
||||
/// Lower UnaryOp and store into vmap (0-diff)
|
||||
pub(in super::super) fn lower_unary<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
func: &MirFunction,
|
||||
vmap: &mut HashMap<ValueId, BasicValueEnum<'ctx>>,
|
||||
dst: ValueId,
|
||||
op: &UnaryOp,
|
||||
operand: &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, BasicValueEnum<'ctx>>,
|
||||
>,
|
||||
) -> Result<(), String> {
|
||||
use crate::mir::MirType as MT;
|
||||
let out = match op {
|
||||
UnaryOp::Neg => match func.metadata.value_types.get(operand) {
|
||||
Some(MT::Float) => {
|
||||
let fv = resolver.resolve_f64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*operand,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_float_neg(fv, "fneg"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
}
|
||||
_ => {
|
||||
let iv = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*operand,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_neg(iv, "ineg"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
}
|
||||
},
|
||||
UnaryOp::Not | UnaryOp::BitNot => {
|
||||
let iv = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*operand,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_not(iv, "inot"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
}
|
||||
};
|
||||
vmap.insert(dst, out);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Lower BinOp and store into vmap (includes concat fallback)
|
||||
pub(in super::super) fn lower_binop<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
func: &MirFunction,
|
||||
vmap: &mut HashMap<ValueId, BasicValueEnum<'ctx>>,
|
||||
dst: ValueId,
|
||||
op: &BinaryOp,
|
||||
lhs: &ValueId,
|
||||
rhs: &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, BasicValueEnum<'ctx>>,
|
||||
>,
|
||||
) -> Result<(), String> {
|
||||
use crate::backend::llvm::compiler::helpers::{as_float, as_int};
|
||||
use inkwell::values::BasicValueEnum as BVE;
|
||||
use inkwell::IntPredicate;
|
||||
// Construct lhs/rhs proxy values via Resolver according to metadata (no vmap direct access)
|
||||
let lv: BasicValueEnum<'ctx> = match func.metadata.value_types.get(lhs) {
|
||||
Some(crate::mir::MirType::Float) => resolver
|
||||
.resolve_f64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*lhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?
|
||||
.into(),
|
||||
Some(crate::mir::MirType::String) | Some(crate::mir::MirType::Box(_)) => resolver
|
||||
.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*lhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?
|
||||
.into(),
|
||||
_ => resolver
|
||||
.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*lhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?
|
||||
.into(),
|
||||
};
|
||||
let rv: BasicValueEnum<'ctx> = match func.metadata.value_types.get(rhs) {
|
||||
Some(crate::mir::MirType::Float) => resolver
|
||||
.resolve_f64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*rhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?
|
||||
.into(),
|
||||
Some(crate::mir::MirType::String) | Some(crate::mir::MirType::Box(_)) => resolver
|
||||
.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*rhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?
|
||||
.into(),
|
||||
_ => resolver
|
||||
.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*rhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?
|
||||
.into(),
|
||||
};
|
||||
let mut handled_concat = false;
|
||||
if let BinaryOp::Add = op {
|
||||
let i8p = codegen.context.ptr_type(AddressSpace::from(0));
|
||||
let is_stringish = |vid: &ValueId| -> bool {
|
||||
match func.metadata.value_types.get(vid) {
|
||||
Some(crate::mir::MirType::String) => true,
|
||||
Some(crate::mir::MirType::Box(_)) => true,
|
||||
_ => false,
|
||||
}
|
||||
};
|
||||
match (lv, rv) {
|
||||
(BVE::PointerValue(lp), BVE::PointerValue(rp)) => {
|
||||
let fnty = i8p.fn_type(&[i8p.into(), i8p.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.string.concat_ss")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.string.concat_ss", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[lp.into(), rp.into()], "concat_ss")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("concat_ss returned void".to_string())?;
|
||||
// store as handle (i64) across blocks
|
||||
let i64t = codegen.context.i64_type();
|
||||
let h = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_ptr_to_int(rv.into_pointer_value(), i64t, "str_ptr2i")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(dst, h.into());
|
||||
handled_concat = true;
|
||||
}
|
||||
(BVE::PointerValue(lp), BVE::IntValue(ri)) => {
|
||||
if is_stringish(lhs) && is_stringish(rhs) {
|
||||
let i64t = codegen.context.i64_type();
|
||||
let fnty_conv = i64t.fn_type(&[i8p.into()], false);
|
||||
let conv = codegen
|
||||
.module
|
||||
.get_function("nyash.box.from_i8_string")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.box.from_i8_string", fnty_conv, None)
|
||||
});
|
||||
let call_c = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(conv, &[lp.into()], "lhs_i8_to_handle")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let lh = call_c
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_i8_string returned void".to_string())?
|
||||
.into_int_value();
|
||||
let fnty_hh = i64t.fn_type(&[i64t.into(), i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.string.concat_hh")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.string.concat_hh", fnty_hh, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[lh.into(), ri.into()], "concat_hh")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("concat_hh returned void".to_string())?;
|
||||
vmap.insert(dst, rv);
|
||||
handled_concat = true;
|
||||
} else {
|
||||
let i64t = codegen.context.i64_type();
|
||||
let fnty = i8p.fn_type(&[i8p.into(), i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.string.concat_si")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.string.concat_si", fnty, None)
|
||||
});
|
||||
let call = codegen
|
||||
.builder
|
||||
.build_call(callee, &[lp.into(), ri.into()], "concat_si")
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("concat_si returned void".to_string())?;
|
||||
let i64t = codegen.context.i64_type();
|
||||
let h = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_ptr_to_int(rv.into_pointer_value(), i64t, "str_ptr2i")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(dst, h.into());
|
||||
handled_concat = true;
|
||||
}
|
||||
}
|
||||
(BVE::IntValue(li), BVE::PointerValue(rp)) => {
|
||||
if is_stringish(lhs) && is_stringish(rhs) {
|
||||
let i64t = codegen.context.i64_type();
|
||||
let fnty_conv = i64t.fn_type(&[i8p.into()], false);
|
||||
let conv = codegen
|
||||
.module
|
||||
.get_function("nyash.box.from_i8_string")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.box.from_i8_string", fnty_conv, None)
|
||||
});
|
||||
let call_c = codegen
|
||||
.builder
|
||||
.build_call(conv, &[rp.into()], "rhs_i8_to_handle")
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rh = call_c
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_i8_string returned void".to_string())?
|
||||
.into_int_value();
|
||||
let fnty_hh = i64t.fn_type(&[i64t.into(), i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.string.concat_hh")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.string.concat_hh", fnty_hh, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[li.into(), rh.into()], "concat_hh")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("concat_hh returned void".to_string())?;
|
||||
vmap.insert(dst, rv);
|
||||
handled_concat = true;
|
||||
} else {
|
||||
let i64t = codegen.context.i64_type();
|
||||
let fnty = i8p.fn_type(&[i64t.into(), i8p.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.string.concat_is")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.string.concat_is", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[li.into(), rp.into()], "concat_is")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("concat_is returned void".to_string())?;
|
||||
let i64t = codegen.context.i64_type();
|
||||
let h = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_ptr_to_int(rv.into_pointer_value(), i64t, "str_ptr2i")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(dst, h.into());
|
||||
handled_concat = true;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if handled_concat {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let out = if let (Some(_li0), Some(_ri0)) = (as_int(lv), as_int(rv)) {
|
||||
// Localize integer operands into current block to satisfy dominance (normalize to i64)
|
||||
let li = resolver
|
||||
.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*lhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)
|
||||
.unwrap_or_else(|_| codegen.context.i64_type().const_zero());
|
||||
let ri = resolver
|
||||
.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*rhs,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)
|
||||
.unwrap_or_else(|_| codegen.context.i64_type().const_zero());
|
||||
use BinaryOp as B;
|
||||
match op {
|
||||
B::Add => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_add(li, ri, "iadd"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
B::Sub => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_sub(li, ri, "isub"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
B::Mul => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_mul(li, ri, "imul"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
B::Div => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_signed_div(li, ri, "idiv"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
B::Mod => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_signed_rem(li, ri, "imod"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
B::BitAnd => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_and(li, ri, "iand"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
B::BitOr => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_or(li, ri, "ior"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
B::BitXor => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_xor(li, ri, "ixor"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
B::Shl => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_left_shift(li, ri, "ishl"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
B::Shr => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_right_shift(li, ri, false, "ishr"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
B::And | B::Or => {
|
||||
// Treat as logical on integers: convert to i1 and and/or
|
||||
let lb = types::to_bool(codegen.context, li.into(), &codegen.builder)?;
|
||||
let rb = types::to_bool(codegen.context, ri.into(), &codegen.builder)?;
|
||||
match op {
|
||||
B::And => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_and(lb, rb, "land"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
_ => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_or(lb, rb, "lor"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if let (Some(lf), Some(rf)) = (as_float(lv), as_float(rv)) {
|
||||
use BinaryOp as B;
|
||||
match op {
|
||||
B::Add => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_float_add(lf, rf, "fadd"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
B::Sub => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_float_sub(lf, rf, "fsub"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
B::Mul => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_float_mul(lf, rf, "fmul"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
B::Div => cursor
|
||||
.emit_instr(cur_bid, |b| b.build_float_div(lf, rf, "fdiv"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into(),
|
||||
B::Mod => return Err("fmod not supported yet".to_string()),
|
||||
_ => return Err("bit/logic ops on float".to_string()),
|
||||
}
|
||||
} else if let (BasicValueEnum::PointerValue(lp), BasicValueEnum::PointerValue(rp)) = (lv, rv) {
|
||||
// Support pointer addition/subtraction if needed? For now, only equality is in compare.
|
||||
return Err("unsupported pointer binop".to_string());
|
||||
} else {
|
||||
return Err("binop type mismatch".to_string());
|
||||
};
|
||||
vmap.insert(dst, out);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn guessed_zero<'ctx>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
func: &MirFunction,
|
||||
vid: &ValueId,
|
||||
) -> BasicValueEnum<'ctx> {
|
||||
use crate::mir::MirType as MT;
|
||||
match func.metadata.value_types.get(vid) {
|
||||
Some(MT::Bool) => codegen.context.bool_type().const_zero().into(),
|
||||
Some(MT::Integer) => codegen.context.i64_type().const_zero().into(),
|
||||
Some(MT::Float) => codegen.context.f64_type().const_zero().into(),
|
||||
Some(MT::String) | Some(MT::Box(_)) | Some(MT::Array(_)) | Some(MT::Future(_))
|
||||
| Some(MT::Unknown) | Some(MT::Void) | None => codegen
|
||||
.context
|
||||
.ptr_type(AddressSpace::from(0))
|
||||
.const_zero()
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,178 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use inkwell::values::BasicValueEnum as BVE;
|
||||
|
||||
use super::builder_cursor::BuilderCursor;
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{function::MirFunction, BasicBlockId, ValueId};
|
||||
|
||||
/// Handle ArrayBox fast-paths. Returns true if handled.
|
||||
pub(super) fn try_handle_array_method<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
func: &MirFunction,
|
||||
vmap: &mut HashMap<ValueId, inkwell::values::BasicValueEnum<'ctx>>,
|
||||
dst: &Option<ValueId>,
|
||||
box_val: &ValueId,
|
||||
method: &str,
|
||||
args: &[ValueId],
|
||||
recv_h: inkwell::values::IntValue<'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> {
|
||||
// Only when receiver is ArrayBox
|
||||
let is_array = matches!(func.metadata.value_types.get(box_val), Some(crate::mir::MirType::Box(b)) if b == "ArrayBox")
|
||||
|| matches!(method, "get" | "set" | "push" | "length");
|
||||
if !is_array {
|
||||
return Ok(false);
|
||||
}
|
||||
let i64t = codegen.context.i64_type();
|
||||
match method {
|
||||
"get" => {
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!("[LLVM] lower Array.get (core)");
|
||||
}
|
||||
if args.len() != 1 {
|
||||
return Err("ArrayBox.get expects 1 arg".to_string());
|
||||
}
|
||||
let idx_i = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i64t.fn_type(&[i64t.into(), i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash_array_get_h")
|
||||
.unwrap_or_else(|| codegen.module.add_function("nyash_array_get_h", fnty, None));
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[recv_h.into(), idx_i.into()], "aget")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("array_get_h returned void".to_string())?;
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
"set" => {
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!("[LLVM] lower Array.set (core)");
|
||||
}
|
||||
if args.len() != 2 {
|
||||
return Err("ArrayBox.set expects 2 arg".to_string());
|
||||
}
|
||||
let idx_i = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let val_i = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[1],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i64t.fn_type(&[i64t.into(), i64t.into(), i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash_array_set_h")
|
||||
.unwrap_or_else(|| codegen.module.add_function("nyash_array_set_h", fnty, None));
|
||||
let _ = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[recv_h.into(), idx_i.into(), val_i.into()], "aset")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(true)
|
||||
}
|
||||
"push" => {
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!("[LLVM] lower Array.push (core)");
|
||||
}
|
||||
if args.len() != 1 {
|
||||
return Err("ArrayBox.push expects 1 arg".to_string());
|
||||
}
|
||||
let val_i = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i64t.fn_type(&[i64t.into(), i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash_array_push_h")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash_array_push_h", fnty, None)
|
||||
});
|
||||
let _ = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[recv_h.into(), val_i.into()], "apush")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(true)
|
||||
}
|
||||
"length" => {
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!("[LLVM] lower Array.length (core)");
|
||||
}
|
||||
if !args.is_empty() {
|
||||
return Err("ArrayBox.length expects 0 arg".to_string());
|
||||
}
|
||||
let fnty = i64t.fn_type(&[i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash_array_length_h")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash_array_length_h", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_call(callee, &[recv_h.into()], "alen"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("array_length_h returned void".to_string())?;
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
_ => Ok(false),
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,113 @@
|
||||
use inkwell::basic_block::BasicBlock;
|
||||
use inkwell::values::{BasicValueEnum, FunctionValue, PhiValue};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::super::types::map_mirtype_to_basic;
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{function::MirFunction, BasicBlockId, ValueId};
|
||||
|
||||
// Small, safe extraction: create LLVM basic blocks for a MIR function and
|
||||
// return the block map together with the entry block.
|
||||
pub(in super::super) fn create_basic_blocks<'ctx>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
llvm_func: FunctionValue<'ctx>,
|
||||
func: &MirFunction,
|
||||
fn_label: &str,
|
||||
) -> (HashMap<BasicBlockId, BasicBlock<'ctx>>, BasicBlock<'ctx>) {
|
||||
let mut bb_map: HashMap<BasicBlockId, BasicBlock> = HashMap::new();
|
||||
let entry_first = func.entry_block;
|
||||
let entry_bb = codegen.context.append_basic_block(
|
||||
llvm_func,
|
||||
&format!("{}_bb{}", fn_label, entry_first.as_u32()),
|
||||
);
|
||||
bb_map.insert(entry_first, entry_bb);
|
||||
for bid in func.block_ids() {
|
||||
if bid == entry_first {
|
||||
continue;
|
||||
}
|
||||
let name = format!("{}_bb{}", fn_label, bid.as_u32());
|
||||
let bb = codegen.context.append_basic_block(llvm_func, &name);
|
||||
bb_map.insert(bid, bb);
|
||||
}
|
||||
(bb_map, entry_bb)
|
||||
}
|
||||
|
||||
// Pre-create PHI nodes for all blocks; also inserts placeholder values into vmap.
|
||||
pub(in super::super) fn precreate_phis<'ctx>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
func: &MirFunction,
|
||||
bb_map: &HashMap<BasicBlockId, BasicBlock<'ctx>>,
|
||||
vmap: &mut HashMap<ValueId, BasicValueEnum<'ctx>>,
|
||||
) -> Result<
|
||||
HashMap<BasicBlockId, Vec<(ValueId, PhiValue<'ctx>, Vec<(BasicBlockId, ValueId)>)>>,
|
||||
String,
|
||||
> {
|
||||
use super::super::types::map_mirtype_to_basic;
|
||||
let mut phis_by_block: HashMap<
|
||||
BasicBlockId,
|
||||
Vec<(ValueId, PhiValue<'ctx>, Vec<(BasicBlockId, ValueId)>)>,
|
||||
> = HashMap::new();
|
||||
for bid in func.block_ids() {
|
||||
let bb = *bb_map.get(&bid).ok_or("missing bb in map")?;
|
||||
codegen.builder.position_at_end(bb);
|
||||
let block = func.blocks.get(&bid).unwrap();
|
||||
for inst in block
|
||||
.instructions
|
||||
.iter()
|
||||
.take_while(|i| matches!(i, crate::mir::instruction::MirInstruction::Phi { .. }))
|
||||
{
|
||||
if let crate::mir::instruction::MirInstruction::Phi { dst, inputs } = inst {
|
||||
let mut phi_ty: Option<inkwell::types::BasicTypeEnum> = None;
|
||||
// Prefer pointer when any input (or dst) is String/Box/Array/Future/Unknown
|
||||
let mut wants_ptr = false;
|
||||
if let Some(mt) = func.metadata.value_types.get(dst) {
|
||||
wants_ptr |= matches!(
|
||||
mt,
|
||||
crate::mir::MirType::String
|
||||
| crate::mir::MirType::Box(_)
|
||||
| crate::mir::MirType::Array(_)
|
||||
| crate::mir::MirType::Future(_)
|
||||
| crate::mir::MirType::Unknown
|
||||
);
|
||||
}
|
||||
for (_, iv) in inputs.iter() {
|
||||
if let Some(mt) = func.metadata.value_types.get(iv) {
|
||||
wants_ptr |= matches!(
|
||||
mt,
|
||||
crate::mir::MirType::String
|
||||
| crate::mir::MirType::Box(_)
|
||||
| crate::mir::MirType::Array(_)
|
||||
| crate::mir::MirType::Future(_)
|
||||
| crate::mir::MirType::Unknown
|
||||
);
|
||||
}
|
||||
}
|
||||
if wants_ptr {
|
||||
phi_ty = Some(
|
||||
codegen
|
||||
.context
|
||||
.ptr_type(inkwell::AddressSpace::from(0))
|
||||
.into(),
|
||||
);
|
||||
} else if let Some(mt) = func.metadata.value_types.get(dst) {
|
||||
phi_ty = Some(map_mirtype_to_basic(codegen.context, mt));
|
||||
} else if let Some((_, iv)) = inputs.first() {
|
||||
if let Some(mt) = func.metadata.value_types.get(iv) {
|
||||
phi_ty = Some(map_mirtype_to_basic(codegen.context, mt));
|
||||
}
|
||||
}
|
||||
let phi_ty = phi_ty.unwrap_or_else(|| codegen.context.i64_type().into());
|
||||
let phi = codegen
|
||||
.builder
|
||||
.build_phi(phi_ty, &format!("phi_{}", dst.as_u32()))
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(*dst, phi.as_basic_value());
|
||||
phis_by_block
|
||||
.entry(bid)
|
||||
.or_default()
|
||||
.push((*dst, phi, inputs.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(phis_by_block)
|
||||
}
|
||||
@ -0,0 +1,629 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use inkwell::values::BasicValueEnum as BVE;
|
||||
use inkwell::AddressSpace;
|
||||
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
mod fields;
|
||||
pub(crate) mod invoke;
|
||||
mod marshal;
|
||||
use self::invoke as invoke_mod;
|
||||
use self::marshal as marshal_mod;
|
||||
use super::builder_cursor::BuilderCursor;
|
||||
use super::ctx::{BlockCtx, LowerFnCtx};
|
||||
use crate::mir::{function::MirFunction, BasicBlockId, ValueId};
|
||||
|
||||
// BoxCall lowering (large): mirrors existing logic; kept in one function for now
|
||||
pub(in super::super) fn lower_boxcall<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
func: &MirFunction,
|
||||
vmap: &mut HashMap<ValueId, inkwell::values::BasicValueEnum<'ctx>>,
|
||||
dst: &Option<ValueId>,
|
||||
box_val: &ValueId,
|
||||
method: &str,
|
||||
method_id: &Option<u16>,
|
||||
args: &[ValueId],
|
||||
box_type_ids: &HashMap<String, i64>,
|
||||
entry_builder: &inkwell::builder::Builder<'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<(), String> {
|
||||
use super::super::types::classify_tag;
|
||||
use crate::backend::llvm::compiler::helpers::{as_float, as_int};
|
||||
let i64t = codegen.context.i64_type();
|
||||
// Resolve receiver as handle and pointer (i8*) via Resolver to ensure dominance safety
|
||||
let recv_h = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*box_val,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let recv_p = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*box_val,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let recv_v: BVE = recv_p.into();
|
||||
|
||||
// Resolve type_id
|
||||
let type_id: i64 =
|
||||
if let Some(crate::mir::MirType::Box(bname)) = func.metadata.value_types.get(box_val) {
|
||||
*box_type_ids.get(bname).unwrap_or(&0)
|
||||
} else if let Some(crate::mir::MirType::String) = func.metadata.value_types.get(box_val) {
|
||||
*box_type_ids.get("StringBox").unwrap_or(&0)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
// Delegate String methods
|
||||
if super::strings::try_handle_string_method(
|
||||
codegen,
|
||||
cursor,
|
||||
resolver,
|
||||
cur_bid,
|
||||
func,
|
||||
vmap,
|
||||
dst,
|
||||
box_val,
|
||||
method,
|
||||
args,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
)? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Delegate Map methods first (to avoid Array fallback catching get/set ambiguously)
|
||||
if super::maps::try_handle_map_method(
|
||||
codegen, cursor, resolver, cur_bid, func, vmap, dst, box_val, method, args, recv_h,
|
||||
)? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Delegate Array methods
|
||||
if super::arrays::try_handle_array_method(
|
||||
codegen,
|
||||
cursor,
|
||||
resolver,
|
||||
cur_bid,
|
||||
func,
|
||||
vmap,
|
||||
dst,
|
||||
box_val,
|
||||
method,
|
||||
args,
|
||||
recv_h,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
)? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Console convenience: treat println as env.console.log
|
||||
if method == "println" {
|
||||
return super::externcall::lower_externcall(
|
||||
codegen,
|
||||
cursor,
|
||||
resolver,
|
||||
cur_bid,
|
||||
func,
|
||||
vmap,
|
||||
dst,
|
||||
&"env.console".to_string(),
|
||||
&"log".to_string(),
|
||||
args,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
);
|
||||
}
|
||||
|
||||
// getField/setField
|
||||
if fields::try_handle_field_method(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
vmap,
|
||||
dst,
|
||||
method,
|
||||
args,
|
||||
recv_h,
|
||||
resolver,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
)? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Minimal untyped fallback: Array.length with missing annotations
|
||||
if method == "length" && args.is_empty() {
|
||||
let fnty = i64t.fn_type(&[i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash_array_length_h")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash_array_length_h", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[recv_h.into()], "alen_fallback")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("array_length_h returned void".to_string())?;
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(mid) = method_id {
|
||||
invoke::try_handle_tagged_invoke(
|
||||
codegen,
|
||||
func,
|
||||
cursor,
|
||||
resolver,
|
||||
vmap,
|
||||
dst,
|
||||
*mid,
|
||||
type_id,
|
||||
recv_h,
|
||||
args,
|
||||
entry_builder,
|
||||
cur_bid,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
)?;
|
||||
return Ok(());
|
||||
} else {
|
||||
// Fallback: treat as direct call to a user function in the same module, if present.
|
||||
// Compose candidate name like "<Module>.<method>/<arity>" (e.g., Main.esc_json/1)
|
||||
let arity = args.len();
|
||||
let module_name = func
|
||||
.signature
|
||||
.name
|
||||
.split('.')
|
||||
.next()
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
if !module_name.is_empty() {
|
||||
let candidate = format!("{}.{}{}", module_name, method, format!("/{}", arity));
|
||||
// Sanitize symbol the same way as codegen/mod.rs does
|
||||
let sym: String = {
|
||||
let mut s = String::from("ny_f_");
|
||||
s.push_str(
|
||||
&candidate
|
||||
.replace('.', "_")
|
||||
.replace('/', "_")
|
||||
.replace('-', "_"),
|
||||
);
|
||||
s
|
||||
};
|
||||
if let Some(callee) = codegen.module.get_function(&sym) {
|
||||
// Coerce arguments to callee parameter types
|
||||
let exp_tys = callee.get_type().get_param_types();
|
||||
if exp_tys.len() != args.len() {
|
||||
return Err("boxcall direct-call: arg count mismatch".to_string());
|
||||
}
|
||||
let mut call_args: Vec<inkwell::values::BasicMetadataValueEnum> =
|
||||
Vec::with_capacity(args.len());
|
||||
for (i, a) in args.iter().enumerate() {
|
||||
use inkwell::types::BasicMetadataTypeEnum as BMTy;
|
||||
let coerced: BVE<'ctx> = match exp_tys[i] {
|
||||
BMTy::IntType(it) => {
|
||||
// Use Resolver via our surrounding lowering
|
||||
let iv = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*a,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let bw_dst = it.get_bit_width();
|
||||
let bw_src = iv.get_type().get_bit_width();
|
||||
if bw_src == bw_dst {
|
||||
iv.into()
|
||||
} else if bw_src < bw_dst {
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_int_z_extend(iv, it, "boxcall_arg_zext")
|
||||
})
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
} else if bw_dst == 1 {
|
||||
super::super::types::to_bool(
|
||||
codegen.context,
|
||||
iv.into(),
|
||||
&codegen.builder,
|
||||
)?
|
||||
.into()
|
||||
} else {
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_int_truncate(iv, it, "boxcall_arg_trunc")
|
||||
})
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
}
|
||||
}
|
||||
BMTy::PointerType(pt) => {
|
||||
let iv = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*a,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let p = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_int_to_ptr(iv, pt, "boxcall_arg_i2p")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
p.into()
|
||||
}
|
||||
BMTy::FloatType(ft) => {
|
||||
let fv = resolver.resolve_f64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*a,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
if fv.get_type() == ft {
|
||||
fv.into()
|
||||
} else {
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_float_cast(fv, ft, "boxcall_arg_fcast")
|
||||
})
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err(
|
||||
"boxcall direct-call: unsupported parameter type".to_string()
|
||||
)
|
||||
}
|
||||
};
|
||||
call_args.push(coerced.into());
|
||||
}
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &call_args, "user_meth_call")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
if let Some(rv) = call.try_as_basic_value().left() {
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
// Last resort: invoke plugin by name (host resolves method_id)
|
||||
{
|
||||
let i64t = codegen.context.i64_type();
|
||||
let argc = i64t.const_int(args.len() as u64, false);
|
||||
let mname = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_global_string_ptr(method, "meth_name"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
// up to 2 args for this minimal path
|
||||
let a1 = if let Some(v0) = args.get(0) {
|
||||
resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*v0,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?
|
||||
} else {
|
||||
i64t.const_zero()
|
||||
};
|
||||
let a2 = if let Some(v1) = args.get(1) {
|
||||
resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*v1,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?
|
||||
} else {
|
||||
i64t.const_zero()
|
||||
};
|
||||
let fnty = i64t.fn_type(
|
||||
&[
|
||||
i64t.into(), // recv handle
|
||||
codegen.context.ptr_type(AddressSpace::from(0)).into(), // method cstr
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(), // argc, a1, a2
|
||||
],
|
||||
false,
|
||||
);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.plugin.invoke_by_name_i64")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.plugin.invoke_by_name_i64", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(
|
||||
callee,
|
||||
&[
|
||||
recv_h.into(),
|
||||
mname.as_pointer_value().into(),
|
||||
argc.into(),
|
||||
a1.into(),
|
||||
a2.into(),
|
||||
],
|
||||
"pinvoke_by_name",
|
||||
)
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("invoke_by_name returned void".to_string())?;
|
||||
// Inline minimal return normalization similar to store_invoke_return()
|
||||
if let Some(mt) = func.metadata.value_types.get(d) {
|
||||
match mt {
|
||||
crate::mir::MirType::Integer => {
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
crate::mir::MirType::Bool => {
|
||||
if let BVE::IntValue(iv) = rv {
|
||||
let i64t = codegen.context.i64_type();
|
||||
let zero = i64t.const_zero();
|
||||
let b1 = cursor
|
||||
.emit_instr(cur_bid, |bd| {
|
||||
bd.build_int_compare(
|
||||
inkwell::IntPredicate::NE,
|
||||
iv,
|
||||
zero,
|
||||
"bool_i64_to_i1",
|
||||
)
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(*d, b1.into());
|
||||
} else {
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
}
|
||||
crate::mir::MirType::String => {
|
||||
if let BVE::IntValue(iv) = rv {
|
||||
let p = cursor
|
||||
.emit_instr(cur_bid, |bd| {
|
||||
bd.build_int_to_ptr(
|
||||
iv,
|
||||
codegen.context.ptr_type(AddressSpace::from(0)),
|
||||
"str_h2p_ret",
|
||||
)
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(*d, p.into());
|
||||
} else {
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
}
|
||||
crate::mir::MirType::Box(_)
|
||||
| crate::mir::MirType::Array(_)
|
||||
| crate::mir::MirType::Future(_)
|
||||
| crate::mir::MirType::Unknown => {
|
||||
if let BVE::IntValue(iv) = rv {
|
||||
let p = cursor
|
||||
.emit_instr(cur_bid, |bd| {
|
||||
bd.build_int_to_ptr(
|
||||
iv,
|
||||
codegen.context.ptr_type(AddressSpace::from(0)),
|
||||
"h2p_ret",
|
||||
)
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(*d, p.into());
|
||||
} else {
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
Err(format!("BoxCall requires method_id for method '{}'. The method_id should be automatically injected during MIR compilation.", method))
|
||||
}
|
||||
}
|
||||
|
||||
// Boxed API: thin shim adapting LowerFnCtx/BlockCtx to the existing implementation.
|
||||
pub(in super::super) fn lower_boxcall_boxed<'ctx, 'b>(
|
||||
ctx: &mut LowerFnCtx<'ctx, 'b>,
|
||||
blk: &BlockCtx<'ctx>,
|
||||
dst: &Option<ValueId>,
|
||||
box_val: &ValueId,
|
||||
method: &str,
|
||||
method_id: &Option<u16>,
|
||||
args: &[ValueId],
|
||||
entry_builder: &inkwell::builder::Builder<'ctx>,
|
||||
) -> Result<(), String> {
|
||||
// Optional dev check: ensure block is open for insertion
|
||||
if ctx.dev_checks {
|
||||
ctx.cursor.assert_open(blk.cur_bid);
|
||||
}
|
||||
lower_boxcall(
|
||||
ctx.codegen,
|
||||
ctx.cursor,
|
||||
ctx.resolver,
|
||||
blk.cur_bid,
|
||||
ctx.func,
|
||||
ctx.vmap,
|
||||
dst,
|
||||
box_val,
|
||||
method,
|
||||
method_id,
|
||||
args,
|
||||
ctx.box_type_ids
|
||||
.ok_or_else(|| "LowerFnCtx.box_type_ids missing".to_string())?,
|
||||
entry_builder,
|
||||
ctx.bb_map,
|
||||
ctx.preds,
|
||||
ctx.block_end_values,
|
||||
)
|
||||
}
|
||||
|
||||
// Convenience wrapper: construct LowerFnCtx/BlockCtx inside to keep caller borrow scopes short.
|
||||
pub(in super::super) fn lower_boxcall_via_ctx<'ctx, 'b>(
|
||||
codegen: &'ctx CodegenContext<'ctx>,
|
||||
cursor: &'b mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &'b mut super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
func: &'b MirFunction,
|
||||
vmap: &'b mut HashMap<ValueId, inkwell::values::BasicValueEnum<'ctx>>,
|
||||
dst: &Option<ValueId>,
|
||||
box_val: &ValueId,
|
||||
method: &str,
|
||||
method_id: &Option<u16>,
|
||||
args: &[ValueId],
|
||||
box_type_ids: &'b HashMap<String, i64>,
|
||||
entry_builder: &inkwell::builder::Builder<'ctx>,
|
||||
bb_map: &'b std::collections::HashMap<
|
||||
crate::mir::BasicBlockId,
|
||||
inkwell::basic_block::BasicBlock<'ctx>,
|
||||
>,
|
||||
preds: &'b std::collections::HashMap<crate::mir::BasicBlockId, Vec<crate::mir::BasicBlockId>>,
|
||||
block_end_values: &'b std::collections::HashMap<
|
||||
crate::mir::BasicBlockId,
|
||||
std::collections::HashMap<ValueId, inkwell::values::BasicValueEnum<'ctx>>,
|
||||
>,
|
||||
) -> Result<(), String> {
|
||||
let llbb = *bb_map.get(&cur_bid).ok_or("missing cur bb")?;
|
||||
let blkctx = BlockCtx::new(cur_bid, llbb);
|
||||
let mut fnctx = LowerFnCtx::new(
|
||||
codegen,
|
||||
func,
|
||||
cursor,
|
||||
resolver,
|
||||
vmap,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
)
|
||||
.with_box_type_ids(box_type_ids);
|
||||
lower_boxcall_boxed(
|
||||
&mut fnctx,
|
||||
&blkctx,
|
||||
dst,
|
||||
box_val,
|
||||
method,
|
||||
method_id,
|
||||
args,
|
||||
entry_builder,
|
||||
)
|
||||
}
|
||||
|
||||
fn coerce_to_type<'ctx>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
val: inkwell::values::BasicValueEnum<'ctx>,
|
||||
target: inkwell::types::BasicMetadataTypeEnum<'ctx>,
|
||||
) -> Result<inkwell::values::BasicValueEnum<'ctx>, String> {
|
||||
use inkwell::types::BasicMetadataTypeEnum as BT;
|
||||
match (val, target) {
|
||||
(inkwell::values::BasicValueEnum::IntValue(iv), BT::IntType(it)) => {
|
||||
let bw_src = iv.get_type().get_bit_width();
|
||||
let bw_dst = it.get_bit_width();
|
||||
if bw_src == bw_dst {
|
||||
Ok(iv.into())
|
||||
} else if bw_src < bw_dst {
|
||||
Ok(codegen
|
||||
.builder
|
||||
.build_int_z_extend(iv, it, "bc_zext")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into())
|
||||
} else if bw_dst == 1 {
|
||||
Ok(
|
||||
super::super::types::to_bool(codegen.context, iv.into(), &codegen.builder)?
|
||||
.into(),
|
||||
)
|
||||
} else {
|
||||
Ok(codegen
|
||||
.builder
|
||||
.build_int_truncate(iv, it, "bc_trunc")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into())
|
||||
}
|
||||
}
|
||||
(inkwell::values::BasicValueEnum::PointerValue(pv), BT::IntType(it)) => Ok(codegen
|
||||
.builder
|
||||
.build_ptr_to_int(pv, it, "bc_p2i")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()),
|
||||
(inkwell::values::BasicValueEnum::FloatValue(fv), BT::IntType(it)) => Ok(codegen
|
||||
.builder
|
||||
.build_float_to_signed_int(fv, it, "bc_f2i")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()),
|
||||
(inkwell::values::BasicValueEnum::IntValue(iv), BT::PointerType(pt)) => Ok(codegen
|
||||
.builder
|
||||
.build_int_to_ptr(iv, pt, "bc_i2p")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()),
|
||||
(inkwell::values::BasicValueEnum::PointerValue(pv), BT::PointerType(_)) => Ok(pv.into()),
|
||||
(inkwell::values::BasicValueEnum::IntValue(iv), BT::FloatType(ft)) => Ok(codegen
|
||||
.builder
|
||||
.build_signed_int_to_float(iv, ft, "bc_i2f")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()),
|
||||
(inkwell::values::BasicValueEnum::FloatValue(fv), BT::FloatType(_)) => Ok(fv.into()),
|
||||
(v, _) => Ok(v),
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,153 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::super::ctx::{BlockCtx, LowerFnCtx};
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::ValueId;
|
||||
use inkwell::{values::BasicValueEnum as BVE, AddressSpace};
|
||||
|
||||
/// 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 super::super::builder_cursor::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 {
|
||||
"getField" => {
|
||||
if args.len() != 1 {
|
||||
return Err("getField expects 1 arg (name)".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 = 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
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("get_field returned void".to_string())?;
|
||||
let h = if let BVE::IntValue(iv) = rv {
|
||||
iv
|
||||
} else {
|
||||
return Err("get_field ret expected i64".to_string());
|
||||
};
|
||||
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());
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
"setField" => {
|
||||
if args.len() != 2 {
|
||||
return Err("setField expects 2 args (name, value)".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 _ = 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)
|
||||
}
|
||||
_ => 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,
|
||||
)
|
||||
}
|
||||
@ -0,0 +1,391 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use inkwell::{values::BasicValueEnum as BVE, AddressSpace};
|
||||
|
||||
use super::super::ctx::{BlockCtx, LowerFnCtx};
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{function::MirFunction, ValueId};
|
||||
|
||||
// use super::marshal::{get_i64, get_tag_const};
|
||||
|
||||
/// Handle method_id-tagged plugin invoke path; returns Ok(()) if handled.
|
||||
pub(super) fn try_handle_tagged_invoke<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
func: &MirFunction,
|
||||
cursor: &mut crate::backend::llvm::compiler::codegen::instructions::builder_cursor::BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::super::Resolver<'ctx>,
|
||||
vmap: &mut HashMap<ValueId, inkwell::values::BasicValueEnum<'ctx>>,
|
||||
dst: &Option<ValueId>,
|
||||
mid: u16,
|
||||
type_id: i64,
|
||||
recv_h: inkwell::values::IntValue<'ctx>,
|
||||
args: &[ValueId],
|
||||
entry_builder: &inkwell::builder::Builder<'ctx>,
|
||||
cur_bid: crate::mir::BasicBlockId,
|
||||
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<(), String> {
|
||||
let i64t = codegen.context.i64_type();
|
||||
let argc_val = i64t.const_int(args.len() as u64, false);
|
||||
|
||||
// Fast path: <= 4 fixed args
|
||||
if args.len() <= 4 {
|
||||
let mut a = [i64t.const_zero(); 4];
|
||||
for (i, vid) in args.iter().enumerate() {
|
||||
let iv = match func.metadata.value_types.get(vid) {
|
||||
Some(crate::mir::MirType::Float) => {
|
||||
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_f2h")
|
||||
.map_err(|e| e.to_string())?;
|
||||
call.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_f64 returned void".to_string())?
|
||||
.into_int_value()
|
||||
}
|
||||
_ => resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*vid,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?,
|
||||
};
|
||||
a[i] = iv;
|
||||
}
|
||||
let mut tags = [i64t.const_int(3, false); 4];
|
||||
for (i, vid) in args.iter().enumerate() {
|
||||
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,
|
||||
};
|
||||
tags[i] = i64t.const_int(tag as u64, false);
|
||||
}
|
||||
let fnty = i64t.fn_type(
|
||||
&[
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
],
|
||||
false,
|
||||
);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash_plugin_invoke3_tagged_i64")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash_plugin_invoke3_tagged_i64", fnty, None)
|
||||
});
|
||||
let tid = i64t.const_int(type_id as u64, true);
|
||||
let midv = i64t.const_int(mid as u64, false);
|
||||
let call = codegen
|
||||
.builder
|
||||
.build_call(
|
||||
callee,
|
||||
&[
|
||||
tid.into(),
|
||||
midv.into(),
|
||||
argc_val.into(),
|
||||
recv_h.into(),
|
||||
a[0].into(),
|
||||
tags[0].into(),
|
||||
a[1].into(),
|
||||
tags[1].into(),
|
||||
a[2].into(),
|
||||
tags[2].into(),
|
||||
a[3].into(),
|
||||
tags[3].into(),
|
||||
],
|
||||
"pinvoke_tagged",
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("invoke3_i64 returned void".to_string())?;
|
||||
store_invoke_return(codegen, func, vmap, *d, rv)?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Variable length path: build i64 arrays for vals and tags
|
||||
let n = args.len() as u32;
|
||||
let arr_ty = i64t.array_type(n);
|
||||
let vals_arr = entry_builder
|
||||
.build_alloca(arr_ty, "vals_arr")
|
||||
.map_err(|e| e.to_string())?;
|
||||
let tags_arr = entry_builder
|
||||
.build_alloca(arr_ty, "tags_arr")
|
||||
.map_err(|e| e.to_string())?;
|
||||
for (i, vid) in args.iter().enumerate() {
|
||||
let idx = [
|
||||
codegen.context.i32_type().const_zero(),
|
||||
codegen.context.i32_type().const_int(i as u64, false),
|
||||
];
|
||||
let gep_v = unsafe {
|
||||
codegen
|
||||
.builder
|
||||
.build_in_bounds_gep(arr_ty, vals_arr, &idx, &format!("v_gep_{}", i))
|
||||
.map_err(|e| e.to_string())?
|
||||
};
|
||||
let gep_t = unsafe {
|
||||
codegen
|
||||
.builder
|
||||
.build_in_bounds_gep(arr_ty, tags_arr, &idx, &format!("t_gep_{}", i))
|
||||
.map_err(|e| e.to_string())?
|
||||
};
|
||||
let vi = match func.metadata.value_types.get(vid) {
|
||||
Some(crate::mir::MirType::Float) => {
|
||||
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_f2h")
|
||||
.map_err(|e| e.to_string())?;
|
||||
call.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_f64 returned void".to_string())?
|
||||
.into_int_value()
|
||||
}
|
||||
_ => resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*vid,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?,
|
||||
};
|
||||
let ti = match func.metadata.value_types.get(vid) {
|
||||
Some(crate::mir::MirType::Float) => i64t.const_int(5, false),
|
||||
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) => i64t.const_int(8, false),
|
||||
_ => i64t.const_int(3, false),
|
||||
};
|
||||
codegen
|
||||
.builder
|
||||
.build_store(gep_v, vi)
|
||||
.map_err(|e| e.to_string())?;
|
||||
codegen
|
||||
.builder
|
||||
.build_store(gep_t, ti)
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
let vals_ptr = codegen
|
||||
.builder
|
||||
.build_pointer_cast(
|
||||
vals_arr,
|
||||
codegen.context.ptr_type(AddressSpace::from(0)),
|
||||
"vals_arr_i8p",
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let tags_ptr = codegen
|
||||
.builder
|
||||
.build_pointer_cast(
|
||||
tags_arr,
|
||||
codegen.context.ptr_type(AddressSpace::from(0)),
|
||||
"tags_arr_i8p",
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let fnty = i64t.fn_type(
|
||||
&[
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
codegen.context.ptr_type(AddressSpace::from(0)).into(),
|
||||
codegen.context.ptr_type(AddressSpace::from(0)).into(),
|
||||
],
|
||||
false,
|
||||
);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.plugin.invoke_tagged_v_i64")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.plugin.invoke_tagged_v_i64", fnty, None)
|
||||
});
|
||||
let tid = i64t.const_int(type_id as u64, true);
|
||||
let midv = i64t.const_int(mid as u64, false);
|
||||
let call = codegen
|
||||
.builder
|
||||
.build_call(
|
||||
callee,
|
||||
&[
|
||||
tid.into(),
|
||||
midv.into(),
|
||||
argc_val.into(),
|
||||
recv_h.into(),
|
||||
vals_ptr.into(),
|
||||
tags_ptr.into(),
|
||||
],
|
||||
"pinvoke_tagged_v",
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("invoke_v returned void".to_string())?;
|
||||
store_invoke_return(codegen, func, vmap, *d, rv)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn store_invoke_return<'ctx>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
func: &MirFunction,
|
||||
vmap: &mut HashMap<ValueId, inkwell::values::BasicValueEnum<'ctx>>,
|
||||
dst: ValueId,
|
||||
rv: inkwell::values::BasicValueEnum<'ctx>,
|
||||
) -> Result<(), String> {
|
||||
if let Some(mt) = func.metadata.value_types.get(&dst) {
|
||||
match mt {
|
||||
crate::mir::MirType::Integer => {
|
||||
vmap.insert(dst, rv);
|
||||
}
|
||||
crate::mir::MirType::Bool => {
|
||||
// Normalize i64 bool (0/1) to i1
|
||||
if let BVE::IntValue(iv) = rv {
|
||||
let i64t = codegen.context.i64_type();
|
||||
let zero = i64t.const_zero();
|
||||
let b1 = codegen
|
||||
.builder
|
||||
.build_int_compare(inkwell::IntPredicate::NE, iv, zero, "bool_i64_to_i1")
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(dst, b1.into());
|
||||
} else {
|
||||
vmap.insert(dst, rv);
|
||||
}
|
||||
}
|
||||
crate::mir::MirType::String => {
|
||||
// Keep as i64 handle across blocks (pointer is produced on demand via Resolver)
|
||||
if let BVE::IntValue(iv) = rv {
|
||||
vmap.insert(dst, iv.into());
|
||||
} else {
|
||||
return Err("invoke ret expected i64 for String".to_string());
|
||||
}
|
||||
}
|
||||
crate::mir::MirType::Box(_)
|
||||
| crate::mir::MirType::Array(_)
|
||||
| crate::mir::MirType::Future(_)
|
||||
| crate::mir::MirType::Unknown => {
|
||||
let h = if let BVE::IntValue(iv) = rv {
|
||||
iv
|
||||
} else {
|
||||
return Err("invoke ret expected i64".to_string());
|
||||
};
|
||||
let pty = codegen.context.ptr_type(AddressSpace::from(0));
|
||||
let ptr = codegen
|
||||
.builder
|
||||
.build_int_to_ptr(h, pty, "ret_handle_to_ptr")
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(dst, ptr.into());
|
||||
}
|
||||
_ => {
|
||||
vmap.insert(dst, rv);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
vmap.insert(dst, rv);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Boxed wrapper delegating to the existing implementation
|
||||
pub(super) fn try_handle_tagged_invoke_boxed<'ctx, 'b>(
|
||||
ctx: &mut LowerFnCtx<'ctx, 'b>,
|
||||
blk: &BlockCtx<'ctx>,
|
||||
dst: &Option<ValueId>,
|
||||
mid: u16,
|
||||
type_id: i64,
|
||||
recv_h: inkwell::values::IntValue<'ctx>,
|
||||
args: &[ValueId],
|
||||
entry_builder: &inkwell::builder::Builder<'ctx>,
|
||||
) -> Result<(), String> {
|
||||
try_handle_tagged_invoke(
|
||||
ctx.codegen,
|
||||
ctx.func,
|
||||
ctx.cursor,
|
||||
ctx.resolver,
|
||||
ctx.vmap,
|
||||
dst,
|
||||
mid,
|
||||
type_id,
|
||||
recv_h,
|
||||
args,
|
||||
entry_builder,
|
||||
blk.cur_bid,
|
||||
ctx.bb_map,
|
||||
ctx.preds,
|
||||
ctx.block_end_values,
|
||||
)
|
||||
}
|
||||
@ -0,0 +1,96 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::backend::llvm::compiler::codegen::types;
|
||||
use inkwell::{values::BasicValueEnum as BVE, AddressSpace};
|
||||
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{function::MirFunction, ValueId};
|
||||
|
||||
/// Convert a value to i64 handle/int for plugin invoke (ptr->i64, f64->box->i64)
|
||||
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();
|
||||
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 = 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())
|
||||
}
|
||||
}
|
||||
_ => 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>,
|
||||
func: &MirFunction,
|
||||
vid: ValueId,
|
||||
) -> inkwell::values::IntValue<'ctx> {
|
||||
let i64t = codegen.context.i64_type();
|
||||
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)
|
||||
}
|
||||
@ -0,0 +1,108 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use inkwell::{basic_block::BasicBlock, builder::Builder};
|
||||
|
||||
use crate::mir::BasicBlockId;
|
||||
|
||||
/// Track per-block open/closed state and centralize terminator emission.
|
||||
pub struct BuilderCursor<'ctx, 'b> {
|
||||
pub builder: &'b Builder<'ctx>,
|
||||
closed_by_bid: HashMap<BasicBlockId, bool>,
|
||||
cur_bid: Option<BasicBlockId>,
|
||||
cur_llbb: Option<BasicBlock<'ctx>>,
|
||||
}
|
||||
|
||||
impl<'ctx, 'b> BuilderCursor<'ctx, 'b> {
|
||||
pub fn new(builder: &'b Builder<'ctx>) -> Self {
|
||||
Self {
|
||||
builder,
|
||||
closed_by_bid: HashMap::new(),
|
||||
cur_bid: None,
|
||||
cur_llbb: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Temporarily switch to another block, run body, then restore previous position/state.
|
||||
pub fn with_block<R>(
|
||||
&mut self,
|
||||
bid: BasicBlockId,
|
||||
bb: BasicBlock<'ctx>,
|
||||
body: impl FnOnce(&mut BuilderCursor<'ctx, 'b>) -> R,
|
||||
) -> R {
|
||||
let prev_bid = self.cur_bid;
|
||||
let prev_bb = self.cur_llbb;
|
||||
// Preserve previous closed state
|
||||
let prev_closed = prev_bid.and_then(|id| self.closed_by_bid.get(&id).copied());
|
||||
// Preserve target block closed state and restore after
|
||||
let tgt_closed_before = self.closed_by_bid.get(&bid).copied();
|
||||
|
||||
self.at_end(bid, bb);
|
||||
let r = body(self);
|
||||
|
||||
// Restore prior insertion point/state
|
||||
if let Some(pbb) = prev_bb {
|
||||
self.builder.position_at_end(pbb);
|
||||
}
|
||||
self.cur_bid = prev_bid;
|
||||
self.cur_llbb = prev_bb;
|
||||
if let (Some(pid), Some(closed)) = (prev_bid, prev_closed) {
|
||||
self.closed_by_bid.insert(pid, closed);
|
||||
}
|
||||
if let Some(closed) = tgt_closed_before {
|
||||
self.closed_by_bid.insert(bid, closed);
|
||||
} else {
|
||||
// If previously unknown, keep it marked as closed if a terminator exists
|
||||
let has_term = unsafe { bb.get_terminator() }.is_some();
|
||||
self.closed_by_bid.insert(bid, has_term);
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
pub fn at_end(&mut self, bid: BasicBlockId, bb: BasicBlock<'ctx>) {
|
||||
self.cur_bid = Some(bid);
|
||||
self.cur_llbb = Some(bb);
|
||||
// Mark closed if LLVM already has a terminator in this block
|
||||
let has_term = unsafe { bb.get_terminator() }.is_some();
|
||||
self.closed_by_bid.insert(bid, has_term);
|
||||
self.builder.position_at_end(bb);
|
||||
}
|
||||
|
||||
pub fn position_at_end(&self, bb: BasicBlock<'ctx>) {
|
||||
self.builder.position_at_end(bb);
|
||||
}
|
||||
|
||||
pub fn assert_open(&self, bid: BasicBlockId) {
|
||||
if let Some(closed) = self.closed_by_bid.get(&bid) {
|
||||
assert!(
|
||||
!closed,
|
||||
"attempt to insert into closed block {}",
|
||||
bid.as_u32()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn emit_instr<T>(&mut self, bid: BasicBlockId, f: impl FnOnce(&Builder<'ctx>) -> T) -> T {
|
||||
self.assert_open(bid);
|
||||
// Extra hard guard: check actual LLVM block state before inserting
|
||||
if let Some(bb) = self.cur_llbb {
|
||||
if unsafe { bb.get_terminator() }.is_some() {
|
||||
panic!("post-terminator insert detected in bb {}", bid.as_u32());
|
||||
}
|
||||
}
|
||||
f(self.builder)
|
||||
}
|
||||
|
||||
pub fn emit_term(&mut self, bid: BasicBlockId, f: impl FnOnce(&Builder<'ctx>)) {
|
||||
self.assert_open(bid);
|
||||
f(self.builder);
|
||||
// After emitting a terminator, assert the current basic block now has one
|
||||
if let Some(bb) = self.cur_llbb {
|
||||
assert!(
|
||||
unsafe { bb.get_terminator() }.is_some(),
|
||||
"expected terminator in bb {}",
|
||||
bid.as_u32()
|
||||
);
|
||||
}
|
||||
self.closed_by_bid.insert(bid, true);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,146 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use inkwell::{
|
||||
types::BasicMetadataTypeEnum as BMT,
|
||||
values::{BasicMetadataValueEnum, BasicValueEnum as BVE, FunctionValue},
|
||||
};
|
||||
|
||||
use crate::backend::llvm::compiler::codegen::instructions::builder_cursor::BuilderCursor;
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{function::MirFunction, BasicBlockId, ValueId};
|
||||
|
||||
/// Lower a direct Call where callee is provided as a const string ValueId in MIR14.
|
||||
///
|
||||
/// Requirements:
|
||||
/// - `const_strs`: mapping from ValueId to the string literal value within the same function.
|
||||
/// - `llvm_funcs`: predeclared LLVM functions keyed by MIR function name (same string as const).
|
||||
pub(in super::super) fn lower_call<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
_func: &MirFunction,
|
||||
vmap: &mut HashMap<ValueId, BVE<'ctx>>,
|
||||
dst: &Option<ValueId>,
|
||||
callee: &ValueId,
|
||||
args: &[ValueId],
|
||||
const_strs: &HashMap<ValueId, String>,
|
||||
llvm_funcs: &HashMap<String, FunctionValue<'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, BVE<'ctx>>,
|
||||
>,
|
||||
) -> Result<(), String> {
|
||||
let name_s = const_strs
|
||||
.get(callee)
|
||||
.ok_or_else(|| format!("call: callee value {} not a const string", callee.as_u32()))?;
|
||||
let target = llvm_funcs
|
||||
.get(name_s)
|
||||
.ok_or_else(|| format!("call: function not predeclared: {}", name_s))?;
|
||||
|
||||
// Collect and coerce args to the callee's expected parameter types
|
||||
let fn_ty = target.get_type();
|
||||
let exp_tys: Vec<BMT<'ctx>> = fn_ty.get_param_types();
|
||||
if exp_tys.len() != args.len() {
|
||||
return Err(format!(
|
||||
"call: arg count mismatch for {} (expected {}, got {})",
|
||||
name_s,
|
||||
exp_tys.len(),
|
||||
args.len()
|
||||
));
|
||||
}
|
||||
let mut params: Vec<BasicMetadataValueEnum> = Vec::with_capacity(args.len());
|
||||
for (i, a) in args.iter().enumerate() {
|
||||
use inkwell::types::BasicMetadataTypeEnum as BMTy;
|
||||
let coerced: BVE<'ctx> = match exp_tys[i] {
|
||||
BMTy::IntType(it) => {
|
||||
// Localize as i64, then adjust width to callee expectation
|
||||
let iv = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*a,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let bw_dst = it.get_bit_width();
|
||||
let bw_src = iv.get_type().get_bit_width();
|
||||
if bw_src == bw_dst {
|
||||
iv.into()
|
||||
} else if bw_src < bw_dst {
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_z_extend(iv, it, "call_arg_zext"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
} else if bw_dst == 1 {
|
||||
super::super::types::to_bool(codegen.context, iv.into(), &codegen.builder)?
|
||||
.into()
|
||||
} else {
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_truncate(iv, it, "call_arg_trunc"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
}
|
||||
}
|
||||
BMTy::PointerType(pt) => {
|
||||
// Localize as i64 handle and convert to expected pointer type
|
||||
let iv = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*a,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let p = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_to_ptr(iv, pt, "call_arg_i2p"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
p.into()
|
||||
}
|
||||
BMTy::FloatType(ft) => {
|
||||
// Localize as f64, then adjust to callee expectation width if needed
|
||||
let fv = resolver.resolve_f64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*a,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
if fv.get_type() == ft {
|
||||
fv.into()
|
||||
} else {
|
||||
// Cast f64<->f32 as needed
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_float_cast(fv, ft, "call_arg_fcast"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err("call: unsupported parameter type (expected int/ptr/float)".to_string());
|
||||
}
|
||||
};
|
||||
params.push(coerced.into());
|
||||
}
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_call(*target, ¶ms, "call"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
if let Some(rv) = call.try_as_basic_value().left() {
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use inkwell::values::BasicValueEnum;
|
||||
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{instruction::ConstValue, ValueId};
|
||||
|
||||
pub(in super::super) fn lower_const<'ctx>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
vmap: &mut HashMap<ValueId, BasicValueEnum<'ctx>>,
|
||||
dst: ValueId,
|
||||
value: &ConstValue,
|
||||
) -> Result<(), String> {
|
||||
let bval = match value {
|
||||
ConstValue::Integer(i) => codegen.context.i64_type().const_int(*i as u64, true).into(),
|
||||
ConstValue::Float(f) => codegen.context.f64_type().const_float(*f).into(),
|
||||
ConstValue::Bool(b) => codegen
|
||||
.context
|
||||
.bool_type()
|
||||
.const_int(*b as u64, false)
|
||||
.into(),
|
||||
ConstValue::String(s) => {
|
||||
let gv = codegen
|
||||
.builder
|
||||
.build_global_string_ptr(s, "str")
|
||||
.map_err(|e| e.to_string())?;
|
||||
let len = codegen.context.i32_type().const_int(s.len() as u64, false);
|
||||
// declare i8* @nyash_string_new(i8*, i32)
|
||||
let rt = codegen.context.ptr_type(inkwell::AddressSpace::from(0));
|
||||
let fn_ty = rt.fn_type(
|
||||
&[
|
||||
codegen
|
||||
.context
|
||||
.ptr_type(inkwell::AddressSpace::from(0))
|
||||
.into(),
|
||||
codegen.context.i32_type().into(),
|
||||
],
|
||||
false,
|
||||
);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash_string_new")
|
||||
.unwrap_or_else(|| codegen.module.add_function("nyash_string_new", fn_ty, None));
|
||||
let call = codegen
|
||||
.builder
|
||||
.build_call(
|
||||
callee,
|
||||
&[gv.as_pointer_value().into(), len.into()],
|
||||
"strnew",
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
call.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("nyash_string_new returned void".to_string())?
|
||||
}
|
||||
ConstValue::Null => codegen
|
||||
.context
|
||||
.ptr_type(inkwell::AddressSpace::from(0))
|
||||
.const_null()
|
||||
.into(),
|
||||
ConstValue::Void => return Err("Const Void unsupported".to_string()),
|
||||
};
|
||||
vmap.insert(dst, bval);
|
||||
Ok(())
|
||||
}
|
||||
@ -0,0 +1,126 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use inkwell::{
|
||||
basic_block::BasicBlock,
|
||||
values::{BasicValueEnum as BVE, FloatValue, IntValue, PointerValue},
|
||||
};
|
||||
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{function::MirFunction, BasicBlockId, ValueId};
|
||||
|
||||
use super::{builder_cursor::BuilderCursor, Resolver};
|
||||
|
||||
pub type LlResult<T> = Result<T, String>;
|
||||
|
||||
/// Per-function lowering context that centralizes access to codegen utilities and
|
||||
/// enforces Resolver-only value access.
|
||||
pub struct LowerFnCtx<'ctx, 'b> {
|
||||
pub codegen: &'ctx CodegenContext<'ctx>,
|
||||
pub func: &'b MirFunction,
|
||||
pub cursor: &'b mut BuilderCursor<'ctx, 'b>,
|
||||
pub resolver: &'b mut Resolver<'ctx>,
|
||||
pub vmap: &'b mut HashMap<ValueId, BVE<'ctx>>,
|
||||
pub bb_map: &'b HashMap<BasicBlockId, BasicBlock<'ctx>>,
|
||||
pub preds: &'b HashMap<BasicBlockId, Vec<BasicBlockId>>,
|
||||
pub block_end_values: &'b HashMap<BasicBlockId, HashMap<ValueId, BVE<'ctx>>>,
|
||||
// Optional extras commonly needed by some paths
|
||||
pub box_type_ids: Option<&'b HashMap<String, i64>>,
|
||||
pub const_strs: Option<&'b HashMap<ValueId, String>>,
|
||||
// Dev flag: extra runtime assertions
|
||||
pub dev_checks: bool,
|
||||
}
|
||||
|
||||
impl<'ctx, 'b> LowerFnCtx<'ctx, 'b> {
|
||||
pub fn new(
|
||||
codegen: &'ctx CodegenContext<'ctx>,
|
||||
func: &'b MirFunction,
|
||||
cursor: &'b mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &'b mut Resolver<'ctx>,
|
||||
vmap: &'b mut HashMap<ValueId, BVE<'ctx>>,
|
||||
bb_map: &'b HashMap<BasicBlockId, BasicBlock<'ctx>>,
|
||||
preds: &'b HashMap<BasicBlockId, Vec<BasicBlockId>>,
|
||||
block_end_values: &'b HashMap<BasicBlockId, HashMap<ValueId, BVE<'ctx>>>,
|
||||
) -> Self {
|
||||
let dev_checks = std::env::var("NYASH_DEV_CHECKS").ok().as_deref() == Some("1");
|
||||
Self {
|
||||
codegen,
|
||||
func,
|
||||
cursor,
|
||||
resolver,
|
||||
vmap,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
box_type_ids: None,
|
||||
const_strs: None,
|
||||
dev_checks,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_box_type_ids(mut self, ids: &'b HashMap<String, i64>) -> Self {
|
||||
self.box_type_ids = Some(ids);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_const_strs(mut self, m: &'b HashMap<ValueId, String>) -> Self {
|
||||
self.const_strs = Some(m);
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn ensure_i64(&mut self, blk: &BlockCtx<'ctx>, v: ValueId) -> LlResult<IntValue<'ctx>> {
|
||||
self.cursor.assert_open(blk.cur_bid);
|
||||
self.resolver.resolve_i64(
|
||||
self.codegen,
|
||||
self.cursor,
|
||||
blk.cur_bid,
|
||||
v,
|
||||
self.bb_map,
|
||||
self.preds,
|
||||
self.block_end_values,
|
||||
self.vmap,
|
||||
)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn ensure_ptr(&mut self, blk: &BlockCtx<'ctx>, v: ValueId) -> LlResult<PointerValue<'ctx>> {
|
||||
self.cursor.assert_open(blk.cur_bid);
|
||||
self.resolver.resolve_ptr(
|
||||
self.codegen,
|
||||
self.cursor,
|
||||
blk.cur_bid,
|
||||
v,
|
||||
self.bb_map,
|
||||
self.preds,
|
||||
self.block_end_values,
|
||||
self.vmap,
|
||||
)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn ensure_f64(&mut self, blk: &BlockCtx<'ctx>, v: ValueId) -> LlResult<FloatValue<'ctx>> {
|
||||
self.cursor.assert_open(blk.cur_bid);
|
||||
self.resolver.resolve_f64(
|
||||
self.codegen,
|
||||
self.cursor,
|
||||
blk.cur_bid,
|
||||
v,
|
||||
self.bb_map,
|
||||
self.preds,
|
||||
self.block_end_values,
|
||||
self.vmap,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Per-basic-block context to keep insertion site and block identity together.
|
||||
pub struct BlockCtx<'ctx> {
|
||||
pub cur_bid: BasicBlockId,
|
||||
pub cur_llbb: BasicBlock<'ctx>,
|
||||
}
|
||||
|
||||
impl<'ctx> BlockCtx<'ctx> {
|
||||
pub fn new(cur_bid: BasicBlockId, cur_llbb: BasicBlock<'ctx>) -> Self {
|
||||
Self { cur_bid, cur_llbb }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,102 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use inkwell::values::BasicValueEnum as BVE;
|
||||
use inkwell::AddressSpace;
|
||||
|
||||
use crate::backend::llvm::compiler::codegen::instructions::builder_cursor::BuilderCursor;
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{BasicBlockId, ValueId};
|
||||
|
||||
pub(super) fn lower_log_or_trace<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
vmap: &mut HashMap<ValueId, BVE<'ctx>>,
|
||||
dst: &Option<ValueId>,
|
||||
iface_name: &str,
|
||||
method_name: &str,
|
||||
args: &[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, BVE<'ctx>>,
|
||||
>,
|
||||
) -> Result<(), String> {
|
||||
if args.len() != 1 {
|
||||
return Err(format!("{}.{} expects 1 arg", iface_name, method_name));
|
||||
}
|
||||
// Localize to i64 (handle path) to avoid vmap shape inspection
|
||||
let arg_val = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let i64t = codegen.context.i64_type();
|
||||
let fnty = i64t.fn_type(&[i64t.into()], false);
|
||||
let fname = if iface_name == "env.console" {
|
||||
match method_name {
|
||||
"log" => "nyash.console.log_handle",
|
||||
"warn" => "nyash.console.warn_handle",
|
||||
_ => "nyash.console.error_handle",
|
||||
}
|
||||
} else {
|
||||
"nyash.debug.trace_handle"
|
||||
};
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function(fname)
|
||||
.unwrap_or_else(|| codegen.module.add_function(fname, fnty, None));
|
||||
let _ = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[arg_val.into()], "console_log_h")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
vmap.insert(*d, codegen.context.i64_type().const_zero().into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn lower_readline<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
cur_bid: BasicBlockId,
|
||||
vmap: &mut HashMap<ValueId, BVE<'ctx>>,
|
||||
dst: &Option<ValueId>,
|
||||
args: &[ValueId],
|
||||
) -> Result<(), String> {
|
||||
if !args.is_empty() {
|
||||
return Err("console.readLine expects 0 args".to_string());
|
||||
}
|
||||
let i8p = codegen.context.ptr_type(AddressSpace::from(0));
|
||||
let fnty = i8p.fn_type(&[], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.console.readline")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.console.readline", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_call(callee, &[], "readline"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("readline returned void".to_string())?;
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -0,0 +1,597 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use inkwell::values::BasicValueEnum as BVE;
|
||||
use inkwell::AddressSpace;
|
||||
|
||||
use crate::backend::llvm::compiler::codegen::instructions::builder_cursor::BuilderCursor;
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{function::MirFunction, BasicBlockId, ValueId};
|
||||
|
||||
pub(super) fn lower_future_spawn_instance<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
vmap: &mut HashMap<ValueId, BVE<'ctx>>,
|
||||
dst: &Option<ValueId>,
|
||||
args: &[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, BVE<'ctx>>,
|
||||
>,
|
||||
) -> Result<(), String> {
|
||||
if args.len() < 2 {
|
||||
return Err("env.future.spawn_instance expects at least (recv, method_name)".to_string());
|
||||
}
|
||||
let i64t = codegen.context.i64_type();
|
||||
let i8p = codegen.context.ptr_type(AddressSpace::from(0));
|
||||
let recv_h = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let name_p = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[1],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i64t.fn_type(&[i64t.into(), i8p.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.future.spawn_instance")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.future.spawn_instance", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[recv_h.into(), name_p.into()], "spawn_instance")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("spawn_instance returned void".to_string())?;
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn lower_local_get<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
_resolver: &mut super::super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
func: &MirFunction,
|
||||
vmap: &mut HashMap<ValueId, BVE<'ctx>>,
|
||||
dst: &Option<ValueId>,
|
||||
args: &[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, BVE<'ctx>>,
|
||||
>,
|
||||
) -> Result<(), String> {
|
||||
if args.len() != 1 {
|
||||
return Err("env.local.get expects 1 arg".to_string());
|
||||
}
|
||||
let name_p = _resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
_bb_map,
|
||||
_preds,
|
||||
_block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let i64t = codegen.context.i64_type();
|
||||
let i8p = codegen.context.ptr_type(AddressSpace::from(0));
|
||||
let fnty = i64t.fn_type(&[i8p.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.env.local.get_h")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.env.local.get_h", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[name_p.into()], "local_get_h")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("local.get returned void".to_string())?;
|
||||
// Cast handle to pointer for Box-like return types
|
||||
if let Some(d) = dst {
|
||||
if let Some(mt) = func.metadata.value_types.get(d) {
|
||||
match mt {
|
||||
crate::mir::MirType::Integer | crate::mir::MirType::Bool => {
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
crate::mir::MirType::String => {
|
||||
// keep as handle (i64)
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
crate::mir::MirType::Box(_)
|
||||
| crate::mir::MirType::Array(_)
|
||||
| crate::mir::MirType::Future(_)
|
||||
| crate::mir::MirType::Unknown => {
|
||||
let h = rv.into_int_value();
|
||||
let pty = codegen.context.ptr_type(AddressSpace::from(0));
|
||||
let ptr = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_int_to_ptr(h, pty, "local_get_handle_to_ptr")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(*d, ptr.into());
|
||||
}
|
||||
_ => {
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn lower_box_new<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
func: &MirFunction,
|
||||
vmap: &mut HashMap<ValueId, BVE<'ctx>>,
|
||||
dst: &Option<ValueId>,
|
||||
args: &[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, BVE<'ctx>>,
|
||||
>,
|
||||
) -> Result<(), String> {
|
||||
// Two variants: (name) and (argc, arg1, arg2, arg3, arg4) with optional ptr conversion
|
||||
// Prefer the i64 birth when possible; else call env.box.new(name)
|
||||
let i64t = codegen.context.i64_type();
|
||||
let i8p = codegen.context.ptr_type(AddressSpace::from(0));
|
||||
if args.len() == 1 {
|
||||
let name_p = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i64t.fn_type(&[i8p.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.env.box.new")
|
||||
.unwrap_or_else(|| codegen.module.add_function("nyash.env.box.new", fnty, None));
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[name_p.into()], "env_box_new")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let h = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("env.box.new returned void".to_string())?
|
||||
.into_int_value();
|
||||
let out_ptr = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_to_ptr(h, i8p, "box_handle_to_ptr"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
vmap.insert(*d, out_ptr.into());
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
if !args.is_empty() {
|
||||
// argc + up to 4 i64 payloads: build i64 via conversions
|
||||
let argc_val = i64t.const_int(args.len() as u64, false);
|
||||
let fnty = i64t.fn_type(
|
||||
&[
|
||||
i8p.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
i64t.into(),
|
||||
],
|
||||
false,
|
||||
);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.env.box.new_i64")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.env.box.new_i64", fnty, None)
|
||||
});
|
||||
// arg0: type name string pointer
|
||||
if args.is_empty() {
|
||||
return Err("env.box.new_i64 requires at least type name".to_string());
|
||||
}
|
||||
let ty_ptr = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let mut a1 = i64t.const_zero();
|
||||
if args.len() >= 2 {
|
||||
a1 = match func.metadata.value_types.get(&args[1]) {
|
||||
Some(crate::mir::MirType::Float) => {
|
||||
let fv = resolver.resolve_f64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[1],
|
||||
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 = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[fv.into()], "arg1_f64_to_box")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
call.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_f64 returned void".to_string())?
|
||||
.into_int_value()
|
||||
}
|
||||
Some(crate::mir::MirType::String) => {
|
||||
let pv = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[1],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i64t.fn_type(&[i8p.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.box.from_i8_string")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.box.from_i8_string", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[pv.into()], "arg1_i8_to_box")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
call.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_i8_string returned void".to_string())?
|
||||
.into_int_value()
|
||||
}
|
||||
_ => resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[1],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?,
|
||||
};
|
||||
}
|
||||
let mut a2 = i64t.const_zero();
|
||||
if args.len() >= 3 {
|
||||
a2 = match func.metadata.value_types.get(&args[2]) {
|
||||
Some(crate::mir::MirType::Float) => {
|
||||
let fv = resolver.resolve_f64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[2],
|
||||
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 = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[fv.into()], "arg2_f64_to_box")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
call.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_f64 returned void".to_string())?
|
||||
.into_int_value()
|
||||
}
|
||||
Some(crate::mir::MirType::String) => {
|
||||
let pv = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[2],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i64t.fn_type(&[i8p.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.box.from_i8_string")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.box.from_i8_string", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[pv.into()], "arg2_i8_to_box")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
call.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_i8_string returned void".to_string())?
|
||||
.into_int_value()
|
||||
}
|
||||
_ => resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[2],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?,
|
||||
};
|
||||
}
|
||||
let mut a3 = i64t.const_zero();
|
||||
if args.len() >= 4 {
|
||||
a3 = match func.metadata.value_types.get(&args[3]) {
|
||||
Some(crate::mir::MirType::Integer) | Some(crate::mir::MirType::Bool) => resolver
|
||||
.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[3],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?,
|
||||
Some(crate::mir::MirType::Float) => {
|
||||
let fv = resolver.resolve_f64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[3],
|
||||
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 = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[fv.into()], "arg3_f64_to_box")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
call.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_f64 returned void".to_string())?
|
||||
.into_int_value()
|
||||
}
|
||||
Some(crate::mir::MirType::String) => {
|
||||
let pv = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[3],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i64t.fn_type(&[i8p.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.box.from_i8_string")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.box.from_i8_string", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[pv.into()], "arg3_i8_to_box")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
call.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_i8_string returned void".to_string())?
|
||||
.into_int_value()
|
||||
}
|
||||
_ => return Err("unsupported arg value for env.box.new".to_string()),
|
||||
};
|
||||
}
|
||||
let mut a4 = i64t.const_zero();
|
||||
if args.len() >= 5 {
|
||||
a4 = match func.metadata.value_types.get(&args[4]) {
|
||||
Some(crate::mir::MirType::Integer) | Some(crate::mir::MirType::Bool) => resolver
|
||||
.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[4],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?,
|
||||
Some(crate::mir::MirType::Float) => {
|
||||
let fv = resolver.resolve_f64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[4],
|
||||
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 = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[fv.into()], "arg4_f64_to_box")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
call.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_f64 returned void".to_string())?
|
||||
.into_int_value()
|
||||
}
|
||||
Some(crate::mir::MirType::String) => {
|
||||
let pv = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[4],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i64t.fn_type(&[i8p.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.box.from_i8_string")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.box.from_i8_string", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[pv.into()], "arg4_i8_to_box")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
call.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_i8_string returned void".to_string())?
|
||||
.into_int_value()
|
||||
}
|
||||
_ => return Err("unsupported arg value for env.box.new".to_string()),
|
||||
};
|
||||
}
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(
|
||||
callee,
|
||||
&[
|
||||
ty_ptr.into(),
|
||||
argc_val.into(),
|
||||
a1.into(),
|
||||
a2.into(),
|
||||
a3.into(),
|
||||
a4.into(),
|
||||
],
|
||||
"env_box_new_i64x",
|
||||
)
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("env.box.new_i64 returned void".to_string())?;
|
||||
let i64v = if let BVE::IntValue(iv) = rv {
|
||||
iv
|
||||
} else {
|
||||
return Err("env.box.new_i64 ret expected i64".to_string());
|
||||
};
|
||||
let out_ptr = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_int_to_ptr(i64v, i8p, "box_handle_to_ptr")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
vmap.insert(*d, out_ptr.into());
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
Err("env.box.new requires at least 1 arg".to_string())
|
||||
}
|
||||
@ -0,0 +1,106 @@
|
||||
mod console;
|
||||
mod env;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::backend::llvm::compiler::codegen::instructions::builder_cursor::BuilderCursor;
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{function::MirFunction, BasicBlockId, ValueId};
|
||||
use inkwell::values::BasicValueEnum as BVE;
|
||||
|
||||
/// Full ExternCall lowering dispatcher (console/debug/env.*)
|
||||
pub(in super::super) fn lower_externcall<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
func: &MirFunction,
|
||||
vmap: &mut HashMap<ValueId, BVE<'ctx>>,
|
||||
dst: &Option<ValueId>,
|
||||
iface_name: &str,
|
||||
method_name: &str,
|
||||
args: &[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, BVE<'ctx>>,
|
||||
>,
|
||||
) -> Result<(), String> {
|
||||
// console/debug
|
||||
if (iface_name == "env.console" && matches!(method_name, "log" | "warn" | "error"))
|
||||
|| (iface_name == "env.debug" && method_name == "trace")
|
||||
{
|
||||
return console::lower_log_or_trace(
|
||||
codegen,
|
||||
cursor,
|
||||
resolver,
|
||||
cur_bid,
|
||||
vmap,
|
||||
dst,
|
||||
iface_name,
|
||||
method_name,
|
||||
args,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
);
|
||||
}
|
||||
if iface_name == "env.console" && method_name == "readLine" {
|
||||
return console::lower_readline(codegen, cursor, cur_bid, vmap, dst, args);
|
||||
}
|
||||
|
||||
// env.*
|
||||
if iface_name == "env.future" && method_name == "spawn_instance" {
|
||||
return env::lower_future_spawn_instance(
|
||||
codegen,
|
||||
cursor,
|
||||
resolver,
|
||||
cur_bid,
|
||||
vmap,
|
||||
dst,
|
||||
args,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
);
|
||||
}
|
||||
if iface_name == "env.local" && method_name == "get" {
|
||||
return env::lower_local_get(
|
||||
codegen,
|
||||
cursor,
|
||||
resolver,
|
||||
cur_bid,
|
||||
func,
|
||||
vmap,
|
||||
dst,
|
||||
args,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
);
|
||||
}
|
||||
if iface_name == "env.box" && method_name == "new" {
|
||||
return env::lower_box_new(
|
||||
codegen,
|
||||
cursor,
|
||||
resolver,
|
||||
cur_bid,
|
||||
func,
|
||||
vmap,
|
||||
dst,
|
||||
args,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
);
|
||||
}
|
||||
|
||||
Err(format!(
|
||||
"ExternCall lowering unsupported: {}.{} (add a NyRT shim for this interface method)",
|
||||
iface_name, method_name
|
||||
))
|
||||
}
|
||||
@ -0,0 +1,588 @@
|
||||
use inkwell::basic_block::BasicBlock;
|
||||
use inkwell::values::{BasicValueEnum, IntValue, PhiValue};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{function::MirFunction, BasicBlockId, ValueId};
|
||||
|
||||
use super::super::types::{map_mirtype_to_basic, to_bool};
|
||||
use super::builder_cursor::BuilderCursor;
|
||||
use super::Resolver;
|
||||
|
||||
fn phi_trace_on() -> bool {
|
||||
std::env::var("NYASH_LLVM_TRACE_PHI").ok().as_deref() == Some("1")
|
||||
}
|
||||
|
||||
pub(in super::super) fn emit_return<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
func: &MirFunction,
|
||||
vmap: &HashMap<ValueId, BasicValueEnum<'ctx>>,
|
||||
value: &Option<ValueId>,
|
||||
bb_map: &HashMap<BasicBlockId, BasicBlock<'ctx>>,
|
||||
preds: &HashMap<BasicBlockId, Vec<BasicBlockId>>,
|
||||
block_end_values: &HashMap<BasicBlockId, HashMap<ValueId, BasicValueEnum<'ctx>>>,
|
||||
) -> Result<(), String> {
|
||||
match (&func.signature.return_type, value) {
|
||||
(crate::mir::MirType::Void, _) => {
|
||||
cursor.emit_term(cur_bid, |b| {
|
||||
b.build_return(None).unwrap();
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
(_t, Some(vid)) => {
|
||||
// Resolve return value according to expected type
|
||||
let expected = map_mirtype_to_basic(codegen.context, &func.signature.return_type);
|
||||
use inkwell::types::BasicTypeEnum as BT;
|
||||
let v_adj: BasicValueEnum<'ctx> = match expected {
|
||||
BT::IntType(it) => {
|
||||
let iv = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*vid,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
// Cast to expected width
|
||||
let bw_src = iv.get_type().get_bit_width();
|
||||
let bw_dst = it.get_bit_width();
|
||||
if bw_src == bw_dst {
|
||||
iv.into()
|
||||
} else if bw_src < bw_dst {
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_z_extend(iv, it, "ret_zext"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
} else if bw_dst == 1 {
|
||||
to_bool(codegen.context, iv.into(), &codegen.builder)?.into()
|
||||
} else {
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_truncate(iv, it, "ret_trunc"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
}
|
||||
}
|
||||
BT::PointerType(pt) => {
|
||||
let pv = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*vid,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
// If expected pointer type differs (e.g., typed ptr vs i8*), bitcast
|
||||
if pv.get_type() == pt {
|
||||
pv.into()
|
||||
} else {
|
||||
codegen
|
||||
.builder
|
||||
.build_pointer_cast(pv, pt, "ret_bitcast")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
}
|
||||
}
|
||||
BT::FloatType(ft) => {
|
||||
let fv = resolver.resolve_f64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*vid,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
if fv.get_type() == ft {
|
||||
fv.into()
|
||||
} else {
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_float_cast(fv, ft, "ret_fcast"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()
|
||||
}
|
||||
}
|
||||
_ => return Err("unsupported return basic type".to_string()),
|
||||
};
|
||||
cursor.emit_term(cur_bid, |b| {
|
||||
b.build_return(Some(&v_adj))
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap();
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
(_t, None) => Err("non-void function missing return value".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(in super::super) fn emit_jump<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
bid: BasicBlockId,
|
||||
target: &BasicBlockId,
|
||||
bb_map: &HashMap<BasicBlockId, BasicBlock<'ctx>>,
|
||||
phis_by_block: &HashMap<
|
||||
BasicBlockId,
|
||||
Vec<(ValueId, PhiValue<'ctx>, Vec<(BasicBlockId, ValueId)>)>,
|
||||
>,
|
||||
) -> Result<(), String> {
|
||||
// Non-sealed incoming wiring removed: rely on sealed snapshots and resolver-driven PHIs.
|
||||
let tbb = *bb_map.get(target).ok_or("target bb missing")?;
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!("[LLVM] emit_jump: {} -> {}", bid.as_u32(), target.as_u32());
|
||||
}
|
||||
cursor.emit_term(bid, |b| {
|
||||
b.build_unconditional_branch(tbb)
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap();
|
||||
});
|
||||
if phi_trace_on() {
|
||||
eprintln!(
|
||||
"[PHI:jump] pred={} -> succ={}",
|
||||
bid.as_u32(),
|
||||
target.as_u32()
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(in super::super) fn emit_branch<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::Resolver<'ctx>,
|
||||
bid: BasicBlockId,
|
||||
condition: &ValueId,
|
||||
then_bb: &BasicBlockId,
|
||||
else_bb: &BasicBlockId,
|
||||
bb_map: &HashMap<BasicBlockId, BasicBlock<'ctx>>,
|
||||
phis_by_block: &HashMap<
|
||||
BasicBlockId,
|
||||
Vec<(ValueId, PhiValue<'ctx>, Vec<(BasicBlockId, ValueId)>)>,
|
||||
>,
|
||||
vmap: &HashMap<ValueId, BasicValueEnum<'ctx>>,
|
||||
preds: &HashMap<BasicBlockId, Vec<BasicBlockId>>,
|
||||
block_end_values: &HashMap<BasicBlockId, HashMap<ValueId, BasicValueEnum<'ctx>>>,
|
||||
) -> Result<(), String> {
|
||||
// Localize condition as i64 and convert to i1 via != 0(Resolver 経由のみ)
|
||||
let ci = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
bid,
|
||||
*condition,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let zero = codegen.context.i64_type().const_zero();
|
||||
let b = codegen
|
||||
.builder
|
||||
.build_int_compare(inkwell::IntPredicate::NE, ci, zero, "cond_nez")
|
||||
.map_err(|e| e.to_string())?;
|
||||
// Non-sealed incoming wiring removed: rely on sealed snapshots and resolver-driven PHIs.
|
||||
let tbb = *bb_map.get(then_bb).ok_or("then bb missing")?;
|
||||
let ebb = *bb_map.get(else_bb).ok_or("else bb missing")?;
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!(
|
||||
"[LLVM] emit_branch: {} -> then {} / else {}",
|
||||
bid.as_u32(),
|
||||
then_bb.as_u32(),
|
||||
else_bb.as_u32()
|
||||
);
|
||||
}
|
||||
cursor.emit_term(bid, |bd| {
|
||||
bd.build_conditional_branch(b, tbb, ebb)
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap();
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Coerce a value to the PHI node's type, inserting casts in the current block if necessary.
|
||||
fn coerce_to_type<'ctx>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
phi: &PhiValue<'ctx>,
|
||||
val: BasicValueEnum<'ctx>,
|
||||
) -> Result<BasicValueEnum<'ctx>, String> {
|
||||
use inkwell::types::BasicTypeEnum as BT;
|
||||
match (phi.as_basic_value().get_type(), val) {
|
||||
(BT::IntType(it), BasicValueEnum::IntValue(iv)) => {
|
||||
let bw_src = iv.get_type().get_bit_width();
|
||||
let bw_dst = it.get_bit_width();
|
||||
if bw_src == bw_dst {
|
||||
Ok(iv.into())
|
||||
} else if bw_src < bw_dst {
|
||||
Ok(codegen
|
||||
.builder
|
||||
.build_int_z_extend(iv, it, "phi_zext")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into())
|
||||
} else if bw_dst == 1 {
|
||||
// Narrow to i1 via != 0
|
||||
Ok(
|
||||
super::super::types::to_bool(codegen.context, iv.into(), &codegen.builder)?
|
||||
.into(),
|
||||
)
|
||||
} else {
|
||||
Ok(codegen
|
||||
.builder
|
||||
.build_int_truncate(iv, it, "phi_trunc")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into())
|
||||
}
|
||||
}
|
||||
(BT::IntType(it), BasicValueEnum::PointerValue(pv)) => Ok(codegen
|
||||
.builder
|
||||
.build_ptr_to_int(pv, it, "phi_p2i")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()),
|
||||
(BT::IntType(it), BasicValueEnum::FloatValue(fv)) => Ok(codegen
|
||||
.builder
|
||||
.build_float_to_signed_int(fv, it, "phi_f2i")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()),
|
||||
(BT::PointerType(pt), BasicValueEnum::IntValue(iv)) => Ok(codegen
|
||||
.builder
|
||||
.build_int_to_ptr(iv, pt, "phi_i2p")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()),
|
||||
(BT::PointerType(_), BasicValueEnum::PointerValue(pv)) => Ok(pv.into()),
|
||||
(BT::FloatType(ft), BasicValueEnum::IntValue(iv)) => Ok(codegen
|
||||
.builder
|
||||
.build_signed_int_to_float(iv, ft, "phi_i2f")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into()),
|
||||
(BT::FloatType(_), BasicValueEnum::FloatValue(fv)) => Ok(fv.into()),
|
||||
// Already matching or unsupported combination
|
||||
(_, v) => Ok(v),
|
||||
}
|
||||
}
|
||||
|
||||
/// Sealed-SSA style: when a block is finalized, add PHI incoming for all successor blocks.
|
||||
pub(in super::super) fn seal_block<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
func: &MirFunction,
|
||||
bid: BasicBlockId,
|
||||
succs: &HashMap<BasicBlockId, Vec<BasicBlockId>>,
|
||||
bb_map: &HashMap<BasicBlockId, BasicBlock<'ctx>>,
|
||||
phis_by_block: &HashMap<
|
||||
BasicBlockId,
|
||||
Vec<(ValueId, PhiValue<'ctx>, Vec<(BasicBlockId, ValueId)>)>,
|
||||
>,
|
||||
// Snapshot of value map at end of each predecessor block
|
||||
block_end_values: &HashMap<BasicBlockId, HashMap<ValueId, BasicValueEnum<'ctx>>>,
|
||||
) -> Result<(), String> {
|
||||
if let Some(slist) = succs.get(&bid) {
|
||||
for sb in slist {
|
||||
if let Some(pl) = phis_by_block.get(sb) {
|
||||
for (_dst, phi, inputs) in pl {
|
||||
// Handle only the current predecessor (bid)
|
||||
if let Some((_, in_vid)) = inputs.iter().find(|(p, _)| p == &bid) {
|
||||
// Prefer the predecessorの block-end snapshot。なければ型ゼロを合成
|
||||
let snap_opt = block_end_values
|
||||
.get(&bid)
|
||||
.and_then(|m| m.get(in_vid).copied());
|
||||
let mut val = if let Some(sv) = snap_opt {
|
||||
sv
|
||||
} else {
|
||||
// Synthesize zero to avoid dominance violations(vmap には依存しない)
|
||||
let bt = phi.as_basic_value().get_type();
|
||||
use inkwell::types::BasicTypeEnum as BT;
|
||||
match bt {
|
||||
BT::IntType(it) => it.const_zero().into(),
|
||||
BT::FloatType(ft) => ft.const_zero().into(),
|
||||
BT::PointerType(pt) => pt.const_zero().into(),
|
||||
_ => return Err(format!(
|
||||
"phi incoming (seal) missing: pred={} succ_bb={} in_vid={} (no snapshot)",
|
||||
bid.as_u32(), sb.as_u32(), in_vid.as_u32()
|
||||
)),
|
||||
}
|
||||
};
|
||||
// Insert any required casts in the predecessor block, right before its terminator
|
||||
if let Some(pred_llbb) = bb_map.get(&bid) {
|
||||
cursor.with_block(bid, *pred_llbb, |c| {
|
||||
let term = unsafe { pred_llbb.get_terminator() };
|
||||
if let Some(t) = term {
|
||||
codegen.builder.position_before(&t);
|
||||
} else {
|
||||
c.position_at_end(*pred_llbb);
|
||||
}
|
||||
val = coerce_to_type(codegen, phi, val)
|
||||
.expect("coerce_to_type in seal_block");
|
||||
});
|
||||
}
|
||||
let pred_bb = *bb_map.get(&bid).ok_or("pred bb missing")?;
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
let tys = phi
|
||||
.as_basic_value()
|
||||
.get_type()
|
||||
.print_to_string()
|
||||
.to_string();
|
||||
eprintln!(
|
||||
"[PHI] sealed add pred_bb={} val={} ty={}{}",
|
||||
bid.as_u32(),
|
||||
in_vid.as_u32(),
|
||||
tys,
|
||||
if snap_opt.is_some() {
|
||||
" (snapshot)"
|
||||
} else {
|
||||
" (synth)"
|
||||
}
|
||||
);
|
||||
}
|
||||
match val {
|
||||
BasicValueEnum::IntValue(iv) => phi.add_incoming(&[(&iv, pred_bb)]),
|
||||
BasicValueEnum::FloatValue(fv) => phi.add_incoming(&[(&fv, pred_bb)]),
|
||||
BasicValueEnum::PointerValue(pv) => phi.add_incoming(&[(&pv, pred_bb)]),
|
||||
_ => return Err("unsupported phi incoming value (seal)".to_string()),
|
||||
}
|
||||
} else {
|
||||
// Missing mapping for this predecessor: synthesize a typed zero
|
||||
let pred_bb = *bb_map.get(&bid).ok_or("pred bb missing")?;
|
||||
use inkwell::types::BasicTypeEnum as BT;
|
||||
let bt = phi.as_basic_value().get_type();
|
||||
let z: BasicValueEnum = match bt {
|
||||
BT::IntType(it) => it.const_zero().into(),
|
||||
BT::FloatType(ft) => ft.const_zero().into(),
|
||||
BT::PointerType(pt) => pt.const_zero().into(),
|
||||
_ => {
|
||||
return Err("unsupported phi type for zero synth (seal)".to_string())
|
||||
}
|
||||
};
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!(
|
||||
"[PHI] sealed add (synth) pred_bb={} zero-ty={}",
|
||||
bid.as_u32(),
|
||||
bt.print_to_string().to_string()
|
||||
);
|
||||
}
|
||||
match z {
|
||||
BasicValueEnum::IntValue(iv) => phi.add_incoming(&[(&iv, pred_bb)]),
|
||||
BasicValueEnum::FloatValue(fv) => phi.add_incoming(&[(&fv, pred_bb)]),
|
||||
BasicValueEnum::PointerValue(pv) => phi.add_incoming(&[(&pv, pred_bb)]),
|
||||
_ => return Err("unsupported phi incoming (synth)".to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Normalize PHI incoming entries for a successor block, ensuring exactly
|
||||
/// one entry per predecessor. This runs once all preds have been sealed.
|
||||
pub(in super::super) fn finalize_phis<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
func: &MirFunction,
|
||||
succ_bb: BasicBlockId,
|
||||
preds: &HashMap<BasicBlockId, Vec<BasicBlockId>>,
|
||||
bb_map: &HashMap<BasicBlockId, BasicBlock<'ctx>>,
|
||||
phis_by_block: &HashMap<
|
||||
BasicBlockId,
|
||||
Vec<(ValueId, PhiValue<'ctx>, Vec<(BasicBlockId, ValueId)>)>,
|
||||
>,
|
||||
block_end_values: &HashMap<BasicBlockId, HashMap<ValueId, BasicValueEnum<'ctx>>>,
|
||||
vmap: &HashMap<ValueId, BasicValueEnum<'ctx>>,
|
||||
) -> Result<(), String> {
|
||||
let pred_list = preds.get(&succ_bb).cloned().unwrap_or_default();
|
||||
if pred_list.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
if let Some(phis) = phis_by_block.get(&succ_bb) {
|
||||
for (_dst, phi, inputs) in phis {
|
||||
for pred in &pred_list {
|
||||
// If this phi expects a value from pred, find the associated Mir ValueId
|
||||
if let Some((_, in_vid)) = inputs.iter().find(|(p, _)| p == pred) {
|
||||
// If an incoming from this pred already exists, skip
|
||||
// Note: inkwell does not expose an iterator over incoming; rely on the fact
|
||||
// we add at most once per pred in seal_block. If duplicates occurred earlier,
|
||||
// adding again is harmlessly ignored by verifier if identical; otherwise rely on our new regime.
|
||||
// Fetch value snapshot at end of pred; fallback per our policy
|
||||
let snap_opt = block_end_values
|
||||
.get(pred)
|
||||
.and_then(|m| m.get(in_vid).copied());
|
||||
let mut val = if let Some(sv) = snap_opt {
|
||||
sv
|
||||
} else {
|
||||
let bt = phi.as_basic_value().get_type();
|
||||
use inkwell::types::BasicTypeEnum as BT;
|
||||
match bt {
|
||||
BT::IntType(it) => it.const_zero().into(),
|
||||
BT::FloatType(ft) => ft.const_zero().into(),
|
||||
BT::PointerType(pt) => pt.const_zero().into(),
|
||||
_ => return Err(format!(
|
||||
"phi incoming (finalize) missing: pred={} succ_bb={} in_vid={} (no snapshot)",
|
||||
pred.as_u32(), succ_bb.as_u32(), in_vid.as_u32()
|
||||
)),
|
||||
}
|
||||
};
|
||||
// Insert casts in pred block, just before its terminator
|
||||
if let Some(pred_llbb) = bb_map.get(pred) {
|
||||
cursor.with_block(*pred, *pred_llbb, |c| {
|
||||
let term = unsafe { pred_llbb.get_terminator() };
|
||||
if let Some(t) = term {
|
||||
codegen.builder.position_before(&t);
|
||||
} else {
|
||||
c.position_at_end(*pred_llbb);
|
||||
}
|
||||
val = coerce_to_type(codegen, phi, val)
|
||||
.expect("coerce_to_type finalize_phis");
|
||||
});
|
||||
}
|
||||
let pred_bb = *bb_map.get(pred).ok_or("pred bb missing")?;
|
||||
if phi_trace_on() {
|
||||
eprintln!(
|
||||
"[PHI:finalize] succ={} pred={} vid={} ty={}",
|
||||
succ_bb.as_u32(),
|
||||
pred.as_u32(),
|
||||
in_vid.as_u32(),
|
||||
phi.as_basic_value()
|
||||
.get_type()
|
||||
.print_to_string()
|
||||
.to_string()
|
||||
);
|
||||
}
|
||||
match val {
|
||||
BasicValueEnum::IntValue(iv) => phi.add_incoming(&[(&iv, pred_bb)]),
|
||||
BasicValueEnum::FloatValue(fv) => phi.add_incoming(&[(&fv, pred_bb)]),
|
||||
BasicValueEnum::PointerValue(pv) => phi.add_incoming(&[(&pv, pred_bb)]),
|
||||
_ => return Err("unsupported phi incoming value (finalize)".to_string()),
|
||||
}
|
||||
} else {
|
||||
// This PHI lacks a mapping for this predecessor entirely; synthesize zero
|
||||
let pred_bb = *bb_map.get(pred).ok_or("pred bb missing")?;
|
||||
use inkwell::types::BasicTypeEnum as BT;
|
||||
let bt = phi.as_basic_value().get_type();
|
||||
let z: BasicValueEnum = match bt {
|
||||
BT::IntType(it) => it.const_zero().into(),
|
||||
BT::FloatType(ft) => ft.const_zero().into(),
|
||||
BT::PointerType(pt) => pt.const_zero().into(),
|
||||
_ => {
|
||||
return Err("unsupported phi type for zero synth (finalize)".to_string())
|
||||
}
|
||||
};
|
||||
if phi_trace_on() {
|
||||
eprintln!(
|
||||
"[PHI:finalize] succ={} pred={} vid=? ty={} src=synth_zero",
|
||||
succ_bb.as_u32(),
|
||||
pred.as_u32(),
|
||||
bt.print_to_string().to_string()
|
||||
);
|
||||
}
|
||||
match z {
|
||||
BasicValueEnum::IntValue(iv) => phi.add_incoming(&[(&iv, pred_bb)]),
|
||||
BasicValueEnum::FloatValue(fv) => phi.add_incoming(&[(&fv, pred_bb)]),
|
||||
BasicValueEnum::PointerValue(pv) => phi.add_incoming(&[(&pv, pred_bb)]),
|
||||
_ => return Err("unsupported phi incoming (synth finalize)".to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Localize a MIR value as an i64 in the current block by creating a PHI that merges
|
||||
/// predecessor snapshots. This avoids using values defined in non-dominating blocks.
|
||||
/// Sealed SSA mode is assumed; when a predecessor snapshot is missing, synthesize zero.
|
||||
pub(in super::super) fn localize_to_i64<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
cur_bid: BasicBlockId,
|
||||
vid: ValueId,
|
||||
bb_map: &std::collections::HashMap<BasicBlockId, BasicBlock<'ctx>>,
|
||||
preds: &std::collections::HashMap<BasicBlockId, Vec<BasicBlockId>>,
|
||||
block_end_values: &std::collections::HashMap<
|
||||
BasicBlockId,
|
||||
std::collections::HashMap<ValueId, BasicValueEnum<'ctx>>,
|
||||
>,
|
||||
vmap: &std::collections::HashMap<ValueId, BasicValueEnum<'ctx>>,
|
||||
) -> Result<IntValue<'ctx>, String> {
|
||||
let i64t = codegen.context.i64_type();
|
||||
// Note: avoid using current vmap directly here, as it may hold values
|
||||
// defined in non-dominating predecessors. We rely on predecessor snapshots
|
||||
// in sealed SSA mode to maintain dominance.
|
||||
let cur_llbb = *bb_map.get(&cur_bid).ok_or("cur bb missing")?;
|
||||
// If no predecessors, conservatively return zero(vmap には依存しない)
|
||||
let pred_list = preds.get(&cur_bid).cloned().unwrap_or_default();
|
||||
if pred_list.is_empty() {
|
||||
return Ok(i64t.const_zero());
|
||||
}
|
||||
// Build PHI at the top of current block (before any non-PHI), then restore insertion point
|
||||
let saved_ip = codegen.builder.get_insert_block();
|
||||
if let Some(first) = cur_llbb.get_first_instruction() {
|
||||
codegen.builder.position_before(&first);
|
||||
} else {
|
||||
codegen.builder.position_at_end(cur_llbb);
|
||||
}
|
||||
let phi = codegen
|
||||
.builder
|
||||
.build_phi(i64t, &format!("loc_i64_{}", vid.as_u32()))
|
||||
.map_err(|e| e.to_string())?;
|
||||
for p in &pred_list {
|
||||
let pred_bb = *bb_map.get(p).ok_or("pred bb missing")?;
|
||||
// Fetch snapshot at end of pred; if missing, synthesize zero
|
||||
let base = block_end_values
|
||||
.get(p)
|
||||
.and_then(|m| m.get(&vid).copied())
|
||||
.unwrap_or_else(|| i64t.const_zero().into());
|
||||
// Insert required casts in the predecessor block before its terminator
|
||||
let mut iv_out = i64t.const_zero();
|
||||
cursor.with_block(*p, pred_bb, |c| {
|
||||
let term = unsafe { pred_bb.get_terminator() };
|
||||
if let Some(t) = term {
|
||||
codegen.builder.position_before(&t);
|
||||
} else {
|
||||
c.position_at_end(pred_bb);
|
||||
}
|
||||
iv_out = match base {
|
||||
BasicValueEnum::IntValue(iv) => {
|
||||
if iv.get_type() == i64t {
|
||||
iv
|
||||
} else {
|
||||
codegen
|
||||
.builder
|
||||
.build_int_z_extend(iv, i64t, "loc_zext_p")
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
BasicValueEnum::PointerValue(pv) => codegen
|
||||
.builder
|
||||
.build_ptr_to_int(pv, i64t, "loc_p2i_p")
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap(),
|
||||
BasicValueEnum::FloatValue(fv) => codegen
|
||||
.builder
|
||||
.build_float_to_signed_int(fv, i64t, "loc_f2i_p")
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap(),
|
||||
_ => i64t.const_zero(),
|
||||
};
|
||||
});
|
||||
phi.add_incoming(&[(&iv_out, pred_bb)]);
|
||||
if phi_trace_on() {
|
||||
eprintln!(
|
||||
"[PHI:resolve] cur={} pred={} vid={} ty=i64",
|
||||
cur_bid.as_u32(),
|
||||
p.as_u32(),
|
||||
vid.as_u32()
|
||||
);
|
||||
}
|
||||
}
|
||||
// Restore insertion point
|
||||
if let Some(bb) = saved_ip {
|
||||
codegen.builder.position_at_end(bb);
|
||||
}
|
||||
Ok(phi.as_basic_value().into_int_value())
|
||||
}
|
||||
@ -0,0 +1,281 @@
|
||||
use inkwell::{
|
||||
basic_block::BasicBlock,
|
||||
values::{BasicValueEnum, FunctionValue, PhiValue},
|
||||
};
|
||||
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{function::MirFunction, instruction::MirInstruction, BasicBlockId, ValueId};
|
||||
|
||||
use super::super::types::to_bool;
|
||||
use super::builder_cursor::BuilderCursor;
|
||||
use super::Resolver;
|
||||
|
||||
/// LoopForm scaffolding — fixed block layout for while/loop normalization
|
||||
pub struct LoopFormContext<'ctx> {
|
||||
pub preheader: BasicBlock<'ctx>,
|
||||
pub header: BasicBlock<'ctx>,
|
||||
pub body: BasicBlock<'ctx>,
|
||||
pub dispatch: BasicBlock<'ctx>,
|
||||
pub latch: BasicBlock<'ctx>,
|
||||
pub exit: BasicBlock<'ctx>,
|
||||
pub loop_id: u32,
|
||||
}
|
||||
|
||||
impl<'ctx> LoopFormContext<'ctx> {
|
||||
/// Create a new LoopForm block set under `function` with a readable name prefix.
|
||||
pub fn new(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
function: FunctionValue<'ctx>,
|
||||
loop_id: u32,
|
||||
prefix: &str,
|
||||
) -> Self {
|
||||
let preheader = codegen
|
||||
.context
|
||||
.append_basic_block(function, &format!("{}_lf{}_preheader", prefix, loop_id));
|
||||
let header = codegen
|
||||
.context
|
||||
.append_basic_block(function, &format!("{}_lf{}_header", prefix, loop_id));
|
||||
let body = codegen
|
||||
.context
|
||||
.append_basic_block(function, &format!("{}_lf{}_body", prefix, loop_id));
|
||||
let dispatch = codegen
|
||||
.context
|
||||
.append_basic_block(function, &format!("{}_lf{}_dispatch", prefix, loop_id));
|
||||
let latch = codegen
|
||||
.context
|
||||
.append_basic_block(function, &format!("{}_lf{}_latch", prefix, loop_id));
|
||||
let exit = codegen
|
||||
.context
|
||||
.append_basic_block(function, &format!("{}_lf{}_exit", prefix, loop_id));
|
||||
Self {
|
||||
preheader,
|
||||
header,
|
||||
body,
|
||||
dispatch,
|
||||
latch,
|
||||
exit,
|
||||
loop_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lower a while-like loop using LoopForm shape (Phase 1: scaffold only).
|
||||
/// - condition: MIR value producing i1/i64 truthy
|
||||
/// - body_mir: MIR instructions of loop body
|
||||
/// Note: In Phase 1, this function is not invoked by default lowering; it is a gated scaffold.
|
||||
pub fn lower_while_loopform<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut Resolver<'ctx>,
|
||||
func: &MirFunction,
|
||||
llvm_func: FunctionValue<'ctx>,
|
||||
condition: &ValueId,
|
||||
_body_mir: &[MirInstruction],
|
||||
loop_id: u32,
|
||||
prefix: &str,
|
||||
header_bid: BasicBlockId,
|
||||
body_bb: BasicBlockId,
|
||||
after_bb: BasicBlockId,
|
||||
bb_map: &std::collections::HashMap<BasicBlockId, BasicBlock<'ctx>>,
|
||||
vmap: &std::collections::HashMap<ValueId, BasicValueEnum<'ctx>>,
|
||||
preds: &std::collections::HashMap<BasicBlockId, Vec<BasicBlockId>>,
|
||||
block_end_values: &std::collections::HashMap<
|
||||
BasicBlockId,
|
||||
std::collections::HashMap<ValueId, BasicValueEnum<'ctx>>,
|
||||
>,
|
||||
// Registry to allow later body→dispatch wiring (simple bodies)
|
||||
registry: &mut std::collections::HashMap<
|
||||
BasicBlockId,
|
||||
(
|
||||
BasicBlock<'ctx>,
|
||||
PhiValue<'ctx>,
|
||||
PhiValue<'ctx>,
|
||||
BasicBlock<'ctx>,
|
||||
),
|
||||
>,
|
||||
body_to_header: &mut std::collections::HashMap<BasicBlockId, BasicBlockId>,
|
||||
) -> Result<bool, String> {
|
||||
let enabled = std::env::var("NYASH_ENABLE_LOOPFORM").ok().as_deref() == Some("1");
|
||||
if !enabled {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
// Create LoopForm fixed blocks under the same function
|
||||
let lf = LoopFormContext::new(codegen, llvm_func, loop_id, prefix);
|
||||
// Preheader: currently a pass-through to header (Phase 1)
|
||||
codegen.builder.position_at_end(lf.preheader);
|
||||
codegen
|
||||
.builder
|
||||
.build_unconditional_branch(lf.header)
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap();
|
||||
|
||||
// Header: evaluate condition via Resolver and branch to body (for true) or dispatch (for false)
|
||||
let ci = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
header_bid,
|
||||
*condition,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let cond_i1 = codegen
|
||||
.builder
|
||||
.build_int_compare(
|
||||
inkwell::IntPredicate::NE,
|
||||
ci,
|
||||
codegen.context.i64_type().const_zero(),
|
||||
"lf_cond_nez",
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
cursor.emit_term(header_bid, |b| {
|
||||
b.build_conditional_branch(cond_i1, lf.body, lf.dispatch)
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap();
|
||||
});
|
||||
|
||||
// Body: currently pass-through to original body block (non-invasive Phase 1)
|
||||
let orig_body = *bb_map.get(&body_bb).ok_or("loopform: body bb missing")?;
|
||||
cursor.with_block(body_bb, lf.body, |c| {
|
||||
c.emit_term(body_bb, |b| {
|
||||
b.build_unconditional_branch(orig_body)
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap();
|
||||
});
|
||||
});
|
||||
|
||||
// Dispatch: create PHIs (tag i8, payload i64) and switch(tag)
|
||||
// For now, only header(false) contributes (Break=1); body path does not reach dispatch in Phase 1 wiring.
|
||||
let orig_after = *bb_map.get(&after_bb).ok_or("loopform: after bb missing")?;
|
||||
let header_llbb = *bb_map
|
||||
.get(&header_bid)
|
||||
.ok_or("loopform: header bb missing")?;
|
||||
let (tag_phi, payload_phi) = cursor.with_block(after_bb, lf.dispatch, |c| {
|
||||
let i8t = codegen.context.i8_type();
|
||||
let i64t = codegen.context.i64_type();
|
||||
let tag_ty: inkwell::types::BasicTypeEnum = i8t.into();
|
||||
let tag_phi = codegen
|
||||
.builder
|
||||
.build_phi(tag_ty, "lf_tag")
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap();
|
||||
let payload_ty: inkwell::types::BasicTypeEnum = i64t.into();
|
||||
let payload_phi = codegen
|
||||
.builder
|
||||
.build_phi(payload_ty, "lf_payload")
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap();
|
||||
let tag_break = i8t.const_int(1, false);
|
||||
let payload_zero = i64t.const_zero();
|
||||
tag_phi.add_incoming(&[(&tag_break, header_llbb)]);
|
||||
payload_phi.add_incoming(&[(&payload_zero, header_llbb)]);
|
||||
let tag_iv = tag_phi.as_basic_value().into_int_value();
|
||||
c.emit_term(after_bb, |b| {
|
||||
b.build_switch(tag_iv, lf.exit, &[(i8t.const_int(0, false), lf.latch)])
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap();
|
||||
});
|
||||
(tag_phi, payload_phi)
|
||||
});
|
||||
|
||||
// Register for simple body→dispatch wiring later (at body terminator lowering time)
|
||||
registry.insert(header_bid, (lf.dispatch, tag_phi, payload_phi, lf.latch));
|
||||
body_to_header.insert(body_bb, header_bid);
|
||||
|
||||
// Latch: optionally jump back to header (gated), otherwise keep unreachable to avoid header pred増
|
||||
codegen.builder.position_at_end(lf.latch);
|
||||
if std::env::var("NYASH_LOOPFORM_LATCH2HEADER").ok().as_deref() == Some("1") {
|
||||
codegen
|
||||
.builder
|
||||
.build_unconditional_branch(header_llbb)
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap();
|
||||
} else {
|
||||
let _ = codegen.builder.build_unreachable();
|
||||
}
|
||||
// Exit: to original after
|
||||
codegen.builder.position_at_end(lf.exit);
|
||||
codegen
|
||||
.builder
|
||||
.build_unconditional_branch(orig_after)
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap();
|
||||
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!(
|
||||
"[LoopForm] wired header->(body/dispatch) and pass-through to then/else (lf_id={})",
|
||||
loop_id
|
||||
);
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// LoopForm header PHI normalization: when enabling latch→header, header gains an extra LLVM
|
||||
/// predecessor (latch) that is not represented in MIR predecessors. To satisfy LLVM's verifier,
|
||||
/// ensure every PHI in the header has an incoming for the latch. For Phase 1, we conservatively
|
||||
/// wire a typed zero as the incoming value for the latch.
|
||||
pub fn normalize_header_phis_for_latch<'ctx>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
header_bid: BasicBlockId,
|
||||
latch_bb: BasicBlock<'ctx>,
|
||||
phis: &[(ValueId, PhiValue<'ctx>, Vec<(BasicBlockId, ValueId)>)],
|
||||
) -> Result<(), String> {
|
||||
use inkwell::types::BasicTypeEnum as BT;
|
||||
let _ = header_bid; // reserved for future diagnostics
|
||||
for (_dst, phi, _inputs) in phis {
|
||||
let bt = phi.as_basic_value().get_type();
|
||||
let z = match bt {
|
||||
BT::IntType(it) => it.const_zero().into(),
|
||||
BT::FloatType(ft) => ft.const_zero().into(),
|
||||
BT::PointerType(pt) => pt.const_zero().into(),
|
||||
_ => return Err("unsupported phi type for latch incoming".to_string()),
|
||||
};
|
||||
match z {
|
||||
BasicValueEnum::IntValue(iv) => phi.add_incoming(&[(&iv, latch_bb)]),
|
||||
BasicValueEnum::FloatValue(fv) => phi.add_incoming(&[(&fv, latch_bb)]),
|
||||
BasicValueEnum::PointerValue(pv) => phi.add_incoming(&[(&pv, latch_bb)]),
|
||||
_ => return Err("unsupported zero value kind for latch incoming".to_string()),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Dev check: when enabled, log PHIs that live outside dispatch blocks created by LoopForm
|
||||
pub(in super::super) fn dev_check_dispatch_only_phi<'ctx>(
|
||||
phis_by_block: &std::collections::HashMap<
|
||||
crate::mir::BasicBlockId,
|
||||
Vec<(
|
||||
crate::mir::ValueId,
|
||||
inkwell::values::PhiValue<'ctx>,
|
||||
Vec<(crate::mir::BasicBlockId, crate::mir::ValueId)>,
|
||||
)>,
|
||||
>,
|
||||
loopform_registry: &std::collections::HashMap<
|
||||
crate::mir::BasicBlockId,
|
||||
(
|
||||
inkwell::basic_block::BasicBlock<'ctx>,
|
||||
inkwell::values::PhiValue<'ctx>,
|
||||
inkwell::values::PhiValue<'ctx>,
|
||||
inkwell::basic_block::BasicBlock<'ctx>,
|
||||
),
|
||||
>,
|
||||
) {
|
||||
if std::env::var("NYASH_DEV_CHECK_DISPATCH_ONLY_PHI")
|
||||
.ok()
|
||||
.as_deref()
|
||||
!= Some("1")
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Best-effort: Just report PHI presence per block when LoopForm registry is non-empty.
|
||||
if !loopform_registry.is_empty() {
|
||||
for (bid, phis) in phis_by_block.iter() {
|
||||
if phis.is_empty() {
|
||||
continue;
|
||||
}
|
||||
eprintln!("[DEV][PHI] bb={} has {} PHI(s)", bid.as_u32(), phis.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,293 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use inkwell::{values::BasicValueEnum as BVE, AddressSpace};
|
||||
|
||||
use super::builder_cursor::BuilderCursor;
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{function::MirFunction, BasicBlockId, ValueId};
|
||||
|
||||
/// Handle MapBox fast-paths (core-first). Returns true if handled.
|
||||
pub(super) fn try_handle_map_method<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
func: &MirFunction,
|
||||
vmap: &mut HashMap<ValueId, inkwell::values::BasicValueEnum<'ctx>>,
|
||||
dst: &Option<ValueId>,
|
||||
box_val: &ValueId,
|
||||
method: &str,
|
||||
args: &[ValueId],
|
||||
recv_h: inkwell::values::IntValue<'ctx>,
|
||||
) -> Result<bool, String> {
|
||||
// Only when receiver is annotated as MapBox
|
||||
let is_map_annot = matches!(func.metadata.value_types.get(box_val), Some(crate::mir::MirType::Box(b)) if b == "MapBox");
|
||||
if !is_map_annot {
|
||||
return Ok(false);
|
||||
}
|
||||
let i64t = codegen.context.i64_type();
|
||||
match method {
|
||||
"size" => {
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!("[LLVM] lower Map.size (core)");
|
||||
}
|
||||
if !args.is_empty() {
|
||||
return Err("MapBox.size expects 0 arg".to_string());
|
||||
}
|
||||
let fnty = i64t.fn_type(&[i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.map.size_h")
|
||||
.unwrap_or_else(|| codegen.module.add_function("nyash.map.size_h", fnty, None));
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_call(callee, &[recv_h.into()], "msize"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("map.size_h returned void".to_string())?;
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
"has" => {
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!("[LLVM] lower Map.has (core)");
|
||||
}
|
||||
if args.len() != 1 {
|
||||
return Err("MapBox.has expects 1 arg".to_string());
|
||||
}
|
||||
let key_i = match func.metadata.value_types.get(&args[0]) {
|
||||
Some(crate::mir::MirType::String) => {
|
||||
// string key: i8* -> handle
|
||||
let pv = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
&std::collections::HashMap::new(),
|
||||
&std::collections::HashMap::new(),
|
||||
&std::collections::HashMap::new(),
|
||||
vmap,
|
||||
)?;
|
||||
let fnty_conv = i64t.fn_type(
|
||||
&[codegen.context.ptr_type(AddressSpace::from(0)).into()],
|
||||
false,
|
||||
);
|
||||
let conv = codegen
|
||||
.module
|
||||
.get_function("nyash.box.from_i8_string")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.box.from_i8_string", fnty_conv, None)
|
||||
});
|
||||
let kcall = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(conv, &[pv.into()], "key_i8_to_handle")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
kcall
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_i8_string returned void".to_string())?
|
||||
.into_int_value()
|
||||
}
|
||||
_ => resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
&std::collections::HashMap::new(),
|
||||
&std::collections::HashMap::new(),
|
||||
&std::collections::HashMap::new(),
|
||||
vmap,
|
||||
)?,
|
||||
};
|
||||
let fnty = i64t.fn_type(&[i64t.into(), i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.map.has_h")
|
||||
.unwrap_or_else(|| codegen.module.add_function("nyash.map.has_h", fnty, None));
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[recv_h.into(), key_i.into()], "mhas")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("map.has_h returned void".to_string())?;
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
"get" => {
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!("[LLVM] lower Map.get (core)");
|
||||
}
|
||||
if args.len() != 1 {
|
||||
return Err("MapBox.get expects 1 arg".to_string());
|
||||
}
|
||||
let call = match func.metadata.value_types.get(&args[0]) {
|
||||
Some(crate::mir::MirType::String) => {
|
||||
// key: i8* -> i64 handle via from_i8_string (string key)
|
||||
let pv = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
&std::collections::HashMap::new(),
|
||||
&std::collections::HashMap::new(),
|
||||
&std::collections::HashMap::new(),
|
||||
vmap,
|
||||
)?;
|
||||
let fnty_conv = i64t.fn_type(
|
||||
&[codegen.context.ptr_type(AddressSpace::from(0)).into()],
|
||||
false,
|
||||
);
|
||||
let conv = codegen
|
||||
.module
|
||||
.get_function("nyash.box.from_i8_string")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.box.from_i8_string", fnty_conv, None)
|
||||
});
|
||||
let kcall = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(conv, &[pv.into()], "key_i8_to_handle")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let kh = kcall
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_i8_string returned void".to_string())?
|
||||
.into_int_value();
|
||||
let fnty = i64t.fn_type(&[i64t.into(), i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.map.get_hh")
|
||||
.unwrap_or_else(|| {
|
||||
codegen.module.add_function("nyash.map.get_hh", fnty, None)
|
||||
});
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[recv_h.into(), kh.into()], "mget_hh")
|
||||
})
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
_ => {
|
||||
let iv = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
&std::collections::HashMap::new(),
|
||||
&std::collections::HashMap::new(),
|
||||
&std::collections::HashMap::new(),
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i64t.fn_type(&[i64t.into(), i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.map.get_h")
|
||||
.unwrap_or_else(|| {
|
||||
codegen.module.add_function("nyash.map.get_h", fnty, None)
|
||||
});
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[recv_h.into(), iv.into()], "mget")
|
||||
})
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
};
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("map.get returned void".to_string())?;
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
"set" => {
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!("[LLVM] lower Map.set (core)");
|
||||
}
|
||||
if args.len() != 2 {
|
||||
return Err("MapBox.set expects 2 args (key, value)".to_string());
|
||||
}
|
||||
let key_i = match func.metadata.value_types.get(&args[0]) {
|
||||
Some(crate::mir::MirType::String) => {
|
||||
let pv = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
&std::collections::HashMap::new(),
|
||||
&std::collections::HashMap::new(),
|
||||
&std::collections::HashMap::new(),
|
||||
vmap,
|
||||
)?;
|
||||
let fnty_conv = i64t.fn_type(
|
||||
&[codegen.context.ptr_type(AddressSpace::from(0)).into()],
|
||||
false,
|
||||
);
|
||||
let conv = codegen
|
||||
.module
|
||||
.get_function("nyash.box.from_i8_string")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.box.from_i8_string", fnty_conv, None)
|
||||
});
|
||||
let kcall = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(conv, &[pv.into()], "key_i8_to_handle")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
kcall
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_i8_string returned void".to_string())?
|
||||
.into_int_value()
|
||||
}
|
||||
_ => resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
&std::collections::HashMap::new(),
|
||||
&std::collections::HashMap::new(),
|
||||
&std::collections::HashMap::new(),
|
||||
vmap,
|
||||
)?,
|
||||
};
|
||||
let val_i = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[1],
|
||||
&std::collections::HashMap::new(),
|
||||
&std::collections::HashMap::new(),
|
||||
&std::collections::HashMap::new(),
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i64t.fn_type(&[i64t.into(), i64t.into(), i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.map.set_h")
|
||||
.unwrap_or_else(|| codegen.module.add_function("nyash.map.set_h", fnty, None));
|
||||
let _ = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[recv_h.into(), key_i.into(), val_i.into()], "mset")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(true)
|
||||
}
|
||||
_ => Ok(false),
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,267 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use inkwell::values::BasicValueEnum;
|
||||
|
||||
use super::builder_cursor::BuilderCursor;
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{BasicBlockId, ValueId};
|
||||
|
||||
// Lower Store: handle allocas with element type tracking and integer width adjust
|
||||
pub(in super::super) fn lower_store<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
vmap: &HashMap<ValueId, BasicValueEnum<'ctx>>,
|
||||
allocas: &mut HashMap<ValueId, inkwell::values::PointerValue<'ctx>>,
|
||||
alloca_elem_types: &mut HashMap<ValueId, inkwell::types::BasicTypeEnum<'ctx>>,
|
||||
value: &ValueId,
|
||||
ptr: &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, BasicValueEnum<'ctx>>,
|
||||
>,
|
||||
) -> Result<(), String> {
|
||||
use inkwell::types::BasicTypeEnum;
|
||||
// Resolve value preferring native kind; try i64, then f64, else pointer
|
||||
let i64t = codegen.context.i64_type();
|
||||
let val: BasicValueEnum = if let Ok(iv) = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*value,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
) {
|
||||
iv.into()
|
||||
} else if let Ok(fv) = resolver.resolve_f64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*value,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
) {
|
||||
fv.into()
|
||||
} else if let Ok(pv) = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*value,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
) {
|
||||
pv.into()
|
||||
} else {
|
||||
// Fallback: zero i64
|
||||
i64t.const_zero().into()
|
||||
};
|
||||
let elem_ty = match val {
|
||||
BasicValueEnum::IntValue(iv) => BasicTypeEnum::IntType(iv.get_type()),
|
||||
BasicValueEnum::FloatValue(fv) => BasicTypeEnum::FloatType(fv.get_type()),
|
||||
BasicValueEnum::PointerValue(pv) => BasicTypeEnum::PointerType(pv.get_type()),
|
||||
_ => return Err("unsupported store value type".to_string()),
|
||||
};
|
||||
if let Some(existing) = allocas.get(ptr).copied() {
|
||||
let existing_elem = *alloca_elem_types
|
||||
.get(ptr)
|
||||
.ok_or("alloca elem type missing")?;
|
||||
if existing_elem != elem_ty {
|
||||
match (val, existing_elem) {
|
||||
(BasicValueEnum::IntValue(iv), BasicTypeEnum::IntType(t)) => {
|
||||
let bw_src = iv.get_type().get_bit_width();
|
||||
let bw_dst = t.get_bit_width();
|
||||
if bw_src < bw_dst {
|
||||
let adj = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_z_extend(iv, t, "zext"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_store(existing, adj))
|
||||
.map_err(|e| e.to_string())?;
|
||||
} else if bw_src > bw_dst {
|
||||
let adj = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_truncate(iv, t, "trunc"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_store(existing, adj))
|
||||
.map_err(|e| e.to_string())?;
|
||||
} else {
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_store(existing, iv))
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
}
|
||||
(BasicValueEnum::PointerValue(pv), BasicTypeEnum::PointerType(pt)) => {
|
||||
let adj = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_pointer_cast(pv, pt, "pcast"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_store(existing, adj))
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
(BasicValueEnum::FloatValue(fv), BasicTypeEnum::FloatType(ft)) => {
|
||||
// Only f64 currently expected
|
||||
if fv.get_type() != ft {
|
||||
return Err("float width mismatch in store".to_string());
|
||||
}
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_store(existing, fv))
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
_ => return Err("store type mismatch".to_string()),
|
||||
}
|
||||
} else {
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_store(existing, val))
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
} else {
|
||||
let slot = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_alloca(elem_ty, &format!("slot_{}", ptr.as_u32()))
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
cursor
|
||||
.emit_instr(cur_bid, |b| b.build_store(slot, val))
|
||||
.map_err(|e| e.to_string())?;
|
||||
allocas.insert(*ptr, slot);
|
||||
alloca_elem_types.insert(*ptr, elem_ty);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(in super::super) fn lower_load<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
cur_bid: BasicBlockId,
|
||||
vmap: &mut HashMap<ValueId, BasicValueEnum<'ctx>>,
|
||||
allocas: &mut HashMap<ValueId, inkwell::values::PointerValue<'ctx>>,
|
||||
alloca_elem_types: &mut HashMap<ValueId, inkwell::types::BasicTypeEnum<'ctx>>,
|
||||
dst: &ValueId,
|
||||
ptr: &ValueId,
|
||||
) -> Result<(), String> {
|
||||
use inkwell::types::BasicTypeEnum;
|
||||
let (slot, elem_ty) = if let Some(s) = allocas.get(ptr).copied() {
|
||||
let et = *alloca_elem_types
|
||||
.get(ptr)
|
||||
.ok_or("alloca elem type missing")?;
|
||||
(s, et)
|
||||
} else {
|
||||
// Default new slot as i64 for uninitialized loads
|
||||
let i64t = codegen.context.i64_type();
|
||||
let slot = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_alloca(i64t, &format!("slot_{}", ptr.as_u32()))
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
allocas.insert(*ptr, slot);
|
||||
alloca_elem_types.insert(*ptr, i64t.into());
|
||||
(slot, i64t.into())
|
||||
};
|
||||
let lv = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_load(elem_ty, slot, &format!("load_{}", dst.as_u32()))
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(*dst, lv);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Lower Copy: define dst in the current block by localizing src via Resolver
|
||||
pub(in super::super) fn lower_copy<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
func: &crate::mir::function::MirFunction,
|
||||
vmap: &mut HashMap<ValueId, BasicValueEnum<'ctx>>,
|
||||
dst: &ValueId,
|
||||
src: &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, BasicValueEnum<'ctx>>,
|
||||
>,
|
||||
) -> Result<(), String> {
|
||||
// Choose resolution kind based on metadata type preference
|
||||
use inkwell::types::BasicTypeEnum as BT;
|
||||
let expected_bt: Option<BT> = func
|
||||
.metadata
|
||||
.value_types
|
||||
.get(dst)
|
||||
.or_else(|| func.metadata.value_types.get(src))
|
||||
.map(|mt| super::super::types::map_mirtype_to_basic(codegen.context, mt));
|
||||
let out: BasicValueEnum<'ctx> = match expected_bt {
|
||||
Some(BT::IntType(_)) | None => {
|
||||
// Prefer i64 for unknown
|
||||
let iv = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*src,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
iv.into()
|
||||
}
|
||||
Some(BT::PointerType(_)) => {
|
||||
let pv = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*src,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
pv.into()
|
||||
}
|
||||
Some(BT::FloatType(_)) => {
|
||||
let fv = resolver.resolve_f64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*src,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
fv.into()
|
||||
}
|
||||
_ => {
|
||||
// Fallback i64
|
||||
let iv = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*src,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
iv.into()
|
||||
}
|
||||
};
|
||||
vmap.insert(*dst, out);
|
||||
Ok(())
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
mod arith;
|
||||
mod arith_ops;
|
||||
mod arrays;
|
||||
mod blocks;
|
||||
mod boxcall;
|
||||
pub mod builder_cursor;
|
||||
mod call;
|
||||
mod consts;
|
||||
pub mod ctx;
|
||||
mod externcall;
|
||||
pub mod flow;
|
||||
mod loopform;
|
||||
mod maps;
|
||||
mod mem;
|
||||
mod newbox;
|
||||
mod resolver;
|
||||
pub mod string_ops;
|
||||
mod strings;
|
||||
mod terminators; // scaffolding: re-exports flow terminators
|
||||
mod select; // scaffolding: prepare for cond/short-circuit helpers
|
||||
|
||||
pub(super) use arith::lower_compare;
|
||||
pub(super) use arith_ops::{lower_binop, lower_unary};
|
||||
pub(super) use blocks::{create_basic_blocks, precreate_phis};
|
||||
pub(super) use boxcall::{lower_boxcall, lower_boxcall_boxed, lower_boxcall_via_ctx};
|
||||
pub(super) use call::lower_call;
|
||||
pub(super) use consts::lower_const;
|
||||
pub(super) use externcall::lower_externcall;
|
||||
pub(super) use flow::{emit_branch, emit_jump, emit_return};
|
||||
// Future: swap callers to use `terminators::*` instead of `flow::*` directly
|
||||
pub(super) use terminators::{emit_branch as term_emit_branch, emit_jump as term_emit_jump, emit_return as term_emit_return};
|
||||
pub(super) use select::normalize_branch_condition;
|
||||
pub(super) use loopform::dev_check_dispatch_only_phi;
|
||||
pub(super) use loopform::normalize_header_phis_for_latch;
|
||||
pub(super) use loopform::{lower_while_loopform, LoopFormContext};
|
||||
pub(super) use mem::lower_copy;
|
||||
pub(super) use mem::{lower_load, lower_store};
|
||||
pub(super) use newbox::lower_newbox;
|
||||
pub(super) use resolver::Resolver;
|
||||
@ -0,0 +1,183 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use inkwell::values::BasicValueEnum as BVE;
|
||||
use inkwell::AddressSpace;
|
||||
|
||||
use super::builder_cursor::BuilderCursor;
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{BasicBlockId, ValueId};
|
||||
|
||||
// NewBox lowering (subset consistent with existing code)
|
||||
pub(in super::super) fn lower_newbox<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut super::Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
vmap: &mut HashMap<ValueId, inkwell::values::BasicValueEnum<'ctx>>,
|
||||
dst: ValueId,
|
||||
box_type: &str,
|
||||
args: &[ValueId],
|
||||
box_type_ids: &HashMap<String, i64>,
|
||||
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<(), String> {
|
||||
match (box_type, args.len()) {
|
||||
("StringBox", 1) => {
|
||||
// Resolve as i8* string pointer (AOT string fast-path)
|
||||
let p = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
vmap.insert(dst, p.into());
|
||||
Ok(())
|
||||
}
|
||||
(_, n) if n == 1 || n == 2 => {
|
||||
let type_id = *box_type_ids.get(box_type).unwrap_or(&0);
|
||||
let i64t = codegen.context.i64_type();
|
||||
let fnty = i64t.fn_type(&[i64t.into(), i64t.into(), i64t.into(), i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.box.birth_i64")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.box.birth_i64", fnty, None)
|
||||
});
|
||||
let argc = i64t.const_int(args.len() as u64, false);
|
||||
let mut a1 = i64t.const_zero();
|
||||
let mut a2 = i64t.const_zero();
|
||||
if args.len() >= 1 {
|
||||
a1 = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
}
|
||||
if args.len() >= 2 {
|
||||
a2 = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[1],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
}
|
||||
let tid = i64t.const_int(type_id as u64, true);
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(
|
||||
callee,
|
||||
&[tid.into(), argc.into(), a1.into(), a2.into()],
|
||||
"birth_i64",
|
||||
)
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let h = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("birth_i64 returned void".to_string())?
|
||||
.into_int_value();
|
||||
let pty = codegen.context.ptr_type(AddressSpace::from(0));
|
||||
let ptr = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_to_ptr(h, pty, "handle_to_ptr"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(dst, ptr.into());
|
||||
Ok(())
|
||||
}
|
||||
_ => {
|
||||
// No-arg birth via central type registry (preferred),
|
||||
// fallback to env.box.new(name) when type_id is unavailable.
|
||||
if !args.is_empty() {
|
||||
return Err("NewBox with >2 args not yet supported in LLVM lowering".to_string());
|
||||
}
|
||||
let type_id = *box_type_ids.get(box_type).unwrap_or(&0);
|
||||
// Temporary gate: allow forcing MapBox to plugin path explicitly
|
||||
let force_plugin_map =
|
||||
std::env::var("NYASH_LLVM_FORCE_PLUGIN_MAP").ok().as_deref() == Some("1");
|
||||
let i64t = codegen.context.i64_type();
|
||||
// Core-first: avoid birth_h for built-ins we provide directly (MapBox/ArrayBox)
|
||||
let is_core_builtin = box_type == "MapBox" || box_type == "ArrayBox";
|
||||
if type_id != 0 && !(is_core_builtin && !force_plugin_map) {
|
||||
// declare i64 @nyash.box.birth_h(i64)
|
||||
let fn_ty = i64t.fn_type(&[i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.box.birth_h")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.box.birth_h", fn_ty, None)
|
||||
});
|
||||
let tid = i64t.const_int(type_id as u64, true);
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_call(callee, &[tid.into()], "birth"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
let h_i64 = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("birth_h returned void".to_string())?
|
||||
.into_int_value();
|
||||
let pty = codegen.context.ptr_type(AddressSpace::from(0));
|
||||
let ptr = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_to_ptr(h_i64, pty, "handle_to_ptr"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(dst, ptr.into());
|
||||
Ok(())
|
||||
} else {
|
||||
// Fallback: call i64 @nyash.env.box.new(i8*) with type name
|
||||
let i8p = codegen.context.ptr_type(AddressSpace::from(0));
|
||||
let fn_ty = i64t.fn_type(&[i8p.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.env.box.new")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.env.box.new", fn_ty, None)
|
||||
});
|
||||
let tn = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_global_string_ptr(box_type, "box_type_name")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[tn.as_pointer_value().into()], "env_box_new")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let h_i64 = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("env.box.new returned void".to_string())?
|
||||
.into_int_value();
|
||||
let pty = codegen.context.ptr_type(AddressSpace::from(0));
|
||||
let ptr = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_to_ptr(h_i64, pty, "handle_to_ptr"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(dst, ptr.into());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,172 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use inkwell::values::PointerValue;
|
||||
use inkwell::values::{BasicValueEnum as BVE, IntValue};
|
||||
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{BasicBlockId, ValueId};
|
||||
|
||||
use super::builder_cursor::BuilderCursor;
|
||||
use super::flow::localize_to_i64;
|
||||
|
||||
/// Minimal per-function resolver caches. Caches localized i64 values per (block,value) to avoid
|
||||
/// redundant PHIs and casts when multiple users in the same block request the same MIR value.
|
||||
pub struct Resolver<'ctx> {
|
||||
i64_locals: HashMap<(BasicBlockId, ValueId), IntValue<'ctx>>,
|
||||
ptr_locals: HashMap<(BasicBlockId, ValueId), PointerValue<'ctx>>,
|
||||
f64_locals: HashMap<(BasicBlockId, ValueId), inkwell::values::FloatValue<'ctx>>,
|
||||
}
|
||||
|
||||
impl<'ctx> Resolver<'ctx> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
i64_locals: HashMap::new(),
|
||||
ptr_locals: HashMap::new(),
|
||||
f64_locals: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve a MIR value as an i64 dominating the current block.
|
||||
/// Strategy: if present in cache, return it; otherwise localize via sealed snapshots and cache.
|
||||
pub fn resolve_i64<'b>(
|
||||
&mut self,
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
cur_bid: BasicBlockId,
|
||||
vid: ValueId,
|
||||
bb_map: &std::collections::HashMap<BasicBlockId, inkwell::basic_block::BasicBlock<'ctx>>,
|
||||
preds: &std::collections::HashMap<BasicBlockId, Vec<BasicBlockId>>,
|
||||
block_end_values: &std::collections::HashMap<
|
||||
BasicBlockId,
|
||||
std::collections::HashMap<ValueId, BVE<'ctx>>,
|
||||
>,
|
||||
vmap: &std::collections::HashMap<ValueId, BVE<'ctx>>,
|
||||
) -> Result<IntValue<'ctx>, String> {
|
||||
if let Some(iv) = self.i64_locals.get(&(cur_bid, vid)).copied() {
|
||||
return Ok(iv);
|
||||
}
|
||||
let iv = localize_to_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
vid,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
self.i64_locals.insert((cur_bid, vid), iv);
|
||||
Ok(iv)
|
||||
}
|
||||
|
||||
/// Resolve a MIR value as an i8* pointer dominating the current block.
|
||||
pub fn resolve_ptr<'b>(
|
||||
&mut self,
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
cur_bid: BasicBlockId,
|
||||
vid: ValueId,
|
||||
bb_map: &std::collections::HashMap<BasicBlockId, inkwell::basic_block::BasicBlock<'ctx>>,
|
||||
preds: &std::collections::HashMap<BasicBlockId, Vec<BasicBlockId>>,
|
||||
block_end_values: &std::collections::HashMap<
|
||||
BasicBlockId,
|
||||
std::collections::HashMap<ValueId, BVE<'ctx>>,
|
||||
>,
|
||||
vmap: &std::collections::HashMap<ValueId, BVE<'ctx>>,
|
||||
) -> Result<PointerValue<'ctx>, String> {
|
||||
if let Some(pv) = self.ptr_locals.get(&(cur_bid, vid)).copied() {
|
||||
return Ok(pv);
|
||||
}
|
||||
// Avoid using current vmap directly to keep dominance safe under multiple predecessors.
|
||||
// Strategy: localize as i64 (dominance-safe PHI), then convert to i8* in current block.
|
||||
let i8p = codegen.context.ptr_type(inkwell::AddressSpace::from(0));
|
||||
let iv = localize_to_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
vid,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let pv = cursor
|
||||
.emit_instr(cur_bid, |b| b.build_int_to_ptr(iv, i8p, "loc_i2p_dom"))
|
||||
.map_err(|e| e.to_string())?;
|
||||
self.ptr_locals.insert((cur_bid, vid), pv);
|
||||
Ok(pv)
|
||||
}
|
||||
|
||||
/// Resolve a MIR value as an f64 dominating the current block.
|
||||
pub fn resolve_f64<'b>(
|
||||
&mut self,
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
cur_bid: BasicBlockId,
|
||||
vid: ValueId,
|
||||
bb_map: &std::collections::HashMap<BasicBlockId, inkwell::basic_block::BasicBlock<'ctx>>,
|
||||
preds: &std::collections::HashMap<BasicBlockId, Vec<BasicBlockId>>,
|
||||
block_end_values: &std::collections::HashMap<
|
||||
BasicBlockId,
|
||||
std::collections::HashMap<ValueId, BVE<'ctx>>,
|
||||
>,
|
||||
vmap: &std::collections::HashMap<ValueId, BVE<'ctx>>,
|
||||
) -> Result<inkwell::values::FloatValue<'ctx>, String> {
|
||||
if let Some(fv) = self.f64_locals.get(&(cur_bid, vid)).copied() {
|
||||
return Ok(fv);
|
||||
}
|
||||
// Avoid using current vmap directly to keep dominance safe under multiple predecessors.
|
||||
let f64t = codegen.context.f64_type();
|
||||
let cur_llbb = *bb_map.get(&cur_bid).ok_or("cur bb missing")?;
|
||||
let pred_list = preds.get(&cur_bid).cloned().unwrap_or_default();
|
||||
let saved_ip = codegen.builder.get_insert_block();
|
||||
if let Some(first) = cur_llbb.get_first_instruction() {
|
||||
codegen.builder.position_before(&first);
|
||||
} else {
|
||||
codegen.builder.position_at_end(cur_llbb);
|
||||
}
|
||||
let phi = codegen
|
||||
.builder
|
||||
.build_phi(f64t, &format!("loc_f64_{}", vid.as_u32()))
|
||||
.map_err(|e| e.to_string())?;
|
||||
if pred_list.is_empty() {
|
||||
// No predecessor: conservatively zero(vmap には依存しない)
|
||||
let z = f64t.const_zero();
|
||||
phi.add_incoming(&[(&z, cur_llbb)]);
|
||||
} else {
|
||||
for p in &pred_list {
|
||||
let pred_bb = *bb_map.get(p).ok_or("pred bb missing")?;
|
||||
let base = block_end_values
|
||||
.get(p)
|
||||
.and_then(|m| m.get(&vid).copied())
|
||||
.unwrap_or_else(|| f64t.const_zero().into());
|
||||
let mut coerced = f64t.const_zero();
|
||||
cursor.with_block(*p, pred_bb, |c| {
|
||||
let term = unsafe { pred_bb.get_terminator() };
|
||||
if let Some(t) = term {
|
||||
codegen.builder.position_before(&t);
|
||||
} else {
|
||||
c.position_at_end(pred_bb);
|
||||
}
|
||||
coerced = match base {
|
||||
BVE::FloatValue(fv) => fv,
|
||||
BVE::IntValue(iv) => codegen
|
||||
.builder
|
||||
.build_signed_int_to_float(iv, f64t, "loc_i2f_p")
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap(),
|
||||
BVE::PointerValue(_) => f64t.const_zero(),
|
||||
_ => f64t.const_zero(),
|
||||
};
|
||||
});
|
||||
phi.add_incoming(&[(&coerced, pred_bb)]);
|
||||
}
|
||||
}
|
||||
if let Some(bb) = saved_ip {
|
||||
codegen.builder.position_at_end(bb);
|
||||
}
|
||||
let out = phi.as_basic_value().into_float_value();
|
||||
self.f64_locals.insert((cur_bid, vid), out);
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
/*!
|
||||
* Select & Condition helpers (scaffolding)
|
||||
*
|
||||
* Placeholder for condition normalization / short-circuit pre-processing
|
||||
* to keep `function.rs` focused on structure. Implementations will be
|
||||
* added incrementally; for now, this module is documentation-only.
|
||||
*/
|
||||
|
||||
use crate::mir::{function::MirFunction, ValueId};
|
||||
use crate::mir::MirType;
|
||||
use super::super::types; // access mapping helpers if needed later
|
||||
|
||||
/// Normalize a branch condition if needed (scaffolding).
|
||||
/// Currently returns the input unchanged; provides a single place
|
||||
/// to adjust semantics later (e.g., truthy rules, short-circuit pre-pass).
|
||||
pub(crate) fn normalize_branch_condition(func: &MirFunction, cond: &ValueId) -> ValueId {
|
||||
// Minimal truthy normalization hook.
|
||||
// Strategy (no new instructions here):
|
||||
// - If we have a recorded type for `cond` and it is a boolean-like i1/i64 (0/1), return as-is.
|
||||
// - Otherwise, return the original cond and let flow/emit handle `!= 0` lowering as today.
|
||||
if let Some(ty) = func.metadata.value_types.get(cond) {
|
||||
match ty {
|
||||
MirType::I1 | MirType::I64 | MirType::Bool => { return *cond; }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
*cond
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
use inkwell::values::{IntValue, PointerValue};
|
||||
|
||||
/// Lightweight newtypes for string representations used in lowering.
|
||||
/// StrHandle crosses basic blocks; StrPtr is created at call sites within the same block.
|
||||
pub struct StrHandle<'ctx>(pub IntValue<'ctx>);
|
||||
pub struct StrPtr<'ctx>(pub PointerValue<'ctx>);
|
||||
|
||||
impl<'ctx> StrHandle<'ctx> {
|
||||
#[inline]
|
||||
pub fn as_i64(&self) -> IntValue<'ctx> {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ctx> From<PointerValue<'ctx>> for StrPtr<'ctx> {
|
||||
fn from(p: PointerValue<'ctx>) -> Self {
|
||||
Self(p)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,418 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use inkwell::{values::BasicValueEnum as BVE, AddressSpace};
|
||||
|
||||
use super::builder_cursor::BuilderCursor;
|
||||
use super::Resolver;
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::{function::MirFunction, BasicBlockId, ValueId};
|
||||
|
||||
/// Handle String-specific methods. Returns true if handled, false to let caller continue.
|
||||
pub(super) fn try_handle_string_method<'ctx, 'b>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
cursor: &mut BuilderCursor<'ctx, 'b>,
|
||||
resolver: &mut Resolver<'ctx>,
|
||||
cur_bid: BasicBlockId,
|
||||
func: &MirFunction,
|
||||
vmap: &mut HashMap<ValueId, inkwell::values::BasicValueEnum<'ctx>>,
|
||||
dst: &Option<ValueId>,
|
||||
box_val: &ValueId,
|
||||
method: &str,
|
||||
args: &[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<bool, String> {
|
||||
// Receiver annotation check (kept for future diagnostics)
|
||||
let _is_string_recv = match func.metadata.value_types.get(box_val) {
|
||||
Some(crate::mir::MirType::String) => true,
|
||||
Some(crate::mir::MirType::Box(b)) if b == "StringBox" => true,
|
||||
_ => false,
|
||||
};
|
||||
// Do not early-return; method-specific checksで型検証を行う
|
||||
|
||||
// concat fast-paths
|
||||
if method == "concat" {
|
||||
if args.len() != 1 {
|
||||
return Err("String.concat expects 1 arg".to_string());
|
||||
}
|
||||
let i8p = codegen.context.ptr_type(AddressSpace::from(0));
|
||||
// Resolve rhs as either pointer (string) or i64 (handle/int)
|
||||
let rhs_val = match func.metadata.value_types.get(&args[0]) {
|
||||
Some(crate::mir::MirType::String) => {
|
||||
let p = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
BVE::PointerValue(p)
|
||||
}
|
||||
_ => {
|
||||
// Default to integer form for non-String metadata
|
||||
let iv = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
BVE::IntValue(iv)
|
||||
}
|
||||
};
|
||||
let lp = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*box_val,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
match (BVE::PointerValue(lp), rhs_val) {
|
||||
(BVE::PointerValue(lp), BVE::PointerValue(rp)) => {
|
||||
let fnty = i8p.fn_type(&[i8p.into(), i8p.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.string.concat_ss")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.string.concat_ss", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[lp.into(), rp.into()], "concat_ss_call")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("concat_ss returned void".to_string())?;
|
||||
// return as handle (i64) across blocks
|
||||
let i64t = codegen.context.i64_type();
|
||||
let h = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_ptr_to_int(rv.into_pointer_value(), i64t, "str_ptr2i")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(*d, h.into());
|
||||
}
|
||||
return Ok(true);
|
||||
}
|
||||
(BVE::PointerValue(lp), BVE::IntValue(_ri)) => {
|
||||
let i64t = codegen.context.i64_type();
|
||||
// Localize rhs integer in current block via Resolver
|
||||
let ri = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i8p.fn_type(&[i8p.into(), i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.string.concat_si")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.string.concat_si", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[lp.into(), ri.into()], "concat_si_call")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("concat_si returned void".to_string())?;
|
||||
let i64t = codegen.context.i64_type();
|
||||
let h = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_ptr_to_int(rv.into_pointer_value(), i64t, "str_ptr2i")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(*d, h.into());
|
||||
}
|
||||
return Ok(true);
|
||||
}
|
||||
(BVE::PointerValue(_li_as_p), BVE::PointerValue(rp)) => {
|
||||
let i64t = codegen.context.i64_type();
|
||||
// Localize receiver integer in current block (box_val)
|
||||
let li = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*box_val,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i8p.fn_type(&[i64t.into(), i8p.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.string.concat_is")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.string.concat_is", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[li.into(), rp.into()], "concat_is_call")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("concat_is returned void".to_string())?;
|
||||
let i64t = codegen.context.i64_type();
|
||||
let h = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_ptr_to_int(rv.into_pointer_value(), i64t, "str_ptr2i")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(*d, h.into());
|
||||
}
|
||||
return Ok(true);
|
||||
}
|
||||
_ => { /* fall through */ }
|
||||
}
|
||||
}
|
||||
|
||||
// length/len fast-path
|
||||
if method == "length" || method == "len" {
|
||||
let i64t = codegen.context.i64_type();
|
||||
// Ensure handle for receiver (i8* -> i64 via from_i8_string)
|
||||
let recv_h = {
|
||||
// Prefer i64 handle from resolver; if metadata says String but actual is i8*, box it
|
||||
if let Some(crate::mir::MirType::String) = func.metadata.value_types.get(box_val) {
|
||||
// Receiver is a String: resolve pointer then box to i64
|
||||
let p = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*box_val,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i64t.fn_type(
|
||||
&[codegen.context.ptr_type(AddressSpace::from(0)).into()],
|
||||
false,
|
||||
);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.box.from_i8_string")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.box.from_i8_string", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[p.into()], "str_ptr_to_handle")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("from_i8_string returned void".to_string())?;
|
||||
if let BVE::IntValue(iv) = rv {
|
||||
iv
|
||||
} else {
|
||||
return Err("from_i8_string ret expected i64".to_string());
|
||||
}
|
||||
} else {
|
||||
resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*box_val,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?
|
||||
}
|
||||
};
|
||||
// call i64 @nyash.string.len_h(i64)
|
||||
let fnty = i64t.fn_type(&[i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.string.len_h")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.string.len_h", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(callee, &[recv_h.into()], "strlen_h")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("len_h returned void".to_string())?;
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
// substring(start, end) -> i8*
|
||||
if method == "substring" {
|
||||
if args.len() != 2 {
|
||||
return Err("String.substring expects 2 args (start, end)".to_string());
|
||||
}
|
||||
let i64t = codegen.context.i64_type();
|
||||
let i8p = codegen.context.ptr_type(AddressSpace::from(0));
|
||||
// receiver pointer via Resolver
|
||||
let recv_p = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*box_val,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
// Localize start/end indices to current block via sealed snapshots (i64)
|
||||
let s = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let e = resolver.resolve_i64(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[1],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i8p.fn_type(&[i8p.into(), i64t.into(), i64t.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.string.substring_sii")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.string.substring_sii", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(
|
||||
callee,
|
||||
&[recv_p.into(), s.into(), e.into()],
|
||||
"substring_call",
|
||||
)
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("substring returned void".to_string())?;
|
||||
let i64t = codegen.context.i64_type();
|
||||
let h = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_ptr_to_int(rv.into_pointer_value(), i64t, "str_ptr2i_sub")
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
vmap.insert(*d, h.into());
|
||||
}
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
// lastIndexOf(needle) -> i64
|
||||
if method == "lastIndexOf" {
|
||||
if args.len() != 1 {
|
||||
return Err("String.lastIndexOf expects 1 arg".to_string());
|
||||
}
|
||||
let i64t = codegen.context.i64_type();
|
||||
let i8p = codegen.context.ptr_type(AddressSpace::from(0));
|
||||
// receiver pointer via Resolver (String fast path)
|
||||
let recv_p = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
*box_val,
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let needle_p = resolver.resolve_ptr(
|
||||
codegen,
|
||||
cursor,
|
||||
cur_bid,
|
||||
args[0],
|
||||
bb_map,
|
||||
preds,
|
||||
block_end_values,
|
||||
vmap,
|
||||
)?;
|
||||
let fnty = i64t.fn_type(&[i8p.into(), i8p.into()], false);
|
||||
let callee = codegen
|
||||
.module
|
||||
.get_function("nyash.string.lastIndexOf_ss")
|
||||
.unwrap_or_else(|| {
|
||||
codegen
|
||||
.module
|
||||
.add_function("nyash.string.lastIndexOf_ss", fnty, None)
|
||||
});
|
||||
let call = cursor
|
||||
.emit_instr(cur_bid, |b| {
|
||||
b.build_call(
|
||||
callee,
|
||||
&[recv_p.into(), needle_p.into()],
|
||||
"lastindexof_call",
|
||||
)
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(d) = dst {
|
||||
let rv = call
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.ok_or("lastIndexOf returned void".to_string())?;
|
||||
vmap.insert(*d, rv);
|
||||
}
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
Ok(false)
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
/*!
|
||||
* Terminators (scaffolding)
|
||||
*
|
||||
* Thin re-exports of flow-level terminators. Call sites can gradually
|
||||
* migrate to `terminators::*` without changing behavior.
|
||||
*/
|
||||
|
||||
pub use super::flow::{emit_branch, emit_jump, emit_return};
|
||||
|
||||
122
archive/rust-llvm-backend/llvm/compiler/codegen/mod.rs
Normal file
122
archive/rust-llvm-backend/llvm/compiler/codegen/mod.rs
Normal file
@ -0,0 +1,122 @@
|
||||
//! LLVM Codegen Orchestrator
|
||||
//!
|
||||
//! Structure
|
||||
//! - `function.rs`: per-function lowering (MIR → LLVM IR)
|
||||
//! - `utils.rs`: helpers like `sanitize_symbol` and const-string maps
|
||||
//! - `instructions/*`: focused lowerers and terminators (branch/jump/return, calls, boxcall, externcall)
|
||||
//! - `types.rs`: MIR→LLVM type mapping and classifiers
|
||||
//!
|
||||
//! Keep this file slim: predeclare functions, delegate lowering, emit entry wrapper/object.
|
||||
use super::helpers::map_type;
|
||||
use super::LLVMCompiler;
|
||||
pub(super) use crate::backend::llvm::context::CodegenContext;
|
||||
use crate::mir::function::MirModule;
|
||||
pub(super) use crate::mir::instruction::{ConstValue, MirInstruction, UnaryOp};
|
||||
pub(super) use crate::mir::ValueId;
|
||||
use inkwell::context::Context;
|
||||
pub(super) use inkwell::{
|
||||
types::{BasicTypeEnum, FloatType, IntType, PointerType},
|
||||
values::{BasicValueEnum, FloatValue, FunctionValue, IntValue, PhiValue, PointerValue},
|
||||
AddressSpace,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
mod types;
|
||||
pub(super) use self::types::{
|
||||
classify_tag, cmp_eq_ne_any, i64_to_ptr, map_mirtype_to_basic, to_bool, to_i64_any,
|
||||
};
|
||||
mod function;
|
||||
mod instructions;
|
||||
mod object;
|
||||
mod utils;
|
||||
|
||||
fn sanitize_symbol(name: &str) -> String {
|
||||
utils::sanitize_symbol(name)
|
||||
}
|
||||
|
||||
impl LLVMCompiler {
|
||||
pub fn new() -> Result<Self, String> {
|
||||
Ok(Self {
|
||||
values: HashMap::new(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn compile_module(&self, mir_module: &MirModule, output_path: &str) -> Result<(), String> {
|
||||
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
||||
eprintln!(
|
||||
"[LLVM] compile_module start: functions={}, out={}",
|
||||
mir_module.functions.len(),
|
||||
output_path
|
||||
);
|
||||
}
|
||||
let context = Context::create();
|
||||
let codegen = CodegenContext::new(&context, "nyash_module")?;
|
||||
let box_type_ids = crate::backend::llvm::box_types::load_box_type_ids();
|
||||
|
||||
// Find entry function
|
||||
let (entry_name, _entry_func_ref) = if let Some((n, f)) = mir_module
|
||||
.functions
|
||||
.iter()
|
||||
.find(|(_n, f)| f.metadata.is_entry_point)
|
||||
{
|
||||
(n.clone(), f)
|
||||
} else if let Some(f) = mir_module.functions.get("Main.main") {
|
||||
("Main.main".to_string(), f)
|
||||
} else if let Some(f) = mir_module.functions.get("main") {
|
||||
("main".to_string(), f)
|
||||
} else if let Some((n, f)) = mir_module.functions.iter().next() {
|
||||
(n.clone(), f)
|
||||
} else {
|
||||
return Err("Main.main function not found in module".to_string());
|
||||
};
|
||||
|
||||
// Predeclare all MIR functions as LLVM functions
|
||||
let mut llvm_funcs: HashMap<String, FunctionValue> = HashMap::new();
|
||||
for (name, f) in &mir_module.functions {
|
||||
let ret_bt = match f.signature.return_type {
|
||||
crate::mir::MirType::Void => codegen.context.i64_type().into(),
|
||||
ref t => map_type(codegen.context, t)?,
|
||||
};
|
||||
let mut params_bt: Vec<BasicTypeEnum> = Vec::new();
|
||||
for pt in &f.signature.params {
|
||||
params_bt.push(map_type(codegen.context, pt)?);
|
||||
}
|
||||
let param_vals: Vec<_> = params_bt.iter().map(|t| (*t).into()).collect();
|
||||
let ll_fn_ty = match ret_bt {
|
||||
BasicTypeEnum::IntType(t) => t.fn_type(¶m_vals, false),
|
||||
BasicTypeEnum::FloatType(t) => t.fn_type(¶m_vals, false),
|
||||
BasicTypeEnum::PointerType(t) => t.fn_type(¶m_vals, false),
|
||||
_ => return Err("Unsupported return basic type".to_string()),
|
||||
};
|
||||
let sym = format!("ny_f_{}", sanitize_symbol(name));
|
||||
let lf = codegen.module.add_function(&sym, ll_fn_ty, None);
|
||||
llvm_funcs.insert(name.clone(), lf);
|
||||
}
|
||||
|
||||
// Lower all functions
|
||||
for (name, func) in &mir_module.functions {
|
||||
let llvm_func = *llvm_funcs.get(name).ok_or("predecl not found")?;
|
||||
function::lower_one_function(
|
||||
&codegen,
|
||||
llvm_func,
|
||||
func,
|
||||
name,
|
||||
&box_type_ids,
|
||||
&llvm_funcs,
|
||||
)?;
|
||||
}
|
||||
|
||||
// Build entry wrapper and emit object
|
||||
object::emit_wrapper_and_object(&codegen, &entry_name, output_path)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_compiler_creation() {
|
||||
let compiler = LLVMCompiler::new();
|
||||
assert!(compiler.is_ok());
|
||||
}
|
||||
}
|
||||
125
archive/rust-llvm-backend/llvm/compiler/codegen/object.rs
Normal file
125
archive/rust-llvm-backend/llvm/compiler/codegen/object.rs
Normal file
@ -0,0 +1,125 @@
|
||||
use super::sanitize_symbol;
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
use inkwell::values::BasicValueEnum;
|
||||
|
||||
pub(super) fn emit_wrapper_and_object<'ctx>(
|
||||
codegen: &CodegenContext<'ctx>,
|
||||
entry_name: &str,
|
||||
output_path: &str,
|
||||
) -> Result<(), String> {
|
||||
let i64t = codegen.context.i64_type();
|
||||
let ny_main_ty = i64t.fn_type(&[], false);
|
||||
let ny_main = codegen.module.add_function("ny_main", ny_main_ty, None);
|
||||
let entry_bb = codegen.context.append_basic_block(ny_main, "entry");
|
||||
codegen.builder.position_at_end(entry_bb);
|
||||
let entry_sym = format!("ny_f_{}", sanitize_symbol(entry_name));
|
||||
let entry_fn = codegen
|
||||
.module
|
||||
.get_function(&entry_sym)
|
||||
.ok_or_else(|| format!("entry function symbol not found: {}", entry_sym))?;
|
||||
let call = codegen
|
||||
.builder
|
||||
.build_call(entry_fn, &[], "call_main")
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rv = call.try_as_basic_value().left();
|
||||
let ret_v = if let Some(v) = rv {
|
||||
match v {
|
||||
BasicValueEnum::IntValue(iv) => {
|
||||
if iv.get_type().get_bit_width() == 64 {
|
||||
iv
|
||||
} else {
|
||||
codegen
|
||||
.builder
|
||||
.build_int_z_extend(iv, i64t, "ret_zext")
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
}
|
||||
BasicValueEnum::PointerValue(pv) => codegen
|
||||
.builder
|
||||
.build_ptr_to_int(pv, i64t, "ret_p2i")
|
||||
.map_err(|e| e.to_string())?,
|
||||
BasicValueEnum::FloatValue(fv) => codegen
|
||||
.builder
|
||||
.build_float_to_signed_int(fv, i64t, "ret_f2i")
|
||||
.map_err(|e| e.to_string())?,
|
||||
_ => i64t.const_zero(),
|
||||
}
|
||||
} else {
|
||||
i64t.const_zero()
|
||||
};
|
||||
codegen
|
||||
.builder
|
||||
.build_return(Some(&ret_v))
|
||||
.map_err(|e| e.to_string())?;
|
||||
if !ny_main.verify(true) {
|
||||
return Err("ny_main verification failed".to_string());
|
||||
}
|
||||
let verbose = std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1");
|
||||
if verbose {
|
||||
eprintln!("[LLVM] emitting object to {} (begin)", output_path);
|
||||
}
|
||||
match codegen.target_machine.write_to_file(
|
||||
&codegen.module,
|
||||
inkwell::targets::FileType::Object,
|
||||
std::path::Path::new(output_path),
|
||||
) {
|
||||
Ok(_) => {
|
||||
if std::fs::metadata(output_path).is_err() {
|
||||
let buf = codegen
|
||||
.target_machine
|
||||
.write_to_memory_buffer(&codegen.module, inkwell::targets::FileType::Object)
|
||||
.map_err(|e| format!("Failed to get object buffer: {}", e))?;
|
||||
std::fs::write(output_path, buf.as_slice())
|
||||
.map_err(|e| format!("Failed to write object to '{}': {}", output_path, e))?;
|
||||
if verbose {
|
||||
eprintln!(
|
||||
"[LLVM] wrote object via memory buffer fallback: {} ({} bytes)",
|
||||
output_path,
|
||||
buf.get_size()
|
||||
);
|
||||
}
|
||||
} else if verbose {
|
||||
if let Ok(meta) = std::fs::metadata(output_path) {
|
||||
eprintln!(
|
||||
"[LLVM] wrote object via file API: {} ({} bytes)",
|
||||
output_path,
|
||||
meta.len()
|
||||
);
|
||||
}
|
||||
}
|
||||
if verbose {
|
||||
eprintln!("[LLVM] emit complete (Ok branch) for {}", output_path);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
let buf = codegen
|
||||
.target_machine
|
||||
.write_to_memory_buffer(&codegen.module, inkwell::targets::FileType::Object)
|
||||
.map_err(|ee| {
|
||||
format!(
|
||||
"Failed to write object ({}); and memory buffer failed: {}",
|
||||
e, ee
|
||||
)
|
||||
})?;
|
||||
std::fs::write(output_path, buf.as_slice()).map_err(|ee| {
|
||||
format!(
|
||||
"Failed to write object to '{}': {} (original error: {})",
|
||||
output_path, ee, e
|
||||
)
|
||||
})?;
|
||||
if verbose {
|
||||
eprintln!(
|
||||
"[LLVM] wrote object via error fallback: {} ({} bytes)",
|
||||
output_path,
|
||||
buf.get_size()
|
||||
);
|
||||
eprintln!(
|
||||
"[LLVM] emit complete (Err branch handled) for {}",
|
||||
output_path
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
187
archive/rust-llvm-backend/llvm/compiler/codegen/types.rs
Normal file
187
archive/rust-llvm-backend/llvm/compiler/codegen/types.rs
Normal file
@ -0,0 +1,187 @@
|
||||
use inkwell::context::Context;
|
||||
use inkwell::{
|
||||
types::BasicTypeEnum,
|
||||
values::{BasicValueEnum, FloatValue, IntValue, PhiValue, PointerValue},
|
||||
AddressSpace,
|
||||
};
|
||||
|
||||
use crate::mir;
|
||||
|
||||
use super::super::helpers::{as_float, as_int};
|
||||
|
||||
// Helper conversions and classifiers extracted from the monolithic codegen.
|
||||
// Kept signature-compatible to minimize churn in compile_module.
|
||||
|
||||
pub(super) fn to_i64_any<'ctx>(
|
||||
ctx: &'ctx Context,
|
||||
builder: &inkwell::builder::Builder<'ctx>,
|
||||
v: BasicValueEnum<'ctx>,
|
||||
) -> Result<IntValue<'ctx>, String> {
|
||||
let i64t = ctx.i64_type();
|
||||
Ok(match v {
|
||||
BasicValueEnum::IntValue(iv) => {
|
||||
if iv.get_type().get_bit_width() == 64 {
|
||||
iv
|
||||
} else if iv.get_type().get_bit_width() < 64 {
|
||||
builder
|
||||
.build_int_z_extend(iv, i64t, "zext_i64")
|
||||
.map_err(|e| e.to_string())?
|
||||
} else {
|
||||
builder
|
||||
.build_int_truncate(iv, i64t, "trunc_i64")
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
}
|
||||
BasicValueEnum::PointerValue(pv) => builder
|
||||
.build_ptr_to_int(pv, i64t, "p2i64")
|
||||
.map_err(|e| e.to_string())?,
|
||||
BasicValueEnum::FloatValue(fv) => {
|
||||
// Bitcast f64 -> i64 via stack slot
|
||||
let slot_builder = builder
|
||||
.get_insert_block()
|
||||
.and_then(|bb| bb.get_parent())
|
||||
.and_then(|f| f.get_first_basic_block())
|
||||
.map(|entry| {
|
||||
let eb = ctx.create_builder();
|
||||
eb.position_at_end(entry);
|
||||
eb
|
||||
})
|
||||
.unwrap_or_else(|| ctx.create_builder());
|
||||
let tmp = slot_builder
|
||||
.build_alloca(i64t, "f2i_tmp")
|
||||
.map_err(|e| e.to_string())?;
|
||||
let fptr_ty = ctx.ptr_type(AddressSpace::from(0));
|
||||
let castp = builder
|
||||
.build_pointer_cast(tmp, fptr_ty, "i64p_to_f64p")
|
||||
.map_err(|e| e.to_string())?;
|
||||
builder.build_store(castp, fv).map_err(|e| e.to_string())?;
|
||||
builder
|
||||
.build_load(i64t, tmp, "ld_f2i")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into_int_value()
|
||||
}
|
||||
_ => return Err("unsupported value for i64 conversion".to_string()),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn i64_to_ptr<'ctx>(
|
||||
ctx: &'ctx Context,
|
||||
builder: &inkwell::builder::Builder<'ctx>,
|
||||
iv: IntValue<'ctx>,
|
||||
) -> Result<PointerValue<'ctx>, String> {
|
||||
let pty = ctx.ptr_type(AddressSpace::from(0));
|
||||
builder
|
||||
.build_int_to_ptr(iv, pty, "i64_to_ptr")
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
pub(super) fn classify_tag<'ctx>(v: BasicValueEnum<'ctx>) -> i64 {
|
||||
match v {
|
||||
BasicValueEnum::FloatValue(_) => 5, // float
|
||||
BasicValueEnum::PointerValue(_) => 8, // handle/ptr
|
||||
BasicValueEnum::IntValue(_) => 3, // integer/bool
|
||||
_ => 3,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn to_bool<'ctx>(
|
||||
ctx: &'ctx Context,
|
||||
b: BasicValueEnum<'ctx>,
|
||||
builder: &inkwell::builder::Builder<'ctx>,
|
||||
) -> Result<IntValue<'ctx>, String> {
|
||||
if let Some(bb) = as_int(b) {
|
||||
// If not i1, compare != 0
|
||||
if bb.get_type().get_bit_width() == 1 {
|
||||
Ok(bb)
|
||||
} else {
|
||||
Ok(builder
|
||||
.build_int_compare(
|
||||
inkwell::IntPredicate::NE,
|
||||
bb,
|
||||
bb.get_type().const_zero(),
|
||||
"tobool",
|
||||
)
|
||||
.map_err(|e| e.to_string())?)
|
||||
}
|
||||
} else if let Some(fv) = as_float(b) {
|
||||
let zero = fv.get_type().const_float(0.0);
|
||||
Ok(builder
|
||||
.build_float_compare(inkwell::FloatPredicate::ONE, fv, zero, "toboolf")
|
||||
.map_err(|e| e.to_string())?)
|
||||
} else if let BasicValueEnum::PointerValue(pv) = b {
|
||||
let i64t = ctx.i64_type();
|
||||
let p2i = builder
|
||||
.build_ptr_to_int(pv, i64t, "p2i")
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(builder
|
||||
.build_int_compare(inkwell::IntPredicate::NE, p2i, i64t.const_zero(), "toboolp")
|
||||
.map_err(|e| e.to_string())?)
|
||||
} else {
|
||||
Err("Unsupported value for boolean conversion".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn cmp_eq_ne_any<'ctx>(
|
||||
ctx: &'ctx Context,
|
||||
builder: &inkwell::builder::Builder<'ctx>,
|
||||
op: &crate::mir::CompareOp,
|
||||
lv: BasicValueEnum<'ctx>,
|
||||
rv: BasicValueEnum<'ctx>,
|
||||
) -> Result<BasicValueEnum<'ctx>, String> {
|
||||
use crate::mir::CompareOp as C;
|
||||
match (lv, rv) {
|
||||
(BasicValueEnum::IntValue(li), BasicValueEnum::IntValue(ri)) => {
|
||||
let pred = if matches!(op, C::Eq) {
|
||||
inkwell::IntPredicate::EQ
|
||||
} else {
|
||||
inkwell::IntPredicate::NE
|
||||
};
|
||||
Ok(builder
|
||||
.build_int_compare(pred, li, ri, "icmp")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into())
|
||||
}
|
||||
(BasicValueEnum::FloatValue(lf), BasicValueEnum::FloatValue(rf)) => {
|
||||
let pred = if matches!(op, C::Eq) {
|
||||
inkwell::FloatPredicate::OEQ
|
||||
} else {
|
||||
inkwell::FloatPredicate::ONE
|
||||
};
|
||||
Ok(builder
|
||||
.build_float_compare(pred, lf, rf, "fcmp")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into())
|
||||
}
|
||||
(BasicValueEnum::PointerValue(_), _) | (_, BasicValueEnum::PointerValue(_)) => {
|
||||
let li = to_i64_any(ctx, builder, lv)?;
|
||||
let ri = to_i64_any(ctx, builder, rv)?;
|
||||
let pred = if matches!(op, C::Eq) {
|
||||
inkwell::IntPredicate::EQ
|
||||
} else {
|
||||
inkwell::IntPredicate::NE
|
||||
};
|
||||
Ok(builder
|
||||
.build_int_compare(pred, li, ri, "icmp_any")
|
||||
.map_err(|e| e.to_string())?
|
||||
.into())
|
||||
}
|
||||
_ => Err("unsupported compare types".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn map_mirtype_to_basic<'ctx>(
|
||||
ctx: &'ctx Context,
|
||||
t: &mir::MirType,
|
||||
) -> BasicTypeEnum<'ctx> {
|
||||
match t {
|
||||
mir::MirType::Integer => ctx.i64_type().into(),
|
||||
mir::MirType::Bool => ctx.bool_type().into(),
|
||||
mir::MirType::Float => ctx.f64_type().into(),
|
||||
mir::MirType::String => ctx.ptr_type(AddressSpace::from(0)).into(),
|
||||
mir::MirType::Box(_)
|
||||
| mir::MirType::Array(_)
|
||||
| mir::MirType::Future(_)
|
||||
| mir::MirType::Unknown => ctx.ptr_type(AddressSpace::from(0)).into(),
|
||||
mir::MirType::Void => ctx.i64_type().into(), // avoid void as a value type; default to i64
|
||||
}
|
||||
}
|
||||
39
archive/rust-llvm-backend/llvm/compiler/codegen/utils.rs
Normal file
39
archive/rust-llvm-backend/llvm/compiler/codegen/utils.rs
Normal file
@ -0,0 +1,39 @@
|
||||
use crate::mir::instruction::{ConstValue, MirInstruction};
|
||||
use crate::mir::ValueId;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub(super) fn sanitize_symbol(name: &str) -> String {
|
||||
name.chars()
|
||||
.map(|c| match c {
|
||||
'.' | '/' | '-' => '_',
|
||||
other => other,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(super) fn build_const_str_map(
|
||||
f: &crate::mir::function::MirFunction,
|
||||
) -> HashMap<ValueId, String> {
|
||||
let mut m = HashMap::new();
|
||||
for bid in f.block_ids() {
|
||||
if let Some(b) = f.blocks.get(&bid) {
|
||||
for inst in &b.instructions {
|
||||
if let MirInstruction::Const {
|
||||
dst,
|
||||
value: ConstValue::String(s),
|
||||
} = inst
|
||||
{
|
||||
m.insert(*dst, s.clone());
|
||||
}
|
||||
}
|
||||
if let Some(MirInstruction::Const {
|
||||
dst,
|
||||
value: ConstValue::String(s),
|
||||
}) = &b.terminator
|
||||
{
|
||||
m.insert(*dst, s.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
m
|
||||
}
|
||||
Reference in New Issue
Block a user