2 % (c) The AQUA Project, Glasgow University, 1994-2000
6 {-# OPTIONS -fno-implicit-prelude #-}
13 hugsprimPmFromInteger,
16 hugsprimRunIO_toplevel,
20 hugsprimCreateAdjThunk,
32 import PrelReal(Integral)
33 import Prelude(fromIntegral)
34 import IO(putStr,hFlush,stdout,stderr)
35 import PrelException(catch,catchException)
36 import PrelIOBase(IO(..),unsafePerformIO)
37 import PrelShow(show,shows,showString,showChar,Show,ShowS)
38 import PrelRead(Read,ReadS,lex,reads)
39 import PrelFloat(Double)
40 import PrelReal(Fractional,fromRational,toRational)
41 import PrelAddr(Addr(..),nullAddr)
42 import PrelStable(StablePtr,makeStablePtr)
44 import PrelPack(unpackCString)
47 -- Stuff needed by Hugs for desugaring. Do not mess with these!
48 -- They need to correspond exactly to versions written in
49 -- the Hugs standalone Prelude.
51 -- hugs doesn't know about RealWorld and so throws this
52 -- away if the original type signature is used
53 -- hugsprimMkIO :: (RealWorld -> (a,RealWorld)) -> IO a
55 -- The first arg is an IO value created by Hugs, without the
56 -- newtype ST wrapper. What we do here place a wrapper around
57 -- it, so that it can be called from GHC-land, which uses a
58 -- different IO representation.
60 -- This is all very delicate and relies crucially on the non-inlined
61 -- connectWorlds fn to create an artificial dependency of the hugs_ioaction
62 -- on the grealworld. That's needed to stop the simplifier floating
63 -- the case outside of the \ grealworld.
64 hugsprimMkIO :: (rw -> (a,rw)) -> IO a
65 hugsprimMkIO hugs_ioaction
66 = IO ( \ grealworld -> case hugs_ioaction
67 (connectWorlds grealworld) of
68 (res, hrealworld') -> (# grealworld, res #)
71 {-# NOINLINE connectWorlds #-}
72 connectWorlds :: State# RealWorld -> a -- really, -> Hugs' RealWorld
73 connectWorlds hrealworld
74 = error "connectWorlds: hugs entered the RealWorld"
78 -- StgAddr createAdjThunk ( StgStablePtr stableptr,
82 foreign import "createAdjThunk" unsafe hugsCreateAdjThunk
83 :: StablePtr (a -> b) -> Addr{-mallocville String-} -> Char -> IO Addr
84 foreign import "malloc" unsafe malloc
86 hugsprimCreateAdjThunk :: (a -> b) -> String -> Char -> IO Addr
87 hugsprimCreateAdjThunk fun typestr callconv
88 = do sp <- makeStablePtr fun
89 p <- copy_String_to_cstring typestr -- is never freed
90 a <- hugsCreateAdjThunk sp p callconv
93 copy_String_to_cstring :: String -> IO Addr
94 copy_String_to_cstring s
95 = malloc (1 + length s) >>= \ptr0 ->
96 let loop off [] = writeCharOffAddr ptr0 off (chr 0)
98 loop off (c:cs) = writeCharOffAddr ptr0 off c
102 then error "copy_String_to_cstring: malloc failed"
105 isNullAddr a = a == nullAddr
107 writeCharOffAddr :: Addr -> Int -> Char -> IO ()
108 writeCharOffAddr (A# buf#) (I# n#) (C# c#)
110 case (writeCharOffAddr# buf# n# c# s#) of
111 s2# -> (# s2#, () #) )
115 fromDouble :: Fractional a => Double -> a
116 fromDouble n = fromRational (toRational n)
118 hugsprimEqChar :: Char -> Char -> Bool
119 hugsprimEqChar c1 c2 = c1 == c2
121 hugsprimPmInt :: Num a => Int -> a -> Bool
122 hugsprimPmInt n x = fromInt n == x
124 hugsprimPmInteger :: Num a => Integer -> a -> Bool
125 hugsprimPmInteger n x = fromInteger n == x
127 hugsprimPmDouble :: Fractional a => Double -> a -> Bool
128 hugsprimPmDouble n x = fromDouble n == x
130 -- The following primitives are only needed if (n+k) patterns are enabled:
131 hugsprimPmSub :: Integral a => Int -> a -> a
132 hugsprimPmSub n x = x - fromInt n
134 hugsprimPmFromInteger :: Integral a => Integer -> a
135 hugsprimPmFromInteger = fromIntegral
137 hugsprimPmSubtract :: Integral a => a -> a -> a
138 hugsprimPmSubtract x y = x - y
140 hugsprimPmLe :: Integral a => a -> a -> Bool
141 hugsprimPmLe x y = x <= y
143 hugsprimUnpackString :: Addr -> String
144 hugsprimUnpackString a = unpackCString a
146 -- ToDo: make the message more informative.
148 hugsprimPmFail = error "Pattern Match Failure"
150 hugsprimCompAux :: Ord a => a -> a -> Ordering -> Ordering
151 hugsprimCompAux x y o = case compare x y of EQ -> o; LT -> LT; GT -> GT
153 hugsprimError :: String -> a
154 hugsprimError s = error s
156 hugsprimShowField :: Show a => String -> a -> ShowS
157 hugsprimShowField m v = showString m . showChar '=' . shows v
159 hugsprimReadField :: Read a => String -> ReadS a
160 hugsprimReadField m s0 = [ r | (t, s1) <- lex s0, t == m,
165 -- used when Hugs invokes top level function
167 hugsprimRunIO_toplevel :: IO a -> ()
168 hugsprimRunIO_toplevel m
169 = protect 5 (fst (unST composite_action realWorld))
172 = do writeIORef prelCleanupAfterRunAction Nothing
174 cleanup_handles <- readIORef prelCleanupAfterRunAction
175 case cleanup_handles of
179 realWorld = error "primRunIO: entered the RealWorld"
180 protect :: Int -> () -> ()
184 = primCatch (protect (n-1) comp)
185 (\e -> fst (unST (putStr (show e ++ "\n")) realWorld))
188 hugsprimRunIO_toplevel :: IO a -> ()
189 hugsprimRunIO_toplevel m
191 catchException (m >> hFlush stderr >> hFlush stdout)
192 (\e -> putStr ("error: " ++ show e ++ "\n"))