From b20ad7047f80021e4ca4d19b00e98bdde4352ced Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 13 May 2024 18:24:00 +0800 Subject: [PATCH 1/4] _xtool: llpyg --- _xtool/llpyg/llpyg.go | 48 +++++++++++++++++++++++++++++++++++++++++++ py/bytes.go | 4 ++++ py/dict.go | 45 ++++++++++++++++++++++++++++++++++++++++ py/list.go | 41 ++++++++++++++++++++++++++++++++++++ py/module.go | 2 +- py/tuple.go | 39 +++++++++++++++++++++++++++++++++++ py/unicode.go | 47 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 _xtool/llpyg/llpyg.go create mode 100644 py/dict.go create mode 100644 py/list.go create mode 100644 py/tuple.go create mode 100644 py/unicode.go diff --git a/_xtool/llpyg/llpyg.go b/_xtool/llpyg/llpyg.go new file mode 100644 index 00000000..56053d27 --- /dev/null +++ b/_xtool/llpyg/llpyg.go @@ -0,0 +1,48 @@ +/* + * 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 main + +import ( + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/py" +) + +func main() { + if c.Argc < 2 { + c.Fprintf(c.Stderr, c.Str("Usage: llpyg []\n")) + return + } + + pyLib := c.Index(c.Argv, 1) + destDir := c.Str(".") + if c.Argc > 2 { + destDir = c.Index(c.Argv, 2) + } + c.Fprintf(c.Stderr, c.Str("pyLib: %s, destDir: %s\n"), pyLib, destDir) + + py.Initialize() + mod := py.ImportModule(pyLib) + dict := mod.ModuleGetDict() + items := dict.DictItems() + for i, n := uintptr(0), items.ListLen(); i < n; i++ { + item := items.ListItem(i) + key := item.TupleItem(0) + val := item.TupleItem(1) + _ = val + c.Fprintf(c.Stderr, c.Str("%s\n"), key.CStr()) + } +} diff --git a/py/bytes.go b/py/bytes.go index 1b5277db..52f0275f 100644 --- a/py/bytes.go +++ b/py/bytes.go @@ -16,6 +16,8 @@ package py +/* + import ( _ "unsafe" @@ -44,3 +46,5 @@ func (o *Object) CStr() *c.Char { return nil } // llgo:link (*Object).Strlen C.PyBytes_Size func (o *Object) Strlen() uintptr { return 0 } + +*/ diff --git a/py/dict.go b/py/dict.go new file mode 100644 index 00000000..8f8923b4 --- /dev/null +++ b/py/dict.go @@ -0,0 +1,45 @@ +/* + * 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 py + +import ( + _ "unsafe" +) + +// https://docs.python.org/3/c-api/dict.html + +// Return a new empty dictionary, or nil on failure. +// +//go:linkname NewDict C.PyDict_New +func NewDict() *Object + +// Return a ListObject containing all the keys from the dictionary. +// +// llgo:link (*Object).DictKeys C.PyDict_Keys +func (d *Object) DictKeys() *Object { return nil } + +// Return a ListObject containing all the values from the dictionary. +// +// llgo:link (*Object).DictValues C.PyDict_Values +func (d *Object) DictValues() *Object { return nil } + +// Return a ListObject containing all the items from the dictionary. +// +// llgo:link (*Object).DictItems C.PyDict_Items +func (d *Object) DictItems() *Object { return nil } + +// ----------------------------------------------------------------------------- diff --git a/py/list.go b/py/list.go new file mode 100644 index 00000000..4a5297c9 --- /dev/null +++ b/py/list.go @@ -0,0 +1,41 @@ +/* + * 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 py + +import ( + _ "unsafe" +) + +// https://docs.python.org/3/c-api/list.html + +// Return a new list of length len on success, or nil on failure. +// +//go:linkname NewList C.PyList_New +func NewList(len uintptr) *Object + +// Return the length of the list object in list; this is equivalent to len(list) +// on a list object. +// +// llgo:link (*Object).ListLen C.PyList_Size +func (l *Object) ListLen() uintptr { return 0 } + +// Return the object at position index in the list pointed to by list. The position +// must be non-negative; indexing from the end of the list is not supported. If index +// is out of bounds (<0 or >=len(list)), return nil and set an IndexError exception. +// +// llgo:link (*Object).ListItem C.PyList_GetItem +func (l *Object) ListItem(index uintptr) *Object { return nil } diff --git a/py/module.go b/py/module.go index 17f19b14..6f33cfaf 100644 --- a/py/module.go +++ b/py/module.go @@ -22,7 +22,7 @@ import ( "github.com/goplus/llgo/c" ) -// ----------------------------------------------------------------------------- +// https://docs.python.org/3/c-api/module.html // This is a wrapper around py.Import which takes a const char* as an argument // instead of an Object. diff --git a/py/tuple.go b/py/tuple.go new file mode 100644 index 00000000..a7d7aa30 --- /dev/null +++ b/py/tuple.go @@ -0,0 +1,39 @@ +/* + * 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 py + +import ( + _ "unsafe" +) + +// https://docs.python.org/3/c-api/tuple.html + +// Return a new tuple object of size len, or nil on failure. +// +//go:linkname NewTuple C.PyTuple_New +func NewTuple(len uintptr) *Object + +// Take a pointer to a tuple object, and return the size of that tuple. +// +// llgo:link (*Object).TupleLen C.PyTuple_Size +func (t *Object) TupleLen() uintptr { return 0 } + +// Return the object at position pos in the tuple pointed to by p. If pos is +// negative or out of bounds, return nil and set an IndexError exception. +// +// llgo:link (*Object).TupleItem C.PyTuple_GetItem +func (t *Object) TupleItem(index uintptr) *Object { return nil } diff --git a/py/unicode.go b/py/unicode.go new file mode 100644 index 00000000..6c9fb85c --- /dev/null +++ b/py/unicode.go @@ -0,0 +1,47 @@ +/* + * 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 py + +import ( + _ "unsafe" + + "github.com/goplus/llgo/c" +) + +// https://docs.python.org/3/c-api/unicode.html + +// Return a pointer to the UTF-8 encoding of the Unicode object, and store the +// size of the encoded representation (in bytes) in size. The size argument can +// be nil; in this case no size will be stored. The returned buffer always has +// an extra null byte appended (not included in size), regardless of whether +// there are any other null code points. +// +// In the case of an error, nil is returned with an exception set and no size is +// stored. +// +// This caches the UTF-8 representation of the string in the Unicode object, and +// subsequent calls will return a pointer to the same buffer. The caller is not +// responsible for deallocating the buffer. The buffer is deallocated and pointers +// to it become invalid when the Unicode object is garbage collected. +// +// llgo:link (*Object).CStrAndLen C.PyUnicode_AsUTF8AndSize +func (o *Object) CStrAndLen() (*c.Char, uintptr) { return nil, 0 } + +// As CStrAndLen, but does not store the len. +// +// llgo:link (*Object).CStr C.PyUnicode_AsUTF8 +func (o *Object) CStr() *c.Char { return nil } From 55c0adb23de7fd386af0ac4cab0abbf0f5646179 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 13 May 2024 18:25:05 +0800 Subject: [PATCH 2/4] _testpy/callpy fix --- cl/_testpy/callpy/out.ll | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cl/_testpy/callpy/out.ll b/cl/_testpy/callpy/out.ll index 1f6d541c..1d9a8119 100644 --- a/cl/_testpy/callpy/out.ll +++ b/cl/_testpy/callpy/out.ll @@ -46,7 +46,7 @@ _llgo_0: %6 = call ptr @PyObject_CallNoArgs(ptr %5) %7 = call double @PyFloat_AsDouble(ptr %4) %8 = call i32 (ptr, ...) @printf(ptr @0, double %7) - %9 = call ptr @PyBytes_AsString(ptr %6) + %9 = call ptr @PyUnicode_AsUTF8(ptr %6) %10 = call i32 (ptr, ...) @printf(ptr @1, ptr %9) ret void } @@ -67,7 +67,7 @@ declare double @PyFloat_AsDouble(ptr) declare i32 @printf(ptr, ...) -declare ptr @PyBytes_AsString(ptr) +declare ptr @PyUnicode_AsUTF8(ptr) declare void @llgoLoadPyModSyms(ptr, ...) From 521376e8e872ec3aefa7eb56b50422b7f12ca0a0 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 13 May 2024 18:29:00 +0800 Subject: [PATCH 3/4] py --- py/llgo_autogen.lla | Bin 1822 -> 1762 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/py/llgo_autogen.lla b/py/llgo_autogen.lla index ab226c5eee6f15bb2cb9eddb5bc7a0613c4f92e3..2de8f8f7c65876413c5ad99ebda0676c56d5cb48 100644 GIT binary patch delta 1203 zcmV;k1Wfy$4&n{4ssVq6ldM<@FxDYO1ONb44gdfN02}~qZ)A0BWiD)NRa6N81G$+% zW+k0KW_5TA009K`0RR956aWAK*Iz+tFdEmAC(^PlHwe&TMTcS?79hZ0 zio&2M%GN?rDn;3Ig8c70O7agSc2XnT)AFEDyzf39zdIh~OZ0zx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sk$4i1R^G=GjKoDm>6m-2lRq19(I*JxXwR)8Vk=PrSX{Ik0u{^l>H;Z<{^@@0x*S5&Vj! zhg3A-kQ1lVts!Le;$|VHT-N2}>1^L^nzKKcHs^oB4IDOd{#-#Pq#kCkV{~2*Gk)E| z9<8OsKO#h@u^*_TUrWX4aaak)0cE8#h*f`V9_vEq&LGJ(a3{_nEnK)uE<@A8L7E9@ zK+Q8R&OnsR8Az5(XP_uhsJH+Qm1NGKTu@R!NAS#iXRr*1W9kg<;>D~9>%<4fM>p`{d!EK@kMYIP6vj;V|2Pc1Z zFMMcSdBUV_e5QuatJ{1;ka4wdKKzOPVyc8)YCGsFK|c`KzM-!MVeVMwck~U|@Yd0c zYp>!3RkK>m2IK>VY%!t1M2gCI;kI%znda~%l|ekMiw7>Fu1g68-)&OGrAY$nmwRCU zMX(E*xaTl_*GkdPtt<8%AL`UjIJ24M~GkgQn1Zb|Eb Z1ONcIla>Y}0_2pFy9OZ!WCj2L005eHSA_ro From 947e5591eaad0bc493b4c3a0ac72e0104b2ccc04 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 13 May 2024 18:32:43 +0800 Subject: [PATCH 4/4] py llgo_autogen --- py/llgo.cfg | 1 + py/llgo_autogen.lla | Bin 1762 -> 1635 bytes 2 files changed, 1 insertion(+) diff --git a/py/llgo.cfg b/py/llgo.cfg index 09270308..dcc7c16d 100644 --- a/py/llgo.cfg +++ b/py/llgo.cfg @@ -1,6 +1,7 @@ { "cl": [ "clang -emit-llvm -S -o module.ll -c _pyg/module.c", + "llgen .", "rm llgo_autogen.lla; zip llgo_autogen.lla llgo_autogen.ll module.ll", ] } diff --git a/py/llgo_autogen.lla b/py/llgo_autogen.lla index 2de8f8f7c65876413c5ad99ebda0676c56d5cb48..61233d79e365f891bfc65b3946aec8df95f0cc57 100644 GIT binary patch delta 341 zcmaFF`x8R0|VHAr4!G0F&Xl-zpv~vShM5MgEJK&Ec|ibHk>yo z$$2A|WL$EqHTC1&-v18bDXi7mlWX?(mo&~#syDuTTxz;SnO*JEyj>GxpDuZB6LQ9H z)xUI}<|jKAmc5?YbaP^vIR9?$hO3WXU16VpFUIVmSXZIW+{8D*M<&c<)XA+?_#>A8 z(cb5y-NE>be*aus_9KC7J;ahPnm^_IwZyZtZ7=r?jV0YyYgY)}EIDFf#eU}B9qAn^ z=EXI?bT;%Wr?79`6OhF#Gs(p&ZP|>MqHnA3ufG3A)IRocZ|0JcUy?TtGj3kMIE94| eHFzdFv1x!pVsb8<6{F(h6>KJKD_Ma7#{d9bA&$%d delta 469 zcmaFN^N6=Tz?+#xgn@y9gTbhFO~iw+)&Ij885p=(7#R2&WEgUC((~gJOH1<8Q}gt4 zazaBm8JIT|Iizg@;?fFk21b^zK-FLa)&|CUA2txFJ->bj&uKG>*X@rEw;8yy$g?Ec zCoFjz%X>#n)wiwm&HmY2f}bZ8TYaAPWL|&uxq6X#A$gqg?;o=LYBkU|lTBa6!yP(t zt@9$M*3ir8TQ^x9J{lz86}M>iJJ~t!48>%AiC$VTZH`m!QbE0DEf?J3jSuCuTGmWFa~+~th!=QsYn`+hjS zsLx*fabIx!)cj@ITFUD_w(PsTsr`-YW<|y+EPSZZGx;#91}G*bzhkvxTrgRa&4f*Z I4H%6K0ENB9ZU6uP