9a452b56b79777cc061f93f2729cec2a57bf448d
[ghc-hetmet.git] / ghc / compiler / ghci / StgInterp.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-2000
3 %
4 \section[StgInterp]{Translates STG syntax to interpretable form, and run it}
5
6 \begin{code}
7
8 module StgInterp ( 
9
10     ClosureEnv, ItblEnv, 
11     filterRdrNameEnv,   -- :: [ModuleName] -> FiniteMap RdrName a 
12                         -- -> FiniteMap RdrName a
13
14     linkIModules,       -- :: ItblEnv -> ClosureEnv
15                         -- -> [([UnlinkedIBind], ItblEnv)]
16                         -- -> IO ([LinkedIBind], ItblEnv, ClosureEnv)
17
18     iExprToHValue,      --  :: ItblEnv -> ClosureEnv 
19                         --  -> UnlinkedIExpr -> HValue
20
21     stgBindsToInterpSyn,-- :: [StgBinding] 
22                         -- -> [TyCon] -> [Class] 
23                         -- -> IO ([UnlinkedIBind], ItblEnv)
24
25     stgExprToInterpSyn, -- :: StgExpr
26                         -- -> IO UnlinkedIExpr
27
28     interp              -- :: LinkedIExpr -> HValue
29  ) where
30
31 {- -----------------------------------------------------------------------------
32
33  ToDo:
34    - link should be in the IO monad, so it can modify the symtabs as it
35      goes along
36  
37    - need a way to remove the bindings for a module from the symtabs. 
38      maybe the symtabs should be indexed by module first.
39
40    - change the representation to something less verbose (?).
41
42    - converting string literals to Addr# is horrible and introduces
43      a memory leak.  See if something can be done about this.
44
45 ----------------------------------------------------------------------------- -}
46
47 #include "HsVersions.h"
48
49 import Linker
50 import Id               ( Id, idPrimRep )
51 import Outputable
52 import Var
53 import PrimOp           ( PrimOp(..) )
54 import PrimRep          ( PrimRep(..) )
55 import Literal          ( Literal(..) )
56 import Type             ( Type, typePrimRep, deNoteType, repType, funResultTy )
57 import DataCon          ( DataCon, dataConTag, dataConRepArgTys )
58 import ClosureInfo      ( mkVirtHeapOffsets )
59 import Module           ( ModuleName )
60 import Name             ( toRdrName )
61 import UniqFM
62 import UniqSet
63
64 import {-# SOURCE #-} MCI_make_constr
65
66 import IOExts           ( unsafePerformIO ) -- ToDo: remove
67 import PrelGHC          --( unsafeCoerce#, dataToTag#,
68                         --  indexPtrOffClosure#, indexWordOffClosure# )
69 import PrelAddr         ( Addr(..) )
70 import PrelFloat        ( Float(..), Double(..) )
71 import Bits
72 import FastString
73 import GlaExts          ( Int(..) )
74 import Module           ( moduleNameFS )
75
76 import TyCon            ( TyCon, isDataTyCon, tyConDataCons, tyConFamilySize )
77 import Class            ( Class, classTyCon )
78 import InterpSyn
79 import StgSyn
80 import Addr
81 import RdrName          ( RdrName, rdrNameModule, rdrNameOcc )
82 import FiniteMap
83 import Panic            ( panic )
84 import OccName          ( occNameString )
85 import ErrUtils         ( showPass, dumpIfSet_dyn )
86 import CmdLineOpts      ( DynFlags, DynFlag(..) )
87
88 import Foreign
89 import CTypes
90 import IO
91
92 -- ---------------------------------------------------------------------------
93 -- Environments needed by the linker
94 -- ---------------------------------------------------------------------------
95
96 type ItblEnv    = FiniteMap RdrName (Ptr StgInfoTable)
97 type ClosureEnv = FiniteMap RdrName HValue
98 emptyClosureEnv = emptyFM
99
100 -- remove all entries for a given set of modules from the environment
101 filterRdrNameEnv :: [ModuleName] -> FiniteMap RdrName a -> FiniteMap RdrName a
102 filterRdrNameEnv mods env 
103    = filterFM (\n _ -> rdrNameModule n `notElem` mods) env
104
105 -- ---------------------------------------------------------------------------
106 -- Turn an UnlinkedIExpr into a value we can run, for the interpreter
107 -- ---------------------------------------------------------------------------
108
109 iExprToHValue :: ItblEnv -> ClosureEnv -> UnlinkedIExpr -> IO HValue
110 iExprToHValue ie ce expr = return (interp (linkIExpr ie ce expr))
111
112 -- ---------------------------------------------------------------------------
113 -- Convert STG to an unlinked interpretable
114 -- ---------------------------------------------------------------------------
115
116 -- visible from outside
117 stgBindsToInterpSyn :: DynFlags
118                     -> [StgBinding] 
119                     -> [TyCon] -> [Class] 
120                     -> IO ([UnlinkedIBind], ItblEnv)
121 stgBindsToInterpSyn dflags binds local_tycons local_classes
122  = do showPass dflags "StgToInterp"
123       let ibinds = concatMap (translateBind emptyUniqSet) binds
124       let tycs   = local_tycons ++ map classTyCon local_classes
125       dumpIfSet_dyn dflags Opt_D_dump_InterpSyn
126          "Convert To InterpSyn" (vcat (map pprIBind ibinds))
127       itblenv <- mkITbls tycs
128       return (ibinds, itblenv)
129
130 stgExprToInterpSyn :: DynFlags
131                    -> StgExpr
132                    -> IO UnlinkedIExpr
133 stgExprToInterpSyn dflags expr
134  = do showPass dflags "StgToInterp"
135       let iexpr = stg2expr emptyUniqSet expr
136       dumpIfSet_dyn dflags Opt_D_dump_InterpSyn
137         "Convert To InterpSyn" (pprIExpr iexpr)
138       return iexpr
139
140 translateBind :: UniqSet Id -> StgBinding -> [UnlinkedIBind]
141 translateBind ie (StgNonRec v e)  = [IBind v (rhs2expr ie e)]
142 translateBind ie (StgRec vs_n_es) = [IBind v (rhs2expr ie' e) | (v,e) <- vs_n_es]
143   where ie' = addListToUniqSet ie (map fst vs_n_es)
144
145 isRec (StgNonRec _ _) = False
146 isRec (StgRec _)      = True
147
148 rhs2expr :: UniqSet Id -> StgRhs -> UnlinkedIExpr
149 rhs2expr ie (StgRhsClosure ccs binfo srt fvs uflag args rhs)
150    = mkLambdas args
151      where
152         rhsExpr = stg2expr (addListToUniqSet ie args) rhs
153         rhsRep  = repOfStgExpr rhs
154         mkLambdas [] = rhsExpr
155         mkLambdas (v:vs) = mkLam (repOfId v) rhsRep v (mkLambdas vs)
156 rhs2expr ie (StgRhsCon ccs dcon args)
157    = conapp2expr ie dcon args
158
159 conapp2expr :: UniqSet Id -> DataCon -> [StgArg] -> UnlinkedIExpr
160 conapp2expr ie dcon args
161    = mkConApp con_rdrname reps exprs
162      where
163         con_rdrname = toRdrName dcon
164         exprs       = map (arg2expr ie) inHeapOrder
165         reps        = map repOfArg inHeapOrder
166         inHeapOrder = toHeapOrder args
167
168         toHeapOrder :: [StgArg] -> [StgArg]
169         toHeapOrder args
170            = let (_, _, rearranged_w_offsets) = mkVirtHeapOffsets getArgPrimRep args
171                  (rearranged, offsets) = unzip rearranged_w_offsets
172              in
173                  rearranged
174
175 foreign label "PrelBase_Izh_con_info" prelbase_Izh_con_info :: Addr
176
177 -- Handle most common cases specially; do the rest with a generic
178 -- mechanism (deferred till later :)
179 mkConApp :: RdrName -> [Rep] -> [UnlinkedIExpr] -> UnlinkedIExpr
180 mkConApp nm []               []         = ConApp    nm
181 mkConApp nm [RepI]           [a1]       = ConAppI   nm a1
182 mkConApp nm [RepP]           [a1]       = ConAppP   nm a1
183 mkConApp nm [RepP,RepP]      [a1,a2]    = ConAppPP  nm a1 a2
184 mkConApp nm [RepP,RepP,RepP] [a1,a2,a3] = ConAppPPP nm a1 a2 a3
185 mkConApp nm reps args
186    = pprPanic "StgInterp.mkConApp: unhandled reps" (hsep (map ppr reps))
187
188 mkLam RepP RepP = LamPP
189 mkLam RepI RepP = LamIP
190 mkLam RepP RepI = LamPI
191 mkLam RepI RepI = LamII
192 mkLam repa repr = pprPanic "StgInterp.mkLam" (ppr repa <+> ppr repr)
193
194 mkApp RepP RepP = AppPP
195 mkApp RepI RepP = AppIP
196 mkApp RepP RepI = AppPI
197 mkApp RepI RepI = AppII
198 mkApp repa repr = pprPanic "StgInterp.mkApp" (ppr repa <+> ppr repr)
199
200 repOfId :: Id -> Rep
201 repOfId = primRep2Rep . idPrimRep
202
203 primRep2Rep primRep
204    = case primRep of
205
206         -- genuine lifted types
207         PtrRep        -> RepP
208
209         -- all these are unboxed, fit into a word, and we assume they
210         -- all have the same call/return convention.
211         IntRep        -> RepI
212         CharRep       -> RepI
213         WordRep       -> RepI
214         AddrRep       -> RepI
215         WeakPtrRep    -> RepI
216         StablePtrRep  -> RepI
217
218         -- these are pretty dodgy: really pointers, but
219         -- we can't let the compiler build thunks with these reps.
220         ForeignObjRep -> RepP
221         StableNameRep -> RepP
222         ThreadIdRep   -> RepP
223         ArrayRep      -> RepP
224         ByteArrayRep  -> RepP
225
226         FloatRep      -> RepF
227         DoubleRep     -> RepD
228
229         other -> pprPanic "primRep2Rep" (ppr other)
230
231 repOfStgExpr :: StgExpr -> Rep
232 repOfStgExpr stgexpr
233    = case stgexpr of
234         StgLit lit 
235            -> repOfLit lit
236         StgCase scrut live liveR bndr srt alts
237            -> case altRhss alts of
238                  (a:_) -> repOfStgExpr a
239                  []    -> panic "repOfStgExpr: no alts"
240         StgApp var []
241            -> repOfId var
242         StgApp var args
243            -> repOfApp ((deNoteType.repType.idType) var) (length args)
244
245         StgPrimApp op args res_ty
246            -> (primRep2Rep.typePrimRep) res_ty
247
248         StgLet binds body -> repOfStgExpr body
249         StgLetNoEscape live liveR binds body -> repOfStgExpr body
250
251         StgConApp con args -> RepP -- by definition
252
253         other 
254            -> pprPanic "repOfStgExpr" (ppr other)
255      where
256         altRhss (StgAlgAlts tycon alts def)
257            = [rhs | (dcon,bndrs,uses,rhs) <- alts] ++ defRhs def
258         altRhss (StgPrimAlts tycon alts def)
259            = [rhs | (lit,rhs) <- alts] ++ defRhs def
260         defRhs StgNoDefault 
261            = []
262         defRhs (StgBindDefault rhs)
263            = [rhs]
264
265         -- returns the Rep of the result of applying ty to n args.
266         repOfApp :: Type -> Int -> Rep
267         repOfApp ty 0 = (primRep2Rep.typePrimRep) ty
268         repOfApp ty n = repOfApp (funResultTy ty) (n-1)
269
270
271
272 repOfLit lit
273    = case lit of
274         MachInt _    -> RepI
275         MachWord _   -> RepI
276         MachAddr _   -> RepI
277         MachChar _   -> RepI
278         MachFloat _  -> RepF
279         MachDouble _ -> RepD
280         MachStr _    -> RepI   -- because it's a ptr outside the heap
281         other -> pprPanic "repOfLit" (ppr lit)
282
283 lit2expr :: Literal -> UnlinkedIExpr
284 lit2expr lit
285    = case lit of
286         MachInt  i   -> case fromIntegral i of I# i -> LitI i
287         MachWord i   -> case fromIntegral i of I# i -> LitI i
288         MachAddr i   -> case fromIntegral i of I# i -> LitI i
289         MachChar i   -> case fromIntegral i of I# i -> LitI i
290         MachFloat f  -> case fromRational f of F# f -> LitF f
291         MachDouble f -> case fromRational f of D# f -> LitD f
292         MachStr s    -> 
293            case s of
294                 CharStr s i -> LitI (addr2Int# s)
295
296                 FastString _ l ba -> 
297                 -- sigh, a string in the heap is no good to us.  We need a 
298                 -- static C pointer, since the type of a string literal is 
299                 -- Addr#.  So, copy the string into C land and introduce a 
300                 -- memory leak at the same time.
301                   let n = I# l in
302                  -- CAREFUL!  Chars are 32 bits in ghc 4.09+
303                   case unsafePerformIO (do a@(Ptr addr) <- mallocBytes (n+1)
304                                            strncpy a ba (fromIntegral n)
305                                            writeCharOffAddr addr n '\0'
306                                            return addr)
307                   of  A# a -> LitI (addr2Int# a)
308
309                 _ -> error "StgInterp.lit2expr: unhandled string constant type"
310
311         other -> pprPanic "lit2expr" (ppr lit)
312
313 stg2expr :: UniqSet Id -> StgExpr -> UnlinkedIExpr
314 stg2expr ie stgexpr
315    = case stgexpr of
316         StgApp var []
317            -> mkVar ie (repOfId var) var
318
319         StgApp var args
320            -> mkAppChain ie (repOfStgExpr stgexpr) (mkVar ie (repOfId var) var) args
321         StgLit lit
322            -> lit2expr lit
323
324         StgCase scrut live liveR bndr srt (StgPrimAlts ty alts def)
325            |  repOfStgExpr scrut /= RepP
326            -> mkCasePrim (repOfStgExpr stgexpr) 
327                          bndr (stg2expr ie scrut) 
328                               (map doPrimAlt alts) 
329                               (def2expr def)
330
331         StgCase scrut live liveR bndr srt (StgAlgAlts tycon alts def)
332            |  repOfStgExpr scrut == RepP
333            -> mkCaseAlg (repOfStgExpr stgexpr) 
334                         bndr (stg2expr ie scrut) 
335                              (map doAlgAlt alts) 
336                              (def2expr def)
337
338         StgPrimApp op args res_ty
339            -> mkPrimOp (repOfStgExpr stgexpr)
340                        op (map (arg2expr ie) args)
341
342         StgConApp dcon args
343            -> conapp2expr ie dcon args
344
345         StgLet binds@(StgNonRec v e) body
346            -> mkNonRec (repOfStgExpr stgexpr) 
347                 (head (translateBind ie binds)) 
348                 (stg2expr (addOneToUniqSet ie v) body)
349
350         StgLet binds@(StgRec bs) body
351            -> mkRec (repOfStgExpr stgexpr) 
352                 (translateBind ie binds) 
353                 (stg2expr (addListToUniqSet ie (map fst bs)) body)
354
355         -- treat let-no-escape just like let.
356         StgLetNoEscape _ _ binds body
357            -> stg2expr ie (StgLet binds body)
358
359         other
360            -> pprPanic "stg2expr" (ppr stgexpr)
361      where
362         doPrimAlt (lit,rhs) 
363            = AltPrim (lit2expr lit) (stg2expr ie rhs)
364         doAlgAlt (dcon,vars,uses,rhs) 
365            = AltAlg (dataConTag dcon - 1) 
366                     (map id2VaaRep (toHeapOrder vars)) 
367                         (stg2expr (addListToUniqSet ie vars) rhs)
368
369         toHeapOrder vars
370            = let (_,_,rearranged_w_offsets) = mkVirtHeapOffsets idPrimRep vars
371                  (rearranged,offsets)       = unzip rearranged_w_offsets
372              in
373                  rearranged
374
375         def2expr StgNoDefault         = Nothing
376         def2expr (StgBindDefault rhs) = Just (stg2expr ie rhs)
377
378         mkAppChain ie result_rep so_far []
379            = panic "mkAppChain"
380         mkAppChain ie result_rep so_far [a]
381            = mkApp (repOfArg a) result_rep so_far (arg2expr ie a)
382         mkAppChain ie result_rep so_far (a:as)
383            = mkAppChain ie result_rep (mkApp (repOfArg a) RepP so_far (arg2expr ie a)) as
384
385 mkCasePrim RepI = CasePrimI
386 mkCasePrim RepP = CasePrimP
387
388 mkCaseAlg  RepI = CaseAlgI
389 mkCaseAlg  RepP = CaseAlgP
390
391 -- any var that isn't in scope is turned into a Native
392 mkVar ie rep var
393   | var `elementOfUniqSet` ie = 
394         (case rep of
395            RepI -> VarI
396            RepF -> VarF
397            RepD -> VarD
398            RepP -> VarP)  var
399   | otherwise = Native (toRdrName var)
400
401 mkRec RepI = RecI
402 mkRec RepP = RecP
403 mkNonRec RepI = NonRecI
404 mkNonRec RepP = NonRecP
405
406 mkPrimOp RepI = PrimOpI
407 mkPrimOp RepP = PrimOpP        
408
409 arg2expr :: UniqSet Id -> StgArg -> UnlinkedIExpr
410 arg2expr ie (StgVarArg v)   = mkVar ie (repOfId v) v
411 arg2expr ie (StgLitArg lit) = lit2expr lit
412 arg2expr ie (StgTypeArg ty) = pprPanic "arg2expr" (ppr ty)
413
414 repOfArg :: StgArg -> Rep
415 repOfArg (StgVarArg v)   = repOfId v
416 repOfArg (StgLitArg lit) = repOfLit lit
417 repOfArg (StgTypeArg ty) = pprPanic "repOfArg" (ppr ty)
418
419 id2VaaRep var = (var, repOfId var)
420
421
422 -- ---------------------------------------------------------------------------
423 -- Link interpretables into something we can run
424 -- ---------------------------------------------------------------------------
425
426 linkIModules :: ItblEnv    -- incoming global itbl env; returned updated
427              -> ClosureEnv -- incoming global closure env; returned updated
428              -> [([UnlinkedIBind], ItblEnv)]
429              -> IO ([LinkedIBind], ItblEnv, ClosureEnv)
430 linkIModules gie gce mods = do
431   let (bindss, ies) = unzip mods
432       binds  = concat bindss
433       top_level_binders = map (toRdrName.binder) binds
434       final_gie = foldr plusFM gie ies
435   
436   let {-rec-}
437       new_gce = addListToFM gce (zip top_level_binders new_rhss)
438       new_rhss = map (\b -> evalP (bindee b) emptyUFM) new_binds
439     --vvvvvvvvv----------------------------------------^^^^^^^^^-- circular
440       new_binds = linkIBinds final_gie new_gce binds
441
442   return (new_binds, final_gie, new_gce)
443
444
445 -- We're supposed to augment the environments with the values of any
446 -- external functions/info tables we need as we go along, but that's a
447 -- lot of hassle so for now I'll look up external things as they crop
448 -- up and not cache them in the source symbol tables.  The interpreted
449 -- code will still be referenced in the source symbol tables.
450
451 linkIBinds :: ItblEnv -> ClosureEnv -> [UnlinkedIBind] -> [LinkedIBind]
452 linkIBinds ie ce binds = map (linkIBind ie ce) binds
453
454 linkIBind ie ce (IBind bndr expr) = IBind bndr (linkIExpr ie ce expr)
455
456 linkIExpr :: ItblEnv -> ClosureEnv -> UnlinkedIExpr -> LinkedIExpr
457 linkIExpr ie ce expr = case expr of
458
459    CaseAlgP  bndr expr alts dflt -> 
460         CaseAlgP bndr (linkIExpr ie ce expr) (linkAlgAlts ie ce alts)
461                         (linkDefault ie ce dflt)
462
463    CaseAlgI  bndr expr alts dflt ->
464         CaseAlgI bndr (linkIExpr ie ce expr) (linkAlgAlts ie ce alts)
465                         (linkDefault ie ce dflt)
466
467    CasePrimP bndr expr alts dflt ->
468         CasePrimP bndr (linkIExpr ie ce expr) (linkPrimAlts ie ce alts)
469                         (linkDefault ie ce dflt)
470
471    CasePrimI bndr expr alts dflt ->
472         CasePrimI bndr (linkIExpr ie ce expr) (linkPrimAlts ie ce alts)
473                         (linkDefault ie ce dflt)
474    
475    ConApp con -> 
476         ConApp (lookupCon ie con)
477
478    ConAppI   con arg0 -> 
479         ConAppI (lookupCon ie con) (linkIExpr ie ce arg0)
480
481    ConAppP   con arg0 ->
482         ConAppP (lookupCon ie con) (linkIExpr ie ce arg0)
483
484    ConAppPP  con arg0 arg1 -> 
485         ConAppPP (lookupCon ie con) (linkIExpr ie ce arg0) (linkIExpr ie ce arg1)
486
487    ConAppPPP con arg0 arg1 arg2 -> 
488         ConAppPPP (lookupCon ie con) (linkIExpr ie ce arg0) 
489                         (linkIExpr ie ce arg1) (linkIExpr ie ce arg2)
490    
491    PrimOpI op args -> PrimOpI op (map (linkIExpr ie ce) args)
492    PrimOpP op args -> PrimOpP op (map (linkIExpr ie ce) args)
493    
494    NonRecP bind expr  -> NonRecP (linkIBind ie ce bind) (linkIExpr ie ce expr)
495    RecP    binds expr -> RecP (linkIBinds ie ce binds) (linkIExpr ie ce expr)
496    
497    NonRecI bind expr  -> NonRecI (linkIBind ie ce bind) (linkIExpr ie ce expr)
498    RecI    binds expr -> RecI (linkIBinds ie ce binds) (linkIExpr ie ce expr)
499    
500    LitI i -> LitI i
501    LitF i -> LitF i
502    LitD i -> LitD i
503
504    Native var -> lookupNative ce var
505    
506    VarP v -> lookupVar ce VarP v
507    VarI v -> lookupVar ce VarI v
508    
509    LamPP  bndr expr -> LamPP bndr (linkIExpr ie ce expr)
510    LamPI  bndr expr -> LamPI bndr (linkIExpr ie ce expr)
511    LamIP  bndr expr -> LamIP bndr (linkIExpr ie ce expr)
512    LamII  bndr expr -> LamII bndr (linkIExpr ie ce expr)
513    
514    AppPP  fun arg -> AppPP (linkIExpr ie ce fun) (linkIExpr ie ce arg)
515    AppPI  fun arg -> AppPI (linkIExpr ie ce fun) (linkIExpr ie ce arg)
516    AppIP  fun arg -> AppIP (linkIExpr ie ce fun) (linkIExpr ie ce arg)
517    AppII  fun arg -> AppII (linkIExpr ie ce fun) (linkIExpr ie ce arg)
518
519 lookupCon ie con = 
520   case lookupFM ie con of
521     Just (Ptr addr) -> addr
522     Nothing   -> 
523         -- try looking up in the object files.
524         case {-HACK!!!-}
525                 unsafePerformIO (lookupSymbol (rdrNameToCLabel con "con_info")) of
526             Just addr -> addr
527             Nothing   -> pprPanic "linkIExpr" (ppr con)
528
529 lookupNative ce var =
530   case lookupFM ce var of
531     Just e  -> Native e
532     Nothing -> 
533         -- try looking up in the object files.
534         let lbl = (rdrNameToCLabel var "closure")
535             addr = unsafePerformIO (lookupSymbol lbl) in
536         case {- trace (lbl ++ " -> " ++ show addr) $ -} addr of
537             Just (A# addr) -> Native (unsafeCoerce# addr)
538             Nothing   -> pprPanic "linkIExpr" (ppr var)
539
540 -- some VarI/VarP refer to top-level interpreted functions; we change
541 -- them into Natives here.
542 lookupVar ce f v =
543   case lookupFM ce (toRdrName v) of
544         Nothing -> f v
545         Just e  -> Native e
546
547 -- HACK!!!  ToDo: cleaner
548 rdrNameToCLabel :: RdrName -> String{-suffix-} -> String
549 rdrNameToCLabel rn suffix = 
550   _UNPK_(moduleNameFS (rdrNameModule rn)) 
551   ++ '_':occNameString(rdrNameOcc rn) ++ '_':suffix
552
553 linkAlgAlts ie ce = map (linkAlgAlt ie ce)
554 linkAlgAlt ie ce (AltAlg tag args rhs) = AltAlg tag args (linkIExpr ie ce rhs)
555
556 linkPrimAlts ie ce = map (linkPrimAlt ie ce)
557 linkPrimAlt ie ce (AltPrim lit rhs)
558    = AltPrim (linkIExpr ie ce lit) (linkIExpr ie ce rhs)
559
560 linkDefault ie ce Nothing = Nothing
561 linkDefault ie ce (Just expr) = Just (linkIExpr ie ce expr)
562
563 -- ---------------------------------------------------------------------------
564 -- The interpreter proper
565 -- ---------------------------------------------------------------------------
566
567 -- The dynamic environment contains everything boxed.
568 -- eval* functions which look up values in it will know the
569 -- representation of the thing they are looking up, so they
570 -- can cast/unbox it as necessary.
571
572 -- ---------------------------------------------------------------------------
573 -- Evaluator for things of boxed (pointer) representation
574 -- ---------------------------------------------------------------------------
575
576 interp :: LinkedIExpr -> HValue
577 interp iexpr = unsafeCoerce# (evalP iexpr emptyUFM)
578
579 evalP :: LinkedIExpr -> UniqFM boxed -> boxed
580
581 {-
582 evalP expr de
583 --   | trace ("evalP: " ++ showExprTag expr) False
584    | trace ("evalP:\n" ++ showSDoc (pprIExpr expr) ++ "\n") False
585    = error "evalP: ?!?!"
586 -}
587
588 evalP (Native p) de  = unsafeCoerce# p
589
590 -- First try the dynamic env.  If that fails, assume it's a top-level
591 -- binding and look in the static env.  That gives an Expr, which we
592 -- must convert to a boxed thingy by applying evalP to it.  Because
593 -- top-level bindings are always ptr-rep'd (either lambdas or boxed
594 -- CAFs), it's always safe to use evalP.
595 evalP (VarP v) de 
596    = case lookupUFM de v of
597         Just xx -> xx
598         Nothing -> error ("evalP: lookupUFM " ++ show v)
599
600 -- Deal with application of a function returning a pointer rep
601 -- to arguments of any persuasion.  Note that the function itself
602 -- always has pointer rep.
603 evalP (AppIP e1 e2) de  = unsafeCoerce# (evalP e1 de) (evalI e2 de)
604 evalP (AppPP e1 e2) de  = unsafeCoerce# (evalP e1 de) (evalP e2 de)
605 evalP (AppFP e1 e2) de  = unsafeCoerce# (evalP e1 de) (evalF e2 de)
606 evalP (AppDP e1 e2) de  = unsafeCoerce# (evalP e1 de) (evalD e2 de)
607
608 -- Lambdas always return P-rep, but we need to do different things
609 -- depending on both the argument and result representations.
610 evalP (LamPP x b) de
611    = unsafeCoerce# (\ xP -> evalP b (addToUFM de x xP))
612 evalP (LamPI x b) de
613    = unsafeCoerce# (\ xP -> evalI b (addToUFM de x xP))
614 evalP (LamPF x b) de
615    = unsafeCoerce# (\ xP -> evalF b (addToUFM de x xP))
616 evalP (LamPD x b) de
617    = unsafeCoerce# (\ xP -> evalD b (addToUFM de x xP))
618 evalP (LamIP x b) de
619    = unsafeCoerce# (\ xI -> evalP b (addToUFM de x (unsafeCoerce# (I# xI))))
620 evalP (LamII x b) de
621    = unsafeCoerce# (\ xI -> evalI b (addToUFM de x (unsafeCoerce# (I# xI))))
622 evalP (LamIF x b) de
623    = unsafeCoerce# (\ xI -> evalF b (addToUFM de x (unsafeCoerce# (I# xI))))
624 evalP (LamID x b) de
625    = unsafeCoerce# (\ xI -> evalD b (addToUFM de x (unsafeCoerce# (I# xI))))
626 evalP (LamFP x b) de
627    = unsafeCoerce# (\ xI -> evalP b (addToUFM de x (unsafeCoerce# (F# xI))))
628 evalP (LamFI x b) de
629    = unsafeCoerce# (\ xI -> evalI b (addToUFM de x (unsafeCoerce# (F# xI))))
630 evalP (LamFF x b) de
631    = unsafeCoerce# (\ xI -> evalF b (addToUFM de x (unsafeCoerce# (F# xI))))
632 evalP (LamFD x b) de
633    = unsafeCoerce# (\ xI -> evalD b (addToUFM de x (unsafeCoerce# (F# xI))))
634 evalP (LamDP x b) de
635    = unsafeCoerce# (\ xI -> evalP b (addToUFM de x (unsafeCoerce# (D# xI))))
636 evalP (LamDI x b) de
637    = unsafeCoerce# (\ xI -> evalI b (addToUFM de x (unsafeCoerce# (D# xI))))
638 evalP (LamDF x b) de
639    = unsafeCoerce# (\ xI -> evalF b (addToUFM de x (unsafeCoerce# (D# xI))))
640 evalP (LamDD x b) de
641    = unsafeCoerce# (\ xI -> evalD b (addToUFM de x (unsafeCoerce# (D# xI))))
642
643
644 -- NonRec, Rec, CaseAlg and CasePrim are the same for all result reps, 
645 -- except in the sense that we go on and evaluate the body with whichever
646 -- evaluator was used for the expression as a whole.
647 evalP (NonRecP bind e) de
648    = evalP e (augment_nonrec bind de)
649 evalP (RecP binds b) de
650    = evalP b (augment_rec binds de)
651 evalP (CaseAlgP bndr expr alts def) de
652    = case helper_caseAlg bndr expr alts def de of
653         (rhs, de') -> evalP rhs de'
654 evalP (CasePrimP bndr expr alts def) de
655    = case helper_casePrim bndr expr alts def de of
656         (rhs, de') -> evalP rhs de'
657
658 {-
659 -- ConApp can only be handled by evalP
660 evalP (ConApp itbl args) se de
661    = loop args
662      where
663         -- This appalling hack suggested (gleefully) by SDM
664         -- It is not well typed (needless to say?)
665         loop :: [Expr] -> boxed
666         loop [] 
667            = trace "loop-empty" (
668              case itbl of A# addr# -> unsafeCoerce# (mci_make_constr addr#)
669              )
670         loop (a:as) 
671            = trace "loop-not-empty" (
672              case repOf a of
673                 RepI -> case evalI a de of i# -> loop as i#
674                 RepP -> let p = evalP a de in loop as p                
675              )
676 -}
677
678 evalP (ConAppI (A# itbl) a1) de
679    = case evalI a1 de of i1 -> mci_make_constrI itbl i1
680
681 evalP (ConApp (A# itbl)) de
682    = mci_make_constr itbl
683
684 evalP (ConAppP (A# itbl) a1) de
685    = let p1 = evalP a1 de
686      in  mci_make_constrP itbl p1
687
688 evalP (ConAppPP (A# itbl) a1 a2) de
689    = let p1 = evalP a1 de
690          p2 = evalP a2 de
691      in  mci_make_constrPP itbl p1 p2
692
693 evalP (ConAppPPP (A# itbl) a1 a2 a3) de
694    = let p1 = evalP a1 de
695          p2 = evalP a2 de
696          p3 = evalP a3 de
697      in  mci_make_constrPPP itbl p1 p2 p3
698
699
700
701 evalP other de
702    = error ("evalP: unhandled case: " ++ showExprTag other)
703
704 --------------------------------------------------------
705 --- Evaluator for things of Int# representation
706 --------------------------------------------------------
707
708 -- Evaluate something which has an unboxed Int rep
709 evalI :: LinkedIExpr -> UniqFM boxed -> Int#
710
711 {-
712 evalI expr de
713 --   | trace ("evalI: " ++ showExprTag expr) False
714    | trace ("evalI:\n" ++ showSDoc (pprIExpr expr) ++ "\n") False
715    = error "evalI: ?!?!"
716 -}
717
718 evalI (LitI i#) de = i#
719
720 evalI (VarI v) de = 
721    case lookupUFM de v of
722         Just e  -> case unsafeCoerce# e of I# i -> i
723         Nothing -> error ("evalI: lookupUFM " ++ show v)
724
725 -- Deal with application of a function returning an Int# rep
726 -- to arguments of any persuasion.  Note that the function itself
727 -- always has pointer rep.
728 evalI (AppII e1 e2) de 
729    = unsafeCoerce# (evalP e1 de) (evalI e2 de)
730 evalI (AppPI e1 e2) de
731    = unsafeCoerce# (evalP e1 de) (evalP e2 de)
732 evalI (AppFI e1 e2) de 
733    = unsafeCoerce# (evalP e1 de) (evalF e2 de)
734 evalI (AppDI e1 e2) de
735    = unsafeCoerce# (evalP e1 de) (evalD e2 de)
736
737 -- NonRec, Rec, CaseAlg and CasePrim are the same for all result reps, 
738 -- except in the sense that we go on and evaluate the body with whichever
739 -- evaluator was used for the expression as a whole.
740 evalI (NonRecI bind b) de
741    = evalI b (augment_nonrec bind de)
742 evalI (RecI binds b) de
743    = evalI b (augment_rec binds de)
744 evalI (CaseAlgI bndr expr alts def) de
745    = case helper_caseAlg bndr expr alts def de of
746         (rhs, de') -> evalI rhs de'
747 evalI (CasePrimI bndr expr alts def) de
748    = case helper_casePrim bndr expr alts def de of
749         (rhs, de') -> evalI rhs de'
750
751 -- evalI can't be applied to a lambda term, by defn, since those
752 -- are ptr-rep'd.
753
754 evalI (PrimOpI IntAddOp [e1,e2]) de  = evalI e1 de +# evalI e2 de
755 evalI (PrimOpI IntSubOp [e1,e2]) de  = evalI e1 de -# evalI e2 de
756
757 --evalI (NonRec (IBind v e) b) de
758 --   = evalI b (augment de v (eval e de))
759
760 evalI other de
761    = error ("evalI: unhandled case: " ++ showExprTag other)
762
763 --------------------------------------------------------
764 --- Evaluator for things of Float# representation
765 --------------------------------------------------------
766
767 -- Evaluate something which has an unboxed Int rep
768 evalF :: LinkedIExpr -> UniqFM boxed -> Float#
769
770 {-
771 evalF expr de
772 --   | trace ("evalF: " ++ showExprTag expr) False
773    | trace ("evalF:\n" ++ showSDoc (pprIExpr expr) ++ "\n") False
774    = error "evalF: ?!?!"
775 -}
776
777 evalF (LitF f#) de = f#
778
779 evalF (VarF v) de = 
780    case lookupUFM de v of
781         Just e  -> case unsafeCoerce# e of F# i -> i
782         Nothing -> error ("evalF: lookupUFM " ++ show v)
783
784 -- Deal with application of a function returning an Int# rep
785 -- to arguments of any persuasion.  Note that the function itself
786 -- always has pointer rep.
787 evalF (AppIF e1 e2) de 
788    = unsafeCoerce# (evalP e1 de) (evalI e2 de)
789 evalF (AppPF e1 e2) de
790    = unsafeCoerce# (evalP e1 de) (evalP e2 de)
791 evalF (AppFF e1 e2) de 
792    = unsafeCoerce# (evalP e1 de) (evalF e2 de)
793 evalF (AppDF e1 e2) de
794    = unsafeCoerce# (evalP e1 de) (evalD e2 de)
795
796 -- NonRec, Rec, CaseAlg and CasePrim are the same for all result reps, 
797 -- except in the sense that we go on and evaluate the body with whichever
798 -- evaluator was used for the expression as a whole.
799 evalF (NonRecF bind b) de
800    = evalF b (augment_nonrec bind de)
801 evalF (RecF binds b) de
802    = evalF b (augment_rec binds de)
803 evalF (CaseAlgF bndr expr alts def) de
804    = case helper_caseAlg bndr expr alts def de of
805         (rhs, de') -> evalF rhs de'
806 evalF (CasePrimF bndr expr alts def) de
807    = case helper_casePrim bndr expr alts def de of
808         (rhs, de') -> evalF rhs de'
809
810 -- evalF can't be applied to a lambda term, by defn, since those
811 -- are ptr-rep'd.
812
813 evalF (PrimOpF op _) de 
814   = error ("evalF: unhandled primop: " ++ showSDoc (ppr op))
815
816 evalF other de
817   = error ("evalF: unhandled case: " ++ showExprTag other)
818
819 --------------------------------------------------------
820 --- Evaluator for things of Double# representation
821 --------------------------------------------------------
822
823 -- Evaluate something which has an unboxed Int rep
824 evalD :: LinkedIExpr -> UniqFM boxed -> Double#
825
826 {-
827 evalD expr de
828 --   | trace ("evalD: " ++ showExprTag expr) False
829    | trace ("evalD:\n" ++ showSDoc (pprIExpr expr) ++ "\n") False
830    = error "evalD: ?!?!"
831 -}
832
833 evalD (LitD d#) de = d#
834
835 evalD (VarD v) de = 
836    case lookupUFM de v of
837         Just e  -> case unsafeCoerce# e of D# i -> i
838         Nothing -> error ("evalD: lookupUFM " ++ show v)
839
840 -- Deal with application of a function returning an Int# rep
841 -- to arguments of any persuasion.  Note that the function itself
842 -- always has pointer rep.
843 evalD (AppID e1 e2) de 
844    = unsafeCoerce# (evalP e1 de) (evalI e2 de)
845 evalD (AppPD e1 e2) de
846    = unsafeCoerce# (evalP e1 de) (evalP e2 de)
847 evalD (AppFD e1 e2) de 
848    = unsafeCoerce# (evalP e1 de) (evalF e2 de)
849 evalD (AppDD e1 e2) de
850    = unsafeCoerce# (evalP e1 de) (evalD e2 de)
851
852 -- NonRec, Rec, CaseAlg and CasePrim are the same for all result reps, 
853 -- except in the sense that we go on and evaluate the body with whichever
854 -- evaluator was used for the expression as a whole.
855 evalD (NonRecD bind b) de
856    = evalD b (augment_nonrec bind de)
857 evalD (RecD binds b) de
858    = evalD b (augment_rec binds de)
859 evalD (CaseAlgD bndr expr alts def) de
860    = case helper_caseAlg bndr expr alts def de of
861         (rhs, de') -> evalD rhs de'
862 evalD (CasePrimD bndr expr alts def) de
863    = case helper_casePrim bndr expr alts def de of
864         (rhs, de') -> evalD rhs de'
865
866 -- evalD can't be applied to a lambda term, by defn, since those
867 -- are ptr-rep'd.
868
869 evalD (PrimOpD op _) de
870   = error ("evalD: unhandled primop: " ++ showSDoc (ppr op))
871
872 evalD other de 
873   = error ("evalD: unhandled case: " ++ showExprTag other)
874
875 --------------------------------------------------------
876 --- Helper bits and pieces
877 --------------------------------------------------------
878
879 -- Find the Rep of any Expr
880 repOf :: LinkedIExpr -> Rep
881
882 repOf (LamPP _ _)      = RepP 
883 repOf (LamPI _ _)      = RepP 
884 repOf (LamPF _ _)      = RepP 
885 repOf (LamPD _ _)      = RepP 
886 repOf (LamIP _ _)      = RepP 
887 repOf (LamII _ _)      = RepP 
888 repOf (LamIF _ _)      = RepP 
889 repOf (LamID _ _)      = RepP 
890 repOf (LamFP _ _)      = RepP 
891 repOf (LamFI _ _)      = RepP 
892 repOf (LamFF _ _)      = RepP 
893 repOf (LamFD _ _)      = RepP 
894 repOf (LamDP _ _)      = RepP 
895 repOf (LamDI _ _)      = RepP 
896 repOf (LamDF _ _)      = RepP 
897 repOf (LamDD _ _)      = RepP 
898
899 repOf (AppPP _ _)      = RepP
900 repOf (AppPI _ _)      = RepI
901 repOf (AppPF _ _)      = RepF
902 repOf (AppPD _ _)      = RepD
903 repOf (AppIP _ _)      = RepP
904 repOf (AppII _ _)      = RepI
905 repOf (AppIF _ _)      = RepF
906 repOf (AppID _ _)      = RepD
907 repOf (AppFP _ _)      = RepP
908 repOf (AppFI _ _)      = RepI
909 repOf (AppFF _ _)      = RepF
910 repOf (AppFD _ _)      = RepD
911 repOf (AppDP _ _)      = RepP
912 repOf (AppDI _ _)      = RepI
913 repOf (AppDF _ _)      = RepF
914 repOf (AppDD _ _)      = RepD
915
916 repOf (NonRecP _ _)    = RepP
917 repOf (NonRecI _ _)    = RepI
918 repOf (NonRecF _ _)    = RepF
919 repOf (NonRecD _ _)    = RepD
920
921 repOf (RecP _ _)       = RepP
922 repOf (RecI _ _)       = RepI
923 repOf (RecF _ _)       = RepF
924 repOf (RecD _ _)       = RepD
925
926 repOf (LitI _)         = RepI
927 repOf (LitF _)         = RepF
928 repOf (LitD _)         = RepD
929
930 repOf (Native _)       = RepP
931
932 repOf (VarP _)         = RepP
933 repOf (VarI _)         = RepI
934 repOf (VarF _)         = RepF
935 repOf (VarD _)         = RepD
936
937 repOf (PrimOpP _ _)    = RepP
938 repOf (PrimOpI _ _)    = RepI
939 repOf (PrimOpF _ _)    = RepF
940 repOf (PrimOpD _ _)    = RepD
941
942 repOf (ConApp _)       = RepP
943 repOf (ConAppI _ _)    = RepP
944 repOf (ConAppP _ _)    = RepP
945 repOf (ConAppPP _ _ _) = RepP
946 repOf (ConAppPPP _ _ _ _) = RepP
947
948 repOf (CaseAlgP _ _ _ _) = RepP
949 repOf (CaseAlgI _ _ _ _) = RepI
950 repOf (CaseAlgF _ _ _ _) = RepF
951 repOf (CaseAlgD _ _ _ _) = RepD
952
953 repOf (CasePrimP _ _ _ _) = RepP
954 repOf (CasePrimI _ _ _ _) = RepI
955 repOf (CasePrimF _ _ _ _) = RepF
956 repOf (CasePrimD _ _ _ _) = RepD
957
958 repOf other         
959    = error ("repOf: unhandled case: " ++ showExprTag other)
960
961 -- how big (in words) is one of these
962 repSizeW :: Rep -> Int
963 repSizeW RepI = 1
964 repSizeW RepP = 1
965
966
967 -- Evaluate an expression, using the appropriate evaluator,
968 -- then box up the result.  Note that it's only safe to use this 
969 -- to create values to put in the environment.  You can't use it 
970 -- to create a value which might get passed to native code since that
971 -- code will have no idea that unboxed things have been boxed.
972 eval :: LinkedIExpr -> UniqFM boxed -> boxed
973 eval expr de
974    = case repOf expr of
975         RepI -> unsafeCoerce# (I# (evalI expr de))
976         RepP -> evalP expr de
977         RepF -> unsafeCoerce# (F# (evalF expr de))
978         RepD -> unsafeCoerce# (D# (evalD expr de))
979
980 -- Evaluate the scrutinee of a case, select an alternative,
981 -- augment the environment appropriately, and return the alt
982 -- and the augmented environment.
983 helper_caseAlg :: Id -> LinkedIExpr -> [LinkedAltAlg] -> Maybe LinkedIExpr 
984                   -> UniqFM boxed
985                   -> (LinkedIExpr, UniqFM boxed)
986 helper_caseAlg bndr expr alts def de
987    = let exprEv = evalP expr de
988      in  
989      exprEv `seq` -- vitally important; otherwise exprEv is never eval'd
990      case select_altAlg (tagOf exprEv) alts def of
991         (vars,rhs) -> (rhs, augment_from_constr (addToUFM de bndr exprEv) 
992                                                 exprEv (vars,1))
993
994 helper_casePrim :: Var -> LinkedIExpr -> [LinkedAltPrim] -> Maybe LinkedIExpr 
995                    -> UniqFM boxed
996                    -> (LinkedIExpr, UniqFM boxed)
997 helper_casePrim bndr expr alts def de
998    = case repOf expr of
999         -- Umm, can expr have any other rep?  Yes ...
1000         -- CharRep, DoubleRep, FloatRep.  What about string reps?
1001         RepI -> case evalI expr de of 
1002                    i# -> (select_altPrim alts def (LitI i#), 
1003                           addToUFM de bndr (unsafeCoerce# (I# i#)))
1004
1005
1006 augment_from_constr :: UniqFM boxed -> a -> ([(Id,Rep)],Int) -> UniqFM boxed
1007 augment_from_constr de con ([],offset) 
1008    = de
1009 augment_from_constr de con ((v,rep):vs,offset)
1010    = let v_binding
1011             = case rep of
1012                  RepP -> indexPtrOffClosure con offset
1013                  RepI -> unsafeCoerce# (I# (indexIntOffClosure con offset))
1014      in
1015          augment_from_constr (addToUFM de v v_binding) con 
1016                              (vs,offset + repSizeW rep)
1017
1018 -- Augment the environment for a non-recursive let.
1019 augment_nonrec :: LinkedIBind -> UniqFM boxed -> UniqFM boxed
1020 augment_nonrec (IBind v e) de  = addToUFM de v (eval e de)
1021
1022 -- Augment the environment for a recursive let.
1023 augment_rec :: [LinkedIBind] -> UniqFM boxed -> UniqFM boxed
1024 augment_rec binds de
1025    = let vars   = map binder binds
1026          rhss   = map bindee binds
1027          rhs_vs = map (\rhs -> eval rhs de') rhss
1028          de'    = addListToUFM de (zip vars rhs_vs)
1029      in
1030          de'
1031
1032 -- a must be a constructor?
1033 tagOf :: a -> Int
1034 tagOf x = I# (dataToTag# x)
1035
1036 select_altAlg :: Int -> [LinkedAltAlg] -> Maybe LinkedIExpr -> ([(Id,Rep)],LinkedIExpr)
1037 select_altAlg tag [] Nothing = error "select_altAlg: no match and no default?!"
1038 select_altAlg tag [] (Just def) = ([],def)
1039 select_altAlg tag ((AltAlg tagNo vars rhs):alts) def
1040    = if   tag == tagNo 
1041      then (vars,rhs) 
1042      else select_altAlg tag alts def
1043
1044 -- literal may only be a literal, not an arbitrary expression
1045 select_altPrim :: [LinkedAltPrim] -> Maybe LinkedIExpr -> LinkedIExpr -> LinkedIExpr
1046 select_altPrim [] Nothing    literal = error "select_altPrim: no match and no default?!"
1047 select_altPrim [] (Just def) literal = def
1048 select_altPrim ((AltPrim lit rhs):alts) def literal
1049    = if eqLits lit literal
1050      then rhs
1051      else select_altPrim alts def literal
1052
1053 eqLits (LitI i1#) (LitI i2#) = i1# ==# i2#
1054
1055
1056 -- a is a constructor
1057 indexPtrOffClosure :: a -> Int -> b
1058 indexPtrOffClosure con (I# offset)
1059    = case indexPtrOffClosure# con offset of (# x #) -> x
1060
1061 indexIntOffClosure :: a -> Int -> Int#
1062 indexIntOffClosure con (I# offset)
1063    = case wordToInt (W# (indexWordOffClosure# con offset)) of I# i# -> i#
1064
1065
1066 ------------------------------------------------------------------------
1067 --- Manufacturing of info tables for DataCons defined in this module ---
1068 ------------------------------------------------------------------------
1069
1070 #if __GLASGOW_HASKELL__ <= 408
1071 type ItblPtr = Addr
1072 #else
1073 type ItblPtr = Ptr StgInfoTable
1074 #endif
1075
1076 -- Make info tables for the data decls in this module
1077 mkITbls :: [TyCon] -> IO ItblEnv
1078 mkITbls [] = return emptyFM
1079 mkITbls (tc:tcs) = do itbls  <- mkITbl tc
1080                       itbls2 <- mkITbls tcs
1081                       return (itbls `plusFM` itbls2)
1082
1083 mkITbl :: TyCon -> IO ItblEnv
1084 mkITbl tc
1085 --   | trace ("TYCON: " ++ showSDoc (ppr tc)) False
1086 --   = error "?!?!"
1087    | not (isDataTyCon tc) 
1088    = return emptyFM
1089    | n == length dcs  -- paranoia; this is an assertion.
1090    = make_constr_itbls dcs
1091      where
1092         dcs = tyConDataCons tc
1093         n   = tyConFamilySize tc
1094
1095 cONSTR :: Int
1096 cONSTR = 1  -- as defined in ghc/includes/ClosureTypes.h
1097
1098 -- Assumes constructors are numbered from zero, not one
1099 make_constr_itbls :: [DataCon] -> IO ItblEnv
1100 make_constr_itbls cons
1101    | length cons <= 8
1102    = do is <- mapM mk_vecret_itbl (zip cons [0..])
1103         return (listToFM is)
1104    | otherwise
1105    = do is <- mapM mk_dirret_itbl (zip cons [0..])
1106         return (listToFM is)
1107      where
1108         mk_vecret_itbl (dcon, conNo)
1109            = mk_itbl dcon conNo (vecret_entry conNo)
1110         mk_dirret_itbl (dcon, conNo)
1111            = mk_itbl dcon conNo mci_constr_entry
1112
1113         mk_itbl :: DataCon -> Int -> Addr -> IO (RdrName,ItblPtr)
1114         mk_itbl dcon conNo entry_addr
1115            = let (tot_wds, ptr_wds, _) 
1116                     = mkVirtHeapOffsets typePrimRep (dataConRepArgTys dcon)
1117                  ptrs = ptr_wds
1118                  nptrs  = tot_wds - ptr_wds
1119                  itbl  = StgInfoTable {
1120                            ptrs = fromIntegral ptrs, nptrs = fromIntegral nptrs,
1121                            tipe = fromIntegral cONSTR,
1122                            srtlen = fromIntegral conNo,
1123                            code0 = fromIntegral code0, code1 = fromIntegral code1,
1124                            code2 = fromIntegral code2, code3 = fromIntegral code3,
1125                            code4 = fromIntegral code4, code5 = fromIntegral code5,
1126                            code6 = fromIntegral code6, code7 = fromIntegral code7 
1127                         }
1128                  -- Make a piece of code to jump to "entry_label".
1129                  -- This is the only arch-dependent bit.
1130                  -- On x86, if entry_label has an address 0xWWXXYYZZ,
1131                  -- emit   movl $0xWWXXYYZZ,%eax  ;  jmp *%eax
1132                  -- which is
1133                  -- B8 ZZ YY XX WW FF E0
1134                  (code0,code1,code2,code3,code4,code5,code6,code7)
1135                     = (0xB8, byte 0 entry_addr_w, byte 1 entry_addr_w, 
1136                              byte 2 entry_addr_w, byte 3 entry_addr_w, 
1137                        0xFF, 0xE0, 
1138                        0x90 {-nop-})
1139
1140                  entry_addr_w :: Word32
1141                  entry_addr_w = fromIntegral (addrToInt entry_addr)
1142              in
1143                  do addr <- malloc
1144                     putStrLn ("SIZE of itbl is " ++ show (sizeOf itbl))
1145                     putStrLn ("# ptrs  of itbl is " ++ show ptrs)
1146                     putStrLn ("# nptrs of itbl is " ++ show nptrs)
1147                     poke addr itbl
1148                     return (toRdrName dcon, addr `plusPtr` 8)
1149
1150
1151 byte :: Int -> Word32 -> Word32
1152 byte 0 w = w .&. 0xFF
1153 byte 1 w = (w `shiftR` 8) .&. 0xFF
1154 byte 2 w = (w `shiftR` 16) .&. 0xFF
1155 byte 3 w = (w `shiftR` 24) .&. 0xFF
1156
1157
1158 vecret_entry 0 = mci_constr1_entry
1159 vecret_entry 1 = mci_constr2_entry
1160 vecret_entry 2 = mci_constr3_entry
1161 vecret_entry 3 = mci_constr4_entry
1162 vecret_entry 4 = mci_constr5_entry
1163 vecret_entry 5 = mci_constr6_entry
1164 vecret_entry 6 = mci_constr7_entry
1165 vecret_entry 7 = mci_constr8_entry
1166
1167 -- entry point for direct returns for created constr itbls
1168 foreign label "stg_mci_constr_entry" mci_constr_entry :: Addr
1169 -- and the 8 vectored ones
1170 foreign label "stg_mci_constr1_entry" mci_constr1_entry :: Addr
1171 foreign label "stg_mci_constr2_entry" mci_constr2_entry :: Addr
1172 foreign label "stg_mci_constr3_entry" mci_constr3_entry :: Addr
1173 foreign label "stg_mci_constr4_entry" mci_constr4_entry :: Addr
1174 foreign label "stg_mci_constr5_entry" mci_constr5_entry :: Addr
1175 foreign label "stg_mci_constr6_entry" mci_constr6_entry :: Addr
1176 foreign label "stg_mci_constr7_entry" mci_constr7_entry :: Addr
1177 foreign label "stg_mci_constr8_entry" mci_constr8_entry :: Addr
1178
1179
1180
1181 data Constructor = Constructor Int{-ptrs-} Int{-nptrs-}
1182
1183
1184 -- Ultra-minimalist version specially for constructors
1185 data StgInfoTable = StgInfoTable {
1186    ptrs :: Word16,
1187    nptrs :: Word16,
1188    srtlen :: Word16,
1189    tipe :: Word16,
1190    code0, code1, code2, code3, code4, code5, code6, code7 :: Word8
1191 }
1192
1193
1194 instance Storable StgInfoTable where
1195
1196    sizeOf itbl 
1197       = (sum . map (\f -> f itbl))
1198         [fieldSz ptrs, fieldSz nptrs, fieldSz srtlen, fieldSz tipe,
1199          fieldSz code0, fieldSz code1, fieldSz code2, fieldSz code3, 
1200          fieldSz code4, fieldSz code5, fieldSz code6, fieldSz code7]
1201
1202    alignment itbl 
1203       = (sum . map (\f -> f itbl))
1204         [fieldAl ptrs, fieldAl nptrs, fieldAl srtlen, fieldAl tipe,
1205          fieldAl code0, fieldAl code1, fieldAl code2, fieldAl code3, 
1206          fieldAl code4, fieldAl code5, fieldAl code6, fieldAl code7]
1207
1208    poke a0 itbl
1209       = do a1 <- store (ptrs   itbl) (castPtr a0)
1210            a2 <- store (nptrs  itbl) a1
1211            a3 <- store (tipe   itbl) a2
1212            a4 <- store (srtlen itbl) a3
1213            a5 <- store (code0  itbl) a4
1214            a6 <- store (code1  itbl) a5
1215            a7 <- store (code2  itbl) a6
1216            a8 <- store (code3  itbl) a7
1217            a9 <- store (code4  itbl) a8
1218            aA <- store (code5  itbl) a9
1219            aB <- store (code6  itbl) aA
1220            aC <- store (code7  itbl) aB
1221            return ()
1222
1223    peek a0
1224       = do (a1,ptrs)   <- load (castPtr a0)
1225            (a2,nptrs)  <- load a1
1226            (a3,tipe)   <- load a2
1227            (a4,srtlen) <- load a3
1228            (a5,code0)  <- load a4
1229            (a6,code1)  <- load a5
1230            (a7,code2)  <- load a6
1231            (a8,code3)  <- load a7
1232            (a9,code4)  <- load a8
1233            (aA,code5)  <- load a9
1234            (aB,code6)  <- load aA
1235            (aC,code7)  <- load aB
1236            return StgInfoTable { ptrs = ptrs, nptrs = nptrs, 
1237                                  srtlen = srtlen, tipe = tipe,
1238                                  code0 = code0, code1 = code1, code2 = code2,
1239                                  code3 = code3, code4 = code4, code5 = code5,
1240                                  code6 = code6, code7 = code7 }
1241
1242 fieldSz :: (Storable a, Storable b) => (a -> b) -> a -> Int
1243 fieldSz sel x = sizeOf (sel x)
1244
1245 fieldAl :: (Storable a, Storable b) => (a -> b) -> a -> Int
1246 fieldAl sel x = alignment (sel x)
1247
1248 store :: Storable a => a -> Ptr a -> IO (Ptr b)
1249 store x addr = do poke addr x
1250                   return (castPtr (addr `plusPtr` sizeOf x))
1251
1252 load :: Storable a => Ptr a -> IO (Ptr b, a)
1253 load addr = do x <- peek addr
1254                return (castPtr (addr `plusPtr` sizeOf x), x)
1255
1256 -----------------------------------------------------------------------------q
1257
1258 foreign import "strncpy" strncpy :: Ptr a -> ByteArray# -> CInt -> IO ()
1259 \end{code}
1260