wip bytecode format specification in rust

This commit is contained in:
tali 2023-12-16 14:15:39 -05:00
parent 5ebb404276
commit d65bef661e
7 changed files with 186 additions and 0 deletions

1
.cargo/config.toml Normal file
View File

@ -0,0 +1 @@
build.target-dir = "_build/native"

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "spicei"
version = "0.1.0"

3
Cargo.toml Normal file
View File

@ -0,0 +1,3 @@
[workspace]
members = ["native"]
resolver = "2"

6
native/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "spicei"
version = "0.1.0"
edition = "2021"
[dependencies]

1
native/src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod opcodes;

2
native/src/main.rs Normal file
View File

@ -0,0 +1,2 @@
fn main() {}

166
native/src/opcodes.rs Normal file
View File

@ -0,0 +1,166 @@
use std::fmt;
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Reg(pub u16);
impl fmt::Display for Reg {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "R{}", self.0)
}
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[repr(transparent)]
pub struct ConstIdx(pub u16);
impl fmt::Display for ConstIdx {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({})", self.0)
}
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum ArgV {
R(Reg),
K(ConstIdx),
}
impl fmt::Display for ArgV {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::R(r) => r.fmt(f),
Self::K(k) => k.fmt(f),
}
}
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum ArgL {
SubI(Reg, i16),
SubR(Reg, Reg),
}
impl fmt::Display for ArgL {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::SubI(r, l) => write!(f, "{r}[{l}]"),
Self::SubR(r, l) => write!(f, "{r}[{l}]"),
}
}
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum Ins {
O(InsO, i32),
V(InsV, ArgV),
RV(InsRV, Reg, ArgV),
RL(InsRL, Reg, ArgL),
LV(InsLV, ArgL, ArgV),
RRK(InsRRK, Reg, Reg, ConstIdx),
}
impl fmt::Display for Ins {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::O(InsO::JMP, o) => write!(f, "jmp {o:+}"),
Self::V(InsV::RET, v) => write!(f, "ret {v}"),
Self::V(InsV::BTR, v) => write!(f, "btr {v}"),
Self::RV(InsRV::Op(opr), r, v) => write!(f, "{opr} {r}, {v}"),
Self::RV(InsRV::Br(cmp), r, v) => write!(f, "b{cmp} {r}, {v}"),
Self::RL(InsRL::Get(opr), r, l) => write!(f, "{opr} {r}, {l}"),
Self::RL(InsRL::CAL(n), r, l) => write!(f, "cal {r}, {l}({r},...{n})"),
Self::RL(InsRL::RETCAL(n), r, l) => write!(f, "ret cal {l}({r},...{n})"),
Self::LV(InsLV::Set(opr), l, v) => write!(f, "{opr} {l}, {v}"),
Self::RRK(InsRRK::LOC, r, v, k) => write!(f, "loc {r}, {v}, {k}"),
}
}
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum InsV {
RET,
BTR,
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum InsRV {
Op(Opr),
Br(Cmp),
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum InsRL {
Get(Opr),
CAL(u16),
RETCAL(u16),
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum InsLV {
Set(Opr),
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum InsRRK {
LOC,
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum InsO {
JMP,
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum Opr {
MOV,
CON,
NOT,
NEG,
ADD,
SUB,
MUL,
DIV,
MOD,
Cmp(Cmp),
}
impl fmt::Display for Opr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MOV => write!(f, "mov"),
Self::CON => write!(f, "con"),
Self::NOT => write!(f, "not"),
Self::NEG => write!(f, "neg"),
Self::ADD => write!(f, "add"),
Self::SUB => write!(f, "sub"),
Self::MUL => write!(f, "mul"),
Self::DIV => write!(f, "div"),
Self::MOD => write!(f, "mod"),
Self::Cmp(cmp) => write!(f, "c{cmp}"),
}
}
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum Cmp {
EQ,
NE,
LT,
GT,
LE,
GE,
}
impl fmt::Display for Cmp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EQ => write!(f, "eq"),
Self::NE => write!(f, "ne"),
Self::LT => write!(f, "lt"),
Self::GT => write!(f, "gt"),
Self::LE => write!(f, "le"),
Self::GE => write!(f, "ge"),
}
}
}