@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,237 +0,0 @@
1
- // ============================================================
2
- // ir.js —— 三地址码 IR 的数据结构与工具函数
3
- //
4
- // 这是优化器与代码生成器之间的【契约】:
5
- // 输入:AST 后端产出的三地址码 IR(本格式)
6
- // 输出:优化后的 IR + 寄存器分配结果 + 调度后的指令序列
7
- //
8
- // 设计:
9
- // Program = { functions: [Function] }
10
- // Function = { name, params:[str], blocks:[Block] }
11
- // Block = { id, instrs:[Instr], term:Term, preds:[id], succs:[id] }
12
- // Instr = 三地址指令(见下)
13
- // Term = {kind:'jump',target} | {kind:'branch',cond,then,else}
14
- // | {kind:'return',value} | {kind:'unreachable'}
15
- //
16
- // 指令形式(三地址):
17
- // {op:'const', dest, value} // dest = 字面量
18
- // {op:'mov', dest, src} // dest = src
19
- // {op:'add'|'sub'|'mul'|'div'|'mod'|
20
- // 'lt'|'le'|'eq'|'ne'|'gt'|'ge'|
21
- // 'and'|'or', dest, left, right} // dest = left OP right
22
- // {op:'neg'|'not', dest, src} // 一元
23
- // {op:'call', dest, name, args:[...]} // dest = name(args)
24
- // {op:'load', dest, base, offset} // dest = *(base+offset)
25
- // {op:'store', base, offset, src} // *(base+offset) = src
26
- // {op:'phi', dest, blocks:[...], args:[...]} // SSA phi
27
- //
28
- // 操作数约定:
29
- // - 所有变量/临时量都是【名字】字符串(如 "t1"、"x"、"x#2")。
30
- // - 常量只出现在 const 指令的 value 字段;指令里 left/right/src 都是名字。
31
- // - SSA 版本号通过名字后缀 "#n" 表示(如 "x#3"),由 ssa.js 重命名产生。
32
- // ============================================================
33
-
34
- 'use strict';
35
-
36
- // ---------- 指令构造器(便于手写测试 IR) ----------
37
- const const_ = (dest, value) => ({ op: 'const', dest, value });
38
- const mov = (dest, src) => ({ op: 'mov', dest, src });
39
- const binop = (op, dest, left, right) => ({ op, dest, left, right });
40
- const unop = (op, dest, src) => ({ op, dest, src });
41
- const call = (dest, name, args = []) => ({ op: 'call', dest, name, args });
42
- const phi = (dest, blocks, args) => ({ op: 'phi', dest, blocks: [...blocks], args: [...args] });
43
- const unbox = (dest, src, kind = 'int') => ({ op: 'unbox', dest, src, kind });
44
- const itof = (dest, src) => ({ op: 'itof', dest, src }); // int -> double 转换
45
-
46
- // ---------- 终结符 ----------
47
- const jump = (target) => ({ kind: 'jump', target });
48
- const branch = (cond, thenBlock, elseBlock) => ({ kind: 'branch', cond, then: thenBlock, else: elseBlock });
49
- const ret = (value) => ({ kind: 'return', value });
50
- const unreachable = () => ({ kind: 'unreachable' });
51
-
52
- // ---------- 基本块 ----------
53
- class Block {
54
- constructor(id) {
55
- this.id = id;
56
- this.instrs = []; // 非终结指令
57
- this.term = unreachable();
58
- this.preds = []; // 前驱块 id 列表
59
- this.succs = []; // 后继块 id 列表
60
- }
61
- }
62
-
63
- // ---------- 函数 ----------
64
- class Func {
65
- constructor(name, params = []) {
66
- this.name = name;
67
- this.params = [...params];
68
- this.blocks = [];
69
- }
70
- addBlock(b) { this.blocks.push(b); return b; }
71
- newBlock() { const b = new Block(this.blocks.length); this.blocks.push(b); return b; }
72
- block(id) { return this.blocks[id]; }
73
- }
74
-
75
- // ============================================================
76
- // CFG 构建:根据 term 填充 preds / succs
77
- // ============================================================
78
- function buildCFG(fn) {
79
- // 清掉旧数据
80
- for (const b of fn.blocks) { b.preds = []; b.succs = []; }
81
- for (const b of fn.blocks) {
82
- const t = b.term;
83
- if (t.kind === 'jump') {
84
- b.succs.push(t.target);
85
- } else if (t.kind === 'branch') {
86
- b.succs.push(t.then, t.else);
87
- } else if (t.kind === 'return' || t.kind === 'unreachable') {
88
- // 无后继
89
- } else {
90
- throw new Error(`未知终结符: ${t.kind}`);
91
- }
92
- }
93
- // 反向填 preds
94
- for (const b of fn.blocks) {
95
- for (const s of b.succs) {
96
- fn.blocks[s].preds.push(b.id);
97
- }
98
- }
99
- return fn;
100
- }
101
-
102
- // ============================================================
103
- // 指令操作数工具
104
- // ============================================================
105
-
106
- // 一条指令读取的所有变量(use 集合)
107
- function instrUse(instr) {
108
- const uses = [];
109
- switch (instr.op) {
110
- case 'const':
111
- case 'phi':
112
- break;
113
- case 'mov':
114
- case 'neg':
115
- case 'not':
116
- uses.push(instr.src);
117
- break;
118
- case 'call':
119
- for (const a of instr.args) uses.push(a);
120
- break;
121
- case 'unbox':
122
- uses.push(instr.src);
123
- break;
124
- case 'itof':
125
- uses.push(instr.src);
126
- break;
127
- case 'load':
128
- uses.push(instr.base);
129
- break;
130
- case 'store':
131
- uses.push(instr.base, instr.src);
132
- break;
133
- default: // 二元运算
134
- uses.push(instr.left, instr.right);
135
- }
136
- return uses;
137
- }
138
-
139
- // 一条指令定义(写入)的变量
140
- function instrDef(instr) {
141
- if (instr.op === 'store') return null;
142
- return instr.dest || null;
143
- }
144
-
145
- // 终结符读取的变量
146
- function termUse(term) {
147
- switch (term.kind) {
148
- case 'branch': return [term.cond];
149
- case 'return': return term.value != null ? [term.value] : [];
150
- default: return [];
151
- }
152
- }
153
-
154
- // 判断指令是否有副作用(不能被 DCE 删除)
155
- function hasSideEffect(instr) {
156
- switch (instr.op) {
157
- case 'call': // 保守假设调用有副作用
158
- case 'store':
159
- return true;
160
- default:
161
- return false;
162
- }
163
- }
164
-
165
- // 判断指令是否是可折叠的纯计算(无副作用)
166
- function isPure(instr) {
167
- return !hasSideEffect(instr);
168
- }
169
-
170
- // ============================================================
171
- // 克隆工具(优化 pass 需要拷贝 IR)
172
- // ============================================================
173
- function cloneInstr(i) { return { ...i, args: i.args ? [...i.args] : undefined }; }
174
- function cloneBlock(b) {
175
- const nb = new Block(b.id);
176
- nb.instrs = b.instrs.map(cloneInstr);
177
- nb.term = { ...b.term };
178
- return nb;
179
- }
180
- function cloneFunc(fn) {
181
- const nf = new Func(fn.name, fn.params);
182
- nf.blocks = fn.blocks.map(cloneBlock);
183
- buildCFG(nf);
184
- return nf;
185
- }
186
-
187
- // ============================================================
188
- // 打印(调试用)
189
- // ============================================================
190
- function instrToString(i) {
191
- switch (i.op) {
192
- case 'const': return ` ${i.dest} = ${JSON.stringify(i.value)}`;
193
- case 'mov': return ` ${i.dest} = ${i.src}`;
194
- case 'phi': return ` ${i.dest} = phi(${i.args.join(', ')}) [from ${i.blocks.join(',')}]`;
195
- case 'call': return ` ${i.dest} = call ${i.name}(${i.args.join(', ')})`;
196
- case 'unbox': return ` ${i.dest} = unbox_${i.kind}(${i.src})`;
197
- case 'itof': return ` ${i.dest} = itof(${i.src})`;
198
- case 'load': return ` ${i.dest} = *(${i.base} + ${i.offset})`;
199
- case 'store': return ` *(${i.base} + ${i.offset}) = ${i.src}`;
200
- default:
201
- if (['neg','not'].includes(i.op)) return ` ${i.dest} = ${i.op} ${i.src}`;
202
- return ` ${i.dest} = ${i.left} ${i.op} ${i.right}`;
203
- }
204
- }
205
- function termToString(t) {
206
- switch (t.kind) {
207
- case 'jump': return ` goto L${t.target}`;
208
- case 'branch': return ` if ${t.cond} goto L${t.then} else goto L${t.else}`;
209
- case 'return': return ` ret ${t.value != null ? t.value : ''}`;
210
- case 'unreachable': return ` unreachable`;
211
- }
212
- }
213
- function funcToString(fn) {
214
- const lines = [`function ${fn.name}(${fn.params.join(', ')}) {`];
215
- for (const b of fn.blocks) {
216
- lines.push(`L${b.id}:`);
217
- for (const i of b.instrs) lines.push(instrToString(i));
218
- lines.push(termToString(b.term));
219
- }
220
- lines.push('}');
221
- return lines.join('\n');
222
- }
223
-
224
- module.exports = {
225
- // 构造器
226
- const_, mov, binop, unop, call, phi, unbox, itof,
227
- jump, branch, ret, unreachable,
228
- Block, Func,
229
- // CFG
230
- buildCFG,
231
- // 操作数
232
- instrUse, instrDef, termUse, hasSideEffect, isPure,
233
- // 克隆
234
- cloneInstr, cloneBlock, cloneFunc,
235
- // 打印
236
- instrToString, termToString, funcToString,
237
- };
@@ -1,126 +0,0 @@
1
- // ============================================================
2
- // passes/constProp.js —— Pass 1:常量传播与常量折叠
3
- //
4
- // - 局部块内常量传播:跟踪 var -> 字面值
5
- // - 二元/一元常量折叠:1+2*3 在编译期算出
6
- // - 条件为常量的分支:删除不可达分支
7
- // - 跨块:通过支配关系做简单的全局常量传播(常量在块入口已知)
8
- // ============================================================
9
- 'use strict';
10
-
11
- const I = require('../ir');
12
- const A = require('../analysis');
13
-
14
- /**
15
- * 在单个基本块内做常量折叠 + 条件常量分支消解。
16
- * known: 入口处已知常量 map(var->literal),会被原地更新。
17
- * 返回该块出口仍成立的 known 快照(供后继使用)。
18
- */
19
- function simplifyBlock(fn, bid, known) {
20
- const blk = fn.blocks[bid];
21
- const out = new Map(known); // 出口快照
22
-
23
- for (let i = 0; i < blk.instrs.length; i++) {
24
- const instr = blk.instrs[i];
25
- const d = I.instrDef(instr);
26
-
27
- switch (instr.op) {
28
- case 'const':
29
- out.set(instr.dest, instr.value);
30
- break;
31
-
32
- case 'mov': {
33
- // x = y :若 y 已知常量,直接折叠成 const x
34
- if (out.has(instr.src)) {
35
- blk.instrs[i] = I.const_(instr.dest, out.get(instr.src));
36
- out.set(instr.dest, out.get(instr.src));
37
- } else {
38
- out.delete(instr.dest);
39
- }
40
- break;
41
- }
42
-
43
- case 'call':
44
- case 'load':
45
- case 'store':
46
- // 调用/加载结果未知
47
- if (d) out.delete(d);
48
- break;
49
-
50
- case 'phi':
51
- // phi 的每个参数常量值不一致就放弃;一致则传播
52
- {
53
- const vals = instr.args.map(a => (typeof a === 'string' && out.has(a)) ? out.get(a) : a);
54
- const first = vals[0];
55
- if (vals.every(v => v === first && typeof v !== 'string')) {
56
- blk.instrs[i] = I.const_(instr.dest, first);
57
- out.set(instr.dest, first);
58
- } else {
59
- out.delete(instr.dest);
60
- }
61
- }
62
- break;
63
-
64
- default:
65
- if (['neg', 'not'].includes(instr.op)) {
66
- if (out.has(instr.src)) {
67
- const r = A.foldUnop(instr.op, out.get(instr.src));
68
- if (r !== null) {
69
- blk.instrs[i] = I.const_(instr.dest, A.toLiteral(r));
70
- out.set(instr.dest, A.toLiteral(r));
71
- } else out.delete(instr.dest);
72
- } else out.delete(instr.dest);
73
- } else {
74
- // 二元
75
- if (out.has(instr.left) && out.has(instr.right)) {
76
- const r = A.foldBinop(instr.op, out.get(instr.left), out.get(instr.right));
77
- if (r !== null) {
78
- blk.instrs[i] = I.const_(instr.dest, A.toLiteral(r));
79
- out.set(instr.dest, A.toLiteral(r));
80
- } else out.delete(instr.dest);
81
- } else out.delete(instr.dest);
82
- }
83
- }
84
- }
85
-
86
- // 终结符:条件常量 -> 改为无条件跳转
87
- const t = blk.term;
88
- if (t.kind === 'branch' && out.has(t.cond)) {
89
- const cv = !!A.toLiteral(out.get(t.cond));
90
- const target = cv ? t.then : t.else;
91
- blk.term = I.jump(target);
92
- }
93
- return out;
94
- }
95
-
96
- /**
97
- * 函数级:迭代直到不动点(前向数据流)。
98
- * knownEntry[blockId] = Map of known constants at block entry。
99
- */
100
- function constPropagate(fn) {
101
- const n = fn.blocks.length;
102
- const entryKnown = Array.from({ length: n }, () => new Map());
103
- entryKnown[0] = new Map(); // 入口未知
104
-
105
- let changed = true, guard = 0;
106
- while (changed && guard++ < 100) {
107
- changed = false;
108
- for (let b = 0; b < n; b++) {
109
- if (fn.blocks[b].instrs.length === 0 && fn.blocks[b].term.kind === 'unreachable') continue;
110
- const before = entryKnown[b];
111
- const after = simplifyBlock(fn, b, before);
112
- // 把 after 传播到后继
113
- for (const s of fn.blocks[b].succs) {
114
- const sk = entryKnown[s];
115
- // 合并:两个前驱都知道同一个常量才传播
116
- for (const [k, v] of after) {
117
- if (!sk.has(k)) { sk.set(k, v); changed = true; }
118
- else if (sk.get(k) !== v) sk.set(k, undefined); // 冲突 -> 未知
119
- }
120
- }
121
- }
122
- }
123
- return fn;
124
- }
125
-
126
- module.exports = { constPropagate, simplifyBlock };
@@ -1,57 +0,0 @@
1
- // ============================================================
2
- // passes/copyProp.js —— Pass 4:复制传播
3
- //
4
- // x = y 之后,把后续对 x 的 use 替换为 y,形成链 x=y=z。
5
- // 减少寄存器压力,让 mov 最终被 DCE 删除。
6
- // 在 SSA 形式下安全:每个变量只被赋值一次,无需跨块分析。
7
- // ============================================================
8
- 'use strict';
9
-
10
- const I = require('../ir');
11
-
12
- function propagateCopies(fn) {
13
- // SSA 下:每个 dest 唯一定义。建立 dest -> src 的别名图。
14
- const alias = new Map(); // x -> y (x = y)
15
- for (const blk of fn.blocks) {
16
- for (const instr of blk.instrs) {
17
- if (instr.op === 'mov') {
18
- alias.set(instr.dest, instr.src);
19
- }
20
- }
21
- }
22
- // 传递闭包:x -> y -> z 归约成 x -> z
23
- const resolve = (v) => {
24
- const seen = new Set();
25
- while (alias.has(v) && !seen.has(v)) { seen.add(v); v = alias.get(v); }
26
- return v;
27
- };
28
-
29
- // 替换所有 use
30
- for (const blk of fn.blocks) {
31
- for (const instr of blk.instrs) {
32
- if (instr.op === 'mov' && alias.get(instr.dest) === instr.src) {
33
- continue; // 这是复制本身,不动
34
- }
35
- switch (instr.op) {
36
- case 'mov': case 'neg': case 'not':
37
- instr.src = resolve(instr.src); break;
38
- case 'call':
39
- instr.args = instr.args.map(resolve); break;
40
- case 'load':
41
- instr.base = resolve(instr.base); break;
42
- case 'store':
43
- instr.base = resolve(instr.base); instr.src = resolve(instr.src); break;
44
- case 'phi':
45
- instr.args = instr.args.map(resolve); break;
46
- default:
47
- instr.left = resolve(instr.left);
48
- instr.right = resolve(instr.right);
49
- }
50
- }
51
- if (blk.term.kind === 'branch') blk.term.cond = resolve(blk.term.cond);
52
- else if (blk.term.kind === 'return' && blk.term.value != null) blk.term.value = resolve(blk.term.value);
53
- }
54
- return fn;
55
- }
56
-
57
- module.exports = { propagateCopies };
@@ -1,62 +0,0 @@
1
- // ============================================================
2
- // passes/cse.js —— Pass 3:公共子表达式消除(局部 + 全局)
3
- //
4
- // 可用表达式(available expressions):
5
- // 表达式 key = op(left,right)(或 unop src)
6
- // 块内:若同一表达式已算过一次(结果为 y),后续重复计算替换为 mov
7
- // 失效:出现 call/store/load 或对 left/right 的重新定义时,
8
- // 包含该变量的表达式全部失效(保守)
9
- // ============================================================
10
- 'use strict';
11
-
12
- const I = require('../ir');
13
-
14
- function exprKey(instr) {
15
- if (['add','sub','mul','div','mod','lt','le','eq','ne','gt','ge','and','or'].includes(instr.op)) {
16
- // 交换律规范化:add/and/or/eq/ne 的操作数排序,让 a+b 与 b+a 命中
17
- if (['add','mul','and','or','eq','ne'].includes(instr.op)) {
18
- const [l, r] = instr.left <= instr.right ? [instr.left, instr.right] : [instr.right, instr.left];
19
- return `${instr.op}(${l},${r})`;
20
- }
21
- return `${instr.op}(${instr.left},${instr.right})`;
22
- }
23
- if (['neg','not'].includes(instr.op)) return `${instr.op}(${instr.src})`;
24
- return null;
25
- }
26
-
27
- function eliminateCSE(fn) {
28
- for (const blk of fn.blocks) {
29
- // avail: exprKey -> 结果变量名
30
- const avail = new Map();
31
- for (let i = 0; i < blk.instrs.length; i++) {
32
- const instr = blk.instrs[i];
33
- if (instr.op === 'phi') continue;
34
- const key = exprKey(instr);
35
-
36
- if (key && avail.has(key)) {
37
- // 已有相同表达式,结果在 avail.get(key),替换为 mov
38
- const existing = avail.get(key);
39
- if (existing !== instr.dest) {
40
- blk.instrs[i] = I.mov(instr.dest, existing);
41
- // dest 现在是 existing 的别名,继续
42
- }
43
- continue;
44
- }
45
-
46
- // 这条指令定义了新变量,把 avail 中所有用到该变量的表达式删掉
47
- const d = I.instrDef(instr);
48
- if (d) {
49
- for (const [k, v] of avail) {
50
- if (k.includes(`(${d}`) || k.includes(`,${d})`) || k === `${instr.op}(${d})`) avail.delete(k);
51
- }
52
- }
53
- // 有副作用的指令:保守清空 avail(因为可能改内存)
54
- if (I.hasSideEffect(instr)) avail.clear();
55
-
56
- if (key) avail.set(key, instr.dest);
57
- }
58
- }
59
- return fn;
60
- }
61
-
62
- module.exports = { eliminateCSE, exprKey };
@@ -1,68 +0,0 @@
1
- // ============================================================
2
- // passes/dce.js —— Pass 2:死代码消除
3
- //
4
- // 使用跨块活跃变量分析:
5
- // 1. 计算每个块的 liveIn/liveOut
6
- // 2. 反向扫描块内指令,从 liveOut 开始维护 liveVars
7
- // 3. 无副作用且 dest 不在 liveVars 的指令 -> 死,删除
8
- // 4. 同时删除不可达块
9
- // 5. 迭代直到不动点
10
- // ============================================================
11
- 'use strict';
12
-
13
- const I = require('../ir');
14
- const A = require('../analysis');
15
-
16
- function eliminateDeadCode(fn) {
17
- let outer = 0;
18
- let changedOverall = true;
19
- while (changedOverall && outer++ < 30) {
20
- changedOverall = false;
21
- I.buildCFG(fn);
22
-
23
- // 跨块活跃分析
24
- const { liveIn, liveOut } = A.liveness(fn);
25
-
26
- // 逐块反向扫描
27
- for (const blk of fn.blocks) {
28
- let liveVars = new Set(liveOut[blk.id]);
29
- // 先加终结符的 use
30
- for (const v of I.termUse(blk.term)) liveVars.add(v);
31
-
32
- const kept = [];
33
- // 反向遍历
34
- for (let i = blk.instrs.length - 1; i >= 0; i--) {
35
- const instr = blk.instrs[i];
36
- const d = I.instrDef(instr);
37
- const side = I.hasSideEffect(instr);
38
- const isLive = side || (d && liveVars.has(d));
39
-
40
- if (isLive) {
41
- kept.unshift(instr);
42
- // def 先杀
43
- if (d) liveVars.delete(d);
44
- // 加 use
45
- for (const u of I.instrUse(instr)) liveVars.add(u);
46
- }
47
- // phi 不能在这里删(SSA 结构需要);后续 pass 处理
48
- if (!isLive && instr.op === 'phi') kept.unshift(instr);
49
- }
50
- if (kept.length !== blk.instrs.length) changedOverall = true;
51
- blk.instrs = kept;
52
- }
53
-
54
- // 不可达块删除
55
- const reach = A.reachable(fn);
56
- for (let b = 0; b < fn.blocks.length; b++) {
57
- if (!reach[b]) {
58
- fn.blocks[b].instrs = [];
59
- fn.blocks[b].term = I.unreachable();
60
- changedOverall = true;
61
- }
62
- }
63
- if (changedOverall) I.buildCFG(fn);
64
- }
65
- return fn;
66
- }
67
-
68
- module.exports = { eliminateDeadCode };
@@ -1,38 +0,0 @@
1
- // ============================================================
2
- // passes/devirt.js —— Pass 8:去虚拟化
3
- //
4
- // 分析 call 指令的目标是否可静态确定。
5
- // coCN 里函数是一等公民,但:
6
- // - 直接调用用户定义函数名(call foo(args))本来就是直接调用
7
- // - 通过变量调用:f = foo; call f(...) —— 若 f 在函数内只被赋值一次且
8
- // 目标已知,可替换为直接 call foo
9
- // 内联会进一步消费直接调用。
10
- // ============================================================
11
- 'use strict';
12
-
13
- const I = require('../ir');
14
-
15
- function devirtualize(fn, fnPool) {
16
- // 收集每个变量被赋值成了哪个函数名(mov v, funcName 形式)
17
- const funcPtr = new Map();
18
- for (const blk of fn.blocks) {
19
- for (const instr of blk.instrs) {
20
- if (instr.op === 'mov' && typeof instr.src === 'string' && fnPool && fnPool.has(instr.src)) {
21
- funcPtr.set(instr.dest, instr.src);
22
- }
23
- }
24
- }
25
- // 替换间接调用:call dest, varName(args) -> call dest, realFunc(args)
26
- for (const blk of fn.blocks) {
27
- for (const instr of blk.instrs) {
28
- if (instr.op !== 'call') continue;
29
- if (funcPtr.has(instr.name)) {
30
- instr.name = funcPtr.get(instr.name);
31
- instr.devirtualized = true;
32
- }
33
- }
34
- }
35
- return fn;
36
- }
37
-
38
- module.exports = { devirtualize };
@@ -1,67 +0,0 @@
1
- // ============================================================
2
- // passes/escape.js —— Pass 7:逃逸分析
3
- //
4
- // 分析堆分配对象(call __alloc / load 后地址运算)是否逃逸出函数。
5
- // 不逃逸的对象:
6
- // - 不传到其他函数作为参数
7
- // - 不存到全局/堆中
8
- // - 不被返回
9
- // 就替换为栈分配(直接在 IR 上记录 stackAllocated=true)。
10
- // ============================================================
11
- 'use strict';
12
-
13
- const I = require('../ir');
14
-
15
- function escapeAnalysis(fn) {
16
- // 收集所有分配点:call 到名字是 "__alloc_list"/"__alloc_dict" 的 dest
17
- const allocSites = new Set();
18
- for (const blk of fn.blocks) {
19
- for (const instr of blk.instrs) {
20
- if (instr.op === 'call' && /^__alloc/.test(instr.name)) allocSites.add(instr.dest);
21
- }
22
- }
23
-
24
- // 对每个分配变量,检查它是否逃逸:
25
- // 作为 call 的实参 -> 逃逸
26
- // 作为 return 值 -> 逃逸
27
- // 被 store 到内存(base/offset 含该变量)-> 逃逸(保守)
28
- const escapes = new Set();
29
- const worklist = [...allocSites];
30
- const visited = new Set();
31
-
32
- while (worklist.length) {
33
- const v = worklist.pop();
34
- if (visited.has(v)) continue;
35
- visited.add(v);
36
-
37
- for (const blk of fn.blocks) {
38
- for (const instr of blk.instrs) {
39
- // 作为 call 实参
40
- if (instr.op === 'call' && instr.args.includes(v)) { escapes.add(v); continue; }
41
- // store 进去
42
- if (instr.op === 'store' && (instr.base === v || instr.src === v)) { escapes.add(v); continue; }
43
- // mov 传播:v = x 且 x 在 allocSites,或 x = v 且 x 也可能逃逸
44
- if (instr.op === 'mov') {
45
- if (instr.src === v && !escapes.has(instr.dest)) worklist.push(instr.dest);
46
- if (instr.dest === v && !escapes.has(instr.src)) worklist.push(instr.src);
47
- }
48
- }
49
- // 作为 return 值
50
- const t = blk.term;
51
- if (t.kind === 'return' && t.value === v) escapes.add(v);
52
- }
53
- }
54
-
55
- // 标记不逃逸的分配为栈分配
56
- for (const blk of fn.blocks) {
57
- for (const instr of blk.instrs) {
58
- if (instr.op === 'call' && /^__alloc/.test(instr.name) && !escapes.has(instr.dest)) {
59
- instr.stackAlloc = true; // 后端看到此标记就栈上分配
60
- }
61
- }
62
- }
63
- fn._escaped = escapes;
64
- return fn;
65
- }
66
-
67
- module.exports = { escapeAnalysis };