Skip to content

Commit 9c2c83e

Browse files
authored
Add --llc-path and --log-file options (#479)
* Add --log-file flag (#478) * Add --llc-path flag (#477)
1 parent af2f3b3 commit 9c2c83e

File tree

5 files changed

+44
-24
lines changed

5 files changed

+44
-24
lines changed

src/AST.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,18 +483,19 @@ data CompilerState = Compiler {
483483
errorState :: Bool, -- ^whether or not we've seen any errors
484484
modules :: Map ModSpec Module, -- ^all known modules except what we're loading
485485
underCompilation :: [Module], -- ^the modules in the process of being compiled
486-
unchangedMods :: Set ModSpec -- ^record mods that are loaded from object
486+
unchangedMods :: Set ModSpec, -- ^record mods that are loaded from object
487487
-- and unchanged.
488+
logHandle :: Handle -- ^handle to write logs to
488489
}
489490

490491
-- |The compiler monad is a state transformer monad carrying the
491492
-- compiler state over the IO monad.
492493
type Compiler = StateT CompilerState IO
493494

494495
-- |Run a compiler function from outside the Compiler monad.
495-
runCompiler :: Options -> Compiler t -> IO t
496-
runCompiler opts comp = evalStateT comp
497-
(Compiler opts "" [] False Map.empty [] Set.empty)
496+
runCompiler :: Options -> Handle -> Compiler t -> IO t
497+
runCompiler opts logHandle comp = evalStateT comp
498+
(Compiler opts "" [] False Map.empty [] Set.empty logHandle)
498499

499500

500501
-- |Apply some transformation function to the compiler state.
@@ -4476,8 +4477,9 @@ logMsg :: LogSelection -- ^ The aspect of the compiler being logged,
44764477
-> Compiler () -- ^ Works in the Compiler monad
44774478
logMsg selector msg = do
44784479
prefix <- makeBold $ show selector ++ ": "
4479-
whenLogging selector $
4480-
liftIO $ hPutStrLn stderr (prefix ++ List.intercalate ('\n':prefix) (lines msg))
4480+
whenLogging selector $ do
4481+
logFile <- gets logHandle
4482+
liftIO $ hPutStrLn logFile (prefix ++ List.intercalate ('\n':prefix) (lines msg))
44814483

44824484
-- | Appends a ISO/IEC 6429 code to the given string to print it bold
44834485
-- in a terminal output.

src/ASTShow.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,15 @@ logDumpWith :: (Handle -> ModSpec -> Bool -> Bool -> Compiler ())
9494
-> LogSelection -> LogSelection -> String -> Compiler ()
9595
logDumpWith llPrinter selector1 selector2 pass =
9696
whenLogging2 selector1 selector2 $ do
97+
logFile <- gets logHandle
9798
modList <- gets (Map.elems . modules)
9899
dumpLib <- gets (optDumpLib . options)
99100
let toLog mod = let spec = modSpec mod
100101
in List.null spec || dumpLib || head spec /= "wybe"
101102
let logging = List.filter toLog modList
102-
liftIO $ hPutStrLn stderr $ replicate 70 '='
103+
liftIO $ hPutStrLn logFile $ replicate 70 '='
103104
++ "\nAFTER " ++ pass ++ ":\n"
104105
forM_ logging $ \mod -> do
105-
liftIO $ hPutStrLn stderr $ "\n" ++ replicate 50 '-' ++ "\n"
106+
liftIO $ hPutStrLn logFile $ "\n" ++ replicate 50 '-' ++ "\n"
106107
++ show mod ++ "\n\n LLVM code :\n"
107-
llPrinter stderr (modSpec mod) False False
108+
llPrinter logFile (modSpec mod) False False

src/Config.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,21 @@ llvmToNativeAssemblerCommand :: FilePath -> FilePath -> Options
180180
-> (String, [String])
181181
llvmToNativeAssemblerCommand llFile sFile options =
182182
let opt = "-O" ++ show (optLLVMOptLevel options)
183+
llc = optLlcBin options
183184
in case buildOS of
184-
OSX -> ("llc", ["--filetype=asm", opt, "-o", sFile, llFile])
185-
Linux -> ("llc", ["--filetype=asm", opt, "-o", sFile, llFile])
185+
OSX -> (llc, ["--filetype=asm", opt, "-o", sFile, llFile])
186+
Linux -> (llc, ["--filetype=asm", opt, "-o", sFile, llFile])
186187
os -> error $ "Unsupported OS: " ++ show os
187188

188189

189190
-- | Command and switches to compile a .ll file to an object file.
190191
llvmToObjectCommand :: FilePath -> FilePath -> Options -> (String, [String])
191192
llvmToObjectCommand llFile oFile options =
192193
let opt = "-O" ++ show (optLLVMOptLevel options)
194+
llc = optLlcBin options
193195
in case buildOS of
194-
OSX -> ("llc", ["--filetype=obj", opt, "-o", oFile, llFile])
195-
Linux -> ("llc", ["--filetype=obj", opt, "-o", oFile, llFile])
196+
OSX -> (llc, ["--filetype=obj", opt, "-o", oFile, llFile])
197+
Linux -> (llc, ["--filetype=obj", opt, "-o", oFile, llFile])
196198
os -> error $ "Unsupported OS: " ++ show os
197199

198200

src/Options.hs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ data Options = Options
4141
, optHelpLog :: Bool -- ^Print log option help and exit
4242
, optHelpOpt :: Bool -- ^Print optiisation option help and exit
4343
, optLibDirs :: [String] -- ^Directories where library files live
44+
, optLogFile :: Maybe String
45+
-- ^Path where to dump logs
4446
, optLogAspects :: Set LogSelection
4547
-- ^Which aspects to log
4648
, optOptimisations :: Set OptFlag
4749
-- ^Enabled optimisations
50+
, optLlcBin :: String -- ^LLVM 'llc' binary path
4851
, optLLVMOptLevel :: Word -- ^LLVM optimisation level
4952
, optDumpLib :: Bool -- ^Also dump wybe.* modules when dumping
5053
, optVerbose :: Bool -- ^Be verbose in compiler output
@@ -66,7 +69,9 @@ defaultOptions = Options
6669
, optHelpOpt = False
6770
, optLibDirs = []
6871
, optLogAspects = Set.empty
72+
, optLogFile = Nothing
6973
, optOptimisations = defaultOptFlags
74+
, optLlcBin = "llc"
7075
, optLLVMOptLevel = 3
7176
, optDumpLib = False
7277
, optVerbose = False
@@ -222,8 +227,11 @@ options =
222227
(ReqArg (\ d opts -> opts { optLibDirs = optLibDirs opts ++ [d] }) "DIR")
223228
("specify a library directory [default $WYBELIBS or " ++ libDir ++ "]")
224229
, Option ['l'] ["log"]
225-
(ReqArg addLogAspects "ASPECT")
226-
"add comma-separated aspects to log, or 'all'"
230+
(ReqArg addLogAspects "ASPECT")
231+
"add comma-separated aspects to log, or 'all'"
232+
, Option [] ["log-file"]
233+
(ReqArg (\ f opts -> opts { optLogFile = Just f }) "FILE")
234+
"File to write logs to"
227235
, Option ['h'] ["help"]
228236
(NoArg (\ opts -> opts { optShowHelp = True }))
229237
"display this help text and exit"
@@ -239,6 +247,9 @@ options =
239247
, Option ['x'] ["opt"]
240248
(ReqArg addOptFlags "FLAGS")
241249
"add comma-separated optimisation flags"
250+
, Option [] ["llc-path"]
251+
(ReqArg (\ llc opts -> opts { optLlcBin = llc }) "PATH")
252+
"specify the path of the 'llc' used"
242253
, Option ['O'] ["llvm-opt-level"]
243254
(ReqArg setLLVMOptLevel "LEVEL")
244255
"specify the LLVM compiler optimisation level"

src/wybemk.hs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,26 @@ import AST
1717
import Builder
1818
import Control.Exception
1919
import Control.Monad
20+
import Data.Maybe
2021
import Options
2122
import System.Exit
23+
import System.IO
2224

2325
-- |The main wybe compiler command line.
2426
main :: IO ()
2527
main = do
2628
(opts, files) <- handleCmdline
27-
catchAny
28-
(runCompiler opts (buildTargets files))
29-
-- if there's an exception, print to stdout
30-
-- XXX should probably go to stderr; but for now logging goes there
31-
(\e -> do
32-
let msg = show e
33-
when (msg /= "ExitFailure 1") $
34-
putStrLn $ displayException e
35-
exitFailure)
29+
let logger = fromMaybe ($ stderr) $ (`withFile` WriteMode) <$> optLogFile opts
30+
logger $ \logHandle ->
31+
catchAny
32+
(runCompiler opts logHandle (buildTargets files))
33+
-- if there's an exception, print to stdout
34+
-- XXX should probably go to stderr; but for now logging defaults to there
35+
(\e -> do
36+
let msg = show e
37+
when (msg /= "ExitFailure 1") $
38+
putStrLn $ displayException e
39+
exitFailure)
3640

3741

3842
catchAny :: IO a -> (SomeException -> IO a) -> IO a

0 commit comments

Comments
 (0)