Compare commits
117 Commits
1.0.0-beta
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| fbbaf09f73 | |||
| 4cb0ce9d69 | |||
| 5a65f7912f | |||
| c04dc01258 | |||
| d5a4168b0d | |||
| 663f327ccf | |||
| 84233c8f49 | |||
| 07b0403e9b | |||
| 28934ea6fc | |||
| 7dd06567bb | |||
| 77107ef95e | |||
| 1d0fd9dd0a | |||
| 3a5766186a | |||
| 8d8d6ac336 | |||
| fa95da97ae | |||
| 407c681cb6 | |||
| 2dd482c9c2 | |||
| 1a5823c59c | |||
| 3ebe16b80b | |||
| 7a6f99479a | |||
| f4bce14299 | |||
| aa1de16f4c | |||
| 89b6fc1a71 | |||
| ceee2f286a | |||
| dcb4e76a40 | |||
| 6dccab8b12 | |||
| 7b4f38406d | |||
| 9e37ae7aac | |||
| 3e0367fb1e | |||
| 685520a6cf | |||
| 0203213b28 | |||
| c23160bc32 | |||
| fe6fe6c435 | |||
| 3e85ed2705 | |||
| d50a748884 | |||
| c44d588b30 | |||
| 0e23ab4636 | |||
| ce2473a715 | |||
| 1a8535516c | |||
| e1ac019ecd | |||
| 82b67dfaaa | |||
| a5c7823a29 | |||
| 3dba4a79ff | |||
| 2a8842658d | |||
| 8d32f2bbad | |||
| 0ee9d3bf7d | |||
| 1dd4bbbb05 | |||
| 8cbc65422e | |||
| 3cd4121951 | |||
| c84954480b | |||
| 67106ea17b | |||
| 015c111b29 | |||
| aad3c93068 | |||
| 1ba0de442a | |||
| 1c3386bc9a | |||
| 8a178387ca | |||
| b93b7aa52b | |||
| 1275063dc2 | |||
| 02522dd36d | |||
| 5b7c3d5b3a | |||
| 70a968d7a0 | |||
| a9d5a4d03b | |||
| 11d93e4adf | |||
| c2f6cfb8e6 | |||
| 34612e98ce | |||
| 3b4835cff8 | |||
| 627d1bcfa5 | |||
| ae6796acfc | |||
| 5d19d38682 | |||
| 4a33e7d123 | |||
| 79ecb3b9ba | |||
| 28157ae4b8 | |||
| 8d27a6e7bd | |||
| 136e9f9d1f | |||
| 8e41deb653 | |||
| 766a853b48 | |||
| 109fedb624 | |||
| d27ec2bb70 | |||
| 5706fd99e3 | |||
| 3b3b21d4dc | |||
| 7809aeb2b5 | |||
| cd2ebb5224 | |||
| 8b0d09c08d | |||
| a215a2116a | |||
| 0abeb0c4cd | |||
| ac0d79f816 | |||
| 909728a564 | |||
| 82537224e7 | |||
| 79b3c6b3ef | |||
| 1ae164b1d6 | |||
| 9b9baabc81 | |||
| 13c462cb9b | |||
| 8739fe16d1 | |||
| 1438ba7bd1 | |||
| d9911a8ff5 | |||
| 48dd17b320 | |||
| dcc53498e7 | |||
| 018f3e2561 | |||
| 6a9133baff | |||
| 3f3de9e2c0 | |||
| b965ca11b9 | |||
| 3537318466 | |||
| dbc43f51ee | |||
| c7f1b81c9d | |||
| a51a2c8f56 | |||
| 101ee2d8e5 | |||
| a6844b919b | |||
| 4ea0913842 | |||
| bb9f69ee53 | |||
| 97a5c3a65e | |||
| 8595da0c30 | |||
| dae39bc9d2 | |||
| 658450993a | |||
| 3f6d26679d | |||
| 16082752e2 | |||
| 8a71ce3629 | |||
| 81d418c6d8 |
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[submodule "reid-llvm-lib"]
|
||||||
|
path = reid-llvm-lib
|
||||||
|
url = gitea@git.teascade.net:teascade/reid-llvm-lib.git
|
||||||
326
Cargo.lock
generated
326
Cargo.lock
generated
@ -17,12 +17,59 @@ version = "2.0.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
|
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "android-tzdata"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "android_system_properties"
|
||||||
|
version = "0.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anyhow"
|
name = "anyhow"
|
||||||
version = "1.0.98"
|
version = "1.0.98"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487"
|
checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "argh"
|
||||||
|
version = "0.1.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "34ff18325c8a36b82f992e533ece1ec9f9a9db446bd1c14d4f936bac88fcd240"
|
||||||
|
dependencies = [
|
||||||
|
"argh_derive",
|
||||||
|
"argh_shared",
|
||||||
|
"rust-fuzzy-search",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "argh_derive"
|
||||||
|
version = "0.1.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "adb7b2b83a50d329d5d8ccc620f5c7064028828538bdf5646acd60dc1f767803"
|
||||||
|
dependencies = [
|
||||||
|
"argh_shared",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "argh_shared"
|
||||||
|
version = "0.1.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a464143cc82dedcdc3928737445362466b7674b5db4e2eb8e869846d6d84f4f6"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-trait"
|
name = "async-trait"
|
||||||
version = "0.1.88"
|
version = "0.1.88"
|
||||||
@ -78,6 +125,12 @@ version = "2.9.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
|
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bumpalo"
|
||||||
|
version = "3.19.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bytes"
|
name = "bytes"
|
||||||
version = "1.10.1"
|
version = "1.10.1"
|
||||||
@ -99,6 +152,20 @@ version = "1.0.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
|
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "chrono"
|
||||||
|
version = "0.4.41"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d"
|
||||||
|
dependencies = [
|
||||||
|
"android-tzdata",
|
||||||
|
"iana-time-zone",
|
||||||
|
"js-sys",
|
||||||
|
"num-traits",
|
||||||
|
"wasm-bindgen",
|
||||||
|
"windows-link",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "colored"
|
name = "colored"
|
||||||
version = "3.0.0"
|
version = "3.0.0"
|
||||||
@ -108,6 +175,12 @@ dependencies = [
|
|||||||
"windows-sys",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "core-foundation-sys"
|
||||||
|
version = "0.8.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crossbeam-utils"
|
name = "crossbeam-utils"
|
||||||
version = "0.8.21"
|
version = "0.8.21"
|
||||||
@ -250,12 +323,42 @@ version = "0.14.5"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
|
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hermit-abi"
|
||||||
|
version = "0.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "httparse"
|
name = "httparse"
|
||||||
version = "1.10.1"
|
version = "1.10.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
|
checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "iana-time-zone"
|
||||||
|
version = "0.1.63"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b0c919e5debc312ad217002b8048a17b7d83f80703865bbfcfebb0458b0b27d8"
|
||||||
|
dependencies = [
|
||||||
|
"android_system_properties",
|
||||||
|
"core-foundation-sys",
|
||||||
|
"iana-time-zone-haiku",
|
||||||
|
"js-sys",
|
||||||
|
"log",
|
||||||
|
"wasm-bindgen",
|
||||||
|
"windows-core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "iana-time-zone-haiku"
|
||||||
|
version = "0.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "icu_collections"
|
name = "icu_collections"
|
||||||
version = "2.0.0"
|
version = "2.0.0"
|
||||||
@ -374,12 +477,33 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "is-terminal"
|
||||||
|
version = "0.4.16"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9"
|
||||||
|
dependencies = [
|
||||||
|
"hermit-abi",
|
||||||
|
"libc",
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itoa"
|
name = "itoa"
|
||||||
version = "1.0.15"
|
version = "1.0.15"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
|
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "js-sys"
|
||||||
|
version = "0.3.77"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
|
||||||
|
dependencies = [
|
||||||
|
"once_cell",
|
||||||
|
"wasm-bindgen",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.5.0"
|
version = "1.5.0"
|
||||||
@ -422,6 +546,12 @@ dependencies = [
|
|||||||
"scopeguard",
|
"scopeguard",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "log"
|
||||||
|
version = "0.4.27"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lsp-types"
|
name = "lsp-types"
|
||||||
version = "0.94.1"
|
version = "0.94.1"
|
||||||
@ -654,38 +784,55 @@ checksum = "53a49587ad06b26609c52e423de037e7f57f20d53535d66e08c695f347df952a"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "reid"
|
name = "reid"
|
||||||
version = "1.0.0-beta.2"
|
version = "1.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"argh",
|
||||||
"colored",
|
"colored",
|
||||||
|
"log",
|
||||||
"reid-lib",
|
"reid-lib",
|
||||||
|
"stderrlog",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "reid-lib"
|
name = "reid-language-server"
|
||||||
version = "1.0.0-beta.2"
|
version = "1.0.0"
|
||||||
dependencies = [
|
|
||||||
"llvm-sys",
|
|
||||||
"thiserror",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "reid-lsp"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"dashmap 6.1.0",
|
"dashmap 6.1.0",
|
||||||
"reid",
|
"reid",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"socket",
|
"socket",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tower-lsp",
|
"tower-lsp",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "reid-lib"
|
||||||
|
version = "1.0.0"
|
||||||
|
dependencies = [
|
||||||
|
"llvm-sys",
|
||||||
|
"thiserror",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rust-fuzzy-search"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a157657054ffe556d8858504af8a672a054a6e0bd9e8ee531059100c0fa11bb2"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustc-demangle"
|
name = "rustc-demangle"
|
||||||
version = "0.1.26"
|
version = "0.1.26"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace"
|
checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustversion"
|
||||||
|
version = "1.0.22"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ryu"
|
name = "ryu"
|
||||||
version = "1.0.20"
|
version = "1.0.20"
|
||||||
@ -800,6 +947,19 @@ version = "1.2.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
|
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "stderrlog"
|
||||||
|
version = "0.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "61c910772f992ab17d32d6760e167d2353f4130ed50e796752689556af07dc6b"
|
||||||
|
dependencies = [
|
||||||
|
"chrono",
|
||||||
|
"is-terminal",
|
||||||
|
"log",
|
||||||
|
"termcolor",
|
||||||
|
"thread_local",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.104"
|
version = "2.0.104"
|
||||||
@ -822,6 +982,15 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "termcolor"
|
||||||
|
version = "1.1.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror"
|
name = "thiserror"
|
||||||
version = "1.0.69"
|
version = "1.0.69"
|
||||||
@ -842,6 +1011,15 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thread_local"
|
||||||
|
version = "1.1.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tinystr"
|
name = "tinystr"
|
||||||
version = "0.8.1"
|
version = "0.8.1"
|
||||||
@ -1017,6 +1195,132 @@ version = "0.11.1+wasi-snapshot-preview1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
|
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasm-bindgen"
|
||||||
|
version = "0.2.100"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"once_cell",
|
||||||
|
"rustversion",
|
||||||
|
"wasm-bindgen-macro",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasm-bindgen-backend"
|
||||||
|
version = "0.2.100"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
|
||||||
|
dependencies = [
|
||||||
|
"bumpalo",
|
||||||
|
"log",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
"wasm-bindgen-shared",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasm-bindgen-macro"
|
||||||
|
version = "0.2.100"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
|
||||||
|
dependencies = [
|
||||||
|
"quote",
|
||||||
|
"wasm-bindgen-macro-support",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasm-bindgen-macro-support"
|
||||||
|
version = "0.2.100"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
"wasm-bindgen-backend",
|
||||||
|
"wasm-bindgen-shared",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasm-bindgen-shared"
|
||||||
|
version = "0.2.100"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-util"
|
||||||
|
version = "0.1.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
|
||||||
|
dependencies = [
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-core"
|
||||||
|
version = "0.61.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3"
|
||||||
|
dependencies = [
|
||||||
|
"windows-implement",
|
||||||
|
"windows-interface",
|
||||||
|
"windows-link",
|
||||||
|
"windows-result",
|
||||||
|
"windows-strings",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-implement"
|
||||||
|
version = "0.60.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a47fddd13af08290e67f4acabf4b459f647552718f683a7b415d290ac744a836"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-interface"
|
||||||
|
version = "0.59.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bd9211b69f8dcdfa817bfd14bf1c97c9188afa36f4750130fcdf3f400eca9fa8"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-link"
|
||||||
|
version = "0.1.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-result"
|
||||||
|
version = "0.3.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6"
|
||||||
|
dependencies = [
|
||||||
|
"windows-link",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-strings"
|
||||||
|
version = "0.4.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57"
|
||||||
|
dependencies = [
|
||||||
|
"windows-link",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-sys"
|
name = "windows-sys"
|
||||||
version = "0.59.0"
|
version = "0.59.0"
|
||||||
|
|||||||
@ -73,7 +73,12 @@ Big features that I want later but are not necessary:
|
|||||||
- ~~Asymmetric binary operations (e.g. string + u32)~~ (DONE)
|
- ~~Asymmetric binary operations (e.g. string + u32)~~ (DONE)
|
||||||
- ~~Error handling~~ (Not Doing It)
|
- ~~Error handling~~ (Not Doing It)
|
||||||
- ~~Lexing & parsing of whitespace and comments as well~~ (DONE)
|
- ~~Lexing & parsing of whitespace and comments as well~~ (DONE)
|
||||||
- LSP implementation
|
- ~~LSP implementation~~ (DONE)
|
||||||
|
- ~~Syntax Highlighting~~ (DONE)
|
||||||
|
- ~~Semantic Highlighting~~ (DONE)
|
||||||
|
- ~~Go-To-Definition~~ (DONE)
|
||||||
|
- ~~Find-All-References~~ (DONE)
|
||||||
|
- ~~Refactoring~~ (DONE)
|
||||||
|
|
||||||
Smaller features:
|
Smaller features:
|
||||||
- ~~Hex-numbers~~ (DONE)
|
- ~~Hex-numbers~~ (DONE)
|
||||||
|
|||||||
@ -7,16 +7,6 @@ pre-existing binary-operators, but also some regular functions and associated
|
|||||||
functions (that every type has by-default). This document lists them all (except
|
functions (that every type has by-default). This document lists them all (except
|
||||||
for the binary operators, because there are hundreds of those).
|
for the binary operators, because there are hundreds of those).
|
||||||
|
|
||||||
### Global Intrinsics
|
|
||||||
|
|
||||||
#### `malloc(size: u64) -> *u8`
|
|
||||||
|
|
||||||
Allocates `size` bytes and returns a pointer of `u8` of length `size`.
|
|
||||||
|
|
||||||
```rust
|
|
||||||
i32::malloc(40); // Reserves 40 bytes
|
|
||||||
```
|
|
||||||
|
|
||||||
### Macro Intrinsics
|
### Macro Intrinsics
|
||||||
|
|
||||||
#### `include_bytes!(path: *char) -> &[u8; _]`
|
#### `include_bytes!(path: *char) -> &[u8; _]`
|
||||||
@ -43,15 +33,155 @@ Returns a null-pointer of type `T`.
|
|||||||
i32::null(); // Returns *i32 (null-ptr)
|
i32::null(); // Returns *i32 (null-ptr)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### `<T>::is_null(val: T) -> bool`
|
||||||
|
|
||||||
|
Returns a boolean representing if `val` is a nullptr or not.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
i32::is_null(i32::null()); // Returns true
|
||||||
|
```
|
||||||
|
|
||||||
#### `<T>::malloc(size: u64) -> *T`
|
#### `<T>::malloc(size: u64) -> *T`
|
||||||
|
|
||||||
Allocates `T::sizeof() * size` bytes and returns a pointer to `T`.
|
Allocates `T::sizeof() * size` bytes and returns a pointer to `T`.
|
||||||
|
|
||||||
**Note:** This does not seem to work correctly currently.
|
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
i32::malloc(30); // Returns *i32
|
i32::malloc(30); // Returns *i32
|
||||||
|
|
||||||
// Equivalent to
|
// Equivalent to
|
||||||
malloc(i32::sizeof() * 30) as *i32
|
malloc(i32::sizeof() * 30) as *i32
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### `<T>::memcpy(destination: *T, source: *T, size: u64)`
|
||||||
|
|
||||||
|
Copies `T::sizeof() * size` bytes from pointer `source` to pointer
|
||||||
|
`destination`.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let a = i32::malloc(30);
|
||||||
|
let b = i32::malloc(30);
|
||||||
|
|
||||||
|
// Copies the contents from b to a
|
||||||
|
i32::memcpy(a, b, 30);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
#### `<T>::min(a: T, b: T) -> T`
|
||||||
|
*Note: (only on integer- and floating-point values)*
|
||||||
|
|
||||||
|
Returns the smaller of `a` and `b`.
|
||||||
|
|
||||||
|
#### `<T>::max(a: T, b: T) -> T`
|
||||||
|
*Note: (only on integer- and floating-point values)*
|
||||||
|
|
||||||
|
Returns the larger of `a` and `b`.
|
||||||
|
|
||||||
|
#### `<T>::abs(value: T) -> T`
|
||||||
|
*Note: (only on signed integer and floating-point values)*
|
||||||
|
|
||||||
|
Returns the absolute value of `value`.
|
||||||
|
|
||||||
|
#### `<T>::sqrt(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Calculates the square-root of `value`
|
||||||
|
|
||||||
|
#### `<T>::pow(value: T, exponent: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Returns `value` raised to the exponent of `exponent`.
|
||||||
|
|
||||||
|
#### `<T>::powi(value: T, exponent: u64) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Returns `value` raised to the exponent of `exponent`.
|
||||||
|
|
||||||
|
#### `<T>::sin(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Calculates sine of `value`
|
||||||
|
|
||||||
|
#### `<T>::cos(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Calculates cosine of `value`
|
||||||
|
|
||||||
|
#### `<T>::tan(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Calculates tangent of `value`
|
||||||
|
|
||||||
|
#### `<T>::asin(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Calculates arcsine of `value`
|
||||||
|
|
||||||
|
#### `<T>::acos(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Calculates arccosine of `value`
|
||||||
|
|
||||||
|
#### `<T>::atan(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Calculates arctangent of `value`
|
||||||
|
|
||||||
|
#### `<T>::atan2(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Calculates 2-argument arctangent of `value`
|
||||||
|
|
||||||
|
#### `<T>::sinh(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Calculates hyperbolic sine of `value`
|
||||||
|
|
||||||
|
#### `<T>::cosh(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Calculates hyperbolic cosine of `value`
|
||||||
|
|
||||||
|
#### `<T>::tanh(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Calculates hyperbolic tangent of `value`
|
||||||
|
|
||||||
|
#### `<T>::log(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Returns logₑ of `value`
|
||||||
|
|
||||||
|
#### `<T>::log2(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Returns log₂ of `value`
|
||||||
|
|
||||||
|
#### `<T>::log10(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Returns log₁₀ of `value`
|
||||||
|
|
||||||
|
#### `<T>::round(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Rounds `value` to the nearest integer
|
||||||
|
|
||||||
|
#### `<T>::trunc(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Truncates `value` to the integer nearest to `0`.
|
||||||
|
|
||||||
|
#### `<T>::ceil(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Rounds `value` towards positive infinity.
|
||||||
|
|
||||||
|
#### `<T>::floor(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Rounds `value` towards negative infinity.
|
||||||
|
|
||||||
|
#### `<T>::even(value: T) -> T`
|
||||||
|
*Note: (only on floating-point numbers)*
|
||||||
|
|
||||||
|
Rounds `value` to the closest even integer.
|
||||||
@ -64,10 +64,6 @@ _deprecated: Use `String::concat()`_
|
|||||||
|
|
||||||
## General
|
## General
|
||||||
|
|
||||||
#### `pub fn allocate(size: u64) -> *u8`
|
|
||||||
|
|
||||||
Unsafely allocates `size` bytes of memory from the stack, and returns a pointer to it, which must be manually freed.
|
|
||||||
|
|
||||||
## Maths
|
## Maths
|
||||||
|
|
||||||
#### `pub fn clamp(min: f32, max: f32, value: f32) -> f32`
|
#### `pub fn clamp(min: f32, max: f32, value: f32) -> f32`
|
||||||
|
|||||||
@ -2,11 +2,14 @@ import std::print;
|
|||||||
import std::from_str;
|
import std::from_str;
|
||||||
import std::String;
|
import std::String;
|
||||||
|
|
||||||
|
/// Asd
|
||||||
struct Otus {
|
struct Otus {
|
||||||
field: u32,
|
field: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Otus {
|
impl Otus {
|
||||||
|
/// Some test documentation here.
|
||||||
|
/// On a second line
|
||||||
fn test(&self) -> u32 {
|
fn test(&self) -> u32 {
|
||||||
*self.field
|
*self.field
|
||||||
}
|
}
|
||||||
@ -18,12 +21,18 @@ impl i32 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Hello there!
|
||||||
|
fn test() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> u32 {
|
fn main() -> u32 {
|
||||||
let otus = Otus { field: 17 };
|
let otus = Otus { field: 17 };
|
||||||
print(from_str("otus: ") + Otus::test(&otus) as u64);
|
print(from_str("otus: ") + Otus::test(&otus) as u64);
|
||||||
print(from_str("i32: ") + i32::test(54) as u64);
|
print(from_str("i32: ") + i32::test(54) as u64);
|
||||||
print(from_str("sizeof i32: ") + i32::sizeof());
|
print(from_str("sizeof i32: ") + i32::sizeof());
|
||||||
|
|
||||||
|
|
||||||
let nullptr = i32::null();
|
let nullptr = i32::null();
|
||||||
let mut list = u64::malloc(15);
|
let mut list = u64::malloc(15);
|
||||||
list[4] = 17;
|
list[4] = 17;
|
||||||
|
|||||||
@ -22,6 +22,8 @@ fn main() -> u32 {
|
|||||||
let otus = Otus { field: 17 };
|
let otus = Otus { field: 17 };
|
||||||
print(from_str("otus: ") + otus.test() as u64);
|
print(from_str("otus: ") + otus.test() as u64);
|
||||||
|
|
||||||
|
otus.field;
|
||||||
|
|
||||||
|
|
||||||
return otus.test();
|
return otus.test();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
// Arithmetic, function calls and imports!
|
// Arithmetic, function calls and imports!
|
||||||
|
|
||||||
|
/// Test stuff
|
||||||
fn changer(param: &mut u32) {
|
fn changer(param: &mut u32) {
|
||||||
*param = 17;
|
*param = 17;
|
||||||
}
|
}
|
||||||
|
|||||||
10
examples/compilcated_main.reid
Normal file
10
examples/compilcated_main.reid
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import complicated_imported::Foo;
|
||||||
|
import complicated_imported::A;
|
||||||
|
import complicated_imported::AResult;
|
||||||
|
|
||||||
|
fn main() -> i32 {
|
||||||
|
let foo = Foo {};
|
||||||
|
let a = A::new();
|
||||||
|
foo.foo(&a.a);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
12
examples/complicated_imported.reid
Normal file
12
examples/complicated_imported.reid
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
struct A {}
|
||||||
|
struct AResult { a: A }
|
||||||
|
impl A {
|
||||||
|
pub fn new() -> AResult {
|
||||||
|
AResult { a: A {} }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Foo {}
|
||||||
|
impl Foo {
|
||||||
|
pub fn foo(&self, a: &A) {}
|
||||||
|
}
|
||||||
@ -1,13 +1,12 @@
|
|||||||
a
|
// Main
|
||||||
(
|
fn main() -> bool {
|
||||||
b
|
return 144 == fibonacci(0xc);
|
||||||
)
|
}
|
||||||
x
|
|
||||||
(
|
// Fibonacci
|
||||||
(
|
fn fibonacci(value: u16) -> u16 {
|
||||||
c
|
if value <= 2 {
|
||||||
xyz
|
return 1;
|
||||||
)
|
}
|
||||||
)
|
fibonacci(value - 1) + fibonacci(value - 2)
|
||||||
(
|
}
|
||||||
a
|
|
||||||
6
examples/foreign_init.reid
Normal file
6
examples/foreign_init.reid
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import foreign_struct::Vec2;
|
||||||
|
|
||||||
|
fn main() -> u32 {
|
||||||
|
let a = Vec2 {x: 16, y: 32};
|
||||||
|
return a.x;
|
||||||
|
}
|
||||||
1
examples/foreign_struct.reid
Normal file
1
examples/foreign_struct.reid
Normal file
@ -0,0 +1 @@
|
|||||||
|
struct Vec2 { x: u32, y: u32 }
|
||||||
9
examples/loop_edge_case.reid
Normal file
9
examples/loop_edge_case.reid
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
fn main() -> i32 {
|
||||||
|
for i in 0..1 {
|
||||||
|
let j = i;
|
||||||
|
if i != j {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
15
examples/math_intrinsics.reid
Normal file
15
examples/math_intrinsics.reid
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
extern fn printf(message: *char, num: f64);
|
||||||
|
|
||||||
|
fn main() -> i32 {
|
||||||
|
let b = 5;
|
||||||
|
let mut otus = i32::malloc(1);
|
||||||
|
otus[0] = 10500300;
|
||||||
|
let potus = i32::malloc(1);
|
||||||
|
i32::memcpy(potus, otus, 1);
|
||||||
|
|
||||||
|
printf("log10 %f\n", f64::round(123.3) as f64);
|
||||||
|
printf("sqrt %f\n", f64::sqrt(2) as f64);
|
||||||
|
printf("log10 %f\n", potus[0] as f64);
|
||||||
|
return potus[0];
|
||||||
|
}
|
||||||
8
examples/module_importee.reid
Normal file
8
examples/module_importee.reid
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
struct Otus {
|
||||||
|
field: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test() -> Otus {
|
||||||
|
Otus {field: 4}
|
||||||
|
}
|
||||||
11
examples/module_impoter.reid
Normal file
11
examples/module_impoter.reid
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// Arithmetic, function calls and imports!
|
||||||
|
|
||||||
|
import module_importee::Otus;
|
||||||
|
import module_importee::test;
|
||||||
|
|
||||||
|
fn main() -> u32 {
|
||||||
|
let value = 0b110;
|
||||||
|
let other = 0o17;
|
||||||
|
|
||||||
|
return value * other + test().field * -value;
|
||||||
|
}
|
||||||
4
examples/nullptr_comparison.reid
Normal file
4
examples/nullptr_comparison.reid
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
fn main() -> bool {
|
||||||
|
let ptr = i32::null();
|
||||||
|
return i32::is_null(ptr);
|
||||||
|
}
|
||||||
@ -16,7 +16,7 @@ BINARY="$(echo $1 | cut -d'.' -f1)"".out"
|
|||||||
|
|
||||||
echo $1
|
echo $1
|
||||||
|
|
||||||
cargo run --example cli $@ && \
|
cargo run -p reid -- $@ && \
|
||||||
./$BINARY ; echo "Return value: ""$?"
|
./$BINARY ; echo "Return value: ""$?"
|
||||||
|
|
||||||
## Command from: clang -v hello.o -o test
|
## Command from: clang -v hello.o -o test
|
||||||
|
|||||||
1
reid-llvm-lib
Submodule
1
reid-llvm-lib
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 100cd96a6dc3bb882ce60e78c4eab47e77fdd8c4
|
||||||
@ -1,12 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "reid-lib"
|
|
||||||
version = "1.0.0-beta.2"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
## LLVM Bindings
|
|
||||||
llvm-sys = {version ="201.0.1" }
|
|
||||||
## Make it easier to generate errors
|
|
||||||
thiserror = "1.0.44"
|
|
||||||
@ -1,56 +0,0 @@
|
|||||||
use reid_lib::{CmpPredicate, ConstValueKind, Context, FunctionFlags, Instr, TerminatorKind, Type};
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
use ConstValueKind::*;
|
|
||||||
use Instr::*;
|
|
||||||
|
|
||||||
let context = Context::new("libtest");
|
|
||||||
|
|
||||||
let module = context.module("test", true);
|
|
||||||
|
|
||||||
let main = module.function("main", None, Type::I32, Vec::new(), FunctionFlags::default());
|
|
||||||
let mut m_entry = main.block("entry");
|
|
||||||
|
|
||||||
let fibonacci = module.function("fibonacci", None, Type::I32, vec![Type::I32], FunctionFlags::default());
|
|
||||||
|
|
||||||
let arg = m_entry.build_named("const", Constant(I32(5))).unwrap();
|
|
||||||
let fibonacci_call = m_entry
|
|
||||||
.build_named("const", FunctionCall(fibonacci.value(), vec![arg]))
|
|
||||||
.unwrap();
|
|
||||||
m_entry.terminate(TerminatorKind::Ret(fibonacci_call)).unwrap();
|
|
||||||
|
|
||||||
let mut f_entry = fibonacci.block("entry");
|
|
||||||
|
|
||||||
let num_3 = f_entry.build_named("const", Constant(I32(3))).unwrap();
|
|
||||||
let param_n = f_entry.build_named("param", Param(0)).unwrap();
|
|
||||||
let cond = f_entry
|
|
||||||
.build_named("cmp", ICmp(CmpPredicate::LT, param_n, num_3))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let mut then_b = fibonacci.block("then");
|
|
||||||
let mut else_b = fibonacci.block("else");
|
|
||||||
|
|
||||||
f_entry
|
|
||||||
.terminate(TerminatorKind::CondBr(cond, then_b.value(), else_b.value()))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let ret_const = then_b.build_named("const", Constant(I32(1))).unwrap();
|
|
||||||
then_b.terminate(TerminatorKind::Ret(ret_const)).unwrap();
|
|
||||||
|
|
||||||
let const_1 = else_b.build_named("const", Constant(I32(1))).unwrap();
|
|
||||||
let const_2 = else_b.build_named("const", Constant(I32(2))).unwrap();
|
|
||||||
let param_1 = else_b.build_named("sub", Sub(param_n, const_1)).unwrap();
|
|
||||||
let param_2 = else_b.build_named("sub", Sub(param_n, const_2)).unwrap();
|
|
||||||
let call_1 = else_b
|
|
||||||
.build_named("fibonacci", FunctionCall(fibonacci.value(), vec![param_1]))
|
|
||||||
.unwrap();
|
|
||||||
let call_2 = else_b
|
|
||||||
.build_named("fibonacci", FunctionCall(fibonacci.value(), vec![param_2]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let add = else_b.build_named("add", Add(call_1, call_2)).unwrap();
|
|
||||||
|
|
||||||
else_b.terminate(TerminatorKind::Ret(add)).unwrap();
|
|
||||||
|
|
||||||
context.compile(None, Vec::new());
|
|
||||||
}
|
|
||||||
@ -1,782 +0,0 @@
|
|||||||
//! This module contains simply [`Builder`] and it's related utility Values.
|
|
||||||
//! Builder is the actual struct being modified when building the LLIR.
|
|
||||||
|
|
||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
Block, BlockData, CompileResult, ConstValueKind, CustomTypeKind, ErrorKind, FunctionData, Instr, InstructionData,
|
|
||||||
ModuleData, NamedStruct, TerminatorKind, Type, TypeCategory, TypeData,
|
|
||||||
debug_information::{
|
|
||||||
DebugInformation, DebugLocationValue, DebugMetadataValue, DebugScopeValue, InstructionDebugRecordData,
|
|
||||||
},
|
|
||||||
util::match_types,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Clone, Hash, Copy, PartialEq, Eq)]
|
|
||||||
pub struct ModuleValue(pub(crate) usize);
|
|
||||||
|
|
||||||
#[derive(Clone, Hash, Copy, PartialEq, Eq)]
|
|
||||||
pub struct TypeValue(pub(crate) ModuleValue, pub(crate) usize);
|
|
||||||
|
|
||||||
#[derive(Clone, Hash, Copy, PartialEq, Eq)]
|
|
||||||
pub struct FunctionValue(pub(crate) ModuleValue, pub(crate) usize);
|
|
||||||
|
|
||||||
#[derive(Clone, Hash, Copy, PartialEq, Eq)]
|
|
||||||
pub struct BlockValue(pub(crate) FunctionValue, pub(crate) usize);
|
|
||||||
|
|
||||||
#[derive(Clone, Hash, Copy, PartialEq, Eq)]
|
|
||||||
pub struct InstructionValue(pub(crate) BlockValue, pub(crate) usize);
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Hash, Copy, PartialEq, Eq)]
|
|
||||||
pub struct ConstantValue(pub(crate) ModuleValue, pub(crate) usize);
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Hash, Copy, PartialEq, Eq)]
|
|
||||||
pub struct GlobalValue(pub(crate) ModuleValue, pub(crate) usize);
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct ModuleHolder {
|
|
||||||
pub(crate) value: ModuleValue,
|
|
||||||
pub(crate) data: ModuleData,
|
|
||||||
pub(crate) functions: Vec<FunctionHolder>,
|
|
||||||
pub(crate) types: Vec<TypeHolder>,
|
|
||||||
pub(crate) debug_information: Option<DebugInformation>,
|
|
||||||
pub(crate) constants: Vec<ConstantValueHolder>,
|
|
||||||
pub(crate) globals: Vec<GlobalValueHolder>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct ConstantValueHolder {
|
|
||||||
pub(crate) value: ConstantValue,
|
|
||||||
pub(crate) kind: ConstValueKind,
|
|
||||||
}
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct GlobalValueHolder {
|
|
||||||
pub(crate) value: GlobalValue,
|
|
||||||
pub(crate) name: String,
|
|
||||||
pub(crate) initializer: ConstantValue,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct TypeHolder {
|
|
||||||
pub(crate) value: TypeValue,
|
|
||||||
pub(crate) data: TypeData,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct FunctionHolder {
|
|
||||||
pub(crate) value: FunctionValue,
|
|
||||||
pub(crate) data: FunctionData,
|
|
||||||
pub(crate) blocks: Vec<BlockHolder>,
|
|
||||||
/// Debug scope value of this current function
|
|
||||||
pub(crate) debug_info: Option<DebugScopeValue>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct BlockHolder {
|
|
||||||
pub(crate) value: BlockValue,
|
|
||||||
pub(crate) data: BlockData,
|
|
||||||
pub(crate) instructions: Vec<InstructionHolder>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct InstructionHolder {
|
|
||||||
pub(crate) value: InstructionValue,
|
|
||||||
pub(crate) data: InstructionData,
|
|
||||||
pub(crate) name: String,
|
|
||||||
pub(crate) record: Option<InstructionDebugRecordData>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub(crate) struct Builder {
|
|
||||||
pub(crate) modules: Rc<RefCell<Vec<ModuleHolder>>>,
|
|
||||||
pub(crate) producer: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Builder {
|
|
||||||
pub fn new(producer: String) -> Builder {
|
|
||||||
Builder {
|
|
||||||
modules: Rc::new(RefCell::new(Vec::new())),
|
|
||||||
producer,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn add_module(&self, data: ModuleData) -> ModuleValue {
|
|
||||||
let value = ModuleValue(self.modules.borrow().len());
|
|
||||||
self.modules.borrow_mut().push(ModuleHolder {
|
|
||||||
value,
|
|
||||||
data,
|
|
||||||
functions: Vec::new(),
|
|
||||||
types: Vec::new(),
|
|
||||||
debug_information: None,
|
|
||||||
constants: Vec::new(),
|
|
||||||
globals: Vec::new(),
|
|
||||||
});
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn set_debug_information(&self, mod_val: &ModuleValue, debug_info: DebugInformation) {
|
|
||||||
unsafe {
|
|
||||||
let mut modules = self.modules.borrow_mut();
|
|
||||||
let module = modules.get_unchecked_mut(mod_val.0);
|
|
||||||
module.debug_information = Some(debug_info);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn add_type(&self, mod_val: &ModuleValue, data: TypeData) -> TypeValue {
|
|
||||||
unsafe {
|
|
||||||
let mut modules = self.modules.borrow_mut();
|
|
||||||
let module = modules.get_unchecked_mut(mod_val.0);
|
|
||||||
let value = TypeValue(module.value, module.types.len());
|
|
||||||
module.types.push(TypeHolder { value, data });
|
|
||||||
value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn add_function(&self, mod_val: &ModuleValue, data: FunctionData) -> FunctionValue {
|
|
||||||
unsafe {
|
|
||||||
let mut modules = self.modules.borrow_mut();
|
|
||||||
let module = modules.get_unchecked_mut(mod_val.0);
|
|
||||||
let value = FunctionValue(module.value, module.functions.len());
|
|
||||||
module.functions.push(FunctionHolder {
|
|
||||||
value,
|
|
||||||
data,
|
|
||||||
blocks: Vec::new(),
|
|
||||||
debug_info: None,
|
|
||||||
});
|
|
||||||
value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn add_block(&self, fun_val: &FunctionValue, data: BlockData) -> BlockValue {
|
|
||||||
unsafe {
|
|
||||||
let mut modules = self.modules.borrow_mut();
|
|
||||||
let module = modules.get_unchecked_mut(fun_val.0.0);
|
|
||||||
let function = module.functions.get_unchecked_mut(fun_val.1);
|
|
||||||
let value = BlockValue(function.value, function.blocks.len());
|
|
||||||
function.blocks.push(BlockHolder {
|
|
||||||
value,
|
|
||||||
data,
|
|
||||||
instructions: Vec::new(),
|
|
||||||
});
|
|
||||||
value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn add_instruction(
|
|
||||||
&self,
|
|
||||||
block_val: &BlockValue,
|
|
||||||
data: InstructionData,
|
|
||||||
name: String,
|
|
||||||
) -> CompileResult<InstructionValue> {
|
|
||||||
unsafe {
|
|
||||||
let mut modules = self.modules.borrow_mut();
|
|
||||||
let module = modules.get_unchecked_mut(block_val.0.0.0);
|
|
||||||
let function = module.functions.get_unchecked_mut(block_val.0.1);
|
|
||||||
let block = function.blocks.get_unchecked_mut(block_val.1);
|
|
||||||
let value = InstructionValue(block.value, block.instructions.len());
|
|
||||||
block.instructions.push(InstructionHolder {
|
|
||||||
value,
|
|
||||||
data,
|
|
||||||
name,
|
|
||||||
record: None,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Drop modules so that it is no longer mutable borrowed
|
|
||||||
// (check_instruction requires an immutable borrow).
|
|
||||||
drop(modules);
|
|
||||||
|
|
||||||
self.check_instruction(&value)?;
|
|
||||||
Ok(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn build_constant(&self, module: ModuleValue, kind: ConstValueKind) -> ConstantValue {
|
|
||||||
unsafe {
|
|
||||||
let mut modules = self.modules.borrow_mut();
|
|
||||||
let module = modules.get_unchecked_mut(module.0);
|
|
||||||
let value = ConstantValue(module.value, module.constants.len());
|
|
||||||
module.constants.push(ConstantValueHolder { value, kind });
|
|
||||||
value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn add_global(
|
|
||||||
&self,
|
|
||||||
module: ModuleValue,
|
|
||||||
name: String,
|
|
||||||
initializer: ConstantValue,
|
|
||||||
) -> GlobalValue {
|
|
||||||
unsafe {
|
|
||||||
let mut modules = self.modules.borrow_mut();
|
|
||||||
let module = modules.get_unchecked_mut(module.0);
|
|
||||||
let value = GlobalValue(module.value, module.globals.len());
|
|
||||||
module.globals.push(GlobalValueHolder {
|
|
||||||
value,
|
|
||||||
name,
|
|
||||||
initializer,
|
|
||||||
});
|
|
||||||
value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn find_function(&self, module: ModuleValue, name: &String) -> Option<FunctionValue> {
|
|
||||||
unsafe {
|
|
||||||
let mut modules = self.modules.borrow_mut();
|
|
||||||
let module = modules.get_unchecked_mut(module.0);
|
|
||||||
module.functions.iter().find(|f| f.data.name == *name).map(|f| f.value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn add_instruction_location(&self, value: &InstructionValue, location: DebugLocationValue) {
|
|
||||||
unsafe {
|
|
||||||
let mut modules = self.modules.borrow_mut();
|
|
||||||
let module = modules.get_unchecked_mut(value.0.0.0.0);
|
|
||||||
let function = module.functions.get_unchecked_mut(value.0.0.1);
|
|
||||||
let block = function.blocks.get_unchecked_mut(value.0.1);
|
|
||||||
let instr = block.instructions.get_unchecked_mut(value.1);
|
|
||||||
instr.data.location = Some(location)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn add_instruction_metadata(&self, value: &InstructionValue, metadata: DebugMetadataValue) {
|
|
||||||
unsafe {
|
|
||||||
let mut modules = self.modules.borrow_mut();
|
|
||||||
let module = modules.get_unchecked_mut(value.0.0.0.0);
|
|
||||||
let function = module.functions.get_unchecked_mut(value.0.0.1);
|
|
||||||
let block = function.blocks.get_unchecked_mut(value.0.1);
|
|
||||||
let instr = block.instructions.get_unchecked_mut(value.1);
|
|
||||||
instr.data.meta = Some(metadata)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn add_instruction_record(&self, value: &InstructionValue, record: InstructionDebugRecordData) {
|
|
||||||
unsafe {
|
|
||||||
let mut modules = self.modules.borrow_mut();
|
|
||||||
let module = modules.get_unchecked_mut(value.0.0.0.0);
|
|
||||||
let function = module.functions.get_unchecked_mut(value.0.0.1);
|
|
||||||
let block = function.blocks.get_unchecked_mut(value.0.1);
|
|
||||||
let instr = block.instructions.get_unchecked_mut(value.1);
|
|
||||||
instr.record = Some(record)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn set_debug_subprogram(&self, value: &FunctionValue, subprogram: DebugScopeValue) {
|
|
||||||
unsafe {
|
|
||||||
let mut modules = self.modules.borrow_mut();
|
|
||||||
let module = modules.get_unchecked_mut(value.0.0);
|
|
||||||
let function = module.functions.get_unchecked_mut(value.1);
|
|
||||||
function.debug_info = Some(subprogram)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn terminate(&self, block: &BlockValue, value: TerminatorKind) -> CompileResult<()> {
|
|
||||||
unsafe {
|
|
||||||
let mut modules = self.modules.borrow_mut();
|
|
||||||
let module = modules.get_unchecked_mut(block.0.0.0);
|
|
||||||
let function = module.functions.get_unchecked_mut(block.0.1);
|
|
||||||
let block = function.blocks.get_unchecked_mut(block.1);
|
|
||||||
if let Some(_) = &block.data.terminator {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
} else {
|
|
||||||
block.data.terminator = Some(value);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn set_terminator_location(
|
|
||||||
&self,
|
|
||||||
block: &BlockValue,
|
|
||||||
location: DebugLocationValue,
|
|
||||||
) -> CompileResult<()> {
|
|
||||||
unsafe {
|
|
||||||
let mut modules = self.modules.borrow_mut();
|
|
||||||
let module = modules.get_unchecked_mut(block.0.0.0);
|
|
||||||
let function = module.functions.get_unchecked_mut(block.0.1);
|
|
||||||
let block = function.blocks.get_unchecked_mut(block.1);
|
|
||||||
if let Some(_) = &block.data.terminator_location {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
} else {
|
|
||||||
block.data.terminator_location = Some(location);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn delete_block(&self, block: &BlockValue) -> CompileResult<()> {
|
|
||||||
unsafe {
|
|
||||||
let mut modules = self.modules.borrow_mut();
|
|
||||||
let module = modules.get_unchecked_mut(block.0.0.0);
|
|
||||||
let function = module.functions.get_unchecked_mut(block.0.1);
|
|
||||||
let block = function.blocks.get_unchecked_mut(block.1);
|
|
||||||
block.data.deleted = true;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub(crate) unsafe fn module_data(&self, value: &ModuleValue) -> ModuleData {
|
|
||||||
unsafe { self.modules.borrow().get_unchecked(value.0).data.clone() }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn function_data(&self, value: &FunctionValue) -> FunctionData {
|
|
||||||
unsafe {
|
|
||||||
self.modules
|
|
||||||
.borrow()
|
|
||||||
.get_unchecked(value.0.0)
|
|
||||||
.functions
|
|
||||||
.get_unchecked(value.1)
|
|
||||||
.data
|
|
||||||
.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub(crate) unsafe fn block_data(&self, value: &BlockValue) -> BlockData {
|
|
||||||
unsafe {
|
|
||||||
self.modules
|
|
||||||
.borrow()
|
|
||||||
.get_unchecked(value.0.0.0)
|
|
||||||
.functions
|
|
||||||
.get_unchecked(value.0.1)
|
|
||||||
.blocks
|
|
||||||
.get_unchecked(value.1)
|
|
||||||
.data
|
|
||||||
.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn instr_data(&self, value: &InstructionValue) -> InstructionData {
|
|
||||||
unsafe {
|
|
||||||
self.modules
|
|
||||||
.borrow()
|
|
||||||
.get_unchecked(value.0.0.0.0)
|
|
||||||
.functions
|
|
||||||
.get_unchecked(value.0.0.1)
|
|
||||||
.blocks
|
|
||||||
.get_unchecked(value.0.1)
|
|
||||||
.instructions
|
|
||||||
.get_unchecked(value.1)
|
|
||||||
.data
|
|
||||||
.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) unsafe fn type_data(&self, value: &TypeValue) -> TypeData {
|
|
||||||
unsafe {
|
|
||||||
self.modules
|
|
||||||
.borrow()
|
|
||||||
.get_unchecked(value.0.0)
|
|
||||||
.types
|
|
||||||
.get_unchecked(value.1)
|
|
||||||
.data
|
|
||||||
.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn find_module<'ctx>(&'ctx self, value: ModuleValue) -> ModuleHolder {
|
|
||||||
unsafe { self.modules.borrow().get_unchecked(value.0).clone() }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn get_modules(&self) -> Rc<RefCell<Vec<ModuleHolder>>> {
|
|
||||||
self.modules.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn get_global_initializer(&self, value: GlobalValue) -> ConstantValue {
|
|
||||||
unsafe {
|
|
||||||
let modules = self.modules.borrow();
|
|
||||||
let module = modules.get_unchecked(value.0.0);
|
|
||||||
let global = module.globals.get_unchecked(value.1);
|
|
||||||
global.initializer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn get_const_kind(&self, value: ConstantValue) -> ConstValueKind {
|
|
||||||
unsafe {
|
|
||||||
let modules = self.modules.borrow();
|
|
||||||
let module = modules.get_unchecked(value.0.0);
|
|
||||||
let constant = module.constants.get_unchecked(value.1);
|
|
||||||
constant.kind.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn check_instruction(&self, instruction: &InstructionValue) -> CompileResult<()> {
|
|
||||||
unsafe {
|
|
||||||
match self.instr_data(&instruction).kind {
|
|
||||||
Instr::Param(_) => Ok(()),
|
|
||||||
Instr::Constant(_) => Ok(()),
|
|
||||||
Instr::Add(lhs, rhs) => {
|
|
||||||
if match_types(&lhs, &rhs, &self)?.category().integer() {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::FAdd(lhs, rhs) => {
|
|
||||||
if match_types(&lhs, &rhs, &self)?.category() == TypeCategory::Real {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::Sub(lhs, rhs) => {
|
|
||||||
if match_types(&lhs, &rhs, &self)?.category().integer() {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::FSub(lhs, rhs) => {
|
|
||||||
if match_types(&lhs, &rhs, &self)?.category() == TypeCategory::Real {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::Mul(lhs, rhs) => {
|
|
||||||
if match_types(&lhs, &rhs, &self)?.category().integer() {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::FMul(lhs, rhs) => {
|
|
||||||
if match_types(&lhs, &rhs, &self)?.category() == TypeCategory::Real {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::UDiv(lhs, rhs) => {
|
|
||||||
if match_types(&lhs, &rhs, &self)?.category() == TypeCategory::UnsignedInteger {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::SDiv(lhs, rhs) => {
|
|
||||||
if match_types(&lhs, &rhs, &self)?.category() == TypeCategory::SignedInteger {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::FDiv(lhs, rhs) => {
|
|
||||||
if match_types(&lhs, &rhs, &self)?.category() == TypeCategory::Real {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::URem(lhs, rhs) => {
|
|
||||||
if match_types(&lhs, &rhs, &self)?.category() == TypeCategory::UnsignedInteger {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::SRem(lhs, rhs) => {
|
|
||||||
if match_types(&lhs, &rhs, &self)?.category() == TypeCategory::SignedInteger {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::FRem(lhs, rhs) => {
|
|
||||||
if match_types(&lhs, &rhs, &self)?.category() == TypeCategory::Real {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::And(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()),
|
|
||||||
Instr::Or(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()),
|
|
||||||
Instr::XOr(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()),
|
|
||||||
Instr::ICmp(_, lhs, rhs) => {
|
|
||||||
let t = match_types(&lhs, &rhs, self)?;
|
|
||||||
if t.category().comparable() || !t.category().integer() {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null) // TODO error: Types not comparable
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::FCmp(_, lhs, rhs) => {
|
|
||||||
let t = match_types(&lhs, &rhs, self)?;
|
|
||||||
if t.category().comparable() || t.category() != TypeCategory::Real {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null) // TODO error: Types not comparable
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::FunctionCall(fun, params) => {
|
|
||||||
let param_types = self.function_data(&fun).params;
|
|
||||||
if param_types.len() != params.len() {
|
|
||||||
return Err(ErrorKind::Null); // TODO error: invalid amount of params
|
|
||||||
}
|
|
||||||
for (a, b) in param_types.iter().zip(params) {
|
|
||||||
if *a != b.get_type(&self)? {
|
|
||||||
return Err(ErrorKind::Null); // TODO error: params do not match
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
Instr::Phi(vals) => {
|
|
||||||
let mut iter = vals.iter();
|
|
||||||
// TODO error: Phi must contain at least one item
|
|
||||||
|
|
||||||
// TODO error: compile can actually crash here if any of the
|
|
||||||
// incoming values come from blocks that are added later
|
|
||||||
// than the one where this one exists.
|
|
||||||
|
|
||||||
let first = iter.next().ok_or(ErrorKind::Null)?;
|
|
||||||
for item in iter {
|
|
||||||
match_types(first, item, &self)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
Instr::Alloca(_) => Ok(()),
|
|
||||||
Instr::Load(ptr, load_ty) => {
|
|
||||||
let ptr_ty = ptr.get_type(&self)?;
|
|
||||||
if let Type::Ptr(ptr_ty_inner) = ptr_ty {
|
|
||||||
if *ptr_ty_inner == load_ty {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null) // TODO error: inner type mismatch
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null) // TODO error: not a pointer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::Store(ptr, _) => {
|
|
||||||
let ty = ptr.get_type(&self)?;
|
|
||||||
if let Type::Ptr(_) = ty {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null) // TODO error: not a pointer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::ArrayAlloca(_, val) => {
|
|
||||||
if val.get_type(self)?.category() == TypeCategory::UnsignedInteger {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null) // TODO error: not a pointer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::GetElemPtr(ptr_val, _) => {
|
|
||||||
let ptr_ty = ptr_val.get_type(&self)?;
|
|
||||||
match ptr_ty {
|
|
||||||
Type::Ptr(_) => Ok(()),
|
|
||||||
_ => Err(ErrorKind::Null),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::GetStructElemPtr(ptr_val, idx) => {
|
|
||||||
let ptr_ty = ptr_val.get_type(&self)?;
|
|
||||||
if let Type::Ptr(ty) = ptr_ty {
|
|
||||||
if let Type::CustomType(val) = *ty {
|
|
||||||
match self.type_data(&val).kind {
|
|
||||||
CustomTypeKind::NamedStruct(NamedStruct(_, fields)) => {
|
|
||||||
if fields.len() <= idx as usize {
|
|
||||||
return Err(ErrorKind::Null); // TODO error: no such field
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null) // TODO error: not a struct
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null) // TODO error: not a pointer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::ExtractValue(val, _) => {
|
|
||||||
let val_ty = val.get_type(&self)?;
|
|
||||||
match val_ty {
|
|
||||||
Type::CustomType(custom) => match self.type_data(&custom).kind {
|
|
||||||
CustomTypeKind::NamedStruct(_) => Ok(()),
|
|
||||||
},
|
|
||||||
Type::Array(_, _) => Ok(()),
|
|
||||||
_ => Err(ErrorKind::Null),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Instr::Trunc(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
|
|
||||||
Instr::ZExt(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
|
|
||||||
Instr::SExt(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
|
|
||||||
Instr::FPTrunc(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
|
|
||||||
Instr::FPExt(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
|
|
||||||
Instr::FPToUI(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
|
|
||||||
Instr::FPToSI(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
|
|
||||||
Instr::UIToFP(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
|
|
||||||
Instr::SIToFP(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
|
|
||||||
Instr::PtrToInt(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
|
|
||||||
Instr::IntToPtr(instr, ty) => instr.cast_to(self, &ty).map(|_| ()),
|
|
||||||
Instr::BitCast(..) => Ok(()),
|
|
||||||
Instr::ShiftRightLogical(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()),
|
|
||||||
Instr::ShiftRightArithmetic(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()),
|
|
||||||
Instr::ShiftLeft(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()),
|
|
||||||
Instr::GetGlobal(_) => Ok(()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_block_used(&self, block_v: BlockValue) -> bool {
|
|
||||||
unsafe {
|
|
||||||
let modules = self.modules.borrow();
|
|
||||||
let module = modules.get_unchecked(block_v.0.0.0);
|
|
||||||
let function = module.functions.get_unchecked(block_v.0.1);
|
|
||||||
let block = function.blocks.get_unchecked(block_v.1);
|
|
||||||
|
|
||||||
if block.instructions.len() > 0 || block.data.terminator.is_some() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
for other in &function.blocks {
|
|
||||||
if let Some(term) = &other.data.terminator {
|
|
||||||
match term {
|
|
||||||
TerminatorKind::Ret(_) => {}
|
|
||||||
TerminatorKind::RetVoid => {}
|
|
||||||
TerminatorKind::Br(other_val) => {
|
|
||||||
if other_val == &block_v {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TerminatorKind::CondBr(_, then_other_v, else_other_v) => {
|
|
||||||
if then_other_v == &block_v || else_other_v == &block_v {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl InstructionValue {
|
|
||||||
pub fn with_location(self, block: &Block, location: DebugLocationValue) -> InstructionValue {
|
|
||||||
unsafe {
|
|
||||||
block.builder.add_instruction_location(&self, location);
|
|
||||||
}
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn maybe_location(self, block: &mut Block, location: Option<DebugLocationValue>) -> InstructionValue {
|
|
||||||
unsafe {
|
|
||||||
if let Some(location) = location {
|
|
||||||
block.builder.add_instruction_location(&self, location);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_record(&self, block: &mut Block, record: InstructionDebugRecordData) {
|
|
||||||
unsafe {
|
|
||||||
block.builder.add_instruction_record(self, record);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn get_type(&self, builder: &Builder) -> CompileResult<Type> {
|
|
||||||
use Instr::*;
|
|
||||||
unsafe {
|
|
||||||
match &builder.instr_data(self).kind {
|
|
||||||
Param(nth) => builder
|
|
||||||
.function_data(&self.0.0)
|
|
||||||
.params
|
|
||||||
.get(*nth)
|
|
||||||
.cloned()
|
|
||||||
.ok_or(ErrorKind::Null),
|
|
||||||
Constant(c) => Ok(c.get_type()),
|
|
||||||
Add(lhs, rhs) => match_types(lhs, rhs, &builder),
|
|
||||||
FAdd(lhs, rhs) => match_types(lhs, rhs, &builder),
|
|
||||||
Sub(lhs, rhs) => match_types(lhs, rhs, &builder),
|
|
||||||
FSub(lhs, rhs) => match_types(lhs, rhs, &builder),
|
|
||||||
Mul(lhs, rhs) => match_types(lhs, rhs, &builder),
|
|
||||||
FMul(lhs, rhs) => match_types(lhs, rhs, &builder),
|
|
||||||
UDiv(lhs, rhs) => match_types(lhs, rhs, &builder),
|
|
||||||
SDiv(lhs, rhs) => match_types(lhs, rhs, &builder),
|
|
||||||
FDiv(lhs, rhs) => match_types(lhs, rhs, &builder),
|
|
||||||
URem(lhs, rhs) => match_types(lhs, rhs, &builder),
|
|
||||||
SRem(lhs, rhs) => match_types(lhs, rhs, &builder),
|
|
||||||
FRem(lhs, rhs) => match_types(lhs, rhs, &builder),
|
|
||||||
And(lhs, rhs) => match_types(lhs, rhs, &builder),
|
|
||||||
Or(lhs, rhs) => match_types(lhs, rhs, &builder),
|
|
||||||
XOr(lhs, rhs) => match_types(lhs, rhs, &builder),
|
|
||||||
ICmp(_, _, _) => Ok(Type::Bool),
|
|
||||||
FCmp(_, _, _) => Ok(Type::Bool),
|
|
||||||
FunctionCall(function_value, _) => Ok(builder.function_data(function_value).ret),
|
|
||||||
Phi(values) => values.first().ok_or(ErrorKind::Null).and_then(|v| v.get_type(&builder)),
|
|
||||||
Alloca(ty) => Ok(Type::Ptr(Box::new(ty.clone()))),
|
|
||||||
Load(_, ty) => Ok(ty.clone()),
|
|
||||||
Store(_, value) => value.get_type(builder),
|
|
||||||
ArrayAlloca(ty, _) => Ok(Type::Ptr(Box::new(ty.clone()))),
|
|
||||||
GetElemPtr(instr, _) => {
|
|
||||||
let instr_ty = instr.get_type(builder)?;
|
|
||||||
let Type::Ptr(inner_ty) = &instr_ty else {
|
|
||||||
panic!("GetStructElemPtr on non-pointer! ({:?})", &instr_ty)
|
|
||||||
};
|
|
||||||
match *inner_ty.clone() {
|
|
||||||
Type::Array(elem_ty, _) => Ok(Type::Ptr(Box::new(*elem_ty.clone()))),
|
|
||||||
_ => Ok(instr_ty),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
GetStructElemPtr(instr, idx) => {
|
|
||||||
let instr_ty = instr.get_type(builder)?;
|
|
||||||
let Type::Ptr(inner_ty) = instr_ty else {
|
|
||||||
panic!("GetStructElemPtr on non-pointer! ({:?})", &instr_ty)
|
|
||||||
};
|
|
||||||
let Type::CustomType(ty_value) = *inner_ty else {
|
|
||||||
panic!("GetStructElemPtr on non-struct! ({:?})", &inner_ty)
|
|
||||||
};
|
|
||||||
let field_ty = match builder.type_data(&ty_value).kind {
|
|
||||||
CustomTypeKind::NamedStruct(NamedStruct(_, fields)) => {
|
|
||||||
fields.get_unchecked(*idx as usize).clone()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(Type::Ptr(Box::new(field_ty)))
|
|
||||||
}
|
|
||||||
ExtractValue(instr, idx) => {
|
|
||||||
let instr_ty = instr.get_type(builder)?;
|
|
||||||
Ok(match instr_ty {
|
|
||||||
Type::CustomType(struct_ty) => {
|
|
||||||
let data = builder.type_data(&struct_ty);
|
|
||||||
match data.kind {
|
|
||||||
CustomTypeKind::NamedStruct(named_struct) => {
|
|
||||||
named_struct.1.get(*idx as usize).unwrap().clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Type::Array(elem_ty, _) => *elem_ty.clone(),
|
|
||||||
_ => return Err(ErrorKind::Null),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Trunc(instr, ty) => instr.cast_to(builder, ty).map(|_| ty.clone()),
|
|
||||||
ZExt(instr, ty) => instr.cast_to(builder, ty).map(|_| ty.clone()),
|
|
||||||
SExt(instr, ty) => instr.cast_to(builder, ty).map(|_| ty.clone()),
|
|
||||||
FPTrunc(instr, ty) => instr.cast_to(builder, ty).map(|_| ty.clone()),
|
|
||||||
FPExt(instr, ty) => instr.cast_to(builder, ty).map(|_| ty.clone()),
|
|
||||||
FPToUI(instr, ty) => instr.cast_to(builder, ty).map(|_| ty.clone()),
|
|
||||||
FPToSI(instr, ty) => instr.cast_to(builder, ty).map(|_| ty.clone()),
|
|
||||||
UIToFP(instr, ty) => instr.cast_to(builder, ty).map(|_| ty.clone()),
|
|
||||||
SIToFP(instr, ty) => instr.cast_to(builder, ty).map(|_| ty.clone()),
|
|
||||||
PtrToInt(instr, ty) => instr.cast_to(builder, ty).map(|_| ty.clone()),
|
|
||||||
IntToPtr(instr, ty) => instr.cast_to(builder, ty).map(|_| ty.clone()),
|
|
||||||
BitCast(_, ty) => Ok(ty.clone()),
|
|
||||||
ShiftRightLogical(lhs, _) => lhs.get_type(builder),
|
|
||||||
ShiftRightArithmetic(lhs, _) => lhs.get_type(builder),
|
|
||||||
ShiftLeft(lhs, _) => lhs.get_type(builder),
|
|
||||||
GetGlobal(global_value) => {
|
|
||||||
let constant = builder.get_global_initializer(*global_value);
|
|
||||||
let kind = builder.get_const_kind(constant);
|
|
||||||
Ok(kind.get_type())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cast_to(&self, builder: &Builder, ty: &Type) -> CompileResult<Instr> {
|
|
||||||
self.get_type(builder)?
|
|
||||||
.cast_instruction(*self, &ty)
|
|
||||||
.ok_or(ErrorKind::Null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@ -1,399 +0,0 @@
|
|||||||
use std::{
|
|
||||||
cell::{Ref, RefCell, RefMut},
|
|
||||||
rc::Rc,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::builder::InstructionValue;
|
|
||||||
|
|
||||||
/// Represents 1. the compilation context, 2. subprogram or 3. a lexical scope
|
|
||||||
#[derive(Clone, Hash, PartialEq, Eq)]
|
|
||||||
pub struct DebugScopeValue(pub Vec<usize>);
|
|
||||||
|
|
||||||
#[derive(Clone, Hash, PartialEq, Eq)]
|
|
||||||
pub struct DebugLocationValue(pub DebugScopeValue, pub usize);
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
|
|
||||||
pub struct DebugTypeValue(pub usize);
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
|
|
||||||
pub struct DebugMetadataValue(pub usize);
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct DebugFileData {
|
|
||||||
pub name: String,
|
|
||||||
pub directory: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub(crate) struct DebugScopeHolder {
|
|
||||||
pub(crate) value: DebugScopeValue,
|
|
||||||
pub(crate) data: DebugScopeData,
|
|
||||||
pub(crate) inner_scopes: Vec<DebugScopeHolder>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct DebugMetadataHolder {
|
|
||||||
pub(crate) location: DebugLocation,
|
|
||||||
pub(crate) value: DebugMetadataValue,
|
|
||||||
pub(crate) data: DebugMetadata,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct DebugTypeHolder {
|
|
||||||
pub(crate) value: DebugTypeValue,
|
|
||||||
pub(crate) data: DebugTypeData,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub(crate) struct DebugLocationHolder {
|
|
||||||
pub(crate) scope: DebugScopeValue,
|
|
||||||
pub(crate) value: DebugLocationValue,
|
|
||||||
pub(crate) location: DebugLocation,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct DebugInformation {
|
|
||||||
pub file: DebugFileData,
|
|
||||||
scope: Rc<RefCell<DebugScopeHolder>>,
|
|
||||||
locations: Rc<RefCell<Vec<DebugLocationHolder>>>,
|
|
||||||
metadata: Rc<RefCell<Vec<DebugMetadataHolder>>>,
|
|
||||||
types: Rc<RefCell<Vec<DebugTypeHolder>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DebugInformation {
|
|
||||||
pub fn from_file(file: DebugFileData) -> (DebugInformation, DebugScopeValue) {
|
|
||||||
let scope_value = DebugScopeValue(Vec::new());
|
|
||||||
(
|
|
||||||
DebugInformation {
|
|
||||||
file,
|
|
||||||
scope: Rc::new(RefCell::new(DebugScopeHolder {
|
|
||||||
value: scope_value.clone(),
|
|
||||||
inner_scopes: Vec::new(),
|
|
||||||
data: DebugScopeData {
|
|
||||||
parent: None,
|
|
||||||
kind: DebugScopeKind::CodegenContext,
|
|
||||||
},
|
|
||||||
})),
|
|
||||||
locations: Rc::new(RefCell::new(Vec::new())),
|
|
||||||
metadata: Rc::new(RefCell::new(Vec::new())),
|
|
||||||
types: Rc::new(RefCell::new(Vec::new())),
|
|
||||||
},
|
|
||||||
scope_value,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn location(&self, scope_value: &DebugScopeValue, location: DebugLocation) -> DebugLocationValue {
|
|
||||||
let value = DebugLocationValue(scope_value.clone(), self.locations.borrow().len());
|
|
||||||
let location = DebugLocationHolder {
|
|
||||||
scope: scope_value.clone(),
|
|
||||||
value: value.clone(),
|
|
||||||
location,
|
|
||||||
};
|
|
||||||
self.locations.borrow_mut().push(location);
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn debug_type(&self, kind: DebugTypeData) -> DebugTypeValue {
|
|
||||||
let mut types = self.types.borrow_mut();
|
|
||||||
let value = DebugTypeValue(types.len());
|
|
||||||
types.push(DebugTypeHolder {
|
|
||||||
value: value.clone(),
|
|
||||||
data: kind,
|
|
||||||
});
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn metadata(&self, location: &DebugLocation, kind: DebugMetadata) -> DebugMetadataValue {
|
|
||||||
let mut metadata = self.metadata.borrow_mut();
|
|
||||||
let value = DebugMetadataValue(metadata.len());
|
|
||||||
metadata.push(DebugMetadataHolder {
|
|
||||||
location: location.clone(),
|
|
||||||
value: value.clone(),
|
|
||||||
data: kind,
|
|
||||||
});
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn subprogram(&self, parent: DebugScopeValue, kind: DebugSubprogramData) -> DebugScopeValue {
|
|
||||||
unsafe {
|
|
||||||
let mut outer_scope = RefMut::map(self.scope.borrow_mut(), |mut v| {
|
|
||||||
for i in &parent.0 {
|
|
||||||
v = v.inner_scopes.get_unchecked_mut(*i);
|
|
||||||
}
|
|
||||||
v
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut arr = parent.0.clone();
|
|
||||||
arr.push(outer_scope.inner_scopes.len());
|
|
||||||
let value = DebugScopeValue(arr);
|
|
||||||
|
|
||||||
outer_scope.inner_scopes.push(DebugScopeHolder {
|
|
||||||
value: value.clone(),
|
|
||||||
inner_scopes: Vec::new(),
|
|
||||||
data: DebugScopeData {
|
|
||||||
parent: Some(parent.clone()),
|
|
||||||
kind: DebugScopeKind::Subprogram(kind),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn lexical_scope(&self, parent: &DebugScopeValue, data: DebugLexicalScope) -> DebugScopeValue {
|
|
||||||
unsafe {
|
|
||||||
let mut outer_scope = RefMut::map(self.scope.borrow_mut(), |mut v| {
|
|
||||||
for i in &parent.0 {
|
|
||||||
v = v.inner_scopes.get_unchecked_mut(*i);
|
|
||||||
}
|
|
||||||
v
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut arr = parent.0.clone();
|
|
||||||
arr.push(outer_scope.inner_scopes.len());
|
|
||||||
let value = DebugScopeValue(arr);
|
|
||||||
|
|
||||||
outer_scope.inner_scopes.push(DebugScopeHolder {
|
|
||||||
value: value.clone(),
|
|
||||||
inner_scopes: Vec::new(),
|
|
||||||
data: DebugScopeData {
|
|
||||||
parent: Some(parent.clone()),
|
|
||||||
kind: DebugScopeKind::LexicalScope(data),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_metadata(&self, value: DebugMetadataValue) -> DebugMetadata {
|
|
||||||
unsafe { self.metadata.borrow().get_unchecked(value.0).data.clone() }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_metadata_location(&self, value: DebugMetadataValue) -> DebugLocation {
|
|
||||||
unsafe { self.metadata.borrow().get_unchecked(value.0).location.clone() }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_scope_data(&self, value: &DebugScopeValue) -> Option<DebugScopeData> {
|
|
||||||
let scope = Ref::filter_map(self.scope.borrow(), |v: &DebugScopeHolder| {
|
|
||||||
let mut opt = Some(v);
|
|
||||||
for i in &value.0 {
|
|
||||||
if let Some(inner) = opt {
|
|
||||||
opt = inner.inner_scopes.get(*i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
opt
|
|
||||||
});
|
|
||||||
|
|
||||||
if let Ok(scope) = scope {
|
|
||||||
Some(scope.data.clone())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_type_data(&self, value: DebugTypeValue) -> DebugTypeData {
|
|
||||||
unsafe { self.types.borrow().get_unchecked(value.0).data.clone() }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_location(&self, value: &DebugLocationValue) -> DebugLocation {
|
|
||||||
unsafe { self.locations.borrow().get_unchecked(value.1).location.clone() }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_metadatas(&self) -> Rc<RefCell<Vec<DebugMetadataHolder>>> {
|
|
||||||
self.metadata.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn get_scope(&self) -> Rc<RefCell<DebugScopeHolder>> {
|
|
||||||
self.scope.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_types(&self) -> Rc<RefCell<Vec<DebugTypeHolder>>> {
|
|
||||||
self.types.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn get_locations(&self) -> Rc<RefCell<Vec<DebugLocationHolder>>> {
|
|
||||||
self.locations.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct DebugLocation {
|
|
||||||
pub scope: DebugScopeValue,
|
|
||||||
pub pos: DebugPosition,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
|
||||||
pub struct DebugPosition {
|
|
||||||
pub line: u32,
|
|
||||||
pub column: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum DebugMetadata {
|
|
||||||
ParamVar(DebugParamVariable),
|
|
||||||
LocalVar(DebugLocalVariable),
|
|
||||||
VarAssignment,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct DebugParamVariable {
|
|
||||||
pub name: String,
|
|
||||||
/// the index (starting from 1) of this variable in the subprogram
|
|
||||||
/// parameters. arg_idx should not conflict with other parameters of the
|
|
||||||
/// same subprogram.
|
|
||||||
pub arg_idx: u32,
|
|
||||||
pub ty: DebugTypeValue,
|
|
||||||
/// If this variable will be referenced from its containing subprogram, and
|
|
||||||
/// will survive some optimizations.
|
|
||||||
pub always_preserve: bool,
|
|
||||||
pub flags: DwarfFlags,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct DebugLocalVariable {
|
|
||||||
pub name: String,
|
|
||||||
pub ty: DebugTypeValue,
|
|
||||||
pub always_preserve: bool,
|
|
||||||
pub flags: DwarfFlags,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for DebugSubprogramOptionals {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
scope_line: 0,
|
|
||||||
is_local: false,
|
|
||||||
is_definition: true,
|
|
||||||
is_optimized: false,
|
|
||||||
flags: DwarfFlags,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct DwarfFlags;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum DebugTypeData {
|
|
||||||
Basic(DebugBasicType),
|
|
||||||
Subprogram(DebugSubprogramType),
|
|
||||||
Pointer(DebugPointerType),
|
|
||||||
Array(DebugArrayType),
|
|
||||||
Struct(DebugStructType),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct DebugBasicType {
|
|
||||||
pub name: String,
|
|
||||||
/// Size of the type.
|
|
||||||
pub size_bits: u64,
|
|
||||||
/// DWARF encoding code, e.g., dwarf::DW_ATE_float.
|
|
||||||
pub encoding: DwarfEncoding,
|
|
||||||
/// Optional DWARF attributes, e.g., DW_AT_endianity.
|
|
||||||
pub flags: DwarfFlags,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct DebugArrayType {
|
|
||||||
pub size_bits: u64,
|
|
||||||
pub align_bits: u32,
|
|
||||||
pub element_type: DebugTypeValue,
|
|
||||||
pub length: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct DebugPointerType {
|
|
||||||
pub name: String,
|
|
||||||
pub pointee: DebugTypeValue,
|
|
||||||
pub size_bits: u64,
|
|
||||||
}
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct DebugStructType {
|
|
||||||
pub name: String,
|
|
||||||
pub scope: DebugScopeValue,
|
|
||||||
pub pos: Option<DebugPosition>,
|
|
||||||
pub size_bits: u64,
|
|
||||||
pub flags: DwarfFlags,
|
|
||||||
pub fields: Vec<DebugFieldType>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct DebugFieldType {
|
|
||||||
pub name: String,
|
|
||||||
pub scope: DebugScopeValue,
|
|
||||||
pub pos: Option<DebugPosition>,
|
|
||||||
pub size_bits: u64,
|
|
||||||
pub offset: u64,
|
|
||||||
pub flags: DwarfFlags,
|
|
||||||
pub ty: DebugTypeValue,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct DebugSubprogramType {
|
|
||||||
pub parameters: Vec<DebugTypeValue>,
|
|
||||||
pub flags: DwarfFlags,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
pub enum DwarfEncoding {
|
|
||||||
Address = 1,
|
|
||||||
Boolean = 2,
|
|
||||||
Float = 4,
|
|
||||||
Signed = 5,
|
|
||||||
SignedChar = 6,
|
|
||||||
Unsigned = 7,
|
|
||||||
UnsignedChar = 8,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct DebugScopeData {
|
|
||||||
pub parent: Option<DebugScopeValue>,
|
|
||||||
pub kind: DebugScopeKind,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum DebugScopeKind {
|
|
||||||
CodegenContext,
|
|
||||||
LexicalScope(DebugLexicalScope),
|
|
||||||
Subprogram(DebugSubprogramData),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct DebugLexicalScope {
|
|
||||||
pub location: DebugLocation,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct DebugSubprogramData {
|
|
||||||
/// Function name.
|
|
||||||
pub name: String,
|
|
||||||
pub outer_scope: DebugScopeValue,
|
|
||||||
/// Used for line number.
|
|
||||||
pub location: DebugLocation,
|
|
||||||
/// Function type.
|
|
||||||
pub ty: DebugTypeValue,
|
|
||||||
pub opts: DebugSubprogramOptionals,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct DebugSubprogramOptionals {
|
|
||||||
/// Set to the beginning of the scope this starts
|
|
||||||
pub scope_line: u32,
|
|
||||||
pub is_local: bool,
|
|
||||||
pub is_definition: bool,
|
|
||||||
pub is_optimized: bool,
|
|
||||||
/// These flags are used to emit dwarf attributes. e.g. is this function
|
|
||||||
/// prototyped or not.
|
|
||||||
pub flags: DwarfFlags,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct InstructionDebugRecordData {
|
|
||||||
pub scope: DebugScopeValue,
|
|
||||||
pub variable: DebugMetadataValue,
|
|
||||||
pub location: DebugLocation,
|
|
||||||
pub kind: DebugRecordKind,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
|
||||||
pub enum DebugRecordKind {
|
|
||||||
Declare(InstructionValue),
|
|
||||||
Value(InstructionValue),
|
|
||||||
}
|
|
||||||
@ -1,594 +0,0 @@
|
|||||||
//! Debug implementations for relevant types
|
|
||||||
|
|
||||||
use std::{
|
|
||||||
fmt::{Debug, Display, Write},
|
|
||||||
marker::PhantomData,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
CmpPredicate, Context, Instr, InstructionData, TerminatorKind,
|
|
||||||
builder::*,
|
|
||||||
debug_information::{
|
|
||||||
DebugArrayType, DebugBasicType, DebugFieldType, DebugInformation, DebugLocalVariable, DebugLocation,
|
|
||||||
DebugLocationValue, DebugMetadata, DebugMetadataValue, DebugParamVariable, DebugPointerType, DebugPosition,
|
|
||||||
DebugRecordKind, DebugScopeValue, DebugStructType, DebugSubprogramType, DebugTypeData, DebugTypeHolder,
|
|
||||||
DebugTypeValue,
|
|
||||||
},
|
|
||||||
pad_adapter::PadAdapter,
|
|
||||||
};
|
|
||||||
|
|
||||||
impl Display for Context {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
Display::fmt(&self.builder, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Builder {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
writeln!(f, "Producer: {}", self.producer)?;
|
|
||||||
for module in self.modules.borrow().iter() {
|
|
||||||
if module.data.is_main {
|
|
||||||
write!(f, "main ")?;
|
|
||||||
}
|
|
||||||
writeln!(f, "{} ({:?}) {{", module.data.name, module.value)?;
|
|
||||||
for function in &module.functions {
|
|
||||||
let mut state = Default::default();
|
|
||||||
let mut inner = PadAdapter::wrap(f, &mut state);
|
|
||||||
function.builder_fmt(&mut inner, self, &module.debug_information)?;
|
|
||||||
}
|
|
||||||
writeln!(f, "}}")?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FunctionHolder {
|
|
||||||
fn builder_fmt(
|
|
||||||
&self,
|
|
||||||
f: &mut impl std::fmt::Write,
|
|
||||||
builder: &Builder,
|
|
||||||
debug: &Option<DebugInformation>,
|
|
||||||
) -> std::fmt::Result {
|
|
||||||
if self.data.flags.is_imported {
|
|
||||||
write!(f, "imported ")?;
|
|
||||||
}
|
|
||||||
if self.data.flags.is_extern {
|
|
||||||
write!(f, "extern ")?;
|
|
||||||
}
|
|
||||||
if self.data.flags.is_pub {
|
|
||||||
write!(f, "pub ")?;
|
|
||||||
}
|
|
||||||
if self.data.flags.is_main {
|
|
||||||
write!(f, "main ")?;
|
|
||||||
}
|
|
||||||
let params = self
|
|
||||||
.data
|
|
||||||
.params
|
|
||||||
.iter()
|
|
||||||
.map(|p| format!("{:?}", p))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(", ");
|
|
||||||
write!(f, "fn {}({}) -> {:?} ", self.data.name, params, self.data.ret)?;
|
|
||||||
|
|
||||||
writeln!(f, "{{")?;
|
|
||||||
let mut state = Default::default();
|
|
||||||
let mut inner = PadAdapter::wrap(f, &mut state);
|
|
||||||
writeln!(inner, "(Value = {:?}) ", self.value)?;
|
|
||||||
if let Some(debug) = &self.debug_info {
|
|
||||||
writeln!(inner, "(Debug = {:?})", debug)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
for block in &self.blocks {
|
|
||||||
let mut state = Default::default();
|
|
||||||
let mut inner = PadAdapter::wrap(&mut inner, &mut state);
|
|
||||||
block.builder_fmt(&mut inner, builder, debug)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
writeln!(f, "}}")?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BlockHolder {
|
|
||||||
fn builder_fmt(
|
|
||||||
&self,
|
|
||||||
f: &mut impl std::fmt::Write,
|
|
||||||
builder: &Builder,
|
|
||||||
debug: &Option<DebugInformation>,
|
|
||||||
) -> std::fmt::Result {
|
|
||||||
if self.data.deleted {
|
|
||||||
write!(f, "deleted ")?;
|
|
||||||
}
|
|
||||||
writeln!(f, "{} ({:?}):", self.data.name, self.value)?;
|
|
||||||
|
|
||||||
let mut state = Default::default();
|
|
||||||
let mut inner = PadAdapter::wrap(f, &mut state);
|
|
||||||
|
|
||||||
for instr in &self.instructions {
|
|
||||||
instr.builder_fmt(&mut inner, builder, debug)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(terminator) = &self.data.terminator {
|
|
||||||
terminator.builder_fmt(&mut inner, builder, debug)?;
|
|
||||||
}
|
|
||||||
if let Some(location) = &self.data.terminator_location {
|
|
||||||
writeln!(inner, " ^ (At {}) ", debug.as_ref().unwrap().get_location(location))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl InstructionHolder {
|
|
||||||
fn builder_fmt(
|
|
||||||
&self,
|
|
||||||
f: &mut impl std::fmt::Write,
|
|
||||||
_builder: &Builder,
|
|
||||||
debug: &Option<DebugInformation>,
|
|
||||||
) -> std::fmt::Result {
|
|
||||||
if let Some(record) = &self.record {
|
|
||||||
let kind = match record.kind {
|
|
||||||
DebugRecordKind::Declare(instruction_value) => {
|
|
||||||
format!("= {:?} (Assign)", instruction_value)
|
|
||||||
}
|
|
||||||
DebugRecordKind::Value(instruction_value) => {
|
|
||||||
format!("= {:?} (Value)", instruction_value)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(debug) = debug {
|
|
||||||
writeln!(f, " (Debug {} {})", record.variable.hr(debug), kind)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
writeln!(f, "{:?} ({}) = {:?} ", self.value, self.name, self.data.kind)?;
|
|
||||||
if let Some(debug) = debug {
|
|
||||||
if let Some(location) = &self.data.location {
|
|
||||||
writeln!(f, " ^ (At {}) ", debug.get_location(location))?;
|
|
||||||
}
|
|
||||||
if let Some(meta) = self.data.meta {
|
|
||||||
writeln!(f, " ^ (Meta {}) ", meta.hr(debug))?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
writeln!(f)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TerminatorKind {
|
|
||||||
fn builder_fmt(
|
|
||||||
&self,
|
|
||||||
f: &mut impl std::fmt::Write,
|
|
||||||
_builder: &Builder,
|
|
||||||
_debug: &Option<DebugInformation>,
|
|
||||||
) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
TerminatorKind::Ret(instr) => writeln!(f, "ret {:?}", instr),
|
|
||||||
TerminatorKind::RetVoid => writeln!(f, "ret void"),
|
|
||||||
TerminatorKind::Br(block) => writeln!(f, "br {:?}", block),
|
|
||||||
TerminatorKind::CondBr(instr, lhs, rhs) => {
|
|
||||||
writeln!(f, "condbr {:?}, {:?} or {:?}", instr, lhs, rhs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DebugMetadataValue {
|
|
||||||
fn hr(&self, debug: &DebugInformation) -> String {
|
|
||||||
let kind = match debug.get_metadata(*self) {
|
|
||||||
DebugMetadata::ParamVar(DebugParamVariable { name, arg_idx, ty, .. }) => {
|
|
||||||
format!("param {} (idx {}) (type {:?}) ", name, arg_idx, ty)
|
|
||||||
}
|
|
||||||
DebugMetadata::LocalVar(DebugLocalVariable { name, ty, .. }) => {
|
|
||||||
format!("var {} (type {:?}) ", name, ty)
|
|
||||||
}
|
|
||||||
DebugMetadata::VarAssignment => todo!(),
|
|
||||||
};
|
|
||||||
format!("{} at {}", kind, debug.get_metadata_location(*self))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for DebugLocation {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "{:?} on scope {:?}", self.pos, self.scope)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for DebugPosition {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "line {}, col {}", self.line, self.column)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for Builder {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_list().entries(self.get_modules().borrow().iter());
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct PrintableModule<'ctx> {
|
|
||||||
pub phantom: PhantomData<&'ctx ()>,
|
|
||||||
pub module: ModuleHolder,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'ctx> Debug for PrintableModule<'ctx> {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
Debug::fmt(&self.module, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for ModuleHolder {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_tuple(&format!("{}({:#?}) ", self.data.name, self.value))
|
|
||||||
.field(&self.functions)
|
|
||||||
// .field(&self.debug_information)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for FunctionHolder {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_tuple(&format!(
|
|
||||||
"{}({:?}) -> {:?} ",
|
|
||||||
self.data.name, self.data.params, self.data.ret
|
|
||||||
))
|
|
||||||
.field(&self.blocks)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for BlockHolder {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
let deleted = if self.data.deleted { " (deleted)" } else { "" };
|
|
||||||
f.debug_tuple(&format!("{}[{:?}]{} ", &self.data.name, &self.value, deleted))
|
|
||||||
.field(&self.instructions)
|
|
||||||
.field(&self.data.terminator)
|
|
||||||
.field(&self.data.terminator_location)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for InstructionHolder {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
self.value.fmt(f)?;
|
|
||||||
write!(f, " ({})", self.name)?;
|
|
||||||
f.write_str(" = ")?;
|
|
||||||
self.data.fmt(f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for InstructionData {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
self.kind.fmt(f)?;
|
|
||||||
if let Some(location) = &self.location {
|
|
||||||
write!(f, " ({:?})", location)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for ModuleValue {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "M[{:0>2}]", self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for FunctionValue {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "F[{:0>2}-{:0>2}]", &self.0.0, self.1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for BlockValue {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "B[{:0>2}-{:0>2}-{:0>2}]", &self.0.0.0, &self.0.1, self.1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for InstructionValue {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "%{}.{}.{}.{}", self.0.0.0.0, self.0.0.1, self.0.1, self.1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// impl Debug for InstructionValue {
|
|
||||||
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
// write!(
|
|
||||||
// f,
|
|
||||||
// "I[{:0>2}-{:0>2}-{:0>2}-{:0>2}]",
|
|
||||||
// &self.0.0.0.0, &self.0.0.1, &self.0.1, self.1
|
|
||||||
// )
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
impl Debug for TypeValue {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "Ty[{:0>2}-{:0>2}]", &self.0.0, self.1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for Instr {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
Instr::Param(nth) => fmt_call(f, &"Param", &nth),
|
|
||||||
Instr::Constant(c) => c.fmt(f),
|
|
||||||
Instr::Add(lhs, rhs) => fmt_binop(f, lhs, &"+", rhs),
|
|
||||||
Instr::FAdd(lhs, rhs) => fmt_binop(f, lhs, &"+", rhs),
|
|
||||||
Instr::Sub(lhs, rhs) => fmt_binop(f, lhs, &"-", rhs),
|
|
||||||
Instr::FSub(lhs, rhs) => fmt_binop(f, lhs, &"-", rhs),
|
|
||||||
Instr::Mul(lhs, rhs) => fmt_binop(f, lhs, &"*", rhs),
|
|
||||||
Instr::FMul(lhs, rhs) => fmt_binop(f, lhs, &"*", rhs),
|
|
||||||
Instr::UDiv(lhs, rhs) => fmt_binop(f, lhs, &"/", rhs),
|
|
||||||
Instr::SDiv(lhs, rhs) => fmt_binop(f, lhs, &"/", rhs),
|
|
||||||
Instr::FDiv(lhs, rhs) => fmt_binop(f, lhs, &"/", rhs),
|
|
||||||
Instr::URem(lhs, rhs) => fmt_binop(f, lhs, &"%", rhs),
|
|
||||||
Instr::SRem(lhs, rhs) => fmt_binop(f, lhs, &"%", rhs),
|
|
||||||
Instr::FRem(lhs, rhs) => fmt_binop(f, lhs, &"%", rhs),
|
|
||||||
Instr::And(lhs, rhs) => fmt_binop(f, lhs, &"&&", rhs),
|
|
||||||
Instr::Phi(val) => fmt_call(f, &"Phi", &val),
|
|
||||||
Instr::ICmp(cmp, lhs, rhs) => fmt_binop(f, lhs, cmp, rhs),
|
|
||||||
Instr::FCmp(cmp, lhs, rhs) => fmt_binop(f, lhs, cmp, rhs),
|
|
||||||
Instr::FunctionCall(fun, params) => fmt_call(f, fun, params),
|
|
||||||
Instr::Alloca(ty) => write!(f, "alloca<{:?}>", ty),
|
|
||||||
Instr::Load(val, ty) => write!(f, "load<{:?}>({:?})", ty, val),
|
|
||||||
Instr::Store(ptr, val) => write!(f, "store({:?} = {:?})", ptr, val),
|
|
||||||
Instr::ArrayAlloca(ty, instruction_value) => {
|
|
||||||
write!(f, "array_alloca<{:?}>({:?})", ty, instruction_value)
|
|
||||||
}
|
|
||||||
Instr::GetElemPtr(instruction_value, items) => fmt_index(
|
|
||||||
f,
|
|
||||||
instruction_value,
|
|
||||||
&items
|
|
||||||
.iter()
|
|
||||||
.map(|expr| format!("{:?}", expr))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(", "),
|
|
||||||
),
|
|
||||||
Instr::GetStructElemPtr(instruction_value, index) => {
|
|
||||||
write!(f, "GEP(")?;
|
|
||||||
fmt_index(f, instruction_value, &index.to_string())?;
|
|
||||||
write!(f, ")")
|
|
||||||
}
|
|
||||||
Instr::ExtractValue(instruction_value, index) => fmt_index(f, instruction_value, &index.to_string()),
|
|
||||||
Instr::Trunc(instr_val, ty) => {
|
|
||||||
write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name())
|
|
||||||
}
|
|
||||||
Instr::ZExt(instr_val, ty) => {
|
|
||||||
write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name())
|
|
||||||
}
|
|
||||||
Instr::SExt(instr_val, ty) => {
|
|
||||||
write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name())
|
|
||||||
}
|
|
||||||
Instr::FPTrunc(instr_val, ty) => {
|
|
||||||
write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name())
|
|
||||||
}
|
|
||||||
Instr::FPExt(instr_val, ty) => {
|
|
||||||
write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name())
|
|
||||||
}
|
|
||||||
Instr::FPToUI(instr_val, ty) => {
|
|
||||||
write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name())
|
|
||||||
}
|
|
||||||
Instr::FPToSI(instr_val, ty) => {
|
|
||||||
write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name())
|
|
||||||
}
|
|
||||||
Instr::UIToFP(instr_val, ty) => {
|
|
||||||
write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name())
|
|
||||||
}
|
|
||||||
Instr::SIToFP(instr_val, ty) => {
|
|
||||||
write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name())
|
|
||||||
}
|
|
||||||
Instr::PtrToInt(instr_val, ty) => {
|
|
||||||
write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name())
|
|
||||||
}
|
|
||||||
Instr::IntToPtr(instr_val, ty) => {
|
|
||||||
write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name())
|
|
||||||
}
|
|
||||||
Instr::BitCast(instr_val, ty) => {
|
|
||||||
write!(f, "{:?} to {:?} ({})", instr_val, ty, self.default_name())
|
|
||||||
}
|
|
||||||
Instr::Or(lhs, rhs) => fmt_binop(f, lhs, &"||", rhs),
|
|
||||||
Instr::XOr(lhs, rhs) => fmt_binop(f, lhs, &"^", rhs),
|
|
||||||
Instr::ShiftRightLogical(lhs, rhs) => fmt_binop(f, lhs, &">>l", rhs),
|
|
||||||
Instr::ShiftRightArithmetic(lhs, rhs) => fmt_binop(f, lhs, &">>a", rhs),
|
|
||||||
Instr::ShiftLeft(lhs, rhs) => fmt_binop(f, lhs, &"<<", rhs),
|
|
||||||
Instr::GetGlobal(global_value) => write!(f, "global {:?}", global_value),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fmt_binop(
|
|
||||||
f: &mut std::fmt::Formatter<'_>,
|
|
||||||
lhs: &impl std::fmt::Debug,
|
|
||||||
op: &impl std::fmt::Debug,
|
|
||||||
rhs: &impl std::fmt::Debug,
|
|
||||||
) -> std::fmt::Result {
|
|
||||||
lhs.fmt(f)?;
|
|
||||||
f.write_char(' ')?;
|
|
||||||
op.fmt(f)?;
|
|
||||||
f.write_char(' ')?;
|
|
||||||
rhs.fmt(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fmt_call(
|
|
||||||
f: &mut std::fmt::Formatter<'_>,
|
|
||||||
fun: &impl std::fmt::Debug,
|
|
||||||
params: &impl std::fmt::Debug,
|
|
||||||
) -> std::fmt::Result {
|
|
||||||
fun.fmt(f)?;
|
|
||||||
f.write_char('(')?;
|
|
||||||
params.fmt(f)?;
|
|
||||||
f.write_char(')')
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fmt_index(
|
|
||||||
f: &mut std::fmt::Formatter<'_>,
|
|
||||||
fun: &impl std::fmt::Debug,
|
|
||||||
params: &impl std::fmt::Debug,
|
|
||||||
) -> std::fmt::Result {
|
|
||||||
fun.fmt(f)?;
|
|
||||||
f.write_char('[')?;
|
|
||||||
params.fmt(f)?;
|
|
||||||
f.write_char(']')
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for CmpPredicate {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
Self::LT => write!(f, "<"),
|
|
||||||
Self::GT => write!(f, ">"),
|
|
||||||
Self::LE => write!(f, "<="),
|
|
||||||
Self::GE => write!(f, ">="),
|
|
||||||
Self::EQ => write!(f, "=="),
|
|
||||||
Self::NE => write!(f, "!="),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for TerminatorKind {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
Self::Ret(val) => {
|
|
||||||
write!(f, "Ret ")?;
|
|
||||||
val.fmt(f)
|
|
||||||
}
|
|
||||||
Self::RetVoid => write!(f, "Void Ret"),
|
|
||||||
Self::Br(val) => {
|
|
||||||
write!(f, "Br ")?;
|
|
||||||
val.fmt(f)
|
|
||||||
}
|
|
||||||
Self::CondBr(cond, b1, b2) => {
|
|
||||||
write!(f, "CondBr ")?;
|
|
||||||
cond.fmt(f)?;
|
|
||||||
write!(f, " ? ")?;
|
|
||||||
b1.fmt(f)?;
|
|
||||||
write!(f, " : ")?;
|
|
||||||
b2.fmt(f)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for DebugTypeHolder {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_tuple(&format!("DebugTypeHolder {:?}", self.value))
|
|
||||||
.field(&self.data)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for DebugTypeData {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
DebugTypeData::Basic(ty) => Debug::fmt(ty, f),
|
|
||||||
DebugTypeData::Subprogram(ty) => Debug::fmt(ty, f),
|
|
||||||
DebugTypeData::Pointer(ty) => Debug::fmt(ty, f),
|
|
||||||
DebugTypeData::Array(ty) => Debug::fmt(ty, f),
|
|
||||||
DebugTypeData::Struct(ty) => Debug::fmt(ty, f),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for DebugBasicType {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_tuple("BasicType")
|
|
||||||
.field(&self.name)
|
|
||||||
.field(&self.size_bits)
|
|
||||||
.field(&self.encoding)
|
|
||||||
.field(&self.flags)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for DebugStructType {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_struct("Struct")
|
|
||||||
.field("name", &self.name)
|
|
||||||
.field("scope", &self.scope)
|
|
||||||
.field("pos", &self.pos)
|
|
||||||
.field("size_bit", &self.size_bits)
|
|
||||||
.field("flags", &self.flags)
|
|
||||||
.field("elements", &self.fields)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for DebugFieldType {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_struct(&format!("Field({})", self.name))
|
|
||||||
.field("scope", &self.scope)
|
|
||||||
.field("pos", &self.pos)
|
|
||||||
.field("size_bits", &self.size_bits)
|
|
||||||
.field("offset", &self.offset)
|
|
||||||
.field("flags", &self.flags)
|
|
||||||
.field("ty", &self.ty)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for DebugSubprogramType {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_tuple("Subprogram")
|
|
||||||
.field(&self.parameters)
|
|
||||||
.field(&self.flags)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for DebugPointerType {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_tuple(&format!("Pointer<{:?}>({})", self.pointee, self.name))
|
|
||||||
.field(&self.size_bits)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for DebugArrayType {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_struct(&format!("Array<{:?}>[{}]", self.element_type, self.length))
|
|
||||||
.field("size_bits", &self.size_bits)
|
|
||||||
.field("align_bits", &self.align_bits)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for DebugMetadataValue {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "Meta[{}]", self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for DebugScopeValue {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"Scope[{}]",
|
|
||||||
self.0.iter().map(|v| v.to_string()).collect::<Vec<_>>().join(", ")
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for DebugTypeValue {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "Type[{}]", self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for DebugLocationValue {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "Value[{:?}][{}]", self.0, self.1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for DebugLocation {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "{:?} on scope {:?}", self.pos, self.scope)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for DebugPosition {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "ln {}, col {}", self.line, self.column)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,688 +0,0 @@
|
|||||||
//! Reid LLVM Lib is an ergonomic Rust'y API which is used to produce a
|
|
||||||
//! Low-Level IR (LLIR) using [`Context`] and [`Builder`]. This Builder can then
|
|
||||||
//! be used at the end to compile said LLIR into LLVM IR.
|
|
||||||
|
|
||||||
use std::{fmt::Debug, marker::PhantomData};
|
|
||||||
|
|
||||||
use builder::{BlockValue, Builder, FunctionValue, InstructionValue, ModuleValue, TypeValue};
|
|
||||||
use debug_information::{DebugFileData, DebugInformation, DebugLocationValue, DebugMetadataValue};
|
|
||||||
use fmt::PrintableModule;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
builder::{ConstantValue, GlobalValue},
|
|
||||||
debug_information::DebugScopeValue,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub mod builder;
|
|
||||||
pub mod compile;
|
|
||||||
pub mod debug_information;
|
|
||||||
mod fmt;
|
|
||||||
mod pad_adapter;
|
|
||||||
mod util;
|
|
||||||
|
|
||||||
#[derive(thiserror::Error, Debug, Clone, PartialEq, PartialOrd)]
|
|
||||||
pub enum ErrorKind {
|
|
||||||
#[error("NULL error, should never occur!")]
|
|
||||||
Null,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type CompileResult<T> = Result<T, ErrorKind>;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Context {
|
|
||||||
builder: Builder,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Context {
|
|
||||||
pub fn new<T: Into<String>>(producer: T) -> Context {
|
|
||||||
Context {
|
|
||||||
builder: Builder::new(producer.into()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn module<'ctx>(&'ctx self, name: &str, main: bool) -> Module<'ctx> {
|
|
||||||
let value = self.builder.add_module(ModuleData {
|
|
||||||
name: name.to_owned(),
|
|
||||||
is_main: main,
|
|
||||||
});
|
|
||||||
Module {
|
|
||||||
phantom: PhantomData,
|
|
||||||
builder: self.builder.clone(),
|
|
||||||
value,
|
|
||||||
debug_info: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Hash)]
|
|
||||||
pub struct ModuleData {
|
|
||||||
name: String,
|
|
||||||
is_main: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Module<'ctx> {
|
|
||||||
phantom: PhantomData<&'ctx ()>,
|
|
||||||
builder: Builder,
|
|
||||||
value: ModuleValue,
|
|
||||||
debug_info: Option<DebugInformation>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'ctx> Module<'ctx> {
|
|
||||||
pub fn function(
|
|
||||||
&self,
|
|
||||||
name: &str,
|
|
||||||
linkage: Option<String>,
|
|
||||||
ret: Type,
|
|
||||||
params: Vec<Type>,
|
|
||||||
flags: FunctionFlags,
|
|
||||||
) -> Function<'ctx> {
|
|
||||||
unsafe {
|
|
||||||
Function {
|
|
||||||
phantom: PhantomData,
|
|
||||||
builder: self.builder.clone(),
|
|
||||||
value: self.builder.add_function(
|
|
||||||
&self.value,
|
|
||||||
FunctionData {
|
|
||||||
name: name.to_owned(),
|
|
||||||
linkage_name: linkage,
|
|
||||||
ret,
|
|
||||||
params,
|
|
||||||
flags,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn custom_type(&self, ty: CustomTypeKind) -> TypeValue {
|
|
||||||
unsafe {
|
|
||||||
let (name, kind) = match &ty {
|
|
||||||
CustomTypeKind::NamedStruct(NamedStruct(name, _)) => (name.clone(), ty),
|
|
||||||
};
|
|
||||||
self.builder.add_type(&self.value, TypeData { name, kind })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn value(&self) -> ModuleValue {
|
|
||||||
self.value
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_printable(&self) -> PrintableModule<'ctx> {
|
|
||||||
PrintableModule {
|
|
||||||
phantom: PhantomData,
|
|
||||||
module: self.builder.find_module(self.value),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create_debug_info(&mut self, file: DebugFileData) -> (DebugInformation, DebugScopeValue) {
|
|
||||||
let (debug_info, scope_value) = DebugInformation::from_file(file);
|
|
||||||
self.debug_info = Some(debug_info.clone());
|
|
||||||
(debug_info, scope_value)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_debug_info(&self) -> &Option<DebugInformation> {
|
|
||||||
&self.debug_info
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_constant(&self, constant: ConstValueKind) -> ConstantValue {
|
|
||||||
unsafe { self.builder.build_constant(self.value, constant) }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_global<T: Into<String>>(&self, name: T, constant: ConstantValue) -> GlobalValue {
|
|
||||||
unsafe { self.builder.add_global(self.value, name.into(), constant) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'ctx> Drop for Module<'ctx> {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
if let Some(debug_info) = self.debug_info.take() {
|
|
||||||
self.builder.set_debug_information(&self.value, debug_info);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Hash)]
|
|
||||||
pub struct FunctionData {
|
|
||||||
name: String,
|
|
||||||
linkage_name: Option<String>,
|
|
||||||
ret: Type,
|
|
||||||
params: Vec<Type>,
|
|
||||||
flags: FunctionFlags,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Hash)]
|
|
||||||
pub struct FunctionFlags {
|
|
||||||
/// True in the destination module of the import, false in the source module.
|
|
||||||
pub is_extern: bool,
|
|
||||||
/// Whether this function is the main function of the module, that should be
|
|
||||||
/// executed (and linked externally also).
|
|
||||||
pub is_main: bool,
|
|
||||||
/// Whether this function should be available externally always.
|
|
||||||
pub is_pub: bool,
|
|
||||||
/// If this function is an imported function (either in the source or
|
|
||||||
/// destination module)
|
|
||||||
pub is_imported: bool,
|
|
||||||
/// Whether this function should add "alwaysinline"-attribute.
|
|
||||||
pub inline: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for FunctionFlags {
|
|
||||||
fn default() -> FunctionFlags {
|
|
||||||
FunctionFlags {
|
|
||||||
is_extern: false,
|
|
||||||
is_main: false,
|
|
||||||
is_pub: false,
|
|
||||||
is_imported: false,
|
|
||||||
inline: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Function<'ctx> {
|
|
||||||
phantom: PhantomData<&'ctx ()>,
|
|
||||||
builder: Builder,
|
|
||||||
value: FunctionValue,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'ctx> Function<'ctx> {
|
|
||||||
pub fn block(&self, name: &str) -> Block<'ctx> {
|
|
||||||
unsafe {
|
|
||||||
Block {
|
|
||||||
phantom: PhantomData,
|
|
||||||
builder: self.builder.clone(),
|
|
||||||
value: self.builder.add_block(
|
|
||||||
&self.value,
|
|
||||||
BlockData {
|
|
||||||
name: name.to_owned(),
|
|
||||||
terminator: None,
|
|
||||||
terminator_location: None,
|
|
||||||
deleted: false,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_debug(&self, subprogram: DebugScopeValue) {
|
|
||||||
unsafe {
|
|
||||||
self.builder.set_debug_subprogram(&self.value, subprogram);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn value(&self) -> FunctionValue {
|
|
||||||
self.value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Hash)]
|
|
||||||
pub struct BlockData {
|
|
||||||
name: String,
|
|
||||||
terminator: Option<TerminatorKind>,
|
|
||||||
terminator_location: Option<DebugLocationValue>,
|
|
||||||
deleted: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Block<'builder> {
|
|
||||||
phantom: PhantomData<&'builder ()>,
|
|
||||||
builder: Builder,
|
|
||||||
value: BlockValue,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Instr {
|
|
||||||
pub fn default_name(&self) -> &str {
|
|
||||||
match self {
|
|
||||||
Instr::Param(_) => "param",
|
|
||||||
Instr::Constant(_) => "const1",
|
|
||||||
Instr::Add(..) => "add",
|
|
||||||
Instr::FAdd(..) => "fadd",
|
|
||||||
Instr::Sub(..) => "sub",
|
|
||||||
Instr::FSub(..) => "fsub",
|
|
||||||
Instr::Mul(..) => "mul",
|
|
||||||
Instr::FMul(..) => "fmul",
|
|
||||||
Instr::UDiv(..) => "udiv",
|
|
||||||
Instr::SDiv(..) => "sdiv",
|
|
||||||
Instr::FDiv(..) => "fdiv",
|
|
||||||
Instr::URem(..) => "urem",
|
|
||||||
Instr::SRem(..) => "srem",
|
|
||||||
Instr::FRem(..) => "frem",
|
|
||||||
Instr::And(..) => "and",
|
|
||||||
Instr::Phi(_) => "phi",
|
|
||||||
Instr::Alloca(_) => "alloca",
|
|
||||||
Instr::Load(_, _) => "load",
|
|
||||||
Instr::Store(..) => "store",
|
|
||||||
Instr::ArrayAlloca(_, _) => "arrayalloca",
|
|
||||||
Instr::GetElemPtr(..) => "getelemptr",
|
|
||||||
Instr::GetStructElemPtr(..) => "getstructelemptr",
|
|
||||||
Instr::ExtractValue(..) => "extractvalue",
|
|
||||||
Instr::ICmp(..) => "icmp",
|
|
||||||
Instr::FunctionCall(..) => "call",
|
|
||||||
Instr::FCmp(_, _, _) => "fcmp",
|
|
||||||
Instr::Trunc(_, _) => "trunc",
|
|
||||||
Instr::ZExt(_, _) => "zext",
|
|
||||||
Instr::SExt(_, _) => "sext",
|
|
||||||
Instr::FPTrunc(_, _) => "fptrunc",
|
|
||||||
Instr::FPExt(_, _) => "pfext",
|
|
||||||
Instr::FPToUI(_, _) => "fptoui",
|
|
||||||
Instr::FPToSI(_, _) => "fptosi",
|
|
||||||
Instr::UIToFP(_, _) => "uitofp",
|
|
||||||
Instr::SIToFP(_, _) => "sitofp",
|
|
||||||
Instr::PtrToInt(_, _) => "ptrtoint",
|
|
||||||
Instr::IntToPtr(_, _) => "inttoptr",
|
|
||||||
Instr::BitCast(_, _) => "bitcast",
|
|
||||||
Instr::Or(..) => "or",
|
|
||||||
Instr::XOr(..) => "xor",
|
|
||||||
Instr::ShiftRightLogical(..) => "lshr",
|
|
||||||
Instr::ShiftRightArithmetic(..) => "ashr",
|
|
||||||
Instr::ShiftLeft(..) => "shl",
|
|
||||||
Instr::GetGlobal(..) => "global",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'builder> Block<'builder> {
|
|
||||||
pub fn build_named<T: Into<String>>(&mut self, name: T, instruction: Instr) -> CompileResult<InstructionValue> {
|
|
||||||
unsafe {
|
|
||||||
self.builder.add_instruction(
|
|
||||||
&self.value,
|
|
||||||
InstructionData {
|
|
||||||
kind: instruction,
|
|
||||||
location: None,
|
|
||||||
meta: None,
|
|
||||||
},
|
|
||||||
name.into(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn build(&mut self, instruction: Instr) -> CompileResult<InstructionValue> {
|
|
||||||
unsafe {
|
|
||||||
let name = instruction.default_name().to_owned();
|
|
||||||
self.builder.add_instruction(
|
|
||||||
&self.value,
|
|
||||||
InstructionData {
|
|
||||||
kind: instruction,
|
|
||||||
location: None,
|
|
||||||
meta: None,
|
|
||||||
},
|
|
||||||
name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn find_function(&mut self, name: &String) -> Option<FunctionValue> {
|
|
||||||
unsafe { self.builder.find_function(self.value.0.0, name) }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_instr_location(&self, instruction: InstructionValue, location: DebugLocationValue) {
|
|
||||||
unsafe {
|
|
||||||
self.builder.add_instruction_location(&instruction, location);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_instr_metadata(&self, instruction: InstructionValue, location: DebugMetadataValue) {
|
|
||||||
unsafe {
|
|
||||||
self.builder.add_instruction_metadata(&instruction, location);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn terminate(&mut self, instruction: TerminatorKind) -> CompileResult<()> {
|
|
||||||
unsafe { self.builder.terminate(&self.value, instruction) }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_terminator_location(&mut self, location: DebugLocationValue) -> CompileResult<()> {
|
|
||||||
unsafe { self.builder.set_terminator_location(&self.value, location) }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Delete block if it is unused. Return true if deleted, false if not.
|
|
||||||
pub fn delete_if_unused(&mut self) -> CompileResult<bool> {
|
|
||||||
unsafe {
|
|
||||||
if !self.builder.is_block_used(self.value()) {
|
|
||||||
self.builder.delete_block(&self.value)?;
|
|
||||||
Ok(true)
|
|
||||||
} else {
|
|
||||||
Ok(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn value(&self) -> BlockValue {
|
|
||||||
self.value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct InstructionData {
|
|
||||||
kind: Instr,
|
|
||||||
location: Option<DebugLocationValue>,
|
|
||||||
meta: Option<DebugMetadataValue>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Hash)]
|
|
||||||
pub enum CmpPredicate {
|
|
||||||
LT,
|
|
||||||
LE,
|
|
||||||
GT,
|
|
||||||
GE,
|
|
||||||
EQ,
|
|
||||||
NE,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// https://llvm.org/docs/LangRef.html#instruction-reference
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum Instr {
|
|
||||||
Param(usize),
|
|
||||||
Constant(ConstValueKind),
|
|
||||||
GetGlobal(GlobalValue),
|
|
||||||
|
|
||||||
/// Add two integers
|
|
||||||
Add(InstructionValue, InstructionValue),
|
|
||||||
/// Add two floats
|
|
||||||
FAdd(InstructionValue, InstructionValue),
|
|
||||||
/// Subtract two integers
|
|
||||||
Sub(InstructionValue, InstructionValue),
|
|
||||||
/// Subtract two floats
|
|
||||||
FSub(InstructionValue, InstructionValue),
|
|
||||||
/// Multiply two integers
|
|
||||||
Mul(InstructionValue, InstructionValue),
|
|
||||||
/// Multiply two floats
|
|
||||||
FMul(InstructionValue, InstructionValue),
|
|
||||||
/// Divide two unsigned integers
|
|
||||||
UDiv(InstructionValue, InstructionValue),
|
|
||||||
/// Divide two signed integers
|
|
||||||
SDiv(InstructionValue, InstructionValue),
|
|
||||||
/// Divide two floats
|
|
||||||
FDiv(InstructionValue, InstructionValue),
|
|
||||||
/// Get the remainder from two unsigned integers
|
|
||||||
URem(InstructionValue, InstructionValue),
|
|
||||||
/// Get the remainder from two signed integers
|
|
||||||
SRem(InstructionValue, InstructionValue),
|
|
||||||
/// Get the remainder from two floats
|
|
||||||
FRem(InstructionValue, InstructionValue),
|
|
||||||
|
|
||||||
And(InstructionValue, InstructionValue),
|
|
||||||
Or(InstructionValue, InstructionValue),
|
|
||||||
XOr(InstructionValue, InstructionValue),
|
|
||||||
ShiftRightLogical(InstructionValue, InstructionValue),
|
|
||||||
ShiftRightArithmetic(InstructionValue, InstructionValue),
|
|
||||||
ShiftLeft(InstructionValue, InstructionValue),
|
|
||||||
|
|
||||||
Phi(Vec<InstructionValue>),
|
|
||||||
|
|
||||||
Alloca(Type),
|
|
||||||
Load(InstructionValue, Type),
|
|
||||||
Store(InstructionValue, InstructionValue),
|
|
||||||
ArrayAlloca(Type, InstructionValue),
|
|
||||||
GetElemPtr(InstructionValue, Vec<InstructionValue>),
|
|
||||||
GetStructElemPtr(InstructionValue, u32),
|
|
||||||
ExtractValue(InstructionValue, u32),
|
|
||||||
|
|
||||||
/// Integer Comparison
|
|
||||||
ICmp(CmpPredicate, InstructionValue, InstructionValue),
|
|
||||||
/// FLoat Comparison
|
|
||||||
FCmp(CmpPredicate, InstructionValue, InstructionValue),
|
|
||||||
|
|
||||||
/// The `trunc` instruction truncates the high order bits in value and
|
|
||||||
/// converts the remaining bits to ty2. Since the source size must be larger
|
|
||||||
/// than the destination size, `trunc` cannot be a no-op cast. It will
|
|
||||||
/// always truncate bits.
|
|
||||||
Trunc(InstructionValue, Type),
|
|
||||||
/// The `zext` fills the high order bits of the value with zero bits until
|
|
||||||
/// it reaches the size of the destination type, ty2.
|
|
||||||
ZExt(InstructionValue, Type),
|
|
||||||
/// The `sext` instruction performs a sign extension by copying the sign bit
|
|
||||||
/// (highest order bit) of the value until it reaches the bit size of the
|
|
||||||
/// type ty2.
|
|
||||||
SExt(InstructionValue, Type),
|
|
||||||
/// The `fptrunc` instruction casts a value from a larger floating-point
|
|
||||||
/// type to a smaller floating-point type.
|
|
||||||
FPTrunc(InstructionValue, Type),
|
|
||||||
/// The `fpext` instruction extends the value from a smaller floating-point
|
|
||||||
/// type to a larger floating-point type.
|
|
||||||
FPExt(InstructionValue, Type),
|
|
||||||
/// The `fptoui` instruction takes a value to cast, which must be a scalar
|
|
||||||
/// or vector floating-point value, and a type to cast it to ty2, which must
|
|
||||||
/// be an integer type.
|
|
||||||
FPToUI(InstructionValue, Type),
|
|
||||||
/// The `fptosi` instruction takes a value to cast, which must be a scalar
|
|
||||||
/// or vector floating-point value, and a type to cast it to ty2, which must
|
|
||||||
/// be an integer type.
|
|
||||||
FPToSI(InstructionValue, Type),
|
|
||||||
/// The `uitofp` instruction takes a value to cast, which must be a scalar
|
|
||||||
/// or vector integer value, and a type to cast it to ty2, which must be an
|
|
||||||
/// floating-point type.
|
|
||||||
UIToFP(InstructionValue, Type),
|
|
||||||
/// The `sitofp` instruction takes a value to cast, which must be a scalar
|
|
||||||
/// or vector integer value, and a type to cast it to ty2, which must be an
|
|
||||||
/// floating-point type
|
|
||||||
SIToFP(InstructionValue, Type),
|
|
||||||
/// The `ptrtoint` instruction converts value to integer type ty2 by
|
|
||||||
/// interpreting the all pointer representation bits as an integer
|
|
||||||
/// (equivalent to a bitcast) and either truncating or zero extending that
|
|
||||||
/// value to the size of the integer type.
|
|
||||||
PtrToInt(InstructionValue, Type),
|
|
||||||
/// The `inttoptr` instruction converts value to type ty2 by applying either
|
|
||||||
/// a zero extension or a truncation depending on the size of the integer
|
|
||||||
/// value.
|
|
||||||
IntToPtr(InstructionValue, Type),
|
|
||||||
/// The `bitcast` instruction converts value to type ty2. It is always a
|
|
||||||
/// no-op cast because no bits change with this conversion.
|
|
||||||
BitCast(InstructionValue, Type),
|
|
||||||
|
|
||||||
FunctionCall(FunctionValue, Vec<InstructionValue>),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
|
|
||||||
pub enum Type {
|
|
||||||
I8,
|
|
||||||
I16,
|
|
||||||
I32,
|
|
||||||
I64,
|
|
||||||
I128,
|
|
||||||
U8,
|
|
||||||
U16,
|
|
||||||
U32,
|
|
||||||
U64,
|
|
||||||
U128,
|
|
||||||
F16,
|
|
||||||
F32B,
|
|
||||||
F32,
|
|
||||||
F64,
|
|
||||||
F80,
|
|
||||||
F128,
|
|
||||||
F128PPC,
|
|
||||||
Bool,
|
|
||||||
Void,
|
|
||||||
CustomType(TypeValue),
|
|
||||||
Array(Box<Type>, u64),
|
|
||||||
Ptr(Box<Type>),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum ConstValueKind {
|
|
||||||
I8(i8),
|
|
||||||
I16(i16),
|
|
||||||
I32(i32),
|
|
||||||
I64(i64),
|
|
||||||
I128(i128),
|
|
||||||
U8(u8),
|
|
||||||
U16(u16),
|
|
||||||
U32(u32),
|
|
||||||
U64(u64),
|
|
||||||
U128(u128),
|
|
||||||
Bool(bool),
|
|
||||||
Str(String),
|
|
||||||
F16(f32),
|
|
||||||
F32B(f32),
|
|
||||||
F32(f32),
|
|
||||||
F64(f64),
|
|
||||||
F80(f64),
|
|
||||||
F128(f64),
|
|
||||||
F128PPC(f64),
|
|
||||||
Array(Vec<ConstantValue>, Type),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Hash)]
|
|
||||||
pub enum TerminatorKind {
|
|
||||||
Ret(InstructionValue),
|
|
||||||
RetVoid,
|
|
||||||
Br(BlockValue),
|
|
||||||
CondBr(InstructionValue, BlockValue, BlockValue),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
|
|
||||||
pub struct TypeData {
|
|
||||||
name: String,
|
|
||||||
kind: CustomTypeKind,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
|
|
||||||
pub enum CustomTypeKind {
|
|
||||||
NamedStruct(NamedStruct),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
|
|
||||||
pub struct NamedStruct(pub String, pub Vec<Type>);
|
|
||||||
|
|
||||||
impl ConstValueKind {
|
|
||||||
pub fn get_type(&self) -> Type {
|
|
||||||
use Type::*;
|
|
||||||
match self {
|
|
||||||
ConstValueKind::I8(_) => I8,
|
|
||||||
ConstValueKind::I16(_) => I16,
|
|
||||||
ConstValueKind::I32(_) => I32,
|
|
||||||
ConstValueKind::I64(_) => I64,
|
|
||||||
ConstValueKind::I128(_) => I128,
|
|
||||||
ConstValueKind::U8(_) => U8,
|
|
||||||
ConstValueKind::U16(_) => U16,
|
|
||||||
ConstValueKind::U32(_) => U32,
|
|
||||||
ConstValueKind::U64(_) => U64,
|
|
||||||
ConstValueKind::U128(_) => U128,
|
|
||||||
ConstValueKind::Str(_) => Type::Ptr(Box::new(U8)),
|
|
||||||
ConstValueKind::Bool(_) => Bool,
|
|
||||||
ConstValueKind::F16(_) => F16,
|
|
||||||
ConstValueKind::F32B(_) => F32B,
|
|
||||||
ConstValueKind::F32(_) => F32,
|
|
||||||
ConstValueKind::F64(_) => F64,
|
|
||||||
ConstValueKind::F80(_) => F80,
|
|
||||||
ConstValueKind::F128(_) => F128,
|
|
||||||
ConstValueKind::F128PPC(_) => F128PPC,
|
|
||||||
ConstValueKind::Array(vals, ty) => Type::Array(Box::new(ty.clone()), vals.len() as u64),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
|
|
||||||
pub enum TypeCategory {
|
|
||||||
SignedInteger,
|
|
||||||
UnsignedInteger,
|
|
||||||
Void,
|
|
||||||
Real,
|
|
||||||
Ptr,
|
|
||||||
CustomType,
|
|
||||||
Array,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TypeCategory {
|
|
||||||
pub fn comparable(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
TypeCategory::SignedInteger => true,
|
|
||||||
TypeCategory::UnsignedInteger => true,
|
|
||||||
TypeCategory::Real => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn signed(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
TypeCategory::SignedInteger => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn integer(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
TypeCategory::SignedInteger => true,
|
|
||||||
TypeCategory::UnsignedInteger => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Type {
|
|
||||||
pub fn category(&self) -> TypeCategory {
|
|
||||||
match self {
|
|
||||||
Type::I8 | Type::I16 | Type::I32 | Type::I64 | Type::I128 => TypeCategory::SignedInteger,
|
|
||||||
Type::U8 | Type::U16 | Type::U32 | Type::U64 | Type::U128 => TypeCategory::UnsignedInteger,
|
|
||||||
Type::F16 | Type::F32B | Type::F32 | Type::F64 | Type::F80 | Type::F128 | Type::F128PPC => {
|
|
||||||
TypeCategory::Real
|
|
||||||
}
|
|
||||||
Type::Bool => TypeCategory::UnsignedInteger,
|
|
||||||
Type::Void => TypeCategory::Void,
|
|
||||||
Type::CustomType(_) => TypeCategory::CustomType,
|
|
||||||
Type::Array(_, _) => TypeCategory::Array,
|
|
||||||
Type::Ptr(_) => TypeCategory::Ptr,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn cast_instruction(&self, value: InstructionValue, other: &Type) -> Option<Instr> {
|
|
||||||
use Type::*;
|
|
||||||
match (self, other) {
|
|
||||||
(I8, I16 | I32 | I64 | I128) => Some(Instr::SExt(value, other.clone())),
|
|
||||||
(I16, I32 | I64 | I128) => Some(Instr::SExt(value, other.clone())),
|
|
||||||
(I32, I64 | I128) => Some(Instr::SExt(value, other.clone())),
|
|
||||||
(I64, I128) => Some(Instr::SExt(value, other.clone())),
|
|
||||||
(I128 | U128, I64 | U64 | I32 | U32 | I16 | U16 | I8 | U8) => Some(Instr::Trunc(value, other.clone())),
|
|
||||||
(I64 | U64, I32 | U32 | I16 | U16 | I8 | U8) => Some(Instr::Trunc(value, other.clone())),
|
|
||||||
(I32 | U32, I16 | U16 | I8 | U8) => Some(Instr::Trunc(value, other.clone())),
|
|
||||||
(I16 | U16, I8 | U8) => Some(Instr::Trunc(value, other.clone())),
|
|
||||||
(U8 | I8, U8 | I8 | U16 | I16 | U32 | I32 | U64 | I64 | U128 | I128) => {
|
|
||||||
Some(Instr::ZExt(value, other.clone()))
|
|
||||||
}
|
|
||||||
(U16 | I16, U16 | I16 | U32 | I32 | U64 | I64 | U128 | I128) => Some(Instr::ZExt(value, other.clone())),
|
|
||||||
(U32 | I32, U32 | I32 | U64 | I64 | U128 | I128) => Some(Instr::ZExt(value, other.clone())),
|
|
||||||
(U64 | I64, U64 | I64 | U128 | I128) => Some(Instr::ZExt(value, other.clone())),
|
|
||||||
(U128 | I128, U128 | I128) => Some(Instr::ZExt(value, other.clone())),
|
|
||||||
(U8 | U16 | U32 | U64 | U128, F16 | F32 | F32B | F64 | F80 | F128 | F128PPC) => {
|
|
||||||
Some(Instr::UIToFP(value, other.clone()))
|
|
||||||
}
|
|
||||||
(I8 | I16 | I32 | I64 | I128, F16 | F32 | F32B | F64 | F80 | F128 | F128PPC) => {
|
|
||||||
Some(Instr::SIToFP(value, other.clone()))
|
|
||||||
}
|
|
||||||
(F16 | F32 | F32B | F64 | F80 | F128 | F128PPC, U8 | U16 | U32 | U64 | U128) => {
|
|
||||||
Some(Instr::FPToUI(value, other.clone()))
|
|
||||||
}
|
|
||||||
(F16 | F32 | F32B | F64 | F80 | F128 | F128PPC, I8 | I16 | I32 | I64 | I128) => {
|
|
||||||
Some(Instr::FPToSI(value, other.clone()))
|
|
||||||
}
|
|
||||||
(I128 | U128 | I64 | U64 | I32 | U32 | I16 | U16 | I8 | U8, Ptr(_)) => {
|
|
||||||
Some(Instr::IntToPtr(value, other.clone()))
|
|
||||||
}
|
|
||||||
(Ptr(_), I128 | U128 | I64 | U64 | I32 | U32 | I16 | U16 | I8 | U8) => {
|
|
||||||
Some(Instr::PtrToInt(value, other.clone()))
|
|
||||||
}
|
|
||||||
(F16, F32 | F32B | F64 | F80 | F128 | F128PPC) => Some(Instr::FPExt(value, other.clone())),
|
|
||||||
(F32 | F32B, F64 | F80 | F128 | F128PPC) => Some(Instr::FPExt(value, other.clone())),
|
|
||||||
(F64, F80 | F128 | F128PPC) => Some(Instr::FPExt(value, other.clone())),
|
|
||||||
(F80, F128 | F128PPC) => Some(Instr::FPExt(value, other.clone())),
|
|
||||||
(F128PPC | F128, F80 | F64 | F32B | F32 | F16) => Some(Instr::FPTrunc(value, other.clone())),
|
|
||||||
(F80, F64 | F32B | F32 | F16) => Some(Instr::FPTrunc(value, other.clone())),
|
|
||||||
(F64, F32B | F32 | F16) => Some(Instr::FPTrunc(value, other.clone())),
|
|
||||||
(F32B | F32, F16) => Some(Instr::FPTrunc(value, other.clone())),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TerminatorKind {
|
|
||||||
pub(crate) fn get_type(&self, builder: &Builder) -> CompileResult<Type> {
|
|
||||||
use TerminatorKind::*;
|
|
||||||
match self {
|
|
||||||
Ret(instr_val) => instr_val.get_type(builder),
|
|
||||||
RetVoid => Ok(Type::Void),
|
|
||||||
Br(_) => Ok(Type::Void),
|
|
||||||
CondBr(_, _, _) => Ok(Type::Void),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,69 +0,0 @@
|
|||||||
//! Copied from
|
|
||||||
//! https://github.com/rust-lang/rust/blob/6b3ae3f6e45a33c2d95fa0362c9b2593e567fd34/library/core/src/fmt/builders.rs#L102
|
|
||||||
|
|
||||||
// Copyright (c) The Rust Project Contributors
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
|
||||||
// in the Software without restriction, including without limitation the rights
|
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
// copies of the Software, and to permit persons to whom the Software is
|
|
||||||
// furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in
|
|
||||||
// all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
// SOFTWARE.
|
|
||||||
|
|
||||||
use std::fmt;
|
|
||||||
|
|
||||||
pub struct PadAdapter<'buf, 'state> {
|
|
||||||
buf: &'buf mut (dyn fmt::Write + 'buf),
|
|
||||||
state: &'state mut PadAdapterState,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct PadAdapterState {
|
|
||||||
on_newline: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for PadAdapterState {
|
|
||||||
fn default() -> Self {
|
|
||||||
PadAdapterState { on_newline: true }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'buf, 'state> PadAdapter<'buf, 'state> {
|
|
||||||
pub fn wrap<'slot, 'fmt: 'buf + 'slot>(
|
|
||||||
fmt: &'buf mut (dyn fmt::Write + 'buf),
|
|
||||||
state: &'state mut PadAdapterState,
|
|
||||||
) -> Self {
|
|
||||||
PadAdapter { buf: fmt, state }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Write for PadAdapter<'_, '_> {
|
|
||||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
|
||||||
for s in s.split_inclusive('\n') {
|
|
||||||
if self.state.on_newline {
|
|
||||||
self.buf.write_str(" ")?;
|
|
||||||
}
|
|
||||||
self.state.on_newline = s.ends_with('\n');
|
|
||||||
self.buf.write_str(s)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_char(&mut self, c: char) -> fmt::Result {
|
|
||||||
if self.state.on_newline {
|
|
||||||
self.buf.write_str(" ")?;
|
|
||||||
}
|
|
||||||
self.state.on_newline = c == '\n';
|
|
||||||
self.buf.write_char(c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,132 +0,0 @@
|
|||||||
use std::{
|
|
||||||
ffi::{CStr, CString, c_char},
|
|
||||||
ptr::null_mut,
|
|
||||||
string::FromUtf8Error,
|
|
||||||
};
|
|
||||||
|
|
||||||
use llvm_sys::{
|
|
||||||
core::{
|
|
||||||
LLVMCreateMemoryBufferWithMemoryRange, LLVMDisposeMemoryBuffer, LLVMGetBufferSize,
|
|
||||||
LLVMGetBufferStart,
|
|
||||||
},
|
|
||||||
error::LLVMDisposeErrorMessage,
|
|
||||||
prelude::LLVMMemoryBufferRef,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
CompileResult, ErrorKind, Type,
|
|
||||||
builder::{Builder, InstructionValue},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn into_cstring<T: Into<String>>(value: T) -> CString {
|
|
||||||
let string = value.into();
|
|
||||||
unsafe { CString::from_vec_with_nul_unchecked((string + "\0").into_bytes()) }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_cstring(pointer: *mut c_char) -> Option<String> {
|
|
||||||
if pointer.is_null() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
unsafe { CStr::from_ptr(pointer).to_str().ok().map(|s| s.to_owned()) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cstring_to_err(value: *mut c_char) -> Result<(), String> {
|
|
||||||
from_cstring(value)
|
|
||||||
.filter(|s| !s.is_empty())
|
|
||||||
.map_or(Ok(()), |s| Err(s))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Utility struct for LLVM's Error Messages, which need to be disposed
|
|
||||||
/// manually.
|
|
||||||
pub struct ErrorMessageHolder(*mut c_char);
|
|
||||||
|
|
||||||
impl ErrorMessageHolder {
|
|
||||||
pub fn null() -> Self {
|
|
||||||
ErrorMessageHolder(null_mut())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn borrow_mut(&mut self) -> *mut *mut c_char {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn into_result(&self) -> Result<(), String> {
|
|
||||||
cstring_to_err(self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for ErrorMessageHolder {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
unsafe {
|
|
||||||
if !self.0.is_null() {
|
|
||||||
LLVMDisposeErrorMessage(self.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Utility for creating and handling LLVM MemoryBuffers, needed for printing
|
|
||||||
/// out ASM and .o -files without relying on LLVM's own API.
|
|
||||||
pub struct MemoryBufferHolder {
|
|
||||||
pub buffer: LLVMMemoryBufferRef,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MemoryBufferHolder {
|
|
||||||
pub fn empty(name: &str) -> MemoryBufferHolder {
|
|
||||||
let array = [0i8; 0];
|
|
||||||
unsafe {
|
|
||||||
let buffer = LLVMCreateMemoryBufferWithMemoryRange(
|
|
||||||
array.as_ptr(),
|
|
||||||
array.len(),
|
|
||||||
into_cstring(name).as_ptr(),
|
|
||||||
0,
|
|
||||||
);
|
|
||||||
MemoryBufferHolder { buffer }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_buffer(&self) -> Vec<u8> {
|
|
||||||
unsafe {
|
|
||||||
let start = LLVMGetBufferStart(self.buffer);
|
|
||||||
let size = LLVMGetBufferSize(self.buffer);
|
|
||||||
|
|
||||||
let mut buff = Vec::with_capacity(size);
|
|
||||||
for i in 0..size {
|
|
||||||
buff.push(*start.add(i) as u8);
|
|
||||||
}
|
|
||||||
buff
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_string(&self) -> Result<String, FromUtf8Error> {
|
|
||||||
String::from_utf8(self.as_buffer())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for MemoryBufferHolder {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
unsafe {
|
|
||||||
LLVMDisposeMemoryBuffer(self.buffer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Make sure types for given instructions match. Return Ok(type) if they do,
|
|
||||||
/// and error otherwise.
|
|
||||||
pub fn match_types(
|
|
||||||
lhs: &InstructionValue,
|
|
||||||
rhs: &InstructionValue,
|
|
||||||
builder: &Builder,
|
|
||||||
) -> CompileResult<Type> {
|
|
||||||
let lhs_type = lhs.get_type(&builder);
|
|
||||||
let rhs_type = rhs.get_type(&builder);
|
|
||||||
if let (Ok(lhs_t), Ok(rhs_t)) = (lhs_type, rhs_type) {
|
|
||||||
if lhs_t == rhs_t {
|
|
||||||
Ok(lhs_t)
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Err(ErrorKind::Null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
1
reid-lsp/.gitignore
vendored
1
reid-lsp/.gitignore
vendored
@ -4,3 +4,4 @@ dist
|
|||||||
package-lock.json
|
package-lock.json
|
||||||
pnpm-lock.yaml
|
pnpm-lock.yaml
|
||||||
tsconfig.tsbuildinfo
|
tsconfig.tsbuildinfo
|
||||||
|
*.vsix
|
||||||
@ -1,6 +1,6 @@
|
|||||||
# Change Log
|
# Change Log
|
||||||
|
|
||||||
All notable changes to the "reid-lsp" extension will be documented in this file.
|
All notable changes to the "reid-language-server" extension will be documented in this file.
|
||||||
|
|
||||||
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
|
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,13 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "reid-lsp"
|
name = "reid-language-server"
|
||||||
version = "0.1.0"
|
version = "1.0.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
socket = "0.0.7"
|
socket = "0.0.7"
|
||||||
tokio = { version = "1.47.0", features = ["full"] }
|
tokio = { version = "1.47.0", features = ["full"] }
|
||||||
tower-lsp = "0.20.0"
|
tower-lsp = "0.20.0"
|
||||||
reid = { path = "../reid", version = "1.0.0-beta.2", registry="gitea-teascade", features=[] }
|
reid = { path = "../reid", version = "1.0.0", registry="gitea-teascade", features=[] }
|
||||||
dashmap = "6.1.0"
|
dashmap = "6.1.0"
|
||||||
|
serde = "*"
|
||||||
|
serde_json = "*"
|
||||||
|
|||||||
@ -1,71 +1 @@
|
|||||||
# reid-lsp README
|
# Reid Language Server
|
||||||
|
|
||||||
This is the README for your extension "reid-lsp". After writing up a brief description, we recommend including the following sections.
|
|
||||||
|
|
||||||
## Features
|
|
||||||
|
|
||||||
Describe specific features of your extension including screenshots of your extension in action. Image paths are relative to this README file.
|
|
||||||
|
|
||||||
For example if there is an image subfolder under your extension project workspace:
|
|
||||||
|
|
||||||
\!\[feature X\]\(images/feature-x.png\)
|
|
||||||
|
|
||||||
> Tip: Many popular extensions utilize animations. This is an excellent way to show off your extension! We recommend short, focused animations that are easy to follow.
|
|
||||||
|
|
||||||
## Requirements
|
|
||||||
|
|
||||||
If you have any requirements or dependencies, add a section describing those and how to install and configure them.
|
|
||||||
|
|
||||||
## Extension Settings
|
|
||||||
|
|
||||||
Include if your extension adds any VS Code settings through the `contributes.configuration` extension point.
|
|
||||||
|
|
||||||
For example:
|
|
||||||
|
|
||||||
This extension contributes the following settings:
|
|
||||||
|
|
||||||
* `myExtension.enable`: Enable/disable this extension.
|
|
||||||
* `myExtension.thing`: Set to `blah` to do something.
|
|
||||||
|
|
||||||
## Known Issues
|
|
||||||
|
|
||||||
Calling out known issues can help limit users opening duplicate issues against your extension.
|
|
||||||
|
|
||||||
## Release Notes
|
|
||||||
|
|
||||||
Users appreciate release notes as you update your extension.
|
|
||||||
|
|
||||||
### 1.0.0
|
|
||||||
|
|
||||||
Initial release of ...
|
|
||||||
|
|
||||||
### 1.0.1
|
|
||||||
|
|
||||||
Fixed issue #.
|
|
||||||
|
|
||||||
### 1.1.0
|
|
||||||
|
|
||||||
Added features X, Y, and Z.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Following extension guidelines
|
|
||||||
|
|
||||||
Ensure that you've read through the extensions guidelines and follow the best practices for creating your extension.
|
|
||||||
|
|
||||||
* [Extension Guidelines](https://code.visualstudio.com/api/references/extension-guidelines)
|
|
||||||
|
|
||||||
## Working with Markdown
|
|
||||||
|
|
||||||
You can author your README using Visual Studio Code. Here are some useful editor keyboard shortcuts:
|
|
||||||
|
|
||||||
* Split the editor (`Cmd+\` on macOS or `Ctrl+\` on Windows and Linux).
|
|
||||||
* Toggle preview (`Shift+Cmd+V` on macOS or `Shift+Ctrl+V` on Windows and Linux).
|
|
||||||
* Press `Ctrl+Space` (Windows, Linux, macOS) to see a list of Markdown snippets.
|
|
||||||
|
|
||||||
## For more information
|
|
||||||
|
|
||||||
* [Visual Studio Code's Markdown Support](http://code.visualstudio.com/docs/languages/markdown)
|
|
||||||
* [Markdown Syntax Reference](https://help.github.com/articles/markdown-basics/)
|
|
||||||
|
|
||||||
**Enjoy!**
|
|
||||||
@ -4,7 +4,7 @@
|
|||||||
* ------------------------------------------------------------------------------------------ */
|
* ------------------------------------------------------------------------------------------ */
|
||||||
|
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import { workspace, ExtensionContext, window } from 'vscode';
|
import { workspace, ExtensionContext, window, languages, SemanticTokensBuilder } from 'vscode';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Executable,
|
Executable,
|
||||||
@ -18,16 +18,25 @@ import {
|
|||||||
let client: LanguageClient;
|
let client: LanguageClient;
|
||||||
|
|
||||||
export function activate(context: ExtensionContext) {
|
export function activate(context: ExtensionContext) {
|
||||||
const traceOutputChannel = window.createOutputChannel("Reid Language Server trace");
|
const configuration = workspace.getConfiguration('reid-language-server');
|
||||||
const command = process.env.SERVER_PATH || "reid-language-server";
|
let server_path: string = process.env.SERVER_PATH ?? configuration.get("language-server-path") ?? 'reid-language-server';
|
||||||
|
const regex = /\$(\w+)/;
|
||||||
|
while (regex.test(server_path)) {
|
||||||
|
let envVar = regex.exec(server_path)?.[1];
|
||||||
|
const envVal = envVar ? process.env[envVar] : undefined;
|
||||||
|
if (envVar === undefined || envVal === undefined) {
|
||||||
|
console.error(`No such environment variables as ${envVar}`);
|
||||||
|
}
|
||||||
|
server_path = server_path.replaceAll(`$${envVar}`, envVal ?? '');
|
||||||
|
}
|
||||||
|
|
||||||
const run: Executable = {
|
const run: Executable = {
|
||||||
command,
|
command: server_path,
|
||||||
options: {
|
options: {
|
||||||
env: {
|
env: {
|
||||||
...process.env,
|
...process.env,
|
||||||
RUST_LOG: "debug",
|
RUST_LOG: "debug",
|
||||||
|
RUST_BACKTRACE: 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -44,21 +53,36 @@ export function activate(context: ExtensionContext) {
|
|||||||
synchronize: {
|
synchronize: {
|
||||||
// Notify the server about file changes to '.clientrc files contained in the workspace
|
// Notify the server about file changes to '.clientrc files contained in the workspace
|
||||||
fileEvents: workspace.createFileSystemWatcher('**/.clientrc')
|
fileEvents: workspace.createFileSystemWatcher('**/.clientrc')
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create the language client and start the client.
|
// Create the language client and start the client.
|
||||||
client = new LanguageClient(
|
client = new LanguageClient(
|
||||||
'reid-lsp',
|
'reid-language-server',
|
||||||
'Reid Language Server',
|
'Reid Language Server',
|
||||||
serverOptions,
|
serverOptions,
|
||||||
clientOptions
|
clientOptions
|
||||||
);
|
);
|
||||||
|
client.info(JSON.stringify(server_path));
|
||||||
|
|
||||||
client.info("hello");
|
client.info(`Loaded Reid Language Server from ${server_path}`);
|
||||||
|
|
||||||
workspace.onDidOpenTextDocument((e) => {
|
|
||||||
});
|
workspace.onDidOpenTextDocument((e) => { });
|
||||||
|
|
||||||
|
client.info("Registering semantic tokens provide");
|
||||||
|
context.subscriptions.push(languages.registerDocumentSemanticTokensProvider({
|
||||||
|
language: 'reid',
|
||||||
|
scheme: 'file'
|
||||||
|
}, {
|
||||||
|
provideDocumentSemanticTokens: () => {
|
||||||
|
const builder = new SemanticTokensBuilder();
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
tokenTypes: [],
|
||||||
|
tokenModifiers: [],
|
||||||
|
}));
|
||||||
|
|
||||||
// Start the client. This will also launch the server
|
// Start the client. This will also launch the server
|
||||||
client.start();
|
client.start();
|
||||||
|
|||||||
92
reid-lsp/language-configuration.json
Normal file
92
reid-lsp/language-configuration.json
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
{
|
||||||
|
"comments": {
|
||||||
|
"lineComment": "//",
|
||||||
|
},
|
||||||
|
"brackets": [
|
||||||
|
[
|
||||||
|
"{",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"[",
|
||||||
|
"]"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"(",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
],
|
||||||
|
"autoClosingPairs": [
|
||||||
|
{
|
||||||
|
"open": "{",
|
||||||
|
"close": "}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"open": "[",
|
||||||
|
"close": "]"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"open": "(",
|
||||||
|
"close": ")"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"open": "'",
|
||||||
|
"close": "'",
|
||||||
|
"notIn": [
|
||||||
|
"string",
|
||||||
|
"comment"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"open": "\"",
|
||||||
|
"close": "\"",
|
||||||
|
"notIn": [
|
||||||
|
"string"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"autoCloseBefore": ";:.,=}])>` \n\t",
|
||||||
|
"surroundingPairs": [
|
||||||
|
[
|
||||||
|
"{",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"[",
|
||||||
|
"]"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"(",
|
||||||
|
")"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"\"",
|
||||||
|
"\""
|
||||||
|
],
|
||||||
|
],
|
||||||
|
"folding": {
|
||||||
|
"markers": {
|
||||||
|
"start": "^\\s*//\\s*#?region\\b",
|
||||||
|
"end": "^\\s*//\\s*#?endregion\\b"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"wordPattern": "[a-Z](\\w*)",
|
||||||
|
"indentationRules": {
|
||||||
|
"increaseIndentPattern": "^((?!\\/\\/).)*(\\{[^}\"'`]*|\\([^)\"'`]*|\\[[^\\]\"'`]*)$",
|
||||||
|
"decreaseIndentPattern": "^((?!.*?\\/\\*).*\\*/)?\\s*[\\)\\}\\]].*$"
|
||||||
|
},
|
||||||
|
"colorizedBracketPairs": [
|
||||||
|
[
|
||||||
|
"(",
|
||||||
|
")"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"[",
|
||||||
|
"]"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"{",
|
||||||
|
"}"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -1,46 +1,56 @@
|
|||||||
{
|
{
|
||||||
"name": "reid-lsp",
|
"name": "reid-language-server",
|
||||||
"displayName": "Reid Language Server",
|
"displayName": "Reid Language Server",
|
||||||
"description": "Language Server Extension for Reid",
|
"description": "Language Server Extension for Reid",
|
||||||
"version": "0.0.1",
|
"version": "1.0.0",
|
||||||
|
"repository": {
|
||||||
|
"url": "https://git.teascade.net/teascade"
|
||||||
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"vscode": "^1.102.0"
|
"vscode": "^1.102.0"
|
||||||
},
|
},
|
||||||
"categories": [
|
"categories": [
|
||||||
"Other"
|
"Other"
|
||||||
],
|
],
|
||||||
"activationEvents": [
|
|
||||||
"onLanguage:reid"
|
|
||||||
],
|
|
||||||
"main": "./dist/extension.js",
|
"main": "./dist/extension.js",
|
||||||
|
"icon": "./reid.png",
|
||||||
|
"publisher": "Teascade",
|
||||||
|
"author": {
|
||||||
|
"email": "teascade@teascade.net",
|
||||||
|
"name": "Teascade",
|
||||||
|
"url": "https://teascade.net"
|
||||||
|
},
|
||||||
"contributes": {
|
"contributes": {
|
||||||
"languages": [
|
"languages": [
|
||||||
{
|
{
|
||||||
"id": "reid",
|
"id": "reid",
|
||||||
"extensions": [
|
"extensions": [
|
||||||
".reid"
|
".reid"
|
||||||
]
|
],
|
||||||
|
"aliases": [
|
||||||
|
"Reid"
|
||||||
|
],
|
||||||
|
"configuration": "./language-configuration.json",
|
||||||
|
"icon": {
|
||||||
|
"dark": "./reid.png",
|
||||||
|
"light": "./reid.png"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"breakpoints": [
|
||||||
|
{
|
||||||
|
"language": "reid"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"configuration": {
|
"configuration": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"title": "reid-language-server",
|
"title": "reid-language-server",
|
||||||
"properties": {
|
"properties": {
|
||||||
"nrs-language-server.trace.server": {
|
"reid-language-server.language-server-path": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"scope": "window",
|
"scope": "window",
|
||||||
"enum": [
|
"default": "$HOME/.cargo/bin/reid-language-server",
|
||||||
"off",
|
"description": "Path to the Reid Language Server executable"
|
||||||
"messages",
|
|
||||||
"verbose"
|
|
||||||
],
|
|
||||||
"enumDescriptions": [
|
|
||||||
"No traces",
|
|
||||||
"Error only",
|
|
||||||
"Full log"
|
|
||||||
],
|
|
||||||
"default": "off",
|
|
||||||
"description": "Traces the communication between VS Code and the language server."
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
BIN
reid-lsp/reid.png
Normal file
BIN
reid-lsp/reid.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
1255
reid-lsp/src/analysis.rs
Normal file
1255
reid-lsp/src/analysis.rs
Normal file
File diff suppressed because it is too large
Load Diff
@ -3,27 +3,41 @@ use std::path::PathBuf;
|
|||||||
|
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
use reid::ast::lexer::{FullToken, Position};
|
use reid::ast::lexer::{FullToken, Position};
|
||||||
use reid::error_raporting::{ErrorModules, ReidError};
|
use reid::error_raporting::{self, ErrorModules, ReidError};
|
||||||
use reid::mir::{
|
use reid::mir::SourceModuleId;
|
||||||
self, Context, FunctionCall, FunctionDefinition, FunctionParam, IfExpression, SourceModuleId, StructType, TypeKind,
|
use reid::parse_module;
|
||||||
WhileStatement,
|
use serde::{Deserialize, Serialize};
|
||||||
};
|
use tokio::sync::Mutex;
|
||||||
use reid::{compile_module, parse_module, perform_all_passes};
|
|
||||||
use tower_lsp::lsp_types::{
|
use tower_lsp::lsp_types::{
|
||||||
self, CompletionItem, CompletionOptions, CompletionParams, CompletionResponse, Diagnostic, DiagnosticSeverity,
|
self, CompletionItem, CompletionOptions, CompletionParams, CompletionResponse, Diagnostic, DiagnosticSeverity,
|
||||||
DidChangeTextDocumentParams, DidOpenTextDocumentParams, Hover, HoverContents, HoverParams, HoverProviderCapability,
|
DidChangeTextDocumentParams, DidOpenTextDocumentParams, DidSaveTextDocumentParams, DocumentFilter,
|
||||||
InitializeParams, InitializeResult, InitializedParams, MarkedString, MarkupContent, MarkupKind, MessageType, OneOf,
|
GotoDefinitionParams, GotoDefinitionResponse, Hover, HoverContents, HoverParams, HoverProviderCapability,
|
||||||
Range, ServerCapabilities, TextDocumentItem, TextDocumentSyncCapability, TextDocumentSyncKind,
|
InitializeParams, InitializeResult, InitializedParams, Location, MarkedString, MarkupContent, MarkupKind,
|
||||||
TextDocumentSyncOptions, WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities,
|
MessageType, OneOf, Range, ReferenceParams, RenameParams, SemanticToken, SemanticTokensLegend,
|
||||||
|
SemanticTokensOptions, SemanticTokensParams, SemanticTokensResult, SemanticTokensServerCapabilities,
|
||||||
|
ServerCapabilities, TextDocumentItem, TextDocumentRegistrationOptions, TextDocumentSyncCapability,
|
||||||
|
TextDocumentSyncKind, TextDocumentSyncOptions, TextDocumentSyncSaveOptions, TextEdit, Url, WorkspaceEdit,
|
||||||
|
WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities,
|
||||||
};
|
};
|
||||||
use tower_lsp::{Client, LanguageServer, LspService, Server, jsonrpc};
|
use tower_lsp::{Client, LanguageServer, LspService, Server, jsonrpc};
|
||||||
|
|
||||||
|
use crate::analysis::{MODIFIER_LEGEND, StateMap, StaticAnalysis, TOKEN_LEGEND, analyze};
|
||||||
|
|
||||||
|
mod analysis;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Backend {
|
struct Backend {
|
||||||
client: Client,
|
client: Client,
|
||||||
tokens: DashMap<String, Vec<FullToken>>,
|
analysis: DashMap<PathBuf, StaticAnalysis>,
|
||||||
ast: DashMap<String, reid::ast::Module>,
|
module_to_path: DashMap<SourceModuleId, PathBuf>,
|
||||||
types: DashMap<String, DashMap<FullToken, Option<TypeKind>>>,
|
path_to_module: DashMap<PathBuf, SourceModuleId>,
|
||||||
|
module_id_counter: Mutex<SourceModuleId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
struct CompletionData {
|
||||||
|
token_idx: usize,
|
||||||
|
path: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tower_lsp::async_trait]
|
#[tower_lsp::async_trait]
|
||||||
@ -38,29 +52,67 @@ impl LanguageServer for Backend {
|
|||||||
change: Some(TextDocumentSyncKind::FULL),
|
change: Some(TextDocumentSyncKind::FULL),
|
||||||
will_save: None,
|
will_save: None,
|
||||||
will_save_wait_until: None,
|
will_save_wait_until: None,
|
||||||
save: None,
|
save: Some(TextDocumentSyncSaveOptions::SaveOptions(lsp_types::SaveOptions {
|
||||||
|
include_text: Some(true),
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
|
||||||
|
let capabilities = ServerCapabilities {
|
||||||
|
hover_provider: Some(HoverProviderCapability::Simple(true)),
|
||||||
|
completion_provider: Some(CompletionOptions {
|
||||||
|
trigger_characters: None,
|
||||||
|
all_commit_characters: None,
|
||||||
|
completion_item: Some(lsp_types::CompletionOptionsCompletionItem {
|
||||||
|
label_details_support: Some(true),
|
||||||
|
}),
|
||||||
|
resolve_provider: Some(false),
|
||||||
|
work_done_progress_options: lsp_types::WorkDoneProgressOptions {
|
||||||
|
work_done_progress: Some(true),
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
text_document_sync: Some(TextDocumentSyncCapability::Options(sync)),
|
||||||
|
workspace: Some(WorkspaceServerCapabilities {
|
||||||
|
workspace_folders: Some(WorkspaceFoldersServerCapabilities {
|
||||||
|
supported: Some(true),
|
||||||
|
change_notifications: Some(OneOf::Left(true)),
|
||||||
|
}),
|
||||||
|
file_operations: None,
|
||||||
|
}),
|
||||||
|
semantic_tokens_provider: Some(SemanticTokensServerCapabilities::SemanticTokensRegistrationOptions(
|
||||||
|
lsp_types::SemanticTokensRegistrationOptions {
|
||||||
|
text_document_registration_options: TextDocumentRegistrationOptions {
|
||||||
|
document_selector: Some(vec![DocumentFilter {
|
||||||
|
language: Some("reid".to_owned()),
|
||||||
|
scheme: Some("file".to_owned()),
|
||||||
|
pattern: None,
|
||||||
|
}]),
|
||||||
|
},
|
||||||
|
semantic_tokens_options: SemanticTokensOptions {
|
||||||
|
work_done_progress_options: Default::default(),
|
||||||
|
legend: SemanticTokensLegend {
|
||||||
|
token_types: TOKEN_LEGEND.into(),
|
||||||
|
token_modifiers: MODIFIER_LEGEND.into(),
|
||||||
|
},
|
||||||
|
range: None,
|
||||||
|
full: Some(lsp_types::SemanticTokensFullOptions::Bool(true)),
|
||||||
|
},
|
||||||
|
static_registration_options: Default::default(),
|
||||||
|
},
|
||||||
|
)),
|
||||||
|
references_provider: Some(OneOf::Left(true)),
|
||||||
|
definition_provider: Some(OneOf::Left(true)),
|
||||||
|
rename_provider: Some(OneOf::Left(true)),
|
||||||
|
..Default::default()
|
||||||
};
|
};
|
||||||
Ok(InitializeResult {
|
Ok(InitializeResult {
|
||||||
capabilities: ServerCapabilities {
|
capabilities,
|
||||||
hover_provider: Some(HoverProviderCapability::Simple(true)),
|
|
||||||
completion_provider: Some(CompletionOptions { ..Default::default() }),
|
|
||||||
text_document_sync: Some(TextDocumentSyncCapability::Options(sync)),
|
|
||||||
workspace: Some(WorkspaceServerCapabilities {
|
|
||||||
workspace_folders: Some(WorkspaceFoldersServerCapabilities {
|
|
||||||
supported: Some(true),
|
|
||||||
change_notifications: Some(OneOf::Left(true)),
|
|
||||||
}),
|
|
||||||
file_operations: None,
|
|
||||||
}),
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn initialized(&self, _: InitializedParams) {
|
async fn initialized(&self, _: InitializedParams) {
|
||||||
self.client
|
self.client
|
||||||
.log_message(MessageType::INFO, "Reid Language Server initialized hello!")
|
.log_message(MessageType::INFO, "Reid Language Server initialized!")
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,20 +121,64 @@ impl LanguageServer for Backend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn completion(&self, params: CompletionParams) -> jsonrpc::Result<Option<CompletionResponse>> {
|
async fn completion(&self, params: CompletionParams) -> jsonrpc::Result<Option<CompletionResponse>> {
|
||||||
Ok(Some(CompletionResponse::Array(vec![
|
let path = PathBuf::from(params.text_document_position.text_document.uri.path());
|
||||||
CompletionItem::new_simple("Hello".to_string(), "Some detail".to_string()),
|
let analysis = self.analysis.get(&path);
|
||||||
CompletionItem::new_simple("Bye".to_string(), "More detail".to_string()),
|
let position = params.text_document_position.position;
|
||||||
])))
|
|
||||||
|
let token = if let Some(analysis) = &analysis {
|
||||||
|
analysis.tokens.iter().enumerate().find(|(_, tok)| {
|
||||||
|
tok.position.1 == position.line + 1
|
||||||
|
&& (tok.position.0 <= position.character
|
||||||
|
&& (tok.position.0 + tok.token.len() as u32) > position.character)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
// dbg!(position, token);
|
||||||
|
|
||||||
|
let list = if let Some((idx, _)) = token {
|
||||||
|
if let Some(token_analysis) = self.analysis.get(&path).unwrap().state.map.get(&idx) {
|
||||||
|
token_analysis
|
||||||
|
.autocomplete
|
||||||
|
.iter()
|
||||||
|
.map(|autocomplete| {
|
||||||
|
let mut item =
|
||||||
|
CompletionItem::new_simple(autocomplete.text.to_string(), autocomplete.kind.to_string());
|
||||||
|
item.data = Some(
|
||||||
|
serde_json::to_value(CompletionData {
|
||||||
|
token_idx: idx,
|
||||||
|
path: path.clone(),
|
||||||
|
})
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
item.documentation = autocomplete.documentation.as_ref().and_then(|d| {
|
||||||
|
Some(lsp_types::Documentation::MarkupContent(MarkupContent {
|
||||||
|
kind: MarkupKind::Markdown,
|
||||||
|
value: d.clone(),
|
||||||
|
}))
|
||||||
|
});
|
||||||
|
item
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
};
|
||||||
|
|
||||||
|
// dbg!(&list);
|
||||||
|
Ok(Some(CompletionResponse::Array(list)))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn hover(&self, params: HoverParams) -> jsonrpc::Result<Option<Hover>> {
|
async fn hover(&self, params: HoverParams) -> jsonrpc::Result<Option<Hover>> {
|
||||||
let path = PathBuf::from(params.text_document_position_params.text_document.uri.path());
|
let path = PathBuf::from(params.text_document_position_params.text_document.uri.path());
|
||||||
let file_name = path.file_name().unwrap().to_str().unwrap().to_owned();
|
let analysis = self.analysis.get(&path);
|
||||||
let tokens = self.tokens.get(&file_name);
|
|
||||||
let position = params.text_document_position_params.position;
|
let position = params.text_document_position_params.position;
|
||||||
|
|
||||||
let token = if let Some(tokens) = &tokens {
|
let token = if let Some(analysis) = &analysis {
|
||||||
tokens.iter().find(|tok| {
|
analysis.tokens.iter().enumerate().find(|(_, tok)| {
|
||||||
tok.position.1 == position.line + 1
|
tok.position.1 == position.line + 1
|
||||||
&& (tok.position.0 <= position.character + 1
|
&& (tok.position.0 <= position.character + 1
|
||||||
&& (tok.position.0 + tok.token.len() as u32) > position.character + 1)
|
&& (tok.position.0 + tok.token.len() as u32) > position.character + 1)
|
||||||
@ -91,8 +187,8 @@ impl LanguageServer for Backend {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let (range, ty) = if let Some(token) = token {
|
let (range, ty, documentation) = if let Some((idx, token)) = token {
|
||||||
if let Some(possible_ty) = self.types.get(&file_name).unwrap().get(token) {
|
if let Some(analysis) = self.analysis.get(&path).unwrap().state.map.get(&idx) {
|
||||||
let start = token.position;
|
let start = token.position;
|
||||||
let end = token.position.add(token.token.len() as u32);
|
let end = token.position.add(token.token.len() as u32);
|
||||||
let range = Range {
|
let range = Range {
|
||||||
@ -105,22 +201,50 @@ impl LanguageServer for Backend {
|
|||||||
character: (end.0 as i32 - 1).max(0) as u32,
|
character: (end.0 as i32 - 1).max(0) as u32,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
if let Some(ty) = possible_ty.clone() {
|
if let Some(kind) = &analysis.hover.kind {
|
||||||
(Some(range), format!("{}", ty))
|
match kind {
|
||||||
|
analysis::HoverKind::Type(type_kind) => (
|
||||||
|
Some(range),
|
||||||
|
format!("{}", type_kind),
|
||||||
|
analysis.hover.documentation.clone(),
|
||||||
|
),
|
||||||
|
analysis::HoverKind::Function(name, function_params, return_type) => (
|
||||||
|
Some(range),
|
||||||
|
format!(
|
||||||
|
"{}({}) -> {}",
|
||||||
|
name,
|
||||||
|
function_params
|
||||||
|
.iter()
|
||||||
|
.map(|p| format!("{}: {}", p.name, p.ty))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", "),
|
||||||
|
return_type
|
||||||
|
),
|
||||||
|
analysis.hover.documentation.clone(),
|
||||||
|
),
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
(Some(range), String::from("no type"))
|
(
|
||||||
|
Some(range),
|
||||||
|
String::from("No type"),
|
||||||
|
analysis.hover.documentation.clone(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
(None, String::from("no token"))
|
(None, String::from("no type"), None)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
(None, String::from("no token"))
|
(None, String::from("no token"), None)
|
||||||
};
|
};
|
||||||
|
|
||||||
let contents = HoverContents::Markup(MarkupContent {
|
let contents = if let Some(doc) = documentation {
|
||||||
kind: MarkupKind::Markdown,
|
HoverContents::Array(vec![MarkedString::String(doc), MarkedString::String(format!("`{ty}`"))])
|
||||||
value: format!("`{ty}`"),
|
} else {
|
||||||
});
|
HoverContents::Markup(MarkupContent {
|
||||||
|
kind: MarkupKind::Markdown,
|
||||||
|
value: format!("`{ty}`"),
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
Ok(Some(Hover { contents, range }))
|
Ok(Some(Hover { contents, range }))
|
||||||
}
|
}
|
||||||
@ -144,60 +268,270 @@ impl LanguageServer for Backend {
|
|||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn did_save(&self, params: DidSaveTextDocumentParams) {
|
||||||
|
self.recompile(TextDocumentItem {
|
||||||
|
text: params.text.unwrap(),
|
||||||
|
uri: params.text_document.uri,
|
||||||
|
version: 0,
|
||||||
|
language_id: String::new(),
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn semantic_tokens_full(
|
||||||
|
&self,
|
||||||
|
params: SemanticTokensParams,
|
||||||
|
) -> jsonrpc::Result<Option<SemanticTokensResult>> {
|
||||||
|
let path = PathBuf::from(params.text_document.uri.path());
|
||||||
|
let analysis = self.analysis.get(&path);
|
||||||
|
|
||||||
|
let mut semantic_tokens = Vec::new();
|
||||||
|
if let Some(analysis) = analysis {
|
||||||
|
let mut prev_line = 0;
|
||||||
|
let mut prev_start = 0;
|
||||||
|
for (i, token) in analysis.tokens.iter().enumerate() {
|
||||||
|
let vscode_line = token.position.1.max(1) - 1;
|
||||||
|
let vscode_col = token.position.0.max(1) - 1;
|
||||||
|
|
||||||
|
let delta_line = vscode_line - prev_line;
|
||||||
|
let delta_start = if delta_line == 0 {
|
||||||
|
vscode_col - prev_start
|
||||||
|
} else {
|
||||||
|
vscode_col
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(token_analysis) = analysis.state.map.get(&i) {
|
||||||
|
if let Some(symbol_id) = token_analysis.symbol {
|
||||||
|
let symbol = analysis.state.get_local_symbol(symbol_id);
|
||||||
|
if let Some(idx) = symbol.kind.into_token_idx(&self.state_map()) {
|
||||||
|
let semantic_token = SemanticToken {
|
||||||
|
delta_line,
|
||||||
|
delta_start,
|
||||||
|
length: token.token.len() as u32,
|
||||||
|
token_type: idx,
|
||||||
|
token_modifiers_bitset: symbol.kind.get_modifier().unwrap_or(0),
|
||||||
|
};
|
||||||
|
semantic_tokens.push(semantic_token);
|
||||||
|
prev_line = vscode_line;
|
||||||
|
prev_start = vscode_col;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Some(SemanticTokensResult::Tokens(lsp_types::SemanticTokens {
|
||||||
|
result_id: None,
|
||||||
|
data: semantic_tokens,
|
||||||
|
})))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn goto_definition(&self, params: GotoDefinitionParams) -> jsonrpc::Result<Option<GotoDefinitionResponse>> {
|
||||||
|
let path = PathBuf::from(params.text_document_position_params.text_document.uri.path());
|
||||||
|
let analysis = self.analysis.get(&path);
|
||||||
|
let position = params.text_document_position_params.position;
|
||||||
|
|
||||||
|
if let Some(analysis) = &analysis {
|
||||||
|
let token = analysis.tokens.iter().enumerate().find(|(_, tok)| {
|
||||||
|
tok.position.1 == position.line + 1
|
||||||
|
&& (tok.position.0 <= position.character + 1
|
||||||
|
&& (tok.position.0 + tok.token.len() as u32) > position.character + 1)
|
||||||
|
});
|
||||||
|
|
||||||
|
if let Some(token) = token {
|
||||||
|
if let Some((module_id, def_token)) = analysis.find_definition(token.0, &self.state_map()) {
|
||||||
|
return if let Some(path) = self.module_to_path.get(&module_id) {
|
||||||
|
Ok(Some(GotoDefinitionResponse::Scalar(lsp_types::Location {
|
||||||
|
uri: Url::from_file_path(path.value()).unwrap(),
|
||||||
|
range: token_to_range(def_token),
|
||||||
|
})))
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn references(&self, params: ReferenceParams) -> jsonrpc::Result<Option<Vec<Location>>> {
|
||||||
|
let path = PathBuf::from(params.text_document_position.text_document.uri.path());
|
||||||
|
let analysis = self.analysis.get(&path);
|
||||||
|
let position = params.text_document_position.position;
|
||||||
|
|
||||||
|
if let Some(analysis) = &analysis {
|
||||||
|
let token = analysis.tokens.iter().enumerate().find(|(_, tok)| {
|
||||||
|
tok.position.1 == position.line + 1
|
||||||
|
&& (tok.position.0 <= position.character + 1
|
||||||
|
&& (tok.position.0 + tok.token.len() as u32) > position.character + 1)
|
||||||
|
});
|
||||||
|
if let Some(token) = token {
|
||||||
|
let reference_tokens = analysis.find_references(token.0, &self.state_map());
|
||||||
|
let mut locations = Vec::new();
|
||||||
|
if let Some(reference_tokens) = reference_tokens {
|
||||||
|
for (module_id, symbol_idx) in reference_tokens {
|
||||||
|
if let Some(path) = self.module_to_path.get(&module_id) {
|
||||||
|
let url = Url::from_file_path(path.value()).unwrap();
|
||||||
|
if let Some(inner_analysis) = self.analysis.get(path.value()) {
|
||||||
|
if let Some(token_idx) = inner_analysis.state.symbol_to_token.get(&symbol_idx) {
|
||||||
|
let token = inner_analysis.tokens.get(*token_idx).unwrap();
|
||||||
|
|
||||||
|
locations.push(lsp_types::Location {
|
||||||
|
uri: url,
|
||||||
|
range: token_to_range(token),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Some(locations))
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn rename(&self, params: RenameParams) -> jsonrpc::Result<Option<WorkspaceEdit>> {
|
||||||
|
let path = PathBuf::from(params.text_document_position.text_document.uri.path());
|
||||||
|
let analysis = self.analysis.get(&path);
|
||||||
|
let position = params.text_document_position.position;
|
||||||
|
|
||||||
|
if let Some(analysis) = &analysis {
|
||||||
|
let token = analysis.tokens.iter().enumerate().find(|(_, tok)| {
|
||||||
|
tok.position.1 == position.line + 1
|
||||||
|
&& (tok.position.0 <= position.character + 1
|
||||||
|
&& (tok.position.0 + tok.token.len() as u32) > position.character + 1)
|
||||||
|
});
|
||||||
|
if let Some(token) = token {
|
||||||
|
let symbols = analysis.find_references(token.0, &self.state_map());
|
||||||
|
let mut changes: HashMap<Url, Vec<TextEdit>> = HashMap::new();
|
||||||
|
if let Some(symbols) = symbols {
|
||||||
|
for (module_id, symbol_id) in symbols {
|
||||||
|
let path = self.module_to_path.get(&module_id);
|
||||||
|
if let Some(path) = path {
|
||||||
|
let url = Url::from_file_path(path.value()).unwrap();
|
||||||
|
let analysis = self.analysis.get(&path.clone());
|
||||||
|
|
||||||
|
if let Some(analysis) = analysis {
|
||||||
|
if let Some(token_idx) = analysis.state.symbol_to_token.get(&symbol_id) {
|
||||||
|
let token = analysis.tokens.get(*token_idx).unwrap();
|
||||||
|
|
||||||
|
// edits = changes.get(k)
|
||||||
|
let edit = TextEdit {
|
||||||
|
range: token_to_range(token),
|
||||||
|
new_text: params.new_name.clone(),
|
||||||
|
};
|
||||||
|
if let Some(edits) = changes.get_mut(&url) {
|
||||||
|
edits.push(edit);
|
||||||
|
} else {
|
||||||
|
changes.insert(url, vec![edit]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Some(WorkspaceEdit {
|
||||||
|
changes: Some(changes),
|
||||||
|
document_changes: None,
|
||||||
|
change_annotations: None,
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn token_to_range(token: &FullToken) -> lsp_types::Range {
|
||||||
|
Range {
|
||||||
|
start: lsp_types::Position {
|
||||||
|
line: token.position.1.max(1) - 1,
|
||||||
|
character: token.position.0.max(1) - 1,
|
||||||
|
},
|
||||||
|
end: lsp_types::Position {
|
||||||
|
line: token.position.1.max(1) - 1,
|
||||||
|
character: token.position.0.max(1) - 1 + token.token.len() as u32,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Backend {
|
impl Backend {
|
||||||
async fn recompile(&self, params: TextDocumentItem) {
|
fn state_map(&self) -> StateMap {
|
||||||
let path = PathBuf::from(params.uri.clone().path());
|
let mut state_map = HashMap::new();
|
||||||
let file_name = path.file_name().unwrap().to_str().unwrap().to_owned();
|
for path_state in self.analysis.iter() {
|
||||||
|
let (path, state) = path_state.pair();
|
||||||
|
if let Some(module_id) = self.path_to_module.get(path) {
|
||||||
|
state_map.insert(*module_id, state.state.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state_map
|
||||||
|
}
|
||||||
|
|
||||||
let mut map = Default::default();
|
async fn recompile(&self, params: TextDocumentItem) {
|
||||||
let parse_res = parse(¶ms.text, path.clone(), &mut map);
|
let file_path = PathBuf::from(params.uri.clone().path());
|
||||||
|
|
||||||
|
let mut map: ErrorModules = Default::default();
|
||||||
|
for url_module in self.path_to_module.iter() {
|
||||||
|
let (url, module) = url_module.pair();
|
||||||
|
map.add_module(
|
||||||
|
url.file_name().unwrap().to_str().unwrap().to_owned(),
|
||||||
|
Some(url.clone()),
|
||||||
|
Some(*module),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let module_id = if let Some(module_id) = self.path_to_module.get(&file_path) {
|
||||||
|
*module_id
|
||||||
|
} else {
|
||||||
|
let mut lock = self.module_id_counter.lock().await;
|
||||||
|
let module_id = lock.increment();
|
||||||
|
drop(lock);
|
||||||
|
self.path_to_module.insert(file_path.clone(), module_id);
|
||||||
|
self.module_to_path.insert(module_id, file_path.clone());
|
||||||
|
module_id
|
||||||
|
};
|
||||||
|
|
||||||
|
let parse_res = parse(¶ms.text, file_path.clone(), &mut map, module_id);
|
||||||
let (tokens, result) = match parse_res {
|
let (tokens, result) = match parse_res {
|
||||||
Ok((module_id, tokens)) => (tokens.clone(), compile(module_id, tokens, path, &mut map)),
|
Ok((module_id, tokens)) => (
|
||||||
|
tokens.clone(),
|
||||||
|
analyze(module_id, tokens, file_path.clone(), &mut map, &self.state_map()),
|
||||||
|
),
|
||||||
Err(e) => (Vec::new(), Err(e)),
|
Err(e) => (Vec::new(), Err(e)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut diagnostics = Vec::new();
|
let mut diagnostics = Vec::new();
|
||||||
match result {
|
match result {
|
||||||
Ok(Some(result)) => {
|
Ok(Some(mut analysis)) => {
|
||||||
self.tokens.insert(file_name.clone(), result.tokens);
|
if let Some(reid_error) = &mut analysis.error {
|
||||||
self.types.insert(file_name.clone(), result.types);
|
self.client
|
||||||
|
.log_message(
|
||||||
|
MessageType::INFO,
|
||||||
|
format!("Successfully compiled despite parsing errors!"),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
reid_error.errors.dedup();
|
||||||
|
for error in &reid_error.errors {
|
||||||
|
diagnostics.push(reid_error_into_diagnostic(error, &tokens));
|
||||||
|
self.client.log_message(MessageType::INFO, format!("{}", error)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.analysis.insert(file_path, analysis);
|
||||||
}
|
}
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(mut reid_error) => {
|
Err(mut reid_error) => {
|
||||||
reid_error.errors.dedup();
|
reid_error.errors.dedup();
|
||||||
for error in reid_error.errors {
|
for error in &reid_error.errors {
|
||||||
let meta = error.get_meta();
|
diagnostics.push(reid_error_into_diagnostic(error, &tokens));
|
||||||
let positions = meta
|
|
||||||
.range
|
|
||||||
.into_position(&tokens)
|
|
||||||
.unwrap_or((Position(0, 0), Position(0, 0)));
|
|
||||||
self.client.log_message(MessageType::INFO, format!("{:?}", &meta)).await;
|
|
||||||
self.client
|
|
||||||
.log_message(MessageType::INFO, format!("{:?}", &positions))
|
|
||||||
.await;
|
|
||||||
|
|
||||||
diagnostics.push(Diagnostic {
|
|
||||||
range: Range {
|
|
||||||
start: lsp_types::Position {
|
|
||||||
line: ((positions.0.1 as i32) - 1).max(0) as u32,
|
|
||||||
character: ((positions.0.0 as i32) - 1).max(0) as u32,
|
|
||||||
},
|
|
||||||
end: lsp_types::Position {
|
|
||||||
line: ((positions.1.1 as i32) - 1).max(0) as u32,
|
|
||||||
character: ((positions.1.0 as i32) - 1).max(0) as u32,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
severity: Some(DiagnosticSeverity::ERROR),
|
|
||||||
code: None,
|
|
||||||
code_description: None,
|
|
||||||
source: Some(error.get_type_str().to_owned()),
|
|
||||||
message: format!("{}", error),
|
|
||||||
related_information: None,
|
|
||||||
tags: None,
|
|
||||||
data: None,
|
|
||||||
});
|
|
||||||
self.client.log_message(MessageType::INFO, format!("{}", error)).await;
|
self.client.log_message(MessageType::INFO, format!("{}", error)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -209,45 +543,50 @@ impl Backend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CompileResult {
|
fn reid_error_into_diagnostic(error: &error_raporting::ErrorKind, tokens: &Vec<FullToken>) -> Diagnostic {
|
||||||
tokens: Vec<FullToken>,
|
let meta = error.get_meta();
|
||||||
types: DashMap<FullToken, Option<TypeKind>>,
|
let positions = meta
|
||||||
|
.range
|
||||||
|
.into_position(&tokens)
|
||||||
|
.unwrap_or((Position(0, 0), Position(0, 0)));
|
||||||
|
|
||||||
|
Diagnostic {
|
||||||
|
range: Range {
|
||||||
|
start: lsp_types::Position {
|
||||||
|
line: ((positions.0.1 as i32) - 1).max(0) as u32,
|
||||||
|
character: ((positions.0.0 as i32) - 1).max(0) as u32,
|
||||||
|
},
|
||||||
|
end: lsp_types::Position {
|
||||||
|
line: ((positions.1.1 as i32) - 1).max(0) as u32,
|
||||||
|
character: ((positions.1.0 as i32) - 1).max(0) as u32,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
severity: Some(DiagnosticSeverity::ERROR),
|
||||||
|
code: None,
|
||||||
|
code_description: None,
|
||||||
|
source: Some(error.get_type_str().to_owned()),
|
||||||
|
message: format!("{}", error),
|
||||||
|
related_information: None,
|
||||||
|
tags: None,
|
||||||
|
data: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse(source: &str, path: PathBuf, map: &mut ErrorModules) -> Result<(SourceModuleId, Vec<FullToken>), ReidError> {
|
fn parse(
|
||||||
let file_name = path.file_name().unwrap().to_str().unwrap().to_owned();
|
source: &str,
|
||||||
|
|
||||||
Ok(parse_module(source, file_name.clone(), map)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn compile(
|
|
||||||
module_id: SourceModuleId,
|
|
||||||
tokens: Vec<FullToken>,
|
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
map: &mut ErrorModules,
|
map: &mut ErrorModules,
|
||||||
) -> Result<Option<CompileResult>, ReidError> {
|
module_id: SourceModuleId,
|
||||||
let token_types = DashMap::new();
|
) -> Result<(SourceModuleId, Vec<FullToken>), ReidError> {
|
||||||
|
let file_name = path.file_name().unwrap().to_str().unwrap().to_owned();
|
||||||
|
|
||||||
let module = compile_module(module_id, tokens, map, Some(path.clone()), true)?;
|
Ok(parse_module(
|
||||||
|
source,
|
||||||
let module_id = module.module_id;
|
file_name.clone(),
|
||||||
let mut context = Context::from(vec![module], path.parent().unwrap().to_owned());
|
Some(path),
|
||||||
perform_all_passes(&mut context, map)?;
|
map,
|
||||||
|
Some(module_id),
|
||||||
for module in context.modules.into_values() {
|
)?)
|
||||||
if module.module_id != module_id {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
for (idx, token) in module.tokens.iter().enumerate() {
|
|
||||||
token_types.insert(token.clone(), find_type_in_context(&module, idx));
|
|
||||||
}
|
|
||||||
|
|
||||||
return Ok(Some(CompileResult {
|
|
||||||
tokens: module.tokens,
|
|
||||||
types: token_types,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
return Ok(None);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
@ -257,234 +596,10 @@ async fn main() {
|
|||||||
|
|
||||||
let (service, socket) = LspService::new(|client| Backend {
|
let (service, socket) = LspService::new(|client| Backend {
|
||||||
client,
|
client,
|
||||||
ast: DashMap::new(),
|
analysis: DashMap::new(),
|
||||||
tokens: DashMap::new(),
|
module_to_path: DashMap::new(),
|
||||||
types: DashMap::new(),
|
path_to_module: DashMap::new(),
|
||||||
|
module_id_counter: Mutex::new(SourceModuleId(0)),
|
||||||
});
|
});
|
||||||
Server::new(stdin, stdout, socket).serve(service).await;
|
Server::new(stdin, stdout, socket).serve(service).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_type_in_context(module: &mir::Module, token_idx: usize) -> Option<TypeKind> {
|
|
||||||
for import in &module.imports {
|
|
||||||
if import.1.contains(token_idx) {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for typedef in &module.typedefs {
|
|
||||||
if !typedef.meta.contains(token_idx) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
match &typedef.kind {
|
|
||||||
mir::TypeDefinitionKind::Struct(StructType(fields)) => {
|
|
||||||
for field in fields {
|
|
||||||
if field.2.contains(token_idx) {
|
|
||||||
return Some(field.1.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for binop in &module.binop_defs {
|
|
||||||
if let Some(meta) = binop.block_meta() {
|
|
||||||
if !meta.contains(token_idx) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return match &binop.fn_kind {
|
|
||||||
mir::FunctionDefinitionKind::Local(block, _) => find_type_in_block(&block, module.module_id, token_idx),
|
|
||||||
mir::FunctionDefinitionKind::Extern(_) => None,
|
|
||||||
mir::FunctionDefinitionKind::Intrinsic(_) => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
for (_, function) in &module.associated_functions {
|
|
||||||
if !(function.signature() + function.block_meta()).contains(token_idx) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for param in &function.parameters {
|
|
||||||
if param.meta.contains(token_idx) {
|
|
||||||
return Some(param.ty.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return match &function.kind {
|
|
||||||
mir::FunctionDefinitionKind::Local(block, _) => find_type_in_block(&block, module.module_id, token_idx),
|
|
||||||
mir::FunctionDefinitionKind::Extern(_) => None,
|
|
||||||
mir::FunctionDefinitionKind::Intrinsic(_) => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
for function in &module.functions {
|
|
||||||
if !(function.signature() + function.block_meta()).contains(token_idx) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for param in &function.parameters {
|
|
||||||
if param.meta.contains(token_idx) {
|
|
||||||
return Some(param.ty.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return match &function.kind {
|
|
||||||
mir::FunctionDefinitionKind::Local(block, _) => find_type_in_block(&block, module.module_id, token_idx),
|
|
||||||
mir::FunctionDefinitionKind::Extern(_) => None,
|
|
||||||
mir::FunctionDefinitionKind::Intrinsic(_) => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn find_type_in_block(block: &mir::Block, module_id: SourceModuleId, token_idx: usize) -> Option<TypeKind> {
|
|
||||||
if !block.meta.contains(token_idx) {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
for statement in &block.statements {
|
|
||||||
if !statement.1.contains(token_idx) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
match &statement.0 {
|
|
||||||
mir::StmtKind::Let(named_variable_ref, _, expression) => {
|
|
||||||
if named_variable_ref.2.contains(token_idx) {
|
|
||||||
return expression
|
|
||||||
.return_type(&Default::default(), module_id)
|
|
||||||
.ok()
|
|
||||||
.map(|(_, ty)| ty);
|
|
||||||
} else {
|
|
||||||
return find_type_in_expr(&expression, module_id, token_idx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mir::StmtKind::Set(lhs, rhs) => {
|
|
||||||
return find_type_in_expr(lhs, module_id, token_idx).or(find_type_in_expr(rhs, module_id, token_idx));
|
|
||||||
}
|
|
||||||
mir::StmtKind::Import(_) => {}
|
|
||||||
mir::StmtKind::Expression(expression) => return find_type_in_expr(expression, module_id, token_idx),
|
|
||||||
mir::StmtKind::While(WhileStatement { condition, block, .. }) => {
|
|
||||||
return find_type_in_expr(condition, module_id, token_idx)
|
|
||||||
.or(find_type_in_block(block, module_id, token_idx));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some((_, Some(return_exp))) = &block.return_expression {
|
|
||||||
if let Some(ty) = find_type_in_expr(return_exp, module_id, token_idx) {
|
|
||||||
return Some(ty);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn find_type_in_expr(expr: &mir::Expression, module_id: SourceModuleId, token_idx: usize) -> Option<TypeKind> {
|
|
||||||
if !expr.1.contains(token_idx) {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
match &expr.0 {
|
|
||||||
mir::ExprKind::Variable(named_variable_ref) => Some(named_variable_ref.0.clone()),
|
|
||||||
mir::ExprKind::Indexed(value, type_kind, index_expr) => Some(
|
|
||||||
find_type_in_expr(&value, module_id, token_idx)
|
|
||||||
.or(find_type_in_expr(&index_expr, module_id, token_idx))
|
|
||||||
.unwrap_or(type_kind.clone()),
|
|
||||||
),
|
|
||||||
mir::ExprKind::Accessed(expression, type_kind, _, meta) => {
|
|
||||||
if meta.contains(token_idx) {
|
|
||||||
Some(type_kind.clone())
|
|
||||||
} else {
|
|
||||||
find_type_in_expr(&expression, module_id, token_idx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mir::ExprKind::Array(expressions) => {
|
|
||||||
for expr in expressions {
|
|
||||||
if let Some(ty) = find_type_in_expr(expr, module_id, token_idx) {
|
|
||||||
return Some(ty);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
mir::ExprKind::Struct(name, items) => {
|
|
||||||
for (_, expr, meta) in items {
|
|
||||||
if meta.contains(token_idx) {
|
|
||||||
return expr.return_type(&Default::default(), module_id).map(|(_, t)| t).ok();
|
|
||||||
}
|
|
||||||
if let Some(ty) = find_type_in_expr(expr, module_id, token_idx) {
|
|
||||||
return Some(ty);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(TypeKind::CustomType(mir::CustomTypeKey(name.clone(), module_id)))
|
|
||||||
}
|
|
||||||
mir::ExprKind::Literal(literal) => Some(literal.as_type()),
|
|
||||||
mir::ExprKind::BinOp(_, lhs, rhs, type_kind) => {
|
|
||||||
if let Some(ty) = find_type_in_expr(lhs, module_id, token_idx) {
|
|
||||||
return Some(ty);
|
|
||||||
}
|
|
||||||
if let Some(ty) = find_type_in_expr(rhs, module_id, token_idx) {
|
|
||||||
return Some(ty);
|
|
||||||
}
|
|
||||||
Some(type_kind.clone())
|
|
||||||
}
|
|
||||||
mir::ExprKind::FunctionCall(FunctionCall {
|
|
||||||
return_type,
|
|
||||||
parameters,
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
for expr in parameters {
|
|
||||||
if let Some(ty) = find_type_in_expr(expr, module_id, token_idx) {
|
|
||||||
return Some(ty);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(return_type.clone())
|
|
||||||
}
|
|
||||||
mir::ExprKind::AssociatedFunctionCall(
|
|
||||||
_,
|
|
||||||
FunctionCall {
|
|
||||||
return_type,
|
|
||||||
parameters,
|
|
||||||
..
|
|
||||||
},
|
|
||||||
) => {
|
|
||||||
for expr in parameters {
|
|
||||||
if let Some(ty) = find_type_in_expr(expr, module_id, token_idx) {
|
|
||||||
return Some(ty);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(return_type.clone())
|
|
||||||
}
|
|
||||||
mir::ExprKind::If(IfExpression(cond, then_e, else_e)) => find_type_in_expr(&cond, module_id, token_idx)
|
|
||||||
.or(find_type_in_expr(&then_e, module_id, token_idx))
|
|
||||||
.or(else_e.clone().and_then(|e| find_type_in_expr(&e, module_id, token_idx))),
|
|
||||||
mir::ExprKind::Block(block) => find_type_in_block(block, module_id, token_idx),
|
|
||||||
mir::ExprKind::Borrow(expression, mutable) => {
|
|
||||||
if let Some(ty) = find_type_in_expr(&expression, module_id, token_idx) {
|
|
||||||
return Some(ty);
|
|
||||||
}
|
|
||||||
if let Ok(inner) = expression.return_type(&Default::default(), module_id).map(|(_, ty)| ty) {
|
|
||||||
Some(TypeKind::Borrow(Box::new(inner.clone()), *mutable))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mir::ExprKind::Deref(expression) => {
|
|
||||||
if let Some(ty) = find_type_in_expr(&expression, module_id, token_idx) {
|
|
||||||
return Some(ty);
|
|
||||||
}
|
|
||||||
if let Ok(TypeKind::Borrow(inner, _)) =
|
|
||||||
expression.return_type(&Default::default(), module_id).map(|(_, ty)| ty)
|
|
||||||
{
|
|
||||||
Some(*inner.clone())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mir::ExprKind::CastTo(expression, type_kind) => {
|
|
||||||
Some(find_type_in_expr(&expression, module_id, token_idx).unwrap_or(type_kind.clone()))
|
|
||||||
}
|
|
||||||
mir::ExprKind::GlobalRef(_, type_kind) => Some(type_kind.clone()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,56 +1,11 @@
|
|||||||
{
|
{
|
||||||
"scopeName": "source.reid",
|
"scopeName": "source.reid",
|
||||||
"patterns": [
|
"patterns": [
|
||||||
{
|
|
||||||
"include": "#import"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"include": "#expression"
|
"include": "#expression"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"repository": {
|
"repository": {
|
||||||
"import": {
|
|
||||||
"begin": "(import)\\s*",
|
|
||||||
"end": ";",
|
|
||||||
"beginCaptures": {
|
|
||||||
"1": {
|
|
||||||
"name": "keyword"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"endCaptures": {
|
|
||||||
"0": {
|
|
||||||
"name": "punctuation.semi.reid"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#identifier"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#punctiation"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"punctuation": {
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"match": "::",
|
|
||||||
"name": "keyword.operator.namespace.reid"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match": ";",
|
|
||||||
"name": "punctuation.semi.reid"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match": ".",
|
|
||||||
"name": "punctuation.dot.reid"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match": ",",
|
|
||||||
"name": "punctuation.comma.reid"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"expression": {
|
"expression": {
|
||||||
"patterns": [
|
"patterns": [
|
||||||
{
|
{
|
||||||
@ -59,27 +14,18 @@
|
|||||||
{
|
{
|
||||||
"include": "#fn-signature"
|
"include": "#fn-signature"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"include": "#namespace"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"include": "#common-type"
|
"include": "#common-type"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"include": "#binop-impl"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#type-impl"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"include": "#struct-definition"
|
"include": "#struct-definition"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"include": "#block"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"include": "#binop"
|
"include": "#binop"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"include": "#namespace"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"include": "#cast"
|
"include": "#cast"
|
||||||
},
|
},
|
||||||
@ -95,9 +41,6 @@
|
|||||||
{
|
{
|
||||||
"include": "#keywords"
|
"include": "#keywords"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"include": "#struct-expression"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"include": "#number-literal"
|
"include": "#number-literal"
|
||||||
},
|
},
|
||||||
@ -112,93 +55,44 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
"punctuation": {
|
||||||
|
"patterns": [
|
||||||
|
{
|
||||||
|
"match": "::",
|
||||||
|
"name": "keyword.operator.namespace.reid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"match": ":",
|
||||||
|
"name": "keyword.operator.colon.reid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"match": ";",
|
||||||
|
"name": "punctuation.semi.reid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"match": ".",
|
||||||
|
"name": "punctuation.dot.reid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"match": ",",
|
||||||
|
"name": "punctuation.comma.reid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"match": "\\{|\\}",
|
||||||
|
"name": "punctuation.brackets.curly.reid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"match": "\\(|\\)",
|
||||||
|
"name": "punctuation.parenthesis.reid"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
"comment": {
|
"comment": {
|
||||||
"match": "\\/\\/(.|\\/)*",
|
"match": "\\/\\/(.|\\/)*",
|
||||||
"name": "comment.line.double-slash.reid"
|
"name": "comment.line.double-slash.reid"
|
||||||
},
|
},
|
||||||
"fn-signature": {
|
|
||||||
"begin": "(fn)\\s*(\\w+)\\(",
|
|
||||||
"beginCaptures": {
|
|
||||||
"1": {
|
|
||||||
"name": "keyword.fn.reid"
|
|
||||||
},
|
|
||||||
"2": {
|
|
||||||
"name": "entity.name.function.reid"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"end": "\\)",
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#annotated-identifier"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#keywords"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"include": "#binop"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"endCaptures": {
|
|
||||||
"2": {
|
|
||||||
"name": "entity.name.type.reid"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"type-impl": {
|
|
||||||
"begin": "(impl)\\s* (\\w+)\\s* \\{\n",
|
|
||||||
"end": "\\}",
|
|
||||||
"captures": {
|
|
||||||
"1": {
|
|
||||||
"name": "keyword.impl.reid"
|
|
||||||
},
|
|
||||||
"2": {
|
|
||||||
"name": "entity.name.type"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#expression"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"binop-impl": {
|
|
||||||
"begin": "(impl)\\s+(binop)\\s+\\(((.*)\\s*:\\s*(.*))\\)(.*)\\(((.*)\\s*:\\s*(.*))\\)\\s*->\\s*(\\w+)\\s*\\{",
|
|
||||||
"end": "\\}",
|
|
||||||
"beginCaptures": {
|
|
||||||
"1": {
|
|
||||||
"name": "keyword.impl.reid"
|
|
||||||
},
|
|
||||||
"2": {
|
|
||||||
"name": "keyword.impl.reid"
|
|
||||||
},
|
|
||||||
"4": {
|
|
||||||
"name": "variable.parameter.binop.reid"
|
|
||||||
},
|
|
||||||
"5": {
|
|
||||||
"name": "entity.name.type.parameter.binop.reid"
|
|
||||||
},
|
|
||||||
"6": {
|
|
||||||
"name": "keyword.operator.math.reid"
|
|
||||||
},
|
|
||||||
"8": {
|
|
||||||
"name": "variable.parameter.binop.reid"
|
|
||||||
},
|
|
||||||
"9": {
|
|
||||||
"name": "entity.name.type.parameter.binop.reid"
|
|
||||||
},
|
|
||||||
"10": {
|
|
||||||
"name": "entity.name.type.return.binop.reid"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#expression"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"struct-definition": {
|
"struct-definition": {
|
||||||
"begin": "(struct)\\s*(\\w+)\\s*\\{",
|
"match": "(struct)\\s*(\\w+)",
|
||||||
"end": "\\}",
|
|
||||||
"captures": {
|
"captures": {
|
||||||
"1": {
|
"1": {
|
||||||
"name": "keyword.struct.reid"
|
"name": "keyword.struct.reid"
|
||||||
@ -206,15 +100,10 @@
|
|||||||
"2": {
|
"2": {
|
||||||
"name": "entity.name.type"
|
"name": "entity.name.type"
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#annotated-identifier"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"struct-expression": {
|
"struct-expression": {
|
||||||
"begin": "([A-Z]\\w*)\\s*\\{",
|
"begin": "\\b([A-Z]\\w*)\\s*\\{",
|
||||||
"end": "\\}",
|
"end": "\\}",
|
||||||
"captures": {
|
"captures": {
|
||||||
"1": {
|
"1": {
|
||||||
@ -230,19 +119,19 @@
|
|||||||
"number-literal": {
|
"number-literal": {
|
||||||
"patterns": [
|
"patterns": [
|
||||||
{
|
{
|
||||||
"match": "0x[0-9a-fA-F]+(\\.[0-9a-fA-F]+)?",
|
"match": "\\b0x[0-9a-fA-F]+(\\.[0-9a-fA-F]+)?\\b",
|
||||||
"name": "constant.hexadecimal"
|
"name": "constant.hexadecimal"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"match": "0o[0-7]+(\\.[0-7]+)?",
|
"match": "\\b0o[0-7]+(\\.[0-7]+)?\\b",
|
||||||
"name": "constant.octal"
|
"name": "constant.octal"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"match": "0b[01]+(\\.[01]+)?",
|
"match": "\\b0b[01]+(\\.[01]+)?\\b",
|
||||||
"name": "constant.binary"
|
"name": "constant.binary"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"match": "[0-9]+(\\.[0-9]+)?",
|
"match": "\\b[0-9]+(\\.[0-9]+)?\\b",
|
||||||
"name": "constant.numeric"
|
"name": "constant.numeric"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -253,25 +142,16 @@
|
|||||||
"name": "string.quoted.double",
|
"name": "string.quoted.double",
|
||||||
"patterns": [
|
"patterns": [
|
||||||
{
|
{
|
||||||
"match": "\\.",
|
"match": "\\\\\\w",
|
||||||
"name": "constant.character.escape"
|
"name": "constant.character.escape"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"block": {
|
|
||||||
"begin": "\\{",
|
|
||||||
"end": "\\}",
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#expression"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"namespace": {
|
"namespace": {
|
||||||
"match": "(\\w+)(\\:\\:)",
|
"match": "(\\w+)(\\:\\:)",
|
||||||
"captures": {
|
"captures": {
|
||||||
"1": {
|
"1": {
|
||||||
"name": "entity.name.function.reid"
|
"name": "entity.name.namespace.reid"
|
||||||
},
|
},
|
||||||
"2": {
|
"2": {
|
||||||
"name": "keyword.operator.namespace.reid"
|
"name": "keyword.operator.namespace.reid"
|
||||||
@ -290,11 +170,19 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"function-call": {
|
"function-call": {
|
||||||
"begin": "(\\w+)?\\(",
|
"begin": "(\\w+)?(\\()",
|
||||||
"end": "\\)",
|
"end": "(\\))",
|
||||||
"beginCaptures": {
|
"beginCaptures": {
|
||||||
"1": {
|
"1": {
|
||||||
"name": "entity.name.function.reid"
|
"name": "entity.name.function.reid"
|
||||||
|
},
|
||||||
|
"2": {
|
||||||
|
"name": "punctuation.parenthesis.reid"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"endCaptures": {
|
||||||
|
"1": {
|
||||||
|
"name": "punctuation.parenthesis.reid"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"patterns": [
|
"patterns": [
|
||||||
@ -322,28 +210,10 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"annotated-identifier": {
|
|
||||||
"begin": "(\\w+)\\:",
|
|
||||||
"end": ",",
|
|
||||||
"beginCaptures": {
|
|
||||||
"1": {
|
|
||||||
"name": "variable.language.reid"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"patterns": [
|
|
||||||
{
|
|
||||||
"include": "#expression"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"identifier": {
|
"identifier": {
|
||||||
"patterns": [
|
"patterns": [
|
||||||
{
|
{
|
||||||
"match": "[A-Z]\\w*",
|
"match": "\\b(?:\\w+)\\b",
|
||||||
"name": "entity.name.type.reid"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"match": "\\w+",
|
|
||||||
"name": "variable.language.reid"
|
"name": "variable.language.reid"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -351,16 +221,32 @@
|
|||||||
"keywords": {
|
"keywords": {
|
||||||
"patterns": [
|
"patterns": [
|
||||||
{
|
{
|
||||||
"match": "let|mut|pub|extern",
|
"match": "\\b(?:let|mut|pub|extern)\\b",
|
||||||
"name": "storage.type.reid"
|
"name": "storage.type.reid"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"match": "if|return",
|
"match": "\\bimport\\b",
|
||||||
|
"name": "keyword.import.reid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"match": "\\bbinop\\b",
|
||||||
|
"name": "keyword.binop.reid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"match": "\\bimpl\\b",
|
||||||
|
"name": "keyword.impl.reid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"match": "\\b(?:if|return|for|in)\\b",
|
||||||
"name": "keyword.control"
|
"name": "keyword.control"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"match": "self",
|
"match": "\\bself\\b",
|
||||||
"name": "variable.language.self.reid"
|
"name": "variable.language.self.reid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"match": "\\bfn\\b",
|
||||||
|
"name": "keyword.fn.reid"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -388,7 +274,7 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"common-type": {
|
"common-type": {
|
||||||
"match": "u8|u16|u32|u64|u128|i8|i16|i32|i64|i128|bool",
|
"match": "\\b(?:u8|u16|u32|u64|u128|i8|i16|i32|i64|i128|f16|f16b|f32|f64|f80|f128|f128ppc|bool|char|([A-Z]\\w*))\\b",
|
||||||
"name": "entity.name.type.common.reid"
|
"name": "entity.name.type.common.reid"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,133 +1,52 @@
|
|||||||
scopeName: source.reid
|
scopeName: source.reid
|
||||||
patterns:
|
patterns:
|
||||||
- include: "#import"
|
|
||||||
- include: "#expression"
|
- include: "#expression"
|
||||||
repository:
|
repository:
|
||||||
# function-definition:
|
expression:
|
||||||
# begin: "(fn)\\s*(\\w+)\\(((\\w+)\\s*\\:\\s*(\\w+),?)*\\)\\s*->\\s*(\\w+)\\s*\\{"
|
|
||||||
# end: "\\}"
|
|
||||||
# beginCaptures:
|
|
||||||
# 1:
|
|
||||||
# name: "keyword.other"
|
|
||||||
# 2:
|
|
||||||
# name: "entity.name.function"
|
|
||||||
# 4:
|
|
||||||
# name: "entity.name.parameter"
|
|
||||||
# 5:
|
|
||||||
# name: "entity.name.type"
|
|
||||||
# 6:
|
|
||||||
# name: "entity.name.type"
|
|
||||||
# patterns:
|
|
||||||
# - include: "#type"
|
|
||||||
# - include: "#expression"
|
|
||||||
import:
|
|
||||||
begin: "(import)\\s*"
|
|
||||||
end: ";"
|
|
||||||
beginCaptures:
|
|
||||||
1:
|
|
||||||
name: keyword
|
|
||||||
endCaptures:
|
|
||||||
0:
|
|
||||||
name: punctuation.semi.reid
|
|
||||||
patterns:
|
patterns:
|
||||||
|
- include: "#comment"
|
||||||
|
- include: "#fn-signature"
|
||||||
|
- include: "#namespace"
|
||||||
|
- include: "#common-type"
|
||||||
|
- include: "#struct-definition"
|
||||||
|
- include: "#binop"
|
||||||
|
- include: "#cast"
|
||||||
|
- include: "#function-call"
|
||||||
|
- include: "#parenthesis"
|
||||||
|
- include: "#array"
|
||||||
|
- include: "#keywords"
|
||||||
|
- include: "#number-literal"
|
||||||
|
- include: "#string-literal"
|
||||||
- include: "#identifier"
|
- include: "#identifier"
|
||||||
- include: "#punctiation"
|
- include: "#punctuation"
|
||||||
punctuation:
|
punctuation:
|
||||||
patterns:
|
patterns:
|
||||||
- match: "::"
|
- match: "::"
|
||||||
name: keyword.operator.namespace.reid
|
name: keyword.operator.namespace.reid
|
||||||
|
- match: ":"
|
||||||
|
name: keyword.operator.colon.reid
|
||||||
- match: ";"
|
- match: ";"
|
||||||
name: punctuation.semi.reid
|
name: punctuation.semi.reid
|
||||||
- match: "."
|
- match: "."
|
||||||
name: punctuation.dot.reid
|
name: punctuation.dot.reid
|
||||||
- match: ","
|
- match: ","
|
||||||
name: punctuation.comma.reid
|
name: punctuation.comma.reid
|
||||||
expression:
|
- match: "\\{|\\}"
|
||||||
patterns:
|
name: punctuation.brackets.curly.reid
|
||||||
- include: "#comment"
|
- match: "\\(|\\)"
|
||||||
- include: "#fn-signature"
|
name: punctuation.parenthesis.reid
|
||||||
- include: "#common-type"
|
|
||||||
- include: "#binop-impl"
|
|
||||||
- include: "#type-impl"
|
|
||||||
- include: "#struct-definition"
|
|
||||||
- include: "#block"
|
|
||||||
- include: "#binop"
|
|
||||||
- include: "#namespace"
|
|
||||||
- include: "#cast"
|
|
||||||
- include: "#function-call"
|
|
||||||
- include: "#parenthesis"
|
|
||||||
- include: "#array"
|
|
||||||
- include: "#keywords"
|
|
||||||
- include: "#struct-expression"
|
|
||||||
- include: "#number-literal"
|
|
||||||
- include: "#string-literal"
|
|
||||||
- include: "#identifier"
|
|
||||||
- include: "#punctuation"
|
|
||||||
comment:
|
comment:
|
||||||
match: "\\/\\/(.|\\/)*"
|
match: "\\/\\/(.|\\/)*"
|
||||||
name: comment.line.double-slash.reid
|
name: comment.line.double-slash.reid
|
||||||
fn-signature:
|
|
||||||
begin: "(fn)\\s*(\\w+)\\("
|
|
||||||
beginCaptures:
|
|
||||||
1:
|
|
||||||
name: keyword.fn.reid
|
|
||||||
2:
|
|
||||||
name: entity.name.function.reid
|
|
||||||
end: "\\)"
|
|
||||||
patterns:
|
|
||||||
- include: "#annotated-identifier"
|
|
||||||
- include: "#keywords"
|
|
||||||
- include: "#binop"
|
|
||||||
endCaptures:
|
|
||||||
2:
|
|
||||||
name: entity.name.type.reid
|
|
||||||
type-impl:
|
|
||||||
begin: >
|
|
||||||
(impl)\s*
|
|
||||||
(\w+)\s*
|
|
||||||
\{
|
|
||||||
end: "\\}"
|
|
||||||
captures:
|
|
||||||
1:
|
|
||||||
name: keyword.impl.reid
|
|
||||||
2:
|
|
||||||
name: entity.name.type
|
|
||||||
patterns:
|
|
||||||
- include: "#expression"
|
|
||||||
binop-impl:
|
|
||||||
begin: "(impl)\\s+(binop)\\s+\\(((.*)\\s*:\\s*(.*))\\)(.*)\\(((.*)\\s*:\\s*(.*))\\)\\s*->\\s*(\\w+)\\s*\\{"
|
|
||||||
end: "\\}"
|
|
||||||
beginCaptures:
|
|
||||||
1:
|
|
||||||
name: keyword.impl.reid
|
|
||||||
2:
|
|
||||||
name: keyword.impl.reid
|
|
||||||
4:
|
|
||||||
name: variable.parameter.binop.reid
|
|
||||||
5:
|
|
||||||
name: entity.name.type.parameter.binop.reid
|
|
||||||
6:
|
|
||||||
name: keyword.operator.math.reid
|
|
||||||
8:
|
|
||||||
name: variable.parameter.binop.reid
|
|
||||||
9:
|
|
||||||
name: entity.name.type.parameter.binop.reid
|
|
||||||
10:
|
|
||||||
name: entity.name.type.return.binop.reid
|
|
||||||
patterns:
|
|
||||||
- include: "#expression"
|
|
||||||
struct-definition:
|
struct-definition:
|
||||||
begin: "(struct)\\s*(\\w+)\\s*\\{"
|
match: "(struct)\\s*(\\w+)"
|
||||||
end: "\\}"
|
|
||||||
captures:
|
captures:
|
||||||
1:
|
1:
|
||||||
name: keyword.struct.reid
|
name: keyword.struct.reid
|
||||||
2:
|
2:
|
||||||
name: entity.name.type
|
name: entity.name.type
|
||||||
patterns:
|
|
||||||
- include: "#annotated-identifier"
|
|
||||||
struct-expression:
|
struct-expression:
|
||||||
begin: "([A-Z]\\w*)\\s*\\{"
|
begin: "\\b([A-Z]\\w*)\\s*\\{"
|
||||||
end: "\\}"
|
end: "\\}"
|
||||||
captures:
|
captures:
|
||||||
1:
|
1:
|
||||||
@ -136,31 +55,26 @@ repository:
|
|||||||
- include: "#expression"
|
- include: "#expression"
|
||||||
number-literal:
|
number-literal:
|
||||||
patterns:
|
patterns:
|
||||||
- match: "0x[0-9a-fA-F]+(\\.[0-9a-fA-F]+)?"
|
- match: "\\b0x[0-9a-fA-F]+(\\.[0-9a-fA-F]+)?\\b"
|
||||||
name: "constant.hexadecimal"
|
name: "constant.hexadecimal"
|
||||||
- match: "0o[0-7]+(\\.[0-7]+)?"
|
- match: "\\b0o[0-7]+(\\.[0-7]+)?\\b"
|
||||||
name: "constant.octal"
|
name: "constant.octal"
|
||||||
- match: "0b[01]+(\\.[01]+)?"
|
- match: "\\b0b[01]+(\\.[01]+)?\\b"
|
||||||
name: "constant.binary"
|
name: "constant.binary"
|
||||||
- match: "[0-9]+(\\.[0-9]+)?"
|
- match: "\\b[0-9]+(\\.[0-9]+)?\\b"
|
||||||
name: "constant.numeric"
|
name: "constant.numeric"
|
||||||
string-literal:
|
string-literal:
|
||||||
begin: '"'
|
begin: '"'
|
||||||
end: '"'
|
end: '"'
|
||||||
name: string.quoted.double
|
name: string.quoted.double
|
||||||
patterns:
|
patterns:
|
||||||
- match: "\\."
|
- match: "\\\\\\w"
|
||||||
name: constant.character.escape
|
name: constant.character.escape
|
||||||
block:
|
|
||||||
begin: "\\{"
|
|
||||||
end: "\\}"
|
|
||||||
patterns:
|
|
||||||
- include: "#expression"
|
|
||||||
namespace:
|
namespace:
|
||||||
match: "(\\w+)(\\:\\:)"
|
match: "(\\w+)(\\:\\:)"
|
||||||
captures:
|
captures:
|
||||||
1:
|
1:
|
||||||
name: entity.name.function.reid
|
name: entity.name.namespace.reid
|
||||||
2:
|
2:
|
||||||
name: keyword.operator.namespace.reid
|
name: keyword.operator.namespace.reid
|
||||||
cast:
|
cast:
|
||||||
@ -171,11 +85,16 @@ repository:
|
|||||||
2:
|
2:
|
||||||
name: entity.name.type.reid
|
name: entity.name.type.reid
|
||||||
function-call:
|
function-call:
|
||||||
begin: "(\\w+)?\\("
|
begin: "(\\w+)?(\\()"
|
||||||
end: "\\)"
|
end: "(\\))"
|
||||||
beginCaptures:
|
beginCaptures:
|
||||||
1:
|
1:
|
||||||
name: entity.name.function.reid
|
name: entity.name.function.reid
|
||||||
|
2:
|
||||||
|
name: punctuation.parenthesis.reid
|
||||||
|
endCaptures:
|
||||||
|
1:
|
||||||
|
name: punctuation.parenthesis.reid
|
||||||
patterns:
|
patterns:
|
||||||
- include: "#expression"
|
- include: "#expression"
|
||||||
parenthesis:
|
parenthesis:
|
||||||
@ -189,28 +108,26 @@ repository:
|
|||||||
name: keyword.operator.parenthesis.reid
|
name: keyword.operator.parenthesis.reid
|
||||||
patterns:
|
patterns:
|
||||||
- include: "#expression"
|
- include: "#expression"
|
||||||
annotated-identifier:
|
|
||||||
begin: "(\\w+)\\:"
|
|
||||||
end: ","
|
|
||||||
beginCaptures:
|
|
||||||
1:
|
|
||||||
name: variable.language.reid
|
|
||||||
patterns:
|
|
||||||
- include: "#expression"
|
|
||||||
identifier:
|
identifier:
|
||||||
patterns:
|
patterns:
|
||||||
- match: "[A-Z]\\w*"
|
- match: "\\b(?:\\w+)\\b"
|
||||||
name: entity.name.type.reid
|
|
||||||
- match: "\\w+"
|
|
||||||
name: variable.language.reid
|
name: variable.language.reid
|
||||||
keywords:
|
keywords:
|
||||||
patterns:
|
patterns:
|
||||||
- match: "let|mut|pub|extern"
|
- match: "\\b(?:let|mut|pub|extern)\\b"
|
||||||
name: "storage.type.reid"
|
name: storage.type.reid
|
||||||
- match: "if|return"
|
- match: "\\bimport\\b"
|
||||||
name: "keyword.control"
|
name: keyword.import.reid
|
||||||
- match: "self"
|
- match: "\\bbinop\\b"
|
||||||
name: "variable.language.self.reid"
|
name: keyword.binop.reid
|
||||||
|
- match: "\\bimpl\\b"
|
||||||
|
name: keyword.impl.reid
|
||||||
|
- match: "\\b(?:if|return|for|in)\\b"
|
||||||
|
name: keyword.control
|
||||||
|
- match: "\\bself\\b"
|
||||||
|
name: variable.language.self.reid
|
||||||
|
- match: "\\bfn\\b"
|
||||||
|
name: keyword.fn.reid
|
||||||
binop:
|
binop:
|
||||||
match: "\\<\\=|\\>\\=|\\=\\=|\\<|\\>|\\*|\\+|\\-|\\^|\\&\\&|\\&"
|
match: "\\<\\=|\\>\\=|\\=\\=|\\<|\\>|\\*|\\+|\\-|\\^|\\&\\&|\\&"
|
||||||
name: keyword.operator.math.reid
|
name: keyword.operator.math.reid
|
||||||
@ -226,7 +143,7 @@ repository:
|
|||||||
patterns:
|
patterns:
|
||||||
- include: "#expression"
|
- include: "#expression"
|
||||||
common-type:
|
common-type:
|
||||||
match: "u8|u16|u32|u64|u128|i8|i16|i32|i64|i128|bool"
|
match: "\\b(?:u8|u16|u32|u64|u128|i8|i16|i32|i64|i128|f16|f16b|f32|f64|f80|f128|f128ppc|bool|char|([A-Z]\\w*))\\b"
|
||||||
name: entity.name.type.common.reid
|
name: entity.name.type.common.reid
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,21 +1,23 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "reid"
|
name = "reid"
|
||||||
version = "1.0.0-beta.2"
|
version = "1.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["color"]
|
default = ["color", "cli"]
|
||||||
|
|
||||||
color = ["colored"]
|
color = ["colored"]
|
||||||
log_output = []
|
cli = ["argh", "stderrlog"]
|
||||||
context_debug = []
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
## Make it easier to generate errors
|
## Make it easier to generate errors
|
||||||
thiserror = "1.0.44"
|
thiserror = "1.0.44"
|
||||||
reid-lib = { path = "../reid-llvm-lib", version = "1.0.0-beta.1", registry="gitea-teascade" }
|
reid-lib = { path = "../reid-llvm-lib", version = "1.0.0", registry="gitea-teascade" }
|
||||||
|
|
||||||
colored = {version = "3.0.0", optional = true}
|
argh = { version = "0.1.13", optional = true }
|
||||||
|
stderrlog = { version = "0.6.0", optional = true }
|
||||||
|
log = "*"
|
||||||
|
colored = { version = "3.0.0", optional = true }
|
||||||
@ -20,7 +20,6 @@ fn main() -> Result<(), std::io::Error> {
|
|||||||
let mir_path = parent.with_extension("mir");
|
let mir_path = parent.with_extension("mir");
|
||||||
let asm_path = parent.with_extension("asm");
|
let asm_path = parent.with_extension("asm");
|
||||||
|
|
||||||
#[cfg(feature = "log_output")]
|
|
||||||
let before = std::time::SystemTime::now();
|
let before = std::time::SystemTime::now();
|
||||||
|
|
||||||
let text = fs::read_to_string(&path)?;
|
let text = fs::read_to_string(&path)?;
|
||||||
@ -38,35 +37,29 @@ fn main() -> Result<(), std::io::Error> {
|
|||||||
},
|
},
|
||||||
CustomIRs { llir, mir },
|
CustomIRs { llir, mir },
|
||||||
)) => {
|
)) => {
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{}", _llvm_ir);
|
||||||
{
|
log::debug!("Compiled with triple: {}\n", &_triple);
|
||||||
println!("{}", _llvm_ir);
|
log::debug!("Output LLVM IR to {:?}", llvm_ir_path);
|
||||||
println!("Compiled with triple: {}\n", &_triple);
|
log::debug!("Output Assembly to {:?}", asm_path);
|
||||||
println!("Output LLVM IR to {:?}", llvm_ir_path);
|
log::debug!("Output Object-file to {:?}\n", object_path);
|
||||||
println!("Output Assembly to {:?}", asm_path);
|
log::debug!("Output LLIR-file to {:?}\n", llir_path);
|
||||||
println!("Output Object-file to {:?}\n", object_path);
|
log::debug!("Output MIR-file to {:?}\n", mir_path);
|
||||||
println!("Output LLIR-file to {:?}\n", llir_path);
|
|
||||||
println!("Output MIR-file to {:?}\n", mir_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
fs::write(&llvm_ir_path, &_llvm_ir).expect("Could not write LLVM IR -file!");
|
fs::write(&llvm_ir_path, &_llvm_ir).expect("Could not write LLVM IR -file!");
|
||||||
fs::write(&asm_path, &assembly).expect("Could not write Assembly-file!");
|
fs::write(&asm_path, &assembly).expect("Could not write Assembly-file!");
|
||||||
fs::write(&object_path, &obj_buffer).expect("Could not write Object-file!");
|
fs::write(&object_path, &obj_buffer).expect("Could not write Object-file!");
|
||||||
fs::write(&llir_path, &llir).expect("Could not write LLIR-file!");
|
fs::write(&llir_path, &llir).expect("Could not write LLIR-file!");
|
||||||
fs::write(&mir_path, &mir).expect("Could not write MIR-file!");
|
fs::write(&mir_path, &mir).expect("Could not write MIR-file!");
|
||||||
#[cfg(feature = "log_output")]
|
let after = std::time::SystemTime::now();
|
||||||
{
|
println!(
|
||||||
let after = std::time::SystemTime::now();
|
"Compilation took: {:.2}ms\n",
|
||||||
println!(
|
(after.duration_since(before).unwrap().as_micros() as f32) / 1000.
|
||||||
"Compilation took: {:.2}ms\n",
|
);
|
||||||
(after.duration_since(before).unwrap().as_micros() as f32) / 1000.
|
|
||||||
);
|
|
||||||
|
|
||||||
println!("Linking {:?}", &object_path);
|
println!("Linking {:?}", &object_path);
|
||||||
}
|
|
||||||
|
|
||||||
let linker = std::env::var("LD").unwrap_or("ld".to_owned());
|
let linker = std::env::var("LD").unwrap_or("ld".to_owned());
|
||||||
let mut linker = LDRunner::from_command(&linker).with_library("c");
|
let mut linker = LDRunner::from_command(&linker).with_library("c").with_library("m");
|
||||||
for library in libraries {
|
for library in libraries {
|
||||||
linker = linker.with_library(&library);
|
linker = linker.with_library(&library);
|
||||||
}
|
}
|
||||||
@ -75,8 +68,7 @@ fn main() -> Result<(), std::io::Error> {
|
|||||||
Err(e) => panic!("{}", e),
|
Err(e) => panic!("{}", e),
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
#[cfg(feature = "log_output")]
|
log::error!("Please input compiled file path!")
|
||||||
println!("Please input compiled file path!")
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ extern fn puts(message: *char) -> i32;
|
|||||||
extern fn free(ptr: *u8);
|
extern fn free(ptr: *u8);
|
||||||
extern fn div(numerator: i32, denominator: i32) -> div_t;
|
extern fn div(numerator: i32, denominator: i32) -> div_t;
|
||||||
|
|
||||||
|
/// Editable string value that can be printed and extended
|
||||||
struct String {
|
struct String {
|
||||||
inner: *char,
|
inner: *char,
|
||||||
length: u64,
|
length: u64,
|
||||||
@ -11,6 +12,7 @@ struct String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl String {
|
impl String {
|
||||||
|
/// Returns a new empty `String`-object, which must later be manually freed.
|
||||||
pub fn new() -> String {
|
pub fn new() -> String {
|
||||||
String {
|
String {
|
||||||
inner: char::malloc(0),
|
inner: char::malloc(0),
|
||||||
@ -20,6 +22,8 @@ impl String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a new `String`-object containing initially data from the given
|
||||||
|
/// string-literal which must be later freed.
|
||||||
pub fn from(str: *char) -> String {
|
pub fn from(str: *char) -> String {
|
||||||
let length = str_length(str) as u64;
|
let length = str_length(str) as u64;
|
||||||
let mut new = String::new();
|
let mut new = String::new();
|
||||||
@ -33,12 +37,14 @@ impl String {
|
|||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Same as `concat`
|
||||||
pub fn push(&mut self, other: String) {
|
pub fn push(&mut self, other: String) {
|
||||||
for i in 0 .. (str_length(other.inner) - 1) {
|
for i in 0 .. (str_length(other.inner) - 1) {
|
||||||
add_char(self, other.inner[i]);
|
add_char(self, other.inner[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds a character to the end of this string
|
||||||
pub fn add_char(&mut self, c: char) {
|
pub fn add_char(&mut self, c: char) {
|
||||||
if ((*self).length + 1) >= (*self).max_length {
|
if ((*self).length + 1) >= (*self).max_length {
|
||||||
let new = char::malloc((*self).max_length + 4);
|
let new = char::malloc((*self).max_length + 4);
|
||||||
@ -57,6 +63,7 @@ impl String {
|
|||||||
(*self).length = (*self).length + 1;
|
(*self).length = (*self).length + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Formats the given number into the end of the string.
|
||||||
pub fn push_num(&mut self, num: u64) {
|
pub fn push_num(&mut self, num: u64) {
|
||||||
if num >= 10 {
|
if num >= 10 {
|
||||||
self.push_num(num / 10)
|
self.push_num(num / 10)
|
||||||
@ -75,18 +82,22 @@ impl String {
|
|||||||
else if rem == 9 { self.add_char('9'); }
|
else if rem == 9 { self.add_char('9'); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Concatenates `source` to the end of `destination`.
|
||||||
pub fn concat(&mut self, other: &String) {
|
pub fn concat(&mut self, other: &String) {
|
||||||
for i in 0 .. *other.length {
|
for i in 0 .. *other.length {
|
||||||
self.add_char(*other.inner[i]);
|
self.add_char(*other.inner[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Edits given `string` by setting the character at index `position` to be
|
||||||
|
/// `c`.
|
||||||
pub fn set(&mut self, c: char, position: u64) {
|
pub fn set(&mut self, c: char, position: u64) {
|
||||||
if position <= (*self).length {
|
if position <= (*self).length {
|
||||||
(*self).inner[position] = c;
|
(*self).inner[position] = c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Frees this given string
|
||||||
pub fn free(&self) {
|
pub fn free(&self) {
|
||||||
free((*self).inner as *u8);
|
free((*self).inner as *u8);
|
||||||
}
|
}
|
||||||
@ -111,14 +122,17 @@ struct div_t {
|
|||||||
remainder: i32,
|
remainder: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Print given string to stdout
|
||||||
pub fn print(message: String) {
|
pub fn print(message: String) {
|
||||||
puts(message.inner);
|
puts(message.inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Divide an integer, returning the quotient and remainder.
|
||||||
pub fn int_div(numerator: i32, denominator: i32) -> div_t {
|
pub fn int_div(numerator: i32, denominator: i32) -> div_t {
|
||||||
return div(numerator, denominator);
|
return div(numerator, denominator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// (deprecated) creates a new editable string
|
||||||
pub fn new_string() -> String {
|
pub fn new_string() -> String {
|
||||||
String {
|
String {
|
||||||
inner: char::malloc(0),
|
inner: char::malloc(0),
|
||||||
@ -128,6 +142,8 @@ pub fn new_string() -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a new `String`-object containing initially data from the given
|
||||||
|
/// string-literal which must be later freed.
|
||||||
pub fn from_str(str: *char) -> String {
|
pub fn from_str(str: *char) -> String {
|
||||||
let length = str_length(str) as u64;
|
let length = str_length(str) as u64;
|
||||||
let mut new = new_string();
|
let mut new = new_string();
|
||||||
@ -141,6 +157,7 @@ pub fn from_str(str: *char) -> String {
|
|||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// (deprecated) Adds a character to the end of a given string
|
||||||
pub fn add_char(string: &mut String, c: char) {
|
pub fn add_char(string: &mut String, c: char) {
|
||||||
if ((*string).length + 1) >= (*string).max_length {
|
if ((*string).length + 1) >= (*string).max_length {
|
||||||
let new = char::malloc((*string).max_length + 4);
|
let new = char::malloc((*string).max_length + 4);
|
||||||
@ -159,12 +176,14 @@ pub fn add_char(string: &mut String, c: char) {
|
|||||||
(*string).length = (*string).length + 1;
|
(*string).length = (*string).length + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// (deprecated) sets a character in a string
|
||||||
pub fn set_char(string: &mut String, c: char, position: u64) {
|
pub fn set_char(string: &mut String, c: char, position: u64) {
|
||||||
if position <= (*string).length {
|
if position <= (*string).length {
|
||||||
(*string).inner[position] = c;
|
(*string).inner[position] = c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// (deprecated) frees given string
|
||||||
pub fn free_string(string: &String) {
|
pub fn free_string(string: &String) {
|
||||||
free((*string).inner as *u8);
|
free((*string).inner as *u8);
|
||||||
}
|
}
|
||||||
@ -183,6 +202,7 @@ fn str_length(string: *char) -> u32 {
|
|||||||
return pos + 1;
|
return pos + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// (deprecated) concatenates number to the end of this string.
|
||||||
pub fn add_num_to_str(string: &mut String, num: u64) {
|
pub fn add_num_to_str(string: &mut String, num: u64) {
|
||||||
if num >= 10 {
|
if num >= 10 {
|
||||||
add_num_to_str(string, num / 10)
|
add_num_to_str(string, num / 10)
|
||||||
@ -201,12 +221,15 @@ pub fn add_num_to_str(string: &mut String, num: u64) {
|
|||||||
else if rem == 9 { add_char(string, '9'); }
|
else if rem == 9 { add_char(string, '9'); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// (deprecated) concatenates two strings to the destination
|
||||||
pub fn concat_strings(destination: &mut String, source: String) {
|
pub fn concat_strings(destination: &mut String, source: String) {
|
||||||
for i in 0 .. (str_length(source.inner) - 1) {
|
for i in 0 .. (str_length(source.inner) - 1) {
|
||||||
add_char(destination, source.inner[i]);
|
add_char(destination, source.inner[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `value` as clamped between `min` and `max`. Equivalent to
|
||||||
|
/// `max(min(value, max), min)`
|
||||||
pub fn clamp(min: f32, max: f32, value: f32) -> f32 {
|
pub fn clamp(min: f32, max: f32, value: f32) -> f32 {
|
||||||
if value > max {
|
if value > max {
|
||||||
return max;
|
return max;
|
||||||
@ -217,6 +240,7 @@ pub fn clamp(min: f32, max: f32, value: f32) -> f32 {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the absolute value of `value`.
|
||||||
pub fn abs(f: f32) -> f32 {
|
pub fn abs(f: f32) -> f32 {
|
||||||
if f < 0.0 {
|
if f < 0.0 {
|
||||||
return f * (0.0 - 1.0);
|
return f * (0.0 - 1.0);
|
||||||
|
|||||||
@ -116,6 +116,7 @@ pub enum Token {
|
|||||||
|
|
||||||
Whitespace(String),
|
Whitespace(String),
|
||||||
Comment(String),
|
Comment(String),
|
||||||
|
Doc(String),
|
||||||
Eof,
|
Eof,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,6 +197,7 @@ impl ToString for Token {
|
|||||||
Token::Percent => String::from('%'),
|
Token::Percent => String::from('%'),
|
||||||
Token::Whitespace(val) => val.clone(),
|
Token::Whitespace(val) => val.clone(),
|
||||||
Token::Comment(val) => format!("//{}", val.clone()),
|
Token::Comment(val) => format!("//{}", val.clone()),
|
||||||
|
Token::Doc(val) => format!("///{}", val.clone()),
|
||||||
Token::Unknown(val) => val.to_string(),
|
Token::Unknown(val) => val.to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -309,13 +311,25 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
|
|||||||
}
|
}
|
||||||
// Comments
|
// Comments
|
||||||
'/' if cursor.first() == Some('/') => {
|
'/' if cursor.first() == Some('/') => {
|
||||||
|
cursor.next();
|
||||||
|
let doc = if cursor.first() == Some('/') {
|
||||||
|
cursor.next();
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
let mut comment = String::new();
|
let mut comment = String::new();
|
||||||
while !matches!(cursor.first(), Some('\n') | None) {
|
while !matches!(cursor.first(), Some('\n') | None) {
|
||||||
if let Some(c) = cursor.next() {
|
if let Some(c) = cursor.next() {
|
||||||
comment.push(c);
|
comment.push(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Token::Comment(comment)
|
if doc {
|
||||||
|
Token::Doc(comment)
|
||||||
|
} else {
|
||||||
|
Token::Comment(comment)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
'\"' | '\'' => {
|
'\"' | '\'' => {
|
||||||
let mut value = String::new();
|
let mut value = String::new();
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
//! This is the module that contains relevant code to parsing Reid, that is to
|
//! This is the module that contains relevant code to parsing Reid, that is to
|
||||||
//! say transforming a Vec of FullTokens into a loose parsed AST that can be
|
//! say transforming a Vec of FullTokens into a loose parsed AST that can be
|
||||||
//! used for unwrapping syntax sugar, and then be transformed into Reid MIR.
|
//! used for unwrapping syntax sugar, and then be transformed into Reid MIR.
|
||||||
use std::{fs::Metadata, path::PathBuf};
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use token_stream::TokenRange;
|
use token_stream::TokenRange;
|
||||||
|
|
||||||
@ -184,7 +184,7 @@ pub struct LetStatement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ImportStatement(pub Vec<String>, pub TokenRange);
|
pub struct ImportStatement(pub Vec<(String, TokenRange)>, pub TokenRange);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct FunctionDefinition(pub FunctionSignature, pub bool, pub Block, pub TokenRange);
|
pub struct FunctionDefinition(pub FunctionSignature, pub bool, pub Block, pub TokenRange);
|
||||||
@ -192,6 +192,7 @@ pub struct FunctionDefinition(pub FunctionSignature, pub bool, pub Block, pub To
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct FunctionSignature {
|
pub struct FunctionSignature {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub documentation: Option<String>,
|
||||||
pub self_kind: SelfKind,
|
pub self_kind: SelfKind,
|
||||||
pub params: Vec<(String, Type, TokenRange)>,
|
pub params: Vec<(String, Type, TokenRange)>,
|
||||||
pub return_type: Option<Type>,
|
pub return_type: Option<Type>,
|
||||||
@ -207,7 +208,7 @@ pub enum SelfKind {
|
|||||||
None,
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum ReturnType {
|
pub enum ReturnType {
|
||||||
Soft,
|
Soft,
|
||||||
Hard,
|
Hard,
|
||||||
@ -272,9 +273,9 @@ pub enum TopLevelStatement {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct BinopDefinition {
|
pub struct BinopDefinition {
|
||||||
pub lhs: (String, Type),
|
pub lhs: (String, Type, TokenRange),
|
||||||
pub op: BinaryOperator,
|
pub op: BinaryOperator,
|
||||||
pub rhs: (String, Type),
|
pub rhs: (String, Type, TokenRange),
|
||||||
pub return_ty: Type,
|
pub return_ty: Type,
|
||||||
pub block: Block,
|
pub block: Block,
|
||||||
pub signature_range: TokenRange,
|
pub signature_range: TokenRange,
|
||||||
|
|||||||
@ -175,7 +175,50 @@ impl Parse for AssociatedFunctionCall {
|
|||||||
let ty = stream.parse()?;
|
let ty = stream.parse()?;
|
||||||
stream.expect(Token::Colon)?;
|
stream.expect(Token::Colon)?;
|
||||||
stream.expect(Token::Colon)?;
|
stream.expect(Token::Colon)?;
|
||||||
Ok(AssociatedFunctionCall(ty, stream.parse()?))
|
|
||||||
|
if stream.next_is_whitespace() {
|
||||||
|
stream.expecting_err_nonfatal("associated function name");
|
||||||
|
return Ok(AssociatedFunctionCall(
|
||||||
|
ty,
|
||||||
|
FunctionCallExpression {
|
||||||
|
name: String::new(),
|
||||||
|
params: Vec::new(),
|
||||||
|
range: stream.get_range_prev_curr().unwrap(),
|
||||||
|
is_macro: false,
|
||||||
|
},
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
match stream.parse() {
|
||||||
|
Ok(fn_call) => Ok(AssociatedFunctionCall(ty, fn_call)),
|
||||||
|
_ => {
|
||||||
|
if let Some(Token::Identifier(fn_name)) = stream.peek() {
|
||||||
|
stream.next();
|
||||||
|
stream.expected_err_nonfatal("associated function call");
|
||||||
|
|
||||||
|
Ok(AssociatedFunctionCall(
|
||||||
|
ty,
|
||||||
|
FunctionCallExpression {
|
||||||
|
name: fn_name,
|
||||||
|
params: Vec::new(),
|
||||||
|
range: stream.get_range_prev_curr().unwrap(),
|
||||||
|
is_macro: false,
|
||||||
|
},
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
stream.expected_err_nonfatal("associated function name");
|
||||||
|
Ok(AssociatedFunctionCall(
|
||||||
|
ty,
|
||||||
|
FunctionCallExpression {
|
||||||
|
name: String::new(),
|
||||||
|
params: Vec::new(),
|
||||||
|
range: stream.get_range_prev_curr().unwrap(),
|
||||||
|
is_macro: false,
|
||||||
|
},
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -244,9 +287,9 @@ impl Parse for PrimaryExpression {
|
|||||||
stream.get_range().unwrap(),
|
stream.get_range().unwrap(),
|
||||||
)
|
)
|
||||||
} else if let Some(Token::Star) = stream.peek() {
|
} else if let Some(Token::Star) = stream.peek() {
|
||||||
stream.next(); // Consume Et
|
stream.next(); // Consume Star
|
||||||
apply_inner(stream.parse()?, |e| {
|
apply_inner(stream.parse()?, |e| {
|
||||||
Expression(Kind::Deref(Box::new(e.0)), stream.get_range().unwrap())
|
Expression(Kind::Deref(Box::new(e.0.clone())), e.0 .1)
|
||||||
})
|
})
|
||||||
} else if let Ok(unary) = stream.parse() {
|
} else if let Ok(unary) = stream.parse() {
|
||||||
Expression(
|
Expression(
|
||||||
@ -467,7 +510,7 @@ fn parse_binop_rhs(
|
|||||||
if curr_token_prec < next_prec {
|
if curr_token_prec < next_prec {
|
||||||
// Operator on the right of rhs has more precedence, turn
|
// Operator on the right of rhs has more precedence, turn
|
||||||
// rhs into lhs for new binop
|
// rhs into lhs for new binop
|
||||||
rhs = parse_binop_rhs(stream, rhs, Some(op))?;
|
rhs = stream.parse_with(|mut st| parse_binop_rhs(&mut st, rhs, Some(op)))?;
|
||||||
} else {
|
} else {
|
||||||
let _ = prev_operator.insert(next_op);
|
let _ = prev_operator.insert(next_op);
|
||||||
}
|
}
|
||||||
@ -610,7 +653,7 @@ impl Parse for LetStatement {
|
|||||||
stream.expect(Token::Equals)?;
|
stream.expect(Token::Equals)?;
|
||||||
|
|
||||||
let expression = stream.parse()?;
|
let expression = stream.parse()?;
|
||||||
stream.expect(Token::Semi)?;
|
stream.expect_nonfatal(Token::Semi).ok();
|
||||||
Ok(LetStatement {
|
Ok(LetStatement {
|
||||||
name: variable,
|
name: variable,
|
||||||
ty,
|
ty,
|
||||||
@ -631,19 +674,21 @@ impl Parse for ImportStatement {
|
|||||||
let mut import_list = Vec::new();
|
let mut import_list = Vec::new();
|
||||||
|
|
||||||
if let Some(Token::Identifier(name)) = stream.next() {
|
if let Some(Token::Identifier(name)) = stream.next() {
|
||||||
import_list.push(name);
|
import_list.push((name, stream.get_range_prev_curr().unwrap()));
|
||||||
while stream.expect(Token::Colon).is_ok() && stream.expect(Token::Colon).is_ok() {
|
while stream.expect(Token::Colon).is_ok() && stream.expect(Token::Colon).is_ok() {
|
||||||
if let Some(Token::Identifier(name)) = stream.next() {
|
if let Some(Token::Identifier(name)) = stream.peek() {
|
||||||
import_list.push(name);
|
stream.next(); // Consume identifier
|
||||||
|
import_list.push((name, stream.get_range_prev_curr().unwrap()));
|
||||||
} else {
|
} else {
|
||||||
Err(stream.expected_err("identifier")?)?
|
stream.expected_err_nonfatal("identifier");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(stream.expected_err("identifier")?)?
|
Err(stream.expected_err("identifier")?)?
|
||||||
}
|
}
|
||||||
|
|
||||||
stream.expect(Token::Semi)?;
|
stream.expect_nonfatal(Token::Semi).ok();
|
||||||
|
|
||||||
Ok(ImportStatement(import_list, stream.get_range().unwrap()))
|
Ok(ImportStatement(import_list, stream.get_range().unwrap()))
|
||||||
}
|
}
|
||||||
@ -651,6 +696,8 @@ impl Parse for ImportStatement {
|
|||||||
|
|
||||||
impl Parse for FunctionDefinition {
|
impl Parse for FunctionDefinition {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
|
let documentation = stream.find_documentation();
|
||||||
|
|
||||||
let is_pub = if let Some(Token::PubKeyword) = stream.peek() {
|
let is_pub = if let Some(Token::PubKeyword) = stream.peek() {
|
||||||
stream.next(); // Consume pub
|
stream.next(); // Consume pub
|
||||||
true
|
true
|
||||||
@ -659,8 +706,10 @@ impl Parse for FunctionDefinition {
|
|||||||
};
|
};
|
||||||
|
|
||||||
stream.expect(Token::FnKeyword)?;
|
stream.expect(Token::FnKeyword)?;
|
||||||
|
let mut signature: FunctionSignature = stream.parse()?;
|
||||||
|
signature.documentation = documentation;
|
||||||
Ok(FunctionDefinition(
|
Ok(FunctionDefinition(
|
||||||
stream.parse()?,
|
signature,
|
||||||
is_pub,
|
is_pub,
|
||||||
stream.parse()?,
|
stream.parse()?,
|
||||||
stream.get_range().unwrap(),
|
stream.get_range().unwrap(),
|
||||||
@ -765,6 +814,7 @@ impl Parse for FunctionSignature {
|
|||||||
|
|
||||||
Ok(FunctionSignature {
|
Ok(FunctionSignature {
|
||||||
name,
|
name,
|
||||||
|
documentation: None,
|
||||||
params,
|
params,
|
||||||
self_kind,
|
self_kind,
|
||||||
return_type,
|
return_type,
|
||||||
@ -788,7 +838,7 @@ impl Parse for Block {
|
|||||||
// if semicolon is missing.
|
// if semicolon is missing.
|
||||||
if !matches!(e, Expression(ExpressionKind::IfExpr(_), _)) {
|
if !matches!(e, Expression(ExpressionKind::IfExpr(_), _)) {
|
||||||
// In theory could ignore the missing semicolon..
|
// In theory could ignore the missing semicolon..
|
||||||
return Err(stream.expected_err("semicolon to complete statement")?);
|
stream.expected_err_nonfatal("semicolon to complete statement");
|
||||||
}
|
}
|
||||||
|
|
||||||
statements.push(BlockLevelStatement::Expression(e));
|
statements.push(BlockLevelStatement::Expression(e));
|
||||||
@ -906,7 +956,8 @@ pub enum DotIndexKind {
|
|||||||
impl Parse for DotIndexKind {
|
impl Parse for DotIndexKind {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
stream.expect(Token::Dot)?;
|
stream.expect(Token::Dot)?;
|
||||||
if let Some(Token::Identifier(name)) = stream.next() {
|
if let Some(Token::Identifier(name)) = stream.peek() {
|
||||||
|
stream.next(); // Consume identifer
|
||||||
if let Ok(args) = stream.parse::<FunctionArgs>() {
|
if let Ok(args) = stream.parse::<FunctionArgs>() {
|
||||||
Ok(Self::FunctionCall(FunctionCallExpression {
|
Ok(Self::FunctionCall(FunctionCallExpression {
|
||||||
name,
|
name,
|
||||||
@ -915,10 +966,18 @@ impl Parse for DotIndexKind {
|
|||||||
is_macro: false,
|
is_macro: false,
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
Ok(Self::StructValueIndex(name, stream.get_range().unwrap()))
|
Ok(Self::StructValueIndex(name, stream.get_range_prev().unwrap()))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return Err(stream.expected_err("struct index (number)")?);
|
if stream.next_is_whitespace() {
|
||||||
|
stream.expecting_err_nonfatal("struct index");
|
||||||
|
Ok(Self::StructValueIndex(
|
||||||
|
String::new(),
|
||||||
|
stream.get_range_prev_curr().unwrap(),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
Err(stream.expecting_err("struct index")?)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -932,7 +991,7 @@ impl Parse for BlockLevelStatement {
|
|||||||
Some(Token::ReturnKeyword) => {
|
Some(Token::ReturnKeyword) => {
|
||||||
stream.next();
|
stream.next();
|
||||||
let exp = stream.parse().ok();
|
let exp = stream.parse().ok();
|
||||||
stream.expect(Token::Semi)?;
|
stream.expect_nonfatal(Token::Semi).ok();
|
||||||
Stmt::Return(ReturnType::Hard, exp)
|
Stmt::Return(ReturnType::Hard, exp)
|
||||||
}
|
}
|
||||||
Some(Token::For) => {
|
Some(Token::For) => {
|
||||||
@ -997,7 +1056,7 @@ impl Parse for SetStatement {
|
|||||||
let var_ref = stream.parse()?;
|
let var_ref = stream.parse()?;
|
||||||
stream.expect(Token::Equals)?;
|
stream.expect(Token::Equals)?;
|
||||||
let expr = stream.parse()?;
|
let expr = stream.parse()?;
|
||||||
stream.expect(Token::Semi)?;
|
stream.expect_nonfatal(Token::Semi).ok();
|
||||||
Ok(SetStatement(var_ref, expr, stream.get_range().unwrap()))
|
Ok(SetStatement(var_ref, expr, stream.get_range().unwrap()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1035,10 +1094,13 @@ impl Parse for TopLevelStatement {
|
|||||||
Ok(match stream.peek() {
|
Ok(match stream.peek() {
|
||||||
Some(Token::ImportKeyword) => Stmt::Import(stream.parse()?),
|
Some(Token::ImportKeyword) => Stmt::Import(stream.parse()?),
|
||||||
Some(Token::Extern) => {
|
Some(Token::Extern) => {
|
||||||
|
let documentation = stream.find_documentation();
|
||||||
stream.next(); // Consume Extern
|
stream.next(); // Consume Extern
|
||||||
stream.expect(Token::FnKeyword)?;
|
stream.expect(Token::FnKeyword)?;
|
||||||
let extern_fn = Stmt::ExternFunction(stream.parse()?);
|
let mut signature: FunctionSignature = stream.parse()?;
|
||||||
stream.expect(Token::Semi)?;
|
signature.documentation = documentation;
|
||||||
|
let extern_fn = Stmt::ExternFunction(signature);
|
||||||
|
stream.expect_nonfatal(Token::Semi).ok();
|
||||||
extern_fn
|
extern_fn
|
||||||
}
|
}
|
||||||
Some(Token::FnKeyword) | Some(Token::PubKeyword) => Stmt::FunctionDefinition(stream.parse()?),
|
Some(Token::FnKeyword) | Some(Token::PubKeyword) => Stmt::FunctionDefinition(stream.parse()?),
|
||||||
@ -1072,6 +1134,7 @@ impl Parse for BinopDefinition {
|
|||||||
let Some(Token::Identifier(lhs_name)) = stream.next() else {
|
let Some(Token::Identifier(lhs_name)) = stream.next() else {
|
||||||
return Err(stream.expected_err("lhs name")?);
|
return Err(stream.expected_err("lhs name")?);
|
||||||
};
|
};
|
||||||
|
let lhs_range = stream.get_range_prev_curr().unwrap();
|
||||||
stream.expect(Token::Colon)?;
|
stream.expect(Token::Colon)?;
|
||||||
let lhs_type = stream.parse()?;
|
let lhs_type = stream.parse()?;
|
||||||
stream.expect(Token::ParenClose)?;
|
stream.expect(Token::ParenClose)?;
|
||||||
@ -1082,6 +1145,7 @@ impl Parse for BinopDefinition {
|
|||||||
let Some(Token::Identifier(rhs_name)) = stream.next() else {
|
let Some(Token::Identifier(rhs_name)) = stream.next() else {
|
||||||
return Err(stream.expected_err("rhs name")?);
|
return Err(stream.expected_err("rhs name")?);
|
||||||
};
|
};
|
||||||
|
let rhs_range = stream.get_range_prev_curr().unwrap();
|
||||||
stream.expect(Token::Colon)?;
|
stream.expect(Token::Colon)?;
|
||||||
let rhs_type = stream.parse()?;
|
let rhs_type = stream.parse()?;
|
||||||
stream.expect(Token::ParenClose)?;
|
stream.expect(Token::ParenClose)?;
|
||||||
@ -1091,9 +1155,9 @@ impl Parse for BinopDefinition {
|
|||||||
stream.expect(Token::Arrow)?;
|
stream.expect(Token::Arrow)?;
|
||||||
|
|
||||||
Ok(BinopDefinition {
|
Ok(BinopDefinition {
|
||||||
lhs: (lhs_name, lhs_type),
|
lhs: (lhs_name, lhs_type, lhs_range),
|
||||||
op: operator,
|
op: operator,
|
||||||
rhs: (rhs_name, rhs_type),
|
rhs: (rhs_name, rhs_type, rhs_range),
|
||||||
return_ty: stream.parse()?,
|
return_ty: stream.parse()?,
|
||||||
block: stream.parse()?,
|
block: stream.parse()?,
|
||||||
signature_range,
|
signature_range,
|
||||||
@ -1114,11 +1178,11 @@ impl Parse for AssociatedFunctionBlock {
|
|||||||
match stream.peek() {
|
match stream.peek() {
|
||||||
Some(Token::FnKeyword) | Some(Token::PubKeyword) => {
|
Some(Token::FnKeyword) | Some(Token::PubKeyword) => {
|
||||||
let mut fun: FunctionDefinition = stream.parse()?;
|
let mut fun: FunctionDefinition = stream.parse()?;
|
||||||
fun.0.self_kind = match fun.0.self_kind {
|
match &mut fun.0.self_kind {
|
||||||
SelfKind::Owned(_) => SelfKind::Owned(ty.clone()),
|
SelfKind::Owned(inner_ty) => inner_ty.0 = ty.0.clone(),
|
||||||
SelfKind::Borrow(_) => SelfKind::Borrow(ty.clone()),
|
SelfKind::Borrow(inner_ty) => inner_ty.0 = ty.0.clone(),
|
||||||
SelfKind::MutBorrow(_) => SelfKind::MutBorrow(ty.clone()),
|
SelfKind::MutBorrow(inner_ty) => inner_ty.0 = ty.0.clone(),
|
||||||
SelfKind::None => SelfKind::None,
|
SelfKind::None => {}
|
||||||
};
|
};
|
||||||
functions.push(fun);
|
functions.push(fun);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{self},
|
ast::{self, ReturnType},
|
||||||
mir::{
|
mir::{
|
||||||
self, CustomTypeKey, FunctionParam, ModuleMap, NamedVariableRef, ReturnKind, SourceModuleId, StmtKind,
|
self, CustomTypeKey, FunctionParam, ModuleMap, NamedVariableRef, ReturnKind, SourceModuleId, StmtKind,
|
||||||
StructField, StructType, WhileStatement,
|
StructField, StructType, WhileStatement,
|
||||||
@ -30,12 +30,20 @@ impl ast::Module {
|
|||||||
for stmt in &self.top_level_statements {
|
for stmt in &self.top_level_statements {
|
||||||
match stmt {
|
match stmt {
|
||||||
Import(import) => {
|
Import(import) => {
|
||||||
imports.push(mir::Import(import.0.clone(), import.1.as_meta(module_id)));
|
imports.push(mir::Import(
|
||||||
|
import
|
||||||
|
.0
|
||||||
|
.iter()
|
||||||
|
.map(|(s, range)| (s.clone(), range.as_meta(module_id)))
|
||||||
|
.collect(),
|
||||||
|
import.1.as_meta(module_id),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
FunctionDefinition(function_def) => functions.push(function_def.into_mir(module_id)),
|
FunctionDefinition(function_def) => functions.push(function_def.into_mir(module_id)),
|
||||||
ExternFunction(signature) => {
|
ExternFunction(signature) => {
|
||||||
let def = mir::FunctionDefinition {
|
let def = mir::FunctionDefinition {
|
||||||
name: signature.name.clone(),
|
name: signature.name.clone(),
|
||||||
|
documentation: signature.documentation.clone(),
|
||||||
linkage_name: None,
|
linkage_name: None,
|
||||||
is_pub: false,
|
is_pub: false,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
@ -56,6 +64,7 @@ impl ast::Module {
|
|||||||
.collect(),
|
.collect(),
|
||||||
kind: mir::FunctionDefinitionKind::Extern(false),
|
kind: mir::FunctionDefinitionKind::Extern(false),
|
||||||
source: Some(module_id),
|
source: Some(module_id),
|
||||||
|
signature_meta: signature.range.as_meta(module_id),
|
||||||
};
|
};
|
||||||
functions.push(def);
|
functions.push(def);
|
||||||
}
|
}
|
||||||
@ -96,13 +105,13 @@ impl ast::Module {
|
|||||||
lhs: mir::FunctionParam {
|
lhs: mir::FunctionParam {
|
||||||
name: lhs.0.clone(),
|
name: lhs.0.clone(),
|
||||||
ty: lhs.1 .0.into_mir(module_id),
|
ty: lhs.1 .0.into_mir(module_id),
|
||||||
meta: lhs.1 .1.as_meta(module_id),
|
meta: lhs.2.as_meta(module_id),
|
||||||
},
|
},
|
||||||
op: op.mir(),
|
op: op.mir(),
|
||||||
rhs: mir::FunctionParam {
|
rhs: mir::FunctionParam {
|
||||||
name: rhs.0.clone(),
|
name: rhs.0.clone(),
|
||||||
ty: rhs.1 .0.into_mir(module_id),
|
ty: rhs.1 .0.into_mir(module_id),
|
||||||
meta: rhs.1 .1.as_meta(module_id),
|
meta: rhs.2.as_meta(module_id),
|
||||||
},
|
},
|
||||||
return_type: return_ty.0.into_mir(module_id),
|
return_type: return_ty.0.into_mir(module_id),
|
||||||
fn_kind: mir::FunctionDefinitionKind::Local(
|
fn_kind: mir::FunctionDefinitionKind::Local(
|
||||||
@ -168,6 +177,7 @@ impl ast::FunctionDefinition {
|
|||||||
}));
|
}));
|
||||||
mir::FunctionDefinition {
|
mir::FunctionDefinition {
|
||||||
name: signature.name.clone(),
|
name: signature.name.clone(),
|
||||||
|
documentation: signature.documentation.clone(),
|
||||||
linkage_name: None,
|
linkage_name: None,
|
||||||
is_pub: *is_pub,
|
is_pub: *is_pub,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
@ -179,6 +189,7 @@ impl ast::FunctionDefinition {
|
|||||||
parameters: params,
|
parameters: params,
|
||||||
kind: mir::FunctionDefinitionKind::Local(block.into_mir(module_id), (range).as_meta(module_id)),
|
kind: mir::FunctionDefinitionKind::Local(block.into_mir(module_id), (range).as_meta(module_id)),
|
||||||
source: Some(module_id),
|
source: Some(module_id),
|
||||||
|
signature_meta: signature.range.as_meta(module_id),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -226,8 +237,9 @@ impl ast::Block {
|
|||||||
);
|
);
|
||||||
let let_statement = mir::Statement(
|
let let_statement = mir::Statement(
|
||||||
StmtKind::Let(counter_var.clone(), true, start.process(module_id)),
|
StmtKind::Let(counter_var.clone(), true, start.process(module_id)),
|
||||||
counter_range.as_meta(module_id),
|
start.1.as_meta(module_id),
|
||||||
);
|
);
|
||||||
|
let statement_range = counter_range.clone() + start.1 + end.1 + block.2;
|
||||||
|
|
||||||
let set_new = mir::Statement(
|
let set_new = mir::Statement(
|
||||||
StmtKind::Set(
|
StmtKind::Set(
|
||||||
@ -253,8 +265,33 @@ impl ast::Block {
|
|||||||
),
|
),
|
||||||
counter_range.as_meta(module_id),
|
counter_range.as_meta(module_id),
|
||||||
);
|
);
|
||||||
let mut block = block.into_mir(module_id);
|
|
||||||
block.statements.push(set_new);
|
let mir_block = if let Some((ret_kind, ret_expr)) = &block.1 {
|
||||||
|
if *ret_kind == ReturnType::Soft {
|
||||||
|
if let Some(ret_expr) = ret_expr {
|
||||||
|
let mir_ret = ret_expr.process(module_id);
|
||||||
|
let mut clone = block.clone();
|
||||||
|
clone.1 = None;
|
||||||
|
let mut mir_block = clone.into_mir(module_id);
|
||||||
|
mir_block
|
||||||
|
.statements
|
||||||
|
.push(mir::Statement(StmtKind::Expression(mir_ret.clone()), mir_ret.1));
|
||||||
|
mir_block.statements.push(set_new);
|
||||||
|
mir_block
|
||||||
|
} else {
|
||||||
|
let mut mir_block = block.into_mir(module_id);
|
||||||
|
mir_block.statements.push(set_new);
|
||||||
|
mir_block
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
block.into_mir(module_id)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let mut mir_block = block.into_mir(module_id);
|
||||||
|
mir_block.statements.push(set_new);
|
||||||
|
mir_block
|
||||||
|
};
|
||||||
|
|
||||||
let while_statement = mir::Statement(
|
let while_statement = mir::Statement(
|
||||||
StmtKind::While(WhileStatement {
|
StmtKind::While(WhileStatement {
|
||||||
condition: mir::Expression(
|
condition: mir::Expression(
|
||||||
@ -267,23 +304,23 @@ impl ast::Block {
|
|||||||
Box::new(end.process(module_id)),
|
Box::new(end.process(module_id)),
|
||||||
mir::TypeKind::Vague(mir::VagueType::Unknown),
|
mir::TypeKind::Vague(mir::VagueType::Unknown),
|
||||||
),
|
),
|
||||||
counter_range.as_meta(module_id),
|
end.1.as_meta(module_id),
|
||||||
),
|
),
|
||||||
block,
|
block: mir_block.clone(),
|
||||||
meta: self.2.as_meta(module_id),
|
meta: (*counter_range + end.1 + block.2).as_meta(module_id),
|
||||||
}),
|
}),
|
||||||
self.2.as_meta(module_id),
|
(*counter_range + end.1 + block.2).as_meta(module_id),
|
||||||
);
|
);
|
||||||
|
|
||||||
let inner_scope = StmtKind::Expression(mir::Expression(
|
let inner_scope = StmtKind::Expression(mir::Expression(
|
||||||
mir::ExprKind::Block(mir::Block {
|
mir::ExprKind::Block(mir::Block {
|
||||||
statements: vec![let_statement, while_statement],
|
statements: vec![let_statement, while_statement],
|
||||||
return_expression: None,
|
return_expression: None,
|
||||||
meta: counter_range.as_meta(module_id) + end.1.as_meta(module_id),
|
meta: statement_range.as_meta(module_id),
|
||||||
}),
|
}),
|
||||||
counter_range.as_meta(module_id) + end.1.as_meta(module_id),
|
statement_range.as_meta(module_id),
|
||||||
));
|
));
|
||||||
(inner_scope, self.2)
|
(inner_scope, statement_range)
|
||||||
}
|
}
|
||||||
ast::BlockLevelStatement::WhileLoop(expression, block) => (
|
ast::BlockLevelStatement::WhileLoop(expression, block) => (
|
||||||
StmtKind::While(WhileStatement {
|
StmtKind::While(WhileStatement {
|
||||||
@ -291,7 +328,7 @@ impl ast::Block {
|
|||||||
block: block.into_mir(module_id),
|
block: block.into_mir(module_id),
|
||||||
meta: self.2.as_meta(module_id),
|
meta: self.2.as_meta(module_id),
|
||||||
}),
|
}),
|
||||||
self.2,
|
expression.1 + block.2,
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -371,7 +408,7 @@ impl ast::Expression {
|
|||||||
Box::new(idx_expr.process(module_id)),
|
Box::new(idx_expr.process(module_id)),
|
||||||
),
|
),
|
||||||
ast::ExpressionKind::StructExpression(struct_init) => mir::ExprKind::Struct(
|
ast::ExpressionKind::StructExpression(struct_init) => mir::ExprKind::Struct(
|
||||||
struct_init.name.clone(),
|
CustomTypeKey(struct_init.name.clone(), module_id),
|
||||||
struct_init
|
struct_init
|
||||||
.fields
|
.fields
|
||||||
.iter()
|
.iter()
|
||||||
@ -546,7 +583,7 @@ impl ast::TypeKind {
|
|||||||
}
|
}
|
||||||
ast::TypeKind::Ptr(type_kind) => mir::TypeKind::UserPtr(Box::new(type_kind.clone().into_mir(source_mod))),
|
ast::TypeKind::Ptr(type_kind) => mir::TypeKind::UserPtr(Box::new(type_kind.clone().into_mir(source_mod))),
|
||||||
ast::TypeKind::F16 => mir::TypeKind::F16,
|
ast::TypeKind::F16 => mir::TypeKind::F16,
|
||||||
ast::TypeKind::F32B => mir::TypeKind::F32B,
|
ast::TypeKind::F32B => mir::TypeKind::F16B,
|
||||||
ast::TypeKind::F32 => mir::TypeKind::F32,
|
ast::TypeKind::F32 => mir::TypeKind::F32,
|
||||||
ast::TypeKind::F64 => mir::TypeKind::F64,
|
ast::TypeKind::F64 => mir::TypeKind::F64,
|
||||||
ast::TypeKind::F80 => mir::TypeKind::F80,
|
ast::TypeKind::F80 => mir::TypeKind::F80,
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
//! Contains relevant code for parsing tokens received from
|
//! Contains relevant code for parsing tokens received from
|
||||||
//! Lexing/Tokenizing-stage.
|
//! Lexing/Tokenizing-stage.
|
||||||
|
|
||||||
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::parse::Parse,
|
ast::parse::Parse,
|
||||||
lexer::{FullToken, Token},
|
lexer::{FullToken, Token},
|
||||||
@ -12,6 +14,7 @@ use crate::{
|
|||||||
pub struct TokenStream<'a, 'b> {
|
pub struct TokenStream<'a, 'b> {
|
||||||
ref_position: Option<&'b mut usize>,
|
ref_position: Option<&'b mut usize>,
|
||||||
tokens: &'a [FullToken],
|
tokens: &'a [FullToken],
|
||||||
|
errors: Rc<RefCell<Vec<Error>>>,
|
||||||
pub position: usize,
|
pub position: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,6 +23,7 @@ impl<'a, 'b> TokenStream<'a, 'b> {
|
|||||||
TokenStream {
|
TokenStream {
|
||||||
ref_position: None,
|
ref_position: None,
|
||||||
tokens,
|
tokens,
|
||||||
|
errors: Rc::new(RefCell::new(Vec::new())),
|
||||||
position: 0,
|
position: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -38,6 +42,16 @@ impl<'a, 'b> TokenStream<'a, 'b> {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns expected-error for the next token in-line. Useful in conjunction
|
||||||
|
/// with [`TokenStream::peek`]
|
||||||
|
pub fn expected_err_nonfatal<T: Into<String>>(&mut self, expected: T) {
|
||||||
|
let err = match self.expected_err(expected) {
|
||||||
|
Ok(e) => e,
|
||||||
|
Err(e) => e,
|
||||||
|
};
|
||||||
|
self.errors.borrow_mut().push(err);
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns expected-error for the previous token that was already consumed.
|
/// Returns expected-error for the previous token that was already consumed.
|
||||||
/// Useful in conjunction with [`TokenStream::next`]
|
/// Useful in conjunction with [`TokenStream::next`]
|
||||||
pub fn expecting_err<T: Into<String>>(&mut self, expected: T) -> Result<Error, Error> {
|
pub fn expecting_err<T: Into<String>>(&mut self, expected: T) -> Result<Error, Error> {
|
||||||
@ -50,6 +64,16 @@ impl<'a, 'b> TokenStream<'a, 'b> {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns expected-error for the previous token that was already consumed.
|
||||||
|
/// Useful in conjunction with [`TokenStream::next`]
|
||||||
|
pub fn expecting_err_nonfatal<T: Into<String>>(&mut self, expected: T) {
|
||||||
|
let err = match self.expecting_err(expected) {
|
||||||
|
Ok(e) => e,
|
||||||
|
Err(e) => e,
|
||||||
|
};
|
||||||
|
self.errors.borrow_mut().push(err);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn expect(&mut self, token: Token) -> Result<(), Error> {
|
pub fn expect(&mut self, token: Token) -> Result<(), Error> {
|
||||||
if let (pos, Some(peeked)) = self.next_token(self.position) {
|
if let (pos, Some(peeked)) = self.next_token(self.position) {
|
||||||
if token == peeked.token {
|
if token == peeked.token {
|
||||||
@ -63,6 +87,42 @@ impl<'a, 'b> TokenStream<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn find_documentation(&mut self) -> Option<String> {
|
||||||
|
let mut from = self.position;
|
||||||
|
let mut documentation = None;
|
||||||
|
while let Some(token) = self.tokens.get(from) {
|
||||||
|
if matches!(token.token, Token::Whitespace(_) | Token::Comment(_) | Token::Doc(_)) {
|
||||||
|
from += 1;
|
||||||
|
if let Token::Doc(doctext) = &token.token {
|
||||||
|
documentation = Some(
|
||||||
|
match documentation {
|
||||||
|
Some(t) => t + " ",
|
||||||
|
None => String::new(),
|
||||||
|
} + doctext.trim(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
documentation
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn expect_nonfatal(&mut self, token: Token) -> Result<(), ()> {
|
||||||
|
if let (pos, Some(peeked)) = self.next_token(self.position) {
|
||||||
|
if token == peeked.token {
|
||||||
|
self.position = pos + 1;
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
self.expecting_err_nonfatal(token);
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.expecting_err_nonfatal(token);
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn next(&mut self) -> Option<Token> {
|
pub fn next(&mut self) -> Option<Token> {
|
||||||
let (position, token) = self.next_token(self.position);
|
let (position, token) = self.next_token(self.position);
|
||||||
self.position = position + 1;
|
self.position = position + 1;
|
||||||
@ -147,6 +207,7 @@ impl<'a, 'b> TokenStream<'a, 'b> {
|
|||||||
let clone = TokenStream {
|
let clone = TokenStream {
|
||||||
ref_position: Some(&mut ref_pos),
|
ref_position: Some(&mut ref_pos),
|
||||||
tokens: self.tokens,
|
tokens: self.tokens,
|
||||||
|
errors: self.errors.clone(),
|
||||||
position,
|
position,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -159,6 +220,29 @@ impl<'a, 'b> TokenStream<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse_with<T, U>(&mut self, fun: T) -> Result<U, Error>
|
||||||
|
where
|
||||||
|
T: FnOnce(TokenStream) -> Result<U, Error>,
|
||||||
|
{
|
||||||
|
let mut ref_pos = self.position;
|
||||||
|
|
||||||
|
let position = self.position;
|
||||||
|
let clone = TokenStream {
|
||||||
|
ref_position: Some(&mut ref_pos),
|
||||||
|
tokens: self.tokens,
|
||||||
|
errors: self.errors.clone(),
|
||||||
|
position,
|
||||||
|
};
|
||||||
|
|
||||||
|
match fun(clone) {
|
||||||
|
Ok(res) => {
|
||||||
|
self.position = ref_pos.max(self.position);
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_range(&self) -> Option<TokenRange> {
|
pub fn get_range(&self) -> Option<TokenRange> {
|
||||||
self.ref_position.as_ref().map(|ref_pos| TokenRange {
|
self.ref_position.as_ref().map(|ref_pos| TokenRange {
|
||||||
start: **ref_pos,
|
start: **ref_pos,
|
||||||
@ -175,10 +259,18 @@ impl<'a, 'b> TokenStream<'a, 'b> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets range of the previous token only.
|
||||||
|
pub fn get_range_prev_curr(&self) -> Option<TokenRange> {
|
||||||
|
Some(TokenRange {
|
||||||
|
start: self.previous_token(self.position).0,
|
||||||
|
end: self.previous_token(self.position).0,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn previous_token(&self, mut from: usize) -> (usize, Option<&'a FullToken>) {
|
fn previous_token(&self, mut from: usize) -> (usize, Option<&'a FullToken>) {
|
||||||
from -= 1;
|
from -= 1;
|
||||||
while let Some(token) = self.tokens.get(from) {
|
while let Some(token) = self.tokens.get(from) {
|
||||||
if matches!(token.token, Token::Whitespace(_) | Token::Comment(_)) {
|
if matches!(token.token, Token::Whitespace(_) | Token::Comment(_) | Token::Doc(_)) {
|
||||||
from -= 1;
|
from -= 1;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@ -189,7 +281,7 @@ impl<'a, 'b> TokenStream<'a, 'b> {
|
|||||||
|
|
||||||
fn next_token(&self, mut from: usize) -> (usize, Option<&'a FullToken>) {
|
fn next_token(&self, mut from: usize) -> (usize, Option<&'a FullToken>) {
|
||||||
while let Some(token) = self.tokens.get(from) {
|
while let Some(token) = self.tokens.get(from) {
|
||||||
if matches!(token.token, Token::Whitespace(_) | Token::Comment(_)) {
|
if matches!(token.token, Token::Whitespace(_) | Token::Comment(_) | Token::Doc(_)) {
|
||||||
from += 1;
|
from += 1;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@ -197,6 +289,22 @@ impl<'a, 'b> TokenStream<'a, 'b> {
|
|||||||
}
|
}
|
||||||
(from, self.tokens.get(from))
|
(from, self.tokens.get(from))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn errors(&self) -> Vec<Error> {
|
||||||
|
self.errors.borrow().clone().clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn next_is_whitespace(&self) -> bool {
|
||||||
|
if let Some(token) = self.tokens.get(self.position) {
|
||||||
|
if let Token::Whitespace(_) = token.token {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for TokenStream<'_, '_> {
|
impl Drop for TokenStream<'_, '_> {
|
||||||
|
|||||||
@ -5,7 +5,7 @@ use reid_lib::{
|
|||||||
Block, Instr,
|
Block, Instr,
|
||||||
};
|
};
|
||||||
|
|
||||||
use mir::{CustomTypeKey, FunctionCall, FunctionDefinitionKind, IfExpression, TypeKind, WhileStatement};
|
use mir::{CustomTypeKey, FunctionDefinitionKind, IfExpression, TypeKind, WhileStatement};
|
||||||
|
|
||||||
use crate::mir::{self, FunctionParam, Metadata, SourceModuleId};
|
use crate::mir::{self, FunctionParam, Metadata, SourceModuleId};
|
||||||
|
|
||||||
@ -151,11 +151,11 @@ impl mir::Expression {
|
|||||||
allocated.extend(expression.allocate(scope));
|
allocated.extend(expression.allocate(scope));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mir::ExprKind::Struct(name, items) => {
|
mir::ExprKind::Struct(key, items) => {
|
||||||
let (_, ty) = self.return_type(&Default::default(), scope.mod_id).unwrap();
|
let (_, ty) = self.return_type(&Default::default(), scope.mod_id).unwrap();
|
||||||
let allocation = scope
|
let allocation = scope
|
||||||
.block
|
.block
|
||||||
.build_named(name, Instr::Alloca(ty.get_type(scope.type_values)))
|
.build_named(key.0.clone(), Instr::Alloca(ty.get_type(scope.type_values)))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
allocated.push(Allocation(self.1, ty, allocation));
|
allocated.push(Allocation(self.1, ty, allocation));
|
||||||
|
|
||||||
|
|||||||
@ -3,8 +3,8 @@ use reid_lib::{builder::InstructionValue, CmpPredicate, ConstValueKind, Instr, T
|
|||||||
use crate::{
|
use crate::{
|
||||||
codegen::{ErrorKind, StackValueKind},
|
codegen::{ErrorKind, StackValueKind},
|
||||||
mir::{
|
mir::{
|
||||||
BinaryOperator, BinopDefinition, CmpOperator, FunctionDefinition, FunctionDefinitionKind, FunctionParam,
|
implement::TypeCategory, BinaryOperator, BinopDefinition, CmpOperator, FunctionDefinition,
|
||||||
TypeKind,
|
FunctionDefinitionKind, FunctionParam, TypeKind,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -26,21 +26,58 @@ const INTEGERS: [TypeKind; 10] = [
|
|||||||
const FLOATS: [TypeKind; 7] = [
|
const FLOATS: [TypeKind; 7] = [
|
||||||
TypeKind::F16,
|
TypeKind::F16,
|
||||||
TypeKind::F32,
|
TypeKind::F32,
|
||||||
TypeKind::F32B,
|
TypeKind::F16B,
|
||||||
TypeKind::F64,
|
TypeKind::F64,
|
||||||
TypeKind::F80,
|
TypeKind::F80,
|
||||||
TypeKind::F128,
|
TypeKind::F128,
|
||||||
TypeKind::F128PPC,
|
TypeKind::F128PPC,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
|
||||||
|
pub enum LLVMIntrinsicKind {
|
||||||
|
Max(TypeKind),
|
||||||
|
Min(TypeKind),
|
||||||
|
Abs(TypeKind),
|
||||||
|
Memcpy(TypeKind),
|
||||||
|
Sqrt(TypeKind),
|
||||||
|
PowI(TypeKind, TypeKind),
|
||||||
|
Pow(TypeKind),
|
||||||
|
Sin(TypeKind),
|
||||||
|
Cos(TypeKind),
|
||||||
|
Tan(TypeKind),
|
||||||
|
ASin(TypeKind),
|
||||||
|
ACos(TypeKind),
|
||||||
|
ATan(TypeKind),
|
||||||
|
ATan2(TypeKind),
|
||||||
|
SinH(TypeKind),
|
||||||
|
CosH(TypeKind),
|
||||||
|
TanH(TypeKind),
|
||||||
|
Log(TypeKind),
|
||||||
|
Log2(TypeKind),
|
||||||
|
Log10(TypeKind),
|
||||||
|
Copysign(TypeKind),
|
||||||
|
Floor(TypeKind),
|
||||||
|
Ceil(TypeKind),
|
||||||
|
Trunc(TypeKind),
|
||||||
|
RoundEven(TypeKind),
|
||||||
|
Round(TypeKind),
|
||||||
|
}
|
||||||
|
|
||||||
const INTRINSIC_IDENT: &str = "reid.intrinsic";
|
const INTRINSIC_IDENT: &str = "reid.intrinsic";
|
||||||
const MALLOC_IDENT: &str = "malloc";
|
const MALLOC_IDENT: &str = "malloc";
|
||||||
|
|
||||||
|
macro_rules! doc {
|
||||||
|
($str:expr) => {
|
||||||
|
Some($str.to_string())
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub fn form_intrinsics() -> Vec<FunctionDefinition> {
|
pub fn form_intrinsics() -> Vec<FunctionDefinition> {
|
||||||
let mut intrinsics = Vec::new();
|
let mut intrinsics = Vec::new();
|
||||||
|
|
||||||
intrinsics.push(FunctionDefinition {
|
intrinsics.push(FunctionDefinition {
|
||||||
name: MALLOC_IDENT.to_owned(),
|
name: MALLOC_IDENT.to_owned(),
|
||||||
|
documentation: doc!("Allocates `size` bytes and returns a `u8`-pointer."),
|
||||||
linkage_name: Some("malloc".to_owned()),
|
linkage_name: Some("malloc".to_owned()),
|
||||||
is_pub: false,
|
is_pub: false,
|
||||||
is_imported: true,
|
is_imported: true,
|
||||||
@ -52,71 +89,368 @@ pub fn form_intrinsics() -> Vec<FunctionDefinition> {
|
|||||||
}],
|
}],
|
||||||
kind: FunctionDefinitionKind::Extern(false),
|
kind: FunctionDefinitionKind::Extern(false),
|
||||||
source: None,
|
source: None,
|
||||||
|
signature_meta: Default::default(),
|
||||||
|
});
|
||||||
|
|
||||||
|
intrinsics
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn simple_intrinsic<T: Into<String> + Clone>(
|
||||||
|
name: T,
|
||||||
|
doc: T,
|
||||||
|
params: Vec<T>,
|
||||||
|
ret: TypeKind,
|
||||||
|
intrisic: LLVMIntrinsicKind,
|
||||||
|
) -> FunctionDefinition {
|
||||||
|
FunctionDefinition {
|
||||||
|
name: name.into(),
|
||||||
|
documentation: Some(doc.into()),
|
||||||
|
linkage_name: None,
|
||||||
|
is_pub: true,
|
||||||
|
is_imported: false,
|
||||||
|
return_type: ret.clone(),
|
||||||
|
parameters: params
|
||||||
|
.iter()
|
||||||
|
.map(|p| FunctionParam::from(p.clone(), ret.clone()))
|
||||||
|
.collect(),
|
||||||
|
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicLLVM(intrisic, ret.clone()))),
|
||||||
|
source: None,
|
||||||
|
signature_meta: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
|
||||||
|
let mut intrinsics = Vec::new();
|
||||||
|
if let TypeKind::Array(_, len) = ty {
|
||||||
|
intrinsics.push(FunctionDefinition {
|
||||||
|
name: "length".to_owned(),
|
||||||
|
documentation: doc!("Returns the length of this given array"),
|
||||||
|
linkage_name: None,
|
||||||
|
is_pub: true,
|
||||||
|
is_imported: false,
|
||||||
|
return_type: TypeKind::U64,
|
||||||
|
parameters: vec![FunctionParam {
|
||||||
|
name: String::from("self"),
|
||||||
|
ty: TypeKind::Borrow(Box::new(ty.clone()), false),
|
||||||
|
meta: Default::default(),
|
||||||
|
}],
|
||||||
|
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicConst(*len))),
|
||||||
|
source: None,
|
||||||
|
signature_meta: Default::default(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if ty.category() == TypeCategory::Real {
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"sqrt",
|
||||||
|
"Calculates the square-root of `value`",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::Sqrt(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"sin",
|
||||||
|
"Calculates sine of `value`",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::Sin(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"cos",
|
||||||
|
"Calculates cosine of `value`",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::Cos(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"tan",
|
||||||
|
"Calculates tangent of `value`",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::Tan(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"sinh",
|
||||||
|
"Calculates hyperbolic sine of `value`",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::SinH(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"cosh",
|
||||||
|
"Calculates hyperbolic cosine of `value`",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::CosH(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"tanh",
|
||||||
|
"Calculates hyperbolic tangent of `value`",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::TanH(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"asin",
|
||||||
|
"Calculates arcsine of `value`",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::ASin(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"acos",
|
||||||
|
"Calculates arccosine of `value`",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::ACos(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"atan",
|
||||||
|
"Calculates arctangent of `value`",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::ATan(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"atan2",
|
||||||
|
"Calculates 2-argument arctangent of `value`",
|
||||||
|
vec!["self", "other"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::ATan2(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"log",
|
||||||
|
"Returns logₑ of `value`",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::Log(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"log2",
|
||||||
|
"Returns log₂ of `value`",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::Log2(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"log10",
|
||||||
|
"Returns log₁₀ of `value`",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::Log10(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"floor",
|
||||||
|
"Rounds `value` towards negative infinity.",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::Floor(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"ceil",
|
||||||
|
"Rounds `value` towards positive infinity.",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::Ceil(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"trunc",
|
||||||
|
"Truncates `value` to the integer nearest to `0`.",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::Trunc(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"round",
|
||||||
|
"Rounds `value` to the closest even integer.",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::Round(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"even",
|
||||||
|
"Rounds `value` to the closest even integer.",
|
||||||
|
vec!["self"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::RoundEven(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"pow",
|
||||||
|
"Returns `value` raised to the exponent of `exponent`.",
|
||||||
|
vec!["self", "exponent"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::Pow(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(FunctionDefinition {
|
||||||
|
name: "powi".to_owned(),
|
||||||
|
documentation: doc!("Returns `value` raised to the exponent of `exponent`."),
|
||||||
|
linkage_name: None,
|
||||||
|
is_pub: true,
|
||||||
|
is_imported: false,
|
||||||
|
return_type: ty.clone(),
|
||||||
|
parameters: vec![
|
||||||
|
FunctionParam {
|
||||||
|
name: String::from("self"),
|
||||||
|
ty: ty.clone(),
|
||||||
|
meta: Default::default(),
|
||||||
|
},
|
||||||
|
FunctionParam {
|
||||||
|
name: String::from("exponent"),
|
||||||
|
ty: TypeKind::U32,
|
||||||
|
meta: Default::default(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicLLVM(
|
||||||
|
LLVMIntrinsicKind::PowI(ty.clone(), TypeKind::U32),
|
||||||
|
ty.clone(),
|
||||||
|
))),
|
||||||
|
source: None,
|
||||||
|
signature_meta: Default::default(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
match ty.category() {
|
||||||
|
TypeCategory::Integer | TypeCategory::Real | TypeCategory::Bool => {
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"max",
|
||||||
|
"Returns the larger of `a` and `b`.",
|
||||||
|
vec!["self", "other"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::Max(ty.clone()),
|
||||||
|
));
|
||||||
|
intrinsics.push(simple_intrinsic(
|
||||||
|
"min",
|
||||||
|
"Returns the smaller of `a` and `b`.",
|
||||||
|
vec!["self", "other"],
|
||||||
|
ty.clone(),
|
||||||
|
LLVMIntrinsicKind::Min(ty.clone()),
|
||||||
|
));
|
||||||
|
if ty.signed() {
|
||||||
|
intrinsics.push(FunctionDefinition {
|
||||||
|
name: "abs".to_owned(),
|
||||||
|
documentation: doc!("Returns the absolute value of `value`."),
|
||||||
|
linkage_name: None,
|
||||||
|
is_pub: true,
|
||||||
|
is_imported: false,
|
||||||
|
return_type: ty.clone(),
|
||||||
|
parameters: vec![FunctionParam {
|
||||||
|
name: String::from("self"),
|
||||||
|
ty: ty.clone(),
|
||||||
|
meta: Default::default(),
|
||||||
|
}],
|
||||||
|
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicSimpleUnaryInstr({
|
||||||
|
let ty = ty.clone();
|
||||||
|
|scope, param| {
|
||||||
|
let intrinsic = scope.get_intrinsic(LLVMIntrinsicKind::Abs(ty));
|
||||||
|
let constant = scope.block.build(Instr::Constant(ConstValueKind::Bool(false))).unwrap();
|
||||||
|
let value = scope
|
||||||
|
.block
|
||||||
|
.build(Instr::FunctionCall(intrinsic, vec![param, constant]))
|
||||||
|
.unwrap();
|
||||||
|
value
|
||||||
|
}
|
||||||
|
}))),
|
||||||
|
source: None,
|
||||||
|
signature_meta: Default::default(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
intrinsics.push(FunctionDefinition {
|
||||||
|
name: "sizeof".to_owned(),
|
||||||
|
documentation: doc!("Simply returns the size of type `T` in bytes."),
|
||||||
|
linkage_name: None,
|
||||||
|
is_pub: true,
|
||||||
|
is_imported: false,
|
||||||
|
return_type: TypeKind::U64,
|
||||||
|
parameters: Vec::new(),
|
||||||
|
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicSizeOf(ty.clone()))),
|
||||||
|
source: None,
|
||||||
|
signature_meta: Default::default(),
|
||||||
|
});
|
||||||
|
intrinsics.push(FunctionDefinition {
|
||||||
|
name: "malloc".to_owned(),
|
||||||
|
documentation: doc!("Allocates `T::sizeof() * size` bytes and returns a pointer to `T`."),
|
||||||
|
linkage_name: None,
|
||||||
|
is_pub: true,
|
||||||
|
is_imported: false,
|
||||||
|
return_type: TypeKind::UserPtr(Box::new(ty.clone())),
|
||||||
|
parameters: vec![FunctionParam {
|
||||||
|
name: String::from("size"),
|
||||||
|
ty: TypeKind::U64,
|
||||||
|
meta: Default::default(),
|
||||||
|
}],
|
||||||
|
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicMalloc(ty.clone()))),
|
||||||
|
source: None,
|
||||||
|
signature_meta: Default::default(),
|
||||||
|
});
|
||||||
|
|
||||||
|
intrinsics.push(FunctionDefinition {
|
||||||
|
name: "memcpy".to_owned(),
|
||||||
|
documentation: doc!(
|
||||||
|
"Copies `T::sizeof() * size` bytes from pointer `source` to pointer
|
||||||
|
`destination`."
|
||||||
|
),
|
||||||
|
linkage_name: None,
|
||||||
|
is_pub: true,
|
||||||
|
is_imported: false,
|
||||||
|
return_type: TypeKind::Void,
|
||||||
|
parameters: vec![
|
||||||
|
FunctionParam {
|
||||||
|
name: String::from("destination"),
|
||||||
|
ty: TypeKind::UserPtr(Box::new(ty.clone())),
|
||||||
|
meta: Default::default(),
|
||||||
|
},
|
||||||
|
FunctionParam {
|
||||||
|
name: String::from("source"),
|
||||||
|
ty: TypeKind::UserPtr(Box::new(ty.clone())),
|
||||||
|
meta: Default::default(),
|
||||||
|
},
|
||||||
|
FunctionParam {
|
||||||
|
name: String::from("length"),
|
||||||
|
ty: TypeKind::U64,
|
||||||
|
meta: Default::default(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicMemcpy(ty.clone()))),
|
||||||
|
source: None,
|
||||||
|
signature_meta: Default::default(),
|
||||||
|
});
|
||||||
|
|
||||||
|
intrinsics.push(FunctionDefinition {
|
||||||
|
name: "null".to_owned(),
|
||||||
|
documentation: doc!("Returns a null-pointer of type `T`."),
|
||||||
|
linkage_name: None,
|
||||||
|
is_pub: true,
|
||||||
|
is_imported: false,
|
||||||
|
return_type: TypeKind::UserPtr(Box::new(ty.clone())),
|
||||||
|
parameters: Vec::new(),
|
||||||
|
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicNullPtr(ty.clone()))),
|
||||||
|
source: None,
|
||||||
|
signature_meta: Default::default(),
|
||||||
|
});
|
||||||
|
|
||||||
|
intrinsics.push(FunctionDefinition {
|
||||||
|
name: "is_null".to_owned(),
|
||||||
|
documentation: doc!("Returns a boolean representing if `val` is a nullptr or not."),
|
||||||
|
linkage_name: None,
|
||||||
|
is_pub: true,
|
||||||
|
is_imported: false,
|
||||||
|
return_type: TypeKind::Bool,
|
||||||
|
parameters: vec![FunctionParam {
|
||||||
|
name: "value".to_string(),
|
||||||
|
ty: TypeKind::UserPtr(Box::new(ty.clone())),
|
||||||
|
meta: Default::default(),
|
||||||
|
}],
|
||||||
|
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicIsNull)),
|
||||||
|
source: None,
|
||||||
|
signature_meta: Default::default(),
|
||||||
});
|
});
|
||||||
|
|
||||||
intrinsics
|
intrinsics
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_intrinsic_assoc_func(ty: &TypeKind, name: &str) -> Option<FunctionDefinition> {
|
pub fn get_intrinsic_assoc_func(ty: &TypeKind, name: &str) -> Option<FunctionDefinition> {
|
||||||
if let TypeKind::Array(_, len) = ty {
|
get_intrinsic_assoc_functions(ty).into_iter().find(|f| f.name == name)
|
||||||
match name {
|
|
||||||
"length" => {
|
|
||||||
return Some(FunctionDefinition {
|
|
||||||
name: "length".to_owned(),
|
|
||||||
linkage_name: None,
|
|
||||||
is_pub: true,
|
|
||||||
is_imported: false,
|
|
||||||
return_type: TypeKind::U64,
|
|
||||||
parameters: vec![FunctionParam {
|
|
||||||
name: String::from("self"),
|
|
||||||
ty: TypeKind::Borrow(Box::new(ty.clone()), false),
|
|
||||||
meta: Default::default(),
|
|
||||||
}],
|
|
||||||
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicConst(*len))),
|
|
||||||
source: None,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match name {
|
|
||||||
"sizeof" => Some(FunctionDefinition {
|
|
||||||
name: "sizeof".to_owned(),
|
|
||||||
linkage_name: None,
|
|
||||||
is_pub: true,
|
|
||||||
is_imported: false,
|
|
||||||
return_type: TypeKind::U64,
|
|
||||||
parameters: Vec::new(),
|
|
||||||
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicSizeOf(ty.clone()))),
|
|
||||||
source: None,
|
|
||||||
}),
|
|
||||||
"malloc" => Some(FunctionDefinition {
|
|
||||||
name: "malloc".to_owned(),
|
|
||||||
linkage_name: None,
|
|
||||||
is_pub: true,
|
|
||||||
is_imported: false,
|
|
||||||
return_type: TypeKind::UserPtr(Box::new(ty.clone())),
|
|
||||||
parameters: vec![FunctionParam {
|
|
||||||
name: String::from("size"),
|
|
||||||
ty: TypeKind::U64,
|
|
||||||
meta: Default::default(),
|
|
||||||
}],
|
|
||||||
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicMalloc(ty.clone()))),
|
|
||||||
source: None,
|
|
||||||
}),
|
|
||||||
"null" => Some(FunctionDefinition {
|
|
||||||
name: "null".to_owned(),
|
|
||||||
linkage_name: None,
|
|
||||||
is_pub: true,
|
|
||||||
is_imported: false,
|
|
||||||
return_type: TypeKind::UserPtr(Box::new(ty.clone())),
|
|
||||||
parameters: Vec::new(),
|
|
||||||
kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicNullPtr(ty.clone()))),
|
|
||||||
source: None,
|
|
||||||
}),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn simple_binop_def<T: Clone + 'static>(op: BinaryOperator, ty: &TypeKind, fun: T) -> BinopDefinition
|
fn simple_binop_def<T: Clone + 'static>(op: BinaryOperator, ty: &TypeKind, fun: T) -> BinopDefinition
|
||||||
@ -136,7 +470,7 @@ where
|
|||||||
meta: Default::default(),
|
meta: Default::default(),
|
||||||
},
|
},
|
||||||
return_type: ty.clone(),
|
return_type: ty.clone(),
|
||||||
fn_kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicSimpleInstr(fun))),
|
fn_kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicSimpleBinaryInstr(fun))),
|
||||||
meta: Default::default(),
|
meta: Default::default(),
|
||||||
exported: false,
|
exported: false,
|
||||||
}
|
}
|
||||||
@ -159,7 +493,7 @@ where
|
|||||||
meta: Default::default(),
|
meta: Default::default(),
|
||||||
},
|
},
|
||||||
return_type: lhs.clone(),
|
return_type: lhs.clone(),
|
||||||
fn_kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicSimpleInstr(fun))),
|
fn_kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicSimpleBinaryInstr(fun))),
|
||||||
meta: Default::default(),
|
meta: Default::default(),
|
||||||
exported: false,
|
exported: false,
|
||||||
}
|
}
|
||||||
@ -280,17 +614,22 @@ pub fn form_intrinsic_binops() -> Vec<BinopDefinition> {
|
|||||||
intrinsics.push(simple_binop_def(Div, &ty, |scope, lhs, rhs| {
|
intrinsics.push(simple_binop_def(Div, &ty, |scope, lhs, rhs| {
|
||||||
scope.block.build(Instr::FDiv(lhs, rhs)).unwrap()
|
scope.block.build(Instr::FDiv(lhs, rhs)).unwrap()
|
||||||
}));
|
}));
|
||||||
intrinsics.push(simple_binop_def(Mod, &ty, |scope, lhs, rhs| {
|
intrinsics.push(simple_binop_def(Mod, &ty, {
|
||||||
let div = scope.block.build(Instr::FDiv(lhs, rhs)).unwrap();
|
let ty = ty.clone();
|
||||||
let mul = scope.block.build(Instr::Mul(rhs, div)).unwrap();
|
|scope, lhs, rhs| {
|
||||||
scope.block.build(Instr::Sub(lhs, mul)).unwrap()
|
let div = scope.block.build(Instr::FDiv(lhs, rhs)).unwrap();
|
||||||
}));
|
let fun = scope.get_intrinsic(LLVMIntrinsicKind::Trunc(ty));
|
||||||
intrinsics.push(boolean_binop_def(Cmp(CmpOperator::EQ), &ty, |scope, lhs, rhs| {
|
let div_truncated = scope.block.build(Instr::FunctionCall(fun, vec![div])).unwrap();
|
||||||
scope.block.build(Instr::FCmp(CmpPredicate::EQ, lhs, rhs)).unwrap()
|
let mul = scope.block.build(Instr::FMul(rhs, div_truncated)).unwrap();
|
||||||
|
scope.block.build(Instr::FSub(lhs, mul)).unwrap()
|
||||||
|
}
|
||||||
}));
|
}));
|
||||||
intrinsics.push(boolean_binop_def(Cmp(CmpOperator::NE), &ty, |scope, lhs, rhs| {
|
intrinsics.push(boolean_binop_def(Cmp(CmpOperator::NE), &ty, |scope, lhs, rhs| {
|
||||||
scope.block.build(Instr::FCmp(CmpPredicate::NE, lhs, rhs)).unwrap()
|
scope.block.build(Instr::FCmp(CmpPredicate::NE, lhs, rhs)).unwrap()
|
||||||
}));
|
}));
|
||||||
|
intrinsics.push(boolean_binop_def(Cmp(CmpOperator::EQ), &ty, |scope, lhs, rhs| {
|
||||||
|
scope.block.build(Instr::FCmp(CmpPredicate::EQ, lhs, rhs)).unwrap()
|
||||||
|
}));
|
||||||
intrinsics.push(boolean_binop_def(Cmp(CmpOperator::GT), &ty, |scope, lhs, rhs| {
|
intrinsics.push(boolean_binop_def(Cmp(CmpOperator::GT), &ty, |scope, lhs, rhs| {
|
||||||
scope.block.build(Instr::FCmp(CmpPredicate::GT, lhs, rhs)).unwrap()
|
scope.block.build(Instr::FCmp(CmpPredicate::GT, lhs, rhs)).unwrap()
|
||||||
}));
|
}));
|
||||||
@ -336,12 +675,37 @@ macro_rules! intrinsic_debug {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct IntrinsicSimpleInstr<T>(T)
|
pub struct IntrinsicSimpleUnaryInstr<T>(T)
|
||||||
|
where
|
||||||
|
T: FnOnce(&mut Scope, InstructionValue) -> InstructionValue;
|
||||||
|
|
||||||
|
impl<T> std::fmt::Debug for IntrinsicSimpleUnaryInstr<T>
|
||||||
|
where
|
||||||
|
T: FnOnce(&mut Scope, InstructionValue) -> InstructionValue,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.debug_tuple("IntrinsicSimpleUnaryInstr").finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Clone> IntrinsicFunction for IntrinsicSimpleUnaryInstr<T>
|
||||||
|
where
|
||||||
|
T: FnOnce(&mut Scope, InstructionValue) -> InstructionValue,
|
||||||
|
{
|
||||||
|
fn codegen<'b, 'c>(&self, scope: &mut Scope<'b, 'c>, params: &[StackValue]) -> Result<StackValue, ErrorKind> {
|
||||||
|
let param = params.get(0).unwrap();
|
||||||
|
let instr = self.clone().0(scope, param.instr());
|
||||||
|
Ok(StackValue(StackValueKind::Literal(instr), param.1.clone()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct IntrinsicSimpleBinaryInstr<T>(T)
|
||||||
where
|
where
|
||||||
T: FnOnce(&mut Scope, InstructionValue, InstructionValue) -> InstructionValue;
|
T: FnOnce(&mut Scope, InstructionValue, InstructionValue) -> InstructionValue;
|
||||||
intrinsic_debug!(IntrinsicSimpleInstr<T>, "IntrinsicSimpleInstr");
|
intrinsic_debug!(IntrinsicSimpleBinaryInstr<T>, "IntrinsicSimpleBinaryInstr");
|
||||||
|
|
||||||
impl<T: Clone> IntrinsicFunction for IntrinsicSimpleInstr<T>
|
impl<T: Clone> IntrinsicFunction for IntrinsicSimpleBinaryInstr<T>
|
||||||
where
|
where
|
||||||
T: FnOnce(&mut Scope, InstructionValue, InstructionValue) -> InstructionValue,
|
T: FnOnce(&mut Scope, InstructionValue, InstructionValue) -> InstructionValue,
|
||||||
{
|
{
|
||||||
@ -385,6 +749,34 @@ impl IntrinsicFunction for IntrinsicSizeOf {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct IntrinsicMemcpy(TypeKind);
|
||||||
|
impl IntrinsicFunction for IntrinsicMemcpy {
|
||||||
|
fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, params: &[StackValue]) -> Result<StackValue, ErrorKind> {
|
||||||
|
let dest = params.get(0).unwrap();
|
||||||
|
let src = params.get(1).unwrap();
|
||||||
|
let length = params.get(2).unwrap();
|
||||||
|
let intrinsic = scope.get_intrinsic(LLVMIntrinsicKind::Memcpy(TypeKind::UserPtr(Box::new(self.0.clone()))));
|
||||||
|
|
||||||
|
let sizeof = scope
|
||||||
|
.block
|
||||||
|
.build(Instr::Constant(ConstValueKind::U64(
|
||||||
|
self.0.size_of(&scope.type_map) / 8,
|
||||||
|
)))
|
||||||
|
.unwrap();
|
||||||
|
let bytes = scope.block.build(Instr::Mul(sizeof, length.instr())).unwrap();
|
||||||
|
|
||||||
|
let params = vec![
|
||||||
|
dest.instr(),
|
||||||
|
src.instr(),
|
||||||
|
bytes,
|
||||||
|
scope.block.build(Instr::Constant(ConstValueKind::Bool(false))).unwrap(),
|
||||||
|
];
|
||||||
|
let value = scope.block.build(Instr::FunctionCall(intrinsic, params)).unwrap();
|
||||||
|
Ok(StackValue(StackValueKind::Literal(value), TypeKind::Void))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct IntrinsicMalloc(TypeKind);
|
pub struct IntrinsicMalloc(TypeKind);
|
||||||
impl IntrinsicFunction for IntrinsicMalloc {
|
impl IntrinsicFunction for IntrinsicMalloc {
|
||||||
@ -425,6 +817,17 @@ impl IntrinsicFunction for IntrinsicNullPtr {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct IntrinsicIsNull;
|
||||||
|
impl IntrinsicFunction for IntrinsicIsNull {
|
||||||
|
fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, params: &[StackValue]) -> Result<StackValue, ErrorKind> {
|
||||||
|
let val = params.get(0).unwrap().instr();
|
||||||
|
let instr = scope.block.build(Instr::IsNull(val)).unwrap();
|
||||||
|
Ok(StackValue(StackValueKind::Literal(instr), TypeKind::Bool))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct IntrinsicConst(u64);
|
pub struct IntrinsicConst(u64);
|
||||||
impl IntrinsicFunction for IntrinsicConst {
|
impl IntrinsicFunction for IntrinsicConst {
|
||||||
@ -434,6 +837,23 @@ impl IntrinsicFunction for IntrinsicConst {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct IntrinsicLLVM(LLVMIntrinsicKind, TypeKind);
|
||||||
|
impl IntrinsicFunction for IntrinsicLLVM {
|
||||||
|
fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, params: &[StackValue]) -> Result<StackValue, ErrorKind> {
|
||||||
|
let intrinsic = scope.get_intrinsic(self.0.clone());
|
||||||
|
let value = scope
|
||||||
|
.block
|
||||||
|
.build(Instr::FunctionCall(
|
||||||
|
intrinsic,
|
||||||
|
params.iter().map(|p| p.instr()).collect(),
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(StackValue(StackValueKind::Literal(value), self.1.clone()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// impl IntrinsicFunction for IntrinsicIAdd {
|
// impl IntrinsicFunction for IntrinsicIAdd {
|
||||||
// fn codegen<'ctx, 'a>(
|
// fn codegen<'ctx, 'a>(
|
||||||
// &self,
|
// &self,
|
||||||
|
|||||||
@ -1,4 +1,8 @@
|
|||||||
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
use std::{
|
||||||
|
cell::RefCell,
|
||||||
|
collections::{HashMap, HashSet},
|
||||||
|
rc::Rc,
|
||||||
|
};
|
||||||
|
|
||||||
use allocator::{Allocator, AllocatorScope};
|
use allocator::{Allocator, AllocatorScope};
|
||||||
use intrinsics::*;
|
use intrinsics::*;
|
||||||
@ -58,10 +62,12 @@ impl mir::Context {
|
|||||||
let mut modules = HashMap::new();
|
let mut modules = HashMap::new();
|
||||||
let mut modules_sorted = self.modules.iter().map(|(_, m)| m).collect::<Vec<_>>();
|
let mut modules_sorted = self.modules.iter().map(|(_, m)| m).collect::<Vec<_>>();
|
||||||
modules_sorted.sort_by(|m1, m2| m2.module_id.cmp(&m1.module_id));
|
modules_sorted.sort_by(|m1, m2| m2.module_id.cmp(&m1.module_id));
|
||||||
|
for module in &modules_sorted {
|
||||||
|
modules.insert(module.module_id, *module);
|
||||||
|
}
|
||||||
|
|
||||||
for module in &modules_sorted {
|
for module in &modules_sorted {
|
||||||
let codegen = module.codegen(context, modules.clone())?;
|
module.codegen(context, modules.clone())?;
|
||||||
modules.insert(module.module_id, codegen);
|
|
||||||
}
|
}
|
||||||
Ok(CodegenContext { context })
|
Ok(CodegenContext { context })
|
||||||
}
|
}
|
||||||
@ -69,7 +75,6 @@ impl mir::Context {
|
|||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct ModuleCodegen<'ctx> {
|
struct ModuleCodegen<'ctx> {
|
||||||
name: String,
|
|
||||||
module: Module<'ctx>,
|
module: Module<'ctx>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,11 +125,22 @@ impl mir::GlobalKind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_typekey(ty: &TypeKind) -> Option<CustomTypeKey> {
|
||||||
|
match ty {
|
||||||
|
TypeKind::Array(type_kind, _) => get_typekey(type_kind.as_ref()),
|
||||||
|
TypeKind::CustomType(custom_type_key) => Some(custom_type_key.clone()),
|
||||||
|
TypeKind::Borrow(type_kind, _) => get_typekey(type_kind.as_ref()),
|
||||||
|
TypeKind::UserPtr(type_kind) => get_typekey(type_kind.as_ref()),
|
||||||
|
TypeKind::CodegenPtr(type_kind) => get_typekey(type_kind.as_ref()),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl mir::Module {
|
impl mir::Module {
|
||||||
fn codegen<'ctx>(
|
fn codegen<'ctx>(
|
||||||
&'ctx self,
|
&'ctx self,
|
||||||
context: &'ctx Context,
|
context: &'ctx Context,
|
||||||
modules: HashMap<SourceModuleId, ModuleCodegen<'ctx>>,
|
modules: HashMap<SourceModuleId, &mir::Module>,
|
||||||
) -> Result<ModuleCodegen<'ctx>, ErrorKind> {
|
) -> Result<ModuleCodegen<'ctx>, ErrorKind> {
|
||||||
let mut module = context.module(&self.name, self.is_main);
|
let mut module = context.module(&self.name, self.is_main);
|
||||||
let tokens = &self.tokens;
|
let tokens = &self.tokens;
|
||||||
@ -177,10 +193,40 @@ impl mir::Module {
|
|||||||
insert_debug!(&TypeKind::Void);
|
insert_debug!(&TypeKind::Void);
|
||||||
insert_debug!(&TypeKind::Char);
|
insert_debug!(&TypeKind::Char);
|
||||||
|
|
||||||
let mut typedefs = self.typedefs.clone();
|
// Since we know by this point that no types are recursive, we can
|
||||||
typedefs.sort_by(|a, b| b.source_module.cmp(&a.source_module));
|
// somewhat easily sort the type-definitions such that we can process
|
||||||
|
// the ones with no depencencies first, and later the ones that depend
|
||||||
|
// on the earlier ones.
|
||||||
|
let mut typekeys_seen: HashSet<CustomTypeKey> = HashSet::new();
|
||||||
|
let mut typedefs_sorted: Vec<TypeDefinition> = Vec::new();
|
||||||
|
let mut typedefs_left = self.typedefs.clone();
|
||||||
|
while let Some(typedef) = typedefs_left.pop() {
|
||||||
|
let is_ok = match &typedef.kind {
|
||||||
|
TypeDefinitionKind::Struct(StructType(fields)) => {
|
||||||
|
let mut field_iter = fields.iter();
|
||||||
|
loop {
|
||||||
|
if let Some(field) = field_iter.next() {
|
||||||
|
if let Some(key) = get_typekey(&field.1) {
|
||||||
|
if !typekeys_seen.contains(&key) {
|
||||||
|
break false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
for typedef in typedefs {
|
if is_ok {
|
||||||
|
typekeys_seen.insert(CustomTypeKey(typedef.name.clone(), typedef.source_module));
|
||||||
|
typedefs_sorted.push(typedef);
|
||||||
|
} else {
|
||||||
|
typedefs_left.insert(0, typedef.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for typedef in typedefs_sorted {
|
||||||
let type_key = CustomTypeKey(typedef.name.clone(), typedef.source_module);
|
let type_key = CustomTypeKey(typedef.name.clone(), typedef.source_module);
|
||||||
type_map.insert(type_key.clone(), typedef.clone());
|
type_map.insert(type_key.clone(), typedef.clone());
|
||||||
|
|
||||||
@ -190,9 +236,6 @@ impl mir::Module {
|
|||||||
typedef.name.clone(),
|
typedef.name.clone(),
|
||||||
fields
|
fields
|
||||||
.iter()
|
.iter()
|
||||||
// TODO: Reorder custom-type definitions such that
|
|
||||||
// inner types get evaluated first. Otherwise this
|
|
||||||
// will cause a panic!
|
|
||||||
.map(|StructField(_, t, _)| t.get_type(&type_values))
|
.map(|StructField(_, t, _)| t.get_type(&type_values))
|
||||||
.collect(),
|
.collect(),
|
||||||
)))
|
)))
|
||||||
@ -287,11 +330,11 @@ impl mir::Module {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let is_main = self.is_main && function.name == "main";
|
let is_main = self.is_main && function.name == "main";
|
||||||
let module_prefix = if let Some(module) = function.source {
|
let module_prefix = if let Some(module_id) = function.source {
|
||||||
if module == self.module_id {
|
if module_id == self.module_id {
|
||||||
format!("reid.{}.", self.name)
|
format!("reid.{}.", self.name)
|
||||||
} else {
|
} else {
|
||||||
format!("reid.{}.", modules.get(&module).unwrap().name)
|
format!("reid.{}.", modules.get(&module_id).unwrap().name)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
format!("reid.intrinsic.")
|
format!("reid.intrinsic.")
|
||||||
@ -393,6 +436,7 @@ impl mir::Module {
|
|||||||
}),
|
}),
|
||||||
binops: &binops,
|
binops: &binops,
|
||||||
allocator: Rc::new(RefCell::new(allocator)),
|
allocator: Rc::new(RefCell::new(allocator)),
|
||||||
|
llvm_intrinsics: Rc::new(RefCell::new(HashMap::new())),
|
||||||
};
|
};
|
||||||
|
|
||||||
binop
|
binop
|
||||||
@ -471,6 +515,7 @@ impl mir::Module {
|
|||||||
globals: &globals,
|
globals: &globals,
|
||||||
binops: &binops,
|
binops: &binops,
|
||||||
allocator: Rc::new(RefCell::new(allocator)),
|
allocator: Rc::new(RefCell::new(allocator)),
|
||||||
|
llvm_intrinsics: Rc::new(RefCell::new(HashMap::new())),
|
||||||
};
|
};
|
||||||
|
|
||||||
mir_function
|
mir_function
|
||||||
@ -533,6 +578,7 @@ impl mir::Module {
|
|||||||
globals: &globals,
|
globals: &globals,
|
||||||
binops: &binops,
|
binops: &binops,
|
||||||
allocator: Rc::new(RefCell::new(allocator)),
|
allocator: Rc::new(RefCell::new(allocator)),
|
||||||
|
llvm_intrinsics: Rc::new(RefCell::new(HashMap::new())),
|
||||||
};
|
};
|
||||||
|
|
||||||
mir_function
|
mir_function
|
||||||
@ -556,10 +602,7 @@ impl mir::Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(ModuleCodegen {
|
Ok(ModuleCodegen { module })
|
||||||
name: self.name.clone(),
|
|
||||||
module,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1205,10 +1248,9 @@ impl mir::Expression {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mir::ExprKind::Struct(name, items) => {
|
mir::ExprKind::Struct(key, items) => {
|
||||||
let type_key = CustomTypeKey(name.clone(), scope.module_id);
|
|
||||||
let ty = Type::CustomType({
|
let ty = Type::CustomType({
|
||||||
let Some(a) = scope.type_values.get(&type_key) else {
|
let Some(a) = scope.type_values.get(&key) else {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
*a
|
*a
|
||||||
@ -1217,20 +1259,20 @@ impl mir::Expression {
|
|||||||
let TypeDefinition {
|
let TypeDefinition {
|
||||||
kind: TypeDefinitionKind::Struct(struct_ty),
|
kind: TypeDefinitionKind::Struct(struct_ty),
|
||||||
..
|
..
|
||||||
} = scope.types.get(scope.type_values.get(&type_key).unwrap()).unwrap();
|
} = scope.types.get(scope.type_values.get(&key).unwrap()).unwrap();
|
||||||
|
|
||||||
let indices = struct_ty.0.iter().enumerate();
|
let indices = struct_ty.0.iter().enumerate();
|
||||||
|
|
||||||
let load_n = format!("{}.load", name);
|
let load_n = format!("{}@{}.load", key.0, key.1);
|
||||||
|
|
||||||
let struct_ptr = scope
|
let struct_ptr = scope
|
||||||
.allocate(&self.1, &TypeKind::CustomType(type_key.clone()))
|
.allocate(&self.1, &TypeKind::CustomType(key.clone()))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.maybe_location(&mut scope.block, location.clone());
|
.maybe_location(&mut scope.block, location.clone());
|
||||||
|
|
||||||
for (field_n, exp, _) in items {
|
for (field_n, exp, _) in items {
|
||||||
let gep_n = format!("{}.{}.gep", name, field_n);
|
let gep_n = format!("{}@{}.{}.gep", key.0, key.1, field_n);
|
||||||
let store_n = format!("{}.{}.store", name, field_n);
|
let store_n = format!("{}@{}.{}.store", key.0, key.1, field_n);
|
||||||
let i = indices.clone().find(|(_, f)| f.0 == *field_n).unwrap().0;
|
let i = indices.clone().find(|(_, f)| f.0 == *field_n).unwrap().0;
|
||||||
|
|
||||||
let elem_ptr = scope
|
let elem_ptr = scope
|
||||||
@ -1251,7 +1293,7 @@ impl mir::Expression {
|
|||||||
|
|
||||||
Some(StackValue(
|
Some(StackValue(
|
||||||
StackValueKind::Literal(struct_val),
|
StackValueKind::Literal(struct_val),
|
||||||
TypeKind::CustomType(type_key),
|
TypeKind::CustomType(key.clone()),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
mir::ExprKind::Borrow(expr, mutable) => {
|
mir::ExprKind::Borrow(expr, mutable) => {
|
||||||
@ -1326,8 +1368,6 @@ impl mir::Expression {
|
|||||||
(val.1.clone(), type_kind.clone())
|
(val.1.clone(), type_kind.clone())
|
||||||
};
|
};
|
||||||
|
|
||||||
dbg!(&ty, type_kind);
|
|
||||||
|
|
||||||
match (&ty, type_kind) {
|
match (&ty, type_kind) {
|
||||||
(TypeKind::UserPtr(_), TypeKind::UserPtr(_)) => Some(StackValue(
|
(TypeKind::UserPtr(_), TypeKind::UserPtr(_)) => Some(StackValue(
|
||||||
val.0.derive(
|
val.0.derive(
|
||||||
|
|||||||
@ -1,24 +1,27 @@
|
|||||||
use std::{cell::RefCell, collections::HashMap, mem, rc::Rc};
|
use std::{cell::RefCell, collections::HashMap, mem, rc::Rc};
|
||||||
|
|
||||||
use reid_lib::{
|
use reid_lib::{
|
||||||
builder::{GlobalValue, InstructionValue, TypeValue},
|
builder::{FunctionValue, GlobalValue, InstructionValue, TypeValue},
|
||||||
debug_information::{DebugInformation, DebugLocation, DebugScopeValue, DebugTypeValue},
|
debug_information::{DebugInformation, DebugLocation, DebugScopeValue, DebugTypeValue},
|
||||||
|
intrinsics::LLVMIntrinsic,
|
||||||
Block, Context, Function, Instr, Module,
|
Block, Context, Function, Instr, Module,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
codegen::intrinsics::LLVMIntrinsicKind,
|
||||||
lexer::FullToken,
|
lexer::FullToken,
|
||||||
mir::{
|
mir::{
|
||||||
|
self,
|
||||||
pass::{AssociatedFunctionKey, BinopKey},
|
pass::{AssociatedFunctionKey, BinopKey},
|
||||||
CustomTypeKey, FunctionParam, Metadata, SourceModuleId, TypeDefinition, TypeKind,
|
CustomTypeKey, FunctionParam, Metadata, SourceModuleId, TypeDefinition, TypeKind,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{allocator::Allocator, ErrorKind, IntrinsicFunction, ModuleCodegen};
|
use super::{allocator::Allocator, ErrorKind, IntrinsicFunction};
|
||||||
|
|
||||||
pub struct Scope<'ctx, 'scope> {
|
pub struct Scope<'ctx, 'scope> {
|
||||||
pub(super) context: &'ctx Context,
|
pub(super) context: &'ctx Context,
|
||||||
pub(super) modules: &'scope HashMap<SourceModuleId, ModuleCodegen<'ctx>>,
|
pub(super) modules: &'scope HashMap<SourceModuleId, &'ctx mir::Module>,
|
||||||
pub(super) tokens: &'ctx Vec<FullToken>,
|
pub(super) tokens: &'ctx Vec<FullToken>,
|
||||||
pub(super) module: &'ctx Module<'ctx>,
|
pub(super) module: &'ctx Module<'ctx>,
|
||||||
pub(super) module_id: SourceModuleId,
|
pub(super) module_id: SourceModuleId,
|
||||||
@ -34,6 +37,7 @@ pub struct Scope<'ctx, 'scope> {
|
|||||||
pub(super) globals: &'scope HashMap<String, GlobalValue>,
|
pub(super) globals: &'scope HashMap<String, GlobalValue>,
|
||||||
pub(super) debug: Option<Debug<'ctx>>,
|
pub(super) debug: Option<Debug<'ctx>>,
|
||||||
pub(super) allocator: Rc<RefCell<Allocator>>,
|
pub(super) allocator: Rc<RefCell<Allocator>>,
|
||||||
|
pub(super) llvm_intrinsics: Rc<RefCell<HashMap<LLVMIntrinsicKind, FunctionValue>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ctx, 'a> Scope<'ctx, 'a> {
|
impl<'ctx, 'a> Scope<'ctx, 'a> {
|
||||||
@ -56,6 +60,7 @@ impl<'ctx, 'a> Scope<'ctx, 'a> {
|
|||||||
allocator: self.allocator.clone(),
|
allocator: self.allocator.clone(),
|
||||||
globals: self.globals,
|
globals: self.globals,
|
||||||
binops: self.binops,
|
binops: self.binops,
|
||||||
|
llvm_intrinsics: self.llvm_intrinsics.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,6 +79,49 @@ impl<'ctx, 'a> Scope<'ctx, 'a> {
|
|||||||
pub fn allocate(&self, meta: &Metadata, ty: &TypeKind) -> Option<InstructionValue> {
|
pub fn allocate(&self, meta: &Metadata, ty: &TypeKind) -> Option<InstructionValue> {
|
||||||
self.allocator.borrow_mut().allocate(meta, ty)
|
self.allocator.borrow_mut().allocate(meta, ty)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_intrinsic(&self, kind: LLVMIntrinsicKind) -> FunctionValue {
|
||||||
|
let mut intrinsics = self.llvm_intrinsics.borrow_mut();
|
||||||
|
if let Some(fun) = intrinsics.get(&kind) {
|
||||||
|
*fun
|
||||||
|
} else {
|
||||||
|
let intrinsic = self
|
||||||
|
.module
|
||||||
|
.intrinsic(match &kind {
|
||||||
|
LLVMIntrinsicKind::Max(ty) => LLVMIntrinsic::Max(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::Min(ty) => LLVMIntrinsic::Min(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::Abs(ty) => LLVMIntrinsic::Abs(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::Memcpy(ty) => LLVMIntrinsic::Memcpy(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::Sqrt(ty) => LLVMIntrinsic::Sqrt(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::PowI(lhs, rhs) => {
|
||||||
|
LLVMIntrinsic::PowI(lhs.get_type(self.type_values), rhs.get_type(self.type_values))
|
||||||
|
}
|
||||||
|
LLVMIntrinsicKind::Pow(ty) => LLVMIntrinsic::Pow(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::Sin(ty) => LLVMIntrinsic::Sin(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::Cos(ty) => LLVMIntrinsic::Cos(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::Tan(ty) => LLVMIntrinsic::Tan(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::ASin(ty) => LLVMIntrinsic::ASin(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::ACos(ty) => LLVMIntrinsic::ACos(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::ATan(ty) => LLVMIntrinsic::ATan(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::ATan2(ty) => LLVMIntrinsic::ATan2(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::SinH(ty) => LLVMIntrinsic::SinH(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::CosH(ty) => LLVMIntrinsic::CosH(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::TanH(ty) => LLVMIntrinsic::TanH(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::Log(ty) => LLVMIntrinsic::Log(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::Log2(ty) => LLVMIntrinsic::Log2(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::Log10(ty) => LLVMIntrinsic::Log10(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::Copysign(ty) => LLVMIntrinsic::Copysign(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::Floor(ty) => LLVMIntrinsic::Floor(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::Ceil(ty) => LLVMIntrinsic::Ceil(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::Trunc(ty) => LLVMIntrinsic::Trunc(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::RoundEven(ty) => LLVMIntrinsic::RoundEven(ty.get_type(self.type_values)),
|
||||||
|
LLVMIntrinsicKind::Round(ty) => LLVMIntrinsic::Round(ty.get_type(self.type_values)),
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
intrinsics.insert(kind, intrinsic.clone());
|
||||||
|
intrinsic
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
|||||||
@ -14,10 +14,7 @@ use crate::{
|
|||||||
mir::{self, CustomTypeKey, Metadata, SourceModuleId, TypeDefinition, TypeDefinitionKind, TypeKind, VagueLiteral},
|
mir::{self, CustomTypeKey, Metadata, SourceModuleId, TypeDefinition, TypeDefinitionKind, TypeKind, VagueLiteral},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::scope::{Debug, Scope};
|
||||||
scope::{Debug, Scope},
|
|
||||||
ModuleCodegen,
|
|
||||||
};
|
|
||||||
|
|
||||||
impl mir::CmpOperator {
|
impl mir::CmpOperator {
|
||||||
pub(super) fn predicate(&self) -> CmpPredicate {
|
pub(super) fn predicate(&self) -> CmpPredicate {
|
||||||
@ -82,7 +79,7 @@ impl TypeKind {
|
|||||||
TypeKind::U128 => Type::U128,
|
TypeKind::U128 => Type::U128,
|
||||||
TypeKind::Bool => Type::Bool,
|
TypeKind::Bool => Type::Bool,
|
||||||
TypeKind::F16 => Type::F16,
|
TypeKind::F16 => Type::F16,
|
||||||
TypeKind::F32B => Type::F32B,
|
TypeKind::F16B => Type::F32B,
|
||||||
TypeKind::F32 => Type::F32,
|
TypeKind::F32 => Type::F32,
|
||||||
TypeKind::F64 => Type::F64,
|
TypeKind::F64 => Type::F64,
|
||||||
TypeKind::F128 => Type::F128,
|
TypeKind::F128 => Type::F128,
|
||||||
@ -124,7 +121,7 @@ impl TypeKind {
|
|||||||
type_map: &HashMap<CustomTypeKey, TypeDefinition>,
|
type_map: &HashMap<CustomTypeKey, TypeDefinition>,
|
||||||
local_mod: SourceModuleId,
|
local_mod: SourceModuleId,
|
||||||
tokens: &Vec<FullToken>,
|
tokens: &Vec<FullToken>,
|
||||||
modules: &HashMap<SourceModuleId, ModuleCodegen>,
|
modules: &HashMap<SourceModuleId, &mir::Module>,
|
||||||
) -> DebugTypeValue {
|
) -> DebugTypeValue {
|
||||||
if let Some(ty) = debug_types.get(self) {
|
if let Some(ty) = debug_types.get(self) {
|
||||||
return *ty;
|
return *ty;
|
||||||
@ -226,7 +223,7 @@ impl TypeKind {
|
|||||||
TypeKind::U16 | TypeKind::U32 | TypeKind::U64 | TypeKind::U128 => DwarfEncoding::Unsigned,
|
TypeKind::U16 | TypeKind::U32 | TypeKind::U64 | TypeKind::U128 => DwarfEncoding::Unsigned,
|
||||||
TypeKind::F16
|
TypeKind::F16
|
||||||
| TypeKind::F32
|
| TypeKind::F32
|
||||||
| TypeKind::F32B
|
| TypeKind::F16B
|
||||||
| TypeKind::F64
|
| TypeKind::F64
|
||||||
| TypeKind::F80
|
| TypeKind::F80
|
||||||
| TypeKind::F128
|
| TypeKind::F128
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
fmt::{Debug, Write},
|
fmt::{Debug, Write},
|
||||||
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -95,21 +96,46 @@ pub struct ErrorModule {
|
|||||||
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
||||||
pub struct ErrorModules {
|
pub struct ErrorModules {
|
||||||
pub(super) module_map: HashMap<mir::SourceModuleId, ErrorModule>,
|
pub(super) module_map: HashMap<mir::SourceModuleId, ErrorModule>,
|
||||||
|
pub(super) source_id_map: HashMap<PathBuf, mir::SourceModuleId>,
|
||||||
module_counter: mir::SourceModuleId,
|
module_counter: mir::SourceModuleId,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ErrorModules {
|
impl ErrorModules {
|
||||||
pub fn add_module<T: Into<String>>(&mut self, name: T) -> Option<mir::SourceModuleId> {
|
pub fn add_module<T: Into<String>>(
|
||||||
let id = self.module_counter.increment();
|
&mut self,
|
||||||
self.module_map.insert(
|
name: T,
|
||||||
id,
|
path: Option<PathBuf>,
|
||||||
ErrorModule {
|
external_module_id: Option<SourceModuleId>,
|
||||||
name: name.into(),
|
) -> Option<mir::SourceModuleId> {
|
||||||
tokens: None,
|
let module_id = path.as_ref().and_then(|p| self.source_id_map.get(p));
|
||||||
source: None,
|
|
||||||
},
|
if let Some(module_id) = module_id {
|
||||||
);
|
Some(*module_id)
|
||||||
Some(id)
|
} else {
|
||||||
|
let id = if let Some(module_id) = external_module_id {
|
||||||
|
self.module_counter = SourceModuleId(module_id.0.max(self.module_counter.0));
|
||||||
|
if let Some(_) = self.module_map.get(&module_id) {
|
||||||
|
panic!("Can not use external module id: Module already exists!")
|
||||||
|
}
|
||||||
|
module_id
|
||||||
|
} else {
|
||||||
|
self.module_counter.increment()
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(path) = path {
|
||||||
|
self.source_id_map.insert(path, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.module_map.insert(
|
||||||
|
id,
|
||||||
|
ErrorModule {
|
||||||
|
name: name.into(),
|
||||||
|
tokens: None,
|
||||||
|
source: None,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
Some(id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_tokens(&mut self, id: mir::SourceModuleId, tokens: Vec<FullToken>) {
|
pub fn set_tokens(&mut self, id: mir::SourceModuleId, tokens: Vec<FullToken>) {
|
||||||
@ -181,6 +207,10 @@ impl ReidError {
|
|||||||
pub fn from_kind(errors: Vec<ErrorKind>, map: ErrorModules) -> ReidError {
|
pub fn from_kind(errors: Vec<ErrorKind>, map: ErrorModules) -> ReidError {
|
||||||
ReidError { map, errors }
|
ReidError { map, errors }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn extend(&mut self, other: ReidError) {
|
||||||
|
self.errors.extend(other.errors);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for ReidError {}
|
impl std::error::Error for ReidError {}
|
||||||
|
|||||||
@ -25,36 +25,58 @@ impl LDRunner {
|
|||||||
|
|
||||||
let dyn_linker_path = find_objectfile(&self.dynamic_linker);
|
let dyn_linker_path = find_objectfile(&self.dynamic_linker);
|
||||||
let crt1_path = find_objectfile("crt1.o");
|
let crt1_path = find_objectfile("crt1.o");
|
||||||
|
let crti_path = find_objectfile("crti.o");
|
||||||
|
let crtn_path = find_objectfile("crtn.o");
|
||||||
|
|
||||||
#[cfg(feature = "log_output")]
|
log::debug!("LDRunner: Using dynamic linker at: {:?}", dyn_linker_path);
|
||||||
println!("LDRunner: Using dynamic linker at: {:?}", dyn_linker_path);
|
|
||||||
|
|
||||||
let mut ld = Command::new(&self.command);
|
let mut ld = Command::new(&self.command);
|
||||||
ld.arg("-dynamic-linker").arg(dyn_linker_path).arg(crt1_path);
|
ld.arg("-dynamic-linker")
|
||||||
|
.arg(dyn_linker_path)
|
||||||
|
.arg(crt1_path)
|
||||||
|
.arg(crti_path);
|
||||||
|
|
||||||
for library in &self.libraries {
|
for library in &self.libraries {
|
||||||
ld.arg(format!("-l{}", library));
|
ld.arg(format!("-l{}", library));
|
||||||
}
|
}
|
||||||
|
ld.arg(crtn_path);
|
||||||
|
|
||||||
ld.arg(input_path.to_str().unwrap())
|
ld.arg(input_path.to_str().unwrap())
|
||||||
.arg("-o")
|
.arg("-o")
|
||||||
.arg(out_path.to_str().unwrap());
|
.arg(out_path.to_str().unwrap());
|
||||||
|
|
||||||
#[cfg(feature = "log_output")]
|
log::debug!("{:#?}", ld);
|
||||||
println!(
|
|
||||||
"LDRunner: Executing linker to objfile at {:?} => {:?}",
|
|
||||||
input_path, out_path
|
|
||||||
);
|
|
||||||
#[cfg(feature = "log_output")]
|
|
||||||
dbg!(&ld);
|
|
||||||
|
|
||||||
ld.spawn().expect("Unable to execute ld!");
|
log::debug!(
|
||||||
|
"LDRunner: Executing linker to objfile at {:?} => {:?}",
|
||||||
|
input_path,
|
||||||
|
out_path
|
||||||
|
);
|
||||||
|
|
||||||
|
let ld_output = ld.output().expect("Unable to execute ld!");
|
||||||
|
if !ld_output.status.success() {
|
||||||
|
let code = ld_output.status.code().unwrap_or(255);
|
||||||
|
log::error!("LD exited with code {code}");
|
||||||
|
println!("{}", unsafe { String::from_utf8_unchecked(ld_output.stderr) });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
thread::sleep(Duration::from_millis(100));
|
thread::sleep(Duration::from_millis(100));
|
||||||
|
|
||||||
#[cfg(feature = "log_output")]
|
log::debug!("Setting executable bit to {:?}..", out_path);
|
||||||
println!("Setting executable bit to {:?}..", out_path);
|
|
||||||
Command::new("chmod").arg("+x").arg(out_path).spawn().unwrap();
|
let chmod_output = Command::new("chmod")
|
||||||
|
.arg("+x")
|
||||||
|
.arg(out_path)
|
||||||
|
.output()
|
||||||
|
.expect("Unable to execute ld!");
|
||||||
|
|
||||||
|
if !chmod_output.status.success() {
|
||||||
|
let code = chmod_output.status.code().unwrap_or(255);
|
||||||
|
log::error!("chmod exited with code {code}");
|
||||||
|
println!("{}", unsafe { String::from_utf8_unchecked(chmod_output.stderr) });
|
||||||
|
return;
|
||||||
|
}
|
||||||
thread::sleep(Duration::from_millis(100));
|
thread::sleep(Duration::from_millis(100));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,6 +86,12 @@ fn find_objectfile(name: &str) -> String {
|
|||||||
.arg(&name)
|
.arg(&name)
|
||||||
.output()
|
.output()
|
||||||
.expect("Unable to execute whereis");
|
.expect("Unable to execute whereis");
|
||||||
|
if !whereis.status.success() {
|
||||||
|
let code = whereis.status.code().unwrap_or(255);
|
||||||
|
log::error!("whereis exited with code {code}");
|
||||||
|
println!("{}", unsafe { String::from_utf8_unchecked(whereis.stderr) });
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
let whereis_output = String::from_utf8(whereis.stdout).unwrap();
|
let whereis_output = String::from_utf8(whereis.stdout).unwrap();
|
||||||
|
|
||||||
whereis_output
|
whereis_output
|
||||||
@ -76,3 +104,15 @@ fn find_objectfile(name: &str) -> String {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.to_owned()
|
.to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn execute(path: &PathBuf) -> Option<i32> {
|
||||||
|
let output = Command::new(path.clone()).output().expect("Unable to execute {path}");
|
||||||
|
|
||||||
|
if !output.status.success() {
|
||||||
|
let code = output.status.code().unwrap_or(255);
|
||||||
|
log::error!("{path:?} exited with code {code}");
|
||||||
|
println!("{}", unsafe { String::from_utf8_unchecked(output.stderr) });
|
||||||
|
}
|
||||||
|
|
||||||
|
output.status.code()
|
||||||
|
}
|
||||||
|
|||||||
109
reid/src/lib.rs
109
reid/src/lib.rs
@ -69,11 +69,14 @@ use reid_lib::{compile::CompileOutput, Context};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::TopLevelStatement,
|
ast::TopLevelStatement,
|
||||||
mir::macros::{form_macros, MacroModule, MacroPass},
|
mir::{
|
||||||
|
macros::{form_macros, MacroModule, MacroPass},
|
||||||
|
SourceModuleId,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub mod ast;
|
pub mod ast;
|
||||||
mod codegen;
|
pub mod codegen;
|
||||||
pub mod error_raporting;
|
pub mod error_raporting;
|
||||||
pub mod ld;
|
pub mod ld;
|
||||||
pub mod mir;
|
pub mod mir;
|
||||||
@ -83,9 +86,11 @@ mod util;
|
|||||||
pub fn parse_module<'map, T: Into<String>>(
|
pub fn parse_module<'map, T: Into<String>>(
|
||||||
source: &str,
|
source: &str,
|
||||||
name: T,
|
name: T,
|
||||||
|
path: Option<PathBuf>,
|
||||||
map: &'map mut ErrorModules,
|
map: &'map mut ErrorModules,
|
||||||
|
module_id: Option<SourceModuleId>,
|
||||||
) -> Result<(mir::SourceModuleId, Vec<FullToken>), ReidError> {
|
) -> Result<(mir::SourceModuleId, Vec<FullToken>), ReidError> {
|
||||||
let id = map.add_module(name.into()).unwrap();
|
let id = map.add_module(name.into(), path, module_id).unwrap();
|
||||||
map.set_source(id, source.to_owned());
|
map.set_source(id, source.to_owned());
|
||||||
|
|
||||||
let tokens = ReidError::from_lexer(lexer::tokenize(source), map.clone(), id)?;
|
let tokens = ReidError::from_lexer(lexer::tokenize(source), map.clone(), id)?;
|
||||||
@ -93,8 +98,7 @@ pub fn parse_module<'map, T: Into<String>>(
|
|||||||
map.set_tokens(id, tokens.clone());
|
map.set_tokens(id, tokens.clone());
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:#?}", &tokens);
|
||||||
println!("{:#?}", &tokens);
|
|
||||||
|
|
||||||
Ok((id, tokens))
|
Ok((id, tokens))
|
||||||
}
|
}
|
||||||
@ -105,7 +109,7 @@ pub fn compile_module<'map>(
|
|||||||
map: &'map mut ErrorModules,
|
map: &'map mut ErrorModules,
|
||||||
path: Option<PathBuf>,
|
path: Option<PathBuf>,
|
||||||
is_main: bool,
|
is_main: bool,
|
||||||
) -> Result<mir::Module, ReidError> {
|
) -> Result<Result<mir::Module, (ast::Module, ReidError)>, ReidError> {
|
||||||
let module = map.module(&module_id).cloned().unwrap();
|
let module = map.module(&module_id).cloned().unwrap();
|
||||||
|
|
||||||
let mut token_stream = TokenStream::from(&tokens);
|
let mut token_stream = TokenStream::from(&tokens);
|
||||||
@ -117,6 +121,8 @@ pub fn compile_module<'map>(
|
|||||||
statements.push(statement);
|
statements.push(statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let errors = token_stream.errors();
|
||||||
|
|
||||||
drop(token_stream);
|
drop(token_stream);
|
||||||
|
|
||||||
let ast_module = ast::Module {
|
let ast_module = ast::Module {
|
||||||
@ -127,11 +133,33 @@ pub fn compile_module<'map>(
|
|||||||
is_main,
|
is_main,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
if errors.len() > 0 {
|
||||||
#[cfg(feature = "log_output")]
|
// dbg!(&ast_module);
|
||||||
dbg!(&ast_module);
|
return Ok(Err((
|
||||||
|
ast_module,
|
||||||
|
ReidError::from_kind(
|
||||||
|
errors
|
||||||
|
.into_iter()
|
||||||
|
.map(|e| {
|
||||||
|
error_raporting::ErrorKind::from(mir::pass::Error {
|
||||||
|
metadata: mir::Metadata {
|
||||||
|
source_module_id: module_id,
|
||||||
|
range: *e.get_range().unwrap_or(&Default::default()),
|
||||||
|
position: None,
|
||||||
|
},
|
||||||
|
kind: e,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
map.clone(),
|
||||||
|
),
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
Ok(ast_module.process(module_id))
|
#[cfg(debug_assertions)]
|
||||||
|
log::trace!("{:#?}", &ast_module);
|
||||||
|
|
||||||
|
Ok(Ok(ast_module.process(module_id)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn perform_all_passes<'map>(
|
pub fn perform_all_passes<'map>(
|
||||||
@ -139,12 +167,10 @@ pub fn perform_all_passes<'map>(
|
|||||||
module_map: &'map mut ErrorModules,
|
module_map: &'map mut ErrorModules,
|
||||||
) -> Result<(), ReidError> {
|
) -> Result<(), ReidError> {
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:#?}", &context);
|
||||||
dbg!(&context);
|
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:#}", &context);
|
||||||
println!("{:#}", &context);
|
|
||||||
|
|
||||||
let state = context.pass(&mut LinkerPass {
|
let state = context.pass(&mut LinkerPass {
|
||||||
module_map,
|
module_map,
|
||||||
@ -158,14 +184,11 @@ pub fn perform_all_passes<'map>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:-^100}", "LINKER OUTPUT");
|
||||||
println!("{:-^100}", "LINKER OUTPUT");
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:#}", &context);
|
||||||
println!("{:#}", &context);
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:#?}", &state);
|
||||||
dbg!(&state);
|
|
||||||
|
|
||||||
if !state.errors.is_empty() {
|
if !state.errors.is_empty() {
|
||||||
return Err(ReidError::from_kind(
|
return Err(ReidError::from_kind(
|
||||||
@ -186,14 +209,11 @@ pub fn perform_all_passes<'map>(
|
|||||||
let state = context.pass(&mut macro_pass)?;
|
let state = context.pass(&mut macro_pass)?;
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:-^100}", "MACRO OUTPUT");
|
||||||
println!("{:-^100}", "MACRO OUTPUT");
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:#}", &context);
|
||||||
println!("{:#}", &context);
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:#?}", &state);
|
||||||
dbg!(&state);
|
|
||||||
|
|
||||||
if !state.errors.is_empty() {
|
if !state.errors.is_empty() {
|
||||||
return Err(ReidError::from_kind(
|
return Err(ReidError::from_kind(
|
||||||
@ -227,17 +247,13 @@ pub fn perform_all_passes<'map>(
|
|||||||
let state = context.pass(&mut TypeInference { refs: &mut refs })?;
|
let state = context.pass(&mut TypeInference { refs: &mut refs })?;
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:-^70}", "TYPE INFERRER OUTPUT");
|
||||||
println!("{:-^100}", "TYPE INFERRER OUTPUT");
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{}", &refs);
|
||||||
println!("{}", &refs);
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:#}", &context);
|
||||||
println!("{:#}", &context);
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:#?}", &state);
|
||||||
dbg!(&state);
|
|
||||||
|
|
||||||
if !state.errors.is_empty() {
|
if !state.errors.is_empty() {
|
||||||
return Err(ReidError::from_kind(
|
return Err(ReidError::from_kind(
|
||||||
@ -253,14 +269,11 @@ pub fn perform_all_passes<'map>(
|
|||||||
let state = context.pass(&mut TypeCheck { refs: &refs })?;
|
let state = context.pass(&mut TypeCheck { refs: &refs })?;
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:-^100}", "TYPECHECKER OUTPUT");
|
||||||
println!("{:-^100}", "TYPECHECKER OUTPUT");
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:#}", &context);
|
||||||
println!("{:#}", &context);
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:#?}", &state);
|
||||||
dbg!(&state);
|
|
||||||
|
|
||||||
if !state.errors.is_empty() {
|
if !state.errors.is_empty() {
|
||||||
return Err(ReidError::from_kind(
|
return Err(ReidError::from_kind(
|
||||||
@ -273,9 +286,6 @@ pub fn perform_all_passes<'map>(
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "context_debug")]
|
|
||||||
dbg!(&context);
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -292,19 +302,17 @@ pub fn compile_and_pass<'map>(
|
|||||||
let path = path.canonicalize().unwrap();
|
let path = path.canonicalize().unwrap();
|
||||||
let name = path.file_name().unwrap().to_str().unwrap().to_owned();
|
let name = path.file_name().unwrap().to_str().unwrap().to_owned();
|
||||||
|
|
||||||
let (id, tokens) = parse_module(source, name, module_map)?;
|
let (id, tokens) = parse_module(source, name, Some(path.clone()), module_map, None)?;
|
||||||
let module = compile_module(id, tokens, module_map, Some(path.clone()), true)?;
|
let module = compile_module(id, tokens, module_map, Some(path.clone()), true)?.map_err(|(_, e)| e)?;
|
||||||
|
|
||||||
let mut mir_context = mir::Context::from(vec![module], path.parent().unwrap().to_owned());
|
let mut mir_context = mir::Context::from(vec![module], path.parent().unwrap().to_owned());
|
||||||
|
|
||||||
perform_all_passes(&mut mir_context, module_map)?;
|
perform_all_passes(&mut mir_context, module_map)?;
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:-^100}", "FINAL OUTPUT");
|
||||||
println!("{:-^100}", "FINAL OUTPUT");
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{:#}", &mir_context);
|
||||||
println!("{:#}", &mir_context);
|
|
||||||
|
|
||||||
let mut context = Context::new(format!("Reid ({})", env!("CARGO_PKG_VERSION")));
|
let mut context = Context::new(format!("Reid ({})", env!("CARGO_PKG_VERSION")));
|
||||||
let codegen_modules = match mir_context.codegen(&mut context) {
|
let codegen_modules = match mir_context.codegen(&mut context) {
|
||||||
@ -313,8 +321,7 @@ pub fn compile_and_pass<'map>(
|
|||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[cfg(feature = "log_output")]
|
log::trace!("{}", &codegen_modules.context);
|
||||||
println!("{}", &codegen_modules.context);
|
|
||||||
|
|
||||||
let compiled = codegen_modules.compile(cpu, features);
|
let compiled = codegen_modules.compile(cpu, features);
|
||||||
Ok((
|
Ok((
|
||||||
|
|||||||
149
reid/src/main.rs
Normal file
149
reid/src/main.rs
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
use std::{fs, path::PathBuf, process};
|
||||||
|
|
||||||
|
use argh::FromArgs;
|
||||||
|
use log::*;
|
||||||
|
use reid::{
|
||||||
|
compile_simple,
|
||||||
|
ld::{execute, LDRunner},
|
||||||
|
CustomIRs,
|
||||||
|
};
|
||||||
|
use reid_lib::compile::CompileOutput;
|
||||||
|
|
||||||
|
#[derive(FromArgs, PartialEq, Debug)]
|
||||||
|
/// Compile or run a Reid (.reid) file
|
||||||
|
#[argh(help_triggers("-h", "--help", "help"))]
|
||||||
|
struct Options {
|
||||||
|
#[argh(option, short = 'l', default = "log::Level::Info")]
|
||||||
|
/// log level
|
||||||
|
log_level: log::Level,
|
||||||
|
#[argh(switch, short = 't')]
|
||||||
|
/// should logs be timestamped
|
||||||
|
timestamps: bool,
|
||||||
|
#[argh(subcommand)]
|
||||||
|
command: Command,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(FromArgs, PartialEq, Debug)]
|
||||||
|
#[argh(subcommand)]
|
||||||
|
enum Command {
|
||||||
|
Build(BuildOpts),
|
||||||
|
Run(RunOpts),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(FromArgs, PartialEq, Debug)]
|
||||||
|
/// Build an executable file without running it
|
||||||
|
#[argh(subcommand, name = "build")]
|
||||||
|
struct BuildOpts {
|
||||||
|
#[argh(option, long = "lib", short = 'l')]
|
||||||
|
/// additional libraries to link against (with ld)
|
||||||
|
libraries: Vec<String>,
|
||||||
|
#[argh(positional)]
|
||||||
|
path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(FromArgs, PartialEq, Debug)]
|
||||||
|
/// Build and then execute the executable
|
||||||
|
#[argh(subcommand, name = "run")]
|
||||||
|
struct RunOpts {
|
||||||
|
#[argh(option, long = "lib", short = 'l')]
|
||||||
|
/// additional libraries to link against (with ld)
|
||||||
|
libraries: Vec<String>,
|
||||||
|
#[argh(positional)]
|
||||||
|
path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let options: Options = argh::from_env();
|
||||||
|
let mut errlog = stderrlog::new();
|
||||||
|
errlog.module(module_path!()).verbosity(options.log_level);
|
||||||
|
if options.timestamps {
|
||||||
|
errlog.timestamp(stderrlog::Timestamp::Second);
|
||||||
|
}
|
||||||
|
errlog.init().unwrap();
|
||||||
|
|
||||||
|
match &options.command {
|
||||||
|
Command::Build(BuildOpts { libraries, path }) | Command::Run(RunOpts { libraries, path }) => {
|
||||||
|
let cpu = std::env::var("CPU").unwrap_or("generic".to_owned());
|
||||||
|
let features = std::env::var("REIDFLAGS").unwrap_or("".to_owned());
|
||||||
|
|
||||||
|
let path = match path.canonicalize() {
|
||||||
|
Ok(path) => path,
|
||||||
|
Err(e) => {
|
||||||
|
error!("{e}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let parent = path.with_extension("");
|
||||||
|
let llvm_ir_path = parent.with_extension("ll");
|
||||||
|
let object_path = parent.with_extension("o");
|
||||||
|
let llir_path = parent.with_extension("llir");
|
||||||
|
let mir_path = parent.with_extension("mir");
|
||||||
|
let asm_path = parent.with_extension("asm");
|
||||||
|
let out_path = object_path.with_extension("out");
|
||||||
|
|
||||||
|
let before = std::time::SystemTime::now();
|
||||||
|
|
||||||
|
let text = match fs::read_to_string(&path) {
|
||||||
|
Ok(text) => text,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Could not read file: {e}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match compile_simple(&text, PathBuf::from(&path), Some(cpu), vec![features]) {
|
||||||
|
Ok((
|
||||||
|
CompileOutput {
|
||||||
|
triple: _triple,
|
||||||
|
assembly,
|
||||||
|
obj_buffer,
|
||||||
|
llvm_ir: _llvm_ir,
|
||||||
|
},
|
||||||
|
CustomIRs { llir, mir },
|
||||||
|
)) => {
|
||||||
|
log::trace!("{}", _llvm_ir);
|
||||||
|
log::debug!("Compiled with triple: {}\n", &_triple);
|
||||||
|
log::debug!("Output LLVM IR to {:?}", llvm_ir_path);
|
||||||
|
log::debug!("Output Assembly to {:?}", asm_path);
|
||||||
|
log::debug!("Output Object-file to {:?}\n", object_path);
|
||||||
|
log::debug!("Output LLIR-file to {:?}\n", llir_path);
|
||||||
|
log::debug!("Output MIR-file to {:?}\n", mir_path);
|
||||||
|
|
||||||
|
fs::write(&llvm_ir_path, &_llvm_ir).expect("Could not write LLVM IR -file!");
|
||||||
|
fs::write(&asm_path, &assembly).expect("Could not write Assembly-file!");
|
||||||
|
fs::write(&object_path, &obj_buffer).expect("Could not write Object-file!");
|
||||||
|
fs::write(&llir_path, &llir).expect("Could not write LLIR-file!");
|
||||||
|
fs::write(&mir_path, &mir).expect("Could not write MIR-file!");
|
||||||
|
let after = std::time::SystemTime::now();
|
||||||
|
log::info!(
|
||||||
|
"Compilation took: {:.2}ms\n",
|
||||||
|
(after.duration_since(before).unwrap().as_micros() as f32) / 1000.
|
||||||
|
);
|
||||||
|
|
||||||
|
log::info!("Linking {:?}", &object_path);
|
||||||
|
|
||||||
|
let linker = std::env::var("LD").unwrap_or("ld".to_owned());
|
||||||
|
let mut linker = LDRunner::from_command(&linker).with_library("c").with_library("m");
|
||||||
|
for library in libraries {
|
||||||
|
linker = linker.with_library(&library);
|
||||||
|
}
|
||||||
|
linker.invoke(&object_path, &out_path);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("{e}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match &options.command {
|
||||||
|
Command::Build(_) => {}
|
||||||
|
Command::Run(_) => {
|
||||||
|
if let Some(code) = execute(&out_path) {
|
||||||
|
process::exit(code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -84,7 +84,11 @@ impl Display for GlobalKind {
|
|||||||
|
|
||||||
impl Display for Import {
|
impl Display for Import {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "import {}", self.0.join("::"))
|
write!(
|
||||||
|
f,
|
||||||
|
"import {}",
|
||||||
|
self.0.iter().map(|(s, _)| s.clone()).collect::<Vec<_>>().join("::")
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,7 +117,7 @@ impl Display for TypeDefinition {
|
|||||||
self.name,
|
self.name,
|
||||||
self.source_module,
|
self.source_module,
|
||||||
if let Some(mod_id) = self.importer {
|
if let Some(mod_id) = self.importer {
|
||||||
format!("; imported to {}", mod_id)
|
format!("; imported by {}", mod_id)
|
||||||
} else {
|
} else {
|
||||||
String::new()
|
String::new()
|
||||||
}
|
}
|
||||||
@ -148,6 +152,9 @@ impl Display for StructField {
|
|||||||
|
|
||||||
impl Display for FunctionDefinition {
|
impl Display for FunctionDefinition {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
if let Some(documentation) = &self.documentation {
|
||||||
|
writeln!(f, "/// {}", documentation)?;
|
||||||
|
}
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"{}fn {}({}) -> {:#} ",
|
"{}fn {}({}) -> {:#} ",
|
||||||
@ -272,8 +279,8 @@ impl Display for ExprKind {
|
|||||||
}
|
}
|
||||||
f.write_char(']')
|
f.write_char(']')
|
||||||
}
|
}
|
||||||
ExprKind::Struct(name, items) => {
|
ExprKind::Struct(key, items) => {
|
||||||
write!(f, "{} ", name)?;
|
write!(f, "{:?} ", key)?;
|
||||||
|
|
||||||
f.write_char('{')?;
|
f.write_char('{')?;
|
||||||
let mut state = Default::default();
|
let mut state = Default::default();
|
||||||
@ -464,7 +471,7 @@ impl Display for TypeKind {
|
|||||||
}
|
}
|
||||||
TypeKind::Vague(vague_type) => Display::fmt(vague_type, f),
|
TypeKind::Vague(vague_type) => Display::fmt(vague_type, f),
|
||||||
TypeKind::F16 => write!(f, "f16"),
|
TypeKind::F16 => write!(f, "f16"),
|
||||||
TypeKind::F32B => write!(f, "f16b"),
|
TypeKind::F16B => write!(f, "f16b"),
|
||||||
TypeKind::F32 => write!(f, "f32"),
|
TypeKind::F32 => write!(f, "f32"),
|
||||||
TypeKind::F64 => write!(f, "f64"),
|
TypeKind::F64 => write!(f, "f64"),
|
||||||
TypeKind::F128 => write!(f, "f128"),
|
TypeKind::F128 => write!(f, "f128"),
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
use reid_lib::builder::TypeValue;
|
|
||||||
|
|
||||||
use crate::util::maybe;
|
use crate::util::maybe;
|
||||||
|
|
||||||
use super::{typecheck::typerefs::TypeRefs, *};
|
use super::{typecheck::typerefs::TypeRefs, *};
|
||||||
@ -50,7 +48,7 @@ impl TypeKind {
|
|||||||
TypeKind::Borrow(..) => false,
|
TypeKind::Borrow(..) => false,
|
||||||
TypeKind::UserPtr(..) => false,
|
TypeKind::UserPtr(..) => false,
|
||||||
TypeKind::F16 => true,
|
TypeKind::F16 => true,
|
||||||
TypeKind::F32B => true,
|
TypeKind::F16B => true,
|
||||||
TypeKind::F32 => true,
|
TypeKind::F32 => true,
|
||||||
TypeKind::F64 => true,
|
TypeKind::F64 => true,
|
||||||
TypeKind::F128 => true,
|
TypeKind::F128 => true,
|
||||||
@ -75,21 +73,26 @@ impl TypeKind {
|
|||||||
TypeKind::Void => 0,
|
TypeKind::Void => 0,
|
||||||
TypeKind::Char => 8,
|
TypeKind::Char => 8,
|
||||||
TypeKind::Array(type_kind, len) => type_kind.size_of(map) * (*len as u64),
|
TypeKind::Array(type_kind, len) => type_kind.size_of(map) * (*len as u64),
|
||||||
TypeKind::CustomType(key) => match &map.get(key).unwrap().kind {
|
TypeKind::CustomType(key) => match &map.get(key) {
|
||||||
TypeDefinitionKind::Struct(struct_type) => {
|
Some(def) => match &def.kind {
|
||||||
let mut size = 0;
|
TypeDefinitionKind::Struct(struct_type) => {
|
||||||
for field in &struct_type.0 {
|
let mut size = 0;
|
||||||
size += field.1.size_of(map)
|
for field in &struct_type.0 {
|
||||||
|
size += field.1.size_of(map)
|
||||||
|
}
|
||||||
|
size
|
||||||
}
|
}
|
||||||
size
|
},
|
||||||
}
|
// Easy to recognize default number. Used e.g. when sorting
|
||||||
|
// types by size
|
||||||
|
None => 404,
|
||||||
},
|
},
|
||||||
TypeKind::CodegenPtr(_) => 64,
|
TypeKind::CodegenPtr(_) => 64,
|
||||||
TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"),
|
TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"),
|
||||||
TypeKind::Borrow(..) => 64,
|
TypeKind::Borrow(..) => 64,
|
||||||
TypeKind::UserPtr(_) => 64,
|
TypeKind::UserPtr(_) => 64,
|
||||||
TypeKind::F16 => 16,
|
TypeKind::F16 => 16,
|
||||||
TypeKind::F32B => 16,
|
TypeKind::F16B => 16,
|
||||||
TypeKind::F32 => 32,
|
TypeKind::F32 => 32,
|
||||||
TypeKind::F64 => 64,
|
TypeKind::F64 => 64,
|
||||||
TypeKind::F128 => 128,
|
TypeKind::F128 => 128,
|
||||||
@ -120,7 +123,7 @@ impl TypeKind {
|
|||||||
TypeKind::Borrow(_, _) => 64,
|
TypeKind::Borrow(_, _) => 64,
|
||||||
TypeKind::UserPtr(_) => 64,
|
TypeKind::UserPtr(_) => 64,
|
||||||
TypeKind::F16 => 16,
|
TypeKind::F16 => 16,
|
||||||
TypeKind::F32B => 16,
|
TypeKind::F16B => 16,
|
||||||
TypeKind::F32 => 32,
|
TypeKind::F32 => 32,
|
||||||
TypeKind::F64 => 64,
|
TypeKind::F64 => 64,
|
||||||
TypeKind::F128 => 128,
|
TypeKind::F128 => 128,
|
||||||
@ -150,7 +153,7 @@ impl TypeKind {
|
|||||||
| TypeKind::U128
|
| TypeKind::U128
|
||||||
| TypeKind::Char => TypeCategory::Integer,
|
| TypeKind::Char => TypeCategory::Integer,
|
||||||
TypeKind::F16
|
TypeKind::F16
|
||||||
| TypeKind::F32B
|
| TypeKind::F16B
|
||||||
| TypeKind::F32
|
| TypeKind::F32
|
||||||
| TypeKind::F64
|
| TypeKind::F64
|
||||||
| TypeKind::F128
|
| TypeKind::F128
|
||||||
@ -199,6 +202,36 @@ impl TypeKind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Ord for TypeKind {
|
||||||
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||||
|
use std::cmp::*;
|
||||||
|
|
||||||
|
let category_ord = self.category().partial_cmp(&other.category());
|
||||||
|
|
||||||
|
match category_ord {
|
||||||
|
Some(Ordering::Equal) | None => {
|
||||||
|
if !self.signed() && other.signed() {
|
||||||
|
return Ordering::Less;
|
||||||
|
}
|
||||||
|
if self.signed() && !other.signed() {
|
||||||
|
return Ordering::Greater;
|
||||||
|
}
|
||||||
|
|
||||||
|
let self_size = self.size_of(&HashMap::new());
|
||||||
|
let other_size = other.size_of(&HashMap::new());
|
||||||
|
if self_size == 32 && other_size != 32 {
|
||||||
|
return Ordering::Less;
|
||||||
|
} else if self_size != 32 && other_size == 32 {
|
||||||
|
return Ordering::Greater;
|
||||||
|
}
|
||||||
|
|
||||||
|
self_size.cmp(&self_size)
|
||||||
|
}
|
||||||
|
Some(ord) => ord,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl BinaryOperator {
|
impl BinaryOperator {
|
||||||
pub fn is_commutative(&self) -> bool {
|
pub fn is_commutative(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
@ -226,7 +259,28 @@ impl BinaryOperator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
const TYPE_CATEGORY_ORDER: [TypeCategory; 5] = [
|
||||||
|
TypeCategory::Integer,
|
||||||
|
TypeCategory::Bool,
|
||||||
|
TypeCategory::Real,
|
||||||
|
TypeCategory::Other,
|
||||||
|
TypeCategory::TypeRef,
|
||||||
|
];
|
||||||
|
|
||||||
|
impl PartialOrd for TypeCategory {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||||
|
use std::cmp::*;
|
||||||
|
let self_idx = TYPE_CATEGORY_ORDER.iter().enumerate().find(|s| s.1 == self);
|
||||||
|
let other_idx = TYPE_CATEGORY_ORDER.iter().enumerate().find(|s| s.1 == other);
|
||||||
|
if let (Some(self_idx), Some(other_idx)) = (self_idx, other_idx) {
|
||||||
|
Some(self_idx.cmp(&other_idx))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq, Ord)]
|
||||||
pub enum TypeCategory {
|
pub enum TypeCategory {
|
||||||
Integer,
|
Integer,
|
||||||
Real,
|
Real,
|
||||||
@ -416,10 +470,7 @@ impl Expression {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
Accessed(_, type_kind, ..) => Ok((ReturnKind::Soft, type_kind.clone())),
|
Accessed(_, type_kind, ..) => Ok((ReturnKind::Soft, type_kind.clone())),
|
||||||
Struct(name, _) => Ok((
|
Struct(key, _) => Ok((ReturnKind::Soft, TypeKind::CustomType(key.clone()))),
|
||||||
ReturnKind::Soft,
|
|
||||||
TypeKind::CustomType(CustomTypeKey(name.clone(), mod_id)),
|
|
||||||
)),
|
|
||||||
Borrow(expr, mutable) => {
|
Borrow(expr, mutable) => {
|
||||||
let ret_type = expr.return_type(refs, mod_id)?;
|
let ret_type = expr.return_type(refs, mod_id)?;
|
||||||
Ok((ret_type.0, TypeKind::Borrow(Box::new(ret_type.1), *mutable)))
|
Ok((ret_type.0, TypeKind::Borrow(Box::new(ret_type.1), *mutable)))
|
||||||
|
|||||||
@ -11,8 +11,8 @@ use crate::{
|
|||||||
compile_module,
|
compile_module,
|
||||||
error_raporting::{ErrorModules, ReidError},
|
error_raporting::{ErrorModules, ReidError},
|
||||||
mir::{
|
mir::{
|
||||||
pass::BinopKey, BinopDefinition, CustomTypeKey, FunctionDefinitionKind, FunctionParam, SourceModuleId,
|
pass::BinopKey, BinopDefinition, CustomTypeKey, FunctionDefinitionKind, SourceModuleId, StructType,
|
||||||
TypeDefinition, TypeKind,
|
TypeDefinition, TypeDefinitionKind, TypeKind,
|
||||||
},
|
},
|
||||||
parse_module,
|
parse_module,
|
||||||
};
|
};
|
||||||
@ -46,13 +46,19 @@ pub enum ErrorKind {
|
|||||||
NoMainDefined,
|
NoMainDefined,
|
||||||
#[error("Main module has no main-function!")]
|
#[error("Main module has no main-function!")]
|
||||||
NoMainFunction,
|
NoMainFunction,
|
||||||
|
#[error("Type {0} has cyclical fields!")]
|
||||||
|
CyclicalType(String),
|
||||||
|
#[error("Type {0} is imported cyclically!")]
|
||||||
|
RecursiveTypeImport(String),
|
||||||
|
#[error("Type {} does not exist in module {}", 0.0, 0.1)]
|
||||||
|
NoSuchTypeInModule(CustomTypeKey),
|
||||||
#[error("Function {1} in module {0} is private!")]
|
#[error("Function {1} in module {0} is private!")]
|
||||||
FunctionIsPrivate(String, String),
|
FunctionIsPrivate(String, String),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn compile_std(module_map: &mut ErrorModules) -> Result<Module, ReidError> {
|
pub fn compile_std(module_map: &mut ErrorModules) -> Result<Module, ReidError> {
|
||||||
let (id, tokens) = parse_module(STD_SOURCE, STD_NAME, module_map)?;
|
let (id, tokens) = parse_module(STD_SOURCE, STD_NAME, None, module_map, None)?;
|
||||||
let module = compile_module(id, tokens, module_map, None, false)?;
|
let module = compile_module(id, tokens, module_map, None, false)?.map_err(|(_, e)| e)?;
|
||||||
|
|
||||||
let module_id = module.module_id;
|
let module_id = module.module_id;
|
||||||
let mut mir_context = super::Context::from(vec![module], Default::default());
|
let mut mir_context = super::Context::from(vec![module], Default::default());
|
||||||
@ -70,11 +76,21 @@ pub struct LinkerPass<'map> {
|
|||||||
|
|
||||||
#[derive(Default, Clone)]
|
#[derive(Default, Clone)]
|
||||||
pub struct LinkerState {
|
pub struct LinkerState {
|
||||||
extern_imported_types: HashMap<SourceModuleId, HashMap<String, SourceModuleId>>,
|
foreign_types: HashMap<SourceModuleId, HashMap<CustomTypeKey, SourceModuleId>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
type LinkerPassState<'st, 'sc> = PassState<'st, 'sc, LinkerState, ErrorKind>;
|
type LinkerPassState<'st, 'sc> = PassState<'st, 'sc, LinkerState, ErrorKind>;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
struct LinkerModule {
|
||||||
|
module: Rc<RefCell<Module>>,
|
||||||
|
// Functions imported directly from a module
|
||||||
|
function_imports: HashMap<String, (SourceModuleId, Metadata)>,
|
||||||
|
// Types imported either directly by the user or indirectly via functions.
|
||||||
|
// May contain type-imports that are again recursively imported elsewhere.
|
||||||
|
type_imports: HashMap<String, (SourceModuleId, Metadata)>,
|
||||||
|
}
|
||||||
|
|
||||||
impl<'map> Pass for LinkerPass<'map> {
|
impl<'map> Pass for LinkerPass<'map> {
|
||||||
type Data = LinkerState;
|
type Data = LinkerState;
|
||||||
type TError = ErrorKind;
|
type TError = ErrorKind;
|
||||||
@ -102,37 +118,56 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut modules = HashMap::<SourceModuleId, Rc<RefCell<_>>>::new();
|
let mut modules = HashMap::<SourceModuleId, LinkerModule>::new();
|
||||||
let mut module_ids = HashMap::<String, SourceModuleId>::new();
|
let mut module_ids = HashMap::<String, SourceModuleId>::new();
|
||||||
|
|
||||||
for (mod_id, module) in context.modules.drain() {
|
for (mod_id, module) in context.modules.drain() {
|
||||||
modules.insert(mod_id, Rc::new(RefCell::new(module)));
|
modules.insert(
|
||||||
|
mod_id,
|
||||||
|
LinkerModule {
|
||||||
|
module: Rc::new(RefCell::new(module)),
|
||||||
|
function_imports: HashMap::new(),
|
||||||
|
type_imports: HashMap::new(),
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut modules_to_process: Vec<Rc<RefCell<_>>> = modules.values().cloned().collect();
|
let mut module_queue: Vec<LinkerModule> = modules.values().cloned().collect();
|
||||||
|
|
||||||
let mut already_imported_types = HashSet::<CustomTypeKey>::new();
|
while let Some(mut importer) = module_queue.pop() {
|
||||||
let mut already_imported_binops = HashSet::<BinopKey>::new();
|
let importer_mod = importer.module.borrow_mut();
|
||||||
|
|
||||||
while let Some(module) = modules_to_process.pop() {
|
// Gp go through all imports in this specific modulee
|
||||||
let mut extern_types = HashMap::new();
|
for import in importer_mod.imports.clone() {
|
||||||
let mut importer_module = module.borrow_mut();
|
|
||||||
|
|
||||||
for import in importer_module.imports.clone() {
|
|
||||||
let Import(path, _) = &import;
|
let Import(path, _) = &import;
|
||||||
if path.len() != 2 {
|
if path.len() != 2 {
|
||||||
state.ok::<_, Infallible>(Err(ErrorKind::InnerModulesNotYetSupported(import.clone())), import.1);
|
state.ok::<_, Infallible>(Err(ErrorKind::InnerModulesNotYetSupported(import.clone())), import.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let module_name = unsafe { path.get_unchecked(0) };
|
// Cut the import statement into parts
|
||||||
|
let Some((module_name, _)) = path.get(0) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let Some((import_name, _)) = path.get(1) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
let mut imported = if let Some(mod_id) = module_ids.get(module_name) {
|
// Actually compile or fetch the imported module
|
||||||
|
let imported = if let Some(mod_id) = module_ids.get(module_name) {
|
||||||
modules.get(mod_id).unwrap()
|
modules.get(mod_id).unwrap()
|
||||||
} else if module_name == STD_NAME {
|
} else if module_name == STD_NAME {
|
||||||
let std = compile_std(&mut self.module_map)?;
|
let std = compile_std(&mut self.module_map)?;
|
||||||
modules.insert(std.module_id, Rc::new(RefCell::new(compile_std(&mut self.module_map)?)));
|
let module_id = std.module_id;
|
||||||
module_ids.insert(std.name, std.module_id);
|
modules.insert(
|
||||||
modules.get(&std.module_id).unwrap()
|
std.module_id,
|
||||||
|
LinkerModule {
|
||||||
|
module: Rc::new(RefCell::new(std)),
|
||||||
|
function_imports: HashMap::new(),
|
||||||
|
type_imports: HashMap::new(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
module_ids.insert(module_name.clone(), module_id);
|
||||||
|
modules.get(&module_id).unwrap()
|
||||||
} else {
|
} else {
|
||||||
let file_path = PathBuf::from(&context.base.clone()).join(module_name.to_owned() + ".reid");
|
let file_path = PathBuf::from(&context.base.clone()).join(module_name.to_owned() + ".reid");
|
||||||
|
|
||||||
@ -141,7 +176,13 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
let (id, tokens) = match parse_module(&source, module_name.clone(), &mut self.module_map) {
|
let (id, tokens) = match parse_module(
|
||||||
|
&source,
|
||||||
|
module_name.clone(),
|
||||||
|
Some(file_path.clone()),
|
||||||
|
&mut self.module_map,
|
||||||
|
None,
|
||||||
|
) {
|
||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
state.ok::<_, Infallible>(
|
state.ok::<_, Infallible>(
|
||||||
@ -156,21 +197,40 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
match compile_module(id, tokens, &mut self.module_map, Some(file_path), false) {
|
match compile_module(id, tokens, &mut self.module_map, Some(file_path), false) {
|
||||||
Ok(imported_module) => {
|
Ok(res) => match res {
|
||||||
if imported_module.is_main {
|
Ok(imported_module) => {
|
||||||
|
if imported_module.is_main {
|
||||||
|
state.ok::<_, Infallible>(
|
||||||
|
Err(ErrorKind::TriedLinkingMain(module_name.clone())),
|
||||||
|
import.1,
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let module_id = imported_module.module_id;
|
||||||
|
module_ids.insert(imported_module.name.clone(), imported_module.module_id);
|
||||||
|
modules.insert(
|
||||||
|
module_id,
|
||||||
|
LinkerModule {
|
||||||
|
module: Rc::new(RefCell::new(imported_module)),
|
||||||
|
function_imports: HashMap::new(),
|
||||||
|
type_imports: HashMap::new(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
let imported = modules.get_mut(&module_id).unwrap();
|
||||||
|
module_queue.push(imported.clone());
|
||||||
|
imported
|
||||||
|
}
|
||||||
|
Err((_, err)) => {
|
||||||
state.ok::<_, Infallible>(
|
state.ok::<_, Infallible>(
|
||||||
Err(ErrorKind::TriedLinkingMain(module_name.clone())),
|
Err(ErrorKind::ModuleCompilationError(
|
||||||
|
module_name.clone(),
|
||||||
|
format!("{}", err),
|
||||||
|
)),
|
||||||
import.1,
|
import.1,
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let module_id = imported_module.module_id;
|
},
|
||||||
module_ids.insert(imported_module.name.clone(), imported_module.module_id);
|
|
||||||
modules.insert(module_id, Rc::new(RefCell::new(imported_module)));
|
|
||||||
let imported = modules.get_mut(&module_id).unwrap();
|
|
||||||
modules_to_process.push(imported.clone());
|
|
||||||
imported
|
|
||||||
}
|
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
state.ok::<_, Infallible>(
|
state.ok::<_, Infallible>(
|
||||||
Err(ErrorKind::ModuleCompilationError(
|
Err(ErrorKind::ModuleCompilationError(
|
||||||
@ -182,66 +242,151 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let imported_module = imported.module.borrow();
|
||||||
|
|
||||||
|
if let Some(func) = imported_module.functions.iter().find(|f| f.name == *import_name) {
|
||||||
|
// If the imported item is a function, add it to the list of imported functions
|
||||||
|
importer
|
||||||
|
.function_imports
|
||||||
|
.insert(func.name.clone(), (imported_module.module_id, import.1));
|
||||||
|
} else if let Some(ty) = imported_module.typedefs.iter().find(|t| t.name == *import_name) {
|
||||||
|
// If the imported item is a type, add it to the list of imported types
|
||||||
|
// imported_types.insert((CustomTypeKey(ty.name.clone(), ty.source_module), true));
|
||||||
|
importer
|
||||||
|
.type_imports
|
||||||
|
.insert(ty.name.clone(), (imported_module.module_id, import.1));
|
||||||
}
|
}
|
||||||
.borrow_mut();
|
}
|
||||||
|
|
||||||
let import_name = unsafe { path.get_unchecked(1) };
|
let module_id = importer_mod.module_id;
|
||||||
let import_id = imported.module_id;
|
drop(importer_mod);
|
||||||
|
modules.insert(module_id, importer);
|
||||||
|
}
|
||||||
|
|
||||||
let mut imported_types = Vec::new();
|
for (_, linker_module) in &modules {
|
||||||
|
let mut importer_module = linker_module.module.borrow_mut();
|
||||||
|
|
||||||
if let Some(func) = imported.functions.iter_mut().find(|f| f.name == *import_name) {
|
let mut unresolved_types = HashMap::new();
|
||||||
let func_name = func.name.clone();
|
|
||||||
|
|
||||||
if !func.is_pub {
|
// 1. Import functions and find all types that are dependencies of
|
||||||
|
// functions
|
||||||
|
for (name, (function_source, import_meta)) in &linker_module.function_imports {
|
||||||
|
let mut function_module = modules.get(&function_source).unwrap().module.borrow_mut();
|
||||||
|
let func_module_name = function_module.name.clone();
|
||||||
|
let func_module_id = function_module.module_id;
|
||||||
|
|
||||||
|
let function = function_module.functions.iter_mut().find(|f| f.name == *name).unwrap();
|
||||||
|
|
||||||
|
// If function is not pub, error
|
||||||
|
if !function.is_pub {
|
||||||
|
state.ok::<_, Infallible>(
|
||||||
|
Err(ErrorKind::FunctionIsPrivate(func_module_name, function.name.clone())),
|
||||||
|
import_meta.clone(),
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If function already exists, error
|
||||||
|
if let Some(existing) = importer_module.functions.iter().find(|f| f.name == *name) {
|
||||||
|
if let Err(e) = existing.equals_as_imported(&function) {
|
||||||
state.ok::<_, Infallible>(
|
state.ok::<_, Infallible>(
|
||||||
Err(ErrorKind::FunctionIsPrivate(module_name.clone(), func_name.clone())),
|
Err(ErrorKind::FunctionImportIssue(func_module_name, name.clone(), e)),
|
||||||
import.1,
|
import_meta.clone(),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function.is_imported = true;
|
||||||
|
|
||||||
|
for ty in import_type(&function.return_type) {
|
||||||
|
unresolved_types.insert(ty, (import_meta.clone(), true));
|
||||||
|
}
|
||||||
|
for param in &function.parameters {
|
||||||
|
for ty in import_type(¶m.ty) {
|
||||||
|
unresolved_types.insert(ty, (import_meta.clone(), true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
importer_module.functions.push(FunctionDefinition {
|
||||||
|
name: function.name.clone(),
|
||||||
|
documentation: function.documentation.clone(),
|
||||||
|
linkage_name: None,
|
||||||
|
is_pub: false,
|
||||||
|
is_imported: false,
|
||||||
|
return_type: function.return_type.clone(),
|
||||||
|
parameters: function.parameters.clone(),
|
||||||
|
kind: super::FunctionDefinitionKind::Extern(true),
|
||||||
|
source: Some(func_module_id),
|
||||||
|
signature_meta: function.signature(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Add all manually imported types to the list of types that need
|
||||||
|
// to be resolved and recursed
|
||||||
|
for (name, (source_module, meta)) in &linker_module.type_imports {
|
||||||
|
let imported_ty_key = CustomTypeKey(name.clone(), source_module.clone());
|
||||||
|
|
||||||
|
let imported_ty = TypeKind::CustomType(imported_ty_key.clone());
|
||||||
|
let imported = modules.get(&imported_ty_key.1).unwrap().module.borrow();
|
||||||
|
|
||||||
|
for (ty, func) in &imported.associated_functions {
|
||||||
|
if *ty != imported_ty {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
func.is_imported = true;
|
for ty in import_type(&func.return_type) {
|
||||||
|
if unresolved_types.contains_key(&ty) {
|
||||||
if let Some(existing) = importer_module.functions.iter().find(|f| f.name == *func_name) {
|
continue;
|
||||||
if let Err(e) = existing.equals_as_imported(func) {
|
}
|
||||||
state.ok::<_, Infallible>(
|
unresolved_types.insert(ty, (meta.clone(), true));
|
||||||
Err(ErrorKind::FunctionImportIssue(
|
}
|
||||||
module_name.clone(),
|
for param in &func.parameters {
|
||||||
func_name.clone(),
|
for ty in import_type(¶m.ty) {
|
||||||
e,
|
if unresolved_types.contains_key(&ty) {
|
||||||
)),
|
continue;
|
||||||
import.1,
|
}
|
||||||
);
|
unresolved_types.insert(ty, (meta.clone(), true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let types = import_type(&func.return_type, false);
|
unresolved_types.insert(imported_ty_key.clone(), (meta.clone(), false));
|
||||||
let return_type = func.return_type.clone();
|
}
|
||||||
imported_types.extend(types);
|
|
||||||
|
|
||||||
let mut param_tys = Vec::new();
|
// 3. Recurse these types to find their true sources, find their
|
||||||
for param in &func.parameters {
|
// dependencies, and list them all. Store manually imported types
|
||||||
let types = import_type(¶m.ty, false);
|
// in a separate mapping for later.
|
||||||
imported_types.extend(types);
|
let mut imported_types = HashMap::new();
|
||||||
param_tys.push(param.clone());
|
let mut foreign_keys = HashSet::new();
|
||||||
}
|
|
||||||
|
|
||||||
importer_module.functions.push(FunctionDefinition {
|
let mut already_imported_binops = HashSet::new();
|
||||||
name: func_name.clone(),
|
|
||||||
linkage_name: None,
|
|
||||||
is_pub: false,
|
|
||||||
is_imported: false,
|
|
||||||
return_type,
|
|
||||||
parameters: param_tys,
|
|
||||||
kind: super::FunctionDefinitionKind::Extern(true),
|
|
||||||
source: Some(imported.module_id),
|
|
||||||
});
|
|
||||||
} else if let Some(ty) = imported.typedefs.iter_mut().find(|f| f.name == *import_name) {
|
|
||||||
let external_key = CustomTypeKey(ty.name.clone(), ty.source_module);
|
|
||||||
let imported_ty = TypeKind::CustomType(external_key.clone());
|
|
||||||
imported_types.push((external_key, true));
|
|
||||||
|
|
||||||
|
for (ty, (meta, is_dependency)) in unresolved_types {
|
||||||
|
// First deal with manually imported types
|
||||||
|
if !is_dependency {
|
||||||
|
// Add them to the list of foreign types (types that are
|
||||||
|
// later replaced in-source by name)
|
||||||
|
let imported_ty_key = match resolve_type(&ty, &modules) {
|
||||||
|
Ok(ty) => {
|
||||||
|
foreign_keys.insert(CustomTypeKey(ty.0.clone(), importer_module.module_id));
|
||||||
|
ty
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
state.note_errors(&vec![e], meta);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
imported_types.insert(CustomTypeKey(ty.0.clone(), importer_module.module_id), ty.1);
|
||||||
|
|
||||||
|
let mut imported = modules.get(&imported_ty_key.1).unwrap().module.borrow_mut();
|
||||||
|
let imported_module_name = imported.name.clone();
|
||||||
|
let imported_module_id = imported.module_id.clone();
|
||||||
|
let imported_ty = TypeKind::CustomType(imported_ty_key);
|
||||||
|
|
||||||
|
// Add all binary operators that are defined for this type
|
||||||
for binop in &mut imported.binop_defs {
|
for binop in &mut imported.binop_defs {
|
||||||
if binop.lhs.ty != imported_ty && binop.rhs.ty != imported_ty {
|
if binop.lhs.ty != imported_ty && binop.rhs.ty != imported_ty {
|
||||||
continue;
|
continue;
|
||||||
@ -272,6 +417,7 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Import all functions that are associated with this type
|
||||||
for (ty, func) in &mut imported.associated_functions {
|
for (ty, func) in &mut imported.associated_functions {
|
||||||
if *ty != imported_ty {
|
if *ty != imported_ty {
|
||||||
continue;
|
continue;
|
||||||
@ -281,8 +427,11 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
|
|
||||||
if !func.is_pub {
|
if !func.is_pub {
|
||||||
state.ok::<_, Infallible>(
|
state.ok::<_, Infallible>(
|
||||||
Err(ErrorKind::FunctionIsPrivate(module_name.clone(), func_name.clone())),
|
Err(ErrorKind::FunctionIsPrivate(
|
||||||
import.1,
|
imported_module_name.clone(),
|
||||||
|
func_name.clone(),
|
||||||
|
)),
|
||||||
|
meta.clone(),
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -297,116 +446,81 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
if let Err(e) = existing.equals_as_imported(func) {
|
if let Err(e) = existing.equals_as_imported(func) {
|
||||||
state.ok::<_, Infallible>(
|
state.ok::<_, Infallible>(
|
||||||
Err(ErrorKind::FunctionImportIssue(
|
Err(ErrorKind::FunctionImportIssue(
|
||||||
module_name.clone(),
|
imported_module_name.clone(),
|
||||||
func_name.clone(),
|
func_name.clone(),
|
||||||
e,
|
e,
|
||||||
)),
|
)),
|
||||||
import.1,
|
meta.clone(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let types = import_type(&func.return_type, false);
|
|
||||||
let return_type = func.return_type.clone();
|
|
||||||
imported_types.extend(types);
|
|
||||||
|
|
||||||
let mut param_tys = Vec::new();
|
|
||||||
for param in &func.parameters {
|
|
||||||
let types = import_type(¶m.ty, false);
|
|
||||||
imported_types.extend(types);
|
|
||||||
param_tys.push(param.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
importer_module.associated_functions.push((
|
importer_module.associated_functions.push((
|
||||||
ty.clone(),
|
ty.clone(),
|
||||||
FunctionDefinition {
|
FunctionDefinition {
|
||||||
name: func_name.clone(),
|
name: func_name.clone(),
|
||||||
|
documentation: func.documentation.clone(),
|
||||||
linkage_name: Some(format!("{}::{}", ty, func_name)),
|
linkage_name: Some(format!("{}::{}", ty, func_name)),
|
||||||
is_pub: false,
|
is_pub: false,
|
||||||
is_imported: false,
|
is_imported: false,
|
||||||
return_type,
|
return_type: func.return_type.clone(),
|
||||||
parameters: param_tys,
|
parameters: func.parameters.clone(),
|
||||||
kind: super::FunctionDefinitionKind::Extern(true),
|
kind: super::FunctionDefinitionKind::Extern(true),
|
||||||
source: Some(import_id),
|
source: Some(imported_module_id),
|
||||||
|
signature_meta: func.signature_meta,
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
state.ok::<_, Infallible>(
|
|
||||||
Err(ErrorKind::ImportDoesNotExist(module_name.clone(), import_name.clone())),
|
|
||||||
import.1,
|
|
||||||
);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
match resolve_types_recursively(&TypeKind::CustomType(ty.clone()), &modules, HashSet::new()) {
|
||||||
let mut seen = HashSet::new();
|
Ok(resolved) => {
|
||||||
let mut current_extern_types = HashSet::new();
|
imported_types.extend(resolved);
|
||||||
seen.extend(imported_types.clone().iter().map(|t| t.0.clone()));
|
|
||||||
current_extern_types.extend(imported_types.clone().iter().filter(|t| t.1).map(|t| t.0.clone()));
|
|
||||||
for extern_type in ¤t_extern_types {
|
|
||||||
extern_types.insert(extern_type.0.clone(), extern_type.1);
|
|
||||||
}
|
|
||||||
|
|
||||||
let imported_mod_id = imported.module_id;
|
|
||||||
let imported_mod_typedefs = &mut imported.typedefs;
|
|
||||||
|
|
||||||
for typekey in imported_types.clone() {
|
|
||||||
let typedef = imported_mod_typedefs
|
|
||||||
.iter()
|
|
||||||
.find(|ty| CustomTypeKey(ty.name.clone(), imported_mod_id) == typekey.0)
|
|
||||||
.unwrap();
|
|
||||||
let inner = find_inner_types(typedef, seen.clone(), imported_mod_id);
|
|
||||||
seen.extend(inner.iter().cloned());
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Unable to import same-named type from multiple places..
|
|
||||||
let seen = seen
|
|
||||||
.difference(&already_imported_types)
|
|
||||||
.cloned()
|
|
||||||
.collect::<HashSet<_>>();
|
|
||||||
|
|
||||||
already_imported_types.extend(seen.clone());
|
|
||||||
|
|
||||||
for typekey in &already_imported_types {
|
|
||||||
if current_extern_types.contains(typekey) {
|
|
||||||
let module_id = importer_module.module_id;
|
|
||||||
let typedef = importer_module
|
|
||||||
.typedefs
|
|
||||||
.iter_mut()
|
|
||||||
.find(|t| t.name == typekey.0 && t.source_module == typekey.1);
|
|
||||||
if let Some(typedef) = typedef {
|
|
||||||
typedef.importer = Some(module_id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
Err(e) => {
|
||||||
|
state.note_errors(&vec![e], meta);
|
||||||
for typekey in seen.into_iter() {
|
return Ok(());
|
||||||
let mut typedef = imported_mod_typedefs
|
|
||||||
.iter()
|
|
||||||
.find(|ty| CustomTypeKey(ty.name.clone(), imported_mod_id) == typekey)
|
|
||||||
.unwrap()
|
|
||||||
.clone();
|
|
||||||
|
|
||||||
if current_extern_types.contains(&typekey) {
|
|
||||||
typedef = TypeDefinition {
|
|
||||||
importer: Some(importer_module.module_id),
|
|
||||||
..typedef
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
importer_module.typedefs.push(typedef);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut typedef_keys = HashMap::new();
|
||||||
|
// 4. Import all listed types.
|
||||||
|
for (importer_typekey, imported_module_id) in &imported_types {
|
||||||
|
let importee_typekey = CustomTypeKey(importer_typekey.0.clone(), *imported_module_id);
|
||||||
|
if let Some(module_id) = typedef_keys.get(&importee_typekey) {
|
||||||
|
if *module_id != importer_module.module_id {
|
||||||
|
typedef_keys.insert(importee_typekey.clone(), importer_typekey.1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
typedef_keys.insert(importee_typekey.clone(), importer_typekey.1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (typedef_key, importer_module_id) in &typedef_keys {
|
||||||
|
let imported_ty_module = modules.get(&typedef_key.1).unwrap().module.borrow();
|
||||||
|
if let Some(typedef) = imported_ty_module
|
||||||
|
.typedefs
|
||||||
|
.iter()
|
||||||
|
.find(|ty| CustomTypeKey(ty.name.clone(), ty.source_module) == *typedef_key)
|
||||||
|
.cloned()
|
||||||
|
{
|
||||||
|
importer_module.typedefs.push(TypeDefinition {
|
||||||
|
importer: Some(*importer_module_id),
|
||||||
|
..typedef
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
state
|
state
|
||||||
.scope
|
.scope
|
||||||
.data
|
.data
|
||||||
.extern_imported_types
|
.foreign_types
|
||||||
.insert(importer_module.module_id, extern_types);
|
.insert(importer_module.module_id, imported_types);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut modules: Vec<Module> = modules
|
let mut modules: Vec<Module> = modules
|
||||||
.into_values()
|
.into_values()
|
||||||
.map(|v| Rc::into_inner(v).unwrap().into_inner())
|
.map(|v| Rc::into_inner(v.module).unwrap().into_inner())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
for module in modules.drain(..) {
|
for module in modules.drain(..) {
|
||||||
@ -416,19 +530,33 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn module(&mut self, module: &mut Module, state: PassState<Self::Data, Self::TError>) -> PassResult {
|
||||||
|
let foreign_types = &state.scope.data.foreign_types.get(&module.module_id);
|
||||||
|
if let Some(foreign_types) = foreign_types {
|
||||||
|
for ty in &mut module.typedefs {
|
||||||
|
match &mut ty.kind {
|
||||||
|
TypeDefinitionKind::Struct(StructType(fields)) => {
|
||||||
|
for field in fields {
|
||||||
|
field.1 = field.1.update_imported(foreign_types);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn function(
|
fn function(
|
||||||
&mut self,
|
&mut self,
|
||||||
function: &mut FunctionDefinition,
|
function: &mut FunctionDefinition,
|
||||||
state: PassState<Self::Data, Self::TError>,
|
state: PassState<Self::Data, Self::TError>,
|
||||||
) -> PassResult {
|
) -> PassResult {
|
||||||
if matches!(function.kind, FunctionDefinitionKind::Local(_, _)) {
|
let mod_id = state.scope.module_id.unwrap();
|
||||||
let mod_id = state.scope.module_id.unwrap();
|
let foreign_types = &state.scope.data.foreign_types.get(&mod_id);
|
||||||
let extern_types = &state.scope.data.extern_imported_types.get(&mod_id);
|
if let Some(foreign_types) = foreign_types {
|
||||||
if let Some(extern_types) = extern_types {
|
function.return_type = function.return_type.update_imported(*foreign_types);
|
||||||
function.return_type = function.return_type.update_imported(*extern_types, mod_id);
|
for param in function.parameters.iter_mut() {
|
||||||
for param in function.parameters.iter_mut() {
|
param.ty = param.ty.update_imported(foreign_types);
|
||||||
param.ty = param.ty.update_imported(extern_types, mod_id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -436,11 +564,11 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
|
|
||||||
fn stmt(&mut self, stmt: &mut super::Statement, state: PassState<Self::Data, Self::TError>) -> PassResult {
|
fn stmt(&mut self, stmt: &mut super::Statement, state: PassState<Self::Data, Self::TError>) -> PassResult {
|
||||||
let mod_id = state.scope.module_id.unwrap();
|
let mod_id = state.scope.module_id.unwrap();
|
||||||
let extern_types = &state.scope.data.extern_imported_types.get(&mod_id);
|
let foreign_types = &state.scope.data.foreign_types.get(&mod_id);
|
||||||
if let Some(extern_types) = extern_types {
|
if let Some(foreign_types) = foreign_types {
|
||||||
match &mut stmt.0 {
|
match &mut stmt.0 {
|
||||||
super::StmtKind::Let(var_ref, _, _) => {
|
super::StmtKind::Let(var_ref, _, _) => {
|
||||||
var_ref.0 = var_ref.0.update_imported(extern_types, mod_id);
|
var_ref.0 = var_ref.0.update_imported(foreign_types);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@ -450,25 +578,29 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
|
|
||||||
fn expr(&mut self, expr: &mut super::Expression, state: PassState<Self::Data, Self::TError>) -> PassResult {
|
fn expr(&mut self, expr: &mut super::Expression, state: PassState<Self::Data, Self::TError>) -> PassResult {
|
||||||
let mod_id = state.scope.module_id.unwrap();
|
let mod_id = state.scope.module_id.unwrap();
|
||||||
let extern_types = &state.scope.data.extern_imported_types.get(&mod_id);
|
let foreign_types = &state.scope.data.foreign_types.get(&mod_id);
|
||||||
if let Some(extern_types) = extern_types {
|
if let Some(foreign_types) = foreign_types {
|
||||||
match &mut expr.0 {
|
match &mut expr.0 {
|
||||||
super::ExprKind::Variable(var_ref) => {
|
super::ExprKind::Variable(var_ref) => {
|
||||||
var_ref.0 = var_ref.0.update_imported(extern_types, mod_id);
|
var_ref.0 = var_ref.0.update_imported(foreign_types);
|
||||||
}
|
}
|
||||||
super::ExprKind::Indexed(.., type_kind, _) => {
|
super::ExprKind::Indexed(.., type_kind, _) => *type_kind = type_kind.update_imported(foreign_types),
|
||||||
*type_kind = type_kind.update_imported(extern_types, mod_id)
|
super::ExprKind::Accessed(.., type_kind, _, _) => *type_kind = type_kind.update_imported(foreign_types),
|
||||||
}
|
super::ExprKind::BinOp(.., type_kind) => *type_kind = type_kind.update_imported(foreign_types),
|
||||||
super::ExprKind::Accessed(.., type_kind, _, _) => {
|
|
||||||
*type_kind = type_kind.update_imported(extern_types, mod_id)
|
|
||||||
}
|
|
||||||
super::ExprKind::BinOp(.., type_kind) => *type_kind = type_kind.update_imported(extern_types, mod_id),
|
|
||||||
|
|
||||||
super::ExprKind::Borrow(..) => {}
|
super::ExprKind::Borrow(..) => {}
|
||||||
super::ExprKind::Deref(..) => {}
|
super::ExprKind::Deref(..) => {}
|
||||||
super::ExprKind::CastTo(_, type_kind) => *type_kind = type_kind.update_imported(extern_types, mod_id),
|
super::ExprKind::CastTo(_, type_kind) => *type_kind = type_kind.update_imported(foreign_types),
|
||||||
super::ExprKind::AssociatedFunctionCall(type_kind, _) => {
|
super::ExprKind::AssociatedFunctionCall(type_kind, fn_call) => {
|
||||||
*type_kind = type_kind.update_imported(extern_types, mod_id)
|
*type_kind = type_kind.update_imported(foreign_types);
|
||||||
|
fn_call.return_type = fn_call.return_type.update_imported(foreign_types);
|
||||||
|
}
|
||||||
|
super::ExprKind::Struct(key, _) => {
|
||||||
|
*key = if let Some(mod_id) = foreign_types.get(&key) {
|
||||||
|
CustomTypeKey(key.0.clone(), *mod_id)
|
||||||
|
} else {
|
||||||
|
key.clone()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@ -478,78 +610,103 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TypeKind {
|
impl TypeKind {
|
||||||
fn update_imported(
|
fn update_imported(&self, foreign_types: &HashMap<CustomTypeKey, SourceModuleId>) -> TypeKind {
|
||||||
&self,
|
|
||||||
extern_types: &HashMap<String, SourceModuleId>,
|
|
||||||
importer_mod_id: SourceModuleId,
|
|
||||||
) -> TypeKind {
|
|
||||||
match &self {
|
match &self {
|
||||||
TypeKind::Array(type_kind, len) => {
|
TypeKind::Array(type_kind, len) => {
|
||||||
TypeKind::Array(Box::new(type_kind.update_imported(extern_types, importer_mod_id)), *len)
|
TypeKind::Array(Box::new(type_kind.update_imported(foreign_types)), *len)
|
||||||
}
|
}
|
||||||
TypeKind::CustomType(custom_type_key) => {
|
TypeKind::CustomType(custom_type_key) => {
|
||||||
if let Some(mod_id) = extern_types.get(&custom_type_key.0) {
|
if let Some(mod_id) = foreign_types.get(&custom_type_key) {
|
||||||
TypeKind::CustomType(CustomTypeKey(custom_type_key.0.clone(), *mod_id))
|
TypeKind::CustomType(CustomTypeKey(custom_type_key.0.clone(), *mod_id))
|
||||||
} else {
|
} else {
|
||||||
self.clone()
|
self.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TypeKind::Borrow(type_kind, mutable) => TypeKind::Borrow(
|
TypeKind::Borrow(type_kind, mutable) => {
|
||||||
Box::new(type_kind.update_imported(extern_types, importer_mod_id)),
|
TypeKind::Borrow(Box::new(type_kind.update_imported(foreign_types)), *mutable)
|
||||||
*mutable,
|
|
||||||
),
|
|
||||||
TypeKind::UserPtr(type_kind) => {
|
|
||||||
TypeKind::UserPtr(Box::new(type_kind.update_imported(extern_types, importer_mod_id)))
|
|
||||||
}
|
|
||||||
TypeKind::CodegenPtr(type_kind) => {
|
|
||||||
TypeKind::CodegenPtr(Box::new(type_kind.update_imported(extern_types, importer_mod_id)))
|
|
||||||
}
|
}
|
||||||
|
TypeKind::UserPtr(type_kind) => TypeKind::UserPtr(Box::new(type_kind.update_imported(foreign_types))),
|
||||||
|
TypeKind::CodegenPtr(type_kind) => TypeKind::CodegenPtr(Box::new(type_kind.update_imported(foreign_types))),
|
||||||
_ => self.clone(),
|
_ => self.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn import_type(ty: &TypeKind, usable_import: bool) -> Vec<(CustomTypeKey, bool)> {
|
fn import_type(ty: &TypeKind) -> Vec<CustomTypeKey> {
|
||||||
let mut imported_types = Vec::new();
|
let mut imported_types = Vec::new();
|
||||||
match &ty {
|
match &ty {
|
||||||
TypeKind::CustomType(key) => imported_types.push((key.clone(), usable_import)),
|
TypeKind::CustomType(key) => imported_types.push(key.clone()),
|
||||||
TypeKind::Borrow(ty, _) => imported_types.extend(import_type(ty, usable_import)),
|
TypeKind::Borrow(ty, _) => imported_types.extend(import_type(ty)),
|
||||||
TypeKind::Array(ty, _) => imported_types.extend(import_type(ty, usable_import)),
|
TypeKind::Array(ty, _) => imported_types.extend(import_type(ty)),
|
||||||
TypeKind::UserPtr(ty) => imported_types.extend(import_type(ty, usable_import)),
|
TypeKind::UserPtr(ty) => imported_types.extend(import_type(ty)),
|
||||||
TypeKind::CodegenPtr(ty) => imported_types.extend(import_type(ty, usable_import)),
|
TypeKind::CodegenPtr(ty) => imported_types.extend(import_type(ty)),
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
imported_types
|
imported_types
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_inner_types(
|
fn resolve_type(
|
||||||
typedef: &TypeDefinition,
|
ty: &CustomTypeKey,
|
||||||
mut seen: HashSet<CustomTypeKey>,
|
modules: &HashMap<SourceModuleId, LinkerModule>,
|
||||||
mod_id: SourceModuleId,
|
) -> Result<CustomTypeKey, ErrorKind> {
|
||||||
) -> Vec<CustomTypeKey> {
|
let mut source_module_id = ty.1;
|
||||||
match &typedef.kind {
|
let mut seen = HashSet::new();
|
||||||
crate::mir::TypeDefinitionKind::Struct(struct_type) => {
|
loop {
|
||||||
let typenames = struct_type
|
seen.insert(source_module_id);
|
||||||
.0
|
let source_module = modules.get(&source_module_id).unwrap();
|
||||||
.iter()
|
if let Some((new_module_id, _)) = source_module.type_imports.get(&ty.0) {
|
||||||
.filter(|t| matches!(t.1, TypeKind::CustomType(..)))
|
if seen.contains(new_module_id) {
|
||||||
.map(|t| match &t.1 {
|
return Err(ErrorKind::RecursiveTypeImport(ty.0.clone()));
|
||||||
TypeKind::CustomType(CustomTypeKey(t, _)) => t,
|
|
||||||
_ => panic!(),
|
|
||||||
})
|
|
||||||
.cloned()
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
for typename in typenames {
|
|
||||||
if seen.contains(&CustomTypeKey(typename.clone(), mod_id)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let inner = find_inner_types(typedef, seen.clone(), mod_id);
|
|
||||||
seen.insert(CustomTypeKey(typename, mod_id));
|
|
||||||
seen.extend(inner);
|
|
||||||
}
|
}
|
||||||
|
source_module_id = *new_module_id;
|
||||||
seen.into_iter().collect()
|
} else {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(CustomTypeKey(ty.0.clone(), source_module_id))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve_types_recursively(
|
||||||
|
ty: &TypeKind,
|
||||||
|
modules: &HashMap<SourceModuleId, LinkerModule>,
|
||||||
|
mut seen: HashSet<CustomTypeKey>,
|
||||||
|
) -> Result<HashMap<CustomTypeKey, SourceModuleId>, ErrorKind> {
|
||||||
|
let mut types = HashMap::new();
|
||||||
|
match ty {
|
||||||
|
TypeKind::CustomType(type_key) => {
|
||||||
|
let resolved_ty = resolve_type(type_key, modules)?;
|
||||||
|
|
||||||
|
if seen.contains(&resolved_ty) {
|
||||||
|
return Err(ErrorKind::CyclicalType(type_key.0.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
types.insert(type_key.clone(), resolved_ty.1);
|
||||||
|
seen.insert(resolved_ty.clone());
|
||||||
|
|
||||||
|
let resolved = modules
|
||||||
|
.get(&resolved_ty.1)
|
||||||
|
.unwrap()
|
||||||
|
.module
|
||||||
|
.borrow()
|
||||||
|
.typedefs
|
||||||
|
.iter()
|
||||||
|
.find(|t| t.name == resolved_ty.0)
|
||||||
|
.ok_or(ErrorKind::NoSuchTypeInModule(type_key.clone()))
|
||||||
|
.cloned()?;
|
||||||
|
match resolved.kind {
|
||||||
|
TypeDefinitionKind::Struct(StructType(fields)) => {
|
||||||
|
for field in fields {
|
||||||
|
types.extend(resolve_types_recursively(&field.1, modules, seen.clone())?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TypeKind::Array(type_kind, _) => types.extend(resolve_types_recursively(&type_kind, modules, seen.clone())?),
|
||||||
|
TypeKind::Borrow(type_kind, _) => types.extend(resolve_types_recursively(&type_kind, modules, seen.clone())?),
|
||||||
|
TypeKind::UserPtr(type_kind) => types.extend(resolve_types_recursively(&type_kind, modules, seen.clone())?),
|
||||||
|
TypeKind::CodegenPtr(type_kind) => types.extend(resolve_types_recursively(&type_kind, modules, seen.clone())?),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
Ok(types)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -177,12 +177,7 @@ impl mir::Expression {
|
|||||||
globals.extend(expression.gen_macros(data, state, map));
|
globals.extend(expression.gen_macros(data, state, map));
|
||||||
globals.extend(expression1.gen_macros(data, state, map));
|
globals.extend(expression1.gen_macros(data, state, map));
|
||||||
}
|
}
|
||||||
mir::ExprKind::AssociatedFunctionCall(
|
mir::ExprKind::AssociatedFunctionCall(_, FunctionCall { parameters, .. }) => {
|
||||||
_,
|
|
||||||
FunctionCall {
|
|
||||||
parameters, is_macro, ..
|
|
||||||
},
|
|
||||||
) => {
|
|
||||||
for expression in parameters {
|
for expression in parameters {
|
||||||
globals.extend(expression.gen_macros(data, state, map));
|
globals.extend(expression.gen_macros(data, state, map));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -55,6 +55,28 @@ impl Metadata {
|
|||||||
pub fn contains(&self, token_idx: usize) -> bool {
|
pub fn contains(&self, token_idx: usize) -> bool {
|
||||||
return token_idx >= self.range.start && token_idx <= self.range.end;
|
return token_idx >= self.range.start && token_idx <= self.range.end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn after(&self, cutoff: usize) -> Metadata {
|
||||||
|
Metadata {
|
||||||
|
source_module_id: self.source_module_id,
|
||||||
|
range: TokenRange {
|
||||||
|
start: cutoff.max(self.range.start),
|
||||||
|
end: cutoff.max(self.range.end),
|
||||||
|
},
|
||||||
|
position: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn before(&self, cutoff: usize) -> Metadata {
|
||||||
|
Metadata {
|
||||||
|
source_module_id: self.source_module_id,
|
||||||
|
range: TokenRange {
|
||||||
|
start: cutoff.min(self.range.start),
|
||||||
|
end: cutoff.min(self.range.end),
|
||||||
|
},
|
||||||
|
position: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Add for Metadata {
|
impl std::ops::Add for Metadata {
|
||||||
@ -83,7 +105,7 @@ impl TokenRange {
|
|||||||
#[derive(Hash, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
|
#[derive(Hash, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
|
||||||
pub struct CustomTypeKey(pub String, pub SourceModuleId);
|
pub struct CustomTypeKey(pub String, pub SourceModuleId);
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
|
||||||
pub enum TypeKind {
|
pub enum TypeKind {
|
||||||
Bool,
|
Bool,
|
||||||
I8,
|
I8,
|
||||||
@ -98,7 +120,7 @@ pub enum TypeKind {
|
|||||||
U128,
|
U128,
|
||||||
Void,
|
Void,
|
||||||
F16,
|
F16,
|
||||||
F32B,
|
F16B,
|
||||||
F32,
|
F32,
|
||||||
F64,
|
F64,
|
||||||
F128,
|
F128,
|
||||||
@ -198,7 +220,7 @@ impl Literal {
|
|||||||
Literal::Vague(VagueLiteral::Number(_)) => TypeKind::Vague(VagueType::Integer),
|
Literal::Vague(VagueLiteral::Number(_)) => TypeKind::Vague(VagueType::Integer),
|
||||||
Literal::Vague(VagueLiteral::Decimal(_)) => TypeKind::Vague(VagueType::Decimal),
|
Literal::Vague(VagueLiteral::Decimal(_)) => TypeKind::Vague(VagueType::Decimal),
|
||||||
Literal::F16(_) => TypeKind::F16,
|
Literal::F16(_) => TypeKind::F16,
|
||||||
Literal::F32B(_) => TypeKind::F32B,
|
Literal::F32B(_) => TypeKind::F16B,
|
||||||
Literal::F32(_) => TypeKind::F32,
|
Literal::F32(_) => TypeKind::F32,
|
||||||
Literal::F64(_) => TypeKind::F64,
|
Literal::F64(_) => TypeKind::F64,
|
||||||
Literal::F80(_) => TypeKind::F80,
|
Literal::F80(_) => TypeKind::F80,
|
||||||
@ -256,7 +278,7 @@ pub enum ReturnKind {
|
|||||||
pub struct NamedVariableRef(pub TypeKind, pub String, pub Metadata);
|
pub struct NamedVariableRef(pub TypeKind, pub String, pub Metadata);
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct Import(pub Vec<String>, pub Metadata);
|
pub struct Import(pub Vec<(String, Metadata)>, pub Metadata);
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum ExprKind {
|
pub enum ExprKind {
|
||||||
@ -264,7 +286,7 @@ pub enum ExprKind {
|
|||||||
Indexed(Box<Expression>, TypeKind, Box<Expression>),
|
Indexed(Box<Expression>, TypeKind, Box<Expression>),
|
||||||
Accessed(Box<Expression>, TypeKind, String, Metadata),
|
Accessed(Box<Expression>, TypeKind, String, Metadata),
|
||||||
Array(Vec<Expression>),
|
Array(Vec<Expression>),
|
||||||
Struct(String, Vec<(String, Expression, Metadata)>),
|
Struct(CustomTypeKey, Vec<(String, Expression, Metadata)>),
|
||||||
Literal(Literal),
|
Literal(Literal),
|
||||||
BinOp(BinaryOperator, Box<Expression>, Box<Expression>, TypeKind),
|
BinOp(BinaryOperator, Box<Expression>, Box<Expression>, TypeKind),
|
||||||
FunctionCall(FunctionCall),
|
FunctionCall(FunctionCall),
|
||||||
@ -296,6 +318,7 @@ pub struct FunctionCall {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct FunctionDefinition {
|
pub struct FunctionDefinition {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub documentation: Option<String>,
|
||||||
pub linkage_name: Option<String>,
|
pub linkage_name: Option<String>,
|
||||||
/// Whether this function is visible to outside modules
|
/// Whether this function is visible to outside modules
|
||||||
pub is_pub: bool,
|
pub is_pub: bool,
|
||||||
@ -305,6 +328,7 @@ pub struct FunctionDefinition {
|
|||||||
pub parameters: Vec<FunctionParam>,
|
pub parameters: Vec<FunctionParam>,
|
||||||
pub kind: FunctionDefinitionKind,
|
pub kind: FunctionDefinitionKind,
|
||||||
pub source: Option<SourceModuleId>,
|
pub source: Option<SourceModuleId>,
|
||||||
|
pub signature_meta: Metadata,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, PartialOrd)]
|
#[derive(Debug, Clone, PartialEq, PartialOrd)]
|
||||||
@ -314,6 +338,16 @@ pub struct FunctionParam {
|
|||||||
pub meta: Metadata,
|
pub meta: Metadata,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FunctionParam {
|
||||||
|
pub fn from<T: Into<String>>(name: T, ty: TypeKind) -> FunctionParam {
|
||||||
|
FunctionParam {
|
||||||
|
name: name.into(),
|
||||||
|
ty: ty,
|
||||||
|
meta: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub enum SelfKind {
|
pub enum SelfKind {
|
||||||
Borrow,
|
Borrow,
|
||||||
MutBorrow,
|
MutBorrow,
|
||||||
@ -340,11 +374,7 @@ impl FunctionDefinition {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn signature(&self) -> Metadata {
|
pub fn signature(&self) -> Metadata {
|
||||||
match &self.kind {
|
self.signature_meta
|
||||||
FunctionDefinitionKind::Local(_, metadata) => metadata.clone(),
|
|
||||||
FunctionDefinitionKind::Extern(_) => Metadata::default(),
|
|
||||||
FunctionDefinitionKind::Intrinsic(_) => Metadata::default(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -450,6 +450,10 @@ impl Module {
|
|||||||
for function in &mut self.functions {
|
for function in &mut self.functions {
|
||||||
function.pass(pass, state, &mut scope.inner(), self.module_id)?;
|
function.pass(pass, state, &mut scope.inner(), self.module_id)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (_, function) in &mut self.associated_functions {
|
||||||
|
function.pass(pass, state, &mut scope.inner(), self.module_id)?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -141,7 +141,7 @@ impl TypeKind {
|
|||||||
| TypeKind::U64
|
| TypeKind::U64
|
||||||
| TypeKind::U128
|
| TypeKind::U128
|
||||||
| TypeKind::F16
|
| TypeKind::F16
|
||||||
| TypeKind::F32B
|
| TypeKind::F16B
|
||||||
| TypeKind::F32
|
| TypeKind::F32
|
||||||
| TypeKind::F64
|
| TypeKind::F64
|
||||||
| TypeKind::F80
|
| TypeKind::F80
|
||||||
@ -153,7 +153,7 @@ impl TypeKind {
|
|||||||
TypeKind::Vague(Vague::Unknown) => Ok(TypeKind::Vague(Vague::Decimal)),
|
TypeKind::Vague(Vague::Unknown) => Ok(TypeKind::Vague(Vague::Decimal)),
|
||||||
TypeKind::Vague(Vague::Decimal) => Ok(TypeKind::Vague(Vague::Decimal)),
|
TypeKind::Vague(Vague::Decimal) => Ok(TypeKind::Vague(Vague::Decimal)),
|
||||||
TypeKind::F16
|
TypeKind::F16
|
||||||
| TypeKind::F32B
|
| TypeKind::F16B
|
||||||
| TypeKind::F32
|
| TypeKind::F32
|
||||||
| TypeKind::F64
|
| TypeKind::F64
|
||||||
| TypeKind::F80
|
| TypeKind::F80
|
||||||
@ -207,7 +207,7 @@ impl TypeKind {
|
|||||||
},
|
},
|
||||||
(TypeKind::Vague(Vague::Decimal), other) | (other, TypeKind::Vague(Vague::Decimal)) => match other {
|
(TypeKind::Vague(Vague::Decimal), other) | (other, TypeKind::Vague(Vague::Decimal)) => match other {
|
||||||
TypeKind::F16
|
TypeKind::F16
|
||||||
| TypeKind::F32B
|
| TypeKind::F16B
|
||||||
| TypeKind::F32
|
| TypeKind::F32
|
||||||
| TypeKind::F64
|
| TypeKind::F64
|
||||||
| TypeKind::F80
|
| TypeKind::F80
|
||||||
@ -333,7 +333,7 @@ impl TypeKind {
|
|||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
.ok_or(ErrorKind::NoSuchType(
|
.ok_or(ErrorKind::NoSuchType(
|
||||||
custom_type_key.0.clone(),
|
custom_type_key.0.clone(),
|
||||||
state.module_id.unwrap(),
|
custom_type_key.1.clone(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
TypeKind::Borrow(type_kind, _) => type_kind.is_known(state),
|
TypeKind::Borrow(type_kind, _) => type_kind.is_known(state),
|
||||||
|
|||||||
@ -84,7 +84,7 @@ impl<'t> Pass for TypeCheck<'t> {
|
|||||||
fn check_typedefs_for_recursion<'a, 'b>(
|
fn check_typedefs_for_recursion<'a, 'b>(
|
||||||
defmap: &'b HashMap<&'a String, &'b TypeDefinition>,
|
defmap: &'b HashMap<&'a String, &'b TypeDefinition>,
|
||||||
typedef: &'b TypeDefinition,
|
typedef: &'b TypeDefinition,
|
||||||
mut seen: HashSet<String>,
|
seen: HashSet<String>,
|
||||||
state: &mut TypecheckPassState,
|
state: &mut TypecheckPassState,
|
||||||
) {
|
) {
|
||||||
match &typedef.kind {
|
match &typedef.kind {
|
||||||
@ -194,7 +194,7 @@ impl FunctionDefinitionKind {
|
|||||||
block.typecheck(&mut state.inner(), &typerefs, hint.into())
|
block.typecheck(&mut state.inner(), &typerefs, hint.into())
|
||||||
}
|
}
|
||||||
FunctionDefinitionKind::Extern(_) => Ok((ReturnKind::Soft, TypeKind::Vague(Vague::Unknown))),
|
FunctionDefinitionKind::Extern(_) => Ok((ReturnKind::Soft, TypeKind::Vague(Vague::Unknown))),
|
||||||
FunctionDefinitionKind::Intrinsic(intrinsic) => Ok((ReturnKind::Soft, TypeKind::Vague(Vague::Unknown))),
|
FunctionDefinitionKind::Intrinsic(_) => Ok((ReturnKind::Soft, TypeKind::Vague(Vague::Unknown))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -621,21 +621,20 @@ impl Expression {
|
|||||||
// Update possibly resolved type
|
// Update possibly resolved type
|
||||||
Ok(true_ty)
|
Ok(true_ty)
|
||||||
} else {
|
} else {
|
||||||
Err(ErrorKind::NoSuchField(field_name.clone()))
|
Err(ErrorKind::NoSuchField(key.0.clone()))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(ErrorKind::TriedAccessingNonStruct(expr_ty))
|
Err(ErrorKind::TriedAccessingNonStruct(expr_ty))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ExprKind::Struct(struct_name, items) => {
|
ExprKind::Struct(struct_key, items) => {
|
||||||
let type_key = CustomTypeKey(struct_name.clone(), state.module_id.unwrap());
|
|
||||||
let struct_def = state
|
let struct_def = state
|
||||||
.scope
|
.scope
|
||||||
.get_struct_type(&type_key)
|
.get_struct_type(&struct_key)
|
||||||
.ok_or(ErrorKind::NoSuchType(struct_name.clone(), type_key.1))?
|
.ok_or(ErrorKind::NoSuchType(struct_key.0.clone(), struct_key.1))?
|
||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
let mut expected_fields = if let Some(struct_ty) = state.scope.get_struct_type(&type_key) {
|
let mut expected_fields = if let Some(struct_ty) = state.scope.get_struct_type(&struct_key) {
|
||||||
struct_ty.0.iter().map(|f| f.0.clone()).collect()
|
struct_ty.0.iter().map(|f| f.0.clone()).collect()
|
||||||
} else {
|
} else {
|
||||||
HashSet::new()
|
HashSet::new()
|
||||||
@ -646,7 +645,7 @@ impl Expression {
|
|||||||
let expected_ty = state.or_else(
|
let expected_ty = state.or_else(
|
||||||
struct_def
|
struct_def
|
||||||
.get_field_ty(field_name)
|
.get_field_ty(field_name)
|
||||||
.ok_or(ErrorKind::NoSuchField(format!("{}.{}", struct_name, field_name))),
|
.ok_or(ErrorKind::NoSuchField(format!("{:?}.{}", struct_key, field_name))),
|
||||||
&TypeKind::Vague(VagueType::Unknown),
|
&TypeKind::Vague(VagueType::Unknown),
|
||||||
field_expr.1,
|
field_expr.1,
|
||||||
);
|
);
|
||||||
@ -668,7 +667,7 @@ impl Expression {
|
|||||||
self.1,
|
self.1,
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(TypeKind::CustomType(type_key))
|
Ok(TypeKind::CustomType(struct_key.clone()))
|
||||||
}
|
}
|
||||||
ExprKind::Borrow(expr, mutable) => {
|
ExprKind::Borrow(expr, mutable) => {
|
||||||
let hint_t = if let HintKind::Coerce(hint_t) = hint_t {
|
let hint_t = if let HintKind::Coerce(hint_t) = hint_t {
|
||||||
@ -721,15 +720,19 @@ impl Expression {
|
|||||||
expr.resolve_ref(typerefs).cast_into(type_kind)
|
expr.resolve_ref(typerefs).cast_into(type_kind)
|
||||||
}
|
}
|
||||||
ExprKind::AssociatedFunctionCall(type_kind, function_call) => {
|
ExprKind::AssociatedFunctionCall(type_kind, function_call) => {
|
||||||
|
*type_kind = type_kind.or_default()?;
|
||||||
let true_function = state
|
let true_function = state
|
||||||
.scope
|
.scope
|
||||||
.get_associated_function(&pass::AssociatedFunctionKey(
|
.get_associated_function(&pass::AssociatedFunctionKey(
|
||||||
type_kind.clone(),
|
type_kind.clone(),
|
||||||
function_call.name.clone(),
|
function_call.name.clone(),
|
||||||
))
|
))
|
||||||
.ok_or(ErrorKind::FunctionNotDefined(function_call.name.clone()));
|
.ok_or(ErrorKind::AssocFunctionNotDefined(
|
||||||
|
function_call.name.clone(),
|
||||||
|
type_kind.clone(),
|
||||||
|
));
|
||||||
|
|
||||||
if let Some(f) = state.ok(true_function, self.1) {
|
if let Some(f) = state.ok(true_function, function_call.meta) {
|
||||||
let param_len_given = function_call.parameters.len();
|
let param_len_given = function_call.parameters.len();
|
||||||
let param_len_expected = f.params.len();
|
let param_len_expected = f.params.len();
|
||||||
|
|
||||||
@ -768,7 +771,7 @@ impl Expression {
|
|||||||
Ok(function_call.return_type.clone().resolve_ref(typerefs))
|
Ok(function_call.return_type.clone().resolve_ref(typerefs))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ExprKind::GlobalRef(global_value, type_kind) => Ok(self
|
ExprKind::GlobalRef(..) => Ok(self
|
||||||
.return_type(typerefs, state.scope.module_id.unwrap())
|
.return_type(typerefs, state.scope.module_id.unwrap())
|
||||||
.map(|r| r.1)
|
.map(|r| r.1)
|
||||||
.unwrap()),
|
.unwrap()),
|
||||||
@ -803,14 +806,14 @@ impl Literal {
|
|||||||
(L::Vague(VagueL::Number(v)), TypeKind::U128) => L::U128(v as u128),
|
(L::Vague(VagueL::Number(v)), TypeKind::U128) => L::U128(v as u128),
|
||||||
(L::Vague(VagueL::Number(v)), TypeKind::F16) => L::F16(v as f32),
|
(L::Vague(VagueL::Number(v)), TypeKind::F16) => L::F16(v as f32),
|
||||||
(L::Vague(VagueL::Number(v)), TypeKind::F32) => L::F32(v as f32),
|
(L::Vague(VagueL::Number(v)), TypeKind::F32) => L::F32(v as f32),
|
||||||
(L::Vague(VagueL::Number(v)), TypeKind::F32B) => L::F32B(v as f32),
|
(L::Vague(VagueL::Number(v)), TypeKind::F16B) => L::F32B(v as f32),
|
||||||
(L::Vague(VagueL::Number(v)), TypeKind::F64) => L::F64(v as f64),
|
(L::Vague(VagueL::Number(v)), TypeKind::F64) => L::F64(v as f64),
|
||||||
(L::Vague(VagueL::Number(v)), TypeKind::F80) => L::F80(v as f64),
|
(L::Vague(VagueL::Number(v)), TypeKind::F80) => L::F80(v as f64),
|
||||||
(L::Vague(VagueL::Number(v)), TypeKind::F128) => L::F128(v as f64),
|
(L::Vague(VagueL::Number(v)), TypeKind::F128) => L::F128(v as f64),
|
||||||
(L::Vague(VagueL::Number(v)), TypeKind::F128PPC) => L::F128PPC(v as f64),
|
(L::Vague(VagueL::Number(v)), TypeKind::F128PPC) => L::F128PPC(v as f64),
|
||||||
(L::Vague(VagueL::Decimal(v)), TypeKind::F16) => L::F16(v as f32),
|
(L::Vague(VagueL::Decimal(v)), TypeKind::F16) => L::F16(v as f32),
|
||||||
(L::Vague(VagueL::Decimal(v)), TypeKind::F32) => L::F32(v as f32),
|
(L::Vague(VagueL::Decimal(v)), TypeKind::F32) => L::F32(v as f32),
|
||||||
(L::Vague(VagueL::Decimal(v)), TypeKind::F32B) => L::F32B(v as f32),
|
(L::Vague(VagueL::Decimal(v)), TypeKind::F16B) => L::F32B(v as f32),
|
||||||
(L::Vague(VagueL::Decimal(v)), TypeKind::F64) => L::F64(v as f64),
|
(L::Vague(VagueL::Decimal(v)), TypeKind::F64) => L::F64(v as f64),
|
||||||
(L::Vague(VagueL::Decimal(v)), TypeKind::F80) => L::F80(v as f64),
|
(L::Vague(VagueL::Decimal(v)), TypeKind::F80) => L::F80(v as f64),
|
||||||
(L::Vague(VagueL::Decimal(v)), TypeKind::F128) => L::F128(v as f64),
|
(L::Vague(VagueL::Decimal(v)), TypeKind::F128) => L::F128(v as f64),
|
||||||
|
|||||||
@ -12,9 +12,10 @@ use std::{
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
mir::{
|
mir::{
|
||||||
|
implement::TypeCategory,
|
||||||
pass::{AssociatedFunctionKey, ScopeVariable},
|
pass::{AssociatedFunctionKey, ScopeVariable},
|
||||||
BinopDefinition, Block, CustomTypeKey, ExprKind, Expression, FunctionDefinition, FunctionDefinitionKind,
|
BinopDefinition, Block, ExprKind, Expression, FunctionDefinition, FunctionDefinitionKind, IfExpression, Module,
|
||||||
IfExpression, Module, ReturnKind, StmtKind, TypeKind, WhileStatement,
|
ReturnKind, StmtKind, TypeKind, VagueType, WhileStatement,
|
||||||
},
|
},
|
||||||
util::try_all,
|
util::try_all,
|
||||||
};
|
};
|
||||||
@ -382,6 +383,7 @@ impl Expression {
|
|||||||
} else {
|
} else {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
if binops.len() > 0 {
|
if binops.len() > 0 {
|
||||||
let binop = unsafe { binops.get_unchecked(0) };
|
let binop = unsafe { binops.get_unchecked(0) };
|
||||||
let mut widened_lhs = binop.hands.0.clone();
|
let mut widened_lhs = binop.hands.0.clone();
|
||||||
@ -391,9 +393,6 @@ impl Expression {
|
|||||||
widened_rhs = widened_rhs.widen_into(&binop.hands.1);
|
widened_rhs = widened_rhs.widen_into(&binop.hands.1);
|
||||||
}
|
}
|
||||||
let binop_res = type_refs.from_binop(*op, &lhs_ref, &rhs_ref);
|
let binop_res = type_refs.from_binop(*op, &lhs_ref, &rhs_ref);
|
||||||
// dbg!(&return_ty);
|
|
||||||
// dbg!(&binop_res);
|
|
||||||
// dbg!(&lhs_ref, &rhs_ref, &binops, &widened_lhs, &widened_rhs);
|
|
||||||
lhs_ref.narrow(&type_refs.from_type(&widened_lhs).unwrap());
|
lhs_ref.narrow(&type_refs.from_type(&widened_lhs).unwrap());
|
||||||
rhs_ref.narrow(&type_refs.from_type(&widened_rhs).unwrap());
|
rhs_ref.narrow(&type_refs.from_type(&widened_rhs).unwrap());
|
||||||
*return_ty = binop_res.as_type();
|
*return_ty = binop_res.as_type();
|
||||||
@ -546,18 +545,17 @@ impl Expression {
|
|||||||
*type_kind = elem_ty.as_type().clone();
|
*type_kind = elem_ty.as_type().clone();
|
||||||
Ok(elem_ty)
|
Ok(elem_ty)
|
||||||
}
|
}
|
||||||
None => Err(ErrorKind::NoSuchField(field_name.clone())),
|
None => Ok(type_refs.from_type(&TypeKind::Vague(VagueType::Unknown)).unwrap()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => Err(ErrorKind::TriedAccessingNonStruct(kind)),
|
_ => Ok(type_refs.from_type(&TypeKind::Vague(VagueType::Unknown)).unwrap()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ExprKind::Struct(struct_name, fields) => {
|
ExprKind::Struct(struct_key, fields) => {
|
||||||
let type_key = CustomTypeKey(struct_name.clone(), state.module_id.unwrap());
|
|
||||||
let expected_struct_ty = state
|
let expected_struct_ty = state
|
||||||
.scope
|
.scope
|
||||||
.get_struct_type(&type_key)
|
.get_struct_type(&struct_key)
|
||||||
.ok_or(ErrorKind::NoSuchType(struct_name.clone(), state.module_id.unwrap()))?
|
.ok_or(ErrorKind::NoSuchType(struct_key.0.clone(), state.module_id.unwrap()))?
|
||||||
.clone();
|
.clone();
|
||||||
for field in fields {
|
for field in fields {
|
||||||
if let Some(expected_field_ty) = expected_struct_ty.get_field_ty(&field.0) {
|
if let Some(expected_field_ty) = expected_struct_ty.get_field_ty(&field.0) {
|
||||||
@ -567,12 +565,12 @@ impl Expression {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
state.ok::<_, Infallible>(
|
state.ok::<_, Infallible>(
|
||||||
Err(ErrorKind::NoSuchField(format!("{}.{}", struct_name, field.0))),
|
Err(ErrorKind::NoSuchField(format!("{:?}.{}", struct_key, field.0))),
|
||||||
field.1 .1,
|
field.1 .1,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(type_refs.from_type(&TypeKind::CustomType(type_key.clone())).unwrap())
|
Ok(type_refs.from_type(&TypeKind::CustomType(struct_key.clone())).unwrap())
|
||||||
}
|
}
|
||||||
ExprKind::Borrow(expr, mutable) => {
|
ExprKind::Borrow(expr, mutable) => {
|
||||||
// Find variable type
|
// Find variable type
|
||||||
@ -627,19 +625,27 @@ impl Expression {
|
|||||||
|
|
||||||
if backing_var.is_some() {
|
if backing_var.is_some() {
|
||||||
if let TypeKind::Borrow(inner, _) = type_kind {
|
if let TypeKind::Borrow(inner, _) = type_kind {
|
||||||
|
let ty_cat = inner.category();
|
||||||
if let TypeKind::Borrow(..) = *inner.clone() {
|
if let TypeKind::Borrow(..) = *inner.clone() {
|
||||||
*type_kind = type_kind.unroll_borrow();
|
*type_kind = type_kind.unroll_borrow();
|
||||||
let ExprKind::Borrow(val, _) = &first_param.0 else {
|
let ExprKind::Borrow(val, _) = &first_param.0 else {
|
||||||
panic!()
|
panic!()
|
||||||
};
|
};
|
||||||
*first_param = *val.clone();
|
*first_param = *val.clone();
|
||||||
|
} else if ty_cat == TypeCategory::Integer || ty_cat == TypeCategory::Real {
|
||||||
|
if let ExprKind::Borrow(val, _) = &first_param.0 {
|
||||||
|
*first_param = *val.clone();
|
||||||
|
}
|
||||||
|
*type_kind = *inner.clone();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let ExprKind::Borrow(val, _) = &first_param.0 else {
|
if let ExprKind::Borrow(val, _) = &first_param.0 {
|
||||||
panic!()
|
*first_param = *val.clone();
|
||||||
};
|
}
|
||||||
*first_param = *val.clone();
|
if let TypeKind::Borrow(inner_ty, _) = type_kind {
|
||||||
|
*type_kind = *inner_ty.clone();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !is_mutable {
|
if !is_mutable {
|
||||||
@ -655,9 +661,13 @@ impl Expression {
|
|||||||
.ok_or(ErrorKind::AssocFunctionNotDefined(
|
.ok_or(ErrorKind::AssocFunctionNotDefined(
|
||||||
function_call.name.clone(),
|
function_call.name.clone(),
|
||||||
type_kind.clone(),
|
type_kind.clone(),
|
||||||
))?
|
))
|
||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
|
let Ok(fn_call) = fn_call else {
|
||||||
|
return Ok(type_refs.from_type(&Vague(Unknown)).unwrap());
|
||||||
|
};
|
||||||
|
|
||||||
// Infer param expression types and narrow them to the
|
// Infer param expression types and narrow them to the
|
||||||
// expected function parameters (or Unknown types if too
|
// expected function parameters (or Unknown types if too
|
||||||
// many were provided)
|
// many were provided)
|
||||||
@ -673,7 +683,7 @@ impl Expression {
|
|||||||
// Provide function return type
|
// Provide function return type
|
||||||
Ok(type_refs.from_type(&fn_call.ret).unwrap())
|
Ok(type_refs.from_type(&fn_call.ret).unwrap())
|
||||||
}
|
}
|
||||||
ExprKind::GlobalRef(global_value, type_kind) => Ok(self
|
ExprKind::GlobalRef(..) => Ok(self
|
||||||
.return_type(type_refs.types, state.scope.module_id.unwrap())
|
.return_type(type_refs.types, state.scope.module_id.unwrap())
|
||||||
.map(|r| type_refs.from_type(&r.1).unwrap())
|
.map(|r| type_refs.from_type(&r.1).unwrap())
|
||||||
.unwrap()),
|
.unwrap()),
|
||||||
|
|||||||
@ -97,6 +97,9 @@ pub struct TypeRefs {
|
|||||||
/// Indirect ID-references, referring to hints-vec
|
/// Indirect ID-references, referring to hints-vec
|
||||||
pub(super) type_refs: RefCell<Vec<TypeIdRef>>,
|
pub(super) type_refs: RefCell<Vec<TypeIdRef>>,
|
||||||
pub(super) binop_types: BinopMap,
|
pub(super) binop_types: BinopMap,
|
||||||
|
/// Used when the real typerefs are not available, and any TypeRefs need to
|
||||||
|
/// be resolved as Unknown.
|
||||||
|
pub unknown_typerefs: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for TypeRefs {
|
impl std::fmt::Display for TypeRefs {
|
||||||
@ -122,6 +125,14 @@ impl TypeRefs {
|
|||||||
hints: Default::default(),
|
hints: Default::default(),
|
||||||
type_refs: Default::default(),
|
type_refs: Default::default(),
|
||||||
binop_types: binops,
|
binop_types: binops,
|
||||||
|
unknown_typerefs: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unknown() -> TypeRefs {
|
||||||
|
TypeRefs {
|
||||||
|
unknown_typerefs: true,
|
||||||
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,8 +188,12 @@ impl TypeRefs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn retrieve_typeref(&self, idx: usize) -> Option<TypeRefKind> {
|
pub fn retrieve_typeref(&self, idx: usize) -> Option<TypeRefKind> {
|
||||||
let inner_idx = unsafe { *self.recurse_type_ref(idx).borrow() };
|
if !self.unknown_typerefs {
|
||||||
self.hints.borrow().get(inner_idx).cloned()
|
let inner_idx = unsafe { *self.recurse_type_ref(idx).borrow() };
|
||||||
|
self.hints.borrow().get(inner_idx).cloned()
|
||||||
|
} else {
|
||||||
|
Some(TypeRefKind::Direct(TypeKind::Vague(VagueType::Unknown)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn retrieve_wide_type(&self, idx: usize, seen: &mut HashSet<usize>) -> Option<TypeKind> {
|
pub fn retrieve_wide_type(&self, idx: usize, seen: &mut HashSet<usize>) -> Option<TypeKind> {
|
||||||
@ -293,12 +308,23 @@ impl<'outer> ScopeTypeRefs<'outer> {
|
|||||||
let lhs_resolved = lhs.resolve_ref(self.types);
|
let lhs_resolved = lhs.resolve_ref(self.types);
|
||||||
let rhs_resolved = rhs.resolve_ref(self.types);
|
let rhs_resolved = rhs.resolve_ref(self.types);
|
||||||
|
|
||||||
let binops = self
|
let mut binops = self
|
||||||
.types
|
.types
|
||||||
.binop_types
|
.binop_types
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|b| b.1.operator == op && b.1.return_ty == *ty)
|
.filter(|b| b.1.operator == op && b.1.return_ty == *ty)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
// Sort binops by lhs and then rhs
|
||||||
|
binops.sort_by(|a, b| {
|
||||||
|
let lhs = a.1.hands.0.cmp(&b.1.hands.0);
|
||||||
|
let rhs = a.1.hands.1.cmp(&b.1.hands.1);
|
||||||
|
match lhs {
|
||||||
|
std::cmp::Ordering::Equal => rhs,
|
||||||
|
_ => lhs,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
for binop in binops {
|
for binop in binops {
|
||||||
if let (Ok(lhs_narrow), Ok(rhs_narrow)) = (
|
if let (Ok(lhs_narrow), Ok(rhs_narrow)) = (
|
||||||
lhs_resolved.narrow_into(&binop.1.hands.0),
|
lhs_resolved.narrow_into(&binop.1.hands.0),
|
||||||
@ -472,6 +498,17 @@ impl<'outer> ScopeTypeRefs<'outer> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sort binops by lhs and then rhs
|
||||||
|
applying_binops.sort_by(|a, b| {
|
||||||
|
let lhs = a.hands.0.cmp(&b.hands.0);
|
||||||
|
let rhs = a.hands.1.cmp(&b.hands.1);
|
||||||
|
match lhs {
|
||||||
|
std::cmp::Ordering::Equal => rhs,
|
||||||
|
_ => lhs,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
applying_binops
|
applying_binops
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,9 +14,9 @@ mod util;
|
|||||||
fn test_compile(source: &str, name: &str) -> CompileOutput {
|
fn test_compile(source: &str, name: &str) -> CompileOutput {
|
||||||
assert_err(assert_err(std::panic::catch_unwind(|| {
|
assert_err(assert_err(std::panic::catch_unwind(|| {
|
||||||
let mut map = Default::default();
|
let mut map = Default::default();
|
||||||
let (id, tokens) = assert_err(parse_module(source, name, &mut map));
|
let (id, tokens) = assert_err(parse_module(source, name, None, &mut map, None));
|
||||||
|
|
||||||
let module = assert_err(compile_module(id, tokens, &mut map, None, true));
|
let module = assert_err(assert_err(compile_module(id, tokens, &mut map, None, true)).map_err(|(_, e)| e));
|
||||||
let mut mir_context = mir::Context::from(vec![module], Default::default());
|
let mut mir_context = mir::Context::from(vec![module], Default::default());
|
||||||
assert_err(perform_all_passes(&mut mir_context, &mut map));
|
assert_err(perform_all_passes(&mut mir_context, &mut map));
|
||||||
|
|
||||||
@ -169,3 +169,8 @@ fn mutable_inner_functions() {
|
|||||||
fn cpu_raytracer_compiles() {
|
fn cpu_raytracer_compiles() {
|
||||||
test_compile(include_str!("../../examples/cpu_raytracer.reid"), "test");
|
test_compile(include_str!("../../examples/cpu_raytracer.reid"), "test");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn loop_edge_case_functions() {
|
||||||
|
test(include_str!("../../examples/loop_edge_case.reid"), "test", Some(0));
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user