Move the "This is a generated file" to the top of GHC.Prim
[ghc-hetmet.git] / utils / genprimopcode / Main.hs
index ffd10ff..760ebf4 100644 (file)
@@ -110,7 +110,13 @@ known_args
 
 gen_hs_source :: Info -> String
 gen_hs_source (Info defaults entries) =
-          "-----------------------------------------------------------------------------\n"
+       "{-\n"
+    ++ "This is a generated file (generated by genprimopcode).\n"
+    ++ "It is not code to actually be used. Its only purpose is to be\n"
+    ++ "consumed by haddock.\n"
+    ++ "-}\n"
+    ++ "\n"
+       ++ "-----------------------------------------------------------------------------\n"
        ++ "-- |\n"
        ++ "-- Module      :  GHC.Prim\n"
        ++ "-- \n"
@@ -123,9 +129,14 @@ gen_hs_source (Info defaults entries) =
        ++ "-----------------------------------------------------------------------------\n"
        ++ "module GHC.Prim (\n"
        ++ unlines (map (("\t" ++) . hdr) entries)
-       ++ ") where\n\n{-\n"
-       ++ unlines (map opt defaults) ++ "-}\n"
-       ++ unlines (map ent entries) ++ "\n\n\n"
+       ++ ") where\n"
+    ++ "\n"
+    ++ "import GHC.Bool\n"
+    ++ "\n"
+    ++ "{-\n"
+       ++ unlines (map opt defaults)
+    ++ "-}\n"
+       ++ unlines (concatMap ent entries) ++ "\n\n\n"
      where opt (OptionFalse n)   = n ++ " = False"
            opt (OptionTrue n)    = n ++ " = True"
           opt (OptionString n v) = n ++ " = { " ++ v ++ "}"
@@ -136,7 +147,7 @@ gen_hs_source (Info defaults entries) =
           hdr (PrimTypeSpec { ty = TyApp n _ }) = wrapTy n ++ ","
           hdr (PrimTypeSpec {})                 = error "Illegal type spec"
 
-          ent   (Section {})      = ""
+          ent   (Section {})      = []
           ent o@(PrimOpSpec {})   = spec o
           ent o@(PrimTypeSpec {}) = spec o
           ent o@(PseudoOpSpec {}) = spec o
@@ -144,27 +155,22 @@ gen_hs_source (Info defaults entries) =
           sec s = "\n-- * " ++ escape (title s) ++ "\n"
                        ++ (unlines $ map ("-- " ++ ) $ lines $ unlatex $ escape $ "|" ++ desc s) ++ "\n"
 
-          spec o = comm ++ decl
-            where decl = case o of
-                       PrimOpSpec { name = n, ty = t }   -> wrapOp n ++ " :: " ++ pty t
-                       PseudoOpSpec { name = n, ty = t } -> wrapOp n ++ " :: " ++ pty t
-                       PrimTypeSpec { ty = t }   -> "data " ++ pty t
-                       Section { } -> ""
+          spec o = comm : decls
+            where decls = case o of
+                       PrimOpSpec { name = n, ty = t }   ->
+                            [ wrapOp n ++ " :: " ++ pprTy t,
+                              wrapOp n ++ " = let x = x in x" ]
+                       PseudoOpSpec { name = n, ty = t } ->
+                            [ wrapOp n ++ " :: " ++ pprTy t,
+                              wrapOp n ++ " = let x = x in x" ]
+                       PrimTypeSpec { ty = t }   ->
+                            [ "data " ++ pprTy t ]
+                       Section { } -> []
 
                   comm = case (desc o) of
                        [] -> ""
                        d -> "\n" ++ (unlines $ map ("-- " ++ ) $ lines $ unlatex $ escape $ "|" ++ d)
 
-                  pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
-                  pty t           = pbty t
-
-                  pbty (TyApp tc ts) = tc ++ (concat (map (' ':) (map paty ts)))
-                  pbty (TyUTup ts)   = "(# " ++ (concat (intersperse "," (map pty ts))) ++ " #)"
-                  pbty t             = paty t
-
-                  paty (TyVar tv)      = tv
-                  paty t               = "(" ++ pty t ++ ")"
-
           wrapOp nm | isAlpha (head nm) = nm
                     | otherwise         = "(" ++ nm ++ ")"
           wrapTy nm | isAlpha (head nm) = nm
@@ -183,6 +189,19 @@ gen_hs_source (Info defaults entries) =
           escape = concatMap (\c -> if c `elem` special then '\\':c:[] else c:[])
                where special = "/'`\"@<"
 
+pprTy = pty
+    where
+          pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
+          pty t      = pbty t
+          pbty (TyApp tc ts) = tc ++ concat (map (' ' :) (map paty ts))
+          pbty (TyUTup ts)   = "(# "
+                            ++ concat (intersperse "," (map pty ts))
+                            ++ " #)"
+          pbty t             = paty t
+
+          paty (TyVar tv)    = tv
+          paty t             = "(" ++ pty t ++ ")"
+--
 -- Generates the type environment that the stand-alone External Core tools use.
 gen_ext_core_source :: [Entry] -> String
 gen_ext_core_source entries =
