removing garbage

This commit is contained in:
mlokr 2024-07-08 07:22:53 +02:00
parent 880cd66c66
commit e00f2f08c8
No known key found for this signature in database
GPG key ID: DEA147DDEE644993
37 changed files with 675 additions and 2329 deletions

435
Cargo.lock generated
View file

@ -2,35 +2,6 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "addr2line"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "ahash"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
dependencies = [
"cfg-if",
"const-random",
"getrandom",
"once_cell",
"version_check",
"zerocopy",
]
[[package]] [[package]]
name = "argh" name = "argh"
version = "0.1.12" version = "0.1.12"
@ -50,7 +21,7 @@ dependencies = [
"argh_shared", "argh_shared",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.63", "syn",
] ]
[[package]] [[package]]
@ -62,134 +33,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "autocfg"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]]
name = "backtrace"
version = "0.3.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "bitflags"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
[[package]]
name = "cc"
version = "1.0.97"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "099a5357d84c4c61eb35fc8eafa9a79a902c2f76911e5747ced4e032edd8d9b4"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "color-eyre"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55146f5e46f237f7423d74111267d4597b59b0dad0ffaf7303bce9945d843ad5"
dependencies = [
"backtrace",
"color-spantrace",
"eyre",
"indenter",
"once_cell",
"owo-colors",
"tracing-error",
]
[[package]]
name = "color-spantrace"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd6be1b2a7e382e2b98b43b2adcca6bb0e465af0bdd38123873ae61eb17a72c2"
dependencies = [
"once_cell",
"owo-colors",
"tracing-core",
"tracing-error",
]
[[package]]
name = "const-random"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87e00182fe74b066627d63b85fd550ac2998d4b0bd86bfed477a0ae4c7c71359"
dependencies = [
"const-random-macro",
]
[[package]]
name = "const-random-macro"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e"
dependencies = [
"getrandom",
"once_cell",
"tiny-keccak",
]
[[package]]
name = "crunchy"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "eyre"
version = "0.6.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec"
dependencies = [
"indenter",
"once_cell",
]
[[package]]
name = "getrandom"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "gimli"
version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
[[package]]
name = "hbasm"
version = "0.1.0"
dependencies = [
"paste",
"rhai",
"with_builtin_macros",
]
[[package]] [[package]]
name = "hbbytecode" name = "hbbytecode"
version = "0.1.0" version = "0.1.0"
@ -220,39 +63,12 @@ dependencies = [
"memmap2", "memmap2",
] ]
[[package]]
name = "indenter"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]]
name = "instant"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
dependencies = [
"cfg-if",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.154" version = "0.2.154"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"
[[package]]
name = "memchr"
version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
[[package]] [[package]]
name = "memmap2" name = "memmap2"
version = "0.9.4" version = "0.9.4"
@ -262,57 +78,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "miniz_oxide"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7"
dependencies = [
"adler",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "object"
version = "0.32.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.19.0" version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "owo-colors"
version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
[[package]]
name = "paste"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]]
name = "pin-project-lite"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.82" version = "1.0.82"
@ -331,40 +102,6 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rhai"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a7d88770120601ba1e548bb6bc2a05019e54ff01b51479e38e64ec3b59d4759"
dependencies = [
"ahash",
"bitflags",
"instant",
"num-traits",
"once_cell",
"rhai_codegen",
"smallvec",
"smartstring",
"thin-vec",
]
[[package]]
name = "rhai_codegen"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59aecf17969c04b9c0c5d21f6bc9da9fec9dd4980e64d1871443a476589d8c86"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.63",
]
[[package]]
name = "rustc-demangle"
version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.202" version = "1.0.202"
@ -382,50 +119,7 @@ checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.63", "syn",
]
[[package]]
name = "sharded-slab"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
dependencies = [
"lazy_static",
]
[[package]]
name = "smallvec"
version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "smartstring"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fb72c633efbaa2dd666986505016c32c3044395ceaf881518399d2f4127ee29"
dependencies = [
"autocfg",
"static_assertions",
"version_check",
]
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
] ]
[[package]] [[package]]
@ -439,141 +133,16 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "thin-vec"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a38c90d48152c236a3ab59271da4f4ae63d678c5d7ad6b7714d7cb9760be5e4b"
[[package]]
name = "thread_local"
version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c"
dependencies = [
"cfg-if",
"once_cell",
]
[[package]]
name = "tiny-keccak"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237"
dependencies = [
"crunchy",
]
[[package]]
name = "tracing"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
dependencies = [
"pin-project-lite",
"tracing-core",
]
[[package]]
name = "tracing-core"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
dependencies = [
"once_cell",
"valuable",
]
[[package]]
name = "tracing-error"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e"
dependencies = [
"tracing",
"tracing-subscriber",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b"
dependencies = [
"sharded-slab",
"thread_local",
"tracing-core",
]
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.12" version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "with_builtin_macros"
version = "0.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a59d55032495429b87f9d69954c6c8602e4d3f3e0a747a12dea6b0b23de685da"
dependencies = [
"with_builtin_macros-proc_macros",
]
[[package]]
name = "with_builtin_macros-proc_macros"
version = "0.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15bd7679c15e22924f53aee34d4e448c45b674feb6129689af88593e129f8f42"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]] [[package]]
name = "xtask" name = "xtask"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"argh", "argh",
"color-eyre",
"once_cell", "once_cell",
] ]
[[package]]
name = "zerocopy"
version = "0.7.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae87e3fcd617500e5d106f0380cf7b77f3c6092aae37191433159dda23cfb087"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15e934569e47891f7d9411f1a451d947a60e000ab3bd24fbb970f000387d1b3b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.63",
]

View file

@ -1,3 +1,3 @@
[workspace] [workspace]
resolver = "2" resolver = "2"
members = ["hbasm", "hbbytecode", "hbvm", "hbxrt", "xtask", "hblang", "hbjit"] members = ["hbbytecode", "hbvm", "hbxrt", "xtask", "hblang", "hbjit"]

View file

@ -1,9 +0,0 @@
[package]
name = "hbasm"
version = "0.1.0"
edition = "2021"
[dependencies]
paste = "1.0"
rhai = "1.16"
with_builtin_macros = "0.0.3"

View file

@ -1,13 +0,0 @@
import "hbasm/examples/ableos/std" as std;
fn main(){
std::Error(":+)");
std::Warn("Your mom fell in a well!");
std::Info("Hello, world!");
std::Debug("ABC");
std::Trace("Trace Deez");
tx();
}
main();

View file

@ -1,24 +0,0 @@
fn ipc_send(buffer_id, mem_addr, length){
// set the ecall
li8(r1, 3);
// Set the buffer ID to be the BufferID
li64(r2, buffer_id);
lra(r3, r0, mem_addr);
// set the length
li64(r4, length);
// ecall
eca();
}
private fn log(log_level, string){
let str = data::str(string);
ipc_send(1, str, str.len);
}
fn Error(string) {log(0, string);}
fn Warn(string) {log(1, string);}
fn Info(string) {log(2, string);}
// Due to rhai limitations this cannot be debug
// because of this all of the log levels are upper case
fn Debug(string) {log(3, string);}
fn Trace(string) {log(4, string);}

View file

@ -1,9 +0,0 @@
let hello = data::str("Hello, world!");
li8 (r1, 1); // Write syscall
li8 (r2, 1); // Stdout FD
lra16 (r3, r0, hello); // String buffer
li8 (r4, hello.len); // String length
eca (); // System call
tx (); // End program

View file

@ -1,33 +0,0 @@
li8(r1, 69);
li8(r2, 0);
if_eq(r1, r2,
|| puts("Equals!"),
|| puts("Not equals!"),
);
tx(); // END OF MAIN
/// Inline function write text to stdout
fn puts(string) {
let d = data::str(string);
li8 (r1, 1); // Write syscall
li8 (r2, 1); // Stdout handle
lra16 (r3, r0, d);
li64 (r4, d.len);
eca ();
}
fn if_eq(a, b, thenblk, elseblk) {
let elselbl = declabel();
let endlbl = declabel();
jne(a, b, elselbl);
thenblk.call();
jmp16(endlbl);
elselbl.here();
elseblk.call();
endlbl.here();
}

View file

@ -1,101 +0,0 @@
//! Data section inserts
use {
crate::{object::SymbolRef, SharedObject},
rhai::{CustomType, Engine, FuncRegistration, ImmutableString, Module},
};
/// Generate insertions for data types
///
/// `gen_data_instructions!($module, $obj, [$type, …]);`
/// - `$module`: Rhai module
/// - `$obj`: Code object
/// - `$type`: Type of single array item
macro_rules! gen_data_insertions {
($module:expr, $obj:expr, [$($ty:ident),* $(,)?] $(,)?) => {{
let (module, obj) = ($module, $obj);
$({
// Clone object to each function
let obj = ::std::rc::Rc::clone(obj);
FuncRegistration::new(stringify!($ty))
.with_namespace(rhai::FnNamespace::Global)
.set_into_module::<_, 1, false, _, true, _>(module, move |arr: ::rhai::Array| {
let obj = &mut *obj.borrow_mut();
let symbol = obj.symbol($crate::object::Section::Data);
// Reserve space for object so we don't resize it
// all the time
obj.sections
.data
.reserve(arr.len() * ::std::mem::size_of::<$ty>());
// For every item…
for item in arr {
// … try do conversions from i32 to desired type
// and insert it.
obj.sections.data.extend(
match item.as_int() {
Ok(num) => $ty::try_from(num).map_err(|_| "i64".to_owned()),
Err(ty) => Err(ty.to_owned()),
}
.map_err(|err| {
::rhai::EvalAltResult::ErrorMismatchDataType(
stringify!($ty).to_owned(),
err,
::rhai::Position::NONE,
)
})?
.to_le_bytes(),
);
}
Ok(DataRef {
symbol,
len: obj.sections.data.len() - symbol.0,
})
});
})*
}};
}
/// Reference to entry in data section
#[derive(Clone, Copy, Debug)]
pub struct DataRef {
pub symbol: SymbolRef,
pub len: usize,
}
impl CustomType for DataRef {
fn build(mut builder: rhai::TypeBuilder<Self>) {
builder
.with_name("DataRef")
.with_get("symbol", |this: &mut Self| this.symbol)
.with_get("len", |this: &mut Self| this.len as u64 as i64);
}
}
pub fn module(engine: &mut Engine, obj: SharedObject) -> Module {
let mut module = Module::new();
gen_data_insertions!(&mut module, &obj, [i8, i16, i32, i64]);
// Specialisation for strings, they should be
// inserted as plain UTF-8 arrays
FuncRegistration::new("str")
.with_namespace(rhai::FnNamespace::Global)
.set_into_module::<_, 1, false, _, true, _>(&mut module, move |s: ImmutableString| {
let obj = &mut *obj.borrow_mut();
let symbol = obj.symbol(crate::object::Section::Data);
obj.sections.data.extend(s.as_bytes());
Ok(DataRef {
symbol,
len: s.len(),
})
});
engine.build_type::<DataRef>();
module
}

View file

