This commit is contained in:
Nilton Constantino
2026-01-31 01:18:42 +00:00
parent ef8b7b524a
commit 180c7e19e0
8 changed files with 242 additions and 28 deletions
@@ -7,6 +7,12 @@ use crate::ir_core::ids::{FieldId, FunctionId, TypeId, ValueId};
use crate::ir_core::{Block, Function, Instr, Module, Param, Program, Terminator, Type};
use std::collections::HashMap;
#[derive(Clone)]
struct LocalInfo {
slot: u32,
ty: Type,
}
pub struct Lowerer<'a> {
module_symbols: &'a ModuleSymbols,
program: Program,
@@ -15,7 +21,7 @@ pub struct Lowerer<'a> {
next_block_id: u32,
next_func_id: u32,
next_type_id: u32,
local_vars: Vec<HashMap<String, u32>>,
local_vars: Vec<HashMap<String, LocalInfo>>,
function_ids: HashMap<String, FunctionId>,
type_ids: HashMap<String, TypeId>,
struct_slots: HashMap<String, u32>,
@@ -28,6 +34,12 @@ impl<'a> Lowerer<'a> {
let mut field_offsets = HashMap::new();
field_offsets.insert(FieldId(0), 0); // V0 hardcoded field resolution foundation
let mut struct_slots = HashMap::new();
struct_slots.insert("Color".to_string(), 1);
struct_slots.insert("ButtonState".to_string(), 4);
struct_slots.insert("Pad".to_string(), 48);
struct_slots.insert("Touch".to_string(), 6);
Self {
module_symbols,
program: Program {
@@ -44,7 +56,7 @@ impl<'a> Lowerer<'a> {
local_vars: Vec::new(),
function_ids: HashMap::new(),
type_ids: HashMap::new(),
struct_slots: HashMap::new(),
struct_slots,
contract_registry: ContractRegistry::new(),
diagnostics: Vec::new(),
}
@@ -342,9 +354,32 @@ impl<'a> Lowerer<'a> {
fn lower_let_stmt(&mut self, n: &LetStmtNode) -> Result<(), ()> {
self.lower_node(&n.init)?;
let ty = if let Some(ty_node) = &n.ty {
self.lower_type_node(ty_node)
} else {
// Very basic inference for host calls
if let Node::Call(call) = &*n.init {
if let Node::MemberAccess(ma) = &*call.callee {
if let Node::Ident(obj) = &*ma.object {
match (obj.name.as_str(), ma.member.as_str()) {
("Input", "pad") => Type::Struct("Pad".to_string()),
("Input", "touch") => Type::Struct("Touch".to_string()),
_ => Type::Int,
}
} else { Type::Int }
} else { Type::Int }
} else { Type::Int }
};
let slot = self.get_next_local_slot();
self.local_vars.last_mut().unwrap().insert(n.name.clone(), slot);
self.emit(Instr::SetLocal(slot));
let slots = self.get_type_slots(&ty);
self.local_vars.last_mut().unwrap().insert(n.name.clone(), LocalInfo { slot, ty });
for i in (0..slots).rev() {
self.emit(Instr::SetLocal(slot + i));
}
Ok(())
}
@@ -357,8 +392,11 @@ impl<'a> Lowerer<'a> {
}
fn lower_ident(&mut self, n: &IdentNode) -> Result<(), ()> {
if let Some(slot) = self.lookup_local(&n.name) {
self.emit(Instr::GetLocal(slot));
if let Some(info) = self.find_local(&n.name) {
let slots = self.get_type_slots(&info.ty);
for i in 0..slots {
self.emit(Instr::GetLocal(info.slot + i));
}
Ok(())
} else {
// Check for special identifiers
@@ -407,8 +445,8 @@ impl<'a> Lowerer<'a> {
"TRANSPARENT" => 0x0000,
"COLOR_KEY" => 0x0000,
_ => {
self.error("E_RESOLVE_UNDEFINED", format!("Undefined Color constant '{}'", n.member), n.span);
return Err(());
// Check if it's a method call like Color.rgb, handled in lower_call
return Ok(());
}
};
let id = self.program.const_pool.add_int(val);
@@ -417,18 +455,94 @@ impl<'a> Lowerer<'a> {
}
}
// For instance members (e.g., p.any), we'll just ignore for now in v0
// to pass typecheck tests if they are being lowered.
// In a real implementation we would need type information.
if let Some((slot, ty)) = self.resolve_member_access(n) {
let slots = self.get_type_slots(&ty);
for i in 0..slots {
self.emit(Instr::GetLocal(slot + i));
}
return Ok(());
}
Ok(())
}
fn lower_call(&mut self, n: &CallNode) -> Result<(), ()> {
for arg in &n.args {
self.lower_node(arg)?;
fn resolve_member_access(&self, n: &MemberAccessNode) -> Option<(u32, Type)> {
match &*n.object {
Node::Ident(id) => {
let info = self.find_local(&id.name)?;
if let Type::Struct(sname) = &info.ty {
let offset = self.get_field_offset(sname, &n.member);
let ty = self.get_field_type(sname, &n.member);
Some((info.slot + offset, ty))
} else { None }
}
Node::MemberAccess(inner) => {
let (base_slot, ty) = self.resolve_member_access(inner)?;
if let Type::Struct(sname) = &ty {
let offset = self.get_field_offset(sname, &n.member);
let final_ty = self.get_field_type(sname, &n.member);
Some((base_slot + offset, final_ty))
} else { None }
}
_ => None
}
}
fn get_field_offset(&self, struct_name: &str, field_name: &str) -> u32 {
match struct_name {
"ButtonState" => match field_name {
"pressed" => 0,
"released" => 1,
"down" => 2,
"hold_frames" => 3,
_ => 0,
},
"Pad" => match field_name {
"up" => 0,
"down" => 4,
"left" => 8,
"right" => 12,
"a" => 16,
"b" => 20,
"x" => 24,
"y" => 28,
"l" => 32,
"r" => 36,
"start" => 40,
"select" => 44,
_ => 0,
},
"Touch" => match field_name {
"f" => 0,
"x" => 4,
"y" => 5,
_ => 0,
},
_ => 0,
}
}
fn get_field_type(&self, struct_name: &str, field_name: &str) -> Type {
match struct_name {
"Pad" => Type::Struct("ButtonState".to_string()),
"ButtonState" => match field_name {
"hold_frames" => Type::Bounded,
_ => Type::Bool,
},
"Touch" => match field_name {
"f" => Type::Struct("ButtonState".to_string()),
_ => Type::Int,
},
_ => Type::Int,
}
}
fn lower_call(&mut self, n: &CallNode) -> Result<(), ()> {
match &*n.callee {
Node::Ident(id_node) => {
for arg in &n.args {
self.lower_node(arg)?;
}
if let Some(func_id) = self.function_ids.get(&id_node.name) {
self.emit(Instr::Call(*func_id, n.args.len() as u32));
Ok(())
@@ -436,9 +550,6 @@ impl<'a> Lowerer<'a> {
// Check for special built-in functions
match id_node.name.as_str() {
"some" | "ok" | "err" => {
// For now, these are effectively nops in terms of IR emission,
// as they just wrap the already pushed arguments.
// In a real implementation, they might push a tag.
return Ok(());
}
_ => {}
@@ -449,14 +560,59 @@ impl<'a> Lowerer<'a> {
}
}
Node::MemberAccess(ma) => {
// Check for Pad.any()
if ma.member == "any" {
if let Node::Ident(obj_id) = &*ma.object {
if let Some(info) = self.find_local(&obj_id.name) {
if let Type::Struct(sname) = &info.ty {
if sname == "Pad" {
self.lower_pad_any(info.slot);
return Ok(());
}
}
}
}
}
// Check for Color.rgb
if ma.member == "rgb" {
if let Node::Ident(obj_id) = &*ma.object {
if obj_id.name == "Color" {
if n.args.len() == 3 {
// Try to get literal values for r, g, b
let mut literals = Vec::new();
for arg in &n.args {
if let Node::IntLiteral(lit) = arg {
literals.push(Some(lit.value));
} else {
literals.push(None);
}
}
if let (Some(r), Some(g), Some(b)) = (literals[0], literals[1], literals[2]) {
let r5 = (r & 0xFF) >> 3;
let g6 = (g & 0xFF) >> 2;
let b5 = (b & 0xFF) >> 3;
let rgb565 = (r5 << 11) | (g6 << 5) | b5;
let id = self.program.const_pool.add_int(rgb565);
self.emit(Instr::PushConst(id));
return Ok(());
}
}
}
}
}
for arg in &n.args {
self.lower_node(arg)?;
}
if let Node::Ident(obj_id) = &*ma.object {
// Check if it's a host contract according to symbol table
let is_host_contract = self.module_symbols.type_symbols.get(&obj_id.name)
.map(|sym| sym.kind == SymbolKind::Contract && sym.is_host)
.unwrap_or(false);
// Ensure it's not shadowed by a local variable
let is_shadowed = self.lookup_local(&obj_id.name).is_some();
let is_shadowed = self.find_local(&obj_id.name).is_some();
if is_host_contract && !is_shadowed {
if let Some(syscall_id) = self.contract_registry.resolve(&obj_id.name, &ma.member) {
@@ -469,18 +625,33 @@ impl<'a> Lowerer<'a> {
}
}
// Regular member call (method) or fallback
// In v0 we don't handle this yet.
self.error("E_LOWER_UNSUPPORTED", "Method calls not supported in v0".to_string(), ma.span);
Err(())
}
_ => {
for arg in &n.args {
self.lower_node(arg)?;
}
self.error("E_LOWER_UNSUPPORTED", "Indirect calls not supported in v0".to_string(), n.callee.span());
Err(())
}
}
}
fn lower_pad_any(&mut self, base_slot: u32) {
for i in 0..12 {
let btn_base = base_slot + (i * 4);
self.emit(Instr::GetLocal(btn_base)); // pressed
self.emit(Instr::GetLocal(btn_base + 1)); // released
self.emit(Instr::Or);
self.emit(Instr::GetLocal(btn_base + 2)); // down
self.emit(Instr::Or);
if i > 0 {
self.emit(Instr::Or);
}
}
}
fn lower_binary(&mut self, n: &BinaryNode) -> Result<(), ()> {
self.lower_node(&n.left)?;
self.lower_node(&n.right)?;
@@ -550,6 +721,7 @@ impl<'a> Lowerer<'a> {
match node {
Node::TypeName(n) => match n.name.as_str() {
"int" => Type::Int,
"bounded" => Type::Bounded,
"float" => Type::Float,
"bool" => Type::Bool,
"string" => Type::String,
@@ -624,17 +796,25 @@ impl<'a> Lowerer<'a> {
}
fn get_next_local_slot(&self) -> u32 {
self.local_vars.iter().map(|s| s.len() as u32).sum()
self.local_vars.iter().flat_map(|s| s.values()).map(|info| self.get_type_slots(&info.ty)).sum()
}
fn lookup_local(&self, name: &str) -> Option<u32> {
fn find_local(&self, name: &str) -> Option<LocalInfo> {
for scope in self.local_vars.iter().rev() {
if let Some(slot) = scope.get(name) {
return Some(*slot);
if let Some(info) = scope.get(name) {
return Some(info.clone());
}
}
None
}
fn get_type_slots(&self, ty: &Type) -> u32 {
match ty {
Type::Struct(name) => self.struct_slots.get(name).cloned().unwrap_or(1),
Type::Array(_, size) => *size,
_ => 1,
}
}
}
#[cfg(test)]
@@ -5,6 +5,7 @@ use std::fmt;
pub enum Type {
Void,
Int,
Bounded,
Float,
Bool,
String,
@@ -26,6 +27,7 @@ impl fmt::Display for Type {
match self {
Type::Void => write!(f, "void"),
Type::Int => write!(f, "int"),
Type::Bounded => write!(f, "bounded"),
Type::Float => write!(f, "float"),
Type::Bool => write!(f, "bool"),
Type::String => write!(f, "string"),
@@ -29,6 +29,7 @@ pub enum Type {
Null,
Bool,
Int,
Bounded,
Float,
String,
Color,