1-- SPDX-FileCopyrightText: 2025 Sören Tempel <soeren+git@soeren-tempel.net>2--3-- SPDX-License-Identifier: GPL-3.0-only45module Main (main) where67import Language.QBE.Backend.Store qualified as ST8import Language.QBE.Backend.Tracer qualified as T9import Language.QBE.CmdLine qualified as CMD10import Language.QBE.Simulator.Concolic.State (mkEnv)11import Language.QBE.Simulator.Explorer (defSolver, explore, logSolver, newEngine)12import Language.QBE.Types qualified as QBE13import Options.Applicative qualified as OPT14import System.IO (IOMode (WriteMode), withFile)1516data Opts = Opts17 { optLog :: Maybe FilePath,18 optSeed :: Maybe Int,19 optBase :: CMD.BasicArgs20 }2122optsParser :: OPT.Parser Opts23optsParser =24 Opts25 <$> OPT.optional26 ( OPT.strOption27 ( OPT.long "dump-smt2"28 <> OPT.short 'd'29 <> OPT.metavar "FILE"30 <> OPT.help "Output queries as an SMT-LIB file"31 )32 )33 <*> OPT.optional34 ( OPT.option35 OPT.auto36 ( OPT.long "random-seed"37 <> OPT.short 'r'38 <> OPT.help "Initial seed to for the random number generator"39 )40 )41 <*> CMD.basicArgs4243------------------------------------------------------------------------4445exploreFile :: Opts -> IO [(ST.Assign, T.ExecTrace)]46exploreFile opts@Opts {optBase = base} = do47 (prog, func) <- CMD.parseEntryFile $ CMD.optQBEFile base4849 env <- mkEnv prog (CMD.optMemStart base) (CMD.optMemSize base) (optSeed opts)50 case optLog opts of51 Just fn -> withFile fn WriteMode (exploreWithHandle env func)52 Nothing -> do53 engine <- newEngine <$> defSolver54 explore engine env func params55 where56 params :: [(String, QBE.ExtType)]57 params = []5859 exploreWithHandle env func handle = do60 engine <- newEngine <$> logSolver handle61 explore engine env func params6263main :: IO ()64main = do65 args <- OPT.execParser cmd66 paths <- exploreFile args67 putStrLn $ "Amount of paths: " ++ show (length paths)68 where69 cmd :: OPT.ParserInfo Opts70 cmd =71 OPT.info72 (optsParser OPT.<**> OPT.helper)73 ( OPT.fullDesc74 <> OPT.progDesc "Symbolic execution of programs in the QBE intermediate language"75 )