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,114 @@
|
|
|
1
|
+
package aot
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
_ "embed"
|
|
5
|
+
"encoding/binary"
|
|
6
|
+
"fmt"
|
|
7
|
+
"io"
|
|
8
|
+
"os"
|
|
9
|
+
|
|
10
|
+
"codecin-native/engine"
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
//go:embed stub_main.go.txt
|
|
14
|
+
var stubSource string
|
|
15
|
+
|
|
16
|
+
// StubSource 返回生成的 main.go 源码。
|
|
17
|
+
func StubSource() string { return stubSource }
|
|
18
|
+
|
|
19
|
+
const maxSteps = 100000000
|
|
20
|
+
|
|
21
|
+
// 段文件 (program.segs) 布局, 由 aot.py 生成:
|
|
22
|
+
// seg_count u32
|
|
23
|
+
// mem_size u64 sp u64 heap_base u64
|
|
24
|
+
// 每段: addr u64 + len u32 + data (初始内存段, 通常只有数据段/零头)
|
|
25
|
+
// 程序内存本身由 VM 按 mem_size 分配 (1 GiB 默认, OS 懒提交)。
|
|
26
|
+
|
|
27
|
+
// Main 运行内嵌的字节码并返回进程退出码 (0 = 正常结束, 1 = 运行期错误)。
|
|
28
|
+
//
|
|
29
|
+
// 标准输入只在被重定向 (管道/文件) 时读取, 交互式终端下不会挂起等待 EOF。
|
|
30
|
+
// AOT 产物里 arg_count()/arg(i) 直接取本进程 os.Args[1:]。
|
|
31
|
+
func Main(bytecode, segFile []byte) int {
|
|
32
|
+
segs, memSize, sp, heapBase, err := parseSegFile(segFile)
|
|
33
|
+
if err != nil {
|
|
34
|
+
fmt.Fprintf(os.Stderr, "runtime error: %s\n", err)
|
|
35
|
+
return 1
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
engine.SetProgramArgs(os.Args[1:])
|
|
39
|
+
|
|
40
|
+
var in []byte
|
|
41
|
+
if fi, err := os.Stdin.Stat(); err == nil &&
|
|
42
|
+
fi.Mode()&os.ModeCharDevice == 0 {
|
|
43
|
+
if data, err := io.ReadAll(os.Stdin); err == nil {
|
|
44
|
+
in = data
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
res := engine.RunV2(&engine.RunOptions{
|
|
49
|
+
BC: bytecode,
|
|
50
|
+
Segments: segs,
|
|
51
|
+
Entry: 0,
|
|
52
|
+
SP: int64(sp),
|
|
53
|
+
HeapBase: int64(heapBase),
|
|
54
|
+
MemSize: int64(memSize),
|
|
55
|
+
Input: in,
|
|
56
|
+
MaxSteps: maxSteps,
|
|
57
|
+
// AOT 产物在真实终端下边跑边输出; 管道/重定向保持结束一次性打印。
|
|
58
|
+
StreamOutput: true,
|
|
59
|
+
})
|
|
60
|
+
if res == nil {
|
|
61
|
+
fmt.Fprintln(os.Stderr, "runtime error: no result")
|
|
62
|
+
return 1
|
|
63
|
+
}
|
|
64
|
+
engine.ConsoleInit()
|
|
65
|
+
if !res.Streamed { // 实时模式下输出已写到终端, Output 只是缓存
|
|
66
|
+
fmt.Print(res.Output)
|
|
67
|
+
}
|
|
68
|
+
if res.Status == engine.StatusError ||
|
|
69
|
+
res.Status == engine.StatusUnsupported {
|
|
70
|
+
msg := res.ErrMsg
|
|
71
|
+
if msg == "" {
|
|
72
|
+
msg = "unsupported instruction (原生 VM 未实现该指令)"
|
|
73
|
+
}
|
|
74
|
+
fmt.Fprintf(os.Stderr, "runtime error: %s\n", msg)
|
|
75
|
+
return 1
|
|
76
|
+
}
|
|
77
|
+
return 0
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// parseSegFile 解析内嵌的段文件。
|
|
81
|
+
func parseSegFile(data []byte) ([]engine.MemSeg, uint64, uint64, uint64, error) {
|
|
82
|
+
if len(data) < 4 {
|
|
83
|
+
return nil, 0, 0, 0, fmt.Errorf("malformed segment file")
|
|
84
|
+
}
|
|
85
|
+
pos := 0
|
|
86
|
+
segCount := int(binary.LittleEndian.Uint32(data[pos:]))
|
|
87
|
+
pos += 4
|
|
88
|
+
if len(data) < pos+24 {
|
|
89
|
+
return nil, 0, 0, 0, fmt.Errorf("malformed segment file")
|
|
90
|
+
}
|
|
91
|
+
memSize := binary.LittleEndian.Uint64(data[pos:])
|
|
92
|
+
sp := binary.LittleEndian.Uint64(data[pos+8:])
|
|
93
|
+
heapBase := binary.LittleEndian.Uint64(data[pos+16:])
|
|
94
|
+
pos += 24
|
|
95
|
+
var segs []engine.MemSeg
|
|
96
|
+
for i := 0; i < segCount; i++ {
|
|
97
|
+
if len(data) < pos+12 {
|
|
98
|
+
return nil, 0, 0, 0, fmt.Errorf("malformed segment file")
|
|
99
|
+
}
|
|
100
|
+
addr := binary.LittleEndian.Uint64(data[pos:])
|
|
101
|
+
dataLen := int(binary.LittleEndian.Uint32(data[pos+8:]))
|
|
102
|
+
pos += 12
|
|
103
|
+
if len(data) < pos+dataLen {
|
|
104
|
+
return nil, 0, 0, 0, fmt.Errorf("malformed segment file")
|
|
105
|
+
}
|
|
106
|
+
if dataLen > 0 {
|
|
107
|
+
segs = append(segs, engine.MemSeg{
|
|
108
|
+
Addr: addr, Data: data[pos : pos+dataLen],
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
pos += dataLen
|
|
112
|
+
}
|
|
113
|
+
return segs, memSize, sp, heapBase, nil
|
|
114
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Code generated by the Code CIN AOT builder; DO NOT EDIT.
|
|
2
|
+
//
|
|
3
|
+
// 这是独立可执行文件的入口 shell: program.ucbc 与 program.segs 由构建器
|
|
4
|
+
// 生成并嵌入, 运行时由内置 Go VM 执行 (静态链接, 无需 Python/动态库)。
|
|
5
|
+
// 程序内存由 VM 按 mem_size 动态分配 (默认 1 GiB, OS 懒提交, 不占实际物理内存)。
|
|
6
|
+
package main
|
|
7
|
+
|
|
8
|
+
import (
|
|
9
|
+
_ "embed"
|
|
10
|
+
"os"
|
|
11
|
+
|
|
12
|
+
"codecin-native/aot"
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
//go:embed program.ucbc
|
|
16
|
+
var bytecode []byte
|
|
17
|
+
|
|
18
|
+
//go:embed program.segs
|
|
19
|
+
var segFile []byte
|
|
20
|
+
|
|
21
|
+
func main() {
|
|
22
|
+
os.Exit(aot.Main(bytecode, segFile))
|
|
23
|
+
}
|
codecin/native/build.ps1
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Code CIN 原生库构建脚本 (Windows)
|
|
2
|
+
# 依赖: Go 1.26+ 且启用 cgo (需 C 编译器, 如 MinGW-w64 / TDM-GCC 的 gcc / clang)
|
|
3
|
+
# 用法: 在本目录执行 .\build.ps1
|
|
4
|
+
# 静态链接: $env:CODECIN_STATIC = '1' (默认尝试静态, 失败自动回退动态)
|
|
5
|
+
# 动态链接: $env:CODECIN_STATIC = '0'
|
|
6
|
+
param()
|
|
7
|
+
$ErrorActionPreference = 'Stop'
|
|
8
|
+
$dir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
9
|
+
Set-Location $dir
|
|
10
|
+
$env:CGO_ENABLED = '1'
|
|
11
|
+
|
|
12
|
+
$out = Join-Path $dir '..\codecin_native.dll'
|
|
13
|
+
$mode = if ($env:CODECIN_STATIC) { $env:CODECIN_STATIC } else { 'auto' }
|
|
14
|
+
|
|
15
|
+
$staticLd = '-linkmode external -extldflags -static'
|
|
16
|
+
$built = $false
|
|
17
|
+
|
|
18
|
+
if ($mode -ne '0') {
|
|
19
|
+
Write-Host "Building (static linking)..."
|
|
20
|
+
go build -buildmode=c-shared -ldflags $staticLd -o $out .
|
|
21
|
+
if ($LASTEXITCODE -eq 0) { $built = $true }
|
|
22
|
+
elseif ($mode -eq '1') { throw "static go build failed (exit $LASTEXITCODE)" }
|
|
23
|
+
else { Write-Host "static linking unavailable, falling back to dynamic" -ForegroundColor Yellow }
|
|
24
|
+
}
|
|
25
|
+
if (-not $built) {
|
|
26
|
+
Write-Host "Building (dynamic linking)..."
|
|
27
|
+
go build -buildmode=c-shared -o $out .
|
|
28
|
+
if ($LASTEXITCODE -ne 0) { throw "go build failed (exit $LASTEXITCODE)" }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
# c-shared 会附带生成头文件, Python ctypes 不需要
|
|
32
|
+
Remove-Item -Force (Join-Path $dir '..\codecin_native.h') -ErrorAction SilentlyContinue
|
|
33
|
+
Write-Host ("Built: " + (Resolve-Path $out).Path)
|
codecin/native/build.sh
ADDED
|
@@ -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,66 @@
|
|
|
1
|
+
// cincc: CIN → 目标机器码 (不经 C / 不经 UCBC)。
|
|
2
|
+
//
|
|
3
|
+
// 用法: cincc -o out prog.cin [--target x86_64-linux]
|
|
4
|
+
package main
|
|
5
|
+
|
|
6
|
+
import (
|
|
7
|
+
"flag"
|
|
8
|
+
"fmt"
|
|
9
|
+
"os"
|
|
10
|
+
|
|
11
|
+
"codecin-native/compiler"
|
|
12
|
+
"codecin-native/mc"
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
func main() {
|
|
16
|
+
out := flag.String("o", "", "输出可执行文件")
|
|
17
|
+
target := flag.String("target", "x86_64-linux", "目标平台")
|
|
18
|
+
verbose := flag.Bool("v", false, "打印 IR")
|
|
19
|
+
flag.Parse()
|
|
20
|
+
if flag.NArg() != 1 || *out == "" {
|
|
21
|
+
fmt.Fprintln(os.Stderr, "用法: cincc -o <out> <prog.cin>")
|
|
22
|
+
os.Exit(2)
|
|
23
|
+
}
|
|
24
|
+
if *target != "x86_64-linux" && *target != "aarch64-linux" {
|
|
25
|
+
fmt.Fprintf(os.Stderr, "cincc: 暂不支持目标 %s\n", *target)
|
|
26
|
+
os.Exit(2)
|
|
27
|
+
}
|
|
28
|
+
src, err := os.ReadFile(flag.Arg(0))
|
|
29
|
+
if err != nil {
|
|
30
|
+
fmt.Fprintf(os.Stderr, "cincc: %v\n", err)
|
|
31
|
+
os.Exit(1)
|
|
32
|
+
}
|
|
33
|
+
prog, err := compiler.CompileFile(flag.Arg(0), "")
|
|
34
|
+
if err != nil {
|
|
35
|
+
prog, err = compiler.Compile(string(src), flag.Arg(0), false)
|
|
36
|
+
if err != nil {
|
|
37
|
+
fmt.Fprintf(os.Stderr, "cincc: 编译失败: %v\n", err)
|
|
38
|
+
os.Exit(1)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if *verbose {
|
|
42
|
+
for i, ins := range prog.Instructions {
|
|
43
|
+
fmt.Printf("%3d %-6s %v\n", i, ins.Op, ins.Args)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
var code []byte
|
|
47
|
+
if *target == "aarch64-linux" {
|
|
48
|
+
code, err = mc.EncodeAArch64(prog)
|
|
49
|
+
} else {
|
|
50
|
+
code, err = mc.Encode(prog)
|
|
51
|
+
}
|
|
52
|
+
if err != nil {
|
|
53
|
+
fmt.Fprintf(os.Stderr, "cincc: %v\n", err)
|
|
54
|
+
os.Exit(1)
|
|
55
|
+
}
|
|
56
|
+
img := mc.WrapELF(prog, code)
|
|
57
|
+
if *target == "aarch64-linux" {
|
|
58
|
+
img = mc.WrapAArch64ELF(prog, code)
|
|
59
|
+
}
|
|
60
|
+
if err := os.WriteFile(*out, img, 0o755); err != nil {
|
|
61
|
+
fmt.Fprintf(os.Stderr, "cincc: 写出失败: %v\n", err)
|
|
62
|
+
os.Exit(1)
|
|
63
|
+
}
|
|
64
|
+
fmt.Printf("cincc: %s -> %s (%d 字节, %d 条 IR)\n",
|
|
65
|
+
flag.Arg(0), *out, len(img), len(prog.Instructions))
|
|
66
|
+
}
|