Compare commits
16 Commits
fa04cfcdc8
...
b2994ff90a
Author | SHA1 | Date |
---|---|---|
David Mak | b2994ff90a | |
David Mak | 2d1f243975 | |
David Mak | 4f236ea411 | |
David Mak | e3fe3f03fb | |
David Mak | deb325de4f | |
David Mak | aa84cc425f | |
David Mak | f7fbc629aa | |
David Mak | 724651d2bb | |
David Mak | 2665668e21 | |
David Mak | 9b1c559efb | |
David Mak | 5ecc2a905e | |
David Mak | 2a8a5bbfea | |
David Mak | 3ed8ce7215 | |
David Mak | c8866b1534 | |
David Mak | 84a888758a | |
David Mak | 9d550725b7 |
|
@ -4,9 +4,9 @@ version = 3
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ahash"
|
name = "ahash"
|
||||||
version = "0.8.9"
|
version = "0.8.11"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d713b3834d76b85304d4d525563c1276e2e30dc97cc67bfb4585a4a29fc2c89f"
|
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"getrandom",
|
"getrandom",
|
||||||
|
@ -17,18 +17,18 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aho-corasick"
|
name = "aho-corasick"
|
||||||
version = "1.1.2"
|
version = "1.1.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
|
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"memchr",
|
"memchr",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anstream"
|
name = "anstream"
|
||||||
version = "0.6.12"
|
version = "0.6.13"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "96b09b5178381e0874812a9b157f7fe84982617e48f71f4e3235482775e5b540"
|
checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anstyle",
|
"anstyle",
|
||||||
"anstyle-parse",
|
"anstyle-parse",
|
||||||
|
@ -59,7 +59,7 @@ version = "1.0.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
|
checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-sys 0.52.0",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -69,7 +69,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
|
checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anstyle",
|
"anstyle",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -110,9 +110,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "2.4.2"
|
version = "2.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
|
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "byteorder"
|
name = "byteorder"
|
||||||
|
@ -122,9 +122,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cc"
|
name = "cc"
|
||||||
version = "1.0.86"
|
version = "1.0.90"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7f9fa1897e4325be0d68d48df6aa1a71ac2ed4d27723887e7754192705350730"
|
checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
|
@ -134,9 +134,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap"
|
name = "clap"
|
||||||
version = "4.5.1"
|
version = "4.5.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c918d541ef2913577a0f9566e9ce27cb35b6df072075769e0b26cb5a554520da"
|
checksum = "949626d00e063efc93b6dca932419ceb5432f99769911c0b995f7e884c778813"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap_builder",
|
"clap_builder",
|
||||||
"clap_derive",
|
"clap_derive",
|
||||||
|
@ -144,9 +144,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap_builder"
|
name = "clap_builder"
|
||||||
version = "4.5.1"
|
version = "4.5.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9f3e7391dad68afb0c2ede1bf619f579a3dc9c2ec67f089baa397123a2f3d1eb"
|
checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anstream",
|
"anstream",
|
||||||
"anstyle",
|
"anstyle",
|
||||||
|
@ -156,14 +156,14 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap_derive"
|
name = "clap_derive"
|
||||||
version = "4.5.0"
|
version = "4.5.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "307bc0538d5f0f83b8248db3087aa92fe504e4691294d0c96c0eabc33f47ba47"
|
checksum = "90239a040c80f5e14809ca132ddc4176ab33d5e17e49691793296e3fcb34d72f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"heck",
|
"heck 0.5.0",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.50",
|
"syn 2.0.53",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -187,7 +187,7 @@ dependencies = [
|
||||||
"encode_unicode",
|
"encode_unicode",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -205,9 +205,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crossbeam-channel"
|
name = "crossbeam-channel"
|
||||||
version = "0.5.11"
|
version = "0.5.12"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "176dc175b78f56c0f321911d9c8eb2b77a78a4860b9c19db83835fea1a46649b"
|
checksum = "ab3db02a9c5b5121e1e42fbdb1aeb65f5e02624cc58c43f2884c6ccac0b82f95"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"crossbeam-utils",
|
"crossbeam-utils",
|
||||||
]
|
]
|
||||||
|
@ -252,12 +252,6 @@ version = "0.2.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
|
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "diff"
|
|
||||||
version = "0.1.13"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dirs-next"
|
name = "dirs-next"
|
||||||
version = "2.0.0"
|
version = "2.0.0"
|
||||||
|
@ -313,7 +307,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245"
|
checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -379,10 +373,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
|
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hermit-abi"
|
name = "heck"
|
||||||
version = "0.3.6"
|
version = "0.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bd5256b483761cd23699d0da46cc6fd2ee3be420bbe6d020ae4a091e70b7e9fd"
|
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "indexmap"
|
name = "indexmap"
|
||||||
|
@ -396,9 +390,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "indexmap"
|
name = "indexmap"
|
||||||
version = "2.2.3"
|
version = "2.2.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "233cf39063f058ea2caae4091bf4a3ef70a653afbc026f5c4a4135d114e3c177"
|
checksum = "7b0b929d511467233429c45a44ac1dcaa21ba0f5ba11e4879e6ed28ddb4f9df4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"equivalent",
|
"equivalent",
|
||||||
"hashbrown 0.14.3",
|
"hashbrown 0.14.3",
|
||||||
|
@ -432,7 +426,7 @@ checksum = "4fa4d8d74483041a882adaa9a29f633253a66dde85055f0495c121620ac484b2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.50",
|
"syn 2.0.53",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -449,22 +443,11 @@ dependencies = [
|
||||||
"similar",
|
"similar",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "is-terminal"
|
|
||||||
version = "0.4.12"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b"
|
|
||||||
dependencies = [
|
|
||||||
"hermit-abi",
|
|
||||||
"libc",
|
|
||||||
"windows-sys 0.52.0",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itertools"
|
name = "itertools"
|
||||||
version = "0.10.5"
|
version = "0.11.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
|
checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"either",
|
"either",
|
||||||
]
|
]
|
||||||
|
@ -486,34 +469,33 @@ checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lalrpop"
|
name = "lalrpop"
|
||||||
version = "0.20.0"
|
version = "0.20.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "da4081d44f4611b66c6dd725e6de3169f9f63905421e8626fcb86b6a898998b8"
|
checksum = "55cb077ad656299f160924eb2912aa147d7339ea7d69e1b5517326fdcec3c1ca"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ascii-canvas",
|
"ascii-canvas",
|
||||||
"bit-set",
|
"bit-set",
|
||||||
"diff",
|
|
||||||
"ena",
|
"ena",
|
||||||
"is-terminal",
|
"itertools 0.11.0",
|
||||||
"itertools 0.10.5",
|
|
||||||
"lalrpop-util",
|
"lalrpop-util",
|
||||||
"petgraph",
|
"petgraph",
|
||||||
"pico-args",
|
"pico-args",
|
||||||
"regex",
|
"regex",
|
||||||
"regex-syntax 0.7.5",
|
"regex-syntax",
|
||||||
"string_cache",
|
"string_cache",
|
||||||
"term",
|
"term",
|
||||||
"tiny-keccak",
|
"tiny-keccak",
|
||||||
"unicode-xid",
|
"unicode-xid",
|
||||||
|
"walkdir",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lalrpop-util"
|
name = "lalrpop-util"
|
||||||
version = "0.20.0"
|
version = "0.20.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3f35c735096c0293d313e8f2a641627472b83d01b937177fe76e5e2708d31e0d"
|
checksum = "507460a910eb7b32ee961886ff48539633b788a36b65692b95f225b844c82553"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"regex",
|
"regex-automata",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -530,12 +512,12 @@ checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libloading"
|
name = "libloading"
|
||||||
version = "0.8.1"
|
version = "0.8.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c571b676ddfc9a8c12f1f3d3085a7b163966a8fd8098a90640953ce5f6170161"
|
checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"windows-sys 0.48.0",
|
"windows-targets 0.52.4",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -544,7 +526,7 @@ version = "0.0.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8"
|
checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.4.2",
|
"bitflags 2.5.0",
|
||||||
"libc",
|
"libc",
|
||||||
"redox_syscall",
|
"redox_syscall",
|
||||||
]
|
]
|
||||||
|
@ -586,9 +568,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "log"
|
name = "log"
|
||||||
version = "0.4.20"
|
version = "0.4.21"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
|
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
|
@ -634,6 +616,7 @@ name = "nac3core"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"crossbeam",
|
"crossbeam",
|
||||||
|
"indexmap 2.2.5",
|
||||||
"indoc",
|
"indoc",
|
||||||
"inkwell",
|
"inkwell",
|
||||||
"insta",
|
"insta",
|
||||||
|
@ -681,9 +664,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "new_debug_unreachable"
|
name = "new_debug_unreachable"
|
||||||
version = "1.0.4"
|
version = "1.0.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
|
checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell"
|
name = "once_cell"
|
||||||
|
@ -721,7 +704,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9"
|
checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"fixedbitset",
|
"fixedbitset",
|
||||||
"indexmap 2.2.3",
|
"indexmap 2.2.5",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -764,7 +747,7 @@ dependencies = [
|
||||||
"phf_shared 0.11.2",
|
"phf_shared 0.11.2",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.50",
|
"syn 2.0.53",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -791,6 +774,12 @@ version = "0.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
|
checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "portable-atomic"
|
||||||
|
version = "1.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ppv-lite86"
|
name = "ppv-lite86"
|
||||||
version = "0.2.17"
|
version = "0.2.17"
|
||||||
|
@ -805,24 +794,25 @@ checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.78"
|
version = "1.0.79"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
|
checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pyo3"
|
name = "pyo3"
|
||||||
version = "0.20.2"
|
version = "0.20.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9a89dc7a5850d0e983be1ec2a463a171d20990487c3cfcd68b5363f1ee3d6fe0"
|
checksum = "53bdbb96d49157e65d45cc287af5f32ffadd5f4761438b527b055fb0d4bb8233"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"indoc",
|
"indoc",
|
||||||
"libc",
|
"libc",
|
||||||
"memoffset",
|
"memoffset",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
|
"portable-atomic",
|
||||||
"pyo3-build-config",
|
"pyo3-build-config",
|
||||||
"pyo3-ffi",
|
"pyo3-ffi",
|
||||||
"pyo3-macros",
|
"pyo3-macros",
|
||||||
|
@ -831,9 +821,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pyo3-build-config"
|
name = "pyo3-build-config"
|
||||||
version = "0.20.2"
|
version = "0.20.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "07426f0d8fe5a601f26293f300afd1a7b1ed5e78b2a705870c5f30893c5163be"
|
checksum = "deaa5745de3f5231ce10517a1f5dd97d53e5a2fd77aa6b5842292085831d48d7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"target-lexicon",
|
"target-lexicon",
|
||||||
|
@ -841,9 +831,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pyo3-ffi"
|
name = "pyo3-ffi"
|
||||||
version = "0.20.2"
|
version = "0.20.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "dbb7dec17e17766b46bca4f1a4215a85006b4c2ecde122076c562dd058da6cf1"
|
checksum = "62b42531d03e08d4ef1f6e85a2ed422eb678b8cd62b762e53891c05faf0d4afa"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"pyo3-build-config",
|
"pyo3-build-config",
|
||||||
|
@ -851,26 +841,27 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pyo3-macros"
|
name = "pyo3-macros"
|
||||||
version = "0.20.2"
|
version = "0.20.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "05f738b4e40d50b5711957f142878cfa0f28e054aa0ebdfc3fd137a843f74ed3"
|
checksum = "7305c720fa01b8055ec95e484a6eca7a83c841267f0dd5280f0c8b8551d2c158"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"pyo3-macros-backend",
|
"pyo3-macros-backend",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.50",
|
"syn 2.0.53",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pyo3-macros-backend"
|
name = "pyo3-macros-backend"
|
||||||
version = "0.20.2"
|
version = "0.20.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0fc910d4851847827daf9d6cdd4a823fbdaab5b8818325c5e97a86da79e8881f"
|
checksum = "7c7e9b68bb9c3149c5b0cade5d07f953d6d125eb4337723c4ccdb665f1f96185"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"heck",
|
"heck 0.4.1",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
|
"pyo3-build-config",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.50",
|
"syn 2.0.53",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -914,9 +905,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rayon"
|
name = "rayon"
|
||||||
version = "1.8.1"
|
version = "1.9.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fa7237101a77a10773db45d62004a272517633fbcc3df19d96455ede1122e051"
|
checksum = "e4963ed1bc86e4f3ee217022bd855b297cef07fb9eac5dfa1f788b220b49b3bd"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"either",
|
"either",
|
||||||
"rayon-core",
|
"rayon-core",
|
||||||
|
@ -961,26 +952,20 @@ dependencies = [
|
||||||
"aho-corasick",
|
"aho-corasick",
|
||||||
"memchr",
|
"memchr",
|
||||||
"regex-automata",
|
"regex-automata",
|
||||||
"regex-syntax 0.8.2",
|
"regex-syntax",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "regex-automata"
|
name = "regex-automata"
|
||||||
version = "0.4.5"
|
version = "0.4.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd"
|
checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aho-corasick",
|
"aho-corasick",
|
||||||
"memchr",
|
"memchr",
|
||||||
"regex-syntax 0.8.2",
|
"regex-syntax",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "regex-syntax"
|
|
||||||
version = "0.7.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "regex-syntax"
|
name = "regex-syntax"
|
||||||
version = "0.8.2"
|
version = "0.8.2"
|
||||||
|
@ -996,15 +981,15 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustix"
|
name = "rustix"
|
||||||
version = "0.38.31"
|
version = "0.38.32"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949"
|
checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.4.2",
|
"bitflags 2.5.0",
|
||||||
"errno",
|
"errno",
|
||||||
"libc",
|
"libc",
|
||||||
"linux-raw-sys",
|
"linux-raw-sys",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1019,6 +1004,15 @@ version = "1.0.17"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1"
|
checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "same-file"
|
||||||
|
version = "1.0.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "scopeguard"
|
name = "scopeguard"
|
||||||
version = "1.2.0"
|
version = "1.2.0"
|
||||||
|
@ -1048,7 +1042,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.50",
|
"syn 2.0.53",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1088,9 +1082,9 @@ checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "smallvec"
|
name = "smallvec"
|
||||||
version = "1.13.1"
|
version = "1.13.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7"
|
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "string-interner"
|
name = "string-interner"
|
||||||
|
@ -1135,9 +1129,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.50"
|
version = "2.0.53"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "74f1bdc9872430ce9b75da68329d1c1746faf50ffac5f19e02b71e37ff881ffb"
|
checksum = "7383cd0e49fff4b6b90ca5670bfd3e9d6a733b3f90c686605aa7eec8c4996032"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
@ -1152,14 +1146,14 @@ checksum = "e1fc403891a21bcfb7c37834ba66a547a8f402146eba7265b5a6d88059c9ff2f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tempfile"
|
name = "tempfile"
|
||||||
version = "3.10.0"
|
version = "3.10.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a365e8cd18e44762ef95d87f284f4b5cd04107fec2ff3052bd6a3e6069669e67"
|
checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"fastrand",
|
"fastrand",
|
||||||
"rustix",
|
"rustix",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1188,40 +1182,24 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror"
|
name = "thiserror"
|
||||||
version = "1.0.57"
|
version = "1.0.58"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b"
|
checksum = "03468839009160513471e86a034bb2c5c0e4baae3b43f79ffc55c4a5427b3297"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"thiserror-impl",
|
"thiserror-impl",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror-impl"
|
name = "thiserror-impl"
|
||||||
version = "1.0.57"
|
version = "1.0.58"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81"
|
checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.50",
|
"syn 2.0.53",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "time"
|
|
||||||
version = "0.3.20"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "cd0cbfecb4d19b5ea75bb31ad904eb5b9fa13f21079c3b92017ebdf4999a5890"
|
|
||||||
dependencies = [
|
|
||||||
"serde",
|
|
||||||
"time-core",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "time-core"
|
|
||||||
version = "0.1.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tiny-keccak"
|
name = "tiny-keccak"
|
||||||
version = "2.0.2"
|
version = "2.0.2"
|
||||||
|
@ -1303,9 +1281,9 @@ checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode_names2"
|
name = "unicode_names2"
|
||||||
version = "1.2.1"
|
version = "1.2.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ac64ef2f016dc69dfa8283394a70b057066eb054d5fcb6b9eb17bd2ec5097211"
|
checksum = "addeebf294df7922a1164f729fb27ebbbcea99cc32b3bf08afab62757f707677"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"phf",
|
"phf",
|
||||||
"unicode_names2_generator",
|
"unicode_names2_generator",
|
||||||
|
@ -1313,15 +1291,14 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode_names2_generator"
|
name = "unicode_names2_generator"
|
||||||
version = "1.2.1"
|
version = "1.2.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "013f6a731e80f3930de580e55ba41dfa846de4e0fdee4a701f97989cb1597d6a"
|
checksum = "f444b8bba042fe3c1251ffaca35c603f2dc2ccc08d595c65a8c4f76f3e8426c0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"getopts",
|
"getopts",
|
||||||
"log",
|
"log",
|
||||||
"phf_codegen",
|
"phf_codegen",
|
||||||
"rand",
|
"rand",
|
||||||
"time",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1342,6 +1319,16 @@ version = "0.9.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "walkdir"
|
||||||
|
version = "2.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
|
||||||
|
dependencies = [
|
||||||
|
"same-file",
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasi"
|
name = "wasi"
|
||||||
version = "0.11.0+wasi-snapshot-preview1"
|
version = "0.11.0+wasi-snapshot-preview1"
|
||||||
|
@ -1364,28 +1351,28 @@ version = "0.4.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-util"
|
||||||
|
version = "0.1.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
|
||||||
|
dependencies = [
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi-x86_64-pc-windows-gnu"
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows-sys"
|
|
||||||
version = "0.48.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
|
|
||||||
dependencies = [
|
|
||||||
"windows-targets 0.48.5",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-sys"
|
name = "windows-sys"
|
||||||
version = "0.52.0"
|
version = "0.52.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-targets 0.52.3",
|
"windows-targets 0.52.4",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1405,17 +1392,17 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-targets"
|
name = "windows-targets"
|
||||||
version = "0.52.3"
|
version = "0.52.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d380ba1dc7187569a8a9e91ed34b8ccfc33123bbacb8c0aed2d1ad7f3ef2dc5f"
|
checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows_aarch64_gnullvm 0.52.3",
|
"windows_aarch64_gnullvm 0.52.4",
|
||||||
"windows_aarch64_msvc 0.52.3",
|
"windows_aarch64_msvc 0.52.4",
|
||||||
"windows_i686_gnu 0.52.3",
|
"windows_i686_gnu 0.52.4",
|
||||||
"windows_i686_msvc 0.52.3",
|
"windows_i686_msvc 0.52.4",
|
||||||
"windows_x86_64_gnu 0.52.3",
|
"windows_x86_64_gnu 0.52.4",
|
||||||
"windows_x86_64_gnullvm 0.52.3",
|
"windows_x86_64_gnullvm 0.52.4",
|
||||||
"windows_x86_64_msvc 0.52.3",
|
"windows_x86_64_msvc 0.52.4",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1426,9 +1413,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_aarch64_gnullvm"
|
name = "windows_aarch64_gnullvm"
|
||||||
version = "0.52.3"
|
version = "0.52.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "68e5dcfb9413f53afd9c8f86e56a7b4d86d9a2fa26090ea2dc9e40fba56c6ec6"
|
checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_aarch64_msvc"
|
name = "windows_aarch64_msvc"
|
||||||
|
@ -1438,9 +1425,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_aarch64_msvc"
|
name = "windows_aarch64_msvc"
|
||||||
version = "0.52.3"
|
version = "0.52.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8dab469ebbc45798319e69eebf92308e541ce46760b49b18c6b3fe5e8965b30f"
|
checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_gnu"
|
name = "windows_i686_gnu"
|
||||||
|
@ -1450,9 +1437,9 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_gnu"
|
name = "windows_i686_gnu"
|
||||||
version = "0.52.3"
|
version = "0.52.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2a4e9b6a7cac734a8b4138a4e1044eac3404d8326b6c0f939276560687a033fb"
|
checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_msvc"
|
name = "windows_i686_msvc"
|
||||||
|
@ -1462,9 +1449,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_msvc"
|
name = "windows_i686_msvc"
|
||||||
version = "0.52.3"
|
version = "0.52.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "28b0ec9c422ca95ff34a78755cfa6ad4a51371da2a5ace67500cf7ca5f232c58"
|
checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnu"
|
name = "windows_x86_64_gnu"
|
||||||
|
@ -1474,9 +1461,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnu"
|
name = "windows_x86_64_gnu"
|
||||||
version = "0.52.3"
|
version = "0.52.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "704131571ba93e89d7cd43482277d6632589b18ecf4468f591fbae0a8b101614"
|
checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnullvm"
|
name = "windows_x86_64_gnullvm"
|
||||||
|
@ -1486,9 +1473,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnullvm"
|
name = "windows_x86_64_gnullvm"
|
||||||
version = "0.52.3"
|
version = "0.52.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "42079295511643151e98d61c38c0acc444e52dd42ab456f7ccfd5152e8ecf21c"
|
checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_msvc"
|
name = "windows_x86_64_msvc"
|
||||||
|
@ -1498,9 +1485,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_msvc"
|
name = "windows_x86_64_msvc"
|
||||||
version = "0.52.3"
|
version = "0.52.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0770833d60a970638e989b3fa9fd2bb1aaadcf88963d1659fd7d9990196ed2d6"
|
checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "yaml-rust"
|
name = "yaml-rust"
|
||||||
|
@ -1528,5 +1515,5 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.50",
|
"syn 2.0.53",
|
||||||
]
|
]
|
||||||
|
|
|
@ -5,7 +5,7 @@ use nac3core::{
|
||||||
toplevel::{
|
toplevel::{
|
||||||
DefinitionId,
|
DefinitionId,
|
||||||
helper::PRIMITIVE_DEF_IDS,
|
helper::PRIMITIVE_DEF_IDS,
|
||||||
numpy::{make_ndarray_ty, unpack_ndarray_tvars},
|
numpy::{make_ndarray_ty, unpack_ndarray_var_tys},
|
||||||
TopLevelDef,
|
TopLevelDef,
|
||||||
},
|
},
|
||||||
typecheck::{
|
typecheck::{
|
||||||
|
@ -654,7 +654,7 @@ impl InnerResolver {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(TypeEnum::TObj { obj_id, .. }, false) if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
(TypeEnum::TObj { obj_id, .. }, false) if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
||||||
let (ty, ndims) = unpack_ndarray_tvars(unifier, extracted_ty);
|
let (ty, ndims) = unpack_ndarray_var_tys(unifier, extracted_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!(
|
||||||
|
|
|
@ -7,6 +7,7 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
itertools = "0.12"
|
itertools = "0.12"
|
||||||
crossbeam = "0.8"
|
crossbeam = "0.8"
|
||||||
|
indexmap = "2.2"
|
||||||
parking_lot = "0.12"
|
parking_lot = "0.12"
|
||||||
rayon = "1.8"
|
rayon = "1.8"
|
||||||
nac3parser = { path = "../nac3parser" }
|
nac3parser = { path = "../nac3parser" }
|
||||||
|
|
|
@ -21,7 +21,7 @@ fn main() {
|
||||||
match env::var("PROFILE").as_deref() {
|
match env::var("PROFILE").as_deref() {
|
||||||
Ok("debug") => "-O0",
|
Ok("debug") => "-O0",
|
||||||
Ok("release") => "-O3",
|
Ok("release") => "-O3",
|
||||||
flavor => panic!("Unknown or missing build flavor {:?}", flavor),
|
flavor => panic!("Unknown or missing build flavor {flavor:?}"),
|
||||||
},
|
},
|
||||||
"-emit-llvm",
|
"-emit-llvm",
|
||||||
"-S",
|
"-S",
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -9,6 +9,7 @@ use crate::{
|
||||||
|
|
||||||
use nac3parser::ast::StrRef;
|
use nac3parser::ast::StrRef;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use indexmap::IndexMap;
|
||||||
|
|
||||||
pub struct ConcreteTypeStore {
|
pub struct ConcreteTypeStore {
|
||||||
store: Vec<ConcreteTypeEnum>,
|
store: Vec<ConcreteTypeEnum>,
|
||||||
|
@ -50,7 +51,7 @@ pub enum ConcreteTypeEnum {
|
||||||
TObj {
|
TObj {
|
||||||
obj_id: DefinitionId,
|
obj_id: DefinitionId,
|
||||||
fields: HashMap<StrRef, (ConcreteType, bool)>,
|
fields: HashMap<StrRef, (ConcreteType, bool)>,
|
||||||
params: HashMap<u32, ConcreteType>,
|
params: IndexMap<u32, ConcreteType>,
|
||||||
},
|
},
|
||||||
TVirtual {
|
TVirtual {
|
||||||
ty: ConcreteType,
|
ty: ConcreteType,
|
||||||
|
|
|
@ -2,13 +2,21 @@ use std::{collections::HashMap, convert::TryInto, iter::once, iter::zip};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
codegen::{
|
codegen::{
|
||||||
classes::{ListValue, NDArrayValue, RangeValue},
|
classes::{
|
||||||
|
ArrayLikeIndexer,
|
||||||
|
ArrayLikeValue,
|
||||||
|
ListValue,
|
||||||
|
NDArrayValue,
|
||||||
|
RangeValue,
|
||||||
|
UntypedArrayLikeAccessor,
|
||||||
|
},
|
||||||
concrete_type::{ConcreteFuncArg, ConcreteTypeEnum, ConcreteTypeStore},
|
concrete_type::{ConcreteFuncArg, ConcreteTypeEnum, ConcreteTypeStore},
|
||||||
gen_in_range_check,
|
gen_in_range_check,
|
||||||
get_llvm_type,
|
get_llvm_type,
|
||||||
get_llvm_abi_type,
|
get_llvm_abi_type,
|
||||||
irrt::*,
|
irrt::*,
|
||||||
llvm_intrinsics::{call_expect, call_float_floor, call_float_pow, call_float_powi},
|
llvm_intrinsics::{call_expect, call_float_floor, call_float_pow, call_float_powi},
|
||||||
|
numpy,
|
||||||
stmt::{gen_raise, gen_var},
|
stmt::{gen_raise, gen_var},
|
||||||
CodeGenContext, CodeGenTask,
|
CodeGenContext, CodeGenTask,
|
||||||
},
|
},
|
||||||
|
@ -16,7 +24,7 @@ use crate::{
|
||||||
toplevel::{
|
toplevel::{
|
||||||
DefinitionId,
|
DefinitionId,
|
||||||
helper::PRIMITIVE_DEF_IDS,
|
helper::PRIMITIVE_DEF_IDS,
|
||||||
numpy::make_ndarray_ty,
|
numpy::{make_ndarray_ty, unpack_ndarray_var_tys},
|
||||||
TopLevelDef,
|
TopLevelDef,
|
||||||
},
|
},
|
||||||
typecheck::{
|
typecheck::{
|
||||||
|
@ -52,7 +60,7 @@ pub fn get_subst_key(
|
||||||
params.clone()
|
params.clone()
|
||||||
})
|
})
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
vars.extend(fun_vars.iter());
|
vars.extend(fun_vars);
|
||||||
let sorted = vars.keys().filter(|id| filter.map_or(true, |v| v.contains(id))).sorted();
|
let sorted = vars.keys().filter(|id| filter.map_or(true, |v| v.contains(id))).sorted();
|
||||||
sorted
|
sorted
|
||||||
.map(|id| {
|
.map(|id| {
|
||||||
|
@ -103,9 +111,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
||||||
index
|
index
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gen_symbol_val(
|
pub fn gen_symbol_val<G: CodeGenerator + ?Sized>(
|
||||||
&mut self,
|
&mut self,
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
val: &SymbolValue,
|
val: &SymbolValue,
|
||||||
ty: Type,
|
ty: Type,
|
||||||
) -> BasicValueEnum<'ctx> {
|
) -> BasicValueEnum<'ctx> {
|
||||||
|
@ -174,9 +182,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// See [`get_llvm_type`].
|
/// See [`get_llvm_type`].
|
||||||
pub fn get_llvm_type(
|
pub fn get_llvm_type<G: CodeGenerator + ?Sized>(
|
||||||
&mut self,
|
&mut self,
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
ty: Type,
|
ty: Type,
|
||||||
) -> BasicTypeEnum<'ctx> {
|
) -> BasicTypeEnum<'ctx> {
|
||||||
get_llvm_type(
|
get_llvm_type(
|
||||||
|
@ -191,9 +199,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// See [`get_llvm_abi_type`].
|
/// See [`get_llvm_abi_type`].
|
||||||
pub fn get_llvm_abi_type(
|
pub fn get_llvm_abi_type<G: CodeGenerator + ?Sized>(
|
||||||
&mut self,
|
&mut self,
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
ty: Type,
|
ty: Type,
|
||||||
) -> BasicTypeEnum<'ctx> {
|
) -> BasicTypeEnum<'ctx> {
|
||||||
get_llvm_abi_type(
|
get_llvm_abi_type(
|
||||||
|
@ -209,9 +217,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates an LLVM variable for a [constant value][value] with a given [type][ty].
|
/// Generates an LLVM variable for a [constant value][value] with a given [type][ty].
|
||||||
pub fn gen_const(
|
pub fn gen_const<G: CodeGenerator + ?Sized>(
|
||||||
&mut self,
|
&mut self,
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
value: &Constant,
|
value: &Constant,
|
||||||
ty: Type,
|
ty: Type,
|
||||||
) -> Option<BasicValueEnum<'ctx>> {
|
) -> Option<BasicValueEnum<'ctx>> {
|
||||||
|
@ -291,9 +299,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates a binary operation `op` between two integral operands `lhs` and `rhs`.
|
/// Generates a binary operation `op` between two integral operands `lhs` and `rhs`.
|
||||||
pub fn gen_int_ops(
|
pub fn gen_int_ops<G: CodeGenerator + ?Sized>(
|
||||||
&mut self,
|
&mut self,
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
op: &Operator,
|
op: &Operator,
|
||||||
lhs: BasicValueEnum<'ctx>,
|
lhs: BasicValueEnum<'ctx>,
|
||||||
rhs: BasicValueEnum<'ctx>,
|
rhs: BasicValueEnum<'ctx>,
|
||||||
|
@ -492,17 +500,21 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper function for generating a LLVM variable storing a [String].
|
/// Helper function for generating a LLVM variable storing a [String].
|
||||||
pub fn gen_string<S: Into<String>>(
|
pub fn gen_string<G, S>(
|
||||||
&mut self,
|
&mut self,
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
s: S,
|
s: S,
|
||||||
) -> BasicValueEnum<'ctx> {
|
) -> BasicValueEnum<'ctx>
|
||||||
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
|
S: Into<String>,
|
||||||
|
{
|
||||||
self.gen_const(generator, &Constant::Str(s.into()), self.primitives.str).unwrap()
|
self.gen_const(generator, &Constant::Str(s.into()), self.primitives.str).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn raise_exn(
|
pub fn raise_exn<G: CodeGenerator + ?Sized>(
|
||||||
&mut self,
|
&mut self,
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
name: &str,
|
name: &str,
|
||||||
msg: BasicValueEnum<'ctx>,
|
msg: BasicValueEnum<'ctx>,
|
||||||
params: [Option<IntValue<'ctx>>; 3],
|
params: [Option<IntValue<'ctx>>; 3],
|
||||||
|
@ -546,9 +558,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
||||||
gen_raise(generator, self, Some(&zelf.into()), loc);
|
gen_raise(generator, self, Some(&zelf.into()), loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn make_assert(
|
pub fn make_assert<G: CodeGenerator + ?Sized>(
|
||||||
&mut self,
|
&mut self,
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
cond: IntValue<'ctx>,
|
cond: IntValue<'ctx>,
|
||||||
err_name: &str,
|
err_name: &str,
|
||||||
err_msg: &str,
|
err_msg: &str,
|
||||||
|
@ -559,9 +571,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
||||||
self.make_assert_impl(generator, cond, err_name, err_msg, params, loc);
|
self.make_assert_impl(generator, cond, err_name, err_msg, params, loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn make_assert_impl(
|
pub fn make_assert_impl<G: CodeGenerator + ?Sized>(
|
||||||
&mut self,
|
&mut self,
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
cond: IntValue<'ctx>,
|
cond: IntValue<'ctx>,
|
||||||
err_name: &str,
|
err_name: &str,
|
||||||
err_msg: BasicValueEnum<'ctx>,
|
err_msg: BasicValueEnum<'ctx>,
|
||||||
|
@ -878,7 +890,7 @@ pub fn destructure_range<'ctx>(
|
||||||
/// Returns an instance of [`PointerValue`] pointing to the List structure. The List structure is
|
/// Returns an instance of [`PointerValue`] pointing to the List structure. The List structure is
|
||||||
/// defined as `type { ty*, size_t }` in LLVM, where the first element stores the pointer to the
|
/// defined as `type { ty*, size_t }` in LLVM, where the first element stores the pointer to the
|
||||||
/// data, and the second element stores the size of the List.
|
/// data, and the second element stores the size of the List.
|
||||||
pub fn allocate_list<'ctx, G: CodeGenerator>(
|
pub fn allocate_list<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
generator: &mut G,
|
generator: &mut G,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
ty: BasicTypeEnum<'ctx>,
|
ty: BasicTypeEnum<'ctx>,
|
||||||
|
@ -978,7 +990,7 @@ pub fn gen_comprehension<'ctx, G: CodeGenerator>(
|
||||||
list_alloc_size.into_int_value(),
|
list_alloc_size.into_int_value(),
|
||||||
Some("listcomp.addr")
|
Some("listcomp.addr")
|
||||||
);
|
);
|
||||||
list_content = list.get_data().get_ptr(ctx);
|
list_content = list.data().base_ptr(ctx, generator);
|
||||||
|
|
||||||
let i = generator.gen_store_target(ctx, target, Some("i.addr"))?.unwrap();
|
let i = generator.gen_store_target(ctx, target, Some("i.addr"))?.unwrap();
|
||||||
ctx.builder
|
ctx.builder
|
||||||
|
@ -1011,7 +1023,7 @@ pub fn gen_comprehension<'ctx, G: CodeGenerator>(
|
||||||
)
|
)
|
||||||
.into_int_value();
|
.into_int_value();
|
||||||
list = allocate_list(generator, ctx, elem_ty, length, Some("listcomp"));
|
list = allocate_list(generator, ctx, elem_ty, length, Some("listcomp"));
|
||||||
list_content = list.get_data().get_ptr(ctx);
|
list_content = list.data().base_ptr(ctx, generator);
|
||||||
let counter = generator.gen_var_alloc(ctx, size_t.into(), Some("counter.addr"))?;
|
let counter = generator.gen_var_alloc(ctx, size_t.into(), Some("counter.addr"))?;
|
||||||
// counter = -1
|
// counter = -1
|
||||||
ctx.builder.build_store(counter, size_t.const_int(u64::MAX, true)).unwrap();
|
ctx.builder.build_store(counter, size_t.const_int(u64::MAX, true)).unwrap();
|
||||||
|
@ -1075,37 +1087,25 @@ pub fn gen_comprehension<'ctx, G: CodeGenerator>(
|
||||||
|
|
||||||
emit_cont_bb(ctx, generator, list);
|
emit_cont_bb(ctx, generator, list);
|
||||||
|
|
||||||
Ok(Some(list.get_ptr().into()))
|
Ok(Some(list.as_ptr_value().into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates LLVM IR for a [binary operator expression][expr].
|
/// Generates LLVM IR for a binary operator expression using the [`Type`] and
|
||||||
///
|
/// [LLVM value][`BasicValueEnum`] of the operands.
|
||||||
/// * `left` - The left-hand side of the binary operator.
|
pub fn gen_binop_expr_with_values<'ctx, G: CodeGenerator>(
|
||||||
/// * `op` - The operator applied on the operands.
|
|
||||||
/// * `right` - The right-hand side of the binary operator.
|
|
||||||
/// * `loc` - The location of the full expression.
|
|
||||||
/// * `is_aug_assign` - Whether the binary operator expression is also an assignment operator.
|
|
||||||
pub fn gen_binop_expr<'ctx, G: CodeGenerator>(
|
|
||||||
generator: &mut G,
|
generator: &mut G,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
left: &Expr<Option<Type>>,
|
left: (&Option<Type>, BasicValueEnum<'ctx>),
|
||||||
op: &Operator,
|
op: &Operator,
|
||||||
right: &Expr<Option<Type>>,
|
right: (&Option<Type>, BasicValueEnum<'ctx>),
|
||||||
loc: Location,
|
loc: Location,
|
||||||
is_aug_assign: bool,
|
is_aug_assign: bool,
|
||||||
) -> Result<Option<ValueEnum<'ctx>>, String> {
|
) -> Result<Option<ValueEnum<'ctx>>, String> {
|
||||||
let ty1 = ctx.unifier.get_representative(left.custom.unwrap());
|
let (left_ty, left_val) = left;
|
||||||
let ty2 = ctx.unifier.get_representative(right.custom.unwrap());
|
let (right_ty, right_val) = right;
|
||||||
let left_val = if let Some(v) = generator.gen_expr(ctx, left)? {
|
|
||||||
v.to_basic_value_enum(ctx, generator, left.custom.unwrap())?
|
let ty1 = ctx.unifier.get_representative(left_ty.unwrap());
|
||||||
} else {
|
let ty2 = ctx.unifier.get_representative(right_ty.unwrap());
|
||||||
return Ok(None)
|
|
||||||
};
|
|
||||||
let right_val = if let Some(v) = generator.gen_expr(ctx, right)? {
|
|
||||||
v.to_basic_value_enum(ctx, generator, right.custom.unwrap())?
|
|
||||||
} else {
|
|
||||||
return Ok(None)
|
|
||||||
};
|
|
||||||
|
|
||||||
// we can directly compare the types, because we've got their representatives
|
// we can directly compare the types, because we've got their representatives
|
||||||
// which would be unchanged until further unification, which we would never do
|
// which would be unchanged until further unification, which we would never do
|
||||||
|
@ -1129,8 +1129,46 @@ pub fn gen_binop_expr<'ctx, G: CodeGenerator>(
|
||||||
Some("f_pow_i")
|
Some("f_pow_i")
|
||||||
);
|
);
|
||||||
Ok(Some(res.into()))
|
Ok(Some(res.into()))
|
||||||
|
} else if matches!(&*ctx.unifier.get_ty(ty1), TypeEnum::TObj { obj_id, .. } if obj_id == &PRIMITIVE_DEF_IDS.ndarray) && matches!(&*ctx.unifier.get_ty(ty2), TypeEnum::TObj { obj_id, .. } if obj_id == &PRIMITIVE_DEF_IDS.ndarray) {
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
let (ndarray_dtype1, _) = unpack_ndarray_var_tys(&mut ctx.unifier, ty1);
|
||||||
|
let (ndarray_dtype2, _) = unpack_ndarray_var_tys(&mut ctx.unifier, ty1);
|
||||||
|
|
||||||
|
assert!(ctx.unifier.unioned(ndarray_dtype1, ndarray_dtype2));
|
||||||
|
|
||||||
|
let left_val = NDArrayValue::from_ptr_val(
|
||||||
|
left_val.into_pointer_value(),
|
||||||
|
llvm_usize,
|
||||||
|
None
|
||||||
|
);
|
||||||
|
let right_val = NDArrayValue::from_ptr_val(
|
||||||
|
right_val.into_pointer_value(),
|
||||||
|
llvm_usize,
|
||||||
|
None
|
||||||
|
);
|
||||||
|
let res = numpy::ndarray_elementwise_binop_impl(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
ndarray_dtype1,
|
||||||
|
if is_aug_assign { Some(left_val) } else { None },
|
||||||
|
left_val,
|
||||||
|
right_val,
|
||||||
|
|generator, ctx, elem_ty, (lhs, rhs)| {
|
||||||
|
gen_binop_expr_with_values(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
(&Some(elem_ty), lhs),
|
||||||
|
op,
|
||||||
|
(&Some(elem_ty), rhs),
|
||||||
|
ctx.current_loc,
|
||||||
|
is_aug_assign,
|
||||||
|
)?.unwrap().to_basic_value_enum(ctx, generator, elem_ty)
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(Some(res.as_ptr_value().into()))
|
||||||
} else {
|
} else {
|
||||||
let left_ty_enum = ctx.unifier.get_ty_immutable(left.custom.unwrap());
|
let left_ty_enum = ctx.unifier.get_ty_immutable(left_ty.unwrap());
|
||||||
let TypeEnum::TObj { fields, obj_id, .. } = left_ty_enum.as_ref() else {
|
let TypeEnum::TObj { fields, obj_id, .. } = left_ty_enum.as_ref() else {
|
||||||
unreachable!("must be tobj")
|
unreachable!("must be tobj")
|
||||||
};
|
};
|
||||||
|
@ -1150,7 +1188,7 @@ pub fn gen_binop_expr<'ctx, G: CodeGenerator>(
|
||||||
let signature = if let Some(call) = ctx.calls.get(&loc.into()) {
|
let signature = if let Some(call) = ctx.calls.get(&loc.into()) {
|
||||||
ctx.unifier.get_call_signature(*call).unwrap()
|
ctx.unifier.get_call_signature(*call).unwrap()
|
||||||
} else {
|
} else {
|
||||||
let left_enum_ty = ctx.unifier.get_ty_immutable(left.custom.unwrap());
|
let left_enum_ty = ctx.unifier.get_ty_immutable(left_ty.unwrap());
|
||||||
let TypeEnum::TObj { fields, .. } = left_enum_ty.as_ref() else {
|
let TypeEnum::TObj { fields, .. } = left_enum_ty.as_ref() else {
|
||||||
unreachable!("must be tobj")
|
unreachable!("must be tobj")
|
||||||
};
|
};
|
||||||
|
@ -1175,13 +1213,51 @@ pub fn gen_binop_expr<'ctx, G: CodeGenerator>(
|
||||||
generator
|
generator
|
||||||
.gen_call(
|
.gen_call(
|
||||||
ctx,
|
ctx,
|
||||||
Some((left.custom.unwrap(), left_val.into())),
|
Some((left_ty.unwrap(), left_val.into())),
|
||||||
(&signature, fun_id),
|
(&signature, fun_id),
|
||||||
vec![(None, right_val.into())],
|
vec![(None, right_val.into())],
|
||||||
).map(|f| f.map(Into::into))
|
).map(|f| f.map(Into::into))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generates LLVM IR for a [binary operator expression][expr].
|
||||||
|
///
|
||||||
|
/// * `left` - The left-hand side of the binary operator.
|
||||||
|
/// * `op` - The operator applied on the operands.
|
||||||
|
/// * `right` - The right-hand side of the binary operator.
|
||||||
|
/// * `loc` - The location of the full expression.
|
||||||
|
/// * `is_aug_assign` - Whether the binary operator expression is also an assignment operator.
|
||||||
|
pub fn gen_binop_expr<'ctx, G: CodeGenerator>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
left: &Expr<Option<Type>>,
|
||||||
|
op: &Operator,
|
||||||
|
right: &Expr<Option<Type>>,
|
||||||
|
loc: Location,
|
||||||
|
is_aug_assign: bool,
|
||||||
|
) -> Result<Option<ValueEnum<'ctx>>, String> {
|
||||||
|
let left_val = if let Some(v) = generator.gen_expr(ctx, left)? {
|
||||||
|
v.to_basic_value_enum(ctx, generator, left.custom.unwrap())?
|
||||||
|
} else {
|
||||||
|
return Ok(None)
|
||||||
|
};
|
||||||
|
let right_val = if let Some(v) = generator.gen_expr(ctx, right)? {
|
||||||
|
v.to_basic_value_enum(ctx, generator, right.custom.unwrap())?
|
||||||
|
} else {
|
||||||
|
return Ok(None)
|
||||||
|
};
|
||||||
|
|
||||||
|
gen_binop_expr_with_values(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
(&left.custom, left_val),
|
||||||
|
op,
|
||||||
|
(&right.custom, right_val),
|
||||||
|
loc,
|
||||||
|
is_aug_assign,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Generates code for a subscript expression on an `ndarray`.
|
/// Generates code for a subscript expression on an `ndarray`.
|
||||||
///
|
///
|
||||||
/// * `ty` - The `Type` of the `NDArray` elements.
|
/// * `ty` - The `Type` of the `NDArray` elements.
|
||||||
|
@ -1255,8 +1331,8 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
|
||||||
return Ok(None)
|
return Ok(None)
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Some(v.get_data()
|
Ok(Some(v.data()
|
||||||
.get_const(
|
.get(
|
||||||
ctx,
|
ctx,
|
||||||
generator,
|
generator,
|
||||||
ctx.ctx.i32_type().const_array(&[index]),
|
ctx.ctx.i32_type().const_array(&[index]),
|
||||||
|
@ -1297,18 +1373,20 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
|
||||||
);
|
);
|
||||||
|
|
||||||
let ndarray_num_dims = ndarray.load_ndims(ctx);
|
let ndarray_num_dims = ndarray.load_ndims(ctx);
|
||||||
ndarray.create_dims(ctx, llvm_usize, ndarray_num_dims);
|
ndarray.create_dim_sizes(ctx, llvm_usize, ndarray_num_dims);
|
||||||
|
|
||||||
let ndarray_num_dims = ndarray.load_ndims(ctx);
|
let ndarray_num_dims = ndarray.load_ndims(ctx);
|
||||||
let v_dims_src_ptr = v.get_dims().ptr_offset(
|
let v_dims_src_ptr = unsafe {
|
||||||
ctx,
|
v.dim_sizes().ptr_offset_unchecked(
|
||||||
generator,
|
ctx,
|
||||||
llvm_usize.const_int(1, false),
|
generator,
|
||||||
None,
|
llvm_usize.const_int(1, false),
|
||||||
);
|
None,
|
||||||
|
)
|
||||||
|
};
|
||||||
call_memcpy_generic(
|
call_memcpy_generic(
|
||||||
ctx,
|
ctx,
|
||||||
ndarray.get_dims().get_ptr(ctx),
|
ndarray.dim_sizes().base_ptr(ctx, generator),
|
||||||
v_dims_src_ptr,
|
v_dims_src_ptr,
|
||||||
ctx.builder
|
ctx.builder
|
||||||
.build_int_mul(ndarray_num_dims, llvm_usize.size_of(), "")
|
.build_int_mul(ndarray_num_dims, llvm_usize.size_of(), "")
|
||||||
|
@ -1320,12 +1398,11 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
|
||||||
let ndarray_num_elems = call_ndarray_calc_size(
|
let ndarray_num_elems = call_ndarray_calc_size(
|
||||||
generator,
|
generator,
|
||||||
ctx,
|
ctx,
|
||||||
ndarray.load_ndims(ctx),
|
&ndarray.dim_sizes().as_slice_value(ctx, generator),
|
||||||
ndarray.get_dims().get_ptr(ctx),
|
|
||||||
);
|
);
|
||||||
ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems);
|
ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems);
|
||||||
|
|
||||||
let v_data_src_ptr = v.get_data().ptr_offset_const(
|
let v_data_src_ptr = v.data().ptr_offset(
|
||||||
ctx,
|
ctx,
|
||||||
generator,
|
generator,
|
||||||
ctx.ctx.i32_type().const_array(&[index]),
|
ctx.ctx.i32_type().const_array(&[index]),
|
||||||
|
@ -1333,7 +1410,7 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
|
||||||
);
|
);
|
||||||
call_memcpy_generic(
|
call_memcpy_generic(
|
||||||
ctx,
|
ctx,
|
||||||
ndarray.get_data().get_ptr(ctx),
|
ndarray.data().base_ptr(ctx, generator),
|
||||||
v_data_src_ptr,
|
v_data_src_ptr,
|
||||||
ctx.builder
|
ctx.builder
|
||||||
.build_int_mul(ndarray_num_elems, llvm_ndarray_data_t.size_of().unwrap(), "")
|
.build_int_mul(ndarray_num_elems, llvm_ndarray_data_t.size_of().unwrap(), "")
|
||||||
|
@ -1342,7 +1419,7 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
|
||||||
llvm_i1.const_zero(),
|
llvm_i1.const_zero(),
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(Some(ndarray.get_ptr().into()))
|
Ok(Some(ndarray.as_ptr_value().into()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1425,13 +1502,13 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
|
||||||
};
|
};
|
||||||
let length = generator.get_size_type(ctx.ctx).const_int(elements.len() as u64, false);
|
let length = generator.get_size_type(ctx.ctx).const_int(elements.len() as u64, false);
|
||||||
let arr_str_ptr = allocate_list(generator, ctx, ty, length, Some("list"));
|
let arr_str_ptr = allocate_list(generator, ctx, ty, length, Some("list"));
|
||||||
let arr_ptr = arr_str_ptr.get_data();
|
let arr_ptr = arr_str_ptr.data();
|
||||||
for (i, v) in elements.iter().enumerate() {
|
for (i, v) in elements.iter().enumerate() {
|
||||||
let elem_ptr = arr_ptr
|
let elem_ptr = arr_ptr
|
||||||
.ptr_offset(ctx, generator, usize.const_int(i as u64, false), Some("elem_ptr"));
|
.ptr_offset(ctx, generator, usize.const_int(i as u64, false), Some("elem_ptr"));
|
||||||
ctx.builder.build_store(elem_ptr, *v).unwrap();
|
ctx.builder.build_store(elem_ptr, *v).unwrap();
|
||||||
}
|
}
|
||||||
arr_str_ptr.get_ptr().into()
|
arr_str_ptr.as_ptr_value().into()
|
||||||
}
|
}
|
||||||
ExprKind::Tuple { elts, .. } => {
|
ExprKind::Tuple { elts, .. } => {
|
||||||
let elements_val = elts
|
let elements_val = elts
|
||||||
|
@ -1924,7 +2001,7 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
|
||||||
v,
|
v,
|
||||||
(start, end, step),
|
(start, end, step),
|
||||||
);
|
);
|
||||||
res_array_ret.get_ptr().into()
|
res_array_ret.as_ptr_value().into()
|
||||||
} else {
|
} else {
|
||||||
let len = v.load_size(ctx, Some("len"));
|
let len = v.load_size(ctx, Some("len"));
|
||||||
let raw_index = if let Some(v) = generator.gen_expr(ctx, slice)? {
|
let raw_index = if let Some(v) = generator.gen_expr(ctx, slice)? {
|
||||||
|
@ -1966,12 +2043,11 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
|
||||||
[Some(raw_index), Some(len), None],
|
[Some(raw_index), Some(len), None],
|
||||||
expr.location,
|
expr.location,
|
||||||
);
|
);
|
||||||
v.get_data().get(ctx, generator, index, None).into()
|
v.data().get(ctx, generator, index, None).into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TypeEnum::TObj { obj_id, params, .. } if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
TypeEnum::TObj { obj_id, params, .. } if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
||||||
let (ty, ndims) = params.iter()
|
let (ty, ndims) = params.iter()
|
||||||
.sorted_by_key(|(var_id, _)| *var_id)
|
|
||||||
.map(|(_, ty)| ty)
|
.map(|(_, ty)| ty)
|
||||||
.collect_tuple()
|
.collect_tuple()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
codegen::{expr::*, stmt::*, bool_to_i1, bool_to_i8, CodeGenContext},
|
codegen::{classes::ArraySliceValue, expr::*, stmt::*, bool_to_i1, bool_to_i8, CodeGenContext},
|
||||||
symbol_resolver::ValueEnum,
|
symbol_resolver::ValueEnum,
|
||||||
toplevel::{DefinitionId, TopLevelDef},
|
toplevel::{DefinitionId, TopLevelDef},
|
||||||
typecheck::typedef::{FunSignature, Type},
|
typecheck::typedef::{FunSignature, Type},
|
||||||
|
@ -99,8 +99,8 @@ pub trait CodeGenerator {
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
ty: BasicTypeEnum<'ctx>,
|
ty: BasicTypeEnum<'ctx>,
|
||||||
size: IntValue<'ctx>,
|
size: IntValue<'ctx>,
|
||||||
name: Option<&str>,
|
name: Option<&'ctx str>,
|
||||||
) -> Result<PointerValue<'ctx>, String> {
|
) -> Result<ArraySliceValue<'ctx>, String> {
|
||||||
gen_array_var(ctx, ty, size, name)
|
gen_array_var(ctx, ty, size, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,8 @@ typedef unsigned _BitInt(64) uint64_t;
|
||||||
# define MAX(a, b) (a > b ? a : b)
|
# define MAX(a, b) (a > b ? a : b)
|
||||||
# define MIN(a, b) (a > b ? b : a)
|
# 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
|
// 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
|
// need to make sure `exp >= 0` before calling this function
|
||||||
#define DEF_INT_EXP(T) T __nac3_int_exp_##T( \
|
#define DEF_INT_EXP(T) T __nac3_int_exp_##T( \
|
||||||
|
@ -293,3 +295,87 @@ uint64_t __nac3_ndarray_flatten_index64(
|
||||||
}
|
}
|
||||||
return idx;
|
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 uint64_t *in_idx,
|
||||||
|
uint64_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 : in_idx[src_i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,9 +1,18 @@
|
||||||
use crate::typecheck::typedef::Type;
|
use crate::typecheck::typedef::Type;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
classes::{ListValue, NDArrayValue},
|
classes::{
|
||||||
|
ArrayLikeIndexer,
|
||||||
|
ArraySliceValue,
|
||||||
|
ArrayLikeValue,
|
||||||
|
ListValue,
|
||||||
|
NDArrayValue,
|
||||||
|
UntypedArrayLikeAccessor,
|
||||||
|
UntypedArrayLikeMutator,
|
||||||
|
},
|
||||||
CodeGenContext,
|
CodeGenContext,
|
||||||
CodeGenerator,
|
CodeGenerator,
|
||||||
|
llvm_intrinsics,
|
||||||
};
|
};
|
||||||
use inkwell::{
|
use inkwell::{
|
||||||
attributes::{Attribute, AttributeLoc},
|
attributes::{Attribute, AttributeLoc},
|
||||||
|
@ -39,8 +48,8 @@ pub fn load_irrt(ctx: &Context) -> Module {
|
||||||
|
|
||||||
// repeated squaring method adapted from GNU Scientific Library:
|
// repeated squaring method adapted from GNU Scientific Library:
|
||||||
// https://git.savannah.gnu.org/cgit/gsl.git/tree/sys/pow_int.c
|
// https://git.savannah.gnu.org/cgit/gsl.git/tree/sys/pow_int.c
|
||||||
pub fn integer_power<'ctx>(
|
pub fn integer_power<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
base: IntValue<'ctx>,
|
base: IntValue<'ctx>,
|
||||||
exp: IntValue<'ctx>,
|
exp: IntValue<'ctx>,
|
||||||
|
@ -81,8 +90,8 @@ pub fn integer_power<'ctx>(
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn calculate_len_for_slice_range<'ctx>(
|
pub fn calculate_len_for_slice_range<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
start: IntValue<'ctx>,
|
start: IntValue<'ctx>,
|
||||||
end: IntValue<'ctx>,
|
end: IntValue<'ctx>,
|
||||||
|
@ -303,8 +312,8 @@ pub fn handle_slice_index_bound<'ctx, G: CodeGenerator>(
|
||||||
/// This function handles 'end' **inclusively**.
|
/// This function handles 'end' **inclusively**.
|
||||||
/// Order of tuples `assign_idx` and `value_idx` is ('start', 'end', 'step').
|
/// Order of tuples `assign_idx` and `value_idx` is ('start', 'end', 'step').
|
||||||
/// Negative index should be handled before entering this function
|
/// Negative index should be handled before entering this function
|
||||||
pub fn list_slice_assignment<'ctx>(
|
pub fn list_slice_assignment<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
ty: BasicTypeEnum<'ctx>,
|
ty: BasicTypeEnum<'ctx>,
|
||||||
dest_arr: ListValue<'ctx>,
|
dest_arr: ListValue<'ctx>,
|
||||||
|
@ -338,7 +347,7 @@ pub fn list_slice_assignment<'ctx>(
|
||||||
|
|
||||||
let zero = int32.const_zero();
|
let zero = int32.const_zero();
|
||||||
let one = int32.const_int(1, false);
|
let one = int32.const_int(1, false);
|
||||||
let dest_arr_ptr = dest_arr.get_data().get_ptr(ctx);
|
let dest_arr_ptr = dest_arr.data().base_ptr(ctx, generator);
|
||||||
let dest_arr_ptr = ctx.builder.build_pointer_cast(
|
let dest_arr_ptr = ctx.builder.build_pointer_cast(
|
||||||
dest_arr_ptr,
|
dest_arr_ptr,
|
||||||
elem_ptr_type,
|
elem_ptr_type,
|
||||||
|
@ -346,7 +355,7 @@ pub fn list_slice_assignment<'ctx>(
|
||||||
).unwrap();
|
).unwrap();
|
||||||
let dest_len = dest_arr.load_size(ctx, Some("dest.len"));
|
let dest_len = dest_arr.load_size(ctx, Some("dest.len"));
|
||||||
let dest_len = ctx.builder.build_int_truncate_or_bit_cast(dest_len, int32, "srclen32").unwrap();
|
let dest_len = ctx.builder.build_int_truncate_or_bit_cast(dest_len, int32, "srclen32").unwrap();
|
||||||
let src_arr_ptr = src_arr.get_data().get_ptr(ctx);
|
let src_arr_ptr = src_arr.data().base_ptr(ctx, generator);
|
||||||
let src_arr_ptr = ctx.builder.build_pointer_cast(
|
let src_arr_ptr = ctx.builder.build_pointer_cast(
|
||||||
src_arr_ptr,
|
src_arr_ptr,
|
||||||
elem_ptr_type,
|
elem_ptr_type,
|
||||||
|
@ -468,8 +477,8 @@ pub fn list_slice_assignment<'ctx>(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates a call to `isinf` in IR. Returns an `i1` representing the result.
|
/// Generates a call to `isinf` in IR. Returns an `i1` representing the result.
|
||||||
pub fn call_isinf<'ctx>(
|
pub fn call_isinf<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
v: FloatValue<'ctx>,
|
v: FloatValue<'ctx>,
|
||||||
) -> IntValue<'ctx> {
|
) -> IntValue<'ctx> {
|
||||||
|
@ -489,8 +498,8 @@ pub fn call_isinf<'ctx>(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates a call to `isnan` in IR. Returns an `i1` representing the result.
|
/// Generates a call to `isnan` in IR. Returns an `i1` representing the result.
|
||||||
pub fn call_isnan<'ctx>(
|
pub fn call_isnan<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
v: FloatValue<'ctx>,
|
v: FloatValue<'ctx>,
|
||||||
) -> IntValue<'ctx> {
|
) -> IntValue<'ctx> {
|
||||||
|
@ -574,12 +583,14 @@ pub fn call_j0<'ctx>(
|
||||||
///
|
///
|
||||||
/// * `num_dims` - An [`IntValue`] containing the number of dimensions.
|
/// * `num_dims` - An [`IntValue`] containing the number of dimensions.
|
||||||
/// * `dims` - A [`PointerValue`] to an array containing the size of each dimension.
|
/// * `dims` - A [`PointerValue`] to an array containing the size of each dimension.
|
||||||
pub fn call_ndarray_calc_size<'ctx>(
|
pub fn call_ndarray_calc_size<'ctx, G, Dims>(
|
||||||
generator: &dyn CodeGenerator,
|
generator: &G,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
num_dims: IntValue<'ctx>,
|
dims: &Dims,
|
||||||
dims: PointerValue<'ctx>,
|
) -> IntValue<'ctx>
|
||||||
) -> IntValue<'ctx> {
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
|
Dims: ArrayLikeIndexer<'ctx>, {
|
||||||
let llvm_i64 = ctx.ctx.i64_type();
|
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);
|
||||||
|
|
||||||
|
@ -606,8 +617,8 @@ pub fn call_ndarray_calc_size<'ctx>(
|
||||||
.build_call(
|
.build_call(
|
||||||
ndarray_calc_size_fn,
|
ndarray_calc_size_fn,
|
||||||
&[
|
&[
|
||||||
dims.into(),
|
dims.base_ptr(ctx, generator).into(),
|
||||||
num_dims.into(),
|
dims.size(ctx, generator).into(),
|
||||||
],
|
],
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
@ -622,12 +633,12 @@ pub fn call_ndarray_calc_size<'ctx>(
|
||||||
/// * `index` - The index to compute the multidimensional index for.
|
/// * `index` - The index to compute the multidimensional index for.
|
||||||
/// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an
|
/// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an
|
||||||
/// `NDArray`.
|
/// `NDArray`.
|
||||||
pub fn call_ndarray_calc_nd_indices<'ctx>(
|
pub fn call_ndarray_calc_nd_indices<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
generator: &dyn CodeGenerator,
|
generator: &G,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
index: IntValue<'ctx>,
|
index: IntValue<'ctx>,
|
||||||
ndarray: NDArrayValue<'ctx>,
|
ndarray: NDArrayValue<'ctx>,
|
||||||
) -> PointerValue<'ctx> {
|
) -> ArraySliceValue<'ctx> {
|
||||||
let llvm_void = ctx.ctx.void_type();
|
let llvm_void = ctx.ctx.void_type();
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
@ -653,7 +664,7 @@ pub fn call_ndarray_calc_nd_indices<'ctx>(
|
||||||
});
|
});
|
||||||
|
|
||||||
let ndarray_num_dims = ndarray.load_ndims(ctx);
|
let ndarray_num_dims = ndarray.load_ndims(ctx);
|
||||||
let ndarray_dims = ndarray.get_dims();
|
let ndarray_dims = ndarray.dim_sizes();
|
||||||
|
|
||||||
let indices = ctx.builder.build_array_alloca(
|
let indices = ctx.builder.build_array_alloca(
|
||||||
llvm_usize,
|
llvm_usize,
|
||||||
|
@ -666,7 +677,7 @@ pub fn call_ndarray_calc_nd_indices<'ctx>(
|
||||||
ndarray_calc_nd_indices_fn,
|
ndarray_calc_nd_indices_fn,
|
||||||
&[
|
&[
|
||||||
index.into(),
|
index.into(),
|
||||||
ndarray_dims.get_ptr(ctx).into(),
|
ndarray_dims.base_ptr(ctx, generator).into(),
|
||||||
ndarray_num_dims.into(),
|
ndarray_num_dims.into(),
|
||||||
indices.into(),
|
indices.into(),
|
||||||
],
|
],
|
||||||
|
@ -674,16 +685,18 @@ pub fn call_ndarray_calc_nd_indices<'ctx>(
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
indices
|
ArraySliceValue::from_ptr_val(indices, ndarray_num_dims, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call_ndarray_flatten_index_impl<'ctx>(
|
fn call_ndarray_flatten_index_impl<'ctx, G, Indices>(
|
||||||
generator: &dyn CodeGenerator,
|
generator: &G,
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
ndarray: NDArrayValue<'ctx>,
|
ndarray: NDArrayValue<'ctx>,
|
||||||
indices: PointerValue<'ctx>,
|
indices: &Indices,
|
||||||
indices_size: IntValue<'ctx>,
|
) -> IntValue<'ctx>
|
||||||
) -> IntValue<'ctx> {
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
|
Indices: ArrayLikeIndexer<'ctx>, {
|
||||||
let llvm_i32 = ctx.ctx.i32_type();
|
let llvm_i32 = ctx.ctx.i32_type();
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
@ -691,14 +704,14 @@ fn call_ndarray_flatten_index_impl<'ctx>(
|
||||||
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
|
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
|
||||||
|
|
||||||
debug_assert_eq!(
|
debug_assert_eq!(
|
||||||
IntType::try_from(indices.get_type().get_element_type())
|
IntType::try_from(indices.element_type(ctx, generator))
|
||||||
.map(IntType::get_bit_width)
|
.map(IntType::get_bit_width)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
llvm_i32.get_bit_width(),
|
llvm_i32.get_bit_width(),
|
||||||
"Expected i32 value for argument `indices` to `call_ndarray_flatten_index_impl`"
|
"Expected i32 value for argument `indices` to `call_ndarray_flatten_index_impl`"
|
||||||
);
|
);
|
||||||
debug_assert_eq!(
|
debug_assert_eq!(
|
||||||
indices_size.get_type().get_bit_width(),
|
indices.size(ctx, generator).get_type().get_bit_width(),
|
||||||
llvm_usize.get_bit_width(),
|
llvm_usize.get_bit_width(),
|
||||||
"Expected usize integer value for argument `indices_size` to `call_ndarray_flatten_index_impl`"
|
"Expected usize integer value for argument `indices_size` to `call_ndarray_flatten_index_impl`"
|
||||||
);
|
);
|
||||||
|
@ -723,16 +736,16 @@ fn call_ndarray_flatten_index_impl<'ctx>(
|
||||||
});
|
});
|
||||||
|
|
||||||
let ndarray_num_dims = ndarray.load_ndims(ctx);
|
let ndarray_num_dims = ndarray.load_ndims(ctx);
|
||||||
let ndarray_dims = ndarray.get_dims();
|
let ndarray_dims = ndarray.dim_sizes();
|
||||||
|
|
||||||
let index = ctx.builder
|
let index = ctx.builder
|
||||||
.build_call(
|
.build_call(
|
||||||
ndarray_flatten_index_fn,
|
ndarray_flatten_index_fn,
|
||||||
&[
|
&[
|
||||||
ndarray_dims.get_ptr(ctx).into(),
|
ndarray_dims.base_ptr(ctx, generator).into(),
|
||||||
ndarray_num_dims.into(),
|
ndarray_num_dims.into(),
|
||||||
indices.into(),
|
indices.base_ptr(ctx, generator).into(),
|
||||||
indices_size.into(),
|
indices.size(ctx, generator).into(),
|
||||||
],
|
],
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
@ -750,21 +763,21 @@ fn call_ndarray_flatten_index_impl<'ctx>(
|
||||||
/// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an
|
/// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an
|
||||||
/// `NDArray`.
|
/// `NDArray`.
|
||||||
/// * `indices` - The multidimensional index to compute the flattened index for.
|
/// * `indices` - The multidimensional index to compute the flattened index for.
|
||||||
pub fn call_ndarray_flatten_index<'ctx>(
|
pub fn call_ndarray_flatten_index<'ctx, G, Index>(
|
||||||
generator: &dyn CodeGenerator,
|
generator: &mut G,
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
ndarray: NDArrayValue<'ctx>,
|
ndarray: NDArrayValue<'ctx>,
|
||||||
indices: ListValue<'ctx>,
|
indices: &Index,
|
||||||
) -> IntValue<'ctx> {
|
) -> IntValue<'ctx>
|
||||||
let indices_size = indices.load_size(ctx, None);
|
where
|
||||||
let indices_data = indices.get_data();
|
G: CodeGenerator + ?Sized,
|
||||||
|
Index: ArrayLikeIndexer<'ctx>, {
|
||||||
|
|
||||||
call_ndarray_flatten_index_impl(
|
call_ndarray_flatten_index_impl(
|
||||||
generator,
|
generator,
|
||||||
ctx,
|
ctx,
|
||||||
ndarray,
|
ndarray,
|
||||||
indices_data.get_ptr(ctx),
|
indices,
|
||||||
indices_size,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
/// Generates a call to `__nac3_ndarray_flatten_index`. Returns the flattened index for the
|
/// Generates a call to `__nac3_ndarray_flatten_index`. Returns the flattened index for the
|
||||||
|
@ -773,8 +786,8 @@ pub fn call_ndarray_flatten_index<'ctx>(
|
||||||
/// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an
|
/// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an
|
||||||
/// `NDArray`.
|
/// `NDArray`.
|
||||||
/// * `indices` - The multidimensional index to compute the flattened index for.
|
/// * `indices` - The multidimensional index to compute the flattened index for.
|
||||||
pub fn call_ndarray_flatten_index_const<'ctx>(
|
pub fn call_ndarray_flatten_index_const<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
ndarray: NDArrayValue<'ctx>,
|
ndarray: NDArrayValue<'ctx>,
|
||||||
indices: ArrayValue<'ctx>,
|
indices: ArrayValue<'ctx>,
|
||||||
|
@ -786,27 +799,165 @@ pub fn call_ndarray_flatten_index_const<'ctx>(
|
||||||
ctx,
|
ctx,
|
||||||
indices.get_type().get_element_type(),
|
indices.get_type().get_element_type(),
|
||||||
llvm_usize.const_int(indices_size as u64, false),
|
llvm_usize.const_int(indices_size as u64, false),
|
||||||
None
|
None,
|
||||||
).unwrap();
|
).unwrap();
|
||||||
for i in 0..indices_size {
|
for i in 0..indices_size {
|
||||||
let v = ctx.builder.build_extract_value(indices, i, "")
|
let v = ctx.builder.build_extract_value(indices, i, "")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_int_value();
|
.into_int_value();
|
||||||
let elem_ptr = unsafe {
|
|
||||||
ctx.builder.build_in_bounds_gep(
|
unsafe {
|
||||||
indices_alloca,
|
indices_alloca.set_unchecked(
|
||||||
&[ctx.ctx.i32_type().const_int(i as u64, false)],
|
ctx,
|
||||||
""
|
generator,
|
||||||
)
|
ctx.ctx.i32_type().const_int(i as u64, false),
|
||||||
}.unwrap();
|
v.into(),
|
||||||
ctx.builder.build_store(elem_ptr, v).unwrap();
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
call_ndarray_flatten_index_impl(
|
call_ndarray_flatten_index_impl(
|
||||||
generator,
|
generator,
|
||||||
ctx,
|
ctx,
|
||||||
ndarray,
|
ndarray,
|
||||||
indices_alloca,
|
&indices_alloca,
|
||||||
llvm_usize.const_int(indices_size as u64, false),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generates a call to `__nac3_ndarray_calc_broadcast`. Returns a tuple containing the number of
|
||||||
|
/// dimension and size of each dimension of the resultant `ndarray`.
|
||||||
|
pub fn call_ndarray_calc_broadcast<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
lhs: NDArrayValue<'ctx>,
|
||||||
|
rhs: NDArrayValue<'ctx>,
|
||||||
|
) -> (IntValue<'ctx>, PointerValue<'ctx>) {
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
|
||||||
|
|
||||||
|
let ndarray_calc_broadcast_fn_name = match llvm_usize.get_bit_width() {
|
||||||
|
32 => "__nac3_ndarray_calc_broadcast",
|
||||||
|
64 => "__nac3_ndarray_calc_broadcast64",
|
||||||
|
bw => unreachable!("Unsupported size type bit width: {}", bw)
|
||||||
|
};
|
||||||
|
let ndarray_calc_broadcast_fn = ctx.module.get_function(ndarray_calc_broadcast_fn_name).unwrap_or_else(|| {
|
||||||
|
let fn_type = llvm_usize.fn_type(
|
||||||
|
&[
|
||||||
|
llvm_pusize.into(),
|
||||||
|
llvm_usize.into(),
|
||||||
|
llvm_pusize.into(),
|
||||||
|
llvm_usize.into(),
|
||||||
|
llvm_pusize.into(),
|
||||||
|
],
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
|
||||||
|
ctx.module.add_function(ndarray_calc_broadcast_fn_name, fn_type, None)
|
||||||
|
});
|
||||||
|
|
||||||
|
let lhs_ndims = lhs.load_ndims(ctx);
|
||||||
|
let rhs_ndims = rhs.load_ndims(ctx);
|
||||||
|
let max_ndims = llvm_intrinsics::call_int_umax(ctx, lhs_ndims, rhs_ndims, None);
|
||||||
|
|
||||||
|
// TODO: Generate assertion checks for whether each dimension is compatible
|
||||||
|
// gen_for_callback_incrementing(
|
||||||
|
// generator,
|
||||||
|
// ctx,
|
||||||
|
// llvm_usize.const_zero(),
|
||||||
|
// (max_ndims, false),
|
||||||
|
// |generator, ctx, idx| {
|
||||||
|
// let lhs_dim_sz =
|
||||||
|
//
|
||||||
|
// let lhs_elem = lhs.get_dims().get(ctx, generator, idx, None);
|
||||||
|
// let rhs_elem = rhs.get_dims().get(ctx, generator, idx, None);
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// },
|
||||||
|
// llvm_usize.const_int(1, false),
|
||||||
|
// ).unwrap();
|
||||||
|
|
||||||
|
let lhs_dims = lhs.dim_sizes().base_ptr(ctx, generator);
|
||||||
|
let lhs_ndims = lhs.load_ndims(ctx);
|
||||||
|
let rhs_dims = rhs.dim_sizes().base_ptr(ctx, generator);
|
||||||
|
let rhs_ndims = rhs.load_ndims(ctx);
|
||||||
|
let out_dims = ctx.builder.build_array_alloca(llvm_usize, max_ndims, "").unwrap();
|
||||||
|
|
||||||
|
ctx.builder
|
||||||
|
.build_call(
|
||||||
|
ndarray_calc_broadcast_fn,
|
||||||
|
&[
|
||||||
|
lhs_dims.into(),
|
||||||
|
lhs_ndims.into(),
|
||||||
|
rhs_dims.into(),
|
||||||
|
rhs_ndims.into(),
|
||||||
|
out_dims.into(),
|
||||||
|
],
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
(max_ndims, out_dims)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates a call to `__nac3_ndarray_calc_broadcast_idx`. Returns an [`ArrayAllocaValue`]
|
||||||
|
/// containing the indices used for accessing `array` corresponding to the `broadcast_idx`.
|
||||||
|
pub fn call_ndarray_calc_broadcast_index<'ctx, G: CodeGenerator + ?Sized, BroadcastIdx: UntypedArrayLikeAccessor<'ctx>>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
array: NDArrayValue<'ctx>,
|
||||||
|
broadcast_idx: &BroadcastIdx,
|
||||||
|
) -> ArraySliceValue<'ctx> {
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
|
||||||
|
|
||||||
|
let ndarray_calc_broadcast_fn_name = match llvm_usize.get_bit_width() {
|
||||||
|
32 => "__nac3_ndarray_calc_broadcast_idx",
|
||||||
|
64 => "__nac3_ndarray_calc_broadcast_idx64",
|
||||||
|
bw => unreachable!("Unsupported size type bit width: {}", bw)
|
||||||
|
};
|
||||||
|
let ndarray_calc_broadcast_fn = ctx.module.get_function(ndarray_calc_broadcast_fn_name).unwrap_or_else(|| {
|
||||||
|
let fn_type = llvm_usize.fn_type(
|
||||||
|
&[
|
||||||
|
llvm_pusize.into(),
|
||||||
|
llvm_usize.into(),
|
||||||
|
llvm_pusize.into(),
|
||||||
|
llvm_usize.into(),
|
||||||
|
],
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
|
||||||
|
ctx.module.add_function(ndarray_calc_broadcast_fn_name, fn_type, None)
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO: Assertions
|
||||||
|
|
||||||
|
let broadcast_size = broadcast_idx.size(ctx, generator);
|
||||||
|
let out_idx = ctx.builder.build_array_alloca(llvm_usize, broadcast_size, "").unwrap();
|
||||||
|
let out_idx = ArraySliceValue::from_ptr_val(out_idx, broadcast_size, None);
|
||||||
|
|
||||||
|
let array_dims = array.dim_sizes().base_ptr(ctx, generator);
|
||||||
|
let array_ndims = array.load_ndims(ctx);
|
||||||
|
let broadcast_idx_ptr = unsafe {
|
||||||
|
broadcast_idx.ptr_offset_unchecked(
|
||||||
|
ctx,
|
||||||
|
generator,
|
||||||
|
llvm_usize.const_zero(),
|
||||||
|
None
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.builder
|
||||||
|
.build_call(
|
||||||
|
ndarray_calc_broadcast_fn,
|
||||||
|
&[
|
||||||
|
array_dims.into(),
|
||||||
|
array_ndims.into(),
|
||||||
|
broadcast_idx_ptr.into(),
|
||||||
|
out_idx.base_ptr(ctx, generator).into(),
|
||||||
|
],
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
out_idx
|
||||||
|
}
|
|
@ -2,7 +2,7 @@ use crate::{
|
||||||
symbol_resolver::{StaticValue, SymbolResolver},
|
symbol_resolver::{StaticValue, SymbolResolver},
|
||||||
toplevel::{
|
toplevel::{
|
||||||
helper::PRIMITIVE_DEF_IDS,
|
helper::PRIMITIVE_DEF_IDS,
|
||||||
numpy::unpack_ndarray_tvars,
|
numpy::unpack_ndarray_var_tys,
|
||||||
TopLevelContext,
|
TopLevelContext,
|
||||||
TopLevelDef,
|
TopLevelDef,
|
||||||
},
|
},
|
||||||
|
@ -45,6 +45,7 @@ pub mod expr;
|
||||||
mod generator;
|
mod generator;
|
||||||
pub mod irrt;
|
pub mod irrt;
|
||||||
pub mod llvm_intrinsics;
|
pub mod llvm_intrinsics;
|
||||||
|
pub mod numpy;
|
||||||
pub mod stmt;
|
pub mod stmt;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -415,10 +416,10 @@ pub struct CodeGenTask {
|
||||||
/// This function is used to obtain the in-memory representation of `ty`, e.g. a `bool` variable
|
/// This function is used to obtain the in-memory representation of `ty`, e.g. a `bool` variable
|
||||||
/// would be represented by an `i8`.
|
/// would be represented by an `i8`.
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
fn get_llvm_type<'ctx>(
|
fn get_llvm_type<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
ctx: &'ctx Context,
|
ctx: &'ctx Context,
|
||||||
module: &Module<'ctx>,
|
module: &Module<'ctx>,
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
unifier: &mut Unifier,
|
unifier: &mut Unifier,
|
||||||
top_level: &TopLevelContext,
|
top_level: &TopLevelContext,
|
||||||
type_cache: &mut HashMap<Type, BasicTypeEnum<'ctx>>,
|
type_cache: &mut HashMap<Type, BasicTypeEnum<'ctx>>,
|
||||||
|
@ -450,7 +451,7 @@ fn get_llvm_type<'ctx>(
|
||||||
|
|
||||||
TObj { obj_id, .. } if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
TObj { obj_id, .. } if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
||||||
let llvm_usize = generator.get_size_type(ctx);
|
let llvm_usize = generator.get_size_type(ctx);
|
||||||
let (dtype, _) = unpack_ndarray_tvars(unifier, ty);
|
let (dtype, _) = unpack_ndarray_var_tys(unifier, ty);
|
||||||
let element_type = get_llvm_type(
|
let element_type = get_llvm_type(
|
||||||
ctx,
|
ctx,
|
||||||
module,
|
module,
|
||||||
|
@ -553,10 +554,10 @@ fn get_llvm_type<'ctx>(
|
||||||
/// be byte-aligned for the variable to be addressable in memory, whereas there is no such
|
/// be byte-aligned for the variable to be addressable in memory, whereas there is no such
|
||||||
/// restriction for ABI representations.
|
/// restriction for ABI representations.
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
fn get_llvm_abi_type<'ctx>(
|
fn get_llvm_abi_type<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
ctx: &'ctx Context,
|
ctx: &'ctx Context,
|
||||||
module: &Module<'ctx>,
|
module: &Module<'ctx>,
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
unifier: &mut Unifier,
|
unifier: &mut Unifier,
|
||||||
top_level: &TopLevelContext,
|
top_level: &TopLevelContext,
|
||||||
type_cache: &mut HashMap<Type, BasicTypeEnum<'ctx>>,
|
type_cache: &mut HashMap<Type, BasicTypeEnum<'ctx>>,
|
||||||
|
|
|
@ -0,0 +1,928 @@
|
||||||
|
use inkwell::{
|
||||||
|
IntPredicate,
|
||||||
|
types::BasicType,
|
||||||
|
values::{AggregateValueEnum, ArrayValue, BasicValueEnum, IntValue, PointerValue}
|
||||||
|
};
|
||||||
|
use nac3parser::ast::StrRef;
|
||||||
|
use crate::{
|
||||||
|
codegen::{
|
||||||
|
classes::{
|
||||||
|
ArrayLikeIndexer,
|
||||||
|
ArraySliceValue,
|
||||||
|
ArrayLikeValue,
|
||||||
|
ListValue,
|
||||||
|
NDArrayValue,
|
||||||
|
TypedArrayLikeAccessor,
|
||||||
|
UntypedArrayLikeAccessor,
|
||||||
|
},
|
||||||
|
CodeGenContext,
|
||||||
|
CodeGenerator,
|
||||||
|
irrt::{
|
||||||
|
call_ndarray_calc_broadcast,
|
||||||
|
call_ndarray_calc_nd_indices,
|
||||||
|
call_ndarray_calc_size,
|
||||||
|
},
|
||||||
|
llvm_intrinsics::call_memcpy_generic,
|
||||||
|
stmt::gen_for_callback_incrementing,
|
||||||
|
},
|
||||||
|
symbol_resolver::ValueEnum,
|
||||||
|
toplevel::{
|
||||||
|
DefinitionId,
|
||||||
|
numpy::{make_ndarray_ty, unpack_ndarray_var_tys},
|
||||||
|
},
|
||||||
|
typecheck::typedef::{FunSignature, Type},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Creates an `NDArray` instance from a dynamic shape.
|
||||||
|
///
|
||||||
|
/// * `elem_ty` - The element type of the `NDArray`.
|
||||||
|
/// * `shape` - The shape of the `NDArray`.
|
||||||
|
/// * `shape_len_fn` - A function that retrieves the number of dimensions from `shape`.
|
||||||
|
/// * `shape_data_fn` - A function that retrieves the size of a dimension from `shape`.
|
||||||
|
fn create_ndarray_dyn_shape<'ctx, 'a, G, V, LenFn, DataFn>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, 'a>,
|
||||||
|
elem_ty: Type,
|
||||||
|
shape: &V,
|
||||||
|
shape_len_fn: LenFn,
|
||||||
|
shape_data_fn: DataFn,
|
||||||
|
) -> Result<NDArrayValue<'ctx>, String>
|
||||||
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
|
LenFn: Fn(&mut G, &mut CodeGenContext<'ctx, 'a>, &V) -> Result<IntValue<'ctx>, String>,
|
||||||
|
DataFn: Fn(&mut G, &mut CodeGenContext<'ctx, 'a>, &V, IntValue<'ctx>) -> Result<IntValue<'ctx>, String>,
|
||||||
|
{
|
||||||
|
let ndarray_ty = make_ndarray_ty(&mut ctx.unifier, &ctx.primitives, Some(elem_ty), None);
|
||||||
|
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
let llvm_pndarray_t = ctx.get_llvm_type(generator, ndarray_ty).into_pointer_type();
|
||||||
|
let llvm_ndarray_t = llvm_pndarray_t.get_element_type().into_struct_type();
|
||||||
|
let llvm_ndarray_data_t = ctx.get_llvm_type(generator, elem_ty).as_basic_type_enum();
|
||||||
|
assert!(llvm_ndarray_data_t.is_sized());
|
||||||
|
|
||||||
|
// Assert that all dimensions are non-negative
|
||||||
|
let shape_len = shape_len_fn(generator, ctx, shape)?;
|
||||||
|
gen_for_callback_incrementing(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
llvm_usize.const_zero(),
|
||||||
|
(shape_len, false),
|
||||||
|
|generator, ctx, i| {
|
||||||
|
let shape_dim = shape_data_fn(generator, ctx, shape, i)?;
|
||||||
|
debug_assert!(shape_dim.get_type().get_bit_width() <= llvm_usize.get_bit_width());
|
||||||
|
|
||||||
|
let shape_dim_gez = ctx.builder
|
||||||
|
.build_int_compare(IntPredicate::SGE, shape_dim, shape_dim.get_type().const_zero(), "")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
ctx.make_assert(
|
||||||
|
generator,
|
||||||
|
shape_dim_gez,
|
||||||
|
"0:ValueError",
|
||||||
|
"negative dimensions not supported",
|
||||||
|
[None, None, None],
|
||||||
|
ctx.current_loc,
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
llvm_usize.const_int(1, false),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let ndarray = generator.gen_var_alloc(
|
||||||
|
ctx,
|
||||||
|
llvm_ndarray_t.into(),
|
||||||
|
None,
|
||||||
|
)?;
|
||||||
|
let ndarray = NDArrayValue::from_ptr_val(ndarray, llvm_usize, None);
|
||||||
|
|
||||||
|
let num_dims = shape_len_fn(generator, ctx, shape)?;
|
||||||
|
ndarray.store_ndims(ctx, generator, num_dims);
|
||||||
|
|
||||||
|
let ndarray_num_dims = ndarray.load_ndims(ctx);
|
||||||
|
ndarray.create_dim_sizes(ctx, llvm_usize, ndarray_num_dims);
|
||||||
|
|
||||||
|
// Copy the dimension sizes from shape to ndarray.dims
|
||||||
|
let shape_len = shape_len_fn(generator, ctx, shape)?;
|
||||||
|
gen_for_callback_incrementing(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
llvm_usize.const_zero(),
|
||||||
|
(shape_len, false),
|
||||||
|
|generator, ctx, i| {
|
||||||
|
let shape_dim = shape_data_fn(generator, ctx, shape, i)?;
|
||||||
|
debug_assert!(shape_dim.get_type().get_bit_width() <= llvm_usize.get_bit_width());
|
||||||
|
let shape_dim = ctx.builder
|
||||||
|
.build_int_z_extend(shape_dim, llvm_usize, "")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let ndarray_pdim = unsafe {
|
||||||
|
ndarray.dim_sizes().ptr_offset_unchecked(ctx, generator, i, None)
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.builder.build_store(ndarray_pdim, shape_dim).unwrap();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
llvm_usize.const_int(1, false),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let ndarray_num_elems = call_ndarray_calc_size(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
&ndarray.dim_sizes().as_slice_value(ctx, generator),
|
||||||
|
);
|
||||||
|
ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems);
|
||||||
|
|
||||||
|
Ok(ndarray)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates an `NDArray` instance from a constant shape.
|
||||||
|
///
|
||||||
|
/// * `elem_ty` - The element type of the `NDArray`.
|
||||||
|
/// * `shape` - The shape of the `NDArray`, represented as an LLVM [`ArrayValue`].
|
||||||
|
fn create_ndarray_const_shape<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_ty: Type,
|
||||||
|
shape: ArrayValue<'ctx>
|
||||||
|
) -> Result<NDArrayValue<'ctx>, String> {
|
||||||
|
let ndarray_ty = make_ndarray_ty(&mut ctx.unifier, &ctx.primitives, Some(elem_ty), None);
|
||||||
|
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
let llvm_pndarray_t = ctx.get_llvm_type(generator, ndarray_ty).into_pointer_type();
|
||||||
|
let llvm_ndarray_t = llvm_pndarray_t.get_element_type().into_struct_type();
|
||||||
|
let llvm_ndarray_data_t = ctx.get_llvm_type(generator, elem_ty).as_basic_type_enum();
|
||||||
|
assert!(llvm_ndarray_data_t.is_sized());
|
||||||
|
|
||||||
|
for i in 0..shape.get_type().len() {
|
||||||
|
let shape_dim = ctx.builder
|
||||||
|
.build_extract_value(shape, i, "")
|
||||||
|
.map(BasicValueEnum::into_int_value)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let shape_dim_gez = ctx.builder
|
||||||
|
.build_int_compare(IntPredicate::SGE, shape_dim, llvm_usize.const_zero(), "")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
ctx.make_assert(
|
||||||
|
generator,
|
||||||
|
shape_dim_gez,
|
||||||
|
"0:ValueError",
|
||||||
|
"negative dimensions not supported",
|
||||||
|
[None, None, None],
|
||||||
|
ctx.current_loc,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let ndarray = generator.gen_var_alloc(
|
||||||
|
ctx,
|
||||||
|
llvm_ndarray_t.into(),
|
||||||
|
None,
|
||||||
|
)?;
|
||||||
|
let ndarray = NDArrayValue::from_ptr_val(ndarray, llvm_usize, None);
|
||||||
|
|
||||||
|
let num_dims = llvm_usize.const_int(shape.get_type().len() as u64, false);
|
||||||
|
ndarray.store_ndims(ctx, generator, num_dims);
|
||||||
|
|
||||||
|
let ndarray_num_dims = ndarray.load_ndims(ctx);
|
||||||
|
ndarray.create_dim_sizes(ctx, llvm_usize, ndarray_num_dims);
|
||||||
|
|
||||||
|
for i in 0..shape.get_type().len() {
|
||||||
|
let ndarray_dim = ndarray
|
||||||
|
.dim_sizes()
|
||||||
|
.ptr_offset(ctx, generator, llvm_usize.const_int(i as u64, true), None);
|
||||||
|
let shape_dim = ctx.builder.build_extract_value(shape, i, "")
|
||||||
|
.map(BasicValueEnum::into_int_value)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
ctx.builder.build_store(ndarray_dim, shape_dim).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
let ndarray_num_elems = call_ndarray_calc_size(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
&ndarray.dim_sizes().as_slice_value(ctx, generator),
|
||||||
|
);
|
||||||
|
ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems);
|
||||||
|
|
||||||
|
Ok(ndarray)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ndarray_zero_value<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_ty: Type,
|
||||||
|
) -> BasicValueEnum<'ctx> {
|
||||||
|
if [ctx.primitives.int32, ctx.primitives.uint32].iter().any(|ty| ctx.unifier.unioned(elem_ty, *ty)) {
|
||||||
|
ctx.ctx.i32_type().const_zero().into()
|
||||||
|
} else if [ctx.primitives.int64, ctx.primitives.uint64].iter().any(|ty| ctx.unifier.unioned(elem_ty, *ty)) {
|
||||||
|
ctx.ctx.i64_type().const_zero().into()
|
||||||
|
} else if ctx.unifier.unioned(elem_ty, ctx.primitives.float) {
|
||||||
|
ctx.ctx.f64_type().const_zero().into()
|
||||||
|
} else if ctx.unifier.unioned(elem_ty, ctx.primitives.bool) {
|
||||||
|
ctx.ctx.bool_type().const_zero().into()
|
||||||
|
} else if ctx.unifier.unioned(elem_ty, ctx.primitives.str) {
|
||||||
|
ctx.gen_string(generator, "")
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ndarray_one_value<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_ty: Type,
|
||||||
|
) -> BasicValueEnum<'ctx> {
|
||||||
|
if [ctx.primitives.int32, ctx.primitives.uint32].iter().any(|ty| ctx.unifier.unioned(elem_ty, *ty)) {
|
||||||
|
let is_signed = ctx.unifier.unioned(elem_ty, ctx.primitives.int32);
|
||||||
|
ctx.ctx.i32_type().const_int(1, is_signed).into()
|
||||||
|
} else if [ctx.primitives.int64, ctx.primitives.uint64].iter().any(|ty| ctx.unifier.unioned(elem_ty, *ty)) {
|
||||||
|
let is_signed = ctx.unifier.unioned(elem_ty, ctx.primitives.int64);
|
||||||
|
ctx.ctx.i64_type().const_int(1, is_signed).into()
|
||||||
|
} else if ctx.unifier.unioned(elem_ty, ctx.primitives.float) {
|
||||||
|
ctx.ctx.f64_type().const_float(1.0).into()
|
||||||
|
} else if ctx.unifier.unioned(elem_ty, ctx.primitives.bool) {
|
||||||
|
ctx.ctx.bool_type().const_int(1, false).into()
|
||||||
|
} else if ctx.unifier.unioned(elem_ty, ctx.primitives.str) {
|
||||||
|
ctx.gen_string(generator, "1")
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// LLVM-typed implementation for generating the implementation for constructing an `NDArray`.
|
||||||
|
///
|
||||||
|
/// * `elem_ty` - The element type of the `NDArray`.
|
||||||
|
/// * `shape` - The `shape` parameter used to construct the `NDArray`.
|
||||||
|
fn call_ndarray_empty_impl<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_ty: Type,
|
||||||
|
shape: ListValue<'ctx>,
|
||||||
|
) -> Result<NDArrayValue<'ctx>, String> {
|
||||||
|
create_ndarray_dyn_shape(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
elem_ty,
|
||||||
|
&shape,
|
||||||
|
|_, ctx, shape| {
|
||||||
|
Ok(shape.load_size(ctx, None))
|
||||||
|
},
|
||||||
|
|generator, ctx, shape, idx| {
|
||||||
|
Ok(shape.data().get(ctx, generator, idx, None).into_int_value())
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates LLVM IR for populating the entire `NDArray` using a lambda with its flattened index as
|
||||||
|
/// its input.
|
||||||
|
fn ndarray_fill_flattened<'ctx, 'a, G, ValueFn>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, 'a>,
|
||||||
|
ndarray: NDArrayValue<'ctx>,
|
||||||
|
value_fn: ValueFn,
|
||||||
|
) -> Result<(), String>
|
||||||
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
|
ValueFn: Fn(&mut G, &mut CodeGenContext<'ctx, 'a>, IntValue<'ctx>) -> Result<BasicValueEnum<'ctx>, String>,
|
||||||
|
{
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
let ndarray_num_elems = call_ndarray_calc_size(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
&ndarray.dim_sizes().as_slice_value(ctx, generator),
|
||||||
|
);
|
||||||
|
|
||||||
|
gen_for_callback_incrementing(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
llvm_usize.const_zero(),
|
||||||
|
(ndarray_num_elems, false),
|
||||||
|
|generator, ctx, i| {
|
||||||
|
let elem = unsafe {
|
||||||
|
ndarray.data().ptr_offset_unchecked(ctx, generator, i, None)
|
||||||
|
};
|
||||||
|
|
||||||
|
let value = value_fn(generator, ctx, i)?;
|
||||||
|
ctx.builder.build_store(elem, value).unwrap();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
llvm_usize.const_int(1, false),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates LLVM IR for populating the entire `NDArray` using a lambda with the dimension-indices
|
||||||
|
/// as its input.
|
||||||
|
fn ndarray_fill_indexed<'ctx, G, ValueFn>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
ndarray: NDArrayValue<'ctx>,
|
||||||
|
value_fn: ValueFn,
|
||||||
|
) -> Result<(), String>
|
||||||
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
|
ValueFn: Fn(&mut G, &mut CodeGenContext<'ctx, '_>, ArraySliceValue<'ctx>) -> Result<BasicValueEnum<'ctx>, String>,
|
||||||
|
{
|
||||||
|
ndarray_fill_flattened(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
ndarray,
|
||||||
|
|generator, ctx, idx| {
|
||||||
|
let indices = call_ndarray_calc_nd_indices(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
idx,
|
||||||
|
ndarray,
|
||||||
|
);
|
||||||
|
|
||||||
|
value_fn(generator, ctx, indices)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates the LLVM IR for populating the entire `NDArray` using a lambda with the same-indexed
|
||||||
|
/// element from two other `NDArray` as its input.
|
||||||
|
fn ndarray_broadcast_fill_flattened<'ctx, G, ValueFn>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_ty: Type,
|
||||||
|
res: NDArrayValue<'ctx>,
|
||||||
|
lhs: NDArrayValue<'ctx>,
|
||||||
|
rhs: NDArrayValue<'ctx>,
|
||||||
|
value_fn: ValueFn,
|
||||||
|
) -> Result<NDArrayValue<'ctx>, String>
|
||||||
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
|
ValueFn: Fn(&mut G, &mut CodeGenContext<'ctx, '_>, Type, (BasicValueEnum<'ctx>, BasicValueEnum<'ctx>)) -> Result<BasicValueEnum<'ctx>, String>,
|
||||||
|
{
|
||||||
|
ndarray_fill_flattened(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
res,
|
||||||
|
|generator, ctx, idx| {
|
||||||
|
let elem = unsafe {
|
||||||
|
(
|
||||||
|
lhs.data().get_unchecked(ctx, generator, idx, None),
|
||||||
|
rhs.data().get_unchecked(ctx, generator, idx, None),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
debug_assert_eq!(elem.0.get_type(), elem.1.get_type());
|
||||||
|
|
||||||
|
value_fn(generator, ctx, elem_ty, elem)
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// LLVM-typed implementation for generating the implementation for `ndarray.zeros`.
|
||||||
|
///
|
||||||
|
/// * `elem_ty` - The element type of the `NDArray`.
|
||||||
|
/// * `shape` - The `shape` parameter used to construct the `NDArray`.
|
||||||
|
fn call_ndarray_zeros_impl<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_ty: Type,
|
||||||
|
shape: ListValue<'ctx>,
|
||||||
|
) -> Result<NDArrayValue<'ctx>, String> {
|
||||||
|
let supported_types = [
|
||||||
|
ctx.primitives.int32,
|
||||||
|
ctx.primitives.int64,
|
||||||
|
ctx.primitives.uint32,
|
||||||
|
ctx.primitives.uint64,
|
||||||
|
ctx.primitives.float,
|
||||||
|
ctx.primitives.bool,
|
||||||
|
ctx.primitives.str,
|
||||||
|
];
|
||||||
|
assert!(supported_types.iter().any(|supported_ty| ctx.unifier.unioned(*supported_ty, elem_ty)));
|
||||||
|
|
||||||
|
let ndarray = call_ndarray_empty_impl(generator, ctx, elem_ty, shape)?;
|
||||||
|
ndarray_fill_flattened(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
ndarray,
|
||||||
|
|generator, ctx, _| {
|
||||||
|
let value = ndarray_zero_value(generator, ctx, elem_ty);
|
||||||
|
|
||||||
|
Ok(value)
|
||||||
|
}
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(ndarray)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// LLVM-typed implementation for generating the implementation for `ndarray.ones`.
|
||||||
|
///
|
||||||
|
/// * `elem_ty` - The element type of the `NDArray`.
|
||||||
|
/// * `shape` - The `shape` parameter used to construct the `NDArray`.
|
||||||
|
fn call_ndarray_ones_impl<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_ty: Type,
|
||||||
|
shape: ListValue<'ctx>,
|
||||||
|
) -> Result<NDArrayValue<'ctx>, String> {
|
||||||
|
let supported_types = [
|
||||||
|
ctx.primitives.int32,
|
||||||
|
ctx.primitives.int64,
|
||||||
|
ctx.primitives.uint32,
|
||||||
|
ctx.primitives.uint64,
|
||||||
|
ctx.primitives.float,
|
||||||
|
ctx.primitives.bool,
|
||||||
|
ctx.primitives.str,
|
||||||
|
];
|
||||||
|
assert!(supported_types.iter().any(|supported_ty| ctx.unifier.unioned(*supported_ty, elem_ty)));
|
||||||
|
|
||||||
|
let ndarray = call_ndarray_empty_impl(generator, ctx, elem_ty, shape)?;
|
||||||
|
ndarray_fill_flattened(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
ndarray,
|
||||||
|
|generator, ctx, _| {
|
||||||
|
let value = ndarray_one_value(generator, ctx, elem_ty);
|
||||||
|
|
||||||
|
Ok(value)
|
||||||
|
}
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(ndarray)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// LLVM-typed implementation for generating the implementation for `ndarray.full`.
|
||||||
|
///
|
||||||
|
/// * `elem_ty` - The element type of the `NDArray`.
|
||||||
|
/// * `shape` - The `shape` parameter used to construct the `NDArray`.
|
||||||
|
fn call_ndarray_full_impl<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_ty: Type,
|
||||||
|
shape: ListValue<'ctx>,
|
||||||
|
fill_value: BasicValueEnum<'ctx>,
|
||||||
|
) -> Result<NDArrayValue<'ctx>, String> {
|
||||||
|
let ndarray = call_ndarray_empty_impl(generator, ctx, elem_ty, shape)?;
|
||||||
|
ndarray_fill_flattened(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
ndarray,
|
||||||
|
|generator, ctx, _| {
|
||||||
|
let value = if fill_value.is_pointer_value() {
|
||||||
|
let llvm_i1 = ctx.ctx.bool_type();
|
||||||
|
|
||||||
|
let copy = generator.gen_var_alloc(ctx, fill_value.get_type(), None)?;
|
||||||
|
|
||||||
|
call_memcpy_generic(
|
||||||
|
ctx,
|
||||||
|
copy,
|
||||||
|
fill_value.into_pointer_value(),
|
||||||
|
fill_value.get_type().size_of().map(Into::into).unwrap(),
|
||||||
|
llvm_i1.const_zero(),
|
||||||
|
);
|
||||||
|
|
||||||
|
copy.into()
|
||||||
|
} else if fill_value.is_int_value() || fill_value.is_float_value() {
|
||||||
|
fill_value
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(value)
|
||||||
|
}
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(ndarray)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// LLVM-typed implementation for generating the implementation for `ndarray.eye`.
|
||||||
|
///
|
||||||
|
/// * `elem_ty` - The element type of the `NDArray`.
|
||||||
|
fn call_ndarray_eye_impl<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_ty: Type,
|
||||||
|
nrows: IntValue<'ctx>,
|
||||||
|
ncols: IntValue<'ctx>,
|
||||||
|
offset: IntValue<'ctx>,
|
||||||
|
) -> Result<NDArrayValue<'ctx>, String> {
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
let llvm_usize_2 = llvm_usize.array_type(2);
|
||||||
|
|
||||||
|
let shape_addr = generator.gen_var_alloc(ctx, llvm_usize_2.into(), None)?;
|
||||||
|
|
||||||
|
let shape = ctx.builder.build_load(shape_addr, "")
|
||||||
|
.map(BasicValueEnum::into_array_value)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let nrows = ctx.builder.build_int_z_extend_or_bit_cast(nrows, llvm_usize, "").unwrap();
|
||||||
|
let shape = ctx.builder
|
||||||
|
.build_insert_value(shape, nrows, 0, "")
|
||||||
|
.map(AggregateValueEnum::into_array_value)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let ncols = ctx.builder.build_int_z_extend_or_bit_cast(ncols, llvm_usize, "").unwrap();
|
||||||
|
let shape = ctx.builder
|
||||||
|
.build_insert_value(shape, ncols, 1, "")
|
||||||
|
.map(AggregateValueEnum::into_array_value)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let ndarray = create_ndarray_const_shape(generator, ctx, elem_ty, shape)?;
|
||||||
|
|
||||||
|
ndarray_fill_indexed(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
ndarray,
|
||||||
|
|generator, ctx, indices| {
|
||||||
|
let (row, col) = unsafe {
|
||||||
|
(
|
||||||
|
indices.get_unchecked(ctx, generator, llvm_usize.const_int(0, false), None).into_int_value(),
|
||||||
|
indices.get_unchecked(ctx, generator, llvm_usize.const_int(1, false), None).into_int_value(),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
let col_with_offset = ctx.builder
|
||||||
|
.build_int_add(
|
||||||
|
col,
|
||||||
|
ctx.builder.build_int_s_extend_or_bit_cast(offset, llvm_usize, "").unwrap(),
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let is_on_diag = ctx.builder
|
||||||
|
.build_int_compare(IntPredicate::EQ, row, col_with_offset, "")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let zero = ndarray_zero_value(generator, ctx, elem_ty);
|
||||||
|
let one = ndarray_one_value(generator, ctx, elem_ty);
|
||||||
|
|
||||||
|
let value = ctx.builder.build_select(is_on_diag, one, zero, "").unwrap();
|
||||||
|
|
||||||
|
Ok(value)
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(ndarray)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// LLVM-typed implementation for generating the implementation for `ndarray.copy`.
|
||||||
|
///
|
||||||
|
/// * `elem_ty` - The element type of the `NDArray`.
|
||||||
|
fn ndarray_copy_impl<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_ty: Type,
|
||||||
|
this: NDArrayValue<'ctx>,
|
||||||
|
) -> Result<NDArrayValue<'ctx>, String> {
|
||||||
|
let llvm_i1 = ctx.ctx.bool_type();
|
||||||
|
|
||||||
|
let ndarray = create_ndarray_dyn_shape(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
elem_ty,
|
||||||
|
&this,
|
||||||
|
|_, ctx, shape| {
|
||||||
|
Ok(shape.load_ndims(ctx))
|
||||||
|
},
|
||||||
|
|generator, ctx, shape, idx| {
|
||||||
|
unsafe { Ok(shape.dim_sizes().get_typed_unchecked(ctx, generator, idx, None)) }
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let len = call_ndarray_calc_size(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
&ndarray.dim_sizes().as_slice_value(ctx, generator),
|
||||||
|
);
|
||||||
|
let sizeof_ty = ctx.get_llvm_type(generator, elem_ty);
|
||||||
|
let len_bytes = ctx.builder
|
||||||
|
.build_int_mul(
|
||||||
|
len,
|
||||||
|
sizeof_ty.size_of().unwrap(),
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
call_memcpy_generic(
|
||||||
|
ctx,
|
||||||
|
ndarray.data().base_ptr(ctx, generator),
|
||||||
|
this.data().base_ptr(ctx, generator),
|
||||||
|
len_bytes,
|
||||||
|
llvm_i1.const_zero(),
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(ndarray)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// LLVM-typed implementation for computing elementwise binary operations.
|
||||||
|
///
|
||||||
|
/// * `elem_ty` - The element type of the `NDArray`.
|
||||||
|
/// * `res` - The `ndarray` instance to write results into, or [`None`] if the result should be
|
||||||
|
/// written to a new `ndarray`.
|
||||||
|
/// * `value_fn` - Function mapping the two input elements into the result.
|
||||||
|
pub fn ndarray_elementwise_binop_impl<'ctx, G, ValueFn>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_ty: Type,
|
||||||
|
res: Option<NDArrayValue<'ctx>>,
|
||||||
|
this: NDArrayValue<'ctx>,
|
||||||
|
other: NDArrayValue<'ctx>,
|
||||||
|
value_fn: ValueFn,
|
||||||
|
) -> Result<NDArrayValue<'ctx>, String>
|
||||||
|
where
|
||||||
|
G: CodeGenerator,
|
||||||
|
ValueFn: Fn(&mut G, &mut CodeGenContext<'ctx, '_>, Type, (BasicValueEnum<'ctx>, BasicValueEnum<'ctx>)) -> Result<BasicValueEnum<'ctx>, String>,
|
||||||
|
{
|
||||||
|
let ndarray_dims = call_ndarray_calc_broadcast(generator, ctx, this, other);
|
||||||
|
let ndarray = res.unwrap_or_else(|| {
|
||||||
|
create_ndarray_dyn_shape(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
elem_ty,
|
||||||
|
&ndarray_dims,
|
||||||
|
|_, _, v| {
|
||||||
|
Ok(v.0)
|
||||||
|
},
|
||||||
|
|_, ctx, v, idx| {
|
||||||
|
unsafe {
|
||||||
|
let data_ptr = ctx.builder.build_in_bounds_gep(v.1, &[idx], "")
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
|
ctx.builder.build_load(data_ptr, "")
|
||||||
|
.map(BasicValueEnum::into_int_value)
|
||||||
|
.map_err(|e| e.to_string())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
).unwrap()
|
||||||
|
});
|
||||||
|
|
||||||
|
ndarray_broadcast_fill_flattened(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
elem_ty,
|
||||||
|
ndarray,
|
||||||
|
this,
|
||||||
|
other,
|
||||||
|
|generator, ctx, elem_ty, elems| {
|
||||||
|
value_fn(generator, ctx, elem_ty, elems)
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(ndarray)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates LLVM IR for `ndarray.empty`.
|
||||||
|
pub fn gen_ndarray_empty<'ctx>(
|
||||||
|
context: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
||||||
|
fun: (&FunSignature, DefinitionId),
|
||||||
|
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
||||||
|
generator: &mut dyn CodeGenerator,
|
||||||
|
) -> Result<PointerValue<'ctx>, String> {
|
||||||
|
assert!(obj.is_none());
|
||||||
|
assert_eq!(args.len(), 1);
|
||||||
|
|
||||||
|
let llvm_usize = generator.get_size_type(context.ctx);
|
||||||
|
let shape_ty = fun.0.args[0].ty;
|
||||||
|
let shape_arg = args[0].1.clone()
|
||||||
|
.to_basic_value_enum(context, generator, shape_ty)?;
|
||||||
|
|
||||||
|
call_ndarray_empty_impl(
|
||||||
|
generator,
|
||||||
|
context,
|
||||||
|
context.primitives.float,
|
||||||
|
ListValue::from_ptr_val(shape_arg.into_pointer_value(), llvm_usize, None),
|
||||||
|
).map(NDArrayValue::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates LLVM IR for `ndarray.zeros`.
|
||||||
|
pub fn gen_ndarray_zeros<'ctx>(
|
||||||
|
context: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
||||||
|
fun: (&FunSignature, DefinitionId),
|
||||||
|
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
||||||
|
generator: &mut dyn CodeGenerator,
|
||||||
|
) -> Result<PointerValue<'ctx>, String> {
|
||||||
|
assert!(obj.is_none());
|
||||||
|
assert_eq!(args.len(), 1);
|
||||||
|
|
||||||
|
let llvm_usize = generator.get_size_type(context.ctx);
|
||||||
|
let shape_ty = fun.0.args[0].ty;
|
||||||
|
let shape_arg = args[0].1.clone()
|
||||||
|
.to_basic_value_enum(context, generator, shape_ty)?;
|
||||||
|
|
||||||
|
call_ndarray_zeros_impl(
|
||||||
|
generator,
|
||||||
|
context,
|
||||||
|
context.primitives.float,
|
||||||
|
ListValue::from_ptr_val(shape_arg.into_pointer_value(), llvm_usize, None),
|
||||||
|
).map(NDArrayValue::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates LLVM IR for `ndarray.ones`.
|
||||||
|
pub fn gen_ndarray_ones<'ctx>(
|
||||||
|
context: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
||||||
|
fun: (&FunSignature, DefinitionId),
|
||||||
|
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
||||||
|
generator: &mut dyn CodeGenerator,
|
||||||
|
) -> Result<PointerValue<'ctx>, String> {
|
||||||
|
assert!(obj.is_none());
|
||||||
|
assert_eq!(args.len(), 1);
|
||||||
|
|
||||||
|
let llvm_usize = generator.get_size_type(context.ctx);
|
||||||
|
let shape_ty = fun.0.args[0].ty;
|
||||||
|
let shape_arg = args[0].1.clone()
|
||||||
|
.to_basic_value_enum(context, generator, shape_ty)?;
|
||||||
|
|
||||||
|
call_ndarray_ones_impl(
|
||||||
|
generator,
|
||||||
|
context,
|
||||||
|
context.primitives.float,
|
||||||
|
ListValue::from_ptr_val(shape_arg.into_pointer_value(), llvm_usize, None),
|
||||||
|
).map(NDArrayValue::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates LLVM IR for `ndarray.full`.
|
||||||
|
pub fn gen_ndarray_full<'ctx>(
|
||||||
|
context: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
||||||
|
fun: (&FunSignature, DefinitionId),
|
||||||
|
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
||||||
|
generator: &mut dyn CodeGenerator,
|
||||||
|
) -> Result<PointerValue<'ctx>, String> {
|
||||||
|
assert!(obj.is_none());
|
||||||
|
assert_eq!(args.len(), 2);
|
||||||
|
|
||||||
|
let llvm_usize = generator.get_size_type(context.ctx);
|
||||||
|
let shape_ty = fun.0.args[0].ty;
|
||||||
|
let shape_arg = args[0].1.clone()
|
||||||
|
.to_basic_value_enum(context, generator, shape_ty)?;
|
||||||
|
let fill_value_ty = fun.0.args[1].ty;
|
||||||
|
let fill_value_arg = args[1].1.clone()
|
||||||
|
.to_basic_value_enum(context, generator, fill_value_ty)?;
|
||||||
|
|
||||||
|
call_ndarray_full_impl(
|
||||||
|
generator,
|
||||||
|
context,
|
||||||
|
fill_value_ty,
|
||||||
|
ListValue::from_ptr_val(shape_arg.into_pointer_value(), llvm_usize, None),
|
||||||
|
fill_value_arg,
|
||||||
|
).map(NDArrayValue::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates LLVM IR for `ndarray.eye`.
|
||||||
|
pub fn gen_ndarray_eye<'ctx>(
|
||||||
|
context: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
||||||
|
fun: (&FunSignature, DefinitionId),
|
||||||
|
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
||||||
|
generator: &mut dyn CodeGenerator,
|
||||||
|
) -> Result<PointerValue<'ctx>, String> {
|
||||||
|
assert!(obj.is_none());
|
||||||
|
assert!(matches!(args.len(), 1..=3));
|
||||||
|
|
||||||
|
let nrows_ty = fun.0.args[0].ty;
|
||||||
|
let nrows_arg = args[0].1.clone()
|
||||||
|
.to_basic_value_enum(context, generator, nrows_ty)?;
|
||||||
|
|
||||||
|
let ncols_ty = fun.0.args[1].ty;
|
||||||
|
let ncols_arg = args.iter()
|
||||||
|
.find(|arg| arg.0.is_some_and(|name| name == fun.0.args[1].name))
|
||||||
|
.map(|arg| arg.1.clone().to_basic_value_enum(context, generator, ncols_ty))
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
args[0].1.clone().to_basic_value_enum(context, generator, nrows_ty)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let offset_ty = fun.0.args[2].ty;
|
||||||
|
let offset_arg = args.iter()
|
||||||
|
.find(|arg| arg.0.is_some_and(|name| name == fun.0.args[2].name))
|
||||||
|
.map(|arg| arg.1.clone().to_basic_value_enum(context, generator, offset_ty))
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
Ok(context.gen_symbol_val(
|
||||||
|
generator,
|
||||||
|
fun.0.args[2].default_value.as_ref().unwrap(),
|
||||||
|
offset_ty
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
call_ndarray_eye_impl(
|
||||||
|
generator,
|
||||||
|
context,
|
||||||
|
context.primitives.float,
|
||||||
|
nrows_arg.into_int_value(),
|
||||||
|
ncols_arg.into_int_value(),
|
||||||
|
offset_arg.into_int_value(),
|
||||||
|
).map(NDArrayValue::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates LLVM IR for `ndarray.identity`.
|
||||||
|
pub fn gen_ndarray_identity<'ctx>(
|
||||||
|
context: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
||||||
|
fun: (&FunSignature, DefinitionId),
|
||||||
|
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
||||||
|
generator: &mut dyn CodeGenerator,
|
||||||
|
) -> Result<PointerValue<'ctx>, String> {
|
||||||
|
assert!(obj.is_none());
|
||||||
|
assert_eq!(args.len(), 1);
|
||||||
|
|
||||||
|
let llvm_usize = generator.get_size_type(context.ctx);
|
||||||
|
|
||||||
|
let n_ty = fun.0.args[0].ty;
|
||||||
|
let n_arg = args[0].1.clone()
|
||||||
|
.to_basic_value_enum(context, generator, n_ty)?;
|
||||||
|
|
||||||
|
call_ndarray_eye_impl(
|
||||||
|
generator,
|
||||||
|
context,
|
||||||
|
context.primitives.float,
|
||||||
|
n_arg.into_int_value(),
|
||||||
|
n_arg.into_int_value(),
|
||||||
|
llvm_usize.const_zero(),
|
||||||
|
).map(NDArrayValue::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates LLVM IR for `ndarray.copy`.
|
||||||
|
pub fn gen_ndarray_copy<'ctx>(
|
||||||
|
context: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
||||||
|
_fun: (&FunSignature, DefinitionId),
|
||||||
|
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
||||||
|
generator: &mut dyn CodeGenerator,
|
||||||
|
) -> Result<PointerValue<'ctx>, String> {
|
||||||
|
assert!(obj.is_some());
|
||||||
|
assert!(args.is_empty());
|
||||||
|
|
||||||
|
let llvm_usize = generator.get_size_type(context.ctx);
|
||||||
|
|
||||||
|
let this_ty = obj.as_ref().unwrap().0;
|
||||||
|
let (this_elem_ty, _) = unpack_ndarray_var_tys(&mut context.unifier, this_ty);
|
||||||
|
let this_arg = obj
|
||||||
|
.as_ref()
|
||||||
|
.unwrap()
|
||||||
|
.1
|
||||||
|
.clone()
|
||||||
|
.to_basic_value_enum(context, generator, this_ty)?;
|
||||||
|
|
||||||
|
ndarray_copy_impl(
|
||||||
|
generator,
|
||||||
|
context,
|
||||||
|
this_elem_ty,
|
||||||
|
NDArrayValue::from_ptr_val(this_arg.into_pointer_value(), llvm_usize, None),
|
||||||
|
).map(NDArrayValue::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates LLVM IR for `ndarray.fill`.
|
||||||
|
pub fn gen_ndarray_fill<'ctx>(
|
||||||
|
context: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
||||||
|
fun: (&FunSignature, DefinitionId),
|
||||||
|
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
||||||
|
generator: &mut dyn CodeGenerator,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
assert!(obj.is_some());
|
||||||
|
assert_eq!(args.len(), 1);
|
||||||
|
|
||||||
|
let llvm_usize = generator.get_size_type(context.ctx);
|
||||||
|
|
||||||
|
let this_ty = obj.as_ref().unwrap().0;
|
||||||
|
let this_arg = obj.as_ref().unwrap().1.clone()
|
||||||
|
.to_basic_value_enum(context, generator, this_ty)?
|
||||||
|
.into_pointer_value();
|
||||||
|
let value_ty = fun.0.args[0].ty;
|
||||||
|
let value_arg = args[0].1.clone()
|
||||||
|
.to_basic_value_enum(context, generator, value_ty)?;
|
||||||
|
|
||||||
|
ndarray_fill_flattened(
|
||||||
|
generator,
|
||||||
|
context,
|
||||||
|
NDArrayValue::from_ptr_val(this_arg, llvm_usize, None),
|
||||||
|
|generator, ctx, _| {
|
||||||
|
let value = if value_arg.is_pointer_value() {
|
||||||
|
let llvm_i1 = ctx.ctx.bool_type();
|
||||||
|
|
||||||
|
let copy = generator.gen_var_alloc(ctx, value_arg.get_type(), None)?;
|
||||||
|
|
||||||
|
call_memcpy_generic(
|
||||||
|
ctx,
|
||||||
|
copy,
|
||||||
|
value_arg.into_pointer_value(),
|
||||||
|
value_arg.get_type().size_of().map(Into::into).unwrap(),
|
||||||
|
llvm_i1.const_zero(),
|
||||||
|
);
|
||||||
|
|
||||||
|
copy.into()
|
||||||
|
} else if value_arg.is_int_value() || value_arg.is_float_value() {
|
||||||
|
value_arg
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(value)
|
||||||
|
}
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -6,14 +6,14 @@ use super::{
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
codegen::{
|
codegen::{
|
||||||
classes::{ListValue, RangeValue},
|
classes::{ArrayLikeIndexer, ArraySliceValue, ListValue, RangeValue},
|
||||||
expr::gen_binop_expr,
|
expr::gen_binop_expr,
|
||||||
gen_in_range_check,
|
gen_in_range_check,
|
||||||
},
|
},
|
||||||
toplevel::{
|
toplevel::{
|
||||||
DefinitionId,
|
DefinitionId,
|
||||||
helper::PRIMITIVE_DEF_IDS,
|
helper::PRIMITIVE_DEF_IDS,
|
||||||
numpy::unpack_ndarray_tvars,
|
numpy::unpack_ndarray_var_tys,
|
||||||
TopLevelDef,
|
TopLevelDef,
|
||||||
},
|
},
|
||||||
typecheck::typedef::{FunSignature, Type, TypeEnum},
|
typecheck::typedef::{FunSignature, Type, TypeEnum},
|
||||||
|
@ -65,8 +65,8 @@ pub fn gen_array_var<'ctx, 'a, T: BasicType<'ctx>>(
|
||||||
ctx: &mut CodeGenContext<'ctx, 'a>,
|
ctx: &mut CodeGenContext<'ctx, 'a>,
|
||||||
ty: T,
|
ty: T,
|
||||||
size: IntValue<'ctx>,
|
size: IntValue<'ctx>,
|
||||||
name: Option<&str>,
|
name: Option<&'ctx str>,
|
||||||
) -> Result<PointerValue<'ctx>, String> {
|
) -> Result<ArraySliceValue<'ctx>, String> {
|
||||||
// Restore debug location
|
// Restore debug location
|
||||||
let di_loc = ctx.debug_info.0.create_debug_location(
|
let di_loc = ctx.debug_info.0.create_debug_location(
|
||||||
ctx.ctx,
|
ctx.ctx,
|
||||||
|
@ -84,6 +84,7 @@ pub fn gen_array_var<'ctx, 'a, T: BasicType<'ctx>>(
|
||||||
ctx.builder.set_current_debug_location(di_loc);
|
ctx.builder.set_current_debug_location(di_loc);
|
||||||
|
|
||||||
let ptr = ctx.builder.build_array_alloca(ty, size, name.unwrap_or("")).unwrap();
|
let ptr = ctx.builder.build_array_alloca(ty, size, name.unwrap_or("")).unwrap();
|
||||||
|
let ptr = ArraySliceValue::from_ptr_val(ptr, size, name);
|
||||||
|
|
||||||
ctx.builder.position_at_end(current);
|
ctx.builder.position_at_end(current);
|
||||||
ctx.builder.set_current_debug_location(di_loc);
|
ctx.builder.set_current_debug_location(di_loc);
|
||||||
|
@ -188,7 +189,7 @@ pub fn gen_store_target<'ctx, G: CodeGenerator>(
|
||||||
[Some(raw_index), Some(len), None],
|
[Some(raw_index), Some(len), None],
|
||||||
slice.location,
|
slice.location,
|
||||||
);
|
);
|
||||||
v.get_data().ptr_offset(ctx, generator, index, name)
|
v.data().ptr_offset(ctx, generator, index, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
TypeEnum::TObj { obj_id, .. } if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
||||||
|
@ -250,7 +251,7 @@ pub fn gen_assign<'ctx, G: CodeGenerator>(
|
||||||
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::TList { ty } => *ty,
|
||||||
TypeEnum::TObj { obj_id, .. } if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
||||||
unpack_ndarray_tvars(&mut ctx.unifier, target.custom.unwrap()).0
|
unpack_ndarray_var_tys(&mut ctx.unifier, target.custom.unwrap()).0
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
@ -478,8 +479,8 @@ pub fn gen_for<G: CodeGenerator>(
|
||||||
/// executing. The result value must be an `i1` indicating if the loop should continue.
|
/// executing. The result value must be an `i1` indicating if the loop should continue.
|
||||||
/// * `body` - A lambda containing IR statements within the loop body.
|
/// * `body` - A lambda containing IR statements within the loop body.
|
||||||
/// * `update` - A lambda containing IR statements updating loop variables.
|
/// * `update` - A lambda containing IR statements updating loop variables.
|
||||||
pub fn gen_for_callback<'ctx, 'a, I, InitFn, CondFn, BodyFn, UpdateFn>(
|
pub fn gen_for_callback<'ctx, 'a, G, I, InitFn, CondFn, BodyFn, UpdateFn>(
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
ctx: &mut CodeGenContext<'ctx, 'a>,
|
ctx: &mut CodeGenContext<'ctx, 'a>,
|
||||||
init: InitFn,
|
init: InitFn,
|
||||||
cond: CondFn,
|
cond: CondFn,
|
||||||
|
@ -487,11 +488,12 @@ pub fn gen_for_callback<'ctx, 'a, I, InitFn, CondFn, BodyFn, UpdateFn>(
|
||||||
update: UpdateFn,
|
update: UpdateFn,
|
||||||
) -> Result<(), String>
|
) -> Result<(), String>
|
||||||
where
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
I: Clone,
|
I: Clone,
|
||||||
InitFn: FnOnce(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>) -> Result<I, String>,
|
InitFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>) -> Result<I, String>,
|
||||||
CondFn: FnOnce(&mut dyn CodeGenerator, &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 dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, I) -> Result<(), String>,
|
BodyFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, I) -> Result<(), String>,
|
||||||
UpdateFn: FnOnce(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, I) -> Result<(), String>,
|
UpdateFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, I) -> Result<(), String>,
|
||||||
{
|
{
|
||||||
let current = ctx.builder.get_insert_block().and_then(BasicBlock::get_parent).unwrap();
|
let current = ctx.builder.get_insert_block().and_then(BasicBlock::get_parent).unwrap();
|
||||||
let init_bb = ctx.ctx.append_basic_block(current, "for.init");
|
let init_bb = ctx.ctx.append_basic_block(current, "for.init");
|
||||||
|
@ -536,6 +538,85 @@ pub fn gen_for_callback<'ctx, 'a, I, InitFn, CondFn, BodyFn, UpdateFn>(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generates a C-style monotonically-increasing `for` construct using lambdas, similar to the
|
||||||
|
/// following C code:
|
||||||
|
///
|
||||||
|
/// ```c
|
||||||
|
/// for (int x = init_val; x /* < or <= ; see `max_val` */ max_val; x += incr_val) {
|
||||||
|
/// body(x);
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// * `init_val` - The initial value of the loop variable. The type of this value will also be used
|
||||||
|
/// as the type of the loop variable.
|
||||||
|
/// * `max_val` - A tuple containing the maximum value of the loop variable, and whether the maximum
|
||||||
|
/// value should be treated as inclusive (as opposed to exclusive).
|
||||||
|
/// * `body` - A lambda containing IR statements within the loop body.
|
||||||
|
/// * `incr_val` - The value to increment the loop variable on each iteration.
|
||||||
|
pub fn gen_for_callback_incrementing<'ctx, 'a, G, BodyFn>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, 'a>,
|
||||||
|
init_val: IntValue<'ctx>,
|
||||||
|
max_val: (IntValue<'ctx>, bool),
|
||||||
|
body: BodyFn,
|
||||||
|
incr_val: IntValue<'ctx>,
|
||||||
|
) -> Result<(), String>
|
||||||
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
|
BodyFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, IntValue<'ctx>) -> Result<(), String>,
|
||||||
|
{
|
||||||
|
let init_val_t = init_val.get_type();
|
||||||
|
|
||||||
|
gen_for_callback(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
|generator, ctx| {
|
||||||
|
let i_addr = generator.gen_var_alloc(ctx, init_val_t.into(), None)?;
|
||||||
|
ctx.builder.build_store(i_addr, init_val).unwrap();
|
||||||
|
|
||||||
|
Ok(i_addr)
|
||||||
|
},
|
||||||
|
|_, ctx, i_addr| {
|
||||||
|
let cmp_op = if max_val.1 {
|
||||||
|
IntPredicate::ULE
|
||||||
|
} else {
|
||||||
|
IntPredicate::ULT
|
||||||
|
};
|
||||||
|
|
||||||
|
let i = ctx.builder
|
||||||
|
.build_load(i_addr, "")
|
||||||
|
.map(BasicValueEnum::into_int_value)
|
||||||
|
.unwrap();
|
||||||
|
let max_val = ctx.builder
|
||||||
|
.build_int_z_extend_or_bit_cast(max_val.0, init_val_t, "")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(ctx.builder.build_int_compare(cmp_op, i, max_val, "").unwrap())
|
||||||
|
},
|
||||||
|
|generator, ctx, i_addr| {
|
||||||
|
let i = ctx.builder
|
||||||
|
.build_load(i_addr, "")
|
||||||
|
.map(BasicValueEnum::into_int_value)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
body(generator, ctx, i)
|
||||||
|
},
|
||||||
|
|_, ctx, i_addr| {
|
||||||
|
let i = ctx.builder
|
||||||
|
.build_load(i_addr, "")
|
||||||
|
.map(BasicValueEnum::into_int_value)
|
||||||
|
.unwrap();
|
||||||
|
let incr_val = ctx.builder
|
||||||
|
.build_int_z_extend_or_bit_cast(incr_val, init_val_t, "")
|
||||||
|
.unwrap();
|
||||||
|
let i = ctx.builder.build_int_add(i, incr_val, "").unwrap();
|
||||||
|
ctx.builder.build_store(i_addr, i).unwrap();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// See [`CodeGenerator::gen_while`].
|
/// See [`CodeGenerator::gen_while`].
|
||||||
pub fn gen_while<G: CodeGenerator>(
|
pub fn gen_while<G: CodeGenerator>(
|
||||||
generator: &mut G,
|
generator: &mut G,
|
||||||
|
@ -701,8 +782,8 @@ pub fn final_proxy<'ctx>(
|
||||||
|
|
||||||
/// Inserts the declaration of the builtin function with the specified `symbol` name, and returns
|
/// Inserts the declaration of the builtin function with the specified `symbol` name, and returns
|
||||||
/// the function.
|
/// the function.
|
||||||
pub fn get_builtins<'ctx>(
|
pub fn get_builtins<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
symbol: &str,
|
symbol: &str,
|
||||||
) -> FunctionValue<'ctx> {
|
) -> FunctionValue<'ctx> {
|
||||||
|
@ -795,8 +876,8 @@ pub fn exn_constructor<'ctx>(
|
||||||
///
|
///
|
||||||
/// * `exception` - The exception thrown by the `raise` statement.
|
/// * `exception` - The exception thrown by the `raise` statement.
|
||||||
/// * `loc` - The location where the exception is raised from.
|
/// * `loc` - The location where the exception is raised from.
|
||||||
pub fn gen_raise<'ctx>(
|
pub fn gen_raise<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
generator: &mut dyn CodeGenerator,
|
generator: &mut G,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
exception: Option<&BasicValueEnum<'ctx>>,
|
exception: Option<&BasicValueEnum<'ctx>>,
|
||||||
loc: Location,
|
loc: Location,
|
||||||
|
|
|
@ -10,7 +10,7 @@ use crate::{
|
||||||
},
|
},
|
||||||
typecheck::{
|
typecheck::{
|
||||||
type_inferencer::{FunctionData, Inferencer, PrimitiveStore},
|
type_inferencer::{FunctionData, Inferencer, PrimitiveStore},
|
||||||
typedef::{FunSignature, FuncArg, Type, TypeEnum, Unifier},
|
typedef::{FunSignature, FuncArg, Type, TypeEnum, Unifier, VarMap},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
|
@ -25,7 +25,6 @@ use nac3parser::{
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use crate::typecheck::typedef::VarMap;
|
|
||||||
|
|
||||||
struct Resolver {
|
struct Resolver {
|
||||||
id_to_type: HashMap<StrRef, Type>,
|
id_to_type: HashMap<StrRef, Type>,
|
||||||
|
|
|
@ -5,11 +5,14 @@ use crate::{
|
||||||
expr::destructure_range,
|
expr::destructure_range,
|
||||||
irrt::*,
|
irrt::*,
|
||||||
llvm_intrinsics::*,
|
llvm_intrinsics::*,
|
||||||
|
numpy::*,
|
||||||
stmt::exn_constructor,
|
stmt::exn_constructor,
|
||||||
},
|
},
|
||||||
symbol_resolver::SymbolValue,
|
symbol_resolver::SymbolValue,
|
||||||
toplevel::helper::PRIMITIVE_DEF_IDS,
|
toplevel::{
|
||||||
toplevel::numpy::*,
|
helper::PRIMITIVE_DEF_IDS,
|
||||||
|
numpy::make_ndarray_ty,
|
||||||
|
},
|
||||||
typecheck::typedef::VarMap,
|
typecheck::typedef::VarMap,
|
||||||
};
|
};
|
||||||
use inkwell::{
|
use inkwell::{
|
||||||
|
@ -296,6 +299,8 @@ pub fn get_builtins(primitives: &mut (PrimitiveStore, Unifier)) -> BuiltinInfo {
|
||||||
Some("N".into()),
|
Some("N".into()),
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
|
let size_t = primitives.0.usize();
|
||||||
|
|
||||||
let var_map: VarMap = vec![(num_ty.1, num_ty.0)].into_iter().collect();
|
let var_map: VarMap = vec![(num_ty.1, num_ty.0)].into_iter().collect();
|
||||||
let exception_fields = vec![
|
let exception_fields = vec![
|
||||||
("__name__".into(), int32, true),
|
("__name__".into(), int32, true),
|
||||||
|
@ -342,8 +347,27 @@ pub fn get_builtins(primitives: &mut (PrimitiveStore, Unifier)) -> BuiltinInfo {
|
||||||
.nth(1)
|
.nth(1)
|
||||||
.map(|(var_id, ty)| (*ty, *var_id))
|
.map(|(var_id, ty)| (*ty, *var_id))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
let ndarray_usized_ndims_tvar = primitives.1.get_fresh_const_generic_var(
|
||||||
|
size_t,
|
||||||
|
Some("ndarray_ndims".into()),
|
||||||
|
None,
|
||||||
|
);
|
||||||
let ndarray_copy_ty = *ndarray_fields.get(&"copy".into()).unwrap();
|
let ndarray_copy_ty = *ndarray_fields.get(&"copy".into()).unwrap();
|
||||||
let ndarray_fill_ty = *ndarray_fields.get(&"fill".into()).unwrap();
|
let ndarray_fill_ty = *ndarray_fields.get(&"fill".into()).unwrap();
|
||||||
|
let ndarray_add_ty = *ndarray_fields.get(&"__add__".into()).unwrap();
|
||||||
|
let ndarray_sub_ty = *ndarray_fields.get(&"__sub__".into()).unwrap();
|
||||||
|
let ndarray_mul_ty = *ndarray_fields.get(&"__mul__".into()).unwrap();
|
||||||
|
let ndarray_truediv_ty = *ndarray_fields.get(&"__truediv__".into()).unwrap();
|
||||||
|
let ndarray_floordiv_ty = *ndarray_fields.get(&"__floordiv__".into()).unwrap();
|
||||||
|
let ndarray_mod_ty = *ndarray_fields.get(&"__mod__".into()).unwrap();
|
||||||
|
let ndarray_pow_ty = *ndarray_fields.get(&"__pow__".into()).unwrap();
|
||||||
|
let ndarray_iadd_ty = *ndarray_fields.get(&"__iadd__".into()).unwrap();
|
||||||
|
let ndarray_isub_ty = *ndarray_fields.get(&"__isub__".into()).unwrap();
|
||||||
|
let ndarray_imul_ty = *ndarray_fields.get(&"__imul__".into()).unwrap();
|
||||||
|
let ndarray_itruediv_ty = *ndarray_fields.get(&"__itruediv__".into()).unwrap();
|
||||||
|
let ndarray_ifloordiv_ty = *ndarray_fields.get(&"__ifloordiv__".into()).unwrap();
|
||||||
|
let ndarray_imod_ty = *ndarray_fields.get(&"__imod__".into()).unwrap();
|
||||||
|
let ndarray_ipow_ty = *ndarray_fields.get(&"__ipow__".into()).unwrap();
|
||||||
|
|
||||||
let top_level_def_list = vec![
|
let top_level_def_list = vec![
|
||||||
Arc::new(RwLock::new(TopLevelComposer::make_top_level_class_def(
|
Arc::new(RwLock::new(TopLevelComposer::make_top_level_class_def(
|
||||||
|
@ -521,6 +545,20 @@ pub fn get_builtins(primitives: &mut (PrimitiveStore, Unifier)) -> BuiltinInfo {
|
||||||
methods: vec![
|
methods: vec![
|
||||||
("copy".into(), ndarray_copy_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 1)),
|
("copy".into(), ndarray_copy_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 1)),
|
||||||
("fill".into(), ndarray_fill_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 2)),
|
("fill".into(), ndarray_fill_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 2)),
|
||||||
|
("__add__".into(), ndarray_add_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 3)),
|
||||||
|
("__sub__".into(), ndarray_sub_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 4)),
|
||||||
|
("__mul__".into(), ndarray_mul_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 5)),
|
||||||
|
("__truediv__".into(), ndarray_mul_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 6)),
|
||||||
|
("__floordiv__".into(), ndarray_mul_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 7)),
|
||||||
|
("__mod__".into(), ndarray_mul_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 8)),
|
||||||
|
("__pow__".into(), ndarray_mul_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 9)),
|
||||||
|
("__iadd__".into(), ndarray_iadd_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 10)),
|
||||||
|
("__isub__".into(), ndarray_isub_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 11)),
|
||||||
|
("__imul__".into(), ndarray_imul_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 12)),
|
||||||
|
("__itruediv__".into(), ndarray_mul_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 13)),
|
||||||
|
("__ifloordiv__".into(), ndarray_mul_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 14)),
|
||||||
|
("__imod__".into(), ndarray_mul_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 15)),
|
||||||
|
("__ipow__".into(), ndarray_imul_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 16)),
|
||||||
],
|
],
|
||||||
ancestors: Vec::default(),
|
ancestors: Vec::default(),
|
||||||
constructor: None,
|
constructor: None,
|
||||||
|
@ -559,6 +597,216 @@ pub fn get_builtins(primitives: &mut (PrimitiveStore, Unifier)) -> BuiltinInfo {
|
||||||
)))),
|
)))),
|
||||||
loc: None,
|
loc: None,
|
||||||
})),
|
})),
|
||||||
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||||
|
name: "ndarray.__add__".into(),
|
||||||
|
simple_name: "__add__".into(),
|
||||||
|
signature: ndarray_add_ty.0,
|
||||||
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id],
|
||||||
|
instance_to_symbol: HashMap::default(),
|
||||||
|
instance_to_stmt: HashMap::default(),
|
||||||
|
resolver: None,
|
||||||
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|
|_, _, _, _, _| {
|
||||||
|
unreachable!("handled in gen_expr")
|
||||||
|
},
|
||||||
|
)))),
|
||||||
|
loc: None,
|
||||||
|
})),
|
||||||
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||||
|
name: "ndarray.__sub__".into(),
|
||||||
|
simple_name: "__sub__".into(),
|
||||||
|
signature: ndarray_sub_ty.0,
|
||||||
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id],
|
||||||
|
instance_to_symbol: HashMap::default(),
|
||||||
|
instance_to_stmt: HashMap::default(),
|
||||||
|
resolver: None,
|
||||||
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|
|_, _, _, _, _| {
|
||||||
|
unreachable!("handled in gen_expr")
|
||||||
|
},
|
||||||
|
)))),
|
||||||
|
loc: None,
|
||||||
|
})),
|
||||||
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||||
|
name: "ndarray.__mul__".into(),
|
||||||
|
simple_name: "__mul__".into(),
|
||||||
|
signature: ndarray_mul_ty.0,
|
||||||
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id],
|
||||||
|
instance_to_symbol: HashMap::default(),
|
||||||
|
instance_to_stmt: HashMap::default(),
|
||||||
|
resolver: None,
|
||||||
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|
|_, _, _, _, _| {
|
||||||
|
unreachable!("handled in gen_expr")
|
||||||
|
},
|
||||||
|
)))),
|
||||||
|
loc: None,
|
||||||
|
})),
|
||||||
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||||
|
name: "ndarray.__truediv__".into(),
|
||||||
|
simple_name: "__truediv__".into(),
|
||||||
|
signature: ndarray_truediv_ty.0,
|
||||||
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id],
|
||||||
|
instance_to_symbol: HashMap::default(),
|
||||||
|
instance_to_stmt: HashMap::default(),
|
||||||
|
resolver: None,
|
||||||
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|
|_, _, _, _, _| {
|
||||||
|
unreachable!("handled in gen_expr")
|
||||||
|
},
|
||||||
|
)))),
|
||||||
|
loc: None,
|
||||||
|
})),
|
||||||
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||||
|
name: "ndarray.__floordiv__".into(),
|
||||||
|
simple_name: "__floordiv__".into(),
|
||||||
|
signature: ndarray_floordiv_ty.0,
|
||||||
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id],
|
||||||
|
instance_to_symbol: HashMap::default(),
|
||||||
|
instance_to_stmt: HashMap::default(),
|
||||||
|
resolver: None,
|
||||||
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|
|_, _, _, _, _| {
|
||||||
|
unreachable!("handled in gen_expr")
|
||||||
|
},
|
||||||
|
)))),
|
||||||
|
loc: None,
|
||||||
|
})),
|
||||||
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||||
|
name: "ndarray.__mod__".into(),
|
||||||
|
simple_name: "__mod__".into(),
|
||||||
|
signature: ndarray_mod_ty.0,
|
||||||
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id],
|
||||||
|
instance_to_symbol: HashMap::default(),
|
||||||
|
instance_to_stmt: HashMap::default(),
|
||||||
|
resolver: None,
|
||||||
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|
|_, _, _, _, _| {
|
||||||
|
unreachable!("handled in gen_expr")
|
||||||
|
},
|
||||||
|
)))),
|
||||||
|
loc: None,
|
||||||
|
})),
|
||||||
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||||
|
name: "ndarray.__pow__".into(),
|
||||||
|
simple_name: "__pow__".into(),
|
||||||
|
signature: ndarray_pow_ty.0,
|
||||||
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id],
|
||||||
|
instance_to_symbol: HashMap::default(),
|
||||||
|
instance_to_stmt: HashMap::default(),
|
||||||
|
resolver: None,
|
||||||
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|
|_, _, _, _, _| {
|
||||||
|
unreachable!("handled in gen_expr")
|
||||||
|
},
|
||||||
|
)))),
|
||||||
|
loc: None,
|
||||||
|
})),
|
||||||
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||||
|
name: "ndarray.__iadd__".into(),
|
||||||
|
simple_name: "__iadd__".into(),
|
||||||
|
signature: ndarray_iadd_ty.0,
|
||||||
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id, ndarray_usized_ndims_tvar.1],
|
||||||
|
instance_to_symbol: HashMap::default(),
|
||||||
|
instance_to_stmt: HashMap::default(),
|
||||||
|
resolver: None,
|
||||||
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|
|_, _, _, _, _| {
|
||||||
|
unreachable!("handled in gen_expr")
|
||||||
|
},
|
||||||
|
)))),
|
||||||
|
loc: None,
|
||||||
|
})),
|
||||||
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||||
|
name: "ndarray.__isub__".into(),
|
||||||
|
simple_name: "__isub__".into(),
|
||||||
|
signature: ndarray_isub_ty.0,
|
||||||
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id],
|
||||||
|
instance_to_symbol: HashMap::default(),
|
||||||
|
instance_to_stmt: HashMap::default(),
|
||||||
|
resolver: None,
|
||||||
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|
|_, _, _, _, _| {
|
||||||
|
unreachable!("handled in gen_expr")
|
||||||
|
},
|
||||||
|
)))),
|
||||||
|
loc: None,
|
||||||
|
})),
|
||||||
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||||
|
name: "ndarray.__imul__".into(),
|
||||||
|
simple_name: "__imul__".into(),
|
||||||
|
signature: ndarray_imul_ty.0,
|
||||||
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id],
|
||||||
|
instance_to_symbol: HashMap::default(),
|
||||||
|
instance_to_stmt: HashMap::default(),
|
||||||
|
resolver: None,
|
||||||
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|
|_, _, _, _, _| {
|
||||||
|
unreachable!("handled in gen_expr")
|
||||||
|
},
|
||||||
|
)))),
|
||||||
|
loc: None,
|
||||||
|
})),
|
||||||
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||||
|
name: "ndarray.__itruediv__".into(),
|
||||||
|
simple_name: "__itruediv__".into(),
|
||||||
|
signature: ndarray_itruediv_ty.0,
|
||||||
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id],
|
||||||
|
instance_to_symbol: HashMap::default(),
|
||||||
|
instance_to_stmt: HashMap::default(),
|
||||||
|
resolver: None,
|
||||||
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|
|_, _, _, _, _| {
|
||||||
|
unreachable!("handled in gen_expr")
|
||||||
|
},
|
||||||
|
)))),
|
||||||
|
loc: None,
|
||||||
|
})),
|
||||||
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||||
|
name: "ndarray.__ifloordiv__".into(),
|
||||||
|
simple_name: "__ifloordiv__".into(),
|
||||||
|
signature: ndarray_ifloordiv_ty.0,
|
||||||
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id],
|
||||||
|
instance_to_symbol: HashMap::default(),
|
||||||
|
instance_to_stmt: HashMap::default(),
|
||||||
|
resolver: None,
|
||||||
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|
|_, _, _, _, _| {
|
||||||
|
unreachable!("handled in gen_expr")
|
||||||
|
},
|
||||||
|
)))),
|
||||||
|
loc: None,
|
||||||
|
})),
|
||||||
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||||
|
name: "ndarray.__imod__".into(),
|
||||||
|
simple_name: "__imod__".into(),
|
||||||
|
signature: ndarray_imod_ty.0,
|
||||||
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id],
|
||||||
|
instance_to_symbol: HashMap::default(),
|
||||||
|
instance_to_stmt: HashMap::default(),
|
||||||
|
resolver: None,
|
||||||
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|
|_, _, _, _, _| {
|
||||||
|
unreachable!("handled in gen_expr")
|
||||||
|
},
|
||||||
|
)))),
|
||||||
|
loc: None,
|
||||||
|
})),
|
||||||
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||||
|
name: "ndarray.__ipow__".into(),
|
||||||
|
simple_name: "__ipow__".into(),
|
||||||
|
signature: ndarray_ipow_ty.0,
|
||||||
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id],
|
||||||
|
instance_to_symbol: HashMap::default(),
|
||||||
|
instance_to_stmt: HashMap::default(),
|
||||||
|
resolver: None,
|
||||||
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
||||||
|
|_, _, _, _, _| {
|
||||||
|
unreachable!("handled in gen_expr")
|
||||||
|
},
|
||||||
|
)))),
|
||||||
|
loc: None,
|
||||||
|
})),
|
||||||
Arc::new(RwLock::new(TopLevelDef::Function {
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||||
name: "int32".into(),
|
name: "int32".into(),
|
||||||
simple_name: "int32".into(),
|
simple_name: "int32".into(),
|
||||||
|
|
|
@ -1926,9 +1926,8 @@ impl TopLevelComposer {
|
||||||
ret_str,
|
ret_str,
|
||||||
name,
|
name,
|
||||||
ast.as_ref().unwrap().location
|
ast.as_ref().unwrap().location
|
||||||
),
|
),]))
|
||||||
]))
|
}
|
||||||
}
|
|
||||||
|
|
||||||
instance_to_stmt.insert(
|
instance_to_stmt.insert(
|
||||||
get_subst_key(unifier, self_type, &subst, Some(&vars.keys().copied().collect())),
|
get_subst_key(unifier, self_type, &subst, Some(&vars.keys().copied().collect())),
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
|
||||||
use crate::symbol_resolver::SymbolValue;
|
use crate::symbol_resolver::SymbolValue;
|
||||||
|
use crate::toplevel::numpy::subst_ndarray_tvars;
|
||||||
use crate::typecheck::typedef::{Mapping, VarMap};
|
use crate::typecheck::typedef::{Mapping, VarMap};
|
||||||
use nac3parser::ast::{Constant, Location};
|
use nac3parser::ast::{Constant, Location};
|
||||||
|
|
||||||
|
@ -226,11 +227,57 @@ impl TopLevelComposer {
|
||||||
(ndarray_ndims_tvar.1, ndarray_ndims_tvar.0),
|
(ndarray_ndims_tvar.1, ndarray_ndims_tvar.0),
|
||||||
]),
|
]),
|
||||||
}));
|
}));
|
||||||
|
let ndarray_binop_fun_other_ty = unifier.get_fresh_var(None, None);
|
||||||
|
let ndarray_binop_fun_ret_ty = unifier.get_fresh_var(None, None);
|
||||||
|
let ndarray_binop_fun_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
||||||
|
args: vec![
|
||||||
|
FuncArg {
|
||||||
|
name: "other".into(),
|
||||||
|
ty: ndarray_binop_fun_other_ty.0,
|
||||||
|
default_value: None,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
ret: ndarray_binop_fun_ret_ty.0,
|
||||||
|
vars: VarMap::from([
|
||||||
|
(ndarray_dtype_tvar.1, ndarray_dtype_tvar.0),
|
||||||
|
(ndarray_ndims_tvar.1, ndarray_ndims_tvar.0),
|
||||||
|
]),
|
||||||
|
}));
|
||||||
|
let ndarray_truediv_fun_other_ty = unifier.get_fresh_var(None, None);
|
||||||
|
let ndarray_truediv_fun_ret_ty = unifier.get_fresh_var(None, None);
|
||||||
|
let ndarray_truediv_fun_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
||||||
|
args: vec![
|
||||||
|
FuncArg {
|
||||||
|
name: "other".into(),
|
||||||
|
ty: ndarray_truediv_fun_other_ty.0,
|
||||||
|
default_value: None,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
ret: ndarray_truediv_fun_ret_ty.0,
|
||||||
|
vars: VarMap::from([
|
||||||
|
(ndarray_dtype_tvar.1, ndarray_dtype_tvar.0),
|
||||||
|
(ndarray_ndims_tvar.1, ndarray_ndims_tvar.0),
|
||||||
|
]),
|
||||||
|
}));
|
||||||
let ndarray = unifier.add_ty(TypeEnum::TObj {
|
let ndarray = unifier.add_ty(TypeEnum::TObj {
|
||||||
obj_id: PRIMITIVE_DEF_IDS.ndarray,
|
obj_id: PRIMITIVE_DEF_IDS.ndarray,
|
||||||
fields: Mapping::from([
|
fields: Mapping::from([
|
||||||
("copy".into(), (ndarray_copy_fun_ty, true)),
|
("copy".into(), (ndarray_copy_fun_ty, true)),
|
||||||
("fill".into(), (ndarray_fill_fun_ty, true)),
|
("fill".into(), (ndarray_fill_fun_ty, true)),
|
||||||
|
("__add__".into(), (ndarray_binop_fun_ty, true)),
|
||||||
|
("__sub__".into(), (ndarray_binop_fun_ty, true)),
|
||||||
|
("__mul__".into(), (ndarray_binop_fun_ty, true)),
|
||||||
|
("__truediv__".into(), (ndarray_truediv_fun_ty, true)),
|
||||||
|
("__floordiv__".into(), (ndarray_binop_fun_ty, true)),
|
||||||
|
("__mod__".into(), (ndarray_binop_fun_ty, true)),
|
||||||
|
("__pow__".into(), (ndarray_binop_fun_ty, true)),
|
||||||
|
("__iadd__".into(), (ndarray_binop_fun_ty, true)),
|
||||||
|
("__isub__".into(), (ndarray_binop_fun_ty, true)),
|
||||||
|
("__imul__".into(), (ndarray_binop_fun_ty, true)),
|
||||||
|
("__itruediv__".into(), (ndarray_truediv_fun_ty, true)),
|
||||||
|
("__ifloordiv__".into(), (ndarray_binop_fun_ty, true)),
|
||||||
|
("__imod__".into(), (ndarray_binop_fun_ty, true)),
|
||||||
|
("__ipow__".into(), (ndarray_binop_fun_ty, true)),
|
||||||
]),
|
]),
|
||||||
params: VarMap::from([
|
params: VarMap::from([
|
||||||
(ndarray_dtype_tvar.1, ndarray_dtype_tvar.0),
|
(ndarray_dtype_tvar.1, ndarray_dtype_tvar.0),
|
||||||
|
@ -238,7 +285,16 @@ impl TopLevelComposer {
|
||||||
]),
|
]),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let ndarray_usized_ndims_tvar = unifier.get_fresh_const_generic_var(size_t_ty, Some("ndarray_ndims".into()), None);
|
||||||
|
let ndarray_unsized = subst_ndarray_tvars(&mut unifier, ndarray, Some(ndarray_usized_ndims_tvar.0), None);
|
||||||
|
|
||||||
unifier.unify(ndarray_copy_fun_ret_ty.0, ndarray).unwrap();
|
unifier.unify(ndarray_copy_fun_ret_ty.0, ndarray).unwrap();
|
||||||
|
unifier.unify(ndarray_binop_fun_other_ty.0, ndarray_unsized).unwrap();
|
||||||
|
unifier.unify(ndarray_binop_fun_ret_ty.0, ndarray).unwrap();
|
||||||
|
|
||||||
|
let ndarray_float = subst_ndarray_tvars(&mut unifier, ndarray, Some(float), None);
|
||||||
|
unifier.unify(ndarray_truediv_fun_other_ty.0, ndarray).unwrap();
|
||||||
|
unifier.unify(ndarray_truediv_fun_ret_ty.0, ndarray_float).unwrap();
|
||||||
|
|
||||||
let primitives = PrimitiveStore {
|
let primitives = PrimitiveStore {
|
||||||
int32,
|
int32,
|
||||||
|
|
|
@ -1,24 +1,9 @@
|
||||||
use inkwell::{IntPredicate, types::BasicType, values::{BasicValueEnum, PointerValue}};
|
|
||||||
use inkwell::values::{AggregateValueEnum, ArrayValue, IntValue};
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use nac3parser::ast::StrRef;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
codegen::{
|
toplevel::helper::PRIMITIVE_DEF_IDS,
|
||||||
classes::{ListValue, NDArrayValue},
|
|
||||||
CodeGenContext,
|
|
||||||
CodeGenerator,
|
|
||||||
irrt::{
|
|
||||||
call_ndarray_calc_nd_indices,
|
|
||||||
call_ndarray_calc_size,
|
|
||||||
},
|
|
||||||
llvm_intrinsics::call_memcpy_generic,
|
|
||||||
stmt::gen_for_callback
|
|
||||||
},
|
|
||||||
symbol_resolver::ValueEnum,
|
|
||||||
toplevel::{DefinitionId, helper::PRIMITIVE_DEF_IDS},
|
|
||||||
typecheck::{
|
typecheck::{
|
||||||
type_inferencer::PrimitiveStore,
|
type_inferencer::PrimitiveStore,
|
||||||
typedef::{FunSignature, Type, TypeEnum, Unifier, VarMap},
|
typedef::{Type, TypeEnum, Unifier, VarMap},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -34,16 +19,32 @@ pub fn make_ndarray_ty(
|
||||||
dtype: Option<Type>,
|
dtype: Option<Type>,
|
||||||
ndims: Option<Type>,
|
ndims: Option<Type>,
|
||||||
) -> Type {
|
) -> Type {
|
||||||
let ndarray = primitives.ndarray;
|
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 {
|
let TypeEnum::TObj { obj_id, params, .. } = &*unifier.get_ty_immutable(ndarray) else {
|
||||||
panic!("Expected `ndarray` to be TObj, but got {}", unifier.stringify(ndarray))
|
panic!("Expected `ndarray` to be TObj, but got {}", unifier.stringify(ndarray))
|
||||||
};
|
};
|
||||||
debug_assert_eq!(*obj_id, PRIMITIVE_DEF_IDS.ndarray);
|
debug_assert_eq!(*obj_id, PRIMITIVE_DEF_IDS.ndarray);
|
||||||
|
|
||||||
|
if dtype.is_none() && ndims.is_none() {
|
||||||
|
return ndarray
|
||||||
|
}
|
||||||
|
|
||||||
let tvar_ids = params.iter()
|
let tvar_ids = params.iter()
|
||||||
.map(|(obj_id, _)| *obj_id)
|
.map(|(obj_id, _)| *obj_id)
|
||||||
.sorted()
|
|
||||||
.collect_vec();
|
.collect_vec();
|
||||||
debug_assert_eq!(tvar_ids.len(), 2);
|
debug_assert_eq!(tvar_ids.len(), 2);
|
||||||
|
|
||||||
|
@ -58,12 +59,10 @@ pub fn make_ndarray_ty(
|
||||||
unifier.subst(ndarray, &tvar_subst).unwrap_or(ndarray)
|
unifier.subst(ndarray, &tvar_subst).unwrap_or(ndarray)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unpacks the type variables of `ndarray` into a tuple. The elements of the tuple corresponds to
|
fn unpack_ndarray_tvars(
|
||||||
/// `dtype` (the element type) and `ndims` (the number of dimensions) of the `ndarray` respectively.
|
|
||||||
pub fn unpack_ndarray_tvars(
|
|
||||||
unifier: &mut Unifier,
|
unifier: &mut Unifier,
|
||||||
ndarray: Type,
|
ndarray: Type,
|
||||||
) -> (Type, Type) {
|
) -> Vec<(u32, Type)> {
|
||||||
let TypeEnum::TObj { obj_id, params, .. } = &*unifier.get_ty_immutable(ndarray) else {
|
let TypeEnum::TObj { obj_id, params, .. } = &*unifier.get_ty_immutable(ndarray) else {
|
||||||
panic!("Expected `ndarray` to be TObj, but got {}", unifier.stringify(ndarray))
|
panic!("Expected `ndarray` to be TObj, but got {}", unifier.stringify(ndarray))
|
||||||
};
|
};
|
||||||
|
@ -72,889 +71,33 @@ pub fn unpack_ndarray_tvars(
|
||||||
|
|
||||||
params.iter()
|
params.iter()
|
||||||
.sorted_by_key(|(obj_id, _)| *obj_id)
|
.sorted_by_key(|(obj_id, _)| *obj_id)
|
||||||
.map(|(_, ty)| *ty)
|
.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,
|
||||||
|
) -> (u32, u32) {
|
||||||
|
unpack_ndarray_tvars(unifier, ndarray)
|
||||||
|
.into_iter()
|
||||||
|
.map(|v| v.0)
|
||||||
.collect_tuple()
|
.collect_tuple()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an `NDArray` instance from a dynamic shape.
|
/// 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.
|
||||||
/// * `elem_ty` - The element type of the `NDArray`.
|
pub fn unpack_ndarray_var_tys(
|
||||||
/// * `shape` - The shape of the `NDArray`.
|
unifier: &mut Unifier,
|
||||||
/// * `shape_len_fn` - A function that retrieves the number of dimensions from `shape`.
|
ndarray: Type,
|
||||||
/// * `shape_data_fn` - A function that retrieves the size of a dimension from `shape`.
|
) -> (Type, Type) {
|
||||||
fn create_ndarray_dyn_shape<'ctx, 'a, V, LenFn, DataFn>(
|
unpack_ndarray_tvars(unifier, ndarray)
|
||||||
generator: &mut dyn CodeGenerator,
|
.into_iter()
|
||||||
ctx: &mut CodeGenContext<'ctx, 'a>,
|
.map(|v| v.1)
|
||||||
elem_ty: Type,
|
.collect_tuple()
|
||||||
shape: &V,
|
|
||||||
shape_len_fn: LenFn,
|
|
||||||
shape_data_fn: DataFn,
|
|
||||||
) -> Result<NDArrayValue<'ctx>, String>
|
|
||||||
where
|
|
||||||
LenFn: Fn(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, &V) -> Result<IntValue<'ctx>, String>,
|
|
||||||
DataFn: Fn(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, &V, IntValue<'ctx>) -> Result<IntValue<'ctx>, String>,
|
|
||||||
{
|
|
||||||
let ndarray_ty = make_ndarray_ty(&mut ctx.unifier, &ctx.primitives, Some(elem_ty), None);
|
|
||||||
|
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
|
||||||
|
|
||||||
let llvm_pndarray_t = ctx.get_llvm_type(generator, ndarray_ty).into_pointer_type();
|
|
||||||
let llvm_ndarray_t = llvm_pndarray_t.get_element_type().into_struct_type();
|
|
||||||
let llvm_ndarray_data_t = ctx.get_llvm_type(generator, elem_ty).as_basic_type_enum();
|
|
||||||
assert!(llvm_ndarray_data_t.is_sized());
|
|
||||||
|
|
||||||
// Assert that all dimensions are non-negative
|
|
||||||
gen_for_callback(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
|generator, ctx| {
|
|
||||||
let i = generator.gen_var_alloc(ctx, llvm_usize.into(), None)?;
|
|
||||||
ctx.builder.build_store(i, llvm_usize.const_zero()).unwrap();
|
|
||||||
|
|
||||||
Ok(i)
|
|
||||||
},
|
|
||||||
|generator, ctx, i_addr| {
|
|
||||||
let i = ctx.builder
|
|
||||||
.build_load(i_addr, "")
|
|
||||||
.map(BasicValueEnum::into_int_value)
|
|
||||||
.unwrap();
|
|
||||||
let shape_len = shape_len_fn(generator, ctx, shape)?;
|
|
||||||
debug_assert!(shape_len.get_type().get_bit_width() <= llvm_usize.get_bit_width());
|
|
||||||
|
|
||||||
Ok(ctx.builder.build_int_compare(IntPredicate::ULT, i, shape_len, "").unwrap())
|
|
||||||
},
|
|
||||||
|generator, ctx, i_addr| {
|
|
||||||
let i = ctx.builder
|
|
||||||
.build_load(i_addr, "")
|
|
||||||
.map(BasicValueEnum::into_int_value)
|
|
||||||
.unwrap();
|
|
||||||
let shape_dim = shape_data_fn(generator, ctx, shape, i)?;
|
|
||||||
debug_assert!(shape_dim.get_type().get_bit_width() <= llvm_usize.get_bit_width());
|
|
||||||
|
|
||||||
let shape_dim_gez = ctx.builder
|
|
||||||
.build_int_compare(IntPredicate::SGE, shape_dim, shape_dim.get_type().const_zero(), "")
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.make_assert(
|
|
||||||
generator,
|
|
||||||
shape_dim_gez,
|
|
||||||
"0:ValueError",
|
|
||||||
"negative dimensions not supported",
|
|
||||||
[None, None, None],
|
|
||||||
ctx.current_loc,
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
|_, ctx, i_addr| {
|
|
||||||
let i = ctx.builder
|
|
||||||
.build_load(i_addr, "")
|
|
||||||
.map(BasicValueEnum::into_int_value)
|
|
||||||
.unwrap();
|
|
||||||
let i = ctx.builder.build_int_add(i, llvm_usize.const_int(1, true), "").unwrap();
|
|
||||||
ctx.builder.build_store(i_addr, i).unwrap();
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let ndarray = generator.gen_var_alloc(
|
|
||||||
ctx,
|
|
||||||
llvm_ndarray_t.into(),
|
|
||||||
None,
|
|
||||||
)?;
|
|
||||||
let ndarray = NDArrayValue::from_ptr_val(ndarray, llvm_usize, None);
|
|
||||||
|
|
||||||
let num_dims = shape_len_fn(generator, ctx, shape)?;
|
|
||||||
ndarray.store_ndims(ctx, generator, num_dims);
|
|
||||||
|
|
||||||
let ndarray_num_dims = ndarray.load_ndims(ctx);
|
|
||||||
ndarray.create_dims(ctx, llvm_usize, ndarray_num_dims);
|
|
||||||
|
|
||||||
// Copy the dimension sizes from shape to ndarray.dims
|
|
||||||
gen_for_callback(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
|generator, ctx| {
|
|
||||||
let i = generator.gen_var_alloc(ctx, llvm_usize.into(), None)?;
|
|
||||||
ctx.builder.build_store(i, llvm_usize.const_zero()).unwrap();
|
|
||||||
|
|
||||||
Ok(i)
|
|
||||||
},
|
|
||||||
|generator, ctx, i_addr| {
|
|
||||||
let i = ctx.builder
|
|
||||||
.build_load(i_addr, "")
|
|
||||||
.map(BasicValueEnum::into_int_value)
|
|
||||||
.unwrap();
|
|
||||||
let shape_len = shape_len_fn(generator, ctx, shape)?;
|
|
||||||
debug_assert!(shape_len.get_type().get_bit_width() <= llvm_usize.get_bit_width());
|
|
||||||
|
|
||||||
Ok(ctx.builder.build_int_compare(IntPredicate::ULT, i, shape_len, "").unwrap())
|
|
||||||
},
|
|
||||||
|generator, ctx, i_addr| {
|
|
||||||
let i = ctx.builder
|
|
||||||
.build_load(i_addr, "")
|
|
||||||
.map(BasicValueEnum::into_int_value)
|
|
||||||
.unwrap();
|
|
||||||
let shape_dim = shape_data_fn(generator, ctx, shape, i)?;
|
|
||||||
debug_assert!(shape_dim.get_type().get_bit_width() <= llvm_usize.get_bit_width());
|
|
||||||
let shape_dim = ctx.builder
|
|
||||||
.build_int_z_extend(shape_dim, llvm_usize, "")
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let ndarray_pdim = ndarray.get_dims().ptr_offset(ctx, generator, i, None);
|
|
||||||
|
|
||||||
ctx.builder.build_store(ndarray_pdim, shape_dim).unwrap();
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
|_, ctx, i_addr| {
|
|
||||||
let i = ctx.builder
|
|
||||||
.build_load(i_addr, "")
|
|
||||||
.map(BasicValueEnum::into_int_value)
|
|
||||||
.unwrap();
|
|
||||||
let i = ctx.builder.build_int_add(i, llvm_usize.const_int(1, true), "").unwrap();
|
|
||||||
ctx.builder.build_store(i_addr, i).unwrap();
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let ndarray_num_elems = call_ndarray_calc_size(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
ndarray.load_ndims(ctx),
|
|
||||||
ndarray.get_dims().get_ptr(ctx),
|
|
||||||
);
|
|
||||||
ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems);
|
|
||||||
|
|
||||||
Ok(ndarray)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates an `NDArray` instance from a constant shape.
|
|
||||||
///
|
|
||||||
/// * `elem_ty` - The element type of the `NDArray`.
|
|
||||||
/// * `shape` - The shape of the `NDArray`, represented as an LLVM [`ArrayValue`].
|
|
||||||
fn create_ndarray_const_shape<'ctx>(
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
elem_ty: Type,
|
|
||||||
shape: ArrayValue<'ctx>
|
|
||||||
) -> Result<NDArrayValue<'ctx>, String> {
|
|
||||||
let ndarray_ty = make_ndarray_ty(&mut ctx.unifier, &ctx.primitives, Some(elem_ty), None);
|
|
||||||
|
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
|
||||||
|
|
||||||
let llvm_pndarray_t = ctx.get_llvm_type(generator, ndarray_ty).into_pointer_type();
|
|
||||||
let llvm_ndarray_t = llvm_pndarray_t.get_element_type().into_struct_type();
|
|
||||||
let llvm_ndarray_data_t = ctx.get_llvm_type(generator, elem_ty).as_basic_type_enum();
|
|
||||||
assert!(llvm_ndarray_data_t.is_sized());
|
|
||||||
|
|
||||||
for i in 0..shape.get_type().len() {
|
|
||||||
let shape_dim = ctx.builder
|
|
||||||
.build_extract_value(shape, i, "")
|
|
||||||
.map(BasicValueEnum::into_int_value)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let shape_dim_gez = ctx.builder
|
|
||||||
.build_int_compare(IntPredicate::SGE, shape_dim, llvm_usize.const_zero(), "")
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.make_assert(
|
|
||||||
generator,
|
|
||||||
shape_dim_gez,
|
|
||||||
"0:ValueError",
|
|
||||||
"negative dimensions not supported",
|
|
||||||
[None, None, None],
|
|
||||||
ctx.current_loc,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let ndarray = generator.gen_var_alloc(
|
|
||||||
ctx,
|
|
||||||
llvm_ndarray_t.into(),
|
|
||||||
None,
|
|
||||||
)?;
|
|
||||||
let ndarray = NDArrayValue::from_ptr_val(ndarray, llvm_usize, None);
|
|
||||||
|
|
||||||
let num_dims = llvm_usize.const_int(shape.get_type().len() as u64, false);
|
|
||||||
ndarray.store_ndims(ctx, generator, num_dims);
|
|
||||||
|
|
||||||
let ndarray_num_dims = ndarray.load_ndims(ctx);
|
|
||||||
ndarray.create_dims(ctx, llvm_usize, ndarray_num_dims);
|
|
||||||
|
|
||||||
for i in 0..shape.get_type().len() {
|
|
||||||
let ndarray_dim = ndarray
|
|
||||||
.get_dims()
|
|
||||||
.ptr_offset(ctx, generator, llvm_usize.const_int(i as u64, true), None);
|
|
||||||
let shape_dim = ctx.builder.build_extract_value(shape, i, "")
|
|
||||||
.map(BasicValueEnum::into_int_value)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.builder.build_store(ndarray_dim, shape_dim).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
let ndarray_dims = ndarray.get_dims().get_ptr(ctx);
|
|
||||||
let ndarray_num_elems = call_ndarray_calc_size(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
ndarray.load_ndims(ctx),
|
|
||||||
ndarray_dims,
|
|
||||||
);
|
|
||||||
ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems);
|
|
||||||
|
|
||||||
Ok(ndarray)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ndarray_zero_value<'ctx>(
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
elem_ty: Type,
|
|
||||||
) -> BasicValueEnum<'ctx> {
|
|
||||||
if [ctx.primitives.int32, ctx.primitives.uint32].iter().any(|ty| ctx.unifier.unioned(elem_ty, *ty)) {
|
|
||||||
ctx.ctx.i32_type().const_zero().into()
|
|
||||||
} else if [ctx.primitives.int64, ctx.primitives.uint64].iter().any(|ty| ctx.unifier.unioned(elem_ty, *ty)) {
|
|
||||||
ctx.ctx.i64_type().const_zero().into()
|
|
||||||
} else if ctx.unifier.unioned(elem_ty, ctx.primitives.float) {
|
|
||||||
ctx.ctx.f64_type().const_zero().into()
|
|
||||||
} else if ctx.unifier.unioned(elem_ty, ctx.primitives.bool) {
|
|
||||||
ctx.ctx.bool_type().const_zero().into()
|
|
||||||
} else if ctx.unifier.unioned(elem_ty, ctx.primitives.str) {
|
|
||||||
ctx.gen_string(generator, "")
|
|
||||||
} else {
|
|
||||||
unreachable!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ndarray_one_value<'ctx>(
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
elem_ty: Type,
|
|
||||||
) -> BasicValueEnum<'ctx> {
|
|
||||||
if [ctx.primitives.int32, ctx.primitives.uint32].iter().any(|ty| ctx.unifier.unioned(elem_ty, *ty)) {
|
|
||||||
let is_signed = ctx.unifier.unioned(elem_ty, ctx.primitives.int32);
|
|
||||||
ctx.ctx.i32_type().const_int(1, is_signed).into()
|
|
||||||
} else if [ctx.primitives.int64, ctx.primitives.uint64].iter().any(|ty| ctx.unifier.unioned(elem_ty, *ty)) {
|
|
||||||
let is_signed = ctx.unifier.unioned(elem_ty, ctx.primitives.int64);
|
|
||||||
ctx.ctx.i64_type().const_int(1, is_signed).into()
|
|
||||||
} else if ctx.unifier.unioned(elem_ty, ctx.primitives.float) {
|
|
||||||
ctx.ctx.f64_type().const_float(1.0).into()
|
|
||||||
} else if ctx.unifier.unioned(elem_ty, ctx.primitives.bool) {
|
|
||||||
ctx.ctx.bool_type().const_int(1, false).into()
|
|
||||||
} else if ctx.unifier.unioned(elem_ty, ctx.primitives.str) {
|
|
||||||
ctx.gen_string(generator, "1")
|
|
||||||
} else {
|
|
||||||
unreachable!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// LLVM-typed implementation for generating the implementation for constructing an `NDArray`.
|
|
||||||
///
|
|
||||||
/// * `elem_ty` - The element type of the `NDArray`.
|
|
||||||
/// * `shape` - The `shape` parameter used to construct the `NDArray`.
|
|
||||||
fn call_ndarray_empty_impl<'ctx>(
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
elem_ty: Type,
|
|
||||||
shape: ListValue<'ctx>,
|
|
||||||
) -> Result<NDArrayValue<'ctx>, String> {
|
|
||||||
create_ndarray_dyn_shape(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
elem_ty,
|
|
||||||
&shape,
|
|
||||||
|_, ctx, shape| {
|
|
||||||
Ok(shape.load_size(ctx, None))
|
|
||||||
},
|
|
||||||
|generator, ctx, shape, idx| {
|
|
||||||
Ok(shape.get_data().get(ctx, generator, idx, None).into_int_value())
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates LLVM IR for populating the entire `NDArray` using a lambda with its flattened index as
|
|
||||||
/// its input.
|
|
||||||
fn ndarray_fill_flattened<'ctx, 'a, ValueFn>(
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, 'a>,
|
|
||||||
ndarray: NDArrayValue<'ctx>,
|
|
||||||
value_fn: ValueFn,
|
|
||||||
) -> Result<(), String>
|
|
||||||
where
|
|
||||||
ValueFn: Fn(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, IntValue<'ctx>) -> Result<BasicValueEnum<'ctx>, String>,
|
|
||||||
{
|
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
|
||||||
|
|
||||||
let ndarray_num_elems = call_ndarray_calc_size(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
ndarray.load_ndims(ctx),
|
|
||||||
ndarray.get_dims().get_ptr(ctx),
|
|
||||||
);
|
|
||||||
|
|
||||||
gen_for_callback(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
|generator, ctx| {
|
|
||||||
let i = generator.gen_var_alloc(ctx, llvm_usize.into(), None)?;
|
|
||||||
ctx.builder.build_store(i, llvm_usize.const_zero()).unwrap();
|
|
||||||
|
|
||||||
Ok(i)
|
|
||||||
},
|
|
||||||
|_, ctx, i_addr| {
|
|
||||||
let i = ctx.builder
|
|
||||||
.build_load(i_addr, "")
|
|
||||||
.map(BasicValueEnum::into_int_value)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
Ok(ctx.builder.build_int_compare(IntPredicate::ULT, i, ndarray_num_elems, "").unwrap())
|
|
||||||
},
|
|
||||||
|generator, ctx, i_addr| {
|
|
||||||
let i = ctx.builder
|
|
||||||
.build_load(i_addr, "")
|
|
||||||
.map(BasicValueEnum::into_int_value)
|
|
||||||
.unwrap();
|
|
||||||
let elem = unsafe {
|
|
||||||
ndarray.get_data().ptr_to_data_flattened_unchecked(ctx, i, None)
|
|
||||||
};
|
|
||||||
|
|
||||||
let value = value_fn(generator, ctx, i)?;
|
|
||||||
ctx.builder.build_store(elem, value).unwrap();
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
|_, ctx, i_addr| {
|
|
||||||
let i = ctx.builder
|
|
||||||
.build_load(i_addr, "")
|
|
||||||
.map(BasicValueEnum::into_int_value)
|
|
||||||
.unwrap();
|
|
||||||
let i = ctx.builder.build_int_add(i, llvm_usize.const_int(1, true), "").unwrap();
|
|
||||||
ctx.builder.build_store(i_addr, i).unwrap();
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates LLVM IR for populating the entire `NDArray` using a lambda with the dimension-indices
|
|
||||||
/// as its input.
|
|
||||||
fn ndarray_fill_indexed<'ctx, ValueFn>(
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
ndarray: NDArrayValue<'ctx>,
|
|
||||||
value_fn: ValueFn,
|
|
||||||
) -> Result<(), String>
|
|
||||||
where
|
|
||||||
ValueFn: Fn(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, '_>, PointerValue<'ctx>) -> Result<BasicValueEnum<'ctx>, String>,
|
|
||||||
{
|
|
||||||
ndarray_fill_flattened(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
ndarray,
|
|
||||||
|generator, ctx, idx| {
|
|
||||||
let indices = call_ndarray_calc_nd_indices(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
idx,
|
|
||||||
ndarray,
|
|
||||||
);
|
|
||||||
|
|
||||||
value_fn(generator, ctx, indices)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// LLVM-typed implementation for generating the implementation for `ndarray.zeros`.
|
|
||||||
///
|
|
||||||
/// * `elem_ty` - The element type of the `NDArray`.
|
|
||||||
/// * `shape` - The `shape` parameter used to construct the `NDArray`.
|
|
||||||
fn call_ndarray_zeros_impl<'ctx>(
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
elem_ty: Type,
|
|
||||||
shape: ListValue<'ctx>,
|
|
||||||
) -> Result<NDArrayValue<'ctx>, String> {
|
|
||||||
let supported_types = [
|
|
||||||
ctx.primitives.int32,
|
|
||||||
ctx.primitives.int64,
|
|
||||||
ctx.primitives.uint32,
|
|
||||||
ctx.primitives.uint64,
|
|
||||||
ctx.primitives.float,
|
|
||||||
ctx.primitives.bool,
|
|
||||||
ctx.primitives.str,
|
|
||||||
];
|
|
||||||
assert!(supported_types.iter().any(|supported_ty| ctx.unifier.unioned(*supported_ty, elem_ty)));
|
|
||||||
|
|
||||||
let ndarray = call_ndarray_empty_impl(generator, ctx, elem_ty, shape)?;
|
|
||||||
ndarray_fill_flattened(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
ndarray,
|
|
||||||
|generator, ctx, _| {
|
|
||||||
let value = ndarray_zero_value(generator, ctx, elem_ty);
|
|
||||||
|
|
||||||
Ok(value)
|
|
||||||
}
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(ndarray)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// LLVM-typed implementation for generating the implementation for `ndarray.ones`.
|
|
||||||
///
|
|
||||||
/// * `elem_ty` - The element type of the `NDArray`.
|
|
||||||
/// * `shape` - The `shape` parameter used to construct the `NDArray`.
|
|
||||||
fn call_ndarray_ones_impl<'ctx>(
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
elem_ty: Type,
|
|
||||||
shape: ListValue<'ctx>,
|
|
||||||
) -> Result<NDArrayValue<'ctx>, String> {
|
|
||||||
let supported_types = [
|
|
||||||
ctx.primitives.int32,
|
|
||||||
ctx.primitives.int64,
|
|
||||||
ctx.primitives.uint32,
|
|
||||||
ctx.primitives.uint64,
|
|
||||||
ctx.primitives.float,
|
|
||||||
ctx.primitives.bool,
|
|
||||||
ctx.primitives.str,
|
|
||||||
];
|
|
||||||
assert!(supported_types.iter().any(|supported_ty| ctx.unifier.unioned(*supported_ty, elem_ty)));
|
|
||||||
|
|
||||||
let ndarray = call_ndarray_empty_impl(generator, ctx, elem_ty, shape)?;
|
|
||||||
ndarray_fill_flattened(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
ndarray,
|
|
||||||
|generator, ctx, _| {
|
|
||||||
let value = ndarray_one_value(generator, ctx, elem_ty);
|
|
||||||
|
|
||||||
Ok(value)
|
|
||||||
}
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(ndarray)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// LLVM-typed implementation for generating the implementation for `ndarray.full`.
|
|
||||||
///
|
|
||||||
/// * `elem_ty` - The element type of the `NDArray`.
|
|
||||||
/// * `shape` - The `shape` parameter used to construct the `NDArray`.
|
|
||||||
fn call_ndarray_full_impl<'ctx>(
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
elem_ty: Type,
|
|
||||||
shape: ListValue<'ctx>,
|
|
||||||
fill_value: BasicValueEnum<'ctx>,
|
|
||||||
) -> Result<NDArrayValue<'ctx>, String> {
|
|
||||||
let ndarray = call_ndarray_empty_impl(generator, ctx, elem_ty, shape)?;
|
|
||||||
ndarray_fill_flattened(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
ndarray,
|
|
||||||
|generator, ctx, _| {
|
|
||||||
let value = if fill_value.is_pointer_value() {
|
|
||||||
let llvm_i1 = ctx.ctx.bool_type();
|
|
||||||
|
|
||||||
let copy = generator.gen_var_alloc(ctx, fill_value.get_type(), None)?;
|
|
||||||
|
|
||||||
call_memcpy_generic(
|
|
||||||
ctx,
|
|
||||||
copy,
|
|
||||||
fill_value.into_pointer_value(),
|
|
||||||
fill_value.get_type().size_of().map(Into::into).unwrap(),
|
|
||||||
llvm_i1.const_zero(),
|
|
||||||
);
|
|
||||||
|
|
||||||
copy.into()
|
|
||||||
} else if fill_value.is_int_value() || fill_value.is_float_value() {
|
|
||||||
fill_value
|
|
||||||
} else {
|
|
||||||
unreachable!()
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(value)
|
|
||||||
}
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(ndarray)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// LLVM-typed implementation for generating the implementation for `ndarray.eye`.
|
|
||||||
///
|
|
||||||
/// * `elem_ty` - The element type of the `NDArray`.
|
|
||||||
fn call_ndarray_eye_impl<'ctx>(
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
elem_ty: Type,
|
|
||||||
nrows: IntValue<'ctx>,
|
|
||||||
ncols: IntValue<'ctx>,
|
|
||||||
offset: IntValue<'ctx>,
|
|
||||||
) -> Result<NDArrayValue<'ctx>, String> {
|
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
|
||||||
let llvm_usize_2 = llvm_usize.array_type(2);
|
|
||||||
|
|
||||||
let shape_addr = generator.gen_var_alloc(ctx, llvm_usize_2.into(), None)?;
|
|
||||||
|
|
||||||
let shape = ctx.builder.build_load(shape_addr, "")
|
|
||||||
.map(BasicValueEnum::into_array_value)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let nrows = ctx.builder.build_int_z_extend_or_bit_cast(nrows, llvm_usize, "").unwrap();
|
|
||||||
let shape = ctx.builder
|
|
||||||
.build_insert_value(shape, nrows, 0, "")
|
|
||||||
.map(AggregateValueEnum::into_array_value)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let ncols = ctx.builder.build_int_z_extend_or_bit_cast(ncols, llvm_usize, "").unwrap();
|
|
||||||
let shape = ctx.builder
|
|
||||||
.build_insert_value(shape, ncols, 1, "")
|
|
||||||
.map(AggregateValueEnum::into_array_value)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let ndarray = create_ndarray_const_shape(generator, ctx, elem_ty, shape)?;
|
|
||||||
|
|
||||||
ndarray_fill_indexed(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
ndarray,
|
|
||||||
|generator, ctx, indices| {
|
|
||||||
let row = ctx.build_gep_and_load(
|
|
||||||
indices,
|
|
||||||
&[llvm_usize.const_int(0, false)],
|
|
||||||
None,
|
|
||||||
).into_int_value();
|
|
||||||
let col = ctx.build_gep_and_load(
|
|
||||||
indices,
|
|
||||||
&[llvm_usize.const_int(1, false)],
|
|
||||||
None,
|
|
||||||
).into_int_value();
|
|
||||||
|
|
||||||
let col_with_offset = ctx.builder
|
|
||||||
.build_int_add(
|
|
||||||
col,
|
|
||||||
ctx.builder.build_int_s_extend_or_bit_cast(offset, llvm_usize, "").unwrap(),
|
|
||||||
"",
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
let is_on_diag = ctx.builder
|
|
||||||
.build_int_compare(IntPredicate::EQ, row, col_with_offset, "")
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let zero = ndarray_zero_value(generator, ctx, elem_ty);
|
|
||||||
let one = ndarray_one_value(generator, ctx, elem_ty);
|
|
||||||
|
|
||||||
let value = ctx.builder.build_select(is_on_diag, one, zero, "").unwrap();
|
|
||||||
|
|
||||||
Ok(value)
|
|
||||||
},
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(ndarray)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// LLVM-typed implementation for generating the implementation for `ndarray.copy`.
|
|
||||||
///
|
|
||||||
/// * `elem_ty` - The element type of the `NDArray`.
|
|
||||||
fn ndarray_copy_impl<'ctx>(
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
elem_ty: Type,
|
|
||||||
this: NDArrayValue<'ctx>,
|
|
||||||
) -> Result<NDArrayValue<'ctx>, String> {
|
|
||||||
let llvm_i1 = ctx.ctx.bool_type();
|
|
||||||
|
|
||||||
let ndarray = create_ndarray_dyn_shape(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
elem_ty,
|
|
||||||
&this,
|
|
||||||
|_, ctx, shape| {
|
|
||||||
Ok(shape.load_ndims(ctx))
|
|
||||||
},
|
|
||||||
|generator, ctx, shape, idx| {
|
|
||||||
Ok(shape.get_dims().get(ctx, generator, idx, None))
|
|
||||||
},
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let len = call_ndarray_calc_size(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
ndarray.load_ndims(ctx),
|
|
||||||
ndarray.get_dims().get_ptr(ctx),
|
|
||||||
);
|
|
||||||
let sizeof_ty = ctx.get_llvm_type(generator, elem_ty);
|
|
||||||
let len_bytes = ctx.builder
|
|
||||||
.build_int_mul(
|
|
||||||
len,
|
|
||||||
sizeof_ty.size_of().unwrap(),
|
|
||||||
"",
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
call_memcpy_generic(
|
|
||||||
ctx,
|
|
||||||
ndarray.get_data().get_ptr(ctx),
|
|
||||||
this.get_data().get_ptr(ctx),
|
|
||||||
len_bytes,
|
|
||||||
llvm_i1.const_zero(),
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(ndarray)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates LLVM IR for `ndarray.empty`.
|
|
||||||
pub fn gen_ndarray_empty<'ctx>(
|
|
||||||
context: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
|
||||||
fun: (&FunSignature, DefinitionId),
|
|
||||||
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
) -> Result<PointerValue<'ctx>, String> {
|
|
||||||
assert!(obj.is_none());
|
|
||||||
assert_eq!(args.len(), 1);
|
|
||||||
|
|
||||||
let llvm_usize = generator.get_size_type(context.ctx);
|
|
||||||
let shape_ty = fun.0.args[0].ty;
|
|
||||||
let shape_arg = args[0].1.clone()
|
|
||||||
.to_basic_value_enum(context, generator, shape_ty)?;
|
|
||||||
|
|
||||||
call_ndarray_empty_impl(
|
|
||||||
generator,
|
|
||||||
context,
|
|
||||||
context.primitives.float,
|
|
||||||
ListValue::from_ptr_val(shape_arg.into_pointer_value(), llvm_usize, None),
|
|
||||||
).map(NDArrayValue::into)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates LLVM IR for `ndarray.zeros`.
|
|
||||||
pub fn gen_ndarray_zeros<'ctx>(
|
|
||||||
context: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
|
||||||
fun: (&FunSignature, DefinitionId),
|
|
||||||
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
) -> Result<PointerValue<'ctx>, String> {
|
|
||||||
assert!(obj.is_none());
|
|
||||||
assert_eq!(args.len(), 1);
|
|
||||||
|
|
||||||
let llvm_usize = generator.get_size_type(context.ctx);
|
|
||||||
let shape_ty = fun.0.args[0].ty;
|
|
||||||
let shape_arg = args[0].1.clone()
|
|
||||||
.to_basic_value_enum(context, generator, shape_ty)?;
|
|
||||||
|
|
||||||
call_ndarray_zeros_impl(
|
|
||||||
generator,
|
|
||||||
context,
|
|
||||||
context.primitives.float,
|
|
||||||
ListValue::from_ptr_val(shape_arg.into_pointer_value(), llvm_usize, None),
|
|
||||||
).map(NDArrayValue::into)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates LLVM IR for `ndarray.ones`.
|
|
||||||
pub fn gen_ndarray_ones<'ctx>(
|
|
||||||
context: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
|
||||||
fun: (&FunSignature, DefinitionId),
|
|
||||||
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
) -> Result<PointerValue<'ctx>, String> {
|
|
||||||
assert!(obj.is_none());
|
|
||||||
assert_eq!(args.len(), 1);
|
|
||||||
|
|
||||||
let llvm_usize = generator.get_size_type(context.ctx);
|
|
||||||
let shape_ty = fun.0.args[0].ty;
|
|
||||||
let shape_arg = args[0].1.clone()
|
|
||||||
.to_basic_value_enum(context, generator, shape_ty)?;
|
|
||||||
|
|
||||||
call_ndarray_ones_impl(
|
|
||||||
generator,
|
|
||||||
context,
|
|
||||||
context.primitives.float,
|
|
||||||
ListValue::from_ptr_val(shape_arg.into_pointer_value(), llvm_usize, None),
|
|
||||||
).map(NDArrayValue::into)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates LLVM IR for `ndarray.full`.
|
|
||||||
pub fn gen_ndarray_full<'ctx>(
|
|
||||||
context: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
|
||||||
fun: (&FunSignature, DefinitionId),
|
|
||||||
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
) -> Result<PointerValue<'ctx>, String> {
|
|
||||||
assert!(obj.is_none());
|
|
||||||
assert_eq!(args.len(), 2);
|
|
||||||
|
|
||||||
let llvm_usize = generator.get_size_type(context.ctx);
|
|
||||||
let shape_ty = fun.0.args[0].ty;
|
|
||||||
let shape_arg = args[0].1.clone()
|
|
||||||
.to_basic_value_enum(context, generator, shape_ty)?;
|
|
||||||
let fill_value_ty = fun.0.args[1].ty;
|
|
||||||
let fill_value_arg = args[1].1.clone()
|
|
||||||
.to_basic_value_enum(context, generator, fill_value_ty)?;
|
|
||||||
|
|
||||||
call_ndarray_full_impl(
|
|
||||||
generator,
|
|
||||||
context,
|
|
||||||
fill_value_ty,
|
|
||||||
ListValue::from_ptr_val(shape_arg.into_pointer_value(), llvm_usize, None),
|
|
||||||
fill_value_arg,
|
|
||||||
).map(NDArrayValue::into)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates LLVM IR for `ndarray.eye`.
|
|
||||||
pub fn gen_ndarray_eye<'ctx>(
|
|
||||||
context: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
|
||||||
fun: (&FunSignature, DefinitionId),
|
|
||||||
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
) -> Result<PointerValue<'ctx>, String> {
|
|
||||||
assert!(obj.is_none());
|
|
||||||
assert!(matches!(args.len(), 1..=3));
|
|
||||||
|
|
||||||
let nrows_ty = fun.0.args[0].ty;
|
|
||||||
let nrows_arg = args[0].1.clone()
|
|
||||||
.to_basic_value_enum(context, generator, nrows_ty)?;
|
|
||||||
|
|
||||||
let ncols_ty = fun.0.args[1].ty;
|
|
||||||
let ncols_arg = args.iter()
|
|
||||||
.find(|arg| arg.0.is_some_and(|name| name == fun.0.args[1].name))
|
|
||||||
.map(|arg| arg.1.clone().to_basic_value_enum(context, generator, ncols_ty))
|
|
||||||
.unwrap_or_else(|| {
|
|
||||||
args[0].1.clone().to_basic_value_enum(context, generator, nrows_ty)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let offset_ty = fun.0.args[2].ty;
|
|
||||||
let offset_arg = args.iter()
|
|
||||||
.find(|arg| arg.0.is_some_and(|name| name == fun.0.args[2].name))
|
|
||||||
.map(|arg| arg.1.clone().to_basic_value_enum(context, generator, offset_ty))
|
|
||||||
.unwrap_or_else(|| {
|
|
||||||
Ok(context.gen_symbol_val(
|
|
||||||
generator,
|
|
||||||
fun.0.args[2].default_value.as_ref().unwrap(),
|
|
||||||
offset_ty
|
|
||||||
))
|
|
||||||
})?;
|
|
||||||
|
|
||||||
call_ndarray_eye_impl(
|
|
||||||
generator,
|
|
||||||
context,
|
|
||||||
context.primitives.float,
|
|
||||||
nrows_arg.into_int_value(),
|
|
||||||
ncols_arg.into_int_value(),
|
|
||||||
offset_arg.into_int_value(),
|
|
||||||
).map(NDArrayValue::into)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates LLVM IR for `ndarray.identity`.
|
|
||||||
pub fn gen_ndarray_identity<'ctx>(
|
|
||||||
context: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
|
||||||
fun: (&FunSignature, DefinitionId),
|
|
||||||
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
) -> Result<PointerValue<'ctx>, String> {
|
|
||||||
assert!(obj.is_none());
|
|
||||||
assert_eq!(args.len(), 1);
|
|
||||||
|
|
||||||
let llvm_usize = generator.get_size_type(context.ctx);
|
|
||||||
|
|
||||||
let n_ty = fun.0.args[0].ty;
|
|
||||||
let n_arg = args[0].1.clone()
|
|
||||||
.to_basic_value_enum(context, generator, n_ty)?;
|
|
||||||
|
|
||||||
call_ndarray_eye_impl(
|
|
||||||
generator,
|
|
||||||
context,
|
|
||||||
context.primitives.float,
|
|
||||||
n_arg.into_int_value(),
|
|
||||||
n_arg.into_int_value(),
|
|
||||||
llvm_usize.const_zero(),
|
|
||||||
).map(NDArrayValue::into)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates LLVM IR for `ndarray.copy`.
|
|
||||||
pub fn gen_ndarray_copy<'ctx>(
|
|
||||||
context: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
|
||||||
_fun: (&FunSignature, DefinitionId),
|
|
||||||
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
) -> Result<PointerValue<'ctx>, String> {
|
|
||||||
assert!(obj.is_some());
|
|
||||||
assert!(args.is_empty());
|
|
||||||
|
|
||||||
let llvm_usize = generator.get_size_type(context.ctx);
|
|
||||||
|
|
||||||
let this_ty = obj.as_ref().unwrap().0;
|
|
||||||
let (this_elem_ty, _) = unpack_ndarray_tvars(&mut context.unifier, this_ty);
|
|
||||||
let this_arg = obj
|
|
||||||
.as_ref()
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.1
|
|
||||||
.clone()
|
|
||||||
.to_basic_value_enum(context, generator, this_ty)?;
|
|
||||||
|
|
||||||
ndarray_copy_impl(
|
|
||||||
generator,
|
|
||||||
context,
|
|
||||||
this_elem_ty,
|
|
||||||
NDArrayValue::from_ptr_val(this_arg.into_pointer_value(), llvm_usize, None),
|
|
||||||
).map(NDArrayValue::into)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates LLVM IR for `ndarray.fill`.
|
|
||||||
pub fn gen_ndarray_fill<'ctx>(
|
|
||||||
context: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
|
||||||
fun: (&FunSignature, DefinitionId),
|
|
||||||
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
|
||||||
generator: &mut dyn CodeGenerator,
|
|
||||||
) -> Result<(), String> {
|
|
||||||
assert!(obj.is_some());
|
|
||||||
assert_eq!(args.len(), 1);
|
|
||||||
|
|
||||||
let llvm_usize = generator.get_size_type(context.ctx);
|
|
||||||
|
|
||||||
let this_ty = obj.as_ref().unwrap().0;
|
|
||||||
let this_arg = obj.as_ref().unwrap().1.clone()
|
|
||||||
.to_basic_value_enum(context, generator, this_ty)?
|
|
||||||
.into_pointer_value();
|
|
||||||
let value_ty = fun.0.args[0].ty;
|
|
||||||
let value_arg = args[0].1.clone()
|
|
||||||
.to_basic_value_enum(context, generator, value_ty)?;
|
|
||||||
|
|
||||||
ndarray_fill_flattened(
|
|
||||||
generator,
|
|
||||||
context,
|
|
||||||
NDArrayValue::from_ptr_val(this_arg, llvm_usize, None),
|
|
||||||
|generator, ctx, _| {
|
|
||||||
let value = if value_arg.is_pointer_value() {
|
|
||||||
let llvm_i1 = ctx.ctx.bool_type();
|
|
||||||
|
|
||||||
let copy = generator.gen_var_alloc(ctx, value_arg.get_type(), None)?;
|
|
||||||
|
|
||||||
call_memcpy_generic(
|
|
||||||
ctx,
|
|
||||||
copy,
|
|
||||||
value_arg.into_pointer_value(),
|
|
||||||
value_arg.get_type().size_of().map(Into::into).unwrap(),
|
|
||||||
llvm_i1.const_zero(),
|
|
||||||
);
|
|
||||||
|
|
||||||
copy.into()
|
|
||||||
} else if value_arg.is_int_value() || value_arg.is_float_value() {
|
|
||||||
value_arg
|
|
||||||
} else {
|
|
||||||
unreachable!()
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(value)
|
|
||||||
}
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ expression: res_vec
|
||||||
"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: [32]\n}\n",
|
"Function {\nname: \"A.__init__\",\nsig: \"fn[[v:V], none]\",\nvar_id: [32]\n}\n",
|
||||||
"Function {\nname: \"A.fun\",\nsig: \"fn[[a:T], V]\",\nvar_id: [37]\n}\n",
|
"Function {\nname: \"A.fun\",\nsig: \"fn[[a:T], V]\",\nvar_id: [37]\n}\n",
|
||||||
"Function {\nname: \"gfun\",\nsig: \"fn[[a:A[int32, list[float]]], 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,11 +3,11 @@ source: nac3core/src/toplevel/test.rs
|
||||||
expression: res_vec
|
expression: res_vec
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
"Class {\nname: \"A\",\nancestors: [\"A[typevar18, typevar19]\"],\nfields: [\"a\", \"b\"],\nmethods: [(\"__init__\", \"fn[[a:A[bool, float], b:B], none]\"), (\"fun\", \"fn[[a:A[bool, float]], A[bool, int32]]\")],\ntype_vars: [\"typevar18\", \"typevar19\"]\n}\n",
|
"Class {\nname: \"A\",\nancestors: [\"A[typevar18, typevar19]\"],\nfields: [\"a\", \"b\"],\nmethods: [(\"__init__\", \"fn[[a:A[float, bool], b:B], none]\"), (\"fun\", \"fn[[a:A[float, bool]], A[bool, int32]]\")],\ntype_vars: [\"typevar18\", \"typevar19\"]\n}\n",
|
||||||
"Function {\nname: \"A.__init__\",\nsig: \"fn[[a:A[bool, float], 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[bool, float]], 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[bool, float]], A[bool, int32]]\"), (\"foo\", \"fn[[b:B], B]\"), (\"bar\", \"fn[[a:A[int32, list[B]]], tuple[A[bool, virtual[A[B, int32]]], 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",
|
||||||
"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:B], B]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"B.foo\",\nsig: \"fn[[b:B], B]\",\nvar_id: []\n}\n",
|
||||||
"Function {\nname: \"B.bar\",\nsig: \"fn[[a:A[int32, list[B]]], tuple[A[bool, virtual[A[B, int32]]], B]]\",\nvar_id: []\n}\n",
|
"Function {\nname: \"B.bar\",\nsig: \"fn[[a:A[list[B], int32]], tuple[A[virtual[A[B, int32]], bool], B]]\",\nvar_id: []\n}\n",
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use crate::symbol_resolver::SymbolValue;
|
use crate::symbol_resolver::SymbolValue;
|
||||||
use crate::toplevel::helper::PRIMITIVE_DEF_IDS;
|
use crate::toplevel::helper::PRIMITIVE_DEF_IDS;
|
||||||
|
use crate::typecheck::typedef::VarMap;
|
||||||
use super::*;
|
use super::*;
|
||||||
use nac3parser::ast::Constant;
|
use nac3parser::ast::Constant;
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::toplevel::numpy::make_ndarray_ty;
|
||||||
use crate::typecheck::{
|
use crate::typecheck::{
|
||||||
type_inferencer::*,
|
type_inferencer::*,
|
||||||
typedef::{FunSignature, FuncArg, Type, TypeEnum, Unifier, VarMap},
|
typedef::{FunSignature, FuncArg, Type, TypeEnum, Unifier, VarMap},
|
||||||
|
@ -234,8 +235,14 @@ pub fn impl_bitwise_shift(unifier: &mut Unifier, store: &PrimitiveStore, ty: Typ
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `Div`
|
/// `Div`
|
||||||
pub fn impl_div(unifier: &mut Unifier, store: &PrimitiveStore, ty: Type, other_ty: &[Type]) {
|
pub fn impl_div(
|
||||||
impl_binop(unifier, store, ty, other_ty, store.float, &[Operator::Div]);
|
unifier: &mut Unifier,
|
||||||
|
store: &PrimitiveStore,
|
||||||
|
ty: Type,
|
||||||
|
other_ty: &[Type],
|
||||||
|
ret_ty: Type,
|
||||||
|
) {
|
||||||
|
impl_binop(unifier, store, ty, other_ty, ret_ty, &[Operator::Div]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `FloorDiv`
|
/// `FloorDiv`
|
||||||
|
@ -299,8 +306,10 @@ 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,
|
||||||
|
ndarray: ndarray_t,
|
||||||
..
|
..
|
||||||
} = *store;
|
} = *store;
|
||||||
|
let size_t = store.usize();
|
||||||
|
|
||||||
/* int ======== */
|
/* int ======== */
|
||||||
for t in [int32_t, int64_t, uint32_t, uint64_t] {
|
for t in [int32_t, int64_t, uint32_t, uint64_t] {
|
||||||
|
@ -308,7 +317,7 @@ pub fn set_primitives_magic_methods(store: &PrimitiveStore, unifier: &mut Unifie
|
||||||
impl_pow(unifier, store, t, &[t], t);
|
impl_pow(unifier, store, t, &[t], t);
|
||||||
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]);
|
impl_div(unifier, store, t, &[t], float_t);
|
||||||
impl_floordiv(unifier, store, t, &[t], t);
|
impl_floordiv(unifier, store, t, &[t], t);
|
||||||
impl_mod(unifier, store, t, &[t], t);
|
impl_mod(unifier, store, t, &[t], t);
|
||||||
impl_invert(unifier, store, t);
|
impl_invert(unifier, store, t);
|
||||||
|
@ -323,7 +332,7 @@ pub fn set_primitives_magic_methods(store: &PrimitiveStore, unifier: &mut Unifie
|
||||||
/* float ======== */
|
/* float ======== */
|
||||||
impl_basic_arithmetic(unifier, store, float_t, &[float_t], float_t);
|
impl_basic_arithmetic(unifier, store, float_t, &[float_t], float_t);
|
||||||
impl_pow(unifier, store, float_t, &[int32_t, float_t], float_t);
|
impl_pow(unifier, store, float_t, &[int32_t, float_t], float_t);
|
||||||
impl_div(unifier, store, float_t, &[float_t]);
|
impl_div(unifier, store, float_t, &[float_t], float_t);
|
||||||
impl_floordiv(unifier, store, float_t, &[float_t], float_t);
|
impl_floordiv(unifier, store, float_t, &[float_t], float_t);
|
||||||
impl_mod(unifier, store, float_t, &[float_t], float_t);
|
impl_mod(unifier, store, float_t, &[float_t], float_t);
|
||||||
impl_sign(unifier, store, float_t);
|
impl_sign(unifier, store, float_t);
|
||||||
|
@ -334,4 +343,14 @@ pub fn set_primitives_magic_methods(store: &PrimitiveStore, unifier: &mut Unifie
|
||||||
/* bool ======== */
|
/* bool ======== */
|
||||||
impl_not(unifier, store, bool_t);
|
impl_not(unifier, store, bool_t);
|
||||||
impl_eq(unifier, store, bool_t);
|
impl_eq(unifier, store, bool_t);
|
||||||
|
|
||||||
|
/* ndarray ===== */
|
||||||
|
let ndarray_float_t = make_ndarray_ty(unifier, store, Some(float_t), None);
|
||||||
|
let ndarray_usized_ndims_tvar = unifier.get_fresh_const_generic_var(size_t, Some("ndarray_ndims".into()), None);
|
||||||
|
let ndarray_unsized_t = make_ndarray_ty(unifier, store, None, Some(ndarray_usized_ndims_tvar.0));
|
||||||
|
impl_basic_arithmetic(unifier, store, ndarray_t, &[ndarray_unsized_t], ndarray_t);
|
||||||
|
impl_pow(unifier, store, ndarray_t, &[ndarray_unsized_t], ndarray_t);
|
||||||
|
impl_div(unifier, store, ndarray_t, &[ndarray_t], ndarray_float_t);
|
||||||
|
impl_floordiv(unifier, store, ndarray_t, &[ndarray_unsized_t], ndarray_t);
|
||||||
|
impl_mod(unifier, store, ndarray_t, &[ndarray_unsized_t], ndarray_t);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ use crate::{
|
||||||
symbol_resolver::{SymbolResolver, SymbolValue},
|
symbol_resolver::{SymbolResolver, SymbolValue},
|
||||||
toplevel::{
|
toplevel::{
|
||||||
helper::PRIMITIVE_DEF_IDS,
|
helper::PRIMITIVE_DEF_IDS,
|
||||||
numpy::{make_ndarray_ty, unpack_ndarray_tvars},
|
numpy::{make_ndarray_ty, unpack_ndarray_var_tys},
|
||||||
TopLevelContext,
|
TopLevelContext,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -1334,7 +1334,7 @@ impl<'a> Inferencer<'a> {
|
||||||
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::TList { .. } => self.unifier.add_ty(TypeEnum::TList { ty }),
|
||||||
TypeEnum::TObj { obj_id, .. } if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
||||||
let (_, ndims) = unpack_ndarray_tvars(self.unifier, value.custom.unwrap());
|
let (_, ndims) = unpack_ndarray_var_tys(self.unifier, value.custom.unwrap());
|
||||||
|
|
||||||
make_ndarray_ty(self.unifier, self.primitives, Some(ty), Some(ndims))
|
make_ndarray_ty(self.unifier, self.primitives, Some(ty), Some(ndims))
|
||||||
}
|
}
|
||||||
|
@ -1347,7 +1347,7 @@ 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 == PRIMITIVE_DEF_IDS.ndarray => {
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
||||||
let (_, ndims) = unpack_ndarray_tvars(self.unifier, value.custom.unwrap());
|
let (_, ndims) = unpack_ndarray_var_tys(self.unifier, value.custom.unwrap());
|
||||||
self.infer_subscript_ndarray(value, ty, ndims)
|
self.infer_subscript_ndarray(value, ty, ndims)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -1379,7 +1379,7 @@ impl<'a> Inferencer<'a> {
|
||||||
Ok(ty)
|
Ok(ty)
|
||||||
}
|
}
|
||||||
TypeEnum::TObj { obj_id, .. } if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
||||||
let (_, ndims) = unpack_ndarray_tvars(self.unifier, value.custom.unwrap());
|
let (_, ndims) = unpack_ndarray_var_tys(self.unifier, value.custom.unwrap());
|
||||||
|
|
||||||
self.constrain(slice.custom.unwrap(), self.primitives.usize(), &slice.location)?;
|
self.constrain(slice.custom.unwrap(), self.primitives.usize(), &slice.location)?;
|
||||||
self.infer_subscript_ndarray(value, ty, ndims)
|
self.infer_subscript_ndarray(value, ty, ndims)
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::{BTreeMap, HashMap};
|
use std::collections::HashMap;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::rc::Rc;
|
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 std::iter::zip;
|
use std::iter::zip;
|
||||||
|
use indexmap::IndexMap;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
use nac3parser::ast::{Location, StrRef};
|
use nac3parser::ast::{Location, StrRef};
|
||||||
|
@ -25,14 +26,10 @@ pub type Type = UnificationKey;
|
||||||
pub struct CallId(pub(super) usize);
|
pub struct CallId(pub(super) usize);
|
||||||
|
|
||||||
pub type Mapping<K, V = Type> = HashMap<K, V>;
|
pub type Mapping<K, V = Type> = HashMap<K, V>;
|
||||||
|
pub type IndexMapping<K, V = Type> = IndexMap<K, V>;
|
||||||
|
|
||||||
/// A [`Mapping`] sorted by its key.
|
/// The mapping between type variable ID and [unifier type][`Type`].
|
||||||
///
|
pub type VarMap = IndexMapping<u32>;
|
||||||
/// This type is recommended for mappings that should be stored and/or iterated by its sorted key.
|
|
||||||
pub type SortedMapping<K, V = Type> = BTreeMap<K, V>;
|
|
||||||
|
|
||||||
/// A [`BTreeMap`] storing the mapping between type variable ID and [unifier type][`Type`].
|
|
||||||
pub type VarMap = SortedMapping<u32>;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Call {
|
pub struct Call {
|
||||||
|
@ -920,8 +917,8 @@ impl Unifier {
|
||||||
// Sort the type arguments by its UnificationKey first, since `HashMap::iter` visits
|
// Sort the type arguments by its UnificationKey first, since `HashMap::iter` visits
|
||||||
// all K-V pairs "in arbitrary order"
|
// all K-V pairs "in arbitrary order"
|
||||||
let (tv1, tv2) = (
|
let (tv1, tv2) = (
|
||||||
params1.iter().sorted_by_key(|(k, _)| *k).map(|(_, v)| v).collect_vec(),
|
params1.iter().map(|(_, v)| v).collect_vec(),
|
||||||
params2.iter().sorted_by_key(|(k, _)| *k).map(|(_, v)| v).collect_vec(),
|
params2.iter().map(|(_, v)| v).collect_vec(),
|
||||||
);
|
);
|
||||||
for (x, y) in zip(tv1, tv2) {
|
for (x, y) in zip(tv1, tv2) {
|
||||||
if self.unify_impl(*x, *y, false).is_err() {
|
if self.unify_impl(*x, *y, false).is_err() {
|
||||||
|
@ -1097,11 +1094,9 @@ impl Unifier {
|
||||||
if params.is_empty() {
|
if params.is_empty() {
|
||||||
name
|
name
|
||||||
} else {
|
} else {
|
||||||
let params = params
|
let mut params = params
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(_, v)| self.internal_stringify(*v, obj_to_name, var_to_name, notes));
|
.map(|(_, v)| self.internal_stringify(*v, obj_to_name, var_to_name, notes));
|
||||||
// sort to preserve order
|
|
||||||
let mut params = params.sorted();
|
|
||||||
format!("{}[{}]", name, params.join(", "))
|
format!("{}[{}]", name, params.join(", "))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1283,12 +1278,12 @@ impl Unifier {
|
||||||
|
|
||||||
fn subst_map<K>(
|
fn subst_map<K>(
|
||||||
&mut self,
|
&mut self,
|
||||||
map: &SortedMapping<K>,
|
map: &IndexMapping<K>,
|
||||||
mapping: &VarMap,
|
mapping: &VarMap,
|
||||||
cache: &mut HashMap<Type, Option<Type>>,
|
cache: &mut HashMap<Type, Option<Type>>,
|
||||||
) -> Option<SortedMapping<K>>
|
) -> Option<IndexMapping<K>>
|
||||||
where
|
where
|
||||||
K: Ord + Eq + Clone,
|
K: std::hash::Hash + Eq + Clone,
|
||||||
{
|
{
|
||||||
let mut map2 = None;
|
let mut map2 = None;
|
||||||
for (k, v) in map {
|
for (k, v) in map {
|
||||||
|
|
|
@ -45,9 +45,9 @@ impl Unifier {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map_eq<K>(&mut self, map1: &SortedMapping<K>, map2: &SortedMapping<K>) -> bool
|
fn map_eq<K>(&mut self, map1: &IndexMapping<K>, map2: &IndexMapping<K>) -> bool
|
||||||
where
|
where
|
||||||
K: Ord + Eq + Clone,
|
K: std::hash::Hash + Eq + Clone
|
||||||
{
|
{
|
||||||
if map1.len() != map2.len() {
|
if map1.len() != map2.len() {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -67,6 +67,181 @@ def test_ndarray_copy():
|
||||||
output_float64(y[1][0])
|
output_float64(y[1][0])
|
||||||
output_float64(y[1][1])
|
output_float64(y[1][1])
|
||||||
|
|
||||||
|
def test_ndarray_add():
|
||||||
|
x = np_identity(2)
|
||||||
|
y = x + np_ones([2, 2])
|
||||||
|
|
||||||
|
output_float64(x[0][0])
|
||||||
|
output_float64(x[0][1])
|
||||||
|
output_float64(x[1][0])
|
||||||
|
output_float64(x[1][1])
|
||||||
|
|
||||||
|
output_float64(y[0][0])
|
||||||
|
output_float64(y[0][1])
|
||||||
|
output_float64(y[1][0])
|
||||||
|
output_float64(y[1][1])
|
||||||
|
|
||||||
|
# def test_ndarray_add_broadcast():
|
||||||
|
# x = np_identity(2)
|
||||||
|
# y: ndarray[float, 2] = x + np_ones([2])
|
||||||
|
#
|
||||||
|
# output_float64(x[0][0])
|
||||||
|
# output_float64(x[0][1])
|
||||||
|
# output_float64(x[1][0])
|
||||||
|
# output_float64(x[1][1])
|
||||||
|
#
|
||||||
|
# output_float64(y[0][0])
|
||||||
|
# output_float64(y[0][1])
|
||||||
|
# output_float64(y[1][0])
|
||||||
|
# output_float64(y[1][1])
|
||||||
|
|
||||||
|
def test_ndarray_iadd():
|
||||||
|
x = np_identity(2)
|
||||||
|
x += np_ones([2, 2])
|
||||||
|
|
||||||
|
output_float64(x[0][0])
|
||||||
|
output_float64(x[0][1])
|
||||||
|
output_float64(x[1][0])
|
||||||
|
output_float64(x[1][1])
|
||||||
|
|
||||||
|
def test_ndarray_sub():
|
||||||
|
x = np_ones([2, 2])
|
||||||
|
y = x - np_identity(2)
|
||||||
|
|
||||||
|
output_float64(x[0][0])
|
||||||
|
output_float64(x[0][1])
|
||||||
|
output_float64(x[1][0])
|
||||||
|
output_float64(x[1][1])
|
||||||
|
|
||||||
|
output_float64(y[0][0])
|
||||||
|
output_float64(y[0][1])
|
||||||
|
output_float64(y[1][0])
|
||||||
|
output_float64(y[1][1])
|
||||||
|
|
||||||
|
def test_ndarray_isub():
|
||||||
|
x = np_ones([2, 2])
|
||||||
|
x -= np_identity(2)
|
||||||
|
|
||||||
|
output_float64(x[0][0])
|
||||||
|
output_float64(x[0][1])
|
||||||
|
output_float64(x[1][0])
|
||||||
|
output_float64(x[1][1])
|
||||||
|
|
||||||
|
def test_ndarray_mul():
|
||||||
|
x = np_ones([2, 2])
|
||||||
|
y = x * np_identity(2)
|
||||||
|
|
||||||
|
output_float64(x[0][0])
|
||||||
|
output_float64(x[0][1])
|
||||||
|
output_float64(x[1][0])
|
||||||
|
output_float64(x[1][1])
|
||||||
|
|
||||||
|
output_float64(y[0][0])
|
||||||
|
output_float64(y[0][1])
|
||||||
|
output_float64(y[1][0])
|
||||||
|
output_float64(y[1][1])
|
||||||
|
|
||||||
|
def test_ndarray_imul():
|
||||||
|
x = np_ones([2, 2])
|
||||||
|
x *= np_identity(2)
|
||||||
|
|
||||||
|
output_float64(x[0][0])
|
||||||
|
output_float64(x[0][1])
|
||||||
|
output_float64(x[1][0])
|
||||||
|
output_float64(x[1][1])
|
||||||
|
|
||||||
|
def test_ndarray_truediv():
|
||||||
|
x = np_identity(2)
|
||||||
|
y = x / np_ones([2, 2])
|
||||||
|
|
||||||
|
output_float64(x[0][0])
|
||||||
|
output_float64(x[0][1])
|
||||||
|
output_float64(x[1][0])
|
||||||
|
output_float64(x[1][1])
|
||||||
|
|
||||||
|
output_float64(y[0][0])
|
||||||
|
output_float64(y[0][1])
|
||||||
|
output_float64(y[1][0])
|
||||||
|
output_float64(y[1][1])
|
||||||
|
|
||||||
|
def test_ndarray_itruediv():
|
||||||
|
x = np_identity(2)
|
||||||
|
x /= np_ones([2, 2])
|
||||||
|
|
||||||
|
output_float64(x[0][0])
|
||||||
|
output_float64(x[0][1])
|
||||||
|
output_float64(x[1][0])
|
||||||
|
output_float64(x[1][1])
|
||||||
|
|
||||||
|
def test_ndarray_floordiv():
|
||||||
|
x = np_identity(2)
|
||||||
|
y = x // np_ones([2, 2])
|
||||||
|
|
||||||
|
output_float64(x[0][0])
|
||||||
|
output_float64(x[0][1])
|
||||||
|
output_float64(x[1][0])
|
||||||
|
output_float64(x[1][1])
|
||||||
|
|
||||||
|
output_float64(y[0][0])
|
||||||
|
output_float64(y[0][1])
|
||||||
|
output_float64(y[1][0])
|
||||||
|
output_float64(y[1][1])
|
||||||
|
|
||||||
|
def test_ndarray_ifloordiv():
|
||||||
|
x = np_identity(2)
|
||||||
|
x //= np_ones([2, 2])
|
||||||
|
|
||||||
|
output_float64(x[0][0])
|
||||||
|
output_float64(x[0][1])
|
||||||
|
output_float64(x[1][0])
|
||||||
|
output_float64(x[1][1])
|
||||||
|
|
||||||
|
def test_ndarray_mod():
|
||||||
|
x = np_identity(2)
|
||||||
|
y = x % np_full([2, 2], 2.0)
|
||||||
|
|
||||||
|
output_float64(x[0][0])
|
||||||
|
output_float64(x[0][1])
|
||||||
|
output_float64(x[1][0])
|
||||||
|
output_float64(x[1][1])
|
||||||
|
|
||||||
|
output_float64(y[0][0])
|
||||||
|
output_float64(y[0][1])
|
||||||
|
output_float64(y[1][0])
|
||||||
|
output_float64(y[1][1])
|
||||||
|
|
||||||
|
def test_ndarray_imod():
|
||||||
|
x = np_identity(2)
|
||||||
|
x %= np_full([2, 2], 2.0)
|
||||||
|
|
||||||
|
output_float64(x[0][0])
|
||||||
|
output_float64(x[0][1])
|
||||||
|
output_float64(x[1][0])
|
||||||
|
output_float64(x[1][1])
|
||||||
|
|
||||||
|
def test_ndarray_pow():
|
||||||
|
x = np_identity(2)
|
||||||
|
y = x ** np_full([2, 2], 2.0)
|
||||||
|
|
||||||
|
output_float64(x[0][0])
|
||||||
|
output_float64(x[0][1])
|
||||||
|
output_float64(x[1][0])
|
||||||
|
output_float64(x[1][1])
|
||||||
|
|
||||||
|
output_float64(y[0][0])
|
||||||
|
output_float64(y[0][1])
|
||||||
|
output_float64(y[1][0])
|
||||||
|
output_float64(y[1][1])
|
||||||
|
|
||||||
|
def test_ndarray_ipow():
|
||||||
|
x = np_identity(2)
|
||||||
|
x **= np_full([2, 2], 2.0)
|
||||||
|
|
||||||
|
output_float64(x[0][0])
|
||||||
|
output_float64(x[0][1])
|
||||||
|
output_float64(x[1][0])
|
||||||
|
output_float64(x[1][1])
|
||||||
|
|
||||||
def run() -> int32:
|
def run() -> int32:
|
||||||
test_ndarray_ctor()
|
test_ndarray_ctor()
|
||||||
test_ndarray_empty()
|
test_ndarray_empty()
|
||||||
|
@ -77,5 +252,17 @@ def run() -> int32:
|
||||||
test_ndarray_identity()
|
test_ndarray_identity()
|
||||||
test_ndarray_fill()
|
test_ndarray_fill()
|
||||||
test_ndarray_copy()
|
test_ndarray_copy()
|
||||||
|
test_ndarray_add()
|
||||||
|
test_ndarray_iadd()
|
||||||
|
test_ndarray_sub()
|
||||||
|
test_ndarray_isub()
|
||||||
|
test_ndarray_mul()
|
||||||
|
test_ndarray_imul()
|
||||||
|
test_ndarray_truediv()
|
||||||
|
test_ndarray_itruediv()
|
||||||
|
test_ndarray_floordiv()
|
||||||
|
test_ndarray_ifloordiv()
|
||||||
|
test_ndarray_mod()
|
||||||
|
test_ndarray_imod()
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
Loading…
Reference in New Issue