@ -1,321 +0,0 @@
//! Functions for inserting instructions
//!
//! Most of the code you see is just metaprogramming stuff.
//! This ensures that adding new instructions won't need any
//! specific changes and consistent behaviour.
//!
//! > I tried to comment stuff here, but I meanwhile forgor how it works.
//!
//! — Erin
use {
crate::object::Object,
rhai::{FuncRegistration, Module},
std::{cell::RefCell, rc::Rc},
};
/// Operand types and their insertions
pub mod optypes {
use {
crate::{
label::UnboundLabel,
object::{Object, RelocKey, RelocType, SymbolRef},
},
rhai::{Dynamic, EvalAltResult, ImmutableString, Position},
};
// These types represent operand types to be inserted
pub type R = u8;
pub type B = i8;
pub type H = i16;
pub type W = i32;
pub type D = i64;
pub type A = Dynamic;
pub type O = Dynamic;
pub type P = Dynamic;
/// Insert relocation into code
///
/// - If integer, just write it to the code
/// - Otherwise insert entry into relocation table
/// and fill zeroes
pub fn insert_reloc(
obj: &mut Object,
ty: RelocType,
val: &Dynamic,
) -> Result<(), EvalAltResult> {
match () {
// Direct references insert directly to table
_ if val.is::<SymbolRef>() => {
obj.relocation(RelocKey::Symbol(val.clone_cast::<SymbolRef>().0), ty)
}
_ if val.is::<UnboundLabel>() => {
obj.relocation(RelocKey::Symbol(val.clone_cast::<UnboundLabel>().0), ty)
}
_ if val.is::<DataRef>() => {
obj.relocation(RelocKey::Symbol(val.clone_cast::<DataRef>().symbol.0), ty)
}
// String (indirect) reference
_ if val.is_string() => {
obj.relocation(RelocKey::Label(val.clone_cast::<ImmutableString>()), ty)
}
// Manual offset
_ if val.is_int() => {
let int = val.clone_cast::<i64>();
match ty {
RelocType::Rel32 => obj.sections.text.extend((int as i32).to_le_bytes()),
RelocType::Rel16 => obj.sections.text.extend((int as i16).to_le_bytes()),
RelocType::Abs64 => obj.sections.text.extend(int.to_le_bytes()),
}
}
_ => {
return Err(EvalAltResult::ErrorMismatchDataType(
"SymbolRef, UnboundLabel, String or Int".to_owned(),
val.type_name().to_owned(),
Position::NONE,
))
}
}
Ok(())
}
/// Generate macro for inserting item into the output object
///
/// Pre-defines inserts for absolute address and relative offsets.
/// These are inserted with function [`insert_reloc`]
/// # le_bytes
/// `gen_insert!(le_bytes: [B, …]);`
///
/// Takes sequence of operand types which should be inserted
/// by invoking `to_le_bytes` method on it.
macro_rules! gen_insert {
(le_bytes: [$($lety:ident),* $(,)?]) => {
/// `insert!($thing, $obj, $type)` where
/// - `$thing`: Value you want to insert
/// - `$obj`: Code object
/// - `$type`: Type of inserted value
///
/// Eg. `insert!(69_u8, obj, B);`
macro_rules! insert {
$(($thing:expr, $obj: expr, $lety) => {
$obj.sections.text.extend($thing.to_le_bytes());
};)*
($thing:expr, $obj:expr, A) => {
$crate::ins::optypes::insert_reloc(
$obj,
$crate::object::RelocType::Abs64,
$thing
)?
};
($thing:expr, $obj:expr, O) => {
$crate::ins::optypes::insert_reloc(
$obj,
$crate::object::RelocType::Rel32,
$thing
)?
};
($thing:expr, $obj:expr, P) => {
$crate::ins::optypes::insert_reloc(
$obj,
$crate::object::RelocType::Rel16,
$thing
)?
};
}
};
}
gen_insert!(le_bytes: [R, B, H, W, D]);
#[allow(clippy::single_component_path_imports)]
pub(super) use insert;
use crate::data::DataRef;
}
/// Rhai Types (types for function parameters as Rhai uses only 64bit signed integers)
pub mod rity {
pub use super::optypes::{A, O, P, R};
pub type B = i64;
pub type H = i64;
pub type W = i64;
pub type D = i64;
}
/// Generic instruction (instruction of certain operands type) inserts
pub mod generic {
use {crate::object::Object, rhai::EvalAltResult};
pub(super) fn convert_op<A, B>(from: A) -> Result<B, EvalAltResult>
where
B: TryFrom<A>,
<B as TryFrom<A>>::Error: std::error::Error + Sync + Send + 'static,
{
B::try_from(from).map_err(|e| {
EvalAltResult::ErrorSystem("Data conversion error".to_owned(), Box::new(e))
})
}
/// Generate opcode-generic instruction insert macro
macro_rules! gen_ins {
($($($name:ident : $ty:ty),*;)*) => {
paste::paste! {
$(
/// Instruction-generic opcode insertion function
/// - `obj`: Code object
/// - `opcode`: opcode, not checked if valid for instruction type
/// - … for operands
#[inline]
pub fn [<$($ty:lower)*>](
obj: &mut Object,
opcode: u8,
$($name: $crate::ins::optypes::$ty),*,
) -> Result<(), EvalAltResult> {
// Push opcode
obj.sections.text.push(opcode);
// Insert based on type
$($crate::ins::optypes::insert!(&$name, obj, $ty);)*
Ok(())
}
)*
/// Generate Rhai opcode-specific instruction insertion functions
///
/// `gen_ins_fn!($obj, $opcode, $optype);` where:
/// - `$obj`: Code object
/// - `$opcode`: Opcode value
macro_rules! gen_ins_fn {
$(
($obj:expr, $opcode:expr, [<$($ty)*>]) => {
// Opcode-specific insertion function
// - Parameters = operands
move |$($name: $crate::ins::rity::$ty),*| {
// Invoke generic function
$crate::ins::generic::[<$($ty:lower)*>](
&mut *$obj.borrow_mut(),
$opcode,
$(
// Convert to desired type (from Rhai-provided values)
$crate::ins::generic::convert_op::<
_,
$crate::ins::optypes::$ty
>($name)?
),*
)?;
Ok(())
}
};
// Internal-use: count args
(@arg_count [<$($ty)*>]) => {
{ ["", $(stringify!($ty)),*].len() - 1 }
};
)*
// Specialisation for no-operand instructions
($obj:expr, $opcode:expr, N) => {
move || {
$crate::ins::generic::n(&mut *$obj.borrow_mut(), $opcode);
Ok(())
}
};
// Internal-use specialisation: no-operand instructions
(@arg_count N) => {
{ 0 }
};
}
}
};
}
/// Specialisation for no-operand instructions simply just push opcode
#[inline]
pub fn n(obj: &mut Object, opcode: u8) {
obj.sections.text.push(opcode);
}
// Generate opcode-generic instruction inserters
// (operand identifiers are arbitrary)
//
// New instruction types have to be added manually here
gen_ins! {
o0: R, o1: R;
o0: R, o1: R, o2: R;
o0: R, o1: R, o2: R, o3: R;
o0: R, o1: R, o2: B;
o0: R, o1: R, o2: H;
o0: R, o1: R, o2: W;
o0: R, o1: R, o2: D;
o0: R, o1: B;
o0: R, o1: H;
o0: R, o1: W;
o0: R, o1: D;
o0: R, o1: R, o2: A;
o0: R, o1: R, o2: A, o3: H;
o0: R, o1: R, o2: O, o3: H;
o0: R, o1: R, o2: P, o3: H;
o0: R, o1: R, o2: O;
o0: R, o1: R, o2: P;
o0: O;
o0: P;
}
#[allow(clippy::single_component_path_imports)]
pub(super) use gen_ins_fn;
}
/// Generate instructions from instruction table
///
/// ```ignore
/// instructions!(($module, $obj) {
/// // Data from instruction table
/// $opcode, $mnemonic, $opty, $doc;
/// …
/// });
/// ```
/// - `$module`: Rhai module
/// - `$obj`: Code object
macro_rules! instructions {
(
($module:expr, $obj:expr $(,)?)
{ $($opcode:expr, $mnemonic:ident, $ops:tt, $doc:literal;)* }
) => {{
paste::paste! {
let (module, obj) = ($module, $obj);
$({
// Object is shared across all functions
let obj = Rc::clone(&obj);
// Register newly generated function for each instruction
FuncRegistration::new(stringify!([<$mnemonic:lower>]))
.with_namespace(rhai::FnNamespace::Global)
.set_into_module::<_, { generic::gen_ins_fn!(@arg_count $ops) }, false, _, true, _>(
module,
generic::gen_ins_fn!(
obj,
$opcode,
$ops
)
);
})*
}
}};
}
/// Setup instruction insertors
pub fn setup(module: &mut Module, obj: Rc<RefCell<Object>>) {
// Import instructions table and use it for generation
with_builtin_macros::with_builtin! {
let $spec = include_from_root!("../hbbytecode/instructions.in") in {
instructions!((module, obj) { $spec });
}
}
}

View file

@ -1,112 +0,0 @@
//! Stuff related to labels
use {
crate::SharedObject,
rhai::{Engine, FuncRegistration, ImmutableString, Module},
};
/// Macro for creating functions for Rhai which
/// is bit more friendly
///
/// ```ignore
/// shdm_fns!{
/// module: $module;
/// shared: $shared => $shname;
///
/// $vis fn $name($param_name: $param_ty, …) -> $ret { … }
/// …
/// }
/// ```
/// - `$module`: Rhai module
/// - `$shared`: Data to be shared across the functions
/// - `$shname`: The binding name inside functions
/// - `$vis`: Function visibility for Rhai
/// - Lowercased [`rhai::FnNamespace`] variants
/// - `$name`: Function name
/// - `$param_name`: Parameter name
/// - `$param_ty`: Rust parameter type
/// - `$ret`: Optional return type (otherwise infer)
macro_rules! shdm_fns {
(
module: $module:expr;
shared: $shared:expr => $shname:ident;
$(
$vis:ident fn $name:ident($($param_name:ident: $param_ty:ty),*) $(-> $ret:ty)? $blk:block
)*
) => {{
let module = $module;
let shared = $shared;
paste::paste! {
$({
let $shname = SharedObject::clone(&shared);
FuncRegistration::new(stringify!($name))
.with_namespace(rhai::FnNamespace::[<$vis:camel>])
.set_into_module::<_, { ["", $(stringify!($param_name)),*].len() - 1 }, false, _, true, _>(
module,
move |$($param_name: $param_ty),*| $(-> $ret)? {
let mut $shname = $shname.borrow_mut();
$blk
}
);
})*
}
}};
}
/// Label without any place bound
#[derive(Clone, Copy, Debug)]
pub struct UnboundLabel(pub usize);
pub fn setup(engine: &mut Engine, module: &mut Module, object: SharedObject) {
shdm_fns! {
module: module;
shared: object => obj;
// Insert unnamed label
global fn label() {
let symbol = obj.symbol(crate::object::Section::Text);
Ok(symbol)
}
// Insert string-labeled label
global fn label(label: ImmutableString) {
let symbol = obj.symbol(crate::object::Section::Text);
obj.labels.insert(label, symbol.0);
Ok(symbol)
}
// Declare unbound label (to be bound later)
global fn declabel() {
let index = obj.symbols.len();
obj.symbols.push(None);
Ok(UnboundLabel(index))
}
// Declare unbound label (to be bound later)
// with string label
global fn declabel(label: ImmutableString) {
let index = obj.symbols.len();
obj.symbols.push(None);
obj.labels.insert(label, index);
Ok(UnboundLabel(index))
}
// Set location for unbound label
global fn here(label: UnboundLabel) {
obj.symbols[label.0] = Some(crate::object::SymbolEntry {
location: crate::object::Section::Text,
offset: obj.sections.text.len(),
});
Ok(())
}
}
engine.register_type_with_name::<UnboundLabel>("UnboundLabel");
}

View file

@ -1,45 +0,0 @@
pub mod data;
pub mod ins;
pub mod label;
pub mod linker;
pub mod object;
use {
object::Object,
rhai::{Engine, Module},
std::{cell::RefCell, rc::Rc},
};
type SharedObject = Rc<RefCell<Object>>;
pub fn assembler(
linkout: &mut impl std::io::Write,
loader: impl FnOnce(&mut Engine) -> Result<(), Box<rhai::EvalAltResult>>,
) -> Result<(), Box<dyn std::error::Error>> {
let mut engine = Engine::new();
let mut module = Module::new();
let obj = Rc::new(RefCell::new(Object::default()));
ins::setup(&mut module, Rc::clone(&obj));
label::setup(&mut engine, &mut module, Rc::clone(&obj));
// Registers
for n in 0_u8..=255 {
module.set_var(format!("r{n}"), n);
}
module.set_native_fn("reg", |n: i64| {
Ok(u8::try_from(n).map_err(|_| {
rhai::EvalAltResult::ErrorRuntime("Invalid register value".into(), rhai::Position::NONE)
})?)
});
module.set_native_fn("as_i64", |n: u8| Ok(n as i64));
let datamod = Rc::new(data::module(&mut engine, SharedObject::clone(&obj)));
engine.register_global_module(Rc::new(module));
engine.register_static_module("data", datamod);
engine.register_type_with_name::<object::SymbolRef>("SymbolRef");
loader(&mut engine)?;
linker::link(obj, linkout)?;
Ok(())
}

View file

@ -1,47 +0,0 @@
//! Simple flat-bytecode linker
use {
crate::{
object::{RelocKey, RelocType, Section},
SharedObject,
},
std::io::Write,
};
pub fn link(object: SharedObject, out: &mut impl Write) -> std::io::Result<()> {
let obj = &mut *object.borrow_mut();
// Walk relocation table entries
for (&loc, entry) in &obj.relocs {
let value = match &entry.key {
// Symbol direct reference
RelocKey::Symbol(sym) => obj.symbols[*sym],
// Label indirect label reference
RelocKey::Label(label) => obj.symbols[obj.labels[label]],
}
.ok_or_else(|| std::io::Error::other("Invalid symbol"))?;
let offset = match value.location {
// Text section is on the beginning
Section::Text => value.offset,
// Data section follows text section immediately
Section::Data => value.offset + obj.sections.text.len(),
};
// Insert address or calulate relative offset
match entry.ty {
RelocType::Rel32 => obj.sections.text[loc..loc + 4]
.copy_from_slice(&((offset as isize - loc as isize) as i32).to_le_bytes()),
RelocType::Rel16 => obj.sections.text[loc..loc + 2]
.copy_from_slice(&((offset as isize - loc as isize) as i16).to_le_bytes()),
RelocType::Abs64 => obj.sections.text[loc..loc + 8]
.copy_from_slice(&(offset as isize - loc as isize).to_le_bytes()),
}
}
// Write to output
out.write_all(&obj.sections.text)?;
out.write_all(&obj.sections.data)
}

View file

@ -1,8 +0,0 @@
use std::{io::stdout, path::PathBuf};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let path = PathBuf::from(std::env::args().nth(1).ok_or("Missing path")?);
hbasm::assembler(&mut stdout(), |engine| engine.run_file(path))?;
Ok(())
}

View file

