2 % (c) The University of Glasgow 2000
4 \section[ByteCodeGen]{Generate bytecode from Core}
7 module ByteCodeGen ( UnlinkedBCO, UnlinkedBCOExpr, ItblEnv, ClosureEnv, HValue,
9 byteCodeGen, coreExprToBCOs,
10 linkIModules, linkIExpr
13 #include "HsVersions.h"
16 import Name ( Name, getName, nameModule )
17 import Id ( Id, idType, isDataConId_maybe )
18 import OrdList ( OrdList, consOL, snocOL, appOL, unitOL,
19 nilOL, toOL, concatOL, fromOL )
20 import FiniteMap ( FiniteMap, addListToFM, listToFM, filterFM,
21 addToFM, lookupFM, fmToList, emptyFM, plusFM )
23 import PprCore ( pprCoreExpr, pprCoreAlt )
24 import Literal ( Literal(..) )
25 import PrimRep ( PrimRep(..) )
26 import CoreFVs ( freeVars )
27 import Type ( typePrimRep )
28 import DataCon ( DataCon, dataConTag, fIRST_TAG, dataConTyCon,
30 import TyCon ( TyCon, tyConFamilySize, isDataTyCon, tyConDataCons )
31 import Class ( Class, classTyCon )
32 import Util ( zipEqual, zipWith4Equal, naturalMergeSortLe, nOfThem, global )
33 import Var ( isTyVar )
34 import VarSet ( VarSet, varSetElems )
35 import PrimRep ( getPrimRepSize, isFollowableRep )
36 import Constants ( wORD_SIZE )
37 import CmdLineOpts ( DynFlags, DynFlag(..) )
38 import ErrUtils ( showPass, dumpIfSet_dyn )
39 import ClosureInfo ( mkVirtHeapOffsets )
40 import Module ( ModuleName, moduleName )
42 import List ( intersperse )
43 import Monad ( foldM )
45 import MArray ( MArray(..), IOArray, IOUArray, HasBounds(..), freeze,
47 castSTUArray, readWord32Array,
48 newFloatArray, writeFloatArray,
49 newDoubleArray, writeDoubleArray,
50 newIntArray, writeIntArray,
51 newAddrArray, writeAddrArray )
52 import Foreign ( Storable(..), Word8, Word16, Word32, Ptr(..),
53 malloc, castPtr, plusPtr )
54 import Addr ( Addr, addrToInt, nullAddr )
55 import Bits ( Bits(..), shiftR )
57 import PrelGHC ( BCO#, newBCO#, unsafeCoerce#, ByteArray#, Array# )
58 import IOExts ( IORef, readIORef, writeIORef, fixIO )
60 import PrelArr ( Array(..) )
61 import PrelIOBase ( IO(..) )
65 %************************************************************************
67 \subsection{Functions visible from outside this module.}
69 %************************************************************************
73 byteCodeGen :: DynFlags
76 -> IO ([UnlinkedBCO], ItblEnv)
77 byteCodeGen dflags binds local_tycons local_classes
78 = do showPass dflags "ByteCodeGen"
79 let tycs = local_tycons ++ map classTyCon local_classes
80 itblenv <- mkITbls tycs
82 let flatBinds = concatMap getBind binds
83 getBind (NonRec bndr rhs) = [(bndr, freeVars rhs)]
84 getBind (Rec binds) = [(bndr, freeVars rhs) | (bndr,rhs) <- binds]
85 final_state = runBc (BcM_State [] 0)
86 (mapBc schemeR flatBinds `thenBc_` returnBc ())
87 (BcM_State proto_bcos final_ctr) = final_state
89 dumpIfSet_dyn dflags Opt_D_dump_BCOs
90 "Proto-bcos" (vcat (intersperse (char ' ') (map ppr proto_bcos)))
92 bcos <- mapM assembleBCO proto_bcos
94 return (bcos, itblenv)
97 -- Returns: (the root BCO for this expression,
98 -- a list of auxilary BCOs resulting from compiling closures)
99 coreExprToBCOs :: DynFlags
101 -> IO UnlinkedBCOExpr
102 coreExprToBCOs dflags expr
103 = do showPass dflags "ByteCodeGen"
104 let invented_id = panic "invented_id" :: Id
105 (BcM_State all_proto_bcos final_ctr)
106 = runBc (BcM_State [] 0)
107 (schemeR (invented_id, freeVars expr))
108 dumpIfSet_dyn dflags Opt_D_dump_InterpSyn
109 "Proto-bcos" (vcat (intersperse (char ' ') (map ppr all_proto_bcos)))
111 let invented_name = getName invented_id
113 = case filter ((== invented_name).nameOfProtoBCO) all_proto_bcos of
114 [root_bco] -> root_bco
116 = filter ((/= invented_name).nameOfProtoBCO) all_proto_bcos
118 auxiliary_bcos <- mapM assembleBCO auxiliary_proto_bcos
119 root_bco <- assembleBCO root_proto_bco
121 return (root_bco, auxiliary_bcos)
128 Int (IOUArray Int Word16) -- insns
129 Int (IOUArray Int Word32) -- literals
130 Int (IOArray Int Name) -- ptrs
131 Int (IOArray Int Name) -- itbl refs
133 nameOfUnlinkedBCO (UnlinkedBCO nm _ _ _ _ _ _ _ _) = nm
135 -- When translating expressions, we need to distinguish the root
136 -- BCO for the expression
137 type UnlinkedBCOExpr = (UnlinkedBCO, [UnlinkedBCO])
139 instance Outputable UnlinkedBCO where
140 ppr (UnlinkedBCO nm n_insns insns n_lits lits n_ptrs ptrs n_itbls itbls)
141 = sep [text "BCO", ppr nm, text "with",
142 int n_insns, text "insns",
143 int n_lits, text "lits",
144 int n_ptrs, text "ptrs",
145 int n_itbls, text "itbls"]
148 -- these need a proper home
149 type ItblEnv = FiniteMap Name (Ptr StgInfoTable)
150 type ClosureEnv = FiniteMap Name HValue
151 data HValue = HValue -- dummy type, actually a pointer to some Real Code.
153 -- remove all entries for a given set of modules from the environment
154 filterNameMap :: [ModuleName] -> FiniteMap Name a -> FiniteMap Name a
155 filterNameMap mods env
156 = filterFM (\n _ -> moduleName (nameModule n) `notElem` mods) env
159 %************************************************************************
161 \subsection{Bytecodes, and Outputery.}
163 %************************************************************************
167 type LocalLabel = Int
169 data UnboxedLit = UnboxedI Int | UnboxedF Float | UnboxedD Double
172 -- Messing with the stack
174 -- Push locals (existing bits of the stack)
175 | PUSH_L Int{-offset-}
176 | PUSH_LL Int Int{-2 offsets-}
177 | PUSH_LLL Int Int Int{-3 offsets-}
180 -- Push an alt continuation
181 | PUSH_AS Name PrimRep -- push alts and BCO_ptr_ret_info
182 -- PrimRep so we know which itbl
184 | PUSH_UBX Literal Int
185 -- push this int/float/double, NO TAG, on the stack
186 -- Int is # of items in literal pool to push
187 | PUSH_TAG Int -- push this tag on the stack
189 | SLIDE Int{-this many-} Int{-down by this much-}
190 -- To do with the heap
191 | ALLOC Int -- make an AP_UPD with this many payload words, zeroed
192 | MKAP Int{-ptr to AP_UPD is this far down stack-} Int{-# words-}
193 | UNPACK Int -- unpack N ptr words from t.o.s Constr
194 | UPK_TAG Int Int Int
195 -- unpack N non-ptr words from offset M in constructor
196 -- K words down the stack
198 -- after assembly, the DataCon is an index into the
200 -- For doing case trees
202 | TESTLT_I Int LocalLabel
203 | TESTEQ_I Int LocalLabel
204 | TESTLT_F Float LocalLabel
205 | TESTEQ_F Float LocalLabel
206 | TESTLT_D Double LocalLabel
207 | TESTEQ_D Double LocalLabel
208 | TESTLT_P Int LocalLabel
209 | TESTEQ_P Int LocalLabel
211 -- To Infinity And Beyond
213 | RETURN -- unboxed value on TOS. Use tag to find underlying ret itbl
214 -- and return as per that.
217 instance Outputable BCInstr where
218 ppr (ARGCHECK n) = text "ARGCHECK" <+> int n
219 ppr (PUSH_L offset) = text "PUSH_L " <+> int offset
220 ppr (PUSH_LL o1 o2) = text "PUSH_LL " <+> int o1 <+> int o2
221 ppr (PUSH_LLL o1 o2 o3) = text "PUSH_LLL" <+> int o1 <+> int o2 <+> int o3
222 ppr (PUSH_G nm) = text "PUSH_G " <+> ppr nm
223 ppr (PUSH_AS nm pk) = text "PUSH_AS " <+> ppr nm <+> ppr pk
224 ppr (SLIDE n d) = text "SLIDE " <+> int n <+> int d
225 ppr (ALLOC sz) = text "ALLOC " <+> int sz
226 ppr (MKAP offset sz) = text "MKAP " <+> int offset <+> int sz
227 ppr (UNPACK sz) = text "UNPACK " <+> int sz
228 ppr (PACK dcon sz) = text "PACK " <+> ppr dcon <+> ppr sz
229 ppr (LABEL lab) = text "__" <> int lab <> colon
230 ppr (TESTLT_I i lab) = text "TESTLT_I" <+> int i <+> text "__" <> int lab
231 ppr (TESTEQ_I i lab) = text "TESTEQ_I" <+> int i <+> text "__" <> int lab
232 ppr (TESTLT_F f lab) = text "TESTLT_F" <+> float f <+> text "__" <> int lab
233 ppr (TESTEQ_F f lab) = text "TESTEQ_F" <+> float f <+> text "__" <> int lab
234 ppr (TESTLT_D d lab) = text "TESTLT_D" <+> double d <+> text "__" <> int lab
235 ppr (TESTEQ_D d lab) = text "TESTEQ_D" <+> double d <+> text "__" <> int lab
236 ppr (TESTLT_P i lab) = text "TESTLT_P" <+> int i <+> text "__" <> int lab
237 ppr (TESTEQ_P i lab) = text "TESTEQ_P" <+> int i <+> text "__" <> int lab
238 ppr CASEFAIL = text "CASEFAIL"
239 ppr ENTER = text "ENTER"
240 ppr RETURN = text "RETURN"
242 pprAltCode discrs_n_codes
243 = vcat (map f discrs_n_codes)
244 where f (discr, code) = ppr discr <> colon <+> vcat (map ppr (fromOL code))
246 instance Outputable a => Outputable (ProtoBCO a) where
247 ppr (ProtoBCO name instrs origin)
248 = (text "ProtoBCO" <+> ppr name <> colon)
249 $$ nest 6 (vcat (map ppr instrs))
251 Left alts -> vcat (map (pprCoreAlt.deAnnAlt) alts)
252 Right rhs -> pprCoreExpr (deAnnotate rhs)
255 %************************************************************************
257 \subsection{Compilation schema for the bytecode generator.}
259 %************************************************************************
263 type BCInstrList = OrdList BCInstr
266 = ProtoBCO a -- name, in some sense
268 -- what the BCO came from
269 (Either [AnnAlt Id VarSet]
272 nameOfProtoBCO (ProtoBCO nm insns origin) = nm
275 type Sequel = Int -- back off to this depth before ENTER
277 -- Maps Ids to the offset from the stack _base_ so we don't have
278 -- to mess with it after each push/pop.
279 type BCEnv = FiniteMap Id Int -- To find vars on the stack
282 -- Create a BCO and do a spot of peephole optimisation on the insns
284 mkProtoBCO nm instrs_ordlist origin
285 = ProtoBCO nm (peep (fromOL instrs_ordlist)) origin
287 peep (PUSH_L off1 : PUSH_L off2 : PUSH_L off3 : rest)
288 = PUSH_LLL off1 (off2-1) (off3-2) : peep rest
289 peep (PUSH_L off1 : PUSH_L off2 : rest)
290 = PUSH_LL off1 off2 : peep rest
297 -- Compile code for the right hand side of a let binding.
298 -- Park the resulting BCO in the monad. Also requires the
299 -- variable to which this value was bound, so as to give the
300 -- resulting BCO a name.
301 schemeR :: (Id, AnnExpr Id VarSet) -> BcM ()
302 schemeR (nm, rhs) = schemeR_wrk rhs nm (collect [] rhs)
304 collect xs (_, AnnLam x e)
305 = collect (if isTyVar x then xs else (x:xs)) e
306 collect xs not_lambda
307 = (reverse xs, not_lambda)
309 schemeR_wrk original_body nm (args, body)
310 = let fvs = filter (not.isTyVar) (varSetElems (fst original_body))
311 all_args = fvs ++ reverse args
312 szsw_args = map taggedIdSizeW all_args
313 szw_args = sum szsw_args
314 p_init = listToFM (zip all_args (mkStackOffsets 0 szsw_args))
315 argcheck = if null args then nilOL else unitOL (ARGCHECK szw_args)
317 schemeE szw_args 0 p_init body `thenBc` \ body_code ->
318 emitBc (mkProtoBCO (getName nm) (appOL argcheck body_code) (Right original_body))
320 -- Let szsw be the sizes in words of some items pushed onto the stack,
321 -- which has initial depth d'. Return the values which the stack environment
322 -- should map these items to.
323 mkStackOffsets :: Int -> [Int] -> [Int]
324 mkStackOffsets original_depth szsw
325 = map (subtract 1) (tail (scanl (+) original_depth szsw))
327 -- Compile code to apply the given expression to the remaining args
328 -- on the stack, returning a HNF.
329 schemeE :: Int -> Sequel -> BCEnv -> AnnExpr Id VarSet -> BcM BCInstrList
331 -- Delegate tail-calls to schemeT.
332 schemeE d s p e@(fvs, AnnApp f a)
333 = returnBc (schemeT (should_args_be_tagged e) d s 0 p (fvs, AnnApp f a))
334 schemeE d s p e@(fvs, AnnVar v)
335 | isFollowableRep (typePrimRep (idType v))
336 = returnBc (schemeT (should_args_be_tagged e) d s 0 p (fvs, AnnVar v))
338 = -- returning an unboxed value. Heave it on the stack, SLIDE, and RETURN.
339 let (push, szw) = pushAtom True d p (AnnVar v)
340 in returnBc (push -- value onto stack
341 `snocOL` SLIDE szw (d-s) -- clear to sequel
342 `snocOL` RETURN) -- go
344 schemeE d s p (fvs, AnnLit literal)
345 = let (push, szw) = pushAtom True d p (AnnLit literal)
346 in returnBc (push -- value onto stack
347 `snocOL` SLIDE szw (d-s) -- clear to sequel
348 `snocOL` RETURN) -- go
350 schemeE d s p (fvs, AnnLet binds b)
351 = let (xs,rhss) = case binds of AnnNonRec x rhs -> ([x],[rhs])
352 AnnRec xs_n_rhss -> unzip xs_n_rhss
354 fvss = map (filter (not.isTyVar).varSetElems.fst) rhss
355 sizes = map (\rhs_fvs -> 1 + sum (map taggedIdSizeW rhs_fvs)) fvss
357 -- This p', d' defn is safe because all the items being pushed
358 -- are ptrs, so all have size 1. d' and p' reflect the stack
359 -- after the closures have been allocated in the heap (but not
360 -- filled in), and pointers to them parked on the stack.
361 p' = addListToFM p (zipE xs (mkStackOffsets d (nOfThem n 1)))
364 infos = zipE4 fvss sizes xs [n, n-1 .. 1]
365 zipE = zipEqual "schemeE"
366 zipE4 = zipWith4Equal "schemeE" (\a b c d -> (a,b,c,d))
368 -- ToDo: don't build thunks for things with no free variables
369 buildThunk dd ([], size, id, off)
370 = PUSH_G (getName id)
371 `consOL` unitOL (MKAP (off+size-1) size)
372 buildThunk dd ((fv:fvs), size, id, off)
373 = case pushAtom True dd p' (AnnVar fv) of
374 (push_code, pushed_szw)
376 buildThunk (dd+pushed_szw) (fvs, size, id, off)
378 thunkCode = concatOL (map (buildThunk d') infos)
379 allocCode = toOL (map ALLOC sizes)
381 schemeE d' s p' b `thenBc` \ bodyCode ->
382 mapBc schemeR (zip xs rhss) `thenBc_`
383 returnBc (allocCode `appOL` thunkCode `appOL` bodyCode)
386 schemeE d s p (fvs, AnnCase scrut bndr alts)
388 -- Top of stack is the return itbl, as usual.
389 -- underneath it is the pointer to the alt_code BCO.
390 -- When an alt is entered, it assumes the returned value is
391 -- on top of the itbl.
394 -- Env and depth in which to compile the alts, not including
395 -- any vars bound by the alts themselves
396 d' = d + ret_frame_sizeW + taggedIdSizeW bndr
397 p' = addToFM p bndr (d' - 1)
399 scrut_primrep = typePrimRep (idType bndr)
401 = case scrut_primrep of
402 IntRep -> False ; FloatRep -> False ; DoubleRep -> False
404 other -> pprPanic "ByteCodeGen.schemeE" (ppr other)
406 -- given an alt, return a discr and code for it.
407 codeAlt alt@(discr, binds_f, rhs)
409 = let binds_r = reverse binds_f
410 binds_r_szsw = map untaggedIdSizeW binds_r
411 binds_szw = sum binds_r_szsw
413 p' (zip binds_r (mkStackOffsets d' binds_r_szsw))
415 unpack_code = mkUnpackCode 0 0 (map (typePrimRep.idType) binds_f)
416 in schemeE d'' s p'' rhs `thenBc` \ rhs_code ->
417 returnBc (my_discr alt, unpack_code `appOL` rhs_code)
419 = ASSERT(null binds_f)
420 schemeE d' s p' rhs `thenBc` \ rhs_code ->
421 returnBc (my_discr alt, rhs_code)
423 my_discr (DEFAULT, binds, rhs) = NoDiscr
424 my_discr (DataAlt dc, binds, rhs) = DiscrP (dataConTag dc)
425 my_discr (LitAlt l, binds, rhs)
426 = case l of MachInt i -> DiscrI (fromInteger i)
427 MachFloat r -> DiscrF (fromRational r)
428 MachDouble r -> DiscrD (fromRational r)
431 | not isAlgCase = Nothing
433 = case [dc | (DataAlt dc, _, _) <- alts] of
435 (dc:_) -> Just (tyConFamilySize (dataConTyCon dc))
438 mapBc codeAlt alts `thenBc` \ alt_stuff ->
439 mkMultiBranch maybe_ncons alt_stuff `thenBc` \ alt_final ->
441 alt_bco_name = getName bndr
442 alt_bco = mkProtoBCO alt_bco_name alt_final (Left alts)
444 schemeE (d + ret_frame_sizeW)
445 (d + ret_frame_sizeW) p scrut `thenBc` \ scrut_code ->
447 emitBc alt_bco `thenBc_`
448 returnBc (PUSH_AS alt_bco_name scrut_primrep `consOL` scrut_code)
451 schemeE d s p (fvs, AnnNote note body)
455 = pprPanic "ByteCodeGen.schemeE: unhandled case"
456 (pprCoreExpr (deAnnotate other))
459 -- Compile code to do a tail call. Doesn't need to be monadic.
460 schemeT :: Bool -- do tagging?
461 -> Int -- Stack depth
462 -> Sequel -- Sequel depth
463 -> Int -- # arg words so far
464 -> BCEnv -- stack env
468 schemeT enTag d s narg_words p (_, AnnApp f a)
470 AnnType _ -> schemeT enTag d s narg_words p f
472 -> let (push, arg_words) = pushAtom enTag d p (snd a)
474 `appOL` schemeT enTag (d+arg_words) s (narg_words+arg_words) p f
476 schemeT enTag d s narg_words p (_, AnnVar f)
477 | Just con <- isDataConId_maybe f
478 = ASSERT(enTag == False)
479 PACK con narg_words `consOL` (mkSLIDE 1 (d-s-1) `snocOL` ENTER)
481 = ASSERT(enTag == True)
482 let (push, arg_words) = pushAtom True d p (AnnVar f)
484 `appOL` mkSLIDE (narg_words+arg_words) (d - s - narg_words)
488 = if d == 0 then nilOL else unitOL (SLIDE n d)
490 should_args_be_tagged (_, AnnVar v)
491 = case isDataConId_maybe v of
492 Just dcon -> False; Nothing -> True
493 should_args_be_tagged (_, AnnApp f a)
494 = should_args_be_tagged f
495 should_args_be_tagged (_, other)
496 = panic "should_args_be_tagged: tail call to non-con, non-var"
499 -- Make code to unpack a constructor onto the stack, adding
500 -- tags for the unboxed bits. Takes the PrimReps of the constructor's
501 -- arguments, and a travelling offset along both the constructor
502 -- (off_h) and the stack (off_s).
503 mkUnpackCode :: Int -> Int -> [PrimRep] -> BCInstrList
504 mkUnpackCode off_h off_s [] = nilOL
505 mkUnpackCode off_h off_s (r:rs)
507 = let (rs_ptr, rs_nptr) = span isFollowableRep (r:rs)
508 ptrs_szw = sum (map untaggedSizeW rs_ptr)
509 in ASSERT(ptrs_szw == length rs_ptr)
513 `consOL` mkUnpackCode (off_h + ptrs_szw) (off_s + ptrs_szw) rs_nptr
518 DoubleRep -> approved
520 approved = UPK_TAG usizeW off_h off_s `consOL` theRest
521 theRest = mkUnpackCode (off_h + usizeW) (off_s + tsizeW) rs
522 usizeW = untaggedSizeW r
523 tsizeW = taggedSizeW r
525 -- Push an atom onto the stack, returning suitable code & number of
526 -- stack words used. Pushes it either tagged or untagged, since
527 -- pushAtom is used to set up the stack prior to copying into the
528 -- heap for both APs (requiring tags) and constructors (which don't).
530 -- NB this means NO GC between pushing atoms for a constructor and
531 -- copying them into the heap. It probably also means that
532 -- tail calls MUST be of the form atom{atom ... atom} since if the
533 -- expression head was allowed to be arbitrary, there could be GC
534 -- in between pushing the arg atoms and completing the head.
535 -- (not sure; perhaps the allocate/doYouWantToGC interface means this
536 -- isn't a problem; but only if arbitrary graph construction for the
537 -- head doesn't leave this BCO, since GC might happen at the start of
538 -- each BCO (we consult doYouWantToGC there).
540 -- Blargh. JRS 001206
542 -- NB (further) that the env p must map each variable to the highest-
543 -- numbered stack slot for it. For example, if the stack has depth 4
544 -- and we tagged-ly push (v :: Int#) on it, the value will be in stack[4],
545 -- the tag in stack[5], the stack will have depth 6, and p must map v to
546 -- 5 and not to 4. Stack locations are numbered from zero, so a depth
547 -- 6 stack has valid words 0 .. 5.
549 pushAtom :: Bool -> Int -> BCEnv -> AnnExpr' Id VarSet -> (BCInstrList, Int)
550 pushAtom tagged d p (AnnVar v)
551 = let str = "\npushAtom " ++ showSDocDebug (ppr v) ++ ", depth = " ++ show d
553 showSDocDebug (nest 4 (vcat (map ppr (fmToList p))))
555 showSDoc (nest 4 (vcat (map ppr (fromOL (fst result)))))
556 ++ "\nendPushAtom " ++ showSDocDebug (ppr v)
557 str' = if str == str then str else str
560 = case lookupBCEnv_maybe p v of
561 Just d_v -> (toOL (nOfThem nwords (PUSH_L (d-d_v+sz_t-2))), sz_t)
562 Nothing -> ASSERT(sz_t == 1) (unitOL (PUSH_G nm), sz_t)
565 sz_t = taggedIdSizeW v
566 sz_u = untaggedIdSizeW v
567 nwords = if tagged then sz_t else sz_u
572 pushAtom True d p (AnnLit lit)
573 = let (ubx_code, ubx_size) = pushAtom False d p (AnnLit lit)
574 in (ubx_code `snocOL` PUSH_TAG ubx_size, 1 + ubx_size)
576 pushAtom False d p (AnnLit lit)
578 MachInt i -> code IntRep
579 MachFloat r -> code FloatRep
580 MachDouble r -> code DoubleRep
583 = let size_host_words = untaggedSizeW rep
584 size_in_word32s = (size_host_words * wORD_SIZE) `div` 4
585 in (unitOL (PUSH_UBX lit size_in_word32s), size_host_words)
587 pushAtom tagged d p (AnnApp f (_, AnnType _))
588 = pushAtom tagged d p (snd f)
590 pushAtom tagged d p other
591 = pprPanic "ByteCodeGen.pushAtom"
592 (pprCoreExpr (deAnnotate (undefined, other)))
595 -- Given a bunch of alts code and their discrs, do the donkey work
596 -- of making a multiway branch using a switch tree.
597 -- What a load of hassle!
598 mkMultiBranch :: Maybe Int -- # datacons in tycon, if alg alt
599 -- a hint; generates better code
600 -- Nothing is always safe
601 -> [(Discr, BCInstrList)]
603 mkMultiBranch maybe_ncons raw_ways
604 = let d_way = filter (isNoDiscr.fst) raw_ways
605 notd_ways = naturalMergeSortLe
606 (\w1 w2 -> leAlt (fst w1) (fst w2))
607 (filter (not.isNoDiscr.fst) raw_ways)
609 mkTree :: [(Discr, BCInstrList)] -> Discr -> Discr -> BcM BCInstrList
610 mkTree [] range_lo range_hi = returnBc the_default
612 mkTree [val] range_lo range_hi
613 | range_lo `eqAlt` range_hi
616 = getLabelBc `thenBc` \ label_neq ->
617 returnBc (mkTestEQ (fst val) label_neq
619 `appOL` unitOL (LABEL label_neq)
620 `appOL` the_default))
622 mkTree vals range_lo range_hi
623 = let n = length vals `div` 2
624 vals_lo = take n vals
625 vals_hi = drop n vals
626 v_mid = fst (head vals_hi)
628 getLabelBc `thenBc` \ label_geq ->
629 mkTree vals_lo range_lo (dec v_mid) `thenBc` \ code_lo ->
630 mkTree vals_hi v_mid range_hi `thenBc` \ code_hi ->
631 returnBc (mkTestLT v_mid label_geq
633 `appOL` unitOL (LABEL label_geq)
637 = case d_way of [] -> unitOL CASEFAIL
640 -- None of these will be needed if there are no non-default alts
641 (mkTestLT, mkTestEQ, init_lo, init_hi)
643 = panic "mkMultiBranch: awesome foursome"
645 = case fst (head notd_ways) of {
646 DiscrI _ -> ( \(DiscrI i) fail_label -> TESTLT_I i fail_label,
647 \(DiscrI i) fail_label -> TESTEQ_I i fail_label,
650 DiscrF _ -> ( \(DiscrF f) fail_label -> TESTLT_F f fail_label,
651 \(DiscrF f) fail_label -> TESTEQ_F f fail_label,
654 DiscrD _ -> ( \(DiscrD d) fail_label -> TESTLT_D d fail_label,
655 \(DiscrD d) fail_label -> TESTEQ_D d fail_label,
658 DiscrP _ -> ( \(DiscrP i) fail_label -> TESTLT_P i fail_label,
659 \(DiscrP i) fail_label -> TESTEQ_P i fail_label,
664 (algMinBound, algMaxBound)
665 = case maybe_ncons of
666 Just n -> (fIRST_TAG, fIRST_TAG + n - 1)
667 Nothing -> (minBound, maxBound)
669 (DiscrI i1) `eqAlt` (DiscrI i2) = i1 == i2
670 (DiscrF f1) `eqAlt` (DiscrF f2) = f1 == f2
671 (DiscrD d1) `eqAlt` (DiscrD d2) = d1 == d2
672 (DiscrP i1) `eqAlt` (DiscrP i2) = i1 == i2
673 NoDiscr `eqAlt` NoDiscr = True
676 (DiscrI i1) `leAlt` (DiscrI i2) = i1 <= i2
677 (DiscrF f1) `leAlt` (DiscrF f2) = f1 <= f2
678 (DiscrD d1) `leAlt` (DiscrD d2) = d1 <= d2
679 (DiscrP i1) `leAlt` (DiscrP i2) = i1 <= i2
680 NoDiscr `leAlt` NoDiscr = True
683 isNoDiscr NoDiscr = True
686 dec (DiscrI i) = DiscrI (i-1)
687 dec (DiscrP i) = DiscrP (i-1)
688 dec other = other -- not really right, but if you
689 -- do cases on floating values, you'll get what you deserve
691 -- same snotty comment applies to the following
699 mkTree notd_ways init_lo init_hi
703 %************************************************************************
705 \subsection{Supporting junk for the compilation schemes}
707 %************************************************************************
711 -- Describes case alts
719 instance Outputable Discr where
720 ppr (DiscrI i) = int i
721 ppr (DiscrF f) = text (show f)
722 ppr (DiscrD d) = text (show d)
723 ppr (DiscrP i) = int i
724 ppr NoDiscr = text "DEF"
727 -- Find things in the BCEnv (the what's-on-the-stack-env)
728 -- See comment preceding pushAtom for precise meaning of env contents
729 lookupBCEnv :: BCEnv -> Id -> Int
731 = case lookupFM env nm of
732 Nothing -> pprPanic "lookupBCEnv"
733 (ppr nm $$ char ' ' $$ vcat (map ppr (fmToList env)))
736 lookupBCEnv_maybe :: BCEnv -> Id -> Maybe Int
737 lookupBCEnv_maybe = lookupFM
740 -- When I push one of these on the stack, how much does Sp move by?
741 taggedSizeW :: PrimRep -> Int
743 | isFollowableRep pr = 1
744 | otherwise = 1{-the tag-} + getPrimRepSize pr
747 -- The plain size of something, without tag.
748 untaggedSizeW :: PrimRep -> Int
750 | isFollowableRep pr = 1
751 | otherwise = getPrimRepSize pr
754 taggedIdSizeW, untaggedIdSizeW :: Id -> Int
755 taggedIdSizeW = taggedSizeW . typePrimRep . idType
756 untaggedIdSizeW = untaggedSizeW . typePrimRep . idType
760 %************************************************************************
762 \subsection{The bytecode generator's monad}
764 %************************************************************************
768 = BcM_State { bcos :: [ProtoBCO Name], -- accumulates completed BCOs
769 nextlabel :: Int } -- for generating local labels
771 type BcM result = BcM_State -> (result, BcM_State)
773 mkBcM_State :: [ProtoBCO Name] -> Int -> BcM_State
774 mkBcM_State = BcM_State
776 runBc :: BcM_State -> BcM () -> BcM_State
777 runBc init_st m = case m init_st of { (r,st) -> st }
779 thenBc :: BcM a -> (a -> BcM b) -> BcM b
781 = case expr st of { (result, st') -> cont result st' }
783 thenBc_ :: BcM a -> BcM b -> BcM b
785 = case expr st of { (result, st') -> cont st' }
787 returnBc :: a -> BcM a
788 returnBc result st = (result, st)
790 mapBc :: (a -> BcM b) -> [a] -> BcM [b]
791 mapBc f [] = returnBc []
793 = f x `thenBc` \ r ->
794 mapBc f xs `thenBc` \ rs ->
797 emitBc :: ProtoBCO Name -> BcM ()
799 = ((), st{bcos = bco : bcos st})
801 getLabelBc :: BcM Int
803 = (nextlabel st, st{nextlabel = 1 + nextlabel st})
807 %************************************************************************
809 \subsection{The bytecode assembler}
811 %************************************************************************
813 The object format for bytecodes is: 16 bits for the opcode, and 16 for
814 each field -- so the code can be considered a sequence of 16-bit ints.
815 Each field denotes either a stack offset or number of items on the
816 stack (eg SLIDE), and index into the pointer table (eg PUSH_G), an
817 index into the literal table (eg PUSH_I/D/L), or a bytecode address in
821 -- Top level assembler fn.
822 assembleBCO :: ProtoBCO Name -> IO UnlinkedBCO
824 assembleBCO (ProtoBCO nm instrs origin)
826 -- pass 1: collect up the offsets of the local labels
827 label_env = mkLabelEnv emptyFM 0 instrs
829 mkLabelEnv env i_offset [] = env
830 mkLabelEnv env i_offset (i:is)
832 = case i of LABEL n -> addToFM env n i_offset ; _ -> env
833 in mkLabelEnv new_env (i_offset + instrSizeB i) is
836 = case lookupFM label_env lab of
837 Just bco_offset -> bco_offset
838 Nothing -> pprPanic "assembleBCO.findLabel" (int lab)
845 do insns <- newXIOUArray init_n_insns :: IO (XIOUArray Word16)
846 lits <- newXIOUArray init_n_lits :: IO (XIOUArray Word32)
847 ptrs <- newXIOArray init_n_ptrs -- :: IO (XIOArray Name)
848 itbls <- newXIOArray init_n_itbls -- :: IO (XIOArray Name)
850 -- pass 2: generate the instruction, ptr and nonptr bits
851 let init_asm_state = (insns,lits,ptrs,itbls)
852 final_asm_state <- mkBits findLabel init_asm_state instrs
854 -- unwrap the expandable arrays
855 let final_insns = stuffXIOU insns
856 final_lits = stuffXIOU lits
857 final_ptrs = stuffXIO ptrs
858 final_itbls = stuffXIO itbls
860 return (UnlinkedBCO nm
861 (usedXIOU insns) final_insns
862 (usedXIOU lits) final_lits
863 (usedXIO ptrs) final_ptrs
864 (usedXIO itbls) final_itbls)
867 -- instrs nonptrs ptrs itbls
868 type AsmState = (XIOUArray Word16, XIOUArray Word32, XIOArray Name, XIOArray Name)
871 -- This is where all the action is (pass 2 of the assembler)
872 mkBits :: (Int -> Int) -- label finder
874 -> [BCInstr] -- instructions (in)
877 mkBits findLabel st proto_insns
878 = foldM doInstr st proto_insns
880 doInstr :: AsmState -> BCInstr -> IO AsmState
883 ARGCHECK n -> instr2 st i_ARGCHECK n
884 PUSH_L o1 -> instr2 st i_PUSH_L o1
885 PUSH_LL o1 o2 -> instr3 st i_PUSH_LL o1 o2
886 PUSH_LLL o1 o2 o3 -> instr4 st i_PUSH_LLL o1 o2 o3
887 PUSH_G nm -> do (p, st2) <- ptr st nm
888 instr2 st2 i_PUSH_G p
889 PUSH_AS nm pk -> do (p, st2) <- ptr st nm
890 (np, st3) <- ret_itbl st2 pk
891 instr3 st3 i_PUSH_AS p np
892 PUSH_UBX lit nw32s -> do (np, st2) <- literal st lit
893 instr3 st2 i_PUSH_UBX np nw32s
894 PUSH_TAG tag -> instr2 st i_PUSH_TAG tag
895 SLIDE n by -> instr3 st i_SLIDE n by
896 ALLOC n -> instr2 st i_ALLOC n
897 MKAP off sz -> instr3 st i_MKAP off sz
898 UNPACK n -> instr2 st i_UNPACK n
899 UPK_TAG n m k -> instr4 st i_UPK_TAG n m k
900 PACK dcon sz -> do (itbl_no,st2) <- itbl st dcon
901 instr3 st2 i_PACK itbl_no sz
902 LABEL lab -> return st
903 TESTLT_I i l -> do (np, st2) <- int st i
904 instr3 st2 i_TESTLT_I np (findLabel l)
905 TESTEQ_I i l -> do (np, st2) <- int st i
906 instr3 st2 i_TESTEQ_I np (findLabel l)
907 TESTLT_F f l -> do (np, st2) <- float st f
908 instr3 st2 i_TESTLT_F np (findLabel l)
909 TESTEQ_F f l -> do (np, st2) <- float st f
910 instr3 st2 i_TESTEQ_F np (findLabel l)
911 TESTLT_D d l -> do (np, st2) <- double st d
912 instr3 st2 i_TESTLT_D np (findLabel l)
913 TESTEQ_D d l -> do (np, st2) <- double st d
914 instr3 st2 i_TESTEQ_D np (findLabel l)
915 TESTLT_P i l -> do (np, st2) <- int st i
916 instr3 st2 i_TESTLT_P np (findLabel l)
917 TESTEQ_P i l -> do (np, st2) <- int st i
918 instr3 st2 i_TESTEQ_P np (findLabel l)
919 CASEFAIL -> instr1 st i_CASEFAIL
920 ENTER -> instr1 st i_ENTER
921 RETURN -> instr1 st i_RETURN
926 instr1 (st_i0,st_l0,st_p0,st_I0) i1
927 = do st_i1 <- addToXIOUArray st_i0 (i2s i1)
928 return (st_i1,st_l0,st_p0,st_I0)
930 instr2 (st_i0,st_l0,st_p0,st_I0) i1 i2
931 = do st_i1 <- addToXIOUArray st_i0 (i2s i1)
932 st_i2 <- addToXIOUArray st_i1 (i2s i2)
933 return (st_i2,st_l0,st_p0,st_I0)
935 instr3 (st_i0,st_l0,st_p0,st_I0) i1 i2 i3
936 = do st_i1 <- addToXIOUArray st_i0 (i2s i1)
937 st_i2 <- addToXIOUArray st_i1 (i2s i2)
938 st_i3 <- addToXIOUArray st_i2 (i2s i3)
939 return (st_i3,st_l0,st_p0,st_I0)
941 instr4 (st_i0,st_l0,st_p0,st_I0) i1 i2 i3 i4
942 = do st_i1 <- addToXIOUArray st_i0 (i2s i1)
943 st_i2 <- addToXIOUArray st_i1 (i2s i2)
944 st_i3 <- addToXIOUArray st_i2 (i2s i3)
945 st_i4 <- addToXIOUArray st_i3 (i2s i4)
946 return (st_i4,st_l0,st_p0,st_I0)
948 float (st_i0,st_l0,st_p0,st_I0) f
949 = do let w32s = mkLitF f
950 st_l1 <- addListToXIOUArray st_l0 w32s
951 return (usedXIOU st_l0, (st_i0,st_l1,st_p0,st_I0))
953 double (st_i0,st_l0,st_p0,st_I0) d
954 = do let w32s = mkLitD d
955 st_l1 <- addListToXIOUArray st_l0 w32s
956 return (usedXIOU st_l0, (st_i0,st_l1,st_p0,st_I0))
958 int (st_i0,st_l0,st_p0,st_I0) i
959 = do let w32s = mkLitI i
960 st_l1 <- addListToXIOUArray st_l0 w32s
961 return (usedXIOU st_l0, (st_i0,st_l1,st_p0,st_I0))
963 addr (st_i0,st_l0,st_p0,st_I0) a
964 = do let w32s = mkLitA a
965 st_l1 <- addListToXIOUArray st_l0 w32s
966 return (usedXIOU st_l0, (st_i0,st_l1,st_p0,st_I0))
968 ptr (st_i0,st_l0,st_p0,st_I0) p
969 = do st_p1 <- addToXIOArray st_p0 p
970 return (usedXIO st_p0, (st_i0,st_l0,st_p1,st_I0))
972 itbl (st_i0,st_l0,st_p0,st_I0) dcon
973 = do st_I1 <- addToXIOArray st_I0 (getName dcon)
974 return (usedXIO st_I0, (st_i0,st_l0,st_p0,st_I1))
976 literal st (MachInt j) = int st (fromIntegral j)
977 literal st (MachFloat r) = float st (fromRational r)
978 literal st (MachDouble r) = double st (fromRational r)
981 = addr st ret_itbl_addr
985 IntRep -> stg_ctoi_ret_R1_info
986 FloatRep -> stg_ctoi_ret_F1_info
987 DoubleRep -> stg_ctoi_ret_D1_info
989 stg_ctoi_ret_F1_info = nullAddr
990 stg_ctoi_ret_D1_info = nullAddr
992 foreign label "stg_ctoi_ret_R1_info" stg_ctoi_ret_R1_info :: Addr
993 --foreign label "stg_ctoi_ret_F1_info" stg_ctoi_ret_F1_info :: Addr
994 --foreign label "stg_ctoi_ret_D1_info" stg_ctoi_ret_D1_info :: Addr
996 -- The size in bytes of an instruction.
997 instrSizeB :: BCInstr -> Int
1024 -- Sizes of Int, Float and Double literals, in units of 32-bitses
1025 intLitSz32s, floatLitSz32s, doubleLitSz32s, addrLitSz32s :: Int
1026 intLitSz32s = wORD_SIZE `div` 4
1027 floatLitSz32s = 1 -- Assume IEEE floats
1029 addrLitSz32s = intLitSz32s
1031 -- Make lists of 32-bit words for literals, so that when the
1032 -- words are placed in memory at increasing addresses, the
1033 -- bit pattern is correct for the host's word size and endianness.
1034 mkLitI :: Int -> [Word32]
1035 mkLitF :: Float -> [Word32]
1036 mkLitD :: Double -> [Word32]
1037 mkLitA :: Addr -> [Word32]
1041 arr <- newFloatArray ((0::Int),0)
1042 writeFloatArray arr 0 f
1043 f_arr <- castSTUArray arr
1044 w0 <- readWord32Array f_arr 0
1050 arr <- newDoubleArray ((0::Int),0)
1051 writeDoubleArray arr 0 d
1052 d_arr <- castSTUArray arr
1053 w0 <- readWord32Array d_arr 0
1054 w1 <- readWord32Array d_arr 1
1061 arr <- newIntArray ((0::Int),0)
1062 writeIntArray arr 0 i
1063 i_arr <- castSTUArray arr
1064 w0 <- readWord32Array i_arr 0
1069 arr <- newIntArray ((0::Int),0)
1070 writeIntArray arr 0 i
1071 i_arr <- castSTUArray arr
1072 w0 <- readWord32Array i_arr 0
1073 w1 <- readWord32Array i_arr 1
1080 arr <- newAddrArray ((0::Int),0)
1081 writeAddrArray arr 0 a
1082 a_arr <- castSTUArray arr
1083 w0 <- readWord32Array a_arr 0
1088 arr <- newAddrArray ((0::Int),0)
1089 writeAddrArray arr 0 a
1090 a_arr <- castSTUArray arr
1091 w0 <- readWord32Array a_arr 0
1092 w1 <- readWord32Array a_arr 1
1098 -- Zero-based expandable arrays
1100 = XIOUArray { usedXIOU :: Int, stuffXIOU :: (IOUArray Int ele) }
1102 = XIOArray { usedXIO :: Int , stuffXIO :: (IOArray Int ele) }
1105 = do arr <- newArray (0, size-1)
1106 return (XIOUArray 0 arr)
1108 addListToXIOUArray xarr []
1110 addListToXIOUArray xarr (x:xs)
1111 = addToXIOUArray xarr x >>= \ xarr' -> addListToXIOUArray xarr' xs
1114 addToXIOUArray :: MArray IOUArray a IO
1115 => XIOUArray a -> a -> IO (XIOUArray a)
1116 addToXIOUArray (XIOUArray n_arr arr) x
1117 = case bounds arr of
1118 (lo, hi) -> ASSERT(lo == 0)
1120 then do new_arr <- newArray (0, 2*hi-1)
1122 addToXIOUArray (XIOUArray n_arr new_arr) x
1123 else do writeArray arr n_arr x
1124 return (XIOUArray (n_arr+1) arr)
1126 copy :: MArray IOUArray a IO
1127 => Int -> IOUArray Int a -> IOUArray Int a -> IO ()
1130 | otherwise = do nx <- readArray src n
1137 = do arr <- newArray (0, size-1)
1138 return (XIOArray 0 arr)
1140 addToXIOArray :: XIOArray a -> a -> IO (XIOArray a)
1141 addToXIOArray (XIOArray n_arr arr) x
1142 = case bounds arr of
1143 (lo, hi) -> ASSERT(lo == 0)
1145 then do new_arr <- newArray (0, 2*hi-1)
1147 addToXIOArray (XIOArray n_arr new_arr) x
1148 else do writeArray arr n_arr x
1149 return (XIOArray (n_arr+1) arr)
1151 copy :: Int -> IOArray Int a -> IOArray Int a -> IO ()
1154 | otherwise = do nx <- readArray src n
1160 %************************************************************************
1162 \subsection{Linking interpretables into something we can run}
1164 %************************************************************************
1170 = UnlinkedBCO Int (IOUArray Int Word16) -- #insns insns
1171 Int (IOUArray Int Word32) -- #literals literals
1172 Int (IOArray Int Name) -- #ptrs ptrs
1173 Int (IOArray Int Name) -- #itblrefs itblrefs
1175 data BCO# = BCO# ByteArray# -- instrs :: array Word16#
1176 ByteArray# -- literals :: array Word32#
1177 PtrArray# -- ptrs :: Array HValue
1178 ByteArray# -- itbls :: Array Addr#
1181 data LinkedBCO = LinkedBCO BCO#
1185 GLOBAL_VAR(v_cafTable, [], [HValue])
1187 addCAF :: HValue -> IO ()
1188 addCAF x = do xs <- readIORef v_cafTable; writeIORef v_cafTable (x:xs)
1190 bcosToHValue :: ItblEnv -> ClosureEnv -> UnlinkedBCOExpr -> IO HValue
1191 bcosToHValue ie ce (root_bco, other_bcos)
1192 = do linked_expr <- linkIExpr ie ce (root_bco, other_bcos)
1196 linkIModules :: ItblEnv -- incoming global itbl env; returned updated
1197 -> ClosureEnv -- incoming global closure env; returned updated
1198 -> [([UnlinkedBCO], ItblEnv)]
1199 -> IO ([HValue], ItblEnv, ClosureEnv)
1200 linkIModules gie gce mods = do
1201 let (bcoss, ies) = unzip mods
1203 top_level_binders = map nameOfUnlinkedBCO bcos
1204 final_gie = foldr plusFM gie ies
1206 (new_bcos, new_gce) <-
1207 fixIO (\ ~(new_bcos, new_gce) -> do
1208 new_bcos <- linkBCOs final_gie new_gce bcos
1209 let new_gce = addListToFM gce (zip top_level_binders new_bcos)
1210 return (new_bcos, new_gce))
1212 return (new_bcos, final_gie, new_gce)
1215 linkIExpr :: ItblEnv -> ClosureEnv -> UnlinkedBCOExpr
1216 -> IO HValue -- IO BCO# really
1217 linkIExpr ie ce (root_ul_bco, aux_ul_bcos)
1218 = do let aux_ul_binders = map nameOfUnlinkedBCO aux_ul_bcos
1221 (\ ~(aux_bcos, new_ce)
1222 -> do new_bcos <- linkBCOs ie new_ce aux_ul_bcos
1223 let new_ce = addListToFM ce (zip aux_ul_binders new_bcos)
1224 return (new_bcos, new_ce)
1227 <- linkBCOs ie aux_ce [root_ul_bco]
1231 linkBCOs :: ItblEnv -> ClosureEnv -> [UnlinkedBCO]
1232 -> IO [HValue] -- IO [BCO#] really
1233 linkBCOs ie ce binds = mapM (linkBCO ie ce) binds
1235 linkBCO ie ce (UnlinkedBCO nm
1236 n_insns insns n_literals literals
1237 n_ptrs ptrs n_itbls itbls)
1238 = do linked_ptrs <- mapArray (lookupCE ce) ptrs
1239 linked_itbls <- mapArray (lookupIE ie) itbls
1241 ptrs_froz <- freeze linked_ptrs
1242 let ptrs_parr = case ptrs_froz of Array lo hi parr -> parr
1244 insns_froz <- freeze insns
1245 let insns_barr = case insns_froz of UArray lo hi barr -> barr
1247 literals_froz <- freeze literals
1248 let literals_barr = case literals_froz of UArray lo hi barr -> barr
1250 itbls_froz <- freeze linked_itbls
1251 let itbls_barr = case itbls_froz of UArray lo hi barr -> barr
1253 BCO bco# <- newBCO insns_barr literals_barr ptrs_parr itbls_barr
1255 return (unsafeCoerce# bco#)
1259 newBCO :: ByteArray# -> ByteArray# -> Array# a -> ByteArray# -> IO BCO
1260 newBCO a b c d = IO (\s -> case newBCO# a b c d s of (# s1, bco #) -> (# s1, BCO bco #))
1263 lookupCE :: ClosureEnv -> Name -> HValue
1265 = case lookupFM ce nm of
1266 Just aa -> unsafeCoerce# aa
1267 Nothing -> pprPanic "ByteCodeGen.lookupCE" (ppr nm)
1269 lookupIE :: ItblEnv -> Name -> Addr
1271 = case lookupFM ie nm of
1273 Nothing -> pprPanic "ByteCodeGen.lookupIE" (ppr nm)
1279 case lookupFM ie con of
1280 Just (Ptr addr) -> return addr
1282 -- try looking up in the object files.
1283 m <- lookupSymbol (nameToCLabel con "con_info")
1285 Just addr -> return addr
1286 Nothing -> pprPanic "linkIExpr" (ppr con)
1288 -- nullary constructors don't have normal _con_info tables.
1289 lookupNullaryCon ie con =
1290 case lookupFM ie con of
1291 Just (Ptr addr) -> return (ConApp addr)
1293 -- try looking up in the object files.
1294 m <- lookupSymbol (nameToCLabel con "closure")
1296 Just (A# addr) -> return (Native (unsafeCoerce# addr))
1297 Nothing -> pprPanic "lookupNullaryCon" (ppr con)
1300 lookupNative ce var =
1301 unsafeInterleaveIO (do
1302 case lookupFM ce var of
1303 Just e -> return (Native e)
1305 -- try looking up in the object files.
1306 let lbl = (nameToCLabel var "closure")
1307 m <- lookupSymbol lbl
1310 -> do addCAF (unsafeCoerce# addr)
1311 return (Native (unsafeCoerce# addr))
1312 Nothing -> pprPanic "linkIExpr" (ppr var)
1315 -- some VarI/VarP refer to top-level interpreted functions; we change
1316 -- them into Natives here.
1318 unsafeInterleaveIO (
1319 case lookupFM ce (getName v) of
1320 Nothing -> return (f v)
1321 Just e -> return (Native e)
1324 -- HACK!!! ToDo: cleaner
1325 nameToCLabel :: Name -> String{-suffix-} -> String
1326 nameToCLabel n suffix =
1327 _UNPK_(moduleNameFS (rdrNameModule rn))
1328 ++ '_':occNameString(rdrNameOcc rn) ++ '_':suffix
1329 where rn = toRdrName n
1333 %************************************************************************
1335 \subsection{Manufacturing of info tables for DataCons}
1337 %************************************************************************
1341 #if __GLASGOW_HASKELL__ <= 408
1344 type ItblPtr = Ptr StgInfoTable
1347 -- Make info tables for the data decls in this module
1348 mkITbls :: [TyCon] -> IO ItblEnv
1349 mkITbls [] = return emptyFM
1350 mkITbls (tc:tcs) = do itbls <- mkITbl tc
1351 itbls2 <- mkITbls tcs
1352 return (itbls `plusFM` itbls2)
1354 mkITbl :: TyCon -> IO ItblEnv
1356 -- | trace ("TYCON: " ++ showSDoc (ppr tc)) False
1358 | not (isDataTyCon tc)
1360 | n == length dcs -- paranoia; this is an assertion.
1361 = make_constr_itbls dcs
1363 dcs = tyConDataCons tc
1364 n = tyConFamilySize tc
1367 cONSTR = 1 -- as defined in ghc/includes/ClosureTypes.h
1369 -- Assumes constructors are numbered from zero, not one
1370 make_constr_itbls :: [DataCon] -> IO ItblEnv
1371 make_constr_itbls cons
1373 = do is <- mapM mk_vecret_itbl (zip cons [0..])
1374 return (listToFM is)
1376 = do is <- mapM mk_dirret_itbl (zip cons [0..])
1377 return (listToFM is)
1379 mk_vecret_itbl (dcon, conNo)
1380 = mk_itbl dcon conNo (vecret_entry conNo)
1381 mk_dirret_itbl (dcon, conNo)
1382 = mk_itbl dcon conNo stg_interp_constr_entry
1384 mk_itbl :: DataCon -> Int -> Addr -> IO (Name,ItblPtr)
1385 mk_itbl dcon conNo entry_addr
1386 = let (tot_wds, ptr_wds, _)
1387 = mkVirtHeapOffsets typePrimRep (dataConRepArgTys dcon)
1389 nptrs = tot_wds - ptr_wds
1390 itbl = StgInfoTable {
1391 ptrs = fromIntegral ptrs, nptrs = fromIntegral nptrs,
1392 tipe = fromIntegral cONSTR,
1393 srtlen = fromIntegral conNo,
1394 code0 = fromIntegral code0, code1 = fromIntegral code1,
1395 code2 = fromIntegral code2, code3 = fromIntegral code3,
1396 code4 = fromIntegral code4, code5 = fromIntegral code5,
1397 code6 = fromIntegral code6, code7 = fromIntegral code7
1399 -- Make a piece of code to jump to "entry_label".
1400 -- This is the only arch-dependent bit.
1401 -- On x86, if entry_label has an address 0xWWXXYYZZ,
1402 -- emit movl $0xWWXXYYZZ,%eax ; jmp *%eax
1404 -- B8 ZZ YY XX WW FF E0
1405 (code0,code1,code2,code3,code4,code5,code6,code7)
1406 = (0xB8, byte 0 entry_addr_w, byte 1 entry_addr_w,
1407 byte 2 entry_addr_w, byte 3 entry_addr_w,
1411 entry_addr_w :: Word32
1412 entry_addr_w = fromIntegral (addrToInt entry_addr)
1415 --putStrLn ("SIZE of itbl is " ++ show (sizeOf itbl))
1416 --putStrLn ("# ptrs of itbl is " ++ show ptrs)
1417 --putStrLn ("# nptrs of itbl is " ++ show nptrs)
1419 return (getName dcon, addr `plusPtr` 8)
1422 byte :: Int -> Word32 -> Word32
1423 byte 0 w = w .&. 0xFF
1424 byte 1 w = (w `shiftR` 8) .&. 0xFF
1425 byte 2 w = (w `shiftR` 16) .&. 0xFF
1426 byte 3 w = (w `shiftR` 24) .&. 0xFF
1429 vecret_entry 0 = stg_interp_constr1_entry
1430 vecret_entry 1 = stg_interp_constr2_entry
1431 vecret_entry 2 = stg_interp_constr3_entry
1432 vecret_entry 3 = stg_interp_constr4_entry
1433 vecret_entry 4 = stg_interp_constr5_entry
1434 vecret_entry 5 = stg_interp_constr6_entry
1435 vecret_entry 6 = stg_interp_constr7_entry
1436 vecret_entry 7 = stg_interp_constr8_entry
1438 -- entry point for direct returns for created constr itbls
1439 foreign label "stg_interp_constr_entry" stg_interp_constr_entry :: Addr
1440 -- and the 8 vectored ones
1441 foreign label "stg_interp_constr1_entry" stg_interp_constr1_entry :: Addr
1442 foreign label "stg_interp_constr2_entry" stg_interp_constr2_entry :: Addr
1443 foreign label "stg_interp_constr3_entry" stg_interp_constr3_entry :: Addr
1444 foreign label "stg_interp_constr4_entry" stg_interp_constr4_entry :: Addr
1445 foreign label "stg_interp_constr5_entry" stg_interp_constr5_entry :: Addr
1446 foreign label "stg_interp_constr6_entry" stg_interp_constr6_entry :: Addr
1447 foreign label "stg_interp_constr7_entry" stg_interp_constr7_entry :: Addr
1448 foreign label "stg_interp_constr8_entry" stg_interp_constr8_entry :: Addr
1452 data Constructor = Constructor Int{-ptrs-} Int{-nptrs-}
1455 -- Ultra-minimalist version specially for constructors
1456 data StgInfoTable = StgInfoTable {
1461 code0, code1, code2, code3, code4, code5, code6, code7 :: Word8
1465 instance Storable StgInfoTable where
1468 = (sum . map (\f -> f itbl))
1469 [fieldSz ptrs, fieldSz nptrs, fieldSz srtlen, fieldSz tipe,
1470 fieldSz code0, fieldSz code1, fieldSz code2, fieldSz code3,
1471 fieldSz code4, fieldSz code5, fieldSz code6, fieldSz code7]
1474 = (sum . map (\f -> f itbl))
1475 [fieldAl ptrs, fieldAl nptrs, fieldAl srtlen, fieldAl tipe,
1476 fieldAl code0, fieldAl code1, fieldAl code2, fieldAl code3,
1477 fieldAl code4, fieldAl code5, fieldAl code6, fieldAl code7]
1480 = do a1 <- store (ptrs itbl) (castPtr a0)
1481 a2 <- store (nptrs itbl) a1
1482 a3 <- store (tipe itbl) a2
1483 a4 <- store (srtlen itbl) a3
1484 a5 <- store (code0 itbl) a4
1485 a6 <- store (code1 itbl) a5
1486 a7 <- store (code2 itbl) a6
1487 a8 <- store (code3 itbl) a7
1488 a9 <- store (code4 itbl) a8
1489 aA <- store (code5 itbl) a9
1490 aB <- store (code6 itbl) aA
1491 aC <- store (code7 itbl) aB
1495 = do (a1,ptrs) <- load (castPtr a0)
1496 (a2,nptrs) <- load a1
1497 (a3,tipe) <- load a2
1498 (a4,srtlen) <- load a3
1499 (a5,code0) <- load a4
1500 (a6,code1) <- load a5
1501 (a7,code2) <- load a6
1502 (a8,code3) <- load a7
1503 (a9,code4) <- load a8
1504 (aA,code5) <- load a9
1505 (aB,code6) <- load aA
1506 (aC,code7) <- load aB
1507 return StgInfoTable { ptrs = ptrs, nptrs = nptrs,
1508 srtlen = srtlen, tipe = tipe,
1509 code0 = code0, code1 = code1, code2 = code2,
1510 code3 = code3, code4 = code4, code5 = code5,
1511 code6 = code6, code7 = code7 }
1513 fieldSz :: (Storable a, Storable b) => (a -> b) -> a -> Int
1514 fieldSz sel x = sizeOf (sel x)
1516 fieldAl :: (Storable a, Storable b) => (a -> b) -> a -> Int
1517 fieldAl sel x = alignment (sel x)
1519 store :: Storable a => a -> Ptr a -> IO (Ptr b)
1520 store x addr = do poke addr x
1521 return (castPtr (addr `plusPtr` sizeOf x))
1523 load :: Storable a => Ptr a -> IO (Ptr b, a)
1524 load addr = do x <- peek addr
1525 return (castPtr (addr `plusPtr` sizeOf x), x)
1529 %************************************************************************
1531 \subsection{Connect to actual values for bytecode opcodes}
1533 %************************************************************************
1537 #include "Bytecodes.h"
1539 i_ARGCHECK = (bci_ARGCHECK :: Int)
1540 i_PUSH_L = (bci_PUSH_L :: Int)
1541 i_PUSH_LL = (bci_PUSH_LL :: Int)
1542 i_PUSH_LLL = (bci_PUSH_LLL :: Int)
1543 i_PUSH_G = (bci_PUSH_G :: Int)
1544 i_PUSH_AS = (bci_PUSH_AS :: Int)
1545 i_PUSH_UBX = (bci_PUSH_UBX :: Int)
1546 i_PUSH_TAG = (bci_PUSH_TAG :: Int)
1547 i_SLIDE = (bci_SLIDE :: Int)
1548 i_ALLOC = (bci_ALLOC :: Int)
1549 i_MKAP = (bci_MKAP :: Int)
1550 i_UNPACK = (bci_UNPACK :: Int)
1551 i_UPK_TAG = (bci_UPK_TAG :: Int)
1552 i_PACK = (bci_PACK :: Int)
1553 i_LABEL = (bci_LABEL :: Int)
1554 i_TESTLT_I = (bci_TESTLT_I :: Int)
1555 i_TESTEQ_I = (bci_TESTEQ_I :: Int)
1556 i_TESTLT_F = (bci_TESTLT_F :: Int)
1557 i_TESTEQ_F = (bci_TESTEQ_F :: Int)
1558 i_TESTLT_D = (bci_TESTLT_D :: Int)
1559 i_TESTEQ_D = (bci_TESTEQ_D :: Int)
1560 i_TESTLT_P = (bci_TESTLT_P :: Int)
1561 i_TESTEQ_P = (bci_TESTEQ_P :: Int)
1562 i_CASEFAIL = (bci_CASEFAIL :: Int)
1563 i_ENTER = (bci_ENTER :: Int)
1564 i_RETURN = (bci_RETURN :: Int)