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, mkSysLocalName, toRdrName )
17 import RdrName ( rdrNameOcc, rdrNameModule )
18 import OccName ( occNameString )
19 import Id ( Id, idType, isDataConId_maybe, mkVanillaId )
20 import OrdList ( OrdList, consOL, snocOL, appOL, unitOL,
21 nilOL, toOL, concatOL, fromOL )
22 import FiniteMap ( FiniteMap, addListToFM, listToFM, filterFM,
23 addToFM, lookupFM, fmToList, emptyFM, plusFM )
25 import PprCore ( pprCoreExpr, pprCoreAlt )
26 import Literal ( Literal(..), literalPrimRep )
27 import PrimRep ( PrimRep(..) )
28 import CoreFVs ( freeVars )
29 import Type ( typePrimRep )
30 import DataCon ( DataCon, dataConTag, fIRST_TAG, dataConTyCon,
32 import TyCon ( TyCon, tyConFamilySize, isDataTyCon, tyConDataCons )
33 import Class ( Class, classTyCon )
34 import Util ( zipEqual, zipWith4Equal, naturalMergeSortLe, nOfThem, global )
35 import Var ( isTyVar )
36 import VarSet ( VarSet, varSetElems )
37 import PrimRep ( getPrimRepSize, isFollowableRep )
38 import Constants ( wORD_SIZE )
39 import CmdLineOpts ( DynFlags, DynFlag(..) )
40 import ErrUtils ( showPass, dumpIfSet_dyn )
41 import ClosureInfo ( mkVirtHeapOffsets )
42 import Module ( ModuleName, moduleName, moduleNameFS )
43 import Unique ( mkPseudoUnique3 )
44 import Linker ( lookupSymbol )
45 import FastString ( FastString(..) )
48 import List ( intersperse )
49 import Monad ( foldM )
51 import MArray ( castSTUArray,
52 newFloatArray, writeFloatArray,
53 newDoubleArray, writeDoubleArray,
54 newIntArray, writeIntArray,
55 newAddrArray, writeAddrArray )
56 import Foreign ( Storable(..), Word8, Word16, Word32, Ptr(..),
57 malloc, castPtr, plusPtr, mallocBytes )
58 import Addr ( Word, addrToInt, writeCharOffAddr )
59 import Bits ( Bits(..), shiftR )
60 import CTypes ( CInt )
62 import PrelBase ( Int(..) )
63 import PrelAddr ( Addr(..) )
64 import PrelGHC ( BCO#, newBCO#, unsafeCoerce#,
65 ByteArray#, Array#, addrToHValue# )
66 import IOExts ( IORef, fixIO, unsafePerformIO )
68 import PrelArr ( Array(..) )
69 import PrelIOBase ( IO(..) )
73 %************************************************************************
75 \subsection{Functions visible from outside this module.}
77 %************************************************************************
81 byteCodeGen :: DynFlags
84 -> IO ([UnlinkedBCO], ItblEnv)
85 byteCodeGen dflags binds local_tycons local_classes
86 = do showPass dflags "ByteCodeGen"
87 let tycs = local_tycons ++ map classTyCon local_classes
88 itblenv <- mkITbls tycs
90 let flatBinds = concatMap getBind binds
91 getBind (NonRec bndr rhs) = [(bndr, freeVars rhs)]
92 getBind (Rec binds) = [(bndr, freeVars rhs) | (bndr,rhs) <- binds]
93 final_state = runBc (BcM_State [] 0)
94 (mapBc schemeR flatBinds `thenBc_` returnBc ())
95 (BcM_State proto_bcos final_ctr) = final_state
97 dumpIfSet_dyn dflags Opt_D_dump_BCOs
98 "Proto-bcos" (vcat (intersperse (char ' ') (map ppr proto_bcos)))
100 bcos <- mapM assembleBCO proto_bcos
102 return (bcos, itblenv)
105 -- Returns: (the root BCO for this expression,
106 -- a list of auxilary BCOs resulting from compiling closures)
107 coreExprToBCOs :: DynFlags
109 -> IO UnlinkedBCOExpr
110 coreExprToBCOs dflags expr
111 = do showPass dflags "ByteCodeGen"
113 -- create a totally bogus name for the top-level BCO; this
114 -- should be harmless, since it's never used for anything
115 let invented_name = mkSysLocalName (mkPseudoUnique3 0) SLIT("Expr-Top-Level")
116 let invented_id = mkVanillaId invented_name (panic "invented_id's type")
118 let (BcM_State all_proto_bcos final_ctr)
119 = runBc (BcM_State [] 0)
120 (schemeR (invented_id, freeVars expr))
121 dumpIfSet_dyn dflags Opt_D_dump_BCOs
122 "Proto-bcos" (vcat (intersperse (char ' ') (map ppr all_proto_bcos)))
125 = case filter ((== invented_name).nameOfProtoBCO) all_proto_bcos of
126 [root_bco] -> root_bco
128 = filter ((/= invented_name).nameOfProtoBCO) all_proto_bcos
130 auxiliary_bcos <- mapM assembleBCO auxiliary_proto_bcos
131 root_bco <- assembleBCO root_proto_bco
133 return (root_bco, auxiliary_bcos)
137 linkIModules :: ItblEnv -- incoming global itbl env; returned updated
138 -> ClosureEnv -- incoming global closure env; returned updated
139 -> [([UnlinkedBCO], ItblEnv)]
140 -> IO ([HValue], ItblEnv, ClosureEnv)
141 linkIModules gie gce mods
142 = do let (bcoss, ies) = unzip mods
144 final_gie = foldr plusFM gie ies
145 (final_gce, linked_bcos) <- linkSomeBCOs final_gie gce bcos
146 return (linked_bcos, final_gie, final_gce)
149 linkIExpr :: ItblEnv -> ClosureEnv -> UnlinkedBCOExpr
150 -> IO HValue -- IO BCO# really
151 linkIExpr ie ce (root_ul_bco, aux_ul_bcos)
152 = do (aux_ce, _) <- linkSomeBCOs ie ce aux_ul_bcos
153 (_, [root_bco]) <- linkSomeBCOs ie aux_ce [root_ul_bco]
156 -- Link a bunch of BCOs and return them + updated closure env.
157 linkSomeBCOs :: ItblEnv -> ClosureEnv -> [UnlinkedBCO]
158 -> IO (ClosureEnv, [HValue])
159 linkSomeBCOs ie ce_in ul_bcos
160 = do let nms = map nameOfUnlinkedBCO ul_bcos
162 ( \ hvs -> let ce_out = addListToFM ce_in (zipLazily nms hvs)
163 in mapM (linkBCO ie ce_out) ul_bcos )
164 let ce_out = addListToFM ce_in (zip nms hvals)
165 return (ce_out, hvals)
167 -- A lazier zip, in which no demand is propagated to the second
168 -- list unless some demand is propagated to the snd of one of the
169 -- result list elems.
171 zipLazily (x:xs) ys = (x, head ys) : zipLazily xs (tail ys)
176 (SizedSeq Word16) -- insns
177 (SizedSeq Word) -- literals
178 (SizedSeq Name) -- ptrs
179 (SizedSeq Name) -- itbl refs
181 nameOfUnlinkedBCO (UnlinkedBCO nm _ _ _ _) = nm
183 -- When translating expressions, we need to distinguish the root
184 -- BCO for the expression
185 type UnlinkedBCOExpr = (UnlinkedBCO, [UnlinkedBCO])
187 instance Outputable UnlinkedBCO where
188 ppr (UnlinkedBCO nm insns lits ptrs itbls)
189 = sep [text "BCO", ppr nm, text "with",
190 int (sizeSS insns), text "insns",
191 int (sizeSS lits), text "lits",
192 int (sizeSS ptrs), text "ptrs",
193 int (sizeSS itbls), text "itbls"]
196 -- these need a proper home
197 type ItblEnv = FiniteMap Name (Ptr StgInfoTable)
198 type ClosureEnv = FiniteMap Name HValue
199 data HValue = HValue -- dummy type, actually a pointer to some Real Code.
201 -- remove all entries for a given set of modules from the environment
202 filterNameMap :: [ModuleName] -> FiniteMap Name a -> FiniteMap Name a
203 filterNameMap mods env
204 = filterFM (\n _ -> moduleName (nameModule n) `notElem` mods) env
207 %************************************************************************
209 \subsection{Bytecodes, and Outputery.}
211 %************************************************************************
215 type LocalLabel = Int
218 -- Messing with the stack
220 -- Push locals (existing bits of the stack)
221 | PUSH_L Int{-offset-}
222 | PUSH_LL Int Int{-2 offsets-}
223 | PUSH_LLL Int Int Int{-3 offsets-}
226 -- Push an alt continuation
227 | PUSH_AS Name PrimRep -- push alts and BCO_ptr_ret_info
228 -- PrimRep so we know which itbl
230 | PUSH_UBX Literal Int
231 -- push this int/float/double, NO TAG, on the stack
232 -- Int is # of words to copy from literal pool
233 | PUSH_TAG Int -- push this tag on the stack
235 | SLIDE Int{-this many-} Int{-down by this much-}
236 -- To do with the heap
237 | ALLOC Int -- make an AP_UPD with this many payload words, zeroed
238 | MKAP Int{-ptr to AP_UPD is this far down stack-} Int{-# words-}
239 | UNPACK Int -- unpack N ptr words from t.o.s Constr
240 | UPK_TAG Int Int Int
241 -- unpack N non-ptr words from offset M in constructor
242 -- K words down the stack
244 -- after assembly, the DataCon is an index into the
246 -- For doing case trees
248 | TESTLT_I Int LocalLabel
249 | TESTEQ_I Int LocalLabel
250 | TESTLT_F Float LocalLabel
251 | TESTEQ_F Float LocalLabel
252 | TESTLT_D Double LocalLabel
253 | TESTEQ_D Double LocalLabel
255 -- The Int value is a constructor number and therefore
256 -- stored in the insn stream rather than as an offset into
258 | TESTLT_P Int LocalLabel
259 | TESTEQ_P Int LocalLabel
262 -- To Infinity And Beyond
265 -- unboxed value on TOS. Use tag to find underlying ret itbl
266 -- and return as per that.
269 instance Outputable BCInstr where
270 ppr (ARGCHECK n) = text "ARGCHECK" <+> int n
271 ppr (PUSH_L offset) = text "PUSH_L " <+> int offset
272 ppr (PUSH_LL o1 o2) = text "PUSH_LL " <+> int o1 <+> int o2
273 ppr (PUSH_LLL o1 o2 o3) = text "PUSH_LLL" <+> int o1 <+> int o2 <+> int o3
274 ppr (PUSH_G nm) = text "PUSH_G " <+> ppr nm
275 ppr (PUSH_AS nm pk) = text "PUSH_AS " <+> ppr nm <+> ppr pk
276 ppr (PUSH_UBX lit nw) = text "PUSH_UBX" <+> parens (int nw) <+> ppr lit
277 ppr (PUSH_TAG n) = text "PUSH_TAG" <+> int n
278 ppr (SLIDE n d) = text "SLIDE " <+> int n <+> int d
279 ppr (ALLOC sz) = text "ALLOC " <+> int sz
280 ppr (MKAP offset sz) = text "MKAP " <+> int sz <+> text "words,"
281 <+> int offset <+> text "stkoff"
282 ppr (UNPACK sz) = text "UNPACK " <+> int sz
283 ppr (UPK_TAG n m k) = text "UPK_TAG " <+> int n <> text "words"
284 <+> int m <> text "conoff"
285 <+> int k <> text "stkoff"
286 ppr (PACK dcon sz) = text "PACK " <+> ppr dcon <+> ppr sz
287 ppr (LABEL lab) = text "__" <> int lab <> colon
288 ppr (TESTLT_I i lab) = text "TESTLT_I" <+> int i <+> text "__" <> int lab
289 ppr (TESTEQ_I i lab) = text "TESTEQ_I" <+> int i <+> text "__" <> int lab
290 ppr (TESTLT_F f lab) = text "TESTLT_F" <+> float f <+> text "__" <> int lab
291 ppr (TESTEQ_F f lab) = text "TESTEQ_F" <+> float f <+> text "__" <> int lab
292 ppr (TESTLT_D d lab) = text "TESTLT_D" <+> double d <+> text "__" <> int lab
293 ppr (TESTEQ_D d lab) = text "TESTEQ_D" <+> double d <+> text "__" <> int lab
294 ppr (TESTLT_P i lab) = text "TESTLT_P" <+> int i <+> text "__" <> int lab
295 ppr (TESTEQ_P i lab) = text "TESTEQ_P" <+> int i <+> text "__" <> int lab
296 ppr CASEFAIL = text "CASEFAIL"
297 ppr ENTER = text "ENTER"
298 ppr (RETURN pk) = text "RETURN " <+> ppr pk
300 instance Outputable a => Outputable (ProtoBCO a) where
301 ppr (ProtoBCO name instrs origin)
302 = (text "ProtoBCO" <+> ppr name <> colon)
303 $$ nest 6 (vcat (map ppr instrs))
305 Left alts -> vcat (map (pprCoreAlt.deAnnAlt) alts)
306 Right rhs -> pprCoreExpr (deAnnotate rhs)
309 %************************************************************************
311 \subsection{Compilation schema for the bytecode generator.}
313 %************************************************************************
317 type BCInstrList = OrdList BCInstr
320 = ProtoBCO a -- name, in some sense
322 -- what the BCO came from
323 (Either [AnnAlt Id VarSet]
326 nameOfProtoBCO (ProtoBCO nm insns origin) = nm
329 type Sequel = Int -- back off to this depth before ENTER
331 -- Maps Ids to the offset from the stack _base_ so we don't have
332 -- to mess with it after each push/pop.
333 type BCEnv = FiniteMap Id Int -- To find vars on the stack
336 -- Create a BCO and do a spot of peephole optimisation on the insns
338 mkProtoBCO nm instrs_ordlist origin
339 = ProtoBCO nm (id {-peep-} (fromOL instrs_ordlist)) origin
341 peep (PUSH_L off1 : PUSH_L off2 : PUSH_L off3 : rest)
342 = PUSH_LLL off1 (off2-1) (off3-2) : peep rest
343 peep (PUSH_L off1 : PUSH_L off2 : rest)
344 = PUSH_LL off1 off2 : peep rest
351 -- Compile code for the right hand side of a let binding.
352 -- Park the resulting BCO in the monad. Also requires the
353 -- variable to which this value was bound, so as to give the
354 -- resulting BCO a name.
355 schemeR :: (Id, AnnExpr Id VarSet) -> BcM ()
360 $$ (ppr.filter (not.isTyVar).varSetElems.fst) rhs
361 $$ pprCoreExpr (deAnnotate rhs)
367 = schemeR_wrk rhs nm (collect [] rhs)
370 collect xs (_, AnnNote note e)
372 collect xs (_, AnnLam x e)
373 = collect (if isTyVar x then xs else (x:xs)) e
374 collect xs not_lambda
375 = (reverse xs, not_lambda)
377 schemeR_wrk original_body nm (args, body)
378 = let fvs = filter (not.isTyVar) (varSetElems (fst original_body))
379 all_args = reverse args ++ fvs --ORIG: fvs ++ reverse args
380 szsw_args = map taggedIdSizeW all_args
381 szw_args = sum szsw_args
382 p_init = listToFM (zip all_args (mkStackOffsets 0 szsw_args))
383 argcheck = --if null args then nilOL else
384 unitOL (ARGCHECK szw_args)
386 schemeE szw_args 0 p_init body `thenBc` \ body_code ->
387 emitBc (mkProtoBCO (getName nm) (appOL argcheck body_code) (Right original_body))
389 -- Let szsw be the sizes in words of some items pushed onto the stack,
390 -- which has initial depth d'. Return the values which the stack environment
391 -- should map these items to.
392 mkStackOffsets :: Int -> [Int] -> [Int]
393 mkStackOffsets original_depth szsw
394 = map (subtract 1) (tail (scanl (+) original_depth szsw))
396 -- Compile code to apply the given expression to the remaining args
397 -- on the stack, returning a HNF.
398 schemeE :: Int -> Sequel -> BCEnv -> AnnExpr Id VarSet -> BcM BCInstrList
400 -- Delegate tail-calls to schemeT.
401 schemeE d s p e@(fvs, AnnApp f a)
402 = returnBc (schemeT (should_args_be_tagged e) d s 0 p (fvs, AnnApp f a))
403 schemeE d s p e@(fvs, AnnVar v)
404 | isFollowableRep v_rep
405 = returnBc (schemeT (should_args_be_tagged e) d s 0 p (fvs, AnnVar v))
407 = -- returning an unboxed value. Heave it on the stack, SLIDE, and RETURN.
408 let (push, szw) = pushAtom True d p (AnnVar v)
409 in returnBc (push -- value onto stack
410 `appOL` mkSLIDE szw (d-s) -- clear to sequel
411 `snocOL` RETURN v_rep) -- go
413 v_rep = typePrimRep (idType v)
415 schemeE d s p (fvs, AnnLit literal)
416 = let (push, szw) = pushAtom True d p (AnnLit literal)
417 l_rep = literalPrimRep literal
418 in returnBc (push -- value onto stack
419 `appOL` mkSLIDE szw (d-s) -- clear to sequel
420 `snocOL` RETURN l_rep) -- go
422 schemeE d s p (fvs, AnnLet binds b)
423 = let (xs,rhss) = case binds of AnnNonRec x rhs -> ([x],[rhs])
424 AnnRec xs_n_rhss -> unzip xs_n_rhss
426 fvss = map (filter (not.isTyVar).varSetElems.fst) rhss
428 -- Sizes of tagged free vars, + 1 for the fn
429 sizes = map (\rhs_fvs -> 1 + sum (map taggedIdSizeW rhs_fvs)) fvss
431 -- This p', d' defn is safe because all the items being pushed
432 -- are ptrs, so all have size 1. d' and p' reflect the stack
433 -- after the closures have been allocated in the heap (but not
434 -- filled in), and pointers to them parked on the stack.
435 p' = addListToFM p (zipE xs (mkStackOffsets d (nOfThem n 1)))
438 infos = zipE4 fvss sizes xs [n, n-1 .. 1]
439 zipE = zipEqual "schemeE"
440 zipE4 = zipWith4Equal "schemeE" (\a b c d -> (a,b,c,d))
442 -- ToDo: don't build thunks for things with no free variables
443 buildThunk dd ([], size, id, off)
444 = PUSH_G (getName id)
445 `consOL` unitOL (MKAP (off+size-1) size)
446 buildThunk dd ((fv:fvs), size, id, off)
447 = case pushAtom True dd p' (AnnVar fv) of
448 (push_code, pushed_szw)
450 buildThunk (dd+pushed_szw) (fvs, size, id, off)
452 thunkCode = concatOL (map (buildThunk d') infos)
453 allocCode = toOL (map ALLOC sizes)
455 schemeE d' s p' b `thenBc` \ bodyCode ->
456 mapBc schemeR (zip xs rhss) `thenBc_`
457 returnBc (allocCode `appOL` thunkCode `appOL` bodyCode)
460 schemeE d s p (fvs, AnnCase scrut bndr alts)
462 -- Top of stack is the return itbl, as usual.
463 -- underneath it is the pointer to the alt_code BCO.
464 -- When an alt is entered, it assumes the returned value is
465 -- on top of the itbl.
468 -- Env and depth in which to compile the alts, not including
469 -- any vars bound by the alts themselves
470 d' = d + ret_frame_sizeW + taggedIdSizeW bndr
471 p' = addToFM p bndr (d' - 1)
473 scrut_primrep = typePrimRep (idType bndr)
475 = case scrut_primrep of
476 CharRep -> False ; AddrRep -> False
477 IntRep -> False ; FloatRep -> False ; DoubleRep -> False
479 other -> pprPanic "ByteCodeGen.schemeE" (ppr other)
481 -- given an alt, return a discr and code for it.
482 codeAlt alt@(discr, binds_f, rhs)
484 = let binds_r = reverse binds_f
485 binds_r_t_szsw = map taggedIdSizeW binds_r
486 binds_t_szw = sum binds_r_t_szsw
488 p' (zip binds_r (mkStackOffsets d' binds_r_t_szsw))
489 d'' = d' + binds_t_szw
490 unpack_code = mkUnpackCode {-0 0-} (map (typePrimRep.idType) binds_f)
491 in schemeE d'' s p'' rhs `thenBc` \ rhs_code ->
492 returnBc (my_discr alt, unpack_code `appOL` rhs_code)
494 = ASSERT(null binds_f)
495 schemeE d' s p' rhs `thenBc` \ rhs_code ->
496 returnBc (my_discr alt, rhs_code)
498 my_discr (DEFAULT, binds, rhs) = NoDiscr
499 my_discr (DataAlt dc, binds, rhs) = DiscrP (dataConTag dc - fIRST_TAG)
500 my_discr (LitAlt l, binds, rhs)
501 = case l of MachInt i -> DiscrI (fromInteger i)
502 MachFloat r -> DiscrF (fromRational r)
503 MachDouble r -> DiscrD (fromRational r)
506 | not isAlgCase = Nothing
508 = case [dc | (DataAlt dc, _, _) <- alts] of
510 (dc:_) -> Just (tyConFamilySize (dataConTyCon dc))
513 mapBc codeAlt alts `thenBc` \ alt_stuff ->
514 mkMultiBranch maybe_ncons alt_stuff `thenBc` \ alt_final ->
516 alt_final_ac = ARGCHECK (taggedIdSizeW bndr) `consOL` alt_final
517 alt_bco_name = getName bndr
518 alt_bco = mkProtoBCO alt_bco_name alt_final_ac (Left alts)
520 schemeE (d + ret_frame_sizeW)
521 (d + ret_frame_sizeW) p scrut `thenBc` \ scrut_code ->
523 emitBc alt_bco `thenBc_`
524 returnBc (PUSH_AS alt_bco_name scrut_primrep `consOL` scrut_code)
527 schemeE d s p (fvs, AnnNote note body)
531 = pprPanic "ByteCodeGen.schemeE: unhandled case"
532 (pprCoreExpr (deAnnotate other))
535 -- Compile code to do a tail call. Doesn't need to be monadic.
536 schemeT :: Bool -- do tagging?
537 -> Int -- Stack depth
538 -> Sequel -- Sequel depth
539 -> Int -- # arg words so far
540 -> BCEnv -- stack env
544 schemeT enTag d s narg_words p (_, AnnApp f a)
546 AnnType _ -> schemeT enTag d s narg_words p f
548 -> let (push, arg_words) = pushAtom enTag d p (snd a)
550 `appOL` schemeT enTag (d+arg_words) s (narg_words+arg_words) p f
552 schemeT enTag d s narg_words p (_, AnnVar f)
553 | Just con <- isDataConId_maybe f
554 = ASSERT(enTag == False)
555 --trace ("schemeT: d = " ++ show d ++ ", s = " ++ show s ++ ", naw = " ++ show narg_words) (
556 PACK con narg_words `consOL` (mkSLIDE 1 (d - narg_words - s) `snocOL` ENTER)
559 = ASSERT(enTag == True)
560 let (push, arg_words) = pushAtom True d p (AnnVar f)
562 `appOL` mkSLIDE (narg_words+arg_words) (d - s - narg_words)
566 = if d == 0 then nilOL else unitOL (SLIDE n d)
568 should_args_be_tagged (_, AnnVar v)
569 = case isDataConId_maybe v of
570 Just dcon -> False; Nothing -> True
571 should_args_be_tagged (_, AnnApp f a)
572 = should_args_be_tagged f
573 should_args_be_tagged (_, other)
574 = panic "should_args_be_tagged: tail call to non-con, non-var"
577 -- Make code to unpack the top-of-stack constructor onto the stack,
578 -- adding tags for the unboxed bits. Takes the PrimReps of the
579 -- constructor's arguments. off_h and off_s are travelling offsets
580 -- along the constructor and the stack.
581 mkUnpackCode :: [PrimRep] -> BCInstrList
585 all_code = ptrs_code `appOL` do_nptrs ptrs_szw ptrs_szw reps_nptr
587 reps_ptr = filter isFollowableRep reps
588 reps_nptr = filter (not.isFollowableRep) reps
590 ptrs_szw = sum (map untaggedSizeW reps_ptr)
591 ptrs_code | null reps_ptr = nilOL
592 | otherwise = unitOL (UNPACK ptrs_szw)
594 do_nptrs off_h off_s [] = nilOL
595 do_nptrs off_h off_s (npr:nprs)
597 IntRep -> approved ; FloatRep -> approved
598 DoubleRep -> approved ; AddrRep -> approved
599 _ -> pprPanic "ByteCodeGen.mkUnpackCode" (ppr npr)
601 approved = UPK_TAG usizeW off_h off_s `consOL` theRest
602 theRest = do_nptrs (off_h + usizeW) (off_s + tsizeW) nprs
603 usizeW = untaggedSizeW npr
604 tsizeW = taggedSizeW npr
607 -- Push an atom onto the stack, returning suitable code & number of
608 -- stack words used. Pushes it either tagged or untagged, since
609 -- pushAtom is used to set up the stack prior to copying into the
610 -- heap for both APs (requiring tags) and constructors (which don't).
612 -- NB this means NO GC between pushing atoms for a constructor and
613 -- copying them into the heap. It probably also means that
614 -- tail calls MUST be of the form atom{atom ... atom} since if the
615 -- expression head was allowed to be arbitrary, there could be GC
616 -- in between pushing the arg atoms and completing the head.
617 -- (not sure; perhaps the allocate/doYouWantToGC interface means this
618 -- isn't a problem; but only if arbitrary graph construction for the
619 -- head doesn't leave this BCO, since GC might happen at the start of
620 -- each BCO (we consult doYouWantToGC there).
622 -- Blargh. JRS 001206
624 -- NB (further) that the env p must map each variable to the highest-
625 -- numbered stack slot for it. For example, if the stack has depth 4
626 -- and we tagged-ly push (v :: Int#) on it, the value will be in stack[4],
627 -- the tag in stack[5], the stack will have depth 6, and p must map v to
628 -- 5 and not to 4. Stack locations are numbered from zero, so a depth
629 -- 6 stack has valid words 0 .. 5.
631 pushAtom :: Bool -> Int -> BCEnv -> AnnExpr' Id VarSet -> (BCInstrList, Int)
632 pushAtom tagged d p (AnnVar v)
633 = let str = "\npushAtom " ++ showSDocDebug (ppr v) ++ ", depth = " ++ show d
635 showSDocDebug (nest 4 (vcat (map ppr (fmToList p))))
637 showSDoc (nest 4 (vcat (map ppr (fromOL (fst result)))))
638 ++ "\nendPushAtom " ++ showSDocDebug (ppr v)
639 str' = if str == str then str else str
642 = case lookupBCEnv_maybe p v of
643 Just d_v -> (toOL (nOfThem nwords (PUSH_L (d-d_v+sz_t-2))), sz_t)
644 Nothing -> ASSERT(sz_t == 1) (unitOL (PUSH_G nm), sz_t)
646 nm = case isDataConId_maybe v of
650 sz_t = taggedIdSizeW v
651 sz_u = untaggedIdSizeW v
652 nwords = if tagged then sz_t else sz_u
657 pushAtom True d p (AnnLit lit)
658 = let (ubx_code, ubx_size) = pushAtom False d p (AnnLit lit)
659 in (ubx_code `snocOL` PUSH_TAG ubx_size, 1 + ubx_size)
661 pushAtom False d p (AnnLit lit)
663 MachInt i -> code IntRep
664 MachFloat r -> code FloatRep
665 MachDouble r -> code DoubleRep
666 MachChar c -> code CharRep
667 MachStr s -> pushStr s
670 = let size_host_words = untaggedSizeW rep
671 in (unitOL (PUSH_UBX lit size_host_words), size_host_words)
674 = let mallocvilleAddr
679 -- sigh, a string in the heap is no good to us.
680 -- We need a static C pointer, since the type of
681 -- a string literal is Addr#. So, copy the string
682 -- into C land and introduce a memory leak
685 -- CAREFUL! Chars are 32 bits in ghc 4.09+
687 do a@(Ptr addr) <- mallocBytes (n+1)
688 strncpy a ba (fromIntegral n)
689 writeCharOffAddr addr n '\0'
692 _ -> panic "StgInterp.lit2expr: unhandled string constant type"
695 = MachInt (toInteger (addrToInt mallocvilleAddr))
697 -- Get the addr on the stack, untaggedly
698 (unitOL (PUSH_UBX addrLit 1), 1)
704 pushAtom tagged d p (AnnApp f (_, AnnType _))
705 = pushAtom tagged d p (snd f)
707 pushAtom tagged d p (AnnNote note e)
708 = pushAtom tagged d p (snd e)
710 pushAtom tagged d p other
711 = pprPanic "ByteCodeGen.pushAtom"
712 (pprCoreExpr (deAnnotate (undefined, other)))
714 foreign import "strncpy" strncpy :: Ptr a -> ByteArray# -> CInt -> IO ()
717 -- Given a bunch of alts code and their discrs, do the donkey work
718 -- of making a multiway branch using a switch tree.
719 -- What a load of hassle!
720 mkMultiBranch :: Maybe Int -- # datacons in tycon, if alg alt
721 -- a hint; generates better code
722 -- Nothing is always safe
723 -> [(Discr, BCInstrList)]
725 mkMultiBranch maybe_ncons raw_ways
726 = let d_way = filter (isNoDiscr.fst) raw_ways
727 notd_ways = naturalMergeSortLe
728 (\w1 w2 -> leAlt (fst w1) (fst w2))
729 (filter (not.isNoDiscr.fst) raw_ways)
731 mkTree :: [(Discr, BCInstrList)] -> Discr -> Discr -> BcM BCInstrList
732 mkTree [] range_lo range_hi = returnBc the_default
734 mkTree [val] range_lo range_hi
735 | range_lo `eqAlt` range_hi
738 = getLabelBc `thenBc` \ label_neq ->
739 returnBc (mkTestEQ (fst val) label_neq
741 `appOL` unitOL (LABEL label_neq)
742 `appOL` the_default))
744 mkTree vals range_lo range_hi
745 = let n = length vals `div` 2
746 vals_lo = take n vals
747 vals_hi = drop n vals
748 v_mid = fst (head vals_hi)
750 getLabelBc `thenBc` \ label_geq ->
751 mkTree vals_lo range_lo (dec v_mid) `thenBc` \ code_lo ->
752 mkTree vals_hi v_mid range_hi `thenBc` \ code_hi ->
753 returnBc (mkTestLT v_mid label_geq
755 `appOL` unitOL (LABEL label_geq)
759 = case d_way of [] -> unitOL CASEFAIL
762 -- None of these will be needed if there are no non-default alts
763 (mkTestLT, mkTestEQ, init_lo, init_hi)
765 = panic "mkMultiBranch: awesome foursome"
767 = case fst (head notd_ways) of {
768 DiscrI _ -> ( \(DiscrI i) fail_label -> TESTLT_I i fail_label,
769 \(DiscrI i) fail_label -> TESTEQ_I i fail_label,
772 DiscrF _ -> ( \(DiscrF f) fail_label -> TESTLT_F f fail_label,
773 \(DiscrF f) fail_label -> TESTEQ_F f fail_label,
776 DiscrD _ -> ( \(DiscrD d) fail_label -> TESTLT_D d fail_label,
777 \(DiscrD d) fail_label -> TESTEQ_D d fail_label,
780 DiscrP _ -> ( \(DiscrP i) fail_label -> TESTLT_P i fail_label,
781 \(DiscrP i) fail_label -> TESTEQ_P i fail_label,
786 (algMinBound, algMaxBound)
787 = case maybe_ncons of
789 Nothing -> (minBound, maxBound)
791 (DiscrI i1) `eqAlt` (DiscrI i2) = i1 == i2
792 (DiscrF f1) `eqAlt` (DiscrF f2) = f1 == f2
793 (DiscrD d1) `eqAlt` (DiscrD d2) = d1 == d2
794 (DiscrP i1) `eqAlt` (DiscrP i2) = i1 == i2
795 NoDiscr `eqAlt` NoDiscr = True
798 (DiscrI i1) `leAlt` (DiscrI i2) = i1 <= i2
799 (DiscrF f1) `leAlt` (DiscrF f2) = f1 <= f2
800 (DiscrD d1) `leAlt` (DiscrD d2) = d1 <= d2
801 (DiscrP i1) `leAlt` (DiscrP i2) = i1 <= i2
802 NoDiscr `leAlt` NoDiscr = True
805 isNoDiscr NoDiscr = True
808 dec (DiscrI i) = DiscrI (i-1)
809 dec (DiscrP i) = DiscrP (i-1)
810 dec other = other -- not really right, but if you
811 -- do cases on floating values, you'll get what you deserve
813 -- same snotty comment applies to the following
821 mkTree notd_ways init_lo init_hi
825 %************************************************************************
827 \subsection{Supporting junk for the compilation schemes}
829 %************************************************************************
833 -- Describes case alts
841 instance Outputable Discr where
842 ppr (DiscrI i) = int i
843 ppr (DiscrF f) = text (show f)
844 ppr (DiscrD d) = text (show d)
845 ppr (DiscrP i) = int i
846 ppr NoDiscr = text "DEF"
849 -- Find things in the BCEnv (the what's-on-the-stack-env)
850 -- See comment preceding pushAtom for precise meaning of env contents
851 --lookupBCEnv :: BCEnv -> Id -> Int
853 -- = case lookupFM env nm of
854 -- Nothing -> pprPanic "lookupBCEnv"
855 -- (ppr nm $$ char ' ' $$ vcat (map ppr (fmToList env)))
858 lookupBCEnv_maybe :: BCEnv -> Id -> Maybe Int
859 lookupBCEnv_maybe = lookupFM
862 -- When I push one of these on the stack, how much does Sp move by?
863 taggedSizeW :: PrimRep -> Int
865 | isFollowableRep pr = 1
866 | otherwise = 1{-the tag-} + getPrimRepSize pr
869 -- The plain size of something, without tag.
870 untaggedSizeW :: PrimRep -> Int
872 | isFollowableRep pr = 1
873 | otherwise = getPrimRepSize pr
876 taggedIdSizeW, untaggedIdSizeW :: Id -> Int
877 taggedIdSizeW = taggedSizeW . typePrimRep . idType
878 untaggedIdSizeW = untaggedSizeW . typePrimRep . idType
882 %************************************************************************
884 \subsection{The bytecode generator's monad}
886 %************************************************************************
890 = BcM_State { bcos :: [ProtoBCO Name], -- accumulates completed BCOs
891 nextlabel :: Int } -- for generating local labels
893 type BcM result = BcM_State -> (result, BcM_State)
895 runBc :: BcM_State -> BcM () -> BcM_State
896 runBc init_st m = case m init_st of { (r,st) -> st }
898 thenBc :: BcM a -> (a -> BcM b) -> BcM b
900 = case expr st of { (result, st') -> cont result st' }
902 thenBc_ :: BcM a -> BcM b -> BcM b
904 = case expr st of { (result, st') -> cont st' }
906 returnBc :: a -> BcM a
907 returnBc result st = (result, st)
909 mapBc :: (a -> BcM b) -> [a] -> BcM [b]
910 mapBc f [] = returnBc []
912 = f x `thenBc` \ r ->
913 mapBc f xs `thenBc` \ rs ->
916 emitBc :: ProtoBCO Name -> BcM ()
918 = ((), st{bcos = bco : bcos st})
920 getLabelBc :: BcM Int
922 = (nextlabel st, st{nextlabel = 1 + nextlabel st})
926 %************************************************************************
928 \subsection{The bytecode assembler}
930 %************************************************************************
932 The object format for bytecodes is: 16 bits for the opcode, and 16 for
933 each field -- so the code can be considered a sequence of 16-bit ints.
934 Each field denotes either a stack offset or number of items on the
935 stack (eg SLIDE), and index into the pointer table (eg PUSH_G), an
936 index into the literal table (eg PUSH_I/D/L), or a bytecode address in
940 -- Top level assembler fn.
941 assembleBCO :: ProtoBCO Name -> IO UnlinkedBCO
943 assembleBCO (ProtoBCO nm instrs origin)
945 -- pass 1: collect up the offsets of the local labels.
946 -- Remember that the first insn starts at offset 1 since offset 0
947 -- (eventually) will hold the total # of insns.
948 label_env = mkLabelEnv emptyFM 1 instrs
950 mkLabelEnv env i_offset [] = env
951 mkLabelEnv env i_offset (i:is)
953 = case i of LABEL n -> addToFM env n i_offset ; _ -> env
954 in mkLabelEnv new_env (i_offset + instrSize16s i) is
957 = case lookupFM label_env lab of
958 Just bco_offset -> bco_offset
959 Nothing -> pprPanic "assembleBCO.findLabel" (int lab)
961 do -- pass 2: generate the instruction, ptr and nonptr bits
962 insns <- return emptySS :: IO (SizedSeq Word16)
963 lits <- return emptySS :: IO (SizedSeq Word)
964 ptrs <- return emptySS :: IO (SizedSeq Name)
965 itbls <- return emptySS :: IO (SizedSeq Name)
966 let init_asm_state = (insns,lits,ptrs,itbls)
967 (final_insns, final_lits, final_ptrs, final_itbls)
968 <- mkBits findLabel init_asm_state instrs
970 return (UnlinkedBCO nm final_insns final_lits final_ptrs final_itbls)
972 -- instrs nonptrs ptrs itbls
973 type AsmState = (SizedSeq Word16, SizedSeq Word, SizedSeq Name, SizedSeq Name)
975 data SizedSeq a = SizedSeq !Int [a]
976 emptySS = SizedSeq 0 []
977 addToSS (SizedSeq n r_xs) x = return (SizedSeq (n+1) (x:r_xs))
978 addListToSS (SizedSeq n r_xs) xs
979 = return (SizedSeq (n + length xs) (reverse xs ++ r_xs))
980 sizeSS (SizedSeq n r_xs) = n
981 listFromSS (SizedSeq n r_xs) = return (reverse r_xs)
984 -- This is where all the action is (pass 2 of the assembler)
985 mkBits :: (Int -> Int) -- label finder
987 -> [BCInstr] -- instructions (in)
990 mkBits findLabel st proto_insns
991 = foldM doInstr st proto_insns
993 doInstr :: AsmState -> BCInstr -> IO AsmState
996 ARGCHECK n -> instr2 st i_ARGCHECK n
997 PUSH_L o1 -> instr2 st i_PUSH_L o1
998 PUSH_LL o1 o2 -> instr3 st i_PUSH_LL o1 o2
999 PUSH_LLL o1 o2 o3 -> instr4 st i_PUSH_LLL o1 o2 o3
1000 PUSH_G nm -> do (p, st2) <- ptr st nm
1001 instr2 st2 i_PUSH_G p
1002 PUSH_AS nm pk -> do (p, st2) <- ptr st nm
1003 (np, st3) <- ctoi_itbl st2 pk
1004 instr3 st3 i_PUSH_AS p np
1005 PUSH_UBX lit nws -> do (np, st2) <- literal st lit
1006 instr3 st2 i_PUSH_UBX np nws
1007 PUSH_TAG tag -> instr2 st i_PUSH_TAG tag
1008 SLIDE n by -> instr3 st i_SLIDE n by
1009 ALLOC n -> instr2 st i_ALLOC n
1010 MKAP off sz -> instr3 st i_MKAP off sz
1011 UNPACK n -> instr2 st i_UNPACK n
1012 UPK_TAG n m k -> instr4 st i_UPK_TAG n m k
1013 PACK dcon sz -> do (itbl_no,st2) <- itbl st dcon
1014 instr3 st2 i_PACK itbl_no sz
1015 LABEL lab -> return st
1016 TESTLT_I i l -> do (np, st2) <- int st i
1017 instr3 st2 i_TESTLT_I np (findLabel l)
1018 TESTEQ_I i l -> do (np, st2) <- int st i
1019 instr3 st2 i_TESTEQ_I np (findLabel l)
1020 TESTLT_F f l -> do (np, st2) <- float st f
1021 instr3 st2 i_TESTLT_F np (findLabel l)
1022 TESTEQ_F f l -> do (np, st2) <- float st f
1023 instr3 st2 i_TESTEQ_F np (findLabel l)
1024 TESTLT_D d l -> do (np, st2) <- double st d
1025 instr3 st2 i_TESTLT_D np (findLabel l)
1026 TESTEQ_D d l -> do (np, st2) <- double st d
1027 instr3 st2 i_TESTEQ_D np (findLabel l)
1028 TESTLT_P i l -> instr3 st i_TESTLT_P i (findLabel l)
1029 TESTEQ_P i l -> instr3 st i_TESTEQ_P i (findLabel l)
1030 CASEFAIL -> instr1 st i_CASEFAIL
1031 ENTER -> instr1 st i_ENTER
1032 RETURN rep -> do (itbl_no,st2) <- itoc_itbl st rep
1033 instr2 st2 i_RETURN itbl_no
1035 i2s :: Int -> Word16
1038 instr1 (st_i0,st_l0,st_p0,st_I0) i1
1039 = do st_i1 <- addToSS st_i0 (i2s i1)
1040 return (st_i1,st_l0,st_p0,st_I0)
1042 instr2 (st_i0,st_l0,st_p0,st_I0) i1 i2
1043 = do st_i1 <- addToSS st_i0 (i2s i1)
1044 st_i2 <- addToSS st_i1 (i2s i2)
1045 return (st_i2,st_l0,st_p0,st_I0)
1047 instr3 (st_i0,st_l0,st_p0,st_I0) i1 i2 i3
1048 = do st_i1 <- addToSS st_i0 (i2s i1)
1049 st_i2 <- addToSS st_i1 (i2s i2)
1050 st_i3 <- addToSS st_i2 (i2s i3)
1051 return (st_i3,st_l0,st_p0,st_I0)
1053 instr4 (st_i0,st_l0,st_p0,st_I0) i1 i2 i3 i4
1054 = do st_i1 <- addToSS st_i0 (i2s i1)
1055 st_i2 <- addToSS st_i1 (i2s i2)
1056 st_i3 <- addToSS st_i2 (i2s i3)
1057 st_i4 <- addToSS st_i3 (i2s i4)
1058 return (st_i4,st_l0,st_p0,st_I0)
1060 float (st_i0,st_l0,st_p0,st_I0) f
1061 = do let ws = mkLitF f
1062 st_l1 <- addListToSS st_l0 ws
1063 return (sizeSS st_l0, (st_i0,st_l1,st_p0,st_I0))
1065 double (st_i0,st_l0,st_p0,st_I0) d
1066 = do let ws = mkLitD d
1067 st_l1 <- addListToSS st_l0 ws
1068 return (sizeSS st_l0, (st_i0,st_l1,st_p0,st_I0))
1070 int (st_i0,st_l0,st_p0,st_I0) i
1071 = do let ws = mkLitI i
1072 st_l1 <- addListToSS st_l0 ws
1073 return (sizeSS st_l0, (st_i0,st_l1,st_p0,st_I0))
1075 addr (st_i0,st_l0,st_p0,st_I0) a
1076 = do let ws = mkLitA a
1077 st_l1 <- addListToSS st_l0 ws
1078 return (sizeSS st_l0, (st_i0,st_l1,st_p0,st_I0))
1080 ptr (st_i0,st_l0,st_p0,st_I0) p
1081 = do st_p1 <- addToSS st_p0 p
1082 return (sizeSS st_p0, (st_i0,st_l0,st_p1,st_I0))
1084 itbl (st_i0,st_l0,st_p0,st_I0) dcon
1085 = do st_I1 <- addToSS st_I0 (getName dcon)
1086 return (sizeSS st_I0, (st_i0,st_l0,st_p0,st_I1))
1088 literal st (MachInt j) = int st (fromIntegral j)
1089 literal st (MachFloat r) = float st (fromRational r)
1090 literal st (MachDouble r) = double st (fromRational r)
1091 literal st (MachChar c) = int st c
1094 = addr st ret_itbl_addr
1096 ret_itbl_addr = case pk of
1097 PtrRep -> stg_ctoi_ret_R1_info
1098 IntRep -> stg_ctoi_ret_R1_info
1099 CharRep -> stg_ctoi_ret_R1_info
1100 FloatRep -> stg_ctoi_ret_F1_info
1101 DoubleRep -> stg_ctoi_ret_D1_info
1102 _ -> pprPanic "mkBits.ctoi_itbl" (ppr pk)
1105 = addr st ret_itbl_addr
1107 ret_itbl_addr = case pk of
1108 IntRep -> stg_gc_unbx_r1_info
1109 FloatRep -> stg_gc_f1_info
1110 DoubleRep -> stg_gc_d1_info
1112 foreign label "stg_ctoi_ret_R1_info" stg_ctoi_ret_R1_info :: Addr
1113 foreign label "stg_ctoi_ret_F1_info" stg_ctoi_ret_F1_info :: Addr
1114 foreign label "stg_ctoi_ret_D1_info" stg_ctoi_ret_D1_info :: Addr
1116 foreign label "stg_gc_unbx_r1_info" stg_gc_unbx_r1_info :: Addr
1117 foreign label "stg_gc_f1_info" stg_gc_f1_info :: Addr
1118 foreign label "stg_gc_d1_info" stg_gc_d1_info :: Addr
1120 -- The size in 16-bit entities of an instruction.
1121 instrSize16s :: BCInstr -> Int
1152 -- Make lists of host-sized words for literals, so that when the
1153 -- words are placed in memory at increasing addresses, the
1154 -- bit pattern is correct for the host's word size and endianness.
1155 mkLitI :: Int -> [Word]
1156 mkLitF :: Float -> [Word]
1157 mkLitD :: Double -> [Word]
1158 mkLitA :: Addr -> [Word]
1162 arr <- newFloatArray ((0::Int),0)
1163 writeFloatArray arr 0 f
1164 f_arr <- castSTUArray arr
1165 w0 <- readWordArray f_arr 0
1172 arr <- newDoubleArray ((0::Int),1)
1173 writeDoubleArray arr 0 d
1174 d_arr <- castSTUArray arr
1175 w0 <- readWordArray d_arr 0
1176 w1 <- readWordArray d_arr 1
1181 arr <- newDoubleArray ((0::Int),0)
1182 writeDoubleArray arr 0 d
1183 d_arr <- castSTUArray arr
1184 w0 <- readWordArray d_arr 0
1190 arr <- newIntArray ((0::Int),0)
1191 writeIntArray arr 0 i
1192 i_arr <- castSTUArray arr
1193 w0 <- readWordArray i_arr 0
1199 arr <- newAddrArray ((0::Int),0)
1200 writeAddrArray arr 0 a
1201 a_arr <- castSTUArray arr
1202 w0 <- readWordArray a_arr 0
1208 %************************************************************************
1210 \subsection{Linking interpretables into something we can run}
1212 %************************************************************************
1217 data BCO# = BCO# ByteArray# -- instrs :: array Word16#
1218 ByteArray# -- literals :: array Word32#
1219 PtrArray# -- ptrs :: Array HValue
1220 ByteArray# -- itbls :: Array Addr#
1223 GLOBAL_VAR(v_cafTable, [], [HValue])
1225 --addCAF :: HValue -> IO ()
1226 --addCAF x = do xs <- readIORef v_cafTable; writeIORef v_cafTable (x:xs)
1228 --bcosToHValue :: ItblEnv -> ClosureEnv -> UnlinkedBCOExpr -> IO HValue
1229 --bcosToHValue ie ce (root_bco, other_bcos)
1230 -- = do linked_expr <- linkIExpr ie ce (root_bco, other_bcos)
1231 -- return linked_expr
1233 linkBCO ie ce (UnlinkedBCO nm insnsSS literalsSS ptrsSS itblsSS)
1234 = do insns <- listFromSS insnsSS
1235 literals <- listFromSS literalsSS
1236 ptrs <- listFromSS ptrsSS
1237 itbls <- listFromSS itblsSS
1239 linked_ptrs <- mapM (lookupCE ce) ptrs
1240 linked_itbls <- mapM (lookupIE ie) itbls
1242 let n_insns = sizeSS insnsSS
1243 n_literals = sizeSS literalsSS
1244 n_ptrs = sizeSS ptrsSS
1245 n_itbls = sizeSS itblsSS
1247 let ptrs_arr = array (0, n_ptrs-1) (indexify linked_ptrs)
1249 ptrs_parr = case ptrs_arr of Array lo hi parr -> parr
1251 itbls_arr = array (0, n_itbls-1) (indexify linked_itbls)
1253 itbls_barr = case itbls_arr of UArray lo hi barr -> barr
1255 insns_arr | n_insns > 65535
1256 = panic "linkBCO: >= 64k insns in BCO"
1258 = array (0, n_insns)
1259 (indexify (fromIntegral n_insns:insns))
1260 :: UArray Int Word16
1261 insns_barr = case insns_arr of UArray lo hi barr -> barr
1263 literals_arr = array (0, n_literals-1) (indexify literals)
1265 literals_barr = case literals_arr of UArray lo hi barr -> barr
1267 indexify :: [a] -> [(Int, a)]
1268 indexify xs = zip [0..] xs
1270 BCO bco# <- newBCO insns_barr literals_barr ptrs_parr itbls_barr
1272 return (unsafeCoerce# bco#)
1277 newBCO :: ByteArray# -> ByteArray# -> Array# a -> ByteArray# -> IO BCO
1279 = IO (\s -> case newBCO# a b c d s of (# s1, bco #) -> (# s1, BCO bco #))
1282 lookupCE :: ClosureEnv -> Name -> IO HValue
1284 = case lookupFM ce nm of
1285 Just aa -> return aa
1287 -> do m <- lookupSymbol (nameToCLabel nm "closure")
1289 Just (A# addr) -> case addrToHValue# addr of
1290 (# hval #) -> return hval
1291 Nothing -> pprPanic "ByteCodeGen.lookupCE" (ppr nm)
1293 lookupIE :: ItblEnv -> Name -> IO Addr
1295 = case lookupFM ie con_nm of
1296 Just (Ptr a) -> return a
1298 -> do -- try looking up in the object files.
1299 m <- lookupSymbol (nameToCLabel con_nm "con_info")
1301 Just addr -> return addr
1303 -> do -- perhaps a nullary constructor?
1304 n <- lookupSymbol (nameToCLabel con_nm "static_info")
1306 Just addr -> return addr
1307 Nothing -> pprPanic "ByteCodeGen.lookupIE" (ppr con_nm)
1309 -- HACK!!! ToDo: cleaner
1310 nameToCLabel :: Name -> String{-suffix-} -> String
1311 nameToCLabel n suffix
1312 = _UNPK_(moduleNameFS (rdrNameModule rn))
1313 ++ '_':occNameString(rdrNameOcc rn) ++ '_':suffix
1314 where rn = toRdrName n
1318 %************************************************************************
1320 \subsection{Manufacturing of info tables for DataCons}
1322 %************************************************************************
1326 #if __GLASGOW_HASKELL__ <= 408
1329 type ItblPtr = Ptr StgInfoTable
1332 -- Make info tables for the data decls in this module
1333 mkITbls :: [TyCon] -> IO ItblEnv
1334 mkITbls [] = return emptyFM
1335 mkITbls (tc:tcs) = do itbls <- mkITbl tc
1336 itbls2 <- mkITbls tcs
1337 return (itbls `plusFM` itbls2)
1339 mkITbl :: TyCon -> IO ItblEnv
1341 | not (isDataTyCon tc)
1343 | n == length dcs -- paranoia; this is an assertion.
1344 = make_constr_itbls dcs
1346 dcs = tyConDataCons tc
1347 n = tyConFamilySize tc
1350 cONSTR = 1 -- as defined in ghc/includes/ClosureTypes.h
1352 -- Assumes constructors are numbered from zero, not one
1353 make_constr_itbls :: [DataCon] -> IO ItblEnv
1354 make_constr_itbls cons
1356 = do is <- mapM mk_vecret_itbl (zip cons [0..])
1357 return (listToFM is)
1359 = do is <- mapM mk_dirret_itbl (zip cons [0..])
1360 return (listToFM is)
1362 mk_vecret_itbl (dcon, conNo)
1363 = mk_itbl dcon conNo (vecret_entry conNo)
1364 mk_dirret_itbl (dcon, conNo)
1365 = mk_itbl dcon conNo stg_interp_constr_entry
1367 mk_itbl :: DataCon -> Int -> Addr -> IO (Name,ItblPtr)
1368 mk_itbl dcon conNo entry_addr
1369 = let (tot_wds, ptr_wds, _)
1370 = mkVirtHeapOffsets typePrimRep (dataConRepArgTys dcon)
1372 nptrs = tot_wds - ptr_wds
1373 itbl = StgInfoTable {
1374 ptrs = fromIntegral ptrs, nptrs = fromIntegral nptrs,
1375 tipe = fromIntegral cONSTR,
1376 srtlen = fromIntegral conNo,
1377 code0 = fromIntegral code0, code1 = fromIntegral code1,
1378 code2 = fromIntegral code2, code3 = fromIntegral code3,
1379 code4 = fromIntegral code4, code5 = fromIntegral code5,
1380 code6 = fromIntegral code6, code7 = fromIntegral code7
1382 -- Make a piece of code to jump to "entry_label".
1383 -- This is the only arch-dependent bit.
1384 -- On x86, if entry_label has an address 0xWWXXYYZZ,
1385 -- emit movl $0xWWXXYYZZ,%eax ; jmp *%eax
1387 -- B8 ZZ YY XX WW FF E0
1388 (code0,code1,code2,code3,code4,code5,code6,code7)
1389 = (0xB8, byte 0 entry_addr_w, byte 1 entry_addr_w,
1390 byte 2 entry_addr_w, byte 3 entry_addr_w,
1394 entry_addr_w :: Word32
1395 entry_addr_w = fromIntegral (addrToInt entry_addr)
1398 --putStrLn ("SIZE of itbl is " ++ show (sizeOf itbl))
1399 --putStrLn ("# ptrs of itbl is " ++ show ptrs)
1400 --putStrLn ("# nptrs of itbl is " ++ show nptrs)
1402 return (getName dcon, addr `plusPtr` 8)
1405 byte :: Int -> Word32 -> Word32
1406 byte 0 w = w .&. 0xFF
1407 byte 1 w = (w `shiftR` 8) .&. 0xFF
1408 byte 2 w = (w `shiftR` 16) .&. 0xFF
1409 byte 3 w = (w `shiftR` 24) .&. 0xFF
1412 vecret_entry 0 = stg_interp_constr1_entry
1413 vecret_entry 1 = stg_interp_constr2_entry
1414 vecret_entry 2 = stg_interp_constr3_entry
1415 vecret_entry 3 = stg_interp_constr4_entry
1416 vecret_entry 4 = stg_interp_constr5_entry
1417 vecret_entry 5 = stg_interp_constr6_entry
1418 vecret_entry 6 = stg_interp_constr7_entry
1419 vecret_entry 7 = stg_interp_constr8_entry
1421 -- entry point for direct returns for created constr itbls
1422 foreign label "stg_interp_constr_entry" stg_interp_constr_entry :: Addr
1423 -- and the 8 vectored ones
1424 foreign label "stg_interp_constr1_entry" stg_interp_constr1_entry :: Addr
1425 foreign label "stg_interp_constr2_entry" stg_interp_constr2_entry :: Addr
1426 foreign label "stg_interp_constr3_entry" stg_interp_constr3_entry :: Addr
1427 foreign label "stg_interp_constr4_entry" stg_interp_constr4_entry :: Addr
1428 foreign label "stg_interp_constr5_entry" stg_interp_constr5_entry :: Addr
1429 foreign label "stg_interp_constr6_entry" stg_interp_constr6_entry :: Addr
1430 foreign label "stg_interp_constr7_entry" stg_interp_constr7_entry :: Addr
1431 foreign label "stg_interp_constr8_entry" stg_interp_constr8_entry :: Addr
1437 -- Ultra-minimalist version specially for constructors
1438 data StgInfoTable = StgInfoTable {
1443 code0, code1, code2, code3, code4, code5, code6, code7 :: Word8
1447 instance Storable StgInfoTable where
1450 = (sum . map (\f -> f itbl))
1451 [fieldSz ptrs, fieldSz nptrs, fieldSz srtlen, fieldSz tipe,
1452 fieldSz code0, fieldSz code1, fieldSz code2, fieldSz code3,
1453 fieldSz code4, fieldSz code5, fieldSz code6, fieldSz code7]
1456 = (sum . map (\f -> f itbl))
1457 [fieldAl ptrs, fieldAl nptrs, fieldAl srtlen, fieldAl tipe,
1458 fieldAl code0, fieldAl code1, fieldAl code2, fieldAl code3,
1459 fieldAl code4, fieldAl code5, fieldAl code6, fieldAl code7]
1462 = do a1 <- store (ptrs itbl) (castPtr a0)
1463 a2 <- store (nptrs itbl) a1
1464 a3 <- store (tipe itbl) a2
1465 a4 <- store (srtlen itbl) a3
1466 a5 <- store (code0 itbl) a4
1467 a6 <- store (code1 itbl) a5
1468 a7 <- store (code2 itbl) a6
1469 a8 <- store (code3 itbl) a7
1470 a9 <- store (code4 itbl) a8
1471 aA <- store (code5 itbl) a9
1472 aB <- store (code6 itbl) aA
1473 aC <- store (code7 itbl) aB
1477 = do (a1,ptrs) <- load (castPtr a0)
1478 (a2,nptrs) <- load a1
1479 (a3,tipe) <- load a2
1480 (a4,srtlen) <- load a3
1481 (a5,code0) <- load a4
1482 (a6,code1) <- load a5
1483 (a7,code2) <- load a6
1484 (a8,code3) <- load a7
1485 (a9,code4) <- load a8
1486 (aA,code5) <- load a9
1487 (aB,code6) <- load aA
1488 (aC,code7) <- load aB
1489 return StgInfoTable { ptrs = ptrs, nptrs = nptrs,
1490 srtlen = srtlen, tipe = tipe,
1491 code0 = code0, code1 = code1, code2 = code2,
1492 code3 = code3, code4 = code4, code5 = code5,
1493 code6 = code6, code7 = code7 }
1495 fieldSz :: (Storable a, Storable b) => (a -> b) -> a -> Int
1496 fieldSz sel x = sizeOf (sel x)
1498 fieldAl :: (Storable a, Storable b) => (a -> b) -> a -> Int
1499 fieldAl sel x = alignment (sel x)
1501 store :: Storable a => a -> Ptr a -> IO (Ptr b)
1502 store x addr = do poke addr x
1503 return (castPtr (addr `plusPtr` sizeOf x))
1505 load :: Storable a => Ptr a -> IO (Ptr b, a)
1506 load addr = do x <- peek addr
1507 return (castPtr (addr `plusPtr` sizeOf x), x)
1511 %************************************************************************
1513 \subsection{Connect to actual values for bytecode opcodes}
1515 %************************************************************************
1519 #include "Bytecodes.h"
1521 i_ARGCHECK = (bci_ARGCHECK :: Int)
1522 i_PUSH_L = (bci_PUSH_L :: Int)
1523 i_PUSH_LL = (bci_PUSH_LL :: Int)
1524 i_PUSH_LLL = (bci_PUSH_LLL :: Int)
1525 i_PUSH_G = (bci_PUSH_G :: Int)
1526 i_PUSH_AS = (bci_PUSH_AS :: Int)
1527 i_PUSH_UBX = (bci_PUSH_UBX :: Int)
1528 i_PUSH_TAG = (bci_PUSH_TAG :: Int)
1529 i_SLIDE = (bci_SLIDE :: Int)
1530 i_ALLOC = (bci_ALLOC :: Int)
1531 i_MKAP = (bci_MKAP :: Int)
1532 i_UNPACK = (bci_UNPACK :: Int)
1533 i_UPK_TAG = (bci_UPK_TAG :: Int)
1534 i_PACK = (bci_PACK :: Int)
1535 i_TESTLT_I = (bci_TESTLT_I :: Int)
1536 i_TESTEQ_I = (bci_TESTEQ_I :: Int)
1537 i_TESTLT_F = (bci_TESTLT_F :: Int)
1538 i_TESTEQ_F = (bci_TESTEQ_F :: Int)
1539 i_TESTLT_D = (bci_TESTLT_D :: Int)
1540 i_TESTEQ_D = (bci_TESTEQ_D :: Int)
1541 i_TESTLT_P = (bci_TESTLT_P :: Int)
1542 i_TESTEQ_P = (bci_TESTEQ_P :: Int)
1543 i_CASEFAIL = (bci_CASEFAIL :: Int)
1544 i_ENTER = (bci_ENTER :: Int)
1545 i_RETURN = (bci_RETURN :: Int)