{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
module GHC.CmmToAsm.Dwarf.Types
(
DwarfInfo(..)
, pprDwarfInfo
, pprAbbrevDecls
, DwarfARange(..)
, pprDwarfARanges
, DwarfFrame(..), DwarfFrameProc(..), DwarfFrameBlock(..)
, pprDwarfFrame
, pprByte
, pprHalf
, pprData4'
, pprDwWord
, pprWord
, pprLEBWord
, pprLEBInt
, wordAlign
, sectionOffset
)
where
import GHC.Prelude
import GHC.Cmm.DebugBlock
import GHC.Cmm.CLabel
import GHC.Cmm.Expr ( GlobalReg(..) )
import GHC.Utils.Encoding
import GHC.Data.FastString
import GHC.Utils.Outputable
import GHC.Platform
import GHC.Types.Unique
import GHC.Platform.Reg
import GHC.Types.SrcLoc
import GHC.Utils.Misc
import GHC.CmmToAsm.Dwarf.Constants
import qualified Data.ByteString as BS
import qualified GHC.Utils.Monad.State.Strict as S
import Control.Monad (zipWithM, join)
import qualified Data.Map as Map
import Data.Word
import Data.Char
import GHC.Platform.Regs
data DwarfInfo
= DwarfCompileUnit { DwarfInfo -> [DwarfInfo]
dwChildren :: [DwarfInfo]
, DwarfInfo -> String
dwName :: String
, DwarfInfo -> String
dwProducer :: String
, DwarfInfo -> String
dwCompDir :: String
, DwarfInfo -> CLabel
dwLowLabel :: CLabel
, DwarfInfo -> CLabel
dwHighLabel :: CLabel }
| DwarfSubprogram { dwChildren :: [DwarfInfo]
, dwName :: String
, DwarfInfo -> CLabel
dwLabel :: CLabel
, DwarfInfo -> Maybe CLabel
dwParent :: Maybe CLabel
}
| DwarfBlock { dwChildren :: [DwarfInfo]
, dwLabel :: CLabel
, DwarfInfo -> Maybe CLabel
dwMarker :: Maybe CLabel
}
| DwarfSrcNote { DwarfInfo -> RealSrcSpan
dwSrcSpan :: RealSrcSpan
}
data DwarfAbbrev
= DwAbbrNull
| DwAbbrCompileUnit
| DwAbbrSubprogram
| DwAbbrSubprogramWithParent
| DwAbbrBlockWithoutCode
| DwAbbrBlock
| DwAbbrGhcSrcNote
deriving (DwarfAbbrev -> DwarfAbbrev -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DwarfAbbrev -> DwarfAbbrev -> Bool
$c/= :: DwarfAbbrev -> DwarfAbbrev -> Bool
== :: DwarfAbbrev -> DwarfAbbrev -> Bool
$c== :: DwarfAbbrev -> DwarfAbbrev -> Bool
Eq, Int -> DwarfAbbrev
DwarfAbbrev -> Int
DwarfAbbrev -> [DwarfAbbrev]
DwarfAbbrev -> DwarfAbbrev
DwarfAbbrev -> DwarfAbbrev -> [DwarfAbbrev]
DwarfAbbrev -> DwarfAbbrev -> DwarfAbbrev -> [DwarfAbbrev]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: DwarfAbbrev -> DwarfAbbrev -> DwarfAbbrev -> [DwarfAbbrev]
$cenumFromThenTo :: DwarfAbbrev -> DwarfAbbrev -> DwarfAbbrev -> [DwarfAbbrev]
enumFromTo :: DwarfAbbrev -> DwarfAbbrev -> [DwarfAbbrev]
$cenumFromTo :: DwarfAbbrev -> DwarfAbbrev -> [DwarfAbbrev]
enumFromThen :: DwarfAbbrev -> DwarfAbbrev -> [DwarfAbbrev]
$cenumFromThen :: DwarfAbbrev -> DwarfAbbrev -> [DwarfAbbrev]
enumFrom :: DwarfAbbrev -> [DwarfAbbrev]
$cenumFrom :: DwarfAbbrev -> [DwarfAbbrev]
fromEnum :: DwarfAbbrev -> Int
$cfromEnum :: DwarfAbbrev -> Int
toEnum :: Int -> DwarfAbbrev
$ctoEnum :: Int -> DwarfAbbrev
pred :: DwarfAbbrev -> DwarfAbbrev
$cpred :: DwarfAbbrev -> DwarfAbbrev
succ :: DwarfAbbrev -> DwarfAbbrev
$csucc :: DwarfAbbrev -> DwarfAbbrev
Enum)
pprAbbrev :: IsDoc doc => DwarfAbbrev -> doc
pprAbbrev :: forall doc. IsDoc doc => DwarfAbbrev -> doc
pprAbbrev = forall doc. IsDoc doc => Word -> doc
pprLEBWord forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => a -> Int
fromEnum
pprAbbrevDecls :: IsDoc doc => Platform -> Bool -> doc
pprAbbrevDecls :: forall doc. IsDoc doc => Platform -> Bool -> doc
pprAbbrevDecls Platform
platform Bool
haveDebugLine =
let mkAbbrev :: DwarfAbbrev -> Word -> Word8 -> [(Word, Word)] -> doc
mkAbbrev DwarfAbbrev
abbr Word
tag Word8
chld [(Word, Word)]
flds =
let fld :: (Word, Word) -> doc
fld (Word
tag, Word
form) = forall doc. IsDoc doc => Word -> doc
pprLEBWord Word
tag forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word -> doc
pprLEBWord Word
form
in forall doc. IsDoc doc => DwarfAbbrev -> doc
pprAbbrev DwarfAbbrev
abbr forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word -> doc
pprLEBWord Word
tag forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
chld forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => [doc] -> doc
vcat (forall a b. (a -> b) -> [a] -> [b]
map forall {doc}. IsDoc doc => (Word, Word) -> doc
fld [(Word, Word)]
flds) forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
0 forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
0
subprogramAttrs :: [(Word, Word)]
subprogramAttrs =
[ (Word
dW_AT_name, Word
dW_FORM_string)
, (Word
dW_AT_linkage_name, Word
dW_FORM_string)
, (Word
dW_AT_external, Word
dW_FORM_flag)
, (Word
dW_AT_low_pc, Word
dW_FORM_addr)
, (Word
dW_AT_high_pc, Word
dW_FORM_addr)
, (Word
dW_AT_frame_base, Word
dW_FORM_block1)
]
in forall doc. IsDoc doc => Platform -> doc
dwarfAbbrevSection Platform
platform forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Line doc -> doc
line (forall doc. IsLine doc => doc
dwarfAbbrevLabel forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => doc
colon) forall doc. IsDoc doc => doc -> doc -> doc
$$
forall {doc}.
IsDoc doc =>
DwarfAbbrev -> Word -> Word8 -> [(Word, Word)] -> doc
mkAbbrev DwarfAbbrev
DwAbbrCompileUnit Word
dW_TAG_compile_unit Word8
dW_CHILDREN_yes
([(Word
dW_AT_name, Word
dW_FORM_string)
, (Word
dW_AT_producer, Word
dW_FORM_string)
, (Word
dW_AT_language, Word
dW_FORM_data4)
, (Word
dW_AT_comp_dir, Word
dW_FORM_string)
, (Word
dW_AT_use_UTF8, Word
dW_FORM_flag_present)
, (Word
dW_AT_low_pc, Word
dW_FORM_addr)
, (Word
dW_AT_high_pc, Word
dW_FORM_addr)
] forall a. [a] -> [a] -> [a]
++
(if Bool
haveDebugLine
then [ (Word
dW_AT_stmt_list, Word
dW_FORM_data4) ]
else [])) forall doc. IsDoc doc => doc -> doc -> doc
$$
forall {doc}.
IsDoc doc =>
DwarfAbbrev -> Word -> Word8 -> [(Word, Word)] -> doc
mkAbbrev DwarfAbbrev
DwAbbrSubprogram Word
dW_TAG_subprogram Word8
dW_CHILDREN_yes
[(Word, Word)]
subprogramAttrs forall doc. IsDoc doc => doc -> doc -> doc
$$
forall {doc}.
IsDoc doc =>
DwarfAbbrev -> Word -> Word8 -> [(Word, Word)] -> doc
mkAbbrev DwarfAbbrev
DwAbbrSubprogramWithParent Word
dW_TAG_subprogram Word8
dW_CHILDREN_yes
([(Word, Word)]
subprogramAttrs forall a. [a] -> [a] -> [a]
++ [(Word
dW_AT_ghc_tick_parent, Word
dW_FORM_ref_addr)]) forall doc. IsDoc doc => doc -> doc -> doc
$$
forall {doc}.
IsDoc doc =>
DwarfAbbrev -> Word -> Word8 -> [(Word, Word)] -> doc
mkAbbrev DwarfAbbrev
DwAbbrBlockWithoutCode Word
dW_TAG_lexical_block Word8
dW_CHILDREN_yes
[ (Word
dW_AT_name, Word
dW_FORM_string)
] forall doc. IsDoc doc => doc -> doc -> doc
$$
forall {doc}.
IsDoc doc =>
DwarfAbbrev -> Word -> Word8 -> [(Word, Word)] -> doc
mkAbbrev DwarfAbbrev
DwAbbrBlock Word
dW_TAG_lexical_block Word8
dW_CHILDREN_yes
[ (Word
dW_AT_name, Word
dW_FORM_string)
, (Word
dW_AT_low_pc, Word
dW_FORM_addr)
, (Word
dW_AT_high_pc, Word
dW_FORM_addr)
] forall doc. IsDoc doc => doc -> doc -> doc
$$
forall {doc}.
IsDoc doc =>
DwarfAbbrev -> Word -> Word8 -> [(Word, Word)] -> doc
mkAbbrev DwarfAbbrev
DwAbbrGhcSrcNote Word
dW_TAG_ghc_src_note Word8
dW_CHILDREN_no
[ (Word
dW_AT_ghc_span_file, Word
dW_FORM_string)
, (Word
dW_AT_ghc_span_start_line, Word
dW_FORM_data4)
, (Word
dW_AT_ghc_span_start_col, Word
dW_FORM_data2)
, (Word
dW_AT_ghc_span_end_line, Word
dW_FORM_data4)
, (Word
dW_AT_ghc_span_end_col, Word
dW_FORM_data2)
] forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
0
{-# SPECIALIZE pprAbbrevDecls :: Platform -> Bool -> SDoc #-}
{-# SPECIALIZE pprAbbrevDecls :: Platform -> Bool -> HDoc #-}
pprDwarfInfo :: IsDoc doc => Platform -> Bool -> DwarfInfo -> doc
pprDwarfInfo :: forall doc. IsDoc doc => Platform -> Bool -> DwarfInfo -> doc
pprDwarfInfo Platform
platform Bool
haveSrc DwarfInfo
d
= case DwarfInfo
d of
DwarfCompileUnit {} -> doc
hasChildren
DwarfSubprogram {} -> doc
hasChildren
DwarfBlock {} -> doc
hasChildren
DwarfSrcNote {} -> doc
noChildren
where
hasChildren :: doc
hasChildren =
forall doc. IsDoc doc => Platform -> Bool -> DwarfInfo -> doc
pprDwarfInfoOpen Platform
platform Bool
haveSrc DwarfInfo
d forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => [doc] -> doc
vcat (forall a b. (a -> b) -> [a] -> [b]
map (forall doc. IsDoc doc => Platform -> Bool -> DwarfInfo -> doc
pprDwarfInfo Platform
platform Bool
haveSrc) (DwarfInfo -> [DwarfInfo]
dwChildren DwarfInfo
d)) forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => doc
pprDwarfInfoClose
noChildren :: doc
noChildren = forall doc. IsDoc doc => Platform -> Bool -> DwarfInfo -> doc
pprDwarfInfoOpen Platform
platform Bool
haveSrc DwarfInfo
d
{-# SPECIALIZE pprDwarfInfo :: Platform -> Bool -> DwarfInfo -> SDoc #-}
{-# SPECIALIZE pprDwarfInfo :: Platform -> Bool -> DwarfInfo -> HDoc #-}
pprLabelString :: IsDoc doc => Platform -> CLabel -> doc
pprLabelString :: forall doc. IsDoc doc => Platform -> CLabel -> doc
pprLabelString Platform
platform CLabel
label =
forall doc. IsDoc doc => Line doc -> doc
pprString'
forall a b. (a -> b) -> a -> b
$ forall doc. IsLine doc => Platform -> CLabel -> doc
pprCLabel Platform
platform CLabel
label
pprDwarfInfoOpen :: IsDoc doc => Platform -> Bool -> DwarfInfo -> doc
pprDwarfInfoOpen :: forall doc. IsDoc doc => Platform -> Bool -> DwarfInfo -> doc
pprDwarfInfoOpen Platform
platform Bool
haveSrc (DwarfCompileUnit [DwarfInfo]
_ String
name String
producer String
compDir CLabel
lowLabel
CLabel
highLabel) =
forall doc. IsDoc doc => DwarfAbbrev -> doc
pprAbbrev DwarfAbbrev
DwAbbrCompileUnit
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => String -> doc
pprString String
name
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => String -> doc
pprString String
producer
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word -> doc
pprData4 Word
dW_LANG_Haskell
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => String -> doc
pprString String
compDir
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> Line doc -> doc
pprWord Platform
platform (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
lowLabel forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => String -> doc
text String
"-1")
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> Line doc -> doc
pprWord Platform
platform (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
highLabel)
forall doc. IsDoc doc => doc -> doc -> doc
$$ if Bool
haveSrc
then forall doc. IsDoc doc => Platform -> Line doc -> Line doc -> doc
sectionOffset Platform
platform forall doc. IsLine doc => doc
dwarfLineLabel forall doc. IsLine doc => doc
dwarfLineLabel
else forall doc. IsOutput doc => doc
empty
pprDwarfInfoOpen Platform
platform Bool
_ (DwarfSubprogram [DwarfInfo]
_ String
name CLabel
label Maybe CLabel
parent) =
forall doc. IsDoc doc => Line doc -> doc
line (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform (CLabel -> CLabel
mkAsmTempDieLabel CLabel
label) forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => doc
colon)
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => DwarfAbbrev -> doc
pprAbbrev DwarfAbbrev
abbrev
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => String -> doc
pprString String
name
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> CLabel -> doc
pprLabelString Platform
platform CLabel
label
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Bool -> doc
pprFlag (CLabel -> Bool
externallyVisibleCLabel CLabel
label)
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> Line doc -> doc
pprWord Platform
platform (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
label forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => String -> doc
text String
"-1")
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> Line doc -> doc
pprWord Platform
platform (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform forall a b. (a -> b) -> a -> b
$ CLabel -> CLabel
mkAsmTempProcEndLabel CLabel
label)
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
1
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_OP_call_frame_cfa
forall doc. IsDoc doc => doc -> doc -> doc
$$ doc
parentValue
where
abbrev :: DwarfAbbrev
abbrev = case Maybe CLabel
parent of Maybe CLabel
Nothing -> DwarfAbbrev
DwAbbrSubprogram
Just CLabel
_ -> DwarfAbbrev
DwAbbrSubprogramWithParent
parentValue :: doc
parentValue = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall doc. IsOutput doc => doc
empty CLabel -> doc
pprParentDie Maybe CLabel
parent
pprParentDie :: CLabel -> doc
pprParentDie CLabel
sym = forall doc. IsDoc doc => Platform -> Line doc -> Line doc -> doc
sectionOffset Platform
platform (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
sym) forall doc. IsLine doc => doc
dwarfInfoLabel
pprDwarfInfoOpen Platform
platform Bool
_ (DwarfBlock [DwarfInfo]
_ CLabel
label Maybe CLabel
Nothing) =
forall doc. IsDoc doc => Line doc -> doc
line (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform (CLabel -> CLabel
mkAsmTempDieLabel CLabel
label) forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => doc
colon)
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => DwarfAbbrev -> doc
pprAbbrev DwarfAbbrev
DwAbbrBlockWithoutCode
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> CLabel -> doc
pprLabelString Platform
platform CLabel
label
pprDwarfInfoOpen Platform
platform Bool
_ (DwarfBlock [DwarfInfo]
_ CLabel
label (Just CLabel
marker)) =
forall doc. IsDoc doc => Line doc -> doc
line (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform (CLabel -> CLabel
mkAsmTempDieLabel CLabel
label) forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => doc
colon)
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => DwarfAbbrev -> doc
pprAbbrev DwarfAbbrev
DwAbbrBlock
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> CLabel -> doc
pprLabelString Platform
platform CLabel
label
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> Line doc -> doc
pprWord Platform
platform (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
marker)
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> Line doc -> doc
pprWord Platform
platform (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform forall a b. (a -> b) -> a -> b
$ CLabel -> CLabel
mkAsmTempEndLabel CLabel
marker)
pprDwarfInfoOpen Platform
_ Bool
_ (DwarfSrcNote RealSrcSpan
ss) =
forall doc. IsDoc doc => DwarfAbbrev -> doc
pprAbbrev DwarfAbbrev
DwAbbrGhcSrcNote
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Line doc -> doc
pprString' (forall doc. IsLine doc => FastString -> doc
ftext forall a b. (a -> b) -> a -> b
$ RealSrcSpan -> FastString
srcSpanFile RealSrcSpan
ss)
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word -> doc
pprData4 (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ RealSrcSpan -> Int
srcSpanStartLine RealSrcSpan
ss)
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word16 -> doc
pprHalf (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ RealSrcSpan -> Int
srcSpanStartCol RealSrcSpan
ss)
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word -> doc
pprData4 (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ RealSrcSpan -> Int
srcSpanEndLine RealSrcSpan
ss)
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word16 -> doc
pprHalf (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ RealSrcSpan -> Int
srcSpanEndCol RealSrcSpan
ss)
pprDwarfInfoClose :: IsDoc doc => doc
pprDwarfInfoClose :: forall doc. IsDoc doc => doc
pprDwarfInfoClose = forall doc. IsDoc doc => DwarfAbbrev -> doc
pprAbbrev DwarfAbbrev
DwAbbrNull
data DwarfARange
= DwarfARange
{ DwarfARange -> CLabel
dwArngStartLabel :: CLabel
, DwarfARange -> CLabel
dwArngEndLabel :: CLabel
}
pprDwarfARanges :: IsDoc doc => Platform -> [DwarfARange] -> Unique -> doc
pprDwarfARanges :: forall doc. IsDoc doc => Platform -> [DwarfARange] -> Unique -> doc
pprDwarfARanges Platform
platform [DwarfARange]
arngs Unique
unitU =
let wordSize :: Int
wordSize = Platform -> Int
platformWordSizeInBytes Platform
platform
paddingSize :: Int
paddingSize = Int
4 :: Int
pad :: Int -> doc
pad Int
n = forall doc. IsDoc doc => [doc] -> doc
vcat forall a b. (a -> b) -> a -> b
$ forall a. Int -> a -> [a]
replicate Int
n forall a b. (a -> b) -> a -> b
$ forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
0
initialLength :: Int
initialLength = Int
8 forall a. Num a => a -> a -> a
+ Int
paddingSize forall a. Num a => a -> a -> a
+ (Int
1 forall a. Num a => a -> a -> a
+ forall (t :: * -> *) a. Foldable t => t a -> Int
length [DwarfARange]
arngs) forall a. Num a => a -> a -> a
* Int
2 forall a. Num a => a -> a -> a
* Int
wordSize
in forall doc. IsDoc doc => Line doc -> doc
pprDwWord (forall doc. IsLine doc => Int -> doc
int Int
initialLength)
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word16 -> doc
pprHalf Word16
2
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> Line doc -> Line doc -> doc
sectionOffset Platform
platform (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform forall a b. (a -> b) -> a -> b
$ forall a. Uniquable a => a -> CLabel
mkAsmTempLabel forall a b. (a -> b) -> a -> b
$ Unique
unitU) forall doc. IsLine doc => doc
dwarfInfoLabel
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word8 -> doc
pprByte (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
wordSize)
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
0
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall {doc}. IsDoc doc => Int -> doc
pad Int
paddingSize
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => [doc] -> doc
vcat (forall a b. (a -> b) -> [a] -> [b]
map (forall doc. IsDoc doc => Platform -> DwarfARange -> doc
pprDwarfARange Platform
platform) [DwarfARange]
arngs)
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> Line doc -> doc
pprWord Platform
platform (forall doc. IsLine doc => Char -> doc
char Char
'0')
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> Line doc -> doc
pprWord Platform
platform (forall doc. IsLine doc => Char -> doc
char Char
'0')
{-# SPECIALIZE pprDwarfARanges :: Platform -> [DwarfARange] -> Unique -> SDoc #-}
{-# SPECIALIZE pprDwarfARanges :: Platform -> [DwarfARange] -> Unique -> HDoc #-}
pprDwarfARange :: IsDoc doc => Platform -> DwarfARange -> doc
pprDwarfARange :: forall doc. IsDoc doc => Platform -> DwarfARange -> doc
pprDwarfARange Platform
platform DwarfARange
arng =
forall doc. IsDoc doc => Platform -> Line doc -> doc
pprWord Platform
platform (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform (DwarfARange -> CLabel
dwArngStartLabel DwarfARange
arng) forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => String -> doc
text String
"-1")
forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> Line doc -> doc
pprWord Platform
platform Line doc
length
where
length :: Line doc
length = forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform (DwarfARange -> CLabel
dwArngEndLabel DwarfARange
arng)
forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => Char -> doc
char Char
'-' forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform (DwarfARange -> CLabel
dwArngStartLabel DwarfARange
arng)
data DwarfFrame
= DwarfFrame
{ DwarfFrame -> CLabel
dwCieLabel :: CLabel
, DwarfFrame -> UnwindTable
dwCieInit :: UnwindTable
, DwarfFrame -> [DwarfFrameProc]
dwCieProcs :: [DwarfFrameProc]
}
data DwarfFrameProc
= DwarfFrameProc
{ DwarfFrameProc -> CLabel
dwFdeProc :: CLabel
, DwarfFrameProc -> Bool
dwFdeHasInfo :: Bool
, DwarfFrameProc -> [DwarfFrameBlock]
dwFdeBlocks :: [DwarfFrameBlock]
}
data DwarfFrameBlock
= DwarfFrameBlock
{ DwarfFrameBlock -> Bool
dwFdeBlkHasInfo :: Bool
, DwarfFrameBlock -> [UnwindPoint]
dwFdeUnwind :: [UnwindPoint]
}
instance OutputableP Platform DwarfFrameBlock where
pdoc :: Platform -> DwarfFrameBlock -> SDoc
pdoc Platform
env (DwarfFrameBlock Bool
hasInfo [UnwindPoint]
unwinds) = forall doc. IsLine doc => doc -> doc
braces forall a b. (a -> b) -> a -> b
$ forall a. Outputable a => a -> SDoc
ppr Bool
hasInfo forall doc. IsLine doc => doc -> doc -> doc
<+> forall env a. OutputableP env a => env -> a -> SDoc
pdoc Platform
env [UnwindPoint]
unwinds
pprDwarfFrame :: forall doc. IsDoc doc => Platform -> DwarfFrame -> doc
pprDwarfFrame :: forall doc. IsDoc doc => Platform -> DwarfFrame -> doc
pprDwarfFrame Platform
platform DwarfFrame{dwCieLabel :: DwarfFrame -> CLabel
dwCieLabel=CLabel
cieLabel,dwCieInit :: DwarfFrame -> UnwindTable
dwCieInit=UnwindTable
cieInit,dwCieProcs :: DwarfFrame -> [DwarfFrameProc]
dwCieProcs=[DwarfFrameProc]
procs}
= let cieStartLabel :: CLabel
cieStartLabel= CLabel -> FastString -> CLabel
mkAsmTempDerivedLabel CLabel
cieLabel (String -> FastString
fsLit String
"_start")
cieEndLabel :: CLabel
cieEndLabel = CLabel -> CLabel
mkAsmTempEndLabel CLabel
cieLabel
length :: Line doc
length = forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
cieEndLabel forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => Char -> doc
char Char
'-' forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
cieStartLabel
spReg :: Word8
spReg = Platform -> GlobalReg -> Word8
dwarfGlobalRegNo Platform
platform GlobalReg
Sp
retReg :: Word8
retReg = Platform -> Word8
dwarfReturnRegNo Platform
platform
wordSize :: Int
wordSize = Platform -> Int
platformWordSizeInBytes Platform
platform
pprInit :: (GlobalReg, Maybe UnwindExpr) -> doc
pprInit :: (GlobalReg, Maybe UnwindExpr) -> doc
pprInit (GlobalReg
g, Maybe UnwindExpr
uw) = forall doc.
IsDoc doc =>
Platform
-> GlobalReg -> (Maybe UnwindExpr, Maybe UnwindExpr) -> doc
pprSetUnwind Platform
platform GlobalReg
g (forall a. Maybe a
Nothing, Maybe UnwindExpr
uw)
preserveSp :: doc
preserveSp = case Platform -> Arch
platformArch Platform
platform of
Arch
ArchX86 -> forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_CFA_same_value forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word -> doc
pprLEBWord Word
4
Arch
ArchX86_64 -> forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_CFA_same_value forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word -> doc
pprLEBWord Word
7
Arch
_ -> forall doc. IsOutput doc => doc
empty
in forall doc. IsDoc doc => [doc] -> doc
vcat [ forall doc. IsDoc doc => Line doc -> doc
line (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
cieLabel forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => doc
colon)
, forall doc. IsDoc doc => Line doc -> doc
pprData4' Line doc
length
, forall doc. IsDoc doc => Line doc -> doc
line (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
cieStartLabel forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => doc
colon)
, forall doc. IsDoc doc => Line doc -> doc
pprData4' (forall doc. IsLine doc => String -> doc
text String
"-1")
, forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
3
, forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
0
, forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
1
, forall doc. IsDoc doc => Word8 -> doc
pprByte (Word8
128forall a. Num a => a -> a -> a
-forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
wordSize)
, forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
retReg
] forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => [doc] -> doc
vcat (forall a b. (a -> b) -> [a] -> [b]
map (GlobalReg, Maybe UnwindExpr) -> doc
pprInit forall a b. (a -> b) -> a -> b
$ forall k a. Map k a -> [(k, a)]
Map.toList UnwindTable
cieInit) forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => [doc] -> doc
vcat [
forall doc. IsDoc doc => Word8 -> doc
pprByte (Word8
dW_CFA_offsetforall a. Num a => a -> a -> a
+Word8
retReg)
, forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
0
, doc
preserveSp
, forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_CFA_val_offset
, forall doc. IsDoc doc => Word -> doc
pprLEBWord (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
spReg)
, forall doc. IsDoc doc => Word -> doc
pprLEBWord Word
0
] forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Platform -> doc
wordAlign Platform
platform forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Line doc -> doc
line (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
cieEndLabel forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => doc
colon) forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => [doc] -> doc
vcat (forall a b. (a -> b) -> [a] -> [b]
map (forall doc.
IsDoc doc =>
Platform -> CLabel -> UnwindTable -> DwarfFrameProc -> doc
pprFrameProc Platform
platform CLabel
cieLabel UnwindTable
cieInit) [DwarfFrameProc]
procs)
{-# SPECIALIZE pprDwarfFrame :: Platform -> DwarfFrame -> SDoc #-}
{-# SPECIALIZE pprDwarfFrame :: Platform -> DwarfFrame -> HDoc #-}
pprFrameProc :: IsDoc doc => Platform -> CLabel -> UnwindTable -> DwarfFrameProc -> doc
pprFrameProc :: forall doc.
IsDoc doc =>
Platform -> CLabel -> UnwindTable -> DwarfFrameProc -> doc
pprFrameProc Platform
platform CLabel
frameLbl UnwindTable
initUw (DwarfFrameProc CLabel
procLbl Bool
hasInfo [DwarfFrameBlock]
blocks)
= let fdeLabel :: CLabel
fdeLabel = CLabel -> FastString -> CLabel
mkAsmTempDerivedLabel CLabel
procLbl (String -> FastString
fsLit String
"_fde")
fdeEndLabel :: CLabel
fdeEndLabel = CLabel -> FastString -> CLabel
mkAsmTempDerivedLabel CLabel
procLbl (String -> FastString
fsLit String
"_fde_end")
procEnd :: CLabel
procEnd = CLabel -> CLabel
mkAsmTempProcEndLabel CLabel
procLbl
ifInfo :: String -> Line doc
ifInfo String
str = if Bool
hasInfo then forall doc. IsLine doc => String -> doc
text String
str else forall doc. IsOutput doc => doc
empty
in forall doc. IsDoc doc => [doc] -> doc
vcat [ forall doc. IsOutput doc => doc -> doc
whenPprDebug forall a b. (a -> b) -> a -> b
$ forall doc. IsDoc doc => Line doc -> doc
line forall a b. (a -> b) -> a -> b
$ forall doc. IsLine doc => String -> doc
text String
"# Unwinding for" forall doc. IsLine doc => doc -> doc -> doc
<+> forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
procLbl forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => doc
colon
, forall doc. IsDoc doc => Line doc -> doc
pprData4' (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
fdeEndLabel forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => Char -> doc
char Char
'-' forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
fdeLabel)
, forall doc. IsDoc doc => Line doc -> doc
line (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
fdeLabel forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => doc
colon)
, forall doc. IsDoc doc => Line doc -> doc
pprData4' (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
frameLbl forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => Char -> doc
char Char
'-' forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => doc
dwarfFrameLabel)
, forall doc. IsDoc doc => Platform -> Line doc -> doc
pprWord Platform
platform (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
procLbl forall doc. IsLine doc => doc -> doc -> doc
<> String -> Line doc
ifInfo String
"-1")
, forall doc. IsDoc doc => Platform -> Line doc -> doc
pprWord Platform
platform (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
procEnd forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => Char -> doc
char Char
'-' forall doc. IsLine doc => doc -> doc -> doc
<>
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
procLbl forall doc. IsLine doc => doc -> doc -> doc
<> String -> Line doc
ifInfo String
"+1")
] forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => [doc] -> doc
vcat (forall s a. State s a -> s -> a
S.evalState (forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall doc.
IsDoc doc =>
Platform -> DwarfFrameBlock -> State UnwindTable doc
pprFrameBlock Platform
platform) [DwarfFrameBlock]
blocks) UnwindTable
initUw) forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Platform -> doc
wordAlign Platform
platform forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Line doc -> doc
line (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
fdeEndLabel forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => doc
colon)
pprFrameBlock :: forall doc. IsDoc doc => Platform -> DwarfFrameBlock -> S.State UnwindTable doc
pprFrameBlock :: forall doc.
IsDoc doc =>
Platform -> DwarfFrameBlock -> State UnwindTable doc
pprFrameBlock Platform
platform (DwarfFrameBlock Bool
hasInfo [UnwindPoint]
uws0) =
forall doc. IsDoc doc => [doc] -> doc
vcat forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM Bool -> UnwindPoint -> State UnwindTable doc
pprFrameDecl (Bool
True forall a. a -> [a] -> [a]
: forall a. a -> [a]
repeat Bool
False) [UnwindPoint]
uws0
where
pprFrameDecl :: Bool -> UnwindPoint -> S.State UnwindTable doc
pprFrameDecl :: Bool -> UnwindPoint -> State UnwindTable doc
pprFrameDecl Bool
firstDecl (UnwindPoint CLabel
lbl UnwindTable
uws) = forall s a. (s -> (a, s)) -> State s a
S.state forall a b. (a -> b) -> a -> b
$ \UnwindTable
oldUws ->
let
isChanged :: GlobalReg -> Maybe UnwindExpr
-> Maybe (Maybe UnwindExpr, Maybe UnwindExpr)
isChanged :: GlobalReg
-> Maybe UnwindExpr -> Maybe (Maybe UnwindExpr, Maybe UnwindExpr)
isChanged GlobalReg
g Maybe UnwindExpr
new
| forall a. a -> Maybe a
Just Maybe UnwindExpr
new forall a. Eq a => a -> a -> Bool
== Maybe (Maybe UnwindExpr)
old = forall a. Maybe a
Nothing
| Maybe (Maybe UnwindExpr)
Nothing <- Maybe (Maybe UnwindExpr)
old
, Maybe UnwindExpr
Nothing <- Maybe UnwindExpr
new = forall a. Maybe a
Nothing
| Bool
otherwise = forall a. a -> Maybe a
Just (forall (m :: * -> *) a. Monad m => m (m a) -> m a
join Maybe (Maybe UnwindExpr)
old, Maybe UnwindExpr
new)
where
old :: Maybe (Maybe UnwindExpr)
old = forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup GlobalReg
g UnwindTable
oldUws
changed :: [(GlobalReg, (Maybe UnwindExpr, Maybe UnwindExpr))]
changed = forall k a. Map k a -> [(k, a)]
Map.toList forall a b. (a -> b) -> a -> b
$ forall k a b. (k -> a -> Maybe b) -> Map k a -> Map k b
Map.mapMaybeWithKey GlobalReg
-> Maybe UnwindExpr -> Maybe (Maybe UnwindExpr, Maybe UnwindExpr)
isChanged UnwindTable
uws
in if UnwindTable
oldUws forall a. Eq a => a -> a -> Bool
== UnwindTable
uws
then (forall doc. IsOutput doc => doc
empty, UnwindTable
oldUws)
else let
needsOffset :: Bool
needsOffset = Bool
firstDecl Bool -> Bool -> Bool
&& Bool
hasInfo
lblDoc :: Line doc
lblDoc = forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
lbl forall doc. IsLine doc => doc -> doc -> doc
<>
if Bool
needsOffset then forall doc. IsLine doc => String -> doc
text String
"-1" else forall doc. IsOutput doc => doc
empty
doc :: doc
doc = forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_CFA_set_loc forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> Line doc -> doc
pprWord Platform
platform Line doc
lblDoc forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => [doc] -> doc
vcat (forall a b. (a -> b) -> [a] -> [b]
map (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a b. (a -> b) -> a -> b
$ forall doc.
IsDoc doc =>
Platform
-> GlobalReg -> (Maybe UnwindExpr, Maybe UnwindExpr) -> doc
pprSetUnwind Platform
platform) [(GlobalReg, (Maybe UnwindExpr, Maybe UnwindExpr))]
changed)
in (doc
doc, UnwindTable
uws)
dwarfGlobalRegNo :: Platform -> GlobalReg -> Word8
dwarfGlobalRegNo :: Platform -> GlobalReg -> Word8
dwarfGlobalRegNo Platform
p GlobalReg
UnwindReturnReg = Platform -> Word8
dwarfReturnRegNo Platform
p
dwarfGlobalRegNo Platform
p GlobalReg
reg = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Word8
0 (Platform -> Reg -> Word8
dwarfRegNo Platform
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. RealReg -> Reg
RegReal) forall a b. (a -> b) -> a -> b
$ Platform -> GlobalReg -> Maybe RealReg
globalRegMaybe Platform
p GlobalReg
reg
pprSetUnwind :: IsDoc doc => Platform
-> GlobalReg
-> (Maybe UnwindExpr, Maybe UnwindExpr)
-> doc
pprSetUnwind :: forall doc.
IsDoc doc =>
Platform
-> GlobalReg -> (Maybe UnwindExpr, Maybe UnwindExpr) -> doc
pprSetUnwind Platform
plat GlobalReg
g (Maybe UnwindExpr
_, Maybe UnwindExpr
Nothing)
= forall doc. IsDoc doc => Platform -> GlobalReg -> doc
pprUndefUnwind Platform
plat GlobalReg
g
pprSetUnwind Platform
_ GlobalReg
Sp (Just (UwReg GlobalReg
s Int
_), Just (UwReg GlobalReg
s' Int
o')) | GlobalReg
s forall a. Eq a => a -> a -> Bool
== GlobalReg
s'
= if Int
o' forall a. Ord a => a -> a -> Bool
>= Int
0
then forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_CFA_def_cfa_offset forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word -> doc
pprLEBWord (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
o')
else forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_CFA_def_cfa_offset_sf forall doc. IsDoc doc => doc -> doc -> doc
$$ forall {doc}. IsDoc doc => Int -> doc
pprLEBInt Int
o'
pprSetUnwind Platform
plat GlobalReg
Sp (Maybe UnwindExpr
_, Just (UwReg GlobalReg
s' Int
o'))
= if Int
o' forall a. Ord a => a -> a -> Bool
>= Int
0
then forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_CFA_def_cfa forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Platform -> GlobalReg -> doc
pprLEBRegNo Platform
plat GlobalReg
s' forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Word -> doc
pprLEBWord (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
o')
else forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_CFA_def_cfa_sf forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Platform -> GlobalReg -> doc
pprLEBRegNo Platform
plat GlobalReg
s' forall doc. IsDoc doc => doc -> doc -> doc
$$
forall {doc}. IsDoc doc => Int -> doc
pprLEBInt Int
o'
pprSetUnwind Platform
plat GlobalReg
Sp (Maybe UnwindExpr
_, Just UnwindExpr
uw)
= forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_CFA_def_cfa_expression forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> Bool -> UnwindExpr -> doc
pprUnwindExpr Platform
plat Bool
False UnwindExpr
uw
pprSetUnwind Platform
plat GlobalReg
g (Maybe UnwindExpr
_, Just (UwDeref (UwReg GlobalReg
Sp Int
o)))
| Int
o forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
&& ((-Int
o) forall a. Integral a => a -> a -> a
`mod` Platform -> Int
platformWordSizeInBytes Platform
plat) forall a. Eq a => a -> a -> Bool
== Int
0
= forall doc. IsDoc doc => Word8 -> doc
pprByte (Word8
dW_CFA_offset forall a. Num a => a -> a -> a
+ Platform -> GlobalReg -> Word8
dwarfGlobalRegNo Platform
plat GlobalReg
g) forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Word -> doc
pprLEBWord (forall a b. (Integral a, Num b) => a -> b
fromIntegral ((-Int
o) forall a. Integral a => a -> a -> a
`div` Platform -> Int
platformWordSizeInBytes Platform
plat))
| Bool
otherwise
= forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_CFA_offset_extended_sf forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Platform -> GlobalReg -> doc
pprLEBRegNo Platform
plat GlobalReg
g forall doc. IsDoc doc => doc -> doc -> doc
$$
forall {doc}. IsDoc doc => Int -> doc
pprLEBInt Int
o
pprSetUnwind Platform
plat GlobalReg
g (Maybe UnwindExpr
_, Just (UwDeref UnwindExpr
uw))
= forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_CFA_expression forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Platform -> GlobalReg -> doc
pprLEBRegNo Platform
plat GlobalReg
g forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Platform -> Bool -> UnwindExpr -> doc
pprUnwindExpr Platform
plat Bool
True UnwindExpr
uw
pprSetUnwind Platform
plat GlobalReg
g (Maybe UnwindExpr
_, Just (UwReg GlobalReg
g' Int
0))
| GlobalReg
g forall a. Eq a => a -> a -> Bool
== GlobalReg
g'
= forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_CFA_same_value forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Platform -> GlobalReg -> doc
pprLEBRegNo Platform
plat GlobalReg
g
pprSetUnwind Platform
plat GlobalReg
g (Maybe UnwindExpr
_, Just UnwindExpr
uw)
= forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_CFA_val_expression forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Platform -> GlobalReg -> doc
pprLEBRegNo Platform
plat GlobalReg
g forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Platform -> Bool -> UnwindExpr -> doc
pprUnwindExpr Platform
plat Bool
True UnwindExpr
uw
pprLEBRegNo :: IsDoc doc => Platform -> GlobalReg -> doc
pprLEBRegNo :: forall doc. IsDoc doc => Platform -> GlobalReg -> doc
pprLEBRegNo Platform
plat = forall doc. IsDoc doc => Word -> doc
pprLEBWord forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. Platform -> GlobalReg -> Word8
dwarfGlobalRegNo Platform
plat
pprUnwindExpr :: IsDoc doc => Platform -> Bool -> UnwindExpr -> doc
pprUnwindExpr :: forall doc. IsDoc doc => Platform -> Bool -> UnwindExpr -> doc
pprUnwindExpr Platform
platform Bool
spIsCFA UnwindExpr
expr
= let pprE :: UnwindExpr -> doc
pprE (UwConst Int
i)
| Int
i forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
i forall a. Ord a => a -> a -> Bool
< Int
32 = forall doc. IsDoc doc => Word8 -> doc
pprByte (Word8
dW_OP_lit0 forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i)
| Bool
otherwise = forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_OP_consts forall doc. IsDoc doc => doc -> doc -> doc
$$ forall {doc}. IsDoc doc => Int -> doc
pprLEBInt Int
i
pprE (UwReg GlobalReg
Sp Int
i) | Bool
spIsCFA
= if Int
i forall a. Eq a => a -> a -> Bool
== Int
0
then forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_OP_call_frame_cfa
else UnwindExpr -> doc
pprE (UnwindExpr -> UnwindExpr -> UnwindExpr
UwPlus (GlobalReg -> Int -> UnwindExpr
UwReg GlobalReg
Sp Int
0) (Int -> UnwindExpr
UwConst Int
i))
pprE (UwReg GlobalReg
g Int
i) = forall doc. IsDoc doc => Word8 -> doc
pprByte (Word8
dW_OP_breg0forall a. Num a => a -> a -> a
+Platform -> GlobalReg -> Word8
dwarfGlobalRegNo Platform
platform GlobalReg
g) forall doc. IsDoc doc => doc -> doc -> doc
$$
forall {doc}. IsDoc doc => Int -> doc
pprLEBInt Int
i
pprE (UwDeref UnwindExpr
u) = UnwindExpr -> doc
pprE UnwindExpr
u forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_OP_deref
pprE (UwLabel CLabel
l) = forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_OP_addr forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Platform -> Line doc -> doc
pprWord Platform
platform (forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
l)
pprE (UwPlus UnwindExpr
u1 UnwindExpr
u2) = UnwindExpr -> doc
pprE UnwindExpr
u1 forall doc. IsDoc doc => doc -> doc -> doc
$$ UnwindExpr -> doc
pprE UnwindExpr
u2 forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_OP_plus
pprE (UwMinus UnwindExpr
u1 UnwindExpr
u2) = UnwindExpr -> doc
pprE UnwindExpr
u1 forall doc. IsDoc doc => doc -> doc -> doc
$$ UnwindExpr -> doc
pprE UnwindExpr
u2 forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_OP_minus
pprE (UwTimes UnwindExpr
u1 UnwindExpr
u2) = UnwindExpr -> doc
pprE UnwindExpr
u1 forall doc. IsDoc doc => doc -> doc -> doc
$$ UnwindExpr -> doc
pprE UnwindExpr
u2 forall doc. IsDoc doc => doc -> doc -> doc
$$ forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_OP_mul
in forall doc. IsDoc doc => Line doc -> doc
line (forall doc. IsLine doc => String -> doc
text String
"\t.uleb128 2f-1f") forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Line doc -> doc
line (forall doc. IsLine doc => String -> doc
text String
"1:") forall doc. IsDoc doc => doc -> doc -> doc
$$
UnwindExpr -> doc
pprE UnwindExpr
expr forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Line doc -> doc
line (forall doc. IsLine doc => String -> doc
text String
"2:")
pprUndefUnwind :: IsDoc doc => Platform -> GlobalReg -> doc
pprUndefUnwind :: forall doc. IsDoc doc => Platform -> GlobalReg -> doc
pprUndefUnwind Platform
plat GlobalReg
g = forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
dW_CFA_undefined forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Platform -> GlobalReg -> doc
pprLEBRegNo Platform
plat GlobalReg
g
wordAlign :: IsDoc doc => Platform -> doc
wordAlign :: forall doc. IsDoc doc => Platform -> doc
wordAlign Platform
plat =
forall doc. IsDoc doc => Line doc -> doc
line forall a b. (a -> b) -> a -> b
$ forall doc. IsLine doc => String -> doc
text String
"\t.align " forall doc. IsLine doc => doc -> doc -> doc
<> case Platform -> OS
platformOS Platform
plat of
OS
OSDarwin -> case Platform -> PlatformWordSize
platformWordSize Platform
plat of
PlatformWordSize
PW8 -> forall doc. IsLine doc => Char -> doc
char Char
'3'
PlatformWordSize
PW4 -> forall doc. IsLine doc => Char -> doc
char Char
'2'
OS
_other -> forall doc. IsLine doc => Int -> doc
int (Platform -> Int
platformWordSizeInBytes Platform
plat)
{-# SPECIALIZE wordAlign :: Platform -> SDoc #-}
{-# SPECIALIZE wordAlign :: Platform -> HDoc #-}
pprByte :: IsDoc doc => Word8 -> doc
pprByte :: forall doc. IsDoc doc => Word8 -> doc
pprByte Word8
x = forall doc. IsDoc doc => Line doc -> doc
line forall a b. (a -> b) -> a -> b
$ forall doc. IsLine doc => String -> doc
text String
"\t.byte " forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => Integer -> doc
integer (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
x)
{-# SPECIALIZE pprByte :: Word8 -> SDoc #-}
{-# SPECIALIZE pprByte :: Word8 -> HDoc #-}
pprHalf :: IsDoc doc => Word16 -> doc
pprHalf :: forall doc. IsDoc doc => Word16 -> doc
pprHalf Word16
x = forall doc. IsDoc doc => Line doc -> doc
line forall a b. (a -> b) -> a -> b
$ forall doc. IsLine doc => String -> doc
text String
"\t.short" forall doc. IsLine doc => doc -> doc -> doc
<+> forall doc. IsLine doc => Integer -> doc
integer (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
x)
{-# SPECIALIZE pprHalf :: Word16 -> SDoc #-}
{-# SPECIALIZE pprHalf :: Word16 -> HDoc #-}
pprFlag :: IsDoc doc => Bool -> doc
pprFlag :: forall doc. IsDoc doc => Bool -> doc
pprFlag Bool
f = forall doc. IsDoc doc => Word8 -> doc
pprByte (if Bool
f then Word8
0xff else Word8
0x00)
pprData4' :: IsDoc doc => Line doc -> doc
pprData4' :: forall doc. IsDoc doc => Line doc -> doc
pprData4' Line doc
x = forall doc. IsDoc doc => Line doc -> doc
line (forall doc. IsLine doc => String -> doc
text String
"\t.long " forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
x)
{-# SPECIALIZE pprData4' :: SDoc -> SDoc #-}
{-# SPECIALIZE pprData4' :: HLine -> HDoc #-}
pprData4 :: IsDoc doc => Word -> doc
pprData4 :: forall doc. IsDoc doc => Word -> doc
pprData4 = forall doc. IsDoc doc => Line doc -> doc
pprData4' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall doc. IsLine doc => Integer -> doc
integer forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
pprDwWord :: IsDoc doc => Line doc -> doc
pprDwWord :: forall doc. IsDoc doc => Line doc -> doc
pprDwWord = forall doc. IsDoc doc => Line doc -> doc
pprData4'
{-# SPECIALIZE pprDwWord :: SDoc -> SDoc #-}
{-# SPECIALIZE pprDwWord :: HLine -> HDoc #-}
pprWord :: IsDoc doc => Platform -> Line doc -> doc
pprWord :: forall doc. IsDoc doc => Platform -> Line doc -> doc
pprWord Platform
plat Line doc
s =
forall doc. IsDoc doc => Line doc -> doc
line forall a b. (a -> b) -> a -> b
$ case Platform -> PlatformWordSize
platformWordSize Platform
plat of
PlatformWordSize
PW4 -> forall doc. IsLine doc => String -> doc
text String
"\t.long " forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
s
PlatformWordSize
PW8 -> forall doc. IsLine doc => String -> doc
text String
"\t.quad " forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
s
{-# SPECIALIZE pprWord :: Platform -> SDoc -> SDoc #-}
{-# SPECIALIZE pprWord :: Platform -> HLine -> HDoc #-}
pprLEBWord :: IsDoc doc => Word -> doc
pprLEBWord :: forall doc. IsDoc doc => Word -> doc
pprLEBWord Word
x | Word
x forall a. Ord a => a -> a -> Bool
< Word
128 = forall doc. IsDoc doc => Word8 -> doc
pprByte (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
x)
| Bool
otherwise = forall doc. IsDoc doc => Word8 -> doc
pprByte (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Word
128 forall a. Bits a => a -> a -> a
.|. (Word
x forall a. Bits a => a -> a -> a
.&. Word
127)) forall doc. IsDoc doc => doc -> doc -> doc
$$
forall doc. IsDoc doc => Word -> doc
pprLEBWord (Word
x forall a. Bits a => a -> Int -> a
`shiftR` Int
7)
{-# SPECIALIZE pprLEBWord :: Word -> SDoc #-}
{-# SPECIALIZE pprLEBWord :: Word -> HDoc #-}
pprLEBInt :: IsDoc doc => Int -> doc
pprLEBInt :: forall {doc}. IsDoc doc => Int -> doc
pprLEBInt Int
x | Int
x forall a. Ord a => a -> a -> Bool
>= -Int
64 Bool -> Bool -> Bool
&& Int
x forall a. Ord a => a -> a -> Bool
< Int
64
= forall doc. IsDoc doc => Word8 -> doc
pprByte (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
x forall a. Bits a => a -> a -> a
.&. Int
127))
| Bool
otherwise = forall doc. IsDoc doc => Word8 -> doc
pprByte (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Int
128 forall a. Bits a => a -> a -> a
.|. (Int
x forall a. Bits a => a -> a -> a
.&. Int
127)) forall doc. IsDoc doc => doc -> doc -> doc
$$
forall {doc}. IsDoc doc => Int -> doc
pprLEBInt (Int
x forall a. Bits a => a -> Int -> a
`shiftR` Int
7)
{-# SPECIALIZE pprLEBInt :: Int -> SDoc #-}
{-# SPECIALIZE pprLEBInt :: Int -> HDoc #-}
pprString' :: IsDoc doc => Line doc -> doc
pprString' :: forall doc. IsDoc doc => Line doc -> doc
pprString' Line doc
str = forall doc. IsDoc doc => Line doc -> doc
line (forall doc. IsLine doc => String -> doc
text String
"\t.asciz \"" forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
str forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => Char -> doc
char Char
'"')
pprString :: IsDoc doc => String -> doc
pprString :: forall doc. IsDoc doc => String -> doc
pprString String
str
= forall doc. IsDoc doc => Line doc -> doc
pprString' forall a b. (a -> b) -> a -> b
$ forall doc. IsLine doc => [doc] -> doc
hcat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall doc. IsLine doc => Char -> doc
escapeChar forall a b. (a -> b) -> a -> b
$
if String
str forall a. [a] -> Int -> Bool
`lengthIs` String -> Int
utf8EncodedLength String
str
then String
str
else forall a b. (a -> b) -> [a] -> [b]
map (Int -> Char
chr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral) forall a b. (a -> b) -> a -> b
$ ByteString -> [Word8]
BS.unpack forall a b. (a -> b) -> a -> b
$ String -> ByteString
utf8EncodeByteString String
str
escapeChar :: IsLine doc => Char -> doc
escapeChar :: forall doc. IsLine doc => Char -> doc
escapeChar Char
'\\' = forall doc. IsLine doc => String -> doc
text String
"\\\\"
escapeChar Char
'\"' = forall doc. IsLine doc => String -> doc
text String
"\\\""
escapeChar Char
'\n' = forall doc. IsLine doc => String -> doc
text String
"\\n"
escapeChar Char
c
| Char -> Bool
isAscii Char
c Bool -> Bool -> Bool
&& Char -> Bool
isPrint Char
c Bool -> Bool -> Bool
&& Char
c forall a. Eq a => a -> a -> Bool
/= Char
'?'
= forall doc. IsLine doc => Char -> doc
char Char
c
| Bool
otherwise
= forall doc. IsLine doc => Char -> doc
char Char
'\\' forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => Char -> doc
char (Int -> Char
intToDigit (Int
ch forall a. Integral a => a -> a -> a
`div` Int
64)) forall doc. IsLine doc => doc -> doc -> doc
<>
forall doc. IsLine doc => Char -> doc
char (Int -> Char
intToDigit ((Int
ch forall a. Integral a => a -> a -> a
`div` Int
8) forall a. Integral a => a -> a -> a
`mod` Int
8)) forall doc. IsLine doc => doc -> doc -> doc
<>
forall doc. IsLine doc => Char -> doc
char (Int -> Char
intToDigit (Int
ch forall a. Integral a => a -> a -> a
`mod` Int
8))
where ch :: Int
ch = Char -> Int
ord Char
c
sectionOffset :: IsDoc doc => Platform -> Line doc -> Line doc -> doc
sectionOffset :: forall doc. IsDoc doc => Platform -> Line doc -> Line doc -> doc
sectionOffset Platform
plat Line doc
target Line doc
section =
case Platform -> OS
platformOS Platform
plat of
OS
OSDarwin -> forall doc. IsDoc doc => Line doc -> doc
pprDwWord (Line doc
target forall doc. IsLine doc => doc -> doc -> doc
<> forall doc. IsLine doc => Char -> doc
char Char
'-' forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
section)
OS
OSMinGW32 -> forall doc. IsDoc doc => Line doc -> doc
line (forall doc. IsLine doc => String -> doc
text String
"\t.secrel32 " forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
target)
OS
_other -> forall doc. IsDoc doc => Line doc -> doc
pprDwWord Line doc
target
{-# SPECIALIZE sectionOffset :: Platform -> SDoc -> SDoc -> SDoc #-}
{-# SPECIALIZE sectionOffset :: Platform -> HLine -> HLine -> HDoc #-}