X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=utils%2Fext-core%2FDriver.hs;h=927a95d7f37d0401e1a57dc244bbbb57e807bb1b;hp=b9d55561f743330bbd86d1a9922dd1e2ef4438ff;hb=8bfeb25ae78e99c7014113468b0057342db4208f;hpb=6e93da5e0a775b2bfb9c9f2bd31a36cc828521cb diff --git a/utils/ext-core/Driver.hs b/utils/ext-core/Driver.hs index b9d5556..927a95d 100644 --- a/utils/ext-core/Driver.hs +++ b/utils/ext-core/Driver.hs @@ -1,32 +1,46 @@ +{-# OPTIONS -Wall #-} + {- A simple driver that loads, typechecks, prepares, re-typechecks, and interprets the GHC standard Prelude modules and an application module called Main. Note that, if compiled under GHC, this requires a very large heap to run! -} +import Control.Exception +import Data.List +import Data.Maybe import Monad -import System.Environment +import Prelude hiding (catch) import System.Cmd +import System.Console.GetOpt +import System.Environment import System.Exit +import System.FilePath import Core -import Printer -import ParsecParser -import Env +import Dependencies +import Overrides import Prims import Check import Prep import Interp +import ParsecParser + -- You may need to change this. +baseDir :: FilePath baseDir = "../../libraries/" --------- +-- change to True to typecheck library files as well as reading type signatures +typecheckLibs :: Bool +typecheckLibs = False --- Code for checking that the external and GHC printers print the same results -testFlag = "-t" +-- You shouldn't *need* to change anything below this line... -validateResults :: FilePath -> FilePath -> IO () -validateResults origFile genFile = do +-- Code to check that the external and GHC printers print the same results +validateResults :: FilePath -> Module -> IO () +validateResults origFile m = do + let genFile = origFile "parsed" + writeFile genFile (show m) resultCode <- system $ "diff -u " ++ origFile ++ " " ++ genFile putStrLn $ case resultCode of ExitSuccess -> "Parse validated for " ++ origFile @@ -34,106 +48,101 @@ validateResults origFile genFile = do _ -> "Error diffing files: " ++ origFile ++ " " ++ genFile ------------------------------------------------------------------------------ -process :: Bool -> (Check.Menv,[Module]) -> String -> IO (Check.Menv,[Module]) -process doTest (senv,modules) f = - do putStrLn ("Processing " ++ f) - resultOrErr <- parseCore f - case resultOrErr of - Right m -> do - putStrLn "Parse succeeded" - let outF = f ++ ".parsed" - writeFile outF (show m) - when doTest $ (validateResults f outF) - case checkModule senv m of - OkC senv' -> - do putStrLn "Check succeeded" - let m' = prepModule senv' m - {- writeFile (f ++ ".prepped") (show m') -} - case checkModule senv m' of - OkC senv'' -> - do putStrLn "Recheck succeeded" - return (senv'',modules ++ [m']) - FailC s -> - do putStrLn ("Recheck failed: " ++ s) - error "quit" - FailC s -> - do putStrLn ("Check failed: " ++ s) - error "quit" - Left err -> do putStrLn ("Parse failed: " ++ show err) - error "quit" - -main = do args <- getArgs - let (doTest, fname) = - case args of - (f:fn:_) | f == testFlag -> (True,fn) - (fn:_) -> (False,fn) - _ -> error $ - "usage: ./Driver [filename]" - mapM_ (process doTest (initialEnv,[])) (libs ++ [fname]) - (_,modules) <- foldM (process doTest) (initialEnv,[]) (libs ++ [fname]) - let result = evalProgram modules - putStrLn ("Result = " ++ show result) - putStrLn "All done" - where libs = map (baseDir ++) ["./ghc-prim/GHC/Generics.hcr", - "./ghc-prim/GHC/PrimopWrappers.hcr", - "./ghc-prim/GHC/Bool.hcr", - "./ghc-prim/GHC/IntWord64.hcr", - "./base/GHC/Base.hcr", - "./base/GHC/List.hcr", - "./base/GHC/Enum.hcr", - "./base/GHC/Show.hcr", - "./base/GHC/Num.hcr", - "./base/GHC/ST.hcr", - "./base/GHC/Real.hcr", - "./base/GHC/STRef.hcr", - "./base/GHC/Arr.hcr", - "./base/GHC/Float.hcr", - "./base/GHC/Read.hcr", - "./base/GHC/Ptr.hcr", - "./base/GHC/Word.hcr", - "./base/GHC/Int.hcr", - "./base/GHC/Unicode.hcr", - "./base/GHC/IOBase.hcr", - "./base/GHC/Err.hcr", - "./base/GHC/Exception.hcr", - "./base/GHC/Stable.hcr", - "./base/GHC/Storable.hcr", - "./base/GHC/Pack.hcr", - "./base/GHC/Weak.hcr", - "./base/GHC/Handle.hcr", - "./base/GHC/IO.hcr", - "./base/GHC/Dotnet.hcr", - "./base/GHC/Environment.hcr", - "./base/GHC/Exts.hcr", - "./base/GHC/PArr.hcr", - "./base/GHC/TopHandler.hcr", - "./base/GHC/Desugar.hcr", - "./base/Data/Ord.hcr", - "./base/Data/Maybe.hcr", - "./base/Data/Bits.hcr", - "./base/Data/STRef/Lazy.hcr", - "./base/Data/Generics/Basics.hcr", - "./base/Data/Generics/Aliases.hcr", - "./base/Data/Generics/Twins.hcr", - "./base/Data/Generics/Instances.hcr", - "./base/Data/Generics/Text.hcr", - "./base/Data/Generics/Schemes.hcr", - "./base/Data/Tuple.hcr", - "./base/Data/String.hcr", - "./base/Data/Either.hcr", - "./base/Data/Char.hcr", - "./base/Data/List.hcr", - "./base/Data/HashTable.hcr", - "./base/Data/Typeable.hcr", - "./base/Data/Dynamic.hcr", - "./base/Data/Function.hcr", - "./base/Data/IORef.hcr", - "./base/Data/Fixed.hcr", - "./base/Data/Monoid.hcr", - "./base/Data/Ratio.hcr", - "./base/Data/STRef.hcr", - "./base/Data/Version.hcr", - "./base/Data/Complex.hcr", - "./base/Data/Unique.hcr", - "./base/Data/Foldable.hcr", - "./base/Data/Traversable.hcr"] \ No newline at end of file +data Flag = Test | NoDeps + deriving Eq + +options :: [OptDescr Flag] +options = + [Option ['t'] ["test"] (NoArg Test) "validate prettyprinted code", + Option ['n'] ["no-deps"] (NoArg NoDeps) "don't compute dependencies automatically" + ] + +process :: Bool -> (Check.Menv,[Module]) -> (FilePath, Module) + -> IO (Check.Menv,[Module]) +process _ (senv,modules) p@(f,m) | isLib p && not typecheckLibs = do + -- if it's a library and we set typecheckLibs to False: + -- prep, but don't typecheck + m' <- prepM senv m f + return (senv, modules ++ [m']) + where isLib (fp,_) = baseDir `isPrefixOf` fp +process doTest (senv,modules) (f, m@(Module mn _ _)) = catch (do + when doTest $ validateResults f m + (case checkModule senv m of + OkC senv' -> + do putStrLn $ "Check succeeded for " ++ show mn + m' <- prepM senv' m f + case checkModule senv' m' of + OkC senv'' -> + do putStrLn "Recheck succeeded" + return (senv'',modules ++ [m']) + FailC s -> + do putStrLn ("Recheck failed: " ++ s) + error "quit" + FailC s -> error ("Typechecking failed: " ++ s))) handler + where handler e = do + putStrLn ("WARNING: we caught an exception " ++ show e + ++ " while processing " ++ f) + return (senv, modules) + +prepM :: Check.Menv -> Module -> FilePath -> IO Module +prepM senv' m _f = do + let m' = prepModule senv' m + --writeFile (f ".prepped") (show m') + return m' + +main :: IO () +main = do + args <- getArgs + case getOpt Permute options args of + (opts, fnames@(_:_), _) -> + let doTest = Test `elem` opts + computeDeps = NoDeps `notElem` opts in + doOneProgram computeDeps doTest fnames + _ -> error "usage: ./Driver [filename]" + where doOneProgram :: Bool -> Bool -> [FilePath] -> IO () + doOneProgram computeDeps doTest fns = do + putStrLn $ "========== Program " ++ (show fns) ++ " =============" + deps <- if computeDeps + then + getDependencies fns + else (liftM catMaybes) (mapM findModuleDirect fns) + putStrLn $ "deps = " ++ show (fst (unzip deps)) + {- + Note that we scan over the libraries twice: + first to gather together all type sigs, then to typecheck them + (the latter of which doesn't necessarily have to be done every time.) + This is a hack to avoid dealing with circular dependencies. + -} + -- notice: scan over libraries *and* input modules first, not just libs + topEnv <- mkInitialEnv (snd (unzip deps)) + (_,modules) <- foldM (process doTest) (topEnv,[]) deps + let succeeded = length modules + putStrLn ("Finished typechecking. Successfully checked " + ++ show succeeded) + overridden <- override modules + result <- evalProgram overridden + putStrLn ("Result = " ++ show result) + putStrLn "All done\n=============================================" + + mkInitialEnv :: [Module] -> IO Menv + mkInitialEnv libs = foldM mkTypeEnv initialEnv libs + + mkTypeEnv :: Menv -> Module -> IO Menv + mkTypeEnv globalEnv m@(Module mn _ _) = + catch (return (envsModule globalEnv m)) handler + where handler e = do + putStrLn ("WARNING: mkTypeEnv caught an exception " ++ show e + ++ " while processing " ++ show mn) + return globalEnv + +findModuleDirect :: FilePath -> IO (Maybe (FilePath, Module)) +-- kludge to let us run "make libtest" -- +-- this module (in the Cabal package) causes an uncaught exception +-- from Prelude.chr, which I haven't been able to track down +findModuleDirect fn | "PackageDescription.hcr" `isSuffixOf` fn = return Nothing +findModuleDirect fn = do + putStrLn $ "Finding " ++ show fn + res <- parseCore fn + case res of + Left err -> error (show err) + Right m -> return $ Just (fn,m) \ No newline at end of file