Unpack.hs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. module Gidl.Backend.Ivory.Unpack where
  2. import Data.List (intercalate)
  3. import Ivory.Artifact
  4. unpackModule :: [String] -> Artifact
  5. unpackModule modulepath =
  6. artifactPath (intercalate "/" modulepath) $
  7. artifactString "Unpack.hs" $
  8. unlines
  9. [ ""
  10. , "{-# LANGUAGE DataKinds #-}"
  11. , "{-# LANGUAGE RankNTypes #-}"
  12. , "{-# LANGUAGE ScopedTypeVariables #-}"
  13. , ""
  14. , "module " ++ (intercalate "." modulepath) ++ ".Unpack where"
  15. , ""
  16. , "import Ivory.Language"
  17. , "import Ivory.Serialize"
  18. , "import Ivory.Stdlib"
  19. , ""
  20. , "unpackWithCallback :: forall n a s1 s2 r b s0"
  21. , " . (ANat n, IvorySizeOf a, IvoryArea a, IvoryZero a, Packable a)"
  22. , " => ConstRef s1 (Array n (Stored Uint8))"
  23. , " -> Ref s2 (Stored Uint32)"
  24. , " -> (ConstRef (Stack s0) a -> Ivory ('Effects r b (Scope s0)) ())"
  25. , " -> Ivory ('Effects r b (Scope s0)) ()"
  26. , "unpackWithCallback arr offs k = do"
  27. , " o <- deref offs"
  28. , " let sufficient_remaining = ((o + sizeOf (Proxy :: Proxy a)) <?"
  29. , " sizeOf (Proxy :: Proxy (Array n (Stored Uint8))))"
  30. , " when sufficient_remaining $ do"
  31. , " v <- local izero"
  32. , " unpackFrom arr o v"
  33. , " k (constRef v)"
  34. , " offs += undefined"
  35. ]