-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
304 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,300 @@ | ||
use core::borrow::Borrow; | ||
|
||
use vm_core::{code_blocks::CodeBlock, Operation}; | ||
|
||
use crate::{ | ||
parsers::{Instruction, Node}, | ||
ModuleProvider, ProcedureAst, ProcedureId, ProgramAst, | ||
}; | ||
|
||
pub struct ContextProcedure { | ||
pub id: ProcedureId, | ||
pub procedure: ProcedureAst, | ||
} | ||
|
||
pub struct AssemblerContext {} | ||
|
||
pub struct Assembler {} | ||
|
||
impl Assembler { | ||
pub fn compile<M, S>(provider: &M, source: S) | ||
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(); | ||
} | ||
} | ||
|
||
fn ast_to_mast<A, N>(ast: A) -> CodeBlock | ||
where | ||
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 { | ||
Node::Instruction(instruction) => { | ||
// TODO consider batching chunks of ops into spans | ||
let operations = instruction_to_operations(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 code = CodeBlock::new_split(t, f); | ||
|
||
root = CodeBlock::new_join([root, code]); | ||
} | ||
|
||
Node::Repeat(n, nodes) => { | ||
let body = ast_to_mast(nodes.iter()); | ||
|
||
for _ in 0..*n { | ||
root = CodeBlock::new_join([root, body.clone()]); | ||
} | ||
} | ||
|
||
Node::While(nodes) => { | ||
let body = ast_to_mast(nodes.iter()); | ||
let code = CodeBlock::new_loop(body); | ||
|
||
root = CodeBlock::new_join([root, code]); | ||
} | ||
} | ||
} | ||
|
||
root | ||
} | ||
|
||
fn instruction_to_operations(instruction: &Instruction) -> Vec<Operation> { | ||
use Operation::*; | ||
|
||
match instruction { | ||
Instruction::Assert => vec![Assert], | ||
Instruction::AssertEq => vec![Eq, Assert], | ||
Instruction::Assertz => vec![Eqz, Assert], | ||
Instruction::Add => vec![Add], | ||
Instruction::AddImm(imm) => vec![Push(*imm), Add], | ||
Instruction::Sub => vec![Neg, Add], | ||
Instruction::SubImm(imm) => vec![Push(*imm), Neg, Add], | ||
Instruction::Mul => vec![Mul], | ||
Instruction::MulImm(imm) => vec![Push(*imm), Mul], | ||
Instruction::Div => vec![Inv, Mul], | ||
Instruction::DivImm(imm) => vec![Push(*imm), Inv, Mul], | ||
Instruction::Neg => vec![Neg], | ||
Instruction::Inv => vec![Inv], | ||
Instruction::Pow2 => vec![Dup0, Mul], | ||
Instruction::Exp => todo!(), | ||
Instruction::Not => vec![Not], | ||
Instruction::And => vec![And], | ||
Instruction::Or => vec![Or], | ||
Instruction::Xor => todo!(), | ||
Instruction::Eq => vec![Eq], | ||
Instruction::EqImm(imm) => vec![Push(*imm), Eq], | ||
Instruction::Neq => vec![Eq, Eqz], | ||
Instruction::NeqImm(imm) => vec![Push(*imm), Eq, Eqz], | ||
Instruction::Eqw => todo!(), | ||
Instruction::Lt => todo!(), | ||
Instruction::Lte => todo!(), | ||
Instruction::Gt => todo!(), | ||
Instruction::Gte => todo!(), | ||
Instruction::U32Test => todo!(), | ||
Instruction::U32TestW => todo!(), | ||
Instruction::U32Assert => todo!(), | ||
Instruction::U32AssertW => todo!(), | ||
Instruction::U32Split => todo!(), | ||
Instruction::U32Cast => todo!(), | ||
Instruction::U32CheckedAdd => todo!(), | ||
Instruction::U32CheckedAddImm(_) => todo!(), | ||
Instruction::U32WrappingAdd => todo!(), | ||
Instruction::U32WrappingAddImm(_) => todo!(), | ||
Instruction::U32OverflowingAdd => todo!(), | ||
Instruction::U32OverflowingAddImm(_) => todo!(), | ||
Instruction::U32OverflowingAdd3 => todo!(), | ||
Instruction::U32WrappingAdd3 => todo!(), | ||
Instruction::U32CheckedSub => todo!(), | ||
Instruction::U32CheckedSubImm(_) => todo!(), | ||
Instruction::U32WrappingSub => todo!(), | ||
Instruction::U32WrappingSubImm(_) => todo!(), | ||
Instruction::U32OverflowingSub => todo!(), | ||
Instruction::U32OverflowingSubImm(_) => todo!(), | ||
Instruction::U32CheckedMul => todo!(), | ||
Instruction::U32CheckedMulImm(_) => todo!(), | ||
Instruction::U32WrappingMul => todo!(), | ||
Instruction::U32WrappingMulImm(_) => todo!(), | ||
Instruction::U32OverflowingMul => todo!(), | ||
Instruction::U32OverflowingMulImm(_) => todo!(), | ||
Instruction::U32OverflowingMadd => todo!(), | ||
Instruction::U32WrappingMadd => todo!(), | ||
Instruction::U32CheckedDiv => todo!(), | ||
Instruction::U32CheckedDivImm(_) => todo!(), | ||
Instruction::U32UncheckedDiv => todo!(), | ||
Instruction::U32UncheckedDivImm(_) => todo!(), | ||
Instruction::U32CheckedMod => todo!(), | ||
Instruction::U32CheckedModImm(_) => todo!(), | ||
Instruction::U32UncheckedMod => todo!(), | ||
Instruction::U32UncheckedModImm(_) => todo!(), | ||
Instruction::U32CheckedDivMod => todo!(), | ||
Instruction::U32CheckedDivModImm(_) => todo!(), | ||
Instruction::U32UncheckedDivMod => todo!(), | ||
Instruction::U32UncheckedDivModImm(_) => todo!(), | ||
Instruction::U32CheckedAnd => todo!(), | ||
Instruction::U32CheckedOr => todo!(), | ||
Instruction::U32CheckedXor => todo!(), | ||
Instruction::U32CheckedNot => todo!(), | ||
Instruction::U32CheckedShr => todo!(), | ||
Instruction::U32CheckedShrImm(_) => todo!(), | ||
Instruction::U32UncheckedShr => todo!(), | ||
Instruction::U32UncheckedShrImm(_) => todo!(), | ||
Instruction::U32CheckedShl => todo!(), | ||
Instruction::U32CheckedShlImm(_) => todo!(), | ||
Instruction::U32UncheckedShl => todo!(), | ||
Instruction::U32UncheckedShlImm(_) => todo!(), | ||
Instruction::U32CheckedRotr => todo!(), | ||
Instruction::U32CheckedRotrImm(_) => todo!(), | ||
Instruction::U32UncheckedRotr => todo!(), | ||
Instruction::U32UncheckedRotrImm(_) => todo!(), | ||
Instruction::U32CheckedRotl => todo!(), | ||
Instruction::U32CheckedRotlImm(_) => todo!(), | ||
Instruction::U32UncheckedRotl => todo!(), | ||
Instruction::U32UncheckedRotlImm(_) => todo!(), | ||
Instruction::U32CheckedEq => todo!(), | ||
Instruction::U32CheckedEqImm(_) => todo!(), | ||
Instruction::U32CheckedNeq => todo!(), | ||
Instruction::U32CheckedNeqImm(_) => todo!(), | ||
Instruction::U32CheckedLt => todo!(), | ||
Instruction::U32UncheckedLt => todo!(), | ||
Instruction::U32CheckedLte => todo!(), | ||
Instruction::U32UncheckedLte => todo!(), | ||
Instruction::U32CheckedGt => todo!(), | ||
Instruction::U32UncheckedGt => todo!(), | ||
Instruction::U32CheckedGte => todo!(), | ||
Instruction::U32UncheckedGte => todo!(), | ||
Instruction::U32CheckedMin => todo!(), | ||
Instruction::U32UncheckedMin => todo!(), | ||
Instruction::U32CheckedMax => todo!(), | ||
Instruction::U32UncheckedMax => todo!(), | ||
Instruction::Drop => todo!(), | ||
Instruction::DropW => todo!(), | ||
Instruction::PadW => todo!(), | ||
Instruction::Dup0 => todo!(), | ||
Instruction::Dup1 => todo!(), | ||
Instruction::Dup2 => todo!(), | ||
Instruction::Dup3 => todo!(), | ||
Instruction::Dup4 => todo!(), | ||
Instruction::Dup5 => todo!(), | ||
Instruction::Dup6 => todo!(), | ||
Instruction::Dup7 => todo!(), | ||
Instruction::Dup8 => todo!(), | ||
Instruction::Dup9 => todo!(), | ||
Instruction::Dup10 => todo!(), | ||
Instruction::Dup11 => todo!(), | ||
Instruction::Dup12 => todo!(), | ||
Instruction::Dup13 => todo!(), | ||
Instruction::Dup14 => todo!(), | ||
Instruction::Dup15 => todo!(), | ||
Instruction::DupW0 => todo!(), | ||
Instruction::DupW1 => todo!(), | ||
Instruction::DupW2 => todo!(), | ||
Instruction::DupW3 => todo!(), | ||
Instruction::Swap => todo!(), | ||
Instruction::Swap2 => todo!(), | ||
Instruction::Swap3 => todo!(), | ||
Instruction::Swap4 => todo!(), | ||
Instruction::Swap5 => todo!(), | ||
Instruction::Swap6 => todo!(), | ||
Instruction::Swap7 => todo!(), | ||
Instruction::Swap8 => todo!(), | ||
Instruction::Swap9 => todo!(), | ||
Instruction::Swap10 => todo!(), | ||
Instruction::Swap11 => todo!(), | ||
Instruction::Swap12 => todo!(), | ||
Instruction::Swap13 => todo!(), | ||
Instruction::Swap14 => todo!(), | ||
Instruction::Swap15 => todo!(), | ||
Instruction::SwapW => todo!(), | ||
Instruction::SwapW2 => todo!(), | ||
Instruction::SwapW3 => todo!(), | ||
Instruction::SwapDW => todo!(), | ||
Instruction::MovUp2 => todo!(), | ||
Instruction::MovUp3 => todo!(), | ||
Instruction::MovUp4 => todo!(), | ||
Instruction::MovUp5 => todo!(), | ||
Instruction::MovUp6 => todo!(), | ||
Instruction::MovUp7 => todo!(), | ||
Instruction::MovUp8 => todo!(), | ||
Instruction::MovUp9 => todo!(), | ||
Instruction::MovUp10 => todo!(), | ||
Instruction::MovUp11 => todo!(), | ||
Instruction::MovUp12 => todo!(), | ||
Instruction::MovUp13 => todo!(), | ||
Instruction::MovUp14 => todo!(), | ||
Instruction::MovUp15 => todo!(), | ||
Instruction::MovUpW2 => todo!(), | ||
Instruction::MovUpW3 => todo!(), | ||
Instruction::MovDn2 => todo!(), | ||
Instruction::MovDn3 => todo!(), | ||
Instruction::MovDn4 => todo!(), | ||
Instruction::MovDn5 => todo!(), | ||
Instruction::MovDn6 => todo!(), | ||
Instruction::MovDn7 => todo!(), | ||
Instruction::MovDn8 => todo!(), | ||
Instruction::MovDn9 => todo!(), | ||
Instruction::MovDn10 => todo!(), | ||
Instruction::MovDn11 => todo!(), | ||
Instruction::MovDn12 => todo!(), | ||
Instruction::MovDn13 => todo!(), | ||
Instruction::MovDn14 => todo!(), | ||
Instruction::MovDn15 => todo!(), | ||
Instruction::MovDnW2 => todo!(), | ||
Instruction::MovDnW3 => todo!(), | ||
Instruction::CSwap => todo!(), | ||
Instruction::CSwapW => todo!(), | ||
Instruction::CDrop => todo!(), | ||
Instruction::CDropW => todo!(), | ||
Instruction::PushConstants(_) => todo!(), | ||
Instruction::Locaddr(_) => todo!(), | ||
Instruction::Sdepth => todo!(), | ||
Instruction::Caller => todo!(), | ||
Instruction::MemLoad => todo!(), | ||
Instruction::MemLoadImm(_) => todo!(), | ||
Instruction::MemLoadW => todo!(), | ||
Instruction::MemLoadWImm(_) => todo!(), | ||
Instruction::LocLoad(_) => todo!(), | ||
Instruction::LocLoadW(_) => todo!(), | ||
Instruction::MemStore => todo!(), | ||
Instruction::MemStoreImm(_) => todo!(), | ||
Instruction::LocStore(_) => todo!(), | ||
Instruction::MemStoreW => todo!(), | ||
Instruction::MemStoreWImm(_) => todo!(), | ||
Instruction::LocStoreW(_) => todo!(), | ||
Instruction::MemStream => todo!(), | ||
Instruction::AdvU64Div => todo!(), | ||
Instruction::AdvPush(_) => todo!(), | ||
Instruction::AdvLoadW(_) => todo!(), | ||
Instruction::RPHash => todo!(), | ||
Instruction::RPPerm => todo!(), | ||
Instruction::MTreeGet => todo!(), | ||
Instruction::MTreeSet => todo!(), | ||
Instruction::MTreeCWM => todo!(), | ||
Instruction::ExecLocal(_) => todo!(), | ||
Instruction::ExecImported(_) => todo!(), | ||
Instruction::CallLocal(_) => todo!(), | ||
Instruction::CallImported(_) => todo!(), | ||
} | ||
} |