codecin 7.22.10__py3-none-win_amd64.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.
- codecin/__init__.py +27 -0
- codecin/aot.py +1057 -0
- codecin/assembler.py +619 -0
- codecin/cbackend.py +3024 -0
- codecin/cin.py +4265 -0
- codecin/cli.py +670 -0
- codecin/codecin_native.dll +0 -0
- codecin/config.py +50 -0
- codecin/console.py +183 -0
- codecin/cpu.py +312 -0
- codecin/crom.py +449 -0
- codecin/cvm.py +708 -0
- codecin/disasm.py +149 -0
- codecin/environment.py +176 -0
- codecin/errors.py +54 -0
- codecin/isa.py +560 -0
- codecin/lib/array/pic.toml +10 -0
- codecin/lib/array/src/array.cin +103 -0
- codecin/lib/bigint/pic.toml +10 -0
- codecin/lib/bigint/src/bigint.cin +589 -0
- codecin/lib/bits/pic.toml +10 -0
- codecin/lib/bits/src/bits.cin +143 -0
- codecin/lib/bitset/pic.toml +10 -0
- codecin/lib/bitset/src/bitset.cin +374 -0
- codecin/lib/codec/pic.toml +10 -0
- codecin/lib/codec/src/codec.cin +563 -0
- codecin/lib/combin/pic.toml +10 -0
- codecin/lib/combin/src/combin.cin +414 -0
- codecin/lib/conv/pic.toml +10 -0
- codecin/lib/conv/src/conv.cin +151 -0
- codecin/lib/cppstd/pic.toml +10 -0
- codecin/lib/cppstd/src/cppstd.cin +418 -0
- codecin/lib/cstd/pic.toml +10 -0
- codecin/lib/cstd/src/cstd.cin +424 -0
- codecin/lib/csv/pic.toml +10 -0
- codecin/lib/csv/src/csv.cin +405 -0
- codecin/lib/dp/pic.toml +10 -0
- codecin/lib/dp/src/dp.cin +325 -0
- codecin/lib/ffi/pic.toml +10 -0
- codecin/lib/ffi/src/ffi.cin +163 -0
- codecin/lib/fmt/pic.toml +10 -0
- codecin/lib/fmt/src/fmt.cin +325 -0
- codecin/lib/frac/pic.toml +10 -0
- codecin/lib/frac/src/frac.cin +252 -0
- codecin/lib/gostd/pic.toml +10 -0
- codecin/lib/gostd/src/gostd.cin +415 -0
- codecin/lib/graph/pic.toml +10 -0
- codecin/lib/graph/src/graph.cin +367 -0
- codecin/lib/gui/pic.toml +10 -0
- codecin/lib/gui/src/gui.cin +218 -0
- codecin/lib/hash/pic.toml +10 -0
- codecin/lib/hash/src/hash.cin +67 -0
- codecin/lib/heap/pic.toml +10 -0
- codecin/lib/heap/src/heap.cin +199 -0
- codecin/lib/io/pic.toml +10 -0
- codecin/lib/io/src/io.cin +141 -0
- codecin/lib/json/pic.toml +10 -0
- codecin/lib/json/src/json.cin +84 -0
- codecin/lib/key/pic.toml +10 -0
- codecin/lib/key/src/key.cin +78 -0
- codecin/lib/math/pic.toml +10 -0
- codecin/lib/math/src/math.cin +50 -0
- codecin/lib/matrix/pic.toml +10 -0
- codecin/lib/matrix/src/matrix.cin +155 -0
- codecin/lib/net/pic.toml +10 -0
- codecin/lib/net/src/net.cin +171 -0
- codecin/lib/path/pic.toml +10 -0
- codecin/lib/path/src/path.cin +372 -0
- codecin/lib/queue/pic.toml +10 -0
- codecin/lib/queue/src/queue.cin +112 -0
- codecin/lib/rand/pic.toml +10 -0
- codecin/lib/rand/src/rand.cin +46 -0
- codecin/lib/set/pic.toml +10 -0
- codecin/lib/set/src/set.cin +269 -0
- codecin/lib/sort/pic.toml +10 -0
- codecin/lib/sort/src/sort.cin +93 -0
- codecin/lib/stat/pic.toml +10 -0
- codecin/lib/stat/src/stat.cin +138 -0
- codecin/lib/str/pic.toml +10 -0
- codecin/lib/str/src/str.cin +54 -0
- codecin/lib/termux/pic.toml +10 -0
- codecin/lib/termux/src/termux.cin +118 -0
- codecin/lib/test/pic.toml +10 -0
- codecin/lib/test/src/test.cin +74 -0
- codecin/lib/text/pic.toml +10 -0
- codecin/lib/text/src/text.cin +323 -0
- codecin/lib/time/pic.toml +10 -0
- codecin/lib/time/src/time.cin +54 -0
- codecin/lib/token/pic.toml +10 -0
- codecin/lib/token/src/token.cin +183 -0
- codecin/lib/tree/pic.toml +10 -0
- codecin/lib/tree/src/tree.cin +256 -0
- codecin/lib/unionfind/pic.toml +10 -0
- codecin/lib/unionfind/src/unionfind.cin +115 -0
- codecin/lib/validate/pic.toml +10 -0
- codecin/lib/validate/src/validate.cin +148 -0
- codecin/lib/vec/pic.toml +10 -0
- codecin/lib/vec/src/vec.cin +88 -0
- codecin/logger.py +145 -0
- codecin/memory.py +306 -0
- codecin/mkn.py +916 -0
- codecin/native/aot/aot.go +114 -0
- codecin/native/aot/stub_main.go.txt +23 -0
- codecin/native/build.ps1 +33 -0
- codecin/native/build.sh +48 -0
- codecin/native/cmd/cincc/main.go +66 -0
- codecin/native/compiler/codegen.go +2238 -0
- codecin/native/compiler/compile_test.go +1069 -0
- codecin/native/compiler/parser.go +1331 -0
- codecin/native/compiler/syscalls.go +166 -0
- codecin/native/compiler/tokenizer.go +394 -0
- codecin/native/compiler/types.go +217 -0
- codecin/native/engine/args.go +37 -0
- codecin/native/engine/audio.go +402 -0
- codecin/native/engine/audio_other.go +105 -0
- codecin/native/engine/audio_test.go +279 -0
- codecin/native/engine/audio_windows.go +190 -0
- codecin/native/engine/canvas.go +271 -0
- codecin/native/engine/console_other.go +9 -0
- codecin/native/engine/console_windows.go +43 -0
- codecin/native/engine/crom.go +86 -0
- codecin/native/engine/encode.go +93 -0
- codecin/native/engine/engine_test.go +198 -0
- codecin/native/engine/ffi_nocgo.go +20 -0
- codecin/native/engine/ffi_registry.go +69 -0
- codecin/native/engine/ffi_unix.go +109 -0
- codecin/native/engine/ffi_windows.go +117 -0
- codecin/native/engine/gui.go +209 -0
- codecin/native/engine/gui_stub.go +13 -0
- codecin/native/engine/gui_test.go +148 -0
- codecin/native/engine/gui_windows.go +604 -0
- codecin/native/engine/gui_x11.go +723 -0
- codecin/native/engine/host_test.go +139 -0
- codecin/native/engine/hostcrypto.go +30 -0
- codecin/native/engine/hostdesktop.go +173 -0
- codecin/native/engine/hostnet.go +127 -0
- codecin/native/engine/isa_gen.go +689 -0
- codecin/native/engine/keyboard.go +432 -0
- codecin/native/engine/keyboard_other.go +94 -0
- codecin/native/engine/keyboard_stub.go +17 -0
- codecin/native/engine/keyboard_termios_darwin.go +9 -0
- codecin/native/engine/keyboard_termios_linux.go +9 -0
- codecin/native/engine/keyboard_test.go +246 -0
- codecin/native/engine/keyboard_windows.go +208 -0
- codecin/native/engine/keyboard_windows_test.go +123 -0
- codecin/native/engine/lineedit.go +262 -0
- codecin/native/engine/lineedit_test.go +105 -0
- codecin/native/engine/lineint_unix.go +17 -0
- codecin/native/engine/lineint_windows.go +19 -0
- codecin/native/engine/mem_other.go +37 -0
- codecin/native/engine/mem_windows.go +33 -0
- codecin/native/engine/native.go +640 -0
- codecin/native/engine/netsock.go +321 -0
- codecin/native/engine/ops_ext.go +515 -0
- codecin/native/engine/stdin.go +72 -0
- codecin/native/engine/stdin_other.go +9 -0
- codecin/native/engine/stdin_windows.go +60 -0
- codecin/native/engine/system.go +313 -0
- codecin/native/engine/termux.go +180 -0
- codecin/native/engine/version_gen.go +7 -0
- codecin/native/engine/vkb_darwin.go +62 -0
- codecin/native/engine/vkb_linux.go +95 -0
- codecin/native/engine/vkb_stub.go +11 -0
- codecin/native/engine/vm.go +1677 -0
- codecin/native/go.mod +3 -0
- codecin/native/ir/ir.go +48 -0
- codecin/native/main.go +448 -0
- codecin/native/mc/aarch64.go +165 -0
- codecin/native/mc/aarch64_test.go +30 -0
- codecin/native/mc/elf.go +88 -0
- codecin/native/mc/print_test.go +52 -0
- codecin/native/mc/x86_64.go +1262 -0
- codecin/native/mc/x86_64_test.go +102 -0
- codecin/native.py +432 -0
- codecin/native_codegen.py +987 -0
- codecin/nativegen.py +786 -0
- codecin/object.py +539 -0
- codecin/optimizer.py +236 -0
- codecin/packaging/__init__.py +46 -0
- codecin/packaging/archive.py +177 -0
- codecin/packaging/model.py +519 -0
- codecin/packaging/spec.py +193 -0
- codecin/packaging/store.py +372 -0
- codecin/packaging/toml.py +541 -0
- codecin/packaging/vcs.py +215 -0
- codecin/pic.py +1917 -0
- codecin/pic_api.py +374 -0
- codecin/pic_login.py +519 -0
- codecin/pic_registry.py +515 -0
- codecin/registers.py +130 -0
- codecin/sdgcc.py +493 -0
- codecin/stats.py +230 -0
- codecin/stdlib.py +138 -0
- codecin/toolchain.py +396 -0
- codecin/toolkit.py +26 -0
- codecin/version.py +260 -0
- codecin-7.22.10.dist-info/METADATA +1077 -0
- codecin-7.22.10.dist-info/RECORD +202 -0
- codecin-7.22.10.dist-info/WHEEL +5 -0
- codecin-7.22.10.dist-info/entry_points.txt +5 -0
- codecin-7.22.10.dist-info/licenses/LICENSE +7 -0
- codecin-7.22.10.dist-info/top_level.txt +1 -0
codecin/native/go.mod
ADDED
codecin/native/ir/ir.go
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Package ir 定义 Code CIN 字节码中间表示 (IR)。
|
|
2
|
+
//
|
|
3
|
+
// 该 IR 与 Python codecin 侧 CINCompiler 产出的结构完全等价:
|
|
4
|
+
//
|
|
5
|
+
// 指令 = (opcode 名, 操作数列表); 操作数用 Kind 区分 (reg/imm/mem/label/cond/float/str)。
|
|
6
|
+
//
|
|
7
|
+
// 编译器 (compiler 包) 产出 *Program, engine 包将其编码为 UCBC 字节码后交由 VM 执行。
|
|
8
|
+
package ir
|
|
9
|
+
|
|
10
|
+
// Operand 表示一条指令的操作数。
|
|
11
|
+
type Operand struct {
|
|
12
|
+
Kind string // reg / imm / mem / label / cond / float / str / vec / veclane
|
|
13
|
+
V int64 // reg 号 / imm 值 / mem base(-1=绝对地址) / str 地址 / vec 号
|
|
14
|
+
Off int64 // mem 偏移 / veclane
|
|
15
|
+
Name string // label 名 / cond 名
|
|
16
|
+
F float64 // float 值
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Instr 是一条 IR 指令。
|
|
20
|
+
type Instr struct {
|
|
21
|
+
Op string
|
|
22
|
+
Args []Operand
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// DataWrite 是一次数据段写入 (地址 + 字节)。
|
|
26
|
+
type DataWrite struct {
|
|
27
|
+
Addr int
|
|
28
|
+
Data []byte
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Program 是编译产物: 指令流 + 标签 + 数据段写入。
|
|
32
|
+
type Program struct {
|
|
33
|
+
Instructions []Instr
|
|
34
|
+
Labels map[string]int
|
|
35
|
+
DataLabels map[string]int
|
|
36
|
+
DataWrites []DataWrite
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 操作数构造辅助 (与编译器共享书写习惯)。
|
|
40
|
+
func Reg(n int) Operand { return Operand{Kind: "reg", V: int64(n)} }
|
|
41
|
+
func Imm(v int64) Operand { return Operand{Kind: "imm", V: v} }
|
|
42
|
+
func Mem(base, off int) Operand {
|
|
43
|
+
return Operand{Kind: "mem", V: int64(base), Off: int64(off)}
|
|
44
|
+
}
|
|
45
|
+
func Label(name string) Operand { return Operand{Kind: "label", Name: name} }
|
|
46
|
+
func Cond(name string) Operand { return Operand{Kind: "cond", Name: name} }
|
|
47
|
+
func Float(f float64) Operand { return Operand{Kind: "float", F: f} }
|
|
48
|
+
func Str(addr int) Operand { return Operand{Kind: "str", V: int64(addr)} }
|
codecin/native/main.go
ADDED
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
#include <stdlib.h>
|
|
5
|
+
*/
|
|
6
|
+
import "C"
|
|
7
|
+
|
|
8
|
+
import (
|
|
9
|
+
"encoding/binary"
|
|
10
|
+
"fmt"
|
|
11
|
+
"math"
|
|
12
|
+
"sync"
|
|
13
|
+
"unsafe"
|
|
14
|
+
|
|
15
|
+
"codecin-native/engine"
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
// 结果结构布局 (与 codecin/native.py _parse_result 严格一致):
|
|
19
|
+
// status u8 + 3B pad
|
|
20
|
+
// pc u64 sp u64 heap_ptr u64 steps u64
|
|
21
|
+
// regs 33×u64
|
|
22
|
+
// vec 32×4×f64
|
|
23
|
+
// mem_len u64 | mem ...
|
|
24
|
+
// out_len u64 | out ...
|
|
25
|
+
// err_len u16 | err ...
|
|
26
|
+
|
|
27
|
+
// v2 结果缓冲布局 (与 codecin/native.py _parse_result_v2 严格一致):
|
|
28
|
+
// status u8 | NZCV 标志 u8 (bit0=N bit1=Z bit2=C bit3=V) | streamed u8 | pad u8
|
|
29
|
+
// pc u64 sp u64 heap_ptr u64 steps u64
|
|
30
|
+
// regs 33×u64
|
|
31
|
+
// vec 32×4×f64
|
|
32
|
+
// seg_count u64 | 每段: addr u64 + len u64 + data (脏段, 按地址升序)
|
|
33
|
+
// out_len u64 | out ...
|
|
34
|
+
// err_len u16 | err ...
|
|
35
|
+
|
|
36
|
+
// v2 请求缓冲布局 (与 codecin/native.py _build_request_v2 严格一致):
|
|
37
|
+
// bc_len u32 | bc ...
|
|
38
|
+
// seg_count u32 | 每段: addr u64 + len u32 + data (输入内存段)
|
|
39
|
+
// in_len u32 | in ... (stdin 重定向数据)
|
|
40
|
+
|
|
41
|
+
// buildVersion 由构建时注入: -ldflags "-X main.buildVersion=<版本>"
|
|
42
|
+
// (codecin/native/build.ps1 / build.sh 会传入 codecin 包版本)。
|
|
43
|
+
var buildVersion = "dev"
|
|
44
|
+
|
|
45
|
+
// marshalResult 把执行结果序列化为 ABI 缓冲。
|
|
46
|
+
// mem 为执行后的内存镜像; panic 降级路径传 nil (调用方按 mem_len=0 处理)。
|
|
47
|
+
func marshalResult(res *engine.Result, mem []byte,
|
|
48
|
+
sp0, heap0 int64) unsafe.Pointer {
|
|
49
|
+
|
|
50
|
+
const regBytes = 33 * 8
|
|
51
|
+
const vecBytes = 32 * 4 * 8
|
|
52
|
+
|
|
53
|
+
status := engine.StatusError
|
|
54
|
+
pc := 0
|
|
55
|
+
sp := uint64(sp0)
|
|
56
|
+
heap := uint64(heap0)
|
|
57
|
+
var steps uint64
|
|
58
|
+
var regs [33]uint64
|
|
59
|
+
out := []byte{}
|
|
60
|
+
errb := []byte{}
|
|
61
|
+
if res != nil {
|
|
62
|
+
status = res.Status
|
|
63
|
+
pc = res.Pc
|
|
64
|
+
sp = res.Sp
|
|
65
|
+
heap = res.HeapPtr
|
|
66
|
+
steps = res.Steps
|
|
67
|
+
regs = res.Regs
|
|
68
|
+
out = []byte(res.Output)
|
|
69
|
+
errb = []byte(res.ErrMsg)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
total := 36 + regBytes + vecBytes + 8 + len(mem) + 8 + len(out) + 2 + len(errb)
|
|
73
|
+
buf := make([]byte, total)
|
|
74
|
+
|
|
75
|
+
buf[0] = byte(status)
|
|
76
|
+
pos := 4
|
|
77
|
+
binary.LittleEndian.PutUint64(buf[pos:], uint64(pc))
|
|
78
|
+
binary.LittleEndian.PutUint64(buf[pos+8:], sp)
|
|
79
|
+
binary.LittleEndian.PutUint64(buf[pos+16:], heap)
|
|
80
|
+
binary.LittleEndian.PutUint64(buf[pos+24:], steps)
|
|
81
|
+
pos = 36 // 状态头 36 字节: status+pad(4) + 4×u64(32)
|
|
82
|
+
for i := 0; i < 33; i++ {
|
|
83
|
+
binary.LittleEndian.PutUint64(buf[pos+i*8:], regs[i])
|
|
84
|
+
}
|
|
85
|
+
pos += regBytes
|
|
86
|
+
pos += vecBytes // 向量区保持 0
|
|
87
|
+
binary.LittleEndian.PutUint64(buf[pos:], uint64(len(mem)))
|
|
88
|
+
pos += 8
|
|
89
|
+
copy(buf[pos:], mem)
|
|
90
|
+
pos += len(mem)
|
|
91
|
+
binary.LittleEndian.PutUint64(buf[pos:], uint64(len(out)))
|
|
92
|
+
pos += 8
|
|
93
|
+
copy(buf[pos:], out)
|
|
94
|
+
pos += len(out)
|
|
95
|
+
binary.LittleEndian.PutUint16(buf[pos:], uint16(len(errb)))
|
|
96
|
+
pos += 2
|
|
97
|
+
copy(buf[pos:], errb)
|
|
98
|
+
|
|
99
|
+
cbuf := C.malloc(C.size_t(total))
|
|
100
|
+
if cbuf == nil {
|
|
101
|
+
return nil
|
|
102
|
+
}
|
|
103
|
+
copy(unsafe.Slice((*byte)(cbuf), total), buf)
|
|
104
|
+
return cbuf
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// errResult 是 panic / 参数非法时的降级结果 (仍返回合法缓冲, 供 Python 解析)。
|
|
108
|
+
func errResult(msg string, sp0, heap0 int64) unsafe.Pointer {
|
|
109
|
+
return marshalResult(&engine.Result{
|
|
110
|
+
Status: engine.StatusError,
|
|
111
|
+
ErrMsg: msg,
|
|
112
|
+
Sp: uint64(sp0),
|
|
113
|
+
HeapPtr: uint64(heap0),
|
|
114
|
+
}, nil, sp0, heap0)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
//export codecin_run
|
|
118
|
+
func codecin_run(bcPtr unsafe.Pointer, bcLen C.int,
|
|
119
|
+
memPtr unsafe.Pointer, memLen C.int,
|
|
120
|
+
entry C.longlong, sp C.longlong, heapBase C.longlong,
|
|
121
|
+
inPtr unsafe.Pointer, inLen C.int,
|
|
122
|
+
maxSteps C.longlong) (ret unsafe.Pointer) {
|
|
123
|
+
|
|
124
|
+
// panic 不得跨越 CGO 边界: Go 运行时会把未捕获 panic 当作致命错误直接
|
|
125
|
+
// 终止宿主进程 (Python 无法拦截)。这里降级为"一次调用返回错误结果"。
|
|
126
|
+
defer func() {
|
|
127
|
+
if r := recover(); r != nil {
|
|
128
|
+
ret = errResult(fmt.Sprintf("native panic: %v", r),
|
|
129
|
+
int64(sp), int64(heapBase))
|
|
130
|
+
}
|
|
131
|
+
}()
|
|
132
|
+
|
|
133
|
+
if bcLen < 0 || memLen < 0 || inLen < 0 {
|
|
134
|
+
return errResult("native: negative buffer length",
|
|
135
|
+
int64(sp), int64(heapBase))
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
var bc, mem []byte
|
|
139
|
+
if bcPtr != nil && bcLen > 0 {
|
|
140
|
+
bc = C.GoBytes(bcPtr, bcLen)
|
|
141
|
+
}
|
|
142
|
+
if memPtr != nil && memLen > 0 {
|
|
143
|
+
mem = C.GoBytes(memPtr, memLen)
|
|
144
|
+
}
|
|
145
|
+
in := []byte{}
|
|
146
|
+
if inPtr != nil && inLen > 0 {
|
|
147
|
+
in = C.GoBytes(inPtr, inLen)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
res := engine.Run(bc, mem, int64(entry), int64(sp), int64(heapBase),
|
|
151
|
+
in, int64(maxSteps))
|
|
152
|
+
return marshalResult(res, mem, int64(sp), int64(heapBase))
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// marshalResultV2 把执行结果序列化为 v2 ABI 缓冲 (段式内存, 带 NZCV 与真实向量)。
|
|
156
|
+
func marshalResultV2(res *engine.Result) unsafe.Pointer {
|
|
157
|
+
const head = 4 + 32 + 33*8 + 32*4*8 + 8 // 1332
|
|
158
|
+
|
|
159
|
+
var status byte
|
|
160
|
+
var flg byte
|
|
161
|
+
var streamed byte
|
|
162
|
+
pc, sp, heap, steps := uint64(0), uint64(0), uint64(0), uint64(0)
|
|
163
|
+
var regs [33]uint64
|
|
164
|
+
var vec [32][4]float64
|
|
165
|
+
var segs []engine.MemSeg
|
|
166
|
+
out := []byte{}
|
|
167
|
+
errb := []byte{}
|
|
168
|
+
if res != nil {
|
|
169
|
+
status = byte(res.Status)
|
|
170
|
+
pc, sp, heap, steps = uint64(res.Pc), res.Sp, res.HeapPtr, res.Steps
|
|
171
|
+
regs = res.Regs
|
|
172
|
+
vec = res.Vec
|
|
173
|
+
segs = res.Segments
|
|
174
|
+
out = []byte(res.Output)
|
|
175
|
+
errb = []byte(res.ErrMsg)
|
|
176
|
+
if res.Flags.N {
|
|
177
|
+
flg |= 1
|
|
178
|
+
}
|
|
179
|
+
if res.Flags.Z {
|
|
180
|
+
flg |= 2
|
|
181
|
+
}
|
|
182
|
+
if res.Flags.C {
|
|
183
|
+
flg |= 4
|
|
184
|
+
}
|
|
185
|
+
if res.Flags.V {
|
|
186
|
+
flg |= 8
|
|
187
|
+
}
|
|
188
|
+
if res.Streamed {
|
|
189
|
+
streamed = 1
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
segsBytes := 0
|
|
194
|
+
for _, s := range segs {
|
|
195
|
+
segsBytes += 16 + len(s.Data)
|
|
196
|
+
}
|
|
197
|
+
total := head + segsBytes + 8 + len(out) + 2 + len(errb)
|
|
198
|
+
buf := make([]byte, total)
|
|
199
|
+
|
|
200
|
+
buf[0] = status
|
|
201
|
+
buf[1] = flg
|
|
202
|
+
buf[2] = streamed
|
|
203
|
+
pos := 4
|
|
204
|
+
binary.LittleEndian.PutUint64(buf[pos:], pc)
|
|
205
|
+
binary.LittleEndian.PutUint64(buf[pos+8:], sp)
|
|
206
|
+
binary.LittleEndian.PutUint64(buf[pos+16:], heap)
|
|
207
|
+
binary.LittleEndian.PutUint64(buf[pos+24:], steps)
|
|
208
|
+
pos = 36
|
|
209
|
+
for i := 0; i < 33; i++ {
|
|
210
|
+
binary.LittleEndian.PutUint64(buf[pos+i*8:], regs[i])
|
|
211
|
+
}
|
|
212
|
+
pos += 33 * 8
|
|
213
|
+
for i := 0; i < 32; i++ {
|
|
214
|
+
for j := 0; j < 4; j++ {
|
|
215
|
+
binary.LittleEndian.PutUint64(buf[pos:], math.Float64bits(vec[i][j]))
|
|
216
|
+
pos += 8
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
binary.LittleEndian.PutUint64(buf[pos:], uint64(len(segs)))
|
|
220
|
+
pos += 8
|
|
221
|
+
for _, s := range segs {
|
|
222
|
+
binary.LittleEndian.PutUint64(buf[pos:], s.Addr)
|
|
223
|
+
binary.LittleEndian.PutUint64(buf[pos+8:], uint64(len(s.Data)))
|
|
224
|
+
copy(buf[pos+16:], s.Data)
|
|
225
|
+
pos += 16 + len(s.Data)
|
|
226
|
+
}
|
|
227
|
+
binary.LittleEndian.PutUint64(buf[pos:], uint64(len(out)))
|
|
228
|
+
pos += 8
|
|
229
|
+
copy(buf[pos:], out)
|
|
230
|
+
pos += len(out)
|
|
231
|
+
binary.LittleEndian.PutUint16(buf[pos:], uint16(len(errb)))
|
|
232
|
+
pos += 2
|
|
233
|
+
copy(buf[pos:], errb)
|
|
234
|
+
|
|
235
|
+
cbuf := C.malloc(C.size_t(total))
|
|
236
|
+
if cbuf == nil {
|
|
237
|
+
return nil
|
|
238
|
+
}
|
|
239
|
+
copy(unsafe.Slice((*byte)(cbuf), total), buf)
|
|
240
|
+
return cbuf
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// errResultV2 是 panic / 参数非法时的 v2 降级结果。
|
|
244
|
+
func errResultV2(msg string, sp0, heap0 int64) unsafe.Pointer {
|
|
245
|
+
return marshalResultV2(&engine.Result{
|
|
246
|
+
Status: engine.StatusError,
|
|
247
|
+
ErrMsg: msg,
|
|
248
|
+
Sp: uint64(sp0),
|
|
249
|
+
HeapPtr: uint64(heap0),
|
|
250
|
+
})
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
//export codecin_run_v2
|
|
254
|
+
func codecin_run_v2(reqPtr unsafe.Pointer, reqLen C.int,
|
|
255
|
+
entry, sp, heapBase, memSize, maxSteps, seed, flags C.longlong) (ret unsafe.Pointer) {
|
|
256
|
+
|
|
257
|
+
// panic 不得跨越 CGO 边界 (同 codecin_run)。
|
|
258
|
+
defer func() {
|
|
259
|
+
if r := recover(); r != nil {
|
|
260
|
+
ret = errResultV2(fmt.Sprintf("native panic: %v", r),
|
|
261
|
+
int64(sp), int64(heapBase))
|
|
262
|
+
}
|
|
263
|
+
}()
|
|
264
|
+
|
|
265
|
+
if reqLen < 0 {
|
|
266
|
+
return errResultV2("native: negative buffer length",
|
|
267
|
+
int64(sp), int64(heapBase))
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
var req []byte
|
|
271
|
+
if reqPtr != nil && reqLen > 0 {
|
|
272
|
+
req = C.GoBytes(reqPtr, reqLen)
|
|
273
|
+
}
|
|
274
|
+
readU32 := func(pos *int) uint32 {
|
|
275
|
+
if *pos+4 > len(req) {
|
|
276
|
+
panic("malformed v2 request")
|
|
277
|
+
}
|
|
278
|
+
v := binary.LittleEndian.Uint32(req[*pos:])
|
|
279
|
+
*pos += 4
|
|
280
|
+
return v
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
opts := &engine.RunOptions{
|
|
284
|
+
Entry: int64(entry),
|
|
285
|
+
SP: int64(sp),
|
|
286
|
+
HeapBase: int64(heapBase),
|
|
287
|
+
MemSize: int64(memSize),
|
|
288
|
+
MaxSteps: int64(maxSteps),
|
|
289
|
+
Seed: int64(seed),
|
|
290
|
+
Sandbox: flags&1 != 0,
|
|
291
|
+
StreamOutput: flags&2 != 0,
|
|
292
|
+
}
|
|
293
|
+
pos := 0
|
|
294
|
+
bcLen := readU32(&pos)
|
|
295
|
+
if int(bcLen) > len(req)-pos {
|
|
296
|
+
return errResultV2("native: malformed v2 request (bytecode)",
|
|
297
|
+
int64(sp), int64(heapBase))
|
|
298
|
+
}
|
|
299
|
+
if bcLen > 0 {
|
|
300
|
+
opts.BC = req[pos : pos+int(bcLen)]
|
|
301
|
+
}
|
|
302
|
+
pos += int(bcLen)
|
|
303
|
+
|
|
304
|
+
segCount := readU32(&pos)
|
|
305
|
+
for i := uint32(0); i < segCount; i++ {
|
|
306
|
+
if pos+12 > len(req) {
|
|
307
|
+
return errResultV2("native: malformed v2 request (segment)",
|
|
308
|
+
int64(sp), int64(heapBase))
|
|
309
|
+
}
|
|
310
|
+
addr := binary.LittleEndian.Uint64(req[pos:])
|
|
311
|
+
dataLen := binary.LittleEndian.Uint32(req[pos+8:])
|
|
312
|
+
pos += 12
|
|
313
|
+
if int(dataLen) > len(req)-pos {
|
|
314
|
+
return errResultV2("native: malformed v2 request (segment data)",
|
|
315
|
+
int64(sp), int64(heapBase))
|
|
316
|
+
}
|
|
317
|
+
if dataLen > 0 {
|
|
318
|
+
opts.Segments = append(opts.Segments, engine.MemSeg{
|
|
319
|
+
Addr: addr, Data: req[pos : pos+int(dataLen)],
|
|
320
|
+
})
|
|
321
|
+
}
|
|
322
|
+
pos += int(dataLen)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
inLen := readU32(&pos)
|
|
326
|
+
if int(inLen) > len(req)-pos {
|
|
327
|
+
return errResultV2("native: malformed v2 request (input)",
|
|
328
|
+
int64(sp), int64(heapBase))
|
|
329
|
+
}
|
|
330
|
+
if inLen > 0 {
|
|
331
|
+
opts.Input = req[pos : pos+int(inLen)]
|
|
332
|
+
}
|
|
333
|
+
_ = pos
|
|
334
|
+
|
|
335
|
+
res := engine.RunV2(opts)
|
|
336
|
+
if res == nil {
|
|
337
|
+
return errResultV2("native: no result", int64(sp), int64(heapBase))
|
|
338
|
+
}
|
|
339
|
+
return marshalResultV2(res)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
//export codecin_free
|
|
343
|
+
func codecin_free(ptr unsafe.Pointer) {
|
|
344
|
+
if ptr != nil {
|
|
345
|
+
C.free(ptr)
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
//export codecin_set_args
|
|
350
|
+
func codecin_set_args(argv **C.char, argc C.int) {
|
|
351
|
+
defer func() {
|
|
352
|
+
recover() // 参数注入失败不得拖垮宿主进程: 降级为无参数
|
|
353
|
+
}()
|
|
354
|
+
if argc < 0 {
|
|
355
|
+
return
|
|
356
|
+
}
|
|
357
|
+
args := make([]string, 0, int(argc))
|
|
358
|
+
if argc > 0 {
|
|
359
|
+
for _, p := range unsafe.Slice(argv, int(argc)) {
|
|
360
|
+
if p == nil {
|
|
361
|
+
args = append(args, "")
|
|
362
|
+
} else {
|
|
363
|
+
args = append(args, C.GoString(p))
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
engine.SetProgramArgs(args)
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
//export codecin_crom_pack
|
|
371
|
+
func codecin_crom_pack(dataPtr unsafe.Pointer, dataLen C.int, compress C.int,
|
|
372
|
+
outLen *C.int) (ret unsafe.Pointer) {
|
|
373
|
+
defer func() {
|
|
374
|
+
if r := recover(); r != nil {
|
|
375
|
+
ret = nil
|
|
376
|
+
}
|
|
377
|
+
}()
|
|
378
|
+
if outLen == nil || dataLen < 0 || dataPtr == nil {
|
|
379
|
+
return nil
|
|
380
|
+
}
|
|
381
|
+
data := C.GoBytes(dataPtr, dataLen)
|
|
382
|
+
packed := engine.CromPack(data, compress != 0)
|
|
383
|
+
if packed == nil {
|
|
384
|
+
return nil
|
|
385
|
+
}
|
|
386
|
+
cbuf := C.malloc(C.size_t(len(packed)))
|
|
387
|
+
if cbuf == nil {
|
|
388
|
+
return nil
|
|
389
|
+
}
|
|
390
|
+
copy(unsafe.Slice((*byte)(cbuf), len(packed)), packed)
|
|
391
|
+
*outLen = C.int(len(packed))
|
|
392
|
+
return cbuf
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
//export codecin_crom_unpack
|
|
396
|
+
func codecin_crom_unpack(dataPtr unsafe.Pointer, dataLen C.int,
|
|
397
|
+
memLen *C.int, flags *C.int) (ret unsafe.Pointer) {
|
|
398
|
+
defer func() {
|
|
399
|
+
if r := recover(); r != nil {
|
|
400
|
+
ret = nil
|
|
401
|
+
}
|
|
402
|
+
}()
|
|
403
|
+
if memLen == nil || flags == nil || dataLen < 0 || dataPtr == nil {
|
|
404
|
+
return nil
|
|
405
|
+
}
|
|
406
|
+
data := C.GoBytes(dataPtr, dataLen)
|
|
407
|
+
raw, flg, ok := engine.CromUnpack(data)
|
|
408
|
+
if !ok {
|
|
409
|
+
return nil
|
|
410
|
+
}
|
|
411
|
+
cbuf := C.malloc(C.size_t(len(raw)))
|
|
412
|
+
if cbuf == nil {
|
|
413
|
+
return nil
|
|
414
|
+
}
|
|
415
|
+
copy(unsafe.Slice((*byte)(cbuf), len(raw)), raw)
|
|
416
|
+
*memLen = C.int(len(raw))
|
|
417
|
+
*flags = C.int(flg)
|
|
418
|
+
return cbuf
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// buildVersion 为空时使用生成器注入的 engine.BuildVersion
|
|
422
|
+
// (单一事实来源: codecin/__init__.py 的 __version__)。
|
|
423
|
+
func versionString() string {
|
|
424
|
+
v := buildVersion
|
|
425
|
+
if v == "" || v == "dev" {
|
|
426
|
+
v = engine.BuildVersion
|
|
427
|
+
}
|
|
428
|
+
return "codecin-native " + v + " (Go)"
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
var (
|
|
432
|
+
versionOnce sync.Once
|
|
433
|
+
versionCStr *C.char
|
|
434
|
+
)
|
|
435
|
+
|
|
436
|
+
// codecin_version 返回静态版本字符串。
|
|
437
|
+
// 旧实现每次调用都 C.CString, 而 Python 侧以 c_char_p 取走后永不 free → 每次调用泄漏。
|
|
438
|
+
// 返回的指针为进程级常量, 调用方不要 free。
|
|
439
|
+
//
|
|
440
|
+
//export codecin_version
|
|
441
|
+
func codecin_version() *C.char {
|
|
442
|
+
versionOnce.Do(func() {
|
|
443
|
+
versionCStr = C.CString(versionString())
|
|
444
|
+
})
|
|
445
|
+
return versionCStr
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
func main() {}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
package mc
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/binary"
|
|
5
|
+
"fmt"
|
|
6
|
+
|
|
7
|
+
"codecin-native/ir"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
// AArch64 (ARM64) 最小后端: MOV/ADD/SUB/HALT/OUT(str) 与 MOV reg,reg。
|
|
11
|
+
// 说明: 本机 WSL 无 qemu-aarch64, 只能做结构校验 (e_machine=0xB7/入口/段),
|
|
12
|
+
// 无法像 x86-64 那样真实执行——见 aarch64_test.go 的说明。
|
|
13
|
+
|
|
14
|
+
const aarchEntryOff = 0x78
|
|
15
|
+
|
|
16
|
+
type a8 struct{ code []byte }
|
|
17
|
+
|
|
18
|
+
func (a *a8) word(w uint32) {
|
|
19
|
+
var b [4]byte
|
|
20
|
+
binary.LittleEndian.PutUint32(b[:], w)
|
|
21
|
+
a.code = append(a.code, b[:]...)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
func (a *a8) movz(d int, imm16 uint32, shift uint) {
|
|
25
|
+
a.word(0xD2800000 | uint32(shift)<<21 | (imm16&0xFFFF)<<5 | uint32(d))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
func (a *a8) movk(d int, imm16 uint32, shift uint) {
|
|
29
|
+
a.word(0xF2800000 | uint32(shift)<<21 | (imm16&0xFFFF)<<5 | uint32(d))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// movImm 把 64 位常量装入 x<d>。
|
|
33
|
+
func (a *a8) movImm(d int, v uint64) {
|
|
34
|
+
a.movz(d, uint32(v&0xFFFF), 0)
|
|
35
|
+
for s := uint(1); s < 4; s++ {
|
|
36
|
+
if part := uint32((v >> (16 * s)) & 0xFFFF); part != 0 {
|
|
37
|
+
a.movk(d, part, s)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
func (a *a8) ldr(t, n int) { a.word(0xF9400000 | uint32(n)<<5 | uint32(t)) }
|
|
43
|
+
func (a *a8) str(t, n int) { a.word(0xF9000000 | uint32(n)<<5 | uint32(t)) }
|
|
44
|
+
func (a *a8) addImm(d, n int, imm uint32) {
|
|
45
|
+
a.word(0x91000000 | (imm&0xFFF)<<10 | uint32(n)<<5 | uint32(d))
|
|
46
|
+
}
|
|
47
|
+
func (a *a8) subImm(d, n int, imm uint32) {
|
|
48
|
+
a.word(0xD1000000 | (imm&0xFFF)<<10 | uint32(n)<<5 | uint32(d))
|
|
49
|
+
}
|
|
50
|
+
func (a *a8) movReg(d, m int) { a.word(0xAA0003E0 | uint32(m)<<16 | uint32(d)) }
|
|
51
|
+
func (a *a8) addReg(d, n, m int) { a.word(0x8B000000 | uint32(m)<<16 | uint32(n)<<5 | uint32(d)) }
|
|
52
|
+
func (a *a8) subReg(d, n, m int) { a.word(0xCB000000 | uint32(m)<<16 | uint32(n)<<5 | uint32(d)) }
|
|
53
|
+
func (a *a8) svc() { a.word(0xD4000001) }
|
|
54
|
+
|
|
55
|
+
// loadSlot: x<d> = VM 寄存器槽 [slot]
|
|
56
|
+
func (a *a8) loadSlot(d int, slot int32) {
|
|
57
|
+
a.movImm(16, uint64(uint32(slot)))
|
|
58
|
+
a.ldr(d, 16)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
func (a *a8) storeSlot(s int, slot int32) {
|
|
62
|
+
a.movImm(16, uint64(uint32(slot)))
|
|
63
|
+
a.str(s, 16)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// EncodeAArch64 把 IR 编译为 AArch64 机器码。
|
|
67
|
+
func EncodeAArch64(p *ir.Program) ([]byte, error) {
|
|
68
|
+
a := &a8{}
|
|
69
|
+
for _, ins := range p.Instructions {
|
|
70
|
+
switch ins.Op {
|
|
71
|
+
case "MOV":
|
|
72
|
+
if len(ins.Args) == 2 && ins.Args[0].Kind == "reg" && ins.Args[1].Kind == "imm" {
|
|
73
|
+
a.movImm(0, uint64(ins.Args[1].V))
|
|
74
|
+
a.storeSlot(0, regAddr(int(ins.Args[0].V)))
|
|
75
|
+
continue
|
|
76
|
+
}
|
|
77
|
+
if len(ins.Args) == 2 && ins.Args[0].Kind == "reg" && ins.Args[1].Kind == "reg" {
|
|
78
|
+
a.loadSlot(0, regAddr(int(ins.Args[1].V)))
|
|
79
|
+
a.storeSlot(0, regAddr(int(ins.Args[0].V)))
|
|
80
|
+
continue
|
|
81
|
+
}
|
|
82
|
+
return nil, fmt.Errorf("aarch64: MOV 只支持 reg,imm / reg,reg")
|
|
83
|
+
case "ADD", "SUB":
|
|
84
|
+
if len(ins.Args) == 2 && ins.Args[1].Kind == "imm" {
|
|
85
|
+
slot := regAddr(int(ins.Args[0].V))
|
|
86
|
+
a.loadSlot(0, slot)
|
|
87
|
+
if ins.Op == "ADD" {
|
|
88
|
+
a.addImm(0, 0, uint32(ins.Args[1].V))
|
|
89
|
+
} else {
|
|
90
|
+
a.subImm(0, 0, uint32(ins.Args[1].V))
|
|
91
|
+
}
|
|
92
|
+
a.storeSlot(0, slot)
|
|
93
|
+
continue
|
|
94
|
+
}
|
|
95
|
+
if len(ins.Args) == 2 && ins.Args[1].Kind == "reg" {
|
|
96
|
+
rd := regAddr(int(ins.Args[0].V))
|
|
97
|
+
a.loadSlot(0, rd)
|
|
98
|
+
a.loadSlot(1, regAddr(int(ins.Args[1].V)))
|
|
99
|
+
if ins.Op == "ADD" {
|
|
100
|
+
a.addReg(0, 0, 1)
|
|
101
|
+
} else {
|
|
102
|
+
a.subReg(0, 0, 1)
|
|
103
|
+
}
|
|
104
|
+
a.storeSlot(0, rd)
|
|
105
|
+
continue
|
|
106
|
+
}
|
|
107
|
+
return nil, fmt.Errorf("aarch64: %s 操作数不支持", ins.Op)
|
|
108
|
+
case "OUT":
|
|
109
|
+
if len(ins.Args) == 1 && ins.Args[0].Kind == "str" {
|
|
110
|
+
addr := ins.Args[0].V
|
|
111
|
+
n := dataLen(p, addr)
|
|
112
|
+
a.movImm(0, 1) // fd
|
|
113
|
+
a.movImm(1, uint64(uint32(dataBase+int(addr)))) // buf
|
|
114
|
+
a.movImm(2, uint64(n)) // len
|
|
115
|
+
a.movImm(8, 64) // write
|
|
116
|
+
a.svc()
|
|
117
|
+
continue
|
|
118
|
+
}
|
|
119
|
+
return nil, fmt.Errorf("aarch64: OUT 只支持静态字符串")
|
|
120
|
+
case "HALT":
|
|
121
|
+
a.loadSlot(0, regAddr(0))
|
|
122
|
+
a.movImm(8, 93) // exit
|
|
123
|
+
a.svc()
|
|
124
|
+
return a.code, nil
|
|
125
|
+
default:
|
|
126
|
+
return nil, fmt.Errorf("aarch64 后端暂不支持指令: %s", ins.Op)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return nil, fmt.Errorf("程序缺少 HALT")
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// WrapAArch64ELF 包装为 AArch64 ELF64。
|
|
133
|
+
func WrapAArch64ELF(p *ir.Program, code []byte) []byte {
|
|
134
|
+
dataOff := dataBase - baseAddr
|
|
135
|
+
end := dataOff + 0x500
|
|
136
|
+
for _, w := range p.DataWrites {
|
|
137
|
+
if e := dataOff + w.Addr + len(w.Data); e > end {
|
|
138
|
+
end = e
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
img := make([]byte, end)
|
|
142
|
+
copy(img[aarchEntryOff:], code)
|
|
143
|
+
for _, w := range p.DataWrites {
|
|
144
|
+
copy(img[dataOff+w.Addr:], w.Data)
|
|
145
|
+
}
|
|
146
|
+
le := binary.LittleEndian
|
|
147
|
+
copy(img[0:16], []byte{0x7F, 'E', 'L', 'F', 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0})
|
|
148
|
+
le.PutUint16(img[16:], 2) // ET_EXEC
|
|
149
|
+
le.PutUint16(img[18:], 0xB7) // EM_AARCH64
|
|
150
|
+
le.PutUint32(img[20:], 1)
|
|
151
|
+
le.PutUint64(img[24:], uint64(baseAddr+aarchEntryOff)) // e_entry
|
|
152
|
+
le.PutUint64(img[32:], ehdrSize)
|
|
153
|
+
le.PutUint16(img[52:], ehdrSize)
|
|
154
|
+
le.PutUint16(img[54:], phdrSize)
|
|
155
|
+
le.PutUint16(img[56:], 1)
|
|
156
|
+
ph := ehdrSize
|
|
157
|
+
le.PutUint32(img[ph:], 1)
|
|
158
|
+
le.PutUint32(img[ph+4:], 7)
|
|
159
|
+
le.PutUint64(img[ph+16:], uint64(baseAddr))
|
|
160
|
+
le.PutUint64(img[ph+24:], uint64(baseAddr))
|
|
161
|
+
le.PutUint64(img[ph+32:], uint64(len(img)))
|
|
162
|
+
le.PutUint64(img[ph+40:], uint64(len(img)))
|
|
163
|
+
le.PutUint64(img[ph+48:], 0x1000)
|
|
164
|
+
return img
|
|
165
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
package mc
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/binary"
|
|
5
|
+
"testing"
|
|
6
|
+
|
|
7
|
+
"codecin-native/ir"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
func TestAArch64ELFAndEncoding(t *testing.T) {
|
|
11
|
+
p := &ir.Program{Instructions: []ir.Instr{
|
|
12
|
+
{Op: "MOV", Args: []ir.Operand{ir.Reg(0), ir.Imm(42)}},
|
|
13
|
+
{Op: "HALT"},
|
|
14
|
+
}}
|
|
15
|
+
code, err := EncodeAArch64(p)
|
|
16
|
+
if err != nil {
|
|
17
|
+
t.Fatalf("EncodeAArch64: %v", err)
|
|
18
|
+
}
|
|
19
|
+
img := WrapAArch64ELF(p, code)
|
|
20
|
+
if binary.LittleEndian.Uint16(img[18:]) != 0xB7 {
|
|
21
|
+
t.Fatalf("e_machine != AArch64")
|
|
22
|
+
}
|
|
23
|
+
if binary.LittleEndian.Uint64(img[24:]) != uint64(baseAddr+aarchEntryOff) {
|
|
24
|
+
t.Fatalf("入口不正确")
|
|
25
|
+
}
|
|
26
|
+
last := len(code) - 4
|
|
27
|
+
if w := binary.LittleEndian.Uint32(code[last:]); w != 0xD4000001 {
|
|
28
|
+
t.Fatalf("最后一条应为 svc #0, got %#x", w)
|
|
29
|
+
}
|
|
30
|
+
}
|