@@ -279,10 +298,6 @@ gen_ext_core_source entries =
 
         -- more hacks. might be better to do this on the ext-core side,
         -- as per earlier comment
-        qualify _ tc | tc == "ByteArr#" = qualify True "ByteArray#"
-        qualify _ tc | tc == "MutArr#" = qualify True "MutableArray#"
-        qualify _ tc | tc == "MutByteArr#" = 
-                     qualify True "MutableByteArray#"
         qualify _ tc | tc == "Bool" = "Just boolMname" ++ ", " 
                                                 ++ ze True tc
         qualify _ tc | tc == "()"  = "Just baseMname" ++ ", "
@@ -465,18 +480,27 @@ gen_latex_doc (Info defaults entries)
 
 gen_wrappers :: Info -> String
 gen_wrappers (Info _ entries)
-   = "{-# LANGUAGE NoImplicitPrelude #-}\n" 
+   = "{-# LANGUAGE NoImplicitPrelude, UnboxedTuples #-}\n"
        -- Dependencies on Prelude must be explicit in libraries/base, but we
        -- don't need the Prelude here so we add NoImplicitPrelude.
      ++ "module GHC.PrimopWrappers where\n" 
      ++ "import qualified GHC.Prim\n" 
-     ++ unlines (map f (filter (not.dodgy) (filter is_primop entries)))
+     ++ "import GHC.Bool (Bool)\n"
+     ++ "import GHC.Unit ()\n"
+     ++ "import GHC.Prim (" ++ types ++ ")\n"
+     ++ unlines (concatMap f specs)
      where
+        specs = filter (not.dodgy) (filter is_primop entries)
+        tycons = foldr union [] $ map (tyconsIn . ty) specs
+        tycons' = filter (`notElem` ["()", "Bool"]) tycons
+        types = concat $ intersperse ", " tycons'
         f spec = let args = map (\n -> "a" ++ show n) [1 .. arity (ty spec)]
                      src_name = wrap (name spec)
-                 in "{-# NOINLINE " ++ src_name ++ " #-}\n" ++ 
-                    src_name ++ " " ++ unwords args 
-                     ++ " = (GHC.Prim." ++ name spec ++ ") " ++ unwords args
+                     lhs = src_name ++ " " ++ unwords args
+                     rhs = "(GHC.Prim." ++ name spec ++ ") " ++ unwords args
+                 in ["{-# NOINLINE " ++ src_name ++ " #-}",
+                     src_name ++ " :: " ++ pprTy (ty spec),
+                     lhs ++ " = " ++ rhs]
         wrap nm | isLower (head nm) = nm
                 | otherwise = "(" ++ nm ++ ")"
 
@@ -604,7 +628,7 @@ ppType (TyApp "Word64#"     []) = "word64PrimTy"
 ppType (TyApp "Addr#"       []) = "addrPrimTy"
 ppType (TyApp "Float#"      []) = "floatPrimTy"
 ppType (TyApp "Double#"     []) = "doublePrimTy"
-ppType (TyApp "ByteArr#"    []) = "byteArrayPrimTy"
+ppType (TyApp "ByteArray#"  []) = "byteArrayPrimTy"
 ppType (TyApp "RealWorld"   []) = "realWorldTy"
 ppType (TyApp "ThreadId#"   []) = "threadIdPrimTy"
 ppType (TyApp "ForeignObj#" []) = "foreignObjPrimTy"
@@ -619,10 +643,10 @@ ppType (TyVar "o")               = "openAlphaTy"
 ppType (TyApp "State#" [x])      = "mkStatePrimTy " ++ ppType x
 ppType (TyApp "MutVar#" [x,y])   = "mkMutVarPrimTy " ++ ppType x 
                                    ++ " " ++ ppType y
-ppType (TyApp "MutArr#" [x,y])   = "mkMutableArrayPrimTy " ++ ppType x 
-                                   ++ " " ++ ppType y
+ppType (TyApp "MutableArray#" [x,y]) = "mkMutableArrayPrimTy " ++ ppType x
+                                    ++ " " ++ ppType y
 
-ppType (TyApp "MutByteArr#" [x]) = "mkMutableByteArrayPrimTy " 
+ppType (TyApp "MutableByteArray#" [x]) = "mkMutableByteArrayPrimTy " 
                                    ++ ppType x
 
 ppType (TyApp "Array#" [x])      = "mkArrayPrimTy " ++ ppType x
@@ -658,6 +682,12 @@ tvsIn (TyApp _ tys)  = concatMap tvsIn tys
 tvsIn (TyVar tv)     = [tv]
 tvsIn (TyUTup tys)   = concatMap tvsIn tys
 
+tyconsIn :: Ty -> [TyCon]
+tyconsIn (TyF t1 t2)    = tyconsIn t1 `union` tyconsIn t2
+tyconsIn (TyApp tc tys) = foldr union [tc] $ map tyconsIn tys
+tyconsIn (TyVar _)      = []
+tyconsIn (TyUTup tys)   = foldr union [] $ map tyconsIn tys
+
 arity :: Ty -> Int
 arity = length . fst . flatTys