{-|
Copyright   : (c) Galois Inc, 2016
Maintainer  : jhendrix@galois.com

Common datatypes for creating a memory from a binary file.
-}
module Data.Macaw.Memory.LoadCommon
  ( LoadOptions(..)
  , loadRegionIndex
  , loadRegionBaseOffset
  , defaultLoadOptions
  , LoadStyle(..)
  ) where

import Data.Macaw.Memory
import Data.Word

------------------------------------------------------------------------
-- LoadOptions

-- | How to load Elf file.
data LoadStyle
   = LoadBySection
     -- ^ Load loadable sections in Elf file.
   | LoadBySegment
     -- ^ Load segments in Elf file.
  deriving (LoadStyle -> LoadStyle -> Bool
(LoadStyle -> LoadStyle -> Bool)
-> (LoadStyle -> LoadStyle -> Bool) -> Eq LoadStyle
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LoadStyle -> LoadStyle -> Bool
== :: LoadStyle -> LoadStyle -> Bool
$c/= :: LoadStyle -> LoadStyle -> Bool
/= :: LoadStyle -> LoadStyle -> Bool
Eq)

-- | This contains options for loading.
newtype LoadOptions =
  LoadOptions { LoadOptions -> Maybe Word64
loadOffset :: Maybe Word64
                -- ^ If set, the Elf file should be loaded at a specific offset.
              }

loadRegionIndex :: LoadOptions -> Maybe RegionIndex
loadRegionIndex :: LoadOptions -> Maybe RegionIndex
loadRegionIndex LoadOptions
opts =
  case LoadOptions -> Maybe Word64
loadOffset LoadOptions
opts of
    Maybe Word64
Nothing -> Maybe RegionIndex
forall a. Maybe a
Nothing
    Just Word64
_ -> RegionIndex -> Maybe RegionIndex
forall a. a -> Maybe a
Just RegionIndex
0

loadRegionBaseOffset :: LoadOptions -> Integer
loadRegionBaseOffset :: LoadOptions -> Integer
loadRegionBaseOffset LoadOptions
opts =
  case LoadOptions -> Maybe Word64
loadOffset LoadOptions
opts of
    Maybe Word64
Nothing -> Integer
0
    Just Word64
o -> Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
o

defaultLoadOptions :: LoadOptions
defaultLoadOptions :: LoadOptions
defaultLoadOptions = LoadOptions { loadOffset :: Maybe Word64
loadOffset = Maybe Word64
forall a. Maybe a
Nothing }