-
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.
Transform JuvixReg into SSA form (#2646)
* Closes #2560 * Adds a transformation of JuvixReg into SSA form. * Adds an "output variable" field to branching instructions (`Case`, `Branch`) which indicates the output variable to which the result is assigned in both branches. The output variable corresponds to top of stack in JuvixAsm after executing the branches. In the SSA transformation, differently renamed output variables are unified by inserting assignment instructions at the end of branches. * Adds tests for the SSA transformation. * Depends on #2641.
- Loading branch information
Showing
21 changed files
with
522 additions
and
74 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
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,58 @@ | ||
module Juvix.Compiler.Reg.Data.IndexMap where | ||
|
||
import Data.HashMap.Strict qualified as HashMap | ||
import Juvix.Compiler.Reg.Language.Base hiding (lookup) | ||
|
||
data IndexMap k = IndexMap | ||
{ _indexMapFirstFree :: Int, | ||
_indexMapTable :: HashMap k Index | ||
} | ||
|
||
makeLenses ''IndexMap | ||
|
||
instance (Hashable k) => Semigroup (IndexMap k) where | ||
m1 <> m2 = | ||
IndexMap | ||
{ _indexMapTable = m1 ^. indexMapTable <> m2 ^. indexMapTable, | ||
_indexMapFirstFree = max (m1 ^. indexMapFirstFree) (m2 ^. indexMapFirstFree) | ||
} | ||
|
||
instance (Hashable k) => Monoid (IndexMap k) where | ||
mempty = | ||
IndexMap | ||
{ _indexMapFirstFree = 0, | ||
_indexMapTable = mempty | ||
} | ||
|
||
assign :: (Hashable k) => IndexMap k -> k -> (Index, IndexMap k) | ||
assign IndexMap {..} k = | ||
( _indexMapFirstFree, | ||
IndexMap | ||
{ _indexMapFirstFree = _indexMapFirstFree + 1, | ||
_indexMapTable = HashMap.insert k _indexMapFirstFree _indexMapTable | ||
} | ||
) | ||
|
||
lookup' :: (Hashable k) => IndexMap k -> k -> Maybe Index | ||
lookup' IndexMap {..} k = HashMap.lookup k _indexMapTable | ||
|
||
lookup :: (Hashable k) => IndexMap k -> k -> Index | ||
lookup mp = fromJust . lookup' mp | ||
|
||
combine :: forall k. (Hashable k) => IndexMap k -> IndexMap k -> IndexMap k | ||
combine mp1 mp2 = | ||
IndexMap | ||
{ _indexMapFirstFree = max (mp1 ^. indexMapFirstFree) (mp2 ^. indexMapFirstFree), | ||
_indexMapTable = mp | ||
} | ||
where | ||
mp = | ||
foldr | ||
(\k -> HashMap.update (checkVal k) k) | ||
(HashMap.intersection (mp1 ^. indexMapTable) (mp2 ^. indexMapTable)) | ||
(HashMap.keys (mp2 ^. indexMapTable)) | ||
|
||
checkVal :: k -> Index -> Maybe Index | ||
checkVal k idx | ||
| lookup mp2 k == idx = Just idx | ||
| otherwise = Nothing |
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 |
---|---|---|
|
@@ -10,3 +10,6 @@ strCairoPipeline = "pipeline-cairo" | |
|
||
strIdentity :: Text | ||
strIdentity = "identity" | ||
|
||
strSSA :: Text | ||
strSSA = "ssa" |
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,10 @@ | ||
module Juvix.Compiler.Reg.Extra | ||
( module Juvix.Compiler.Reg.Extra.Base, | ||
module Juvix.Compiler.Reg.Extra.Recursors, | ||
module Juvix.Compiler.Reg.Extra.Info, | ||
) | ||
where | ||
|
||
import Juvix.Compiler.Reg.Extra.Base | ||
import Juvix.Compiler.Reg.Extra.Info | ||
import Juvix.Compiler.Reg.Extra.Recursors |
Oops, something went wrong.