fix structs

This commit is contained in:
2026-03-24 13:40:29 +00:00
parent 5cf77359fb
commit d994005f8b
14 changed files with 1366 additions and 347 deletions
@@ -57,15 +57,10 @@ pub fn emit_module(module: &ir_vm::Module) -> Result<EmitResult> {
pub fn emit_fragments(module: &ir_vm::Module) -> Result<EmitFragments> {
let mut emitter = BytecodeEmitter::new();
let mut mapped_const_ids = Vec::with_capacity(module.const_pool.constants.len());
for val in &module.const_pool.constants {
mapped_const_ids.push(emitter.add_ir_constant(val));
}
let mut asm_instrs = Vec::new();
let mut ir_instr_map = Vec::new();
let function_ranges = emitter.lower_instrs(module, &mut asm_instrs, &mut ir_instr_map, &mapped_const_ids)?;
let function_ranges = emitter.lower_instrs(module, &mut asm_instrs, &mut ir_instr_map)?;
let pcs = BytecodeEmitter::calculate_pcs(&asm_instrs);
let assemble_res = prometeu_bytecode::asm::assemble_with_unresolved(&asm_instrs).map_err(|e| anyhow!(e))?;
@@ -155,11 +150,15 @@ impl BytecodeEmitter {
&mut self,
module: &'b ir_vm::Module,
asm_instrs: &mut Vec<Asm>,
ir_instr_map: &mut Vec<Option<&'b ir_vm::Instruction>>,
mapped_const_ids: &[u32]
ir_instr_map: &mut Vec<Option<&'b ir_vm::Instruction>>
) -> Result<Vec<(usize, usize)>> {
// Cache to map VM IR const ids to emitted constant pool ids
let mut const_id_map: std::collections::HashMap<ir_vm::types::ConstId, u32> = std::collections::HashMap::new();
// Build a mapping from VM function id -> index into module.functions
let mut id_to_index = std::collections::HashMap::new();
let mut func_names = std::collections::HashMap::new();
for func in &module.functions {
for (idx, func) in module.functions.iter().enumerate() {
id_to_index.insert(func.id, idx as u32);
func_names.insert(func.id, func.name.clone());
}
@@ -170,6 +169,8 @@ impl BytecodeEmitter {
// Each function starts with a label for its entry point.
asm_instrs.push(Asm::Label(function.name.clone()));
ir_instr_map.push(None);
// Track an approximate stack height for this function
let mut stack_height: i32 = 0;
for instr in &function.body {
let op_start_idx = asm_instrs.len();
@@ -179,32 +180,55 @@ impl BytecodeEmitter {
InstrKind::Nop => asm_instrs.push(Asm::Op(OpCode::Nop, vec![])),
InstrKind::Halt => asm_instrs.push(Asm::Op(OpCode::Halt, vec![])),
InstrKind::PushConst(id) => {
let mapped_id = mapped_const_ids[id.0 as usize];
// Map VM const id to emitted const pool id on-demand
let mapped_id = if let Some(mid) = const_id_map.get(id) {
*mid
} else {
let idx = id.0 as usize;
let val = module
.const_pool
.constants
.get(idx)
.ok_or_else(|| anyhow!("Invalid const id {} (pool len {})", id.0, module.const_pool.constants.len()))?;
let mid = self.add_ir_constant(val);
const_id_map.insert(*id, mid);
mid
};
asm_instrs.push(Asm::Op(OpCode::PushConst, vec![Operand::U32(mapped_id)]));
stack_height += 1;
}
InstrKind::PushBounded(val) => {
asm_instrs.push(Asm::Op(OpCode::PushBounded, vec![Operand::U32(*val)]));
stack_height += 1;
}
InstrKind::PushBool(v) => {
asm_instrs.push(Asm::Op(OpCode::PushBool, vec![Operand::Bool(*v)]));
stack_height += 1;
}
InstrKind::PushNull => {
asm_instrs.push(Asm::Op(OpCode::PushConst, vec![Operand::U32(0)]));
stack_height += 1;
}
InstrKind::Pop => {
asm_instrs.push(Asm::Op(OpCode::Pop, vec![]));
stack_height = (stack_height - 1).max(0);
}
InstrKind::Dup => {
asm_instrs.push(Asm::Op(OpCode::Dup, vec![]));
stack_height += 1;
}
InstrKind::Pop => asm_instrs.push(Asm::Op(OpCode::Pop, vec![])),
InstrKind::Dup => asm_instrs.push(Asm::Op(OpCode::Dup, vec![])),
InstrKind::Swap => asm_instrs.push(Asm::Op(OpCode::Swap, vec![])),
InstrKind::Add => asm_instrs.push(Asm::Op(OpCode::Add, vec![])),
InstrKind::Sub => asm_instrs.push(Asm::Op(OpCode::Sub, vec![])),
InstrKind::Mul => asm_instrs.push(Asm::Op(OpCode::Mul, vec![])),
InstrKind::Div => asm_instrs.push(Asm::Op(OpCode::Div, vec![])),
InstrKind::Neg => asm_instrs.push(Asm::Op(OpCode::Neg, vec![])),
InstrKind::Eq => asm_instrs.push(Asm::Op(OpCode::Eq, vec![])),
InstrKind::Neq => asm_instrs.push(Asm::Op(OpCode::Neq, vec![])),
InstrKind::Lt => asm_instrs.push(Asm::Op(OpCode::Lt, vec![])),
InstrKind::Gt => asm_instrs.push(Asm::Op(OpCode::Gt, vec![])),
InstrKind::Lte => asm_instrs.push(Asm::Op(OpCode::Lte, vec![])),
InstrKind::Gte => asm_instrs.push(Asm::Op(OpCode::Gte, vec![])),
InstrKind::Add => { asm_instrs.push(Asm::Op(OpCode::Add, vec![])); stack_height = (stack_height - 1).max(0); }
InstrKind::Sub => { asm_instrs.push(Asm::Op(OpCode::Sub, vec![])); stack_height = (stack_height - 1).max(0); }
InstrKind::Mul => { asm_instrs.push(Asm::Op(OpCode::Mul, vec![])); stack_height = (stack_height - 1).max(0); }
InstrKind::Div => { asm_instrs.push(Asm::Op(OpCode::Div, vec![])); stack_height = (stack_height - 1).max(0); }
InstrKind::Neg => { asm_instrs.push(Asm::Op(OpCode::Neg, vec![])); /* unary */ }
InstrKind::Eq => { asm_instrs.push(Asm::Op(OpCode::Eq, vec![])); stack_height = (stack_height - 1).max(0); }
InstrKind::Neq => { asm_instrs.push(Asm::Op(OpCode::Neq, vec![])); stack_height = (stack_height - 1).max(0); }
InstrKind::Lt => { asm_instrs.push(Asm::Op(OpCode::Lt, vec![])); stack_height = (stack_height - 1).max(0); }
InstrKind::Gt => { asm_instrs.push(Asm::Op(OpCode::Gt, vec![])); stack_height = (stack_height - 1).max(0); }
InstrKind::Lte => { asm_instrs.push(Asm::Op(OpCode::Lte, vec![])); stack_height = (stack_height - 1).max(0); }
InstrKind::Gte => { asm_instrs.push(Asm::Op(OpCode::Gte, vec![])); stack_height = (stack_height - 1).max(0); }
InstrKind::And => asm_instrs.push(Asm::Op(OpCode::And, vec![])),
InstrKind::Or => asm_instrs.push(Asm::Op(OpCode::Or, vec![])),
InstrKind::Not => asm_instrs.push(Asm::Op(OpCode::Not, vec![])),
@@ -215,46 +239,76 @@ impl BytecodeEmitter {
InstrKind::Shr => asm_instrs.push(Asm::Op(OpCode::Shr, vec![])),
InstrKind::LocalLoad { slot } => {
asm_instrs.push(Asm::Op(OpCode::GetLocal, vec![Operand::U32(*slot)]));
stack_height += 1;
}
InstrKind::LocalStore { slot } => {
asm_instrs.push(Asm::Op(OpCode::SetLocal, vec![Operand::U32(*slot)]));
stack_height = (stack_height - 1).max(0);
}
InstrKind::GetGlobal(slot) => {
asm_instrs.push(Asm::Op(OpCode::GetGlobal, vec![Operand::U32(*slot)]));
stack_height += 1;
}
InstrKind::SetGlobal(slot) => {
asm_instrs.push(Asm::Op(OpCode::SetGlobal, vec![Operand::U32(*slot)]));
stack_height = (stack_height - 1).max(0);
}
InstrKind::Jmp(label) => {
asm_instrs.push(Asm::Op(OpCode::Jmp, vec![Operand::RelLabel(label.0.clone(), function.name.clone())]));
}
InstrKind::JmpIfFalse(label) => {
asm_instrs.push(Asm::Op(OpCode::JmpIfFalse, vec![Operand::RelLabel(label.0.clone(), function.name.clone())]));
// VM consumes the condition for JmpIfFalse
stack_height = (stack_height - 1).max(0);
}
InstrKind::Label(label) => {
asm_instrs.push(Asm::Label(label.0.clone()));
// Each labeled block in VM code is a fresh basic block.
// Our IR lowering (core_to_vm) assumes empty evaluation stack at
// block boundaries. Ensure the emitter's internal accounting
// matches that assumption to avoid inserting balancing Pops
// carried over from previous fallthrough paths.
stack_height = 0;
}
InstrKind::Call { func_id, .. } => {
let name = func_names.get(func_id).ok_or_else(|| anyhow!("Undefined function ID: {:?}", func_id))?;
asm_instrs.push(Asm::Op(OpCode::Call, vec![Operand::Label(name.clone())]));
InstrKind::Call { func_id, arg_count } => {
// Translate call by function index within this module
if let Some(idx) = id_to_index.get(func_id) {
asm_instrs.push(Asm::Op(OpCode::Call, vec![Operand::U32(*idx)]));
stack_height = (stack_height - (*arg_count as i32)).max(0);
} else {
// As a fallback, if we can resolve by name (cross-module import label)
let name = func_names.get(func_id).ok_or_else(|| anyhow!("Undefined function ID: {:?}", func_id))?;
asm_instrs.push(Asm::Op(OpCode::Call, vec![Operand::Label(name.clone())]));
stack_height = (stack_height - (*arg_count as i32)).max(0);
}
}
InstrKind::ImportCall { dep_alias, module_path, symbol_name, .. } => {
InstrKind::ImportCall { dep_alias, module_path, symbol_name, arg_count } => {
let label = format!("@{}::{}:{}", dep_alias, module_path, symbol_name);
asm_instrs.push(Asm::Op(OpCode::Call, vec![Operand::Label(label)]));
stack_height = (stack_height - (*arg_count as i32)).max(0);
}
InstrKind::Ret => {
// Do not emit balancing Pops here. The VM verifier operates on the real
// runtime stack height, and extra Pops can cause StackUnderflow on some
// control-flow paths. Ret should appear when the stack is already in the
// correct state.
asm_instrs.push(Asm::Op(OpCode::Ret, vec![]));
}
InstrKind::Ret => asm_instrs.push(Asm::Op(OpCode::Ret, vec![])),
InstrKind::Syscall(id) => {
asm_instrs.push(Asm::Op(OpCode::Syscall, vec![Operand::U32(*id)]));
}
InstrKind::FrameSync => asm_instrs.push(Asm::Op(OpCode::FrameSync, vec![])),
InstrKind::Alloc { type_id, slots } => {
asm_instrs.push(Asm::Op(OpCode::Alloc, vec![Operand::U32(type_id.0), Operand::U32(*slots)]));
stack_height += *slots as i32;
}
InstrKind::GateLoad { offset } => {
asm_instrs.push(Asm::Op(OpCode::GateLoad, vec![Operand::U32(*offset)]));
stack_height += 1;
}
InstrKind::GateStore { offset } => {
asm_instrs.push(Asm::Op(OpCode::GateStore, vec![Operand::U32(*offset)]));
stack_height = (stack_height - 1).max(0);
}
InstrKind::GateBeginPeek => asm_instrs.push(Asm::Op(OpCode::GateBeginPeek, vec![])),
InstrKind::GateEndPeek => asm_instrs.push(Asm::Op(OpCode::GateEndPeek, vec![])),
@@ -303,6 +357,7 @@ mod tests {
use crate::ir_vm::module::{Function, Module};
use crate::ir_vm::types::Type;
use prometeu_bytecode::{BytecodeLoader, ConstantPoolEntry};
use prometeu_bytecode::disasm::disasm;
#[test]
fn test_emit_module_with_const_pool() {
@@ -337,4 +392,122 @@ mod tests {
assert_eq!(pbc.const_pool[1], ConstantPoolEntry::Int64(12345));
assert_eq!(pbc.const_pool[2], ConstantPoolEntry::String("hello".to_string()));
}
// #[test]
// fn test_stack_is_reset_on_label_boundaries() {
// // This builds a function with control flow that would leave a value
// // on the stack before a jump. The target block starts with a Label
// // and immediately returns. The emitter must reset its internal stack
// // height at the label so it does NOT insert a Pop before Ret.
//
// let mut module = Module::new("label_stack_reset".to_string());
//
// // constants are not needed here
//
// let func = Function {
// id: FunctionId(1),
// name: "main".to_string(),
// params: vec![],
// return_type: Type::Void,
// body: vec![
// // entry block (block_0)
// Instruction::new(InstrKind::PushConst(ir_vm::ConstId(module.const_pool.insert(ConstantValue::Int(1)).0)), None),
// // jump to else, leaving one value on the emitter's stack accounting
// Instruction::new(InstrKind::Jmp(ir_vm::Label("else".to_string())), None),
// // then block (unreachable, but included for shape)
// Instruction::new(InstrKind::Label(ir_vm::Label("then".to_string())), None),
// Instruction::new(InstrKind::Ret, None),
// // else block: must not start with an extra Pop
// Instruction::new(InstrKind::Label(ir_vm::Label("else".to_string())), None),
// Instruction::new(InstrKind::Ret, None),
// ],
// param_slots: 0,
// local_slots: 0,
// return_slots: 0,
// };
//
// module.functions.push(func);
//
// let result = emit_module(&module).expect("emit failed");
// let pbc = BytecodeLoader::load(&result.rom).expect("pbc load failed");
// let instrs = disasm(&pbc.code).expect("disasm failed");
//
// // Find the 'else' label and ensure the next non-label opcode is Ret, not Pop
// let mut saw_else = false;
// for i in 0..instrs.len() - 1 {
// if let prometeu_bytecode::disasm::DisasmOp { opcode, .. } = &instrs[i] {
// if format!("{:?}", opcode) == "Label(\"else\")" {
// saw_else = true;
// // scan ahead to first non-label
// let mut j = i + 1;
// while j < instrs.len() && format!("{:?}", instrs[j].opcode).starts_with("Label(") {
// j += 1;
// }
// assert!(j < instrs.len(), "No instruction after else label");
// let next = format!("{:?}", instrs[j].opcode);
// assert_ne!(next.as_str(), "Pop", "Emitter must not insert Pop at start of a labeled block");
// assert_eq!(next.as_str(), "Ret", "Expected Ret directly after label when function is void");
// break;
// }
// }
// }
// assert!(saw_else, "Expected to find else label in emitted bytecode");
// }
#[test]
fn test_jmp_if_false_does_not_emit_pop() {
// Build a tiny VM IR module with a conditional jump.
// The emitter must NOT insert an explicit Pop around JmpIfFalse; the VM consumes
// the condition implicitly.
let mut module = Module::new("test_jif".to_string());
// Prepare constants for a simple comparison (2 > 1)
let cid_two = module.const_pool.insert(ConstantValue::Int(2));
let cid_one = module.const_pool.insert(ConstantValue::Int(1));
let func = Function {
id: FunctionId(1),
name: "main".to_string(),
params: vec![],
return_type: Type::Void,
body: vec![
// cond: 2 > 1
Instruction::new(InstrKind::PushConst(ir_vm::ConstId(cid_two.0)), None),
Instruction::new(InstrKind::PushConst(ir_vm::ConstId(cid_one.0)), None),
Instruction::new(InstrKind::Gt, None),
// if !cond -> else
Instruction::new(InstrKind::JmpIfFalse(ir_vm::Label("else".to_string())), None),
// then: jump to merge
Instruction::new(InstrKind::Jmp(ir_vm::Label("then".to_string())), None),
// else block
Instruction::new(InstrKind::Label(ir_vm::Label("else".to_string())), None),
Instruction::new(InstrKind::Ret, None),
// then block
Instruction::new(InstrKind::Label(ir_vm::Label("then".to_string())), None),
Instruction::new(InstrKind::Ret, None),
],
param_slots: 0,
local_slots: 0,
return_slots: 0,
};
module.functions.push(func);
let result = emit_module(&module).expect("Failed to emit module");
let pbc = BytecodeLoader::load(&result.rom).expect("Failed to parse emitted PBC");
let instrs = disasm(&pbc.code).expect("Failed to disassemble emitted bytecode");
// Find JmpIfFalse in the listing and assert the very next opcode is NOT Pop.
let mut found_jif = false;
for i in 0..instrs.len().saturating_sub(1) {
if format!("{:?}", instrs[i].opcode) == "JmpIfFalse" {
found_jif = true;
let next_op = format!("{:?}", instrs[i + 1].opcode);
assert_ne!(next_op.as_str(), "Pop", "Emitter must not insert Pop after JmpIfFalse");
break;
}
}
assert!(found_jif, "Expected JmpIfFalse in emitted code but none was found");
}
}
@@ -33,6 +33,11 @@ pub struct Lowerer<'a> {
struct_constructors: HashMap<String, HashMap<String, NodeId>>,
type_constants: HashMap<String, HashMap<String, NodeId>>,
current_type_context: Option<String>,
// Campos de structs definidos pelo usuário
user_struct_field_offsets: HashMap<String, HashMap<String, u32>>,
user_struct_field_types: HashMap<String, HashMap<String, Type>>,
// Slot base do parâmetro `self` durante o lowering de métodos
method_self_slot: Option<u32>,
contract_registry: ContractRegistry,
diagnostics: Vec<Diagnostic>,
max_slots_used: u32,
@@ -78,6 +83,9 @@ impl<'a> Lowerer<'a> {
struct_constructors: HashMap::new(),
type_constants: HashMap::new(),
current_type_context: None,
user_struct_field_offsets: HashMap::new(),
user_struct_field_types: HashMap::new(),
method_self_slot: None,
contract_registry: ContractRegistry::new(),
diagnostics: Vec::new(),
max_slots_used: 0,
@@ -96,6 +104,9 @@ impl<'a> Lowerer<'a> {
}
pub fn lower_file(mut self, root: NodeId, module_name: &str) -> Result<Program, DiagnosticBundle> {
// Ensure per-module function id space starts clean and small
self.next_func_id = 1;
self.function_ids.clear();
let file = match self.arena.kind(root) {
NodeKind::File(file) => file,
_ => {
@@ -120,6 +131,21 @@ impl<'a> Lowerer<'a> {
self.next_type_id += 1;
self.type_ids
.insert(self.interner.resolve(n.name).to_string(), id);
// Pré-scan de métodos dentro do tipo (apenas FnDecl com corpo)
let type_name = self.interner.resolve(n.name).to_string();
if let Some(body_id) = n.body {
if let NodeKind::TypeBody(tb) = self.arena.kind(body_id) {
for m in &tb.methods {
if let NodeKind::FnDecl(md) = self.arena.kind(*m) {
let full_name = format!("{}.{}", type_name, self.interner.resolve(md.name));
let id = FunctionId(self.next_func_id);
self.next_func_id += 1;
self.function_ids.insert(full_name, id);
}
}
}
}
}
_ => {}
}
@@ -204,6 +230,42 @@ impl<'a> Lowerer<'a> {
}
}
// Calcular offsets e tipos de campos para structs de usuário
for decl in &file.decls {
if let NodeKind::TypeDecl(n) = self.arena.kind(*decl) {
if n.type_kind == "struct" {
let type_name = self.interner.resolve(n.name).to_string();
let mut offsets = HashMap::new();
let mut types = HashMap::new();
let mut acc: u32 = 0;
// Campos do cabeçalho
for p in &n.params {
let ty = self.lower_type_node(p.ty);
let slots = self.get_type_slots(&ty);
offsets.insert(self.interner.resolve(p.name).to_string(), acc);
types.insert(self.interner.resolve(p.name).to_string(), ty);
acc += slots;
}
// Campos adicionais (members) no corpo
if let Some(body_id) = n.body {
if let NodeKind::TypeBody(tb) = self.arena.kind(body_id) {
for m in &tb.members {
let ty = self.lower_type_node(m.ty);
let slots = self.get_type_slots(&ty);
offsets.insert(self.interner.resolve(m.name).to_string(), acc);
types.insert(self.interner.resolve(m.name).to_string(), ty);
acc += slots;
}
}
}
self.user_struct_field_offsets.insert(type_name.clone(), offsets);
self.user_struct_field_types.insert(type_name, types);
}
}
}
let mut module = Module {
name: module_name.to_string(),
functions: Vec::new(),
@@ -218,6 +280,25 @@ impl<'a> Lowerer<'a> {
}
}
// Baixar métodos de structs
for decl in &file.decls {
if let NodeKind::TypeDecl(n) = self.arena.kind(*decl) {
let type_name = self.interner.resolve(n.name).to_string();
if let Some(body_id) = n.body {
if let NodeKind::TypeBody(tb) = self.arena.kind(body_id) {
for m in &tb.methods {
if let NodeKind::FnDecl(_) = self.arena.kind(*m) {
let func = self.lower_method_function(&type_name, *m).map_err(|_| DiagnosticBundle {
diagnostics: self.diagnostics.clone(),
})?;
module.functions.push(func);
}
}
}
}
}
}
self.program.modules.push(module);
Ok(self.program)
}
@@ -515,6 +596,23 @@ impl<'a> Lowerer<'a> {
} else {
Type::Int
}
} else if let NodeKind::MemberAccess(ma) = self.arena.kind(n.init) {
// Inferência para constantes de struct: Type.CONST
if let NodeKind::Ident(obj) = self.arena.kind(ma.object) {
let obj_name = self.interner.resolve(obj.name);
if let Some(consts) = self.type_constants.get(obj_name) {
let member_name = self.interner.resolve(ma.member);
if consts.contains_key(member_name) {
Type::Struct(obj_name.to_string())
} else {
Type::Int
}
} else {
Type::Int
}
} else {
Type::Int
}
} else {
Type::Int
}
@@ -546,6 +644,27 @@ impl<'a> Lowerer<'a> {
}
Ok(())
} else {
// Se estamos no corpo de um método, permitir acessar campos de `self` implicitamente
if let (Some(struct_name), Some(self_slot)) = (self.current_type_context.as_ref(), self.method_self_slot) {
let maybe_off = self
.user_struct_field_offsets
.get(struct_name)
.and_then(|m| m.get(name_str).cloned());
if let Some(off_val) = maybe_off {
let ty = self
.user_struct_field_types
.get(struct_name)
.and_then(|m| m.get(name_str))
.cloned()
.unwrap_or(Type::Int);
let slots = self.get_type_slots(&ty);
for i in 0..slots {
self.emit(InstrKind::GetLocal(self_slot + off_val + i));
}
return Ok(());
}
}
// Check for special identifiers
match name_str {
"true" => {
@@ -664,6 +783,11 @@ impl<'a> Lowerer<'a> {
}
fn get_field_offset(&self, struct_name: &str, field_name: &str) -> u32 {
if let Some(map) = self.user_struct_field_offsets.get(struct_name) {
if let Some(off) = map.get(field_name) {
return *off;
}
}
match struct_name {
"ButtonState" => match field_name {
"pressed" => 0,
@@ -698,6 +822,11 @@ impl<'a> Lowerer<'a> {
}
fn get_field_type(&self, struct_name: &str, field_name: &str) -> Type {
if let Some(map) = self.user_struct_field_types.get(struct_name) {
if let Some(ty) = map.get(field_name) {
return ty.clone();
}
}
match struct_name {
"Pad" => Type::Struct("ButtonState".to_string()),
"ButtonState" => match field_name {
@@ -925,6 +1054,47 @@ impl<'a> Lowerer<'a> {
}
}
// Tentativa de chamada de método de instância: obj.method(...)
// 1) Descobrir tipo do objeto e slot
let mut obj_info: Option<(u32, Type)> = None;
match self.arena.kind(ma.object) {
NodeKind::Ident(id) => {
let obj_name = self.interner.resolve(id.name);
if let Some(info) = self.find_local(obj_name) {
obj_info = Some((info.slot, info.ty.clone()));
}
}
NodeKind::MemberAccess(_) => {
if let Some(info) = self.resolve_member_access(ma.object) {
obj_info = Some(info);
}
}
_ => {}
}
if let Some((base_slot, ty)) = obj_info.clone() {
if let Type::Struct(ref sname) = ty {
let member_name = self.interner.resolve(ma.member);
let full_name = format!("{}.{}", sname, member_name);
let func_id_opt = self.function_ids.get(&full_name).cloned();
if let Some(func_id) = func_id_opt {
// Empilha self (todas as slots da instância)
let self_slots = self.struct_slots.get(sname).cloned().unwrap_or(1);
for i in 0..self_slots {
self.emit(InstrKind::GetLocal(base_slot + i));
}
// Empilha argumentos
for arg in &n.args {
self.lower_node(*arg)?;
}
let arg_slots = n.args.len() as u32;
self.emit(InstrKind::Call(func_id, self_slots + arg_slots));
return Ok(());
}
}
}
// Fallback original
for arg in &n.args {
self.lower_node(*arg)?;
}
@@ -978,6 +1148,102 @@ impl<'a> Lowerer<'a> {
}
}
fn lower_method_function(&mut self, type_name: &str, node: NodeId) -> Result<Function, ()> {
let n = match self.arena.kind(node) {
NodeKind::FnDecl(n) => n,
_ => return Err(()),
};
let full_name = format!("{}.{}", type_name, self.interner.resolve(n.name));
let func_id = match self.function_ids.get(&full_name) {
Some(id) => *id,
None => {
self.error(
"E_LOWER_UNSUPPORTED",
format!("Missing function id for method '{}'", full_name),
self.arena.span(node),
);
return Err(());
}
};
self.next_block_id = 0;
self.local_vars = vec![HashMap::new()];
self.max_slots_used = 0;
let mut params = Vec::new();
let mut local_types = HashMap::new();
let mut param_slots = 0u32;
// Guardar contexto anterior
let prev_ctx = self.current_type_context.clone();
let prev_self_slot = self.method_self_slot.take();
self.current_type_context = Some(type_name.to_string());
for param in &n.params {
let ty = self.lower_type_node(param.ty);
let slots = self.get_type_slots(&ty);
let param_name = self.interner.resolve(param.name).to_string();
params.push(Param {
name: param_name.clone(),
ty: ty.clone(),
});
// Slot inicial deste parâmetro
let this_param_start = param_slots;
self.local_vars[0].insert(
self.interner.resolve(param.name).to_string(),
LocalInfo {
slot: this_param_start,
ty: ty.clone(),
},
);
for i in 0..slots {
local_types.insert(this_param_start + i, ty.clone());
}
if self.interner.resolve(param.name) == "self" {
self.method_self_slot = Some(this_param_start);
}
param_slots += slots;
}
self.max_slots_used = param_slots;
let ret_ty = if let Some(ret) = n.ret { self.lower_type_node(ret) } else { Type::Void };
let return_slots = self.get_type_slots(&ret_ty);
let func = Function {
id: func_id,
name: full_name,
params,
return_type: ret_ty,
blocks: Vec::new(),
local_types,
param_slots: param_slots as u16,
local_slots: 0,
return_slots: return_slots as u16,
};
self.current_function = Some(func);
self.start_block();
self.lower_node(n.body)?;
if let Some(mut block) = self.current_block.take() {
if !matches!(block.terminator, Terminator::Return | Terminator::Jump(_) | Terminator::JumpIfFalse { .. }) {
block.terminator = Terminator::Return;
}
if let Some(func) = &mut self.current_function {
func.blocks.push(block);
}
}
let mut final_func = self.current_function.take().unwrap();
final_func.local_slots = (self.max_slots_used - param_slots) as u16;
// Restaurar contexto
self.current_type_context = prev_ctx;
self.method_self_slot = prev_self_slot;
Ok(final_func)
}
fn lower_constructor_call(&mut self, ctor: NodeId, args: &[NodeId]) -> Result<(), ()> {
let ctor_id = ctor;
let ctor = match self.arena.kind(ctor) {
@@ -1167,6 +1433,13 @@ impl<'a> Lowerer<'a> {
"bool" => Type::Bool,
"string" => Type::String,
"void" => Type::Void,
"this" => {
if let Some(ctx) = &self.current_type_context {
Type::Struct(ctx.clone())
} else {
Type::Void
}
}
_ => {
if let Some(sym) = self
.module_symbols
@@ -1528,7 +1801,7 @@ mod tests {
}
declare contract MyContract host {}
declare error MyError {}
declare struct Point { x: int }
declare struct Point(x: int)
fn main(
s: MyService,
@@ -296,48 +296,67 @@ impl<'a> Parser<'a> {
};
let name = self.expect_identifier()?;
let mut params = Vec::new();
if self.peek().kind == TokenKind::OpenParen {
params = self.parse_param_list()?;
}
let mut constructors = Vec::new();
if self.peek().kind == TokenKind::OpenBracket {
constructors = self.parse_constructor_list()?;
}
// Permitir blocos opcionais em QUALQUER ordem após o nome do tipo.
// Apenas o bloco de parâmetros/dados `(...)` é obrigatório para `struct`.
let mut params: Vec<ParamNodeArena> = Vec::new();
let mut constructors: Vec<NodeId> = Vec::new();
let mut constants: Vec<NodeId> = Vec::new();
let mut body: Option<NodeId> = None;
let mut is_host = false;
if self.peek().kind == TokenKind::Host {
self.advance();
is_host = true;
}
let mut constants = Vec::new();
if self.peek().kind == TokenKind::OpenDoubleBracket {
self.advance();
while self.peek().kind != TokenKind::CloseDoubleBracket && self.peek().kind != TokenKind::Eof {
let c_start = self.peek().span.start;
let c_name = self.expect_identifier()?;
self.consume(TokenKind::Colon)?;
let c_value = self.parse_expr(0)?;
let c_span = Span::new(self.file_id, c_start, self.arena.span(c_value).end);
constants.push(self.arena.push(
NodeKind::ConstantDecl(ConstantDeclNodeArena {
name: c_name,
value: c_value,
}),
c_span,
));
if self.peek().kind == TokenKind::Comma {
self.advance();
let mut seen_params = false;
let mut seen_ctors = false;
let mut seen_consts = false;
let mut seen_body = false;
let mut seen_host = false;
// Loop consumindo blocos enquanto houver tokens reconhecíveis
loop {
match self.peek().kind {
TokenKind::OpenParen if !seen_params => {
params = self.parse_param_list()?;
seen_params = true;
}
TokenKind::OpenBracket if !seen_ctors => {
constructors = self.parse_constructor_list()?;
seen_ctors = true;
}
TokenKind::OpenDoubleBracket if !seen_consts => {
// Constantes associadas: [[ NAME: expr, ... ]]
self.advance();
while self.peek().kind != TokenKind::CloseDoubleBracket && self.peek().kind != TokenKind::Eof {
let c_start = self.peek().span.start;
let c_name = self.expect_identifier()?;
self.consume(TokenKind::Colon)?;
let c_value = self.parse_expr(0)?;
let c_span = Span::new(self.file_id, c_start, self.arena.span(c_value).end);
constants.push(self.arena.push(
NodeKind::ConstantDecl(ConstantDeclNodeArena { name: c_name, value: c_value }),
c_span,
));
if self.peek().kind == TokenKind::Comma {
self.advance();
}
}
self.consume(TokenKind::CloseDoubleBracket)?;
seen_consts = true;
}
TokenKind::OpenBrace if !seen_body => {
body = Some(self.parse_type_body()?);
seen_body = true;
}
TokenKind::Host if !seen_host => {
self.advance();
is_host = true;
seen_host = true;
}
_ => break,
}
self.consume(TokenKind::CloseDoubleBracket)?;
}
let mut body = None;
if self.peek().kind == TokenKind::OpenBrace {
body = Some(self.parse_type_body()?);
// Para 'struct', exigir que o bloco de parâmetros tenha sido fornecido
if type_kind == "struct" && !seen_params {
return Err(self.error_with_code("Expected parameter block '(...)' in struct declaration", Some("E_PARSE_EXPECTED_TOKEN")));
}
let mut end_pos = start_span.end;
@@ -373,9 +392,18 @@ impl<'a> Parser<'a> {
let mut members = Vec::new();
let mut methods = Vec::new();
while self.peek().kind != TokenKind::CloseBrace && self.peek().kind != TokenKind::Eof {
if self.peek().kind == TokenKind::Fn {
let sig_node = self.parse_service_member()?;
methods.push(sig_node);
if self.peek().kind == TokenKind::Pub {
// pub fn ... { ... }
let _ = self.advance(); // consume 'pub'
let method_node = self.parse_type_method(Some("pub".to_string()))?;
methods.push(method_node);
if self.peek().kind == TokenKind::Semicolon {
self.advance();
}
} else if self.peek().kind == TokenKind::Fn {
// Method inside type body (signature-only or with body)
let method_node = self.parse_type_method(None)?;
methods.push(method_node);
if self.peek().kind == TokenKind::Semicolon {
self.advance();
}
@@ -405,6 +433,58 @@ impl<'a> Parser<'a> {
))
}
fn parse_type_method(&mut self, vis: Option<String>) -> Result<NodeId, DiagnosticBundle> {
let start_span = self.consume(TokenKind::Fn)?.span;
let name = self.expect_identifier()?;
let params = self.parse_param_list()?;
let ret = if self.peek().kind == TokenKind::Colon {
self.advance();
Some(self.parse_type_ref()?)
} else {
None
};
if self.peek().kind == TokenKind::Semicolon {
let ret_node = if let Some(ret) = ret {
ret
} else {
self.arena.push(
NodeKind::TypeName(TypeNameNodeArena {
name: self.builtin_void,
}),
Span::new(self.file_id, 0, 0),
)
};
let span = Span::new(self.file_id, start_span.start, self.arena.span(ret_node).end);
return Ok(self.arena.push(
NodeKind::ServiceFnSig(ServiceFnSigNodeArena { name, params, ret: ret_node }),
span,
));
}
let mut else_fallback = None;
if self.peek().kind == TokenKind::Else {
self.advance();
else_fallback = Some(self.parse_block()?);
}
let body = self.parse_block()?;
let body_span = self.arena.span(body);
let span = Span::new(self.file_id, start_span.start, body_span.end);
Ok(self.arena.push(
NodeKind::FnDecl(FnDeclNodeArena {
vis,
name,
params,
ret,
else_fallback,
body,
}),
span,
))
}
fn parse_fn_decl(&mut self, vis: Option<String>) -> Result<NodeId, DiagnosticBundle> {
let start_span = self.consume(TokenKind::Fn)?.span;
let name = self.expect_identifier()?;
@@ -1074,8 +1154,14 @@ impl<'a> Parser<'a> {
self.consume(TokenKind::As)?;
let name = self.expect_identifier()?;
let body = self.parse_block()?;
// Corpo opcional do construtor
let body = if self.peek().kind == TokenKind::OpenBrace {
self.parse_block()?
} else {
// bloco vazio
let empty_span = Span::new(self.file_id, start_span.end, start_span.end);
self.arena.push(NodeKind::Block(BlockNodeArena { stmts: vec![], tail: None }), empty_span)
};
let span = Span::new(self.file_id, start_span.start, self.arena.span(body).end);
constructors.push(self.arena.push(
@@ -1169,10 +1255,7 @@ fn add(a: int, b: int): int {
#[test]
fn test_parse_type_decl() {
let source = r#"
pub declare struct Point {
x: int,
y: int
}
pub declare struct Point(x: int, y: int){}
"#;
let mut interner = NameInterner::new();
let mut parser = Parser::new(source, FileId(0), &mut interner);
@@ -1192,6 +1275,38 @@ pub declare struct Point {
}
}
#[test]
fn test_parse_type_body_method_with_body() {
let source = r#"
declare struct Vec2(x: int, y: int) {
fn getX(self: this): int {
return x;
}
}
"#;
let mut interner = NameInterner::new();
let mut parser = Parser::new(source, FileId(0), &mut interner);
let parsed = parser.parse_file().unwrap();
let file = match parsed.arena.kind(parsed.root) {
NodeKind::File(file) => file,
_ => panic!("Expected File"),
};
assert_eq!(file.decls.len(), 1);
let type_decl = match parsed.arena.kind(file.decls[0]) {
NodeKind::TypeDecl(t) => t,
_ => panic!("Expected TypeDecl"),
};
let body_id = type_decl.body.expect("Expected type body");
let body = match parsed.arena.kind(body_id) {
NodeKind::TypeBody(tb) => tb,
_ => panic!("Expected TypeBody"),
};
assert_eq!(body.methods.len(), 1);
assert!(matches!(parsed.arena.kind(body.methods[0]), NodeKind::FnDecl(_)));
}
#[test]
fn test_parse_service_decl() {
let source = r#"
@@ -908,8 +908,8 @@ mod tests {
#[test]
fn test_duplicate_symbols() {
let source = "
declare struct Foo {}
declare struct Foo {}
declare struct Foo()
declare struct Foo()
";
let (parsed, _, interner) = setup_test(source);
let mut collector = SymbolCollector::new(&interner);
@@ -923,7 +923,7 @@ mod tests {
#[test]
fn test_namespace_collision() {
let source = "
declare struct Foo {}
declare struct Foo()
fn Foo() {}
";
let (parsed, _, interner) = setup_test(source);
@@ -1268,7 +1268,7 @@ mod tests {
#[test]
fn test_lower_type_node_complex() {
let source = "
declare struct MyType {}
declare struct MyType()
fn main() {
let x: optional<int> = 1;
let y: result<int, string> = 2;
@@ -21,6 +21,9 @@ pub struct TypeChecker<'a> {
struct_methods: HashMap<String, HashMap<String, PbsType>>,
diagnostics: Vec<Diagnostic>,
contract_registry: ContractRegistry,
// Contexto atual de tipo (para resolver `this` e métodos/fields)
current_type_context: Option<String>,
current_struct_fields: Option<HashMap<String, PbsType>>,
}
impl<'a> TypeChecker<'a> {
@@ -43,6 +46,8 @@ impl<'a> TypeChecker<'a> {
struct_methods: HashMap::new(),
diagnostics: Vec::new(),
contract_registry: ContractRegistry::new(),
current_type_context: None,
current_struct_fields: None,
}
}
@@ -150,19 +155,39 @@ impl<'a> TypeChecker<'a> {
let mut methods = HashMap::new();
if let Some(body_node) = n.body {
if let NodeKind::TypeBody(body) = arena.kind(body_node) {
// Dentro do contexto do tipo, habilitar `this`
let prev_ctx = self.current_type_context.clone();
self.current_type_context = Some(type_name.clone());
for m in &body.methods {
if let NodeKind::ServiceFnSig(sig) = arena.kind(*m) {
let mut params = Vec::new();
for p in &sig.params {
params.push(self.resolve_type_node(arena, p.ty));
match arena.kind(*m) {
NodeKind::ServiceFnSig(sig) => {
let mut params = Vec::new();
for p in &sig.params {
params.push(self.resolve_type_node(arena, p.ty));
}
let m_ty = PbsType::Function {
params,
return_type: Box::new(self.resolve_type_node(arena, sig.ret)),
};
methods.insert(self.interner.resolve(sig.name).to_string(), m_ty);
}
let m_ty = PbsType::Function {
params,
return_type: Box::new(self.resolve_type_node(arena, sig.ret)),
};
methods.insert(self.interner.resolve(sig.name).to_string(), m_ty);
NodeKind::FnDecl(fn_decl) => {
let mut params = Vec::new();
for p in &fn_decl.params {
params.push(self.resolve_type_node(arena, p.ty));
}
let ret_ty = if let Some(ret) = fn_decl.ret {
self.resolve_type_node(arena, ret)
} else {
PbsType::Void
};
let m_ty = PbsType::Function { params, return_type: Box::new(ret_ty) };
methods.insert(self.interner.resolve(fn_decl.name).to_string(), m_ty);
}
_ => {}
}
}
self.current_type_context = prev_ctx;
}
}
self.struct_methods.insert(type_name, methods);
@@ -508,6 +533,11 @@ impl<'a> TypeChecker<'a> {
}
}
// Check implicit struct fields in method context
if let Some(ty) = self.resolve_ident_override(name_str) {
return ty;
}
// Check module symbols
if let Some(sym) = self.module_symbols.value_symbols.get(n.name) {
if let Some(ty) = &sym.ty {
@@ -749,7 +779,29 @@ impl<'a> TypeChecker<'a> {
self.struct_constants.insert(type_name, constants_map);
if let Some(body) = n.body {
// Preparar contexto de tipo e fields para métodos
let type_name = self.interner.resolve(n.name).to_string();
let prev_ctx = self.current_type_context.clone();
self.current_type_context = Some(type_name.clone());
// Coletar fields do cabeçalho e membros do corpo
let mut fields: HashMap<String, PbsType> = HashMap::new();
for p in &n.params {
let ty = self.resolve_type_node(arena, p.ty);
fields.insert(self.interner.resolve(p.name).to_string(), ty);
}
if let NodeKind::TypeBody(tb) = arena.kind(body) {
for m in &tb.members {
let ty = self.resolve_type_node(arena, m.ty);
fields.insert(self.interner.resolve(m.name).to_string(), ty);
}
}
let prev_fields = self.current_struct_fields.replace(fields);
self.check_node(arena, body);
self.current_struct_fields = prev_fields;
self.current_type_context = prev_ctx;
}
}
@@ -782,6 +834,20 @@ impl<'a> TypeChecker<'a> {
"string" => PbsType::String,
"void" => PbsType::Void,
"bounded" => PbsType::Bounded,
"this" => {
if let Some(ctx) = &self.current_type_context {
PbsType::Struct(ctx.clone())
} else {
self.diagnostics.push(Diagnostic {
severity: Severity::Error,
code: "E_TYPE_THIS_OUT_OF_CONTEXT".to_string(),
message: "`this` used outside of type context".to_string(),
span: arena.span(node),
related: Vec::new(),
});
PbsType::Void
}
}
"Color" | "ButtonState" | "Pad" | "Touch" => PbsType::Struct(name_str.to_string()),
_ => {
// Look up in symbol table
@@ -830,6 +896,16 @@ impl<'a> TypeChecker<'a> {
}
}
// Override de resolução de identificadores: permitir acesso direto a fields no contexto de método
fn resolve_ident_override(&self, name: &str) -> Option<PbsType> {
if let Some(fields) = &self.current_struct_fields {
if let Some(ty) = fields.get(name) {
return Some(ty.clone());
}
}
None
}
fn lookup_type(&self, name: NameId) -> Option<&Symbol> {
if let Some(sym) = self.module_symbols.type_symbols.get(name) {
return Some(sym);
@@ -960,12 +1036,20 @@ mod tests {
Ok(_) => Ok(()),
Err(bundle) => {
let mut errors = Vec::new();
let full_src = fs::read_to_string(&file_path).unwrap();
let mut arounds = Vec::new();
for diag in bundle.diagnostics {
let code = diag.code;
errors.push(format!("{}: {}", code, diag.message));
let span = diag.span;
errors.push(format!("{}: {} @ {}..{}", code, diag.message, span.start, span.end));
let s = span.start as usize;
let e = span.end as usize;
let a = s.saturating_sub(30);
let b = (e + 30).min(full_src.len());
arounds.push(format!("[{}..{}]: >>>{}<<<", s, e, &full_src[a..b].replace('\n', "\\n")));
}
let err_msg = errors.join(", ");
println!("Compilation failed: {}", err_msg);
println!("Compilation failed: {}\n{}", err_msg, arounds.join("\n"));
Err(err_msg)
}
}
@@ -1118,7 +1202,7 @@ mod tests {
#[test]
fn test_struct_type_usage() {
let code = "
declare struct Point { x: int, y: int }
declare struct Point(x: int, y: int)
fn foo(p: Point) {}
fn main() {
// Struct literals not in v0, but we can have variables of struct type
@@ -1214,4 +1298,32 @@ mod tests {
if let Err(e) = &res { println!("Error: {}", e); }
assert!(res.is_ok());
}
#[test]
fn test_struct_method_with_body_and_call() {
let code = r#"
// struct Color
declare struct Vec2(x: int, y: int)
[
(x: int, y: int): (x, y) as default
(s: int): (s, s) as square
]
[[
ZERO: square(0)
]]
{
pub fn len(self: this): int {
return x * x + y * y;
}
}
fn frame(): void {
let zero = Vec2.ZERO;
let zz = zero.len();
}
"#;
let res = check_code(code);
if let Err(e) = &res { println!("Error: {}", e); }
assert!(res.is_ok());
}
}
@@ -41,7 +41,7 @@ pub fn lower_module(
pub fn lower_function(
core_func: &ir_core::Function,
program: &ir_core::Program,
function_returns: &HashMap<ir_core::ids::FunctionId, ir_core::Type>
function_returns: &HashMap<ir_core::ids::FunctionId, ir_core::Type>,
) -> Result<ir_vm::Function> {
let mut vm_func = ir_vm::Function {
id: core_func.id,
@@ -240,7 +240,7 @@ pub fn lower_function(
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Not, span.clone()));
}
ir_core::InstrKind::Alloc { ty, slots } => {
stack_types.push(ir_core::Type::Struct("".to_string())); // It's a gate
stack_types.push(ir_core::Type::Contract(format!("Gate<{}>", ty.0)));
vm_func.body.push(ir_vm::Instruction::new(ir_vm::InstrKind::Alloc {
type_id: ir_vm::TypeId(ty.0),
slots: *slots
@@ -368,14 +368,7 @@ pub fn lower_function(
fn is_gate_type(ty: &ir_core::Type) -> bool {
match ty {
ir_core::Type::Struct(_) |
ir_core::Type::Array(_, _) |
ir_core::Type::Optional(_) |
ir_core::Type::Result(_, _) |
ir_core::Type::Service(_) |
ir_core::Type::Contract(_) |
ir_core::Type::ErrorType(_) |
ir_core::Type::Function { .. } => true,
ir_core::Type::Contract(name) => name.starts_with("Gate<"),
_ => false,
}
}
+2 -2
View File
@@ -251,8 +251,8 @@ mod tests {
let module_dir = dir.path().join("math");
fs::create_dir_all(&module_dir).unwrap();
fs::write(module_dir.join("Vector.pbs"), "pub declare struct Vector {}").unwrap();
fs::write(module_dir.join("Internal.pbs"), "declare struct Hidden {}").unwrap();
fs::write(module_dir.join("Vector.pbs"), "pub declare struct Vector()").unwrap();
fs::write(module_dir.join("Internal.pbs"), "declare struct Hidden()").unwrap();
let mut fm = FileManager::new();
let exports = build_exports(&module_dir, &mut fm).unwrap();