Comparison primops return Int# (Fixes #6135)
[ghc.git] / utils / genprimopcode / Main.hs
1 {-# OPTIONS -cpp #-}
2 ------------------------------------------------------------------
3 -- A primop-table mangling program --
4 ------------------------------------------------------------------
5
6 module Main where
7
8 import Parser
9 import Syntax
10
11 import Data.Char
12 import Data.List
13 import Data.Maybe ( catMaybes )
14 import System.Environment ( getArgs )
15
16 main :: IO ()
17 main = getArgs >>= \args ->
18 if length args /= 1 || head args `notElem` known_args
19 then error ("usage: genprimopcode command < primops.txt > ...\n"
20 ++ " where command is one of\n"
21 ++ unlines (map (" "++) known_args)
22 )
23 else
24 do s <- getContents
25 case parse s of
26 Left err -> error ("parse error at " ++ (show err))
27 Right p_o_specs@(Info _ entries)
28 -> seq (sanityTop p_o_specs) (
29 case head args of
30
31 "--data-decl"
32 -> putStr (gen_data_decl p_o_specs)
33
34 "--has-side-effects"
35 -> putStr (gen_switch_from_attribs
36 "has_side_effects"
37 "primOpHasSideEffects" p_o_specs)
38
39 "--out-of-line"
40 -> putStr (gen_switch_from_attribs
41 "out_of_line"
42 "primOpOutOfLine" p_o_specs)
43
44 "--commutable"
45 -> putStr (gen_switch_from_attribs
46 "commutable"
47 "commutableOp" p_o_specs)
48
49 "--code-size"
50 -> putStr (gen_switch_from_attribs
51 "code_size"
52 "primOpCodeSize" p_o_specs)
53
54 "--can-fail"
55 -> putStr (gen_switch_from_attribs
56 "can_fail"
57 "primOpCanFail" p_o_specs)
58
59 "--strictness"
60 -> putStr (gen_switch_from_attribs
61 "strictness"
62 "primOpStrictness" p_o_specs)
63
64 "--fixity"
65 -> putStr (gen_switch_from_attribs
66 "fixity"
67 "primOpFixity" p_o_specs)
68
69 "--primop-primop-info"
70 -> putStr (gen_primop_info p_o_specs)
71
72 "--primop-tag"
73 -> putStr (gen_primop_tag p_o_specs)
74
75 "--primop-list"
76 -> putStr (gen_primop_list p_o_specs)
77
78 "--make-haskell-wrappers"
79 -> putStr (gen_wrappers p_o_specs)
80
81 "--make-haskell-source"
82 -> putStr (gen_hs_source p_o_specs)
83
84 "--make-ext-core-source"
85 -> putStr (gen_ext_core_source entries)
86
87 "--make-latex-doc"
88 -> putStr (gen_latex_doc p_o_specs)
89
90 _ -> error "Should not happen, known_args out of sync?"
91 )
92
93 known_args :: [String]
94 known_args
95 = [ "--data-decl",
96 "--has-side-effects",
97 "--out-of-line",
98 "--commutable",
99 "--code-size",
100 "--can-fail",
101 "--strictness",
102 "--fixity",
103 "--primop-primop-info",
104 "--primop-tag",
105 "--primop-list",
106 "--make-haskell-wrappers",
107 "--make-haskell-source",
108 "--make-ext-core-source",
109 "--make-latex-doc"
110 ]
111
112 ------------------------------------------------------------------
113 -- Code generators -----------------------------------------------
114 ------------------------------------------------------------------
115
116 gen_hs_source :: Info -> String
117 gen_hs_source (Info defaults entries) =
118 "{-\n"
119 ++ "This is a generated file (generated by genprimopcode).\n"
120 ++ "It is not code to actually be used. Its only purpose is to be\n"
121 ++ "consumed by haddock.\n"
122 ++ "-}\n"
123 ++ "\n"
124 ++ "-----------------------------------------------------------------------------\n"
125 ++ "-- |\n"
126 ++ "-- Module : GHC.Prim\n"
127 ++ "-- \n"
128 ++ "-- Maintainer : cvs-ghc@haskell.org\n"
129 ++ "-- Stability : internal\n"
130 ++ "-- Portability : non-portable (GHC extensions)\n"
131 ++ "--\n"
132 ++ "-- GHC\'s primitive types and operations.\n"
133 ++ "-- Use GHC.Exts from the base package instead of importing this\n"
134 ++ "-- module directly.\n"
135 ++ "--\n"
136 ++ "-----------------------------------------------------------------------------\n"
137 ++ "module GHC.Prim (\n"
138 ++ unlines (map (("\t" ++) . hdr) entries)
139 ++ ") where\n"
140 ++ "\n"
141 ++ "{-\n"
142 ++ unlines (map opt defaults)
143 ++ "-}\n"
144 ++ unlines (concatMap ent entries) ++ "\n\n\n"
145 where opt (OptionFalse n) = n ++ " = False"
146 opt (OptionTrue n) = n ++ " = True"
147 opt (OptionString n v) = n ++ " = { " ++ v ++ "}"
148 opt (OptionInteger n v) = n ++ " = " ++ show v
149 opt (OptionFixity mf) = "fixity" ++ " = " ++ show mf
150
151 hdr s@(Section {}) = sec s
152 hdr (PrimOpSpec { name = n }) = wrapOp n ++ ","
153 hdr (PseudoOpSpec { name = n }) = wrapOp n ++ ","
154 hdr (PrimTypeSpec { ty = TyApp n _ }) = wrapTy n ++ ","
155 hdr (PrimTypeSpec {}) = error "Illegal type spec"
156
157 ent (Section {}) = []
158 ent o@(PrimOpSpec {}) = spec o
159 ent o@(PrimTypeSpec {}) = spec o
160 ent o@(PseudoOpSpec {}) = spec o
161
162 sec s = "\n-- * " ++ escape (title s) ++ "\n"
163 ++ (unlines $ map ("-- " ++ ) $ lines $ unlatex $ escape $ "|" ++ desc s) ++ "\n"
164
165 spec o = comm : decls
166 where decls = case o of
167 PrimOpSpec { name = n, ty = t, opts = options } ->
168 [ pprFixity fixity n | OptionFixity (Just fixity) <- options ]
169 ++
170 [ wrapOp n ++ " :: " ++ pprTy t,
171 wrapOp n ++ " = let x = x in x" ]
172 PseudoOpSpec { name = n, ty = t } ->
173 [ wrapOp n ++ " :: " ++ pprTy t,
174 wrapOp n ++ " = let x = x in x" ]
175 PrimTypeSpec { ty = t } ->
176 [ "data " ++ pprTy t ]
177 Section { } -> []
178
179 comm = case (desc o) of
180 [] -> ""
181 d -> "\n" ++ (unlines $ map ("-- " ++ ) $ lines $ unlatex $ escape $ "|" ++ d)
182
183 wrapOp nm | isAlpha (head nm) = nm
184 | otherwise = "(" ++ nm ++ ")"
185 wrapTy nm | isAlpha (head nm) = nm
186 | otherwise = "(" ++ nm ++ ")"
187 unlatex s = case s of
188 '\\':'t':'e':'x':'t':'t':'t':'{':cs -> markup "@" "@" cs
189 '{':'\\':'t':'t':cs -> markup "@" "@" cs
190 '{':'\\':'i':'t':cs -> markup "/" "/" cs
191 c : cs -> c : unlatex cs
192 [] -> []
193 markup s t xs = s ++ mk (dropWhile isSpace xs)
194 where mk "" = t
195 mk ('\n':cs) = ' ' : mk cs
196 mk ('}':cs) = t ++ unlatex cs
197 mk (c:cs) = c : mk cs
198 escape = concatMap (\c -> if c `elem` special then '\\':c:[] else c:[])
199 where special = "/'`\"@<"
200
201 pprFixity (Fixity i d) n = pprFixityDir d ++ " " ++ show i ++ " " ++ n
202
203 pprTy :: Ty -> String
204 pprTy = pty
205 where
206 pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
207 pty t = pbty t
208 pbty (TyApp tc ts) = tc ++ concat (map (' ' :) (map paty ts))
209 pbty (TyUTup ts) = "(# "
210 ++ concat (intersperse "," (map pty ts))
211 ++ " #)"
212 pbty t = paty t
213
214 paty (TyVar tv) = tv
215 paty t = "(" ++ pty t ++ ")"
216 --
217 -- Generates the type environment that the stand-alone External Core tools use.
218 gen_ext_core_source :: [Entry] -> String
219 gen_ext_core_source entries =
220 "-----------------------------------------------------------------------\n"
221 ++ "-- This module is automatically generated by the GHC utility\n"
222 ++ "-- \"genprimopcode\". Do not edit!\n"
223 ++ "-----------------------------------------------------------------------\n"
224 ++ "module Language.Core.PrimEnv(primTcs, primVals, intLitTypes, ratLitTypes,"
225 ++ "\n charLitTypes, stringLitTypes) where\nimport Language.Core.Core"
226 ++ "\nimport Language.Core.Encoding\n\n"
227 ++ "primTcs :: [(Tcon, Kind)]\n"
228 ++ "primTcs = [\n"
229 ++ printList tcEnt entries
230 ++ " ]\n"
231 ++ "primVals :: [(Var, Ty)]\n"
232 ++ "primVals = [\n"
233 ++ printList valEnt entries
234 ++ "]\n"
235 ++ "intLitTypes :: [Ty]\n"
236 ++ "intLitTypes = [\n"
237 ++ printList tyEnt (intLitTys entries)
238 ++ "]\n"
239 ++ "ratLitTypes :: [Ty]\n"
240 ++ "ratLitTypes = [\n"
241 ++ printList tyEnt (ratLitTys entries)
242 ++ "]\n"
243 ++ "charLitTypes :: [Ty]\n"
244 ++ "charLitTypes = [\n"
245 ++ printList tyEnt (charLitTys entries)
246 ++ "]\n"
247 ++ "stringLitTypes :: [Ty]\n"
248 ++ "stringLitTypes = [\n"
249 ++ printList tyEnt (stringLitTys entries)
250 ++ "]\n\n"
251
252 where printList f = concat . intersperse ",\n" . filter (not . null) . map f
253 tcEnt (PrimTypeSpec {ty=t}) =
254 case t of
255 TyApp tc args -> parens tc (tcKind tc args)
256 _ -> error ("tcEnt: type in PrimTypeSpec is not a type"
257 ++ " constructor: " ++ show t)
258 tcEnt _ = ""
259 -- hack alert!
260 -- The primops.txt.pp format doesn't have enough information in it to
261 -- print out some of the information that ext-core needs (like kinds,
262 -- and later on in this code, module names) so we special-case. An
263 -- alternative would be to refer to things indirectly and hard-wire
264 -- certain things (e.g., the kind of the Any constructor, here) into
265 -- ext-core's Prims module again.
266 tcKind "Any" _ = "Klifted"
267 tcKind tc [] | last tc == '#' = "Kunlifted"
268 tcKind _ [] | otherwise = "Klifted"
269 -- assumes that all type arguments are lifted (are they?)
270 tcKind tc (_v:as) = "(Karrow Klifted " ++ tcKind tc as
271 ++ ")"
272 valEnt (PseudoOpSpec {name=n, ty=t}) = valEntry n t
273 valEnt (PrimOpSpec {name=n, ty=t}) = valEntry n t
274 valEnt _ = ""
275 valEntry name' ty' = parens name' (mkForallTy (freeTvars ty') (pty ty'))
276 where pty (TyF t1 t2) = mkFunTy (pty t1) (pty t2)
277 pty (TyApp tc ts) = mkTconApp (mkTcon tc) (map pty ts)
278 pty (TyUTup ts) = mkUtupleTy (map pty ts)
279 pty (TyVar tv) = paren $ "Tvar \"" ++ tv ++ "\""
280
281 mkFunTy s1 s2 = "Tapp " ++ (paren ("Tapp (Tcon tcArrow)"
282 ++ " " ++ paren s1))
283 ++ " " ++ paren s2
284 mkTconApp tc args = foldl tapp tc args
285 mkTcon tc = paren $ "Tcon " ++ paren (qualify True tc)
286 mkUtupleTy args = foldl tapp (tcUTuple (length args)) args
287 mkForallTy [] t = t
288 mkForallTy vs t = foldr
289 (\ v s -> "Tforall " ++
290 (paren (quote v ++ ", " ++ vKind v)) ++ " "
291 ++ paren s) t vs
292
293 -- hack alert!
294 vKind "o" = "Kopen"
295 vKind _ = "Klifted"
296
297 freeTvars (TyF t1 t2) = freeTvars t1 `union` freeTvars t2
298 freeTvars (TyApp _ tys) = freeTvarss tys
299 freeTvars (TyVar v) = [v]
300 freeTvars (TyUTup tys) = freeTvarss tys
301 freeTvarss = nub . concatMap freeTvars
302
303 tapp s nextArg = paren $ "Tapp " ++ s ++ " " ++ paren nextArg
304 tcUTuple n = paren $ "Tcon " ++ paren (qualify False $ "Z"
305 ++ show n ++ "H")
306
307 tyEnt (PrimTypeSpec {ty=(TyApp tc _args)}) = " " ++ paren ("Tcon " ++
308 (paren (qualify True tc)))
309 tyEnt _ = ""
310
311 -- more hacks. might be better to do this on the ext-core side,
312 -- as per earlier comment
313 qualify _ tc | tc == "Bool" = "Just boolMname" ++ ", "
314 ++ ze True tc
315 qualify _ tc | tc == "()" = "Just baseMname" ++ ", "
316 ++ ze True tc
317 qualify enc tc = "Just primMname" ++ ", " ++ (ze enc tc)
318 ze enc tc = (if enc then "zEncodeString " else "")
319 ++ "\"" ++ tc ++ "\""
320
321 intLitTys = prefixes ["Int", "Word", "Addr", "Char"]
322 ratLitTys = prefixes ["Float", "Double"]
323 charLitTys = prefixes ["Char"]
324 stringLitTys = prefixes ["Addr"]
325 prefixes ps = filter (\ t ->
326 case t of
327 (PrimTypeSpec {ty=(TyApp tc _args)}) ->
328 any (\ p -> p `isPrefixOf` tc) ps
329 _ -> False)
330
331 parens n ty' = " (zEncodeString \"" ++ n ++ "\", " ++ ty' ++ ")"
332 paren s = "(" ++ s ++ ")"
333 quote s = "\"" ++ s ++ "\""
334
335 gen_latex_doc :: Info -> String
336 gen_latex_doc (Info defaults entries)
337 = "\\primopdefaults{"
338 ++ mk_options defaults
339 ++ "}\n"
340 ++ (concat (map mk_entry entries))
341 where mk_entry (PrimOpSpec {cons=constr,name=n,ty=t,cat=c,desc=d,opts=o}) =
342 "\\primopdesc{"
343 ++ latex_encode constr ++ "}{"
344 ++ latex_encode n ++ "}{"
345 ++ latex_encode (zencode n) ++ "}{"
346 ++ latex_encode (show c) ++ "}{"
347 ++ latex_encode (mk_source_ty t) ++ "}{"
348 ++ latex_encode (mk_core_ty t) ++ "}{"
349 ++ d ++ "}{"
350 ++ mk_options o
351 ++ "}\n"
352 mk_entry (Section {title=ti,desc=d}) =
353 "\\primopsection{"
354 ++ latex_encode ti ++ "}{"
355 ++ d ++ "}\n"
356 mk_entry (PrimTypeSpec {ty=t,desc=d,opts=o}) =
357 "\\primtypespec{"
358 ++ latex_encode (mk_source_ty t) ++ "}{"
359 ++ latex_encode (mk_core_ty t) ++ "}{"
360 ++ d ++ "}{"
361 ++ mk_options o
362 ++ "}\n"
363 mk_entry (PseudoOpSpec {name=n,ty=t,desc=d,opts=o}) =
364 "\\pseudoopspec{"
365 ++ latex_encode (zencode n) ++ "}{"
366 ++ latex_encode (mk_source_ty t) ++ "}{"
367 ++ latex_encode (mk_core_ty t) ++ "}{"
368 ++ d ++ "}{"
369 ++ mk_options o
370 ++ "}\n"
371 mk_source_ty typ = pty typ
372 where pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
373 pty t = pbty t
374 pbty (TyApp tc ts) = tc ++ (concat (map (' ':) (map paty ts)))
375 pbty (TyUTup ts) = "(# " ++ (concat (intersperse "," (map pty ts))) ++ " #)"
376 pbty t = paty t
377 paty (TyVar tv) = tv
378 paty t = "(" ++ pty t ++ ")"
379
380 mk_core_ty typ = foralls ++ (pty typ)
381 where pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
382 pty t = pbty t
383 pbty (TyApp tc ts) = (zencode tc) ++ (concat (map (' ':) (map paty ts)))
384 pbty (TyUTup ts) = (zencode (utuplenm (length ts))) ++ (concat ((map (' ':) (map paty ts))))
385 pbty t = paty t
386 paty (TyVar tv) = zencode tv
387 paty (TyApp tc []) = zencode tc
388 paty t = "(" ++ pty t ++ ")"
389 utuplenm 1 = "(# #)"
390 utuplenm n = "(#" ++ (replicate (n-1) ',') ++ "#)"
391 foralls = if tvars == [] then "" else "%forall " ++ (tbinds tvars)
392 tvars = tvars_of typ
393 tbinds [] = ". "
394 tbinds ("o":tbs) = "(o::?) " ++ (tbinds tbs)
395 tbinds (tv:tbs) = tv ++ " " ++ (tbinds tbs)
396 tvars_of (TyF t1 t2) = tvars_of t1 `union` tvars_of t2
397 tvars_of (TyApp _ ts) = foldl union [] (map tvars_of ts)
398 tvars_of (TyUTup ts) = foldr union [] (map tvars_of ts)
399 tvars_of (TyVar tv) = [tv]
400
401 mk_options o =
402 "\\primoptions{"
403 ++ mk_has_side_effects o ++ "}{"
404 ++ mk_out_of_line o ++ "}{"
405 ++ mk_commutable o ++ "}{"
406 ++ mk_needs_wrapper o ++ "}{"
407 ++ mk_can_fail o ++ "}{"
408 ++ mk_fixity o ++ "}{"
409 ++ latex_encode (mk_strictness o) ++ "}{"
410 ++ "}"
411
412 mk_has_side_effects o = mk_bool_opt o "has_side_effects" "Has side effects." "Has no side effects."
413 mk_out_of_line o = mk_bool_opt o "out_of_line" "Implemented out of line." "Implemented in line."
414 mk_commutable o = mk_bool_opt o "commutable" "Commutable." "Not commutable."
415 mk_needs_wrapper o = mk_bool_opt o "needs_wrapper" "Needs wrapper." "Needs no wrapper."
416 mk_can_fail o = mk_bool_opt o "can_fail" "Can fail." "Cannot fail."
417
418 mk_bool_opt o opt_name if_true if_false =
419 case lookup_attrib opt_name o of
420 Just (OptionTrue _) -> if_true
421 Just (OptionFalse _) -> if_false
422 Just (OptionString _ _) -> error "String value for boolean option"
423 Just (OptionInteger _ _) -> error "Integer value for boolean option"
424 Just (OptionFixity _) -> error "Fixity value for boolean option"
425 Nothing -> ""
426
427 mk_strictness o =
428 case lookup_attrib "strictness" o of
429 Just (OptionString _ s) -> s -- for now
430 Just _ -> error "Wrong value for strictness"
431 Nothing -> ""
432
433 mk_fixity o = case lookup_attrib "fixity" o of
434 Just (OptionFixity (Just (Fixity i d)))
435 -> pprFixityDir d ++ " " ++ show i
436 _ -> ""
437
438 zencode xs =
439 case maybe_tuple xs of
440 Just n -> n -- Tuples go to Z2T etc
441 Nothing -> concat (map encode_ch xs)
442 where
443 maybe_tuple "(# #)" = Just("Z1H")
444 maybe_tuple ('(' : '#' : cs) = case count_commas (0::Int) cs of
445 (n, '#' : ')' : _) -> Just ('Z' : shows (n+1) "H")
446 _ -> Nothing
447 maybe_tuple "()" = Just("Z0T")
448 maybe_tuple ('(' : cs) = case count_commas (0::Int) cs of
449 (n, ')' : _) -> Just ('Z' : shows (n+1) "T")
450 _ -> Nothing
451 maybe_tuple _ = Nothing
452
453 count_commas :: Int -> String -> (Int, String)
454 count_commas n (',' : cs) = count_commas (n+1) cs
455 count_commas n cs = (n,cs)
456
457 unencodedChar :: Char -> Bool -- True for chars that don't need encoding
458 unencodedChar 'Z' = False
459 unencodedChar 'z' = False
460 unencodedChar c = isAlphaNum c
461
462 encode_ch :: Char -> String
463 encode_ch c | unencodedChar c = [c] -- Common case first
464
465 -- Constructors
466 encode_ch '(' = "ZL" -- Needed for things like (,), and (->)
467 encode_ch ')' = "ZR" -- For symmetry with (
468 encode_ch '[' = "ZM"
469 encode_ch ']' = "ZN"
470 encode_ch ':' = "ZC"
471 encode_ch 'Z' = "ZZ"
472
473 -- Variables
474 encode_ch 'z' = "zz"
475 encode_ch '&' = "za"
476 encode_ch '|' = "zb"
477 encode_ch '^' = "zc"
478 encode_ch '$' = "zd"
479 encode_ch '=' = "ze"
480 encode_ch '>' = "zg"
481 encode_ch '#' = "zh"
482 encode_ch '.' = "zi"
483 encode_ch '<' = "zl"
484 encode_ch '-' = "zm"
485 encode_ch '!' = "zn"
486 encode_ch '+' = "zp"
487 encode_ch '\'' = "zq"
488 encode_ch '\\' = "zr"
489 encode_ch '/' = "zs"
490 encode_ch '*' = "zt"
491 encode_ch '_' = "zu"
492 encode_ch '%' = "zv"
493 encode_ch c = 'z' : shows (ord c) "U"
494
495 latex_encode [] = []
496 latex_encode (c:cs) | c `elem` "#$%&_^{}" = "\\" ++ c:(latex_encode cs)
497 latex_encode ('~':cs) = "\\verb!~!" ++ (latex_encode cs)
498 latex_encode ('\\':cs) = "$\\backslash$" ++ (latex_encode cs)
499 latex_encode (c:cs) = c:(latex_encode cs)
500
501 gen_wrappers :: Info -> String
502 gen_wrappers (Info _ entries)
503 = "{-# LANGUAGE CPP, NoImplicitPrelude, UnboxedTuples #-}\n"
504 -- Dependencies on Prelude must be explicit in libraries/base, but we
505 -- don't need the Prelude here so we add NoImplicitPrelude.
506 ++ "module GHC.PrimopWrappers where\n"
507 ++ "import qualified GHC.Prim\n"
508 ++ "import GHC.Tuple ()\n"
509 ++ "import GHC.Prim (" ++ concat (intersperse ", " othertycons) ++ ")\n"
510 ++ "#if defined (__GLASGOW_HASKELL_LLVM__)\n"
511 ++ "import GHC.Prim (" ++ concat (intersperse ", " vectycons) ++ ")\n"
512 ++ "#endif /* defined (__GLASGOW_HASKELL_LLVM__) */\n"
513 ++ unlines (concatMap f otherspecs)
514 ++ "#if defined (__GLASGOW_HASKELL_LLVM__)\n"
515 ++ unlines (concatMap f vecspecs)
516 ++ "#endif /* defined (__GLASGOW_HASKELL_LLVM__) */\n"
517 where
518 specs = filter (not.dodgy) (filter is_primop entries)
519 (vecspecs, otherspecs) = partition is_llvm_only specs
520 tycons = foldr union [] $ map (tyconsIn . ty) specs
521 (vectycons, othertycons) =
522 (partition llvmOnlyTyCon . filter (`notElem` ["()", "Bool"])) tycons
523 f spec = let args = map (\n -> "a" ++ show n) [1 .. arity (ty spec)]
524 src_name = wrap (name spec)
525 lhs = src_name ++ " " ++ unwords args
526 rhs = "(GHC.Prim." ++ name spec ++ ") " ++ unwords args
527 in ["{-# NOINLINE " ++ src_name ++ " #-}",
528 src_name ++ " :: " ++ pprTy (ty spec),
529 lhs ++ " = " ++ rhs]
530 wrap nm | isLower (head nm) = nm
531 | otherwise = "(" ++ nm ++ ")"
532
533 dodgy spec
534 = name spec `elem`
535 [-- C code generator can't handle these
536 "seq#",
537 "tagToEnum#",
538 -- not interested in parallel support
539 "par#", "parGlobal#", "parLocal#", "parAt#",
540 "parAtAbs#", "parAtRel#", "parAtForNow#"
541 ]
542
543 is_llvm_only :: Entry -> Bool
544 is_llvm_only entry =
545 case lookup_attrib "llvm_only" (opts entry) of
546 Just (OptionTrue _) -> True
547 _ -> False
548
549 llvmOnlyTyCon :: TyCon -> Bool
550 llvmOnlyTyCon "Int32#" = True
551 llvmOnlyTyCon "FloatX4#" = True
552 llvmOnlyTyCon "DoubleX2#" = True
553 llvmOnlyTyCon "Int32X4#" = True
554 llvmOnlyTyCon "Int64X2#" = True
555 llvmOnlyTyCon _ = False
556
557 gen_primop_list :: Info -> String
558 gen_primop_list (Info _ entries)
559 = unlines (
560 [ " [" ++ cons first ]
561 ++
562 map (\p -> " , " ++ cons p) rest
563 ++
564 [ " ]" ]
565 ) where (first:rest) = filter is_primop entries
566
567 gen_primop_tag :: Info -> String
568 gen_primop_tag (Info _ entries)
569 = unlines (max_def_type : max_def :
570 tagOf_type : zipWith f primop_entries [1 :: Int ..])
571 where
572 primop_entries = filter is_primop entries
573 tagOf_type = "tagOf_PrimOp :: PrimOp -> FastInt"
574 f i n = "tagOf_PrimOp " ++ cons i ++ " = _ILIT(" ++ show n ++ ")"
575 max_def_type = "maxPrimOpTag :: Int"
576 max_def = "maxPrimOpTag = " ++ show (length primop_entries)
577
578 gen_data_decl :: Info -> String
579 gen_data_decl (Info _ entries)
580 = let conss = map cons (filter is_primop entries)
581 in "data PrimOp\n = " ++ head conss ++ "\n"
582 ++ unlines (map (" | "++) (tail conss))
583
584 gen_switch_from_attribs :: String -> String -> Info -> String
585 gen_switch_from_attribs attrib_name fn_name (Info defaults entries)
586 = let defv = lookup_attrib attrib_name defaults
587 alternatives = catMaybes (map mkAlt (filter is_primop entries))
588
589 getAltRhs (OptionFalse _) = "False"
590 getAltRhs (OptionTrue _) = "True"
591 getAltRhs (OptionInteger _ i) = show i
592 getAltRhs (OptionString _ s) = s
593 getAltRhs (OptionFixity mf) = show mf
594
595 mkAlt po
596 = case lookup_attrib attrib_name (opts po) of
597 Nothing -> Nothing
598 Just xx -> Just (fn_name ++ " " ++ cons po ++ " = " ++ getAltRhs xx)
599
600 in
601 case defv of
602 Nothing -> error ("gen_switch_from: " ++ attrib_name)
603 Just xx
604 -> unlines alternatives
605 ++ fn_name ++ " _ = " ++ getAltRhs xx ++ "\n"
606
607 ------------------------------------------------------------------
608 -- Create PrimOpInfo text from PrimOpSpecs -----------------------
609 ------------------------------------------------------------------
610
611 gen_primop_info :: Info -> String
612 gen_primop_info (Info _ entries)
613 = unlines (map mkPOItext (filter is_primop entries))
614
615 mkPOItext :: Entry -> String
616 mkPOItext i = mkPOI_LHS_text i ++ mkPOI_RHS_text i
617
618 mkPOI_LHS_text :: Entry -> String
619 mkPOI_LHS_text i
620 = "primOpInfo " ++ cons i ++ " = "
621
622 mkPOI_RHS_text :: Entry -> String
623 mkPOI_RHS_text i
624 = case cat i of
625 Compare
626 -> case ty i of
627 TyF t1 (TyF _ _)
628 -> "mkCompare " ++ sl_name i ++ ppType t1
629 _ -> error "Type error in comparison op"
630 Monadic
631 -> case ty i of
632 TyF t1 _
633 -> "mkMonadic " ++ sl_name i ++ ppType t1
634 _ -> error "Type error in monadic op"
635 Dyadic
636 -> case ty i of
637 TyF t1 (TyF _ _)
638 -> "mkDyadic " ++ sl_name i ++ ppType t1
639 _ -> error "Type error in dyadic op"
640 GenPrimOp
641 -> let (argTys, resTy) = flatTys (ty i)
642 tvs = nub (tvsIn (ty i))
643 in
644 "mkGenPrimOp " ++ sl_name i ++ " "
645 ++ listify (map ppTyVar tvs) ++ " "
646 ++ listify (map ppType argTys) ++ " "
647 ++ "(" ++ ppType resTy ++ ")"
648
649 sl_name :: Entry -> String
650 sl_name i = "(fsLit \"" ++ name i ++ "\") "
651
652 ppTyVar :: String -> String
653 ppTyVar "a" = "alphaTyVar"
654 ppTyVar "b" = "betaTyVar"
655 ppTyVar "c" = "gammaTyVar"
656 ppTyVar "s" = "deltaTyVar"
657 ppTyVar "o" = "openAlphaTyVar"
658 ppTyVar _ = error "Unknown type var"
659
660 ppType :: Ty -> String
661 ppType (TyApp "Any" []) = "anyTy"
662 ppType (TyApp "Bool" []) = "boolTy"
663
664 ppType (TyApp "Int#" []) = "intPrimTy"
665 ppType (TyApp "Int32#" []) = "int32PrimTy"
666 ppType (TyApp "Int64#" []) = "int64PrimTy"
667 ppType (TyApp "Char#" []) = "charPrimTy"
668 ppType (TyApp "Word#" []) = "wordPrimTy"
669 ppType (TyApp "Word32#" []) = "word32PrimTy"
670 ppType (TyApp "Word64#" []) = "word64PrimTy"
671 ppType (TyApp "Addr#" []) = "addrPrimTy"
672 ppType (TyApp "Float#" []) = "floatPrimTy"
673 ppType (TyApp "Double#" []) = "doublePrimTy"
674 ppType (TyApp "FloatX4#" []) = "floatX4PrimTy"
675 ppType (TyApp "DoubleX2#" []) = "doubleX2PrimTy"
676 ppType (TyApp "Int32X4#" []) = "int32X4PrimTy"
677 ppType (TyApp "Int64X2#" []) = "int64X2PrimTy"
678 ppType (TyApp "ByteArray#" []) = "byteArrayPrimTy"
679 ppType (TyApp "RealWorld" []) = "realWorldTy"
680 ppType (TyApp "ThreadId#" []) = "threadIdPrimTy"
681 ppType (TyApp "ForeignObj#" []) = "foreignObjPrimTy"
682 ppType (TyApp "BCO#" []) = "bcoPrimTy"
683 ppType (TyApp "()" []) = "unitTy" -- unitTy is TysWiredIn's name for ()
684
685 ppType (TyVar "a") = "alphaTy"
686 ppType (TyVar "b") = "betaTy"
687 ppType (TyVar "c") = "gammaTy"
688 ppType (TyVar "s") = "deltaTy"
689 ppType (TyVar "o") = "openAlphaTy"
690
691 ppType (TyApp "State#" [x]) = "mkStatePrimTy " ++ ppType x
692 ppType (TyApp "MutVar#" [x,y]) = "mkMutVarPrimTy " ++ ppType x
693 ++ " " ++ ppType y
694 ppType (TyApp "MutableArray#" [x,y]) = "mkMutableArrayPrimTy " ++ ppType x
695 ++ " " ++ ppType y
696 ppType (TyApp "MutableArrayArray#" [x]) = "mkMutableArrayArrayPrimTy " ++ ppType x
697 ppType (TyApp "MutableByteArray#" [x]) = "mkMutableByteArrayPrimTy "
698 ++ ppType x
699 ppType (TyApp "Array#" [x]) = "mkArrayPrimTy " ++ ppType x
700 ppType (TyApp "ArrayArray#" []) = "mkArrayArrayPrimTy"
701
702
703 ppType (TyApp "Weak#" [x]) = "mkWeakPrimTy " ++ ppType x
704 ppType (TyApp "StablePtr#" [x]) = "mkStablePtrPrimTy " ++ ppType x
705 ppType (TyApp "StableName#" [x]) = "mkStableNamePrimTy " ++ ppType x
706
707 ppType (TyApp "MVar#" [x,y]) = "mkMVarPrimTy " ++ ppType x
708 ++ " " ++ ppType y
709 ppType (TyApp "TVar#" [x,y]) = "mkTVarPrimTy " ++ ppType x
710 ++ " " ++ ppType y
711 ppType (TyUTup ts) = "(mkTupleTy UnboxedTuple "
712 ++ listify (map ppType ts) ++ ")"
713
714 ppType (TyF s d) = "(mkFunTy (" ++ ppType s ++ ") (" ++ ppType d ++ "))"
715
716 ppType other
717 = error ("ppType: can't handle: " ++ show other ++ "\n")
718
719 pprFixityDir :: FixityDirection -> String
720 pprFixityDir InfixN = "infix"
721 pprFixityDir InfixL = "infixl"
722 pprFixityDir InfixR = "infixr"
723
724 listify :: [String] -> String
725 listify ss = "[" ++ concat (intersperse ", " ss) ++ "]"
726
727 flatTys :: Ty -> ([Ty],Ty)
728 flatTys (TyF t1 t2) = case flatTys t2 of (ts,t) -> (t1:ts,t)
729 flatTys other = ([],other)
730
731 tvsIn :: Ty -> [TyVar]
732 tvsIn (TyF t1 t2) = tvsIn t1 ++ tvsIn t2
733 tvsIn (TyApp _ tys) = concatMap tvsIn tys
734 tvsIn (TyVar tv) = [tv]
735 tvsIn (TyUTup tys) = concatMap tvsIn tys
736
737 tyconsIn :: Ty -> [TyCon]
738 tyconsIn (TyF t1 t2) = tyconsIn t1 `union` tyconsIn t2
739 tyconsIn (TyApp tc tys) = foldr union [tc] $ map tyconsIn tys
740 tyconsIn (TyVar _) = []
741 tyconsIn (TyUTup tys) = foldr union [] $ map tyconsIn tys
742
743 arity :: Ty -> Int
744 arity = length . fst . flatTys