Skip to content

Commit

Permalink
make werbolg-compile no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenthz committed Dec 19, 2023
1 parent f652aed commit bf696ab
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
1 change: 1 addition & 0 deletions werbolg-compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
1 change: 1 addition & 0 deletions werbolg-compile/src/defs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::code::InstructionAddress;
use super::instructions::*;
use alloc::vec::Vec;
use werbolg_core::{ConstrId, Ident};

#[derive(Copy, Clone, Debug)]
Expand Down
15 changes: 13 additions & 2 deletions werbolg-compile/src/instructions.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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),
Expand All @@ -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);
19 changes: 15 additions & 4 deletions werbolg-compile/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![no_std]

extern crate alloc;

mod bindings;
Expand All @@ -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),
Expand Down Expand Up @@ -98,7 +103,11 @@ pub fn compile<'a, L: Clone + Eq + core::hash::Hash>(
})
}

pub fn code_dump(code: &IdVec<InstructionAddress, Instruction>, fundefs: &IdVec<FunId, FunDef>) {
pub fn code_dump<W: Write>(
writer: &mut W,
code: &IdVec<InstructionAddress, Instruction>,
fundefs: &IdVec<FunId, FunDef>,
) -> Result<(), core::fmt::Error> {
let mut place = hashbrown::HashMap::new();
for (funid, fundef) in fundefs.iter() {
place.insert(fundef.code_pos, funid);
Expand All @@ -107,16 +116,18 @@ pub fn code_dump(code: &IdVec<InstructionAddress, Instruction>, 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
.as_ref()
.map(|n| format!("{:?}", n))
.unwrap_or(format!("{:?}", funid)),
fundef.stack_size.0
);
)?;
}
println!("{} {:?}", ia, stmt)
write!(writer, "{} {:?}", ia, stmt)?
}
Ok(())
}

0 comments on commit bf696ab

Please sign in to comment.