From 9ace3632bc074c06456055d8664fe5b15a646059 Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 25 Nov 2019 13:02:44 +0000 Subject: [PATCH] Export symbol and flag files --- app/MimaAsm/Main.hs | 84 +++++++++++++++++++++++++++++++++++++++++---- app/MimaRun/Main.hs | 2 +- 2 files changed, 79 insertions(+), 7 deletions(-) diff --git a/app/MimaAsm/Main.hs b/app/MimaAsm/Main.hs index 3f5e81d..2388165 100644 --- a/app/MimaAsm/Main.hs +++ b/app/MimaAsm/Main.hs @@ -1,17 +1,52 @@ +{-# LANGUAGE OverloadedStrings #-} + module Main where -import Control.Monad.Trans.Class -import Options.Applicative +import Control.Monad.Trans.Class +import qualified Data.Text as T +import qualified Data.Text.IO as T +import Options.Applicative +import System.FilePath -import Mima.IO -import Mima.Load -import Mima.Parse.Assembly +import Mima.Flag +import Mima.Format.FlagFile +import Mima.Format.SymbolFile +import Mima.IO +import Mima.Label +import Mima.Load +import Mima.Options +import Mima.Parse.Assembly data Settings = Settings { infile :: String , outfile :: String + , discover :: Bool + , flagFile :: Maybe FilePath + , symbolFile :: Maybe FilePath } deriving (Show) +getFlagFile :: Settings -> File +getFlagFile settings = + case flagFile settings of + Just path -> RequiredFile path + Nothing -> case discover settings of + False -> NoFile + True -> OptionalFile discoveredPath + where + discoveredPath = dropExtension (infile settings) ++ ".mima-flags" + +getSymbolFile :: Settings -> File +getSymbolFile settings = + case symbolFile settings of + Just path -> RequiredFile path + Nothing -> case discover settings of + False -> NoFile + True -> OptionalFile discoveredPath + where + discoveredPath = dropExtension (infile settings) ++ ".mima-symbols" + +{- Command-line parameters -} + settingsParser :: Parser Settings settingsParser = Settings <$> strArgument @@ -24,17 +59,54 @@ settingsParser = Settings <> help "The .mima file to write the assembled result to" <> value "out.mima" <> showDefault) + <*> switchWithNo "discover" True + "Derive the file names for the .mima-flags and .mima-symbols files from the name of the input file" + <*> (optional . strOption) + (long "flag-file" + <> short 'f' + <> metavar "FLAGFILE" + <> help "A file containing extension memory flags, specified in the .mima-flags format") + <*> (optional . strOption) + (long "symbol-file" + <> short 's' + <> metavar "SYMBOLFILE" + <> help "A file containing label names and addresses, specified in the .mima-symbols format") opts :: ParserInfo Settings opts = info (helper <*> settingsParser) $ fullDesc <> failureCode 1 +{- Saving supplemental files -} + +printFile :: T.Text -> File -> Run () +printFile name NoFile = + lift $ T.putStrLn $ "Not saving " <> name <> ": No file specified and discovery turned off" +printFile name (OptionalFile path) = + lift $ T.putStrLn $ "Saving " <> name <> " to " <> T.pack path +printFile name (RequiredFile path) = + lift $ T.putStrLn $ "Saving " <> name <> " to " <> T.pack path + +saveFlags :: RawFlags -> Settings -> Run () +saveFlags flags settings = do + let file = getFlagFile settings + printFile "flags" file + storeFile' file (formatFlagFile flags) + +saveSymbols :: LabelSpec -> Settings -> Run () +saveSymbols labels settings = do + let file = getSymbolFile settings + printFile "symbols" file + storeFile' file (formatSymbolFile labels) + main :: IO () main = doRun_ $ do settings <- lift $ execParser opts lift $ putStrLn $ "Loading assembly file at " ++ infile settings - (state, _, _) <- loadFile readAssembly (infile settings) + (state, labels, flags) <- loadFile readAssembly (infile settings) lift $ putStrLn "Parsing successful" lift $ putStrLn $ "Writing result to " ++ outfile settings saveStateToFile (outfile settings) state + + saveFlags flags settings + saveSymbols labels settings diff --git a/app/MimaRun/Main.hs b/app/MimaRun/Main.hs index 0aa8967..261c75e 100644 --- a/app/MimaRun/Main.hs +++ b/app/MimaRun/Main.hs @@ -70,7 +70,7 @@ settingsParser = Settings <> metavar "OUTFILE" <> help "If specified, write the memory dump to this file after execution is finished") <*> switchWithNo "discover" True - "Try to load .mima-flags and .mima-symbols corresponding to the .mima input file" + "Derive the file names for the .mima-flags and .mima-symbols files from the name of the input file" <*> (optional . strOption) (long "flag-file" <> short 'f'