Add assembler and execution logic

Most everything works, but there's one small bug with the execution
involving jumps - still have to figure that one out.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-02-18 17:44:41 -05:00
parent 0598bd1526
commit b0ef49bc2a
11 changed files with 788 additions and 78 deletions

View File

@@ -46,6 +46,7 @@ instructions! {
DUMP = 0xF002,
}
#[derive(Debug, Clone, Copy)]
pub enum Inst {
Add(Dest, Source),
Sub(Dest, Source),
@@ -98,6 +99,33 @@ impl Inst {
Inst::Dump => DUMP,
}
}
pub fn len(&self) -> usize {
match self {
Inst::Add(dest, source)
| Inst::Sub(dest, source)
| Inst::Mul(dest, source)
| Inst::Div(dest, source)
| Inst::Mod(dest, source)
| Inst::And(dest, source)
| Inst::Or(dest, source)
| Inst::Xor(dest, source)
| Inst::Shl(dest, source)
| Inst::Shr(dest, source)
| Inst::INeg(dest, source)
| Inst::Inv(dest, source)
| Inst::Not(dest, source)
| Inst::Mov(dest, source) => { 3 + dest.len() + source.len() }
Inst::CmpEq(s1, s2)
| Inst::CmpLt(s1, s2) => { 3 + s1.len() + s2.len() }
Inst::Jmp(v)
| Inst::Jz(v)
| Inst::Jnz(v) => { 3 + v.len() }
Inst::Halt
| Inst::Nop
| Inst::Dump => { 2 }
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -110,6 +138,16 @@ pub enum Source {
Imm(u64),
}
impl Source {
pub fn len(&self) -> usize {
match self {
Source::Addr64(_) | Source::Addr32(_) | Source::Addr16(_) | Source::Addr8(_) => 8,
Source::Reg(_) => 1,
Source::Imm(_) => 8,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Dest {
Addr64(Addr),
@@ -119,6 +157,15 @@ pub enum Dest {
Reg(Reg),
}
impl Dest {
pub fn len(&self) -> usize {
match self {
Dest::Addr64(_) | Dest::Addr32(_) | Dest::Addr16(_) | Dest::Addr8(_) => 8,
Dest::Reg(_) => 1,
}
}
}
pub const DEST_ADDR64: u8 = 0b0000;
pub const DEST_ADDR32: u8 = 0b0001;
pub const DEST_ADDR16: u8 = 0b0010;