From b16974c7c48da3395beb97040feed53f77069156 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Mon, 27 Jan 2020 18:41:33 -0500 Subject: [PATCH] Update registers to be constants, add instructions to parser Signed-off-by: Alek Ratzloff --- src/vm/reg.rs | 157 ++++++++++++++++++++------------------ src/vm/syn/ast.rs | 39 ++-------- src/vm/syn/parser.lalrpop | 100 ++++++++++++------------ src/vm/vm.rs | 12 +-- 4 files changed, 149 insertions(+), 159 deletions(-) diff --git a/src/vm/reg.rs b/src/vm/reg.rs index 183bf01..388a7cf 100644 --- a/src/vm/reg.rs +++ b/src/vm/reg.rs @@ -1,83 +1,94 @@ -#[repr(u8)] -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -#[allow(dead_code)] -pub enum Reg { +macro_rules! registers { + { + $($variant:ident = $value:expr),* $(,)? + } => { + pub type Reg = u8; + + $( + #[allow(dead_code)] + pub const $variant: Reg = $value; + )* + }; +} + +registers! { // https://crates.io/crates/packed_struct // TODO : check this muffugin shit out!! - /// Instruction pointer - Ip = 0, + // Instruction pointer + IP = 0, - /// Stack pointer - Sp, + // Stack pointer + SP = 1, - /// Frame pointer - Fp, + // Frame pointer + FP = 2, - /// Flags - Flags, + // Flags + FLAGS = 3, - Unused00, - Unused01, - Unused02, - Unused03, - Unused04, - Unused05, - Unused06, - Unused07, - Unused08, + UNUSED00 = 4, + UNUSED01 = 5, + UNUSED02 = 6, + UNUSED03 = 7, + UNUSED04 = 8, + UNUSED05 = 9, + UNUSED06 = 10, + UNUSED07 = 11, + UNUSED08 = 12, - /// General status code - Status, + // General status code + STATUS = 13, - R00, - R01, - R02, - R03, - R04, - R05, - R06, - R07, - R08, - R09, - R10, - R11, - R12, - R13, - R14, - R15, - R16, - R17, - R18, - R19, - R20, - R21, - R22, - R23, - R24, - R25, - R26, - R27, - R28, - R29, - R30, - R31, - R32, - R33, - R34, - R35, - R36, - R37, - R38, - R39, - R40, - R41, - R42, - R43, - R44, - R45, - R46, - R47, - R48, - R49, + R00 = 14, + R01 = 15, + R02 = 16, + R03 = 17, + R04 = 18, + R05 = 19, + R06 = 20, + R07 = 21, + R08 = 22, + R09 = 23, + R10 = 24, + R11 = 25, + R12 = 26, + R13 = 27, + R14 = 28, + R15 = 29, + R16 = 30, + R17 = 31, + R18 = 32, + R19 = 33, + R20 = 34, + R21 = 35, + R22 = 36, + R23 = 37, + R24 = 38, + R25 = 39, + R26 = 40, + R27 = 41, + R28 = 42, + R29 = 43, + R30 = 44, + R31 = 45, + R32 = 46, + R33 = 47, + R34 = 48, + R35 = 49, + R36 = 50, + R37 = 51, + R38 = 52, + R39 = 53, + R40 = 54, + R41 = 55, + R42 = 56, + R43 = 57, + R44 = 58, + R45 = 59, + R46 = 60, + R47 = 61, + R48 = 62, + R49 = 63, + LAST_REG = R49, } diff --git a/src/vm/syn/ast.rs b/src/vm/syn/ast.rs index c0a5486..e7e2974 100644 --- a/src/vm/syn/ast.rs +++ b/src/vm/syn/ast.rs @@ -1,42 +1,15 @@ +use crate::vm::{inst::Inst, reg::Reg}; + #[derive(Debug, Clone, PartialEq, Eq)] pub enum Line { - Section(Section), + Directive(Directive), Inst(Inst), -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Section { - Code, - Data, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum InstOp { - Add, - Mul, - Div, - Neg, - And, - Or, - Xor, - Shl, - Shr, - CmpEq, - CmpLt, - Jz, - Jnz, - Load, - Store, - StoreImm, - Copy, - Nop, - Halt, + LabelDef(String), } #[derive(Debug, Clone, PartialEq, Eq)] -pub struct Inst { - pub op: InstOp, - pub args: Vec, +pub enum Directive { + Section(String), } #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/src/vm/syn/parser.lalrpop b/src/vm/syn/parser.lalrpop index ca86108..1e89e74 100644 --- a/src/vm/syn/parser.lalrpop +++ b/src/vm/syn/parser.lalrpop @@ -1,73 +1,79 @@ use std::str::FromStr; use crate::vm::{ syn::ast::*, + reg::*, + inst::Inst, }; grammar; -// TODO : instkind -InstOp: InstOp = { - "add" => InstOp::Add, - "mul" => InstOp::Mul, - "div" => InstOp::Div, - "neg" => InstOp::Neg, - "and" => InstOp::And, - "or" => InstOp::Or, - "xor" => InstOp::Xor, - "shl" => InstOp::Shl, - "shr" => InstOp::Shr, - "cmpeq" => InstOp::CmpEq, - "cmplt" => InstOp::CmpLt, - "jz" => InstOp::Jz, - "jnz" => InstOp::Jnz, - "load" => InstOp::Load, - "store" => InstOp::Store, - "storeimm" => InstOp::StoreImm, - "copy" => InstOp::Copy, - "nop" => InstOp::Nop, - "halt" => InstOp::Halt, -} - LabelDef: String = {