1-- SPDX-FileCopyrightText: 2025 Sören Tempel <soeren+git@soeren-tempel.net>2--3-- SPDX-License-Identifier: GPL-3.0-only45module Main (main) where67import Data.Word (Word64, Word8)8import Language.QBE.CmdLine qualified as CMD9import Language.QBE.Simulator (execFunc)10import Language.QBE.Simulator.Default.Expression qualified as DE11import Language.QBE.Simulator.Default.State (Env, mkEnv, run)12import Language.QBE.Simulator.Expression qualified as E13import Language.QBE.Types qualified as QBE14import Options.Applicative qualified as OPT15import System.Exit (ExitCode (ExitFailure, ExitSuccess), exitWith)1617fromWord :: DE.RegVal -> Maybe Word6418fromWord v19 | E.getType v == QBE.Base QBE.Word = Just $ E.toWord64 v20 | otherwise = Nothing2122execFile :: CMD.BasicArgs -> IO Int23execFile opts = do24 (prog, func) <- CMD.parseEntryFile $ CMD.optQBEFile opts2526 env <- mkEnv prog (CMD.optMemStart opts) (CMD.optMemSize opts)27 res <- run (env :: Env DE.RegVal Word8) (execFunc func [])28 case res >>= fromWord of29 Just x -> pure $ fromIntegral x30 Nothing ->31 -- The main function emitted by the Hare compiler does not32 -- return an int. Therefore, we do not emit an error here.33 pure 03435main :: IO ()36main = do37 retVal <- OPT.execParser cmd >>= execFile38 exitWith $39 if retVal == 040 then ExitSuccess41 else ExitFailure retVal42 where43 cmd :: OPT.ParserInfo CMD.BasicArgs44 cmd =45 OPT.info46 (CMD.basicArgs OPT.<**> OPT.helper)47 ( OPT.fullDesc48 <> OPT.progDesc "Concrete execution of programs in the QBE intermediate language"49 )