@yeyelailou/co 0.0.3 → 0.0.4
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.
- package/README.md +54 -11
- package/bin//345/205/245/345/217/243.js +0 -8
- package/package.json +4 -5
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//346/234/272/345/231/250/347/240/201/347/224/237/346/210/220/345/231/250.co +21 -1
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/347/274/226/350/257/221.sh +73 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201//347/274/226/350/257/221/351/251/261/345/212/250.co +3 -1
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201//347/274/226/350/257/221/351/252/214/350/257/201.sh +24 -8
- package/bin/install.js +0 -276
- package//345/267/245/345/205/267/lsp/server.js +0 -419
- package//345/267/245/345/205/267/lsp//346/265/213/350/257/225.sh +0 -81
- package//345/267/245/345/205/267/vscode-cocn/extension.js +0 -203
- package//345/267/245/345/205/267/vscode-cocn/package.json +0 -58
- package//345/267/245/345/205/267/vscode-cocn/syntaxes/coCN.tmLanguage.json +0 -130
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/273/243/347/240/201/347/224/237/346/210/220/345/231/250.js +0 -2284
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/analysis.js +0 -116
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/benchmark_demo.js +0 -75
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/dominance.js +0 -158
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/gvn.js +0 -45
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/index.js +0 -121
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/ir.js +0 -237
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/constProp.js +0 -126
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/copyProp.js +0 -57
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/cse.js +0 -62
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/dce.js +0 -68
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/devirt.js +0 -38
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/escape.js +0 -67
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/inline.js +0 -142
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/loopOpt.js +0 -215
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/peephole.js +0 -42
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/regalloc.js +0 -212
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/scheduler.js +0 -86
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/spill.js +0 -78
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/ssa.js +0 -235
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/traceSched.js +0 -47
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/unroll.js +0 -46
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/harness.js +0 -33
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/run_all.js +0 -11
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_constfold.js +0 -58
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_dce.js +0 -49
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_e2e.js +0 -57
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_gvn_spill.js +0 -45
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_loop.js +0 -46
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_regalloc.js +0 -51
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_ssa.js +0 -65
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//345/205/245/345/217/243.c +0 -9
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//345/205/245/345/217/243.js +0 -38
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237/AST/345/267/245/345/205/267.js +0 -126
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237/index.js +0 -103
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//345/206/205/350/201/224.js +0 -194
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//345/207/275/346/225/260/347/211/271/345/214/226.js +0 -168
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/243/200/346/237/245/346/266/210/351/231/244.js +0 -166
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//345/206/205/350/201/224/346/265/213/350/257/225.js +0 -53
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//345/207/275/346/225/260/347/211/271/345/214/226/346/265/213/350/257/225.js +0 -67
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//347/253/257/345/210/260/347/253/257/346/265/213/350/257/225.js +0 -104
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//347/261/273/345/236/213/346/216/250/346/226/255/346/265/213/350/257/225.js +0 -87
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//350/277/220/350/241/214/346/265/213/350/257/225.js +0 -43
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//351/200/203/351/200/270/345/210/206/346/236/220/346/265/213/350/257/225.js +0 -58
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//347/211/271/345/214/226/347/274/223/345/255/230.js +0 -83
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//347/216/257/345/242/203.js +0 -126
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//347/261/273/345/236/213.js +0 -294
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//347/261/273/345/236/213/346/216/250/346/226/255.js +0 -461
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//350/260/203/347/224/250/345/233/276.js +0 -119
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//351/200/203/351/200/270/345/210/206/346/236/220.js +0 -184
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/274/226/350/257/221.sh +0 -45
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/alloc.c +0 -206
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/build.sh +0 -70
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/dict.c +0 -143
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/list.c +0 -148
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/math.c +0 -129
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/print.c +0 -135
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/rt.h +0 -177
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/string.c +0 -288
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/syscall.asm +0 -153
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/test.c +0 -228
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266//344/270/264/346/227/266.c +0 -379
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266.c +0 -441
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//346/267/261/345/272/246/346/257/224/350/276/203.js +0 -51
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/Empty.java +0 -1
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.c +0 -53
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.co +0 -42
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.go +0 -43
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.java +0 -29
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.js +0 -24
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.py +0 -36
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.rs +0 -35
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty.co +0 -0
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_c.c +0 -1
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_go.go +0 -2
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_rs.rs +0 -1
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/213/274/346/216/245.c +0 -32
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/213/274/346/216/245.co +0 -10
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/216/222/345/272/217.c +0 -23
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/216/222/345/272/217.co +0 -31
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/226/220/346/263/242/351/202/243/345/245/221.c +0 -17
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/226/220/346/263/242/351/202/243/345/245/221.co +0 -10
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/226/220/346/263/242/351/202/243/345/245/22120.co +0 -10
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/347/264/257/345/212/240.c +0 -14
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/347/264/257/345/212/240.co +0 -10
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200/345/257/271/346/257/224/347/273/223/346/236/234.txt +0 -164
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206/fib.c +0 -9
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206/fib.co +0 -2
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206/sum.c +0 -10
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206/sum.co +0 -3
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206//350/256/241/346/265/213.sh +0 -32
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206//350/256/241/346/265/213/347/273/223/346/236/234.txt +0 -7
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//350/241/245/346/265/213.sh +0 -45
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//350/256/241/346/265/213.sh +0 -163
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//350/256/241/346/265/213/347/273/223/346/236/234.txt +0 -29
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
// ============================================================
|
|
2
|
-
// analysis.js —— 共享数据流分析(活跃变量、use/def、可达性)
|
|
3
|
-
// ============================================================
|
|
4
|
-
'use strict';
|
|
5
|
-
|
|
6
|
-
const I = require('./ir');
|
|
7
|
-
|
|
8
|
-
// 二元常量折叠
|
|
9
|
-
function foldBinop(op, l, r) {
|
|
10
|
-
switch (op) {
|
|
11
|
-
case 'add': return l + r;
|
|
12
|
-
case 'sub': return l - r;
|
|
13
|
-
case 'mul': return l * r;
|
|
14
|
-
case 'div': return r === 0 ? null : Math.trunc(l / r);
|
|
15
|
-
case 'mod': return r === 0 ? null : l % r;
|
|
16
|
-
case 'lt': return l < r;
|
|
17
|
-
case 'le': return l <= r;
|
|
18
|
-
case 'eq': return l === r;
|
|
19
|
-
case 'ne': return l !== r;
|
|
20
|
-
case 'gt': return l > r;
|
|
21
|
-
case 'ge': return l >= r;
|
|
22
|
-
case 'and': return l && r;
|
|
23
|
-
case 'or': return l || r;
|
|
24
|
-
}
|
|
25
|
-
return null;
|
|
26
|
-
}
|
|
27
|
-
function foldUnop(op, v) {
|
|
28
|
-
switch (op) {
|
|
29
|
-
case 'neg': return -v;
|
|
30
|
-
case 'not': return !v;
|
|
31
|
-
}
|
|
32
|
-
return null;
|
|
33
|
-
}
|
|
34
|
-
// 把 JS 值转成 IR 字面量(布尔 -> 0/1)
|
|
35
|
-
function toLiteral(v) {
|
|
36
|
-
if (typeof v === 'boolean') return v ? 1 : 0;
|
|
37
|
-
return v;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* 活跃变量分析(逆向数据流)。
|
|
42
|
-
* 正确处理 SSA phi:phi 在块 B 的第 k 个实参来自第 k 个前驱 P,
|
|
43
|
-
* 该实参视为 P 的 liveOut,而非 B 的 liveIn。
|
|
44
|
-
*/
|
|
45
|
-
function liveness(fn) {
|
|
46
|
-
const n = fn.blocks.length;
|
|
47
|
-
const liveIn = Array.from({ length: n }, () => new Set());
|
|
48
|
-
const liveOut = Array.from({ length: n }, () => new Set());
|
|
49
|
-
|
|
50
|
-
let changed = true;
|
|
51
|
-
let guard = 0;
|
|
52
|
-
while (changed && guard++ < 200) {
|
|
53
|
-
changed = false;
|
|
54
|
-
for (let b = n - 1; b >= 0; b--) {
|
|
55
|
-
const blk = fn.blocks[b];
|
|
56
|
-
|
|
57
|
-
// liveOut(b) = 对每个后继 s:
|
|
58
|
-
// (liveIn(s) - s 中所有 phi 的 dest) ∪ { phi.args[k] | phi 来自 b->s 这条边 }
|
|
59
|
-
const newOut = new Set();
|
|
60
|
-
for (const s of blk.succs) {
|
|
61
|
-
const succ = fn.blocks[s];
|
|
62
|
-
// 先把 liveIn[s] 去掉 phi 定义的变量
|
|
63
|
-
for (const v of liveIn[s]) {
|
|
64
|
-
let isPhiDef = false;
|
|
65
|
-
for (const instr of succ.instrs) {
|
|
66
|
-
if (instr.op === 'phi' && instr.dest === v) { isPhiDef = true; break; }
|
|
67
|
-
}
|
|
68
|
-
if (!isPhiDef) newOut.add(v);
|
|
69
|
-
}
|
|
70
|
-
// 再把 phi 中来自 b 这条边的实参加进来
|
|
71
|
-
const edgeIdx = succ.preds.indexOf(b);
|
|
72
|
-
if (edgeIdx === -1) continue;
|
|
73
|
-
for (const instr of succ.instrs) {
|
|
74
|
-
if (instr.op !== 'phi') continue;
|
|
75
|
-
const a = instr.args[edgeIdx];
|
|
76
|
-
if (a != null && typeof a === 'string') newOut.add(a);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// liveIn(b) = use(b) ∪ (liveOut(b) - def(b))
|
|
81
|
-
let newIn = new Set([...newOut]);
|
|
82
|
-
for (const v of I.termUse(blk.term)) newIn.add(v);
|
|
83
|
-
for (let i = blk.instrs.length - 1; i >= 0; i--) {
|
|
84
|
-
const instr = blk.instrs[i];
|
|
85
|
-
const d = I.instrDef(instr);
|
|
86
|
-
if (d) newIn.delete(d);
|
|
87
|
-
// phi 的 use 已经在 liveOut 处理过了,不再重复加
|
|
88
|
-
if (instr.op !== 'phi') {
|
|
89
|
-
for (const u of I.instrUse(instr)) newIn.add(u);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
if (b === 0) for (const p of fn.params) newIn.add(p);
|
|
93
|
-
|
|
94
|
-
if (!setEq(newOut, liveOut[b])) { liveOut[b] = newOut; changed = true; }
|
|
95
|
-
if (!setEq(newIn, liveIn[b])) { liveIn[b] = newIn; changed = true; }
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return { liveIn, liveOut };
|
|
99
|
-
}
|
|
100
|
-
function setEq(a, b) { return a.size === b.size && [...a].every(v => b.has(v)); }
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* 可达块分析:从入口 BFS。
|
|
104
|
-
*/
|
|
105
|
-
function reachable(fn) {
|
|
106
|
-
const vis = new Array(fn.blocks.length).fill(false);
|
|
107
|
-
const queue = [0];
|
|
108
|
-
vis[0] = true;
|
|
109
|
-
while (queue.length) {
|
|
110
|
-
const b = queue.shift();
|
|
111
|
-
for (const s of fn.blocks[b].succs) if (!vis[s]) { vis[s] = true; queue.push(s); }
|
|
112
|
-
}
|
|
113
|
-
return vis;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
module.exports = { foldBinop, foldUnop, toLiteral, liveness, reachable };
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
// 基准演示:模拟 coCN 性能基准(斐波那契/循环求和/素数计数)
|
|
2
|
-
// 运行:node benchmark_demo.js
|
|
3
|
-
'use strict';
|
|
4
|
-
const I = require('./ir');
|
|
5
|
-
const { optimizeProgram } = require('./index');
|
|
6
|
-
|
|
7
|
-
// 斐波那契:递归
|
|
8
|
-
function fibFn() {
|
|
9
|
-
const fn = new I.Func('fib', ['n']);
|
|
10
|
-
const b0 = fn.newBlock();
|
|
11
|
-
b0.instrs.push(I.binop('lt','c','n','two'), I.const_('two',2));
|
|
12
|
-
b0.term = I.branch('c', 1, 2);
|
|
13
|
-
const b1 = fn.newBlock(); b1.term = I.ret('n');
|
|
14
|
-
const b2 = fn.newBlock();
|
|
15
|
-
b2.instrs.push(
|
|
16
|
-
I.binop('sub','n1','n','one'), I.const_('one',1),
|
|
17
|
-
I.call('r1','fib',['n1']),
|
|
18
|
-
I.binop('sub','n2','n','two'),
|
|
19
|
-
I.call('r2','fib',['n2']),
|
|
20
|
-
I.binop('add','r3','r1','r2'),
|
|
21
|
-
);
|
|
22
|
-
b2.term = I.ret('r3');
|
|
23
|
-
return fn;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// 循环求和
|
|
27
|
-
function sumFn() {
|
|
28
|
-
const fn = new I.Func('sum', ['n']);
|
|
29
|
-
const b0 = fn.newBlock();
|
|
30
|
-
b0.instrs.push(I.const_('s',0), I.const_('i',0));
|
|
31
|
-
b0.term = I.jump(1);
|
|
32
|
-
const b1 = fn.newBlock();
|
|
33
|
-
b1.instrs.push(I.binop('lt','c','i','n'));
|
|
34
|
-
b1.term = I.branch('c',2,3);
|
|
35
|
-
const b2 = fn.newBlock();
|
|
36
|
-
b2.instrs.push(I.const_('one',1), I.binop('add','s','s','i'), I.binop('add','i','i','one'));
|
|
37
|
-
b2.term = I.jump(1);
|
|
38
|
-
const b3 = fn.newBlock(); b3.term = I.ret('s');
|
|
39
|
-
return fn;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// 素数计数(简化版)
|
|
43
|
-
function primeFn() {
|
|
44
|
-
const fn = new I.Func('primes', ['n']);
|
|
45
|
-
const b0 = fn.newBlock();
|
|
46
|
-
b0.instrs.push(I.const_('count',0), I.const_('i',2));
|
|
47
|
-
b0.term = I.jump(1);
|
|
48
|
-
const b1 = fn.newBlock();
|
|
49
|
-
b1.instrs.push(I.binop('lt','c','i','n'));
|
|
50
|
-
b1.term = I.branch('c',2,4);
|
|
51
|
-
const b2 = fn.newBlock(); // 内层:检查 i 是否素数(简化:i%2==0?)
|
|
52
|
-
b2.instrs.push(I.const_('two',2), I.binop('mod','r','i','two'));
|
|
53
|
-
b2.term = I.branch('r',3,3); // 简化:都走 3
|
|
54
|
-
const b3 = fn.newBlock();
|
|
55
|
-
b3.instrs = [I.const_('one',1), I.binop('add','count','count','one'), I.binop('add','i','i','one')];
|
|
56
|
-
b3.term = I.jump(1);
|
|
57
|
-
const b4 = fn.newBlock(); b4.term = I.ret('count');
|
|
58
|
-
return fn;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
console.log('=== 优化前(指令数统计)===');
|
|
62
|
-
const prog = { functions: [fibFn(), sumFn(), primeFn()] };
|
|
63
|
-
for (const f of prog.functions) {
|
|
64
|
-
const n = f.blocks.reduce((s,b)=>s+b.instrs.length, 0);
|
|
65
|
-
console.log(` ${f.name}: ${n} 条 IR 指令, ${f.blocks.length} 个块`);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
console.log('\n=== 优化后 ===');
|
|
69
|
-
const result = optimizeProgram(prog, {});
|
|
70
|
-
for (const f of result.functions) {
|
|
71
|
-
const n = f.blocks.reduce((s,b)=>s+b.instrs.length, 0);
|
|
72
|
-
const a = result.allocation.get(f.name);
|
|
73
|
-
console.log(` ${f.name}: ${n} 条 IR 指令, ${f.blocks.length} 块, 溢出 ${a.spilled.length} 个变量`);
|
|
74
|
-
}
|
|
75
|
-
console.log('\n基准演示完成。');
|
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
// ============================================================
|
|
2
|
-
// dominance.js —— 支配树与支配边界计算
|
|
3
|
-
//
|
|
4
|
-
// 实现 Cooper-Harvey-Kennedy 的【简单迭代支配者算法】,
|
|
5
|
-
// 以及由支配树推导的【支配边界 (Dominance Frontier)】。
|
|
6
|
-
//
|
|
7
|
-
// 输入:已 buildCFG 的 Func
|
|
8
|
-
// 输出:
|
|
9
|
-
// idom[blockId] = 直接支配者 id
|
|
10
|
-
// domChildren[blockId] = [被支配的子块id]
|
|
11
|
-
// domFrontier[blockId] = Set<blockId>
|
|
12
|
-
// ============================================================
|
|
13
|
-
|
|
14
|
-
'use strict';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 迭代支配者算法(Cooper et al. "A Simple, Fast Dominance Algorithm")
|
|
18
|
-
*
|
|
19
|
-
* 入口块 = 第 0 块。
|
|
20
|
-
* 遍历顺序用逆后序(reverse postorder)收敛更快。
|
|
21
|
-
*/
|
|
22
|
-
function computeDominators(fn) {
|
|
23
|
-
const n = fn.blocks.length;
|
|
24
|
-
if (n === 0) return { idom: {}, domChildren: {}, domFrontier: {} };
|
|
25
|
-
|
|
26
|
-
// --- 1. 计算逆后序 RPO ---
|
|
27
|
-
const rpo = reversePostOrder(fn);
|
|
28
|
-
// 记录每个块在 RPO 中的序号,用于快速比较“谁在前”
|
|
29
|
-
const rpoNum = new Array(n).fill(-1);
|
|
30
|
-
rpo.forEach((b, i) => { rpoNum[b] = i; });
|
|
31
|
-
|
|
32
|
-
// --- 2. 初始化 ---
|
|
33
|
-
const ENTRY = rpo[0]; // RPO 第一个是入口
|
|
34
|
-
const idom = new Array(n).fill(-1);
|
|
35
|
-
idom[ENTRY] = ENTRY; // 入口支配自己
|
|
36
|
-
|
|
37
|
-
const intersect = (b1, b2) => {
|
|
38
|
-
let f1 = b1, f2 = b2;
|
|
39
|
-
// 不断把“更深”(RPO 序号更大)的手指沿支配树向上跳,直到相遇。
|
|
40
|
-
// 入口 idom[ENTRY]=ENTRY,必然收敛。
|
|
41
|
-
while (f1 !== f2) {
|
|
42
|
-
while (rpoNum[f1] > rpoNum[f2]) f1 = idom[f1];
|
|
43
|
-
while (rpoNum[f2] > rpoNum[f1]) f2 = idom[f2];
|
|
44
|
-
}
|
|
45
|
-
return f1;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
// --- 3. 迭代直到不动点 ---
|
|
49
|
-
let changed = true;
|
|
50
|
-
while (changed) {
|
|
51
|
-
changed = false;
|
|
52
|
-
// 按 RPO 顺序遍历(跳过入口)
|
|
53
|
-
for (let i = 1; i < rpo.length; i++) {
|
|
54
|
-
const b = rpo[i];
|
|
55
|
-
// 选第一个已处理的前驱作为新的集合起点
|
|
56
|
-
let newIdom = -1;
|
|
57
|
-
for (const p of fn.blocks[b].preds) {
|
|
58
|
-
if (idom[p] === -1) continue; // 还没处理过
|
|
59
|
-
if (newIdom === -1) { newIdom = p; continue; }
|
|
60
|
-
newIdom = intersect(p, newIdom);
|
|
61
|
-
}
|
|
62
|
-
if (newIdom !== -1 && idom[b] !== newIdom) {
|
|
63
|
-
idom[b] = newIdom;
|
|
64
|
-
changed = true;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// --- 4. 构造支配树子节点表 ---
|
|
70
|
-
const domChildren = Array.from({ length: n }, () => []);
|
|
71
|
-
for (let b = 0; b < n; b++) {
|
|
72
|
-
if (b !== ENTRY && idom[b] !== -1) {
|
|
73
|
-
domChildren[idom[b]].push(b);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// --- 5. 计算支配边界 DF(b) ---
|
|
78
|
-
const domFrontier = Array.from({ length: n }, () => new Set());
|
|
79
|
-
for (let b = 0; b < n; b++) {
|
|
80
|
-
const blk = fn.blocks[b];
|
|
81
|
-
if (blk.preds.length < 2) continue; // 不是合并点,跳过
|
|
82
|
-
for (const p of blk.preds) {
|
|
83
|
-
let runner = p;
|
|
84
|
-
// 沿支配树上跳,直到遇到 b 的直接支配者
|
|
85
|
-
while (runner !== -1 && runner !== idom[b] && idom[runner] !== -1) {
|
|
86
|
-
domFrontier[runner].add(b);
|
|
87
|
-
runner = idom[runner];
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return {
|
|
93
|
-
idom,
|
|
94
|
-
domChildren,
|
|
95
|
-
domFrontier,
|
|
96
|
-
entry: ENTRY,
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* 逆后序遍历(RPO):DFS,先访问后继,再把块压栈。
|
|
102
|
-
* 循环遍历(处理不可达块)。
|
|
103
|
-
*/
|
|
104
|
-
function reversePostOrder(fn) {
|
|
105
|
-
const n = fn.blocks.length;
|
|
106
|
-
const visited = new Array(n).fill(false);
|
|
107
|
-
const order = [];
|
|
108
|
-
|
|
109
|
-
const dfs = (start) => {
|
|
110
|
-
const stack = [[start, 0]]; // [blockId, nextSuccIndex]
|
|
111
|
-
visited[start] = true;
|
|
112
|
-
while (stack.length > 0) {
|
|
113
|
-
const top = stack[stack.length - 1];
|
|
114
|
-
const b = top[0];
|
|
115
|
-
const succs = fn.blocks[b].succs;
|
|
116
|
-
if (top[1] < succs.length) {
|
|
117
|
-
const s = succs[top[1]];
|
|
118
|
-
top[1]++;
|
|
119
|
-
if (!visited[s]) {
|
|
120
|
-
visited[s] = true;
|
|
121
|
-
stack.push([s, 0]);
|
|
122
|
-
}
|
|
123
|
-
} else {
|
|
124
|
-
order.push(b);
|
|
125
|
-
stack.pop();
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
dfs(0);
|
|
131
|
-
// 处理不可达块
|
|
132
|
-
for (let b = 0; b < n; b++) {
|
|
133
|
-
if (!visited[b]) {
|
|
134
|
-
// 不可达块也纳入(它们不会被 phi 影响,但要保证 idom 数组长度一致)
|
|
135
|
-
visited[b] = true;
|
|
136
|
-
dfs(b);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
// order 是后序;逆序 = 逆后序
|
|
140
|
-
order.reverse();
|
|
141
|
-
return order;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* 判断 a 是否支配 b(严格支配,a≠b)
|
|
146
|
-
*/
|
|
147
|
-
function strictlyDominates(idom, a, b) {
|
|
148
|
-
let cur = b;
|
|
149
|
-
let guard = 0;
|
|
150
|
-
while (cur !== -1 && guard++ < 1000) {
|
|
151
|
-
if (cur === a) return a !== b;
|
|
152
|
-
if (idom[cur] === cur) break;
|
|
153
|
-
cur = idom[cur];
|
|
154
|
-
}
|
|
155
|
-
return false;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
module.exports = { computeDominators, reversePostOrder, strictlyDominates };
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
// ============================================================
|
|
2
|
-
// gvn.js —— 全局值编号(Global Value Numbering)
|
|
3
|
-
//
|
|
4
|
-
// 跨块公共子表达式消除:
|
|
5
|
-
// 为每个表达式分配一个全局编号,
|
|
6
|
-
// 同一表达式(交换律等价)在整个函数内只计算一次。
|
|
7
|
-
// 基于支配树:在 y 支配 x 的前提下,x 处的重复计算可替换为 y 的结果。
|
|
8
|
-
// ============================================================
|
|
9
|
-
'use strict';
|
|
10
|
-
|
|
11
|
-
const I = require('./ir');
|
|
12
|
-
const { computeDominators } = require('./dominance');
|
|
13
|
-
const { exprKey } = require('./passes/cse');
|
|
14
|
-
|
|
15
|
-
function globalValueNumbering(fn) {
|
|
16
|
-
const dom = computeDominators(fn);
|
|
17
|
-
// exprKey -> 第一个计算它的 (blockId, instr, resultName)
|
|
18
|
-
const firstComputed = new Map();
|
|
19
|
-
// 块的支配后代链上可见的表达式
|
|
20
|
-
// 简化:DFS 支配树,传递 firstComputed 表
|
|
21
|
-
const dfs = (bid, table) => {
|
|
22
|
-
const blk = fn.blocks[bid];
|
|
23
|
-
const localTable = new Map(table); // 继承父块的可见表
|
|
24
|
-
for (let i = 0; i < blk.instrs.length; i++) {
|
|
25
|
-
const instr = blk.instrs[i];
|
|
26
|
-
if (instr.op === 'phi') continue;
|
|
27
|
-
const key = exprKey(instr);
|
|
28
|
-
if (!key) continue;
|
|
29
|
-
if (localTable.has(key)) {
|
|
30
|
-
// 已有相同表达式,替换为 mov
|
|
31
|
-
const existing = localTable.get(key);
|
|
32
|
-
if (existing !== instr.dest) {
|
|
33
|
-
blk.instrs[i] = I.mov(instr.dest, existing);
|
|
34
|
-
}
|
|
35
|
-
} else {
|
|
36
|
-
localTable.set(key, instr.dest);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
for (const child of dom.domChildren[bid]) dfs(child, localTable);
|
|
40
|
-
};
|
|
41
|
-
dfs(dom.entry, new Map());
|
|
42
|
-
return fn;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
module.exports = { globalValueNumbering };
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
// ============================================================
|
|
2
|
-
// index.js —— 优化器主流水线
|
|
3
|
-
//
|
|
4
|
-
// AST → 三地址码IR → 构建CFG → SSA转换 → 优化passes →
|
|
5
|
-
// SSA销毁 → 寄存器分配 → 指令调度 → 窥孔 → 输出
|
|
6
|
-
//
|
|
7
|
-
// 用法:
|
|
8
|
-
// const opt = require('./index');
|
|
9
|
-
// const result = opt.optimizeProgram(program, options);
|
|
10
|
-
// // program = { functions: [Func-like POJO] }
|
|
11
|
-
// ============================================================
|
|
12
|
-
'use strict';
|
|
13
|
-
|
|
14
|
-
const I = require('./ir');
|
|
15
|
-
const { toSSA, destroySSA } = require('./ssa');
|
|
16
|
-
const constProp = require('./passes/constProp');
|
|
17
|
-
const dce = require('./passes/dce');
|
|
18
|
-
const cse = require('./passes/cse');
|
|
19
|
-
const copyProp = require('./passes/copyProp');
|
|
20
|
-
const loopOpt = require('./passes/loopOpt');
|
|
21
|
-
const inline = require('./passes/inline');
|
|
22
|
-
const escape = require('./passes/escape');
|
|
23
|
-
const devirt = require('./passes/devirt');
|
|
24
|
-
const regalloc = require('./regalloc');
|
|
25
|
-
const scheduler = require('./scheduler');
|
|
26
|
-
const peephole = require('./peephole');
|
|
27
|
-
const gvn = require('./gvn');
|
|
28
|
-
const spill = require('./spill');
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 把 POJO 函数转成 Func 对象(若需要)
|
|
32
|
-
*/
|
|
33
|
-
function normalizeFunc(pojo) {
|
|
34
|
-
if (pojo instanceof I.Func) return pojo;
|
|
35
|
-
const fn = new I.Func(pojo.name, pojo.params || []);
|
|
36
|
-
for (const bb of pojo.blocks) {
|
|
37
|
-
const nb = new I.Block(bb.id);
|
|
38
|
-
nb.instrs = bb.instrs.map(i => ({ ...i, args: i.args ? [...i.args] : undefined }));
|
|
39
|
-
nb.term = { ...bb.term };
|
|
40
|
-
fn.blocks.push(nb);
|
|
41
|
-
}
|
|
42
|
-
I.buildCFG(fn);
|
|
43
|
-
return fn;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* 优化整个程序。
|
|
48
|
-
* @param {{functions:Array}} program
|
|
49
|
-
* @param {{dumpEach?:boolean, regs?:number}} opts
|
|
50
|
-
* @returns {{functions:Func[], allocation:Map, scheduled:Array}}
|
|
51
|
-
*/
|
|
52
|
-
function optimizeProgram(program, opts = {}) {
|
|
53
|
-
const fnPool = new Map();
|
|
54
|
-
const funcs = program.functions.map(normalizeFunc);
|
|
55
|
-
for (const f of funcs) fnPool.set(f.name, f);
|
|
56
|
-
|
|
57
|
-
const dump = opts.dumpEach || false;
|
|
58
|
-
|
|
59
|
-
for (const fn of funcs) {
|
|
60
|
-
if (dump) console.log(`\n=== [原始] ${fn.name} ===\n` + I.funcToString(fn));
|
|
61
|
-
|
|
62
|
-
// 1. CFG + SSA
|
|
63
|
-
I.buildCFG(fn);
|
|
64
|
-
toSSA(fn);
|
|
65
|
-
if (dump) console.log(`\n=== [SSA] ${fn.name} ===\n` + I.funcToString(fn));
|
|
66
|
-
|
|
67
|
-
// 2. 优化 pass 循环(跑 2 轮,让内联后重新优化)
|
|
68
|
-
for (let round = 0; round < 2; round++) {
|
|
69
|
-
constProp.constPropagate(fn);
|
|
70
|
-
dce.eliminateDeadCode(fn);
|
|
71
|
-
gvn.globalValueNumbering(fn);
|
|
72
|
-
cse.eliminateCSE(fn);
|
|
73
|
-
copyProp.propagateCopies(fn);
|
|
74
|
-
dce.eliminateDeadCode(fn);
|
|
75
|
-
loopOpt.optimizeLoops(fn);
|
|
76
|
-
escape.escapeAnalysis(fn);
|
|
77
|
-
devirt.devirtualize(fn, fnPool);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// 3. 函数内联(全局,跑一轮)
|
|
81
|
-
inline.inlineFunctions(fnPool, opts.inlineThreshold || 20);
|
|
82
|
-
// 内联后重跑一遍优化
|
|
83
|
-
for (const f of fnPool.values()) {
|
|
84
|
-
constProp.constPropagate(f);
|
|
85
|
-
dce.eliminateDeadCode(f);
|
|
86
|
-
cse.eliminateCSE(f);
|
|
87
|
-
copyProp.propagateCopies(f);
|
|
88
|
-
dce.eliminateDeadCode(f);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (dump) console.log(`\n=== [优化后] ${fn.name} ===\n` + I.funcToString(fn));
|
|
92
|
-
|
|
93
|
-
// 4. 销毁 SSA(phi -> mov)
|
|
94
|
-
destroySSA(fn);
|
|
95
|
-
if (dump) console.log(`\n=== [SSA销毁] ${fn.name} ===\n` + I.funcToString(fn));
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// 5. 寄存器分配(每个函数独立)
|
|
99
|
-
const allocation = new Map();
|
|
100
|
-
for (const fn of funcs) {
|
|
101
|
-
const allocResult = regalloc.allocate(fn, opts.numIntRegs || 13, opts.numFpRegs || 15);
|
|
102
|
-
// 插入溢出 load/store 代码
|
|
103
|
-
if (allocResult.spilled.length > 0) {
|
|
104
|
-
spill.insertSpillCode(fn, new Set(allocResult.spilled), allocResult.color);
|
|
105
|
-
}
|
|
106
|
-
allocation.set(fn.name, allocResult);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// 6. 指令调度
|
|
110
|
-
const scheduled = [];
|
|
111
|
-
for (const fn of funcs) {
|
|
112
|
-
scheduled.push(scheduler.scheduleFunction(fn, allocation.get(fn.name)));
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// 7. 窥孔优化
|
|
116
|
-
for (const fn of funcs) peephole.optimize(fn);
|
|
117
|
-
|
|
118
|
-
return { functions: funcs, allocation, scheduled };
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
module.exports = { optimizeProgram, normalizeFunc };
|