@ -1,94 +0,0 @@
//! Code object
use {rhai::ImmutableString, std::collections::HashMap};
/// Section tabel
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Section {
Text,
Data,
}
/// Symbol entry (in what section, where)
#[derive(Clone, Copy, Debug)]
pub struct SymbolEntry {
pub location: Section,
pub offset: usize,
}
/// Relocation table key
#[derive(Clone, Debug)]
pub enum RelocKey {
/// Direct reference
Symbol(usize),
/// Indirect reference
Label(ImmutableString),
}
/// Relocation type
#[derive(Clone, Copy, Debug)]
pub enum RelocType {
Rel32,
Rel16,
Abs64,
}
/// Relocation table entry
#[derive(Clone, Debug)]
pub struct RelocEntry {
pub key: RelocKey,
pub ty: RelocType,
}
/// Object code
#[derive(Clone, Debug, Default)]
pub struct Sections {
pub text: Vec<u8>,
pub data: Vec<u8>,
}
/// Object
#[derive(Clone, Debug, Default)]
pub struct Object {
/// Vectors with sections
pub sections: Sections,
/// Symbol table
pub symbols: Vec<Option<SymbolEntry>>,
/// Labels to symbols table
pub labels: HashMap<ImmutableString, usize>,
/// Relocation table
pub relocs: HashMap<usize, RelocEntry>,
}
#[derive(Clone, Copy, Debug)]
#[repr(transparent)]
pub struct SymbolRef(pub usize);
impl Object {
/// Insert symbol at current location in specified section
pub fn symbol(&mut self, section: Section) -> SymbolRef {
let section_buf = match section {
Section::Text => &mut self.sections.text,
Section::Data => &mut self.sections.data,
};
self.symbols.push(Some(SymbolEntry {
location: section,
offset: section_buf.len(),
}));
SymbolRef(self.symbols.len() - 1)
}
/// Insert to relocation table and write zeroes to code
pub fn relocation(&mut self, key: RelocKey, ty: RelocType) {
self.relocs
.insert(self.sections.text.len(), RelocEntry { key, ty });
self.sections.text.extend(match ty {
RelocType::Rel32 => &[0_u8; 4] as &[u8],
RelocType::Rel16 => &[0; 2],
RelocType::Abs64 => &[0; 8],
});
}
}

View file

@ -28,16 +28,14 @@ unsafe impl BytecodeItem for u8 {}
pub enum RoundingMode { pub enum RoundingMode {
NearestEven = 0, NearestEven = 0,
Truncate = 1, Truncate = 1,
Up = 2, Up = 2,
Down = 3, Down = 3,
} }
impl TryFrom<u8> for RoundingMode { impl TryFrom<u8> for RoundingMode {
type Error = (); type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> { fn try_from(value: u8) -> Result<Self, Self::Error> {
(value <= 3) (value <= 3).then(|| unsafe { core::mem::transmute(value) }).ok_or(())
.then(|| unsafe { core::mem::transmute(value) })
.ok_or(())
} }
} }

View file

