💢 The truth about Rust + LLVM development hell
ChatGPT5 struggling for 34+ minutes with Rust lifetime/build errors...
This perfectly illustrates why we need Phase 22 (Nyash LLVM compiler)\!
Key insights:
- 'Rust is safe and beautiful' - Gemini (who never fought lifetime errors)
- Reality: 500-line error messages, 34min debug sessions, lifetime hell
- C would just work: void* compile(void* mir) { done; }
- Python would work: 100 lines with llvmlite
- ANY language with C ABI would work\!
The frustration is real:
- We're SO CLOSE to Nyash self-hosting paradise
- Once bootstrapped, EVERYTHING can be written in Nyash
- No more Rust complexity, no more 5-7min builds
- Just simple, beautiful Box-based code
Current status:
- PHI/SSA hardening in progress (ChatGPT5)
- 'phi incoming value missing' in Main.esc_json/1
- Sealed SSA approach being implemented
The dream is near: Everything is Box, even the compiler\! 🌟
This commit is contained in:
@ -5,8 +5,10 @@ use inkwell::values::BasicValueEnum as BVE;
|
||||
|
||||
use crate::backend::llvm::context::CodegenContext;
|
||||
mod fields;
|
||||
mod invoke;
|
||||
pub(crate) mod invoke;
|
||||
mod marshal;
|
||||
use self::marshal as marshal_mod;
|
||||
use self::invoke as invoke_mod;
|
||||
use crate::mir::{function::MirFunction, ValueId};
|
||||
|
||||
// BoxCall lowering (large): mirrors existing logic; kept in one function for now
|
||||
@ -66,6 +68,11 @@ pub(in super::super) fn lower_boxcall<'ctx>(
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Console convenience: treat println as env.console.log
|
||||
if method == "println" {
|
||||
return super::externcall::lower_externcall(codegen, func, vmap, dst, &"env.console".to_string(), &"log".to_string(), args);
|
||||
}
|
||||
|
||||
// getField/setField
|
||||
if fields::try_handle_field_method(codegen, vmap, dst, method, args, recv_h)? {
|
||||
return Ok(());
|
||||
@ -106,6 +113,107 @@ pub(in super::super) fn lower_boxcall<'ctx>(
|
||||
)?;
|
||||
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) {
|
||||
let mut call_args: Vec<inkwell::values::BasicMetadataValueEnum> = Vec::with_capacity(args.len());
|
||||
for a in args {
|
||||
let v = *vmap.get(a).ok_or("boxcall func arg missing")?;
|
||||
call_args.push(v.into());
|
||||
}
|
||||
let call = codegen
|
||||
.builder
|
||||
.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)
|
||||
{
|
||||
use crate::backend::llvm::compiler::codegen::instructions::boxcall::marshal::get_i64 as get_i64_any;
|
||||
let i64t = codegen.context.i64_type();
|
||||
let argc = i64t.const_int(args.len() as u64, false);
|
||||
let mname = codegen
|
||||
.builder
|
||||
.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) { get_i64_any(codegen, vmap, *v0)? } else { i64t.const_zero() };
|
||||
let a2 = if let Some(v1) = args.get(1) { get_i64_any(codegen, vmap, *v1)? } 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 = codegen
|
||||
.builder
|
||||
.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 = codegen.builder.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 = codegen.builder.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 = codegen.builder.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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user