2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-2000
4 \section[StgInterp]{Translates STG syntax to interpretable form, and run it}
11 filterNameMap, -- :: [ModuleName] -> FiniteMap Name a
12 -- -> FiniteMap Name a
14 linkIModules, -- :: ItblEnv -> ClosureEnv
15 -- -> [([UnlinkedIBind], ItblEnv)]
16 -- -> IO ([LinkedIBind], ItblEnv, ClosureEnv)
18 iExprToHValue, -- :: ItblEnv -> ClosureEnv
19 -- -> UnlinkedIExpr -> HValue
21 stgBindsToInterpSyn,-- :: [StgBinding]
22 -- -> [TyCon] -> [Class]
23 -- -> IO ([UnlinkedIBind], ItblEnv)
25 stgExprToInterpSyn, -- :: StgExpr
26 -- -> IO UnlinkedIExpr
28 interp -- :: LinkedIExpr -> HValue
31 {- -----------------------------------------------------------------------------
34 - link should be in the IO monad, so it can modify the symtabs as it
37 - need a way to remove the bindings for a module from the symtabs.
38 maybe the symtabs should be indexed by module first.
40 - change the representation to something less verbose (?).
42 - converting string literals to Addr# is horrible and introduces
43 a memory leak. See if something can be done about this.
45 - lots of assumptions about word size vs. double size etc.
47 ----------------------------------------------------------------------------- -}
49 #include "HsVersions.h"
52 import Id ( Id, idPrimRep )
55 import PrimOp ( PrimOp(..) )
56 import PrimRep ( PrimRep(..) )
57 import Literal ( Literal(..) )
58 import Type ( Type, typePrimRep, deNoteType, repType, funResultTy )
59 import DataCon ( DataCon, dataConTag, dataConRepArgTys )
60 import ClosureInfo ( mkVirtHeapOffsets )
61 import Module ( ModuleName, moduleName )
63 import Name hiding (filterNameEnv)
68 import {-# SOURCE #-} MCI_make_constr
71 import GlaExts ( Int(..) )
72 import Module ( moduleNameFS )
74 import TyCon ( TyCon, isDataTyCon, tyConDataCons, tyConFamilySize )
75 import Class ( Class, classTyCon )
79 import OccName ( occNameString )
80 import ErrUtils ( showPass, dumpIfSet_dyn )
81 import CmdLineOpts ( DynFlags, DynFlag(..) )
82 import Panic ( panic )
92 import PrelGHC --( unsafeCoerce#, dataToTag#,
93 -- indexPtrOffClosure#, indexWordOffClosure# )
94 import PrelAddr ( Addr(..) )
95 import PrelFloat ( Float(..), Double(..) )
97 -- ---------------------------------------------------------------------------
98 -- Environments needed by the linker
99 -- ---------------------------------------------------------------------------
101 type ItblEnv = FiniteMap Name (Ptr StgInfoTable)
102 type ClosureEnv = FiniteMap Name HValue
103 emptyClosureEnv = emptyFM
105 -- remove all entries for a given set of modules from the environment
106 filterNameMap :: [ModuleName] -> FiniteMap Name a -> FiniteMap Name a
107 filterNameMap mods env
108 = filterFM (\n _ -> moduleName (nameModule n) `notElem` mods) env
110 -- ---------------------------------------------------------------------------
111 -- Turn an UnlinkedIExpr into a value we can run, for the interpreter
112 -- ---------------------------------------------------------------------------
114 iExprToHValue :: ItblEnv -> ClosureEnv -> UnlinkedIExpr -> IO HValue
115 iExprToHValue ie ce expr
116 = do linked_expr <- linkIExpr ie ce expr
117 return (interp linked_expr)
119 -- ---------------------------------------------------------------------------
120 -- Convert STG to an unlinked interpretable
121 -- ---------------------------------------------------------------------------
123 -- visible from outside
124 stgBindsToInterpSyn :: DynFlags
126 -> [TyCon] -> [Class]
127 -> IO ([UnlinkedIBind], ItblEnv)
128 stgBindsToInterpSyn dflags binds local_tycons local_classes
129 = do showPass dflags "StgToInterp"
130 let ibinds = concatMap (translateBind emptyUniqSet) binds
131 let tycs = local_tycons ++ map classTyCon local_classes
132 dumpIfSet_dyn dflags Opt_D_dump_InterpSyn
133 "Convert To InterpSyn" (vcat (map pprIBind ibinds))
134 itblenv <- mkITbls tycs
135 return (ibinds, itblenv)
137 stgExprToInterpSyn :: DynFlags
140 stgExprToInterpSyn dflags expr
141 = do showPass dflags "StgToInterp"
142 let iexpr = stg2expr emptyUniqSet expr
143 dumpIfSet_dyn dflags Opt_D_dump_InterpSyn
144 "Convert To InterpSyn" (pprIExpr iexpr)
147 translateBind :: UniqSet Id -> StgBinding -> [UnlinkedIBind]
148 translateBind ie (StgNonRec v e) = [IBind v (rhs2expr ie e)]
149 translateBind ie (StgRec vs_n_es) = [IBind v (rhs2expr ie' e) | (v,e) <- vs_n_es]
150 where ie' = addListToUniqSet ie (map fst vs_n_es)
152 isRec (StgNonRec _ _) = False
153 isRec (StgRec _) = True
155 rhs2expr :: UniqSet Id -> StgRhs -> UnlinkedIExpr
156 rhs2expr ie (StgRhsClosure ccs binfo srt fvs uflag args rhs)
159 rhsExpr = stg2expr (addListToUniqSet ie args) rhs
160 rhsRep = repOfStgExpr rhs
161 mkLambdas [] = rhsExpr
162 mkLambdas [v] = mkLam (repOfId v) rhsRep v rhsExpr
163 mkLambdas (v:vs) = mkLam (repOfId v) RepP v (mkLambdas vs)
164 rhs2expr ie (StgRhsCon ccs dcon args)
165 = conapp2expr ie dcon args
167 conapp2expr :: UniqSet Id -> DataCon -> [StgArg] -> UnlinkedIExpr
168 conapp2expr ie dcon args
169 = mkConApp con_rdrname reps exprs
171 con_rdrname = getName dcon
172 exprs = map (arg2expr ie) inHeapOrder
173 reps = map repOfArg inHeapOrder
174 inHeapOrder = toHeapOrder args
176 toHeapOrder :: [StgArg] -> [StgArg]
178 = let (_, _, rearranged_w_offsets) = mkVirtHeapOffsets getArgPrimRep args
179 (rearranged, offsets) = unzip rearranged_w_offsets
183 foreign label "PrelBase_Izh_con_info" prelbase_Izh_con_info :: Addr
185 -- Handle most common cases specially; do the rest with a generic
186 -- mechanism (deferred till later :)
187 mkConApp :: Name -> [Rep] -> [UnlinkedIExpr] -> UnlinkedIExpr
188 mkConApp nm [] [] = ConApp nm
189 mkConApp nm [RepI] [a1] = ConAppI nm a1
190 mkConApp nm [RepP] [a1] = ConAppP nm a1
191 mkConApp nm [RepP,RepP] [a1,a2] = ConAppPP nm a1 a2
192 mkConApp nm reps args = ConAppGen nm args
194 mkLam RepP RepP = LamPP
195 mkLam RepI RepP = LamIP
196 mkLam RepP RepI = LamPI
197 mkLam RepI RepI = LamII
198 mkLam repa repr = pprPanic "StgInterp.mkLam" (ppr repa <+> ppr repr)
200 mkApp RepP RepP = AppPP
201 mkApp RepI RepP = AppIP
202 mkApp RepP RepI = AppPI
203 mkApp RepI RepI = AppII
204 mkApp repa repr = pprPanic "StgInterp.mkApp" (ppr repa <+> ppr repr)
207 repOfId = primRep2Rep . idPrimRep
212 -- genuine lifted types
215 -- all these are unboxed, fit into a word, and we assume they
216 -- all have the same call/return convention.
224 -- these are pretty dodgy: really pointers, but
225 -- we can't let the compiler build thunks with these reps.
226 ForeignObjRep -> RepP
227 StableNameRep -> RepP
235 other -> pprPanic "primRep2Rep" (ppr other)
237 repOfStgExpr :: StgExpr -> Rep
242 StgCase scrut live liveR bndr srt alts
243 -> case altRhss alts of
244 (a:_) -> repOfStgExpr a
245 [] -> panic "repOfStgExpr: no alts"
249 -> repOfApp ((deNoteType.repType.idType) var) (length args)
251 StgPrimApp op args res_ty
252 -> (primRep2Rep.typePrimRep) res_ty
254 StgLet binds body -> repOfStgExpr body
255 StgLetNoEscape live liveR binds body -> repOfStgExpr body
257 StgConApp con args -> RepP -- by definition
260 -> pprPanic "repOfStgExpr" (ppr other)
262 altRhss (StgAlgAlts tycon alts def)
263 = [rhs | (dcon,bndrs,uses,rhs) <- alts] ++ defRhs def
264 altRhss (StgPrimAlts tycon alts def)
265 = [rhs | (lit,rhs) <- alts] ++ defRhs def
268 defRhs (StgBindDefault rhs)
271 -- returns the Rep of the result of applying ty to n args.
272 repOfApp :: Type -> Int -> Rep
273 repOfApp ty 0 = (primRep2Rep.typePrimRep) ty
274 repOfApp ty n = repOfApp (funResultTy ty) (n-1)
286 MachStr _ -> RepI -- because it's a ptr outside the heap
287 other -> pprPanic "repOfLit" (ppr lit)
289 lit2expr :: Literal -> UnlinkedIExpr
292 MachInt i -> case fromIntegral i of I# i -> LitI i
293 MachWord i -> case fromIntegral i of I# i -> LitI i
294 MachAddr i -> case fromIntegral i of I# i -> LitI i
295 MachChar i -> case fromIntegral i of I# i -> LitI i
296 MachFloat f -> case fromRational f of F# f -> LitF f
297 MachDouble f -> case fromRational f of D# f -> LitD f
300 CharStr s i -> LitI (addr2Int# s)
303 -- sigh, a string in the heap is no good to us. We need a
304 -- static C pointer, since the type of a string literal is
305 -- Addr#. So, copy the string into C land and introduce a
306 -- memory leak at the same time.
308 -- CAREFUL! Chars are 32 bits in ghc 4.09+
309 case unsafePerformIO (do a@(Ptr addr) <- mallocBytes (n+1)
310 strncpy a ba (fromIntegral n)
311 writeCharOffAddr addr n '\0'
313 of A# a -> LitI (addr2Int# a)
315 _ -> error "StgInterp.lit2expr: unhandled string constant type"
317 other -> pprPanic "lit2expr" (ppr lit)
319 stg2expr :: UniqSet Id -> StgExpr -> UnlinkedIExpr
323 -> mkVar ie (repOfId var) var
326 -> mkAppChain ie (repOfStgExpr stgexpr) (mkVar ie (repOfId var) var) args
330 StgCase scrut live liveR bndr srt (StgPrimAlts ty alts def)
331 | repOfStgExpr scrut /= RepP
332 -> mkCasePrim (repOfStgExpr stgexpr)
333 bndr (stg2expr ie scrut)
334 (map (doPrimAlt ie') alts)
337 pprPanic "stg2expr(StgCase,prim)" (ppr (repOfStgExpr scrut) $$ (case scrut of (StgApp v _) -> ppr v <+> ppr (idType v) <+> ppr (idPrimRep v)) $$ ppr stgexpr)
338 where ie' = addOneToUniqSet ie bndr
340 StgCase scrut live liveR bndr srt (StgAlgAlts tycon alts def)
341 | repOfStgExpr scrut == RepP
342 -> mkCaseAlg (repOfStgExpr stgexpr)
343 bndr (stg2expr ie scrut)
344 (map (doAlgAlt ie') alts)
346 where ie' = addOneToUniqSet ie bndr
349 StgPrimApp op args res_ty
350 -> mkPrimOp (repOfStgExpr stgexpr) op (map (arg2expr ie) args)
353 -> conapp2expr ie dcon args
355 StgLet binds@(StgNonRec v e) body
356 -> mkNonRec (repOfStgExpr stgexpr)
357 (head (translateBind ie binds))
358 (stg2expr (addOneToUniqSet ie v) body)
360 StgLet binds@(StgRec bs) body
361 -> mkRec (repOfStgExpr stgexpr)
362 (translateBind ie binds)
363 (stg2expr (addListToUniqSet ie (map fst bs)) body)
365 -- treat let-no-escape just like let.
366 StgLetNoEscape _ _ binds body
367 -> stg2expr ie (StgLet binds body)
370 -> pprPanic "stg2expr" (ppr stgexpr)
372 doPrimAlt ie (lit,rhs)
373 = AltPrim (lit2expr lit) (stg2expr ie rhs)
374 doAlgAlt ie (dcon,vars,uses,rhs)
375 = AltAlg (dataConTag dcon - 1)
376 (map id2VaaRep (toHeapOrder vars))
377 (stg2expr (addListToUniqSet ie vars) rhs)
380 = let (_,_,rearranged_w_offsets) = mkVirtHeapOffsets idPrimRep vars
381 (rearranged,offsets) = unzip rearranged_w_offsets
385 def2expr ie StgNoDefault = Nothing
386 def2expr ie (StgBindDefault rhs) = Just (stg2expr ie rhs)
388 mkAppChain ie result_rep so_far []
390 mkAppChain ie result_rep so_far [a]
391 = mkApp (repOfArg a) result_rep so_far (arg2expr ie a)
392 mkAppChain ie result_rep so_far (a:as)
393 = mkAppChain ie result_rep (mkApp (repOfArg a) RepP so_far (arg2expr ie a)) as
395 mkCasePrim RepI = CasePrimI
396 mkCasePrim RepP = CasePrimP
398 mkCaseAlg RepI = CaseAlgI
399 mkCaseAlg RepP = CaseAlgP
401 -- any var that isn't in scope is turned into a Native
403 | var `elementOfUniqSet` ie =
409 | otherwise = Native (getName var)
413 mkNonRec RepI = NonRecI
414 mkNonRec RepP = NonRecP
416 mkPrimOp RepI = PrimOpI
417 mkPrimOp RepP = PrimOpP
419 arg2expr :: UniqSet Id -> StgArg -> UnlinkedIExpr
420 arg2expr ie (StgVarArg v) = mkVar ie (repOfId v) v
421 arg2expr ie (StgLitArg lit) = lit2expr lit
422 arg2expr ie (StgTypeArg ty) = pprPanic "arg2expr" (ppr ty)
424 repOfArg :: StgArg -> Rep
425 repOfArg (StgVarArg v) = repOfId v
426 repOfArg (StgLitArg lit) = repOfLit lit
427 repOfArg (StgTypeArg ty) = pprPanic "repOfArg" (ppr ty)
429 id2VaaRep var = (var, repOfId var)
432 -- ---------------------------------------------------------------------------
433 -- Link interpretables into something we can run
434 -- ---------------------------------------------------------------------------
436 GLOBAL_VAR(cafTable, [], [HValue])
438 addCAF :: HValue -> IO ()
439 addCAF x = do xs <- readIORef cafTable; writeIORef cafTable (x:xs)
441 linkIModules :: ItblEnv -- incoming global itbl env; returned updated
442 -> ClosureEnv -- incoming global closure env; returned updated
443 -> [([UnlinkedIBind], ItblEnv)]
444 -> IO ([LinkedIBind], ItblEnv, ClosureEnv)
445 linkIModules gie gce mods = do
446 let (bindss, ies) = unzip mods
447 binds = concat bindss
448 top_level_binders = map (getName.binder) binds
449 final_gie = foldr plusFM gie ies
451 (new_binds, new_gce) <-
452 fixIO (\ ~(new_binds, new_gce) -> do
454 new_binds <- linkIBinds final_gie new_gce binds
456 let new_rhss = map (\b -> evalP (bindee b) emptyUFM) new_binds
457 let new_gce = addListToFM gce (zip top_level_binders new_rhss)
459 return (new_binds, new_gce))
461 return (new_binds, final_gie, new_gce)
464 -- We're supposed to augment the environments with the values of any
465 -- external functions/info tables we need as we go along, but that's a
466 -- lot of hassle so for now I'll look up external things as they crop
467 -- up and not cache them in the source symbol tables. The interpreted
468 -- code will still be referenced in the source symbol tables.
470 linkIBinds :: ItblEnv -> ClosureEnv -> [UnlinkedIBind] -> IO [LinkedIBind]
471 linkIBinds ie ce binds = mapM (linkIBind ie ce) binds
473 linkIBind ie ce (IBind bndr expr)
474 = do expr <- linkIExpr ie ce expr
475 return (IBind bndr expr)
477 linkIExpr :: ItblEnv -> ClosureEnv -> UnlinkedIExpr -> IO LinkedIExpr
478 linkIExpr ie ce expr = case expr of
480 CaseAlgP bndr expr alts dflt -> linkAlgCase ie ce bndr expr alts dflt CaseAlgP
481 CaseAlgI bndr expr alts dflt -> linkAlgCase ie ce bndr expr alts dflt CaseAlgI
482 CaseAlgF bndr expr alts dflt -> linkAlgCase ie ce bndr expr alts dflt CaseAlgF
483 CaseAlgD bndr expr alts dflt -> linkAlgCase ie ce bndr expr alts dflt CaseAlgD
485 CasePrimP bndr expr alts dflt -> linkPrimCase ie ce bndr expr alts dflt CasePrimP
486 CasePrimI bndr expr alts dflt -> linkPrimCase ie ce bndr expr alts dflt CasePrimI
487 CasePrimF bndr expr alts dflt -> linkPrimCase ie ce bndr expr alts dflt CasePrimF
488 CasePrimD bndr expr alts dflt -> linkPrimCase ie ce bndr expr alts dflt CasePrimD
490 ConApp con -> lookupNullaryCon ie con
492 ConAppI con arg0 -> do
493 con' <- lookupCon ie con
494 arg' <- linkIExpr ie ce arg0
495 return (ConAppI con' arg')
497 ConAppP con arg0 -> do
498 con' <- lookupCon ie con
499 arg' <- linkIExpr ie ce arg0
500 return (ConAppP con' arg')
502 ConAppPP con arg0 arg1 -> do
503 con' <- lookupCon ie con
504 arg0' <- linkIExpr ie ce arg0
505 arg1' <- linkIExpr ie ce arg1
506 return (ConAppPP con' arg0' arg1')
508 ConAppGen con args -> do
509 con <- lookupCon ie con
510 args <- mapM (linkIExpr ie ce) args
511 return (ConAppGen con args)
513 PrimOpI op args -> linkPrimOp ie ce PrimOpI op args
514 PrimOpP op args -> linkPrimOp ie ce PrimOpP op args
516 NonRecP bind expr -> linkNonRec ie ce NonRecP bind expr
517 NonRecI bind expr -> linkNonRec ie ce NonRecI bind expr
518 NonRecF bind expr -> linkNonRec ie ce NonRecF bind expr
519 NonRecD bind expr -> linkNonRec ie ce NonRecD bind expr
521 RecP binds expr -> linkRec ie ce RecP binds expr
522 RecI binds expr -> linkRec ie ce RecI binds expr
523 RecF binds expr -> linkRec ie ce RecF binds expr
524 RecD binds expr -> linkRec ie ce RecD binds expr
526 LitI i -> return (LitI i)
527 LitF i -> return (LitF i)
528 LitD i -> return (LitD i)
530 Native var -> lookupNative ce var
532 VarP v -> lookupVar ce VarP v
533 VarI v -> lookupVar ce VarI v
534 VarF v -> lookupVar ce VarF v
535 VarD v -> lookupVar ce VarD v
537 LamPP bndr expr -> linkLam ie ce LamPP bndr expr
538 LamPI bndr expr -> linkLam ie ce LamPI bndr expr
539 LamPF bndr expr -> linkLam ie ce LamPF bndr expr
540 LamPD bndr expr -> linkLam ie ce LamPD bndr expr
541 LamIP bndr expr -> linkLam ie ce LamIP bndr expr
542 LamII bndr expr -> linkLam ie ce LamII bndr expr
543 LamIF bndr expr -> linkLam ie ce LamIF bndr expr
544 LamID bndr expr -> linkLam ie ce LamID bndr expr
545 LamFP bndr expr -> linkLam ie ce LamFP bndr expr
546 LamFI bndr expr -> linkLam ie ce LamFI bndr expr
547 LamFF bndr expr -> linkLam ie ce LamFF bndr expr
548 LamFD bndr expr -> linkLam ie ce LamFD bndr expr
549 LamDP bndr expr -> linkLam ie ce LamDP bndr expr
550 LamDI bndr expr -> linkLam ie ce LamDI bndr expr
551 LamDF bndr expr -> linkLam ie ce LamDF bndr expr
552 LamDD bndr expr -> linkLam ie ce LamDD bndr expr
554 AppPP fun arg -> linkApp ie ce AppPP fun arg
555 AppPI fun arg -> linkApp ie ce AppPI fun arg
556 AppPF fun arg -> linkApp ie ce AppPF fun arg
557 AppPD fun arg -> linkApp ie ce AppPD fun arg
558 AppIP fun arg -> linkApp ie ce AppIP fun arg
559 AppII fun arg -> linkApp ie ce AppII fun arg
560 AppIF fun arg -> linkApp ie ce AppIF fun arg
561 AppID fun arg -> linkApp ie ce AppID fun arg
562 AppFP fun arg -> linkApp ie ce AppFP fun arg
563 AppFI fun arg -> linkApp ie ce AppFI fun arg
564 AppFF fun arg -> linkApp ie ce AppFF fun arg
565 AppFD fun arg -> linkApp ie ce AppFD fun arg
566 AppDP fun arg -> linkApp ie ce AppDP fun arg
567 AppDI fun arg -> linkApp ie ce AppDI fun arg
568 AppDF fun arg -> linkApp ie ce AppDF fun arg
569 AppDD fun arg -> linkApp ie ce AppDD fun arg
571 linkAlgCase ie ce bndr expr alts dflt con
572 = do expr <- linkIExpr ie ce expr
573 alts <- mapM (linkAlgAlt ie ce) alts
574 dflt <- linkDefault ie ce dflt
575 return (con bndr expr alts dflt)
577 linkPrimCase ie ce bndr expr alts dflt con
578 = do expr <- linkIExpr ie ce expr
579 alts <- mapM (linkPrimAlt ie ce) alts
580 dflt <- linkDefault ie ce dflt
581 return (con bndr expr alts dflt)
583 linkAlgAlt ie ce (AltAlg tag args rhs)
584 = do rhs <- linkIExpr ie ce rhs
585 return (AltAlg tag args rhs)
587 linkPrimAlt ie ce (AltPrim lit rhs)
588 = do rhs <- linkIExpr ie ce rhs
589 lit <- linkIExpr ie ce lit
590 return (AltPrim lit rhs)
592 linkDefault ie ce Nothing = return Nothing
593 linkDefault ie ce (Just expr)
594 = do expr <- linkIExpr ie ce expr
597 linkNonRec ie ce con bind expr
598 = do expr <- linkIExpr ie ce expr
599 bind <- linkIBind ie ce bind
600 return (con bind expr)
602 linkRec ie ce con binds expr
603 = do expr <- linkIExpr ie ce expr
604 binds <- linkIBinds ie ce binds
605 return (con binds expr)
607 linkLam ie ce con bndr expr
608 = do expr <- linkIExpr ie ce expr
609 return (con bndr expr)
611 linkApp ie ce con fun arg
612 = do fun <- linkIExpr ie ce fun
613 arg <- linkIExpr ie ce arg
616 linkPrimOp ie ce con op args
617 = do args <- mapM (linkIExpr ie ce) args
621 case lookupFM ie con of
622 Just (Ptr addr) -> return addr
624 -- try looking up in the object files.
625 m <- lookupSymbol (nameToCLabel con "con_info")
627 Just addr -> return addr
628 Nothing -> pprPanic "linkIExpr" (ppr con)
630 -- nullary constructors don't have normal _con_info tables.
631 lookupNullaryCon ie con =
632 case lookupFM ie con of
633 Just (Ptr addr) -> return (ConApp addr)
635 -- try looking up in the object files.
636 m <- lookupSymbol (nameToCLabel con "closure")
638 Just (A# addr) -> return (Native (unsafeCoerce# addr))
639 Nothing -> pprPanic "lookupNullaryCon" (ppr con)
642 lookupNative ce var =
643 unsafeInterleaveIO (do
644 case lookupFM ce var of
645 Just e -> return (Native e)
647 -- try looking up in the object files.
648 let lbl = (nameToCLabel var "closure")
649 m <- lookupSymbol lbl
652 -> do addCAF (unsafeCoerce# addr)
653 return (Native (unsafeCoerce# addr))
654 Nothing -> pprPanic "linkIExpr" (ppr var)
657 -- some VarI/VarP refer to top-level interpreted functions; we change
658 -- them into Natives here.
661 case lookupFM ce (getName v) of
662 Nothing -> return (f v)
663 Just e -> return (Native e)
666 -- HACK!!! ToDo: cleaner
667 nameToCLabel :: Name -> String{-suffix-} -> String
668 nameToCLabel n suffix =
669 _UNPK_(moduleNameFS (rdrNameModule rn))
670 ++ '_':occNameString(rdrNameOcc rn) ++ '_':suffix
671 where rn = toRdrName n
673 -- ---------------------------------------------------------------------------
674 -- The interpreter proper
675 -- ---------------------------------------------------------------------------
677 -- The dynamic environment contains everything boxed.
678 -- eval* functions which look up values in it will know the
679 -- representation of the thing they are looking up, so they
680 -- can cast/unbox it as necessary.
682 -- ---------------------------------------------------------------------------
683 -- Evaluator for things of boxed (pointer) representation
684 -- ---------------------------------------------------------------------------
686 interp :: LinkedIExpr -> HValue
687 interp iexpr = unsafeCoerce# (evalP iexpr emptyUFM)
689 evalP :: LinkedIExpr -> UniqFM boxed -> boxed
693 -- | trace ("evalP: " ++ showExprTag expr) False
694 | trace ("evalP:\n" ++ showSDoc (pprIExpr expr) ++ "\n") False
695 = error "evalP: ?!?!"
698 evalP (Native p) de = unsafeCoerce# p
700 -- First try the dynamic env. If that fails, assume it's a top-level
701 -- binding and look in the static env. That gives an Expr, which we
702 -- must convert to a boxed thingy by applying evalP to it. Because
703 -- top-level bindings are always ptr-rep'd (either lambdas or boxed
704 -- CAFs), it's always safe to use evalP.
706 = case lookupUFM de v of
708 Nothing -> error ("evalP: lookupUFM " ++ show v)
710 -- Deal with application of a function returning a pointer rep
711 -- to arguments of any persuasion. Note that the function itself
712 -- always has pointer rep.
713 evalP (AppIP e1 e2) de = unsafeCoerce# (evalP e1 de) (evalI e2 de)
714 evalP (AppPP e1 e2) de = unsafeCoerce# (evalP e1 de) (evalP e2 de)
715 evalP (AppFP e1 e2) de = unsafeCoerce# (evalP e1 de) (evalF e2 de)
716 evalP (AppDP e1 e2) de = unsafeCoerce# (evalP e1 de) (evalD e2 de)
718 -- Lambdas always return P-rep, but we need to do different things
719 -- depending on both the argument and result representations.
721 = unsafeCoerce# (\ xP -> evalP b (addToUFM de x xP))
723 = unsafeCoerce# (\ xP -> evalI b (addToUFM de x xP))
725 = unsafeCoerce# (\ xP -> evalF b (addToUFM de x xP))
727 = unsafeCoerce# (\ xP -> evalD b (addToUFM de x xP))
729 = unsafeCoerce# (\ xI -> evalP b (addToUFM de x (unsafeCoerce# (I# xI))))
731 = unsafeCoerce# (\ xI -> evalI b (addToUFM de x (unsafeCoerce# (I# xI))))
733 = unsafeCoerce# (\ xI -> evalF b (addToUFM de x (unsafeCoerce# (I# xI))))
735 = unsafeCoerce# (\ xI -> evalD b (addToUFM de x (unsafeCoerce# (I# xI))))
737 = unsafeCoerce# (\ xI -> evalP b (addToUFM de x (unsafeCoerce# (F# xI))))
739 = unsafeCoerce# (\ xI -> evalI b (addToUFM de x (unsafeCoerce# (F# xI))))
741 = unsafeCoerce# (\ xI -> evalF b (addToUFM de x (unsafeCoerce# (F# xI))))
743 = unsafeCoerce# (\ xI -> evalD b (addToUFM de x (unsafeCoerce# (F# xI))))
745 = unsafeCoerce# (\ xI -> evalP b (addToUFM de x (unsafeCoerce# (D# xI))))
747 = unsafeCoerce# (\ xI -> evalI b (addToUFM de x (unsafeCoerce# (D# xI))))
749 = unsafeCoerce# (\ xI -> evalF b (addToUFM de x (unsafeCoerce# (D# xI))))
751 = unsafeCoerce# (\ xI -> evalD b (addToUFM de x (unsafeCoerce# (D# xI))))
754 -- NonRec, Rec, CaseAlg and CasePrim are the same for all result reps,
755 -- except in the sense that we go on and evaluate the body with whichever
756 -- evaluator was used for the expression as a whole.
757 evalP (NonRecP bind e) de
758 = evalP e (augment_nonrec bind de)
759 evalP (RecP binds b) de
760 = evalP b (augment_rec binds de)
761 evalP (CaseAlgP bndr expr alts def) de
762 = case helper_caseAlg bndr expr alts def de of
763 (rhs, de') -> evalP rhs de'
764 evalP (CasePrimP bndr expr alts def) de
765 = case helper_casePrim bndr expr alts def de of
766 (rhs, de') -> evalP rhs de'
768 evalP (ConApp (A# itbl)) de
769 = mci_make_constr0 itbl
771 evalP (ConAppI (A# itbl) a1) de
772 = case evalI a1 de of i1 -> mci_make_constrI itbl i1
774 evalP (ConAppP (A# itbl) a1) de
775 = evalP (ConAppGen (A# itbl) [a1]) de
776 -- = let p1 = evalP a1 de
777 -- in mci_make_constrP itbl p1
779 evalP (ConAppPP (A# itbl) a1 a2) de
780 = let p1 = evalP a1 de
782 in mci_make_constrPP itbl p1 p2
784 evalP (ConAppGen itbl args) de
785 = let c = case itbl of A# a# -> mci_make_constr a# in
786 c `seq` loop c 1#{-leave room for hdr-} args
788 loop :: a{-closure-} -> Int# -> [LinkedIExpr] -> a
792 RepP -> let c' = setPtrOffClosure c off (evalP a de)
793 in c' `seq` loop c' (off +# 1#) as
794 RepI -> case evalI a de of { i# ->
795 let c' = setIntOffClosure c off i#
796 in c' `seq` loop c' (off +# 1#) as }
797 RepF -> case evalF a de of { f# ->
798 let c' = setFloatOffClosure c off f#
799 in c' `seq` loop c' (off +# 1#) as }
800 RepD -> case evalD a de of { d# ->
801 let c' = setDoubleOffClosure c off d#
802 in c' `seq` loop c' (off +# 2#) as }
804 evalP (PrimOpP IntEqOp [e1,e2]) de = unsafeCoerce# (evalI e1 de ==# evalI e2 de)
806 evalP (PrimOpP primop _) de
807 = error ("evalP: unhandled primop: " ++ showSDoc (ppr primop))
809 = error ("evalP: unhandled case: " ++ showExprTag other)
811 --------------------------------------------------------
812 --- Evaluator for things of Int# representation
813 --------------------------------------------------------
815 -- Evaluate something which has an unboxed Int rep
816 evalI :: LinkedIExpr -> UniqFM boxed -> Int#
820 -- | trace ("evalI: " ++ showExprTag expr) False
821 | trace ("evalI:\n" ++ showSDoc (pprIExpr expr) ++ "\n") False
822 = error "evalI: ?!?!"
825 evalI (LitI i#) de = i#
828 case lookupUFM de v of
829 Just e -> case unsafeCoerce# e of I# i -> i
830 Nothing -> error ("evalI: lookupUFM " ++ show v)
832 -- Deal with application of a function returning an Int# rep
833 -- to arguments of any persuasion. Note that the function itself
834 -- always has pointer rep.
835 evalI (AppII e1 e2) de
836 = unsafeCoerce# (evalP e1 de) (evalI e2 de)
837 evalI (AppPI e1 e2) de
838 = unsafeCoerce# (evalP e1 de) (evalP e2 de)
839 evalI (AppFI e1 e2) de
840 = unsafeCoerce# (evalP e1 de) (evalF e2 de)
841 evalI (AppDI e1 e2) de
842 = unsafeCoerce# (evalP e1 de) (evalD e2 de)
844 -- NonRec, Rec, CaseAlg and CasePrim are the same for all result reps,
845 -- except in the sense that we go on and evaluate the body with whichever
846 -- evaluator was used for the expression as a whole.
847 evalI (NonRecI bind b) de
848 = evalI b (augment_nonrec bind de)
849 evalI (RecI binds b) de
850 = evalI b (augment_rec binds de)
851 evalI (CaseAlgI bndr expr alts def) de
852 = case helper_caseAlg bndr expr alts def de of
853 (rhs, de') -> evalI rhs de'
854 evalI (CasePrimI bndr expr alts def) de
855 = case helper_casePrim bndr expr alts def de of
856 (rhs, de') -> evalI rhs de'
858 -- evalI can't be applied to a lambda term, by defn, since those
861 evalI (PrimOpI IntAddOp [e1,e2]) de = evalI e1 de +# evalI e2 de
862 evalI (PrimOpI IntSubOp [e1,e2]) de = evalI e1 de -# evalI e2 de
863 evalI (PrimOpI DataToTagOp [e1]) de = dataToTag# (evalP e1 de)
865 evalI (PrimOpI primop _) de
866 = error ("evalI: unhandled primop: " ++ showSDoc (ppr primop))
868 --evalI (NonRec (IBind v e) b) de
869 -- = evalI b (augment de v (eval e de))
872 = error ("evalI: unhandled case: " ++ showExprTag other)
874 --------------------------------------------------------
875 --- Evaluator for things of Float# representation
876 --------------------------------------------------------
878 -- Evaluate something which has an unboxed Int rep
879 evalF :: LinkedIExpr -> UniqFM boxed -> Float#
883 -- | trace ("evalF: " ++ showExprTag expr) False
884 | trace ("evalF:\n" ++ showSDoc (pprIExpr expr) ++ "\n") False
885 = error "evalF: ?!?!"
888 evalF (LitF f#) de = f#
891 case lookupUFM de v of
892 Just e -> case unsafeCoerce# e of F# i -> i
893 Nothing -> error ("evalF: lookupUFM " ++ show v)
895 -- Deal with application of a function returning an Int# rep
896 -- to arguments of any persuasion. Note that the function itself
897 -- always has pointer rep.
898 evalF (AppIF e1 e2) de
899 = unsafeCoerce# (evalP e1 de) (evalI e2 de)
900 evalF (AppPF e1 e2) de
901 = unsafeCoerce# (evalP e1 de) (evalP e2 de)
902 evalF (AppFF e1 e2) de
903 = unsafeCoerce# (evalP e1 de) (evalF e2 de)
904 evalF (AppDF e1 e2) de
905 = unsafeCoerce# (evalP e1 de) (evalD e2 de)
907 -- NonRec, Rec, CaseAlg and CasePrim are the same for all result reps,
908 -- except in the sense that we go on and evaluate the body with whichever
909 -- evaluator was used for the expression as a whole.
910 evalF (NonRecF bind b) de
911 = evalF b (augment_nonrec bind de)
912 evalF (RecF binds b) de
913 = evalF b (augment_rec binds de)
914 evalF (CaseAlgF bndr expr alts def) de
915 = case helper_caseAlg bndr expr alts def de of
916 (rhs, de') -> evalF rhs de'
917 evalF (CasePrimF bndr expr alts def) de
918 = case helper_casePrim bndr expr alts def de of
919 (rhs, de') -> evalF rhs de'
921 -- evalF can't be applied to a lambda term, by defn, since those
924 evalF (PrimOpF op _) de
925 = error ("evalF: unhandled primop: " ++ showSDoc (ppr op))
928 = error ("evalF: unhandled case: " ++ showExprTag other)
930 --------------------------------------------------------
931 --- Evaluator for things of Double# representation
932 --------------------------------------------------------
934 -- Evaluate something which has an unboxed Int rep
935 evalD :: LinkedIExpr -> UniqFM boxed -> Double#
939 -- | trace ("evalD: " ++ showExprTag expr) False
940 | trace ("evalD:\n" ++ showSDoc (pprIExpr expr) ++ "\n") False
941 = error "evalD: ?!?!"
944 evalD (LitD d#) de = d#
947 case lookupUFM de v of
948 Just e -> case unsafeCoerce# e of D# i -> i
949 Nothing -> error ("evalD: lookupUFM " ++ show v)
951 -- Deal with application of a function returning an Int# rep
952 -- to arguments of any persuasion. Note that the function itself
953 -- always has pointer rep.
954 evalD (AppID e1 e2) de
955 = unsafeCoerce# (evalP e1 de) (evalI e2 de)
956 evalD (AppPD e1 e2) de
957 = unsafeCoerce# (evalP e1 de) (evalP e2 de)
958 evalD (AppFD e1 e2) de
959 = unsafeCoerce# (evalP e1 de) (evalF e2 de)
960 evalD (AppDD e1 e2) de
961 = unsafeCoerce# (evalP e1 de) (evalD e2 de)
963 -- NonRec, Rec, CaseAlg and CasePrim are the same for all result reps,
964 -- except in the sense that we go on and evaluate the body with whichever
965 -- evaluator was used for the expression as a whole.
966 evalD (NonRecD bind b) de
967 = evalD b (augment_nonrec bind de)
968 evalD (RecD binds b) de
969 = evalD b (augment_rec binds de)
970 evalD (CaseAlgD bndr expr alts def) de
971 = case helper_caseAlg bndr expr alts def de of
972 (rhs, de') -> evalD rhs de'
973 evalD (CasePrimD bndr expr alts def) de
974 = case helper_casePrim bndr expr alts def de of
975 (rhs, de') -> evalD rhs de'
977 -- evalD can't be applied to a lambda term, by defn, since those
980 evalD (PrimOpD op _) de
981 = error ("evalD: unhandled primop: " ++ showSDoc (ppr op))
984 = error ("evalD: unhandled case: " ++ showExprTag other)
986 --------------------------------------------------------
987 --- Helper bits and pieces
988 --------------------------------------------------------
990 -- Find the Rep of any Expr
991 repOf :: LinkedIExpr -> Rep
993 repOf (LamPP _ _) = RepP
994 repOf (LamPI _ _) = RepP
995 repOf (LamPF _ _) = RepP
996 repOf (LamPD _ _) = RepP
997 repOf (LamIP _ _) = RepP
998 repOf (LamII _ _) = RepP
999 repOf (LamIF _ _) = RepP
1000 repOf (LamID _ _) = RepP
1001 repOf (LamFP _ _) = RepP
1002 repOf (LamFI _ _) = RepP
1003 repOf (LamFF _ _) = RepP
1004 repOf (LamFD _ _) = RepP
1005 repOf (LamDP _ _) = RepP
1006 repOf (LamDI _ _) = RepP
1007 repOf (LamDF _ _) = RepP
1008 repOf (LamDD _ _) = RepP
1010 repOf (AppPP _ _) = RepP
1011 repOf (AppPI _ _) = RepI
1012 repOf (AppPF _ _) = RepF
1013 repOf (AppPD _ _) = RepD
1014 repOf (AppIP _ _) = RepP
1015 repOf (AppII _ _) = RepI
1016 repOf (AppIF _ _) = RepF
1017 repOf (AppID _ _) = RepD
1018 repOf (AppFP _ _) = RepP
1019 repOf (AppFI _ _) = RepI
1020 repOf (AppFF _ _) = RepF
1021 repOf (AppFD _ _) = RepD
1022 repOf (AppDP _ _) = RepP
1023 repOf (AppDI _ _) = RepI
1024 repOf (AppDF _ _) = RepF
1025 repOf (AppDD _ _) = RepD
1027 repOf (NonRecP _ _) = RepP
1028 repOf (NonRecI _ _) = RepI
1029 repOf (NonRecF _ _) = RepF
1030 repOf (NonRecD _ _) = RepD
1032 repOf (RecP _ _) = RepP
1033 repOf (RecI _ _) = RepI
1034 repOf (RecF _ _) = RepF
1035 repOf (RecD _ _) = RepD
1037 repOf (LitI _) = RepI
1038 repOf (LitF _) = RepF
1039 repOf (LitD _) = RepD
1041 repOf (Native _) = RepP
1043 repOf (VarP _) = RepP
1044 repOf (VarI _) = RepI
1045 repOf (VarF _) = RepF
1046 repOf (VarD _) = RepD
1048 repOf (PrimOpP _ _) = RepP
1049 repOf (PrimOpI _ _) = RepI
1050 repOf (PrimOpF _ _) = RepF
1051 repOf (PrimOpD _ _) = RepD
1053 repOf (ConApp _) = RepP
1054 repOf (ConAppI _ _) = RepP
1055 repOf (ConAppP _ _) = RepP
1056 repOf (ConAppPP _ _ _) = RepP
1057 repOf (ConAppGen _ _) = RepP
1059 repOf (CaseAlgP _ _ _ _) = RepP
1060 repOf (CaseAlgI _ _ _ _) = RepI
1061 repOf (CaseAlgF _ _ _ _) = RepF
1062 repOf (CaseAlgD _ _ _ _) = RepD
1064 repOf (CasePrimP _ _ _ _) = RepP
1065 repOf (CasePrimI _ _ _ _) = RepI
1066 repOf (CasePrimF _ _ _ _) = RepF
1067 repOf (CasePrimD _ _ _ _) = RepD
1070 = error ("repOf: unhandled case: " ++ showExprTag other)
1072 -- how big (in words) is one of these
1073 repSizeW :: Rep -> Int
1078 -- Evaluate an expression, using the appropriate evaluator,
1079 -- then box up the result. Note that it's only safe to use this
1080 -- to create values to put in the environment. You can't use it
1081 -- to create a value which might get passed to native code since that
1082 -- code will have no idea that unboxed things have been boxed.
1083 eval :: LinkedIExpr -> UniqFM boxed -> boxed
1085 = case repOf expr of
1086 RepI -> unsafeCoerce# (I# (evalI expr de))
1087 RepP -> evalP expr de
1088 RepF -> unsafeCoerce# (F# (evalF expr de))
1089 RepD -> unsafeCoerce# (D# (evalD expr de))
1091 -- Evaluate the scrutinee of a case, select an alternative,
1092 -- augment the environment appropriately, and return the alt
1093 -- and the augmented environment.
1094 helper_caseAlg :: Id -> LinkedIExpr -> [LinkedAltAlg] -> Maybe LinkedIExpr
1096 -> (LinkedIExpr, UniqFM boxed)
1097 helper_caseAlg bndr expr alts def de
1098 = let exprEv = evalP expr de
1100 exprEv `seq` -- vitally important; otherwise exprEv is never eval'd
1101 case select_altAlg (tagOf exprEv) alts def of
1102 (vars,rhs) -> (rhs, augment_from_constr (addToUFM de bndr exprEv)
1105 helper_casePrim :: Var -> LinkedIExpr -> [LinkedAltPrim] -> Maybe LinkedIExpr
1107 -> (LinkedIExpr, UniqFM boxed)
1108 helper_casePrim bndr expr alts def de
1109 = case repOf expr of
1110 RepI -> case evalI expr de of
1111 i# -> (select_altPrim alts def (LitI i#),
1112 addToUFM de bndr (unsafeCoerce# (I# i#)))
1113 RepF -> case evalF expr de of
1114 f# -> (select_altPrim alts def (LitF f#),
1115 addToUFM de bndr (unsafeCoerce# (F# f#)))
1116 RepD -> case evalD expr de of
1117 d# -> (select_altPrim alts def (LitD d#),
1118 addToUFM de bndr (unsafeCoerce# (D# d#)))
1121 augment_from_constr :: UniqFM boxed -> a -> ([(Id,Rep)],Int) -> UniqFM boxed
1122 augment_from_constr de con ([],offset)
1124 augment_from_constr de con ((v,rep):vs,offset)
1127 RepP -> indexPtrOffClosure con offset
1128 RepI -> unsafeCoerce# (I# (indexIntOffClosure con offset))
1129 RepF -> unsafeCoerce# (F# (indexFloatOffClosure con offset))
1130 RepD -> unsafeCoerce# (D# (indexDoubleOffClosure con offset))
1132 augment_from_constr (addToUFM de v v_binding) con
1133 (vs,offset + repSizeW rep)
1135 -- Augment the environment for a non-recursive let.
1136 augment_nonrec :: LinkedIBind -> UniqFM boxed -> UniqFM boxed
1137 augment_nonrec (IBind v e) de = addToUFM de v (eval e de)
1139 -- Augment the environment for a recursive let.
1140 augment_rec :: [LinkedIBind] -> UniqFM boxed -> UniqFM boxed
1141 augment_rec binds de
1142 = let vars = map binder binds
1143 rhss = map bindee binds
1144 rhs_vs = map (\rhs -> eval rhs de') rhss
1145 de' = addListToUFM de (zip vars rhs_vs)
1149 -- a must be a constructor?
1151 tagOf x = I# (dataToTag# x)
1153 select_altAlg :: Int -> [LinkedAltAlg] -> Maybe LinkedIExpr -> ([(Id,Rep)],LinkedIExpr)
1154 select_altAlg tag [] Nothing = error "select_altAlg: no match and no default?!"
1155 select_altAlg tag [] (Just def) = ([],def)
1156 select_altAlg tag ((AltAlg tagNo vars rhs):alts) def
1159 else select_altAlg tag alts def
1161 -- literal may only be a literal, not an arbitrary expression
1162 select_altPrim :: [LinkedAltPrim] -> Maybe LinkedIExpr -> LinkedIExpr -> LinkedIExpr
1163 select_altPrim [] Nothing literal = error "select_altPrim: no match and no default?!"
1164 select_altPrim [] (Just def) literal = def
1165 select_altPrim ((AltPrim lit rhs):alts) def literal
1166 = if eqLits lit literal
1168 else select_altPrim alts def literal
1170 eqLits (LitI i1#) (LitI i2#) = i1# ==# i2#
1172 -- ----------------------------------------------------------------------
1173 -- Grotty inspection and creation of closures
1174 -- ----------------------------------------------------------------------
1176 -- a is a constructor
1177 indexPtrOffClosure :: a -> Int -> b
1178 indexPtrOffClosure con (I# offset)
1179 = case indexPtrOffClosure# con offset of (# x #) -> x
1181 indexIntOffClosure :: a -> Int -> Int#
1182 indexIntOffClosure con (I# offset)
1183 = case wordToInt (W# (indexWordOffClosure# con offset)) of I# i# -> i#
1185 indexFloatOffClosure :: a -> Int -> Float#
1186 indexFloatOffClosure con (I# offset)
1187 = unsafeCoerce# (indexWordOffClosure# con offset)
1188 -- TOCK TOCK TOCK! Those GHC developers are crazy.
1190 indexDoubleOffClosure :: a -> Int -> Double#
1191 indexDoubleOffClosure con (I# offset)
1192 = unsafeCoerce# (panic "indexDoubleOffClosure")
1194 setPtrOffClosure :: a -> Int# -> b -> a
1195 setPtrOffClosure a i b = case setPtrOffClosure# a i b of (# c #) -> c
1197 setIntOffClosure :: a -> Int# -> Int# -> a
1198 setIntOffClosure a i b = case setWordOffClosure# a i (int2Word# b) of (# c #) -> c
1200 setFloatOffClosure :: a -> Int# -> Float# -> a
1201 setFloatOffClosure a i b = case setWordOffClosure# a i (unsafeCoerce# b) of (# c #) -> c
1203 setDoubleOffClosure :: a -> Int# -> Double# -> a
1204 setDoubleOffClosure a i b = unsafeCoerce# (panic "setDoubleOffClosure")
1206 ------------------------------------------------------------------------
1207 --- Manufacturing of info tables for DataCons defined in this module ---
1208 ------------------------------------------------------------------------
1210 #if __GLASGOW_HASKELL__ <= 408
1213 type ItblPtr = Ptr StgInfoTable
1216 -- Make info tables for the data decls in this module
1217 mkITbls :: [TyCon] -> IO ItblEnv
1218 mkITbls [] = return emptyFM
1219 mkITbls (tc:tcs) = do itbls <- mkITbl tc
1220 itbls2 <- mkITbls tcs
1221 return (itbls `plusFM` itbls2)
1223 mkITbl :: TyCon -> IO ItblEnv
1225 -- | trace ("TYCON: " ++ showSDoc (ppr tc)) False
1227 | not (isDataTyCon tc)
1229 | n == length dcs -- paranoia; this is an assertion.
1230 = make_constr_itbls dcs
1232 dcs = tyConDataCons tc
1233 n = tyConFamilySize tc
1236 cONSTR = 1 -- as defined in ghc/includes/ClosureTypes.h
1238 -- Assumes constructors are numbered from zero, not one
1239 make_constr_itbls :: [DataCon] -> IO ItblEnv
1240 make_constr_itbls cons
1242 = do is <- mapM mk_vecret_itbl (zip cons [0..])
1243 return (listToFM is)
1245 = do is <- mapM mk_dirret_itbl (zip cons [0..])
1246 return (listToFM is)
1248 mk_vecret_itbl (dcon, conNo)
1249 = mk_itbl dcon conNo (vecret_entry conNo)
1250 mk_dirret_itbl (dcon, conNo)
1251 = mk_itbl dcon conNo mci_constr_entry
1253 mk_itbl :: DataCon -> Int -> Addr -> IO (Name,ItblPtr)
1254 mk_itbl dcon conNo entry_addr
1255 = let (tot_wds, ptr_wds, _)
1256 = mkVirtHeapOffsets typePrimRep (dataConRepArgTys dcon)
1258 nptrs = tot_wds - ptr_wds
1259 itbl = StgInfoTable {
1260 ptrs = fromIntegral ptrs, nptrs = fromIntegral nptrs,
1261 tipe = fromIntegral cONSTR,
1262 srtlen = fromIntegral conNo,
1263 code0 = fromIntegral code0, code1 = fromIntegral code1,
1264 code2 = fromIntegral code2, code3 = fromIntegral code3,
1265 code4 = fromIntegral code4, code5 = fromIntegral code5,
1266 code6 = fromIntegral code6, code7 = fromIntegral code7
1268 -- Make a piece of code to jump to "entry_label".
1269 -- This is the only arch-dependent bit.
1270 -- On x86, if entry_label has an address 0xWWXXYYZZ,
1271 -- emit movl $0xWWXXYYZZ,%eax ; jmp *%eax
1273 -- B8 ZZ YY XX WW FF E0
1274 (code0,code1,code2,code3,code4,code5,code6,code7)
1275 = (0xB8, byte 0 entry_addr_w, byte 1 entry_addr_w,
1276 byte 2 entry_addr_w, byte 3 entry_addr_w,
1280 entry_addr_w :: Word32
1281 entry_addr_w = fromIntegral (addrToInt entry_addr)
1284 --putStrLn ("SIZE of itbl is " ++ show (sizeOf itbl))
1285 --putStrLn ("# ptrs of itbl is " ++ show ptrs)
1286 --putStrLn ("# nptrs of itbl is " ++ show nptrs)
1288 return (getName dcon, addr `plusPtr` 8)
1291 byte :: Int -> Word32 -> Word32
1292 byte 0 w = w .&. 0xFF
1293 byte 1 w = (w `shiftR` 8) .&. 0xFF
1294 byte 2 w = (w `shiftR` 16) .&. 0xFF
1295 byte 3 w = (w `shiftR` 24) .&. 0xFF
1298 vecret_entry 0 = mci_constr1_entry
1299 vecret_entry 1 = mci_constr2_entry
1300 vecret_entry 2 = mci_constr3_entry
1301 vecret_entry 3 = mci_constr4_entry
1302 vecret_entry 4 = mci_constr5_entry
1303 vecret_entry 5 = mci_constr6_entry
1304 vecret_entry 6 = mci_constr7_entry
1305 vecret_entry 7 = mci_constr8_entry
1307 -- entry point for direct returns for created constr itbls
1308 foreign label "stg_mci_constr_entry" mci_constr_entry :: Addr
1309 -- and the 8 vectored ones
1310 foreign label "stg_mci_constr1_entry" mci_constr1_entry :: Addr
1311 foreign label "stg_mci_constr2_entry" mci_constr2_entry :: Addr
1312 foreign label "stg_mci_constr3_entry" mci_constr3_entry :: Addr
1313 foreign label "stg_mci_constr4_entry" mci_constr4_entry :: Addr
1314 foreign label "stg_mci_constr5_entry" mci_constr5_entry :: Addr
1315 foreign label "stg_mci_constr6_entry" mci_constr6_entry :: Addr
1316 foreign label "stg_mci_constr7_entry" mci_constr7_entry :: Addr
1317 foreign label "stg_mci_constr8_entry" mci_constr8_entry :: Addr
1321 data Constructor = Constructor Int{-ptrs-} Int{-nptrs-}
1324 -- Ultra-minimalist version specially for constructors
1325 data StgInfoTable = StgInfoTable {
1330 code0, code1, code2, code3, code4, code5, code6, code7 :: Word8
1334 instance Storable StgInfoTable where
1337 = (sum . map (\f -> f itbl))
1338 [fieldSz ptrs, fieldSz nptrs, fieldSz srtlen, fieldSz tipe,
1339 fieldSz code0, fieldSz code1, fieldSz code2, fieldSz code3,
1340 fieldSz code4, fieldSz code5, fieldSz code6, fieldSz code7]
1343 = (sum . map (\f -> f itbl))
1344 [fieldAl ptrs, fieldAl nptrs, fieldAl srtlen, fieldAl tipe,
1345 fieldAl code0, fieldAl code1, fieldAl code2, fieldAl code3,
1346 fieldAl code4, fieldAl code5, fieldAl code6, fieldAl code7]
1349 = do a1 <- store (ptrs itbl) (castPtr a0)
1350 a2 <- store (nptrs itbl) a1
1351 a3 <- store (tipe itbl) a2
1352 a4 <- store (srtlen itbl) a3
1353 a5 <- store (code0 itbl) a4
1354 a6 <- store (code1 itbl) a5
1355 a7 <- store (code2 itbl) a6
1356 a8 <- store (code3 itbl) a7
1357 a9 <- store (code4 itbl) a8
1358 aA <- store (code5 itbl) a9
1359 aB <- store (code6 itbl) aA
1360 aC <- store (code7 itbl) aB
1364 = do (a1,ptrs) <- load (castPtr a0)
1365 (a2,nptrs) <- load a1
1366 (a3,tipe) <- load a2
1367 (a4,srtlen) <- load a3
1368 (a5,code0) <- load a4
1369 (a6,code1) <- load a5
1370 (a7,code2) <- load a6
1371 (a8,code3) <- load a7
1372 (a9,code4) <- load a8
1373 (aA,code5) <- load a9
1374 (aB,code6) <- load aA
1375 (aC,code7) <- load aB
1376 return StgInfoTable { ptrs = ptrs, nptrs = nptrs,
1377 srtlen = srtlen, tipe = tipe,
1378 code0 = code0, code1 = code1, code2 = code2,
1379 code3 = code3, code4 = code4, code5 = code5,
1380 code6 = code6, code7 = code7 }
1382 fieldSz :: (Storable a, Storable b) => (a -> b) -> a -> Int
1383 fieldSz sel x = sizeOf (sel x)
1385 fieldAl :: (Storable a, Storable b) => (a -> b) -> a -> Int
1386 fieldAl sel x = alignment (sel x)
1388 store :: Storable a => a -> Ptr a -> IO (Ptr b)
1389 store x addr = do poke addr x
1390 return (castPtr (addr `plusPtr` sizeOf x))
1392 load :: Storable a => Ptr a -> IO (Ptr b, a)
1393 load addr = do x <- peek addr
1394 return (castPtr (addr `plusPtr` sizeOf x), x)
1396 -----------------------------------------------------------------------------q
1398 foreign import "strncpy" strncpy :: Ptr a -> ByteArray# -> CInt -> IO ()