@yeyelailou/co 0.0.2 → 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.
Files changed (109) hide show
  1. package/README.md +73 -16
  2. package/bin//345/205/245/345/217/243.js +0 -8
  3. package/package.json +7 -6
  4. package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//345/220/257/345/212/250/345/231/250.c +3 -3
  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
  6. 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
  7. 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
  8. 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
  9. package/bin/install.js +0 -276
  10. package//345/267/245/345/205/267/lsp/server.js +0 -419
  11. package//345/267/245/345/205/267/lsp//346/265/213/350/257/225.sh +0 -81
  12. package//345/267/245/345/205/267/vscode-cocn/extension.js +0 -203
  13. package//345/267/245/345/205/267/vscode-cocn/package.json +0 -58
  14. package//345/267/245/345/205/267/vscode-cocn/syntaxes/coCN.tmLanguage.json +0 -130
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  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/spill.js +0 -78
  34. 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
  35. 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
  36. 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
  37. 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
  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//346/265/213/350/257/225/run_all.js +0 -11
  39. 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
  40. 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
  41. 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
  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//346/265/213/350/257/225/test_gvn_spill.js +0 -45
  43. 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
  44. 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
  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_ssa.js +0 -65
  46. 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
  47. 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
  48. 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
  49. 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
  50. 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
  51. 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
  52. 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
  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/206/205/350/201/224/346/265/213/350/257/225.js +0 -53
  54. 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
  55. 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
  56. 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
  57. 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
  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//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
  59. 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
  60. 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
  61. 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
  62. 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
  63. 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
  64. 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
  65. 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
  66. 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
  67. 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
  68. 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
  69. 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
  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/math.c +0 -129
  71. 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
  72. 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
  73. 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
  74. 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
  75. 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
  76. 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
  77. 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
  78. 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
  79. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/Empty.java +0 -1
  80. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.c +0 -53
  81. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.co +0 -42
  82. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.go +0 -43
  83. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.java +0 -29
  84. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.js +0 -24
  85. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.py +0 -36
  86. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.rs +0 -35
  87. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty.co +0 -0
  88. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_c.c +0 -1
  89. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_go.go +0 -2
  90. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_rs.rs +0 -1
  91. 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
  92. 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
  93. 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
  94. 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
  95. 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
  96. 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
  97. 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
  98. 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
  99. 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
  100. 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
  101. 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
  102. 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
  103. 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
  104. 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
  105. 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
  106. 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
  107. 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
  108. 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
  109. 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,78 +0,0 @@
