-
Notifications
You must be signed in to change notification settings - Fork 3
docs: compare the design difference with llcppg #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
toaction
wants to merge
2
commits into
goplus:main
Choose a base branch
from
toaction:docs/diff
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| ## llpyg 与 llcppg 的设计差异 | ||
|
|
||
| > llcppg: https://github.com/goplus/llcppg | ||
|
|
||
| ### 1. 目标语言 | ||
|
|
||
| - llcppg 面向 C/C++ 库,处理静态类型语言 | ||
| - llpyg 面向 Python 库,处理动态类型语言 | ||
|
|
||
| ### 2. 符号信息获取方式 | ||
|
|
||
| - llcppg 静态分析:C/C++ 库有明确的头文件定义,使用 libclang 解析头文件,获取 AST,得到类型与函数声明 | ||
|
|
||
| - llpyg 动态分析:Python 库没有统一的符号定义文件,目前借助 CPython API,利用反射获取符号信息 | ||
|
|
||
| > llpyg 可以通过解析 AST 的方式获取符号信息,但工作量较大,且具有一定难度 | ||
|
|
||
| ### 3. 类型处理 | ||
|
|
||
| llcppg 类型处理:将头文件声明与库符号匹配,处理复杂类型映射。 | ||
|
|
||
| ```go | ||
| // llgo:link (*BZFILE).Read C.BZ2_bzread | ||
| func (recv_ *BZFILE) Read(buf c.Pointer, len c.Int) c.Int { | ||
| return 0 | ||
| } | ||
| ``` | ||
|
|
||
| llpyg 类型处理:主要使用 `py.Object` 作为通用参数类型,返回类型为 `*py.Object`。 | ||
| > 完善对类的支持后将会增加返回类型转换的功能。 | ||
|
|
||
| ```go | ||
| //go:linkname A py.A | ||
| var A *py.Object | ||
|
|
||
| //llgo:link (*Dog).Speak py.Dog.speak | ||
| func (d *Dog) Speak(msg py.Object) *py.Object { | ||
|
toaction marked this conversation as resolved.
|
||
| return nil | ||
| } | ||
| ``` | ||
|
|
||
| ### 4. 配置文件 | ||
|
|
||
| llcppg 配置文件 (llcppg.cfg): | ||
| ```json | ||
| { | ||
| "name": "zlib", | ||
| "cflags": "$(pkg-config --cflags zlib)", | ||
| "libs": "$(pkg-config --libs zlib)", | ||
| "include": [ | ||
| "zlib.h", | ||
| "zconf.h" | ||
| ], | ||
| "deps": ["c/os"], | ||
|
luoliwoshang marked this conversation as resolved.
|
||
| } | ||
| ``` | ||
| llpyg 配置文件 (llpyg.cfg): | ||
| ```json | ||
| { | ||
| "name": "numpy", | ||
| "libName": "numpy", | ||
| "modules": ["numpy", "numpy.random"] | ||
| } | ||
| ``` | ||
| - `llcppg.cfg` 中的 `cflags` 和 `libs` 字段用于提供编译和链接 C/C++ 库所需的信息,而 `llpyg.cfg` 中的 `libName` 字段则指定了要处理的 Python 库名。两者都用于定位目标库。 | ||
| - `include` 字段和 `modules` 字段作用等同,指出要处理的文件/模块有哪些。 | ||
| - `deps` 字段用于声明库的依赖,在类型处理时需要用到。Python 也有类似的需求,但目前类型统一为了 `py.Object`,后续会添加依赖声明相关功能。 | ||
|
|
||
|
|
||
| ### 5. 架构设计 | ||
|
|
||
| llcppg 与 llpyg 在生成 LLGo Bindings 的处理逻辑是接近的,大致包含三个阶段:符号信息提取 -> 函数签名解析/类型处理 -> Go 代码生成。 | ||
|
|
||
| 主要差异体现在处理方式上: | ||
| - llcppg 通过解析头文件的 AST 提取符号信息,llpyg 通过导入模块利用反射获取符号信息 | ||
| - llcppg 利用符号表对各种类型都做了映射,llpyg 统一为了 `py.Object` | ||
|
|
||
|
|
||
| ### 总结 | ||
|
|
||
| 由于目标语言的根本差异,它们在设计和实现上有显著不同。llcppg 更注重静态分析和复杂的类型系统处理,而 llpyg 更依赖运行时反射和简化的对象模型。 | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.