renaming directories, reducing temporary allocations during parsing

This commit is contained in:
Jakub Doka 2024-10-10 15:46:33 +02:00
parent a538c0ddb0
commit 42f55c442d
No known key found for this signature in database
GPG key ID: C6E9A89936B8C143
105 changed files with 1045 additions and 76 deletions

16
Cargo.lock generated
View file

@ -271,10 +271,6 @@ dependencies = [
name = "hbbytecode"
version = "0.1.0"
[[package]]
name = "hbjit"
version = "0.1.0"
[[package]]
name = "hblang"
version = "0.1.0"
@ -309,18 +305,18 @@ checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]]
name = "htmlm"
version = "0.3.0"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91eeb833c1f091f72c22c2d213d8acb12b953cea20fd911a2f636e806858a391"
checksum = "a95b97f2c13991e486bf95be6d19c6c3d1fef4f8ec1e298e40aaf98769789295"
dependencies = [
"htmlm-macro",
]
[[package]]
name = "htmlm-macro"
version = "0.1.0"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3607a5606a3432058fa765bc4dec74ab8d103282cf16f35df7be838a674ebaa9"
checksum = "7f881f4929b944a9566f12d8ac3bf9881325c77c11b9b0adcdc6944018b55ac7"
[[package]]
name = "http"
@ -809,6 +805,10 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-hbc"
version = "0.1.0"
[[package]]
name = "wasm-hbfmt"
version = "0.1.0"

View file

@ -1,19 +1,27 @@
[workspace]
resolver = "2"
members = [
"hbbytecode",
"hbvm",
"hbxrt",
"bytecode",
"vm",
"xrt",
"xtask",
"hblang",
"hbjit",
"lang",
"depell",
"depell/wasm-hbfmt"
"depell/wasm-fmt",
"depell/wasm-hbc",
]
[workspace.dependencies]
hbbytecode = { path = "bytecode", default-features = false }
hbvm = { path = "vm" }
hbxrt = { path = "xrt" }
hblang = { path = "lang", default-features = false }
hbjit = { path = "jit" }
[profile.release]
lto = true
debug = true
#strip = true
codegen-units = 1
panic = "abort"

View file

