2 ------------------------------------------------------------------
3 -- A primop-table mangling program --
4 ------------------------------------------------------------------
14 import System ( getArgs )
15 import Maybe ( catMaybes )
18 main = getArgs >>= \args ->
19 if length args /= 1 || head args `notElem` known_args
20 then error ("usage: genprimopcode command < primops.txt > ...\n"
21 ++ " where command is one of\n"
22 ++ unlines (map (" "++) known_args)
27 Left err -> error ("parse error at " ++ (show err))
29 -> seq (sanityTop p_o_specs) (
33 -> putStr (gen_data_decl p_o_specs)
36 -> putStr (gen_switch_from_attribs
38 "primOpHasSideEffects" p_o_specs)
41 -> putStr (gen_switch_from_attribs
43 "primOpOutOfLine" p_o_specs)
46 -> putStr (gen_switch_from_attribs
48 "commutableOp" p_o_specs)
51 -> putStr (gen_switch_from_attribs
53 "primOpNeedsWrapper" p_o_specs)
56 -> putStr (gen_switch_from_attribs
58 "primOpCanFail" p_o_specs)
61 -> putStr (gen_switch_from_attribs
63 "primOpStrictness" p_o_specs)
66 -> putStr (gen_switch_from_attribs
68 "primOpUsg" p_o_specs)
70 "--primop-primop-info"
71 -> putStr (gen_primop_info p_o_specs)
74 -> putStr (gen_primop_tag p_o_specs)
77 -> putStr (gen_primop_list p_o_specs)
79 "--make-haskell-wrappers"
80 -> putStr (gen_wrappers p_o_specs)
82 "--make-haskell-source"
83 -> putStr (gen_hs_source p_o_specs)
86 -> putStr (gen_latex_doc p_o_specs)
88 _ -> error "Should not happen, known_args out of sync?"
91 known_args :: [String]
101 "--primop-primop-info",
104 "--make-haskell-wrappers",
105 "--make-haskell-source",
109 ------------------------------------------------------------------
110 -- Code generators -----------------------------------------------
111 ------------------------------------------------------------------
113 gen_hs_source :: Info -> String
114 gen_hs_source (Info defaults entries) =
115 "-----------------------------------------------------------------------------\n"
117 ++ "-- Module : GHC.Arr\n"
119 ++ "-- Maintainer : cvs-ghc@haskell.org\n"
120 ++ "-- Stability : internal\n"
121 ++ "-- Portability : non-portable (GHC extensions)\n"
123 ++ "-- GHC\'s primitive types and operations.\n"
125 ++ "-----------------------------------------------------------------------------\n"
126 ++ "module GHC.Prim (\n"
127 ++ unlines (map (("\t" ++) . hdr) entries)
129 ++ unlines (map opt defaults) ++ "-}\n"
130 ++ unlines (map ent entries) ++ "\n\n\n"
131 where opt (OptionFalse n) = n ++ " = False"
132 opt (OptionTrue n) = n ++ " = True"
133 opt (OptionString n v) = n ++ " = { " ++ v ++ "}"
135 hdr s@(Section {}) = sec s
136 hdr (PrimOpSpec { name = n }) = wrapOp n ++ ","
137 hdr (PseudoOpSpec { name = n }) = wrapOp n ++ ","
138 hdr (PrimTypeSpec { ty = TyApp n _ }) = wrapTy n ++ ","
139 hdr (PrimTypeSpec {}) = error "Illegal type spec"
141 ent (Section {}) = ""
142 ent o@(PrimOpSpec {}) = spec o
143 ent o@(PrimTypeSpec {}) = spec o
144 ent o@(PseudoOpSpec {}) = spec o
146 sec s = "\n-- * " ++ escape (title s) ++ "\n"
147 ++ (unlines $ map ("-- " ++ ) $ lines $ unlatex $ escape $ "|" ++ desc s) ++ "\n"
149 spec o = comm ++ decl
150 where decl = case o of
151 PrimOpSpec { name = n, ty = t } -> wrapOp n ++ " :: " ++ pty t
152 PseudoOpSpec { name = n, ty = t } -> wrapOp n ++ " :: " ++ pty t
153 PrimTypeSpec { ty = t } -> "data " ++ pty t
156 comm = case (desc o) of
158 d -> "\n" ++ (unlines $ map ("-- " ++ ) $ lines $ unlatex $ escape $ "|" ++ d)
160 pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
163 pbty (TyApp tc ts) = tc ++ (concat (map (' ':) (map paty ts)))
164 pbty (TyUTup ts) = "(# " ++ (concat (intersperse "," (map pty ts))) ++ " #)"
168 paty t = "(" ++ pty t ++ ")"
170 wrapOp nm | isAlpha (head nm) = nm
171 | otherwise = "(" ++ nm ++ ")"
172 wrapTy nm | isAlpha (head nm) = nm
173 | otherwise = "(" ++ nm ++ ")"
174 unlatex s = case s of
175 '\\':'t':'e':'x':'t':'t':'t':'{':cs -> markup "@" "@" cs
176 '{':'\\':'t':'t':cs -> markup "@" "@" cs
177 '{':'\\':'i':'t':cs -> markup "/" "/" cs
178 c : cs -> c : unlatex cs
180 markup s t xs = s ++ mk (dropWhile isSpace xs)
182 mk ('\n':cs) = ' ' : mk cs
183 mk ('}':cs) = t ++ unlatex cs
184 mk (c:cs) = c : mk cs
185 escape = concatMap (\c -> if c `elem` special then '\\':c:[] else c:[])
186 where special = "/'`\"@<"
188 gen_latex_doc :: Info -> String
189 gen_latex_doc (Info defaults entries)
190 = "\\primopdefaults{"
191 ++ mk_options defaults
193 ++ (concat (map mk_entry entries))
194 where mk_entry (PrimOpSpec {cons=constr,name=n,ty=t,cat=c,desc=d,opts=o}) =
196 ++ latex_encode constr ++ "}{"
197 ++ latex_encode n ++ "}{"
198 ++ latex_encode (zencode n) ++ "}{"
199 ++ latex_encode (show c) ++ "}{"
200 ++ latex_encode (mk_source_ty t) ++ "}{"
201 ++ latex_encode (mk_core_ty t) ++ "}{"
205 mk_entry (Section {title=ti,desc=d}) =
207 ++ latex_encode ti ++ "}{"
209 mk_entry (PrimTypeSpec {ty=t,desc=d,opts=o}) =
211 ++ latex_encode (mk_source_ty t) ++ "}{"
212 ++ latex_encode (mk_core_ty t) ++ "}{"
216 mk_entry (PseudoOpSpec {name=n,ty=t,desc=d,opts=o}) =
218 ++ latex_encode (zencode n) ++ "}{"
219 ++ latex_encode (mk_source_ty t) ++ "}{"
220 ++ latex_encode (mk_core_ty t) ++ "}{"
224 mk_source_ty typ = pty typ
225 where pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
227 pbty (TyApp tc ts) = tc ++ (concat (map (' ':) (map paty ts)))
228 pbty (TyUTup ts) = "(# " ++ (concat (intersperse "," (map pty ts))) ++ " #)"
231 paty t = "(" ++ pty t ++ ")"
233 mk_core_ty typ = foralls ++ (pty typ)
234 where pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
236 pbty (TyApp tc ts) = (zencode tc) ++ (concat (map (' ':) (map paty ts)))
237 pbty (TyUTup ts) = (zencode (utuplenm (length ts))) ++ (concat ((map (' ':) (map paty ts))))
239 paty (TyVar tv) = zencode tv
240 paty (TyApp tc []) = zencode tc
241 paty t = "(" ++ pty t ++ ")"
243 utuplenm n = "(#" ++ (replicate (n-1) ',') ++ "#)"
244 foralls = if tvars == [] then "" else "%forall " ++ (tbinds tvars)
247 tbinds ("o":tbs) = "(o::?) " ++ (tbinds tbs)
248 tbinds (tv:tbs) = tv ++ " " ++ (tbinds tbs)
249 tvars_of (TyF t1 t2) = tvars_of t1 `union` tvars_of t2
250 tvars_of (TyApp _ ts) = foldl union [] (map tvars_of ts)
251 tvars_of (TyUTup ts) = foldr union [] (map tvars_of ts)
252 tvars_of (TyVar tv) = [tv]
256 ++ mk_has_side_effects o ++ "}{"
257 ++ mk_out_of_line o ++ "}{"
258 ++ mk_commutable o ++ "}{"
259 ++ mk_needs_wrapper o ++ "}{"
260 ++ mk_can_fail o ++ "}{"
261 ++ latex_encode (mk_strictness o) ++ "}{"
262 ++ latex_encode (mk_usage o)
265 mk_has_side_effects o = mk_bool_opt o "has_side_effects" "Has side effects." "Has no side effects."
266 mk_out_of_line o = mk_bool_opt o "out_of_line" "Implemented out of line." "Implemented in line."
267 mk_commutable o = mk_bool_opt o "commutable" "Commutable." "Not commutable."
268 mk_needs_wrapper o = mk_bool_opt o "needs_wrapper" "Needs wrapper." "Needs no wrapper."
269 mk_can_fail o = mk_bool_opt o "can_fail" "Can fail." "Cannot fail."
271 mk_bool_opt o opt_name if_true if_false =
272 case lookup_attrib opt_name o of
273 Just (OptionTrue _) -> if_true
274 Just (OptionFalse _) -> if_false
275 Just (OptionString _ _) -> error "String value for boolean option"
279 case lookup_attrib "strictness" o of
280 Just (OptionString _ s) -> s -- for now
281 Just _ -> error "Boolean value for strictness"
285 case lookup_attrib "usage" o of
286 Just (OptionString _ s) -> s -- for now
287 Just _ -> error "Boolean value for usage"
291 case maybe_tuple xs of
292 Just n -> n -- Tuples go to Z2T etc
293 Nothing -> concat (map encode_ch xs)
295 maybe_tuple "(# #)" = Just("Z1H")
296 maybe_tuple ('(' : '#' : cs) = case count_commas (0::Int) cs of
297 (n, '#' : ')' : _) -> Just ('Z' : shows (n+1) "H")
299 maybe_tuple "()" = Just("Z0T")
300 maybe_tuple ('(' : cs) = case count_commas (0::Int) cs of
301 (n, ')' : _) -> Just ('Z' : shows (n+1) "T")
303 maybe_tuple _ = Nothing
305 count_commas :: Int -> String -> (Int, String)
306 count_commas n (',' : cs) = count_commas (n+1) cs
307 count_commas n cs = (n,cs)
309 unencodedChar :: Char -> Bool -- True for chars that don't need encoding
310 unencodedChar 'Z' = False
311 unencodedChar 'z' = False
312 unencodedChar c = isAlphaNum c
314 encode_ch :: Char -> String
315 encode_ch c | unencodedChar c = [c] -- Common case first
318 encode_ch '(' = "ZL" -- Needed for things like (,), and (->)
319 encode_ch ')' = "ZR" -- For symmetry with (
339 encode_ch '\'' = "zq"
340 encode_ch '\\' = "zr"
345 encode_ch c = 'z' : shows (ord c) "U"
348 latex_encode (c:cs) | c `elem` "#$%&_^{}" = "\\" ++ c:(latex_encode cs)
349 latex_encode ('~':cs) = "\\verb!~!" ++ (latex_encode cs)
350 latex_encode ('\\':cs) = "$\\backslash$" ++ (latex_encode cs)
351 latex_encode (c:cs) = c:(latex_encode cs)
353 gen_wrappers :: Info -> String
354 gen_wrappers (Info _ entries)
355 = "{-# OPTIONS -fno-implicit-prelude #-}\n"
356 -- Dependencies on Prelude must be explicit in libraries/base, but we
357 -- don't need the Prelude here so we add -fno-implicit-prelude.
358 ++ "module GHC.PrimopWrappers where\n"
359 ++ "import qualified GHC.Prim\n"
360 ++ unlines (map f (filter (not.dodgy) (filter is_primop entries)))
362 f spec = let args = map (\n -> "a" ++ show n) [1 .. arity (ty spec)]
363 src_name = wrap (name spec)
364 in "{-# NOINLINE " ++ src_name ++ " #-}\n" ++
365 src_name ++ " " ++ unwords args
366 ++ " = (GHC.Prim." ++ name spec ++ ") " ++ unwords args
367 wrap nm | isLower (head nm) = nm
368 | otherwise = "(" ++ nm ++ ")"
372 [-- C code generator can't handle these
375 -- not interested in parallel support
376 "par#", "parGlobal#", "parLocal#", "parAt#",
377 "parAtAbs#", "parAtRel#", "parAtForNow#"
380 gen_primop_list :: Info -> String
381 gen_primop_list (Info _ entries)
383 [ " [" ++ cons first ]
385 map (\p -> " , " ++ cons p) rest
388 ) where (first:rest) = filter is_primop entries
390 gen_primop_tag :: Info -> String
391 gen_primop_tag (Info _ entries)
392 = unlines (max_def : zipWith f primop_entries [1 :: Int ..])
394 primop_entries = filter is_primop entries
395 f i n = "tagOf_PrimOp " ++ cons i
396 ++ " = _ILIT(" ++ show n ++ ") :: FastInt"
397 max_def = "maxPrimOpTag = " ++ show (length primop_entries) ++ " :: Int"
399 gen_data_decl :: Info -> String
400 gen_data_decl (Info _ entries)
401 = let conss = map cons (filter is_primop entries)
402 in "data PrimOp\n = " ++ head conss ++ "\n"
403 ++ unlines (map (" | "++) (tail conss))
405 gen_switch_from_attribs :: String -> String -> Info -> String
406 gen_switch_from_attribs attrib_name fn_name (Info defaults entries)
407 = let defv = lookup_attrib attrib_name defaults
408 alternatives = catMaybes (map mkAlt (filter is_primop entries))
410 getAltRhs (OptionFalse _) = "False"
411 getAltRhs (OptionTrue _) = "True"
412 getAltRhs (OptionString _ s) = s
415 = case lookup_attrib attrib_name (opts po) of
417 Just xx -> Just (fn_name ++ " " ++ cons po ++ " = " ++ getAltRhs xx)
421 Nothing -> error ("gen_switch_from: " ++ attrib_name)
423 -> unlines alternatives
424 ++ fn_name ++ " other = " ++ getAltRhs xx ++ "\n"
426 ------------------------------------------------------------------
427 -- Create PrimOpInfo text from PrimOpSpecs -----------------------
428 ------------------------------------------------------------------
430 gen_primop_info :: Info -> String
431 gen_primop_info (Info _ entries)
432 = unlines (map mkPOItext (filter is_primop entries))
434 mkPOItext :: Entry -> String
435 mkPOItext i = mkPOI_LHS_text i ++ mkPOI_RHS_text i
437 mkPOI_LHS_text :: Entry -> String
439 = "primOpInfo " ++ cons i ++ " = "
441 mkPOI_RHS_text :: Entry -> String
447 -> "mkCompare " ++ sl_name i ++ ppType t1
448 _ -> error "Type error in comparison op"
452 -> "mkMonadic " ++ sl_name i ++ ppType t1
453 _ -> error "Type error in monadic op"
457 -> "mkDyadic " ++ sl_name i ++ ppType t1
458 _ -> error "Type error in dyadic op"
460 -> let (argTys, resTy) = flatTys (ty i)
461 tvs = nub (tvsIn (ty i))
463 "mkGenPrimOp " ++ sl_name i ++ " "
464 ++ listify (map ppTyVar tvs) ++ " "
465 ++ listify (map ppType argTys) ++ " "
466 ++ "(" ++ ppType resTy ++ ")"
468 sl_name :: Entry -> String
469 sl_name i = "FSLIT(\"" ++ name i ++ "\") "
471 ppTyVar :: String -> String
472 ppTyVar "a" = "alphaTyVar"
473 ppTyVar "b" = "betaTyVar"
474 ppTyVar "c" = "gammaTyVar"
475 ppTyVar "s" = "deltaTyVar"
476 ppTyVar "o" = "openAlphaTyVar"
477 ppTyVar _ = error "Unknown type var"
479 ppType :: Ty -> String
480 ppType (TyApp "Bool" []) = "boolTy"
482 ppType (TyApp "Int#" []) = "intPrimTy"
483 ppType (TyApp "Int32#" []) = "int32PrimTy"
484 ppType (TyApp "Int64#" []) = "int64PrimTy"
485 ppType (TyApp "Char#" []) = "charPrimTy"
486 ppType (TyApp "Word#" []) = "wordPrimTy"
487 ppType (TyApp "Word32#" []) = "word32PrimTy"
488 ppType (TyApp "Word64#" []) = "word64PrimTy"
489 ppType (TyApp "Addr#" []) = "addrPrimTy"
490 ppType (TyApp "Float#" []) = "floatPrimTy"
491 ppType (TyApp "Double#" []) = "doublePrimTy"
492 ppType (TyApp "ByteArr#" []) = "byteArrayPrimTy"
493 ppType (TyApp "RealWorld" []) = "realWorldTy"
494 ppType (TyApp "ThreadId#" []) = "threadIdPrimTy"
495 ppType (TyApp "ForeignObj#" []) = "foreignObjPrimTy"
496 ppType (TyApp "BCO#" []) = "bcoPrimTy"
497 ppType (TyApp "()" []) = "unitTy" -- unitTy is TysWiredIn's name for ()
499 ppType (TyVar "a") = "alphaTy"
500 ppType (TyVar "b") = "betaTy"
501 ppType (TyVar "c") = "gammaTy"
502 ppType (TyVar "s") = "deltaTy"
503 ppType (TyVar "o") = "openAlphaTy"
504 ppType (TyApp "State#" [x]) = "mkStatePrimTy " ++ ppType x
505 ppType (TyApp "MutVar#" [x,y]) = "mkMutVarPrimTy " ++ ppType x
507 ppType (TyApp "MutArr#" [x,y]) = "mkMutableArrayPrimTy " ++ ppType x
510 ppType (TyApp "MutByteArr#" [x]) = "mkMutableByteArrayPrimTy "
513 ppType (TyApp "Array#" [x]) = "mkArrayPrimTy " ++ ppType x
516 ppType (TyApp "Weak#" [x]) = "mkWeakPrimTy " ++ ppType x
517 ppType (TyApp "StablePtr#" [x]) = "mkStablePtrPrimTy " ++ ppType x
518 ppType (TyApp "StableName#" [x]) = "mkStableNamePrimTy " ++ ppType x
520 ppType (TyApp "MVar#" [x,y]) = "mkMVarPrimTy " ++ ppType x
522 ppType (TyApp "TVar#" [x,y]) = "mkTVarPrimTy " ++ ppType x
524 ppType (TyUTup ts) = "(mkTupleTy Unboxed " ++ show (length ts)
526 ++ listify (map ppType ts) ++ ")"
528 ppType (TyF s d) = "(mkFunTy (" ++ ppType s ++ ") (" ++ ppType d ++ "))"
531 = error ("ppType: can't handle: " ++ show other ++ "\n")
533 listify :: [String] -> String
534 listify ss = "[" ++ concat (intersperse ", " ss) ++ "]"
536 flatTys :: Ty -> ([Ty],Ty)
537 flatTys (TyF t1 t2) = case flatTys t2 of (ts,t) -> (t1:ts,t)
538 flatTys other = ([],other)
540 tvsIn :: Ty -> [TyVar]
541 tvsIn (TyF t1 t2) = tvsIn t1 ++ tvsIn t2
542 tvsIn (TyApp _ tys) = concatMap tvsIn tys
543 tvsIn (TyVar tv) = [tv]
544 tvsIn (TyUTup tys) = concatMap tvsIn tys
547 arity = length . fst . flatTys