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, mkSysLocalName )
17 import Id ( Id, idType, isDataConId_maybe, mkVanillaId,
18 isPrimOpId_maybe, idPrimRep )
19 import OrdList ( OrdList, consOL, snocOL, appOL, unitOL,
20 nilOL, toOL, concatOL, fromOL )
21 import FiniteMap ( FiniteMap, addListToFM, listToFM,
22 addToFM, lookupFM, fmToList, plusFM )
24 import PprCore ( pprCoreExpr )
25 import Literal ( Literal(..), literalPrimRep )
26 import PrimRep ( PrimRep(..) )
27 import PrimOp ( PrimOp(..) )
28 import CoreFVs ( freeVars )
29 import Type ( typePrimRep )
30 import DataCon ( dataConTag, fIRST_TAG, dataConTyCon, dataConWrapId )
31 import TyCon ( TyCon, tyConFamilySize )
32 import Class ( Class, classTyCon )
33 import Util ( zipEqual, zipWith4Equal, naturalMergeSortLe, nOfThem )
34 import Var ( isTyVar )
35 import VarSet ( VarSet, varSetElems )
36 import PrimRep ( getPrimRepSize, isFollowableRep )
37 import CmdLineOpts ( DynFlags, DynFlag(..) )
38 import ErrUtils ( showPass, dumpIfSet_dyn )
39 import Unique ( mkPseudoUnique3 )
40 import FastString ( FastString(..) )
41 import PprType ( pprType )
42 import ByteCodeInstr ( BCInstr(..), ProtoBCO(..), nameOfProtoBCO, bciStackUse )
43 import ByteCodeItbls ( ItblEnv, mkITbls )
44 import ByteCodeLink ( UnlinkedBCO, UnlinkedBCOExpr, assembleBCO,
45 ClosureEnv, HValue, linkSomeBCOs, filterNameMap,
46 iNTERP_STACK_CHECK_THRESH )
48 import List ( intersperse, sortBy )
49 import Foreign ( Ptr(..), mallocBytes )
50 import Addr ( Addr(..), addrToInt, writeCharOffAddr )
51 import CTypes ( CInt )
53 import PrelBase ( Int(..) )
54 import PrelGHC ( ByteArray# )
55 import IOExts ( unsafePerformIO )
56 import PrelIOBase ( IO(..) )
60 %************************************************************************
62 \subsection{Functions visible from outside this module.}
64 %************************************************************************
68 byteCodeGen :: DynFlags
71 -> IO ([UnlinkedBCO], ItblEnv)
72 byteCodeGen dflags binds local_tycons local_classes
73 = do showPass dflags "ByteCodeGen"
74 let tycs = local_tycons ++ map classTyCon local_classes
75 itblenv <- mkITbls tycs
77 let flatBinds = concatMap getBind binds
78 getBind (NonRec bndr rhs) = [(bndr, freeVars rhs)]
79 getBind (Rec binds) = [(bndr, freeVars rhs) | (bndr,rhs) <- binds]
80 final_state = runBc (BcM_State [] 0)
81 (mapBc (schemeR True) flatBinds
82 `thenBc_` returnBc ())
83 (BcM_State proto_bcos final_ctr) = final_state
85 dumpIfSet_dyn dflags Opt_D_dump_BCOs
86 "Proto-bcos" (vcat (intersperse (char ' ') (map ppr proto_bcos)))
88 bcos <- mapM assembleBCO proto_bcos
90 return (bcos, itblenv)
93 -- Returns: (the root BCO for this expression,
94 -- a list of auxilary BCOs resulting from compiling closures)
95 coreExprToBCOs :: DynFlags
98 coreExprToBCOs dflags expr
99 = do showPass dflags "ByteCodeGen"
101 -- create a totally bogus name for the top-level BCO; this
102 -- should be harmless, since it's never used for anything
103 let invented_name = mkSysLocalName (mkPseudoUnique3 0) SLIT("Expr-Top-Level")
104 let invented_id = mkVanillaId invented_name (panic "invented_id's type")
106 let (BcM_State all_proto_bcos final_ctr)
107 = runBc (BcM_State [] 0)
108 (schemeR True (invented_id, freeVars expr))
109 dumpIfSet_dyn dflags Opt_D_dump_BCOs
110 "Proto-bcos" (vcat (intersperse (char ' ') (map ppr all_proto_bcos)))
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)
125 linkIModules :: ItblEnv -- incoming global itbl env; returned updated
126 -> ClosureEnv -- incoming global closure env; returned updated
127 -> [([UnlinkedBCO], ItblEnv)]
128 -> IO ([HValue], ItblEnv, ClosureEnv)
129 linkIModules gie gce mods
130 = do let (bcoss, ies) = unzip mods
132 final_gie = foldr plusFM gie ies
133 (final_gce, linked_bcos) <- linkSomeBCOs True final_gie gce bcos
134 return (linked_bcos, final_gie, final_gce)
137 linkIExpr :: ItblEnv -> ClosureEnv -> UnlinkedBCOExpr
138 -> IO HValue -- IO BCO# really
139 linkIExpr ie ce (root_ul_bco, aux_ul_bcos)
140 = do (aux_ce, _) <- linkSomeBCOs False ie ce aux_ul_bcos
141 (_, [root_bco]) <- linkSomeBCOs False ie aux_ce [root_ul_bco]
145 %************************************************************************
147 \subsection{Compilation schema for the bytecode generator.}
149 %************************************************************************
153 type BCInstrList = OrdList BCInstr
155 type Sequel = Int -- back off to this depth before ENTER
157 -- Maps Ids to the offset from the stack _base_ so we don't have
158 -- to mess with it after each push/pop.
159 type BCEnv = FiniteMap Id Int -- To find vars on the stack
161 ppBCEnv :: BCEnv -> SDoc
164 $$ nest 4 (vcat (map pp_one (sortBy cmp_snd (fmToList p))))
167 pp_one (var, offset) = int offset <> colon <+> ppr var
168 cmp_snd x y = compare (snd x) (snd y)
170 -- Create a BCO and do a spot of peephole optimisation on the insns
172 mkProtoBCO nm instrs_ordlist origin
173 = ProtoBCO nm maybe_with_stack_check origin
175 -- Overestimate the stack usage (in words) of this BCO,
176 -- and if >= iNTERP_STACK_CHECK_THRESH, add an explicit
177 -- stack check. (The interpreter always does a stack check
178 -- for iNTERP_STACK_CHECK_THRESH words at the start of each
179 -- BCO anyway, so we only need to add an explicit on in the
180 -- (hopefully rare) cases when the (overestimated) stack use
181 -- exceeds iNTERP_STACK_CHECK_THRESH.
182 maybe_with_stack_check
183 | stack_overest >= 65535
184 = pprPanic "mkProtoBCO: stack use won't fit in 16 bits"
186 | stack_overest >= iNTERP_STACK_CHECK_THRESH
187 = (STKCHECK stack_overest) : peep_d
189 = peep_d -- the supposedly common case
191 stack_overest = sum (map bciStackUse peep_d)
192 + 10 {- just to be really really sure -}
195 -- Merge local pushes
196 peep_d = peep (fromOL instrs_ordlist)
198 peep (PUSH_L off1 : PUSH_L off2 : PUSH_L off3 : rest)
199 = PUSH_LLL off1 (off2-1) (off3-2) : peep rest
200 peep (PUSH_L off1 : PUSH_L off2 : rest)
201 = PUSH_LL off1 (off2-1) : peep rest
208 -- Compile code for the right hand side of a let binding.
209 -- Park the resulting BCO in the monad. Also requires the
210 -- variable to which this value was bound, so as to give the
211 -- resulting BCO a name. Bool indicates top-levelness.
213 schemeR :: Bool -> (Id, AnnExpr Id VarSet) -> BcM ()
214 schemeR is_top (nm, rhs)
218 $$ (ppr.filter (not.isTyVar).varSetElems.fst) rhs
219 $$ pprCoreExpr (deAnnotate rhs)
225 = schemeR_wrk is_top rhs nm (collect [] rhs)
228 collect xs (_, AnnNote note e)
230 collect xs (_, AnnLam x e)
231 = collect (if isTyVar x then xs else (x:xs)) e
232 collect xs not_lambda
233 = (reverse xs, not_lambda)
235 schemeR_wrk is_top original_body nm (args, body)
236 | Just dcon <- maybe_toplevel_null_con_rhs
237 = --trace ("nullary constructor! " ++ showSDocDebug (ppr nm)) (
238 emitBc (mkProtoBCO (getName nm) (toOL [PACK dcon 0, ENTER])
239 (Right original_body))
243 = let fvs = filter (not.isTyVar) (varSetElems (fst original_body))
244 all_args = reverse args ++ fvs
245 szsw_args = map taggedIdSizeW all_args
246 szw_args = sum szsw_args
247 p_init = listToFM (zip all_args (mkStackOffsets 0 szsw_args))
248 argcheck = unitOL (ARGCHECK szw_args)
250 schemeE szw_args 0 p_init body `thenBc` \ body_code ->
251 emitBc (mkProtoBCO (getName nm) (appOL argcheck body_code)
252 (Right original_body))
255 maybe_toplevel_null_con_rhs
256 | is_top && null args
259 -> case isDataConId_maybe v_wrk of
261 Just dc_wrk | nm == dataConWrapId dc_wrk
269 -- Let szsw be the sizes in words of some items pushed onto the stack,
270 -- which has initial depth d'. Return the values which the stack environment
271 -- should map these items to.
272 mkStackOffsets :: Int -> [Int] -> [Int]
273 mkStackOffsets original_depth szsw
274 = map (subtract 1) (tail (scanl (+) original_depth szsw))
276 -- Compile code to apply the given expression to the remaining args
277 -- on the stack, returning a HNF.
278 schemeE :: Int -> Sequel -> BCEnv -> AnnExpr Id VarSet -> BcM BCInstrList
280 -- Delegate tail-calls to schemeT.
281 schemeE d s p e@(fvs, AnnApp f a)
282 = returnBc (schemeT d s p (fvs, AnnApp f a))
283 schemeE d s p e@(fvs, AnnVar v)
284 | isFollowableRep v_rep
285 = returnBc (schemeT d s p (fvs, AnnVar v))
288 = -- returning an unboxed value. Heave it on the stack, SLIDE, and RETURN.
289 let (push, szw) = pushAtom True d p (AnnVar v)
290 in returnBc (push -- value onto stack
291 `appOL` mkSLIDE szw (d-s) -- clear to sequel
292 `snocOL` RETURN v_rep) -- go
294 v_rep = typePrimRep (idType v)
296 schemeE d s p (fvs, AnnLit literal)
297 = let (push, szw) = pushAtom True d p (AnnLit literal)
298 l_rep = literalPrimRep literal
299 in returnBc (push -- value onto stack
300 `appOL` mkSLIDE szw (d-s) -- clear to sequel
301 `snocOL` RETURN l_rep) -- go
303 schemeE d s p (fvs, AnnLet binds b)
304 = let (xs,rhss) = case binds of AnnNonRec x rhs -> ([x],[rhs])
305 AnnRec xs_n_rhss -> unzip xs_n_rhss
307 fvss = map (filter (not.isTyVar).varSetElems.fst) rhss
309 -- Sizes of tagged free vars, + 1 for the fn
310 sizes = map (\rhs_fvs -> 1 + sum (map taggedIdSizeW rhs_fvs)) fvss
312 -- This p', d' defn is safe because all the items being pushed
313 -- are ptrs, so all have size 1. d' and p' reflect the stack
314 -- after the closures have been allocated in the heap (but not
315 -- filled in), and pointers to them parked on the stack.
316 p' = addListToFM p (zipE xs (mkStackOffsets d (nOfThem n 1)))
319 infos = zipE4 fvss sizes xs [n, n-1 .. 1]
320 zipE = zipEqual "schemeE"
321 zipE4 = zipWith4Equal "schemeE" (\a b c d -> (a,b,c,d))
323 -- ToDo: don't build thunks for things with no free variables
324 buildThunk dd ([], size, id, off)
325 = PUSH_G (Left (getName id))
326 `consOL` unitOL (MKAP (off+size-1) size)
327 buildThunk dd ((fv:fvs), size, id, off)
328 = case pushAtom True dd p' (AnnVar fv) of
329 (push_code, pushed_szw)
331 buildThunk (dd+pushed_szw) (fvs, size, id, off)
333 thunkCode = concatOL (map (buildThunk d') infos)
334 allocCode = toOL (map ALLOC sizes)
336 schemeE d' s p' b `thenBc` \ bodyCode ->
337 mapBc (schemeR False) (zip xs rhss) `thenBc_`
338 returnBc (allocCode `appOL` thunkCode `appOL` bodyCode)
341 schemeE d s p (fvs, AnnCase scrut bndr alts)
343 -- Top of stack is the return itbl, as usual.
344 -- underneath it is the pointer to the alt_code BCO.
345 -- When an alt is entered, it assumes the returned value is
346 -- on top of the itbl.
349 -- Env and depth in which to compile the alts, not including
350 -- any vars bound by the alts themselves
351 d' = d + ret_frame_sizeW + taggedIdSizeW bndr
352 p' = addToFM p bndr (d' - 1)
354 scrut_primrep = typePrimRep (idType bndr)
356 = case scrut_primrep of
357 CharRep -> False ; AddrRep -> False ; WordRep -> False
358 IntRep -> False ; FloatRep -> False ; DoubleRep -> False
360 other -> pprPanic "ByteCodeGen.schemeE" (ppr other)
362 -- given an alt, return a discr and code for it.
363 codeAlt alt@(discr, binds_f, rhs)
365 = let (unpack_code, d_after_unpack, p_after_unpack)
366 = mkUnpackCode binds_f d' p'
367 in schemeE d_after_unpack s p_after_unpack rhs
368 `thenBc` \ rhs_code ->
369 returnBc (my_discr alt, unpack_code `appOL` rhs_code)
371 = ASSERT(null binds_f)
372 schemeE d' s p' rhs `thenBc` \ rhs_code ->
373 returnBc (my_discr alt, rhs_code)
375 my_discr (DEFAULT, binds, rhs) = NoDiscr
376 my_discr (DataAlt dc, binds, rhs) = DiscrP (dataConTag dc - fIRST_TAG)
377 my_discr (LitAlt l, binds, rhs)
378 = case l of MachInt i -> DiscrI (fromInteger i)
379 MachFloat r -> DiscrF (fromRational r)
380 MachDouble r -> DiscrD (fromRational r)
381 MachChar i -> DiscrI i
382 _ -> pprPanic "schemeE(AnnCase).my_discr" (ppr l)
385 | not isAlgCase = Nothing
387 = case [dc | (DataAlt dc, _, _) <- alts] of
389 (dc:_) -> Just (tyConFamilySize (dataConTyCon dc))
392 mapBc codeAlt alts `thenBc` \ alt_stuff ->
393 mkMultiBranch maybe_ncons alt_stuff `thenBc` \ alt_final ->
395 alt_final_ac = ARGCHECK (taggedIdSizeW bndr) `consOL` alt_final
396 alt_bco_name = getName bndr
397 alt_bco = mkProtoBCO alt_bco_name alt_final_ac (Left alts)
399 schemeE (d + ret_frame_sizeW)
400 (d + ret_frame_sizeW) p scrut `thenBc` \ scrut_code ->
402 emitBc alt_bco `thenBc_`
403 returnBc (PUSH_AS alt_bco_name scrut_primrep `consOL` scrut_code)
406 schemeE d s p (fvs, AnnNote note body)
410 = pprPanic "ByteCodeGen.schemeE: unhandled case"
411 (pprCoreExpr (deAnnotate other))
414 -- Compile code to do a tail call. Three cases:
416 -- 1. A nullary constructor. Push its closure on the stack
417 -- and SLIDE and RETURN.
419 -- 2. Application of a non-nullary constructor, by defn saturated.
420 -- Split the args into ptrs and non-ptrs, and push the nonptrs,
421 -- then the ptrs, and then do PACK and RETURN.
423 -- 3. Otherwise, it must be a function call. Push the args
424 -- right to left, SLIDE and ENTER.
426 schemeT :: Int -- Stack depth
427 -> Sequel -- Sequel depth
428 -> BCEnv -- stack env
433 -- | trace ("schemeT: env in = \n" ++ showSDocDebug (ppBCEnv p)) False
434 -- = panic "schemeT ?!?!"
437 | is_con_call && null args_r_to_l
438 = (PUSH_G (Left (getName con)) `consOL` mkSLIDE 1 (d-s))
446 -- Extract the args (R->L) and fn
447 (args_r_to_l_raw, fn) = chomp app
451 AnnApp f a -> case chomp f of (az, f) -> (snd a:az, f)
452 AnnNote n e -> chomp e
453 other -> pprPanic "schemeT"
454 (ppr (deAnnotate (panic "schemeT.chomp", other)))
456 args_r_to_l = filter (not.isTypeAtom) args_r_to_l_raw
457 isTypeAtom (AnnType _) = True
460 -- decide if this is a constructor call, and rearrange
461 -- args appropriately.
462 maybe_dcon = isDataConId_maybe fn
463 is_con_call = case maybe_dcon of Nothing -> False; Just _ -> True
464 (Just con) = maybe_dcon
470 = filter (not.isPtr) args_r_to_l ++ filter isPtr args_r_to_l
471 where isPtr = isFollowableRep . atomRep
473 -- make code to push the args and then do the SLIDE-ENTER thing
474 code = do_pushery d args_final_r_to_l
476 tag_when_push = not is_con_call
477 narg_words = sum (map (get_arg_szw . atomRep) args_r_to_l)
478 get_arg_szw = if tag_when_push then taggedSizeW else untaggedSizeW
480 do_pushery d (arg:args)
481 = let (push, arg_words) = pushAtom tag_when_push d p arg
482 in push `appOL` do_pushery (d+arg_words) args
485 Just con -> PACK con narg_words `consOL` (
486 mkSLIDE 1 (d - narg_words - s) `snocOL` ENTER)
488 -> let (push, arg_words) = pushAtom True d p (AnnVar fn)
490 `appOL` mkSLIDE (narg_words+arg_words)
495 = if d == 0 then nilOL else unitOL (SLIDE n d)
497 atomRep (AnnVar v) = typePrimRep (idType v)
498 atomRep (AnnLit l) = literalPrimRep l
499 atomRep (AnnNote n b) = atomRep (snd b)
500 atomRep (AnnApp f (_, AnnType _)) = atomRep (snd f)
501 atomRep (AnnLam x e) | isTyVar x = atomRep (snd e)
502 atomRep other = pprPanic "atomRep" (ppr (deAnnotate (undefined,other)))
505 -- Make code to unpack the top-of-stack constructor onto the stack,
506 -- adding tags for the unboxed bits. Takes the PrimReps of the
507 -- constructor's arguments. off_h and off_s are travelling offsets
508 -- along the constructor and the stack.
510 -- Supposing a constructor in the heap has layout
512 -- Itbl p_1 ... p_i np_1 ... np_j
514 -- then we add to the stack, shown growing down, the following:
526 -- so that in the common case (ptrs only) a single UNPACK instr can
527 -- copy all the payload of the constr onto the stack with no further ado.
529 mkUnpackCode :: [Id] -- constr args
530 -> Int -- depth before unpack
531 -> BCEnv -- env before unpack
532 -> (BCInstrList, Int, BCEnv)
533 mkUnpackCode vars d p
534 = --trace ("mkUnpackCode: " ++ showSDocDebug (ppr vars)
535 -- ++ " --> " ++ show d' ++ "\n" ++ showSDocDebug (ppBCEnv p')
537 (code_p `appOL` code_np, d', p')
541 vreps = [(var, typePrimRep (idType var)) | var <- vars]
543 -- ptrs and nonptrs, forward
544 vreps_p = filter (isFollowableRep.snd) vreps
545 vreps_np = filter (not.isFollowableRep.snd) vreps
547 -- the order in which we will augment the environment
548 vreps_env = reverse vreps_p ++ reverse vreps_np
551 vreps_env_tszsw = map (taggedSizeW.snd) vreps_env
552 p' = addListToFM p (zip (map fst vreps_env)
553 (mkStackOffsets d vreps_env_tszsw))
554 d' = d + sum vreps_env_tszsw
556 -- code to unpack the ptrs
557 ptrs_szw = sum (map (untaggedSizeW.snd) vreps_p)
558 code_p | null vreps_p = nilOL
559 | otherwise = unitOL (UNPACK ptrs_szw)
561 -- code to unpack the nonptrs
562 vreps_env_uszw = sum (map (untaggedSizeW.snd) vreps_env)
563 code_np = do_nptrs vreps_env_uszw ptrs_szw (reverse (map snd vreps_np))
564 do_nptrs off_h off_s [] = nilOL
565 do_nptrs off_h off_s (npr:nprs)
567 IntRep -> approved ; FloatRep -> approved
568 DoubleRep -> approved ; AddrRep -> approved
570 _ -> pprPanic "ByteCodeGen.mkUnpackCode" (ppr npr)
572 approved = UPK_TAG usizeW (off_h-usizeW) off_s `consOL` theRest
573 theRest = do_nptrs (off_h-usizeW) (off_s + tsizeW) nprs
574 usizeW = untaggedSizeW npr
575 tsizeW = taggedSizeW npr
578 -- Push an atom onto the stack, returning suitable code & number of
579 -- stack words used. Pushes it either tagged or untagged, since
580 -- pushAtom is used to set up the stack prior to copying into the
581 -- heap for both APs (requiring tags) and constructors (which don't).
583 -- NB this means NO GC between pushing atoms for a constructor and
584 -- copying them into the heap. It probably also means that
585 -- tail calls MUST be of the form atom{atom ... atom} since if the
586 -- expression head was allowed to be arbitrary, there could be GC
587 -- in between pushing the arg atoms and completing the head.
588 -- (not sure; perhaps the allocate/doYouWantToGC interface means this
589 -- isn't a problem; but only if arbitrary graph construction for the
590 -- head doesn't leave this BCO, since GC might happen at the start of
591 -- each BCO (we consult doYouWantToGC there).
593 -- Blargh. JRS 001206
595 -- NB (further) that the env p must map each variable to the highest-
596 -- numbered stack slot for it. For example, if the stack has depth 4
597 -- and we tagged-ly push (v :: Int#) on it, the value will be in stack[4],
598 -- the tag in stack[5], the stack will have depth 6, and p must map v to
599 -- 5 and not to 4. Stack locations are numbered from zero, so a depth
600 -- 6 stack has valid words 0 .. 5.
602 pushAtom :: Bool -> Int -> BCEnv -> AnnExpr' Id VarSet -> (BCInstrList, Int)
603 pushAtom tagged d p (AnnVar v)
605 | idPrimRep v == VoidRep
607 (unitOL (PUSH_TAG 0), 1)
609 | Just primop <- isPrimOpId_maybe v
611 CCallOp _ -> panic "pushAtom: byte code generator can't handle CCalls"
612 other -> (unitOL (PUSH_G (Right primop)), 1)
615 = let str = "\npushAtom " ++ showSDocDebug (ppr v)
616 ++ " :: " ++ showSDocDebug (pprType (idType v))
617 ++ ", depth = " ++ show d
618 ++ ", tagged = " ++ show tagged ++ ", env =\n" ++
619 showSDocDebug (ppBCEnv p)
620 ++ " --> words: " ++ show (snd result) ++ "\n" ++
621 showSDoc (nest 4 (vcat (map ppr (fromOL (fst result)))))
622 ++ "\nendPushAtom " ++ showSDocDebug (ppr v)
624 cmp_snd x y = compare (snd x) (snd y)
625 str' = if str == str then str else str
628 = case lookupBCEnv_maybe p v of
629 Just d_v -> (toOL (nOfThem nwords (PUSH_L (d-d_v+sz_t-2))), nwords)
630 Nothing -> ASSERT(sz_t == 1) (unitOL (PUSH_G (Left nm)), nwords)
632 nm = case isDataConId_maybe v of
636 sz_t = taggedIdSizeW v
637 sz_u = untaggedIdSizeW v
638 nwords = if tagged then sz_t else sz_u
643 pushAtom True d p (AnnLit lit)
644 = let (ubx_code, ubx_size) = pushAtom False d p (AnnLit lit)
645 in (ubx_code `snocOL` PUSH_TAG ubx_size, 1 + ubx_size)
647 pushAtom False d p (AnnLit lit)
649 MachWord w -> code WordRep
650 MachInt i -> code IntRep
651 MachFloat r -> code FloatRep
652 MachDouble r -> code DoubleRep
653 MachChar c -> code CharRep
654 MachStr s -> pushStr s
657 = let size_host_words = untaggedSizeW rep
658 in (unitOL (PUSH_UBX lit size_host_words), size_host_words)
661 = let mallocvilleAddr
666 -- sigh, a string in the heap is no good to us.
667 -- We need a static C pointer, since the type of
668 -- a string literal is Addr#. So, copy the string
669 -- into C land and introduce a memory leak
672 -- CAREFUL! Chars are 32 bits in ghc 4.09+
674 do (Ptr a#) <- mallocBytes (n+1)
675 strncpy (Ptr a#) ba (fromIntegral n)
676 writeCharOffAddr (A# a#) n '\0'
679 _ -> panic "StgInterp.lit2expr: unhandled string constant type"
682 = MachInt (toInteger (addrToInt mallocvilleAddr))
684 -- Get the addr on the stack, untaggedly
685 (unitOL (PUSH_UBX addrLit 1), 1)
691 pushAtom tagged d p (AnnApp f (_, AnnType _))
692 = pushAtom tagged d p (snd f)
694 pushAtom tagged d p (AnnNote note e)
695 = pushAtom tagged d p (snd e)
697 pushAtom tagged d p (AnnLam x e)
699 = pushAtom tagged d p (snd e)
701 pushAtom tagged d p other
702 = pprPanic "ByteCodeGen.pushAtom"
703 (pprCoreExpr (deAnnotate (undefined, other)))
705 foreign import "strncpy" strncpy :: Ptr a -> ByteArray# -> CInt -> IO ()
708 -- Given a bunch of alts code and their discrs, do the donkey work
709 -- of making a multiway branch using a switch tree.
710 -- What a load of hassle!
711 mkMultiBranch :: Maybe Int -- # datacons in tycon, if alg alt
712 -- a hint; generates better code
713 -- Nothing is always safe
714 -> [(Discr, BCInstrList)]
716 mkMultiBranch maybe_ncons raw_ways
717 = let d_way = filter (isNoDiscr.fst) raw_ways
718 notd_ways = naturalMergeSortLe
719 (\w1 w2 -> leAlt (fst w1) (fst w2))
720 (filter (not.isNoDiscr.fst) raw_ways)
722 mkTree :: [(Discr, BCInstrList)] -> Discr -> Discr -> BcM BCInstrList
723 mkTree [] range_lo range_hi = returnBc the_default
725 mkTree [val] range_lo range_hi
726 | range_lo `eqAlt` range_hi
729 = getLabelBc `thenBc` \ label_neq ->
730 returnBc (mkTestEQ (fst val) label_neq
732 `appOL` unitOL (LABEL label_neq)
733 `appOL` the_default))
735 mkTree vals range_lo range_hi
736 = let n = length vals `div` 2
737 vals_lo = take n vals
738 vals_hi = drop n vals
739 v_mid = fst (head vals_hi)
741 getLabelBc `thenBc` \ label_geq ->
742 mkTree vals_lo range_lo (dec v_mid) `thenBc` \ code_lo ->
743 mkTree vals_hi v_mid range_hi `thenBc` \ code_hi ->
744 returnBc (mkTestLT v_mid label_geq
746 `appOL` unitOL (LABEL label_geq)
750 = case d_way of [] -> unitOL CASEFAIL
753 -- None of these will be needed if there are no non-default alts
754 (mkTestLT, mkTestEQ, init_lo, init_hi)
756 = panic "mkMultiBranch: awesome foursome"
758 = case fst (head notd_ways) of {
759 DiscrI _ -> ( \(DiscrI i) fail_label -> TESTLT_I i fail_label,
760 \(DiscrI i) fail_label -> TESTEQ_I i fail_label,
763 DiscrF _ -> ( \(DiscrF f) fail_label -> TESTLT_F f fail_label,
764 \(DiscrF f) fail_label -> TESTEQ_F f fail_label,
767 DiscrD _ -> ( \(DiscrD d) fail_label -> TESTLT_D d fail_label,
768 \(DiscrD d) fail_label -> TESTEQ_D d fail_label,
771 DiscrP _ -> ( \(DiscrP i) fail_label -> TESTLT_P i fail_label,
772 \(DiscrP i) fail_label -> TESTEQ_P i fail_label,
777 (algMinBound, algMaxBound)
778 = case maybe_ncons of
780 Nothing -> (minBound, maxBound)
782 (DiscrI i1) `eqAlt` (DiscrI i2) = i1 == i2
783 (DiscrF f1) `eqAlt` (DiscrF f2) = f1 == f2
784 (DiscrD d1) `eqAlt` (DiscrD d2) = d1 == d2
785 (DiscrP i1) `eqAlt` (DiscrP i2) = i1 == i2
786 NoDiscr `eqAlt` NoDiscr = True
789 (DiscrI i1) `leAlt` (DiscrI i2) = i1 <= i2
790 (DiscrF f1) `leAlt` (DiscrF f2) = f1 <= f2
791 (DiscrD d1) `leAlt` (DiscrD d2) = d1 <= d2
792 (DiscrP i1) `leAlt` (DiscrP i2) = i1 <= i2
793 NoDiscr `leAlt` NoDiscr = True
796 isNoDiscr NoDiscr = True
799 dec (DiscrI i) = DiscrI (i-1)
800 dec (DiscrP i) = DiscrP (i-1)
801 dec other = other -- not really right, but if you
802 -- do cases on floating values, you'll get what you deserve
804 -- same snotty comment applies to the following
812 mkTree notd_ways init_lo init_hi
816 %************************************************************************
818 \subsection{Supporting junk for the compilation schemes}
820 %************************************************************************
824 -- Describes case alts
832 instance Outputable Discr where
833 ppr (DiscrI i) = int i
834 ppr (DiscrF f) = text (show f)
835 ppr (DiscrD d) = text (show d)
836 ppr (DiscrP i) = int i
837 ppr NoDiscr = text "DEF"
840 -- Find things in the BCEnv (the what's-on-the-stack-env)
841 -- See comment preceding pushAtom for precise meaning of env contents
842 --lookupBCEnv :: BCEnv -> Id -> Int
844 -- = case lookupFM env nm of
845 -- Nothing -> pprPanic "lookupBCEnv"
846 -- (ppr nm $$ char ' ' $$ vcat (map ppr (fmToList env)))
849 lookupBCEnv_maybe :: BCEnv -> Id -> Maybe Int
850 lookupBCEnv_maybe = lookupFM
853 -- When I push one of these on the stack, how much does Sp move by?
854 taggedSizeW :: PrimRep -> Int
856 | isFollowableRep pr = 1
857 | otherwise = 1{-the tag-} + getPrimRepSize pr
860 -- The plain size of something, without tag.
861 untaggedSizeW :: PrimRep -> Int
863 | isFollowableRep pr = 1
864 | otherwise = getPrimRepSize pr
867 taggedIdSizeW, untaggedIdSizeW :: Id -> Int
868 taggedIdSizeW = taggedSizeW . typePrimRep . idType
869 untaggedIdSizeW = untaggedSizeW . typePrimRep . idType
873 %************************************************************************
875 \subsection{The bytecode generator's monad}
877 %************************************************************************
881 = BcM_State { bcos :: [ProtoBCO Name], -- accumulates completed BCOs
882 nextlabel :: Int } -- for generating local labels
884 type BcM result = BcM_State -> (result, BcM_State)
886 runBc :: BcM_State -> BcM () -> BcM_State
887 runBc init_st m = case m init_st of { (r,st) -> st }
889 thenBc :: BcM a -> (a -> BcM b) -> BcM b
891 = case expr st of { (result, st') -> cont result st' }
893 thenBc_ :: BcM a -> BcM b -> BcM b
895 = case expr st of { (result, st') -> cont st' }
897 returnBc :: a -> BcM a
898 returnBc result st = (result, st)
900 mapBc :: (a -> BcM b) -> [a] -> BcM [b]
901 mapBc f [] = returnBc []
903 = f x `thenBc` \ r ->
904 mapBc f xs `thenBc` \ rs ->
907 emitBc :: ProtoBCO Name -> BcM ()
909 = ((), st{bcos = bco : bcos st})
911 getLabelBc :: BcM Int
913 = (nextlabel st, st{nextlabel = 1 + nextlabel st})