diff options
author | Ömer Sinan Ağacan <omeragacan@gmail.com> | 2019-08-21 15:32:09 +0300 |
---|---|---|
committer | Ömer Sinan Ağacan <omeragacan@gmail.com> | 2019-08-28 12:51:12 +0300 |
commit | 1c7ec4499ffec5e6b9c97e7a5c8d31062d1e2822 (patch) | |
tree | 1cc732f1ab66b6c5963970b33f816e5bbd998edf /compiler/utils | |
parent | ee2fad9e503ffdf61a086f721553aa3c502d1cb8 (diff) | |
download | haskell-1c7ec4499ffec5e6b9c97e7a5c8d31062d1e2822.tar.gz |
Return results of Cmm streams in backends
This generalizes code generators (outputAsm, outputLlvm, outputC, and
the call site codeOutput) so that they'll return the return values of
the passed Cmm streams.
This allows accumulating data during Cmm generation and returning it to
the call site in HscMain.
Previously the Cmm streams were assumed to return (), so the code
generators returned () as well.
This change is required by !1304 and !1530.
Skipping CI as this was tested before and I only updated the commit
message.
[skip ci]
Diffstat (limited to 'compiler/utils')
-rw-r--r-- | compiler/utils/Stream.hs | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/compiler/utils/Stream.hs b/compiler/utils/Stream.hs index 2ad2b8cc7a..7eabbe1958 100644 --- a/compiler/utils/Stream.hs +++ b/compiler/utils/Stream.hs @@ -7,8 +7,8 @@ -- ----------------------------------------------------------------------------- module Stream ( Stream(..), yield, liftIO, - collect, consume, fromList, - Stream.map, Stream.mapM, Stream.mapAccumL + collect, collect_, consume, fromList, + Stream.map, Stream.mapM, Stream.mapAccumL, Stream.mapAccumL_ ) where import GhcPrelude @@ -71,6 +71,16 @@ collect str = go str [] Left () -> return (reverse acc) Right (a, str') -> go str' (a:acc) +-- | Turn a Stream into an ordinary list, by demanding all the elements. +collect_ :: Monad m => Stream m a r -> m ([a], r) +collect_ str = go str [] + where + go str acc = do + r <- runStream str + case r of + Left r -> return (reverse acc, r) + Right (a, str') -> go str' (a:acc) + consume :: Monad m => Stream m a b -> (a -> m ()) -> m b consume str f = do r <- runStream str @@ -113,3 +123,13 @@ mapAccumL f c str = Stream $ do Right (a, str') -> do (c',b) <- f c a return (Right (b, mapAccumL f c' str')) + +mapAccumL_ :: Monad m => (c -> a -> m (c,b)) -> c -> Stream m a r + -> Stream m b (c, r) +mapAccumL_ f c str = Stream $ do + r <- runStream str + case r of + Left r -> return (Left (c, r)) + Right (a, str') -> do + (c',b) <- f c a + return (Right (b, mapAccumL_ f c' str')) |