@ -159,7 +159,7 @@ fn comma_sep(items: impl Iterator<Item = String>) -> String {
}
fn instructions() -> impl Iterator<Item = [&'static str; 4]> {
include_str!("../hbbytecode/instructions.in")
include_str!("instructions.in")
.lines()
.filter_map(|line| line.strip_suffix(';'))
.map(|line| line.splitn(4, ',').map(str::trim).next_chunk().unwrap())

904
bytecode/src/instrs.rs Normal file
View file

@ -0,0 +1,904 @@
#![allow(dead_code)] #![allow(clippy::upper_case_acronyms)]
use crate::*;
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsN();
unsafe impl BytecodeItem for OpsN {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRRR(pub OpR,pub OpR,pub OpR);
unsafe impl BytecodeItem for OpsRRR {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRRRR(pub OpR,pub OpR,pub OpR,pub OpR);
unsafe impl BytecodeItem for OpsRRRR {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRR(pub OpR,pub OpR);
unsafe impl BytecodeItem for OpsRR {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRRB(pub OpR,pub OpR,pub OpB);
unsafe impl BytecodeItem for OpsRRB {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRRH(pub OpR,pub OpR,pub OpH);
unsafe impl BytecodeItem for OpsRRH {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRRW(pub OpR,pub OpR,pub OpW);
unsafe impl BytecodeItem for OpsRRW {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRRD(pub OpR,pub OpR,pub OpD);
unsafe impl BytecodeItem for OpsRRD {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRB(pub OpR,pub OpB);
unsafe impl BytecodeItem for OpsRB {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRH(pub OpR,pub OpH);
unsafe impl BytecodeItem for OpsRH {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRW(pub OpR,pub OpW);
unsafe impl BytecodeItem for OpsRW {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRD(pub OpR,pub OpD);
unsafe impl BytecodeItem for OpsRD {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRRO(pub OpR,pub OpR,pub OpO);
unsafe impl BytecodeItem for OpsRRO {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRRAH(pub OpR,pub OpR,pub OpA,pub OpH);
unsafe impl BytecodeItem for OpsRRAH {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRROH(pub OpR,pub OpR,pub OpO,pub OpH);
unsafe impl BytecodeItem for OpsRROH {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsO(pub OpO);
unsafe impl BytecodeItem for OpsO {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRRA(pub OpR,pub OpR,pub OpA);
unsafe impl BytecodeItem for OpsRRA {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRRP(pub OpR,pub OpR,pub OpP);
unsafe impl BytecodeItem for OpsRRP {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsRRPH(pub OpR,pub OpR,pub OpP,pub OpH);
unsafe impl BytecodeItem for OpsRRPH {}
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct OpsP(pub OpP);
unsafe impl BytecodeItem for OpsP {}
pub const MAX_SIZE: usize = 13;
/// Cause an unreachable code trap
pub fn un() -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(N(0x00, )) }
}
/// Termiante execution
pub fn tx() -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(N(0x01, )) }
}
/// Do nothing
pub fn nop() -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(N(0x02, )) }
}
/// Addition (8b)
pub fn add8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x03, reg0, reg1, reg2)) }
}
/// Addition (16b)
pub fn add16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x04, reg0, reg1, reg2)) }
}
/// Addition (32b)
pub fn add32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x05, reg0, reg1, reg2)) }
}
/// Addition (64b)
pub fn add64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x06, reg0, reg1, reg2)) }
}
/// Subtraction (8b)
pub fn sub8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x07, reg0, reg1, reg2)) }
}
/// Subtraction (16b)
pub fn sub16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x08, reg0, reg1, reg2)) }
}
/// Subtraction (32b)
pub fn sub32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x09, reg0, reg1, reg2)) }
}
/// Subtraction (64b)
pub fn sub64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x0A, reg0, reg1, reg2)) }
}
/// Multiplication (8b)
pub fn mul8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x0B, reg0, reg1, reg2)) }
}
/// Multiplication (16b)
pub fn mul16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x0C, reg0, reg1, reg2)) }
}
/// Multiplication (32b)
pub fn mul32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x0D, reg0, reg1, reg2)) }
}
/// Multiplication (64b)
pub fn mul64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x0E, reg0, reg1, reg2)) }
}
/// Bitand
pub fn and(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x0F, reg0, reg1, reg2)) }
}
/// Bitor
pub fn or(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x10, reg0, reg1, reg2)) }
}
/// Bitxor
pub fn xor(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x11, reg0, reg1, reg2)) }
}
/// Unsigned left bitshift (8b)
pub fn slu8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x12, reg0, reg1, reg2)) }
}
/// Unsigned left bitshift (16b)
pub fn slu16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x13, reg0, reg1, reg2)) }
}
/// Unsigned left bitshift (32b)
pub fn slu32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x14, reg0, reg1, reg2)) }
}
/// Unsigned left bitshift (64b)
pub fn slu64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x15, reg0, reg1, reg2)) }
}
/// Unsigned right bitshift (8b)
pub fn sru8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x16, reg0, reg1, reg2)) }
}
/// Unsigned right bitshift (16b)
pub fn sru16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x17, reg0, reg1, reg2)) }
}
/// Unsigned right bitshift (32b)
pub fn sru32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x18, reg0, reg1, reg2)) }
}
/// Unsigned right bitshift (64b)
pub fn sru64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x19, reg0, reg1, reg2)) }
}
/// Signed right bitshift (8b)
pub fn srs8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x1A, reg0, reg1, reg2)) }
}
/// Signed right bitshift (16b)
pub fn srs16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x1B, reg0, reg1, reg2)) }
}
/// Signed right bitshift (32b)
pub fn srs32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x1C, reg0, reg1, reg2)) }
}
/// Signed right bitshift (64b)
pub fn srs64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x1D, reg0, reg1, reg2)) }
}
/// Unsigned comparsion
pub fn cmpu(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x1E, reg0, reg1, reg2)) }
}
/// Signed comparsion
pub fn cmps(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x1F, reg0, reg1, reg2)) }
}
/// Merged divide-remainder (unsigned 8b)
pub fn diru8(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRRR(0x20, reg0, reg1, reg2, reg3)) }
}
/// Merged divide-remainder (unsigned 16b)
pub fn diru16(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRRR(0x21, reg0, reg1, reg2, reg3)) }
}
/// Merged divide-remainder (unsigned 32b)
pub fn diru32(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRRR(0x22, reg0, reg1, reg2, reg3)) }
}
/// Merged divide-remainder (unsigned 64b)
pub fn diru64(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRRR(0x23, reg0, reg1, reg2, reg3)) }
}
/// Merged divide-remainder (signed 8b)
pub fn dirs8(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRRR(0x24, reg0, reg1, reg2, reg3)) }
}
/// Merged divide-remainder (signed 16b)
pub fn dirs16(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRRR(0x25, reg0, reg1, reg2, reg3)) }
}
/// Merged divide-remainder (signed 32b)
pub fn dirs32(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRRR(0x26, reg0, reg1, reg2, reg3)) }
}
/// Merged divide-remainder (signed 64b)
pub fn dirs64(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRRR(0x27, reg0, reg1, reg2, reg3)) }
}
/// Bit negation
pub fn neg(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RR(0x28, reg0, reg1)) }
}
/// Logical negation
pub fn not(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RR(0x29, reg0, reg1)) }
}
/// Sign extend 8b to 64b
pub fn sxt8(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RR(0x2A, reg0, reg1)) }
}
/// Sign extend 16b to 64b
pub fn sxt16(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RR(0x2B, reg0, reg1)) }
}
/// Sign extend 32b to 64b
pub fn sxt32(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RR(0x2C, reg0, reg1)) }
}
/// Addition with immediate (8b)
pub fn addi8(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x2D, reg0, reg1, imm2)) }
}
/// Addition with immediate (16b)
pub fn addi16(reg0: u8, reg1: u8, imm2: u16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRH(0x2E, reg0, reg1, imm2)) }
}
/// Addition with immediate (32b)
pub fn addi32(reg0: u8, reg1: u8, imm2: u32) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRW(0x2F, reg0, reg1, imm2)) }
}
/// Addition with immediate (64b)
pub fn addi64(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRD(0x30, reg0, reg1, imm2)) }
}
/// Multiplication with immediate (8b)
pub fn muli8(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x31, reg0, reg1, imm2)) }
}
/// Multiplication with immediate (16b)
pub fn muli16(reg0: u8, reg1: u8, imm2: u16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRH(0x32, reg0, reg1, imm2)) }
}
/// Multiplication with immediate (32b)
pub fn muli32(reg0: u8, reg1: u8, imm2: u32) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRW(0x33, reg0, reg1, imm2)) }
}
/// Multiplication with immediate (64b)
pub fn muli64(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRD(0x34, reg0, reg1, imm2)) }
}
/// Bitand with immediate
pub fn andi(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRD(0x35, reg0, reg1, imm2)) }
}
/// Bitor with immediate
pub fn ori(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRD(0x36, reg0, reg1, imm2)) }
}
/// Bitxor with immediate
pub fn xori(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRD(0x37, reg0, reg1, imm2)) }
}
/// Unsigned left bitshift with immedidate (8b)
pub fn slui8(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x38, reg0, reg1, imm2)) }
}
/// Unsigned left bitshift with immedidate (16b)
pub fn slui16(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x39, reg0, reg1, imm2)) }
}
/// Unsigned left bitshift with immedidate (32b)
pub fn slui32(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x3A, reg0, reg1, imm2)) }
}
/// Unsigned left bitshift with immedidate (64b)
pub fn slui64(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x3B, reg0, reg1, imm2)) }
}
/// Unsigned right bitshift with immediate (8b)
pub fn srui8(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x3C, reg0, reg1, imm2)) }
}
/// Unsigned right bitshift with immediate (16b)
pub fn srui16(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x3D, reg0, reg1, imm2)) }
}
/// Unsigned right bitshift with immediate (32b)
pub fn srui32(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x3E, reg0, reg1, imm2)) }
}
/// Unsigned right bitshift with immediate (64b)
pub fn srui64(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x3F, reg0, reg1, imm2)) }
}
/// Signed right bitshift with immediate
pub fn srsi8(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x40, reg0, reg1, imm2)) }
}
/// Signed right bitshift with immediate
pub fn srsi16(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x41, reg0, reg1, imm2)) }
}
/// Signed right bitshift with immediate
pub fn srsi32(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x42, reg0, reg1, imm2)) }
}
/// Signed right bitshift with immediate
pub fn srsi64(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x43, reg0, reg1, imm2)) }
}
/// Unsigned compare with immediate
pub fn cmpui(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRD(0x44, reg0, reg1, imm2)) }
}
/// Signed compare with immediate
pub fn cmpsi(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRD(0x45, reg0, reg1, imm2)) }
}
/// Copy register
pub fn cp(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RR(0x46, reg0, reg1)) }
}
/// Swap registers
pub fn swa(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RR(0x47, reg0, reg1)) }
}
/// Load immediate (8b)
pub fn li8(reg0: u8, imm1: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RB(0x48, reg0, imm1)) }
}
/// Load immediate (16b)
pub fn li16(reg0: u8, imm1: u16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RH(0x49, reg0, imm1)) }
}
/// Load immediate (32b)
pub fn li32(reg0: u8, imm1: u32) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RW(0x4A, reg0, imm1)) }
}
/// Load immediate (64b)
pub fn li64(reg0: u8, imm1: u64) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RD(0x4B, reg0, imm1)) }
}
/// Load relative address
pub fn lra(reg0: u8, reg1: u8, offset2: i32) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRO(0x4C, reg0, reg1, offset2)) }
}
/// Load from absolute address
pub fn ld(reg0: u8, reg1: u8, addr2: u64, imm3: u16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRAH(0x4D, reg0, reg1, addr2, imm3)) }
}
/// Store to absolute address
pub fn st(reg0: u8, reg1: u8, addr2: u64, imm3: u16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRAH(0x4E, reg0, reg1, addr2, imm3)) }
}
/// Load from relative address
pub fn ldr(reg0: u8, reg1: u8, offset2: i32, imm3: u16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RROH(0x4F, reg0, reg1, offset2, imm3)) }
}
/// Store to relative address
pub fn str(reg0: u8, reg1: u8, offset2: i32, imm3: u16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RROH(0x50, reg0, reg1, offset2, imm3)) }
}
/// Copy block of memory
pub fn bmc(reg0: u8, reg1: u8, imm2: u16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRH(0x51, reg0, reg1, imm2)) }
}
/// Copy register block
pub fn brc(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x52, reg0, reg1, imm2)) }
}
/// Relative jump
pub fn jmp(offset0: i32) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(O(0x53, offset0)) }
}
/// Linking relative jump
pub fn jal(reg0: u8, reg1: u8, offset2: i32) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRO(0x54, reg0, reg1, offset2)) }
}
/// Linking absolute jump
pub fn jala(reg0: u8, reg1: u8, addr2: u64) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRA(0x55, reg0, reg1, addr2)) }
}
/// Branch on equal
pub fn jeq(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRP(0x56, reg0, reg1, offset2)) }
}
/// Branch on nonequal
pub fn jne(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRP(0x57, reg0, reg1, offset2)) }
}
/// Branch on lesser-than (unsigned)
pub fn jltu(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRP(0x58, reg0, reg1, offset2)) }
}
/// Branch on greater-than (unsigned)
pub fn jgtu(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRP(0x59, reg0, reg1, offset2)) }
}
/// Branch on lesser-than (signed)
pub fn jlts(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRP(0x5A, reg0, reg1, offset2)) }
}
/// Branch on greater-than (signed)
pub fn jgts(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRP(0x5B, reg0, reg1, offset2)) }
}
/// Environment call trap
pub fn eca() -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(N(0x5C, )) }
}
/// Environment breakpoint
pub fn ebp() -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(N(0x5D, )) }
}
/// Floating point addition (32b)
pub fn fadd32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x5E, reg0, reg1, reg2)) }
}
/// Floating point addition (64b)
pub fn fadd64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x5F, reg0, reg1, reg2)) }
}
/// Floating point subtraction (32b)
pub fn fsub32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x60, reg0, reg1, reg2)) }
}
/// Floating point subtraction (64b)
pub fn fsub64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x61, reg0, reg1, reg2)) }
}
/// Floating point multiply (32b)
pub fn fmul32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x62, reg0, reg1, reg2)) }
}
/// Floating point multiply (64b)
pub fn fmul64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x63, reg0, reg1, reg2)) }
}
/// Floating point division (32b)
pub fn fdiv32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x64, reg0, reg1, reg2)) }
}
/// Floating point division (64b)
pub fn fdiv64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x65, reg0, reg1, reg2)) }
}
/// Float fused multiply-add (32b)
pub fn fma32(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRRR(0x66, reg0, reg1, reg2, reg3)) }
}
/// Float fused multiply-add (64b)
pub fn fma64(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRRR(0x67, reg0, reg1, reg2, reg3)) }
}
/// Float reciprocal (32b)
pub fn finv32(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RR(0x68, reg0, reg1)) }
}
/// Float reciprocal (64b)
pub fn finv64(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RR(0x69, reg0, reg1)) }
}
/// Flaot compare less than (32b)
pub fn fcmplt32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x6A, reg0, reg1, reg2)) }
}
/// Flaot compare less than (64b)
pub fn fcmplt64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x6B, reg0, reg1, reg2)) }
}
/// Flaot compare greater than (32b)
pub fn fcmpgt32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x6C, reg0, reg1, reg2)) }
}
/// Flaot compare greater than (64b)
pub fn fcmpgt64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRR(0x6D, reg0, reg1, reg2)) }
}
/// Int to 32 bit float
pub fn itf32(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RR(0x6E, reg0, reg1)) }
}
/// Int to 64 bit float
pub fn itf64(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RR(0x6F, reg0, reg1)) }
}
/// Float 32 to int
pub fn fti32(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x70, reg0, reg1, imm2)) }
}
/// Float 64 to int
pub fn fti64(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x71, reg0, reg1, imm2)) }
}
/// Float 64 to Float 32
pub fn fc32t64(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RR(0x72, reg0, reg1)) }
}
/// Float 32 to Float 64
pub fn fc64t32(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRB(0x73, reg0, reg1, imm2)) }
}
/// Load relative immediate (16 bit)
pub fn lra16(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRP(0x74, reg0, reg1, offset2)) }
}
/// Load from relative address (16 bit)
pub fn ldr16(reg0: u8, reg1: u8, offset2: i16, imm3: u16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRPH(0x75, reg0, reg1, offset2, imm3)) }
}
/// Store to relative address (16 bit)
pub fn str16(reg0: u8, reg1: u8, offset2: i16, imm3: u16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(RRPH(0x76, reg0, reg1, offset2, imm3)) }
}
/// Relative jump (16 bit)
pub fn jmp16(offset0: i16) -> (usize, [u8; MAX_SIZE]) {
unsafe { crate::encode(P(0x77, offset0)) }
}
#[repr(packed)] pub struct N(u8, );
#[repr(packed)] pub struct RRR(u8, u8, u8, u8);
#[repr(packed)] pub struct RRRR(u8, u8, u8, u8, u8);
#[repr(packed)] pub struct RR(u8, u8, u8);
#[repr(packed)] pub struct RRB(u8, u8, u8, u8);
#[repr(packed)] pub struct RRH(u8, u8, u8, u16);
#[repr(packed)] pub struct RRW(u8, u8, u8, u32);
#[repr(packed)] pub struct RRD(u8, u8, u8, u64);
#[repr(packed)] pub struct RB(u8, u8, u8);
#[repr(packed)] pub struct RH(u8, u8, u16);
#[repr(packed)] pub struct RW(u8, u8, u32);
#[repr(packed)] pub struct RD(u8, u8, u64);
#[repr(packed)] pub struct RRO(u8, u8, u8, i32);
#[repr(packed)] pub struct RRAH(u8, u8, u8, u64, u16);
#[repr(packed)] pub struct RROH(u8, u8, u8, i32, u16);
#[repr(packed)] pub struct O(u8, i32);
#[repr(packed)] pub struct RRA(u8, u8, u8, u64);
#[repr(packed)] pub struct RRP(u8, u8, u8, i16);
#[repr(packed)] pub struct RRPH(u8, u8, u8, i16, u16);
#[repr(packed)] pub struct P(u8, i16);
pub const COUNT: u8 = 120;
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(u8)]
pub enum Instr {
UN = 0x00,
TX = 0x01,
NOP = 0x02,
ADD8 = 0x03,
ADD16 = 0x04,
ADD32 = 0x05,
ADD64 = 0x06,
SUB8 = 0x07,
SUB16 = 0x08,
SUB32 = 0x09,
SUB64 = 0x0A,
MUL8 = 0x0B,
MUL16 = 0x0C,
MUL32 = 0x0D,
MUL64 = 0x0E,
AND = 0x0F,
OR = 0x10,
XOR = 0x11,
SLU8 = 0x12,
SLU16 = 0x13,
SLU32 = 0x14,
SLU64 = 0x15,
SRU8 = 0x16,
SRU16 = 0x17,
SRU32 = 0x18,
SRU64 = 0x19,
SRS8 = 0x1A,
SRS16 = 0x1B,
SRS32 = 0x1C,
SRS64 = 0x1D,
CMPU = 0x1E,
CMPS = 0x1F,
DIRU8 = 0x20,
DIRU16 = 0x21,
DIRU32 = 0x22,
DIRU64 = 0x23,
DIRS8 = 0x24,
DIRS16 = 0x25,
DIRS32 = 0x26,
DIRS64 = 0x27,
NEG = 0x28,
NOT = 0x29,
SXT8 = 0x2A,
SXT16 = 0x2B,
SXT32 = 0x2C,
ADDI8 = 0x2D,
ADDI16 = 0x2E,
ADDI32 = 0x2F,
ADDI64 = 0x30,
MULI8 = 0x31,
MULI16 = 0x32,
MULI32 = 0x33,
MULI64 = 0x34,
ANDI = 0x35,
ORI = 0x36,
XORI = 0x37,
SLUI8 = 0x38,
SLUI16 = 0x39,
SLUI32 = 0x3A,
SLUI64 = 0x3B,
SRUI8 = 0x3C,
SRUI16 = 0x3D,
SRUI32 = 0x3E,
SRUI64 = 0x3F,
SRSI8 = 0x40,
SRSI16 = 0x41,
SRSI32 = 0x42,
SRSI64 = 0x43,
CMPUI = 0x44,
CMPSI = 0x45,
CP = 0x46,
SWA = 0x47,
LI8 = 0x48,
LI16 = 0x49,
LI32 = 0x4A,
LI64 = 0x4B,
LRA = 0x4C,
LD = 0x4D,
ST = 0x4E,
LDR = 0x4F,
STR = 0x50,
BMC = 0x51,
BRC = 0x52,
JMP = 0x53,
JAL = 0x54,
JALA = 0x55,
JEQ = 0x56,
JNE = 0x57,
JLTU = 0x58,
JGTU = 0x59,
JLTS = 0x5A,
JGTS = 0x5B,
ECA = 0x5C,
EBP = 0x5D,
FADD32 = 0x5E,
FADD64 = 0x5F,
FSUB32 = 0x60,
FSUB64 = 0x61,
FMUL32 = 0x62,
FMUL64 = 0x63,
FDIV32 = 0x64,
FDIV64 = 0x65,
FMA32 = 0x66,
FMA64 = 0x67,
FINV32 = 0x68,
FINV64 = 0x69,
FCMPLT32 = 0x6A,
FCMPLT64 = 0x6B,
FCMPGT32 = 0x6C,
FCMPGT64 = 0x6D,
ITF32 = 0x6E,
ITF64 = 0x6F,
FTI32 = 0x70,
FTI64 = 0x71,
FC32T64 = 0x72,
FC64T32 = 0x73,
LRA16 = 0x74,
LDR16 = 0x75,
STR16 = 0x76,
JMP16 = 0x77,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Oper {
R(u8),
B(u8),
H(u16),
W(u32),
D(u64),
O(i32),
A(u64),
P(i16),
}
/// This assumes the instruction byte is still at the beginning of the buffer
#[cfg(feature = "disasm")]
pub fn parse_args(bytes: &mut &[u8], kind: Instr, buf: &mut alloc::vec::Vec<Oper>) -> Option<()> {
match kind {
| Instr::TX
| Instr::NOP
| Instr::UN
| Instr::ECA
| Instr::EBP => {
crate::decode::<N>(bytes)?;
}
| Instr::JMP => {
let data = crate::decode::<O>(bytes)?;
buf.extend([Oper::O(data.1)]);
}
| Instr::JMP16 => {
let data = crate::decode::<P>(bytes)?;
buf.extend([Oper::P(data.1)]);
}
| Instr::LI8 => {
let data = crate::decode::<RB>(bytes)?;
buf.extend([Oper::R(data.1), Oper::B(data.2)]);
}
| Instr::LI64 => {
let data = crate::decode::<RD>(bytes)?;
buf.extend([Oper::R(data.1), Oper::D(data.2)]);
}
| Instr::LI16 => {
let data = crate::decode::<RH>(bytes)?;
buf.extend([Oper::R(data.1), Oper::H(data.2)]);
}
| Instr::NEG
| Instr::NOT
| Instr::SXT8
| Instr::SXT16
| Instr::SXT32
| Instr::CP
| Instr::SWA
| Instr::FINV32
| Instr::FINV64
| Instr::ITF32
| Instr::ITF64
| Instr::FC32T64 => {
let data = crate::decode::<RR>(bytes)?;
buf.extend([Oper::R(data.1), Oper::R(data.2)]);
}
| Instr::JALA => {
let data = crate::decode::<RRA>(bytes)?;
buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::A(data.3)]);
}
| Instr::LD
| Instr::ST => {
let data = crate::decode::<RRAH>(bytes)?;
buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::A(data.3), Oper::H(data.4)]);
}
| Instr::SRSI8
| Instr::ADDI8
| Instr::MULI8
| Instr::SLUI8
| Instr::SLUI16
| Instr::SLUI32
| Instr::SLUI64
| Instr::SRUI8
| Instr::SRUI16
| Instr::SRUI32
| Instr::SRUI64
| Instr::SRSI16
| Instr::SRSI32
| Instr::BRC
| Instr::FTI32
| Instr::FTI64
| Instr::FC64T32
| Instr::SRSI64 => {
let data = crate::decode::<RRB>(bytes)?;
buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::B(data.3)]);
}
| Instr::ADDI64
| Instr::CMPUI
| Instr::CMPSI
| Instr::MULI64
| Instr::ANDI
| Instr::ORI
| Instr::XORI => {
let data = crate::decode::<RRD>(bytes)?;
buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::D(data.3)]);
}
| Instr::ADDI16
| Instr::MULI16
| Instr::BMC => {
let data = crate::decode::<RRH>(bytes)?;
buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::H(data.3)]);
}
| Instr::LRA
| Instr::JAL => {
let data = crate::decode::<RRO>(bytes)?;
buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::O(data.3)]);
}
| Instr::LDR
| Instr::STR => {
let data = crate::decode::<RROH>(bytes)?;
buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::O(data.3), Oper::H(data.4)]);
}
| Instr::JEQ
| Instr::JNE
| Instr::JLTU
| Instr::JGTU
| Instr::JLTS
| Instr::JGTS
| Instr::LRA16 => {
let data = crate::decode::<RRP>(bytes)?;
buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::P(data.3)]);
}
| Instr::STR16
| Instr::LDR16 => {
let data = crate::decode::<RRPH>(bytes)?;
buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::P(data.3), Oper::H(data.4)]);
}
| Instr::SUB32
| Instr::CMPU
| Instr::CMPS
| Instr::SRS8
| Instr::SUB64
| Instr::MUL8
| Instr::MUL16
| Instr::MUL32
| Instr::MUL64
| Instr::AND
| Instr::OR
| Instr::XOR
| Instr::ADD8
| Instr::ADD16
| Instr::SRS16
| Instr::SLU8
| Instr::SLU16
| Instr::FADD32
| Instr::FADD64
| Instr::FSUB32
| Instr::FSUB64
| Instr::FMUL32
| Instr::FMUL64
| Instr::FDIV32
| Instr::FDIV64
| Instr::SLU32
| Instr::SLU64
| Instr::FCMPLT32
| Instr::FCMPLT64
| Instr::FCMPGT32
| Instr::FCMPGT64
| Instr::SRU8
| Instr::SRU16
| Instr::ADD64
| Instr::SUB8
| Instr::SRU32
| Instr::SUB16
| Instr::SRS32
| Instr::SRS64
| Instr::SRU64
| Instr::ADD32 => {
let data = crate::decode::<RRR>(bytes)?;
buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::R(data.3)]);
}
| Instr::DIRU8
| Instr::DIRS16
| Instr::DIRS32
| Instr::DIRS64
| Instr::DIRU16
| Instr::DIRU32
| Instr::FMA32
| Instr::FMA64
| Instr::DIRU64
| Instr::DIRS8 => {
let data = crate::decode::<RRRR>(bytes)?;
buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::R(data.3), Oper::R(data.4)]);
}
| Instr::MULI32
| Instr::ADDI32 => {
let data = crate::decode::<RRW>(bytes)?;
buf.extend([Oper::R(data.1), Oper::R(data.2), Oper::W(data.3)]);
}
| Instr::LI32 => {
let data = crate::decode::<RW>(bytes)?;
buf.extend([Oper::R(data.1), Oper::W(data.2)]);
}
}
Some(())
}

