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

@@ -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},
@@ -27,7 +27,7 @@ impl Object {
let section_count = cursor.read_u32::<LE>()?;
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::<LE>()?;
let mut entries = HashMap::new();
for _ in 0 .. entry_count {
for _ in 0..entry_count {
// key
let key_len = cursor.read_u64::<LE>()?;
let mut key_bytes = vec![0u8; key_len as usize];
@@ -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 {