Skip to content

Commit

Permalink
Merge pull request #303 from GaloisInc/vr/pretty
Browse files Browse the repository at this point in the history
pretty-print Reopt's maps
  • Loading branch information
Ptival authored Jan 5, 2024
2 parents 3aa9bb9 + e28a989 commit f1443f8
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/Reopt/TypeInference/FunTypeMaps.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Reopt.TypeInference.FunTypeMaps (

import Control.Monad.State
import Data.ByteString qualified as BS
import Data.ByteString.UTF8 qualified as UTF8
import Data.ElfEdit.Prim qualified as Elf
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
Expand All @@ -31,7 +32,7 @@ import Prettyprinter qualified as PP
import Text.Printf (printf)

import Data.Macaw.Discovery (NoReturnFunStatus (..))
import Data.Macaw.Memory (MemSegmentOff)
import Data.Macaw.Memory (MemSegmentOff, MemWidth)

import Reopt.TypeInference.HeaderTypes (AnnFunType)

Expand All @@ -50,6 +51,9 @@ data QualifiedSymbolName = QualifiedSymbolName
}
deriving (Show)

instance PP.Pretty QualifiedSymbolName where
pretty = PP.viaShow

mkQualifiedSymbolName ::
Elf.SymtabEntry BS.ByteString w ->
QualifiedSymbolName
Expand Down Expand Up @@ -84,6 +88,23 @@ data SymAddrMap w = SymAddrMap
-- prioritize global symbols over local symbols.
}

prettyMapExplicit :: (a -> PP.Doc c) -> (b -> PP.Doc c) -> Map a b -> PP.Doc c
prettyMapExplicit ppKey ppVal = PP.vcat . map prettyEntry . Map.assocs
where
prettyEntry (a, b) = PP.hsep [ppKey a, "", ppVal b]

prettyMap :: PP.Pretty a => PP.Pretty b => Map a b -> PP.Doc c
prettyMap = prettyMapExplicit PP.pretty PP.pretty

instance MemWidth w => PP.Pretty (SymAddrMap w) where
pretty sam =
PP.vcat
["Name map:"
, prettyMapExplicit (PP.pretty . UTF8.toString) (PP.pretty . Set.toList) (samNameMap sam)
, "Address map:"
, prettyMap (samAddrMap sam)
]

-- | Return list of names and addresses stored in sym addr map
symAddrMapContents :: SymAddrMap w -> [(BS.ByteString, MemSegmentOff w)]
symAddrMapContents m =
Expand Down Expand Up @@ -185,6 +206,19 @@ data FunTypeMaps w = FunTypeMaps
, noreturnMap :: !(Map (MemSegmentOff w) NoReturnFunStatus)
}

instance MemWidth w => PP.Pretty (FunTypeMaps w) where
pretty ftm =
PP.vcat
[ "Name to address map:"
, PP.pretty (nameToAddrMap ftm)
, "Name to type map:"
, prettyMapExplicit (PP.pretty . UTF8.toString) PP.pretty (nameTypeMap ftm)
, "Address to type map:"
, prettyMap (addrTypeMap ftm)
, "No return map:"
, prettyMap (noreturnMap ftm)
]

-- | Empty function type information.
funTypeMapsEmpty :: FunTypeMaps w
funTypeMapsEmpty = FunTypeMaps symAddrMapEmpty Map.empty Map.empty Map.empty
Expand Down

0 comments on commit f1443f8

Please sign in to comment.