many improvements on VM and tooling

This commit is contained in:
Nilton Constantino
2026-01-23 09:33:44 +00:00
parent a7b824fe21
commit a7817534d2
36 changed files with 5261 additions and 558 deletions
+245 -23
View File
@@ -2,7 +2,10 @@ use crate::codegen::ast_util;
use crate::codegen::{input_map, syscall_map};
use crate::compiler::Symbol;
use anyhow::{anyhow, Result};
use oxc_allocator::Vec as OXCVec;
use oxc_ast::ast::*;
use oxc_ast_visit::{walk, Visit};
use oxc_syntax::scope::ScopeFlags;
use oxc_span::{GetSpan, Span};
use prometeu_bytecode::asm;
use prometeu_bytecode::asm::{assemble, Asm, Operand};
@@ -12,6 +15,48 @@ use prometeu_core::hardware::Syscall;
use prometeu_core::model::Color;
use std::collections::HashMap;
/// Helper to count local variables and hoisted functions in a function body.
struct LocalCounter {
count: u32,
}
impl<'a> Visit<'a> for LocalCounter {
fn visit_statement(&mut self, stmt: &Statement<'a>) {
match stmt {
Statement::FunctionDeclaration(f) => {
self.visit_function(f, ScopeFlags::empty());
}
_ => walk::walk_statement(self, stmt),
}
}
fn visit_variable_declaration(&mut self, decl: &VariableDeclaration<'a>) {
self.count += decl.declarations.len() as u32;
walk::walk_variable_declaration(self, decl);
}
fn visit_function(&mut self, f: &Function<'a>, _flags: ScopeFlags) {
if f.id.is_some() {
self.count += 1;
}
// Stop recursion: nested functions have their own frames and locals.
}
}
/// Metadata for a symbol (variable or function) in the symbol table.
#[allow(dead_code)]
struct SymbolEntry {
name: String,
slot_index: u32,
scope_depth: usize,
is_const: bool,
is_initialized: bool,
// PC of the instruction where the symbol was declared/initialized.
// Useful for debug symbols and potentially for more advanced TDZ checks.
declared_at_pc: u32,
initialized_at_pc: u32,
}
/// The core Code Generator for the Prometeu Compiler.
///
/// It maintains the state of the compilation process, including symbol tables,
@@ -26,8 +71,10 @@ pub struct Codegen {
/// The stream of generated assembly instructions.
/// The boolean indicates if the instruction should have a debug symbol attached.
instructions: Vec<(Asm, bool)>,
/// Mapping of local variable names to their stack offsets in the current frame.
locals: HashMap<String, u32>,
/// Scoped symbol table. Each element is a scope level containing a map of symbols.
symbol_table: Vec<HashMap<String, SymbolEntry>>,
/// Current depth of the scope (0 is global/function top-level).
scope_depth: usize,
/// Mapping of global variable names to their slots in the VM's global memory.
globals: HashMap<String, u32>,
/// The Constant Pool, which stores unique values (strings, large numbers).
@@ -48,7 +95,8 @@ impl Codegen {
source_text,
symbols: Vec::new(),
instructions: Vec::new(),
locals: HashMap::new(),
symbol_table: Vec::new(),
scope_depth: 0,
globals: HashMap::new(),
constant_pool: vec![ConstantPoolEntry::Null], // Index 0 is always Null
next_local: 0,
@@ -57,6 +105,95 @@ impl Codegen {
}
}
/// Enters a new scope level.
fn enter_scope(&mut self) {
self.symbol_table.push(HashMap::new());
self.scope_depth += 1;
}
/// Exits the current scope level.
fn exit_scope(&mut self) {
self.symbol_table.pop();
self.scope_depth -= 1;
}
/// Declares a new symbol in the current scope.
fn declare_symbol(&mut self, name: String, is_const: bool, is_initialized: bool, span: Span) -> Result<&mut SymbolEntry> {
let current_scope = self.symbol_table.last_mut().ok_or_else(|| anyhow!("No active scope"))?;
if current_scope.contains_key(&name) {
return Err(anyhow!("Variable '{}' already declared in this scope at {:?}", name, span));
}
let slot_index = self.next_local;
self.next_local += 1;
let entry = SymbolEntry {
name: name.clone(),
slot_index,
scope_depth: self.scope_depth,
is_const,
is_initialized,
declared_at_pc: 0, // Will be filled if needed
initialized_at_pc: 0,
};
current_scope.insert(name.clone(), entry);
Ok(self.symbol_table.last_mut().unwrap().get_mut(&name).unwrap())
}
/// Marks a symbol as initialized.
fn initialize_symbol(&mut self, name: &str) {
for scope in self.symbol_table.iter_mut().rev() {
if let Some(entry) = scope.get_mut(name) {
entry.is_initialized = true;
return;
}
}
}
/// Resolves a symbol name to its entry, searching from inner to outer scopes.
fn resolve_symbol(&self, name: &str) -> Option<&SymbolEntry> {
for scope in self.symbol_table.iter().rev() {
if let Some(entry) = scope.get(name) {
return Some(entry);
}
}
None
}
/// Discovers all function declarations in a program, including nested ones.
fn discover_functions<'a>(
&self,
file: String,
source: String,
program: &'a Program<'a>,
all_functions: &mut Vec<(String, String, &'a Function<'a>)>,
) {
struct Collector<'a, 'b> {
file: String,
source: String,
functions: &'b mut Vec<(String, String, &'a Function<'a>)>,
}
impl<'a, 'b> Visit<'a> for Collector<'a, 'b> {
fn visit_function(&mut self, f: &Function<'a>, flags: ScopeFlags) {
// Safety: The program AST lives long enough as it's owned by the caller
// of compile_programs and outlives the compilation process.
let f_ref = unsafe { std::mem::transmute::<&Function<'a>, &'a Function<'a>>(f) };
self.functions.push((self.file.clone(), self.source.clone(), f_ref));
walk::walk_function(self, f, flags);
}
}
let mut collector = Collector {
file,
source,
functions: all_functions,
};
collector.visit_program(program);
}
/// Adds a value to the Constant Pool and returns its index.
/// If the value already exists, it returns the existing index.
fn add_constant(&mut self, entry: ConstantPoolEntry) -> u32 {
@@ -89,18 +226,14 @@ impl Codegen {
// and global variables before starting code generation. This allows a
// function to call another that was defined later or in another file.
let mut all_functions = Vec::new();
for (file, source, program) in &programs {
// Collect global variables and discover all functions (including nested)
for item in &program.body {
match item {
// Standard function declaration: function foo() { ... }
Statement::FunctionDeclaration(f) => {
all_functions.push((file.clone(), source.clone(), f.as_ref()));
}
// Exported declaration: export function foo() ... or export const x = 1;
Statement::ExportNamedDeclaration(decl) => {
if let Some(Declaration::FunctionDeclaration(f)) = &decl.declaration {
all_functions.push((file.clone(), source.clone(), f.as_ref()));
} else if let Some(Declaration::VariableDeclaration(var)) = &decl.declaration {
if let Some(Declaration::VariableDeclaration(var)) = &decl.declaration {
self.export_global_variable_declarations(&var);
}
}
@@ -111,6 +244,9 @@ impl Codegen {
_ => {}
}
}
// Discovery Pass for all functions in this program
self.discover_functions(file.clone(), source.clone(), program, &mut all_functions);
}
// --- ENTRY POINT VERIFICATION ---
@@ -253,23 +389,35 @@ impl Codegen {
/// 3. The body is compiled sequentially.
/// 4. `PopScope` and `Push Null` are executed before `Ret` to ensure the stack rule.
fn compile_function(&mut self, f: &Function) -> Result<()> {
self.locals.clear();
self.symbol_table.clear();
self.scope_depth = 0;
self.next_local = 0;
// Start scope for parameters and local variables
self.enter_scope();
self.emit_op(OpCode::PushScope, vec![], f.span);
// Map parameters to locals (they are pushed by the caller before the Call instruction)
for param in &f.params.items {
if let BindingPattern::BindingIdentifier(ident) = &param.pattern {
let name = ident.name.to_string();
let id = self.next_local;
self.locals.insert(name, id);
self.next_local += 1;
// Parameters are considered initialized
self.declare_symbol(name, false, true, ident.span)?;
}
}
if let Some(body) = &f.body {
// Reserve slots for all local variables and hoisted functions
let locals_to_reserve = self.count_locals(&body.statements);
for _ in 0..locals_to_reserve {
// Initializing with I32 0 as it's the safest default for Prometeu VM
self.emit_op(OpCode::PushI32, vec![Operand::I32(0)], f.span);
}
// Function and Variable hoisting within the function scope
self.hoist_functions(&body.statements)?;
self.hoist_variables(&body.statements)?;
for stmt in &body.statements {
self.compile_stmt(stmt)?;
}
@@ -282,23 +430,75 @@ impl Codegen {
Ok(())
}
/// Counts the total number of local variable and function declarations in a function body.
fn count_locals(&self, statements: &OXCVec<Statement>) -> u32 {
let mut counter = LocalCounter { count: 0 };
for stmt in statements {
counter.visit_statement(stmt);
}
counter.count
}
/// Hoists function declarations to the top of the current scope.
fn hoist_functions(&mut self, statements: &OXCVec<Statement>) -> Result<()> {
for stmt in statements {
if let Statement::FunctionDeclaration(f) = stmt {
if let Some(ident) = &f.id {
let name = ident.name.to_string();
// Functions are hoisted and already considered initialized
self.declare_symbol(name, false, true, ident.span)?;
}
}
}
Ok(())
}
/// Hoists variable declarations (let/const) to the top of the current scope.
fn hoist_variables(&mut self, statements: &OXCVec<Statement>) -> Result<()> {
for stmt in statements {
if let Statement::VariableDeclaration(var) = stmt {
let is_const = var.kind == VariableDeclarationKind::Const;
for decl in &var.declarations {
if let BindingPattern::BindingIdentifier(ident) = &decl.id {
let name = ident.name.to_string();
// Register as uninitialized for TDZ
self.declare_symbol(name, is_const, false, ident.span)?;
}
}
}
}
Ok(())
}
/// Translates a Statement into bytecode.
fn compile_stmt(&mut self, stmt: &Statement) -> Result<()> {
match stmt {
// var x = 10;
Statement::VariableDeclaration(var) => {
let is_const = var.kind == VariableDeclarationKind::Const;
for decl in &var.declarations {
if let BindingPattern::BindingIdentifier(ident) = &decl.id {
let name = ident.name.to_string();
// Variable should already be in the symbol table due to hoisting
let entry = self.resolve_symbol(&name)
.ok_or_else(|| anyhow!("Internal compiler error: symbol '{}' not hoisted at {:?}", name, ident.span))?;
let slot_index = entry.slot_index;
if let Some(init) = &decl.init {
self.compile_expr(init)?;
self.emit_op(OpCode::SetLocal, vec![Operand::U32(slot_index)], decl.span);
self.initialize_symbol(&name);
} else {
if is_const {
return Err(anyhow!("Missing initializer in const declaration at {:?}", decl.span));
}
// Default initialization to 0
self.emit_op(OpCode::PushI32, vec![Operand::I32(0)], decl.span);
self.emit_op(OpCode::SetLocal, vec![Operand::U32(slot_index)], decl.span);
self.initialize_symbol(&name);
}
let id = self.next_local;
self.locals.insert(name, id);
self.next_local += 1;
}
}
}
@@ -328,12 +528,22 @@ impl Codegen {
}
// { let x = 1; }
Statement::BlockStatement(block) => {
self.enter_scope();
self.emit_op(OpCode::PushScope, vec![], block.span);
// Hoist functions and variables in the block
self.hoist_functions(&block.body)?;
self.hoist_variables(&block.body)?;
for stmt in &block.body {
self.compile_stmt(stmt)?;
}
self.emit_op(OpCode::PopScope, vec![], block.span);
self.exit_scope();
}
// Function declarations are handled by hoisting and compiled separately
Statement::FunctionDeclaration(_) => {}
_ => return Err(anyhow!("Unsupported statement type at {:?}", stmt.span())),
}
Ok(())
@@ -365,8 +575,11 @@ impl Codegen {
// Variable access: resolve to GetLocal or GetGlobal
Expression::Identifier(ident) => {
let name = ident.name.to_string();
if let Some(&id) = self.locals.get(&name) {
self.emit_op(OpCode::GetLocal, vec![Operand::U32(id)], ident.span);
if let Some(entry) = self.resolve_symbol(&name) {
if !entry.is_initialized {
return Err(anyhow!("TDZ Violation: Variable '{}' accessed before initialization at {:?}", name, ident.span));
}
self.emit_op(OpCode::GetLocal, vec![Operand::U32(entry.slot_index)], ident.span);
} else if let Some(&id) = self.globals.get(&name) {
self.emit_op(OpCode::GetGlobal, vec![Operand::U32(id)], ident.span);
} else {
@@ -377,11 +590,20 @@ impl Codegen {
Expression::AssignmentExpression(assign) => {
if let AssignmentTarget::AssignmentTargetIdentifier(ident) = &assign.left {
let name = ident.name.to_string();
self.compile_expr(&assign.right)?;
if let Some(&id) = self.locals.get(&name) {
self.emit_op(OpCode::SetLocal, vec![Operand::U32(id)], assign.span);
self.emit_op(OpCode::GetLocal, vec![Operand::U32(id)], assign.span); // Assignment returns the value
if let Some(entry) = self.resolve_symbol(&name) {
if entry.is_const {
return Err(anyhow!("Assignment to constant variable '{}' at {:?}", name, assign.span));
}
if !entry.is_initialized {
return Err(anyhow!("TDZ Violation: Variable '{}' accessed before initialization at {:?}", name, assign.span));
}
let slot_index = entry.slot_index;
self.compile_expr(&assign.right)?;
self.emit_op(OpCode::SetLocal, vec![Operand::U32(slot_index)], assign.span);
self.emit_op(OpCode::GetLocal, vec![Operand::U32(slot_index)], assign.span); // Assignment returns the value
} else if let Some(&id) = self.globals.get(&name) {
self.compile_expr(&assign.right)?;
self.emit_op(OpCode::SetGlobal, vec![Operand::U32(id)], assign.span);
self.emit_op(OpCode::GetGlobal, vec![Operand::U32(id)], assign.span);
} else {
@@ -0,0 +1,37 @@
use anyhow::anyhow;
use prometeu_core::model::ButtonId;
/// Mapping of physical button names to their virtual Button ID.
///
/// These constants match the `ButtonId` enum in `prometeu-core`.
pub const BTN_UP: u32 = ButtonId::Up as u32;
pub const BTN_DOWN: u32 = ButtonId::Down as u32;
pub const BTN_LEFT: u32 = ButtonId::Left as u32;
pub const BTN_RIGHT: u32 = ButtonId::Right as u32;
pub const BTN_A: u32 = ButtonId::A as u32;
pub const BTN_B: u32 = ButtonId::B as u32;
pub const BTN_X: u32 = ButtonId::X as u32;
pub const BTN_Y: u32 = ButtonId::Y as u32;
pub const BTN_L: u32 = ButtonId::L as u32;
pub const BTN_R: u32 = ButtonId::R as u32;
pub const BTN_START: u32 = ButtonId::Start as u32;
pub const BTN_SELECT: u32 = ButtonId::Select as u32;
/// Translates a string identifier (e.g., "up") into a numeric Button ID.
pub fn map_btn_name(btn_name: &str) -> anyhow::Result<u32> {
match btn_name.to_lowercase().as_str() {
"up" => Ok(BTN_UP),
"down" => Ok(BTN_DOWN),
"left" => Ok(BTN_LEFT),
"right" => Ok(BTN_RIGHT),
"a" => Ok(BTN_A),
"b" => Ok(BTN_B),
"x" => Ok(BTN_X),
"y" => Ok(BTN_Y),
"l" => Ok(BTN_L),
"r" => Ok(BTN_R),
"start" => Ok(BTN_START),
"select" => Ok(BTN_SELECT),
_ => Err(anyhow!("Unsupported button: {}. Expected one of: up, down, left, right, a, b, x, y, l, r, start, select.", btn_name)),
}
}
@@ -3,6 +3,7 @@ use crate::codegen::syscall_map;
use anyhow::{anyhow, Result};
use oxc_ast::ast::*;
use oxc_ast_visit::{walk, Visit};
use oxc_syntax::scope::ScopeFlags;
use oxc_span::GetSpan;
/// AST Visitor that ensures the source code follows the Prometeu subset of JS/TS.
@@ -29,42 +30,9 @@ impl Validator {
local_functions: std::collections::HashSet::new(),
};
// 1. Discovery Pass: Collect all function names and imports
for item in &program.body {
match item {
Statement::FunctionDeclaration(f) => {
if let Some(ident) = &f.id {
validator.local_functions.insert(ident.name.to_string());
}
}
Statement::ExportNamedDeclaration(decl) => {
if let Some(Declaration::FunctionDeclaration(f)) = &decl.declaration {
if let Some(ident) = &f.id {
validator.local_functions.insert(ident.name.to_string());
}
}
}
Statement::ImportDeclaration(decl) => {
if let Some(specifiers) = &decl.specifiers {
for specifier in specifiers {
match specifier {
ImportDeclarationSpecifier::ImportSpecifier(s) => {
validator.local_functions.insert(s.local.name.to_string());
}
ImportDeclarationSpecifier::ImportDefaultSpecifier(s) => {
validator.local_functions.insert(s.local.name.to_string());
}
ImportDeclarationSpecifier::ImportNamespaceSpecifier(s) => {
validator.local_functions.insert(s.local.name.to_string());
}
}
}
}
}
_ => {}
}
}
// 1. Discovery Pass: Collect all function names and imports recursively
validator.discover_functions(program);
// 2. Traversal Pass: Check every node for compatibility
validator.visit_program(program);
@@ -74,28 +42,43 @@ impl Validator {
Err(anyhow!("Validation errors:\n{}", validator.errors.join("\n")))
}
}
/// Recursively discovers all function declarations in the program.
fn discover_functions(&mut self, program: &Program) {
struct FunctionDiscoverer<'a> {
functions: &'a mut std::collections::HashSet<String>,
}
impl<'a, 'b> Visit<'b> for FunctionDiscoverer<'a> {
fn visit_function(&mut self, f: &Function<'b>, _flags: ScopeFlags) {
if let Some(ident) = &f.id {
self.functions.insert(ident.name.to_string());
}
walk::walk_function(self, f, _flags);
}
fn visit_import_declaration(&mut self, decl: &ImportDeclaration<'b>) {
if let Some(specifiers) = &decl.specifiers {
for specifier in specifiers {
match specifier {
ImportDeclarationSpecifier::ImportSpecifier(s) => {
self.functions.insert(s.local.name.to_string());
}
ImportDeclarationSpecifier::ImportDefaultSpecifier(s) => {
self.functions.insert(s.local.name.to_string());
}
ImportDeclarationSpecifier::ImportNamespaceSpecifier(s) => {
self.functions.insert(s.local.name.to_string());
}
}
}
}
}
}
let mut discoverer = FunctionDiscoverer { functions: &mut self.local_functions };
discoverer.visit_program(program);
}
}
impl<'a> Visit<'a> for Validator {
/// Validates that only supported statements are used.
fn visit_statement(&mut self, stmt: &Statement<'a>) {
match stmt {
Statement::VariableDeclaration(_) |
Statement::ExpressionStatement(_) |
Statement::IfStatement(_) |
Statement::BlockStatement(_) |
Statement::ExportNamedDeclaration(_) |
Statement::ImportDeclaration(_) |
Statement::FunctionDeclaration(_) => {
// These are the only statements the PVM handles currently.
walk::walk_statement(self, stmt);
}
_ => {
self.errors.push(format!("Unsupported statement type at {:?}. Note: Prometeu does not support while/for loops or classes yet.", stmt.span()));
}
}
}
/// Validates that only supported expressions are used.
fn visit_expression(&mut self, expr: &Expression<'a>) {
match expr {
@@ -119,6 +102,17 @@ impl<'a> Visit<'a> for Validator {
}
}
fn visit_call_expression(&mut self, expr: &CallExpression<'a>) {
if let Ok(name) = ast_util::get_callee_name(&expr.callee) {
if syscall_map::map_syscall(&name).is_none() && !self.local_functions.contains(&name) {
self.errors.push(format!("Unsupported function call: {} at {:?}", name, expr.span));
}
} else {
self.errors.push(format!("Unsupported callee expression at {:?}", expr.callee.span()));
}
walk::walk_call_expression(self, expr);
}
fn visit_unary_expression(&mut self, expr: &UnaryExpression<'a>) {
match expr.operator {
UnaryOperator::UnaryNegation |
@@ -152,17 +146,6 @@ impl<'a> Visit<'a> for Validator {
}
}
fn visit_call_expression(&mut self, expr: &CallExpression<'a>) {
if let Ok(name) = ast_util::get_callee_name(&expr.callee) {
if syscall_map::map_syscall(&name).is_none() && !self.local_functions.contains(&name) {
self.errors.push(format!("Unsupported function call: {} at {:?}", name, expr.span));
}
} else {
self.errors.push(format!("Unsupported callee expression at {:?}", expr.callee.span()));
}
walk::walk_call_expression(self, expr);
}
fn visit_logical_expression(&mut self, expr: &LogicalExpression<'a>) {
match expr.operator {
LogicalOperator::And |
@@ -174,4 +157,24 @@ impl<'a> Visit<'a> for Validator {
}
}
}
/// Validates that only supported statements are used.
fn visit_statement(&mut self, stmt: &Statement<'a>) {
match stmt {
Statement::VariableDeclaration(_) |
Statement::ExpressionStatement(_) |
Statement::IfStatement(_) |
Statement::BlockStatement(_) |
Statement::ExportNamedDeclaration(_) |
Statement::ImportDeclaration(_) |
Statement::FunctionDeclaration(_) |
Statement::ReturnStatement(_) => {
// These are the only statements the PVM handles currently.
walk::walk_statement(self, stmt);
}
_ => {
self.errors.push(format!("Unsupported statement type at {:?}. Note: Prometeu does not support while/for loops or classes yet.", stmt.span()));
}
}
}
}