Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
381 commits
Select commit Hold shift + click to select a range
bf718c8
update todos with more test cases and comments
ychenfo Mar 12, 2025
3f3fe05
cleanup and more tests
ychenfo Mar 12, 2025
5d96ecb
better usage of blocktransformer
ychenfo Mar 13, 2025
2268f3c
further fix to take care of the `rest`s from non-fusing parent match …
ychenfo Mar 13, 2025
41fd30c
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo Mar 14, 2025
7f85ac1
fix after merge: add val to public class fields; remove subst for blo…
ychenfo Mar 14, 2025
f117a68
another problematic test
ychenfo Mar 17, 2025
8991cd3
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo Mar 17, 2025
640cc59
more comments on tests
ychenfo Mar 18, 2025
87781c3
check and compare outputs from deforestated programs
ychenfo Mar 18, 2025
c5afea7
update test
ychenfo Mar 18, 2025
921665d
remove a useless field from CtorFinalDest.Match
ychenfo Mar 19, 2025
f901a80
add another test
ychenfo Mar 19, 2025
120d7be
wip: try to take care of matches from multiple levels up
ychenfo Mar 20, 2025
5615ba8
update test; need to clean undefined vars in dead code...
ychenfo Mar 21, 2025
6a664fc
better name; wip: tidy up computation of free vars
ychenfo Mar 24, 2025
7d9ce98
remove wrong fixmes; add more comments
ychenfo Mar 24, 2025
4d337fd
don't rewrite already rewritten blocks
ychenfo Mar 24, 2025
b258545
revisit free var computation and update comments...
ychenfo Mar 24, 2025
2373e06
improve `matchRest` impl
ychenfo Mar 25, 2025
58cc209
minor fix and a new test
ychenfo Mar 25, 2025
35679fe
include runtimeSymbol as always in scope
ychenfo Mar 25, 2025
d65224e
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo Mar 25, 2025
4614a0d
better handling for dflt arms
ychenfo Mar 27, 2025
d029daf
better names for fields of classes
ychenfo Mar 27, 2025
4ba1ea9
update fixme tests
ychenfo Mar 28, 2025
a855c0f
tests output program despite of undefined var
ychenfo Mar 29, 2025
a666b60
avoid creating needless `match_rest` functions
ychenfo Mar 29, 2025
8d8a83a
use extension method for resultId
ychenfo Mar 29, 2025
b61ea2e
update test: found code duplication; some problems are masked by chan…
ychenfo Mar 30, 2025
7a3cb55
another duplication
ychenfo Mar 31, 2025
f4c693f
fix for avoiding the previous duplication
ychenfo Mar 31, 2025
f86dc2c
minor
ychenfo Mar 31, 2025
f8c2fc5
use flatten to remove some dead code which contains free vars
ychenfo Mar 31, 2025
b648936
remove dead code which contains f.v.s in match `rest`
ychenfo Apr 1, 2025
5194522
return block unchanged instead of panic for unsupported cases
ychenfo Apr 1, 2025
4b27f05
wip: no need for two repl hosts
ychenfo Apr 2, 2025
0739bf6
better handling for noprod
ychenfo Apr 2, 2025
8bcb753
update test
ychenfo Apr 2, 2025
a16a2b7
minor fix
ychenfo Apr 2, 2025
31bc090
fusion stats
ychenfo Apr 2, 2025
0e68eec
improve jsbackenddiffmaker
ychenfo Apr 2, 2025
2486b4d
lessen output
ychenfo Apr 2, 2025
f75d2be
use locally; use `data class`
ychenfo Apr 2, 2025
b92abee
cleanup jsbackenddiffmaker
ychenfo Apr 3, 2025
3a47e3b
better code: avoid local `object`s
ychenfo Apr 3, 2025
33f4703
tests from meeting
ychenfo Apr 3, 2025
1553cc8
more comments
ychenfo Apr 3, 2025
ca9a63f
wip: more needs to be done for things like `id` to block fusion
ychenfo Apr 7, 2025
a3b4308
use of functions without defn now really blocks fusion
ychenfo Apr 7, 2025
ce79cb5
more helpful deforestation debug output from `tl.log`
ychenfo Apr 7, 2025
f0c022c
fresh type var for `throw` to avoid blocking fusion
ychenfo Apr 8, 2025
0e96f9e
more recursive tests
ychenfo Apr 8, 2025
418ee33
zip unzip tests
ychenfo Apr 9, 2025
c91409c
use infix `::`; more tests
ychenfo Apr 9, 2025
987e7bd
more tests
ychenfo Apr 9, 2025
2efe8c6
fix: match block now correctly contain the scrut as a f.v. (if it is …
ychenfo Apr 10, 2025
6ade5c3
update fix
ychenfo Apr 10, 2025
8f7639d
fix nested definitions using `definedVar`; more tests
ychenfo Apr 10, 2025
aafc673
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo Apr 10, 2025
2519a80
style: use `locally:`
ychenfo Apr 10, 2025
7e835e1
trailing whitespaces
ychenfo Apr 10, 2025
dbd9173
remove useless `locally`; add reified tests
ychenfo Apr 11, 2025
3125840
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo Apr 11, 2025
15d1524
minor
ychenfo Apr 15, 2025
56e2373
Merge branch 'hkmc2' into hkmc2-deforest
LPTK Apr 16, 2025
04bdcfb
Merge branch 'hkmc2' into hkmc2-deforest
LPTK Apr 22, 2025
5e19b31
Merge branch 'hkmc2' into hkmc2-deforest
LPTK Apr 22, 2025
51485b3
fix `Deforetation.scala`
ychenfo Apr 22, 2025
1048529
deduplicate logic in `JSBackendDiffMaker.scala`
ychenfo Apr 23, 2025
d7ce214
use identity for ResultId and remove global state
ychenfo Apr 24, 2025
48467a7
remove awkward type projection
ychenfo Apr 24, 2025
cadb80b
better documentation on the todo about ctor as function; properly blo…
ychenfo Apr 24, 2025
79e38c7
just use `ResultId`
ychenfo Apr 24, 2025
10c9d22
better handling of throw and instantiate
ychenfo Apr 24, 2025
daf8749
update inappropriate uses of `???` in `Deforestation.scala`
ychenfo Apr 25, 2025
793c57e
minor update on comment
ychenfo Apr 25, 2025
fd306ac
minor
ychenfo Apr 25, 2025
7edb825
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo Apr 26, 2025
e382b8e
wip: use linkedhashmap for determinism; mutate map instead of generat…
ychenfo Apr 27, 2025
8b2b77a
fix another nondeterministic behavior; add some tests
ychenfo Apr 27, 2025
f5da463
remove the useless ordering and the unnecessary lazy val which only s…
ychenfo Apr 27, 2025
e92a50f
cleanup
ychenfo Apr 27, 2025
a529545
do not silently discard extra parameter lists
ychenfo Apr 27, 2025
f4f7f95
clarify comment
ychenfo Apr 28, 2025
0e662c3
track where a ctor expr come from
ychenfo Apr 28, 2025
5d3c3ff
minor fix
ychenfo Apr 28, 2025
245a011
wip
ychenfo Apr 29, 2025
e56612e
remove unnecessary default arg; better type var name
ychenfo Apr 29, 2025
ef9c008
Merge branch 'hkmc2' into hkmc2-deforest
LPTK Apr 30, 2025
028f5f4
improve fusion clash resolving impl
ychenfo May 1, 2025
0546800
improve stratvar uid impl
ychenfo May 1, 2025
0da2822
further improve clash resolving impl
ychenfo May 1, 2025
817d408
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo May 1, 2025
53bccba
also track if a type var represent a call result
ychenfo May 2, 2025
c8455b9
wip
ychenfo May 2, 2025
b0b9c8d
wip: keep track of intermediate vars as ctor dests
ychenfo May 2, 2025
0bbce59
only track vars that represent call res
ychenfo May 5, 2025
06484e7
wip: find def dup chances
ychenfo May 5, 2025
fc63b1a
wip: still need to compute all upperBounds
ychenfo May 6, 2025
ee34529
minor cleanup
ychenfo May 6, 2025
e26a234
find def dup chance
ychenfo May 7, 2025
c7fe134
need a toMap to dedup
ychenfo May 7, 2025
3759331
improve later: flag for deforest def dup
ychenfo May 7, 2025
5951318
try to fix nondeterminism
ychenfo May 7, 2025
f9c216b
minor; update test
ychenfo May 7, 2025
b59f46c
ignore obvious recursive callres
ychenfo May 8, 2025
c457613
minor
ychenfo May 8, 2025
fe02fa7
cache some result
ychenfo May 8, 2025
3a4f269
improve where to check obvious recursive calls
ychenfo May 9, 2025
7c49c57
minor fix
ychenfo May 9, 2025
ee963ca
fix the passing of `inDef`
ychenfo May 9, 2025
85cc27f
another duplication chance
ychenfo May 9, 2025
d9d9ace
update comment to include an example for dead code using never assign…
ychenfo May 9, 2025
90841f9
Merge branch 'hkmc2-deforest' into hkmc2-def-duplicate
ychenfo May 9, 2025
d693b26
fix tests
ychenfo May 10, 2025
f5065fa
add stub implementation for `notOnlyOneCallSite` filtering of duplica…
ychenfo May 10, 2025
a07b201
more todos and tothinks...
ychenfo May 10, 2025
4fef072
rewrite find def dup chance
ychenfo May 11, 2025
5fa7a0d
fix nondeterminism
ychenfo May 11, 2025
82f7846
remove old impl
ychenfo May 11, 2025
d59d322
update tests
ychenfo May 11, 2025
f3b05d8
more updates to tests
ychenfo May 11, 2025
32696c5
wip: basic def dup impl with very rough driver code
ychenfo May 11, 2025
d0b60e8
also dup those inner calls whose result flows into the call results t…
ychenfo May 12, 2025
bb10714
update tests
ychenfo May 12, 2025
31a56ae
typo
ychenfo May 12, 2025
771c0c9
pipeline: deforest the duplicated program; update tests
ychenfo May 12, 2025
22415f9
to fix: fix dup of recursive calls
ychenfo May 12, 2025
f41f9b4
local defined vars should also be renewed when duplicating defns
ychenfo May 12, 2025
dc03d25
wip: cleanup; update tests
ychenfo May 12, 2025
702cdd7
update tests
ychenfo May 13, 2025
db1e49a
previous method wip
ychenfo May 13, 2025
83b5759
save some tests
ychenfo May 15, 2025
f491f7c
wip: polymorphism
ychenfo May 27, 2025
14d8a64
move instantiate function
ychenfo May 30, 2025
d489137
ctor keep track of instantiate id
ychenfo Jun 2, 2025
abc0333
wip: preparation traversal to store function bodies
ychenfo Jun 2, 2025
de255e8
allow multiple creation of Dtor strat since now strats can be duplicated
ychenfo Jun 2, 2025
5a73860
update instantiate
ychenfo Jun 3, 2025
1dfb527
do not add unnecessary constraints to the work list
ychenfo Jun 4, 2025
2f281f4
also include constraints from instantiating other functions into the …
ychenfo Jun 4, 2025
ecb2842
wip: instantiate accumulate call path for ctor
ychenfo Jun 4, 2025
e4a4d78
wip: ctor dests differentiate between instantiations
ychenfo Jun 4, 2025
588c155
correctly update indef
ychenfo Jun 5, 2025
080c25e
only solve constraints from top-level
ychenfo Jun 5, 2025
1561322
refactoring...
ychenfo Jun 8, 2025
ec3deae
wip: collector
ychenfo Jun 9, 2025
84f9590
wip: resolve clashes
ychenfo Jun 10, 2025
ac4ef44
fix
ychenfo Jun 10, 2025
0cc725d
fix: no check when collecting constraints, and inst duplicate things …
ychenfo Jun 10, 2025
1988f32
some new tests
ychenfo Jun 10, 2025
852cdc5
wip
ychenfo Jun 10, 2025
b48f3d4
fix
ychenfo Jun 10, 2025
208af14
small improvements
ychenfo Jun 10, 2025
5155e91
fix selection filter
ychenfo Jun 10, 2025
6a7392b
stable resultid when needed
ychenfo Jun 10, 2025
7548db4
tests
ychenfo Jun 10, 2025
677b9f0
distinguish instantiation id: Some(Nil) vs None
ychenfo Jun 11, 2025
7ae110f
wip
ychenfo Jun 11, 2025
6210ef2
more on cycle detection
ychenfo Jun 11, 2025
c1acf12
wip
ychenfo Jun 12, 2025
8c424eb
selections also have instantiation ids now
ychenfo Jun 13, 2025
f8ecafc
free vars traversers
ychenfo Jun 13, 2025
7d78d05
more lastWords instead of ???
ychenfo Jun 13, 2025
4702a5d
also precompute tmp vars for the fields of fusing ctors
ychenfo Jun 13, 2025
341730c
match arms and tests
ychenfo Jun 14, 2025
f15be70
transform
ychenfo Jun 14, 2025
96ba053
make new functions
ychenfo Jun 14, 2025
26e8ecb
some fixes to free vars
ychenfo Jun 15, 2025
705b0a3
better instantiation id suffix
ychenfo Jun 15, 2025
7409fe7
better logic
ychenfo Jun 15, 2025
53d915c
correct instantiatino id in rewrite
ychenfo Jun 15, 2025
41de79b
fix instantiation id for recursive calls; more tests
ychenfo Jun 15, 2025
cec7eed
minor
ychenfo Jun 15, 2025
74afb68
minor
ychenfo Jun 16, 2025
b67ff20
vars for outside symbols used in function bodies shouldn't be duplicated
ychenfo Jun 16, 2025
39cfebc
more fix to sym to strat vars; pipeline
ychenfo Jun 16, 2025
0f46035
fix the checking when handling match rests
ychenfo Jun 16, 2025
477d19b
fix fusion of selection
ychenfo Jun 16, 2025
9854d01
remove old deforestation
ychenfo Jun 17, 2025
a0c314c
new deforest runner
ychenfo Jun 17, 2025
6715757
handle functions nested in functions
ychenfo Jun 17, 2025
c26bfab
slightly improve sym to strat var
ychenfo Jun 18, 2025
168e1e1
more fixes for lambdas and nested functions; update tests
ychenfo Jun 19, 2025
9f85855
make it possible to know whether a symbol is for a function...?
ychenfo Jun 19, 2025
364298d
cleanup
ychenfo Jun 19, 2025
a72843f
wip
ychenfo Jun 19, 2025
555b7d1
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-def-duplicate-new
ychenfo Jun 19, 2025
dc083b4
new todos
ychenfo Jun 19, 2025
1e2509d
wip: noprod for arrays for now
ychenfo Jun 20, 2025
cc9eb03
minor
ychenfo Jun 20, 2025
917aafa
update new todos
ychenfo Jun 21, 2025
2a11373
rewrite handling of things inside modules
ychenfo Jun 22, 2025
775cdf2
wip: deforestation of import and nofib
ychenfo Jun 24, 2025
59aae7b
change from stand-up meeting
ychenfo Jun 25, 2025
6a4ba76
update tests about reordered nested functions
ychenfo Jun 25, 2025
690638e
new import semantics
ychenfo Jun 29, 2025
d61976e
use dummy term defn for identifying function blockmemsym for now
ychenfo Jun 30, 2025
c47c53e
Merge branch 'hkmc2-def-duplicate-new-import-semantics' into hkmc2-de…
ychenfo Jun 30, 2025
adffa62
selection on non-existing fields give noprod; use cached elaborated t…
ychenfo Jul 2, 2025
18f273e
cleanup
ychenfo Jul 2, 2025
4dd8fad
fuse tuple/array
ychenfo Jul 2, 2025
b714f4a
minor fix
ychenfo Jul 2, 2025
84284d9
minor fix and update some tests
ychenfo Jul 2, 2025
f134ef7
update tests
ychenfo Jul 4, 2025
57110b0
move data class lazy to predef; do not deforest when seeing spread ar…
ychenfo Jul 5, 2025
0096663
wip: better import
ychenfo Jul 7, 2025
49b6923
wip: try to look through lazy and force
ychenfo Jul 7, 2025
0b42e9e
wip: more nofib tests; fixes; see through lz
ychenfo Jul 8, 2025
ada7e68
wip: update tests
ychenfo Jul 12, 2025
0445418
wip: more tests to fix; cyclic strategy removal consider instantiatio…
ychenfo Jul 13, 2025
3de91fa
fixes: flatten; symbol for the class Lazy; deforest defined symbols
ychenfo Jul 14, 2025
e8f2b52
fix on imported functions referring to private field
ychenfo Aug 4, 2025
79de7ca
avoid generating more than one Ctor objects for the same combination …
ychenfo Aug 7, 2025
fce9895
update tests
ychenfo Aug 7, 2025
cbe6210
propagate NoProd through FieldSel
ychenfo Aug 10, 2025
a5cac6b
add one test
ychenfo Aug 11, 2025
f970651
fix incorrect in-place rewriting of mutually recursive exported funct…
ychenfo Aug 11, 2025
da46d27
update output mjs
ychenfo Aug 12, 2025
00992c6
manually add `<module-name>.` to work around "access before init" errors
ychenfo Aug 12, 2025
2127eaf
update tests
ychenfo Aug 12, 2025
cb7f4f0
free vars fix
ychenfo Aug 12, 2025
1e08caa
ctor dest match arm symbol replacement map fixes
ychenfo Aug 13, 2025
b7c3a1d
fix: avoid traversing the same fun body again
ychenfo Aug 14, 2025
3ac3039
multiple param list
ychenfo Aug 15, 2025
c9feaa5
fix the scc algo
ychenfo Aug 17, 2025
e8b1510
fix eliza
ychenfo Aug 17, 2025
9ac17f8
lift before deforest; no trace for the `reserveNames` function in jsb…
ychenfo Aug 18, 2025
353ed8c
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-def-duplicate-…
ychenfo Aug 19, 2025
065ab56
admit cyclic strategies
ychenfo Aug 19, 2025
5f643ef
update tests
ychenfo Aug 19, 2025
5eb4f57
update from meeting
ychenfo Aug 21, 2025
5f68982
tmp changes to build.sbt for adding profiling utilities
ychenfo Aug 21, 2025
676880f
fix slow filtering
ychenfo Aug 21, 2025
dce4242
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-def-duplicate-…
ychenfo Aug 23, 2025
a5c7d07
wip cleanup
ychenfo Aug 26, 2025
5ab74a2
fuse across blocks
ychenfo Aug 28, 2025
c66eaa1
fix: not changing owned val defs
ychenfo Aug 28, 2025
d990850
config for deforest
ychenfo Aug 28, 2025
8d31044
bench nofibs
ychenfo Aug 29, 2025
4a83c3c
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest-subproj
ychenfo Sep 12, 2025
90616ee
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest-subproj
ychenfo Sep 15, 2025
0655abc
a pass of cleanup
ychenfo Sep 15, 2025
60c572c
fix path; cleanup
ychenfo Sep 16, 2025
5097459
avoid two copies of the same file
ychenfo Sep 16, 2025
ed8f60b
cleanup tests
ychenfo Sep 16, 2025
c11eb72
cleanup
ychenfo Sep 16, 2025
7e83917
fix path again
ychenfo Sep 16, 2025
1879d79
Merge branch 'hkmc2' into hkmc2-deforest-new
LPTK Sep 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion hkmc2/jvm/src/test/scala/hkmc2/CompileTestRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ class CompileTestRunner

val preludePath = mainTestDir/"mlscript"/"decls"/"Prelude.mls"

given Config = Config.default
given Config =
if file.segments.contains("nofib-deforest") then
Config.default.copy(deforest = S(Config.Deforestation(
importedPublicModNames = Set("NofibPrelude"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
importedPublicModNames = Set("NofibPrelude"),
seethroughModules = Set("NofibPrelude"),

seeThroughLazySymbolsNames = Set("lazy"),
seeThroughForceSymbolsNames = Set("force")
)))
else
Config.default

val compiler = MLsCompiler(
preludePath,
Expand Down
8 changes: 8 additions & 0 deletions hkmc2/shared/src/main/scala/hkmc2/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ case class Config(
sanityChecks: Opt[SanityChecks],
effectHandlers: Opt[EffectHandlers],
liftDefns: Opt[LiftDefns],
deforest: Opt[Deforestation]
):

def stackSafety: Opt[StackSafety] = effectHandlers.flatMap(_.stackSafety)
Expand All @@ -28,6 +29,7 @@ object Config:
// sanityChecks = S(SanityChecks(light = true)),
effectHandlers = N,
liftDefns = N,
deforest = N
)

case class SanityChecks(light: Bool)
Expand All @@ -42,6 +44,12 @@ object Config:

case class LiftDefns() // there may be other settings in the future, having it as a case class now

case class Deforestation(
val importedPublicModNames: Set[String],
val seeThroughLazySymbolsNames: Set[String],
val seeThroughForceSymbolsNames: Set[String]
)

end Config


32 changes: 30 additions & 2 deletions hkmc2/shared/src/main/scala/hkmc2/MLsCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import hkmc2.semantics.Elaborator
import hkmc2.semantics.Resolver
import hkmc2.syntax.Keyword.`override`
import semantics.Elaborator.{Ctx, State}
import hkmc2.codegen.deforest.Deforest
import hkmc2.Config.LiftDefns


class ParserSetup(file: os.Path, dbgParsing: Bool)(using Elaborator.State, Raise):
Expand Down Expand Up @@ -37,7 +39,7 @@ class ParserSetup(file: os.Path, dbgParsing: Bool)(using Elaborator.State, Raise


// * The weird type of `mkOutput` is to allow wrapping the reporting of diagnostics in synchronized blocks
class MLsCompiler(preludeFile: os.Path, mkOutput: ((Str => Unit) => Unit) => Unit)(using Config):
class MLsCompiler(preludeFile: os.Path, mkOutput: ((Str => Unit) => Unit) => Unit)(using cfg: Config):

val runtimeFile: os.Path = preludeFile/os.up/os.up/os.up/"mlscript-compile"/"Runtime.mjs"
val termFile: os.Path = preludeFile/os.up/os.up/os.up/"mlscript-compile"/"Term.mjs"
Expand Down Expand Up @@ -94,6 +96,32 @@ class MLsCompiler(preludeFile: os.Path, mkOutput: ((Str => Unit) => Unit) => Uni
val jsb = ltl.givenIn:
codegen.js.JSBuilder()
val le = low.program(blk)

val lowered =
if cfg.deforest.isEmpty then
le
else
val deforestLow = ltl.givenIn:
cfg.copy(liftDefns = S(LiftDefns())).givenIn:
new codegen.Lowering()
val deforestResult = Deforest.apply(deforestLow.program(blk), wd)(using
cfg,
ltl,
raise,
newCtx,
new Deforest.State(),
State,
preludeFile)
deforestResult match
case Right(msg) =>
println(msg) // TODO: no println
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
println(msg) // TODO: no println

le
case Left(prog -> summary -> detail) =>
Copy link
Contributor

@LPTK LPTK Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't build a summary string even when not used – use a lambda?

// if summary.nonEmpty then
// println("-----summary-----")
// println(summary.mapLines(l => s"\t$l"))
prog

val baseScp: utils.Scope =
utils.Scope.empty
// * This line serves for `import.meta.url`, which retrieves directory and file names of mjs files.
Expand All @@ -103,7 +131,7 @@ class MLsCompiler(preludeFile: os.Path, mkOutput: ((Str => Unit) => Unit) => Uni
val nme = file.baseName
val exportedSymbol = parsed.definedSymbols.find(_._1 === nme).map(_._2)
val je = nestedScp.givenIn:
jsb.program(le, exportedSymbol, wd)
jsb.program(lowered, exportedSymbol, wd)
val jsStr = je.stripBreaks.mkString(100)
val out = file / os.up / (file.baseName + ".mjs")
os.write.over(out, jsStr)
Expand Down
1 change: 1 addition & 0 deletions hkmc2/shared/src/main/scala/hkmc2/Uid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object Uid:
curUid
def reset = curUid = -1
object Symbol extends Handler[semantics.Symbol]
object StratVar extends Handler[codegen.deforest.StratVarState]

extension [T] (x: Uid[T])
def <=(rhs: Uid[T]) = x <= rhs
Expand Down
25 changes: 23 additions & 2 deletions hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,18 @@ sealed abstract class Block extends Product:
case f@FunDefn(owner, sym, params, body) =>
val newBody = body.flattened
if newBody is body then f else f.copy(body = newBody)
if (newPreCtor is c.preCtor) && (newCtor is c.ctor) && (newMethods is c.methods)
val newCompanion = c.companion.mapConserve:
case b@ClsLikeBody(isym, methods, privateFields, publicFields, ctor) =>
val newMethods = methods.mapConserve:
case f@FunDefn(owner, sym, params, body) =>
val newBody = body.flattened
if newBody is body then f else f.copy(body = newBody)
val newCtor = ctor.flattened
if (newMethods is methods) && (newCtor is ctor) then b
else b.copy(methods = newMethods, ctor = newCtor)
if (newPreCtor is c.preCtor) && (newCtor is c.ctor) && (newMethods is c.methods) && (newCompanion is c.companion)
then c
else c.copy(preCtor = newPreCtor, ctor = newCtor, methods = newMethods)
else c.copy(preCtor = newPreCtor, ctor = newCtor, methods = newMethods, companion = newCompanion)

val newRest = rest.flatten(k)
if (newDefn is defn) && (newRest is rest)
Expand Down Expand Up @@ -478,6 +487,11 @@ enum Case:

sealed trait TrivialResult extends Result

object Result:
opaque type ResultId = Int
private def ResultId(v: Int): ResultId = v


sealed abstract class Result extends AutoLocated:
// // * Used for debugging locations:
// sealed abstract class Result extends AutoLocated with ProductWithExtraInfo:
Expand Down Expand Up @@ -538,6 +552,13 @@ sealed abstract class Result extends AutoLocated:
args.flatMap(arg => arg.idx.fold(Set.empty)(_.freeVarsLLIR) ++ arg.value.freeVarsLLIR).toSet
case DynSelect(qual, fld, arrayIdx) => qual.freeVarsLLIR ++ fld.freeVarsLLIR

def uid =
import Result.*
val uidValue = ResultId(System.identityHashCode(this))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not guaranteed to be unique. Use the Uid definitions.

uidValue



// type Local = LocalSymbol
type Local = Symbol

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object LambdaRewriter:

def rewriteOneBlk(b: Block) = b match
case Assign(lhs, Value.Lam(params, body), rest) if !lhs.isInstanceOf[TempSymbol] =>
val newSym = BlockMemberSymbol(lhs.nme, Nil,
val newSym = BlockMemberSymbol(lhs.nme, syntax.Tree.DummyTermDef(syntax.Fun) :: Nil,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document why the dummy Fun.

nameIsMeaningful = true // TODO: lhs.nme is not always meaningful
)
val blk = blockBuilder
Expand All @@ -26,7 +26,10 @@ object LambdaRewriter:
val lambdaRewriter = new BlockDataTransformer(SymbolSubst()):
override def applyValue(v: Value): Value = v match
case lam: Value.Lam =>
val sym = BlockMemberSymbol("lambda", Nil, nameIsMeaningful = false)
val sym = BlockMemberSymbol(
"lambda",
syntax.Tree.DummyTermDef(syntax.Fun) :: Nil,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document why the dummy Fun.

nameIsMeaningful = false)
lambdasList ::= (sym -> super.applyLam(lam))
Value.Ref(sym)
case _ => super.applyValue(v)
Expand Down
8 changes: 4 additions & 4 deletions hkmc2/shared/src/main/scala/hkmc2/codegen/Lifter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ object Lifter:
def getVars(d: Defn)(using state: State): Set[Local] = d match
case f: FunDefn =>
(f.body.definedVars ++ f.params.flatMap(_.paramSyms)).collect:
case s: FlowSymbol if !(s is state.runtimeSymbol) => s
case s: FlowSymbol if !(s is state.runtimeSymbol) => s // FIXME: doesn't this test always fail?
case c: ClsLikeDefn =>
val companionVars = c.companion.fold(Set.empty)(_.ctor.definedVars)
(companionVars ++ c.preCtor.definedVars ++ c.ctor.definedVars).collect:
Expand Down Expand Up @@ -551,9 +551,9 @@ class Lifter(handlerPaths: Opt[HandlerPaths])(using State, Raise):
val fakeCtorBms = d match
case c: ClsLikeDefn if !modLocal.isDefined => S(BlockMemberSymbol(d.sym.nme + "$ctor", Nil))
case _ => N
val singleCallBms = BlockMemberSymbol(d.sym.nme + "$", Nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎


val singleCallBms = BlockMemberSymbol(d.sym.nme + "$", Tree.DummyTermDef(syntax.Fun) :: Nil)

val info = LiftedInfo(
includedCaptures, includedLocals, clsCaptures,
refBms, fakeCtorBms, singleCallBms
Expand Down
4 changes: 3 additions & 1 deletion hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ class Lowering()(using Config, TL, Raise, State, Ctx):
case st.Asc(lhs, rhs) =>
term(lhs, inStmtPos = inStmtPos)(k)
case st.Tup(fs) =>
args(fs)(args => k(Value.Arr(mut = false, args)))
args(fs): args =>
val tmpSym = TempSymbol(N, "arr")
Assign(tmpSym, Value.Arr(mut = false, args), k(Value.Ref(tmpSym)))
Comment on lines +314 to +316
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really needed? At the very least, we shouldn't do it when the array is immediately returned or already assigned to a variable.

case Mut(st.Tup(fs)) =>
args(fs)(args => k(Value.Arr(mut = true, args)))
case st.CtxTup(fs) =>
Expand Down
Loading
Loading