diff --git a/.gitignore b/.gitignore index ae3b53d..f53b832 100644 --- a/.gitignore +++ b/.gitignore @@ -9,5 +9,5 @@ db.sqlite-journal # assets /depell/src/*.gz /depell/src/*.wasm -**/*-sv.rs +#**/*-sv.rs /bytecode/src/instrs.rs diff --git a/Cargo.lock b/Cargo.lock index 2992a05..acbf2f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,21 +2,6 @@ # It is not intended for manual editing. version = 4 -[[package]] -name = "addr2line" -version = "0.24.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" -dependencies = [ - "gimli 0.31.1", -] - -[[package]] -name = "adler2" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" - [[package]] name = "ahash" version = "0.8.11" @@ -29,15 +14,6 @@ dependencies = [ "zerocopy", ] -[[package]] -name = "aho-corasick" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" -dependencies = [ - "memchr", -] - [[package]] name = "allocator-api2" version = "0.2.18" @@ -50,221 +26,18 @@ version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" -[[package]] -name = "arc-swap" -version = "1.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457" - -[[package]] -name = "argon2" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c3610892ee6e0cbce8ae2700349fcf8f98adb0dbfbee85aec3c9179d29cc072" -dependencies = [ - "base64ct", - "blake2", - "cpufeatures", - "password-hash", -] - -[[package]] -name = "async-trait" -version = "0.1.83" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "atomic-waker" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" - [[package]] name = "autocfg" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" -[[package]] -name = "aws-lc-rs" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdd82dba44d209fddb11c190e0a94b78651f95299598e472215667417a03ff1d" -dependencies = [ - "aws-lc-sys", - "mirai-annotations", - "paste", - "zeroize", -] - -[[package]] -name = "aws-lc-sys" -version = "0.22.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df7a4168111d7eb622a31b214057b8509c0a7e1794f44c546d742330dc793972" -dependencies = [ - "bindgen", - "cc", - "cmake", - "dunce", - "fs_extra", - "libc", - "paste", -] - -[[package]] -name = "axum" -version = "0.7.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "504e3947307ac8326a5437504c517c4b56716c9d98fac0028c2acc7ca47d70ae" -dependencies = [ - "async-trait", - "axum-core", - "bytes", - "futures-util", - "http", - "http-body", - "http-body-util", - "hyper", - "hyper-util", - "itoa", - "matchit", - "memchr", - "mime", - "percent-encoding", - "pin-project-lite", - "rustversion", - "serde", - "serde_json", - "serde_path_to_error", - "serde_urlencoded", - "sync_wrapper 1.0.1", - "tokio", - "tower 0.5.1", - "tower-layer", - "tower-service", - "tracing", -] - -[[package]] -name = "axum-core" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09f2bd6146b97ae3359fa0cc6d6b376d9539582c7b4220f041a33ec24c226199" -dependencies = [ - "async-trait", - "bytes", - "futures-util", - "http", - "http-body", - "http-body-util", - "mime", - "pin-project-lite", - "rustversion", - "sync_wrapper 1.0.1", - "tower-layer", - "tower-service", - "tracing", -] - -[[package]] -name = "axum-server" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56bac90848f6a9393ac03c63c640925c4b7c8ca21654de40d53f55964667c7d8" -dependencies = [ - "arc-swap", - "bytes", - "futures-util", - "http", - "http-body", - "http-body-util", - "hyper", - "hyper-util", - "pin-project-lite", - "rustls", - "rustls-pemfile", - "rustls-pki-types", - "tokio", - "tokio-rustls", - "tower 0.4.13", - "tower-service", -] - -[[package]] -name = "backtrace" -version = "0.3.74" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" -dependencies = [ - "addr2line", - "cfg-if", - "libc", - "miniz_oxide", - "object", - "rustc-demangle", - "windows-targets", -] - -[[package]] -name = "base64ct" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" - -[[package]] -name = "bindgen" -version = "0.69.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088" -dependencies = [ - "bitflags", - "cexpr", - "clang-sys", - "itertools", - "lazy_static", - "lazycell", - "log", - "prettyplease", - "proc-macro2", - "quote", - "regex", - "rustc-hash 1.1.0", - "shlex", - "syn", - "which", -] - [[package]] name = "bitflags" version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" -[[package]] -name = "blake2" -version = "0.10.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" -dependencies = [ - "digest", -] - -[[package]] -name = "block-buffer" -version = "0.10.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" -dependencies = [ - "generic-array", -] - [[package]] name = "bumpalo" version = "3.16.0" @@ -274,305 +47,35 @@ dependencies = [ "allocator-api2", ] -[[package]] -name = "bytes" -version = "1.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" - -[[package]] -name = "cc" -version = "1.1.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b16803a61b81d9eabb7eae2588776c4c1e584b738ede45fdbb4c972cec1e9945" -dependencies = [ - "jobserver", - "libc", - "shlex", -] - -[[package]] -name = "cexpr" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" -dependencies = [ - "nom", -] - [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "clang-sys" -version = "1.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4" -dependencies = [ - "glob", - "libc", - "libloading", -] - -[[package]] -name = "cmake" -version = "0.1.51" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb1e43aa7fd152b1f968787f7dbcdeb306d1867ff373c69955211876c053f91a" -dependencies = [ - "cc", -] - -[[package]] -name = "const_format" -version = "0.2.33" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50c655d81ff1114fb0dcdea9225ea9f0cc712a6f8d189378e82bdf62a473a64b" -dependencies = [ - "const_format_proc_macros", -] - -[[package]] -name = "const_format_proc_macros" -version = "0.2.33" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eff1a44b93f47b1bac19a27932f5c591e43d1ba357ee4f61526c8a25603f0eb1" -dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", -] - -[[package]] -name = "cpufeatures" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "608697df725056feaccfa42cffdaeeec3fccc4ffc38358ecd19b243e716a78e0" -dependencies = [ - "libc", -] - -[[package]] -name = "crypto-common" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" -dependencies = [ - "generic-array", - "typenum", -] - -[[package]] -name = "depell" -version = "0.1.0" -dependencies = [ - "argon2", - "axum", - "axum-server", - "const_format", - "getrandom", - "hblang", - "htmlm", - "log", - "rand_core", - "rusqlite", - "serde", - "time", - "tokio", -] - -[[package]] -name = "deranged" -version = "0.3.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" -dependencies = [ - "powerfmt", -] - -[[package]] -name = "digest" -version = "0.10.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" -dependencies = [ - "block-buffer", - "crypto-common", - "subtle", -] - -[[package]] -name = "dunce" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" - -[[package]] -name = "either" -version = "1.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" - [[package]] name = "equivalent" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" -[[package]] -name = "errno" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" -dependencies = [ - "libc", - "windows-sys", -] - [[package]] name = "fallible-iterator" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" -[[package]] -name = "fallible-iterator" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" - -[[package]] -name = "fallible-streaming-iterator" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" - -[[package]] -name = "fnv" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - -[[package]] -name = "form_urlencoded" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" -dependencies = [ - "percent-encoding", -] - -[[package]] -name = "fs_extra" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" - -[[package]] -name = "futures-channel" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" -dependencies = [ - "futures-core", -] - -[[package]] -name = "futures-core" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" - -[[package]] -name = "futures-sink" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" - -[[package]] -name = "futures-task" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" - -[[package]] -name = "futures-util" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" -dependencies = [ - "futures-core", - "futures-task", - "pin-project-lite", - "pin-utils", -] - -[[package]] -name = "generic-array" -version = "0.14.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" -dependencies = [ - "typenum", - "version_check", -] - -[[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.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22030e2c5a68ec659fde1e949a745124b48e6fa8b045b7ed5bd1fe4ccc5c4e5d" dependencies = [ - "fallible-iterator 0.2.0", + "fallible-iterator", "indexmap 1.9.3", "stable_deref_trait", ] -[[package]] -name = "gimli" -version = "0.31.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" - -[[package]] -name = "glob" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" - -[[package]] -name = "h2" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "524e8ac6999421f49a846c2d4411f337e53497d8ec55d67753beffa43c5d9205" -dependencies = [ - "atomic-waker", - "bytes", - "fnv", - "futures-core", - "futures-sink", - "http", - "indexmap 2.6.0", - "slab", - "tokio", - "tokio-util", - "tracing", -] - [[package]] name = "hashbrown" version = "0.12.3" @@ -598,15 +101,6 @@ dependencies = [ "allocator-api2", ] -[[package]] -name = "hashlink" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af" -dependencies = [ - "hashbrown 0.14.5", -] - [[package]] name = "hbbytecode" version = "0.1.0" @@ -643,118 +137,6 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" -[[package]] -name = "hermit-abi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" - -[[package]] -name = "home" -version = "0.5.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" -dependencies = [ - "windows-sys", -] - -[[package]] -name = "htmlm" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a95b97f2c13991e486bf95be6d19c6c3d1fef4f8ec1e298e40aaf98769789295" -dependencies = [ - "htmlm-macro", -] - -[[package]] -name = "htmlm-macro" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f881f4929b944a9566f12d8ac3bf9881325c77c11b9b0adcdc6944018b55ac7" - -[[package]] -name = "http" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - -[[package]] -name = "http-body" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" -dependencies = [ - "bytes", - "http", -] - -[[package]] -name = "http-body-util" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f" -dependencies = [ - "bytes", - "futures-util", - "http", - "http-body", - "pin-project-lite", -] - -[[package]] -name = "httparse" -version = "1.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d71d3574edd2771538b901e6549113b4006ece66150fb69c0fb6d9a2adae946" - -[[package]] -name = "httpdate" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" - -[[package]] -name = "hyper" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50dfd22e0e76d0f662d429a5f80fcaf3855009297eab6a0a9f8543834744ba05" -dependencies = [ - "bytes", - "futures-channel", - "futures-util", - "h2", - "http", - "http-body", - "httparse", - "httpdate", - "itoa", - "pin-project-lite", - "smallvec", - "tokio", -] - -[[package]] -name = "hyper-util" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41296eb09f183ac68eec06e03cdbea2e759633d4067b2f6552fc2e009bcad08b" -dependencies = [ - "bytes", - "futures-util", - "http", - "http-body", - "hyper", - "pin-project-lite", - "tokio", - "tower-service", -] - [[package]] name = "id-arena" version = "2.2.1" @@ -782,42 +164,6 @@ dependencies = [ "serde", ] -[[package]] -name = "itertools" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" -dependencies = [ - "either", -] - -[[package]] -name = "itoa" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" - -[[package]] -name = "jobserver" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48d1dbcbbeb6a7fec7e059840aa538bd62aaccf972c7346c4d9d2059312853d0" -dependencies = [ - "libc", -] - -[[package]] -name = "lazy_static" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" - -[[package]] -name = "lazycell" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" - [[package]] name = "leb128" version = "0.2.5" @@ -830,51 +176,12 @@ version = "0.2.159" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" -[[package]] -name = "libloading" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" -dependencies = [ - "cfg-if", - "windows-targets", -] - -[[package]] -name = "libsqlite3-sys" -version = "0.30.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149" -dependencies = [ - "cc", - "pkg-config", - "vcpkg", -] - -[[package]] -name = "linux-raw-sys" -version = "0.4.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" - [[package]] name = "log" version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" -[[package]] -name = "matchit" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" - -[[package]] -name = "memchr" -version = "2.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" - [[package]] name = "memmap2" version = "0.9.5" @@ -884,153 +191,12 @@ dependencies = [ "libc", ] -[[package]] -name = "mime" -version = "0.3.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" - -[[package]] -name = "minimal-lexical" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" - -[[package]] -name = "miniz_oxide" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" -dependencies = [ - "adler2", -] - -[[package]] -name = "mio" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" -dependencies = [ - "hermit-abi", - "libc", - "wasi", - "windows-sys", -] - -[[package]] -name = "mirai-annotations" -version = "1.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9be0862c1b3f26a88803c4a49de6889c10e608b3ee9344e6ef5b45fb37ad3d1" - -[[package]] -name = "nom" -version = "7.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" -dependencies = [ - "memchr", - "minimal-lexical", -] - -[[package]] -name = "num-conv" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" - -[[package]] -name = "object" -version = "0.36.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" -dependencies = [ - "memchr", -] - [[package]] name = "once_cell" version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" -[[package]] -name = "password-hash" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "346f04948ba92c43e8469c1ee6736c7563d71012b17d40745260fe106aac2166" -dependencies = [ - "base64ct", - "rand_core", - "subtle", -] - -[[package]] -name = "paste" -version = "1.0.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" - -[[package]] -name = "percent-encoding" -version = "2.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" - -[[package]] -name = "pin-project" -version = "1.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf123a161dde1e524adf36f90bc5d8d3462824a9c43553ad07a8183161189ec" -dependencies = [ - "pin-project-internal", -] - -[[package]] -name = "pin-project-internal" -version = "1.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4502d8515ca9f32f1fb543d987f63d95a14934883db45bdb48060b6b69257f8" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "pin-project-lite" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" - -[[package]] -name = "pin-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" - -[[package]] -name = "pkg-config" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" - -[[package]] -name = "powerfmt" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" - -[[package]] -name = "prettyplease" -version = "0.2.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "479cf940fbbb3426c32c5d5176f62ad57549a0bb84773423ba8be9d089f5faba" -dependencies = [ - "proc-macro2", - "syn", -] - [[package]] name = "proc-macro2" version = "1.0.87" @@ -1049,15 +215,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rand_core" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" -dependencies = [ - "getrandom", -] - [[package]] name = "regalloc2" version = "0.10.2" @@ -1067,152 +224,16 @@ dependencies = [ "bumpalo", "hashbrown 0.14.5", "log", - "rustc-hash 2.0.0", + "rustc-hash", "smallvec", ] -[[package]] -name = "regex" -version = "1.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", -] - -[[package]] -name = "regex-automata" -version = "0.4.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" - -[[package]] -name = "ring" -version = "0.17.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" -dependencies = [ - "cc", - "cfg-if", - "getrandom", - "libc", - "spin", - "untrusted", - "windows-sys", -] - -[[package]] -name = "rusqlite" -version = "0.32.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7753b721174eb8ff87a9a0e799e2d7bc3749323e773db92e0984debb00019d6e" -dependencies = [ - "bitflags", - "fallible-iterator 0.3.0", - "fallible-streaming-iterator", - "hashlink", - "libsqlite3-sys", - "smallvec", -] - -[[package]] -name = "rustc-demangle" -version = "0.1.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" - -[[package]] -name = "rustc-hash" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" - [[package]] name = "rustc-hash" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" -[[package]] -name = "rustix" -version = "0.38.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys", - "windows-sys", -] - -[[package]] -name = "rustls" -version = "0.23.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "415d9944693cb90382053259f89fbb077ea730ad7273047ec63b19bc9b160ba8" -dependencies = [ - "aws-lc-rs", - "once_cell", - "rustls-pki-types", - "rustls-webpki", - "subtle", - "zeroize", -] - -[[package]] -name = "rustls-pemfile" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" -dependencies = [ - "rustls-pki-types", -] - -[[package]] -name = "rustls-pki-types" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e696e35370c65c9c541198af4543ccd580cf17fc25d8e05c5a242b202488c55" - -[[package]] -name = "rustls-webpki" -version = "0.102.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64ca1bc8749bd4cf37b5ce386cc146580777b4e8572c7b97baf22c83f444bee9" -dependencies = [ - "aws-lc-rs", - "ring", - "rustls-pki-types", - "untrusted", -] - -[[package]] -name = "rustversion" -version = "1.0.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" - -[[package]] -name = "ryu" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" - [[package]] name = "semver" version = "1.0.23" @@ -1239,89 +260,18 @@ dependencies = [ "syn", ] -[[package]] -name = "serde_json" -version = "1.0.128" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" -dependencies = [ - "itoa", - "memchr", - "ryu", - "serde", -] - -[[package]] -name = "serde_path_to_error" -version = "0.1.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af99884400da37c88f5e9146b7f1fd0fbcae8f6eec4e9da38b67d05486f814a6" -dependencies = [ - "itoa", - "serde", -] - -[[package]] -name = "serde_urlencoded" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" -dependencies = [ - "form_urlencoded", - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "shlex" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - -[[package]] -name = "slab" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" -dependencies = [ - "autocfg", -] - [[package]] name = "smallvec" version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" -[[package]] -name = "socket2" -version = "0.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" -dependencies = [ - "libc", - "windows-sys", -] - -[[package]] -name = "spin" -version = "0.9.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" - [[package]] name = "stable_deref_trait" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" -[[package]] -name = "subtle" -version = "2.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" - [[package]] name = "syn" version = "2.0.79" @@ -1333,181 +283,12 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "sync_wrapper" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" - -[[package]] -name = "sync_wrapper" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" - -[[package]] -name = "time" -version = "0.3.36" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" -dependencies = [ - "deranged", - "num-conv", - "powerfmt", - "serde", - "time-core", -] - -[[package]] -name = "time-core" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" - -[[package]] -name = "tokio" -version = "1.40.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998" -dependencies = [ - "backtrace", - "bytes", - "libc", - "mio", - "pin-project-lite", - "socket2", - "tokio-macros", - "windows-sys", -] - -[[package]] -name = "tokio-macros" -version = "2.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "tokio-rustls" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4" -dependencies = [ - "rustls", - "rustls-pki-types", - "tokio", -] - -[[package]] -name = "tokio-util" -version = "0.7.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a" -dependencies = [ - "bytes", - "futures-core", - "futures-sink", - "pin-project-lite", - "tokio", -] - -[[package]] -name = "tower" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" -dependencies = [ - "futures-core", - "futures-util", - "pin-project", - "pin-project-lite", - "tower-layer", - "tower-service", - "tracing", -] - -[[package]] -name = "tower" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2873938d487c3cfb9aed7546dc9f2711d867c9f90c46b889989a2cb84eba6b4f" -dependencies = [ - "futures-core", - "futures-util", - "pin-project-lite", - "sync_wrapper 0.1.2", - "tokio", - "tower-layer", - "tower-service", - "tracing", -] - -[[package]] -name = "tower-layer" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" - -[[package]] -name = "tower-service" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" - -[[package]] -name = "tracing" -version = "0.1.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" -dependencies = [ - "log", - "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", -] - -[[package]] -name = "typenum" -version = "1.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" - [[package]] name = "unicode-ident" version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" -[[package]] -name = "unicode-xid" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" - -[[package]] -name = "untrusted" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" - -[[package]] -name = "vcpkg" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" - [[package]] name = "version_check" version = "0.9.5" @@ -1521,7 +302,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d68aa3c7b80be75c8458fc087453e5a31a226cfffede2e9b932393b2ea1c624a" dependencies = [ "anyhow", - "gimli 0.26.2", + "gimli", "id-arena", "leb128", "log", @@ -1542,12 +323,6 @@ dependencies = [ "syn", ] -[[package]] -name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" - [[package]] name = "wasm-encoder" version = "0.212.0" @@ -1557,31 +332,6 @@ dependencies = [ "leb128", ] -[[package]] -name = "wasm-hbc" -version = "0.1.0" -dependencies = [ - "hblang", - "hbvm", - "log", - "wasm-rt", -] - -[[package]] -name = "wasm-hbfmt" -version = "0.1.0" -dependencies = [ - "hblang", - "wasm-rt", -] - -[[package]] -name = "wasm-rt" -version = "0.1.0" -dependencies = [ - "log", -] - [[package]] name = "wasmparser" version = "0.212.0" @@ -1596,91 +346,6 @@ dependencies = [ "serde", ] -[[package]] -name = "which" -version = "4.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" -dependencies = [ - "either", - "home", - "once_cell", - "rustix", -] - -[[package]] -name = "windows-sys" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-targets" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" - -[[package]] -name = "windows_i686_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" - -[[package]] -name = "windows_i686_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" - [[package]] name = "xtask" version = "0.1.0" @@ -1708,9 +373,3 @@ dependencies = [ "quote", "syn", ] - -[[package]] -name = "zeroize" -version = "1.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" diff --git a/Cargo.toml b/Cargo.toml index d4c33a3..68e8a0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,10 @@ members = [ "xrt", "xtask", "lang", - "depell", - "depell/wasm-fmt", - "depell/wasm-hbc", - "depell/wasm-rt", + #"depell", + #"depell/wasm-fmt", + #"depell/wasm-hbc", + #"depell/wasm-rt", ] [workspace.dependencies] diff --git a/lang/src/regalloc.rs b/lang/src/regalloc.rs index ecaccf7..139597f 100644 --- a/lang/src/regalloc.rs +++ b/lang/src/regalloc.rs @@ -1,150 +1,2 @@ -use {crate::reg::Reg, alloc::vec::Vec, core::ops::Range}; -type Nid = u16; -pub trait Ctx { - fn uses_of(&self, nid: Nid) -> impl Iterator; - fn params_of(&self, nid: Nid) -> impl Iterator; - fn args_of(&self, nid: Nid) -> impl Iterator; - fn dom_of(&self, nid: Nid) -> Nid; -} - -pub struct Env<'a, C: Ctx> { - ctx: &'a C, - func: &'a Func, - res: &'a mut Res, -} - -impl<'a, C: Ctx> Env<'a, C> { - pub fn new(ctx: &'a C, func: &'a Func, res: &'a mut Res) -> Self { - Self { ctx, func, res } - } - - pub fn run(&mut self) { - self.res.reg_to_node.clear(); - self.res.reg_to_node.resize(self.func.instrs.len(), 0); - - let mut bundle = Bundle::new(self.func.instrs.len()); - for &inst in &self.func.instrs { - for uinst in self.ctx.uses_of(inst) { - let mut cursor = self.ctx.dom_of(uinst); - while cursor != self.ctx.dom_of(inst) { - let mut range = self.func.blocks - [self.func.id_to_block[cursor as usize] as usize] - .range - .clone(); - range.start = range.start.max(inst as usize); - range.end = range.end.min(uinst as usize); - bundle.add(range); - cursor = self.ctx.dom_of(cursor); - } - } - - match self.res.bundles.iter_mut().enumerate().find(|(_, b)| !b.overlaps(&bundle)) { - Some((i, other)) => { - other.merge(&bundle); - bundle.clear(); - self.res.reg_to_node[inst as usize] = i as Reg; - } - None => { - self.res.reg_to_node[inst as usize] = self.res.bundles.len() as Reg; - self.res.bundles.push(bundle); - bundle = Bundle::new(self.func.instrs.len()); - } - } - } - } -} - -pub struct Res { - bundles: Vec, - pub reg_to_node: Vec, -} - -pub struct Bundle { - //unit_range: Range, - //set: BitSet, - taken: Vec, -} - -impl Bundle { - fn new(size: usize) -> Self { - Self { taken: vec![false; size] } - } - - fn add(&mut self, range: Range) { - self.taken[range].fill(true); - } - - fn overlaps(&self, other: &Self) -> bool { - self.taken.iter().zip(other.taken.iter()).any(|(a, b)| a & b) - } - - fn merge(&mut self, other: &Self) { - debug_assert!(!self.overlaps(other)); - self.taken.iter_mut().zip(other.taken.iter()).for_each(|(a, b)| *a = *b); - } - - fn clear(&mut self) { - self.taken.fill(false); - } - - //fn overlaps(&self, other: &Self) -> bool { - // if self.unit_range.start >= other.unit_range.end - // || self.unit_range.end <= other.unit_range.start - // { - // return false; - // } - - // let [mut a, mut b] = [self, other]; - // if a.unit_range.start > b.unit_range.start { - // mem::swap(&mut a, &mut b); - // } - // let [mut tmp_a, mut tmp_b] = [0; 2]; - // let [units_a, units_b] = [a.set.units(&mut tmp_a), b.set.units(&mut tmp_b)]; - // let len = a.unit_range.len().min(b.unit_range.len()); - // let [units_a, units_b] = - // [&units_a[b.unit_range.start - a.unit_range.start..][..len], &units_b[..len]]; - // units_a.iter().zip(units_b).any(|(&a, &b)| a & b != 0) - //} - - //fn merge(mut self, mut other: Self) -> Self { - // debug_assert!(!self.overlaps(&other)); - - // if self.unit_range.start > other.unit_range.start { - // mem::swap(&mut self, &mut other); - // } - - // let final_range = self.unit_range.start..self.unit_range.end.max(other.unit_range.end); - - // self.set.reserve(final_range.len()); - - // let mut tmp = 0; - // let other_units = other.set.units(&mut tmp); - - // match self.set.units_mut() { - // Ok(units) => { - // units[other.unit_range.start - self.unit_range.start..] - // .iter_mut() - // .zip(other_units) - // .for_each(|(a, b)| *a |= b); - // } - // Err(view) => view.add_mask(tmp), - // } - - // self - //} -} - -pub struct Func { - pub blocks: Vec, - pub instrs: Vec, - pub id_to_instr: Vec, - pub id_to_block: Vec, -} - -pub struct Block { - pub range: Range, - pub start_id: Nid, - pub eld_id: Nid, -} diff --git a/lang/src/son.rs b/lang/src/son.rs index 6097a88..fdf63a1 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -44,7 +44,7 @@ type Nid = u16; type AClassId = i16; pub struct AssemblySpec { - entry: u64, + entry: u32, code_length: u64, data_length: u64, } @@ -118,16 +118,16 @@ impl Default for Nodes { } impl Nodes { - fn loop_depth(&mut self, target: Nid) -> LoopDepth { - if self[target].loop_depth != 0 { - return self[target].loop_depth; + fn loop_depth(&self, target: Nid) -> LoopDepth { + if self[target].loop_depth.get() != 0 { + return self[target].loop_depth.get(); } - self[target].loop_depth = match self[target].kind { + self[target].loop_depth.set(match self[target].kind { Kind::Entry | Kind::Then | Kind::Else | Kind::Call { .. } | Kind::Return | Kind::If => { let dpth = self.loop_depth(self[target].inputs[0]); - if self[target].loop_depth != 0 { - return self[target].loop_depth; + if self[target].loop_depth.get() != 0 { + return self[target].loop_depth.get(); } dpth } @@ -139,10 +139,10 @@ impl Nodes { } Kind::Loop => { let depth = Self::loop_depth(self, self[target].inputs[0]) + 1; - self[target].loop_depth = depth; + self[target].loop_depth.set(depth); let mut cursor = self[target].inputs[1]; while cursor != target { - self[cursor].loop_depth = depth; + self[cursor].loop_depth.set(depth); let next = if self[cursor].kind == Kind::Region { self.loop_depth(self[cursor].inputs[0]); self[cursor].inputs[1] @@ -156,8 +156,8 @@ impl Nodes { .iter() .find(|&&n| self[n].kind != self[cursor].kind) .unwrap(); - if self[other].loop_depth == 0 { - self[other].loop_depth = depth - 1; + if self[other].loop_depth.get() == 0 { + self[other].loop_depth.set(depth - 1); } } cursor = next; @@ -166,9 +166,9 @@ impl Nodes { } Kind::Start | Kind::End | Kind::Die => 1, u => unreachable!("{u:?}"), - }; + }); - self[target].loop_depth + self[target].loop_depth.get() } fn idepth(&self, target: Nid) -> IDomDepth { @@ -444,7 +444,7 @@ impl Nodes { self[to].inputs.push(from); } - fn use_block(&mut self, target: Nid, from: Nid) -> Nid { + fn use_block(&self, target: Nid, from: Nid) -> Nid { if self[from].kind != Kind::Phi { return self.idom(from); } @@ -1428,7 +1428,7 @@ impl Nodes { out, "region{node}: {} {} {:?}", self[node].depth.get(), - self[node].loop_depth, + self[node].loop_depth.get(), self[node].inputs )?; let mut cfg_index = Nid::MAX; @@ -1445,7 +1445,7 @@ impl Nodes { out, "loop{node}: {} {} {:?}", self[node].depth.get(), - self[node].loop_depth, + self[node].loop_depth.get(), self[node].outputs )?; let mut cfg_index = Nid::MAX; @@ -1465,7 +1465,7 @@ impl Nodes { out, "b{node}: {} {} {:?}", self[node].depth.get(), - self[node].loop_depth, + self[node].loop_depth.get(), self[node].outputs )?; let mut cfg_index = Nid::MAX; @@ -1621,6 +1621,15 @@ impl Nodes { dominated = self.idom(dominated); } } + + fn is_data_dep(&self, nid: Nid, n: Nid) -> bool { + match self[n].kind { + _ if self.is_cfg(n) && !matches!(self[n].kind, Kind::Call { .. }) => false, + Kind::Stre => self[n].inputs[3] != nid, + Kind::Load => self[n].inputs[2] != nid, + _ => self[n].inputs[0] != nid || self[n].inputs[1..].contains(&nid), + } + } } enum CondOptRes { @@ -1737,6 +1746,10 @@ impl Kind { matches!(self, Self::Return | Self::If | Self::End | Self::Die) } + fn starts_basic_block(&self) -> bool { + matches!(self, Self::Region | Self::Loop | Self::Start | Kind::Then | Kind::Else) + } + fn is_peeped(&self) -> bool { !matches!(self, Self::End | Self::Arg | Self::Mem | Self::Loops) } @@ -1766,7 +1779,7 @@ pub struct Node { ty: ty::Id, depth: Cell, lock_rc: LockRc, - loop_depth: LoopDepth, + loop_depth: Cell, aclass: AClassId, antidep: Nid, } @@ -1793,9 +1806,12 @@ impl Node { fn is_mem(&self) -> bool { matches!(self.kind, Kind::Stre | Kind::Load | Kind::Stck) } + + fn is_data_phi(&self) -> bool { + self.kind == Kind::Phi && self.ty != ty::Id::VOID + } } -type RallocBRef = u16; type LoopDepth = u16; type LockRc = u16; type IDomDepth = u16; @@ -2272,8 +2288,7 @@ impl<'a> Codegen<'a> { // TODO: return them back - let entry = - self.ct_backend.assemble_reachable(fuc, self.tys, &mut self.ct.code).entry as u32; + let entry = self.ct_backend.assemble_reachable(fuc, self.tys, &mut self.ct.code).entry; #[cfg(debug_assertions)] { @@ -4516,10 +4531,9 @@ mod tests { let err = codegen.disasm(output, &out); if let Err(e) = err { writeln!(output, "!!! asm is invalid: {e}").unwrap(); - return; + } else { + super::hbvm::test_run_vm(&out, output); } - - super::hbvm::test_run_vm(&out, output); } crate::run_tests! { generate: diff --git a/lang/src/son/hbvm.rs b/lang/src/son/hbvm.rs index ae11bee..5d9d173 100644 --- a/lang/src/son/hbvm.rs +++ b/lang/src/son/hbvm.rs @@ -1,18 +1,20 @@ use { - super::{AssemblySpec, Backend, ItemCtx, Nid, Nodes, RallocBRef, ARG_START, NEVER, VOID}, + super::{AssemblySpec, Backend, Nid, Node, Nodes}, crate::{ lexer::TokenKind, parser, reg, - son::{write_reloc, Kind, MEM}, - ty::{self, Arg, Loc}, - utils::{BitSet, Vc}, - HashMap, Offset, PLoc, Reloc, Sig, Size, TypedReloc, Types, + son::{debug_assert_matches, write_reloc, Kind, MEM}, + ty::{self, Loc}, + Offset, Reloc, Size, TypedReloc, Types, }, - alloc::{borrow::ToOwned, boxed::Box, collections::BTreeMap, string::String, vec::Vec}, - core::{assert_matches::debug_assert_matches, mem}, + alloc::{boxed::Box, collections::BTreeMap, string::String, vec::Vec}, + core::mem, hbbytecode::{self as instrs, *}, }; +mod my_regalloc; +mod their_regalloc; + struct FuncDt { offset: Offset, // TODO: change to indices into common vec @@ -43,39 +45,13 @@ struct Assembler { funcs: Vec, } -struct Regalloc { - env: regalloc2::MachineEnv, - ctx: regalloc2::Ctx, -} - -impl Default for Regalloc { - fn default() -> Self { - Self { - env: regalloc2::MachineEnv { - preferred_regs_by_class: [ - (1..13).map(|i| regalloc2::PReg::new(i, regalloc2::RegClass::Int)).collect(), - vec![], - vec![], - ], - non_preferred_regs_by_class: [ - (13..64).map(|i| regalloc2::PReg::new(i, regalloc2::RegClass::Int)).collect(), - vec![], - vec![], - ], - scratch_by_class: Default::default(), - fixed_stack_slots: Default::default(), - }, - ctx: Default::default(), - } - } -} - #[derive(Default)] pub struct HbvmBackend { funcs: Vec, globals: Vec, asm: Assembler, - ralloc: Regalloc, + ralloc: their_regalloc::Regalloc, + ralloc_my: my_regalloc::Res, ret_relocs: Vec, relocs: Vec, @@ -88,391 +64,6 @@ impl HbvmBackend { fn emit(&mut self, instr: (usize, [u8; instrs::MAX_SIZE])) { emit(&mut self.code, instr); } - - fn emit_body_code( - &mut self, - nodes: &mut Nodes, - sig: Sig, - tys: &Types, - files: &[parser::Ast], - ) -> (usize, bool) { - let mut ralloc = mem::take(&mut self.ralloc); - - let fuc = Function::new(nodes, tys, sig); - log::info!("{:?}", fuc); - if !fuc.tail { - mem::swap( - &mut ralloc.env.preferred_regs_by_class, - &mut ralloc.env.non_preferred_regs_by_class, - ); - }; - - let options = regalloc2::RegallocOptions { - verbose_log: false, - validate_ssa: cfg!(debug_assertions), - algorithm: regalloc2::Algorithm::Ion, - }; - regalloc2::run_with_ctx(&fuc, &ralloc.env, &options, &mut ralloc.ctx).unwrap_or_else( - |err| { - if let regalloc2::RegAllocError::SSA(vreg, inst) = err { - fuc.nodes[vreg.vreg() as Nid].lock_rc = Nid::MAX; - fuc.nodes[fuc.instrs[inst.index()].nid].lock_rc = Nid::MAX - 1; - } - fuc.nodes.graphviz_in_browser(ty::Display::new(tys, files, ty::Id::VOID)); - panic!("{err}") - }, - ); - - if !fuc.tail { - mem::swap( - &mut ralloc.env.preferred_regs_by_class, - &mut ralloc.env.non_preferred_regs_by_class, - ); - }; - - let mut saved_regs = HashMap::::default(); - let mut atr = |allc: regalloc2::Allocation| { - debug_assert!(allc.is_reg()); - let hvenc = regalloc2::PReg::from_index(allc.index()).hw_enc() as u8; - if hvenc <= 12 { - return hvenc; - } - let would_insert = saved_regs.len() as u8 + reg::RET_ADDR + 1; - *saved_regs.entry(hvenc).or_insert(would_insert) - }; - - '_open_function: { - self.emit(instrs::addi64(reg::STACK_PTR, reg::STACK_PTR, 0)); - self.emit(instrs::st(reg::RET_ADDR + fuc.tail as u8, reg::STACK_PTR, 0, 0)); - } - - let (retl, mut parama) = tys.parama(sig.ret); - let mut typs = sig.args.args(); - let mut args = fuc.nodes[VOID].outputs[ARG_START..].iter(); - while let Some(aty) = typs.next(tys) { - let Arg::Value(ty) = aty else { continue }; - let Some(loc) = parama.next(ty, tys) else { continue }; - let &arg = args.next().unwrap(); - let (rg, size) = match loc { - PLoc::WideReg(rg, size) => (rg, size), - PLoc::Reg(rg, size) if ty.loc(tys) == Loc::Stack => (rg, size), - PLoc::Reg(..) | PLoc::Ref(..) => continue, - }; - self.emit(instrs::st(rg, reg::STACK_PTR, self.offsets[arg as usize] as _, size)); - if fuc.nodes[arg].lock_rc == 0 { - self.emit(instrs::addi64(rg, reg::STACK_PTR, self.offsets[arg as usize] as _)); - } - } - - for (i, block) in fuc.blocks.iter().enumerate() { - let blk = regalloc2::Block(i as _); - self.offsets[block.nid as usize] = self.code.len() as _; - for instr_or_edit in ralloc.ctx.output.block_insts_and_edits(&fuc, blk) { - let inst = match instr_or_edit { - regalloc2::InstOrEdit::Inst(inst) => inst, - regalloc2::InstOrEdit::Edit(®alloc2::Edit::Move { from, to }) => { - self.emit(instrs::cp(atr(to), atr(from))); - continue; - } - }; - - let nid = fuc.instrs[inst.index()].nid; - if nid == NEVER { - continue; - }; - let allocs = ralloc.ctx.output.inst_allocs(inst); - let node = &fuc.nodes[nid]; - let backref = fuc.backrefs[nid as usize]; - - let mut extend = |base: ty::Id, dest: ty::Id, from: usize, to: usize| { - let (bsize, dsize) = (tys.size_of(base), tys.size_of(dest)); - debug_assert!(bsize <= 8, "{}", ty::Display::new(tys, files, base)); - debug_assert!(dsize <= 8, "{}", ty::Display::new(tys, files, dest)); - if bsize == dsize { - return Default::default(); - } - match (base.is_signed(), dest.is_signed()) { - (true, true) => { - let op = [instrs::sxt8, instrs::sxt16, instrs::sxt32] - [bsize.ilog2() as usize]; - op(atr(allocs[to]), atr(allocs[from])) - } - _ => { - let mask = (1u64 << (bsize * 8)) - 1; - instrs::andi(atr(allocs[to]), atr(allocs[from]), mask) - } - } - }; - - match node.kind { - Kind::If => { - let &[_, cnd] = node.inputs.as_slice() else { unreachable!() }; - if let Kind::BinOp { op } = fuc.nodes[cnd].kind - && let Some((op, swapped)) = - op.cond_op(fuc.nodes[fuc.nodes[cnd].inputs[1]].ty) - { - let &[lhs, rhs] = allocs else { unreachable!() }; - let &[_, lh, rh] = fuc.nodes[cnd].inputs.as_slice() else { - unreachable!() - }; - - self.emit(extend(fuc.nodes[lh].ty, fuc.nodes[lh].ty.extend(), 0, 0)); - self.emit(extend(fuc.nodes[rh].ty, fuc.nodes[rh].ty.extend(), 1, 1)); - - let rel = Reloc::new(self.code.len(), 3, 2); - self.jump_relocs.push((node.outputs[!swapped as usize], rel)); - self.emit(op(atr(lhs), atr(rhs), 0)); - } else { - self.emit(extend(fuc.nodes[cnd].ty, fuc.nodes[cnd].ty.extend(), 0, 0)); - let rel = Reloc::new(self.code.len(), 3, 2); - self.jump_relocs.push((node.outputs[0], rel)); - self.emit(instrs::jne(atr(allocs[0]), reg::ZERO, 0)); - } - } - Kind::Loop | Kind::Region => { - if backref as usize != i + 1 { - let rel = Reloc::new(self.code.len(), 1, 4); - self.jump_relocs.push((nid, rel)); - self.emit(instrs::jmp(0)); - } - } - Kind::Return => { - match retl { - Some(PLoc::Reg(r, size)) if sig.ret.loc(tys) == Loc::Stack => { - self.emit(instrs::ld(r, atr(allocs[0]), 0, size)) - } - None | Some(PLoc::Reg(..)) => {} - Some(PLoc::WideReg(r, size)) => { - self.emit(instrs::ld(r, atr(allocs[0]), 0, size)) - } - Some(PLoc::Ref(_, size)) => { - let [src, dst] = [atr(allocs[0]), atr(allocs[1])]; - if let Ok(size) = u16::try_from(size) { - self.emit(instrs::bmc(src, dst, size)); - } else { - for _ in 0..size / u16::MAX as u32 { - self.emit(instrs::bmc(src, dst, u16::MAX)); - self.emit(instrs::addi64(src, src, u16::MAX as _)); - self.emit(instrs::addi64(dst, dst, u16::MAX as _)); - } - self.emit(instrs::bmc(src, dst, size as u16)); - self.emit(instrs::addi64(src, src, size.wrapping_neg() as _)); - self.emit(instrs::addi64(dst, dst, size.wrapping_neg() as _)); - } - } - } - - if i != fuc.blocks.len() - 1 { - let rel = Reloc::new(self.code.len(), 1, 4); - self.ret_relocs.push(rel); - self.emit(instrs::jmp(0)); - } - } - Kind::Die => { - self.emit(instrs::un()); - } - Kind::CInt { value } if node.ty.is_float() => { - self.emit(match node.ty { - ty::Id::F32 => instrs::li32( - atr(allocs[0]), - (f64::from_bits(value as _) as f32).to_bits(), - ), - ty::Id::F64 => instrs::li64(atr(allocs[0]), value as _), - _ => unreachable!(), - }); - } - Kind::CInt { value } => self.emit(match tys.size_of(node.ty) { - 1 => instrs::li8(atr(allocs[0]), value as _), - 2 => instrs::li16(atr(allocs[0]), value as _), - 4 => instrs::li32(atr(allocs[0]), value as _), - _ => instrs::li64(atr(allocs[0]), value as _), - }), - Kind::UnOp { op } => { - let op = op - .unop(node.ty, fuc.nodes[node.inputs[1]].ty) - .expect("TODO: unary operator not supported"); - let &[dst, oper] = allocs else { unreachable!() }; - self.emit(op(atr(dst), atr(oper))); - } - Kind::BinOp { .. } if node.lock_rc != 0 => {} - Kind::BinOp { op } => { - let &[.., lh, rh] = node.inputs.as_slice() else { unreachable!() }; - - if let Kind::CInt { value } = fuc.nodes[rh].kind - && fuc.nodes[rh].lock_rc != 0 - && let Some(op) = op.imm_binop(node.ty) - { - let &[dst, lhs] = allocs else { unreachable!() }; - self.emit(op(atr(dst), atr(lhs), value as _)); - } else if let Some(op) = - op.binop(node.ty).or(op.float_cmp(fuc.nodes[lh].ty)) - { - let &[dst, lhs, rhs] = allocs else { unreachable!() }; - self.emit(op(atr(dst), atr(lhs), atr(rhs))); - } else if let Some(against) = op.cmp_against() { - let op_ty = fuc.nodes[rh].ty; - - self.emit(extend(fuc.nodes[lh].ty, fuc.nodes[lh].ty.extend(), 0, 0)); - self.emit(extend(fuc.nodes[rh].ty, fuc.nodes[rh].ty.extend(), 1, 1)); - let &[dst, lhs, rhs] = allocs else { unreachable!() }; - - if op_ty.is_float() && matches!(op, TokenKind::Le | TokenKind::Ge) { - let opop = match op { - TokenKind::Le => TokenKind::Gt, - TokenKind::Ge => TokenKind::Lt, - _ => unreachable!(), - }; - let op_fn = opop.float_cmp(op_ty).unwrap(); - self.emit(op_fn(atr(dst), atr(lhs), atr(rhs))); - self.emit(instrs::not(atr(dst), atr(dst))); - } else { - let op_fn = - if op_ty.is_signed() { instrs::cmps } else { instrs::cmpu }; - self.emit(op_fn(atr(dst), atr(lhs), atr(rhs))); - self.emit(instrs::cmpui(atr(dst), atr(dst), against)); - if matches!(op, TokenKind::Eq | TokenKind::Lt | TokenKind::Gt) { - self.emit(instrs::not(atr(dst), atr(dst))); - } - } - } else { - todo!("unhandled operator: {op}"); - } - } - Kind::Call { args, func } => { - let (ret, mut parama) = tys.parama(node.ty); - let has_ret = ret.is_some() as usize; - let mut args = args.args(); - let mut allocs = allocs[has_ret..].iter(); - while let Some(arg) = args.next(tys) { - let Arg::Value(ty) = arg else { continue }; - let Some(loc) = parama.next(ty, tys) else { continue }; - - let &arg = allocs.next().unwrap(); - let (rg, size) = match loc { - PLoc::Reg(rg, size) if ty.loc(tys) == Loc::Stack => (rg, size), - PLoc::WideReg(rg, size) => (rg, size), - PLoc::Ref(..) | PLoc::Reg(..) => continue, - }; - if size > 8 { - allocs.next().unwrap(); - } - self.emit(instrs::ld(rg, atr(arg), 0, size)); - } - - debug_assert!( - !matches!(ret, Some(PLoc::Ref(..))) || allocs.next().is_some() - ); - - if func == ty::ECA { - self.emit(instrs::eca()); - } else { - self.relocs.push(TypedReloc { - target: ty::Kind::Func(func).compress(), - reloc: Reloc::new(self.code.len(), 3, 4), - }); - self.emit(instrs::jal(reg::RET_ADDR, reg::ZERO, 0)); - } - - if let Some(PLoc::WideReg(r, size)) = ret { - debug_assert_eq!( - fuc.nodes[*node.inputs.last().unwrap()].kind, - Kind::Stck - ); - let stck = self.offsets[*node.inputs.last().unwrap() as usize]; - self.emit(instrs::st(r, reg::STACK_PTR, stck as _, size)); - } - if let Some(PLoc::Reg(r, size)) = ret - && node.ty.loc(tys) == Loc::Stack - { - debug_assert_eq!( - fuc.nodes[*node.inputs.last().unwrap()].kind, - Kind::Stck - ); - let stck = self.offsets[*node.inputs.last().unwrap() as usize]; - self.emit(instrs::st(r, reg::STACK_PTR, stck as _, size)); - } - } - Kind::Global { global } => { - let reloc = Reloc::new(self.code.len(), 3, 4); - self.relocs.push(TypedReloc { - target: ty::Kind::Global(global).compress(), - reloc, - }); - self.emit(instrs::lra(atr(allocs[0]), 0, 0)); - } - Kind::Stck => { - let base = reg::STACK_PTR; - let offset = self.offsets[nid as usize]; - self.emit(instrs::addi64(atr(allocs[0]), base, offset as _)); - } - Kind::Load => { - let mut region = node.inputs[1]; - let mut offset = 0; - if fuc.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add }) - && let Kind::CInt { value } = - fuc.nodes[fuc.nodes[region].inputs[2]].kind - { - region = fuc.nodes[region].inputs[1]; - offset = value as Offset; - } - let size = tys.size_of(node.ty); - if node.ty.loc(tys) != Loc::Stack { - let (base, offset) = match fuc.nodes[region].kind { - Kind::Stck => { - (reg::STACK_PTR, self.offsets[region as usize] + offset) - } - _ => (atr(allocs[1]), offset), - }; - self.emit(instrs::ld(atr(allocs[0]), base, offset as _, size as _)); - } - } - Kind::Stre if node.inputs[1] == VOID => {} - Kind::Stre => { - let mut region = node.inputs[2]; - let mut offset = 0; - let size = u16::try_from(tys.size_of(node.ty)).expect("TODO"); - if fuc.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add }) - && let Kind::CInt { value } = - fuc.nodes[fuc.nodes[region].inputs[2]].kind - && node.ty.loc(tys) == Loc::Reg - { - region = fuc.nodes[region].inputs[1]; - offset = value as Offset; - } - let nd = &fuc.nodes[region]; - let (base, offset, src) = match nd.kind { - Kind::Stck if node.ty.loc(tys) == Loc::Reg => { - (reg::STACK_PTR, self.offsets[region as usize] + offset, allocs[0]) - } - _ => (atr(allocs[0]), offset, allocs[1]), - }; - - match node.ty.loc(tys) { - Loc::Reg => self.emit(instrs::st(atr(src), base, offset as _, size)), - Loc::Stack => { - debug_assert_eq!(offset, 0); - self.emit(instrs::bmc(atr(src), base, size)) - } - } - } - Kind::Start - | Kind::Assert { .. } - | Kind::Entry - | Kind::Mem - | Kind::End - | Kind::Loops - | Kind::Then - | Kind::Else - | Kind::Phi - | Kind::Arg => unreachable!(), - } - } - } - - self.ralloc = ralloc; - - (saved_regs.len(), fuc.tail) - } } impl Backend for HbvmBackend { @@ -492,7 +83,7 @@ impl Backend for HbvmBackend { config_length: 0, metadata_length: 0, }; - Reloc::new(HEADER_SIZE, 3, 4).apply_jump(to, entry as _, 0); + Reloc::new(HEADER_SIZE, 3, 4).apply_jump(to, entry, 0); unsafe { *to.as_mut_ptr().cast::() = exe } } @@ -514,6 +105,7 @@ impl Backend for HbvmBackend { match itm.expand() { ty::Kind::Func(func) => { let fuc = &mut self.funcs[func as usize]; + debug_assert!(!fuc.code.is_empty()); if fuc.offset != u32::MAX { continue; } @@ -566,7 +158,7 @@ impl Backend for HbvmBackend { AssemblySpec { code_length: code_length as _, data_length: data_length as _, - entry: self.funcs[from as usize].offset as _, + entry: self.funcs[from as usize].offset, } } @@ -660,6 +252,7 @@ impl Backend for HbvmBackend { } let (saved, tail) = self.emit_body_code(nodes, sig, tys, files); + //let (saved, tail) = self.emit_body_code_my(nodes, sig, tys, files); if let Some(last_ret) = self.ret_relocs.last() && last_ret.offset as usize == self.code.len() - 5 @@ -739,631 +332,42 @@ impl Backend for HbvmBackend { } } -impl ItemCtx {} - -#[derive(Debug)] -struct Block { - nid: Nid, - preds: Vec, - succs: Vec, - instrs: regalloc2::InstRange, - params: Vec, - branch_blockparams: Vec, -} - -#[derive(Debug)] -struct Instr { - nid: Nid, - ops: Vec, -} - -pub struct Function<'a> { - sig: Sig, - nodes: &'a mut Nodes, - tys: &'a Types, - tail: bool, - visited: BitSet, - backrefs: Vec, - blocks: Vec, - instrs: Vec, -} - -impl core::fmt::Debug for Function<'_> { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - for (i, block) in self.blocks.iter().enumerate() { - writeln!(f, "sb{i}{:?}-{:?}:", block.params, block.preds)?; - - for inst in block.instrs.iter() { - let instr = &self.instrs[inst.index()]; - writeln!(f, "{}: i{:?}:{:?}", inst.index(), self.nodes[instr.nid].kind, instr.ops)?; - } - - writeln!(f, "eb{i}{:?}-{:?}:", block.branch_blockparams, block.succs)?; - } - Ok(()) - } -} - -impl<'a> Function<'a> { - fn new(nodes: &'a mut Nodes, tys: &'a Types, sig: Sig) -> Self { - let mut s = Self { - tys, - sig, - tail: true, - visited: Default::default(), - backrefs: vec![u16::MAX; nodes.values.len()], - blocks: Default::default(), - instrs: Default::default(), - nodes, - }; - s.visited.clear(s.nodes.values.len()); - s.emit_node(VOID, VOID); - s.add_block(0); - s.blocks.pop(); - s - } - - fn add_block(&mut self, nid: Nid) -> RallocBRef { - if let Some(prev) = self.blocks.last_mut() { - prev.instrs = regalloc2::InstRange::new( - prev.instrs.first(), - regalloc2::Inst::new(self.instrs.len()), - ); - } - - self.blocks.push(Block { - nid, - preds: Default::default(), - succs: Default::default(), - instrs: regalloc2::InstRange::new( - regalloc2::Inst::new(self.instrs.len()), - regalloc2::Inst::new(self.instrs.len() + 1), - ), - params: Default::default(), - branch_blockparams: Default::default(), - }); - self.blocks.len() as RallocBRef - 1 - } - - fn add_instr(&mut self, nid: Nid, ops: Vec) { - self.instrs.push(Instr { nid, ops }); - } - - fn urg(&mut self, nid: Nid) -> regalloc2::Operand { - regalloc2::Operand::reg_use(self.rg(nid)) - } - - fn drg(&mut self, nid: Nid) -> regalloc2::Operand { - regalloc2::Operand::reg_def(self.rg(nid)) - } - - fn rg(&self, nid: Nid) -> regalloc2::VReg { - debug_assert!( - !self.nodes.is_cfg(nid) || matches!(self.nodes[nid].kind, Kind::Call { .. }), - "{:?}", - self.nodes[nid] - ); - debug_assert_eq!(self.nodes[nid].lock_rc, 0, "{nid} {:?}", self.nodes[nid]); - debug_assert!(self.nodes[nid].kind != Kind::Phi || self.nodes[nid].ty != ty::Id::VOID); - regalloc2::VReg::new(nid as _, regalloc2::RegClass::Int) - } - - fn emit_node(&mut self, nid: Nid, prev: Nid) { - if matches!(self.nodes[nid].kind, Kind::Region | Kind::Loop) { - let prev_bref = self.backrefs[prev as usize]; - let node = self.nodes[nid].clone(); - - let idx = 1 + node.inputs.iter().position(|&i| i == prev).unwrap(); - - for ph in node.outputs { - if self.nodes[ph].kind != Kind::Phi || self.nodes[ph].ty == ty::Id::VOID { - continue; - } - - let rg = self.rg(self.nodes[ph].inputs[idx]); - self.blocks[prev_bref as usize].branch_blockparams.push(rg); - } - - self.add_instr(nid, vec![]); - - match (self.nodes[nid].kind, self.visited.set(nid)) { - (Kind::Loop, false) => { - for i in node.inputs { - self.bridge(i, nid); - } - return; - } - (Kind::Region, true) => return, - _ => {} - } - } else if !self.visited.set(nid) { - return; - } - - let mut node = self.nodes[nid].clone(); +impl Nodes { + fn is_never_used(&self, nid: Nid, tys: &Types) -> bool { + let node = &self[nid]; match node.kind { - Kind::Start => { - debug_assert_matches!(self.nodes[node.outputs[0]].kind, Kind::Entry); - self.emit_node(node.outputs[0], VOID) + Kind::CInt { .. } => node.outputs.iter().all(|&o| { + matches!(self[o].kind, Kind::BinOp { op } + if op.imm_binop(self[o].ty).is_some() + && self.is_const(self[o].inputs[2]) + && op.cond_op(self[o].ty).is_none()) + }), + Kind::BinOp { op: TokenKind::Add | TokenKind::Sub } => { + self[node.inputs[1]].lock_rc != 0 + || (self.is_const(node.inputs[2]) + && node.outputs.iter().all(|&n| self[n].uses_direct_offset_of(nid, tys))) } - Kind::If => { - self.backrefs[nid as usize] = self.backrefs[prev as usize]; - - let &[_, cond] = node.inputs.as_slice() else { unreachable!() }; - let &[mut then, mut else_] = node.outputs.as_slice() else { unreachable!() }; - - if let Kind::BinOp { op } = self.nodes[cond].kind - && let Some((_, swapped)) = op.cond_op(node.ty) - { - if swapped { - mem::swap(&mut then, &mut else_); - } - let &[_, lhs, rhs] = self.nodes[cond].inputs.as_slice() else { unreachable!() }; - let ops = vec![self.urg(lhs), self.urg(rhs)]; - self.add_instr(nid, ops); - } else { - mem::swap(&mut then, &mut else_); - let ops = vec![self.urg(cond)]; - self.add_instr(nid, ops); - } - - self.emit_node(then, nid); - self.emit_node(else_, nid); - } - Kind::Region | Kind::Loop => { - self.backrefs[nid as usize] = self.add_block(nid); - if node.kind == Kind::Region { - for i in node.inputs { - self.bridge(i, nid); - } - } - let mut block = vec![]; - for ph in node.outputs.clone() { - if self.nodes[ph].kind != Kind::Phi || self.nodes[ph].ty == ty::Id::VOID { - continue; - } - block.push(self.rg(ph)); - } - self.blocks[self.backrefs[nid as usize] as usize].params = block; - self.reschedule_block(nid, &mut node.outputs); - for o in node.outputs.into_iter().rev() { - self.emit_node(o, nid); - } - } - Kind::Return => { - let ops = match self.tys.parama(self.sig.ret).0 { - None => vec![], - Some(PLoc::Reg(..)) if self.sig.ret.loc(self.tys) == Loc::Stack => { - vec![self.urg(self.nodes[node.inputs[1]].inputs[1])] - } - Some(PLoc::Reg(r, ..)) => { - vec![regalloc2::Operand::reg_fixed_use( - self.rg(node.inputs[1]), - regalloc2::PReg::new(r as _, regalloc2::RegClass::Int), - )] - } - Some(PLoc::WideReg(..)) => { - vec![self.urg(self.nodes[node.inputs[1]].inputs[1])] - } - Some(PLoc::Ref(..)) => { - vec![self.urg(self.nodes[node.inputs[1]].inputs[1]), self.urg(MEM)] - } - }; - - self.add_instr(nid, ops); - self.emit_node(node.outputs[0], nid); - } - Kind::Die => { - self.add_instr(nid, vec![]); - self.emit_node(node.outputs[0], nid); - } - Kind::CInt { .. } - if node.outputs.iter().all(|&o| { - let ond = &self.nodes[o]; - matches!(ond.kind, Kind::BinOp { op } - if op.imm_binop(ond.ty).is_some() - && self.nodes.is_const(ond.inputs[2]) - && op.cond_op(ond.ty).is_none()) - }) => - { - self.nodes.lock(nid) - } - Kind::CInt { .. } => { - let ops = vec![self.drg(nid)]; - self.add_instr(nid, ops); - } - Kind::Entry => { - self.backrefs[nid as usize] = self.add_block(nid); - - let (ret, mut parama) = self.tys.parama(self.sig.ret); - let mut typs = self.sig.args.args(); - #[expect(clippy::unnecessary_to_owned)] - let mut args = self.nodes[VOID].outputs[ARG_START..].to_owned().into_iter(); - while let Some(ty) = typs.next_value(self.tys) { - let arg = args.next().unwrap(); - debug_assert_eq!(self.nodes[arg].kind, Kind::Arg); - match parama.next(ty, self.tys) { - None => {} - Some(PLoc::Reg(r, _) | PLoc::WideReg(r, _) | PLoc::Ref(r, _)) => { - self.add_instr(NEVER, vec![regalloc2::Operand::reg_fixed_def( - self.rg(arg), - regalloc2::PReg::new(r as _, regalloc2::RegClass::Int), - )]); - } - } - } - - if let Some(PLoc::Ref(r, ..)) = ret { - self.add_instr(NEVER, vec![regalloc2::Operand::reg_fixed_def( - self.rg(MEM), - regalloc2::PReg::new(r as _, regalloc2::RegClass::Int), - )]); - } - - self.reschedule_block(nid, &mut node.outputs); - for o in node.outputs.into_iter().rev() { - self.emit_node(o, nid); - } - } - Kind::Then | Kind::Else => { - self.backrefs[nid as usize] = self.add_block(nid); - self.bridge(prev, nid); - self.reschedule_block(nid, &mut node.outputs); - for o in node.outputs.into_iter().rev() { - self.emit_node(o, nid); - } - } - Kind::BinOp { op: TokenKind::Add } if self.nodes[node.inputs[1]].lock_rc != 0 => self.nodes.lock(nid), - Kind::BinOp { op: TokenKind::Add } - if self.nodes.is_const(node.inputs[2]) - && node.outputs.iter().all(|&n| { - (matches!(self.nodes[n].kind, Kind::Stre if self.nodes[n].inputs[2] == nid) - || matches!(self.nodes[n].kind, Kind::Load if self.nodes[n].inputs[1] == nid)) - && self.nodes[n].ty.loc(self.tys) == Loc::Reg - }) => - { - self.nodes.lock(nid) - } - Kind::BinOp { op } - if op.cond_op(node.ty).is_some() - && node.outputs.iter().all(|&n| self.nodes[n].kind == Kind::If) => - { - self.nodes.lock(nid) - } - Kind::BinOp { .. } => { - let &[_, lhs, rhs] = node.inputs.as_slice() else { unreachable!() }; - - let ops = if let Kind::CInt { .. } = self.nodes[rhs].kind - && self.nodes[rhs].lock_rc != 0 - { - vec![self.drg(nid), self.urg(lhs)] - } else { - vec![self.drg(nid), self.urg(lhs), self.urg(rhs)] - }; - self.add_instr(nid, ops); - } - Kind::UnOp { .. } => { - let ops = vec![self.drg(nid), self.urg(node.inputs[1])]; - self.add_instr(nid, ops); - } - Kind::Call { args, func } => { - self.tail &= func == ty::ECA; - self.backrefs[nid as usize] = self.backrefs[prev as usize]; - let mut ops = vec![]; - - let (ret, mut parama) = self.tys.parama(node.ty); - if ret.is_some() { - ops.push(regalloc2::Operand::reg_fixed_def( - self.rg(nid), - regalloc2::PReg::new(1, regalloc2::RegClass::Int), - )); - } - - let mut tys = args.args(); - let mut args = node.inputs[1..].iter(); - while let Some(ty) = tys.next_value(self.tys) { - let mut i = *args.next().unwrap(); - let Some(loc) = parama.next(ty, self.tys) else { continue }; - - match loc { - PLoc::Reg(r, _) if ty.loc(self.tys) == Loc::Reg => { - ops.push(regalloc2::Operand::reg_fixed_use( - self.rg(i), - regalloc2::PReg::new(r as _, regalloc2::RegClass::Int), - )); - } - PLoc::WideReg(r, size) | PLoc::Reg(r, size) => { - loop { - match self.nodes[i].kind { - Kind::Stre { .. } => i = self.nodes[i].inputs[2], - Kind::Load { .. } => i = self.nodes[i].inputs[1], - _ => break, - } - debug_assert_ne!(i, 0); - } - debug_assert!(i != 0); - ops.push(regalloc2::Operand::reg_fixed_use( - self.rg(i), - regalloc2::PReg::new(r as _, regalloc2::RegClass::Int), - )); - if size > 8 { - ops.push(regalloc2::Operand::reg_fixed_use( - self.rg(i), - regalloc2::PReg::new((r + 1) as _, regalloc2::RegClass::Int), - )); - } - } - PLoc::Ref(r, _) => { - loop { - match self.nodes[i].kind { - Kind::Stre { .. } => i = self.nodes[i].inputs[2], - Kind::Load { .. } => i = self.nodes[i].inputs[1], - _ => break, - } - debug_assert_ne!(i, 0); - } - debug_assert!(i != 0); - ops.push(regalloc2::Operand::reg_fixed_use( - self.rg(i), - regalloc2::PReg::new(r as _, regalloc2::RegClass::Int), - )); - } - } - } - - if let Some(PLoc::Ref(r, _)) = ret { - ops.push(regalloc2::Operand::reg_fixed_use( - self.rg(*node.inputs.last().unwrap()), - regalloc2::PReg::new(r as _, regalloc2::RegClass::Int), - )); - } - - self.add_instr(nid, ops); - - self.reschedule_block(nid, &mut node.outputs); - for o in node.outputs.into_iter().rev() { - if self.nodes[o].inputs[0] == nid - || (matches!(self.nodes[o].kind, Kind::Loop | Kind::Region) - && self.nodes[o].inputs[1] == nid) - { - self.emit_node(o, nid); - } - } - } - Kind::Global { .. } => { - let ops = vec![self.drg(nid)]; - self.add_instr(nid, ops); - } - Kind::Stck | Kind::Arg - if node.outputs.iter().all(|&n| { - matches!(self.nodes[n].kind, Kind::Load - if self.nodes[n].ty.loc(self.tys) == Loc::Reg) - || matches!(self.nodes[n].kind, Kind::Stre - if self.nodes[n].ty.loc(self.tys) == Loc::Reg - && self.nodes[n].inputs[1] != nid) - || matches!(self.nodes[n].kind, Kind::BinOp { op: TokenKind::Add } - if self.nodes.is_const(self.nodes[n].inputs[2]) - && self.nodes[n] - .outputs - .iter() - .all(|&n| matches!(self.nodes[n].kind, Kind::Load - if self.nodes[n].ty.loc(self.tys) == Loc::Reg) - || matches!(self.nodes[n].kind, Kind::Stre - if self.nodes[n].ty.loc(self.tys) == Loc::Reg - && self.nodes[n].inputs[1] != nid))) - }) => self.nodes.lock(nid), - Kind::Stck if self.tys.size_of(node.ty) == 0 => self.nodes.lock(nid), - Kind::Stck => { - let ops = vec![self.drg(nid)]; - self.add_instr(nid, ops); - } - Kind::Assert { .. } => unreachable!(), - Kind::End | - Kind::Phi | Kind::Arg | Kind::Mem | Kind::Loops => {} - Kind::Load { .. } if node.ty.loc(self.tys) == Loc::Stack => { - self.nodes.lock(nid) - } - Kind::Load { .. } => { - let mut region = node.inputs[1]; - if self.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add }) - && self.nodes.is_const(self.nodes[region].inputs[2]) - && node.ty.loc(self.tys) == Loc::Reg - { - region = self.nodes[region].inputs[1] - } - let ops = match self.nodes[region].kind { - Kind::Stck => vec![self.drg(nid)], - _ => vec![self.drg(nid), self.urg(region)], - }; - self.add_instr(nid, ops); - } - Kind::Stre if node.inputs[1] == VOID => self.nodes.lock(nid), - Kind::Stre => { - debug_assert_ne!(self.tys.size_of(node.ty), 0); - let mut region = node.inputs[2]; - if self.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add }) - && self.nodes.is_const(self.nodes[region].inputs[2]) - && node.ty.loc(self.tys) == Loc::Reg - { - region = self.nodes[region].inputs[1] - } - let ops = match self.nodes[region].kind { - _ if node.ty.loc(self.tys) == Loc::Stack => { - if self.nodes[node.inputs[1]].kind == Kind::Arg { - vec![self.urg(region), self.urg(node.inputs[1])] - } else { - vec![self.urg(region), self.urg(self.nodes[node.inputs[1]].inputs[1])] - } - } - Kind::Stck => vec![self.urg(node.inputs[1])], - _ => vec![self.urg(region), self.urg(node.inputs[1])], - }; - self.add_instr(nid, ops); + Kind::BinOp { op } => { + op.cond_op(node.ty).is_some() + && node.outputs.iter().all(|&n| self[n].kind == Kind::If) } + Kind::Stck if tys.size_of(node.ty) == 0 => true, + Kind::Stck | Kind::Arg => node.outputs.iter().all(|&n| { + self[n].uses_direct_offset_of(nid, tys) + || (matches!(self[n].kind, Kind::BinOp { op: TokenKind::Add }) + && self.is_never_used(n, tys)) + }), + Kind::Load { .. } => node.ty.loc(tys) == Loc::Stack, + _ => false, } } - - fn bridge(&mut self, pred: u16, succ: u16) { - if self.backrefs[pred as usize] == u16::MAX || self.backrefs[succ as usize] == u16::MAX { - return; - } - self.blocks[self.backrefs[pred as usize] as usize] - .succs - .push(regalloc2::Block::new(self.backrefs[succ as usize] as usize)); - self.blocks[self.backrefs[succ as usize] as usize] - .preds - .push(regalloc2::Block::new(self.backrefs[pred as usize] as usize)); - } - - fn reschedule_block(&mut self, from: Nid, outputs: &mut Vc) { - let from = Some(&from); - let mut buf = Vec::with_capacity(outputs.len()); - let mut seen = BitSet::default(); - seen.clear(self.nodes.values.len()); - - for &o in outputs.iter() { - if !self.nodes.is_cfg(o) { - continue; - } - - seen.set(o); - - let mut cursor = buf.len(); - buf.push(o); - while let Some(&n) = buf.get(cursor) { - for &i in &self.nodes[n].inputs[1..] { - if from == self.nodes[i].inputs.first() - && self.nodes[i] - .outputs - .iter() - .all(|&o| self.nodes[o].inputs.first() != from || seen.get(o)) - && seen.set(i) - { - buf.push(i); - } - } - cursor += 1; - } - } - - for &o in outputs.iter() { - if !seen.set(o) { - continue; - } - let mut cursor = buf.len(); - buf.push(o); - while let Some(&n) = buf.get(cursor) { - for &i in &self.nodes[n].inputs[1..] { - if from == self.nodes[i].inputs.first() - && self.nodes[i] - .outputs - .iter() - .all(|&o| self.nodes[o].inputs.first() != from || seen.get(o)) - && seen.set(i) - { - buf.push(i); - } - } - cursor += 1; - } - } - - debug_assert!( - outputs.len() == buf.len() || outputs.len() == buf.len() + 1, - "{:?} {:?}", - outputs, - buf - ); - - if buf.len() + 1 == outputs.len() { - outputs.remove(outputs.len() - 1); - } - outputs.copy_from_slice(&buf); - } } -impl regalloc2::Function for Function<'_> { - fn num_insts(&self) -> usize { - self.instrs.len() - } - - fn num_blocks(&self) -> usize { - self.blocks.len() - } - - fn entry_block(&self) -> regalloc2::Block { - regalloc2::Block(0) - } - - fn block_insns(&self, block: regalloc2::Block) -> regalloc2::InstRange { - self.blocks[block.index()].instrs - } - - fn block_succs(&self, block: regalloc2::Block) -> &[regalloc2::Block] { - &self.blocks[block.index()].succs - } - - fn block_preds(&self, block: regalloc2::Block) -> &[regalloc2::Block] { - &self.blocks[block.index()].preds - } - - fn block_params(&self, block: regalloc2::Block) -> &[regalloc2::VReg] { - &self.blocks[block.index()].params - } - - fn is_ret(&self, insn: regalloc2::Inst) -> bool { - matches!(self.nodes[self.instrs[insn.index()].nid].kind, Kind::Return | Kind::Die) - } - - fn is_branch(&self, insn: regalloc2::Inst) -> bool { - matches!( - self.nodes[self.instrs[insn.index()].nid].kind, - Kind::If | Kind::Then | Kind::Else | Kind::Entry | Kind::Loop | Kind::Region - ) - } - - fn branch_blockparams( - &self, - block: regalloc2::Block, - _insn: regalloc2::Inst, - _succ_idx: usize, - ) -> &[regalloc2::VReg] { - debug_assert!( - self.blocks[block.index()].succs.len() == 1 - || self.blocks[block.index()].branch_blockparams.is_empty() - ); - - &self.blocks[block.index()].branch_blockparams - } - - fn inst_operands(&self, insn: regalloc2::Inst) -> &[regalloc2::Operand] { - &self.instrs[insn.index()].ops - } - - fn inst_clobbers(&self, insn: regalloc2::Inst) -> regalloc2::PRegSet { - let node = &self.nodes[self.instrs[insn.index()].nid]; - if matches!(node.kind, Kind::Call { .. }) { - let mut set = regalloc2::PRegSet::default(); - let returns = self.tys.parama(node.ty).0.is_some(); - for i in 1 + returns as usize..13 { - set.add(regalloc2::PReg::new(i, regalloc2::RegClass::Int)); - } - set - } else { - regalloc2::PRegSet::default() - } - } - - fn num_vregs(&self) -> usize { - self.nodes.values.len() - } - - fn spillslot_size(&self, regclass: regalloc2::RegClass) -> usize { - match regclass { - regalloc2::RegClass::Int => 1, - regalloc2::RegClass::Float => unreachable!(), - regalloc2::RegClass::Vector => unreachable!(), - } +impl Node { + fn uses_direct_offset_of(&self, nid: Nid, tys: &Types) -> bool { + ((self.kind == Kind::Stre && self.inputs[2] == nid) + || (self.kind == Kind::Load && self.inputs[1] == nid)) + && self.ty.loc(tys) == Loc::Reg } } diff --git a/lang/src/son/hbvm/my_regalloc.rs b/lang/src/son/hbvm/my_regalloc.rs new file mode 100644 index 0000000..16d00d1 --- /dev/null +++ b/lang/src/son/hbvm/my_regalloc.rs @@ -0,0 +1,907 @@ +use { + super::{HbvmBackend, Nid, Nodes}, + crate::{ + lexer::TokenKind, + parser, + reg::{self, Reg}, + son::{debug_assert_matches, Kind, ARG_START, MEM, VOID}, + ty::{self, Arg, Loc}, + utils::{BitSet, Vc}, + Offset, PLoc, Reloc, Sig, TypedReloc, Types, + }, + alloc::{borrow::ToOwned, vec::Vec}, + core::{mem, ops::Range}, + hbbytecode::{self as instrs}, +}; + +impl HbvmBackend { + pub fn emit_body_code_my( + &mut self, + nodes: &mut Nodes, + sig: Sig, + tys: &Types, + files: &[parser::Ast], + ) -> (usize, bool) { + let mut fuc = Function::new(nodes, tys, sig); + log::info!("{fuc:?}"); + + let mut res = mem::take(&mut self.ralloc_my); + + Env::new(&fuc, &fuc.func, &mut res).run(); + + '_open_function: { + self.emit(instrs::addi64(reg::STACK_PTR, reg::STACK_PTR, 0)); + self.emit(instrs::st(reg::RET_ADDR + fuc.tail as u8, reg::STACK_PTR, 0, 0)); + } + + let reg_offset = if fuc.tail { reg::RET + 12 } else { reg::RET_ADDR + 1 }; + + res.node_to_reg.iter_mut().filter(|r| **r != 0).for_each(|r| { + *r += reg_offset - 1; + if fuc.tail && *r >= reg::RET_ADDR { + *r += 1; + } + }); + + let atr = |allc: Nid| res.node_to_reg[allc as usize]; + + //for (id, node) in fuc.nodes.iter() { + // if node.kind == Kind::Phi { + // debug_assert_eq!(atr(node.inputs[1]), atr(node.inputs[2])); + // debug_assert_eq!(atr(id), atr(node.inputs[2])); + // } + //} + + let (retl, mut parama) = tys.parama(sig.ret); + let mut typs = sig.args.args(); + let mut args = fuc.nodes[VOID].outputs[ARG_START..].iter(); + while let Some(aty) = typs.next(tys) { + let Arg::Value(ty) = aty else { continue }; + let Some(loc) = parama.next(ty, tys) else { continue }; + let &arg = args.next().unwrap(); + let (rg, size) = match loc { + PLoc::WideReg(rg, size) => (rg, size), + PLoc::Reg(rg, size) if ty.loc(tys) == Loc::Stack => (rg, size), + PLoc::Reg(r, ..) | PLoc::Ref(r, ..) => { + self.emit(instrs::cp(atr(arg), r)); + continue; + } + }; + self.emit(instrs::st(rg, reg::STACK_PTR, self.offsets[arg as usize] as _, size)); + if fuc.nodes[arg].lock_rc == 0 { + self.emit(instrs::addi64(rg, reg::STACK_PTR, self.offsets[arg as usize] as _)); + } + self.emit(instrs::cp(atr(arg), rg)); + } + + for (i, block) in fuc.func.blocks.iter().enumerate() { + self.offsets[block.entry as usize] = self.code.len() as _; + for &nid in &fuc.func.instrs[block.range.clone()] { + if nid == VOID { + continue; + } + + let node = &fuc.nodes[nid]; + + let extend = |base: ty::Id, dest: ty::Id, from: Nid, to: Nid| { + let (bsize, dsize) = (tys.size_of(base), tys.size_of(dest)); + debug_assert!(bsize <= 8, "{}", ty::Display::new(tys, files, base)); + debug_assert!(dsize <= 8, "{}", ty::Display::new(tys, files, dest)); + if bsize == dsize { + return Default::default(); + } + match (base.is_signed(), dest.is_signed()) { + (true, true) => { + let op = [instrs::sxt8, instrs::sxt16, instrs::sxt32] + [bsize.ilog2() as usize]; + op(atr(to), atr(from)) + } + _ => { + let mask = (1u64 << (bsize * 8)) - 1; + instrs::andi(atr(to), atr(from), mask) + } + } + }; + + match node.kind { + Kind::If => { + let &[_, cnd] = node.inputs.as_slice() else { unreachable!() }; + if let Kind::BinOp { op } = fuc.nodes[cnd].kind + && let Some((op, swapped)) = + op.cond_op(fuc.nodes[fuc.nodes[cnd].inputs[1]].ty) + { + let &[_, lhs, rhs] = fuc.nodes[cnd].inputs.as_slice() else { + unreachable!() + }; + + self.emit(extend(fuc.nodes[lhs].ty, fuc.nodes[lhs].ty.extend(), 0, 0)); + self.emit(extend(fuc.nodes[rhs].ty, fuc.nodes[rhs].ty.extend(), 1, 1)); + + let rel = Reloc::new(self.code.len(), 3, 2); + self.jump_relocs.push((node.outputs[!swapped as usize], rel)); + self.emit(op(atr(lhs), atr(rhs), 0)); + } else { + self.emit(extend(fuc.nodes[cnd].ty, fuc.nodes[cnd].ty.extend(), 0, 0)); + let rel = Reloc::new(self.code.len(), 3, 2); + self.jump_relocs.push((node.outputs[0], rel)); + self.emit(instrs::jne(atr(cnd), reg::ZERO, 0)); + } + } + Kind::Loop | Kind::Region => { + if (mem::replace(&mut fuc.backrefs[nid as usize], u16::MAX) != u16::MAX) + ^ (node.kind == Kind::Loop) + { + let index = (node.kind == Kind::Loop) as usize + 1; + for &out in node.outputs.iter() { + if fuc.nodes[out].is_data_phi() + && atr(out) != atr(fuc.nodes[out].inputs[index]) + { + self.emit(instrs::cp( + atr(out), + atr(fuc.nodes[out].inputs[index]), + )); + } + } + + let rel = Reloc::new(self.code.len(), 1, 4); + self.jump_relocs.push((nid, rel)); + self.emit(instrs::jmp(0)); + } else { + let index = (node.kind != Kind::Loop) as usize + 1; + for &out in node.outputs.iter() { + if fuc.nodes[out].is_data_phi() + && atr(out) != atr(fuc.nodes[out].inputs[index]) + { + self.emit(instrs::cp( + atr(out), + atr(fuc.nodes[out].inputs[index]), + )); + } + } + } + } + Kind::Return => { + let &[_, mut ret, ..] = node.inputs.as_slice() else { unreachable!() }; + match retl { + None => {} + Some(PLoc::Reg(r, _)) if sig.ret.loc(tys) == Loc::Reg => { + self.emit(instrs::cp(r, atr(ret))); + } + Some(PLoc::Reg(r, size)) | Some(PLoc::WideReg(r, size)) => { + ret = match fuc.nodes[ret].kind { + Kind::Load { .. } => fuc.nodes[ret].inputs[1], + _ => ret, + }; + self.emit(instrs::ld(r, atr(ret), 0, size)) + } + Some(PLoc::Ref(_, size)) => { + ret = match fuc.nodes[ret].kind { + Kind::Load { .. } => fuc.nodes[ret].inputs[1], + _ => ret, + }; + + let [src, dst] = [atr(ret), atr(MEM)]; + if let Ok(size) = u16::try_from(size) { + self.emit(instrs::bmc(src, dst, size)); + } else { + for _ in 0..size / u16::MAX as u32 { + self.emit(instrs::bmc(src, dst, u16::MAX)); + self.emit(instrs::addi64(src, src, u16::MAX as _)); + self.emit(instrs::addi64(dst, dst, u16::MAX as _)); + } + self.emit(instrs::bmc(src, dst, size as u16)); + self.emit(instrs::addi64(src, src, size.wrapping_neg() as _)); + self.emit(instrs::addi64(dst, dst, size.wrapping_neg() as _)); + } + } + } + + if i != fuc.func.blocks.len() - 1 { + let rel = Reloc::new(self.code.len(), 1, 4); + self.ret_relocs.push(rel); + self.emit(instrs::jmp(0)); + } + } + Kind::Die => self.emit(instrs::un()), + Kind::CInt { value } if node.ty.is_float() => { + self.emit(match node.ty { + ty::Id::F32 => instrs::li32( + atr(nid), + (f64::from_bits(value as _) as f32).to_bits(), + ), + ty::Id::F64 => instrs::li64(atr(nid), value as _), + _ => unreachable!(), + }); + } + Kind::CInt { value } => self.emit(match tys.size_of(node.ty) { + 1 => instrs::li8(atr(nid), value as _), + 2 => instrs::li16(atr(nid), value as _), + 4 => instrs::li32(atr(nid), value as _), + _ => instrs::li64(atr(nid), value as _), + }), + Kind::UnOp { op } => { + let op = op + .unop(node.ty, fuc.nodes[node.inputs[1]].ty) + .expect("TODO: unary operator not supported"); + self.emit(op(atr(nid), atr(node.inputs[1]))); + } + Kind::BinOp { .. } if node.lock_rc != 0 => {} + Kind::BinOp { op } => { + let &[.., lhs, rhs] = node.inputs.as_slice() else { unreachable!() }; + + if let Kind::CInt { value } = fuc.nodes[rhs].kind + && fuc.nodes[rhs].lock_rc != 0 + && let Some(op) = op.imm_binop(node.ty) + { + self.emit(op(atr(nid), atr(lhs), value as _)); + } else if let Some(op) = + op.binop(node.ty).or(op.float_cmp(fuc.nodes[lhs].ty)) + { + self.emit(op(atr(nid), atr(lhs), atr(rhs))); + } else if let Some(against) = op.cmp_against() { + let op_ty = fuc.nodes[lhs].ty; + + self.emit(extend(fuc.nodes[lhs].ty, fuc.nodes[lhs].ty.extend(), 0, 0)); + self.emit(extend(fuc.nodes[rhs].ty, fuc.nodes[rhs].ty.extend(), 1, 1)); + + if op_ty.is_float() && matches!(op, TokenKind::Le | TokenKind::Ge) { + let opop = match op { + TokenKind::Le => TokenKind::Gt, + TokenKind::Ge => TokenKind::Lt, + _ => unreachable!(), + }; + let op_fn = opop.float_cmp(op_ty).unwrap(); + self.emit(op_fn(atr(nid), atr(lhs), atr(rhs))); + self.emit(instrs::not(atr(nid), atr(nid))); + } else if op_ty.is_integer() { + let op_fn = + if op_ty.is_signed() { instrs::cmps } else { instrs::cmpu }; + self.emit(op_fn(atr(nid), atr(lhs), atr(rhs))); + self.emit(instrs::cmpui(atr(nid), atr(nid), against)); + if matches!(op, TokenKind::Eq | TokenKind::Lt | TokenKind::Gt) { + self.emit(instrs::not(atr(nid), atr(nid))); + } + } else { + todo!("unhandled operator: {op}"); + } + } else { + todo!("unhandled operator: {op}"); + } + } + Kind::Call { args, func } => { + let (ret, mut parama) = tys.parama(node.ty); + let mut args = args.args(); + let mut allocs = node.inputs[1..].iter(); + while let Some(arg) = args.next(tys) { + let Arg::Value(ty) = arg else { continue }; + let Some(loc) = parama.next(ty, tys) else { continue }; + + let mut arg = *allocs.next().unwrap(); + let (rg, size) = match loc { + PLoc::Reg(rg, size) if ty.loc(tys) == Loc::Stack => (rg, size), + PLoc::WideReg(rg, size) => (rg, size), + PLoc::Ref(r, ..) => { + arg = match fuc.nodes[arg].kind { + Kind::Load { .. } => fuc.nodes[arg].inputs[1], + _ => arg, + }; + self.emit(instrs::cp(r, atr(arg))); + continue; + } + PLoc::Reg(r, ..) => { + self.emit(instrs::cp(r, atr(arg))); + continue; + } + }; + + arg = match fuc.nodes[arg].kind { + Kind::Load { .. } => fuc.nodes[arg].inputs[1], + _ => arg, + }; + self.emit(instrs::ld(rg, atr(arg), 0, size)); + } + + debug_assert!( + !matches!(ret, Some(PLoc::Ref(..))) || allocs.next().is_some() + ); + + if func == ty::ECA { + self.emit(instrs::eca()); + } else { + self.relocs.push(TypedReloc { + target: ty::Kind::Func(func).compress(), + reloc: Reloc::new(self.code.len(), 3, 4), + }); + self.emit(instrs::jal(reg::RET_ADDR, reg::ZERO, 0)); + } + + match ret { + Some(PLoc::WideReg(r, size)) => { + debug_assert_eq!( + fuc.nodes[*node.inputs.last().unwrap()].kind, + Kind::Stck + ); + let stck = self.offsets[*node.inputs.last().unwrap() as usize]; + self.emit(instrs::st(r, reg::STACK_PTR, stck as _, size)); + } + Some(PLoc::Reg(r, size)) if node.ty.loc(tys) == Loc::Stack => { + debug_assert_eq!( + fuc.nodes[*node.inputs.last().unwrap()].kind, + Kind::Stck + ); + let stck = self.offsets[*node.inputs.last().unwrap() as usize]; + self.emit(instrs::st(r, reg::STACK_PTR, stck as _, size)); + } + Some(PLoc::Reg(r, ..)) => self.emit(instrs::cp(atr(nid), r)), + None | Some(PLoc::Ref(..)) => {} + } + } + Kind::Global { global } => { + let reloc = Reloc::new(self.code.len(), 3, 4); + self.relocs.push(TypedReloc { + target: ty::Kind::Global(global).compress(), + reloc, + }); + self.emit(instrs::lra(atr(nid), 0, 0)); + } + Kind::Stck => { + let base = reg::STACK_PTR; + let offset = self.offsets[nid as usize]; + self.emit(instrs::addi64(atr(nid), base, offset as _)); + } + Kind::Load => { + let mut region = node.inputs[1]; + let mut offset = 0; + if fuc.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add }) + && let Kind::CInt { value } = + fuc.nodes[fuc.nodes[region].inputs[2]].kind + { + region = fuc.nodes[region].inputs[1]; + offset = value as Offset; + } + let size = tys.size_of(node.ty); + if node.ty.loc(tys) != Loc::Stack { + let (base, offset) = match fuc.nodes[region].kind { + Kind::Stck => { + (reg::STACK_PTR, self.offsets[region as usize] + offset) + } + _ => (atr(region), offset), + }; + self.emit(instrs::ld(atr(nid), base, offset as _, size as _)); + } + } + Kind::Stre if node.inputs[1] == VOID => {} + Kind::Stre => { + let mut region = node.inputs[2]; + let mut offset = 0; + let size = u16::try_from(tys.size_of(node.ty)).expect("TODO"); + if fuc.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add }) + && let Kind::CInt { value } = + fuc.nodes[fuc.nodes[region].inputs[2]].kind + && node.ty.loc(tys) == Loc::Reg + { + region = fuc.nodes[region].inputs[1]; + offset = value as Offset; + } + let nd = &fuc.nodes[region]; + let value = node.inputs[1]; + let (base, offset, src) = match nd.kind { + Kind::Stck if node.ty.loc(tys) == Loc::Reg => { + (reg::STACK_PTR, self.offsets[region as usize] + offset, value) + } + _ => (atr(region), offset, match fuc.nodes[value].kind { + Kind::Load { .. } => fuc.nodes[value].inputs[1], + _ => value, + }), + }; + + match node.ty.loc(tys) { + Loc::Reg => self.emit(instrs::st(atr(src), base, offset as _, size)), + Loc::Stack => { + debug_assert_eq!(offset, 0); + self.emit(instrs::bmc(atr(src), base, size)) + } + } + } + + Kind::Mem => self.emit(instrs::cp(atr(MEM), reg::RET)), + Kind::Arg => {} + e @ (Kind::Start + | Kind::Entry + | Kind::End + | Kind::Loops + | Kind::Then + | Kind::Else + | Kind::Phi + | Kind::Assert { .. }) => unreachable!("{e:?}"), + } + } + } + + self.ralloc_my = res; + + let bundle_count = self.ralloc_my.bundles.len() + (reg_offset as usize); + ( + if fuc.tail { + bundle_count.saturating_sub(reg::RET_ADDR as _) + } else { + assert!(bundle_count < reg::STACK_PTR as usize, "TODO: spill memory"); + self.ralloc_my.bundles.len() + }, + fuc.tail, + ) + } +} + +pub struct Function<'a> { + sig: Sig, + tail: bool, + backrefs: Vec, + nodes: &'a mut Nodes, + tys: &'a Types, + visited: BitSet, + func: Func, +} + +impl Function<'_> { + fn vreg_count(&self) -> usize { + self.nodes.values.len() + } + + fn uses_of(&self, nid: Nid, buf: &mut Vec) { + if self.nodes[nid].kind.is_cfg() && !matches!(self.nodes[nid].kind, Kind::Call { .. }) { + return; + } + + self.nodes[nid] + .outputs + .iter() + .filter(|&&n| self.nodes.is_data_dep(nid, n)) + .collect_into(buf); + } + + fn phi_inputs_of(&self, nid: Nid, buf: &mut Vec) { + match self.nodes[nid].kind { + Kind::Region => { + for &inp in self.nodes[nid].outputs.as_slice() { + if self.nodes[inp].is_data_phi() { + buf.extend(&self.nodes[inp].inputs[1..]); + buf.push(inp); + } + } + } + Kind::Loop => { + for &inp in self.nodes[nid].outputs.as_slice() { + if self.nodes[inp].is_data_phi() { + buf.push(self.nodes[inp].inputs[1]); + buf.push(inp); + buf.push(self.nodes[inp].inputs[2]); + } + } + } + _ => {} + } + } + + fn instr_of(&self, nid: Nid) -> Option { + if self.nodes[nid].kind == Kind::Phi || self.nodes[nid].lock_rc != 0 { + return None; + } + debug_assert_ne!(self.backrefs[nid as usize], Nid::MAX, "{:?}", self.nodes[nid]); + Some(self.backrefs[nid as usize]) + } + + fn block_of(&self, nid: Nid) -> Nid { + debug_assert!(self.nodes[nid].kind.starts_basic_block()); + self.backrefs[nid as usize] + } + + fn idom_of(&self, mut nid: Nid) -> Nid { + while !self.nodes[nid].kind.starts_basic_block() { + nid = self.nodes.idom(nid); + } + nid + } + + fn use_block(&self, inst: Nid, uinst: Nid) -> Nid { + let mut block = self.nodes.use_block(inst, uinst); + while !self.nodes[block].kind.starts_basic_block() { + block = self.nodes.idom(block); + } + block + } +} + +impl core::fmt::Debug for Function<'_> { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + for block in &self.func.blocks { + writeln!(f, "{:?}", self.nodes[block.entry].kind)?; + for &instr in &self.func.instrs[block.range.clone()] { + writeln!(f, "{:?}", self.nodes[instr].kind)?; + } + } + + Ok(()) + } +} + +impl<'a> Function<'a> { + fn new(nodes: &'a mut Nodes, tys: &'a Types, sig: Sig) -> Self { + let mut s = Self { + backrefs: vec![u16::MAX; nodes.values.len()], + tail: true, + nodes, + tys, + sig, + visited: Default::default(), + func: Default::default(), + }; + s.visited.clear(s.nodes.values.len()); + s.emit_node(VOID); + s + } + + fn add_block(&mut self, entry: Nid) { + self.func + .blocks + .push(Block { range: self.func.instrs.len()..self.func.instrs.len(), entry }); + self.backrefs[entry as usize] = self.func.blocks.len() as u16 - 1; + } + + fn close_block(&mut self, exit: Nid) { + if !matches!(self.nodes[exit].kind, Kind::Loop | Kind::Region) { + self.add_instr(exit); + } else { + self.func.instrs.push(exit); + } + let prev = self.func.blocks.last_mut().unwrap(); + prev.range.end = self.func.instrs.len(); + } + + fn add_instr(&mut self, nid: Nid) { + debug_assert_ne!(self.nodes[nid].kind, Kind::Loop); + self.backrefs[nid as usize] = self.func.instrs.len() as u16; + self.func.instrs.push(nid); + } + + fn emit_node(&mut self, nid: Nid) { + if matches!(self.nodes[nid].kind, Kind::Region | Kind::Loop) { + match (self.nodes[nid].kind, self.visited.set(nid)) { + (Kind::Loop, false) | (Kind::Region, true) => { + self.close_block(nid); + return; + } + _ => {} + } + } else if !self.visited.set(nid) { + return; + } + + if self.nodes.is_never_used(nid, self.tys) { + self.nodes.lock(nid); + return; + } + + let mut node = self.nodes[nid].clone(); + match node.kind { + Kind::Start => { + debug_assert_matches!(self.nodes[node.outputs[0]].kind, Kind::Entry); + self.add_block(VOID); + self.emit_node(node.outputs[0]) + } + Kind::If => { + let &[_, cond] = node.inputs.as_slice() else { unreachable!() }; + let &[mut then, mut else_] = node.outputs.as_slice() else { unreachable!() }; + + if let Kind::BinOp { op } = self.nodes[cond].kind + && let Some((_, swapped)) = op.cond_op(node.ty) + && swapped + { + mem::swap(&mut then, &mut else_); + } + + self.close_block(nid); + self.emit_node(then); + self.emit_node(else_); + } + Kind::Region | Kind::Loop => { + self.close_block(nid); + self.add_block(nid); + self.reschedule_block(nid, &mut node.outputs); + for o in node.outputs.into_iter().rev() { + self.emit_node(o); + } + } + Kind::Return | Kind::Die => { + self.close_block(nid); + self.emit_node(node.outputs[0]); + } + Kind::Entry => { + let (ret, mut parama) = self.tys.parama(self.sig.ret); + let mut typs = self.sig.args.args(); + #[expect(clippy::unnecessary_to_owned)] + let mut args = self.nodes[VOID].outputs[ARG_START..].to_owned().into_iter(); + while let Some(ty) = typs.next_value(self.tys) { + let arg = args.next().unwrap(); + debug_assert_eq!(self.nodes[arg].kind, Kind::Arg); + match parama.next(ty, self.tys) { + None => {} + Some(_) => self.add_instr(arg), + } + } + + if let Some(PLoc::Ref(..)) = ret { + self.add_instr(MEM); + } + + self.reschedule_block(nid, &mut node.outputs); + for o in node.outputs.into_iter().rev() { + self.emit_node(o); + } + } + Kind::Then | Kind::Else => { + self.add_block(nid); + self.reschedule_block(nid, &mut node.outputs); + for o in node.outputs.into_iter().rev() { + self.emit_node(o); + } + } + Kind::Call { func, .. } => { + self.tail &= func == ty::ECA; + + self.add_instr(nid); + + self.reschedule_block(nid, &mut node.outputs); + for o in node.outputs.into_iter().rev() { + if self.nodes[o].inputs[0] == nid + || (matches!(self.nodes[o].kind, Kind::Loop | Kind::Region) + && self.nodes[o].inputs[1] == nid) + { + self.emit_node(o); + } + } + } + Kind::CInt { .. } + | Kind::BinOp { .. } + | Kind::UnOp { .. } + | Kind::Global { .. } + | Kind::Load { .. } + | Kind::Stre + | Kind::Stck => self.add_instr(nid), + Kind::End | Kind::Phi | Kind::Arg | Kind::Mem | Kind::Loops => {} + Kind::Assert { .. } => unreachable!(), + } + } + + fn reschedule_block(&mut self, from: Nid, outputs: &mut Vc) { + let from = Some(&from); + let mut buf = Vec::with_capacity(outputs.len()); + let mut seen = BitSet::default(); + seen.clear(self.nodes.values.len()); + + for &o in outputs.iter() { + if !self.nodes.is_cfg(o) { + continue; + } + + seen.set(o); + + let mut cursor = buf.len(); + buf.push(o); + while let Some(&n) = buf.get(cursor) { + for &i in &self.nodes[n].inputs[1..] { + if from == self.nodes[i].inputs.first() + && self.nodes[i] + .outputs + .iter() + .all(|&o| self.nodes[o].inputs.first() != from || seen.get(o)) + && seen.set(i) + { + buf.push(i); + } + } + cursor += 1; + } + } + + for &o in outputs.iter() { + if !seen.set(o) { + continue; + } + let mut cursor = buf.len(); + buf.push(o); + while let Some(&n) = buf.get(cursor) { + for &i in &self.nodes[n].inputs[1..] { + if from == self.nodes[i].inputs.first() + && self.nodes[i] + .outputs + .iter() + .all(|&o| self.nodes[o].inputs.first() != from || seen.get(o)) + && seen.set(i) + { + buf.push(i); + } + } + cursor += 1; + } + } + + debug_assert!( + outputs.len() == buf.len() || outputs.len() == buf.len() + 1, + "{:?} {:?}", + outputs, + buf + ); + + if buf.len() + 1 == outputs.len() { + outputs.remove(outputs.len() - 1); + } + outputs.copy_from_slice(&buf); + } +} + +pub struct Env<'a> { + ctx: &'a Function<'a>, + func: &'a Func, + res: &'a mut Res, +} + +impl<'a> Env<'a> { + pub fn new(ctx: &'a Function<'a>, func: &'a Func, res: &'a mut Res) -> Self { + Self { ctx, func, res } + } + + pub fn run(&mut self) { + self.res.bundles.clear(); + self.res.node_to_reg.clear(); + self.res.node_to_reg.resize(self.ctx.vreg_count(), 0); + + debug_assert!(self.res.dfs_buf.is_empty()); + debug_assert!(self.res.use_buf.is_empty()); + debug_assert!(self.res.phi_input_buf.is_empty()); + + let mut bundle = Bundle::new(self.func.instrs.len()); + let mut visited = BitSet::with_capacity(self.ctx.nodes.values.len()); + let mut use_buf = mem::take(&mut self.res.use_buf); + + let mut phi_input_buf = mem::take(&mut self.res.phi_input_buf); + for block in &self.func.blocks { + self.ctx.phi_inputs_of(block.entry, &mut phi_input_buf); + for param in phi_input_buf.drain(..) { + if !visited.set(param) { + continue; + } + self.append_bundle(param, &mut bundle, &mut use_buf); + } + } + self.res.phi_input_buf = phi_input_buf; + + for &inst in &self.func.instrs { + if visited.get(inst) || inst == 0 { + continue; + } + self.append_bundle(inst, &mut bundle, &mut use_buf); + } + + self.res.use_buf = use_buf; + } + + fn append_bundle(&mut self, inst: Nid, bundle: &mut Bundle, use_buf: &mut Vec) { + let mut dom = self.ctx.idom_of(inst); + if self.ctx.nodes[dom].kind == Kind::Loop && self.ctx.nodes[inst].kind == Kind::Phi { + dom = self.ctx.nodes.idom(dom); + dom = self.ctx.idom_of(dom); + } + std::println!("{inst} {:?}", self.ctx.nodes[inst].key()); + self.ctx.uses_of(inst, use_buf); + for uinst in use_buf.drain(..) { + std::println!("| {uinst} {:?}", self.ctx.nodes[uinst].key()); + let cursor = self.ctx.use_block(inst, uinst); + self.reverse_cfg_dfs(cursor, dom, |_, n, b| { + let mut range = b.range.clone(); + range.start = + range.start.max(self.ctx.instr_of(inst).map_or(0, |n| n + 1) as usize); + range.end = range.end.min( + self.ctx + .instr_of(uinst) + .filter(|_| self.ctx.nodes.loop_depth(dom) == self.ctx.nodes.loop_depth(n)) + .map_or(Nid::MAX, |n| n + 1) as usize, + ); + + std::println!("|- {range:?} {:?}", self.ctx.nodes[n].key()); + bundle.add(range); + }); + } + + match self.res.bundles.iter_mut().enumerate().find(|(_, b)| !b.overlaps(bundle)) { + Some((i, other)) => { + other.merge(bundle); + bundle.clear(); + self.res.node_to_reg[inst as usize] = i as Reg + 1; + } + None => { + self.res.bundles.push(mem::replace(bundle, Bundle::new(self.func.instrs.len()))); + self.res.node_to_reg[inst as usize] = self.res.bundles.len() as Reg; + } + } + std::println!("|= {}", self.res.node_to_reg[inst as usize]); + } + + fn reverse_cfg_dfs( + &mut self, + from: Nid, + until: Nid, + mut each: impl FnMut(&mut Self, Nid, &Block), + ) { + debug_assert!(self.res.dfs_buf.is_empty()); + self.res.dfs_buf.push(from); + self.res.dfs_seem.clear(self.ctx.nodes.values.len()); + + while let Some(nid) = self.res.dfs_buf.pop() { + each(self, nid, &self.func.blocks[self.ctx.block_of(nid) as usize]); + if nid == until { + continue; + } + match self.ctx.nodes[nid].kind { + Kind::Then | Kind::Else | Kind::Region | Kind::Loop => { + for &n in self.ctx.nodes[nid].inputs.iter() { + let d = self.ctx.idom_of(n); + if self.res.dfs_seem.set(d) { + self.res.dfs_buf.push(d); + } + } + } + Kind::Start => {} + _ => unreachable!(), + } + } + } +} + +#[derive(Default)] +pub struct Res { + pub bundles: Vec, + pub node_to_reg: Vec, + use_buf: Vec, + phi_input_buf: Vec, + dfs_buf: Vec, + dfs_seem: BitSet, +} + +pub struct Bundle { + taken: Vec, +} + +impl Bundle { + fn new(size: usize) -> Self { + Self { taken: vec![false; size] } + } + + fn add(&mut self, range: Range) { + self.taken[range].fill(true); + } + + fn overlaps(&self, other: &Self) -> bool { + self.taken.iter().zip(other.taken.iter()).any(|(a, b)| a & b) + } + + fn merge(&mut self, other: &Self) { + debug_assert!(!self.overlaps(other)); + self.taken.iter_mut().zip(other.taken.iter()).for_each(|(a, b)| *a |= *b); + } + + fn clear(&mut self) { + self.taken.fill(false); + } +} + +#[derive(Default)] +pub struct Func { + pub blocks: Vec, + pub instrs: Vec, +} + +pub struct Block { + pub range: Range, + pub entry: Nid, +} diff --git a/lang/src/son/hbvm/their_regalloc.rs b/lang/src/son/hbvm/their_regalloc.rs new file mode 100644 index 0000000..1fee4e6 --- /dev/null +++ b/lang/src/son/hbvm/their_regalloc.rs @@ -0,0 +1,1007 @@ +use { + super::{HbvmBackend, Nid, Nodes}, + crate::{ + lexer::TokenKind, + parser, reg, + son::{debug_assert_matches, Kind, ARG_START, MEM, NEVER, VOID}, + ty::{self, Arg, Loc}, + utils::{BitSet, Vc}, + HashMap, Offset, PLoc, Reloc, Sig, TypedReloc, Types, + }, + alloc::{borrow::ToOwned, vec::Vec}, + core::mem, + hbbytecode::{self as instrs}, +}; + +pub struct Regalloc { + env: regalloc2::MachineEnv, + ctx: regalloc2::Ctx, +} + +impl Default for Regalloc { + fn default() -> Self { + Self { + env: regalloc2::MachineEnv { + preferred_regs_by_class: [ + (1..13).map(|i| regalloc2::PReg::new(i, regalloc2::RegClass::Int)).collect(), + vec![], + vec![], + ], + non_preferred_regs_by_class: [ + (13..64).map(|i| regalloc2::PReg::new(i, regalloc2::RegClass::Int)).collect(), + vec![], + vec![], + ], + scratch_by_class: Default::default(), + fixed_stack_slots: Default::default(), + }, + ctx: Default::default(), + } + } +} + +impl HbvmBackend { + pub fn emit_body_code( + &mut self, + nodes: &mut Nodes, + sig: Sig, + tys: &Types, + files: &[parser::Ast], + ) -> (usize, bool) { + let mut ralloc = mem::take(&mut self.ralloc); + + let fuc = Function::new(nodes, tys, sig); + log::info!("{:?}", fuc); + if !fuc.tail { + mem::swap( + &mut ralloc.env.preferred_regs_by_class, + &mut ralloc.env.non_preferred_regs_by_class, + ); + }; + + let options = regalloc2::RegallocOptions { + verbose_log: false, + validate_ssa: cfg!(debug_assertions), + algorithm: regalloc2::Algorithm::Ion, + }; + regalloc2::run_with_ctx(&fuc, &ralloc.env, &options, &mut ralloc.ctx).unwrap_or_else( + |err| { + if let regalloc2::RegAllocError::SSA(vreg, inst) = err { + fuc.nodes[vreg.vreg() as Nid].lock_rc = Nid::MAX; + fuc.nodes[fuc.instrs[inst.index()].nid].lock_rc = Nid::MAX - 1; + } + fuc.nodes.graphviz_in_browser(ty::Display::new(tys, files, ty::Id::VOID)); + panic!("{err}") + }, + ); + + if !fuc.tail { + mem::swap( + &mut ralloc.env.preferred_regs_by_class, + &mut ralloc.env.non_preferred_regs_by_class, + ); + }; + + let mut saved_regs = HashMap::::default(); + let mut atr = |allc: regalloc2::Allocation| { + debug_assert!(allc.is_reg()); + let hvenc = regalloc2::PReg::from_index(allc.index()).hw_enc() as u8; + if hvenc <= 12 { + return hvenc; + } + let would_insert = saved_regs.len() as u8 + reg::RET_ADDR + 1; + *saved_regs.entry(hvenc).or_insert(would_insert) + }; + + '_open_function: { + self.emit(instrs::addi64(reg::STACK_PTR, reg::STACK_PTR, 0)); + self.emit(instrs::st(reg::RET_ADDR + fuc.tail as u8, reg::STACK_PTR, 0, 0)); + } + + let (retl, mut parama) = tys.parama(sig.ret); + let mut typs = sig.args.args(); + let mut args = fuc.nodes[VOID].outputs[ARG_START..].iter(); + while let Some(aty) = typs.next(tys) { + let Arg::Value(ty) = aty else { continue }; + let Some(loc) = parama.next(ty, tys) else { continue }; + let &arg = args.next().unwrap(); + let (rg, size) = match loc { + PLoc::WideReg(rg, size) => (rg, size), + PLoc::Reg(rg, size) if ty.loc(tys) == Loc::Stack => (rg, size), + PLoc::Reg(..) | PLoc::Ref(..) => continue, + }; + self.emit(instrs::st(rg, reg::STACK_PTR, self.offsets[arg as usize] as _, size)); + if fuc.nodes[arg].lock_rc == 0 { + self.emit(instrs::addi64(rg, reg::STACK_PTR, self.offsets[arg as usize] as _)); + } + } + + for (i, block) in fuc.blocks.iter().enumerate() { + let blk = regalloc2::Block(i as _); + self.offsets[block.nid as usize] = self.code.len() as _; + for instr_or_edit in ralloc.ctx.output.block_insts_and_edits(&fuc, blk) { + let inst = match instr_or_edit { + regalloc2::InstOrEdit::Inst(inst) => inst, + regalloc2::InstOrEdit::Edit(®alloc2::Edit::Move { from, to }) => { + self.emit(instrs::cp(atr(to), atr(from))); + continue; + } + }; + + let nid = fuc.instrs[inst.index()].nid; + if nid == NEVER { + continue; + }; + let allocs = ralloc.ctx.output.inst_allocs(inst); + let node = &fuc.nodes[nid]; + let backref = fuc.backrefs[nid as usize]; + + let mut extend = |base: ty::Id, dest: ty::Id, from: usize, to: usize| { + let (bsize, dsize) = (tys.size_of(base), tys.size_of(dest)); + debug_assert!(bsize <= 8, "{}", ty::Display::new(tys, files, base)); + debug_assert!(dsize <= 8, "{}", ty::Display::new(tys, files, dest)); + if bsize == dsize { + return Default::default(); + } + match (base.is_signed(), dest.is_signed()) { + (true, true) => { + let op = [instrs::sxt8, instrs::sxt16, instrs::sxt32] + [bsize.ilog2() as usize]; + op(atr(allocs[to]), atr(allocs[from])) + } + _ => { + let mask = (1u64 << (bsize * 8)) - 1; + instrs::andi(atr(allocs[to]), atr(allocs[from]), mask) + } + } + }; + + match node.kind { + Kind::If => { + let &[_, cnd] = node.inputs.as_slice() else { unreachable!() }; + if let Kind::BinOp { op } = fuc.nodes[cnd].kind + && let Some((op, swapped)) = + op.cond_op(fuc.nodes[fuc.nodes[cnd].inputs[1]].ty) + { + let &[lhs, rhs] = allocs else { unreachable!() }; + let &[_, lh, rh] = fuc.nodes[cnd].inputs.as_slice() else { + unreachable!() + }; + + self.emit(extend(fuc.nodes[lh].ty, fuc.nodes[lh].ty.extend(), 0, 0)); + self.emit(extend(fuc.nodes[rh].ty, fuc.nodes[rh].ty.extend(), 1, 1)); + + let rel = Reloc::new(self.code.len(), 3, 2); + self.jump_relocs.push((node.outputs[!swapped as usize], rel)); + self.emit(op(atr(lhs), atr(rhs), 0)); + } else { + self.emit(extend(fuc.nodes[cnd].ty, fuc.nodes[cnd].ty.extend(), 0, 0)); + let rel = Reloc::new(self.code.len(), 3, 2); + self.jump_relocs.push((node.outputs[0], rel)); + self.emit(instrs::jne(atr(allocs[0]), reg::ZERO, 0)); + } + } + Kind::Loop | Kind::Region => { + if backref as usize != i + 1 { + let rel = Reloc::new(self.code.len(), 1, 4); + self.jump_relocs.push((nid, rel)); + self.emit(instrs::jmp(0)); + } + } + Kind::Return => { + match retl { + Some(PLoc::Reg(r, size)) if sig.ret.loc(tys) == Loc::Stack => { + self.emit(instrs::ld(r, atr(allocs[0]), 0, size)) + } + None | Some(PLoc::Reg(..)) => {} + Some(PLoc::WideReg(r, size)) => { + self.emit(instrs::ld(r, atr(allocs[0]), 0, size)) + } + Some(PLoc::Ref(_, size)) => { + let [src, dst] = [atr(allocs[0]), atr(allocs[1])]; + if let Ok(size) = u16::try_from(size) { + self.emit(instrs::bmc(src, dst, size)); + } else { + for _ in 0..size / u16::MAX as u32 { + self.emit(instrs::bmc(src, dst, u16::MAX)); + self.emit(instrs::addi64(src, src, u16::MAX as _)); + self.emit(instrs::addi64(dst, dst, u16::MAX as _)); + } + self.emit(instrs::bmc(src, dst, size as u16)); + self.emit(instrs::addi64(src, src, size.wrapping_neg() as _)); + self.emit(instrs::addi64(dst, dst, size.wrapping_neg() as _)); + } + } + } + + if i != fuc.blocks.len() - 1 { + let rel = Reloc::new(self.code.len(), 1, 4); + self.ret_relocs.push(rel); + self.emit(instrs::jmp(0)); + } + } + Kind::Die => { + self.emit(instrs::un()); + } + Kind::CInt { value } if node.ty.is_float() => { + self.emit(match node.ty { + ty::Id::F32 => instrs::li32( + atr(allocs[0]), + (f64::from_bits(value as _) as f32).to_bits(), + ), + ty::Id::F64 => instrs::li64(atr(allocs[0]), value as _), + _ => unreachable!(), + }); + } + Kind::CInt { value } => self.emit(match tys.size_of(node.ty) { + 1 => instrs::li8(atr(allocs[0]), value as _), + 2 => instrs::li16(atr(allocs[0]), value as _), + 4 => instrs::li32(atr(allocs[0]), value as _), + _ => instrs::li64(atr(allocs[0]), value as _), + }), + Kind::UnOp { op } => { + let op = op + .unop(node.ty, fuc.nodes[node.inputs[1]].ty) + .expect("TODO: unary operator not supported"); + let &[dst, oper] = allocs else { unreachable!() }; + self.emit(op(atr(dst), atr(oper))); + } + Kind::BinOp { .. } if node.lock_rc != 0 => {} + Kind::BinOp { op } => { + let &[.., lh, rh] = node.inputs.as_slice() else { unreachable!() }; + + if let Kind::CInt { value } = fuc.nodes[rh].kind + && fuc.nodes[rh].lock_rc != 0 + && let Some(op) = op.imm_binop(node.ty) + { + let &[dst, lhs] = allocs else { unreachable!() }; + self.emit(op(atr(dst), atr(lhs), value as _)); + } else if let Some(op) = + op.binop(node.ty).or(op.float_cmp(fuc.nodes[lh].ty)) + { + let &[dst, lhs, rhs] = allocs else { unreachable!() }; + self.emit(op(atr(dst), atr(lhs), atr(rhs))); + } else if let Some(against) = op.cmp_against() { + let op_ty = fuc.nodes[rh].ty; + + self.emit(extend(fuc.nodes[lh].ty, fuc.nodes[lh].ty.extend(), 0, 0)); + self.emit(extend(fuc.nodes[rh].ty, fuc.nodes[rh].ty.extend(), 1, 1)); + let &[dst, lhs, rhs] = allocs else { unreachable!() }; + + if op_ty.is_float() && matches!(op, TokenKind::Le | TokenKind::Ge) { + let opop = match op { + TokenKind::Le => TokenKind::Gt, + TokenKind::Ge => TokenKind::Lt, + _ => unreachable!(), + }; + let op_fn = opop.float_cmp(op_ty).unwrap(); + self.emit(op_fn(atr(dst), atr(lhs), atr(rhs))); + self.emit(instrs::not(atr(dst), atr(dst))); + } else { + let op_fn = + if op_ty.is_signed() { instrs::cmps } else { instrs::cmpu }; + self.emit(op_fn(atr(dst), atr(lhs), atr(rhs))); + self.emit(instrs::cmpui(atr(dst), atr(dst), against)); + if matches!(op, TokenKind::Eq | TokenKind::Lt | TokenKind::Gt) { + self.emit(instrs::not(atr(dst), atr(dst))); + } + } + } else { + todo!("unhandled operator: {op}"); + } + } + Kind::Call { args, func } => { + let (ret, mut parama) = tys.parama(node.ty); + let has_ret = ret.is_some() as usize; + let mut args = args.args(); + let mut allocs = allocs[has_ret..].iter(); + while let Some(arg) = args.next(tys) { + let Arg::Value(ty) = arg else { continue }; + let Some(loc) = parama.next(ty, tys) else { continue }; + + let &arg = allocs.next().unwrap(); + let (rg, size) = match loc { + PLoc::Reg(rg, size) if ty.loc(tys) == Loc::Stack => (rg, size), + PLoc::WideReg(rg, size) => (rg, size), + PLoc::Ref(..) | PLoc::Reg(..) => continue, + }; + if size > 8 { + allocs.next().unwrap(); + } + self.emit(instrs::ld(rg, atr(arg), 0, size)); + } + + debug_assert!( + !matches!(ret, Some(PLoc::Ref(..))) || allocs.next().is_some() + ); + + if func == ty::ECA { + self.emit(instrs::eca()); + } else { + self.relocs.push(TypedReloc { + target: ty::Kind::Func(func).compress(), + reloc: Reloc::new(self.code.len(), 3, 4), + }); + self.emit(instrs::jal(reg::RET_ADDR, reg::ZERO, 0)); + } + + if let Some(PLoc::WideReg(r, size)) = ret { + debug_assert_eq!( + fuc.nodes[*node.inputs.last().unwrap()].kind, + Kind::Stck + ); + let stck = self.offsets[*node.inputs.last().unwrap() as usize]; + self.emit(instrs::st(r, reg::STACK_PTR, stck as _, size)); + } + if let Some(PLoc::Reg(r, size)) = ret + && node.ty.loc(tys) == Loc::Stack + { + debug_assert_eq!( + fuc.nodes[*node.inputs.last().unwrap()].kind, + Kind::Stck + ); + let stck = self.offsets[*node.inputs.last().unwrap() as usize]; + self.emit(instrs::st(r, reg::STACK_PTR, stck as _, size)); + } + } + Kind::Global { global } => { + let reloc = Reloc::new(self.code.len(), 3, 4); + self.relocs.push(TypedReloc { + target: ty::Kind::Global(global).compress(), + reloc, + }); + self.emit(instrs::lra(atr(allocs[0]), 0, 0)); + } + Kind::Stck => { + let base = reg::STACK_PTR; + let offset = self.offsets[nid as usize]; + self.emit(instrs::addi64(atr(allocs[0]), base, offset as _)); + } + Kind::Load => { + let mut region = node.inputs[1]; + let mut offset = 0; + if fuc.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add }) + && let Kind::CInt { value } = + fuc.nodes[fuc.nodes[region].inputs[2]].kind + { + region = fuc.nodes[region].inputs[1]; + offset = value as Offset; + } + let size = tys.size_of(node.ty); + if node.ty.loc(tys) != Loc::Stack { + let (base, offset) = match fuc.nodes[region].kind { + Kind::Stck => { + (reg::STACK_PTR, self.offsets[region as usize] + offset) + } + _ => (atr(allocs[1]), offset), + }; + self.emit(instrs::ld(atr(allocs[0]), base, offset as _, size as _)); + } + } + Kind::Stre if node.inputs[1] == VOID => {} + Kind::Stre => { + let mut region = node.inputs[2]; + let mut offset = 0; + let size = u16::try_from(tys.size_of(node.ty)).expect("TODO"); + if fuc.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add }) + && let Kind::CInt { value } = + fuc.nodes[fuc.nodes[region].inputs[2]].kind + && node.ty.loc(tys) == Loc::Reg + { + region = fuc.nodes[region].inputs[1]; + offset = value as Offset; + } + let nd = &fuc.nodes[region]; + let (base, offset, src) = match nd.kind { + Kind::Stck if node.ty.loc(tys) == Loc::Reg => { + (reg::STACK_PTR, self.offsets[region as usize] + offset, allocs[0]) + } + _ => (atr(allocs[0]), offset, allocs[1]), + }; + + match node.ty.loc(tys) { + Loc::Reg => self.emit(instrs::st(atr(src), base, offset as _, size)), + Loc::Stack => { + debug_assert_eq!(offset, 0); + self.emit(instrs::bmc(atr(src), base, size)) + } + } + } + Kind::Start + | Kind::Assert { .. } + | Kind::Entry + | Kind::Mem + | Kind::End + | Kind::Loops + | Kind::Then + | Kind::Else + | Kind::Phi + | Kind::Arg => unreachable!(), + } + } + } + + self.ralloc = ralloc; + + (saved_regs.len(), fuc.tail) + } +} + +#[derive(Debug)] +struct Block { + nid: Nid, + preds: Vec, + succs: Vec, + instrs: regalloc2::InstRange, + params: Vec, + branch_blockparams: Vec, +} + +#[derive(Debug)] +struct Instr { + nid: Nid, + ops: Vec, +} + +pub struct Function<'a> { + sig: Sig, + nodes: &'a mut Nodes, + tys: &'a Types, + tail: bool, + visited: BitSet, + backrefs: Vec, + blocks: Vec, + instrs: Vec, +} + +impl core::fmt::Debug for Function<'_> { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + for (i, block) in self.blocks.iter().enumerate() { + writeln!(f, "sb{i}{:?}-{:?}:", block.params, block.preds)?; + + for inst in block.instrs.iter() { + let instr = &self.instrs[inst.index()]; + writeln!(f, "{}: i{:?}:{:?}", inst.index(), self.nodes[instr.nid].kind, instr.ops)?; + } + + writeln!(f, "eb{i}{:?}-{:?}:", block.branch_blockparams, block.succs)?; + } + Ok(()) + } +} + +impl<'a> Function<'a> { + fn new(nodes: &'a mut Nodes, tys: &'a Types, sig: Sig) -> Self { + let mut s = Self { + tys, + sig, + tail: true, + visited: Default::default(), + backrefs: vec![u16::MAX; nodes.values.len()], + blocks: Default::default(), + instrs: Default::default(), + nodes, + }; + s.visited.clear(s.nodes.values.len()); + s.emit_node(VOID, VOID); + s.add_block(0); + s.blocks.pop(); + s + } + + fn add_block(&mut self, nid: Nid) -> u16 { + if let Some(prev) = self.blocks.last_mut() { + prev.instrs = regalloc2::InstRange::new( + prev.instrs.first(), + regalloc2::Inst::new(self.instrs.len()), + ); + } + + self.blocks.push(Block { + nid, + preds: Default::default(), + succs: Default::default(), + instrs: regalloc2::InstRange::new( + regalloc2::Inst::new(self.instrs.len()), + regalloc2::Inst::new(self.instrs.len() + 1), + ), + params: Default::default(), + branch_blockparams: Default::default(), + }); + self.blocks.len() as u16 - 1 + } + + fn add_instr(&mut self, nid: Nid, ops: Vec) { + self.instrs.push(Instr { nid, ops }); + } + + fn urg(&mut self, nid: Nid) -> regalloc2::Operand { + regalloc2::Operand::reg_use(self.rg(nid)) + } + + fn drg(&mut self, nid: Nid) -> regalloc2::Operand { + regalloc2::Operand::reg_def(self.rg(nid)) + } + + fn rg(&self, nid: Nid) -> regalloc2::VReg { + debug_assert!( + !self.nodes.is_cfg(nid) || matches!(self.nodes[nid].kind, Kind::Call { .. }), + "{:?}", + self.nodes[nid] + ); + debug_assert_eq!(self.nodes[nid].lock_rc, 0, "{nid} {:?}", self.nodes[nid]); + debug_assert!(self.nodes[nid].kind != Kind::Phi || self.nodes[nid].ty != ty::Id::VOID); + regalloc2::VReg::new(nid as _, regalloc2::RegClass::Int) + } + + fn emit_node(&mut self, nid: Nid, prev: Nid) { + if matches!(self.nodes[nid].kind, Kind::Region | Kind::Loop) { + let prev_bref = self.backrefs[prev as usize]; + let node = self.nodes[nid].clone(); + + let idx = 1 + node.inputs.iter().position(|&i| i == prev).unwrap(); + + for ph in node.outputs { + if self.nodes[ph].kind != Kind::Phi || self.nodes[ph].ty == ty::Id::VOID { + continue; + } + + let rg = self.rg(self.nodes[ph].inputs[idx]); + self.blocks[prev_bref as usize].branch_blockparams.push(rg); + } + + self.add_instr(nid, vec![]); + + match (self.nodes[nid].kind, self.visited.set(nid)) { + (Kind::Loop, false) => { + for i in node.inputs { + self.bridge(i, nid); + } + return; + } + (Kind::Region, true) => return, + _ => {} + } + } else if !self.visited.set(nid) { + return; + } + + if self.nodes.is_never_used(nid, self.tys) { + self.nodes.lock(nid); + return; + } + + let mut node = self.nodes[nid].clone(); + match node.kind { + Kind::Start => { + debug_assert_matches!(self.nodes[node.outputs[0]].kind, Kind::Entry); + self.emit_node(node.outputs[0], VOID) + } + Kind::If => { + self.backrefs[nid as usize] = self.backrefs[prev as usize]; + + let &[_, cond] = node.inputs.as_slice() else { unreachable!() }; + let &[mut then, mut else_] = node.outputs.as_slice() else { unreachable!() }; + + if let Kind::BinOp { op } = self.nodes[cond].kind + && let Some((_, swapped)) = op.cond_op(node.ty) + { + if swapped { + mem::swap(&mut then, &mut else_); + } + let &[_, lhs, rhs] = self.nodes[cond].inputs.as_slice() else { unreachable!() }; + let ops = vec![self.urg(lhs), self.urg(rhs)]; + self.add_instr(nid, ops); + } else { + mem::swap(&mut then, &mut else_); + let ops = vec![self.urg(cond)]; + self.add_instr(nid, ops); + } + + self.emit_node(then, nid); + self.emit_node(else_, nid); + } + Kind::Region | Kind::Loop => { + self.backrefs[nid as usize] = self.add_block(nid); + if node.kind == Kind::Region { + for i in node.inputs { + self.bridge(i, nid); + } + } + let mut block = vec![]; + for ph in node.outputs.clone() { + if self.nodes[ph].kind != Kind::Phi || self.nodes[ph].ty == ty::Id::VOID { + continue; + } + block.push(self.rg(ph)); + } + self.blocks[self.backrefs[nid as usize] as usize].params = block; + self.reschedule_block(nid, &mut node.outputs); + for o in node.outputs.into_iter().rev() { + self.emit_node(o, nid); + } + } + Kind::Return => { + let ops = match self.tys.parama(self.sig.ret).0 { + None => vec![], + Some(PLoc::Reg(..)) if self.sig.ret.loc(self.tys) == Loc::Stack => { + vec![self.urg(self.nodes[node.inputs[1]].inputs[1])] + } + Some(PLoc::Reg(r, ..)) => { + vec![regalloc2::Operand::reg_fixed_use( + self.rg(node.inputs[1]), + regalloc2::PReg::new(r as _, regalloc2::RegClass::Int), + )] + } + Some(PLoc::WideReg(..)) => { + vec![self.urg(self.nodes[node.inputs[1]].inputs[1])] + } + Some(PLoc::Ref(..)) => { + vec![self.urg(self.nodes[node.inputs[1]].inputs[1]), self.urg(MEM)] + } + }; + + self.add_instr(nid, ops); + self.emit_node(node.outputs[0], nid); + } + Kind::Die => { + self.add_instr(nid, vec![]); + self.emit_node(node.outputs[0], nid); + } + Kind::CInt { .. } => { + let ops = vec![self.drg(nid)]; + self.add_instr(nid, ops); + } + Kind::Entry => { + self.backrefs[nid as usize] = self.add_block(nid); + + let (ret, mut parama) = self.tys.parama(self.sig.ret); + let mut typs = self.sig.args.args(); + #[expect(clippy::unnecessary_to_owned)] + let mut args = self.nodes[VOID].outputs[ARG_START..].to_owned().into_iter(); + while let Some(ty) = typs.next_value(self.tys) { + let arg = args.next().unwrap(); + debug_assert_eq!(self.nodes[arg].kind, Kind::Arg); + match parama.next(ty, self.tys) { + None => {} + Some(PLoc::Reg(r, _) | PLoc::WideReg(r, _) | PLoc::Ref(r, _)) => { + self.add_instr(NEVER, vec![regalloc2::Operand::reg_fixed_def( + self.rg(arg), + regalloc2::PReg::new(r as _, regalloc2::RegClass::Int), + )]); + } + } + } + + if let Some(PLoc::Ref(r, ..)) = ret { + self.add_instr(NEVER, vec![regalloc2::Operand::reg_fixed_def( + self.rg(MEM), + regalloc2::PReg::new(r as _, regalloc2::RegClass::Int), + )]); + } + + self.reschedule_block(nid, &mut node.outputs); + for o in node.outputs.into_iter().rev() { + self.emit_node(o, nid); + } + } + Kind::Then | Kind::Else => { + self.backrefs[nid as usize] = self.add_block(nid); + self.bridge(prev, nid); + self.reschedule_block(nid, &mut node.outputs); + for o in node.outputs.into_iter().rev() { + self.emit_node(o, nid); + } + } + Kind::BinOp { .. } => { + let &[_, lhs, rhs] = node.inputs.as_slice() else { unreachable!() }; + + let ops = if let Kind::CInt { .. } = self.nodes[rhs].kind + && self.nodes[rhs].lock_rc != 0 + { + vec![self.drg(nid), self.urg(lhs)] + } else { + vec![self.drg(nid), self.urg(lhs), self.urg(rhs)] + }; + self.add_instr(nid, ops); + } + Kind::UnOp { .. } => { + let ops = vec![self.drg(nid), self.urg(node.inputs[1])]; + self.add_instr(nid, ops); + } + Kind::Call { args, func } => { + self.tail &= func == ty::ECA; + self.backrefs[nid as usize] = self.backrefs[prev as usize]; + let mut ops = vec![]; + + let (ret, mut parama) = self.tys.parama(node.ty); + if ret.is_some() { + ops.push(regalloc2::Operand::reg_fixed_def( + self.rg(nid), + regalloc2::PReg::new(1, regalloc2::RegClass::Int), + )); + } + + let mut tys = args.args(); + let mut args = node.inputs[1..].iter(); + while let Some(ty) = tys.next_value(self.tys) { + let mut i = *args.next().unwrap(); + let Some(loc) = parama.next(ty, self.tys) else { continue }; + + match loc { + PLoc::Reg(r, _) if ty.loc(self.tys) == Loc::Reg => { + ops.push(regalloc2::Operand::reg_fixed_use( + self.rg(i), + regalloc2::PReg::new(r as _, regalloc2::RegClass::Int), + )); + } + PLoc::WideReg(r, size) | PLoc::Reg(r, size) => { + loop { + match self.nodes[i].kind { + Kind::Stre { .. } => i = self.nodes[i].inputs[2], + Kind::Load { .. } => i = self.nodes[i].inputs[1], + _ => break, + } + debug_assert_ne!(i, 0); + } + debug_assert!(i != 0); + ops.push(regalloc2::Operand::reg_fixed_use( + self.rg(i), + regalloc2::PReg::new(r as _, regalloc2::RegClass::Int), + )); + if size > 8 { + ops.push(regalloc2::Operand::reg_fixed_use( + self.rg(i), + regalloc2::PReg::new((r + 1) as _, regalloc2::RegClass::Int), + )); + } + } + PLoc::Ref(r, _) => { + loop { + match self.nodes[i].kind { + Kind::Stre { .. } => i = self.nodes[i].inputs[2], + Kind::Load { .. } => i = self.nodes[i].inputs[1], + _ => break, + } + debug_assert_ne!(i, 0); + } + debug_assert!(i != 0); + ops.push(regalloc2::Operand::reg_fixed_use( + self.rg(i), + regalloc2::PReg::new(r as _, regalloc2::RegClass::Int), + )); + } + } + } + + if let Some(PLoc::Ref(r, _)) = ret { + ops.push(regalloc2::Operand::reg_fixed_use( + self.rg(*node.inputs.last().unwrap()), + regalloc2::PReg::new(r as _, regalloc2::RegClass::Int), + )); + } + + self.add_instr(nid, ops); + + self.reschedule_block(nid, &mut node.outputs); + for o in node.outputs.into_iter().rev() { + if self.nodes[o].inputs[0] == nid + || (matches!(self.nodes[o].kind, Kind::Loop | Kind::Region) + && self.nodes[o].inputs[1] == nid) + { + self.emit_node(o, nid); + } + } + } + Kind::Global { .. } => { + let ops = vec![self.drg(nid)]; + self.add_instr(nid, ops); + } + Kind::Stck => { + let ops = vec![self.drg(nid)]; + self.add_instr(nid, ops); + } + Kind::Assert { .. } => unreachable!(), + Kind::End | Kind::Phi | Kind::Arg | Kind::Mem | Kind::Loops => {} + Kind::Load { .. } => { + let mut region = node.inputs[1]; + if self.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add }) + && self.nodes.is_const(self.nodes[region].inputs[2]) + && node.ty.loc(self.tys) == Loc::Reg + { + region = self.nodes[region].inputs[1] + } + let ops = match self.nodes[region].kind { + Kind::Stck => vec![self.drg(nid)], + _ => vec![self.drg(nid), self.urg(region)], + }; + self.add_instr(nid, ops); + } + Kind::Stre => { + debug_assert_ne!(self.tys.size_of(node.ty), 0); + let mut region = node.inputs[2]; + if self.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add }) + && self.nodes.is_const(self.nodes[region].inputs[2]) + && node.ty.loc(self.tys) == Loc::Reg + { + region = self.nodes[region].inputs[1] + } + let ops = match self.nodes[region].kind { + _ if node.ty.loc(self.tys) == Loc::Stack => { + if self.nodes[node.inputs[1]].kind == Kind::Arg { + vec![self.urg(region), self.urg(node.inputs[1])] + } else { + vec![self.urg(region), self.urg(self.nodes[node.inputs[1]].inputs[1])] + } + } + Kind::Stck => vec![self.urg(node.inputs[1])], + _ => vec![self.urg(region), self.urg(node.inputs[1])], + }; + self.add_instr(nid, ops); + } + } + } + + fn bridge(&mut self, pred: u16, succ: u16) { + if self.backrefs[pred as usize] == u16::MAX || self.backrefs[succ as usize] == u16::MAX { + return; + } + self.blocks[self.backrefs[pred as usize] as usize] + .succs + .push(regalloc2::Block::new(self.backrefs[succ as usize] as usize)); + self.blocks[self.backrefs[succ as usize] as usize] + .preds + .push(regalloc2::Block::new(self.backrefs[pred as usize] as usize)); + } + + fn reschedule_block(&mut self, from: Nid, outputs: &mut Vc) { + let from = Some(&from); + let mut buf = Vec::with_capacity(outputs.len()); + let mut seen = BitSet::default(); + seen.clear(self.nodes.values.len()); + + for &o in outputs.iter() { + if !self.nodes.is_cfg(o) { + continue; + } + + seen.set(o); + + let mut cursor = buf.len(); + buf.push(o); + while let Some(&n) = buf.get(cursor) { + for &i in &self.nodes[n].inputs[1..] { + if from == self.nodes[i].inputs.first() + && self.nodes[i] + .outputs + .iter() + .all(|&o| self.nodes[o].inputs.first() != from || seen.get(o)) + && seen.set(i) + { + buf.push(i); + } + } + cursor += 1; + } + } + + for &o in outputs.iter() { + if !seen.set(o) { + continue; + } + let mut cursor = buf.len(); + buf.push(o); + while let Some(&n) = buf.get(cursor) { + for &i in &self.nodes[n].inputs[1..] { + if from == self.nodes[i].inputs.first() + && self.nodes[i] + .outputs + .iter() + .all(|&o| self.nodes[o].inputs.first() != from || seen.get(o)) + && seen.set(i) + { + buf.push(i); + } + } + cursor += 1; + } + } + + debug_assert!( + outputs.len() == buf.len() || outputs.len() == buf.len() + 1, + "{:?} {:?}", + outputs, + buf + ); + + if buf.len() + 1 == outputs.len() { + outputs.remove(outputs.len() - 1); + } + outputs.copy_from_slice(&buf); + } +} + +impl regalloc2::Function for Function<'_> { + fn num_insts(&self) -> usize { + self.instrs.len() + } + + fn num_blocks(&self) -> usize { + self.blocks.len() + } + + fn entry_block(&self) -> regalloc2::Block { + regalloc2::Block(0) + } + + fn block_insns(&self, block: regalloc2::Block) -> regalloc2::InstRange { + self.blocks[block.index()].instrs + } + + fn block_succs(&self, block: regalloc2::Block) -> &[regalloc2::Block] { + &self.blocks[block.index()].succs + } + + fn block_preds(&self, block: regalloc2::Block) -> &[regalloc2::Block] { + &self.blocks[block.index()].preds + } + + fn block_params(&self, block: regalloc2::Block) -> &[regalloc2::VReg] { + &self.blocks[block.index()].params + } + + fn is_ret(&self, insn: regalloc2::Inst) -> bool { + matches!(self.nodes[self.instrs[insn.index()].nid].kind, Kind::Return | Kind::Die) + } + + fn is_branch(&self, insn: regalloc2::Inst) -> bool { + matches!( + self.nodes[self.instrs[insn.index()].nid].kind, + Kind::If | Kind::Then | Kind::Else | Kind::Entry | Kind::Loop | Kind::Region + ) + } + + fn branch_blockparams( + &self, + block: regalloc2::Block, + _insn: regalloc2::Inst, + _succ_idx: usize, + ) -> &[regalloc2::VReg] { + debug_assert!( + self.blocks[block.index()].succs.len() == 1 + || self.blocks[block.index()].branch_blockparams.is_empty() + ); + + &self.blocks[block.index()].branch_blockparams + } + + fn inst_operands(&self, insn: regalloc2::Inst) -> &[regalloc2::Operand] { + &self.instrs[insn.index()].ops + } + + fn inst_clobbers(&self, insn: regalloc2::Inst) -> regalloc2::PRegSet { + let node = &self.nodes[self.instrs[insn.index()].nid]; + if matches!(node.kind, Kind::Call { .. }) { + let mut set = regalloc2::PRegSet::default(); + let returns = self.tys.parama(node.ty).0.is_some(); + for i in 1 + returns as usize..13 { + set.add(regalloc2::PReg::new(i, regalloc2::RegClass::Int)); + } + set + } else { + regalloc2::PRegSet::default() + } + } + + fn num_vregs(&self) -> usize { + self.nodes.values.len() + } + + fn spillslot_size(&self, regclass: regalloc2::RegClass) -> usize { + match regclass { + regalloc2::RegClass::Int => 1, + regalloc2::RegClass::Float => unreachable!(), + regalloc2::RegClass::Vector => unreachable!(), + } + } +} diff --git a/lang/src/utils.rs b/lang/src/utils.rs index ca781bf..b0d3477 100644 --- a/lang/src/utils.rs +++ b/lang/src/utils.rs @@ -62,6 +62,12 @@ impl BitSet { const INLINE_ELEMS: usize = Self::UNIT - 1; const UNIT: usize = core::mem::size_of::() * 8; + pub fn with_capacity(len: usize) -> Self { + let mut s = Self::default(); + s.reserve(len); + s + } + fn is_inline(&self) -> bool { unsafe { self.inline & Self::FLAG != 0 } }