This module provides a way to serialize graph-like structures into
lazy ByteString
s. Graph-like structures here are structures that
may reference other locations in the resulting output. The references
are serialized as relative byte offsets.
A simple example:
test1 :: [Word8] test1 = L.unpack $ toLazyByteString id $ do r <-newRegion
l1 <-label
remit
r (42 :: Word32)reference
S4 LE r l1 emit r (43 :: Word32) test1 == [42,0,0,0,252,255,255,255,43,0,0,0]
- data BuildM a
- toLazyByteString :: ([Region] -> [Region]) -> BuildM () -> ByteString
- data Region
- newRegion :: BuildM Region
- data Label
- label :: Region -> BuildM Label
- makeLabel :: BuildM Label
- placeLabel :: Region -> Label -> BuildM ()
- reference :: Size -> ByteOrder -> Region -> Label -> BuildM ()
- data Size
- sizeToBytes :: Size -> Int
- data ByteOrder
- emitWord8 :: Region -> Word8 -> BuildM ()
- emitWord8s :: Region -> [Word8] -> BuildM ()
- emitWord16le :: Region -> Word16 -> BuildM ()
- emitWord16be :: Region -> Word16 -> BuildM ()
- emitWord16host :: Region -> Word16 -> BuildM ()
- emitWord32le :: Region -> Word32 -> BuildM ()
- emitWord32be :: Region -> Word32 -> BuildM ()
- emitWord32host :: Region -> Word32 -> BuildM ()
- emitWord64le :: Region -> Word64 -> BuildM ()
- emitWord64be :: Region -> Word64 -> BuildM ()
- emitWord64host :: Region -> Word64 -> BuildM ()
- emitInt8 :: Region -> Int8 -> BuildM ()
- emitInt8s :: Region -> [Int8] -> BuildM ()
- emitInt16le :: Region -> Int16 -> BuildM ()
- emitInt16be :: Region -> Int16 -> BuildM ()
- emitInt16host :: Region -> Int16 -> BuildM ()
- emitInt32le :: Region -> Int32 -> BuildM ()
- emitInt32be :: Region -> Int32 -> BuildM ()
- emitInt32host :: Region -> Int32 -> BuildM ()
- emitInt64le :: Region -> Int64 -> BuildM ()
- emitInt64be :: Region -> Int64 -> BuildM ()
- emitInt64host :: Region -> Int64 -> BuildM ()
- emitStorable :: Storable a => Region -> a -> BuildM ()
- emitStorableList :: Storable a => Region -> [a] -> BuildM ()
- padTo :: Region -> Int -> Word8 -> BuildM ()
- alignedLabel :: Region -> Int -> BuildM Label
Monad and ByteString construction
Monad for constructing the serialised structure.
:: ([Region] -> [Region]) | Determines the ordering of the regions. If you pass |
-> BuildM () | |
-> ByteString |
Serialise the graph into a lazy ByteString
.
Regions
Emitting Data, Labels, References
makeLabel :: BuildM LabelSource
Create a new label (with no location attached to it).
It is up to the user to ensure that if this label is ever used in a
reference
, then the label must have been placed via placeLabel
.
This is intended for forward references within a region:
example r = do l <- makeLabel reference S4 Host r l ... more stuff ... placeLabel r l ... other stuff ...
placeLabel :: Region -> Label -> BuildM ()Source
Place a label previously created with makeLabel
.
This function must only be called once per label. If the same label is placed multiple times, it is undefined where references to it point to.
:: Size | The size of the reference in bytes. |
-> ByteOrder | Byte order used for encoding the reference. |
-> Region | The region in which the reference will be emitted. |
-> Label | The target label. |
-> BuildM () |
Emit a reference to the given label in the current region.
The reference will be encoded as a signed integer that specifies the relative distance (in bytes) from the current location to the target label.
The current location starts before the reference. A serialised
reference with value 0
therefore refers to itself.
It is up to the user to ensure that references are large enough to
encode the required range. If they are not in range
toLazyByteString
will fail.
sizeToBytes :: Size -> IntSource
Translate Size
into matching number of bytes.
Emitting Words
emitWord16host :: Region -> Word16 -> BuildM ()Source
Emit a Word16
in native host order and host endianness.
emitWord32host :: Region -> Word32 -> BuildM ()Source
Emit a Word32
in native host order and host endianness.
emitWord64host :: Region -> Word64 -> BuildM ()Source
Emit a Word64
in native host order and host endianness.
Emitting Ints
emitInt16host :: Region -> Int16 -> BuildM ()Source
Emit a Int16
in native host order and host endianness.
emitInt32host :: Region -> Int32 -> BuildM ()Source
Emit a Int32
in native host order and host endianness.
emitInt64host :: Region -> Int64 -> BuildM ()Source
Emit a Int64
in native host order and host endianness.
Storables
emitStorable :: Storable a => Region -> a -> BuildM ()Source
Emit an instance of Storable
. Does not take into account alignment.
emitStorableList :: Storable a => Region -> [a] -> BuildM ()Source
Emit a list of Storable
instances. Ignores alignment.
Alignment
Insert padding bytes into given region until its size is a multiple of the expected alignment.