diff --git a/src/common/span.rs b/src/common/span.rs index 8692736..9981f28 100644 --- a/src/common/span.rs +++ b/src/common/span.rs @@ -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 } } } diff --git a/src/main.rs b/src/main.rs index a31b315..9cf7426 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); diff --git a/src/vm/error.rs b/src/vm/error.rs index 5b9898e..dc52452 100644 --- a/src/vm/error.rs +++ b/src/vm/error.rs @@ -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 = std::result::Result; diff --git a/src/vm/inst.rs b/src/vm/inst.rs index f2e246a..b91cc2b 100644 --- a/src/vm/inst.rs +++ b/src/vm/inst.rs @@ -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, diff --git a/src/vm/mem.rs b/src/vm/mem.rs index e5d6efe..69d8cd2 100644 --- a/src/vm/mem.rs +++ b/src/vm/mem.rs @@ -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), } } diff --git a/src/vm/obj/assemble/error.rs b/src/vm/obj/assemble/error.rs index e744abc..605b9b1 100644 --- a/src/vm/obj/assemble/error.rs +++ b/src/vm/obj/assemble/error.rs @@ -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 }, diff --git a/src/vm/obj/mod.rs b/src/vm/obj/mod.rs index 2daefad..ffb1f35 100644 --- a/src/vm/obj/mod.rs +++ b/src/vm/obj/mod.rs @@ -1,3 +1,3 @@ pub mod assemble; -pub mod syn; pub mod obj; +pub mod syn; diff --git a/src/vm/obj/obj.rs b/src/vm/obj/obj.rs index 5860ee9..598dd86 100644 --- a/src/vm/obj/obj.rs +++ b/src/vm/obj/obj.rs @@ -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}, @@ -27,7 +27,7 @@ impl Object { let section_count = cursor.read_u32::()?; let mut sections = Vec::new(); - for _ in 0 .. section_count { + for _ in 0..section_count { let section = Section::from_bytes(&mut cursor)?; sections.push(section); } @@ -94,7 +94,7 @@ impl Section { let start = cursor.position() as usize; let end = start + len as usize; - let bytes = &cursor.get_ref()[start .. end]; + let bytes = &cursor.get_ref()[start..end]; let kind: SectionKind = cursor.read_u8()?.try_into()?; match kind { SectionKind::Data => Section::data_section_from_bytes(bytes), @@ -131,7 +131,7 @@ impl Section { let mut cursor = Cursor::new(bytes); let entry_count = cursor.read_u64::()?; let mut entries = HashMap::new(); - for _ in 0 .. entry_count { + for _ in 0..entry_count { // key let key_len = cursor.read_u64::()?; let mut key_bytes = vec![0u8; key_len as usize]; @@ -142,8 +142,6 @@ impl Section { let value = cursor.read_u64::()?; entries.insert(key, value); } - Ok(Section::Meta { - entries - }) + Ok(Section::Meta { entries }) } } diff --git a/src/vm/obj/syn/ast.rs b/src/vm/obj/syn/ast.rs index 27fd306..e6709d4 100644 --- a/src/vm/obj/syn/ast.rs +++ b/src/vm/obj/syn/ast.rs @@ -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 { diff --git a/src/vm/obj/syn/error.rs b/src/vm/obj/syn/error.rs index 3fdcb22..229bcbc 100644 --- a/src/vm/obj/syn/error.rs +++ b/src/vm/obj/syn/error.rs @@ -1,8 +1,5 @@ use snafu::Snafu; -use std::{ - fmt::Debug, - io, -}; +use std::{fmt::Debug, io}; #[derive(Debug, Snafu)] pub enum ParseError { diff --git a/src/vm/tick.rs b/src/vm/tick.rs index 2fdb46e..d6ef601 100644 --- a/src/vm/tick.rs +++ b/src/vm/tick.rs @@ -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 => {