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
aa62f3c2
Commit
aa62f3c2
authored
Apr 30, 2021
by
Sapphie
Browse files
Implement constant folding for value primitives
parent
c6f5bb5f
Changes
2
Hide whitespace changes
Inline
Side-by-side
compiler/src/l3/CPSOptimizer.scala
View file @
aa62f3c2
...
...
@@ -105,12 +105,21 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
case
LetP
(
name
,
prim
,
args
,
body
)
=>
{
val
replacedArgs
=
replaceArgs
(
args
,
s
)
val
newState
=
s
.
withExp
(
name
,
prim
,
replacedArgs
)
val
shrunkBody
=
shrink
(
body
,
newState
)
lazy
val
shrunkBody
=
shrink
(
body
,
newState
)
if
(
s
.
dead
(
name
)
&&
!
impure
(
prim
))
{
shrunkBody
}
else
{
LetP
(
name
,
prim
,
replacedArgs
,
shrunkBody
)
val
allLitOpt
=
replacedArgs
.
map
(
_
.
asLiteral
)
val
isAllLit
=
allLitOpt
.
forall
(
_
.
isDefined
)
lazy
val
asLit
=
allLitOpt
.
map
(
_
.
get
)
if
(
isAllLit
&&
vEvaluator
.
isDefinedAt
((
prim
,
asLit
)))
{
val
newNewState
=
newState
.
withASubst
(
name
,
vEvaluator
((
prim
,
asLit
)))
val
newBody
=
shrink
(
body
,
newNewState
)
newBody
}
else
{
LetP
(
name
,
prim
,
replacedArgs
,
shrunkBody
)
}
}
}
...
...
compiler/src/l3/Main.scala
View file @
aa62f3c2
...
...
@@ -11,6 +11,7 @@ import CPSTreeChecker._ // Implicits required for CPS tree checking
object
Main
{
def
main
(
args
:
Array
[
String
])
:
Unit
=
{
val
stats
=
new
Statistics
()
val
backEnd
:
Tree
=>
TerminalPhaseResult
=
(
CL3ToCPSTranslator
andThen
treePrinter
(
"---------- After CPS translation"
)
...
...
@@ -25,7 +26,7 @@ object Main {
andThen
CPSOptimizerLow
andThen
treePrinter
(
"---------- After Optimization (Low)"
)
andThen
treeChecker
andThen
CPSInterpreterLow
andThen
(
new
CPSInterpreterLow
(
stats
.
log
_
))
)
...
...
@@ -37,6 +38,7 @@ object Main {
.
flatMap
(
backEnd
)
match
{
case
Right
((
retCode
,
maybeMsg
))
=>
maybeMsg
foreach
println
println
(
stats
)
sys
.
exit
(
retCode
)
case
Left
(
errMsg
)
=>
println
(
s
"Error: $errMsg"
)
...
...
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