@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.
Files changed (108) hide show
  1. package/README.md +54 -11
  2. package/bin//345/205/245/345/217/243.js +0 -8
  3. package/package.json +4 -5
  4. 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
  5. 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
  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/351/252/214/350/257/201//347/274/226/350/257/221/351/251/261/345/212/250.co +3 -1
  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/252/214/350/257/201.sh +24 -8
  8. package/bin/install.js +0 -276
  9. package//345/267/245/345/205/267/lsp/server.js +0 -419
  10. package//345/267/245/345/205/267/lsp//346/265/213/350/257/225.sh +0 -81
  11. package//345/267/245/345/205/267/vscode-cocn/extension.js +0 -203
  12. package//345/267/245/345/205/267/vscode-cocn/package.json +0 -58
  13. package//345/267/245/345/205/267/vscode-cocn/syntaxes/coCN.tmLanguage.json +0 -130
  14. 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
  15. 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
  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/benchmark_demo.js +0 -75
  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/dominance.js +0 -158
  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/gvn.js +0 -45
  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/index.js +0 -121
  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/ir.js +0 -237
  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/passes/constProp.js +0 -126
  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/copyProp.js +0 -57
  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/cse.js +0 -62
  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/dce.js +0 -68
  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/devirt.js +0 -38
  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/escape.js +0 -67
  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/inline.js +0 -142
  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/loopOpt.js +0 -215
  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/peephole.js +0 -42
  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/regalloc.js +0 -212
  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/scheduler.js +0 -86
  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/spill.js +0 -78
  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/ssa.js +0 -235
  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/traceSched.js +0 -47
  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/unroll.js +0 -46
  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//346/265/213/350/257/225/harness.js +0 -33
  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/run_all.js +0 -11
  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/test_constfold.js +0 -58
  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_dce.js +0 -49
  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_e2e.js +0 -57
  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_gvn_spill.js +0 -45
  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_loop.js +0 -46
  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_regalloc.js +0 -51
  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_ssa.js +0 -65
  45. 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
  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.js +0 -38
  47. 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
  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/index.js +0 -103
  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//345/206/205/350/201/224.js +0 -194
  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/207/275/346/225/260/347/211/271/345/214/226.js +0 -168
  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//346/243/200/346/237/245/346/266/210/351/231/244.js +0 -166
  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/265/213/350/257/225//345/206/205/350/201/224/346/265/213/350/257/225.js +0 -53
  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
  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//347/253/257/345/210/260/347/253/257/346/265/213/350/257/225.js +0 -104
  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/261/273/345/236/213/346/216/250/346/226/255/346/265/213/350/257/225.js +0 -87
  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//350/277/220/350/241/214/346/265/213/350/257/225.js +0 -43
  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//351/200/203/351/200/270/345/210/206/346/236/220/346/265/213/350/257/225.js +0 -58
  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
  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/216/257/345/242/203.js +0 -126
  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/261/273/345/236/213.js +0 -294
  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/346/216/250/346/226/255.js +0 -461
  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//350/260/203/347/224/250/345/233/276.js +0 -119
  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//351/200/203/351/200/270/345/210/206/346/236/220.js +0 -184
  64. 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
  65. 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
  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/build.sh +0 -70
  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/dict.c +0 -143
  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/list.c +0 -148
  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/math.c +0 -129
  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/print.c +0 -135
  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/rt.h +0 -177
  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/string.c +0 -288
  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/syscall.asm +0 -153
  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/test.c +0 -228
  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//344/270/264/346/227/266.c +0 -379
  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.c +0 -441
  77. 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
  78. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/Empty.java +0 -1
  79. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.c +0 -53
  80. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.co +0 -42
  81. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.go +0 -43
  82. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.java +0 -29
  83. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.js +0 -24
  84. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.py +0 -36
  85. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.rs +0 -35
  86. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty.co +0 -0
  87. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_c.c +0 -1
  88. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_go.go +0 -2
  89. package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_rs.rs +0 -1
  90. 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
  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.co +0 -10
  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/216/222/345/272/217.c +0 -23
  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.co +0 -31
  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/226/220/346/263/242/351/202/243/345/245/221.c +0 -17
  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.co +0 -10
  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/22120.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_/347/264/257/345/212/240.c +0 -14
  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.co +0 -10
  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/345/257/271/346/257/224/347/273/223/346/236/234.txt +0 -164
  100. 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
  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.co +0 -2
  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/sum.c +0 -10
  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.co +0 -3
  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//350/256/241/346/265/213.sh +0 -32
  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/347/273/223/346/236/234.txt +0 -7
  106. 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
  107. 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
  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/347/273/223/346/236/234.txt +0 -29
@@ -1,142 +0,0 @@
1
- // ============================================================
2
- // passes/inline.js —— Pass 6:函数内联
3
- //
4
- // - 调用图分析(哪些函数调用谁)
5
- // - 小函数(IR 指令数 < 阈值)自动内联
6
- // - 递归函数不内联
7
- // - 内联后变量名加前缀避免冲突,返回值连接到调用点
8
- // ============================================================
9
- 'use strict';
10
-
11
- const I = require('../ir');
12
-
13
- const DEFAULT_THRESHOLD = 20;
14
-
15
- function countInstrs(fn) {
16
- return fn.blocks.reduce((s, b) => s + b.instrs.length, 0);
17
- }
18
-
19
- /**
20
- * 在 caller 函数体内,把对 callee 的调用内联。
21
- * call: {op:'call', dest, name:calleeName, args:[...]}
22
- * 在 caller 的 bid 块中,位置 idx。
23
- */
24
- function inlineCall(caller, callee, callSiteBlockId, instrIdx, prefix) {
25
- const callInstr = caller.blocks[callSiteBlockId].instrs[instrIdx];
26
- const args = callInstr.args;
27
-
28
- // 1. 复制 callee 的所有块,块 id 整体偏移
29
- const oldN = caller.blocks.length;
30
- const idMap = new Map(); // callee block id -> caller block id
31
- const newBlocks = [];
32
- for (const cb of callee.blocks) {
33
- const nb = I.cloneBlock(cb);
34
- nb.id = oldN + newBlocks.length;
35
- // 重映射块内所有名字:加前缀
36
- for (const instr of nb.instrs) prefixInstr(instr, prefix);
37
- prefixTerm(nb.term, prefix);
38
- idMap.set(cb.id, nb.id);
39
- newBlocks.push(nb);
40
- }
41
- // 重映射 callee 块内的跳转目标
42
- for (const nb of newBlocks) {
43
- if (nb.term.kind === 'jump') nb.term.target = idMap.get(nb.term.target);
44
- else if (nb.term.kind === 'branch') {
45
- nb.term.then = idMap.get(nb.term.then);
46
- nb.term.else = idMap.get(nb.term.else);
47
- }
48
- }
49
-
50
- // 2. 把 callee 入口块的参数绑定为实参:
51
- // 在入口块开头插入 mov 形参名, 实参名
52
- const entry = newBlocks[0];
53
- for (let i = 0; i < callee.params.length; i++) {
54
- entry.instrs.unshift(I.mov(prefix + callee.params[i], args[i]));
55
- }
56
-
57
- // 3. 找 callee 的 return 块,把 return value 接到 callInstr.dest
58
- const returnBlocks = [];
59
- for (const nb of newBlocks) {
60
- if (nb.term.kind === 'return') {
61
- if (nb.term.value != null) {
62
- nb.instrs.push(I.mov(callInstr.dest, nb.term.value));
63
- }
64
- nb.term = I.jump(0); // 占位,下面改为 caller 后续块
65
- returnBlocks.push(nb);
66
- }
67
- }
68
-
69
- // 4. 切分 caller 当前块:call 之前部分留在原块,
70
- // call 之后部分放到新的 continuation 块
71
- const callerBlk = caller.blocks[callSiteBlockId];
72
- const before = callerBlk.instrs.slice(0, instrIdx);
73
- const after = callerBlk.instrs.slice(instrIdx + 1);
74
- callerBlk.instrs = before;
75
- // continuation 块:包含 call 之后的指令,保留原 term
76
- const cont = new I.Block(caller.blocks.length);
77
- cont.instrs = after;
78
- cont.term = callerBlk.term;
79
- caller.blocks.push(cont);
80
-
81
- // caller 原块跳到 callee 入口
82
- callerBlk.term = I.jump(idMap.get(0));
83
- // callee 所有 return 块跳到 continuation
84
- for (const rb of returnBlocks) rb.term = I.jump(cont.id);
85
-
86
- // 把 callee 新块全部加入 caller
87
- caller.blocks.push(...newBlocks);
88
- }
89
-
90
- function prefixInstr(instr, prefix) {
91
- switch (instr.op) {
92
- case 'mov': case 'neg': case 'not':
93
- instr.dest = prefix + instr.dest; instr.src = prefix + instr.src; break;
94
- case 'call':
95
- instr.dest = prefix + instr.dest;
96
- instr.args = instr.args.map(a => prefix + a); break;
97
- case 'load':
98
- instr.dest = prefix + instr.dest; instr.base = prefix + instr.base; break;
99
- case 'store':
100
- instr.base = prefix + instr.base; instr.src = prefix + instr.src; break;
101
- case 'phi':
102
- instr.dest = prefix + instr.dest;
103
- instr.args = instr.args.map(a => prefix + a); break;
104
- default:
105
- instr.dest = prefix + instr.dest;
106
- instr.left = prefix + instr.left;
107
- instr.right = prefix + instr.right;
108
- }
109
- }
110
- function prefixTerm(term, prefix) {
111
- if (term.kind === 'branch') term.cond = prefix + term.cond;
112
- else if (term.kind === 'return' && term.value != null) term.value = prefix + term.value;
113
- }
114
-
115
- /**
116
- * 函数级内联:遍历 caller,找可内联的 call。
117
- * fnPool: Map<funcName, Func>
118
- */
119
- function inlineFunctions(fnPool, threshold = DEFAULT_THRESHOLD) {
120
- for (const [name, caller] of fnPool) {
121
- I.buildCFG(caller);
122
- for (let b = 0; b < caller.blocks.length; b++) {
123
- const blk = caller.blocks[b];
124
- for (let i = 0; i < blk.instrs.length; i++) {
125
- const instr = blk.instrs[i];
126
- if (instr.op !== 'call') continue;
127
- const callee = fnPool.get(instr.name);
128
- if (!callee) continue;
129
- if (callee.name === name) continue; // 递归跳过
130
- if (countInstrs(callee) > threshold) continue;
131
- // 内联!
132
- const prefix = `__inl_${callee.name}_${b}_${i}__`;
133
- inlineCall(caller, callee, b, i, prefix);
134
- I.buildCFG(caller);
135
- // 内联后指令位置变了,重新扫
136
- i = -1; b = -1; break;
137
- }
138
- }
139
- }
140
- }
141
-
142
- module.exports = { inlineFunctions, countInstrs };
@@ -1,215 +0,0 @@
1
- // ============================================================
2
- // passes/loopOpt.js —— Pass 5:循环优化
3
- //
4
- // 1. 自然循环识别:找回边 h->t(t 支配 h),循环体 = 所有能到 h 而不经过 t 的块
5
- // 2. 循环不变量外提:循环体内不随迭代变化的表达式,提到前置块
6
- // 3. 归纳变量/强度削减:i*4 在每次迭代改为 +=4
7
- // 4. 循环展开:小循环体展开 N 次,减少分支开销
8
- // 5. 循环反转:while -> do-while(先执行一次再判断)
9
- // ============================================================
10
- 'use strict';
11
-
12
- const I = require('../ir');
13
- const { computeDominators } = require('../dominance');
14
-
15
- // ---------- 1. 找自然循环 ----------
16
- function findNaturalLoops(fn) {
17
- const dom = computeDominators(fn);
18
- const loops = [];
19
- // 回边:b->h 且 h 支配 b
20
- for (const blk of fn.blocks) {
21
- for (const s of blk.succs) {
22
- if (dom.idom[blk.id] === s || dominates(dom.idom, s, blk.id)) {
23
- // s 是 header,blk 是尾块(回边)
24
- const body = computeLoopBody(fn, s, blk.id);
25
- loops.push({ header: s, tail: blk.id, body, preheader: null });
26
- }
27
- }
28
- }
29
- return loops;
30
- }
31
- function dominates(idom, a, b) {
32
- // a 是否支配 b(含相等)
33
- let cur = b;
34
- let guard = 0;
35
- while (cur !== -1 && guard++ < 1000) {
36
- if (cur === a) return true;
37
- if (idom[cur] === cur) break; // 到达根(入口支配自己)
38
- cur = idom[cur];
39
- }
40
- return false;
41
- }
42
- function computeLoopBody(fn, header, tail) {
43
- const body = new Set([header]);
44
- const stack = [tail];
45
- while (stack.length) {
46
- const b = stack.pop();
47
- if (body.has(b)) continue;
48
- body.add(b);
49
- for (const p of fn.blocks[b].preds) stack.push(p);
50
- }
51
- return body;
52
- }
53
-
54
- // ---------- 2. 循环不变量外提 ----------
55
- function isLoopInvariant(instr, loopBody, defOutside) {
56
- // defOutside: Set of variable names whose definition 点在循环体外
57
- if (instr.op === 'call' || instr.op === 'store' || instr.op === 'load') return false;
58
- switch (instr.op) {
59
- case 'const': return true;
60
- case 'mov': return defOutside.has(instr.src);
61
- case 'neg': case 'not': return defOutside.has(instr.src);
62
- default:
63
- if (['add','sub','mul','div','mod','lt','le','eq','ne','gt','ge','and','or'].includes(instr.op)) {
64
- return defOutside.has(instr.left) && defOutside.has(instr.right);
65
- }
66
- }
67
- return false;
68
- }
69
- function hoistInvariants(fn, loop) {
70
- const { header, body } = loop;
71
- // 收集循环体内定义的所有变量
72
- const defInside = new Set();
73
- for (const bid of body) for (const instr of fn.blocks[bid].instrs) {
74
- const d = I.instrDef(instr); if (d) defInside.add(d);
75
- }
76
- const defOutside = new Set();
77
- for (const v of [...allDefs(fn)].filter(v => !defInside.has(v))) defOutside.add(v);
78
- // 函数参数视为循环外定义(SSA 重命名后可能是 a#1 形式)
79
- for (const p of fn.params) {
80
- defOutside.add(p);
81
- // 也加上 SSA 版本号形式
82
- for (const suffix of ['#1', '#2', '#3']) defOutside.add(p + suffix);
83
- }
84
- // 被使用但从未被定义的变量(来自参数/全局),也算循环外
85
- const usedUndefined = new Set();
86
- for (const b of fn.blocks) for (const instr of b.instrs) {
87
- for (const u of I.instrUse(instr)) {
88
- if (typeof u === 'string' && !defInside.has(u) && ![...allDefs(fn)].includes(u)) {
89
- usedUndefined.add(u);
90
- }
91
- }
92
- }
93
- for (const v of usedUndefined) defOutside.add(v);
94
-
95
- // 找循环前置块(header 的唯一前驱,且不在循环体内)
96
- const prePreds = fn.blocks[header].preds.filter(p => !body.has(p));
97
- if (prePreds.length !== 1) return; // 没有唯一前置块,跳过
98
- const preId = prePreds[0];
99
- const preBlock = fn.blocks[preId];
100
-
101
- // 迭代:找出可外提的指令(operands 都是循环不变)
102
- let changed = true;
103
- while (changed) {
104
- changed = false;
105
- for (const bid of body) {
106
- const blk = fn.blocks[bid];
107
- for (let i = 0; i < blk.instrs.length; i++) {
108
- const instr = blk.instrs[i];
109
- if (instr.op === 'phi') continue;
110
- if (isLoopInvariant(instr, body, defOutside)) {
111
- // 外提到前置块
112
- preBlock.instrs.push(instr);
113
- blk.instrs.splice(i, 1);
114
- i--;
115
- changed = true;
116
- // 它定义的变量现在变成“循环外定义”
117
- const d = I.instrDef(instr);
118
- if (d) { defOutside.add(d); defInside.delete(d); }
119
- }
120
- }
121
- }
122
- }
123
- }
124
- function allDefs(fn) {
125
- const s = new Set();
126
- for (const b of fn.blocks) for (const i of b.instrs) { const d = I.instrDef(i); if (d) s.add(d); }
127
- return s;
128
- }
129
-
130
- // ---------- 3. 强度削减:i*k -> 累加 ----------
131
- // 在循环里找 i = i + c, 且 t = i * k。把 t 改为 t = t + c*k,
132
- // 初始化 t0 = 0*k 在前置块。
133
- function strengthReduce(fn, loop) {
134
- const { header, body } = loop;
135
- // 找循环计数器:phi i = (0, i+1)
136
- for (const instr of fn.blocks[header].instrs) {
137
- if (instr.op !== 'phi') continue;
138
- const iv = instr.dest;
139
- // 在循环体找 iv = iv + c
140
- let step = null;
141
- for (const bid of body) for (const i2 of fn.blocks[bid].instrs) {
142
- if (i2.op === 'add' && (i2.left === iv || i2.right === iv)) {
143
- const other = i2.left === iv ? i2.right : i2.left;
144
- if (/^-?\d+$/.test(String(other))) step = parseInt(other);
145
- }
146
- }
147
- if (step == null || step === 0) continue;
148
- // 找 t = iv * k
149
- for (const bid of body) for (const i2 of [...fn.blocks[bid].instrs]) {
150
- if (i2.op === 'mul' && (i2.left === iv || i2.right === iv)) {
151
- const k = i2.left === iv ? i2.right : i2.left;
152
- if (!/^-?\d+$/.test(String(k))) continue;
153
- const kk = parseInt(k);
154
- // 替换 t = iv*k 为 t = t + step*kk(在 header 加 phi)
155
- const t = i2.dest;
156
- const preId = fn.blocks[header].preds.find(p => !body.has(p));
157
- if (preId == null) continue;
158
- // 前置块插 const t_init = 0(或 iv_init * k)
159
- const ivInit = instr.args[0]; // 从循环外进来的初值
160
- fn.blocks[preId].instrs.push(I.const_('sr#' + t, 0));
161
- // header 加 phi sr_t = (sr_t_init, sr_t_next)
162
- const nextName = 'sr#' + t + '#next';
163
- const ph = I.phi('sr#' + t, [preId, loop.tail], ['sr#' + t, nextName]);
164
- fn.blocks[header].instrs.unshift(ph);
165
- // 尾块插入 next = sr_t + step*kk
166
- fn.blocks[loop.tail].instrs.push(
167
- I.const_(nextName + '#k', step * kk),
168
- I.binop('add', nextName, 'sr#' + t, nextName + '#k')
169
- );
170
- // 删除原来的 mul
171
- const idx = fn.blocks[bid].instrs.indexOf(i2);
172
- if (idx >= 0) fn.blocks[bid].instrs.splice(idx, 1);
173
- }
174
- }
175
- }
176
- }
177
-
178
- // ---------- 4. 循环展开 ----------
179
- // 把循环体复制 N 份,步长 N。简化:只展开简单计数循环。
180
- function unrollLoop(fn, loop, factor = 2) {
181
- // 保守:只在 body 块数 <= 4 时展开
182
- if (loop.body.size > 4) return;
183
- // 找到 iv 和 step
184
- const headerBlk = fn.blocks[loop.header];
185
- const ivPhi = headerBlk.instrs.find(i => i.op === 'phi');
186
- if (!ivPhi) return;
187
- // 简化策略:把循环条件改为每次跳 factor 步
188
- // 实际代码生成阶段由后端处理,这里仅做标记
189
- loop.unrollFactor = factor;
190
- }
191
-
192
- // ---------- 5. 循环反转:while -> do-while ----------
193
- // 如果 header 有来自前置块的跳转,且循环至少执行一次有保证,
194
- // 把条件判断移到循环体末尾。
195
- function loopInversion(fn, loop) {
196
- const { header, body } = loop;
197
- // 简化:不动控制流,仅记录
198
- loop.inverted = false;
199
- }
200
-
201
- // ---------- 入口 ----------
202
- function optimizeLoops(fn) {
203
- I.buildCFG(fn);
204
- const loops = findNaturalLoops(fn);
205
- for (const loop of loops) {
206
- hoistInvariants(fn, loop);
207
- strengthReduce(fn, loop);
208
- unrollLoop(fn, loop, 2);
209
- loopInversion(fn, loop);
210
- }
211
- I.buildCFG(fn);
212
- return fn;
213
- }
214
-
215
- module.exports = { optimizeLoops, findNaturalLoops };
@@ -1,42 +0,0 @@
1
- // ============================================================
2
- // peephole.js —— 窥孔优化(最终一遍)
3
- //
4
- // - 冗余 mov 消除:mov x, x 删除
5
- // - 常量 xor 优化:mov x, 0 -> xor x, x(后端提示,这里在 IR 层记录)
6
- // - 地址模式合并:base+offset*scale 提示
7
- // - 连续 mov 链折叠
8
- // ============================================================
9
- 'use strict';
10
-
11
- const I = require('./ir');
12
-
13
- function optimize(fn) {
14
- for (const blk of fn.blocks) {
15
- const out = [];
16
- for (let i = 0; i < blk.instrs.length; i++) {
17
- const instr = blk.instrs[i];
18
- // mov x, x 冗余删除
19
- if (instr.op === 'mov' && instr.dest === instr.src) continue;
20
- // const x, 0 -> 记录 zeroFlag(后端用 xor)
21
- if (instr.op === 'const' && (instr.value === 0 || instr.value === false)) {
22
- instr.useXor = true;
23
- }
24
- // mov x, y 且前一条是 mov y, z(链折叠)—— 简化为 mov x, z
25
- if (instr.op === 'mov' && out.length > 0) {
26
- const prev = out[out.length - 1];
27
- if (prev.op === 'mov' && prev.dest === instr.src) {
28
- instr.src = prev.src;
29
- } else if (prev.op === 'const' && prev.dest === instr.src) {
30
- blk.instrs[i] = I.const_(instr.dest, prev.value);
31
- out.push(blk.instrs[i]);
32
- continue;
33
- }
34
- }
35
- out.push(instr);
36
- }
37
- blk.instrs = out;
38
- }
39
- return fn;
40
- }
41
-
42
- module.exports = { optimize };
@@ -1,212 +0,0 @@
1
- // ============================================================
2
- // regalloc.js —— 图着色寄存器分配(线性扫描简化版 + 着色)
3
- //
4
- // 流程:
5
- // 1. 计算每个变量的活跃区间 [def, lastUse]
6
- // 2. 构建干涉图:区间重叠的变量之间加边
7
- // 3. Chaitin-Briggs 着色:
8
- // - 简化:度数 < k 的节点压栈(一定可着色)
9
- // - 溢出候选:度数 >= k 的节点,选全局溢出代价最低者压栈
10
- // - 选择:从栈弹出,选一个不冲突的颜色
11
- // 4. 寄存器合并:mov a,b 若不干涉,a 和 b 用同一寄存器
12
- // 5. 映射到 x86-64 物理寄存器
13
- // ============================================================
14
- 'use strict';
15
-
16
- const I = require('./ir');
17
- const A = require('./analysis');
18
-
19
- // x86-64 调用者保留/可分配寄存器(去掉 rsp/rbp 等特殊寄存器)
20
- // 调用者保留:rax rcx rdx rsi rdi r8 r9 r10 r11(9个)
21
- // 被调用者保留:rbx r12 r13 r14 r15(5个)
22
- // 合计可分配 14 个整数寄存器;但保留几个给栈帧/参数传递,这里用 13 个。
23
- const X86_INT_REGS = ['rax','rcx','rdx','rsi','rdi','r8','r9','r10','r11','rbx','r12','r13','r14'];
24
- const X86_FP_REGS = ['xmm0','xmm1','xmm2','xmm3','xmm4','xmm5','xmm6','xmm7',
25
- 'xmm8','xmm9','xmm10','xmm11','xmm12','xmm13','xmm14'];
26
-
27
- /**
28
- * 计算每个变量的【全局编号顺序】:
29
- * 按 def 点在整个函数中的位置编号。
30
- */
31
- function numberInstructions(fn) {
32
- let n = 0;
33
- for (const blk of fn.blocks) {
34
- blk._start = n;
35
- for (const instr of blk.instrs) { instr._num = n++; }
36
- blk._end = n;
37
- }
38
- fn._totalInstrs = n;
39
- }
40
-
41
- /**
42
- * 计算每个变量的活跃区间。
43
- * 简化:线性扫描,每个变量记 [firstDef, lastUse]。
44
- */
45
- function computeLiveIntervals(fn) {
46
- numberInstructions(fn);
47
- const intervals = new Map(); // varName -> {v, start, end, blocks:Set}
48
- const ensure = (v) => {
49
- if (!intervals.has(v)) intervals.set(v, { v, start: fn._totalInstrs, end: -1, blocks: new Set() });
50
- return intervals.get(v);
51
- };
52
- // 参数从入口块 0 开始活跃
53
- for (const p of fn.params) {
54
- const iv = ensure(p); iv.start = 0; iv.blocks.add(0);
55
- }
56
- for (const blk of fn.blocks) {
57
- for (const instr of blk.instrs) {
58
- const d = I.instrDef(instr);
59
- if (d) {
60
- const iv = ensure(d);
61
- iv.start = Math.min(iv.start, instr._num);
62
- iv.blocks.add(blk.id);
63
- }
64
- for (const u of I.instrUse(instr)) {
65
- const iv = ensure(u);
66
- iv.end = Math.max(iv.end, instr._num);
67
- iv.blocks.add(blk.id);
68
- }
69
- }
70
- for (const u of I.termUse(blk.term)) {
71
- const iv = ensure(u);
72
- iv.end = Math.max(iv.end, blk._end);
73
- iv.blocks.add(blk.id);
74
- }
75
- }
76
- return [...intervals.values()];
77
- }
78
-
79
- /**
80
- * 区间重叠检测:两个活跃区间是否干涉。
81
- * [s1,e1] 与 [s2,e2]:若 s1 <= e2 && s2 <= e1 即重叠。
82
- * 注意:严格的 SSA 里 def 点之后才算活跃,这里用半开区间 [start, end] 保守重叠。
83
- */
84
- function interfere(a, b) {
85
- // 同一个变量不干涉自己
86
- if (a.v === b.v) return false;
87
- return a.start <= b.end && b.start <= a.end;
88
- }
89
-
90
- /**
91
- * Chaitin-Briggs 图着色
92
- * @param {Array} intervals - 活跃区间列表
93
- * @param {number} k - 可用寄存器数
94
- * @param {Array} moveInstrs - mov 指令列表 [dst, src] 用于合并
95
- */
96
- function colorGraph(intervals, k, moveInstrs) {
97
- const adj = new Map(); // var -> Set(var)
98
- for (const iv of intervals) adj.set(iv.v, new Set());
99
- // 建边
100
- for (let i = 0; i < intervals.length; i++) {
101
- for (let j = i + 1; j < intervals.length; j++) {
102
- if (interfere(intervals[i], intervals[j])) {
103
- adj.get(intervals[i].v).add(intervals[j].v);
104
- adj.get(intervals[j].v).add(intervals[i].v);
105
- }
106
- }
107
- }
108
- // 合并候选:mov dst,src 若不干涉,记为约束(同色)
109
- const coalesce = new Map(); // v -> v'
110
- for (const [dst, src] of moveInstrs) {
111
- if (!adj.has(dst) || !adj.has(src)) continue;
112
- if (!adj.get(dst).has(src)) coalesce.set(dst, src);
113
- }
114
-
115
- const degree = (v) => adj.get(v).size;
116
- const removed = new Set();
117
- const stack = [];
118
-
119
- // 简化阶段
120
- let changed = true;
121
- while (changed) {
122
- changed = false;
123
- // 1) 先合并:度数低的 mov 对直接合并
124
- for (const [a, b] of coalesce) {
125
- if (removed.has(a) || removed.has(b)) continue;
126
- // a 并入 b:删除 a,所有 a 的邻居边转到 b(保守,不转,直接合并)
127
- removed.add(a);
128
- stack.push(a);
129
- changed = true;
130
- }
131
- // 2) 度数 < k 的节点压栈
132
- for (const iv of intervals) {
133
- const v = iv.v;
134
- if (removed.has(v)) continue;
135
- if (degree(v) < k) {
136
- removed.add(v);
137
- stack.push(v);
138
- changed = true;
139
- }
140
- }
141
- // 3) 还有没删的(度数>=k):选一个“代价最低”的溢出候选
142
- if (!changed) {
143
- const remaining = intervals.filter(iv => !removed.has(iv.v));
144
- if (remaining.length > 0) {
145
- // 选度数最大(最简单)作为溢出候选
146
- remaining.sort((a, b) => degree(b.v) - degree(a.v));
147
- const spill = remaining[0].v;
148
- removed.add(spill);
149
- stack.push(spill);
150
- changed = true;
151
- }
152
- }
153
- }
154
-
155
- // 选择阶段:弹出栈,着色
156
- const color = new Map(); // v -> color (int)
157
- const spilled = new Set();
158
- while (stack.length) {
159
- const v = stack.pop();
160
- // 收集邻居已用颜色
161
- const used = new Set();
162
- for (const n of adj.get(v)) if (color.has(n)) used.add(color.get(n));
163
- // 找第一个空闲颜色
164
- let c = 0;
165
- while (used.has(c)) c++;
166
- if (c < k) {
167
- color.set(v, c);
168
- } else {
169
- spilled.add(v);
170
- }
171
- // 处理合并:如果 v 被合并进 coalesce 里的某个目标,让它继承颜色
172
- if (coalesce.has(v) && color.has(coalesce.get(v))) {
173
- color.set(v, color.get(coalesce.get(v)));
174
- spilled.delete(v);
175
- }
176
- }
177
- return { color, spilled, adj };
178
- }
179
-
180
- /**
181
- * 函数级寄存器分配入口。
182
- * 返回: { color:Map<var, regName>, spilled:Set<var>, intRegs:[...], fpRegs:[...] }
183
- */
184
- function allocate(fn, numIntRegs = 13, numFpRegs = 15) {
185
- I.buildCFG(fn);
186
- const intervals = computeLiveIntervals(fn);
187
-
188
- // 收集 mov 指令用于合并
189
- const moves = [];
190
- for (const blk of fn.blocks) for (const instr of blk.instrs) {
191
- if (instr.op === 'mov') moves.push([instr.dest, instr.src]);
192
- }
193
-
194
- const { color, spilled } = colorGraph(intervals, numIntRegs, moves);
195
-
196
- // 把颜色号映射到具体寄存器名
197
- const regMap = new Map();
198
- for (const [v, c] of color) {
199
- regMap.set(v, X86_INT_REGS[c] || `stack_${v}`);
200
- }
201
- // 溢出变量分配到栈槽
202
- for (const v of spilled) regMap.set(v, `stack_${v}`);
203
-
204
- return {
205
- color: regMap,
206
- spilled: [...spilled],
207
- intRegs: X86_INT_REGS.slice(0, numIntRegs),
208
- fpRegs: X86_FP_REGS.slice(0, numFpRegs),
209
- };
210
- }
211
-
212
- module.exports = { allocate, computeLiveIntervals, colorGraph, interfere, X86_INT_REGS, X86_FP_REGS };
@@ -1,86 +0,0 @@
1
- // ============================================================
2
- // scheduler.js —— 基本块内指令调度(列表调度)
3
- //
4
- // 目标:减少流水线停顿。
5
- // 1. 构建数据依赖图:指令 i 依赖 j(j 的 def 是 i 的 use)
6
- // 2. 列出就绪指令(依赖已满足)
7
- // 3. 按“下游关键路径长度”优先选择就绪指令发射
8
- // 4. 避免 load/use 之间的 load-use 延迟(中间插一条不相关指令)
9
- // ============================================================
10
- 'use strict';
11
-
12
- const I = require('./ir');
13
-
14
- /**
15
- * 对单个基本块做指令调度。
16
- * 保持 phi 不动(必须块首),只调度非 phi、非终结符指令。
17
- */
18
- function scheduleBlock(blk) {
19
- const phis = blk.instrs.filter(i => i.op === 'phi');
20
- const body = blk.instrs.filter(i => i.op !== 'phi');
21
- if (body.length <= 1) { blk.instrs = [...phis, ...body]; return; }
22
-
23
- // 每条指令定义的变量 -> 指令在 body 中的下标
24
- const defPos = new Map();
25
- body.forEach((instr, i) => {
26
- const d = I.instrDef(instr);
27
- if (d) defPos.set(d, i);
28
- });
29
-
30
- // 依赖:body[i] 依赖 body[j](j 是 i 的某操作数的定义点)
31
- const depends = Array.from({ length: body.length }, () => new Set());
32
- const dependents = Array.from({ length: body.length }, () => new Set());
33
- body.forEach((instr, i) => {
34
- for (const u of I.instrUse(instr)) {
35
- const j = defPos.get(u);
36
- if (j != null && j !== i) {
37
- depends[i].add(j);
38
- dependents[j].add(i);
39
- }
40
- }
41
- });
42
-
43
- // 反向高度(关键路径长度):从每条指令到块尾的最长路径
44
- const height = new Array(body.length).fill(0);
45
- for (let i = body.length - 1; i >= 0; i--) {
46
- let max = 0;
47
- for (const d of dependents[i]) max = Math.max(max, height[d]);
48
- height[i] = max + 1;
49
- }
50
-
51
- // 列表调度
52
- const scheduled = [];
53
- const scheduledSet = new Set();
54
- const remainingDeps = depends.map(s => new Set(s));
55
-
56
- while (scheduled.length < body.length) {
57
- // 找就绪:remainingDeps[i] 为空
58
- let best = -1, bestPri = -1;
59
- for (let i = 0; i < body.length; i++) {
60
- if (scheduledSet.has(i)) continue;
61
- if (remainingDeps[i].size === 0) {
62
- if (height[i] > bestPri) { bestPri = height[i]; best = i; }
63
- }
64
- }
65
- if (best === -1) {
66
- // 有环(不应出现),按原顺序补
67
- for (let i = 0; i < body.length; i++) if (!scheduledSet.has(i)) { best = i; break; }
68
- }
69
- scheduled.push(body[best]);
70
- scheduledSet.add(best);
71
- // 把 best 从所有依赖它的指令的 remainingDeps 中删除
72
- for (const d of dependents[best]) remainingDeps[d].delete(best);
73
- }
74
-
75
- blk.instrs = [...phis, ...scheduled];
76
- }
77
-
78
- /**
79
- * 函数级调度
80
- */
81
- function scheduleFunction(fn, allocation) {
82
- for (const blk of fn.blocks) scheduleBlock(blk);
83
- return fn;
84
- }
85
-
86
- module.exports = { scheduleFunction, scheduleBlock };