Compare commits
29 Commits
Author | SHA1 | Date |
---|---|---|
IntoTheNight | da8dc69ea0 | |
MunirG05 | 82ce817802 | |
MunirG05 | 08e413de72 | |
ondra05 | e7c014f6e4 | |
MunirG05 | 7e363c465f | |
ondra05 | 32b63c0f23 | |
ondra05 | 9c93a0fa8f | |
ondra05 | d8ee65b873 | |
ondra05 | 8a9e485d45 | |
ondra05 | 3ec7e5f29f | |
ondra05 | b6ea49a1b5 | |
ondra05 | 5852a640bb | |
ondra05 | 9bc3000782 | |
ondra05 | e01e2ea7a7 | |
ondra05 | 3c19cd4c9a | |
able | b7c34775e4 | |
able | 69d2516ca3 | |
ondra05 | 8a3616b352 | |
ondra05 | 1ff31e47a4 | |
ondra05 | 45d1265007 | |
ondra05 | 42e4cb6273 | |
able | 8ab55e801f | |
able | eefde51ddf | |
able | 453309f2a4 | |
able | 5edf0a1b9b | |
ondra05 | c0fb38d717 | |
ondra05 | 64745257b2 | |
ondra05 | f99f729be6 | |
ondra05 | 908ee5b922 |
|
@ -1,2 +0,0 @@
|
|||
[alias]
|
||||
xtask = "r -p xtask --"
|
|
@ -2,101 +2,38 @@
|
|||
# It is not intended for manual editing.
|
||||
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.8"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42cd52102d3df161c77a887b608d7a4897d7cc112886a9537b738a887a03aaff"
|
||||
checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"const-random",
|
||||
"getrandom",
|
||||
"once_cell",
|
||||
"version_check",
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "argh"
|
||||
version = "0.1.12"
|
||||
name = "allocator-api2"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7af5ba06967ff7214ce4c7419c7d185be7ecd6cc4965a8f6e1d8ce0398aad219"
|
||||
checksum = "56fc6cf8dc8c4158eed8649f9b8b0ea1518eb62b544fe9490d66fa0b349eafe9"
|
||||
|
||||
[[package]]
|
||||
name = "ariadne"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72fe02fc62033df9ba41cba57ee19acf5e742511a140c7dbc3a873e19a19a1bd"
|
||||
dependencies = [
|
||||
"argh_derive",
|
||||
"argh_shared",
|
||||
"unicode-width",
|
||||
"yansi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "argh_derive"
|
||||
version = "0.1.12"
|
||||
name = "beef"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56df0aeedf6b7a2fc67d06db35b09684c3e8da0c95f8f27685cb17e08413d87a"
|
||||
dependencies = [
|
||||
"argh_shared",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "argh_shared"
|
||||
version = "0.1.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5693f39141bda5760ecc4111ab08da40565d1771038c4a0250f03457ec707531"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "backtrace"
|
||||
version = "0.3.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837"
|
||||
dependencies = [
|
||||
"addr2line",
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"miniz_oxide",
|
||||
"object",
|
||||
"rustc-demangle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
checksum = "3a8241f3ebb85c056b509d4327ad0358fbbba6ffb340bf388f26350aeda225b1"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
|
@ -105,314 +42,190 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "color-eyre"
|
||||
version = "0.6.2"
|
||||
name = "convert_case"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a667583cca8c4f8436db8de46ea8233c42a7d9ae424a82d338f2e4675229204"
|
||||
checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
|
||||
|
||||
[[package]]
|
||||
name = "delegate"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d358e0ec5c59a5e1603b933def447096886121660fc680dc1e64a0753981fe3c"
|
||||
dependencies = [
|
||||
"backtrace",
|
||||
"color-spantrace",
|
||||
"eyre",
|
||||
"indenter",
|
||||
"once_cell",
|
||||
"owo-colors",
|
||||
"tracing-error",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "color-spantrace"
|
||||
version = "0.2.1"
|
||||
name = "derive_more"
|
||||
version = "0.99.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cd6be1b2a7e382e2b98b43b2adcca6bb0e465af0bdd38123873ae61eb17a72c2"
|
||||
checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"owo-colors",
|
||||
"tracing-core",
|
||||
"tracing-error",
|
||||
"convert_case",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"rustc_version",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "const-random"
|
||||
version = "0.1.17"
|
||||
name = "fnv"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5aaf16c9c2c612020bcfd042e170f6e32de9b9d75adb5277cdbbd2e2c8c8299a"
|
||||
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e"
|
||||
dependencies = [
|
||||
"const-random-macro",
|
||||
"ahash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "const-random-macro"
|
||||
version = "0.1.16"
|
||||
name = "hashbrown"
|
||||
version = "0.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e"
|
||||
checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"once_cell",
|
||||
"tiny-keccak",
|
||||
"ahash",
|
||||
"allocator-api2",
|
||||
]
|
||||
|
||||
[[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.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5"
|
||||
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 = [
|
||||
"ariadne",
|
||||
"hashbrown 0.14.0",
|
||||
"hbbytecode",
|
||||
"lasso",
|
||||
"logos",
|
||||
"paste",
|
||||
"rhai",
|
||||
"with_builtin_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hbbytecode"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"paste",
|
||||
"with_builtin_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hblang"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"hbvm",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hbvm"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"delegate",
|
||||
"derive_more",
|
||||
"hashbrown 0.13.2",
|
||||
"hbbytecode",
|
||||
"log",
|
||||
"paste",
|
||||
"static_assertions",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hbxrt"
|
||||
version = "0.1.0"
|
||||
name = "lasso"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4644821e1c3d7a560fe13d842d13f587c07348a1a05d3a797152d41c90c56df2"
|
||||
dependencies = [
|
||||
"hbvm",
|
||||
"memmap2",
|
||||
"ahash",
|
||||
"hashbrown 0.13.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indenter"
|
||||
version = "0.3.3"
|
||||
name = "log"
|
||||
version = "0.4.17"
|
||||
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"
|
||||
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
name = "logos"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.153"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
|
||||
|
||||
[[package]]
|
||||
name = "memmap2"
|
||||
version = "0.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fe751422e4a8caa417e13c3ea66452215d7d63e19e604f4980461212f3ae1322"
|
||||
checksum = "c000ca4d908ff18ac99b93a062cb8958d331c3220719c52e77cb19cc6ac5d2c1"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"logos-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.7.2"
|
||||
name = "logos-codegen"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7"
|
||||
checksum = "dc487311295e0002e452025d6b580b77bb17286de87b57138f3b5db711cded68"
|
||||
dependencies = [
|
||||
"adler",
|
||||
"beef",
|
||||
"fnv",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"regex-syntax",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.18"
|
||||
name = "logos-derive"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a"
|
||||
checksum = "dbfc0d229f1f42d790440136d941afd806bc9e949e2bcb8faa813b0f00d1267e"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.32.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"logos-codegen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.19.0"
|
||||
version = "1.17.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
||||
|
||||
[[package]]
|
||||
name = "owo-colors"
|
||||
version = "3.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
|
||||
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
|
||||
|
||||
[[package]]
|
||||
name = "paste"
|
||||
version = "1.0.14"
|
||||
version = "1.0.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c"
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58"
|
||||
checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.78"
|
||||
version = "1.0.59"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
|
||||
checksum = "6aeca18b86b413c660b781aa319e4e2648a3e6f9eadc9b47e9038e6fe9f3451b"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.35"
|
||||
version = "1.0.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
|
||||
checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rhai"
|
||||
version = "1.17.1"
|
||||
name = "regex-syntax"
|
||||
version = "0.6.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f6273372244d04a8a4b0bec080ea1e710403e88c5d9d83f9808b2bfa64f0982a"
|
||||
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
|
||||
|
||||
[[package]]
|
||||
name = "rustc_version"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"bitflags",
|
||||
"instant",
|
||||
"num-traits",
|
||||
"once_cell",
|
||||
"rhai_codegen",
|
||||
"smallvec",
|
||||
"smartstring",
|
||||
"thin-vec",
|
||||
"semver",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rhai_codegen"
|
||||
version = "2.0.0"
|
||||
name = "semver"
|
||||
version = "1.0.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9db7f8dc4c9d48183a17ce550574c42995252b82d267eaca3fcd1b979159856c"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.196"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "870026e60fa08c69f064aa766c10f10b1d62db9ccd4d0abb206472bee0ce3b32"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.196"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "33c85360c95e7d137454dc81d9a4ed2b8efd8fbe19cee57357b32b9771fccb67"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[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.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7"
|
||||
|
||||
[[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",
|
||||
]
|
||||
checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed"
|
||||
|
||||
[[package]]
|
||||
name = "static_assertions"
|
||||
|
@ -433,92 +246,26 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.48"
|
||||
version = "2.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f"
|
||||
checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"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.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152"
|
||||
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]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||
checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0"
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.0"
|
||||
name = "unicode-width"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
|
||||
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
|
@ -527,56 +274,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
name = "yansi"
|
||||
version = "0.5.1"
|
||||
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]]
|
||||
name = "xtask"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"argh",
|
||||
"color-eyre",
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.7.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.7.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"
|
||||
|
|
|
@ -1,3 +1,2 @@
|
|||
[workspace]
|
||||
resolver = "2"
|
||||
members = ["hbasm", "hbbytecode", "hbvm", "hbxrt", "xtask", "hblang"]
|
||||
members = ["hbasm", "hbbytecode", "hbvm"]
|
||||
|
|
|
@ -1,100 +0,0 @@
|
|||
jmp entry
|
||||
|
||||
puts:
|
||||
-- Write string to console
|
||||
-- r2: [IN] *const u8 String pointer
|
||||
-- r3: [IN] usize String length
|
||||
|
||||
li8 r1, 0x1 -- Write syscall
|
||||
brc r2, r3, 2 -- Copy parameters
|
||||
li8 r2, 0x1 -- STDOUT
|
||||
eca
|
||||
|
||||
jal r0, r31, 0
|
||||
|
||||
gets:
|
||||
-- Read string until end of buffer or LF
|
||||
-- r2: [IN] *mut u8 Buffer
|
||||
-- r3: [IN] usize Buffer length
|
||||
|
||||
-- Register allocations:
|
||||
-- r33: *mut u8 Buffer end
|
||||
-- r34: u8 Immediate char
|
||||
-- r35: u8 Const [0x0A = LF]
|
||||
|
||||
li8 r35, 0x0A
|
||||
add64 r33, r2, r3
|
||||
|
||||
-- Setup syscall
|
||||
li8 r2, 0x1 -- Stdin
|
||||
cp r3, r2
|
||||
li8 r4, 0x1 -- Read one char
|
||||
|
||||
jeq r3, r33, end
|
||||
loop:
|
||||
li8 r1, 0x1 -- Read syscall
|
||||
eca
|
||||
addi64 r3, r3, 1
|
||||
ld r34, r3, 0, 1
|
||||
jeq r34, r35, end
|
||||
jne r3, r33, loop
|
||||
|
||||
end:
|
||||
-- Set copied amount
|
||||
sub64 r1, r33, r3
|
||||
addi64 r1, -1
|
||||
jal r0, r31, 0
|
||||
|
||||
alloc-pages:
|
||||
-- Allocate pages
|
||||
-- r1: [OUT] *mut u8 Pointer to page
|
||||
-- r2: [IN] u16 Page count
|
||||
|
||||
muli16 r3, r2, 4096 -- page count
|
||||
li8 r1, 0x9 -- mmap syscall
|
||||
li8 r2, 0x0 -- no address set, kernel chosen
|
||||
li8 r4, 0x2 -- PROT_WRITE
|
||||
li8 r5, 0x20 -- MAP_ANONYMOUS
|
||||
li64 r6, -1 -- Doesn't map file
|
||||
li8 r7, 0x0 -- Doesn't map file
|
||||
eca
|
||||
|
||||
jal r0, r31, 0
|
||||
|
||||
entry:
|
||||
-- Program entrypoint
|
||||
|
||||
-- Register allocations:
|
||||
-- r32: *mut u8 Buffer
|
||||
-- r36: usize Read buffer length
|
||||
|
||||
-- Allocate one page (4096 KiB)
|
||||
li8 r2, 1
|
||||
jal r31, 0, alloc-pages
|
||||
cp r32, r1
|
||||
|
||||
-- Print message
|
||||
lra16 r2, r0, #enter-your-name
|
||||
li8 r3, 17
|
||||
jal r31, r0, puts
|
||||
|
||||
-- Read name
|
||||
cp r2, r32
|
||||
li16 r3, 4096
|
||||
jal r31, r0, gets
|
||||
cp r36, r1
|
||||
|
||||
-- Print your name is
|
||||
lra16 r2, r0, #your-name-is
|
||||
li8 r3, 15
|
||||
jal r31, r0, puts
|
||||
|
||||
-- And now print the name
|
||||
cp r2, r32
|
||||
cp r3, r36
|
||||
jal r31, r0, puts
|
||||
|
||||
tx
|
||||
|
||||
#enter-your-name: "Enter your name: "
|
||||
#your-name-is : "\nYour name is: "
|
Binary file not shown.
Binary file not shown.
|
@ -4,6 +4,17 @@ version = "0.1.0"
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
paste = "1.0"
|
||||
rhai = "1.16"
|
||||
with_builtin_macros = "0.0.3"
|
||||
hbbytecode = { path = "../hbbytecode" }
|
||||
paste = "1.0"
|
||||
hashbrown = "0.14.0"
|
||||
ariadne = "0.3.0"
|
||||
|
||||
[dependencies.lasso]
|
||||
version = "0.7"
|
||||
default-features = false
|
||||
features = ["no-std"]
|
||||
|
||||
[dependencies.logos]
|
||||
version = "0.13"
|
||||
default-features = false
|
||||
features = ["export_derive"]
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
jmp r0, start
|
||||
start:
|
||||
jmp r0, init_serial_port
|
||||
|
||||
-- Uses r20 to set the port
|
||||
init_serial_port:
|
||||
add r20, r30, r10
|
||||
li r20, 00
|
||||
|
||||
-- outb(PORT + 1, 0x00); // Disable all interrupts
|
||||
-- outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
|
||||
-- outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
|
||||
-- outb(PORT + 1, 0x00); // (hi byte)
|
||||
-- outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit
|
||||
-- outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
|
||||
-- outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
|
||||
-- outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
|
||||
-- outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)
|
|
@ -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();
|
|
@ -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);}
|
|
@ -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
|
|
@ -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();
|
||||
}
|
|
@ -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
|
||||
}
|
321
hbasm/src/ins.rs
321
hbasm/src/ins.rs
|
@ -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 });
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
|
@ -1,45 +1,65 @@
|
|||
pub mod data;
|
||||
pub mod ins;
|
||||
pub mod label;
|
||||
pub mod linker;
|
||||
pub mod object;
|
||||
#![no_std]
|
||||
#![feature(error_in_core)]
|
||||
|
||||
use {
|
||||
object::Object,
|
||||
rhai::{Engine, Module},
|
||||
std::{cell::RefCell, rc::Rc},
|
||||
};
|
||||
extern crate alloc;
|
||||
|
||||
type SharedObject = Rc<RefCell<Object>>;
|
||||
pub mod text;
|
||||
|
||||
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));
|
||||
mod macros;
|
||||
|
||||
// Registers
|
||||
for n in 0_u8..=255 {
|
||||
module.set_var(format!("r{n}"), n);
|
||||
}
|
||||
use {alloc::vec::Vec, hashbrown::HashSet};
|
||||
|
||||
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(())
|
||||
#[derive(Default)]
|
||||
pub struct Assembler {
|
||||
pub buf: Vec<u8>,
|
||||
sub: HashSet<usize>,
|
||||
}
|
||||
|
||||
impl Assembler {
|
||||
macros::impl_asm!(
|
||||
bbbb(p0: u8, p1: u8, p2: u8, p3: u8)
|
||||
=> [DIR, DIRF, FMAF],
|
||||
bbb(p0: u8, p1: u8, p2: u8)
|
||||
=> [ADD, SUB, MUL, AND, OR, XOR, SL, SR, SRS, CMP, CMPU, BRC, ADDF, SUBF, MULF],
|
||||
bbdh(p0: u8, p1: u8, p2: impl Imm, p3: u16)
|
||||
=> [LD, ST],
|
||||
bbd(p0: u8, p1: u8, p2: impl Imm)
|
||||
=> [ADDI, MULI, ANDI, ORI, XORI, SLI, SRI, SRSI, CMPI, CMPUI,
|
||||
BMC, JEQ, JNE, JLT, JGT, JLTU, JGTU, ADDFI, MULFI],
|
||||
bb(p0: u8, p1: u8)
|
||||
=> [NEG, NOT, CP, SWA, NEGF, ITF, FTI],
|
||||
bd(p0: u8, p1: impl Imm)
|
||||
=> [LI, JMP],
|
||||
n()
|
||||
=> [NOP, ECALL],
|
||||
);
|
||||
}
|
||||
|
||||
pub trait Imm {
|
||||
fn insert(&self, asm: &mut Assembler);
|
||||
}
|
||||
|
||||
macro_rules! impl_imm_le_bytes {
|
||||
($($ty:ty),* $(,)?) => {
|
||||
$(
|
||||
impl Imm for $ty {
|
||||
#[inline(always)]
|
||||
fn insert(&self, asm: &mut Assembler) {
|
||||
asm.buf.extend(self.to_le_bytes());
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
impl_imm_le_bytes!(u64, i64, f64);
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Symbol(pub u64);
|
||||
impl Imm for Symbol {
|
||||
#[inline(always)]
|
||||
fn insert(&self, asm: &mut Assembler) {
|
||||
asm.sub.insert(asm.buf.len());
|
||||
asm.buf.extend(self.0.to_le_bytes());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
macro_rules! impl_asm_opcodes {
|
||||
(
|
||||
$generic:ident
|
||||
($($param_i:ident: $param_ty:ty),*)
|
||||
=> []
|
||||
) => {};
|
||||
|
||||
(
|
||||
$generic:ident
|
||||
($($param_i:ident: $param_ty:ty),*)
|
||||
=> [$opcode:ident, $($rest:tt)*]
|
||||
) => {
|
||||
paste::paste! {
|
||||
#[allow(dead_code)]
|
||||
#[inline(always)]
|
||||
pub fn [<i_ $opcode:lower>](&mut self, $($param_i: $param_ty),*) {
|
||||
self.$generic(hbbytecode::opcode::$opcode, $($param_i),*)
|
||||
}
|
||||
}
|
||||
|
||||
macros::impl_asm_opcodes!(
|
||||
$generic($($param_i: $param_ty),*)
|
||||
=> [$($rest)*]
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! gen_impl_asm_insert {
|
||||
($($ty:ident),* $(,)?) => {
|
||||
macro_rules! impl_asm_insert {
|
||||
$(($self:expr, $id:ident, $ty) => {
|
||||
$self.buf.extend($id.to_le_bytes())
|
||||
};)*
|
||||
|
||||
($self:expr, $id:ident, $_:ty) => {
|
||||
Imm::insert(&$id, $self)
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
gen_impl_asm_insert!(u8, u16, u64);
|
||||
|
||||
macro_rules! impl_asm {
|
||||
(
|
||||
$(
|
||||
$ityn:ident
|
||||
($($param_i:ident: $param_ty:ty),* $(,)?)
|
||||
=> [$($opcode:ident),* $(,)?],
|
||||
)*
|
||||
) => {
|
||||
paste::paste! {
|
||||
$(
|
||||
#[allow(dead_code)]
|
||||
fn [<i_param_ $ityn>](&mut self, opcode: u8, $($param_i: $param_ty),*) {
|
||||
self.buf.push(opcode);
|
||||
$(macros::impl_asm_insert!(self, $param_i, $param_ty);)*
|
||||
}
|
||||
|
||||
macros::impl_asm_opcodes!(
|
||||
[<i_param_ $ityn>]($($param_i: $param_ty),*)
|
||||
=> [$($opcode,)*]
|
||||
);
|
||||
)*
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub(super) use {impl_asm, impl_asm_opcodes};
|
||||
|
||||
#[allow(clippy::single_component_path_imports)]
|
||||
pub(super) use impl_asm_insert;
|
|
@ -1,8 +1,46 @@
|
|||
use std::{io::stdout, path::PathBuf};
|
||||
use {
|
||||
ariadne::{ColorGenerator, Label, Report, ReportKind, Source},
|
||||
std::{
|
||||
error::Error,
|
||||
io::{stdin, Read},
|
||||
},
|
||||
};
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let mut code = String::new();
|
||||
stdin().read_to_string(&mut code)?;
|
||||
|
||||
let mut buf = vec![];
|
||||
|
||||
if let Err(e) = hbasm::text::assembly(&code, &mut buf) {
|
||||
let mut colors = ColorGenerator::new();
|
||||
|
||||
let e_code = match e.kind {
|
||||
hbasm::text::ErrorKind::UnexpectedToken => 1,
|
||||
hbasm::text::ErrorKind::InvalidToken => 2,
|
||||
hbasm::text::ErrorKind::UnexpectedEnd => 3,
|
||||
hbasm::text::ErrorKind::InvalidSymbol => 4,
|
||||
};
|
||||
let message = match e.kind {
|
||||
hbasm::text::ErrorKind::UnexpectedToken => "This token is not expected!",
|
||||
hbasm::text::ErrorKind::InvalidToken => "The token is not valid!",
|
||||
hbasm::text::ErrorKind::UnexpectedEnd => "The assembler reached the end of input unexpectedly!",
|
||||
hbasm::text::ErrorKind::InvalidSymbol => "This referenced symbol doesn't have a corresponding label!",
|
||||
};
|
||||
let a = colors.next();
|
||||
|
||||
Report::build(ReportKind::Error, "engine_internal", e.span.clone().start)
|
||||
.with_code(e_code)
|
||||
.with_message(format!("{:?}", e.kind))
|
||||
.with_label(
|
||||
Label::new(("engine_internal", e.span.clone()))
|
||||
.with_message(message)
|
||||
.with_color(a),
|
||||
)
|
||||
.finish()
|
||||
.eprint(("engine_internal", Source::from(&code)))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
|
|
|
@ -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],
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,273 @@
|
|||
extern crate alloc;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
use {
|
||||
core::fmt::{Display, Formatter},
|
||||
hashbrown::HashMap,
|
||||
lasso::{Rodeo, Spur},
|
||||
logos::{Lexer, Logos, Span},
|
||||
};
|
||||
|
||||
macro_rules! tokendef {
|
||||
($($opcode:literal),* $(,)?) => {
|
||||
paste::paste! {
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Logos)]
|
||||
#[logos(extras = Rodeo)]
|
||||
#[logos(skip r"[ \t\f]+")]
|
||||
#[logos(skip r"-- .*")]
|
||||
pub enum Token {
|
||||
$(#[token($opcode, |_| hbbytecode::opcode::[<$opcode:upper>])])*
|
||||
OpCode(u8),
|
||||
|
||||
#[regex("[0-9]+", |l| l.slice().parse().ok())]
|
||||
#[regex(
|
||||
"-[0-9]+",
|
||||
|lexer| {
|
||||
Some(u64::from_ne_bytes(lexer.slice().parse::<i64>().ok()?.to_ne_bytes()))
|
||||
},
|
||||
)] Integer(u64),
|
||||
|
||||
#[regex(
|
||||
"r[0-9]+",
|
||||
|lexer| match lexer.slice()[1..].parse() {
|
||||
Ok(n) => Some(n),
|
||||
_ => None
|
||||
},
|
||||
)] Register(u8),
|
||||
|
||||
#[regex(
|
||||
r"\p{XID_Start}\p{XID_Continue}*:",
|
||||
|lexer| lexer.extras.get_or_intern(&lexer.slice()[..lexer.slice().len() - 1]),
|
||||
)] Label(Spur),
|
||||
|
||||
#[regex(
|
||||
r"\p{XID_Start}\p{XID_Continue}*",
|
||||
|lexer| lexer.extras.get_or_intern(lexer.slice()),
|
||||
)] Symbol(Spur),
|
||||
|
||||
#[token("\n")]
|
||||
#[token(";")] ISep,
|
||||
#[token(",")] PSep,
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
tokendef![
|
||||
"nop", "add", "sub", "mul", "and", "or", "xor", "sl", "sr", "srs", "cmp", "cmpu",
|
||||
"dir", "neg", "not", "addi", "muli", "andi", "ori", "xori", "sli", "sri", "srsi",
|
||||
"cmpi", "cmpui", "cp", "swa", "li", "ld", "st", "bmc", "brc", "jmp", "jeq", "jne",
|
||||
"jlt", "jgt", "jltu", "jgtu", "ecall", "addf", "subf", "mulf", "dirf", "fmaf", "negf",
|
||||
"itf", "fti", "addfi", "mulfi",
|
||||
];
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub enum ErrorKind {
|
||||
UnexpectedToken,
|
||||
InvalidToken,
|
||||
UnexpectedEnd,
|
||||
InvalidSymbol,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Error {
|
||||
pub kind: ErrorKind,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
||||
write!(f, "Error {:?} at {:?}", self.kind, self.span)
|
||||
}
|
||||
}
|
||||
|
||||
impl core::error::Error for Error {}
|
||||
|
||||
macro_rules! expect_matches {
|
||||
($self:expr, $($pat:pat),* $(,)?) => {$(
|
||||
let $pat = $self.next()?
|
||||
else { return Err(ErrorKind::UnexpectedToken) };
|
||||
)*}
|
||||
}
|
||||
|
||||
pub fn assembly(code: &str, buf: &mut Vec<u8>) -> Result<(), Error> {
|
||||
struct Assembler<'a> {
|
||||
lexer: Lexer<'a, Token>,
|
||||
buf: &'a mut Vec<u8>,
|
||||
label_map: HashMap<Spur, u64>,
|
||||
to_sub_label: HashMap<usize, Spur>,
|
||||
}
|
||||
|
||||
impl<'a> Assembler<'a> {
|
||||
fn next(&mut self) -> Result<Token, ErrorKind> {
|
||||
match self.lexer.next() {
|
||||
Some(Ok(t)) => Ok(t),
|
||||
Some(Err(())) => Err(ErrorKind::InvalidToken),
|
||||
None => Err(ErrorKind::UnexpectedEnd),
|
||||
}
|
||||
}
|
||||
|
||||
fn assemble(&mut self) -> Result<(), ErrorKind> {
|
||||
use hbbytecode::opcode::*;
|
||||
loop {
|
||||
match self.lexer.next() {
|
||||
Some(Ok(Token::OpCode(op))) => {
|
||||
self.buf.push(op);
|
||||
match op {
|
||||
NOP | ECALL => Ok(()),
|
||||
DIR | DIRF => {
|
||||
expect_matches!(
|
||||
self,
|
||||
Token::Register(r0),
|
||||
Token::PSep,
|
||||
Token::Register(r1),
|
||||
Token::PSep,
|
||||
Token::Register(r2),
|
||||
Token::PSep,
|
||||
Token::Register(r3),
|
||||
);
|
||||
self.buf.extend([r0, r1, r2, r3]);
|
||||
Ok(())
|
||||
}
|
||||
ADD..=CMPU | ADDF..=MULF => {
|
||||
expect_matches!(
|
||||
self,
|
||||
Token::Register(r0),
|
||||
Token::PSep,
|
||||
Token::Register(r1),
|
||||
Token::PSep,
|
||||
Token::Register(r2),
|
||||
);
|
||||
self.buf.extend([r0, r1, r2]);
|
||||
Ok(())
|
||||
}
|
||||
BRC => {
|
||||
expect_matches!(
|
||||
self,
|
||||
Token::Register(r0),
|
||||
Token::PSep,
|
||||
Token::Register(r1),
|
||||
Token::PSep,
|
||||
Token::Integer(count),
|
||||
);
|
||||
self.buf.extend([
|
||||
r0,
|
||||
r1,
|
||||
u8::try_from(count).map_err(|_| ErrorKind::UnexpectedToken)?,
|
||||
]);
|
||||
Ok(())
|
||||
}
|
||||
NEG..=NOT | CP..=SWA | NEGF..=FTI => {
|
||||
expect_matches!(
|
||||
self,
|
||||
Token::Register(r0),
|
||||
Token::PSep,
|
||||
Token::Register(r1),
|
||||
);
|
||||
self.buf.extend([r0, r1]);
|
||||
Ok(())
|
||||
}
|
||||
LI | JMP => {
|
||||
expect_matches!(self, Token::Register(r0), Token::PSep);
|
||||
self.buf.push(r0);
|
||||
self.insert_imm()?;
|
||||
Ok(())
|
||||
}
|
||||
ADDI..=CMPUI | BMC | JEQ..=JGTU | ADDFI..=MULFI => {
|
||||
expect_matches!(
|
||||
self,
|
||||
Token::Register(r0),
|
||||
Token::PSep,
|
||||
Token::Register(r1),
|
||||
Token::PSep,
|
||||
);
|
||||
self.buf.extend([r0, r1]);
|
||||
self.insert_imm()?;
|
||||
Ok(())
|
||||
}
|
||||
LD..=ST => {
|
||||
expect_matches!(
|
||||
self,
|
||||
Token::Register(r0),
|
||||
Token::PSep,
|
||||
Token::Register(r1),
|
||||
Token::PSep,
|
||||
Token::Integer(offset),
|
||||
Token::PSep,
|
||||
Token::Integer(len),
|
||||
);
|
||||
self.buf.extend([r0, r1]);
|
||||
self.buf.extend(offset.to_le_bytes());
|
||||
self.buf.extend(
|
||||
u16::try_from(len)
|
||||
.map_err(|_| ErrorKind::InvalidToken)?
|
||||
.to_le_bytes(),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}?;
|
||||
match self.next() {
|
||||
Ok(Token::ISep) => (),
|
||||
Ok(_) => return Err(ErrorKind::UnexpectedToken),
|
||||
Err(ErrorKind::UnexpectedEnd) => return Ok(()),
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
Some(Ok(Token::Label(lbl))) => {
|
||||
self.label_map.insert(lbl, self.buf.len() as u64);
|
||||
}
|
||||
Some(Ok(Token::ISep)) => (),
|
||||
Some(Ok(_)) => return Err(ErrorKind::UnexpectedToken),
|
||||
Some(Err(())) => return Err(ErrorKind::InvalidToken),
|
||||
None => return Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn link_local_syms(&mut self) -> Result<(), ErrorKind> {
|
||||
for (ix, sym) in &self.to_sub_label {
|
||||
self.label_map
|
||||
.get(sym)
|
||||
.ok_or(ErrorKind::InvalidSymbol)?
|
||||
.to_le_bytes()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.for_each(|(i, b)| {
|
||||
self.buf[ix + i] = *b;
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn insert_imm(&mut self) -> Result<(), ErrorKind> {
|
||||
let imm = match self.next()? {
|
||||
Token::Integer(i) => i.to_le_bytes(),
|
||||
Token::Symbol(s) => {
|
||||
self.to_sub_label.insert(self.buf.len(), s);
|
||||
[0; 8]
|
||||
}
|
||||
_ => return Err(ErrorKind::UnexpectedToken),
|
||||
};
|
||||
self.buf.extend(imm);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
let mut asm = Assembler {
|
||||
lexer: Token::lexer(code),
|
||||
label_map: Default::default(),
|
||||
to_sub_label: Default::default(),
|
||||
buf,
|
||||
};
|
||||
|
||||
asm.assemble().map_err(|kind| Error {
|
||||
kind,
|
||||
span: asm.lexer.span(),
|
||||
})?;
|
||||
|
||||
asm.link_local_syms()
|
||||
.map_err(|kind| Error { kind, span: 0..0 })
|
||||
}
|
|
@ -1,8 +1,6 @@
|
|||
[package]
|
||||
name = "hbbytecode"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
paste = "1.0.14"
|
||||
with_builtin_macros = "0.0.3"
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
/* HoleyBytes Bytecode representation in C
|
||||
* Requires C23 compiler or better
|
||||
*
|
||||
* Uses MSVC pack pragma extension,
|
||||
* proved to work with Clang and GNU® GCC™.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
@ -13,16 +10,15 @@
|
|||
static_assert(CHAR_BIT == 8, "Cursed architectures are not supported");
|
||||
|
||||
enum hbbc_Opcode: uint8_t {
|
||||
hbbc_Op_UN , hbbc_Op_TX , hbbc_Op_NOP , hbbc_Op_ADD , hbbc_Op_SUB , hbbc_Op_MUL ,
|
||||
hbbc_Op_AND , hbbc_Op_OR , hbbc_Op_XOR , hbbc_Op_SL , hbbc_Op_SR , hbbc_Op_SRS ,
|
||||
hbbc_Op_CMP , hbbc_Op_CMPU , hbbc_Op_DIR , hbbc_Op_NEG , hbbc_Op_NOT , hbbc_Op_ADDI ,
|
||||
hbbc_Op_MULI , hbbc_Op_ANDI , hbbc_Op_ORI , hbbc_Op_XORI , hbbc_Op_SLI , hbbc_Op_SRI ,
|
||||
hbbc_Op_SRSI , hbbc_Op_CMPI , hbbc_Op_CMPUI , hbbc_Op_CP , hbbc_Op_SWA , hbbc_Op_LI ,
|
||||
hhbc_Op_LRA , hbbc_Op_LD , hbbc_Op_ST , hbbc_Op_LDR , hhbc_Op_STR , hbbc_Op_BMC ,
|
||||
hbbc_Op_BRC , hbbc_Op_JMP , hbbc_Op_JMPR , hbbc_Op_JAL , hbbc_Op_JALR , hbbc_Op_JEQ ,
|
||||
hbbc_Op_JNE , hbbc_Op_JLT , hbbc_Op_JGT , hbbc_Op_JLTU , hbbc_Op_JGTU , hbbc_Op_ECALL ,
|
||||
hbbc_Op_ADDF , hbbc_Op_SUBF , hbbc_Op_MULF , hbbc_Op_DIRF , hbbc_Op_FMAF , hbbc_Op_NEGF ,
|
||||
hbbc_Op_ITF , hbbc_Op_FTI , hbbc_Op_ADDFI , hbbc_Op_MULFI ,
|
||||
hbbc_Op_NOP , hbbc_Op_ADD , hbbc_Op_SUB , hbbc_Op_MUL , hbbc_Op_AND , hbbc_Op_OR ,
|
||||
hbbc_Op_XOR , hbbc_Op_SL , hbbc_Op_SR , hbbc_Op_SRS , hbbc_Op_CMP , hbbc_Op_CMPU ,
|
||||
hbbc_Op_DIR , hbbc_Op_NEG , hbbc_Op_NOT , hbbc_Op_ADDI , hbbc_Op_MULI , hbbc_Op_ANDI ,
|
||||
hbbc_Op_ORI , hbbc_Op_XORI , hbbc_Op_SLI , hbbc_Op_SRI , hbbc_Op_SRSI , hbbc_Op_CMPI ,
|
||||
hbbc_Op_CMPUI , hbbc_Op_CP , hbbc_Op_SWA , hbbc_Op_LI , hbbc_Op_LD , hbbc_Op_ST ,
|
||||
hbbc_Op_BMC , hbbc_Op_BRC , hbbc_Op_JMP , hbbc_Op_JEQ , hbbc_Op_JNE , hbbc_Op_JLT ,
|
||||
hbbc_Op_JGT , hbbc_Op_JLTU , hbbc_Op_JGTU , hbbc_Op_ECALL , hbbc_Op_ADDF , hbbc_Op_SUBF ,
|
||||
hbbc_Op_MULF , hbbc_Op_DIRF , hbbc_Op_FMAF , hbbc_Op_NEGF , hbbc_Op_ITF , hbbc_Op_FTI ,
|
||||
hbbc_Op_ADDFI , hbbc_Op_MULFI ,
|
||||
} typedef hbbc_Opcode;
|
||||
|
||||
static_assert(sizeof(hbbc_Opcode) == 1);
|
||||
|
@ -43,22 +39,11 @@ struct hbbc_ParamBBDH
|
|||
typedef hbbc_ParamBBDH;
|
||||
static_assert(sizeof(hbbc_ParamBBDH) == 96 / 8);
|
||||
|
||||
struct hbbc_ParamBBWH
|
||||
{ uint8_t _0; uint8_t _1; uint32_t _2; uint16_t _3; }
|
||||
typedef hbbc_ParamBBWH;
|
||||
static_assert(sizeof(hbbc_ParamBBWH) == 64 / 8);
|
||||
|
||||
|
||||
struct hbbc_ParamBBD
|
||||
{ uint8_t _0; uint8_t _1; uint64_t _2; }
|
||||
typedef hbbc_ParamBBD;
|
||||
static_assert(sizeof(hbbc_ParamBBD) == 80 / 8);
|
||||
|
||||
struct hbbc_ParamBBW
|
||||
{ uint8_t _0; uint8_t _1; uint32_t _2; }
|
||||
typedef hbbc_ParamBBW;
|
||||
static_assert(sizeof(hbbc_ParamBBW) == 48 / 8);
|
||||
|
||||
struct hbbc_ParamBB
|
||||
{ uint8_t _0; uint8_t _1; }
|
||||
typedef hbbc_ParamBB;
|
||||
|
|
|
@ -1,120 +0,0 @@
|
|||
0x00, UN, N, "Cause an unreachable code trap" ;
|
||||
0x01, TX, N, "Termiante execution" ;
|
||||
0x02, NOP, N, "Do nothing" ;
|
||||
0x03, ADD8, RRR, "Addition (8b)" ;
|
||||
0x04, ADD16, RRR, "Addition (16b)" ;
|
||||
0x05, ADD32, RRR, "Addition (32b)" ;
|
||||
0x06, ADD64, RRR, "Addition (64b)" ;
|
||||
0x07, SUB8, RRR, "Subtraction (8b)" ;
|
||||
0x08, SUB16, RRR, "Subtraction (16b)" ;
|
||||
0x09, SUB32, RRR, "Subtraction (32b)" ;
|
||||
0x0A, SUB64, RRR, "Subtraction (64b)" ;
|
||||
0x0B, MUL8, RRR, "Multiplication (8b)" ;
|
||||
0x0C, MUL16, RRR, "Multiplication (16b)" ;
|
||||
0x0D, MUL32, RRR, "Multiplication (32b)" ;
|
||||
0x0E, MUL64, RRR, "Multiplication (64b)" ;
|
||||
0x0F, AND, RRR, "Bitand" ;
|
||||
0x10, OR, RRR, "Bitor" ;
|
||||
0x11, XOR, RRR, "Bitxor" ;
|
||||
0x12, SLU8, RRR, "Unsigned left bitshift (8b)" ;
|
||||
0x13, SLU16, RRR, "Unsigned left bitshift (16b)" ;
|
||||
0x14, SLU32, RRR, "Unsigned left bitshift (32b)" ;
|
||||
0x15, SLU64, RRR, "Unsigned left bitshift (64b)" ;
|
||||
0x16, SRU8, RRR, "Unsigned right bitshift (8b)" ;
|
||||
0x17, SRU16, RRR, "Unsigned right bitshift (16b)" ;
|
||||
0x18, SRU32, RRR, "Unsigned right bitshift (32b)" ;
|
||||
0x19, SRU64, RRR, "Unsigned right bitshift (64b)" ;
|
||||
0x1A, SRS8, RRR, "Signed right bitshift (8b)" ;
|
||||
0x1B, SRS16, RRR, "Signed right bitshift (16b)" ;
|
||||
0x1C, SRS32, RRR, "Signed right bitshift (32b)" ;
|
||||
0x1D, SRS64, RRR, "Signed right bitshift (64b)" ;
|
||||
0x1E, CMPU, RRR, "Unsigned comparsion" ;
|
||||
0x1F, CMPS, RRR, "Signed comparsion" ;
|
||||
0x20, DIRU8, RRRR, "Merged divide-remainder (unsigned 8b)" ;
|
||||
0x21, DIRU16, RRRR, "Merged divide-remainder (unsigned 16b)" ;
|
||||
0x22, DIRU32, RRRR, "Merged divide-remainder (unsigned 32b)" ;
|
||||
0x23, DIRU64, RRRR, "Merged divide-remainder (unsigned 64b)" ;
|
||||
0x24, DIRS8, RRRR, "Merged divide-remainder (signed 8b)" ;
|
||||
0x25, DIRS16, RRRR, "Merged divide-remainder (signed 16b)" ;
|
||||
0x26, DIRS32, RRRR, "Merged divide-remainder (signed 32b)" ;
|
||||
0x27, DIRS64, RRRR, "Merged divide-remainder (signed 64b)" ;
|
||||
0x28, NEG, RR, "Bit negation" ;
|
||||
0x29, NOT, RR, "Logical negation" ;
|
||||
0x2A, SXT8, RR, "Sign extend 8b to 64b" ;
|
||||
0x2B, SXT16, RR, "Sign extend 16b to 64b" ;
|
||||
0x2C, SXT32, RR, "Sign extend 32b to 64b" ;
|
||||
0x2D, ADDI8, RRB, "Addition with immediate (8b)" ;
|
||||
0x2E, ADDI16, RRH, "Addition with immediate (16b)" ;
|
||||
0x2F, ADDI32, RRW, "Addition with immediate (32b)" ;
|
||||
0x30, ADDI64, RRD, "Addition with immediate (64b)" ;
|
||||
0x31, MULI8, RRB, "Multiplication with immediate (8b)" ;
|
||||
0x32, MULI16, RRH, "Multiplication with immediate (16b)" ;
|
||||
0x33, MULI32, RRW, "Multiplication with immediate (32b)" ;
|
||||
0x34, MULI64, RRD, "Multiplication with immediate (64b)" ;
|
||||
0x35, ANDI, RRD, "Bitand with immediate" ;
|
||||
0x36, ORI, RRD, "Bitor with immediate" ;
|
||||
0x37, XORI, RRD, "Bitxor with immediate" ;
|
||||
0x38, SLUI8, RRB, "Unsigned left bitshift with immedidate (8b)" ;
|
||||
0x39, SLUI16, RRB, "Unsigned left bitshift with immedidate (16b)";
|
||||
0x3A, SLUI32, RRB, "Unsigned left bitshift with immedidate (32b)";
|
||||
0x3B, SLUI64, RRB, "Unsigned left bitshift with immedidate (64b)";
|
||||
0x3C, SRUI8, RRB, "Unsigned right bitshift with immediate (8b)" ;
|
||||
0x3D, SRUI16, RRB, "Unsigned right bitshift with immediate (16b)";
|
||||
0x3E, SRUI32, RRB, "Unsigned right bitshift with immediate (32b)";
|
||||
0x3F, SRUI64, RRB, "Unsigned right bitshift with immediate (64b)";
|
||||
0x40, SRSI8, RRB, "Signed right bitshift with immediate" ;
|
||||
0x41, SRSI16, RRB, "Signed right bitshift with immediate" ;
|
||||
0x42, SRSI32, RRB, "Signed right bitshift with immediate" ;
|
||||
0x43, SRSI64, RRB, "Signed right bitshift with immediate" ;
|
||||
0x44, CMPUI, RRD, "Unsigned compare with immediate" ;
|
||||
0x45, CMPSI, RRD, "Signed compare with immediate" ;
|
||||
0x46, CP, RR, "Copy register" ;
|
||||
0x47, SWA, RR, "Swap registers" ;
|
||||
0x48, LI8, RB, "Load immediate (8b)" ;
|
||||
0x49, LI16, RH, "Load immediate (16b)" ;
|
||||
0x4A, LI32, RW, "Load immediate (32b)" ;
|
||||
0x4B, LI64, RD, "Load immediate (64b)" ;
|
||||
0x4C, LRA, RRO, "Load relative address" ;
|
||||
0x4D, LD, RRAH, "Load from absolute address" ;
|
||||
0x4E, ST, RRAH, "Store to absolute address" ;
|
||||
0x4F, LDR, RROH, "Load from relative address" ;
|
||||
0x50, STR, RROH, "Store to relative address" ;
|
||||
0x51, BMC, RRH, "Copy block of memory" ;
|
||||
0x52, BRC, RRB, "Copy register block" ;
|
||||
0x53, JMP, O, "Relative jump" ;
|
||||
0x54, JAL, RRO, "Linking relative jump" ;
|
||||
0x55, JALA, RRA, "Linking absolute jump" ;
|
||||
0x56, JEQ, RRP, "Branch on equal" ;
|
||||
0x57, JNE, RRP, "Branch on nonequal" ;
|
||||
0x58, JLTU, RRP, "Branch on lesser-than (unsigned)" ;
|
||||
0x59, JGTU, RRP, "Branch on greater-than (unsigned)" ;
|
||||
0x5A, JLTS, RRP, "Branch on lesser-than (signed)" ;
|
||||
0x5B, JGTS, RRP, "Branch on greater-than (signed)" ;
|
||||
0x5C, ECA, N, "Environment call trap" ;
|
||||
0x5D, EBP, N, "Environment breakpoint" ;
|
||||
0x5E, FADD32, RRR, "Floating point addition (32b)" ;
|
||||
0x5F, FADD64, RRR, "Floating point addition (64b)" ;
|
||||
0x60, FSUB32, RRR, "Floating point subtraction (32b)" ;
|
||||
0x61, FSUB64, RRR, "Floating point subtraction (64b)" ;
|
||||
0x62, FMUL32, RRR, "Floating point multiply (32b)" ;
|
||||
0x63, FMUL64, RRR, "Floating point multiply (64b)" ;
|
||||
0x64, FDIV32, RRR, "Floating point division (32b)" ;
|
||||
0x65, FDIV64, RRR, "Floating point division (64b)" ;
|
||||
0x66, FMA32, RRRR, "Float fused multiply-add (32b)" ;
|
||||
0x67, FMA64, RRRR, "Float fused multiply-add (64b)" ;
|
||||
0x68, FINV32, RR, "Float reciprocal (32b)" ;
|
||||
0x69, FINV64, RR, "Float reciprocal (64b)" ;
|
||||
0x6A, FCMPLT32, RRR, "Flaot compare less than (32b)" ;
|
||||
0x6B, FCMPLT64, RRR, "Flaot compare less than (64b)" ;
|
||||
0x6C, FCMPGT32, RRR, "Flaot compare greater than (32b)" ;
|
||||
0x6D, FCMPGT64, RRR, "Flaot compare greater than (64b)" ;
|
||||
0x6E, ITF32, RR, "Int to 32 bit float" ;
|
||||
0x6F, ITF64, RR, "Int to 64 bit float" ;
|
||||
0x70, FTI32, RRB, "Float 32 to int" ;
|
||||
0x71, FTI64, RRB, "Float 64 to int" ;
|
||||
0x72, FC32T64, RR, "Float 64 to Float 32" ;
|
||||
0x73, FC64T32, RRB, "Float 32 to Float 64" ;
|
||||
0x74, LRA16, RRP, "Load relative immediate (16 bit)" ;
|
||||
0x75, LDR16, RRPH, "Load from relative address (16 bit)" ;
|
||||
0x76, STR16, RRPH, "Store to relative address (16 bit)" ;
|
||||
0x77, JMP16, P, "Relative jump (16 bit)" ;
|
|
@ -1,185 +1,107 @@
|
|||
#![no_std]
|
||||
|
||||
use core::convert::TryFrom;
|
||||
macro_rules! constmod {
|
||||
($vis:vis $mname:ident($repr:ty) {
|
||||
$(#![doc = $mdoc:literal])?
|
||||
$($cname:ident = $val:expr $(,$doc:literal)?;)*
|
||||
}) => {
|
||||
$(#[doc = $mdoc])?
|
||||
$vis mod $mname {
|
||||
$(
|
||||
$(#[doc = $doc])?
|
||||
pub const $cname: $repr = $val;
|
||||
)*
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
type OpR = u8;
|
||||
constmod!(pub opcode(u8) {
|
||||
//! Opcode constant module
|
||||
|
||||
type OpA = u64;
|
||||
type OpO = i32;
|
||||
type OpP = i16;
|
||||
NOP = 0, "N; Do nothing";
|
||||
|
||||
type OpB = u8;
|
||||
type OpH = u16;
|
||||
type OpW = u32;
|
||||
type OpD = u64;
|
||||
ADD = 1, "BBB; #0 ← #1 + #2";
|
||||
SUB = 2, "BBB; #0 ← #1 - #2";
|
||||
MUL = 3, "BBB; #0 ← #1 × #2";
|
||||
AND = 4, "BBB; #0 ← #1 & #2";
|
||||
OR = 5, "BBB; #0 ← #1 | #2";
|
||||
XOR = 6, "BBB; #0 ← #1 ^ #2";
|
||||
SL = 7, "BBB; #0 ← #1 « #2";
|
||||
SR = 8, "BBB; #0 ← #1 » #2";
|
||||
SRS = 9, "BBB; #0 ← #1 » #2 (signed)";
|
||||
CMP = 10, "BBB; #0 ← #1 <=> #2";
|
||||
CMPU = 11, "BBB; #0 ← #1 <=> #2 (unsigned)";
|
||||
DIR = 12, "BBBB; #0 ← #2 / #3, #1 ← #2 % #3";
|
||||
NEG = 13, "BB; #0 ← -#1";
|
||||
NOT = 14, "BB; #0 ← !#1";
|
||||
|
||||
ADDI = 15, "BBD; #0 ← #1 + imm #2";
|
||||
MULI = 16, "BBD; #0 ← #1 × imm #2";
|
||||
ANDI = 17, "BBD; #0 ← #1 & imm #2";
|
||||
ORI = 18, "BBD; #0 ← #1 | imm #2";
|
||||
XORI = 19, "BBD; #0 ← #1 ^ imm #2";
|
||||
SLI = 20, "BBD; #0 ← #1 « imm #2";
|
||||
SRI = 21, "BBD; #0 ← #1 » imm #2";
|
||||
SRSI = 22, "BBD; #0 ← #1 » imm #2 (signed)";
|
||||
CMPI = 23, "BBD; #0 ← #1 <=> imm #2";
|
||||
CMPUI = 24, "BBD; #0 ← #1 <=> imm #2 (unsigned)";
|
||||
|
||||
CP = 25, "BB; Copy #0 ← #1";
|
||||
SWA = 26, "BB; Swap #0 and #1";
|
||||
LI = 27, "BD; #0 ← imm #1";
|
||||
LD = 28, "BBDB; #0 ← [#1 + imm #3], imm #4 bytes, overflowing";
|
||||
ST = 29, "BBDB; [#1 + imm #3] ← #0, imm #4 bytes, overflowing";
|
||||
BMC = 30, "BBD; [#0] ← [#1], imm #2 bytes";
|
||||
BRC = 31, "BBB; #0 ← #1, imm #2 registers";
|
||||
|
||||
JMP = 32, "BD; Unconditional jump [#0 + imm #1]";
|
||||
JEQ = 33, "BBD; if #0 = #1 → jump imm #2";
|
||||
JNE = 34, "BBD; if #0 ≠ #1 → jump imm #2";
|
||||
JLT = 35, "BBD; if #0 < #1 → jump imm #2";
|
||||
JGT = 36, "BBD; if #0 > #1 → jump imm #2";
|
||||
JLTU = 37, "BBD; if #0 < #1 → jump imm #2 (unsigned)";
|
||||
JGTU = 38, "BBD; if #0 > #1 → jump imm #2 (unsigned)";
|
||||
ECALL = 39, "N; Issue system call";
|
||||
|
||||
ADDF = 40, "BBB; #0 ← #1 +. #2";
|
||||
SUBF = 41, "BBB; #0 ← #1 -. #2";
|
||||
MULF = 42, "BBB; #0 ← #1 +. #2";
|
||||
DIRF = 43, "BBBB; #0 ← #2 / #3, #1 ← #2 % #3";
|
||||
FMAF = 44, "BBBB; #0 ← (#1 * #2) + #3";
|
||||
NEGF = 45, "BB; #0 ← -#1";
|
||||
ITF = 46, "BB; #0 ← #1 as float";
|
||||
FTI = 47, "BB; #0 ← #1 as int";
|
||||
|
||||
ADDFI = 48, "BBD; #0 ← #1 +. imm #2";
|
||||
MULFI = 49, "BBD; #0 ← #1 *. imm #2";
|
||||
});
|
||||
|
||||
#[repr(packed)]
|
||||
pub struct ParamBBBB(pub u8, pub u8, pub u8, pub u8);
|
||||
|
||||
#[repr(packed)]
|
||||
pub struct ParamBBB(pub u8, pub u8, pub u8);
|
||||
|
||||
#[repr(packed)]
|
||||
pub struct ParamBBDH(pub u8, pub u8, pub u64, pub u16);
|
||||
|
||||
#[repr(packed)]
|
||||
pub struct ParamBBD(pub u8, pub u8, pub u64);
|
||||
|
||||
#[repr(packed)]
|
||||
pub struct ParamBB(pub u8, pub u8);
|
||||
|
||||
#[repr(packed)]
|
||||
pub struct ParamBD(pub u8, pub u64);
|
||||
|
||||
/// # Safety
|
||||
/// Has to be valid to be decoded from bytecode.
|
||||
pub unsafe trait BytecodeItem {}
|
||||
macro_rules! define_items {
|
||||
($($name:ident ($($nm:ident: $item:ident),* $(,)?)),* $(,)?) => {
|
||||
$(
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
#[repr(packed)]
|
||||
pub struct $name($(pub $item),*);
|
||||
unsafe impl BytecodeItem for $name {}
|
||||
|
||||
impl Encodable for $name {
|
||||
fn encode(self, _buffer: &mut impl Buffer) {
|
||||
let Self($($nm),*) = self;
|
||||
$(
|
||||
for byte in $nm.to_le_bytes() {
|
||||
unsafe { _buffer.write(byte) };
|
||||
}
|
||||
)*
|
||||
}
|
||||
|
||||
fn encode_len(self) -> usize {
|
||||
core::mem::size_of::<Self>()
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
define_items! {
|
||||
OpsRR (a: OpR, b: OpR ),
|
||||
OpsRRR (a: OpR, b: OpR, c: OpR ),
|
||||
OpsRRRR (a: OpR, b: OpR, c: OpR, d: OpR),
|
||||
OpsRRB (a: OpR, b: OpR, c: OpB ),
|
||||
OpsRRH (a: OpR, b: OpR, c: OpH ),
|
||||
OpsRRW (a: OpR, b: OpR, c: OpW ),
|
||||
OpsRRD (a: OpR, b: OpR, c: OpD ),
|
||||
OpsRB (a: OpR, b: OpB ),
|
||||
OpsRH (a: OpR, b: OpH ),
|
||||
OpsRW (a: OpR, b: OpW ),
|
||||
OpsRD (a: OpR, b: OpD ),
|
||||
OpsRRA (a: OpR, b: OpR, c: OpA ),
|
||||
OpsRRAH (a: OpR, b: OpR, c: OpA, d: OpH),
|
||||
OpsRROH (a: OpR, b: OpR, c: OpO, d: OpH),
|
||||
OpsRRPH (a: OpR, b: OpR, c: OpP, d: OpH),
|
||||
OpsRRO (a: OpR, b: OpR, c: OpO ),
|
||||
OpsRRP (a: OpR, b: OpR, c: OpP ),
|
||||
OpsO (a: OpO, ),
|
||||
OpsP (a: OpP, ),
|
||||
OpsN ( ),
|
||||
}
|
||||
|
||||
unsafe impl BytecodeItem for u8 {}
|
||||
|
||||
::with_builtin_macros::with_builtin! {
|
||||
let $spec = include_from_root!("instructions.in") in {
|
||||
/// Invoke macro with bytecode definition
|
||||
///
|
||||
/// # Format
|
||||
/// ```text
|
||||
/// Opcode, Mnemonic, Type, Docstring;
|
||||
/// ```
|
||||
///
|
||||
/// # Type
|
||||
/// ```text
|
||||
/// Types consist of letters meaning a single field
|
||||
/// | Type | Size (B) | Meaning |
|
||||
/// |:-----|:---------|:------------------------|
|
||||
/// | N | 0 | Empty |
|
||||
/// | R | 1 | Register |
|
||||
/// | A | 8 | Absolute address |
|
||||
/// | O | 4 | Relative address offset |
|
||||
/// | P | 2 | Relative address offset |
|
||||
/// | B | 1 | Immediate |
|
||||
/// | H | 2 | Immediate |
|
||||
/// | W | 4 | Immediate |
|
||||
/// | D | 8 | Immediate |
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! invoke_with_def {
|
||||
($($macro:tt)*) => {
|
||||
$($macro)*! { $spec }
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Buffer {
|
||||
fn reserve(&mut self, bytes: usize);
|
||||
/// # Safety
|
||||
/// Reserve needs to be called before this function, and only reserved amount can be written.
|
||||
unsafe fn write(&mut self, byte: u8);
|
||||
}
|
||||
|
||||
pub trait Encodable {
|
||||
fn encode(self, buffer: &mut impl Buffer);
|
||||
fn encode_len(self) -> usize;
|
||||
}
|
||||
|
||||
macro_rules! gen_opcodes {
|
||||
($($opcode:expr, $mnemonic:ident, $ty:ident, $doc:literal;)*) => {
|
||||
pub mod opcode {
|
||||
$(
|
||||
#[doc = $doc]
|
||||
pub const $mnemonic: u8 = $opcode;
|
||||
)*
|
||||
|
||||
paste::paste! {
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
#[repr(u8)]
|
||||
pub enum Op { $(
|
||||
[< $mnemonic:lower:camel >](super::[<Ops $ty>]),
|
||||
)* }
|
||||
|
||||
impl Op {
|
||||
pub fn size(&self) -> usize {
|
||||
(match self {
|
||||
$(Self::[<$mnemonic:lower:camel>] { .. } => core::mem::size_of::<super::[<Ops $ty>]>(),)*
|
||||
}) + 1
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::Encodable for Op {
|
||||
fn encode(self, buffer: &mut impl crate::Buffer) {
|
||||
match self {
|
||||
$(
|
||||
Self::[< $mnemonic:lower:camel >](op) => {
|
||||
unsafe { buffer.write($opcode) };
|
||||
op.encode(buffer);
|
||||
}
|
||||
)*
|
||||
}
|
||||
}
|
||||
|
||||
fn encode_len(self) -> usize {
|
||||
match self {
|
||||
$(
|
||||
Self::[< $mnemonic:lower:camel >](op) => {
|
||||
1 + crate::Encodable::encode_len(op)
|
||||
}
|
||||
)*
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Rounding mode
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
pub enum RoundingMode {
|
||||
NearestEven = 0,
|
||||
Truncate = 1,
|
||||
Up = 2,
|
||||
Down = 3,
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for RoundingMode {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
(value <= 3)
|
||||
.then(|| unsafe { core::mem::transmute(value) })
|
||||
.ok_or(())
|
||||
}
|
||||
}
|
||||
|
||||
invoke_with_def!(gen_opcodes);
|
||||
pub unsafe trait OpParam {}
|
||||
unsafe impl OpParam for ParamBBBB {}
|
||||
unsafe impl OpParam for ParamBBB {}
|
||||
unsafe impl OpParam for ParamBBDH {}
|
||||
unsafe impl OpParam for ParamBBD {}
|
||||
unsafe impl OpParam for ParamBB {}
|
||||
unsafe impl OpParam for ParamBD {}
|
||||
unsafe impl OpParam for u64 {}
|
||||
unsafe impl OpParam for () {}
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
[package]
|
||||
name = "hblang"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[[bin]]
|
||||
name = "hbc"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
|
||||
[dev-dependencies]
|
||||
hbvm = { path = "../hbvm", features = ["nightly"] }
|
136
hblang/build.rs
136
hblang/build.rs
|
@ -1,136 +0,0 @@
|
|||
#![feature(iter_next_chunk)]
|
||||
use std::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");
|
||||
|
||||
let mut generated = String::new();
|
||||
|
||||
gen_max_size(&mut generated)?;
|
||||
gen_encodes(&mut generated)?;
|
||||
gen_structs(&mut generated)?;
|
||||
gen_name_list(&mut generated)?;
|
||||
|
||||
std::fs::write("src/instrs.rs", generated)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn gen_name_list(generated: &mut String) -> Result<(), Box<dyn std::error::Error>> {
|
||||
writeln!(
|
||||
generated,
|
||||
"pub const NAMES: [&str; {}] = [",
|
||||
instructions().count()
|
||||
)?;
|
||||
for [_, name, _, _] in instructions() {
|
||||
writeln!(generated, " \"{}\",", name.to_lowercase())?;
|
||||
}
|
||||
writeln!(generated, "];")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn gen_max_size(generated: &mut String) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let max = instructions()
|
||||
.map(|[_, _, ty, _]| {
|
||||
if ty == "N" {
|
||||
1
|
||||
} else {
|
||||
iter_args(ty).map(|(_, c)| arg_to_width(c)).sum::<usize>() + 1
|
||||
}
|
||||
})
|
||||
.max()
|
||||
.unwrap();
|
||||
|
||||
writeln!(generated, "pub const MAX_SIZE: usize = {};", max)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn gen_encodes(generated: &mut String) -> Result<(), Box<dyn std::error::Error>> {
|
||||
for [op, name, ty, doc] in instructions() {
|
||||
writeln!(generated, "/// {}", doc.trim_matches('"'))?;
|
||||
let name = name.to_lowercase();
|
||||
let args = comma_sep(
|
||||
iter_args(ty).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).map(|(i, c)| format!("{}{i}", arg_to_name(c))));
|
||||
writeln!(
|
||||
generated,
|
||||
" unsafe {{ crate::encode({ty}({op}, {arg_names})) }}"
|
||||
)?;
|
||||
writeln!(generated, "}}")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn gen_structs(generated: &mut String) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for [_, _, ty, _] in instructions() {
|
||||
if !seen.insert(ty) {
|
||||
continue;
|
||||
}
|
||||
let types = comma_sep(iter_args(ty).map(|(_, c)| arg_to_type(c).to_string()));
|
||||
writeln!(generated, "#[repr(packed)] pub struct {ty}(u8, {types});")?;
|
||||
}
|
||||
|
||||
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 = (usize, char)> {
|
||||
ty.chars().enumerate().filter(|(_, c)| *c != 'N')
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
main := fn(): int {
|
||||
return 10 - 20 / 2 + 4 * (2 + 2) - 4 * 4 + 1;
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
|
||||
Color := struct {
|
||||
r: u8,
|
||||
g: u8,
|
||||
b: u8,
|
||||
a: u8,
|
||||
}
|
||||
|
||||
Point := struct {
|
||||
x: u32,
|
||||
y: u32,
|
||||
}
|
||||
|
||||
Pixel := struct {
|
||||
color: Color,
|
||||
point: Point,
|
||||
}
|
||||
|
||||
main := fn(): int {
|
||||
pixel := Pixel.{
|
||||
color: Color.{
|
||||
r: 255,
|
||||
g: 0,
|
||||
b: 0,
|
||||
a: 255,
|
||||
},
|
||||
point: Point.{
|
||||
x: 0,
|
||||
y: 2,
|
||||
},
|
||||
};
|
||||
|
||||
if *(&pixel.point.x + 1) != 2 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if *(&pixel.point.y - 1) != 0 {
|
||||
return 64;
|
||||
}
|
||||
|
||||
return pixel.point.x + pixel.point.y + pixel.color.r
|
||||
+ pixel.color.g + pixel.color.b + pixel.color.a;
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
|
||||
Type := struct {
|
||||
brah: int,
|
||||
blah: int,
|
||||
}
|
||||
|
||||
main := fn(): int {
|
||||
return @eca(int, 1, Type.(10, 20), @sizeof(Type), @alignof(Type), 5, 6);
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
arm_fb_ptr := fn(): int return 100;
|
||||
x86_fb_ptr := fn(): int return 100;
|
||||
|
||||
|
||||
check_platform := fn(): int {
|
||||
return x86_fb_ptr();
|
||||
}
|
||||
|
||||
set_pixel := fn(x: int, y: int, width: int): int {
|
||||
pix_offset := y * width + x;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
main := fn(): int {
|
||||
fb_ptr := check_platform();
|
||||
width := 100;
|
||||
height := 30;
|
||||
x:= 0;
|
||||
y:= 0;
|
||||
|
||||
loop {
|
||||
if x <= height + 1 {
|
||||
set_pixel(x,y,width);
|
||||
x = x + 1;
|
||||
} else {
|
||||
set_pixel(x,y,width);
|
||||
x = 0;
|
||||
y = y + 1;
|
||||
}
|
||||
if y == width {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
|
||||
main := fn(): int {
|
||||
return add_one(10) + add_two(20);
|
||||
}
|
||||
|
||||
add_two := fn(x: int): int {
|
||||
return x + 2;
|
||||
}
|
||||
|
||||
add_one := fn(x: int): int {
|
||||
return x + 1;
|
||||
}
|
||||
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
|
||||
main := fn(): int {
|
||||
return fib(10);
|
||||
}
|
||||
|
||||
fib := fn(x: int): int {
|
||||
if x <= 2 {
|
||||
return 1;
|
||||
} else {
|
||||
return fib(x - 1) + fib(x - 2);
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
main := fn(): int {
|
||||
return fib(10);
|
||||
}
|
||||
|
||||
fib := fn(n: int): int {
|
||||
a := 0;
|
||||
b := 1;
|
||||
loop {
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
c := a + b;
|
||||
a = b;
|
||||
b = c;
|
||||
n = n - 1;
|
||||
|
||||
stack_reclamation_edge_case := 0;
|
||||
|
||||
continue;
|
||||
}
|
||||
return a;
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
main := fn(): int {
|
||||
return 1;
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
main := fn(): int {
|
||||
a := 1;
|
||||
b := &a;
|
||||
modify(b);
|
||||
drop(a);
|
||||
stack_reclamation_edge_case := 0;
|
||||
return *b - 2;
|
||||
}
|
||||
|
||||
modify := fn(a: *int): void {
|
||||
*a = 2;
|
||||
return;
|
||||
}
|
||||
|
||||
drop := fn(a: int): void {
|
||||
return;
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
Point := struct {
|
||||
x: int,
|
||||
y: int,
|
||||
}
|
||||
|
||||
Rect := struct {
|
||||
a: Point,
|
||||
b: Point,
|
||||
}
|
||||
|
||||
main := fn(): int {
|
||||
a := Point.(1, 2);
|
||||
b := Point.(3, 4);
|
||||
|
||||
d := Rect.(a + b, b - a);
|
||||
d2 := Rect.(Point.(0, 0) - b, a);
|
||||
d2 = d2 + d;
|
||||
|
||||
c := d2.a + d2.b;
|
||||
return c.x + c.y;
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
Ty := struct {
|
||||
a: int,
|
||||
b: int,
|
||||
}
|
||||
|
||||
Ty2 := struct {
|
||||
ty: Ty,
|
||||
c: int,
|
||||
}
|
||||
|
||||
main := fn(): int {
|
||||
finst := Ty2.{ ty: Ty.{ a: 4, b: 1 }, c: 3 };
|
||||
inst := odher_pass(finst);
|
||||
if inst.c == 3 {
|
||||
return pass(&inst.ty);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
pass := fn(t: *Ty): int {
|
||||
return t.a - t.b;
|
||||
}
|
||||
|
||||
odher_pass := fn(t: Ty2): Ty2 {
|
||||
return t;
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
main := fn(): int {
|
||||
a := 1;
|
||||
b := 2;
|
||||
a = a + 1;
|
||||
return a - b;
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -1,25 +0,0 @@
|
|||
pub type Ident = u32;
|
||||
|
||||
const LEN_BITS: u32 = 6;
|
||||
|
||||
pub fn len(ident: Ident) -> u32 {
|
||||
ident & ((1 << LEN_BITS) - 1)
|
||||
}
|
||||
|
||||
pub fn is_null(ident: Ident) -> bool {
|
||||
(ident >> LEN_BITS) == 0
|
||||
}
|
||||
|
||||
pub fn pos(ident: Ident) -> u32 {
|
||||
(ident >> LEN_BITS).saturating_sub(1)
|
||||
}
|
||||
|
||||
pub fn new(pos: u32, len: u32) -> Ident {
|
||||
debug_assert!(len < (1 << LEN_BITS));
|
||||
((pos + 1) << LEN_BITS) | len
|
||||
}
|
||||
|
||||
pub fn range(ident: Ident) -> std::ops::Range<usize> {
|
||||
let (len, pos) = (len(ident) as usize, pos(ident) as usize);
|
||||
pos..pos + len
|
||||
}
|
|
@ -1,623 +0,0 @@
|
|||
pub const MAX_SIZE: usize = 13;
|
||||
/// Cause an unreachable code trap
|
||||
pub fn un() -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(N(0x00, )) }
|
||||
}
|
||||
/// Termiante execution
|
||||
pub fn tx() -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(N(0x01, )) }
|
||||
}
|
||||
/// Do nothing
|
||||
pub fn nop() -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(N(0x02, )) }
|
||||
}
|
||||
/// Addition (8b)
|
||||
pub fn add8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x03, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Addition (16b)
|
||||
pub fn add16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x04, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Addition (32b)
|
||||
pub fn add32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x05, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Addition (64b)
|
||||
pub fn add64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x06, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Subtraction (8b)
|
||||
pub fn sub8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x07, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Subtraction (16b)
|
||||
pub fn sub16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x08, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Subtraction (32b)
|
||||
pub fn sub32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x09, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Subtraction (64b)
|
||||
pub fn sub64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x0A, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Multiplication (8b)
|
||||
pub fn mul8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x0B, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Multiplication (16b)
|
||||
pub fn mul16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x0C, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Multiplication (32b)
|
||||
pub fn mul32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x0D, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Multiplication (64b)
|
||||
pub fn mul64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x0E, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Bitand
|
||||
pub fn and(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x0F, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Bitor
|
||||
pub fn or(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x10, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Bitxor
|
||||
pub fn xor(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x11, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Unsigned left bitshift (8b)
|
||||
pub fn slu8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x12, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Unsigned left bitshift (16b)
|
||||
pub fn slu16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x13, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Unsigned left bitshift (32b)
|
||||
pub fn slu32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x14, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Unsigned left bitshift (64b)
|
||||
pub fn slu64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x15, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Unsigned right bitshift (8b)
|
||||
pub fn sru8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x16, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Unsigned right bitshift (16b)
|
||||
pub fn sru16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x17, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Unsigned right bitshift (32b)
|
||||
pub fn sru32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x18, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Unsigned right bitshift (64b)
|
||||
pub fn sru64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x19, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Signed right bitshift (8b)
|
||||
pub fn srs8(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x1A, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Signed right bitshift (16b)
|
||||
pub fn srs16(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x1B, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Signed right bitshift (32b)
|
||||
pub fn srs32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x1C, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Signed right bitshift (64b)
|
||||
pub fn srs64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x1D, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Unsigned comparsion
|
||||
pub fn cmpu(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x1E, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Signed comparsion
|
||||
pub fn cmps(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x1F, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Merged divide-remainder (unsigned 8b)
|
||||
pub fn diru8(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRRR(0x20, reg0, reg1, reg2, reg3)) }
|
||||
}
|
||||
/// Merged divide-remainder (unsigned 16b)
|
||||
pub fn diru16(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRRR(0x21, reg0, reg1, reg2, reg3)) }
|
||||
}
|
||||
/// Merged divide-remainder (unsigned 32b)
|
||||
pub fn diru32(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRRR(0x22, reg0, reg1, reg2, reg3)) }
|
||||
}
|
||||
/// Merged divide-remainder (unsigned 64b)
|
||||
pub fn diru64(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRRR(0x23, reg0, reg1, reg2, reg3)) }
|
||||
}
|
||||
/// Merged divide-remainder (signed 8b)
|
||||
pub fn dirs8(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRRR(0x24, reg0, reg1, reg2, reg3)) }
|
||||
}
|
||||
/// Merged divide-remainder (signed 16b)
|
||||
pub fn dirs16(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRRR(0x25, reg0, reg1, reg2, reg3)) }
|
||||
}
|
||||
/// Merged divide-remainder (signed 32b)
|
||||
pub fn dirs32(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRRR(0x26, reg0, reg1, reg2, reg3)) }
|
||||
}
|
||||
/// Merged divide-remainder (signed 64b)
|
||||
pub fn dirs64(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRRR(0x27, reg0, reg1, reg2, reg3)) }
|
||||
}
|
||||
/// Bit negation
|
||||
pub fn neg(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RR(0x28, reg0, reg1)) }
|
||||
}
|
||||
/// Logical negation
|
||||
pub fn not(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RR(0x29, reg0, reg1)) }
|
||||
}
|
||||
/// Sign extend 8b to 64b
|
||||
pub fn sxt8(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RR(0x2A, reg0, reg1)) }
|
||||
}
|
||||
/// Sign extend 16b to 64b
|
||||
pub fn sxt16(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RR(0x2B, reg0, reg1)) }
|
||||
}
|
||||
/// Sign extend 32b to 64b
|
||||
pub fn sxt32(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RR(0x2C, reg0, reg1)) }
|
||||
}
|
||||
/// Addition with immediate (8b)
|
||||
pub fn addi8(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x2D, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Addition with immediate (16b)
|
||||
pub fn addi16(reg0: u8, reg1: u8, imm2: u16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRH(0x2E, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Addition with immediate (32b)
|
||||
pub fn addi32(reg0: u8, reg1: u8, imm2: u32) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRW(0x2F, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Addition with immediate (64b)
|
||||
pub fn addi64(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRD(0x30, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Multiplication with immediate (8b)
|
||||
pub fn muli8(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x31, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Multiplication with immediate (16b)
|
||||
pub fn muli16(reg0: u8, reg1: u8, imm2: u16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRH(0x32, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Multiplication with immediate (32b)
|
||||
pub fn muli32(reg0: u8, reg1: u8, imm2: u32) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRW(0x33, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Multiplication with immediate (64b)
|
||||
pub fn muli64(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRD(0x34, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Bitand with immediate
|
||||
pub fn andi(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRD(0x35, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Bitor with immediate
|
||||
pub fn ori(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRD(0x36, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Bitxor with immediate
|
||||
pub fn xori(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRD(0x37, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Unsigned left bitshift with immedidate (8b)
|
||||
pub fn slui8(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x38, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Unsigned left bitshift with immedidate (16b)
|
||||
pub fn slui16(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x39, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Unsigned left bitshift with immedidate (32b)
|
||||
pub fn slui32(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x3A, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Unsigned left bitshift with immedidate (64b)
|
||||
pub fn slui64(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x3B, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Unsigned right bitshift with immediate (8b)
|
||||
pub fn srui8(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x3C, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Unsigned right bitshift with immediate (16b)
|
||||
pub fn srui16(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x3D, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Unsigned right bitshift with immediate (32b)
|
||||
pub fn srui32(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x3E, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Unsigned right bitshift with immediate (64b)
|
||||
pub fn srui64(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x3F, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Signed right bitshift with immediate
|
||||
pub fn srsi8(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x40, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Signed right bitshift with immediate
|
||||
pub fn srsi16(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x41, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Signed right bitshift with immediate
|
||||
pub fn srsi32(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x42, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Signed right bitshift with immediate
|
||||
pub fn srsi64(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x43, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Unsigned compare with immediate
|
||||
pub fn cmpui(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRD(0x44, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Signed compare with immediate
|
||||
pub fn cmpsi(reg0: u8, reg1: u8, imm2: u64) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRD(0x45, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Copy register
|
||||
pub fn cp(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RR(0x46, reg0, reg1)) }
|
||||
}
|
||||
/// Swap registers
|
||||
pub fn swa(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RR(0x47, reg0, reg1)) }
|
||||
}
|
||||
/// Load immediate (8b)
|
||||
pub fn li8(reg0: u8, imm1: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RB(0x48, reg0, imm1)) }
|
||||
}
|
||||
/// Load immediate (16b)
|
||||
pub fn li16(reg0: u8, imm1: u16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RH(0x49, reg0, imm1)) }
|
||||
}
|
||||
/// Load immediate (32b)
|
||||
pub fn li32(reg0: u8, imm1: u32) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RW(0x4A, reg0, imm1)) }
|
||||
}
|
||||
/// Load immediate (64b)
|
||||
pub fn li64(reg0: u8, imm1: u64) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RD(0x4B, reg0, imm1)) }
|
||||
}
|
||||
/// Load relative address
|
||||
pub fn lra(reg0: u8, reg1: u8, offset2: i32) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRO(0x4C, reg0, reg1, offset2)) }
|
||||
}
|
||||
/// Load from absolute address
|
||||
pub fn ld(reg0: u8, reg1: u8, addr2: u64, imm3: u16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRAH(0x4D, reg0, reg1, addr2, imm3)) }
|
||||
}
|
||||
/// Store to absolute address
|
||||
pub fn st(reg0: u8, reg1: u8, addr2: u64, imm3: u16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRAH(0x4E, reg0, reg1, addr2, imm3)) }
|
||||
}
|
||||
/// Load from relative address
|
||||
pub fn ldr(reg0: u8, reg1: u8, offset2: i32, imm3: u16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RROH(0x4F, reg0, reg1, offset2, imm3)) }
|
||||
}
|
||||
/// Store to relative address
|
||||
pub fn str(reg0: u8, reg1: u8, offset2: i32, imm3: u16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RROH(0x50, reg0, reg1, offset2, imm3)) }
|
||||
}
|
||||
/// Copy block of memory
|
||||
pub fn bmc(reg0: u8, reg1: u8, imm2: u16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRH(0x51, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Copy register block
|
||||
pub fn brc(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x52, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Relative jump
|
||||
pub fn jmp(offset0: i32) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(O(0x53, offset0)) }
|
||||
}
|
||||
/// Linking relative jump
|
||||
pub fn jal(reg0: u8, reg1: u8, offset2: i32) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRO(0x54, reg0, reg1, offset2)) }
|
||||
}
|
||||
/// Linking absolute jump
|
||||
pub fn jala(reg0: u8, reg1: u8, addr2: u64) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRA(0x55, reg0, reg1, addr2)) }
|
||||
}
|
||||
/// Branch on equal
|
||||
pub fn jeq(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRP(0x56, reg0, reg1, offset2)) }
|
||||
}
|
||||
/// Branch on nonequal
|
||||
pub fn jne(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRP(0x57, reg0, reg1, offset2)) }
|
||||
}
|
||||
/// Branch on lesser-than (unsigned)
|
||||
pub fn jltu(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRP(0x58, reg0, reg1, offset2)) }
|
||||
}
|
||||
/// Branch on greater-than (unsigned)
|
||||
pub fn jgtu(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRP(0x59, reg0, reg1, offset2)) }
|
||||
}
|
||||
/// Branch on lesser-than (signed)
|
||||
pub fn jlts(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRP(0x5A, reg0, reg1, offset2)) }
|
||||
}
|
||||
/// Branch on greater-than (signed)
|
||||
pub fn jgts(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRP(0x5B, reg0, reg1, offset2)) }
|
||||
}
|
||||
/// Environment call trap
|
||||
pub fn eca() -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(N(0x5C, )) }
|
||||
}
|
||||
/// Environment breakpoint
|
||||
pub fn ebp() -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(N(0x5D, )) }
|
||||
}
|
||||
/// Floating point addition (32b)
|
||||
pub fn fadd32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x5E, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Floating point addition (64b)
|
||||
pub fn fadd64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x5F, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Floating point subtraction (32b)
|
||||
pub fn fsub32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x60, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Floating point subtraction (64b)
|
||||
pub fn fsub64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x61, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Floating point multiply (32b)
|
||||
pub fn fmul32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x62, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Floating point multiply (64b)
|
||||
pub fn fmul64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x63, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Floating point division (32b)
|
||||
pub fn fdiv32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x64, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Floating point division (64b)
|
||||
pub fn fdiv64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x65, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Float fused multiply-add (32b)
|
||||
pub fn fma32(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRRR(0x66, reg0, reg1, reg2, reg3)) }
|
||||
}
|
||||
/// Float fused multiply-add (64b)
|
||||
pub fn fma64(reg0: u8, reg1: u8, reg2: u8, reg3: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRRR(0x67, reg0, reg1, reg2, reg3)) }
|
||||
}
|
||||
/// Float reciprocal (32b)
|
||||
pub fn finv32(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RR(0x68, reg0, reg1)) }
|
||||
}
|
||||
/// Float reciprocal (64b)
|
||||
pub fn finv64(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RR(0x69, reg0, reg1)) }
|
||||
}
|
||||
/// Flaot compare less than (32b)
|
||||
pub fn fcmplt32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x6A, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Flaot compare less than (64b)
|
||||
pub fn fcmplt64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x6B, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Flaot compare greater than (32b)
|
||||
pub fn fcmpgt32(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x6C, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Flaot compare greater than (64b)
|
||||
pub fn fcmpgt64(reg0: u8, reg1: u8, reg2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRR(0x6D, reg0, reg1, reg2)) }
|
||||
}
|
||||
/// Int to 32 bit float
|
||||
pub fn itf32(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RR(0x6E, reg0, reg1)) }
|
||||
}
|
||||
/// Int to 64 bit float
|
||||
pub fn itf64(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RR(0x6F, reg0, reg1)) }
|
||||
}
|
||||
/// Float 32 to int
|
||||
pub fn fti32(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x70, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Float 64 to int
|
||||
pub fn fti64(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x71, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Float 64 to Float 32
|
||||
pub fn fc32t64(reg0: u8, reg1: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RR(0x72, reg0, reg1)) }
|
||||
}
|
||||
/// Float 32 to Float 64
|
||||
pub fn fc64t32(reg0: u8, reg1: u8, imm2: u8) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRB(0x73, reg0, reg1, imm2)) }
|
||||
}
|
||||
/// Load relative immediate (16 bit)
|
||||
pub fn lra16(reg0: u8, reg1: u8, offset2: i16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRP(0x74, reg0, reg1, offset2)) }
|
||||
}
|
||||
/// Load from relative address (16 bit)
|
||||
pub fn ldr16(reg0: u8, reg1: u8, offset2: i16, imm3: u16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRPH(0x75, reg0, reg1, offset2, imm3)) }
|
||||
}
|
||||
/// Store to relative address (16 bit)
|
||||
pub fn str16(reg0: u8, reg1: u8, offset2: i16, imm3: u16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(RRPH(0x76, reg0, reg1, offset2, imm3)) }
|
||||
}
|
||||
/// Relative jump (16 bit)
|
||||
pub fn jmp16(offset0: i16) -> (usize, [u8; MAX_SIZE]) {
|
||||
unsafe { crate::encode(P(0x77, offset0)) }
|
||||
}
|
||||
#[repr(packed)] pub struct N(u8, );
|
||||
#[repr(packed)] pub struct RRR(u8, u8, u8, u8);
|
||||
#[repr(packed)] pub struct RRRR(u8, u8, u8, u8, u8);
|
||||
#[repr(packed)] pub struct RR(u8, u8, u8);
|
||||
#[repr(packed)] pub struct RRB(u8, u8, u8, u8);
|
||||
#[repr(packed)] pub struct RRH(u8, u8, u8, u16);
|
||||
#[repr(packed)] pub struct RRW(u8, u8, u8, u32);
|
||||
#[repr(packed)] pub struct RRD(u8, u8, u8, u64);
|
||||
#[repr(packed)] pub struct RB(u8, u8, u8);
|
||||
#[repr(packed)] pub struct RH(u8, u8, u16);
|
||||
#[repr(packed)] pub struct RW(u8, u8, u32);
|
||||
#[repr(packed)] pub struct RD(u8, u8, u64);
|
||||
#[repr(packed)] pub struct RRO(u8, u8, u8, i32);
|
||||
#[repr(packed)] pub struct RRAH(u8, u8, u8, u64, u16);
|
||||
#[repr(packed)] pub struct RROH(u8, u8, u8, i32, u16);
|
||||
#[repr(packed)] pub struct O(u8, i32);
|
||||
#[repr(packed)] pub struct RRA(u8, u8, u8, u64);
|
||||
#[repr(packed)] pub struct RRP(u8, u8, u8, i16);
|
||||
#[repr(packed)] pub struct RRPH(u8, u8, u8, i16, u16);
|
||||
#[repr(packed)] pub struct P(u8, i16);
|
||||
pub const NAMES: [&str; 120] = [
|
||||
"un",
|
||||
"tx",
|
||||
"nop",
|
||||
"add8",
|
||||
"add16",
|
||||
"add32",
|
||||
"add64",
|
||||
"sub8",
|
||||
"sub16",
|
||||
"sub32",
|
||||
"sub64",
|
||||
"mul8",
|
||||
"mul16",
|
||||
"mul32",
|
||||
"mul64",
|
||||
"and",
|
||||
"or",
|
||||
"xor",
|
||||
"slu8",
|
||||
"slu16",
|
||||
"slu32",
|
||||
"slu64",
|
||||
"sru8",
|
||||
"sru16",
|
||||
"sru32",
|
||||
"sru64",
|
||||
"srs8",
|
||||
"srs16",
|
||||
"srs32",
|
||||
"srs64",
|
||||
"cmpu",
|
||||
"cmps",
|
||||
"diru8",
|
||||
"diru16",
|
||||
"diru32",
|
||||
"diru64",
|
||||
"dirs8",
|
||||
"dirs16",
|
||||
"dirs32",
|
||||
"dirs64",
|
||||
"neg",
|
||||
"not",
|
||||
"sxt8",
|
||||
"sxt16",
|
||||
"sxt32",
|
||||
"addi8",
|
||||
"addi16",
|
||||
"addi32",
|
||||
"addi64",
|
||||
"muli8",
|
||||
"muli16",
|
||||
"muli32",
|
||||
"muli64",
|
||||
"andi",
|
||||
"ori",
|
||||
"xori",
|
||||
"slui8",
|
||||
"slui16",
|
||||
"slui32",
|
||||
"slui64",
|
||||
"srui8",
|
||||
"srui16",
|
||||
"srui32",
|
||||
"srui64",
|
||||
"srsi8",
|
||||
"srsi16",
|
||||
"srsi32",
|
||||
"srsi64",
|
||||
"cmpui",
|
||||
"cmpsi",
|
||||
"cp",
|
||||
"swa",
|
||||
"li8",
|
||||
"li16",
|
||||
"li32",
|
||||
"li64",
|
||||
"lra",
|
||||
"ld",
|
||||
"st",
|
||||
"ldr",
|
||||
"str",
|
||||
"bmc",
|
||||
"brc",
|
||||
"jmp",
|
||||
"jal",
|
||||
"jala",
|
||||
"jeq",
|
||||
"jne",
|
||||
"jltu",
|
||||
"jgtu",
|
||||
"jlts",
|
||||
"jgts",
|
||||
"eca",
|
||||
"ebp",
|
||||
"fadd32",
|
||||
"fadd64",
|
||||
"fsub32",
|
||||
"fsub64",
|
||||
"fmul32",
|
||||
"fmul64",
|
||||
"fdiv32",
|
||||
"fdiv64",
|
||||
"fma32",
|
||||
"fma64",
|
||||
"finv32",
|
||||
"finv64",
|
||||
"fcmplt32",
|
||||
"fcmplt64",
|
||||
"fcmpgt32",
|
||||
"fcmpgt64",
|
||||
"itf32",
|
||||
"itf64",
|
||||
"fti32",
|
||||
"fti64",
|
||||
"fc32t64",
|
||||
"fc64t32",
|
||||
"lra16",
|
||||
"ldr16",
|
||||
"str16",
|
||||
"jmp16",
|
||||
];
|
|
@ -1,279 +0,0 @@
|
|||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub struct Token {
|
||||
pub kind: TokenKind,
|
||||
pub start: u32,
|
||||
pub end: u32,
|
||||
}
|
||||
|
||||
impl Token {
|
||||
pub fn range(&self) -> std::ops::Range<usize> {
|
||||
self.start as usize..self.end as usize
|
||||
}
|
||||
|
||||
pub fn len(&self) -> u32 {
|
||||
self.end - self.start
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! gen_token_kind {
|
||||
($(
|
||||
#[$atts:meta])*
|
||||
$vis:vis enum $name:ident {
|
||||
#[patterns] $(
|
||||
$pattern:ident,
|
||||
)*
|
||||
#[keywords] $(
|
||||
$keyword:ident = $keyword_lit:literal,
|
||||
)*
|
||||
#[punkt] $(
|
||||
$punkt:ident = $punkt_lit:literal,
|
||||
)*
|
||||
#[ops] $(
|
||||
#[prec = $prec:literal] $(
|
||||
$op:ident = $op_lit:literal,
|
||||
)*
|
||||
)*
|
||||
}
|
||||
) => {
|
||||
impl std::fmt::Display for $name {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let s = match *self {
|
||||
$( Self::$pattern => concat!('<', stringify!($pattern), '>'), )*
|
||||
|
||||
$( Self::$keyword => stringify!($keyword_lit), )*
|
||||
$( Self::$punkt => stringify!($punkt_lit), )*
|
||||
$($( Self::$op => $op_lit, )*)*
|
||||
};
|
||||
f.write_str(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl $name {
|
||||
#[inline(always)]
|
||||
pub fn precedence(&self) -> Option<u8> {
|
||||
Some(match self {
|
||||
$($(Self::$op)|* => $prec,)*
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn from_ident(ident: &[u8]) -> Self {
|
||||
match ident {
|
||||
$($keyword_lit => Self::$keyword,)*
|
||||
_ => Self::Ident,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
$vis enum $name {
|
||||
$( $pattern, )*
|
||||
$( $keyword, )*
|
||||
$( $punkt, )*
|
||||
$($( $op, )*)*
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
gen_token_kind! {
|
||||
pub enum TokenKind {
|
||||
#[patterns]
|
||||
Ident,
|
||||
Number,
|
||||
Eof,
|
||||
Error,
|
||||
Driective,
|
||||
#[keywords]
|
||||
Return = b"return",
|
||||
If = b"if",
|
||||
Else = b"else",
|
||||
Loop = b"loop",
|
||||
Break = b"break",
|
||||
Continue = b"continue",
|
||||
Fn = b"fn",
|
||||
Struct = b"struct",
|
||||
True = b"true",
|
||||
#[punkt]
|
||||
LParen = "(",
|
||||
RParen = ")",
|
||||
LBrace = "{",
|
||||
RBrace = "}",
|
||||
Semi = ";",
|
||||
Colon = ":",
|
||||
Comma = ",",
|
||||
Dot = ".",
|
||||
Ctor = ".{",
|
||||
Tupl = ".(",
|
||||
#[ops]
|
||||
#[prec = 1]
|
||||
Decl = ":=",
|
||||
Assign = "=",
|
||||
#[prec = 21]
|
||||
Le = "<=",
|
||||
Ge = ">=",
|
||||
Lt = "<",
|
||||
Gt = ">",
|
||||
Eq = "==",
|
||||
Ne = "!=",
|
||||
#[prec = 22]
|
||||
Amp = "&",
|
||||
#[prec = 23]
|
||||
Plus = "+",
|
||||
Minus = "-",
|
||||
#[prec = 24]
|
||||
Star = "*",
|
||||
FSlash = "/",
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Lexer<'a> {
|
||||
pos: u32,
|
||||
bytes: &'a [u8],
|
||||
}
|
||||
|
||||
impl<'a> Lexer<'a> {
|
||||
pub fn new(input: &'a str) -> Self {
|
||||
Self {
|
||||
pos: 0,
|
||||
bytes: input.as_bytes(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn slice(&self, tok: std::ops::Range<usize>) -> &'a str {
|
||||
unsafe { std::str::from_utf8_unchecked(&self.bytes[tok]) }
|
||||
}
|
||||
|
||||
fn peek(&self) -> Option<u8> {
|
||||
self.bytes.get(self.pos as usize).copied()
|
||||
}
|
||||
|
||||
fn advance(&mut self) -> Option<u8> {
|
||||
let c = self.peek()?;
|
||||
self.pos += 1;
|
||||
Some(c)
|
||||
}
|
||||
|
||||
pub fn next(&mut self) -> Token {
|
||||
Iterator::next(self).unwrap_or(Token {
|
||||
kind: TokenKind::Eof,
|
||||
start: self.pos,
|
||||
end: self.pos,
|
||||
})
|
||||
}
|
||||
|
||||
fn advance_if(&mut self, arg: u8) -> bool {
|
||||
if self.peek() == Some(arg) {
|
||||
self.advance();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn line_col(&self, pos: u32) -> (usize, usize) {
|
||||
line_col(self.bytes, pos)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn line_col(bytes: &[u8], mut start: u32) -> (usize, usize) {
|
||||
bytes
|
||||
.split(|&b| b == b'\n')
|
||||
.enumerate()
|
||||
.find_map(|(i, line)| {
|
||||
if start < line.len() as u32 {
|
||||
return Some((i + 1, start as usize + 1));
|
||||
}
|
||||
start -= line.len() as u32 + 1;
|
||||
None
|
||||
})
|
||||
.unwrap_or((1, 1))
|
||||
}
|
||||
|
||||
impl<'a> Iterator for Lexer<'a> {
|
||||
type Item = Token;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
use TokenKind as T;
|
||||
loop {
|
||||
let mut start = self.pos;
|
||||
let kind = match self.advance()? {
|
||||
b'\n' | b'\r' | b'\t' | b' ' => continue,
|
||||
b'0'..=b'9' => {
|
||||
while let Some(b'0'..=b'9') = self.peek() {
|
||||
self.advance();
|
||||
}
|
||||
T::Number
|
||||
}
|
||||
c @ (b'a'..=b'z' | b'A'..=b'Z' | b'_' | b'@') => {
|
||||
while let Some(b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_') = self.peek() {
|
||||
self.advance();
|
||||
}
|
||||
|
||||
if c == b'@' {
|
||||
start += 1;
|
||||
T::Driective
|
||||
} else {
|
||||
let ident = &self.bytes[start as usize..self.pos as usize];
|
||||
T::from_ident(ident)
|
||||
}
|
||||
}
|
||||
b':' if self.advance_if(b'=') => T::Decl,
|
||||
b':' => T::Colon,
|
||||
b',' => T::Comma,
|
||||
b'.' if self.advance_if(b'{') => T::Ctor,
|
||||
b'.' if self.advance_if(b'(') => T::Tupl,
|
||||
b'.' => T::Dot,
|
||||
b';' => T::Semi,
|
||||
b'!' if self.advance_if(b'=') => T::Ne,
|
||||
b'=' if self.advance_if(b'=') => T::Eq,
|
||||
b'=' => T::Assign,
|
||||
b'<' if self.advance_if(b'=') => T::Le,
|
||||
b'<' => T::Lt,
|
||||
b'>' if self.advance_if(b'=') => T::Ge,
|
||||
b'>' => T::Gt,
|
||||
b'+' => T::Plus,
|
||||
b'-' => T::Minus,
|
||||
b'*' => T::Star,
|
||||
b'/' => T::FSlash,
|
||||
b'&' => T::Amp,
|
||||
b'(' => T::LParen,
|
||||
b')' => T::RParen,
|
||||
b'{' => T::LBrace,
|
||||
b'}' => T::RBrace,
|
||||
_ => T::Error,
|
||||
};
|
||||
|
||||
return Some(Token {
|
||||
kind,
|
||||
start,
|
||||
end: self.pos,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
fn lex(input: &'static str, output: &mut String) {
|
||||
use {
|
||||
super::{Lexer, TokenKind as T},
|
||||
std::fmt::Write,
|
||||
};
|
||||
let mut lexer = Lexer::new(input);
|
||||
loop {
|
||||
let token = lexer.next();
|
||||
writeln!(output, "{:?} {:?}", token.kind, &input[token.range()],).unwrap();
|
||||
if token.kind == T::Eof {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
crate::run_tests! { lex:
|
||||
empty => "";
|
||||
whitespace => " \t\n\r";
|
||||
example => include_str!("../examples/main_fn.hb");
|
||||
arithmetic => include_str!("../examples/arithmetic.hb");
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
#![feature(noop_waker)]
|
||||
#![feature(macro_metavar_expr)]
|
||||
#![feature(let_chains)]
|
||||
#![feature(non_null_convenience)]
|
||||
#![allow(dead_code)]
|
||||
#![feature(const_mut_refs)]
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! run_tests {
|
||||
($runner:path: $($name:ident => $input:expr;)*) => {$(
|
||||
#[test]
|
||||
fn $name() {
|
||||
$crate::tests::run_test(std::any::type_name_of_val(&$name), $input, $runner);
|
||||
}
|
||||
)*};
|
||||
}
|
||||
|
||||
pub mod codegen;
|
||||
mod ident;
|
||||
mod instrs;
|
||||
mod lexer;
|
||||
mod log;
|
||||
pub mod parser;
|
||||
mod tests;
|
||||
mod typechk;
|
||||
|
||||
#[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)
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
#![allow(unused_macros)]
|
||||
|
||||
#[derive(PartialOrd, PartialEq, Ord, Eq, Debug)]
|
||||
pub enum Level {
|
||||
Err,
|
||||
Wrn,
|
||||
Inf,
|
||||
Dbg,
|
||||
}
|
||||
|
||||
pub const LOG_LEVEL: Level = match option_env!("LOG_LEVEL") {
|
||||
Some(val) => match val.as_bytes()[0] {
|
||||
b'e' => Level::Err,
|
||||
b'w' => Level::Wrn,
|
||||
b'i' => Level::Inf,
|
||||
b'd' => Level::Dbg,
|
||||
_ => panic!("Invalid log level."),
|
||||
},
|
||||
None => {
|
||||
if cfg!(debug_assertions) {
|
||||
Level::Dbg
|
||||
} else {
|
||||
Level::Err
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
macro_rules! log {
|
||||
($level:expr, $fmt:literal $($expr:tt)*) => {
|
||||
if $level <= $crate::log::LOG_LEVEL {
|
||||
println!("{:?}: {}", $level, format_args!($fmt $($expr)*));
|
||||
}
|
||||
};
|
||||
|
||||
($level:expr, $($arg:expr),*) => {
|
||||
if $level <= $crate::log::LOG_LEVEL {
|
||||
$(println!("[{}{}{}][{:?}]: {} = {:?}", line!(), column!(), file!(), $level, stringify!($arg), $arg);)*
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! err { ($($arg:tt)*) => { $crate::log::log!($crate::log::Level::Err, $($arg)*) }; }
|
||||
macro_rules! wrn { ($($arg:tt)*) => { $crate::log::log!($crate::log::Level::Wrn, $($arg)*) }; }
|
||||
macro_rules! inf { ($($arg:tt)*) => { $crate::log::log!($crate::log::Level::Inf, $($arg)*) }; }
|
||||
macro_rules! dbg { ($($arg:tt)*) => { $crate::log::log!($crate::log::Level::Dbg, $($arg)*) }; }
|
||||
|
||||
#[allow(unused_imports)]
|
||||
pub(crate) use {dbg, err, inf, log, wrn};
|
|
@ -1,25 +0,0 @@
|
|||
use std::io;
|
||||
|
||||
use hblang::{codegen, parser};
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
if std::env::args().len() == 1 {
|
||||
eprintln!("Usage: hblang <file1> <file2> ...");
|
||||
eprintln!(" 1. compiled binary will be printed to stdout");
|
||||
eprintln!(" 2. order of files matters");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
let files = std::env::args()
|
||||
.skip(1)
|
||||
.map(|path| std::fs::read_to_string(&path).map(|src| (path, src)))
|
||||
.collect::<io::Result<Vec<_>>>()?;
|
||||
let arena = parser::Arena::default();
|
||||
let mut parser = parser::Parser::new(&arena);
|
||||
let mut codegen = codegen::Codegen::default();
|
||||
for (path, content) in files.iter() {
|
||||
let file = parser.file(&path, content.as_str());
|
||||
codegen.file(path, content.as_bytes(), file);
|
||||
}
|
||||
codegen.dump(&mut std::io::stdout())
|
||||
}
|
|
@ -1,774 +0,0 @@
|
|||
use std::{cell::Cell, ops::Not, ptr::NonNull};
|
||||
|
||||
use crate::{
|
||||
codegen::bt,
|
||||
ident::{self, Ident},
|
||||
lexer::{Lexer, Token, TokenKind},
|
||||
};
|
||||
|
||||
pub type Pos = u32;
|
||||
|
||||
struct ScopeIdent<'a> {
|
||||
ident: Ident,
|
||||
declared: bool,
|
||||
last: &'a Cell<bool>,
|
||||
}
|
||||
|
||||
pub struct Parser<'a, 'b> {
|
||||
path: &'a str,
|
||||
lexer: Lexer<'a>,
|
||||
arena: &'b Arena<'a>,
|
||||
token: Token,
|
||||
idents: Vec<ScopeIdent<'a>>,
|
||||
}
|
||||
|
||||
impl<'a, 'b> Parser<'a, 'b> {
|
||||
pub fn new(arena: &'b Arena<'a>) -> Self {
|
||||
let mut lexer = Lexer::new("");
|
||||
let token = lexer.next();
|
||||
Self {
|
||||
lexer,
|
||||
token,
|
||||
path: "",
|
||||
arena,
|
||||
idents: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file(&mut self, input: &'a str, path: &'a str) -> &'a [Expr<'a>] {
|
||||
self.path = path;
|
||||
self.lexer = Lexer::new(input);
|
||||
self.token = self.lexer.next();
|
||||
|
||||
let f = self.collect(|s| (s.token.kind != TokenKind::Eof).then(|| s.expr()));
|
||||
self.pop_scope(0);
|
||||
let has_undeclared = !self.idents.is_empty();
|
||||
for id in self.idents.drain(..) {
|
||||
let (line, col) = self.lexer.line_col(ident::pos(id.ident));
|
||||
eprintln!(
|
||||
"{}:{}:{} => undeclared identifier: {}",
|
||||
self.path,
|
||||
line,
|
||||
col,
|
||||
self.lexer.slice(ident::range(id.ident))
|
||||
);
|
||||
}
|
||||
|
||||
if has_undeclared {
|
||||
unreachable!();
|
||||
}
|
||||
|
||||
f
|
||||
}
|
||||
|
||||
fn next(&mut self) -> Token {
|
||||
std::mem::replace(&mut self.token, self.lexer.next())
|
||||
}
|
||||
|
||||
fn ptr_expr(&mut self) -> &'a Expr<'a> {
|
||||
self.arena.alloc(self.expr())
|
||||
}
|
||||
|
||||
fn expr(&mut self) -> Expr<'a> {
|
||||
let left = self.unit_expr();
|
||||
self.bin_expr(left, 0)
|
||||
}
|
||||
|
||||
fn bin_expr(&mut self, mut left: Expr<'a>, min_prec: u8) -> Expr<'a> {
|
||||
loop {
|
||||
let Some(prec) = self.token.kind.precedence() else {
|
||||
break;
|
||||
};
|
||||
|
||||
if prec <= min_prec {
|
||||
break;
|
||||
}
|
||||
|
||||
let op = self.next().kind;
|
||||
let right = self.unit_expr();
|
||||
let right = self.bin_expr(right, prec);
|
||||
left = Expr::BinOp {
|
||||
left: self.arena.alloc(left),
|
||||
right: self.arena.alloc(right),
|
||||
op,
|
||||
};
|
||||
}
|
||||
|
||||
left
|
||||
}
|
||||
|
||||
fn try_resolve_builtin(name: &str) -> Option<Ident> {
|
||||
// FIXME: we actually do this the second time in the codegen
|
||||
Some(match name {
|
||||
"int" | "i64" => bt::INT,
|
||||
"i8" => bt::I8,
|
||||
"i16" => bt::I16,
|
||||
"i32" => bt::I32,
|
||||
"u8" => bt::U8,
|
||||
"u16" => bt::U16,
|
||||
"uint" | "u32" => bt::U32,
|
||||
"bool" => bt::BOOL,
|
||||
"void" => bt::VOID,
|
||||
"never" => bt::NEVER,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
fn resolve_ident(&mut self, token: Token, decl: bool) -> (Ident, Option<&'a Cell<bool>>) {
|
||||
let name = self.lexer.slice(token.range());
|
||||
|
||||
if let Some(builtin) = Self::try_resolve_builtin(name) {
|
||||
return (builtin, None);
|
||||
}
|
||||
|
||||
let last = self.arena.alloc(Cell::new(false));
|
||||
let id = match self
|
||||
.idents
|
||||
.iter_mut()
|
||||
.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(elem) => elem,
|
||||
None => {
|
||||
let id = ident::new(token.start, name.len() as _);
|
||||
self.idents.push(ScopeIdent {
|
||||
ident: id,
|
||||
declared: false,
|
||||
last,
|
||||
});
|
||||
self.idents.last_mut().unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
id.last = last;
|
||||
id.declared |= decl;
|
||||
|
||||
(id.ident, Some(last))
|
||||
}
|
||||
|
||||
fn unit_expr(&mut self) -> Expr<'a> {
|
||||
use {Expr as E, TokenKind as T};
|
||||
let frame = self.idents.len();
|
||||
let token = self.next();
|
||||
let mut expr = match token.kind {
|
||||
T::Driective => E::Directive {
|
||||
pos: token.start,
|
||||
name: self.lexer.slice(token.range()),
|
||||
args: {
|
||||
self.expect_advance(T::LParen);
|
||||
self.collect_list(T::Comma, T::RParen, Self::expr)
|
||||
},
|
||||
},
|
||||
T::True => E::Bool {
|
||||
pos: token.start,
|
||||
value: true,
|
||||
},
|
||||
T::Struct => E::Struct {
|
||||
pos: token.start,
|
||||
fields: {
|
||||
self.expect_advance(T::LBrace);
|
||||
self.collect_list(T::Comma, T::RBrace, |s| {
|
||||
let name = s.expect_advance(T::Ident);
|
||||
s.expect_advance(T::Colon);
|
||||
let ty = s.expr();
|
||||
(s.lexer.slice(name.range()), ty)
|
||||
})
|
||||
},
|
||||
},
|
||||
T::Ident => {
|
||||
let (id, last) = self.resolve_ident(token, self.token.kind == T::Decl);
|
||||
let name = self.lexer.slice(token.range());
|
||||
E::Ident { name, id, last }
|
||||
}
|
||||
T::If => E::If {
|
||||
pos: token.start,
|
||||
cond: self.ptr_expr(),
|
||||
then: self.ptr_expr(),
|
||||
else_: self.advance_if(T::Else).then(|| self.ptr_expr()),
|
||||
},
|
||||
T::Loop => E::Loop {
|
||||
pos: token.start,
|
||||
body: self.ptr_expr(),
|
||||
},
|
||||
T::Break => E::Break { pos: token.start },
|
||||
T::Continue => E::Continue { pos: token.start },
|
||||
T::Return => E::Return {
|
||||
pos: token.start,
|
||||
val: (self.token.kind != T::Semi).then(|| self.ptr_expr()),
|
||||
},
|
||||
T::Fn => E::Closure {
|
||||
pos: token.start,
|
||||
args: {
|
||||
self.expect_advance(T::LParen);
|
||||
self.collect_list(T::Comma, T::RParen, |s| {
|
||||
let name = s.expect_advance(T::Ident);
|
||||
let (id, last) = s.resolve_ident(name, true);
|
||||
s.expect_advance(T::Colon);
|
||||
Arg {
|
||||
name: s.lexer.slice(name.range()),
|
||||
id,
|
||||
last,
|
||||
ty: s.expr(),
|
||||
}
|
||||
})
|
||||
},
|
||||
ret: {
|
||||
self.expect_advance(T::Colon);
|
||||
self.ptr_expr()
|
||||
},
|
||||
body: self.ptr_expr(),
|
||||
},
|
||||
T::Amp | T::Star => E::UnOp {
|
||||
pos: token.start,
|
||||
op: token.kind,
|
||||
val: self.ptr_unit_expr(),
|
||||
},
|
||||
T::LBrace => E::Block {
|
||||
pos: token.start,
|
||||
stmts: self.collect_list(T::Semi, T::RBrace, Self::expr),
|
||||
},
|
||||
T::Number => E::Number {
|
||||
pos: token.start,
|
||||
value: match self.lexer.slice(token.range()).parse() {
|
||||
Ok(value) => value,
|
||||
Err(e) => self.report(format_args!("invalid number: {e}")),
|
||||
},
|
||||
},
|
||||
T::LParen => {
|
||||
let expr = self.expr();
|
||||
self.expect_advance(T::RParen);
|
||||
expr
|
||||
}
|
||||
tok => self.report(format_args!("unexpected token: {tok:?}")),
|
||||
};
|
||||
|
||||
loop {
|
||||
let token = self.token;
|
||||
if matches!(token.kind, T::LParen | T::Ctor | T::Dot | T::Tupl) {
|
||||
self.next();
|
||||
}
|
||||
|
||||
expr = match token.kind {
|
||||
T::LParen => Expr::Call {
|
||||
func: self.arena.alloc(expr),
|
||||
args: self.collect_list(T::Comma, T::RParen, Self::expr),
|
||||
},
|
||||
T::Ctor => E::Ctor {
|
||||
pos: token.start,
|
||||
ty: Some(self.arena.alloc(expr)),
|
||||
fields: self.collect_list(T::Comma, T::RBrace, |s| {
|
||||
let name = s.expect_advance(T::Ident);
|
||||
s.expect_advance(T::Colon);
|
||||
let val = s.expr();
|
||||
(Some(s.lexer.slice(name.range())), val)
|
||||
}),
|
||||
},
|
||||
T::Tupl => E::Ctor {
|
||||
pos: token.start,
|
||||
ty: Some(self.arena.alloc(expr)),
|
||||
fields: self.collect_list(T::Comma, T::RParen, |s| (None, s.expr())),
|
||||
},
|
||||
T::Dot => E::Field {
|
||||
target: self.arena.alloc(expr),
|
||||
field: {
|
||||
let token = self.expect_advance(T::Ident);
|
||||
self.lexer.slice(token.range())
|
||||
},
|
||||
},
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
|
||||
if matches!(token.kind, T::Return) {
|
||||
self.expect_advance(T::Semi);
|
||||
}
|
||||
|
||||
if matches!(token.kind, T::Loop | T::LBrace | T::Fn) {
|
||||
self.pop_scope(frame);
|
||||
}
|
||||
|
||||
expr
|
||||
}
|
||||
|
||||
fn pop_scope(&mut self, frame: usize) {
|
||||
let mut undeclared_count = frame;
|
||||
for i in frame..self.idents.len() {
|
||||
if !self.idents[i].declared {
|
||||
self.idents.swap(i, undeclared_count);
|
||||
undeclared_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
for id in self.idents.drain(undeclared_count..) {
|
||||
id.last.set(true);
|
||||
}
|
||||
}
|
||||
|
||||
fn ptr_unit_expr(&mut self) -> &'a Expr<'a> {
|
||||
self.arena.alloc(self.unit_expr())
|
||||
}
|
||||
|
||||
fn collect_list<T: Copy>(
|
||||
&mut self,
|
||||
delim: TokenKind,
|
||||
end: TokenKind,
|
||||
mut f: impl FnMut(&mut Self) -> T,
|
||||
) -> &'a [T] {
|
||||
self.collect(|s| {
|
||||
s.advance_if(end).not().then(|| {
|
||||
let val = f(s);
|
||||
s.advance_if(delim);
|
||||
val
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn collect<T: Copy>(&mut self, mut f: impl FnMut(&mut Self) -> Option<T>) -> &'a [T] {
|
||||
let vec = std::iter::from_fn(|| f(self)).collect::<Vec<_>>();
|
||||
self.arena.alloc_slice(&vec)
|
||||
}
|
||||
|
||||
fn advance_if(&mut self, kind: TokenKind) -> bool {
|
||||
if self.token.kind == kind {
|
||||
self.next();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_advance(&mut self, kind: TokenKind) -> Token {
|
||||
if self.token.kind != kind {
|
||||
self.report(format_args!(
|
||||
"expected {:?}, found {:?}",
|
||||
kind, self.token.kind
|
||||
));
|
||||
}
|
||||
self.next()
|
||||
}
|
||||
|
||||
fn report(&self, msg: impl std::fmt::Display) -> ! {
|
||||
let (line, col) = self.lexer.line_col(self.token.start);
|
||||
eprintln!("{}:{}:{} => {}", self.path, line, col, msg);
|
||||
unreachable!();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Arg<'a> {
|
||||
pub name: &'a str,
|
||||
pub id: Ident,
|
||||
pub last: Option<&'a Cell<bool>>,
|
||||
pub ty: Expr<'a>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Expr<'a> {
|
||||
Break {
|
||||
pos: Pos,
|
||||
},
|
||||
Continue {
|
||||
pos: Pos,
|
||||
},
|
||||
Closure {
|
||||
pos: Pos,
|
||||
args: &'a [Arg<'a>],
|
||||
ret: &'a Self,
|
||||
body: &'a Self,
|
||||
},
|
||||
Call {
|
||||
func: &'a Self,
|
||||
args: &'a [Self],
|
||||
},
|
||||
Return {
|
||||
pos: Pos,
|
||||
val: Option<&'a Self>,
|
||||
},
|
||||
Ident {
|
||||
name: &'a str,
|
||||
id: Ident,
|
||||
last: Option<&'a Cell<bool>>,
|
||||
},
|
||||
Block {
|
||||
pos: Pos,
|
||||
stmts: &'a [Self],
|
||||
},
|
||||
Number {
|
||||
pos: Pos,
|
||||
value: u64,
|
||||
},
|
||||
BinOp {
|
||||
left: &'a Self,
|
||||
op: TokenKind,
|
||||
right: &'a Self,
|
||||
},
|
||||
If {
|
||||
pos: Pos,
|
||||
cond: &'a Self,
|
||||
then: &'a Self,
|
||||
else_: Option<&'a Self>,
|
||||
},
|
||||
Loop {
|
||||
pos: Pos,
|
||||
body: &'a Self,
|
||||
},
|
||||
UnOp {
|
||||
pos: Pos,
|
||||
op: TokenKind,
|
||||
val: &'a Self,
|
||||
},
|
||||
Struct {
|
||||
pos: Pos,
|
||||
fields: &'a [(&'a str, Self)],
|
||||
},
|
||||
Ctor {
|
||||
pos: Pos,
|
||||
ty: Option<&'a Self>,
|
||||
fields: &'a [(Option<&'a str>, Self)],
|
||||
},
|
||||
Field {
|
||||
target: &'a Self,
|
||||
field: &'a str,
|
||||
},
|
||||
Bool {
|
||||
pos: Pos,
|
||||
value: bool,
|
||||
},
|
||||
Directive {
|
||||
pos: u32,
|
||||
name: &'a str,
|
||||
args: &'a [Self],
|
||||
},
|
||||
}
|
||||
|
||||
impl<'a> Expr<'a> {
|
||||
pub fn pos(&self) -> Pos {
|
||||
match self {
|
||||
Self::Call { func, .. } => func.pos(),
|
||||
Self::Ident { id, .. } => ident::pos(*id),
|
||||
Self::Break { pos }
|
||||
| Self::Directive { pos, .. }
|
||||
| Self::Continue { pos }
|
||||
| Self::Closure { pos, .. }
|
||||
| Self::Block { pos, .. }
|
||||
| Self::Number { pos, .. }
|
||||
| Self::Return { pos, .. }
|
||||
| Self::If { pos, .. }
|
||||
| Self::Loop { pos, .. }
|
||||
| Self::UnOp { pos, .. }
|
||||
| Self::Struct { pos, .. }
|
||||
| Self::Ctor { pos, .. }
|
||||
| Self::Bool { pos, .. } => *pos,
|
||||
Self::BinOp { left, .. } => left.pos(),
|
||||
Self::Field { target, .. } => target.pos(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> std::fmt::Display for Expr<'a> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
thread_local! {
|
||||
static INDENT: Cell<usize> = Cell::new(0);
|
||||
}
|
||||
|
||||
fn fmt_list<'a, T>(
|
||||
f: &mut std::fmt::Formatter,
|
||||
end: &str,
|
||||
list: &'a [T],
|
||||
fmt: impl Fn(&T, &mut std::fmt::Formatter) -> std::fmt::Result,
|
||||
) -> std::fmt::Result {
|
||||
let first = &mut true;
|
||||
for expr in list {
|
||||
if !std::mem::take(first) {
|
||||
write!(f, ", ")?;
|
||||
}
|
||||
fmt(expr, f)?;
|
||||
}
|
||||
write!(f, "{end}")
|
||||
}
|
||||
|
||||
match *self {
|
||||
Self::Field { target, field } => write!(f, "{target}.{field}"),
|
||||
Self::Directive { name, args, .. } => {
|
||||
write!(f, "@{name}(")?;
|
||||
fmt_list(f, ")", args, std::fmt::Display::fmt)
|
||||
}
|
||||
Self::Struct { fields, .. } => {
|
||||
write!(f, "struct {{")?;
|
||||
fmt_list(f, "}", fields, |(name, val), f| write!(f, "{name}: {val}",))
|
||||
}
|
||||
Self::Ctor { ty, fields, .. } => {
|
||||
let (left, rith) = if fields.iter().any(|(name, _)| name.is_some()) {
|
||||
('{', '}')
|
||||
} else {
|
||||
('(', ')')
|
||||
};
|
||||
|
||||
if let Some(ty) = ty {
|
||||
write!(f, "{ty}")?;
|
||||
}
|
||||
write!(f, ".{left}")?;
|
||||
let first = &mut true;
|
||||
for (name, val) in fields {
|
||||
if !std::mem::take(first) {
|
||||
write!(f, ", ")?;
|
||||
}
|
||||
if let Some(name) = name {
|
||||
write!(f, "{name}: ")?;
|
||||
}
|
||||
write!(f, "{val}")?;
|
||||
}
|
||||
write!(f, "{rith}")
|
||||
}
|
||||
Self::UnOp { op, val, .. } => write!(f, "{op}{val}"),
|
||||
Self::Break { .. } => write!(f, "break;"),
|
||||
Self::Continue { .. } => write!(f, "continue;"),
|
||||
Self::If {
|
||||
cond, then, else_, ..
|
||||
} => {
|
||||
write!(f, "if {cond} {then}")?;
|
||||
if let Some(else_) = else_ {
|
||||
write!(f, " else {else_}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Self::Loop { body, .. } => write!(f, "loop {body}"),
|
||||
Self::Closure {
|
||||
ret, body, args, ..
|
||||
} => {
|
||||
write!(f, "fn(")?;
|
||||
fmt_list(f, "", args, |arg, f| write!(f, "{}: {}", arg.name, arg.ty))?;
|
||||
write!(f, "): {ret} {body}")
|
||||
}
|
||||
Self::Call { func, args } => {
|
||||
write!(f, "{func}(")?;
|
||||
fmt_list(f, ")", args, std::fmt::Display::fmt)
|
||||
}
|
||||
Self::Return { val: Some(val), .. } => write!(f, "return {val};"),
|
||||
Self::Return { val: None, .. } => write!(f, "return;"),
|
||||
Self::Ident { name, .. } => write!(f, "{name}"),
|
||||
Self::Block { stmts, .. } => {
|
||||
writeln!(f, "{{")?;
|
||||
INDENT.with(|i| i.set(i.get() + 1));
|
||||
let res = (|| {
|
||||
for stmt in stmts {
|
||||
for _ in 0..INDENT.with(|i| i.get()) {
|
||||
write!(f, " ")?;
|
||||
}
|
||||
writeln!(f, "{stmt}")?;
|
||||
}
|
||||
Ok(())
|
||||
})();
|
||||
INDENT.with(|i| i.set(i.get() - 1));
|
||||
write!(f, "}}")?;
|
||||
res
|
||||
}
|
||||
Self::Number { value, .. } => write!(f, "{value}"),
|
||||
Self::Bool { value, .. } => write!(f, "{value}"),
|
||||
Self::BinOp { left, right, op } => {
|
||||
let display_branch = |f: &mut std::fmt::Formatter, expr: &Self| {
|
||||
if let Self::BinOp { op: lop, .. } = expr
|
||||
&& op.precedence() > lop.precedence()
|
||||
{
|
||||
write!(f, "({expr})")
|
||||
} else {
|
||||
write!(f, "{expr}")
|
||||
}
|
||||
};
|
||||
|
||||
display_branch(f, left)?;
|
||||
write!(f, " {op} ")?;
|
||||
display_branch(f, right)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Arena<'a> {
|
||||
chunk: Cell<ArenaChunk>,
|
||||
ph: std::marker::PhantomData<&'a ()>,
|
||||
}
|
||||
|
||||
impl<'a> Arena<'a> {
|
||||
pub fn alloc_str(&self, token: &str) -> &'a str {
|
||||
let ptr = self.alloc_slice(token.as_bytes());
|
||||
unsafe { std::str::from_utf8_unchecked_mut(ptr) }
|
||||
}
|
||||
|
||||
pub fn alloc<T>(&self, value: T) -> &'a mut T {
|
||||
let layout = std::alloc::Layout::new::<T>();
|
||||
let ptr = self.alloc_low(layout);
|
||||
unsafe { ptr.cast::<T>().write(value) };
|
||||
unsafe { ptr.cast::<T>().as_mut() }
|
||||
}
|
||||
|
||||
pub fn alloc_slice<T: Copy>(&self, slice: &[T]) -> &'a mut [T] {
|
||||
let layout = std::alloc::Layout::array::<T>(slice.len()).unwrap();
|
||||
let ptr = self.alloc_low(layout);
|
||||
unsafe {
|
||||
ptr.as_ptr()
|
||||
.cast::<T>()
|
||||
.copy_from_nonoverlapping(slice.as_ptr(), slice.len())
|
||||
};
|
||||
unsafe { std::slice::from_raw_parts_mut(ptr.as_ptr() as _, slice.len()) }
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
let chunk = self.chunk.get_mut();
|
||||
if chunk.base.is_null() {
|
||||
return;
|
||||
}
|
||||
|
||||
loop {
|
||||
let prev = ArenaChunk::prev(chunk.base);
|
||||
if prev.is_null() {
|
||||
break;
|
||||
}
|
||||
chunk.base = prev;
|
||||
}
|
||||
|
||||
chunk.end = unsafe { chunk.base.add(ArenaChunk::PREV_OFFSET) };
|
||||
}
|
||||
|
||||
fn with_chunk<R>(&self, f: impl FnOnce(&mut ArenaChunk) -> R) -> R {
|
||||
let mut chunk = self.chunk.get();
|
||||
let r = f(&mut chunk);
|
||||
self.chunk.set(chunk);
|
||||
r
|
||||
}
|
||||
|
||||
fn alloc_low(&self, layout: std::alloc::Layout) -> NonNull<u8> {
|
||||
assert!(layout.align() <= ArenaChunk::ALIGN);
|
||||
assert!(layout.size() <= ArenaChunk::CHUNK_SIZE);
|
||||
self.with_chunk(|chunk| {
|
||||
if let Some(ptr) = chunk.alloc(layout) {
|
||||
return ptr;
|
||||
}
|
||||
|
||||
if let Some(prev) = ArenaChunk::reset(ArenaChunk::prev(chunk.base)) {
|
||||
*chunk = prev;
|
||||
} else {
|
||||
*chunk = ArenaChunk::new(chunk.base);
|
||||
}
|
||||
|
||||
chunk.alloc(layout).unwrap()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for Arena<'a> {
|
||||
fn drop(&mut self) {
|
||||
use ArenaChunk as AC;
|
||||
|
||||
let mut current = self.chunk.get().base;
|
||||
|
||||
let mut prev = AC::prev(current);
|
||||
while !prev.is_null() {
|
||||
let next = AC::next(prev);
|
||||
unsafe { std::alloc::dealloc(prev, AC::LAYOUT) };
|
||||
prev = next;
|
||||
}
|
||||
|
||||
while !current.is_null() {
|
||||
let next = AC::next(current);
|
||||
unsafe { std::alloc::dealloc(current, AC::LAYOUT) };
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct ArenaChunk {
|
||||
base: *mut u8,
|
||||
end: *mut u8,
|
||||
}
|
||||
|
||||
impl Default for ArenaChunk {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
base: std::ptr::null_mut(),
|
||||
end: std::ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ArenaChunk {
|
||||
const CHUNK_SIZE: usize = 1 << 16;
|
||||
const ALIGN: usize = std::mem::align_of::<Self>();
|
||||
const NEXT_OFFSET: usize = Self::CHUNK_SIZE - std::mem::size_of::<*mut u8>();
|
||||
const PREV_OFFSET: usize = Self::NEXT_OFFSET - std::mem::size_of::<*mut u8>();
|
||||
const LAYOUT: std::alloc::Layout =
|
||||
unsafe { std::alloc::Layout::from_size_align_unchecked(Self::CHUNK_SIZE, Self::ALIGN) };
|
||||
|
||||
fn new(next: *mut u8) -> Self {
|
||||
let base = unsafe { std::alloc::alloc(Self::LAYOUT) };
|
||||
let end = unsafe { base.add(Self::PREV_OFFSET) };
|
||||
if !next.is_null() {
|
||||
Self::set_prev(next, base);
|
||||
}
|
||||
Self::set_next(base, next);
|
||||
Self::set_prev(base, std::ptr::null_mut());
|
||||
Self { base, end }
|
||||
}
|
||||
|
||||
fn set_next(curr: *mut u8, next: *mut u8) {
|
||||
unsafe { std::ptr::write(curr.add(Self::NEXT_OFFSET) as *mut _, next) };
|
||||
}
|
||||
|
||||
fn set_prev(curr: *mut u8, prev: *mut u8) {
|
||||
unsafe { std::ptr::write(curr.add(Self::PREV_OFFSET) as *mut _, prev) };
|
||||
}
|
||||
|
||||
fn next(curr: *mut u8) -> *mut u8 {
|
||||
unsafe { std::ptr::read(curr.add(Self::NEXT_OFFSET) as *mut _) }
|
||||
}
|
||||
|
||||
fn prev(curr: *mut u8) -> *mut u8 {
|
||||
if curr.is_null() {
|
||||
return std::ptr::null_mut();
|
||||
}
|
||||
unsafe { std::ptr::read(curr.add(Self::PREV_OFFSET) as *mut _) }
|
||||
}
|
||||
|
||||
fn reset(prev: *mut u8) -> Option<Self> {
|
||||
if prev.is_null() {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Self {
|
||||
base: prev,
|
||||
end: unsafe { prev.add(Self::CHUNK_SIZE) },
|
||||
})
|
||||
}
|
||||
|
||||
fn alloc(&mut self, layout: std::alloc::Layout) -> Option<NonNull<u8>> {
|
||||
let padding = self.end as usize - (self.end as usize & !(layout.align() - 1));
|
||||
let size = layout.size() + padding;
|
||||
if size > self.end as usize - self.base as usize {
|
||||
return None;
|
||||
}
|
||||
unsafe { self.end = self.end.sub(size) };
|
||||
unsafe { Some(NonNull::new_unchecked(self.end)) }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
fn parse(input: &'static str, output: &mut String) {
|
||||
use std::fmt::Write;
|
||||
let mut arena = super::Arena::default();
|
||||
let mut parser = super::Parser::new(&arena);
|
||||
for expr in parser.file(input, "test") {
|
||||
writeln!(output, "{}", expr).unwrap();
|
||||
}
|
||||
arena.clear();
|
||||
}
|
||||
|
||||
crate::run_tests! { parse:
|
||||
example => include_str!("../examples/main_fn.hb");
|
||||
arithmetic => include_str!("../examples/arithmetic.hb");
|
||||
}
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
#![cfg(test)]
|
||||
|
||||
pub fn run_test(name: &'static str, input: &'static str, test: fn(&'static str, &mut String)) {
|
||||
use std::{io::Write, path::PathBuf};
|
||||
|
||||
let filter = std::env::var("PT_FILTER").unwrap_or_default();
|
||||
if !filter.is_empty() && !name.contains(&filter) {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut output = String::new();
|
||||
test(input, &mut output);
|
||||
|
||||
let mut root = PathBuf::from(std::env::var("PT_TEST_ROOT").unwrap_or("tests".to_string()));
|
||||
root.push(
|
||||
name.replace("::", "_")
|
||||
.replace(concat!(env!("CARGO_PKG_NAME"), "_"), ""),
|
||||
);
|
||||
root.set_extension("txt");
|
||||
|
||||
let expected = std::fs::read_to_string(&root).unwrap_or_default();
|
||||
|
||||
if output == expected {
|
||||
return;
|
||||
}
|
||||
|
||||
if std::env::var("PT_UPDATE").is_ok() {
|
||||
std::fs::write(&root, output).unwrap();
|
||||
return;
|
||||
}
|
||||
|
||||
if !root.exists() {
|
||||
std::fs::create_dir_all(root.parent().unwrap()).unwrap();
|
||||
std::fs::write(&root, vec![]).unwrap();
|
||||
}
|
||||
|
||||
let mut proc = std::process::Command::new("diff")
|
||||
.arg("-u")
|
||||
.arg("--color")
|
||||
.arg(&root)
|
||||
.arg("-")
|
||||
.stdin(std::process::Stdio::piped())
|
||||
.stdout(std::process::Stdio::inherit())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
|
||||
proc.stdin
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.write_all(output.as_bytes())
|
||||
.unwrap();
|
||||
|
||||
proc.wait().unwrap();
|
||||
|
||||
panic!();
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
code size: 209
|
||||
ret: 1
|
||||
status: Ok(())
|
|
@ -1,3 +0,0 @@
|
|||
code size: 527
|
||||
ret: 512
|
||||
status: Ok(())
|
|
@ -1,4 +0,0 @@
|
|||
ev: Ecall
|
||||
code size: 217
|
||||
ret: 0
|
||||
status: Ok(())
|
|
@ -1,3 +0,0 @@
|
|||
code size: 96
|
||||
ret: 1
|
||||
status: Ok(())
|
|
@ -1,3 +0,0 @@
|
|||
code size: 997
|
||||
ret: 0
|
||||
status: Ok(())
|
|
@ -1,3 +0,0 @@
|
|||
code size: 388
|
||||
ret: 33
|
||||
status: Ok(())
|
|
@ -1,3 +0,0 @@
|
|||
code size: 347
|
||||
ret: 55
|
||||
status: Ok(())
|
|
@ -1,3 +0,0 @@
|
|||
code size: 475
|
||||
ret: 55
|
||||
status: Ok(())
|
|
@ -1,3 +0,0 @@
|
|||
code size: 486
|
||||
ret: 0
|
||||
status: Ok(())
|
|
@ -1,3 +0,0 @@
|
|||
code size: 800
|
||||
ret: 10
|
||||
status: Ok(())
|
|
@ -1,3 +0,0 @@
|
|||
code size: 530
|
||||
ret: 3
|
||||
status: Ok(())
|
|
@ -1,3 +0,0 @@
|
|||
code size: 213
|
||||
ret: 0
|
||||
status: Ok(())
|
|
@ -1,31 +0,0 @@
|
|||
Ident "main"
|
||||
Decl ":="
|
||||
Fn "fn"
|
||||
LParen "("
|
||||
RParen ")"
|
||||
Colon ":"
|
||||
Ident "int"
|
||||
LBrace "{"
|
||||
Return "return"
|
||||
Number "10"
|
||||
Minus "-"
|
||||
Number "20"
|
||||
FSlash "/"
|
||||
Number "2"
|
||||
Plus "+"
|
||||
Number "4"
|
||||
Star "*"
|
||||
LParen "("
|
||||
Number "2"
|
||||
Plus "+"
|
||||
Number "2"
|
||||
RParen ")"
|
||||
Minus "-"
|
||||
Number "4"
|
||||
Star "*"
|
||||
Number "4"
|
||||
Plus "+"
|
||||
Number "1"
|
||||
Semi ";"
|
||||
RBrace "}"
|
||||
Eof ""
|
|
@ -1 +0,0 @@
|
|||
Eof ""
|
|
@ -1,13 +0,0 @@
|
|||
Ident "main"
|
||||
Decl ":="
|
||||
Fn "fn"
|
||||
LParen "("
|
||||
RParen ")"
|
||||
Colon ":"
|
||||
Ident "int"
|
||||
LBrace "{"
|
||||
Return "return"
|
||||
Number "1"
|
||||
Semi ";"
|
||||
RBrace "}"
|
||||
Eof ""
|
|
@ -1 +0,0 @@
|
|||
Eof ""
|
|
@ -1,3 +0,0 @@
|
|||
main := fn(): int {
|
||||
return 10 - 20 / 2 + 4 * (2 + 2) - 4 * 4 + 1;
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
main := fn(): int {
|
||||
return 1;
|
||||
}
|
|
@ -3,10 +3,14 @@ name = "hbvm"
|
|||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
default = ["alloc"]
|
||||
alloc = []
|
||||
nightly = []
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
||||
[dependencies]
|
||||
hbbytecode = { path = "../hbbytecode" }
|
||||
delegate = "0.9"
|
||||
derive_more = "0.99"
|
||||
hashbrown = "0.13"
|
||||
hbbytecode.path = "../hbbytecode"
|
||||
log = "0.4"
|
||||
paste = "1.0"
|
||||
static_assertions = "1.0"
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,5 +0,0 @@
|
|||
target
|
||||
artifacts
|
||||
corpus
|
||||
coverage
|
||||
Cargo.lock
|
|
@ -1,30 +0,0 @@
|
|||
[package]
|
||||
name = "hbvm-fuzz"
|
||||
version = "0.0.0"
|
||||
publish = false
|
||||
edition = "2021"
|
||||
|
||||
[package.metadata]
|
||||
cargo-fuzz = true
|
||||
|
||||
[dependencies]
|
||||
libfuzzer-sys = "0.4"
|
||||
|
||||
[dependencies.hbvm]
|
||||
path = ".."
|
||||
|
||||
[dependencies.hbbytecode]
|
||||
path = "../../hbbytecode"
|
||||
|
||||
# Prevent this from interfering with workspaces
|
||||
[workspace]
|
||||
members = ["."]
|
||||
|
||||
[profile.release]
|
||||
debug = 1
|
||||
|
||||
[[bin]]
|
||||
name = "vm"
|
||||
path = "fuzz_targets/vm.rs"
|
||||
test = false
|
||||
doc = false
|
|
@ -1,82 +0,0 @@
|
|||
#![no_main]
|
||||
|
||||
use {
|
||||
hbvm::{
|
||||
mem::{
|
||||
softpaging::{
|
||||
paging::{PageTable, Permission},
|
||||
HandlePageFault, PageSize, SoftPagedMem,
|
||||
},
|
||||
Address, MemoryAccessReason,
|
||||
},
|
||||
Vm,
|
||||
},
|
||||
libfuzzer_sys::fuzz_target,
|
||||
};
|
||||
|
||||
fuzz_target!(|data: &[u8]| {
|
||||
let mut vm = unsafe {
|
||||
Vm::<_, 16384>::new(
|
||||
SoftPagedMem::<_, true> {
|
||||
pf_handler: TestTrapHandler,
|
||||
program: data,
|
||||
root_pt: Box::into_raw(Default::default()),
|
||||
icache: Default::default(),
|
||||
},
|
||||
Address::new(4),
|
||||
)
|
||||
};
|
||||
|
||||
// Alloc and map some memory
|
||||
let pages = [
|
||||
alloc_and_map(&mut vm.memory, 0),
|
||||
alloc_and_map(&mut vm.memory, 4096),
|
||||
];
|
||||
|
||||
// Run VM
|
||||
let _ = vm.run();
|
||||
|
||||
// Unmap and dealloc the memory
|
||||
for (i, page) in pages.into_iter().enumerate() {
|
||||
unmap_and_dealloc(&mut vm.memory, page, i as u64 * 4096);
|
||||
}
|
||||
|
||||
let _ = unsafe { Box::from_raw(vm.memory.root_pt) };
|
||||
});
|
||||
|
||||
fn alloc_and_map(memory: &mut SoftPagedMem<TestTrapHandler>, at: u64) -> *mut u8 {
|
||||
let ptr = Box::into_raw(Box::<Page>::default()).cast();
|
||||
unsafe {
|
||||
memory
|
||||
.map(ptr, Address::new(at), Permission::Write, PageSize::Size4K)
|
||||
.unwrap()
|
||||
};
|
||||
ptr
|
||||
}
|
||||
|
||||
fn unmap_and_dealloc(memory: &mut SoftPagedMem<TestTrapHandler>, ptr: *mut u8, from: u64) {
|
||||
memory.unmap(Address::new(from)).unwrap();
|
||||
let _ = unsafe { Box::from_raw(ptr.cast::<Page>()) };
|
||||
}
|
||||
|
||||
#[repr(align(4096))]
|
||||
struct Page([u8; 4096]);
|
||||
impl Default for Page {
|
||||
fn default() -> Self {
|
||||
unsafe { std::mem::MaybeUninit::zeroed().assume_init() }
|
||||
}
|
||||
}
|
||||
|
||||
struct TestTrapHandler;
|
||||
impl HandlePageFault for TestTrapHandler {
|
||||
fn page_fault(
|
||||
&mut self,
|
||||
_: MemoryAccessReason,
|
||||
_: &mut PageTable,
|
||||
_: Address,
|
||||
_: PageSize,
|
||||
_: *mut u8,
|
||||
) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
141
hbvm/src/bmc.rs
141
hbvm/src/bmc.rs
|
@ -1,141 +0,0 @@
|
|||
//! Block memory copier state machine
|
||||
|
||||
use {
|
||||
super::{mem::MemoryAccessReason, Memory, VmRunError},
|
||||
crate::mem::Address,
|
||||
core::{mem::MaybeUninit, task::Poll},
|
||||
};
|
||||
|
||||
/// Buffer size (defaults to 4 KiB, a smallest page size on most platforms)
|
||||
const BUF_SIZE: usize = 4096;
|
||||
|
||||
/// Buffer of possibly uninitialised bytes, aligned to [`BUF_SIZE`]
|
||||
#[repr(align(4096))]
|
||||
struct AlignedBuf([MaybeUninit<u8>; BUF_SIZE]);
|
||||
|
||||
/// State for block memory copy
|
||||
pub struct BlockCopier {
|
||||
/// Source address
|
||||
src: Address,
|
||||
/// Destination address
|
||||
dst: Address,
|
||||
/// How many buffer sizes to copy?
|
||||
n_buffers: usize,
|
||||
/// …and what remainds after?
|
||||
rem: usize,
|
||||
}
|
||||
|
||||
impl BlockCopier {
|
||||
/// Construct a new one
|
||||
#[inline]
|
||||
pub fn new(src: Address, dst: Address, count: usize) -> Self {
|
||||
Self {
|
||||
src,
|
||||
dst,
|
||||
n_buffers: count / BUF_SIZE,
|
||||
rem: count % BUF_SIZE,
|
||||
}
|
||||
}
|
||||
|
||||
/// Copy one block
|
||||
///
|
||||
/// # Safety
|
||||
/// - Same as for [`Memory::load`] and [`Memory::store`]
|
||||
pub unsafe fn poll(&mut self, memory: &mut impl Memory) -> Poll<Result<(), BlkCopyError>> {
|
||||
// Safety: Assuming uninit of array of MaybeUninit is sound
|
||||
let mut buf = AlignedBuf(unsafe { MaybeUninit::uninit().assume_init() });
|
||||
|
||||
// We have at least one buffer size to copy
|
||||
if self.n_buffers != 0 {
|
||||
if let Err(e) = unsafe {
|
||||
act(
|
||||
memory,
|
||||
self.src,
|
||||
self.dst,
|
||||
buf.0.as_mut_ptr().cast(),
|
||||
BUF_SIZE,
|
||||
)
|
||||
} {
|
||||
return Poll::Ready(Err(e));
|
||||
}
|
||||
|
||||
// Bump source and destination address
|
||||
self.src += BUF_SIZE;
|
||||
self.dst += BUF_SIZE;
|
||||
|
||||
self.n_buffers -= 1;
|
||||
|
||||
return if self.n_buffers + self.rem == 0 {
|
||||
// If there is nothing left, we are done
|
||||
Poll::Ready(Ok(()))
|
||||
} else {
|
||||
// Otherwise let's advice to run it again
|
||||
Poll::Pending
|
||||
};
|
||||
}
|
||||
|
||||
if self.rem != 0 {
|
||||
if let Err(e) = unsafe {
|
||||
act(
|
||||
memory,
|
||||
self.src,
|
||||
self.dst,
|
||||
buf.0.as_mut_ptr().cast(),
|
||||
self.rem,
|
||||
)
|
||||
} {
|
||||
return Poll::Ready(Err(e));
|
||||
}
|
||||
}
|
||||
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Load to buffer and store from buffer
|
||||
#[inline]
|
||||
unsafe fn act(
|
||||
memory: &mut impl Memory,
|
||||
src: Address,
|
||||
dst: Address,
|
||||
buf: *mut u8,
|
||||
count: usize,
|
||||
) -> Result<(), BlkCopyError> {
|
||||
unsafe {
|
||||
// Load to buffer
|
||||
memory
|
||||
.load(src, buf, count)
|
||||
.map_err(|super::mem::LoadError(addr)| BlkCopyError {
|
||||
access_reason: MemoryAccessReason::Load,
|
||||
addr,
|
||||
})?;
|
||||
|
||||
// Store from buffer
|
||||
memory
|
||||
.store(dst, buf, count)
|
||||
.map_err(|super::mem::StoreError(addr)| BlkCopyError {
|
||||
access_reason: MemoryAccessReason::Store,
|
||||
addr,
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Error occured when copying a block of memory
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct BlkCopyError {
|
||||
/// Kind of access
|
||||
access_reason: MemoryAccessReason,
|
||||
/// VM Address
|
||||
addr: Address,
|
||||
}
|
||||
|
||||
impl From<BlkCopyError> for VmRunError {
|
||||
fn from(value: BlkCopyError) -> Self {
|
||||
match value.access_reason {
|
||||
MemoryAccessReason::Load => Self::LoadAccessEx(value.addr),
|
||||
MemoryAccessReason::Store => Self::StoreAccessEx(value.addr),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
use {core::arch::asm, hbbytecode::RoundingMode};
|
||||
|
||||
macro_rules! fnsdef {
|
||||
{$(
|
||||
$(#[$attr:meta])*
|
||||
$vis:vis fn $name:ident[$inreg:ident -> $outreg:ident]($from:ident -> $to:ident): $ins:literal;
|
||||
)*} => {$(
|
||||
$(#[$attr])*
|
||||
$vis fn $name(val: $from, mode: RoundingMode) -> $to {
|
||||
let result: $to;
|
||||
unsafe {
|
||||
if mode == RoundingMode::NearestEven {
|
||||
return;
|
||||
}
|
||||
|
||||
let fpcr: u64;
|
||||
unsafe { asm!("mrs {}, fpcr", out(reg) fpcr) };
|
||||
|
||||
let fpcr_new = fpcr & !(0b11 << 22)
|
||||
| (match mode {
|
||||
RoundingMode::NearestEven => 0b00,
|
||||
RoundingMode::Truncate => 0b11,
|
||||
RoundingMode::Up => 0b01,
|
||||
RoundingMode::Down => 0b10,
|
||||
}) << 22;
|
||||
|
||||
unsafe { asm!("msr fpcr, {}", in(reg) fpcr_new) };
|
||||
asm!(
|
||||
$ins,
|
||||
out($outreg) result,
|
||||
in($inreg) val,
|
||||
);
|
||||
unsafe { asm!("msr fpcr, {}", in(reg) fpcr) };
|
||||
}
|
||||
result
|
||||
}
|
||||
)*};
|
||||
}
|
||||
|
||||
fnsdef! {
|
||||
/// Convert [`f64`] to [`f32`] with chosen rounding mode
|
||||
pub fn conv64to32[vreg -> vreg](f64 -> f32): "fcvt {:s}, {:d}";
|
||||
|
||||
/// Convert [`f32`] to [`i64`] with chosen rounding mode
|
||||
pub fn f32toint[vreg -> reg](f32 -> i64): "fcvtzs {}, {:s}";
|
||||
|
||||
/// Convert [`f64`] to [`i64`] with chosen rounding mode
|
||||
pub fn f64toint[vreg -> reg](f64 -> i64): "fcvtzs {}, {:d}";
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
macro_rules! arch_specific {
|
||||
{
|
||||
$({$($cfg:tt)*} : $mod:ident;)*
|
||||
} => {
|
||||
$(
|
||||
#[cfg($($cfg)*)]
|
||||
mod $mod;
|
||||
|
||||
#[cfg($($cfg)*)]
|
||||
pub use $mod::*;
|
||||
|
||||
#[cfg($($cfg)*)]
|
||||
pub const FL_ARCH_SPECIFIC_SUPPORTED: bool = true;
|
||||
)*
|
||||
|
||||
#[cfg(not(any($($($cfg)*),*)))]
|
||||
mod unsupported;
|
||||
|
||||
#[cfg(not(any($($($cfg)*),*)))]
|
||||
pub use unsupported::*;
|
||||
|
||||
#[cfg(not(any($($($cfg)*),*)))]
|
||||
pub const FL_ARCH_SPECIFIC_SUPPORTED: bool = false;
|
||||
};
|
||||
}
|
||||
|
||||
arch_specific! {
|
||||
{target_arch = "x86_64" }: x86_64;
|
||||
{target_arch = "riscv64"}: riscv64;
|
||||
{target_arch = "aarch64"}: aarch64;
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
use {core::arch::asm, hbbytecode::RoundingMode};
|
||||
|
||||
macro_rules! roundm_op_litmode_internal {
|
||||
($ins:literal, $in:expr, $out:expr => $outy:ident, $mode:literal $(,)?) => {
|
||||
asm!(
|
||||
concat!($ins, " {}, {}, ", $mode),
|
||||
out($outy) $out,
|
||||
in(freg) $in,
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! gen_roundm_op_litmode {
|
||||
[$($ty:ident => $reg:ident),* $(,)?] => {
|
||||
macro_rules! roundm_op_litmode {
|
||||
$(
|
||||
($ins:literal, $in:expr, $out:expr => $ty, $mode:literal) => {
|
||||
roundm_op_litmode_internal!($ins, $in, $out => $reg, $mode)
|
||||
};
|
||||
)*
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
gen_roundm_op_litmode![
|
||||
f32 => freg,
|
||||
f64 => freg,
|
||||
i64 => reg,
|
||||
];
|
||||
|
||||
macro_rules! fnsdef {
|
||||
{$(
|
||||
$(#[$attr:meta])*
|
||||
$vis:vis fn $name:ident($from:ident -> $to:ident): $ins:literal;
|
||||
)*} => {$(
|
||||
$(#[$attr])*
|
||||
$vis fn $name(val: $from, mode: RoundingMode) -> $to {
|
||||
let result: $to;
|
||||
unsafe {
|
||||
match mode {
|
||||
RoundingMode::NearestEven => roundm_op_litmode!($ins, val, result => $to, "rne"),
|
||||
RoundingMode::Truncate => roundm_op_litmode!($ins, val, result => $to, "rtz"),
|
||||
RoundingMode::Up => roundm_op_litmode!($ins, val, result => $to, "rup"),
|
||||
RoundingMode::Down => roundm_op_litmode!($ins, val, result => $to, "rdn"),
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
)*};
|
||||
}
|
||||
|
||||
fnsdef! {
|
||||
/// Convert [`f64`] to [`f32`] with chosen rounding mode
|
||||
pub fn conv64to32(f64 -> f32): "fcvt.s.d";
|
||||
/// Convert [`f32`] to [`i64`] with chosen rounding mode
|
||||
pub fn f32toint(f32 -> i64): "fcvt.l.s";
|
||||
/// Convert [`f64`] to [`i64`] with chosen rounding mode
|
||||
pub fn f64toint(f64 -> i64): "fcvt.l.d";
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
use hbbytecode::RoundingMode;
|
||||
|
||||
#[inline(always)]
|
||||
pub fn conv64to32(_: f64, _: RoundingMode) -> f32 {
|
||||
f32::NAN
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn f32toint(_: f32, _: RoundingMode) -> i64 {
|
||||
i64::MAX
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn f64toint(_: f64, _: RoundingMode) -> i64 {
|
||||
i64::MAX
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
use {
|
||||
core::arch::{asm, x86_64 as arin},
|
||||
hbbytecode::RoundingMode,
|
||||
};
|
||||
|
||||
macro_rules! gen_op {
|
||||
[$($ty:ident => $reg:ident),* $(,)?] => {
|
||||
macro_rules! op {
|
||||
$(
|
||||
($ins:literal, $in:expr, $out:expr => $ty) => {
|
||||
asm!(concat!($ins, " {}, {}"), out($reg) $out, in(xmm_reg) $in)
|
||||
};
|
||||
)*
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
gen_op![
|
||||
f32 => xmm_reg,
|
||||
f64 => xmm_reg,
|
||||
i64 => reg,
|
||||
];
|
||||
|
||||
macro_rules! fnsdef {
|
||||
{$(
|
||||
$(#[$attr:meta])*
|
||||
$vis:vis fn $name:ident($from:ident -> $to:ident): $ins:literal;
|
||||
)*} => {$(
|
||||
$(#[$attr])*
|
||||
$vis fn $name(val: $from, mode: RoundingMode) -> $to {
|
||||
let result: $to;
|
||||
unsafe {
|
||||
let mut mxcsr = 0_u32;
|
||||
'a: {
|
||||
asm!("stmxcsr [{}]", in(reg) &mut mxcsr);
|
||||
asm!(
|
||||
"ldmxcsr [{}]",
|
||||
in(reg) &(mxcsr & !arin::_MM_ROUND_MASK | match mode {
|
||||
RoundingMode::NearestEven => break 'a,
|
||||
RoundingMode::Truncate => arin::_MM_ROUND_TOWARD_ZERO,
|
||||
RoundingMode::Up => arin::_MM_ROUND_UP,
|
||||
RoundingMode::Down => arin::_MM_ROUND_DOWN,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
op!($ins, val, result => $to);
|
||||
|
||||
// Set MXCSR to original value
|
||||
asm!("ldmxcsr [{}]", in(reg) &mxcsr);
|
||||
}
|
||||
result
|
||||
}
|
||||
)*};
|
||||
}
|
||||
|
||||
fnsdef! {
|
||||
/// Convert [`f64`] to [`f32`] with chosen rounding mode
|
||||
pub fn conv64to32(f64 -> f32): "cvtsd2ss";
|
||||
/// Convert [`f32`] to [`i64`] with chosen rounding mode
|
||||
pub fn f32toint(f32 -> i64): "cvttss2si";
|
||||
/// Convert [`f64`] to [`i64`] with chosen rounding mode
|
||||
pub fn f64toint(f64 -> i64): "cvttsd2si";
|
||||
}
|
142
hbvm/src/lib.rs
142
hbvm/src/lib.rs
|
@ -1,143 +1,7 @@
|
|||
//! HoleyBytes Virtual Machine
|
||||
//!
|
||||
//! # Alloc feature
|
||||
//! - Enabled by default
|
||||
//! - Provides mapping / unmapping, as well as [`Default`] and [`Drop`]
|
||||
//! implementations for soft-paged memory implementation
|
||||
|
||||
// # General safety notice:
|
||||
// - Validation has to assure there is 256 registers (r0 - r255)
|
||||
// - Instructions have to be valid as specified (values and sizes)
|
||||
// - Mapped pages should be at least 4 KiB
|
||||
|
||||
#![doc = include_str!("../README.md")]
|
||||
#![no_std]
|
||||
#![cfg_attr(feature = "nightly", feature(fn_align))]
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
extern crate alloc;
|
||||
|
||||
pub mod mem;
|
||||
pub mod value;
|
||||
|
||||
mod bmc;
|
||||
mod float;
|
||||
mod utils;
|
||||
mod vmrun;
|
||||
|
||||
pub use float::FL_ARCH_SPECIFIC_SUPPORTED;
|
||||
|
||||
use {
|
||||
bmc::BlockCopier,
|
||||
mem::{Address, Memory},
|
||||
value::{Value, ValueVariant},
|
||||
};
|
||||
|
||||
/// HoleyBytes Virtual Machine
|
||||
pub struct Vm<Mem, const TIMER_QUOTIENT: usize> {
|
||||
/// Holds 256 registers
|
||||
///
|
||||
/// Writing to register 0 is considered undefined behaviour
|
||||
/// in terms of HoleyBytes program execution
|
||||
pub registers: [Value; 256],
|
||||
|
||||
/// Memory implementation
|
||||
pub memory: Mem,
|
||||
|
||||
/// Program counter
|
||||
pub pc: Address,
|
||||
|
||||
/// Program timer
|
||||
timer: usize,
|
||||
|
||||
/// Saved block copier
|
||||
copier: Option<BlockCopier>,
|
||||
}
|
||||
|
||||
impl<Mem, const TIMER_QUOTIENT: usize> Vm<Mem, TIMER_QUOTIENT>
|
||||
where
|
||||
Mem: Memory,
|
||||
{
|
||||
/// Create a new VM with program and trap handler
|
||||
///
|
||||
/// # Safety
|
||||
/// Program code has to be validated
|
||||
pub unsafe fn new(memory: Mem, entry: Address) -> Self {
|
||||
Self {
|
||||
registers: [Value::from(0_u64); 256],
|
||||
memory,
|
||||
pc: entry,
|
||||
timer: 0,
|
||||
copier: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Read register
|
||||
#[inline(always)]
|
||||
pub fn read_reg(&self, n: u8) -> Value {
|
||||
unsafe { *self.registers.get_unchecked(n as usize) }
|
||||
}
|
||||
|
||||
/// Write a register.
|
||||
/// Writing to register 0 is no-op.
|
||||
#[inline(always)]
|
||||
pub fn write_reg<T: ValueVariant>(&mut self, n: u8, value: T) {
|
||||
if n != 0 {
|
||||
unsafe {
|
||||
core::ptr::copy_nonoverlapping(
|
||||
(&value as *const T).cast::<u8>(),
|
||||
self.registers.as_mut_ptr().add(n.into()).cast::<u8>(),
|
||||
core::mem::size_of::<T>(),
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Virtual machine halt error
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
pub enum VmRunError {
|
||||
/// Tried to execute invalid instruction
|
||||
InvalidOpcode(u8),
|
||||
|
||||
/// Unhandled load access exception
|
||||
LoadAccessEx(Address),
|
||||
|
||||
/// Unhandled instruction load access exception
|
||||
ProgramFetchLoadEx(Address),
|
||||
|
||||
/// Unhandled store access exception
|
||||
StoreAccessEx(Address),
|
||||
|
||||
/// Register out-of-bounds access
|
||||
RegOutOfBounds,
|
||||
|
||||
/// Address out-of-bounds
|
||||
AddrOutOfBounds,
|
||||
|
||||
/// Reached unreachable code
|
||||
Unreachable,
|
||||
|
||||
/// Invalid operand
|
||||
InvalidOperand,
|
||||
|
||||
/// Unimplemented feature
|
||||
Unimplemented,
|
||||
}
|
||||
|
||||
/// Virtual machine halt ok
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub enum VmRunOk {
|
||||
/// Program has eached its end
|
||||
End,
|
||||
|
||||
/// Program was interrupted by a timer
|
||||
Timer,
|
||||
|
||||
/// Environment call
|
||||
Ecall,
|
||||
|
||||
/// Breakpoint
|
||||
Breakpoint,
|
||||
}
|
||||
pub mod validate;
|
||||
pub mod vm;
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
use hbvm::vm::{
|
||||
mem::{Memory, MemoryAccessReason, PageSize},
|
||||
trap::HandleTrap,
|
||||
value::Value,
|
||||
};
|
||||
|
||||
use {
|
||||
hbvm::{validate::validate, vm::Vm},
|
||||
std::io::{stdin, Read},
|
||||
};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut prog = vec![];
|
||||
stdin().read_to_end(&mut prog)?;
|
||||
|
||||
if let Err(e) = validate(&prog) {
|
||||
eprintln!("Program validation error: {e:?}");
|
||||
return Ok(());
|
||||
} else {
|
||||
unsafe {
|
||||
let mut vm = Vm::<_, 0>::new_unchecked(&prog, TestTrapHandler);
|
||||
vm.memory.insert_test_page();
|
||||
println!("Program interrupt: {:?}", vm.run());
|
||||
println!("{:?}", vm.registers);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn time() -> u32 {
|
||||
9
|
||||
}
|
||||
|
||||
struct TestTrapHandler;
|
||||
impl HandleTrap for TestTrapHandler {
|
||||
fn page_fault(
|
||||
&mut self,
|
||||
_: MemoryAccessReason,
|
||||
_: &mut Memory,
|
||||
_: u64,
|
||||
_: PageSize,
|
||||
_: *mut u8,
|
||||
) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn invalid_op(&mut self, _: &mut [Value; 256], _: &mut usize, _: &mut Memory, _: u8) -> bool
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
false
|
||||
}
|
||||
|
||||
fn ecall(&mut self, _: &mut [Value; 256], _: &mut usize, _: &mut Memory)
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,137 +0,0 @@
|
|||
//! Virtual(?) memory address
|
||||
|
||||
use {
|
||||
crate::utils::impl_display,
|
||||
core::{fmt::Debug, ops},
|
||||
};
|
||||
|
||||
/// Memory address
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Address(u64);
|
||||
impl Address {
|
||||
/// A null address
|
||||
pub const NULL: Self = Self(0);
|
||||
|
||||
/// Saturating integer addition. Computes self + rhs, saturating at the numeric bounds instead of overflowing.
|
||||
#[inline]
|
||||
pub fn saturating_add<T: AddressOp>(self, rhs: T) -> Self {
|
||||
Self(self.0.saturating_add(rhs.cast_u64()))
|
||||
}
|
||||
|
||||
/// Saturating integer subtraction. Computes self - rhs, saturating at the numeric bounds instead of overflowing.
|
||||
#[inline]
|
||||
pub fn saturating_sub<T: AddressOp>(self, rhs: T) -> Self {
|
||||
Self(self.0.saturating_sub(rhs.cast_u64()))
|
||||
}
|
||||
|
||||
/// Wrapping integer addition. Computes self + rhs, wrapping the numeric bounds.
|
||||
#[inline]
|
||||
pub fn wrapping_add<T: AddressOp>(self, rhs: T) -> Self {
|
||||
Self(self.0.wrapping_add(rhs.cast_u64()))
|
||||
}
|
||||
|
||||
/// Wrapping integer subtraction. Computes self + rhs, wrapping the numeric bounds.
|
||||
#[inline]
|
||||
pub fn wrapping_sub<T: AddressOp>(self, rhs: T) -> Self {
|
||||
Self(self.0.wrapping_sub(rhs.cast_u64()))
|
||||
}
|
||||
|
||||
/// Cast or if smaller, truncate to [`usize`]
|
||||
pub fn truncate_usize(self) -> usize {
|
||||
self.0 as _
|
||||
}
|
||||
|
||||
/// Get inner value
|
||||
#[inline(always)]
|
||||
pub fn get(self) -> u64 {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Get ptr to the next instruction
|
||||
#[inline(always)]
|
||||
pub fn next<A>(self) -> u64 {
|
||||
self.0.wrapping_add(core::mem::size_of::<A>() as u64 + 1)
|
||||
}
|
||||
|
||||
/// Construct new address
|
||||
#[inline(always)]
|
||||
pub fn new(val: u64) -> Self {
|
||||
Self(val)
|
||||
}
|
||||
|
||||
/// Do something with inner value
|
||||
#[inline(always)]
|
||||
pub fn map(self, f: impl Fn(u64) -> u64) -> Self {
|
||||
Self(f(self.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl_display!(for Address =>
|
||||
|Address(a)| "{a:0x}"
|
||||
);
|
||||
|
||||
impl<T: AddressOp> ops::Add<T> for Address {
|
||||
type Output = Self;
|
||||
|
||||
#[inline]
|
||||
fn add(self, rhs: T) -> Self::Output {
|
||||
Self(self.0.wrapping_add(rhs.cast_u64()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AddressOp> ops::Sub<T> for Address {
|
||||
type Output = Self;
|
||||
|
||||
#[inline]
|
||||
fn sub(self, rhs: T) -> Self::Output {
|
||||
Self(self.0.wrapping_sub(rhs.cast_u64()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AddressOp> ops::AddAssign<T> for Address {
|
||||
fn add_assign(&mut self, rhs: T) {
|
||||
self.0 = self.0.wrapping_add(rhs.cast_u64())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AddressOp> ops::SubAssign<T> for Address {
|
||||
fn sub_assign(&mut self, rhs: T) {
|
||||
self.0 = self.0.wrapping_sub(rhs.cast_u64())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Address> for u64 {
|
||||
#[inline(always)]
|
||||
fn from(value: Address) -> Self {
|
||||
value.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Address {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
write!(f, "[{:0x}]", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Can perform address operations with
|
||||
pub trait AddressOp {
|
||||
/// Cast to u64, truncating or extending
|
||||
fn cast_u64(self) -> u64;
|
||||
}
|
||||
|
||||
macro_rules! impl_address_ops_u(($($ty:ty),* $(,)?) => {
|
||||
$(impl AddressOp for $ty {
|
||||
#[inline(always)]
|
||||
fn cast_u64(self) -> u64 { self as _ }
|
||||
})*
|
||||
});
|
||||
|
||||
macro_rules! impl_address_ops_i(($($ty:ty),* $(,)?) => {
|
||||
$(impl AddressOp for $ty {
|
||||
#[inline(always)]
|
||||
fn cast_u64(self) -> u64 { self as i64 as u64 }
|
||||
})*
|
||||
});
|
||||
|
||||
impl_address_ops_u!(u8, u16, u32, u64, usize);
|
||||
impl_address_ops_i!(i8, i16, i32, i64, isize);
|
|
@ -1,80 +0,0 @@
|
|||
//! Memory implementations
|
||||
|
||||
pub mod softpaging;
|
||||
|
||||
pub(crate) mod addr;
|
||||
|
||||
pub use addr::Address;
|
||||
|
||||
use crate::utils::impl_display;
|
||||
|
||||
/// Load-store memory access
|
||||
pub trait Memory {
|
||||
/// Load data from memory on address
|
||||
///
|
||||
/// # Safety
|
||||
/// - Shall not overrun the buffer
|
||||
unsafe fn load(
|
||||
&mut self,
|
||||
addr: Address,
|
||||
target: *mut u8,
|
||||
count: usize,
|
||||
) -> Result<(), LoadError>;
|
||||
|
||||
/// Store data to memory on address
|
||||
///
|
||||
/// # Safety
|
||||
/// - Shall not overrun the buffer
|
||||
unsafe fn store(
|
||||
&mut self,
|
||||
addr: Address,
|
||||
source: *const u8,
|
||||
count: usize,
|
||||
) -> Result<(), StoreError>;
|
||||
|
||||
/// Read from program memory to execute
|
||||
///
|
||||
/// # Safety
|
||||
/// - Data read have to be valid
|
||||
unsafe fn prog_read<T: Copy>(&mut self, addr: Address) -> T;
|
||||
}
|
||||
|
||||
/// Unhandled load access trap
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct LoadError(pub Address);
|
||||
impl_display!(for LoadError =>
|
||||
|LoadError(a)| "Load access error at address {a}",
|
||||
);
|
||||
|
||||
/// Unhandled store access trap
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct StoreError(pub Address);
|
||||
impl_display!(for StoreError =>
|
||||
|StoreError(a)| "Load access error at address {a}",
|
||||
);
|
||||
|
||||
/// Reason to access memory
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum MemoryAccessReason {
|
||||
/// Memory was accessed for load (read)
|
||||
Load,
|
||||
/// Memory was accessed for store (write)
|
||||
Store,
|
||||
}
|
||||
|
||||
impl_display!(for MemoryAccessReason => match {
|
||||
Self::Load => const "Load";
|
||||
Self::Store => const "Store";
|
||||
});
|
||||
|
||||
impl From<LoadError> for crate::VmRunError {
|
||||
fn from(value: LoadError) -> Self {
|
||||
Self::LoadAccessEx(value.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<StoreError> for crate::VmRunError {
|
||||
fn from(value: StoreError) -> Self {
|
||||
Self::StoreAccessEx(value.0)
|
||||
}
|
||||
}
|
|
@ -1,111 +0,0 @@
|
|||
//! Program instruction cache
|
||||
|
||||
use crate::mem::Address;
|
||||
|
||||
use {
|
||||
super::{lookup::AddrPageLookuper, paging::PageTable, PageSize},
|
||||
core::{
|
||||
mem::{size_of, MaybeUninit},
|
||||
ptr::{copy_nonoverlapping, NonNull},
|
||||
},
|
||||
};
|
||||
|
||||
/// Instruction cache
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ICache {
|
||||
/// Current page address base
|
||||
base: Address,
|
||||
/// Curent page pointer
|
||||
data: Option<NonNull<u8>>,
|
||||
/// Current page size
|
||||
size: PageSize,
|
||||
/// Address mask
|
||||
mask: u64,
|
||||
}
|
||||
|
||||
impl Default for ICache {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
base: Address::NULL,
|
||||
data: Default::default(),
|
||||
size: PageSize::Size4K,
|
||||
mask: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ICache {
|
||||
/// Fetch instruction from cache
|
||||
///
|
||||
/// # Safety
|
||||
/// `T` should be valid to read from instruction memory
|
||||
pub(super) unsafe fn fetch<T>(
|
||||
&mut self,
|
||||
addr: Address,
|
||||
root_pt: *const PageTable,
|
||||
) -> Option<T> {
|
||||
let mut ret = MaybeUninit::<T>::uninit();
|
||||
|
||||
let pbase = self
|
||||
.data
|
||||
.or_else(|| unsafe { self.fetch_page(self.base + self.size, root_pt) })?;
|
||||
|
||||
// Get address base
|
||||
let base = addr.map(|x| x & self.mask);
|
||||
|
||||
// Base not matching, fetch anew
|
||||
if base != self.base {
|
||||
unsafe { self.fetch_page(base, root_pt) }?;
|
||||
};
|
||||
|
||||
let offset = addr.get() & !self.mask;
|
||||
let requ_size = size_of::<T>();
|
||||
|
||||
// Page overflow
|
||||
let rem = (offset as usize)
|
||||
.saturating_add(requ_size)
|
||||
.saturating_sub(self.size as _);
|
||||
let first_copy = requ_size.saturating_sub(rem);
|
||||
|
||||
// Copy non-overflowing part
|
||||
unsafe { copy_nonoverlapping(pbase.as_ptr(), ret.as_mut_ptr().cast::<u8>(), first_copy) };
|
||||
|
||||
// Copy overflow
|
||||
if rem != 0 {
|
||||
let pbase = unsafe { self.fetch_page(self.base + self.size, root_pt) }?;
|
||||
|
||||
// Unlikely, unsupported scenario
|
||||
if rem > self.size as _ {
|
||||
return None;
|
||||
}
|
||||
|
||||
unsafe {
|
||||
copy_nonoverlapping(
|
||||
pbase.as_ptr(),
|
||||
ret.as_mut_ptr().cast::<u8>().add(first_copy),
|
||||
rem,
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
Some(unsafe { ret.assume_init() })
|
||||
}
|
||||
|
||||
/// Fetch a page
|
||||
unsafe fn fetch_page(&mut self, addr: Address, pt: *const PageTable) -> Option<NonNull<u8>> {
|
||||
let res = AddrPageLookuper::new(addr, 0, pt).next()?.ok()?;
|
||||
if !super::perm_check::executable(res.perm) {
|
||||
return None;
|
||||
}
|
||||
|
||||
(self.size, self.mask) = match res.size {
|
||||
4096 => (PageSize::Size4K, !((1 << 8) - 1)),
|
||||
2097152 => (PageSize::Size2M, !((1 << (8 * 2)) - 1)),
|
||||
1073741824 => (PageSize::Size1G, !((1 << (8 * 3)) - 1)),
|
||||
_ => return None,
|
||||
};
|
||||
self.data = Some(NonNull::new(res.ptr)?);
|
||||
self.base = addr.map(|x| x & self.mask);
|
||||
self.data
|
||||
}
|
||||
}
|
|
@ -1,126 +0,0 @@
|
|||
//! Address lookup
|
||||
|
||||
use crate::mem::addr::Address;
|
||||
|
||||
use super::{
|
||||
addr_extract_index,
|
||||
paging::{PageTable, Permission},
|
||||
PageSize,
|
||||
};
|
||||
|
||||
/// Good result from address split
|
||||
pub struct AddrPageLookupOk {
|
||||
/// Virtual address
|
||||
pub vaddr: Address,
|
||||
|
||||
/// Pointer to the start for perform operation
|
||||
pub ptr: *mut u8,
|
||||
|
||||
/// Size to the end of page / end of desired size
|
||||
pub size: usize,
|
||||
|
||||
/// Page permission
|
||||
pub perm: Permission,
|
||||
}
|
||||
|
||||
/// Errornous address split result
|
||||
pub struct AddrPageLookupError {
|
||||
/// Address of failure
|
||||
pub addr: Address,
|
||||
|
||||
/// Requested page size
|
||||
pub size: PageSize,
|
||||
}
|
||||
|
||||
/// Address splitter into pages
|
||||
pub struct AddrPageLookuper {
|
||||
/// Current address
|
||||
addr: Address,
|
||||
|
||||
/// Size left
|
||||
size: usize,
|
||||
|
||||
/// Page table
|
||||
pagetable: *const PageTable,
|
||||
}
|
||||
|
||||
impl AddrPageLookuper {
|
||||
/// Create a new page lookuper
|
||||
#[inline]
|
||||
pub const fn new(addr: Address, size: usize, pagetable: *const PageTable) -> Self {
|
||||
Self {
|
||||
addr,
|
||||
size,
|
||||
pagetable,
|
||||
}
|
||||
}
|
||||
|
||||
/// Bump address by size X
|
||||
pub fn bump(&mut self, page_size: PageSize) {
|
||||
self.addr += page_size;
|
||||
self.size = self.size.saturating_sub(page_size as _);
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for AddrPageLookuper {
|
||||
type Item = Result<AddrPageLookupOk, AddrPageLookupError>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
// The end, everything is fine
|
||||
if self.size == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let (base, perm, size, offset) = 'a: {
|
||||
let mut current_pt = self.pagetable;
|
||||
|
||||
// Walk the page table
|
||||
for lvl in (0..5).rev() {
|
||||
// Get an entry
|
||||
unsafe {
|
||||
let entry = (*current_pt)
|
||||
.table
|
||||
.get_unchecked(addr_extract_index(self.addr, lvl));
|
||||
|
||||
let ptr = entry.ptr();
|
||||
match entry.permission() {
|
||||
// No page → page fault
|
||||
Permission::Empty => {
|
||||
return Some(Err(AddrPageLookupError {
|
||||
addr: self.addr,
|
||||
size: PageSize::from_lvl(lvl)?,
|
||||
}))
|
||||
}
|
||||
|
||||
// Node → proceed waking
|
||||
Permission::Node => current_pt = ptr as _,
|
||||
|
||||
// Leaf → return relevant data
|
||||
perm => {
|
||||
break 'a (
|
||||
// Pointer in host memory
|
||||
ptr as *mut u8,
|
||||
perm,
|
||||
PageSize::from_lvl(lvl)?,
|
||||
// In-page offset
|
||||
addr_extract_index(self.addr, lvl),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return None; // Reached the end (should not happen)
|
||||
};
|
||||
|
||||
// Get available byte count in the selected page with offset
|
||||
let avail = (size as usize).saturating_sub(offset).clamp(0, self.size);
|
||||
self.bump(size);
|
||||
|
||||
Some(Ok(AddrPageLookupOk {
|
||||
vaddr: self.addr,
|
||||
ptr: unsafe { base.add(offset) }, // Return pointer to the start of region
|
||||
size: avail,
|
||||
perm,
|
||||
}))
|
||||
}
|
||||
}
|
|
@ -1,172 +0,0 @@
|
|||
//! Automatic memory mapping
|
||||
|
||||
use crate::{mem::addr::Address, utils::impl_display};
|
||||
|
||||
use {
|
||||
super::{
|
||||
addr_extract_index,
|
||||
paging::{PageTable, Permission, PtEntry, PtPointedData},
|
||||
PageSize, SoftPagedMem,
|
||||
},
|
||||
alloc::boxed::Box,
|
||||
};
|
||||
|
||||
impl<'p, A, const OUT_PROG_EXEC: bool> SoftPagedMem<'p, A, OUT_PROG_EXEC> {
|
||||
/// Maps host's memory into VM's memory
|
||||
///
|
||||
/// # Safety
|
||||
/// - Your faith in the gods of UB
|
||||
/// - Addr-san claims it's fine but who knows is she isn't lying :ferrisSus:
|
||||
/// - Alright, Miri-sama is also fine with this, who knows why
|
||||
pub unsafe fn map(
|
||||
&mut self,
|
||||
host: *mut u8,
|
||||
target: Address,
|
||||
perm: Permission,
|
||||
pagesize: PageSize,
|
||||
) -> Result<(), MapError> {
|
||||
let mut current_pt = self.root_pt;
|
||||
|
||||
// Decide on what level depth are we going
|
||||
let lookup_depth = match pagesize {
|
||||
PageSize::Size4K => 0,
|
||||
PageSize::Size2M => 1,
|
||||
PageSize::Size1G => 2,
|
||||
};
|
||||
|
||||
// Walk pagetable levels
|
||||
for lvl in (lookup_depth + 1..5).rev() {
|
||||
let entry = unsafe {
|
||||
(*current_pt)
|
||||
.table
|
||||
.get_unchecked_mut(addr_extract_index(target, lvl))
|
||||
};
|
||||
|
||||
let ptr = entry.ptr();
|
||||
match entry.permission() {
|
||||
// Still not on target and already seeing empty entry?
|
||||
// No worries! Let's create one (allocates).
|
||||
Permission::Empty => {
|
||||
// Increase children count
|
||||
unsafe { *current_pt }.childen += 1;
|
||||
|
||||
let table = Box::into_raw(Box::new(PtPointedData {
|
||||
pt: PageTable::default(),
|
||||
}));
|
||||
|
||||
unsafe { core::ptr::write(entry, PtEntry::new(table, Permission::Node)) };
|
||||
current_pt = table as _;
|
||||
}
|
||||
// Continue walking
|
||||
Permission::Node => current_pt = ptr as _,
|
||||
|
||||
// There is some entry on place of node
|
||||
_ => return Err(MapError::PageOnNode),
|
||||
}
|
||||
}
|
||||
|
||||
let node = unsafe {
|
||||
(*current_pt)
|
||||
.table
|
||||
.get_unchecked_mut(addr_extract_index(target, lookup_depth))
|
||||
};
|
||||
|
||||
// Check if node is not mapped
|
||||
if node.permission() != Permission::Empty {
|
||||
return Err(MapError::AlreadyMapped);
|
||||
}
|
||||
|
||||
// Write entry
|
||||
unsafe {
|
||||
(*current_pt).childen += 1;
|
||||
core::ptr::write(node, PtEntry::new(host.cast(), perm));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Unmaps pages from VM's memory
|
||||
///
|
||||
/// If errors, it only means there is no entry to unmap and in most cases
|
||||
/// just should be ignored.
|
||||
pub fn unmap(&mut self, addr: Address) -> Result<(), NothingToUnmap> {
|
||||
let mut current_pt = self.root_pt;
|
||||
let mut page_tables = [core::ptr::null_mut(); 5];
|
||||
|
||||
// Walk page table in reverse
|
||||
for lvl in (0..5).rev() {
|
||||
let entry = unsafe {
|
||||
(*current_pt)
|
||||
.table
|
||||
.get_unchecked_mut(addr_extract_index(addr, lvl))
|
||||
};
|
||||
|
||||
let ptr = entry.ptr();
|
||||
match entry.permission() {
|
||||
// Nothing is there, throw an error, not critical!
|
||||
Permission::Empty => return Err(NothingToUnmap),
|
||||
// Node – Save to visited pagetables and continue walking
|
||||
Permission::Node => {
|
||||
page_tables[lvl as usize] = entry;
|
||||
current_pt = ptr as _
|
||||
}
|
||||
// Page entry – zero it out!
|
||||
// Zero page entry is completely valid entry with
|
||||
// empty permission - no UB here!
|
||||
_ => unsafe {
|
||||
core::ptr::write_bytes(entry, 0, 1);
|
||||
break;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Now walk in order visited page tables
|
||||
for entry in page_tables.into_iter() {
|
||||
// Level not visited, skip.
|
||||
if entry.is_null() {
|
||||
continue;
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let children = &mut (*(*entry).ptr()).pt.childen;
|
||||
*children -= 1; // Decrease children count
|
||||
|
||||
// If there are no children, deallocate.
|
||||
if *children == 0 {
|
||||
let _ = Box::from_raw((*entry).ptr() as *mut PageTable);
|
||||
|
||||
// Zero visited entry
|
||||
core::ptr::write_bytes(entry, 0, 1);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Error mapping
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum MapError {
|
||||
/// Entry was already mapped
|
||||
AlreadyMapped,
|
||||
/// When walking a page entry was
|
||||
/// encounterd.
|
||||
PageOnNode,
|
||||
}
|
||||
|
||||
impl_display!(for MapError => match {
|
||||
Self::AlreadyMapped => "There is already a page mapped on specified address";
|
||||
Self::PageOnNode => "There was a page mapped on the way instead of node";
|
||||
});
|
||||
|
||||
/// There was no entry in page table to unmap
|
||||
///
|
||||
/// No worry, don't panic, nothing bad has happened,
|
||||
/// but if you are 120% sure there should be something,
|
||||
/// double-check your addresses.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct NothingToUnmap;
|
||||
impl_display!(for NothingToUnmap => "There is no entry to unmap");
|
|
@ -1,282 +0,0 @@
|
|||
//! Platform independent, software paged memory implementation
|
||||
|
||||
pub mod icache;
|
||||
pub mod lookup;
|
||||
pub mod paging;
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
pub mod mapping;
|
||||
|
||||
use {
|
||||
super::{addr::Address, LoadError, Memory, MemoryAccessReason, StoreError},
|
||||
core::mem::size_of,
|
||||
icache::ICache,
|
||||
lookup::{AddrPageLookupError, AddrPageLookupOk, AddrPageLookuper},
|
||||
paging::{PageTable, Permission},
|
||||
};
|
||||
|
||||
/// HoleyBytes software paged memory
|
||||
///
|
||||
/// - `OUT_PROG_EXEC`: set to `false` to disable executing program
|
||||
/// not contained in initially provided program, even the pages
|
||||
/// are executable
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SoftPagedMem<'p, PfH, const OUT_PROG_EXEC: bool = true> {
|
||||
/// Root page table
|
||||
pub root_pt: *mut PageTable,
|
||||
/// Page fault handler
|
||||
pub pf_handler: PfH,
|
||||
/// Program memory segment
|
||||
pub program: &'p [u8],
|
||||
/// Program instruction cache
|
||||
pub icache: ICache,
|
||||
}
|
||||
|
||||
impl<'p, PfH: HandlePageFault, const OUT_PROG_EXEC: bool> Memory
|
||||
for SoftPagedMem<'p, PfH, OUT_PROG_EXEC>
|
||||
{
|
||||
/// Load value from an address
|
||||
///
|
||||
/// # Safety
|
||||
/// Applies same conditions as for [`core::ptr::copy_nonoverlapping`]
|
||||
unsafe fn load(
|
||||
&mut self,
|
||||
addr: Address,
|
||||
target: *mut u8,
|
||||
count: usize,
|
||||
) -> Result<(), LoadError> {
|
||||
self.memory_access(
|
||||
MemoryAccessReason::Load,
|
||||
addr,
|
||||
target,
|
||||
count,
|
||||
perm_check::readable,
|
||||
|src, dst, count| unsafe { core::ptr::copy_nonoverlapping(src, dst, count) },
|
||||
)
|
||||
.map_err(LoadError)
|
||||
}
|
||||
|
||||
/// Store value to an address
|
||||
///
|
||||
/// # Safety
|
||||
/// Applies same conditions as for [`core::ptr::copy_nonoverlapping`]
|
||||
unsafe fn store(
|
||||
&mut self,
|
||||
addr: Address,
|
||||
source: *const u8,
|
||||
count: usize,
|
||||
) -> Result<(), StoreError> {
|
||||
self.memory_access(
|
||||
MemoryAccessReason::Store,
|
||||
addr,
|
||||
source.cast_mut(),
|
||||
count,
|
||||
perm_check::writable,
|
||||
|dst, src, count| unsafe { core::ptr::copy_nonoverlapping(src, dst, count) },
|
||||
)
|
||||
.map_err(StoreError)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
unsafe fn prog_read<T>(&mut self, addr: Address) -> T {
|
||||
if OUT_PROG_EXEC && addr.truncate_usize() > self.program.len() {
|
||||
return unsafe { self.icache.fetch::<T>(addr, self.root_pt) }
|
||||
.unwrap_or_else(|| unsafe { core::mem::zeroed() });
|
||||
}
|
||||
|
||||
let addr = addr.truncate_usize();
|
||||
self.program
|
||||
.get(addr..addr + size_of::<T>())
|
||||
.map(|x| unsafe { x.as_ptr().cast::<T>().read() })
|
||||
.unwrap_or_else(|| unsafe { core::mem::zeroed() })
|
||||
}
|
||||
}
|
||||
|
||||
impl<'p, PfH: HandlePageFault, const OUT_PROG_EXEC: bool> SoftPagedMem<'p, PfH, OUT_PROG_EXEC> {
|
||||
// Everyone behold, the holy function, the god of HBVM memory accesses!
|
||||
|
||||
/// Split address to pages, check their permissions and feed pointers with offset
|
||||
/// to a specified function.
|
||||
///
|
||||
/// If page is not found, execute page fault trap handler.
|
||||
#[allow(clippy::too_many_arguments)] // Silence peasant
|
||||
fn memory_access(
|
||||
&mut self,
|
||||
reason: MemoryAccessReason,
|
||||
src: Address,
|
||||
mut dst: *mut u8,
|
||||
len: usize,
|
||||
permission_check: fn(Permission) -> bool,
|
||||
action: fn(*mut u8, *mut u8, usize),
|
||||
) -> Result<(), Address> {
|
||||
// Memory load from program section
|
||||
let (src, len) = if src.truncate_usize() < self.program.len() as _ {
|
||||
// Allow only loads
|
||||
if reason != MemoryAccessReason::Load {
|
||||
return Err(src);
|
||||
}
|
||||
|
||||
// Determine how much data to copy from here
|
||||
let to_copy = len.clamp(0, self.program.len().saturating_sub(src.truncate_usize()));
|
||||
|
||||
// Perform action
|
||||
action(
|
||||
unsafe { self.program.as_ptr().add(src.truncate_usize()).cast_mut() },
|
||||
dst,
|
||||
to_copy,
|
||||
);
|
||||
|
||||
// Return shifted from what we've already copied
|
||||
(
|
||||
src.saturating_add(to_copy as u64),
|
||||
len.saturating_sub(to_copy),
|
||||
)
|
||||
} else {
|
||||
(src, len) // Nothing weird!
|
||||
};
|
||||
|
||||
// Nothing to copy? Don't bother doing anything, bail.
|
||||
if len == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Create new splitter
|
||||
let mut pspl = AddrPageLookuper::new(src, len, self.root_pt);
|
||||
loop {
|
||||
match pspl.next() {
|
||||
// Page is found
|
||||
Some(Ok(AddrPageLookupOk {
|
||||
vaddr,
|
||||
ptr,
|
||||
size,
|
||||
perm,
|
||||
})) => {
|
||||
if !permission_check(perm) {
|
||||
return Err(vaddr);
|
||||
}
|
||||
|
||||
// Perform specified memory action and bump destination pointer
|
||||
action(ptr, dst, size);
|
||||
dst = unsafe { dst.add(size) };
|
||||
}
|
||||
// No page found
|
||||
Some(Err(AddrPageLookupError { addr, size })) => {
|
||||
// Attempt to execute page fault handler
|
||||
if self.pf_handler.page_fault(
|
||||
reason,
|
||||
unsafe { &mut *self.root_pt },
|
||||
addr,
|
||||
size,
|
||||
dst,
|
||||
) {
|
||||
// Shift the splitter address
|
||||
pspl.bump(size);
|
||||
|
||||
// Bump dst pointer
|
||||
dst = unsafe { dst.add(size as _) };
|
||||
} else {
|
||||
return Err(addr); // Unhandleable, VM will yield.
|
||||
}
|
||||
}
|
||||
// No remaining pages, we are done!
|
||||
None => return Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract index in page table on specified level
|
||||
///
|
||||
/// The level shall not be larger than 4, otherwise
|
||||
/// the output of the function is unspecified (yes, it can also panic :)
|
||||
pub fn addr_extract_index(addr: Address, lvl: u8) -> usize {
|
||||
debug_assert!(lvl <= 4);
|
||||
let addr = addr.get();
|
||||
usize::try_from((addr >> (lvl * 8 + 12)) & ((1 << 8) - 1)).expect("?conradluget a better CPU")
|
||||
}
|
||||
|
||||
/// Page size
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum PageSize {
|
||||
/// 4 KiB page (on level 0)
|
||||
Size4K = 4096,
|
||||
|
||||
/// 2 MiB page (on level 1)
|
||||
Size2M = 1024 * 1024 * 2,
|
||||
|
||||
/// 1 GiB page (on level 2)
|
||||
Size1G = 1024 * 1024 * 1024,
|
||||
}
|
||||
|
||||
impl PageSize {
|
||||
/// Convert page table level to size of page
|
||||
const fn from_lvl(lvl: u8) -> Option<Self> {
|
||||
match lvl {
|
||||
0 => Some(PageSize::Size4K),
|
||||
1 => Some(PageSize::Size2M),
|
||||
2 => Some(PageSize::Size1G),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl core::ops::Add<PageSize> for Address {
|
||||
type Output = Self;
|
||||
|
||||
#[inline(always)]
|
||||
fn add(self, rhs: PageSize) -> Self::Output {
|
||||
self + (rhs as u64)
|
||||
}
|
||||
}
|
||||
|
||||
impl core::ops::AddAssign<PageSize> for Address {
|
||||
#[inline(always)]
|
||||
fn add_assign(&mut self, rhs: PageSize) {
|
||||
*self = Self::new(self.get().wrapping_add(rhs as u64));
|
||||
}
|
||||
}
|
||||
|
||||
/// Permisison checks
|
||||
pub mod perm_check {
|
||||
use super::paging::Permission;
|
||||
|
||||
/// Page is readable
|
||||
#[inline(always)]
|
||||
pub const fn readable(perm: Permission) -> bool {
|
||||
matches!(
|
||||
perm,
|
||||
Permission::Readonly | Permission::Write | Permission::Exec
|
||||
)
|
||||
}
|
||||
|
||||
/// Page is writable
|
||||
#[inline(always)]
|
||||
pub const fn writable(perm: Permission) -> bool {
|
||||
matches!(perm, Permission::Write)
|
||||
}
|
||||
|
||||
/// Page is executable
|
||||
#[inline(always)]
|
||||
pub const fn executable(perm: Permission) -> bool {
|
||||
matches!(perm, Permission::Exec)
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle VM traps
|
||||
pub trait HandlePageFault {
|
||||
/// Handle page fault
|
||||
///
|
||||
/// Return true if handling was sucessful,
|
||||
/// otherwise the program will be interrupted and will
|
||||
/// yield an error.
|
||||
fn page_fault(
|
||||
&mut self,
|
||||
reason: MemoryAccessReason,
|
||||
pagetable: &mut PageTable,
|
||||
vaddr: Address,
|
||||
size: PageSize,
|
||||
dataptr: *mut u8,
|
||||
) -> bool
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
|
@ -1,86 +0,0 @@
|
|||
//! Page table and associated structures implementation
|
||||
|
||||
use core::{fmt::Debug, mem::MaybeUninit};
|
||||
|
||||
/// Page entry permission
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
pub enum Permission {
|
||||
/// No page present
|
||||
#[default]
|
||||
Empty,
|
||||
/// Points to another pagetable
|
||||
Node,
|
||||
/// Page is read only
|
||||
Readonly,
|
||||
/// Page is readable and writable
|
||||
Write,
|
||||
/// Page is readable and executable
|
||||
Exec,
|
||||
}
|
||||
|
||||
/// Page table entry
|
||||
#[derive(Clone, Copy, Default, PartialEq, Eq)]
|
||||
pub struct PtEntry(u64);
|
||||
impl PtEntry {
|
||||
/// Create new
|
||||
///
|
||||
/// # Safety
|
||||
/// - `ptr` has to point to valid data and shall not be deallocated
|
||||
/// troughout the entry lifetime
|
||||
#[inline]
|
||||
pub unsafe fn new(ptr: *mut PtPointedData, permission: Permission) -> Self {
|
||||
Self(ptr as u64 | permission as u64)
|
||||
}
|
||||
|
||||
/// Get permission
|
||||
#[inline]
|
||||
pub fn permission(&self) -> Permission {
|
||||
unsafe { core::mem::transmute(self.0 as u8 & 0b111) }
|
||||
}
|
||||
|
||||
/// Get pointer to the data (leaf) or next page table (node)
|
||||
#[inline]
|
||||
pub fn ptr(&self) -> *mut PtPointedData {
|
||||
(self.0 & !((1 << 12) - 1)) as _
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for PtEntry {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
f.debug_struct("PtEntry")
|
||||
.field("ptr", &self.ptr())
|
||||
.field("permission", &self.permission())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// Page table
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[repr(align(4096))]
|
||||
pub struct PageTable {
|
||||
/// How much entries are in use
|
||||
pub childen: u8,
|
||||
/// Entries
|
||||
pub table: [PtEntry; 256],
|
||||
}
|
||||
|
||||
impl Default for PageTable {
|
||||
fn default() -> Self {
|
||||
// SAFETY: It's fine, zeroed page table entry is valid (= empty)
|
||||
Self {
|
||||
childen: 0,
|
||||
table: unsafe { MaybeUninit::zeroed().assume_init() },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Data page table entry can possibly point to
|
||||
#[derive(Clone, Copy)]
|
||||
#[repr(C, align(4096))]
|
||||
pub union PtPointedData {
|
||||
/// Node - next page table
|
||||
pub pt: PageTable,
|
||||
/// Leaf
|
||||
pub page: u8,
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
macro_rules! impl_display {
|
||||
(for $ty:ty => $(|$selfty:pat_param|)? $fmt:literal $(, $($param:expr),+)? $(,)?) => {
|
||||
impl ::core::fmt::Display for $ty {
|
||||
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
|
||||
$(let $selfty = self;)?
|
||||
write!(f, $fmt, $($param),*)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
(for $ty:ty => $str:literal) => {
|
||||
impl ::core::fmt::Display for $ty {
|
||||
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
|
||||
f.write_str($str)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
(for $ty:ty => match {$(
|
||||
$bind:pat => $($const:ident)? $fmt:literal $(,$($params:tt)*)?;
|
||||
)*}) => {
|
||||
impl ::core::fmt::Display for $ty {
|
||||
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
|
||||
match self {
|
||||
$(
|
||||
$bind => $crate::utils::internal::impl_display_match_fragment!($($const,)? f, $fmt $(, $($params)*)?)
|
||||
),*
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub(crate) mod internal {
|
||||
macro_rules! impl_display_match_fragment {
|
||||
(const, $f:expr, $lit:literal) => {
|
||||
$f.write_str($lit)
|
||||
};
|
||||
|
||||
($f:expr, $fmt:literal $(, $($params:tt)*)?) => {
|
||||
write!($f, $fmt, $($($params)*)?)
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use impl_display_match_fragment;
|
||||
}
|
||||
|
||||
macro_rules! static_assert(($expr:expr $(,)?) => {
|
||||
const _: [(); !$expr as usize] = [];
|
||||
});
|
||||
|
||||
pub(crate) use {impl_display, static_assert};
|
|
@ -0,0 +1,64 @@
|
|||
//! Validate if program is sound to execute
|
||||
|
||||
/// Program validation error kind
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ErrorKind {
|
||||
/// Unknown opcode
|
||||
InvalidInstruction,
|
||||
/// VM doesn't implement this valid opcode
|
||||
Unimplemented,
|
||||
/// Attempted to copy over register boundary
|
||||
RegisterArrayOverflow,
|
||||
}
|
||||
|
||||
/// Error
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Error {
|
||||
/// Kind
|
||||
pub kind: ErrorKind,
|
||||
/// Location in bytecode
|
||||
pub index: usize,
|
||||
}
|
||||
|
||||
/// Perform bytecode validation. If it passes, the program should be
|
||||
/// sound to execute.
|
||||
pub fn validate(mut program: &[u8]) -> Result<(), Error> {
|
||||
use hbbytecode::opcode::*;
|
||||
|
||||
let start = program;
|
||||
loop {
|
||||
// Match on instruction types and perform necessary checks
|
||||
program = match program {
|
||||
[] => return Ok(()),
|
||||
[LD..=ST, reg, _, _, _, _, _, _, _, _, _, count, ..]
|
||||
if usize::from(*reg) * 8 + usize::from(*count) > 2048 =>
|
||||
{
|
||||
return Err(Error {
|
||||
kind: ErrorKind::RegisterArrayOverflow,
|
||||
index: (program.as_ptr() as usize) - (start.as_ptr() as usize),
|
||||
})
|
||||
}
|
||||
[BRC, src, dst, count, ..]
|
||||
if src.checked_add(*count).is_none() || dst.checked_add(*count).is_none() =>
|
||||
{
|
||||
return Err(Error {
|
||||
kind: ErrorKind::RegisterArrayOverflow,
|
||||
index: (program.as_ptr() as usize) - (start.as_ptr() as usize),
|
||||
})
|
||||
}
|
||||
[NOP | ECALL, rest @ ..]
|
||||
| [DIR | DIRF, _, _, _, _, rest @ ..]
|
||||
| [ADD..=CMPU | BRC | ADDF..=MULF, _, _, _, rest @ ..]
|
||||
| [NEG..=NOT | CP..=SWA | NEGF..=FTI, _, _, rest @ ..]
|
||||
| [LI | JMP, _, _, _, _, _, _, _, _, _, rest @ ..]
|
||||
| [ADDI..=CMPUI | BMC | JEQ..=JGTU | ADDFI..=MULFI, _, _, _, _, _, _, _, _, _, _, rest @ ..]
|
||||
| [LD..=ST, _, _, _, _, _, _, _, _, _, _, _, _, rest @ ..] => rest,
|
||||
_ => {
|
||||
return Err(Error {
|
||||
kind: ErrorKind::InvalidInstruction,
|
||||
index: (program.as_ptr() as usize) - (start.as_ptr() as usize),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,88 +0,0 @@
|
|||
//! HoleyBytes register value definition
|
||||
|
||||
use crate::utils::static_assert;
|
||||
|
||||
/// Define [`Value`] »union« (it's fake)
|
||||
///
|
||||
/// # Safety
|
||||
/// Its variants have to be sound to byte-reinterpretate
|
||||
/// between each other. Otherwise the behaviour is undefined.
|
||||
macro_rules! value_def {
|
||||
($($ty:ident),* $(,)?) => {
|
||||
/// HBVM register value
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(transparent)]
|
||||
pub struct Value(pub u64);
|
||||
|
||||
$(
|
||||
impl From<$ty> for Value {
|
||||
#[inline]
|
||||
fn from(value: $ty) -> Self {
|
||||
let mut new = core::mem::MaybeUninit::<u64>::zeroed();
|
||||
unsafe {
|
||||
new.as_mut_ptr().cast::<$ty>().write(value);
|
||||
Self(new.assume_init())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static_assert!(core::mem::size_of::<$ty>() <= core::mem::size_of::<Value>());
|
||||
|
||||
impl private::Sealed for $ty {}
|
||||
unsafe impl ValueVariant for $ty {}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
impl Value {
|
||||
/// Byte reinterpret value to target variant
|
||||
#[inline]
|
||||
pub fn cast<V: ValueVariant>(self) -> V {
|
||||
unsafe { core::mem::transmute_copy(&self.0) }
|
||||
}
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
/// - N/A, not to be implemented manually
|
||||
pub unsafe trait ValueVariant: private::Sealed + Copy + Into<Value> {}
|
||||
impl private::Sealed for Value {}
|
||||
unsafe impl ValueVariant for Value {}
|
||||
|
||||
mod private {
|
||||
pub trait Sealed {}
|
||||
}
|
||||
|
||||
value_def!(u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
|
||||
static_assert!(core::mem::size_of::<Value>() == 8);
|
||||
|
||||
impl core::fmt::Debug for Value {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
// Print formatted as hexadecimal, unsigned integer
|
||||
write!(f, "{:x}", self.cast::<u64>())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait CheckedDivRem {
|
||||
fn checked_div(self, other: Self) -> Option<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
fn checked_rem(self, other: Self) -> Option<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
macro_rules! impl_checked_div_rem {
|
||||
($($ty:ty),* $(,)?) => {
|
||||
$(impl CheckedDivRem for $ty {
|
||||
#[inline(always)]
|
||||
fn checked_div(self, another: Self) -> Option<Self>
|
||||
{ self.checked_div(another) }
|
||||
|
||||
#[inline(always)]
|
||||
fn checked_rem(self, another: Self) -> Option<Self>
|
||||
{ self.checked_rem(another) }
|
||||
})*
|
||||
};
|
||||
}
|
||||
|
||||
impl_checked_div_rem!(u8, u16, u32, u64, i8, i16, i32, i64);
|
|
@ -0,0 +1,430 @@
|
|||
//! Program memory implementation
|
||||
|
||||
pub mod paging;
|
||||
|
||||
use {
|
||||
self::paging::{PageTable, Permission, PtEntry},
|
||||
super::{trap::HandleTrap, VmRunError},
|
||||
alloc::boxed::Box,
|
||||
core::mem::MaybeUninit,
|
||||
derive_more::Display,
|
||||
};
|
||||
|
||||
/// HoleyBytes virtual memory
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Memory {
|
||||
/// Root page table
|
||||
root_pt: *mut PageTable,
|
||||
}
|
||||
|
||||
impl Default for Memory {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
root_pt: Box::into_raw(Box::default()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Memory {
|
||||
fn drop(&mut self) {
|
||||
let _ = unsafe { Box::from_raw(self.root_pt) };
|
||||
}
|
||||
}
|
||||
|
||||
impl Memory {
|
||||
// HACK: Just for allocation testing, will be removed when proper memory interfaces
|
||||
// implemented.
|
||||
pub fn insert_test_page(&mut self) {
|
||||
unsafe {
|
||||
let mut entry = PtEntry::new(
|
||||
{
|
||||
let layout = alloc::alloc::Layout::from_size_align_unchecked(4096, 4096);
|
||||
let ptr = alloc::alloc::alloc_zeroed(layout);
|
||||
if ptr.is_null() {
|
||||
alloc::alloc::handle_alloc_error(layout);
|
||||
}
|
||||
|
||||
core::ptr::write_bytes(ptr, 69, 10);
|
||||
ptr.cast()
|
||||
},
|
||||
Permission::Write,
|
||||
);
|
||||
|
||||
for _ in 0..4 {
|
||||
let mut pt = Box::<PageTable>::default();
|
||||
pt[0] = entry;
|
||||
entry = PtEntry::new(Box::into_raw(pt) as _, Permission::Node);
|
||||
}
|
||||
|
||||
(*self.root_pt)[0] = entry;
|
||||
}
|
||||
}
|
||||
|
||||
/// Load value from an address
|
||||
///
|
||||
/// # Safety
|
||||
/// Applies same conditions as for [`core::ptr::copy_nonoverlapping`]
|
||||
pub unsafe fn load(
|
||||
&mut self,
|
||||
addr: u64,
|
||||
target: *mut u8,
|
||||
count: usize,
|
||||
traph: &mut impl HandleTrap,
|
||||
) -> Result<(), LoadError> {
|
||||
self.memory_access(
|
||||
MemoryAccessReason::Load,
|
||||
addr,
|
||||
target,
|
||||
count,
|
||||
|perm| {
|
||||
matches!(
|
||||
perm,
|
||||
Permission::Readonly | Permission::Write | Permission::Exec
|
||||
)
|
||||
},
|
||||
|src, dst, count| core::ptr::copy_nonoverlapping(src, dst, count),
|
||||
traph,
|
||||
)
|
||||
.map_err(LoadError)
|
||||
}
|
||||
|
||||
/// Store value to an address
|
||||
///
|
||||
/// # Safety
|
||||
/// Applies same conditions as for [`core::ptr::copy_nonoverlapping`]
|
||||
pub unsafe fn store(
|
||||
&mut self,
|
||||
addr: u64,
|
||||
source: *const u8,
|
||||
count: usize,
|
||||
traph: &mut impl HandleTrap,
|
||||
) -> Result<(), StoreError> {
|
||||
self.memory_access(
|
||||
MemoryAccessReason::Store,
|
||||
addr,
|
||||
source.cast_mut(),
|
||||
count,
|
||||
|perm| perm == Permission::Write,
|
||||
|dst, src, count| core::ptr::copy_nonoverlapping(src, dst, count),
|
||||
traph,
|
||||
)
|
||||
.map_err(StoreError)
|
||||
}
|
||||
|
||||
/// Copy a block of memory
|
||||
///
|
||||
/// # Safety
|
||||
/// - Same as for [`Self::load`] and [`Self::store`]
|
||||
/// - Your faith in the gods of UB
|
||||
/// - Addr-san claims it's fine but who knows is she isn't lying :ferrisSus:
|
||||
pub unsafe fn block_copy(
|
||||
&mut self,
|
||||
src: u64,
|
||||
dst: u64,
|
||||
count: usize,
|
||||
traph: &mut impl HandleTrap,
|
||||
) -> Result<(), BlkCopyError> {
|
||||
// Yea, i know it is possible to do this more efficiently, but I am too lazy.
|
||||
|
||||
const STACK_BUFFER_SIZE: usize = 512;
|
||||
|
||||
// Decide if to use stack-allocated buffer or to heap allocate
|
||||
// Deallocation is again decided on size at the end of the function
|
||||
let mut buf = MaybeUninit::<[u8; STACK_BUFFER_SIZE]>::uninit();
|
||||
let buf = if count <= STACK_BUFFER_SIZE {
|
||||
buf.as_mut_ptr().cast()
|
||||
} else {
|
||||
unsafe {
|
||||
let layout = core::alloc::Layout::from_size_align_unchecked(count, 1);
|
||||
let ptr = alloc::alloc::alloc(layout);
|
||||
if ptr.is_null() {
|
||||
alloc::alloc::handle_alloc_error(layout);
|
||||
}
|
||||
|
||||
ptr
|
||||
}
|
||||
};
|
||||
|
||||
// Perform memory block transfer
|
||||
let status = (|| {
|
||||
// Load to buffer
|
||||
self.memory_access(
|
||||
MemoryAccessReason::Load,
|
||||
src,
|
||||
buf,
|
||||
count,
|
||||
|perm| {
|
||||
matches!(
|
||||
perm,
|
||||
Permission::Readonly | Permission::Write | Permission::Exec
|
||||
)
|
||||
},
|
||||
|src, dst, count| core::ptr::copy(src, dst, count),
|
||||
traph,
|
||||
)
|
||||
.map_err(|addr| BlkCopyError {
|
||||
access_reason: MemoryAccessReason::Load,
|
||||
addr,
|
||||
})?;
|
||||
|
||||
// Store from buffer
|
||||
self.memory_access(
|
||||
MemoryAccessReason::Store,
|
||||
dst,
|
||||
buf,
|
||||
count,
|
||||
|perm| perm == Permission::Write,
|
||||
|dst, src, count| core::ptr::copy(src, dst, count),
|
||||
traph,
|
||||
)
|
||||
.map_err(|addr| BlkCopyError {
|
||||
access_reason: MemoryAccessReason::Store,
|
||||
addr,
|
||||
})?;
|
||||
|
||||
Ok::<_, BlkCopyError>(())
|
||||
})();
|
||||
|
||||
// Deallocate if used heap-allocated array
|
||||
if count > STACK_BUFFER_SIZE {
|
||||
alloc::alloc::dealloc(
|
||||
buf,
|
||||
core::alloc::Layout::from_size_align_unchecked(count, 1),
|
||||
);
|
||||
}
|
||||
|
||||
status
|
||||
}
|
||||
|
||||
/// Split address to pages, check their permissions and feed pointers with offset
|
||||
/// to a specified function.
|
||||
///
|
||||
/// If page is not found, execute page fault trap handler.
|
||||
#[allow(clippy::too_many_arguments)] // Silence peasant
|
||||
fn memory_access(
|
||||
&mut self,
|
||||
reason: MemoryAccessReason,
|
||||
src: u64,
|
||||
mut dst: *mut u8,
|
||||
len: usize,
|
||||
permission_check: fn(Permission) -> bool,
|
||||
action: fn(*mut u8, *mut u8, usize),
|
||||
traph: &mut impl HandleTrap,
|
||||
) -> Result<(), u64> {
|
||||
let mut pspl = AddrSplitter::new(src, len, self.root_pt);
|
||||
loop {
|
||||
match pspl.next() {
|
||||
// Page found
|
||||
Some(Ok(AddrSplitOk {
|
||||
vaddr,
|
||||
ptr,
|
||||
size,
|
||||
perm,
|
||||
})) => {
|
||||
if !permission_check(perm) {
|
||||
return Err(vaddr);
|
||||
}
|
||||
|
||||
// Perform memory action and bump dst pointer
|
||||
action(ptr, dst, size);
|
||||
dst = unsafe { dst.add(size) };
|
||||
}
|
||||
Some(Err(AddrSplitError { addr, size })) => {
|
||||
// Execute page fault handler
|
||||
if traph.page_fault(reason, self, addr, size, dst) {
|
||||
// Shift the splitter address
|
||||
pspl.bump(size);
|
||||
|
||||
// Bump dst pointer
|
||||
dst = unsafe { dst.add(size as _) };
|
||||
} else {
|
||||
return Err(addr); // Unhandleable
|
||||
}
|
||||
}
|
||||
None => return Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Result from address split
|
||||
struct AddrSplitOk {
|
||||
/// Virtual address
|
||||
vaddr: u64,
|
||||
|
||||
/// Pointer to the start for perform operation
|
||||
ptr: *mut u8,
|
||||
|
||||
/// Size to the end of page / end of desired size
|
||||
size: usize,
|
||||
|
||||
/// Page permission
|
||||
perm: Permission,
|
||||
}
|
||||
|
||||
struct AddrSplitError {
|
||||
/// Address of failure
|
||||
addr: u64,
|
||||
|
||||
/// Requested page size
|
||||
size: PageSize,
|
||||
}
|
||||
|
||||
/// Address splitter into pages
|
||||
struct AddrSplitter {
|
||||
/// Current address
|
||||
addr: u64,
|
||||
|
||||
/// Size left
|
||||
size: usize,
|
||||
|
||||
/// Page table
|
||||
pagetable: *const PageTable,
|
||||
}
|
||||
|
||||
impl AddrSplitter {
|
||||
/// Create a new page splitter
|
||||
pub const fn new(addr: u64, size: usize, pagetable: *const PageTable) -> Self {
|
||||
Self {
|
||||
addr,
|
||||
size,
|
||||
pagetable,
|
||||
}
|
||||
}
|
||||
|
||||
/// Bump address by size X
|
||||
fn bump(&mut self, page_size: PageSize) {
|
||||
self.addr += page_size as u64;
|
||||
self.size = self.size.saturating_sub(page_size as _);
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for AddrSplitter {
|
||||
type Item = Result<AddrSplitOk, AddrSplitError>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
// The end, everything is fine
|
||||
if self.size == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let (base, perm, size, offset) = 'a: {
|
||||
let mut current_pt = self.pagetable;
|
||||
|
||||
// Walk the page table
|
||||
for lvl in (0..5).rev() {
|
||||
// Get an entry
|
||||
unsafe {
|
||||
let entry = (*current_pt).get_unchecked(
|
||||
usize::try_from((self.addr >> (lvl * 9 + 12)) & ((1 << 9) - 1))
|
||||
.expect("?conradluget a better CPU"),
|
||||
);
|
||||
|
||||
let ptr = entry.ptr();
|
||||
match entry.permission() {
|
||||
// No page → page fault
|
||||
Permission::Empty => {
|
||||
return Some(Err(AddrSplitError {
|
||||
addr: self.addr,
|
||||
size: PageSize::from_lvl(lvl)?,
|
||||
}))
|
||||
}
|
||||
|
||||
// Node → proceed waking
|
||||
Permission::Node => current_pt = ptr as _,
|
||||
|
||||
// Leaft → return relevant data
|
||||
perm => {
|
||||
break 'a (
|
||||
// Pointer in host memory
|
||||
ptr as *mut u8,
|
||||
perm,
|
||||
PageSize::from_lvl(lvl)?,
|
||||
// In-page offset
|
||||
self.addr as usize & ((1 << (lvl * 9 + 12)) - 1),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return None; // Reached the end (should not happen)
|
||||
};
|
||||
|
||||
// Get available byte count in the selected page with offset
|
||||
let avail = (size as usize - offset).clamp(0, self.size);
|
||||
self.bump(size);
|
||||
|
||||
Some(Ok(AddrSplitOk {
|
||||
vaddr: self.addr,
|
||||
ptr: unsafe { base.add(offset) }, // Return pointer to the start of region
|
||||
size: avail,
|
||||
perm,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
/// Page size
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum PageSize {
|
||||
/// 4 KiB page (on level 0)
|
||||
Size4K = 4096,
|
||||
|
||||
/// 2 MiB page (on level 1)
|
||||
Size2M = 1024 * 1024 * 2,
|
||||
|
||||
/// 1 GiB page (on level 2)
|
||||
Size1G = 1024 * 1024 * 1024,
|
||||
}
|
||||
|
||||
impl PageSize {
|
||||
/// Convert page table level to size of page
|
||||
fn from_lvl(lvl: u8) -> Option<Self> {
|
||||
match lvl {
|
||||
0 => Some(PageSize::Size4K),
|
||||
1 => Some(PageSize::Size2M),
|
||||
2 => Some(PageSize::Size1G),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Unhandled load access trap
|
||||
#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)]
|
||||
pub struct LoadError(u64);
|
||||
|
||||
/// Unhandled store access trap
|
||||
#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)]
|
||||
pub struct StoreError(u64);
|
||||
|
||||
#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)]
|
||||
pub enum MemoryAccessReason {
|
||||
Load,
|
||||
Store,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct BlkCopyError {
|
||||
access_reason: MemoryAccessReason,
|
||||
addr: u64,
|
||||
}
|
||||
|
||||
impl From<BlkCopyError> for VmRunError {
|
||||
fn from(value: BlkCopyError) -> Self {
|
||||
match value.access_reason {
|
||||
MemoryAccessReason::Load => Self::LoadAccessEx(value.addr),
|
||||
MemoryAccessReason::Store => Self::StoreAccessEx(value.addr),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LoadError> for VmRunError {
|
||||
fn from(value: LoadError) -> Self {
|
||||
Self::LoadAccessEx(value.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<StoreError> for VmRunError {
|
||||
fn from(value: StoreError) -> Self {
|
||||
Self::StoreAccessEx(value.0)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,152 @@
|
|||
//! Page table and associated structures implementation
|
||||
|
||||
use {
|
||||
core::{
|
||||
fmt::Debug,
|
||||
mem::MaybeUninit,
|
||||
ops::{Index, IndexMut},
|
||||
slice::SliceIndex,
|
||||
},
|
||||
delegate::delegate,
|
||||
};
|
||||
|
||||
/// Page entry permission
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
pub enum Permission {
|
||||
/// No page present
|
||||
#[default]
|
||||
Empty,
|
||||
/// Points to another pagetable
|
||||
Node,
|
||||
/// Page is read only
|
||||
Readonly,
|
||||
/// Page is readable and writable
|
||||
Write,
|
||||
/// Page is readable and executable
|
||||
Exec,
|
||||
}
|
||||
|
||||
/// Page table entry
|
||||
#[derive(Clone, Copy, Default, PartialEq, Eq)]
|
||||
pub struct PtEntry(u64);
|
||||
impl PtEntry {
|
||||
/// Create new
|
||||
///
|
||||
/// # Safety
|
||||
/// - `ptr` has to point to valid data and shall not be deallocated
|
||||
/// troughout the entry lifetime
|
||||
#[inline]
|
||||
pub unsafe fn new(ptr: *mut PtPointedData, permission: Permission) -> Self {
|
||||
Self(ptr as u64 | permission as u64)
|
||||
}
|
||||
|
||||
/// Get permission
|
||||
#[inline]
|
||||
pub fn permission(&self) -> Permission {
|
||||
unsafe { core::mem::transmute(self.0 as u8 & 0b111) }
|
||||
}
|
||||
|
||||
/// Get pointer to the data (leaf) or next page table (node)
|
||||
#[inline]
|
||||
pub fn ptr(&self) -> *mut PtPointedData {
|
||||
(self.0 & !((1 << 12) - 1)) as _
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for PtEntry {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
f.debug_struct("PtEntry")
|
||||
.field("ptr", &self.ptr())
|
||||
.field("permission", &self.permission())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// Page table
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[repr(align(4096))]
|
||||
pub struct PageTable([PtEntry; 512]);
|
||||
|
||||
impl PageTable {
|
||||
delegate!(to self.0 {
|
||||
/// Returns a reference to an element or subslice depending on the type of
|
||||
/// index.
|
||||
///
|
||||
/// - If given a position, returns a reference to the element at that
|
||||
/// position or `None` if out of bounds.
|
||||
/// - If given a range, returns the subslice corresponding to that range,
|
||||
/// or `None` if out of bounds.
|
||||
///
|
||||
pub fn get<I>(&self, ix: I) -> Option<&I::Output>
|
||||
where I: SliceIndex<[PtEntry]>;
|
||||
|
||||
/// Returns a mutable reference to an element or subslice depending on the
|
||||
/// type of index (see [`get`]) or `None` if the index is out of bounds.
|
||||
pub fn get_mut<I>(&mut self, ix: I) -> Option<&mut I::Output>
|
||||
where I: SliceIndex<[PtEntry]>;
|
||||
|
||||
/// Returns a reference to an element or subslice, without doing bounds
|
||||
/// checking.
|
||||
///
|
||||
/// For a safe alternative see [`get`].
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Calling this method with an out-of-bounds index is *[undefined behavior]*
|
||||
/// even if the resulting reference is not used.
|
||||
pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
|
||||
where I: SliceIndex<[PtEntry]>;
|
||||
|
||||
/// Returns a mutable reference to an element or subslice, without doing
|
||||
/// bounds checking.
|
||||
///
|
||||
/// For a safe alternative see [`get_mut`].
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Calling this method with an out-of-bounds index is *[undefined behavior]*
|
||||
/// even if the resulting reference is not used.
|
||||
pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
|
||||
where I: SliceIndex<[PtEntry]>;
|
||||
});
|
||||
}
|
||||
|
||||
impl<Idx> Index<Idx> for PageTable
|
||||
where
|
||||
Idx: SliceIndex<[PtEntry]>,
|
||||
{
|
||||
type Output = Idx::Output;
|
||||
|
||||
#[inline(always)]
|
||||
fn index(&self, index: Idx) -> &Self::Output {
|
||||
&self.0[index]
|
||||
}
|
||||
}
|
||||
|
||||
impl<Idx> IndexMut<Idx> for PageTable
|
||||
where
|
||||
Idx: SliceIndex<[PtEntry]>,
|
||||
{
|
||||
#[inline(always)]
|
||||
fn index_mut(&mut self, index: Idx) -> &mut Self::Output {
|
||||
&mut self.0[index]
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for PageTable {
|
||||
fn default() -> Self {
|
||||
// SAFETY: It's fine, zeroed page table entry is valid (= empty)
|
||||
Self(unsafe { MaybeUninit::zeroed().assume_init() })
|
||||
}
|
||||
}
|
||||
|
||||
/// Data page table entry can possibly point to
|
||||
#[derive(Clone, Copy)]
|
||||
#[repr(C, align(4096))]
|
||||
pub union PtPointedData {
|
||||
/// Node - next page table
|
||||
pub pt: PageTable,
|
||||
/// Leaf
|
||||
pub page: u8,
|
||||
}
|
|
@ -0,0 +1,373 @@
|
|||
//! HoleyBytes Virtual Machine
|
||||
//!
|
||||
//! All unsafe code here should be sound, if input bytecode passes validation.
|
||||
|
||||
// # General safety notice:
|
||||
// - Validation has to assure there is 256 registers (r0 - r255)
|
||||
// - Instructions have to be valid as specified (values and sizes)
|
||||
// - Mapped pages should be at least 4 KiB
|
||||
// - Yes, I am aware of the UB when jumping in-mid of instruction where
|
||||
// the read byte corresponds to an instruction whose lenght exceets the
|
||||
// program size. If you are (rightfully) worried about the UB, for now just
|
||||
// append your program with 11 zeroes.
|
||||
|
||||
use self::trap::HandleTrap;
|
||||
|
||||
pub mod mem;
|
||||
pub mod trap;
|
||||
pub mod value;
|
||||
|
||||
use {
|
||||
crate::validate,
|
||||
core::ops,
|
||||
hbbytecode::{OpParam, ParamBB, ParamBBB, ParamBBBB, ParamBBD, ParamBBDH, ParamBD},
|
||||
mem::Memory,
|
||||
static_assertions::assert_impl_one,
|
||||
value::Value,
|
||||
};
|
||||
|
||||
/// Extract a parameter from program
|
||||
macro_rules! param {
|
||||
($self:expr, $ty:ty) => {{
|
||||
assert_impl_one!($ty: OpParam);
|
||||
let data = $self
|
||||
.program
|
||||
.as_ptr()
|
||||
.add($self.pc + 1)
|
||||
.cast::<$ty>()
|
||||
.read();
|
||||
$self.pc += 1 + core::mem::size_of::<$ty>();
|
||||
data
|
||||
}};
|
||||
}
|
||||
|
||||
/// Perform binary operation `#0 ← #1 OP #2`
|
||||
macro_rules! binary_op {
|
||||
($self:expr, $ty:ident, $handler:expr) => {{
|
||||
let ParamBBB(tg, a0, a1) = param!($self, ParamBBB);
|
||||
$self.write_reg(
|
||||
tg,
|
||||
$handler(
|
||||
Value::$ty(&$self.read_reg(a0)),
|
||||
Value::$ty(&$self.read_reg(a1)),
|
||||
),
|
||||
);
|
||||
}};
|
||||
}
|
||||
|
||||
/// Perform binary operation with immediate `#0 ← #1 OP imm #2`
|
||||
macro_rules! binary_op_imm {
|
||||
($self:expr, $ty:ident, $handler:expr) => {{
|
||||
let ParamBBD(tg, a0, imm) = param!($self, ParamBBD);
|
||||
$self.write_reg(
|
||||
tg,
|
||||
$handler(Value::$ty(&$self.read_reg(a0)), Value::$ty(&imm.into())),
|
||||
);
|
||||
}};
|
||||
}
|
||||
|
||||
/// Jump at `#3` if ordering on `#0 <=> #1` is equal to expected
|
||||
macro_rules! cond_jump {
|
||||
($self:expr, $ty:ident, $expected:ident) => {{
|
||||
let ParamBBD(a0, a1, jt) = param!($self, ParamBBD);
|
||||
if core::cmp::Ord::cmp(&$self.read_reg(a0).as_u64(), &$self.read_reg(a1).as_u64())
|
||||
== core::cmp::Ordering::$expected
|
||||
{
|
||||
$self.pc = jt as usize;
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
/// HoleyBytes Virtual Machine
|
||||
pub struct Vm<'a, T, const TIMER_QUOTIENT: usize> {
|
||||
/// Holds 256 registers
|
||||
///
|
||||
/// Writing to register 0 is considered undefined behaviour
|
||||
/// in terms of HoleyBytes program execution
|
||||
pub registers: [Value; 256],
|
||||
|
||||
/// Memory implementation
|
||||
pub memory: Memory,
|
||||
|
||||
/// Trap handler
|
||||
pub traph: T,
|
||||
|
||||
// Program counter
|
||||
pc: usize,
|
||||
|
||||
/// Program
|
||||
program: &'a [u8],
|
||||
|
||||
/// Program timer
|
||||
timer: usize,
|
||||
}
|
||||
|
||||
impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
|
||||
/// Create a new VM with program and trap handler
|
||||
///
|
||||
/// # Safety
|
||||
/// Program code has to be validated
|
||||
pub unsafe fn new_unchecked(program: &'a [u8], traph: T) -> Self {
|
||||
Self {
|
||||
registers: [Value::from(0_u64); 256],
|
||||
memory: Default::default(),
|
||||
traph,
|
||||
pc: 0,
|
||||
program,
|
||||
timer: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new VM with program and trap handler only if it passes validation
|
||||
pub fn new_validated(program: &'a [u8], traph: T) -> Result<Self, validate::Error> {
|
||||
validate::validate(program)?;
|
||||
Ok(unsafe { Self::new_unchecked(program, traph) })
|
||||
}
|
||||
|
||||
/// Execute program
|
||||
///
|
||||
/// Program can return [`VmRunError`] if a trap handling failed
|
||||
pub fn run(&mut self) -> Result<VmRunOk, VmRunError> {
|
||||
use hbbytecode::opcode::*;
|
||||
loop {
|
||||
// Fetch instruction
|
||||
let Some(&opcode) = self.program.get(self.pc)
|
||||
else { return Ok(VmRunOk::End) };
|
||||
|
||||
// Big match
|
||||
unsafe {
|
||||
match opcode {
|
||||
NOP => param!(self, ()),
|
||||
ADD => binary_op!(self, as_u64, u64::wrapping_add),
|
||||
SUB => binary_op!(self, as_u64, u64::wrapping_sub),
|
||||
MUL => binary_op!(self, as_u64, u64::wrapping_mul),
|
||||
AND => binary_op!(self, as_u64, ops::BitAnd::bitand),
|
||||
OR => binary_op!(self, as_u64, ops::BitOr::bitor),
|
||||
XOR => binary_op!(self, as_u64, ops::BitXor::bitxor),
|
||||
SL => binary_op!(self, as_u64, ops::Shl::shl),
|
||||
SR => binary_op!(self, as_u64, ops::Shr::shr),
|
||||
SRS => binary_op!(self, as_i64, ops::Shr::shr),
|
||||
CMP => {
|
||||
let ParamBBB(tg, a0, a1) = param!(self, ParamBBB);
|
||||
self.write_reg(
|
||||
tg,
|
||||
self.read_reg(a0).as_i64().cmp(&self.read_reg(a1).as_i64()) as i64,
|
||||
);
|
||||
}
|
||||
CMPU => {
|
||||
let ParamBBB(tg, a0, a1) = param!(self, ParamBBB);
|
||||
self.write_reg(
|
||||
tg,
|
||||
self.read_reg(a0).as_u64().cmp(&self.read_reg(a1).as_u64()) as i64,
|
||||
);
|
||||
}
|
||||
NOT => {
|
||||
let param = param!(self, ParamBB);
|
||||
self.write_reg(param.0, !self.read_reg(param.1).as_u64());
|
||||
}
|
||||
NEG => {
|
||||
let param = param!(self, ParamBB);
|
||||
self.write_reg(
|
||||
param.0,
|
||||
match self.read_reg(param.1).as_u64() {
|
||||
0 => 1_u64,
|
||||
_ => 0,
|
||||
},
|
||||
);
|
||||
}
|
||||
DIR => {
|
||||
let ParamBBBB(dt, rt, a0, a1) = param!(self, ParamBBBB);
|
||||
let a0 = self.read_reg(a0).as_u64();
|
||||
let a1 = self.read_reg(a1).as_u64();
|
||||
self.write_reg(dt, a0.checked_div(a1).unwrap_or(u64::MAX));
|
||||
self.write_reg(rt, a0.checked_rem(a1).unwrap_or(u64::MAX));
|
||||
}
|
||||
ADDI => binary_op_imm!(self, as_u64, ops::Add::add),
|
||||
MULI => binary_op_imm!(self, as_u64, ops::Mul::mul),
|
||||
ANDI => binary_op_imm!(self, as_u64, ops::BitAnd::bitand),
|
||||
ORI => binary_op_imm!(self, as_u64, ops::BitOr::bitor),
|
||||
XORI => binary_op_imm!(self, as_u64, ops::BitXor::bitxor),
|
||||
SLI => binary_op_imm!(self, as_u64, ops::Shl::shl),
|
||||
SRI => binary_op_imm!(self, as_u64, ops::Shr::shr),
|
||||
SRSI => binary_op_imm!(self, as_i64, ops::Shr::shr),
|
||||
CMPI => {
|
||||
let ParamBBD(tg, a0, imm) = param!(self, ParamBBD);
|
||||
self.write_reg(
|
||||
tg,
|
||||
self.read_reg(a0).as_i64().cmp(&Value::from(imm).as_i64()) as i64,
|
||||
);
|
||||
}
|
||||
CMPUI => {
|
||||
let ParamBBD(tg, a0, imm) = param!(self, ParamBBD);
|
||||
self.write_reg(tg, self.read_reg(a0).as_u64().cmp(&imm) as i64);
|
||||
}
|
||||
CP => {
|
||||
let param = param!(self, ParamBB);
|
||||
self.write_reg(param.0, self.read_reg(param.1));
|
||||
}
|
||||
SWA => {
|
||||
let ParamBB(src, dst) = param!(self, ParamBB);
|
||||
if src + dst != 0 {
|
||||
core::ptr::swap(
|
||||
self.registers.get_unchecked_mut(usize::from(src)),
|
||||
self.registers.get_unchecked_mut(usize::from(dst)),
|
||||
);
|
||||
}
|
||||
}
|
||||
LI => {
|
||||
let param = param!(self, ParamBD);
|
||||
self.write_reg(param.0, param.1);
|
||||
}
|
||||
LD => {
|
||||
let ParamBBDH(dst, base, off, count) = param!(self, ParamBBDH);
|
||||
let n: usize = match dst {
|
||||
0 => 1,
|
||||
_ => 0,
|
||||
};
|
||||
|
||||
self.memory.load(
|
||||
self.read_reg(base).as_u64() + off + n as u64,
|
||||
self.registers.as_mut_ptr().add(usize::from(dst) + n).cast(),
|
||||
usize::from(count).saturating_sub(n),
|
||||
&mut self.traph,
|
||||
)?;
|
||||
}
|
||||
ST => {
|
||||
let ParamBBDH(dst, base, off, count) = param!(self, ParamBBDH);
|
||||
self.memory.store(
|
||||
self.read_reg(base).as_u64() + off,
|
||||
self.registers.as_ptr().add(usize::from(dst)).cast(),
|
||||
count.into(),
|
||||
&mut self.traph,
|
||||
)?;
|
||||
}
|
||||
BMC => {
|
||||
let ParamBBD(src, dst, count) = param!(self, ParamBBD);
|
||||
self.memory.block_copy(
|
||||
self.read_reg(src).as_u64(),
|
||||
self.read_reg(dst).as_u64(),
|
||||
count as _,
|
||||
&mut self.traph,
|
||||
)?;
|
||||
}
|
||||
BRC => {
|
||||
let ParamBBB(src, dst, count) = param!(self, ParamBBB);
|
||||
core::ptr::copy(
|
||||
self.registers.get_unchecked(usize::from(src)),
|
||||
self.registers.get_unchecked_mut(usize::from(dst)),
|
||||
usize::from(count * 8),
|
||||
);
|
||||
}
|
||||
JMP => {
|
||||
let ParamBD(reg, offset) = param!(self, ParamBD);
|
||||
self.pc = (self.read_reg(reg).as_u64() + offset) as usize;
|
||||
}
|
||||
JEQ => cond_jump!(self, int, Equal),
|
||||
JNE => {
|
||||
let ParamBBD(a0, a1, jt) = param!(self, ParamBBD);
|
||||
if self.read_reg(a0).as_u64() != self.read_reg(a1).as_u64() {
|
||||
self.pc = jt as usize;
|
||||
}
|
||||
}
|
||||
JLT => cond_jump!(self, int, Less),
|
||||
JGT => cond_jump!(self, int, Greater),
|
||||
JLTU => cond_jump!(self, sint, Less),
|
||||
JGTU => cond_jump!(self, sint, Greater),
|
||||
ECALL => {
|
||||
param!(self, ());
|
||||
self.traph
|
||||
.ecall(&mut self.registers, &mut self.pc, &mut self.memory);
|
||||
}
|
||||
ADDF => binary_op!(self, as_f64, ops::Add::add),
|
||||
SUBF => binary_op!(self, as_f64, ops::Sub::sub),
|
||||
MULF => binary_op!(self, as_f64, ops::Mul::mul),
|
||||
DIRF => {
|
||||
let ParamBBBB(dt, rt, a0, a1) = param!(self, ParamBBBB);
|
||||
let a0 = self.read_reg(a0).as_f64();
|
||||
let a1 = self.read_reg(a1).as_f64();
|
||||
self.write_reg(dt, a0 / a1);
|
||||
self.write_reg(rt, a0 % a1);
|
||||
}
|
||||
FMAF => {
|
||||
let ParamBBBB(dt, a0, a1, a2) = param!(self, ParamBBBB);
|
||||
self.write_reg(
|
||||
dt,
|
||||
self.read_reg(a0).as_f64() * self.read_reg(a1).as_f64()
|
||||
+ self.read_reg(a2).as_f64(),
|
||||
);
|
||||
}
|
||||
NEGF => {
|
||||
let ParamBB(dt, a0) = param!(self, ParamBB);
|
||||
self.write_reg(dt, -self.read_reg(a0).as_f64());
|
||||
}
|
||||
ITF => {
|
||||
let ParamBB(dt, a0) = param!(self, ParamBB);
|
||||
self.write_reg(dt, self.read_reg(a0).as_i64() as f64);
|
||||
}
|
||||
FTI => {
|
||||
let ParamBB(dt, a0) = param!(self, ParamBB);
|
||||
self.write_reg(dt, self.read_reg(a0).as_f64() as i64);
|
||||
}
|
||||
ADDFI => binary_op_imm!(self, as_f64, ops::Add::add),
|
||||
MULFI => binary_op_imm!(self, as_f64, ops::Mul::mul),
|
||||
op => {
|
||||
if !self.traph.invalid_op(
|
||||
&mut self.registers,
|
||||
&mut self.pc,
|
||||
&mut self.memory,
|
||||
op,
|
||||
) {
|
||||
return Err(VmRunError::InvalidOpcodeEx(op));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if TIMER_QUOTIENT != 0 {
|
||||
self.timer = self.timer.wrapping_add(1);
|
||||
if self.timer % TIMER_QUOTIENT == 0 {
|
||||
return Ok(VmRunOk::Timer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Read register
|
||||
#[inline]
|
||||
unsafe fn read_reg(&self, n: u8) -> Value {
|
||||
*self.registers.get_unchecked(n as usize)
|
||||
}
|
||||
|
||||
/// Write a register.
|
||||
/// Writing to register 0 is no-op.
|
||||
#[inline]
|
||||
unsafe fn write_reg(&mut self, n: u8, value: impl Into<Value>) {
|
||||
if n != 0 {
|
||||
*self.registers.get_unchecked_mut(n as usize) = value.into();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Virtual machine halt error
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
pub enum VmRunError {
|
||||
/// Unhandled invalid opcode exceptions
|
||||
InvalidOpcodeEx(u8),
|
||||
|
||||
/// Unhandled load access exception
|
||||
LoadAccessEx(u64),
|
||||
|
||||
/// Unhandled store access exception
|
||||
StoreAccessEx(u64),
|
||||
}
|
||||
|
||||
/// Virtual machine halt ok
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub enum VmRunOk {
|
||||
/// Program has eached its end
|
||||
End,
|
||||
|
||||
/// Program was interrupted by a timer
|
||||
Timer,
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
//! Program trap handling interfaces
|
||||
|
||||
use super::{
|
||||
mem::{Memory, MemoryAccessReason, PageSize},
|
||||
value::Value,
|
||||
};
|
||||
|
||||
/// Handle VM traps
|
||||
pub trait HandleTrap {
|
||||
/// Handle page fault
|
||||
fn page_fault(
|
||||
&mut self,
|
||||
reason: MemoryAccessReason,
|
||||
memory: &mut Memory,
|
||||
vaddr: u64,
|
||||
size: PageSize,
|
||||
dataptr: *mut u8,
|
||||
) -> bool;
|
||||
|
||||
/// Handle invalid opcode exception
|
||||
fn invalid_op(
|
||||
&mut self,
|
||||
regs: &mut [Value; 256],
|
||||
pc: &mut usize,
|
||||
memory: &mut Memory,
|
||||
op: u8,
|
||||
) -> bool
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
/// Handle environment calls
|
||||
fn ecall(&mut self, regs: &mut [Value; 256], pc: &mut usize, memory: &mut Memory)
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
//! HoleyBytes register value definition
|
||||
|
||||
use core::fmt::Debug;
|
||||
|
||||
/// Define [`Value`] union
|
||||
///
|
||||
/// # Safety
|
||||
/// Union variants have to be sound to byte-reinterpretate
|
||||
/// between each other. Otherwise the behaviour is undefined.
|
||||
macro_rules! value_def {
|
||||
($($ty:ident),* $(,)?) => {
|
||||
/// HBVM register value
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(packed)]
|
||||
pub union Value {
|
||||
$(pub $ty: $ty),*
|
||||
}
|
||||
|
||||
paste::paste! {
|
||||
impl Value {$(
|
||||
#[doc = "Byte-reinterpret [`Value`] as [`" $ty "`]"]
|
||||
#[inline]
|
||||
pub fn [<as_ $ty>](&self) -> $ty {
|
||||
unsafe { self.$ty }
|
||||
}
|
||||
)*}
|
||||
}
|
||||
|
||||
$(
|
||||
impl From<$ty> for Value {
|
||||
#[inline]
|
||||
fn from(value: $ty) -> Self {
|
||||
Self { $ty: value }
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
value_def!(u64, i64, f64);
|
||||
static_assertions::const_assert_eq!(core::mem::size_of::<Value>(), 8);
|
||||
|
||||
impl Debug for Value {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
// Print formatted as hexadecimal, unsigned integer
|
||||
write!(f, "{:x}", self.as_u64())
|
||||
}
|
||||
}
|
|
@ -1,578 +0,0 @@
|
|||
//! Welcome to the land of The Great Dispatch Loop
|
||||
//!
|
||||
//! Have fun
|
||||
|
||||
use {
|
||||
super::{bmc::BlockCopier, mem::Memory, value::ValueVariant, Vm, VmRunError, VmRunOk},
|
||||
crate::{
|
||||
mem::{addr::AddressOp, Address},
|
||||
value::CheckedDivRem,
|
||||
},
|
||||
core::{cmp::Ordering, ops},
|
||||
hbbytecode::{
|
||||
OpsN, OpsO, OpsP, OpsRB, OpsRD, OpsRH, OpsRR, OpsRRA, OpsRRAH, OpsRRB, OpsRRD, OpsRRH,
|
||||
OpsRRO, OpsRROH, OpsRRP, OpsRRPH, OpsRRR, OpsRRRR, OpsRW, RoundingMode,
|
||||
},
|
||||
};
|
||||
|
||||
macro_rules! handler {
|
||||
($self:expr, |$ty:ident ($($ident:pat),* $(,)?)| $expr:expr) => {{
|
||||
#[allow(unused_unsafe)]
|
||||
let $ty($($ident),*) = unsafe { $self.decode::<$ty>() };
|
||||
#[allow(clippy::no_effect)] let e = $expr;
|
||||
$self.bump_pc::<$ty>();
|
||||
e
|
||||
}};
|
||||
}
|
||||
|
||||
impl<Mem, const TIMER_QUOTIENT: usize> Vm<Mem, TIMER_QUOTIENT>
|
||||
where
|
||||
Mem: Memory,
|
||||
{
|
||||
/// Execute program
|
||||
///
|
||||
/// 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::*;
|
||||
loop {
|
||||
// Big match
|
||||
//
|
||||
// Contribution guide:
|
||||
// - Zero register shall never be overwitten. It's value has to always be 0.
|
||||
// - Prefer `Self::read_reg` and `Self::write_reg` functions
|
||||
// - Try to use `handler!` macro for decoding and then bumping program counter
|
||||
// - Prioritise speed over code size
|
||||
// - Memory is cheap, CPUs not that much
|
||||
// - Do not heap allocate at any cost
|
||||
// - Yes, user-provided trap handler may allocate,
|
||||
// but that is not our »fault«.
|
||||
// - Unsafe is kinda must, but be sure you have validated everything
|
||||
// - Your contributions have to pass sanitizers, fuzzer and Miri
|
||||
// - Strictly follow the spec
|
||||
// - The spec does not specify how you perform actions, in what order,
|
||||
// just that the observable effects have to be performed in order and
|
||||
// correctly.
|
||||
// - 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 => {
|
||||
self.bump_pc::<OpsN>();
|
||||
return Err(VmRunError::Unreachable);
|
||||
}
|
||||
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(
|
||||
tg,
|
||||
a0,
|
||||
self.read_reg(a1).cast::<u64>()
|
||||
)),
|
||||
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)| {
|
||||
// Bit negation
|
||||
self.write_reg(tg, !self.read_reg(a0).cast::<u64>())
|
||||
}),
|
||||
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)| {
|
||||
self.write_reg(tg, self.read_reg(a0).cast::<i8>() as i64)
|
||||
}),
|
||||
SXT16 => handler!(self, |OpsRR(tg, a0)| {
|
||||
self.write_reg(tg, self.read_reg(a0).cast::<i16>() as i64)
|
||||
}),
|
||||
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)| {
|
||||
// Swap registers
|
||||
match (r0, r1) {
|
||||
(0, 0) => (),
|
||||
(dst, 0) | (0, dst) => self.write_reg(dst, 0_u64),
|
||||
(r0, r1) => {
|
||||
core::ptr::swap(
|
||||
self.registers.get_unchecked_mut(usize::from(r0)),
|
||||
self.registers.get_unchecked_mut(usize::from(r1)),
|
||||
);
|
||||
}
|
||||
}
|
||||
}),
|
||||
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(
|
||||
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
|
||||
.load(dst, base, off, count)?),
|
||||
// Store. Same rules apply as to LD
|
||||
ST => handler!(self, |OpsRRAH(dst, base, off, count)| self
|
||||
.store(dst, base, off, count)?),
|
||||
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(
|
||||
dst,
|
||||
base,
|
||||
self.pcrel(off).get(),
|
||||
count
|
||||
)?),
|
||||
BMC => {
|
||||
// Block memory copy
|
||||
match if let Some(copier) = &mut self.copier {
|
||||
// There is some copier, poll.
|
||||
copier.poll(&mut self.memory)
|
||||
} else {
|
||||
// There is none, make one!
|
||||
let OpsRRH(src, dst, count) = self.decode();
|
||||
|
||||
self.copier = Some(BlockCopier::new(
|
||||
Address::new(self.read_reg(src).cast()),
|
||||
Address::new(self.read_reg(dst).cast()),
|
||||
count as _,
|
||||
));
|
||||
|
||||
self.copier
|
||||
.as_mut()
|
||||
.unwrap_unchecked() // SAFETY: We just assigned there
|
||||
.poll(&mut self.memory)
|
||||
} {
|
||||
// We are done, shift program counter
|
||||
core::task::Poll::Ready(Ok(())) => {
|
||||
self.copier = None;
|
||||
self.bump_pc::<OpsRRH>();
|
||||
}
|
||||
// Error, shift program counter (for consistency)
|
||||
// and yield error
|
||||
core::task::Poll::Ready(Err(e)) => {
|
||||
return Err(e.into());
|
||||
}
|
||||
// Not done yet, proceed to next cycle
|
||||
core::task::Poll::Pending => (),
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
core::ptr::copy(
|
||||
self.registers.get_unchecked(usize::from(src)),
|
||||
self.registers.get_unchecked_mut(usize::from(dst)),
|
||||
usize::from(count),
|
||||
);
|
||||
}),
|
||||
JMP => {
|
||||
let OpsO(off) = self.decode();
|
||||
self.pc = self.pc.wrapping_add(off);
|
||||
}
|
||||
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();
|
||||
|
||||
self.write_reg(save, self.pc.next::<OpsRRO>());
|
||||
self.pc = self
|
||||
.pcrel(offset)
|
||||
.wrapping_add(self.read_reg(reg).cast::<i64>());
|
||||
}
|
||||
JALA => {
|
||||
// Jump and link. Save PC after this instruction to
|
||||
// specified register and jump to reg
|
||||
let OpsRRA(save, reg, offset) = self.decode();
|
||||
|
||||
self.write_reg(save, self.pc.next::<OpsRRA>());
|
||||
self.pc =
|
||||
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 => {
|
||||
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);
|
||||
} else {
|
||||
self.bump_pc::<OpsRRP>();
|
||||
}
|
||||
}
|
||||
JLTS => self.cond_jmp::<u64>(Ordering::Less),
|
||||
JGTS => self.cond_jmp::<u64>(Ordering::Greater),
|
||||
JLTU => self.cond_jmp::<i64>(Ordering::Less),
|
||||
JGTU => self.cond_jmp::<i64>(Ordering::Greater),
|
||||
ECA => {
|
||||
// So we don't get timer interrupt after ECALL
|
||||
if TIMER_QUOTIENT != 0 {
|
||||
self.timer = self.timer.wrapping_add(1);
|
||||
}
|
||||
|
||||
self.bump_pc::<OpsN>();
|
||||
return Ok(VmRunOk::Ecall);
|
||||
}
|
||||
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
|
||||
.write_reg(tg, 1. / self.read_reg(reg).cast::<f32>())),
|
||||
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
|
||||
.write_reg(tg, self.read_reg(reg).cast::<i64>() as f32)),
|
||||
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(
|
||||
tg,
|
||||
crate::float::f32toint(
|
||||
self.read_reg(reg).cast::<f32>(),
|
||||
RoundingMode::try_from(mode)
|
||||
.map_err(|()| VmRunError::InvalidOperand)?,
|
||||
),
|
||||
)),
|
||||
FTI64 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg(
|
||||
tg,
|
||||
crate::float::f64toint(
|
||||
self.read_reg(reg).cast::<f64>(),
|
||||
RoundingMode::try_from(mode)
|
||||
.map_err(|()| VmRunError::InvalidOperand)?,
|
||||
),
|
||||
)),
|
||||
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(
|
||||
tg,
|
||||
crate::float::conv64to32(
|
||||
self.read_reg(reg).cast(),
|
||||
RoundingMode::try_from(mode)
|
||||
.map_err(|()| VmRunError::InvalidOperand)?,
|
||||
),
|
||||
)),
|
||||
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(
|
||||
dst,
|
||||
base,
|
||||
self.pcrel(off).get(),
|
||||
count
|
||||
)?),
|
||||
STR16 => handler!(self, |OpsRRPH(dst, base, off, count)| self.store(
|
||||
dst,
|
||||
base,
|
||||
self.pcrel(off).get(),
|
||||
count
|
||||
)?),
|
||||
JMP16 => {
|
||||
let OpsP(off) = self.decode();
|
||||
self.pc = self.pcrel(off);
|
||||
}
|
||||
op => return Err(VmRunError::InvalidOpcode(op)),
|
||||
}
|
||||
}
|
||||
|
||||
if TIMER_QUOTIENT != 0 {
|
||||
self.timer = self.timer.wrapping_add(1);
|
||||
if self.timer % TIMER_QUOTIENT == 0 {
|
||||
return Ok(VmRunOk::Timer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Bump instruction pointer
|
||||
#[inline(always)]
|
||||
fn bump_pc<T: Copy>(&mut self) {
|
||||
self.pc = self
|
||||
.pc
|
||||
.wrapping_add(core::mem::size_of::<T>())
|
||||
.wrapping_add(1);
|
||||
}
|
||||
|
||||
/// Decode instruction operands
|
||||
#[inline(always)]
|
||||
unsafe fn decode<T: Copy>(&mut self) -> T {
|
||||
unsafe { self.memory.prog_read::<T>(self.pc + 1_u64) }
|
||||
}
|
||||
|
||||
/// Load
|
||||
#[inline(always)]
|
||||
unsafe fn load(
|
||||
&mut self,
|
||||
dst: u8,
|
||||
base: u8,
|
||||
offset: u64,
|
||||
count: u16,
|
||||
) -> Result<(), VmRunError> {
|
||||
let n: u8 = match dst {
|
||||
0 => 1,
|
||||
_ => 0,
|
||||
};
|
||||
|
||||
unsafe {
|
||||
self.memory.load(
|
||||
self.ldst_addr_uber(dst, base, offset, count, n)?,
|
||||
self.registers
|
||||
.as_mut_ptr()
|
||||
.add(usize::from(dst) + usize::from(n))
|
||||
.cast(),
|
||||
usize::from(count).saturating_sub(n.into()),
|
||||
)
|
||||
}?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Store
|
||||
#[inline(always)]
|
||||
unsafe fn store(
|
||||
&mut self,
|
||||
dst: u8,
|
||||
base: u8,
|
||||
offset: u64,
|
||||
count: u16,
|
||||
) -> Result<(), VmRunError> {
|
||||
unsafe {
|
||||
self.memory.store(
|
||||
self.ldst_addr_uber(dst, base, offset, count, 0)?,
|
||||
self.registers.as_ptr().add(usize::from(dst)).cast(),
|
||||
count.into(),
|
||||
)
|
||||
}?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Three-way comparsion
|
||||
#[inline(always)]
|
||||
unsafe fn cmp<T: ValueVariant + Ord>(&mut self, to: u8, reg: u8, val: T) {
|
||||
self.write_reg(to, self.read_reg(reg).cast::<T>().cmp(&val) as i64);
|
||||
}
|
||||
|
||||
/// Perform binary operating over two registers
|
||||
#[inline(always)]
|
||||
unsafe fn binary_op<T: ValueVariant>(&mut self, op: impl Fn(T, T) -> T) {
|
||||
let OpsRRR(tg, a0, a1) = unsafe { self.decode() };
|
||||
self.write_reg(
|
||||
tg,
|
||||
op(self.read_reg(a0).cast::<T>(), self.read_reg(a1).cast::<T>()),
|
||||
);
|
||||
self.bump_pc::<OpsRRR>();
|
||||
}
|
||||
|
||||
/// Perform binary operation over register and immediate
|
||||
#[inline(always)]
|
||||
unsafe fn binary_op_imm<T: ValueVariant>(&mut self, op: impl Fn(T, T) -> T) {
|
||||
#[derive(Clone, Copy)]
|
||||
#[repr(packed)]
|
||||
struct OpsRRImm<I>(OpsRR, I);
|
||||
|
||||
let OpsRRImm::<T>(OpsRR(tg, reg), imm) = unsafe { self.decode() };
|
||||
self.write_reg(tg, op(self.read_reg(reg).cast::<T>(), imm));
|
||||
self.bump_pc::<OpsRRImm<T>>();
|
||||
}
|
||||
|
||||
/// Perform binary operation over register and shift immediate
|
||||
#[inline(always)]
|
||||
unsafe fn binary_op_shift<T: ValueVariant>(&mut self, op: impl Fn(T, u32) -> T) {
|
||||
let OpsRRR(tg, a0, a1) = unsafe { self.decode() };
|
||||
self.write_reg(
|
||||
tg,
|
||||
op(
|
||||
self.read_reg(a0).cast::<T>(),
|
||||
self.read_reg(a1).cast::<u32>(),
|
||||
),
|
||||
);
|
||||
self.bump_pc::<OpsRRR>();
|
||||
}
|
||||
|
||||
/// Perform binary operation over register and shift immediate
|
||||
#[inline(always)]
|
||||
unsafe fn binary_op_ims<T: ValueVariant>(&mut self, op: impl Fn(T, u32) -> T) {
|
||||
let OpsRRB(tg, reg, imm) = unsafe { self.decode() };
|
||||
self.write_reg(tg, op(self.read_reg(reg).cast::<T>(), imm.into()));
|
||||
self.bump_pc::<OpsRRB>();
|
||||
}
|
||||
|
||||
/// Fused division-remainder
|
||||
#[inline(always)]
|
||||
unsafe fn dir<T: ValueVariant + CheckedDivRem>(&mut self) {
|
||||
handler!(self, |OpsRRRR(td, tr, a0, a1)| {
|
||||
let a0 = self.read_reg(a0).cast::<T>();
|
||||
let a1 = self.read_reg(a1).cast::<T>();
|
||||
|
||||
if let Some(div) = a0.checked_div(a1) {
|
||||
self.write_reg(td, div);
|
||||
} else {
|
||||
self.write_reg(td, -1_i64);
|
||||
}
|
||||
|
||||
if let Some(rem) = a0.checked_rem(a1) {
|
||||
self.write_reg(tr, rem);
|
||||
} else {
|
||||
self.write_reg(tr, a0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Fused multiply-add
|
||||
#[inline(always)]
|
||||
unsafe fn fma<T>(&mut self)
|
||||
where
|
||||
T: ValueVariant + core::ops::Mul<Output = T> + core::ops::Add<Output = T>,
|
||||
{
|
||||
handler!(self, |OpsRRRR(tg, a0, a1, a2)| {
|
||||
let a0 = self.read_reg(a0).cast::<T>();
|
||||
let a1 = self.read_reg(a1).cast::<T>();
|
||||
let a2 = self.read_reg(a2).cast::<T>();
|
||||
self.write_reg(tg, a0 * a1 + a2)
|
||||
});
|
||||
}
|
||||
|
||||
/// Float comparsion
|
||||
#[inline(always)]
|
||||
unsafe fn fcmp<T: PartialOrd + ValueVariant>(&mut self, nan: Ordering) {
|
||||
handler!(self, |OpsRRR(tg, a0, a1)| {
|
||||
let a0 = self.read_reg(a0).cast::<T>();
|
||||
let a1 = self.read_reg(a1).cast::<T>();
|
||||
self.write_reg(tg, (a0.partial_cmp(&a1).unwrap_or(nan) as i8 + 1) as u8)
|
||||
});
|
||||
}
|
||||
|
||||
/// Calculate pc-relative address
|
||||
#[inline(always)]
|
||||
fn pcrel(&self, offset: impl AddressOp) -> Address {
|
||||
self.pc.wrapping_add(offset)
|
||||
}
|
||||
|
||||
/// Jump at `PC + #3` if ordering on `#0 <=> #1` is equal to expected
|
||||
#[inline(always)]
|
||||
unsafe fn cond_jmp<T: ValueVariant + Ord>(&mut self, expected: Ordering) {
|
||||
let OpsRRP(a0, a1, ja) = unsafe { self.decode() };
|
||||
if self
|
||||
.read_reg(a0)
|
||||
.cast::<T>()
|
||||
.cmp(&self.read_reg(a1).cast::<T>())
|
||||
== expected
|
||||
{
|
||||
self.pc = self.pcrel(ja);
|
||||
} else {
|
||||
self.bump_pc::<OpsRRP>();
|
||||
}
|
||||
}
|
||||
|
||||
/// Load / Store Address check-computation überfunction
|
||||
#[inline(always)]
|
||||
unsafe fn ldst_addr_uber(
|
||||
&self,
|
||||
dst: u8,
|
||||
base: u8,
|
||||
offset: u64,
|
||||
size: u16,
|
||||
adder: u8,
|
||||
) -> Result<Address, VmRunError> {
|
||||
let reg = dst.checked_add(adder).ok_or(VmRunError::RegOutOfBounds)?;
|
||||
|
||||
if usize::from(reg) * 8 + usize::from(size) > 2048 {
|
||||
Err(VmRunError::RegOutOfBounds)
|
||||
} else {
|
||||
self.read_reg(base)
|
||||
.cast::<u64>()
|
||||
.checked_add(offset)
|
||||
.and_then(|x| x.checked_add(adder.into()))
|
||||
.ok_or(VmRunError::AddrOutOfBounds)
|
||||
.map(Address::new)
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue