forked from AbleOS/holey-bytes
Compare commits
42 commits
master
...
wip/its-no
Author | SHA1 | Date | |
---|---|---|---|
Erin | bf78cc751a | ||
Erin | c95deefcb2 | ||
Erin | 2c20c7c859 | ||
Erin | f85e3eb062 | ||
Erin | 7f2676af91 | ||
Erin | 4dd2052634 | ||
Erin | 8afb251950 | ||
Erin | e4f84aadc0 | ||
Erin | bdb596befb | ||
Erin | 9ee9d6e266 | ||
Erin | c7b5512ada | ||
Erin | 059e2b4b66 | ||
Erin | b7ff456808 | ||
Erin | 1ec4a7a653 | ||
Erin | ed3bcba42e | ||
Erin | 86df8ddc64 | ||
Erin | 1263941560 | ||
Erin | efdcd826ee | ||
Erin | c873945681 | ||
Erin | 2e6e6b7939 | ||
Erin | 2144c055d1 | ||
Erin | f832f6a04a | ||
Erin | 36f4d31fb2 | ||
Erin | e1499fd5a1 | ||
Erin | d32b9e7fba | ||
Erin | 8ba86db561 | ||
Erin | 642488cb64 | ||
Erin | 82160af7af | ||
Erin | 2a08362b52 | ||
Erin | 48353db26f | ||
Erin | 417047806b | ||
Erin | da6ad6d2c7 | ||
Erin | a34f2fc9f8 | ||
Erin | fce7a96e50 | ||
Erin | 859e14daa6 | ||
Erin | 0fd3aee6b5 | ||
Erin | cbb0ac2abe | ||
Erin | 8675965ef5 | ||
Erin | 8d6e7af9d8 | ||
Erin | bb3a472eeb | ||
Erin | e67d512f89 | ||
Erin | a4b22e2053 |
80
Cargo.lock
generated
80
Cargo.lock
generated
|
@ -13,22 +13,6 @@ dependencies = [
|
|||
"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]]
|
||||
name = "beef"
|
||||
version = "0.5.2"
|
||||
|
@ -41,12 +25,6 @@ version = "1.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "convert_case"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
|
||||
|
||||
[[package]]
|
||||
name = "delegate"
|
||||
version = "0.9.0"
|
||||
|
@ -58,19 +36,6 @@ dependencies = [
|
|||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "derive_more"
|
||||
version = "0.99.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321"
|
||||
dependencies = [
|
||||
"convert_case",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"rustc_version",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fnv"
|
||||
version = "1.0.7"
|
||||
|
@ -86,22 +51,10 @@ dependencies = [
|
|||
"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]]
|
||||
name = "hbasm"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ariadne",
|
||||
"hashbrown 0.14.0",
|
||||
"hbbytecode",
|
||||
"lasso",
|
||||
"logos",
|
||||
|
@ -117,8 +70,7 @@ name = "hbvm"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"delegate",
|
||||
"derive_more",
|
||||
"hashbrown 0.13.2",
|
||||
"hashbrown",
|
||||
"hbbytecode",
|
||||
"log",
|
||||
"paste",
|
||||
|
@ -131,8 +83,7 @@ version = "0.7.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4644821e1c3d7a560fe13d842d13f587c07348a1a05d3a797152d41c90c56df2"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"hashbrown 0.13.2",
|
||||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -212,21 +163,6 @@ version = "0.6.29"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
|
||||
|
||||
[[package]]
|
||||
name = "rustc_version"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
|
||||
dependencies = [
|
||||
"semver",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "1.0.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed"
|
||||
|
||||
[[package]]
|
||||
name = "static_assertions"
|
||||
version = "1.1.0"
|
||||
|
@ -261,20 +197,8 @@ version = "1.0.9"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||
|
||||
[[package]]
|
||||
name = "yansi"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"
|
||||
|
|
4
assets/example.asm
Normal file
4
assets/example.asm
Normal file
|
@ -0,0 +1,4 @@
|
|||
load 0 a0 ;; 05 00 A0
|
||||
load 10 a1 ;; 05 10 A1
|
||||
add a0 1 a0 ;; 01 A0 01 A0
|
||||
jump_neq a0 a1 0 ;; a1 A0 A1 0
|
4
assets/example.bytes
Normal file
4
assets/example.bytes
Normal file
|
@ -0,0 +1,4 @@
|
|||
load 10 A1
|
||||
load 0 A0
|
||||
add A0 1
|
||||
jump_less_than A0 A1 0
|
|
@ -5,14 +5,8 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
hbbytecode = { path = "../hbbytecode" }
|
||||
lasso = "0.7"
|
||||
paste = "1.0"
|
||||
hashbrown = "0.14.0"
|
||||
ariadne = "0.3.0"
|
||||
|
||||
[dependencies.lasso]
|
||||
version = "0.7"
|
||||
default-features = false
|
||||
features = ["no-std"]
|
||||
|
||||
[dependencies.logos]
|
||||
version = "0.13"
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
jmp r0, start
|
||||
start:
|
||||
jmp r0, init_serial_port
|
||||
|
||||
-- Uses r20 to set the port
|
||||
init_serial_port:
|
||||
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)
|
306
hbasm/src/lib.rs
306
hbasm/src/lib.rs
|
@ -1,65 +1,269 @@
|
|||
#![no_std]
|
||||
#![feature(error_in_core)]
|
||||
use std::collections::HashMap;
|
||||
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)]
|
||||
pub struct Assembler {
|
||||
pub buf: Vec<u8>,
|
||||
sub: HashSet<usize>,
|
||||
}
|
||||
#[regex(
|
||||
r"\p{XID_Start}\p{XID_Continue}*",
|
||||
|lexer| lexer.extras.get_or_intern(lexer.slice()),
|
||||
)] Symbol(Spur),
|
||||
|
||||
impl Assembler {
|
||||
macros::impl_asm!(
|
||||
bbbb(p0: u8, p1: u8, p2: u8, p3: u8)
|
||||
=> [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());
|
||||
#[token("\n")]
|
||||
#[token(";")] ISep,
|
||||
#[token(",")] PSep,
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
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)]
|
||||
pub struct Symbol(pub u64);
|
||||
impl Imm for Symbol {
|
||||
#[inline(always)]
|
||||
fn insert(&self, asm: &mut Assembler) {
|
||||
asm.sub.insert(asm.buf.len());
|
||||
asm.buf.extend(self.0.to_le_bytes());
|
||||
#[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<'_>) -> 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 {
|
||||
ariadne::{ColorGenerator, Label, Report, ReportKind, Source},
|
||||
std::{
|
||||
use std::{
|
||||
error::Error,
|
||||
io::{stdin, Read},
|
||||
},
|
||||
io::{stdin, stdout, Read, Write},
|
||||
};
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
@ -11,36 +8,14 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
stdin().read_to_string(&mut code)?;
|
||||
|
||||
let mut buf = vec![];
|
||||
|
||||
if let Err(e) = hbasm::text::assembly(&code, &mut buf) {
|
||||
let mut colors = ColorGenerator::new();
|
||||
|
||||
let e_code = match e.kind {
|
||||
hbasm::text::ErrorKind::UnexpectedToken => 1,
|
||||
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();
|
||||
if let Err(e) = hbasm::assembly(&code, &mut buf) {
|
||||
eprintln!(
|
||||
"Error {:?} at {:?} (`{}`)",
|
||||
e.kind,
|
||||
e.span.clone(),
|
||||
&code[e.span],
|
||||
);
|
||||
}
|
||||
|
||||
stdout().write_all(&buf)?;
|
||||
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
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static_assert(CHAR_BIT == 8, "Cursed architectures are not supported");
|
||||
|
||||
enum hbbc_Opcode: uint8_t {
|
||||
hbbc_Op_NOP , hbbc_Op_ADD , hbbc_Op_SUB , hbbc_Op_MUL , hbbc_Op_AND , hbbc_Op_OR ,
|
||||
hbbc_Op_XOR , hbbc_Op_SL , hbbc_Op_SR , hbbc_Op_SRS , hbbc_Op_CMP , hbbc_Op_CMPU ,
|
||||
hbbc_Op_DIR , hbbc_Op_NEG , hbbc_Op_NOT , hbbc_Op_ADDI , hbbc_Op_MULI , hbbc_Op_ANDI ,
|
||||
hbbc_Op_ORI , hbbc_Op_XORI , hbbc_Op_SLI , hbbc_Op_SRI , hbbc_Op_SRSI , hbbc_Op_CMPI ,
|
||||
hbbc_Op_CMPUI , hbbc_Op_CP , hbbc_Op_SWA , hbbc_Op_LI , hbbc_Op_LD , hbbc_Op_ST ,
|
||||
hbbc_Op_BMC , hbbc_Op_BRC , hbbc_Op_JMP , hbbc_Op_JEQ , hbbc_Op_JNE , hbbc_Op_JLT ,
|
||||
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;
|
||||
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,
|
||||
hbbc_Op_SR, hbbc_Op_SRS, hbbc_Op_CMP, hbbc_Op_CMPU, hbbc_Op_DIR, hbbc_Op_NEG, hbbc_Op_NOT,
|
||||
hbbc_Op_ADDI, hbbc_Op_MULI, hbbc_Op_ANDI, hbbc_Op_ORI, hbbc_Op_XORI, hbbc_Op_SLI, hbbc_Op_SRI,
|
||||
hbbc_Op_SRSI, hbbc_Op_CMPI, hbbc_Op_CMPUI, hbbc_Op_CP, hbbc_Op_SWA, hbbc_Op_LI, hbbc_Op_LD,
|
||||
hbbc_Op_ST, hbbc_Op_BMC, hbbc_Op_BRC, hbbc_Op_JMP, hbbc_Op_JEQ, hbbc_Op_JNE, hbbc_Op_JLT,
|
||||
hbbc_Op_JGT, hbbc_Op_JLTU, hbbc_Op_JGTU, hbbc_Op_ECALL, hbbc_Op_ADDF, hbbc_Op_MULF,
|
||||
hbbc_Op_DIRF, hbbc_Op_ADDFI, hbbc_Op_MULFI,
|
||||
} hbbc_Opcode;
|
||||
|
||||
static_assert(sizeof(hbbc_Opcode) == 1);
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct hbbc_ParamBBBB
|
||||
typedef struct hbbc_ParamBBBB
|
||||
{ uint8_t _0; uint8_t _1; uint8_t _2; uint8_t _3; }
|
||||
typedef hbbc_ParamBBBB;
|
||||
static_assert(sizeof(hbbc_ParamBBBB) == 32 / 8);
|
||||
hbbc_ParamBBBB;
|
||||
static_assert(sizeof(hbbc_ParamBBBB) == 4);
|
||||
|
||||
struct hbbc_ParamBBB
|
||||
typedef struct hbbc_ParamBBB
|
||||
{ uint8_t _0; uint8_t _1; uint8_t _2; }
|
||||
typedef hbbc_ParamBBB;
|
||||
static_assert(sizeof(hbbc_ParamBBB) == 24 / 8);
|
||||
hbbc_ParamBBB;
|
||||
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; }
|
||||
typedef hbbc_ParamBBDH;
|
||||
static_assert(sizeof(hbbc_ParamBBDH) == 96 / 8);
|
||||
hbbc_ParamBBDH;
|
||||
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; }
|
||||
typedef hbbc_ParamBBD;
|
||||
static_assert(sizeof(hbbc_ParamBBD) == 80 / 8);
|
||||
hbbc_ParamBBD;
|
||||
static_assert(sizeof(hbbc_ParamBBD) == 10);
|
||||
|
||||
struct hbbc_ParamBB
|
||||
typedef struct hbbc_ParamBB
|
||||
{ uint8_t _0; uint8_t _1; }
|
||||
typedef hbbc_ParamBB;
|
||||
static_assert(sizeof(hbbc_ParamBB) == 16 / 8);
|
||||
hbbc_ParamBB;
|
||||
static_assert(sizeof(hbbc_ParamBB) == 2);
|
||||
|
||||
struct hbbc_ParamBD
|
||||
typedef struct hbbc_ParamBD
|
||||
{ uint8_t _0; uint64_t _1; }
|
||||
typedef hbbc_ParamBD;
|
||||
static_assert(sizeof(hbbc_ParamBD) == 72 / 8);
|
||||
hbbc_ParamBD;
|
||||
static_assert(sizeof(hbbc_ParamBD) == 9);
|
||||
|
||||
typedef uint64_t hbbc_ParamD;
|
||||
static_assert(sizeof(hbbc_ParamD) == 64 / 8);
|
||||
static_assert(sizeof(hbbc_ParamD) == 8);
|
||||
|
||||
#pragma pack(pop)
|
||||
|
|
|
@ -32,7 +32,7 @@ constmod!(pub opcode(u8) {
|
|||
CMP = 10, "BBB; #0 ← #1 <=> #2";
|
||||
CMPU = 11, "BBB; #0 ← #1 <=> #2 (unsigned)";
|
||||
DIR = 12, "BBBB; #0 ← #2 / #3, #1 ← #2 % #3";
|
||||
NEG = 13, "BB; #0 ← -#1";
|
||||
NEG = 13, "BB; #0 ← ~#1";
|
||||
NOT = 14, "BB; #0 ← !#1";
|
||||
|
||||
ADDI = 15, "BBD; #0 ← #1 + imm #2";
|
||||
|
@ -64,16 +64,11 @@ constmod!(pub opcode(u8) {
|
|||
ECALL = 39, "N; Issue system call";
|
||||
|
||||
ADDF = 40, "BBB; #0 ← #1 +. #2";
|
||||
SUBF = 41, "BBB; #0 ← #1 -. #2";
|
||||
MULF = 42, "BBB; #0 ← #1 +. #2";
|
||||
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";
|
||||
MULF = 41, "BBB; #0 ← #1 +. #2";
|
||||
DIRF = 42, "BBBB; #0 ← #2 / #3, #1 ← #2 % #3";
|
||||
|
||||
ADDFI = 48, "BBD; #0 ← #1 +. imm #2";
|
||||
MULFI = 49, "BBD; #0 ← #1 *. imm #2";
|
||||
ADDFI = 43, "BBD; #0 ← #1 +. imm #2";
|
||||
MULFI = 44, "BBD; #0 ← #1 *. imm #2";
|
||||
});
|
||||
|
||||
#[repr(packed)]
|
||||
|
@ -85,6 +80,9 @@ pub struct ParamBBB(pub u8, pub u8, pub u8);
|
|||
#[repr(packed)]
|
||||
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)]
|
||||
pub struct ParamBBD(pub u8, pub u8, pub u64);
|
||||
|
||||
|
@ -99,6 +97,7 @@ pub struct ParamBD(pub u8, pub u64);
|
|||
pub unsafe trait OpParam {}
|
||||
unsafe impl OpParam for ParamBBBB {}
|
||||
unsafe impl OpParam for ParamBBB {}
|
||||
unsafe impl OpParam for ParamBBDB {}
|
||||
unsafe impl OpParam for ParamBBDH {}
|
||||
unsafe impl OpParam for ParamBBD {}
|
||||
unsafe impl OpParam for ParamBB {}
|
||||
|
|
|
@ -8,7 +8,6 @@ lto = true
|
|||
|
||||
[dependencies]
|
||||
delegate = "0.9"
|
||||
derive_more = "0.99"
|
||||
hashbrown = "0.13"
|
||||
hbbytecode.path = "../hbbytecode"
|
||||
log = "0.4"
|
||||
|
|
|
@ -1,7 +1,22 @@
|
|||
#![doc = include_str!("../README.md")]
|
||||
#![no_std]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
pub mod validate;
|
||||
pub mod vm;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum RuntimeErrors {
|
||||
InvalidOpcodePair(u8, u8),
|
||||
RegisterTooSmall,
|
||||
HostError(u64),
|
||||
PageNotMapped(u64),
|
||||
InvalidJumpAddress(u64),
|
||||
InvalidSystemCall(u8),
|
||||
}
|
||||
|
||||
// If you solve the halting problem feel free to remove this
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum HaltStatus {
|
||||
Halted,
|
||||
Running,
|
||||
}
|
||||
|
|
|
@ -1,9 +1,3 @@
|
|||
use hbvm::vm::{
|
||||
mem::{Memory, MemoryAccessReason, PageSize},
|
||||
trap::HandleTrap,
|
||||
value::Value,
|
||||
};
|
||||
|
||||
use {
|
||||
hbvm::{validate::validate, vm::Vm},
|
||||
std::io::{stdin, Read},
|
||||
|
@ -18,7 +12,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
return Ok(());
|
||||
} else {
|
||||
unsafe {
|
||||
let mut vm = Vm::<_, 0>::new_unchecked(&prog, TestTrapHandler);
|
||||
let mut vm = Vm::new_unchecked(&prog);
|
||||
vm.memory.insert_test_page();
|
||||
println!("Program interrupt: {:?}", vm.run());
|
||||
println!("{:?}", vm.registers);
|
||||
|
@ -30,30 +24,3 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
pub fn time() -> u32 {
|
||||
9
|
||||
}
|
||||
|
||||
struct TestTrapHandler;
|
||||
impl HandleTrap for TestTrapHandler {
|
||||
fn page_fault(
|
||||
&mut self,
|
||||
_: MemoryAccessReason,
|
||||
_: &mut Memory,
|
||||
_: u64,
|
||||
_: PageSize,
|
||||
_: *mut u8,
|
||||
) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn invalid_op(&mut self, _: &mut [Value; 256], _: &mut usize, _: &mut Memory, _: u8) -> bool
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
false
|
||||
}
|
||||
|
||||
fn ecall(&mut self, _: &mut [Value; 256], _: &mut usize, _: &mut Memory)
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,33 +1,21 @@
|
|||
//! Validate if program is sound to execute
|
||||
|
||||
/// Program validation error kind
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ErrorKind {
|
||||
/// Unknown opcode
|
||||
InvalidInstruction,
|
||||
/// VM doesn't implement this valid opcode
|
||||
Unimplemented,
|
||||
/// Attempted to copy over register boundary
|
||||
RegisterArrayOverflow,
|
||||
}
|
||||
|
||||
/// Error
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Error {
|
||||
/// Kind
|
||||
pub kind: ErrorKind,
|
||||
/// Location in bytecode
|
||||
pub index: usize,
|
||||
}
|
||||
|
||||
/// Perform bytecode validation. If it passes, the program should be
|
||||
/// sound to execute.
|
||||
pub fn validate(mut program: &[u8]) -> Result<(), Error> {
|
||||
use hbbytecode::opcode::*;
|
||||
|
||||
let start = program;
|
||||
loop {
|
||||
// Match on instruction types and perform necessary checks
|
||||
program = match program {
|
||||
[] => return Ok(()),
|
||||
[LD..=ST, reg, _, _, _, _, _, _, _, _, _, count, ..]
|
||||
|
@ -49,7 +37,7 @@ pub fn validate(mut program: &[u8]) -> Result<(), Error> {
|
|||
[NOP | ECALL, rest @ ..]
|
||||
| [DIR | DIRF, _, _, _, _, rest @ ..]
|
||||
| [ADD..=CMPU | BRC | ADDF..=MULF, _, _, _, rest @ ..]
|
||||
| [NEG..=NOT | CP..=SWA | NEGF..=FTI, _, _, rest @ ..]
|
||||
| [NEG..=NOT | CP..=SWA, _, _, rest @ ..]
|
||||
| [LI | JMP, _, _, _, _, _, _, _, _, _, rest @ ..]
|
||||
| [ADDI..=CMPUI | BMC | JEQ..=JGTU | ADDFI..=MULFI, _, _, _, _, _, _, _, _, _, _, rest @ ..]
|
||||
| [LD..=ST, _, _, _, _, _, _, _, _, _, _, _, _, rest @ ..] => rest,
|
||||
|
|
|
@ -1,19 +1,10 @@
|
|||
//! Program memory implementation
|
||||
mod paging;
|
||||
|
||||
pub mod paging;
|
||||
use self::paging::{PageTable, Permission, PtEntry};
|
||||
use alloc::boxed::Box;
|
||||
|
||||
use {
|
||||
self::paging::{PageTable, Permission, PtEntry},
|
||||
super::{trap::HandleTrap, VmRunError},
|
||||
alloc::boxed::Box,
|
||||
core::mem::MaybeUninit,
|
||||
derive_more::Display,
|
||||
};
|
||||
|
||||
/// HoleyBytes virtual memory
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Memory {
|
||||
/// Root page table
|
||||
root_pt: *mut PageTable,
|
||||
}
|
||||
|
||||
|
@ -56,23 +47,13 @@ impl Memory {
|
|||
entry = PtEntry::new(Box::into_raw(pt) as _, Permission::Node);
|
||||
}
|
||||
|
||||
(*self.root_pt)[0] = entry;
|
||||
self.root_pt_mut()[0] = entry;
|
||||
}
|
||||
}
|
||||
|
||||
/// Load value from an address
|
||||
///
|
||||
/// # Safety
|
||||
/// Applies same conditions as for [`core::ptr::copy_nonoverlapping`]
|
||||
pub unsafe fn load(
|
||||
&mut self,
|
||||
addr: u64,
|
||||
target: *mut u8,
|
||||
count: usize,
|
||||
traph: &mut impl HandleTrap,
|
||||
) -> Result<(), LoadError> {
|
||||
pub unsafe fn load(&self, addr: u64, target: *mut u8, count: usize) -> Result<(), AccessFault> {
|
||||
self.memory_access(
|
||||
MemoryAccessReason::Load,
|
||||
addr,
|
||||
target,
|
||||
count,
|
||||
|
@ -83,207 +64,105 @@ impl Memory {
|
|||
)
|
||||
},
|
||||
|src, dst, count| core::ptr::copy_nonoverlapping(src, dst, count),
|
||||
traph,
|
||||
)
|
||||
.map_err(LoadError)
|
||||
}
|
||||
|
||||
/// Store value to an address
|
||||
///
|
||||
/// # Safety
|
||||
/// Applies same conditions as for [`core::ptr::copy_nonoverlapping`]
|
||||
pub unsafe fn store(
|
||||
&mut self,
|
||||
addr: u64,
|
||||
source: *const u8,
|
||||
count: usize,
|
||||
traph: &mut impl HandleTrap,
|
||||
) -> Result<(), StoreError> {
|
||||
) -> Result<(), AccessFault> {
|
||||
self.memory_access(
|
||||
MemoryAccessReason::Store,
|
||||
addr,
|
||||
source.cast_mut(),
|
||||
count,
|
||||
|perm| perm == Permission::Write,
|
||||
|dst, src, count| core::ptr::copy_nonoverlapping(src, dst, count),
|
||||
traph,
|
||||
)
|
||||
.map_err(StoreError)
|
||||
}
|
||||
|
||||
/// Copy a block of memory
|
||||
///
|
||||
/// # Safety
|
||||
/// - Same as for [`Self::load`] and [`Self::store`]
|
||||
/// - Your faith in the gods of UB
|
||||
/// - Addr-san claims it's fine but who knows is she isn't lying :ferrisSus:
|
||||
pub unsafe fn block_copy(
|
||||
&mut self,
|
||||
src: u64,
|
||||
dst: u64,
|
||||
count: usize,
|
||||
traph: &mut impl HandleTrap,
|
||||
) -> Result<(), BlkCopyError> {
|
||||
// Yea, i know it is possible to do this more efficiently, but I am too lazy.
|
||||
pub unsafe fn block_copy(&mut self, src: u64, dst: u64, count: u64) -> Result<(), ()> {
|
||||
/* let count = usize::try_from(count).expect("?conradluget a better CPU");
|
||||
|
||||
const STACK_BUFFER_SIZE: usize = 512;
|
||||
let mut srcs = PageSplitter::new(src, count, self.root_pt);
|
||||
let mut dsts = PageSplitter::new(dst, count, self.root_pt);
|
||||
let mut c_src = srcs.next().ok_or(())?;
|
||||
let mut c_dst = dsts.next().ok_or(())?;
|
||||
|
||||
// Decide if to use stack-allocated buffer or to heap allocate
|
||||
// Deallocation is again decided on size at the end of the function
|
||||
let mut buf = MaybeUninit::<[u8; STACK_BUFFER_SIZE]>::uninit();
|
||||
let buf = if count <= STACK_BUFFER_SIZE {
|
||||
buf.as_mut_ptr().cast()
|
||||
} else {
|
||||
loop {
|
||||
let min_size = c_src.size.min(c_dst.size);
|
||||
unsafe {
|
||||
let layout = core::alloc::Layout::from_size_align_unchecked(count, 1);
|
||||
let ptr = alloc::alloc::alloc(layout);
|
||||
if ptr.is_null() {
|
||||
alloc::alloc::handle_alloc_error(layout);
|
||||
core::ptr::copy(c_src.ptr, c_dst.ptr, min_size);
|
||||
}
|
||||
|
||||
ptr
|
||||
}
|
||||
};
|
||||
|
||||
// Perform memory block transfer
|
||||
let status = (|| {
|
||||
// Load to buffer
|
||||
self.memory_access(
|
||||
MemoryAccessReason::Load,
|
||||
src,
|
||||
buf,
|
||||
count,
|
||||
|perm| {
|
||||
matches!(
|
||||
perm,
|
||||
Permission::Readonly | Permission::Write | Permission::Exec
|
||||
)
|
||||
match (
|
||||
match c_src.size.saturating_sub(min_size) {
|
||||
0 => srcs.next(),
|
||||
size => Some(PageSplitResult { size, ..c_src }),
|
||||
},
|
||||
|src, dst, count| core::ptr::copy(src, dst, count),
|
||||
traph,
|
||||
)
|
||||
.map_err(|addr| BlkCopyError {
|
||||
access_reason: MemoryAccessReason::Load,
|
||||
addr,
|
||||
})?;
|
||||
match c_dst.size.saturating_sub(min_size) {
|
||||
0 => dsts.next(),
|
||||
size => Some(PageSplitResult { size, ..c_dst }),
|
||||
},
|
||||
) {
|
||||
(None, None) => return Ok(()),
|
||||
(Some(src), Some(dst)) => (c_src, c_dst) = (src, dst),
|
||||
_ => return Err(()),
|
||||
}
|
||||
} */
|
||||
|
||||
// Store from buffer
|
||||
self.memory_access(
|
||||
MemoryAccessReason::Store,
|
||||
dst,
|
||||
buf,
|
||||
count,
|
||||
|perm| perm == Permission::Write,
|
||||
|dst, src, count| core::ptr::copy(src, dst, count),
|
||||
traph,
|
||||
)
|
||||
.map_err(|addr| BlkCopyError {
|
||||
access_reason: MemoryAccessReason::Store,
|
||||
addr,
|
||||
})?;
|
||||
|
||||
Ok::<_, BlkCopyError>(())
|
||||
})();
|
||||
|
||||
// Deallocate if used heap-allocated array
|
||||
if count > STACK_BUFFER_SIZE {
|
||||
alloc::alloc::dealloc(
|
||||
buf,
|
||||
core::alloc::Layout::from_size_align_unchecked(count, 1),
|
||||
);
|
||||
todo!("Block memory copy")
|
||||
}
|
||||
|
||||
status
|
||||
#[inline]
|
||||
pub fn root_pt(&self) -> &PageTable {
|
||||
unsafe { &*self.root_pt }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn root_pt_mut(&mut self) -> &mut PageTable {
|
||||
unsafe { &mut *self.root_pt }
|
||||
}
|
||||
|
||||
/// Split address to pages, check their permissions and feed pointers with offset
|
||||
/// to a specified function.
|
||||
///
|
||||
/// If page is not found, execute page fault trap handler.
|
||||
#[allow(clippy::too_many_arguments)] // Silence peasant
|
||||
fn memory_access(
|
||||
&mut self,
|
||||
reason: MemoryAccessReason,
|
||||
&self,
|
||||
src: u64,
|
||||
mut dst: *mut u8,
|
||||
len: usize,
|
||||
permission_check: fn(Permission) -> bool,
|
||||
action: fn(*mut u8, *mut u8, usize),
|
||||
traph: &mut impl HandleTrap,
|
||||
) -> Result<(), u64> {
|
||||
let mut pspl = AddrSplitter::new(src, len, self.root_pt);
|
||||
loop {
|
||||
match pspl.next() {
|
||||
// Page found
|
||||
Some(Ok(AddrSplitOk {
|
||||
vaddr,
|
||||
ptr,
|
||||
size,
|
||||
perm,
|
||||
})) => {
|
||||
permission_check: impl Fn(Permission) -> bool,
|
||||
action: impl Fn(*mut u8, *mut u8, usize),
|
||||
) -> Result<(), AccessFault> {
|
||||
for item in PageSplitter::new(src, len, self.root_pt) {
|
||||
let PageSplitResult { ptr, size, perm } = item?;
|
||||
if !permission_check(perm) {
|
||||
return Err(vaddr);
|
||||
return Err(AccessFault::Permission);
|
||||
}
|
||||
|
||||
// Perform memory action and bump dst pointer
|
||||
action(ptr, dst, size);
|
||||
dst = unsafe { dst.add(size) };
|
||||
}
|
||||
Some(Err(AddrSplitError { addr, size })) => {
|
||||
// Execute page fault handler
|
||||
if traph.page_fault(reason, self, addr, size, dst) {
|
||||
// Shift the splitter address
|
||||
pspl.bump(size);
|
||||
|
||||
// Bump dst pointer
|
||||
dst = unsafe { dst.add(size as _) };
|
||||
} else {
|
||||
return Err(addr); // Unhandleable
|
||||
}
|
||||
}
|
||||
None => return Ok(()),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Result from address split
|
||||
struct AddrSplitOk {
|
||||
/// Virtual address
|
||||
vaddr: u64,
|
||||
|
||||
/// Pointer to the start for perform operation
|
||||
#[derive(Debug)]
|
||||
struct PageSplitResult {
|
||||
ptr: *mut u8,
|
||||
|
||||
/// Size to the end of page / end of desired size
|
||||
size: usize,
|
||||
|
||||
/// Page permission
|
||||
perm: Permission,
|
||||
}
|
||||
|
||||
struct AddrSplitError {
|
||||
/// Address of failure
|
||||
struct PageSplitter {
|
||||
addr: u64,
|
||||
|
||||
/// Requested page size
|
||||
size: PageSize,
|
||||
}
|
||||
|
||||
/// Address splitter into pages
|
||||
struct AddrSplitter {
|
||||
/// Current address
|
||||
addr: u64,
|
||||
|
||||
/// Size left
|
||||
size: usize,
|
||||
|
||||
/// Page table
|
||||
pagetable: *const PageTable,
|
||||
}
|
||||
|
||||
impl AddrSplitter {
|
||||
/// Create a new page splitter
|
||||
impl PageSplitter {
|
||||
pub const fn new(addr: u64, size: usize, pagetable: *const PageTable) -> Self {
|
||||
Self {
|
||||
addr,
|
||||
|
@ -291,29 +170,19 @@ impl AddrSplitter {
|
|||
pagetable,
|
||||
}
|
||||
}
|
||||
|
||||
/// Bump address by size X
|
||||
fn bump(&mut self, page_size: PageSize) {
|
||||
self.addr += page_size as u64;
|
||||
self.size = self.size.saturating_sub(page_size as _);
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for AddrSplitter {
|
||||
type Item = Result<AddrSplitOk, AddrSplitError>;
|
||||
impl Iterator for PageSplitter {
|
||||
type Item = Result<PageSplitResult, AccessFault>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
// The end, everything is fine
|
||||
if self.size == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let (base, perm, size, offset) = 'a: {
|
||||
let mut current_pt = self.pagetable;
|
||||
|
||||
// Walk the page table
|
||||
for lvl in (0..5).rev() {
|
||||
// Get an entry
|
||||
unsafe {
|
||||
let entry = (*current_pt).get_unchecked(
|
||||
usize::try_from((self.addr >> (lvl * 9 + 12)) & ((1 << 9) - 1))
|
||||
|
@ -322,109 +191,48 @@ impl Iterator for AddrSplitter {
|
|||
|
||||
let ptr = entry.ptr();
|
||||
match entry.permission() {
|
||||
// No page → page fault
|
||||
Permission::Empty => {
|
||||
return Some(Err(AddrSplitError {
|
||||
return Some(Err(AccessFault::NoPage {
|
||||
addr: self.addr,
|
||||
size: PageSize::from_lvl(lvl)?,
|
||||
remaining: self.size,
|
||||
}))
|
||||
}
|
||||
|
||||
// Node → proceed waking
|
||||
Permission::Node => current_pt = ptr as _,
|
||||
|
||||
// Leaft → return relevant data
|
||||
perm => {
|
||||
break 'a (
|
||||
// Pointer in host memory
|
||||
ptr as *mut u8,
|
||||
perm,
|
||||
PageSize::from_lvl(lvl)?,
|
||||
// In-page offset
|
||||
match lvl {
|
||||
0 => 4096,
|
||||
1 => 1024_usize.pow(2) * 2,
|
||||
2 => 1024_usize.pow(3),
|
||||
_ => return Some(Err(AccessFault::TooShallow)),
|
||||
},
|
||||
self.addr as usize & ((1 << (lvl * 9 + 12)) - 1),
|
||||
);
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return None; // Reached the end (should not happen)
|
||||
return Some(Err(AccessFault::TooDeep));
|
||||
};
|
||||
|
||||
// Get available byte count in the selected page with offset
|
||||
let avail = (size as usize - offset).clamp(0, self.size);
|
||||
self.bump(size);
|
||||
|
||||
Some(Ok(AddrSplitOk {
|
||||
vaddr: self.addr,
|
||||
ptr: unsafe { base.add(offset) }, // Return pointer to the start of region
|
||||
extern crate std;
|
||||
let avail = (size - offset).clamp(0, self.size);
|
||||
self.addr += size as u64;
|
||||
self.size = self.size.saturating_sub(size);
|
||||
std::dbg!(Some(Ok(PageSplitResult {
|
||||
ptr: unsafe { base.add(offset) },
|
||||
size: avail,
|
||||
perm,
|
||||
}))
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
||||
/// Page size
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum PageSize {
|
||||
/// 4 KiB page (on level 0)
|
||||
Size4K = 4096,
|
||||
|
||||
/// 2 MiB page (on level 1)
|
||||
Size2M = 1024 * 1024 * 2,
|
||||
|
||||
/// 1 GiB page (on level 2)
|
||||
Size1G = 1024 * 1024 * 1024,
|
||||
}
|
||||
|
||||
impl PageSize {
|
||||
/// Convert page table level to size of page
|
||||
fn from_lvl(lvl: u8) -> Option<Self> {
|
||||
match lvl {
|
||||
0 => Some(PageSize::Size4K),
|
||||
1 => Some(PageSize::Size2M),
|
||||
2 => Some(PageSize::Size1G),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Unhandled load access trap
|
||||
#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)]
|
||||
pub struct LoadError(u64);
|
||||
|
||||
/// Unhandled store access trap
|
||||
#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)]
|
||||
pub struct StoreError(u64);
|
||||
|
||||
#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)]
|
||||
pub enum MemoryAccessReason {
|
||||
Load,
|
||||
Store,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct BlkCopyError {
|
||||
access_reason: MemoryAccessReason,
|
||||
addr: u64,
|
||||
}
|
||||
|
||||
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 {
|
||||
fn from(value: LoadError) -> Self {
|
||||
Self::LoadAccessEx(value.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<StoreError> for VmRunError {
|
||||
fn from(value: StoreError) -> Self {
|
||||
Self::StoreAccessEx(value.0)
|
||||
}
|
||||
pub enum AccessFault {
|
||||
NoPage { addr: u64, remaining: usize },
|
||||
Permission,
|
||||
TooShallow,
|
||||
TooDeep,
|
||||
}
|
||||
|
|
|
@ -1,53 +1,35 @@
|
|||
//! Page table and associated structures implementation
|
||||
|
||||
use {
|
||||
core::{
|
||||
use core::{
|
||||
fmt::Debug,
|
||||
mem::MaybeUninit,
|
||||
ops::{Index, IndexMut},
|
||||
slice::SliceIndex,
|
||||
},
|
||||
delegate::delegate,
|
||||
};
|
||||
use delegate::delegate;
|
||||
|
||||
/// Page entry permission
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
pub enum Permission {
|
||||
/// No page present
|
||||
#[default]
|
||||
Empty,
|
||||
/// Points to another pagetable
|
||||
Node,
|
||||
/// Page is read only
|
||||
Readonly,
|
||||
/// Page is readable and writable
|
||||
Write,
|
||||
/// Page is readable and executable
|
||||
Exec,
|
||||
}
|
||||
|
||||
/// Page table entry
|
||||
#[derive(Clone, Copy, Default, PartialEq, Eq)]
|
||||
pub struct PtEntry(u64);
|
||||
impl PtEntry {
|
||||
/// Create new
|
||||
///
|
||||
/// # Safety
|
||||
/// - `ptr` has to point to valid data and shall not be deallocated
|
||||
/// troughout the entry lifetime
|
||||
#[inline]
|
||||
pub unsafe fn new(ptr: *mut PtPointedData, permission: Permission) -> Self {
|
||||
Self(ptr as u64 | permission as u64)
|
||||
}
|
||||
|
||||
/// Get permission
|
||||
#[inline]
|
||||
pub fn permission(&self) -> Permission {
|
||||
unsafe { core::mem::transmute(self.0 as u8 & 0b111) }
|
||||
}
|
||||
|
||||
/// Get pointer to the data (leaf) or next page table (node)
|
||||
#[inline]
|
||||
pub fn ptr(&self) -> *mut PtPointedData {
|
||||
(self.0 & !((1 << 12) - 1)) as _
|
||||
|
@ -63,50 +45,21 @@ impl Debug for PtEntry {
|
|||
}
|
||||
}
|
||||
|
||||
/// Page table
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[repr(align(4096))]
|
||||
pub struct PageTable([PtEntry; 512]);
|
||||
|
||||
impl PageTable {
|
||||
delegate!(to self.0 {
|
||||
/// Returns a reference to an element or subslice depending on the type of
|
||||
/// 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>
|
||||
pub unsafe fn get<I>(&self, ix: I) -> Option<&I::Output>
|
||||
where I: SliceIndex<[PtEntry]>;
|
||||
|
||||
/// Returns a mutable reference to an element or subslice depending on the
|
||||
/// 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>
|
||||
pub unsafe fn get_mut<I>(&mut self, ix: I) -> Option<&mut I::Output>
|
||||
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
|
||||
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
|
||||
where I: SliceIndex<[PtEntry]>;
|
||||
});
|
||||
|
@ -136,17 +89,13 @@ where
|
|||
|
||||
impl Default for PageTable {
|
||||
fn default() -> Self {
|
||||
// SAFETY: It's fine, zeroed page table entry is valid (= empty)
|
||||
Self(unsafe { MaybeUninit::zeroed().assume_init() })
|
||||
}
|
||||
}
|
||||
|
||||
/// Data page table entry can possibly point to
|
||||
#[derive(Clone, Copy)]
|
||||
#[repr(C, align(4096))]
|
||||
pub union PtPointedData {
|
||||
/// Node - next page table
|
||||
pub pt: PageTable,
|
||||
/// Leaf
|
||||
pub page: u8,
|
||||
}
|
||||
|
|
|
@ -11,11 +11,10 @@
|
|||
// program size. If you are (rightfully) worried about the UB, for now just
|
||||
// append your program with 11 zeroes.
|
||||
|
||||
use self::trap::HandleTrap;
|
||||
use self::mem::AccessFault;
|
||||
|
||||
pub mod mem;
|
||||
pub mod trap;
|
||||
pub mod value;
|
||||
mod mem;
|
||||
mod value;
|
||||
|
||||
use {
|
||||
crate::validate,
|
||||
|
@ -26,7 +25,6 @@ use {
|
|||
value::Value,
|
||||
};
|
||||
|
||||
/// Extract a parameter from program
|
||||
macro_rules! param {
|
||||
($self:expr, $ty:ty) => {{
|
||||
assert_impl_one!($ty: OpParam);
|
||||
|
@ -41,7 +39,6 @@ macro_rules! param {
|
|||
}};
|
||||
}
|
||||
|
||||
/// Perform binary operation `#0 ← #1 OP #2`
|
||||
macro_rules! binary_op {
|
||||
($self:expr, $ty:ident, $handler:expr) => {{
|
||||
let ParamBBB(tg, a0, a1) = param!($self, ParamBBB);
|
||||
|
@ -50,23 +47,22 @@ macro_rules! binary_op {
|
|||
$handler(
|
||||
Value::$ty(&$self.read_reg(a0)),
|
||||
Value::$ty(&$self.read_reg(a1)),
|
||||
),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
}};
|
||||
}
|
||||
|
||||
/// Perform binary operation with immediate `#0 ← #1 OP imm #2`
|
||||
macro_rules! binary_op_imm {
|
||||
($self:expr, $ty:ident, $handler:expr) => {{
|
||||
let ParamBBD(tg, a0, imm) = param!($self, ParamBBD);
|
||||
$self.write_reg(
|
||||
tg,
|
||||
$handler(Value::$ty(&$self.read_reg(a0)), Value::$ty(&imm.into())),
|
||||
$handler(Value::$ty(&$self.read_reg(a0)), Value::$ty(&imm.into())).into(),
|
||||
);
|
||||
}};
|
||||
}
|
||||
|
||||
/// Jump at `#3` if ordering on `#0 <=> #1` is equal to expected
|
||||
macro_rules! cond_jump {
|
||||
($self:expr, $ty:ident, $expected:ident) => {{
|
||||
let ParamBBD(a0, a1, jt) = param!($self, ParamBBD);
|
||||
|
@ -78,63 +74,36 @@ macro_rules! cond_jump {
|
|||
}};
|
||||
}
|
||||
|
||||
/// HoleyBytes Virtual Machine
|
||||
pub struct Vm<'a, T, const TIMER_QUOTIENT: usize> {
|
||||
/// Holds 256 registers
|
||||
///
|
||||
/// Writing to register 0 is considered undefined behaviour
|
||||
/// in terms of HoleyBytes program execution
|
||||
pub struct Vm<'a> {
|
||||
pub registers: [Value; 256],
|
||||
|
||||
/// Memory implementation
|
||||
pub memory: Memory,
|
||||
|
||||
/// Trap handler
|
||||
pub traph: T,
|
||||
|
||||
// Program counter
|
||||
pc: usize,
|
||||
|
||||
/// Program
|
||||
program: &'a [u8],
|
||||
|
||||
/// Program timer
|
||||
timer: usize,
|
||||
}
|
||||
|
||||
impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
||||
/// Create a new VM with program and trap handler
|
||||
///
|
||||
impl<'a> Vm<'a> {
|
||||
/// # Safety
|
||||
/// Program code has to be validated
|
||||
pub unsafe fn new_unchecked(program: &'a [u8], traph: T) -> Self {
|
||||
pub unsafe fn new_unchecked(program: &'a [u8]) -> Self {
|
||||
Self {
|
||||
registers: [Value::from(0_u64); 256],
|
||||
memory: Default::default(),
|
||||
traph,
|
||||
pc: 0,
|
||||
program,
|
||||
timer: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new VM with program and trap handler only if it passes validation
|
||||
pub fn new_validated(program: &'a [u8], traph: T) -> Result<Self, validate::Error> {
|
||||
pub fn new_validated(program: &'a [u8]) -> Result<Self, validate::Error> {
|
||||
validate::validate(program)?;
|
||||
Ok(unsafe { Self::new_unchecked(program, traph) })
|
||||
Ok(unsafe { Self::new_unchecked(program) })
|
||||
}
|
||||
|
||||
/// Execute program
|
||||
///
|
||||
/// Program can return [`VmRunError`] if a trap handling failed
|
||||
pub fn run(&mut self) -> Result<VmRunOk, VmRunError> {
|
||||
pub fn run(&mut self) -> HaltReason {
|
||||
use hbbytecode::opcode::*;
|
||||
loop {
|
||||
// Fetch instruction
|
||||
let Some(&opcode) = self.program.get(self.pc)
|
||||
else { return Ok(VmRunOk::End) };
|
||||
else { return HaltReason::ProgramEnd };
|
||||
|
||||
// Big match
|
||||
unsafe {
|
||||
match opcode {
|
||||
NOP => param!(self, ()),
|
||||
|
@ -151,19 +120,21 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
|||
let ParamBBB(tg, a0, a1) = param!(self, ParamBBB);
|
||||
self.write_reg(
|
||||
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 => {
|
||||
let ParamBBB(tg, a0, a1) = param!(self, ParamBBB);
|
||||
self.write_reg(
|
||||
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 => {
|
||||
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 => {
|
||||
let param = param!(self, ParamBB);
|
||||
|
@ -172,15 +143,16 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
|||
match self.read_reg(param.1).as_u64() {
|
||||
0 => 1_u64,
|
||||
_ => 0,
|
||||
},
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
DIR => {
|
||||
let ParamBBBB(dt, rt, a0, a1) = param!(self, ParamBBBB);
|
||||
let a0 = self.read_reg(a0).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(rt, a0.checked_rem(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)).into());
|
||||
}
|
||||
ADDI => binary_op_imm!(self, as_u64, ops::Add::add),
|
||||
MULI => binary_op_imm!(self, as_u64, ops::Mul::mul),
|
||||
|
@ -194,12 +166,13 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
|||
let ParamBBD(tg, a0, imm) = param!(self, ParamBBD);
|
||||
self.write_reg(
|
||||
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 => {
|
||||
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 => {
|
||||
let param = param!(self, ParamBB);
|
||||
|
@ -216,7 +189,7 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
|||
}
|
||||
LI => {
|
||||
let param = param!(self, ParamBD);
|
||||
self.write_reg(param.0, param.1);
|
||||
self.write_reg(param.0, param.1.into());
|
||||
}
|
||||
LD => {
|
||||
let ParamBBDH(dst, base, off, count) = param!(self, ParamBBDH);
|
||||
|
@ -225,30 +198,37 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
|||
_ => 0,
|
||||
};
|
||||
|
||||
self.memory.load(
|
||||
if let Err(e) = self.memory.load(
|
||||
self.read_reg(base).as_u64() + off + n as u64,
|
||||
self.registers.as_mut_ptr().add(usize::from(dst) + n).cast(),
|
||||
usize::from(count).saturating_sub(n),
|
||||
&mut self.traph,
|
||||
)?;
|
||||
) {
|
||||
return HaltReason::LoadAccessEx(e);
|
||||
}
|
||||
}
|
||||
ST => {
|
||||
let ParamBBDH(dst, base, off, count) = param!(self, ParamBBDH);
|
||||
self.memory.store(
|
||||
if let Err(e) = self.memory.store(
|
||||
self.read_reg(base).as_u64() + off,
|
||||
self.registers.as_ptr().add(usize::from(dst)).cast(),
|
||||
count.into(),
|
||||
&mut self.traph,
|
||||
)?;
|
||||
) {
|
||||
return HaltReason::StoreAccessEx(e);
|
||||
}
|
||||
}
|
||||
BMC => {
|
||||
let ParamBBD(src, dst, count) = param!(self, ParamBBD);
|
||||
self.memory.block_copy(
|
||||
if self
|
||||
.memory
|
||||
.block_copy(
|
||||
self.read_reg(src).as_u64(),
|
||||
self.read_reg(dst).as_u64(),
|
||||
count as _,
|
||||
&mut self.traph,
|
||||
)?;
|
||||
count,
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
todo!("Block memory copy fault");
|
||||
}
|
||||
}
|
||||
BRC => {
|
||||
let ParamBBB(src, dst, count) = param!(self, ParamBBB);
|
||||
|
@ -275,99 +255,48 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
|||
JGTU => cond_jump!(self, sint, Greater),
|
||||
ECALL => {
|
||||
param!(self, ());
|
||||
self.traph
|
||||
.ecall(&mut self.registers, &mut self.pc, &mut self.memory);
|
||||
return HaltReason::Ecall;
|
||||
}
|
||||
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),
|
||||
DIRF => {
|
||||
let ParamBBBB(dt, rt, a0, a1) = param!(self, ParamBBBB);
|
||||
let a0 = self.read_reg(a0).as_f64();
|
||||
let a1 = self.read_reg(a1).as_f64();
|
||||
self.write_reg(dt, a0 / a1);
|
||||
self.write_reg(rt, a0 % a1);
|
||||
}
|
||||
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);
|
||||
self.write_reg(dt, (a0 / a1).into());
|
||||
self.write_reg(rt, (a0 % a1).into());
|
||||
}
|
||||
ADDFI => binary_op_imm!(self, as_f64, ops::Add::add),
|
||||
MULFI => binary_op_imm!(self, as_f64, ops::Mul::mul),
|
||||
op => {
|
||||
if !self.traph.invalid_op(
|
||||
&mut self.registers,
|
||||
&mut self.pc,
|
||||
&mut self.memory,
|
||||
op,
|
||||
) {
|
||||
return Err(VmRunError::InvalidOpcodeEx(op));
|
||||
op => return HaltReason::InvalidOpEx(op),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if TIMER_QUOTIENT != 0 {
|
||||
self.timer = self.timer.wrapping_add(1);
|
||||
if self.timer % TIMER_QUOTIENT == 0 {
|
||||
return Ok(VmRunOk::Timer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Read register
|
||||
#[inline]
|
||||
unsafe fn read_reg(&self, n: u8) -> Value {
|
||||
if n == 0 {
|
||||
0_u64.into()
|
||||
} else {
|
||||
*self.registers.get_unchecked(n as usize)
|
||||
}
|
||||
}
|
||||
|
||||
/// Write a register.
|
||||
/// Writing to register 0 is no-op.
|
||||
#[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 {
|
||||
*self.registers.get_unchecked_mut(n as usize) = value.into();
|
||||
*self.registers.get_unchecked_mut(n as usize) = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Virtual machine halt error
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
pub enum VmRunError {
|
||||
/// Unhandled invalid opcode exceptions
|
||||
InvalidOpcodeEx(u8),
|
||||
|
||||
/// Unhandled load access exception
|
||||
LoadAccessEx(u64),
|
||||
|
||||
/// Unhandled store access exception
|
||||
StoreAccessEx(u64),
|
||||
}
|
||||
|
||||
/// 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,
|
||||
pub enum HaltReason {
|
||||
ProgramEnd,
|
||||
Ecall,
|
||||
InvalidOpEx(u8),
|
||||
LoadAccessEx(AccessFault),
|
||||
StoreAccessEx(AccessFault),
|
||||
}
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
//! Program trap handling interfaces
|
||||
|
||||
use super::{
|
||||
mem::{Memory, MemoryAccessReason, PageSize},
|
||||
value::Value,
|
||||
};
|
||||
|
||||
/// Handle VM traps
|
||||
pub trait HandleTrap {
|
||||
/// Handle page fault
|
||||
fn page_fault(
|
||||
&mut self,
|
||||
reason: MemoryAccessReason,
|
||||
memory: &mut Memory,
|
||||
vaddr: u64,
|
||||
size: PageSize,
|
||||
dataptr: *mut u8,
|
||||
) -> bool;
|
||||
|
||||
/// Handle invalid opcode exception
|
||||
fn invalid_op(
|
||||
&mut self,
|
||||
regs: &mut [Value; 256],
|
||||
pc: &mut usize,
|
||||
memory: &mut Memory,
|
||||
op: u8,
|
||||
) -> bool
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
/// Handle environment calls
|
||||
fn ecall(&mut self, regs: &mut [Value; 256], pc: &mut usize, memory: &mut Memory)
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
|
@ -1,15 +1,7 @@
|
|||
//! HoleyBytes register value definition
|
||||
|
||||
use core::fmt::Debug;
|
||||
|
||||
/// Define [`Value`] union
|
||||
///
|
||||
/// # Safety
|
||||
/// Union variants have to be sound to byte-reinterpretate
|
||||
/// between each other. Otherwise the behaviour is undefined.
|
||||
macro_rules! value_def {
|
||||
($($ty:ident),* $(,)?) => {
|
||||
/// HBVM register value
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(packed)]
|
||||
pub union Value {
|
||||
|
@ -18,7 +10,6 @@ macro_rules! value_def {
|
|||
|
||||
paste::paste! {
|
||||
impl Value {$(
|
||||
#[doc = "Byte-reinterpret [`Value`] as [`" $ty "`]"]
|
||||
#[inline]
|
||||
pub fn [<as_ $ty>](&self) -> $ty {
|
||||
unsafe { self.$ty }
|
||||
|
@ -38,11 +29,9 @@ macro_rules! value_def {
|
|||
}
|
||||
|
||||
value_def!(u64, i64, f64);
|
||||
static_assertions::const_assert_eq!(core::mem::size_of::<Value>(), 8);
|
||||
|
||||
impl Debug for Value {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
// Print formatted as hexadecimal, unsigned integer
|
||||
write!(f, "{:x}", self.as_u64())
|
||||
self.as_u64().fmt(f)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
nightly
|
|
@ -1,4 +1,3 @@
|
|||
hex_literal_case = "Upper"
|
||||
imports_granularity = "One"
|
||||
struct_field_align_threshold = 5
|
||||
enum_discrim_align_threshold = 5
|
35
spec.md
35
spec.md
|
@ -19,6 +19,7 @@
|
|||
| BBBB | 32 bits |
|
||||
| BBB | 24 bits |
|
||||
| BBDH | 96 bits |
|
||||
| BBDB | 88 bits |
|
||||
| BBD | 80 bits |
|
||||
| BB | 16 bits |
|
||||
| BD | 72 bits |
|
||||
|
@ -199,38 +200,14 @@
|
|||
| Opcode | Name | Action |
|
||||
|:------:|:----:|:--------------:|
|
||||
| 40 | ADDF | Addition |
|
||||
| 41 | SUBF | Subtraction |
|
||||
| 42 | MULF | Multiplication |
|
||||
| 41 | MULF | Multiplication |
|
||||
|
||||
### Division-remainder
|
||||
- Type BBBB
|
||||
|
||||
| Opcode | Name | Action |
|
||||
|:------:|:----:|:-------------------------:|
|
||||
| 43 | DIRF | Same 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 |
|
||||
|:------:|:----:|:--------------------------------------:|
|
||||
| 42 | DIRF | Same flow applies as for integer `DIR` |
|
||||
|
||||
## Floating point immediate operations
|
||||
- Type BBD
|
||||
|
@ -238,8 +215,8 @@
|
|||
|
||||
| Opcode | Name | Action |
|
||||
|:------:|:-----:|:--------------:|
|
||||
| 48 | ADDFI | Addition |
|
||||
| 49 | MULFI | Multiplication |
|
||||
| 43 | ADDFI | Addition |
|
||||
| 44 | MULFI | Multiplication |
|
||||
|
||||
# Registers
|
||||
- There is 255 registers + one zero register (with index 0)
|
||||
|
|
Loading…
Reference in a new issue