1
- // ============================================================
2
- // spill.js —— 溢出代码插入
3
- //
4
- // 寄存器分配后,对每个溢出变量:
5
- // - 在它的定义点插入 store 到栈槽
6
- // - 在它的每个 use 点插入 load 从栈槽
7
- // ============================================================
8
- 'use strict';
9
-
10
- const I = require('./ir');
11
-
12
- /**
13
- * 为 spilled 变量插入 load/store。
14
- * @param {Func} fn
15
- * @param {Set<string>} spilled
16
- * @param {Map<string,string>} color - 已分配的寄存器映射
17
- * @returns {Map<string,number>} 栈槽偏移表
18
- */
19
- function insertSpillCode(fn, spilled, color) {
20
- const stackSlots = new Map(); // var -> 栈偏移
21
- let nextOffset = 0;
22
- for (const v of spilled) {
23
- stackSlots.set(v, nextOffset);
24
- nextOffset += 8; // 每个槽 8 字节
25
- }
26
-
27
- for (const blk of fn.blocks) {
28
- const newInstrs = [];
29
- for (const instr of blk.instrs) {
30
- const d = I.instrDef(instr);
31
- // use 点:先把 spilled 变量 load 出来到临时寄存器
32
- const uses = I.instrUse(instr);
33
- for (const u of uses) {
34
- if (spilled.has(u)) {
35
- const tmp = `spillload_${u}`;
36
- newInstrs.push({ op: 'load', dest: tmp, base: 'rbp', offset: stackSlots.get(u) });
37
- // 替换 instr 里的 u
38
- replaceUse(instr, u, tmp);
39
- }
40
- }
41
- newInstrs.push(instr);
42
- // def 点:存回栈槽
43
- if (d && spilled.has(d)) {
44
- newInstrs.push({ op: 'store', base: 'rbp', offset: stackSlots.get(d), src: d });
45
- }
46
- }
47
- blk.instrs = newInstrs;
48
- // 终结符的 use 也处理
49
- if (blk.term.kind === 'branch' && spilled.has(blk.term.cond)) {
50
- const tmp = `spillload_${blk.term.cond}`;
51
- blk.instrs.push({ op: 'load', dest: tmp, base: 'rbp', offset: stackSlots.get(blk.term.cond) });
52
- blk.term.cond = tmp;
53
- }
54
- }
55
- fn.stackFrameSize = nextOffset;
56
- return stackSlots;
57
- }
58
-
59
- function replaceUse(instr, oldName, newName) {
60
- switch (instr.op) {
61
- case 'mov': case 'neg': case 'not':
62
- if (instr.src === oldName) instr.src = newName; break;
63
- case 'call':
64
- instr.args = instr.args.map(a => a === oldName ? newName : a); break;
65
- case 'load':
66
- if (instr.base === oldName) instr.base = newName; break;
67
- case 'store':
68
- if (instr.base === oldName) instr.base = newName;
69
- if (instr.src === oldName) instr.src = newName; break;
70
- case 'phi':
71
- instr.args = instr.args.map(a => a === oldName ? newName : a); break;
72
- default:
73
- if (instr.left === oldName) instr.left = newName;
74
- if (instr.right === oldName) instr.right = newName;
75
- }
76
- }
77
-
78
- module.exports = { insertSpillCode };
@@ -1,235 +0,0 @@
1
- // ============================================================
2
- // ssa.js —— 静态单赋值 (SSA) 形式转换
3
- //
4
- // 流程:
5
- // 1. 收集每个原始变量在哪些块被【定义】
6
- // 2. 标准 phi 插入算法:
7
- // - 对每个变量 v,把所有定义块放进 worklist
8
- // - 沿支配树向上,在支配边界 DF 中插入 phi(若尚未插入)
9
- // - 插入 phi 的块成为新的“定义点”,继续向上传播
10
- // 3. 重命名:DFS 支配树,每个变量维护版本栈,
11
- // 每次定义弹出栈顶生成新版本,use 处取栈顶。
12
- //
13
- // SSA 销毁(destruction):
14
- // 优化结束后,把 phi 的参数变成前驱块末尾的 mov。
15
- // ============================================================
16
-
17
- 'use strict';
18
-
19
- const I = require('./ir');
20
- const { computeDominators } = require('./dominance');
21
-
22
- /**
23
- * 把函数转为 SSA 形式(原地修改)。
24
- * 前置:已经 buildCFG。
25
- */
26
- function toSSA(fn) {
27
- const dom = computeDominators(fn);
28
- const { idom, domFrontier } = dom;
29
- const n = fn.blocks.length;
30
-
31
- // ---- 1. 收集每个变量被哪些块定义 ----
32
- // defBlock[v] = Set of block ids that define v
33
- const defBlock = new Map();
34
- const recordDef = (v, bid) => {
35
- if (!defBlock.has(v)) defBlock.set(v, new Set());
36
- defBlock.get(v).add(bid);
37
- };
38
- // 函数参数视为在入口块定义
39
- for (const p of fn.params) recordDef(p, dom.entry);
40
- for (const b of fn.blocks) {
41
- for (const instr of b.instrs) {
42
- const d = I.instrDef(instr);
43
- if (d) recordDef(d, b.id);
44
- }
45
- }
46
-
47
- // ---- 2. phi 插入 ----
48
- // phiAlready[b] = Set of variables that already have a phi in block b
49
- const phiAlready = Array.from({ length: n }, () => new Set());
50
- // phiInserted[b] = Set of vars that we newly discovered need processing
51
- const phiInserted = Array.from({ length: n }, () => new Set());
52
-
53
- for (const [v, defBlocks] of defBlock) {
54
- // worklist = 该变量的所有定义块
55
- const worklist = [...defBlocks];
56
- while (worklist.length > 0) {
57
- const x = worklist.pop();
58
- // 遍历 x 的支配边界
59
- for (const y of domFrontier[x]) {
60
- if (!phiAlready[y].has(v)) {
61
- // 在块 y 插入 phi: v = phi(v, v, ...)
62
- const ph = I.phi(v, [], []);
63
- fn.blocks[y].instrs.unshift(ph); // phi 必须在块首
64
- phiAlready[y].add(v);
65
- // 块 y 现在定义了 v,如果它之前没定义过,加入 worklist
66
- if (!defBlock.get(v).has(y)) {
67
- defBlock.get(v).add(y);
68
- worklist.push(y);
69
- }
70
- }
71
- }
72
- }
73
- }
74
-
75
- // ---- 3. 重命名 ----
76
- // 每个变量的版本计数
77
- const varCount = new Map();
78
- // 每个变量的版本栈(栈顶 = 当前可见版本)
79
- const varStack = new Map();
80
- // 记录所有定义点 -> 新版本号,供后续 pass 使用
81
- const versions = new Map(); // 原名 -> 当前版本号
82
-
83
- const pushVersion = (v) => {
84
- const cnt = (varCount.get(v) || 0) + 1;
85
- varCount.set(v, cnt);
86
- const newName = `${v}#${cnt}`;
87
- if (!varStack.has(v)) varStack.set(v, []);
88
- varStack.get(v).push(newName);
89
- return newName;
90
- };
91
- const peekVersion = (v) => {
92
- const s = varStack.get(v);
93
- if (!s || s.length === 0) return v; // 未定义的(全局/参数外部)保持原名
94
- return s[s.length - 1];
95
- };
96
- const popVersion = (v) => {
97
- const s = varStack.get(v);
98
- if (s && s.length > 0) s.pop();
99
- };
100
-
101
- // 参数在入口块先压栈
102
- for (const p of fn.params) pushVersion(p);
103
-
104
- // 对 phi 里记录的 blocks/args,重命名后回填
105
- const renameBlock = (bid) => {
106
- const b = fn.blocks[bid];
107
- const defsThisBlock = []; // 本块新定义的变量名,退出时 pop
108
-
109
- // 先处理本块的 phi:它们定义的是新版本
110
- for (const instr of b.instrs) {
111
- if (instr.op !== 'phi') continue;
112
- const v = instr.dest; // 注意:phi 的 dest 现在还是原名
113
- const newName = pushVersion(v);
114
- instr.dest = newName;
115
- defsThisBlock.push(v);
116
- }
117
-
118
- // 处理普通指令:先重命名 use,再重命名 dest
119
- for (const instr of b.instrs) {
120
- if (instr.op === 'phi') continue;
121
- renameUses(instr, peekVersion);
122
- const d = I.instrDef(instr);
123
- if (d) {
124
- const newName = pushVersion(d);
125
- instr.dest = newName;
126
- defsThisBlock.push(d);
127
- }
128
- }
129
-
130
- // 重命名终结符的 use
131
- renameTermUses(b.term, peekVersion);
132
-
133
- // 把当前版本信息填进每个后继块的 phi
134
- for (const s of b.succs) {
135
- const succ = fn.blocks[s];
136
- for (const instr of succ.instrs) {
137
- if (instr.op !== 'phi') continue;
138
- // phi 的 args 对应各个前驱;我们要找到 b 在 phi.blocks 中的位置
139
- // 注意:此时 phi.blocks 还是空的,需要动态维护。
140
- // 约定:按【前驱顺序】填。我们在 succ.preds 中找 b 的索引。
141
- const predIdx = succ.preds.indexOf(bid);
142
- if (predIdx === -1) continue;
143
- // phi 对应的变量原名 = instr.dest 去掉 #n 后缀
144
- const orig = stripSSA(instr.dest);
145
- const cur = peekVersion(orig);
146
- // 保证 args/blocks 数组长度
147
- while (instr.blocks.length <= predIdx) { instr.blocks.push(-1); instr.args.push(null); }
148
- instr.blocks[predIdx] = bid;
149
- instr.args[predIdx] = cur;
150
- }
151
- }
152
-
153
- // 递归支配树子节点
154
- for (const child of dom.domChildren[bid]) {
155
- renameBlock(child);
156
- }
157
-
158
- // 退出:pop 本块定义的版本
159
- for (const v of defsThisBlock) popVersion(v);
160
- };
161
-
162
- renameBlock(dom.entry);
163
-
164
- fn._ssa = true;
165
- fn._dom = dom;
166
- return fn;
167
- }
168
-
169
- // 重命名一条普通指令的所有 use
170
- function renameUses(instr, peek) {
171
- switch (instr.op) {
172
- case 'mov':
173
- case 'neg':
174
- case 'not':
175
- instr.src = peek(instr.src); break;
176
- case 'call':
177
- instr.args = instr.args.map(peek); break;
178
- case 'load':
179
- instr.base = peek(instr.base); break;
180
- case 'store':
181
- instr.base = peek(instr.base); instr.src = peek(instr.src); break;
182
- case 'phi':
183
- break;
184
- default:
185
- instr.left = peek(instr.left);
186
- instr.right = peek(instr.right);
187
- }
188
- }
189
- function renameTermUses(term, peek) {
190
- if (term.kind === 'branch') term.cond = peek(term.cond);
191
- else if (term.kind === 'return' && term.value != null) term.value = peek(term.value);
192
- }
193
-
194
- // 去掉 SSA 版本后缀: "x#3" -> "x"
195
- function stripSSA(name) {
196
- if (typeof name !== 'string') return name;
197
- const idx = name.lastIndexOf('#');
198
- // 只在 # 后面全是数字时才剥离
199
- if (idx > 0 && /^\d+$/.test(name.slice(idx + 1))) return name.slice(0, idx);
200
- return name;
201
- }
202
-
203
- /**
204
- * SSA 销毁:把每个块首的 phi 指令展开为前驱块末尾的 mov。
205
- *
206
- * 对 phi: x = phi(a from P1, b from P2):
207
- * 在 P1 末尾(终结符前)插入 mov x, a
208
- * 在 P2 末尾(终结符前)插入 mov x, b
209
- * 然后删除所有 phi。
210
- *
211
- * 注意:这是寄存器分配前的最后一步。
212
- */
213
- function destroySSA(fn) {
214
- for (const b of fn.blocks) {
215
- const phis = b.instrs.filter(i => i.op === 'phi');
216
- if (phis.length === 0) continue;
217
-
218
- for (const ph of phis) {
219
- for (let k = 0; k < ph.blocks.length; k++) {
220
- const predId = ph.blocks[k];
221
- const arg = ph.args[k];
222
- if (predId == null || arg == null) continue;
223
- const pred = fn.blocks[predId];
224
- // 在 pred 的终结符前插入 mov
225
- pred.instrs.push(I.mov(ph.dest, arg));
226
- }
227
- }
228
- // 删除本块的 phi
229
- b.instrs = b.instrs.filter(i => i.op !== 'phi');
230
- }
231
- fn._ssa = false;
232
- return fn;
233
- }
234
-
235
- module.exports = { toSSA, destroySSA, stripSSA, computeDominators };
@@ -1,47 +0,0 @@
1
- // ============================================================
2
- // traceSched.js —— Trace 调度(跨基本块调度,可选)
3
- //
4
- // 把最频繁执行的路径(trace)连成一条直线,跨块调度。
5
- // 这里只做 trace 选择,实际指令重排由后端完成。
6
- // ============================================================
7
- 'use strict';
8
-
9
- const I = require('./ir');
10
-
11
- /**
12
- * 从入口开始,按分支概率(启发式:then 分支更可能走)选 trace。
13
- * 返回基本块 id 列表,代表一条热路径。
14
- */
15
- function selectTrace(fn) {
16
- const trace = [];
17
- let cur = 0;
18
- const visited = new Set();
19
- while (cur != null && !visited.has(cur)) {
20
- visited.add(cur);
21
- trace.push(cur);
22
- const t = fn.blocks[cur].term;
23
- if (t.kind === 'jump') cur = t.target;
24
- else if (t.kind === 'branch') cur = t.then; // 启发式:默认走 then
25
- else cur = null;
26
- }
27
- return trace;
28
- }
29
-
30
- /**
31
- * 对 trace 上的块做跨块指令移动提示:
32
- * 把 trace 上每个块的第一条指令标记 traceHot,供后端调度参考。
33
- */
34
- function annotateTrace(fn) {
35
- const trace = selectTrace(fn);
36
- const traceSet = new Set(trace);
37
- for (const bid of trace) {
38
- const blk = fn.blocks[bid];
39
- for (let i = 0; i < Math.min(2, blk.instrs.length); i++) {
40
- blk.instrs[i].traceHot = true;
41
- }
42
- }
43
- fn._trace = trace;
44
- return trace;
45
- }
46
-
47
- module.exports = { selectTrace, annotateTrace };
@@ -1,46 +0,0 @@
1
- // ============================================================
2
- // unroll.js —— 循环展开(真实实现)
3
- //
4
- // 对计数循环 for i = 0 to n step 1,展开 factor 次:
5
- // 原循环体执行一次 i++,展开后执行 factor 次,i += factor。
6
- // 减少分支判断开销,给指令调度更多空间。
7
- // ============================================================
8
- 'use strict';
9
-
10
- const I = require('./ir');
11
-
12
- /**
13
- * 尝试展开指定循环。
14
- * @param {Func} fn
15
- * @param {{header:number, tail:number, body:Set}} loop
16
- * @param {number} factor
17
- * @returns {boolean} 是否成功展开
18
- */
19
- function tryUnroll(fn, loop, factor = 2) {
20
- const { header, tail, body } = loop;
21
- if (body.size > 4) return false; // 只展开小块
22
-
23
- // 找 phi 归纳变量
24
- const hb = fn.blocks[header];
25
- const ivPhi = hb.instrs.find(i => i.op === 'phi');
26
- if (!ivPhi) return false;
27
-
28
- // 找步长
29
- let stepName = null;
30
- for (const bid of body) for (const instr of fn.blocks[bid].instrs) {
31
- if (instr.op === 'add' && (instr.left === ivPhi.dest || instr.right === ivPhi.dest)) {
32
- stepName = instr.left === ivPhi.dest ? instr.right : instr.left;
33
- }
34
- }
35
- if (!stepName) return false;
36
-
37
- // 简化:标记 loop.unrolled = true,后端代码生成器看到此标记
38
- // 即可按 factor 展开。这里不改 IR,避免破坏 SSA 结构。
39
- loop.unrolled = true;
40
- loop.unrollFactor = factor;
41
- loop.inductionVar = ivPhi.dest;
42
- loop.step = stepName;
43
- return true;
44
- }
45
-
46
- module.exports = { tryUnroll };
@@ -1,33 +0,0 @@
1
- // ============================================================
2
- // test/run.js —— 简易测试框架(node:test 风格但零依赖)
3
- // ============================================================
4
- 'use strict';
5
-
6
- let passed = 0, failed = 0;
7
- const failures = [];
8
-
9
- function test(name, fn) {
10
- try {
11
- fn();
12
- passed++;
13
- console.log(` ✓ ${name}`);
14
- } catch (e) {
15
- failed++;
16
- failures.push({ name, err: e });
17
- console.log(` ✗ ${name}`);
18
- console.log(` ${e.message}`);
19
- }
20
- }
21
- function assert(cond, msg) { if (!cond) throw new Error(msg || 'assertion failed'); }
22
- function eq(a, b, msg) { if (a !== b) throw new Error(msg || `expected ${b}, got ${a}`); }
23
-
24
- function summary() {
25
- console.log(`\n 通过 ${passed} / ${passed + failed}`);
26
- if (failed > 0) {
27
- console.log(` ${failed} 个失败:`);
28
- for (const f of failures) console.log(` - ${f.name}: ${f.err.message}`);
29
- process.exit(1);
30
- }
31
- }
32
-
33
- module.exports = { test, assert, eq, summary };
@@ -1,11 +0,0 @@
1
- // 测试入口:node run_all.js
2
- 'use strict';
3
- require('./test_ssa');
4
- require('./test_constfold');
5
- require('./test_dce');
6
- require('./test_loop');
7
- require('./test_regalloc');
8
- require('./test_e2e');
9
- require('./test_gvn_spill');
10
- const { summary } = require('./harness');
11
- summary();
@@ -1,58 +0,0 @@
1
- // 常量折叠测试:1+2*3 -> 7
2
- 'use strict';
3
- const I = require('../ir');
4
- const { constPropagate } = require('../passes/constProp');
5
- const { test, assert, eq } = require('./harness');
6
-
7
- test('常量折叠: 1+2*3 -> 7', () => {
8
- const fn = new I.Func('calc', []);
9
- const b0 = fn.newBlock();
10
- // t1 = 1; t2 = 2; t3 = 3; t4 = t2*t3; t5 = t1+t4
11
- b0.instrs.push(
12
- I.const_('a', 1),
13
- I.const_('b', 2),
14
- I.const_('c', 3),
15
- I.binop('mul', 'd', 'b', 'c'),
16
- I.binop('add', 'e', 'a', 'd'),
17
- );
18
- b0.term = I.ret('e');
19
- I.buildCFG(fn);
20
- constPropagate(fn);
21
- // e 应该被折叠成 const 7
22
- const eInstr = b0.instrs.find(i => i.dest === 'e' || (i.op === 'const' && false));
23
- // 折叠后 d 和 e 都应变成 const
24
- const allConst = b0.instrs.filter(i => i.op === 'const');
25
- const eConst = allConst.find(i => i.dest === 'e');
26
- assert(eConst, 'e 应被折叠为常量');
27
- eq(eConst.value, 7, '1+2*3 应 = 7');
28
- });
29
-
30
- test('常量折叠: 比较常量条件分支', () => {
31
- const fn = new I.Func('br', []);
32
- const b0 = fn.newBlock();
33
- b0.instrs.push(
34
- I.const_('x', 5),
35
- I.const_('ten', 10),
36
- I.binop('lt','c','x','ten'),
37
- );
38
- b0.term = I.branch('c', 1, 2);
39
- const b1 = fn.newBlock(); b1.term = I.ret('x');
40
- const b2 = fn.newBlock(); b2.term = I.ret('x');
41
- I.buildCFG(fn);
42
- constPropagate(fn);
43
- // c = 5 < 10 = true,branch 应变成 jump 到 L1
44
- eq(b0.term.kind, 'jump', '常量真分支应消解为 jump');
45
- eq(b0.term.target, 1, '应跳转到 L1');
46
- });
47
-
48
- test('常量折叠: 布尔 not', () => {
49
- const fn = new I.Func('not', []);
50
- const b0 = fn.newBlock();
51
- b0.instrs.push(I.const_('x', true), I.unop('not','y','x'));
52
- b0.term = I.ret('y');
53
- I.buildCFG(fn);
54
- constPropagate(fn);
55
- const y = b0.instrs.find(i => i.dest === 'y');
56
- eq(y.op, 'const', 'not true 应折叠');
57
- eq(y.value, 0, 'not true = false = 0');
58
- });
@@ -1,49 +0,0 @@
1
- // 死代码消除测试
2
- 'use strict';
3
- const I = require('../ir');
4
- const { eliminateDeadCode } = require('../passes/dce');
5
- const { test, assert, eq } = require('./harness');
6
-
7
- test('DCE-1: 未使用的赋值被删除', () => {
8
- const fn = new I.Func('dead', []);
9
- const b0 = fn.newBlock();
10
- b0.instrs.push(
11
- I.const_('a', 1),
12
- I.const_('b', 2), // b 从未被使用
13
- I.binop('add','c','a','a'),
14
- );
15
- b0.term = I.ret('c');
16
- I.buildCFG(fn);
17
- eliminateDeadCode(fn);
18
- const hasB = b0.instrs.some(i => i.dest === 'b');
19
- assert(!hasB, '未使用的 b=2 应被删除');
20
- const hasC = b0.instrs.some(i => i.dest === 'c');
21
- assert(hasC, 'c 被返回,应保留');
22
- });
23
-
24
- test('DCE-2: 有副作用的 call 不删除', () => {
25
- const fn = new I.Func('side', []);
26
- const b0 = fn.newBlock();
27
- b0.instrs.push(
28
- I.call('r', '__printf', ['"hello"']), // call 有副作用
29
- I.const_('a', 1),
30
- );
31
- b0.term = I.ret('a');
32
- I.buildCFG(fn);
33
- eliminateDeadCode(fn);
34
- const hasCall = b0.instrs.some(i => i.op === 'call');
35
- assert(hasCall, '有副作用的 call 应保留');
36
- });
37
-
38
- test('DCE-3: 不可达块被清空', () => {
39
- const fn = new I.Func('unreach', []);
40
- const b0 = fn.newBlock(); b0.term = I.ret(I.const_('x',0) || 'x');
41
- fn.blocks[0].instrs = [I.const_('x',0)];
42
- fn.blocks[0].term = I.ret('x');
43
- const b1 = fn.newBlock(); // 不可达
44
- b1.instrs.push(I.const_('dead', 999));
45
- b1.term = I.ret('dead');
46
- I.buildCFG(fn);
47
- eliminateDeadCode(fn);
48
- eq(b1.instrs.length, 0, '不可达块的指令应清空');
49
- });
@@ -1,57 +0,0 @@
1
- // 端到端测试:完整优化流水线
2
- 'use strict';
3
- const I = require('../ir');
4
- const { optimizeProgram } = require('../index');
5
- const { test, assert, eq } = require('./harness');
6
-
7
- test('E2E-1: 完整流水线跑通循环求和', () => {
8
- const fn = new I.Func('sum', ['n']);
9
- const b0 = fn.newBlock();
10
- b0.instrs.push(I.const_('s',0), I.const_('i',0));
11
- b0.term = I.jump(1);
12
- const b1 = fn.newBlock();
13
- b1.instrs.push(I.binop('lt','c','i','n'));
14
- b1.term = I.branch('c',2,3);
15
- const b2 = fn.newBlock();
16
- b2.instrs.push(I.const_('one',1), I.binop('add','s','s','i'), I.binop('add','i','i','one'));
17
- b2.term = I.jump(1);
18
- const b3 = fn.newBlock(); b3.term = I.ret('s');
19
-
20
- const res = optimizeProgram({functions:[fn]}, {});
21
- assert(res.functions.length === 1, '应输出 1 个函数');
22
- assert(res.allocation.has('sum'), '应有寄存器分配结果');
23
- assert(res.scheduled.length === 1, '应输出调度结果');
24
- });
25
-
26
- test('E2E-2: 多函数 + 内联', () => {
27
- // add(a,b) = a+b; caller() = add(3,4)
28
- const callee = new I.Func('add', ['a','b']);
29
- const cb0 = callee.newBlock();
30
- cb0.instrs.push(I.binop('add','r','a','b'));
31
- cb0.term = I.ret('r');
32
-
33
- const caller = new I.Func('caller', []);
34
- const db0 = caller.newBlock();
35
- db0.instrs.push(I.const_('x',3), I.const_('y',4), I.call('z','add',['x','y']));
36
- db0.term = I.ret('z');
37
-
38
- const res = optimizeProgram({functions:[callee, caller]}, {});
39
- // caller 内联后应该不再有 call 指令(因为 add 很小)
40
- const callerFn = res.functions.find(f => f.name === 'caller');
41
- let hasCall = false;
42
- for (const b of callerFn.blocks) for (const i of b.instrs) if (i.op === 'call') hasCall = true;
43
- assert(!hasCall, '小函数 add 应被内联,caller 内不应再有 call');
44
- });
45
-
46
- test('E2E-3: 窥孔优化消除 mov x,x', () => {
47
- const fn = new I.Func('peep', ['a']);
48
- const b0 = fn.newBlock();
49
- b0.instrs.push(I.mov('b','a'), I.mov('c','c')); // mov c,c 冗余
50
- b0.term = I.ret('c');
51
- I.buildCFG(fn);
52
- // 直接跑 peephole
53
- const peephole = require('../peephole');
54
- peephole.optimize(fn);
55
- const hasSelfMov = b0.instrs.some(i => i.op === 'mov' && i.dest === i.src);
56
- assert(!hasSelfMov, 'mov c,c 应被窥孔删除');
57
- });
@@ -1,45 +0,0 @@
1
- // GVN 与溢出代码测试
2
- 'use strict';
3
- const I = require('../ir');
4
- const { globalValueNumbering } = require('../gvn');
5
- const { insertSpillCode } = require('../spill');
6
- const { test, assert, eq } = require('./harness');
7
-
8
- test('GVN: 跨块公共子表达式消除', () => {
9
- // L0: a = 1; b = a+2; jump L1
10
- // L1: c = a+2; ret c (c = b 重复计算)
11
- const fn = new I.Func('gvn', []);
12
- const b0 = fn.newBlock();
13
- b0.instrs.push(
14
- I.const_('a', 1),
15
- I.const_('two', 2),
16
- I.binop('add','b','a','two'),
17
- );
18
- b0.term = I.jump(1);
19
- const b1 = fn.newBlock();
20
- b1.instrs.push(I.binop('add','c','a','two'));
21
- b1.term = I.ret('c');
22
- I.buildCFG(fn);
23
- globalValueNumbering(fn);
24
- // c 应被替换为 mov c, b
25
- const cInstr = b1.instrs.find(i => i.dest === 'c');
26
- eq(cInstr.op, 'mov', 'GVN 应把重复的 a+2 替换为 mov');
27
- eq(cInstr.src, 'b', '应引用 L0 已算好的 b');
28
- });
29
-
30
- test('溢出代码: spilled 变量插入 load/store', () => {
31
- const fn = new I.Func('spill', ['x']);
32
- const b0 = fn.newBlock();
33
- b0.instrs.push(
34
- I.binop('add','y','x','x'),
35
- I.binop('add','z','y','y'),
36
- );
37
- b0.term = I.ret('z');
38
- I.buildCFG(fn);
39
- const spilled = new Set(['x']);
40
- insertSpillCode(fn, spilled, new Map());
41
- // 应有 load 指令从栈槽加载 x
42
- const hasLoad = b0.instrs.some(i => i.op === 'load');
43
- assert(hasLoad, 'spilled 变量 x 的 use 点应插入 load');
44
- assert(fn.stackFrameSize > 0, '应分配栈帧空间');
45
- });