Compare commits
8 commits
Author | SHA1 | Date | |
---|---|---|---|
69e7ad0642 | |||
7f3984ad9a | |||
bfc92431ea | |||
ede18f86f8 | |||
31c501c643 | |||
0d118c17b2 | |||
0ae0cae825 | |||
da69b705f1 |
7
.gitignore
vendored
7
.gitignore
vendored
|
@ -1,7 +1,4 @@
|
|||
/target
|
||||
/hbbytecode/src/opcode.rs
|
||||
/hbbytecode/src/ops.rs
|
||||
/hblang/src/instrs.rs
|
||||
/hblang/src/disasm.rs
|
||||
/hbbytecode/src/instrs.rs
|
||||
/.rgignore
|
||||
rust-ice-*
|
||||
rustc-ice-*
|
||||
|
|
321
Cargo.lock
generated
321
Cargo.lock
generated
|
@ -2,119 +2,11 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "ahash"
|
||||
version = "0.8.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
"version_check",
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"anstyle-parse",
|
||||
"anstyle-query",
|
||||
"anstyle-wincon",
|
||||
"colorchoice",
|
||||
"is_terminal_polyfill",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-parse"
|
||||
version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb"
|
||||
dependencies = [
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-query"
|
||||
version = "1.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a"
|
||||
dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-wincon"
|
||||
version = "3.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
|
||||
|
||||
[[package]]
|
||||
name = "env_filter"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab"
|
||||
dependencies = [
|
||||
"log",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
version = "0.11.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"env_filter",
|
||||
"humantime",
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.14.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hbbytecode"
|
||||
|
@ -128,9 +20,8 @@ version = "0.1.0"
|
|||
name = "hblang"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"env_logger",
|
||||
"hbbytecode",
|
||||
"hbvm",
|
||||
"log",
|
||||
"regalloc2",
|
||||
]
|
||||
|
||||
|
@ -149,36 +40,12 @@ dependencies = [
|
|||
"memmap2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
||||
|
||||
[[package]]
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.70.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.158"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||
|
||||
[[package]]
|
||||
name = "memmap2"
|
||||
version = "0.9.5"
|
||||
|
@ -188,212 +55,28 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.86"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.37"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regalloc2"
|
||||
version = "0.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "12908dbeb234370af84d0579b9f68258a0f67e201412dd9a2814e6f45b2fc0f0"
|
||||
source = "git+https://github.com/jakubDoka/regalloc2#7e74b2fde4f022816cded93ab5685e46f8e3a159"
|
||||
dependencies = [
|
||||
"hashbrown",
|
||||
"log",
|
||||
"rustc-hash",
|
||||
"slice-group-by",
|
||||
"smallvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.10.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152"
|
||||
|
||||
[[package]]
|
||||
name = "slice-group-by"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "826167069c09b99d56f31e9ae5c99049e932a98c9dc2dac47645b08dbbf76ba7"
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.77"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.52.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
||||
dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm",
|
||||
"windows_aarch64_msvc",
|
||||
"windows_i686_gnu",
|
||||
"windows_i686_gnullvm",
|
||||
"windows_i686_msvc",
|
||||
"windows_x86_64_gnu",
|
||||
"windows_x86_64_gnullvm",
|
||||
"windows_x86_64_msvc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||
|
||||
[[package]]
|
||||
name = "xtask"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.7.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.7.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
|
|
@ -2,3 +2,8 @@
|
|||
name = "hbbytecode"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
[features]
|
||||
default = ["disasm"]
|
||||
std = []
|
||||
disasm = ["std"]
|
||||
|
|
|
@ -1,58 +1,208 @@
|
|||
#![feature(iter_next_chunk)]
|
||||
|
||||
use std::{collections::HashSet, fmt::Write};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
println!("cargo:rerun-if-changed=instructions.in");
|
||||
|
||||
let mut generated = String::new();
|
||||
gen_op_structs(&mut generated)?;
|
||||
std::fs::write("src/ops.rs", generated)?;
|
||||
|
||||
let mut generated = String::new();
|
||||
gen_op_codes(&mut generated)?;
|
||||
std::fs::write("src/opcode.rs", generated)?;
|
||||
gen_instrs(&mut generated)?;
|
||||
std::fs::write("src/instrs.rs", generated)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn gen_op_structs(generated: &mut String) -> std::fmt::Result {
|
||||
use std::fmt::Write;
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
fn gen_instrs(generated: &mut String) -> Result<(), Box<dyn std::error::Error>> {
|
||||
writeln!(generated, "#![allow(dead_code)] #![allow(clippy::upper_case_acronyms)]")?;
|
||||
writeln!(generated, "use crate::*;")?;
|
||||
for [.., args, _] in instructions() {
|
||||
if !seen.insert(args) {
|
||||
continue;
|
||||
}
|
||||
|
||||
writeln!(generated, "#[derive(Clone, Copy, Debug)]")?;
|
||||
writeln!(generated, "#[repr(packed)]")?;
|
||||
write!(generated, "pub struct Ops{args}(")?;
|
||||
let mut first = true;
|
||||
for ch in args.chars().filter(|&ch| ch != 'N') {
|
||||
if !std::mem::take(&mut first) {
|
||||
write!(generated, ",")?;
|
||||
'_opcode_structs: {
|
||||
let mut seen = HashSet::new();
|
||||
for [.., args, _] in instructions() {
|
||||
if !seen.insert(args) {
|
||||
continue;
|
||||
}
|
||||
write!(generated, "pub Op{ch}")?;
|
||||
|
||||
writeln!(generated, "#[derive(Clone, Copy, Debug)]")?;
|
||||
writeln!(generated, "#[repr(packed)]")?;
|
||||
write!(generated, "pub struct Ops{args}(")?;
|
||||
let mut first = true;
|
||||
for ch in args.chars().filter(|&ch| ch != 'N') {
|
||||
if !std::mem::take(&mut first) {
|
||||
write!(generated, ",")?;
|
||||
}
|
||||
write!(generated, "pub Op{ch}")?;
|
||||
}
|
||||
writeln!(generated, ");")?;
|
||||
writeln!(generated, "unsafe impl BytecodeItem for Ops{args} {{}}")?;
|
||||
}
|
||||
writeln!(generated, ");")?;
|
||||
writeln!(generated, "unsafe impl BytecodeItem for Ops{args} {{}}")?;
|
||||
}
|
||||
|
||||
'_max_size: {
|
||||
let max = instructions()
|
||||
.map(
|
||||
|[_, _, ty, _]| {
|
||||
if ty == "N" {
|
||||
1
|
||||
} else {
|
||||
iter_args(ty).map(arg_to_width).sum::<usize>() + 1
|
||||
}
|
||||
},
|
||||
)
|
||||
.max()
|
||||
.unwrap();
|
||||
|
||||
writeln!(generated, "pub const MAX_SIZE: usize = {max};")?;
|
||||
}
|
||||
|
||||
'_encoders: {
|
||||
for [op, name, ty, doc] in instructions() {
|
||||
writeln!(generated, "/// {}", doc.trim_matches('"'))?;
|
||||
let name = name.to_lowercase();
|
||||
let args = comma_sep(
|
||||
iter_args(ty)
|
||||
.enumerate()
|
||||
.map(|(i, c)| format!("{}{i}: {}", arg_to_name(c), arg_to_type(c))),
|
||||
);
|
||||
writeln!(generated, "pub fn {name}({args}) -> (usize, [u8; MAX_SIZE]) {{")?;
|
||||
let arg_names =
|
||||
comma_sep(iter_args(ty).enumerate().map(|(i, c)| format!("{}{i}", arg_to_name(c))));
|
||||
writeln!(generated, " unsafe {{ crate::encode({ty}({op}, {arg_names})) }}")?;
|
||||
writeln!(generated, "}}")?;
|
||||
}
|
||||
}
|
||||
|
||||
'_structs: {
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for [_, _, ty, _] in instructions() {
|
||||
if !seen.insert(ty) {
|
||||
continue;
|
||||
}
|
||||
let types = comma_sep(iter_args(ty).map(arg_to_type).map(|s| s.to_string()));
|
||||
writeln!(generated, "#[repr(packed)] pub struct {ty}(u8, {types});")?;
|
||||
}
|
||||
}
|
||||
|
||||
'_name_list: {
|
||||
writeln!(generated, "pub const NAMES: [&str; {}] = [", instructions().count())?;
|
||||
for [_, name, _, _] in instructions() {
|
||||
writeln!(generated, " \"{}\",", name.to_lowercase())?;
|
||||
}
|
||||
writeln!(generated, "];")?;
|
||||
}
|
||||
|
||||
let instr = "Instr";
|
||||
let oper = "Oper";
|
||||
|
||||
'_instr_enum: {
|
||||
writeln!(generated, "#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(u8)]")?;
|
||||
writeln!(generated, "pub enum {instr} {{")?;
|
||||
for [id, name, ..] in instructions() {
|
||||
writeln!(generated, " {name} = {id},")?;
|
||||
}
|
||||
writeln!(generated, "}}")?;
|
||||
}
|
||||
|
||||
'_arg_kind: {
|
||||
writeln!(generated, "#[derive(Debug, Clone, Copy, PartialEq, Eq)]")?;
|
||||
writeln!(generated, "pub enum {oper} {{")?;
|
||||
let mut seen = HashSet::new();
|
||||
for ty in instructions().flat_map(|[.., ty, _]| iter_args(ty)) {
|
||||
if !seen.insert(ty) {
|
||||
continue;
|
||||
}
|
||||
writeln!(generated, " {ty}({}),", arg_to_type(ty))?;
|
||||
}
|
||||
writeln!(generated, "}}")?;
|
||||
}
|
||||
|
||||
'_parse_opers: {
|
||||
writeln!(
|
||||
generated,
|
||||
"/// This assumes the instruction byte is still at the beginning of the buffer"
|
||||
)?;
|
||||
writeln!(generated, "#[cfg(feature = \"disasm\")]")?;
|
||||
writeln!(generated, "pub fn parse_args(bytes: &mut &[u8], kind: {instr}, buf: &mut std::vec::Vec<{oper}>) -> Option<()> {{")?;
|
||||
writeln!(generated, " match kind {{")?;
|
||||
let mut instrs = instructions().collect::<Vec<_>>();
|
||||
instrs.sort_unstable_by_key(|&[.., ty, _]| ty);
|
||||
for group in instrs.chunk_by(|[.., a, _], [.., b, _]| a == b) {
|
||||
let ty = group[0][2];
|
||||
for &[_, name, ..] in group {
|
||||
writeln!(generated, " | {instr}::{name}")?;
|
||||
}
|
||||
generated.pop();
|
||||
writeln!(generated, " => {{")?;
|
||||
if iter_args(ty).count() != 0 {
|
||||
writeln!(generated, " let data = crate::decode::<{ty}>(bytes)?;")?;
|
||||
writeln!(
|
||||
generated,
|
||||
" buf.extend([{}]);",
|
||||
comma_sep(
|
||||
iter_args(ty).zip(1u32..).map(|(t, i)| format!("{oper}::{t}(data.{i})"))
|
||||
)
|
||||
)?;
|
||||
} else {
|
||||
writeln!(generated, " crate::decode::<{ty}>(bytes)?;")?;
|
||||
}
|
||||
|
||||
writeln!(generated, " }}")?;
|
||||
}
|
||||
writeln!(generated, " }}")?;
|
||||
writeln!(generated, " Some(())")?;
|
||||
writeln!(generated, "}}")?;
|
||||
}
|
||||
|
||||
std::fs::write("src/instrs.rs", generated)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn gen_op_codes(generated: &mut String) -> std::fmt::Result {
|
||||
use std::fmt::Write;
|
||||
for [op, name, _, comment] in instructions() {
|
||||
writeln!(generated, "#[doc = {comment}]")?;
|
||||
writeln!(generated, "pub const {name}: u8 = {op};")?;
|
||||
}
|
||||
Ok(())
|
||||
fn comma_sep(items: impl Iterator<Item = String>) -> String {
|
||||
items.map(|item| item.to_string()).collect::<Vec<_>>().join(", ")
|
||||
}
|
||||
|
||||
fn instructions() -> impl Iterator<Item = [&'static str; 4]> {
|
||||
include_str!("../hbbytecode/instructions.in")
|
||||
.lines()
|
||||
.map(|line| line.strip_suffix(';').unwrap())
|
||||
.filter_map(|line| line.strip_suffix(';'))
|
||||
.map(|line| line.splitn(4, ',').map(str::trim).next_chunk().unwrap())
|
||||
}
|
||||
|
||||
fn arg_to_type(arg: char) -> &'static str {
|
||||
match arg {
|
||||
'R' | 'B' => "u8",
|
||||
'H' => "u16",
|
||||
'W' => "u32",
|
||||
'D' | 'A' => "u64",
|
||||
'P' => "i16",
|
||||
'O' => "i32",
|
||||
_ => panic!("unknown type: {}", arg),
|
||||
}
|
||||
}
|
||||
|
||||
fn arg_to_width(arg: char) -> usize {
|
||||
match arg {
|
||||
'R' | 'B' => 1,
|
||||
'H' => 2,
|
||||
'W' => 4,
|
||||
'D' | 'A' => 8,
|
||||
'P' => 2,
|
||||
'O' => 4,
|
||||
_ => panic!("unknown type: {}", arg),
|
||||
}
|
||||
}
|
||||
|
||||
fn arg_to_name(arg: char) -> &'static str {
|
||||
match arg {
|
||||
'R' => "reg",
|
||||
'B' | 'H' | 'W' | 'D' => "imm",
|
||||
'P' | 'O' => "offset",
|
||||
'A' => "addr",
|
||||
_ => panic!("unknown type: {}", arg),
|
||||
}
|
||||
}
|
||||
|
||||
fn iter_args(ty: &'static str) -> impl Iterator<Item = char> {
|
||||
ty.chars().filter(|c| *c != 'N')
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
#![no_std]
|
||||
|
||||
pub use crate::ops::*;
|
||||
#[cfg(feature = "std")]
|
||||
extern crate std;
|
||||
|
||||
pub use crate::instrs::*;
|
||||
use core::convert::TryFrom;
|
||||
|
||||
pub mod opcode;
|
||||
mod ops;
|
||||
mod instrs;
|
||||
|
||||
type OpR = u8;
|
||||
|
||||
|
@ -22,6 +24,38 @@ type OpD = u64;
|
|||
pub unsafe trait BytecodeItem {}
|
||||
unsafe impl BytecodeItem for u8 {}
|
||||
|
||||
impl TryFrom<u8> for Instr {
|
||||
type Error = u8;
|
||||
|
||||
#[inline]
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
#[cold]
|
||||
fn failed(value: u8) -> Result<Instr, u8> {
|
||||
Err(value)
|
||||
}
|
||||
|
||||
if value < NAMES.len() as u8 {
|
||||
unsafe { Ok(std::mem::transmute::<u8, Instr>(value)) }
|
||||
} else {
|
||||
failed(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn encode<T>(instr: T) -> (usize, [u8; instrs::MAX_SIZE]) {
|
||||
let mut buf = [0; instrs::MAX_SIZE];
|
||||
core::ptr::write(buf.as_mut_ptr() as *mut T, instr);
|
||||
(core::mem::size_of::<T>(), buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn decode<T>(binary: &mut &[u8]) -> Option<T> {
|
||||
let (front, rest) = std::mem::take(binary).split_at_checked(core::mem::size_of::<T>())?;
|
||||
*binary = rest;
|
||||
unsafe { Some(core::ptr::read(front.as_ptr() as *const T)) }
|
||||
}
|
||||
|
||||
/// Rounding mode
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
|
@ -39,3 +73,181 @@ impl TryFrom<u8> for RoundingMode {
|
|||
(value <= 3).then(|| unsafe { core::mem::transmute(value) }).ok_or(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "disasm")]
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum DisasmItem {
|
||||
Func,
|
||||
Global,
|
||||
}
|
||||
|
||||
#[cfg(feature = "disasm")]
|
||||
pub fn disasm(
|
||||
binary: &mut &[u8],
|
||||
functions: &std::collections::BTreeMap<u32, (&str, u32, DisasmItem)>,
|
||||
out: &mut impl std::io::Write,
|
||||
mut eca_handler: impl FnMut(&mut &[u8]),
|
||||
) -> std::io::Result<()> {
|
||||
use {
|
||||
self::instrs::Instr,
|
||||
std::{
|
||||
collections::{hash_map::Entry, HashMap},
|
||||
convert::TryInto,
|
||||
vec::Vec,
|
||||
},
|
||||
};
|
||||
|
||||
fn instr_from_byte(b: u8) -> std::io::Result<Instr> {
|
||||
if b as usize >= instrs::NAMES.len() {
|
||||
return Err(std::io::ErrorKind::InvalidData.into());
|
||||
}
|
||||
Ok(unsafe { std::mem::transmute::<u8, Instr>(b) })
|
||||
}
|
||||
|
||||
let mut labels = HashMap::<u32, u32>::default();
|
||||
let mut buf = Vec::<instrs::Oper>::new();
|
||||
let mut has_cycle = false;
|
||||
let mut has_oob = false;
|
||||
|
||||
'_offset_pass: for (&off, &(_name, len, kind)) in functions.iter() {
|
||||
if matches!(kind, DisasmItem::Global) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let prev = *binary;
|
||||
|
||||
*binary = &binary[..off as usize];
|
||||
|
||||
let mut label_count = 0;
|
||||
while let Some(&byte) = binary.first() {
|
||||
let offset: i32 = (prev.len() - binary.len()).try_into().unwrap();
|
||||
if offset as u32 == off + len {
|
||||
break;
|
||||
}
|
||||
let Ok(inst) = instr_from_byte(byte) else { break };
|
||||
instrs::parse_args(binary, inst, &mut buf).ok_or(std::io::ErrorKind::OutOfMemory)?;
|
||||
|
||||
for op in buf.drain(..) {
|
||||
let rel = match op {
|
||||
instrs::Oper::O(rel) => rel,
|
||||
instrs::Oper::P(rel) => rel.into(),
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
has_cycle |= rel == 0;
|
||||
|
||||
let global_offset: u32 = (offset + rel).try_into().unwrap();
|
||||
if functions.get(&global_offset).is_some() {
|
||||
continue;
|
||||
}
|
||||
label_count += match labels.entry(global_offset) {
|
||||
Entry::Occupied(_) => 0,
|
||||
Entry::Vacant(entry) => {
|
||||
entry.insert(label_count);
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if matches!(inst, Instr::ECA) {
|
||||
eca_handler(binary);
|
||||
}
|
||||
}
|
||||
|
||||
*binary = prev;
|
||||
}
|
||||
|
||||
let mut ordered = functions.iter().collect::<Vec<_>>();
|
||||
ordered.sort_unstable_by_key(|(_, (name, _, _))| name);
|
||||
|
||||
'_dump: for (&off, &(name, len, kind)) in ordered {
|
||||
if matches!(kind, DisasmItem::Global) {
|
||||
continue;
|
||||
}
|
||||
let prev = *binary;
|
||||
|
||||
writeln!(out, "{name}:")?;
|
||||
|
||||
*binary = &binary[..off as usize];
|
||||
while let Some(&byte) = binary.first() {
|
||||
let offset: i32 = (prev.len() - binary.len()).try_into().unwrap();
|
||||
if offset as u32 == off + len {
|
||||
break;
|
||||
}
|
||||
let Ok(inst) = instr_from_byte(byte) else {
|
||||
writeln!(out, "invalid instr {byte}")?;
|
||||
break;
|
||||
};
|
||||
instrs::parse_args(binary, inst, &mut buf).unwrap();
|
||||
|
||||
if let Some(label) = labels.get(&offset.try_into().unwrap()) {
|
||||
write!(out, "{:>2}: ", label)?;
|
||||
} else {
|
||||
write!(out, " ")?;
|
||||
}
|
||||
|
||||
write!(out, "{inst:<8?} ")?;
|
||||
|
||||
'a: for (i, op) in buf.drain(..).enumerate() {
|
||||
if i != 0 {
|
||||
write!(out, ", ")?;
|
||||
}
|
||||
|
||||
let rel = 'b: {
|
||||
match op {
|
||||
instrs::Oper::O(rel) => break 'b rel,
|
||||
instrs::Oper::P(rel) => break 'b rel.into(),
|
||||
instrs::Oper::R(r) => write!(out, "r{r}")?,
|
||||
instrs::Oper::B(b) => write!(out, "{b}b")?,
|
||||
instrs::Oper::H(h) => write!(out, "{h}h")?,
|
||||
instrs::Oper::W(w) => write!(out, "{w}w")?,
|
||||
instrs::Oper::D(d) if (d as i64) < 0 => write!(out, "{}d", d as i64)?,
|
||||
instrs::Oper::D(d) => write!(out, "{d}d")?,
|
||||
instrs::Oper::A(a) => write!(out, "{a}a")?,
|
||||
}
|
||||
|
||||
continue 'a;
|
||||
};
|
||||
|
||||
let global_offset: u32 = (offset + rel).try_into().unwrap();
|
||||
if let Some(&(name, ..)) = functions.get(&global_offset) {
|
||||
if name.contains('\0') {
|
||||
write!(out, ":{name:?}")?;
|
||||
} else {
|
||||
write!(out, ":{name}")?;
|
||||
}
|
||||
} else {
|
||||
let local_has_oob = global_offset < off
|
||||
|| global_offset > off + len
|
||||
|| instr_from_byte(prev[global_offset as usize]).is_err()
|
||||
|| prev[global_offset as usize] == 0;
|
||||
has_oob |= local_has_oob;
|
||||
let label = labels.get(&global_offset).unwrap();
|
||||
if local_has_oob {
|
||||
write!(out, "!!!!!!!!!{rel}")?;
|
||||
} else {
|
||||
write!(out, ":{label}")?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
writeln!(out)?;
|
||||
|
||||
if matches!(inst, Instr::ECA) {
|
||||
eca_handler(binary);
|
||||
}
|
||||
}
|
||||
|
||||
*binary = prev;
|
||||
}
|
||||
|
||||
if has_oob {
|
||||
return Err(std::io::ErrorKind::InvalidInput.into());
|
||||
}
|
||||
|
||||
if has_cycle {
|
||||
return Err(std::io::ErrorKind::TimedOut.into());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ name = "hbc"
|
|||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
env_logger = "0.11.5"
|
||||
hbbytecode = { version = "0.1.0", path = "../hbbytecode" }
|
||||
hbvm = { path = "../hbvm", features = ["nightly"] }
|
||||
log = "0.4.22"
|
||||
regalloc2 = { version = "0.10.2", features = ["trace-log"] }
|
||||
regalloc2 = { git = "https://github.com/jakubDoka/regalloc2" }
|
||||
|
|
183
hblang/build.rs
183
hblang/build.rs
|
@ -1,183 +0,0 @@
|
|||
#![feature(iter_next_chunk)]
|
||||
use std::{collections::HashSet, fmt::Write};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
println!("cargo:rerun-if-changed=../hbbytecode/instructions.in");
|
||||
|
||||
gen_instrs()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn gen_instrs() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut generated = String::new();
|
||||
|
||||
writeln!(generated, "#![allow(dead_code)] #![allow(clippy::upper_case_acronyms)]")?;
|
||||
|
||||
'_max_size: {
|
||||
let max = instructions()
|
||||
.map(
|
||||
|[_, _, ty, _]| {
|
||||
if ty == "N" {
|
||||
1
|
||||
} else {
|
||||
iter_args(ty).map(arg_to_width).sum::<usize>() + 1
|
||||
}
|
||||
},
|
||||
)
|
||||
.max()
|
||||
.unwrap();
|
||||
|
||||
writeln!(generated, "pub const MAX_SIZE: usize = {max};")?;
|
||||
}
|
||||
|
||||
'_encoders: {
|
||||
for [op, name, ty, doc] in instructions() {
|
||||
writeln!(generated, "/// {}", doc.trim_matches('"'))?;
|
||||
let name = name.to_lowercase();
|
||||
let args = comma_sep(
|
||||
iter_args(ty)
|
||||
.enumerate()
|
||||
.map(|(i, c)| format!("{}{i}: {}", arg_to_name(c), arg_to_type(c))),
|
||||
);
|
||||
writeln!(generated, "pub fn {name}({args}) -> (usize, [u8; MAX_SIZE]) {{")?;
|
||||
let arg_names =
|
||||
comma_sep(iter_args(ty).enumerate().map(|(i, c)| format!("{}{i}", arg_to_name(c))));
|
||||
writeln!(generated, " unsafe {{ crate::encode({ty}({op}, {arg_names})) }}")?;
|
||||
writeln!(generated, "}}")?;
|
||||
}
|
||||
}
|
||||
|
||||
'_structs: {
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for [_, _, ty, _] in instructions() {
|
||||
if !seen.insert(ty) {
|
||||
continue;
|
||||
}
|
||||
let types = comma_sep(iter_args(ty).map(arg_to_type).map(|s| s.to_string()));
|
||||
writeln!(generated, "#[repr(packed)] pub struct {ty}(u8, {types});")?;
|
||||
}
|
||||
}
|
||||
|
||||
'_name_list: {
|
||||
writeln!(generated, "pub const NAMES: [&str; {}] = [", instructions().count())?;
|
||||
for [_, name, _, _] in instructions() {
|
||||
writeln!(generated, " \"{}\",", name.to_lowercase())?;
|
||||
}
|
||||
writeln!(generated, "];")?;
|
||||
}
|
||||
|
||||
let instr = "Instr";
|
||||
let oper = "Oper";
|
||||
|
||||
'_instr_enum: {
|
||||
writeln!(generated, "#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(u8)]")?;
|
||||
writeln!(generated, "pub enum {instr} {{")?;
|
||||
for [id, name, ..] in instructions() {
|
||||
writeln!(generated, " {name} = {id},")?;
|
||||
}
|
||||
writeln!(generated, "}}")?;
|
||||
}
|
||||
|
||||
'_arg_kind: {
|
||||
writeln!(generated, "#[derive(Debug, Clone, Copy, PartialEq, Eq)]")?;
|
||||
writeln!(generated, "pub enum {oper} {{")?;
|
||||
let mut seen = HashSet::new();
|
||||
for ty in instructions().flat_map(|[.., ty, _]| iter_args(ty)) {
|
||||
if !seen.insert(ty) {
|
||||
continue;
|
||||
}
|
||||
writeln!(generated, " {ty}({}),", arg_to_type(ty))?;
|
||||
}
|
||||
writeln!(generated, "}}")?;
|
||||
}
|
||||
|
||||
'_parse_opers: {
|
||||
writeln!(
|
||||
generated,
|
||||
"/// This assumes the instruction byte is still at the beginning of the buffer"
|
||||
)?;
|
||||
writeln!(generated, "pub fn parse_args(bytes: &mut &[u8], kind: {instr}, buf: &mut Vec<{oper}>) -> Option<()> {{")?;
|
||||
writeln!(generated, " match kind {{")?;
|
||||
let mut instrs = instructions().collect::<Vec<_>>();
|
||||
instrs.sort_unstable_by_key(|&[.., ty, _]| ty);
|
||||
for group in instrs.chunk_by(|[.., a, _], [.., b, _]| a == b) {
|
||||
let ty = group[0][2];
|
||||
for &[_, name, ..] in group {
|
||||
writeln!(generated, " | {instr}::{name}")?;
|
||||
}
|
||||
generated.pop();
|
||||
writeln!(generated, " => {{")?;
|
||||
if iter_args(ty).count() != 0 {
|
||||
writeln!(generated, " let data = crate::decode::<{ty}>(bytes)?;")?;
|
||||
writeln!(
|
||||
generated,
|
||||
" buf.extend([{}]);",
|
||||
comma_sep(
|
||||
iter_args(ty).zip(1u32..).map(|(t, i)| format!("{oper}::{t}(data.{i})"))
|
||||
)
|
||||
)?;
|
||||
} else {
|
||||
writeln!(generated, " crate::decode::<{ty}>(bytes)?;")?;
|
||||
}
|
||||
|
||||
writeln!(generated, " }}")?;
|
||||
}
|
||||
writeln!(generated, " }}")?;
|
||||
writeln!(generated, " Some(())")?;
|
||||
writeln!(generated, "}}")?;
|
||||
}
|
||||
|
||||
std::fs::write("src/instrs.rs", generated)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn comma_sep(items: impl Iterator<Item = String>) -> String {
|
||||
items.map(|item| item.to_string()).collect::<Vec<_>>().join(", ")
|
||||
}
|
||||
|
||||
fn instructions() -> impl Iterator<Item = [&'static str; 4]> {
|
||||
include_str!("../hbbytecode/instructions.in")
|
||||
.lines()
|
||||
.filter_map(|line| line.strip_suffix(';'))
|
||||
.map(|line| line.splitn(4, ',').map(str::trim).next_chunk().unwrap())
|
||||
}
|
||||
|
||||
fn arg_to_type(arg: char) -> &'static str {
|
||||
match arg {
|
||||
'R' | 'B' => "u8",
|
||||
'H' => "u16",
|
||||
'W' => "u32",
|
||||
'D' | 'A' => "u64",
|
||||
'P' => "i16",
|
||||
'O' => "i32",
|
||||
_ => panic!("unknown type: {}", arg),
|
||||
}
|
||||
}
|
||||
|
||||
fn arg_to_width(arg: char) -> usize {
|
||||
match arg {
|
||||
'R' | 'B' => 1,
|
||||
'H' => 2,
|
||||
'W' => 4,
|
||||
'D' | 'A' => 8,
|
||||
'P' => 2,
|
||||
'O' => 4,
|
||||
_ => panic!("unknown type: {}", arg),
|
||||
}
|
||||
}
|
||||
|
||||
fn arg_to_name(arg: char) -> &'static str {
|
||||
match arg {
|
||||
'R' => "reg",
|
||||
'B' | 'H' | 'W' | 'D' => "imm",
|
||||
'P' | 'O' => "offset",
|
||||
'A' => "addr",
|
||||
_ => panic!("unknown type: {}", arg),
|
||||
}
|
||||
}
|
||||
|
||||
fn iter_args(ty: &'static str) -> impl Iterator<Item = char> {
|
||||
ty.chars().filter(|c| *c != 'N')
|
||||
}
|
|
@ -27,6 +27,7 @@ use {
|
|||
son::reg,
|
||||
ty::ArrayLen,
|
||||
},
|
||||
hbbytecode as instrs,
|
||||
parser::Ast,
|
||||
std::{
|
||||
collections::{hash_map, BTreeMap, VecDeque},
|
||||
|
@ -52,7 +53,6 @@ pub mod codegen;
|
|||
pub mod parser;
|
||||
pub mod son;
|
||||
|
||||
mod instrs;
|
||||
mod lexer;
|
||||
|
||||
mod task {
|
||||
|
@ -715,7 +715,7 @@ impl Types {
|
|||
output: &mut impl std::io::Write,
|
||||
eca_handler: impl FnMut(&mut &[u8]),
|
||||
) -> std::io::Result<()> {
|
||||
use crate::DisasmItem;
|
||||
use instrs::DisasmItem;
|
||||
let functions = self
|
||||
.funcs
|
||||
.iter()
|
||||
|
@ -744,7 +744,7 @@ impl Types {
|
|||
(g.offset, (name, g.data.len() as Size, DisasmItem::Global))
|
||||
}))
|
||||
.collect::<BTreeMap<_, _>>();
|
||||
crate::disasm(&mut sluce, &functions, output, eca_handler)
|
||||
instrs::disasm(&mut sluce, &functions, output, eca_handler)
|
||||
}
|
||||
|
||||
fn parama(&self, ret: impl Into<ty::Id>) -> ParamAlloc {
|
||||
|
@ -857,181 +857,6 @@ impl Types {
|
|||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn encode<T>(instr: T) -> (usize, [u8; instrs::MAX_SIZE]) {
|
||||
let mut buf = [0; instrs::MAX_SIZE];
|
||||
std::ptr::write(buf.as_mut_ptr() as *mut T, instr);
|
||||
(std::mem::size_of::<T>(), buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn decode<T>(binary: &mut &[u8]) -> Option<T> {
|
||||
unsafe { Some(std::ptr::read(binary.take(..std::mem::size_of::<T>())?.as_ptr() as *const T)) }
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum DisasmItem {
|
||||
Func,
|
||||
Global,
|
||||
}
|
||||
|
||||
fn disasm(
|
||||
binary: &mut &[u8],
|
||||
functions: &BTreeMap<u32, (&str, u32, DisasmItem)>,
|
||||
out: &mut impl std::io::Write,
|
||||
mut eca_handler: impl FnMut(&mut &[u8]),
|
||||
) -> std::io::Result<()> {
|
||||
use self::instrs::Instr;
|
||||
|
||||
fn instr_from_byte(b: u8) -> std::io::Result<Instr> {
|
||||
if b as usize >= instrs::NAMES.len() {
|
||||
return Err(std::io::ErrorKind::InvalidData.into());
|
||||
}
|
||||
Ok(unsafe { std::mem::transmute::<u8, Instr>(b) })
|
||||
}
|
||||
|
||||
let mut labels = HashMap::<u32, u32>::default();
|
||||
let mut buf = Vec::<instrs::Oper>::new();
|
||||
let mut has_cycle = false;
|
||||
let mut has_oob = false;
|
||||
|
||||
'_offset_pass: for (&off, &(_name, len, kind)) in functions.iter() {
|
||||
if matches!(kind, DisasmItem::Global) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let prev = *binary;
|
||||
|
||||
binary.take(..off as usize).unwrap();
|
||||
|
||||
let mut label_count = 0;
|
||||
while let Some(&byte) = binary.first() {
|
||||
let offset: i32 = (prev.len() - binary.len()).try_into().unwrap();
|
||||
if offset as u32 == off + len {
|
||||
break;
|
||||
}
|
||||
let Ok(inst) = instr_from_byte(byte) else { break };
|
||||
instrs::parse_args(binary, inst, &mut buf).ok_or(std::io::ErrorKind::OutOfMemory)?;
|
||||
|
||||
for op in buf.drain(..) {
|
||||
let rel = match op {
|
||||
instrs::Oper::O(rel) => rel,
|
||||
instrs::Oper::P(rel) => rel.into(),
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
has_cycle |= rel == 0;
|
||||
|
||||
let global_offset: u32 = (offset + rel).try_into().unwrap();
|
||||
if functions.get(&global_offset).is_some() {
|
||||
continue;
|
||||
}
|
||||
label_count += labels.try_insert(global_offset, label_count).is_ok() as u32;
|
||||
}
|
||||
|
||||
if matches!(inst, Instr::ECA) {
|
||||
eca_handler(binary);
|
||||
}
|
||||
}
|
||||
|
||||
*binary = prev;
|
||||
}
|
||||
|
||||
let mut ordered = functions.iter().collect::<Vec<_>>();
|
||||
ordered.sort_unstable_by_key(|(_, (name, _, _))| name);
|
||||
|
||||
'_dump: for (&off, &(name, len, kind)) in ordered {
|
||||
if matches!(kind, DisasmItem::Global) {
|
||||
continue;
|
||||
}
|
||||
let prev = *binary;
|
||||
|
||||
writeln!(out, "{name}:")?;
|
||||
|
||||
binary.take(..off as usize).unwrap();
|
||||
while let Some(&byte) = binary.first() {
|
||||
let offset: i32 = (prev.len() - binary.len()).try_into().unwrap();
|
||||
if offset as u32 == off + len {
|
||||
break;
|
||||
}
|
||||
let Ok(inst) = instr_from_byte(byte) else {
|
||||
writeln!(out, "invalid instr {byte}")?;
|
||||
break;
|
||||
};
|
||||
instrs::parse_args(binary, inst, &mut buf).unwrap();
|
||||
|
||||
if let Some(label) = labels.get(&offset.try_into().unwrap()) {
|
||||
write!(out, "{:>2}: ", label)?;
|
||||
} else {
|
||||
write!(out, " ")?;
|
||||
}
|
||||
|
||||
write!(out, "{inst:<8?} ")?;
|
||||
|
||||
'a: for (i, op) in buf.drain(..).enumerate() {
|
||||
if i != 0 {
|
||||
write!(out, ", ")?;
|
||||
}
|
||||
|
||||
let rel = 'b: {
|
||||
match op {
|
||||
instrs::Oper::O(rel) => break 'b rel,
|
||||
instrs::Oper::P(rel) => break 'b rel.into(),
|
||||
instrs::Oper::R(r) => write!(out, "r{r}")?,
|
||||
instrs::Oper::B(b) => write!(out, "{b}b")?,
|
||||
instrs::Oper::H(h) => write!(out, "{h}h")?,
|
||||
instrs::Oper::W(w) => write!(out, "{w}w")?,
|
||||
instrs::Oper::D(d) if (d as i64) < 0 => write!(out, "{}d", d as i64)?,
|
||||
instrs::Oper::D(d) => write!(out, "{d}d")?,
|
||||
instrs::Oper::A(a) => write!(out, "{a}a")?,
|
||||
}
|
||||
|
||||
continue 'a;
|
||||
};
|
||||
|
||||
let global_offset: u32 = (offset + rel).try_into().unwrap();
|
||||
if let Some(&(name, ..)) = functions.get(&global_offset) {
|
||||
if name.contains('\0') {
|
||||
write!(out, ":{name:?}")?;
|
||||
} else {
|
||||
write!(out, ":{name}")?;
|
||||
}
|
||||
} else {
|
||||
let local_has_oob = global_offset < off
|
||||
|| global_offset > off + len
|
||||
|| instr_from_byte(prev[global_offset as usize]).is_err()
|
||||
|| prev[global_offset as usize] == 0;
|
||||
has_oob |= local_has_oob;
|
||||
let label = labels.get(&global_offset).unwrap();
|
||||
if local_has_oob {
|
||||
write!(out, "!!!!!!!!!{rel}")?;
|
||||
} else {
|
||||
write!(out, ":{label}")?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
writeln!(out)?;
|
||||
|
||||
if matches!(inst, Instr::ECA) {
|
||||
eca_handler(binary);
|
||||
}
|
||||
}
|
||||
|
||||
*binary = prev;
|
||||
}
|
||||
|
||||
if has_oob {
|
||||
return Err(std::io::ErrorKind::InvalidInput.into());
|
||||
}
|
||||
|
||||
if has_cycle {
|
||||
return Err(std::io::ErrorKind::TimedOut.into());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct TaskQueue<T> {
|
||||
inner: Mutex<TaskQueueInner<T>>,
|
||||
}
|
||||
|
|
|
@ -1712,8 +1712,7 @@ impl Codegen {
|
|||
self.ci.emit(instrs::st(reg::RET_ADDR, reg::STACK_PTR, 0, 0));
|
||||
}
|
||||
|
||||
self.ci.nodes.basic_blocks();
|
||||
|
||||
//self.ci.nodes.basic_blocks();
|
||||
//self.ci.nodes.graphviz();
|
||||
|
||||
self.ci.vars = orig_vars;
|
||||
|
@ -1761,412 +1760,6 @@ impl Codegen {
|
|||
}
|
||||
|
||||
fn emit_body(&mut self, sig: Sig) -> usize {
|
||||
// FIXME: make this more efficient (allocated with arena)
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Block {
|
||||
nid: Nid,
|
||||
preds: Vec<regalloc2::Block>,
|
||||
succs: Vec<regalloc2::Block>,
|
||||
instrs: regalloc2::InstRange,
|
||||
params: Vec<regalloc2::VReg>,
|
||||
branch_blockparams: Vec<regalloc2::VReg>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Instr {
|
||||
nid: Nid,
|
||||
ops: Vec<regalloc2::Operand>,
|
||||
}
|
||||
|
||||
struct Function<'a> {
|
||||
sig: Sig,
|
||||
nodes: &'a mut Nodes,
|
||||
tys: &'a Types,
|
||||
blocks: Vec<Block>,
|
||||
instrs: Vec<Instr>,
|
||||
}
|
||||
|
||||
impl Debug for Function<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
for (i, block) in self.blocks.iter().enumerate() {
|
||||
writeln!(f, "sb{i}{:?}-{:?}:", block.params, block.preds)?;
|
||||
|
||||
for inst in block.instrs.iter() {
|
||||
let instr = &self.instrs[inst.index()];
|
||||
writeln!(
|
||||
f,
|
||||
"{}: i{:?}:{:?}",
|
||||
inst.index(),
|
||||
self.nodes[instr.nid].kind,
|
||||
instr.ops
|
||||
)?;
|
||||
}
|
||||
|
||||
writeln!(f, "eb{i}{:?}-{:?}:", block.branch_blockparams, block.succs)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Function<'a> {
|
||||
fn new(nodes: &'a mut Nodes, tys: &'a Types, sig: Sig) -> Self {
|
||||
let mut s = Self {
|
||||
nodes,
|
||||
tys,
|
||||
sig,
|
||||
blocks: Default::default(),
|
||||
instrs: Default::default(),
|
||||
};
|
||||
s.nodes.visited.clear(s.nodes.values.len());
|
||||
s.emit_node(VOID, VOID);
|
||||
s.add_block(0);
|
||||
s.blocks.pop();
|
||||
s
|
||||
}
|
||||
|
||||
fn add_block(&mut self, nid: Nid) -> RallocBRef {
|
||||
if let Some(prev) = self.blocks.last_mut() {
|
||||
prev.instrs = regalloc2::InstRange::new(
|
||||
prev.instrs.first(),
|
||||
regalloc2::Inst::new(self.instrs.len()),
|
||||
);
|
||||
}
|
||||
|
||||
self.blocks.push(Block {
|
||||
nid,
|
||||
preds: Default::default(),
|
||||
succs: Default::default(),
|
||||
instrs: regalloc2::InstRange::new(
|
||||
regalloc2::Inst::new(self.instrs.len()),
|
||||
regalloc2::Inst::new(self.instrs.len() + 1),
|
||||
),
|
||||
params: Default::default(),
|
||||
branch_blockparams: Default::default(),
|
||||
});
|
||||
self.blocks.len() as RallocBRef - 1
|
||||
}
|
||||
|
||||
fn add_instr(&mut self, nid: Nid, ops: Vec<regalloc2::Operand>) {
|
||||
self.instrs.push(Instr { nid, ops });
|
||||
}
|
||||
|
||||
fn urg(&mut self, nid: Nid) -> regalloc2::Operand {
|
||||
regalloc2::Operand::reg_use(self.rg(nid))
|
||||
}
|
||||
|
||||
fn def_nid(&mut self, _nid: Nid) {}
|
||||
|
||||
fn drg(&mut self, nid: Nid) -> regalloc2::Operand {
|
||||
self.def_nid(nid);
|
||||
regalloc2::Operand::reg_def(self.rg(nid))
|
||||
}
|
||||
|
||||
fn rg(&self, nid: Nid) -> VReg {
|
||||
regalloc2::VReg::new(nid as _, regalloc2::RegClass::Int)
|
||||
}
|
||||
|
||||
fn emit_node(&mut self, nid: Nid, prev: Nid) {
|
||||
if matches!(self.nodes[nid].kind, Kind::Region | Kind::Loop) {
|
||||
let prev_bref = self.nodes[prev].ralloc_backref;
|
||||
let node = self.nodes[nid].clone();
|
||||
|
||||
let idx = 1 + node.inputs.iter().position(|&i| i == prev).unwrap();
|
||||
|
||||
for ph in node.outputs {
|
||||
if self.nodes[ph].kind != Kind::Phi {
|
||||
continue;
|
||||
}
|
||||
|
||||
let rg = self.rg(self.nodes[ph].inputs[idx]);
|
||||
self.blocks[prev_bref as usize].branch_blockparams.push(rg);
|
||||
}
|
||||
|
||||
self.add_instr(nid, vec![]);
|
||||
|
||||
match (self.nodes[nid].kind, self.nodes.visited.set(nid)) {
|
||||
(Kind::Loop, false) => {
|
||||
for i in node.inputs {
|
||||
self.bridge(i, nid);
|
||||
}
|
||||
return;
|
||||
}
|
||||
(Kind::Region, true) => return,
|
||||
_ => {}
|
||||
}
|
||||
} else if !self.nodes.visited.set(nid) {
|
||||
return;
|
||||
}
|
||||
|
||||
let node = self.nodes[nid].clone();
|
||||
match node.kind {
|
||||
Kind::Start => self.emit_node(node.outputs[0], VOID),
|
||||
Kind::End => {}
|
||||
Kind::If => {
|
||||
self.nodes[nid].ralloc_backref = self.nodes[prev].ralloc_backref;
|
||||
|
||||
let &[_, cond] = node.inputs.as_slice() else { unreachable!() };
|
||||
let &[mut then, mut else_] = node.outputs.as_slice() else {
|
||||
unreachable!()
|
||||
};
|
||||
|
||||
if let Kind::BinOp { op } = self.nodes[cond].kind
|
||||
&& let Some((_, swapped)) = op.cond_op(node.ty.is_signed())
|
||||
{
|
||||
if swapped {
|
||||
std::mem::swap(&mut then, &mut else_);
|
||||
}
|
||||
let &[_, lhs, rhs] = self.nodes[cond].inputs.as_slice() else {
|
||||
unreachable!()
|
||||
};
|
||||
let ops = vec![self.urg(lhs), self.urg(rhs)];
|
||||
self.add_instr(nid, ops);
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
|
||||
self.emit_node(then, nid);
|
||||
self.emit_node(else_, nid);
|
||||
}
|
||||
Kind::Region | Kind::Loop => {
|
||||
self.nodes[nid].ralloc_backref = self.add_block(nid);
|
||||
if node.kind == Kind::Region {
|
||||
for i in node.inputs {
|
||||
self.bridge(i, nid);
|
||||
}
|
||||
}
|
||||
let mut block = vec![];
|
||||
for ph in node.outputs.clone() {
|
||||
if self.nodes[ph].kind != Kind::Phi {
|
||||
continue;
|
||||
}
|
||||
self.def_nid(ph);
|
||||
block.push(self.rg(ph));
|
||||
}
|
||||
self.blocks[self.nodes[nid].ralloc_backref as usize].params = block;
|
||||
for o in node.outputs.into_iter().rev() {
|
||||
self.emit_node(o, nid);
|
||||
}
|
||||
}
|
||||
Kind::Return => {
|
||||
let ops = if node.inputs[1] != VOID {
|
||||
vec![regalloc2::Operand::reg_fixed_use(
|
||||
self.rg(node.inputs[1]),
|
||||
regalloc2::PReg::new(1, regalloc2::RegClass::Int),
|
||||
)]
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
self.add_instr(nid, ops);
|
||||
self.emit_node(node.outputs[0], nid);
|
||||
}
|
||||
Kind::CInt { .. } => {
|
||||
let ops = vec![self.drg(nid)];
|
||||
self.add_instr(nid, ops);
|
||||
}
|
||||
Kind::Phi => {}
|
||||
Kind::Tuple { index } => {
|
||||
let is_start = self.nodes[node.inputs[0]].kind == Kind::Start && index == 0;
|
||||
if is_start || (self.nodes[node.inputs[0]].kind == Kind::If && index < 2) {
|
||||
self.nodes[nid].ralloc_backref = self.add_block(nid);
|
||||
self.bridge(prev, nid);
|
||||
|
||||
if is_start {
|
||||
let mut parama = self.tys.parama(self.sig.ret);
|
||||
for (arg, ti) in self.nodes[VOID]
|
||||
.clone()
|
||||
.outputs
|
||||
.into_iter()
|
||||
.skip(1)
|
||||
.zip(self.sig.args.range())
|
||||
{
|
||||
let ty = self.tys.args[ti];
|
||||
match self.tys.size_of(ty) {
|
||||
0 => continue,
|
||||
1..=8 => {
|
||||
self.def_nid(arg);
|
||||
self.add_instr(NEVER, vec![
|
||||
regalloc2::Operand::reg_fixed_def(
|
||||
self.rg(arg),
|
||||
regalloc2::PReg::new(
|
||||
parama.next() as _,
|
||||
regalloc2::RegClass::Int,
|
||||
),
|
||||
),
|
||||
]);
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for o in node.outputs.into_iter().rev() {
|
||||
self.emit_node(o, nid);
|
||||
}
|
||||
} else {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
Kind::BinOp { op } => {
|
||||
let &[_, lhs, rhs] = node.inputs.as_slice() else { unreachable!() };
|
||||
|
||||
let ops = if let Kind::CInt { .. } = self.nodes[rhs].kind
|
||||
&& op.imm_binop(node.ty.is_signed(), 8).is_some()
|
||||
{
|
||||
vec![self.drg(nid), self.urg(lhs)]
|
||||
} else if op.binop(node.ty.is_signed(), 8).is_some() {
|
||||
vec![self.drg(nid), self.urg(lhs), self.urg(rhs)]
|
||||
} else if op.cond_op(node.ty.is_signed()).is_some() {
|
||||
return;
|
||||
} else {
|
||||
todo!("{op}")
|
||||
};
|
||||
self.add_instr(nid, ops);
|
||||
}
|
||||
Kind::UnOp { .. } => {
|
||||
let ops = vec![self.drg(nid), self.urg(node.inputs[1])];
|
||||
self.add_instr(nid, ops);
|
||||
}
|
||||
Kind::Call { func } => {
|
||||
self.nodes[nid].ralloc_backref = self.nodes[prev].ralloc_backref;
|
||||
let mut ops = vec![];
|
||||
|
||||
let fuc = self.tys.funcs[func as usize].sig.unwrap();
|
||||
if self.tys.size_of(fuc.ret) != 0 {
|
||||
self.def_nid(nid);
|
||||
ops.push(regalloc2::Operand::reg_fixed_def(
|
||||
self.rg(nid),
|
||||
regalloc2::PReg::new(1, regalloc2::RegClass::Int),
|
||||
));
|
||||
}
|
||||
|
||||
let mut parama = self.tys.parama(fuc.ret);
|
||||
for (&i, ti) in node.inputs[1..].iter().zip(fuc.args.range()) {
|
||||
let ty = self.tys.args[ti];
|
||||
match self.tys.size_of(ty) {
|
||||
0 => continue,
|
||||
1..=8 => {
|
||||
ops.push(regalloc2::Operand::reg_fixed_use(
|
||||
self.rg(i),
|
||||
regalloc2::PReg::new(
|
||||
parama.next() as _,
|
||||
regalloc2::RegClass::Int,
|
||||
),
|
||||
));
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
self.add_instr(nid, ops);
|
||||
|
||||
for o in node.outputs.into_iter().rev() {
|
||||
if self.nodes[o].inputs[0] == nid {
|
||||
self.emit_node(o, nid);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn bridge(&mut self, pred: u16, succ: u16) {
|
||||
if self.nodes[pred].ralloc_backref == u16::MAX
|
||||
|| self.nodes[succ].ralloc_backref == u16::MAX
|
||||
{
|
||||
return;
|
||||
}
|
||||
self.blocks[self.nodes[pred].ralloc_backref as usize]
|
||||
.succs
|
||||
.push(regalloc2::Block::new(self.nodes[succ].ralloc_backref as usize));
|
||||
self.blocks[self.nodes[succ].ralloc_backref as usize]
|
||||
.preds
|
||||
.push(regalloc2::Block::new(self.nodes[pred].ralloc_backref as usize));
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> regalloc2::Function for Function<'a> {
|
||||
fn num_insts(&self) -> usize {
|
||||
self.instrs.len()
|
||||
}
|
||||
|
||||
fn num_blocks(&self) -> usize {
|
||||
self.blocks.len()
|
||||
}
|
||||
|
||||
fn entry_block(&self) -> regalloc2::Block {
|
||||
regalloc2::Block(0)
|
||||
}
|
||||
|
||||
fn block_insns(&self, block: regalloc2::Block) -> regalloc2::InstRange {
|
||||
self.blocks[block.index()].instrs
|
||||
}
|
||||
|
||||
fn block_succs(&self, block: regalloc2::Block) -> &[regalloc2::Block] {
|
||||
&self.blocks[block.index()].succs
|
||||
}
|
||||
|
||||
fn block_preds(&self, block: regalloc2::Block) -> &[regalloc2::Block] {
|
||||
&self.blocks[block.index()].preds
|
||||
}
|
||||
|
||||
fn block_params(&self, block: regalloc2::Block) -> &[regalloc2::VReg] {
|
||||
&self.blocks[block.index()].params
|
||||
}
|
||||
|
||||
fn is_ret(&self, insn: regalloc2::Inst) -> bool {
|
||||
self.nodes[self.instrs[insn.index()].nid].kind == Kind::Return
|
||||
}
|
||||
|
||||
fn is_branch(&self, insn: regalloc2::Inst) -> bool {
|
||||
matches!(
|
||||
self.nodes[self.instrs[insn.index()].nid].kind,
|
||||
Kind::If | Kind::Tuple { .. } | Kind::Loop | Kind::Region
|
||||
)
|
||||
}
|
||||
|
||||
fn branch_blockparams(
|
||||
&self,
|
||||
block: regalloc2::Block,
|
||||
_insn: regalloc2::Inst,
|
||||
_succ_idx: usize,
|
||||
) -> &[regalloc2::VReg] {
|
||||
debug_assert!(
|
||||
self.blocks[block.index()].succs.len() == 1
|
||||
|| self.blocks[block.index()].branch_blockparams.is_empty()
|
||||
);
|
||||
|
||||
&self.blocks[block.index()].branch_blockparams
|
||||
}
|
||||
|
||||
fn inst_operands(&self, insn: regalloc2::Inst) -> &[regalloc2::Operand] {
|
||||
&self.instrs[insn.index()].ops
|
||||
}
|
||||
|
||||
fn inst_clobbers(&self, insn: regalloc2::Inst) -> regalloc2::PRegSet {
|
||||
if matches!(self.nodes[self.instrs[insn.index()].nid].kind, Kind::Call { .. }) {
|
||||
let mut set = regalloc2::PRegSet::default();
|
||||
for i in 2..12 {
|
||||
set.add(regalloc2::PReg::new(i, regalloc2::RegClass::Int));
|
||||
}
|
||||
set
|
||||
} else {
|
||||
regalloc2::PRegSet::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn num_vregs(&self) -> usize {
|
||||
self.nodes.values.len()
|
||||
}
|
||||
|
||||
fn spillslot_size(&self, regclass: regalloc2::RegClass) -> usize {
|
||||
match regclass {
|
||||
regalloc2::RegClass::Int => 1,
|
||||
regalloc2::RegClass::Float => unreachable!(),
|
||||
regalloc2::RegClass::Vector => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut nodes = std::mem::take(&mut self.ci.nodes);
|
||||
|
||||
let func = Function::new(&mut nodes, &self.tys, sig);
|
||||
|
@ -2492,6 +2085,392 @@ impl Codegen {
|
|||
}
|
||||
}
|
||||
|
||||
// FIXME: make this more efficient (allocated with arena)
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Block {
|
||||
nid: Nid,
|
||||
preds: Vec<regalloc2::Block>,
|
||||
succs: Vec<regalloc2::Block>,
|
||||
instrs: regalloc2::InstRange,
|
||||
params: Vec<regalloc2::VReg>,
|
||||
branch_blockparams: Vec<regalloc2::VReg>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Instr {
|
||||
nid: Nid,
|
||||
ops: Vec<regalloc2::Operand>,
|
||||
}
|
||||
|
||||
struct Function<'a> {
|
||||
sig: Sig,
|
||||
nodes: &'a mut Nodes,
|
||||
tys: &'a Types,
|
||||
blocks: Vec<Block>,
|
||||
instrs: Vec<Instr>,
|
||||
}
|
||||
|
||||
impl Debug for Function<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
for (i, block) in self.blocks.iter().enumerate() {
|
||||
writeln!(f, "sb{i}{:?}-{:?}:", block.params, block.preds)?;
|
||||
|
||||
for inst in block.instrs.iter() {
|
||||
let instr = &self.instrs[inst.index()];
|
||||
writeln!(f, "{}: i{:?}:{:?}", inst.index(), self.nodes[instr.nid].kind, instr.ops)?;
|
||||
}
|
||||
|
||||
writeln!(f, "eb{i}{:?}-{:?}:", block.branch_blockparams, block.succs)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Function<'a> {
|
||||
fn new(nodes: &'a mut Nodes, tys: &'a Types, sig: Sig) -> Self {
|
||||
let mut s =
|
||||
Self { nodes, tys, sig, blocks: Default::default(), instrs: Default::default() };
|
||||
s.nodes.visited.clear(s.nodes.values.len());
|
||||
s.emit_node(VOID, VOID);
|
||||
s.add_block(0);
|
||||
s.blocks.pop();
|
||||
s
|
||||
}
|
||||
|
||||
fn add_block(&mut self, nid: Nid) -> RallocBRef {
|
||||
if let Some(prev) = self.blocks.last_mut() {
|
||||
prev.instrs = regalloc2::InstRange::new(
|
||||
prev.instrs.first(),
|
||||
regalloc2::Inst::new(self.instrs.len()),
|
||||
);
|
||||
}
|
||||
|
||||
self.blocks.push(Block {
|
||||
nid,
|
||||
preds: Default::default(),
|
||||
succs: Default::default(),
|
||||
instrs: regalloc2::InstRange::new(
|
||||
regalloc2::Inst::new(self.instrs.len()),
|
||||
regalloc2::Inst::new(self.instrs.len() + 1),
|
||||
),
|
||||
params: Default::default(),
|
||||
branch_blockparams: Default::default(),
|
||||
});
|
||||
self.blocks.len() as RallocBRef - 1
|
||||
}
|
||||
|
||||
fn add_instr(&mut self, nid: Nid, ops: Vec<regalloc2::Operand>) {
|
||||
self.instrs.push(Instr { nid, ops });
|
||||
}
|
||||
|
||||
fn urg(&mut self, nid: Nid) -> regalloc2::Operand {
|
||||
regalloc2::Operand::reg_use(self.rg(nid))
|
||||
}
|
||||
|
||||
fn def_nid(&mut self, _nid: Nid) {}
|
||||
|
||||
fn drg(&mut self, nid: Nid) -> regalloc2::Operand {
|
||||
self.def_nid(nid);
|
||||
regalloc2::Operand::reg_def(self.rg(nid))
|
||||
}
|
||||
|
||||
fn rg(&self, nid: Nid) -> VReg {
|
||||
regalloc2::VReg::new(nid as _, regalloc2::RegClass::Int)
|
||||
}
|
||||
|
||||
fn emit_node(&mut self, nid: Nid, prev: Nid) {
|
||||
if matches!(self.nodes[nid].kind, Kind::Region | Kind::Loop) {
|
||||
let prev_bref = self.nodes[prev].ralloc_backref;
|
||||
let node = self.nodes[nid].clone();
|
||||
|
||||
let idx = 1 + node.inputs.iter().position(|&i| i == prev).unwrap();
|
||||
|
||||
for ph in node.outputs {
|
||||
if self.nodes[ph].kind != Kind::Phi {
|
||||
continue;
|
||||
}
|
||||
|
||||
let rg = self.rg(self.nodes[ph].inputs[idx]);
|
||||
self.blocks[prev_bref as usize].branch_blockparams.push(rg);
|
||||
}
|
||||
|
||||
self.add_instr(nid, vec![]);
|
||||
|
||||
match (self.nodes[nid].kind, self.nodes.visited.set(nid)) {
|
||||
(Kind::Loop, false) => {
|
||||
for i in node.inputs {
|
||||
self.bridge(i, nid);
|
||||
}
|
||||
return;
|
||||
}
|
||||
(Kind::Region, true) => return,
|
||||
_ => {}
|
||||
}
|
||||
} else if !self.nodes.visited.set(nid) {
|
||||
return;
|
||||
}
|
||||
|
||||
let node = self.nodes[nid].clone();
|
||||
match node.kind {
|
||||
Kind::Start => self.emit_node(node.outputs[0], VOID),
|
||||
Kind::End => {}
|
||||
Kind::If => {
|
||||
self.nodes[nid].ralloc_backref = self.nodes[prev].ralloc_backref;
|
||||
|
||||
let &[_, cond] = node.inputs.as_slice() else { unreachable!() };
|
||||
let &[mut then, mut else_] = node.outputs.as_slice() else { unreachable!() };
|
||||
|
||||
if let Kind::BinOp { op } = self.nodes[cond].kind
|
||||
&& let Some((_, swapped)) = op.cond_op(node.ty.is_signed())
|
||||
{
|
||||
if swapped {
|
||||
std::mem::swap(&mut then, &mut else_);
|
||||
}
|
||||
let &[_, lhs, rhs] = self.nodes[cond].inputs.as_slice() else { unreachable!() };
|
||||
let ops = vec![self.urg(lhs), self.urg(rhs)];
|
||||
self.add_instr(nid, ops);
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
|
||||
self.emit_node(then, nid);
|
||||
self.emit_node(else_, nid);
|
||||
}
|
||||
Kind::Region | Kind::Loop => {
|
||||
self.nodes[nid].ralloc_backref = self.add_block(nid);
|
||||
if node.kind == Kind::Region {
|
||||
for i in node.inputs {
|
||||
self.bridge(i, nid);
|
||||
}
|
||||
}
|
||||
let mut block = vec![];
|
||||
for ph in node.outputs.clone() {
|
||||
if self.nodes[ph].kind != Kind::Phi {
|
||||
continue;
|
||||
}
|
||||
self.def_nid(ph);
|
||||
block.push(self.rg(ph));
|
||||
}
|
||||
self.blocks[self.nodes[nid].ralloc_backref as usize].params = block;
|
||||
for o in node.outputs.into_iter().rev() {
|
||||
self.emit_node(o, nid);
|
||||
}
|
||||
}
|
||||
Kind::Return => {
|
||||
let ops = if node.inputs[1] != VOID {
|
||||
vec![regalloc2::Operand::reg_fixed_use(
|
||||
self.rg(node.inputs[1]),
|
||||
regalloc2::PReg::new(1, regalloc2::RegClass::Int),
|
||||
)]
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
self.add_instr(nid, ops);
|
||||
self.emit_node(node.outputs[0], nid);
|
||||
}
|
||||
Kind::CInt { .. } => {
|
||||
let ops = vec![self.drg(nid)];
|
||||
self.add_instr(nid, ops);
|
||||
}
|
||||
Kind::Phi => {}
|
||||
Kind::Tuple { index } => {
|
||||
let is_start = self.nodes[node.inputs[0]].kind == Kind::Start && index == 0;
|
||||
if is_start || (self.nodes[node.inputs[0]].kind == Kind::If && index < 2) {
|
||||
self.nodes[nid].ralloc_backref = self.add_block(nid);
|
||||
self.bridge(prev, nid);
|
||||
|
||||
if is_start {
|
||||
let mut parama = self.tys.parama(self.sig.ret);
|
||||
for (arg, ti) in self.nodes[VOID]
|
||||
.clone()
|
||||
.outputs
|
||||
.into_iter()
|
||||
.skip(1)
|
||||
.zip(self.sig.args.range())
|
||||
{
|
||||
let ty = self.tys.args[ti];
|
||||
match self.tys.size_of(ty) {
|
||||
0 => continue,
|
||||
1..=8 => {
|
||||
self.def_nid(arg);
|
||||
self.add_instr(NEVER, vec![regalloc2::Operand::reg_fixed_def(
|
||||
self.rg(arg),
|
||||
regalloc2::PReg::new(
|
||||
parama.next() as _,
|
||||
regalloc2::RegClass::Int,
|
||||
),
|
||||
)]);
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for o in node.outputs.into_iter().rev() {
|
||||
self.emit_node(o, nid);
|
||||
}
|
||||
} else {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
Kind::BinOp { op } => {
|
||||
let &[_, lhs, rhs] = node.inputs.as_slice() else { unreachable!() };
|
||||
|
||||
let ops = if let Kind::CInt { .. } = self.nodes[rhs].kind
|
||||
&& op.imm_binop(node.ty.is_signed(), 8).is_some()
|
||||
{
|
||||
vec![self.drg(nid), self.urg(lhs)]
|
||||
} else if op.binop(node.ty.is_signed(), 8).is_some() {
|
||||
vec![self.drg(nid), self.urg(lhs), self.urg(rhs)]
|
||||
} else if op.cond_op(node.ty.is_signed()).is_some() {
|
||||
return;
|
||||
} else {
|
||||
todo!("{op}")
|
||||
};
|
||||
self.add_instr(nid, ops);
|
||||
}
|
||||
Kind::UnOp { .. } => {
|
||||
let ops = vec![self.drg(nid), self.urg(node.inputs[1])];
|
||||
self.add_instr(nid, ops);
|
||||
}
|
||||
Kind::Call { func } => {
|
||||
self.nodes[nid].ralloc_backref = self.nodes[prev].ralloc_backref;
|
||||
let mut ops = vec![];
|
||||
|
||||
let fuc = self.tys.funcs[func as usize].sig.unwrap();
|
||||
if self.tys.size_of(fuc.ret) != 0 {
|
||||
self.def_nid(nid);
|
||||
ops.push(regalloc2::Operand::reg_fixed_def(
|
||||
self.rg(nid),
|
||||
regalloc2::PReg::new(1, regalloc2::RegClass::Int),
|
||||
));
|
||||
}
|
||||
|
||||
let mut parama = self.tys.parama(fuc.ret);
|
||||
for (&i, ti) in node.inputs[1..].iter().zip(fuc.args.range()) {
|
||||
let ty = self.tys.args[ti];
|
||||
match self.tys.size_of(ty) {
|
||||
0 => continue,
|
||||
1..=8 => {
|
||||
ops.push(regalloc2::Operand::reg_fixed_use(
|
||||
self.rg(i),
|
||||
regalloc2::PReg::new(parama.next() as _, regalloc2::RegClass::Int),
|
||||
));
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
self.add_instr(nid, ops);
|
||||
|
||||
for o in node.outputs.into_iter().rev() {
|
||||
if self.nodes[o].inputs[0] == nid {
|
||||
self.emit_node(o, nid);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn bridge(&mut self, pred: u16, succ: u16) {
|
||||
if self.nodes[pred].ralloc_backref == u16::MAX
|
||||
|| self.nodes[succ].ralloc_backref == u16::MAX
|
||||
{
|
||||
return;
|
||||
}
|
||||
self.blocks[self.nodes[pred].ralloc_backref as usize]
|
||||
.succs
|
||||
.push(regalloc2::Block::new(self.nodes[succ].ralloc_backref as usize));
|
||||
self.blocks[self.nodes[succ].ralloc_backref as usize]
|
||||
.preds
|
||||
.push(regalloc2::Block::new(self.nodes[pred].ralloc_backref as usize));
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> regalloc2::Function for Function<'a> {
|
||||
fn num_insts(&self) -> usize {
|
||||
self.instrs.len()
|
||||
}
|
||||
|
||||
fn num_blocks(&self) -> usize {
|
||||
self.blocks.len()
|
||||
}
|
||||
|
||||
fn entry_block(&self) -> regalloc2::Block {
|
||||
regalloc2::Block(0)
|
||||
}
|
||||
|
||||
fn block_insns(&self, block: regalloc2::Block) -> regalloc2::InstRange {
|
||||
self.blocks[block.index()].instrs
|
||||
}
|
||||
|
||||
fn block_succs(&self, block: regalloc2::Block) -> impl Iterator<Item = regalloc2::Block> {
|
||||
self.blocks[block.index()].succs.iter().copied()
|
||||
}
|
||||
|
||||
fn block_preds(&self, block: regalloc2::Block) -> impl Iterator<Item = regalloc2::Block> {
|
||||
self.blocks[block.index()].preds.iter().copied()
|
||||
}
|
||||
|
||||
fn block_params(&self, block: regalloc2::Block) -> impl Iterator<Item = regalloc2::VReg> {
|
||||
self.blocks[block.index()].params.iter().copied()
|
||||
}
|
||||
|
||||
fn is_ret(&self, insn: regalloc2::Inst) -> bool {
|
||||
self.nodes[self.instrs[insn.index()].nid].kind == Kind::Return
|
||||
}
|
||||
|
||||
fn is_branch(&self, insn: regalloc2::Inst) -> bool {
|
||||
matches!(
|
||||
self.nodes[self.instrs[insn.index()].nid].kind,
|
||||
Kind::If | Kind::Tuple { .. } | Kind::Loop | Kind::Region
|
||||
)
|
||||
}
|
||||
|
||||
fn branch_blockparams(
|
||||
&self,
|
||||
block: regalloc2::Block,
|
||||
_insn: regalloc2::Inst,
|
||||
_succ_idx: usize,
|
||||
) -> impl Iterator<Item = regalloc2::VReg> {
|
||||
debug_assert!(
|
||||
self.blocks[block.index()].succs.len() == 1
|
||||
|| self.blocks[block.index()].branch_blockparams.is_empty()
|
||||
);
|
||||
|
||||
self.blocks[block.index()].branch_blockparams.iter().copied()
|
||||
}
|
||||
|
||||
fn inst_operands(&self, insn: regalloc2::Inst) -> impl Iterator<Item = regalloc2::Operand> {
|
||||
self.instrs[insn.index()].ops.iter().copied()
|
||||
}
|
||||
|
||||
fn inst_clobbers(&self, insn: regalloc2::Inst) -> regalloc2::PRegSet {
|
||||
if matches!(self.nodes[self.instrs[insn.index()].nid].kind, Kind::Call { .. }) {
|
||||
let mut set = regalloc2::PRegSet::default();
|
||||
for i in 2..12 {
|
||||
set.add(regalloc2::PReg::new(i, regalloc2::RegClass::Int));
|
||||
}
|
||||
set
|
||||
} else {
|
||||
regalloc2::PRegSet::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn num_vregs(&self) -> usize {
|
||||
self.nodes.values.len()
|
||||
}
|
||||
|
||||
fn spillslot_size(&self, regclass: regalloc2::RegClass) -> usize {
|
||||
match regclass {
|
||||
regalloc2::RegClass::Int => 1,
|
||||
regalloc2::RegClass::Float => unreachable!(),
|
||||
regalloc2::RegClass::Vector => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn loop_depth(target: Nid, nodes: &mut Nodes) -> LoopDepth {
|
||||
if nodes[target].loop_depth != 0 {
|
||||
return nodes[target].loop_depth;
|
||||
|
@ -2731,7 +2710,6 @@ mod tests {
|
|||
const README: &str = include_str!("../README.md");
|
||||
|
||||
fn generate(ident: &'static str, input: &'static str, output: &mut String) {
|
||||
_ = env_logger::builder().is_test(true).try_init();
|
||||
let mut codegen =
|
||||
super::Codegen { files: crate::test_parse_files(ident, input), ..Default::default() };
|
||||
|
||||
|
@ -2756,7 +2734,7 @@ mod tests {
|
|||
return;
|
||||
}
|
||||
|
||||
println!("{output}");
|
||||
//println!("{output}");
|
||||
|
||||
crate::test_run_vm(&out, output);
|
||||
}
|
||||
|
|
|
@ -9,4 +9,4 @@ alloc = []
|
|||
nightly = []
|
||||
|
||||
[dependencies]
|
||||
hbbytecode = { path = "../hbbytecode" }
|
||||
hbbytecode = { path = "../hbbytecode", default-features = false }
|
||||
|
|
|
@ -34,7 +34,7 @@ where
|
|||
/// Program can return [`VmRunError`] if a trap handling failed
|
||||
#[cfg_attr(feature = "nightly", repr(align(4096)))]
|
||||
pub fn run(&mut self) -> Result<VmRunOk, VmRunError> {
|
||||
use hbbytecode::opcode::*;
|
||||
use hbbytecode::Instr as I;
|
||||
loop {
|
||||
// Big match
|
||||
//
|
||||
|
@ -56,105 +56,112 @@ where
|
|||
// - Yes, we assume you run 64 bit CPU. Else ?conradluget a better CPU
|
||||
// sorry 8 bit fans, HBVM won't run on your Speccy :(
|
||||
unsafe {
|
||||
match self.memory.prog_read::<u8>(self.pc as _) {
|
||||
UN => {
|
||||
match self
|
||||
.memory
|
||||
.prog_read::<u8>(self.pc as _)
|
||||
.try_into()
|
||||
.map_err(VmRunError::InvalidOpcode)?
|
||||
{
|
||||
I::UN => {
|
||||
self.bump_pc::<OpsN>();
|
||||
return Err(VmRunError::Unreachable);
|
||||
}
|
||||
TX => {
|
||||
I::TX => {
|
||||
self.bump_pc::<OpsN>();
|
||||
return Ok(VmRunOk::End);
|
||||
}
|
||||
NOP => handler!(self, |OpsN()| ()),
|
||||
ADD8 => self.binary_op(u8::wrapping_add),
|
||||
ADD16 => self.binary_op(u16::wrapping_add),
|
||||
ADD32 => self.binary_op(u32::wrapping_add),
|
||||
ADD64 => self.binary_op(u64::wrapping_add),
|
||||
SUB8 => self.binary_op(u8::wrapping_sub),
|
||||
SUB16 => self.binary_op(u16::wrapping_sub),
|
||||
SUB32 => self.binary_op(u32::wrapping_sub),
|
||||
SUB64 => self.binary_op(u64::wrapping_sub),
|
||||
MUL8 => self.binary_op(u8::wrapping_mul),
|
||||
MUL16 => self.binary_op(u16::wrapping_mul),
|
||||
MUL32 => self.binary_op(u32::wrapping_mul),
|
||||
MUL64 => self.binary_op(u64::wrapping_mul),
|
||||
AND => self.binary_op::<u64>(ops::BitAnd::bitand),
|
||||
OR => self.binary_op::<u64>(ops::BitOr::bitor),
|
||||
XOR => self.binary_op::<u64>(ops::BitXor::bitxor),
|
||||
SLU8 => self.binary_op_shift::<u8>(u8::wrapping_shl),
|
||||
SLU16 => self.binary_op_shift::<u16>(u16::wrapping_shl),
|
||||
SLU32 => self.binary_op_shift::<u32>(u32::wrapping_shl),
|
||||
SLU64 => self.binary_op_shift::<u64>(u64::wrapping_shl),
|
||||
SRU8 => self.binary_op_shift::<u8>(u8::wrapping_shr),
|
||||
SRU16 => self.binary_op_shift::<u16>(u16::wrapping_shr),
|
||||
SRU32 => self.binary_op_shift::<u32>(u32::wrapping_shr),
|
||||
SRU64 => self.binary_op_shift::<u64>(u64::wrapping_shr),
|
||||
SRS8 => self.binary_op_shift::<i8>(i8::wrapping_shr),
|
||||
SRS16 => self.binary_op_shift::<i16>(i16::wrapping_shr),
|
||||
SRS32 => self.binary_op_shift::<i32>(i32::wrapping_shr),
|
||||
SRS64 => self.binary_op_shift::<i64>(i64::wrapping_shr),
|
||||
CMPU => handler!(self, |OpsRRR(tg, a0, a1)| self.cmp(
|
||||
I::NOP => handler!(self, |OpsN()| ()),
|
||||
I::ADD8 => self.binary_op(u8::wrapping_add),
|
||||
I::ADD16 => self.binary_op(u16::wrapping_add),
|
||||
I::ADD32 => self.binary_op(u32::wrapping_add),
|
||||
I::ADD64 => self.binary_op(u64::wrapping_add),
|
||||
I::SUB8 => self.binary_op(u8::wrapping_sub),
|
||||
I::SUB16 => self.binary_op(u16::wrapping_sub),
|
||||
I::SUB32 => self.binary_op(u32::wrapping_sub),
|
||||
I::SUB64 => self.binary_op(u64::wrapping_sub),
|
||||
I::MUL8 => self.binary_op(u8::wrapping_mul),
|
||||
I::MUL16 => self.binary_op(u16::wrapping_mul),
|
||||
I::MUL32 => self.binary_op(u32::wrapping_mul),
|
||||
I::MUL64 => self.binary_op(u64::wrapping_mul),
|
||||
I::AND => self.binary_op::<u64>(ops::BitAnd::bitand),
|
||||
I::OR => self.binary_op::<u64>(ops::BitOr::bitor),
|
||||
I::XOR => self.binary_op::<u64>(ops::BitXor::bitxor),
|
||||
I::SLU8 => self.binary_op_shift::<u8>(u8::wrapping_shl),
|
||||
I::SLU16 => self.binary_op_shift::<u16>(u16::wrapping_shl),
|
||||
I::SLU32 => self.binary_op_shift::<u32>(u32::wrapping_shl),
|
||||
I::SLU64 => self.binary_op_shift::<u64>(u64::wrapping_shl),
|
||||
I::SRU8 => self.binary_op_shift::<u8>(u8::wrapping_shr),
|
||||
I::SRU16 => self.binary_op_shift::<u16>(u16::wrapping_shr),
|
||||
I::SRU32 => self.binary_op_shift::<u32>(u32::wrapping_shr),
|
||||
I::SRU64 => self.binary_op_shift::<u64>(u64::wrapping_shr),
|
||||
I::SRS8 => self.binary_op_shift::<i8>(i8::wrapping_shr),
|
||||
I::SRS16 => self.binary_op_shift::<i16>(i16::wrapping_shr),
|
||||
I::SRS32 => self.binary_op_shift::<i32>(i32::wrapping_shr),
|
||||
I::SRS64 => self.binary_op_shift::<i64>(i64::wrapping_shr),
|
||||
I::CMPU => handler!(self, |OpsRRR(tg, a0, a1)| self.cmp(
|
||||
tg,
|
||||
a0,
|
||||
self.read_reg(a1).cast::<u64>()
|
||||
)),
|
||||
CMPS => handler!(self, |OpsRRR(tg, a0, a1)| self.cmp(
|
||||
I::CMPS => handler!(self, |OpsRRR(tg, a0, a1)| self.cmp(
|
||||
tg,
|
||||
a0,
|
||||
self.read_reg(a1).cast::<i64>()
|
||||
)),
|
||||
DIRU8 => self.dir::<u8>(),
|
||||
DIRU16 => self.dir::<u16>(),
|
||||
DIRU32 => self.dir::<u32>(),
|
||||
DIRU64 => self.dir::<u64>(),
|
||||
DIRS8 => self.dir::<i8>(),
|
||||
DIRS16 => self.dir::<i16>(),
|
||||
DIRS32 => self.dir::<i32>(),
|
||||
DIRS64 => self.dir::<i64>(),
|
||||
NEG => handler!(self, |OpsRR(tg, a0)| {
|
||||
I::DIRU8 => self.dir::<u8>(),
|
||||
I::DIRU16 => self.dir::<u16>(),
|
||||
I::DIRU32 => self.dir::<u32>(),
|
||||
I::DIRU64 => self.dir::<u64>(),
|
||||
I::DIRS8 => self.dir::<i8>(),
|
||||
I::DIRS16 => self.dir::<i16>(),
|
||||
I::DIRS32 => self.dir::<i32>(),
|
||||
I::DIRS64 => self.dir::<i64>(),
|
||||
I::NEG => handler!(self, |OpsRR(tg, a0)| {
|
||||
// Bit negation
|
||||
self.write_reg(tg, self.read_reg(a0).cast::<u64>().wrapping_neg())
|
||||
}),
|
||||
NOT => handler!(self, |OpsRR(tg, a0)| {
|
||||
I::NOT => handler!(self, |OpsRR(tg, a0)| {
|
||||
// Logical negation
|
||||
self.write_reg(tg, u64::from(self.read_reg(a0).cast::<u64>() == 0));
|
||||
}),
|
||||
SXT8 => handler!(self, |OpsRR(tg, a0)| {
|
||||
I::SXT8 => handler!(self, |OpsRR(tg, a0)| {
|
||||
self.write_reg(tg, self.read_reg(a0).cast::<i8>() as i64)
|
||||
}),
|
||||
SXT16 => handler!(self, |OpsRR(tg, a0)| {
|
||||
I::SXT16 => handler!(self, |OpsRR(tg, a0)| {
|
||||
self.write_reg(tg, self.read_reg(a0).cast::<i16>() as i64)
|
||||
}),
|
||||
SXT32 => handler!(self, |OpsRR(tg, a0)| {
|
||||
I::SXT32 => handler!(self, |OpsRR(tg, a0)| {
|
||||
self.write_reg(tg, self.read_reg(a0).cast::<i32>() as i64)
|
||||
}),
|
||||
ADDI8 => self.binary_op_imm(u8::wrapping_add),
|
||||
ADDI16 => self.binary_op_imm(u16::wrapping_add),
|
||||
ADDI32 => self.binary_op_imm(u32::wrapping_add),
|
||||
ADDI64 => self.binary_op_imm(u64::wrapping_add),
|
||||
MULI8 => self.binary_op_imm(u8::wrapping_mul),
|
||||
MULI16 => self.binary_op_imm(u16::wrapping_mul),
|
||||
MULI32 => self.binary_op_imm(u32::wrapping_mul),
|
||||
MULI64 => self.binary_op_imm(u64::wrapping_mul),
|
||||
ANDI => self.binary_op_imm::<u64>(ops::BitAnd::bitand),
|
||||
ORI => self.binary_op_imm::<u64>(ops::BitOr::bitor),
|
||||
XORI => self.binary_op_imm::<u64>(ops::BitXor::bitxor),
|
||||
SLUI8 => self.binary_op_ims::<u8>(u8::wrapping_shl),
|
||||
SLUI16 => self.binary_op_ims::<u16>(u16::wrapping_shl),
|
||||
SLUI32 => self.binary_op_ims::<u32>(u32::wrapping_shl),
|
||||
SLUI64 => self.binary_op_ims::<u64>(u64::wrapping_shl),
|
||||
SRUI8 => self.binary_op_ims::<u8>(u8::wrapping_shr),
|
||||
SRUI16 => self.binary_op_ims::<u16>(u16::wrapping_shr),
|
||||
SRUI32 => self.binary_op_ims::<u32>(u32::wrapping_shr),
|
||||
SRUI64 => self.binary_op_ims::<u64>(u64::wrapping_shr),
|
||||
SRSI8 => self.binary_op_ims::<i8>(i8::wrapping_shr),
|
||||
SRSI16 => self.binary_op_ims::<i16>(i16::wrapping_shr),
|
||||
SRSI32 => self.binary_op_ims::<i32>(i32::wrapping_shr),
|
||||
SRSI64 => self.binary_op_ims::<i64>(i64::wrapping_shr),
|
||||
CMPUI => handler!(self, |OpsRRD(tg, a0, imm)| { self.cmp(tg, a0, imm) }),
|
||||
CMPSI => handler!(self, |OpsRRD(tg, a0, imm)| { self.cmp(tg, a0, imm as i64) }),
|
||||
CP => handler!(self, |OpsRR(tg, a0)| self.write_reg(tg, self.read_reg(a0))),
|
||||
SWA => handler!(self, |OpsRR(r0, r1)| {
|
||||
I::ADDI8 => self.binary_op_imm(u8::wrapping_add),
|
||||
I::ADDI16 => self.binary_op_imm(u16::wrapping_add),
|
||||
I::ADDI32 => self.binary_op_imm(u32::wrapping_add),
|
||||
I::ADDI64 => self.binary_op_imm(u64::wrapping_add),
|
||||
I::MULI8 => self.binary_op_imm(u8::wrapping_mul),
|
||||
I::MULI16 => self.binary_op_imm(u16::wrapping_mul),
|
||||
I::MULI32 => self.binary_op_imm(u32::wrapping_mul),
|
||||
I::MULI64 => self.binary_op_imm(u64::wrapping_mul),
|
||||
I::ANDI => self.binary_op_imm::<u64>(ops::BitAnd::bitand),
|
||||
I::ORI => self.binary_op_imm::<u64>(ops::BitOr::bitor),
|
||||
I::XORI => self.binary_op_imm::<u64>(ops::BitXor::bitxor),
|
||||
I::SLUI8 => self.binary_op_ims::<u8>(u8::wrapping_shl),
|
||||
I::SLUI16 => self.binary_op_ims::<u16>(u16::wrapping_shl),
|
||||
I::SLUI32 => self.binary_op_ims::<u32>(u32::wrapping_shl),
|
||||
I::SLUI64 => self.binary_op_ims::<u64>(u64::wrapping_shl),
|
||||
I::SRUI8 => self.binary_op_ims::<u8>(u8::wrapping_shr),
|
||||
I::SRUI16 => self.binary_op_ims::<u16>(u16::wrapping_shr),
|
||||
I::SRUI32 => self.binary_op_ims::<u32>(u32::wrapping_shr),
|
||||
I::SRUI64 => self.binary_op_ims::<u64>(u64::wrapping_shr),
|
||||
I::SRSI8 => self.binary_op_ims::<i8>(i8::wrapping_shr),
|
||||
I::SRSI16 => self.binary_op_ims::<i16>(i16::wrapping_shr),
|
||||
I::SRSI32 => self.binary_op_ims::<i32>(i32::wrapping_shr),
|
||||
I::SRSI64 => self.binary_op_ims::<i64>(i64::wrapping_shr),
|
||||
I::CMPUI => handler!(self, |OpsRRD(tg, a0, imm)| { self.cmp(tg, a0, imm) }),
|
||||
I::CMPSI => {
|
||||
handler!(self, |OpsRRD(tg, a0, imm)| { self.cmp(tg, a0, imm as i64) })
|
||||
}
|
||||
I::CP => handler!(self, |OpsRR(tg, a0)| self.write_reg(tg, self.read_reg(a0))),
|
||||
I::SWA => handler!(self, |OpsRR(r0, r1)| {
|
||||
// Swap registers
|
||||
match (r0, r1) {
|
||||
(0, 0) => (),
|
||||
|
@ -167,33 +174,33 @@ where
|
|||
}
|
||||
}
|
||||
}),
|
||||
LI8 => handler!(self, |OpsRB(tg, imm)| self.write_reg(tg, imm)),
|
||||
LI16 => handler!(self, |OpsRH(tg, imm)| self.write_reg(tg, imm)),
|
||||
LI32 => handler!(self, |OpsRW(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(
|
||||
I::LI8 => handler!(self, |OpsRB(tg, imm)| self.write_reg(tg, imm)),
|
||||
I::LI16 => handler!(self, |OpsRH(tg, imm)| self.write_reg(tg, imm)),
|
||||
I::LI32 => handler!(self, |OpsRW(tg, imm)| self.write_reg(tg, imm)),
|
||||
I::LI64 => handler!(self, |OpsRD(tg, imm)| self.write_reg(tg, imm)),
|
||||
I::LRA => handler!(self, |OpsRRO(tg, reg, off)| self.write_reg(
|
||||
tg,
|
||||
self.pcrel(off).wrapping_add(self.read_reg(reg).cast::<i64>()).get(),
|
||||
)),
|
||||
// Load. If loading more than register size, continue on adjecent registers
|
||||
LD => handler!(self, |OpsRRAH(dst, base, off, count)| self
|
||||
I::LD => handler!(self, |OpsRRAH(dst, base, off, count)| self
|
||||
.load(dst, base, off, count)?),
|
||||
// Store. Same rules apply as to LD
|
||||
ST => handler!(self, |OpsRRAH(dst, base, off, count)| self
|
||||
I::ST => handler!(self, |OpsRRAH(dst, base, off, count)| self
|
||||
.store(dst, base, off, count)?),
|
||||
LDR => handler!(self, |OpsRROH(dst, base, off, count)| self.load(
|
||||
I::LDR => handler!(self, |OpsRROH(dst, base, off, count)| self.load(
|
||||
dst,
|
||||
base,
|
||||
self.pcrel(off).get(),
|
||||
count
|
||||
)?),
|
||||
STR => handler!(self, |OpsRROH(dst, base, off, count)| self.store(
|
||||
I::STR => handler!(self, |OpsRROH(dst, base, off, count)| self.store(
|
||||
dst,
|
||||
base,
|
||||
self.pcrel(off).get(),
|
||||
count
|
||||
)?),
|
||||
BMC => {
|
||||
I::BMC => {
|
||||
// Block memory copy
|
||||
match if let Some(copier) = &mut self.copier {
|
||||
// There is some copier, poll.
|
||||
|
@ -227,7 +234,7 @@ where
|
|||
core::task::Poll::Pending => (),
|
||||
}
|
||||
}
|
||||
BRC => handler!(self, |OpsRRB(src, dst, count)| {
|
||||
I::BRC => handler!(self, |OpsRRB(src, dst, count)| {
|
||||
// Block register copy
|
||||
if src.checked_add(count).is_none() || dst.checked_add(count).is_none() {
|
||||
return Err(VmRunError::RegOutOfBounds);
|
||||
|
@ -239,11 +246,11 @@ where
|
|||
usize::from(count),
|
||||
);
|
||||
}),
|
||||
JMP => {
|
||||
I::JMP => {
|
||||
let OpsO(off) = self.decode();
|
||||
self.pc = self.pc.wrapping_add(off);
|
||||
}
|
||||
JAL => {
|
||||
I::JAL => {
|
||||
// Jump and link. Save PC after this instruction to
|
||||
// specified register and jump to reg + relative offset.
|
||||
let OpsRRO(save, reg, offset) = self.decode();
|
||||
|
@ -251,7 +258,7 @@ where
|
|||
self.write_reg(save, self.pc.next::<OpsRRO>());
|
||||
self.pc = self.pcrel(offset).wrapping_add(self.read_reg(reg).cast::<i64>());
|
||||
}
|
||||
JALA => {
|
||||
I::JALA => {
|
||||
// Jump and link. Save PC after this instruction to
|
||||
// specified register and jump to reg
|
||||
let OpsRRA(save, reg, offset) = self.decode();
|
||||
|
@ -261,8 +268,8 @@ where
|
|||
Address::new(self.read_reg(reg).cast::<u64>().wrapping_add(offset));
|
||||
}
|
||||
// Conditional jumps, jump only to immediates
|
||||
JEQ => self.cond_jmp::<u64>(Ordering::Equal),
|
||||
JNE => {
|
||||
I::JEQ => self.cond_jmp::<u64>(Ordering::Equal),
|
||||
I::JNE => {
|
||||
let OpsRRP(a0, a1, ja) = self.decode();
|
||||
if self.read_reg(a0).cast::<u64>() != self.read_reg(a1).cast::<u64>() {
|
||||
self.pc = self.pcrel(ja);
|
||||
|
@ -270,11 +277,11 @@ where
|
|||
self.bump_pc::<OpsRRP>();
|
||||
}
|
||||
}
|
||||
JLTS => self.cond_jmp::<i64>(Ordering::Less),
|
||||
JGTS => self.cond_jmp::<i64>(Ordering::Greater),
|
||||
JLTU => self.cond_jmp::<u64>(Ordering::Less),
|
||||
JGTU => self.cond_jmp::<u64>(Ordering::Greater),
|
||||
ECA => {
|
||||
I::JLTS => self.cond_jmp::<i64>(Ordering::Less),
|
||||
I::JGTS => self.cond_jmp::<i64>(Ordering::Greater),
|
||||
I::JLTU => self.cond_jmp::<u64>(Ordering::Less),
|
||||
I::JGTU => self.cond_jmp::<u64>(Ordering::Greater),
|
||||
I::ECA => {
|
||||
// So we don't get timer interrupt after ECALL
|
||||
if TIMER_QUOTIENT != 0 {
|
||||
self.timer = self.timer.wrapping_add(1);
|
||||
|
@ -283,33 +290,33 @@ where
|
|||
self.bump_pc::<OpsN>();
|
||||
return Ok(VmRunOk::Ecall);
|
||||
}
|
||||
EBP => {
|
||||
I::EBP => {
|
||||
self.bump_pc::<OpsN>();
|
||||
return Ok(VmRunOk::Breakpoint);
|
||||
}
|
||||
FADD32 => self.binary_op::<f32>(ops::Add::add),
|
||||
FADD64 => self.binary_op::<f64>(ops::Add::add),
|
||||
FSUB32 => self.binary_op::<f32>(ops::Sub::sub),
|
||||
FSUB64 => self.binary_op::<f64>(ops::Sub::sub),
|
||||
FMUL32 => self.binary_op::<f32>(ops::Mul::mul),
|
||||
FMUL64 => self.binary_op::<f64>(ops::Mul::mul),
|
||||
FDIV32 => self.binary_op::<f32>(ops::Div::div),
|
||||
FDIV64 => self.binary_op::<f64>(ops::Div::div),
|
||||
FMA32 => self.fma::<f32>(),
|
||||
FMA64 => self.fma::<f64>(),
|
||||
FINV32 => handler!(self, |OpsRR(tg, reg)| self
|
||||
I::FADD32 => self.binary_op::<f32>(ops::Add::add),
|
||||
I::FADD64 => self.binary_op::<f64>(ops::Add::add),
|
||||
I::FSUB32 => self.binary_op::<f32>(ops::Sub::sub),
|
||||
I::FSUB64 => self.binary_op::<f64>(ops::Sub::sub),
|
||||
I::FMUL32 => self.binary_op::<f32>(ops::Mul::mul),
|
||||
I::FMUL64 => self.binary_op::<f64>(ops::Mul::mul),
|
||||
I::FDIV32 => self.binary_op::<f32>(ops::Div::div),
|
||||
I::FDIV64 => self.binary_op::<f64>(ops::Div::div),
|
||||
I::FMA32 => self.fma::<f32>(),
|
||||
I::FMA64 => self.fma::<f64>(),
|
||||
I::FINV32 => handler!(self, |OpsRR(tg, reg)| self
|
||||
.write_reg(tg, 1. / self.read_reg(reg).cast::<f32>())),
|
||||
FINV64 => handler!(self, |OpsRR(tg, reg)| self
|
||||
I::FINV64 => handler!(self, |OpsRR(tg, reg)| self
|
||||
.write_reg(tg, 1. / self.read_reg(reg).cast::<f64>())),
|
||||
FCMPLT32 => self.fcmp::<f32>(Ordering::Less),
|
||||
FCMPLT64 => self.fcmp::<f64>(Ordering::Less),
|
||||
FCMPGT32 => self.fcmp::<f32>(Ordering::Greater),
|
||||
FCMPGT64 => self.fcmp::<f64>(Ordering::Greater),
|
||||
ITF32 => handler!(self, |OpsRR(tg, reg)| self
|
||||
I::FCMPLT32 => self.fcmp::<f32>(Ordering::Less),
|
||||
I::FCMPLT64 => self.fcmp::<f64>(Ordering::Less),
|
||||
I::FCMPGT32 => self.fcmp::<f32>(Ordering::Greater),
|
||||
I::FCMPGT64 => self.fcmp::<f64>(Ordering::Greater),
|
||||
I::ITF32 => handler!(self, |OpsRR(tg, reg)| self
|
||||
.write_reg(tg, self.read_reg(reg).cast::<i64>() as f32)),
|
||||
ITF64 => handler!(self, |OpsRR(tg, reg)| self
|
||||
I::ITF64 => handler!(self, |OpsRR(tg, reg)| self
|
||||
.write_reg(tg, self.read_reg(reg).cast::<i64>() as f64)),
|
||||
FTI32 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg(
|
||||
I::FTI32 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg(
|
||||
tg,
|
||||
crate::float::f32toint(
|
||||
self.read_reg(reg).cast::<f32>(),
|
||||
|
@ -317,7 +324,7 @@ where
|
|||
.map_err(|()| VmRunError::InvalidOperand)?,
|
||||
),
|
||||
)),
|
||||
FTI64 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg(
|
||||
I::FTI64 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg(
|
||||
tg,
|
||||
crate::float::f64toint(
|
||||
self.read_reg(reg).cast::<f64>(),
|
||||
|
@ -325,9 +332,9 @@ where
|
|||
.map_err(|()| VmRunError::InvalidOperand)?,
|
||||
),
|
||||
)),
|
||||
FC32T64 => handler!(self, |OpsRR(tg, reg)| self
|
||||
I::FC32T64 => handler!(self, |OpsRR(tg, reg)| self
|
||||
.write_reg(tg, self.read_reg(reg).cast::<f32>() as f64)),
|
||||
FC64T32 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg(
|
||||
I::FC64T32 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg(
|
||||
tg,
|
||||
crate::float::conv64to32(
|
||||
self.read_reg(reg).cast(),
|
||||
|
@ -335,27 +342,26 @@ where
|
|||
.map_err(|()| VmRunError::InvalidOperand)?,
|
||||
),
|
||||
)),
|
||||
LRA16 => handler!(self, |OpsRRP(tg, reg, imm)| self.write_reg(
|
||||
I::LRA16 => handler!(self, |OpsRRP(tg, reg, imm)| self.write_reg(
|
||||
tg,
|
||||
(self.pc + self.read_reg(reg).cast::<u64>() + imm + 3_u16).get(),
|
||||
)),
|
||||
LDR16 => handler!(self, |OpsRRPH(dst, base, off, count)| self.load(
|
||||
I::LDR16 => handler!(self, |OpsRRPH(dst, base, off, count)| self.load(
|
||||
dst,
|
||||
base,
|
||||
self.pcrel(off).get(),
|
||||
count
|
||||
)?),
|
||||
STR16 => handler!(self, |OpsRRPH(dst, base, off, count)| self.store(
|
||||
I::STR16 => handler!(self, |OpsRRPH(dst, base, off, count)| self.store(
|
||||
dst,
|
||||
base,
|
||||
self.pcrel(off).get(),
|
||||
count
|
||||
)?),
|
||||
JMP16 => {
|
||||
I::JMP16 => {
|
||||
let OpsP(off) = self.decode();
|
||||
self.pc = self.pcrel(off);
|
||||
}
|
||||
op => return Err(VmRunError::InvalidOpcode(op)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue