module Gibbon.Language.Constants where

import qualified Data.List as L

import Gibbon.Language.Syntax
import Gibbon.Common

--------------------------------------------------------------------------------

redirectionSize :: Int
redirectionSize :: Int
redirectionSize = Int
9

redirectionTag :: DataCon
redirectionTag :: DataCon
redirectionTag = DataCon
"REDIRECTION"

isRedirectionTag :: DataCon -> Bool
isRedirectionTag :: DataCon -> Bool
isRedirectionTag = DataCon -> DataCon -> Bool
forall a. Eq a => [a] -> [a] -> Bool
L.isPrefixOf DataCon
redirectionTag

redirectionAlt :: Num a => a
redirectionAlt :: forall a. Num a => a
redirectionAlt = a
255

indirectionTag :: DataCon
indirectionTag :: DataCon
indirectionTag = DataCon
"INDIRECTION"

isIndirectionTag :: DataCon -> Bool
isIndirectionTag :: DataCon -> Bool
isIndirectionTag = DataCon -> DataCon -> Bool
forall a. Eq a => [a] -> [a] -> Bool
L.isPrefixOf DataCon
indirectionTag

indirectionAlt :: Num a => a
indirectionAlt :: forall a. Num a => a
indirectionAlt = a
254

toAbsRANDataCon :: DataCon -> DataCon
toAbsRANDataCon :: DataCon -> DataCon
toAbsRANDataCon DataCon
dcon = DataCon
dcon DataCon -> DataCon -> DataCon
forall a. [a] -> [a] -> [a]
++ DataCon
"^"

isAbsRANDataCon :: DataCon -> Bool
isAbsRANDataCon :: DataCon -> Bool
isAbsRANDataCon = DataCon -> DataCon -> Bool
forall a. Eq a => [a] -> [a] -> Bool
L.isSuffixOf DataCon
"^"

toRelRANDataCon :: DataCon -> DataCon
toRelRANDataCon :: DataCon -> DataCon
toRelRANDataCon DataCon
dcon = DataCon
dcon DataCon -> DataCon -> DataCon
forall a. [a] -> [a] -> [a]
++ DataCon
"*"

isRelRANDataCon :: DataCon -> Bool
isRelRANDataCon :: DataCon -> Bool
isRelRANDataCon = DataCon -> DataCon -> Bool
forall a. Eq a => [a] -> [a] -> Bool
L.isSuffixOf DataCon
"*"

fromRANDataCon :: DataCon -> DataCon
fromRANDataCon :: DataCon -> DataCon
fromRANDataCon = DataCon -> DataCon
forall a. HasCallStack => [a] -> [a]
init

--------------------------------------------------------------------------------

-- | Map a DataCon onto the name of the generated unpack function.
mkUnpackerName :: TyCon -> Var
mkUnpackerName :: DataCon -> Var
mkUnpackerName DataCon
tyCons = DataCon -> Var
toVar (DataCon -> Var) -> DataCon -> Var
forall a b. (a -> b) -> a -> b
$ DataCon
"_unpack_" DataCon -> DataCon -> DataCon
forall a. [a] -> [a] -> [a]
++ DataCon
tyCons

isUnpackerName :: Var -> Bool
isUnpackerName :: Var -> Bool
isUnpackerName Var
v = DataCon -> DataCon -> Bool
forall a. Eq a => [a] -> [a] -> Bool
L.isPrefixOf DataCon
"_unpack_" (Var -> DataCon
fromVar Var
v)

-- | Map a DataCon onto the name of the generated print function.
mkPrinterName :: TyCon -> Var
mkPrinterName :: DataCon -> Var
mkPrinterName DataCon
tyCons = DataCon -> Var
toVar (DataCon -> Var) -> DataCon -> Var
forall a b. (a -> b) -> a -> b
$ DataCon
"_print_" DataCon -> DataCon -> DataCon
forall a. [a] -> [a] -> [a]
++ DataCon
tyCons

isPrinterName :: Var -> Bool
isPrinterName :: Var -> Bool
isPrinterName Var
v = DataCon -> DataCon -> Bool
forall a. Eq a => [a] -> [a] -> Bool
L.isPrefixOf DataCon
"_print_" (Var -> DataCon
fromVar Var
v)

mkCopyFunName :: TyCon -> Var
mkCopyFunName :: DataCon -> Var
mkCopyFunName DataCon
dcon = Var
"_copy_" Var -> Var -> Var
`varAppend` (DataCon -> Var
toVar DataCon
dcon)

isCopyFunName :: Var -> Bool
isCopyFunName :: Var -> Bool
isCopyFunName = DataCon -> DataCon -> Bool
forall a. Eq a => [a] -> [a] -> Bool
L.isPrefixOf DataCon
"_copy_" (DataCon -> Bool) -> (Var -> DataCon) -> Var -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Var -> DataCon
fromVar

mkCopySansPtrsFunName :: TyCon -> Var
mkCopySansPtrsFunName :: DataCon -> Var
mkCopySansPtrsFunName DataCon
dcon = Var
"_copy_without_ptrs_" Var -> Var -> Var
`varAppend` (DataCon -> Var
toVar DataCon
dcon)

isCopySansPtrsFunName :: Var -> Bool
isCopySansPtrsFunName :: Var -> Bool
isCopySansPtrsFunName = DataCon -> DataCon -> Bool
forall a. Eq a => [a] -> [a] -> Bool
L.isPrefixOf DataCon
"_copy_without_ptrs_" (DataCon -> Bool) -> (Var -> DataCon) -> Var -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Var -> DataCon
fromVar

mkTravFunName :: TyCon -> Var
mkTravFunName :: DataCon -> Var
mkTravFunName DataCon
dcon = Var
"_traverse_" Var -> Var -> Var
`varAppend` (DataCon -> Var
toVar DataCon
dcon)

isTravFunName :: Var -> Bool
isTravFunName :: Var -> Bool
isTravFunName = DataCon -> DataCon -> Bool
forall a. Eq a => [a] -> [a] -> Bool
L.isPrefixOf DataCon
"_traverse_" (DataCon -> Bool) -> (Var -> DataCon) -> Var -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Var -> DataCon
fromVar

mkRelOffsetsFunName :: DataCon -> Var
mkRelOffsetsFunName :: DataCon -> Var
mkRelOffsetsFunName DataCon
dcon = Var
"_add_size_and_rel_offsets_" Var -> Var -> Var
`varAppend` (DataCon -> Var
toVar DataCon
dcon)

isRelOffsetsFunName :: Var -> Bool
isRelOffsetsFunName :: Var -> Bool
isRelOffsetsFunName = DataCon -> DataCon -> Bool
forall a. Eq a => [a] -> [a] -> Bool
L.isPrefixOf DataCon
"_add_size_and_rel_offsets_" (DataCon -> Bool) -> (Var -> DataCon) -> Var -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Var -> DataCon
fromVar