diff --git a/src/main.rs b/src/main.rs index 041a181..5948c59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -216,7 +216,9 @@ fn main() -> Result<(), ()> { let exec_module = compile(&compilation_params, module, &mut env.environment).expect("no compilation error"); - code_dump(&exec_module.code, &exec_module.funs); + let mut out = String::new(); + code_dump(&mut out, &exec_module.code, &exec_module.funs).expect("writing to string work"); + println!("{}", out); let ee = env.finalize(); diff --git a/werbolg-compile/src/compile.rs b/werbolg-compile/src/compile.rs index 0009a90..dfbcd64 100644 --- a/werbolg-compile/src/compile.rs +++ b/werbolg-compile/src/compile.rs @@ -5,6 +5,7 @@ use super::instructions::*; use super::symbols::*; use super::CompilationError; use super::CompilationParams; +use alloc::{vec, vec::Vec}; use werbolg_core as ir; use werbolg_core::{ConstrId, FunId, GlobalId, Ident, LitId, NifId, Span}; diff --git a/werbolg-compile/src/defs.rs b/werbolg-compile/src/defs.rs index 6adb791..75cffce 100644 --- a/werbolg-compile/src/defs.rs +++ b/werbolg-compile/src/defs.rs @@ -1,5 +1,6 @@ use super::code::InstructionAddress; use super::instructions::*; +use alloc::vec::Vec; use werbolg_core::{ConstrId, Ident}; #[derive(Copy, Clone, Debug)] diff --git a/werbolg-compile/src/instructions.rs b/werbolg-compile/src/instructions.rs index 57c7f6d..3346451 100644 --- a/werbolg-compile/src/instructions.rs +++ b/werbolg-compile/src/instructions.rs @@ -1,6 +1,7 @@ use super::code::InstructionDiff; use werbolg_core::{ConstrId, FunId, GlobalId, LitId, NifId}; +/// Instruction for execution #[derive(Clone, Debug)] pub enum Instruction { /// Push a literal value on the stack @@ -15,7 +16,7 @@ pub enum Instruction { FetchStackParam(ParamBindIndex), /// Fetch from the localstack values (which is relative and after SP) FetchStackLocal(LocalBindIndex), - /// Access a field in a structure value as stack[top] + /// Access a field in a structure value as stack\[top\] AccessField(ConstrId, StructFieldIndex), /// Bind Locally a value LocalBind(LocalBindIndex), @@ -27,20 +28,30 @@ pub enum Instruction { Call(CallArity), /// Jump by N instructions Jump(InstructionDiff), - /// Jump by N instructions if stack[top] is true + /// Jump by N instructions if stack\[top\] is true CondJump(InstructionDiff), /// Return from call Ret, } +/// The index of locally (in the context of a function) bind value +/// +/// This is limited (arbitrarily) to a maximum of 65535 values #[derive(Clone, Copy, Debug)] pub struct LocalBindIndex(pub u16); +/// the index of function parameter #[derive(Clone, Copy, Debug)] pub struct ParamBindIndex(pub u8); +/// A field in a structured indexed by its order in the structure +/// +/// This is limited (arbitrarily) to a maximum of 255 #[derive(Clone, Copy, Debug)] pub struct StructFieldIndex(pub u8); +/// The arity (number of parameter) of a function. +/// +/// This is limited (arbitrarily) to a maximum of 255 #[derive(Clone, Copy, Debug)] pub struct CallArity(pub u8); diff --git a/werbolg-compile/src/lib.rs b/werbolg-compile/src/lib.rs index 54a5c98..7701fde 100644 --- a/werbolg-compile/src/lib.rs +++ b/werbolg-compile/src/lib.rs @@ -1,3 +1,5 @@ +#![no_std] + extern crate alloc; mod bindings; @@ -22,6 +24,9 @@ use bindings::BindingsStack; pub use environ::Environment; use symbols::{IdVec, IdVecAfter, SymbolsTable, SymbolsTableData}; +use alloc::format; +use core::fmt::Write; + #[derive(Debug)] pub enum CompilationError { DuplicateSymbol(Ident), @@ -98,7 +103,11 @@ pub fn compile<'a, L: Clone + Eq + core::hash::Hash>( }) } -pub fn code_dump(code: &IdVec, fundefs: &IdVec) { +pub fn code_dump( + writer: &mut W, + code: &IdVec, + fundefs: &IdVec, +) -> Result<(), core::fmt::Error> { let mut place = hashbrown::HashMap::new(); for (funid, fundef) in fundefs.iter() { place.insert(fundef.code_pos, funid); @@ -107,7 +116,8 @@ pub fn code_dump(code: &IdVec, fundefs: &IdVec< for (ia, stmt) in code.iter() { if let Some(funid) = place.get(&ia) { let fundef = &fundefs[*funid]; - println!( + write!( + writer, "[{} local-stack={}]", fundef .name @@ -115,8 +125,9 @@ pub fn code_dump(code: &IdVec, fundefs: &IdVec< .map(|n| format!("{:?}", n)) .unwrap_or(format!("{:?}", funid)), fundef.stack_size.0 - ); + )?; } - println!("{} {:?}", ia, stmt) + write!(writer, "{} {:?}", ia, stmt)? } + Ok(()) }