@ -7,10 +7,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut generated = String::new(); let mut generated = String::new();
writeln!( writeln!(generated, "#![allow(dead_code)] #![allow(clippy::upper_case_acronyms)]")?;
generated,
"#![allow(dead_code)] #![allow(clippy::upper_case_acronyms)]"
)?;
gen_max_size(&mut generated)?; gen_max_size(&mut generated)?;
gen_encodes(&mut generated)?; gen_encodes(&mut generated)?;
gen_structs(&mut generated)?; gen_structs(&mut generated)?;
@ -22,11 +19,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
fn gen_name_list(generated: &mut String) -> Result<(), Box<dyn std::error::Error>> { fn gen_name_list(generated: &mut String) -> Result<(), Box<dyn std::error::Error>> {
writeln!( writeln!(generated, "pub const NAMES: [&str; {}] = [", instructions().count())?;
generated,
"pub const NAMES: [&str; {}] = [",
instructions().count()
)?;
for [_, name, _, _] in instructions() { for [_, name, _, _] in instructions() {
writeln!(generated, " \"{}\",", name.to_lowercase())?; writeln!(generated, " \"{}\",", name.to_lowercase())?;
} }
@ -59,15 +52,9 @@ fn gen_encodes(generated: &mut String) -> Result<(), Box<dyn std::error::Error>>
let args = comma_sep( let args = comma_sep(
iter_args(ty).map(|(i, c)| format!("{}{i}: {}", arg_to_name(c), arg_to_type(c))), iter_args(ty).map(|(i, c)| format!("{}{i}: {}", arg_to_name(c), arg_to_type(c))),
); );
writeln!( writeln!(generated, "pub fn {name}({args}) -> (usize, [u8; MAX_SIZE]) {{")?;
generated,
"pub fn {name}({args}) -> (usize, [u8; MAX_SIZE]) {{"
)?;
let arg_names = comma_sep(iter_args(ty).map(|(i, c)| format!("{}{i}", arg_to_name(c)))); let arg_names = comma_sep(iter_args(ty).map(|(i, c)| format!("{}{i}", arg_to_name(c))));
writeln!( writeln!(generated, " unsafe {{ crate::encode({ty}({op}, {arg_names})) }}")?;
generated,
" unsafe {{ crate::encode({ty}({op}, {arg_names})) }}"
)?;
writeln!(generated, "}}")?; writeln!(generated, "}}")?;
} }
@ -88,10 +75,7 @@ fn gen_structs(generated: &mut String) -> Result<(), Box<dyn std::error::Error>>
} }
fn comma_sep(items: impl Iterator<Item = String>) -> String { fn comma_sep(items: impl Iterator<Item = String>) -> String {
items items.map(|item| item.to_string()).collect::<Vec<_>>().join(", ")
.map(|item| item.to_string())
.collect::<Vec<_>>()
.join(", ")
} }
fn instructions() -> impl Iterator<Item = [&'static str; 4]> { fn instructions() -> impl Iterator<Item = [&'static str; 4]> {

File diff suppressed because it is too large Load diff

View file

@ -13,9 +13,9 @@ const fn ascii_mask(chars: &[u8]) -> u128 {
#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct Token { pub struct Token {
pub kind: TokenKind, pub kind: TokenKind,
pub start: u32, pub start: u32,
pub end: u32, pub end: u32,
} }
impl Token { impl Token {
@ -82,31 +82,31 @@ macro_rules! gen_token_kind {
#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[repr(u8)] #[repr(u8)]
pub enum TokenKind { pub enum TokenKind {
Not = b'!', Not = b'!',
DQuote = b'"', DQuote = b'"',
Pound = b'#', Pound = b'#',
CtIdent = b'$', CtIdent = b'$',
Mod = b'%', Mod = b'%',
Band = b'&', Band = b'&',
Quote = b'\'', Quote = b'\'',
LParen = b'(', LParen = b'(',
RParen = b')', RParen = b')',
Mul = b'*', Mul = b'*',
Add = b'+', Add = b'+',
Comma = b',', Comma = b',',
Sub = b'-', Sub = b'-',
Dot = b'.', Dot = b'.',
Div = b'/', Div = b'/',
// Unused = 2-6 // Unused = 2-6
Shr = b'<' - 5, Shr = b'<' - 5,
// Unused = 8 // Unused = 8
Shl = b'>' - 5, Shl = b'>' - 5,
Colon = b':', Colon = b':',
Semi = b';', Semi = b';',
Lt = b'<', Lt = b'<',
Assign = b'=', Assign = b'=',
Gt = b'>', Gt = b'>',
Que = b'?', Que = b'?',
Directive = b'@', Directive = b'@',
Comment, Comment,
@ -132,37 +132,37 @@ pub enum TokenKind {
And, And,
// Unused = R-Z // Unused = R-Z
LBrack = b'[', LBrack = b'[',
BSlash = b'\\', BSlash = b'\\',
RBrack = b']', RBrack = b']',
Xor = b'^', Xor = b'^',
Tick = b'`', Tick = b'`',
// Unused = a-z // Unused = a-z
LBrace = b'{', LBrace = b'{',
Bor = b'|', Bor = b'|',
RBrace = b'}', RBrace = b'}',
Tilde = b'~', Tilde = b'~',
Decl = b':' + 128, Decl = b':' + 128,
Eq = b'=' + 128, Eq = b'=' + 128,
Ne = b'!' + 128, Ne = b'!' + 128,
Le = b'<' + 128, Le = b'<' + 128,
Ge = b'>' + 128, Ge = b'>' + 128,
BorAss = b'|' + 128, BorAss = b'|' + 128,
AddAss = b'+' + 128, AddAss = b'+' + 128,
SubAss = b'-' + 128, SubAss = b'-' + 128,
MulAss = b'*' + 128, MulAss = b'*' + 128,
DivAss = b'/' + 128, DivAss = b'/' + 128,
ModAss = b'%' + 128, ModAss = b'%' + 128,
XorAss = b'^' + 128, XorAss = b'^' + 128,
BandAss = b'&' + 128, BandAss = b'&' + 128,
ShlAss = b'0' + 128, ShlAss = b'0' + 128,
ShrAss = b'1' + 128, ShrAss = b'1' + 128,
} }
impl TokenKind { impl TokenKind {
pub fn assign_op(self) -> Option<Self> { pub fn ass_op(self) -> Option<Self> {
let id = (self as u8).saturating_sub(128); let id = (self as u8).saturating_sub(128);
if ascii_mask(b"|+-*/%^&01") & (1u128 << id) == 0 { if ascii_mask(b"|+-*/%^&01") & (1u128 << id) == 0 {
return None; return None;
@ -228,7 +228,7 @@ gen_token_kind! {
} }
pub struct Lexer<'a> { pub struct Lexer<'a> {
pos: u32, pos: u32,
bytes: &'a [u8], bytes: &'a [u8],
} }
@ -238,10 +238,7 @@ impl<'a> Lexer<'a> {
} }
pub fn restore(input: &'a str, pos: u32) -> Self { pub fn restore(input: &'a str, pos: u32) -> Self {
Self { Self { pos, bytes: input.as_bytes() }
pos,
bytes: input.as_bytes(),
}
} }
pub fn slice(&self, tok: std::ops::Range<usize>) -> &'a str { pub fn slice(&self, tok: std::ops::Range<usize>) -> &'a str {
@ -268,11 +265,7 @@ impl<'a> Lexer<'a> {
let mut start = self.pos; let mut start = self.pos;
let Some(c) = self.advance() else { let Some(c) = self.advance() else {
return Token { return Token { kind: T::Eof, start, end: self.pos };
kind: T::Eof,
start,
end: self.pos,
};
}; };
let advance_ident = |s: &mut Self| { let advance_ident = |s: &mut Self| {
@ -345,11 +338,7 @@ impl<'a> Lexer<'a> {
_ => identity(c), _ => identity(c),
}; };
return Token { return Token { kind, start, end: self.pos };
kind,
start,
end: self.pos,
};
} }
} }
@ -416,10 +405,8 @@ impl LineMap {
let query = std::simd::u8x16::splat(b'\n'); let query = std::simd::u8x16::splat(b'\n');
let nl_count = start.iter().map(|&b| (b == b'\n') as usize).sum::<usize>() let nl_count = start.iter().map(|&b| (b == b'\n') as usize).sum::<usize>()
+ simd_mid + simd_mid.iter().map(|s| s.simd_eq(query).to_bitmask().count_ones()).sum::<u32>()
.iter() as usize
.map(|s| s.simd_eq(query).to_bitmask().count_ones())
.sum::<u32>() as usize
+ end.iter().map(|&b| (b == b'\n') as usize).sum::<usize>(); + end.iter().map(|&b| (b == b'\n') as usize).sum::<usize>();
let mut lines = Vec::with_capacity(nl_count); let mut lines = Vec::with_capacity(nl_count);
@ -457,9 +444,7 @@ impl LineMap {
handle_rem(bytes.len() - end.len(), end, &mut last_nl, &mut lines); handle_rem(bytes.len() - end.len(), end, &mut last_nl, &mut lines);
Self { Self { lines: Box::from(lines) }
lines: Box::from(lines),
}
} }
} }

View file

@ -20,15 +20,16 @@
#![allow(internal_features)] #![allow(internal_features)]
#![allow(clippy::format_collect)] #![allow(clippy::format_collect)]
use std::{ use {
collections::VecDeque, parser::Ast,
io::{self, Read}, std::{
path::{Path, PathBuf}, collections::VecDeque,
sync::Mutex, io::{self, Read},
path::{Path, PathBuf},
sync::Mutex,
},
}; };
use parser::Ast;
#[macro_export] #[macro_export]
macro_rules! run_tests { macro_rules! run_tests {
($runner:path: $($name:ident => $input:expr;)*) => {$( ($runner:path: $($name:ident => $input:expr;)*) => {$(
@ -137,9 +138,7 @@ struct TaskQueue<T> {
impl<T> TaskQueue<T> { impl<T> TaskQueue<T> {
fn new(max_waiters: usize) -> Self { fn new(max_waiters: usize) -> Self {
Self { Self { inner: Mutex::new(TaskQueueInner::new(max_waiters)) }
inner: Mutex::new(TaskQueueInner::new(max_waiters)),
}
} }
pub fn push(&self, message: T) { pub fn push(&self, message: T) {
@ -163,8 +162,8 @@ enum TaskSlot<T> {
struct TaskQueueInner<T> { struct TaskQueueInner<T> {
max_waiters: usize, max_waiters: usize,
messages: VecDeque<T>, messages: VecDeque<T>,
parked: VecDeque<(*mut TaskSlot<T>, std::thread::Thread)>, parked: VecDeque<(*mut TaskSlot<T>, std::thread::Thread)>,
} }
unsafe impl<T: Send> Send for TaskQueueInner<T> {} unsafe impl<T: Send> Send for TaskQueueInner<T> {}
@ -172,11 +171,7 @@ unsafe impl<T: Send + Sync> Sync for TaskQueueInner<T> {}
impl<T> TaskQueueInner<T> { impl<T> TaskQueueInner<T> {
fn new(max_waiters: usize) -> Self { fn new(max_waiters: usize) -> Self {
Self { Self { max_waiters, messages: Default::default(), parked: Default::default() }
max_waiters,
messages: Default::default(),
parked: Default::default(),
}
} }
fn push(&mut self, messages: impl IntoIterator<Item = T>) { fn push(&mut self, messages: impl IntoIterator<Item = T>) {
@ -232,17 +227,9 @@ pub fn parse_from_fs(threads: usize, root: &str) -> io::Result<Vec<Ast>> {
} }
enum ImportPath<'a> { enum ImportPath<'a> {
Root { Root { path: &'a str },
path: &'a str, Rel { path: &'a str },
}, Git { link: &'a str, path: &'a str, chk: Option<Chk<'a>> },
Rel {
path: &'a str,
},
Git {
link: &'a str,
path: &'a str,
chk: Option<Chk<'a>>,
},
} }
impl<'a> TryFrom<&'a str> for ImportPath<'a> { impl<'a> TryFrom<&'a str> for ImportPath<'a> {
@ -258,15 +245,14 @@ pub fn parse_from_fs(threads: usize, root: &str) -> io::Result<Vec<Ast>> {
let (link, path) = let (link, path) =
path.split_once(':').ok_or(ParseImportError::ExpectedPath)?; path.split_once(':').ok_or(ParseImportError::ExpectedPath)?;
let (link, params) = link.split_once('?').unwrap_or((link, "")); let (link, params) = link.split_once('?').unwrap_or((link, ""));
let chk = params let chk = params.split('&').filter_map(|s| s.split_once('=')).find_map(
.split('&') |(key, value)| match key {
.filter_map(|s| s.split_once('='))
.find_map(|(key, value)| match key {
"branch" => Some(Chk::Branch(value)), "branch" => Some(Chk::Branch(value)),
"rev" => Some(Chk::Rev(value)), "rev" => Some(Chk::Rev(value)),
"tag" => Some(Chk::Tag(value)), "tag" => Some(Chk::Tag(value)),
_ => None, _ => None,
}); },
);
Ok(Self::Git { link, path, chk }) Ok(Self::Git { link, path, chk })
} }
_ => Err(ParseImportError::InvalidPrefix), _ => Err(ParseImportError::InvalidPrefix),
@ -296,8 +282,8 @@ pub fn parse_from_fs(threads: usize, root: &str) -> io::Result<Vec<Ast>> {
path.canonicalize().map_err(|e| CantLoadFile { path.canonicalize().map_err(|e| CantLoadFile {
file_name: path, file_name: path,
directory: PathBuf::from(root), directory: PathBuf::from(root),
from: PathBuf::from(from), from: PathBuf::from(from),
source: e, source: e,
}) })
} }
} }
@ -331,8 +317,8 @@ pub fn parse_from_fs(threads: usize, root: &str) -> io::Result<Vec<Ast>> {
struct CantLoadFile { struct CantLoadFile {
file_name: PathBuf, file_name: PathBuf,
directory: PathBuf, directory: PathBuf,
from: PathBuf, from: PathBuf,
source: io::Error, source: io::Error,
} }
impl std::fmt::Display for CantLoadFile { impl std::fmt::Display for CantLoadFile {
@ -439,10 +425,8 @@ pub fn parse_from_fs(threads: usize, root: &str) -> io::Result<Vec<Ast>> {
if let Some(mut command) = command { if let Some(mut command) = command {
let output = command.output()?; let output = command.output()?;
if !output.status.success() { if !output.status.success() {
let msg = format!( let msg =
"git command failed: {}", format!("git command failed: {}", String::from_utf8_lossy(&output.stderr));
String::from_utf8_lossy(&output.stderr)
);
return Err(io::Error::new(io::ErrorKind::Other, msg)); return Err(io::Error::new(io::ErrorKind::Other, msg));
} }
} }
@ -478,10 +462,7 @@ pub fn parse_from_fs(threads: usize, root: &str) -> io::Result<Vec<Ast>> {
std::thread::scope(|s| (0..threads).for_each(|_| _ = s.spawn(thread))); std::thread::scope(|s| (0..threads).for_each(|_| _ = s.spawn(thread)));
ast.into_inner() ast.into_inner().unwrap().into_iter().collect::<io::Result<Vec<_>>>()
.unwrap()
.into_iter()
.collect::<io::Result<Vec<_>>>()
} }
type HashMap<K, V> = std::collections::HashMap<K, V, std::hash::BuildHasherDefault<FnvHasher>>; type HashMap<K, V> = std::collections::HashMap<K, V, std::hash::BuildHasherDefault<FnvHasher>>;
@ -530,10 +511,7 @@ pub fn run_test(
std::env::var("PT_TEST_ROOT") std::env::var("PT_TEST_ROOT")
.unwrap_or(concat!(env!("CARGO_MANIFEST_DIR"), "/tests").to_string()), .unwrap_or(concat!(env!("CARGO_MANIFEST_DIR"), "/tests").to_string()),
); );
root.push( root.push(name.replace("::", "_").replace(concat!(env!("CARGO_PKG_NAME"), "_"), ""));
name.replace("::", "_")
.replace(concat!(env!("CARGO_PKG_NAME"), "_"), ""),
);
root.set_extension("txt"); root.set_extension("txt");
let expected = std::fs::read_to_string(&root).unwrap_or_default(); let expected = std::fs::read_to_string(&root).unwrap_or_default();
@ -562,11 +540,7 @@ pub fn run_test(
.spawn() .spawn()
.unwrap(); .unwrap();
proc.stdin proc.stdin.as_mut().unwrap().write_all(output.as_bytes()).unwrap();
.as_mut()
.unwrap()
.write_all(output.as_bytes())
.unwrap();
proc.wait().unwrap(); proc.wait().unwrap();
@ -575,7 +549,7 @@ pub fn run_test(
#[derive(Default)] #[derive(Default)]
pub struct Options { pub struct Options {
pub fmt: bool, pub fmt: bool,
pub fmt_current: bool, pub fmt_current: bool,
} }

View file

@ -11,7 +11,7 @@ fn main() -> std::io::Result<()> {
hblang::run_compiler( hblang::run_compiler(
args.get(1).copied().unwrap_or("main.hb"), args.get(1).copied().unwrap_or("main.hb"),
hblang::Options { hblang::Options {
fmt: args.contains(&"--fmt"), fmt: args.contains(&"--fmt"),
fmt_current: args.contains(&"--fmt-current"), fmt_current: args.contains(&"--fmt-current"),
}, },
&mut std::io::stdout(), &mut std::io::stdout(),

View file

@ -1,22 +1,24 @@
use std::{ use {
cell::{Cell, UnsafeCell}, crate::{
io, codegen,
ops::{Deref, Not}, ident::{self, Ident},
ptr::NonNull, lexer::{Lexer, LineMap, Token, TokenKind},
sync::atomic::AtomicUsize, log,
}; },
std::{
use crate::{ cell::{Cell, UnsafeCell},
codegen, io,
ident::{self, Ident}, ops::{Deref, Not},
lexer::{Lexer, LineMap, Token, TokenKind}, ptr::NonNull,
log, sync::atomic::AtomicUsize,
},
}; };
pub type Pos = u32; pub type Pos = u32;
pub type IdentFlags = u32; pub type IdentFlags = u32;
pub type Symbols = Vec<Symbol>; pub type Symbols = Vec<Symbol>;
pub type FileId = u32; pub type FileId = u32;
pub type IdentIndex = u16;
pub type Loader<'a> = &'a (dyn Fn(&str, &str) -> io::Result<FileId> + 'a); pub type Loader<'a> = &'a (dyn Fn(&str, &str) -> io::Result<FileId> + 'a);
pub mod idfl { pub mod idfl {
@ -35,7 +37,7 @@ pub mod idfl {
COMPTIME, COMPTIME,
} }
pub fn index(i: IdentFlags) -> u16 { pub fn index(i: IdentFlags) -> IdentIndex {
(i & !ALL) as _ (i & !ALL) as _
} }
} }
@ -46,28 +48,28 @@ pub fn no_loader(_: &str, _: &str) -> io::Result<FileId> {
#[derive(Debug)] #[derive(Debug)]
pub struct Symbol { pub struct Symbol {
pub name: Ident, pub name: Ident,
pub flags: IdentFlags, pub flags: IdentFlags,
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
struct ScopeIdent { struct ScopeIdent {
ident: Ident, ident: Ident,
declared: bool, declared: bool,
flags: IdentFlags, flags: IdentFlags,
} }
pub struct Parser<'a, 'b> { pub struct Parser<'a, 'b> {
path: &'b str, path: &'b str,
loader: Loader<'b>, loader: Loader<'b>,
lexer: Lexer<'b>, lexer: Lexer<'b>,
arena: &'b Arena<'a>, arena: &'b Arena<'a>,
token: Token, token: Token,
symbols: &'b mut Symbols, symbols: &'b mut Symbols,
ns_bound: usize, ns_bound: usize,
trailing_sep: bool, trailing_sep: bool,
idents: Vec<ScopeIdent>, idents: Vec<ScopeIdent>,
captured: Vec<Ident>, captured: Vec<Ident>,
} }
impl<'a, 'b> Parser<'a, 'b> { impl<'a, 'b> Parser<'a, 'b> {
@ -92,7 +94,7 @@ impl<'a, 'b> Parser<'a, 'b> {
self.lexer = Lexer::new(input); self.lexer = Lexer::new(input);
self.token = self.lexer.next(); self.token = self.lexer.next();
let f = self.collect_list(TokenKind::Semi, TokenKind::Eof, Self::expr); let f = self.collect_list(TokenKind::Semi, TokenKind::Eof, |s| s.expr_low(true));
self.pop_scope(0); self.pop_scope(0);
let has_undeclared = !self.idents.is_empty(); let has_undeclared = !self.idents.is_empty();
@ -123,12 +125,16 @@ impl<'a, 'b> Parser<'a, 'b> {
self.arena.alloc(self.expr()) self.arena.alloc(self.expr())
} }
fn expr(&mut self) -> Expr<'a> { fn expr_low(&mut self, top_level: bool) -> Expr<'a> {
let left = self.unit_expr(); let left = self.unit_expr();
self.bin_expr(left, 0) self.bin_expr(left, 0, top_level)
} }
fn bin_expr(&mut self, mut fold: Expr<'a>, min_prec: u8) -> Expr<'a> { fn expr(&mut self) -> Expr<'a> {
self.expr_low(false)
}
fn bin_expr(&mut self, mut fold: Expr<'a>, min_prec: u8, top_level: bool) -> Expr<'a> {
loop { loop {
let Some(prec) = self.token.kind.precedence() else { let Some(prec) = self.token.kind.precedence() else {
break; break;
@ -141,7 +147,11 @@ impl<'a, 'b> Parser<'a, 'b> {
let checkpoint = self.token.start; let checkpoint = self.token.start;
let op = self.next().kind; let op = self.next().kind;
let op_ass = op.assign_op().map(|op| { if op == TokenKind::Decl {
self.declare_rec(&fold, top_level);
}
let op_ass = op.ass_op().map(|op| {
// this abomination reparses the left side, so that the desubaring adheres to the // this abomination reparses the left side, so that the desubaring adheres to the
// parser invariants. // parser invariants.
let source = self.lexer.slice(0..checkpoint as usize); let source = self.lexer.slice(0..checkpoint as usize);
@ -156,23 +166,15 @@ impl<'a, 'b> Parser<'a, 'b> {
}); });
let right = self.unit_expr(); let right = self.unit_expr();
let right = self.bin_expr(right, prec); let right = self.bin_expr(right, prec, false);
let right = self.arena.alloc(right); let right = self.arena.alloc(right);
let left = self.arena.alloc(fold); let left = self.arena.alloc(fold);
if let Some((op, clone)) = op_ass { if let Some((op, clone)) = op_ass {
self.flag_idents(*left, idfl::MUTABLE); self.flag_idents(*left, idfl::MUTABLE);
let right = Expr::BinOp { let right = Expr::BinOp { left: self.arena.alloc(clone), op, right };
left: self.arena.alloc(clone), fold = Expr::BinOp { left, op: TokenKind::Assign, right: self.arena.alloc(right) };
op,
right,
};
fold = Expr::BinOp {
left,
op: TokenKind::Assign,
right: self.arena.alloc(right),
};
} else { } else {
fold = Expr::BinOp { left, right, op }; fold = Expr::BinOp { left, right, op };
if op == TokenKind::Assign { if op == TokenKind::Assign {
@ -184,7 +186,42 @@ impl<'a, 'b> Parser<'a, 'b> {
fold fold
} }
fn resolve_ident(&mut self, token: Token, decl: bool) -> (Ident, u16) { fn declare_rec(&mut self, expr: &Expr, top_level: bool) {
let idx = |idx| top_level.not().then_some(idx);
match *expr {
Expr::Ident { pos, id, index, .. } => self.declare(pos, id, idx(index)),
Expr::Ctor { fields, .. } => {
for CtorField { value, .. } in fields {
self.declare_rec(value, top_level)
}
}
_ => self.report_pos(expr.pos(), "cant declare this shit (yet)"),
}
}
fn declare(&mut self, pos: Pos, id: Ident, index_to_check: Option<IdentIndex>) {
if let Some(index) = index_to_check
&& index != 0
{
self.report_pos(
pos,
format_args!(
"out of order declaration not allowed: {}",
self.lexer.slice(ident::range(id))
),
);
}
let index = self.idents.binary_search_by_key(&id, |s| s.ident).expect("fck up");
if std::mem::replace(&mut self.idents[index].declared, true) {
self.report_pos(
pos,
format_args!("redeclaration of identifier: {}", self.lexer.slice(ident::range(id))),
)
}
}
fn resolve_ident(&mut self, token: Token) -> (Ident, IdentIndex) {
let is_ct = token.kind == TokenKind::CtIdent; let is_ct = token.kind == TokenKind::CtIdent;
let name = self.lexer.slice(token.range()); let name = self.lexer.slice(token.range());
@ -198,25 +235,17 @@ impl<'a, 'b> Parser<'a, 'b> {
.enumerate() .enumerate()
.rfind(|(_, elem)| self.lexer.slice(ident::range(elem.ident)) == name) .rfind(|(_, elem)| self.lexer.slice(ident::range(elem.ident)) == name)
{ {
Some((_, elem)) if decl && elem.declared => {
self.report(format_args!("redeclaration of identifier: {name}"))
}
Some((i, elem)) => { Some((i, elem)) => {
elem.flags += 1; elem.flags += 1;
(i, elem) (i, elem)
} }
None => { None => {
let id = ident::new(token.start, name.len() as _); let id = ident::new(token.start, name.len() as _);
self.idents.push(ScopeIdent { self.idents.push(ScopeIdent { ident: id, declared: false, flags: 0 });
ident: id,
declared: false,
flags: 0,
});
(self.idents.len() - 1, self.idents.last_mut().unwrap()) (self.idents.len() - 1, self.idents.last_mut().unwrap())
} }
}; };
id.declared |= decl;
id.flags |= idfl::COMPTIME * is_ct as u32; id.flags |= idfl::COMPTIME * is_ct as u32;
if id.declared && self.ns_bound > i { if id.declared && self.ns_bound > i {
id.flags |= idfl::COMPTIME; id.flags |= idfl::COMPTIME;
@ -244,32 +273,26 @@ impl<'a, 'b> Parser<'a, 'b> {
let path = self.lexer.slice(str.range()).trim_matches('"'); let path = self.lexer.slice(str.range()).trim_matches('"');
E::Mod { E::Mod {
pos: token.start, pos: token.start,
path: self.arena.alloc_str(path), path: self.arena.alloc_str(path),
id: match (self.loader)(path, self.path) { id: match (self.loader)(path, self.path) {
Ok(id) => id, Ok(id) => id,
Err(e) => self.report(format_args!("error loading dependency: {e:#}")), Err(e) => self.report(format_args!("error loading dependency: {e:#}")),
}, },
} }
} }
T::Directive => E::Directive { T::Directive => E::Directive {
pos: token.start, pos: token.start,
name: self.move_str(token), name: self.move_str(token),
args: { args: {
self.expect_advance(T::LParen); self.expect_advance(T::LParen);
self.collect_list(T::Comma, T::RParen, Self::expr) self.collect_list(T::Comma, T::RParen, Self::expr)
}, },
}, },
T::True => E::Bool { T::True => E::Bool { pos: token.start, value: true },
pos: token.start, T::DQuote => E::String { pos: token.start, literal: self.move_str(token) },
value: true,
},
T::DQuote => E::String {
pos: token.start,
literal: self.move_str(token),
},
T::Struct => E::Struct { T::Struct => E::Struct {
fields: { fields: {
self.ns_bound = self.idents.len(); self.ns_bound = self.idents.len();
self.expect_advance(T::LBrace); self.expect_advance(T::LBrace);
self.collect_list(T::Comma, T::RBrace, |s| { self.collect_list(T::Comma, T::RBrace, |s| {
@ -285,7 +308,7 @@ impl<'a, 'b> Parser<'a, 'b> {
self.captured.truncate(prev_captured + preserved); self.captured.truncate(prev_captured + preserved);
self.arena.alloc_slice(&self.captured[prev_captured..]) self.arena.alloc_slice(&self.captured[prev_captured..])
}, },
pos: { pos: {
if self.ns_bound == 0 { if self.ns_bound == 0 {
// we might save some memory // we might save some memory
self.captured.clear(); self.captured.clear();
@ -294,25 +317,17 @@ impl<'a, 'b> Parser<'a, 'b> {
}, },
}, },
T::Ident | T::CtIdent => { T::Ident | T::CtIdent => {
let (id, index) = self.resolve_ident(token, self.token.kind == T::Decl); let (id, index) = self.resolve_ident(token);
let name = self.move_str(token); let name = self.move_str(token);
E::Ident { E::Ident { pos: token.start, name, id, index }
pos: token.start,
name,
id,
index,
}
} }
T::If => E::If { T::If => E::If {
pos: token.start, pos: token.start,
cond: self.ptr_expr(), cond: self.ptr_expr(),
then: self.ptr_expr(), then: self.ptr_expr(),
else_: self.advance_if(T::Else).then(|| self.ptr_expr()), else_: self.advance_if(T::Else).then(|| self.ptr_expr()),
}, },
T::Loop => E::Loop { T::Loop => E::Loop { pos: token.start, body: self.ptr_expr() },
pos: token.start,
body: self.ptr_expr(),
},
T::Break => E::Break { pos: token.start }, T::Break => E::Break { pos: token.start },
T::Continue => E::Continue { pos: token.start }, T::Continue => E::Continue { pos: token.start },
T::Return => E::Return { T::Return => E::Return {
@ -320,22 +335,18 @@ impl<'a, 'b> Parser<'a, 'b> {
val: (self.token.kind != T::Semi).then(|| self.ptr_expr()), val: (self.token.kind != T::Semi).then(|| self.ptr_expr()),
}, },
T::Fn => E::Closure { T::Fn => E::Closure {
pos: token.start, pos: token.start,
args: { args: {
self.expect_advance(T::LParen); self.expect_advance(T::LParen);
self.collect_list(T::Comma, T::RParen, |s| { self.collect_list(T::Comma, T::RParen, |s| {
let name = s.advance_ident(); let name = s.advance_ident();
let (id, index) = s.resolve_ident(name, true); let (id, index) = s.resolve_ident(name);
s.declare(name.start, id, None);
s.expect_advance(T::Colon); s.expect_advance(T::Colon);
Arg { Arg { name: s.move_str(name), id, index, ty: s.expr() }
name: s.move_str(name),
id,
index,
ty: s.expr(),
}
}) })
}, },
ret: { ret: {
self.expect_advance(T::Colon); self.expect_advance(T::Colon);
self.ptr_expr() self.ptr_expr()
}, },
@ -345,7 +356,7 @@ impl<'a, 'b> Parser<'a, 'b> {
T::Tupl => self.tupl(token.start, None), T::Tupl => self.tupl(token.start, None),
T::Band | T::Mul | T::Xor => E::UnOp { T::Band | T::Mul | T::Xor => E::UnOp {
pos: token.start, pos: token.start,
op: token.kind, op: token.kind,
val: { val: {
let expr = if token.kind == T::Xor { let expr = if token.kind == T::Xor {
let expr = self.expr(); let expr = self.expr();
@ -360,11 +371,11 @@ impl<'a, 'b> Parser<'a, 'b> {
}, },
}, },
T::LBrace => E::Block { T::LBrace => E::Block {
pos: token.start, pos: token.start,
stmts: self.collect_list(T::Semi, T::RBrace, Self::expr), stmts: self.collect_list(T::Semi, T::RBrace, Self::expr),
}, },
T::Number => E::Number { T::Number => E::Number {
pos: token.start, pos: token.start,
value: match self.lexer.slice(token.range()).parse() { value: match self.lexer.slice(token.range()).parse() {
Ok(value) => value, Ok(value) => value,
Err(e) => self.report(format_args!("invalid number: {e}")), Err(e) => self.report(format_args!("invalid number: {e}")),
@ -375,10 +386,7 @@ impl<'a, 'b> Parser<'a, 'b> {
self.expect_advance(T::RParen); self.expect_advance(T::RParen);
expr expr
} }
T::Comment => Expr::Comment { T::Comment => Expr::Comment { pos: token.start, literal: self.move_str(token) },
pos: token.start,
literal: self.move_str(token),
},
tok => self.report(format_args!("unexpected token: {tok:?}")), tok => self.report(format_args!("unexpected token: {tok:?}")),
}; };
@ -398,7 +406,7 @@ impl<'a, 'b> Parser<'a, 'b> {
T::Tupl => self.tupl(token.start, Some(expr)), T::Tupl => self.tupl(token.start, Some(expr)),
T::Dot => E::Field { T::Dot => E::Field {
target: self.arena.alloc(expr), target: self.arena.alloc(expr),
field: { name: {
let token = self.expect_advance(T::Ident); let token = self.expect_advance(T::Ident);
self.move_str(token) self.move_str(token)
}, },
@ -432,9 +440,18 @@ impl<'a, 'b> Parser<'a, 'b> {
pos, pos,
ty: ty.map(|ty| self.arena.alloc(ty)), ty: ty.map(|ty| self.arena.alloc(ty)),
fields: self.collect_list(TokenKind::Comma, TokenKind::RBrace, |s| { fields: self.collect_list(TokenKind::Comma, TokenKind::RBrace, |s| {
let name = s.advance_ident(); let name_tok = s.advance_ident();
let value = s.advance_if(TokenKind::Colon).then(|| s.expr()); let name = s.move_str(name_tok);
(s.move_str(name), value) CtorField {
pos: name_tok.start,
name,
value: if s.advance_if(TokenKind::Colon) {
s.expr()
} else {
let (id, index) = s.resolve_ident(name_tok);
Expr::Ident { pos: name_tok.start, id, name, index }
},
}
}), }),
trailing_comma: std::mem::take(&mut self.trailing_sep), trailing_comma: std::mem::take(&mut self.trailing_sep),
} }
@ -444,10 +461,7 @@ impl<'a, 'b> Parser<'a, 'b> {
if matches!(self.token.kind, TokenKind::Ident | TokenKind::CtIdent) { if matches!(self.token.kind, TokenKind::Ident | TokenKind::CtIdent) {
self.next() self.next()
} else { } else {
self.report(format_args!( self.report(format_args!("expected identifier, found {:?}", self.token.kind))
"expected identifier, found {:?}",
self.token.kind
))
} }
} }
@ -462,10 +476,7 @@ impl<'a, 'b> Parser<'a, 'b> {
self.idents self.idents
.drain(undeclared_count..) .drain(undeclared_count..)
.map(|ident| Symbol { .map(|ident| Symbol { name: ident.ident, flags: ident.flags })
name: ident.ident,
flags: ident.flags,
})
.collect_into(self.symbols); .collect_into(self.symbols);
} }
@ -489,6 +500,7 @@ impl<'a, 'b> Parser<'a, 'b> {
} }
fn collect<T: Copy>(&mut self, mut f: impl FnMut(&mut Self) -> Option<T>) -> &'a [T] { fn collect<T: Copy>(&mut self, mut f: impl FnMut(&mut Self) -> Option<T>) -> &'a [T] {
// TODO: avoid this allocation
let vec = std::iter::from_fn(|| f(self)).collect::<Vec<_>>(); let vec = std::iter::from_fn(|| f(self)).collect::<Vec<_>>();
self.arena.alloc_slice(&vec) self.arena.alloc_slice(&vec)
} }
@ -504,10 +516,7 @@ impl<'a, 'b> Parser<'a, 'b> {
fn expect_advance(&mut self, kind: TokenKind) -> Token { fn expect_advance(&mut self, kind: TokenKind) -> Token {
if self.token.kind != kind { if self.token.kind != kind {
self.report(format_args!( self.report(format_args!("expected {:?}, found {:?}", kind, self.token.kind));
"expected {:?}, found {:?}",
kind, self.token.kind
));
} }
self.next() self.next()
} }
@ -534,25 +543,19 @@ impl<'a, 'b> Parser<'a, 'b> {
} }
fn find_ident(idents: &mut [ScopeIdent], id: Ident) -> &mut ScopeIdent { fn find_ident(idents: &mut [ScopeIdent], id: Ident) -> &mut ScopeIdent {
idents idents.binary_search_by_key(&id, |si| si.ident).map(|i| &mut idents[i]).unwrap()
.binary_search_by_key(&id, |si| si.ident)
.map(|i| &mut idents[i])
.unwrap()
} }
pub fn find_symbol(symbols: &[Symbol], id: Ident) -> &Symbol { pub fn find_symbol(symbols: &[Symbol], id: Ident) -> &Symbol {
symbols symbols.binary_search_by_key(&id, |s| s.name).map(|i| &symbols[i]).unwrap()
.binary_search_by_key(&id, |s| s.name)
.map(|i| &symbols[i])
.unwrap()
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Arg<'a> { pub struct Arg<'a> {
pub name: &'a str, pub name: &'a str,
pub id: Ident, pub id: Ident,
pub index: u16, pub index: IdentIndex,
pub ty: Expr<'a>, pub ty: Expr<'a>,
} }
macro_rules! generate_expr { macro_rules! generate_expr {
@ -632,7 +635,7 @@ generate_expr! {
pos: Pos, pos: Pos,
id: Ident, id: Ident,
name: &'a str, name: &'a str,
index: u16, index: IdentIndex,
}, },
Block { Block {
pos: Pos, pos: Pos,
@ -670,7 +673,7 @@ generate_expr! {
Ctor { Ctor {
pos: Pos, pos: Pos,
ty: Option<&'a Self>, ty: Option<&'a Self>,
fields: &'a [(&'a str, Option<Self>)], fields: &'a [CtorField<'a>],
trailing_comma: bool, trailing_comma: bool,
}, },
Tupl { Tupl {
@ -681,7 +684,7 @@ generate_expr! {
}, },
Field { Field {
target: &'a Self, target: &'a Self,
field: &'a str, name: &'a str,
}, },
Bool { Bool {
pos: Pos, pos: Pos,
@ -700,6 +703,48 @@ generate_expr! {
} }
} }
impl<'a> Expr<'a> {
pub fn declares(&self, iden: Result<Ident, &str>) -> Option<Ident> {
match *self {
Self::Ident { id, name, .. } if iden == Ok(id) || iden == Err(name) => Some(id),
Self::Ctor { fields, .. } => fields.iter().find_map(|f| f.value.declares(iden)),
_ => None,
}
}
pub fn find_pattern_path<F: FnOnce(&Expr)>(
&self,
ident: Ident,
target: &Expr,
mut with_final: F,
) -> Result<(), F> {
match *self {
Self::Ident { id, .. } if id == ident => {
with_final(target);
Ok(())
}
Self::Ctor { fields, .. } => {
for CtorField { name, value, .. } in fields {
match value.find_pattern_path(ident, &Expr::Field { target, name }, with_final)
{
Ok(()) => return Ok(()),
Err(e) => with_final = e,
}
}
Err(with_final)
}
_ => Err(with_final),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CtorField<'a> {
pub pos: Pos,
pub name: &'a str,
pub value: Expr<'a>,
}
trait Poser { trait Poser {
fn posi(self) -> Pos; fn posi(self) -> Pos;
} }
@ -813,42 +858,30 @@ impl<'a> std::fmt::Display for Expr<'a> {
Self::String { literal, .. } => write!(f, "{}", literal), Self::String { literal, .. } => write!(f, "{}", literal),
Self::Comment { literal, .. } => write!(f, "{}", literal.trim_end()), Self::Comment { literal, .. } => write!(f, "{}", literal.trim_end()),
Self::Mod { path, .. } => write!(f, "@mod(\"{path}\")"), Self::Mod { path, .. } => write!(f, "@mod(\"{path}\")"),
Self::Field { target, field } => write!(f, "{}.{field}", Postfix(target)), Self::Field { target, name: field } => write!(f, "{}.{field}", Postfix(target)),
Self::Directive { name, args, .. } => { Self::Directive { name, args, .. } => {
write!(f, "@{name}(")?; write!(f, "@{name}(")?;
fmt_list(f, false, ")", args, std::fmt::Display::fmt) fmt_list(f, false, ")", args, std::fmt::Display::fmt)
} }
Self::Struct { fields, .. } => { Self::Struct { fields, .. } => {
write!(f, "struct {{")?; write!(f, "struct {{")?;
fmt_list(f, true, "}", fields, |(name, val), f| { fmt_list(f, true, "}", fields, |(name, val), f| write!(f, "{name}: {val}",))
write!(f, "{name}: {val}",)
})
} }
Self::Ctor { Self::Ctor { ty, fields, trailing_comma, .. } => {
ty,
fields,
trailing_comma,
..
} => {
if let Some(ty) = ty { if let Some(ty) = ty {
write!(f, "{}", Unary(ty))?; write!(f, "{}", Unary(ty))?;
} }
write!(f, ".{{")?; write!(f, ".{{")?;
let fmt_field = |(name, val): &_, f: &mut std::fmt::Formatter| { let fmt_field = |CtorField { name, value, .. }: &_, f: &mut std::fmt::Formatter| {
if let Some(val) = val { if matches!(value, Expr::Ident { name: n, .. } if name == n) {
write!(f, "{name}: {val}")
} else {
write!(f, "{name}") write!(f, "{name}")
} else {
write!(f, "{name}: {value}")
} }
}; };
fmt_list(f, trailing_comma, "}", fields, fmt_field) fmt_list(f, trailing_comma, "}", fields, fmt_field)
} }
Self::Tupl { Self::Tupl { ty, fields, trailing_comma, .. } => {
ty,
fields,
trailing_comma,
..
} => {
if let Some(ty) = ty { if let Some(ty) = ty {
write!(f, "{}", Unary(ty))?; write!(f, "{}", Unary(ty))?;
} }
@ -858,9 +891,7 @@ impl<'a> std::fmt::Display for Expr<'a> {
Self::UnOp { op, val, .. } => write!(f, "{op}{}", Unary(val)), Self::UnOp { op, val, .. } => write!(f, "{op}{}", Unary(val)),
Self::Break { .. } => write!(f, "break"), Self::Break { .. } => write!(f, "break"),
Self::Continue { .. } => write!(f, "continue"), Self::Continue { .. } => write!(f, "continue"),
Self::If { Self::If { cond, then, else_, .. } => {
cond, then, else_, ..
} => {
write!(f, "if {cond} {}", Consecutive(then))?; write!(f, "if {cond} {}", Consecutive(then))?;
if let Some(else_) = else_ { if let Some(else_) = else_ {
write!(f, " else {else_}")?; write!(f, " else {else_}")?;
@ -868,24 +899,16 @@ impl<'a> std::fmt::Display for Expr<'a> {
Ok(()) Ok(())
} }
Self::Loop { body, .. } => write!(f, "loop {body}"), Self::Loop { body, .. } => write!(f, "loop {body}"),
Self::Closure { Self::Closure { ret, body, args, .. } => {
ret, body, args, ..
} => {
write!(f, "fn(")?; write!(f, "fn(")?;
fmt_list(f, false, "", args, |arg, f| { fmt_list(f, false, "", args, |arg, f| write!(f, "{}: {}", arg.name, arg.ty))?;
write!(f, "{}: {}", arg.name, arg.ty)
})?;
write!(f, "): {ret} {body}")?; write!(f, "): {ret} {body}")?;
if !matches!(body, Self::Block { .. }) { if !matches!(body, Self::Block { .. }) {
write!(f, ";")?; write!(f, ";")?;
} }
Ok(()) Ok(())
} }
Self::Call { Self::Call { func, args, trailing_comma } => {
func,
args,
trailing_comma,
} => {
write!(f, "{}(", Postfix(func))?; write!(f, "{}(", Postfix(func))?;
fmt_list(f, trailing_comma, ")", args, std::fmt::Display::fmt) fmt_list(f, trailing_comma, ")", args, std::fmt::Display::fmt)
} }
@ -936,11 +959,11 @@ impl<'a> std::fmt::Display for Expr<'a> {
#[repr(C)] #[repr(C)]
pub struct AstInner<T: ?Sized> { pub struct AstInner<T: ?Sized> {
ref_count: AtomicUsize, ref_count: AtomicUsize,
mem: ArenaChunk, mem: ArenaChunk,
exprs: *const [Expr<'static>], exprs: *const [Expr<'static>],
pub path: Box<str>, pub path: Box<str>,
pub nlines: LineMap, pub nlines: LineMap,
pub symbols: T, pub symbols: T,
} }
@ -966,17 +989,14 @@ impl AstInner<[Symbol]> {
let ptr = std::alloc::alloc(layout); let ptr = std::alloc::alloc(layout);
let inner: *mut Self = std::ptr::from_raw_parts_mut(ptr as *mut _, syms.len()); let inner: *mut Self = std::ptr::from_raw_parts_mut(ptr as *mut _, syms.len());
std::ptr::write( std::ptr::write(inner as *mut AstInner<()>, AstInner {
inner as *mut AstInner<()>, ref_count: AtomicUsize::new(1),
AstInner { mem: arena.chunk.into_inner(),
ref_count: AtomicUsize::new(1), exprs,
mem: arena.chunk.into_inner(), path: path.into(),
exprs, nlines: LineMap::new(content),
path: path.into(), symbols: (),
nlines: LineMap::new(content), });
symbols: (),
},
);
std::ptr::addr_of_mut!((*inner).symbols) std::ptr::addr_of_mut!((*inner).symbols)
.as_mut_ptr() .as_mut_ptr()
.copy_from_nonoverlapping(syms.as_ptr(), syms.len()); .copy_from_nonoverlapping(syms.as_ptr(), syms.len());
@ -1004,11 +1024,7 @@ impl Ast {
pub fn find_decl(&self, id: Result<Ident, &str>) -> Option<(&Expr, Ident)> { pub fn find_decl(&self, id: Result<Ident, &str>) -> Option<(&Expr, Ident)> {
self.exprs().iter().find_map(|expr| match expr { self.exprs().iter().find_map(|expr| match expr {
Expr::BinOp { Expr::BinOp { left, op: TokenKind::Decl, .. } => left.declares(id).map(|id| (expr, id)),
left: &Expr::Ident { id: iden, name, .. },
op: TokenKind::Decl,
..
} if Ok(iden) == id || Err(name) == id => Some((expr, iden)),
_ => None, _ => None,
}) })
} }
@ -1052,9 +1068,7 @@ unsafe impl Sync for Ast {}
impl Clone for Ast { impl Clone for Ast {
fn clone(&self) -> Self { fn clone(&self) -> Self {
unsafe { self.0.as_ref() } unsafe { self.0.as_ref() }.ref_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
.ref_count
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
Self(self.0) Self(self.0)
} }
} }
@ -1062,11 +1076,7 @@ impl Clone for Ast {
impl Drop for Ast { impl Drop for Ast {
fn drop(&mut self) { fn drop(&mut self) {
let inner = unsafe { self.0.as_ref() }; let inner = unsafe { self.0.as_ref() };
if inner if inner.ref_count.fetch_sub(1, std::sync::atomic::Ordering::Relaxed) == 1 {
.ref_count
.fetch_sub(1, std::sync::atomic::Ordering::Relaxed)
== 1
{
unsafe { std::ptr::drop_in_place(self.0.as_ptr()) }; unsafe { std::ptr::drop_in_place(self.0.as_ptr()) };
let layout = AstInner::layout(inner.symbols.len()); let layout = AstInner::layout(inner.symbols.len());
@ -1088,7 +1098,7 @@ impl Deref for Ast {
#[derive(Default)] #[derive(Default)]
pub struct Arena<'a> { pub struct Arena<'a> {
chunk: UnsafeCell<ArenaChunk>, chunk: UnsafeCell<ArenaChunk>,
ph: std::marker::PhantomData<&'a ()>, ph: std::marker::PhantomData<&'a ()>,
} }
impl<'a> Arena<'a> { impl<'a> Arena<'a> {
@ -1103,8 +1113,7 @@ impl<'a> Arena<'a> {
let layout = unsafe { std::alloc::Layout::from_size_align_unchecked(size, align) }; let layout = unsafe { std::alloc::Layout::from_size_align_unchecked(size, align) };
let ptr = self.alloc_low(layout); let ptr = self.alloc_low(layout);
unsafe { unsafe {
ptr.cast::<u64>() ptr.cast::<u64>().copy_from_nonoverlapping(NonNull::from(&expr).cast(), size / 8)
.copy_from_nonoverlapping(NonNull::from(&expr).cast(), size / 8)
}; };
unsafe { ptr.cast::<Expr<'a>>().as_ref() } unsafe { ptr.cast::<Expr<'a>>().as_ref() }
} }
@ -1116,11 +1125,7 @@ impl<'a> Arena<'a> {
let layout = std::alloc::Layout::array::<T>(slice.len()).unwrap(); let layout = std::alloc::Layout::array::<T>(slice.len()).unwrap();
let ptr = self.alloc_low(layout); let ptr = self.alloc_low(layout);
unsafe { unsafe { ptr.as_ptr().cast::<T>().copy_from_nonoverlapping(slice.as_ptr(), slice.len()) };
ptr.as_ptr()
.cast::<T>()
.copy_from_nonoverlapping(slice.as_ptr(), slice.len())
};
unsafe { std::slice::from_raw_parts(ptr.as_ptr() as _, slice.len()) } unsafe { std::slice::from_raw_parts(ptr.as_ptr() as _, slice.len()) }
} }
@ -1144,24 +1149,21 @@ impl<'a> Arena<'a> {
struct ArenaChunk { struct ArenaChunk {
base: *mut u8, base: *mut u8,
end: *mut u8, end: *mut u8,
} }
impl Default for ArenaChunk { impl Default for ArenaChunk {
fn default() -> Self { fn default() -> Self {
Self { Self { base: std::ptr::null_mut(), end: std::ptr::null_mut() }
base: std::ptr::null_mut(),
end: std::ptr::null_mut(),
}
} }
} }
impl ArenaChunk { impl ArenaChunk {
const CHUNK_SIZE: usize = 1 << 16;
const ALIGN: usize = std::mem::align_of::<Self>(); const ALIGN: usize = std::mem::align_of::<Self>();
const NEXT_OFFSET: usize = Self::CHUNK_SIZE - std::mem::size_of::<*mut u8>(); const CHUNK_SIZE: usize = 1 << 16;
const LAYOUT: std::alloc::Layout = const LAYOUT: std::alloc::Layout =
unsafe { std::alloc::Layout::from_size_align_unchecked(Self::CHUNK_SIZE, Self::ALIGN) }; unsafe { std::alloc::Layout::from_size_align_unchecked(Self::CHUNK_SIZE, Self::ALIGN) };
const NEXT_OFFSET: usize = Self::CHUNK_SIZE - std::mem::size_of::<*mut u8>();
fn new(next: *mut u8) -> Self { fn new(next: *mut u8) -> Self {
let base = unsafe { std::alloc::alloc(Self::LAYOUT) }; let base = unsafe { std::alloc::alloc(Self::LAYOUT) };

View file

@ -1,5 +1,5 @@
pkg := @use("pkg.hb"); .{ global, fib } := @use("pkg.hb");
main := fn(a: int): int { main := fn(a: int): int {
return pkg.fib(pkg.global); return fib(global);
} }

View file

@ -16,25 +16,20 @@ struct AlignedBuf([MaybeUninit<u8>; BUF_SIZE]);
/// State for block memory copy /// State for block memory copy
pub struct BlockCopier { pub struct BlockCopier {
/// Source address /// Source address
src: Address, src: Address,
/// Destination address /// Destination address
dst: Address, dst: Address,
/// How many buffer sizes to copy? /// How many buffer sizes to copy?
n_buffers: usize, n_buffers: usize,
/// …and what remainds after? /// …and what remainds after?
rem: usize, rem: usize,
} }
impl BlockCopier { impl BlockCopier {
/// Construct a new one /// Construct a new one
#[inline] #[inline]
pub fn new(src: Address, dst: Address, count: usize) -> Self { pub fn new(src: Address, dst: Address, count: usize) -> Self {
Self { Self { src, dst, n_buffers: count / BUF_SIZE, rem: count % BUF_SIZE }
src,
dst,
n_buffers: count / BUF_SIZE,
rem: count % BUF_SIZE,
}
} }
/// Copy one block /// Copy one block
@ -47,15 +42,9 @@ impl BlockCopier {
// We have at least one buffer size to copy // We have at least one buffer size to copy
if self.n_buffers != 0 { if self.n_buffers != 0 {
if let Err(e) = unsafe { if let Err(e) =
act( unsafe { act(memory, self.src, self.dst, buf.0.as_mut_ptr().cast(), BUF_SIZE) }
memory, {
self.src,
self.dst,
buf.0.as_mut_ptr().cast(),
BUF_SIZE,
)
} {
return Poll::Ready(Err(e)); return Poll::Ready(Err(e));
} }
@ -75,15 +64,9 @@ impl BlockCopier {
} }
if self.rem != 0 { if self.rem != 0 {
if let Err(e) = unsafe { if let Err(e) =
act( unsafe { act(memory, self.src, self.dst, buf.0.as_mut_ptr().cast(), self.rem) }
memory, {
self.src,
self.dst,
buf.0.as_mut_ptr().cast(),
self.rem,
)
} {
return Poll::Ready(Err(e)); return Poll::Ready(Err(e));
} }
} }
@ -103,20 +86,16 @@ unsafe fn act(
) -> Result<(), BlkCopyError> { ) -> Result<(), BlkCopyError> {
unsafe { unsafe {
// Load to buffer // Load to buffer
memory memory.load(src, buf, count).map_err(|super::mem::LoadError(addr)| BlkCopyError {
.load(src, buf, count) access_reason: MemoryAccessReason::Load,
.map_err(|super::mem::LoadError(addr)| BlkCopyError { addr,
access_reason: MemoryAccessReason::Load, })?;
addr,
})?;
// Store from buffer // Store from buffer
memory memory.store(dst, buf, count).map_err(|super::mem::StoreError(addr)| BlkCopyError {
.store(dst, buf, count) access_reason: MemoryAccessReason::Store,
.map_err(|super::mem::StoreError(addr)| BlkCopyError { addr,
access_reason: MemoryAccessReason::Store, })?;
addr,
})?;
} }
Ok(()) Ok(())

View file

@ -12,7 +12,7 @@ macro_rules! arch_specific {
#[cfg($($cfg)*)] #[cfg($($cfg)*)]
pub const FL_ARCH_SPECIFIC_SUPPORTED: bool = true; pub const FL_ARCH_SPECIFIC_SUPPORTED: bool = true;
)* )*
#[cfg(not(any($($($cfg)*),*)))] #[cfg(not(any($($($cfg)*),*)))]
mod unsupported; mod unsupported;

View file

@ -26,7 +26,6 @@ mod utils;
mod vmrun; mod vmrun;
pub use float::FL_ARCH_SPECIFIC_SUPPORTED; pub use float::FL_ARCH_SPECIFIC_SUPPORTED;
use { use {
bmc::BlockCopier, bmc::BlockCopier,
mem::{Address, Memory}, mem::{Address, Memory},
@ -58,10 +57,10 @@ impl<Mem: Default, const TIMER_QUOTIENT: usize> Default for Vm<Mem, TIMER_QUOTIE
fn default() -> Self { fn default() -> Self {
Self { Self {
registers: [Value::from(0_u64); 256], registers: [Value::from(0_u64); 256],
memory: Mem::default(), memory: Mem::default(),
pc: Address::default(), pc: Address::default(),
timer: 0, timer: 0,
copier: None, copier: None,
} }
} }
} }
@ -75,13 +74,7 @@ where
/// # Safety /// # Safety
/// Program code has to be validated /// Program code has to be validated
pub unsafe fn new(memory: Mem, entry: Address) -> Self { pub unsafe fn new(memory: Mem, entry: Address) -> Self {
Self { Self { registers: [Value::from(0_u64); 256], memory, pc: entry, timer: 0, copier: None }
registers: [Value::from(0_u64); 256],
memory,
pc: entry,
timer: 0,
copier: None,
}
} }
/// Read register /// Read register

View file

@ -4,9 +4,8 @@ pub mod softpaging;
pub(crate) mod addr; pub(crate) mod addr;
pub use addr::Address;
use crate::utils::impl_display; use crate::utils::impl_display;
pub use addr::Address;
/// Load-store memory access /// Load-store memory access
pub trait Memory { pub trait Memory {

View file

@ -1,9 +1,8 @@
//! Program instruction cache //! Program instruction cache
use crate::mem::Address;
use { use {
super::{lookup::AddrPageLookuper, paging::PageTable, PageSize}, super::{lookup::AddrPageLookuper, paging::PageTable, PageSize},
crate::mem::Address,
core::{ core::{
mem::{size_of, MaybeUninit}, mem::{size_of, MaybeUninit},
ptr::{copy_nonoverlapping, NonNull}, ptr::{copy_nonoverlapping, NonNull},
@ -46,9 +45,8 @@ impl ICache {
) -> Option<T> { ) -> Option<T> {
let mut ret = MaybeUninit::<T>::uninit(); let mut ret = MaybeUninit::<T>::uninit();
let pbase = self let pbase =
.data self.data.or_else(|| unsafe { self.fetch_page(self.base + self.size, root_pt) })?;
.or_else(|| unsafe { self.fetch_page(self.base + self.size, root_pt) })?;
// Get address base // Get address base
let base = addr.map(|x| x & self.mask); let base = addr.map(|x| x & self.mask);
@ -62,9 +60,7 @@ impl ICache {
let requ_size = size_of::<T>(); let requ_size = size_of::<T>();
// Page overflow // Page overflow
let rem = (offset as usize) let rem = (offset as usize).saturating_add(requ_size).saturating_sub(self.size as _);
.saturating_add(requ_size)
.saturating_sub(self.size as _);
let first_copy = requ_size.saturating_sub(rem); let first_copy = requ_size.saturating_sub(rem);
// Copy non-overflowing part // Copy non-overflowing part

View file

@ -1,11 +1,12 @@
//! Address lookup //! Address lookup
use crate::mem::addr::Address; use {
super::{
use super::{ addr_extract_index,
addr_extract_index, paging::{PageTable, Permission},
paging::{PageTable, Permission}, PageSize,
PageSize, },
crate::mem::addr::Address,
}; };
/// Good result from address split /// Good result from address split
@ -48,11 +49,7 @@ impl AddrPageLookuper {
/// Create a new page lookuper /// Create a new page lookuper
#[inline] #[inline]
pub const fn new(addr: Address, size: usize, pagetable: *const PageTable) -> Self { pub const fn new(addr: Address, size: usize, pagetable: *const PageTable) -> Self {
Self { Self { addr, size, pagetable }
addr,
size,
pagetable,
}
} }
/// Bump address by size X /// Bump address by size X
@ -78,9 +75,8 @@ impl Iterator for AddrPageLookuper {
for lvl in (0..5).rev() { for lvl in (0..5).rev() {
// Get an entry // Get an entry
unsafe { unsafe {
let entry = (*current_pt) let entry =
.table (*current_pt).table.get_unchecked(addr_extract_index(self.addr, lvl));
.get_unchecked(addr_extract_index(self.addr, lvl));
let ptr = entry.ptr(); let ptr = entry.ptr();
match entry.permission() { match entry.permission() {

View file

@ -1,13 +1,12 @@
//! Automatic memory mapping //! Automatic memory mapping
use crate::{mem::addr::Address, utils::impl_display};
use { use {
super::{ super::{
addr_extract_index, addr_extract_index,
paging::{PageTable, Permission, PtEntry, PtPointedData}, paging::{PageTable, Permission, PtEntry, PtPointedData},
PageSize, SoftPagedMem, PageSize, SoftPagedMem,
}, },
crate::{mem::addr::Address, utils::impl_display},
alloc::boxed::Box, alloc::boxed::Box,
}; };
@ -36,11 +35,8 @@ impl<'p, A, const OUT_PROG_EXEC: bool> SoftPagedMem<'p, A, OUT_PROG_EXEC> {
// Walk pagetable levels // Walk pagetable levels
for lvl in (lookup_depth + 1..5).rev() { for lvl in (lookup_depth + 1..5).rev() {
let entry = unsafe { let entry =
(*current_pt) unsafe { (*current_pt).table.get_unchecked_mut(addr_extract_index(target, lvl)) };
.table
.get_unchecked_mut(addr_extract_index(target, lvl))
};
let ptr = entry.ptr(); let ptr = entry.ptr();
match entry.permission() { match entry.permission() {
@ -50,9 +46,7 @@ impl<'p, A, const OUT_PROG_EXEC: bool> SoftPagedMem<'p, A, OUT_PROG_EXEC> {
// Increase children count // Increase children count
unsafe { *current_pt }.childen += 1; unsafe { *current_pt }.childen += 1;
let table = Box::into_raw(Box::new(PtPointedData { let table = Box::into_raw(Box::new(PtPointedData { pt: PageTable::default() }));
pt: PageTable::default(),
}));
unsafe { core::ptr::write(entry, PtEntry::new(table, Permission::Node)) }; unsafe { core::ptr::write(entry, PtEntry::new(table, Permission::Node)) };
current_pt = table as _; current_pt = table as _;
@ -66,9 +60,7 @@ impl<'p, A, const OUT_PROG_EXEC: bool> SoftPagedMem<'p, A, OUT_PROG_EXEC> {
} }
let node = unsafe { let node = unsafe {
(*current_pt) (*current_pt).table.get_unchecked_mut(addr_extract_index(target, lookup_depth))
.table
.get_unchecked_mut(addr_extract_index(target, lookup_depth))
}; };
// Check if node is not mapped // Check if node is not mapped
@ -95,11 +87,8 @@ impl<'p, A, const OUT_PROG_EXEC: bool> SoftPagedMem<'p, A, OUT_PROG_EXEC> {
// Walk page table in reverse // Walk page table in reverse
for lvl in (0..5).rev() { for lvl in (0..5).rev() {
let entry = unsafe { let entry =
(*current_pt) unsafe { (*current_pt).table.get_unchecked_mut(addr_extract_index(addr, lvl)) };
.table
.get_unchecked_mut(addr_extract_index(addr, lvl))
};
let ptr = entry.ptr(); let ptr = entry.ptr();
match entry.permission() { match entry.permission() {

View file

@ -23,13 +23,13 @@ use {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct SoftPagedMem<'p, PfH, const OUT_PROG_EXEC: bool = true> { pub struct SoftPagedMem<'p, PfH, const OUT_PROG_EXEC: bool = true> {
/// Root page table /// Root page table
pub root_pt: *mut PageTable, pub root_pt: *mut PageTable,
/// Page fault handler /// Page fault handler
pub pf_handler: PfH, pub pf_handler: PfH,
/// Program memory segment /// Program memory segment
pub program: &'p [u8], pub program: &'p [u8],
/// Program instruction cache /// Program instruction cache
pub icache: ICache, pub icache: ICache,
} }
impl<'p, PfH: HandlePageFault, const OUT_PROG_EXEC: bool> Memory impl<'p, PfH: HandlePageFault, const OUT_PROG_EXEC: bool> Memory
@ -127,10 +127,7 @@ impl<'p, PfH: HandlePageFault, const OUT_PROG_EXEC: bool> SoftPagedMem<'p, PfH,
); );
// Return shifted from what we've already copied // Return shifted from what we've already copied
( (src.saturating_add(to_copy as u64), len.saturating_sub(to_copy))
src.saturating_add(to_copy as u64),
len.saturating_sub(to_copy),
)
} else { } else {
(src, len) // Nothing weird! (src, len) // Nothing weird!
}; };
@ -145,12 +142,7 @@ impl<'p, PfH: HandlePageFault, const OUT_PROG_EXEC: bool> SoftPagedMem<'p, PfH,
loop { loop {
match pspl.next() { match pspl.next() {
// Page is found // Page is found
Some(Ok(AddrPageLookupOk { Some(Ok(AddrPageLookupOk { vaddr, ptr, size, perm })) => {
vaddr,
ptr,
size,
perm,
})) => {
if !permission_check(perm) { if !permission_check(perm) {
return Err(vaddr); return Err(vaddr);
} }
@ -243,10 +235,7 @@ pub mod perm_check {
/// Page is readable /// Page is readable
#[inline(always)] #[inline(always)]
pub const fn readable(perm: Permission) -> bool { pub const fn readable(perm: Permission) -> bool {
matches!( matches!(perm, Permission::Readonly | Permission::Write | Permission::Exec)
perm,
Permission::Readonly | Permission::Write | Permission::Exec
)
} }
/// Page is writable /// Page is writable

View file

@ -62,16 +62,13 @@ pub struct PageTable {
/// How much entries are in use /// How much entries are in use
pub childen: u8, pub childen: u8,
/// Entries /// Entries
pub table: [PtEntry; 256], pub table: [PtEntry; 256],
} }
impl Default for PageTable { impl Default for PageTable {
fn default() -> Self { fn default() -> Self {
// SAFETY: It's fine, zeroed page table entry is valid (= empty) // SAFETY: It's fine, zeroed page table entry is valid (= empty)
Self { Self { childen: 0, table: unsafe { MaybeUninit::zeroed().assume_init() } }
childen: 0,
table: unsafe { MaybeUninit::zeroed().assume_init() },
}
} }
} }
@ -80,7 +77,7 @@ impl Default for PageTable {
#[repr(C, align(4096))] #[repr(C, align(4096))]
pub union PtPointedData { pub union PtPointedData {
/// Node - next page table /// Node - next page table
pub pt: PageTable, pub pt: PageTable,
/// Leaf /// Leaf
pub page: u8, pub page: u8,
} }

View file

@ -173,9 +173,7 @@ where
LI64 => handler!(self, |OpsRD(tg, imm)| self.write_reg(tg, imm)), LI64 => handler!(self, |OpsRD(tg, imm)| self.write_reg(tg, imm)),
LRA => handler!(self, |OpsRRO(tg, reg, off)| self.write_reg( LRA => handler!(self, |OpsRRO(tg, reg, off)| self.write_reg(
tg, tg,
self.pcrel(off) self.pcrel(off).wrapping_add(self.read_reg(reg).cast::<i64>()).get(),
.wrapping_add(self.read_reg(reg).cast::<i64>())
.get(),
)), )),
// Load. If loading more than register size, continue on adjecent registers // Load. If loading more than register size, continue on adjecent registers
LD => handler!(self, |OpsRRAH(dst, base, off, count)| self LD => handler!(self, |OpsRRAH(dst, base, off, count)| self
@ -251,9 +249,7 @@ where
let OpsRRO(save, reg, offset) = self.decode(); let OpsRRO(save, reg, offset) = self.decode();
self.write_reg(save, self.pc.next::<OpsRRO>()); self.write_reg(save, self.pc.next::<OpsRRO>());
self.pc = self self.pc = self.pcrel(offset).wrapping_add(self.read_reg(reg).cast::<i64>());
.pcrel(offset)
.wrapping_add(self.read_reg(reg).cast::<i64>());
} }
JALA => { JALA => {
// Jump and link. Save PC after this instruction to // Jump and link. Save PC after this instruction to
@ -375,10 +371,7 @@ where
/// Bump instruction pointer /// Bump instruction pointer
#[inline(always)] #[inline(always)]
fn bump_pc<T: Copy>(&mut self) { fn bump_pc<T: Copy>(&mut self) {
self.pc = self self.pc = self.pc.wrapping_add(core::mem::size_of::<T>()).wrapping_add(1);
.pc
.wrapping_add(core::mem::size_of::<T>())
.wrapping_add(1);
} }
/// Decode instruction operands /// Decode instruction operands
@ -404,10 +397,7 @@ where
unsafe { unsafe {
self.memory.load( self.memory.load(
self.ldst_addr_uber(dst, base, offset, count, n)?, self.ldst_addr_uber(dst, base, offset, count, n)?,
self.registers self.registers.as_mut_ptr().add(usize::from(dst) + usize::from(n)).cast(),
.as_mut_ptr()
.add(usize::from(dst) + usize::from(n))
.cast(),
usize::from(count).saturating_sub(n.into()), usize::from(count).saturating_sub(n.into()),
) )
}?; }?;
@ -444,10 +434,7 @@ where
#[inline(always)] #[inline(always)]
unsafe fn binary_op<T: ValueVariant>(&mut self, op: impl Fn(T, T) -> T) { unsafe fn binary_op<T: ValueVariant>(&mut self, op: impl Fn(T, T) -> T) {
let OpsRRR(tg, a0, a1) = unsafe { self.decode() }; let OpsRRR(tg, a0, a1) = unsafe { self.decode() };
self.write_reg( self.write_reg(tg, op(self.read_reg(a0).cast::<T>(), self.read_reg(a1).cast::<T>()));
tg,
op(self.read_reg(a0).cast::<T>(), self.read_reg(a1).cast::<T>()),
);
self.bump_pc::<OpsRRR>(); self.bump_pc::<OpsRRR>();
} }
@ -467,13 +454,7 @@ where
#[inline(always)] #[inline(always)]
unsafe fn binary_op_shift<T: ValueVariant>(&mut self, op: impl Fn(T, u32) -> T) { unsafe fn binary_op_shift<T: ValueVariant>(&mut self, op: impl Fn(T, u32) -> T) {
let OpsRRR(tg, a0, a1) = unsafe { self.decode() }; let OpsRRR(tg, a0, a1) = unsafe { self.decode() };
self.write_reg( self.write_reg(tg, op(self.read_reg(a0).cast::<T>(), self.read_reg(a1).cast::<u32>()));
tg,
op(
self.read_reg(a0).cast::<T>(),
self.read_reg(a1).cast::<u32>(),
),
);
self.bump_pc::<OpsRRR>(); self.bump_pc::<OpsRRR>();
} }
@ -540,12 +521,7 @@ where
#[inline(always)] #[inline(always)]
unsafe fn cond_jmp<T: ValueVariant + Ord>(&mut self, expected: Ordering) { unsafe fn cond_jmp<T: ValueVariant + Ord>(&mut self, expected: Ordering) {
let OpsRRP(a0, a1, ja) = unsafe { self.decode() }; let OpsRRP(a0, a1, ja) = unsafe { self.decode() };
if self if self.read_reg(a0).cast::<T>().cmp(&self.read_reg(a1).cast::<T>()) == expected {
.read_reg(a0)
.cast::<T>()
.cmp(&self.read_reg(a1).cast::<T>())
== expected
{
self.pc = self.pcrel(ja); self.pc = self.pcrel(ja);
} else { } else {
self.bump_pc::<OpsRRP>(); self.bump_pc::<OpsRRP>();

View file

@ -53,20 +53,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
eprintln!("[I] Image loaded at {:p}", mmap.as_ptr()); eprintln!("[I] Image loaded at {:p}", mmap.as_ptr());
let mut vm = unsafe { let mut vm = unsafe {
Vm::<_, 0>::new( Vm::<_, 0>::new(hbvm::mem::HostMemory, Address::new(mmap.as_ptr().add(stack.len()) as u64))
hbvm::mem::HostMemory,
Address::new(mmap.as_ptr().add(stack.len()) as u64),
)
}; };
vm.write_reg(254, stack.as_mut_ptr() as u64); vm.write_reg(254, stack.as_mut_ptr() as u64);
// Execute program // Execute program
let stat = loop { let stat = loop {
match vm.run() { match vm.run() {
Ok(VmRunOk::Breakpoint) => eprintln!( Ok(VmRunOk::Breakpoint) => {
"[I] Hit breakpoint\nIP: {}\n== Registers ==\n{:?}", eprintln!("[I] Hit breakpoint\nIP: {}\n== Registers ==\n{:?}", vm.pc, vm.registers)
vm.pc, vm.registers }
),
Ok(VmRunOk::Timer) => (), Ok(VmRunOk::Timer) => (),
Ok(VmRunOk::Ecall) if dsls => unsafe { Ok(VmRunOk::Ecall) if dsls => unsafe {
std::arch::asm!( std::arch::asm!(

View file

@ -1,4 +1,7 @@
hex_literal_case = "Upper"
imports_granularity = "One" imports_granularity = "One"
struct_field_align_threshold = 8 group_imports = "One"
enum_discrim_align_threshold = 8 reorder_impl_items = true
unstable_features = false
overflow_delimited_expr = true
use_small_heuristics = "Max"
use_field_init_shorthand = true

View file

@ -5,5 +5,4 @@ edition = "2021"
[dependencies] [dependencies]
argh = "0.1" argh = "0.1"
color-eyre = "0.6"
once_cell = "1.18" once_cell = "1.18"

View file

@ -1,10 +1,9 @@
use { use {
crate::{utils::IterExt, ROOT}, crate::{utils::IterExt, ROOT},
argh::FromArgs, argh::FromArgs,
color_eyre::{eyre::eyre, Result},
std::{ std::{
fs::File, fs::File,
io::{BufRead, BufReader, BufWriter, Seek, Write}, io::{self, BufRead, BufReader, BufWriter, Seek, Write},
}, },
}; };
@ -17,11 +16,9 @@ pub struct Command {
renumber: bool, renumber: bool,
} }
pub fn command(args: Command) -> Result<()> { pub fn command(args: Command) -> io::Result<()> {
let mut file = File::options() let mut file =
.read(true) File::options().read(true).write(true).open(ROOT.join("hbbytecode/instructions.in"))?;
.write(true)
.open(ROOT.join("hbbytecode/instructions.in"))?;
// Extract records // Extract records
let reader = BufReader::new(&file); let reader = BufReader::new(&file);
@ -36,14 +33,11 @@ pub fn command(args: Command) -> Result<()> {
return None; return None;
} }
s.split(',') s.split(',').map(|s| Box::<str>::from(s.trim())).collect_array::<4>().map(Ok::<_, ()>)
.map(|s| Box::<str>::from(s.trim()))
.collect_array::<4>()
.map(Ok::<_, ()>)
}) })
.transpose() .transpose()
}) { }) {
let rec = rec?.map_err(|_| eyre!("Invalid record format"))?; let rec = rec?.expect("Valid record format");
for (current, next) in lens.iter_mut().zip(rec.iter()) { for (current, next) in lens.iter_mut().zip(rec.iter()) {
*current = (*current).max(next.len()); *current = (*current).max(next.len());
} }

View file

@ -1,7 +1,11 @@
mod fmt; mod fmt;
mod utils; mod utils;
use {argh::FromArgs, color_eyre::Result, once_cell::sync::Lazy, std::path::Path}; use {
argh::FromArgs,
once_cell::sync::Lazy,
std::{io, path::Path},
};
static ROOT: Lazy<&Path> = Lazy::new(|| Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap()); static ROOT: Lazy<&Path> = Lazy::new(|| Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap());
@ -18,7 +22,7 @@ enum Subcommands {
Format(fmt::Command), Format(fmt::Command),
} }
fn main() -> Result<()> { fn main() -> io::Result<()> {
match argh::from_env::<Command>().subcom { match argh::from_env::<Command>().subcom {
Subcommands::Format(com) => fmt::command(com), Subcommands::Format(com) => fmt::command(com),
} }