Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Haley Sapphie Owsianko
ACC-project
Commits
3691e68f
Commit
3691e68f
authored
Mar 30, 2021
by
Sapphie
Browse files
Begin implementation of closure conversion
parent
239ea6e7
Changes
1
Hide whitespace changes
Inline
Side-by-side
compiler/src/l3/CPSValueRepresenter.scala
View file @
3691e68f
...
...
@@ -10,8 +10,7 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
def
apply
(
tree
:
H.Tree
)
:
L.Tree
=
tree
match
{
case
H
.
LetP
(
n
,
prim
,
args
,
body
)
=>
applyLetP
(
n
,
prim
,
args
,
body
)
case
H
.
LetF
(
funs
,
body
)
=>
val
lFuns
=
funs
.
map
(
f
=>
L
.
Fun
(
f
.
name
,
f
.
retC
,
f
.
args
,
apply
(
f
.
body
)))
L
.
LetF
(
lFuns
,
apply
(
body
))
transformLetF
(
funs
,
body
)
case
H
.
LetC
(
cnts
,
body
)
=>
val
lCnts
=
cnts
.
map
(
c
=>
L
.
Cnt
(
c
.
name
,
c
.
args
,
apply
(
c
.
body
)))
L
.
LetC
(
lCnts
,
apply
(
body
))
...
...
@@ -30,6 +29,43 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
case
_
=>
throw
new
Exception
(
"Unimplemented: "
+
tree
.
getClass
.
toString
)
}
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
{
case
Nil
=>
Nil
case
f
::
fs
=>
val
w
=
Symbol
.
fresh
(
"w"
)
val
envName
=
Symbol
.
fresh
(
"env"
)
val
newArgs
=
envName
+:
f
.
args
val
funBody
=
apply
(
f
.
body
)
// Get free variables for this function, then order them
val
fv
=
((
freeVars
(
f
.
body
)
-
f
.
name
)
--
f
.
args
).
toList
// Creates a letP
def
argsBindings
(
freeVars
:
Seq
[
Symbol
],
counter
:
Int
,
accSubst
:
Map
[
Symbol
,
Symbol
])
:
L.Tree
=
freeVars
match
{
case
Nil
=>
substitute
(
accSubst
)(
funBody
)
case
freeVar
::
vs
=>
// Bind the fresh variable v to a block get
val
v
=
Symbol
.
fresh
(
"v"
)
L
.
LetP
(
v
,
CPS
.
BlockGet
,
Seq
(
L
.
AtomN
(
envName
),
L
.
AtomL
(
counter
)),
argsBindings
(
vs
,
counter
+
1
,
accSubst
+
(
freeVar
->
v
)))
}
val
newFunBody
=
argsBindings
(
fv
,
1
,
Map
())
val
newFun
=
L
.
Fun
(
w
,
f
.
retC
,
newArgs
,
newFunBody
)
(
newFun
,
fv
)
+:
transformFunAbs
(
fs
)
}
val
funsAndVars
=
transformFunAbs
(
initialFuns
)
val
newBody
=
apply
(
body
)
// TODO: tree of letFs, tree of letPs to create the closure
???
}
def
substitute
(
subst
:
Map
[
Symbol
,
Symbol
])(
tree
:
L.Tree
)
:
L.Tree
=
???
private
def
transformIf
(
cond
:
L3TestPrimitive
,
args
:
Seq
[
H.Atom
],
thenC
:
H.Name
,
elseC
:
H.Name
)
:
L.Tree
=
{
def
maskAndCheck
(
numBits
:
Int
,
target
:
Bits32
)
:
L.LetP
=
{
val
Seq
(
x
)
=
args
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment