-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implements JuvixReg recursors, which will allow to implement JuvixReg transformations more succinctly and effectively. * Adds a transformation framework to JuvixReg. * Adds identity transformation tests. * Depends on #2635
- Loading branch information
Showing
27 changed files
with
398 additions
and
69 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 |
---|---|---|
@@ -1,15 +1,30 @@ | ||
module Commands.Dev.Reg.Read.Options where | ||
|
||
import CommonOptions | ||
import Juvix.Compiler.Reg.Data.TransformationId | ||
|
||
newtype RegReadOptions = RegReadOptions | ||
{ _regReadInputFile :: AppPath File | ||
data RegReadOptions = RegReadOptions | ||
{ _regReadTransformations :: [TransformationId], | ||
_regReadRun :: Bool, | ||
_regReadNoPrint :: Bool, | ||
_regReadInputFile :: AppPath File | ||
} | ||
deriving stock (Data) | ||
|
||
makeLenses ''RegReadOptions | ||
|
||
parseRegReadOptions :: Parser RegReadOptions | ||
parseRegReadOptions = do | ||
_regReadInputFile <- parseInputFile FileExtJuvixAsm | ||
_regReadNoPrint <- | ||
switch | ||
( long "no-print" | ||
<> help "Do not print the transformed code" | ||
) | ||
_regReadRun <- | ||
switch | ||
( long "run" | ||
<> help "Run the code after the transformation" | ||
) | ||
_regReadTransformations <- optRegTransformationIds | ||
_regReadInputFile <- parseInputFile FileExtJuvixReg | ||
pure RegReadOptions {..} |
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,38 @@ | ||
module Juvix.Compiler.Reg.Data.TransformationId where | ||
|
||
import Juvix.Compiler.Core.Data.TransformationId.Base | ||
import Juvix.Compiler.Reg.Data.TransformationId.Strings | ||
import Juvix.Prelude | ||
|
||
data TransformationId | ||
= Identity | ||
deriving stock (Data, Bounded, Enum, Show) | ||
|
||
data PipelineId | ||
= PipelineC | ||
| PipelineCairo | ||
deriving stock (Data, Bounded, Enum) | ||
|
||
type TransformationLikeId = TransformationLikeId' TransformationId PipelineId | ||
|
||
toCTransformations :: [TransformationId] | ||
toCTransformations = [] | ||
|
||
toCairoTransformations :: [TransformationId] | ||
toCairoTransformations = [] | ||
|
||
instance TransformationId' TransformationId where | ||
transformationText :: TransformationId -> Text | ||
transformationText = \case | ||
Identity -> strIdentity | ||
|
||
instance PipelineId' TransformationId PipelineId where | ||
pipelineText :: PipelineId -> Text | ||
pipelineText = \case | ||
PipelineC -> strCPipeline | ||
PipelineCairo -> strCairoPipeline | ||
|
||
pipeline :: PipelineId -> [TransformationId] | ||
pipeline = \case | ||
PipelineC -> toCTransformations | ||
PipelineCairo -> toCairoTransformations |
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,14 @@ | ||
module Juvix.Compiler.Reg.Data.TransformationId.Parser (parseTransformations, TransformationId (..), completions, completionsString) where | ||
|
||
import Juvix.Compiler.Core.Data.TransformationId.Parser.Base | ||
import Juvix.Compiler.Reg.Data.TransformationId | ||
import Juvix.Prelude | ||
|
||
parseTransformations :: Text -> Either Text [TransformationId] | ||
parseTransformations = parseTransformations' @TransformationId @PipelineId | ||
|
||
completionsString :: String -> [String] | ||
completionsString = completionsString' @TransformationId @PipelineId | ||
|
||
completions :: Text -> [Text] | ||
completions = completions' @TransformationId @PipelineId |
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.Reg.Data.TransformationId.Strings where | ||
|
||
import Juvix.Prelude | ||
|
||
strCPipeline :: Text | ||
strCPipeline = "pipeline-c" | ||
|
||
strCairoPipeline :: Text | ||
strCairoPipeline = "pipeline-cairo" | ||
|
||
strIdentity :: Text | ||
strIdentity = "identity" |
2 changes: 1 addition & 1 deletion
2
src/Juvix/Compiler/Reg/Extra.hs → src/Juvix/Compiler/Reg/Extra/Info.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
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,127 @@ | ||
module Juvix.Compiler.Reg.Extra.Recursors where | ||
|
||
import Data.Functor.Identity | ||
import Juvix.Compiler.Reg.Language | ||
|
||
data ForwardRecursorSig m c = ForwardRecursorSig | ||
{ _forwardFun :: Instruction -> c -> m (c, Instruction), | ||
_forwardCombine :: NonEmpty c -> c | ||
} | ||
|
||
data BackwardRecursorSig m a = BackwardRecursorSig | ||
{ _backwardFun :: Code -> a -> [a] -> m (a, Code), | ||
_backwardAdjust :: a -> a | ||
} | ||
|
||
makeLenses ''ForwardRecursorSig | ||
makeLenses ''BackwardRecursorSig | ||
|
||
recurseF :: forall m c. (Monad m) => ForwardRecursorSig m c -> c -> Code -> m (c, Code) | ||
recurseF sig c = \case | ||
i : instrs -> do | ||
(c0, i0) <- (sig ^. forwardFun) i c | ||
(c', i') <- | ||
case i0 of | ||
Branch x@InstrBranch {..} -> do | ||
(c1, is1) <- recurseF sig c0 _instrBranchTrue | ||
(c2, is2) <- recurseF sig c0 _instrBranchFalse | ||
let c' = (sig ^. forwardCombine) (c1 :| [c2]) | ||
return (c', Branch x {_instrBranchTrue = is1, _instrBranchFalse = is2}) | ||
Case x@InstrCase {..} -> do | ||
brs' <- mapM goBranch _instrCaseBranches | ||
def' <- maybe (return Nothing) (\is -> Just <$> recurseF sig c0 is) _instrCaseDefault | ||
let cs = map fst brs' ++ maybe [] (\md -> [fst md]) def' | ||
brs = map snd brs' | ||
def = maybe Nothing (Just . snd) def' | ||
c' = (sig ^. forwardCombine) (nonEmpty' cs) | ||
return (c', Case x {_instrCaseBranches = brs, _instrCaseDefault = def}) | ||
where | ||
goBranch :: CaseBranch -> m (c, CaseBranch) | ||
goBranch br@CaseBranch {..} = do | ||
(c', is') <- recurseF sig c0 _caseBranchCode | ||
return (c', br {_caseBranchCode = is'}) | ||
Block x@InstrBlock {..} -> do | ||
(c', is) <- recurseF sig c0 _instrBlockCode | ||
return (c', Block x {_instrBlockCode = is}) | ||
_ -> | ||
return (c0, i0) | ||
(c'', instrs') <- recurseF sig c' instrs | ||
return (c'', i' : instrs') | ||
[] -> | ||
return (c, []) | ||
|
||
recurseB :: forall m a. (Monad m) => BackwardRecursorSig m a -> a -> Code -> m (a, Code) | ||
recurseB sig a = \case | ||
i : instrs -> do | ||
(a', instrs') <- recurseB sig a instrs | ||
let a0 = (sig ^. backwardAdjust) a' | ||
(as, i') <- | ||
case i of | ||
Branch x@InstrBranch {..} -> do | ||
(a1, is1) <- recurseB sig a0 _instrBranchTrue | ||
(a2, is2) <- recurseB sig a0 _instrBranchFalse | ||
return ([a1, a2], Branch x {_instrBranchTrue = is1, _instrBranchFalse = is2}) | ||
Case x@InstrCase {..} -> do | ||
brs' <- mapM goBranch _instrCaseBranches | ||
def' <- maybe (return Nothing) (\is -> Just <$> recurseB sig a0 is) _instrCaseDefault | ||
let as = map fst brs' ++ maybe [] (\md -> [fst md]) def' | ||
brs = map snd brs' | ||
def = maybe Nothing (Just . snd) def' | ||
return (as, Case x {_instrCaseBranches = brs, _instrCaseDefault = def}) | ||
where | ||
goBranch :: CaseBranch -> m (a, CaseBranch) | ||
goBranch br@CaseBranch {..} = do | ||
(aa, is') <- recurseB sig a0 _caseBranchCode | ||
return (aa, br {_caseBranchCode = is'}) | ||
Block x@InstrBlock {..} -> do | ||
(aa, is) <- recurseB sig a0 _instrBlockCode | ||
return ([aa], Block x {_instrBlockCode = is}) | ||
_ -> | ||
return ([], i) | ||
(sig ^. backwardFun) (i' : instrs') a' as | ||
[] -> | ||
(sig ^. backwardFun) [] a [] | ||
|
||
cmapM :: (Monad m) => (Code -> m Code) -> Code -> m Code | ||
cmapM f is0 = do | ||
((), is) <- | ||
recurseB | ||
BackwardRecursorSig | ||
{ _backwardFun = \is _ _ -> do | ||
is' <- f is | ||
return ((), is'), | ||
_backwardAdjust = id | ||
} | ||
() | ||
is0 | ||
return is | ||
|
||
cmap :: (Code -> Code) -> Code -> Code | ||
cmap f is = runIdentity (cmapM (return . f) is) | ||
|
||
imapM :: (Monad m) => (Instruction -> m Instruction) -> Code -> m Code | ||
imapM f = cmapM $ \case | ||
i : is -> do | ||
i' <- f i | ||
return (i' : is) | ||
[] -> | ||
return [] | ||
|
||
imap :: (Instruction -> Instruction) -> Code -> Code | ||
imap f is = runIdentity (imapM (return . f) is) | ||
|
||
ifoldFM :: (Monad m, Monoid a) => (a -> Instruction -> m a) -> a -> Code -> m a | ||
ifoldFM f a0 is0 = | ||
fst | ||
<$> recurseF | ||
ForwardRecursorSig | ||
{ _forwardFun = \i a -> do | ||
a' <- f a i | ||
return (a', i), | ||
_forwardCombine = mconcat . toList | ||
} | ||
a0 | ||
is0 | ||
|
||
ifoldF :: (Monoid a) => (a -> Instruction -> a) -> a -> Code -> a | ||
ifoldF f a is = runIdentity (ifoldFM (\a' -> return . f a') a is) |
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,17 @@ | ||
module Juvix.Compiler.Reg.Transformation | ||
( module Juvix.Compiler.Reg.Transformation.Base, | ||
module Juvix.Compiler.Reg.Transformation, | ||
module Juvix.Compiler.Reg.Data.TransformationId, | ||
) | ||
where | ||
|
||
import Juvix.Compiler.Reg.Data.TransformationId | ||
import Juvix.Compiler.Reg.Transformation.Base | ||
import Juvix.Compiler.Reg.Transformation.Identity | ||
|
||
applyTransformations :: forall r. [TransformationId] -> InfoTable -> Sem r InfoTable | ||
applyTransformations ts tbl = foldM (flip appTrans) tbl ts | ||
where | ||
appTrans :: TransformationId -> InfoTable -> Sem r InfoTable | ||
appTrans = \case | ||
Identity -> return . identity |
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.Transformation.Base | ||
( module Juvix.Compiler.Tree.Transformation.Generic.Base, | ||
module Juvix.Compiler.Reg.Data.InfoTable, | ||
module Juvix.Compiler.Reg.Language, | ||
) | ||
where | ||
|
||
import Juvix.Compiler.Reg.Data.InfoTable | ||
import Juvix.Compiler.Reg.Language | ||
import Juvix.Compiler.Tree.Transformation.Generic.Base |
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,7 @@ | ||
module Juvix.Compiler.Reg.Transformation.Identity where | ||
|
||
import Juvix.Compiler.Reg.Extra.Recursors | ||
import Juvix.Compiler.Reg.Transformation.Base | ||
|
||
identity :: InfoTable -> InfoTable | ||
identity = mapT (const (cmap id)) |
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
Oops, something went wrong.