full refactor on linker and verifier to become more JVM-like
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
use crate::building::output::CompiledModule;
|
||||
use crate::building::plan::BuildStep;
|
||||
use prometeu_bytecode::opcode::OpCode;
|
||||
use prometeu_bytecode::layout;
|
||||
use prometeu_bytecode::{ConstantPoolEntry, DebugInfo};
|
||||
use std::collections::HashMap;
|
||||
use prometeu_abi::virtual_machine::{ProgramImage, Value};
|
||||
use prometeu_analysis::ids::ProjectId;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
pub enum LinkError {
|
||||
@@ -150,22 +152,44 @@ impl Linker {
|
||||
|
||||
// Patch imports
|
||||
for import in &module.imports {
|
||||
let dep_project_id = if import.key.dep_alias == "self" || import.key.dep_alias.is_empty() {
|
||||
&module.project_id
|
||||
// Resolve the dependency project id. If alias is missing/self, try all deps as fallback.
|
||||
let mut candidate_projects: Vec<&ProjectId> = Vec::new();
|
||||
if import.key.dep_alias == "self" || import.key.dep_alias.is_empty() {
|
||||
candidate_projects.push(&module.project_id);
|
||||
for (_alias, pid) in &step.deps { candidate_projects.push(pid); }
|
||||
} else {
|
||||
step.deps.get(&import.key.dep_alias)
|
||||
.ok_or_else(|| LinkError::UnresolvedSymbol(format!("Dependency alias '{}' not found in project {:?}", import.key.dep_alias, module.project_id)))?
|
||||
};
|
||||
let pid = step.deps.get(&import.key.dep_alias)
|
||||
.ok_or_else(|| LinkError::UnresolvedSymbol(format!("Dependency alias '{}' not found in project {:?}", import.key.dep_alias, module.project_id)))?;
|
||||
candidate_projects.push(pid);
|
||||
}
|
||||
|
||||
let symbol_id = (dep_project_id.clone(), import.key.module_path.clone(), import.key.symbol_name.clone());
|
||||
let &target_func_idx = global_symbols.get(&symbol_id)
|
||||
.ok_or_else(|| LinkError::UnresolvedSymbol(format!("DebugSymbol '{}:{}' not found in project {:?}", symbol_id.1, symbol_id.2, symbol_id.0)))?;
|
||||
let mut resolved_idx: Option<u32> = None;
|
||||
for pid in candidate_projects {
|
||||
let pid_val: ProjectId = (*pid).clone();
|
||||
let key = (pid_val, import.key.module_path.clone(), import.key.symbol_name.clone());
|
||||
if let Some(&idx) = global_symbols.get(&key) {
|
||||
resolved_idx = Some(idx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
let target_func_idx = resolved_idx.ok_or_else(|| {
|
||||
LinkError::UnresolvedSymbol(format!(
|
||||
"DebugSymbol '{}:{}' not found in any candidate project (self={:?}, deps={:?})",
|
||||
import.key.module_path,
|
||||
import.key.symbol_name,
|
||||
module.project_id,
|
||||
step.deps
|
||||
))
|
||||
})?;
|
||||
|
||||
for &reloc_pc in &import.relocation_pcs {
|
||||
// `reloc_pc` aponta para o INÍCIO do operando (após os 2 bytes do opcode),
|
||||
// conforme `assemble_with_unresolved` grava `pc` antes de escrever o U32.
|
||||
// Portanto, devemos escrever exatamente em `absolute_pc`.
|
||||
let absolute_pc = code_offset + reloc_pc as usize;
|
||||
let imm_offset = absolute_pc + 2;
|
||||
if imm_offset + 4 <= combined_code.len() {
|
||||
combined_code[imm_offset..imm_offset+4].copy_from_slice(&target_func_idx.to_le_bytes());
|
||||
if absolute_pc + 4 <= combined_code.len() {
|
||||
combined_code[absolute_pc..absolute_pc+4]
|
||||
.copy_from_slice(&target_func_idx.to_le_bytes());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -211,7 +235,9 @@ impl Linker {
|
||||
|
||||
// Check if this PC was already patched by an import.
|
||||
// If it wasn't, it's an internal call that needs relocation.
|
||||
let reloc_pc = (pos - 2 - code_offset) as u32;
|
||||
// `import.relocation_pcs` holds the PC at the start of the CALL immediate (after opcode),
|
||||
// and here `pos` currently points exactly at that immediate.
|
||||
let reloc_pc = (pos - code_offset) as u32;
|
||||
let is_import = module.imports.iter().any(|imp| imp.relocation_pcs.contains(&reloc_pc));
|
||||
|
||||
if !is_import {
|
||||
@@ -221,16 +247,13 @@ impl Linker {
|
||||
pos += 4;
|
||||
}
|
||||
}
|
||||
// Relocate control-flow jump targets by adding module code offset
|
||||
// Do NOT relocate intra-function control flow. Branch immediates are
|
||||
// function-relative by contract and must remain untouched by the linker.
|
||||
OpCode::Jmp | OpCode::JmpIfFalse | OpCode::JmpIfTrue => {
|
||||
if pos + 4 <= end {
|
||||
let code_off = module_code_offsets[i];
|
||||
patch_u32_at(&mut combined_code, pos, &|t| t.saturating_add(code_off));
|
||||
pos += 4;
|
||||
}
|
||||
// Just skip the immediate
|
||||
pos += 4;
|
||||
}
|
||||
OpCode::PushI32 | OpCode::PushBounded | OpCode::Jmp | OpCode::JmpIfFalse | OpCode::JmpIfTrue
|
||||
| OpCode::GetGlobal | OpCode::SetGlobal | OpCode::GetLocal | OpCode::SetLocal
|
||||
OpCode::PushI32 | OpCode::PushBounded | OpCode::GetGlobal | OpCode::SetGlobal | OpCode::GetLocal | OpCode::SetLocal
|
||||
| OpCode::PopN | OpCode::Syscall | OpCode::GateLoad | OpCode::GateStore => {
|
||||
pos += 4;
|
||||
}
|
||||
@@ -272,6 +295,51 @@ impl Linker {
|
||||
}
|
||||
}
|
||||
|
||||
// Ajuste final: se os nomes de função no DebugInfo estiverem enriquecidos no formato
|
||||
// "name@offset+len", alinhar apenas o `code_len` de `combined_functions[idx]` a esses
|
||||
// valores (os offsets do DebugInfo são locais ao módulo antes do link). Mantemos o
|
||||
// `code_offset` já realocado durante o PASS 1.
|
||||
for (idx, name) in &combined_function_names {
|
||||
if let Some((base, rest)) = name.split_once('@') {
|
||||
let mut parts = rest.split('+');
|
||||
if let (Some(off_str), Some(len_str)) = (parts.next(), parts.next()) {
|
||||
if let (Ok(_off), Ok(len)) = (off_str.parse::<u32>(), len_str.parse::<u32>()) {
|
||||
if let Some(meta) = combined_functions.get_mut(*idx as usize) {
|
||||
let old_off = meta.code_offset;
|
||||
let old_len = meta.code_len;
|
||||
meta.code_len = len;
|
||||
eprintln!(
|
||||
"[Linker][debug] Align len idx={} name={} -> code_offset {} (kept) | code_len {} -> {}",
|
||||
idx, base, old_off, old_len, len
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recalcular code_len de todas as funções no código combinado com base no deslocamento da próxima função
|
||||
// (end exclusivo). Isso garante que o fim efetivo da função seja exatamente o início da próxima
|
||||
// no buffer combinado, evitando divergências em saltos para o fim da função.
|
||||
// Use rotina canônica compartilhada para recalcular os comprimentos das funções
|
||||
layout::recompute_function_lengths_in_place(&mut combined_functions, combined_code.len());
|
||||
|
||||
// Removido padding específico de `frame`; o emissor passou a garantir que o label de término
|
||||
// esteja no ponto exato do fim do corpo, e, quando necessário, insere NOPs reais antes do fim.
|
||||
|
||||
// Garantir export do entry point 'frame' mesmo com nomes enriquecidos no DebugInfo.
|
||||
if !final_exports.contains_key("frame") {
|
||||
if let Some((idx, _name)) = combined_function_names.iter().find(|(i, name)| {
|
||||
let base = name.split('@').next().unwrap_or(name.as_str());
|
||||
let i_usize = *i as usize;
|
||||
(base == "frame" || base.ends_with(":frame"))
|
||||
&& combined_functions.get(i_usize).map(|m| m.param_slots == 0 && m.return_slots == 0).unwrap_or(false)
|
||||
}) {
|
||||
final_exports.insert("frame".to_string(), *idx);
|
||||
final_exports.insert("src/main/modules:frame".to_string(), *idx);
|
||||
}
|
||||
}
|
||||
|
||||
let combined_debug_info = if combined_pc_to_span.is_empty() && combined_function_names.is_empty() {
|
||||
None
|
||||
} else {
|
||||
|
||||
@@ -73,11 +73,19 @@ pub fn build_from_graph(graph: &ResolvedGraph, target: BuildTarget) -> Result<Bu
|
||||
// 2) Ensure that file declares fn frame(): void (no params)
|
||||
// We validate at the bytecode metadata level using function names and signature slots.
|
||||
if let Some(root_compiled) = compiled_modules.get(&root_step.project_id) {
|
||||
// Instrumentação: listar nomes de funções e metas do módulo root
|
||||
if let Some(di) = &root_compiled.debug_info {
|
||||
// Suprimir logs verbosos na versão final.
|
||||
let _ = di; // no-op
|
||||
}
|
||||
|
||||
// Find function index by name "frame" (tolerate qualified names ending with ":frame")
|
||||
let mut found_valid = false;
|
||||
if let Some(di) = &root_compiled.debug_info {
|
||||
for (idx, name) in &di.function_names {
|
||||
let is_frame_name = name == "frame" || name.ends_with(":frame");
|
||||
// Names in debug_info may be enriched as "name@offset+len". Strip the annotation for comparison.
|
||||
let base_name = name.split('@').next().unwrap_or(name.as_str());
|
||||
let is_frame_name = base_name == "frame" || base_name.ends_with(":frame");
|
||||
if is_frame_name {
|
||||
// Check signature: 0 params, 0 return slots
|
||||
if let Some(meta) = root_compiled.function_metas.get(*idx as usize) {
|
||||
|
||||
@@ -206,6 +206,7 @@ pub fn compile_project(
|
||||
_ => SymbolKind::Struct,
|
||||
}
|
||||
}
|
||||
ExportSurfaceKind::Function => SymbolKind::Function,
|
||||
},
|
||||
namespace: key.kind.namespace(),
|
||||
visibility: Visibility::Pub,
|
||||
@@ -268,35 +269,71 @@ pub fn compile_project(
|
||||
typechecker.check(&parsed.arena, parsed.root)?;
|
||||
}
|
||||
|
||||
// 4. Lower to IR
|
||||
let mut combined_program = crate::ir_core::Program {
|
||||
const_pool: crate::ir_core::ConstPool::new(),
|
||||
modules: Vec::new(),
|
||||
field_offsets: HashMap::new(),
|
||||
field_types: HashMap::new(),
|
||||
// 4. Lower ALL modules to VM and emit a single combined bytecode image for this project
|
||||
// Rationale: services and functions can live in multiple modules; exports must refer to
|
||||
// correct function indices within this CompiledModule. We aggregate all VM functions
|
||||
// into a single ir_vm::Module and assemble once using the public API `emit_fragments`.
|
||||
|
||||
// Combined VM module (we will merge const pools and remap ConstIds accordingly)
|
||||
let mut combined_vm = crate::ir_vm::Module::new(step.project_key.name.clone());
|
||||
combined_vm.const_pool = crate::ir_core::ConstPool::new();
|
||||
// Track origin module_path for each function we append to combined_vm
|
||||
let mut combined_func_origins: Vec<String> = Vec::new();
|
||||
|
||||
// Helper to insert a constant value into combined pool and return its new id
|
||||
let mut insert_const = |pool: &mut crate::ir_core::ConstPool, val: &crate::ir_core::ConstantValue| -> crate::ir_vm::types::ConstId {
|
||||
let new_id = pool.insert(val.clone());
|
||||
crate::ir_vm::types::ConstId(new_id.0)
|
||||
};
|
||||
|
||||
// Accumulate VM functions from each source file, remapping ConstIds as we go
|
||||
for (module_path, parsed) in &parsed_files {
|
||||
let ms = module_symbols_map.get(module_path).unwrap();
|
||||
let imported = file_imported_symbols.get(module_path).unwrap();
|
||||
let lowerer = Lowerer::new(&parsed.arena, ms, imported, &interner);
|
||||
let program = lowerer.lower_file(parsed.root, module_path)?;
|
||||
|
||||
// Combine program into combined_program
|
||||
if combined_program.modules.is_empty() {
|
||||
combined_program = program;
|
||||
} else {
|
||||
// TODO: Real merge
|
||||
|
||||
let vm_module = core_to_vm::lower_program(&program)
|
||||
.map_err(|e| CompileError::Internal(format!("Lowering error ({}): {}", module_path, e)))?;
|
||||
|
||||
// Build remap for this module's const ids
|
||||
let mut const_map: Vec<crate::ir_vm::types::ConstId> = Vec::with_capacity(vm_module.const_pool.constants.len());
|
||||
for c in &vm_module.const_pool.constants {
|
||||
let new_id = insert_const(&mut combined_vm.const_pool, c);
|
||||
const_map.push(new_id);
|
||||
}
|
||||
|
||||
// Clone functions and remap any PushConst const ids
|
||||
for mut f in vm_module.functions.into_iter() {
|
||||
for instr in &mut f.body {
|
||||
if let crate::ir_vm::instr::InstrKind::PushConst(old_id) = instr.kind {
|
||||
let mapped = const_map.get(old_id.0 as usize).cloned().unwrap_or(old_id);
|
||||
instr.kind = crate::ir_vm::instr::InstrKind::PushConst(mapped);
|
||||
}
|
||||
}
|
||||
combined_func_origins.push(module_path.clone());
|
||||
combined_vm.functions.push(f);
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Emit fragments
|
||||
let vm_module = core_to_vm::lower_program(&combined_program)
|
||||
.map_err(|e| CompileError::Internal(format!("Lowering error: {}", e)))?;
|
||||
|
||||
let fragments = emit_fragments(&vm_module)
|
||||
// Assemble once for the whole project using the public API
|
||||
let fragments = emit_fragments(&combined_vm)
|
||||
.map_err(|e| CompileError::Internal(format!("Emission error: {}", e)))?;
|
||||
|
||||
// Post-fix FunctionMeta slots from VM IR (some emitters may default to 0)
|
||||
let mut fixed_function_metas = fragments.functions.clone();
|
||||
for (i, fm) in fixed_function_metas.iter_mut().enumerate() {
|
||||
if let Some(vm_func) = combined_vm.functions.get(i) {
|
||||
fm.param_slots = vm_func.param_slots;
|
||||
fm.local_slots = vm_func.local_slots;
|
||||
fm.return_slots = vm_func.return_slots;
|
||||
}
|
||||
}
|
||||
|
||||
// Note: Entry point validation for the root project is now performed at the orchestrator level,
|
||||
// after compilation and before linking, using enriched debug info. We skip it here to avoid
|
||||
// double validation and mismatches with name annotations.
|
||||
|
||||
// 5. Collect exports
|
||||
let mut exports = BTreeMap::new();
|
||||
for (module_path, ms) in &module_symbols_map {
|
||||
@@ -315,25 +352,59 @@ pub fn compile_project(
|
||||
}
|
||||
}
|
||||
}
|
||||
// Build a set of public function names declared in this module (value namespace)
|
||||
let mut pub_fn_names: std::collections::HashSet<String> = std::collections::HashSet::new();
|
||||
for sym in ms.value_symbols.symbols.values() {
|
||||
if sym.visibility == Visibility::Pub {
|
||||
if let Some(surface_kind) = ExportSurfaceKind::from_symbol_kind(sym.kind) {
|
||||
// Find func_idx if it's a function or service
|
||||
let func_idx = vm_module
|
||||
.functions
|
||||
.iter()
|
||||
.position(|f| f.name == interner.resolve(sym.name))
|
||||
.map(|i| i as u32);
|
||||
|
||||
exports.insert(ExportKey {
|
||||
module_path: module_path.clone(),
|
||||
symbol_name: interner.resolve(sym.name).to_string(),
|
||||
kind: surface_kind,
|
||||
}, ExportMetadata {
|
||||
func_idx,
|
||||
is_host: sym.is_host,
|
||||
ty: sym.ty.clone(),
|
||||
});
|
||||
if matches!(surface_kind, ExportSurfaceKind::Function) {
|
||||
pub_fn_names.insert(interner.resolve(sym.name).to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for sym in ms.value_symbols.symbols.values() {
|
||||
if sym.visibility == Visibility::Pub {
|
||||
if let Some(surface_kind) = ExportSurfaceKind::from_symbol_kind(sym.kind) {
|
||||
// Encontrar TODAS as funções no módulo VM combinado originadas deste module_path
|
||||
// cujo nome seja igual ao do símbolo público; para cada uma, exportar
|
||||
// tanto o nome simples quanto o alias com aridade.
|
||||
let name_simple = interner.resolve(sym.name).to_string();
|
||||
let mut any_found = false;
|
||||
for (i, f) in combined_vm.functions.iter().enumerate() {
|
||||
if combined_func_origins.get(i).map(|s| s.as_str()) != Some(module_path.as_str()) { continue; }
|
||||
if f.name != name_simple { continue; }
|
||||
any_found = true;
|
||||
let meta = ExportMetadata {
|
||||
func_idx: Some(i as u32),
|
||||
is_host: sym.is_host,
|
||||
ty: sym.ty.clone(),
|
||||
};
|
||||
// Simple name
|
||||
exports.insert(ExportKey {
|
||||
module_path: module_path.clone(),
|
||||
symbol_name: name_simple.clone(),
|
||||
kind: surface_kind,
|
||||
}, meta.clone());
|
||||
// name/arity
|
||||
let arity = f.params.len();
|
||||
let export_name_arity = format!("{}/{}", name_simple, arity);
|
||||
exports.insert(ExportKey {
|
||||
module_path: module_path.clone(),
|
||||
symbol_name: export_name_arity,
|
||||
kind: surface_kind,
|
||||
}, meta);
|
||||
}
|
||||
// Caso nada tenha sido encontrado no VM (ex.: métodos ainda não materializados),
|
||||
// publique ao menos o nome simples sem func_idx (mantém compatibilidade de surface)
|
||||
if !any_found {
|
||||
exports.insert(ExportKey {
|
||||
module_path: module_path.clone(),
|
||||
symbol_name: name_simple.clone(),
|
||||
kind: surface_kind,
|
||||
}, ExportMetadata { func_idx: None, is_host: sym.is_host, ty: sym.ty.clone() });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -347,6 +418,27 @@ pub fn compile_project(
|
||||
&interner,
|
||||
);
|
||||
|
||||
// 6.b) Enriquecer debug_info com metadados de função (offset/len) para análise externa
|
||||
let mut dbg = fragments.debug_info.clone().unwrap_or_default();
|
||||
// Adiciona pares (func_idx, (code_offset, code_len)) ao campo function_names como anotações extras
|
||||
// Sem quebrar o formato, usamos o name como "name@offset+len" para tooling/analysis.json
|
||||
let mut enriched_function_names = Vec::new();
|
||||
for (i, (fid, name)) in fragments
|
||||
.debug_info
|
||||
.as_ref()
|
||||
.map(|d| d.function_names.clone())
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
let meta = &fixed_function_metas[i];
|
||||
let annotated = format!("{}@{}+{}", name, meta.code_offset, meta.code_len);
|
||||
enriched_function_names.push((fid, annotated));
|
||||
}
|
||||
if !enriched_function_names.is_empty() {
|
||||
dbg.function_names = enriched_function_names;
|
||||
}
|
||||
|
||||
// 7. Collect imports from unresolved labels
|
||||
let mut imports = Vec::new();
|
||||
for (label, pcs) in fragments.unresolved_labels {
|
||||
@@ -382,8 +474,8 @@ pub fn compile_project(
|
||||
imports,
|
||||
const_pool: fragments.const_pool,
|
||||
code: fragments.code,
|
||||
function_metas: fragments.functions,
|
||||
debug_info: fragments.debug_info,
|
||||
function_metas: fixed_function_metas,
|
||||
debug_info: Some(dbg),
|
||||
symbols: project_symbols,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user