Get building GHC itself with Cabal more-or-less working
[ghc-hetmet.git] / compiler / utils / FastString.lhs
index 4417c08..7d43dc1 100644 (file)
@@ -2,16 +2,24 @@
 % (c) The University of Glasgow, 1997-2006
 %
 \begin{code}
+{-# OPTIONS -fno-warn-unused-imports #-}
+-- XXX GHC 6.9 seems to be confused by unpackCString# being used only in
+--     a RULE
+
+{-# OPTIONS_GHC -O -funbox-strict-fields #-}
+-- We always optimise this, otherwise performance of a non-optimised
+-- compiler is severely affected
+
 {-
 FastString:     A compact, hash-consed, representation of character strings.
                 Comparison is O(1), and you can get a Unique from them.
-                Generated by the FSLIT macro
+                Generated by fsLit
                 Turn into SDoc with Outputable.ftext
 
 LitString:      Just a wrapper for the Addr# of a C string (Ptr CChar).
                 Practically no operations
                 Outputing them is fast
-                Generated by the SLIT macro
+                Generated by sLit
                 Turn into SDoc with Outputable.ptext
 
 Use LitString unless you want the facilities of FastString
@@ -62,13 +70,15 @@ module FastString
         LitString,
 #if defined(__GLASGOW_HASKELL__)
         mkLitString#,
-#else
-        mkLitString,
 #endif
+        mkLitString,
         unpackLitString,
         strLength,
 
-        ptrStrLength
+        ptrStrLength,
+
+        sLit,
+        fsLit,
        ) where
 
 #include "HsVersions.h"
@@ -85,12 +95,13 @@ import System.IO
 import System.IO.Unsafe ( unsafePerformIO )
 import Data.IORef       ( IORef, newIORef, readIORef, writeIORef )
 import Data.Maybe       ( isJust )
-#if !defined(__GLASGOW_HASKELL__)
 import Data.Char        ( ord )
-#endif
 
 import GHC.IOBase       ( IO(..) )
 import GHC.Ptr          ( Ptr(..) )
+#if defined(__GLASGOW_HASKELL__)
+import GHC.Base         ( unpackCString# )
+#endif
 
 #define hASH_TBL_SIZE          4091
 #define hASH_TBL_SIZE_UNBOXED  4091#
@@ -500,7 +511,7 @@ type LitString = Ptr Word8
 #if defined(__GLASGOW_HASKELL__)
 mkLitString# :: Addr# -> LitString
 mkLitString# a# = Ptr a#
-#else
+#endif
 --can/should we use FastTypes here?
 --Is this likely to be memory-preserving if only used on constant strings?
 --should we inline it? If lucky, that would make a CAF that wouldn't
@@ -519,10 +530,12 @@ mkLitString s =
      loop n (c:cs) = do
         pokeByteOff p n (fromIntegral (ord c) :: Word8)
         loop (1+n) cs
+     -- XXX GHC isn't smart enough to know that we have already covered
+     -- this case.
+     loop _ [] = panic "mkLitString"
    loop 0 s
    return p
  )
-#endif
 
 unpackLitString :: LitString -> String
 unpackLitString p_ = case pUnbox p_ of
@@ -568,7 +581,16 @@ pokeCAString ptr str =
   in
   go str 0
 
-#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 602
-peekCAStringLen = peekCStringLen
-#endif
+{-# NOINLINE sLit #-}
+sLit :: String -> LitString
+sLit x  = mkLitString x
+
+{-# NOINLINE fsLit #-}
+fsLit :: String -> FastString
+fsLit x = mkFastString x
+
+{-# RULES "slit"
+    forall x . sLit  (unpackCString# x) = mkLitString#  x #-}
+{-# RULES "fslit"
+    forall x . fsLit (unpackCString# x) = mkFastString# x #-}
 \end{code}