codecin 5.4.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. codecin/__init__.py +27 -0
  2. codecin/aot.py +229 -0
  3. codecin/assembler.py +548 -0
  4. codecin/cache.py +71 -0
  5. codecin/cin.py +2558 -0
  6. codecin/cli.py +282 -0
  7. codecin/config.py +58 -0
  8. codecin/console.py +183 -0
  9. codecin/cpu.py +1654 -0
  10. codecin/crom.py +222 -0
  11. codecin/debugger.py +659 -0
  12. codecin/disasm.py +86 -0
  13. codecin/errors.py +58 -0
  14. codecin/isa.py +457 -0
  15. codecin/jit.py +270 -0
  16. codecin/libcodecin_native.so +0 -0
  17. codecin/logger.py +145 -0
  18. codecin/memory.py +348 -0
  19. codecin/native/aot/aot.go +70 -0
  20. codecin/native/aot/build.go +219 -0
  21. codecin/native/aot/stub_main.go.txt +22 -0
  22. codecin/native/build.ps1 +33 -0
  23. codecin/native/build.sh +48 -0
  24. codecin/native/cmd/codecin/main.go +287 -0
  25. codecin/native/compiler/codegen.go +1938 -0
  26. codecin/native/compiler/compile_test.go +164 -0
  27. codecin/native/compiler/parser.go +1071 -0
  28. codecin/native/compiler/syscalls.go +86 -0
  29. codecin/native/compiler/tokenizer.go +336 -0
  30. codecin/native/compiler/types.go +213 -0
  31. codecin/native/engine/audio.go +152 -0
  32. codecin/native/engine/audio_other.go +46 -0
  33. codecin/native/engine/audio_windows.go +43 -0
  34. codecin/native/engine/canvas.go +271 -0
  35. codecin/native/engine/crom.go +86 -0
  36. codecin/native/engine/encode.go +93 -0
  37. codecin/native/engine/engine_test.go +198 -0
  38. codecin/native/engine/isa_gen.go +445 -0
  39. codecin/native/engine/system.go +190 -0
  40. codecin/native/engine/termux.go +113 -0
  41. codecin/native/engine/version_gen.go +7 -0
  42. codecin/native/engine/vm.go +1157 -0
  43. codecin/native/go.mod +3 -0
  44. codecin/native/ir/ir.go +48 -0
  45. codecin/native/main.go +225 -0
  46. codecin/native.py +336 -0
  47. codecin/registers.py +130 -0
  48. codecin/stats.py +235 -0
  49. codecin-5.4.2.dist-info/METADATA +985 -0
  50. codecin-5.4.2.dist-info/RECORD +54 -0
  51. codecin-5.4.2.dist-info/WHEEL +5 -0
  52. codecin-5.4.2.dist-info/entry_points.txt +2 -0
  53. codecin-5.4.2.dist-info/licenses/LICENSE +7 -0
  54. codecin-5.4.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env sh
