Compare commits
841 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8882c31eb4 | ||
|
|
8455ee8226 | ||
|
|
3f74aded8a | ||
|
|
2e19c2013c | ||
|
|
1edaa2d09b | ||
|
|
beee018287 | ||
|
|
34266ea59d | ||
|
|
f26127ce98 | ||
|
|
ccf321d178 | ||
|
|
355721c47a | ||
|
|
d400663e5d | ||
|
|
2203be945a | ||
|
|
b9a2bf4b42 | ||
|
|
cc08195cf2 | ||
|
|
50c40a7828 | ||
|
|
158be3f949 | ||
|
|
0f87c322ca | ||
|
|
d5dd19b64c | ||
|
|
3032d730b7 | ||
|
|
e93e7126b6 | ||
|
|
9bcf41d28f | ||
|
|
604ce47d5e | ||
|
|
e1ebe150d4 | ||
|
|
ae992737e8 | ||
|
|
c59d609eb8 | ||
|
|
9f26d12a3e | ||
|
|
9102577eba | ||
|
|
f0fcfde22b | ||
|
|
d9d813db56 | ||
|
|
a8808855ae | ||
|
|
5583f9418a | ||
|
|
7369ffd2aa | ||
|
|
ab15f30b13 | ||
|
|
8bff07d66b | ||
|
|
e2e2cb38be | ||
|
|
10d84a6a6b | ||
|
|
a4b4c1574b | ||
|
|
94bc39bdb2 | ||
|
|
7c93b37125 | ||
|
|
4371cc10bd | ||
|
|
eaa2628934 | ||
|
|
30e247186c | ||
|
|
2714da8d98 | ||
|
|
0ef3a7ec25 | ||
|
|
489a0069aa | ||
|
|
a257b7bdfa | ||
|
|
739cc66d0d | ||
|
|
28d944c6af | ||
|
|
19b98393a6 | ||
|
|
5a13e7400e | ||
|
|
a56129d675 | ||
|
|
0d75bbace0 | ||
|
|
29ec4a7a0e | ||
|
|
8b26c48d9b | ||
|
|
ddd0535d30 | ||
|
|
adcd370c27 | ||
|
|
dbead0d725 | ||
|
|
207aea813b | ||
|
|
7a771154a7 | ||
|
|
18c6b9b404 | ||
|
|
3110382d88 | ||
|
|
9ee55896e3 | ||
|
|
ad6f41f312 | ||
|
|
8512395985 | ||
|
|
7284042823 | ||
|
|
0ef683bba9 | ||
|
|
d4bf66936a | ||
|
|
be4737461a | ||
|
|
0d22b3be05 | ||
|
|
7c81d9293b | ||
|
|
d1dce65313 | ||
|
|
641f9bbf7c | ||
|
|
be0e42cf82 | ||
|
|
3a883b8821 | ||
|
|
4bbc58d62d | ||
|
|
5f4b09bede | ||
|
|
f0ade21155 | ||
|
|
f35063ee6e | ||
|
|
e6c7627ee8 | ||
|
|
a85d937482 | ||
|
|
b17632a352 | ||
|
|
b11eb742cb | ||
|
|
b63a7f64fd | ||
|
|
cafc438972 | ||
|
|
33ef7f737b | ||
|
|
3e26685a44 | ||
|
|
6a306be480 | ||
|
|
9defb6289b | ||
|
|
a47ad2886f | ||
|
|
c8d791f206 | ||
|
|
8c5f915569 | ||
|
|
ed366568b4 | ||
|
|
0a8a4eb6a6 | ||
|
|
fdc6cf4838 | ||
|
|
e52af0077b | ||
|
|
847f544839 | ||
|
|
685d3202d0 | ||
|
|
33a2580cc3 | ||
|
|
a8da654597 | ||
|
|
ae01a023ad | ||
|
|
ec20af3c4d | ||
|
|
a6f1ad1c2c | ||
|
|
c3532f17de | ||
|
|
86eac2f04d | ||
|
|
3b9b71643c | ||
|
|
19417cdd52 | ||
|
|
a7c23e25d8 | ||
|
|
f96ab87536 | ||
|
|
d65d49f504 | ||
|
|
8cd5924bf2 | ||
|
|
9f38338c58 | ||
|
|
8c76436d81 | ||
|
|
0e71576265 | ||
|
|
71b34003ca | ||
|
|
2c417d75a2 | ||
|
|
74ededd8c8 | ||
|
|
a732fa237c | ||
|
|
fd4c22308d | ||
|
|
d6f18894e6 | ||
|
|
992d54693f | ||
|
|
9be1bd0775 | ||
|
|
29cc689abd | ||
|
|
39d28e507d | ||
|
|
eacb5bc6f8 | ||
|
|
19658454bd | ||
|
|
02e3a6ae8b | ||
|
|
77376087db | ||
|
|
52a77f9efb | ||
|
|
d483b13107 | ||
|
|
dea4d9df63 | ||
|
|
b7601a7f08 | ||
|
|
2e148d6d0e | ||
|
|
8835a1a232 | ||
|
|
00e3b6b5a0 | ||
|
|
3bb51c5d51 | ||
|
|
433df05858 | ||
|
|
22524b03c8 | ||
|
|
508fa62a48 | ||
|
|
e7f7f98c02 | ||
|
|
9edaa8eb93 | ||
|
|
32587c1a40 | ||
|
|
019c482d18 | ||
|
|
ca906d9c71 | ||
|
|
35ba8476b0 | ||
|
|
a473901cfd | ||
|
|
b163d71aed | ||
|
|
d561f1399a | ||
|
|
00406e08fb | ||
|
|
0e4ca910d6 | ||
|
|
b00f52472b | ||
|
|
95a6e356ee | ||
|
|
f4da8e8876 | ||
|
|
1a08a59b49 | ||
|
|
758ceb791b | ||
|
|
6a1e0084f4 | ||
|
|
e318cab252 | ||
|
|
6b12547303 | ||
|
|
96d6c00815 | ||
|
|
7be79d81f9 | ||
|
|
2be558d554 | ||
|
|
60fac0a010 | ||
|
|
df0f239929 | ||
|
|
4e1aea4597 | ||
|
|
60224dd77d | ||
|
|
ded408f145 | ||
|
|
69deeca321 | ||
|
|
5e1e7bdad0 | ||
|
|
22f4924d8e | ||
|
|
8591275eb2 | ||
|
|
d91bb33178 | ||
|
|
d8e782f3f1 | ||
|
|
51755b8da3 | ||
|
|
e5c0aed75e | ||
|
|
6d7e984238 | ||
|
|
4dcc944f53 | ||
|
|
fbb978e8a5 | ||
|
|
46a3a4f7e3 | ||
|
|
e74d2b45c0 | ||
|
|
d40298ac58 | ||
|
|
87d7a4862e | ||
|
|
8be9a560e9 | ||
|
|
0d218bb4b9 | ||
|
|
1b3464b610 | ||
|
|
c4e66a104b | ||
|
|
d4eaef6ac8 | ||
|
|
9a634b22a7 | ||
|
|
54f5f38637 | ||
|
|
21a5180b2f | ||
|
|
8116d34a60 | ||
|
|
c6462cbcc7 | ||
|
|
4e34ce7470 | ||
|
|
c2299818cd | ||
|
|
2d06dc5cfe | ||
|
|
5329f28580 | ||
|
|
66909b3000 | ||
|
|
ae92904e7d | ||
|
|
70fb5ec7e1 | ||
|
|
058f74c12c | ||
|
|
3a2d24d7cc | ||
|
|
49f2f0dbfd | ||
|
|
5b216153e9 | ||
|
|
43175bb642 | ||
|
|
c1da220a99 | ||
|
|
164c3e0e7b | ||
|
|
491a23b21e | ||
|
|
727ec8a5cf | ||
|
|
28b3ec322c | ||
|
|
38cb89a946 | ||
|
|
b07116f302 | ||
|
|
623b5a511a | ||
|
|
d5d2d6826f | ||
|
|
227f6a4ed7 | ||
|
|
e23d7082fe | ||
|
|
569a29454e | ||
|
|
a07f711e22 | ||
|
|
946f304bb2 | ||
|
|
6b11c100ba | ||
|
|
6048693423 | ||
|
|
a7287754be | ||
|
|
d9037a7fce | ||
|
|
f06d292382 | ||
|
|
85d01d6f28 | ||
|
|
5cfeddef73 | ||
|
|
b975e77a63 | ||
|
|
ba46181365 | ||
|
|
370604f8eb | ||
|
|
368c7d6cda | ||
|
|
0a9dfdc5c0 | ||
|
|
72d0d4e274 | ||
|
|
d0067ccf68 | ||
|
|
409793cb21 | ||
|
|
a9eb65926f | ||
|
|
18d8677fe6 | ||
|
|
09c8567e39 | ||
|
|
6bbd4b214a | ||
|
|
a345746cbd | ||
|
|
0a0bb128d6 | ||
|
|
29ec3014e7 | ||
|
|
9ddd4d4161 | ||
|
|
3caae31374 | ||
|
|
b69abd1058 | ||
|
|
114c6bd6fc | ||
|
|
d81c5e750d | ||
|
|
281d29a100 | ||
|
|
46492009a1 | ||
|
|
ec549a6a25 | ||
|
|
71766d9c55 | ||
|
|
d2d8bd550a | ||
|
|
0c2f6c46f6 | ||
|
|
c90b93c1b7 | ||
|
|
faef4fffab | ||
|
|
8e8b97f623 | ||
|
|
e23d18dab8 | ||
|
|
3a0d1466cc | ||
|
|
83dd77f4c4 | ||
|
|
c601c992c1 | ||
|
|
5727761551 | ||
|
|
08365721de | ||
|
|
692c3542d9 | ||
|
|
60dc1f7d75 | ||
|
|
e5ff33fcc6 | ||
|
|
c60979fff7 | ||
|
|
961ad6ebca | ||
|
|
f18237a005 | ||
|
|
cba93e61b4 | ||
|
|
e0a6e5d2f9 | ||
|
|
a9ee323760 | ||
|
|
977db86700 | ||
|
|
93d36d40d8 | ||
|
|
cabc7ffca8 | ||
|
|
521b70c715 | ||
|
|
b6c5503c5b | ||
|
|
e7e2ba4790 | ||
|
|
8749923f1a | ||
|
|
25a3e19384 | ||
|
|
b6b686665a | ||
|
|
4f5468469b | ||
|
|
7bc7f23125 | ||
|
|
25ed652fc0 | ||
|
|
cc2913352a | ||
|
|
ad796ebbec | ||
|
|
edd561f8ba | ||
|
|
0b1c4fb54f | ||
|
|
9f4eb4f7db | ||
|
|
f6e3a3917e | ||
|
|
1b536bf5f8 | ||
|
|
e3afdb6c02 | ||
|
|
918b36e28f | ||
|
|
ce26637410 | ||
|
|
2053c72008 | ||
|
|
275bde6461 | ||
|
|
f85382edd4 | ||
|
|
e016e92c41 | ||
|
|
2594b8e3f9 | ||
|
|
1e1ef594c8 | ||
|
|
01c660faba | ||
|
|
9f76237705 | ||
|
|
59d3e3f3da | ||
|
|
e5dd237f12 | ||
|
|
e9729bea7e | ||
|
|
d46b17d8a0 | ||
|
|
383d25226e | ||
|
|
745eff0783 | ||
|
|
ac301045b9 | ||
|
|
e1f3ca8172 | ||
|
|
4ff4ceeb22 | ||
|
|
4126992a8f | ||
|
|
fd99f63bd6 | ||
|
|
46dc7c3a77 | ||
|
|
a8cb551465 | ||
|
|
5a5674809c | ||
|
|
761e8a2ac6 | ||
|
|
dfa7effa14 | ||
|
|
09c022e209 | ||
|
|
8666a75161 | ||
|
|
68018d8d04 | ||
|
|
3d2c014801 | ||
|
|
4f9c341103 | ||
|
|
563f03554f | ||
|
|
219376044a | ||
|
|
591dac8b41 | ||
|
|
2bc96ade33 | ||
|
|
95c4456ccc | ||
|
|
6e6b8b404f | ||
|
|
27052c21be | ||
|
|
ec744f7385 | ||
|
|
d8c5105c91 | ||
|
|
acd5fb19f5 | ||
|
|
52ac486814 | ||
|
|
a64b32bf9c | ||
|
|
b299800e64 | ||
|
|
60c430abad | ||
|
|
255cce4f9a | ||
|
|
1ed99c2574 | ||
|
|
64b54d4380 | ||
|
|
6170973b48 | ||
|
|
1172e5bdce | ||
|
|
b0123567cd | ||
|
|
81598621e8 | ||
|
|
239ac496b2 | ||
|
|
76fb91619a | ||
|
|
e3a2d43ffa | ||
|
|
b3f4716616 | ||
|
|
c602038597 | ||
|
|
03b469212e | ||
|
|
db3f6e077e | ||
|
|
06816c51a5 | ||
|
|
9e9e8e82b2 | ||
|
|
031d4ebedc | ||
|
|
3446715af8 | ||
|
|
c7e68dab62 | ||
|
|
7093ced2d8 | ||
|
|
179f4dfe22 | ||
|
|
03517ce22e | ||
|
|
290f99b4d1 | ||
|
|
7f10bc2a1b | ||
|
|
9cb48003b0 | ||
|
|
6df961292d | ||
|
|
65f855b251 | ||
|
|
5131881bf2 | ||
|
|
8fbad90e9d | ||
|
|
d0aceebaad | ||
|
|
01b6680000 | ||
|
|
638883f352 | ||
|
|
467c2fb28d | ||
|
|
0fd129a0d8 | ||
|
|
282125a84b | ||
|
|
97a2824473 | ||
|
|
eff1333269 | ||
|
|
3741a28d94 | ||
|
|
6dd4ec160d | ||
|
|
a6a3c09c05 | ||
|
|
620cfeabe0 | ||
|
|
070d64f365 | ||
|
|
1d3c98372a | ||
|
|
c1588d70cd | ||
|
|
ef28abe896 | ||
|
|
d251232f8a | ||
|
|
05334de855 | ||
|
|
539b55f826 | ||
|
|
0699832240 | ||
|
|
ce169163b9 | ||
|
|
b4af70ada9 | ||
|
|
424670d854 | ||
|
|
6ce4644120 | ||
|
|
eaf7e56083 | ||
|
|
e55a5d7486 | ||
|
|
e1d14b1324 | ||
|
|
04d09eb891 | ||
|
|
09b4f5dffd | ||
|
|
29ad96862f | ||
|
|
839d68d62c | ||
|
|
a4d3bf3cb2 | ||
|
|
a05bda9fc3 | ||
|
|
5936b57bac | ||
|
|
af2bcd389e | ||
|
|
4bec9b038b | ||
|
|
0b3ce5f65a | ||
|
|
cb8e820f5d | ||
|
|
0a4ce9d65e | ||
|
|
f20d74c1f3 | ||
|
|
3505be422e | ||
|
|
7e9542e79a | ||
|
|
a1c1a4889b | ||
|
|
7eeb8084bf | ||
|
|
7a5bd6ae6a | ||
|
|
d20e60afb3 | ||
|
|
7340263e6a | ||
|
|
6e8cecd13e | ||
|
|
f106a0854f | ||
|
|
68b09e87b3 | ||
|
|
11f2ecd675 | ||
|
|
7b6b8b0eeb | ||
|
|
67f9580c5d | ||
|
|
6a3fce97cf | ||
|
|
2bd266a7fc | ||
|
|
f6946b88d2 | ||
|
|
5e5df6becd | ||
|
|
6c4cb92249 | ||
|
|
c3407eac5e | ||
|
|
e46b3e24d6 | ||
|
|
90763de93c | ||
|
|
5380ffa471 | ||
|
|
c9f436cc47 | ||
|
|
39dc68fa4e | ||
|
|
d6c527f662 | ||
|
|
38a7f4f7d5 | ||
|
|
1605959a04 | ||
|
|
b837e0005a | ||
|
|
58937a5366 | ||
|
|
8562c03f80 | ||
|
|
1cd61b9169 | ||
|
|
c69e289afe | ||
|
|
91b46b05ad | ||
|
|
2412760f1c | ||
|
|
d83c5493c7 | ||
|
|
d041703dc6 | ||
|
|
ecba13c38e | ||
|
|
cd3a4bb8c8 | ||
|
|
97f72b93e1 | ||
|
|
e93d57983e | ||
|
|
43c4a3bfdc | ||
|
|
954cc0e8bc | ||
|
|
ad48325dff | ||
|
|
fa1ad20d5d | ||
|
|
0547dede21 | ||
|
|
284ae23db2 | ||
|
|
9105f28c13 | ||
|
|
06cc43b11f | ||
|
|
d879d0d924 | ||
|
|
25bc84817a | ||
|
|
8fb0dfad94 | ||
|
|
c2138037d2 | ||
|
|
fadd64c1e9 | ||
|
|
4552691aed | ||
|
|
bab5c0589f | ||
|
|
2a2c614eb4 | ||
|
|
12529ec81e | ||
|
|
0179609a49 | ||
|
|
860c551aa6 | ||
|
|
1ba3474a5a | ||
|
|
f26c283541 | ||
|
|
56e9dab2ce | ||
|
|
b1fcae5cec | ||
|
|
93245ac37a | ||
|
|
3f795e44c7 | ||
|
|
e732e5158e | ||
|
|
2f5c033f09 | ||
|
|
6099369019 | ||
|
|
8d2b65386c | ||
|
|
ed835225cf | ||
|
|
43c1bc8d5f | ||
|
|
17832fe18c | ||
|
|
252f3f0bd6 | ||
|
|
e042aad819 | ||
|
|
9966daf0cf | ||
|
|
daf5e9cccf | ||
|
|
bf63d731d3 | ||
|
|
12abfc0fcf | ||
|
|
dea1b520f7 | ||
|
|
a5c268491e | ||
|
|
bf34f553bb | ||
|
|
97c61404ca | ||
|
|
c40d9f54a2 | ||
|
|
5e08593358 | ||
|
|
1f06b12f86 | ||
|
|
c7351f724e | ||
|
|
1851bce497 | ||
|
|
5fb8503fd2 | ||
|
|
792716eefc | ||
|
|
15a6c779b8 | ||
|
|
93d24e7106 | ||
|
|
b8a185c112 | ||
|
|
e242e65569 | ||
|
|
62ab33a434 | ||
|
|
f403916ef8 | ||
|
|
38f1585ac6 | ||
|
|
1f757270d9 | ||
|
|
e6de8401bf | ||
|
|
df2e34ac51 | ||
|
|
7bbd3a7e36 | ||
|
|
85a736d49b | ||
|
|
f9eb313f7c | ||
|
|
9cec486a1b | ||
|
|
7734c654a7 | ||
|
|
8e5fff6c5f | ||
|
|
faa9a740db | ||
|
|
70e39e9902 | ||
|
|
9f0b3963cb | ||
|
|
7ec2ce851c | ||
|
|
94ee4223d2 | ||
|
|
a64f4219e9 | ||
|
|
78e96cc312 | ||
|
|
89b111edca | ||
|
|
ec38943c53 | ||
|
|
ea654ef235 | ||
|
|
288b705450 | ||
|
|
73ca579056 | ||
|
|
8b5d7dc181 | ||
|
|
f05f6294cd | ||
|
|
b2c466cf3f | ||
|
|
da1b9a0e91 | ||
|
|
6cbb5a9215 | ||
|
|
cfdbb86bfa | ||
|
|
d61783b2c0 | ||
|
|
65c1e1b9e5 | ||
|
|
17d509a45a | ||
|
|
c7649766fd | ||
|
|
c0ec5e53ba | ||
|
|
a0a18017e8 | ||
|
|
411b84fcc2 | ||
|
|
e499eeb8cc | ||
|
|
803d1de5db | ||
|
|
fc8117c8e7 | ||
|
|
8bef0ede1b | ||
|
|
88128cde48 | ||
|
|
d0217e62f0 | ||
|
|
bba680b636 | ||
|
|
feb914b5c4 | ||
|
|
0a65ea34f3 | ||
|
|
bf299edfc7 | ||
|
|
6b0122547e | ||
|
|
88c0e149b5 | ||
|
|
38091b2021 | ||
|
|
ce87f293aa | ||
|
|
dca028a84f | ||
|
|
be3c4ab24a | ||
|
|
67c9a14902 | ||
|
|
014bdb795f | ||
|
|
df7e8b2e64 | ||
|
|
2b5fdd3548 | ||
|
|
e92a0eb901 | ||
|
|
e72a67f5de | ||
|
|
65dc291ff5 | ||
|
|
0bfc190a86 | ||
|
|
f8303f2e7a | ||
|
|
8ad72b167c | ||
|
|
7747082ae8 | ||
|
|
a7727adca2 | ||
|
|
028b53816d | ||
|
|
5a77117a9b | ||
|
|
05777019c8 | ||
|
|
7ebaad5099 | ||
|
|
09885c8f41 | ||
|
|
4f5ebb279d | ||
|
|
91ebf88c97 | ||
|
|
a608c51e36 | ||
|
|
3877dcf83a | ||
|
|
72d176b77a | ||
|
|
8840968e07 | ||
|
|
b6b889bff6 | ||
|
|
a30bdcbb50 | ||
|
|
82275d49a6 | ||
|
|
ee335de222 | ||
|
|
01bf7c8c38 | ||
|
|
5529a1b0b3 | ||
|
|
363be18599 | ||
|
|
ae8ad3b68b | ||
|
|
348b850e36 | ||
|
|
92c267758e | ||
|
|
b7d1ab6105 | ||
|
|
f7f1b4f594 | ||
|
|
6492bea846 | ||
|
|
b1cb89b0c2 | ||
|
|
7ecd98b0a0 | ||
|
|
7d7d4db329 | ||
|
|
e6bfe1fc88 | ||
|
|
151d3a9610 | ||
|
|
905ed36afd | ||
|
|
7fe9c9366e | ||
|
|
d5237d1a07 | ||
|
|
7bd3b29a11 | ||
|
|
60aa74257f | ||
|
|
ca0492d997 | ||
|
|
944133de6e | ||
|
|
174fdd40da | ||
|
|
ffa823f748 | ||
|
|
a83f7a822e | ||
|
|
29d527bee1 | ||
|
|
75e282c2ac | ||
|
|
8419d9114b | ||
|
|
1c414af7b9 | ||
|
|
834e8c64c5 | ||
|
|
1b3bb86546 | ||
|
|
aa560f42e7 | ||
|
|
4e69cd28cd | ||
|
|
5f0e30e17a | ||
|
|
d682771c35 | ||
|
|
ace3c3e421 | ||
|
|
94005b0c22 | ||
|
|
e9177c8932 | ||
|
|
d62c2d913e | ||
|
|
9ea88fe247 | ||
|
|
e0867a5d11 | ||
|
|
ae91101ea1 | ||
|
|
515057c41a | ||
|
|
d946ba426e | ||
|
|
07874cf77f | ||
|
|
1d9f9e838b | ||
|
|
101691e970 | ||
|
|
e2498c31ea | ||
|
|
9d8c6122cc | ||
|
|
227dda38a2 | ||
|
|
e2091413ea | ||
|
|
4976e82f0f | ||
|
|
5e949d2c6f | ||
|
|
fec7688241 | ||
|
|
2732f6036e | ||
|
|
387c44031a | ||
|
|
6cbd1c5fb1 | ||
|
|
447b9e1ea7 | ||
|
|
be08bcaafc | ||
|
|
1b5e8e0181 | ||
|
|
d2ffbd0395 | ||
|
|
88cb607975 | ||
|
|
e392956e2a | ||
|
|
3028081fa2 | ||
|
|
4003c59471 | ||
|
|
2e042f0c59 | ||
|
|
9a4238d4e2 | ||
|
|
c184dc8d2f | ||
|
|
32f41a04ac | ||
|
|
4b8174f75a | ||
|
|
313e14bc54 | ||
|
|
7aca31992b | ||
|
|
56f53e508f | ||
|
|
6c18dc63aa | ||
|
|
7b498065b5 | ||
|
|
81bd225ff7 | ||
|
|
074090a0aa | ||
|
|
3fcbcca8e4 | ||
|
|
dbaf12b043 | ||
|
|
9eb9b48534 | ||
|
|
e085fd1d57 | ||
|
|
9cc71b320b | ||
|
|
7b74cf1ab9 | ||
|
|
fb0c0e07f7 | ||
|
|
c6bb4a23ae | ||
|
|
70e271959b | ||
|
|
379abeb262 | ||
|
|
9e884847b1 | ||
|
|
5dadf9a087 | ||
|
|
c0630b782a | ||
|
|
62beb73aa2 | ||
|
|
7b6fe0159f | ||
|
|
dad22b1686 | ||
|
|
a715a51865 | ||
|
|
d4ec2319f9 | ||
|
|
88b980ac17 | ||
|
|
867c01d5e8 | ||
|
|
c8a064af3e | ||
|
|
12439f2b99 | ||
|
|
fb47ea301f | ||
|
|
6adecbd7aa | ||
|
|
f71e34fd9f | ||
|
|
dfe89588f0 | ||
|
|
d89b68a279 | ||
|
|
2a4a01cb7b | ||
|
|
c81b7f6bb4 | ||
|
|
0c11afad7a | ||
|
|
d6b26c9975 | ||
|
|
98c628f3eb | ||
|
|
36b2026075 | ||
|
|
7c535ff1a3 | ||
|
|
c6436ea6d1 | ||
|
|
5a8dee3cbe | ||
|
|
d9450d6e12 | ||
|
|
80377b3705 | ||
|
|
847a76b3a2 | ||
|
|
2c3d46bb80 | ||
|
|
e0cb6d4531 | ||
|
|
78f0177ac4 | ||
|
|
4688434c08 | ||
|
|
8913eeb1c1 | ||
|
|
75574e97cc | ||
|
|
f4089bc164 | ||
|
|
b0f04d91bf | ||
|
|
3b514d194c | ||
|
|
3ba405383e | ||
|
|
87f6c8087f | ||
|
|
90a83c8f11 | ||
|
|
27f892a14b | ||
|
|
bf4525d82d | ||
|
|
6bfb1a7fff | ||
|
|
a53ab7438c | ||
|
|
d85a080f9b | ||
|
|
4dbfc9483e | ||
|
|
53097ab183 | ||
|
|
9978a370f1 | ||
|
|
24995f46cb | ||
|
|
7ddc8c6aeb | ||
|
|
4a447f5c12 | ||
|
|
e56647f24d | ||
|
|
25238b53c9 | ||
|
|
1ed798342a | ||
|
|
1d6eb07c62 | ||
|
|
8e3d76b7ea | ||
|
|
d8838503b2 | ||
|
|
78b7742354 | ||
|
|
3d9dca47b8 | ||
|
|
c06c96bc1f | ||
|
|
db128dbc40 | ||
|
|
d6f87a8254 | ||
|
|
4c5f37db0f | ||
|
|
3e5338c902 | ||
|
|
848b7c7a34 | ||
|
|
4bf5dd15e9 | ||
|
|
d4273d8e3f | ||
|
|
780347776b | ||
|
|
5dd6986ad4 | ||
|
|
51bf41009e | ||
|
|
4defe734e2 | ||
|
|
e3cb4ebfdc | ||
|
|
c6345279cc | ||
|
|
e5a9af9a31 | ||
|
|
c0e1e31572 | ||
|
|
1e58c365ed | ||
|
|
bf87b76adb | ||
|
|
c8e06b5837 | ||
|
|
ee5cd06077 | ||
|
|
140352b637 | ||
|
|
b369321e2f | ||
|
|
b9aaba7b16 | ||
|
|
07519732a1 | ||
|
|
ae71f3c186 | ||
|
|
e1236f9deb | ||
|
|
3c9bfb5b4d | ||
|
|
86b50b0a93 | ||
|
|
da6706cb93 | ||
|
|
2842a109da | ||
|
|
021ddefb10 | ||
|
|
7d0b47c5cb | ||
|
|
9351a1f900 | ||
|
|
0ac48369fe | ||
|
|
e57ee17532 | ||
|
|
a897683272 | ||
|
|
38eb981d2c | ||
|
|
b524472b9e | ||
|
|
fc04083cb2 | ||
|
|
b1225951f2 | ||
|
|
24fd2e1849 | ||
|
|
5e5c975a9c | ||
|
|
c6336e920f | ||
|
|
cd19625522 | ||
|
|
3ac95a9213 | ||
|
|
e57ea9b501 | ||
|
|
14b335a51e | ||
|
|
319e746a55 | ||
|
|
eb4d721175 | ||
|
|
a4f850c0c6 | ||
|
|
0a8e25b405 | ||
|
|
1557a76225 | ||
|
|
d4fa379f11 | ||
|
|
e09c5fcb3c | ||
|
|
815fe25f2c | ||
|
|
5e5c84ba27 | ||
|
|
2974b23f26 | ||
|
|
697c21b120 | ||
|
|
1f72a52015 | ||
|
|
43bcf1051d | ||
|
|
bf8aa502f9 | ||
|
|
f0e92343cb | ||
|
|
48efd6689e | ||
|
|
090e689689 | ||
|
|
9d16df5f25 | ||
|
|
02651c93a7 | ||
|
|
2b1d4b6672 | ||
|
|
9087dac6fe | ||
|
|
762ed994c1 | ||
|
|
2c8a9d1160 | ||
|
|
f613316046 | ||
|
|
6b1bc15f37 | ||
|
|
9a77a0c201 | ||
|
|
755cdbb238 | ||
|
|
1996db4b95 | ||
|
|
6297f69e70 | ||
|
|
a796f9f8a8 | ||
|
|
0de9c57ade | ||
|
|
95dc01cdcb | ||
|
|
364d3996f4 | ||
|
|
33af9e878b | ||
|
|
9f8b9ea806 | ||
|
|
d53876ee1b | ||
|
|
4f654e81c8 | ||
|
|
aa33ddcf19 | ||
|
|
c8a57676b4 | ||
|
|
d0d2bc1996 | ||
|
|
2b8d2b0026 | ||
|
|
735953a262 | ||
|
|
1c686f10a1 | ||
|
|
b7088510c5 | ||
|
|
3bdb921ee5 | ||
|
|
74b48ff56b | ||
|
|
9410370cc5 | ||
|
|
743ddf83c1 | ||
|
|
caa707325a | ||
|
|
3c588e67b8 | ||
|
|
6c26dad048 | ||
|
|
393e2c125e | ||
|
|
e56dc2ed6a | ||
|
|
4a449ed85e | ||
|
|
88dbe90075 | ||
|
|
a6f6451434 | ||
|
|
8a4370c1f6 | ||
|
|
7a068450b3 | ||
|
|
ae3222e4c2 | ||
|
|
27b4bfa3fa | ||
|
|
8af229947f | ||
|
|
f235a2f539 | ||
|
|
b0ebb479f6 | ||
|
|
df92e21520 | ||
|
|
a1a25cc57f | ||
|
|
e9aaf8e0af | ||
|
|
7a80e407af | ||
|
|
57f8d535fb | ||
|
|
170a6096b9 | ||
|
|
fcc444a100 | ||
|
|
8ccb3c21e1 | ||
|
|
0484d4bb77 | ||
|
|
3ce9567f62 | ||
|
|
765e812b77 |
48
.github/actions/setup-deps/action.yml
vendored
Normal file
48
.github/actions/setup-deps/action.yml
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
name: "Setup LLGO Dependencies"
|
||||
description: "Install all required dependencies for LLGO"
|
||||
inputs:
|
||||
llvm-version:
|
||||
description: "LLVM version to install"
|
||||
required: true
|
||||
default: "19"
|
||||
|
||||
runs:
|
||||
using: "composite"
|
||||
steps:
|
||||
- name: Install macOS dependencies
|
||||
if: runner.os == 'macOS'
|
||||
shell: bash
|
||||
run: |
|
||||
brew update
|
||||
brew install llvm@${{inputs.llvm-version}} lld@${{inputs.llvm-version}} bdw-gc openssl libffi libuv
|
||||
brew link --overwrite llvm@${{inputs.llvm-version}} lld@${{inputs.llvm-version}} libffi
|
||||
echo "$(brew --prefix llvm@${{inputs.llvm-version}})/bin" >> $GITHUB_PATH
|
||||
|
||||
# Install optional deps for demos.
|
||||
#
|
||||
# NOTE: Keep this list updated as new deps are introduced.
|
||||
opt_deps=(
|
||||
cjson # for github.com/goplus/lib/c/cjson
|
||||
sqlite # for github.com/goplus/lib/c/sqlite
|
||||
python@3.12 # for github.com/goplus/lib/py
|
||||
)
|
||||
brew install "${opt_deps[@]}"
|
||||
- name: Install Ubuntu dependencies
|
||||
if: runner.os == 'Linux'
|
||||
shell: bash
|
||||
run: |
|
||||
echo "deb http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-${{inputs.llvm-version}} main" | sudo tee /etc/apt/sources.list.d/llvm.list
|
||||
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y llvm-${{inputs.llvm-version}}-dev clang-${{inputs.llvm-version}} libclang-${{inputs.llvm-version}}-dev lld-${{inputs.llvm-version}} pkg-config libgc-dev libssl-dev zlib1g-dev libffi-dev libcjson-dev libunwind-dev libuv1-dev
|
||||
echo "PATH=/usr/lib/llvm-${{inputs.llvm-version}}/bin:$PATH" >> $GITHUB_ENV
|
||||
|
||||
# Install optional deps for demos.
|
||||
#
|
||||
# NOTE: Keep this list updated as new deps are introduced.
|
||||
opt_deps=(
|
||||
libcjson-dev # for github.com/goplus/lib/c/cjson
|
||||
libsqlite3-dev # for github.com/goplus/lib/c/sqlite
|
||||
python3.12-dev # for github.com/goplus/lib/py
|
||||
)
|
||||
sudo apt-get install -y "${opt_deps[@]}"
|
||||
51
.github/actions/setup-go/action.yml
vendored
Normal file
51
.github/actions/setup-go/action.yml
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
name: "Setup Go"
|
||||
description: "Setup Go environment by downloading and extracting from go.dev"
|
||||
inputs:
|
||||
go-version:
|
||||
description: "The Go version to download and use"
|
||||
required: true
|
||||
runs:
|
||||
using: "composite"
|
||||
steps:
|
||||
- name: Download and setup Go
|
||||
shell: bash
|
||||
run: |
|
||||
set -e
|
||||
GO_VERSION="${{ inputs.go-version }}"
|
||||
GO_VERSION="${GO_VERSION#go}" # Remove 'go' prefix if present
|
||||
|
||||
# Determine OS and architecture
|
||||
if [[ "$RUNNER_OS" == "macOS" ]]; then
|
||||
OS="darwin"
|
||||
ARCH="arm64"
|
||||
else
|
||||
OS="linux"
|
||||
ARCH="amd64"
|
||||
fi
|
||||
|
||||
DOWNLOAD_URL="https://go.dev/dl/go${GO_VERSION}.${OS}-${ARCH}.tar.gz"
|
||||
echo "Downloading Go from: ${DOWNLOAD_URL}"
|
||||
|
||||
# Create temporary directory for download
|
||||
TMP_DIR=$(mktemp -d)
|
||||
curl -L "${DOWNLOAD_URL}" -o "${TMP_DIR}/go.tar.gz"
|
||||
|
||||
# Remove existing Go installation if any
|
||||
sudo rm -rf /usr/local/go
|
||||
|
||||
# Extract to /usr/local
|
||||
sudo tar -C /usr/local -xzf "${TMP_DIR}/go.tar.gz"
|
||||
|
||||
# Clean up
|
||||
rm -rf "${TMP_DIR}"
|
||||
|
||||
# Add to PATH
|
||||
echo "/usr/local/go/bin" >> $GITHUB_PATH
|
||||
echo "$HOME/go/bin" >> $GITHUB_PATH
|
||||
|
||||
- name: Verify Go installation
|
||||
shell: bash
|
||||
run: |
|
||||
# Verify installation
|
||||
echo "Verifying Go installation..."
|
||||
go version
|
||||
35
.github/actions/test-helloworld/action.yml
vendored
Normal file
35
.github/actions/test-helloworld/action.yml
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
name: 'Test Hello World'
|
||||
description: 'Test Hello World with specific Go and module versions'
|
||||
inputs:
|
||||
go-version:
|
||||
description: 'Go version being tested'
|
||||
required: true
|
||||
mod-version:
|
||||
description: 'Go module version to use'
|
||||
required: true
|
||||
runs:
|
||||
using: "composite"
|
||||
steps:
|
||||
- name: Test Hello World
|
||||
shell: bash
|
||||
run: |
|
||||
echo "Testing with Go ${{ inputs.go-version }} and go.mod ${{ inputs.mod-version }}"
|
||||
mkdir -p _test/helloworld && cd _test/helloworld
|
||||
cat > go.mod << 'EOL'
|
||||
module hello
|
||||
go ${{ inputs.mod-version }}
|
||||
EOL
|
||||
cat > main.go << 'EOL'
|
||||
package main
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/goplus/lib/c"
|
||||
)
|
||||
func main() {
|
||||
fmt.Println("Hello, LLGo!")
|
||||
println("Hello, LLGo!")
|
||||
c.Printf(c.Str("Hello, LLGo!\n"))
|
||||
}
|
||||
EOL
|
||||
go mod tidy
|
||||
llgo run .
|
||||
11
.github/codecov.yml
vendored
Normal file
11
.github/codecov.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
coverage:
|
||||
ignore:
|
||||
- "chore"
|
||||
- "cmd"
|
||||
- "internal/build"
|
||||
- "internal/llgen"
|
||||
- "internal/mockable"
|
||||
- "internal/packages"
|
||||
- "internal/typepatch"
|
||||
- "internal/github"
|
||||
- "xtool/cppkg"
|
||||
169
.github/workflows/doc.yml
vendored
Normal file
169
.github/workflows/doc.yml
vendored
Normal file
@@ -0,0 +1,169 @@
|
||||
name: Docs
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: ["**"]
|
||||
pull_request:
|
||||
branches: ["**"]
|
||||
|
||||
jobs:
|
||||
doc_verify:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: "20"
|
||||
|
||||
- name: Install embedme
|
||||
run: npm install -g embedme
|
||||
|
||||
- name: Verify README.md embedded code
|
||||
run: embedme --verify README.md
|
||||
|
||||
- name: Link Checker
|
||||
id: lychee
|
||||
uses: lycheeverse/lychee-action@v2
|
||||
with:
|
||||
args: --max-concurrency 3 --retry-wait-time 15 README.md
|
||||
|
||||
remote_install:
|
||||
continue-on-error: true
|
||||
strategy:
|
||||
matrix:
|
||||
os:
|
||||
- macos-latest
|
||||
- ubuntu-24.04
|
||||
runs-on: ${{matrix.os}}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Go
|
||||
uses: ./.github/actions/setup-go
|
||||
with:
|
||||
go-version: "1.24.2"
|
||||
|
||||
- name: Install dependencies on macOS
|
||||
if: startsWith(matrix.os, 'macos')
|
||||
run: |
|
||||
set -e
|
||||
set -x
|
||||
source doc/_readme/scripts/install_macos.sh
|
||||
|
||||
- name: Install dependencies on Ubuntu
|
||||
if: startsWith(matrix.os, 'ubuntu')
|
||||
run: |
|
||||
set -e
|
||||
set -x
|
||||
source doc/_readme/scripts/install_ubuntu.sh
|
||||
|
||||
- name: Test doc code blocks
|
||||
run: |
|
||||
set -e
|
||||
set -x
|
||||
source doc/_readme/scripts/run.sh
|
||||
|
||||
local_install:
|
||||
continue-on-error: true
|
||||
strategy:
|
||||
matrix:
|
||||
os:
|
||||
- macos-latest
|
||||
- ubuntu-24.04
|
||||
runs-on: ${{matrix.os}}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: "1.23"
|
||||
|
||||
- name: Install dependencies on macOS
|
||||
if: startsWith(matrix.os, 'macos')
|
||||
run: |
|
||||
set -e
|
||||
set -x
|
||||
source doc/_readme/scripts/install_macos.sh
|
||||
|
||||
- name: Install dependencies on Ubuntu
|
||||
if: startsWith(matrix.os, 'ubuntu')
|
||||
run: |
|
||||
set -e
|
||||
set -x
|
||||
source doc/_readme/scripts/install_ubuntu.sh
|
||||
|
||||
- name: Install llgo with tools
|
||||
run: |
|
||||
set -e
|
||||
set -x
|
||||
git() {
|
||||
if [ "$1" = "clone" ]; then
|
||||
# do nothing because we already have the branch
|
||||
cd ..
|
||||
else
|
||||
command git "$@"
|
||||
fi
|
||||
}
|
||||
source doc/_readme/scripts/install_llgo.sh
|
||||
echo "LLGO_ROOT=$GITHUB_WORKSPACE" >> $GITHUB_ENV
|
||||
|
||||
- name: Test doc code blocks
|
||||
run: |
|
||||
set -e
|
||||
set -x
|
||||
source doc/_readme/scripts/run.sh
|
||||
|
||||
local_install_full:
|
||||
continue-on-error: true
|
||||
strategy:
|
||||
matrix:
|
||||
os:
|
||||
- macos-latest
|
||||
- ubuntu-24.04
|
||||
runs-on: ${{matrix.os}}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: "1.23"
|
||||
|
||||
- name: Install dependencies on macOS
|
||||
if: startsWith(matrix.os, 'macos')
|
||||
run: |
|
||||
set -e
|
||||
set -x
|
||||
source doc/_readme/scripts/install_macos.sh
|
||||
|
||||
- name: Install dependencies on Ubuntu
|
||||
if: startsWith(matrix.os, 'ubuntu')
|
||||
run: |
|
||||
set -e
|
||||
set -x
|
||||
source doc/_readme/scripts/install_ubuntu.sh
|
||||
echo "PATH=/usr/lib/llvm-19/bin:$PATH" >> $GITHUB_ENV
|
||||
|
||||
- name: Install llgo with tools
|
||||
run: |
|
||||
set -e
|
||||
set -x
|
||||
git() {
|
||||
if [ "$1" = "clone" ]; then
|
||||
# do nothing because we already have the branch
|
||||
cd ..
|
||||
else
|
||||
command git "$@"
|
||||
fi
|
||||
}
|
||||
source doc/_readme/scripts/install_full.sh
|
||||
echo "LLGO_ROOT=$GITHUB_WORKSPACE" >> $GITHUB_ENV
|
||||
|
||||
- name: Test doc code blocks
|
||||
run: |
|
||||
set -e
|
||||
set -x
|
||||
source doc/_readme/scripts/run.sh
|
||||
30
.github/workflows/fmt.yml
vendored
Normal file
30
.github/workflows/fmt.yml
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
name: Format Check
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: ["**"]
|
||||
pull_request:
|
||||
branches: ["**"]
|
||||
|
||||
jobs:
|
||||
fmt:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Go
|
||||
uses: ./.github/actions/setup-go
|
||||
with:
|
||||
go-version: "1.24.2"
|
||||
|
||||
- name: Check formatting
|
||||
run: |
|
||||
for dir in . runtime; do
|
||||
pushd $dir
|
||||
if [ -n "$(go fmt ./... | grep -v gop_autogen.go)" ]; then
|
||||
echo "Some files are not properly formatted. Please run 'go fmt ./...'"
|
||||
exit 1
|
||||
fi
|
||||
popd
|
||||
done
|
||||
echo "All files are properly formatted."
|
||||
89
.github/workflows/go.yml
vendored
89
.github/workflows/go.yml
vendored
@@ -5,67 +5,26 @@ name: Go
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "*" ]
|
||||
branches: ["**"]
|
||||
pull_request:
|
||||
branches: [ "*" ]
|
||||
branches: ["**"]
|
||||
|
||||
jobs:
|
||||
|
||||
test:
|
||||
continue-on-error: true
|
||||
strategy:
|
||||
matrix:
|
||||
os:
|
||||
- macos-latest
|
||||
- ubuntu-24.04
|
||||
llvm: [18]
|
||||
llvm: [19]
|
||||
runs-on: ${{matrix.os}}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install dependencies
|
||||
if: startsWith(matrix.os, 'macos')
|
||||
run: |
|
||||
brew update
|
||||
brew install llvm@${{matrix.llvm}} pkg-config bdw-gc openssl
|
||||
echo "$(brew --prefix llvm@${{matrix.llvm}})/bin" >> $GITHUB_PATH
|
||||
|
||||
# Install optional deps for demos.
|
||||
#
|
||||
# NOTE: Keep this list updated as new deps are introduced.
|
||||
opt_deps=(
|
||||
cjson # for github.com/goplus/llgo/c/cjson
|
||||
sqlite # for github.com/goplus/llgo/c/sqlite
|
||||
python@3.12 # for github.com/goplus/llgo/py
|
||||
)
|
||||
brew install "${opt_deps[@]}"
|
||||
|
||||
- name: Install dependencies
|
||||
if: startsWith(matrix.os, 'ubuntu')
|
||||
run: |
|
||||
echo "deb http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-${{matrix.llvm}} main" | sudo tee /etc/apt/sources.list.d/llvm.list
|
||||
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y llvm-${{matrix.llvm}}-dev clang-${{matrix.llvm}} lld-${{matrix.llvm}} pkg-config libgc-dev libssl-dev zlib1g-dev
|
||||
echo "/usr/lib/llvm-${{matrix.llvm}}/bin" >> $GITHUB_PATH
|
||||
|
||||
# Install optional deps for demos.
|
||||
#
|
||||
# NOTE: Keep this list updated as new deps are introduced.
|
||||
opt_deps=(
|
||||
libcjson-dev # for github.com/goplus/llgo/c/cjson
|
||||
libsqlite3-dev # for github.com/goplus/llgo/c/sqlite
|
||||
python3.12-dev # for github.com/goplus/llgo/py
|
||||
)
|
||||
sudo apt-get install -y "${opt_deps[@]}"
|
||||
|
||||
- name: Install further optional dependencies for demos
|
||||
run: |
|
||||
wget -P ./_demo/llama2-c https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin
|
||||
py_deps=(
|
||||
numpy # for github.com/goplus/llgo/py/numpy
|
||||
torch # for github.com/goplus/llgo/py/torch
|
||||
)
|
||||
pip3 install --break-system-packages "${py_deps[@]}"
|
||||
uses: ./.github/actions/setup-deps
|
||||
with:
|
||||
llvm-version: ${{matrix.llvm}}
|
||||
|
||||
- name: Clang information
|
||||
run: |
|
||||
@@ -74,46 +33,22 @@ jobs:
|
||||
clang --version
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
uses: ./.github/actions/setup-go
|
||||
with:
|
||||
go-version: '1.20'
|
||||
go-version: "1.24.2"
|
||||
|
||||
- name: Build
|
||||
run: go build -v ./...
|
||||
|
||||
- name: Test
|
||||
if: ${{!startsWith(matrix.os, 'macos')}}
|
||||
run: go test -v ./...
|
||||
run: go test ./...
|
||||
|
||||
- name: Test with coverage
|
||||
if: startsWith(matrix.os, 'macos')
|
||||
run: go test -v -coverprofile="coverage.txt" -covermode=atomic ./...
|
||||
|
||||
- name: Install
|
||||
run: go install ./...
|
||||
|
||||
- name: LLGO tests
|
||||
if: ${{!startsWith(matrix.os, 'ubuntu')}}
|
||||
run: |
|
||||
echo "Test result on ${{matrix.os}} with LLVM ${{matrix.llvm}}" > result.md
|
||||
bash .github/workflows/test_llgo.sh
|
||||
|
||||
- name: Test demos
|
||||
continue-on-error: true
|
||||
run: bash .github/workflows/test_demo.sh
|
||||
|
||||
- name: Show test result
|
||||
run: cat result.md
|
||||
|
||||
- name: PR comment with test result
|
||||
uses: thollander/actions-comment-pull-request@v2
|
||||
if: false
|
||||
with:
|
||||
filePath: result.md
|
||||
comment_tag: test-result-on-${{matrix.os}}-with-llvm-${{matrix.llvm}}
|
||||
run: go test -coverprofile="coverage.txt" -covermode=atomic ./...
|
||||
|
||||
- name: Upload coverage reports to Codecov
|
||||
uses: codecov/codecov-action@v4
|
||||
uses: codecov/codecov-action@v5
|
||||
with:
|
||||
token: ${{secrets.CODECOV_TOKEN}}
|
||||
slug: goplus/llgo
|
||||
|
||||
247
.github/workflows/llgo.yml
vendored
Normal file
247
.github/workflows/llgo.yml
vendored
Normal file
@@ -0,0 +1,247 @@
|
||||
# This workflow will build a golang project
|
||||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
|
||||
|
||||
name: LLGo
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: ["**"]
|
||||
pull_request:
|
||||
branches: ["**"]
|
||||
|
||||
jobs:
|
||||
download-model:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Download model file
|
||||
run: |
|
||||
mkdir -p ./_demo/llama2-c
|
||||
wget -P ./_demo/llama2-c https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin
|
||||
|
||||
- name: Upload model as artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: llama2-model
|
||||
path: ./_demo/llama2-c/stories15M.bin
|
||||
retention-days: 1
|
||||
|
||||
llgo:
|
||||
needs: download-model
|
||||
continue-on-error: true
|
||||
strategy:
|
||||
matrix:
|
||||
os:
|
||||
- macos-latest
|
||||
- ubuntu-24.04
|
||||
llvm: [19]
|
||||
go: ["1.21.13", "1.22.12", "1.23.6", "1.24.2"]
|
||||
runs-on: ${{matrix.os}}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install dependencies
|
||||
uses: ./.github/actions/setup-deps
|
||||
with:
|
||||
llvm-version: ${{matrix.llvm}}
|
||||
- name: Download model artifact
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: llama2-model
|
||||
path: ./_demo/llama2-c/
|
||||
- name: Install further optional dependencies for demos
|
||||
run: |
|
||||
py_deps=(
|
||||
numpy # for github.com/goplus/lib/py/numpy
|
||||
torch # for github.com/goplus/lib/py/torch
|
||||
)
|
||||
pip3.12 install --break-system-packages "${py_deps[@]}"
|
||||
|
||||
- name: Set up Go for build
|
||||
uses: ./.github/actions/setup-go
|
||||
with:
|
||||
go-version: "1.24.2"
|
||||
|
||||
- name: Install
|
||||
run: |
|
||||
go install ./...
|
||||
echo "LLGO_ROOT=$GITHUB_WORKSPACE" >> $GITHUB_ENV
|
||||
|
||||
- name: Set up Go for testing
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{matrix.go}}
|
||||
|
||||
- name: _xtool build tests
|
||||
run: |
|
||||
cd _xtool
|
||||
llgo build -v ./...
|
||||
|
||||
- name: Test demos
|
||||
run: |
|
||||
# TODO(lijie): force python3-embed to be linked with python-3.12-embed
|
||||
# Currently, python3-embed is python-3.13-embed, doesn't work with pytorch
|
||||
# Will remove this after pytorch is fixed.
|
||||
pcdir=$HOME/pc
|
||||
mkdir -p $pcdir
|
||||
libdir=$(pkg-config --variable=libdir python-3.12-embed)
|
||||
echo "libdir: $libdir"
|
||||
ln -s $libdir/pkgconfig/python-3.12-embed.pc $pcdir/python3-embed.pc
|
||||
export PKG_CONFIG_PATH=$pcdir
|
||||
bash .github/workflows/test_demo.sh
|
||||
|
||||
- name: Show test result
|
||||
run: cat result.md
|
||||
|
||||
- name: LLDB tests
|
||||
if: ${{startsWith(matrix.os, 'macos')}}
|
||||
run: |
|
||||
echo "Test lldb with llgo plugin on ${{matrix.os}} with LLVM ${{matrix.llvm}}"
|
||||
bash _lldb/runtest.sh -v
|
||||
|
||||
test:
|
||||
continue-on-error: true
|
||||
strategy:
|
||||
matrix:
|
||||
os:
|
||||
- macos-latest
|
||||
- ubuntu-24.04
|
||||
llvm: [19]
|
||||
go: ["1.24.2"]
|
||||
runs-on: ${{matrix.os}}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install dependencies
|
||||
uses: ./.github/actions/setup-deps
|
||||
with:
|
||||
llvm-version: ${{matrix.llvm}}
|
||||
- name: Install further optional dependencies for demos
|
||||
run: |
|
||||
py_deps=(
|
||||
numpy # for github.com/goplus/lib/py/numpy
|
||||
torch # for github.com/goplus/lib/py/torch
|
||||
)
|
||||
pip3.12 install --break-system-packages "${py_deps[@]}"
|
||||
|
||||
- name: Set up Go for build
|
||||
uses: ./.github/actions/setup-go
|
||||
with:
|
||||
go-version: "1.24.2"
|
||||
|
||||
- name: Install
|
||||
run: |
|
||||
go install ./...
|
||||
echo "LLGO_ROOT=$GITHUB_WORKSPACE" >> $GITHUB_ENV
|
||||
|
||||
- name: Set up Go for testing
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{matrix.go}}
|
||||
|
||||
- name: run llgo test
|
||||
run: |
|
||||
llgo test ./...
|
||||
|
||||
hello:
|
||||
continue-on-error: true
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-24.04, macos-latest]
|
||||
llvm: [19]
|
||||
go: ["1.21.13", "1.22.12", "1.23.6", "1.24.2"]
|
||||
runs-on: ${{matrix.os}}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install dependencies
|
||||
uses: ./.github/actions/setup-deps
|
||||
with:
|
||||
llvm-version: ${{matrix.llvm}}
|
||||
|
||||
- name: Set up Go 1.23 for building llgo
|
||||
uses: ./.github/actions/setup-go
|
||||
with:
|
||||
go-version: "1.24.2"
|
||||
|
||||
- name: Install llgo
|
||||
run: |
|
||||
go install ./...
|
||||
echo "LLGO_ROOT=$GITHUB_WORKSPACE" >> $GITHUB_ENV
|
||||
|
||||
- name: Set up Go for testing
|
||||
uses: ./.github/actions/setup-go
|
||||
with:
|
||||
go-version: ${{matrix.go}}
|
||||
|
||||
- name: Test Hello World with go.mod 1.21
|
||||
if: startsWith(matrix.go, '1.21') || startsWith(matrix.go, '1.22') || startsWith(matrix.go, '1.23') || startsWith(matrix.go, '1.24')
|
||||
uses: ./.github/actions/test-helloworld
|
||||
with:
|
||||
go-version: ${{matrix.go}}
|
||||
mod-version: "1.21"
|
||||
|
||||
- name: Test Hello World with go.mod 1.22
|
||||
if: startsWith(matrix.go, '1.22') || startsWith(matrix.go, '1.23') || startsWith(matrix.go, '1.24')
|
||||
uses: ./.github/actions/test-helloworld
|
||||
with:
|
||||
go-version: ${{matrix.go}}
|
||||
mod-version: "1.22"
|
||||
|
||||
- name: Test Hello World with go.mod 1.23
|
||||
if: startsWith(matrix.go, '1.23') || startsWith(matrix.go, '1.24')
|
||||
uses: ./.github/actions/test-helloworld
|
||||
with:
|
||||
go-version: ${{matrix.go}}
|
||||
mod-version: "1.23"
|
||||
|
||||
- name: Test Hello World with go.mod 1.24
|
||||
if: startsWith(matrix.go, '1.24')
|
||||
uses: ./.github/actions/test-helloworld
|
||||
with:
|
||||
go-version: ${{matrix.go}}
|
||||
mod-version: "1.24"
|
||||
|
||||
cross-compile:
|
||||
continue-on-error: true
|
||||
strategy:
|
||||
matrix:
|
||||
os: [macos-latest]
|
||||
llvm: [19]
|
||||
runs-on: ${{matrix.os}}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install dependencies
|
||||
uses: ./.github/actions/setup-deps
|
||||
with:
|
||||
llvm-version: ${{matrix.llvm}}
|
||||
|
||||
- name: Set up Go for building llgo
|
||||
uses: ./.github/actions/setup-go
|
||||
with:
|
||||
go-version: "1.24.2"
|
||||
|
||||
- name: Install wamr
|
||||
run: |
|
||||
git clone https://github.com/bytecodealliance/wasm-micro-runtime.git
|
||||
mkdir wasm-micro-runtime/product-mini/platforms/darwin/build
|
||||
cd wasm-micro-runtime/product-mini/platforms/darwin/build
|
||||
cmake -D WAMR_BUILD_EXCE_HANDLING=1 -D WAMR_BUILD_FAST_INTERP=0 -DWAMR_BUILD_SHARED_MEMORY=1 -DWAMR_BUILD_LIB_WASI_THREADS=1 -DWAMR_BUILD_LIB_PTHREAD=1 -DCMAKE_BUILD_TYPE=Debug -DWAMR_BUILD_DEBUG_INTERP=1 ..
|
||||
make -j8
|
||||
echo "$PWD" >> $GITHUB_PATH
|
||||
|
||||
- name: Install llgo
|
||||
run: |
|
||||
go install ./...
|
||||
echo "LLGO_ROOT=$GITHUB_WORKSPACE" >> $GITHUB_ENV
|
||||
|
||||
- name: Test Cross Compilation (wasm)
|
||||
shell: bash
|
||||
working-directory: _demo
|
||||
run: |
|
||||
echo "Testing cross-compilation wasm with Go 1.24.2"
|
||||
|
||||
# Compile for wasm architecture
|
||||
GOOS=wasip1 GOARCH=wasm llgo build -o hello -tags=nogc -v ./helloc
|
||||
|
||||
# Check file type
|
||||
file hello.wasm
|
||||
|
||||
# Run the wasm binary using llgo_wasm
|
||||
iwasm --stack-size=819200000 --heap-size=800000000 hello.wasm
|
||||
6
.github/workflows/populate_darwin_sysroot.sh
vendored
6
.github/workflows/populate_darwin_sysroot.sh
vendored
@@ -6,12 +6,12 @@ TMPDIR="$(mktemp -d)"
|
||||
export TMPDIR
|
||||
trap 'rm -rf "${TMPDIR}"' EXIT
|
||||
|
||||
DARWIN_AMD64_LLVM_PREFIX=.sysroot/darwin/amd64/usr/local/opt/llvm@18
|
||||
DARWIN_ARM64_LLVM_PREFIX=.sysroot/darwin/arm64/opt/homebrew/opt/llvm@18
|
||||
DARWIN_AMD64_LLVM_PREFIX=.sysroot/darwin/amd64/usr/local/opt/llvm@19
|
||||
DARWIN_ARM64_LLVM_PREFIX=.sysroot/darwin/arm64/opt/homebrew/opt/llvm@19
|
||||
mkdir -p "${DARWIN_AMD64_LLVM_PREFIX}" "${DARWIN_ARM64_LLVM_PREFIX}"
|
||||
|
||||
BREW_LLVM_FORMULA_JSON="$(mktemp)"
|
||||
curl -fsSL https://formulae.brew.sh/api/formula/llvm.json > "${BREW_LLVM_FORMULA_JSON}"
|
||||
curl -fsSL https://formulae.brew.sh/api/formula/llvm@19.json > "${BREW_LLVM_FORMULA_JSON}"
|
||||
BREW_LLVM_AMD64_BOTTLE_URL=$(jq -r '.bottle.stable.files.sonoma.url' "${BREW_LLVM_FORMULA_JSON}")
|
||||
BREW_LLVM_ARM64_BOTTLE_URL=$(jq -r '.bottle.stable.files.arm64_sonoma.url' "${BREW_LLVM_FORMULA_JSON}")
|
||||
curl -fsSL -H "Authorization: Bearer QQ==" "${BREW_LLVM_AMD64_BOTTLE_URL}" | tar -xzf - --strip-components=2 -C "${DARWIN_AMD64_LLVM_PREFIX}"
|
||||
|
||||
12
.github/workflows/populate_linux_sysroot.sh
vendored
12
.github/workflows/populate_linux_sysroot.sh
vendored
@@ -19,10 +19,10 @@ export DEBIAN_FRONTEND=noninteractive
|
||||
apt-get update
|
||||
apt-get install -y lsb-release gnupg2 wget rsync
|
||||
|
||||
echo "deb http://apt.llvm.org/\$(lsb_release -cs)/ llvm-toolchain-\$(lsb_release -cs)-18 main" | tee /etc/apt/sources.list.d/llvm.list
|
||||
echo "deb http://apt.llvm.org/\$(lsb_release -cs)/ llvm-toolchain-\$(lsb_release -cs)-19 main" | tee /etc/apt/sources.list.d/llvm.list
|
||||
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add -
|
||||
apt-get update
|
||||
apt-get install -y llvm-18-dev
|
||||
apt-get install -y llvm-19-dev
|
||||
|
||||
error() {
|
||||
echo -e "\$1" >&2
|
||||
@@ -106,7 +106,6 @@ do-sync() {
|
||||
args+=(-d)
|
||||
args+=(-h)
|
||||
args+=(--keep-dirlinks)
|
||||
args+=("--info=progress2")
|
||||
args+=(--delete)
|
||||
args+=(--prune-empty-dirs)
|
||||
args+=(--sparse)
|
||||
@@ -139,5 +138,8 @@ populate_linux_sysroot() {
|
||||
debian:bullseye \
|
||||
/populate_linux_sysroot.sh
|
||||
}
|
||||
populate_linux_sysroot amd64 "${LINUX_AMD64_PREFIX}"
|
||||
populate_linux_sysroot arm64 "${LINUX_ARM64_PREFIX}"
|
||||
populate_linux_sysroot amd64 "${LINUX_AMD64_PREFIX}" &
|
||||
populate_linux_sysroot arm64 "${LINUX_ARM64_PREFIX}" &
|
||||
|
||||
# Wait for both background processes to complete
|
||||
wait
|
||||
|
||||
4
.github/workflows/release-build.yml
vendored
4
.github/workflows/release-build.yml
vendored
@@ -32,9 +32,11 @@ jobs:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: 1.20.x
|
||||
go-version: 1.24.x
|
||||
- name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@v3
|
||||
with:
|
||||
image: tonistiigi/binfmt:qemu-v7.0.0-28
|
||||
- name: Download Darwin sysroot tarball
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
|
||||
6
.github/workflows/test_demo.sh
vendored
6
.github/workflows/test_demo.sh
vendored
@@ -1,13 +1,13 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
# llgo run subdirectories under _demo and _pydemo
|
||||
# llgo run subdirectories under _demo and _pydemo that contain *.go files
|
||||
total=0
|
||||
failed=0
|
||||
failed_cases=""
|
||||
for d in ./_demo/* ./_pydemo/*; do
|
||||
total=$((total+1))
|
||||
if [ -d "$d" ]; then
|
||||
if [ -d "$d" ] && [ -n "$(ls "$d"/*.go 2>/dev/null)" ]; then
|
||||
total=$((total+1))
|
||||
echo "Testing $d"
|
||||
if ! (cd "$d" && llgo run .); then
|
||||
echo "FAIL"
|
||||
|
||||
36
.github/workflows/test_llgo.sh
vendored
36
.github/workflows/test_llgo.sh
vendored
@@ -1,36 +0,0 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
testcmd=/tmp/test
|
||||
llgo build -o $testcmd ./c/bdwgc/_test
|
||||
cases=$($testcmd)
|
||||
total=$(echo "$cases" | wc -l | tr -d ' ')
|
||||
failed=0
|
||||
failed_cases=""
|
||||
|
||||
for idx in $(seq 1 $((total))); do
|
||||
case=$(echo "$cases" | sed -n "${idx}p")
|
||||
case_name=$(echo "$case" | cut -d',' -f2)
|
||||
echo "=== Test case: $case_name"
|
||||
set +e
|
||||
out=$("$testcmd" "$((idx-1))" 2>&1)
|
||||
exit_code=$?
|
||||
set -e
|
||||
if [ "${exit_code:-0}" -ne 0 ]; then
|
||||
echo "failed: $out"
|
||||
failed=$((failed+1))
|
||||
failed_cases="$failed_cases\n* :x: $case_name"
|
||||
else
|
||||
echo "passed"
|
||||
fi
|
||||
done
|
||||
echo "=== Done"
|
||||
echo "$((total-failed))/$total tests passed"
|
||||
|
||||
if [ "$failed" -ne 0 ]; then
|
||||
echo ":bangbang: Failed llgo cases:" | tee -a result.md
|
||||
echo -e "$failed_cases" | tee -a result.md
|
||||
exit 1
|
||||
else
|
||||
echo ":white_check_mark: All llgo tests passed" | tee -a result.md
|
||||
fi
|
||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -22,10 +22,14 @@ _tinygo/
|
||||
_output/
|
||||
build.dir/
|
||||
.vscode/
|
||||
.venv/
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Debug symbols
|
||||
*.dSYM
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
*.swp
|
||||
|
||||
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -1,3 +0,0 @@
|
||||
[submodule "c/llama2/llama2.c"]
|
||||
path = c/llama2/llama2.c
|
||||
url = https://github.com/karpathy/llama2.c.git
|
||||
|
||||
@@ -17,65 +17,69 @@ before:
|
||||
builds:
|
||||
- id: llgo-darwin-amd64
|
||||
main: ./cmd/llgo
|
||||
binary: bin/llgo
|
||||
flags:
|
||||
- -tags=darwin,amd64,byollvm
|
||||
ldflags:
|
||||
- -X github.com/goplus/llgo/x/env.buildVersion=v{{.Version}}
|
||||
- -X github.com/goplus/llgo/x/env.buildTime={{.Date}}
|
||||
- -X github.com/goplus/llgo/xtool/env/llvm.ldLLVMConfigBin=/usr/local/opt/llvm@18/bin/llvm-config
|
||||
- -X github.com/goplus/llgo/internal/env.buildVersion=v{{.Version}}
|
||||
- -X github.com/goplus/llgo/internal/env.buildTime={{.Date}}
|
||||
- -X github.com/goplus/llgo/xtool/env/llvm.ldLLVMConfigBin=/usr/local/opt/llvm@19/bin/llvm-config
|
||||
env:
|
||||
- CC=o64-clang
|
||||
- CXX=o64-clang++
|
||||
- CGO_CPPFLAGS=-I{{.Env.SYSROOT_DARWIN_AMD64}}/usr/local/opt/llvm@18/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
|
||||
- CGO_LDFLAGS=-L{{.Env.SYSROOT_DARWIN_AMD64}}/usr/local/opt/llvm@18/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM-18 -lz -lm
|
||||
- CGO_CPPFLAGS=-I{{.Env.SYSROOT_DARWIN_AMD64}}/usr/local/opt/llvm@19/include -mmacosx-version-min=10.13 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
|
||||
- CGO_LDFLAGS=-L{{.Env.SYSROOT_DARWIN_AMD64}}/usr/local/opt/llvm@19/lib -mmacosx-version-min=10.13 -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM-19 -lz -lm
|
||||
targets:
|
||||
- darwin_amd64
|
||||
mod_timestamp: "{{.CommitTimestamp}}"
|
||||
- id: llgo-darwin-arm64
|
||||
main: ./cmd/llgo
|
||||
binary: bin/llgo
|
||||
flags:
|
||||
- -tags=darwin,arm64,byollvm
|
||||
ldflags:
|
||||
- -X github.com/goplus/llgo/x/env.buildVersion=v{{.Version}}
|
||||
- -X github.com/goplus/llgo/x/env.buildTime={{.Date}}
|
||||
- -X github.com/goplus/llgo/xtool/env/llvm.ldLLVMConfigBin=/opt/homebrew/opt/llvm@18/bin/llvm-config
|
||||
- -X github.com/goplus/llgo/internal/env.buildVersion=v{{.Version}}
|
||||
- -X github.com/goplus/llgo/internal/env.buildTime={{.Date}}
|
||||
- -X github.com/goplus/llgo/xtool/env/llvm.ldLLVMConfigBin=/opt/homebrew/opt/llvm@19/bin/llvm-config
|
||||
env:
|
||||
- CC=oa64-clang
|
||||
- CXX=oa64-clang++
|
||||
- CGO_CPPFLAGS=-I{{.Env.SYSROOT_DARWIN_ARM64}}/opt/homebrew/opt/llvm@18/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
|
||||
- CGO_LDFLAGS=-L{{.Env.SYSROOT_DARWIN_ARM64}}/opt/homebrew/opt/llvm@18/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM-18 -lz -lm
|
||||
- CGO_CPPFLAGS=-I{{.Env.SYSROOT_DARWIN_ARM64}}/opt/homebrew/opt/llvm@19/include -mmacosx-version-min=10.13 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
|
||||
- CGO_LDFLAGS=-L{{.Env.SYSROOT_DARWIN_ARM64}}/opt/homebrew/opt/llvm@19/lib -mmacosx-version-min=10.13 -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM-19 -lz -lm
|
||||
targets:
|
||||
- darwin_arm64
|
||||
mod_timestamp: "{{.CommitTimestamp}}"
|
||||
- id: llgo-linux-amd64
|
||||
main: ./cmd/llgo
|
||||
binary: bin/llgo
|
||||
flags:
|
||||
- -tags=linux,amd64,byollvm
|
||||
ldflags:
|
||||
- -X github.com/goplus/llgo/x/env.buildVersion=v{{.Version}}
|
||||
- -X github.com/goplus/llgo/x/env.buildTime={{.Date}}
|
||||
- -X github.com/goplus/llgo/xtool/env/llvm.ldLLVMConfigBin=/usr/lib/llvm-18/bin/llvm-config
|
||||
- -X github.com/goplus/llgo/internal/env.buildVersion=v{{.Version}}
|
||||
- -X github.com/goplus/llgo/internal/env.buildTime={{.Date}}
|
||||
- -X github.com/goplus/llgo/xtool/env/llvm.ldLLVMConfigBin=/usr/lib/llvm-19/bin/llvm-config
|
||||
env:
|
||||
- CC=x86_64-linux-gnu-gcc
|
||||
- CXX=x86_64-linux-gnu-g++
|
||||
- CGO_CPPFLAGS=--sysroot={{.Env.SYSROOT_LINUX_AMD64}} -I{{.Env.SYSROOT_LINUX_AMD64}}/usr/include/llvm-18 -I{{.Env.SYSROOT_LINUX_AMD64}}/usr/include/llvm-c-18 -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
|
||||
- CGO_LDFLAGS=--sysroot={{.Env.SYSROOT_LINUX_AMD64}} -L{{.Env.SYSROOT_LINUX_AMD64}}/usr/lib/llvm-18/lib -lLLVM-18
|
||||
- CGO_CPPFLAGS=--sysroot={{.Env.SYSROOT_LINUX_AMD64}} -I{{.Env.SYSROOT_LINUX_AMD64}}/usr/include/llvm-19 -I{{.Env.SYSROOT_LINUX_AMD64}}/usr/include/llvm-c-19 -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
|
||||
- CGO_LDFLAGS=--sysroot={{.Env.SYSROOT_LINUX_AMD64}} -L{{.Env.SYSROOT_LINUX_AMD64}}/usr/lib/llvm-19/lib -lLLVM-19
|
||||
targets:
|
||||
- linux_amd64
|
||||
mod_timestamp: "{{.CommitTimestamp}}"
|
||||
- id: llgo-linux-arm64
|
||||
main: ./cmd/llgo
|
||||
binary: bin/llgo
|
||||
flags:
|
||||
- -tags=linux,arm64,byollvm
|
||||
ldflags:
|
||||
- -X github.com/goplus/llgo/x/env.buildVersion=v{{.Version}}
|
||||
- -X github.com/goplus/llgo/x/env.buildTime={{.Date}}
|
||||
- -X github.com/goplus/llgo/xtool/env/llvm.ldLLVMConfigBin=/usr/lib/llvm-18/bin/llvm-config
|
||||
- -X github.com/goplus/llgo/internal/env.buildVersion=v{{.Version}}
|
||||
- -X github.com/goplus/llgo/internal/env.buildTime={{.Date}}
|
||||
- -X github.com/goplus/llgo/xtool/env/llvm.ldLLVMConfigBin=/usr/lib/llvm-19/bin/llvm-config
|
||||
env:
|
||||
- CC=aarch64-linux-gnu-gcc
|
||||
- CXX=aarch64-linux-gnu-g++
|
||||
- CGO_CPPFLAGS=--sysroot={{.Env.SYSROOT_LINUX_ARM64}} -I{{.Env.SYSROOT_LINUX_ARM64}}/usr/include/llvm-18 -I{{.Env.SYSROOT_LINUX_ARM64}}/usr/include/llvm-c-18 -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
|
||||
- CGO_LDFLAGS=--sysroot={{.Env.SYSROOT_LINUX_ARM64}} -L{{.Env.SYSROOT_LINUX_ARM64}}/usr/lib/llvm-18/lib -lLLVM-18
|
||||
- CGO_CPPFLAGS=--sysroot={{.Env.SYSROOT_LINUX_ARM64}} -I{{.Env.SYSROOT_LINUX_ARM64}}/usr/include/llvm-19 -I{{.Env.SYSROOT_LINUX_ARM64}}/usr/include/llvm-c-19 -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
|
||||
- CGO_LDFLAGS=--sysroot={{.Env.SYSROOT_LINUX_ARM64}} -L{{.Env.SYSROOT_LINUX_ARM64}}/usr/lib/llvm-19/lib -lLLVM-19
|
||||
targets:
|
||||
- linux_arm64
|
||||
mod_timestamp: "{{.CommitTimestamp}}"
|
||||
@@ -88,6 +92,7 @@ archives:
|
||||
files:
|
||||
- LICENSE
|
||||
- README.md
|
||||
- runtime
|
||||
|
||||
checksum:
|
||||
name_template: "{{.ProjectName}}{{.Version}}.checksums.txt"
|
||||
|
||||
186
README.md
186
README.md
@@ -24,33 +24,35 @@ How can these be achieved?
|
||||
LLGo := Go + C + Python
|
||||
```
|
||||
|
||||
LLGo is compatible with C and Python through the language's **Application Binary Interface (ABI)**, while LLGo is compatible with Go through its **syntax (source code)**.
|
||||
LLGo is compatible with C and Python through the language's **Application Binary Interface (ABI)**, while LLGo is compatible with Go through its **syntax (source code)**. And here C doesn't just include C, but all languages that are ABI compatible with C, including C/C++, Objective-C, Swift, etc.
|
||||
|
||||
|
||||
## C/C++ standard libary support
|
||||
|
||||
You can import a C/C++ standard library in LLGo!
|
||||
|
||||
* [c](https://pkg.go.dev/github.com/goplus/llgo/c)
|
||||
* [c/syscall](https://pkg.go.dev/github.com/goplus/llgo/c/syscall)
|
||||
* [c/sys](https://pkg.go.dev/github.com/goplus/llgo/c/sys)
|
||||
* [c/os](https://pkg.go.dev/github.com/goplus/llgo/c/os)
|
||||
* [c/math](https://pkg.go.dev/github.com/goplus/llgo/c/math)
|
||||
* [c/math/cmplx](https://pkg.go.dev/github.com/goplus/llgo/c/math/cmplx)
|
||||
* [c/math/rand](https://pkg.go.dev/github.com/goplus/llgo/c/math/rand)
|
||||
* [c/pthread](https://pkg.go.dev/github.com/goplus/llgo/c/pthread)
|
||||
* [c/pthread/sync](https://pkg.go.dev/github.com/goplus/llgo/c/pthread/sync)
|
||||
* [c/sync/atomic](https://pkg.go.dev/github.com/goplus/llgo/c/sync/atomic)
|
||||
* [c/time](https://pkg.go.dev/github.com/goplus/llgo/c/time)
|
||||
* [c/net](https://pkg.go.dev/github.com/goplus/llgo/c/net)
|
||||
* [cpp/std](https://pkg.go.dev/github.com/goplus/llgo/cpp/std)
|
||||
* [c](https://pkg.go.dev/github.com/goplus/lib/c)
|
||||
* [c/syscall](https://pkg.go.dev/github.com/goplus/lib/c/syscall)
|
||||
* [c/sys](https://pkg.go.dev/github.com/goplus/lib/c/sys)
|
||||
* [c/os](https://pkg.go.dev/github.com/goplus/lib/c/os)
|
||||
* [c/math](https://pkg.go.dev/github.com/goplus/lib/c/math)
|
||||
* [c/math/cmplx](https://pkg.go.dev/github.com/goplus/lib/c/math/cmplx)
|
||||
* [c/math/rand](https://pkg.go.dev/github.com/goplus/lib/c/math/rand)
|
||||
* [c/pthread](https://pkg.go.dev/github.com/goplus/lib/c/pthread)
|
||||
* [c/pthread/sync](https://pkg.go.dev/github.com/goplus/lib/c/pthread/sync)
|
||||
* [c/sync/atomic](https://pkg.go.dev/github.com/goplus/lib/c/sync/atomic)
|
||||
* [c/time](https://pkg.go.dev/github.com/goplus/lib/c/time)
|
||||
* [c/net](https://pkg.go.dev/github.com/goplus/lib/c/net)
|
||||
* [cpp/std](https://pkg.go.dev/github.com/goplus/lib/cpp/std)
|
||||
|
||||
Here is a simple example:
|
||||
|
||||
<!-- embedme doc/_readme/llgo_simple/simple.go -->
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import "github.com/goplus/llgo/c"
|
||||
import "github.com/goplus/lib/c"
|
||||
|
||||
func main() {
|
||||
c.Printf(c.Str("Hello world\n"))
|
||||
@@ -73,10 +75,12 @@ llgo run .
|
||||
```
|
||||
|
||||
|
||||
## How support C/C++ and Python
|
||||
## How to support C/C++ and Python
|
||||
|
||||
LLGo use `go:linkname` to link an extern symbol througth its ABI:
|
||||
|
||||
<!-- embedme doc/_readme/llgo_call_c/call_c.go#L3-L6 -->
|
||||
|
||||
```go
|
||||
import _ "unsafe" // for go:linkname
|
||||
|
||||
@@ -86,6 +90,8 @@ func Sqrt(x float64) float64
|
||||
|
||||
You can directly integrate it into [your own code](_demo/linkname/linkname.go):
|
||||
|
||||
<!-- embedme doc/_readme/llgo_call_c/call_c.go -->
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@@ -99,12 +105,14 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
Or put it into a package (see [c/math](c/math/math.go)):
|
||||
Or put it into a package (see [c/math](https://github.com/goplus/lib/tree/main/c/math/math.go)):
|
||||
|
||||
<!-- embedme doc/_readme/llgo_call_cmath/call_cmath.go -->
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import "github.com/goplus/llgo/c/math"
|
||||
import "github.com/goplus/lib/c/math"
|
||||
|
||||
func main() {
|
||||
println("sqrt(2) =", math.Sqrt(2))
|
||||
@@ -118,30 +126,32 @@ You can import a Python library in LLGo!
|
||||
|
||||
And you can import any Python library into `llgo` through a program called `llpyg` (see [Development tools](#development-tools)). The following libraries have been included in `llgo`:
|
||||
|
||||
* [py](https://pkg.go.dev/github.com/goplus/llgo/py) (abi)
|
||||
* [py/std](https://pkg.go.dev/github.com/goplus/llgo/py/std) (builtins)
|
||||
* [py/sys](https://pkg.go.dev/github.com/goplus/llgo/py/sys)
|
||||
* [py/os](https://pkg.go.dev/github.com/goplus/llgo/py/os)
|
||||
* [py/math](https://pkg.go.dev/github.com/goplus/llgo/py/math)
|
||||
* [py/json](https://pkg.go.dev/github.com/goplus/llgo/py/json)
|
||||
* [py/inspect](https://pkg.go.dev/github.com/goplus/llgo/py/inspect)
|
||||
* [py/statistics](https://pkg.go.dev/github.com/goplus/llgo/py/statistics)
|
||||
* [py/numpy](https://pkg.go.dev/github.com/goplus/llgo/py/numpy)
|
||||
* [py/pandas](https://pkg.go.dev/github.com/goplus/llgo/py/pandas)
|
||||
* [py/torch](https://pkg.go.dev/github.com/goplus/llgo/py/torch)
|
||||
* [py/matplotlib](https://pkg.go.dev/github.com/goplus/llgo/py/matplotlib)
|
||||
* [py](https://pkg.go.dev/github.com/goplus/lib/py) (abi)
|
||||
* [py/std](https://pkg.go.dev/github.com/goplus/lib/py/std) (builtins)
|
||||
* [py/sys](https://pkg.go.dev/github.com/goplus/lib/py/sys)
|
||||
* [py/os](https://pkg.go.dev/github.com/goplus/lib/py/os)
|
||||
* [py/math](https://pkg.go.dev/github.com/goplus/lib/py/math)
|
||||
* [py/json](https://pkg.go.dev/github.com/goplus/lib/py/json)
|
||||
* [py/inspect](https://pkg.go.dev/github.com/goplus/lib/py/inspect)
|
||||
* [py/statistics](https://pkg.go.dev/github.com/goplus/lib/py/statistics)
|
||||
* [py/numpy](https://pkg.go.dev/github.com/goplus/lib/py/numpy)
|
||||
* [py/pandas](https://pkg.go.dev/github.com/goplus/lib/py/pandas)
|
||||
* [py/torch](https://pkg.go.dev/github.com/goplus/lib/py/torch)
|
||||
* [py/matplotlib](https://pkg.go.dev/github.com/goplus/lib/py/matplotlib)
|
||||
|
||||
Note: For third-party libraries (such as pandas and pytorch), you still need to install the library files.
|
||||
|
||||
Here is an example:
|
||||
|
||||
<!-- embedme doc/_readme/llgo_call_py/call_py.go -->
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/py"
|
||||
"github.com/goplus/llgo/py/math"
|
||||
"github.com/goplus/llgo/py/std"
|
||||
"github.com/goplus/lib/py"
|
||||
"github.com/goplus/lib/py/math"
|
||||
"github.com/goplus/lib/py/std"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -152,6 +162,8 @@ func main() {
|
||||
|
||||
It is equivalent to the following Python code:
|
||||
|
||||
<!-- embedme doc/_readme/llgo_call_py/call_math.py -->
|
||||
|
||||
```py
|
||||
import math
|
||||
|
||||
@@ -163,13 +175,15 @@ Here, We call `py.Float(2)` to create a Python number 2, and pass it to Python
|
||||
|
||||
Let's look at a slightly more complex example. For example, we use `numpy` to calculate:
|
||||
|
||||
<!-- embedme doc/_readme/llgo_py_list/py_list.go -->
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/py"
|
||||
"github.com/goplus/llgo/py/numpy"
|
||||
"github.com/goplus/llgo/py/std"
|
||||
"github.com/goplus/lib/py"
|
||||
"github.com/goplus/lib/py/numpy"
|
||||
"github.com/goplus/lib/py/std"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -211,31 +225,32 @@ LLGo can easily import any libraries from the C ecosystem. Currently, this impor
|
||||
|
||||
The currently supported libraries include:
|
||||
|
||||
* [c/bdwgc](https://pkg.go.dev/github.com/goplus/llgo/c/bdwgc)
|
||||
* [c/cjson](https://pkg.go.dev/github.com/goplus/llgo/c/cjson)
|
||||
* [c/clang](https://pkg.go.dev/github.com/goplus/llgo/c/clang)
|
||||
* [c/libuv](https://pkg.go.dev/github.com/goplus/llgo/c/libuv)
|
||||
* [c/llama2](https://pkg.go.dev/github.com/goplus/llgo/c/llama2)
|
||||
* [c/lua](https://pkg.go.dev/github.com/goplus/llgo/c/lua)
|
||||
* [c/neco](https://pkg.go.dev/github.com/goplus/llgo/c/neco)
|
||||
* [c/openssl](https://pkg.go.dev/github.com/goplus/llgo/c/openssl)
|
||||
* [c/raylib](https://pkg.go.dev/github.com/goplus/llgo/c/raylib)
|
||||
* [c/sqlite](https://pkg.go.dev/github.com/goplus/llgo/c/sqlite)
|
||||
* [c/zlib](https://pkg.go.dev/github.com/goplus/llgo/c/zlib)
|
||||
* [cpp/inih](https://pkg.go.dev/github.com/goplus/llgo/cpp/inih)
|
||||
* [cpp/llvm](https://pkg.go.dev/github.com/goplus/llgo/cpp/llvm)
|
||||
* [c/bdwgc](https://pkg.go.dev/github.com/goplus/lib/c/bdwgc)
|
||||
* [c/cjson](https://pkg.go.dev/github.com/goplus/lib/c/cjson)
|
||||
* [c/clang](https://pkg.go.dev/github.com/goplus/lib/c/clang)
|
||||
* [c/ffi](https://pkg.go.dev/github.com/goplus/lib/c/ffi)
|
||||
* [c/libuv](https://pkg.go.dev/github.com/goplus/lib/c/libuv)
|
||||
* [c/llama2](https://pkg.go.dev/github.com/goplus/lib/c/llama2)
|
||||
* [c/lua](https://pkg.go.dev/github.com/goplus/lib/c/lua)
|
||||
* [c/neco](https://pkg.go.dev/github.com/goplus/lib/c/neco)
|
||||
* [c/openssl](https://pkg.go.dev/github.com/goplus/lib/c/openssl)
|
||||
* [c/raylib](https://pkg.go.dev/github.com/goplus/lib/c/raylib)
|
||||
* [c/sqlite](https://pkg.go.dev/github.com/goplus/lib/c/sqlite)
|
||||
* [c/zlib](https://pkg.go.dev/github.com/goplus/lib/c/zlib)
|
||||
* [cpp/inih](https://pkg.go.dev/github.com/goplus/lib/cpp/inih)
|
||||
* [cpp/llvm](https://pkg.go.dev/github.com/goplus/lib/cpp/llvm)
|
||||
|
||||
Here are some examples related to them:
|
||||
|
||||
* [llama2-c](_demo/llama2-c): inference Llama 2 (It's the first llgo AI example)
|
||||
* [mkjson](c/cjson/_demo/mkjson/mkjson.go): create a json object and print it
|
||||
* [sqlitedemo](c/sqlite/_demo/sqlitedemo/demo.go): a basic sqlite demo
|
||||
* [tetris](c/raylib/_demo/tetris/tetris.go): a tetris game based on raylib
|
||||
* [mkjson](https://github.com/goplus/lib/tree/main/c/cjson/_demo/mkjson/mkjson.go): create a json object and print it
|
||||
* [sqlitedemo](https://github.com/goplus/lib/tree/main/c/sqlite/_demo/sqlitedemo/demo.go): a basic sqlite demo
|
||||
* [tetris](https://github.com/goplus/lib/tree/main/c/raylib/_demo/tetris/tetris.go): a tetris game based on raylib
|
||||
|
||||
|
||||
## Go syntax support
|
||||
|
||||
All Go syntax (not including `cgo`) is already supported. Here are some examples:
|
||||
All Go syntax (including `cgo`) is already supported. Here are some examples:
|
||||
|
||||
* [concat](_demo/concat/concat.go): define a variadic function
|
||||
* [genints](_demo/genints/genints.go): various forms of closure usage (including C function, recv.method and anonymous function)
|
||||
@@ -320,19 +335,20 @@ Here are the Go packages that can be imported correctly:
|
||||
* [regexp/syntax](https://pkg.go.dev/regexp/syntax)
|
||||
* [go/token](https://pkg.go.dev/go/token)
|
||||
* [go/scanner](https://pkg.go.dev/go/scanner)
|
||||
* [go/parser](https://pkg.go.dev/go/parser)
|
||||
|
||||
|
||||
## Dependencies
|
||||
|
||||
- [Go 1.20+](https://go.dev)
|
||||
- [Go 1.21+](https://go.dev)
|
||||
- [LLVM 18](https://llvm.org)
|
||||
- [LLD 18](https://lld.llvm.org)
|
||||
- [Clang 18](https://clang.llvm.org)
|
||||
- [LLD 18](https://lld.llvm.org)
|
||||
- [pkg-config 0.29+](https://www.freedesktop.org/wiki/Software/pkg-config/)
|
||||
- [bdwgc/libgc 8.0+](https://www.hboehm.info/gc/)
|
||||
- [OpenSSL 3.0+](https://www.openssl.org/)
|
||||
- [zlib 1.2+](https://www.zlib.net)
|
||||
- [Python 3.12+](https://www.python.org) (optional, for [github.com/goplus/llgo/py](https://pkg.go.dev/github.com/goplus/llgo/py))
|
||||
- [Python 3.12+](https://www.python.org) (optional, for [github.com/goplus/lib/py](https://pkg.go.dev/github.com/goplus/lib/py))
|
||||
|
||||
## How to install
|
||||
|
||||
@@ -340,32 +356,63 @@ Follow these steps to generate the `llgo` command (its usage is the same as the
|
||||
|
||||
### on macOS
|
||||
|
||||
<!-- embedme doc/_readme/scripts/install_macos.sh#L2-L1000 -->
|
||||
|
||||
```sh
|
||||
brew update
|
||||
brew install llvm@18 pkg-config bdw-gc openssl
|
||||
brew install llvm@19 lld@19 bdw-gc openssl cjson libffi libuv pkg-config
|
||||
brew install python@3.12 # optional
|
||||
go install -v github.com/goplus/llgo/cmd/llgo@latest
|
||||
brew link --overwrite llvm@19 lld@19 libffi
|
||||
# curl https://raw.githubusercontent.com/goplus/llgo/refs/heads/main/install.sh | bash
|
||||
./install.sh
|
||||
```
|
||||
|
||||
### on Linux (Debian/Ubuntu)
|
||||
### on Linux
|
||||
|
||||
#### Debian/Ubuntu
|
||||
|
||||
<!-- embedme doc/_readme/scripts/install_ubuntu.sh#L2-L1000 -->
|
||||
|
||||
```sh
|
||||
echo "deb http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-18 main" | sudo tee /etc/apt/sources.list.d/llvm.list
|
||||
echo "deb http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-19 main" | sudo tee /etc/apt/sources.list.d/llvm.list
|
||||
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y llvm-18-dev clang-18 lld-18 pkg-config libgc-dev libssl-dev zlib1g-dev
|
||||
sudo apt-get install -y llvm-19-dev clang-19 libclang-19-dev lld-19 pkg-config libgc-dev libssl-dev zlib1g-dev libcjson-dev libsqlite3-dev libunwind-dev libuv1-dev
|
||||
sudo apt-get install -y python3.12-dev # optional
|
||||
go install -v github.com/goplus/llgo/cmd/llgo@latest
|
||||
#curl https://raw.githubusercontent.com/goplus/llgo/refs/heads/main/install.sh | bash
|
||||
./install.sh
|
||||
```
|
||||
|
||||
#### Alpine Linux
|
||||
|
||||
```sh
|
||||
apk add go llvm18-dev clang18-dev lld18 pkgconf gc-dev openssl-dev zlib-dev
|
||||
apk add python3-dev # optional
|
||||
apk add g++ # build only
|
||||
export LLVM_CONFIG=/usr/lib/llvm18/bin/llvm-config
|
||||
export CGO_CPPFLAGS="$($LLVM_CONFIG --cppflags)"
|
||||
export CGO_CXXFLAGS=-std=c++17
|
||||
export CGO_LDFLAGS="$($LLVM_CONFIG --ldflags) $($LLVM_CONFIG --libs all)"
|
||||
curl https://raw.githubusercontent.com/goplus/llgo/refs/heads/main/install.sh | bash
|
||||
```
|
||||
|
||||
### on Windows
|
||||
|
||||
TODO
|
||||
|
||||
### Install from source
|
||||
|
||||
<!-- embedme doc/_readme/scripts/install_llgo.sh#L2-L1000 -->
|
||||
|
||||
```sh
|
||||
git clone https://github.com/goplus/llgo.git
|
||||
cd llgo
|
||||
./install.sh
|
||||
```
|
||||
|
||||
## Development tools
|
||||
|
||||
* [pydump](chore/_xtool/pydump): It's the first program compiled by `llgo` (NOT `go`) in a production environment. It outputs symbol information (functions, variables, and constants) from a Python library in JSON format, preparing for the generation of corresponding packages in `llgo`.
|
||||
* [pydump](_xtool/pydump): It's the first program compiled by `llgo` (NOT `go`) in a production environment. It outputs symbol information (functions, variables, and constants) from a Python library in JSON format, preparing for the generation of corresponding packages in `llgo`.
|
||||
* [pysigfetch](https://github.com/goplus/hdq/tree/main/chore/pysigfetch): It generates symbol information by extracting information from Python's documentation site. This tool is not part of the `llgo` project, but we depend on it.
|
||||
* [llpyg](chore/llpyg): It is used to automatically convert Python libraries into Go packages that `llgo` can import. It depends on `pydump` and `pysigfetch` to accomplish the task.
|
||||
* [llgen](chore/llgen): It is used to compile Go packages into LLVM IR files (*.ll).
|
||||
@@ -373,19 +420,24 @@ TODO
|
||||
|
||||
How do I generate these tools?
|
||||
|
||||
<!-- embedme doc/_readme/scripts/install_full.sh#L2-L1000 -->
|
||||
|
||||
```sh
|
||||
git clone https://github.com/goplus/llgo.git
|
||||
cd llgo
|
||||
go install -v ./cmd/...
|
||||
go install -v ./chore/... # compile all tools except pydump
|
||||
cd chore/_xtool
|
||||
export LLGO_ROOT=$PWD
|
||||
cd _xtool
|
||||
llgo install ./... # compile pydump
|
||||
go install github.com/goplus/hdq/chore/pysigfetch@v0.8.1 # compile pysigfetch
|
||||
|
||||
```
|
||||
|
||||
## Key modules
|
||||
|
||||
Below are the key modules for understanding the implementation principles of `llgo`:
|
||||
|
||||
* [llgo/ssa](https://pkg.go.dev/github.com/goplus/llgo/ssa): It generates LLVM IR files (LLVM SSA) using the semantics (interfaces) of Go SSA. Although `LLVM SSA` and `Go SSA` are both IR languages, they work at completely different levels. `LLVM SSA` is closer to machine code, which abstracts different instruction sets. While `Go SSA` is closer to a high-level language. We can think of it as the instruction set of the `Go computer`. `llgo/ssa` is not just limited to the `llgo` compiler. If we view it as the high-level expressive power of `LLVM`, you'll find it very useful. Prior to `llgo/ssa`, you had to operate `LLVM` using machine code semantics. But now, with the advanced SSA form (in the semantics of Go SSA), you can conveniently utilize `LLVM`.
|
||||
* [llgo/cl](https://pkg.go.dev/github.com/goplus/llgo/cl): It is the core of the llgo compiler. It converts a Go package into LLVM IR files. It depends on `llgo/ssa`.
|
||||
* [llgo/internal/build](https://pkg.go.dev/github.com/goplus/llgo/internal/build): It strings together the entire compilation process of `llgo`. It depends on `llgo/ssa` and `llgo/cl`.
|
||||
* [ssa](https://pkg.go.dev/github.com/goplus/llgo/ssa): It generates LLVM IR files (LLVM SSA) using the semantics (interfaces) of Go SSA. Although `LLVM SSA` and `Go SSA` are both IR languages, they work at completely different levels. `LLVM SSA` is closer to machine code, which abstracts different instruction sets. While `Go SSA` is closer to a high-level language. We can think of it as the instruction set of the `Go computer`. `llgo/ssa` is not just limited to the `llgo` compiler. If we view it as the high-level expressive power of `LLVM`, you'll find it very useful. Prior to `llgo/ssa`, you had to operate `LLVM` using machine code semantics. But now, with the advanced SSA form (in the semantics of Go SSA), you can conveniently utilize `LLVM`.
|
||||
* [cl](https://pkg.go.dev/github.com/goplus/llgo/cl): It is the core of the llgo compiler. It converts a Go package into LLVM IR files. It depends on `llgo/ssa`.
|
||||
* [internal/build](https://pkg.go.dev/github.com/goplus/llgo/internal/build): It strings together the entire compilation process of `llgo`. It depends on `llgo/ssa` and `llgo/cl`.
|
||||
|
||||
33
_cmptest/_goconstdemo/goconst.go
Normal file
33
_cmptest/_goconstdemo/goconst.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/constant"
|
||||
"go/token"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create the complex number 2.3 + 5i.
|
||||
ar := constant.MakeFloat64(2.3)
|
||||
ai := constant.MakeImag(constant.MakeInt64(5))
|
||||
a := constant.BinaryOp(ar, token.ADD, ai)
|
||||
|
||||
// Compute (2.3 + 5i) * 11.
|
||||
b := constant.MakeUint64(11)
|
||||
c := constant.BinaryOp(a, token.MUL, b)
|
||||
|
||||
// Convert c into a complex128.
|
||||
Ar, exact := constant.Float64Val(constant.Real(c))
|
||||
if !exact {
|
||||
fmt.Printf("Could not represent real part %s exactly as float64\n", constant.Real(c))
|
||||
}
|
||||
Ai, exact := constant.Float64Val(constant.Imag(c))
|
||||
if !exact {
|
||||
fmt.Printf("Could not represent imaginary part %s as exactly as float64\n", constant.Imag(c))
|
||||
}
|
||||
C := complex(Ar, Ai)
|
||||
|
||||
fmt.Println("literal", 25.3+55i)
|
||||
fmt.Println("go/constant", c)
|
||||
fmt.Println("complex128", C)
|
||||
}
|
||||
25
_cmptest/mathbigdemo/big.go
Normal file
25
_cmptest/mathbigdemo/big.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Initialize two big ints with the first two numbers in the sequence.
|
||||
a := big.NewInt(0)
|
||||
b := big.NewInt(1)
|
||||
|
||||
// Initialize limit as 10^99, the smallest integer with 100 digits.
|
||||
var limit big.Int
|
||||
limit.Exp(big.NewInt(10), big.NewInt(99), nil)
|
||||
|
||||
// Loop while a is smaller than 1e100.
|
||||
for a.Cmp(&limit) < 0 {
|
||||
// Compute the next Fibonacci number, storing it in a.
|
||||
a.Add(a, b)
|
||||
// Swap a and b so that b is the next number in the sequence.
|
||||
a, b = b, a
|
||||
}
|
||||
fmt.Println(a) // 100-digit Fibonacci number
|
||||
}
|
||||
31
_demo/async/async/async.go
Normal file
31
_demo/async/async/async.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package async
|
||||
|
||||
import (
|
||||
_ "unsafe"
|
||||
)
|
||||
|
||||
type Void = [0]byte
|
||||
|
||||
type Future[T any] interface {
|
||||
Then(cb func(T))
|
||||
}
|
||||
|
||||
type future[T any] struct {
|
||||
cb func(func(T))
|
||||
}
|
||||
|
||||
func (f *future[T]) Then(cb func(T)) {
|
||||
f.cb(cb)
|
||||
}
|
||||
|
||||
func Async[T any](fn func(func(T))) Future[T] {
|
||||
return &future[T]{fn}
|
||||
}
|
||||
|
||||
func Run[T any](future Future[T]) T {
|
||||
var ret T
|
||||
future.Then(func(v T) {
|
||||
ret = v
|
||||
})
|
||||
return ret
|
||||
}
|
||||
23
_demo/async/main.go
Normal file
23
_demo/async/main.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/goplus/llgo/_demo/async/async"
|
||||
"github.com/goplus/llgo/_demo/async/timeout"
|
||||
)
|
||||
|
||||
func Sleep(i int, d time.Duration) async.Future[int] {
|
||||
return async.Async(func(resolve func(int)) {
|
||||
timeout.Timeout(d).Then(func(async.Void) {
|
||||
resolve(i)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
async.Run(async.Async(func(resolve func(async.Void)) {
|
||||
println("read file")
|
||||
defer resolve(async.Void{})
|
||||
}))
|
||||
}
|
||||
16
_demo/async/timeout/timeout.go
Normal file
16
_demo/async/timeout/timeout.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package timeout
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/goplus/llgo/_demo/async/async"
|
||||
)
|
||||
|
||||
func Timeout(d time.Duration) async.Future[async.Void] {
|
||||
return async.Async(func(resolve func(async.Void)) {
|
||||
go func() {
|
||||
time.Sleep(d)
|
||||
resolve(async.Void{})
|
||||
}()
|
||||
})
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c/sync/atomic"
|
||||
"github.com/goplus/lib/c/sync/atomic"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/os"
|
||||
"github.com/goplus/lib/c"
|
||||
"github.com/goplus/lib/c/os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
16
_demo/cgofull/bar.go
Normal file
16
_demo/cgofull/bar.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DBAR
|
||||
#include <stdio.h>
|
||||
#include "foo.h"
|
||||
static void foo(Foo* f) {
|
||||
printf("foo in bar: %d\n", f->a);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
func Bar(f *C.Foo) {
|
||||
C.print_foo(f)
|
||||
C.foo(f)
|
||||
}
|
||||
157
_demo/cgofull/cgofull.go
Normal file
157
_demo/cgofull/cgofull.go
Normal file
@@ -0,0 +1,157 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
#cgo windows,!amd64 CFLAGS: -D_WIN32
|
||||
#cgo !windows CFLAGS: -D_POSIX
|
||||
#cgo windows,amd64 CFLAGS: -D_WIN64
|
||||
#cgo linux,amd64 CFLAGS: -D_LINUX64
|
||||
#cgo !windows,amd64 CFLAGS: -D_UNIX64
|
||||
#cgo pkg-config: python3-embed
|
||||
#include <stdio.h>
|
||||
#include <Python.h>
|
||||
#include "foo.h"
|
||||
typedef struct {
|
||||
int a;
|
||||
} s4;
|
||||
|
||||
typedef struct {
|
||||
int a;
|
||||
int b;
|
||||
} s8;
|
||||
|
||||
typedef struct {
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
} s12;
|
||||
|
||||
typedef struct {
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
int d;
|
||||
} s16;
|
||||
|
||||
typedef struct {
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
int d;
|
||||
int e;
|
||||
} s20;
|
||||
|
||||
static int test_structs(s4* s4, s8* s8, s12* s12, s16* s16, s20* s20) {
|
||||
printf("s4.a: %d\n", s4->a);
|
||||
printf("s8.a: %d, s8.b: %d\n", s8->a, s8->b);
|
||||
printf("s12.a: %d, s12.b: %d, s12.c: %d\n", s12->a, s12->b, s12->c);
|
||||
printf("s16.a: %d, s16.b: %d, s16.c: %d, s16.d: %d\n", s16->a, s16->b, s16->c, s16->d);
|
||||
printf("s20.a: %d, s20.b: %d, s20.c: %d, s20.d: %d, s20.e: %d\n", s20->a, s20->b, s20->c, s20->d, s20->e);
|
||||
|
||||
return s4->a + s8->a + s8->b + s12->a + s12->b + s12->c + s16->a + s16->b + s16->c + s16->d + s20->a + s20->b + s20->c + s20->d + s20->e;
|
||||
}
|
||||
|
||||
static void test_macros() {
|
||||
#ifdef FOO
|
||||
printf("FOO is defined\n");
|
||||
#endif
|
||||
#ifdef BAR
|
||||
printf("BAR is defined\n");
|
||||
#endif
|
||||
#ifdef _WIN32
|
||||
printf("WIN32 is defined\n");
|
||||
#endif
|
||||
#ifdef _POSIX
|
||||
printf("POSIX is defined\n");
|
||||
#endif
|
||||
#ifdef _WIN64
|
||||
printf("WIN64 is defined\n");
|
||||
#endif
|
||||
#ifdef _LINUX64
|
||||
printf("LINUX64 is defined\n");
|
||||
#endif
|
||||
#ifdef _UNIX64
|
||||
printf("UNIX64 is defined\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
#define MY_VERSION "1.0.0"
|
||||
#define MY_CODE 0x12345678
|
||||
|
||||
static void test_void() {
|
||||
printf("test_void\n");
|
||||
}
|
||||
|
||||
typedef int (*Cb)(int);
|
||||
|
||||
extern int go_callback(int);
|
||||
|
||||
extern int c_callback(int i);
|
||||
|
||||
static void test_callback(Cb cb) {
|
||||
printf("test_callback, cb: %p, go_callback: %p, c_callback: %p\n", cb, go_callback, c_callback);
|
||||
printf("test_callback, *cb: %p, *go_callback: %p, *c_callback: %p\n", *(void**)cb, *(void**)(go_callback), *(void**)(c_callback));
|
||||
printf("cb result: %d\n", cb(123));
|
||||
printf("done\n");
|
||||
}
|
||||
|
||||
extern int go_callback_not_use_in_go(int);
|
||||
|
||||
static void run_callback() {
|
||||
test_callback(c_callback);
|
||||
test_callback(go_callback_not_use_in_go);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/_demo/cgofull/pymod1"
|
||||
"github.com/goplus/llgo/_demo/cgofull/pymod2"
|
||||
)
|
||||
|
||||
//export go_callback_not_use_in_go
|
||||
func go_callback_not_use_in_go(i C.int) C.int {
|
||||
return i + 1
|
||||
}
|
||||
|
||||
//export go_callback
|
||||
func go_callback(i C.int) C.int {
|
||||
return i + 1
|
||||
}
|
||||
|
||||
func main() {
|
||||
runPy()
|
||||
f := &C.Foo{a: 1}
|
||||
Foo(f)
|
||||
Bar(f)
|
||||
C.test_macros()
|
||||
r := C.test_structs(&C.s4{a: 1}, &C.s8{a: 1, b: 2}, &C.s12{a: 1, b: 2, c: 3}, &C.s16{a: 1, b: 2, c: 3, d: 4}, &C.s20{a: 1, b: 2, c: 3, d: 4, e: 5})
|
||||
fmt.Println(r)
|
||||
if r != 35 {
|
||||
panic("test_structs failed")
|
||||
}
|
||||
fmt.Println(C.MY_VERSION)
|
||||
fmt.Println(int(C.MY_CODE))
|
||||
C.test_void()
|
||||
|
||||
println("call run_callback")
|
||||
C.run_callback()
|
||||
|
||||
// test _Cgo_ptr and _cgoCheckResult
|
||||
println("call with go_callback")
|
||||
C.test_callback((C.Cb)(C.go_callback))
|
||||
|
||||
println("call with c_callback")
|
||||
C.test_callback((C.Cb)(C.c_callback))
|
||||
}
|
||||
|
||||
func runPy() {
|
||||
Initialize()
|
||||
defer Finalize()
|
||||
Run("print('Hello, Python!')")
|
||||
C.PyObject_Print((*C.PyObject)(unsafe.Pointer(pymod1.Float(1.23))), C.stderr, 0)
|
||||
C.PyObject_Print((*C.PyObject)(unsafe.Pointer(pymod2.Long(123))), C.stdout, 0)
|
||||
// test _Cgo_use
|
||||
C.PyObject_Print((*C.PyObject)(unsafe.Pointer(C.PyComplex_FromDoubles(C.double(1.23), C.double(4.56)))), C.stdout, 0)
|
||||
}
|
||||
12
_demo/cgofull/foo.c
Normal file
12
_demo/cgofull/foo.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
#include "foo.h"
|
||||
|
||||
void print_foo(Foo *f)
|
||||
{
|
||||
printf("print_foo: %d\n", f->a);
|
||||
}
|
||||
|
||||
int c_callback(int i)
|
||||
{
|
||||
return i + 1;
|
||||
}
|
||||
16
_demo/cgofull/foo.go
Normal file
16
_demo/cgofull/foo.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -DFOO
|
||||
#include <stdio.h>
|
||||
#include "foo.h"
|
||||
static void foo(Foo* f) {
|
||||
printf("foo in bar: %d\n", f->a);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
func Foo(f *C.Foo) {
|
||||
C.print_foo(f)
|
||||
C.foo(f)
|
||||
}
|
||||
7
_demo/cgofull/foo.h
Normal file
7
_demo/cgofull/foo.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
typedef struct {
|
||||
int a;
|
||||
} Foo;
|
||||
|
||||
extern void print_foo(Foo* f);
|
||||
24
_demo/cgofull/py.go
Normal file
24
_demo/cgofull/py.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
#cgo pkg-config: python3-embed
|
||||
#include <Python.h>
|
||||
*/
|
||||
import "C"
|
||||
import "fmt"
|
||||
|
||||
func Initialize() {
|
||||
C.Py_Initialize()
|
||||
}
|
||||
|
||||
func Finalize() {
|
||||
C.Py_Finalize()
|
||||
}
|
||||
|
||||
func Run(code string) error {
|
||||
if C.PyRun_SimpleString(C.CString(code)) != 0 {
|
||||
C.PyErr_Print()
|
||||
return fmt.Errorf("failed to run code")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
11
_demo/cgofull/pymod1/pymod1.go
Normal file
11
_demo/cgofull/pymod1/pymod1.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package pymod1
|
||||
|
||||
/*
|
||||
#cgo pkg-config: python3-embed
|
||||
#include <Python.h>
|
||||
*/
|
||||
import "C"
|
||||
|
||||
func Float(f float64) *C.PyObject {
|
||||
return C.PyFloat_FromDouble(C.double(f))
|
||||
}
|
||||
11
_demo/cgofull/pymod2/pymod2.go
Normal file
11
_demo/cgofull/pymod2/pymod2.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package pymod2
|
||||
|
||||
/*
|
||||
#cgo pkg-config: python3-embed
|
||||
#include <Python.h>
|
||||
*/
|
||||
import "C"
|
||||
|
||||
func Long(l int64) *C.PyObject {
|
||||
return C.PyLong_FromLongLong(C.longlong(l))
|
||||
}
|
||||
28
_demo/checkfile/demo.go
Normal file
28
_demo/checkfile/demo.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tempDir := os.TempDir()
|
||||
noexist := filepath.Join(tempDir, "noexist.txt")
|
||||
|
||||
if _, err := os.Stat(noexist); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Println("noexist:", err.Error())
|
||||
} else {
|
||||
fmt.Println("exist,other err:", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := os.Open(noexist); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Println("noexist:", err.Error())
|
||||
} else {
|
||||
fmt.Println("exist,other err:", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
18
_demo/commandrun/commandrun.go
Normal file
18
_demo/commandrun/commandrun.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var data bytes.Buffer
|
||||
cmd := exec.Command("echo", "hello llgo")
|
||||
cmd.Stdout = &data
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
println("len:", len(data.Bytes()))
|
||||
println("data:", data.String())
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/lib/c"
|
||||
)
|
||||
|
||||
func concat(args ...string) (ret string) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/lib/c"
|
||||
"github.com/goplus/lib/c/math"
|
||||
"github.com/goplus/llgo/_demo/cppintf/foo"
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/math"
|
||||
)
|
||||
|
||||
type Bar struct {
|
||||
|
||||
@@ -3,9 +3,9 @@ package main
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/lib/c"
|
||||
"github.com/goplus/lib/c/math"
|
||||
"github.com/goplus/llgo/_demo/cppmintf/foo"
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/math"
|
||||
)
|
||||
|
||||
type Bar struct {
|
||||
|
||||
@@ -3,9 +3,9 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/math/rand"
|
||||
"github.com/goplus/llgo/c/time"
|
||||
"github.com/goplus/lib/c"
|
||||
"github.com/goplus/lib/c/math/rand"
|
||||
"github.com/goplus/lib/c/time"
|
||||
)
|
||||
|
||||
func fastrand64() uint64 {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package main
|
||||
|
||||
import "github.com/goplus/llgo/c/time"
|
||||
import "github.com/goplus/lib/c/time"
|
||||
|
||||
func main() {
|
||||
var tv time.Timespec
|
||||
|
||||
18
_demo/defer/main.go
Normal file
18
_demo/defer/main.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
var a int = 5
|
||||
defer println(a)
|
||||
defer func() {
|
||||
println(a)
|
||||
}()
|
||||
defer func() {
|
||||
println(recover().(string))
|
||||
}()
|
||||
a = 10
|
||||
panic("error")
|
||||
//Output:
|
||||
// error
|
||||
// 10
|
||||
// 5
|
||||
}
|
||||
42
_demo/failed/stacktrace/main.go
Normal file
42
_demo/failed/stacktrace/main.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type MyStruct[T any] struct {
|
||||
value T
|
||||
}
|
||||
|
||||
func (m *MyStruct[T]) Method() {
|
||||
fmt.Println("In generic method")
|
||||
genericFunc[T](m.value)
|
||||
}
|
||||
|
||||
func genericFunc[T any](v T) {
|
||||
fmt.Println("In generic function")
|
||||
normalFunc()
|
||||
}
|
||||
|
||||
func normalFunc() {
|
||||
fmt.Println("In normal function")
|
||||
panic("panic occurs here")
|
||||
}
|
||||
|
||||
func main() {
|
||||
m := &MyStruct[string]{value: "hello"}
|
||||
m.Method()
|
||||
}
|
||||
|
||||
//Expected:
|
||||
// In generic method
|
||||
// In generic function
|
||||
// In normal function
|
||||
// panic: panic occurs here
|
||||
|
||||
// [0x00C6D310 github.com/goplus/llgo/internal/runtime.Rethrow+0x2f, SP = 0x60]
|
||||
// [0x00C6CF44 github.com/goplus/llgo/internal/runtime.Panic+0x2d, SP = 0x50]
|
||||
// [0x00C69420 main.normalFunc+0xf, SP = 0xa8]
|
||||
// [0x00C69564 main.genericFunc[string]+0x18, SP = 0x74]
|
||||
// [0x00C694A8 main.(*MyStruct[string]).Method+0x1f, SP = 0x84]
|
||||
// [0x00C6936C main+0x4, SP = 0x40]
|
||||
@@ -1,9 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/os"
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/lib/c"
|
||||
"github.com/goplus/lib/c/os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -45,8 +46,6 @@ func main() {
|
||||
}
|
||||
c.Printf(c.Str("set file status successfully\n"))
|
||||
|
||||
|
||||
|
||||
c.Printf(c.Str("111"))
|
||||
// Close file
|
||||
os.Close(fd)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/lib/c"
|
||||
)
|
||||
|
||||
type generator struct {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/os"
|
||||
"github.com/goplus/lib/c"
|
||||
"github.com/goplus/lib/c/os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
5
_demo/go.mod
Normal file
5
_demo/go.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module github.com/goplus/llgo/_demo
|
||||
|
||||
go 1.20
|
||||
|
||||
require github.com/goplus/lib v0.2.0
|
||||
2
_demo/go.sum
Normal file
2
_demo/go.sum
Normal file
@@ -0,0 +1,2 @@
|
||||
github.com/goplus/lib v0.2.0 h1:AjqkN1XK5H23wZMMlpaUYAMCDAdSBQ2NMFrLtSh7W4g=
|
||||
github.com/goplus/lib v0.2.0/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=
|
||||
@@ -1,13 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"fmt"
|
||||
|
||||
"github.com/goplus/lib/c"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c.Printf(c.Str("Hello world\n"))
|
||||
println("hello world by println")
|
||||
fmt.Println("hello world by fmt.Println")
|
||||
c.Printf(c.Str("Hello world by c.Printf\n"))
|
||||
}
|
||||
|
||||
/* Expected output:
|
||||
Hello world
|
||||
*/
|
||||
|
||||
15
_demo/helloc/helloc.go
Normal file
15
_demo/helloc/helloc.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/lib/c"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c.Printf(c.Str("Hello world by c.Printf\n"))
|
||||
c.Printf(c.Str("%ld\n"), unsafe.Sizeof(int(0)))
|
||||
c.Printf(c.Str("%ld\n"), unsafe.Sizeof(uintptr(0)))
|
||||
// var v any = int(0)
|
||||
// c.Printf(c.Str("%ld\n"), unsafe.Sizeof(v))
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/llama2"
|
||||
"github.com/goplus/llgo/c/time"
|
||||
"github.com/goplus/lib/c"
|
||||
"github.com/goplus/lib/c/llama2"
|
||||
"github.com/goplus/lib/c/time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/net"
|
||||
"github.com/goplus/lib/c"
|
||||
"github.com/goplus/lib/c/net"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/lib/c"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
42
_demo/reflectfunc/reflectfunc.go
Normal file
42
_demo/reflectfunc/reflectfunc.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func add(a, b int) int {
|
||||
return a + b
|
||||
}
|
||||
|
||||
func main() {
|
||||
fn := func(a, b int) int {
|
||||
return a + b
|
||||
}
|
||||
var i int
|
||||
fn1 := func() {
|
||||
i++
|
||||
}
|
||||
fn2 := func() func() {
|
||||
return func() {
|
||||
println("closure", i)
|
||||
}
|
||||
}
|
||||
|
||||
fns := []any{add, fn, fn1, fn2}
|
||||
for _, fn := range fns {
|
||||
v := reflect.ValueOf(fn)
|
||||
fmt.Println(v.Type())
|
||||
fmt.Println(v.Kind())
|
||||
if v.Kind() != reflect.Func {
|
||||
panic(fmt.Sprintf("not func: %T", fn))
|
||||
}
|
||||
|
||||
t := v.Type()
|
||||
fmt.Println(t)
|
||||
fmt.Println(t.Kind())
|
||||
if t.Kind() != reflect.Func {
|
||||
panic(fmt.Sprintf("not func: %T", fn))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c/setjmp"
|
||||
"github.com/goplus/lib/c/setjmp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -3,9 +3,9 @@ package main
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/net"
|
||||
"github.com/goplus/llgo/c/os"
|
||||
"github.com/goplus/lib/c"
|
||||
"github.com/goplus/lib/c/net"
|
||||
"github.com/goplus/lib/c/os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -3,9 +3,9 @@ package main
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/net"
|
||||
"github.com/goplus/llgo/c/os"
|
||||
"github.com/goplus/lib/c"
|
||||
"github.com/goplus/lib/c/net"
|
||||
"github.com/goplus/lib/c/os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
112
_demo/sync/sync.go
Normal file
112
_demo/sync/sync.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Counter represents a thread-safe counter
|
||||
type Counter struct {
|
||||
mu sync.Mutex
|
||||
value int64
|
||||
}
|
||||
|
||||
// Increment increases the counter value by 1
|
||||
func (c *Counter) Increment() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.value++
|
||||
}
|
||||
|
||||
// GetValue returns the current value of the counter
|
||||
func (c *Counter) GetValue() int64 {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
return c.value
|
||||
}
|
||||
|
||||
// Constant values for the test
|
||||
const (
|
||||
numGoroutines = 64
|
||||
numIterations = 10000
|
||||
expectedTotal = numGoroutines * numIterations
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create a new counter instance
|
||||
counter := &Counter{}
|
||||
|
||||
// Create a wait group to wait for all goroutines to finish
|
||||
var wg sync.WaitGroup
|
||||
|
||||
// Track active goroutines for monitoring
|
||||
var activeGoroutines int32
|
||||
|
||||
// Start time
|
||||
startTime := time.Now()
|
||||
|
||||
// Launch goroutines
|
||||
for i := 0; i < numGoroutines; i++ {
|
||||
wg.Add(1)
|
||||
atomic.AddInt32(&activeGoroutines, 1)
|
||||
|
||||
go func(id int) {
|
||||
defer func() {
|
||||
wg.Done()
|
||||
atomic.AddInt32(&activeGoroutines, -1)
|
||||
}()
|
||||
|
||||
// Each goroutine increments the counter numIterations times
|
||||
for j := 0; j < numIterations; j++ {
|
||||
counter.Increment()
|
||||
|
||||
// Simulate varying workload with random sleeps
|
||||
if j%100 == 0 {
|
||||
time.Sleep(time.Microsecond)
|
||||
}
|
||||
}
|
||||
fmt.Printf("Goroutine %d finished\n", id)
|
||||
}(i)
|
||||
}
|
||||
|
||||
// Monitor progress in a separate goroutine
|
||||
go func() {
|
||||
for {
|
||||
active := atomic.LoadInt32(&activeGoroutines)
|
||||
if active == 0 {
|
||||
break
|
||||
}
|
||||
fmt.Printf("Active goroutines: %d\n", active)
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}()
|
||||
|
||||
// Wait for all goroutines to complete
|
||||
wg.Wait()
|
||||
|
||||
// Calculate execution time
|
||||
duration := time.Since(startTime)
|
||||
|
||||
// Get and verify the final result
|
||||
finalValue := counter.GetValue()
|
||||
fmt.Printf("\nExecution completed in: %v\n", duration)
|
||||
fmt.Printf("Final counter value: %d\n", finalValue)
|
||||
fmt.Printf("Expected value: %d\n", expectedTotal)
|
||||
|
||||
// Assert the result
|
||||
if finalValue != expectedTotal {
|
||||
panic(fmt.Sprintf("ERROR: Counter value mismatch! Expected %d, got %d\n",
|
||||
expectedTotal, finalValue))
|
||||
} else {
|
||||
fmt.Printf("SUCCESS: Counter value matches expected total\n")
|
||||
}
|
||||
|
||||
// Print some statistics
|
||||
opsPerSecond := float64(expectedTotal) / duration.Seconds()
|
||||
fmt.Printf("\nStatistics:\n")
|
||||
fmt.Printf("Operations per second: %.2f\n", opsPerSecond)
|
||||
fmt.Printf("Average time per operation: %.2f ns\n",
|
||||
float64(duration.Nanoseconds())/float64(expectedTotal))
|
||||
}
|
||||
31
_demo/syncdebug/syncdebug.go
Normal file
31
_demo/syncdebug/syncdebug.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
"unsafe"
|
||||
|
||||
llsync "github.com/goplus/lib/c/pthread/sync"
|
||||
)
|
||||
|
||||
type L struct {
|
||||
mu sync.Mutex
|
||||
s string
|
||||
i int
|
||||
w io.Writer
|
||||
}
|
||||
|
||||
func main() {
|
||||
l := &L{s: "hello", i: 123, w: os.Stdout}
|
||||
println("sizeof(L):", unsafe.Sizeof(L{}))
|
||||
println("sizeof(sync.Mutex):", unsafe.Sizeof(sync.Mutex{}))
|
||||
println("sizeof(llsync.Mutex):", unsafe.Sizeof(llsync.Mutex{}))
|
||||
println("l:", l, "l.s:", l.s, "l.i:", l.i, "l.w:", l.w)
|
||||
l.mu.Lock()
|
||||
println("locked")
|
||||
println("l:", l, "l.s:", l.s, "l.i:", l.i, "l.w:", l.w)
|
||||
l.w.Write([]byte(l.s))
|
||||
l.w.Write([]byte("\n"))
|
||||
l.mu.Unlock()
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/pthread"
|
||||
"github.com/goplus/lib/c"
|
||||
"github.com/goplus/lib/c/pthread"
|
||||
)
|
||||
|
||||
var key pthread.Key
|
||||
|
||||
115
_lldb/README.md
Normal file
115
_lldb/README.md
Normal file
@@ -0,0 +1,115 @@
|
||||
## LLGo Plugin of LLDB
|
||||
|
||||
### Build with debug info
|
||||
|
||||
```shell
|
||||
LLGO_DEBUG_SYMBOLS=1 llgo build -o cl/_testdata/debug/out ./cl/_testdata/debug
|
||||
```
|
||||
|
||||
### Debug with lldb
|
||||
|
||||
```shell
|
||||
_lldb/runlldb.sh ./cl/_testdata/debug/out
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```shell
|
||||
/opt/homebrew/bin/lldb -O "command script import _lldb/llgo_plugin.py" ./cl/_testdata/debug/out
|
||||
# github.com/goplus/llgo/cl/_testdata/debug
|
||||
Breakpoint 1: no locations (pending).
|
||||
Breakpoint set in dummy target, will get copied into future targets.
|
||||
(lldb) command script import _lldb/llgo_plugin.py
|
||||
(lldb) target create "./cl/_testdata/debug/out"
|
||||
Current executable set to '/Users/lijie/source/goplus/llgo/cl/_testdata/debug/out' (arm64).
|
||||
(lldb) r
|
||||
Process 21992 launched: '/Users/lijie/source/goplus/llgo/cl/_testdata/debug/out' (arm64)
|
||||
globalInt: 301
|
||||
s: 0x100123e40
|
||||
0x100123be0
|
||||
5 8
|
||||
called function with struct
|
||||
1 2 3 4 5 6 7 8 9 10 +1.100000e+01 +1.200000e+01 true (+1.300000e+01+1.400000e+01i) (+1.500000e+01+1.600000e+01i) [3/3]0x1001129a0 [3/3]0x100112920 hello 0x1001149b0 0x100123ab0 0x100123d10 0x1001149e0 (0x100116810,0x1001149d0) 0x10011bf00 0x10010fa80 (0x100116840,0x100112940) 0x10001b4a4
|
||||
9
|
||||
1 (0x1001167e0,0x100112900)
|
||||
called function with types
|
||||
0x100123e40
|
||||
0x1000343d0
|
||||
Process 21992 stopped
|
||||
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
|
||||
frame #0: 0x000000010001b3b4 out`main at in.go:225:12
|
||||
222 // s.i8: '\x01'
|
||||
223 // s.i16: 2
|
||||
224 s.i8 = 0x12
|
||||
-> 225 println(s.i8)
|
||||
226 // Expected:
|
||||
227 // all variables: globalInt globalStruct globalStructPtr s i err
|
||||
228 // s.i8: '\x12'
|
||||
(lldb) v
|
||||
var i int = <variable not available>
|
||||
var s github.com/goplus/llgo/cl/_testdata/debug.StructWithAllTypeFields = {
|
||||
i8 = '\x12',
|
||||
i16 = 2,
|
||||
i32 = 3,
|
||||
i64 = 4,
|
||||
i = 5,
|
||||
u8 = '\x06',
|
||||
u16 = 7,
|
||||
u32 = 8,
|
||||
u64 = 9,
|
||||
u = 10,
|
||||
f32 = 11,
|
||||
f64 = 12,
|
||||
b = true,
|
||||
c64 = {real = 13, imag = 14},
|
||||
c128 = {real = 15, imag = 16},
|
||||
slice = []int{21, 22, 23},
|
||||
arr = [3]int{24, 25, 26},
|
||||
arr2 = [3]github.com/goplus/llgo/cl/_testdata/debug.E{{i = 27}, {i = 28}, {i = 29}},
|
||||
s = "hello",
|
||||
e = {i = 30},
|
||||
pf = 0x0000000100123d10,
|
||||
pi = 0x00000001001149e0,
|
||||
intr = {type = 0x0000000100116810, data = 0x00000001001149d0},
|
||||
m = {count = 4296130304},
|
||||
c = {},
|
||||
err = {type = 0x0000000100116840, data = 0x0000000100112940},
|
||||
fn = {f = 0x000000010001b4a4, data = 0x00000001001149c0},
|
||||
pad1 = 100,
|
||||
pad2 = 200
|
||||
}
|
||||
var globalStructPtr *github.com/goplus/llgo/cl/_testdata/debug.StructWithAllTypeFields = <variable not available>
|
||||
var globalStruct github.com/goplus/llgo/cl/_testdata/debug.StructWithAllTypeFields = {
|
||||
i8 = '\x01',
|
||||
i16 = 2,
|
||||
i32 = 3,
|
||||
i64 = 4,
|
||||
i = 5,
|
||||
u8 = '\x06',
|
||||
u16 = 7,
|
||||
u32 = 8,
|
||||
u64 = 9,
|
||||
u = 10,
|
||||
f32 = 11,
|
||||
f64 = 12,
|
||||
b = true,
|
||||
c64 = {real = 13, imag = 14},
|
||||
c128 = {real = 15, imag = 16},
|
||||
slice = []int{21, 22, 23},
|
||||
arr = [3]int{24, 25, 26},
|
||||
arr2 = [3]github.com/goplus/llgo/cl/_testdata/debug.E{{i = 27}, {i = 28}, {i = 29}},
|
||||
s = "hello",
|
||||
e = {i = 30},
|
||||
pf = 0x0000000100123d10,
|
||||
pi = 0x00000001001149e0,
|
||||
intr = {type = 0x0000000100116810, data = 0x00000001001149d0},
|
||||
m = {count = 4296130304},
|
||||
c = {},
|
||||
err = {type = 0x0000000100116840, data = 0x0000000100112940},
|
||||
fn = {f = 0x000000010001b4a4, data = 0x00000001001149c0},
|
||||
pad1 = 100,
|
||||
pad2 = 200
|
||||
}
|
||||
var globalInt int = 301
|
||||
var err error = {type = 0x0000000100112900, data = 0x000000000000001a}
|
||||
```
|
||||
54
_lldb/common.sh
Normal file
54
_lldb/common.sh
Normal file
@@ -0,0 +1,54 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Function to find LLDB 18+
|
||||
find_lldb() {
|
||||
local lldb_paths=(
|
||||
"/opt/homebrew/bin/lldb"
|
||||
"/usr/local/bin/lldb"
|
||||
"/usr/bin/lldb"
|
||||
"lldb" # This will use the system PATH
|
||||
)
|
||||
|
||||
for lldb_path in "${lldb_paths[@]}"; do
|
||||
if command -v "$lldb_path" >/dev/null 2>&1; then
|
||||
local version
|
||||
version=$("$lldb_path" --version | grep -oE '[0-9]+' | head -1)
|
||||
if [ "$version" -ge 18 ]; then
|
||||
echo "$lldb_path"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
echo "Error: LLDB 18 or higher not found" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Find LLDB 18+
|
||||
LLDB_PATH=$(find_lldb)
|
||||
echo "LLDB_PATH: $LLDB_PATH"
|
||||
$LLDB_PATH --version
|
||||
export LLDB_PATH
|
||||
|
||||
# Default package path
|
||||
export DEFAULT_PACKAGE_PATH="./_lldb/lldbtest"
|
||||
|
||||
# Function to build the project
|
||||
build_project() {
|
||||
# package_path parameter is kept for backward compatibility
|
||||
local current_dir
|
||||
current_dir=$(pwd) || return
|
||||
|
||||
if ! cd "${DEFAULT_PACKAGE_PATH}"; then
|
||||
echo "Failed to change directory to ${DEFAULT_PACKAGE_PATH}" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
LLGO_DEBUG_SYMBOLS=1 llgo build -o "debug.out" . || {
|
||||
local ret=$?
|
||||
cd "$current_dir" || return
|
||||
return $ret
|
||||
}
|
||||
|
||||
cd "$current_dir" || return
|
||||
}
|
||||
3
_lldb/lldbtest/go.mod
Normal file
3
_lldb/lldbtest/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module lldbtest
|
||||
|
||||
go 1.20
|
||||
569
_lldb/lldbtest/main.go
Normal file
569
_lldb/lldbtest/main.go
Normal file
@@ -0,0 +1,569 @@
|
||||
package main
|
||||
|
||||
import "errors"
|
||||
|
||||
type Base struct {
|
||||
name string
|
||||
}
|
||||
|
||||
type E struct {
|
||||
// Base
|
||||
i int
|
||||
}
|
||||
type StructWithAllTypeFields struct {
|
||||
i8 int8
|
||||
i16 int16
|
||||
i32 int32
|
||||
i64 int64
|
||||
i int
|
||||
u8 uint8
|
||||
u16 uint16
|
||||
u32 uint32
|
||||
u64 uint64
|
||||
u uint
|
||||
f32 float32
|
||||
f64 float64
|
||||
b bool
|
||||
c64 complex64
|
||||
c128 complex128
|
||||
slice []int
|
||||
arr [3]int
|
||||
arr2 [3]E
|
||||
s string
|
||||
e E
|
||||
pf *StructWithAllTypeFields // resursive
|
||||
pi *int
|
||||
intr Interface
|
||||
m map[string]uint64
|
||||
c chan int
|
||||
err error
|
||||
fn func(string) (int, error)
|
||||
pad1 int
|
||||
pad2 int
|
||||
}
|
||||
|
||||
type Interface interface {
|
||||
Foo(a []int, b string) int
|
||||
}
|
||||
|
||||
type Struct struct{}
|
||||
|
||||
func (s *Struct) Foo(a []int, b string) int {
|
||||
return 1
|
||||
}
|
||||
|
||||
func FuncWithAllTypeStructParam(s StructWithAllTypeFields) {
|
||||
println(&s)
|
||||
// Expected:
|
||||
// all variables: s
|
||||
// s.i8: '\x01'
|
||||
// s.i16: 2
|
||||
// s.i32: 3
|
||||
// s.i64: 4
|
||||
// s.i: 5
|
||||
// s.u8: '\x06'
|
||||
// s.u16: 7
|
||||
// s.u32: 8
|
||||
// s.u64: 9
|
||||
// s.u: 10
|
||||
// s.f32: 11
|
||||
// s.f64: 12
|
||||
// s.b: true
|
||||
// s.c64: complex64{real = 13, imag = 14}
|
||||
// s.c128: complex128{real = 15, imag = 16}
|
||||
// s.slice: []int{21, 22, 23}
|
||||
// s.arr: [3]int{24, 25, 26}
|
||||
// s.arr2: [3]lldbtest.E{{i = 27}, {i = 28}, {i = 29}}
|
||||
// s.s: "hello"
|
||||
// s.e: lldbtest.E{i = 30}
|
||||
// s.pad1: 100
|
||||
// s.pad2: 200
|
||||
s.i8 = '\b'
|
||||
// Expected:
|
||||
// s.i8: '\b'
|
||||
// s.i16: 2
|
||||
println(len(s.s), s.i8)
|
||||
}
|
||||
|
||||
// Params is a function with all types of parameters.
|
||||
func FuncWithAllTypeParams(
|
||||
i8 int8,
|
||||
i16 int16,
|
||||
i32 int32,
|
||||
i64 int64,
|
||||
i int,
|
||||
u8 uint8,
|
||||
u16 uint16,
|
||||
u32 uint32,
|
||||
u64 uint64,
|
||||
u uint,
|
||||
f32 float32,
|
||||
f64 float64,
|
||||
b bool,
|
||||
c64 complex64,
|
||||
c128 complex128,
|
||||
slice []int,
|
||||
arr [3]int,
|
||||
arr2 [3]E,
|
||||
s string,
|
||||
e E,
|
||||
f StructWithAllTypeFields,
|
||||
pf *StructWithAllTypeFields,
|
||||
pi *int,
|
||||
intr Interface,
|
||||
m map[string]uint64,
|
||||
c chan int,
|
||||
err error,
|
||||
fn func(string) (int, error),
|
||||
) (int, error) {
|
||||
// Expected:
|
||||
// all variables: i8 i16 i32 i64 i u8 u16 u32 u64 u f32 f64 b c64 c128 slice arr arr2 s e f pf pi intr m c err fn
|
||||
// i32: 3
|
||||
// i64: 4
|
||||
// i: 5
|
||||
// u32: 8
|
||||
// u64: 9
|
||||
// u: 10
|
||||
// f32: 11
|
||||
// f64: 12
|
||||
// slice: []int{21, 22, 23}
|
||||
// arr: [3]int{24, 25, 26}
|
||||
// arr2: [3]lldbtest.E{{i = 27}, {i = 28}, {i = 29}}
|
||||
// slice[0]: 21
|
||||
// slice[1]: 22
|
||||
// slice[2]: 23
|
||||
// arr[0]: 24
|
||||
// arr[1]: 25
|
||||
// arr[2]: 26
|
||||
// arr2[0].i: 27
|
||||
// arr2[1].i: 28
|
||||
// arr2[2].i: 29
|
||||
// e: lldbtest.E{i = 30}
|
||||
|
||||
// Expected(skip):
|
||||
// i8: '\b'
|
||||
// i16: 2
|
||||
// u8: '\x06'
|
||||
// u16: 7
|
||||
// b: true
|
||||
println(
|
||||
i8, i16, i32, i64, i, u8, u16, u32, u64, u,
|
||||
f32, f64, b,
|
||||
c64, c128,
|
||||
slice, arr[0:],
|
||||
s,
|
||||
&e,
|
||||
&f, pf, pi, intr, m,
|
||||
c,
|
||||
err,
|
||||
fn,
|
||||
)
|
||||
i8 = 9
|
||||
i16 = 10
|
||||
i32 = 11
|
||||
i64 = 12
|
||||
i = 13
|
||||
u8 = 14
|
||||
u16 = 15
|
||||
u32 = 16
|
||||
u64 = 17
|
||||
u = 18
|
||||
f32 = 19
|
||||
f64 = 20
|
||||
b = false
|
||||
c64 = 21 + 22i
|
||||
c128 = 23 + 24i
|
||||
slice = []int{31, 32, 33}
|
||||
arr = [3]int{34, 35, 36}
|
||||
arr2 = [3]E{{i: 37}, {i: 38}, {i: 39}}
|
||||
s = "world"
|
||||
e = E{i: 40}
|
||||
|
||||
println(i8, i16, i32, i64, i, u8, u16, u32, u64, u,
|
||||
f32, f64, b,
|
||||
c64, c128,
|
||||
slice, arr[0:], &arr2,
|
||||
s,
|
||||
&e,
|
||||
&f, pf, pi, intr, m,
|
||||
c,
|
||||
err,
|
||||
fn,
|
||||
)
|
||||
// Expected:
|
||||
// i8: '\t'
|
||||
// i16: 10
|
||||
// i32: 11
|
||||
// i64: 12
|
||||
// i: 13
|
||||
// u8: '\x0e'
|
||||
// u16: 15
|
||||
// u32: 16
|
||||
// u64: 17
|
||||
// u: 18
|
||||
// f32: 19
|
||||
// f64: 20
|
||||
// b: false
|
||||
// c64: complex64{real = 21, imag = 22}
|
||||
// c128: complex128{real = 23, imag = 24}
|
||||
// slice: []int{31, 32, 33}
|
||||
// arr2: [3]lldbtest.E{{i = 37}, {i = 38}, {i = 39}}
|
||||
// s: "world"
|
||||
// e: lldbtest.E{i = 40}
|
||||
|
||||
// Expected(skip):
|
||||
// arr: [3]int{34, 35, 36}
|
||||
return 1, errors.New("some error")
|
||||
}
|
||||
|
||||
type TinyStruct struct {
|
||||
I int
|
||||
}
|
||||
|
||||
type SmallStruct struct {
|
||||
I int
|
||||
J int
|
||||
}
|
||||
|
||||
type MidStruct struct {
|
||||
I int
|
||||
J int
|
||||
K int
|
||||
}
|
||||
|
||||
type BigStruct struct {
|
||||
I int
|
||||
J int
|
||||
K int
|
||||
L int
|
||||
M int
|
||||
N int
|
||||
O int
|
||||
P int
|
||||
Q int
|
||||
R int
|
||||
}
|
||||
|
||||
func FuncStructParams(t TinyStruct, s SmallStruct, m MidStruct, b BigStruct) {
|
||||
// println(&t, &s, &m, &b)
|
||||
// Expected:
|
||||
// all variables: t s m b
|
||||
// t.I: 1
|
||||
// s.I: 2
|
||||
// s.J: 3
|
||||
// m.I: 4
|
||||
// m.J: 5
|
||||
// m.K: 6
|
||||
// b.I: 7
|
||||
// b.J: 8
|
||||
// b.K: 9
|
||||
// b.L: 10
|
||||
// b.M: 11
|
||||
// b.N: 12
|
||||
// b.O: 13
|
||||
// b.P: 14
|
||||
// b.Q: 15
|
||||
// b.R: 16
|
||||
println(t.I, s.I, s.J, m.I, m.J, m.K, b.I, b.J, b.K, b.L, b.M, b.N, b.O, b.P, b.Q, b.R)
|
||||
t.I = 10
|
||||
s.I = 20
|
||||
s.J = 21
|
||||
m.I = 40
|
||||
m.J = 41
|
||||
m.K = 42
|
||||
b.I = 70
|
||||
b.J = 71
|
||||
b.K = 72
|
||||
b.L = 73
|
||||
b.M = 74
|
||||
b.N = 75
|
||||
b.O = 76
|
||||
b.P = 77
|
||||
b.Q = 78
|
||||
b.R = 79
|
||||
// Expected:
|
||||
// all variables: t s m b
|
||||
// t.I: 10
|
||||
// s.I: 20
|
||||
// s.J: 21
|
||||
// m.I: 40
|
||||
// m.J: 41
|
||||
// m.K: 42
|
||||
// b.I: 70
|
||||
// b.J: 71
|
||||
// b.K: 72
|
||||
// b.L: 73
|
||||
// b.M: 74
|
||||
// b.N: 75
|
||||
// b.O: 76
|
||||
// b.P: 77
|
||||
// b.Q: 78
|
||||
// b.R: 79
|
||||
println("done")
|
||||
}
|
||||
|
||||
func FuncStructPtrParams(t *TinyStruct, s *SmallStruct, m *MidStruct, b *BigStruct) {
|
||||
// Expected:
|
||||
// all variables: t s m b
|
||||
// t.I: 1
|
||||
// s.I: 2
|
||||
// s.J: 3
|
||||
// m.I: 4
|
||||
// m.J: 5
|
||||
// m.K: 6
|
||||
// b.I: 7
|
||||
// b.J: 8
|
||||
// b.K: 9
|
||||
// b.L: 10
|
||||
// b.M: 11
|
||||
// b.N: 12
|
||||
// b.O: 13
|
||||
// b.P: 14
|
||||
// b.Q: 15
|
||||
// b.R: 16
|
||||
println(t, s, m, b)
|
||||
t.I = 10
|
||||
s.I = 20
|
||||
s.J = 21
|
||||
m.I = 40
|
||||
m.J = 41
|
||||
m.K = 42
|
||||
b.I = 70
|
||||
b.J = 71
|
||||
b.K = 72
|
||||
b.L = 73
|
||||
b.M = 74
|
||||
b.N = 75
|
||||
b.O = 76
|
||||
b.P = 77
|
||||
b.Q = 78
|
||||
b.R = 79
|
||||
// Expected:
|
||||
// all variables: t s m b
|
||||
// t.I: 10
|
||||
// s.I: 20
|
||||
// s.J: 21
|
||||
// m.I: 40
|
||||
// m.J: 41
|
||||
// m.K: 42
|
||||
// b.I: 70
|
||||
// b.J: 71
|
||||
// b.K: 72
|
||||
// b.L: 73
|
||||
// b.M: 74
|
||||
// b.N: 75
|
||||
// b.O: 76
|
||||
// b.P: 77
|
||||
// b.Q: 78
|
||||
// b.R: 79
|
||||
println(t.I, s.I, s.J, m.I, m.J, m.K, b.I, b.J, b.K, b.L, b.M, b.N, b.O, b.P, b.Q, b.R)
|
||||
println("done")
|
||||
}
|
||||
|
||||
func ScopeIf(branch int) {
|
||||
a := 1
|
||||
// Expected:
|
||||
// all variables: a branch
|
||||
// a: 1
|
||||
if branch == 1 {
|
||||
b := 2
|
||||
c := 3
|
||||
// Expected:
|
||||
// all variables: a b c branch
|
||||
// a: 1
|
||||
// b: 2
|
||||
// c: 3
|
||||
// branch: 1
|
||||
println(a, b, c)
|
||||
} else {
|
||||
c := 3
|
||||
d := 4
|
||||
// Expected:
|
||||
// all variables: a c d branch
|
||||
// a: 1
|
||||
// c: 3
|
||||
// d: 4
|
||||
// branch: 0
|
||||
println(a, c, d)
|
||||
}
|
||||
// Expected:
|
||||
// all variables: a branch
|
||||
// a: 1
|
||||
println("a:", a)
|
||||
}
|
||||
|
||||
func ScopeFor() {
|
||||
a := 1
|
||||
for i := 0; i < 10; i++ {
|
||||
switch i {
|
||||
case 0:
|
||||
println("i is 0")
|
||||
// Expected:
|
||||
// all variables: i a
|
||||
// i: 0
|
||||
// a: 1
|
||||
println("i:", i)
|
||||
case 1:
|
||||
println("i is 1")
|
||||
// Expected:
|
||||
// all variables: i a
|
||||
// i: 1
|
||||
// a: 1
|
||||
println("i:", i)
|
||||
default:
|
||||
println("i is", i)
|
||||
}
|
||||
}
|
||||
println("a:", a)
|
||||
}
|
||||
|
||||
func ScopeSwitch(i int) {
|
||||
a := 0
|
||||
switch i {
|
||||
case 1:
|
||||
b := 1
|
||||
println("i is 1")
|
||||
// Expected:
|
||||
// all variables: i a b
|
||||
// i: 1
|
||||
// a: 0
|
||||
// b: 1
|
||||
println("i:", i, "a:", a, "b:", b)
|
||||
case 2:
|
||||
c := 2
|
||||
println("i is 2")
|
||||
// Expected:
|
||||
// all variables: i a c
|
||||
// i: 2
|
||||
// a: 0
|
||||
// c: 2
|
||||
println("i:", i, "a:", a, "c:", c)
|
||||
default:
|
||||
d := 3
|
||||
println("i is", i)
|
||||
// Expected:
|
||||
// all variables: i a d
|
||||
// i: 3
|
||||
// a: 0
|
||||
// d: 3
|
||||
println("i:", i, "a:", a, "d:", d)
|
||||
}
|
||||
// Expected:
|
||||
// all variables: a i
|
||||
// a: 0
|
||||
println("a:", a)
|
||||
}
|
||||
|
||||
func main() {
|
||||
FuncStructParams(TinyStruct{I: 1}, SmallStruct{I: 2, J: 3}, MidStruct{I: 4, J: 5, K: 6}, BigStruct{I: 7, J: 8, K: 9, L: 10, M: 11, N: 12, O: 13, P: 14, Q: 15, R: 16})
|
||||
FuncStructPtrParams(&TinyStruct{I: 1}, &SmallStruct{I: 2, J: 3}, &MidStruct{I: 4, J: 5, K: 6}, &BigStruct{I: 7, J: 8, K: 9, L: 10, M: 11, N: 12, O: 13, P: 14, Q: 15, R: 16})
|
||||
i := 100
|
||||
s := StructWithAllTypeFields{
|
||||
i8: 1,
|
||||
i16: 2,
|
||||
i32: 3,
|
||||
i64: 4,
|
||||
i: 5,
|
||||
u8: 6,
|
||||
u16: 7,
|
||||
u32: 8,
|
||||
u64: 9,
|
||||
u: 10,
|
||||
f32: 11,
|
||||
f64: 12,
|
||||
b: true,
|
||||
c64: 13 + 14i,
|
||||
c128: 15 + 16i,
|
||||
slice: []int{21, 22, 23},
|
||||
arr: [3]int{24, 25, 26},
|
||||
arr2: [3]E{{i: 27}, {i: 28}, {i: 29}},
|
||||
s: "hello",
|
||||
e: E{i: 30},
|
||||
pf: &StructWithAllTypeFields{i16: 100},
|
||||
pi: &i,
|
||||
intr: &Struct{},
|
||||
m: map[string]uint64{"a": 31, "b": 32},
|
||||
c: make(chan int),
|
||||
err: errors.New("Test error"),
|
||||
fn: func(s string) (int, error) {
|
||||
println("fn:", s)
|
||||
i = 201
|
||||
return 1, errors.New("fn error")
|
||||
},
|
||||
pad1: 100,
|
||||
pad2: 200,
|
||||
}
|
||||
// Expected:
|
||||
// all variables: s i err
|
||||
// s.i8: '\x01'
|
||||
// s.i16: 2
|
||||
// s.i32: 3
|
||||
// s.i64: 4
|
||||
// s.i: 5
|
||||
// s.u8: '\x06'
|
||||
// s.u16: 7
|
||||
// s.u32: 8
|
||||
// s.u64: 9
|
||||
// s.u: 10
|
||||
// s.f32: 11
|
||||
// s.f64: 12
|
||||
// s.b: true
|
||||
// s.c64: complex64{real = 13, imag = 14}
|
||||
// s.c128: complex128{real = 15, imag = 16}
|
||||
// s.slice: []int{21, 22, 23}
|
||||
// s.arr: [3]int{24, 25, 26}
|
||||
// s.arr2: [3]lldbtest.E{{i = 27}, {i = 28}, {i = 29}}
|
||||
// s.s: "hello"
|
||||
// s.e: lldbtest.E{i = 30}
|
||||
// s.pf.i16: 100
|
||||
// *(s.pf).i16: 100
|
||||
// *(s.pi): 100
|
||||
globalStructPtr = &s
|
||||
globalStruct = s
|
||||
println("globalInt:", globalInt)
|
||||
// Expected(skip):
|
||||
// all variables: globalInt globalStruct globalStructPtr s i err
|
||||
println("s:", &s)
|
||||
FuncWithAllTypeStructParam(s)
|
||||
println("called function with struct")
|
||||
i, err := FuncWithAllTypeParams(
|
||||
s.i8, s.i16, s.i32, s.i64, s.i, s.u8, s.u16, s.u32, s.u64, s.u,
|
||||
s.f32, s.f64, s.b,
|
||||
s.c64, s.c128,
|
||||
s.slice, s.arr, s.arr2,
|
||||
s.s,
|
||||
s.e, s,
|
||||
s.pf, s.pi,
|
||||
s.intr,
|
||||
s.m,
|
||||
s.c,
|
||||
s.err,
|
||||
s.fn,
|
||||
)
|
||||
println(i, err)
|
||||
ScopeIf(1)
|
||||
ScopeIf(0)
|
||||
ScopeFor()
|
||||
ScopeSwitch(1)
|
||||
ScopeSwitch(2)
|
||||
ScopeSwitch(3)
|
||||
println(globalStructPtr)
|
||||
println(&globalStruct)
|
||||
s.i8 = 0x12
|
||||
println(s.i8)
|
||||
// Expected:
|
||||
// all variables: s i err
|
||||
// s.i8: '\x12'
|
||||
|
||||
// Expected(skip):
|
||||
// globalStruct.i8: '\x01'
|
||||
println((*globalStructPtr).i8)
|
||||
println("done")
|
||||
println("")
|
||||
println(&s, &globalStruct, globalStructPtr.i16, globalStructPtr)
|
||||
globalStructPtr = nil
|
||||
}
|
||||
|
||||
var globalInt int = 301
|
||||
var globalStruct StructWithAllTypeFields
|
||||
var globalStructPtr *StructWithAllTypeFields
|
||||
297
_lldb/llgo_plugin.py
Normal file
297
_lldb/llgo_plugin.py
Normal file
@@ -0,0 +1,297 @@
|
||||
# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring
|
||||
|
||||
from typing import List, Optional, Dict, Any, Tuple
|
||||
import re
|
||||
import lldb
|
||||
|
||||
|
||||
def log(*args: Any, **kwargs: Any) -> None:
|
||||
print(*args, **kwargs, flush=True)
|
||||
|
||||
|
||||
def __lldb_init_module(debugger: lldb.SBDebugger, _: Dict[str, Any]) -> None:
|
||||
debugger.HandleCommand(
|
||||
'command script add -f llgo_plugin.print_go_expression p')
|
||||
debugger.HandleCommand(
|
||||
'command script add -f llgo_plugin.print_all_variables v')
|
||||
|
||||
|
||||
def is_llgo_compiler(_target: lldb.SBTarget) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def get_indexed_value(value: lldb.SBValue, index: int) -> Optional[lldb.SBValue]:
|
||||
if not value or not value.IsValid():
|
||||
return None
|
||||
|
||||
type_name = value.GetType().GetName()
|
||||
|
||||
if type_name.startswith('[]'): # Slice
|
||||
data_ptr = value.GetChildMemberWithName('data')
|
||||
element_type = data_ptr.GetType().GetPointeeType()
|
||||
element_size = element_type.GetByteSize()
|
||||
ptr_value = int(data_ptr.GetValue(), 16)
|
||||
element_address = ptr_value + index * element_size
|
||||
target = value.GetTarget()
|
||||
return target.CreateValueFromAddress(
|
||||
f"element_{index}", lldb.SBAddress(element_address, target), element_type)
|
||||
elif value.GetType().IsArrayType(): # Array
|
||||
return value.GetChildAtIndex(index)
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def evaluate_expression(frame: lldb.SBFrame, expression: str) -> Optional[lldb.SBValue]:
|
||||
parts = re.findall(r'\*|\w+|\(|\)|\[.*?\]|\.', expression)
|
||||
|
||||
def evaluate_part(i: int) -> Tuple[Optional[lldb.SBValue], int]:
|
||||
nonlocal parts
|
||||
value: Optional[lldb.SBValue] = None
|
||||
while i < len(parts):
|
||||
part = parts[i]
|
||||
|
||||
if part == '*':
|
||||
sub_value, i = evaluate_part(i + 1)
|
||||
if sub_value and sub_value.IsValid():
|
||||
value = sub_value.Dereference()
|
||||
else:
|
||||
return None, i
|
||||
elif part == '(':
|
||||
depth = 1
|
||||
j = i + 1
|
||||
while j < len(parts) and depth > 0:
|
||||
if parts[j] == '(':
|
||||
depth += 1
|
||||
elif parts[j] == ')':
|
||||
depth -= 1
|
||||
j += 1
|
||||
value, i = evaluate_part(i + 1)
|
||||
i = j - 1
|
||||
elif part == ')':
|
||||
return value, i + 1
|
||||
elif part == '.':
|
||||
if value is None:
|
||||
value = frame.FindVariable(parts[i+1])
|
||||
else:
|
||||
value = value.GetChildMemberWithName(parts[i+1])
|
||||
i += 2
|
||||
elif part.startswith('['):
|
||||
index = int(part[1:-1])
|
||||
value = get_indexed_value(value, index)
|
||||
i += 1
|
||||
else:
|
||||
if value is None:
|
||||
value = frame.FindVariable(part)
|
||||
else:
|
||||
value = value.GetChildMemberWithName(part)
|
||||
i += 1
|
||||
|
||||
if not value or not value.IsValid():
|
||||
return None, i
|
||||
|
||||
return value, i
|
||||
|
||||
value, _ = evaluate_part(0)
|
||||
return value
|
||||
|
||||
|
||||
def print_go_expression(debugger: lldb.SBDebugger, command: str, result: lldb.SBCommandReturnObject, _internal_dict: Dict[str, Any]) -> None:
|
||||
frame = debugger.GetSelectedTarget().GetProcess(
|
||||
).GetSelectedThread().GetSelectedFrame()
|
||||
value = evaluate_expression(frame, command)
|
||||
if value and value.IsValid():
|
||||
result.AppendMessage(format_value(value, debugger))
|
||||
else:
|
||||
result.AppendMessage(
|
||||
f"Error: Unable to evaluate expression '{command}'")
|
||||
|
||||
|
||||
def print_all_variables(debugger: lldb.SBDebugger, _command: str, result: lldb.SBCommandReturnObject, _internal_dict: Dict[str, Any]) -> None:
|
||||
target = debugger.GetSelectedTarget()
|
||||
if not is_llgo_compiler(target):
|
||||
result.AppendMessage("Not a LLGo compiled binary.")
|
||||
return
|
||||
|
||||
frame = debugger.GetSelectedTarget().GetProcess(
|
||||
).GetSelectedThread().GetSelectedFrame()
|
||||
variables = frame.GetVariables(True, True, True, True)
|
||||
|
||||
output: List[str] = []
|
||||
for var in variables:
|
||||
type_name = map_type_name(var.GetType().GetName())
|
||||
formatted = format_value(var, debugger, include_type=False, indent=0)
|
||||
output.append(f"var {var.GetName()} {type_name} = {formatted}")
|
||||
|
||||
result.AppendMessage("\n".join(output))
|
||||
|
||||
|
||||
def is_pointer(frame: lldb.SBFrame, var_name: str) -> bool:
|
||||
var = frame.FindVariable(var_name)
|
||||
return var.IsValid() and var.GetType().IsPointerType()
|
||||
|
||||
|
||||
def format_value(var: lldb.SBValue, debugger: lldb.SBDebugger, include_type: bool = True, indent: int = 0) -> str:
|
||||
if not var.IsValid():
|
||||
return "<variable not available>"
|
||||
|
||||
var_type = var.GetType()
|
||||
type_class = var_type.GetTypeClass()
|
||||
type_name = map_type_name(var_type.GetName())
|
||||
|
||||
# Handle typedef types
|
||||
original_type_name = type_name
|
||||
while var_type.IsTypedefType():
|
||||
var_type = var_type.GetTypedefedType()
|
||||
type_name = map_type_name(var_type.GetName())
|
||||
type_class = var_type.GetTypeClass()
|
||||
|
||||
if var_type.IsPointerType():
|
||||
return format_pointer(var, debugger, indent, original_type_name)
|
||||
|
||||
if type_name.startswith('[]'): # Slice
|
||||
return format_slice(var, debugger, indent)
|
||||
elif var_type.IsArrayType():
|
||||
return format_array(var, debugger, indent)
|
||||
elif type_name == 'string': # String
|
||||
return format_string(var)
|
||||
elif type_class in [lldb.eTypeClassStruct, lldb.eTypeClassClass]:
|
||||
return format_struct(var, debugger, include_type, indent, original_type_name)
|
||||
else:
|
||||
value = var.GetValue()
|
||||
summary = var.GetSummary()
|
||||
if value is not None:
|
||||
return f"{value}" if include_type else str(value)
|
||||
elif summary is not None:
|
||||
return f"{summary}" if include_type else summary
|
||||
else:
|
||||
return "<variable not available>"
|
||||
|
||||
|
||||
def format_slice(var: lldb.SBValue, debugger: lldb.SBDebugger, indent: int) -> str:
|
||||
length = var.GetChildMemberWithName('len').GetValue()
|
||||
if length is None:
|
||||
return "<variable not available>"
|
||||
length = int(length)
|
||||
data_ptr = var.GetChildMemberWithName('data')
|
||||
elements: List[str] = []
|
||||
|
||||
ptr_value = int(data_ptr.GetValue(), 16)
|
||||
element_type = data_ptr.GetType().GetPointeeType()
|
||||
element_size = element_type.GetByteSize()
|
||||
|
||||
target = debugger.GetSelectedTarget()
|
||||
indent_str = ' ' * indent
|
||||
next_indent_str = ' ' * (indent + 1)
|
||||
|
||||
for i in range(length):
|
||||
element_address = ptr_value + i * element_size
|
||||
element = target.CreateValueFromAddress(
|
||||
f"element_{i}", lldb.SBAddress(element_address, target), element_type)
|
||||
value = format_value(
|
||||
element, debugger, include_type=False, indent=indent+1)
|
||||
elements.append(value)
|
||||
|
||||
type_name = var.GetType().GetName()
|
||||
|
||||
if len(elements) > 5: # 如果元素数量大于5,则进行折行显示
|
||||
result = f"{type_name}{{\n{next_indent_str}" + \
|
||||
f",\n{next_indent_str}".join(elements) + f"\n{indent_str}}}"
|
||||
else:
|
||||
result = f"{type_name}{{{', '.join(elements)}}}"
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def format_array(var: lldb.SBValue, debugger: lldb.SBDebugger, indent: int) -> str:
|
||||
elements: List[str] = []
|
||||
indent_str = ' ' * indent
|
||||
next_indent_str = ' ' * (indent + 1)
|
||||
|
||||
for i in range(var.GetNumChildren()):
|
||||
value = format_value(var.GetChildAtIndex(
|
||||
i), debugger, include_type=False, indent=indent+1)
|
||||
elements.append(value)
|
||||
|
||||
array_size = var.GetNumChildren()
|
||||
element_type = map_type_name(var.GetType().GetArrayElementType().GetName())
|
||||
type_name = f"[{array_size}]{element_type}"
|
||||
|
||||
if len(elements) > 5: # wrap line if too many elements
|
||||
return f"{type_name}{{\n{next_indent_str}" + f",\n{next_indent_str}".join(elements) + f"\n{indent_str}}}"
|
||||
else:
|
||||
return f"{type_name}{{{', '.join(elements)}}}"
|
||||
|
||||
|
||||
def format_string(var: lldb.SBValue) -> str:
|
||||
summary = var.GetSummary()
|
||||
if summary is not None:
|
||||
return summary # Keep the quotes
|
||||
else:
|
||||
data = var.GetChildMemberWithName('data').GetValue()
|
||||
length = var.GetChildMemberWithName('len').GetValue()
|
||||
if data and length:
|
||||
length = int(length)
|
||||
error = lldb.SBError()
|
||||
return '"%s"' % var.process.ReadCStringFromMemory(int(data, 16), length + 1, error)
|
||||
return "<variable not available>"
|
||||
|
||||
|
||||
def format_struct(var: lldb.SBValue, debugger: lldb.SBDebugger, include_type: bool = True, indent: int = 0, type_name: str = "") -> str:
|
||||
children: List[str] = []
|
||||
indent_str = ' ' * indent
|
||||
next_indent_str = ' ' * (indent + 1)
|
||||
|
||||
for i in range(var.GetNumChildren()):
|
||||
child = var.GetChildAtIndex(i)
|
||||
child_name = child.GetName()
|
||||
child_value = format_value(
|
||||
child, debugger, include_type=False, indent=indent+1)
|
||||
children.append(f"{child_name} = {child_value}")
|
||||
|
||||
if len(children) > 5: # 如果字段数量大于5,则进行折行显示
|
||||
struct_content = "{\n" + ",\n".join(
|
||||
[f"{next_indent_str}{child}" for child in children]) + f"\n{indent_str}}}"
|
||||
else:
|
||||
struct_content = f"{{{', '.join(children)}}}"
|
||||
|
||||
if include_type:
|
||||
return f"{type_name}{struct_content}"
|
||||
else:
|
||||
return struct_content
|
||||
|
||||
|
||||
def format_pointer(var: lldb.SBValue, _debugger: lldb.SBDebugger, _indent: int, _type_name: str) -> str:
|
||||
if not var.IsValid() or var.GetValueAsUnsigned() == 0:
|
||||
return "<variable not available>"
|
||||
return var.GetValue() # Return the address as a string
|
||||
|
||||
|
||||
def map_type_name(type_name: str) -> str:
|
||||
# Handle pointer types
|
||||
if type_name.endswith('*'):
|
||||
base_type = type_name[:-1].strip()
|
||||
mapped_base_type = map_type_name(base_type)
|
||||
return f"*{mapped_base_type}"
|
||||
|
||||
# Map other types
|
||||
type_mapping: Dict[str, str] = {
|
||||
'long': 'int',
|
||||
'void': 'unsafe.Pointer',
|
||||
'char': 'byte',
|
||||
'short': 'int16',
|
||||
'int': 'int32',
|
||||
'long long': 'int64',
|
||||
'unsigned char': 'uint8',
|
||||
'unsigned short': 'uint16',
|
||||
'unsigned int': 'uint32',
|
||||
'unsigned long': 'uint',
|
||||
'unsigned long long': 'uint64',
|
||||
'float': 'float32',
|
||||
'double': 'float64',
|
||||
}
|
||||
|
||||
for c_type, go_type in type_mapping.items():
|
||||
if type_name.startswith(c_type):
|
||||
return type_name.replace(c_type, go_type, 1)
|
||||
|
||||
return type_name
|
||||
15
_lldb/runlldb.sh
Executable file
15
_lldb/runlldb.sh
Executable file
@@ -0,0 +1,15 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
# Source common functions and variables
|
||||
# shellcheck source=./_lldb/common.sh
|
||||
source "$(dirname "$0")/common.sh"
|
||||
|
||||
executable="$1"
|
||||
|
||||
# Get the directory of the current script
|
||||
script_dir="$(dirname "$0")"
|
||||
|
||||
# Run LLDB with the LLGO plugin
|
||||
"$LLDB_PATH" -O "command script import ${script_dir}/llgo_plugin.py" "$executable"
|
||||
69
_lldb/runtest.sh
Executable file
69
_lldb/runtest.sh
Executable file
@@ -0,0 +1,69 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
# Source common functions and variables
|
||||
# shellcheck source=./_lldb/common.sh
|
||||
# shellcheck disable=SC1091
|
||||
source "$(dirname "$0")/common.sh" || exit 1
|
||||
|
||||
# Parse command-line arguments
|
||||
package_path="$DEFAULT_PACKAGE_PATH"
|
||||
verbose=False
|
||||
interactive=False
|
||||
plugin_path=None
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
-v|--verbose)
|
||||
verbose=True
|
||||
shift
|
||||
;;
|
||||
-i|--interactive)
|
||||
interactive=True
|
||||
shift
|
||||
;;
|
||||
-p|--plugin)
|
||||
plugin_path="\"$2\""
|
||||
shift 2
|
||||
;;
|
||||
*)
|
||||
package_path="$1"
|
||||
shift
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Build the project
|
||||
build_project "$package_path" || exit 1
|
||||
|
||||
# Set up the result file path
|
||||
result_file="/tmp/lldb_exit_code"
|
||||
|
||||
# Prepare LLDB commands
|
||||
lldb_commands=(
|
||||
"command script import ../llgo_plugin.py"
|
||||
"command script import ../test.py"
|
||||
"script test.run_tests_with_result('./debug.out', ['main.go'], $verbose, $interactive, $plugin_path, '$result_file')"
|
||||
"quit"
|
||||
)
|
||||
|
||||
# Run LLDB with prepared commands
|
||||
lldb_command_string=""
|
||||
for cmd in "${lldb_commands[@]}"; do
|
||||
lldb_command_string+=" -o \"$cmd\""
|
||||
done
|
||||
|
||||
cd "$package_path"
|
||||
# Run LLDB with the test script
|
||||
eval "$LLDB_PATH $lldb_command_string"
|
||||
|
||||
# Read the exit code from the result file
|
||||
if [ -f "$result_file" ]; then
|
||||
exit_code=$(cat "$result_file")
|
||||
rm "$result_file"
|
||||
exit "$exit_code"
|
||||
else
|
||||
echo "Error: Could not find exit code file"
|
||||
exit 1
|
||||
fi
|
||||
402
_lldb/test.py
Normal file
402
_lldb/test.py
Normal file
@@ -0,0 +1,402 @@
|
||||
# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring
|
||||
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
import signal
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List, Optional, Set, Dict, Any
|
||||
import lldb
|
||||
import llgo_plugin
|
||||
from llgo_plugin import log
|
||||
|
||||
|
||||
class LLDBTestException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Test:
|
||||
source_file: str
|
||||
line_number: int
|
||||
variable: str
|
||||
expected_value: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class TestResult:
|
||||
test: Test
|
||||
status: str
|
||||
actual: Optional[str] = None
|
||||
message: Optional[str] = None
|
||||
missing: Optional[Set[str]] = None
|
||||
extra: Optional[Set[str]] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class TestCase:
|
||||
source_file: str
|
||||
start_line: int
|
||||
end_line: int
|
||||
tests: List[Test]
|
||||
|
||||
|
||||
@dataclass
|
||||
class CaseResult:
|
||||
test_case: TestCase
|
||||
function: str
|
||||
results: List[TestResult]
|
||||
|
||||
|
||||
@dataclass
|
||||
class TestResults:
|
||||
total: int = 0
|
||||
passed: int = 0
|
||||
failed: int = 0
|
||||
case_results: List[CaseResult] = field(default_factory=list)
|
||||
|
||||
|
||||
class LLDBDebugger:
|
||||
def __init__(self, executable_path: str, plugin_path: Optional[str] = None) -> None:
|
||||
self.executable_path: str = executable_path
|
||||
self.plugin_path: Optional[str] = plugin_path
|
||||
self.debugger: lldb.SBDebugger = lldb.SBDebugger.Create()
|
||||
self.debugger.SetAsync(False)
|
||||
self.target: Optional[lldb.SBTarget] = None
|
||||
self.process: Optional[lldb.SBProcess] = None
|
||||
self.type_mapping: Dict[str, str] = {
|
||||
'long': 'int',
|
||||
'unsigned long': 'uint',
|
||||
}
|
||||
|
||||
def setup(self) -> None:
|
||||
if self.plugin_path:
|
||||
self.debugger.HandleCommand(
|
||||
f'command script import "{self.plugin_path}"')
|
||||
self.target = self.debugger.CreateTarget(self.executable_path)
|
||||
if not self.target:
|
||||
raise LLDBTestException(
|
||||
f"Failed to create target for {self.executable_path}")
|
||||
|
||||
self.debugger.HandleCommand(
|
||||
'command script add -f llgo_plugin.print_go_expression p')
|
||||
self.debugger.HandleCommand(
|
||||
'command script add -f llgo_plugin.print_all_variables v')
|
||||
|
||||
def set_breakpoint(self, file_spec: str, line_number: int) -> lldb.SBBreakpoint:
|
||||
bp = self.target.BreakpointCreateByLocation(file_spec, line_number)
|
||||
if not bp.IsValid():
|
||||
raise LLDBTestException(
|
||||
f"Failed to set breakpoint at {file_spec}: {line_number}")
|
||||
return bp
|
||||
|
||||
def run_to_breakpoint(self) -> None:
|
||||
if not self.process:
|
||||
self.process = self.target.LaunchSimple(None, None, os.getcwd())
|
||||
else:
|
||||
self.process.Continue()
|
||||
if self.process.GetState() != lldb.eStateStopped:
|
||||
raise LLDBTestException("Process didn't stop at breakpoint")
|
||||
|
||||
def get_variable_value(self, var_expression: str) -> Optional[str]:
|
||||
frame = self.process.GetSelectedThread().GetFrameAtIndex(0)
|
||||
value = llgo_plugin.evaluate_expression(frame, var_expression)
|
||||
if value and value.IsValid():
|
||||
return llgo_plugin.format_value(value, self.debugger)
|
||||
return None
|
||||
|
||||
def get_all_variable_names(self) -> Set[str]:
|
||||
frame = self.process.GetSelectedThread().GetFrameAtIndex(0)
|
||||
return set(var.GetName() for var in frame.GetVariables(True, True, True, True))
|
||||
|
||||
def get_current_function_name(self) -> str:
|
||||
frame = self.process.GetSelectedThread().GetFrameAtIndex(0)
|
||||
return frame.GetFunctionName()
|
||||
|
||||
def cleanup(self) -> None:
|
||||
if self.process and self.process.IsValid():
|
||||
self.process.Kill()
|
||||
lldb.SBDebugger.Destroy(self.debugger)
|
||||
|
||||
def run_console(self) -> bool:
|
||||
log("\nEntering LLDB interactive mode.")
|
||||
log("Type 'quit' to exit and continue with the next test case.")
|
||||
log("Use Ctrl+D to exit and continue, or Ctrl+C to abort all tests.")
|
||||
|
||||
old_stdin, old_stdout, old_stderr = sys.stdin, sys.stdout, sys.stderr
|
||||
sys.stdin, sys.stdout, sys.stderr = sys.__stdin__, sys.__stdout__, sys.__stderr__
|
||||
|
||||
self.debugger.SetAsync(True)
|
||||
self.debugger.HandleCommand("settings set auto-confirm true")
|
||||
self.debugger.HandleCommand("command script import lldb")
|
||||
|
||||
interpreter = self.debugger.GetCommandInterpreter()
|
||||
continue_tests = True
|
||||
|
||||
def keyboard_interrupt_handler(_sig: Any, _frame: Any) -> None:
|
||||
nonlocal continue_tests
|
||||
log("\nTest execution aborted by user.")
|
||||
continue_tests = False
|
||||
raise KeyboardInterrupt
|
||||
|
||||
original_handler = signal.signal(
|
||||
signal.SIGINT, keyboard_interrupt_handler)
|
||||
|
||||
try:
|
||||
while continue_tests:
|
||||
log("\n(lldb) ", end="")
|
||||
try:
|
||||
command = input().strip()
|
||||
except EOFError:
|
||||
log("\nExiting LLDB interactive mode. Continuing with next test case.")
|
||||
break
|
||||
except KeyboardInterrupt:
|
||||
break
|
||||
|
||||
if command.lower() == 'quit':
|
||||
log("\nExiting LLDB interactive mode. Continuing with next test case.")
|
||||
break
|
||||
|
||||
result = lldb.SBCommandReturnObject()
|
||||
interpreter.HandleCommand(command, result)
|
||||
log(result.GetOutput().rstrip() if result.Succeeded()
|
||||
else result.GetError().rstrip())
|
||||
|
||||
finally:
|
||||
signal.signal(signal.SIGINT, original_handler)
|
||||
sys.stdin, sys.stdout, sys.stderr = old_stdin, old_stdout, old_stderr
|
||||
|
||||
return continue_tests
|
||||
|
||||
|
||||
def parse_expected_values(source_files: List[str]) -> List[TestCase]:
|
||||
test_cases: List[TestCase] = []
|
||||
for source_file in source_files:
|
||||
with open(source_file, 'r', encoding='utf-8') as f:
|
||||
content = f.readlines()
|
||||
i = 0
|
||||
while i < len(content):
|
||||
line = content[i].strip()
|
||||
if line.startswith('// Expected:'):
|
||||
start_line = i + 1
|
||||
tests: List[Test] = []
|
||||
i += 1
|
||||
while i < len(content):
|
||||
line = content[i].strip()
|
||||
if not line.startswith('//'):
|
||||
break
|
||||
parts = line.lstrip('//').strip().split(':', 1)
|
||||
if len(parts) == 2:
|
||||
var, value = map(str.strip, parts)
|
||||
tests.append(Test(source_file, i + 1, var, value))
|
||||
i += 1
|
||||
end_line = i
|
||||
test_cases.append(
|
||||
TestCase(source_file, start_line, end_line, tests))
|
||||
else:
|
||||
i += 1
|
||||
return test_cases
|
||||
|
||||
|
||||
def execute_tests(executable_path: str, test_cases: List[TestCase], verbose: bool, interactive: bool, plugin_path: Optional[str]) -> TestResults:
|
||||
results = TestResults()
|
||||
|
||||
for test_case in test_cases:
|
||||
debugger = LLDBDebugger(executable_path, plugin_path)
|
||||
try:
|
||||
if verbose:
|
||||
log(
|
||||
f"\nSetting breakpoint at {test_case.source_file}:{test_case.end_line}")
|
||||
debugger.setup()
|
||||
debugger.set_breakpoint(test_case.source_file, test_case.end_line)
|
||||
debugger.run_to_breakpoint()
|
||||
|
||||
all_variable_names = debugger.get_all_variable_names()
|
||||
|
||||
case_result = execute_test_case(
|
||||
debugger, test_case, all_variable_names)
|
||||
|
||||
results.total += len(case_result.results)
|
||||
results.passed += sum(1 for r in case_result.results if r.status == 'pass')
|
||||
results.failed += sum(1 for r in case_result.results if r.status != 'pass')
|
||||
results.case_results.append(case_result)
|
||||
|
||||
case = case_result.test_case
|
||||
loc = f"{case.source_file}:{case.start_line}-{case.end_line}"
|
||||
if verbose or interactive or any(r.status != 'pass' for r in case_result.results):
|
||||
log(f"\nTest case: {loc} in function '{case_result.function}'")
|
||||
for result in case_result.results:
|
||||
print_test_result(result, verbose=verbose)
|
||||
|
||||
if interactive and any(r.status != 'pass' for r in case_result.results):
|
||||
log("\nTest case failed. Entering LLDB interactive mode.")
|
||||
continue_tests = debugger.run_console()
|
||||
if not continue_tests:
|
||||
log("Aborting all tests.")
|
||||
break
|
||||
|
||||
finally:
|
||||
debugger.cleanup()
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def run_tests(executable_path: str, source_files: List[str], verbose: bool, interactive: bool, plugin_path: Optional[str]) -> int:
|
||||
test_cases = parse_expected_values(source_files)
|
||||
if verbose:
|
||||
log(f"Running tests for {', '.join(source_files)} with {executable_path}")
|
||||
log(f"Found {len(test_cases)} test cases")
|
||||
|
||||
results = execute_tests(executable_path, test_cases,
|
||||
verbose, interactive, plugin_path)
|
||||
print_test_results(results)
|
||||
|
||||
# Return 0 if all tests passed, 1 otherwise
|
||||
return 0 if results.failed == 0 else 1
|
||||
|
||||
|
||||
def execute_test_case(debugger: LLDBDebugger, test_case: TestCase, all_variable_names: Set[str]) -> CaseResult:
|
||||
results: List[TestResult] = []
|
||||
|
||||
for test in test_case.tests:
|
||||
if test.variable == "all variables":
|
||||
result = execute_all_variables_test(test, all_variable_names)
|
||||
else:
|
||||
result = execute_single_variable_test(debugger, test)
|
||||
results.append(result)
|
||||
|
||||
return CaseResult(test_case, debugger.get_current_function_name(), results)
|
||||
|
||||
|
||||
def execute_all_variables_test(test: Test, all_variable_names: Set[str]) -> TestResult:
|
||||
expected_vars = set(test.expected_value.split())
|
||||
if expected_vars == all_variable_names:
|
||||
return TestResult(
|
||||
test=test,
|
||||
status='pass',
|
||||
actual=all_variable_names
|
||||
)
|
||||
else:
|
||||
return TestResult(
|
||||
test=test,
|
||||
status='fail',
|
||||
actual=all_variable_names,
|
||||
missing=expected_vars - all_variable_names,
|
||||
extra=all_variable_names - expected_vars
|
||||
)
|
||||
|
||||
|
||||
def execute_single_variable_test(debugger: LLDBDebugger, test: Test) -> TestResult:
|
||||
actual_value = debugger.get_variable_value(test.variable)
|
||||
if actual_value is None:
|
||||
return TestResult(
|
||||
test=test,
|
||||
status='error',
|
||||
message=f'Unable to fetch value for {test.variable}'
|
||||
)
|
||||
|
||||
actual_value = actual_value.strip()
|
||||
expected_value = test.expected_value.strip()
|
||||
|
||||
if actual_value == expected_value:
|
||||
return TestResult(
|
||||
test=test,
|
||||
status='pass',
|
||||
actual=actual_value
|
||||
)
|
||||
else:
|
||||
return TestResult(
|
||||
test=test,
|
||||
status='fail',
|
||||
actual=actual_value
|
||||
)
|
||||
|
||||
|
||||
def print_test_results(results: TestResults) -> None:
|
||||
log("\nTest results:")
|
||||
log(f" Total tests: {results.total}")
|
||||
log(f" Passed tests: {results.passed}")
|
||||
log(f" Failed tests: {results.failed}")
|
||||
if results.total == results.passed:
|
||||
log("All tests passed!")
|
||||
else:
|
||||
log("Some tests failed")
|
||||
|
||||
|
||||
def print_test_result(result: TestResult, verbose: bool) -> None:
|
||||
status_symbol = "✓" if result.status == 'pass' else "✗"
|
||||
status_text = "Pass" if result.status == 'pass' else "Fail"
|
||||
test = result.test
|
||||
|
||||
if result.status == 'pass':
|
||||
if verbose:
|
||||
log(f"{status_symbol} Line {test.line_number}, {test.variable}: {status_text}")
|
||||
if test.variable == 'all variables':
|
||||
log(f" Variables: {', '.join(sorted(result.actual))}")
|
||||
else: # fail or error
|
||||
log(f"{status_symbol} Line {test.line_number}, {test.variable}: {status_text}")
|
||||
if test.variable == 'all variables':
|
||||
if result.missing:
|
||||
log(f" Missing variables: {', '.join(sorted(result.missing))}")
|
||||
if result.extra:
|
||||
log(f" Extra variables: {', '.join(sorted(result.extra))}")
|
||||
log(f" Expected: {', '.join(sorted(test.expected_value.split()))}")
|
||||
log(f" Actual: {', '.join(sorted(result.actual))}")
|
||||
elif result.status == 'error':
|
||||
log(f" Error: {result.message}")
|
||||
else:
|
||||
log(f" Expected: {test.expected_value}")
|
||||
log(f" Actual: {result.actual}")
|
||||
|
||||
|
||||
def run_tests_with_result(executable_path: str, source_files: List[str], verbose: bool, interactive: bool, plugin_path: Optional[str], result_path: str) -> int:
|
||||
try:
|
||||
exit_code = run_tests(executable_path, source_files,
|
||||
verbose, interactive, plugin_path)
|
||||
except Exception as e:
|
||||
log(f"An error occurred during test execution: {str(e)}")
|
||||
exit_code = 2 # Use a different exit code for unexpected errors
|
||||
|
||||
try:
|
||||
with open(result_path, 'w', encoding='utf-8') as f:
|
||||
f.write(str(exit_code))
|
||||
except IOError as e:
|
||||
log(f"Error writing result to file {result_path}: {str(e)}")
|
||||
# If we can't write to the file, we should still return the exit code
|
||||
|
||||
return exit_code
|
||||
|
||||
|
||||
def main() -> None:
|
||||
log(sys.argv)
|
||||
parser = argparse.ArgumentParser(
|
||||
description="LLDB 18 Debug Script with DWARF 5 Support")
|
||||
parser.add_argument("executable", help="Path to the executable")
|
||||
parser.add_argument("sources", nargs='+', help="Paths to the source files")
|
||||
parser.add_argument("-v", "--verbose", action="store_true",
|
||||
help="Enable verbose output")
|
||||
parser.add_argument("-i", "--interactive", action="store_true",
|
||||
help="Enable interactive mode on test failure")
|
||||
parser.add_argument("--plugin", help="Path to the LLDB plugin")
|
||||
parser.add_argument("--result-path", help="Path to write the result")
|
||||
args = parser.parse_args()
|
||||
|
||||
plugin_path = args.plugin or os.path.join(os.path.dirname(
|
||||
os.path.realpath(__file__)), "go_lldb_plugin.py")
|
||||
|
||||
try:
|
||||
if args.result_path:
|
||||
exit_code = run_tests_with_result(args.executable, args.sources,
|
||||
args.verbose, args.interactive, plugin_path, args.result_path)
|
||||
else:
|
||||
exit_code = run_tests(args.executable, args.sources,
|
||||
args.verbose, args.interactive, plugin_path)
|
||||
except Exception as e:
|
||||
log(f"An unexpected error occurred: {str(e)}")
|
||||
exit_code = 2 # Use a different exit code for unexpected errors
|
||||
|
||||
sys.exit(exit_code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,9 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/py"
|
||||
"github.com/goplus/llgo/py/math"
|
||||
"github.com/goplus/llgo/py/std"
|
||||
"github.com/goplus/lib/py"
|
||||
"github.com/goplus/lib/py/math"
|
||||
"github.com/goplus/lib/py/std"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
5
_pydemo/go.mod
Normal file
5
_pydemo/go.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module github.com/goplus/llgo/_pydemo
|
||||
|
||||
go 1.20
|
||||
|
||||
require github.com/goplus/lib v0.2.0
|
||||
2
_pydemo/go.sum
Normal file
2
_pydemo/go.sum
Normal file
@@ -0,0 +1,2 @@
|
||||
github.com/goplus/lib v0.2.0 h1:AjqkN1XK5H23wZMMlpaUYAMCDAdSBQ2NMFrLtSh7W4g=
|
||||
github.com/goplus/lib v0.2.0/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=
|
||||
@@ -1,9 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/py"
|
||||
"github.com/goplus/llgo/py/numpy"
|
||||
"github.com/goplus/llgo/py/std"
|
||||
"github.com/goplus/lib/py"
|
||||
"github.com/goplus/lib/py/numpy"
|
||||
"github.com/goplus/lib/py/std"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/py"
|
||||
"github.com/goplus/llgo/py/std"
|
||||
"github.com/goplus/lib/py"
|
||||
"github.com/goplus/lib/py/std"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/py/math"
|
||||
"github.com/goplus/lib/c"
|
||||
"github.com/goplus/lib/py/math"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/py"
|
||||
"github.com/goplus/llgo/py/std"
|
||||
"github.com/goplus/lib/py"
|
||||
"github.com/goplus/lib/py/std"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/py"
|
||||
"github.com/goplus/llgo/py/statistics"
|
||||
"github.com/goplus/lib/c"
|
||||
"github.com/goplus/lib/py"
|
||||
"github.com/goplus/lib/py/statistics"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/py"
|
||||
"github.com/goplus/llgo/py/std"
|
||||
"github.com/goplus/llgo/py/torch"
|
||||
"github.com/goplus/lib/py"
|
||||
"github.com/goplus/lib/py/std"
|
||||
"github.com/goplus/lib/py/torch"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/clang"
|
||||
"github.com/goplus/lib/c"
|
||||
"github.com/goplus/lib/c/clang"
|
||||
)
|
||||
|
||||
type Data struct {
|
||||
@@ -61,7 +61,9 @@ func printType(t clang.Type, data *Data) {
|
||||
case clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray, clang.TypeConstantArray:
|
||||
printType(t.ArrayElementType(), data)
|
||||
case clang.TypeTypedef:
|
||||
printType(t.CanonicalType(), data)
|
||||
printType(t.TypeDeclaration().TypedefDeclUnderlyingType(), data)
|
||||
case clang.TypeElaborated:
|
||||
printType(t.NamedType(), data)
|
||||
case clang.TypeFunctionProto:
|
||||
printType(t.ResultType(), data)
|
||||
for i := 0; i < int(t.NumArgTypes()); i++ {
|
||||
5
_xtool/go.mod
Normal file
5
_xtool/go.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module github.com/goplus/llgo/_xtool
|
||||
|
||||
go 1.20
|
||||
|
||||
require github.com/goplus/lib v0.2.0
|
||||
2
_xtool/go.sum
Normal file
2
_xtool/go.sum
Normal file
@@ -0,0 +1,2 @@
|
||||
github.com/goplus/lib v0.2.0 h1:AjqkN1XK5H23wZMMlpaUYAMCDAdSBQ2NMFrLtSh7W4g=
|
||||
github.com/goplus/lib v0.2.0/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=
|
||||
@@ -17,10 +17,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/cjson"
|
||||
"github.com/goplus/llgo/py"
|
||||
"github.com/goplus/llgo/py/inspect"
|
||||
"github.com/goplus/lib/c"
|
||||
"github.com/goplus/lib/c/cjson"
|
||||
"github.com/goplus/lib/py"
|
||||
"github.com/goplus/lib/py/inspect"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -38,7 +38,7 @@ func main() {
|
||||
items := cjson.Array()
|
||||
mod := py.ImportModule(pyLib)
|
||||
keys := mod.ModuleGetDict().DictKeys()
|
||||
for i, n := uintptr(0), keys.ListLen(); i < n; i++ {
|
||||
for i, n := 0, keys.ListLen(); i < n; i++ {
|
||||
key := keys.ListItem(i)
|
||||
val := mod.GetAttr(key)
|
||||
doc := val.GetAttrString(c.Str("__doc__"))
|
||||
@@ -1,17 +0,0 @@
|
||||
typedef union {
|
||||
double d;
|
||||
float f;
|
||||
long v;
|
||||
} castUnion;
|
||||
|
||||
double llgoToFloat64(long v) {
|
||||
castUnion k;
|
||||
k.v = v;
|
||||
return k.d;
|
||||
}
|
||||
|
||||
float llgoToFloat32(long v) {
|
||||
castUnion k;
|
||||
k.v = v;
|
||||
return k.f;
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
LLGo wrapper of DaveGamble/cJSON
|
||||
=====
|
||||
|
||||
## How to install
|
||||
|
||||
### on macOS (Homebrew)
|
||||
|
||||
```sh
|
||||
brew install cjson
|
||||
```
|
||||
|
||||
### on Linux (Debian/Ubuntu)
|
||||
|
||||
```sh
|
||||
apt-get install -y libcjson-dev
|
||||
```
|
||||
|
||||
## Demos
|
||||
|
||||
The `_demo` directory contains our demos (it start with `_` to prevent the `go` command from compiling it):
|
||||
|
||||
* [mkjson](_demo/mkjson/mkjson.go): create a json object and print it
|
||||
|
||||
### How to run demos
|
||||
|
||||
To run the demos in directory `_demo`:
|
||||
|
||||
```sh
|
||||
cd <demo-directory> # eg. cd _demo/mkjson
|
||||
llgo run .
|
||||
```
|
||||
@@ -1,43 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/cjson"
|
||||
)
|
||||
|
||||
func main() {
|
||||
mod := cjson.Object()
|
||||
mod.SetItem(c.Str("name"), cjson.String(c.Str("math")))
|
||||
|
||||
syms := cjson.Array()
|
||||
|
||||
fn := cjson.Object()
|
||||
fn.SetItem(c.Str("name"), cjson.String(c.Str("sqrt")))
|
||||
fn.SetItem(c.Str("sig"), cjson.String(c.Str("(x, /)")))
|
||||
syms.AddItem(fn)
|
||||
|
||||
v := cjson.Object()
|
||||
v.SetItem(c.Str("name"), cjson.String(c.Str("pi")))
|
||||
syms.AddItem(v)
|
||||
|
||||
mod.SetItem(c.Str("items"), syms)
|
||||
|
||||
cstr := mod.CStr()
|
||||
str := c.GoString(cstr)
|
||||
c.Printf(c.Str("%s\n"), cstr)
|
||||
cjson.FreeCStr(cstr)
|
||||
|
||||
mod.Delete()
|
||||
|
||||
cjsonLoad(str)
|
||||
}
|
||||
|
||||
func cjsonLoad(str string) {
|
||||
mod := cjson.ParseString(str)
|
||||
|
||||
cstr := mod.Print()
|
||||
c.Printf(c.Str("%s\n"), cstr)
|
||||
cjson.FreeCStr(cstr)
|
||||
|
||||
mod.Delete()
|
||||
}
|
||||
153
c/cjson/cjson.go
153
c/cjson/cjson.go
@@ -1,153 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cjson
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
)
|
||||
|
||||
const (
|
||||
LLGoPackage = "link: $(pkg-config --libs libcjson); -lcjson"
|
||||
)
|
||||
|
||||
// llgo:type C
|
||||
type JSON struct {
|
||||
Unused [0]byte
|
||||
}
|
||||
|
||||
//go:linkname Parse C.cJSON_Parse
|
||||
func Parse(value *c.Char) *JSON
|
||||
|
||||
//go:linkname ParseWithLength C.cJSON_ParseWithLength
|
||||
func ParseWithLength(value *byte, valueLength uintptr) *JSON
|
||||
|
||||
func ParseBytes(value []byte) *JSON {
|
||||
return ParseWithLength(unsafe.SliceData(value), uintptr(len(value)))
|
||||
}
|
||||
|
||||
func ParseString(value string) *JSON {
|
||||
return ParseWithLength(unsafe.StringData(value), uintptr(len(value)))
|
||||
}
|
||||
|
||||
//go:linkname Null C.cJSON_CreateNull
|
||||
func Null() *JSON
|
||||
|
||||
//go:linkname True C.cJSON_CreateTrue
|
||||
func True() *JSON
|
||||
|
||||
//go:linkname False C.cJSON_CreateFalse
|
||||
func False() *JSON
|
||||
|
||||
//go:linkname Bool C.cJSON_CreateBool
|
||||
func Bool(boolean c.Int) *JSON
|
||||
|
||||
//go:linkname Number C.cJSON_CreateNumber
|
||||
func Number(num float64) *JSON
|
||||
|
||||
//go:linkname String C.cJSON_CreateString
|
||||
func String(str *c.Char) *JSON
|
||||
|
||||
//go:linkname Array C.cJSON_CreateArray
|
||||
func Array() *JSON
|
||||
|
||||
//go:linkname Object C.cJSON_CreateObject
|
||||
func Object() *JSON
|
||||
|
||||
// raw json
|
||||
//
|
||||
//go:linkname Raw C.cJSON_CreateRaw
|
||||
func Raw(raw *c.Char) *JSON
|
||||
|
||||
// Create a string where valuestring references a string so
|
||||
// it will not be freed by Delete
|
||||
//
|
||||
//go:linkname StringRef C.cJSON_CreateStringReference
|
||||
func StringRef(str *c.Char) *JSON
|
||||
|
||||
// Create an object that only references it's elements so
|
||||
// they will not be freed by Delete
|
||||
//
|
||||
//go:linkname ObjectRef C.cJSON_CreateObjectReference
|
||||
func ObjectRef(child *JSON) *JSON
|
||||
|
||||
// Create an array that only references it's elements so
|
||||
// they will not be freed by Delete
|
||||
//
|
||||
//go:linkname ArrayRef C.cJSON_CreateArrayReference
|
||||
func ArrayRef(child *JSON) *JSON
|
||||
|
||||
// Delete a JSON entity and all subentities.
|
||||
//
|
||||
// llgo:link (*JSON).Delete C.cJSON_Delete
|
||||
func (o *JSON) Delete() {}
|
||||
|
||||
// Append item to the specified array.
|
||||
//
|
||||
// llgo:link (*JSON).AddItem C.cJSON_AddItemToArray
|
||||
func (o *JSON) AddItem(item *JSON) c.Int { return 0 }
|
||||
|
||||
// Append item to the specified object.
|
||||
//
|
||||
// llgo:link (*JSON).SetItem C.cJSON_AddItemToObject
|
||||
func (o *JSON) SetItem(key *c.Char, item *JSON) c.Int { return 0 }
|
||||
|
||||
// llgo:link (*JSON).CStr C.cJSON_PrintUnformatted
|
||||
func (o *JSON) CStr() *c.Char { return nil }
|
||||
|
||||
// Same as CStr. Provided for Go+.
|
||||
//
|
||||
// llgo:link (*JSON).Cstr C.cJSON_PrintUnformatted
|
||||
func (o *JSON) Cstr() *c.Char { return nil }
|
||||
|
||||
// Render a JSON entity to text for transfer/storage.
|
||||
//
|
||||
// llgo:link (*JSON).Print C.cJSON_Print
|
||||
func (o *JSON) Print() *c.Char { return nil }
|
||||
|
||||
// Render a JSON entity to text for transfer/storage without any formatting.
|
||||
//
|
||||
// llgo:link (*JSON).PrintUnformatted C.cJSON_PrintUnformatted
|
||||
func (o *JSON) PrintUnformatted() *c.Char { return nil }
|
||||
|
||||
// Render a JSON entity to text using a buffered strategy.
|
||||
//
|
||||
// prebuffer is a guess at the final size. guessing well reduces reallocation.
|
||||
//
|
||||
// fmt=0 gives unformatted, =1 gives formatted.
|
||||
//
|
||||
// llgo:link (*JSON).PrintBuffered C.cJSON_PrintBuffered
|
||||
func (o *JSON) PrintBuffered(prebuffer c.Int, fmt c.Int) *c.Char { return nil }
|
||||
|
||||
// llgo:link (*JSON).GetObjectItemCaseSensitive C.cJSON_GetObjectItemCaseSensitive
|
||||
func (o *JSON) GetObjectItemCaseSensitive(key *c.Char) *JSON { return nil }
|
||||
|
||||
// llgo:link (*JSON).GetArraySize C.cJSON_GetArraySize
|
||||
func (o *JSON) GetArraySize() c.Int { return 0 }
|
||||
|
||||
// llgo:link (*JSON).GetArrayItem C.cJSON_GetArrayItem
|
||||
func (o *JSON) GetArrayItem(index c.Int) *JSON { return nil }
|
||||
|
||||
// llgo:link (*JSON).GetStringValue C.cJSON_GetStringValue
|
||||
func (o *JSON) GetStringValue() *c.Char { return nil }
|
||||
|
||||
//go:linkname Free C.cJSON_free
|
||||
func Free(ptr unsafe.Pointer)
|
||||
|
||||
//go:linkname FreeCStr C.cJSON_free
|
||||
func FreeCStr(*c.Char)
|
||||
@@ -1,163 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/clang"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
namespaceName string
|
||||
className string
|
||||
unit *clang.TranslationUnit
|
||||
}
|
||||
|
||||
func newContext() *Context {
|
||||
return &Context{}
|
||||
}
|
||||
|
||||
func (c *Context) setNamespaceName(name string) {
|
||||
c.namespaceName = name
|
||||
}
|
||||
|
||||
func (c *Context) setClassName(name string) {
|
||||
c.className = name
|
||||
}
|
||||
|
||||
func (c *Context) setUnit(unit *clang.TranslationUnit) {
|
||||
c.unit = unit
|
||||
}
|
||||
|
||||
var context = newContext()
|
||||
|
||||
func printCursorLocation(cursor clang.Cursor) {
|
||||
loc := cursor.Location()
|
||||
var file clang.File
|
||||
var line, column c.Uint
|
||||
|
||||
loc.SpellingLocation(&file, &line, &column, nil)
|
||||
filename := file.FileName()
|
||||
defer filename.Dispose()
|
||||
|
||||
c.Printf(c.Str("%s:%d:%d\n"), filename.CStr(), line, column)
|
||||
}
|
||||
|
||||
func printMarcoInfo(cursor clang.Cursor) {
|
||||
printCursorLocation(cursor)
|
||||
name := cursor.String()
|
||||
c.Printf(c.Str("Marco Name: %s\n"), name.CStr())
|
||||
ran := cursor.Extent()
|
||||
var numTokens c.Uint
|
||||
var tokens *clang.Token
|
||||
context.unit.Tokenize(ran, &tokens, &numTokens)
|
||||
c.Printf(c.Str("Content: "))
|
||||
|
||||
tokensSlice := unsafe.Slice(tokens, int(numTokens))
|
||||
for _, tok := range tokensSlice {
|
||||
tokStr := context.unit.Token(tok)
|
||||
c.Printf(c.Str("%s "), tokStr.CStr())
|
||||
tokStr.Dispose()
|
||||
}
|
||||
|
||||
c.Printf(c.Str("\n"))
|
||||
println("--------------------------------")
|
||||
}
|
||||
func printFuncInfo(cursor clang.Cursor) {
|
||||
printCursorLocation(cursor)
|
||||
|
||||
cursorStr := cursor.String()
|
||||
symbol := cursor.Mangling()
|
||||
defer symbol.Dispose()
|
||||
defer cursorStr.Dispose()
|
||||
|
||||
if context.namespaceName != "" && context.className != "" {
|
||||
fmt.Printf("%s:%s:", context.namespaceName, context.className)
|
||||
} else if context.namespaceName != "" {
|
||||
fmt.Printf("%s:", context.namespaceName)
|
||||
}
|
||||
c.Printf(c.Str("%s\n"), cursorStr.CStr())
|
||||
|
||||
if cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorFunctionDecl {
|
||||
c.Printf(c.Str("symbol:%s\n"), symbol.CStr())
|
||||
|
||||
typeStr := cursor.ResultType().String()
|
||||
defer typeStr.Dispose()
|
||||
c.Printf(c.Str("Return Type: %s\n"), typeStr.CStr())
|
||||
c.Printf(c.Str("Parameters(%d): ( "), cursor.NumArguments())
|
||||
|
||||
for i := 0; i < int(cursor.NumArguments()); i++ {
|
||||
argCurSor := cursor.Argument(c.Uint(i))
|
||||
argType := argCurSor.Type().String()
|
||||
argName := argCurSor.String()
|
||||
c.Printf(c.Str("%s %s"), argType.CStr(), argName.CStr())
|
||||
if i < int(cursor.NumArguments())-1 {
|
||||
c.Printf(c.Str(", "))
|
||||
}
|
||||
|
||||
argType.Dispose()
|
||||
argName.Dispose()
|
||||
}
|
||||
|
||||
c.Printf(c.Str(" )\n"))
|
||||
println("--------------------------------")
|
||||
}
|
||||
}
|
||||
|
||||
func visit(cursor, parent clang.Cursor, clientData c.Pointer) clang.ChildVisitResult {
|
||||
if cursor.Kind == clang.CursorMacroDefinition {
|
||||
printMarcoInfo(cursor)
|
||||
} else if cursor.Kind == clang.CursorNamespace {
|
||||
nameStr := cursor.String()
|
||||
context.setNamespaceName(c.GoString(nameStr.CStr()))
|
||||
clang.VisitChildren(cursor, visit, nil)
|
||||
context.setNamespaceName("")
|
||||
} else if cursor.Kind == clang.CursorClassDecl {
|
||||
nameStr := cursor.String()
|
||||
context.setClassName(c.GoString(nameStr.CStr()))
|
||||
clang.VisitChildren(cursor, visit, nil)
|
||||
context.setClassName("")
|
||||
} else if cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorFunctionDecl {
|
||||
printFuncInfo(cursor)
|
||||
}
|
||||
|
||||
return clang.ChildVisit_Continue
|
||||
}
|
||||
|
||||
func parse(filename *c.Char) {
|
||||
index := clang.CreateIndex(0, 0)
|
||||
args := make([]*c.Char, 3)
|
||||
args[0] = c.Str("-x")
|
||||
args[1] = c.Str("c++")
|
||||
args[2] = c.Str("-std=c++11")
|
||||
unit := index.ParseTranslationUnit(
|
||||
filename,
|
||||
unsafe.SliceData(args), 3,
|
||||
nil, 0,
|
||||
clang.DetailedPreprocessingRecord,
|
||||
)
|
||||
|
||||
if unit == nil {
|
||||
println("Unable to parse translation unit. Quitting.")
|
||||
c.Exit(1)
|
||||
}
|
||||
|
||||
context.setUnit(unit)
|
||||
cursor := unit.Cursor()
|
||||
|
||||
clang.VisitChildren(cursor, visit, nil)
|
||||
unit.Dispose()
|
||||
index.Dispose()
|
||||
}
|
||||
|
||||
func main() {
|
||||
if c.Argc != 2 {
|
||||
fmt.Fprintln(os.Stderr, "Usage: <C++ header file>\n")
|
||||
return
|
||||
} else {
|
||||
sourceFile := *c.Advance(c.Argv, 1)
|
||||
parse(sourceFile)
|
||||
}
|
||||
}
|
||||
@@ -1,184 +0,0 @@
|
||||
#include <clang-c/Index.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef enum CXChildVisitResult (*wrap_CXCursorVisitor)(CXCursor *cursor, CXCursor *parent, CXClientData client_data);
|
||||
|
||||
typedef struct {
|
||||
CXClientData data;
|
||||
wrap_CXCursorVisitor visitor;
|
||||
} wrap_data;
|
||||
|
||||
CXChildVisitResult wrap_visitor(CXCursor cursor, CXCursor parent, CXClientData data) {
|
||||
wrap_data *d = (wrap_data *)(data);
|
||||
return d->visitor(&cursor, &parent, d->data);
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
void wrap_clang_getTranslationUnitCursor(CXTranslationUnit uint, CXCursor *cur) {
|
||||
*cur = clang_getTranslationUnitCursor(uint);
|
||||
}
|
||||
|
||||
unsigned wrap_clang_equalCursors(CXCursor *cursor1, CXCursor *cursor2) {
|
||||
return clang_equalCursors(*cursor1, *cursor2);
|
||||
}
|
||||
|
||||
int wrap_clang_Cursor_isNull(CXCursor *cursor) { return clang_Cursor_isNull(*cursor); }
|
||||
|
||||
void wrap_clang_getCursorSemanticParent(CXCursor *C, CXCursor *parent) { *parent = clang_getCursorSemanticParent(*C); }
|
||||
|
||||
void wrap_clang_getCursorLexicalParent(CXCursor *C, CXCursor *parent) { *parent = clang_getCursorLexicalParent(*C); }
|
||||
|
||||
void wrap_clang_getOverriddenCursors(CXCursor *cursor, CXCursor **overridden, unsigned *num_overridden) {
|
||||
clang_getOverriddenCursors(*cursor, overridden, num_overridden);
|
||||
}
|
||||
|
||||
void wrap_clang_getCursorLocation(CXCursor *cur, CXSourceLocation *loc) { *loc = clang_getCursorLocation(*cur); }
|
||||
|
||||
void wrap_clang_getCursorExtent(CXCursor *cur, CXSourceRange *range) { *range = clang_getCursorExtent(*cur); }
|
||||
|
||||
void wrap_clang_getCursorType(CXCursor *cur, CXType *typ) { *typ = clang_getCursorType(*cur); }
|
||||
|
||||
CXString wrap_clang_getTypeSpelling(CXType *typ) { return clang_getTypeSpelling(*typ); }
|
||||
|
||||
void wrap_clang_getTypedefDeclUnderlyingType(CXCursor *cur, CXType *typ) {
|
||||
*typ = clang_getTypedefDeclUnderlyingType(*cur);
|
||||
}
|
||||
|
||||
long long wrap_clang_getEnumConstantDeclValue(CXCursor *cur) { return clang_getEnumConstantDeclValue(*cur); }
|
||||
|
||||
int wrap_clang_Cursor_getNumArguments(CXCursor *cur) { return clang_Cursor_getNumArguments(*cur); }
|
||||
|
||||
void wrap_clang_Cursor_getArgument(CXCursor *C, unsigned i, CXCursor *argCur) {
|
||||
*argCur = clang_Cursor_getArgument(*C, i);
|
||||
}
|
||||
|
||||
void wrap_clang_getCanonicalType(CXType *typ, CXType *canonicalType) { *canonicalType = clang_getCanonicalType(*typ); }
|
||||
|
||||
unsigned wrap_clang_isConstQualifiedType(CXType *typ) { return clang_isConstQualifiedType(*typ); }
|
||||
|
||||
unsigned wrap_clang_Cursor_isMacroFunctionLike(CXCursor *cur) { return clang_Cursor_isMacroFunctionLike(*cur); }
|
||||
|
||||
unsigned wrap_clang_Cursor_isMacroBuiltin(CXCursor *cur) { return clang_Cursor_isMacroBuiltin(*cur); }
|
||||
|
||||
unsigned wrap_clang_Cursor_isFunctionInlined(CXCursor *cur) { return clang_Cursor_isFunctionInlined(*cur); }
|
||||
|
||||
unsigned wrap_clang_isVolatileQualifiedType(CXType *T) { return clang_isVolatileQualifiedType(*T); }
|
||||
|
||||
unsigned wrap_clang_isRestrictQualifiedType(CXType *T) { return clang_isRestrictQualifiedType(*T); }
|
||||
|
||||
void wrap_clang_getPointeeType(CXType *pointerTyp, CXType *pointeeTyp) {
|
||||
*pointeeTyp = clang_getPointeeType(*pointerTyp);
|
||||
}
|
||||
|
||||
void wrap_clang_getNonReferenceType(CXType *typ, CXType *nonRefTyp) { *nonRefTyp = clang_getNonReferenceType(*typ); }
|
||||
|
||||
void wrap_clang_getTypeDeclaration(CXType *typ, CXCursor *cur) { *cur = clang_getTypeDeclaration(*typ); }
|
||||
|
||||
void wrap_clang_getResultType(CXType *typ, CXType *resultTyp) { *resultTyp = clang_getResultType(*typ); }
|
||||
|
||||
int wrap_clang_getNumArgTypes(CXType *typ) { return clang_getNumArgTypes(*typ); }
|
||||
|
||||
void wrap_clang_getArgType(CXType *typ, unsigned i, CXType *argTyp) { *argTyp = clang_getArgType(*typ, i); }
|
||||
|
||||
unsigned wrap_clang_isFunctionTypeVariadic(CXType *typ) { return clang_isFunctionTypeVariadic(*typ); }
|
||||
|
||||
void wrap_clang_getCursorResultType(CXCursor *cur, CXType *typ) { *typ = clang_getCursorResultType(*cur); }
|
||||
|
||||
void wrap_clang_getElementType(CXType *Typ, CXType *elemTyp) { *elemTyp = clang_getElementType(*Typ); }
|
||||
|
||||
void wrap_clang_getArrayElementType(CXType *arrayTyp, CXType *elemTyp) {
|
||||
*elemTyp = clang_getArrayElementType(*arrayTyp);
|
||||
}
|
||||
|
||||
long long wrap_clang_getArraySize(CXType *arrayTyp) { return clang_getArraySize(*arrayTyp); }
|
||||
|
||||
void wrap_clang_Type_getNamedType(CXType *typ, CXType *namedTyp) { *namedTyp = clang_Type_getNamedType(*typ); }
|
||||
|
||||
unsigned wrap_clang_Cursor_isAnonymous(CXCursor *cursor) { return clang_Cursor_isAnonymous(*cursor); }
|
||||
|
||||
unsigned wrap_clang_Cursor_isAnonymousRecordDecl(CXCursor *cursor) {
|
||||
return clang_Cursor_isAnonymousRecordDecl(*cursor);
|
||||
}
|
||||
|
||||
enum CX_CXXAccessSpecifier wrap_clang_getCXXAccessSpecifier(CXCursor *cursor) {
|
||||
return clang_getCXXAccessSpecifier(*cursor);
|
||||
}
|
||||
|
||||
enum CX_StorageClass wrap_clang_Cursor_getStorageClass(CXCursor *cursor) {
|
||||
return clang_Cursor_getStorageClass(*cursor);
|
||||
}
|
||||
|
||||
CXString wrap_clang_getCursorSpelling(CXCursor *cur) { return clang_getCursorSpelling(*cur); }
|
||||
|
||||
unsigned wrap_clang_Cursor_isVariadic(CXCursor *cur) { return clang_Cursor_isVariadic(*cur); }
|
||||
|
||||
CXString wrap_clang_Cursor_getRawCommentText(CXCursor *cursor) { return clang_Cursor_getRawCommentText(*cursor); }
|
||||
|
||||
CXString wrap_clang_Cursor_getMangling(CXCursor *cur) { return clang_Cursor_getMangling(*cur); }
|
||||
|
||||
unsigned wrap_clang_CXXConstructor_isConvertingConstructor(CXCursor *cursor) {
|
||||
return clang_CXXConstructor_isConvertingConstructor(*cursor);
|
||||
}
|
||||
|
||||
unsigned wrap_clang_CXXConstructor_isCopyConstructor(CXCursor *cursor) {
|
||||
return clang_CXXConstructor_isCopyConstructor(*cursor);
|
||||
}
|
||||
|
||||
unsigned wrap_clang_CXXConstructor_isDefaultConstructor(CXCursor *cursor) {
|
||||
return clang_CXXConstructor_isDefaultConstructor(*cursor);
|
||||
}
|
||||
|
||||
unsigned wrap_clang_CXXConstructor_isMoveConstructor(CXCursor *cursor) {
|
||||
return clang_CXXConstructor_isMoveConstructor(*cursor);
|
||||
}
|
||||
|
||||
unsigned wrap_clang_CXXField_isMutable(CXCursor *cursor) { return clang_CXXField_isMutable(*cursor); }
|
||||
|
||||
unsigned wrap_clang_CXXMethod_isDefaulted(CXCursor *cursor) { return clang_CXXMethod_isDefaulted(*cursor); }
|
||||
|
||||
unsigned wrap_clang_CXXMethod_isDeleted(CXCursor *cursor) { return clang_CXXMethod_isDeleted(*cursor); }
|
||||
|
||||
unsigned wrap_clang_CXXMethod_isPureVirtual(CXCursor *cursor) { return clang_CXXMethod_isPureVirtual(*cursor); }
|
||||
|
||||
unsigned wrap_clang_CXXMethod_isStatic(CXCursor *cursor) { return clang_CXXMethod_isStatic(*cursor); }
|
||||
|
||||
unsigned wrap_clang_CXXMethod_isVirtual(CXCursor *cursor) { return clang_CXXMethod_isVirtual(*cursor); }
|
||||
|
||||
unsigned wrap_clang_CXXMethod_isCopyAssignmentOperator(CXCursor *cursor) {
|
||||
return clang_CXXMethod_isCopyAssignmentOperator(*cursor);
|
||||
}
|
||||
|
||||
unsigned wrap_clang_CXXMethod_isMoveAssignmentOperator(CXCursor *cursor) {
|
||||
return clang_CXXMethod_isMoveAssignmentOperator(*cursor);
|
||||
}
|
||||
|
||||
unsigned wrap_clang_CXXMethod_isExplicit(CXCursor *cursor) { return clang_CXXMethod_isExplicit(*cursor); }
|
||||
|
||||
unsigned wrap_clang_CXXRecord_isAbstract(CXCursor *cursor) { return clang_CXXRecord_isAbstract(*cursor); }
|
||||
|
||||
unsigned wrap_clang_EnumDecl_isScoped(CXCursor *cursor) { return clang_EnumDecl_isScoped(*cursor); }
|
||||
|
||||
unsigned wrap_clang_CXXMethod_isConst(CXCursor *cursor) { return clang_CXXMethod_isConst(*cursor); }
|
||||
|
||||
CXTokenKind wrap_clang_getTokenKind(CXToken *token) { return clang_getTokenKind(*token); }
|
||||
|
||||
CXString wrap_clang_getTokenSpelling(CXTranslationUnit unit, CXToken *token) {
|
||||
return clang_getTokenSpelling(unit, *token);
|
||||
}
|
||||
|
||||
void wrap_clang_tokenize(CXTranslationUnit unit, CXSourceRange *Range, CXToken **Tokens, unsigned *NumTokens) {
|
||||
clang_tokenize(unit, *Range, Tokens, NumTokens);
|
||||
}
|
||||
|
||||
unsigned wrap_clang_visitChildren(CXCursor *parent, wrap_CXCursorVisitor visitor, CXClientData client_data) {
|
||||
wrap_data data = {client_data, visitor};
|
||||
return clang_visitChildren(*parent, wrap_visitor, CXClientData(&data));
|
||||
}
|
||||
|
||||
void wrap_clang_getSpellingLocation(CXSourceLocation *loc, CXFile *file, unsigned *line, unsigned *column,
|
||||
unsigned *offset) {
|
||||
clang_getSpellingLocation(*loc, file, line, column, offset);
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -1,7 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <clang-c/Index.h>
|
||||
|
||||
int main() {
|
||||
printf("sizeof(clang.Cursor) = %lu\n", sizeof(CXCursor));
|
||||
return 0;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/clang"
|
||||
)
|
||||
|
||||
const (
|
||||
LLGoCFlags = "-I$(llvm-config --includedir)"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c.Printf(c.Str("sizeof(clang.Cursor) = %lu\n"), unsafe.Sizeof(clang.Cursor{}))
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package clang
|
||||
|
||||
import (
|
||||
_ "unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
)
|
||||
|
||||
/**
|
||||
* A character string.
|
||||
*
|
||||
* The \c CXString type is used to return strings from the interface when
|
||||
* the ownership of that string might differ from one call to the next.
|
||||
* Use \c clang_getCString() to retrieve the string data and, once finished
|
||||
* with the string data, call \c clang_disposeString() to free the string.
|
||||
*/
|
||||
type String struct {
|
||||
Data c.Pointer
|
||||
PrivateFlags c.Uint
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the character data associated with the given string.
|
||||
*/
|
||||
// llgo:link String.CStr C.clang_getCString
|
||||
func (String) CStr() *c.Char { return nil }
|
||||
|
||||
/**
|
||||
* Free the given string.
|
||||
*/
|
||||
// llgo:link String.Dispose C.clang_disposeString
|
||||
func (String) Dispose() {}
|
||||
|
||||
type StringSet struct {
|
||||
Strings *String
|
||||
Count c.Uint
|
||||
}
|
||||
|
||||
/**
|
||||
* Free the given string set.
|
||||
*/
|
||||
// llgo:link (*StringSet).Dispose C.clang_disposeStringSet
|
||||
func (*StringSet) Dispose() {}
|
||||
2641
c/clang/clang.go
2641
c/clang/clang.go
File diff suppressed because it is too large
Load Diff
26
c/llama2/.gitignore
vendored
26
c/llama2/.gitignore
vendored
@@ -1,26 +0,0 @@
|
||||
# If you prefer the allow list template instead of the deny list, see community template:
|
||||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||
#
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
stories*.bin
|
||||
.DS_Store
|
||||
err.log
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
*.swp
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
# Go workspace file
|
||||
go.work*
|
||||
@@ -1,10 +0,0 @@
|
||||
LLGo wrapper of karpathy/llama2.c
|
||||
=====
|
||||
|
||||
## How to update source to llgo
|
||||
|
||||
```
|
||||
git submodule init
|
||||
git submodule update
|
||||
cp llama2.c/run.c llama2/run.c
|
||||
```
|
||||
Submodule c/llama2/llama2.c deleted from b3c4b6c3c4
@@ -1,160 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package llama2
|
||||
|
||||
import (
|
||||
_ "unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
)
|
||||
|
||||
const (
|
||||
LLGoFiles = "llama2/llama2.c"
|
||||
LLGoPackage = "link: -lm"
|
||||
)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// llgo:type C
|
||||
type TokenIndex struct {
|
||||
Str *c.Char
|
||||
Id c.Int
|
||||
}
|
||||
|
||||
// llgo:type C
|
||||
type Tokenizer struct {
|
||||
Vocab **c.Char
|
||||
VocabScores *c.Float
|
||||
SortedVocab *TokenIndex
|
||||
VocabSize c.Int
|
||||
MaxTokenLength c.Uint
|
||||
BytePieces [512]uint8 // stores all single-byte strings
|
||||
}
|
||||
|
||||
//go:linkname BuildTokenizer C.build_tokenizer
|
||||
func BuildTokenizer(t *Tokenizer, tokenizerPath *c.Char, vocabSize c.Int)
|
||||
|
||||
//go:linkname FreeTokenizer C.free_tokenizer
|
||||
func FreeTokenizer(t *Tokenizer)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// llgo:type C
|
||||
type Config struct {
|
||||
Dim c.Int // transformer dimension
|
||||
HiddenDim c.Int // for ffn layers
|
||||
NLayers c.Int // number of layers
|
||||
NHeads c.Int // number of query heads
|
||||
NKVHeads c.Int // number of key/value heads (can be < query heads because of multiquery)
|
||||
VocabSize c.Int // vocabulary size, usually 256 (byte-level)
|
||||
SeqLen c.Int // max sequence length
|
||||
}
|
||||
|
||||
// llgo:type C
|
||||
type TransformerWeights struct {
|
||||
// token embedding table
|
||||
TokenEmbeddingTable *c.Float // (vocab_size, dim)
|
||||
// weights for rmsnorms
|
||||
RmsAttWeight *c.Float // (layer, dim) rmsnorm weights
|
||||
RmsFfnWeight *c.Float // (layer, dim)
|
||||
// weights for matmuls. note dim == n_heads * head_size
|
||||
Wq *c.Float // (layer, dim, n_heads * head_size)
|
||||
Wk *c.Float // (layer, dim, n_kv_heads * head_size)
|
||||
Wv *c.Float // (layer, dim, n_kv_heads * head_size)
|
||||
Wo *c.Float // (layer, n_heads * head_size, dim)
|
||||
// weights for ffn
|
||||
W1 *c.Float // (layer, hidden_dim, dim)
|
||||
W2 *c.Float // (layer, dim, hidden_dim)
|
||||
W3 *c.Float // (layer, hidden_dim, dim)
|
||||
// final rmsnorm
|
||||
RmsFinalWeight *c.Float // (dim,)
|
||||
// (optional) classifier weights for the logits, on the last layer
|
||||
Wcls *c.Float
|
||||
}
|
||||
|
||||
// llgo:type C
|
||||
type RunState struct {
|
||||
// current wave of activations
|
||||
X *c.Float // activation at current time stamp (dim,)
|
||||
Xb *c.Float // same, but inside a residual branch (dim,)
|
||||
Xb2 *c.Float // an additional buffer just for convenience (dim,)
|
||||
Hb *c.Float // buffer for hidden dimension in the ffn (hidden_dim,)
|
||||
Hb2 *c.Float // buffer for hidden dimension in the ffn (hidden_dim,)
|
||||
Q *c.Float // query (dim,)
|
||||
K *c.Float // key (dim,)
|
||||
V *c.Float // value (dim,)
|
||||
Att *c.Float // buffer for scores/attention values (n_heads, seq_len)
|
||||
Logits *c.Float // output logits
|
||||
// kv cache
|
||||
KeyCache *c.Float // (layer, seq_len, dim)
|
||||
ValueCache *c.Float // (layer, seq_len, dim)
|
||||
}
|
||||
|
||||
// llgo:type C
|
||||
type Transformer struct {
|
||||
Config Config // the hyperparameters of the architecture (the blueprint)
|
||||
Weights TransformerWeights // the weights of the model
|
||||
State RunState // buffers for the "wave" of activations in the forward pass
|
||||
|
||||
// some more state needed to properly clean up the memory mapping (sigh)
|
||||
Fd c.Int // file descriptor for memory mapping
|
||||
Data *c.Float // memory mapped data pointer
|
||||
FileSize uintptr // size of the checkpoint file in bytes
|
||||
}
|
||||
|
||||
//go:linkname BuildTransformer C.build_transformer
|
||||
func BuildTransformer(t *Transformer, checkpointPath *c.Char)
|
||||
|
||||
//go:linkname FreeTransformer C.free_transformer
|
||||
func FreeTransformer(t *Transformer)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// llgo:type C
|
||||
type ProbIndex struct {
|
||||
Prob c.Float
|
||||
Index c.Int
|
||||
} // struct used when sorting probabilities during top-p sampling
|
||||
|
||||
// llgo:type C
|
||||
type Sampler struct {
|
||||
VocabSize c.Int
|
||||
Probindex *ProbIndex // buffer used in top-p sampling
|
||||
Temperature c.Float
|
||||
Topp c.Float
|
||||
RngState uint64
|
||||
}
|
||||
|
||||
//go:linkname BuildSampler C.build_sampler
|
||||
func BuildSampler(sampler *Sampler, vocabSize c.Int, temperature c.Float, topp c.Float, rngSeed uint64)
|
||||
|
||||
//go:linkname FreeSampler C.free_sampler
|
||||
func FreeSampler(sampler *Sampler)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
//go:linkname Generate C.generate
|
||||
func Generate(
|
||||
transformer *Transformer, tokenizer *Tokenizer, sampler *Sampler,
|
||||
prompt *c.Char, steps c.Int)
|
||||
|
||||
//go:linkname Chat C.chat
|
||||
func Chat(
|
||||
transformer *Transformer, tokenizer *Tokenizer, sampler *Sampler,
|
||||
cliUserPrompt *c.Char, cliSystemPrompt *c.Char, steps c.Int)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -1,2 +0,0 @@
|
||||
#define TESTING
|
||||
#include "./run.c"
|
||||
@@ -1,973 +0,0 @@
|
||||
/* Inference for Llama-2 Transformer model in pure C */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#if defined _WIN32
|
||||
#include "win.h"
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#endif
|
||||
// ----------------------------------------------------------------------------
|
||||
// Transformer model
|
||||
|
||||
typedef struct {
|
||||
int dim; // transformer dimension
|
||||
int hidden_dim; // for ffn layers
|
||||
int n_layers; // number of layers
|
||||
int n_heads; // number of query heads
|
||||
int n_kv_heads; // number of key/value heads (can be < query heads because of multiquery)
|
||||
int vocab_size; // vocabulary size, usually 256 (byte-level)
|
||||
int seq_len; // max sequence length
|
||||
} Config;
|
||||
|
||||
typedef struct {
|
||||
// token embedding table
|
||||
float* token_embedding_table; // (vocab_size, dim)
|
||||
// weights for rmsnorms
|
||||
float* rms_att_weight; // (layer, dim) rmsnorm weights
|
||||
float* rms_ffn_weight; // (layer, dim)
|
||||
// weights for matmuls. note dim == n_heads * head_size
|
||||
float* wq; // (layer, dim, n_heads * head_size)
|
||||
float* wk; // (layer, dim, n_kv_heads * head_size)
|
||||
float* wv; // (layer, dim, n_kv_heads * head_size)
|
||||
float* wo; // (layer, n_heads * head_size, dim)
|
||||
// weights for ffn
|
||||
float* w1; // (layer, hidden_dim, dim)
|
||||
float* w2; // (layer, dim, hidden_dim)
|
||||
float* w3; // (layer, hidden_dim, dim)
|
||||
// final rmsnorm
|
||||
float* rms_final_weight; // (dim,)
|
||||
// (optional) classifier weights for the logits, on the last layer
|
||||
float* wcls;
|
||||
} TransformerWeights;
|
||||
|
||||
typedef struct {
|
||||
// current wave of activations
|
||||
float *x; // activation at current time stamp (dim,)
|
||||
float *xb; // same, but inside a residual branch (dim,)
|
||||
float *xb2; // an additional buffer just for convenience (dim,)
|
||||
float *hb; // buffer for hidden dimension in the ffn (hidden_dim,)
|
||||
float *hb2; // buffer for hidden dimension in the ffn (hidden_dim,)
|
||||
float *q; // query (dim,)
|
||||
float *k; // key (dim,)
|
||||
float *v; // value (dim,)
|
||||
float *att; // buffer for scores/attention values (n_heads, seq_len)
|
||||
float *logits; // output logits
|
||||
// kv cache
|
||||
float* key_cache; // (layer, seq_len, dim)
|
||||
float* value_cache; // (layer, seq_len, dim)
|
||||
} RunState;
|
||||
|
||||
typedef struct {
|
||||
Config config; // the hyperparameters of the architecture (the blueprint)
|
||||
TransformerWeights weights; // the weights of the model
|
||||
RunState state; // buffers for the "wave" of activations in the forward pass
|
||||
// some more state needed to properly clean up the memory mapping (sigh)
|
||||
int fd; // file descriptor for memory mapping
|
||||
float* data; // memory mapped data pointer
|
||||
ssize_t file_size; // size of the checkpoint file in bytes
|
||||
} Transformer;
|
||||
|
||||
void malloc_run_state(RunState* s, Config* p) {
|
||||
// we calloc instead of malloc to keep valgrind happy
|
||||
int kv_dim = (p->dim * p->n_kv_heads) / p->n_heads;
|
||||
s->x = calloc(p->dim, sizeof(float));
|
||||
s->xb = calloc(p->dim, sizeof(float));
|
||||
s->xb2 = calloc(p->dim, sizeof(float));
|
||||
s->hb = calloc(p->hidden_dim, sizeof(float));
|
||||
s->hb2 = calloc(p->hidden_dim, sizeof(float));
|
||||
s->q = calloc(p->dim, sizeof(float));
|
||||
s->key_cache = calloc(p->n_layers * p->seq_len * kv_dim, sizeof(float));
|
||||
s->value_cache = calloc(p->n_layers * p->seq_len * kv_dim, sizeof(float));
|
||||
s->att = calloc(p->n_heads * p->seq_len, sizeof(float));
|
||||
s->logits = calloc(p->vocab_size, sizeof(float));
|
||||
// ensure all mallocs went fine
|
||||
if (!s->x || !s->xb || !s->xb2 || !s->hb || !s->hb2 || !s->q
|
||||
|| !s->key_cache || !s->value_cache || !s->att || !s->logits) {
|
||||
fprintf(stderr, "malloc failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void free_run_state(RunState* s) {
|
||||
free(s->x);
|
||||
free(s->xb);
|
||||
free(s->xb2);
|
||||
free(s->hb);
|
||||
free(s->hb2);
|
||||
free(s->q);
|
||||
free(s->att);
|
||||
free(s->logits);
|
||||
free(s->key_cache);
|
||||
free(s->value_cache);
|
||||
}
|
||||
|
||||
void memory_map_weights(TransformerWeights *w, Config* p, float* ptr, int shared_weights) {
|
||||
int head_size = p->dim / p->n_heads;
|
||||
// make sure the multiplications below are done in 64bit to fit the parameter counts of 13B+ models
|
||||
unsigned long long n_layers = p->n_layers;
|
||||
w->token_embedding_table = ptr;
|
||||
ptr += p->vocab_size * p->dim;
|
||||
w->rms_att_weight = ptr;
|
||||
ptr += n_layers * p->dim;
|
||||
w->wq = ptr;
|
||||
ptr += n_layers * p->dim * (p->n_heads * head_size);
|
||||
w->wk = ptr;
|
||||
ptr += n_layers * p->dim * (p->n_kv_heads * head_size);
|
||||
w->wv = ptr;
|
||||
ptr += n_layers * p->dim * (p->n_kv_heads * head_size);
|
||||
w->wo = ptr;
|
||||
ptr += n_layers * (p->n_heads * head_size) * p->dim;
|
||||
w->rms_ffn_weight = ptr;
|
||||
ptr += n_layers * p->dim;
|
||||
w->w1 = ptr;
|
||||
ptr += n_layers * p->dim * p->hidden_dim;
|
||||
w->w2 = ptr;
|
||||
ptr += n_layers * p->hidden_dim * p->dim;
|
||||
w->w3 = ptr;
|
||||
ptr += n_layers * p->dim * p->hidden_dim;
|
||||
w->rms_final_weight = ptr;
|
||||
ptr += p->dim;
|
||||
ptr += p->seq_len * head_size / 2; // skip what used to be freq_cis_real (for RoPE)
|
||||
ptr += p->seq_len * head_size / 2; // skip what used to be freq_cis_imag (for RoPE)
|
||||
w->wcls = shared_weights ? w->token_embedding_table : ptr;
|
||||
}
|
||||
|
||||
void read_checkpoint(char* checkpoint, Config* config, TransformerWeights* weights,
|
||||
int* fd, float** data, ssize_t* file_size) {
|
||||
FILE *file = fopen(checkpoint, "rb");
|
||||
if (!file) { fprintf(stderr, "Couldn't open file %s\n", checkpoint); exit(EXIT_FAILURE); }
|
||||
// read in the config header
|
||||
if (fread(config, sizeof(Config), 1, file) != 1) { exit(EXIT_FAILURE); }
|
||||
// negative vocab size is hacky way of signaling unshared weights. bit yikes.
|
||||
int shared_weights = config->vocab_size > 0 ? 1 : 0;
|
||||
config->vocab_size = abs(config->vocab_size);
|
||||
// figure out the file size
|
||||
fseek(file, 0, SEEK_END); // move file pointer to end of file
|
||||
*file_size = ftell(file); // get the file size, in bytes
|
||||
fclose(file);
|
||||
// memory map the Transformer weights into the data pointer
|
||||
*fd = open(checkpoint, O_RDONLY); // open in read only mode
|
||||
if (*fd == -1) { fprintf(stderr, "open failed!\n"); exit(EXIT_FAILURE); }
|
||||
*data = mmap(NULL, *file_size, PROT_READ, MAP_PRIVATE, *fd, 0);
|
||||
if (*data == MAP_FAILED) { fprintf(stderr, "mmap failed!\n"); exit(EXIT_FAILURE); }
|
||||
float* weights_ptr = *data + sizeof(Config)/sizeof(float);
|
||||
memory_map_weights(weights, config, weights_ptr, shared_weights);
|
||||
}
|
||||
|
||||
void build_transformer(Transformer *t, char* checkpoint_path) {
|
||||
// read in the Config and the Weights from the checkpoint
|
||||
read_checkpoint(checkpoint_path, &t->config, &t->weights, &t->fd, &t->data, &t->file_size);
|
||||
// allocate the RunState buffers
|
||||
malloc_run_state(&t->state, &t->config);
|
||||
}
|
||||
|
||||
void free_transformer(Transformer* t) {
|
||||
// close the memory mapping
|
||||
if (t->data != MAP_FAILED) { munmap(t->data, t->file_size); }
|
||||
if (t->fd != -1) { close(t->fd); }
|
||||
// free the RunState buffers
|
||||
free_run_state(&t->state);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// neural net blocks; the dynamics of the Transformer
|
||||
|
||||
void rmsnorm(float* o, float* x, float* weight, int size) {
|
||||
// calculate sum of squares
|
||||
float ss = 0.0f;
|
||||
for (int j = 0; j < size; j++) {
|
||||
ss += x[j] * x[j];
|
||||
}
|
||||
ss /= size;
|
||||
ss += 1e-5f;
|
||||
ss = 1.0f / sqrtf(ss);
|
||||
// normalize and scale
|
||||
for (int j = 0; j < size; j++) {
|
||||
o[j] = weight[j] * (ss * x[j]);
|
||||
}
|
||||
}
|
||||
|
||||
void softmax(float* x, int size) {
|
||||
// find max value (for numerical stability)
|
||||
float max_val = x[0];
|
||||
for (int i = 1; i < size; i++) {
|
||||
if (x[i] > max_val) {
|
||||
max_val = x[i];
|
||||
}
|
||||
}
|
||||
// exp and sum
|
||||
float sum = 0.0f;
|
||||
for (int i = 0; i < size; i++) {
|
||||
x[i] = expf(x[i] - max_val);
|
||||
sum += x[i];
|
||||
}
|
||||
// normalize
|
||||
for (int i = 0; i < size; i++) {
|
||||
x[i] /= sum;
|
||||
}
|
||||
}
|
||||
|
||||
void matmul(float* xout, float* x, float* w, int n, int d) {
|
||||
// W (d,n) @ x (n,) -> xout (d,)
|
||||
// by far the most amount of time is spent inside this little function
|
||||
int i;
|
||||
#pragma omp parallel for private(i)
|
||||
for (i = 0; i < d; i++) {
|
||||
float val = 0.0f;
|
||||
for (int j = 0; j < n; j++) {
|
||||
val += w[i * n + j] * x[j];
|
||||
}
|
||||
xout[i] = val;
|
||||
}
|
||||
}
|
||||
|
||||
float* forward(Transformer* transformer, int token, int pos) {
|
||||
|
||||
// a few convenience variables
|
||||
Config* p = &transformer->config;
|
||||
TransformerWeights* w = &transformer->weights;
|
||||
RunState* s = &transformer->state;
|
||||
float *x = s->x;
|
||||
int dim = p->dim;
|
||||
int kv_dim = (p->dim * p->n_kv_heads) / p->n_heads;
|
||||
int kv_mul = p->n_heads / p->n_kv_heads; // integer multiplier of the kv sharing in multiquery
|
||||
int hidden_dim = p->hidden_dim;
|
||||
int head_size = dim / p->n_heads;
|
||||
|
||||
// copy the token embedding into x
|
||||
float* content_row = w->token_embedding_table + token * dim;
|
||||
memcpy(x, content_row, dim*sizeof(*x));
|
||||
|
||||
// forward all the layers
|
||||
for(unsigned long long l = 0; l < p->n_layers; l++) {
|
||||
|
||||
// attention rmsnorm
|
||||
rmsnorm(s->xb, x, w->rms_att_weight + l*dim, dim);
|
||||
|
||||
// key and value point to the kv cache
|
||||
int loff = l * p->seq_len * kv_dim; // kv cache layer offset for convenience
|
||||
s->k = s->key_cache + loff + pos * kv_dim;
|
||||
s->v = s->value_cache + loff + pos * kv_dim;
|
||||
|
||||
// qkv matmuls for this position
|
||||
matmul(s->q, s->xb, w->wq + l*dim*dim, dim, dim);
|
||||
matmul(s->k, s->xb, w->wk + l*dim*kv_dim, dim, kv_dim);
|
||||
matmul(s->v, s->xb, w->wv + l*dim*kv_dim, dim, kv_dim);
|
||||
|
||||
// RoPE relative positional encoding: complex-valued rotate q and k in each head
|
||||
for (int i = 0; i < dim; i+=2) {
|
||||
int head_dim = i % head_size;
|
||||
float freq = 1.0f / powf(10000.0f, head_dim / (float)head_size);
|
||||
float val = pos * freq;
|
||||
float fcr = cosf(val);
|
||||
float fci = sinf(val);
|
||||
int rotn = i < kv_dim ? 2 : 1; // how many vectors? 2 = q & k, 1 = q only
|
||||
for (int v = 0; v < rotn; v++) {
|
||||
float* vec = v == 0 ? s->q : s->k; // the vector to rotate (query or key)
|
||||
float v0 = vec[i];
|
||||
float v1 = vec[i+1];
|
||||
vec[i] = v0 * fcr - v1 * fci;
|
||||
vec[i+1] = v0 * fci + v1 * fcr;
|
||||
}
|
||||
}
|
||||
|
||||
// multihead attention. iterate over all heads
|
||||
int h;
|
||||
#pragma omp parallel for private(h)
|
||||
for (h = 0; h < p->n_heads; h++) {
|
||||
// get the query vector for this head
|
||||
float* q = s->q + h * head_size;
|
||||
// attention scores for this head
|
||||
float* att = s->att + h * p->seq_len;
|
||||
// iterate over all timesteps, including the current one
|
||||
for (int t = 0; t <= pos; t++) {
|
||||
// get the key vector for this head and at this timestep
|
||||
float* k = s->key_cache + loff + t * kv_dim + (h / kv_mul) * head_size;
|
||||
// calculate the attention score as the dot product of q and k
|
||||
float score = 0.0f;
|
||||
for (int i = 0; i < head_size; i++) {
|
||||
score += q[i] * k[i];
|
||||
}
|
||||
score /= sqrtf(head_size);
|
||||
// save the score to the attention buffer
|
||||
att[t] = score;
|
||||
}
|
||||
|
||||
// softmax the scores to get attention weights, from 0..pos inclusively
|
||||
softmax(att, pos + 1);
|
||||
|
||||
// weighted sum of the values, store back into xb
|
||||
float* xb = s->xb + h * head_size;
|
||||
memset(xb, 0, head_size * sizeof(float));
|
||||
for (int t = 0; t <= pos; t++) {
|
||||
// get the value vector for this head and at this timestep
|
||||
float* v = s->value_cache + loff + t * kv_dim + (h / kv_mul) * head_size;
|
||||
// get the attention weight for this timestep
|
||||
float a = att[t];
|
||||
// accumulate the weighted value into xb
|
||||
for (int i = 0; i < head_size; i++) {
|
||||
xb[i] += a * v[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// final matmul to get the output of the attention
|
||||
matmul(s->xb2, s->xb, w->wo + l*dim*dim, dim, dim);
|
||||
|
||||
// residual connection back into x
|
||||
for (int i = 0; i < dim; i++) {
|
||||
x[i] += s->xb2[i];
|
||||
}
|
||||
|
||||
// ffn rmsnorm
|
||||
rmsnorm(s->xb, x, w->rms_ffn_weight + l*dim, dim);
|
||||
|
||||
// Now for FFN in PyTorch we have: self.w2(F.silu(self.w1(x)) * self.w3(x))
|
||||
// first calculate self.w1(x) and self.w3(x)
|
||||
matmul(s->hb, s->xb, w->w1 + l*dim*hidden_dim, dim, hidden_dim);
|
||||
matmul(s->hb2, s->xb, w->w3 + l*dim*hidden_dim, dim, hidden_dim);
|
||||
|
||||
// SwiGLU non-linearity
|
||||
for (int i = 0; i < hidden_dim; i++) {
|
||||
float val = s->hb[i];
|
||||
// silu(x)=x*σ(x), where σ(x) is the logistic sigmoid
|
||||
val *= (1.0f / (1.0f + expf(-val)));
|
||||
// elementwise multiply with w3(x)
|
||||
val *= s->hb2[i];
|
||||
s->hb[i] = val;
|
||||
}
|
||||
|
||||
// final matmul to get the output of the ffn
|
||||
matmul(s->xb, s->hb, w->w2 + l*dim*hidden_dim, hidden_dim, dim);
|
||||
|
||||
// residual connection
|
||||
for (int i = 0; i < dim; i++) {
|
||||
x[i] += s->xb[i];
|
||||
}
|
||||
}
|
||||
|
||||
// final rmsnorm
|
||||
rmsnorm(x, x, w->rms_final_weight, dim);
|
||||
|
||||
// classifier into logits
|
||||
matmul(s->logits, x, w->wcls, p->dim, p->vocab_size);
|
||||
return s->logits;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// The Byte Pair Encoding (BPE) Tokenizer that translates strings <-> tokens
|
||||
|
||||
typedef struct {
|
||||
char *str;
|
||||
int id;
|
||||
} TokenIndex;
|
||||
|
||||
typedef struct {
|
||||
char** vocab;
|
||||
float* vocab_scores;
|
||||
TokenIndex *sorted_vocab;
|
||||
int vocab_size;
|
||||
unsigned int max_token_length;
|
||||
unsigned char byte_pieces[512]; // stores all single-byte strings
|
||||
} Tokenizer;
|
||||
|
||||
int compare_tokens(const void *a, const void *b) {
|
||||
return strcmp(((TokenIndex*)a)->str, ((TokenIndex*)b)->str);
|
||||
}
|
||||
|
||||
void build_tokenizer(Tokenizer* t, char* tokenizer_path, int vocab_size) {
|
||||
// i should have written the vocab_size into the tokenizer file... sigh
|
||||
t->vocab_size = vocab_size;
|
||||
// malloc space to hold the scores and the strings
|
||||
t->vocab = (char**)malloc(vocab_size * sizeof(char*));
|
||||
t->vocab_scores = (float*)malloc(vocab_size * sizeof(float));
|
||||
t->sorted_vocab = NULL; // initialized lazily
|
||||
for (int i = 0; i < 256; i++) {
|
||||
t->byte_pieces[i * 2] = (unsigned char)i;
|
||||
t->byte_pieces[i * 2 + 1] = '\0';
|
||||
}
|
||||
// read in the file
|
||||
FILE *file = fopen(tokenizer_path, "rb");
|
||||
if (!file) { fprintf(stderr, "couldn't load %s\n", tokenizer_path); exit(EXIT_FAILURE); }
|
||||
if (fread(&t->max_token_length, sizeof(int), 1, file) != 1) { fprintf(stderr, "failed read\n"); exit(EXIT_FAILURE); }
|
||||
int len;
|
||||
for (int i = 0; i < vocab_size; i++) {
|
||||
if (fread(t->vocab_scores + i, sizeof(float), 1, file) != 1) { fprintf(stderr, "failed read\n"); exit(EXIT_FAILURE);}
|
||||
if (fread(&len, sizeof(int), 1, file) != 1) { fprintf(stderr, "failed read\n"); exit(EXIT_FAILURE); }
|
||||
t->vocab[i] = (char *)malloc(len + 1);
|
||||
if (fread(t->vocab[i], len, 1, file) != 1) { fprintf(stderr, "failed read\n"); exit(EXIT_FAILURE); }
|
||||
t->vocab[i][len] = '\0'; // add the string terminating token
|
||||
}
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void free_tokenizer(Tokenizer* t) {
|
||||
for (int i = 0; i < t->vocab_size; i++) { free(t->vocab[i]); }
|
||||
free(t->vocab);
|
||||
free(t->vocab_scores);
|
||||
free(t->sorted_vocab);
|
||||
}
|
||||
|
||||
char* decode(Tokenizer* t, int prev_token, int token) {
|
||||
char *piece = t->vocab[token];
|
||||
// following BOS (1) token, sentencepiece decoder strips any leading whitespace (see PR #89)
|
||||
if (prev_token == 1 && piece[0] == ' ') { piece++; }
|
||||
// careful, some tokens designate raw bytes, and look like e.g. '<0x01>'
|
||||
// parse this and convert and return the actual byte
|
||||
unsigned char byte_val;
|
||||
if (sscanf(piece, "<0x%02hhX>", &byte_val) == 1) {
|
||||
piece = (char*)t->byte_pieces + byte_val * 2;
|
||||
}
|
||||
return piece;
|
||||
}
|
||||
|
||||
void safe_printf(char *piece) {
|
||||
// piece might be a raw byte token, and we only want to print printable chars or whitespace
|
||||
// because some of the other bytes can be various control codes, backspace, etc.
|
||||
if (piece == NULL) { return; }
|
||||
if (piece[0] == '\0') { return; }
|
||||
if (piece[1] == '\0') {
|
||||
unsigned char byte_val = piece[0];
|
||||
if (!(isprint(byte_val) || isspace(byte_val))) {
|
||||
return; // bad byte, don't print it
|
||||
}
|
||||
}
|
||||
printf("%s", piece);
|
||||
}
|
||||
|
||||
int str_lookup(char *str, TokenIndex *sorted_vocab, int vocab_size) {
|
||||
// efficiently find the perfect match for str in vocab, return its index or -1 if not found
|
||||
TokenIndex tok = { .str = str }; // acts as the key to search for
|
||||
TokenIndex *res = bsearch(&tok, sorted_vocab, vocab_size, sizeof(TokenIndex), compare_tokens);
|
||||
return res != NULL ? res->id : -1;
|
||||
}
|
||||
|
||||
void encode(Tokenizer* t, char *text, int8_t bos, int8_t eos, int *tokens, int *n_tokens) {
|
||||
// encode the string text (input) into an upper-bound preallocated tokens[] array
|
||||
// bos != 0 means prepend the BOS token (=1), eos != 0 means append the EOS token (=2)
|
||||
if (text == NULL) { fprintf(stderr, "cannot encode NULL text\n"); exit(EXIT_FAILURE); }
|
||||
|
||||
if (t->sorted_vocab == NULL) {
|
||||
// lazily malloc and sort the vocabulary
|
||||
t->sorted_vocab = malloc(t->vocab_size * sizeof(TokenIndex));
|
||||
for (int i = 0; i < t->vocab_size; i++) {
|
||||
t->sorted_vocab[i].str = t->vocab[i];
|
||||
t->sorted_vocab[i].id = i;
|
||||
}
|
||||
qsort(t->sorted_vocab, t->vocab_size, sizeof(TokenIndex), compare_tokens);
|
||||
}
|
||||
|
||||
// create a temporary buffer that will store merge candidates of always two consecutive tokens
|
||||
// *2 for concat, +1 for null terminator +2 for UTF8 (in case max_token_length is 1)
|
||||
char* str_buffer = malloc((t->max_token_length*2 +1 +2) * sizeof(char));
|
||||
size_t str_len = 0;
|
||||
|
||||
// start at 0 tokens
|
||||
*n_tokens = 0;
|
||||
|
||||
// add optional BOS (=1) token, if desired
|
||||
if (bos) tokens[(*n_tokens)++] = 1;
|
||||
|
||||
// add_dummy_prefix is true by default
|
||||
// so prepend a dummy prefix token to the input string, but only if text != ""
|
||||
// TODO: pretty sure this isn't correct in the general case but I don't have the
|
||||
// energy to read more of the sentencepiece code to figure out what it's doing
|
||||
if (text[0] != '\0') {
|
||||
int dummy_prefix = str_lookup(" ", t->sorted_vocab, t->vocab_size);
|
||||
tokens[(*n_tokens)++] = dummy_prefix;
|
||||
}
|
||||
|
||||
// Okay UTF-8 time. This will get messy. Here is the reference from Wikipedia:
|
||||
// Code point ↔ UTF-8 conversion
|
||||
// First code point Last code point Byte 1 Byte 2 Byte 3 Byte 4
|
||||
// U+0000 U+007F 0xxxxxxx
|
||||
// U+0080 U+07FF 110xxxxx 10xxxxxx
|
||||
// U+0800 U+FFFF 1110xxxx 10xxxxxx 10xxxxxx
|
||||
// U+10000 U+10FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||
|
||||
// process the raw (UTF-8) byte sequence of the input string
|
||||
for (char *c = text; *c != '\0'; c++) {
|
||||
|
||||
// reset buffer if the current byte is ASCII or a leading byte
|
||||
// 0xC0 is 11000000, so (*c & 0xC0) keeps the first 2 bits and zeros the rest
|
||||
// 0x80 is 10000000
|
||||
// in UTF-8, all continuation bytes start with "10" in first two bits
|
||||
// so in English this is: "if this byte is not a continuation byte"
|
||||
if ((*c & 0xC0) != 0x80) {
|
||||
// this byte must be either a leading byte (11...) or an ASCII char (0x...)
|
||||
// => reset our location, as we're starting a new UTF-8 codepoint
|
||||
str_len = 0;
|
||||
}
|
||||
|
||||
// append the current byte to the buffer
|
||||
str_buffer[str_len++] = *c; // ++ is post-increment, incremented after this line
|
||||
str_buffer[str_len] = '\0';
|
||||
|
||||
// while the next character is a continuation byte, continue appending
|
||||
// but if there are too many of them, just stop to avoid overruning str_buffer size.
|
||||
if ((*(c+1) & 0xC0) == 0x80 && str_len < 4) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// ok c+1 is not a continuation byte, so we've read in a full codepoint
|
||||
int id = str_lookup(str_buffer, t->sorted_vocab, t->vocab_size);
|
||||
|
||||
if (id != -1) {
|
||||
// we found this codepoint in vocab, add it as a token
|
||||
tokens[(*n_tokens)++] = id;
|
||||
} else {
|
||||
// byte_fallback encoding: just encode each byte as a token
|
||||
// +3 is here because the first 3 vocab elements are <unk>, <s>, </s>
|
||||
// so the individual bytes only start at index 3
|
||||
for (int i=0; i < str_len; i++) {
|
||||
tokens[(*n_tokens)++] = (unsigned char)str_buffer[i] + 3;
|
||||
}
|
||||
}
|
||||
str_len = 0; // protect against a sequence of stray UTF8 continuation bytes
|
||||
}
|
||||
|
||||
// merge the best consecutive pair each iteration, according the scores in vocab_scores
|
||||
while (1) {
|
||||
float best_score = -1e10;
|
||||
int best_id = -1;
|
||||
int best_idx = -1;
|
||||
|
||||
for (int i=0; i < (*n_tokens-1); i++) {
|
||||
// check if we can merge the pair (tokens[i], tokens[i+1])
|
||||
sprintf(str_buffer, "%s%s", t->vocab[tokens[i]], t->vocab[tokens[i+1]]);
|
||||
int id = str_lookup(str_buffer, t->sorted_vocab, t->vocab_size);
|
||||
if (id != -1 && t->vocab_scores[id] > best_score) {
|
||||
// this merge pair exists in vocab! record its score and position
|
||||
best_score = t->vocab_scores[id];
|
||||
best_id = id;
|
||||
best_idx = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (best_idx == -1) {
|
||||
break; // we couldn't find any more pairs to merge, so we're done
|
||||
}
|
||||
|
||||
// merge the consecutive pair (best_idx, best_idx+1) into new token best_id
|
||||
tokens[best_idx] = best_id;
|
||||
// delete token at position best_idx+1, shift the entire sequence back 1
|
||||
for (int i = best_idx+1; i < (*n_tokens-1); i++) {
|
||||
tokens[i] = tokens[i+1];
|
||||
}
|
||||
(*n_tokens)--; // token length decreased
|
||||
}
|
||||
|
||||
// add optional EOS (=2) token, if desired
|
||||
if (eos) tokens[(*n_tokens)++] = 2;
|
||||
|
||||
free(str_buffer);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// The Sampler, which takes logits and returns a sampled token
|
||||
// sampling can be done in a few ways: greedy argmax, sampling, top-p sampling
|
||||
|
||||
typedef struct {
|
||||
float prob;
|
||||
int index;
|
||||
} ProbIndex; // struct used when sorting probabilities during top-p sampling
|
||||
|
||||
typedef struct {
|
||||
int vocab_size;
|
||||
ProbIndex* probindex; // buffer used in top-p sampling
|
||||
float temperature;
|
||||
float topp;
|
||||
unsigned long long rng_state;
|
||||
} Sampler;
|
||||
|
||||
int sample_argmax(float* probabilities, int n) {
|
||||
// return the index that has the highest probability
|
||||
int max_i = 0;
|
||||
float max_p = probabilities[0];
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (probabilities[i] > max_p) {
|
||||
max_i = i;
|
||||
max_p = probabilities[i];
|
||||
}
|
||||
}
|
||||
return max_i;
|
||||
}
|
||||
|
||||
int sample_mult(float* probabilities, int n, float coin) {
|
||||
// sample index from probabilities (they must sum to 1!)
|
||||
// coin is a random number in [0, 1), usually from random_f32()
|
||||
float cdf = 0.0f;
|
||||
for (int i = 0; i < n; i++) {
|
||||
cdf += probabilities[i];
|
||||
if (coin < cdf) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n - 1; // in case of rounding errors
|
||||
}
|
||||
|
||||
int compare(const void* a, const void* b) {
|
||||
ProbIndex* a_ = (ProbIndex*) a;
|
||||
ProbIndex* b_ = (ProbIndex*) b;
|
||||
if (a_->prob > b_->prob) return -1;
|
||||
if (a_->prob < b_->prob) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sample_topp(float* probabilities, int n, float topp, ProbIndex* probindex, float coin) {
|
||||
// top-p sampling (or "nucleus sampling") samples from the smallest set of
|
||||
// tokens that exceed probability topp. This way we never sample tokens that
|
||||
// have very low probabilities and are less likely to go "off the rails".
|
||||
// coin is a random number in [0, 1), usually from random_f32()
|
||||
|
||||
int n0 = 0;
|
||||
// quicksort indices in descending order of probabilities
|
||||
// values smaller than (1 - topp) / (n - 1) cannot be part of the result
|
||||
// so for efficiency we crop these out as candidates before sorting
|
||||
const float cutoff = (1.0f - topp) / (n - 1);
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (probabilities[i] >= cutoff) {
|
||||
probindex[n0].index = i;
|
||||
probindex[n0].prob = probabilities[i];
|
||||
n0++;
|
||||
}
|
||||
}
|
||||
qsort(probindex, n0, sizeof(ProbIndex), compare);
|
||||
|
||||
// truncate the list where cumulative probability exceeds topp
|
||||
float cumulative_prob = 0.0f;
|
||||
int last_idx = n0 - 1; // in case of rounding errors consider all elements
|
||||
for (int i = 0; i < n0; i++) {
|
||||
cumulative_prob += probindex[i].prob;
|
||||
if (cumulative_prob > topp) {
|
||||
last_idx = i;
|
||||
break; // we've exceeded topp by including last_idx
|
||||
}
|
||||
}
|
||||
|
||||
// sample from the truncated list
|
||||
float r = coin * cumulative_prob;
|
||||
float cdf = 0.0f;
|
||||
for (int i = 0; i <= last_idx; i++) {
|
||||
cdf += probindex[i].prob;
|
||||
if (r < cdf) {
|
||||
return probindex[i].index;
|
||||
}
|
||||
}
|
||||
return probindex[last_idx].index; // in case of rounding errors
|
||||
}
|
||||
|
||||
void build_sampler(Sampler* sampler, int vocab_size, float temperature, float topp, unsigned long long rng_seed) {
|
||||
sampler->vocab_size = vocab_size;
|
||||
sampler->temperature = temperature;
|
||||
sampler->topp = topp;
|
||||
sampler->rng_state = rng_seed;
|
||||
// buffer only used with nucleus sampling; may not need but it's ~small
|
||||
sampler->probindex = malloc(sampler->vocab_size * sizeof(ProbIndex));
|
||||
}
|
||||
|
||||
void free_sampler(Sampler* sampler) {
|
||||
free(sampler->probindex);
|
||||
}
|
||||
|
||||
unsigned int random_u32(unsigned long long *state) {
|
||||
// xorshift rng: https://en.wikipedia.org/wiki/Xorshift#xorshift.2A
|
||||
*state ^= *state >> 12;
|
||||
*state ^= *state << 25;
|
||||
*state ^= *state >> 27;
|
||||
return (*state * 0x2545F4914F6CDD1Dull) >> 32;
|
||||
}
|
||||
float random_f32(unsigned long long *state) { // random float32 in [0,1)
|
||||
return (random_u32(state) >> 8) / 16777216.0f;
|
||||
}
|
||||
|
||||
int sample(Sampler* sampler, float* logits) {
|
||||
// sample the token given the logits and some hyperparameters
|
||||
int next;
|
||||
if (sampler->temperature == 0.0f) {
|
||||
// greedy argmax sampling: take the token with the highest probability
|
||||
next = sample_argmax(logits, sampler->vocab_size);
|
||||
} else {
|
||||
// apply the temperature to the logits
|
||||
for (int q=0; q<sampler->vocab_size; q++) { logits[q] /= sampler->temperature; }
|
||||
// apply softmax to the logits to get the probabilities for next token
|
||||
softmax(logits, sampler->vocab_size);
|
||||
// flip a (float) coin (this is our source of entropy for sampling)
|
||||
float coin = random_f32(&sampler->rng_state);
|
||||
// we sample from this distribution to get the next token
|
||||
if (sampler->topp <= 0 || sampler->topp >= 1) {
|
||||
// simply sample from the predicted probability distribution
|
||||
next = sample_mult(logits, sampler->vocab_size, coin);
|
||||
} else {
|
||||
// top-p (nucleus) sampling, clamping the least likely tokens to zero
|
||||
next = sample_topp(logits, sampler->vocab_size, sampler->topp, sampler->probindex, coin);
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// utilities: time
|
||||
|
||||
long time_in_ms() {
|
||||
// return time in milliseconds, for benchmarking the model speed
|
||||
struct timespec time;
|
||||
clock_gettime(CLOCK_REALTIME, &time);
|
||||
return time.tv_sec * 1000 + time.tv_nsec / 1000000;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// generation loop
|
||||
|
||||
void generate(Transformer *transformer, Tokenizer *tokenizer, Sampler *sampler, char *prompt, int steps) {
|
||||
char *empty_prompt = "";
|
||||
if (prompt == NULL) { prompt = empty_prompt; }
|
||||
|
||||
// encode the (string) prompt into tokens sequence
|
||||
int num_prompt_tokens = 0;
|
||||
int* prompt_tokens = (int*)malloc((strlen(prompt)+3) * sizeof(int)); // +3 for '\0', ?BOS, ?EOS
|
||||
encode(tokenizer, prompt, 1, 0, prompt_tokens, &num_prompt_tokens);
|
||||
if (num_prompt_tokens < 1) {
|
||||
fprintf(stderr, "something is wrong, expected at least 1 prompt token\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// start the main loop
|
||||
long start = 0; // used to time our code, only initialized after first iteration
|
||||
int next; // will store the next token in the sequence
|
||||
int token = prompt_tokens[0]; // kick off with the first token in the prompt
|
||||
int pos = 0; // position in the sequence
|
||||
while (pos < steps) {
|
||||
|
||||
// forward the transformer to get logits for the next token
|
||||
float* logits = forward(transformer, token, pos);
|
||||
|
||||
// advance the state machine
|
||||
if (pos < num_prompt_tokens - 1) {
|
||||
// if we are still processing the input prompt, force the next prompt token
|
||||
next = prompt_tokens[pos + 1];
|
||||
} else {
|
||||
// otherwise sample the next token from the logits
|
||||
next = sample(sampler, logits);
|
||||
}
|
||||
pos++;
|
||||
|
||||
// data-dependent terminating condition: the BOS (=1) token delimits sequences
|
||||
if (next == 1) { break; }
|
||||
|
||||
// print the token as string, decode it with the Tokenizer object
|
||||
char* piece = decode(tokenizer, token, next);
|
||||
safe_printf(piece); // same as printf("%s", piece), but skips "unsafe" bytes
|
||||
fflush(stdout);
|
||||
token = next;
|
||||
|
||||
// init the timer here because the first iteration can be slower
|
||||
if (start == 0) { start = time_in_ms(); }
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// report achieved tok/s (pos-1 because the timer starts after first iteration)
|
||||
if (pos > 1) {
|
||||
long end = time_in_ms();
|
||||
fprintf(stderr, "achieved tok/s: %f\n", (pos-1) / (double)(end-start)*1000);
|
||||
}
|
||||
|
||||
free(prompt_tokens);
|
||||
}
|
||||
|
||||
void read_stdin(const char* guide, char* buffer, size_t bufsize) {
|
||||
// read a line from stdin, up to but not including \n
|
||||
printf("%s", guide);
|
||||
if (fgets(buffer, bufsize, stdin) != NULL) {
|
||||
size_t len = strlen(buffer);
|
||||
if (len > 0 && buffer[len - 1] == '\n') {
|
||||
buffer[len - 1] = '\0'; // strip newline
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// chat loop
|
||||
// I manually inspected the tokens for a few chat conversations compared to
|
||||
// python reference and that seemed ok, but this was not thoroughly tested and
|
||||
// is not safely implemented, it's more a proof of concept atm.
|
||||
|
||||
void chat(Transformer *transformer, Tokenizer *tokenizer, Sampler *sampler,
|
||||
char *cli_user_prompt, char *cli_system_prompt, int steps) {
|
||||
|
||||
// buffers for reading the system prompt and user prompt from stdin
|
||||
// you'll notice they are soomewhat haphazardly and unsafely set atm
|
||||
char system_prompt[512];
|
||||
char user_prompt[512];
|
||||
char rendered_prompt[1152];
|
||||
int num_prompt_tokens = 0;
|
||||
int* prompt_tokens = (int*)malloc(1152 * sizeof(int));
|
||||
int user_idx;
|
||||
|
||||
// start the main loop
|
||||
int8_t user_turn = 1; // user starts
|
||||
int next; // will store the next token in the sequence
|
||||
int token; // stores the current token to feed into the transformer
|
||||
int prev_token;
|
||||
int pos = 0; // position in the sequence
|
||||
while (pos < steps) {
|
||||
|
||||
// when it is the user's turn to contribute tokens to the dialog...
|
||||
if (user_turn) {
|
||||
// get the (optional) system prompt at position 0
|
||||
if (pos == 0) {
|
||||
// at position 0, the user can also contribute a system prompt
|
||||
if (cli_system_prompt == NULL) {
|
||||
// system prompt was not passed in, attempt to get it from stdin
|
||||
read_stdin("Enter system prompt (optional): ", system_prompt, sizeof(system_prompt));
|
||||
} else {
|
||||
// system prompt was passed in, use it
|
||||
strcpy(system_prompt, cli_system_prompt);
|
||||
}
|
||||
}
|
||||
// get the user prompt
|
||||
if (pos == 0 && cli_user_prompt != NULL) {
|
||||
// user prompt for position 0 was passed in, use it
|
||||
strcpy(user_prompt, cli_user_prompt);
|
||||
} else {
|
||||
// otherwise get user prompt from stdin
|
||||
read_stdin("User: ", user_prompt, sizeof(user_prompt));
|
||||
}
|
||||
// render user/system prompts into the Llama 2 Chat schema
|
||||
if (pos == 0 && system_prompt[0] != '\0') {
|
||||
char system_template[] = "[INST] <<SYS>>\n%s\n<</SYS>>\n\n%s [/INST]";
|
||||
sprintf(rendered_prompt, system_template, system_prompt, user_prompt);
|
||||
} else {
|
||||
char user_template[] = "[INST] %s [/INST]";
|
||||
sprintf(rendered_prompt, user_template, user_prompt);
|
||||
}
|
||||
// encode the rendered prompt into tokens
|
||||
encode(tokenizer, rendered_prompt, 1, 0, prompt_tokens, &num_prompt_tokens);
|
||||
user_idx = 0; // reset the user index
|
||||
user_turn = 0;
|
||||
printf("Assistant: ");
|
||||
}
|
||||
|
||||
// determine the token to pass into the transformer next
|
||||
if (user_idx < num_prompt_tokens) {
|
||||
// if we are still processing the input prompt, force the next prompt token
|
||||
token = prompt_tokens[user_idx++];
|
||||
} else {
|
||||
// otherwise use the next token sampled from previous turn
|
||||
token = next;
|
||||
}
|
||||
// EOS (=2) token ends the Assistant turn
|
||||
if (token == 2) { user_turn = 1; }
|
||||
|
||||
// forward the transformer to get logits for the next token
|
||||
float* logits = forward(transformer, token, pos);
|
||||
next = sample(sampler, logits);
|
||||
pos++;
|
||||
|
||||
if (user_idx >= num_prompt_tokens && next != 2) {
|
||||
// the Assistant is responding, so print its output
|
||||
char* piece = decode(tokenizer, token, next);
|
||||
safe_printf(piece); // same as printf("%s", piece), but skips "unsafe" bytes
|
||||
fflush(stdout);
|
||||
}
|
||||
if (next == 2) { printf("\n"); }
|
||||
}
|
||||
printf("\n");
|
||||
free(prompt_tokens);
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// CLI, include only if not testing
|
||||
#ifndef TESTING
|
||||
|
||||
void error_usage() {
|
||||
fprintf(stderr, "Usage: run <checkpoint> [options]\n");
|
||||
fprintf(stderr, "Example: run model.bin -n 256 -i \"Once upon a time\"\n");
|
||||
fprintf(stderr, "Options:\n");
|
||||
fprintf(stderr, " -t <float> temperature in [0,inf], default 1.0\n");
|
||||
fprintf(stderr, " -p <float> p value in top-p (nucleus) sampling in [0,1] default 0.9\n");
|
||||
fprintf(stderr, " -s <int> random seed, default time(NULL)\n");
|
||||
fprintf(stderr, " -n <int> number of steps to run for, default 256. 0 = max_seq_len\n");
|
||||
fprintf(stderr, " -i <string> input prompt\n");
|
||||
fprintf(stderr, " -z <string> optional path to custom tokenizer\n");
|
||||
fprintf(stderr, " -m <string> mode: generate|chat, default: generate\n");
|
||||
fprintf(stderr, " -y <string> (optional) system prompt in chat mode\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
// default parameters
|
||||
char *checkpoint_path = NULL; // e.g. out/model.bin
|
||||
char *tokenizer_path = "tokenizer.bin";
|
||||
float temperature = 1.0f; // 0.0 = greedy deterministic. 1.0 = original. don't set higher
|
||||
float topp = 0.9f; // top-p in nucleus sampling. 1.0 = off. 0.9 works well, but slower
|
||||
int steps = 256; // number of steps to run for
|
||||
char *prompt = NULL; // prompt string
|
||||
unsigned long long rng_seed = 0; // seed rng with time by default
|
||||
char *mode = "generate"; // generate|chat
|
||||
char *system_prompt = NULL; // the (optional) system prompt to use in chat mode
|
||||
|
||||
// poor man's C argparse so we can override the defaults above from the command line
|
||||
if (argc >= 2) { checkpoint_path = argv[1]; } else { error_usage(); }
|
||||
for (int i = 2; i < argc; i+=2) {
|
||||
// do some basic validation
|
||||
if (i + 1 >= argc) { error_usage(); } // must have arg after flag
|
||||
if (argv[i][0] != '-') { error_usage(); } // must start with dash
|
||||
if (strlen(argv[i]) != 2) { error_usage(); } // must be -x (one dash, one letter)
|
||||
// read in the args
|
||||
if (argv[i][1] == 't') { temperature = atof(argv[i + 1]); }
|
||||
else if (argv[i][1] == 'p') { topp = atof(argv[i + 1]); }
|
||||
else if (argv[i][1] == 's') { rng_seed = atoi(argv[i + 1]); }
|
||||
else if (argv[i][1] == 'n') { steps = atoi(argv[i + 1]); }
|
||||
else if (argv[i][1] == 'i') { prompt = argv[i + 1]; }
|
||||
else if (argv[i][1] == 'z') { tokenizer_path = argv[i + 1]; }
|
||||
else if (argv[i][1] == 'm') { mode = argv[i + 1]; }
|
||||
else if (argv[i][1] == 'y') { system_prompt = argv[i + 1]; }
|
||||
else { error_usage(); }
|
||||
}
|
||||
|
||||
// parameter validation/overrides
|
||||
if (rng_seed <= 0) rng_seed = (unsigned int)time(NULL);
|
||||
if (temperature < 0.0) temperature = 0.0;
|
||||
if (topp < 0.0 || 1.0 < topp) topp = 0.9;
|
||||
if (steps < 0) steps = 0;
|
||||
|
||||
// build the Transformer via the model .bin file
|
||||
Transformer transformer;
|
||||
build_transformer(&transformer, checkpoint_path);
|
||||
if (steps == 0 || steps > transformer.config.seq_len) steps = transformer.config.seq_len; // override to ~max length
|
||||
|
||||
// build the Tokenizer via the tokenizer .bin file
|
||||
Tokenizer tokenizer;
|
||||
build_tokenizer(&tokenizer, tokenizer_path, transformer.config.vocab_size);
|
||||
|
||||
// build the Sampler
|
||||
Sampler sampler;
|
||||
build_sampler(&sampler, transformer.config.vocab_size, temperature, topp, rng_seed);
|
||||
|
||||
// run!
|
||||
if (strcmp(mode, "generate") == 0) {
|
||||
generate(&transformer, &tokenizer, &sampler, prompt, steps);
|
||||
} else if (strcmp(mode, "chat") == 0) {
|
||||
chat(&transformer, &tokenizer, &sampler, prompt, system_prompt, steps);
|
||||
} else {
|
||||
fprintf(stderr, "unknown mode: %s\n", mode);
|
||||
error_usage();
|
||||
}
|
||||
|
||||
// memory and file handles cleanup
|
||||
free_sampler(&sampler);
|
||||
free_tokenizer(&tokenizer);
|
||||
free_transformer(&transformer);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
@@ -1,42 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/lua"
|
||||
)
|
||||
|
||||
func coroutineFunc(L *lua.State) c.Int {
|
||||
c.Printf(c.Str("Coroutine started\n"))
|
||||
L.Yield(0) // Pause the coroutine
|
||||
c.Printf(c.Str("Coroutine resumed\n"))
|
||||
return 0
|
||||
}
|
||||
|
||||
func main() {
|
||||
L := lua.Newstate()
|
||||
defer L.Close()
|
||||
|
||||
L.Openlibs()
|
||||
co := L.Newthread()
|
||||
L.Pushcfunction(coroutineFunc)
|
||||
L.Xmove(co, 1)
|
||||
|
||||
var nres c.Int
|
||||
|
||||
c.Printf(c.Str("Resuming coroutine for the first time\n"))
|
||||
result := co.Resume(nil, 0, &nres)
|
||||
if result == lua.YIELD {
|
||||
c.Printf(c.Str("Coroutine yielded\n"))
|
||||
result = co.Resume(nil, 0, &nres)
|
||||
if result == 0 {
|
||||
c.Printf(c.Str("Coroutine finished\n"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Expected output:
|
||||
Resuming coroutine for the first time
|
||||
Coroutine started
|
||||
Coroutine yielded
|
||||
Coroutine finished
|
||||
*/
|
||||
@@ -1,80 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/lua"
|
||||
)
|
||||
|
||||
func coroutineFunc(L *lua.State) {
|
||||
L.Loadstring(c.Str(`
|
||||
function coro_func()
|
||||
for i = 1, 5 do
|
||||
coroutine.yield(i)
|
||||
end
|
||||
end
|
||||
`))
|
||||
L.Pcall(0, 0, 0)
|
||||
L.Getglobal(c.Str("coro_func"))
|
||||
}
|
||||
|
||||
func main() {
|
||||
L := lua.Newstate()
|
||||
defer L.Close()
|
||||
|
||||
L.Openlibs()
|
||||
|
||||
coroutineFunc(L) // Load and get the coroutine function
|
||||
|
||||
co := L.Newthread() // Create a new coroutine/thread
|
||||
L.Pushvalue(-2) // Move the function to the top of the stack
|
||||
L.Xmove(co, 1) // Move the function to the new coroutine
|
||||
|
||||
var nres c.Int
|
||||
var status c.Int
|
||||
|
||||
c.Printf(c.Str("Resuming coroutine...\n"))
|
||||
// Resume coroutine and handle yields
|
||||
for {
|
||||
status = co.Resume(nil, 0, &nres)
|
||||
if status == lua.YIELD {
|
||||
c.Printf(c.Str("Resuming coroutine %d...\n"), status)
|
||||
yieldValue := co.Tointeger(-1)
|
||||
c.Printf(c.Str("Yield value: %d\n"), yieldValue)
|
||||
co.Pop(1) // Clean up the stack
|
||||
|
||||
// Check if the coroutine is yieldable
|
||||
if co.Isyieldable() != 0 {
|
||||
c.Printf(c.Str("Coroutine is yieldable.\n"))
|
||||
} else {
|
||||
c.Printf(c.Str("Coroutine is not yieldable.\n"))
|
||||
}
|
||||
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Check the final status of the coroutine
|
||||
finalStatus := co.Status()
|
||||
c.Printf(c.Str("Final status of coroutine: %d\n"), finalStatus)
|
||||
}
|
||||
|
||||
/* Expected output:
|
||||
Resuming coroutine...
|
||||
Resuming coroutine 1...
|
||||
Yield value: 1
|
||||
Coroutine is yieldable.
|
||||
Resuming coroutine 1...
|
||||
Yield value: 2
|
||||
Coroutine is yieldable.
|
||||
Resuming coroutine 1...
|
||||
Yield value: 3
|
||||
Coroutine is yieldable.
|
||||
Resuming coroutine 1...
|
||||
Yield value: 4
|
||||
Coroutine is yieldable.
|
||||
Resuming coroutine 1...
|
||||
Yield value: 5
|
||||
Coroutine is yieldable.
|
||||
Final status of coroutine: 0
|
||||
*/
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user