View file

@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
axum = "0.7.7"
getrandom = "0.2.15"
htmlm = "0.3.0"
htmlm = "0.5.0"
log = "0.4.22"
rusqlite = "0.32.1"
serde = { version = "1.0.210", features = ["derive"] }

View file

@ -1,12 +1,13 @@
use {
axum::{
body::Bytes,
http::{header::COOKIE, request::Parts},
response::{AppendHeaders, Html},
},
core::fmt,
htmlm::{html, write_html},
serde::{Deserialize, Serialize},
std::net::Ipv4Addr,
std::{fmt::Write, net::Ipv4Addr},
};
const MAX_NAME_LENGTH: usize = 32;
@ -16,6 +17,17 @@ const SESSION_DURATION_SECS: u64 = 60 * 60;
type Redirect<const COUNT: usize = 1> = AppendHeaders<[(&'static str, &'static str); COUNT]>;
macro_rules! static_asset {
($mime:literal, $body:literal) => {
get(|| async {
axum::http::Response::builder()
.header("content-type", $mime)
.body(axum::body::Body::from(Bytes::from_static(include_bytes!($body))))
.unwrap()
})
};
}
async fn amain() {
use axum::routing::{delete, get, post};
@ -30,15 +42,10 @@ async fn amain() {
.route("/", get(Index::page))
.route(
"/hbfmt.wasm",
get(|| async move {
axum::http::Response::builder()
.header("content-type", "application/wasm")
.body(axum::body::Body::from(
include_bytes!("../../target/wasm32-unknown-unknown/small/wasm_hbfmt.wasm")
.to_vec(),
))
.unwrap()
}),
static_asset!(
"application/wasm",
"../../target/wasm32-unknown-unknown/small/wasm_hbfmt.wasm"
),
)
.route("/index-view", get(Index::get))
.route("/feed", get(Index::page))
@ -71,19 +78,31 @@ async fn amain() {
}
trait PublicPage: Default {
fn render(self) -> String;
fn render_to_buf(self, buf: &mut String);
fn render(self) -> String {
let mut str = String::new();
Self::default().render_to_buf(&mut str);
str
}
async fn get() -> Html<String> {
Html(Self::default().render())
}
async fn page(session: Option<Session>) -> Html<String> {
base(Self::default().render(), session).await
base(|s| Self::default().render_to_buf(s), session.as_ref()).await
}
}
trait Page: Default {
fn render(self, session: &Session) -> String;
fn render_to_buf(self, session: &Session, buf: &mut String);
fn render(self, session: &Session) -> String {
let mut str = String::new();
Self::default().render_to_buf(session, &mut str);
str
}
async fn get(session: Session) -> Html<String> {
Html(Self::default().render(&session))
@ -91,7 +110,9 @@ trait Page: Default {
async fn page(session: Option<Session>) -> Result<Html<String>, axum::response::Redirect> {
match session {
Some(session) => Ok(base(Self::default().render(&session), Some(session)).await),
Some(session) => {
Ok(base(|f| Self::default().render_to_buf(&session, f), Some(&session)).await)
}
None => Err(axum::response::Redirect::permanent("/login")),
}
}
@ -101,8 +122,8 @@ trait Page: Default {
struct Index;
impl PublicPage for Index {
fn render(self) -> String {
include_str!("welcome-page.html").to_string()
fn render_to_buf(self, buf: &mut String) {
buf.push_str(include_str!("welcome-page.html"));
}
}
@ -124,9 +145,9 @@ struct Post {
}
impl Page for Post {
fn render(self, session: &Session) -> String {
fn render_to_buf(self, session: &Session, buf: &mut String) {
let Self { name, code, error, .. } = self;
html! {
write_html! { (buf)
<form id="postForm" "hx-post"="/post" "hx-swap"="outherHTML">
if let Some(e) = error { <div class="error">e</div> }
<input name="author" type="text" value={session.name} hidden>
@ -197,7 +218,7 @@ impl fmt::Display for Post {
struct Profile;
impl Page for Profile {
fn render(self, session: &Session) -> String {
fn render_to_buf(self, session: &Session, buf: &mut String) {
db::with(|db| {
let iter = db
.get_user_posts
@ -214,7 +235,7 @@ impl Page for Profile {
.into_iter()
.flatten()
.filter_map(|p| p.inspect_err(|e| log::error!("{e}")).ok());
html! {
write_html! { (buf)
for post in iter {
!{post}
} else {
@ -235,9 +256,9 @@ struct Login {
}
impl PublicPage for Login {
fn render(self) -> String {
fn render_to_buf(self, buf: &mut String) {
let Login { name, password, error } = self;
html! {
write_html! { (buf)
<form "hx-post"="/login" "hx-swap"="outherHTML">
if let Some(e) = error { <div class="error">e</div> }
<input name="name" type="text" autocomplete="name" placeholder="name" value=name
@ -306,9 +327,9 @@ struct Signup {
}
impl PublicPage for Signup {
fn render(self) -> String {
fn render_to_buf(self, buf: &mut String) {
let Signup { name, new_password, confirm_password, error } = self;
html! {
write_html! { (buf)
<form "hx-post"="/signup" "hx-swap"="outherHTML">
if let Some(e) = error { <div class="error">e</div> }
<input name="name" type="text" autocomplete="name" placeholder="name" value=name
@ -349,11 +370,11 @@ impl Signup {
}
}
async fn base(body: String, session: Option<Session>) -> Html<String> {
let username = session.map(|s| s.name);
async fn base(body: impl FnOnce(&mut String), session: Option<&Session>) -> Html<String> {
let username = session.map(|s| &s.name);
Html(htmlm::html! {
"<!DOCTIPE>"
Html(html! {
"<!DOCTYPE html>"
<html lang="en">
<head>
<style>!{include_str!("index.css")}</style>
@ -377,7 +398,7 @@ async fn base(body: String, session: Option<Session>) -> Html<String> {
</section>
</nav>
<section id="post-form"></section>
<main>!{body}</main>
<main>|f|{body(f)}</main>
</body>
<script src="https://unpkg.com/htmx.org@2.0.3" integrity="sha384-0895/pl2MU10Hqc6jd4RvrthNlDiE9U1tWmX7WRESftEDRosgxNsQG/Ze9YMRzHq" crossorigin="anonymous"></script>
<script>!{include_str!("index.js")}</script>

View file

@ -7,4 +7,4 @@ edition = "2021"
crate-type = ["cdylib"]
[dependencies]
hblang = { version = "0.1.0", path = "../../hblang", default-features = false, features = ["no_log"] }
hblang = { workspace = true, features = ["no_log"] }

View file

@ -120,7 +120,8 @@ unsafe extern "C" fn fmt() {
let arena =
hblang::parser::Arena::with_capacity(code.len() * hblang::parser::SOURCE_TO_AST_FACTOR);
let mut ctx = ParserCtx::default();
let exprs = hblang::parser::Parser::parse(&mut ctx, code, "source.hb", &|_, _| Ok(0), &arena);
let exprs =
hblang::parser::Parser::parse(&mut ctx, code, "source.hb", &mut |_, _| Ok(0), &arena);
let mut f = Write(&mut OUTPUT[..]);
hblang::fmt::fmt_file(exprs, code, &mut f).unwrap();

View file

@ -1,5 +1,5 @@
[package]
name = "hbjit"
name = "wasm-hbc"
version = "0.1.0"
edition = "2021"

View file

@ -0,0 +1,14 @@
pub fn add(left: u64, right: u64) -> u64 {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}

View file

@ -1,3 +0,0 @@
fn main() {
println!("Hello, world!");
}

View file

@ -9,8 +9,8 @@ path = "src/main.rs"
[dependencies]
hashbrown = { version = "0.15.0", default-features = false, features = ["raw-entry"] }
hbbytecode = { version = "0.1.0", path = "../hbbytecode" }
hbvm = { path = "../hbvm", features = ["nightly"] }
hbbytecode = { workspace = true, features = ["disasm"] }
hbvm = { workspace = true, features = ["nightly"] }
log = { version = "0.4.22", features = ["release_max_level_error"] }
[dependencies.regalloc2]

View file

@ -449,7 +449,7 @@ pub mod test {
let len = crate::fmt::minify(&mut minned);
minned.truncate(len);
let ast = parser::Ast::new(ident, minned, &mut ParserCtx::default(), &|_, _| Ok(0));
let ast = parser::Ast::new(ident, minned, &mut ParserCtx::default(), &mut |_, _| Ok(0));
log::error!(
"{} / {} = {} | {} / {} = {}",
ast.mem.size(),

View file

@ -188,13 +188,14 @@ impl<T> TaskQueueInner<T> {
}
pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result<Vec<Ast>> {
fn resolve(path: &str, from: &str) -> Result<PathBuf, CantLoadFile> {
let path = match Path::new(from).parent() {
Some(parent) => PathBuf::from_iter([parent, Path::new(path)]),
None => PathBuf::from(path),
fn resolve(path: &str, from: &str, tmp: &mut PathBuf) -> Result<PathBuf, CantLoadFile> {
tmp.clear();
match Path::new(from).parent() {
Some(parent) => tmp.extend([parent, Path::new(path)]),
None => tmp.push(path),
};
path.canonicalize().map_err(|source| CantLoadFile { path, source })
tmp.canonicalize().map_err(|source| CantLoadFile { path: std::mem::take(tmp), source })
}
#[derive(Debug)]
@ -227,7 +228,7 @@ pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result<Vec<Ast>> {
let tasks = TaskQueue::<Task>::new(extra_threads + 1);
let ast = Mutex::new(Vec::<io::Result<Ast>>::new());
let loader = |path: &str, from: &str| {
let loader = |path: &str, from: &str, tmp: &mut _| {
if path.starts_with("rel:") {
return Err(io::Error::new(
io::ErrorKind::Other,
@ -236,17 +237,17 @@ pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result<Vec<Ast>> {
));
}
let physiscal_path = resolve(path, from)?;
let mut physiscal_path = resolve(path, from, tmp)?;
let id = {
let mut seen = seen.lock().unwrap();
let len = seen.len();
match seen.entry(physiscal_path.clone()) {
match seen.entry(physiscal_path) {
hash_map::Entry::Occupied(entry) => {
return Ok(*entry.get());
}
hash_map::Entry::Vacant(entry) => {
entry.insert(len as _);
physiscal_path = entry.insert_entry(len as _).key().clone();
len as u32
}
}
@ -263,22 +264,23 @@ pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result<Vec<Ast>> {
Ok(id)
};
let execute_task = |ctx: &mut _, (_, path): Task| {
let execute_task = |ctx: &mut _, (_, path): Task, tmp: &mut _| {
let path = path.to_str().ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("path contains invalid characters: {}", display_rel_path(&path)),
)
})?;
Ok(Ast::new(path, std::fs::read_to_string(path)?, ctx, &|path, from| {
loader(path, from).map_err(|e| e.to_string())
Ok(Ast::new(path, std::fs::read_to_string(path)?, ctx, &mut |path, from| {
loader(path, from, tmp).map_err(|e| e.to_string())
}))
};
let thread = || {
let mut ctx = ParserCtx::default();
let mut tmp = PathBuf::new();
while let Some(task @ (indx, ..)) = tasks.pop() {
let res = execute_task(&mut ctx, task);
let res = execute_task(&mut ctx, task, &mut tmp);
let mut ast = ast.lock().unwrap();
let len = ast.len().max(indx as usize + 1);
ast.resize_with(len, || Err(io::ErrorKind::InvalidData.into()));
@ -286,7 +288,9 @@ pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result<Vec<Ast>> {
}
};
let path = Path::new(root).canonicalize()?;
let path = Path::new(root).canonicalize().map_err(|e| {
io::Error::new(e.kind(), format!("can't canonicalize root file path ({root})"))
})?;
seen.lock().unwrap().insert(path.clone(), 0);
tasks.push((0, path));

View file

@ -1348,7 +1348,7 @@ fn test_parse_files(ident: &'static str, input: &'static str) -> Vec<parser::Ast
fmt::test::format(ident, input[last_start..].trim());
module_map.push((last_module_name, input[last_start..].trim()));
let loader = |path: &str, _: &str| {
let mut loader = |path: &str, _: &str| {
module_map
.iter()
.position(|&(name, _)| name == path)
@ -1359,7 +1359,7 @@ fn test_parse_files(ident: &'static str, input: &'static str) -> Vec<parser::Ast
let mut ctx = parser::ParserCtx::default();
module_map
.iter()
.map(|&(path, content)| parser::Ast::new(path, content.to_owned(), &mut ctx, &loader))
.map(|&(path, content)| parser::Ast::new(path, content.to_owned(), &mut ctx, &mut loader))
.collect()
}

View file

@ -23,7 +23,7 @@ pub type Symbols = Vec<Symbol>;
pub type FileId = u32;
pub type IdentIndex = u16;
pub type LoaderError = String;
pub type Loader<'a> = &'a (dyn Fn(&str, &str) -> Result<FileId, LoaderError> + 'a);
pub type Loader<'a> = &'a mut (dyn FnMut(&str, &str) -> Result<FileId, LoaderError> + 'a);
pub const SOURCE_TO_AST_FACTOR: usize = 7 * (core::mem::size_of::<usize>() / 4) + 1;
@ -1068,7 +1068,7 @@ impl Ast {
impl Default for Ast {
fn default() -> Self {
Self(AstInner::new("".into(), "", &mut ParserCtx::default(), &no_loader))
Self(AstInner::new("".into(), "", &mut ParserCtx::default(), &mut no_loader))
}
}

View file

@ -9,4 +9,4 @@ alloc = []
nightly = []
[dependencies]
hbbytecode = { path = "../hbbytecode", default-features = false }
hbbytecode = { workspace = true }

Some files were not shown because too many files have changed in this diff Show more