-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
184 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Juvix.Compiler.Tree.Data.CallGraph where | ||
|
||
import Data.HashSet qualified as HashSet | ||
import Juvix.Compiler.Tree.Data.InfoTable | ||
import Juvix.Compiler.Tree.Extra.Recursors | ||
|
||
-- | Call graph type | ||
type CallGraph = DependencyInfo Symbol | ||
|
||
-- | Compute the call graph | ||
createCallGraph :: InfoTable -> CallGraph | ||
createCallGraph tab = createDependencyInfo (createCallGraphMap tab) startVertices | ||
where | ||
startVertices :: HashSet Symbol | ||
startVertices = HashSet.fromList syms | ||
|
||
syms :: [Symbol] | ||
syms = maybe [] singleton (tab ^. infoMainFunction) | ||
|
||
createCallGraphMap :: InfoTable -> HashMap Symbol (HashSet Symbol) | ||
createCallGraphMap tab = fmap (getFunSymbols . (^. functionCode)) (tab ^. infoFunctions) | ||
|
||
getFunSymbols :: Node -> HashSet Symbol | ||
getFunSymbols = gather go mempty | ||
where | ||
go :: HashSet Symbol -> Node -> HashSet Symbol | ||
go syms = \case | ||
AllocClosure NodeAllocClosure {..} -> HashSet.insert _nodeAllocClosureFunSymbol syms | ||
Call NodeCall {..} -> goCallType syms _nodeCallType | ||
_ -> syms | ||
|
||
goCallType :: HashSet Symbol -> CallType -> HashSet Symbol | ||
goCallType syms = \case | ||
CallFun sym -> HashSet.insert sym syms | ||
CallClosure {} -> syms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Juvix/Compiler/Tree/Transformation/FilterUnreachable.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Juvix.Compiler.Tree.Transformation.FilterUnreachable where | ||
|
||
import Data.HashMap.Strict qualified as HashMap | ||
import Juvix.Compiler.Tree.Data.CallGraph | ||
import Juvix.Compiler.Tree.Data.InfoTable | ||
import Juvix.Prelude | ||
|
||
filterUnreachable :: InfoTable -> InfoTable | ||
filterUnreachable tab = | ||
over infoFunctions (HashMap.filterWithKey (const . isReachable graph)) tab | ||
where | ||
graph = createCallGraph tab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module Tree.Transformation.Reachability (allTests) where | ||
|
||
import Base | ||
import Data.HashMap.Strict qualified as HashMap | ||
import Juvix.Compiler.Tree.Transformation as Tree | ||
import Tree.Eval.Positive qualified as Eval | ||
import Tree.Transformation.Base | ||
|
||
data ReachabilityTest = ReachabilityTest | ||
{ _reachabilityTestReachable :: [Text], | ||
_reachabilityTestEval :: Eval.PosTest | ||
} | ||
|
||
allTests :: TestTree | ||
allTests = | ||
testGroup "Reachability" $ | ||
map liftTest rtests | ||
|
||
rtests :: [ReachabilityTest] | ||
rtests = | ||
[ ReachabilityTest | ||
{ _reachabilityTestReachable = ["f", "f'", "g'", "h", "h'", "main"], | ||
_reachabilityTestEval = | ||
Eval.PosTest | ||
"Test001: Reachability" | ||
$(mkRelDir "reachability") | ||
$(mkRelFile "test001.jvt") | ||
$(mkRelFile "out/test001.out") | ||
}, | ||
ReachabilityTest | ||
{ _reachabilityTestReachable = ["f", "g", "id", "sum", "main"], | ||
_reachabilityTestEval = | ||
Eval.PosTest | ||
"Test002: Reachability with loops & closures" | ||
$(mkRelDir "reachability") | ||
$(mkRelFile "test002.jvt") | ||
$(mkRelFile "out/test002.out") | ||
} | ||
] | ||
|
||
liftTest :: ReachabilityTest -> TestTree | ||
liftTest ReachabilityTest {..} = | ||
fromTest | ||
Test | ||
{ _testTransformations = [Tree.FilterUnreachable], | ||
_testAssertion = \tab -> unless (nubSort (map (^. functionName) (HashMap.elems (tab ^. infoFunctions))) == nubSort _reachabilityTestReachable) (error "check reachable"), | ||
_testEval = _reachabilityTestEval | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
5051 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
function h(integer) : integer; | ||
function h'(integer) : integer; | ||
function f(integer) : integer; | ||
function f'(integer) : integer; | ||
function g(integer) : integer; | ||
function g'(integer) : integer; | ||
function main() : integer; | ||
|
||
function h(integer) : integer { | ||
arg[0] | ||
} | ||
|
||
function h'(integer) : integer { | ||
arg[0] | ||
} | ||
|
||
function f(integer) : integer { | ||
add(call[h](arg[0]), 1) | ||
} | ||
|
||
function f'(integer) : integer { | ||
add(call[h'](arg[0]), 1) | ||
} | ||
|
||
function g(integer) : integer { | ||
add(call[f](arg[0]), 2) | ||
} | ||
|
||
function g'(integer) : integer { | ||
call[f'](arg[0]) | ||
} | ||
|
||
function main() : integer { | ||
call[g'](call[f](7)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
function f(*, integer) : integer; | ||
function id(integer) : integer; | ||
function g(integer) : integer; | ||
function sum(integer) : integer; | ||
function g'(integer) : integer; | ||
function g''(integer) : integer; | ||
function main() : integer; | ||
|
||
function f(*, integer) : integer { | ||
call(arg[0], arg[1]) | ||
} | ||
|
||
function id(integer) : integer { | ||
arg[0] | ||
} | ||
|
||
function g(integer) : integer { | ||
add(call[f](calloc[id](), arg[0]), 1) | ||
} | ||
|
||
function sum(integer) : integer { | ||
br(eq(0, arg[0])) { | ||
true: call[g](0) | ||
false: add(arg[0], call[sum](sub(arg[0], 1))) | ||
} | ||
} | ||
|
||
function g'(integer) : integer { | ||
add(call[id](arg[0]), 2) | ||
} | ||
|
||
function g''(integer) : integer { | ||
call[sum](arg[0]) | ||
} | ||
|
||
function main() : integer { | ||
call[sum](100) | ||
} |