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
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
package engine
|
|
2
|
+
|
|
3
|
+
// NativeRuntime 是 native 代码生成后端使用的运行时。
|
|
4
|
+
//
|
|
5
|
+
// AOT 的 native 后端不再把 UCBC 字节码交给解释循环,而是把每条指令直接
|
|
6
|
+
// 生成成 Go 语句;本类型只暴露这些语句需要的原语 (寄存器/内存/标志/栈/
|
|
7
|
+
// SYS/输出)。整数/ARM64/RISC-V 扩展/FP/Vector 指令直接使用这里的原语;
|
|
8
|
+
// 少数动态控制流/无法静态求值的操作数通过 Exec 复用现有 VM 语义。
|
|
9
|
+
//
|
|
10
|
+
// 生成代码只依赖本文件的导出 API;不要把 vmState 暴露出去。
|
|
11
|
+
|
|
12
|
+
import (
|
|
13
|
+
"encoding/binary"
|
|
14
|
+
"fmt"
|
|
15
|
+
"math"
|
|
16
|
+
"math/bits"
|
|
17
|
+
"math/rand"
|
|
18
|
+
"os"
|
|
19
|
+
"time"
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
// Arg 是生成代码向 Exec 传递的操作数 (与 VM 的 operand 同构)。
|
|
23
|
+
type Arg struct {
|
|
24
|
+
Kind uint8
|
|
25
|
+
Value int64
|
|
26
|
+
Extra int64
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 操作数种类 (与 engine 内部 kind* 常量一致)。
|
|
30
|
+
const (
|
|
31
|
+
KindReg = kindReg
|
|
32
|
+
KindImm = kindImm
|
|
33
|
+
KindMem = kindMem
|
|
34
|
+
KindCond = kindCond
|
|
35
|
+
KindFloat = kindFloat
|
|
36
|
+
KindStr = kindStr
|
|
37
|
+
KindVec = kindVec
|
|
38
|
+
KindVecLane = kindVecLane
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
// NativeRuntime 持有 native 程序的全部可变状态。
|
|
42
|
+
type NativeRuntime struct {
|
|
43
|
+
vm *vmState
|
|
44
|
+
maxSteps int64
|
|
45
|
+
errMsg string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// NewNativeRuntime 创建执行环境。
|
|
49
|
+
//
|
|
50
|
+
// memSize/sp/heapBase 与 VM 路径一致;segments 是初始化内存段。
|
|
51
|
+
// maxSteps <= 0 表示不限制。
|
|
52
|
+
func NewNativeRuntime(memSize, sp, heapBase int64, segments []MemSeg,
|
|
53
|
+
maxSteps int64) *NativeRuntime {
|
|
54
|
+
|
|
55
|
+
if memSize < 256 {
|
|
56
|
+
memSize = 256
|
|
57
|
+
}
|
|
58
|
+
if memSize > 1<<40 {
|
|
59
|
+
memSize = 1 << 40
|
|
60
|
+
}
|
|
61
|
+
mem := make([]byte, memSize)
|
|
62
|
+
for _, seg := range segments {
|
|
63
|
+
if seg.Addr >= uint64(memSize) || len(seg.Data) == 0 {
|
|
64
|
+
continue
|
|
65
|
+
}
|
|
66
|
+
n := uint64(len(seg.Data))
|
|
67
|
+
if seg.Addr+n > uint64(memSize) {
|
|
68
|
+
n = uint64(memSize) - seg.Addr
|
|
69
|
+
}
|
|
70
|
+
copy(mem[seg.Addr:seg.Addr+n], seg.Data)
|
|
71
|
+
}
|
|
72
|
+
vm := &vmState{
|
|
73
|
+
mem: mem,
|
|
74
|
+
sp: uint64(sp),
|
|
75
|
+
heapPtr: uint64(heapBase),
|
|
76
|
+
heapBase: uint64(heapBase),
|
|
77
|
+
rng: rand.New(rand.NewSource(time.Now().UnixNano())),
|
|
78
|
+
args: currentProgramArgs(),
|
|
79
|
+
}
|
|
80
|
+
vm.dirtyBits = make([]uint64,
|
|
81
|
+
(uint64(memSize)+(1<<(dirtyPageShift+6))-1)>>(dirtyPageShift+6))
|
|
82
|
+
return &NativeRuntime{vm: vm, maxSteps: maxSteps}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// SetInput 设置 stdin 重定向数据 (IN 指令使用)。
|
|
86
|
+
func (r *NativeRuntime) SetInput(data []byte) { r.vm.inData = data }
|
|
87
|
+
|
|
88
|
+
// SetStreamOutput 请求实时输出 (AOT 产物): 真实终端下逐条直写 stdout,
|
|
89
|
+
// 同时保留完整缓存 (Finish 不会再打印缓存, 避免重复)。
|
|
90
|
+
func (r *NativeRuntime) SetStreamOutput(on bool) {
|
|
91
|
+
r.vm.outDirect = on && stdoutIsTerminal()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// SetSandbox 打开沙箱宿主调用限制。
|
|
95
|
+
func (r *NativeRuntime) SetSandbox(on bool) { r.vm.sandbox = on }
|
|
96
|
+
|
|
97
|
+
// Tick 推进一条指令的步数并检查上限 (与 VM 的 maxSteps 语义一致)。
|
|
98
|
+
func (r *NativeRuntime) Tick() string {
|
|
99
|
+
if r.maxSteps > 0 && int64(r.vm.steps) >= r.maxSteps {
|
|
100
|
+
return fmt.Sprintf("instruction limit reached (%d steps)", r.maxSteps)
|
|
101
|
+
}
|
|
102
|
+
r.vm.steps++
|
|
103
|
+
return ""
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// SetPC 记录当前指令索引 (少数回退指令如 AUIPC 需要)。
|
|
107
|
+
func (r *NativeRuntime) SetPC(pc int) { r.vm.pc = pc }
|
|
108
|
+
|
|
109
|
+
// PC 返回当前指令索引 (回退控制流指令执行后读取)。
|
|
110
|
+
func (r *NativeRuntime) PC() int { return r.vm.pc }
|
|
111
|
+
|
|
112
|
+
// ---------------- 寄存器 ----------------
|
|
113
|
+
|
|
114
|
+
// Reg 读取寄存器 (32 = SP, 31 = XZR)。
|
|
115
|
+
func (r *NativeRuntime) Reg(n int) uint64 { return r.vm.reg(n) }
|
|
116
|
+
|
|
117
|
+
// SetReg 写寄存器 (32 写 SP, 31 忽略)。
|
|
118
|
+
func (r *NativeRuntime) SetReg(n int, v uint64) { r.vm.setReg(n, v) }
|
|
119
|
+
|
|
120
|
+
// ---------------- 值/地址 ----------------
|
|
121
|
+
|
|
122
|
+
// Val 按操作数种类取值 (reg/imm/float/str/mem/cond/vec/veclane)。
|
|
123
|
+
func (r *NativeRuntime) Val(kind uint8, value, extra int64) (uint64, string) {
|
|
124
|
+
v, ok := r.vm.val(operand{kind: kind, value: value, extra: extra})
|
|
125
|
+
if !ok {
|
|
126
|
+
return 0, "bad operand"
|
|
127
|
+
}
|
|
128
|
+
return v, ""
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Addr 计算内存地址: base >= 0 时为 reg(base)+off,否则为绝对地址 off。
|
|
132
|
+
func (r *NativeRuntime) Addr(base, off int64) uint64 {
|
|
133
|
+
addr, _ := r.vm.memAddr(operand{kind: kindMem, value: base, extra: off})
|
|
134
|
+
return addr
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ---------------- 整数运算 ----------------
|
|
138
|
+
|
|
139
|
+
func (r *NativeRuntime) Mov(rd int, v uint64) { r.vm.setReg(rd, v) }
|
|
140
|
+
|
|
141
|
+
func (r *NativeRuntime) Add(rd int, v uint64) {
|
|
142
|
+
r.vm.setReg(rd, (r.vm.reg(rd)+v)&mask64)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
func (r *NativeRuntime) AddTo(rd, rs int, v uint64) {
|
|
146
|
+
r.vm.setReg(rd, (r.vm.reg(rs)+v)&mask64)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
func (r *NativeRuntime) Sub(rd int, v uint64) {
|
|
150
|
+
r.vm.setReg(rd, (r.vm.reg(rd)-v)&mask64)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
func (r *NativeRuntime) SubFrom(rd, rs int, v uint64) {
|
|
154
|
+
r.vm.setReg(rd, (r.vm.reg(rs)-v)&mask64)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
func (r *NativeRuntime) Mul(rd int, v uint64) {
|
|
158
|
+
r.vm.setReg(rd, (r.vm.reg(rd)*v)&mask64)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
func (r *NativeRuntime) MulFrom(rd, rs int, v uint64) {
|
|
162
|
+
r.vm.setReg(rd, (r.vm.reg(rs)*v)&mask64)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Div 执行有符号 64 位除法 (向零截断),除数为 0 返回错误。
|
|
166
|
+
func (r *NativeRuntime) Div(rd int, v uint64) string {
|
|
167
|
+
if v == 0 {
|
|
168
|
+
return "Division by zero"
|
|
169
|
+
}
|
|
170
|
+
r.vm.setReg(rd, sdiv(r.vm.reg(rd), v))
|
|
171
|
+
return ""
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
func (r *NativeRuntime) DivFrom(rd, rs int, v uint64) string {
|
|
175
|
+
if v == 0 {
|
|
176
|
+
return "Division by zero"
|
|
177
|
+
}
|
|
178
|
+
r.vm.setReg(rd, sdiv(r.vm.reg(rs), v))
|
|
179
|
+
return ""
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
func (r *NativeRuntime) And(rd int, v uint64) { r.vm.setReg(rd, r.vm.reg(rd)&v) }
|
|
183
|
+
func (r *NativeRuntime) Or(rd int, v uint64) { r.vm.setReg(rd, r.vm.reg(rd)|v) }
|
|
184
|
+
func (r *NativeRuntime) Xor(rd int, v uint64) { r.vm.setReg(rd, r.vm.reg(rd)^v) }
|
|
185
|
+
func (r *NativeRuntime) Mvn(rd int, v uint64) { r.vm.setReg(rd, ^v&mask64) }
|
|
186
|
+
func (r *NativeRuntime) Inc(rd int) { r.vm.setReg(rd, (r.vm.reg(rd)+1)&mask64) }
|
|
187
|
+
func (r *NativeRuntime) Dec(rd int) { r.vm.setReg(rd, (r.vm.reg(rd)-1)&mask64) }
|
|
188
|
+
func (r *NativeRuntime) Shl(rd int, v uint64) { r.vm.setReg(rd, (r.vm.reg(rd)<<(v&63))&mask64) }
|
|
189
|
+
func (r *NativeRuntime) Shr(rd int, v uint64) { r.vm.setReg(rd, r.vm.reg(rd)>>(v&63)) }
|
|
190
|
+
func (r *NativeRuntime) Asr(rd int, v uint64) {
|
|
191
|
+
r.vm.setReg(rd, uint64(int64(r.vm.reg(rd))>>(v&63))&mask64)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
func (r *NativeRuntime) LslFrom(rd, rs int, v uint64) {
|
|
195
|
+
r.vm.setReg(rd, (r.vm.reg(rs)<<(v&63))&mask64)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
func (r *NativeRuntime) LsrFrom(rd, rs int, v uint64) {
|
|
199
|
+
r.vm.setReg(rd, r.vm.reg(rs)>>(v&63))
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
func (r *NativeRuntime) AsrFrom(rd, rs int, v uint64) {
|
|
203
|
+
r.vm.setReg(rd, uint64(int64(r.vm.reg(rs))>>(v&63))&mask64)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
func (r *NativeRuntime) Cmp(a, b uint64) { r.vm.setFlagsSub(a, b) }
|
|
207
|
+
|
|
208
|
+
// ---------------- ARM64/RISC-V 扩展整数 ----------------
|
|
209
|
+
|
|
210
|
+
func (r *NativeRuntime) AddS(rd, rn int, v uint64) {
|
|
211
|
+
a := r.vm.reg(rn)
|
|
212
|
+
res := (a + v) & mask64
|
|
213
|
+
r.vm.setFlagsAddGo(a, v, res)
|
|
214
|
+
r.vm.setReg(rd, res)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
func (r *NativeRuntime) SubS(rd, rn int, v uint64) {
|
|
218
|
+
a := r.vm.reg(rn)
|
|
219
|
+
r.vm.setFlagsSub(a, v)
|
|
220
|
+
r.vm.setReg(rd, (a-v)&mask64)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
func (r *NativeRuntime) AddC(rd, rn int, v uint64) {
|
|
224
|
+
c := uint64(0)
|
|
225
|
+
if r.vm.flags.C {
|
|
226
|
+
c = 1
|
|
227
|
+
}
|
|
228
|
+
a := r.vm.reg(rn)
|
|
229
|
+
res := (a + v + c) & mask64
|
|
230
|
+
r.vm.setFlagsAddGo(a, v, res)
|
|
231
|
+
r.vm.setReg(rd, res)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
func (r *NativeRuntime) SubC(rd, rn int, v uint64) {
|
|
235
|
+
c := uint64(1)
|
|
236
|
+
if r.vm.flags.C {
|
|
237
|
+
c = 0
|
|
238
|
+
}
|
|
239
|
+
a := r.vm.reg(rn)
|
|
240
|
+
r.vm.setFlagsSub(a, v)
|
|
241
|
+
r.vm.setReg(rd, (a-v-c)&mask64)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
func (r *NativeRuntime) Ror(rd, rn int, v uint64) {
|
|
245
|
+
amt := v & 63
|
|
246
|
+
x := r.vm.reg(rn)
|
|
247
|
+
if amt == 0 {
|
|
248
|
+
r.vm.setReg(rd, x)
|
|
249
|
+
return
|
|
250
|
+
}
|
|
251
|
+
r.vm.setReg(rd, ((x>>amt)|(x<<(64-amt)))&mask64)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
func (r *NativeRuntime) Eor(rd, rn int, v uint64) {
|
|
255
|
+
r.vm.setReg(rd, r.vm.reg(rn)^v)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
func (r *NativeRuntime) Bic(rd, rn int, v uint64) {
|
|
259
|
+
r.vm.setReg(rd, r.vm.reg(rn)&(^v&mask64))
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
func (r *NativeRuntime) Orn(rd, rn int, v uint64) {
|
|
263
|
+
r.vm.setReg(rd, r.vm.reg(rn)|(^v&mask64))
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Ldp 读取两个 64 位值到 rt/rt2。
|
|
267
|
+
func (r *NativeRuntime) Ldp(rt, rt2 int, addr uint64) string {
|
|
268
|
+
v1, e := r.vm.readQ(addr)
|
|
269
|
+
if e != "" {
|
|
270
|
+
return e
|
|
271
|
+
}
|
|
272
|
+
v2, e := r.vm.readQ(addr + 8)
|
|
273
|
+
if e != "" {
|
|
274
|
+
return e
|
|
275
|
+
}
|
|
276
|
+
r.vm.setReg(rt, v1)
|
|
277
|
+
r.vm.setReg(rt2, v2)
|
|
278
|
+
return ""
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Stp 把 rt/rt2 两个 64 位值写入 addr/addr+8。
|
|
282
|
+
func (r *NativeRuntime) Stp(rt, rt2 int, addr uint64) string {
|
|
283
|
+
if e := r.vm.writeQ(addr, r.vm.reg(rt)); e != "" {
|
|
284
|
+
return e
|
|
285
|
+
}
|
|
286
|
+
return r.vm.writeQ(addr+8, r.vm.reg(rt2))
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// CondSelect 实现 CSEL/CSINC/CSINV/CSNEG。
|
|
290
|
+
func (r *NativeRuntime) CondSelect(kind string, rd, rn int, v uint64,
|
|
291
|
+
cond uint8) {
|
|
292
|
+
if r.vm.condition(cond) {
|
|
293
|
+
r.vm.setReg(rd, r.vm.reg(rn))
|
|
294
|
+
return
|
|
295
|
+
}
|
|
296
|
+
switch kind {
|
|
297
|
+
case "CSEL":
|
|
298
|
+
r.vm.setReg(rd, v)
|
|
299
|
+
case "CSINC":
|
|
300
|
+
r.vm.setReg(rd, v+1)
|
|
301
|
+
case "CSINV":
|
|
302
|
+
r.vm.setReg(rd, ^v&mask64)
|
|
303
|
+
case "CSNEG":
|
|
304
|
+
r.vm.setReg(rd, uint64(-int64(v))&mask64)
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
func (r *NativeRuntime) SignExtend(rd int, v uint64, width int) {
|
|
309
|
+
var out int64
|
|
310
|
+
switch width {
|
|
311
|
+
case 8:
|
|
312
|
+
out = int64(int8(v))
|
|
313
|
+
case 16:
|
|
314
|
+
out = int64(int16(v))
|
|
315
|
+
case 32:
|
|
316
|
+
out = int64(int32(v))
|
|
317
|
+
default:
|
|
318
|
+
out = int64(v)
|
|
319
|
+
}
|
|
320
|
+
r.vm.setReg(rd, uint64(out)&mask64)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
func (r *NativeRuntime) ZeroExtend(rd int, v uint64, width int) {
|
|
324
|
+
switch width {
|
|
325
|
+
case 8:
|
|
326
|
+
v &= 0xFF
|
|
327
|
+
case 16:
|
|
328
|
+
v &= 0xFFFF
|
|
329
|
+
case 32:
|
|
330
|
+
v &= 0xFFFFFFFF
|
|
331
|
+
}
|
|
332
|
+
r.vm.setReg(rd, v)
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
func (r *NativeRuntime) Clz(rd int, v uint64) { r.vm.setReg(rd, uint64(bits.LeadingZeros64(v))) }
|
|
336
|
+
func (r *NativeRuntime) Rbit(rd int, v uint64) { r.vm.setReg(rd, bits.Reverse64(v)) }
|
|
337
|
+
func (r *NativeRuntime) Rev(rd int, v uint64) { r.vm.setReg(rd, bits.ReverseBytes64(v)) }
|
|
338
|
+
|
|
339
|
+
func (r *NativeRuntime) Cls(rd int, v uint64) {
|
|
340
|
+
sv := int64(v)
|
|
341
|
+
if sv >= 0 {
|
|
342
|
+
if sv == 0 {
|
|
343
|
+
r.vm.setReg(rd, 63)
|
|
344
|
+
} else {
|
|
345
|
+
r.vm.setReg(rd, uint64(63-bits.Len64(uint64(sv))))
|
|
346
|
+
}
|
|
347
|
+
return
|
|
348
|
+
}
|
|
349
|
+
r.vm.setReg(rd, uint64(63-bits.Len64(^uint64(sv))+1))
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
func (r *NativeRuntime) SetLessImm(rd, rs int, v uint64, signed bool) {
|
|
353
|
+
var less bool
|
|
354
|
+
if signed {
|
|
355
|
+
less = int64(r.vm.reg(rs)) < int64(v)
|
|
356
|
+
} else {
|
|
357
|
+
less = r.vm.reg(rs) < v
|
|
358
|
+
}
|
|
359
|
+
if less {
|
|
360
|
+
r.vm.setReg(rd, 1)
|
|
361
|
+
} else {
|
|
362
|
+
r.vm.setReg(rd, 0)
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
func (r *NativeRuntime) Lui(rd int, v uint64) {
|
|
367
|
+
r.vm.setReg(rd, (v&0xFFFFF)<<12)
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// Auipc 的 pc 参数是当前指令索引 (内部按 pc+1 计算)。
|
|
371
|
+
func (r *NativeRuntime) Auipc(rd int, v uint64, pc int) {
|
|
372
|
+
r.vm.setReg(rd, (uint64(pc+1)+((v&0xFFFFF)<<12))&mask64)
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// ---------------- 浮点/向量 ----------------
|
|
376
|
+
|
|
377
|
+
func (r *NativeRuntime) FAdd(rd, rn, rm int) {
|
|
378
|
+
r.vm.setVecScalar(rd, r.vm.vecScalar(rn)+r.vm.vecScalar(rm))
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
func (r *NativeRuntime) FSub(rd, rn, rm int) {
|
|
382
|
+
r.vm.setVecScalar(rd, r.vm.vecScalar(rn)-r.vm.vecScalar(rm))
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
func (r *NativeRuntime) FMul(rd, rn, rm int) {
|
|
386
|
+
r.vm.setVecScalar(rd, r.vm.vecScalar(rn)*r.vm.vecScalar(rm))
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
func (r *NativeRuntime) FDiv(rd, rn, rm int) string {
|
|
390
|
+
b := r.vm.vecScalar(rm)
|
|
391
|
+
if b == 0 {
|
|
392
|
+
return "Float division by zero"
|
|
393
|
+
}
|
|
394
|
+
r.vm.setVecScalar(rd, r.vm.vecScalar(rn)/b)
|
|
395
|
+
return ""
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
func (r *NativeRuntime) FCmp(rn, rm int) {
|
|
399
|
+
a, b := r.vm.vecScalar(rn), r.vm.vecScalar(rm)
|
|
400
|
+
r.vm.flags.Z = a == b
|
|
401
|
+
r.vm.flags.N = a < b
|
|
402
|
+
r.vm.flags.C = a >= b
|
|
403
|
+
r.vm.flags.V = false
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
func (r *NativeRuntime) FCvtToInt(rd, rs int, lane int) {
|
|
407
|
+
v := r.vm.vec[rs&31][lane&3]
|
|
408
|
+
r.vm.setReg(rd, uint64(int64(v))&mask64)
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
func (r *NativeRuntime) FCvtToFloat(rd, rs int) {
|
|
412
|
+
r.vm.setVecScalar(rd, float64(r.vm.reg(rs)))
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
func (r *NativeRuntime) FAbs(rd, rs int) {
|
|
416
|
+
r.vm.setVecScalar(rd, math.Abs(r.vm.vecScalar(rs)))
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
func (r *NativeRuntime) FNeg(rd, rs int) {
|
|
420
|
+
r.vm.setVecScalar(rd, -r.vm.vecScalar(rs))
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
func (r *NativeRuntime) LoadF32(rd int, addr uint64) string {
|
|
424
|
+
if e := r.vm.checkAddr(addr, 4); e != "" {
|
|
425
|
+
return e
|
|
426
|
+
}
|
|
427
|
+
f := math.Float32frombits(binary.LittleEndian.Uint32(r.vm.mem[addr : addr+4]))
|
|
428
|
+
r.vm.setVecScalar(rd, float64(f))
|
|
429
|
+
return ""
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
func (r *NativeRuntime) StoreF32(rs int, addr uint64) string {
|
|
433
|
+
if e := r.vm.checkAddr(addr, 4); e != "" {
|
|
434
|
+
return e
|
|
435
|
+
}
|
|
436
|
+
binary.LittleEndian.PutUint32(r.vm.mem[addr:addr+4],
|
|
437
|
+
math.Float32bits(float32(r.vm.vecScalar(rs))))
|
|
438
|
+
r.vm.touch(addr, 4)
|
|
439
|
+
return ""
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
func (r *NativeRuntime) VOp(op string, rd, rn, rm int) string {
|
|
443
|
+
a, b := r.vm.vec[rn&31], r.vm.vec[rm&31]
|
|
444
|
+
var out [4]float64
|
|
445
|
+
for i := 0; i < 4; i++ {
|
|
446
|
+
switch op {
|
|
447
|
+
case "VADD":
|
|
448
|
+
out[i] = a[i] + b[i]
|
|
449
|
+
case "VSUB":
|
|
450
|
+
out[i] = a[i] - b[i]
|
|
451
|
+
case "VMUL":
|
|
452
|
+
out[i] = a[i] * b[i]
|
|
453
|
+
case "VDIV":
|
|
454
|
+
if b[i] == 0 {
|
|
455
|
+
return "Vector division by zero"
|
|
456
|
+
}
|
|
457
|
+
out[i] = a[i] / b[i]
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
r.vm.vec[rd&31] = out
|
|
461
|
+
return ""
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
func (r *NativeRuntime) VLoad1(rd int, addr uint64) string {
|
|
465
|
+
if e := r.vm.checkAddr(addr, 16); e != "" {
|
|
466
|
+
return e
|
|
467
|
+
}
|
|
468
|
+
data := r.vm.mem[addr : addr+16 : addr+16]
|
|
469
|
+
r.vm.vec[rd&31] = [4]float64{
|
|
470
|
+
float64(math.Float32frombits(binary.LittleEndian.Uint32(data[0:4]))),
|
|
471
|
+
float64(math.Float32frombits(binary.LittleEndian.Uint32(data[4:8]))),
|
|
472
|
+
float64(math.Float32frombits(binary.LittleEndian.Uint32(data[8:12]))),
|
|
473
|
+
float64(math.Float32frombits(binary.LittleEndian.Uint32(data[12:16]))),
|
|
474
|
+
}
|
|
475
|
+
return ""
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
func (r *NativeRuntime) VStore1(rs int, addr uint64) string {
|
|
479
|
+
if e := r.vm.checkAddr(addr, 16); e != "" {
|
|
480
|
+
return e
|
|
481
|
+
}
|
|
482
|
+
v := r.vm.vec[rs&31]
|
|
483
|
+
for i := 0; i < 4; i++ {
|
|
484
|
+
binary.LittleEndian.PutUint32(r.vm.mem[addr+uint64(i)*4:],
|
|
485
|
+
math.Float32bits(float32(v[i])))
|
|
486
|
+
}
|
|
487
|
+
r.vm.touch(addr, 16)
|
|
488
|
+
return ""
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// ---------------- 标志位 ----------------
|
|
492
|
+
|
|
493
|
+
func (r *NativeRuntime) N() bool { return r.vm.flags.N }
|
|
494
|
+
func (r *NativeRuntime) Z() bool { return r.vm.flags.Z }
|
|
495
|
+
func (r *NativeRuntime) C() bool { return r.vm.flags.C }
|
|
496
|
+
func (r *NativeRuntime) V() bool { return r.vm.flags.V }
|
|
497
|
+
|
|
498
|
+
// Cond 按 ARM64 条件码求值 (0=EQ .. 14=AL)。
|
|
499
|
+
func (r *NativeRuntime) Cond(code uint8) bool { return r.vm.condition(code) }
|
|
500
|
+
|
|
501
|
+
// ---------------- 栈 ----------------
|
|
502
|
+
|
|
503
|
+
func (r *NativeRuntime) Push(v uint64) string { return r.vm.push(v) }
|
|
504
|
+
func (r *NativeRuntime) Pop() (uint64, string) { return r.vm.pop() }
|
|
505
|
+
|
|
506
|
+
// PopRet 弹出返回地址并转成指令索引 (RET 使用)。
|
|
507
|
+
func (r *NativeRuntime) PopRet() (int, string) {
|
|
508
|
+
v, e := r.vm.pop()
|
|
509
|
+
return int(v), e
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// ---------------- 内存 ----------------
|
|
513
|
+
|
|
514
|
+
// Load 从 addr 读取 width 字节;signed 为真时做符号扩展。
|
|
515
|
+
func (r *NativeRuntime) Load(rd int, addr uint64, width int, signed bool) string {
|
|
516
|
+
if e := r.vm.checkAddr(addr, width); e != "" {
|
|
517
|
+
return e
|
|
518
|
+
}
|
|
519
|
+
var v int64
|
|
520
|
+
switch width {
|
|
521
|
+
case 1:
|
|
522
|
+
v = int64(r.vm.mem[addr])
|
|
523
|
+
if signed && v&0x80 != 0 {
|
|
524
|
+
v -= 256
|
|
525
|
+
}
|
|
526
|
+
if !signed {
|
|
527
|
+
v &= 0xFF
|
|
528
|
+
}
|
|
529
|
+
case 2:
|
|
530
|
+
v = int64(binary.LittleEndian.Uint16(r.vm.mem[addr : addr+2]))
|
|
531
|
+
if signed && v&0x8000 != 0 {
|
|
532
|
+
v -= 65536
|
|
533
|
+
}
|
|
534
|
+
if !signed {
|
|
535
|
+
v &= 0xFFFF
|
|
536
|
+
}
|
|
537
|
+
case 4:
|
|
538
|
+
v = int64(binary.LittleEndian.Uint32(r.vm.mem[addr : addr+4]))
|
|
539
|
+
if signed && v&0x80000000 != 0 {
|
|
540
|
+
v -= 1 << 32
|
|
541
|
+
}
|
|
542
|
+
if !signed {
|
|
543
|
+
v &= 0xFFFFFFFF
|
|
544
|
+
}
|
|
545
|
+
default:
|
|
546
|
+
v = int64(binary.LittleEndian.Uint64(r.vm.mem[addr : addr+8]))
|
|
547
|
+
}
|
|
548
|
+
r.vm.setReg(rd, uint64(v)&mask64)
|
|
549
|
+
return ""
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// Store 把 rs 的低 width 字节写入 addr。
|
|
553
|
+
func (r *NativeRuntime) Store(rs int, addr uint64, width int) string {
|
|
554
|
+
if e := r.vm.checkAddr(addr, width); e != "" {
|
|
555
|
+
return e
|
|
556
|
+
}
|
|
557
|
+
v := r.vm.reg(rs)
|
|
558
|
+
switch width {
|
|
559
|
+
case 1:
|
|
560
|
+
r.vm.mem[addr] = byte(v)
|
|
561
|
+
case 2:
|
|
562
|
+
binary.LittleEndian.PutUint16(r.vm.mem[addr:addr+2], uint16(v))
|
|
563
|
+
case 4:
|
|
564
|
+
binary.LittleEndian.PutUint32(r.vm.mem[addr:addr+4], uint32(v))
|
|
565
|
+
default:
|
|
566
|
+
binary.LittleEndian.PutUint64(r.vm.mem[addr:addr+8], v)
|
|
567
|
+
}
|
|
568
|
+
r.vm.touch(addr, uint64(width))
|
|
569
|
+
return ""
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// LoadQ 供 mem 操作数取值 (64 位)。
|
|
573
|
+
func (r *NativeRuntime) LoadQ(addr uint64) (uint64, string) { return r.vm.readQ(addr) }
|
|
574
|
+
|
|
575
|
+
// ---------------- I/O / SYS ----------------
|
|
576
|
+
|
|
577
|
+
// In 读取一行整数到 rd (与 VM 的 IN 一致)。
|
|
578
|
+
func (r *NativeRuntime) In(rd int) { r.vm.setReg(rd, r.vm.readLineInt()) }
|
|
579
|
+
|
|
580
|
+
// Out 输出操作数 (字符串/整数/浮点),kind 取 Kind* 常量。
|
|
581
|
+
func (r *NativeRuntime) Out(kind uint8, value, extra int64) string {
|
|
582
|
+
return r.vm.doOut(operand{kind: kind, value: value, extra: extra})
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// Sys 执行宿主/核心系统调用,参数在 X0..X4。
|
|
586
|
+
func (r *NativeRuntime) Sys(id uint64) string { return r.vm.doSyscall(id) }
|
|
587
|
+
|
|
588
|
+
// ---------------- 回退执行 ----------------
|
|
589
|
+
|
|
590
|
+
// Exec 执行一条未直接生成的指令,复用 VM 语义。
|
|
591
|
+
// pc 是当前指令索引;内部按 VM 主循环的约定使用 pc+1 (JAL/JALR/AUIPC/CALL
|
|
592
|
+
// 的链接地址因此是下一条指令)。动态控制流 (JALR/BR/动态 CALL) 也走这里。
|
|
593
|
+
func (r *NativeRuntime) Exec(op string, args []Arg, pc int) string {
|
|
594
|
+
opv, ok := opcodeByName[op]
|
|
595
|
+
if !ok {
|
|
596
|
+
return "unsupported opcode: " + op
|
|
597
|
+
}
|
|
598
|
+
insArgs := make([]operand, len(args))
|
|
599
|
+
for i, a := range args {
|
|
600
|
+
insArgs[i] = operand{kind: a.Kind, value: a.Value, extra: a.Extra}
|
|
601
|
+
}
|
|
602
|
+
// 与 VM 主循环一致: execute 看到的是"已自增"的 pc (JAL/JALR/AUIPC/CALL
|
|
603
|
+
// 的链接地址因此是下一条指令)。
|
|
604
|
+
r.vm.pc = pc + 1
|
|
605
|
+
_, e := r.vm.execute(instruction{opcode: opv, args: insArgs})
|
|
606
|
+
return e
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
// ---------------- 收尾 ----------------
|
|
610
|
+
|
|
611
|
+
// Abort 记录运行期错误;返回 false 供生成代码直接 return。
|
|
612
|
+
func (r *NativeRuntime) Abort(msg string) bool {
|
|
613
|
+
if r.errMsg == "" {
|
|
614
|
+
r.errMsg = msg
|
|
615
|
+
}
|
|
616
|
+
return false
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// Finish 输出程序结果并返回进程退出码 (0=正常, 1=错误)。
|
|
620
|
+
func (r *NativeRuntime) Finish(ok bool) int {
|
|
621
|
+
// 键盘监听可能切换终端 raw mode, 任何出口都必须恢复 (幂等)。
|
|
622
|
+
keyboardRestore()
|
|
623
|
+
if r.vm.outOver && ok {
|
|
624
|
+
ok = false
|
|
625
|
+
if r.errMsg == "" {
|
|
626
|
+
r.errMsg = "output limit exceeded (16 MiB)"
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
if r.vm.out.Len() > 0 && !r.vm.outDirect {
|
|
630
|
+
fmt.Print(r.vm.out.String())
|
|
631
|
+
}
|
|
632
|
+
if ok {
|
|
633
|
+
return 0
|
|
634
|
+
}
|
|
635
|
+
if r.errMsg == "" {
|
|
636
|
+
r.errMsg = "runtime error"
|
|
637
|
+
}
|
|
638
|
+
fmt.Fprintf(os.Stderr, "runtime error: %s\n", r.errMsg)
|
|
639
|
+
return 1
|
|
640
|
+
}
|