forked from AbleOS/holey-bytes
Compare commits
248 Commits
Author | SHA1 | Date |
---|---|---|
mlokr | ed648e10e1 | |
mlokr | c79f2c376f | |
mlokr | c1f479e721 | |
mlokr | 6ac6c5b1e5 | |
mlokr | adfbe7c200 | |
mlokr | 409b390ba5 | |
mlokr | 812fe8b4bd | |
mlokr | ac5e469490 | |
mlokr | 7ea51c1d83 | |
mlokr | 939a98921d | |
mlokr | 325832c91e | |
mlokr | 1896675cb0 | |
mlokr | 83ce2471c6 | |
mlokr | f29c022fd0 | |
mlokr | 51280b18cd | |
mlokr | 976cc43057 | |
mlokr | 446060294f | |
mlokr | 23b04c60f4 | |
mlokr | ab5f80aea9 | |
mlokr | b588503c84 | |
mlokr | cc72bfbf3f | |
mlokr | 6b137a4cb7 | |
mlokr | f4dbc81d17 | |
mlokr | 3a8d8b6800 | |
mlokr | 953815c4ed | |
mlokr | 9dbe088328 | |
mlokr | 3645aac4ea | |
mlokr | f53240531e | |
mlokr | dc32bca10e | |
mlokr | 06c81f6d7c | |
able | f421d7e5cb | |
mlokr | 8e35bf6be6 | |
mlokr | 4edcb7cbdc | |
mlokr | 665c16fde2 | |
mlokr | 6e36f62512 | |
mlokr | 1f987a15fb | |
mlokr | e4a8caddbf | |
mlokr | d4fd2ce9d3 | |
mlokr | a33134c908 | |
mlokr | 89b219b6f0 | |
mlokr | 17ef9dc21b | |
mlokr | 579339af4e | |
mlokr | 13218faad1 | |
mlokr | 48ee17e146 | |
able | 796a826cc5 | |
Erin | 247b2c6614 | |
Erin | 0070016f74 | |
Erin | 6c6e29479f | |
Erin | eb46b24a10 | |
Erin | 942839a5f8 | |
mlokr | 9ddc336ecd | |
Erin | 34d1bf415e | |
mlokr | c978b408e2 | |
Erin | fe9a0667b8 | |
mlokr | 975ce8a9fe | |
mlokr | 8a3dd3001d | |
Erin | de723980da | |
Erin | 4aa39f3fbc | |
Erin | 5f8864e251 | |
mlokr | bcbe47bcd6 | |
Erin | 30ee6c84fc | |
mlokr | 6e464be33d | |
mlokr | 09aacff161 | |
mlokr | 433f2db4d1 | |
mlokr | e335e55aa0 | |
Erin | 8e0aeabc07 | |
Erin | a84e93d562 | |
Erin | 8374dfe20a | |
Erin | e9e1242743 | |
Erin | f604a2463d | |
Erin | 59e38db874 | |
Erin | 8c257e9216 | |
Erin | 84cc1db691 | |
Erin | 633e3adc61 | |
Erin | 7f981fe9a0 | |
Erin | 43c36774a5 | |
Erin | 5dd0e22c0d | |
Erin | b161d46a5b | |
Erin | 42488e1e4a | |
Erin | b84ff70014 | |
Erin | b8432d544c | |
Erin | 207d8d7fa6 | |
Erin | 569f154bcc | |
Erin | 68094ce0ae | |
Bee | 84dcbfc6bb | |
Bee | d26c285ca7 | |
Erin | d255967125 | |
Erin | c5c8d23470 | |
Erin | aca8045a98 | |
Erin | 398687d8bf | |
Erin | a7c4379976 | |
Erin | 949dd3ba61 | |
Erin | 3771180909 | |
Erin | 6b3a132451 | |
Erin | b45d235312 | |
Erin | 9ee3e9cb5f | |
Erin | 57f30109c8 | |
Erin | d6243fa99f | |
Erin | 3a6d0fdd2d | |
Erin | 9b823ef660 | |
Erin | 84aeac0b2a | |
Erin | fc4118938e | |
Erin | cb557d1361 | |
Erin | 2715bc9107 | |
Erin | 8182abca98 | |
Erin | 83563fff84 | |
Erin | b4923cfb95 | |
Erin | eab47db4d6 | |
Erin | 4b45407a70 | |
Erin | a944a145ed | |
Erin | 0e701e31b5 | |
Erin | 0cb20d5727 | |
Erin | 889aefe87a | |
Erin | 59be906835 | |
Erin | 441356b6f2 | |
Erin | 2f8612c6d2 | |
Erin | 3e4095da6f | |
Erin | b1bdbea991 | |
Erin | 8c8c708279 | |
Erin | 35f90e94a8 | |
able | e7aa306e5d | |
Erin | 42be580425 | |
Erin | 9b8a4a718e | |
Erin | 0d2949024c | |
Erin | 26105bab70 | |
Erin | 006dcca309 | |
Erin | 3034469e89 | |
Erin | 30070818ae | |
Erin | d282b3d111 | |
Erin | 600528434b | |
Erin | 0deeaf3a7e | |
Erin | 3decd01619 | |
Erin | a071a4a7ae | |
Erin | af1a7d3bfa | |
Erin | 3fdf936f77 | |
Erin | 96b749060d | |
Erin | 770c2ebcf0 | |
Erin | 6609bd10c5 | |
Erin | 97eaae1c76 | |
Erin | 1460a7a230 | |
Erin | 529fbdaed4 | |
Erin | 3ac80a2e3d | |
Erin | 06d66289bc | |
Erin | 430ccd170d | |
Erin | e2d3f46d3f | |
Erin | eadf9e0a1f | |
Erin | d74b32a38d | |
Erin | 4530ff049e | |
Erin | 2d2978eec7 | |
Erin | bf50bcb203 | |
Erin | 82f23ec2e2 | |
Erin | 5264576274 | |
Erin | cdee99598e | |
Erin | f130a27685 | |
Erin | aa186b35c2 | |
Erin | 629fc969c2 | |
Erin | 8287b1bdc1 | |
Erin | 73b998911c | |
Erin | 1a5f101719 | |
Erin | a667c36d6c | |
Erin | 582c716445 | |
Erin | 37a1923f1e | |
Erin | 10f9907c09 | |
Erin | 2480a65947 | |
Erin | 1ed153a9a2 | |
Erin | 19df4538d7 | |
Erin | e07bfb2301 | |
Erin | cfe3203ef1 | |
Erin | c4e062e742 | |
Erin | 83436507df | |
Erin | bdda987da9 | |
Erin | 6588837769 | |
Erin | f2ec9a3034 | |
Erin | 95c979cb83 | |
Erin | 66f634a70f | |
Erin | 077da50787 | |
Erin | 5055626968 | |
Erin | 0f5d78bb27 | |
Erin | 668b324cc8 | |
Erin | 759514686a | |
Erin | 92793dc93b | |
Erin | ac149a5319 | |
able | f4c55ae3cc | |
Erin | a82686ec07 | |
Erin | b3a6c42af3 | |
Erin | d20447dd15 | |
Erin | 193be0bd5a | |
Erin | fce3fa5210 | |
Erin | 4ca4e81ac3 | |
Erin | dcfd51999d | |
Erin | 1532c501a6 | |
Erin | 8eebbacb91 | |
Erin | c621a5c71d | |
Erin | 8d5d22eae1 | |
Erin | 3892a719eb | |
Erin | 47323e140c | |
Erin | 3833beb17d | |
Erin | afbf6dd2e4 | |
Erin | ec7053a289 | |
Erin | 1a53c80a62 | |
Erin | 6fe1fd91bf | |
able | ac7fdc7688 | |
able | 9cf8789e9a | |
able | 3534b26946 | |
Erin | a21f68ffa6 | |
Erin | 7833334713 | |
Erin | 141c5f524f | |
Erin | 446225bcf6 | |
bee | d6ea5adf49 | |
Egggggg | 898738fb40 | |
Erin | beb6e23d71 | |
Egggggg | 5afd081c2a | |
Erin | 81cf5c4336 | |
Egggggg | 63cf7ac0b0 | |
Erin | 3cb3ee1fee | |
Erin | 4dfbe93919 | |
Erin | 6791b6d48e | |
Erin | 0ed89234a7 | |
able | 6afec2a031 | |
IntoTheNight | b83d1838aa | |
MunirG05 | e25a89d56d | |
MunirG05 | b72f0afe84 | |
Erin | 9196519fae | |
MunirG05 | 6759fbd2ab | |
Erin | 4f53fb1c87 | |
Erin | ad96e83f09 | |
Erin | 5ee8a91479 | |
Erin | bde00c13f2 | |
Erin | b4dac1245b | |
Erin | e700010e7f | |
Erin | 2d34ed61d0 | |
Erin | 3919aa8100 | |
Erin | a548a7b08e | |
Erin | 132fc1a6ed | |
able | c31c9e9a54 | |
able | c26b559898 | |
Erin | 907dd66d5e | |
Erin | 2416526014 | |
Erin | bb50c09538 | |
Erin | a7cf5e4847 | |
able | 87ec6ded54 | |
able | 4a840a6ef0 | |
able | 5ec6da9fb4 | |
able | fdca041e6b | |
Erin | 06b1184772 | |
Erin | fb78e0a44a | |
Erin | 7eaa01f53c | |
Erin | 119ce4405f |
|
@ -0,0 +1,2 @@
|
|||
[alias]
|
||||
xtask = "r -p xtask --"
|
|
@ -1 +1,4 @@
|
|||
/target
|
||||
/hbbytecode/src/opcode.rs
|
||||
/hbbytecode/src/ops.rs
|
||||
/hblang/src/instrs.rs
|
||||
|
|
|
@ -3,16 +3,98 @@
|
|||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "ahash"
|
||||
version = "0.8.3"
|
||||
name = "addr2line"
|
||||
version = "0.21.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f"
|
||||
checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb"
|
||||
dependencies = [
|
||||
"gimli",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "adler"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "ahash"
|
||||
version = "0.8.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"const-random",
|
||||
"getrandom",
|
||||
"once_cell",
|
||||
"version_check",
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "argh"
|
||||
version = "0.1.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7af5ba06967ff7214ce4c7419c7d185be7ecd6cc4965a8f6e1d8ce0398aad219"
|
||||
dependencies = [
|
||||
"argh_derive",
|
||||
"argh_shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "argh_derive"
|
||||
version = "0.1.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56df0aeedf6b7a2fc67d06db35b09684c3e8da0c95f8f27685cb17e08413d87a"
|
||||
dependencies = [
|
||||
"argh_shared",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.63",
|
||||
]
|
||||
|
||||
[[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.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
|
||||
|
||||
[[package]]
|
||||
name = "backtrace"
|
||||
version = "0.3.71"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d"
|
||||
dependencies = [
|
||||
"addr2line",
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"miniz_oxide",
|
||||
"object",
|
||||
"rustc-demangle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.97"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "099a5357d84c4c61eb35fc8eafa9a79a902c2f76911e5747ced4e032edd8d9b4"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
|
@ -20,43 +102,474 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "compiler"
|
||||
name = "color-eyre"
|
||||
version = "0.6.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "55146f5e46f237f7423d74111267d4597b59b0dad0ffaf7303bce9945d843ad5"
|
||||
dependencies = [
|
||||
"backtrace",
|
||||
"color-spantrace",
|
||||
"eyre",
|
||||
"indenter",
|
||||
"once_cell",
|
||||
"owo-colors",
|
||||
"tracing-error",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "color-spantrace"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cd6be1b2a7e382e2b98b43b2adcca6bb0e465af0bdd38123873ae61eb17a72c2"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"owo-colors",
|
||||
"tracing-core",
|
||||
"tracing-error",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "const-random"
|
||||
version = "0.1.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87e00182fe74b066627d63b85fd550ac2998d4b0bd86bfed477a0ae4c7c71359"
|
||||
dependencies = [
|
||||
"const-random-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "const-random-macro"
|
||||
version = "0.1.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"once_cell",
|
||||
"tiny-keccak",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crunchy"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
|
||||
|
||||
[[package]]
|
||||
name = "eyre"
|
||||
version = "0.6.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec"
|
||||
dependencies = [
|
||||
"indenter",
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gimli"
|
||||
version = "0.28.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
|
||||
|
||||
[[package]]
|
||||
name = "hbasm"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"paste",
|
||||
"rhai",
|
||||
"with_builtin_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hbbytecode"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e"
|
||||
name = "hblang"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"hbvm",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hbvm"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"hashbrown",
|
||||
"log",
|
||||
"hbbytecode",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.17"
|
||||
name = "hbxrt"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"hbvm",
|
||||
"memmap2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indenter"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
|
||||
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
|
||||
|
||||
[[package]]
|
||||
name = "instant"
|
||||
version = "0.1.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.17.1"
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.154"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
|
||||
|
||||
[[package]]
|
||||
name = "memmap2"
|
||||
version = "0.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fe751422e4a8caa417e13c3ea66452215d7d63e19e604f4980461212f3ae1322"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7"
|
||||
dependencies = [
|
||||
"adler",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.32.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.19.0"
|
||||
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"
|
||||
|
||||
[[package]]
|
||||
name = "paste"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.82"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ad3d49ab951a01fbaafe34f2ec74122942fe18a3f9814c3268f1bb72042131b"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rhai"
|
||||
version = "1.18.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a7d88770120601ba1e548bb6bc2a05019e54ff01b51479e38e64ec3b59d4759"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"bitflags",
|
||||
"instant",
|
||||
"num-traits",
|
||||
"once_cell",
|
||||
"rhai_codegen",
|
||||
"smallvec",
|
||||
"smartstring",
|
||||
"thin-vec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rhai_codegen"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "59aecf17969c04b9c0c5d21f6bc9da9fec9dd4980e64d1871443a476589d8c86"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.63",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.202"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.202"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.63",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sharded-slab"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
|
||||
|
||||
[[package]]
|
||||
name = "smartstring"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3fb72c633efbaa2dd666986505016c32c3044395ceaf881518399d2f4127ee29"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"static_assertions",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "static_assertions"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.109"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.63"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bf5be731623ca1a1fb7d8be6f261a3be6d3e2337b8a1f97be944d020c8fcb704"
|
||||
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.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tiny-keccak"
|
||||
version = "2.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237"
|
||||
dependencies = [
|
||||
"crunchy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.40"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
|
||||
dependencies = [
|
||||
"pin-project-lite",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-core"
|
||||
version = "0.1.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"valuable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-error"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e"
|
||||
dependencies = [
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-subscriber"
|
||||
version = "0.3.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b"
|
||||
dependencies = [
|
||||
"sharded-slab",
|
||||
"thread_local",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||
|
||||
[[package]]
|
||||
name = "with_builtin_macros"
|
||||
version = "0.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a59d55032495429b87f9d69954c6c8602e4d3f3e0a747a12dea6b0b23de685da"
|
||||
dependencies = [
|
||||
"with_builtin_macros-proc_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "with_builtin_macros-proc_macros"
|
||||
version = "0.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "15bd7679c15e22924f53aee34d4e448c45b674feb6129689af88593e129f8f42"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "xtask"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"argh",
|
||||
"color-eyre",
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.7.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae87e3fcd617500e5d106f0380cf7b77f3c6092aae37191433159dda23cfb087"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.7.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "15e934569e47891f7d9411f1a451d947a60e000ab3bd24fbb970f000387d1b3b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.63",
|
||||
]
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
[workspace]
|
||||
members = ["hbvm", "compiler"]
|
||||
resolver = "2"
|
||||
members = ["hbasm", "hbbytecode", "hbvm", "hbxrt", "xtask", "hblang"]
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
# Math operations
|
||||
```
|
||||
MATH_OP
|
||||
Add
|
||||
Sub
|
||||
Mul
|
||||
Div
|
||||
Mod
|
||||
```
|
||||
```
|
||||
MATH_TYPE
|
||||
Unsigned
|
||||
Signed
|
||||
FloatingPoint
|
||||
```
|
||||
|
||||
```
|
||||
MATH_OP_SIDES
|
||||
Register Constant
|
||||
Register Register
|
||||
Constant Constant
|
||||
Constant Register
|
||||
```
|
||||
`[MATH_OP] [MATH_OP_SIDES] [MATH_TYPE] [IMM_LHS] [IMM_RHS] [REG]`
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
load 0 a0 ;; 05 00 A0
|
||||
load 10 a1 ;; 05 10 A1
|
||||
add a0 1 a0 ;; 01 A0 01 A0
|
||||
jump_neq a0 a1 0 ;; a1 A0 A1 0
|
|
@ -1,4 +0,0 @@
|
|||
load 10 A1
|
||||
load 0 A0
|
||||
add A0 1
|
||||
jump_less_than A0 A1 0
|
|
@ -1,8 +0,0 @@
|
|||
[package]
|
||||
name = "compiler"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
|
@ -1,5 +0,0 @@
|
|||
fn main() {
|
||||
let prog = "load 1, A0
|
||||
jump 0";
|
||||
println!("Hello, world!");
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
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.
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "hbasm"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
paste = "1.0"
|
||||
rhai = "1.16"
|
||||
with_builtin_macros = "0.0.3"
|
|
@ -0,0 +1,13 @@
|
|||
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();
|
|
@ -0,0 +1,24 @@
|
|||
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);}
|
|
@ -0,0 +1,9 @@
|
|||
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
|
|
@ -0,0 +1,33 @@
|
|||
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();
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
//! 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
|
||||
}
|
|
@ -0,0 +1,321 @@
|
|||
//! 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 });
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
//! 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");
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
pub mod data;
|
||||
pub mod ins;
|
||||
pub mod label;
|
||||
pub mod linker;
|
||||
pub mod object;
|
||||
|
||||
use {
|
||||
object::Object,
|
||||
rhai::{Engine, Module},
|
||||
std::{cell::RefCell, rc::Rc},
|
||||
};
|
||||
|
||||
type SharedObject = Rc<RefCell<Object>>;
|
||||
|
||||
pub fn assembler(
|
||||
linkout: &mut impl std::io::Write,
|
||||
loader: impl FnOnce(&mut Engine) -> Result<(), Box<rhai::EvalAltResult>>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut engine = Engine::new();
|
||||
let mut module = Module::new();
|
||||
let obj = Rc::new(RefCell::new(Object::default()));
|
||||
ins::setup(&mut module, Rc::clone(&obj));
|
||||
label::setup(&mut engine, &mut module, Rc::clone(&obj));
|
||||
|
||||
// Registers
|
||||
for n in 0_u8..=255 {
|
||||
module.set_var(format!("r{n}"), n);
|
||||
}
|
||||
|
||||
module.set_native_fn("reg", |n: i64| {
|
||||
Ok(u8::try_from(n).map_err(|_| {
|
||||
rhai::EvalAltResult::ErrorRuntime("Invalid register value".into(), rhai::Position::NONE)
|
||||
})?)
|
||||
});
|
||||
|
||||
module.set_native_fn("as_i64", |n: u8| Ok(n as i64));
|
||||
|
||||
let datamod = Rc::new(data::module(&mut engine, SharedObject::clone(&obj)));
|
||||
engine.register_global_module(Rc::new(module));
|
||||
engine.register_static_module("data", datamod);
|
||||
engine.register_type_with_name::<object::SymbolRef>("SymbolRef");
|
||||
loader(&mut engine)?;
|
||||
linker::link(obj, linkout)?;
|
||||
Ok(())
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
//! 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,8 @@
|
|||
use std::{io::stdout, path::PathBuf};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let path = PathBuf::from(std::env::args().nth(1).ok_or("Missing path")?);
|
||||
hbasm::assembler(&mut stdout(), |engine| engine.run_file(path))?;
|
||||
|
||||
Ok(())
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
//! 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,4 @@
|
|||
[package]
|
||||
name = "hbbytecode"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
|
@ -0,0 +1,58 @@
|
|||
#![feature(iter_next_chunk)]
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
println!("cargo:rerun-if-changed=instructions.in");
|
||||
|
||||
let mut generated = String::new();
|
||||
gen_op_structs(&mut generated)?;
|
||||
std::fs::write("src/ops.rs", generated)?;
|
||||
|
||||
let mut generated = String::new();
|
||||
gen_op_codes(&mut generated)?;
|
||||
std::fs::write("src/opcode.rs", generated)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn gen_op_structs(generated: &mut String) -> std::fmt::Result {
|
||||
use std::fmt::Write;
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
writeln!(generated, "use crate::*;")?;
|
||||
for [.., args, _] in instructions() {
|
||||
if !seen.insert(args) {
|
||||
continue;
|
||||
}
|
||||
|
||||
writeln!(generated, "#[derive(Clone, Copy, Debug)]")?;
|
||||
writeln!(generated, "#[repr(packed)]")?;
|
||||
write!(generated, "pub struct Ops{args}(")?;
|
||||
let mut first = true;
|
||||
for ch in args.chars().filter(|&ch| ch != 'N') {
|
||||
if !std::mem::take(&mut first) {
|
||||
write!(generated, ",")?;
|
||||
}
|
||||
write!(generated, "pub Op{ch}")?;
|
||||
}
|
||||
writeln!(generated, ");")?;
|
||||
writeln!(generated, "unsafe impl BytecodeItem for Ops{args} {{}}")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn gen_op_codes(generated: &mut String) -> std::fmt::Result {
|
||||
use std::fmt::Write;
|
||||
for [op, name, _, comment] in instructions() {
|
||||
writeln!(generated, "#[doc = {comment}]")?;
|
||||
writeln!(generated, "pub const {name}: u8 = {op};")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn instructions() -> impl Iterator<Item = [&'static str; 4]> {
|
||||
include_str!("../hbbytecode/instructions.in")
|
||||
.lines()
|
||||
.map(|line| line.strip_suffix(';').unwrap())
|
||||
.map(|line| line.splitn(4, ',').map(str::trim).next_chunk().unwrap())
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
/* 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
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static_assert(CHAR_BIT == 8, "Cursed architectures are not supported");
|
||||
|
||||
enum hbbc_Opcode: uint8_t {
|
||||
hbbc_Op_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 ,
|
||||
} typedef hbbc_Opcode;
|
||||
|
||||
static_assert(sizeof(hbbc_Opcode) == 1);
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct hbbc_ParamBBBB
|
||||
{ uint8_t _0; uint8_t _1; uint8_t _2; uint8_t _3; }
|
||||
typedef hbbc_ParamBBBB;
|
||||
static_assert(sizeof(hbbc_ParamBBBB) == 32 / 8);
|
||||
|
||||
struct hbbc_ParamBBB
|
||||
{ uint8_t _0; uint8_t _1; uint8_t _2; }
|
||||
typedef hbbc_ParamBBB;
|
||||
static_assert(sizeof(hbbc_ParamBBB) == 24 / 8);
|
||||
|
||||
struct hbbc_ParamBBDH
|
||||
{ uint8_t _0; uint8_t _1; uint64_t _2; uint16_t _3; }
|
||||
typedef hbbc_ParamBBDH;
|
||||
static_assert(sizeof(hbbc_ParamBBDH) == 96 / 8);
|
||||
|
||||
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;
|
||||
static_assert(sizeof(hbbc_ParamBB) == 16 / 8);
|
||||
|
||||
struct hbbc_ParamBD
|
||||
{ uint8_t _0; uint64_t _1; }
|
||||
typedef hbbc_ParamBD;
|
||||
static_assert(sizeof(hbbc_ParamBD) == 72 / 8);
|
||||
|
||||
typedef uint64_t hbbc_ParamD;
|
||||
static_assert(sizeof(hbbc_ParamD) == 64 / 8);
|
||||
|
||||
#pragma pack(pop)
|
|
@ -0,0 +1,120 @@
|
|||
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)" ;
|
|
@ -0,0 +1,43 @@
|
|||
#![no_std]
|
||||
|
||||
pub use crate::ops::*;
|
||||
use core::convert::TryFrom;
|
||||
|
||||
pub mod opcode;
|
||||
mod ops;
|
||||
|
||||
type OpR = u8;
|
||||
|
||||
type OpA = u64;
|
||||
type OpO = i32;
|
||||
type OpP = i16;
|
||||
|
||||
type OpB = u8;
|
||||
type OpH = u16;
|
||||
type OpW = u32;
|
||||
type OpD = u64;
|
||||
|
||||
/// # Safety
|
||||
/// Has to be valid to be decoded from bytecode.
|
||||
pub unsafe trait BytecodeItem {}
|
||||
unsafe impl BytecodeItem for u8 {}
|
||||
|
||||
/// 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(())
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "hblang"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[[bin]]
|
||||
name = "hbc"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
hbvm = { path = "../hbvm", features = ["nightly"] }
|
|
@ -0,0 +1,136 @@
|
|||
#![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')
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
main := fn(): int {
|
||||
return 10 - 20 / 2 + 4 * (2 + 2) - 4 * 4 + 1;
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
|
||||
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;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
Type := struct {
|
||||
brah: int,
|
||||
blah: int,
|
||||
}
|
||||
|
||||
main := fn(): int {
|
||||
return @eca(int, 1, Type.(10, 20), @sizeof(Type), @alignof(Type), 5, 6);
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
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;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
|
||||
main := fn(): int {
|
||||
return fib(10);
|
||||
}
|
||||
|
||||
fib := fn(x: int): int {
|
||||
if x <= 2 {
|
||||
return 1;
|
||||
} else {
|
||||
return fib(x - 1) + fib(x - 2);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
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 -= 1;
|
||||
|
||||
stack_reclamation_edge_case := 0;
|
||||
|
||||
continue;
|
||||
}
|
||||
return a;
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
main := fn(): int {
|
||||
return 1;
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
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;
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
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;
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
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;
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
main := fn(): int {
|
||||
a := 1;
|
||||
b := 2;
|
||||
a = a + 1;
|
||||
return a - b;
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,25 @@
|
|||
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
|
||||
}
|
|
@ -0,0 +1,382 @@
|
|||
#[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:ident] $(
|
||||
$op:ident = $op_lit:literal $(=> $assign:ident)?,
|
||||
)*
|
||||
)*
|
||||
}
|
||||
) => {
|
||||
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,
|
||||
$(Self::$assign => concat!($op_lit, "="),)?)*)*
|
||||
};
|
||||
f.write_str(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl $name {
|
||||
#[inline(always)]
|
||||
pub fn precedence(&self) -> Option<u8> {
|
||||
Some(match self {
|
||||
$($(Self::$op => ${ignore($prec)} ${index(1)},
|
||||
$(Self::$assign => 0,)?)*)*
|
||||
_ => return None,
|
||||
} + 1)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn from_ident(ident: &[u8]) -> Self {
|
||||
match ident {
|
||||
$($keyword_lit => Self::$keyword,)*
|
||||
_ => Self::Ident,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn assign_op(&self) -> Option<Self> {
|
||||
Some(match self {
|
||||
$($($(Self::$assign => Self::$op,)?)*)*
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
$vis enum $name {
|
||||
$( $pattern, )*
|
||||
$( $keyword, )*
|
||||
$( $punkt, )*
|
||||
$($( $op, $($assign,)? )*)*
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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]
|
||||
Decl = ":=",
|
||||
Assign = "=",
|
||||
#[prec]
|
||||
Or = "||",
|
||||
#[prec]
|
||||
And = "&&",
|
||||
#[prec]
|
||||
Bor = "|" => BorAss,
|
||||
#[prec]
|
||||
Xor = "^" => XorAss,
|
||||
#[prec]
|
||||
Band = "&" => BandAss,
|
||||
#[prec]
|
||||
Eq = "==",
|
||||
Ne = "!=",
|
||||
#[prec]
|
||||
Le = "<=",
|
||||
Ge = ">=",
|
||||
Lt = "<",
|
||||
Gt = ">",
|
||||
#[prec]
|
||||
Shl = "<<" => ShlAss,
|
||||
Shr = ">>" => ShrAss,
|
||||
#[prec]
|
||||
Add = "+" => AddAss,
|
||||
Sub = "-" => SubAss,
|
||||
#[prec]
|
||||
Mul = "*" => MulAss,
|
||||
Div = "/" => DivAss,
|
||||
Mod = "%" => ModAss,
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
use TokenKind as T;
|
||||
loop {
|
||||
let mut start = self.pos;
|
||||
|
||||
let Some(c) = self.advance() else {
|
||||
return Token {
|
||||
kind: T::Eof,
|
||||
start,
|
||||
end: self.pos,
|
||||
};
|
||||
};
|
||||
|
||||
let kind = match c {
|
||||
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'<' if self.advance_if(b'<') => match self.advance_if(b'=') {
|
||||
true => T::ShlAss,
|
||||
false => T::Shl,
|
||||
},
|
||||
b'<' => T::Lt,
|
||||
b'>' if self.advance_if(b'=') => T::Ge,
|
||||
b'>' if self.advance_if(b'>') => match self.advance_if(b'=') {
|
||||
true => T::ShrAss,
|
||||
false => T::Shr,
|
||||
},
|
||||
b'>' => T::Gt,
|
||||
b'+' if self.advance_if(b'=') => T::AddAss,
|
||||
b'+' => T::Add,
|
||||
b'-' if self.advance_if(b'=') => T::SubAss,
|
||||
b'-' => T::Sub,
|
||||
b'*' if self.advance_if(b'=') => T::MulAss,
|
||||
b'*' => T::Mul,
|
||||
b'/' if self.advance_if(b'=') => T::DivAss,
|
||||
b'/' => T::Div,
|
||||
b'%' if self.advance_if(b'=') => T::ModAss,
|
||||
b'%' => T::Mod,
|
||||
b'&' if self.advance_if(b'=') => T::BandAss,
|
||||
b'&' if self.advance_if(b'&') => T::And,
|
||||
b'&' => T::Band,
|
||||
b'^' if self.advance_if(b'=') => T::XorAss,
|
||||
b'^' => T::Xor,
|
||||
b'|' if self.advance_if(b'=') => T::BorAss,
|
||||
b'|' if self.advance_if(b'|') => T::Or,
|
||||
b'|' => T::Bor,
|
||||
b'(' => T::LParen,
|
||||
b')' => T::RParen,
|
||||
b'{' => T::LBrace,
|
||||
b'}' => T::RBrace,
|
||||
_ => T::Error,
|
||||
};
|
||||
|
||||
return Token {
|
||||
kind,
|
||||
start,
|
||||
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::Add,
|
||||
b'-' => T::Sub,
|
||||
b'*' => T::Mul,
|
||||
b'/' => T::Div,
|
||||
b'&' => T::Band,
|
||||
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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
#![feature(noop_waker)]
|
||||
#![feature(macro_metavar_expr)]
|
||||
#![feature(let_chains)]
|
||||
#![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)
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
#![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};
|
|
@ -0,0 +1,25 @@
|
|||
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())
|
||||
}
|
|
@ -0,0 +1,808 @@
|
|||
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 fold: 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);
|
||||
let right = &*self.arena.alloc(right);
|
||||
let left = &*self.arena.alloc(fold);
|
||||
|
||||
if let Some(op) = op.assign_op() {
|
||||
let right = Expr::BinOp { left, op, right };
|
||||
fold = Expr::BinOp {
|
||||
left,
|
||||
op: TokenKind::Assign,
|
||||
right: self.arena.alloc(right),
|
||||
};
|
||||
} else {
|
||||
fold = Expr::BinOp { left, right, op };
|
||||
}
|
||||
}
|
||||
|
||||
fold
|
||||
}
|
||||
|
||||
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::Band | T::Mul => 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}")
|
||||
}
|
||||
|
||||
macro_rules! impl_parenter {
|
||||
($($name:ident => $pat:pat,)*) => {
|
||||
$(
|
||||
struct $name<'a>(&'a Expr<'a>);
|
||||
|
||||
impl<'a> std::fmt::Display for $name<'a> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
if matches!(self.0, $pat) {
|
||||
write!(f, "({})", self.0)
|
||||
} else {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
impl_parenter! {
|
||||
Unary => Expr::BinOp { .. },
|
||||
Postfix => Expr::UnOp { .. } | Expr::BinOp { .. },
|
||||
Consecutive => Expr::UnOp { .. },
|
||||
}
|
||||
|
||||
match *self {
|
||||
Self::Field { target, field } => {
|
||||
write!(f, "{}.{field}", Postfix(target))
|
||||
}
|
||||
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, "{}", Unary(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}{}", Unary(val)),
|
||||
Self::Break { .. } => write!(f, "break;"),
|
||||
Self::Continue { .. } => write!(f, "continue;"),
|
||||
Self::If {
|
||||
cond, then, else_, ..
|
||||
} => {
|
||||
write!(f, "if {cond} {}", Consecutive(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, "{}(", Postfix(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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
#![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!();
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
code size: 209
|
||||
ret: 1
|
||||
status: Ok(())
|
|
@ -0,0 +1,3 @@
|
|||
code size: 527
|
||||
ret: 512
|
||||
status: Ok(())
|
|
@ -0,0 +1,4 @@
|
|||
ev: Ecall
|
||||
code size: 217
|
||||
ret: 0
|
||||
status: Ok(())
|
|
@ -0,0 +1,3 @@
|
|||
code size: 96
|
||||
ret: 1
|
||||
status: Ok(())
|
|
@ -0,0 +1,3 @@
|
|||
code size: 997
|
||||
ret: 0
|
||||
status: Ok(())
|
|
@ -0,0 +1,3 @@
|
|||
code size: 388
|
||||
ret: 33
|
||||
status: Ok(())
|
|
@ -0,0 +1,3 @@
|
|||
code size: 347
|
||||
ret: 55
|
||||
status: Ok(())
|
|
@ -0,0 +1,3 @@
|
|||
code size: 475
|
||||
ret: 55
|
||||
status: Ok(())
|
|
@ -0,0 +1,3 @@
|
|||
code size: 486
|
||||
ret: 0
|
||||
status: Ok(())
|
|
@ -0,0 +1,3 @@
|
|||
code size: 800
|
||||
ret: 10
|
||||
status: Ok(())
|
|
@ -0,0 +1,3 @@
|
|||
code size: 530
|
||||
ret: 3
|
||||
status: Ok(())
|
|
@ -0,0 +1,3 @@
|
|||
code size: 213
|
||||
ret: 0
|
||||
status: Ok(())
|
|
@ -0,0 +1,31 @@
|
|||
Ident "main"
|
||||
Decl ":="
|
||||
Fn "fn"
|
||||
LParen "("
|
||||
RParen ")"
|
||||
Colon ":"
|
||||
Ident "int"
|
||||
LBrace "{"
|
||||
Return "return"
|
||||
Number "10"
|
||||
Sub "-"
|
||||
Number "20"
|
||||
Div "/"
|
||||
Number "2"
|
||||
Add "+"
|
||||
Number "4"
|
||||
Mul "*"
|
||||
LParen "("
|
||||
Number "2"
|
||||
Add "+"
|
||||
Number "2"
|
||||
RParen ")"
|
||||
Sub "-"
|
||||
Number "4"
|
||||
Mul "*"
|
||||
Number "4"
|
||||
Add "+"
|
||||
Number "1"
|
||||
Semi ";"
|
||||
RBrace "}"
|
||||
Eof ""
|
|
@ -0,0 +1 @@
|
|||
Eof ""
|
|
@ -0,0 +1,13 @@
|
|||
Ident "main"
|
||||
Decl ":="
|
||||
Fn "fn"
|
||||
LParen "("
|
||||
RParen ")"
|
||||
Colon ":"
|
||||
Ident "int"
|
||||
LBrace "{"
|
||||
Return "return"
|
||||
Number "1"
|
||||
Semi ";"
|
||||
RBrace "}"
|
||||
Eof ""
|
|
@ -0,0 +1 @@
|
|||
Eof ""
|
|
@ -0,0 +1,3 @@
|
|||
main := fn(): int {
|
||||
return 10 - 20 / 2 + 4 * (2 + 2) - 4 * 4 + 1;
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
main := fn(): int {
|
||||
return 1;
|
||||
}
|
|
@ -3,8 +3,10 @@ name = "hbvm"
|
|||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[features]
|
||||
default = ["alloc"]
|
||||
alloc = []
|
||||
nightly = []
|
||||
|
||||
[dependencies]
|
||||
log = "*"
|
||||
hashbrown = "0.13.2"
|
||||
hbbytecode = { path = "../hbbytecode" }
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,5 @@
|
|||
target
|
||||
artifacts
|
||||
corpus
|
||||
coverage
|
||||
Cargo.lock
|
|
@ -0,0 +1,30 @@
|
|||
[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
|
|
@ -0,0 +1,82 @@
|
|||
#![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
|
||||
}
|
||||
}
|
|
@ -0,0 +1,141 @@
|
|||
//! 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,2 +0,0 @@
|
|||
pub mod ops;
|
||||
pub mod types;
|
|
@ -1,68 +0,0 @@
|
|||
#[repr(u8)]
|
||||
pub enum Operations {
|
||||
NOP = 0,
|
||||
|
||||
ADD = 1,
|
||||
SUB = 2,
|
||||
MUL = 3,
|
||||
DIV = 4,
|
||||
MOD = 5,
|
||||
|
||||
AND = 6,
|
||||
OR = 7,
|
||||
XOR = 8,
|
||||
NOT = 9,
|
||||
|
||||
// LOADs a memory address/constant into a register
|
||||
LOAD = 15,
|
||||
// STOREs a register/constant into a memory address
|
||||
STORE = 16,
|
||||
|
||||
MapPage = 17,
|
||||
UnmapPage = 18,
|
||||
|
||||
// SHIFT LEFT 16 A0
|
||||
Shift = 20,
|
||||
|
||||
JUMP = 100,
|
||||
JumpCond = 101,
|
||||
RET = 103,
|
||||
|
||||
EnviromentCall = 255,
|
||||
}
|
||||
|
||||
pub enum PageMapTypes {
|
||||
// Have the host make a new VMPage
|
||||
VMPage = 0,
|
||||
// Ask the host to map a RealPage into memory
|
||||
RealPage = 1,
|
||||
}
|
||||
|
||||
pub enum MathOpSubTypes {
|
||||
Unsigned = 0,
|
||||
Signed = 1,
|
||||
FloatingPoint = 2,
|
||||
}
|
||||
|
||||
pub enum MathOpSides {
|
||||
RegisterConstant = 0,
|
||||
RegisterRegister = 1,
|
||||
ConstantConstant = 2,
|
||||
ConstantRegister = 3,
|
||||
}
|
||||
|
||||
pub enum RWSubTypes {
|
||||
AddrToReg = 0,
|
||||
RegToAddr,
|
||||
ConstToReg,
|
||||
ConstToAddr,
|
||||
}
|
||||
|
||||
pub enum JumpConditionals {
|
||||
Equal = 0,
|
||||
NotEqual = 1,
|
||||
LessThan = 2,
|
||||
LessThanOrEqualTo = 3,
|
||||
GreaterThan = 4,
|
||||
GreaterThanOrEqualTo = 5,
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
|
||||
pub const CONST_U8: u8 = 0x00;
|
||||
pub const CONST_I8: i8 = 0x01;
|
||||
|
||||
pub const CONST_U64: u8 = 0x02;
|
||||
pub const CONST_I64: u8 = 0x03;
|
||||
pub const CONST_F64: u8 = 0x04;
|
||||
|
||||
pub const ADDRESS: u8 = 0x05;
|
|
@ -1,6 +0,0 @@
|
|||
use alloc::vec::Vec;
|
||||
|
||||
pub type CallStack = Vec<FnCall>;
|
||||
pub struct FnCall {
|
||||
pub ret: usize,
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
pub struct EngineConfig {
|
||||
pub call_stack_depth: usize,
|
||||
pub quantum: u32,
|
||||
}
|
||||
|
||||
impl EngineConfig {
|
||||
pub fn default() -> Self {
|
||||
Self {
|
||||
call_stack_depth: 32,
|
||||
quantum: 0,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
use super::Engine;
|
||||
|
||||
pub type EnviromentCall = fn(&mut Engine) -> Result<&mut Engine, u64>;
|
|
@ -1,100 +0,0 @@
|
|||
pub mod call_stack;
|
||||
pub mod config;
|
||||
pub mod enviroment_calls;
|
||||
pub mod regs;
|
||||
#[cfg(test)]
|
||||
pub mod tests;
|
||||
|
||||
use {
|
||||
self::call_stack::CallStack,
|
||||
crate::{memory, HaltStatus, RuntimeErrors},
|
||||
alloc::vec::Vec,
|
||||
config::EngineConfig,
|
||||
log::trace,
|
||||
regs::Registers,
|
||||
};
|
||||
// pub const PAGE_SIZE: usize = 8192;
|
||||
|
||||
pub struct RealPage {
|
||||
pub ptr: *mut u8,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct VMPage {
|
||||
pub data: [u8; 8192],
|
||||
}
|
||||
impl VMPage {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
data: [0; 4096 * 2],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Page {
|
||||
VMPage(VMPage),
|
||||
RealPage(RealPage),
|
||||
}
|
||||
impl Page {
|
||||
pub fn data(&self) -> [u8; 4096 * 2] {
|
||||
match self {
|
||||
Page::VMPage(vmpage) => vmpage.data,
|
||||
Page::RealPage(_) => {
|
||||
unimplemented!("Memmapped hw page not yet supported")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn empty_enviroment_call(engine: &mut Engine) -> Result<&mut Engine, u64> {
|
||||
trace!("Registers {:?}", engine.registers);
|
||||
Err(0)
|
||||
}
|
||||
|
||||
pub struct Engine {
|
||||
pub index: usize,
|
||||
pub program: Vec<u8>,
|
||||
pub registers: Registers,
|
||||
pub config: EngineConfig,
|
||||
|
||||
/// BUG: This DOES NOT account for overflowing
|
||||
pub last_timer_count: u32,
|
||||
pub timer_callback: Option<fn() -> u32>,
|
||||
pub memory: memory::Memory,
|
||||
pub enviroment_call_table: [Option<EnviromentCall>; 256],
|
||||
pub call_stack: CallStack,
|
||||
}
|
||||
use crate::engine::enviroment_calls::EnviromentCall;
|
||||
impl Engine {
|
||||
pub fn set_timer_callback(&mut self, func: fn() -> u32) {
|
||||
self.timer_callback = Some(func);
|
||||
}
|
||||
pub fn set_register(&mut self, register: u8, value: u64) {}
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
pub fn new(program: Vec<u8>) -> Self {
|
||||
let mut mem = memory::Memory::new();
|
||||
for (addr, byte) in program.clone().into_iter().enumerate() {
|
||||
let _ = mem.set_addr8(addr as u64, byte);
|
||||
}
|
||||
trace!("{:?}", mem.read_addr8(0));
|
||||
let ecall_table: [Option<EnviromentCall>; 256] = [None; 256];
|
||||
Self {
|
||||
index: 0,
|
||||
program,
|
||||
registers: Registers::new(),
|
||||
config: EngineConfig::default(),
|
||||
last_timer_count: 0,
|
||||
timer_callback: None,
|
||||
enviroment_call_table: ecall_table,
|
||||
memory: mem,
|
||||
call_stack: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dump(&self) {}
|
||||
pub fn run(&mut self) -> Result<HaltStatus, RuntimeErrors> {
|
||||
Ok(HaltStatus::Halted)
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
#[rustfmt::skip]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Registers {
|
||||
pub a0: u64, pub b0: u64, pub c0: u64, pub d0: u64, pub e0: u64, pub f0: u64,
|
||||
pub a1: u64, pub b1: u64, pub c1: u64, pub d1: u64, pub e1: u64, pub f1: u64,
|
||||
pub a2: u64, pub b2: u64, pub c2: u64, pub d2: u64, pub e2: u64, pub f2: u64,
|
||||
pub a3: u64, pub b3: u64, pub c3: u64, pub d3: u64, pub e3: u64, pub f3: u64,
|
||||
pub a4: u64, pub b4: u64, pub c4: u64, pub d4: u64, pub e4: u64, pub f4: u64,
|
||||
pub a5: u64, pub b5: u64, pub c5: u64, pub d5: u64, pub e5: u64, pub f5: u64,
|
||||
pub a6: u64, pub b6: u64, pub c6: u64, pub d6: u64, pub e6: u64, pub f6: u64,
|
||||
pub a7: u64, pub b7: u64, pub c7: u64, pub d7: u64, pub e7: u64, pub f7: u64,
|
||||
pub a8: u64, pub b8: u64, pub c8: u64, pub d8: u64, pub e8: u64, pub f8: u64,
|
||||
pub a9: u64, pub b9: u64, pub c9: u64, pub d9: u64, pub e9: u64, pub f9: u64,
|
||||
}
|
||||
|
||||
impl Registers {
|
||||
#[rustfmt::skip]
|
||||
pub fn new() -> Self{
|
||||
Self {
|
||||
a0: 0, b0: 0, c0: 0, d0: 0, e0: 0, f0: 0,
|
||||
a1: 0, b1: 0, c1: 0, d1: 0, e1: 0, f1: 0,
|
||||
a2: 0, b2: 0, c2: 0, d2: 0, e2: 0, f2: 0,
|
||||
a3: 0, b3: 0, c3: 0, d3: 0, e3: 0, f3: 0,
|
||||
a4: 0, b4: 0, c4: 0, d4: 0, e4: 0, f4: 0,
|
||||
a5: 0, b5: 0, c5: 0, d5: 0, e5: 0, f5: 0,
|
||||
a6: 0, b6: 0, c6: 0, d6: 0, e6: 0, f6: 0,
|
||||
a7: 0, b7: 0, c7: 0, d7: 0, e7: 0, f7: 0,
|
||||
a8: 0, b8: 0, c8: 0, d8: 0, e8: 0, f8: 0,
|
||||
a9: 0, b9: 0, c9: 0, d9: 0, e9: 0, f9: 0,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,125 +0,0 @@
|
|||
use {
|
||||
super::Engine,
|
||||
crate::{HaltStatus, RuntimeErrors},
|
||||
alloc::vec,
|
||||
RuntimeErrors::*,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn invalid_program() {
|
||||
let prog = vec![1, 0];
|
||||
let mut eng = Engine::new(prog);
|
||||
let ret = eng.run();
|
||||
assert_eq!(ret, Err(InvalidOpcodePair(1, 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_program() {
|
||||
let prog = vec![];
|
||||
let mut eng = Engine::new(prog);
|
||||
let ret = eng.run();
|
||||
assert_eq!(ret, Ok(HaltStatus::Halted));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn max_quantum_reached() {
|
||||
let prog = vec![0, 0, 0, 0];
|
||||
let mut eng = Engine::new(prog);
|
||||
eng.set_timer_callback(|| {
|
||||
return 1;
|
||||
});
|
||||
eng.config.quantum = 1;
|
||||
let ret = eng.run();
|
||||
assert_eq!(ret, Ok(HaltStatus::Running));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jump_out_of_bounds() {
|
||||
use crate::bytecode::ops::Operations::JUMP;
|
||||
let prog = vec![JUMP as u8, 0, 0, 0, 0, 0, 0, 1, 0];
|
||||
let mut eng = Engine::new(prog);
|
||||
let ret = eng.run();
|
||||
assert_eq!(ret, Err(InvalidJumpAddress(256)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_system_call() {
|
||||
let prog = vec![255, 0];
|
||||
let mut eng = Engine::new(prog);
|
||||
let ret = eng.run();
|
||||
assert_eq!(ret, Err(InvalidSystemCall(0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_u8() {
|
||||
use crate::bytecode::ops::{MathOpSides::ConstantConstant, Operations::ADD};
|
||||
|
||||
let prog = vec![ADD as u8, ConstantConstant as u8, 100, 98, 0xA0];
|
||||
let mut eng = Engine::new(prog);
|
||||
let _ = eng.run();
|
||||
assert_eq!(eng.registers.a0, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sub_u8() {
|
||||
use crate::bytecode::ops::Operations::SUB;
|
||||
|
||||
let prog = vec![SUB as u8];
|
||||
let mut eng = Engine::new(prog);
|
||||
let _ = eng.run();
|
||||
assert_eq!(eng.registers.a0, 1);
|
||||
}
|
||||
#[test]
|
||||
fn mul_u8() {
|
||||
use crate::bytecode::ops::{MathOpSides::ConstantConstant, Operations::MUL};
|
||||
|
||||
let prog = vec![MUL as u8, ConstantConstant as u8, 1, 2, 0xA0];
|
||||
let mut eng = Engine::new(prog);
|
||||
let _ = eng.run();
|
||||
assert_eq!(eng.registers.a0, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn div_u8() {
|
||||
use crate::bytecode::ops::Operations::DIV;
|
||||
|
||||
let prog = vec![DIV as u8];
|
||||
let mut eng = Engine::new(prog);
|
||||
let _ = eng.run();
|
||||
assert_eq!(eng.registers.a0, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_register() {
|
||||
let prog = alloc::vec![];
|
||||
let mut eng = Engine::new(prog);
|
||||
eng.set_register(0xA0, 1);
|
||||
assert_eq!(eng.registers.a0, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_u8() {
|
||||
use crate::bytecode::ops::{Operations::LOAD, RWSubTypes::AddrToReg};
|
||||
|
||||
let prog = vec![LOAD as u8, AddrToReg as u8, 0, 0, 0, 0, 0, 0, 1, 0, 0xA0];
|
||||
let mut eng = Engine::new(prog);
|
||||
let ret = eng.memory.set_addr8(256, 1);
|
||||
assert_eq!(ret, Ok(()));
|
||||
let _ = eng.run();
|
||||
assert_eq!(eng.registers.a0, 1);
|
||||
}
|
||||
#[test]
|
||||
fn set_memory_8() {
|
||||
let prog = vec![];
|
||||
let mut eng = Engine::new(prog);
|
||||
let ret = eng.memory.set_addr8(256, 1);
|
||||
assert_eq!(ret, Ok(()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_memory_64() {
|
||||
let prog = vec![];
|
||||
let mut eng = Engine::new(prog);
|
||||
let ret = eng.memory.set_addr64(256, 1);
|
||||
assert_eq!(ret, Ok(()));
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
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}";
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
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;
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
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";
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
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
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
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";
|
||||
}
|
152
hbvm/src/lib.rs
152
hbvm/src/lib.rs
|
@ -1,23 +1,143 @@
|
|||
//! 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
|
||||
|
||||
#![no_std]
|
||||
#![cfg_attr(feature = "nightly", feature(fn_align))]
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
extern crate alloc;
|
||||
|
||||
pub mod bytecode;
|
||||
pub mod engine;
|
||||
pub mod memory;
|
||||
pub mod mem;
|
||||
pub mod value;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum RuntimeErrors {
|
||||
InvalidOpcodePair(u8, u8),
|
||||
RegisterTooSmall,
|
||||
HostError(u64),
|
||||
PageNotMapped(u64),
|
||||
InvalidJumpAddress(u64),
|
||||
InvalidSystemCall(u8),
|
||||
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>,
|
||||
}
|
||||
|
||||
// If you solve the halting problem feel free to remove this
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum HaltStatus {
|
||||
Halted,
|
||||
Running,
|
||||
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,
|
||||
}
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
use hbvm::{
|
||||
bytecode::ops::{Operations::*, RWSubTypes::*},
|
||||
engine::Engine,
|
||||
RuntimeErrors,
|
||||
};
|
||||
|
||||
fn main() -> Result<(), RuntimeErrors> {
|
||||
// TODO: Grab program from cmdline
|
||||
#[rustfmt::skip]
|
||||
let prog: Vec<u8> = vec![
|
||||
NOP as u8,
|
||||
JUMP as u8, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
];
|
||||
|
||||
let mut eng = Engine::new(prog);
|
||||
// eng.set_timer_callback(time);
|
||||
eng.enviroment_call_table[10] = Some(print_fn);
|
||||
eng.run()?;
|
||||
eng.dump();
|
||||
println!("{:#?}", eng.registers);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn time() -> u32 {
|
||||
9
|
||||
}
|
||||
pub fn print_fn(engine: &mut Engine) -> Result<&mut Engine, u64> {
|
||||
println!("hello");
|
||||
Ok(engine)
|
||||
}
|
|
@ -0,0 +1,137 @@
|
|||
//! 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);
|
|
@ -0,0 +1,80 @@
|
|||
//! 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)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,111 @@
|
|||
//! 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
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue