2 % (c) The University of Glasgow 2001
6 module MkExternalCore (
10 #include "HsVersions.h"
12 import qualified ExternalCore as C
20 import PprExternalCore -- Instances
21 import DataCon ( DataCon, dataConTyVars, dataConRepArgTys,
22 dataConName, dataConTyCon )
29 import NameSet ( NameSet, emptyNameSet )
30 import UniqSet ( elementOfUniqSet )
33 import DynFlags ( DynFlags(..) )
34 import StaticFlags ( opt_EmitExternalCore )
38 emitExternalCore :: DynFlags -> NameSet -> CgGuts -> IO ()
39 emitExternalCore dflags exports cg_guts
40 | opt_EmitExternalCore
41 = (do handle <- openFile corename WriteMode
42 hPutStrLn handle (show (mkExternalCore exports cg_guts))
44 `catch` (\err -> pprPanic "Failed to open or write external core output file"
46 where corename = extCoreName dflags
47 emitExternalCore _ _ _
52 mkExternalCore :: NameSet -> CgGuts -> C.Module
53 -- The ModGuts has been tidied, but the implicit bindings have
54 -- not been injected, so we have to add them manually here
55 -- We don't include the strange data-con *workers* because they are
56 -- implicit in the data type declaration itself
57 mkExternalCore exports (CgGuts {cg_module=this_mod, cg_tycons = tycons, cg_binds = binds})
58 = C.Module mname tdefs (map (make_vdef exports) binds)
60 mname = make_mid this_mod
61 tdefs = foldr collect_tdefs [] tycons
63 collect_tdefs :: TyCon -> [C.Tdef] -> [C.Tdef]
64 collect_tdefs tcon tdefs
65 | isAlgTyCon tcon = tdef: tdefs
67 tdef | isNewTyCon tcon =
68 C.Newtype (make_con_qid (tyConName tcon)) (map make_tbind tyvars) repclause
69 -- 20060420 GHC handles empty data types just fine. ExtCore should too! jds
70 -- | null (tyConDataCons tcon) = error "MkExternalCore died: can't handle datatype declarations with no data constructors"
72 C.Data (make_con_qid (tyConName tcon)) (map make_tbind tyvars) (map make_cdef (tyConDataCons tcon))
73 where repclause | isRecursiveTyCon tcon = Nothing
74 | otherwise = Just (make_ty rep)
75 where (_, rep) = newTyConRep tcon
76 tyvars = tyConTyVars tcon
78 collect_tdefs _ tdefs = tdefs
81 make_cdef :: DataCon -> C.Cdef
82 make_cdef dcon = C.Constr dcon_name existentials tys
84 dcon_name = make_var_id (dataConName dcon)
85 existentials = map make_tbind ex_tyvars
86 ex_tyvars = drop (tyConArity (dataConTyCon dcon)) (dataConTyVars dcon)
87 tys = map make_ty (dataConRepArgTys dcon)
89 make_tbind :: TyVar -> C.Tbind
90 make_tbind tv = (make_var_id (tyVarName tv), make_kind (tyVarKind tv))
92 make_vbind :: Var -> C.Vbind
93 make_vbind v = (make_var_id (Var.varName v), make_ty (idType v))
95 make_vdef :: NameSet -> CoreBind -> C.Vdefg
98 NonRec v e -> C.Nonrec (f (v,e))
99 Rec ves -> C.Rec (map f ves)
101 f (v,e) = (local, make_var_id (Var.varName v), make_ty (idType v),make_exp e)
102 where local = not $ elementOfUniqSet (Var.varName v) exports
103 -- Top level bindings are unqualified now
105 make_exp :: CoreExpr -> C.Exp
107 case globalIdDetails v of
108 -- a DataConId represents the Id of a worker, which is a varName. -- sof 4/02
109 -- DataConId _ -> C.Dcon (make_con_qid (Var.varName v))
110 FCallId (CCall (CCallSpec (StaticTarget nm) _ _)) -> C.External (unpackFS nm) (make_ty (idType v))
111 FCallId _ -> error "MkExternalCore died: can't handle non-static-C foreign call"
112 _ -> C.Var (make_var_qid (Var.varName v))
113 make_exp (Lit (l@(MachLabel s _))) = error "MkExternalCore died: can't handle \"foreign label\" declarations"
114 make_exp (Lit l) = C.Lit (make_lit l)
115 make_exp (App e (Type t)) = C.Appt (make_exp e) (make_ty t)
116 make_exp (App e1 e2) = C.App (make_exp e1) (make_exp e2)
117 make_exp (Lam v e) | isTyVar v = C.Lam (C.Tb (make_tbind v)) (make_exp e)
118 make_exp (Lam v e) | otherwise = C.Lam (C.Vb (make_vbind v)) (make_exp e)
119 make_exp (Let b e) = C.Let (make_vdef emptyNameSet b) (make_exp e)
121 make_exp (Case e v ty alts) = C.Case (make_exp e) (make_vbind v) (make_ty ty) (map make_alt alts)
122 make_exp (Note (SCC cc) e) = C.Note "SCC" (make_exp e) -- temporary
123 make_exp (Note (Coerce t_to t_from) e) = C.Coerce (make_ty t_to) (make_exp e)
124 make_exp (Note (CoreNote s) e) = C.Note s (make_exp e) -- hdaume: core annotations
125 make_exp (Note InlineMe e) = C.Note "InlineMe" (make_exp e)
126 make_exp _ = error "MkExternalCore died: make_exp"
128 make_alt :: CoreAlt -> C.Alt
129 make_alt (DataAlt dcon, vs, e) =
130 C.Acon (make_con_qid (dataConName dcon))
134 where (tbs,vbs) = span isTyVar vs
135 make_alt (LitAlt l,_,e) = C.Alit (make_lit l) (make_exp e)
136 make_alt (DEFAULT,[],e) = C.Adefault (make_exp e)
138 make_lit :: Literal -> C.Lit
141 MachChar i -> C.Lchar i t
142 MachStr s -> C.Lstring (unpackFS s) t
143 MachNullAddr -> C.Lint 0 t
144 MachInt i -> C.Lint i t
145 MachInt64 i -> C.Lint i t
146 MachWord i -> C.Lint i t
147 MachWord64 i -> C.Lint i t
148 MachFloat r -> C.Lrational r t
149 MachDouble r -> C.Lrational r t
150 _ -> error "MkExternalCore died: make_lit"
152 t = make_ty (literalType l)
154 make_ty :: Type -> C.Ty
155 make_ty (TyVarTy tv) = C.Tvar (make_var_id (tyVarName tv))
156 make_ty (AppTy t1 t2) = C.Tapp (make_ty t1) (make_ty t2)
157 make_ty (FunTy t1 t2) = make_ty (TyConApp funTyCon [t1,t2])
158 make_ty (ForAllTy tv t) = C.Tforall (make_tbind tv) (make_ty t)
159 make_ty (TyConApp tc ts) = foldl C.Tapp (C.Tcon (make_con_qid (tyConName tc)))
161 -- Newtypes are treated just like any other type constructor; not expanded
162 -- Reason: predTypeRep does substitution and, while substitution deals
163 -- correctly with name capture, it's only correct if you see the uniques!
164 -- If you just see occurrence names, name capture may occur.
165 -- Example: newtype A a = A (forall b. b -> a)
166 -- test :: forall q b. q -> A b
167 -- test _ = undefined
168 -- Here the 'a' gets substituted by 'b', which is captured.
169 -- Another solution would be to expand newtypes before tidying; but that would
170 -- expose the representation in interface files, which definitely isn't right.
171 -- Maybe CoreTidy should know whether to expand newtypes or not?
173 make_ty (PredTy p) = make_ty (predTypeRep p)
174 make_ty (NoteTy _ t) = make_ty t
178 make_kind :: Kind -> C.Kind
179 make_kind (FunKind k1 k2) = C.Karrow (make_kind k1) (make_kind k2)
180 make_kind LiftedTypeKind = C.Klifted
181 make_kind UnboxedTypeKind = C.Kunboxed
182 make_kind UnliftedTypeKind = C.Kunlifted
183 make_kind OpenTypeKind = C.Kopen
184 make_kind _ = error "MkExternalCore died: make_kind"
188 {- Use encoded strings.
189 Also, adjust casing to work around some badly-chosen internal names. -}
190 make_id :: Bool -> Name -> C.Id
191 make_id is_var nm = (occNameString . nameOccName) nm
193 {- SIMON thinks this stuff isn't necessary
196 'Z':cs | is_var -> 'z':cs
197 'z':cs | not is_var -> 'Z':cs
198 c:cs | isUpper c && is_var -> 'z':'d':n
199 c:cs | isLower c && (not is_var) -> 'Z':'d':n
201 where n = (occNameString . nameOccName) nm
204 make_var_id :: Name -> C.Id
205 make_var_id = make_id True
207 make_mid :: Module -> C.Id
208 make_mid = showSDoc . pprModule
210 make_qid :: Bool -> Name -> C.Qual C.Id
211 make_qid is_var n = (mname,make_id is_var n)
213 case nameModule_maybe n of
217 make_var_qid :: Name -> C.Qual C.Id
218 make_var_qid = make_qid True
220 make_con_qid :: Name -> C.Qual C.Id
221 make_con_qid = make_qid False