2
+ # Code CIN 原生库构建脚本 (Linux / Termux / macOS)
3
+ # 依赖: Go 1.26+ 且启用 cgo
4
+ # Linux: 安装 gcc (如 apt install gcc golang)
5
+ # Termux: pkg install golang (自带 cgo 工具链, 支持 -buildmode=c-shared)
6
+ # macOS: 安装 Xcode Command Line Tools (clang)
7
+ # 用法: 在本目录执行 sh build.sh
8
+ # 静态链接 (默认): CODECIN_STATIC=auto|1 ; 失败时 auto 会自动回退动态
9
+ # 动态链接: CODECIN_STATIC=0
10
+ set -e
11
+ cd "$(dirname "$0")"
12
+ export CGO_ENABLED=1
13
+
14
+ case "$(uname -s)" in
15
+ Darwin)
16
+ OUT="../libcodecin_native.dylib"
17
+ # macOS 不支持把共享库完全静态链接, 始终动态 (仅依赖系统库)
18
+ STATIC_SUPPORTED=0
19
+ ;;
20
+ *)
21
+ OUT="../libcodecin_native.so"
22
+ STATIC_SUPPORTED=1
23
+ ;;
24
+ esac
25
+
26
+ MODE="${CODECIN_STATIC:-auto}"
27
+ BUILT=0
28
+
29
+ if [ "$MODE" != "0" ] && [ "$STATIC_SUPPORTED" = "1" ]; then
30
+ echo "Building (static linking)..."
31
+ if go build -buildmode=c-shared -ldflags '-linkmode external -extldflags "-static"' -o "$OUT" .; then
32
+ BUILT=1
33
+ elif [ "$MODE" = "1" ]; then
34
+ echo "static go build failed" >&2
35
+ exit 1
36
+ else
37
+ echo "static linking unavailable, falling back to dynamic" >&2
38
+ fi
39
+ fi
40
+
41
+ if [ "$BUILT" != "1" ]; then
42
+ echo "Building (dynamic linking)..."
43
+ go build -buildmode=c-shared -o "$OUT" .
44
+ fi
45
+
46
+ # c-shared 附带的头文件, Python ctypes 不需要
47
+ rm -f ../codecin_native.h ../libcodecin_native.h
48
+ echo "Built: $OUT"
@@ -0,0 +1,287 @@
1
+ // Command codecin 是 Code CIN 的独立 Go CLI: 编译并运行 .cin 程序 (全 Go 链路)。
2
+ //
3
+ // 用法:
4
+ //
5
+ // codecin <program.cin> [--dump-bytecode]
6
+ // codecin build <program.cin> [-o out] [--target os/arch] [--keep-temp]
7
+ //
8
+ // --dump-bytecode 只编译, 把 UCBC 字节码以十六进制打印到 stdout
9
+ // (供 script/diff_go_python.py 做产物级等价比对)
10
+ // build 编译成**独立静态可执行文件** (不依赖 Python/动态库),
11
+ // 默认目标为当前平台, 可用 --target 交叉编译。
12
+ package main
13
+
14
+ import (
15
+ "fmt"
16
+ "io"
17
+ "os"
18
+ "path/filepath"
19
+ "runtime"
20
+
21
+ "codecin-native/aot"
22
+ "codecin-native/compiler"
23
+ "codecin-native/engine"
24
+ "codecin-native/ir"
25
+ )
26
+
27
+ const (
28
+ memSize = aot.DefaultMemSize
29
+ maxSteps = 100000000
30
+ stackSlot = 8
31
+ )
32
+
33
+ func main() {
34
+ args := os.Args[1:]
35
+ if len(args) > 0 && args[0] == "build" {
36
+ os.Exit(runBuild(args[1:]))
37
+ }
38
+ if len(args) > 0 && (args[0] == "--help" || args[0] == "-h") {
39
+ usage(os.Stdout)
40
+ return
41
+ }
42
+
43
+ dumpBytecode := false
44
+ path := ""
45
+ for _, a := range args {
46
+ if a == "--dump-bytecode" {
47
+ dumpBytecode = true
48
+ continue
49
+ }
50
+ if path == "" {
51
+ path = a
52
+ }
53
+ }
54
+ if path == "" {
55
+ usage(os.Stderr)
56
+ os.Exit(2)
57
+ }
58
+
59
+ if _, err := os.Stat(path); err != nil {
60
+ fmt.Fprintf(os.Stderr, "codecin: cannot open %s: %v\n", path, err)
61
+ os.Exit(1)
62
+ }
63
+
64
+ prog, err := compiler.CompileFile(path, findLibDir(path))
65
+ if err != nil {
66
+ fmt.Fprintf(os.Stderr, "codecin: compile error: %v\n", err)
67
+ os.Exit(1)
68
+ }
69
+
70
+ bc, err := engine.EncodeProgram(*prog, 0)
71
+ if err != nil {
72
+ fmt.Fprintf(os.Stderr, "codecin: encode error: %v\n", err)
73
+ os.Exit(1)
74
+ }
75
+ if dumpBytecode {
76
+ fmt.Printf("%x\n", bc)
77
+ return
78
+ }
79
+
80
+ mem := buildMemImage(prog)
81
+ res := engine.Run(bc, mem, 0, int64(memSize-stackSlot),
82
+ int64(memSize/2), readStdin(), maxSteps)
83
+ if res == nil {
84
+ fmt.Fprintln(os.Stderr, "codecin: runtime error: no result")
85
+ os.Exit(1)
86
+ }
87
+ fmt.Print(res.Output)
88
+ if res.Status == engine.StatusError {
89
+ fmt.Fprintf(os.Stderr, "codecin: runtime error: %s\n", res.ErrMsg)
90
+ os.Exit(1)
91
+ }
92
+ }
93
+
94
+ // runBuild 实现 `codecin build`: 编译成独立静态可执行文件。
95
+ func runBuild(args []string) int {
96
+ out := ""
97
+ target := ""
98
+ keepTemp := false
99
+ path := ""
100
+ for i := 0; i < len(args); i++ {
101
+ a := args[i]
102
+ switch a {
103
+ case "-o", "--out", "--output":
104
+ if i+1 >= len(args) {
105
+ fmt.Fprintln(os.Stderr, "codecin build: --out 需要一个路径")
106
+ return 2
107
+ }
108
+ i++
109
+ out = args[i]
110
+ case "--target":
111
+ if i+1 >= len(args) {
112
+ fmt.Fprintln(os.Stderr, "codecin build: --target 需要 os/arch")
113
+ return 2
114
+ }
115
+ i++
116
+ target = args[i]
117
+ case "--keep-temp":
118
+ keepTemp = true
119
+ case "--help", "-h":
120
+ usage(os.Stdout)
121
+ return 0
122
+ default:
123
+ if path == "" {
124
+ path = a
125
+ }
126
+ }
127
+ }
128
+ if path == "" {
129
+ fmt.Fprintln(os.Stderr, "codecin build: 缺少 <program.cin>")
130
+ usage(os.Stderr)
131
+ return 2
132
+ }
133
+ if _, err := os.Stat(path); err != nil {
134
+ fmt.Fprintf(os.Stderr, "codecin: cannot open %s: %v\n", path, err)
135
+ return 1
136
+ }
137
+
138
+ tgt := aot.Target{GOOS: runtime.GOOS, GOARCH: runtime.GOARCH}
139
+ if target != "" {
140
+ parts := splitTarget(target)
141
+ if parts == nil {
142
+ fmt.Fprintf(os.Stderr, "codecin build: --target 格式应为 os/arch, 例如 linux/amd64 (支持: %v)\n",
143
+ aot.SupportedTargets)
144
+ return 2
145
+ }
146
+ tgt = aot.Target{GOOS: parts[0], GOARCH: parts[1]}
147
+ }
148
+ if out == "" {
149
+ base := path[:len(path)-len(filepath.Ext(path))]
150
+ out = base + tgt.ExeSuffix()
151
+ }
152
+
153
+ moduleDir := aot.FindModuleDir(exeDir(), cwd(), path)
154
+ if moduleDir == "" {
155
+ fmt.Fprintln(os.Stderr, "codecin build: 找不到 codecin-native 模块目录 (go.mod); "+
156
+ "AOT 构建需要 Go 工具链与仓库源码")
157
+ return 1
158
+ }
159
+
160
+ prog, err := compiler.CompileFile(path, findLibDir(path))
161
+ if err != nil {
162
+ fmt.Fprintf(os.Stderr, "codecin: compile error: %v\n", err)
163
+ return 1
164
+ }
165
+ bc, err := engine.EncodeProgram(*prog, 0)
166
+ if err != nil {
167
+ fmt.Fprintf(os.Stderr, "codecin: encode error: %v\n", err)
168
+ return 1
169
+ }
170
+
171
+ fmt.Printf("codecin build: %s -> %s (静态链接, 目标 %s)\n", path, out, tgt)
172
+ built, err := aot.Build(aot.BuildOptions{
173
+ ModuleDir: moduleDir,
174
+ Bytecode: bc,
175
+ MemImage: buildMemImage(prog),
176
+ Out: out,
177
+ Target: tgt,
178
+ KeepTemp: keepTemp,
179
+ Stdout: os.Stdout,
180
+ Stderr: os.Stderr,
181
+ })
182
+ if err != nil {
183
+ fmt.Fprintf(os.Stderr, "codecin build: %v\n", err)
184
+ return 1
185
+ }
186
+ fmt.Printf("codecin build: 完成 -> %s\n", built)
187
+ return 0
188
+ }
189
+
190
+ // buildMemImage 生成初始内存镜像 (数据段写入后的 64 KiB 内存)。
191
+ func buildMemImage(prog *ir.Program) []byte {
192
+ mem := make([]byte, memSize)
193
+ for _, dw := range prog.DataWrites {
194
+ if dw.Addr >= 0 && dw.Addr+len(dw.Data) <= len(mem) {
195
+ copy(mem[dw.Addr:], dw.Data)
196
+ }
197
+ }
198
+ return mem
199
+ }
200
+
201
+ // readStdin 仅在标准输入被重定向时读取, 交互终端下不阻塞。
202
+ func readStdin() []byte {
203
+ fi, err := os.Stdin.Stat()
204
+ if err != nil || fi.Mode()&os.ModeCharDevice != 0 {
205
+ return nil
206
+ }
207
+ data, err := io.ReadAll(os.Stdin)
208
+ if err != nil {
209
+ return nil
210
+ }
211
+ return data
212
+ }
213
+
214
+ func exeDir() string {
215
+ exe, err := os.Executable()
216
+ if err != nil {
217
+ return ""
218
+ }
219
+ return filepath.Dir(exe)
220
+ }
221
+
222
+ func cwd() string {
223
+ d, err := os.Getwd()
224
+ if err != nil {
225
+ return ""
226
+ }
227
+ return d
228
+ }
229
+
230
+ func splitTarget(t string) []string {
231
+ for i := 0; i < len(t); i++ {
232
+ if t[i] == '/' {
233
+ if i == 0 || i == len(t)-1 {
234
+ return nil
235
+ }
236
+ return []string{t[:i], t[i+1:]}
237
+ }
238
+ }
239
+ return nil
240
+ }
241
+
242
+ func usage(w *os.File) {
243
+ fmt.Fprint(w, `Code CIN Go CLI
244
+
245
+ 用法:
246
+ codecin <program.cin> [--dump-bytecode] 编译并运行 (全 Go 链路)
247
+ codecin build <program.cin> [-o OUT] [--target OS/ARCH] [--keep-temp]
248
+ 编译成独立静态可执行文件
249
+
250
+ build 选项:
251
+ -o, --out PATH 输出路径 (默认 <程序名> + 平台后缀)
252
+ --target OS/ARCH 交叉编译目标, 支持: `+
253
+ joinTargets(aot.SupportedTargets)+`
254
+ --keep-temp 保留临时构建目录 (排查构建失败)
255
+
256
+ 产物不依赖 Python、Go 工具链与动态库 (CGO_ENABLED=0 静态链接)。
257
+ `)
258
+ }
259
+
260
+ func joinTargets(list []string) string {
261
+ out := ""
262
+ for i, t := range list {
263
+ if i > 0 {
264
+ out += ", "
265
+ }
266
+ out += t
267
+ }
268
+ return out
269
+ }
270
+
271
+ // findLibDir 定位仓库 lib/ 目录 (相对源文件向上查找)。
272
+ func findLibDir(srcPath string) string {
273
+ dir := filepath.Dir(srcPath)
274
+ candidates := []string{
275
+ filepath.Join(dir, "lib"),
276
+ filepath.Join(dir, "..", "lib"),
277
+ filepath.Join(dir, "..", "..", "lib"),
278
+ "lib",
279
+ }
280
+ for _, c := range candidates {
281
+ if fi, err := os.Stat(c); err == nil && fi.IsDir() {
282
+ abs, _ := filepath.Abs(c)
283
+ return abs
284
+ }
285
+ }
286
+ return ""
287
+ }