Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ACC-project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Haley Sapphie Owsianko
ACC-project
Commits
b149a8dc
Commit
b149a8dc
authored
3 years ago
by
Sapphie
Browse files
Options
Downloads
Patches
Plain Diff
Implement Closure Conversion
parent
77081479
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
compiler/src/l3/CPSValueRepresenter.scala
+34
-11
34 additions, 11 deletions
compiler/src/l3/CPSValueRepresenter.scala
with
34 additions
and
11 deletions
compiler/src/l3/CPSValueRepresenter.scala
+
34
−
11
View file @
b149a8dc
...
...
@@ -15,10 +15,10 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
val
lCnts
=
cnts
.
map
(
c
=>
L
.
Cnt
(
c
.
name
,
c
.
args
,
apply
(
c
.
body
)))
L
.
LetC
(
lCnts
,
apply
(
body
))
case
H
.
AppF
(
fun
,
retC
,
args
)
=>
// Assuming a function is just a pointer to its first instruction
val
lFun
=
rewrite
(
fun
)
val
l
Args
=
args
.
map
(
rewrite
)
L
.
AppF
(
lFun
,
r
et
C
,
l
Args
)
val
f
=
Symbol
.
fresh
(
"f"
)
val
newBody
=
L
.
AppF
(
L
.
AtomN
(
f
),
retC
,
rewrite
(
fun
)
+:
args
.
map
(
rewrite
)
)
val
new
Args
=
Seq
(
rewrite
(
fun
),
L
.
AtomL
(
0
)
)
L
.
LetP
(
f
,
CPS
.
BlockG
et
,
new
Args
,
newBody
)
case
H
.
AppC
(
cnt
,
args
)
=>
val
lArgs
=
args
.
map
(
rewrite
)
L
.
AppC
(
cnt
,
lArgs
)
...
...
@@ -32,10 +32,11 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
private
def
transformLetF
(
initialFuns
:
Seq
[
H.Fun
],
body
:
H.Tree
)
:
L.LetF
=
{
// for each function, closes it and returns all the variables that used be free in it
def
transformFunAbs
(
funs
:
Seq
[
H.Fun
])
:
Seq
[(
L.Fun
,
Seq
[
Symbol
])]
=
funs
match
{
// as well as the associated worker function
def
transformFunAbs
(
funs
:
Seq
[
H.Fun
])
:
Seq
[(
L.Fun
,
Seq
[
Symbol
]
,
Symbol
)]
=
funs
match
{
case
Nil
=>
Nil
case
f
::
fs
=>
val
w
=
Symbol
.
fresh
(
"w"
)
val
w
orkerFun
=
Symbol
.
fresh
(
"w"
)
val
envName
=
Symbol
.
fresh
(
"env"
)
val
newArgs
=
envName
+:
f
.
args
val
funBody
=
apply
(
f
.
body
)
...
...
@@ -54,14 +55,36 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
}
val
newFunBody
=
argsBindings
(
fv
,
1
,
Map
())
val
newFun
=
L
.
Fun
(
w
,
f
.
retC
,
newArgs
,
newFunBody
)
(
newFun
,
fv
)
+:
transformFunAbs
(
fs
)
val
newFun
=
L
.
Fun
(
workerFun
,
f
.
retC
,
newArgs
,
newFunBody
)
(
newFun
,
fv
,
workerFun
)
+:
transformFunAbs
(
fs
)
}
def
allocInitFun
(
funsAndVars
:
Seq
[(
L.Fun
,
Seq
[
Symbol
]
,
Symbol
)],
lastBody
:
L.Tree
)
:
L.Tree
=
{
def
initFun
(
remVars
:
Seq
[
Symbol
],
counter
:
Int
,
rest
:
Seq
[(
L.Fun
,
Seq
[
Symbol
]
,
Symbol
)])
:
L.Tree
=
remVars
match
{
case
Nil
=>
allocInitFun
(
rest
,
lastBody
)
case
v
::
vs
=>
val
nextBody
=
initFun
(
vs
,
counter
+
1
,
rest
)
val
args
=
Seq
(
L
.
AtomL
(
counter
),
L
.
AtomN
(
v
))
L
.
LetP
(
Symbol
.
fresh
(
"blockset_unused"
),
CPS
.
BlockSet
,
args
,
nextBody
)
}
funsAndVars
match
{
case
Nil
=>
lastBody
case
(
fun
,
vars
,
workerFun
)
::
rest
=>
val
varInits
=
initFun
(
vars
,
1
,
rest
)
val
t1
=
Symbol
.
fresh
(
"t1"
)
val
primArgs
=
Seq
(
L
.
AtomL
(
0
),
L
.
AtomN
(
workerFun
))
val
nextBody
=
L
.
LetP
(
t1
,
CPS
.
BlockSet
,
primArgs
,
varInits
)
L
.
LetP
(
fun
.
name
,
CPS
.
BlockAlloc
(
202
),
Seq
(
L
.
AtomL
(
vars
.
length
+
1
)),
nextBody
)
}
}
val
funsAndVars
=
transformFunAbs
(
initialFuns
)
val
newBody
=
apply
(
body
)
// TODO: tree of letFs, tree of letPs to create the closure
???
val
lastBody
=
apply
(
body
)
val
closureAllocInit
=
allocInitFun
(
funsAndVars
,
lastBody
)
L
.
LetF
(
funsAndVars
.
unzip3
.
_1
,
closureAllocInit
)
}
def
substitute
(
subst
:
Map
[
Symbol
,
Symbol
])(
tree
:
L.Tree
)
:
L.Tree
=
???
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment