Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-02-09 13:13:26 -05:00
parent 1c16be650a
commit f1ed41f98b
11 changed files with 44 additions and 75 deletions

View File

@@ -64,6 +64,6 @@ impl Span {
pub fn union(self, other: Span) -> Self {
let start = self.start.min(other.start);
let end = self.end.max(other.end);
Span { start, end, }
Span { start, end }
}
}

View File

@@ -26,7 +26,7 @@ fn main() -> Result<()> {
Err(err) => {
eprintln!("{}", err);
process::exit(1);
},
}
};
let obj = vm::obj::obj::Object::try_from(&ast)?;
println!("{:#?}", obj);

View File

@@ -1,9 +1,9 @@
use snafu::Snafu;
use crate::vm::vm::*;
use snafu::Snafu;
#[derive(Snafu, Debug, Clone)]
pub enum VmError {
MemOutOfBounds { addr: Addr, }
MemOutOfBounds { addr: Addr },
}
pub type Result<T, E = VmError> = std::result::Result<T, E>;

View File

@@ -10,7 +10,6 @@ macro_rules! instructions {
}
pub type InstOp = u16;
instructions! {
ADD = 0x0000,
MUL = 0x0001,
@@ -42,30 +41,10 @@ instructions! {
pub fn inst_len(op: InstOp) -> usize {
match op {
// 2 bytes
INEG
| INV
| NOT
| HALT
| NOP => 2,
INEG | INV | NOT | HALT | NOP => 2,
// 4 bytes
ADD
| MUL
| DIV
| MOD
| AND
| OR
| XOR
| SHL
| SHR
| CMPEQ
| CMPLT
| JMP
| JZ
| JNZ
| LOAD
| REGCOPY
| MEMCOPY
| STORE => 4,
ADD | MUL | DIV | MOD | AND | OR | XOR | SHL | SHR | CMPEQ | CMPLT | JMP | JZ | JNZ
| LOAD | REGCOPY | MEMCOPY | STORE => 4,
// Immediates - 4+ bytes
STOREIMM64 => 16,
STOREIMM32 => 8,

View File

@@ -1,8 +1,8 @@
use crate::vm::{error::*, reg::*};
use byteorder::{LE, ReadBytesExt};
use byteorder::{ReadBytesExt, LE};
use std::{
ops::{Deref, DerefMut},
io::Cursor,
ops::{Deref, DerefMut},
};
const R1_MASK: u16 = 0b1111_1100_0000_0000;
@@ -15,7 +15,7 @@ pub struct MemCursor<'mem> {
impl<'mem> MemCursor<'mem> {
pub fn new(mem: &'mem [u8]) -> Self {
MemCursor {
cursor: Cursor::new(mem)
cursor: Cursor::new(mem),
}
}

View File

@@ -1,16 +1,11 @@
use snafu::Snafu;
use std::{
fmt::Debug,
io,
};
use std::{fmt::Debug, io};
#[derive(Debug, Snafu)]
pub enum AssembleError {
#[snafu(display("IO error: {}", source))]
Io { source: io::Error },
#[snafu(display("duplicate symbol name: {}", name))]
DuplicateSymbol { name: String },

View File

@@ -1,3 +1,3 @@
pub mod assemble;
pub mod syn;
pub mod obj;
pub mod syn;

View File

@@ -1,5 +1,5 @@
use crate::vm::obj::syn::error::{ParseError, Result};
use byteorder::{ReadBytesExt, LE};
use crate::vm::obj::syn::error::{Result, ParseError};
use std::{
collections::HashMap,
convert::{TryFrom, TryInto},
@@ -142,8 +142,6 @@ impl Section {
let value = cursor.read_u64::<LE>()?;
entries.insert(key, value);
}
Ok(Section::Meta {
entries
})
Ok(Section::Meta { entries })
}
}

View File

@@ -1,4 +1,4 @@
use crate::vm::{reg::Reg, inst::*};
use crate::vm::{inst::*, reg::Reg};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SectionBlock {
@@ -69,27 +69,27 @@ pub enum Inst {
impl Inst {
pub fn op(&self) -> InstOp {
match self {
Inst::Add(_, _) => { ADD }
Inst::Mul(_, _) => { MUL }
Inst::Div(_, _) => { DIV }
Inst::Mod(_, _) => { MOD }
Inst::INeg(_) => { INEG }
Inst::And(_, _) => { AND }
Inst::Or(_, _) => { OR }
Inst::Inv(_) => { INV }
Inst::Not(_) => { NOT }
Inst::Xor(_, _) => { XOR }
Inst::Shl(_, _) => { SHL }
Inst::Shr(_, _) => { SHR }
Inst::Add(_, _) => ADD,
Inst::Mul(_, _) => MUL,
Inst::Div(_, _) => DIV,
Inst::Mod(_, _) => MOD,
Inst::INeg(_) => INEG,
Inst::And(_, _) => AND,
Inst::Or(_, _) => OR,
Inst::Inv(_) => INV,
Inst::Not(_) => NOT,
Inst::Xor(_, _) => XOR,
Inst::Shl(_, _) => SHL,
Inst::Shr(_, _) => SHR,
Inst::CmpEq(_, _) => { CMPEQ }
Inst::CmpLt(_, _) => { CMPLT }
Inst::Jmp(_) => { JMP }
Inst::Jz(_) => { JZ }
Inst::Jnz(_) => { JNZ }
Inst::CmpEq(_, _) => CMPEQ,
Inst::CmpLt(_, _) => CMPLT,
Inst::Jmp(_) => JMP,
Inst::Jz(_) => JZ,
Inst::Jnz(_) => JNZ,
Inst::Load(_, _) => { LOAD }
Inst::Store(_, _) => { STORE }
Inst::Load(_, _) => LOAD,
Inst::Store(_, _) => STORE,
Inst::StoreImm(_, imm) => {
if let ImmValue::Number(imm) = imm {
if *imm > (u32::max_value() as u64) {
@@ -101,11 +101,11 @@ impl Inst {
STOREIMM64
}
}
Inst::MemCopy(_, _) => { MEMCOPY }
Inst::RegCopy(_, _) => { REGCOPY }
Inst::MemCopy(_, _) => MEMCOPY,
Inst::RegCopy(_, _) => REGCOPY,
Inst::Nop => { NOP }
Inst::Halt => { HALT }
Inst::Nop => NOP,
Inst::Halt => HALT,
}
}
pub fn len(&self) -> usize {

View File

@@ -1,8 +1,5 @@
use snafu::Snafu;
use std::{
fmt::Debug,
io,
};
use std::{fmt::Debug, io};
#[derive(Debug, Snafu)]
pub enum ParseError {

View File

@@ -12,14 +12,14 @@ impl Vm {
next_ip = cursor.position();
let value = ($mapping)(self.get_reg(r1), self.get_reg(r2));
self.set_reg(r1, value);
}}
}};
}
match op {
ADD => math_inst!(|w1: u64, w2: u64| w1.wrapping_add(w2)),
MUL => math_inst!(|w1: u64, w2: u64| w1.wrapping_mul(w2)),
DIV => math_inst!(|w1: u64, w2: u64| w1.wrapping_div(w2)),
MOD => math_inst!(|w1: u64, w2: u64| w1 % w2),
INEG => { todo!() }
INEG => todo!(),
AND => math_inst!(|w1: u64, w2: u64| w1 & w2),
OR => math_inst!(|w1: u64, w2: u64| w1 | w2),
INV => {