forked from M-Labs/nac3
Compare commits
42 Commits
refactor-p
...
ndarray-st
Author | SHA1 | Date |
---|---|---|
lyken | 87d2a4ed59 | |
lyken | 9aae290727 | |
lyken | d18c769cdc | |
lyken | f41f06aec7 | |
lyken | 1303265785 | |
lyken | e9cf6ce1e5 | |
lyken | bc91ab9b13 | |
lyken | 1e06a3d199 | |
lyken | 87511ac749 | |
Sebastien Bourdeauducq | d658d9b00e | |
abdul124 | eeb474f9e6 | |
abdul124 | 88b72af2d1 | |
abdul124 | b73f6c4d68 | |
David Mak | f47cdec650 | |
David Mak | d656880e44 | |
David Mak | a91602915a | |
David Mak | 1c56005a01 | |
David Mak | bc40a32524 | |
David Mak | c820daf5f8 | |
David Mak | 25d2de67f7 | |
David Mak | 2cfb7a7e10 | |
David Mak | 9238a5e86e | |
lyken | 76defac462 | |
lyken | 650f354b74 | |
abdul124 | f062ef5f59 | |
lyken | f52086b706 | |
lyken | 0a732691c9 | |
lyken | cbff356d50 | |
lyken | 24ac3820b2 | |
David Mak | ba32fab374 | |
David Mak | c4052b6342 | |
David Mak | 66c205275f | |
David Mak | c85e412206 | |
David Mak | 075536d7bd | |
David Mak | 13beeaa2bf | |
David Mak | 2194dbddd5 | |
David Mak | 94a1d547d6 | |
lyken | d6565feed3 | |
abdul124 | 83154ef8e1 | |
lyken | 0744b938b8 | |
lyken | 56fa2b6803 | |
lyken | d06c13f936 |
|
@ -117,9 +117,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cc"
|
name = "cc"
|
||||||
version = "1.0.100"
|
version = "1.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c891175c3fb232128f48de6590095e59198bbeb8620c310be349bfc3afd12c7b"
|
checksum = "eaff6f8ce506b9773fa786672d63fc7a191ffea1be33f72bbd4aeacefca9ffc8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
|
@ -129,9 +129,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap"
|
name = "clap"
|
||||||
version = "4.5.7"
|
version = "4.5.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5db83dced34638ad474f39f250d7fea9598bdd239eaced1bdf45d597da0f433f"
|
checksum = "64acc1846d54c1fe936a78dc189c34e28d3f5afc348403f28ecf53660b9b8462"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap_builder",
|
"clap_builder",
|
||||||
"clap_derive",
|
"clap_derive",
|
||||||
|
@ -139,9 +139,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap_builder"
|
name = "clap_builder"
|
||||||
version = "4.5.7"
|
version = "4.5.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f7e204572485eb3fbf28f871612191521df159bc3e15a9f5064c66dba3a8c05f"
|
checksum = "6fb8393d67ba2e7bfaf28a23458e4e2b543cc73a99595511eb207fdb8aede942"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anstream",
|
"anstream",
|
||||||
"anstyle",
|
"anstyle",
|
||||||
|
@ -151,14 +151,14 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap_derive"
|
name = "clap_derive"
|
||||||
version = "4.5.5"
|
version = "4.5.8"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c780290ccf4fb26629baa7a1081e68ced113f1d3ec302fa5948f1c381ebf06c6"
|
checksum = "2bac35c6dafb060fd4d275d9a4ffae97917c13a6327903a8be2153cd964f7085"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"heck 0.5.0",
|
"heck 0.5.0",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.68",
|
"syn 2.0.70",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -270,9 +270,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "either"
|
name = "either"
|
||||||
version = "1.12.0"
|
version = "1.13.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b"
|
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ena"
|
name = "ena"
|
||||||
|
@ -421,7 +421,7 @@ checksum = "4fa4d8d74483041a882adaa9a29f633253a66dde85055f0495c121620ac484b2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.68",
|
"syn 2.0.70",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -568,9 +568,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "log"
|
name = "log"
|
||||||
version = "0.4.21"
|
version = "0.4.22"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
|
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
|
@ -749,7 +749,7 @@ dependencies = [
|
||||||
"phf_shared 0.11.2",
|
"phf_shared 0.11.2",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.68",
|
"syn 2.0.70",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -850,7 +850,7 @@ dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"pyo3-macros-backend",
|
"pyo3-macros-backend",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.68",
|
"syn 2.0.70",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -863,7 +863,7 @@ dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"pyo3-build-config",
|
"pyo3-build-config",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.68",
|
"syn 2.0.70",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1029,29 +1029,29 @@ checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.203"
|
version = "1.0.204"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094"
|
checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.203"
|
version = "1.0.204"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba"
|
checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.68",
|
"syn 2.0.70",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_json"
|
name = "serde_json"
|
||||||
version = "1.0.118"
|
version = "1.0.120"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d947f6b3163d8857ea16c4fa0dd4840d52f3041039a85decd46867eb1abef2e4"
|
checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itoa",
|
"itoa",
|
||||||
"ryu",
|
"ryu",
|
||||||
|
@ -1134,7 +1134,7 @@ dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"rustversion",
|
"rustversion",
|
||||||
"syn 2.0.68",
|
"syn 2.0.70",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1150,9 +1150,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.68"
|
version = "2.0.70"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "901fa70d88b9d6c98022e23b4136f9f3e54e4662c3bc1bd1d84a42a9a0f0c1e9"
|
checksum = "2f0209b68b3613b093e0ec905354eccaedcfe83b8cb37cbdeae64026c3064c16"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
@ -1161,9 +1161,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "target-lexicon"
|
name = "target-lexicon"
|
||||||
version = "0.12.14"
|
version = "0.12.15"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e1fc403891a21bcfb7c37834ba66a547a8f402146eba7265b5a6d88059c9ff2f"
|
checksum = "4873307b7c257eddcb50c9bedf158eb669578359fb28428bef438fec8e6ba7c2"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tempfile"
|
name = "tempfile"
|
||||||
|
@ -1218,7 +1218,7 @@ checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.68",
|
"syn 2.0.70",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1398,9 +1398,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-targets"
|
name = "windows-targets"
|
||||||
version = "0.52.5"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb"
|
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows_aarch64_gnullvm",
|
"windows_aarch64_gnullvm",
|
||||||
"windows_aarch64_msvc",
|
"windows_aarch64_msvc",
|
||||||
|
@ -1414,51 +1414,51 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_aarch64_gnullvm"
|
name = "windows_aarch64_gnullvm"
|
||||||
version = "0.52.5"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263"
|
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_aarch64_msvc"
|
name = "windows_aarch64_msvc"
|
||||||
version = "0.52.5"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6"
|
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_gnu"
|
name = "windows_i686_gnu"
|
||||||
version = "0.52.5"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670"
|
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_gnullvm"
|
name = "windows_i686_gnullvm"
|
||||||
version = "0.52.5"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9"
|
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_msvc"
|
name = "windows_i686_msvc"
|
||||||
version = "0.52.5"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf"
|
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnu"
|
name = "windows_x86_64_gnu"
|
||||||
version = "0.52.5"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9"
|
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnullvm"
|
name = "windows_x86_64_gnullvm"
|
||||||
version = "0.52.5"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596"
|
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_msvc"
|
name = "windows_x86_64_msvc"
|
||||||
version = "0.52.5"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"
|
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "yaml-rust"
|
name = "yaml-rust"
|
||||||
|
@ -1471,20 +1471,20 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zerocopy"
|
name = "zerocopy"
|
||||||
version = "0.7.34"
|
version = "0.7.35"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ae87e3fcd617500e5d106f0380cf7b77f3c6092aae37191433159dda23cfb087"
|
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"zerocopy-derive",
|
"zerocopy-derive",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zerocopy-derive"
|
name = "zerocopy-derive"
|
||||||
version = "0.7.34"
|
version = "0.7.35"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "15e934569e47891f7d9411f1a451d947a60e000ab3bd24fbb970f000387d1b3b"
|
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.68",
|
"syn 2.0.70",
|
||||||
]
|
]
|
||||||
|
|
|
@ -2,11 +2,11 @@
|
||||||
"nodes": {
|
"nodes": {
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1718530797,
|
"lastModified": 1720418205,
|
||||||
"narHash": "sha256-pup6cYwtgvzDpvpSCFh1TEUjw2zkNpk8iolbKnyFmmU=",
|
"narHash": "sha256-cPJoFPXU44GlhWg4pUk9oUPqurPlCFZ11ZQPk21GTPU=",
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "b60ebf54c15553b393d144357375ea956f89e9a9",
|
"rev": "655a58a72a6601292512670343087c2d75d859c1",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
''
|
''
|
||||||
mkdir -p $out/bin
|
mkdir -p $out/bin
|
||||||
ln -s ${pkgs.llvmPackages_14.clang-unwrapped}/bin/clang $out/bin/clang-irrt
|
ln -s ${pkgs.llvmPackages_14.clang-unwrapped}/bin/clang $out/bin/clang-irrt
|
||||||
|
ln -s ${pkgs.llvmPackages_14.clang}/bin/clang $out/bin/clang-irrt-test
|
||||||
ln -s ${pkgs.llvmPackages_14.llvm.out}/bin/llvm-as $out/bin/llvm-as-irrt
|
ln -s ${pkgs.llvmPackages_14.llvm.out}/bin/llvm-as $out/bin/llvm-as-irrt
|
||||||
'';
|
'';
|
||||||
nac3artiq = pkgs.python3Packages.toPythonModule (
|
nac3artiq = pkgs.python3Packages.toPythonModule (
|
||||||
|
@ -23,6 +24,7 @@
|
||||||
cargoLock = {
|
cargoLock = {
|
||||||
lockFile = ./Cargo.lock;
|
lockFile = ./Cargo.lock;
|
||||||
};
|
};
|
||||||
|
cargoTestFlags = [ "--features" "test" ];
|
||||||
passthru.cargoLock = cargoLock;
|
passthru.cargoLock = cargoLock;
|
||||||
nativeBuildInputs = [ pkgs.python3 pkgs.llvmPackages_14.clang llvm-tools-irrt pkgs.llvmPackages_14.llvm.out llvm-nac3 ];
|
nativeBuildInputs = [ pkgs.python3 pkgs.llvmPackages_14.clang llvm-tools-irrt pkgs.llvmPackages_14.llvm.out llvm-nac3 ];
|
||||||
buildInputs = [ pkgs.python3 llvm-nac3 ];
|
buildInputs = [ pkgs.python3 llvm-nac3 ];
|
||||||
|
@ -161,7 +163,10 @@
|
||||||
clippy
|
clippy
|
||||||
pre-commit
|
pre-commit
|
||||||
rustfmt
|
rustfmt
|
||||||
|
rust-analyzer
|
||||||
];
|
];
|
||||||
|
# https://nixos.wiki/wiki/Rust#Shell.nix_example
|
||||||
|
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
|
||||||
};
|
};
|
||||||
devShells.x86_64-linux.msys2 = pkgs.mkShell {
|
devShells.x86_64-linux.msys2 = pkgs.mkShell {
|
||||||
name = "nac3-dev-shell-msys2";
|
name = "nac3-dev-shell-msys2";
|
||||||
|
|
|
@ -6,8 +6,8 @@ use nac3core::{
|
||||||
CodeGenContext, CodeGenerator,
|
CodeGenContext, CodeGenerator,
|
||||||
},
|
},
|
||||||
symbol_resolver::ValueEnum,
|
symbol_resolver::ValueEnum,
|
||||||
toplevel::{helper::PrimDef, DefinitionId, GenCall},
|
toplevel::{helper::PrimDef, numpy::unpack_ndarray_var_tys, DefinitionId, GenCall},
|
||||||
typecheck::typedef::{FunSignature, FuncArg, GenericObjectType, Type, TypeEnum, VarMap},
|
typecheck::typedef::{iter_type_vars, FunSignature, FuncArg, Type, TypeEnum, VarMap},
|
||||||
};
|
};
|
||||||
|
|
||||||
use nac3parser::ast::{Expr, ExprKind, Located, Stmt, StmtKind, StrRef};
|
use nac3parser::ast::{Expr, ExprKind, Located, Stmt, StmtKind, StrRef};
|
||||||
|
@ -23,7 +23,6 @@ use pyo3::{
|
||||||
|
|
||||||
use crate::{symbol_resolver::InnerResolver, timeline::TimeFns};
|
use crate::{symbol_resolver::InnerResolver, timeline::TimeFns};
|
||||||
|
|
||||||
use nac3core::toplevel::primitive_type;
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::hash_map::DefaultHasher,
|
collections::hash_map::DefaultHasher,
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
|
@ -394,14 +393,14 @@ fn gen_rpc_tag(
|
||||||
gen_rpc_tag(ctx, *ty, buffer)?;
|
gen_rpc_tag(ctx, *ty, buffer)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TList { ty } => {
|
TObj { obj_id, params, .. } if *obj_id == PrimDef::List.id() => {
|
||||||
|
let ty = iter_type_vars(params).next().unwrap().ty;
|
||||||
|
|
||||||
buffer.push(b'l');
|
buffer.push(b'l');
|
||||||
gen_rpc_tag(ctx, *ty, buffer)?;
|
gen_rpc_tag(ctx, ty, buffer)?;
|
||||||
}
|
}
|
||||||
TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
||||||
let ndarray_ty = primitive_type::NDArrayType::create(ty, &mut ctx.unifier);
|
let (ndarray_dtype, ndarray_ndims) = unpack_ndarray_var_tys(&mut ctx.unifier, ty);
|
||||||
let ndarray_dtype = ndarray_ty.dtype_tvar(&mut ctx.unifier).ty;
|
|
||||||
let ndarray_ndims = ndarray_ty.ndims_tvar(&mut ctx.unifier).ty;
|
|
||||||
let ndarray_ndims = if let TLiteral { values, .. } =
|
let ndarray_ndims = if let TLiteral { values, .. } =
|
||||||
&*ctx.unifier.get_ty_immutable(ndarray_ndims)
|
&*ctx.unifier.get_ty_immutable(ndarray_ndims)
|
||||||
{
|
{
|
||||||
|
@ -647,7 +646,7 @@ pub fn attributes_writeback(
|
||||||
let ty = ty.unwrap();
|
let ty = ty.unwrap();
|
||||||
match &*ctx.unifier.get_ty(ty) {
|
match &*ctx.unifier.get_ty(ty) {
|
||||||
TypeEnum::TObj { fields, obj_id, .. }
|
TypeEnum::TObj { fields, obj_id, .. }
|
||||||
if *obj_id != ctx.primitives.option.obj_id(&ctx.unifier) =>
|
if *obj_id != ctx.primitives.option.obj_id(&ctx.unifier).unwrap() =>
|
||||||
{
|
{
|
||||||
// we only care about primitive attributes
|
// we only care about primitive attributes
|
||||||
// for non-primitive attributes, they should be in another global
|
// for non-primitive attributes, they should be in another global
|
||||||
|
@ -677,8 +676,10 @@ pub fn attributes_writeback(
|
||||||
host_attributes.append(pydict)?;
|
host_attributes.append(pydict)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TypeEnum::TList { ty: elem_ty } => {
|
TypeEnum::TObj { obj_id, params, .. } if *obj_id == PrimDef::List.id() => {
|
||||||
if gen_rpc_tag(ctx, *elem_ty, &mut scratch_buffer).is_ok() {
|
let elem_ty = iter_type_vars(params).next().unwrap().ty;
|
||||||
|
|
||||||
|
if gen_rpc_tag(ctx, elem_ty, &mut scratch_buffer).is_ok() {
|
||||||
let pydict = PyDict::new(py);
|
let pydict = PyDict::new(py);
|
||||||
pydict.set_item("obj", val)?;
|
pydict.set_item("obj", val)?;
|
||||||
host_attributes.append(pydict)?;
|
host_attributes.append(pydict)?;
|
||||||
|
|
|
@ -4,17 +4,20 @@ use inkwell::{
|
||||||
AddressSpace,
|
AddressSpace,
|
||||||
};
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use nac3core::typecheck::typedef::{GenericObjectType, GenericTypeAdapter};
|
|
||||||
use nac3core::{
|
use nac3core::{
|
||||||
codegen::{
|
codegen::{
|
||||||
classes::{NDArrayType, ProxyType},
|
classes::{NDArrayType, ProxyType},
|
||||||
CodeGenContext, CodeGenerator,
|
CodeGenContext, CodeGenerator,
|
||||||
},
|
},
|
||||||
symbol_resolver::{StaticValue, SymbolResolver, SymbolValue, ValueEnum},
|
symbol_resolver::{StaticValue, SymbolResolver, SymbolValue, ValueEnum},
|
||||||
toplevel::{helper::PrimDef, primitive_type, DefinitionId, TopLevelDef},
|
toplevel::{
|
||||||
|
helper::PrimDef,
|
||||||
|
numpy::{make_ndarray_ty, unpack_ndarray_var_tys},
|
||||||
|
DefinitionId, TopLevelDef,
|
||||||
|
},
|
||||||
typecheck::{
|
typecheck::{
|
||||||
type_inferencer::PrimitiveStore,
|
type_inferencer::PrimitiveStore,
|
||||||
typedef::{Type, TypeEnum, TypeVar, Unifier, VarMap},
|
typedef::{into_var_map, iter_type_vars, Type, TypeEnum, TypeVar, Unifier, VarMap},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use nac3parser::ast::{self, StrRef};
|
use nac3parser::ast::{self, StrRef};
|
||||||
|
@ -326,25 +329,31 @@ impl InnerResolver {
|
||||||
Ok(Ok((primitives.exception, true)))
|
Ok(Ok((primitives.exception, true)))
|
||||||
} else if ty_id == self.primitive_ids.list {
|
} else if ty_id == self.primitive_ids.list {
|
||||||
// do not handle type var param and concrete check here
|
// do not handle type var param and concrete check here
|
||||||
|
let list_tvar = if let TypeEnum::TObj { obj_id, params, .. } =
|
||||||
|
&*unifier.get_ty_immutable(primitives.list)
|
||||||
|
{
|
||||||
|
assert_eq!(*obj_id, PrimDef::List.id());
|
||||||
|
iter_type_vars(params).nth(0).unwrap()
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
let var = unifier.get_dummy_var().ty;
|
let var = unifier.get_dummy_var().ty;
|
||||||
let list = unifier.add_ty(TypeEnum::TList { ty: var });
|
let list = unifier
|
||||||
|
.subst(primitives.list, &into_var_map([TypeVar { id: list_tvar.id, ty: var }]))
|
||||||
|
.unwrap();
|
||||||
Ok(Ok((list, false)))
|
Ok(Ok((list, false)))
|
||||||
} else if ty_id == self.primitive_ids.ndarray {
|
} else if ty_id == self.primitive_ids.ndarray {
|
||||||
// do not handle type var param and concrete check here
|
// do not handle type var param and concrete check here
|
||||||
let var = unifier.get_dummy_var().ty;
|
let var = unifier.get_dummy_var().ty;
|
||||||
let ndims = unifier.get_fresh_const_generic_var(primitives.usize(), None, None).ty;
|
let ndims = unifier.get_fresh_const_generic_var(primitives.usize(), None, None).ty;
|
||||||
let ndarray = primitive_type::NDArrayType::from_primitive(
|
let ndarray = make_ndarray_ty(unifier, primitives, Some(var), Some(ndims));
|
||||||
unifier,
|
Ok(Ok((ndarray, false)))
|
||||||
primitives,
|
|
||||||
Some(var),
|
|
||||||
Some(ndims),
|
|
||||||
);
|
|
||||||
Ok(Ok((ndarray.into(), false)))
|
|
||||||
} else if ty_id == self.primitive_ids.tuple {
|
} else if ty_id == self.primitive_ids.tuple {
|
||||||
// do not handle type var param and concrete check here
|
// do not handle type var param and concrete check here
|
||||||
Ok(Ok((unifier.add_ty(TypeEnum::TTuple { ty: vec![] }), false)))
|
Ok(Ok((unifier.add_ty(TypeEnum::TTuple { ty: vec![] }), false)))
|
||||||
} else if ty_id == self.primitive_ids.option {
|
} else if ty_id == self.primitive_ids.option {
|
||||||
Ok(Ok((primitives.option.into(), false)))
|
Ok(Ok((primitives.option, false)))
|
||||||
} else if ty_id == self.primitive_ids.none {
|
} else if ty_id == self.primitive_ids.none {
|
||||||
unreachable!("none cannot be typeid")
|
unreachable!("none cannot be typeid")
|
||||||
} else if let Some(def_id) = self.pyid_to_def.read().get(&ty_id).copied() {
|
} else if let Some(def_id) = self.pyid_to_def.read().get(&ty_id).copied() {
|
||||||
|
@ -462,7 +471,7 @@ impl InnerResolver {
|
||||||
};
|
};
|
||||||
|
|
||||||
match &*unifier.get_ty(origin_ty) {
|
match &*unifier.get_ty(origin_ty) {
|
||||||
TypeEnum::TList { .. } => {
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::List.id() => {
|
||||||
if args.len() == 1 {
|
if args.len() == 1 {
|
||||||
let ty = match self.get_pyty_obj_type(
|
let ty = match self.get_pyty_obj_type(
|
||||||
py,
|
py,
|
||||||
|
@ -479,7 +488,21 @@ impl InnerResolver {
|
||||||
"type list should take concrete parameters in typevar range".into(),
|
"type list should take concrete parameters in typevar range".into(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Ok(Ok((unifier.add_ty(TypeEnum::TList { ty: ty.0 }), true)))
|
let list_tvar = if let TypeEnum::TObj { obj_id, params, .. } =
|
||||||
|
&*unifier.get_ty_immutable(primitives.list)
|
||||||
|
{
|
||||||
|
assert_eq!(*obj_id, PrimDef::List.id());
|
||||||
|
iter_type_vars(params).nth(0).unwrap()
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
let list = unifier
|
||||||
|
.subst(
|
||||||
|
primitives.list,
|
||||||
|
&into_var_map([TypeVar { id: list_tvar.id, ty: ty.0 }]),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
Ok(Ok((list, true)))
|
||||||
} else {
|
} else {
|
||||||
return Ok(Err(format!(
|
return Ok(Err(format!(
|
||||||
"type list needs exactly 1 type parameters, found {}",
|
"type list needs exactly 1 type parameters, found {}",
|
||||||
|
@ -511,16 +534,7 @@ impl InnerResolver {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Ok((
|
Ok(Ok((make_ndarray_ty(unifier, primitives, Some(ty.0), None), true)))
|
||||||
primitive_type::NDArrayType::from_primitive(
|
|
||||||
unifier,
|
|
||||||
primitives,
|
|
||||||
Some(ty.0),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.into(),
|
|
||||||
true,
|
|
||||||
)))
|
|
||||||
}
|
}
|
||||||
TypeEnum::TTuple { .. } => {
|
TypeEnum::TTuple { .. } => {
|
||||||
let args = match args
|
let args = match args
|
||||||
|
@ -704,11 +718,12 @@ impl InnerResolver {
|
||||||
};
|
};
|
||||||
match (&*unifier.get_ty(extracted_ty), inst_check) {
|
match (&*unifier.get_ty(extracted_ty), inst_check) {
|
||||||
// do the instantiation for these four types
|
// do the instantiation for these four types
|
||||||
(TypeEnum::TList { ty }, false) => {
|
(TypeEnum::TObj { obj_id, params, .. }, false) if *obj_id == PrimDef::List.id() => {
|
||||||
|
let ty = iter_type_vars(params).nth(0).unwrap().ty;
|
||||||
let len: usize = self.helper.len_fn.call1(py, (obj,))?.extract(py)?;
|
let len: usize = self.helper.len_fn.call1(py, (obj,))?.extract(py)?;
|
||||||
if len == 0 {
|
if len == 0 {
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
&*unifier.get_ty(*ty),
|
&*unifier.get_ty(ty),
|
||||||
TypeEnum::TVar { fields: None, range, .. }
|
TypeEnum::TVar { fields: None, range, .. }
|
||||||
if range.is_empty()
|
if range.is_empty()
|
||||||
));
|
));
|
||||||
|
@ -717,8 +732,25 @@ impl InnerResolver {
|
||||||
let actual_ty =
|
let actual_ty =
|
||||||
self.get_list_elem_type(py, obj, len, unifier, defs, primitives)?;
|
self.get_list_elem_type(py, obj, len, unifier, defs, primitives)?;
|
||||||
match actual_ty {
|
match actual_ty {
|
||||||
Ok(t) => match unifier.unify(*ty, t) {
|
Ok(t) => match unifier.unify(ty, t) {
|
||||||
Ok(()) => Ok(Ok(unifier.add_ty(TypeEnum::TList { ty: *ty }))),
|
Ok(()) => {
|
||||||
|
let list_tvar = if let TypeEnum::TObj { obj_id, params, .. } =
|
||||||
|
&*unifier.get_ty_immutable(primitives.list)
|
||||||
|
{
|
||||||
|
assert_eq!(*obj_id, PrimDef::List.id());
|
||||||
|
iter_type_vars(params).nth(0).unwrap()
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
let list = unifier
|
||||||
|
.subst(
|
||||||
|
primitives.list,
|
||||||
|
&into_var_map([TypeVar { id: list_tvar.id, ty }]),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
Ok(Ok(list))
|
||||||
|
}
|
||||||
|
|
||||||
Err(e) => Ok(Err(format!(
|
Err(e) => Ok(Err(format!(
|
||||||
"type error ({}) for the list",
|
"type error ({}) for the list",
|
||||||
e.to_display(unifier)
|
e.to_display(unifier)
|
||||||
|
@ -729,9 +761,7 @@ impl InnerResolver {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(TypeEnum::TObj { obj_id, .. }, false) if *obj_id == PrimDef::NDArray.id() => {
|
(TypeEnum::TObj { obj_id, .. }, false) if *obj_id == PrimDef::NDArray.id() => {
|
||||||
let ndarray = primitive_type::NDArrayType::create(extracted_ty, unifier);
|
let (ty, ndims) = unpack_ndarray_var_tys(unifier, extracted_ty);
|
||||||
let ty = ndarray.dtype_tvar(unifier).ty;
|
|
||||||
let ndims = ndarray.ndims_tvar(unifier).ty;
|
|
||||||
let len: usize = obj.getattr("ndim")?.extract()?;
|
let len: usize = obj.getattr("ndim")?.extract()?;
|
||||||
if len == 0 {
|
if len == 0 {
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
|
@ -746,14 +776,10 @@ impl InnerResolver {
|
||||||
match dtype_ty {
|
match dtype_ty {
|
||||||
Ok((t, _)) => match unifier.unify(ty, t) {
|
Ok((t, _)) => match unifier.unify(ty, t) {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
let ndarray_ty = primitive_type::NDArrayType::from_primitive(
|
let ndarray_ty =
|
||||||
unifier,
|
make_ndarray_ty(unifier, primitives, Some(ty), Some(ndims));
|
||||||
primitives,
|
|
||||||
Some(ty),
|
|
||||||
Some(ndims),
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(Ok(ndarray_ty.into()))
|
Ok(Ok(ndarray_ty))
|
||||||
}
|
}
|
||||||
Err(e) => Ok(Err(format!(
|
Err(e) => Ok(Err(format!(
|
||||||
"type error ({}) for the ndarray",
|
"type error ({}) for the ndarray",
|
||||||
|
@ -776,7 +802,7 @@ impl InnerResolver {
|
||||||
// special handling for option type since its class member layout in python side
|
// special handling for option type since its class member layout in python side
|
||||||
// is special and cannot be mapped directly to a nac3 type as below
|
// is special and cannot be mapped directly to a nac3 type as below
|
||||||
(TypeEnum::TObj { obj_id, params, .. }, false)
|
(TypeEnum::TObj { obj_id, params, .. }, false)
|
||||||
if *obj_id == primitives.option.obj_id(unifier) =>
|
if *obj_id == primitives.option.obj_id(unifier).unwrap() =>
|
||||||
{
|
{
|
||||||
let Ok(field_data) = obj.getattr("_nac3_option") else {
|
let Ok(field_data) = obj.getattr("_nac3_option") else {
|
||||||
unreachable!("cannot be None")
|
unreachable!("cannot be None")
|
||||||
|
@ -784,24 +810,22 @@ impl InnerResolver {
|
||||||
// if is `none`
|
// if is `none`
|
||||||
let zelf_id: u64 = self.helper.id_fn.call1(py, (obj,))?.extract(py)?;
|
let zelf_id: u64 = self.helper.id_fn.call1(py, (obj,))?.extract(py)?;
|
||||||
if zelf_id == self.primitive_ids.none {
|
if zelf_id == self.primitive_ids.none {
|
||||||
let extracted_ty = GenericTypeAdapter::create(extracted_ty, unifier);
|
let ty_enum = unifier.get_ty_immutable(primitives.option);
|
||||||
let var_map = extracted_ty.iter_var_map(unifier, |tvar_iter, unifier| {
|
let TypeEnum::TObj { params, .. } = ty_enum.as_ref() else {
|
||||||
tvar_iter
|
unreachable!("must be tobj")
|
||||||
.map(|tvar| {
|
};
|
||||||
let TypeEnum::TVar { id, range, name, loc, .. } =
|
|
||||||
&*unifier.get_ty(tvar.ty)
|
|
||||||
else {
|
|
||||||
unreachable!()
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(*id, tvar.id);
|
let var_map = into_var_map(iter_type_vars(params).map(|tvar| {
|
||||||
let ty = unifier.get_fresh_var_with_range(range, *name, *loc).ty;
|
let TypeEnum::TVar { id, range, name, loc, .. } = &*unifier.get_ty(tvar.ty)
|
||||||
TypeVar { id: *id, ty }
|
else {
|
||||||
})
|
unreachable!()
|
||||||
.map(TypeVar::into)
|
};
|
||||||
.collect::<VarMap>()
|
|
||||||
});
|
assert_eq!(*id, tvar.id);
|
||||||
return Ok(Ok(unifier.subst(primitives.option.into(), &var_map).unwrap()));
|
let ty = unifier.get_fresh_var_with_range(range, *name, *loc).ty;
|
||||||
|
TypeVar { id: *id, ty }
|
||||||
|
}));
|
||||||
|
return Ok(Ok(unifier.subst(primitives.option, &var_map).unwrap()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let ty = match self.get_obj_type(py, field_data, unifier, defs, primitives)? {
|
let ty = match self.get_obj_type(py, field_data, unifier, defs, primitives)? {
|
||||||
|
@ -816,26 +840,19 @@ impl InnerResolver {
|
||||||
let res = unifier.subst(extracted_ty, &new_var_map).unwrap_or(extracted_ty);
|
let res = unifier.subst(extracted_ty, &new_var_map).unwrap_or(extracted_ty);
|
||||||
Ok(Ok(res))
|
Ok(Ok(res))
|
||||||
}
|
}
|
||||||
(TypeEnum::TObj { fields, .. }, false) => {
|
(TypeEnum::TObj { params, fields, .. }, false) => {
|
||||||
self.pyid_to_type.write().insert(py_obj_id, extracted_ty);
|
self.pyid_to_type.write().insert(py_obj_id, extracted_ty);
|
||||||
let extracted_ty = GenericTypeAdapter::create(extracted_ty, unifier);
|
let var_map = into_var_map(iter_type_vars(params).map(|tvar| {
|
||||||
let var_map = extracted_ty.iter_var_map(unifier, |tvar_iter, unifier| {
|
let TypeEnum::TVar { id, range, name, loc, .. } = &*unifier.get_ty(tvar.ty)
|
||||||
tvar_iter
|
else {
|
||||||
.map(|tvar| {
|
unreachable!()
|
||||||
let TypeEnum::TVar { id, range, name, loc, .. } =
|
};
|
||||||
&*unifier.get_ty(tvar.ty)
|
|
||||||
else {
|
|
||||||
unreachable!()
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(*id, tvar.id);
|
assert_eq!(*id, tvar.id);
|
||||||
let ty = unifier.get_fresh_var_with_range(range, *name, *loc).ty;
|
let ty = unifier.get_fresh_var_with_range(range, *name, *loc).ty;
|
||||||
TypeVar { id: *id, ty }
|
TypeVar { id: *id, ty }
|
||||||
})
|
}));
|
||||||
.map(TypeVar::into)
|
let mut instantiate_obj = || {
|
||||||
.collect::<VarMap>()
|
|
||||||
});
|
|
||||||
let instantiate_obj = || {
|
|
||||||
// loop through non-function fields of the class to get the instantiated value
|
// loop through non-function fields of the class to get the instantiated value
|
||||||
for field in fields {
|
for field in fields {
|
||||||
let name: String = (*field.0).into();
|
let name: String = (*field.0).into();
|
||||||
|
@ -870,7 +887,6 @@ impl InnerResolver {
|
||||||
return Ok(Err("object is not of concrete type".into()));
|
return Ok(Err("object is not of concrete type".into()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let extracted_ty = extracted_ty.into();
|
|
||||||
let extracted_ty =
|
let extracted_ty =
|
||||||
unifier.subst(extracted_ty, &var_map).unwrap_or(extracted_ty);
|
unifier.subst(extracted_ty, &var_map).unwrap_or(extracted_ty);
|
||||||
Ok(Ok(extracted_ty))
|
Ok(Ok(extracted_ty))
|
||||||
|
@ -969,12 +985,11 @@ impl InnerResolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
let len: usize = self.helper.len_fn.call1(py, (obj,))?.extract(py)?;
|
let len: usize = self.helper.len_fn.call1(py, (obj,))?.extract(py)?;
|
||||||
let elem_ty = if let TypeEnum::TList { ty } =
|
let elem_ty = match ctx.unifier.get_ty_immutable(expected_ty).as_ref() {
|
||||||
ctx.unifier.get_ty_immutable(expected_ty).as_ref()
|
TypeEnum::TObj { obj_id, params, .. } if *obj_id == PrimDef::List.id() => {
|
||||||
{
|
iter_type_vars(params).nth(0).unwrap().ty
|
||||||
*ty
|
}
|
||||||
} else {
|
_ => unreachable!("must be list"),
|
||||||
unreachable!("must be list")
|
|
||||||
};
|
};
|
||||||
let ty = ctx.get_llvm_type(generator, elem_ty);
|
let ty = ctx.get_llvm_type(generator, elem_ty);
|
||||||
let size_t = generator.get_size_type(ctx.ctx);
|
let size_t = generator.get_size_type(ctx.ctx);
|
||||||
|
@ -1054,9 +1069,8 @@ impl InnerResolver {
|
||||||
} else {
|
} else {
|
||||||
unreachable!("must be ndarray")
|
unreachable!("must be ndarray")
|
||||||
};
|
};
|
||||||
let ndarray_ty = primitive_type::NDArrayType::create(ndarray_ty, &mut ctx.unifier);
|
let (ndarray_dtype, ndarray_ndims) =
|
||||||
let ndarray_dtype = ndarray_ty.dtype_tvar(&mut ctx.unifier).ty;
|
unpack_ndarray_var_tys(&mut ctx.unifier, ndarray_ty);
|
||||||
let ndarray_ndims = ndarray_ty.ndims_tvar(&mut ctx.unifier).ty;
|
|
||||||
|
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
let ndarray_dtype_llvm_ty = ctx.get_llvm_type(generator, ndarray_dtype);
|
let ndarray_dtype_llvm_ty = ctx.get_llvm_type(generator, ndarray_dtype);
|
||||||
|
@ -1203,7 +1217,7 @@ impl InnerResolver {
|
||||||
} else if ty_id == self.primitive_ids.option {
|
} else if ty_id == self.primitive_ids.option {
|
||||||
let option_val_ty = match ctx.unifier.get_ty_immutable(expected_ty).as_ref() {
|
let option_val_ty = match ctx.unifier.get_ty_immutable(expected_ty).as_ref() {
|
||||||
TypeEnum::TObj { obj_id, params, .. }
|
TypeEnum::TObj { obj_id, params, .. }
|
||||||
if *obj_id == ctx.primitives.option.obj_id(&ctx.unifier) =>
|
if *obj_id == ctx.primitives.option.obj_id(&ctx.unifier).unwrap() =>
|
||||||
{
|
{
|
||||||
*params.iter().next().unwrap().1
|
*params.iter().next().unwrap().1
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
[features]
|
||||||
|
test = []
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "nac3core"
|
name = "nac3core"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
|
@ -7,8 +7,8 @@ use std::{
|
||||||
process::{Command, Stdio},
|
process::{Command, Stdio},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() {
|
fn compile_irrt(irrt_dir: &Path, out_dir: &Path) {
|
||||||
const FILE: &str = "src/codegen/irrt/irrt.c";
|
let irrt_cpp_path = irrt_dir.join("irrt.cpp");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* HACK: Sadly, clang doesn't let us emit generic LLVM bitcode.
|
* HACK: Sadly, clang doesn't let us emit generic LLVM bitcode.
|
||||||
|
@ -16,8 +16,12 @@ fn main() {
|
||||||
*/
|
*/
|
||||||
let flags: &[&str] = &[
|
let flags: &[&str] = &[
|
||||||
"--target=wasm32",
|
"--target=wasm32",
|
||||||
FILE,
|
irrt_cpp_path.to_str().unwrap(),
|
||||||
|
"-x",
|
||||||
|
"c++",
|
||||||
"-fno-discard-value-names",
|
"-fno-discard-value-names",
|
||||||
|
"-fno-exceptions",
|
||||||
|
"-fno-rtti",
|
||||||
match env::var("PROFILE").as_deref() {
|
match env::var("PROFILE").as_deref() {
|
||||||
Ok("debug") => "-O0",
|
Ok("debug") => "-O0",
|
||||||
Ok("release") => "-O3",
|
Ok("release") => "-O3",
|
||||||
|
@ -27,13 +31,14 @@ fn main() {
|
||||||
"-S",
|
"-S",
|
||||||
"-Wall",
|
"-Wall",
|
||||||
"-Wextra",
|
"-Wextra",
|
||||||
|
"-Werror=return-type",
|
||||||
|
"-I",
|
||||||
|
irrt_dir.to_str().unwrap(),
|
||||||
"-o",
|
"-o",
|
||||||
"-",
|
"-",
|
||||||
];
|
];
|
||||||
|
|
||||||
println!("cargo:rerun-if-changed={FILE}");
|
println!("cargo:rerun-if-changed={}", out_dir.to_str().unwrap());
|
||||||
let out_dir = env::var("OUT_DIR").unwrap();
|
|
||||||
let out_path = Path::new(&out_dir);
|
|
||||||
|
|
||||||
let output = Command::new("clang-irrt")
|
let output = Command::new("clang-irrt")
|
||||||
.args(flags)
|
.args(flags)
|
||||||
|
@ -48,7 +53,11 @@ fn main() {
|
||||||
let output = std::str::from_utf8(&output.stdout).unwrap().replace("\r\n", "\n");
|
let output = std::str::from_utf8(&output.stdout).unwrap().replace("\r\n", "\n");
|
||||||
let mut filtered_output = String::with_capacity(output.len());
|
let mut filtered_output = String::with_capacity(output.len());
|
||||||
|
|
||||||
let regex_filter = Regex::new(r"(?ms:^define.*?\}$)|(?m:^declare.*?$)").unwrap();
|
// (?ms:^define.*?\}$) to capture `define` blocks
|
||||||
|
// (?m:^declare.*?$) to capture `declare` blocks
|
||||||
|
// (?m:^%.+?=\s*type\s*\{.+?\}$) to capture `type` declarations
|
||||||
|
let regex_filter =
|
||||||
|
Regex::new(r"(?ms:^define.*?\}$)|(?m:^declare.*?$)|(?m:^%.+?=\s*type\s*\{.+?\}$)").unwrap();
|
||||||
for f in regex_filter.captures_iter(&output) {
|
for f in regex_filter.captures_iter(&output) {
|
||||||
assert_eq!(f.len(), 1);
|
assert_eq!(f.len(), 1);
|
||||||
filtered_output.push_str(&f[0]);
|
filtered_output.push_str(&f[0]);
|
||||||
|
@ -61,18 +70,65 @@ fn main() {
|
||||||
|
|
||||||
println!("cargo:rerun-if-env-changed=DEBUG_DUMP_IRRT");
|
println!("cargo:rerun-if-env-changed=DEBUG_DUMP_IRRT");
|
||||||
if env::var("DEBUG_DUMP_IRRT").is_ok() {
|
if env::var("DEBUG_DUMP_IRRT").is_ok() {
|
||||||
let mut file = File::create(out_path.join("irrt.ll")).unwrap();
|
let mut file = File::create(out_dir.join("irrt.ll")).unwrap();
|
||||||
file.write_all(output.as_bytes()).unwrap();
|
file.write_all(output.as_bytes()).unwrap();
|
||||||
let mut file = File::create(out_path.join("irrt-filtered.ll")).unwrap();
|
let mut file = File::create(out_dir.join("irrt-filtered.ll")).unwrap();
|
||||||
file.write_all(filtered_output.as_bytes()).unwrap();
|
file.write_all(filtered_output.as_bytes()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut llvm_as = Command::new("llvm-as-irrt")
|
let mut llvm_as = Command::new("llvm-as-irrt")
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.arg("-o")
|
.arg("-o")
|
||||||
.arg(out_path.join("irrt.bc"))
|
.arg(out_dir.join("irrt.bc"))
|
||||||
.spawn()
|
.spawn()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
llvm_as.stdin.as_mut().unwrap().write_all(filtered_output.as_bytes()).unwrap();
|
llvm_as.stdin.as_mut().unwrap().write_all(filtered_output.as_bytes()).unwrap();
|
||||||
assert!(llvm_as.wait().unwrap().success());
|
assert!(llvm_as.wait().unwrap().success());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn compile_irrt_test(irrt_dir: &Path, out_dir: &Path) {
|
||||||
|
let irrt_test_cpp_path = irrt_dir.join("irrt_test.cpp");
|
||||||
|
let exe_path = out_dir.join("irrt_test.out");
|
||||||
|
|
||||||
|
let flags: &[&str] = &[
|
||||||
|
irrt_test_cpp_path.to_str().unwrap(),
|
||||||
|
"-x",
|
||||||
|
"c++",
|
||||||
|
"-I",
|
||||||
|
irrt_dir.to_str().unwrap(),
|
||||||
|
"-g",
|
||||||
|
"-fno-discard-value-names",
|
||||||
|
"-O0",
|
||||||
|
"-Wall",
|
||||||
|
"-Wextra",
|
||||||
|
"-Werror=return-type",
|
||||||
|
"-lm", // for `tgamma()`, `lgamma()`
|
||||||
|
"-o",
|
||||||
|
exe_path.to_str().unwrap(),
|
||||||
|
];
|
||||||
|
|
||||||
|
Command::new("clang-irrt-test")
|
||||||
|
.args(flags)
|
||||||
|
.output()
|
||||||
|
.map(|o| {
|
||||||
|
assert!(o.status.success(), "{}", std::str::from_utf8(&o.stderr).unwrap());
|
||||||
|
o
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
println!("cargo:rerun-if-changed={}", out_dir.to_str().unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let out_dir = env::var("OUT_DIR").unwrap();
|
||||||
|
let out_dir = Path::new(&out_dir);
|
||||||
|
|
||||||
|
let irrt_dir = Path::new("./irrt");
|
||||||
|
|
||||||
|
compile_irrt(irrt_dir, out_dir);
|
||||||
|
|
||||||
|
// https://github.com/rust-lang/cargo/issues/2549
|
||||||
|
// `cargo test -F test` to also build `irrt_test.cpp
|
||||||
|
if cfg!(feature = "test") {
|
||||||
|
compile_irrt_test(irrt_dir, out_dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
#include "irrt_everything.hpp"
|
||||||
|
|
||||||
|
/*
|
||||||
|
This file will be read by `clang-irrt` to conveniently produce LLVM IR for `nac3core/codegen`.
|
||||||
|
*/
|
|
@ -0,0 +1,437 @@
|
||||||
|
#ifndef IRRT_DONT_TYPEDEF_INTS
|
||||||
|
typedef _BitInt(8) int8_t;
|
||||||
|
typedef unsigned _BitInt(8) uint8_t;
|
||||||
|
typedef _BitInt(32) int32_t;
|
||||||
|
typedef unsigned _BitInt(32) uint32_t;
|
||||||
|
typedef _BitInt(64) int64_t;
|
||||||
|
typedef unsigned _BitInt(64) uint64_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// NDArray indices are always `uint32_t`.
|
||||||
|
typedef uint32_t NDIndex;
|
||||||
|
// The type of an index or a value describing the length of a range/slice is
|
||||||
|
// always `int32_t`.
|
||||||
|
typedef int32_t SliceIndex;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static T max(T a, T b) {
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static T min(T a, T b) {
|
||||||
|
return a > b ? b : a;
|
||||||
|
}
|
||||||
|
|
||||||
|
// adapted from GNU Scientific Library: https://git.savannah.gnu.org/cgit/gsl.git/tree/sys/pow_int.c
|
||||||
|
// need to make sure `exp >= 0` before calling this function
|
||||||
|
template <typename T>
|
||||||
|
static T __nac3_int_exp_impl(T base, T exp) {
|
||||||
|
T res = 1;
|
||||||
|
/* repeated squaring method */
|
||||||
|
do {
|
||||||
|
if (exp & 1) {
|
||||||
|
res *= base; /* for n odd */
|
||||||
|
}
|
||||||
|
exp >>= 1;
|
||||||
|
base *= base;
|
||||||
|
} while (exp);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename SizeT>
|
||||||
|
static SizeT __nac3_ndarray_calc_size_impl(
|
||||||
|
const SizeT *list_data,
|
||||||
|
SizeT list_len,
|
||||||
|
SizeT begin_idx,
|
||||||
|
SizeT end_idx
|
||||||
|
) {
|
||||||
|
__builtin_assume(end_idx <= list_len);
|
||||||
|
|
||||||
|
SizeT num_elems = 1;
|
||||||
|
for (SizeT i = begin_idx; i < end_idx; ++i) {
|
||||||
|
SizeT val = list_data[i];
|
||||||
|
__builtin_assume(val > 0);
|
||||||
|
num_elems *= val;
|
||||||
|
}
|
||||||
|
return num_elems;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename SizeT>
|
||||||
|
static void __nac3_ndarray_calc_nd_indices_impl(
|
||||||
|
SizeT index,
|
||||||
|
const SizeT *dims,
|
||||||
|
SizeT num_dims,
|
||||||
|
NDIndex *idxs
|
||||||
|
) {
|
||||||
|
SizeT stride = 1;
|
||||||
|
for (SizeT dim = 0; dim < num_dims; dim++) {
|
||||||
|
SizeT i = num_dims - dim - 1;
|
||||||
|
__builtin_assume(dims[i] > 0);
|
||||||
|
idxs[i] = (index / stride) % dims[i];
|
||||||
|
stride *= dims[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename SizeT>
|
||||||
|
static SizeT __nac3_ndarray_flatten_index_impl(
|
||||||
|
const SizeT *dims,
|
||||||
|
SizeT num_dims,
|
||||||
|
const NDIndex *indices,
|
||||||
|
SizeT num_indices
|
||||||
|
) {
|
||||||
|
SizeT idx = 0;
|
||||||
|
SizeT stride = 1;
|
||||||
|
for (SizeT i = 0; i < num_dims; ++i) {
|
||||||
|
SizeT ri = num_dims - i - 1;
|
||||||
|
if (ri < num_indices) {
|
||||||
|
idx += stride * indices[ri];
|
||||||
|
}
|
||||||
|
|
||||||
|
__builtin_assume(dims[i] > 0);
|
||||||
|
stride *= dims[ri];
|
||||||
|
}
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename SizeT>
|
||||||
|
static void __nac3_ndarray_calc_broadcast_impl(
|
||||||
|
const SizeT *lhs_dims,
|
||||||
|
SizeT lhs_ndims,
|
||||||
|
const SizeT *rhs_dims,
|
||||||
|
SizeT rhs_ndims,
|
||||||
|
SizeT *out_dims
|
||||||
|
) {
|
||||||
|
SizeT max_ndims = lhs_ndims > rhs_ndims ? lhs_ndims : rhs_ndims;
|
||||||
|
|
||||||
|
for (SizeT i = 0; i < max_ndims; ++i) {
|
||||||
|
const SizeT *lhs_dim_sz = i < lhs_ndims ? &lhs_dims[lhs_ndims - i - 1] : nullptr;
|
||||||
|
const SizeT *rhs_dim_sz = i < rhs_ndims ? &rhs_dims[rhs_ndims - i - 1] : nullptr;
|
||||||
|
SizeT *out_dim = &out_dims[max_ndims - i - 1];
|
||||||
|
|
||||||
|
if (lhs_dim_sz == nullptr) {
|
||||||
|
*out_dim = *rhs_dim_sz;
|
||||||
|
} else if (rhs_dim_sz == nullptr) {
|
||||||
|
*out_dim = *lhs_dim_sz;
|
||||||
|
} else if (*lhs_dim_sz == 1) {
|
||||||
|
*out_dim = *rhs_dim_sz;
|
||||||
|
} else if (*rhs_dim_sz == 1) {
|
||||||
|
*out_dim = *lhs_dim_sz;
|
||||||
|
} else if (*lhs_dim_sz == *rhs_dim_sz) {
|
||||||
|
*out_dim = *lhs_dim_sz;
|
||||||
|
} else {
|
||||||
|
__builtin_unreachable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename SizeT>
|
||||||
|
static void __nac3_ndarray_calc_broadcast_idx_impl(
|
||||||
|
const SizeT *src_dims,
|
||||||
|
SizeT src_ndims,
|
||||||
|
const NDIndex *in_idx,
|
||||||
|
NDIndex *out_idx
|
||||||
|
) {
|
||||||
|
for (SizeT i = 0; i < src_ndims; ++i) {
|
||||||
|
SizeT src_i = src_ndims - i - 1;
|
||||||
|
out_idx[src_i] = src_dims[src_i] == 1 ? 0 : in_idx[src_i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename SizeT>
|
||||||
|
static void __nac3_ndarray_strides_from_shape_impl(
|
||||||
|
SizeT ndims,
|
||||||
|
SizeT *shape,
|
||||||
|
SizeT *dst_strides
|
||||||
|
) {
|
||||||
|
SizeT stride_product = 1;
|
||||||
|
for (SizeT i = 0; i < ndims; i++) {
|
||||||
|
int dim_i = ndims - i - 1;
|
||||||
|
dst_strides[dim_i] = stride_product;
|
||||||
|
stride_product *= shape[dim_i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#define DEF_nac3_int_exp_(T) \
|
||||||
|
T __nac3_int_exp_##T(T base, T exp) {\
|
||||||
|
return __nac3_int_exp_impl(base, exp);\
|
||||||
|
}
|
||||||
|
|
||||||
|
DEF_nac3_int_exp_(int32_t)
|
||||||
|
DEF_nac3_int_exp_(int64_t)
|
||||||
|
DEF_nac3_int_exp_(uint32_t)
|
||||||
|
DEF_nac3_int_exp_(uint64_t)
|
||||||
|
|
||||||
|
SliceIndex __nac3_slice_index_bound(SliceIndex i, const SliceIndex len) {
|
||||||
|
if (i < 0) {
|
||||||
|
i = len + i;
|
||||||
|
}
|
||||||
|
if (i < 0) {
|
||||||
|
return 0;
|
||||||
|
} else if (i > len) {
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
SliceIndex __nac3_range_slice_len(
|
||||||
|
const SliceIndex start,
|
||||||
|
const SliceIndex end,
|
||||||
|
const SliceIndex step
|
||||||
|
) {
|
||||||
|
SliceIndex diff = end - start;
|
||||||
|
if (diff > 0 && step > 0) {
|
||||||
|
return ((diff - 1) / step) + 1;
|
||||||
|
} else if (diff < 0 && step < 0) {
|
||||||
|
return ((diff + 1) / step) + 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle list assignment and dropping part of the list when
|
||||||
|
// both dest_step and src_step are +1.
|
||||||
|
// - All the index must *not* be out-of-bound or negative,
|
||||||
|
// - The end index is *inclusive*,
|
||||||
|
// - The length of src and dest slice size should already
|
||||||
|
// be checked: if dest.step == 1 then len(src) <= len(dest) else len(src) == len(dest)
|
||||||
|
SliceIndex __nac3_list_slice_assign_var_size(
|
||||||
|
SliceIndex dest_start,
|
||||||
|
SliceIndex dest_end,
|
||||||
|
SliceIndex dest_step,
|
||||||
|
uint8_t *dest_arr,
|
||||||
|
SliceIndex dest_arr_len,
|
||||||
|
SliceIndex src_start,
|
||||||
|
SliceIndex src_end,
|
||||||
|
SliceIndex src_step,
|
||||||
|
uint8_t *src_arr,
|
||||||
|
SliceIndex src_arr_len,
|
||||||
|
const SliceIndex size
|
||||||
|
) {
|
||||||
|
/* if dest_arr_len == 0, do nothing since we do not support extending list */
|
||||||
|
if (dest_arr_len == 0) return dest_arr_len;
|
||||||
|
/* if both step is 1, memmove directly, handle the dropping of the list, and shrink size */
|
||||||
|
if (src_step == dest_step && dest_step == 1) {
|
||||||
|
const SliceIndex src_len = (src_end >= src_start) ? (src_end - src_start + 1) : 0;
|
||||||
|
const SliceIndex dest_len = (dest_end >= dest_start) ? (dest_end - dest_start + 1) : 0;
|
||||||
|
if (src_len > 0) {
|
||||||
|
__builtin_memmove(
|
||||||
|
dest_arr + dest_start * size,
|
||||||
|
src_arr + src_start * size,
|
||||||
|
src_len * size
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (dest_len > 0) {
|
||||||
|
/* dropping */
|
||||||
|
__builtin_memmove(
|
||||||
|
dest_arr + (dest_start + src_len) * size,
|
||||||
|
dest_arr + (dest_end + 1) * size,
|
||||||
|
(dest_arr_len - dest_end - 1) * size
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/* shrink size */
|
||||||
|
return dest_arr_len - (dest_len - src_len);
|
||||||
|
}
|
||||||
|
/* if two range overlaps, need alloca */
|
||||||
|
uint8_t need_alloca =
|
||||||
|
(dest_arr == src_arr)
|
||||||
|
&& !(
|
||||||
|
max(dest_start, dest_end) < min(src_start, src_end)
|
||||||
|
|| max(src_start, src_end) < min(dest_start, dest_end)
|
||||||
|
);
|
||||||
|
if (need_alloca) {
|
||||||
|
uint8_t *tmp = reinterpret_cast<uint8_t *>(__builtin_alloca(src_arr_len * size));
|
||||||
|
__builtin_memcpy(tmp, src_arr, src_arr_len * size);
|
||||||
|
src_arr = tmp;
|
||||||
|
}
|
||||||
|
SliceIndex src_ind = src_start;
|
||||||
|
SliceIndex dest_ind = dest_start;
|
||||||
|
for (;
|
||||||
|
(src_step > 0) ? (src_ind <= src_end) : (src_ind >= src_end);
|
||||||
|
src_ind += src_step, dest_ind += dest_step
|
||||||
|
) {
|
||||||
|
/* for constant optimization */
|
||||||
|
if (size == 1) {
|
||||||
|
__builtin_memcpy(dest_arr + dest_ind, src_arr + src_ind, 1);
|
||||||
|
} else if (size == 4) {
|
||||||
|
__builtin_memcpy(dest_arr + dest_ind * 4, src_arr + src_ind * 4, 4);
|
||||||
|
} else if (size == 8) {
|
||||||
|
__builtin_memcpy(dest_arr + dest_ind * 8, src_arr + src_ind * 8, 8);
|
||||||
|
} else {
|
||||||
|
/* memcpy for var size, cannot overlap after previous alloca */
|
||||||
|
__builtin_memcpy(dest_arr + dest_ind * size, src_arr + src_ind * size, size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* only dest_step == 1 can we shrink the dest list. */
|
||||||
|
/* size should be ensured prior to calling this function */
|
||||||
|
if (dest_step == 1 && dest_end >= dest_start) {
|
||||||
|
__builtin_memmove(
|
||||||
|
dest_arr + dest_ind * size,
|
||||||
|
dest_arr + (dest_end + 1) * size,
|
||||||
|
(dest_arr_len - dest_end - 1) * size
|
||||||
|
);
|
||||||
|
return dest_arr_len - (dest_end - dest_ind) - 1;
|
||||||
|
}
|
||||||
|
return dest_arr_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t __nac3_isinf(double x) {
|
||||||
|
return __builtin_isinf(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t __nac3_isnan(double x) {
|
||||||
|
return __builtin_isnan(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
double tgamma(double arg);
|
||||||
|
|
||||||
|
double __nac3_gamma(double z) {
|
||||||
|
// Handling for denormals
|
||||||
|
// | x | Python gamma(x) | C tgamma(x) |
|
||||||
|
// --- | ----------------- | --------------- | ----------- |
|
||||||
|
// (1) | nan | nan | nan |
|
||||||
|
// (2) | -inf | -inf | inf |
|
||||||
|
// (3) | inf | inf | inf |
|
||||||
|
// (4) | 0.0 | inf | inf |
|
||||||
|
// (5) | {-1.0, -2.0, ...} | inf | nan |
|
||||||
|
|
||||||
|
// (1)-(3)
|
||||||
|
if (__builtin_isinf(z) || __builtin_isnan(z)) {
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
double v = tgamma(z);
|
||||||
|
|
||||||
|
// (4)-(5)
|
||||||
|
return __builtin_isinf(v) || __builtin_isnan(v) ? __builtin_inf() : v;
|
||||||
|
}
|
||||||
|
|
||||||
|
double lgamma(double arg);
|
||||||
|
|
||||||
|
double __nac3_gammaln(double x) {
|
||||||
|
// libm's handling of value overflows differs from scipy:
|
||||||
|
// - scipy: gammaln(-inf) -> -inf
|
||||||
|
// - libm : lgamma(-inf) -> inf
|
||||||
|
|
||||||
|
if (__builtin_isinf(x)) {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
return lgamma(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
double j0(double x);
|
||||||
|
|
||||||
|
double __nac3_j0(double x) {
|
||||||
|
// libm's handling of value overflows differs from scipy:
|
||||||
|
// - scipy: j0(inf) -> nan
|
||||||
|
// - libm : j0(inf) -> 0.0
|
||||||
|
|
||||||
|
if (__builtin_isinf(x)) {
|
||||||
|
return __builtin_nan("");
|
||||||
|
}
|
||||||
|
|
||||||
|
return j0(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t __nac3_ndarray_calc_size(
|
||||||
|
const uint32_t *list_data,
|
||||||
|
uint32_t list_len,
|
||||||
|
uint32_t begin_idx,
|
||||||
|
uint32_t end_idx
|
||||||
|
) {
|
||||||
|
return __nac3_ndarray_calc_size_impl(list_data, list_len, begin_idx, end_idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t __nac3_ndarray_calc_size64(
|
||||||
|
const uint64_t *list_data,
|
||||||
|
uint64_t list_len,
|
||||||
|
uint64_t begin_idx,
|
||||||
|
uint64_t end_idx
|
||||||
|
) {
|
||||||
|
return __nac3_ndarray_calc_size_impl(list_data, list_len, begin_idx, end_idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_calc_nd_indices(
|
||||||
|
uint32_t index,
|
||||||
|
const uint32_t* dims,
|
||||||
|
uint32_t num_dims,
|
||||||
|
NDIndex* idxs
|
||||||
|
) {
|
||||||
|
__nac3_ndarray_calc_nd_indices_impl(index, dims, num_dims, idxs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_calc_nd_indices64(
|
||||||
|
uint64_t index,
|
||||||
|
const uint64_t* dims,
|
||||||
|
uint64_t num_dims,
|
||||||
|
NDIndex* idxs
|
||||||
|
) {
|
||||||
|
__nac3_ndarray_calc_nd_indices_impl(index, dims, num_dims, idxs);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t __nac3_ndarray_flatten_index(
|
||||||
|
const uint32_t* dims,
|
||||||
|
uint32_t num_dims,
|
||||||
|
const NDIndex* indices,
|
||||||
|
uint32_t num_indices
|
||||||
|
) {
|
||||||
|
return __nac3_ndarray_flatten_index_impl(dims, num_dims, indices, num_indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t __nac3_ndarray_flatten_index64(
|
||||||
|
const uint64_t* dims,
|
||||||
|
uint64_t num_dims,
|
||||||
|
const NDIndex* indices,
|
||||||
|
uint64_t num_indices
|
||||||
|
) {
|
||||||
|
return __nac3_ndarray_flatten_index_impl(dims, num_dims, indices, num_indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_calc_broadcast(
|
||||||
|
const uint32_t *lhs_dims,
|
||||||
|
uint32_t lhs_ndims,
|
||||||
|
const uint32_t *rhs_dims,
|
||||||
|
uint32_t rhs_ndims,
|
||||||
|
uint32_t *out_dims
|
||||||
|
) {
|
||||||
|
return __nac3_ndarray_calc_broadcast_impl(lhs_dims, lhs_ndims, rhs_dims, rhs_ndims, out_dims);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_calc_broadcast64(
|
||||||
|
const uint64_t *lhs_dims,
|
||||||
|
uint64_t lhs_ndims,
|
||||||
|
const uint64_t *rhs_dims,
|
||||||
|
uint64_t rhs_ndims,
|
||||||
|
uint64_t *out_dims
|
||||||
|
) {
|
||||||
|
return __nac3_ndarray_calc_broadcast_impl(lhs_dims, lhs_ndims, rhs_dims, rhs_ndims, out_dims);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_calc_broadcast_idx(
|
||||||
|
const uint32_t *src_dims,
|
||||||
|
uint32_t src_ndims,
|
||||||
|
const NDIndex *in_idx,
|
||||||
|
NDIndex *out_idx
|
||||||
|
) {
|
||||||
|
__nac3_ndarray_calc_broadcast_idx_impl(src_dims, src_ndims, in_idx, out_idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_calc_broadcast_idx64(
|
||||||
|
const uint64_t *src_dims,
|
||||||
|
uint64_t src_ndims,
|
||||||
|
const NDIndex *in_idx,
|
||||||
|
NDIndex *out_idx
|
||||||
|
) {
|
||||||
|
__nac3_ndarray_calc_broadcast_idx_impl(src_dims, src_ndims, in_idx, out_idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_strides_from_shape(uint32_t ndims, uint32_t* shape, uint32_t* dst_strides) {
|
||||||
|
__nac3_ndarray_strides_from_shape_impl(ndims, shape, dst_strides);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_strides_from_shape64(uint64_t ndims, uint64_t* shape, uint64_t* dst_strides) {
|
||||||
|
__nac3_ndarray_strides_from_shape_impl(ndims, shape, dst_strides);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,216 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "irrt_utils.hpp"
|
||||||
|
#include "irrt_typedefs.hpp"
|
||||||
|
|
||||||
|
/*
|
||||||
|
This header contains IRRT implementations
|
||||||
|
that do not deserved to be categorized (e.g., into numpy, etc.)
|
||||||
|
|
||||||
|
Check out other *.hpp files before including them here!!
|
||||||
|
*/
|
||||||
|
|
||||||
|
// The type of an index or a value describing the length of a range/slice is
|
||||||
|
// always `int32_t`.
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
// adapted from GNU Scientific Library: https://git.savannah.gnu.org/cgit/gsl.git/tree/sys/pow_int.c
|
||||||
|
// need to make sure `exp >= 0` before calling this function
|
||||||
|
template <typename T>
|
||||||
|
T __nac3_int_exp_impl(T base, T exp) {
|
||||||
|
T res = 1;
|
||||||
|
/* repeated squaring method */
|
||||||
|
do {
|
||||||
|
if (exp & 1) {
|
||||||
|
res *= base; /* for n odd */
|
||||||
|
}
|
||||||
|
exp >>= 1;
|
||||||
|
base *= base;
|
||||||
|
} while (exp);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#define DEF_nac3_int_exp_(T) \
|
||||||
|
T __nac3_int_exp_##T(T base, T exp) {\
|
||||||
|
return __nac3_int_exp_impl(base, exp);\
|
||||||
|
}
|
||||||
|
|
||||||
|
DEF_nac3_int_exp_(int32_t)
|
||||||
|
DEF_nac3_int_exp_(int64_t)
|
||||||
|
DEF_nac3_int_exp_(uint32_t)
|
||||||
|
DEF_nac3_int_exp_(uint64_t)
|
||||||
|
|
||||||
|
SliceIndex __nac3_slice_index_bound(SliceIndex i, const SliceIndex len) {
|
||||||
|
if (i < 0) {
|
||||||
|
i = len + i;
|
||||||
|
}
|
||||||
|
if (i < 0) {
|
||||||
|
return 0;
|
||||||
|
} else if (i > len) {
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
SliceIndex __nac3_range_slice_len(
|
||||||
|
const SliceIndex start,
|
||||||
|
const SliceIndex end,
|
||||||
|
const SliceIndex step
|
||||||
|
) {
|
||||||
|
SliceIndex diff = end - start;
|
||||||
|
if (diff > 0 && step > 0) {
|
||||||
|
return ((diff - 1) / step) + 1;
|
||||||
|
} else if (diff < 0 && step < 0) {
|
||||||
|
return ((diff + 1) / step) + 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle list assignment and dropping part of the list when
|
||||||
|
// both dest_step and src_step are +1.
|
||||||
|
// - All the index must *not* be out-of-bound or negative,
|
||||||
|
// - The end index is *inclusive*,
|
||||||
|
// - The length of src and dest slice size should already
|
||||||
|
// be checked: if dest.step == 1 then len(src) <= len(dest) else len(src) == len(dest)
|
||||||
|
SliceIndex __nac3_list_slice_assign_var_size(
|
||||||
|
SliceIndex dest_start,
|
||||||
|
SliceIndex dest_end,
|
||||||
|
SliceIndex dest_step,
|
||||||
|
uint8_t *dest_arr,
|
||||||
|
SliceIndex dest_arr_len,
|
||||||
|
SliceIndex src_start,
|
||||||
|
SliceIndex src_end,
|
||||||
|
SliceIndex src_step,
|
||||||
|
uint8_t *src_arr,
|
||||||
|
SliceIndex src_arr_len,
|
||||||
|
const SliceIndex size
|
||||||
|
) {
|
||||||
|
/* if dest_arr_len == 0, do nothing since we do not support extending list */
|
||||||
|
if (dest_arr_len == 0) return dest_arr_len;
|
||||||
|
/* if both step is 1, memmove directly, handle the dropping of the list, and shrink size */
|
||||||
|
if (src_step == dest_step && dest_step == 1) {
|
||||||
|
const SliceIndex src_len = (src_end >= src_start) ? (src_end - src_start + 1) : 0;
|
||||||
|
const SliceIndex dest_len = (dest_end >= dest_start) ? (dest_end - dest_start + 1) : 0;
|
||||||
|
if (src_len > 0) {
|
||||||
|
__builtin_memmove(
|
||||||
|
dest_arr + dest_start * size,
|
||||||
|
src_arr + src_start * size,
|
||||||
|
src_len * size
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (dest_len > 0) {
|
||||||
|
/* dropping */
|
||||||
|
__builtin_memmove(
|
||||||
|
dest_arr + (dest_start + src_len) * size,
|
||||||
|
dest_arr + (dest_end + 1) * size,
|
||||||
|
(dest_arr_len - dest_end - 1) * size
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/* shrink size */
|
||||||
|
return dest_arr_len - (dest_len - src_len);
|
||||||
|
}
|
||||||
|
/* if two range overlaps, need alloca */
|
||||||
|
uint8_t need_alloca =
|
||||||
|
(dest_arr == src_arr)
|
||||||
|
&& !(
|
||||||
|
max(dest_start, dest_end) < min(src_start, src_end)
|
||||||
|
|| max(src_start, src_end) < min(dest_start, dest_end)
|
||||||
|
);
|
||||||
|
if (need_alloca) {
|
||||||
|
uint8_t *tmp = reinterpret_cast<uint8_t *>(__builtin_alloca(src_arr_len * size));
|
||||||
|
__builtin_memcpy(tmp, src_arr, src_arr_len * size);
|
||||||
|
src_arr = tmp;
|
||||||
|
}
|
||||||
|
SliceIndex src_ind = src_start;
|
||||||
|
SliceIndex dest_ind = dest_start;
|
||||||
|
for (;
|
||||||
|
(src_step > 0) ? (src_ind <= src_end) : (src_ind >= src_end);
|
||||||
|
src_ind += src_step, dest_ind += dest_step
|
||||||
|
) {
|
||||||
|
/* for constant optimization */
|
||||||
|
if (size == 1) {
|
||||||
|
__builtin_memcpy(dest_arr + dest_ind, src_arr + src_ind, 1);
|
||||||
|
} else if (size == 4) {
|
||||||
|
__builtin_memcpy(dest_arr + dest_ind * 4, src_arr + src_ind * 4, 4);
|
||||||
|
} else if (size == 8) {
|
||||||
|
__builtin_memcpy(dest_arr + dest_ind * 8, src_arr + src_ind * 8, 8);
|
||||||
|
} else {
|
||||||
|
/* memcpy for var size, cannot overlap after previous alloca */
|
||||||
|
__builtin_memcpy(dest_arr + dest_ind * size, src_arr + src_ind * size, size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* only dest_step == 1 can we shrink the dest list. */
|
||||||
|
/* size should be ensured prior to calling this function */
|
||||||
|
if (dest_step == 1 && dest_end >= dest_start) {
|
||||||
|
__builtin_memmove(
|
||||||
|
dest_arr + dest_ind * size,
|
||||||
|
dest_arr + (dest_end + 1) * size,
|
||||||
|
(dest_arr_len - dest_end - 1) * size
|
||||||
|
);
|
||||||
|
return dest_arr_len - (dest_end - dest_ind) - 1;
|
||||||
|
}
|
||||||
|
return dest_arr_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t __nac3_isinf(double x) {
|
||||||
|
return __builtin_isinf(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t __nac3_isnan(double x) {
|
||||||
|
return __builtin_isnan(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
double tgamma(double arg);
|
||||||
|
|
||||||
|
double __nac3_gamma(double z) {
|
||||||
|
// Handling for denormals
|
||||||
|
// | x | Python gamma(x) | C tgamma(x) |
|
||||||
|
// --- | ----------------- | --------------- | ----------- |
|
||||||
|
// (1) | nan | nan | nan |
|
||||||
|
// (2) | -inf | -inf | inf |
|
||||||
|
// (3) | inf | inf | inf |
|
||||||
|
// (4) | 0.0 | inf | inf |
|
||||||
|
// (5) | {-1.0, -2.0, ...} | inf | nan |
|
||||||
|
|
||||||
|
// (1)-(3)
|
||||||
|
if (__builtin_isinf(z) || __builtin_isnan(z)) {
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
double v = tgamma(z);
|
||||||
|
|
||||||
|
// (4)-(5)
|
||||||
|
return __builtin_isinf(v) || __builtin_isnan(v) ? __builtin_inf() : v;
|
||||||
|
}
|
||||||
|
|
||||||
|
double lgamma(double arg);
|
||||||
|
|
||||||
|
double __nac3_gammaln(double x) {
|
||||||
|
// libm's handling of value overflows differs from scipy:
|
||||||
|
// - scipy: gammaln(-inf) -> -inf
|
||||||
|
// - libm : lgamma(-inf) -> inf
|
||||||
|
|
||||||
|
if (__builtin_isinf(x)) {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
return lgamma(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
double j0(double x);
|
||||||
|
|
||||||
|
double __nac3_j0(double x) {
|
||||||
|
// libm's handling of value overflows differs from scipy:
|
||||||
|
// - scipy: j0(inf) -> nan
|
||||||
|
// - libm : j0(inf) -> 0.0
|
||||||
|
|
||||||
|
if (__builtin_isinf(x)) {
|
||||||
|
return __builtin_nan("");
|
||||||
|
}
|
||||||
|
|
||||||
|
return j0(x);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "irrt_utils.hpp"
|
||||||
|
#include "irrt_typedefs.hpp"
|
||||||
|
#include "irrt_basic.hpp"
|
||||||
|
#include "irrt_slice.hpp"
|
||||||
|
#include "irrt_numpy_ndarray.hpp"
|
||||||
|
|
||||||
|
/*
|
||||||
|
All IRRT implementations.
|
||||||
|
|
||||||
|
We don't have any pre-compiled objects, so we are writing all implementations in headers and
|
||||||
|
concatenate them with `#include` into one massive source file that contains all the IRRT stuff.
|
||||||
|
*/
|
|
@ -0,0 +1,466 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "irrt_utils.hpp"
|
||||||
|
#include "irrt_typedefs.hpp"
|
||||||
|
#include "irrt_slice.hpp"
|
||||||
|
|
||||||
|
/*
|
||||||
|
NDArray-related implementations.
|
||||||
|
`*/
|
||||||
|
|
||||||
|
// NDArray indices are always `uint32_t`.
|
||||||
|
using NDIndex = uint32_t;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
namespace ndarray_util {
|
||||||
|
template <typename SizeT>
|
||||||
|
static void set_indices_by_nth(SizeT ndims, const SizeT* shape, SizeT* indices, SizeT nth) {
|
||||||
|
for (int32_t i = 0; i < ndims; i++) {
|
||||||
|
int32_t dim_i = ndims - i - 1;
|
||||||
|
int32_t dim = shape[dim_i];
|
||||||
|
|
||||||
|
indices[dim_i] = nth % dim;
|
||||||
|
nth /= dim;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute the strides of an ndarray given an ndarray `shape`
|
||||||
|
// and assuming that the ndarray is *fully C-contagious*.
|
||||||
|
//
|
||||||
|
// You might want to read up on https://ajcr.net/stride-guide-part-1/.
|
||||||
|
template <typename SizeT>
|
||||||
|
static void set_strides_by_shape(SizeT itemsize, SizeT ndims, SizeT* dst_strides, const SizeT* shape) {
|
||||||
|
SizeT stride_product = 1;
|
||||||
|
for (SizeT i = 0; i < ndims; i++) {
|
||||||
|
int dim_i = ndims - i - 1;
|
||||||
|
dst_strides[dim_i] = stride_product * itemsize;
|
||||||
|
stride_product *= shape[dim_i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute the size/# of elements of an ndarray given its shape
|
||||||
|
template <typename SizeT>
|
||||||
|
static SizeT calc_size_from_shape(SizeT ndims, const SizeT* shape) {
|
||||||
|
SizeT size = 1;
|
||||||
|
for (SizeT dim_i = 0; dim_i < ndims; dim_i++) size *= shape[dim_i];
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename SizeT>
|
||||||
|
static bool can_broadcast_shape_to(
|
||||||
|
const SizeT target_ndims,
|
||||||
|
const SizeT *target_shape,
|
||||||
|
const SizeT src_ndims,
|
||||||
|
const SizeT *src_shape
|
||||||
|
) {
|
||||||
|
/*
|
||||||
|
// See https://numpy.org/doc/stable/user/basics.broadcasting.html
|
||||||
|
|
||||||
|
This function handles this example:
|
||||||
|
```
|
||||||
|
Image (3d array): 256 x 256 x 3
|
||||||
|
Scale (1d array): 3
|
||||||
|
Result (3d array): 256 x 256 x 3
|
||||||
|
```
|
||||||
|
|
||||||
|
Other interesting examples to consider:
|
||||||
|
- `can_broadcast_shape_to([3], [1, 1, 1, 1, 3]) == true`
|
||||||
|
- `can_broadcast_shape_to([3], [3, 1]) == false`
|
||||||
|
- `can_broadcast_shape_to([256, 256, 3], [256, 1, 3]) == true`
|
||||||
|
|
||||||
|
In cases when the shapes contain zero(es):
|
||||||
|
- `can_broadcast_shape_to([0], [1]) == true`
|
||||||
|
- `can_broadcast_shape_to([0], [2]) == false`
|
||||||
|
- `can_broadcast_shape_to([0, 4, 0, 0], [1]) == true`
|
||||||
|
- `can_broadcast_shape_to([0, 4, 0, 0], [1, 1, 1, 1]) == true`
|
||||||
|
- `can_broadcast_shape_to([0, 4, 0, 0], [1, 4, 1, 1]) == true`
|
||||||
|
- `can_broadcast_shape_to([4, 3], [0, 3]) == false`
|
||||||
|
- `can_broadcast_shape_to([4, 3], [0, 0]) == false`
|
||||||
|
*/
|
||||||
|
|
||||||
|
// This is essentially doing the following in Python:
|
||||||
|
// `for target_dim, src_dim in itertools.zip_longest(target_shape[::-1], src_shape[::-1], fillvalue=1)`
|
||||||
|
for (SizeT i = 0; i < max(target_ndims, src_ndims); i++) {
|
||||||
|
SizeT target_dim_i = target_ndims - i - 1;
|
||||||
|
SizeT src_dim_i = src_ndims - i - 1;
|
||||||
|
|
||||||
|
bool target_dim_exists = target_dim_i >= 0;
|
||||||
|
bool src_dim_exists = src_dim_i >= 0;
|
||||||
|
|
||||||
|
SizeT target_dim = target_dim_exists ? target_shape[target_dim_i] : 1;
|
||||||
|
SizeT src_dim = src_dim_exists ? src_shape[src_dim_i] : 1;
|
||||||
|
|
||||||
|
bool ok = src_dim == 1 || target_dim == src_dim;
|
||||||
|
if (!ok) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef uint8_t NDSliceType;
|
||||||
|
extern "C" {
|
||||||
|
const NDSliceType INPUT_SLICE_TYPE_INDEX = 0;
|
||||||
|
const NDSliceType INPUT_SLICE_TYPE_SLICE = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct NDSlice {
|
||||||
|
// A poor-man's `std::variant<int, UserRange>`
|
||||||
|
NDSliceType type;
|
||||||
|
|
||||||
|
/*
|
||||||
|
if type == INPUT_SLICE_TYPE_INDEX => `slice` points to a single `SizeT`
|
||||||
|
if type == INPUT_SLICE_TYPE_SLICE => `slice` points to a single `UserRange`
|
||||||
|
*/
|
||||||
|
uint8_t *slice;
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace ndarray_util {
|
||||||
|
template<typename SizeT>
|
||||||
|
SizeT deduce_ndims_after_slicing(SizeT ndims, SizeT num_slices, const NDSlice *slices) {
|
||||||
|
irrt_assert(num_slices <= ndims);
|
||||||
|
|
||||||
|
SizeT final_ndims = ndims;
|
||||||
|
for (SizeT i = 0; i < num_slices; i++) {
|
||||||
|
if (slices[i].type == INPUT_SLICE_TYPE_INDEX) {
|
||||||
|
final_ndims--; // An integer slice demotes the rank by 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return final_ndims;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename SizeT>
|
||||||
|
struct NDArrayIndicesIter {
|
||||||
|
SizeT ndims;
|
||||||
|
const SizeT *shape;
|
||||||
|
SizeT *indices;
|
||||||
|
|
||||||
|
void set_indices_zero() {
|
||||||
|
__builtin_memset(indices, 0, sizeof(SizeT) * ndims);
|
||||||
|
}
|
||||||
|
|
||||||
|
void next() {
|
||||||
|
for (SizeT i = 0; i < ndims; i++) {
|
||||||
|
SizeT dim_i = ndims - i - 1;
|
||||||
|
|
||||||
|
indices[dim_i]++;
|
||||||
|
if (indices[dim_i] < shape[dim_i]) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
indices[dim_i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// The NDArray object. `SizeT` is the *signed* size type of this ndarray.
|
||||||
|
//
|
||||||
|
// NOTE: The order of fields is IMPORTANT. DON'T TOUCH IT
|
||||||
|
//
|
||||||
|
// Some resources you might find helpful:
|
||||||
|
// - The official numpy implementations:
|
||||||
|
// - https://github.com/numpy/numpy/blob/735a477f0bc2b5b84d0e72d92f224bde78d4e069/doc/source/reference/c-api/types-and-structures.rst
|
||||||
|
// - On strides (about reshaping, slicing, C-contagiousness, etc)
|
||||||
|
// - https://ajcr.net/stride-guide-part-1/.
|
||||||
|
// - https://ajcr.net/stride-guide-part-2/.
|
||||||
|
// - https://ajcr.net/stride-guide-part-3/.
|
||||||
|
template <typename SizeT>
|
||||||
|
struct NDArray {
|
||||||
|
// The underlying data this `ndarray` is pointing to.
|
||||||
|
//
|
||||||
|
// NOTE: Formally this should be of type `void *`, but clang
|
||||||
|
// translates `void *` to `i8 *` when run with `-S -emit-llvm`,
|
||||||
|
// so we will put `uint8_t *` here for clarity.
|
||||||
|
uint8_t *data;
|
||||||
|
|
||||||
|
// The number of bytes of a single element in `data`.
|
||||||
|
//
|
||||||
|
// The `SizeT` is treated as `unsigned`.
|
||||||
|
SizeT itemsize;
|
||||||
|
|
||||||
|
// The number of dimensions of this shape.
|
||||||
|
//
|
||||||
|
// The `SizeT` is treated as `unsigned`.
|
||||||
|
SizeT ndims;
|
||||||
|
|
||||||
|
// Array shape, with length equal to `ndims`.
|
||||||
|
//
|
||||||
|
// The `SizeT` is treated as `unsigned`.
|
||||||
|
//
|
||||||
|
// NOTE: `shape` can contain 0.
|
||||||
|
// (those appear when the user makes an out of bounds slice into an ndarray, e.g., `np.zeros((3, 3))[400:].shape == (0, 3)`)
|
||||||
|
SizeT *shape;
|
||||||
|
|
||||||
|
// Array strides (stride value is in number of bytes, NOT number of elements), with length equal to `ndims`.
|
||||||
|
//
|
||||||
|
// The `SizeT` is treated as `signed`.
|
||||||
|
//
|
||||||
|
// NOTE: `strides` can have negative numbers.
|
||||||
|
// (those appear when there is a slice with a negative step, e.g., `my_array[::-1]`)
|
||||||
|
SizeT *strides;
|
||||||
|
|
||||||
|
// Calculate the size/# of elements of an `ndarray`.
|
||||||
|
// This function corresponds to `np.size(<ndarray>)` or `ndarray.size`
|
||||||
|
SizeT size() {
|
||||||
|
return ndarray_util::calc_size_from_shape(ndims, shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate the number of bytes of its content of an `ndarray` *in its view*.
|
||||||
|
// This function corresponds to `ndarray.nbytes`
|
||||||
|
SizeT nbytes() {
|
||||||
|
return this->size() * itemsize;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_value_at_pelement(uint8_t* pelement, const uint8_t* pvalue) {
|
||||||
|
__builtin_memcpy(pelement, pvalue, itemsize);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t* get_pelement(const SizeT *indices) {
|
||||||
|
uint8_t* element = data;
|
||||||
|
for (SizeT dim_i = 0; dim_i < ndims; dim_i++)
|
||||||
|
element += indices[dim_i] * strides[dim_i];
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t* get_nth_pelement(SizeT nth) {
|
||||||
|
irrt_assert(0 <= nth);
|
||||||
|
irrt_assert(nth < this->size());
|
||||||
|
|
||||||
|
SizeT* indices = (SizeT*) __builtin_alloca(sizeof(SizeT) * this->ndims);
|
||||||
|
ndarray_util::set_indices_by_nth(this->ndims, this->shape, indices, nth);
|
||||||
|
return get_pelement(indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get pointer to the first element of this ndarray, assuming
|
||||||
|
// `this->size() > 0`, i.e., not "degenerate" due to zeroes in `this->shape`)
|
||||||
|
//
|
||||||
|
// This is particularly useful for when the ndarray is just containing a single scalar.
|
||||||
|
uint8_t* get_first_pelement() {
|
||||||
|
irrt_assert(this->size() > 0);
|
||||||
|
return this->data; // ...It is simply `this->data`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is the given `indices` valid/in-bounds?
|
||||||
|
bool in_bounds(const SizeT *indices) {
|
||||||
|
for (SizeT dim_i = 0; dim_i < ndims; dim_i++) {
|
||||||
|
bool dim_ok = indices[dim_i] < shape[dim_i];
|
||||||
|
if (!dim_ok) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fill the ndarray with a value
|
||||||
|
void fill_generic(const uint8_t* pvalue) {
|
||||||
|
NDArrayIndicesIter<SizeT> iter;
|
||||||
|
iter.ndims = this->ndims;
|
||||||
|
iter.shape = this->shape;
|
||||||
|
iter.indices = (SizeT*) __builtin_alloca(sizeof(SizeT) * ndims);
|
||||||
|
iter.set_indices_zero();
|
||||||
|
|
||||||
|
for (SizeT i = 0; i < this->size(); i++, iter.next()) {
|
||||||
|
uint8_t* pelement = get_pelement(iter.indices);
|
||||||
|
set_value_at_pelement(pelement, pvalue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the strides of the ndarray with `ndarray_util::set_strides_by_shape`
|
||||||
|
void set_strides_by_shape() {
|
||||||
|
ndarray_util::set_strides_by_shape(itemsize, ndims, strides, shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://numpy.org/doc/stable/reference/generated/numpy.eye.html
|
||||||
|
void set_to_eye(SizeT k, const uint8_t* zero_pvalue, const uint8_t* one_pvalue) {
|
||||||
|
__builtin_assume(ndims == 2);
|
||||||
|
|
||||||
|
// TODO: Better implementation
|
||||||
|
|
||||||
|
fill_generic(zero_pvalue);
|
||||||
|
for (SizeT i = 0; i < min(shape[0], shape[1]); i++) {
|
||||||
|
SizeT row = i;
|
||||||
|
SizeT col = i + k;
|
||||||
|
SizeT indices[2] = { row, col };
|
||||||
|
|
||||||
|
if (!in_bounds(indices)) continue;
|
||||||
|
|
||||||
|
uint8_t* pelement = get_pelement(indices);
|
||||||
|
set_value_at_pelement(pelement, one_pvalue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// To support numpy complex slices (e.g., `my_array[:50:2,4,:2:-1]`)
|
||||||
|
//
|
||||||
|
// Things assumed by this function:
|
||||||
|
// - `dst_ndarray` is allocated by the caller
|
||||||
|
// - `dst_ndarray.ndims` has the correct value (according to `ndarray_util::deduce_ndims_after_slicing`).
|
||||||
|
// - ... and `dst_ndarray.shape` and `dst_ndarray.strides` have been allocated by the caller as well
|
||||||
|
//
|
||||||
|
// Other notes:
|
||||||
|
// - `dst_ndarray->data` does not have to be set, it will be derived.
|
||||||
|
// - `dst_ndarray->itemsize` does not have to be set, it will be set to `this->itemsize`
|
||||||
|
// - `dst_ndarray->shape` and `dst_ndarray.strides` can contain empty values
|
||||||
|
void slice(SizeT num_ndslices, NDSlice* ndslices, NDArray<SizeT>* dst_ndarray) {
|
||||||
|
// REFERENCE CODE (check out `_index_helper` in `__getitem__`):
|
||||||
|
// https://github.com/wadetb/tinynumpy/blob/0d23d22e07062ffab2afa287374c7b366eebdda1/tinynumpy/tinynumpy.py#L652
|
||||||
|
|
||||||
|
irrt_assert(dst_ndarray->ndims == ndarray_util::deduce_ndims_after_slicing(this->ndims, num_ndslices, ndslices));
|
||||||
|
|
||||||
|
dst_ndarray->data = this->data;
|
||||||
|
|
||||||
|
SizeT this_axis = 0;
|
||||||
|
SizeT dst_axis = 0;
|
||||||
|
|
||||||
|
for (SizeT i = 0; i < num_ndslices; i++) {
|
||||||
|
NDSlice *ndslice = &ndslices[i];
|
||||||
|
if (ndslice->type == INPUT_SLICE_TYPE_INDEX) {
|
||||||
|
// Handle when the ndslice is just a single (possibly negative) integer
|
||||||
|
// e.g., `my_array[::2, -5, ::-1]`
|
||||||
|
// ^^------ like this
|
||||||
|
SizeT index_user = *((SizeT*) ndslice->slice);
|
||||||
|
SizeT index = resolve_index_in_length(this->shape[this_axis], index_user);
|
||||||
|
dst_ndarray->data += index * this->strides[this_axis]; // Add offset
|
||||||
|
|
||||||
|
// Next
|
||||||
|
this_axis++;
|
||||||
|
} else if (ndslice->type == INPUT_SLICE_TYPE_SLICE) {
|
||||||
|
// Handle when the ndslice is a slice (represented by UserSlice in IRRT)
|
||||||
|
// e.g., `my_array[::2, -5, ::-1]`
|
||||||
|
// ^^^------^^^^----- like these
|
||||||
|
UserSlice<SizeT>* user_slice = (UserSlice<SizeT>*) ndslice->slice;
|
||||||
|
Slice<SizeT> slice = user_slice->indices(this->shape[this_axis]); // To resolve negative indices and other funny stuff written by the user
|
||||||
|
|
||||||
|
// NOTE: There is no need to write special code to handle negative steps/strides.
|
||||||
|
// This simple implementation meticulously handles both positive and negative steps/strides.
|
||||||
|
// Check out the tinynumpy and IRRT's test cases if you are not convinced.
|
||||||
|
dst_ndarray->data += slice.start * this->strides[this_axis]; // Add offset (NOTE: no need to `* itemsize`, strides count in # of bytes)
|
||||||
|
dst_ndarray->strides[dst_axis] = slice.step * this->strides[this_axis]; // Determine stride
|
||||||
|
dst_ndarray->shape[dst_axis] = slice.len(); // Determine shape dimension
|
||||||
|
|
||||||
|
// Next
|
||||||
|
dst_axis++;
|
||||||
|
this_axis++;
|
||||||
|
} else {
|
||||||
|
__builtin_unreachable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
irrt_assert(dst_axis == dst_ndarray->ndims); // Sanity check on the implementation
|
||||||
|
}
|
||||||
|
|
||||||
|
// Similar to `np.broadcast_to(<ndarray>, <target_shape>)`
|
||||||
|
// Assumptions:
|
||||||
|
// - `this` has to be fully initialized.
|
||||||
|
// - `dst_ndarray->ndims` has to be set.
|
||||||
|
// - `dst_ndarray->shape` has to be set, this determines the shape `this` broadcasts to.
|
||||||
|
//
|
||||||
|
// Other notes:
|
||||||
|
// - `dst_ndarray->data` does not have to be set, it will be set to `this->data`.
|
||||||
|
// - `dst_ndarray->itemsize` does not have to be set, it will be set to `this->data`.
|
||||||
|
// - `dst_ndarray->strides` does not have to be set, it will be overwritten.
|
||||||
|
//
|
||||||
|
// Cautions:
|
||||||
|
// ```
|
||||||
|
// xs = np.zeros((4,))
|
||||||
|
// ys = np.zero((4, 1))
|
||||||
|
// ys[:] = xs # ok
|
||||||
|
//
|
||||||
|
// xs = np.zeros((1, 4))
|
||||||
|
// ys = np.zero((4,))
|
||||||
|
// ys[:] = xs # allowed
|
||||||
|
// # However `np.broadcast_to(xs, (4,))` would fails, as per numpy's broadcasting rule.
|
||||||
|
// # and apparently numpy will "deprecate" this? SEE https://github.com/numpy/numpy/issues/21744
|
||||||
|
// # This implementation will NOT support this assignment.
|
||||||
|
// ```
|
||||||
|
void broadcast_to(NDArray<SizeT>* dst_ndarray) {
|
||||||
|
dst_ndarray->data = this->data;
|
||||||
|
dst_ndarray->itemsize = this->itemsize;
|
||||||
|
|
||||||
|
irrt_assert(
|
||||||
|
ndarray_util::can_broadcast_shape_to(
|
||||||
|
dst_ndarray->ndims,
|
||||||
|
dst_ndarray->shape,
|
||||||
|
this->ndims,
|
||||||
|
this->shape
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
SizeT stride_product = 1;
|
||||||
|
for (SizeT i = 0; i < max(this->ndims, dst_ndarray->ndims); i++) {
|
||||||
|
SizeT this_dim_i = this->ndims - i - 1;
|
||||||
|
SizeT dst_dim_i = dst_ndarray->ndims - i - 1;
|
||||||
|
|
||||||
|
bool this_dim_exists = this_dim_i >= 0;
|
||||||
|
bool dst_dim_exists = dst_dim_i >= 0;
|
||||||
|
|
||||||
|
// TODO: Explain how this works
|
||||||
|
bool c1 = this_dim_exists && this->shape[this_dim_i] == 1;
|
||||||
|
bool c2 = dst_dim_exists && dst_ndarray->shape[dst_dim_i] != 1;
|
||||||
|
if (!this_dim_exists || (c1 && c2)) {
|
||||||
|
dst_ndarray->strides[dst_dim_i] = 0; // Freeze it in-place
|
||||||
|
} else {
|
||||||
|
dst_ndarray->strides[dst_dim_i] = stride_product * this->itemsize;
|
||||||
|
stride_product *= this->shape[this_dim_i]; // NOTE: this_dim_exist must be true here.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simulates `this_ndarray[:] = src_ndarray`, with automatic broadcasting.
|
||||||
|
// Caution on https://github.com/numpy/numpy/issues/21744
|
||||||
|
// Also see `NDArray::broadcast_to`
|
||||||
|
void assign_with(NDArray<SizeT>* src_ndarray) {
|
||||||
|
irrt_assert(
|
||||||
|
ndarray_util::can_broadcast_shape_to(
|
||||||
|
this->ndims,
|
||||||
|
this->shape,
|
||||||
|
src_ndarray->ndims,
|
||||||
|
src_ndarray->shape
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Broadcast the `src_ndarray` to make the reading process *much* easier
|
||||||
|
SizeT* broadcasted_src_ndarray_strides = __builtin_alloca(sizeof(SizeT) * this->ndims); // Remember to allocate strides beforehand
|
||||||
|
NDArray<SizeT> broadcasted_src_ndarray = {
|
||||||
|
.ndims = this->ndims,
|
||||||
|
.shape = this->shape,
|
||||||
|
.strides = broadcasted_src_ndarray_strides
|
||||||
|
};
|
||||||
|
src_ndarray->broadcast_to(&broadcasted_src_ndarray);
|
||||||
|
|
||||||
|
// Using iter instead of `get_nth_pelement` because it is slightly faster
|
||||||
|
SizeT* indices = __builtin_alloca(sizeof(SizeT) * this->ndims);
|
||||||
|
auto iter = NDArrayIndicesIter<SizeT> {
|
||||||
|
.ndims = this->ndims,
|
||||||
|
.shape = this->shape,
|
||||||
|
.indices = indices
|
||||||
|
};
|
||||||
|
const SizeT this_size = this->size();
|
||||||
|
for (SizeT i = 0; i < this_size; i++, iter.next()) {
|
||||||
|
uint8_t* src_pelement = broadcasted_src_ndarray_strides->get_pelement(indices);
|
||||||
|
uint8_t* this_pelement = this->get_pelement(indices);
|
||||||
|
this->set_value_at_pelement(src_pelement, src_pelement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
uint32_t __nac3_ndarray_size(NDArray<int32_t>* ndarray) {
|
||||||
|
return ndarray->size();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t __nac3_ndarray_size64(NDArray<int64_t>* ndarray) {
|
||||||
|
return ndarray->size();
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_fill_generic(NDArray<int32_t>* ndarray, uint8_t* pvalue) {
|
||||||
|
ndarray->fill_generic(pvalue);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_fill_generic64(NDArray<int64_t>* ndarray, uint8_t* pvalue) {
|
||||||
|
ndarray->fill_generic(pvalue);
|
||||||
|
}
|
||||||
|
|
||||||
|
// void __nac3_ndarray_slice(NDArray<int32_t>* ndarray, int32_t num_slices, NDSlice<int32_t> *slices, NDArray<int32_t> *dst_ndarray) {
|
||||||
|
// // ndarray->slice(num_slices, slices, dst_ndarray);
|
||||||
|
// }
|
||||||
|
}
|
|
@ -0,0 +1,80 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "irrt_utils.hpp"
|
||||||
|
#include "irrt_typedefs.hpp"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
// A proper slice in IRRT, all negative indices have be resolved to absolute values.
|
||||||
|
// Even though nac3core's slices are always `int32_t`, we will template slice anyway
|
||||||
|
// since this struct is used as a general utility.
|
||||||
|
template <typename T>
|
||||||
|
struct Slice {
|
||||||
|
T start;
|
||||||
|
T stop;
|
||||||
|
T step;
|
||||||
|
|
||||||
|
// The length/The number of elements of the slice if it were a range,
|
||||||
|
// i.e., the value of `len(range(this->start, this->stop, this->end))`
|
||||||
|
T len() {
|
||||||
|
T diff = stop - start;
|
||||||
|
if (diff > 0 && step > 0) {
|
||||||
|
return ((diff - 1) / step) + 1;
|
||||||
|
} else if (diff < 0 && step < 0) {
|
||||||
|
return ((diff + 1) / step) + 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T resolve_index_in_length(T length, T index) {
|
||||||
|
irrt_assert(length >= 0);
|
||||||
|
if (index < 0) {
|
||||||
|
// Remember that index is negative, so do a plus here
|
||||||
|
return max(length + index, 0);
|
||||||
|
} else {
|
||||||
|
return min(length, index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: using a bitfield for the `*_defined` is better, at the
|
||||||
|
// cost of a more annoying implementation in nac3core inkwell
|
||||||
|
template <typename T>
|
||||||
|
struct UserSlice {
|
||||||
|
uint8_t start_defined;
|
||||||
|
T start;
|
||||||
|
|
||||||
|
uint8_t stop_defined;
|
||||||
|
T stop;
|
||||||
|
|
||||||
|
uint8_t step_defined;
|
||||||
|
T step;
|
||||||
|
|
||||||
|
// Like Python's `slice(start, stop, step).indices(length)`
|
||||||
|
Slice<T> indices(T length) {
|
||||||
|
// NOTE: This function implements Python's `slice.indices` *FAITHFULLY*.
|
||||||
|
// SEE: https://github.com/python/cpython/blob/f62161837e68c1c77961435f1b954412dd5c2b65/Objects/sliceobject.c#L546
|
||||||
|
irrt_assert(length >= 0);
|
||||||
|
irrt_assert(!step_defined || step != 0); // step_defined -> step != 0; step cannot be zero if specified by user
|
||||||
|
|
||||||
|
Slice<T> result;
|
||||||
|
result.step = step_defined ? step : 1;
|
||||||
|
bool step_is_negative = result.step < 0;
|
||||||
|
|
||||||
|
if (start_defined) {
|
||||||
|
result.start = resolve_index_in_length(length, start);
|
||||||
|
} else {
|
||||||
|
result.start = step_is_negative ? length - 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stop_defined) {
|
||||||
|
result.stop = resolve_index_in_length(length, stop);
|
||||||
|
} else {
|
||||||
|
result.stop = step_is_negative ? -1 : length;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
|
@ -0,0 +1,658 @@
|
||||||
|
// This file will be compiled like a real C++ program,
|
||||||
|
// and we do have the luxury to use the standard libraries.
|
||||||
|
// That is if the nix flakes do not have issues... especially on msys2...
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
// Set `IRRT_DONT_TYPEDEF_INTS` because `cstdint` defines them
|
||||||
|
#define IRRT_DONT_TYPEDEF_INTS
|
||||||
|
#include "irrt_everything.hpp"
|
||||||
|
|
||||||
|
void test_fail() {
|
||||||
|
printf("[!] Test failed\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __begin_test(const char* function_name, const char* file, int line) {
|
||||||
|
printf("######### Running %s @ %s:%d\n", function_name, file, line);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define BEGIN_TEST() __begin_test(__FUNCTION__, __FILE__, __LINE__)
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void debug_print_array(const char* format, int len, T* as) {
|
||||||
|
printf("[");
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
if (i != 0) printf(", ");
|
||||||
|
printf(format, as[i]);
|
||||||
|
}
|
||||||
|
printf("]");
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void assert_arrays_match(const char* label, const char* format, int len, T* expected, T* got) {
|
||||||
|
if (!arrays_match(len, expected, got)) {
|
||||||
|
printf(">>>>>>> %s\n", label);
|
||||||
|
printf(" Expecting = ");
|
||||||
|
debug_print_array(format, len, expected);
|
||||||
|
printf("\n");
|
||||||
|
printf(" Got = ");
|
||||||
|
debug_print_array(format, len, got);
|
||||||
|
printf("\n");
|
||||||
|
test_fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void assert_values_match(const char* label, const char* format, T expected, T got) {
|
||||||
|
if (expected != got) {
|
||||||
|
printf(">>>>>>> %s\n", label);
|
||||||
|
printf(" Expecting = ");
|
||||||
|
printf(format, expected);
|
||||||
|
printf("\n");
|
||||||
|
printf(" Got = ");
|
||||||
|
printf(format, got);
|
||||||
|
printf("\n");
|
||||||
|
test_fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_repeated(const char *str, int count) {
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
printf("%s", str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename SizeT, typename ElementT>
|
||||||
|
void __print_ndarray_aux(const char *format, bool first, bool last, SizeT* cursor, SizeT depth, NDArray<SizeT>* ndarray) {
|
||||||
|
// A really lazy recursive implementation
|
||||||
|
|
||||||
|
// Add left padding unless its the first entry (since there would be "[[[" before it)
|
||||||
|
if (!first) {
|
||||||
|
print_repeated(" ", depth);
|
||||||
|
}
|
||||||
|
|
||||||
|
const SizeT dim = ndarray->shape[depth];
|
||||||
|
if (depth + 1 == ndarray->ndims) {
|
||||||
|
// Recursed down to last dimension, print the values in a nice list
|
||||||
|
printf("[");
|
||||||
|
|
||||||
|
SizeT* indices = (SizeT*) __builtin_alloca(sizeof(SizeT) * ndarray->ndims);
|
||||||
|
for (SizeT i = 0; i < dim; i++) {
|
||||||
|
ndarray_util::set_indices_by_nth(ndarray->ndims, ndarray->shape, indices, *cursor);
|
||||||
|
ElementT* pelement = (ElementT*) ndarray->get_pelement(indices);
|
||||||
|
ElementT element = *pelement;
|
||||||
|
|
||||||
|
if (i != 0) printf(", "); // List delimiter
|
||||||
|
printf(format, element);
|
||||||
|
printf("(@");
|
||||||
|
debug_print_array("%d", ndarray->ndims, indices);
|
||||||
|
printf(")");
|
||||||
|
|
||||||
|
(*cursor)++;
|
||||||
|
}
|
||||||
|
printf("]");
|
||||||
|
} else {
|
||||||
|
printf("[");
|
||||||
|
for (SizeT i = 0; i < ndarray->shape[depth]; i++) {
|
||||||
|
__print_ndarray_aux<SizeT, ElementT>(
|
||||||
|
format,
|
||||||
|
i == 0, // first?
|
||||||
|
i + 1 == dim, // last?
|
||||||
|
cursor,
|
||||||
|
depth + 1,
|
||||||
|
ndarray
|
||||||
|
);
|
||||||
|
}
|
||||||
|
printf("]");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add newline unless its the last entry (since there will be "]]]" after it)
|
||||||
|
if (!last) {
|
||||||
|
print_repeated("\n", depth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename SizeT, typename ElementT>
|
||||||
|
void print_ndarray(const char *format, NDArray<SizeT>* ndarray) {
|
||||||
|
if (ndarray->ndims == 0) {
|
||||||
|
printf("<empty ndarray>");
|
||||||
|
} else {
|
||||||
|
SizeT cursor = 0;
|
||||||
|
__print_ndarray_aux<SizeT, ElementT>(format, true, true, &cursor, 0, ndarray);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_calc_size_from_shape_normal() {
|
||||||
|
// Test shapes with normal values
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
int32_t shape[4] = { 2, 3, 5, 7 };
|
||||||
|
assert_values_match("size", "%d", 210, ndarray_util::calc_size_from_shape<int32_t>(4, shape));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_calc_size_from_shape_has_zero() {
|
||||||
|
// Test shapes with 0 in them
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
int32_t shape[4] = { 2, 0, 5, 7 };
|
||||||
|
assert_values_match("size", "%d", 0, ndarray_util::calc_size_from_shape<int32_t>(4, shape));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_set_strides_by_shape() {
|
||||||
|
// Test `set_strides_by_shape()`
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
int32_t shape[4] = { 99, 3, 5, 7 };
|
||||||
|
int32_t strides[4] = { 0 };
|
||||||
|
ndarray_util::set_strides_by_shape((int32_t) sizeof(int32_t), 4, strides, shape);
|
||||||
|
|
||||||
|
int32_t expected_strides[4] = {
|
||||||
|
105 * sizeof(int32_t),
|
||||||
|
35 * sizeof(int32_t),
|
||||||
|
7 * sizeof(int32_t),
|
||||||
|
1 * sizeof(int32_t)
|
||||||
|
};
|
||||||
|
assert_arrays_match("strides", "%u", 4u, expected_strides, strides);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_ndarray_indices_iter_normal() {
|
||||||
|
// Test NDArrayIndicesIter normal behavior
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
int32_t shape[3] = { 1, 2, 3 };
|
||||||
|
int32_t indices[3] = { 0, 0, 0 };
|
||||||
|
auto iter = NDArrayIndicesIter<int32_t> {
|
||||||
|
.ndims = 3,
|
||||||
|
.shape = shape,
|
||||||
|
.indices = indices
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_arrays_match("indices #0", "%u", 3u, iter.indices, (int32_t[3]) { 0, 0, 0 });
|
||||||
|
iter.next();
|
||||||
|
assert_arrays_match("indices #1", "%u", 3u, iter.indices, (int32_t[3]) { 0, 0, 1 });
|
||||||
|
iter.next();
|
||||||
|
assert_arrays_match("indices #2", "%u", 3u, iter.indices, (int32_t[3]) { 0, 0, 2 });
|
||||||
|
iter.next();
|
||||||
|
assert_arrays_match("indices #3", "%u", 3u, iter.indices, (int32_t[3]) { 0, 1, 0 });
|
||||||
|
iter.next();
|
||||||
|
assert_arrays_match("indices #4", "%u", 3u, iter.indices, (int32_t[3]) { 0, 1, 1 });
|
||||||
|
iter.next();
|
||||||
|
assert_arrays_match("indices #5", "%u", 3u, iter.indices, (int32_t[3]) { 0, 1, 2 });
|
||||||
|
iter.next();
|
||||||
|
assert_arrays_match("indices #6", "%u", 3u, iter.indices, (int32_t[3]) { 0, 0, 0 }); // Loops back
|
||||||
|
iter.next();
|
||||||
|
assert_arrays_match("indices #7", "%u", 3u, iter.indices, (int32_t[3]) { 0, 0, 1 });
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_ndarray_fill_generic() {
|
||||||
|
// Test ndarray fill_generic
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
// Choose a type that's neither int32_t nor uint64_t (candidates of SizeT) to spice it up
|
||||||
|
// Also make all the octets non-zero, to see if `memcpy` in `fill_generic` is working perfectly.
|
||||||
|
uint16_t fill_value = 0xFACE;
|
||||||
|
|
||||||
|
uint16_t in_data[6] = { 100, 101, 102, 103, 104, 105 }; // Fill `data` with values that != `999`
|
||||||
|
int32_t in_itemsize = sizeof(uint16_t);
|
||||||
|
const int32_t in_ndims = 2;
|
||||||
|
int32_t in_shape[in_ndims] = { 2, 3 };
|
||||||
|
int32_t in_strides[in_ndims] = {};
|
||||||
|
NDArray<int32_t> ndarray = {
|
||||||
|
.data = (uint8_t*) in_data,
|
||||||
|
.itemsize = in_itemsize,
|
||||||
|
.ndims = in_ndims,
|
||||||
|
.shape = in_shape,
|
||||||
|
.strides = in_strides,
|
||||||
|
};
|
||||||
|
ndarray.set_strides_by_shape();
|
||||||
|
ndarray.fill_generic((uint8_t*) &fill_value); // `fill_generic` here
|
||||||
|
|
||||||
|
uint16_t expected_data[6] = { fill_value, fill_value, fill_value, fill_value, fill_value, fill_value };
|
||||||
|
assert_arrays_match("data", "0x%hX", 6, expected_data, in_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_ndarray_set_to_eye() {
|
||||||
|
// Test `set_to_eye` behavior (helper function to implement `np.eye()`)
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
double in_data[9] = { 99.0, 99.0, 99.0, 99.0, 99.0, 99.0, 99.0, 99.0, 99.0 };
|
||||||
|
int32_t in_itemsize = sizeof(double);
|
||||||
|
const int32_t in_ndims = 2;
|
||||||
|
int32_t in_shape[in_ndims] = { 3, 3 };
|
||||||
|
int32_t in_strides[in_ndims] = {};
|
||||||
|
NDArray<int32_t> ndarray = {
|
||||||
|
.data = (uint8_t*) in_data,
|
||||||
|
.itemsize = in_itemsize,
|
||||||
|
.ndims = in_ndims,
|
||||||
|
.shape = in_shape,
|
||||||
|
.strides = in_strides,
|
||||||
|
};
|
||||||
|
ndarray.set_strides_by_shape();
|
||||||
|
|
||||||
|
double zero = 0.0;
|
||||||
|
double one = 1.0;
|
||||||
|
ndarray.set_to_eye(1, (uint8_t*) &zero, (uint8_t*) &one);
|
||||||
|
|
||||||
|
assert_values_match("in_data[0]", "%f", 0.0, in_data[0]);
|
||||||
|
assert_values_match("in_data[1]", "%f", 1.0, in_data[1]);
|
||||||
|
assert_values_match("in_data[2]", "%f", 0.0, in_data[2]);
|
||||||
|
assert_values_match("in_data[3]", "%f", 0.0, in_data[3]);
|
||||||
|
assert_values_match("in_data[4]", "%f", 0.0, in_data[4]);
|
||||||
|
assert_values_match("in_data[5]", "%f", 1.0, in_data[5]);
|
||||||
|
assert_values_match("in_data[6]", "%f", 0.0, in_data[6]);
|
||||||
|
assert_values_match("in_data[7]", "%f", 0.0, in_data[7]);
|
||||||
|
assert_values_match("in_data[8]", "%f", 0.0, in_data[8]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_slice_1() {
|
||||||
|
// Test `slice(5, None, None).indices(100) == slice(5, 100, 1)`
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
UserSlice<int> user_slice = {
|
||||||
|
.start_defined = 1,
|
||||||
|
.start = 5,
|
||||||
|
.stop_defined = 0,
|
||||||
|
.step_defined = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
auto slice = user_slice.indices(100);
|
||||||
|
assert_values_match("start", "%d", 5, slice.start);
|
||||||
|
assert_values_match("stop", "%d", 100, slice.stop);
|
||||||
|
assert_values_match("step", "%d", 1, slice.step);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_slice_2() {
|
||||||
|
// Test `slice(400, 999, None).indices(100) == slice(100, 100, 1)`
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
UserSlice<int> user_slice = {
|
||||||
|
.start_defined = 1,
|
||||||
|
.start = 400,
|
||||||
|
.stop_defined = 0,
|
||||||
|
.step_defined = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
auto slice = user_slice.indices(100);
|
||||||
|
assert_values_match("start", "%d", 100, slice.start);
|
||||||
|
assert_values_match("stop", "%d", 100, slice.stop);
|
||||||
|
assert_values_match("step", "%d", 1, slice.step);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_slice_3() {
|
||||||
|
// Test `slice(-10, -5, None).indices(100) == slice(90, 95, 1)`
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
UserSlice<int> user_slice = {
|
||||||
|
.start_defined = 1,
|
||||||
|
.start = -10,
|
||||||
|
.stop_defined = 1,
|
||||||
|
.stop = -5,
|
||||||
|
.step_defined = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
auto slice = user_slice.indices(100);
|
||||||
|
assert_values_match("start", "%d", 90, slice.start);
|
||||||
|
assert_values_match("stop", "%d", 95, slice.stop);
|
||||||
|
assert_values_match("step", "%d", 1, slice.step);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_slice_4() {
|
||||||
|
// Test `slice(None, None, -5).indices(100) == (99, -1, -5)`
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
UserSlice<int> user_slice = {
|
||||||
|
.start_defined = 0,
|
||||||
|
.stop_defined = 0,
|
||||||
|
.step_defined = 1,
|
||||||
|
.step = -5
|
||||||
|
};
|
||||||
|
|
||||||
|
auto slice = user_slice.indices(100);
|
||||||
|
assert_values_match("start", "%d", 99, slice.start);
|
||||||
|
assert_values_match("stop", "%d", -1, slice.stop);
|
||||||
|
assert_values_match("step", "%d", -5, slice.step);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_ndslice_1() {
|
||||||
|
/*
|
||||||
|
Reference Python code:
|
||||||
|
```python
|
||||||
|
ndarray = np.arange(12, dtype=np.float64).reshape((3, 4));
|
||||||
|
# array([[ 0., 1., 2., 3.],
|
||||||
|
# [ 4., 5., 6., 7.],
|
||||||
|
# [ 8., 9., 10., 11.]])
|
||||||
|
|
||||||
|
dst_ndarray = ndarray[-2:, 1::2]
|
||||||
|
# array([[ 5., 7.],
|
||||||
|
# [ 9., 11.]])
|
||||||
|
|
||||||
|
assert dst_ndarray.shape == (2, 2)
|
||||||
|
assert dst_ndarray.strides == (32, 16)
|
||||||
|
assert dst_ndarray[0, 0] == 5.0
|
||||||
|
assert dst_ndarray[0, 1] == 7.0
|
||||||
|
assert dst_ndarray[1, 0] == 9.0
|
||||||
|
assert dst_ndarray[1, 1] == 11.0
|
||||||
|
```
|
||||||
|
*/
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
double in_data[12] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0 };
|
||||||
|
int32_t in_itemsize = sizeof(double);
|
||||||
|
const int32_t in_ndims = 2;
|
||||||
|
int32_t in_shape[in_ndims] = { 3, 4 };
|
||||||
|
int32_t in_strides[in_ndims] = {};
|
||||||
|
NDArray<int32_t> ndarray = {
|
||||||
|
.data = (uint8_t*) in_data,
|
||||||
|
.itemsize = in_itemsize,
|
||||||
|
.ndims = in_ndims,
|
||||||
|
.shape = in_shape,
|
||||||
|
.strides = in_strides
|
||||||
|
};
|
||||||
|
ndarray.set_strides_by_shape();
|
||||||
|
|
||||||
|
// Destination ndarray
|
||||||
|
// As documented, ndims and shape & strides must be allocated and determined by the caller.
|
||||||
|
const int32_t dst_ndims = 2;
|
||||||
|
int32_t dst_shape[dst_ndims] = {999, 999}; // Empty values
|
||||||
|
int32_t dst_strides[dst_ndims] = {999, 999}; // Empty values
|
||||||
|
NDArray<int32_t> dst_ndarray = {
|
||||||
|
.data = nullptr,
|
||||||
|
.ndims = dst_ndims,
|
||||||
|
.shape = dst_shape,
|
||||||
|
.strides = dst_strides
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create the slice in `ndarray[-2::, 1::2]`
|
||||||
|
UserSlice<int32_t> user_slice_1 = {
|
||||||
|
.start_defined = 1,
|
||||||
|
.start = -2,
|
||||||
|
.stop_defined = 0,
|
||||||
|
.step_defined = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
UserSlice<int32_t> user_slice_2 = {
|
||||||
|
.start_defined = 1,
|
||||||
|
.start = 1,
|
||||||
|
.stop_defined = 0,
|
||||||
|
.step_defined = 1,
|
||||||
|
.step = 2
|
||||||
|
};
|
||||||
|
|
||||||
|
const int32_t num_ndslices = 2;
|
||||||
|
NDSlice ndslices[num_ndslices] = {
|
||||||
|
{ .type = INPUT_SLICE_TYPE_SLICE, .slice = (uint8_t*) &user_slice_1 },
|
||||||
|
{ .type = INPUT_SLICE_TYPE_SLICE, .slice = (uint8_t*) &user_slice_2 }
|
||||||
|
};
|
||||||
|
|
||||||
|
ndarray.slice(num_ndslices, ndslices, &dst_ndarray);
|
||||||
|
|
||||||
|
int32_t expected_shape[dst_ndims] = { 2, 2 };
|
||||||
|
int32_t expected_strides[dst_ndims] = { 32, 16 };
|
||||||
|
assert_arrays_match("shape", "%d", dst_ndims, expected_shape, dst_ndarray.shape);
|
||||||
|
assert_arrays_match("strides", "%d", dst_ndims, expected_strides, dst_ndarray.strides);
|
||||||
|
|
||||||
|
assert_values_match("dst_ndarray[0, 0]", "%f", 5.0, *((double *) dst_ndarray.get_pelement((int32_t[dst_ndims]) { 0, 0 })));
|
||||||
|
assert_values_match("dst_ndarray[0, 1]", "%f", 7.0, *((double *) dst_ndarray.get_pelement((int32_t[dst_ndims]) { 0, 1 })));
|
||||||
|
assert_values_match("dst_ndarray[1, 0]", "%f", 9.0, *((double *) dst_ndarray.get_pelement((int32_t[dst_ndims]) { 1, 0 })));
|
||||||
|
assert_values_match("dst_ndarray[1, 1]", "%f", 11.0, *((double *) dst_ndarray.get_pelement((int32_t[dst_ndims]) { 1, 1 })));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_ndslice_2() {
|
||||||
|
/*
|
||||||
|
```python
|
||||||
|
ndarray = np.arange(12, dtype=np.float64).reshape((3, 4))
|
||||||
|
# array([[ 0., 1., 2., 3.],
|
||||||
|
# [ 4., 5., 6., 7.],
|
||||||
|
# [ 8., 9., 10., 11.]])
|
||||||
|
|
||||||
|
dst_ndarray = ndarray[2, ::-2]
|
||||||
|
# array([11., 9.])
|
||||||
|
|
||||||
|
assert dst_ndarray.shape == (2,)
|
||||||
|
assert dst_ndarray.strides == (-16,)
|
||||||
|
assert dst_ndarray[0] == 11.0
|
||||||
|
assert dst_ndarray[1] == 9.0
|
||||||
|
|
||||||
|
dst_ndarray[1, 0] == 99 # If you write to `dst_ndarray`
|
||||||
|
assert ndarray[1, 3] == 99 # `ndarray` also updates!!
|
||||||
|
```
|
||||||
|
*/
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
double in_data[12] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0 };
|
||||||
|
int32_t in_itemsize = sizeof(double);
|
||||||
|
const int32_t in_ndims = 2;
|
||||||
|
int32_t in_shape[in_ndims] = { 3, 4 };
|
||||||
|
int32_t in_strides[in_ndims] = {};
|
||||||
|
NDArray<int32_t> ndarray = {
|
||||||
|
.data = (uint8_t*) in_data,
|
||||||
|
.itemsize = in_itemsize,
|
||||||
|
.ndims = in_ndims,
|
||||||
|
.shape = in_shape,
|
||||||
|
.strides = in_strides
|
||||||
|
};
|
||||||
|
ndarray.set_strides_by_shape();
|
||||||
|
|
||||||
|
// Destination ndarray
|
||||||
|
// As documented, ndims and shape & strides must be allocated and determined by the caller.
|
||||||
|
const int32_t dst_ndims = 1;
|
||||||
|
int32_t dst_shape[dst_ndims] = {999}; // Empty values
|
||||||
|
int32_t dst_strides[dst_ndims] = {999}; // Empty values
|
||||||
|
NDArray<int32_t> dst_ndarray = {
|
||||||
|
.data = nullptr,
|
||||||
|
.ndims = dst_ndims,
|
||||||
|
.shape = dst_shape,
|
||||||
|
.strides = dst_strides
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create the slice in `ndarray[2, ::-2]`
|
||||||
|
int32_t user_slice_1 = 2;
|
||||||
|
UserSlice<int32_t> user_slice_2 = {
|
||||||
|
.start_defined = 0,
|
||||||
|
.stop_defined = 0,
|
||||||
|
.step_defined = 1,
|
||||||
|
.step = -2
|
||||||
|
};
|
||||||
|
|
||||||
|
const int32_t num_ndslices = 2;
|
||||||
|
NDSlice ndslices[num_ndslices] = {
|
||||||
|
{ .type = INPUT_SLICE_TYPE_INDEX, .slice = (uint8_t*) &user_slice_1 },
|
||||||
|
{ .type = INPUT_SLICE_TYPE_SLICE, .slice = (uint8_t*) &user_slice_2 }
|
||||||
|
};
|
||||||
|
|
||||||
|
ndarray.slice(num_ndslices, ndslices, &dst_ndarray);
|
||||||
|
|
||||||
|
int32_t expected_shape[dst_ndims] = { 2 };
|
||||||
|
int32_t expected_strides[dst_ndims] = { -16 };
|
||||||
|
assert_arrays_match("shape", "%d", dst_ndims, expected_shape, dst_ndarray.shape);
|
||||||
|
assert_arrays_match("strides", "%d", dst_ndims, expected_strides, dst_ndarray.strides);
|
||||||
|
|
||||||
|
// [5.0, 3.0]
|
||||||
|
assert_values_match("dst_ndarray[0]", "%f", 11.0, *((double *) dst_ndarray.get_pelement((int32_t[dst_ndims]) { 0 })));
|
||||||
|
assert_values_match("dst_ndarray[1]", "%f", 9.0, *((double *) dst_ndarray.get_pelement((int32_t[dst_ndims]) { 1 })));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_can_broadcast_shape() {
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([3], [1, 1, 1, 1, 3]) == true",
|
||||||
|
"%d",
|
||||||
|
true,
|
||||||
|
ndarray_util::can_broadcast_shape_to(1, (int32_t[]) { 3 }, 5, (int32_t[]) { 1, 1, 1, 1, 3 })
|
||||||
|
);
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([3], [3, 1]) == false",
|
||||||
|
"%d",
|
||||||
|
false,
|
||||||
|
ndarray_util::can_broadcast_shape_to(1, (int32_t[]) { 3 }, 2, (int32_t[]) { 3, 1 }));
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([3], [3]) == true",
|
||||||
|
"%d",
|
||||||
|
true,
|
||||||
|
ndarray_util::can_broadcast_shape_to(1, (int32_t[]) { 3 }, 1, (int32_t[]) { 3 }));
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([1], [3]) == false",
|
||||||
|
"%d",
|
||||||
|
false,
|
||||||
|
ndarray_util::can_broadcast_shape_to(1, (int32_t[]) { 1 }, 1, (int32_t[]) { 3 }));
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([1], [1]) == true",
|
||||||
|
"%d",
|
||||||
|
true,
|
||||||
|
ndarray_util::can_broadcast_shape_to(1, (int32_t[]) { 1 }, 1, (int32_t[]) { 1 }));
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([256, 256, 3], [256, 1, 3]) == true",
|
||||||
|
"%d",
|
||||||
|
true,
|
||||||
|
ndarray_util::can_broadcast_shape_to(3, (int32_t[]) { 256, 256, 3 }, 3, (int32_t[]) { 256, 1, 3 })
|
||||||
|
);
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([256, 256, 3], [3]) == true",
|
||||||
|
"%d",
|
||||||
|
true,
|
||||||
|
ndarray_util::can_broadcast_shape_to(3, (int32_t[]) { 256, 256, 3 }, 1, (int32_t[]) { 3 })
|
||||||
|
);
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([256, 256, 3], [2]) == false",
|
||||||
|
"%d",
|
||||||
|
false,
|
||||||
|
ndarray_util::can_broadcast_shape_to(3, (int32_t[]) { 256, 256, 3 }, 1, (int32_t[]) { 2 })
|
||||||
|
);
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([256, 256, 3], [1]) == true",
|
||||||
|
"%d",
|
||||||
|
true,
|
||||||
|
ndarray_util::can_broadcast_shape_to(3, (int32_t[]) { 256, 256, 3 }, 1, (int32_t[]) { 1 })
|
||||||
|
);
|
||||||
|
|
||||||
|
// In cases when the shapes contain zero(es)
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([0], [1]) == true",
|
||||||
|
"%d",
|
||||||
|
true,
|
||||||
|
ndarray_util::can_broadcast_shape_to(1, (int32_t[]) { 0 }, 1, (int32_t[]) { 1 })
|
||||||
|
);
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([0], [2]) == false",
|
||||||
|
"%d",
|
||||||
|
false,
|
||||||
|
ndarray_util::can_broadcast_shape_to(1, (int32_t[]) { 0 }, 1, (int32_t[]) { 2 })
|
||||||
|
);
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([0, 4, 0, 0], [1]) == true",
|
||||||
|
"%d",
|
||||||
|
true,
|
||||||
|
ndarray_util::can_broadcast_shape_to(4, (int32_t[]) { 0, 4, 0, 0 }, 1, (int32_t[]) { 1 })
|
||||||
|
);
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([0, 4, 0, 0], [1, 1, 1, 1]) == true",
|
||||||
|
"%d",
|
||||||
|
true,
|
||||||
|
ndarray_util::can_broadcast_shape_to(4, (int32_t[]) { 0, 4, 0, 0 }, 4, (int32_t[]) { 1, 1, 1, 1 })
|
||||||
|
);
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([0, 4, 0, 0], [1, 4, 1, 1]) == true",
|
||||||
|
"%d",
|
||||||
|
true,
|
||||||
|
ndarray_util::can_broadcast_shape_to(4, (int32_t[]) { 0, 4, 0, 0 }, 4, (int32_t[]) { 1, 4, 1, 1 })
|
||||||
|
);
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([4, 3], [0, 3]) == false",
|
||||||
|
"%d",
|
||||||
|
false,
|
||||||
|
ndarray_util::can_broadcast_shape_to(2, (int32_t[]) { 4, 3 }, 2, (int32_t[]) { 0, 3 })
|
||||||
|
);
|
||||||
|
assert_values_match(
|
||||||
|
"can_broadcast_shape_to([4, 3], [0, 0]) == false",
|
||||||
|
"%d",
|
||||||
|
false,
|
||||||
|
ndarray_util::can_broadcast_shape_to(2, (int32_t[]) { 4, 3 }, 2, (int32_t[]) { 0, 0 })
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_ndarray_broadcast_1() {
|
||||||
|
/*
|
||||||
|
# array = np.array([[19.9, 29.9, 39.9, 49.9]], dtype=np.float64)
|
||||||
|
# >>> [[19.9 29.9 39.9 49.9]]
|
||||||
|
#
|
||||||
|
# array = np.broadcast_to(array, (2, 3, 4))
|
||||||
|
# >>> [[[19.9 29.9 39.9 49.9]
|
||||||
|
# >>> [19.9 29.9 39.9 49.9]
|
||||||
|
# >>> [19.9 29.9 39.9 49.9]]
|
||||||
|
# >>> [[19.9 29.9 39.9 49.9]
|
||||||
|
# >>> [19.9 29.9 39.9 49.9]
|
||||||
|
# >>> [19.9 29.9 39.9 49.9]]]
|
||||||
|
#
|
||||||
|
# assery array.strides == (0, 0, 8)
|
||||||
|
|
||||||
|
*/
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
double in_data[4] = { 19.9, 29.9, 39.9, 49.9 };
|
||||||
|
const int32_t in_ndims = 2;
|
||||||
|
int32_t in_shape[in_ndims] = {1, 4};
|
||||||
|
int32_t in_strides[in_ndims] = {};
|
||||||
|
NDArray<int32_t> ndarray = {
|
||||||
|
.data = (uint8_t*) in_data,
|
||||||
|
.itemsize = sizeof(double),
|
||||||
|
.ndims = in_ndims,
|
||||||
|
.shape = in_shape,
|
||||||
|
.strides = in_strides
|
||||||
|
};
|
||||||
|
ndarray.set_strides_by_shape();
|
||||||
|
|
||||||
|
const int32_t dst_ndims = 3;
|
||||||
|
int32_t dst_shape[dst_ndims] = {2, 3, 4};
|
||||||
|
int32_t dst_strides[dst_ndims] = {};
|
||||||
|
NDArray<int32_t> dst_ndarray = {
|
||||||
|
.ndims = dst_ndims,
|
||||||
|
.shape = dst_shape,
|
||||||
|
.strides = dst_strides
|
||||||
|
};
|
||||||
|
|
||||||
|
ndarray.broadcast_to(&dst_ndarray);
|
||||||
|
|
||||||
|
assert_arrays_match("dst_ndarray->strides", "%d", dst_ndims, (int32_t[]) { 0, 0, 8 }, dst_ndarray.strides);
|
||||||
|
|
||||||
|
assert_values_match("dst_ndarray[0, 0, 0]", "%f", 19.9, *((double*) dst_ndarray.get_pelement((int32_t[]) {0, 0, 0})));
|
||||||
|
assert_values_match("dst_ndarray[0, 0, 1]", "%f", 29.9, *((double*) dst_ndarray.get_pelement((int32_t[]) {0, 0, 1})));
|
||||||
|
assert_values_match("dst_ndarray[0, 0, 2]", "%f", 39.9, *((double*) dst_ndarray.get_pelement((int32_t[]) {0, 0, 2})));
|
||||||
|
assert_values_match("dst_ndarray[0, 0, 3]", "%f", 49.9, *((double*) dst_ndarray.get_pelement((int32_t[]) {0, 0, 3})));
|
||||||
|
assert_values_match("dst_ndarray[0, 1, 0]", "%f", 19.9, *((double*) dst_ndarray.get_pelement((int32_t[]) {0, 1, 0})));
|
||||||
|
assert_values_match("dst_ndarray[0, 1, 1]", "%f", 29.9, *((double*) dst_ndarray.get_pelement((int32_t[]) {0, 1, 1})));
|
||||||
|
assert_values_match("dst_ndarray[0, 1, 2]", "%f", 39.9, *((double*) dst_ndarray.get_pelement((int32_t[]) {0, 1, 2})));
|
||||||
|
assert_values_match("dst_ndarray[0, 1, 3]", "%f", 49.9, *((double*) dst_ndarray.get_pelement((int32_t[]) {0, 1, 3})));
|
||||||
|
assert_values_match("dst_ndarray[1, 2, 3]", "%f", 49.9, *((double*) dst_ndarray.get_pelement((int32_t[]) {1, 2, 3})));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_assign_with() {
|
||||||
|
/*
|
||||||
|
```
|
||||||
|
xs = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], dtype=np.float64)
|
||||||
|
ys = xs.shape
|
||||||
|
```
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
test_calc_size_from_shape_normal();
|
||||||
|
test_calc_size_from_shape_has_zero();
|
||||||
|
test_set_strides_by_shape();
|
||||||
|
test_ndarray_indices_iter_normal();
|
||||||
|
test_ndarray_fill_generic();
|
||||||
|
test_ndarray_set_to_eye();
|
||||||
|
test_slice_1();
|
||||||
|
test_slice_2();
|
||||||
|
test_slice_3();
|
||||||
|
test_slice_4();
|
||||||
|
test_ndslice_1();
|
||||||
|
test_ndslice_2();
|
||||||
|
test_can_broadcast_shape();
|
||||||
|
test_ndarray_broadcast_1();
|
||||||
|
test_assign_with();
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
// This is made toggleable since `irrt_test.cpp` itself would include
|
||||||
|
// headers that define the `int_t` family.
|
||||||
|
#ifndef IRRT_DONT_TYPEDEF_INTS
|
||||||
|
typedef _BitInt(8) int8_t;
|
||||||
|
typedef unsigned _BitInt(8) uint8_t;
|
||||||
|
typedef _BitInt(32) int32_t;
|
||||||
|
typedef unsigned _BitInt(32) uint32_t;
|
||||||
|
typedef _BitInt(64) int64_t;
|
||||||
|
typedef unsigned _BitInt(64) uint64_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef int32_t SliceIndex;
|
|
@ -0,0 +1,37 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "irrt_typedefs.hpp"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
template <typename T>
|
||||||
|
T max(T a, T b) {
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
T min(T a, T b) {
|
||||||
|
return a > b ? b : a;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
bool arrays_match(int len, T *as, T *bs) {
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
if (as[i] != bs[i]) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void irrt_panic() {
|
||||||
|
// Crash the program for now.
|
||||||
|
// TODO: Don't crash the program
|
||||||
|
// ... or at least produce a good message when doing testing IRRT
|
||||||
|
|
||||||
|
uint8_t* death = nullptr;
|
||||||
|
*death = 0; // TODO: address 0 on hardware might be writable?
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Make this a macro and allow it to be toggled on/off (e.g., debug vs release)
|
||||||
|
void irrt_assert(bool condition) {
|
||||||
|
if (!condition) irrt_panic();
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
|
@ -1,8 +1,6 @@
|
||||||
use crate::codegen::{
|
use crate::codegen::{
|
||||||
irrt::{call_ndarray_calc_size, call_ndarray_flatten_index},
|
llvm_intrinsics::call_int_umin, stmt::gen_for_callback_incrementing, CodeGenContext,
|
||||||
llvm_intrinsics::call_int_umin,
|
CodeGenerator,
|
||||||
stmt::gen_for_callback_incrementing,
|
|
||||||
CodeGenContext, CodeGenerator,
|
|
||||||
};
|
};
|
||||||
use inkwell::context::Context;
|
use inkwell::context::Context;
|
||||||
use inkwell::types::{ArrayType, BasicType, StructType};
|
use inkwell::types::{ArrayType, BasicType, StructType};
|
||||||
|
@ -12,6 +10,7 @@ use inkwell::{
|
||||||
values::{BasicValueEnum, IntValue, PointerValue},
|
values::{BasicValueEnum, IntValue, PointerValue},
|
||||||
AddressSpace, IntPredicate,
|
AddressSpace, IntPredicate,
|
||||||
};
|
};
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
/// A LLVM type that is used to represent a non-primitive type in NAC3.
|
/// A LLVM type that is used to represent a non-primitive type in NAC3.
|
||||||
pub trait ProxyType<'ctx>: Into<Self::Base> {
|
pub trait ProxyType<'ctx>: Into<Self::Base> {
|
||||||
|
@ -713,12 +712,25 @@ impl<'ctx> ListValue<'ctx> {
|
||||||
/// If `size` is [None], the size stored in the field of this instance is used instead.
|
/// If `size` is [None], the size stored in the field of this instance is used instead.
|
||||||
pub fn create_data(
|
pub fn create_data(
|
||||||
&self,
|
&self,
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
elem_ty: BasicTypeEnum<'ctx>,
|
elem_ty: BasicTypeEnum<'ctx>,
|
||||||
size: Option<IntValue<'ctx>>,
|
size: Option<IntValue<'ctx>>,
|
||||||
) {
|
) {
|
||||||
let size = size.unwrap_or_else(|| self.load_size(ctx, None));
|
let size = size.unwrap_or_else(|| self.load_size(ctx, None));
|
||||||
self.store_data(ctx, ctx.builder.build_array_alloca(elem_ty, size, "").unwrap());
|
|
||||||
|
let data = ctx
|
||||||
|
.builder
|
||||||
|
.build_select(
|
||||||
|
ctx.builder
|
||||||
|
.build_int_compare(IntPredicate::NE, size, self.llvm_usize.const_zero(), "")
|
||||||
|
.unwrap(),
|
||||||
|
ctx.builder.build_array_alloca(elem_ty, size, "").unwrap(),
|
||||||
|
elem_ty.ptr_type(AddressSpace::default()).const_zero(),
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
.map(BasicValueEnum::into_pointer_value)
|
||||||
|
.unwrap();
|
||||||
|
self.store_data(ctx, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the double-indirection pointer to the `data` array, as if by calling `getelementptr`
|
/// Returns the double-indirection pointer to the `data` array, as if by calling `getelementptr`
|
||||||
|
@ -1588,7 +1600,8 @@ impl<'ctx> ArrayLikeValue<'ctx> for NDArrayDataProxy<'ctx, '_> {
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
generator: &G,
|
generator: &G,
|
||||||
) -> IntValue<'ctx> {
|
) -> IntValue<'ctx> {
|
||||||
call_ndarray_calc_size(generator, ctx, &self.as_slice_value(ctx, generator), (None, None))
|
todo!()
|
||||||
|
// call_ndarray_calc_size(generator, ctx, &self.as_slice_value(ctx, generator), (None, None))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1662,17 +1675,19 @@ impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> ArrayLikeIndexer<'ctx, Index>
|
||||||
indices_elem_ty.get_bit_width()
|
indices_elem_ty.get_bit_width()
|
||||||
);
|
);
|
||||||
|
|
||||||
let index = call_ndarray_flatten_index(generator, ctx, *self.0, indices);
|
todo!()
|
||||||
|
|
||||||
unsafe {
|
// let index = call_ndarray_flatten_index(generator, ctx, *self.0, indices);
|
||||||
ctx.builder
|
|
||||||
.build_in_bounds_gep(
|
// unsafe {
|
||||||
self.base_ptr(ctx, generator),
|
// ctx.builder
|
||||||
&[index],
|
// .build_in_bounds_gep(
|
||||||
name.unwrap_or_default(),
|
// self.base_ptr(ctx, generator),
|
||||||
)
|
// &[index],
|
||||||
.unwrap()
|
// name.unwrap_or_default(),
|
||||||
}
|
// )
|
||||||
|
// .unwrap()
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
||||||
|
@ -1706,7 +1721,7 @@ impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> ArrayLikeIndexer<'ctx, Index>
|
||||||
ctx,
|
ctx,
|
||||||
llvm_usize.const_zero(),
|
llvm_usize.const_zero(),
|
||||||
(len, false),
|
(len, false),
|
||||||
|generator, ctx, i| {
|
|generator, ctx, _, i| {
|
||||||
let (dim_idx, dim_sz) = unsafe {
|
let (dim_idx, dim_sz) = unsafe {
|
||||||
(
|
(
|
||||||
indices.get_unchecked(ctx, generator, &i, None).into_int_value(),
|
indices.get_unchecked(ctx, generator, &i, None).into_int_value(),
|
||||||
|
@ -1748,3 +1763,307 @@ impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> UntypedArrayLikeMutator<'ctx,
|
||||||
for NDArrayDataProxy<'ctx, '_>
|
for NDArrayDataProxy<'ctx, '_>
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct StructField<'ctx> {
|
||||||
|
/// The GEP index of this struct field.
|
||||||
|
pub gep_index: u32,
|
||||||
|
/// Name of this struct field.
|
||||||
|
///
|
||||||
|
/// Used for generating names.
|
||||||
|
pub name: &'static str,
|
||||||
|
/// The type of this struct field.
|
||||||
|
pub ty: BasicTypeEnum<'ctx>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct StructFields<'ctx> {
|
||||||
|
/// Name of the struct.
|
||||||
|
///
|
||||||
|
/// Used for generating names.
|
||||||
|
pub name: &'static str,
|
||||||
|
|
||||||
|
/// All the [`StructField`]s of this struct.
|
||||||
|
///
|
||||||
|
/// **NOTE:** The index position of a [`StructField`]
|
||||||
|
/// matches the element's [`StructField::index`].
|
||||||
|
pub fields: Vec<StructField<'ctx>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct StructFieldsBuilder<'ctx> {
|
||||||
|
gep_index_counter: u32,
|
||||||
|
/// Name of the struct to be built.
|
||||||
|
name: &'static str,
|
||||||
|
fields: Vec<StructField<'ctx>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> StructField<'ctx> {
|
||||||
|
pub fn gep(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
ptr: PointerValue<'ctx>,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
|
ctx.builder.build_struct_gep(ptr, self.gep_index, self.name).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
ptr: PointerValue<'ctx>,
|
||||||
|
) -> BasicValueEnum<'ctx> {
|
||||||
|
ctx.builder.build_load(self.gep(ctx, ptr), self.name).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn store<V>(&self, ctx: &CodeGenContext<'ctx, '_>, ptr: PointerValue<'ctx>, value: V)
|
||||||
|
where
|
||||||
|
V: BasicValue<'ctx>,
|
||||||
|
{
|
||||||
|
ctx.builder.build_store(ptr, value).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type IsInstanceError = String;
|
||||||
|
type IsInstanceResult = Result<(), IsInstanceError>;
|
||||||
|
|
||||||
|
pub fn check_basic_types_match<'ctx, A, B>(expected: A, got: B) -> IsInstanceResult
|
||||||
|
where
|
||||||
|
A: BasicType<'ctx>,
|
||||||
|
B: BasicType<'ctx>,
|
||||||
|
{
|
||||||
|
let expected = expected.as_basic_type_enum();
|
||||||
|
let got = got.as_basic_type_enum();
|
||||||
|
|
||||||
|
// Put those logic into here,
|
||||||
|
// otherwise there is always a fallback reporting on any kind of mismatch
|
||||||
|
match (expected, got) {
|
||||||
|
(BasicTypeEnum::IntType(expected), BasicTypeEnum::IntType(got)) => {
|
||||||
|
if expected.get_bit_width() != got.get_bit_width() {
|
||||||
|
return Err(format!(
|
||||||
|
"Expected IntType ({expected}-bit(s)), got IntType ({got}-bit(s))"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(expected, got) => {
|
||||||
|
if expected != got {
|
||||||
|
return Err(format!("Expected {expected}, got {got}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> StructFields<'ctx> {
|
||||||
|
pub fn num_fields(&self) -> u32 {
|
||||||
|
self.fields.len() as u32
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_struct_type(&self, ctx: &'ctx Context) -> StructType<'ctx> {
|
||||||
|
let llvm_fields = self.fields.iter().map(|field| field.ty).collect_vec();
|
||||||
|
ctx.struct_type(llvm_fields.as_slice(), false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_type(&self, scrutinee: StructType<'ctx>) -> IsInstanceResult {
|
||||||
|
// Check scrutinee's number of struct fields
|
||||||
|
if scrutinee.count_fields() != self.num_fields() {
|
||||||
|
return Err(format!(
|
||||||
|
"Expected {expected_count} field(s) in `{struct_name}` type, got {got_count}",
|
||||||
|
struct_name = self.name,
|
||||||
|
expected_count = self.num_fields(),
|
||||||
|
got_count = scrutinee.count_fields(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the scrutinee's field types
|
||||||
|
for field in self.fields.iter() {
|
||||||
|
let expected_field_ty = field.ty;
|
||||||
|
let got_field_ty = scrutinee.get_field_type_at_index(field.gep_index).unwrap();
|
||||||
|
|
||||||
|
if let Err(field_err) = check_basic_types_match(expected_field_ty, got_field_ty) {
|
||||||
|
return Err(format!(
|
||||||
|
"Field GEP index {gep_index} does not match the expected type of ({struct_name}::{field_name}): {field_err}",
|
||||||
|
gep_index = field.gep_index,
|
||||||
|
struct_name = self.name,
|
||||||
|
field_name = field.name,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Done
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> StructFieldsBuilder<'ctx> {
|
||||||
|
fn start(name: &'static str) -> Self {
|
||||||
|
StructFieldsBuilder { gep_index_counter: 0, name, fields: Vec::new() }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_field(&mut self, name: &'static str, ty: BasicTypeEnum<'ctx>) -> StructField<'ctx> {
|
||||||
|
let index = self.gep_index_counter;
|
||||||
|
self.gep_index_counter += 1;
|
||||||
|
StructField { gep_index: index, name, ty }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> StructFields<'ctx> {
|
||||||
|
StructFields { name: self.name, fields: self.fields }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct NpArrayType<'ctx> {
|
||||||
|
pub size_type: IntType<'ctx>,
|
||||||
|
pub elem_type: BasicTypeEnum<'ctx>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct NpArrayStructFields<'ctx> {
|
||||||
|
pub whole_struct: StructFields<'ctx>,
|
||||||
|
pub data: StructField<'ctx>,
|
||||||
|
pub itemsize: StructField<'ctx>,
|
||||||
|
pub ndims: StructField<'ctx>,
|
||||||
|
pub shape: StructField<'ctx>,
|
||||||
|
pub strides: StructField<'ctx>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> NpArrayType<'ctx> {
|
||||||
|
pub fn new_opaque_elem(
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
size_type: IntType<'ctx>,
|
||||||
|
) -> NpArrayType<'ctx> {
|
||||||
|
NpArrayType { size_type, elem_type: ctx.ctx.i8_type().as_basic_type_enum() }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn struct_type(&self, ctx: &CodeGenContext<'ctx, '_>) -> StructType<'ctx> {
|
||||||
|
self.fields().whole_struct.as_struct_type(ctx.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fields(&self) -> NpArrayStructFields<'ctx> {
|
||||||
|
let mut builder = StructFieldsBuilder::start("NpArray");
|
||||||
|
|
||||||
|
let addrspace = AddressSpace::default();
|
||||||
|
|
||||||
|
let byte_type = self.size_type.get_context().i8_type();
|
||||||
|
|
||||||
|
// Make sure the struct matches PERFECTLY with that defined in `nac3core/irrt`.
|
||||||
|
let data = builder.add_field("data", byte_type.ptr_type(addrspace).into());
|
||||||
|
let itemsize = builder.add_field("itemsize", self.size_type.into());
|
||||||
|
let ndims = builder.add_field("ndims", self.size_type.into());
|
||||||
|
let shape = builder.add_field("shape", self.size_type.ptr_type(addrspace).into());
|
||||||
|
let strides = builder.add_field("strides", self.size_type.ptr_type(addrspace).into());
|
||||||
|
|
||||||
|
NpArrayStructFields { whole_struct: builder.end(), data, itemsize, ndims, shape, strides }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Allocate an `ndarray` on stack, with the following notes:
|
||||||
|
///
|
||||||
|
/// - `ndarray.ndims` will be initialized to `in_ndims`.
|
||||||
|
/// - `ndarray.itemsize` will be initialized to the size of `self.elem_type.size_of()`.
|
||||||
|
/// - `ndarray.shape` and `ndarray.strides` will be allocated on the stack with number of elements being `in_ndims`,
|
||||||
|
/// all with empty/uninitialized values.
|
||||||
|
pub fn alloca(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
in_ndims: IntValue<'ctx>,
|
||||||
|
name: &str,
|
||||||
|
) -> NpArrayValue<'ctx> {
|
||||||
|
let fields = self.fields();
|
||||||
|
let ptr =
|
||||||
|
ctx.builder.build_alloca(fields.whole_struct.as_struct_type(ctx.ctx), name).unwrap();
|
||||||
|
|
||||||
|
// Allocate `in_dims` number of `size_type` on the stack for `shape` and `strides`
|
||||||
|
let allocated_shape =
|
||||||
|
ctx.builder.build_array_alloca(fields.shape.ty, in_ndims, "allocated_shape").unwrap();
|
||||||
|
let allocated_strides = ctx
|
||||||
|
.builder
|
||||||
|
.build_array_alloca(fields.strides.ty, in_ndims, "allocated_strides")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let value = NpArrayValue { ty: *self, ptr };
|
||||||
|
value.store_ndims(ctx, in_ndims);
|
||||||
|
value.store_itemsize(ctx, self.elem_type.size_of().unwrap());
|
||||||
|
value.store_shape(ctx, allocated_shape);
|
||||||
|
value.store_strides(ctx, allocated_strides);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct NpArrayValue<'ctx> {
|
||||||
|
pub ty: NpArrayType<'ctx>,
|
||||||
|
pub ptr: PointerValue<'ctx>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> NpArrayValue<'ctx> {
|
||||||
|
pub fn load_ndims(&self, ctx: &CodeGenContext<'ctx, '_>) -> IntValue<'ctx> {
|
||||||
|
let field = self.ty.fields().ndims;
|
||||||
|
field.load(ctx, self.ptr).into_int_value()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn store_ndims(&self, ctx: &CodeGenContext<'ctx, '_>, value: IntValue<'ctx>) {
|
||||||
|
let field = self.ty.fields().ndims;
|
||||||
|
field.store(ctx, self.ptr, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_itemsize(&self, ctx: &CodeGenContext<'ctx, '_>) -> IntValue<'ctx> {
|
||||||
|
let field = self.ty.fields().itemsize;
|
||||||
|
field.load(ctx, self.ptr).into_int_value()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn store_itemsize(&self, ctx: &CodeGenContext<'ctx, '_>, value: IntValue<'ctx>) {
|
||||||
|
let field = self.ty.fields().itemsize;
|
||||||
|
field.store(ctx, self.ptr, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_shape(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
||||||
|
let field = self.ty.fields().shape;
|
||||||
|
field.load(ctx, self.ptr).into_pointer_value()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn store_shape(&self, ctx: &CodeGenContext<'ctx, '_>, value: PointerValue<'ctx>) {
|
||||||
|
let field = self.ty.fields().shape;
|
||||||
|
field.store(ctx, self.ptr, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_strides(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
||||||
|
let field = self.ty.fields().strides;
|
||||||
|
field.load(ctx, self.ptr).into_pointer_value()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn store_strides(&self, ctx: &CodeGenContext<'ctx, '_>, value: PointerValue<'ctx>) {
|
||||||
|
let field = self.ty.fields().strides;
|
||||||
|
field.store(ctx, self.ptr, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// TODO: DOCUMENT ME -- NDIMS WOULD NEVER CHANGE!!!!!
|
||||||
|
pub fn shape_slice(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
) -> TypedArrayLikeAdapter<'ctx, IntValue<'ctx>> {
|
||||||
|
let field = self.ty.fields().shape;
|
||||||
|
field.gep(ctx, self.ptr);
|
||||||
|
|
||||||
|
let ndims = self.load_ndims(ctx);
|
||||||
|
|
||||||
|
TypedArrayLikeAdapter {
|
||||||
|
adapted: ArraySliceValue(self.ptr, ndims, Some(field.name)),
|
||||||
|
downcast_fn: Box::new(|_ctx, x| x.into_int_value()),
|
||||||
|
upcast_fn: Box::new(|_ctx, x| x.as_basic_value_enum()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// TODO: DOCUMENT ME -- NDIMS WOULD NEVER CHANGE!!!!!
|
||||||
|
pub fn strides_slice(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
) -> TypedArrayLikeAdapter<'ctx, IntValue<'ctx>> {
|
||||||
|
let field = self.ty.fields().strides;
|
||||||
|
field.gep(ctx, self.ptr);
|
||||||
|
|
||||||
|
let ndims = self.load_ndims(ctx);
|
||||||
|
|
||||||
|
TypedArrayLikeAdapter {
|
||||||
|
adapted: ArraySliceValue(self.ptr, ndims, Some(field.name)),
|
||||||
|
downcast_fn: Box::new(|_ctx, x| x.into_int_value()),
|
||||||
|
upcast_fn: Box::new(|_ctx, x| x.as_basic_value_enum()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -47,9 +47,6 @@ pub enum ConcreteTypeEnum {
|
||||||
TTuple {
|
TTuple {
|
||||||
ty: Vec<ConcreteType>,
|
ty: Vec<ConcreteType>,
|
||||||
},
|
},
|
||||||
TList {
|
|
||||||
ty: ConcreteType,
|
|
||||||
},
|
|
||||||
TObj {
|
TObj {
|
||||||
obj_id: DefinitionId,
|
obj_id: DefinitionId,
|
||||||
fields: HashMap<StrRef, (ConcreteType, bool)>,
|
fields: HashMap<StrRef, (ConcreteType, bool)>,
|
||||||
|
@ -167,9 +164,6 @@ impl ConcreteTypeStore {
|
||||||
.map(|t| self.from_unifier_type(unifier, primitives, *t, cache))
|
.map(|t| self.from_unifier_type(unifier, primitives, *t, cache))
|
||||||
.collect(),
|
.collect(),
|
||||||
},
|
},
|
||||||
TypeEnum::TList { ty } => ConcreteTypeEnum::TList {
|
|
||||||
ty: self.from_unifier_type(unifier, primitives, *ty, cache),
|
|
||||||
},
|
|
||||||
TypeEnum::TObj { obj_id, fields, params } => ConcreteTypeEnum::TObj {
|
TypeEnum::TObj { obj_id, fields, params } => ConcreteTypeEnum::TObj {
|
||||||
obj_id: *obj_id,
|
obj_id: *obj_id,
|
||||||
fields: fields
|
fields: fields
|
||||||
|
@ -260,9 +254,6 @@ impl ConcreteTypeStore {
|
||||||
.map(|cty| self.to_unifier_type(unifier, primitives, *cty, cache))
|
.map(|cty| self.to_unifier_type(unifier, primitives, *cty, cache))
|
||||||
.collect(),
|
.collect(),
|
||||||
},
|
},
|
||||||
ConcreteTypeEnum::TList { ty } => {
|
|
||||||
TypeEnum::TList { ty: self.to_unifier_type(unifier, primitives, *ty, cache) }
|
|
||||||
}
|
|
||||||
ConcreteTypeEnum::TVirtual { ty } => {
|
ConcreteTypeEnum::TVirtual { ty } => {
|
||||||
TypeEnum::TVirtual { ty: self.to_unifier_type(unifier, primitives, *ty, cache) }
|
TypeEnum::TVirtual { ty: self.to_unifier_type(unifier, primitives, *ty, cache) }
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -4,514 +4,97 @@ use itertools::Either;
|
||||||
|
|
||||||
use crate::codegen::CodeGenContext;
|
use crate::codegen::CodeGenContext;
|
||||||
|
|
||||||
/// Invokes the [`tan`](https://en.cppreference.com/w/c/numeric/math/tan) function.
|
/// Macro to generate extern function
|
||||||
pub fn call_tan<'ctx>(
|
/// Both function return type and function parameter type are `FloatValue`
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
///
|
||||||
arg: FloatValue<'ctx>,
|
/// Arguments:
|
||||||
name: Option<&str>,
|
/// * `unary/binary`: Whether the extern function requires one (unary) or two (binary) operands
|
||||||
) -> FloatValue<'ctx> {
|
/// * `$fn_name:ident`: The identifier of the rust function to be generated
|
||||||
const FN_NAME: &str = "tan";
|
/// * `$extern_fn:literal`: Name of underlying extern function
|
||||||
|
///
|
||||||
|
/// Optional Arguments:
|
||||||
|
/// * `$(,$attributes:literal)*)`: Attributes linked with the extern function
|
||||||
|
/// The default attributes are "mustprogress", "nofree", "nounwind", "willreturn", and "writeonly"
|
||||||
|
/// These will be used unless other attributes are specified
|
||||||
|
/// * `$(,$args:ident)*`: Operands of the extern function
|
||||||
|
/// The data type of these operands will be set to `FloatValue`
|
||||||
|
///
|
||||||
|
macro_rules! generate_extern_fn {
|
||||||
|
("unary", $fn_name:ident, $extern_fn:literal) => {
|
||||||
|
generate_extern_fn!($fn_name, $extern_fn, arg, "mustprogress", "nofree", "nounwind", "willreturn", "writeonly");
|
||||||
|
};
|
||||||
|
("unary", $fn_name:ident, $extern_fn:literal $(,$attributes:literal)*) => {
|
||||||
|
generate_extern_fn!($fn_name, $extern_fn, arg $(,$attributes)*);
|
||||||
|
};
|
||||||
|
("binary", $fn_name:ident, $extern_fn:literal) => {
|
||||||
|
generate_extern_fn!($fn_name, $extern_fn, arg1, arg2, "mustprogress", "nofree", "nounwind", "willreturn", "writeonly");
|
||||||
|
};
|
||||||
|
("binary", $fn_name:ident, $extern_fn:literal $(,$attributes:literal)*) => {
|
||||||
|
generate_extern_fn!($fn_name, $extern_fn, arg1, arg2 $(,$attributes)*);
|
||||||
|
};
|
||||||
|
($fn_name:ident, $extern_fn:literal $(,$args:ident)* $(,$attributes:literal)*) => {
|
||||||
|
#[doc = concat!("Invokes the [`", stringify!($extern_fn), "`](https://en.cppreference.com/w/c/numeric/math/", stringify!($llvm_name), ") function." )]
|
||||||
|
pub fn $fn_name<'ctx>(
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>
|
||||||
|
$(,$args: FloatValue<'ctx>)*,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> FloatValue<'ctx> {
|
||||||
|
const FN_NAME: &str = $extern_fn;
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
let llvm_f64 = ctx.ctx.f64_type();
|
||||||
debug_assert_eq!(arg.get_type(), llvm_f64);
|
$(debug_assert_eq!($args.get_type(), llvm_f64);)*
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
|
let fn_type = llvm_f64.fn_type(&[$($args.get_type().into()),*], false);
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
||||||
for attr in ["mustprogress", "nofree", "nounwind", "willreturn", "writeonly"] {
|
for attr in [$($attributes),*] {
|
||||||
func.add_attribute(
|
func.add_attribute(
|
||||||
AttributeLoc::Function,
|
AttributeLoc::Function,
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0),
|
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
func
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.builder
|
||||||
|
.build_call(extern_fn, &[$($args.into()),*], name.unwrap_or_default())
|
||||||
|
.map(CallSiteValue::try_as_basic_value)
|
||||||
|
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
||||||
|
.map(Either::unwrap_left)
|
||||||
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
};
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[arg.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Invokes the [`asin`](https://en.cppreference.com/w/c/numeric/math/asin) function.
|
generate_extern_fn!("unary", call_tan, "tan");
|
||||||
pub fn call_asin<'ctx>(
|
generate_extern_fn!("unary", call_asin, "asin");
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
generate_extern_fn!("unary", call_acos, "acos");
|
||||||
arg: FloatValue<'ctx>,
|
generate_extern_fn!("unary", call_atan, "atan");
|
||||||
name: Option<&str>,
|
generate_extern_fn!("unary", call_sinh, "sinh");
|
||||||
) -> FloatValue<'ctx> {
|
generate_extern_fn!("unary", call_cosh, "cosh");
|
||||||
const FN_NAME: &str = "asin";
|
generate_extern_fn!("unary", call_tanh, "tanh");
|
||||||
|
generate_extern_fn!("unary", call_asinh, "asinh");
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
generate_extern_fn!("unary", call_acosh, "acosh");
|
||||||
debug_assert_eq!(arg.get_type(), llvm_f64);
|
generate_extern_fn!("unary", call_atanh, "atanh");
|
||||||
|
generate_extern_fn!("unary", call_expm1, "expm1");
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
generate_extern_fn!(
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
|
"unary",
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
call_cbrt,
|
||||||
for attr in ["mustprogress", "nofree", "nounwind", "willreturn", "writeonly"] {
|
"cbrt",
|
||||||
func.add_attribute(
|
"mustprogress",
|
||||||
AttributeLoc::Function,
|
"nofree",
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0),
|
"nosync",
|
||||||
);
|
"nounwind",
|
||||||
}
|
"readonly",
|
||||||
|
"willreturn"
|
||||||
func
|
);
|
||||||
});
|
generate_extern_fn!("unary", call_erf, "erf", "nounwind");
|
||||||
|
generate_extern_fn!("unary", call_erfc, "erfc", "nounwind");
|
||||||
ctx.builder
|
generate_extern_fn!("unary", call_j1, "j1", "nounwind");
|
||||||
.build_call(extern_fn, &[arg.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
generate_extern_fn!("binary", call_atan2, "atan2");
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
generate_extern_fn!("binary", call_hypot, "hypot", "nounwind");
|
||||||
.map(Either::unwrap_left)
|
generate_extern_fn!("binary", call_nextafter, "nextafter", "nounwind");
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`acos`](https://en.cppreference.com/w/c/numeric/math/acos) function.
|
|
||||||
pub fn call_acos<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
arg: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "acos";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(arg.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
for attr in ["mustprogress", "nofree", "nounwind", "willreturn", "writeonly"] {
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[arg.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`atan`](https://en.cppreference.com/w/c/numeric/math/atan) function.
|
|
||||||
pub fn call_atan<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
arg: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "atan";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(arg.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
for attr in ["mustprogress", "nofree", "nounwind", "willreturn", "writeonly"] {
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[arg.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`sinh`](https://en.cppreference.com/w/c/numeric/math/sinh) function.
|
|
||||||
pub fn call_sinh<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
arg: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "sinh";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(arg.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
for attr in ["mustprogress", "nofree", "nounwind", "willreturn", "writeonly"] {
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[arg.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`cosh`](https://en.cppreference.com/w/c/numeric/math/cosh) function.
|
|
||||||
pub fn call_cosh<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
arg: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "cosh";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(arg.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
for attr in ["mustprogress", "nofree", "nounwind", "willreturn", "writeonly"] {
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[arg.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`tanh`](https://en.cppreference.com/w/c/numeric/math/tanh) function.
|
|
||||||
pub fn call_tanh<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
arg: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "tanh";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(arg.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
for attr in ["mustprogress", "nofree", "nounwind", "willreturn", "writeonly"] {
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[arg.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`asinh`](https://en.cppreference.com/w/c/numeric/math/asinh) function.
|
|
||||||
pub fn call_asinh<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
arg: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "asinh";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(arg.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
for attr in ["mustprogress", "nofree", "nounwind", "willreturn", "writeonly"] {
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[arg.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`acosh`](https://en.cppreference.com/w/c/numeric/math/acosh) function.
|
|
||||||
pub fn call_acosh<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
arg: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "acosh";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(arg.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
for attr in ["mustprogress", "nofree", "nounwind", "willreturn", "writeonly"] {
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[arg.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`atanh`](https://en.cppreference.com/w/c/numeric/math/atanh) function.
|
|
||||||
pub fn call_atanh<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
arg: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "atanh";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(arg.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
for attr in ["mustprogress", "nofree", "nounwind", "willreturn", "writeonly"] {
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[arg.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`expm1`](https://en.cppreference.com/w/c/numeric/math/expm1) function.
|
|
||||||
pub fn call_expm1<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
arg: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "expm1";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(arg.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
for attr in ["mustprogress", "nofree", "nounwind", "willreturn", "writeonly"] {
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[arg.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`cbrt`](https://en.cppreference.com/w/c/numeric/math/cbrt) function.
|
|
||||||
pub fn call_cbrt<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
arg: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "cbrt";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(arg.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
for attr in ["mustprogress", "nofree", "nosync", "nounwind", "readonly", "willreturn"] {
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[arg.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`erf`](https://en.cppreference.com/w/c/numeric/math/erf) function.
|
|
||||||
pub fn call_erf<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
arg: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "erf";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(arg.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id("nounwind"), 0),
|
|
||||||
);
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[arg.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`erfc`](https://en.cppreference.com/w/c/numeric/math/erfc) function.
|
|
||||||
pub fn call_erfc<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
arg: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "erfc";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(arg.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id("nounwind"), 0),
|
|
||||||
);
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[arg.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`j1`](https://www.gnu.org/software/libc/manual/html_node/Special-Functions.html#index-j1)
|
|
||||||
/// function.
|
|
||||||
pub fn call_j1<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
arg: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "j1";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(arg.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id("nounwind"), 0),
|
|
||||||
);
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[arg.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`atan2`](https://en.cppreference.com/w/c/numeric/math/atan2) function.
|
|
||||||
pub fn call_atan2<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
y: FloatValue<'ctx>,
|
|
||||||
x: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "atan2";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(y.get_type(), llvm_f64);
|
|
||||||
debug_assert_eq!(x.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into(), llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
for attr in ["mustprogress", "nofree", "nounwind", "willreturn", "writeonly"] {
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[y.into(), x.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`ldexp`](https://en.cppreference.com/w/c/numeric/math/ldexp) function.
|
/// Invokes the [`ldexp`](https://en.cppreference.com/w/c/numeric/math/ldexp) function.
|
||||||
pub fn call_ldexp<'ctx>(
|
pub fn call_ldexp<'ctx>(
|
||||||
|
@ -547,67 +130,3 @@ pub fn call_ldexp<'ctx>(
|
||||||
.map(Either::unwrap_left)
|
.map(Either::unwrap_left)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Invokes the [`hypot`](https://en.cppreference.com/w/c/numeric/math/hypot) function.
|
|
||||||
pub fn call_hypot<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
x: FloatValue<'ctx>,
|
|
||||||
y: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "hypot";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(x.get_type(), llvm_f64);
|
|
||||||
debug_assert_eq!(y.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into(), llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id("nounwind"), 0),
|
|
||||||
);
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[x.into(), y.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`nextafter`](https://en.cppreference.com/w/c/numeric/math/nextafter) function.
|
|
||||||
pub fn call_nextafter<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
from: FloatValue<'ctx>,
|
|
||||||
to: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "nextafter";
|
|
||||||
|
|
||||||
let llvm_f64 = ctx.ctx.f64_type();
|
|
||||||
debug_assert_eq!(from.get_type(), llvm_f64);
|
|
||||||
debug_assert_eq!(to.get_type(), llvm_f64);
|
|
||||||
|
|
||||||
let extern_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_f64.fn_type(&[llvm_f64.into(), llvm_f64.into()], false);
|
|
||||||
let func = ctx.module.add_function(FN_NAME, fn_type, None);
|
|
||||||
func.add_attribute(
|
|
||||||
AttributeLoc::Function,
|
|
||||||
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id("nounwind"), 0),
|
|
||||||
);
|
|
||||||
|
|
||||||
func
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(extern_fn, &[from.into(), to.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,389 +0,0 @@
|
||||||
typedef _BitInt(8) int8_t;
|
|
||||||
typedef unsigned _BitInt(8) uint8_t;
|
|
||||||
typedef _BitInt(32) int32_t;
|
|
||||||
typedef unsigned _BitInt(32) uint32_t;
|
|
||||||
typedef _BitInt(64) int64_t;
|
|
||||||
typedef unsigned _BitInt(64) uint64_t;
|
|
||||||
|
|
||||||
# define MAX(a, b) (a > b ? a : b)
|
|
||||||
# define MIN(a, b) (a > b ? b : a)
|
|
||||||
|
|
||||||
# define NULL ((void *) 0)
|
|
||||||
|
|
||||||
// adapted from GNU Scientific Library: https://git.savannah.gnu.org/cgit/gsl.git/tree/sys/pow_int.c
|
|
||||||
// need to make sure `exp >= 0` before calling this function
|
|
||||||
#define DEF_INT_EXP(T) T __nac3_int_exp_##T( \
|
|
||||||
T base, \
|
|
||||||
T exp \
|
|
||||||
) { \
|
|
||||||
T res = (T)1; \
|
|
||||||
/* repeated squaring method */ \
|
|
||||||
do { \
|
|
||||||
if (exp & 1) res *= base; /* for n odd */ \
|
|
||||||
exp >>= 1; \
|
|
||||||
base *= base; \
|
|
||||||
} while (exp); \
|
|
||||||
return res; \
|
|
||||||
} \
|
|
||||||
|
|
||||||
DEF_INT_EXP(int32_t)
|
|
||||||
DEF_INT_EXP(int64_t)
|
|
||||||
DEF_INT_EXP(uint32_t)
|
|
||||||
DEF_INT_EXP(uint64_t)
|
|
||||||
|
|
||||||
|
|
||||||
int32_t __nac3_slice_index_bound(int32_t i, const int32_t len) {
|
|
||||||
if (i < 0) {
|
|
||||||
i = len + i;
|
|
||||||
}
|
|
||||||
if (i < 0) {
|
|
||||||
return 0;
|
|
||||||
} else if (i > len) {
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t __nac3_range_slice_len(const int32_t start, const int32_t end, const int32_t step) {
|
|
||||||
int32_t diff = end - start;
|
|
||||||
if (diff > 0 && step > 0) {
|
|
||||||
return ((diff - 1) / step) + 1;
|
|
||||||
} else if (diff < 0 && step < 0) {
|
|
||||||
return ((diff + 1) / step) + 1;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle list assignment and dropping part of the list when
|
|
||||||
// both dest_step and src_step are +1.
|
|
||||||
// - All the index must *not* be out-of-bound or negative,
|
|
||||||
// - The end index is *inclusive*,
|
|
||||||
// - The length of src and dest slice size should already
|
|
||||||
// be checked: if dest.step == 1 then len(src) <= len(dest) else len(src) == len(dest)
|
|
||||||
int32_t __nac3_list_slice_assign_var_size(
|
|
||||||
int32_t dest_start,
|
|
||||||
int32_t dest_end,
|
|
||||||
int32_t dest_step,
|
|
||||||
uint8_t *dest_arr,
|
|
||||||
int32_t dest_arr_len,
|
|
||||||
int32_t src_start,
|
|
||||||
int32_t src_end,
|
|
||||||
int32_t src_step,
|
|
||||||
uint8_t *src_arr,
|
|
||||||
int32_t src_arr_len,
|
|
||||||
const int32_t size
|
|
||||||
) {
|
|
||||||
/* if dest_arr_len == 0, do nothing since we do not support extending list */
|
|
||||||
if (dest_arr_len == 0) return dest_arr_len;
|
|
||||||
/* if both step is 1, memmove directly, handle the dropping of the list, and shrink size */
|
|
||||||
if (src_step == dest_step && dest_step == 1) {
|
|
||||||
const int32_t src_len = (src_end >= src_start) ? (src_end - src_start + 1) : 0;
|
|
||||||
const int32_t dest_len = (dest_end >= dest_start) ? (dest_end - dest_start + 1) : 0;
|
|
||||||
if (src_len > 0) {
|
|
||||||
__builtin_memmove(
|
|
||||||
dest_arr + dest_start * size,
|
|
||||||
src_arr + src_start * size,
|
|
||||||
src_len * size
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (dest_len > 0) {
|
|
||||||
/* dropping */
|
|
||||||
__builtin_memmove(
|
|
||||||
dest_arr + (dest_start + src_len) * size,
|
|
||||||
dest_arr + (dest_end + 1) * size,
|
|
||||||
(dest_arr_len - dest_end - 1) * size
|
|
||||||
);
|
|
||||||
}
|
|
||||||
/* shrink size */
|
|
||||||
return dest_arr_len - (dest_len - src_len);
|
|
||||||
}
|
|
||||||
/* if two range overlaps, need alloca */
|
|
||||||
uint8_t need_alloca =
|
|
||||||
(dest_arr == src_arr)
|
|
||||||
&& !(
|
|
||||||
MAX(dest_start, dest_end) < MIN(src_start, src_end)
|
|
||||||
|| MAX(src_start, src_end) < MIN(dest_start, dest_end)
|
|
||||||
);
|
|
||||||
if (need_alloca) {
|
|
||||||
uint8_t *tmp = __builtin_alloca(src_arr_len * size);
|
|
||||||
__builtin_memcpy(tmp, src_arr, src_arr_len * size);
|
|
||||||
src_arr = tmp;
|
|
||||||
}
|
|
||||||
int32_t src_ind = src_start;
|
|
||||||
int32_t dest_ind = dest_start;
|
|
||||||
for (;
|
|
||||||
(src_step > 0) ? (src_ind <= src_end) : (src_ind >= src_end);
|
|
||||||
src_ind += src_step, dest_ind += dest_step
|
|
||||||
) {
|
|
||||||
/* for constant optimization */
|
|
||||||
if (size == 1) {
|
|
||||||
__builtin_memcpy(dest_arr + dest_ind, src_arr + src_ind, 1);
|
|
||||||
} else if (size == 4) {
|
|
||||||
__builtin_memcpy(dest_arr + dest_ind * 4, src_arr + src_ind * 4, 4);
|
|
||||||
} else if (size == 8) {
|
|
||||||
__builtin_memcpy(dest_arr + dest_ind * 8, src_arr + src_ind * 8, 8);
|
|
||||||
} else {
|
|
||||||
/* memcpy for var size, cannot overlap after previous alloca */
|
|
||||||
__builtin_memcpy(dest_arr + dest_ind * size, src_arr + src_ind * size, size);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* only dest_step == 1 can we shrink the dest list. */
|
|
||||||
/* size should be ensured prior to calling this function */
|
|
||||||
if (dest_step == 1 && dest_end >= dest_start) {
|
|
||||||
__builtin_memmove(
|
|
||||||
dest_arr + dest_ind * size,
|
|
||||||
dest_arr + (dest_end + 1) * size,
|
|
||||||
(dest_arr_len - dest_end - 1) * size
|
|
||||||
);
|
|
||||||
return dest_arr_len - (dest_end - dest_ind) - 1;
|
|
||||||
}
|
|
||||||
return dest_arr_len;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t __nac3_isinf(double x) {
|
|
||||||
return __builtin_isinf(x);
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t __nac3_isnan(double x) {
|
|
||||||
return __builtin_isnan(x);
|
|
||||||
}
|
|
||||||
|
|
||||||
double tgamma(double arg);
|
|
||||||
|
|
||||||
double __nac3_gamma(double z) {
|
|
||||||
// Handling for denormals
|
|
||||||
// | x | Python gamma(x) | C tgamma(x) |
|
|
||||||
// --- | ----------------- | --------------- | ----------- |
|
|
||||||
// (1) | nan | nan | nan |
|
|
||||||
// (2) | -inf | -inf | inf |
|
|
||||||
// (3) | inf | inf | inf |
|
|
||||||
// (4) | 0.0 | inf | inf |
|
|
||||||
// (5) | {-1.0, -2.0, ...} | inf | nan |
|
|
||||||
|
|
||||||
// (1)-(3)
|
|
||||||
if (__builtin_isinf(z) || __builtin_isnan(z)) {
|
|
||||||
return z;
|
|
||||||
}
|
|
||||||
|
|
||||||
double v = tgamma(z);
|
|
||||||
|
|
||||||
// (4)-(5)
|
|
||||||
return __builtin_isinf(v) || __builtin_isnan(v) ? __builtin_inf() : v;
|
|
||||||
}
|
|
||||||
|
|
||||||
double lgamma(double arg);
|
|
||||||
|
|
||||||
double __nac3_gammaln(double x) {
|
|
||||||
// libm's handling of value overflows differs from scipy:
|
|
||||||
// - scipy: gammaln(-inf) -> -inf
|
|
||||||
// - libm : lgamma(-inf) -> inf
|
|
||||||
|
|
||||||
if (__builtin_isinf(x)) {
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
return lgamma(x);
|
|
||||||
}
|
|
||||||
|
|
||||||
double j0(double x);
|
|
||||||
|
|
||||||
double __nac3_j0(double x) {
|
|
||||||
// libm's handling of value overflows differs from scipy:
|
|
||||||
// - scipy: j0(inf) -> nan
|
|
||||||
// - libm : j0(inf) -> 0.0
|
|
||||||
|
|
||||||
if (__builtin_isinf(x)) {
|
|
||||||
return __builtin_nan("");
|
|
||||||
}
|
|
||||||
|
|
||||||
return j0(x);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t __nac3_ndarray_calc_size(
|
|
||||||
const uint64_t *list_data,
|
|
||||||
uint32_t list_len,
|
|
||||||
uint32_t begin_idx,
|
|
||||||
uint32_t end_idx
|
|
||||||
) {
|
|
||||||
__builtin_assume(end_idx <= list_len);
|
|
||||||
|
|
||||||
uint32_t num_elems = 1;
|
|
||||||
for (uint32_t i = begin_idx; i < end_idx; ++i) {
|
|
||||||
uint64_t val = list_data[i];
|
|
||||||
__builtin_assume(val > 0);
|
|
||||||
num_elems *= val;
|
|
||||||
}
|
|
||||||
return num_elems;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t __nac3_ndarray_calc_size64(
|
|
||||||
const uint64_t *list_data,
|
|
||||||
uint64_t list_len,
|
|
||||||
uint64_t begin_idx,
|
|
||||||
uint64_t end_idx
|
|
||||||
) {
|
|
||||||
__builtin_assume(end_idx <= list_len);
|
|
||||||
|
|
||||||
uint64_t num_elems = 1;
|
|
||||||
for (uint64_t i = begin_idx; i < end_idx; ++i) {
|
|
||||||
uint64_t val = list_data[i];
|
|
||||||
__builtin_assume(val > 0);
|
|
||||||
num_elems *= val;
|
|
||||||
}
|
|
||||||
return num_elems;
|
|
||||||
}
|
|
||||||
|
|
||||||
void __nac3_ndarray_calc_nd_indices(
|
|
||||||
uint32_t index,
|
|
||||||
const uint32_t* dims,
|
|
||||||
uint32_t num_dims,
|
|
||||||
uint32_t* idxs
|
|
||||||
) {
|
|
||||||
uint32_t stride = 1;
|
|
||||||
for (uint32_t dim = 0; dim < num_dims; dim++) {
|
|
||||||
uint32_t i = num_dims - dim - 1;
|
|
||||||
__builtin_assume(dims[i] > 0);
|
|
||||||
idxs[i] = (index / stride) % dims[i];
|
|
||||||
stride *= dims[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void __nac3_ndarray_calc_nd_indices64(
|
|
||||||
uint64_t index,
|
|
||||||
const uint64_t* dims,
|
|
||||||
uint64_t num_dims,
|
|
||||||
uint32_t* idxs
|
|
||||||
) {
|
|
||||||
uint64_t stride = 1;
|
|
||||||
for (uint64_t dim = 0; dim < num_dims; dim++) {
|
|
||||||
uint64_t i = num_dims - dim - 1;
|
|
||||||
__builtin_assume(dims[i] > 0);
|
|
||||||
idxs[i] = (uint32_t) ((index / stride) % dims[i]);
|
|
||||||
stride *= dims[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t __nac3_ndarray_flatten_index(
|
|
||||||
const uint32_t* dims,
|
|
||||||
uint32_t num_dims,
|
|
||||||
const uint32_t* indices,
|
|
||||||
uint32_t num_indices
|
|
||||||
) {
|
|
||||||
uint32_t idx = 0;
|
|
||||||
uint32_t stride = 1;
|
|
||||||
for (uint32_t i = 0; i < num_dims; ++i) {
|
|
||||||
uint32_t ri = num_dims - i - 1;
|
|
||||||
if (ri < num_indices) {
|
|
||||||
idx += (stride * indices[ri]);
|
|
||||||
}
|
|
||||||
|
|
||||||
__builtin_assume(dims[i] > 0);
|
|
||||||
stride *= dims[ri];
|
|
||||||
}
|
|
||||||
return idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t __nac3_ndarray_flatten_index64(
|
|
||||||
const uint64_t* dims,
|
|
||||||
uint64_t num_dims,
|
|
||||||
const uint32_t* indices,
|
|
||||||
uint64_t num_indices
|
|
||||||
) {
|
|
||||||
uint64_t idx = 0;
|
|
||||||
uint64_t stride = 1;
|
|
||||||
for (uint64_t i = 0; i < num_dims; ++i) {
|
|
||||||
uint64_t ri = num_dims - i - 1;
|
|
||||||
if (ri < num_indices) {
|
|
||||||
idx += (stride * indices[ri]);
|
|
||||||
}
|
|
||||||
|
|
||||||
__builtin_assume(dims[i] > 0);
|
|
||||||
stride *= dims[ri];
|
|
||||||
}
|
|
||||||
return idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
void __nac3_ndarray_calc_broadcast(
|
|
||||||
const uint32_t *lhs_dims,
|
|
||||||
uint32_t lhs_ndims,
|
|
||||||
const uint32_t *rhs_dims,
|
|
||||||
uint32_t rhs_ndims,
|
|
||||||
uint32_t *out_dims
|
|
||||||
) {
|
|
||||||
uint32_t max_ndims = lhs_ndims > rhs_ndims ? lhs_ndims : rhs_ndims;
|
|
||||||
|
|
||||||
for (uint32_t i = 0; i < max_ndims; ++i) {
|
|
||||||
uint32_t *lhs_dim_sz = i < lhs_ndims ? &lhs_dims[lhs_ndims - i - 1] : NULL;
|
|
||||||
uint32_t *rhs_dim_sz = i < rhs_ndims ? &rhs_dims[rhs_ndims - i - 1] : NULL;
|
|
||||||
uint32_t *out_dim = &out_dims[max_ndims - i - 1];
|
|
||||||
|
|
||||||
if (lhs_dim_sz == NULL) {
|
|
||||||
*out_dim = *rhs_dim_sz;
|
|
||||||
} else if (rhs_dim_sz == NULL) {
|
|
||||||
*out_dim = *lhs_dim_sz;
|
|
||||||
} else if (*lhs_dim_sz == 1) {
|
|
||||||
*out_dim = *rhs_dim_sz;
|
|
||||||
} else if (*rhs_dim_sz == 1) {
|
|
||||||
*out_dim = *lhs_dim_sz;
|
|
||||||
} else if (*lhs_dim_sz == *rhs_dim_sz) {
|
|
||||||
*out_dim = *lhs_dim_sz;
|
|
||||||
} else {
|
|
||||||
__builtin_unreachable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void __nac3_ndarray_calc_broadcast64(
|
|
||||||
const uint64_t *lhs_dims,
|
|
||||||
uint64_t lhs_ndims,
|
|
||||||
const uint64_t *rhs_dims,
|
|
||||||
uint64_t rhs_ndims,
|
|
||||||
uint64_t *out_dims
|
|
||||||
) {
|
|
||||||
uint64_t max_ndims = lhs_ndims > rhs_ndims ? lhs_ndims : rhs_ndims;
|
|
||||||
|
|
||||||
for (uint64_t i = 0; i < max_ndims; ++i) {
|
|
||||||
uint64_t *lhs_dim_sz = i < lhs_ndims ? &lhs_dims[lhs_ndims - i - 1] : NULL;
|
|
||||||
uint64_t *rhs_dim_sz = i < rhs_ndims ? &rhs_dims[rhs_ndims - i - 1] : NULL;
|
|
||||||
uint64_t *out_dim = &out_dims[max_ndims - i - 1];
|
|
||||||
|
|
||||||
if (lhs_dim_sz == NULL) {
|
|
||||||
*out_dim = *rhs_dim_sz;
|
|
||||||
} else if (rhs_dim_sz == NULL) {
|
|
||||||
*out_dim = *lhs_dim_sz;
|
|
||||||
} else if (*lhs_dim_sz == 1) {
|
|
||||||
*out_dim = *rhs_dim_sz;
|
|
||||||
} else if (*rhs_dim_sz == 1) {
|
|
||||||
*out_dim = *lhs_dim_sz;
|
|
||||||
} else if (*lhs_dim_sz == *rhs_dim_sz) {
|
|
||||||
*out_dim = *lhs_dim_sz;
|
|
||||||
} else {
|
|
||||||
__builtin_unreachable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void __nac3_ndarray_calc_broadcast_idx(
|
|
||||||
const uint32_t *src_dims,
|
|
||||||
uint32_t src_ndims,
|
|
||||||
const uint32_t *in_idx,
|
|
||||||
uint32_t *out_idx
|
|
||||||
) {
|
|
||||||
for (uint32_t i = 0; i < src_ndims; ++i) {
|
|
||||||
uint32_t src_i = src_ndims - i - 1;
|
|
||||||
out_idx[src_i] = src_dims[src_i] == 1 ? 0 : in_idx[src_i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void __nac3_ndarray_calc_broadcast_idx64(
|
|
||||||
const uint64_t *src_dims,
|
|
||||||
uint64_t src_ndims,
|
|
||||||
const uint32_t *in_idx,
|
|
||||||
uint32_t *out_idx
|
|
||||||
) {
|
|
||||||
for (uint64_t i = 0; i < src_ndims; ++i) {
|
|
||||||
uint64_t src_i = src_ndims - i - 1;
|
|
||||||
out_idx[src_i] = src_dims[src_i] == 1 ? 0 : (uint32_t) in_idx[src_i];
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,9 +1,11 @@
|
||||||
use crate::typecheck::typedef::Type;
|
use crate::{typecheck::typedef::Type, util::SizeVariant};
|
||||||
|
|
||||||
|
mod test;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
classes::{
|
classes::{
|
||||||
ArrayLikeIndexer, ArrayLikeValue, ArraySliceValue, ListValue, NDArrayValue,
|
ArrayLikeIndexer, ArrayLikeValue, ArraySliceValue, ListValue, NDArrayValue, NpArrayType,
|
||||||
TypedArrayLikeAdapter, UntypedArrayLikeAccessor,
|
NpArrayValue, TypedArrayLikeAdapter, UntypedArrayLikeAccessor,
|
||||||
},
|
},
|
||||||
llvm_intrinsics, CodeGenContext, CodeGenerator,
|
llvm_intrinsics, CodeGenContext, CodeGenerator,
|
||||||
};
|
};
|
||||||
|
@ -14,8 +16,8 @@ use inkwell::{
|
||||||
context::Context,
|
context::Context,
|
||||||
memory_buffer::MemoryBuffer,
|
memory_buffer::MemoryBuffer,
|
||||||
module::Module,
|
module::Module,
|
||||||
types::{BasicTypeEnum, IntType},
|
types::{BasicType, BasicTypeEnum, FunctionType, IntType, PointerType},
|
||||||
values::{BasicValueEnum, CallSiteValue, FloatValue, IntValue},
|
values::{BasicValueEnum, CallSiteValue, FloatValue, FunctionValue, IntValue},
|
||||||
AddressSpace, IntPredicate,
|
AddressSpace, IntPredicate,
|
||||||
};
|
};
|
||||||
use itertools::Either;
|
use itertools::Either;
|
||||||
|
@ -579,10 +581,8 @@ where
|
||||||
G: CodeGenerator + ?Sized,
|
G: CodeGenerator + ?Sized,
|
||||||
Dims: ArrayLikeIndexer<'ctx>,
|
Dims: ArrayLikeIndexer<'ctx>,
|
||||||
{
|
{
|
||||||
let llvm_i64 = ctx.ctx.i64_type();
|
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
|
||||||
let llvm_pi64 = llvm_i64.ptr_type(AddressSpace::default());
|
|
||||||
|
|
||||||
let ndarray_calc_size_fn_name = match llvm_usize.get_bit_width() {
|
let ndarray_calc_size_fn_name = match llvm_usize.get_bit_width() {
|
||||||
32 => "__nac3_ndarray_calc_size",
|
32 => "__nac3_ndarray_calc_size",
|
||||||
|
@ -590,7 +590,7 @@ where
|
||||||
bw => unreachable!("Unsupported size type bit width: {}", bw),
|
bw => unreachable!("Unsupported size type bit width: {}", bw),
|
||||||
};
|
};
|
||||||
let ndarray_calc_size_fn_t = llvm_usize.fn_type(
|
let ndarray_calc_size_fn_t = llvm_usize.fn_type(
|
||||||
&[llvm_pi64.into(), llvm_usize.into(), llvm_usize.into(), llvm_usize.into()],
|
&[llvm_pusize.into(), llvm_usize.into(), llvm_usize.into(), llvm_usize.into()],
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
let ndarray_calc_size_fn =
|
let ndarray_calc_size_fn =
|
||||||
|
@ -802,7 +802,7 @@ pub fn call_ndarray_calc_broadcast<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
ctx,
|
ctx,
|
||||||
llvm_usize.const_zero(),
|
llvm_usize.const_zero(),
|
||||||
(min_ndims, false),
|
(min_ndims, false),
|
||||||
|generator, ctx, idx| {
|
|generator, ctx, _, idx| {
|
||||||
let idx = ctx.builder.build_int_sub(min_ndims, idx, "").unwrap();
|
let idx = ctx.builder.build_int_sub(min_ndims, idx, "").unwrap();
|
||||||
let (lhs_dim_sz, rhs_dim_sz) = unsafe {
|
let (lhs_dim_sz, rhs_dim_sz) = unsafe {
|
||||||
(
|
(
|
||||||
|
@ -929,3 +929,63 @@ pub fn call_ndarray_calc_broadcast_index<
|
||||||
Box::new(|_, v| v.into()),
|
Box::new(|_, v| v.into()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_size_variant<'ctx>(ty: IntType<'ctx>) -> SizeVariant {
|
||||||
|
match ty.get_bit_width() {
|
||||||
|
32 => SizeVariant::Bits32,
|
||||||
|
64 => SizeVariant::Bits64,
|
||||||
|
_ => unreachable!("Unsupported int type bit width {}", ty.get_bit_width()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_size_type_dependent_function<'ctx, BuildFuncTypeFn>(
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
size_type: IntType<'ctx>,
|
||||||
|
base_name: &str,
|
||||||
|
build_func_type: BuildFuncTypeFn,
|
||||||
|
) -> FunctionValue<'ctx>
|
||||||
|
where
|
||||||
|
BuildFuncTypeFn: Fn() -> FunctionType<'ctx>,
|
||||||
|
{
|
||||||
|
let mut fn_name = base_name.to_owned();
|
||||||
|
match get_size_variant(size_type) {
|
||||||
|
SizeVariant::Bits32 => {
|
||||||
|
// The original fn_name is the correct function name
|
||||||
|
}
|
||||||
|
SizeVariant::Bits64 => {
|
||||||
|
// Append "64" at the end, this is the naming convention for 64-bit
|
||||||
|
fn_name.push_str("64");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get (or declare then get if does not exist) the corresponding function
|
||||||
|
ctx.module.get_function(&fn_name).unwrap_or_else(|| {
|
||||||
|
let fn_type = build_func_type();
|
||||||
|
ctx.module.add_function(&fn_name, fn_type, None)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_ndarray_struct_ptr<'ctx>(ctx: &'ctx Context, size_type: IntType<'ctx>) -> PointerType<'ctx> {
|
||||||
|
let i8_type = ctx.i8_type();
|
||||||
|
|
||||||
|
let ndarray_ty = NpArrayType { size_type, elem_type: i8_type.as_basic_type_enum() };
|
||||||
|
let struct_ty = ndarray_ty.fields().whole_struct.as_struct_type(ctx);
|
||||||
|
struct_ty.ptr_type(AddressSpace::default())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_nac3_ndarray_size<'ctx>(
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
ndarray: NpArrayValue<'ctx>,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
let size_type = ndarray.ty.size_type;
|
||||||
|
let function = get_size_type_dependent_function(ctx, size_type, "__nac3_ndarray_size", || {
|
||||||
|
size_type.fn_type(&[get_ndarray_struct_ptr(ctx.ctx, size_type).into()], false)
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.builder
|
||||||
|
.build_call(function, &[ndarray.ptr.into()], "size")
|
||||||
|
.unwrap()
|
||||||
|
.try_as_basic_value()
|
||||||
|
.unwrap_left()
|
||||||
|
.into_int_value()
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::{path::Path, process::Command};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_irrt_test() {
|
||||||
|
assert!(
|
||||||
|
cfg!(feature = "test"),
|
||||||
|
"Please do `cargo test -F test` to compile `irrt_test.out` and run test"
|
||||||
|
);
|
||||||
|
|
||||||
|
let irrt_test_out_path = Path::new(concat!(env!("OUT_DIR"), "/irrt_test.out"));
|
||||||
|
let output = Command::new(irrt_test_out_path.to_str().unwrap()).output().unwrap();
|
||||||
|
|
||||||
|
if !output.status.success() {
|
||||||
|
eprintln!("irrt_test failed with status {}:", output.status);
|
||||||
|
eprintln!("====== stdout ======");
|
||||||
|
eprintln!("{}", String::from_utf8(output.stdout).unwrap());
|
||||||
|
eprintln!("====== stderr ======");
|
||||||
|
eprintln!("{}", String::from_utf8(output.stderr).unwrap());
|
||||||
|
eprintln!("====================");
|
||||||
|
|
||||||
|
panic!("irrt_test failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -62,145 +62,30 @@ pub fn call_stacksave<'ctx>(
|
||||||
pub fn call_stackrestore<'ctx>(ctx: &CodeGenContext<'ctx, '_>, ptr: PointerValue<'ctx>) {
|
pub fn call_stackrestore<'ctx>(ctx: &CodeGenContext<'ctx, '_>, ptr: PointerValue<'ctx>) {
|
||||||
const FN_NAME: &str = "llvm.stackrestore";
|
const FN_NAME: &str = "llvm.stackrestore";
|
||||||
|
|
||||||
let llvm_i8 = ctx.ctx.i8_type();
|
/*
|
||||||
let llvm_p0i8 = llvm_i8.ptr_type(AddressSpace::default());
|
SEE https://github.com/TheDan64/inkwell/issues/496
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
We want `llvm.stackrestore`, but the following would generate `llvm.stackrestore.p0i8`.
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_p0i8.into()]))
|
```ignore
|
||||||
.unwrap();
|
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
||||||
|
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_p0i8.into()]))
|
||||||
|
.unwrap();
|
||||||
|
```
|
||||||
|
|
||||||
|
Temp workaround by manually declaring the intrinsic with the correct function name instead.
|
||||||
|
*/
|
||||||
|
let intrinsic_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
||||||
|
let llvm_void = ctx.ctx.void_type();
|
||||||
|
let llvm_i8 = ctx.ctx.i8_type();
|
||||||
|
let llvm_p0i8 = llvm_i8.ptr_type(AddressSpace::default());
|
||||||
|
let fn_type = llvm_void.fn_type(&[llvm_p0i8.into()], false);
|
||||||
|
|
||||||
|
ctx.module.add_function(FN_NAME, fn_type, None)
|
||||||
|
});
|
||||||
|
|
||||||
ctx.builder.build_call(intrinsic_fn, &[ptr.into()], "").unwrap();
|
ctx.builder.build_call(intrinsic_fn, &[ptr.into()], "").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Invokes the [`llvm.abs`](https://llvm.org/docs/LangRef.html#llvm-abs-intrinsic) intrinsic.
|
|
||||||
///
|
|
||||||
/// * `src` - The value for which the absolute value is to be returned.
|
|
||||||
/// * `is_int_min_poison` - Whether `poison` is to be returned if `src` is `INT_MIN`.
|
|
||||||
pub fn call_int_abs<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
src: IntValue<'ctx>,
|
|
||||||
is_int_min_poison: IntValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> IntValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.abs";
|
|
||||||
|
|
||||||
debug_assert_eq!(is_int_min_poison.get_type().get_bit_width(), 1);
|
|
||||||
debug_assert!(is_int_min_poison.is_const());
|
|
||||||
|
|
||||||
let llvm_src_t = src.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_src_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[src.into(), is_int_min_poison.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_int_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.smax`](https://llvm.org/docs/LangRef.html#llvm-smax-intrinsic) intrinsic.
|
|
||||||
pub fn call_int_smax<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
a: IntValue<'ctx>,
|
|
||||||
b: IntValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> IntValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.smax";
|
|
||||||
|
|
||||||
debug_assert_eq!(a.get_type().get_bit_width(), b.get_type().get_bit_width());
|
|
||||||
|
|
||||||
let llvm_int_t = a.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_int_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[a.into(), b.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_int_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.smin`](https://llvm.org/docs/LangRef.html#llvm-smin-intrinsic) intrinsic.
|
|
||||||
pub fn call_int_smin<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
a: IntValue<'ctx>,
|
|
||||||
b: IntValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> IntValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.smin";
|
|
||||||
|
|
||||||
debug_assert_eq!(a.get_type().get_bit_width(), b.get_type().get_bit_width());
|
|
||||||
|
|
||||||
let llvm_int_t = a.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_int_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[a.into(), b.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_int_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.umax`](https://llvm.org/docs/LangRef.html#llvm-umax-intrinsic) intrinsic.
|
|
||||||
pub fn call_int_umax<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
a: IntValue<'ctx>,
|
|
||||||
b: IntValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> IntValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.umax";
|
|
||||||
|
|
||||||
debug_assert_eq!(a.get_type().get_bit_width(), b.get_type().get_bit_width());
|
|
||||||
|
|
||||||
let llvm_int_t = a.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_int_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[a.into(), b.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_int_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.umin`](https://llvm.org/docs/LangRef.html#llvm-umin-intrinsic) intrinsic.
|
|
||||||
pub fn call_int_umin<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
a: IntValue<'ctx>,
|
|
||||||
b: IntValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> IntValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.umin";
|
|
||||||
|
|
||||||
debug_assert_eq!(a.get_type().get_bit_width(), b.get_type().get_bit_width());
|
|
||||||
|
|
||||||
let llvm_int_t = a.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_int_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[a.into(), b.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_int_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.memcpy`](https://llvm.org/docs/LangRef.html#llvm-memcpy-intrinsic) intrinsic.
|
/// Invokes the [`llvm.memcpy`](https://llvm.org/docs/LangRef.html#llvm-memcpy-intrinsic) intrinsic.
|
||||||
///
|
///
|
||||||
/// * `dest` - The pointer to the destination. Must be a pointer to an integer type.
|
/// * `dest` - The pointer to the destination. Must be a pointer to an integer type.
|
||||||
|
@ -280,28 +165,122 @@ pub fn call_memcpy_generic<'ctx>(
|
||||||
call_memcpy(ctx, dest, src, len, is_volatile);
|
call_memcpy(ctx, dest, src, len, is_volatile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Invokes the [`llvm.sqrt`](https://llvm.org/docs/LangRef.html#llvm-sqrt-intrinsic) intrinsic.
|
/// Macro to find and generate build call for llvm intrinsic (body of llvm intrinsic function)
|
||||||
pub fn call_float_sqrt<'ctx>(
|
///
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
/// Arguments:
|
||||||
val: FloatValue<'ctx>,
|
/// * `$ctx:ident`: Reference to the current Code Generation Context
|
||||||
name: Option<&str>,
|
/// * `$name:ident`: Optional name to be assigned to the llvm build call (Option<&str>)
|
||||||
) -> FloatValue<'ctx> {
|
/// * `$llvm_name:literal`: Name of underlying llvm intrinsic function
|
||||||
const FN_NAME: &str = "llvm.sqrt";
|
/// * `$map_fn:ident`: Mapping function to be applied on `BasicValue` (`BasicValue` -> Function Return Type)
|
||||||
|
/// Use `BasicValueEnum::into_int_value` for Integer return type and `BasicValueEnum::into_float_value` for Float return type
|
||||||
let llvm_float_t = val.get_type();
|
/// * `$llvm_ty:ident`: Type of first operand
|
||||||
|
/// * `,($val:ident)*`: Comma separated list of operands
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
macro_rules! generate_llvm_intrinsic_fn_body {
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
($ctx:ident, $name:ident, $llvm_name:literal, $map_fn:expr, $llvm_ty:ident $(,$val:ident)*) => {{
|
||||||
.unwrap();
|
const FN_NAME: &str = concat!("llvm.", $llvm_name);
|
||||||
|
let intrinsic_fn = Intrinsic::find(FN_NAME).and_then(|intrinsic| intrinsic.get_declaration(&$ctx.module, &[$llvm_ty.into()])).unwrap();
|
||||||
ctx.builder
|
$ctx.builder.build_call(intrinsic_fn, &[$($val.into()),*], $name.unwrap_or_default()).map(CallSiteValue::try_as_basic_value).map(|v| v.map_left($map_fn)).map(Either::unwrap_left).unwrap()
|
||||||
.build_call(intrinsic_fn, &[val.into()], name.unwrap_or_default())
|
}};
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Macro to generate the llvm intrinsic function using [`generate_llvm_intrinsic_fn_body`].
|
||||||
|
///
|
||||||
|
/// Arguments:
|
||||||
|
/// * `float/int`: Indicates the return and argument type of the function
|
||||||
|
/// * `$fn_name:ident`: The identifier of the rust function to be generated
|
||||||
|
/// * `$llvm_name:literal`: Name of underlying llvm intrinsic function
|
||||||
|
/// Omit "llvm." prefix from the function name i.e. use "ceil" instead of "llvm.ceil"
|
||||||
|
/// * `$val:ident`: The operand for unary operations
|
||||||
|
/// * `$val1:ident`, `$val2:ident`: The operands for binary operations
|
||||||
|
macro_rules! generate_llvm_intrinsic_fn {
|
||||||
|
("float", $fn_name:ident, $llvm_name:literal, $val:ident) => {
|
||||||
|
#[doc = concat!("Invokes the [`", stringify!($llvm_name), "`](https://llvm.org/docs/LangRef.html#llvm-", stringify!($llvm_name), "-intrinsic) intrinsic." )]
|
||||||
|
pub fn $fn_name<'ctx> (
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
$val: FloatValue<'ctx>,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> FloatValue<'ctx> {
|
||||||
|
let llvm_ty = $val.get_type();
|
||||||
|
generate_llvm_intrinsic_fn_body!(ctx, name, $llvm_name, BasicValueEnum::into_float_value, llvm_ty, $val)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
("float", $fn_name:ident, $llvm_name:literal, $val1:ident, $val2:ident) => {
|
||||||
|
#[doc = concat!("Invokes the [`", stringify!($llvm_name), "`](https://llvm.org/docs/LangRef.html#llvm-", stringify!($llvm_name), "-intrinsic) intrinsic." )]
|
||||||
|
pub fn $fn_name<'ctx> (
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
$val1: FloatValue<'ctx>,
|
||||||
|
$val2: FloatValue<'ctx>,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> FloatValue<'ctx> {
|
||||||
|
debug_assert_eq!($val1.get_type(), $val2.get_type());
|
||||||
|
let llvm_ty = $val1.get_type();
|
||||||
|
generate_llvm_intrinsic_fn_body!(ctx, name, $llvm_name, BasicValueEnum::into_float_value, llvm_ty, $val1, $val2)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
("int", $fn_name:ident, $llvm_name:literal, $val1:ident, $val2:ident) => {
|
||||||
|
#[doc = concat!("Invokes the [`", stringify!($llvm_name), "`](https://llvm.org/docs/LangRef.html#llvm-", stringify!($llvm_name), "-intrinsic) intrinsic." )]
|
||||||
|
pub fn $fn_name<'ctx> (
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
$val1: IntValue<'ctx>,
|
||||||
|
$val2: IntValue<'ctx>,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
debug_assert_eq!($val1.get_type().get_bit_width(), $val2.get_type().get_bit_width());
|
||||||
|
let llvm_ty = $val1.get_type();
|
||||||
|
generate_llvm_intrinsic_fn_body!(ctx, name, $llvm_name, BasicValueEnum::into_int_value, llvm_ty, $val1, $val2)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Invokes the [`llvm.abs`](https://llvm.org/docs/LangRef.html#llvm-abs-intrinsic) intrinsic.
|
||||||
|
///
|
||||||
|
/// * `src` - The value for which the absolute value is to be returned.
|
||||||
|
/// * `is_int_min_poison` - Whether `poison` is to be returned if `src` is `INT_MIN`.
|
||||||
|
pub fn call_int_abs<'ctx>(
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
src: IntValue<'ctx>,
|
||||||
|
is_int_min_poison: IntValue<'ctx>,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
debug_assert_eq!(is_int_min_poison.get_type().get_bit_width(), 1);
|
||||||
|
debug_assert!(is_int_min_poison.is_const());
|
||||||
|
|
||||||
|
let src_type = src.get_type();
|
||||||
|
generate_llvm_intrinsic_fn_body!(
|
||||||
|
ctx,
|
||||||
|
name,
|
||||||
|
"abs",
|
||||||
|
BasicValueEnum::into_int_value,
|
||||||
|
src_type,
|
||||||
|
src,
|
||||||
|
is_int_min_poison
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
generate_llvm_intrinsic_fn!("int", call_int_smax, "smax", a, b);
|
||||||
|
generate_llvm_intrinsic_fn!("int", call_int_smin, "smin", a, b);
|
||||||
|
generate_llvm_intrinsic_fn!("int", call_int_umax, "umax", a, b);
|
||||||
|
generate_llvm_intrinsic_fn!("int", call_int_umin, "umin", a, b);
|
||||||
|
generate_llvm_intrinsic_fn!("int", call_expect, "expect", val, expected_val);
|
||||||
|
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_sqrt, "sqrt", val);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_sin, "sin", val);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_cos, "cos", val);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_pow, "pow", val, power);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_exp, "exp", val);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_exp2, "exp2", val);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_log, "log", val);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_log10, "log10", val);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_log2, "log2", val);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_fabs, "fabs", src);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_minnum, "minnum", val, power);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_maxnum, "maxnum", val, power);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_copysign, "copysign", mag, sgn);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_floor, "floor", val);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_ceil, "ceil", val);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_round, "round", val);
|
||||||
|
generate_llvm_intrinsic_fn!("float", call_float_rint, "rint", val);
|
||||||
|
|
||||||
/// Invokes the [`llvm.powi`](https://llvm.org/docs/LangRef.html#llvm-powi-intrinsic) intrinsic.
|
/// Invokes the [`llvm.powi`](https://llvm.org/docs/LangRef.html#llvm-powi-intrinsic) intrinsic.
|
||||||
pub fn call_float_powi<'ctx>(
|
pub fn call_float_powi<'ctx>(
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
@ -327,393 +306,3 @@ pub fn call_float_powi<'ctx>(
|
||||||
.map(Either::unwrap_left)
|
.map(Either::unwrap_left)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Invokes the [`llvm.sin`](https://llvm.org/docs/LangRef.html#llvm-sin-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_sin<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
val: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.sin";
|
|
||||||
|
|
||||||
let llvm_float_t = val.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[val.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.cos`](https://llvm.org/docs/LangRef.html#llvm-cos-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_cos<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
val: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.cos";
|
|
||||||
|
|
||||||
let llvm_float_t = val.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[val.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.pow`](https://llvm.org/docs/LangRef.html#llvm-pow-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_pow<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
val: FloatValue<'ctx>,
|
|
||||||
power: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.pow";
|
|
||||||
|
|
||||||
debug_assert_eq!(val.get_type(), power.get_type());
|
|
||||||
|
|
||||||
let llvm_float_t = val.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[val.into(), power.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.exp`](https://llvm.org/docs/LangRef.html#llvm-exp-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_exp<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
val: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.exp";
|
|
||||||
|
|
||||||
let llvm_float_t = val.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[val.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.exp2`](https://llvm.org/docs/LangRef.html#llvm-exp2-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_exp2<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
val: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.exp2";
|
|
||||||
|
|
||||||
let llvm_float_t = val.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[val.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.log`](https://llvm.org/docs/LangRef.html#llvm-log-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_log<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
val: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.log";
|
|
||||||
|
|
||||||
let llvm_float_t = val.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[val.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.log10`](https://llvm.org/docs/LangRef.html#llvm-log10-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_log10<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
val: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.log10";
|
|
||||||
|
|
||||||
let llvm_float_t = val.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[val.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.log2`](https://llvm.org/docs/LangRef.html#llvm-log2-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_log2<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
val: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.log2";
|
|
||||||
|
|
||||||
let llvm_float_t = val.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[val.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.fabs`](https://llvm.org/docs/LangRef.html#llvm-fabs-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_fabs<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
src: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.fabs";
|
|
||||||
|
|
||||||
let llvm_src_t = src.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_src_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[src.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.minnum`](https://llvm.org/docs/LangRef.html#llvm-minnum-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_minnum<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
val1: FloatValue<'ctx>,
|
|
||||||
val2: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.minnum";
|
|
||||||
|
|
||||||
debug_assert_eq!(val1.get_type(), val2.get_type());
|
|
||||||
|
|
||||||
let llvm_float_t = val1.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[val1.into(), val2.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.maxnum`](https://llvm.org/docs/LangRef.html#llvm-maxnum-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_maxnum<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
val1: FloatValue<'ctx>,
|
|
||||||
val2: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.maxnum";
|
|
||||||
|
|
||||||
debug_assert_eq!(val1.get_type(), val2.get_type());
|
|
||||||
|
|
||||||
let llvm_float_t = val1.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[val1.into(), val2.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.copysign`](https://llvm.org/docs/LangRef.html#llvm-copysign-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_copysign<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
mag: FloatValue<'ctx>,
|
|
||||||
sgn: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.copysign";
|
|
||||||
|
|
||||||
debug_assert_eq!(mag.get_type(), sgn.get_type());
|
|
||||||
|
|
||||||
let llvm_float_t = mag.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[mag.into(), sgn.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.floor`](https://llvm.org/docs/LangRef.html#llvm-floor-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_floor<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
val: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.floor";
|
|
||||||
|
|
||||||
let llvm_float_t = val.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[val.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.ceil`](https://llvm.org/docs/LangRef.html#llvm-ceil-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_ceil<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
val: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.ceil";
|
|
||||||
|
|
||||||
let llvm_float_t = val.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[val.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.round`](https://llvm.org/docs/LangRef.html#llvm-round-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_round<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
val: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.round";
|
|
||||||
|
|
||||||
let llvm_float_t = val.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[val.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the
|
|
||||||
/// [`llvm.roundeven`](https://llvm.org/docs/LangRef.html#llvm-roundeven-intrinsic) intrinsic.
|
|
||||||
pub fn call_float_roundeven<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
val: FloatValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> FloatValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.roundeven";
|
|
||||||
|
|
||||||
let llvm_float_t = val.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_float_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[val.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_float_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the [`llvm.expect`](https://llvm.org/docs/LangRef.html#llvm-expect-intrinsic) intrinsic.
|
|
||||||
pub fn call_expect<'ctx>(
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
val: IntValue<'ctx>,
|
|
||||||
expected_val: IntValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> IntValue<'ctx> {
|
|
||||||
const FN_NAME: &str = "llvm.expect";
|
|
||||||
|
|
||||||
debug_assert_eq!(val.get_type().get_bit_width(), expected_val.get_type().get_bit_width());
|
|
||||||
|
|
||||||
let llvm_int_t = val.get_type();
|
|
||||||
|
|
||||||
let intrinsic_fn = Intrinsic::find(FN_NAME)
|
|
||||||
.and_then(|intrinsic| intrinsic.get_declaration(&ctx.module, &[llvm_int_t.into()]))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(intrinsic_fn, &[val.into(), expected_val.into()], name.unwrap_or_default())
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_int_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
codegen::classes::{ListType, NDArrayType, ProxyType, RangeType},
|
codegen::classes::{ListType, NDArrayType, ProxyType, RangeType},
|
||||||
symbol_resolver::{StaticValue, SymbolResolver},
|
symbol_resolver::{StaticValue, SymbolResolver},
|
||||||
toplevel::{helper::PrimDef, TopLevelContext, TopLevelDef},
|
toplevel::{helper::PrimDef, numpy::unpack_ndarray_var_tys, TopLevelContext, TopLevelDef},
|
||||||
typecheck::{
|
typecheck::{
|
||||||
type_inferencer::{CodeLocation, PrimitiveStore},
|
type_inferencer::{CodeLocation, PrimitiveStore},
|
||||||
typedef::{CallId, FuncArg, Type, TypeEnum, Unifier},
|
typedef::{CallId, FuncArg, Type, TypeEnum, Unifier},
|
||||||
|
@ -47,9 +47,6 @@ pub mod stmt;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test;
|
mod test;
|
||||||
|
|
||||||
use crate::toplevel::primitive_type;
|
|
||||||
use crate::toplevel::primitive_type::OptionType;
|
|
||||||
use crate::typecheck::typedef::GenericObjectType;
|
|
||||||
use concrete_type::{ConcreteType, ConcreteTypeEnum, ConcreteTypeStore};
|
use concrete_type::{ConcreteType, ConcreteTypeEnum, ConcreteTypeStore};
|
||||||
pub use generator::{CodeGenerator, DefaultCodeGenerator};
|
pub use generator::{CodeGenerator, DefaultCodeGenerator};
|
||||||
|
|
||||||
|
@ -459,10 +456,22 @@ fn get_llvm_type<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TObj { obj_id, params, .. } if *obj_id == PrimDef::List.id() => {
|
||||||
|
let element_type = get_llvm_type(
|
||||||
|
ctx,
|
||||||
|
module,
|
||||||
|
generator,
|
||||||
|
unifier,
|
||||||
|
top_level,
|
||||||
|
type_cache,
|
||||||
|
*params.iter().next().unwrap().1,
|
||||||
|
);
|
||||||
|
|
||||||
|
ListType::new(generator, ctx, element_type).as_base_type().into()
|
||||||
|
}
|
||||||
|
|
||||||
TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
||||||
let dtype = primitive_type::NDArrayType::create(ty, unifier)
|
let (dtype, _) = unpack_ndarray_var_tys(unifier, ty);
|
||||||
.dtype_tvar(unifier)
|
|
||||||
.ty;
|
|
||||||
let element_type = get_llvm_type(
|
let element_type = get_llvm_type(
|
||||||
ctx, module, generator, unifier, top_level, type_cache, dtype,
|
ctx, module, generator, unifier, top_level, type_cache, dtype,
|
||||||
);
|
);
|
||||||
|
@ -521,12 +530,6 @@ fn get_llvm_type<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
.collect_vec();
|
.collect_vec();
|
||||||
ctx.struct_type(&fields, false).into()
|
ctx.struct_type(&fields, false).into()
|
||||||
}
|
}
|
||||||
TList { ty } => {
|
|
||||||
let element_type =
|
|
||||||
get_llvm_type(ctx, module, generator, unifier, top_level, type_cache, *ty);
|
|
||||||
|
|
||||||
ListType::new(generator, ctx, element_type).as_base_type().into()
|
|
||||||
}
|
|
||||||
TVirtual { .. } => unimplemented!(),
|
TVirtual { .. } => unimplemented!(),
|
||||||
_ => unreachable!("{}", ty_enum.get_type_name()),
|
_ => unreachable!("{}", ty_enum.get_type_name()),
|
||||||
};
|
};
|
||||||
|
@ -639,10 +642,7 @@ pub fn gen_func_impl<
|
||||||
range: unifier.get_representative(primitives.range),
|
range: unifier.get_representative(primitives.range),
|
||||||
str: unifier.get_representative(primitives.str),
|
str: unifier.get_representative(primitives.str),
|
||||||
exception: unifier.get_representative(primitives.exception),
|
exception: unifier.get_representative(primitives.exception),
|
||||||
option: OptionType::create(
|
option: unifier.get_representative(primitives.option),
|
||||||
unifier.get_representative(primitives.option.into()),
|
|
||||||
&mut unifier,
|
|
||||||
),
|
|
||||||
..primitives
|
..primitives
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -4,16 +4,17 @@ use super::{
|
||||||
irrt::{handle_slice_indices, list_slice_assignment},
|
irrt::{handle_slice_indices, list_slice_assignment},
|
||||||
CodeGenContext, CodeGenerator,
|
CodeGenContext, CodeGenerator,
|
||||||
};
|
};
|
||||||
use crate::toplevel::primitive_type;
|
|
||||||
use crate::typecheck::typedef::GenericObjectType;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
codegen::{
|
codegen::{
|
||||||
classes::{ArrayLikeIndexer, ArraySliceValue, ListValue, RangeValue},
|
classes::{ArrayLikeIndexer, ArraySliceValue, ListValue, RangeValue},
|
||||||
expr::gen_binop_expr,
|
expr::gen_binop_expr,
|
||||||
gen_in_range_check,
|
gen_in_range_check,
|
||||||
},
|
},
|
||||||
toplevel::{helper::PrimDef, DefinitionId, TopLevelDef},
|
toplevel::{helper::PrimDef, numpy::unpack_ndarray_var_tys, DefinitionId, TopLevelDef},
|
||||||
typecheck::typedef::{FunSignature, Type, TypeEnum},
|
typecheck::{
|
||||||
|
magic_methods::Binop,
|
||||||
|
typedef::{FunSignature, Type, TypeEnum},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use inkwell::{
|
use inkwell::{
|
||||||
attributes::{Attribute, AttributeLoc},
|
attributes::{Attribute, AttributeLoc},
|
||||||
|
@ -138,7 +139,7 @@ pub fn gen_store_target<'ctx, G: CodeGenerator>(
|
||||||
}
|
}
|
||||||
ExprKind::Subscript { value, slice, .. } => {
|
ExprKind::Subscript { value, slice, .. } => {
|
||||||
match ctx.unifier.get_ty_immutable(value.custom.unwrap()).as_ref() {
|
match ctx.unifier.get_ty_immutable(value.custom.unwrap()).as_ref() {
|
||||||
TypeEnum::TList { .. } => {
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::List.id() => {
|
||||||
let v = generator
|
let v = generator
|
||||||
.gen_expr(ctx, value)?
|
.gen_expr(ctx, value)?
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -245,11 +246,11 @@ pub fn gen_assign<'ctx, G: CodeGenerator>(
|
||||||
.into_pointer_value();
|
.into_pointer_value();
|
||||||
let value = ListValue::from_ptr_val(value, llvm_usize, None);
|
let value = ListValue::from_ptr_val(value, llvm_usize, None);
|
||||||
let ty = match &*ctx.unifier.get_ty_immutable(target.custom.unwrap()) {
|
let ty = match &*ctx.unifier.get_ty_immutable(target.custom.unwrap()) {
|
||||||
TypeEnum::TList { ty } => *ty,
|
TypeEnum::TObj { obj_id, params, .. } if *obj_id == PrimDef::List.id() => {
|
||||||
|
*params.iter().next().unwrap().1
|
||||||
|
}
|
||||||
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
||||||
primitive_type::NDArrayType::create(target.custom.unwrap(), &mut ctx.unifier)
|
unpack_ndarray_var_tys(&mut ctx.unifier, target.custom.unwrap()).0
|
||||||
.dtype_tvar(&mut ctx.unifier)
|
|
||||||
.ty
|
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
@ -466,6 +467,16 @@ pub fn gen_for<G: CodeGenerator>(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq, Debug, Clone, Copy, Hash)]
|
||||||
|
pub struct BreakContinueHooks<'ctx> {
|
||||||
|
/// The [exit block][`BasicBlock`] to branch to when `break`-ing out of a loop.
|
||||||
|
pub exit_bb: BasicBlock<'ctx>,
|
||||||
|
|
||||||
|
/// The [latch basic block][`BasicBlock`] to branch to for `continue`-ing to the next iteration
|
||||||
|
/// of the loop.
|
||||||
|
pub latch_bb: BasicBlock<'ctx>,
|
||||||
|
}
|
||||||
|
|
||||||
/// Generates a C-style `for` construct using lambdas, similar to the following C code:
|
/// Generates a C-style `for` construct using lambdas, similar to the following C code:
|
||||||
///
|
///
|
||||||
/// ```c
|
/// ```c
|
||||||
|
@ -493,7 +504,8 @@ where
|
||||||
I: Clone,
|
I: Clone,
|
||||||
InitFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>) -> Result<I, String>,
|
InitFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>) -> Result<I, String>,
|
||||||
CondFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, I) -> Result<IntValue<'ctx>, String>,
|
CondFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, I) -> Result<IntValue<'ctx>, String>,
|
||||||
BodyFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, I) -> Result<(), String>,
|
BodyFn:
|
||||||
|
FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, BreakContinueHooks, I) -> Result<(), String>,
|
||||||
UpdateFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, I) -> Result<(), String>,
|
UpdateFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, I) -> Result<(), String>,
|
||||||
{
|
{
|
||||||
let current_bb = ctx.builder.get_insert_block().unwrap();
|
let current_bb = ctx.builder.get_insert_block().unwrap();
|
||||||
|
@ -524,7 +536,8 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.builder.position_at_end(body_bb);
|
ctx.builder.position_at_end(body_bb);
|
||||||
body(generator, ctx, loop_var.clone())?;
|
let hooks = BreakContinueHooks { exit_bb: cont_bb, latch_bb: update_bb };
|
||||||
|
body(generator, ctx, hooks, loop_var.clone())?;
|
||||||
if !ctx.is_terminated() {
|
if !ctx.is_terminated() {
|
||||||
ctx.builder.build_unconditional_branch(update_bb).unwrap();
|
ctx.builder.build_unconditional_branch(update_bb).unwrap();
|
||||||
}
|
}
|
||||||
|
@ -566,7 +579,12 @@ pub fn gen_for_callback_incrementing<'ctx, 'a, G, BodyFn>(
|
||||||
) -> Result<(), String>
|
) -> Result<(), String>
|
||||||
where
|
where
|
||||||
G: CodeGenerator + ?Sized,
|
G: CodeGenerator + ?Sized,
|
||||||
BodyFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, IntValue<'ctx>) -> Result<(), String>,
|
BodyFn: FnOnce(
|
||||||
|
&mut G,
|
||||||
|
&mut CodeGenContext<'ctx, 'a>,
|
||||||
|
BreakContinueHooks,
|
||||||
|
IntValue<'ctx>,
|
||||||
|
) -> Result<(), String>,
|
||||||
{
|
{
|
||||||
let init_val_t = init_val.get_type();
|
let init_val_t = init_val.get_type();
|
||||||
|
|
||||||
|
@ -588,10 +606,10 @@ where
|
||||||
|
|
||||||
Ok(ctx.builder.build_int_compare(cmp_op, i, max_val, "").unwrap())
|
Ok(ctx.builder.build_int_compare(cmp_op, i, max_val, "").unwrap())
|
||||||
},
|
},
|
||||||
|generator, ctx, i_addr| {
|
|generator, ctx, hooks, i_addr| {
|
||||||
let i = ctx.builder.build_load(i_addr, "").map(BasicValueEnum::into_int_value).unwrap();
|
let i = ctx.builder.build_load(i_addr, "").map(BasicValueEnum::into_int_value).unwrap();
|
||||||
|
|
||||||
body(generator, ctx, i)
|
body(generator, ctx, hooks, i)
|
||||||
},
|
},
|
||||||
|_, ctx, i_addr| {
|
|_, ctx, i_addr| {
|
||||||
let i = ctx.builder.build_load(i_addr, "").map(BasicValueEnum::into_int_value).unwrap();
|
let i = ctx.builder.build_load(i_addr, "").map(BasicValueEnum::into_int_value).unwrap();
|
||||||
|
@ -702,7 +720,7 @@ where
|
||||||
|
|
||||||
Ok(cond)
|
Ok(cond)
|
||||||
},
|
},
|
||||||
|generator, ctx, (i_addr, _)| {
|
|generator, ctx, _, (i_addr, _)| {
|
||||||
let i = ctx.builder.build_load(i_addr, "").map(BasicValueEnum::into_int_value).unwrap();
|
let i = ctx.builder.build_load(i_addr, "").map(BasicValueEnum::into_int_value).unwrap();
|
||||||
|
|
||||||
body_fn(generator, ctx, i)
|
body_fn(generator, ctx, i)
|
||||||
|
@ -1578,7 +1596,14 @@ pub fn gen_stmt<G: CodeGenerator>(
|
||||||
StmtKind::For { .. } => generator.gen_for(ctx, stmt)?,
|
StmtKind::For { .. } => generator.gen_for(ctx, stmt)?,
|
||||||
StmtKind::With { .. } => generator.gen_with(ctx, stmt)?,
|
StmtKind::With { .. } => generator.gen_with(ctx, stmt)?,
|
||||||
StmtKind::AugAssign { target, op, value, .. } => {
|
StmtKind::AugAssign { target, op, value, .. } => {
|
||||||
let value = gen_binop_expr(generator, ctx, target, *op, value, stmt.location, true)?;
|
let value = gen_binop_expr(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
target,
|
||||||
|
Binop::aug_assign(*op),
|
||||||
|
value,
|
||||||
|
stmt.location,
|
||||||
|
)?;
|
||||||
generator.gen_assign(ctx, target, value.unwrap())?;
|
generator.gen_assign(ctx, target, value.unwrap())?;
|
||||||
}
|
}
|
||||||
StmtKind::Try { .. } => gen_try(generator, ctx, stmt)?,
|
StmtKind::Try { .. } => gen_try(generator, ctx, stmt)?,
|
||||||
|
|
|
@ -23,3 +23,4 @@ pub mod codegen;
|
||||||
pub mod symbol_resolver;
|
pub mod symbol_resolver;
|
||||||
pub mod toplevel;
|
pub mod toplevel;
|
||||||
pub mod typecheck;
|
pub mod typecheck;
|
||||||
|
pub mod util;
|
|
@ -3,7 +3,6 @@ use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::{collections::HashMap, collections::HashSet, fmt::Display};
|
use std::{collections::HashMap, collections::HashSet, fmt::Display};
|
||||||
|
|
||||||
use crate::typecheck::typedef::GenericObjectType;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
codegen::{CodeGenContext, CodeGenerator},
|
codegen::{CodeGenContext, CodeGenerator},
|
||||||
toplevel::{type_annotation::TypeAnnotation, DefinitionId, TopLevelDef},
|
toplevel::{type_annotation::TypeAnnotation, DefinitionId, TopLevelDef},
|
||||||
|
@ -44,7 +43,7 @@ impl SymbolValue {
|
||||||
) -> Result<Self, String> {
|
) -> Result<Self, String> {
|
||||||
match constant {
|
match constant {
|
||||||
Constant::None => {
|
Constant::None => {
|
||||||
if unifier.unioned(expected_ty, primitives.option.into()) {
|
if unifier.unioned(expected_ty, primitives.option) {
|
||||||
Ok(SymbolValue::OptionNone)
|
Ok(SymbolValue::OptionNone)
|
||||||
} else {
|
} else {
|
||||||
Err(format!("Expected {expected_ty:?}, but got Option"))
|
Err(format!("Expected {expected_ty:?}, but got Option"))
|
||||||
|
@ -158,7 +157,7 @@ impl SymbolValue {
|
||||||
let vs_tys = vs.iter().map(|v| v.get_type(primitives, unifier)).collect::<Vec<_>>();
|
let vs_tys = vs.iter().map(|v| v.get_type(primitives, unifier)).collect::<Vec<_>>();
|
||||||
unifier.add_ty(TypeEnum::TTuple { ty: vs_tys })
|
unifier.add_ty(TypeEnum::TTuple { ty: vs_tys })
|
||||||
}
|
}
|
||||||
SymbolValue::OptionSome(_) | SymbolValue::OptionNone => primitives.option.into(),
|
SymbolValue::OptionSome(_) | SymbolValue::OptionNone => primitives.option,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,13 +183,13 @@ impl SymbolValue {
|
||||||
TypeAnnotation::Tuple(vs_tys)
|
TypeAnnotation::Tuple(vs_tys)
|
||||||
}
|
}
|
||||||
SymbolValue::OptionNone => TypeAnnotation::CustomClass {
|
SymbolValue::OptionNone => TypeAnnotation::CustomClass {
|
||||||
id: primitives.option.obj_id(unifier),
|
id: primitives.option.obj_id(unifier).unwrap(),
|
||||||
params: Vec::default(),
|
params: Vec::default(),
|
||||||
},
|
},
|
||||||
SymbolValue::OptionSome(v) => {
|
SymbolValue::OptionSome(v) => {
|
||||||
let ty = v.get_type_annotation(primitives, unifier);
|
let ty = v.get_type_annotation(primitives, unifier);
|
||||||
TypeAnnotation::CustomClass {
|
TypeAnnotation::CustomClass {
|
||||||
id: primitives.option.obj_id(unifier),
|
id: primitives.option.obj_id(unifier).unwrap(),
|
||||||
params: vec![ty],
|
params: vec![ty],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -383,13 +382,12 @@ pub trait SymbolResolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
static IDENTIFIER_ID: [StrRef; 12] = [
|
static IDENTIFIER_ID: [StrRef; 11] = [
|
||||||
"int32".into(),
|
"int32".into(),
|
||||||
"int64".into(),
|
"int64".into(),
|
||||||
"float".into(),
|
"float".into(),
|
||||||
"bool".into(),
|
"bool".into(),
|
||||||
"virtual".into(),
|
"virtual".into(),
|
||||||
"list".into(),
|
|
||||||
"tuple".into(),
|
"tuple".into(),
|
||||||
"str".into(),
|
"str".into(),
|
||||||
"Exception".into(),
|
"Exception".into(),
|
||||||
|
@ -414,13 +412,12 @@ pub fn parse_type_annotation<T>(
|
||||||
let float_id = ids[2];
|
let float_id = ids[2];
|
||||||
let bool_id = ids[3];
|
let bool_id = ids[3];
|
||||||
let virtual_id = ids[4];
|
let virtual_id = ids[4];
|
||||||
let list_id = ids[5];
|
let tuple_id = ids[5];
|
||||||
let tuple_id = ids[6];
|
let str_id = ids[6];
|
||||||
let str_id = ids[7];
|
let exn_id = ids[7];
|
||||||
let exn_id = ids[8];
|
let uint32_id = ids[8];
|
||||||
let uint32_id = ids[9];
|
let uint64_id = ids[9];
|
||||||
let uint64_id = ids[10];
|
let literal_id = ids[10];
|
||||||
let literal_id = ids[11];
|
|
||||||
|
|
||||||
let name_handling = |id: &StrRef, loc: Location, unifier: &mut Unifier| {
|
let name_handling = |id: &StrRef, loc: Location, unifier: &mut Unifier| {
|
||||||
if *id == int32_id {
|
if *id == int32_id {
|
||||||
|
@ -477,9 +474,6 @@ pub fn parse_type_annotation<T>(
|
||||||
if *id == virtual_id {
|
if *id == virtual_id {
|
||||||
let ty = parse_type_annotation(resolver, top_level_defs, unifier, primitives, slice)?;
|
let ty = parse_type_annotation(resolver, top_level_defs, unifier, primitives, slice)?;
|
||||||
Ok(unifier.add_ty(TypeEnum::TVirtual { ty }))
|
Ok(unifier.add_ty(TypeEnum::TVirtual { ty }))
|
||||||
} else if *id == list_id {
|
|
||||||
let ty = parse_type_annotation(resolver, top_level_defs, unifier, primitives, slice)?;
|
|
||||||
Ok(unifier.add_ty(TypeEnum::TList { ty }))
|
|
||||||
} else if *id == tuple_id {
|
} else if *id == tuple_id {
|
||||||
if let Tuple { elts, .. } = &slice.node {
|
if let Tuple { elts, .. } = &slice.node {
|
||||||
let ty = elts
|
let ty = elts
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use std::iter::once;
|
use std::iter::once;
|
||||||
|
|
||||||
|
use crate::util::SizeVariant;
|
||||||
use helper::{debug_assert_prim_is_allowed, make_exception_fields, PrimDefDetails};
|
use helper::{debug_assert_prim_is_allowed, make_exception_fields, PrimDefDetails};
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use inkwell::{
|
use inkwell::{
|
||||||
|
@ -14,18 +15,15 @@ use strum::IntoEnumIterator;
|
||||||
use crate::{
|
use crate::{
|
||||||
codegen::{
|
codegen::{
|
||||||
builtin_fns,
|
builtin_fns,
|
||||||
classes::{
|
classes::{ArrayLikeValue, NDArrayValue, ProxyValue, RangeValue, TypedArrayLikeAccessor},
|
||||||
ArrayLikeValue, NDArrayValue, ProxyType, ProxyValue, RangeType, RangeValue,
|
|
||||||
TypedArrayLikeAccessor,
|
|
||||||
},
|
|
||||||
expr::destructure_range,
|
expr::destructure_range,
|
||||||
irrt::*,
|
irrt::*,
|
||||||
numpy::*,
|
numpy::*,
|
||||||
stmt::exn_constructor,
|
stmt::exn_constructor,
|
||||||
},
|
},
|
||||||
symbol_resolver::SymbolValue,
|
symbol_resolver::SymbolValue,
|
||||||
toplevel::helper::PrimDef,
|
toplevel::{helper::PrimDef, numpy::make_ndarray_ty},
|
||||||
typecheck::typedef::{into_var_map, TypeVar, VarMap},
|
typecheck::typedef::{into_var_map, iter_type_vars, TypeVar, VarMap},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -281,19 +279,10 @@ pub fn get_builtins(unifier: &mut Unifier, primitives: &PrimitiveStore) -> Built
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A helper enum used by [`BuiltinBuilder`]
|
fn size_variant_to_int_type(variant: SizeVariant, primitives: &PrimitiveStore) -> Type {
|
||||||
#[derive(Clone, Copy)]
|
match variant {
|
||||||
enum SizeVariant {
|
SizeVariant::Bits32 => primitives.int32,
|
||||||
Bits32,
|
SizeVariant::Bits64 => primitives.int64,
|
||||||
Bits64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SizeVariant {
|
|
||||||
fn of_int(self, primitives: &PrimitiveStore) -> Type {
|
|
||||||
match self {
|
|
||||||
SizeVariant::Bits32 => primitives.int32,
|
|
||||||
SizeVariant::Bits64 => primitives.int64,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,7 +292,12 @@ struct BuiltinBuilder<'a> {
|
||||||
|
|
||||||
is_some_ty: (Type, bool),
|
is_some_ty: (Type, bool),
|
||||||
unwrap_ty: (Type, bool),
|
unwrap_ty: (Type, bool),
|
||||||
|
option_tvar: TypeVar,
|
||||||
|
|
||||||
|
list_tvar: TypeVar,
|
||||||
|
|
||||||
|
ndarray_dtype_tvar: TypeVar,
|
||||||
|
ndarray_ndims_tvar: TypeVar,
|
||||||
ndarray_copy_ty: (Type, bool),
|
ndarray_copy_ty: (Type, bool),
|
||||||
ndarray_fill_ty: (Type, bool),
|
ndarray_fill_ty: (Type, bool),
|
||||||
|
|
||||||
|
@ -312,9 +306,9 @@ struct BuiltinBuilder<'a> {
|
||||||
num_ty: TypeVar,
|
num_ty: TypeVar,
|
||||||
num_var_map: VarMap,
|
num_var_map: VarMap,
|
||||||
|
|
||||||
ndarray_float: primitive_type::NDArrayType,
|
ndarray_float: Type,
|
||||||
ndarray_float_2d: primitive_type::NDArrayType,
|
ndarray_float_2d: Type,
|
||||||
ndarray_num_ty: primitive_type::NDArrayType,
|
ndarray_num_ty: Type,
|
||||||
|
|
||||||
float_or_ndarray_ty: TypeVar,
|
float_or_ndarray_ty: TypeVar,
|
||||||
float_or_ndarray_var_map: VarMap,
|
float_or_ndarray_var_map: VarMap,
|
||||||
|
@ -341,19 +335,24 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
} = *primitives;
|
} = *primitives;
|
||||||
|
|
||||||
// Option-related
|
// Option-related
|
||||||
let (is_some_ty, unwrap_ty) =
|
let (is_some_ty, unwrap_ty, option_tvar) =
|
||||||
if let TypeEnum::TObj { fields, .. } = &*unifier.get_ty(option.into()) {
|
if let TypeEnum::TObj { fields, params, .. } = unifier.get_ty(option).as_ref() {
|
||||||
(
|
(
|
||||||
*fields.get(&PrimDef::OptionIsSome.simple_name().into()).unwrap(),
|
*fields.get(&PrimDef::OptionIsSome.simple_name().into()).unwrap(),
|
||||||
*fields.get(&PrimDef::OptionUnwrap.simple_name().into()).unwrap(),
|
*fields.get(&PrimDef::OptionUnwrap.simple_name().into()).unwrap(),
|
||||||
|
iter_type_vars(params).next().unwrap(),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
};
|
};
|
||||||
|
|
||||||
let TypeEnum::TObj { fields: ndarray_fields, .. } = &*unifier.get_ty(ndarray.into()) else {
|
let TypeEnum::TObj { fields: ndarray_fields, params: ndarray_params, .. } =
|
||||||
|
&*unifier.get_ty(ndarray)
|
||||||
|
else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
};
|
};
|
||||||
|
let ndarray_dtype_tvar = iter_type_vars(ndarray_params).next().unwrap();
|
||||||
|
let ndarray_ndims_tvar = iter_type_vars(ndarray_params).nth(1).unwrap();
|
||||||
let ndarray_copy_ty =
|
let ndarray_copy_ty =
|
||||||
*ndarray_fields.get(&PrimDef::NDArrayCopy.simple_name().into()).unwrap();
|
*ndarray_fields.get(&PrimDef::NDArrayCopy.simple_name().into()).unwrap();
|
||||||
let ndarray_fill_ty =
|
let ndarray_fill_ty =
|
||||||
|
@ -366,8 +365,7 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
);
|
);
|
||||||
let num_var_map = into_var_map([num_ty]);
|
let num_var_map = into_var_map([num_ty]);
|
||||||
|
|
||||||
let ndarray_float =
|
let ndarray_float = make_ndarray_ty(unifier, primitives, Some(float), None);
|
||||||
primitive_type::NDArrayType::from_primitive(unifier, primitives, Some(float), None);
|
|
||||||
let ndarray_float_2d = {
|
let ndarray_float_2d = {
|
||||||
let value = match primitives.size_t {
|
let value = match primitives.size_t {
|
||||||
64 => SymbolValue::U64(2u64),
|
64 => SymbolValue::U64(2u64),
|
||||||
|
@ -376,31 +374,29 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
};
|
};
|
||||||
let ndims = unifier.add_ty(TypeEnum::TLiteral { values: vec![value], loc: None });
|
let ndims = unifier.add_ty(TypeEnum::TLiteral { values: vec![value], loc: None });
|
||||||
|
|
||||||
primitive_type::NDArrayType::from_primitive(
|
make_ndarray_ty(unifier, primitives, Some(float), Some(ndims))
|
||||||
unifier,
|
|
||||||
primitives,
|
|
||||||
Some(float),
|
|
||||||
Some(ndims),
|
|
||||||
)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let ndarray_num_ty =
|
let ndarray_num_ty = make_ndarray_ty(unifier, primitives, Some(num_ty.ty), None);
|
||||||
primitive_type::NDArrayType::from_primitive(unifier, primitives, Some(num_ty.ty), None);
|
let float_or_ndarray_ty =
|
||||||
let float_or_ndarray_ty = unifier.get_fresh_var_with_range(
|
unifier.get_fresh_var_with_range(&[float, ndarray_float], Some("T".into()), None);
|
||||||
&[float, ndarray_float.into()],
|
|
||||||
Some("T".into()),
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
let float_or_ndarray_var_map = into_var_map([float_or_ndarray_ty]);
|
let float_or_ndarray_var_map = into_var_map([float_or_ndarray_ty]);
|
||||||
|
|
||||||
let num_or_ndarray_ty = unifier.get_fresh_var_with_range(
|
let num_or_ndarray_ty =
|
||||||
&[num_ty.ty, ndarray_num_ty.into()],
|
unifier.get_fresh_var_with_range(&[num_ty.ty, ndarray_num_ty], Some("T".into()), None);
|
||||||
Some("T".into()),
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
let num_or_ndarray_var_map = into_var_map([num_ty, num_or_ndarray_ty]);
|
let num_or_ndarray_var_map = into_var_map([num_ty, num_or_ndarray_ty]);
|
||||||
|
|
||||||
let list_int32 = unifier.add_ty(TypeEnum::TList { ty: int32 });
|
let list_tvar = if let TypeEnum::TObj { obj_id, params, .. } =
|
||||||
|
&*unifier.get_ty_immutable(primitives.list)
|
||||||
|
{
|
||||||
|
assert_eq!(*obj_id, PrimDef::List.id());
|
||||||
|
iter_type_vars(params).nth(0).unwrap()
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
let list_int32 = unifier
|
||||||
|
.subst(primitives.list, &into_var_map([TypeVar { id: list_tvar.id, ty: int32 }]))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let ndarray_factory_fn_shape_arg_tvar = unifier.get_fresh_var(Some("Shape".into()), None);
|
let ndarray_factory_fn_shape_arg_tvar = unifier.get_fresh_var(Some("Shape".into()), None);
|
||||||
|
|
||||||
|
@ -410,7 +406,12 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
|
|
||||||
is_some_ty,
|
is_some_ty,
|
||||||
unwrap_ty,
|
unwrap_ty,
|
||||||
|
option_tvar,
|
||||||
|
|
||||||
|
list_tvar,
|
||||||
|
|
||||||
|
ndarray_dtype_tvar,
|
||||||
|
ndarray_ndims_tvar,
|
||||||
ndarray_copy_ty,
|
ndarray_copy_ty,
|
||||||
ndarray_fill_ty,
|
ndarray_fill_ty,
|
||||||
|
|
||||||
|
@ -448,9 +449,10 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
| PrimDef::Float
|
| PrimDef::Float
|
||||||
| PrimDef::Bool
|
| PrimDef::Bool
|
||||||
| PrimDef::Str
|
| PrimDef::Str
|
||||||
| PrimDef::Range
|
|
||||||
| PrimDef::None => Self::build_simple_primitive_class(prim),
|
| PrimDef::None => Self::build_simple_primitive_class(prim),
|
||||||
|
|
||||||
|
PrimDef::Range | PrimDef::FunRangeInit => self.build_range_class_related(prim),
|
||||||
|
|
||||||
PrimDef::Exception => self.build_exception_class_related(prim),
|
PrimDef::Exception => self.build_exception_class_related(prim),
|
||||||
|
|
||||||
PrimDef::Option
|
PrimDef::Option
|
||||||
|
@ -459,6 +461,8 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
| PrimDef::OptionUnwrap
|
| PrimDef::OptionUnwrap
|
||||||
| PrimDef::FunSome => self.build_option_class_related(prim),
|
| PrimDef::FunSome => self.build_option_class_related(prim),
|
||||||
|
|
||||||
|
PrimDef::List => self.build_list_class_related(prim),
|
||||||
|
|
||||||
PrimDef::NDArray | PrimDef::NDArrayCopy | PrimDef::NDArrayFill => {
|
PrimDef::NDArray | PrimDef::NDArrayCopy | PrimDef::NDArrayFill => {
|
||||||
self.build_ndarray_class_related(prim)
|
self.build_ndarray_class_related(prim)
|
||||||
}
|
}
|
||||||
|
@ -480,7 +484,6 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
| PrimDef::FunNpEye
|
| PrimDef::FunNpEye
|
||||||
| PrimDef::FunNpIdentity => self.build_ndarray_other_factory_function(prim),
|
| PrimDef::FunNpIdentity => self.build_ndarray_other_factory_function(prim),
|
||||||
|
|
||||||
PrimDef::FunRange => self.build_range_function(),
|
|
||||||
PrimDef::FunStr => self.build_str_function(),
|
PrimDef::FunStr => self.build_str_function(),
|
||||||
|
|
||||||
PrimDef::FunFloor | PrimDef::FunFloor64 | PrimDef::FunCeil | PrimDef::FunCeil64 => {
|
PrimDef::FunFloor | PrimDef::FunFloor64 | PrimDef::FunCeil | PrimDef::FunCeil64 => {
|
||||||
|
@ -585,7 +588,6 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
PrimDef::Float,
|
PrimDef::Float,
|
||||||
PrimDef::Bool,
|
PrimDef::Bool,
|
||||||
PrimDef::Str,
|
PrimDef::Str,
|
||||||
PrimDef::Range,
|
|
||||||
PrimDef::None,
|
PrimDef::None,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
@ -593,6 +595,165 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
TopLevelComposer::make_top_level_class_def(prim.id(), None, prim.name().into(), None, None)
|
TopLevelComposer::make_top_level_class_def(prim.id(), None, prim.name().into(), None, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn build_range_class_related(&mut self, prim: PrimDef) -> TopLevelDef {
|
||||||
|
debug_assert_prim_is_allowed(prim, &[PrimDef::Range, PrimDef::FunRangeInit]);
|
||||||
|
|
||||||
|
let PrimitiveStore { int32, range, .. } = *self.primitives;
|
||||||
|
|
||||||
|
let make_ctor_signature = |unifier: &mut Unifier| {
|
||||||
|
unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
||||||
|
args: vec![
|
||||||
|
FuncArg { name: "start".into(), ty: int32, default_value: None },
|
||||||
|
FuncArg {
|
||||||
|
name: "stop".into(),
|
||||||
|
ty: int32,
|
||||||
|
// placeholder
|
||||||
|
default_value: Some(SymbolValue::I32(0)),
|
||||||
|
},
|
||||||
|
FuncArg {
|
||||||
|
name: "step".into(),
|
||||||
|
ty: int32,
|
||||||
|
default_value: Some(SymbolValue::I32(1)),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
ret: range,
|
||||||
|
vars: VarMap::default(),
|
||||||
|
}))
|
||||||
|
};
|
||||||
|
|
||||||
|
match prim {
|
||||||
|
PrimDef::Range => {
|
||||||
|
let fields = vec![
|
||||||
|
("start".into(), int32, true),
|
||||||
|
("stop".into(), int32, true),
|
||||||
|
("step".into(), int32, true),
|
||||||
|
];
|
||||||
|
let ctor_signature = make_ctor_signature(self.unifier);
|
||||||
|
|
||||||
|
TopLevelDef::Class {
|
||||||
|
name: prim.name().into(),
|
||||||
|
object_id: prim.id(),
|
||||||
|
type_vars: Vec::default(),
|
||||||
|
fields,
|
||||||
|
attributes: Vec::default(),
|
||||||
|
methods: vec![("__init__".into(), ctor_signature, PrimDef::FunRangeInit.id())],
|
||||||
|
ancestors: Vec::default(),
|
||||||
|
constructor: Some(ctor_signature),
|
||||||
|
resolver: None,
|
||||||
|
loc: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PrimDef::FunRangeInit => TopLevelDef::Function {
|
||||||
|
name: prim.name().into(),
|
||||||
|
simple_name: prim.simple_name().into(),
|
||||||
|
signature: make_ctor_signature(self.unifier),
|
||||||
|
var_id: Vec::default(),
|
||||||
|
instance_to_symbol: HashMap::default(),
|
||||||
|
instance_to_stmt: HashMap::default(),
|
||||||
|
resolver: None,
|
||||||
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|
|ctx, obj, _, args, generator| {
|
||||||
|
let (zelf_ty, zelf) = obj.unwrap();
|
||||||
|
let zelf =
|
||||||
|
zelf.to_basic_value_enum(ctx, generator, zelf_ty)?.into_pointer_value();
|
||||||
|
let zelf = RangeValue::from_ptr_val(zelf, Some("range"));
|
||||||
|
|
||||||
|
let mut start = None;
|
||||||
|
let mut stop = None;
|
||||||
|
let mut step = None;
|
||||||
|
let int32 = ctx.ctx.i32_type();
|
||||||
|
let ty_i32 = ctx.primitives.int32;
|
||||||
|
for (i, arg) in args.iter().enumerate() {
|
||||||
|
if arg.0 == Some("start".into()) {
|
||||||
|
start = Some(
|
||||||
|
arg.1
|
||||||
|
.clone()
|
||||||
|
.to_basic_value_enum(ctx, generator, ty_i32)?
|
||||||
|
.into_int_value(),
|
||||||
|
);
|
||||||
|
} else if arg.0 == Some("stop".into()) {
|
||||||
|
stop = Some(
|
||||||
|
arg.1
|
||||||
|
.clone()
|
||||||
|
.to_basic_value_enum(ctx, generator, ty_i32)?
|
||||||
|
.into_int_value(),
|
||||||
|
);
|
||||||
|
} else if arg.0 == Some("step".into()) {
|
||||||
|
step = Some(
|
||||||
|
arg.1
|
||||||
|
.clone()
|
||||||
|
.to_basic_value_enum(ctx, generator, ty_i32)?
|
||||||
|
.into_int_value(),
|
||||||
|
);
|
||||||
|
} else if i == 0 {
|
||||||
|
start = Some(
|
||||||
|
arg.1
|
||||||
|
.clone()
|
||||||
|
.to_basic_value_enum(ctx, generator, ty_i32)?
|
||||||
|
.into_int_value(),
|
||||||
|
);
|
||||||
|
} else if i == 1 {
|
||||||
|
stop = Some(
|
||||||
|
arg.1
|
||||||
|
.clone()
|
||||||
|
.to_basic_value_enum(ctx, generator, ty_i32)?
|
||||||
|
.into_int_value(),
|
||||||
|
);
|
||||||
|
} else if i == 2 {
|
||||||
|
step = Some(
|
||||||
|
arg.1
|
||||||
|
.clone()
|
||||||
|
.to_basic_value_enum(ctx, generator, ty_i32)?
|
||||||
|
.into_int_value(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let step = match step {
|
||||||
|
Some(step) => {
|
||||||
|
// assert step != 0, throw exception if not
|
||||||
|
let not_zero = ctx
|
||||||
|
.builder
|
||||||
|
.build_int_compare(
|
||||||
|
IntPredicate::NE,
|
||||||
|
step,
|
||||||
|
step.get_type().const_zero(),
|
||||||
|
"range_step_ne",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
ctx.make_assert(
|
||||||
|
generator,
|
||||||
|
not_zero,
|
||||||
|
"0:ValueError",
|
||||||
|
"range() step must not be zero",
|
||||||
|
[None, None, None],
|
||||||
|
ctx.current_loc,
|
||||||
|
);
|
||||||
|
step
|
||||||
|
}
|
||||||
|
None => int32.const_int(1, false),
|
||||||
|
};
|
||||||
|
let stop = stop.unwrap_or_else(|| {
|
||||||
|
let v = start.unwrap();
|
||||||
|
start = None;
|
||||||
|
v
|
||||||
|
});
|
||||||
|
let start = start.unwrap_or_else(|| int32.const_zero());
|
||||||
|
|
||||||
|
zelf.store_start(ctx, start);
|
||||||
|
zelf.store_end(ctx, stop);
|
||||||
|
zelf.store_step(ctx, step);
|
||||||
|
|
||||||
|
Ok(Some(zelf.as_base_value().into()))
|
||||||
|
},
|
||||||
|
)))),
|
||||||
|
loc: None,
|
||||||
|
},
|
||||||
|
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Build the class `Exception` and its associated methods.
|
/// Build the class `Exception` and its associated methods.
|
||||||
fn build_exception_class_related(&self, prim: PrimDef) -> TopLevelDef {
|
fn build_exception_class_related(&self, prim: PrimDef) -> TopLevelDef {
|
||||||
// NOTE: currently only contains the class `Exception`
|
// NOTE: currently only contains the class `Exception`
|
||||||
|
@ -634,7 +795,7 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
PrimDef::Option => TopLevelDef::Class {
|
PrimDef::Option => TopLevelDef::Class {
|
||||||
name: prim.name().into(),
|
name: prim.name().into(),
|
||||||
object_id: prim.id(),
|
object_id: prim.id(),
|
||||||
type_vars: vec![self.primitives.option.type_tvar(self.unifier).ty],
|
type_vars: vec![self.option_tvar.ty],
|
||||||
fields: Vec::default(),
|
fields: Vec::default(),
|
||||||
attributes: Vec::default(),
|
attributes: Vec::default(),
|
||||||
methods: vec![
|
methods: vec![
|
||||||
|
@ -655,7 +816,7 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
name: prim.name().into(),
|
name: prim.name().into(),
|
||||||
simple_name: prim.simple_name().into(),
|
simple_name: prim.simple_name().into(),
|
||||||
signature: self.unwrap_ty.0,
|
signature: self.unwrap_ty.0,
|
||||||
var_id: vec![self.primitives.option.type_tvar(self.unifier).id],
|
var_id: vec![self.option_tvar.id],
|
||||||
instance_to_symbol: HashMap::default(),
|
instance_to_symbol: HashMap::default(),
|
||||||
instance_to_stmt: HashMap::default(),
|
instance_to_stmt: HashMap::default(),
|
||||||
resolver: None,
|
resolver: None,
|
||||||
|
@ -669,7 +830,7 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
name: prim.name().to_string(),
|
name: prim.name().to_string(),
|
||||||
simple_name: prim.simple_name().into(),
|
simple_name: prim.simple_name().into(),
|
||||||
signature: self.is_some_ty.0,
|
signature: self.is_some_ty.0,
|
||||||
var_id: vec![self.primitives.option.type_tvar(self.unifier).id],
|
var_id: vec![self.option_tvar.id],
|
||||||
instance_to_symbol: HashMap::default(),
|
instance_to_symbol: HashMap::default(),
|
||||||
instance_to_stmt: HashMap::default(),
|
instance_to_stmt: HashMap::default(),
|
||||||
resolver: None,
|
resolver: None,
|
||||||
|
@ -700,40 +861,36 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
loc: None,
|
loc: None,
|
||||||
},
|
},
|
||||||
|
|
||||||
PrimDef::FunSome => {
|
PrimDef::FunSome => TopLevelDef::Function {
|
||||||
let option_tvar = self.primitives.option.type_tvar(self.unifier);
|
name: prim.name().into(),
|
||||||
|
simple_name: prim.simple_name().into(),
|
||||||
TopLevelDef::Function {
|
signature: self.unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
||||||
name: prim.name().into(),
|
args: vec![FuncArg {
|
||||||
simple_name: prim.simple_name().into(),
|
name: "n".into(),
|
||||||
signature: self.unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
ty: self.option_tvar.ty,
|
||||||
args: vec![FuncArg {
|
default_value: None,
|
||||||
name: "n".into(),
|
}],
|
||||||
ty: option_tvar.ty,
|
ret: self.primitives.option,
|
||||||
default_value: None,
|
vars: into_var_map([self.option_tvar]),
|
||||||
}],
|
})),
|
||||||
ret: self.primitives.option.into(),
|
var_id: vec![self.option_tvar.id],
|
||||||
vars: into_var_map([option_tvar]),
|
instance_to_symbol: HashMap::default(),
|
||||||
})),
|
instance_to_stmt: HashMap::default(),
|
||||||
var_id: vec![self.primitives.option.type_tvar(self.unifier).id],
|
resolver: None,
|
||||||
instance_to_symbol: HashMap::default(),
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
instance_to_stmt: HashMap::default(),
|
|ctx, _, fun, args, generator| {
|
||||||
resolver: None,
|
let arg_ty = fun.0.args[0].ty;
|
||||||
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
let arg_val =
|
||||||
|ctx, _, fun, args, generator| {
|
args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
||||||
let arg_ty = fun.0.args[0].ty;
|
let alloca = generator
|
||||||
let arg_val =
|
.gen_var_alloc(ctx, arg_val.get_type(), Some("alloca_some"))
|
||||||
args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
.unwrap();
|
||||||
let alloca = generator
|
ctx.builder.build_store(alloca, arg_val).unwrap();
|
||||||
.gen_var_alloc(ctx, arg_val.get_type(), Some("alloca_some"))
|
Ok(Some(alloca.into()))
|
||||||
.unwrap();
|
},
|
||||||
ctx.builder.build_store(alloca, arg_val).unwrap();
|
)))),
|
||||||
Ok(Some(alloca.into()))
|
loc: None,
|
||||||
},
|
},
|
||||||
)))),
|
|
||||||
loc: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
|
@ -741,8 +898,29 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn build_list_class_related(&self, prim: PrimDef) -> TopLevelDef {
|
||||||
|
debug_assert_prim_is_allowed(prim, &[PrimDef::List]);
|
||||||
|
|
||||||
|
match prim {
|
||||||
|
PrimDef::List => TopLevelDef::Class {
|
||||||
|
name: prim.name().into(),
|
||||||
|
object_id: prim.id(),
|
||||||
|
type_vars: vec![self.list_tvar.ty],
|
||||||
|
fields: Vec::default(),
|
||||||
|
attributes: Vec::default(),
|
||||||
|
methods: Vec::default(),
|
||||||
|
ancestors: Vec::default(),
|
||||||
|
constructor: None,
|
||||||
|
resolver: None,
|
||||||
|
loc: None,
|
||||||
|
},
|
||||||
|
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Build the class `ndarray` and its associated methods.
|
/// Build the class `ndarray` and its associated methods.
|
||||||
fn build_ndarray_class_related(&mut self, prim: PrimDef) -> TopLevelDef {
|
fn build_ndarray_class_related(&self, prim: PrimDef) -> TopLevelDef {
|
||||||
debug_assert_prim_is_allowed(
|
debug_assert_prim_is_allowed(
|
||||||
prim,
|
prim,
|
||||||
&[PrimDef::NDArray, PrimDef::NDArrayCopy, PrimDef::NDArrayFill],
|
&[PrimDef::NDArray, PrimDef::NDArrayCopy, PrimDef::NDArrayFill],
|
||||||
|
@ -752,10 +930,7 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
PrimDef::NDArray => TopLevelDef::Class {
|
PrimDef::NDArray => TopLevelDef::Class {
|
||||||
name: prim.name().into(),
|
name: prim.name().into(),
|
||||||
object_id: prim.id(),
|
object_id: prim.id(),
|
||||||
type_vars: vec![
|
type_vars: vec![self.ndarray_dtype_tvar.ty, self.ndarray_ndims_tvar.ty],
|
||||||
self.primitives.ndarray.dtype_tvar(self.unifier).ty,
|
|
||||||
self.primitives.ndarray.ndims_tvar(self.unifier).ty,
|
|
||||||
],
|
|
||||||
fields: Vec::default(),
|
fields: Vec::default(),
|
||||||
attributes: Vec::default(),
|
attributes: Vec::default(),
|
||||||
methods: vec![
|
methods: vec![
|
||||||
|
@ -772,17 +947,15 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
name: prim.name().into(),
|
name: prim.name().into(),
|
||||||
simple_name: prim.simple_name().into(),
|
simple_name: prim.simple_name().into(),
|
||||||
signature: self.ndarray_copy_ty.0,
|
signature: self.ndarray_copy_ty.0,
|
||||||
var_id: vec![
|
var_id: vec![self.ndarray_dtype_tvar.id, self.ndarray_ndims_tvar.id],
|
||||||
self.primitives.ndarray.dtype_tvar(self.unifier).id,
|
|
||||||
self.primitives.ndarray.ndims_tvar(self.unifier).id,
|
|
||||||
],
|
|
||||||
instance_to_symbol: HashMap::default(),
|
instance_to_symbol: HashMap::default(),
|
||||||
instance_to_stmt: HashMap::default(),
|
instance_to_stmt: HashMap::default(),
|
||||||
resolver: None,
|
resolver: None,
|
||||||
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|ctx, obj, fun, args, generator| {
|
|ctx, obj, fun, args, generator| {
|
||||||
gen_ndarray_copy(ctx, &obj, fun, &args, generator)
|
todo!()
|
||||||
.map(|val| Some(val.as_basic_value_enum()))
|
// gen_ndarray_copy(ctx, &obj, fun, &args, generator)
|
||||||
|
// .map(|val| Some(val.as_basic_value_enum()))
|
||||||
},
|
},
|
||||||
)))),
|
)))),
|
||||||
loc: None,
|
loc: None,
|
||||||
|
@ -792,17 +965,15 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
name: prim.name().into(),
|
name: prim.name().into(),
|
||||||
simple_name: prim.simple_name().into(),
|
simple_name: prim.simple_name().into(),
|
||||||
signature: self.ndarray_fill_ty.0,
|
signature: self.ndarray_fill_ty.0,
|
||||||
var_id: vec![
|
var_id: vec![self.ndarray_dtype_tvar.id, self.ndarray_ndims_tvar.id],
|
||||||
self.primitives.ndarray.dtype_tvar(self.unifier).id,
|
|
||||||
self.primitives.ndarray.ndims_tvar(self.unifier).id,
|
|
||||||
],
|
|
||||||
instance_to_symbol: HashMap::default(),
|
instance_to_symbol: HashMap::default(),
|
||||||
instance_to_stmt: HashMap::default(),
|
instance_to_stmt: HashMap::default(),
|
||||||
resolver: None,
|
resolver: None,
|
||||||
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|ctx, obj, fun, args, generator| {
|
|ctx, obj, fun, args, generator| {
|
||||||
gen_ndarray_fill(ctx, &obj, fun, &args, generator)?;
|
todo!()
|
||||||
Ok(None)
|
// gen_ndarray_fill(ctx, &obj, fun, &args, generator)?;
|
||||||
|
// Ok(None)
|
||||||
},
|
},
|
||||||
)))),
|
)))),
|
||||||
loc: None,
|
loc: None,
|
||||||
|
@ -882,28 +1053,17 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
);
|
);
|
||||||
|
|
||||||
// The size variant of the function determines the size of the returned int.
|
// The size variant of the function determines the size of the returned int.
|
||||||
let int_sized = size_variant.of_int(self.primitives);
|
let int_sized = size_variant_to_int_type(size_variant, self.primitives);
|
||||||
|
|
||||||
let ndarray_int_sized = primitive_type::NDArrayType::from_primitive(
|
let ndarray_int_sized =
|
||||||
self.unifier,
|
make_ndarray_ty(self.unifier, self.primitives, Some(int_sized), Some(common_ndim.ty));
|
||||||
self.primitives,
|
let ndarray_float =
|
||||||
Some(int_sized),
|
make_ndarray_ty(self.unifier, self.primitives, Some(float), Some(common_ndim.ty));
|
||||||
Some(common_ndim.ty),
|
|
||||||
);
|
|
||||||
let ndarray_float = primitive_type::NDArrayType::from_primitive(
|
|
||||||
self.unifier,
|
|
||||||
self.primitives,
|
|
||||||
Some(float),
|
|
||||||
Some(common_ndim.ty),
|
|
||||||
);
|
|
||||||
|
|
||||||
let p0_ty = self.unifier.get_fresh_var_with_range(
|
let p0_ty =
|
||||||
&[float, ndarray_float.into()],
|
self.unifier.get_fresh_var_with_range(&[float, ndarray_float], Some("T".into()), None);
|
||||||
Some("T".into()),
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
let ret_ty = self.unifier.get_fresh_var_with_range(
|
let ret_ty = self.unifier.get_fresh_var_with_range(
|
||||||
&[int_sized, ndarray_int_sized.into()],
|
&[int_sized, ndarray_int_sized],
|
||||||
Some("R".into()),
|
Some("R".into()),
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
|
@ -918,7 +1078,7 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
let arg_ty = fun.0.args[0].ty;
|
let arg_ty = fun.0.args[0].ty;
|
||||||
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
||||||
|
|
||||||
let ret_elem_ty = size_variant.of_int(&ctx.primitives);
|
let ret_elem_ty = size_variant_to_int_type(size_variant, &ctx.primitives);
|
||||||
Ok(Some(builtin_fns::call_round(generator, ctx, (arg_ty, arg), ret_elem_ty)?))
|
Ok(Some(builtin_fns::call_round(generator, ctx, (arg_ty, arg), ret_elem_ty)?))
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
@ -955,30 +1115,19 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
|
|
||||||
let ndarray_float = primitive_type::NDArrayType::from_primitive(
|
let ndarray_float =
|
||||||
self.unifier,
|
make_ndarray_ty(self.unifier, self.primitives, Some(float), Some(common_ndim.ty));
|
||||||
self.primitives,
|
|
||||||
Some(float),
|
|
||||||
Some(common_ndim.ty),
|
|
||||||
);
|
|
||||||
|
|
||||||
// The size variant of the function determines the type of int returned
|
// The size variant of the function determines the type of int returned
|
||||||
let int_sized = size_variant.of_int(self.primitives);
|
let int_sized = size_variant_to_int_type(size_variant, self.primitives);
|
||||||
let ndarray_int_sized = primitive_type::NDArrayType::from_primitive(
|
let ndarray_int_sized =
|
||||||
self.unifier,
|
make_ndarray_ty(self.unifier, self.primitives, Some(int_sized), Some(common_ndim.ty));
|
||||||
self.primitives,
|
|
||||||
Some(int_sized),
|
|
||||||
Some(common_ndim.ty),
|
|
||||||
);
|
|
||||||
|
|
||||||
let p0_ty = self.unifier.get_fresh_var_with_range(
|
let p0_ty =
|
||||||
&[float, ndarray_float.into()],
|
self.unifier.get_fresh_var_with_range(&[float, ndarray_float], Some("T".into()), None);
|
||||||
Some("T".into()),
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
|
|
||||||
let ret_ty = self.unifier.get_fresh_var_with_range(
|
let ret_ty = self.unifier.get_fresh_var_with_range(
|
||||||
&[int_sized, ndarray_int_sized.into()],
|
&[int_sized, ndarray_int_sized],
|
||||||
Some("R".into()),
|
Some("R".into()),
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
|
@ -993,7 +1142,7 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
let arg_ty = fun.0.args[0].ty;
|
let arg_ty = fun.0.args[0].ty;
|
||||||
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
||||||
|
|
||||||
let ret_elem_ty = size_variant.of_int(&ctx.primitives);
|
let ret_elem_ty = size_variant_to_int_type(size_variant, &ctx.primitives);
|
||||||
let func = match kind {
|
let func = match kind {
|
||||||
Kind::Ceil => builtin_fns::call_ceil,
|
Kind::Ceil => builtin_fns::call_ceil,
|
||||||
Kind::Floor => builtin_fns::call_floor,
|
Kind::Floor => builtin_fns::call_floor,
|
||||||
|
@ -1041,16 +1190,17 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
self.unifier,
|
self.unifier,
|
||||||
&VarMap::new(),
|
&VarMap::new(),
|
||||||
prim.name(),
|
prim.name(),
|
||||||
self.ndarray_float.into(),
|
self.ndarray_float,
|
||||||
&[(self.ndarray_factory_fn_shape_arg_tvar.ty, "shape")],
|
&[(self.ndarray_factory_fn_shape_arg_tvar.ty, "shape")],
|
||||||
Box::new(move |ctx, obj, fun, args, generator| {
|
Box::new(move |ctx, obj, fun, args, generator| {
|
||||||
let func = match prim {
|
todo!()
|
||||||
PrimDef::FunNpNDArray | PrimDef::FunNpEmpty => gen_ndarray_empty,
|
// let func = match prim {
|
||||||
PrimDef::FunNpZeros => gen_ndarray_zeros,
|
// PrimDef::FunNpNDArray | PrimDef::FunNpEmpty => gen_ndarray_empty,
|
||||||
PrimDef::FunNpOnes => gen_ndarray_ones,
|
// PrimDef::FunNpZeros => gen_ndarray_zeros,
|
||||||
_ => unreachable!(),
|
// PrimDef::FunNpOnes => gen_ndarray_ones,
|
||||||
};
|
// _ => unreachable!(),
|
||||||
func(ctx, &obj, fun, &args, generator).map(|val| Some(val.as_basic_value_enum()))
|
// };
|
||||||
|
// func(ctx, &obj, fun, &args, generator).map(|val| Some(val.as_basic_value_enum()))
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1087,7 +1237,7 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
default_value: Some(SymbolValue::U32(0)),
|
default_value: Some(SymbolValue::U32(0)),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
ret: ndarray.into(),
|
ret: ndarray,
|
||||||
vars: into_var_map([tv]),
|
vars: into_var_map([tv]),
|
||||||
})),
|
})),
|
||||||
var_id: vec![tv.id],
|
var_id: vec![tv.id],
|
||||||
|
@ -1096,8 +1246,9 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
resolver: None,
|
resolver: None,
|
||||||
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|ctx, obj, fun, args, generator| {
|
|ctx, obj, fun, args, generator| {
|
||||||
gen_ndarray_array(ctx, &obj, fun, &args, generator)
|
todo!()
|
||||||
.map(|val| Some(val.as_basic_value_enum()))
|
// gen_ndarray_array(ctx, &obj, fun, &args, generator)
|
||||||
|
// .map(|val| Some(val.as_basic_value_enum()))
|
||||||
},
|
},
|
||||||
)))),
|
)))),
|
||||||
loc: None,
|
loc: None,
|
||||||
|
@ -1110,13 +1261,14 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
self.unifier,
|
self.unifier,
|
||||||
&into_var_map([tv]),
|
&into_var_map([tv]),
|
||||||
prim.name(),
|
prim.name(),
|
||||||
self.primitives.ndarray.into(),
|
self.primitives.ndarray,
|
||||||
// We are using List[int32] here, as I don't know a way to specify an n-tuple bound on a
|
// We are using List[int32] here, as I don't know a way to specify an n-tuple bound on a
|
||||||
// type variable
|
// type variable
|
||||||
&[(self.list_int32, "shape"), (tv.ty, "fill_value")],
|
&[(self.list_int32, "shape"), (tv.ty, "fill_value")],
|
||||||
Box::new(move |ctx, obj, fun, args, generator| {
|
Box::new(move |ctx, obj, fun, args, generator| {
|
||||||
gen_ndarray_full(ctx, &obj, fun, &args, generator)
|
todo!()
|
||||||
.map(|val| Some(val.as_basic_value_enum()))
|
// gen_ndarray_full(ctx, &obj, fun, &args, generator)
|
||||||
|
// .map(|val| Some(val.as_basic_value_enum()))
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1139,7 +1291,7 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
default_value: Some(SymbolValue::I32(0)),
|
default_value: Some(SymbolValue::I32(0)),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
ret: self.ndarray_float_2d.into(),
|
ret: self.ndarray_float_2d,
|
||||||
vars: VarMap::default(),
|
vars: VarMap::default(),
|
||||||
})),
|
})),
|
||||||
var_id: Vec::default(),
|
var_id: Vec::default(),
|
||||||
|
@ -1148,8 +1300,9 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
resolver: None,
|
resolver: None,
|
||||||
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|ctx, obj, fun, args, generator| {
|
|ctx, obj, fun, args, generator| {
|
||||||
gen_ndarray_eye(ctx, &obj, fun, &args, generator)
|
todo!()
|
||||||
.map(|val| Some(val.as_basic_value_enum()))
|
// gen_ndarray_eye(ctx, &obj, fun, &args, generator)
|
||||||
|
// .map(|val| Some(val.as_basic_value_enum()))
|
||||||
},
|
},
|
||||||
)))),
|
)))),
|
||||||
loc: None,
|
loc: None,
|
||||||
|
@ -1159,142 +1312,18 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
self.unifier,
|
self.unifier,
|
||||||
&VarMap::new(),
|
&VarMap::new(),
|
||||||
prim.name(),
|
prim.name(),
|
||||||
self.ndarray_float_2d.into(),
|
self.ndarray_float_2d,
|
||||||
&[(int32, "n")],
|
&[(int32, "n")],
|
||||||
Box::new(|ctx, obj, fun, args, generator| {
|
Box::new(|ctx, obj, fun, args, generator| {
|
||||||
gen_ndarray_identity(ctx, &obj, fun, &args, generator)
|
todo!()
|
||||||
.map(|val| Some(val.as_basic_value_enum()))
|
// gen_ndarray_identity(ctx, &obj, fun, &args, generator)
|
||||||
|
// .map(|val| Some(val.as_basic_value_enum()))
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build the `range()` function.
|
|
||||||
fn build_range_function(&mut self) -> TopLevelDef {
|
|
||||||
let prim = PrimDef::FunRange;
|
|
||||||
|
|
||||||
let PrimitiveStore { int32, range, .. } = *self.primitives;
|
|
||||||
|
|
||||||
TopLevelDef::Function {
|
|
||||||
name: prim.name().into(),
|
|
||||||
simple_name: prim.simple_name().into(),
|
|
||||||
signature: self.unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
|
||||||
args: vec![
|
|
||||||
FuncArg { name: "start".into(), ty: int32, default_value: None },
|
|
||||||
FuncArg {
|
|
||||||
name: "stop".into(),
|
|
||||||
ty: int32,
|
|
||||||
// placeholder
|
|
||||||
default_value: Some(SymbolValue::I32(0)),
|
|
||||||
},
|
|
||||||
FuncArg {
|
|
||||||
name: "step".into(),
|
|
||||||
ty: int32,
|
|
||||||
default_value: Some(SymbolValue::I32(1)),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
ret: range,
|
|
||||||
vars: VarMap::default(),
|
|
||||||
})),
|
|
||||||
var_id: Vec::default(),
|
|
||||||
instance_to_symbol: HashMap::default(),
|
|
||||||
instance_to_stmt: HashMap::default(),
|
|
||||||
resolver: None,
|
|
||||||
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
|
||||||
|ctx, _, _, args, generator| {
|
|
||||||
let mut start = None;
|
|
||||||
let mut stop = None;
|
|
||||||
let mut step = None;
|
|
||||||
let int32 = ctx.ctx.i32_type();
|
|
||||||
let ty_i32 = ctx.primitives.int32;
|
|
||||||
for (i, arg) in args.iter().enumerate() {
|
|
||||||
if arg.0 == Some("start".into()) {
|
|
||||||
start = Some(
|
|
||||||
arg.1
|
|
||||||
.clone()
|
|
||||||
.to_basic_value_enum(ctx, generator, ty_i32)?
|
|
||||||
.into_int_value(),
|
|
||||||
);
|
|
||||||
} else if arg.0 == Some("stop".into()) {
|
|
||||||
stop = Some(
|
|
||||||
arg.1
|
|
||||||
.clone()
|
|
||||||
.to_basic_value_enum(ctx, generator, ty_i32)?
|
|
||||||
.into_int_value(),
|
|
||||||
);
|
|
||||||
} else if arg.0 == Some("step".into()) {
|
|
||||||
step = Some(
|
|
||||||
arg.1
|
|
||||||
.clone()
|
|
||||||
.to_basic_value_enum(ctx, generator, ty_i32)?
|
|
||||||
.into_int_value(),
|
|
||||||
);
|
|
||||||
} else if i == 0 {
|
|
||||||
start = Some(
|
|
||||||
arg.1
|
|
||||||
.clone()
|
|
||||||
.to_basic_value_enum(ctx, generator, ty_i32)?
|
|
||||||
.into_int_value(),
|
|
||||||
);
|
|
||||||
} else if i == 1 {
|
|
||||||
stop = Some(
|
|
||||||
arg.1
|
|
||||||
.clone()
|
|
||||||
.to_basic_value_enum(ctx, generator, ty_i32)?
|
|
||||||
.into_int_value(),
|
|
||||||
);
|
|
||||||
} else if i == 2 {
|
|
||||||
step = Some(
|
|
||||||
arg.1
|
|
||||||
.clone()
|
|
||||||
.to_basic_value_enum(ctx, generator, ty_i32)?
|
|
||||||
.into_int_value(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let step = match step {
|
|
||||||
Some(step) => {
|
|
||||||
// assert step != 0, throw exception if not
|
|
||||||
let not_zero = ctx
|
|
||||||
.builder
|
|
||||||
.build_int_compare(
|
|
||||||
IntPredicate::NE,
|
|
||||||
step,
|
|
||||||
step.get_type().const_zero(),
|
|
||||||
"range_step_ne",
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
ctx.make_assert(
|
|
||||||
generator,
|
|
||||||
not_zero,
|
|
||||||
"0:ValueError",
|
|
||||||
"range() step must not be zero",
|
|
||||||
[None, None, None],
|
|
||||||
ctx.current_loc,
|
|
||||||
);
|
|
||||||
step
|
|
||||||
}
|
|
||||||
None => int32.const_int(1, false),
|
|
||||||
};
|
|
||||||
let stop = stop.unwrap_or_else(|| {
|
|
||||||
let v = start.unwrap();
|
|
||||||
start = None;
|
|
||||||
v
|
|
||||||
});
|
|
||||||
let start = start.unwrap_or_else(|| int32.const_zero());
|
|
||||||
|
|
||||||
let ptr = RangeType::new(ctx.ctx).new_value(generator, ctx, Some("range"));
|
|
||||||
ptr.store_start(ctx, start);
|
|
||||||
ptr.store_end(ctx, stop);
|
|
||||||
ptr.store_step(ctx, step);
|
|
||||||
Ok(Some(ptr.as_base_value().into()))
|
|
||||||
},
|
|
||||||
)))),
|
|
||||||
loc: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Build the `str()` function.
|
/// Build the `str()` function.
|
||||||
fn build_str_function(&mut self) -> TopLevelDef {
|
fn build_str_function(&mut self) -> TopLevelDef {
|
||||||
let prim = PrimDef::FunStr;
|
let prim = PrimDef::FunStr;
|
||||||
|
@ -1372,17 +1401,18 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
let PrimitiveStore { uint64, int32, .. } = *self.primitives;
|
let PrimitiveStore { uint64, int32, .. } = *self.primitives;
|
||||||
|
|
||||||
let tvar = self.unifier.get_fresh_var(Some("L".into()), None);
|
let tvar = self.unifier.get_fresh_var(Some("L".into()), None);
|
||||||
let list = self.unifier.add_ty(TypeEnum::TList { ty: tvar.ty });
|
let list = self
|
||||||
|
.unifier
|
||||||
|
.subst(
|
||||||
|
self.primitives.list,
|
||||||
|
&into_var_map([TypeVar { id: self.list_tvar.id, ty: tvar.ty }]),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
let ndims = self.unifier.get_fresh_const_generic_var(uint64, Some("N".into()), None);
|
let ndims = self.unifier.get_fresh_const_generic_var(uint64, Some("N".into()), None);
|
||||||
let ndarray = primitive_type::NDArrayType::from_primitive(
|
let ndarray = make_ndarray_ty(self.unifier, self.primitives, Some(tvar.ty), Some(ndims.ty));
|
||||||
self.unifier,
|
|
||||||
self.primitives,
|
|
||||||
Some(tvar.ty),
|
|
||||||
Some(ndims.ty),
|
|
||||||
);
|
|
||||||
|
|
||||||
let arg_ty = self.unifier.get_fresh_var_with_range(
|
let arg_ty = self.unifier.get_fresh_var_with_range(
|
||||||
&[list, ndarray.into(), self.primitives.range],
|
&[list, ndarray, self.primitives.range],
|
||||||
Some("I".into()),
|
Some("I".into()),
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
|
@ -1409,7 +1439,7 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
Some(calculate_len_for_slice_range(generator, ctx, start, end, step).into())
|
Some(calculate_len_for_slice_range(generator, ctx, start, end, step).into())
|
||||||
} else {
|
} else {
|
||||||
match &*ctx.unifier.get_ty_immutable(arg_ty) {
|
match &*ctx.unifier.get_ty_immutable(arg_ty) {
|
||||||
TypeEnum::TList { .. } => {
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::List.id() => {
|
||||||
let int32 = ctx.ctx.i32_type();
|
let int32 = ctx.ctx.i32_type();
|
||||||
let zero = int32.const_zero();
|
let zero = int32.const_zero();
|
||||||
let len = ctx
|
let len = ctx
|
||||||
|
@ -1840,13 +1870,8 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_type_or_ndarray_ty(&mut self, scalar_ty: Type) -> TypeVar {
|
fn new_type_or_ndarray_ty(&mut self, scalar_ty: Type) -> TypeVar {
|
||||||
let ndarray = primitive_type::NDArrayType::from_primitive(
|
let ndarray = make_ndarray_ty(self.unifier, self.primitives, Some(scalar_ty), None);
|
||||||
self.unifier,
|
|
||||||
self.primitives,
|
|
||||||
Some(scalar_ty),
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
|
|
||||||
self.unifier.get_fresh_var_with_range(&[scalar_ty, ndarray.into()], Some("T".into()), None)
|
self.unifier.get_fresh_var_with_range(&[scalar_ty, ndarray], Some("T".into()), None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,18 @@
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
|
||||||
use super::*;
|
|
||||||
use crate::symbol_resolver::SymbolValue;
|
use crate::symbol_resolver::SymbolValue;
|
||||||
use crate::toplevel::primitive_type::{NDArrayType, OptionType};
|
use crate::toplevel::numpy::unpack_ndarray_var_tys;
|
||||||
use crate::typecheck::typedef::{into_var_map, GenericObjectType, Mapping, TypeVarId, VarMap};
|
use crate::typecheck::typedef::{into_var_map, iter_type_vars, Mapping, TypeVarId, VarMap};
|
||||||
use nac3parser::ast::{Constant, Location};
|
use nac3parser::ast::{Constant, Location};
|
||||||
use strum::IntoEnumIterator;
|
use strum::IntoEnumIterator;
|
||||||
use strum_macros::EnumIter;
|
use strum_macros::EnumIter;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
/// All primitive types and functions in nac3core.
|
/// All primitive types and functions in nac3core.
|
||||||
#[derive(Clone, Copy, Debug, EnumIter, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, EnumIter, PartialEq, Eq)]
|
||||||
pub enum PrimDef {
|
pub enum PrimDef {
|
||||||
|
// Classes
|
||||||
Int32,
|
Int32,
|
||||||
Int64,
|
Int64,
|
||||||
Float,
|
Float,
|
||||||
|
@ -22,10 +24,13 @@ pub enum PrimDef {
|
||||||
UInt32,
|
UInt32,
|
||||||
UInt64,
|
UInt64,
|
||||||
Option,
|
Option,
|
||||||
|
List,
|
||||||
|
NDArray,
|
||||||
|
|
||||||
|
// Member Functions
|
||||||
OptionIsSome,
|
OptionIsSome,
|
||||||
OptionIsNone,
|
OptionIsNone,
|
||||||
OptionUnwrap,
|
OptionUnwrap,
|
||||||
NDArray,
|
|
||||||
NDArrayCopy,
|
NDArrayCopy,
|
||||||
NDArrayFill,
|
NDArrayFill,
|
||||||
FunInt32,
|
FunInt32,
|
||||||
|
@ -44,7 +49,7 @@ pub enum PrimDef {
|
||||||
FunRound,
|
FunRound,
|
||||||
FunRound64,
|
FunRound64,
|
||||||
FunNpRound,
|
FunNpRound,
|
||||||
FunRange,
|
FunRangeInit,
|
||||||
FunStr,
|
FunStr,
|
||||||
FunBool,
|
FunBool,
|
||||||
FunFloor,
|
FunFloor,
|
||||||
|
@ -98,6 +103,8 @@ pub enum PrimDef {
|
||||||
FunNpLdExp,
|
FunNpLdExp,
|
||||||
FunNpHypot,
|
FunNpHypot,
|
||||||
FunNpNextAfter,
|
FunNpNextAfter,
|
||||||
|
|
||||||
|
// Top-Level Functions
|
||||||
FunSome,
|
FunSome,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,6 +183,7 @@ impl PrimDef {
|
||||||
PrimDef::OptionIsSome => fun("Option.is_some", Some("is_some")),
|
PrimDef::OptionIsSome => fun("Option.is_some", Some("is_some")),
|
||||||
PrimDef::OptionIsNone => fun("Option.is_none", Some("is_none")),
|
PrimDef::OptionIsNone => fun("Option.is_none", Some("is_none")),
|
||||||
PrimDef::OptionUnwrap => fun("Option.unwrap", Some("unwrap")),
|
PrimDef::OptionUnwrap => fun("Option.unwrap", Some("unwrap")),
|
||||||
|
PrimDef::List => class("list"),
|
||||||
PrimDef::NDArray => class("ndarray"),
|
PrimDef::NDArray => class("ndarray"),
|
||||||
PrimDef::NDArrayCopy => fun("ndarray.copy", Some("copy")),
|
PrimDef::NDArrayCopy => fun("ndarray.copy", Some("copy")),
|
||||||
PrimDef::NDArrayFill => fun("ndarray.fill", Some("fill")),
|
PrimDef::NDArrayFill => fun("ndarray.fill", Some("fill")),
|
||||||
|
@ -195,7 +203,7 @@ impl PrimDef {
|
||||||
PrimDef::FunRound => fun("round", None),
|
PrimDef::FunRound => fun("round", None),
|
||||||
PrimDef::FunRound64 => fun("round64", None),
|
PrimDef::FunRound64 => fun("round64", None),
|
||||||
PrimDef::FunNpRound => fun("np_round", None),
|
PrimDef::FunNpRound => fun("np_round", None),
|
||||||
PrimDef::FunRange => fun("range", None),
|
PrimDef::FunRangeInit => fun("range.__init__", Some("__init__")),
|
||||||
PrimDef::FunStr => fun("str", None),
|
PrimDef::FunStr => fun("str", None),
|
||||||
PrimDef::FunBool => fun("bool", None),
|
PrimDef::FunBool => fun("bool", None),
|
||||||
PrimDef::FunFloor => fun("floor", None),
|
PrimDef::FunFloor => fun("floor", None),
|
||||||
|
@ -353,7 +361,13 @@ impl TopLevelComposer {
|
||||||
});
|
});
|
||||||
let range = unifier.add_ty(TypeEnum::TObj {
|
let range = unifier.add_ty(TypeEnum::TObj {
|
||||||
obj_id: PrimDef::Range.id(),
|
obj_id: PrimDef::Range.id(),
|
||||||
fields: HashMap::new(),
|
fields: [
|
||||||
|
("start".into(), (int32, true)),
|
||||||
|
("stop".into(), (int32, true)),
|
||||||
|
("step".into(), (int32, true)),
|
||||||
|
]
|
||||||
|
.into_iter()
|
||||||
|
.collect(),
|
||||||
params: VarMap::new(),
|
params: VarMap::new(),
|
||||||
});
|
});
|
||||||
let str = unifier.add_ty(TypeEnum::TObj {
|
let str = unifier.add_ty(TypeEnum::TObj {
|
||||||
|
@ -402,7 +416,6 @@ impl TopLevelComposer {
|
||||||
.collect::<HashMap<_, _>>(),
|
.collect::<HashMap<_, _>>(),
|
||||||
params: into_var_map([option_type_var]),
|
params: into_var_map([option_type_var]),
|
||||||
});
|
});
|
||||||
let option = OptionType::create(option, &mut unifier);
|
|
||||||
|
|
||||||
let size_t_ty = match size_t {
|
let size_t_ty = match size_t {
|
||||||
32 => uint32,
|
32 => uint32,
|
||||||
|
@ -410,6 +423,13 @@ impl TopLevelComposer {
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let list_elem_tvar = unifier.get_fresh_var(Some("list_elem".into()), None);
|
||||||
|
let list = unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id: PrimDef::List.id(),
|
||||||
|
fields: Mapping::new(),
|
||||||
|
params: into_var_map([list_elem_tvar]),
|
||||||
|
});
|
||||||
|
|
||||||
let ndarray_dtype_tvar = unifier.get_fresh_var(Some("ndarray_dtype".into()), None);
|
let ndarray_dtype_tvar = unifier.get_fresh_var(Some("ndarray_dtype".into()), None);
|
||||||
let ndarray_ndims_tvar =
|
let ndarray_ndims_tvar =
|
||||||
unifier.get_fresh_const_generic_var(size_t_ty, Some("ndarray_ndims".into()), None);
|
unifier.get_fresh_const_generic_var(size_t_ty, Some("ndarray_ndims".into()), None);
|
||||||
|
@ -436,9 +456,8 @@ impl TopLevelComposer {
|
||||||
]),
|
]),
|
||||||
params: into_var_map([ndarray_dtype_tvar, ndarray_ndims_tvar]),
|
params: into_var_map([ndarray_dtype_tvar, ndarray_ndims_tvar]),
|
||||||
});
|
});
|
||||||
let ndarray = NDArrayType::create(ndarray, &mut unifier);
|
|
||||||
|
|
||||||
unifier.unify(ndarray_copy_fun_ret_ty.ty, ndarray.into()).unwrap();
|
unifier.unify(ndarray_copy_fun_ret_ty.ty, ndarray).unwrap();
|
||||||
|
|
||||||
let primitives = PrimitiveStore {
|
let primitives = PrimitiveStore {
|
||||||
int32,
|
int32,
|
||||||
|
@ -452,6 +471,7 @@ impl TopLevelComposer {
|
||||||
str,
|
str,
|
||||||
exception,
|
exception,
|
||||||
option,
|
option,
|
||||||
|
list,
|
||||||
ndarray,
|
ndarray,
|
||||||
size_t,
|
size_t,
|
||||||
};
|
};
|
||||||
|
@ -748,7 +768,7 @@ impl TopLevelComposer {
|
||||||
TypeAnnotation::CustomClass { id: e_id, params: e_param },
|
TypeAnnotation::CustomClass { id: e_id, params: e_param },
|
||||||
) => {
|
) => {
|
||||||
*f_id == *e_id
|
*f_id == *e_id
|
||||||
&& *f_id == primitive.option.obj_id(unifier)
|
&& *f_id == primitive.option.obj_id(unifier).unwrap()
|
||||||
&& (f_param.is_empty()
|
&& (f_param.is_empty()
|
||||||
|| (f_param.len() == 1
|
|| (f_param.len() == 1
|
||||||
&& e_param.len() == 1
|
&& e_param.len() == 1
|
||||||
|
@ -886,10 +906,12 @@ pub fn parse_parameter_default_value(
|
||||||
pub fn arraylike_flatten_element_type(unifier: &mut Unifier, ty: Type) -> Type {
|
pub fn arraylike_flatten_element_type(unifier: &mut Unifier, ty: Type) -> Type {
|
||||||
match &*unifier.get_ty(ty) {
|
match &*unifier.get_ty(ty) {
|
||||||
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
||||||
NDArrayType::create(ty, unifier).dtype_tvar(unifier).ty
|
unpack_ndarray_var_tys(unifier, ty).0
|
||||||
}
|
}
|
||||||
|
|
||||||
TypeEnum::TList { ty } => arraylike_flatten_element_type(unifier, *ty),
|
TypeEnum::TObj { obj_id, params, .. } if *obj_id == PrimDef::List.id() => {
|
||||||
|
arraylike_flatten_element_type(unifier, iter_type_vars(params).next().unwrap().ty)
|
||||||
|
}
|
||||||
_ => ty,
|
_ => ty,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -898,7 +920,7 @@ pub fn arraylike_flatten_element_type(unifier: &mut Unifier, ty: Type) -> Type {
|
||||||
pub fn arraylike_get_ndims(unifier: &mut Unifier, ty: Type) -> u64 {
|
pub fn arraylike_get_ndims(unifier: &mut Unifier, ty: Type) -> u64 {
|
||||||
match &*unifier.get_ty(ty) {
|
match &*unifier.get_ty(ty) {
|
||||||
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
||||||
let ndims = NDArrayType::create(ty, unifier).ndims_tvar(unifier).ty;
|
let ndims = unpack_ndarray_var_tys(unifier, ty).1;
|
||||||
let TypeEnum::TLiteral { values, .. } = &*unifier.get_ty_immutable(ndims) else {
|
let TypeEnum::TLiteral { values, .. } = &*unifier.get_ty_immutable(ndims) else {
|
||||||
panic!("Expected TLiteral for ndarray.ndims, got {}", unifier.stringify(ndims))
|
panic!("Expected TLiteral for ndarray.ndims, got {}", unifier.stringify(ndims))
|
||||||
};
|
};
|
||||||
|
@ -910,7 +932,9 @@ pub fn arraylike_get_ndims(unifier: &mut Unifier, ty: Type) -> u64 {
|
||||||
u64::try_from(values[0].clone()).unwrap()
|
u64::try_from(values[0].clone()).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
TypeEnum::TList { ty } => arraylike_get_ndims(unifier, *ty) + 1,
|
TypeEnum::TObj { obj_id, params, .. } if *obj_id == PrimDef::List.id() => {
|
||||||
|
arraylike_get_ndims(unifier, iter_type_vars(params).next().unwrap().ty) + 1
|
||||||
|
}
|
||||||
_ => 0,
|
_ => 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ pub struct DefinitionId(pub usize);
|
||||||
pub mod builtins;
|
pub mod builtins;
|
||||||
pub mod composer;
|
pub mod composer;
|
||||||
pub mod helper;
|
pub mod helper;
|
||||||
pub mod primitive_type;
|
pub mod numpy;
|
||||||
pub mod type_annotation;
|
pub mod type_annotation;
|
||||||
use composer::*;
|
use composer::*;
|
||||||
use type_annotation::*;
|
use type_annotation::*;
|
||||||
|
|
|
@ -0,0 +1,85 @@
|
||||||
|
use crate::{
|
||||||
|
toplevel::helper::PrimDef,
|
||||||
|
typecheck::{
|
||||||
|
type_inferencer::PrimitiveStore,
|
||||||
|
typedef::{Type, TypeEnum, TypeVarId, Unifier, VarMap},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
/// Creates a `ndarray` [`Type`] with the given type arguments.
|
||||||
|
///
|
||||||
|
/// * `dtype` - The element type of the `ndarray`, or [`None`] if the type variable is not
|
||||||
|
/// specialized.
|
||||||
|
/// * `ndims` - The number of dimensions of the `ndarray`, or [`None`] if the type variable is not
|
||||||
|
/// specialized.
|
||||||
|
pub fn make_ndarray_ty(
|
||||||
|
unifier: &mut Unifier,
|
||||||
|
primitives: &PrimitiveStore,
|
||||||
|
dtype: Option<Type>,
|
||||||
|
ndims: Option<Type>,
|
||||||
|
) -> Type {
|
||||||
|
subst_ndarray_tvars(unifier, primitives.ndarray, dtype, ndims)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Substitutes type variables in `ndarray`.
|
||||||
|
///
|
||||||
|
/// * `dtype` - The element type of the `ndarray`, or [`None`] if the type variable is not
|
||||||
|
/// specialized.
|
||||||
|
/// * `ndims` - The number of dimensions of the `ndarray`, or [`None`] if the type variable is not
|
||||||
|
/// specialized.
|
||||||
|
pub fn subst_ndarray_tvars(
|
||||||
|
unifier: &mut Unifier,
|
||||||
|
ndarray: Type,
|
||||||
|
dtype: Option<Type>,
|
||||||
|
ndims: Option<Type>,
|
||||||
|
) -> Type {
|
||||||
|
let TypeEnum::TObj { obj_id, params, .. } = &*unifier.get_ty_immutable(ndarray) else {
|
||||||
|
panic!("Expected `ndarray` to be TObj, but got {}", unifier.stringify(ndarray))
|
||||||
|
};
|
||||||
|
debug_assert_eq!(*obj_id, PrimDef::NDArray.id());
|
||||||
|
|
||||||
|
if dtype.is_none() && ndims.is_none() {
|
||||||
|
return ndarray;
|
||||||
|
}
|
||||||
|
|
||||||
|
let tvar_ids = params.iter().map(|(obj_id, _)| *obj_id).collect_vec();
|
||||||
|
debug_assert_eq!(tvar_ids.len(), 2);
|
||||||
|
|
||||||
|
let mut tvar_subst = VarMap::new();
|
||||||
|
if let Some(dtype) = dtype {
|
||||||
|
tvar_subst.insert(tvar_ids[0], dtype);
|
||||||
|
}
|
||||||
|
if let Some(ndims) = ndims {
|
||||||
|
tvar_subst.insert(tvar_ids[1], ndims);
|
||||||
|
}
|
||||||
|
|
||||||
|
unifier.subst(ndarray, &tvar_subst).unwrap_or(ndarray)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unpack_ndarray_tvars(unifier: &mut Unifier, ndarray: Type) -> Vec<(TypeVarId, Type)> {
|
||||||
|
let TypeEnum::TObj { obj_id, params, .. } = &*unifier.get_ty_immutable(ndarray) else {
|
||||||
|
panic!("Expected `ndarray` to be TObj, but got {}", unifier.stringify(ndarray))
|
||||||
|
};
|
||||||
|
debug_assert_eq!(*obj_id, PrimDef::NDArray.id());
|
||||||
|
debug_assert_eq!(params.len(), 2);
|
||||||
|
|
||||||
|
params
|
||||||
|
.iter()
|
||||||
|
.sorted_by_key(|(obj_id, _)| *obj_id)
|
||||||
|
.map(|(var_id, ty)| (*var_id, *ty))
|
||||||
|
.collect_vec()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unpacks the type variable IDs of `ndarray` into a tuple. The elements of the tuple corresponds
|
||||||
|
/// to `dtype` (the element type) and `ndims` (the number of dimensions) of the `ndarray`
|
||||||
|
/// respectively.
|
||||||
|
pub fn unpack_ndarray_var_ids(unifier: &mut Unifier, ndarray: Type) -> (TypeVarId, TypeVarId) {
|
||||||
|
unpack_ndarray_tvars(unifier, ndarray).into_iter().map(|v| v.0).collect_tuple().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unpacks the type variables of `ndarray` into a tuple. The elements of the tuple corresponds to
|
||||||
|
/// `dtype` (the element type) and `ndims` (the number of dimensions) of the `ndarray` respectively.
|
||||||
|
pub fn unpack_ndarray_var_tys(unifier: &mut Unifier, ndarray: Type) -> (Type, Type) {
|
||||||
|
unpack_ndarray_tvars(unifier, ndarray).into_iter().map(|v| v.1).collect_tuple().unwrap()
|
||||||
|
}
|
|
@ -1,98 +0,0 @@
|
||||||
use crate::toplevel::helper::PrimDef;
|
|
||||||
use crate::typecheck::type_inferencer::PrimitiveStore;
|
|
||||||
use crate::typecheck::typedef::{GenericObjectType, Type, TypeVar, Unifier, VarMap};
|
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
|
||||||
pub struct OptionType(Type);
|
|
||||||
|
|
||||||
impl OptionType {
|
|
||||||
pub fn from_primitive(
|
|
||||||
unifier: &mut Unifier,
|
|
||||||
primitives: &PrimitiveStore,
|
|
||||||
type_ty: Option<Type>,
|
|
||||||
) -> Self {
|
|
||||||
primitives.option.subst(unifier, type_ty)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn type_tvar(&self, unifier: &mut Unifier) -> TypeVar {
|
|
||||||
self.get_var_at(unifier, 0).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
pub fn subst(&self, unifier: &mut Unifier, type_ty: Option<Type>) -> Self {
|
|
||||||
let new_vars = [(self.type_tvar(unifier).id, type_ty)]
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|(id, ty)| ty.map(|ty| (id, ty)))
|
|
||||||
.collect::<VarMap>();
|
|
||||||
|
|
||||||
let new_ty = unifier.subst(self.get_type(), &new_vars).unwrap_or(self.get_type());
|
|
||||||
OptionType(new_ty)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GenericObjectType for OptionType {
|
|
||||||
fn try_create(ty: Type, unifier: &mut Unifier) -> Option<Self> {
|
|
||||||
if ty.obj_id(unifier).is_some_and(|id| id == PrimDef::Option.id()) {
|
|
||||||
Some(OptionType(ty))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_type(&self) -> Type {
|
|
||||||
self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
|
||||||
pub struct NDArrayType(Type);
|
|
||||||
|
|
||||||
impl NDArrayType {
|
|
||||||
pub fn from_primitive(
|
|
||||||
unifier: &mut Unifier,
|
|
||||||
primitives: &PrimitiveStore,
|
|
||||||
dtype: Option<Type>,
|
|
||||||
ndims: Option<Type>,
|
|
||||||
) -> Self {
|
|
||||||
primitives.ndarray.subst(unifier, dtype, ndims)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn dtype_tvar(&self, unifier: &mut Unifier) -> TypeVar {
|
|
||||||
self.get_var_at(unifier, 0).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn ndims_tvar(&self, unifier: &mut Unifier) -> TypeVar {
|
|
||||||
self.get_var_at(unifier, 1).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
pub fn subst(
|
|
||||||
&self,
|
|
||||||
unifier: &mut Unifier,
|
|
||||||
dtype_ty: Option<Type>,
|
|
||||||
ndims_ty: Option<Type>,
|
|
||||||
) -> Self {
|
|
||||||
let new_vars =
|
|
||||||
[(self.dtype_tvar(unifier).id, dtype_ty), (self.ndims_tvar(unifier).id, ndims_ty)]
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|(id, ty)| ty.map(|ty| (id, ty)))
|
|
||||||
.collect::<VarMap>();
|
|
||||||
|
|
||||||
let new_ty = unifier.subst(self.get_type(), &new_vars).unwrap_or(self.get_type());
|
|
||||||
NDArrayType(new_ty)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GenericObjectType for NDArrayType {
|
|
||||||
fn try_create(ty: Type, unifier: &mut Unifier) -> Option<Self> {
|
|
||||||
if ty.obj_id(unifier).is_some_and(|id| id == PrimDef::NDArray.id()) {
|
|
||||||
Some(NDArrayType(ty))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_type(&self) -> Type {
|
|
||||||
self.0
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,7 +5,7 @@ expression: res_vec
|
||||||
[
|
[
|
||||||
"Class {\nname: \"Generic_A\",\nancestors: [\"Generic_A[V]\", \"B\"],\nfields: [\"aa\", \"a\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"foo\", \"fn[[b:T], none]\"), (\"fun\", \"fn[[a:int32], V]\")],\ntype_vars: [\"V\"]\n}\n",
|
"Class {\nname: \"Generic_A\",\nancestors: [\"Generic_A[V]\", \"B\"],\nfields: [\"aa\", \"a\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"foo\", \"fn[[b:T], none]\"), (\"fun\", \"fn[[a:int32], V]\")],\ntype_vars: [\"V\"]\n}\n",
|
||||||
"Function {\nname: \"Generic_A.__init__\",\nsig: \"fn[[], none]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"Generic_A.__init__\",\nsig: \"fn[[], none]\",\nvar_id: []\n}\n",
|
||||||
"Function {\nname: \"Generic_A.fun\",\nsig: \"fn[[a:int32], V]\",\nvar_id: [TypeVarId(240)]\n}\n",
|
"Function {\nname: \"Generic_A.fun\",\nsig: \"fn[[a:int32], V]\",\nvar_id: [TypeVarId(245)]\n}\n",
|
||||||
"Class {\nname: \"B\",\nancestors: [\"B\"],\nfields: [\"aa\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"foo\", \"fn[[b:T], none]\")],\ntype_vars: []\n}\n",
|
"Class {\nname: \"B\",\nancestors: [\"B\"],\nfields: [\"aa\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"foo\", \"fn[[b:T], none]\")],\ntype_vars: []\n}\n",
|
||||||
"Function {\nname: \"B.__init__\",\nsig: \"fn[[], none]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"B.__init__\",\nsig: \"fn[[], none]\",\nvar_id: []\n}\n",
|
||||||
"Function {\nname: \"B.foo\",\nsig: \"fn[[b:T], none]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"B.foo\",\nsig: \"fn[[b:T], none]\",\nvar_id: []\n}\n",
|
||||||
|
|
|
@ -7,7 +7,7 @@ expression: res_vec
|
||||||
"Function {\nname: \"A.__init__\",\nsig: \"fn[[t:T], none]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"A.__init__\",\nsig: \"fn[[t:T], none]\",\nvar_id: []\n}\n",
|
||||||
"Function {\nname: \"A.fun\",\nsig: \"fn[[a:int32, b:T], list[virtual[B[bool]]]]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"A.fun\",\nsig: \"fn[[a:int32, b:T], list[virtual[B[bool]]]]\",\nvar_id: []\n}\n",
|
||||||
"Function {\nname: \"A.foo\",\nsig: \"fn[[c:C], none]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"A.foo\",\nsig: \"fn[[c:C], none]\",\nvar_id: []\n}\n",
|
||||||
"Class {\nname: \"B\",\nancestors: [\"B[typevar229]\", \"A[float]\"],\nfields: [\"a\", \"b\", \"c\", \"d\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"fun\", \"fn[[a:int32, b:T], list[virtual[B[bool]]]]\"), (\"foo\", \"fn[[c:C], none]\")],\ntype_vars: [\"typevar229\"]\n}\n",
|
"Class {\nname: \"B\",\nancestors: [\"B[typevar234]\", \"A[float]\"],\nfields: [\"a\", \"b\", \"c\", \"d\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"fun\", \"fn[[a:int32, b:T], list[virtual[B[bool]]]]\"), (\"foo\", \"fn[[c:C], none]\")],\ntype_vars: [\"typevar234\"]\n}\n",
|
||||||
"Function {\nname: \"B.__init__\",\nsig: \"fn[[], none]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"B.__init__\",\nsig: \"fn[[], none]\",\nvar_id: []\n}\n",
|
||||||
"Function {\nname: \"B.fun\",\nsig: \"fn[[a:int32, b:T], list[virtual[B[bool]]]]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"B.fun\",\nsig: \"fn[[a:int32, b:T], list[virtual[B[bool]]]]\",\nvar_id: []\n}\n",
|
||||||
"Class {\nname: \"C\",\nancestors: [\"C\", \"B[bool]\", \"A[float]\"],\nfields: [\"a\", \"b\", \"c\", \"d\", \"e\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"fun\", \"fn[[a:int32, b:T], list[virtual[B[bool]]]]\"), (\"foo\", \"fn[[c:C], none]\")],\ntype_vars: []\n}\n",
|
"Class {\nname: \"C\",\nancestors: [\"C\", \"B[bool]\", \"A[float]\"],\nfields: [\"a\", \"b\", \"c\", \"d\", \"e\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"fun\", \"fn[[a:int32, b:T], list[virtual[B[bool]]]]\"), (\"foo\", \"fn[[c:C], none]\")],\ntype_vars: []\n}\n",
|
||||||
|
|
|
@ -5,8 +5,8 @@ expression: res_vec
|
||||||
[
|
[
|
||||||
"Function {\nname: \"foo\",\nsig: \"fn[[a:list[int32], b:tuple[T, float]], A[B, bool]]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"foo\",\nsig: \"fn[[a:list[int32], b:tuple[T, float]], A[B, bool]]\",\nvar_id: []\n}\n",
|
||||||
"Class {\nname: \"A\",\nancestors: [\"A[T, V]\"],\nfields: [\"a\", \"b\"],\nmethods: [(\"__init__\", \"fn[[v:V], none]\"), (\"fun\", \"fn[[a:T], V]\")],\ntype_vars: [\"T\", \"V\"]\n}\n",
|
"Class {\nname: \"A\",\nancestors: [\"A[T, V]\"],\nfields: [\"a\", \"b\"],\nmethods: [(\"__init__\", \"fn[[v:V], none]\"), (\"fun\", \"fn[[a:T], V]\")],\ntype_vars: [\"T\", \"V\"]\n}\n",
|
||||||
"Function {\nname: \"A.__init__\",\nsig: \"fn[[v:V], none]\",\nvar_id: [TypeVarId(242)]\n}\n",
|
"Function {\nname: \"A.__init__\",\nsig: \"fn[[v:V], none]\",\nvar_id: [TypeVarId(247)]\n}\n",
|
||||||
"Function {\nname: \"A.fun\",\nsig: \"fn[[a:T], V]\",\nvar_id: [TypeVarId(247)]\n}\n",
|
"Function {\nname: \"A.fun\",\nsig: \"fn[[a:T], V]\",\nvar_id: [TypeVarId(252)]\n}\n",
|
||||||
"Function {\nname: \"gfun\",\nsig: \"fn[[a:A[list[float], int32]], none]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"gfun\",\nsig: \"fn[[a:A[list[float], int32]], none]\",\nvar_id: []\n}\n",
|
||||||
"Class {\nname: \"B\",\nancestors: [\"B\"],\nfields: [],\nmethods: [(\"__init__\", \"fn[[], none]\")],\ntype_vars: []\n}\n",
|
"Class {\nname: \"B\",\nancestors: [\"B\"],\nfields: [],\nmethods: [(\"__init__\", \"fn[[], none]\")],\ntype_vars: []\n}\n",
|
||||||
"Function {\nname: \"B.__init__\",\nsig: \"fn[[], none]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"B.__init__\",\nsig: \"fn[[], none]\",\nvar_id: []\n}\n",
|
||||||
|
|
|
@ -3,7 +3,7 @@ source: nac3core/src/toplevel/test.rs
|
||||||
expression: res_vec
|
expression: res_vec
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
"Class {\nname: \"A\",\nancestors: [\"A[typevar228, typevar229]\"],\nfields: [\"a\", \"b\"],\nmethods: [(\"__init__\", \"fn[[a:A[float, bool], b:B], none]\"), (\"fun\", \"fn[[a:A[float, bool]], A[bool, int32]]\")],\ntype_vars: [\"typevar228\", \"typevar229\"]\n}\n",
|
"Class {\nname: \"A\",\nancestors: [\"A[typevar233, typevar234]\"],\nfields: [\"a\", \"b\"],\nmethods: [(\"__init__\", \"fn[[a:A[float, bool], b:B], none]\"), (\"fun\", \"fn[[a:A[float, bool]], A[bool, int32]]\")],\ntype_vars: [\"typevar233\", \"typevar234\"]\n}\n",
|
||||||
"Function {\nname: \"A.__init__\",\nsig: \"fn[[a:A[float, bool], b:B], none]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"A.__init__\",\nsig: \"fn[[a:A[float, bool], b:B], none]\",\nvar_id: []\n}\n",
|
||||||
"Function {\nname: \"A.fun\",\nsig: \"fn[[a:A[float, bool]], A[bool, int32]]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"A.fun\",\nsig: \"fn[[a:A[float, bool]], A[bool, int32]]\",\nvar_id: []\n}\n",
|
||||||
"Class {\nname: \"B\",\nancestors: [\"B\", \"A[int64, bool]\"],\nfields: [\"a\", \"b\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"fun\", \"fn[[a:A[float, bool]], A[bool, int32]]\"), (\"foo\", \"fn[[b:B], B]\"), (\"bar\", \"fn[[a:A[list[B], int32]], tuple[A[virtual[A[B, int32]], bool], B]]\")],\ntype_vars: []\n}\n",
|
"Class {\nname: \"B\",\nancestors: [\"B\", \"A[int64, bool]\"],\nfields: [\"a\", \"b\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"fun\", \"fn[[a:A[float, bool]], A[bool, int32]]\"), (\"foo\", \"fn[[b:B], B]\"), (\"bar\", \"fn[[a:A[list[B], int32]], tuple[A[virtual[A[B, int32]], bool], B]]\")],\ntype_vars: []\n}\n",
|
||||||
|
|
|
@ -6,12 +6,12 @@ expression: res_vec
|
||||||
"Class {\nname: \"A\",\nancestors: [\"A\"],\nfields: [\"a\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"fun\", \"fn[[b:B], none]\"), (\"foo\", \"fn[[a:T, b:V], none]\")],\ntype_vars: []\n}\n",
|
"Class {\nname: \"A\",\nancestors: [\"A\"],\nfields: [\"a\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"fun\", \"fn[[b:B], none]\"), (\"foo\", \"fn[[a:T, b:V], none]\")],\ntype_vars: []\n}\n",
|
||||||
"Function {\nname: \"A.__init__\",\nsig: \"fn[[], none]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"A.__init__\",\nsig: \"fn[[], none]\",\nvar_id: []\n}\n",
|
||||||
"Function {\nname: \"A.fun\",\nsig: \"fn[[b:B], none]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"A.fun\",\nsig: \"fn[[b:B], none]\",\nvar_id: []\n}\n",
|
||||||
"Function {\nname: \"A.foo\",\nsig: \"fn[[a:T, b:V], none]\",\nvar_id: [TypeVarId(248)]\n}\n",
|
"Function {\nname: \"A.foo\",\nsig: \"fn[[a:T, b:V], none]\",\nvar_id: [TypeVarId(253)]\n}\n",
|
||||||
"Class {\nname: \"B\",\nancestors: [\"B\", \"C\", \"A\"],\nfields: [\"a\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"fun\", \"fn[[b:B], none]\"), (\"foo\", \"fn[[a:T, b:V], none]\")],\ntype_vars: []\n}\n",
|
"Class {\nname: \"B\",\nancestors: [\"B\", \"C\", \"A\"],\nfields: [\"a\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"fun\", \"fn[[b:B], none]\"), (\"foo\", \"fn[[a:T, b:V], none]\")],\ntype_vars: []\n}\n",
|
||||||
"Function {\nname: \"B.__init__\",\nsig: \"fn[[], none]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"B.__init__\",\nsig: \"fn[[], none]\",\nvar_id: []\n}\n",
|
||||||
"Class {\nname: \"C\",\nancestors: [\"C\", \"A\"],\nfields: [\"a\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"fun\", \"fn[[b:B], none]\"), (\"foo\", \"fn[[a:T, b:V], none]\")],\ntype_vars: []\n}\n",
|
"Class {\nname: \"C\",\nancestors: [\"C\", \"A\"],\nfields: [\"a\"],\nmethods: [(\"__init__\", \"fn[[], none]\"), (\"fun\", \"fn[[b:B], none]\"), (\"foo\", \"fn[[a:T, b:V], none]\")],\ntype_vars: []\n}\n",
|
||||||
"Function {\nname: \"C.__init__\",\nsig: \"fn[[], none]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"C.__init__\",\nsig: \"fn[[], none]\",\nvar_id: []\n}\n",
|
||||||
"Function {\nname: \"C.fun\",\nsig: \"fn[[b:B], none]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"C.fun\",\nsig: \"fn[[b:B], none]\",\nvar_id: []\n}\n",
|
||||||
"Function {\nname: \"foo\",\nsig: \"fn[[a:A], none]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"foo\",\nsig: \"fn[[a:A], none]\",\nvar_id: []\n}\n",
|
||||||
"Function {\nname: \"ff\",\nsig: \"fn[[a:T], V]\",\nvar_id: [TypeVarId(256)]\n}\n",
|
"Function {\nname: \"ff\",\nsig: \"fn[[a:T], V]\",\nvar_id: [TypeVarId(261)]\n}\n",
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
use super::*;
|
||||||
|
use crate::toplevel::helper::PrimDef;
|
||||||
|
use crate::typecheck::typedef::into_var_map;
|
||||||
use crate::{
|
use crate::{
|
||||||
codegen::CodeGenContext,
|
codegen::CodeGenContext,
|
||||||
symbol_resolver::{SymbolResolver, ValueEnum},
|
symbol_resolver::{SymbolResolver, ValueEnum},
|
||||||
|
@ -14,8 +17,6 @@ use parking_lot::Mutex;
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::{collections::HashMap, sync::Arc};
|
||||||
use test_case::test_case;
|
use test_case::test_case;
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
struct ResolverInternal {
|
struct ResolverInternal {
|
||||||
id_to_type: Mutex<HashMap<StrRef, Type>>,
|
id_to_type: Mutex<HashMap<StrRef, Type>>,
|
||||||
id_to_def: Mutex<HashMap<StrRef, DefinitionId>>,
|
id_to_def: Mutex<HashMap<StrRef, DefinitionId>>,
|
||||||
|
@ -775,8 +776,15 @@ fn make_internal_resolver_with_tvar(
|
||||||
unifier: &mut Unifier,
|
unifier: &mut Unifier,
|
||||||
print: bool,
|
print: bool,
|
||||||
) -> Arc<ResolverInternal> {
|
) -> Arc<ResolverInternal> {
|
||||||
|
let list_elem_tvar = unifier.get_fresh_var(Some("list_elem".into()), None);
|
||||||
|
let list = unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id: PrimDef::List.id(),
|
||||||
|
fields: HashMap::new(),
|
||||||
|
params: into_var_map([list_elem_tvar]),
|
||||||
|
});
|
||||||
|
|
||||||
let res: Arc<ResolverInternal> = ResolverInternal {
|
let res: Arc<ResolverInternal> = ResolverInternal {
|
||||||
id_to_def: Mutex::default(),
|
id_to_def: Mutex::new(HashMap::from([("list".into(), PrimDef::List.id())])),
|
||||||
id_to_type: tvars
|
id_to_type: tvars
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(name, range)| {
|
.map(|(name, range)| {
|
||||||
|
@ -790,7 +798,7 @@ fn make_internal_resolver_with_tvar(
|
||||||
})
|
})
|
||||||
.collect::<HashMap<_, _>>()
|
.collect::<HashMap<_, _>>()
|
||||||
.into(),
|
.into(),
|
||||||
class_names: Mutex::default(),
|
class_names: Mutex::new(HashMap::from([("list".into(), list)])),
|
||||||
}
|
}
|
||||||
.into();
|
.into();
|
||||||
if print {
|
if print {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::symbol_resolver::SymbolValue;
|
use crate::symbol_resolver::SymbolValue;
|
||||||
use crate::toplevel::helper::PrimDef;
|
use crate::toplevel::helper::PrimDef;
|
||||||
use crate::typecheck::typedef::{GenericObjectType, VarMap};
|
use crate::typecheck::typedef::VarMap;
|
||||||
use nac3parser::ast::Constant;
|
use nac3parser::ast::Constant;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
@ -18,7 +18,6 @@ pub enum TypeAnnotation {
|
||||||
TypeVar(Type),
|
TypeVar(Type),
|
||||||
/// A `Literal` allowing a subset of literals.
|
/// A `Literal` allowing a subset of literals.
|
||||||
Literal(Vec<Constant>),
|
Literal(Vec<Constant>),
|
||||||
List(Box<TypeAnnotation>),
|
|
||||||
Tuple(Vec<TypeAnnotation>),
|
Tuple(Vec<TypeAnnotation>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,7 +50,6 @@ impl TypeAnnotation {
|
||||||
format!("Literal({})", values.iter().map(|v| format!("{v:?}")).join(", "))
|
format!("Literal({})", values.iter().map(|v| format!("{v:?}")).join(", "))
|
||||||
}
|
}
|
||||||
Virtual(ty) => format!("virtual[{}]", ty.stringify(unifier)),
|
Virtual(ty) => format!("virtual[{}]", ty.stringify(unifier)),
|
||||||
List(ty) => format!("list[{}]", ty.stringify(unifier)),
|
|
||||||
Tuple(types) => {
|
Tuple(types) => {
|
||||||
format!(
|
format!(
|
||||||
"tuple[{}]",
|
"tuple[{}]",
|
||||||
|
@ -145,9 +143,7 @@ pub fn parse_ast_to_type_annotation_kinds<T, S: std::hash::BuildHasher + Clone>(
|
||||||
slice: &ast::Expr<T>,
|
slice: &ast::Expr<T>,
|
||||||
unifier: &mut Unifier,
|
unifier: &mut Unifier,
|
||||||
mut locked: HashMap<DefinitionId, Vec<Type>, S>| {
|
mut locked: HashMap<DefinitionId, Vec<Type>, S>| {
|
||||||
if ["virtual".into(), "Generic".into(), "list".into(), "tuple".into(), "Option".into()]
|
if ["virtual".into(), "Generic".into(), "tuple".into(), "Option".into()].contains(id) {
|
||||||
.contains(id)
|
|
||||||
{
|
|
||||||
return Err(HashSet::from([format!(
|
return Err(HashSet::from([format!(
|
||||||
"keywords cannot be class name (at {})",
|
"keywords cannot be class name (at {})",
|
||||||
expr.location
|
expr.location
|
||||||
|
@ -236,23 +232,6 @@ pub fn parse_ast_to_type_annotation_kinds<T, S: std::hash::BuildHasher + Clone>(
|
||||||
Ok(TypeAnnotation::Virtual(def.into()))
|
Ok(TypeAnnotation::Virtual(def.into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// list
|
|
||||||
ast::ExprKind::Subscript { value, slice, .. }
|
|
||||||
if {
|
|
||||||
matches!(&value.node, ast::ExprKind::Name { id, .. } if id == &"list".into())
|
|
||||||
} =>
|
|
||||||
{
|
|
||||||
let def_ann = parse_ast_to_type_annotation_kinds(
|
|
||||||
resolver,
|
|
||||||
top_level_defs,
|
|
||||||
unifier,
|
|
||||||
primitives,
|
|
||||||
slice.as_ref(),
|
|
||||||
locked,
|
|
||||||
)?;
|
|
||||||
Ok(TypeAnnotation::List(def_ann.into()))
|
|
||||||
}
|
|
||||||
|
|
||||||
// option
|
// option
|
||||||
ast::ExprKind::Subscript { value, slice, .. }
|
ast::ExprKind::Subscript { value, slice, .. }
|
||||||
if {
|
if {
|
||||||
|
@ -267,7 +246,12 @@ pub fn parse_ast_to_type_annotation_kinds<T, S: std::hash::BuildHasher + Clone>(
|
||||||
slice.as_ref(),
|
slice.as_ref(),
|
||||||
locked,
|
locked,
|
||||||
)?;
|
)?;
|
||||||
let id = primitives.option.obj_id(unifier);
|
let id =
|
||||||
|
if let TypeEnum::TObj { obj_id, .. } = unifier.get_ty(primitives.option).as_ref() {
|
||||||
|
*obj_id
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
Ok(TypeAnnotation::CustomClass { id, params: vec![def_ann] })
|
Ok(TypeAnnotation::CustomClass { id, params: vec![def_ann] })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -511,15 +495,6 @@ pub fn get_type_from_type_annotation_kinds(
|
||||||
)?;
|
)?;
|
||||||
Ok(unifier.add_ty(TypeEnum::TVirtual { ty }))
|
Ok(unifier.add_ty(TypeEnum::TVirtual { ty }))
|
||||||
}
|
}
|
||||||
TypeAnnotation::List(ty) => {
|
|
||||||
let ty = get_type_from_type_annotation_kinds(
|
|
||||||
top_level_defs,
|
|
||||||
unifier,
|
|
||||||
ty.as_ref(),
|
|
||||||
subst_list,
|
|
||||||
)?;
|
|
||||||
Ok(unifier.add_ty(TypeEnum::TList { ty }))
|
|
||||||
}
|
|
||||||
TypeAnnotation::Tuple(tys) => {
|
TypeAnnotation::Tuple(tys) => {
|
||||||
let tys = tys
|
let tys = tys
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -560,7 +535,7 @@ pub fn get_type_var_contained_in_type_annotation(ann: &TypeAnnotation) -> Vec<Ty
|
||||||
let mut result: Vec<TypeAnnotation> = Vec::new();
|
let mut result: Vec<TypeAnnotation> = Vec::new();
|
||||||
match ann {
|
match ann {
|
||||||
TypeAnnotation::TypeVar(..) => result.push(ann.clone()),
|
TypeAnnotation::TypeVar(..) => result.push(ann.clone()),
|
||||||
TypeAnnotation::Virtual(ann) | TypeAnnotation::List(ann) => {
|
TypeAnnotation::Virtual(ann) => {
|
||||||
result.extend(get_type_var_contained_in_type_annotation(ann.as_ref()));
|
result.extend(get_type_var_contained_in_type_annotation(ann.as_ref()));
|
||||||
}
|
}
|
||||||
TypeAnnotation::CustomClass { params, .. } => {
|
TypeAnnotation::CustomClass { params, .. } => {
|
||||||
|
@ -601,8 +576,7 @@ pub fn check_overload_type_annotation_compatible(
|
||||||
|
|
||||||
a == b
|
a == b
|
||||||
}
|
}
|
||||||
(TypeAnnotation::Virtual(a), TypeAnnotation::Virtual(b))
|
(TypeAnnotation::Virtual(a), TypeAnnotation::Virtual(b)) => {
|
||||||
| (TypeAnnotation::List(a), TypeAnnotation::List(b)) => {
|
|
||||||
check_overload_type_annotation_compatible(a.as_ref(), b.as_ref(), unifier)
|
check_overload_type_annotation_compatible(a.as_ref(), b.as_ref(), unifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::typecheck::typedef::TypeEnum;
|
use crate::toplevel::helper::PrimDef;
|
||||||
|
|
||||||
use super::type_inferencer::Inferencer;
|
use super::type_inferencer::Inferencer;
|
||||||
use super::typedef::Type;
|
use super::typedef::{Type, TypeEnum};
|
||||||
use nac3parser::ast::{
|
use nac3parser::ast::{
|
||||||
self, Constant, Expr, ExprKind,
|
self, Constant, Expr, ExprKind,
|
||||||
Operator::{LShift, RShift},
|
Operator::{LShift, RShift},
|
||||||
|
@ -69,6 +69,7 @@ impl<'a> Inferencer<'a> {
|
||||||
// there are some cases where the custom field is None
|
// there are some cases where the custom field is None
|
||||||
if let Some(ty) = &expr.custom {
|
if let Some(ty) = &expr.custom {
|
||||||
if !matches!(&expr.node, ExprKind::Constant { value: Constant::Ellipsis, .. })
|
if !matches!(&expr.node, ExprKind::Constant { value: Constant::Ellipsis, .. })
|
||||||
|
&& !ty.obj_id(self.unifier).is_some_and(|id| id == PrimDef::List.id())
|
||||||
&& !self.unifier.is_concrete(*ty, &self.function_data.bound_variables)
|
&& !self.unifier.is_concrete(*ty, &self.function_data.bound_variables)
|
||||||
{
|
{
|
||||||
return Err(HashSet::from([format!(
|
return Err(HashSet::from([format!(
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
use crate::symbol_resolver::SymbolValue;
|
use crate::symbol_resolver::SymbolValue;
|
||||||
use crate::toplevel::helper::PrimDef;
|
use crate::toplevel::helper::PrimDef;
|
||||||
use crate::toplevel::primitive_type;
|
use crate::toplevel::numpy::{make_ndarray_ty, unpack_ndarray_var_tys};
|
||||||
use crate::typecheck::{
|
use crate::typecheck::{
|
||||||
type_inferencer::*,
|
type_inferencer::*,
|
||||||
typedef::{FunSignature, FuncArg, GenericObjectType, Type, TypeEnum, Unifier, VarMap},
|
typedef::{FunSignature, FuncArg, Type, TypeEnum, Unifier, VarMap},
|
||||||
};
|
};
|
||||||
use itertools::Itertools;
|
use itertools::{iproduct, Itertools};
|
||||||
use nac3parser::ast::StrRef;
|
use nac3parser::ast::StrRef;
|
||||||
use nac3parser::ast::{Cmpop, Operator, Unaryop};
|
use nac3parser::ast::{Cmpop, Operator, Unaryop};
|
||||||
use std::cmp::max;
|
use std::cmp::max;
|
||||||
|
@ -13,67 +13,138 @@ use std::collections::HashMap;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use strum::IntoEnumIterator;
|
use strum::IntoEnumIterator;
|
||||||
|
|
||||||
#[must_use]
|
/// The variant of a binary operator.
|
||||||
pub fn binop_name(op: Operator) -> &'static str {
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
match op {
|
pub enum BinopVariant {
|
||||||
Operator::Add => "__add__",
|
/// The normal variant.
|
||||||
Operator::Sub => "__sub__",
|
/// For addition, it would be `+`.
|
||||||
Operator::Div => "__truediv__",
|
Normal,
|
||||||
Operator::Mod => "__mod__",
|
/// The "Augmented Assigning Operator" variant.
|
||||||
Operator::Mult => "__mul__",
|
/// For addition, it would be `+=`.
|
||||||
Operator::Pow => "__pow__",
|
AugAssign,
|
||||||
Operator::BitOr => "__or__",
|
}
|
||||||
Operator::BitXor => "__xor__",
|
|
||||||
Operator::BitAnd => "__and__",
|
/// A binary operator with its variant.
|
||||||
Operator::LShift => "__lshift__",
|
#[derive(Debug, Clone, Copy)]
|
||||||
Operator::RShift => "__rshift__",
|
pub struct Binop {
|
||||||
Operator::FloorDiv => "__floordiv__",
|
/// The base [`Operator`] of this binary operator.
|
||||||
Operator::MatMult => "__matmul__",
|
pub base: Operator,
|
||||||
|
/// The variant of this binary operator.
|
||||||
|
pub variant: BinopVariant,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Binop {
|
||||||
|
/// Make a [`Binop`] of the normal variant from an [`Operator`].
|
||||||
|
#[must_use]
|
||||||
|
pub fn normal(base: Operator) -> Self {
|
||||||
|
Binop { base, variant: BinopVariant::Normal }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Make a [`Binop`] of the aug assign variant from an [`Operator`].
|
||||||
|
#[must_use]
|
||||||
|
pub fn aug_assign(base: Operator) -> Self {
|
||||||
|
Binop { base, variant: BinopVariant::AugAssign }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
/// Details about an operator (unary, binary, etc...) in Python
|
||||||
pub fn binop_assign_name(op: Operator) -> &'static str {
|
#[derive(Debug, Clone, Copy)]
|
||||||
match op {
|
pub struct OpInfo {
|
||||||
Operator::Add => "__iadd__",
|
/// The method name of the binary operator.
|
||||||
Operator::Sub => "__isub__",
|
/// For addition, this would be `__add__`, and `__iadd__` if
|
||||||
Operator::Div => "__itruediv__",
|
/// it is the augmented assigning variant.
|
||||||
Operator::Mod => "__imod__",
|
pub method_name: &'static str,
|
||||||
Operator::Mult => "__imul__",
|
/// The symbol of the binary operator.
|
||||||
Operator::Pow => "__ipow__",
|
/// For addition, this would be `+`, and `+=` if
|
||||||
Operator::BitOr => "__ior__",
|
/// it is the augmented assigning variant.
|
||||||
Operator::BitXor => "__ixor__",
|
pub symbol: &'static str,
|
||||||
Operator::BitAnd => "__iand__",
|
|
||||||
Operator::LShift => "__ilshift__",
|
|
||||||
Operator::RShift => "__irshift__",
|
|
||||||
Operator::FloorDiv => "__ifloordiv__",
|
|
||||||
Operator::MatMult => "__imatmul__",
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
/// Helper macro to conveniently build an [`OpInfo`].
|
||||||
pub fn unaryop_name(op: Unaryop) -> &'static str {
|
///
|
||||||
match op {
|
/// Example usage: `make_info("add", "+")` generates `OpInfo { name: "__add__", symbol: "+" }`
|
||||||
Unaryop::UAdd => "__pos__",
|
macro_rules! make_op_info {
|
||||||
Unaryop::USub => "__neg__",
|
($name:expr, $symbol:expr) => {
|
||||||
Unaryop::Not => "__not__",
|
OpInfo { method_name: concat!("__", $name, "__"), symbol: $symbol }
|
||||||
Unaryop::Invert => "__inv__",
|
};
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
pub trait HasOpInfo {
|
||||||
pub fn comparison_name(op: Cmpop) -> Option<&'static str> {
|
fn op_info(&self) -> OpInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_get_cmpop_info(op: Cmpop) -> Option<OpInfo> {
|
||||||
match op {
|
match op {
|
||||||
Cmpop::Lt => Some("__lt__"),
|
Cmpop::Lt => Some(make_op_info!("lt", "<")),
|
||||||
Cmpop::LtE => Some("__le__"),
|
Cmpop::LtE => Some(make_op_info!("le", "<=")),
|
||||||
Cmpop::Gt => Some("__gt__"),
|
Cmpop::Gt => Some(make_op_info!("gt", ">")),
|
||||||
Cmpop::GtE => Some("__ge__"),
|
Cmpop::GtE => Some(make_op_info!("ge", ">=")),
|
||||||
Cmpop::Eq => Some("__eq__"),
|
Cmpop::Eq => Some(make_op_info!("eq", "==")),
|
||||||
Cmpop::NotEq => Some("__ne__"),
|
Cmpop::NotEq => Some(make_op_info!("ne", "!=")),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl OpInfo {
|
||||||
|
#[must_use]
|
||||||
|
pub fn supports_cmpop(op: Cmpop) -> bool {
|
||||||
|
try_get_cmpop_info(op).is_some()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HasOpInfo for Cmpop {
|
||||||
|
fn op_info(&self) -> OpInfo {
|
||||||
|
try_get_cmpop_info(*self).expect("{self:?} is not supported")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HasOpInfo for Binop {
|
||||||
|
fn op_info(&self) -> OpInfo {
|
||||||
|
// Helper macro to generate both the normal variant [`OpInfo`] and the
|
||||||
|
// augmented assigning variant [`OpInfo`] for a binary operator conveniently.
|
||||||
|
macro_rules! info {
|
||||||
|
($name:literal, $symbol:literal) => {
|
||||||
|
(
|
||||||
|
make_op_info!($name, $symbol),
|
||||||
|
make_op_info!(concat!("i", $name), concat!($symbol, "=")),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let (normal_variant, aug_assign_variant) = match self.base {
|
||||||
|
Operator::Add => info!("add", "+"),
|
||||||
|
Operator::Sub => info!("sub", "-"),
|
||||||
|
Operator::Div => info!("truediv", "/"),
|
||||||
|
Operator::Mod => info!("mod", "%"),
|
||||||
|
Operator::Mult => info!("mul", "*"),
|
||||||
|
Operator::Pow => info!("pow", "**"),
|
||||||
|
Operator::BitOr => info!("or", "|"),
|
||||||
|
Operator::BitXor => info!("xor", "^"),
|
||||||
|
Operator::BitAnd => info!("and", "&"),
|
||||||
|
Operator::LShift => info!("lshift", "<<"),
|
||||||
|
Operator::RShift => info!("rshift", ">>"),
|
||||||
|
Operator::FloorDiv => info!("floordiv", "//"),
|
||||||
|
Operator::MatMult => info!("matmul", "@"),
|
||||||
|
};
|
||||||
|
|
||||||
|
match self.variant {
|
||||||
|
BinopVariant::Normal => normal_variant,
|
||||||
|
BinopVariant::AugAssign => aug_assign_variant,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HasOpInfo for Unaryop {
|
||||||
|
fn op_info(&self) -> OpInfo {
|
||||||
|
match self {
|
||||||
|
Unaryop::UAdd => make_op_info!("pos", "+"),
|
||||||
|
Unaryop::USub => make_op_info!("neg", "-"),
|
||||||
|
Unaryop::Not => make_op_info!("not", "not"), // i.e., `not False`, so the symbol is just `not`.
|
||||||
|
Unaryop::Invert => make_op_info!("inv", "~"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(super) fn with_fields<F>(unifier: &mut Unifier, ty: Type, f: F)
|
pub(super) fn with_fields<F>(unifier: &mut Unifier, ty: Type, f: F)
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Unifier, &mut HashMap<StrRef, (Type, bool)>),
|
F: FnOnce(&mut Unifier, &mut HashMap<StrRef, (Type, bool)>),
|
||||||
|
@ -115,23 +186,9 @@ pub fn impl_binop(
|
||||||
|
|
||||||
let ret_ty = ret_ty.unwrap_or_else(|| unifier.get_fresh_var(None, None).ty);
|
let ret_ty = ret_ty.unwrap_or_else(|| unifier.get_fresh_var(None, None).ty);
|
||||||
|
|
||||||
for op in ops {
|
for (base_op, variant) in iproduct!(ops, [BinopVariant::Normal, BinopVariant::AugAssign]) {
|
||||||
fields.insert(binop_name(*op).into(), {
|
let op = Binop { base: *base_op, variant };
|
||||||
(
|
fields.insert(op.op_info().method_name.into(), {
|
||||||
unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
|
||||||
ret: ret_ty,
|
|
||||||
vars: function_vars.clone(),
|
|
||||||
args: vec![FuncArg {
|
|
||||||
ty: other_ty,
|
|
||||||
default_value: None,
|
|
||||||
name: "other".into(),
|
|
||||||
}],
|
|
||||||
})),
|
|
||||||
false,
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
fields.insert(binop_assign_name(*op).into(), {
|
|
||||||
(
|
(
|
||||||
unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
||||||
ret: ret_ty,
|
ret: ret_ty,
|
||||||
|
@ -155,7 +212,7 @@ pub fn impl_unaryop(unifier: &mut Unifier, ty: Type, ret_ty: Option<Type>, ops:
|
||||||
|
|
||||||
for op in ops {
|
for op in ops {
|
||||||
fields.insert(
|
fields.insert(
|
||||||
unaryop_name(*op).into(),
|
op.op_info().method_name.into(),
|
||||||
(
|
(
|
||||||
unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
||||||
ret: ret_ty,
|
ret: ret_ty,
|
||||||
|
@ -195,7 +252,7 @@ pub fn impl_cmpop(
|
||||||
|
|
||||||
for op in ops {
|
for op in ops {
|
||||||
fields.insert(
|
fields.insert(
|
||||||
comparison_name(*op).unwrap().into(),
|
op.op_info().method_name.into(),
|
||||||
(
|
(
|
||||||
unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
||||||
ret: ret_ty,
|
ret: ret_ty,
|
||||||
|
@ -369,12 +426,8 @@ pub fn typeof_ndarray_broadcast(
|
||||||
if is_left_ndarray && is_right_ndarray {
|
if is_left_ndarray && is_right_ndarray {
|
||||||
// Perform broadcasting on two ndarray operands.
|
// Perform broadcasting on two ndarray operands.
|
||||||
|
|
||||||
let left_ty = primitive_type::NDArrayType::create(left, unifier);
|
let (left_ty_dtype, left_ty_ndims) = unpack_ndarray_var_tys(unifier, left);
|
||||||
let left_ty_dtype = left_ty.dtype_tvar(unifier).ty;
|
let (right_ty_dtype, right_ty_ndims) = unpack_ndarray_var_tys(unifier, right);
|
||||||
let left_ty_ndims = left_ty.ndims_tvar(unifier).ty;
|
|
||||||
let right_ty = primitive_type::NDArrayType::create(right, unifier);
|
|
||||||
let right_ty_dtype = right_ty.dtype_tvar(unifier).ty;
|
|
||||||
let right_ty_ndims = right_ty.ndims_tvar(unifier).ty;
|
|
||||||
|
|
||||||
assert!(unifier.unioned(left_ty_dtype, right_ty_dtype));
|
assert!(unifier.unioned(left_ty_dtype, right_ty_dtype));
|
||||||
|
|
||||||
|
@ -401,18 +454,11 @@ pub fn typeof_ndarray_broadcast(
|
||||||
.collect_vec();
|
.collect_vec();
|
||||||
let res_ndims = unifier.get_fresh_literal(res_ndims, None);
|
let res_ndims = unifier.get_fresh_literal(res_ndims, None);
|
||||||
|
|
||||||
Ok(primitive_type::NDArrayType::from_primitive(
|
Ok(make_ndarray_ty(unifier, primitives, Some(left_ty_dtype), Some(res_ndims)))
|
||||||
unifier,
|
|
||||||
primitives,
|
|
||||||
Some(left_ty_dtype),
|
|
||||||
Some(res_ndims),
|
|
||||||
)
|
|
||||||
.into())
|
|
||||||
} else {
|
} else {
|
||||||
let (ndarray_ty, scalar_ty) = if is_left_ndarray { (left, right) } else { (right, left) };
|
let (ndarray_ty, scalar_ty) = if is_left_ndarray { (left, right) } else { (right, left) };
|
||||||
|
|
||||||
let ndarray_ty_dtype =
|
let (ndarray_ty_dtype, _) = unpack_ndarray_var_tys(unifier, ndarray_ty);
|
||||||
primitive_type::NDArrayType::create(ndarray_ty, unifier).ndims_tvar(unifier).ty;
|
|
||||||
|
|
||||||
if unifier.unioned(ndarray_ty_dtype, scalar_ty) {
|
if unifier.unioned(ndarray_ty_dtype, scalar_ty) {
|
||||||
Ok(ndarray_ty)
|
Ok(ndarray_ty)
|
||||||
|
@ -440,12 +486,29 @@ pub fn typeof_binop(
|
||||||
lhs: Type,
|
lhs: Type,
|
||||||
rhs: Type,
|
rhs: Type,
|
||||||
) -> Result<Option<Type>, String> {
|
) -> Result<Option<Type>, String> {
|
||||||
|
let op = Binop { base: op, variant: BinopVariant::Normal };
|
||||||
|
|
||||||
|
let is_left_list = lhs.obj_id(unifier).is_some_and(|id| id == PrimDef::List.id());
|
||||||
|
let is_right_list = rhs.obj_id(unifier).is_some_and(|id| id == PrimDef::List.id());
|
||||||
let is_left_ndarray = lhs.obj_id(unifier).is_some_and(|id| id == PrimDef::NDArray.id());
|
let is_left_ndarray = lhs.obj_id(unifier).is_some_and(|id| id == PrimDef::NDArray.id());
|
||||||
let is_right_ndarray = rhs.obj_id(unifier).is_some_and(|id| id == PrimDef::NDArray.id());
|
let is_right_ndarray = rhs.obj_id(unifier).is_some_and(|id| id == PrimDef::NDArray.id());
|
||||||
|
|
||||||
Ok(Some(match op {
|
Ok(Some(match op.base {
|
||||||
Operator::Add | Operator::Sub | Operator::Mult | Operator::Mod | Operator::FloorDiv => {
|
Operator::Add | Operator::Sub | Operator::Mult | Operator::Mod | Operator::FloorDiv => {
|
||||||
if is_left_ndarray || is_right_ndarray {
|
if is_left_list || is_right_list {
|
||||||
|
if ![Operator::Add, Operator::Mult].contains(&op.base) {
|
||||||
|
return Err(format!(
|
||||||
|
"Binary operator {} not supported for list",
|
||||||
|
op.op_info().symbol
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_left_list {
|
||||||
|
lhs
|
||||||
|
} else {
|
||||||
|
rhs
|
||||||
|
}
|
||||||
|
} else if is_left_ndarray || is_right_ndarray {
|
||||||
typeof_ndarray_broadcast(unifier, primitives, lhs, rhs)?
|
typeof_ndarray_broadcast(unifier, primitives, lhs, rhs)?
|
||||||
} else if unifier.unioned(lhs, rhs) {
|
} else if unifier.unioned(lhs, rhs) {
|
||||||
lhs
|
lhs
|
||||||
|
@ -455,8 +518,7 @@ pub fn typeof_binop(
|
||||||
}
|
}
|
||||||
|
|
||||||
Operator::MatMult => {
|
Operator::MatMult => {
|
||||||
let lhs_ndims =
|
let (_, lhs_ndims) = unpack_ndarray_var_tys(unifier, lhs);
|
||||||
primitive_type::NDArrayType::create(lhs, unifier).ndims_tvar(unifier).ty;
|
|
||||||
let lhs_ndims = match &*unifier.get_ty_immutable(lhs_ndims) {
|
let lhs_ndims = match &*unifier.get_ty_immutable(lhs_ndims) {
|
||||||
TypeEnum::TLiteral { values, .. } => {
|
TypeEnum::TLiteral { values, .. } => {
|
||||||
assert_eq!(values.len(), 1);
|
assert_eq!(values.len(), 1);
|
||||||
|
@ -464,8 +526,7 @@ pub fn typeof_binop(
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
let rhs_ndims =
|
let (_, rhs_ndims) = unpack_ndarray_var_tys(unifier, rhs);
|
||||||
primitive_type::NDArrayType::create(rhs, unifier).ndims_tvar(unifier).ty;
|
|
||||||
let rhs_ndims = match &*unifier.get_ty_immutable(rhs_ndims) {
|
let rhs_ndims = match &*unifier.get_ty_immutable(rhs_ndims) {
|
||||||
TypeEnum::TLiteral { values, .. } => {
|
TypeEnum::TLiteral { values, .. } => {
|
||||||
assert_eq!(values.len(), 1);
|
assert_eq!(values.len(), 1);
|
||||||
|
@ -539,7 +600,7 @@ pub fn typeof_unaryop(
|
||||||
let operand_obj_id = operand.obj_id(unifier);
|
let operand_obj_id = operand.obj_id(unifier);
|
||||||
|
|
||||||
if op == Unaryop::Not
|
if op == Unaryop::Not
|
||||||
&& operand_obj_id.is_some_and(|id| id == primitives.ndarray.obj_id(unifier))
|
&& operand_obj_id.is_some_and(|id| id == primitives.ndarray.obj_id(unifier).unwrap())
|
||||||
{
|
{
|
||||||
return Err(
|
return Err(
|
||||||
"The truth value of an array with more than one element is ambiguous".to_string()
|
"The truth value of an array with more than one element is ambiguous".to_string()
|
||||||
|
@ -565,8 +626,7 @@ pub fn typeof_unaryop(
|
||||||
|
|
||||||
Unaryop::UAdd | Unaryop::USub => {
|
Unaryop::UAdd | Unaryop::USub => {
|
||||||
if operand_obj_id.is_some_and(|id| id == PrimDef::NDArray.id()) {
|
if operand_obj_id.is_some_and(|id| id == PrimDef::NDArray.id()) {
|
||||||
let dtype =
|
let (dtype, _) = unpack_ndarray_var_tys(unifier, operand);
|
||||||
primitive_type::NDArrayType::create(operand, unifier).dtype_tvar(unifier).ty;
|
|
||||||
if dtype.obj_id(unifier).is_some_and(|id| id == PrimDef::Bool.id()) {
|
if dtype.obj_id(unifier).is_some_and(|id| id == PrimDef::Bool.id()) {
|
||||||
return Err(if op == Unaryop::UAdd {
|
return Err(if op == Unaryop::UAdd {
|
||||||
"The ufunc 'positive' cannot be applied to ndarray[bool, N]".to_string()
|
"The ufunc 'positive' cannot be applied to ndarray[bool, N]".to_string()
|
||||||
|
@ -600,15 +660,9 @@ pub fn typeof_cmpop(
|
||||||
|
|
||||||
Ok(Some(if is_left_ndarray || is_right_ndarray {
|
Ok(Some(if is_left_ndarray || is_right_ndarray {
|
||||||
let brd = typeof_ndarray_broadcast(unifier, primitives, lhs, rhs)?;
|
let brd = typeof_ndarray_broadcast(unifier, primitives, lhs, rhs)?;
|
||||||
let ndims = primitive_type::NDArrayType::create(brd, unifier).ndims_tvar(unifier).ty;
|
let (_, ndims) = unpack_ndarray_var_tys(unifier, brd);
|
||||||
|
|
||||||
primitive_type::NDArrayType::from_primitive(
|
make_ndarray_ty(unifier, primitives, Some(primitives.bool), Some(ndims))
|
||||||
unifier,
|
|
||||||
primitives,
|
|
||||||
Some(primitives.bool),
|
|
||||||
Some(ndims),
|
|
||||||
)
|
|
||||||
.into()
|
|
||||||
} else if unifier.unioned(lhs, rhs) {
|
} else if unifier.unioned(lhs, rhs) {
|
||||||
primitives.bool
|
primitives.bool
|
||||||
} else {
|
} else {
|
||||||
|
@ -624,6 +678,7 @@ pub fn set_primitives_magic_methods(store: &PrimitiveStore, unifier: &mut Unifie
|
||||||
bool: bool_t,
|
bool: bool_t,
|
||||||
uint32: uint32_t,
|
uint32: uint32_t,
|
||||||
uint64: uint64_t,
|
uint64: uint64_t,
|
||||||
|
list: list_t,
|
||||||
ndarray: ndarray_t,
|
ndarray: ndarray_t,
|
||||||
..
|
..
|
||||||
} = *store;
|
} = *store;
|
||||||
|
@ -631,108 +686,69 @@ pub fn set_primitives_magic_methods(store: &PrimitiveStore, unifier: &mut Unifie
|
||||||
|
|
||||||
/* int ======== */
|
/* int ======== */
|
||||||
for t in [int32_t, int64_t, uint32_t, uint64_t] {
|
for t in [int32_t, int64_t, uint32_t, uint64_t] {
|
||||||
let ndarray_int_t =
|
let ndarray_int_t = make_ndarray_ty(unifier, store, Some(t), None);
|
||||||
primitive_type::NDArrayType::from_primitive(unifier, store, Some(t), None);
|
impl_basic_arithmetic(unifier, store, t, &[t, ndarray_int_t], None);
|
||||||
impl_basic_arithmetic(unifier, store, t, &[t, ndarray_int_t.into()], None);
|
impl_pow(unifier, store, t, &[t, ndarray_int_t], None);
|
||||||
impl_pow(unifier, store, t, &[t, ndarray_int_t.into()], None);
|
|
||||||
impl_bitwise_arithmetic(unifier, store, t);
|
impl_bitwise_arithmetic(unifier, store, t);
|
||||||
impl_bitwise_shift(unifier, store, t);
|
impl_bitwise_shift(unifier, store, t);
|
||||||
impl_div(unifier, store, t, &[t, ndarray_int_t.into()], None);
|
impl_div(unifier, store, t, &[t, ndarray_int_t], None);
|
||||||
impl_floordiv(unifier, store, t, &[t, ndarray_int_t.into()], None);
|
impl_floordiv(unifier, store, t, &[t, ndarray_int_t], None);
|
||||||
impl_mod(unifier, store, t, &[t, ndarray_int_t.into()], None);
|
impl_mod(unifier, store, t, &[t, ndarray_int_t], None);
|
||||||
impl_invert(unifier, store, t, Some(t));
|
impl_invert(unifier, store, t, Some(t));
|
||||||
impl_not(unifier, store, t, Some(bool_t));
|
impl_not(unifier, store, t, Some(bool_t));
|
||||||
impl_comparison(unifier, store, t, &[t, ndarray_int_t.into()], None);
|
impl_comparison(unifier, store, t, &[t, ndarray_int_t], None);
|
||||||
impl_eq(unifier, store, t, &[t, ndarray_int_t.into()], None);
|
impl_eq(unifier, store, t, &[t, ndarray_int_t], None);
|
||||||
}
|
}
|
||||||
for t in [int32_t, int64_t] {
|
for t in [int32_t, int64_t] {
|
||||||
impl_sign(unifier, store, t, Some(t));
|
impl_sign(unifier, store, t, Some(t));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* float ======== */
|
/* float ======== */
|
||||||
let ndarray_float_t =
|
let ndarray_float_t = make_ndarray_ty(unifier, store, Some(float_t), None);
|
||||||
primitive_type::NDArrayType::from_primitive(unifier, store, Some(float_t), None);
|
let ndarray_int32_t = make_ndarray_ty(unifier, store, Some(int32_t), None);
|
||||||
let ndarray_int32_t =
|
impl_basic_arithmetic(unifier, store, float_t, &[float_t, ndarray_float_t], None);
|
||||||
primitive_type::NDArrayType::from_primitive(unifier, store, Some(int32_t), None);
|
impl_pow(unifier, store, float_t, &[int32_t, float_t, ndarray_int32_t, ndarray_float_t], None);
|
||||||
impl_basic_arithmetic(unifier, store, float_t, &[float_t, ndarray_float_t.into()], None);
|
impl_div(unifier, store, float_t, &[float_t, ndarray_float_t], None);
|
||||||
impl_pow(
|
impl_floordiv(unifier, store, float_t, &[float_t, ndarray_float_t], None);
|
||||||
unifier,
|
impl_mod(unifier, store, float_t, &[float_t, ndarray_float_t], None);
|
||||||
store,
|
|
||||||
float_t,
|
|
||||||
&[int32_t, float_t, ndarray_int32_t.into(), ndarray_float_t.into()],
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
impl_div(unifier, store, float_t, &[float_t, ndarray_float_t.into()], None);
|
|
||||||
impl_floordiv(unifier, store, float_t, &[float_t, ndarray_float_t.into()], None);
|
|
||||||
impl_mod(unifier, store, float_t, &[float_t, ndarray_float_t.into()], None);
|
|
||||||
impl_sign(unifier, store, float_t, Some(float_t));
|
impl_sign(unifier, store, float_t, Some(float_t));
|
||||||
impl_not(unifier, store, float_t, Some(bool_t));
|
impl_not(unifier, store, float_t, Some(bool_t));
|
||||||
impl_comparison(unifier, store, float_t, &[float_t, ndarray_float_t.into()], None);
|
impl_comparison(unifier, store, float_t, &[float_t, ndarray_float_t], None);
|
||||||
impl_eq(unifier, store, float_t, &[float_t, ndarray_float_t.into()], None);
|
impl_eq(unifier, store, float_t, &[float_t, ndarray_float_t], None);
|
||||||
|
|
||||||
/* bool ======== */
|
/* bool ======== */
|
||||||
let ndarray_bool_t =
|
let ndarray_bool_t = make_ndarray_ty(unifier, store, Some(bool_t), None);
|
||||||
primitive_type::NDArrayType::from_primitive(unifier, store, Some(bool_t), None);
|
|
||||||
impl_invert(unifier, store, bool_t, Some(int32_t));
|
impl_invert(unifier, store, bool_t, Some(int32_t));
|
||||||
impl_not(unifier, store, bool_t, Some(bool_t));
|
impl_not(unifier, store, bool_t, Some(bool_t));
|
||||||
impl_sign(unifier, store, bool_t, Some(int32_t));
|
impl_sign(unifier, store, bool_t, Some(int32_t));
|
||||||
impl_eq(unifier, store, bool_t, &[bool_t, ndarray_bool_t.into()], None);
|
impl_eq(unifier, store, bool_t, &[bool_t, ndarray_bool_t], None);
|
||||||
|
|
||||||
|
/* list ======== */
|
||||||
|
impl_binop(unifier, store, list_t, &[list_t], Some(list_t), &[Operator::Add]);
|
||||||
|
impl_binop(unifier, store, list_t, &[int32_t, int64_t], Some(list_t), &[Operator::Mult]);
|
||||||
|
impl_cmpop(unifier, store, list_t, &[list_t], &[Cmpop::Eq, Cmpop::NotEq], Some(bool_t));
|
||||||
|
|
||||||
/* ndarray ===== */
|
/* ndarray ===== */
|
||||||
let ndarray_usized_ndims_tvar =
|
let ndarray_usized_ndims_tvar =
|
||||||
unifier.get_fresh_const_generic_var(size_t, Some("ndarray_ndims".into()), None);
|
unifier.get_fresh_const_generic_var(size_t, Some("ndarray_ndims".into()), None);
|
||||||
let ndarray_unsized_t = primitive_type::NDArrayType::from_primitive(
|
let ndarray_unsized_t =
|
||||||
unifier,
|
make_ndarray_ty(unifier, store, None, Some(ndarray_usized_ndims_tvar.ty));
|
||||||
store,
|
let (ndarray_dtype_t, _) = unpack_ndarray_var_tys(unifier, ndarray_t);
|
||||||
None,
|
let (ndarray_unsized_dtype_t, _) = unpack_ndarray_var_tys(unifier, ndarray_unsized_t);
|
||||||
Some(ndarray_usized_ndims_tvar.ty),
|
|
||||||
);
|
|
||||||
let ndarray_dtype_t = ndarray_t.dtype_tvar(unifier).ty;
|
|
||||||
let ndarray_unsized_dtype_t = ndarray_unsized_t.dtype_tvar(unifier).ty;
|
|
||||||
impl_basic_arithmetic(
|
impl_basic_arithmetic(
|
||||||
unifier,
|
unifier,
|
||||||
store,
|
store,
|
||||||
ndarray_t.into(),
|
ndarray_t,
|
||||||
&[ndarray_unsized_t.into(), ndarray_unsized_dtype_t],
|
&[ndarray_unsized_t, ndarray_unsized_dtype_t],
|
||||||
None,
|
|
||||||
);
|
|
||||||
impl_pow(
|
|
||||||
unifier,
|
|
||||||
store,
|
|
||||||
ndarray_t.into(),
|
|
||||||
&[ndarray_unsized_t.into(), ndarray_unsized_dtype_t],
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
impl_div(unifier, store, ndarray_t.into(), &[ndarray_t.into(), ndarray_dtype_t], None);
|
|
||||||
impl_floordiv(
|
|
||||||
unifier,
|
|
||||||
store,
|
|
||||||
ndarray_t.into(),
|
|
||||||
&[ndarray_unsized_t.into(), ndarray_unsized_dtype_t],
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
impl_mod(
|
|
||||||
unifier,
|
|
||||||
store,
|
|
||||||
ndarray_t.into(),
|
|
||||||
&[ndarray_unsized_t.into(), ndarray_unsized_dtype_t],
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
impl_matmul(unifier, store, ndarray_t.into(), &[ndarray_t.into()], Some(ndarray_t.into()));
|
|
||||||
impl_sign(unifier, store, ndarray_t.into(), Some(ndarray_t.into()));
|
|
||||||
impl_invert(unifier, store, ndarray_t.into(), Some(ndarray_t.into()));
|
|
||||||
impl_eq(
|
|
||||||
unifier,
|
|
||||||
store,
|
|
||||||
ndarray_t.into(),
|
|
||||||
&[ndarray_unsized_t.into(), ndarray_unsized_dtype_t],
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
impl_comparison(
|
|
||||||
unifier,
|
|
||||||
store,
|
|
||||||
ndarray_t.into(),
|
|
||||||
&[ndarray_unsized_t.into(), ndarray_unsized_dtype_t],
|
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
|
impl_pow(unifier, store, ndarray_t, &[ndarray_unsized_t, ndarray_unsized_dtype_t], None);
|
||||||
|
impl_div(unifier, store, ndarray_t, &[ndarray_t, ndarray_dtype_t], None);
|
||||||
|
impl_floordiv(unifier, store, ndarray_t, &[ndarray_unsized_t, ndarray_unsized_dtype_t], None);
|
||||||
|
impl_mod(unifier, store, ndarray_t, &[ndarray_unsized_t, ndarray_unsized_dtype_t], None);
|
||||||
|
impl_matmul(unifier, store, ndarray_t, &[ndarray_t], Some(ndarray_t));
|
||||||
|
impl_sign(unifier, store, ndarray_t, Some(ndarray_t));
|
||||||
|
impl_invert(unifier, store, ndarray_t, Some(ndarray_t));
|
||||||
|
impl_eq(unifier, store, ndarray_t, &[ndarray_unsized_t, ndarray_unsized_dtype_t], None);
|
||||||
|
impl_comparison(unifier, store, ndarray_t, &[ndarray_unsized_t, ndarray_unsized_dtype_t], None);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
use crate::typecheck::typedef::TypeEnum;
|
use crate::typecheck::{magic_methods::HasOpInfo, typedef::TypeEnum};
|
||||||
|
|
||||||
use super::typedef::{RecordKey, Type, Unifier};
|
use super::{
|
||||||
|
magic_methods::Binop,
|
||||||
|
typedef::{RecordKey, Type, Unifier},
|
||||||
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use nac3parser::ast::{Location, StrRef};
|
use nac3parser::ast::{Cmpop, Location, StrRef};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum TypeErrorKind {
|
pub enum TypeErrorKind {
|
||||||
|
@ -26,6 +29,18 @@ pub enum TypeErrorKind {
|
||||||
expected: Type,
|
expected: Type,
|
||||||
got: Type,
|
got: Type,
|
||||||
},
|
},
|
||||||
|
UnsupportedBinaryOpTypes {
|
||||||
|
operator: Binop,
|
||||||
|
lhs_type: Type,
|
||||||
|
rhs_type: Type,
|
||||||
|
expected_rhs_type: Type,
|
||||||
|
},
|
||||||
|
UnsupportedComparsionOpTypes {
|
||||||
|
operator: Cmpop,
|
||||||
|
lhs_type: Type,
|
||||||
|
rhs_type: Type,
|
||||||
|
expected_rhs_type: Type,
|
||||||
|
},
|
||||||
FieldUnificationError {
|
FieldUnificationError {
|
||||||
field: RecordKey,
|
field: RecordKey,
|
||||||
types: (Type, Type),
|
types: (Type, Type),
|
||||||
|
@ -101,6 +116,26 @@ impl<'a> Display for DisplayTypeError<'a> {
|
||||||
let args = missing_arg_names.iter().join(", ");
|
let args = missing_arg_names.iter().join(", ");
|
||||||
write!(f, "Missing arguments: {args}")
|
write!(f, "Missing arguments: {args}")
|
||||||
}
|
}
|
||||||
|
UnsupportedBinaryOpTypes { operator, lhs_type, rhs_type, expected_rhs_type } => {
|
||||||
|
let op_symbol = operator.op_info().symbol;
|
||||||
|
|
||||||
|
let lhs_type_str = self.unifier.stringify_with_notes(*lhs_type, &mut notes);
|
||||||
|
let rhs_type_str = self.unifier.stringify_with_notes(*rhs_type, &mut notes);
|
||||||
|
let expected_rhs_type_str =
|
||||||
|
self.unifier.stringify_with_notes(*expected_rhs_type, &mut notes);
|
||||||
|
|
||||||
|
write!(f, "Unsupported operand type(s) for {op_symbol}: '{lhs_type_str}' and '{rhs_type_str}' (right operand should have type {expected_rhs_type_str})")
|
||||||
|
}
|
||||||
|
UnsupportedComparsionOpTypes { operator, lhs_type, rhs_type, expected_rhs_type } => {
|
||||||
|
let op_symbol = operator.op_info().symbol;
|
||||||
|
|
||||||
|
let lhs_type_str = self.unifier.stringify_with_notes(*lhs_type, &mut notes);
|
||||||
|
let rhs_type_str = self.unifier.stringify_with_notes(*rhs_type, &mut notes);
|
||||||
|
let expected_rhs_type_str =
|
||||||
|
self.unifier.stringify_with_notes(*expected_rhs_type, &mut notes);
|
||||||
|
|
||||||
|
write!(f, "'{op_symbol}' not supported between instances of '{lhs_type_str}' and '{rhs_type_str}' (right operand should have type {expected_rhs_type_str})")
|
||||||
|
}
|
||||||
UnknownArgName(name) => {
|
UnknownArgName(name) => {
|
||||||
write!(f, "Unknown argument name: {name}")
|
write!(f, "Unknown argument name: {name}")
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,17 +4,20 @@ use std::iter::once;
|
||||||
use std::ops::Not;
|
use std::ops::Not;
|
||||||
use std::{cell::RefCell, sync::Arc};
|
use std::{cell::RefCell, sync::Arc};
|
||||||
|
|
||||||
use super::typedef::{
|
use super::{
|
||||||
Call, FunSignature, FuncArg, GenericObjectType, RecordField, Type, TypeEnum, Unifier, VarMap,
|
magic_methods::*,
|
||||||
|
type_error::{TypeError, TypeErrorKind},
|
||||||
|
typedef::{
|
||||||
|
into_var_map, iter_type_vars, Call, CallId, FunSignature, FuncArg, OperatorInfo,
|
||||||
|
RecordField, RecordKey, Type, TypeEnum, TypeVar, Unifier, VarMap,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use super::{magic_methods::*, type_error::TypeError, typedef::CallId};
|
|
||||||
use crate::toplevel::primitive_type::{NDArrayType, OptionType};
|
|
||||||
use crate::toplevel::TopLevelDef;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
symbol_resolver::{SymbolResolver, SymbolValue},
|
symbol_resolver::{SymbolResolver, SymbolValue},
|
||||||
toplevel::{
|
toplevel::{
|
||||||
helper::{arraylike_flatten_element_type, arraylike_get_ndims, PrimDef},
|
helper::{arraylike_flatten_element_type, arraylike_get_ndims, PrimDef},
|
||||||
TopLevelContext,
|
numpy::{make_ndarray_ty, unpack_ndarray_var_tys},
|
||||||
|
TopLevelContext, TopLevelDef,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use itertools::{izip, Itertools};
|
use itertools::{izip, Itertools};
|
||||||
|
@ -51,8 +54,9 @@ pub struct PrimitiveStore {
|
||||||
pub range: Type,
|
pub range: Type,
|
||||||
pub str: Type,
|
pub str: Type,
|
||||||
pub exception: Type,
|
pub exception: Type,
|
||||||
pub option: OptionType,
|
pub option: Type,
|
||||||
pub ndarray: NDArrayType,
|
pub list: Type,
|
||||||
|
pub ndarray: Type,
|
||||||
pub size_t: u32,
|
pub size_t: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,34 +80,6 @@ impl PrimitiveStore {
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an iterator over all primitive types in this store.
|
|
||||||
fn iter(&self) -> impl Iterator<Item = Type> {
|
|
||||||
self.into_iter()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IntoIterator for &PrimitiveStore {
|
|
||||||
type Item = Type;
|
|
||||||
type IntoIter = <Vec<Type> as IntoIterator>::IntoIter;
|
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
|
||||||
vec![
|
|
||||||
self.int32,
|
|
||||||
self.int64,
|
|
||||||
self.uint32,
|
|
||||||
self.uint64,
|
|
||||||
self.float,
|
|
||||||
self.bool,
|
|
||||||
self.none,
|
|
||||||
self.range,
|
|
||||||
self.str,
|
|
||||||
self.exception,
|
|
||||||
self.option.into(),
|
|
||||||
self.ndarray.into(),
|
|
||||||
]
|
|
||||||
.into_iter()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FunctionData {
|
pub struct FunctionData {
|
||||||
|
@ -137,6 +113,14 @@ fn report_error<T>(msg: &str, location: Location) -> Result<T, HashSet<String>>
|
||||||
Err(HashSet::from([format!("{msg} at {location}")]))
|
Err(HashSet::from([format!("{msg} at {location}")]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn report_type_error<T>(
|
||||||
|
kind: TypeErrorKind,
|
||||||
|
loc: Option<Location>,
|
||||||
|
unifier: &Unifier,
|
||||||
|
) -> Result<T, HashSet<String>> {
|
||||||
|
Err(HashSet::from([TypeError::new(kind, loc).to_display(unifier).to_string()]))
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> Fold<()> for Inferencer<'a> {
|
impl<'a> Fold<()> for Inferencer<'a> {
|
||||||
type TargetU = Option<Type>;
|
type TargetU = Option<Type>;
|
||||||
type Error = HashSet<String>;
|
type Error = HashSet<String>;
|
||||||
|
@ -272,13 +256,32 @@ impl<'a> Fold<()> for Inferencer<'a> {
|
||||||
self.unify(self.primitives.int32, target.custom.unwrap(), &target.location)?;
|
self.unify(self.primitives.int32, target.custom.unwrap(), &target.location)?;
|
||||||
} else {
|
} else {
|
||||||
let list_like_ty = match &*self.unifier.get_ty(iter.custom.unwrap()) {
|
let list_like_ty = match &*self.unifier.get_ty(iter.custom.unwrap()) {
|
||||||
TypeEnum::TList { .. } => {
|
TypeEnum::TObj { obj_id, params, .. } if *obj_id == PrimDef::List.id() => {
|
||||||
self.unifier.add_ty(TypeEnum::TList { ty: target.custom.unwrap() })
|
let list_tvar = iter_type_vars(params).nth(0).unwrap();
|
||||||
|
self.unifier
|
||||||
|
.subst(
|
||||||
|
self.primitives.list,
|
||||||
|
&into_var_map([TypeVar {
|
||||||
|
id: list_tvar.id,
|
||||||
|
ty: target.custom.unwrap(),
|
||||||
|
}]),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
}
|
}
|
||||||
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => {
|
||||||
|
// User is attempting to use a for loop to iterate
|
||||||
|
// over a value of an unsupported type.
|
||||||
|
|
||||||
|
let iter_ty = iter.custom.unwrap();
|
||||||
|
let iter_ty_str = self.unifier.stringify(iter_ty);
|
||||||
|
return report_error(
|
||||||
|
format!("'{iter_ty_str}' object is not iterable").as_str(),
|
||||||
|
iter.location,
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
self.unify(list_like_ty, iter.custom.unwrap(), &iter.location)?;
|
self.unify(list_like_ty, iter.custom.unwrap(), &iter.location)?;
|
||||||
}
|
}
|
||||||
|
@ -496,7 +499,8 @@ impl<'a> Fold<()> for Inferencer<'a> {
|
||||||
(None, None) => {}
|
(None, None) => {}
|
||||||
},
|
},
|
||||||
ast::StmtKind::AugAssign { target, op, value, .. } => {
|
ast::StmtKind::AugAssign { target, op, value, .. } => {
|
||||||
let res_ty = self.infer_bin_ops(stmt.location, target, *op, value, true)?;
|
let res_ty =
|
||||||
|
self.infer_bin_ops(stmt.location, target, Binop::aug_assign(*op), value)?;
|
||||||
self.unify(res_ty, target.custom.unwrap(), &stmt.location)?;
|
self.unify(res_ty, target.custom.unwrap(), &stmt.location)?;
|
||||||
}
|
}
|
||||||
ast::StmtKind::Assert { test, msg, .. } => {
|
ast::StmtKind::Assert { test, msg, .. } => {
|
||||||
|
@ -530,7 +534,7 @@ impl<'a> Fold<()> for Inferencer<'a> {
|
||||||
// the name `none` is special since it may have different types
|
// the name `none` is special since it may have different types
|
||||||
if id == &"none".into() {
|
if id == &"none".into() {
|
||||||
if let TypeEnum::TObj { params, .. } =
|
if let TypeEnum::TObj { params, .. } =
|
||||||
&*self.unifier.get_ty_immutable(self.primitives.option.into())
|
self.unifier.get_ty_immutable(self.primitives.option).as_ref()
|
||||||
{
|
{
|
||||||
let var_map = params
|
let var_map = params
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -545,7 +549,7 @@ impl<'a> Fold<()> for Inferencer<'a> {
|
||||||
(*id, self.unifier.get_fresh_var_with_range(range, *name, *loc).ty)
|
(*id, self.unifier.get_fresh_var_with_range(range, *name, *loc).ty)
|
||||||
})
|
})
|
||||||
.collect::<VarMap>();
|
.collect::<VarMap>();
|
||||||
Some(self.unifier.subst(self.primitives.option.into(), &var_map).unwrap())
|
Some(self.unifier.subst(self.primitives.option, &var_map).unwrap())
|
||||||
} else {
|
} else {
|
||||||
unreachable!("must be tobj")
|
unreachable!("must be tobj")
|
||||||
}
|
}
|
||||||
|
@ -578,7 +582,7 @@ impl<'a> Fold<()> for Inferencer<'a> {
|
||||||
}
|
}
|
||||||
ExprKind::BoolOp { values, .. } => Some(self.infer_bool_ops(values)?),
|
ExprKind::BoolOp { values, .. } => Some(self.infer_bool_ops(values)?),
|
||||||
ExprKind::BinOp { left, op, right } => {
|
ExprKind::BinOp { left, op, right } => {
|
||||||
Some(self.infer_bin_ops(expr.location, left, *op, right, false)?)
|
Some(self.infer_bin_ops(expr.location, left, Binop::normal(*op), right)?)
|
||||||
}
|
}
|
||||||
ExprKind::UnaryOp { op, operand } => {
|
ExprKind::UnaryOp { op, operand } => {
|
||||||
Some(self.infer_unary_ops(expr.location, *op, operand)?)
|
Some(self.infer_unary_ops(expr.location, *op, operand)?)
|
||||||
|
@ -645,6 +649,7 @@ impl<'a> Inferencer<'a> {
|
||||||
obj: Type,
|
obj: Type,
|
||||||
params: Vec<Type>,
|
params: Vec<Type>,
|
||||||
ret: Option<Type>,
|
ret: Option<Type>,
|
||||||
|
operator_info: Option<OperatorInfo>,
|
||||||
) -> InferenceResult {
|
) -> InferenceResult {
|
||||||
if let TypeEnum::TObj { params: class_params, fields, .. } = &*self.unifier.get_ty(obj) {
|
if let TypeEnum::TObj { params: class_params, fields, .. } = &*self.unifier.get_ty(obj) {
|
||||||
if class_params.is_empty() {
|
if class_params.is_empty() {
|
||||||
|
@ -658,6 +663,7 @@ impl<'a> Inferencer<'a> {
|
||||||
ret: sign.ret,
|
ret: sign.ret,
|
||||||
fun: RefCell::new(None),
|
fun: RefCell::new(None),
|
||||||
loc: Some(location),
|
loc: Some(location),
|
||||||
|
operator_info,
|
||||||
};
|
};
|
||||||
if let Some(ret) = ret {
|
if let Some(ret) = ret {
|
||||||
self.unifier
|
self.unifier
|
||||||
|
@ -692,6 +698,7 @@ impl<'a> Inferencer<'a> {
|
||||||
ret,
|
ret,
|
||||||
fun: RefCell::new(None),
|
fun: RefCell::new(None),
|
||||||
loc: Some(location),
|
loc: Some(location),
|
||||||
|
operator_info,
|
||||||
});
|
});
|
||||||
self.calls.insert(location.into(), call);
|
self.calls.insert(location.into(), call);
|
||||||
let call = self.unifier.add_ty(TypeEnum::TCall(vec![call]));
|
let call = self.unifier.add_ty(TypeEnum::TCall(vec![call]));
|
||||||
|
@ -784,6 +791,16 @@ impl<'a> Inferencer<'a> {
|
||||||
generators[0].target.location,
|
generators[0].target.location,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let list_tvar = if let TypeEnum::TObj { obj_id, params, .. } =
|
||||||
|
&*self.unifier.get_ty_immutable(self.primitives.list)
|
||||||
|
{
|
||||||
|
assert_eq!(*obj_id, PrimDef::List.id());
|
||||||
|
iter_type_vars(params).nth(0).unwrap()
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
let variable_mapping = self.variable_mapping.clone();
|
let variable_mapping = self.variable_mapping.clone();
|
||||||
let defined_identifiers = self.defined_identifiers.clone();
|
let defined_identifiers = self.defined_identifiers.clone();
|
||||||
let mut new_context = Inferencer {
|
let mut new_context = Inferencer {
|
||||||
|
@ -812,7 +829,13 @@ impl<'a> Inferencer<'a> {
|
||||||
&target.location,
|
&target.location,
|
||||||
)?;
|
)?;
|
||||||
} else {
|
} else {
|
||||||
let list = new_context.unifier.add_ty(TypeEnum::TList { ty: target.custom.unwrap() });
|
let list = new_context
|
||||||
|
.unifier
|
||||||
|
.subst(
|
||||||
|
self.primitives.list,
|
||||||
|
&into_var_map([TypeVar { id: list_tvar.id, ty: target.custom.unwrap() }]),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
new_context.unify(iter.custom.unwrap(), list, &iter.location)?;
|
new_context.unify(iter.custom.unwrap(), list, &iter.location)?;
|
||||||
}
|
}
|
||||||
let ifs: Vec<_> = generator
|
let ifs: Vec<_> = generator
|
||||||
|
@ -829,9 +852,16 @@ impl<'a> Inferencer<'a> {
|
||||||
new_context.unify(v.custom.unwrap(), new_context.primitives.bool, &v.location)?;
|
new_context.unify(v.custom.unwrap(), new_context.primitives.bool, &v.location)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let custom = new_context
|
||||||
|
.unifier
|
||||||
|
.subst(
|
||||||
|
self.primitives.list,
|
||||||
|
&into_var_map([TypeVar { id: list_tvar.id, ty: elt.custom.unwrap() }]),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
Ok(Located {
|
Ok(Located {
|
||||||
location,
|
location,
|
||||||
custom: Some(new_context.unifier.add_ty(TypeEnum::TList { ty: elt.custom.unwrap() })),
|
custom: Some(custom),
|
||||||
node: ExprKind::ListComp {
|
node: ExprKind::ListComp {
|
||||||
elt: Box::new(elt),
|
elt: Box::new(elt),
|
||||||
generators: vec![Comprehension {
|
generators: vec![Comprehension {
|
||||||
|
@ -913,11 +943,13 @@ impl<'a> Inferencer<'a> {
|
||||||
// Here, we also take the opportunity to deduce `ndims` statically.
|
// Here, we also take the opportunity to deduce `ndims` statically.
|
||||||
let shape_ty_enum = &*self.unifier.get_ty(shape_ty);
|
let shape_ty_enum = &*self.unifier.get_ty(shape_ty);
|
||||||
let ndims = match shape_ty_enum {
|
let ndims = match shape_ty_enum {
|
||||||
TypeEnum::TList { ty } => {
|
TypeEnum::TObj { obj_id, params, .. } if *obj_id == PrimDef::List.id() => {
|
||||||
// Handle 1. A list of int32s
|
// Handle 1. A list of int32s
|
||||||
|
|
||||||
|
let ty = iter_type_vars(params).nth(0).unwrap().ty;
|
||||||
|
|
||||||
// Typecheck
|
// Typecheck
|
||||||
self.unifier.unify(*ty, self.primitives.int32).map_err(|err| {
|
self.unifier.unify(ty, self.primitives.int32).map_err(|err| {
|
||||||
HashSet::from([err
|
HashSet::from([err
|
||||||
.at(Some(shape.location))
|
.at(Some(shape.location))
|
||||||
.to_display(self.unifier)
|
.to_display(self.unifier)
|
||||||
|
@ -960,11 +992,10 @@ impl<'a> Inferencer<'a> {
|
||||||
// `ndims` can be deduced statically from the inferred Tuple type.
|
// `ndims` can be deduced statically from the inferred Tuple type.
|
||||||
tuple_element_types.len() as u64
|
tuple_element_types.len() as u64
|
||||||
}
|
}
|
||||||
TypeEnum::TObj { .. } => {
|
TypeEnum::TObj { obj_id, .. }
|
||||||
// Handle 3. An integer (generalized as [`TypeEnum::TObj`])
|
if *obj_id == self.primitives.int32.obj_id(self.unifier).unwrap() =>
|
||||||
|
{
|
||||||
// Typecheck
|
// Handle 3. An int32 (generalized as [`TypeEnum::TObj`])
|
||||||
self.unify(self.primitives.int32, shape_ty, &shape.location)?;
|
|
||||||
|
|
||||||
// Deduce `ndims`
|
// Deduce `ndims`
|
||||||
1
|
1
|
||||||
|
@ -1064,16 +1095,9 @@ impl<'a> Inferencer<'a> {
|
||||||
|
|
||||||
let ret = if arg0_ty.obj_id(self.unifier).is_some_and(|id| id == PrimDef::NDArray.id())
|
let ret = if arg0_ty.obj_id(self.unifier).is_some_and(|id| id == PrimDef::NDArray.id())
|
||||||
{
|
{
|
||||||
let ndarray_ndims =
|
let (_, ndarray_ndims) = unpack_ndarray_var_tys(self.unifier, arg0_ty);
|
||||||
NDArrayType::create(arg0_ty, self.unifier).ndims_tvar(self.unifier).ty;
|
|
||||||
|
|
||||||
NDArrayType::from_primitive(
|
make_ndarray_ty(self.unifier, self.primitives, Some(target_ty), Some(ndarray_ndims))
|
||||||
self.unifier,
|
|
||||||
self.primitives,
|
|
||||||
Some(target_ty),
|
|
||||||
Some(ndarray_ndims),
|
|
||||||
)
|
|
||||||
.into()
|
|
||||||
} else {
|
} else {
|
||||||
target_ty
|
target_ty
|
||||||
};
|
};
|
||||||
|
@ -1109,7 +1133,9 @@ impl<'a> Inferencer<'a> {
|
||||||
|
|
||||||
let ret = if arg0_ty.obj_id(self.unifier).is_some_and(|id| id == PrimDef::NDArray.id())
|
let ret = if arg0_ty.obj_id(self.unifier).is_some_and(|id| id == PrimDef::NDArray.id())
|
||||||
{
|
{
|
||||||
NDArrayType::create(arg0_ty, self.unifier).dtype_tvar(self.unifier).ty
|
let (ndarray_dtype, _) = unpack_ndarray_var_tys(self.unifier, arg0_ty);
|
||||||
|
|
||||||
|
ndarray_dtype
|
||||||
} else {
|
} else {
|
||||||
arg0_ty
|
arg0_ty
|
||||||
};
|
};
|
||||||
|
@ -1161,14 +1187,14 @@ impl<'a> Inferencer<'a> {
|
||||||
|
|
||||||
let arg0_dtype =
|
let arg0_dtype =
|
||||||
if arg0_ty.obj_id(self.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) {
|
if arg0_ty.obj_id(self.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) {
|
||||||
NDArrayType::create(arg0_ty, self.unifier).dtype_tvar(self.unifier).ty
|
unpack_ndarray_var_tys(self.unifier, arg0_ty).0
|
||||||
} else {
|
} else {
|
||||||
arg0_ty
|
arg0_ty
|
||||||
};
|
};
|
||||||
|
|
||||||
let arg1_dtype =
|
let arg1_dtype =
|
||||||
if arg1_ty.obj_id(self.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) {
|
if arg1_ty.obj_id(self.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) {
|
||||||
NDArrayType::create(arg1_ty, self.unifier).dtype_tvar(self.unifier).ty
|
unpack_ndarray_var_tys(self.unifier, arg1_ty).0
|
||||||
} else {
|
} else {
|
||||||
arg1_ty
|
arg1_ty
|
||||||
};
|
};
|
||||||
|
@ -1199,17 +1225,9 @@ impl<'a> Inferencer<'a> {
|
||||||
// (float, int32), so convert it to align with the dtype of the first arg
|
// (float, int32), so convert it to align with the dtype of the first arg
|
||||||
let arg1_ty = if id == &"np_ldexp".into() {
|
let arg1_ty = if id == &"np_ldexp".into() {
|
||||||
if arg1_ty.obj_id(self.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) {
|
if arg1_ty.obj_id(self.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) {
|
||||||
// let (_, ndims) = unpack_ndarray_var_tys(self.unifier, arg1_ty);
|
let (_, ndims) = unpack_ndarray_var_tys(self.unifier, arg1_ty);
|
||||||
let ndims =
|
|
||||||
NDArrayType::create(arg1_ty, self.unifier).ndims_tvar(self.unifier).ty;
|
|
||||||
|
|
||||||
NDArrayType::from_primitive(
|
make_ndarray_ty(self.unifier, self.primitives, Some(target_ty), Some(ndims))
|
||||||
self.unifier,
|
|
||||||
self.primitives,
|
|
||||||
Some(target_ty),
|
|
||||||
Some(ndims),
|
|
||||||
)
|
|
||||||
.into()
|
|
||||||
} else {
|
} else {
|
||||||
target_ty
|
target_ty
|
||||||
}
|
}
|
||||||
|
@ -1296,16 +1314,9 @@ impl<'a> Inferencer<'a> {
|
||||||
|
|
||||||
let ret = if arg0_ty.obj_id(self.unifier).is_some_and(|id| id == PrimDef::NDArray.id())
|
let ret = if arg0_ty.obj_id(self.unifier).is_some_and(|id| id == PrimDef::NDArray.id())
|
||||||
{
|
{
|
||||||
let ndarray_ndims =
|
let (_, ndarray_ndims) = unpack_ndarray_var_tys(self.unifier, arg0_ty);
|
||||||
NDArrayType::create(arg0_ty, self.unifier).ndims_tvar(self.unifier).ty;
|
|
||||||
|
|
||||||
NDArrayType::from_primitive(
|
make_ndarray_ty(self.unifier, self.primitives, Some(target_ty), Some(ndarray_ndims))
|
||||||
self.unifier,
|
|
||||||
self.primitives,
|
|
||||||
Some(target_ty),
|
|
||||||
Some(ndarray_ndims),
|
|
||||||
)
|
|
||||||
.into()
|
|
||||||
} else {
|
} else {
|
||||||
target_ty
|
target_ty
|
||||||
};
|
};
|
||||||
|
@ -1345,7 +1356,7 @@ impl<'a> Inferencer<'a> {
|
||||||
self.fold_numpy_function_call_shape_argument(*id, 0, shape_expr)?; // Special handling for `shape`
|
self.fold_numpy_function_call_shape_argument(*id, 0, shape_expr)?; // Special handling for `shape`
|
||||||
|
|
||||||
let ndims = self.unifier.get_fresh_literal(vec![SymbolValue::U64(ndims)], None);
|
let ndims = self.unifier.get_fresh_literal(vec![SymbolValue::U64(ndims)], None);
|
||||||
let ret = NDArrayType::from_primitive(
|
let ret = make_ndarray_ty(
|
||||||
self.unifier,
|
self.unifier,
|
||||||
self.primitives,
|
self.primitives,
|
||||||
Some(self.primitives.float),
|
Some(self.primitives.float),
|
||||||
|
@ -1357,13 +1368,13 @@ impl<'a> Inferencer<'a> {
|
||||||
ty: shape.custom.unwrap(),
|
ty: shape.custom.unwrap(),
|
||||||
default_value: None,
|
default_value: None,
|
||||||
}],
|
}],
|
||||||
ret: ret.into(),
|
ret,
|
||||||
vars: VarMap::new(),
|
vars: VarMap::new(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return Ok(Some(Located {
|
return Ok(Some(Located {
|
||||||
location,
|
location,
|
||||||
custom: Some(ret.into()),
|
custom: Some(ret),
|
||||||
node: ExprKind::Call {
|
node: ExprKind::Call {
|
||||||
func: Box::new(Located {
|
func: Box::new(Located {
|
||||||
custom: Some(custom),
|
custom: Some(custom),
|
||||||
|
@ -1396,8 +1407,7 @@ impl<'a> Inferencer<'a> {
|
||||||
|
|
||||||
let ty = arg1.custom.unwrap();
|
let ty = arg1.custom.unwrap();
|
||||||
let ndims = self.unifier.get_fresh_literal(vec![SymbolValue::U64(ndims)], None);
|
let ndims = self.unifier.get_fresh_literal(vec![SymbolValue::U64(ndims)], None);
|
||||||
let ret =
|
let ret = make_ndarray_ty(self.unifier, self.primitives, Some(ty), Some(ndims));
|
||||||
NDArrayType::from_primitive(self.unifier, self.primitives, Some(ty), Some(ndims));
|
|
||||||
let custom = self.unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
let custom = self.unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
||||||
args: vec![
|
args: vec![
|
||||||
FuncArg { name: "shape".into(), ty: arg0.custom.unwrap(), default_value: None },
|
FuncArg { name: "shape".into(), ty: arg0.custom.unwrap(), default_value: None },
|
||||||
|
@ -1407,13 +1417,13 @@ impl<'a> Inferencer<'a> {
|
||||||
default_value: None,
|
default_value: None,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
ret: ret.into(),
|
ret,
|
||||||
vars: VarMap::new(),
|
vars: VarMap::new(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return Ok(Some(Located {
|
return Ok(Some(Located {
|
||||||
location,
|
location,
|
||||||
custom: Some(ret.into()),
|
custom: Some(ret),
|
||||||
node: ExprKind::Call {
|
node: ExprKind::Call {
|
||||||
func: Box::new(Located {
|
func: Box::new(Located {
|
||||||
custom: Some(custom),
|
custom: Some(custom),
|
||||||
|
@ -1451,8 +1461,7 @@ impl<'a> Inferencer<'a> {
|
||||||
arraylike_get_ndims(self.unifier, arg0.custom.unwrap())
|
arraylike_get_ndims(self.unifier, arg0.custom.unwrap())
|
||||||
};
|
};
|
||||||
let ndims = self.unifier.get_fresh_literal(vec![SymbolValue::U64(ndims)], None);
|
let ndims = self.unifier.get_fresh_literal(vec![SymbolValue::U64(ndims)], None);
|
||||||
let ret =
|
let ret = make_ndarray_ty(self.unifier, self.primitives, Some(ty), Some(ndims));
|
||||||
NDArrayType::from_primitive(self.unifier, self.primitives, Some(ty), Some(ndims));
|
|
||||||
|
|
||||||
let custom = self.unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
let custom = self.unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
||||||
args: vec![
|
args: vec![
|
||||||
|
@ -1472,13 +1481,13 @@ impl<'a> Inferencer<'a> {
|
||||||
default_value: Some(SymbolValue::U32(0)),
|
default_value: Some(SymbolValue::U32(0)),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
ret: ret.into(),
|
ret,
|
||||||
vars: VarMap::new(),
|
vars: VarMap::new(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return Ok(Some(Located {
|
return Ok(Some(Located {
|
||||||
location,
|
location,
|
||||||
custom: Some(ret.into()),
|
custom: Some(ret),
|
||||||
node: ExprKind::Call {
|
node: ExprKind::Call {
|
||||||
func: Box::new(Located {
|
func: Box::new(Located {
|
||||||
custom: Some(custom),
|
custom: Some(custom),
|
||||||
|
@ -1525,6 +1534,7 @@ impl<'a> Inferencer<'a> {
|
||||||
fun: RefCell::new(None),
|
fun: RefCell::new(None),
|
||||||
ret: sign.ret,
|
ret: sign.ret,
|
||||||
loc: Some(location),
|
loc: Some(location),
|
||||||
|
operator_info: None,
|
||||||
};
|
};
|
||||||
self.unifier.unify_call(&call, func.custom.unwrap(), sign).map_err(|e| {
|
self.unifier.unify_call(&call, func.custom.unwrap(), sign).map_err(|e| {
|
||||||
HashSet::from([e.at(Some(location)).to_display(self.unifier).to_string()])
|
HashSet::from([e.at(Some(location)).to_display(self.unifier).to_string()])
|
||||||
|
@ -1547,6 +1557,7 @@ impl<'a> Inferencer<'a> {
|
||||||
fun: RefCell::new(None),
|
fun: RefCell::new(None),
|
||||||
ret,
|
ret,
|
||||||
loc: Some(location),
|
loc: Some(location),
|
||||||
|
operator_info: None,
|
||||||
});
|
});
|
||||||
self.calls.insert(location.into(), call);
|
self.calls.insert(location.into(), call);
|
||||||
let call = self.unifier.add_ty(TypeEnum::TCall(vec![call]));
|
let call = self.unifier.add_ty(TypeEnum::TCall(vec![call]));
|
||||||
|
@ -1605,7 +1616,19 @@ impl<'a> Inferencer<'a> {
|
||||||
for t in elts {
|
for t in elts {
|
||||||
self.unify(ty, t.custom.unwrap(), &t.location)?;
|
self.unify(ty, t.custom.unwrap(), &t.location)?;
|
||||||
}
|
}
|
||||||
Ok(self.unifier.add_ty(TypeEnum::TList { ty }))
|
let list_tvar = if let TypeEnum::TObj { obj_id, params, .. } =
|
||||||
|
&*self.unifier.get_ty_immutable(self.primitives.list)
|
||||||
|
{
|
||||||
|
assert_eq!(*obj_id, PrimDef::List.id());
|
||||||
|
iter_type_vars(params).nth(0).unwrap()
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
let list = self
|
||||||
|
.unifier
|
||||||
|
.subst(self.primitives.list, &into_var_map([TypeVar { id: list_tvar.id, ty }]))
|
||||||
|
.unwrap();
|
||||||
|
Ok(list)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::unnecessary_wraps)]
|
#[allow(clippy::unnecessary_wraps)]
|
||||||
|
@ -1643,9 +1666,11 @@ impl<'a> Inferencer<'a> {
|
||||||
// just a fast path
|
// just a fast path
|
||||||
match (fields.get(&attr), ctx == ExprContext::Store) {
|
match (fields.get(&attr), ctx == ExprContext::Store) {
|
||||||
(Some((ty, true)), _) | (Some((ty, false)), false) => Ok(*ty),
|
(Some((ty, true)), _) | (Some((ty, false)), false) => Ok(*ty),
|
||||||
(Some((_, false)), true) => {
|
(Some((ty, false)), true) => report_type_error(
|
||||||
report_error(&format!("Field `{attr}` is immutable"), value.location)
|
TypeErrorKind::MutationError(RecordKey::Str(attr), *ty),
|
||||||
}
|
Some(value.location),
|
||||||
|
self.unifier,
|
||||||
|
),
|
||||||
(None, mutable) => {
|
(None, mutable) => {
|
||||||
// Check whether it is a class attribute
|
// Check whether it is a class attribute
|
||||||
let defs = self.top_level.definitions.read();
|
let defs = self.top_level.definitions.read();
|
||||||
|
@ -1667,13 +1692,11 @@ impl<'a> Inferencer<'a> {
|
||||||
&format!("Class Attribute `{attr}` is immutable"),
|
&format!("Class Attribute `{attr}` is immutable"),
|
||||||
value.location,
|
value.location,
|
||||||
),
|
),
|
||||||
None => {
|
None => report_type_error(
|
||||||
let t = self.unifier.stringify(ty);
|
TypeErrorKind::NoSuchField(RecordKey::Str(attr), ty),
|
||||||
report_error(
|
Some(value.location),
|
||||||
&format!("`{t}::{attr}` field/method does not exist"),
|
self.unifier,
|
||||||
value.location,
|
),
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1720,9 +1743,8 @@ impl<'a> Inferencer<'a> {
|
||||||
&mut self,
|
&mut self,
|
||||||
location: Location,
|
location: Location,
|
||||||
left: &ast::Expr<Option<Type>>,
|
left: &ast::Expr<Option<Type>>,
|
||||||
op: ast::Operator,
|
op: Binop,
|
||||||
right: &ast::Expr<Option<Type>>,
|
right: &ast::Expr<Option<Type>>,
|
||||||
is_aug_assign: bool,
|
|
||||||
) -> InferenceResult {
|
) -> InferenceResult {
|
||||||
let left_ty = left.custom.unwrap();
|
let left_ty = left.custom.unwrap();
|
||||||
let right_ty = right.custom.unwrap();
|
let right_ty = right.custom.unwrap();
|
||||||
|
@ -1730,27 +1752,40 @@ impl<'a> Inferencer<'a> {
|
||||||
let method = if let TypeEnum::TObj { fields, .. } =
|
let method = if let TypeEnum::TObj { fields, .. } =
|
||||||
self.unifier.get_ty_immutable(left_ty).as_ref()
|
self.unifier.get_ty_immutable(left_ty).as_ref()
|
||||||
{
|
{
|
||||||
let (binop_name, binop_assign_name) =
|
let normal_method_name = Binop::normal(op.base).op_info().method_name;
|
||||||
(binop_name(op).into(), binop_assign_name(op).into());
|
let assign_method_name = Binop::aug_assign(op.base).op_info().method_name;
|
||||||
|
|
||||||
// if is aug_assign, try aug_assign operator first
|
// if is aug_assign, try aug_assign operator first
|
||||||
if is_aug_assign && fields.contains_key(&binop_assign_name) {
|
if op.variant == BinopVariant::AugAssign
|
||||||
binop_assign_name
|
&& fields.contains_key(&assign_method_name.into())
|
||||||
|
{
|
||||||
|
assign_method_name
|
||||||
} else {
|
} else {
|
||||||
binop_name
|
normal_method_name
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
binop_name(op).into()
|
op.op_info().method_name
|
||||||
};
|
};
|
||||||
|
|
||||||
let ret = if is_aug_assign {
|
let ret = match op.variant {
|
||||||
// The type of augmented assignment operator should never change
|
BinopVariant::Normal => {
|
||||||
Some(left_ty)
|
typeof_binop(self.unifier, self.primitives, op.base, left_ty, right_ty)
|
||||||
} else {
|
.map_err(|e| HashSet::from([format!("{e} (at {location})")]))?
|
||||||
typeof_binop(self.unifier, self.primitives, op, left_ty, right_ty)
|
}
|
||||||
.map_err(|e| HashSet::from([format!("{e} (at {location})")]))?
|
BinopVariant::AugAssign => {
|
||||||
|
// The type of augmented assignment operator should never change
|
||||||
|
Some(left_ty)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
self.build_method_call(location, method, left_ty, vec![right_ty], ret)
|
self.build_method_call(
|
||||||
|
location,
|
||||||
|
method.into(),
|
||||||
|
left_ty,
|
||||||
|
vec![right_ty],
|
||||||
|
ret,
|
||||||
|
Some(OperatorInfo::IsBinaryOp { self_type: left.custom.unwrap(), operator: op }),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn infer_unary_ops(
|
fn infer_unary_ops(
|
||||||
|
@ -1759,12 +1794,19 @@ impl<'a> Inferencer<'a> {
|
||||||
op: ast::Unaryop,
|
op: ast::Unaryop,
|
||||||
operand: &ast::Expr<Option<Type>>,
|
operand: &ast::Expr<Option<Type>>,
|
||||||
) -> InferenceResult {
|
) -> InferenceResult {
|
||||||
let method = unaryop_name(op).into();
|
let method = op.op_info().method_name.into();
|
||||||
|
|
||||||
let ret = typeof_unaryop(self.unifier, self.primitives, op, operand.custom.unwrap())
|
let ret = typeof_unaryop(self.unifier, self.primitives, op, operand.custom.unwrap())
|
||||||
.map_err(|e| HashSet::from([format!("{e} (at {location})")]))?;
|
.map_err(|e| HashSet::from([format!("{e} (at {location})")]))?;
|
||||||
|
|
||||||
self.build_method_call(location, method, operand.custom.unwrap(), vec![], ret)
|
self.build_method_call(
|
||||||
|
location,
|
||||||
|
method,
|
||||||
|
operand.custom.unwrap(),
|
||||||
|
vec![],
|
||||||
|
ret,
|
||||||
|
Some(OperatorInfo::IsUnaryOp { self_type: operand.custom.unwrap(), operator: op }),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn infer_compare(
|
fn infer_compare(
|
||||||
|
@ -1789,9 +1831,11 @@ impl<'a> Inferencer<'a> {
|
||||||
|
|
||||||
let mut res = None;
|
let mut res = None;
|
||||||
for (a, b, c) in izip!(once(left).chain(comparators), comparators, ops) {
|
for (a, b, c) in izip!(once(left).chain(comparators), comparators, ops) {
|
||||||
let method = comparison_name(*c)
|
if !OpInfo::supports_cmpop(*c) {
|
||||||
.ok_or_else(|| HashSet::from(["unsupported comparator".to_string()]))?
|
return Err(HashSet::from(["unsupported comparator".to_string()]));
|
||||||
.into();
|
}
|
||||||
|
|
||||||
|
let method = c.op_info().method_name.into();
|
||||||
|
|
||||||
let ret = typeof_cmpop(
|
let ret = typeof_cmpop(
|
||||||
self.unifier,
|
self.unifier,
|
||||||
|
@ -1808,6 +1852,10 @@ impl<'a> Inferencer<'a> {
|
||||||
a.custom.unwrap(),
|
a.custom.unwrap(),
|
||||||
vec![b.custom.unwrap()],
|
vec![b.custom.unwrap()],
|
||||||
ret,
|
ret,
|
||||||
|
Some(OperatorInfo::IsComparisonOp {
|
||||||
|
self_type: left.custom.unwrap(),
|
||||||
|
operator: *c,
|
||||||
|
}),
|
||||||
)?);
|
)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1827,13 +1875,9 @@ impl<'a> Inferencer<'a> {
|
||||||
TypeEnum::TVar { is_const_generic: false, .. }
|
TypeEnum::TVar { is_const_generic: false, .. }
|
||||||
));
|
));
|
||||||
|
|
||||||
let constrained_ty = NDArrayType::from_primitive(
|
let constrained_ty =
|
||||||
self.unifier,
|
make_ndarray_ty(self.unifier, self.primitives, Some(dummy_tvar), Some(ndims));
|
||||||
self.primitives,
|
self.constrain(value.custom.unwrap(), constrained_ty, &value.location)?;
|
||||||
Some(dummy_tvar),
|
|
||||||
Some(ndims),
|
|
||||||
);
|
|
||||||
self.constrain(value.custom.unwrap(), constrained_ty.into(), &value.location)?;
|
|
||||||
|
|
||||||
let TypeEnum::TLiteral { values, .. } = &*self.unifier.get_ty_immutable(ndims) else {
|
let TypeEnum::TLiteral { values, .. } = &*self.unifier.get_ty_immutable(ndims) else {
|
||||||
panic!("Expected TLiteral for ndarray.ndims, got {}", self.unifier.stringify(ndims))
|
panic!("Expected TLiteral for ndarray.ndims, got {}", self.unifier.stringify(ndims))
|
||||||
|
@ -1899,14 +1943,10 @@ impl<'a> Inferencer<'a> {
|
||||||
let ndims_ty = self
|
let ndims_ty = self
|
||||||
.unifier
|
.unifier
|
||||||
.get_fresh_literal(new_ndims.into_iter().map(SymbolValue::U64).collect(), None);
|
.get_fresh_literal(new_ndims.into_iter().map(SymbolValue::U64).collect(), None);
|
||||||
let subscripted_ty = NDArrayType::from_primitive(
|
let subscripted_ty =
|
||||||
self.unifier,
|
make_ndarray_ty(self.unifier, self.primitives, Some(dummy_tvar), Some(ndims_ty));
|
||||||
self.primitives,
|
|
||||||
Some(dummy_tvar),
|
|
||||||
Some(ndims_ty),
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(subscripted_ty.into())
|
Ok(subscripted_ty)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1916,6 +1956,18 @@ impl<'a> Inferencer<'a> {
|
||||||
slice: &ast::Expr<Option<Type>>,
|
slice: &ast::Expr<Option<Type>>,
|
||||||
ctx: ExprContext,
|
ctx: ExprContext,
|
||||||
) -> InferenceResult {
|
) -> InferenceResult {
|
||||||
|
let report_unscriptable_error = |unifier: &mut Unifier| {
|
||||||
|
// User is attempting to index into a value of an unsupported type.
|
||||||
|
|
||||||
|
let value_ty = value.custom.unwrap();
|
||||||
|
let value_ty_str = unifier.stringify(value_ty);
|
||||||
|
|
||||||
|
return report_error(
|
||||||
|
format!("'{value_ty_str}' object is not subscriptable").as_str(),
|
||||||
|
slice.location, // using the slice's location (rather than value's) because it is more clear
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
let ty = self.unifier.get_dummy_var().ty;
|
let ty = self.unifier.get_dummy_var().ty;
|
||||||
match &slice.node {
|
match &slice.node {
|
||||||
ExprKind::Slice { lower, upper, step } => {
|
ExprKind::Slice { lower, upper, step } => {
|
||||||
|
@ -1923,22 +1975,26 @@ impl<'a> Inferencer<'a> {
|
||||||
self.constrain(v.custom.unwrap(), self.primitives.int32, &v.location)?;
|
self.constrain(v.custom.unwrap(), self.primitives.int32, &v.location)?;
|
||||||
}
|
}
|
||||||
let list_like_ty = match &*self.unifier.get_ty(value.custom.unwrap()) {
|
let list_like_ty = match &*self.unifier.get_ty(value.custom.unwrap()) {
|
||||||
TypeEnum::TList { .. } => self.unifier.add_ty(TypeEnum::TList { ty }),
|
TypeEnum::TObj { obj_id, params, .. } if *obj_id == PrimDef::List.id() => {
|
||||||
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
let list_tvar = iter_type_vars(params).nth(0).unwrap();
|
||||||
let ndims = NDArrayType::create(value.custom.unwrap(), self.unifier)
|
self.unifier
|
||||||
.ndims_tvar(self.unifier)
|
.subst(
|
||||||
.ty;
|
self.primitives.list,
|
||||||
|
&into_var_map([TypeVar { id: list_tvar.id, ty }]),
|
||||||
NDArrayType::from_primitive(
|
)
|
||||||
self.unifier,
|
.unwrap()
|
||||||
self.primitives,
|
|
||||||
Some(ty),
|
|
||||||
Some(ndims),
|
|
||||||
)
|
|
||||||
.into()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => unreachable!(),
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
||||||
|
let (_, ndims) =
|
||||||
|
unpack_ndarray_var_tys(self.unifier, value.custom.unwrap());
|
||||||
|
|
||||||
|
make_ndarray_ty(self.unifier, self.primitives, Some(ty), Some(ndims))
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => {
|
||||||
|
return report_unscriptable_error(self.unifier);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
self.constrain(value.custom.unwrap(), list_like_ty, &value.location)?;
|
self.constrain(value.custom.unwrap(), list_like_ty, &value.location)?;
|
||||||
Ok(list_like_ty)
|
Ok(list_like_ty)
|
||||||
|
@ -1946,10 +2002,8 @@ impl<'a> Inferencer<'a> {
|
||||||
ExprKind::Constant { value: ast::Constant::Int(val), .. } => {
|
ExprKind::Constant { value: ast::Constant::Int(val), .. } => {
|
||||||
match &*self.unifier.get_ty(value.custom.unwrap()) {
|
match &*self.unifier.get_ty(value.custom.unwrap()) {
|
||||||
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
||||||
let ndims = NDArrayType::create(value.custom.unwrap(), self.unifier)
|
let (_, ndims) =
|
||||||
.ndims_tvar(self.unifier)
|
unpack_ndarray_var_tys(self.unifier, value.custom.unwrap());
|
||||||
.ty;
|
|
||||||
|
|
||||||
self.infer_subscript_ndarray(value, slice, ty, ndims)
|
self.infer_subscript_ndarray(value, slice, ty, ndims)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -1992,10 +2046,7 @@ impl<'a> Inferencer<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let ndims = NDArrayType::create(value.custom.unwrap(), self.unifier)
|
let (_, ndims) = unpack_ndarray_var_tys(self.unifier, value.custom.unwrap());
|
||||||
.ndims_tvar(self.unifier)
|
|
||||||
.ty;
|
|
||||||
|
|
||||||
self.infer_subscript_ndarray(value, slice, ty, ndims)
|
self.infer_subscript_ndarray(value, slice, ty, ndims)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -2008,20 +2059,26 @@ impl<'a> Inferencer<'a> {
|
||||||
|
|
||||||
// the index is not a constant, so value can only be a list-like structure
|
// the index is not a constant, so value can only be a list-like structure
|
||||||
match &*self.unifier.get_ty(value.custom.unwrap()) {
|
match &*self.unifier.get_ty(value.custom.unwrap()) {
|
||||||
TypeEnum::TList { .. } => {
|
TypeEnum::TObj { obj_id, params, .. } if *obj_id == PrimDef::List.id() => {
|
||||||
self.constrain(
|
self.constrain(
|
||||||
slice.custom.unwrap(),
|
slice.custom.unwrap(),
|
||||||
self.primitives.int32,
|
self.primitives.int32,
|
||||||
&slice.location,
|
&slice.location,
|
||||||
)?;
|
)?;
|
||||||
let list = self.unifier.add_ty(TypeEnum::TList { ty });
|
let list_tvar = iter_type_vars(params).nth(0).unwrap();
|
||||||
|
let list = self
|
||||||
|
.unifier
|
||||||
|
.subst(
|
||||||
|
self.primitives.list,
|
||||||
|
&into_var_map([TypeVar { id: list_tvar.id, ty }]),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
self.constrain(value.custom.unwrap(), list, &value.location)?;
|
self.constrain(value.custom.unwrap(), list, &value.location)?;
|
||||||
Ok(ty)
|
Ok(ty)
|
||||||
}
|
}
|
||||||
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
||||||
let ndims = NDArrayType::create(value.custom.unwrap(), self.unifier)
|
let (_, ndims) =
|
||||||
.ndims_tvar(self.unifier)
|
unpack_ndarray_var_tys(self.unifier, value.custom.unwrap());
|
||||||
.ty;
|
|
||||||
|
|
||||||
let valid_index_tys = [self.primitives.int32, self.primitives.isize()]
|
let valid_index_tys = [self.primitives.int32, self.primitives.isize()]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -2034,7 +2091,7 @@ impl<'a> Inferencer<'a> {
|
||||||
self.constrain(slice.custom.unwrap(), valid_index_ty, &slice.location)?;
|
self.constrain(slice.custom.unwrap(), valid_index_ty, &slice.location)?;
|
||||||
self.infer_subscript_ndarray(value, slice, ty, ndims)
|
self.infer_subscript_ndarray(value, slice, ty, ndims)
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => report_unscriptable_error(self.unifier),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -139,7 +139,12 @@ impl TestEnvironment {
|
||||||
fields: HashMap::new(),
|
fields: HashMap::new(),
|
||||||
params: VarMap::new(),
|
params: VarMap::new(),
|
||||||
});
|
});
|
||||||
let option = OptionType::create(option, &mut unifier);
|
let list_elem_tvar = unifier.get_fresh_var(Some("list_elem".into()), None);
|
||||||
|
let list = unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id: PrimDef::List.id(),
|
||||||
|
fields: HashMap::new(),
|
||||||
|
params: into_var_map([list_elem_tvar]),
|
||||||
|
});
|
||||||
let ndarray_dtype_tvar = unifier.get_fresh_var(Some("ndarray_dtype".into()), None);
|
let ndarray_dtype_tvar = unifier.get_fresh_var(Some("ndarray_dtype".into()), None);
|
||||||
let ndarray_ndims_tvar =
|
let ndarray_ndims_tvar =
|
||||||
unifier.get_fresh_const_generic_var(uint64, Some("ndarray_ndims".into()), None);
|
unifier.get_fresh_const_generic_var(uint64, Some("ndarray_ndims".into()), None);
|
||||||
|
@ -148,7 +153,6 @@ impl TestEnvironment {
|
||||||
fields: HashMap::new(),
|
fields: HashMap::new(),
|
||||||
params: into_var_map([ndarray_dtype_tvar, ndarray_ndims_tvar]),
|
params: into_var_map([ndarray_dtype_tvar, ndarray_ndims_tvar]),
|
||||||
});
|
});
|
||||||
let ndarray = NDArrayType::create(ndarray, &mut unifier);
|
|
||||||
let primitives = PrimitiveStore {
|
let primitives = PrimitiveStore {
|
||||||
int32,
|
int32,
|
||||||
int64,
|
int64,
|
||||||
|
@ -161,6 +165,7 @@ impl TestEnvironment {
|
||||||
uint32,
|
uint32,
|
||||||
uint64,
|
uint64,
|
||||||
option,
|
option,
|
||||||
|
list,
|
||||||
ndarray,
|
ndarray,
|
||||||
size_t: 64,
|
size_t: 64,
|
||||||
};
|
};
|
||||||
|
@ -275,17 +280,35 @@ impl TestEnvironment {
|
||||||
fields: HashMap::new(),
|
fields: HashMap::new(),
|
||||||
params: VarMap::new(),
|
params: VarMap::new(),
|
||||||
});
|
});
|
||||||
let option = OptionType::create(option, &mut unifier);
|
let list_elem_tvar = unifier.get_fresh_var(Some("list_elem".into()), None);
|
||||||
|
let list = unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id: PrimDef::List.id(),
|
||||||
|
fields: HashMap::new(),
|
||||||
|
params: into_var_map([list_elem_tvar]),
|
||||||
|
});
|
||||||
let ndarray = unifier.add_ty(TypeEnum::TObj {
|
let ndarray = unifier.add_ty(TypeEnum::TObj {
|
||||||
obj_id: PrimDef::NDArray.id(),
|
obj_id: PrimDef::NDArray.id(),
|
||||||
fields: HashMap::new(),
|
fields: HashMap::new(),
|
||||||
params: VarMap::new(),
|
params: VarMap::new(),
|
||||||
});
|
});
|
||||||
let ndarray = NDArrayType::create(ndarray, &mut unifier);
|
|
||||||
identifier_mapping.insert("None".into(), none);
|
identifier_mapping.insert("None".into(), none);
|
||||||
for (i, name) in ["int32", "int64", "float", "bool", "none", "range", "str", "Exception"]
|
for (i, name) in [
|
||||||
.iter()
|
"int32",
|
||||||
.enumerate()
|
"int64",
|
||||||
|
"float",
|
||||||
|
"bool",
|
||||||
|
"none",
|
||||||
|
"range",
|
||||||
|
"str",
|
||||||
|
"Exception",
|
||||||
|
"uint32",
|
||||||
|
"uint64",
|
||||||
|
"Option",
|
||||||
|
"list",
|
||||||
|
"ndarray",
|
||||||
|
]
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
{
|
{
|
||||||
top_level_defs.push(
|
top_level_defs.push(
|
||||||
RwLock::new(TopLevelDef::Class {
|
RwLock::new(TopLevelDef::Class {
|
||||||
|
@ -303,7 +326,7 @@ impl TestEnvironment {
|
||||||
.into(),
|
.into(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let defs = 7;
|
let defs = 12;
|
||||||
|
|
||||||
let primitives = PrimitiveStore {
|
let primitives = PrimitiveStore {
|
||||||
int32,
|
int32,
|
||||||
|
@ -317,6 +340,7 @@ impl TestEnvironment {
|
||||||
uint32,
|
uint32,
|
||||||
uint64,
|
uint64,
|
||||||
option,
|
option,
|
||||||
|
list,
|
||||||
ndarray,
|
ndarray,
|
||||||
size_t: 64,
|
size_t: 64,
|
||||||
};
|
};
|
||||||
|
@ -428,6 +452,11 @@ impl TestEnvironment {
|
||||||
"range".into(),
|
"range".into(),
|
||||||
"str".into(),
|
"str".into(),
|
||||||
"exception".into(),
|
"exception".into(),
|
||||||
|
"uint32".into(),
|
||||||
|
"uint64".into(),
|
||||||
|
"option".into(),
|
||||||
|
"list".into(),
|
||||||
|
"ndarray".into(),
|
||||||
"Foo".into(),
|
"Foo".into(),
|
||||||
"Bar".into(),
|
"Bar".into(),
|
||||||
"Bar2".into(),
|
"Bar2".into(),
|
||||||
|
|
|
@ -8,12 +8,15 @@ use std::rc::Rc;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::{borrow::Cow, collections::HashSet};
|
use std::{borrow::Cow, collections::HashSet};
|
||||||
|
|
||||||
use nac3parser::ast::{Location, StrRef};
|
use nac3parser::ast::{Cmpop, Location, StrRef, Unaryop};
|
||||||
|
|
||||||
|
use super::magic_methods::Binop;
|
||||||
use super::type_error::{TypeError, TypeErrorKind};
|
use super::type_error::{TypeError, TypeErrorKind};
|
||||||
use super::unification_table::{UnificationKey, UnificationTable};
|
use super::unification_table::{UnificationKey, UnificationTable};
|
||||||
use crate::symbol_resolver::SymbolValue;
|
use crate::symbol_resolver::SymbolValue;
|
||||||
|
use crate::toplevel::helper::PrimDef;
|
||||||
use crate::toplevel::{DefinitionId, TopLevelContext, TopLevelDef};
|
use crate::toplevel::{DefinitionId, TopLevelContext, TopLevelDef};
|
||||||
|
use crate::typecheck::magic_methods::OpInfo;
|
||||||
use crate::typecheck::type_inferencer::PrimitiveStore;
|
use crate::typecheck::type_inferencer::PrimitiveStore;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -22,40 +25,6 @@ mod test;
|
||||||
/// Handle for a type, implemented as a key in the unification table.
|
/// Handle for a type, implemented as a key in the unification table.
|
||||||
pub type Type = UnificationKey;
|
pub type Type = UnificationKey;
|
||||||
|
|
||||||
/// Macro for generating functions related to type traits, e.g. whether the type is integral.
|
|
||||||
macro_rules! primitive_type_trait_fn {
|
|
||||||
($id:ident, $( $matches:ident ),*) => {
|
|
||||||
#[must_use]
|
|
||||||
pub fn $id(self, unifier: &mut Unifier, store: &PrimitiveStore) -> bool {
|
|
||||||
[$(store.$matches,)*].into_iter().any(|ty| unifier.unioned(self, ty))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Type {
|
|
||||||
/// Wrapper function for cleaner code so that we don't need to write this long pattern matching
|
|
||||||
/// just to get the field `obj_id`.
|
|
||||||
#[must_use]
|
|
||||||
pub fn obj_id(self, unifier: &Unifier) -> Option<DefinitionId> {
|
|
||||||
if let TypeEnum::TObj { obj_id, .. } = &*unifier.get_ty_immutable(self) {
|
|
||||||
Some(*obj_id)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
pub fn is_primitive(self, unifier: &mut Unifier, store: &PrimitiveStore) -> bool {
|
|
||||||
store.into_iter().any(|ty| unifier.unioned(self, ty))
|
|
||||||
}
|
|
||||||
|
|
||||||
primitive_type_trait_fn!(is_integral, bool, int32, int64, uint32, uint64);
|
|
||||||
primitive_type_trait_fn!(is_floating_point, float);
|
|
||||||
primitive_type_trait_fn!(is_arithmetic, int32, int64, uint32, uint64, float);
|
|
||||||
primitive_type_trait_fn!(is_signed, int32, uint32, float);
|
|
||||||
primitive_type_trait_fn!(is_unsigned, uint32, uint64);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||||
pub struct CallId(pub(super) usize);
|
pub struct CallId(pub(super) usize);
|
||||||
|
|
||||||
|
@ -89,24 +58,6 @@ pub struct TypeVar {
|
||||||
pub ty: Type,
|
pub ty: Type,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<(TypeVarId, Type)> for TypeVar {
|
|
||||||
fn from((id, ty): (TypeVarId, Type)) -> Self {
|
|
||||||
TypeVar { id, ty }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<(&TypeVarId, &Type)> for TypeVar {
|
|
||||||
fn from((id, ty): (&TypeVarId, &Type)) -> Self {
|
|
||||||
TypeVar { id: *id, ty: *ty }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<TypeVar> for (TypeVarId, Type) {
|
|
||||||
fn from(value: TypeVar) -> Self {
|
|
||||||
(value.id, value.ty)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The mapping between [`TypeVarId`] and [unifier type][`Type`].
|
/// The mapping between [`TypeVarId`] and [unifier type][`Type`].
|
||||||
pub type VarMap = IndexMapping<TypeVarId>;
|
pub type VarMap = IndexMapping<TypeVarId>;
|
||||||
|
|
||||||
|
@ -120,84 +71,31 @@ where
|
||||||
vars.into_iter().map(|var| (var.id, var.ty)).collect()
|
vars.into_iter().map(|var| (var.id, var.ty)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A trait representing a possibly generic object type.
|
/// Get an iterator of [`TypeVar`]s from a [`VarMap`]
|
||||||
pub trait GenericObjectType
|
pub fn iter_type_vars(var_map: &VarMap) -> impl Iterator<Item = TypeVar> + '_ {
|
||||||
where
|
var_map.iter().map(|(&id, &ty)| TypeVar { id, ty })
|
||||||
Self: Sized,
|
|
||||||
{
|
|
||||||
fn try_create(ty: Type, unifier: &mut Unifier) -> Option<Self>;
|
|
||||||
|
|
||||||
/// Creates an instance from a [`Type`].
|
|
||||||
#[must_use]
|
|
||||||
fn create(ty: Type, unifier: &mut Unifier) -> Self {
|
|
||||||
Self::try_create(ty, unifier).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the [`Type`] underlying this instance.
|
|
||||||
#[must_use]
|
|
||||||
fn get_type(&self) -> Type;
|
|
||||||
|
|
||||||
/// Similar to [`Type::obj_id`], except that the [`DefinitionId`] is not wrapped within an
|
|
||||||
/// [`Option`].
|
|
||||||
#[must_use]
|
|
||||||
fn obj_id(&self, unifier: &Unifier) -> DefinitionId {
|
|
||||||
self.get_type().obj_id(unifier).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a copy of the [`VarMap`] of this object type.
|
|
||||||
#[must_use]
|
|
||||||
fn var_map(&self, unifier: &mut Unifier) -> VarMap {
|
|
||||||
let TypeEnum::TObj { params, .. } = &*unifier.get_ty(self.get_type()) else {
|
|
||||||
unreachable!()
|
|
||||||
};
|
|
||||||
|
|
||||||
params.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates an iterator over the [`VarMap`] of this object type, applying `iter_fn` on the
|
|
||||||
/// created [`Iterator`].
|
|
||||||
#[must_use]
|
|
||||||
fn iter_var_map<R, IterFn: FnOnce(&mut dyn Iterator<Item = TypeVar>, &mut Unifier) -> R>(
|
|
||||||
&self,
|
|
||||||
unifier: &mut Unifier,
|
|
||||||
iter_fn: IterFn,
|
|
||||||
) -> R {
|
|
||||||
let TypeEnum::TObj { params, .. } = &*unifier.get_ty(self.get_type()) else {
|
|
||||||
unreachable!()
|
|
||||||
};
|
|
||||||
|
|
||||||
let res = iter_fn(&mut params.iter().map(TypeVar::from), unifier);
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the [`TypeVar`] instance at the given index.
|
|
||||||
#[must_use]
|
|
||||||
fn get_var_at(&self, unifier: &mut Unifier, i: usize) -> Option<TypeVar> {
|
|
||||||
self.iter_var_map(unifier, |iter, _| iter.nth(i))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: GenericObjectType> From<T> for Type {
|
#[derive(Debug, Clone)]
|
||||||
fn from(value: T) -> Self {
|
pub enum OperatorInfo {
|
||||||
value.get_type()
|
/// The call was written as an unary operation, e.g., `~a` or `not a`.
|
||||||
}
|
IsUnaryOp {
|
||||||
}
|
/// The [`Type`] of the `self` object
|
||||||
|
self_type: Type,
|
||||||
/// An adapter that converts [`Type`] into
|
operator: Unaryop,
|
||||||
pub struct GenericTypeAdapter(Type);
|
},
|
||||||
|
/// The call was written as a binary operation, e.g., `a + b` or `a += b`.
|
||||||
impl GenericObjectType for GenericTypeAdapter {
|
IsBinaryOp {
|
||||||
fn try_create(ty: Type, unifier: &mut Unifier) -> Option<Self> {
|
/// The [`Type`] of the `self` object
|
||||||
if let TypeEnum::TObj { .. } = &*unifier.get_ty_immutable(ty) {
|
self_type: Type,
|
||||||
Some(GenericTypeAdapter(ty))
|
operator: Binop,
|
||||||
} else {
|
},
|
||||||
None
|
/// The call was written as a binary comparison operation, e.g., `a < b`.
|
||||||
}
|
IsComparisonOp {
|
||||||
}
|
/// The [`Type`] of the `self` object
|
||||||
|
self_type: Type,
|
||||||
fn get_type(&self) -> Type {
|
operator: Cmpop,
|
||||||
self.0
|
},
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
@ -207,6 +105,9 @@ pub struct Call {
|
||||||
pub ret: Type,
|
pub ret: Type,
|
||||||
pub fun: RefCell<Option<Type>>,
|
pub fun: RefCell<Option<Type>>,
|
||||||
pub loc: Option<Location>,
|
pub loc: Option<Location>,
|
||||||
|
|
||||||
|
/// Details about the associated Python user operator expression of this call, if any.
|
||||||
|
pub operator_info: Option<OperatorInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -236,6 +137,19 @@ pub enum RecordKey {
|
||||||
Int(i32),
|
Int(i32),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Type {
|
||||||
|
/// Wrapper function for cleaner code so that we don't need to write this long pattern matching
|
||||||
|
/// just to get the field `obj_id`.
|
||||||
|
#[must_use]
|
||||||
|
pub fn obj_id(self, unifier: &Unifier) -> Option<DefinitionId> {
|
||||||
|
if let TypeEnum::TObj { obj_id, .. } = &*unifier.get_ty_immutable(self) {
|
||||||
|
Some(*obj_id)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<&RecordKey> for StrRef {
|
impl From<&RecordKey> for StrRef {
|
||||||
fn from(r: &RecordKey) -> Self {
|
fn from(r: &RecordKey) -> Self {
|
||||||
match r {
|
match r {
|
||||||
|
@ -321,12 +235,6 @@ pub enum TypeEnum {
|
||||||
ty: Vec<Type>,
|
ty: Vec<Type>,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// A list type.
|
|
||||||
TList {
|
|
||||||
/// The type of elements present in this list.
|
|
||||||
ty: Type,
|
|
||||||
},
|
|
||||||
|
|
||||||
/// An object type.
|
/// An object type.
|
||||||
TObj {
|
TObj {
|
||||||
/// The [`DefinitionId`] of this object type.
|
/// The [`DefinitionId`] of this object type.
|
||||||
|
@ -360,7 +268,6 @@ impl TypeEnum {
|
||||||
TypeEnum::TVar { .. } => "TVar",
|
TypeEnum::TVar { .. } => "TVar",
|
||||||
TypeEnum::TLiteral { .. } => "TConstant",
|
TypeEnum::TLiteral { .. } => "TConstant",
|
||||||
TypeEnum::TTuple { .. } => "TTuple",
|
TypeEnum::TTuple { .. } => "TTuple",
|
||||||
TypeEnum::TList { .. } => "TList",
|
|
||||||
TypeEnum::TObj { .. } => "TObj",
|
TypeEnum::TObj { .. } => "TObj",
|
||||||
TypeEnum::TVirtual { .. } => "TVirtual",
|
TypeEnum::TVirtual { .. } => "TVirtual",
|
||||||
TypeEnum::TCall { .. } => "TCall",
|
TypeEnum::TCall { .. } => "TCall",
|
||||||
|
@ -596,9 +503,27 @@ impl Unifier {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TypeEnum::TList { ty } => self
|
TypeEnum::TObj { obj_id, params, .. } if *obj_id == PrimDef::List.id() => {
|
||||||
.get_instantiations(*ty)
|
let tv = iter_type_vars(params).nth(0).unwrap();
|
||||||
.map(|ty| ty.iter().map(|&ty| self.add_ty(TypeEnum::TList { ty })).collect_vec()),
|
|
||||||
|
let tv_id = if let TypeEnum::TVar { id, .. } =
|
||||||
|
self.unification_table.probe_value(tv.ty).as_ref()
|
||||||
|
{
|
||||||
|
*id
|
||||||
|
} else {
|
||||||
|
tv.id
|
||||||
|
};
|
||||||
|
|
||||||
|
self.get_instantiations(tv.ty).map(|ty_insts| {
|
||||||
|
ty_insts
|
||||||
|
.iter()
|
||||||
|
.map(|&ty_inst| {
|
||||||
|
self.subst(ty, &into_var_map([TypeVar { id: tv_id, ty: ty_inst }]))
|
||||||
|
.unwrap_or(ty)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
|
}
|
||||||
TypeEnum::TVirtual { ty } => self.get_instantiations(*ty).map(|ty| {
|
TypeEnum::TVirtual { ty } => self.get_instantiations(*ty).map(|ty| {
|
||||||
ty.iter().map(|&ty| self.add_ty(TypeEnum::TVirtual { ty })).collect_vec()
|
ty.iter().map(|&ty| self.add_ty(TypeEnum::TVirtual { ty })).collect_vec()
|
||||||
}),
|
}),
|
||||||
|
@ -655,9 +580,7 @@ impl Unifier {
|
||||||
|
|
||||||
TVar { .. } => allowed_typevars.iter().any(|b| self.unification_table.unioned(a, *b)),
|
TVar { .. } => allowed_typevars.iter().any(|b| self.unification_table.unioned(a, *b)),
|
||||||
TCall { .. } => false,
|
TCall { .. } => false,
|
||||||
TList { ty }
|
TVirtual { ty } => self.is_concrete(*ty, allowed_typevars),
|
||||||
| TVirtual { ty } => self.is_concrete(*ty, allowed_typevars),
|
|
||||||
|
|
||||||
TTuple { ty } => ty.iter().all(|ty| self.is_concrete(*ty, allowed_typevars)),
|
TTuple { ty } => ty.iter().all(|ty| self.is_concrete(*ty, allowed_typevars)),
|
||||||
TObj { params: vars, .. } => {
|
TObj { params: vars, .. } => {
|
||||||
vars.values().all(|ty| self.is_concrete(*ty, allowed_typevars))
|
vars.values().all(|ty| self.is_concrete(*ty, allowed_typevars))
|
||||||
|
@ -732,111 +655,178 @@ impl Unifier {
|
||||||
let TypeEnum::TFunc(signature) = &*self.get_ty(b) else { unreachable!() };
|
let TypeEnum::TFunc(signature) = &*self.get_ty(b) else { unreachable!() };
|
||||||
|
|
||||||
// Get details about the input arguments
|
// Get details about the input arguments
|
||||||
let Call { posargs, kwargs, ret, fun, loc } = call;
|
let Call { posargs, kwargs, ret, fun, loc, operator_info } = call;
|
||||||
let num_args = posargs.len() + kwargs.len();
|
let num_args = posargs.len() + kwargs.len();
|
||||||
|
|
||||||
// Now we check the arguments against the parameters
|
// Now we check the arguments against the parameters,
|
||||||
|
// and depending on what `call_info` is, we might change how the behavior `unify_call()`
|
||||||
|
// in hopes to improve user error messages when type checking fails.
|
||||||
|
match operator_info {
|
||||||
|
Some(OperatorInfo::IsBinaryOp { self_type, operator }) => {
|
||||||
|
// The call is written in the form of (say) `a + b`.
|
||||||
|
// Technically, it is `a.__add__(b)`, and they have the following constraints:
|
||||||
|
assert_eq!(posargs.len(), 1);
|
||||||
|
assert_eq!(kwargs.len(), 0);
|
||||||
|
assert_eq!(num_params, 1);
|
||||||
|
|
||||||
// Helper lambdas
|
let other_type = posargs[0]; // the second operand
|
||||||
let mut type_check_arg = |param_name, expected_arg_ty, arg_ty| {
|
let expected_other_type = signature.args[0].ty;
|
||||||
let ok = self.unify_impl(expected_arg_ty, arg_ty, false).is_ok();
|
|
||||||
if ok {
|
let ok = self.unify_impl(expected_other_type, other_type, false).is_ok();
|
||||||
Ok(())
|
if !ok {
|
||||||
} else {
|
self.restore_snapshot();
|
||||||
// Typecheck failed, throw an error.
|
return Err(TypeError::new(
|
||||||
self.restore_snapshot();
|
TypeErrorKind::UnsupportedBinaryOpTypes {
|
||||||
Err(TypeError::new(
|
operator: *operator,
|
||||||
TypeErrorKind::IncorrectArgType {
|
lhs_type: *self_type,
|
||||||
name: param_name,
|
rhs_type: other_type,
|
||||||
expected: expected_arg_ty,
|
expected_rhs_type: expected_other_type,
|
||||||
got: arg_ty,
|
},
|
||||||
},
|
*loc,
|
||||||
*loc,
|
));
|
||||||
))
|
}
|
||||||
}
|
}
|
||||||
};
|
Some(OperatorInfo::IsComparisonOp { self_type, operator })
|
||||||
|
if OpInfo::supports_cmpop(*operator) // Otherwise that comparison operator is not supported.
|
||||||
|
=>
|
||||||
|
{
|
||||||
|
// The call is written in the form of (say) `a <= b`.
|
||||||
|
// Technically, it is `a.__le__(b)`, and they have the following constraints:
|
||||||
|
assert_eq!(posargs.len(), 1);
|
||||||
|
assert_eq!(kwargs.len(), 0);
|
||||||
|
assert_eq!(num_params, 1);
|
||||||
|
|
||||||
// Check for "too many arguments"
|
let other_type = posargs[0]; // the second operand
|
||||||
if num_params < posargs.len() {
|
let expected_other_type = signature.args[0].ty;
|
||||||
let expected_min_count =
|
|
||||||
signature.args.iter().filter(|param| param.is_required()).count();
|
|
||||||
let expected_max_count = num_params;
|
|
||||||
|
|
||||||
self.restore_snapshot();
|
let ok = self.unify_impl(expected_other_type, other_type, false).is_ok();
|
||||||
return Err(TypeError::new(
|
if !ok {
|
||||||
TypeErrorKind::TooManyArguments {
|
self.restore_snapshot();
|
||||||
expected_min_count,
|
return Err(TypeError::new(
|
||||||
expected_max_count,
|
TypeErrorKind::UnsupportedComparsionOpTypes {
|
||||||
got_count: num_args,
|
operator: *operator,
|
||||||
},
|
lhs_type: *self_type,
|
||||||
*loc,
|
rhs_type: other_type,
|
||||||
));
|
expected_rhs_type: expected_other_type,
|
||||||
}
|
},
|
||||||
|
*loc,
|
||||||
// NOTE: order of `param_info_by_name` is leveraged, so use an IndexMap
|
));
|
||||||
let mut param_info_by_name: IndexMap<StrRef, ParamInfo> = signature
|
}
|
||||||
.args
|
|
||||||
.iter()
|
|
||||||
.map(|arg| (arg.name, ParamInfo { has_been_supplied: false, param: arg }))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
// Now consume all positional arguments and typecheck them.
|
|
||||||
for (&arg_ty, param) in zip(posargs, signature.args.iter()) {
|
|
||||||
// We will also use this opportunity to mark the corresponding `param_info` as having been supplied.
|
|
||||||
let param_info = param_info_by_name.get_mut(¶m.name).unwrap();
|
|
||||||
param_info.has_been_supplied = true;
|
|
||||||
|
|
||||||
// Typecheck
|
|
||||||
type_check_arg(param.name, param.ty, arg_ty)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now consume all keyword arguments and typecheck them.
|
|
||||||
for (¶m_name, &arg_ty) in kwargs {
|
|
||||||
// We will also use this opportunity to check if this keyword argument is "legal".
|
|
||||||
|
|
||||||
let Some(param_info) = param_info_by_name.get_mut(¶m_name) else {
|
|
||||||
self.restore_snapshot();
|
|
||||||
return Err(TypeError::new(TypeErrorKind::UnknownArgName(param_name), *loc));
|
|
||||||
};
|
|
||||||
|
|
||||||
if param_info.has_been_supplied {
|
|
||||||
// NOTE: Duplicate keyword argument (i.e., `hello(1, 2, 3, arg = 4, arg = 5)`)
|
|
||||||
// is IMPOSSIBLE as the parser would have already failed.
|
|
||||||
// We only have to care about "got multiple values for XYZ"
|
|
||||||
|
|
||||||
self.restore_snapshot();
|
|
||||||
return Err(TypeError::new(
|
|
||||||
TypeErrorKind::GotMultipleValues { name: param_name },
|
|
||||||
*loc,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
_ => {
|
||||||
|
// Handle [`CallInfo::IsNormalFunctionCall`] and other uninteresting variants
|
||||||
|
// of [`CallInfo`] (e.g, `CallInfo::IsUnaryOp` and unsupported comparison operators)
|
||||||
|
|
||||||
param_info.has_been_supplied = true;
|
// Helper lambdas
|
||||||
|
let mut type_check_arg = |param_name, expected_arg_ty, arg_ty| {
|
||||||
|
let ok = self.unify_impl(expected_arg_ty, arg_ty, false).is_ok();
|
||||||
|
if ok {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
// Typecheck failed, throw an error.
|
||||||
|
self.restore_snapshot();
|
||||||
|
Err(TypeError::new(
|
||||||
|
TypeErrorKind::IncorrectArgType {
|
||||||
|
name: param_name,
|
||||||
|
expected: expected_arg_ty,
|
||||||
|
got: arg_ty,
|
||||||
|
},
|
||||||
|
*loc,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Typecheck
|
// Check for "too many arguments"
|
||||||
type_check_arg(param_name, param_info.param.ty, arg_ty)?;
|
if num_params < posargs.len() {
|
||||||
}
|
let expected_min_count =
|
||||||
|
signature.args.iter().filter(|param| param.is_required()).count();
|
||||||
|
let expected_max_count = num_params;
|
||||||
|
|
||||||
// After checking posargs and kwargs, check if there are any
|
self.restore_snapshot();
|
||||||
// unsupplied required parameters, and throw an error if they exist.
|
return Err(TypeError::new(
|
||||||
let missing_arg_names = param_info_by_name
|
TypeErrorKind::TooManyArguments {
|
||||||
.values()
|
expected_min_count,
|
||||||
.filter(|param_info| param_info.param.is_required() && !param_info.has_been_supplied)
|
expected_max_count,
|
||||||
.map(|param_info| param_info.param.name)
|
got_count: num_args,
|
||||||
.collect_vec();
|
},
|
||||||
if !missing_arg_names.is_empty() {
|
*loc,
|
||||||
self.restore_snapshot();
|
));
|
||||||
return Err(TypeError::new(TypeErrorKind::MissingArgs { missing_arg_names }, *loc));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Finally, check the Call's return type
|
// NOTE: order of `param_info_by_name` is leveraged, so use an IndexMap
|
||||||
self.unify_impl(*ret, signature.ret, false).map_err(|mut err| {
|
let mut param_info_by_name: IndexMap<StrRef, ParamInfo> = signature
|
||||||
self.restore_snapshot();
|
.args
|
||||||
if err.loc.is_none() {
|
.iter()
|
||||||
err.loc = *loc;
|
.map(|arg| (arg.name, ParamInfo { has_been_supplied: false, param: arg }))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// Now consume all positional arguments and typecheck them.
|
||||||
|
for (&arg_ty, param) in zip(posargs, signature.args.iter()) {
|
||||||
|
// We will also use this opportunity to mark the corresponding `param_info` as having been supplied.
|
||||||
|
let param_info = param_info_by_name.get_mut(¶m.name).unwrap();
|
||||||
|
param_info.has_been_supplied = true;
|
||||||
|
|
||||||
|
// Typecheck
|
||||||
|
type_check_arg(param.name, param.ty, arg_ty)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now consume all keyword arguments and typecheck them.
|
||||||
|
for (¶m_name, &arg_ty) in kwargs {
|
||||||
|
// We will also use this opportunity to check if this keyword argument is "legal".
|
||||||
|
|
||||||
|
let Some(param_info) = param_info_by_name.get_mut(¶m_name) else {
|
||||||
|
self.restore_snapshot();
|
||||||
|
return Err(TypeError::new(
|
||||||
|
TypeErrorKind::UnknownArgName(param_name),
|
||||||
|
*loc,
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
|
if param_info.has_been_supplied {
|
||||||
|
// NOTE: Duplicate keyword argument (i.e., `hello(1, 2, 3, arg = 4, arg = 5)`)
|
||||||
|
// is IMPOSSIBLE as the parser would have already failed.
|
||||||
|
// We only have to care about "got multiple values for XYZ"
|
||||||
|
|
||||||
|
self.restore_snapshot();
|
||||||
|
return Err(TypeError::new(
|
||||||
|
TypeErrorKind::GotMultipleValues { name: param_name },
|
||||||
|
*loc,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
param_info.has_been_supplied = true;
|
||||||
|
|
||||||
|
// Typecheck
|
||||||
|
type_check_arg(param_name, param_info.param.ty, arg_ty)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// After checking posargs and kwargs, check if there are any
|
||||||
|
// unsupplied required parameters, and throw an error if they exist.
|
||||||
|
let missing_arg_names = param_info_by_name
|
||||||
|
.values()
|
||||||
|
.filter(|param_info| {
|
||||||
|
param_info.param.is_required() && !param_info.has_been_supplied
|
||||||
|
})
|
||||||
|
.map(|param_info| param_info.param.name)
|
||||||
|
.collect_vec();
|
||||||
|
if !missing_arg_names.is_empty() {
|
||||||
|
self.restore_snapshot();
|
||||||
|
return Err(TypeError::new(
|
||||||
|
TypeErrorKind::MissingArgs { missing_arg_names },
|
||||||
|
*loc,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally, check the Call's return type
|
||||||
|
self.unify_impl(*ret, signature.ret, false).map_err(|mut err| {
|
||||||
|
self.restore_snapshot();
|
||||||
|
if err.loc.is_none() {
|
||||||
|
err.loc = *loc;
|
||||||
|
}
|
||||||
|
err
|
||||||
|
})?;
|
||||||
}
|
}
|
||||||
err
|
}
|
||||||
})?;
|
|
||||||
|
|
||||||
*fun.borrow_mut() = Some(b);
|
*fun.borrow_mut() = Some(b);
|
||||||
|
|
||||||
|
@ -999,22 +989,6 @@ impl Unifier {
|
||||||
self.unify_impl(x, b, false)?;
|
self.unify_impl(x, b, false)?;
|
||||||
self.set_a_to_b(a, x);
|
self.set_a_to_b(a, x);
|
||||||
}
|
}
|
||||||
(TVar { fields: Some(fields), range, is_const_generic: false, .. }, TList { ty }) => {
|
|
||||||
for (k, v) in fields {
|
|
||||||
match *k {
|
|
||||||
RecordKey::Int(_) => {
|
|
||||||
self.unify_impl(v.ty, *ty, false).map_err(|e| e.at(v.loc))?;
|
|
||||||
}
|
|
||||||
RecordKey::Str(_) => {
|
|
||||||
return Err(TypeError::new(TypeErrorKind::NoSuchField(*k, b), v.loc))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let x = self.check_var_compatibility(b, range)?.unwrap_or(b);
|
|
||||||
self.unify_impl(x, b, false)?;
|
|
||||||
self.set_a_to_b(a, x);
|
|
||||||
}
|
|
||||||
|
|
||||||
(
|
(
|
||||||
TVar { id: id1, range: ty1, is_const_generic: true, .. },
|
TVar { id: id1, range: ty1, is_const_generic: true, .. },
|
||||||
TVar { id: id2, range: ty2, .. },
|
TVar { id: id2, range: ty2, .. },
|
||||||
|
@ -1093,13 +1067,7 @@ impl Unifier {
|
||||||
}
|
}
|
||||||
self.set_a_to_b(a, b);
|
self.set_a_to_b(a, b);
|
||||||
}
|
}
|
||||||
(TList { ty: ty1 }, TList { ty: ty2 }) => {
|
(TVar { fields: Some(map), range, .. }, TObj { obj_id, fields, params }) => {
|
||||||
if self.unify_impl(*ty1, *ty2, false).is_err() {
|
|
||||||
return Err(TypeError::new(TypeErrorKind::IncompatibleTypes(a, b), None));
|
|
||||||
}
|
|
||||||
self.set_a_to_b(a, b);
|
|
||||||
}
|
|
||||||
(TVar { fields: Some(map), range, .. }, TObj { fields, .. }) => {
|
|
||||||
for (k, field) in map {
|
for (k, field) in map {
|
||||||
match *k {
|
match *k {
|
||||||
RecordKey::Str(s) => {
|
RecordKey::Str(s) => {
|
||||||
|
@ -1118,10 +1086,18 @@ impl Unifier {
|
||||||
self.unify_impl(field.ty, ty, false).map_err(|v| v.at(field.loc))?;
|
self.unify_impl(field.ty, ty, false).map_err(|v| v.at(field.loc))?;
|
||||||
}
|
}
|
||||||
RecordKey::Int(_) => {
|
RecordKey::Int(_) => {
|
||||||
return Err(TypeError::new(
|
// Allow expressions such as list[0]
|
||||||
TypeErrorKind::NoSuchField(*k, b),
|
if *obj_id == PrimDef::List.id() {
|
||||||
field.loc,
|
let ty = iter_type_vars(params).nth(0).unwrap().ty;
|
||||||
))
|
|
||||||
|
self.unify_impl(field.ty, ty, false)
|
||||||
|
.map_err(|e| e.at(field.loc))?;
|
||||||
|
} else {
|
||||||
|
return Err(TypeError::new(
|
||||||
|
TypeErrorKind::NoSuchField(*k, b),
|
||||||
|
field.loc,
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1336,9 +1312,6 @@ impl Unifier {
|
||||||
ty.iter().map(|v| self.internal_stringify(*v, obj_to_name, var_to_name, notes));
|
ty.iter().map(|v| self.internal_stringify(*v, obj_to_name, var_to_name, notes));
|
||||||
format!("tuple[{}]", fields.join(", "))
|
format!("tuple[{}]", fields.join(", "))
|
||||||
}
|
}
|
||||||
TypeEnum::TList { ty } => {
|
|
||||||
format!("list[{}]", self.internal_stringify(*ty, obj_to_name, var_to_name, notes))
|
|
||||||
}
|
|
||||||
TypeEnum::TVirtual { ty } => {
|
TypeEnum::TVirtual { ty } => {
|
||||||
format!(
|
format!(
|
||||||
"virtual[{}]",
|
"virtual[{}]",
|
||||||
|
@ -1471,9 +1444,6 @@ impl Unifier {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TypeEnum::TList { ty } => {
|
|
||||||
self.subst_impl(*ty, mapping, cache).map(|t| self.add_ty(TypeEnum::TList { ty: t }))
|
|
||||||
}
|
|
||||||
TypeEnum::TVirtual { ty } => self
|
TypeEnum::TVirtual { ty } => self
|
||||||
.subst_impl(*ty, mapping, cache)
|
.subst_impl(*ty, mapping, cache)
|
||||||
.map(|t| self.add_ty(TypeEnum::TVirtual { ty: t })),
|
.map(|t| self.add_ty(TypeEnum::TVirtual { ty: t })),
|
||||||
|
@ -1484,6 +1454,7 @@ impl Unifier {
|
||||||
// This is also used to prevent infinite substitution...
|
// This is also used to prevent infinite substitution...
|
||||||
let need_subst = params.values().any(|v| {
|
let need_subst = params.values().any(|v| {
|
||||||
let ty = self.unification_table.probe_value(*v);
|
let ty = self.unification_table.probe_value(*v);
|
||||||
|
// TODO(Derppening): #444
|
||||||
if let TypeEnum::TVar { id, .. } = ty.as_ref() {
|
if let TypeEnum::TVar { id, .. } = ty.as_ref() {
|
||||||
mapping.contains_key(id)
|
mapping.contains_key(id)
|
||||||
} else {
|
} else {
|
||||||
|
@ -1640,8 +1611,22 @@ impl Unifier {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(TList { ty: ty1 }, TList { ty: ty2 }) => {
|
// TODO(Derppening): #444
|
||||||
Ok(self.get_intersection(*ty1, *ty2)?.map(|ty| self.add_ty(TList { ty })))
|
(
|
||||||
|
TObj { obj_id: id1, fields, params: params1 },
|
||||||
|
TObj { obj_id: id2, params: params2, .. },
|
||||||
|
) if *id1 == PrimDef::List.id() && *id2 == PrimDef::List.id() => {
|
||||||
|
let tv_id = iter_type_vars(params1).nth(0).unwrap().id;
|
||||||
|
let ty1 = iter_type_vars(params1).nth(0).unwrap().ty;
|
||||||
|
let ty2 = iter_type_vars(params2).nth(0).unwrap().ty;
|
||||||
|
|
||||||
|
Ok(self.get_intersection(ty1, ty2)?.map(|ty| {
|
||||||
|
self.add_ty(TObj {
|
||||||
|
obj_id: *id1,
|
||||||
|
fields: fields.clone(),
|
||||||
|
params: into_var_map([TypeVar { id: tv_id, ty }]),
|
||||||
|
})
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
(TVirtual { ty: ty1 }, TVirtual { ty: ty2 }) => {
|
(TVirtual { ty: ty1 }, TVirtual { ty: ty2 }) => {
|
||||||
Ok(self.get_intersection(*ty1, *ty2)?.map(|ty| self.add_ty(TVirtual { ty })))
|
Ok(self.get_intersection(*ty1, *ty2)?.map(|ty| self.add_ty(TVirtual { ty })))
|
||||||
|
|
|
@ -32,10 +32,7 @@ impl Unifier {
|
||||||
ty1.len() == ty2.len()
|
ty1.len() == ty2.len()
|
||||||
&& ty1.iter().zip(ty2.iter()).all(|(t1, t2)| self.eq(*t1, *t2))
|
&& ty1.iter().zip(ty2.iter()).all(|(t1, t2)| self.eq(*t1, *t2))
|
||||||
}
|
}
|
||||||
(TypeEnum::TList { ty: ty1 }, TypeEnum::TList { ty: ty2 })
|
(TypeEnum::TVirtual { ty: ty1 }, TypeEnum::TVirtual { ty: ty2 }) => self.eq(*ty1, *ty2),
|
||||||
| (TypeEnum::TVirtual { ty: ty1 }, TypeEnum::TVirtual { ty: ty2 }) => {
|
|
||||||
self.eq(*ty1, *ty2)
|
|
||||||
}
|
|
||||||
(
|
(
|
||||||
TypeEnum::TObj { obj_id: id1, params: params1, .. },
|
TypeEnum::TObj { obj_id: id1, params: params1, .. },
|
||||||
TypeEnum::TObj { obj_id: id2, params: params2, .. },
|
TypeEnum::TObj { obj_id: id2, params: params2, .. },
|
||||||
|
@ -119,6 +116,15 @@ impl TestEnvironment {
|
||||||
params: into_var_map([tvar]),
|
params: into_var_map([tvar]),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
let tvar = unifier.get_dummy_var();
|
||||||
|
type_mapping.insert(
|
||||||
|
"list".into(),
|
||||||
|
unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id: PrimDef::List.id(),
|
||||||
|
fields: HashMap::new(),
|
||||||
|
params: into_var_map([tvar]),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
TestEnvironment { unifier, type_mapping }
|
TestEnvironment { unifier, type_mapping }
|
||||||
}
|
}
|
||||||
|
@ -133,6 +139,36 @@ impl TestEnvironment {
|
||||||
// for testing only, so we can just panic when the input is malformed
|
// for testing only, so we can just panic when the input is malformed
|
||||||
let end = typ.find(|c| ['[', ',', ']', '='].contains(&c)).unwrap_or(typ.len());
|
let end = typ.find(|c| ['[', ',', ']', '='].contains(&c)).unwrap_or(typ.len());
|
||||||
match &typ[..end] {
|
match &typ[..end] {
|
||||||
|
"list" => {
|
||||||
|
let mut s = &typ[end..];
|
||||||
|
assert_eq!(&s[0..1], "[");
|
||||||
|
let mut ty = Vec::new();
|
||||||
|
while &s[0..1] != "]" {
|
||||||
|
let result = self.internal_parse(&s[1..], mapping);
|
||||||
|
ty.push(result.0);
|
||||||
|
s = result.1;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(ty.len(), 1);
|
||||||
|
|
||||||
|
let list_elem_tvar = if let TypeEnum::TObj { params, .. } =
|
||||||
|
&*self.unifier.get_ty_immutable(self.type_mapping["list"])
|
||||||
|
{
|
||||||
|
iter_type_vars(params).next().unwrap()
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
|
(
|
||||||
|
self.unifier
|
||||||
|
.subst(
|
||||||
|
self.type_mapping["list"],
|
||||||
|
&into_var_map([TypeVar { id: list_elem_tvar.id, ty: ty[0] }]),
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
&s[1..],
|
||||||
|
)
|
||||||
|
}
|
||||||
"tuple" => {
|
"tuple" => {
|
||||||
let mut s = &typ[end..];
|
let mut s = &typ[end..];
|
||||||
assert_eq!(&s[0..1], "[");
|
assert_eq!(&s[0..1], "[");
|
||||||
|
@ -144,12 +180,6 @@ impl TestEnvironment {
|
||||||
}
|
}
|
||||||
(self.unifier.add_ty(TypeEnum::TTuple { ty }), &s[1..])
|
(self.unifier.add_ty(TypeEnum::TTuple { ty }), &s[1..])
|
||||||
}
|
}
|
||||||
"list" => {
|
|
||||||
assert_eq!(&typ[end..=end], "[");
|
|
||||||
let (ty, s) = self.internal_parse(&typ[end + 1..], mapping);
|
|
||||||
assert_eq!(&s[0..1], "]");
|
|
||||||
(self.unifier.add_ty(TypeEnum::TList { ty }), &s[1..])
|
|
||||||
}
|
|
||||||
"Record" => {
|
"Record" => {
|
||||||
let mut s = &typ[end..];
|
let mut s = &typ[end..];
|
||||||
assert_eq!(&s[0..1], "[");
|
assert_eq!(&s[0..1], "[");
|
||||||
|
@ -274,7 +304,7 @@ fn test_unify(
|
||||||
("v1", "tuple[int]"),
|
("v1", "tuple[int]"),
|
||||||
("v2", "list[int]"),
|
("v2", "list[int]"),
|
||||||
],
|
],
|
||||||
(("v1", "v2"), "Incompatible types: list[0] and tuple[0]")
|
(("v1", "v2"), "Incompatible types: 11[0] and tuple[0]")
|
||||||
; "type mismatch"
|
; "type mismatch"
|
||||||
)]
|
)]
|
||||||
#[test_case(2,
|
#[test_case(2,
|
||||||
|
@ -298,7 +328,7 @@ fn test_unify(
|
||||||
("v1", "Record[a=float,b=int]"),
|
("v1", "Record[a=float,b=int]"),
|
||||||
("v2", "Foo[v3]"),
|
("v2", "Foo[v3]"),
|
||||||
],
|
],
|
||||||
(("v1", "v2"), "`3[typevar4]::b` field/method does not exist")
|
(("v1", "v2"), "`3[typevar5]::b` field/method does not exist")
|
||||||
; "record obj merge"
|
; "record obj merge"
|
||||||
)]
|
)]
|
||||||
/// Test cases for invalid unifications.
|
/// Test cases for invalid unifications.
|
||||||
|
@ -388,6 +418,14 @@ fn test_typevar_range() {
|
||||||
let int_list = env.parse("list[int]", &HashMap::new());
|
let int_list = env.parse("list[int]", &HashMap::new());
|
||||||
let float_list = env.parse("list[float]", &HashMap::new());
|
let float_list = env.parse("list[float]", &HashMap::new());
|
||||||
|
|
||||||
|
let list_elem_tvar = if let TypeEnum::TObj { params, .. } =
|
||||||
|
&*env.unifier.get_ty_immutable(env.type_mapping["list"])
|
||||||
|
{
|
||||||
|
iter_type_vars(params).next().unwrap()
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
// unification between v and int
|
// unification between v and int
|
||||||
// where v in (int, bool)
|
// where v in (int, bool)
|
||||||
let v = env.unifier.get_fresh_var_with_range(&[int, boolean], None, None).ty;
|
let v = env.unifier.get_fresh_var_with_range(&[int, boolean], None, None).ty;
|
||||||
|
@ -398,7 +436,7 @@ fn test_typevar_range() {
|
||||||
let v = env.unifier.get_fresh_var_with_range(&[int, boolean], None, None).ty;
|
let v = env.unifier.get_fresh_var_with_range(&[int, boolean], None, None).ty;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
env.unify(int_list, v),
|
env.unify(int_list, v),
|
||||||
Err("Expected any one of these types: 0, 2, but got list[0]".to_string())
|
Err("Expected any one of these types: 0, 2, but got 11[0]".to_string())
|
||||||
);
|
);
|
||||||
|
|
||||||
// unification between v and float
|
// unification between v and float
|
||||||
|
@ -410,7 +448,11 @@ fn test_typevar_range() {
|
||||||
);
|
);
|
||||||
|
|
||||||
let v1 = env.unifier.get_fresh_var_with_range(&[int, boolean], None, None).ty;
|
let v1 = env.unifier.get_fresh_var_with_range(&[int, boolean], None, None).ty;
|
||||||
let v1_list = env.unifier.add_ty(TypeEnum::TList { ty: v1 });
|
let v1_list = env.unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id: env.type_mapping["list"].obj_id(&env.unifier).unwrap(),
|
||||||
|
fields: Mapping::default(),
|
||||||
|
params: into_var_map([TypeVar { id: list_elem_tvar.id, ty: v1 }]),
|
||||||
|
});
|
||||||
let v = env.unifier.get_fresh_var_with_range(&[int, v1_list], None, None).ty;
|
let v = env.unifier.get_fresh_var_with_range(&[int, v1_list], None, None).ty;
|
||||||
// unification between v and int
|
// unification between v and int
|
||||||
// where v in (int, list[v1]), v1 in (int, bool)
|
// where v in (int, list[v1]), v1 in (int, bool)
|
||||||
|
@ -424,9 +466,10 @@ fn test_typevar_range() {
|
||||||
let v = env.unifier.get_fresh_var_with_range(&[int, v1_list], None, None).ty;
|
let v = env.unifier.get_fresh_var_with_range(&[int, v1_list], None, None).ty;
|
||||||
// unification between v and list[float]
|
// unification between v and list[float]
|
||||||
// where v in (int, list[v1]), v1 in (int, bool)
|
// where v in (int, list[v1]), v1 in (int, bool)
|
||||||
|
println!("float_list: {}, v: {}", env.unifier.stringify(float_list), env.unifier.stringify(v));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
env.unify(float_list, v),
|
env.unify(float_list, v),
|
||||||
Err("Expected any one of these types: 0, list[typevar5], but got list[1]\n\nNotes:\n typevar5 ∈ {0, 2}".to_string())
|
Err("Expected any one of these types: 0, 11[typevar6], but got 11[1]\n\nNotes:\n typevar6 ∈ {0, 2}".to_string())
|
||||||
);
|
);
|
||||||
|
|
||||||
let a = env.unifier.get_fresh_var_with_range(&[int, float], None, None).ty;
|
let a = env.unifier.get_fresh_var_with_range(&[int, float], None, None).ty;
|
||||||
|
@ -441,34 +484,66 @@ fn test_typevar_range() {
|
||||||
|
|
||||||
let a = env.unifier.get_fresh_var_with_range(&[int, float], None, None).ty;
|
let a = env.unifier.get_fresh_var_with_range(&[int, float], None, None).ty;
|
||||||
let b = env.unifier.get_fresh_var_with_range(&[boolean, float], None, None).ty;
|
let b = env.unifier.get_fresh_var_with_range(&[boolean, float], None, None).ty;
|
||||||
let a_list = env.unifier.add_ty(TypeEnum::TList { ty: a });
|
let a_list = env.unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id: env.type_mapping["list"].obj_id(&env.unifier).unwrap(),
|
||||||
|
fields: Mapping::default(),
|
||||||
|
params: into_var_map([TypeVar { id: list_elem_tvar.id, ty: a }]),
|
||||||
|
});
|
||||||
let a_list = env.unifier.get_fresh_var_with_range(&[a_list], None, None).ty;
|
let a_list = env.unifier.get_fresh_var_with_range(&[a_list], None, None).ty;
|
||||||
let b_list = env.unifier.add_ty(TypeEnum::TList { ty: b });
|
let b_list = env.unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id: env.type_mapping["list"].obj_id(&env.unifier).unwrap(),
|
||||||
|
fields: Mapping::default(),
|
||||||
|
params: into_var_map([TypeVar { id: list_elem_tvar.id, ty: b }]),
|
||||||
|
});
|
||||||
let b_list = env.unifier.get_fresh_var_with_range(&[b_list], None, None).ty;
|
let b_list = env.unifier.get_fresh_var_with_range(&[b_list], None, None).ty;
|
||||||
env.unifier.unify(a_list, b_list).unwrap();
|
env.unifier.unify(a_list, b_list).unwrap();
|
||||||
let float_list = env.unifier.add_ty(TypeEnum::TList { ty: float });
|
let float_list = env.unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id: env.type_mapping["list"].obj_id(&env.unifier).unwrap(),
|
||||||
|
fields: Mapping::default(),
|
||||||
|
params: into_var_map([TypeVar { id: list_elem_tvar.id, ty: float }]),
|
||||||
|
});
|
||||||
env.unifier.unify(a_list, float_list).unwrap();
|
env.unifier.unify(a_list, float_list).unwrap();
|
||||||
// previous unifications should not affect a and b
|
// previous unifications should not affect a and b
|
||||||
env.unifier.unify(a, int).unwrap();
|
env.unifier.unify(a, int).unwrap();
|
||||||
|
|
||||||
let a = env.unifier.get_fresh_var_with_range(&[int, float], None, None).ty;
|
let a = env.unifier.get_fresh_var_with_range(&[int, float], None, None).ty;
|
||||||
let b = env.unifier.get_fresh_var_with_range(&[boolean, float], None, None).ty;
|
let b = env.unifier.get_fresh_var_with_range(&[boolean, float], None, None).ty;
|
||||||
let a_list = env.unifier.add_ty(TypeEnum::TList { ty: a });
|
let a_list = env.unifier.add_ty(TypeEnum::TObj {
|
||||||
let b_list = env.unifier.add_ty(TypeEnum::TList { ty: b });
|
obj_id: env.type_mapping["list"].obj_id(&env.unifier).unwrap(),
|
||||||
|
fields: Mapping::default(),
|
||||||
|
params: into_var_map([TypeVar { id: list_elem_tvar.id, ty: a }]),
|
||||||
|
});
|
||||||
|
let b_list = env.unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id: env.type_mapping["list"].obj_id(&env.unifier).unwrap(),
|
||||||
|
fields: Mapping::default(),
|
||||||
|
params: into_var_map([TypeVar { id: list_elem_tvar.id, ty: b }]),
|
||||||
|
});
|
||||||
env.unifier.unify(a_list, b_list).unwrap();
|
env.unifier.unify(a_list, b_list).unwrap();
|
||||||
let int_list = env.unifier.add_ty(TypeEnum::TList { ty: int });
|
let int_list = env.unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id: env.type_mapping["list"].obj_id(&env.unifier).unwrap(),
|
||||||
|
fields: Mapping::default(),
|
||||||
|
params: into_var_map([TypeVar { id: list_elem_tvar.id, ty: int }]),
|
||||||
|
});
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
env.unify(a_list, int_list),
|
env.unify(a_list, int_list),
|
||||||
Err("Incompatible types: list[typevar22] and list[0]\
|
Err("Incompatible types: 11[typevar23] and 11[0]\
|
||||||
\n\nNotes:\n typevar22 ∈ {1}"
|
\n\nNotes:\n typevar23 ∈ {1}"
|
||||||
.into())
|
.into())
|
||||||
);
|
);
|
||||||
|
|
||||||
let a = env.unifier.get_fresh_var_with_range(&[int, float], None, None).ty;
|
let a = env.unifier.get_fresh_var_with_range(&[int, float], None, None).ty;
|
||||||
let b = env.unifier.get_dummy_var().ty;
|
let b = env.unifier.get_dummy_var().ty;
|
||||||
let a_list = env.unifier.add_ty(TypeEnum::TList { ty: a });
|
let a_list = env.unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id: env.type_mapping["list"].obj_id(&env.unifier).unwrap(),
|
||||||
|
fields: Mapping::default(),
|
||||||
|
params: into_var_map([TypeVar { id: list_elem_tvar.id, ty: a }]),
|
||||||
|
});
|
||||||
let a_list = env.unifier.get_fresh_var_with_range(&[a_list], None, None).ty;
|
let a_list = env.unifier.get_fresh_var_with_range(&[a_list], None, None).ty;
|
||||||
let b_list = env.unifier.add_ty(TypeEnum::TList { ty: b });
|
let b_list = env.unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id: env.type_mapping["list"].obj_id(&env.unifier).unwrap(),
|
||||||
|
fields: Mapping::default(),
|
||||||
|
params: into_var_map([TypeVar { id: list_elem_tvar.id, ty: b }]),
|
||||||
|
});
|
||||||
env.unifier.unify(a_list, b_list).unwrap();
|
env.unifier.unify(a_list, b_list).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
env.unify(b, boolean),
|
env.unify(b, boolean),
|
||||||
|
@ -482,16 +557,25 @@ fn test_rigid_var() {
|
||||||
let a = env.unifier.get_fresh_rigid_var(None, None).ty;
|
let a = env.unifier.get_fresh_rigid_var(None, None).ty;
|
||||||
let b = env.unifier.get_fresh_rigid_var(None, None).ty;
|
let b = env.unifier.get_fresh_rigid_var(None, None).ty;
|
||||||
let x = env.unifier.get_dummy_var().ty;
|
let x = env.unifier.get_dummy_var().ty;
|
||||||
let list_a = env.unifier.add_ty(TypeEnum::TList { ty: a });
|
let list_elem_tvar = env.unifier.get_fresh_var(Some("list_elem".into()), None);
|
||||||
let list_x = env.unifier.add_ty(TypeEnum::TList { ty: x });
|
let list_a = env.unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id: env.type_mapping["list"].obj_id(&env.unifier).unwrap(),
|
||||||
|
fields: Mapping::default(),
|
||||||
|
params: into_var_map([TypeVar { id: list_elem_tvar.id, ty: a }]),
|
||||||
|
});
|
||||||
|
let list_x = env.unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id: env.type_mapping["list"].obj_id(&env.unifier).unwrap(),
|
||||||
|
fields: Mapping::default(),
|
||||||
|
params: into_var_map([TypeVar { id: list_elem_tvar.id, ty: x }]),
|
||||||
|
});
|
||||||
let int = env.parse("int", &HashMap::new());
|
let int = env.parse("int", &HashMap::new());
|
||||||
let list_int = env.parse("list[int]", &HashMap::new());
|
let list_int = env.parse("list[int]", &HashMap::new());
|
||||||
|
|
||||||
assert_eq!(env.unify(a, b), Err("Incompatible types: typevar3 and typevar2".to_string()));
|
assert_eq!(env.unify(a, b), Err("Incompatible types: typevar4 and typevar3".to_string()));
|
||||||
env.unifier.unify(list_a, list_x).unwrap();
|
env.unifier.unify(list_a, list_x).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
env.unify(list_x, list_int),
|
env.unify(list_x, list_int),
|
||||||
Err("Incompatible types: list[typevar2] and list[0]".to_string())
|
Err("Incompatible types: 11[typevar3] and 11[0]".to_string())
|
||||||
);
|
);
|
||||||
|
|
||||||
env.unifier.replace_rigid_var(a, int);
|
env.unifier.replace_rigid_var(a, int);
|
||||||
|
@ -506,10 +590,21 @@ fn test_instantiation() {
|
||||||
let float = env.parse("float", &HashMap::new());
|
let float = env.parse("float", &HashMap::new());
|
||||||
let list_int = env.parse("list[int]", &HashMap::new());
|
let list_int = env.parse("list[int]", &HashMap::new());
|
||||||
|
|
||||||
let obj_map: HashMap<_, _> = [(0usize, "int"), (1, "float"), (2, "bool")].into();
|
let list_elem_tvar = if let TypeEnum::TObj { params, .. } =
|
||||||
|
&*env.unifier.get_ty_immutable(env.type_mapping["list"])
|
||||||
|
{
|
||||||
|
iter_type_vars(params).next().unwrap()
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
|
let obj_map: HashMap<_, _> = [(0usize, "int"), (1, "float"), (2, "bool"), (11, "list")].into();
|
||||||
|
|
||||||
let v = env.unifier.get_fresh_var_with_range(&[int, boolean], None, None).ty;
|
let v = env.unifier.get_fresh_var_with_range(&[int, boolean], None, None).ty;
|
||||||
let list_v = env.unifier.add_ty(TypeEnum::TList { ty: v });
|
let list_v = env
|
||||||
|
.unifier
|
||||||
|
.subst(env.type_mapping["list"], &into_var_map([TypeVar { id: list_elem_tvar.id, ty: v }]))
|
||||||
|
.unwrap();
|
||||||
let v1 = env.unifier.get_fresh_var_with_range(&[list_v, int], None, None).ty;
|
let v1 = env.unifier.get_fresh_var_with_range(&[list_v, int], None, None).ty;
|
||||||
let v2 = env.unifier.get_fresh_var_with_range(&[list_int, float], None, None).ty;
|
let v2 = env.unifier.get_fresh_var_with_range(&[list_int, float], None, None).ty;
|
||||||
let t = env.unifier.get_dummy_var().ty;
|
let t = env.unifier.get_dummy_var().ty;
|
||||||
|
@ -536,7 +631,7 @@ fn test_instantiation() {
|
||||||
tuple[int, list[bool], list[int]]
|
tuple[int, list[bool], list[int]]
|
||||||
tuple[int, list[int], float]
|
tuple[int, list[int], float]
|
||||||
tuple[int, list[int], list[int]]
|
tuple[int, list[int], list[int]]
|
||||||
v5"
|
v6"
|
||||||
}
|
}
|
||||||
.split('\n')
|
.split('\n')
|
||||||
.collect_vec();
|
.collect_vec();
|
||||||
|
|
|
@ -44,6 +44,15 @@ void output_float64(double x) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void output_range(int32_t range[3]) {
|
||||||
|
printf("range(");
|
||||||
|
printf("%d, %d", range[0], range[1]);
|
||||||
|
if (range[2] != 1) {
|
||||||
|
printf(", %d", range[2]);
|
||||||
|
}
|
||||||
|
puts(")");
|
||||||
|
}
|
||||||
|
|
||||||
void output_asciiart(int32_t x) {
|
void output_asciiart(int32_t x) {
|
||||||
static const char *chars = " .,-:;i+hHM$*#@ ";
|
static const char *chars = " .,-:;i+hHM$*#@ ";
|
||||||
if (x < 0) {
|
if (x < 0) {
|
||||||
|
@ -79,6 +88,10 @@ void output_str(struct cslice *slice) {
|
||||||
for (usize i = 0; i < slice->len; ++i) {
|
for (usize i = 0; i < slice->len; ++i) {
|
||||||
putchar(data[i]);
|
putchar(data[i]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void output_strln(struct cslice *slice) {
|
||||||
|
output_str(slice);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -107,6 +107,9 @@ def patch(module):
|
||||||
def output_float(x):
|
def output_float(x):
|
||||||
print("%f" % x)
|
print("%f" % x)
|
||||||
|
|
||||||
|
def output_strln(x):
|
||||||
|
print(x, end='')
|
||||||
|
|
||||||
def dbg_stack_address(_):
|
def dbg_stack_address(_):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
@ -120,6 +123,8 @@ def patch(module):
|
||||||
return output_asciiart
|
return output_asciiart
|
||||||
elif name == "output_float64":
|
elif name == "output_float64":
|
||||||
return output_float
|
return output_float
|
||||||
|
elif name == "output_str":
|
||||||
|
return output_strln
|
||||||
elif name in {
|
elif name in {
|
||||||
"output_bool",
|
"output_bool",
|
||||||
"output_int32",
|
"output_int32",
|
||||||
|
@ -127,7 +132,8 @@ def patch(module):
|
||||||
"output_int32_list",
|
"output_int32_list",
|
||||||
"output_uint32",
|
"output_uint32",
|
||||||
"output_uint64",
|
"output_uint64",
|
||||||
"output_str",
|
"output_strln",
|
||||||
|
"output_range",
|
||||||
}:
|
}:
|
||||||
return print
|
return print
|
||||||
elif name == "dbg_stack_address":
|
elif name == "dbg_stack_address":
|
||||||
|
|
|
@ -7,7 +7,7 @@ def output_int64(x: int64):
|
||||||
...
|
...
|
||||||
|
|
||||||
@extern
|
@extern
|
||||||
def output_str(x: str):
|
def output_strln(x: str):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ class A:
|
||||||
|
|
||||||
class Initless:
|
class Initless:
|
||||||
def foo(self):
|
def foo(self):
|
||||||
output_str("hello")
|
output_strln("hello")
|
||||||
|
|
||||||
def run() -> int32:
|
def run() -> int32:
|
||||||
a = A(10)
|
a = A(10)
|
||||||
|
|
|
@ -22,6 +22,10 @@ def output_uint64(x: uint64):
|
||||||
def output_float64(x: float):
|
def output_float64(x: float):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
@extern
|
||||||
|
def output_range(x: range):
|
||||||
|
...
|
||||||
|
|
||||||
@extern
|
@extern
|
||||||
def output_int32_list(x: list[int32]):
|
def output_int32_list(x: list[int32]):
|
||||||
...
|
...
|
||||||
|
@ -34,6 +38,10 @@ def output_asciiart(x: int32):
|
||||||
def output_str(x: str):
|
def output_str(x: str):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
@extern
|
||||||
|
def output_strln(x: str):
|
||||||
|
...
|
||||||
|
|
||||||
def test_output_bool():
|
def test_output_bool():
|
||||||
output_bool(True)
|
output_bool(True)
|
||||||
output_bool(False)
|
output_bool(False)
|
||||||
|
@ -59,6 +67,15 @@ def test_output_float64():
|
||||||
output_float64(16.25)
|
output_float64(16.25)
|
||||||
output_float64(-16.25)
|
output_float64(-16.25)
|
||||||
|
|
||||||
|
def test_output_range():
|
||||||
|
r = range(1, 100, 5)
|
||||||
|
output_int32(r.start)
|
||||||
|
output_int32(r.stop)
|
||||||
|
output_int32(r.step)
|
||||||
|
output_range(range(10))
|
||||||
|
output_range(range(1, 10))
|
||||||
|
output_range(range(1, 10, 2))
|
||||||
|
|
||||||
def test_output_asciiart():
|
def test_output_asciiart():
|
||||||
for i in range(17):
|
for i in range(17):
|
||||||
output_asciiart(i)
|
output_asciiart(i)
|
||||||
|
@ -68,7 +85,8 @@ def test_output_int32_list():
|
||||||
output_int32_list([0, 1, 3, 5, 10])
|
output_int32_list([0, 1, 3, 5, 10])
|
||||||
|
|
||||||
def test_output_str_family():
|
def test_output_str_family():
|
||||||
output_str("hello world")
|
output_str("hello")
|
||||||
|
output_strln(" world")
|
||||||
|
|
||||||
def run() -> int32:
|
def run() -> int32:
|
||||||
test_output_bool()
|
test_output_bool()
|
||||||
|
@ -77,6 +95,7 @@ def run() -> int32:
|
||||||
test_output_uint32()
|
test_output_uint32()
|
||||||
test_output_uint64()
|
test_output_uint64()
|
||||||
test_output_float64()
|
test_output_float64()
|
||||||
|
test_output_range()
|
||||||
test_output_asciiart()
|
test_output_asciiart()
|
||||||
test_output_int32_list()
|
test_output_int32_list()
|
||||||
test_output_str_family()
|
test_output_str_family()
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
@extern
|
||||||
|
def output_bool(x: bool):
|
||||||
|
...
|
||||||
|
|
||||||
@extern
|
@extern
|
||||||
def output_int32_list(x: list[int32]):
|
def output_int32_list(x: list[int32]):
|
||||||
...
|
...
|
||||||
|
@ -30,6 +34,32 @@ def run() -> int32:
|
||||||
|
|
||||||
get_list_slice()
|
get_list_slice()
|
||||||
list_slice_assignment()
|
list_slice_assignment()
|
||||||
|
|
||||||
|
output_int32_list([1, 2, 3] + [4, 5, 6])
|
||||||
|
output_int32_list([1, 2, 3] * 3)
|
||||||
|
output_bool([] == [])
|
||||||
|
output_bool([0] == [])
|
||||||
|
output_bool([0] == [0])
|
||||||
|
output_bool([0, 1] == [0])
|
||||||
|
output_bool([0, 1] == [0, 1])
|
||||||
|
output_bool([] != [])
|
||||||
|
output_bool([0] != [])
|
||||||
|
output_bool([0] != [0])
|
||||||
|
output_bool([0] != [0, 1])
|
||||||
|
output_bool([0, 1] != [0, 1])
|
||||||
|
output_bool([] == [] == [])
|
||||||
|
output_bool([0] == [0] == [0])
|
||||||
|
output_bool([0, 1] == [0] == [0, 1])
|
||||||
|
output_bool([0, 1] == [0, 1] == [0])
|
||||||
|
output_bool([0] == [0, 1] == [0, 1])
|
||||||
|
output_bool([0, 1] == [0, 1] == [0, 1])
|
||||||
|
output_bool([] != [] != [])
|
||||||
|
output_bool([0] != [0] != [0])
|
||||||
|
output_bool([0, 1] != [0] != [0, 1])
|
||||||
|
output_bool([0, 1] != [0, 1] != [0])
|
||||||
|
output_bool([0] != [0, 1] != [0, 1])
|
||||||
|
output_bool([0, 1] != [0, 1] != [0, 1])
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def get_list_slice():
|
def get_list_slice():
|
||||||
|
|
|
@ -23,11 +23,12 @@ def run() -> int32:
|
||||||
output_int32(x)
|
output_int32(x)
|
||||||
output_str(" * ")
|
output_str(" * ")
|
||||||
output_float64(n / x)
|
output_float64(n / x)
|
||||||
|
output_str("\n")
|
||||||
except: # Assume this is intended to catch x == 0
|
except: # Assume this is intended to catch x == 0
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
# loop fell through without finding a factor
|
# loop fell through without finding a factor
|
||||||
output_int32(n)
|
output_int32(n)
|
||||||
output_str(" is a prime number")
|
output_str(" is a prime number\n")
|
||||||
|
|
||||||
return 0
|
return 0
|
|
@ -37,7 +37,7 @@ def test_round64():
|
||||||
output_int64(round64(x))
|
output_int64(round64(x))
|
||||||
|
|
||||||
def test_np_round():
|
def test_np_round():
|
||||||
for x in [-1.5, -0.5, 0.5, 1.5, dbl_inf(), -dbl_inf(), dbl_nan()]:
|
for x in [-1.5, -0.5, 0.5, 1.5, dbl_inf(), -dbl_inf(), dbl_nan(), 0.0, -0.0, 1.6, 1.4, -1.4, -1.6]:
|
||||||
output_float64(np_round(x))
|
output_float64(np_round(x))
|
||||||
|
|
||||||
def test_np_isnan():
|
def test_np_isnan():
|
||||||
|
|
|
@ -81,6 +81,7 @@ in rec {
|
||||||
''
|
''
|
||||||
mkdir -p $out/bin
|
mkdir -p $out/bin
|
||||||
ln -s ${llvm-nac3}/bin/clang.exe $out/bin/clang-irrt.exe
|
ln -s ${llvm-nac3}/bin/clang.exe $out/bin/clang-irrt.exe
|
||||||
|
ln -s ${llvm-nac3}/bin/clang.exe $out/bin/clang-irrt-test.exe
|
||||||
ln -s ${llvm-nac3}/bin/llvm-as.exe $out/bin/llvm-as-irrt.exe
|
ln -s ${llvm-nac3}/bin/llvm-as.exe $out/bin/llvm-as-irrt.exe
|
||||||
'';
|
'';
|
||||||
nac3artiq = pkgs.rustPlatform.buildRustPackage {
|
nac3artiq = pkgs.rustPlatform.buildRustPackage {
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
{ pkgs } : [
|
{ pkgs } : [
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-libunwind-18.1.2-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-libunwind-18.1.8-1-any.pkg.tar.zst";
|
||||||
sha256 = "0ksz7xz1lbwsmdr9sa1444k0dlfkbd8k11pq7w08ir7r1wjy6fid";
|
sha256 = "1v8zkfcbf1ga2ndpd1j0dwv5s1rassxs2b5pjhcsmqwjcvczba1m";
|
||||||
name = "mingw-w64-clang-x86_64-libunwind-18.1.2-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-libunwind-18.1.8-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-libc++-18.1.2-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-libc++-18.1.8-1-any.pkg.tar.zst";
|
||||||
sha256 = "0r8skyjqv4cpkqif0niakx4hdpkscil1zf6mzj34pqna0j5gdnq2";
|
sha256 = "0mfd8wrmgx12j5gf354j7pk1l3lg9ykxvq75xdk3jipsr6hbn846";
|
||||||
name = "mingw-w64-clang-x86_64-libc++-18.1.2-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-libc++-18.1.8-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
|
@ -31,9 +31,9 @@
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-xz-5.6.1-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-xz-5.6.2-2-any.pkg.tar.zst";
|
||||||
sha256 = "14p4xxaxjjy6j1ingji82xhai1mc1gls5ali6z40fbb2ylxkaggs";
|
sha256 = "0phb9hwqksk1rg29yhwlc7si78zav19c2kac0i841pc7mc2n9gzx";
|
||||||
name = "mingw-w64-clang-x86_64-xz-5.6.1-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-xz-5.6.2-2-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
|
@ -43,81 +43,81 @@
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-libxml2-2.12.6-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-libxml2-2.12.8-1-any.pkg.tar.zst";
|
||||||
sha256 = "177b3rmsknqq6hf0zqwva71s3avh20ca7vzznp2ls2z5qm8vhhlp";
|
sha256 = "1imipb0dz4w6x4n9arn22imyzzcwdlf2cqxvn7irqq7w9by6fy0b";
|
||||||
name = "mingw-w64-clang-x86_64-libxml2-2.12.6-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-libxml2-2.12.8-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-zstd-1.5.5-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-zstd-1.5.6-2-any.pkg.tar.zst";
|
||||||
sha256 = "07739wmwgxf0d6db4p8w302a6jwcm01aafr1s8jvcl5k1h5a1m2m";
|
sha256 = "02cp5ci8w50k7xn38mpkwnr8sn898v18wcc07y8f9sfla7vcyfix";
|
||||||
name = "mingw-w64-clang-x86_64-zstd-1.5.5-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-zstd-1.5.6-2-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-llvm-libs-18.1.2-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-llvm-libs-18.1.8-1-any.pkg.tar.zst";
|
||||||
sha256 = "0ibiy01v16naik9pj32ch7a9pkbw4yrn3gyq7p0y6kcc63fkjazy";
|
sha256 = "0rpbgvvinsqflhd3nhfxk0g0yy8j80zzw5yx6573ak0m78a9fa06";
|
||||||
name = "mingw-w64-clang-x86_64-llvm-libs-18.1.2-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-llvm-libs-18.1.8-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-llvm-18.1.2-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-llvm-18.1.8-1-any.pkg.tar.zst";
|
||||||
sha256 = "1hcfz6nb6svmmcqzfrdi96az2x7mzj0cispdv2ssbgn7nkf19pi0";
|
sha256 = "185g5h8q3x3rav9lp2njln58ny2idh2067fd02j3nsbik6glshpf";
|
||||||
name = "mingw-w64-clang-x86_64-llvm-18.1.2-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-llvm-18.1.8-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-clang-libs-18.1.2-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-clang-libs-18.1.8-1-any.pkg.tar.zst";
|
||||||
sha256 = "1k17d18g7rmq2ph4kq1mf84vs8133jzf52nkv6syh39ypjga67wa";
|
sha256 = "089hji3yd7wsd03v9mdfgc99l5k1dql8kg7p3hy13vrbgfsabxhc";
|
||||||
name = "mingw-w64-clang-x86_64-clang-libs-18.1.2-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-clang-libs-18.1.8-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-compiler-rt-18.1.2-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-compiler-rt-18.1.8-1-any.pkg.tar.zst";
|
||||||
sha256 = "1w2j0vs888haz9shjr1l8dc4j957sk1p0377zzipkbqnzqwjf1z8";
|
sha256 = "1dwcxnv1k5ljim5ys4h1c3jlrdpi0054z094ynav7if65i8zjj4a";
|
||||||
name = "mingw-w64-clang-x86_64-compiler-rt-18.1.2-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-compiler-rt-18.1.8-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-headers-git-11.0.0.r655.gdbfdf8025-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-headers-git-12.0.0.r81.g90abf784a-1-any.pkg.tar.zst";
|
||||||
sha256 = "18csfwlk2h9pr4411crx1b41qjzn5jgbssm3h109nzwbdizkp62h";
|
sha256 = "1h3cdcajz29iq7vja908kkijz1vb9xn0f7w1lw1ima0q0zhinv4q";
|
||||||
name = "mingw-w64-clang-x86_64-headers-git-11.0.0.r655.gdbfdf8025-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-headers-git-12.0.0.r81.g90abf784a-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-crt-git-11.0.0.r655.gdbfdf8025-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-crt-git-12.0.0.r81.g90abf784a-1-any.pkg.tar.zst";
|
||||||
sha256 = "03l1zkrxgxxssp430xcv2gch1d03rbnbk1c0vgiqxigcs8lljh2g";
|
sha256 = "15kamyi3b0j6f5zxin4i2jgzjc7lzvwl4z5cz3dx0i8hg91aq0n7";
|
||||||
name = "mingw-w64-clang-x86_64-crt-git-11.0.0.r655.gdbfdf8025-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-crt-git-12.0.0.r81.g90abf784a-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-lld-18.1.2-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-lld-18.1.8-1-any.pkg.tar.zst";
|
||||||
sha256 = "1ai4gl7ybpk9n10jmbpf3zzfa893m1krj5qhf44ajln0jabdfnbn";
|
sha256 = "1vpij5d06m4kjy3qv8bizwlkl21gcv6fv0r2f1j9bclgm6k3144x";
|
||||||
name = "mingw-w64-clang-x86_64-lld-18.1.2-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-lld-18.1.8-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-libwinpthread-git-11.0.0.r655.gdbfdf8025-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-libwinpthread-git-12.0.0.r81.g90abf784a-1-any.pkg.tar.zst";
|
||||||
sha256 = "1svhjzwhvl4ldl439jhgfy47g05y2af1cjqvydgijn1dd4g8y8vq";
|
sha256 = "0qdvgs1rmjjhn9klf9kpw7l0ydz36rr5fasn4q9gpby2lgl11bkb";
|
||||||
name = "mingw-w64-clang-x86_64-libwinpthread-git-11.0.0.r655.gdbfdf8025-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-libwinpthread-git-12.0.0.r81.g90abf784a-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-winpthreads-git-11.0.0.r655.gdbfdf8025-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-winpthreads-git-12.0.0.r81.g90abf784a-1-any.pkg.tar.zst";
|
||||||
sha256 = "0jxdhkl256vnr13xf1x3fyjrdf764zg70xcs3gki3rg109f0a6xk";
|
sha256 = "0rh2mn078cifcmr4as4k57jxjln5lbnsmpx47h9d0s5d2i8sf2rc";
|
||||||
name = "mingw-w64-clang-x86_64-winpthreads-git-11.0.0.r655.gdbfdf8025-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-winpthreads-git-12.0.0.r81.g90abf784a-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-clang-18.1.2-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-clang-18.1.8-1-any.pkg.tar.zst";
|
||||||
sha256 = "0ahfic7vdfv96k5v7fdkgk1agk28l833xjn2igrmbvqg96ak0w6n";
|
sha256 = "1qny934nv4g75k9gb5sf31v24bgafkg6qw7r35xv3in491w6annq";
|
||||||
name = "mingw-w64-clang-x86_64-clang-18.1.2-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-clang-18.1.8-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-c-ares-1.27.0-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-c-ares-1.29.0-1-any.pkg.tar.zst";
|
||||||
sha256 = "06y3sgqv6a0gr3dsbzs36jrj8adklssgjqi2ms5clsyq6ay4f91r";
|
sha256 = "01xg1h1a8kda0kq2921w25ybvm1ms7lfdzday0hv93f3myq7briq";
|
||||||
name = "mingw-w64-clang-x86_64-c-ares-1.27.0-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-c-ares-1.29.0-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
|
@ -127,9 +127,9 @@
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-libunistring-1.1-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-libunistring-1.2-1-any.pkg.tar.zst";
|
||||||
sha256 = "16myvbg33q5s7jl30w5qd8n8f1r05335ms8r61234vn52n32l2c4";
|
sha256 = "13nz49li39z1zgfx1q9jg4vrmyrmqb6qdq0nqshidaqc6zr16k3g";
|
||||||
name = "mingw-w64-clang-x86_64-libunistring-1.1-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-libunistring-1.2-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
|
@ -151,9 +151,9 @@
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-p11-kit-0.25.3-2-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-p11-kit-0.25.5-1-any.pkg.tar.zst";
|
||||||
sha256 = "1jrwkc4lvw5hm5rqmi5gqh7mfkbqfa5gi81zjij0krnl0gaxw3c8";
|
sha256 = "00yz6cmr1ldlrskv811n345xcia88mj7w4fyx4m9z5848jxgsabd";
|
||||||
name = "mingw-w64-clang-x86_64-p11-kit-0.25.3-2-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-p11-kit-0.25.5-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
|
@ -163,9 +163,9 @@
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-openssl-3.2.1-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-openssl-3.3.1-1-any.pkg.tar.zst";
|
||||||
sha256 = "0ix2r4ll09m2z5vz2k94gmwfs0pp3ipvjdimwzx7v6xhcs2l25lz";
|
sha256 = "0ywhwm4kw3qjzv0872qwabnsq2rzbmqjb9m69q3fykjl0m9gigsa";
|
||||||
name = "mingw-w64-clang-x86_64-openssl-3.2.1-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-openssl-3.3.1-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
|
@ -175,27 +175,33 @@
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-nghttp2-1.60.0-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-nghttp2-1.61.0-2-any.pkg.tar.zst";
|
||||||
sha256 = "0wxw8266hf4qd2m4zpgb1wvlrnaksmcrs0kh5y9zpf2y5sy8f2bq";
|
sha256 = "07bkk98126gy4k6lb9rrqqnzjfz9j2rsr5dzr2djmzdkw0h4dr95";
|
||||||
name = "mingw-w64-clang-x86_64-nghttp2-1.60.0-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-nghttp2-1.61.0-2-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-curl-8.6.0-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-nghttp3-1.4.0-1-any.pkg.tar.zst";
|
||||||
sha256 = "1racc7cyzj22kink9w8m8jv73ji5hfg6r6d1ka9dqmvcbx04r8p0";
|
sha256 = "007w2252nzn274j4wjc1vf56xyzzh5vg3blj1hil7mlmffgvc923";
|
||||||
name = "mingw-w64-clang-x86_64-curl-8.6.0-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-nghttp3-1.4.0-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-rust-1.76.0-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-curl-8.8.0-10-any.pkg.tar.zst";
|
||||||
sha256 = "0ny3bvwvn5wmqrxzhdfw34akr0kj0m7rg9lg3w5yibqz2mkqhk11";
|
sha256 = "024z5b1achkf448gxqy1i3gcw371x54kfl6igv08b5wb3rrw35a4";
|
||||||
name = "mingw-w64-clang-x86_64-rust-1.76.0-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-curl-8.8.0-10-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-pkgconf-1~2.1.1-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-rust-1.79.0-1-any.pkg.tar.zst";
|
||||||
sha256 = "00kxqg9ds4q74lxrzjh8z0858smqbi1j9r06s0zjadsql0ln98cq";
|
sha256 = "0i7s88hj8m4920xifkj7i4b4sq8cqq7p5cypp3jqx3dc44pwm19a";
|
||||||
name = "mingw-w64-clang-x86_64-pkgconf-12.1.1-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-rust-1.79.0-1-any.pkg.tar.zst";
|
||||||
|
})
|
||||||
|
|
||||||
|
(pkgs.fetchurl {
|
||||||
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-pkgconf-1~2.2.0-1-any.pkg.tar.zst";
|
||||||
|
sha256 = "1y44ijg3y8p80f1yn9972nshrnyrd06a9sh984ajhxg8bi8s5xyl";
|
||||||
|
name = "mingw-w64-clang-x86_64-pkgconf-12.2.0-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
|
@ -241,9 +247,9 @@
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-libarchive-3.7.2-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-libarchive-3.7.4-1-any.pkg.tar.zst";
|
||||||
sha256 = "1p84yh6yzkdpmr02vyvgz16x5gycckah25jkdc2py09l7iw96bmw";
|
sha256 = "1ykw6imllgxv6lsgwxx1miqjr4l1iryqkrj286jcbfrb8ghpzhv5";
|
||||||
name = "mingw-w64-clang-x86_64-libarchive-3.7.2-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-libarchive-3.7.4-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
|
@ -253,9 +259,9 @@
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-ninja-1.11.1-3-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-ninja-1.12.1-1-any.pkg.tar.zst";
|
||||||
sha256 = "13wjfmyfr952n3ydpldjlwx1nla5xpyvr96ng8pfbyw4z900v5ms";
|
sha256 = "1vj9qaa43v316daz8k4ricmz3f33nhjpj7r0vn979nwmy7hzs7jx";
|
||||||
name = "mingw-w64-clang-x86_64-ninja-1.11.1-3-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-ninja-1.12.1-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
|
@ -265,9 +271,9 @@
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-cmake-3.29.0-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-cmake-3.30.0-1-any.pkg.tar.zst";
|
||||||
sha256 = "0l79lf6zihn0k8hz93qnjnq259y45yq19235g9c444jc2w093si1";
|
sha256 = "07b7132hwhiqrf0l2lgw3g4zw9i2lln3kqc9kg2qijvkapbkmwqb";
|
||||||
name = "mingw-w64-clang-x86_64-cmake-3.29.0-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-cmake-3.30.0-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
|
@ -301,15 +307,15 @@
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-sqlite3-3.45.2-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-sqlite3-3.46.0-1-any.pkg.tar.zst";
|
||||||
sha256 = "1icvw3f08cgi94p0177i46v72wgpsxw95p6kd0sm2w3vj0qlqbcw";
|
sha256 = "0q676i2z5nr4c71jnd4z5qz9xa1xryl0cpi84w74yvd0p4qiz7y2";
|
||||||
name = "mingw-w64-clang-x86_64-sqlite3-3.45.2-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-sqlite3-3.46.0-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-tk-8.6.12-2-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-tk-8.6.13-1-any.pkg.tar.zst";
|
||||||
sha256 = "0pi74q91vl6vw8vvmmwnvrgai3b1aanp0zhca5qsmv8ljh2wdgzx";
|
sha256 = "12f6lqx1sglczcnz2ns6sxw9cxwm1klxajqzcrbnfwln1nllz2nd";
|
||||||
name = "mingw-w64-clang-x86_64-tk-8.6.12-2-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-tk-8.6.13-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
|
@ -319,21 +325,21 @@
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-python-3.11.8-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-python-3.11.9-1-any.pkg.tar.zst";
|
||||||
sha256 = "0djpf4k8s25nys6nrm2x2v134lcgzhhbjs37ihkg0b3sxmmc3b0p";
|
sha256 = "0ah1idjqxg7jc07a1gz9z766rjjd0f0c6ri4hpcsimsrbj1zjd3c";
|
||||||
name = "mingw-w64-clang-x86_64-python-3.11.8-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-python-3.11.9-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-openmp-18.1.2-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-llvm-openmp-18.1.8-1-any.pkg.tar.zst";
|
||||||
sha256 = "1v9wm3ja3a7a7yna2bpqky481qf244wc98kfdl7l03k7rkvvydpl";
|
sha256 = "0cy2v0l4af24j34mzj5q5nlzcqhackfajlfj1rpf6mb3rbz23qw9";
|
||||||
name = "mingw-w64-clang-x86_64-openmp-18.1.2-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-llvm-openmp-18.1.8-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-openblas-0.3.26-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-openblas-0.3.27-1-any.pkg.tar.zst";
|
||||||
sha256 = "0kdr72y5lc9dl9s1bjrw8g21qmv2iwd1xvn1r21170i277wsmqiv";
|
sha256 = "06ygz1wa488wqvmxbn74b0fyan4wf3lb6kbwfampgikd1gijww2k";
|
||||||
name = "mingw-w64-clang-x86_64-openblas-0.3.26-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-openblas-0.3.27-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
|
@ -343,8 +349,8 @@
|
||||||
})
|
})
|
||||||
|
|
||||||
(pkgs.fetchurl {
|
(pkgs.fetchurl {
|
||||||
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-python-setuptools-69.1.1-1-any.pkg.tar.zst";
|
url = "https://mirror.msys2.org/mingw/clang64/mingw-w64-clang-x86_64-python-setuptools-70.2.0-1-any.pkg.tar.zst";
|
||||||
sha256 = "1mc56anasj0v92nlg84m3pa7dbqgjakxw0b4ibqlrr9cq0xzsg4b";
|
sha256 = "1q4r9bg2hn3jmshvq81xm5zvy9wn35yf0z2ayksrkwph1zzdkvkm";
|
||||||
name = "mingw-w64-clang-x86_64-python-setuptools-69.1.1-1-any.pkg.tar.zst";
|
name = "mingw-w64-clang-x86_64-python-setuptools-70.2.0-1-any.pkg.tar.zst";
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue