quebex

A software analysis framework built around the QBE intermediate language

git clone https://git.8pit.net/quebex.git

 1-- SPDX-FileCopyrightText: 2024 University of Bremen
 2-- SPDX-FileCopyrightText: 2025 Sören Tempel <soeren+git@soeren-tempel.net>
 3--
 4-- SPDX-License-Identifier: MIT AND GPL-3.0-only
 5
 6module Language.QBE.CmdLine
 7  ( BasicArgs (..),
 8    basicArgs,
 9    entryFunc,
10    parseEntryFile,
11  )
12where
13
14import Language.QBE (Program, parseAndFind)
15import Language.QBE.Simulator.Memory qualified as MEM
16import Language.QBE.Types qualified as QBE
17import Options.Applicative qualified as OPT
18
19-- | t'BasicArgs' can be combined/extended with additional parsers using
20-- the '<*>' applicative operator provided by "Options.Applicative".
21data BasicArgs = BasicArgs
22  { -- | Start address of the general-purpose memory.
23    optMemStart :: MEM.Address,
24    -- | Size of the memory in bytes.
25    optMemSize :: MEM.Size,
26    -- | Path to the QBE input file.
27    optQBEFile :: FilePath
28  }
29
30-- | "Options.Applicative" parser for t'BasicArgs'.
31basicArgs :: OPT.Parser BasicArgs
32basicArgs =
33  BasicArgs
34    <$> OPT.option
35      OPT.auto
36      ( OPT.long "memory-start"
37          <> OPT.short 'm'
38          <> OPT.value 0x0
39      )
40    <*> OPT.option
41      OPT.auto
42      ( OPT.long "memory-size"
43          <> OPT.short 's'
44          <> OPT.value (1024 * 1024) -- 1 MB RAM
45          <> OPT.help "Size of the memory region"
46      )
47    <*> OPT.argument OPT.str (OPT.metavar "FILE")
48
49------------------------------------------------------------------------
50
51-- | Name of the entry function.
52entryFunc :: QBE.GlobalIdent
53entryFunc = QBE.GlobalIdent "main"
54
55-- | Parse a file and find the 'entryFunc'.
56parseEntryFile :: FilePath -> IO (Program, QBE.FuncDef)
57parseEntryFile filePath =
58  readFile filePath >>= parseAndFind entryFunc