forked from AbleOS/holey-bytes
Compare commits
9 commits
master
...
feature/tr
Author | SHA1 | Date | |
---|---|---|---|
Erin | ce878c2319 | ||
Erin | 498e729c90 | ||
Erin | 6356b7dd24 | ||
Erin | a47b556317 | ||
Erin | b237ad90ba | ||
Erin | 4fd96e1b0e | ||
Erin | 965a6a2c19 | ||
Erin | a08e0172c9 | ||
Erin | 60ca26dcd2 |
45
Cargo.lock
generated
45
Cargo.lock
generated
|
@ -13,22 +13,6 @@ dependencies = [
|
||||||
"version_check",
|
"version_check",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "allocator-api2"
|
|
||||||
version = "0.2.15"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "56fc6cf8dc8c4158eed8649f9b8b0ea1518eb62b544fe9490d66fa0b349eafe9"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "ariadne"
|
|
||||||
version = "0.3.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "72fe02fc62033df9ba41cba57ee19acf5e742511a140c7dbc3a873e19a19a1bd"
|
|
||||||
dependencies = [
|
|
||||||
"unicode-width",
|
|
||||||
"yansi",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "beef"
|
name = "beef"
|
||||||
version = "0.5.2"
|
version = "0.5.2"
|
||||||
|
@ -86,22 +70,10 @@ dependencies = [
|
||||||
"ahash",
|
"ahash",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "hashbrown"
|
|
||||||
version = "0.14.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a"
|
|
||||||
dependencies = [
|
|
||||||
"ahash",
|
|
||||||
"allocator-api2",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hbasm"
|
name = "hbasm"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ariadne",
|
|
||||||
"hashbrown 0.14.0",
|
|
||||||
"hbbytecode",
|
"hbbytecode",
|
||||||
"lasso",
|
"lasso",
|
||||||
"logos",
|
"logos",
|
||||||
|
@ -118,7 +90,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"delegate",
|
"delegate",
|
||||||
"derive_more",
|
"derive_more",
|
||||||
"hashbrown 0.13.2",
|
"hashbrown",
|
||||||
"hbbytecode",
|
"hbbytecode",
|
||||||
"log",
|
"log",
|
||||||
"paste",
|
"paste",
|
||||||
|
@ -131,8 +103,7 @@ version = "0.7.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4644821e1c3d7a560fe13d842d13f587c07348a1a05d3a797152d41c90c56df2"
|
checksum = "4644821e1c3d7a560fe13d842d13f587c07348a1a05d3a797152d41c90c56df2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ahash",
|
"hashbrown",
|
||||||
"hashbrown 0.13.2",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -261,20 +232,8 @@ version = "1.0.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0"
|
checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "unicode-width"
|
|
||||||
version = "0.1.10"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "version_check"
|
name = "version_check"
|
||||||
version = "0.9.4"
|
version = "0.9.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "yansi"
|
|
||||||
version = "0.5.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"
|
|
||||||
|
|
|
@ -5,14 +5,8 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
hbbytecode = { path = "../hbbytecode" }
|
hbbytecode = { path = "../hbbytecode" }
|
||||||
paste = "1.0"
|
lasso = "0.7"
|
||||||
hashbrown = "0.14.0"
|
paste = "1.0"
|
||||||
ariadne = "0.3.0"
|
|
||||||
|
|
||||||
[dependencies.lasso]
|
|
||||||
version = "0.7"
|
|
||||||
default-features = false
|
|
||||||
features = ["no-std"]
|
|
||||||
|
|
||||||
[dependencies.logos]
|
[dependencies.logos]
|
||||||
version = "0.13"
|
version = "0.13"
|
||||||
|
|
|
@ -5,14 +5,3 @@ start:
|
||||||
-- Uses r20 to set the port
|
-- Uses r20 to set the port
|
||||||
init_serial_port:
|
init_serial_port:
|
||||||
add r20, r30, r10
|
add r20, r30, r10
|
||||||
li r20, 00
|
|
||||||
|
|
||||||
-- outb(PORT + 1, 0x00); // Disable all interrupts
|
|
||||||
-- outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
|
|
||||||
-- outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
|
|
||||||
-- outb(PORT + 1, 0x00); // (hi byte)
|
|
||||||
-- outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit
|
|
||||||
-- outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
|
|
||||||
-- outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
|
|
||||||
-- outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
|
|
||||||
-- outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)
|
|
308
hbasm/src/lib.rs
308
hbasm/src/lib.rs
|
@ -1,65 +1,269 @@
|
||||||
#![no_std]
|
use std::collections::HashMap;
|
||||||
#![feature(error_in_core)]
|
use {
|
||||||
|
lasso::{Rodeo, Spur},
|
||||||
|
logos::{Lexer, Logos, Span},
|
||||||
|
std::fmt::{Display, Formatter},
|
||||||
|
};
|
||||||
|
|
||||||
extern crate alloc;
|
macro_rules! tokendef {
|
||||||
|
($($opcode:literal),* $(,)?) => {
|
||||||
|
paste::paste! {
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Logos)]
|
||||||
|
#[logos(extras = Rodeo)]
|
||||||
|
#[logos(skip r"[ \t\f]+")]
|
||||||
|
#[logos(skip r"-- .*")]
|
||||||
|
pub enum Token {
|
||||||
|
$(#[token($opcode, |_| hbbytecode::opcode::[<$opcode:upper>])])*
|
||||||
|
OpCode(u8),
|
||||||
|
|
||||||
pub mod text;
|
#[regex("[0-9]+", |l| l.slice().parse().ok())]
|
||||||
|
#[regex(
|
||||||
|
"-[0-9]+",
|
||||||
|
|lexer| {
|
||||||
|
Some(u64::from_ne_bytes(lexer.slice().parse::<i64>().ok()?.to_ne_bytes()))
|
||||||
|
},
|
||||||
|
)] Integer(u64),
|
||||||
|
|
||||||
mod macros;
|
#[regex(
|
||||||
|
"r[0-9]+",
|
||||||
|
|lexer| match lexer.slice()[1..].parse() {
|
||||||
|
Ok(n) => Some(n),
|
||||||
|
_ => None
|
||||||
|
},
|
||||||
|
)] Register(u8),
|
||||||
|
|
||||||
use {alloc::vec::Vec, hashbrown::HashSet};
|
#[regex(
|
||||||
|
r"\p{XID_Start}\p{XID_Continue}*:",
|
||||||
|
|lexer| lexer.extras.get_or_intern(&lexer.slice()[..lexer.slice().len() - 1]),
|
||||||
|
)] Label(Spur),
|
||||||
|
|
||||||
#[derive(Default)]
|
#[regex(
|
||||||
pub struct Assembler {
|
r"\p{XID_Start}\p{XID_Continue}*",
|
||||||
pub buf: Vec<u8>,
|
|lexer| lexer.extras.get_or_intern(lexer.slice()),
|
||||||
sub: HashSet<usize>,
|
)] Symbol(Spur),
|
||||||
}
|
|
||||||
|
|
||||||
impl Assembler {
|
#[token("\n")]
|
||||||
macros::impl_asm!(
|
#[token(";")] ISep,
|
||||||
bbbb(p0: u8, p1: u8, p2: u8, p3: u8)
|
#[token(",")] PSep,
|
||||||
=> [DIR, DIRF, FMAF],
|
|
||||||
bbb(p0: u8, p1: u8, p2: u8)
|
|
||||||
=> [ADD, SUB, MUL, AND, OR, XOR, SL, SR, SRS, CMP, CMPU, BRC, ADDF, SUBF, MULF],
|
|
||||||
bbdh(p0: u8, p1: u8, p2: impl Imm, p3: u16)
|
|
||||||
=> [LD, ST],
|
|
||||||
bbd(p0: u8, p1: u8, p2: impl Imm)
|
|
||||||
=> [ADDI, MULI, ANDI, ORI, XORI, SLI, SRI, SRSI, CMPI, CMPUI,
|
|
||||||
BMC, JEQ, JNE, JLT, JGT, JLTU, JGTU, ADDFI, MULFI],
|
|
||||||
bb(p0: u8, p1: u8)
|
|
||||||
=> [NEG, NOT, CP, SWA, NEGF, ITF, FTI],
|
|
||||||
bd(p0: u8, p1: impl Imm)
|
|
||||||
=> [LI, JMP],
|
|
||||||
n()
|
|
||||||
=> [NOP, ECALL],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Imm {
|
|
||||||
fn insert(&self, asm: &mut Assembler);
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! impl_imm_le_bytes {
|
|
||||||
($($ty:ty),* $(,)?) => {
|
|
||||||
$(
|
|
||||||
impl Imm for $ty {
|
|
||||||
#[inline(always)]
|
|
||||||
fn insert(&self, asm: &mut Assembler) {
|
|
||||||
asm.buf.extend(self.to_le_bytes());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)*
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_imm_le_bytes!(u64, i64, f64);
|
#[rustfmt::skip]
|
||||||
|
tokendef![
|
||||||
|
"nop", "add", "sub", "mul", "and", "or", "xor", "sl", "sr", "srs", "cmp", "cmpu",
|
||||||
|
"dir", "neg", "not", "addi", "muli", "andi", "ori", "xori", "sli", "sri", "srsi",
|
||||||
|
"cmpi", "cmpui", "cp", "swa", "li", "ld", "st", "bmc", "brc", "jmp", "jeq", "jne",
|
||||||
|
"jlt", "jgt", "jltu", "jgtu", "ecall", "addf", "mulf", "dirf", "addfi", "mulfi",
|
||||||
|
];
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
pub struct Symbol(pub u64);
|
pub enum ErrorKind {
|
||||||
impl Imm for Symbol {
|
UnexpectedToken,
|
||||||
#[inline(always)]
|
InvalidToken,
|
||||||
fn insert(&self, asm: &mut Assembler) {
|
UnexpectedEnd,
|
||||||
asm.sub.insert(asm.buf.len());
|
InvalidSymbol,
|
||||||
asm.buf.extend(self.0.to_le_bytes());
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
|
pub struct Error {
|
||||||
|
pub kind: ErrorKind,
|
||||||
|
pub span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Error {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "Error {:?} at {:?}", self.kind, self.span)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for Error {}
|
||||||
|
|
||||||
|
macro_rules! expect_matches {
|
||||||
|
($self:expr, $($pat:pat),* $(,)?) => {$(
|
||||||
|
let $pat = $self.next()?
|
||||||
|
else { return Err(ErrorKind::UnexpectedToken) };
|
||||||
|
)*}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn assembly(code: &str, buf: &mut Vec<u8>) -> Result<(), Error> {
|
||||||
|
struct Assembler<'a> {
|
||||||
|
lexer: Lexer<'a, Token>,
|
||||||
|
buf: &'a mut Vec<u8>,
|
||||||
|
label_map: HashMap<Spur, u64>,
|
||||||
|
to_sub_label: HashMap<usize, Spur>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Assembler<'a> {
|
||||||
|
fn next(&mut self) -> Result<Token, ErrorKind> {
|
||||||
|
match self.lexer.next() {
|
||||||
|
Some(Ok(t)) => Ok(t),
|
||||||
|
Some(Err(())) => Err(ErrorKind::InvalidToken),
|
||||||
|
None => Err(ErrorKind::UnexpectedEnd),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assemble(&mut self) -> Result<(), ErrorKind> {
|
||||||
|
use hbbytecode::opcode::*;
|
||||||
|
loop {
|
||||||
|
match self.lexer.next() {
|
||||||
|
Some(Ok(Token::OpCode(op))) => {
|
||||||
|
self.buf.push(op);
|
||||||
|
match op {
|
||||||
|
NOP | ECALL => Ok(()),
|
||||||
|
DIR | DIRF => {
|
||||||
|
expect_matches!(
|
||||||
|
self,
|
||||||
|
Token::Register(r0),
|
||||||
|
Token::PSep,
|
||||||
|
Token::Register(r1),
|
||||||
|
Token::PSep,
|
||||||
|
Token::Register(r2),
|
||||||
|
Token::PSep,
|
||||||
|
Token::Register(r3),
|
||||||
|
);
|
||||||
|
self.buf.extend([r0, r1, r2, r3]);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
ADD..=CMPU | ADDF..=MULF => {
|
||||||
|
expect_matches!(
|
||||||
|
self,
|
||||||
|
Token::Register(r0),
|
||||||
|
Token::PSep,
|
||||||
|
Token::Register(r1),
|
||||||
|
Token::PSep,
|
||||||
|
Token::Register(r2),
|
||||||
|
);
|
||||||
|
self.buf.extend([r0, r1, r2]);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
BRC => {
|
||||||
|
expect_matches!(
|
||||||
|
self,
|
||||||
|
Token::Register(r0),
|
||||||
|
Token::PSep,
|
||||||
|
Token::Register(r1),
|
||||||
|
Token::PSep,
|
||||||
|
Token::Integer(count),
|
||||||
|
);
|
||||||
|
self.buf.extend([
|
||||||
|
r0,
|
||||||
|
r1,
|
||||||
|
u8::try_from(count).map_err(|_| ErrorKind::UnexpectedToken)?,
|
||||||
|
]);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
NEG..=NOT | CP..=SWA => {
|
||||||
|
expect_matches!(
|
||||||
|
self,
|
||||||
|
Token::Register(r0),
|
||||||
|
Token::PSep,
|
||||||
|
Token::Register(r1),
|
||||||
|
);
|
||||||
|
self.buf.extend([r0, r1]);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
LI | JMP => {
|
||||||
|
expect_matches!(self, Token::Register(r0), Token::PSep);
|
||||||
|
self.buf.push(r0);
|
||||||
|
self.insert_imm()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
ADDI..=CMPUI | BMC | JEQ..=JGTU | ADDFI..=MULFI => {
|
||||||
|
expect_matches!(
|
||||||
|
self,
|
||||||
|
Token::Register(r0),
|
||||||
|
Token::PSep,
|
||||||
|
Token::Register(r1),
|
||||||
|
Token::PSep,
|
||||||
|
);
|
||||||
|
self.buf.extend([r0, r1]);
|
||||||
|
self.insert_imm()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
LD..=ST => {
|
||||||
|
expect_matches!(
|
||||||
|
self,
|
||||||
|
Token::Register(r0),
|
||||||
|
Token::PSep,
|
||||||
|
Token::Register(r1),
|
||||||
|
Token::PSep,
|
||||||
|
Token::Integer(offset),
|
||||||
|
Token::PSep,
|
||||||
|
Token::Integer(len),
|
||||||
|
);
|
||||||
|
self.buf.extend([r0, r1]);
|
||||||
|
self.buf.extend(offset.to_le_bytes());
|
||||||
|
self.buf.extend(
|
||||||
|
u16::try_from(len)
|
||||||
|
.map_err(|_| ErrorKind::InvalidToken)?
|
||||||
|
.to_le_bytes(),
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}?;
|
||||||
|
match self.next() {
|
||||||
|
Ok(Token::ISep) => (),
|
||||||
|
Ok(_) => return Err(ErrorKind::UnexpectedToken),
|
||||||
|
Err(ErrorKind::UnexpectedEnd) => return Ok(()),
|
||||||
|
Err(e) => return Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(Ok(Token::Label(lbl))) => {
|
||||||
|
self.label_map.insert(lbl, self.buf.len() as u64);
|
||||||
|
}
|
||||||
|
Some(Ok(Token::ISep)) => (),
|
||||||
|
Some(Ok(_)) => return Err(ErrorKind::UnexpectedToken),
|
||||||
|
Some(Err(())) => return Err(ErrorKind::InvalidToken),
|
||||||
|
None => return Ok(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn link_local_syms(&mut self) -> Result<(), ErrorKind> {
|
||||||
|
for (ix, sym) in &self.to_sub_label {
|
||||||
|
self.label_map
|
||||||
|
.get(sym)
|
||||||
|
.ok_or(ErrorKind::InvalidSymbol)?
|
||||||
|
.to_le_bytes()
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.for_each(|(i, b)| {
|
||||||
|
self.buf[ix + i] = *b;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn insert_imm(&mut self) -> Result<(), ErrorKind> {
|
||||||
|
let imm = match self.next()? {
|
||||||
|
Token::Integer(i) => i.to_le_bytes(),
|
||||||
|
Token::Symbol(s) => {
|
||||||
|
self.to_sub_label.insert(self.buf.len(), s);
|
||||||
|
[0; 8]
|
||||||
|
}
|
||||||
|
_ => return Err(ErrorKind::UnexpectedToken),
|
||||||
|
};
|
||||||
|
self.buf.extend(imm);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut asm = Assembler {
|
||||||
|
lexer: Token::lexer(code),
|
||||||
|
label_map: Default::default(),
|
||||||
|
to_sub_label: Default::default(),
|
||||||
|
buf,
|
||||||
|
};
|
||||||
|
|
||||||
|
asm.assemble().map_err(|kind| Error {
|
||||||
|
kind,
|
||||||
|
span: asm.lexer.span(),
|
||||||
|
})?;
|
||||||
|
|
||||||
|
asm.link_local_syms()
|
||||||
|
.map_err(|kind| Error { kind, span: 0..0 })
|
||||||
|
}
|
||||||
|
|
|
@ -1,72 +0,0 @@
|
||||||
macro_rules! impl_asm_opcodes {
|
|
||||||
(
|
|
||||||
$generic:ident
|
|
||||||
($($param_i:ident: $param_ty:ty),*)
|
|
||||||
=> []
|
|
||||||
) => {};
|
|
||||||
|
|
||||||
(
|
|
||||||
$generic:ident
|
|
||||||
($($param_i:ident: $param_ty:ty),*)
|
|
||||||
=> [$opcode:ident, $($rest:tt)*]
|
|
||||||
) => {
|
|
||||||
paste::paste! {
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn [<i_ $opcode:lower>](&mut self, $($param_i: $param_ty),*) {
|
|
||||||
self.$generic(hbbytecode::opcode::$opcode, $($param_i),*)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
macros::impl_asm_opcodes!(
|
|
||||||
$generic($($param_i: $param_ty),*)
|
|
||||||
=> [$($rest)*]
|
|
||||||
);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! gen_impl_asm_insert {
|
|
||||||
($($ty:ident),* $(,)?) => {
|
|
||||||
macro_rules! impl_asm_insert {
|
|
||||||
$(($self:expr, $id:ident, $ty) => {
|
|
||||||
$self.buf.extend($id.to_le_bytes())
|
|
||||||
};)*
|
|
||||||
|
|
||||||
($self:expr, $id:ident, $_:ty) => {
|
|
||||||
Imm::insert(&$id, $self)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
gen_impl_asm_insert!(u8, u16, u64);
|
|
||||||
|
|
||||||
macro_rules! impl_asm {
|
|
||||||
(
|
|
||||||
$(
|
|
||||||
$ityn:ident
|
|
||||||
($($param_i:ident: $param_ty:ty),* $(,)?)
|
|
||||||
=> [$($opcode:ident),* $(,)?],
|
|
||||||
)*
|
|
||||||
) => {
|
|
||||||
paste::paste! {
|
|
||||||
$(
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn [<i_param_ $ityn>](&mut self, opcode: u8, $($param_i: $param_ty),*) {
|
|
||||||
self.buf.push(opcode);
|
|
||||||
$(macros::impl_asm_insert!(self, $param_i, $param_ty);)*
|
|
||||||
}
|
|
||||||
|
|
||||||
macros::impl_asm_opcodes!(
|
|
||||||
[<i_param_ $ityn>]($($param_i: $param_ty),*)
|
|
||||||
=> [$($opcode,)*]
|
|
||||||
);
|
|
||||||
)*
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) use {impl_asm, impl_asm_opcodes};
|
|
||||||
|
|
||||||
#[allow(clippy::single_component_path_imports)]
|
|
||||||
pub(super) use impl_asm_insert;
|
|
|
@ -1,9 +1,6 @@
|
||||||
use {
|
use std::{
|
||||||
ariadne::{ColorGenerator, Label, Report, ReportKind, Source},
|
error::Error,
|
||||||
std::{
|
io::{stdin, stdout, Read, Write},
|
||||||
error::Error,
|
|
||||||
io::{stdin, Read},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
@ -11,36 +8,14 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
stdin().read_to_string(&mut code)?;
|
stdin().read_to_string(&mut code)?;
|
||||||
|
|
||||||
let mut buf = vec![];
|
let mut buf = vec![];
|
||||||
|
if let Err(e) = hbasm::assembly(&code, &mut buf) {
|
||||||
if let Err(e) = hbasm::text::assembly(&code, &mut buf) {
|
eprintln!(
|
||||||
let mut colors = ColorGenerator::new();
|
"Error {:?} at {:?} (`{}`)",
|
||||||
|
e.kind,
|
||||||
let e_code = match e.kind {
|
e.span.clone(),
|
||||||
hbasm::text::ErrorKind::UnexpectedToken => 1,
|
&code[e.span],
|
||||||
hbasm::text::ErrorKind::InvalidToken => 2,
|
);
|
||||||
hbasm::text::ErrorKind::UnexpectedEnd => 3,
|
|
||||||
hbasm::text::ErrorKind::InvalidSymbol => 4,
|
|
||||||
};
|
|
||||||
let message = match e.kind {
|
|
||||||
hbasm::text::ErrorKind::UnexpectedToken => "This token is not expected!",
|
|
||||||
hbasm::text::ErrorKind::InvalidToken => "The token is not valid!",
|
|
||||||
hbasm::text::ErrorKind::UnexpectedEnd => "The assembler reached the end of input unexpectedly!",
|
|
||||||
hbasm::text::ErrorKind::InvalidSymbol => "This referenced symbol doesn't have a corresponding label!",
|
|
||||||
};
|
|
||||||
let a = colors.next();
|
|
||||||
|
|
||||||
Report::build(ReportKind::Error, "engine_internal", e.span.clone().start)
|
|
||||||
.with_code(e_code)
|
|
||||||
.with_message(format!("{:?}", e.kind))
|
|
||||||
.with_label(
|
|
||||||
Label::new(("engine_internal", e.span.clone()))
|
|
||||||
.with_message(message)
|
|
||||||
.with_color(a),
|
|
||||||
)
|
|
||||||
.finish()
|
|
||||||
.eprint(("engine_internal", Source::from(&code)))
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
stdout().write_all(&buf)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,273 +0,0 @@
|
||||||
extern crate alloc;
|
|
||||||
use alloc::vec::Vec;
|
|
||||||
|
|
||||||
use {
|
|
||||||
core::fmt::{Display, Formatter},
|
|
||||||
hashbrown::HashMap,
|
|
||||||
lasso::{Rodeo, Spur},
|
|
||||||
logos::{Lexer, Logos, Span},
|
|
||||||
};
|
|
||||||
|
|
||||||
macro_rules! tokendef {
|
|
||||||
($($opcode:literal),* $(,)?) => {
|
|
||||||
paste::paste! {
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Logos)]
|
|
||||||
#[logos(extras = Rodeo)]
|
|
||||||
#[logos(skip r"[ \t\f]+")]
|
|
||||||
#[logos(skip r"-- .*")]
|
|
||||||
pub enum Token {
|
|
||||||
$(#[token($opcode, |_| hbbytecode::opcode::[<$opcode:upper>])])*
|
|
||||||
OpCode(u8),
|
|
||||||
|
|
||||||
#[regex("[0-9]+", |l| l.slice().parse().ok())]
|
|
||||||
#[regex(
|
|
||||||
"-[0-9]+",
|
|
||||||
|lexer| {
|
|
||||||
Some(u64::from_ne_bytes(lexer.slice().parse::<i64>().ok()?.to_ne_bytes()))
|
|
||||||
},
|
|
||||||
)] Integer(u64),
|
|
||||||
|
|
||||||
#[regex(
|
|
||||||
"r[0-9]+",
|
|
||||||
|lexer| match lexer.slice()[1..].parse() {
|
|
||||||
Ok(n) => Some(n),
|
|
||||||
_ => None
|
|
||||||
},
|
|
||||||
)] Register(u8),
|
|
||||||
|
|
||||||
#[regex(
|
|
||||||
r"\p{XID_Start}\p{XID_Continue}*:",
|
|
||||||
|lexer| lexer.extras.get_or_intern(&lexer.slice()[..lexer.slice().len() - 1]),
|
|
||||||
)] Label(Spur),
|
|
||||||
|
|
||||||
#[regex(
|
|
||||||
r"\p{XID_Start}\p{XID_Continue}*",
|
|
||||||
|lexer| lexer.extras.get_or_intern(lexer.slice()),
|
|
||||||
)] Symbol(Spur),
|
|
||||||
|
|
||||||
#[token("\n")]
|
|
||||||
#[token(";")] ISep,
|
|
||||||
#[token(",")] PSep,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[rustfmt::skip]
|
|
||||||
tokendef![
|
|
||||||
"nop", "add", "sub", "mul", "and", "or", "xor", "sl", "sr", "srs", "cmp", "cmpu",
|
|
||||||
"dir", "neg", "not", "addi", "muli", "andi", "ori", "xori", "sli", "sri", "srsi",
|
|
||||||
"cmpi", "cmpui", "cp", "swa", "li", "ld", "st", "bmc", "brc", "jmp", "jeq", "jne",
|
|
||||||
"jlt", "jgt", "jltu", "jgtu", "ecall", "addf", "subf", "mulf", "dirf", "fmaf", "negf",
|
|
||||||
"itf", "fti", "addfi", "mulfi",
|
|
||||||
];
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
||||||
pub enum ErrorKind {
|
|
||||||
UnexpectedToken,
|
|
||||||
InvalidToken,
|
|
||||||
UnexpectedEnd,
|
|
||||||
InvalidSymbol,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
||||||
pub struct Error {
|
|
||||||
pub kind: ErrorKind,
|
|
||||||
pub span: Span,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Error {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
|
||||||
write!(f, "Error {:?} at {:?}", self.kind, self.span)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl core::error::Error for Error {}
|
|
||||||
|
|
||||||
macro_rules! expect_matches {
|
|
||||||
($self:expr, $($pat:pat),* $(,)?) => {$(
|
|
||||||
let $pat = $self.next()?
|
|
||||||
else { return Err(ErrorKind::UnexpectedToken) };
|
|
||||||
)*}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn assembly(code: &str, buf: &mut Vec<u8>) -> Result<(), Error> {
|
|
||||||
struct Assembler<'a> {
|
|
||||||
lexer: Lexer<'a, Token>,
|
|
||||||
buf: &'a mut Vec<u8>,
|
|
||||||
label_map: HashMap<Spur, u64>,
|
|
||||||
to_sub_label: HashMap<usize, Spur>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Assembler<'a> {
|
|
||||||
fn next(&mut self) -> Result<Token, ErrorKind> {
|
|
||||||
match self.lexer.next() {
|
|
||||||
Some(Ok(t)) => Ok(t),
|
|
||||||
Some(Err(())) => Err(ErrorKind::InvalidToken),
|
|
||||||
None => Err(ErrorKind::UnexpectedEnd),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn assemble(&mut self) -> Result<(), ErrorKind> {
|
|
||||||
use hbbytecode::opcode::*;
|
|
||||||
loop {
|
|
||||||
match self.lexer.next() {
|
|
||||||
Some(Ok(Token::OpCode(op))) => {
|
|
||||||
self.buf.push(op);
|
|
||||||
match op {
|
|
||||||
NOP | ECALL => Ok(()),
|
|
||||||
DIR | DIRF => {
|
|
||||||
expect_matches!(
|
|
||||||
self,
|
|
||||||
Token::Register(r0),
|
|
||||||
Token::PSep,
|
|
||||||
Token::Register(r1),
|
|
||||||
Token::PSep,
|
|
||||||
Token::Register(r2),
|
|
||||||
Token::PSep,
|
|
||||||
Token::Register(r3),
|
|
||||||
);
|
|
||||||
self.buf.extend([r0, r1, r2, r3]);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
ADD..=CMPU | ADDF..=MULF => {
|
|
||||||
expect_matches!(
|
|
||||||
self,
|
|
||||||
Token::Register(r0),
|
|
||||||
Token::PSep,
|
|
||||||
Token::Register(r1),
|
|
||||||
Token::PSep,
|
|
||||||
Token::Register(r2),
|
|
||||||
);
|
|
||||||
self.buf.extend([r0, r1, r2]);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
BRC => {
|
|
||||||
expect_matches!(
|
|
||||||
self,
|
|
||||||
Token::Register(r0),
|
|
||||||
Token::PSep,
|
|
||||||
Token::Register(r1),
|
|
||||||
Token::PSep,
|
|
||||||
Token::Integer(count),
|
|
||||||
);
|
|
||||||
self.buf.extend([
|
|
||||||
r0,
|
|
||||||
r1,
|
|
||||||
u8::try_from(count).map_err(|_| ErrorKind::UnexpectedToken)?,
|
|
||||||
]);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
NEG..=NOT | CP..=SWA | NEGF..=FTI => {
|
|
||||||
expect_matches!(
|
|
||||||
self,
|
|
||||||
Token::Register(r0),
|
|
||||||
Token::PSep,
|
|
||||||
Token::Register(r1),
|
|
||||||
);
|
|
||||||
self.buf.extend([r0, r1]);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
LI | JMP => {
|
|
||||||
expect_matches!(self, Token::Register(r0), Token::PSep);
|
|
||||||
self.buf.push(r0);
|
|
||||||
self.insert_imm()?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
ADDI..=CMPUI | BMC | JEQ..=JGTU | ADDFI..=MULFI => {
|
|
||||||
expect_matches!(
|
|
||||||
self,
|
|
||||||
Token::Register(r0),
|
|
||||||
Token::PSep,
|
|
||||||
Token::Register(r1),
|
|
||||||
Token::PSep,
|
|
||||||
);
|
|
||||||
self.buf.extend([r0, r1]);
|
|
||||||
self.insert_imm()?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
LD..=ST => {
|
|
||||||
expect_matches!(
|
|
||||||
self,
|
|
||||||
Token::Register(r0),
|
|
||||||
Token::PSep,
|
|
||||||
Token::Register(r1),
|
|
||||||
Token::PSep,
|
|
||||||
Token::Integer(offset),
|
|
||||||
Token::PSep,
|
|
||||||
Token::Integer(len),
|
|
||||||
);
|
|
||||||
self.buf.extend([r0, r1]);
|
|
||||||
self.buf.extend(offset.to_le_bytes());
|
|
||||||
self.buf.extend(
|
|
||||||
u16::try_from(len)
|
|
||||||
.map_err(|_| ErrorKind::InvalidToken)?
|
|
||||||
.to_le_bytes(),
|
|
||||||
);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
_ => unreachable!(),
|
|
||||||
}?;
|
|
||||||
match self.next() {
|
|
||||||
Ok(Token::ISep) => (),
|
|
||||||
Ok(_) => return Err(ErrorKind::UnexpectedToken),
|
|
||||||
Err(ErrorKind::UnexpectedEnd) => return Ok(()),
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(Ok(Token::Label(lbl))) => {
|
|
||||||
self.label_map.insert(lbl, self.buf.len() as u64);
|
|
||||||
}
|
|
||||||
Some(Ok(Token::ISep)) => (),
|
|
||||||
Some(Ok(_)) => return Err(ErrorKind::UnexpectedToken),
|
|
||||||
Some(Err(())) => return Err(ErrorKind::InvalidToken),
|
|
||||||
None => return Ok(()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn link_local_syms(&mut self) -> Result<(), ErrorKind> {
|
|
||||||
for (ix, sym) in &self.to_sub_label {
|
|
||||||
self.label_map
|
|
||||||
.get(sym)
|
|
||||||
.ok_or(ErrorKind::InvalidSymbol)?
|
|
||||||
.to_le_bytes()
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.for_each(|(i, b)| {
|
|
||||||
self.buf[ix + i] = *b;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn insert_imm(&mut self) -> Result<(), ErrorKind> {
|
|
||||||
let imm = match self.next()? {
|
|
||||||
Token::Integer(i) => i.to_le_bytes(),
|
|
||||||
Token::Symbol(s) => {
|
|
||||||
self.to_sub_label.insert(self.buf.len(), s);
|
|
||||||
[0; 8]
|
|
||||||
}
|
|
||||||
_ => return Err(ErrorKind::UnexpectedToken),
|
|
||||||
};
|
|
||||||
self.buf.extend(imm);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut asm = Assembler {
|
|
||||||
lexer: Token::lexer(code),
|
|
||||||
label_map: Default::default(),
|
|
||||||
to_sub_label: Default::default(),
|
|
||||||
buf,
|
|
||||||
};
|
|
||||||
|
|
||||||
asm.assemble().map_err(|kind| Error {
|
|
||||||
kind,
|
|
||||||
span: asm.lexer.span(),
|
|
||||||
})?;
|
|
||||||
|
|
||||||
asm.link_local_syms()
|
|
||||||
.map_err(|kind| Error { kind, span: 0..0 })
|
|
||||||
}
|
|
|
@ -4,57 +4,57 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <limits.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
static_assert(CHAR_BIT == 8, "Cursed architectures are not supported");
|
typedef enum hbbc_Opcode: uint8_t {
|
||||||
|
hbbc_Op_NOP, hbbc_Op_ADD, hbbc_Op_MUL, hbbc_Op_AND, hbbc_Op_OR, hbbc_Op_XOR, hbbc_Op_SL,
|
||||||
enum hbbc_Opcode: uint8_t {
|
hbbc_Op_SR, hbbc_Op_SRS, hbbc_Op_CMP, hbbc_Op_CMPU, hbbc_Op_DIR, hbbc_Op_NEG, hbbc_Op_NOT,
|
||||||
hbbc_Op_NOP , hbbc_Op_ADD , hbbc_Op_SUB , hbbc_Op_MUL , hbbc_Op_AND , hbbc_Op_OR ,
|
hbbc_Op_ADDI, hbbc_Op_MULI, hbbc_Op_ANDI, hbbc_Op_ORI, hbbc_Op_XORI, hbbc_Op_SLI, hbbc_Op_SRI,
|
||||||
hbbc_Op_XOR , hbbc_Op_SL , hbbc_Op_SR , hbbc_Op_SRS , hbbc_Op_CMP , hbbc_Op_CMPU ,
|
hbbc_Op_SRSI, hbbc_Op_CMPI, hbbc_Op_CMPUI, hbbc_Op_CP, hbbc_Op_SWA, hbbc_Op_LI, hbbc_Op_LD,
|
||||||
hbbc_Op_DIR , hbbc_Op_NEG , hbbc_Op_NOT , hbbc_Op_ADDI , hbbc_Op_MULI , hbbc_Op_ANDI ,
|
hbbc_Op_ST, hbbc_Op_BMC, hbbc_Op_BRC, hbbc_Op_JMP, hbbc_Op_JEQ, hbbc_Op_JNE, hbbc_Op_JLT,
|
||||||
hbbc_Op_ORI , hbbc_Op_XORI , hbbc_Op_SLI , hbbc_Op_SRI , hbbc_Op_SRSI , hbbc_Op_CMPI ,
|
hbbc_Op_JGT, hbbc_Op_JLTU, hbbc_Op_JGTU, hbbc_Op_ECALL, hbbc_Op_ADDF, hbbc_Op_MULF,
|
||||||
hbbc_Op_CMPUI , hbbc_Op_CP , hbbc_Op_SWA , hbbc_Op_LI , hbbc_Op_LD , hbbc_Op_ST ,
|
hbbc_Op_DIRF, hbbc_Op_ADDFI, hbbc_Op_MULFI,
|
||||||
hbbc_Op_BMC , hbbc_Op_BRC , hbbc_Op_JMP , hbbc_Op_JEQ , hbbc_Op_JNE , hbbc_Op_JLT ,
|
} hbbc_Opcode;
|
||||||
hbbc_Op_JGT , hbbc_Op_JLTU , hbbc_Op_JGTU , hbbc_Op_ECALL , hbbc_Op_ADDF , hbbc_Op_SUBF ,
|
|
||||||
hbbc_Op_MULF , hbbc_Op_DIRF , hbbc_Op_FMAF , hbbc_Op_NEGF , hbbc_Op_ITF , hbbc_Op_FTI ,
|
|
||||||
hbbc_Op_ADDFI , hbbc_Op_MULFI ,
|
|
||||||
} typedef hbbc_Opcode;
|
|
||||||
|
|
||||||
static_assert(sizeof(hbbc_Opcode) == 1);
|
static_assert(sizeof(hbbc_Opcode) == 1);
|
||||||
|
|
||||||
#pragma pack(push, 1)
|
#pragma pack(push, 1)
|
||||||
struct hbbc_ParamBBBB
|
typedef struct hbbc_ParamBBBB
|
||||||
{ uint8_t _0; uint8_t _1; uint8_t _2; uint8_t _3; }
|
{ uint8_t _0; uint8_t _1; uint8_t _2; uint8_t _3; }
|
||||||
typedef hbbc_ParamBBBB;
|
hbbc_ParamBBBB;
|
||||||
static_assert(sizeof(hbbc_ParamBBBB) == 32 / 8);
|
static_assert(sizeof(hbbc_ParamBBBB) == 4);
|
||||||
|
|
||||||
struct hbbc_ParamBBB
|
typedef struct hbbc_ParamBBB
|
||||||
{ uint8_t _0; uint8_t _1; uint8_t _2; }
|
{ uint8_t _0; uint8_t _1; uint8_t _2; }
|
||||||
typedef hbbc_ParamBBB;
|
hbbc_ParamBBB;
|
||||||
static_assert(sizeof(hbbc_ParamBBB) == 24 / 8);
|
static_assert(sizeof(hbbc_ParamBBB) == 3);
|
||||||
|
|
||||||
struct hbbc_ParamBBDH
|
typedef struct hbbc_ParamBBDH
|
||||||
{ uint8_t _0; uint8_t _1; uint64_t _2; uint16_t _3; }
|
{ uint8_t _0; uint8_t _1; uint64_t _2; uint16_t _3; }
|
||||||
typedef hbbc_ParamBBDH;
|
hbbc_ParamBBDH;
|
||||||
static_assert(sizeof(hbbc_ParamBBDH) == 96 / 8);
|
static_assert(sizeof(hbbc_ParamBBDH) == 12);
|
||||||
|
|
||||||
struct hbbc_ParamBBD
|
typedef struct hbbc_ParamBBDB
|
||||||
|
{ uint8_t _0; uint8_t _1; uint64_t _2; uint8_t _3; }
|
||||||
|
hbbc_ParamBBDB;
|
||||||
|
static_assert(sizeof(hbbc_ParamBBDB) == 11);
|
||||||
|
|
||||||
|
typedef struct hbbc_ParamBBD
|
||||||
{ uint8_t _0; uint8_t _1; uint64_t _2; }
|
{ uint8_t _0; uint8_t _1; uint64_t _2; }
|
||||||
typedef hbbc_ParamBBD;
|
hbbc_ParamBBD;
|
||||||
static_assert(sizeof(hbbc_ParamBBD) == 80 / 8);
|
static_assert(sizeof(hbbc_ParamBBD) == 10);
|
||||||
|
|
||||||
struct hbbc_ParamBB
|
typedef struct hbbc_ParamBB
|
||||||
{ uint8_t _0; uint8_t _1; }
|
{ uint8_t _0; uint8_t _1; }
|
||||||
typedef hbbc_ParamBB;
|
hbbc_ParamBB;
|
||||||
static_assert(sizeof(hbbc_ParamBB) == 16 / 8);
|
static_assert(sizeof(hbbc_ParamBB) == 2);
|
||||||
|
|
||||||
struct hbbc_ParamBD
|
typedef struct hbbc_ParamBD
|
||||||
{ uint8_t _0; uint64_t _1; }
|
{ uint8_t _0; uint64_t _1; }
|
||||||
typedef hbbc_ParamBD;
|
hbbc_ParamBD;
|
||||||
static_assert(sizeof(hbbc_ParamBD) == 72 / 8);
|
static_assert(sizeof(hbbc_ParamBD) == 9);
|
||||||
|
|
||||||
typedef uint64_t hbbc_ParamD;
|
typedef uint64_t hbbc_ParamD;
|
||||||
static_assert(sizeof(hbbc_ParamD) == 64 / 8);
|
static_assert(sizeof(hbbc_ParamD) == 8);
|
||||||
|
|
||||||
#pragma pack(pop)
|
#pragma pack(pop)
|
||||||
|
|
|
@ -32,7 +32,7 @@ constmod!(pub opcode(u8) {
|
||||||
CMP = 10, "BBB; #0 ← #1 <=> #2";
|
CMP = 10, "BBB; #0 ← #1 <=> #2";
|
||||||
CMPU = 11, "BBB; #0 ← #1 <=> #2 (unsigned)";
|
CMPU = 11, "BBB; #0 ← #1 <=> #2 (unsigned)";
|
||||||
DIR = 12, "BBBB; #0 ← #2 / #3, #1 ← #2 % #3";
|
DIR = 12, "BBBB; #0 ← #2 / #3, #1 ← #2 % #3";
|
||||||
NEG = 13, "BB; #0 ← -#1";
|
NEG = 13, "BB; #0 ← ~#1";
|
||||||
NOT = 14, "BB; #0 ← !#1";
|
NOT = 14, "BB; #0 ← !#1";
|
||||||
|
|
||||||
ADDI = 15, "BBD; #0 ← #1 + imm #2";
|
ADDI = 15, "BBD; #0 ← #1 + imm #2";
|
||||||
|
@ -63,17 +63,12 @@ constmod!(pub opcode(u8) {
|
||||||
JGTU = 38, "BBD; if #0 > #1 → jump imm #2 (unsigned)";
|
JGTU = 38, "BBD; if #0 > #1 → jump imm #2 (unsigned)";
|
||||||
ECALL = 39, "N; Issue system call";
|
ECALL = 39, "N; Issue system call";
|
||||||
|
|
||||||
ADDF = 40, "BBB; #0 ← #1 +. #2";
|
ADDF = 40, "BBB; #0 ← #1 +. #2";
|
||||||
SUBF = 41, "BBB; #0 ← #1 -. #2";
|
MULF = 41, "BBB; #0 ← #1 +. #2";
|
||||||
MULF = 42, "BBB; #0 ← #1 +. #2";
|
DIRF = 42, "BBBB; #0 ← #2 / #3, #1 ← #2 % #3";
|
||||||
DIRF = 43, "BBBB; #0 ← #2 / #3, #1 ← #2 % #3";
|
|
||||||
FMAF = 44, "BBBB; #0 ← (#1 * #2) + #3";
|
|
||||||
NEGF = 45, "BB; #0 ← -#1";
|
|
||||||
ITF = 46, "BB; #0 ← #1 as float";
|
|
||||||
FTI = 47, "BB; #0 ← #1 as int";
|
|
||||||
|
|
||||||
ADDFI = 48, "BBD; #0 ← #1 +. imm #2";
|
ADDFI = 43, "BBD; #0 ← #1 +. imm #2";
|
||||||
MULFI = 49, "BBD; #0 ← #1 *. imm #2";
|
MULFI = 44, "BBD; #0 ← #1 *. imm #2";
|
||||||
});
|
});
|
||||||
|
|
||||||
#[repr(packed)]
|
#[repr(packed)]
|
||||||
|
@ -85,6 +80,9 @@ pub struct ParamBBB(pub u8, pub u8, pub u8);
|
||||||
#[repr(packed)]
|
#[repr(packed)]
|
||||||
pub struct ParamBBDH(pub u8, pub u8, pub u64, pub u16);
|
pub struct ParamBBDH(pub u8, pub u8, pub u64, pub u16);
|
||||||
|
|
||||||
|
#[repr(packed)]
|
||||||
|
pub struct ParamBBDB(pub u8, pub u8, pub u64, pub u8);
|
||||||
|
|
||||||
#[repr(packed)]
|
#[repr(packed)]
|
||||||
pub struct ParamBBD(pub u8, pub u8, pub u64);
|
pub struct ParamBBD(pub u8, pub u8, pub u64);
|
||||||
|
|
||||||
|
@ -99,6 +97,7 @@ pub struct ParamBD(pub u8, pub u64);
|
||||||
pub unsafe trait OpParam {}
|
pub unsafe trait OpParam {}
|
||||||
unsafe impl OpParam for ParamBBBB {}
|
unsafe impl OpParam for ParamBBBB {}
|
||||||
unsafe impl OpParam for ParamBBB {}
|
unsafe impl OpParam for ParamBBB {}
|
||||||
|
unsafe impl OpParam for ParamBBDB {}
|
||||||
unsafe impl OpParam for ParamBBDH {}
|
unsafe impl OpParam for ParamBBDH {}
|
||||||
unsafe impl OpParam for ParamBBD {}
|
unsafe impl OpParam for ParamBBD {}
|
||||||
unsafe impl OpParam for ParamBB {}
|
unsafe impl OpParam for ParamBB {}
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
#![doc = include_str!("../README.md")]
|
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
pub mod validate;
|
pub mod validate;
|
||||||
|
|
|
@ -18,7 +18,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
} else {
|
} else {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut vm = Vm::<_, 0>::new_unchecked(&prog, TestTrapHandler);
|
let mut vm = Vm::new_unchecked(&prog, TestTrapHandler);
|
||||||
vm.memory.insert_test_page();
|
vm.memory.insert_test_page();
|
||||||
println!("Program interrupt: {:?}", vm.run());
|
println!("Program interrupt: {:?}", vm.run());
|
||||||
println!("{:?}", vm.registers);
|
println!("{:?}", vm.registers);
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
//! Validate if program is sound to execute
|
|
||||||
|
|
||||||
/// Program validation error kind
|
/// Program validation error kind
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub enum ErrorKind {
|
pub enum ErrorKind {
|
||||||
|
@ -15,7 +13,7 @@ pub enum ErrorKind {
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub struct Error {
|
pub struct Error {
|
||||||
/// Kind
|
/// Kind
|
||||||
pub kind: ErrorKind,
|
pub kind: ErrorKind,
|
||||||
/// Location in bytecode
|
/// Location in bytecode
|
||||||
pub index: usize,
|
pub index: usize,
|
||||||
}
|
}
|
||||||
|
@ -34,7 +32,7 @@ pub fn validate(mut program: &[u8]) -> Result<(), Error> {
|
||||||
if usize::from(*reg) * 8 + usize::from(*count) > 2048 =>
|
if usize::from(*reg) * 8 + usize::from(*count) > 2048 =>
|
||||||
{
|
{
|
||||||
return Err(Error {
|
return Err(Error {
|
||||||
kind: ErrorKind::RegisterArrayOverflow,
|
kind: ErrorKind::RegisterArrayOverflow,
|
||||||
index: (program.as_ptr() as usize) - (start.as_ptr() as usize),
|
index: (program.as_ptr() as usize) - (start.as_ptr() as usize),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -42,20 +40,20 @@ pub fn validate(mut program: &[u8]) -> Result<(), Error> {
|
||||||
if src.checked_add(*count).is_none() || dst.checked_add(*count).is_none() =>
|
if src.checked_add(*count).is_none() || dst.checked_add(*count).is_none() =>
|
||||||
{
|
{
|
||||||
return Err(Error {
|
return Err(Error {
|
||||||
kind: ErrorKind::RegisterArrayOverflow,
|
kind: ErrorKind::RegisterArrayOverflow,
|
||||||
index: (program.as_ptr() as usize) - (start.as_ptr() as usize),
|
index: (program.as_ptr() as usize) - (start.as_ptr() as usize),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
[NOP | ECALL, rest @ ..]
|
[NOP | ECALL, rest @ ..]
|
||||||
| [DIR | DIRF, _, _, _, _, rest @ ..]
|
| [DIR | DIRF, _, _, _, _, rest @ ..]
|
||||||
| [ADD..=CMPU | BRC | ADDF..=MULF, _, _, _, rest @ ..]
|
| [ADD..=CMPU | BRC | ADDF..=MULF, _, _, _, rest @ ..]
|
||||||
| [NEG..=NOT | CP..=SWA | NEGF..=FTI, _, _, rest @ ..]
|
| [NEG..=NOT | CP..=SWA, _, _, rest @ ..]
|
||||||
| [LI | JMP, _, _, _, _, _, _, _, _, _, rest @ ..]
|
| [LI | JMP, _, _, _, _, _, _, _, _, _, rest @ ..]
|
||||||
| [ADDI..=CMPUI | BMC | JEQ..=JGTU | ADDFI..=MULFI, _, _, _, _, _, _, _, _, _, _, rest @ ..]
|
| [ADDI..=CMPUI | BMC | JEQ..=JGTU | ADDFI..=MULFI, _, _, _, _, _, _, _, _, _, _, rest @ ..]
|
||||||
| [LD..=ST, _, _, _, _, _, _, _, _, _, _, _, _, rest @ ..] => rest,
|
| [LD..=ST, _, _, _, _, _, _, _, _, _, _, _, _, rest @ ..] => rest,
|
||||||
_ => {
|
_ => {
|
||||||
return Err(Error {
|
return Err(Error {
|
||||||
kind: ErrorKind::InvalidInstruction,
|
kind: ErrorKind::InvalidInstruction,
|
||||||
index: (program.as_ptr() as usize) - (start.as_ptr() as usize),
|
index: (program.as_ptr() as usize) - (start.as_ptr() as usize),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
//! Program memory implementation
|
mod paging;
|
||||||
|
|
||||||
pub mod paging;
|
use core::mem::MaybeUninit;
|
||||||
|
|
||||||
use {
|
use self::paging::{PageTable, Permission, PtEntry};
|
||||||
self::paging::{PageTable, Permission, PtEntry},
|
use super::{trap::HandleTrap, VmRunError};
|
||||||
super::{trap::HandleTrap, VmRunError},
|
use alloc::boxed::Box;
|
||||||
alloc::boxed::Box,
|
use derive_more::Display;
|
||||||
core::mem::MaybeUninit,
|
|
||||||
derive_more::Display,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// HoleyBytes virtual memory
|
/// HoleyBytes virtual memory
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
@ -85,7 +82,7 @@ impl Memory {
|
||||||
|src, dst, count| core::ptr::copy_nonoverlapping(src, dst, count),
|
|src, dst, count| core::ptr::copy_nonoverlapping(src, dst, count),
|
||||||
traph,
|
traph,
|
||||||
)
|
)
|
||||||
.map_err(LoadError)
|
.map_err(|_| LoadError)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Store value to an address
|
/// Store value to an address
|
||||||
|
@ -108,7 +105,7 @@ impl Memory {
|
||||||
|dst, src, count| core::ptr::copy_nonoverlapping(src, dst, count),
|
|dst, src, count| core::ptr::copy_nonoverlapping(src, dst, count),
|
||||||
traph,
|
traph,
|
||||||
)
|
)
|
||||||
.map_err(StoreError)
|
.map_err(|_| StoreError)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Copy a block of memory
|
/// Copy a block of memory
|
||||||
|
@ -123,7 +120,7 @@ impl Memory {
|
||||||
dst: u64,
|
dst: u64,
|
||||||
count: usize,
|
count: usize,
|
||||||
traph: &mut impl HandleTrap,
|
traph: &mut impl HandleTrap,
|
||||||
) -> Result<(), BlkCopyError> {
|
) -> Result<(), MemoryAccessReason> {
|
||||||
// Yea, i know it is possible to do this more efficiently, but I am too lazy.
|
// Yea, i know it is possible to do this more efficiently, but I am too lazy.
|
||||||
|
|
||||||
const STACK_BUFFER_SIZE: usize = 512;
|
const STACK_BUFFER_SIZE: usize = 512;
|
||||||
|
@ -162,10 +159,7 @@ impl Memory {
|
||||||
|src, dst, count| core::ptr::copy(src, dst, count),
|
|src, dst, count| core::ptr::copy(src, dst, count),
|
||||||
traph,
|
traph,
|
||||||
)
|
)
|
||||||
.map_err(|addr| BlkCopyError {
|
.map_err(|_| MemoryAccessReason::Load)?;
|
||||||
access_reason: MemoryAccessReason::Load,
|
|
||||||
addr,
|
|
||||||
})?;
|
|
||||||
|
|
||||||
// Store from buffer
|
// Store from buffer
|
||||||
self.memory_access(
|
self.memory_access(
|
||||||
|
@ -177,12 +171,9 @@ impl Memory {
|
||||||
|dst, src, count| core::ptr::copy(src, dst, count),
|
|dst, src, count| core::ptr::copy(src, dst, count),
|
||||||
traph,
|
traph,
|
||||||
)
|
)
|
||||||
.map_err(|addr| BlkCopyError {
|
.map_err(|_| MemoryAccessReason::Store)?;
|
||||||
access_reason: MemoryAccessReason::Store,
|
|
||||||
addr,
|
|
||||||
})?;
|
|
||||||
|
|
||||||
Ok::<_, BlkCopyError>(())
|
Ok::<_, MemoryAccessReason>(())
|
||||||
})();
|
})();
|
||||||
|
|
||||||
// Deallocate if used heap-allocated array
|
// Deallocate if used heap-allocated array
|
||||||
|
@ -210,19 +201,14 @@ impl Memory {
|
||||||
permission_check: fn(Permission) -> bool,
|
permission_check: fn(Permission) -> bool,
|
||||||
action: fn(*mut u8, *mut u8, usize),
|
action: fn(*mut u8, *mut u8, usize),
|
||||||
traph: &mut impl HandleTrap,
|
traph: &mut impl HandleTrap,
|
||||||
) -> Result<(), u64> {
|
) -> Result<(), ()> {
|
||||||
let mut pspl = AddrSplitter::new(src, len, self.root_pt);
|
let mut pspl = AddrSplitter::new(src, len, self.root_pt);
|
||||||
loop {
|
loop {
|
||||||
match pspl.next() {
|
match pspl.next() {
|
||||||
// Page found
|
// Page found
|
||||||
Some(Ok(AddrSplitOk {
|
Some(Ok(AddrSplitOk { ptr, size, perm })) => {
|
||||||
vaddr,
|
|
||||||
ptr,
|
|
||||||
size,
|
|
||||||
perm,
|
|
||||||
})) => {
|
|
||||||
if !permission_check(perm) {
|
if !permission_check(perm) {
|
||||||
return Err(vaddr);
|
return Err(());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform memory action and bump dst pointer
|
// Perform memory action and bump dst pointer
|
||||||
|
@ -238,7 +224,7 @@ impl Memory {
|
||||||
// Bump dst pointer
|
// Bump dst pointer
|
||||||
dst = unsafe { dst.add(size as _) };
|
dst = unsafe { dst.add(size as _) };
|
||||||
} else {
|
} else {
|
||||||
return Err(addr); // Unhandleable
|
return Err(()); // Unhandleable
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => return Ok(()),
|
None => return Ok(()),
|
||||||
|
@ -249,9 +235,6 @@ impl Memory {
|
||||||
|
|
||||||
/// Result from address split
|
/// Result from address split
|
||||||
struct AddrSplitOk {
|
struct AddrSplitOk {
|
||||||
/// Virtual address
|
|
||||||
vaddr: u64,
|
|
||||||
|
|
||||||
/// Pointer to the start for perform operation
|
/// Pointer to the start for perform operation
|
||||||
ptr: *mut u8,
|
ptr: *mut u8,
|
||||||
|
|
||||||
|
@ -355,7 +338,6 @@ impl Iterator for AddrSplitter {
|
||||||
self.bump(size);
|
self.bump(size);
|
||||||
|
|
||||||
Some(Ok(AddrSplitOk {
|
Some(Ok(AddrSplitOk {
|
||||||
vaddr: self.addr,
|
|
||||||
ptr: unsafe { base.add(offset) }, // Return pointer to the start of region
|
ptr: unsafe { base.add(offset) }, // Return pointer to the start of region
|
||||||
size: avail,
|
size: avail,
|
||||||
perm,
|
perm,
|
||||||
|
@ -390,11 +372,11 @@ impl PageSize {
|
||||||
|
|
||||||
/// Unhandled load access trap
|
/// Unhandled load access trap
|
||||||
#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)]
|
||||||
pub struct LoadError(u64);
|
pub struct LoadError;
|
||||||
|
|
||||||
/// Unhandled store access trap
|
/// Unhandled store access trap
|
||||||
#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)]
|
||||||
pub struct StoreError(u64);
|
pub struct StoreError;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)]
|
||||||
pub enum MemoryAccessReason {
|
pub enum MemoryAccessReason {
|
||||||
|
@ -402,29 +384,23 @@ pub enum MemoryAccessReason {
|
||||||
Store,
|
Store,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
impl From<MemoryAccessReason> for VmRunError {
|
||||||
pub struct BlkCopyError {
|
fn from(value: MemoryAccessReason) -> Self {
|
||||||
access_reason: MemoryAccessReason,
|
match value {
|
||||||
addr: u64,
|
MemoryAccessReason::Load => Self::LoadAccessEx,
|
||||||
}
|
MemoryAccessReason::Store => Self::StoreAccessEx,
|
||||||
|
|
||||||
impl From<BlkCopyError> for VmRunError {
|
|
||||||
fn from(value: BlkCopyError) -> Self {
|
|
||||||
match value.access_reason {
|
|
||||||
MemoryAccessReason::Load => Self::LoadAccessEx(value.addr),
|
|
||||||
MemoryAccessReason::Store => Self::StoreAccessEx(value.addr),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<LoadError> for VmRunError {
|
impl From<LoadError> for VmRunError {
|
||||||
fn from(value: LoadError) -> Self {
|
fn from(_: LoadError) -> Self {
|
||||||
Self::LoadAccessEx(value.0)
|
Self::LoadAccessEx
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<StoreError> for VmRunError {
|
impl From<StoreError> for VmRunError {
|
||||||
fn from(value: StoreError) -> Self {
|
fn from(_: StoreError) -> Self {
|
||||||
Self::StoreAccessEx(value.0)
|
Self::StoreAccessEx
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,10 @@
|
||||||
//! Page table and associated structures implementation
|
use core::{
|
||||||
|
fmt::Debug,
|
||||||
use {
|
mem::MaybeUninit,
|
||||||
core::{
|
ops::{Index, IndexMut},
|
||||||
fmt::Debug,
|
slice::SliceIndex,
|
||||||
mem::MaybeUninit,
|
|
||||||
ops::{Index, IndexMut},
|
|
||||||
slice::SliceIndex,
|
|
||||||
},
|
|
||||||
delegate::delegate,
|
|
||||||
};
|
};
|
||||||
|
use delegate::delegate;
|
||||||
|
|
||||||
/// Page entry permission
|
/// Page entry permission
|
||||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||||
|
@ -32,10 +28,6 @@ pub enum Permission {
|
||||||
pub struct PtEntry(u64);
|
pub struct PtEntry(u64);
|
||||||
impl PtEntry {
|
impl PtEntry {
|
||||||
/// Create new
|
/// Create new
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
/// - `ptr` has to point to valid data and shall not be deallocated
|
|
||||||
/// troughout the entry lifetime
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn new(ptr: *mut PtPointedData, permission: Permission) -> Self {
|
pub unsafe fn new(ptr: *mut PtPointedData, permission: Permission) -> Self {
|
||||||
Self(ptr as u64 | permission as u64)
|
Self(ptr as u64 | permission as u64)
|
||||||
|
@ -63,6 +55,7 @@ impl Debug for PtEntry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Page table
|
/// Page table
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
#[repr(align(4096))]
|
#[repr(align(4096))]
|
||||||
|
@ -70,43 +63,15 @@ pub struct PageTable([PtEntry; 512]);
|
||||||
|
|
||||||
impl PageTable {
|
impl PageTable {
|
||||||
delegate!(to self.0 {
|
delegate!(to self.0 {
|
||||||
/// Returns a reference to an element or subslice depending on the type of
|
pub unsafe fn get<I>(&self, ix: I) -> Option<&I::Output>
|
||||||
/// index.
|
|
||||||
///
|
|
||||||
/// - If given a position, returns a reference to the element at that
|
|
||||||
/// position or `None` if out of bounds.
|
|
||||||
/// - If given a range, returns the subslice corresponding to that range,
|
|
||||||
/// or `None` if out of bounds.
|
|
||||||
///
|
|
||||||
pub fn get<I>(&self, ix: I) -> Option<&I::Output>
|
|
||||||
where I: SliceIndex<[PtEntry]>;
|
where I: SliceIndex<[PtEntry]>;
|
||||||
|
|
||||||
/// Returns a mutable reference to an element or subslice depending on the
|
pub unsafe fn get_mut<I>(&mut self, ix: I) -> Option<&mut I::Output>
|
||||||
/// type of index (see [`get`]) or `None` if the index is out of bounds.
|
|
||||||
pub fn get_mut<I>(&mut self, ix: I) -> Option<&mut I::Output>
|
|
||||||
where I: SliceIndex<[PtEntry]>;
|
where I: SliceIndex<[PtEntry]>;
|
||||||
|
|
||||||
/// Returns a reference to an element or subslice, without doing bounds
|
|
||||||
/// checking.
|
|
||||||
///
|
|
||||||
/// For a safe alternative see [`get`].
|
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// Calling this method with an out-of-bounds index is *[undefined behavior]*
|
|
||||||
/// even if the resulting reference is not used.
|
|
||||||
pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
|
pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
|
||||||
where I: SliceIndex<[PtEntry]>;
|
where I: SliceIndex<[PtEntry]>;
|
||||||
|
|
||||||
/// Returns a mutable reference to an element or subslice, without doing
|
|
||||||
/// bounds checking.
|
|
||||||
///
|
|
||||||
/// For a safe alternative see [`get_mut`].
|
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// Calling this method with an out-of-bounds index is *[undefined behavior]*
|
|
||||||
/// even if the resulting reference is not used.
|
|
||||||
pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
|
pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
|
||||||
where I: SliceIndex<[PtEntry]>;
|
where I: SliceIndex<[PtEntry]>;
|
||||||
});
|
});
|
||||||
|
@ -146,7 +111,7 @@ impl Default for PageTable {
|
||||||
#[repr(C, align(4096))]
|
#[repr(C, align(4096))]
|
||||||
pub union PtPointedData {
|
pub union PtPointedData {
|
||||||
/// Node - next page table
|
/// Node - next page table
|
||||||
pub pt: PageTable,
|
pub pt: PageTable,
|
||||||
/// Leaf
|
/// Leaf
|
||||||
pub page: u8,
|
pub page: u8,
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,8 @@ macro_rules! binary_op {
|
||||||
$handler(
|
$handler(
|
||||||
Value::$ty(&$self.read_reg(a0)),
|
Value::$ty(&$self.read_reg(a0)),
|
||||||
Value::$ty(&$self.read_reg(a1)),
|
Value::$ty(&$self.read_reg(a1)),
|
||||||
),
|
)
|
||||||
|
.into(),
|
||||||
);
|
);
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
@ -61,7 +62,7 @@ macro_rules! binary_op_imm {
|
||||||
let ParamBBD(tg, a0, imm) = param!($self, ParamBBD);
|
let ParamBBD(tg, a0, imm) = param!($self, ParamBBD);
|
||||||
$self.write_reg(
|
$self.write_reg(
|
||||||
tg,
|
tg,
|
||||||
$handler(Value::$ty(&$self.read_reg(a0)), Value::$ty(&imm.into())),
|
$handler(Value::$ty(&$self.read_reg(a0)), Value::$ty(&imm.into())).into(),
|
||||||
);
|
);
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
@ -79,7 +80,7 @@ macro_rules! cond_jump {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// HoleyBytes Virtual Machine
|
/// HoleyBytes Virtual Machine
|
||||||
pub struct Vm<'a, T, const TIMER_QUOTIENT: usize> {
|
pub struct Vm<'a, T> {
|
||||||
/// Holds 256 registers
|
/// Holds 256 registers
|
||||||
///
|
///
|
||||||
/// Writing to register 0 is considered undefined behaviour
|
/// Writing to register 0 is considered undefined behaviour
|
||||||
|
@ -97,12 +98,9 @@ pub struct Vm<'a, T, const TIMER_QUOTIENT: usize> {
|
||||||
|
|
||||||
/// Program
|
/// Program
|
||||||
program: &'a [u8],
|
program: &'a [u8],
|
||||||
|
|
||||||
/// Program timer
|
|
||||||
timer: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
impl<'a, T: HandleTrap> Vm<'a, T> {
|
||||||
/// Create a new VM with program and trap handler
|
/// Create a new VM with program and trap handler
|
||||||
///
|
///
|
||||||
/// # Safety
|
/// # Safety
|
||||||
|
@ -114,7 +112,6 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
||||||
traph,
|
traph,
|
||||||
pc: 0,
|
pc: 0,
|
||||||
program,
|
program,
|
||||||
timer: 0,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,12 +124,12 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
||||||
/// Execute program
|
/// Execute program
|
||||||
///
|
///
|
||||||
/// Program can return [`VmRunError`] if a trap handling failed
|
/// Program can return [`VmRunError`] if a trap handling failed
|
||||||
pub fn run(&mut self) -> Result<VmRunOk, VmRunError> {
|
pub fn run(&mut self) -> Result<(), VmRunError> {
|
||||||
use hbbytecode::opcode::*;
|
use hbbytecode::opcode::*;
|
||||||
loop {
|
loop {
|
||||||
// Fetch instruction
|
// Fetch instruction
|
||||||
let Some(&opcode) = self.program.get(self.pc)
|
let Some(&opcode) = self.program.get(self.pc)
|
||||||
else { return Ok(VmRunOk::End) };
|
else { return Ok(()) };
|
||||||
|
|
||||||
// Big match
|
// Big match
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -151,19 +148,21 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
||||||
let ParamBBB(tg, a0, a1) = param!(self, ParamBBB);
|
let ParamBBB(tg, a0, a1) = param!(self, ParamBBB);
|
||||||
self.write_reg(
|
self.write_reg(
|
||||||
tg,
|
tg,
|
||||||
self.read_reg(a0).as_i64().cmp(&self.read_reg(a1).as_i64()) as i64,
|
(self.read_reg(a0).as_i64().cmp(&self.read_reg(a1).as_i64()) as i64)
|
||||||
|
.into(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
CMPU => {
|
CMPU => {
|
||||||
let ParamBBB(tg, a0, a1) = param!(self, ParamBBB);
|
let ParamBBB(tg, a0, a1) = param!(self, ParamBBB);
|
||||||
self.write_reg(
|
self.write_reg(
|
||||||
tg,
|
tg,
|
||||||
self.read_reg(a0).as_u64().cmp(&self.read_reg(a1).as_u64()) as i64,
|
(self.read_reg(a0).as_u64().cmp(&self.read_reg(a1).as_u64()) as i64)
|
||||||
|
.into(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
NOT => {
|
NOT => {
|
||||||
let param = param!(self, ParamBB);
|
let param = param!(self, ParamBB);
|
||||||
self.write_reg(param.0, !self.read_reg(param.1).as_u64());
|
self.write_reg(param.0, (!self.read_reg(param.1).as_u64()).into());
|
||||||
}
|
}
|
||||||
NEG => {
|
NEG => {
|
||||||
let param = param!(self, ParamBB);
|
let param = param!(self, ParamBB);
|
||||||
|
@ -172,15 +171,16 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
||||||
match self.read_reg(param.1).as_u64() {
|
match self.read_reg(param.1).as_u64() {
|
||||||
0 => 1_u64,
|
0 => 1_u64,
|
||||||
_ => 0,
|
_ => 0,
|
||||||
},
|
}
|
||||||
|
.into(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
DIR => {
|
DIR => {
|
||||||
let ParamBBBB(dt, rt, a0, a1) = param!(self, ParamBBBB);
|
let ParamBBBB(dt, rt, a0, a1) = param!(self, ParamBBBB);
|
||||||
let a0 = self.read_reg(a0).as_u64();
|
let a0 = self.read_reg(a0).as_u64();
|
||||||
let a1 = self.read_reg(a1).as_u64();
|
let a1 = self.read_reg(a1).as_u64();
|
||||||
self.write_reg(dt, a0.checked_div(a1).unwrap_or(u64::MAX));
|
self.write_reg(dt, (a0.checked_div(a1).unwrap_or(u64::MAX)).into());
|
||||||
self.write_reg(rt, a0.checked_rem(a1).unwrap_or(u64::MAX));
|
self.write_reg(rt, (a0.checked_rem(a1).unwrap_or(u64::MAX)).into());
|
||||||
}
|
}
|
||||||
ADDI => binary_op_imm!(self, as_u64, ops::Add::add),
|
ADDI => binary_op_imm!(self, as_u64, ops::Add::add),
|
||||||
MULI => binary_op_imm!(self, as_u64, ops::Mul::mul),
|
MULI => binary_op_imm!(self, as_u64, ops::Mul::mul),
|
||||||
|
@ -194,12 +194,13 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
||||||
let ParamBBD(tg, a0, imm) = param!(self, ParamBBD);
|
let ParamBBD(tg, a0, imm) = param!(self, ParamBBD);
|
||||||
self.write_reg(
|
self.write_reg(
|
||||||
tg,
|
tg,
|
||||||
self.read_reg(a0).as_i64().cmp(&Value::from(imm).as_i64()) as i64,
|
(self.read_reg(a0).as_i64().cmp(&Value::from(imm).as_i64()) as i64)
|
||||||
|
.into(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
CMPUI => {
|
CMPUI => {
|
||||||
let ParamBBD(tg, a0, imm) = param!(self, ParamBBD);
|
let ParamBBD(tg, a0, imm) = param!(self, ParamBBD);
|
||||||
self.write_reg(tg, self.read_reg(a0).as_u64().cmp(&imm) as i64);
|
self.write_reg(tg, (self.read_reg(a0).as_u64().cmp(&imm) as i64).into());
|
||||||
}
|
}
|
||||||
CP => {
|
CP => {
|
||||||
let param = param!(self, ParamBB);
|
let param = param!(self, ParamBB);
|
||||||
|
@ -216,7 +217,7 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
||||||
}
|
}
|
||||||
LI => {
|
LI => {
|
||||||
let param = param!(self, ParamBD);
|
let param = param!(self, ParamBD);
|
||||||
self.write_reg(param.0, param.1);
|
self.write_reg(param.0, param.1.into());
|
||||||
}
|
}
|
||||||
LD => {
|
LD => {
|
||||||
let ParamBBDH(dst, base, off, count) = param!(self, ParamBBDH);
|
let ParamBBDH(dst, base, off, count) = param!(self, ParamBBDH);
|
||||||
|
@ -279,34 +280,13 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
||||||
.ecall(&mut self.registers, &mut self.pc, &mut self.memory);
|
.ecall(&mut self.registers, &mut self.pc, &mut self.memory);
|
||||||
}
|
}
|
||||||
ADDF => binary_op!(self, as_f64, ops::Add::add),
|
ADDF => binary_op!(self, as_f64, ops::Add::add),
|
||||||
SUBF => binary_op!(self, as_f64, ops::Sub::sub),
|
|
||||||
MULF => binary_op!(self, as_f64, ops::Mul::mul),
|
MULF => binary_op!(self, as_f64, ops::Mul::mul),
|
||||||
DIRF => {
|
DIRF => {
|
||||||
let ParamBBBB(dt, rt, a0, a1) = param!(self, ParamBBBB);
|
let ParamBBBB(dt, rt, a0, a1) = param!(self, ParamBBBB);
|
||||||
let a0 = self.read_reg(a0).as_f64();
|
let a0 = self.read_reg(a0).as_f64();
|
||||||
let a1 = self.read_reg(a1).as_f64();
|
let a1 = self.read_reg(a1).as_f64();
|
||||||
self.write_reg(dt, a0 / a1);
|
self.write_reg(dt, (a0 / a1).into());
|
||||||
self.write_reg(rt, a0 % a1);
|
self.write_reg(rt, (a0 % a1).into());
|
||||||
}
|
|
||||||
FMAF => {
|
|
||||||
let ParamBBBB(dt, a0, a1, a2) = param!(self, ParamBBBB);
|
|
||||||
self.write_reg(
|
|
||||||
dt,
|
|
||||||
self.read_reg(a0).as_f64() * self.read_reg(a1).as_f64()
|
|
||||||
+ self.read_reg(a2).as_f64(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
NEGF => {
|
|
||||||
let ParamBB(dt, a0) = param!(self, ParamBB);
|
|
||||||
self.write_reg(dt, -self.read_reg(a0).as_f64());
|
|
||||||
}
|
|
||||||
ITF => {
|
|
||||||
let ParamBB(dt, a0) = param!(self, ParamBB);
|
|
||||||
self.write_reg(dt, self.read_reg(a0).as_i64() as f64);
|
|
||||||
}
|
|
||||||
FTI => {
|
|
||||||
let ParamBB(dt, a0) = param!(self, ParamBB);
|
|
||||||
self.write_reg(dt, self.read_reg(a0).as_f64() as i64);
|
|
||||||
}
|
}
|
||||||
ADDFI => binary_op_imm!(self, as_f64, ops::Add::add),
|
ADDFI => binary_op_imm!(self, as_f64, ops::Add::add),
|
||||||
MULFI => binary_op_imm!(self, as_f64, ops::Mul::mul),
|
MULFI => binary_op_imm!(self, as_f64, ops::Mul::mul),
|
||||||
|
@ -317,18 +297,11 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
||||||
&mut self.memory,
|
&mut self.memory,
|
||||||
op,
|
op,
|
||||||
) {
|
) {
|
||||||
return Err(VmRunError::InvalidOpcodeEx(op));
|
return Err(VmRunError::InvalidOpcodeEx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if TIMER_QUOTIENT != 0 {
|
|
||||||
self.timer = self.timer.wrapping_add(1);
|
|
||||||
if self.timer % TIMER_QUOTIENT == 0 {
|
|
||||||
return Ok(VmRunOk::Timer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -341,9 +314,9 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
||||||
/// Write a register.
|
/// Write a register.
|
||||||
/// Writing to register 0 is no-op.
|
/// Writing to register 0 is no-op.
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn write_reg(&mut self, n: u8, value: impl Into<Value>) {
|
unsafe fn write_reg(&mut self, n: u8, value: Value) {
|
||||||
if n != 0 {
|
if n != 0 {
|
||||||
*self.registers.get_unchecked_mut(n as usize) = value.into();
|
*self.registers.get_unchecked_mut(n as usize) = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -353,21 +326,11 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum VmRunError {
|
pub enum VmRunError {
|
||||||
/// Unhandled invalid opcode exceptions
|
/// Unhandled invalid opcode exceptions
|
||||||
InvalidOpcodeEx(u8),
|
InvalidOpcodeEx,
|
||||||
|
|
||||||
/// Unhandled load access exception
|
/// Unhandled load access exception
|
||||||
LoadAccessEx(u64),
|
LoadAccessEx,
|
||||||
|
|
||||||
/// Unhandled store access exception
|
/// Unhandled store access exception
|
||||||
StoreAccessEx(u64),
|
StoreAccessEx,
|
||||||
}
|
|
||||||
|
|
||||||
/// Virtual machine halt ok
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
||||||
pub enum VmRunOk {
|
|
||||||
/// Program has eached its end
|
|
||||||
End,
|
|
||||||
|
|
||||||
/// Program was interrupted by a timer
|
|
||||||
Timer,
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
//! Program trap handling interfaces
|
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
mem::{Memory, MemoryAccessReason, PageSize},
|
mem::{Memory, MemoryAccessReason, PageSize},
|
||||||
value::Value,
|
value::Value,
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
//! HoleyBytes register value definition
|
|
||||||
|
|
||||||
use core::fmt::Debug;
|
use core::fmt::Debug;
|
||||||
|
|
||||||
/// Define [`Value`] union
|
/// Define [`Value`] union
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
nightly
|
stable
|
|
@ -1,4 +1,3 @@
|
||||||
hex_literal_case = "Upper"
|
hex_literal_case = "Upper"
|
||||||
imports_granularity = "One"
|
imports_granularity = "One"
|
||||||
struct_field_align_threshold = 5
|
struct_field_align_threshold = 5
|
||||||
enum_discrim_align_threshold = 5
|
|
37
spec.md
37
spec.md
|
@ -19,6 +19,7 @@
|
||||||
| BBBB | 32 bits |
|
| BBBB | 32 bits |
|
||||||
| BBB | 24 bits |
|
| BBB | 24 bits |
|
||||||
| BBDH | 96 bits |
|
| BBDH | 96 bits |
|
||||||
|
| BBDB | 88 bits |
|
||||||
| BBD | 80 bits |
|
| BBD | 80 bits |
|
||||||
| BB | 16 bits |
|
| BB | 16 bits |
|
||||||
| BD | 72 bits |
|
| BD | 72 bits |
|
||||||
|
@ -199,38 +200,14 @@
|
||||||
| Opcode | Name | Action |
|
| Opcode | Name | Action |
|
||||||
|:------:|:----:|:--------------:|
|
|:------:|:----:|:--------------:|
|
||||||
| 40 | ADDF | Addition |
|
| 40 | ADDF | Addition |
|
||||||
| 41 | SUBF | Subtraction |
|
| 41 | MULF | Multiplication |
|
||||||
| 42 | MULF | Multiplication |
|
|
||||||
|
|
||||||
### Division-remainder
|
### Division-remainder
|
||||||
- Type BBBB
|
- Type BBBB
|
||||||
|
|
||||||
| Opcode | Name | Action |
|
| Opcode | Name | Action |
|
||||||
|:------:|:----:|:-------------------------:|
|
|:------:|:----:|:--------------------------------------:|
|
||||||
| 43 | DIRF | Same as for integer `DIR` |
|
| 42 | DIRF | Same flow applies as for integer `DIR` |
|
||||||
|
|
||||||
### Fused Multiply-Add
|
|
||||||
- Type BBBB
|
|
||||||
|
|
||||||
| Opcode | Name | Action |
|
|
||||||
|:------:|:----:|:---------------------:|
|
|
||||||
| 44 | FMAF | `#0 ← (#1 * #2) + #3` |
|
|
||||||
|
|
||||||
### Negation
|
|
||||||
- Type BB
|
|
||||||
| Opcode | Name | Action |
|
|
||||||
|:------:|:----:|:----------:|
|
|
||||||
| 45 | NEGF | `#0 ← -#1` |
|
|
||||||
|
|
||||||
### Conversion
|
|
||||||
- Type BB
|
|
||||||
- Signed
|
|
||||||
- `#0 ← #1 as _`
|
|
||||||
|
|
||||||
| Opcode | Name | Action |
|
|
||||||
|:------:|:----:|:------------:|
|
|
||||||
| 46 | ITF | Int to Float |
|
|
||||||
| 47 | FTI | Float to Int |
|
|
||||||
|
|
||||||
## Floating point immediate operations
|
## Floating point immediate operations
|
||||||
- Type BBD
|
- Type BBD
|
||||||
|
@ -238,8 +215,8 @@
|
||||||
|
|
||||||
| Opcode | Name | Action |
|
| Opcode | Name | Action |
|
||||||
|:------:|:-----:|:--------------:|
|
|:------:|:-----:|:--------------:|
|
||||||
| 48 | ADDFI | Addition |
|
| 43 | ADDFI | Addition |
|
||||||
| 49 | MULFI | Multiplication |
|
| 44 | MULFI | Multiplication |
|
||||||
|
|
||||||
# Registers
|
# Registers
|
||||||
- There is 255 registers + one zero register (with index 0)
|
- There is 255 registers + one zero register (with index 0)
|
||||||
|
|
Loading…
Reference in a new issue