Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
vlopes11 committed Nov 11, 2022
1 parent b2d3675 commit 8c6d18b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 22 deletions.
2 changes: 1 addition & 1 deletion assembly/src/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ mod stack_ops;
mod u32_ops;

mod ast;
pub use ast::{ModuleAst, NamedModuleAst, ProcedureAst, ProgramAst};
pub(crate) use ast::{Instruction, Node};
pub use ast::{ModuleAst, NamedModuleAst, ProcedureAst, ProgramAst};

// OP PARSER
// ================================================================================================
Expand Down
79 changes: 58 additions & 21 deletions assembly/src/todo/mod.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,105 @@
use core::borrow::Borrow;

use vm_core::{code_blocks::CodeBlock, Operation};
use vm_core::{code_blocks::CodeBlock, CodeBlockTable, Operation};

use crate::{
parsers::{Instruction, Node},
ModuleProvider, ProcedureAst, ProcedureId, ProgramAst,
};

#[derive(Debug)]
pub struct ContextProcedure {
pub id: ProcedureId,
pub procedure: ProcedureAst,
pub procedure: CodeBlock,
}

pub struct AssemblerContext {}
#[derive(Debug, Default)]
pub struct AssemblerContext {
pub locals: Vec<ContextProcedure>,
}

impl AssemblerContext {
pub fn new<M, I, P>(provider: &M, cb_table: &mut CodeBlockTable, procs: I) -> Self
where
M: ModuleProvider,
I: Iterator<Item = P>,
P: Borrow<ProcedureAst>,
{
let (size, hint) = procs.size_hint();
let size = hint.unwrap_or(size);
let mut context = Self {
locals: Vec::with_capacity(size),
};

procs.for_each(|proc| {
context.locals.push(ContextProcedure {
id: todo!(),
procedure: ast_to_mast(provider, &mut context, cb_table, proc.borrow().body.iter()),
})
});

context
}
}

pub struct Assembler {}

impl Assembler {
pub fn compile<M, S>(provider: &M, source: S)
pub fn compile<M, S>(provider: &M, cb_table: &mut CodeBlockTable, source: S) -> CodeBlock
where
M: ModuleProvider,
S: AsRef<str>,
{
let source = source.as_ref();
let ProgramAst { local_procs, body } = ProgramAst::try_from(source).unwrap();
let locals: Vec<_> = local_procs
.into_iter()
.map(|proc| ContextProcedure {
id: todo!(),
procedure: proc,
})
.collect();
let mut context = AssemblerContext::new(provider, cb_table, local_procs.iter());

ast_to_mast(provider, &mut context, cb_table, body.iter())
}
}

fn ast_to_mast<A, N>(ast: A) -> CodeBlock
fn ast_to_mast<M, A, N>(
provider: &M,
context: &mut AssemblerContext,
cb_table: &mut CodeBlockTable,
ast: A,
) -> CodeBlock
where
M: ModuleProvider,
A: Iterator<Item = N>,
N: Borrow<Node>,
{
let mut root = CodeBlock::new_span(vec![Operation::Noop]);

for node in ast {
let node = node.borrow();

match node {
match node.borrow() {
Node::Instruction(instruction) => {
// TODO consider batching chunks of ops into spans
let operations = instruction_to_operations(instruction);
let operations =
instruction_to_operations(provider, context, cb_table, instruction);
let span = CodeBlock::new_span(operations);

root = CodeBlock::new_join([root, span]);
}

Node::IfElse(t, f) => {
let t = ast_to_mast(t.iter());
let f = ast_to_mast(f.iter());
let t = ast_to_mast(provider, context, cb_table, t.iter());
let f = ast_to_mast(provider, context, cb_table, f.iter());
let code = CodeBlock::new_split(t, f);

root = CodeBlock::new_join([root, code]);
}

Node::Repeat(n, nodes) => {
let body = ast_to_mast(nodes.iter());
let body = ast_to_mast(provider, context, cb_table, nodes.iter());

for _ in 0..*n {
root = CodeBlock::new_join([root, body.clone()]);
}
}

Node::While(nodes) => {
let body = ast_to_mast(nodes.iter());
let body = ast_to_mast(provider, context, cb_table, nodes.iter());
let code = CodeBlock::new_loop(body);

root = CodeBlock::new_join([root, code]);
Expand All @@ -81,7 +110,15 @@ where
root
}

fn instruction_to_operations(instruction: &Instruction) -> Vec<Operation> {
fn instruction_to_operations<M>(
_provider: &M,
_context: &mut AssemblerContext,
_cb_table: &mut CodeBlockTable,
instruction: &Instruction,
) -> Vec<Operation>
where
M: ModuleProvider,
{
use Operation::*;

match instruction {
Expand Down

0 comments on commit 8c6d18b

Please sign in to comment.