2 % (c) The University of Glasgow 2002
4 \section[ByteCodeGen]{Generate bytecode from Core}
7 module ByteCodeGen ( UnlinkedBCO, byteCodeGen, coreExprToBCOs ) where
9 #include "HsVersions.h"
12 import ByteCodeFFI ( mkMarshalCode, moan64 )
13 import ByteCodeAsm ( CompiledByteCode(..), UnlinkedBCO,
14 assembleBCO, assembleBCOs, iNTERP_STACK_CHECK_THRESH )
15 import ByteCodeLink ( lookupStaticPtr )
18 import Name ( Name, getName, mkSystemName )
21 import ForeignCall ( ForeignCall(..), CCallTarget(..), CCallSpec(..) )
22 import HscTypes ( TypeEnv, typeEnvTyCons, typeEnvClasses )
23 import CoreUtils ( exprType )
25 import PprCore ( pprCoreExpr )
26 import Literal ( Literal(..), literalPrimRep )
28 import PrimOp ( PrimOp(..) )
29 import CoreFVs ( freeVars )
30 import Type ( typePrimRep, isUnLiftedType, splitTyConApp_maybe )
31 import DataCon ( DataCon, dataConTag, fIRST_TAG, dataConTyCon,
32 isUnboxedTupleCon, isNullaryDataCon, dataConWorkId,
34 import TyCon ( tyConFamilySize, isDataTyCon, tyConDataCons,
36 import Class ( Class, classTyCon )
37 import Type ( Type, repType, splitFunTys, dropForAlls )
39 import DataCon ( dataConRepArity )
40 import Var ( isTyVar )
41 import VarSet ( VarSet, varSetElems )
42 import TysPrim ( arrayPrimTyCon, mutableArrayPrimTyCon,
43 byteArrayPrimTyCon, mutableByteArrayPrimTyCon
45 import PrimRep ( isFollowableRep )
46 import CmdLineOpts ( DynFlags, DynFlag(..) )
47 import ErrUtils ( showPass, dumpIfSet_dyn )
48 import Unique ( mkPseudoUnique3 )
49 import FastString ( FastString(..), unpackFS )
50 import Panic ( GhcException(..) )
51 import PprType ( pprType )
52 import SMRep ( arrWordsHdrSize, arrPtrsHdrSize, StgWord )
53 import Bitmap ( intsToReverseBitmap, mkBitmap )
55 import Constants ( wORD_SIZE )
57 import Data.List ( intersperse, sortBy, zip4, zip5, partition )
58 import Foreign ( Ptr, castPtr, mallocBytes, pokeByteOff, Word8 )
59 import Foreign.C ( CInt )
60 import Control.Exception ( throwDyn )
62 import GHC.Exts ( Int(..), ByteArray# )
64 import Control.Monad ( when )
65 import Data.Char ( ord )
67 -- -----------------------------------------------------------------------------
68 -- Generating byte code for a complete module
70 byteCodeGen :: DynFlags
73 -> IO CompiledByteCode
74 byteCodeGen dflags binds type_env
75 = do showPass dflags "ByteCodeGen"
76 let local_tycons = typeEnvTyCons type_env
77 local_classes = typeEnvClasses type_env
78 tycs = local_tycons ++ map classTyCon local_classes
80 let flatBinds = [ (bndr, freeVars rhs)
81 | (bndr, rhs) <- flattenBinds binds]
83 (BcM_State final_ctr mallocd, proto_bcos)
84 <- runBc (mapM schemeTopBind flatBinds)
86 when (notNull mallocd)
87 (panic "ByteCodeGen.byteCodeGen: missing final emitBc?")
89 dumpIfSet_dyn dflags Opt_D_dump_BCOs
90 "Proto-BCOs" (vcat (intersperse (char ' ') (map ppr proto_bcos)))
92 assembleBCOs proto_bcos tycs
94 -- -----------------------------------------------------------------------------
95 -- Generating byte code for an expression
97 -- Returns: (the root BCO for this expression,
98 -- a list of auxilary BCOs resulting from compiling closures)
99 coreExprToBCOs :: DynFlags
102 coreExprToBCOs dflags expr
103 = do showPass dflags "ByteCodeGen"
105 -- create a totally bogus name for the top-level BCO; this
106 -- should be harmless, since it's never used for anything
107 let invented_name = mkSystemName (mkPseudoUnique3 0) FSLIT("ExprTopLevel")
108 invented_id = mkLocalId invented_name (panic "invented_id's type")
110 (BcM_State final_ctr mallocd, proto_bco)
111 <- runBc (schemeTopBind (invented_id, freeVars expr))
113 when (notNull mallocd)
114 (panic "ByteCodeGen.coreExprToBCOs: missing final emitBc?")
116 dumpIfSet_dyn dflags Opt_D_dump_BCOs "Proto-BCOs" (ppr proto_bco)
118 assembleBCO proto_bco
121 -- -----------------------------------------------------------------------------
122 -- Compilation schema for the bytecode generator
124 type BCInstrList = OrdList BCInstr
126 type Sequel = Int -- back off to this depth before ENTER
128 -- Maps Ids to the offset from the stack _base_ so we don't have
129 -- to mess with it after each push/pop.
130 type BCEnv = FiniteMap Id Int -- To find vars on the stack
132 ppBCEnv :: BCEnv -> SDoc
135 $$ nest 4 (vcat (map pp_one (sortBy cmp_snd (fmToList p))))
138 pp_one (var, offset) = int offset <> colon <+> ppr var <+> ppr (idPrimRep var)
139 cmp_snd x y = compare (snd x) (snd y)
141 -- Create a BCO and do a spot of peephole optimisation on the insns
146 -> Either [AnnAlt Id VarSet] (AnnExpr Id VarSet)
150 -> Bool -- True <=> is a return point, rather than a function
153 mkProtoBCO nm instrs_ordlist origin arity bitmap_size bitmap
154 is_ret mallocd_blocks
157 protoBCOInstrs = maybe_with_stack_check,
158 protoBCOBitmap = bitmap,
159 protoBCOBitmapSize = bitmap_size,
160 protoBCOArity = arity,
161 protoBCOExpr = origin,
162 protoBCOPtrs = mallocd_blocks
165 -- Overestimate the stack usage (in words) of this BCO,
166 -- and if >= iNTERP_STACK_CHECK_THRESH, add an explicit
167 -- stack check. (The interpreter always does a stack check
168 -- for iNTERP_STACK_CHECK_THRESH words at the start of each
169 -- BCO anyway, so we only need to add an explicit on in the
170 -- (hopefully rare) cases when the (overestimated) stack use
171 -- exceeds iNTERP_STACK_CHECK_THRESH.
172 maybe_with_stack_check
174 -- don't do stack checks at return points;
175 -- everything is aggregated up to the top BCO
176 -- (which must be a function)
177 | stack_overest >= 65535
178 = pprPanic "mkProtoBCO: stack use won't fit in 16 bits"
180 | stack_overest >= iNTERP_STACK_CHECK_THRESH
181 = STKCHECK stack_overest : peep_d
183 = peep_d -- the supposedly common case
185 stack_overest = sum (map bciStackUse peep_d)
187 -- Merge local pushes
188 peep_d = peep (fromOL instrs_ordlist)
190 peep (PUSH_L off1 : PUSH_L off2 : PUSH_L off3 : rest)
191 = PUSH_LLL off1 (off2-1) (off3-2) : peep rest
192 peep (PUSH_L off1 : PUSH_L off2 : rest)
193 = PUSH_LL off1 (off2-1) : peep rest
199 argBits :: [PrimRep] -> [Bool]
202 | isFollowableRep rep = False : argBits args
203 | otherwise = take (getPrimRepSize rep) (repeat True) ++ argBits args
205 -- -----------------------------------------------------------------------------
208 -- Compile code for the right-hand side of a top-level binding
210 schemeTopBind :: (Id, AnnExpr Id VarSet) -> BcM (ProtoBCO Name)
213 schemeTopBind (id, rhs)
214 | Just data_con <- isDataConWorkId_maybe id,
215 isNullaryDataCon data_con
216 = -- Special case for the worker of a nullary data con.
217 -- It'll look like this: Nil = /\a -> Nil a
218 -- If we feed it into schemeR, we'll get
220 -- because mkConAppCode treats nullary constructor applications
221 -- by just re-using the single top-level definition. So
222 -- for the worker itself, we must allocate it directly.
223 emitBc (mkProtoBCO (getName id) (toOL [PACK data_con 0, ENTER])
224 (Right rhs) 0 0 [{-no bitmap-}] False{-not alts-})
227 = schemeR [{- No free variables -}] (id, rhs)
229 -- -----------------------------------------------------------------------------
232 -- Compile code for a right-hand side, to give a BCO that,
233 -- when executed with the free variables and arguments on top of the stack,
234 -- will return with a pointer to the result on top of the stack, after
235 -- removing the free variables and arguments.
237 -- Park the resulting BCO in the monad. Also requires the
238 -- variable to which this value was bound, so as to give the
239 -- resulting BCO a name.
241 schemeR :: [Id] -- Free vars of the RHS, ordered as they
242 -- will appear in the thunk. Empty for
243 -- top-level things, which have no free vars.
244 -> (Id, AnnExpr Id VarSet)
245 -> BcM (ProtoBCO Name)
246 schemeR fvs (nm, rhs)
250 $$ (ppr.filter (not.isTyVar).varSetElems.fst) rhs
251 $$ pprCoreExpr (deAnnotate rhs)
257 = schemeR_wrk fvs nm rhs (collect [] rhs)
259 collect xs (_, AnnNote note e) = collect xs e
260 collect xs (_, AnnLam x e) = collect (if isTyVar x then xs else (x:xs)) e
261 collect xs (_, not_lambda) = (reverse xs, not_lambda)
263 schemeR_wrk fvs nm original_body (args, body)
265 all_args = reverse args ++ fvs
266 arity = length all_args
267 -- all_args are the args in reverse order. We're compiling a function
268 -- \fv1..fvn x1..xn -> e
269 -- i.e. the fvs come first
271 szsw_args = map idSizeW all_args
272 szw_args = sum szsw_args
273 p_init = listToFM (zip all_args (mkStackOffsets 0 szsw_args))
275 -- make the arg bitmap
276 bits = argBits (reverse (map idPrimRep all_args))
277 bitmap_size = length bits
278 bitmap = mkBitmap bits
280 schemeE szw_args 0 p_init body `thenBc` \ body_code ->
281 emitBc (mkProtoBCO (getName nm) body_code (Right original_body)
282 arity bitmap_size bitmap False{-not alts-})
285 fvsToEnv :: BCEnv -> VarSet -> [Id]
286 -- Takes the free variables of a right-hand side, and
287 -- delivers an ordered list of the local variables that will
288 -- be captured in the thunk for the RHS
289 -- The BCEnv argument tells which variables are in the local
290 -- environment: these are the ones that should be captured
292 -- The code that constructs the thunk, and the code that executes
293 -- it, have to agree about this layout
294 fvsToEnv p fvs = [v | v <- varSetElems fvs,
295 isId v, -- Could be a type variable
298 -- -----------------------------------------------------------------------------
301 -- Compile code to apply the given expression to the remaining args
302 -- on the stack, returning a HNF.
303 schemeE :: Int -> Sequel -> BCEnv -> AnnExpr' Id VarSet -> BcM BCInstrList
305 -- Delegate tail-calls to schemeT.
306 schemeE d s p e@(AnnApp f a)
309 schemeE d s p e@(AnnVar v)
310 | not (isUnLiftedType v_type)
311 = -- Lifted-type thing; push it in the normal way
315 = -- Returning an unlifted value.
316 -- Heave it on the stack, SLIDE, and RETURN.
317 pushAtom d p (AnnVar v) `thenBc` \ (push, szw) ->
318 returnBc (push -- value onto stack
319 `appOL` mkSLIDE szw (d-s) -- clear to sequel
320 `snocOL` RETURN_UBX v_rep) -- go
323 v_rep = typePrimRep v_type
325 schemeE d s p (AnnLit literal)
326 = pushAtom d p (AnnLit literal) `thenBc` \ (push, szw) ->
327 let l_rep = literalPrimRep literal
328 in returnBc (push -- value onto stack
329 `appOL` mkSLIDE szw (d-s) -- clear to sequel
330 `snocOL` RETURN_UBX l_rep) -- go
333 schemeE d s p (AnnLet (AnnNonRec x (_,rhs)) (_,body))
334 | (AnnVar v, args_r_to_l) <- splitApp rhs,
335 Just data_con <- isDataConWorkId_maybe v,
336 dataConRepArity data_con == length args_r_to_l
337 = -- Special case for a non-recursive let whose RHS is a
338 -- saturatred constructor application.
339 -- Just allocate the constructor and carry on
340 mkConAppCode d s p data_con args_r_to_l `thenBc` \ alloc_code ->
341 schemeE (d+1) s (addToFM p x d) body `thenBc` \ body_code ->
342 returnBc (alloc_code `appOL` body_code)
344 -- General case for let. Generates correct, if inefficient, code in
346 schemeE d s p (AnnLet binds (_,body))
347 = let (xs,rhss) = case binds of AnnNonRec x rhs -> ([x],[rhs])
348 AnnRec xs_n_rhss -> unzip xs_n_rhss
351 fvss = map (fvsToEnv p' . fst) rhss
353 -- Sizes of free vars
354 sizes = map (\rhs_fvs -> sum (map idSizeW rhs_fvs)) fvss
356 -- the arity of each rhs
357 arities = map (length . fst . collect []) rhss
359 -- This p', d' defn is safe because all the items being pushed
360 -- are ptrs, so all have size 1. d' and p' reflect the stack
361 -- after the closures have been allocated in the heap (but not
362 -- filled in), and pointers to them parked on the stack.
363 p' = addListToFM p (zipE xs (mkStackOffsets d (nOfThem n_binds 1)))
365 zipE = zipEqual "schemeE"
367 -- ToDo: don't build thunks for things with no free variables
368 build_thunk dd [] size bco off
369 = returnBc (PUSH_BCO bco
370 `consOL` unitOL (MKAP (off+size) size))
371 build_thunk dd (fv:fvs) size bco off = do
372 (push_code, pushed_szw) <- pushAtom dd p' (AnnVar fv)
373 more_push_code <- build_thunk (dd+pushed_szw) fvs size bco off
374 returnBc (push_code `appOL` more_push_code)
376 alloc_code = toOL (zipWith mkAlloc sizes arities)
377 where mkAlloc sz 0 = ALLOC_AP sz
378 mkAlloc sz arity = ALLOC_PAP arity sz
380 compile_bind d' fvs x rhs size off = do
381 bco <- schemeR fvs (x,rhs)
382 build_thunk d' fvs size bco off
385 [ compile_bind d' fvs x rhs size n
386 | (fvs, x, rhs, size, n) <-
387 zip5 fvss xs rhss sizes [n_binds, n_binds-1 .. 1]
390 body_code <- schemeE d' s p' body
391 thunk_codes <- sequence compile_binds
392 returnBc (alloc_code `appOL` concatOL thunk_codes `appOL` body_code)
396 schemeE d s p (AnnCase scrut bndr [(DataAlt dc, [bind1, bind2], rhs)])
397 | isUnboxedTupleCon dc && VoidRep == typePrimRep (idType bind1)
399 -- case .... of x { (# VoidRep'd-thing, a #) -> ... }
401 -- case .... of a { DEFAULT -> ... }
402 -- becuse the return convention for both are identical.
404 -- Note that it does not matter losing the void-rep thing from the
405 -- envt (it won't be bound now) because we never look such things up.
407 = --trace "automagic mashing of case alts (# VoidRep, a #)" $
408 doCase d s p scrut bind2 [(DEFAULT, [], rhs)] True{-unboxed tuple-}
410 | isUnboxedTupleCon dc && VoidRep == typePrimRep (idType bind2)
411 = --trace "automagic mashing of case alts (# a, VoidRep #)" $
412 doCase d s p scrut bind1 [(DEFAULT, [], rhs)] True{-unboxed tuple-}
414 schemeE d s p (AnnCase scrut bndr [(DataAlt dc, [bind1], rhs)])
415 | isUnboxedTupleCon dc
416 -- Similarly, convert
417 -- case .... of x { (# a #) -> ... }
419 -- case .... of a { DEFAULT -> ... }
420 = --trace "automagic mashing of case alts (# a #)" $
421 doCase d s p scrut bind1 [(DEFAULT, [], rhs)] True{-unboxed tuple-}
423 schemeE d s p (AnnCase scrut bndr alts)
424 = doCase d s p scrut bndr alts False{-not an unboxed tuple-}
426 schemeE d s p (AnnNote note (_, body))
430 = pprPanic "ByteCodeGen.schemeE: unhandled case"
431 (pprCoreExpr (deAnnotate' other))
434 -- Compile code to do a tail call. Specifically, push the fn,
435 -- slide the on-stack app back down to the sequel depth,
436 -- and enter. Four cases:
439 -- An application "GHC.Prim.tagToEnum# <type> unboxed-int".
440 -- The int will be on the stack. Generate a code sequence
441 -- to convert it to the relevant constructor, SLIDE and ENTER.
443 -- 1. The fn denotes a ccall. Defer to generateCCall.
445 -- 2. (Another nasty hack). Spot (# a::VoidRep, b #) and treat
446 -- it simply as b -- since the representations are identical
447 -- (the VoidRep takes up zero stack space). Also, spot
448 -- (# b #) and treat it as b.
450 -- 3. Application of a constructor, by defn saturated.
451 -- Split the args into ptrs and non-ptrs, and push the nonptrs,
452 -- then the ptrs, and then do PACK and RETURN.
454 -- 4. Otherwise, it must be a function call. Push the args
455 -- right to left, SLIDE and ENTER.
457 schemeT :: Int -- Stack depth
458 -> Sequel -- Sequel depth
459 -> BCEnv -- stack env
460 -> AnnExpr' Id VarSet
465 -- | trace ("schemeT: env in = \n" ++ showSDocDebug (ppBCEnv p)) False
466 -- = panic "schemeT ?!?!"
468 -- | trace ("\nschemeT\n" ++ showSDoc (pprCoreExpr (deAnnotate' app)) ++ "\n") False
472 | Just (arg, constr_names) <- maybe_is_tagToEnum_call
473 = pushAtom d p arg `thenBc` \ (push, arg_words) ->
474 implement_tagToId constr_names `thenBc` \ tagToId_sequence ->
475 returnBc (push `appOL` tagToId_sequence
476 `appOL` mkSLIDE 1 (d+arg_words-s)
480 | Just (CCall ccall_spec) <- isFCallId_maybe fn
481 = generateCCall d s p ccall_spec fn args_r_to_l
483 -- Case 2: Constructor application
484 | Just con <- maybe_saturated_dcon,
485 isUnboxedTupleCon con
486 = case args_r_to_l of
487 [arg1,arg2] | isVoidRepAtom arg1 ->
488 unboxedTupleReturn d s p arg2
489 [arg1,arg2] | isVoidRepAtom arg2 ->
490 unboxedTupleReturn d s p arg1
491 _other -> unboxedTupleException
493 -- Case 3: Ordinary data constructor
494 | Just con <- maybe_saturated_dcon
495 = mkConAppCode d s p con args_r_to_l `thenBc` \ alloc_con ->
496 returnBc (alloc_con `appOL`
497 mkSLIDE 1 (d - s) `snocOL`
500 -- Case 4: Tail call of function
502 = doTailCall d s p fn args_r_to_l
505 -- Detect and extract relevant info for the tagToEnum kludge.
506 maybe_is_tagToEnum_call
507 = let extract_constr_Names ty
508 | Just (tyc, []) <- splitTyConApp_maybe (repType ty),
510 = map (getName . dataConWorkId) (tyConDataCons tyc)
511 -- NOTE: use the worker name, not the source name of
512 -- the DataCon. See DataCon.lhs for details.
514 = panic "maybe_is_tagToEnum_call.extract_constr_Ids"
517 (AnnApp (_, AnnApp (_, AnnVar v) (_, AnnType t)) arg)
518 -> case isPrimOpId_maybe v of
519 Just TagToEnumOp -> Just (snd arg, extract_constr_Names t)
523 -- Extract the args (R->L) and fn
524 -- The function will necessarily be a variable,
525 -- because we are compiling a tail call
526 (AnnVar fn, args_r_to_l) = splitApp app
528 -- Only consider this to be a constructor application iff it is
529 -- saturated. Otherwise, we'll call the constructor wrapper.
530 n_args = length args_r_to_l
532 = case isDataConWorkId_maybe fn of
533 Just con | dataConRepArity con == n_args -> Just con
536 -- -----------------------------------------------------------------------------
537 -- Generate code to build a constructor application,
538 -- leaving it on top of the stack
540 mkConAppCode :: Int -> Sequel -> BCEnv
541 -> DataCon -- The data constructor
542 -> [AnnExpr' Id VarSet] -- Args, in *reverse* order
545 mkConAppCode orig_d s p con [] -- Nullary constructor
546 = ASSERT( isNullaryDataCon con )
547 returnBc (unitOL (PUSH_G (getName (dataConWorkId con))))
548 -- Instead of doing a PACK, which would allocate a fresh
549 -- copy of this constructor, use the single shared version.
551 mkConAppCode orig_d s p con args_r_to_l
552 = ASSERT( dataConRepArity con == length args_r_to_l )
553 do_pushery orig_d (non_ptr_args ++ ptr_args)
555 -- The args are already in reverse order, which is the way PACK
556 -- expects them to be. We must push the non-ptrs after the ptrs.
557 (ptr_args, non_ptr_args) = partition isPtrAtom args_r_to_l
559 do_pushery d (arg:args)
560 = pushAtom d p arg `thenBc` \ (push, arg_words) ->
561 do_pushery (d+arg_words) args `thenBc` \ more_push_code ->
562 returnBc (push `appOL` more_push_code)
564 = returnBc (unitOL (PACK con n_arg_words))
566 n_arg_words = d - orig_d
569 -- -----------------------------------------------------------------------------
570 -- Returning an unboxed tuple with one non-void component (the only
571 -- case we can handle).
573 -- Remember, we don't want to *evaluate* the component that is being
574 -- returned, even if it is a pointed type. We always just return.
577 :: Int -> Sequel -> BCEnv
578 -> AnnExpr' Id VarSet -> BcM BCInstrList
579 unboxedTupleReturn d s p arg = do
580 (push, sz) <- pushAtom d p arg
581 returnBc (push `appOL`
582 mkSLIDE sz (d-s) `snocOL`
583 RETURN_UBX (atomRep arg))
585 -- -----------------------------------------------------------------------------
586 -- Generate code for a tail-call
589 :: Int -> Sequel -> BCEnv
590 -> Id -> [AnnExpr' Id VarSet]
592 doTailCall init_d s p fn args
593 = do_pushes init_d args (map (primRepToArgRep.atomRep) args)
595 do_pushes d [] reps = do
597 (push_fn, sz) <- pushAtom d p (AnnVar fn)
599 returnBc (push_fn `appOL` (
600 mkSLIDE ((d-init_d) + 1) (init_d - s) `appOL`
602 do_pushes d args reps = do
603 let (push_apply, n, rest_of_reps) = findPushSeq reps
604 (these_args, rest_of_args) = splitAt n args
605 (next_d, push_code) <- push_seq d these_args
606 instrs <- do_pushes (next_d + 1) rest_of_args rest_of_reps
607 -- ^^^ for the PUSH_APPLY_ instruction
608 returnBc (push_code `appOL` (push_apply `consOL` instrs))
610 push_seq d [] = return (d, nilOL)
611 push_seq d (arg:args) = do
612 (push_code, sz) <- pushAtom d p arg
613 (final_d, more_push_code) <- push_seq (d+sz) args
614 return (final_d, push_code `appOL` more_push_code)
616 -- v. similar to CgStackery.findMatch, ToDo: merge
617 findPushSeq (RepP: RepP: RepP: RepP: RepP: RepP: RepP: rest)
618 = (PUSH_APPLY_PPPPPPP, 7, rest)
619 findPushSeq (RepP: RepP: RepP: RepP: RepP: RepP: rest)
620 = (PUSH_APPLY_PPPPPP, 6, rest)
621 findPushSeq (RepP: RepP: RepP: RepP: RepP: rest)
622 = (PUSH_APPLY_PPPPP, 5, rest)
623 findPushSeq (RepP: RepP: RepP: RepP: rest)
624 = (PUSH_APPLY_PPPP, 4, rest)
625 findPushSeq (RepP: RepP: RepP: rest)
626 = (PUSH_APPLY_PPP, 3, rest)
627 findPushSeq (RepP: RepP: rest)
628 = (PUSH_APPLY_PP, 2, rest)
629 findPushSeq (RepP: rest)
630 = (PUSH_APPLY_P, 1, rest)
631 findPushSeq (RepV: rest)
632 = (PUSH_APPLY_V, 1, rest)
633 findPushSeq (RepN: rest)
634 = (PUSH_APPLY_N, 1, rest)
635 findPushSeq (RepF: rest)
636 = (PUSH_APPLY_F, 1, rest)
637 findPushSeq (RepD: rest)
638 = (PUSH_APPLY_D, 1, rest)
639 findPushSeq (RepL: rest)
640 = (PUSH_APPLY_L, 1, rest)
642 = panic "ByteCodeGen.findPushSeq"
644 -- -----------------------------------------------------------------------------
647 doCase :: Int -> Sequel -> BCEnv
648 -> AnnExpr Id VarSet -> Id -> [AnnAlt Id VarSet]
649 -> Bool -- True <=> is an unboxed tuple case, don't enter the result
651 doCase d s p (_,scrut)
652 bndr alts is_unboxed_tuple
654 -- Top of stack is the return itbl, as usual.
655 -- underneath it is the pointer to the alt_code BCO.
656 -- When an alt is entered, it assumes the returned value is
657 -- on top of the itbl.
660 -- An unlifted value gets an extra info table pushed on top
661 -- when it is returned.
662 unlifted_itbl_sizeW | isAlgCase = 0
665 -- depth of stack after the return value has been pushed
666 d_bndr = d + ret_frame_sizeW + idSizeW bndr
668 -- depth of stack after the extra info table for an unboxed return
669 -- has been pushed, if any. This is the stack depth at the
671 d_alts = d_bndr + unlifted_itbl_sizeW
673 -- Env in which to compile the alts, not including
674 -- any vars bound by the alts themselves
675 p_alts = addToFM p bndr (d_bndr - 1)
677 bndr_ty = idType bndr
678 isAlgCase = not (isUnLiftedType bndr_ty) && not is_unboxed_tuple
680 -- given an alt, return a discr and code for it.
681 codeALt alt@(DEFAULT, _, (_,rhs))
682 = schemeE d_alts s p_alts rhs `thenBc` \ rhs_code ->
683 returnBc (NoDiscr, rhs_code)
684 codeAlt alt@(discr, bndrs, (_,rhs))
685 -- primitive or nullary constructor alt: no need to UNPACK
686 | null real_bndrs = do
687 rhs_code <- schemeE d_alts s p_alts rhs
688 returnBc (my_discr alt, rhs_code)
689 -- algebraic alt with some binders
690 | ASSERT(isAlgCase) otherwise =
692 (ptrs,nptrs) = partition (isFollowableRep.idPrimRep) real_bndrs
693 ptr_sizes = map idSizeW ptrs
694 nptrs_sizes = map idSizeW nptrs
695 bind_sizes = ptr_sizes ++ nptrs_sizes
696 size = sum ptr_sizes + sum nptrs_sizes
697 -- the UNPACK instruction unpacks in reverse order...
698 p' = addListToFM p_alts
699 (zip (reverse (ptrs ++ nptrs))
700 (mkStackOffsets d_alts (reverse bind_sizes)))
702 rhs_code <- schemeE (d_alts+size) s p' rhs
703 return (my_discr alt, unitOL (UNPACK size) `appOL` rhs_code)
705 real_bndrs = filter (not.isTyVar) bndrs
708 my_discr (DEFAULT, binds, rhs) = NoDiscr {-shouldn't really happen-}
709 my_discr (DataAlt dc, binds, rhs)
710 | isUnboxedTupleCon dc
711 = unboxedTupleException
713 = DiscrP (dataConTag dc - fIRST_TAG)
714 my_discr (LitAlt l, binds, rhs)
715 = case l of MachInt i -> DiscrI (fromInteger i)
716 MachFloat r -> DiscrF (fromRational r)
717 MachDouble r -> DiscrD (fromRational r)
718 MachChar i -> DiscrI i
719 _ -> pprPanic "schemeE(AnnCase).my_discr" (ppr l)
722 | not isAlgCase = Nothing
724 = case [dc | (DataAlt dc, _, _) <- alts] of
726 (dc:_) -> Just (tyConFamilySize (dataConTyCon dc))
728 -- the bitmap is relative to stack depth d, i.e. before the
729 -- BCO, info table and return value are pushed on.
730 -- This bit of code is v. similar to buildLivenessMask in CgBindery,
731 -- except that here we build the bitmap from the known bindings of
732 -- things that are pointers, whereas in CgBindery the code builds the
733 -- bitmap from the free slots and unboxed bindings.
735 bitmap = intsToReverseBitmap d{-size-} (sortLt (<) rel_slots)
738 rel_slots = concat (map spread binds)
740 | isFollowableRep (idPrimRep id) = [ rel_offset ]
742 where rel_offset = d - offset - 1
745 alt_stuff <- mapM codeAlt alts
746 alt_final <- mkMultiBranch maybe_ncons alt_stuff
748 alt_bco_name = getName bndr
749 alt_bco = mkProtoBCO alt_bco_name alt_final (Left alts)
750 0{-no arity-} d{-bitmap size-} bitmap True{-is alts-}
752 -- trace ("case: bndr = " ++ showSDocDebug (ppr bndr) ++ "\ndepth = " ++ show d ++ "\nenv = \n" ++ showSDocDebug (ppBCEnv p) ++
753 -- "\n bitmap = " ++ show bitmap) $ do
754 scrut_code <- schemeE (d + ret_frame_sizeW) (d + ret_frame_sizeW) p scrut
755 alt_bco' <- emitBc alt_bco
757 | isAlgCase = PUSH_ALTS alt_bco'
758 | otherwise = PUSH_ALTS_UNLIFTED alt_bco' (typePrimRep bndr_ty)
759 returnBc (push_alts `consOL` scrut_code)
762 -- -----------------------------------------------------------------------------
763 -- Deal with a CCall.
765 -- Taggedly push the args onto the stack R->L,
766 -- deferencing ForeignObj#s and adjusting addrs to point to
767 -- payloads in Ptr/Byte arrays. Then, generate the marshalling
768 -- (machine) code for the ccall, and create bytecodes to call that and
769 -- then return in the right way.
771 generateCCall :: Int -> Sequel -- stack and sequel depths
773 -> CCallSpec -- where to call
774 -> Id -- of target, for type info
775 -> [AnnExpr' Id VarSet] -- args (atoms)
778 generateCCall d0 s p ccall_spec@(CCallSpec target cconv safety) fn args_r_to_l
781 addr_sizeW = getPrimRepSize AddrRep
783 -- Get the args on the stack, with tags and suitably
784 -- dereferenced for the CCall. For each arg, return the
785 -- depth to the first word of the bits for that arg, and the
786 -- PrimRep of what was actually pushed.
788 pargs d [] = returnBc []
790 = let arg_ty = repType (exprType (deAnnotate' a))
792 in case splitTyConApp_maybe arg_ty of
793 -- Don't push the FO; instead push the Addr# it
796 | t == arrayPrimTyCon || t == mutableArrayPrimTyCon
797 -> pargs (d + addr_sizeW) az `thenBc` \ rest ->
798 parg_ArrayishRep arrPtrsHdrSize d p a
800 returnBc ((code,AddrRep):rest)
802 | t == byteArrayPrimTyCon || t == mutableByteArrayPrimTyCon
803 -> pargs (d + addr_sizeW) az `thenBc` \ rest ->
804 parg_ArrayishRep arrWordsHdrSize d p a
806 returnBc ((code,AddrRep):rest)
808 -- Default case: push taggedly, but otherwise intact.
810 -> pushAtom d p a `thenBc` \ (code_a, sz_a) ->
811 pargs (d+sz_a) az `thenBc` \ rest ->
812 returnBc ((code_a, atomRep a) : rest)
814 -- Do magic for Ptr/Byte arrays. Push a ptr to the array on
815 -- the stack but then advance it over the headers, so as to
816 -- point to the payload.
817 parg_ArrayishRep hdrSizeW d p a
818 = pushAtom d p a `thenBc` \ (push_fo, _) ->
819 -- The ptr points at the header. Advance it over the
820 -- header and then pretend this is an Addr#.
821 returnBc (push_fo `snocOL`
822 SWIZZLE 0 (hdrSizeW * getPrimRepSize WordRep
826 pargs d0 args_r_to_l `thenBc` \ code_n_reps ->
828 (pushs_arg, a_reps_pushed_r_to_l) = unzip code_n_reps
830 push_args = concatOL pushs_arg
831 d_after_args = d0 + sum (map getPrimRepSize a_reps_pushed_r_to_l)
833 | null a_reps_pushed_r_to_l || head a_reps_pushed_r_to_l /= VoidRep
834 = panic "ByteCodeGen.generateCCall: missing or invalid World token?"
836 = reverse (tail a_reps_pushed_r_to_l)
838 -- Now: a_reps_pushed_RAW are the reps which are actually on the stack.
839 -- push_args is the code to do that.
840 -- d_after_args is the stack depth once the args are on.
842 -- Get the result rep.
843 (returns_void, r_rep)
844 = case maybe_getCCallReturnRep (idType fn) of
845 Nothing -> (True, VoidRep)
846 Just rr -> (False, rr)
848 Because the Haskell stack grows down, the a_reps refer to
849 lowest to highest addresses in that order. The args for the call
850 are on the stack. Now push an unboxed Addr# indicating
851 the C function to call. Then push a dummy placeholder for the
852 result. Finally, emit a CCALL insn with an offset pointing to the
853 Addr# just pushed, and a literal field holding the mallocville
854 address of the piece of marshalling code we generate.
855 So, just prior to the CCALL insn, the stack looks like this
856 (growing down, as usual):
861 Addr# address_of_C_fn
862 <placeholder-for-result#> (must be an unboxed type)
864 The interpreter then calls the marshall code mentioned
865 in the CCALL insn, passing it (& <placeholder-for-result#>),
866 that is, the addr of the topmost word in the stack.
867 When this returns, the placeholder will have been
868 filled in. The placeholder is slid down to the sequel
869 depth, and we RETURN.
871 This arrangement makes it simple to do f-i-dynamic since the Addr#
872 value is the first arg anyway.
874 The marshalling code is generated specifically for this
875 call site, and so knows exactly the (Haskell) stack
876 offsets of the args, fn address and placeholder. It
877 copies the args to the C stack, calls the stacked addr,
878 and parks the result back in the placeholder. The interpreter
879 calls it as a normal C call, assuming it has a signature
880 void marshall_code ( StgWord* ptr_to_top_of_stack )
882 -- resolve static address
886 -> returnBc (False, panic "ByteCodeGen.generateCCall(dyn)")
888 -> ioToBc (lookupStaticPtr target) `thenBc` \res ->
891 get_target_info `thenBc` \ (is_static, static_target_addr) ->
894 -- Get the arg reps, zapping the leading Addr# in the dynamic case
895 a_reps -- | trace (showSDoc (ppr a_reps_pushed_RAW)) False = error "???"
896 | is_static = a_reps_pushed_RAW
897 | otherwise = if null a_reps_pushed_RAW
898 then panic "ByteCodeGen.generateCCall: dyn with no args"
899 else tail a_reps_pushed_RAW
902 (push_Addr, d_after_Addr)
904 = (toOL [PUSH_UBX (Right static_target_addr) addr_sizeW],
905 d_after_args + addr_sizeW)
906 | otherwise -- is already on the stack
907 = (nilOL, d_after_args)
909 -- Push the return placeholder. For a call returning nothing,
910 -- this is a VoidRep (tag).
911 r_sizeW = getPrimRepSize r_rep
912 d_after_r = d_after_Addr + r_sizeW
913 r_lit = mkDummyLiteral r_rep
914 push_r = (if returns_void
916 else unitOL (PUSH_UBX (Left r_lit) r_sizeW))
918 -- generate the marshalling code we're going to call
921 arg1_offW = r_sizeW + addr_sizeW
922 args_offW = map (arg1_offW +)
923 (init (scanl (+) 0 (map getPrimRepSize a_reps)))
925 ioToBc (mkMarshalCode cconv
926 (r_offW, r_rep) addr_offW
927 (zip args_offW a_reps)) `thenBc` \ addr_of_marshaller ->
928 recordMallocBc addr_of_marshaller `thenBc_`
930 -- Offset of the next stack frame down the stack. The CCALL
931 -- instruction needs to describe the chunk of stack containing
932 -- the ccall args to the GC, so it needs to know how large it
933 -- is. See comment in Interpreter.c with the CCALL instruction.
934 stk_offset = d_after_r - s
937 do_call = unitOL (CCALL stk_offset (castPtr addr_of_marshaller))
939 wrapup = mkSLIDE r_sizeW (d_after_r - r_sizeW - s)
940 `snocOL` RETURN_UBX r_rep
942 --trace (show (arg1_offW, args_offW , (map getPrimRepSize a_reps) )) $
945 push_Addr `appOL` push_r `appOL` do_call `appOL` wrapup
949 -- Make a dummy literal, to be used as a placeholder for FFI return
950 -- values on the stack.
951 mkDummyLiteral :: PrimRep -> Literal
954 CharRep -> MachChar 0
956 WordRep -> MachWord 0
957 DoubleRep -> MachDouble 0
958 FloatRep -> MachFloat 0
959 AddrRep | getPrimRepSize AddrRep == getPrimRepSize WordRep -> MachWord 0
960 _ -> moan64 "mkDummyLiteral" (ppr pr)
964 -- GHC.Prim.Char# -> GHC.Prim.State# GHC.Prim.RealWorld
965 -- -> (# GHC.Prim.State# GHC.Prim.RealWorld, GHC.Prim.Int# #)
968 -- and check that an unboxed pair is returned wherein the first arg is VoidRep'd.
970 -- Alternatively, for call-targets returning nothing, convert
972 -- GHC.Prim.Char# -> GHC.Prim.State# GHC.Prim.RealWorld
973 -- -> (# GHC.Prim.State# GHC.Prim.RealWorld #)
977 maybe_getCCallReturnRep :: Type -> Maybe PrimRep
978 maybe_getCCallReturnRep fn_ty
979 = let (a_tys, r_ty) = splitFunTys (dropForAlls fn_ty)
981 = if isSingleton r_reps then Nothing else Just (r_reps !! 1)
983 = case splitTyConApp_maybe (repType r_ty) of
984 (Just (tyc, tys)) -> (tyc, map typePrimRep tys)
986 ok = ( ( r_reps `lengthIs` 2 && VoidRep == head r_reps)
987 || r_reps == [VoidRep] )
988 && isUnboxedTupleTyCon r_tycon
989 && case maybe_r_rep_to_go of
991 Just r_rep -> r_rep /= PtrRep
992 -- if it was, it would be impossible
993 -- to create a valid return value
994 -- placeholder on the stack
995 blargh = pprPanic "maybe_getCCallReturn: can't handle:"
998 --trace (showSDoc (ppr (a_reps, r_reps))) $
999 if ok then maybe_r_rep_to_go else blargh
1001 -- Compile code which expects an unboxed Int on the top of stack,
1002 -- (call it i), and pushes the i'th closure in the supplied list
1003 -- as a consequence.
1004 implement_tagToId :: [Name] -> BcM BCInstrList
1005 implement_tagToId names
1006 = ASSERT( notNull names )
1007 getLabelsBc (length names) `thenBc` \ labels ->
1008 getLabelBc `thenBc` \ label_fail ->
1009 getLabelBc `thenBc` \ label_exit ->
1010 zip4 labels (tail labels ++ [label_fail])
1011 [0 ..] names `bind` \ infos ->
1012 map (mkStep label_exit) infos `bind` \ steps ->
1013 returnBc (concatOL steps
1015 toOL [LABEL label_fail, CASEFAIL, LABEL label_exit])
1017 mkStep l_exit (my_label, next_label, n, name_for_n)
1018 = toOL [LABEL my_label,
1019 TESTEQ_I n next_label,
1024 -- -----------------------------------------------------------------------------
1027 -- Push an atom onto the stack, returning suitable code & number of
1028 -- stack words used.
1030 -- The env p must map each variable to the highest- numbered stack
1031 -- slot for it. For example, if the stack has depth 4 and we
1032 -- tagged-ly push (v :: Int#) on it, the value will be in stack[4],
1033 -- the tag in stack[5], the stack will have depth 6, and p must map v
1034 -- to 5 and not to 4. Stack locations are numbered from zero, so a
1035 -- depth 6 stack has valid words 0 .. 5.
1037 pushAtom :: Int -> BCEnv -> AnnExpr' Id VarSet -> BcM (BCInstrList, Int)
1039 pushAtom d p (AnnApp f (_, AnnType _))
1040 = pushAtom d p (snd f)
1042 pushAtom d p (AnnNote note e)
1043 = pushAtom d p (snd e)
1045 pushAtom d p (AnnLam x e)
1047 = pushAtom d p (snd e)
1049 pushAtom d p (AnnVar v)
1051 | idPrimRep v == VoidRep
1052 = returnBc (nilOL, 0)
1055 = pprPanic "pushAtom: shouldn't get an FCallId here" (ppr v)
1057 | Just primop <- isPrimOpId_maybe v
1058 = returnBc (unitOL (PUSH_PRIMOP primop), 1)
1060 | Just d_v <- lookupBCEnv_maybe p v -- v is a local variable
1061 = returnBc (toOL (nOfThem sz (PUSH_L (d-d_v+sz-2))), sz)
1062 -- d - d_v the number of words between the TOS
1063 -- and the 1st slot of the object
1065 -- d - d_v - 1 the offset from the TOS of the 1st slot
1067 -- d - d_v - 1 + sz - 1 the offset from the TOS of the last slot
1070 -- Having found the last slot, we proceed to copy the right number of
1071 -- slots on to the top of the stack.
1073 | otherwise -- v must be a global variable
1075 returnBc (unitOL (PUSH_G (getName v)), sz)
1081 pushAtom d p (AnnLit lit)
1083 MachLabel fs _ -> code CodePtrRep
1084 MachWord w -> code WordRep
1085 MachInt i -> code IntRep
1086 MachFloat r -> code FloatRep
1087 MachDouble r -> code DoubleRep
1088 MachChar c -> code CharRep
1089 MachStr s -> pushStr s
1092 = let size_host_words = getPrimRepSize rep
1093 in returnBc (unitOL (PUSH_UBX (Left lit) size_host_words),
1097 = let getMallocvilleAddr
1099 FastString _ l ba ->
1100 -- sigh, a string in the heap is no good to us.
1101 -- We need a static C pointer, since the type of
1102 -- a string literal is Addr#. So, copy the string
1103 -- into C land and remember the pointer so we can
1106 -- CAREFUL! Chars are 32 bits in ghc 4.09+
1107 in ioToBc (mallocBytes (n+1)) `thenBc` \ ptr ->
1108 recordMallocBc ptr `thenBc_`
1110 do memcpy ptr ba (fromIntegral n)
1111 pokeByteOff ptr n (fromIntegral (ord '\0') :: Word8)
1114 other -> panic "ByteCodeGen.pushAtom.pushStr"
1116 getMallocvilleAddr `thenBc` \ addr ->
1117 -- Get the addr on the stack, untaggedly
1118 returnBc (unitOL (PUSH_UBX (Right addr) 1), 1)
1121 = pprPanic "ByteCodeGen.pushAtom"
1122 (pprCoreExpr (deAnnotate (undefined, other)))
1124 foreign import ccall unsafe "memcpy"
1125 memcpy :: Ptr a -> ByteArray# -> CInt -> IO ()
1128 -- -----------------------------------------------------------------------------
1129 -- Given a bunch of alts code and their discrs, do the donkey work
1130 -- of making a multiway branch using a switch tree.
1131 -- What a load of hassle!
1133 mkMultiBranch :: Maybe Int -- # datacons in tycon, if alg alt
1134 -- a hint; generates better code
1135 -- Nothing is always safe
1136 -> [(Discr, BCInstrList)]
1138 mkMultiBranch maybe_ncons raw_ways
1139 = let d_way = filter (isNoDiscr.fst) raw_ways
1140 notd_ways = naturalMergeSortLe
1141 (\w1 w2 -> leAlt (fst w1) (fst w2))
1142 (filter (not.isNoDiscr.fst) raw_ways)
1144 mkTree :: [(Discr, BCInstrList)] -> Discr -> Discr -> BcM BCInstrList
1145 mkTree [] range_lo range_hi = returnBc the_default
1147 mkTree [val] range_lo range_hi
1148 | range_lo `eqAlt` range_hi
1149 = returnBc (snd val)
1151 = getLabelBc `thenBc` \ label_neq ->
1152 returnBc (mkTestEQ (fst val) label_neq
1154 `appOL` unitOL (LABEL label_neq)
1155 `appOL` the_default))
1157 mkTree vals range_lo range_hi
1158 = let n = length vals `div` 2
1159 vals_lo = take n vals
1160 vals_hi = drop n vals
1161 v_mid = fst (head vals_hi)
1163 getLabelBc `thenBc` \ label_geq ->
1164 mkTree vals_lo range_lo (dec v_mid) `thenBc` \ code_lo ->
1165 mkTree vals_hi v_mid range_hi `thenBc` \ code_hi ->
1166 returnBc (mkTestLT v_mid label_geq
1168 `appOL` unitOL (LABEL label_geq)
1172 = case d_way of [] -> unitOL CASEFAIL
1175 -- None of these will be needed if there are no non-default alts
1176 (mkTestLT, mkTestEQ, init_lo, init_hi)
1178 = panic "mkMultiBranch: awesome foursome"
1180 = case fst (head notd_ways) of {
1181 DiscrI _ -> ( \(DiscrI i) fail_label -> TESTLT_I i fail_label,
1182 \(DiscrI i) fail_label -> TESTEQ_I i fail_label,
1185 DiscrF _ -> ( \(DiscrF f) fail_label -> TESTLT_F f fail_label,
1186 \(DiscrF f) fail_label -> TESTEQ_F f fail_label,
1189 DiscrD _ -> ( \(DiscrD d) fail_label -> TESTLT_D d fail_label,
1190 \(DiscrD d) fail_label -> TESTEQ_D d fail_label,
1193 DiscrP _ -> ( \(DiscrP i) fail_label -> TESTLT_P i fail_label,
1194 \(DiscrP i) fail_label -> TESTEQ_P i fail_label,
1196 DiscrP algMaxBound )
1199 (algMinBound, algMaxBound)
1200 = case maybe_ncons of
1201 Just n -> (0, n - 1)
1202 Nothing -> (minBound, maxBound)
1204 (DiscrI i1) `eqAlt` (DiscrI i2) = i1 == i2
1205 (DiscrF f1) `eqAlt` (DiscrF f2) = f1 == f2
1206 (DiscrD d1) `eqAlt` (DiscrD d2) = d1 == d2
1207 (DiscrP i1) `eqAlt` (DiscrP i2) = i1 == i2
1208 NoDiscr `eqAlt` NoDiscr = True
1211 (DiscrI i1) `leAlt` (DiscrI i2) = i1 <= i2
1212 (DiscrF f1) `leAlt` (DiscrF f2) = f1 <= f2
1213 (DiscrD d1) `leAlt` (DiscrD d2) = d1 <= d2
1214 (DiscrP i1) `leAlt` (DiscrP i2) = i1 <= i2
1215 NoDiscr `leAlt` NoDiscr = True
1218 isNoDiscr NoDiscr = True
1221 dec (DiscrI i) = DiscrI (i-1)
1222 dec (DiscrP i) = DiscrP (i-1)
1223 dec other = other -- not really right, but if you
1224 -- do cases on floating values, you'll get what you deserve
1226 -- same snotty comment applies to the following
1228 minD, maxD :: Double
1234 mkTree notd_ways init_lo init_hi
1237 -- -----------------------------------------------------------------------------
1238 -- Supporting junk for the compilation schemes
1240 -- Describes case alts
1248 instance Outputable Discr where
1249 ppr (DiscrI i) = int i
1250 ppr (DiscrF f) = text (show f)
1251 ppr (DiscrD d) = text (show d)
1252 ppr (DiscrP i) = int i
1253 ppr NoDiscr = text "DEF"
1256 lookupBCEnv_maybe :: BCEnv -> Id -> Maybe Int
1257 lookupBCEnv_maybe = lookupFM
1259 idSizeW :: Id -> Int
1260 idSizeW id = getPrimRepSize (typePrimRep (idType id))
1262 unboxedTupleException :: a
1263 unboxedTupleException
1266 ("Bytecode generator can't handle unboxed tuples. Possibly due\n" ++
1267 "\tto foreign import/export decls in source. Workaround:\n" ++
1268 "\tcompile this module to a .o file, then restart session."))
1271 mkSLIDE n d = if d == 0 then nilOL else unitOL (SLIDE n d)
1274 splitApp :: AnnExpr' id ann -> (AnnExpr' id ann, [AnnExpr' id ann])
1275 -- The arguments are returned in *right-to-left* order
1276 splitApp (AnnApp (_,f) (_,a))
1277 | isTypeAtom a = splitApp f
1278 | otherwise = case splitApp f of
1279 (f', as) -> (f', a:as)
1280 splitApp (AnnNote n (_,e)) = splitApp e
1281 splitApp e = (e, [])
1284 isTypeAtom :: AnnExpr' id ann -> Bool
1285 isTypeAtom (AnnType _) = True
1286 isTypeAtom _ = False
1288 isVoidRepAtom :: AnnExpr' id ann -> Bool
1289 isVoidRepAtom (AnnVar v) = typePrimRep (idType v) == VoidRep
1290 isVoidRepAtom (AnnNote n (_,e)) = isVoidRepAtom e
1291 isVoidRepAtom _ = False
1293 atomRep :: AnnExpr' Id ann -> PrimRep
1294 atomRep (AnnVar v) = typePrimRep (idType v)
1295 atomRep (AnnLit l) = literalPrimRep l
1296 atomRep (AnnNote n b) = atomRep (snd b)
1297 atomRep (AnnApp f (_, AnnType _)) = atomRep (snd f)
1298 atomRep (AnnLam x e) | isTyVar x = atomRep (snd e)
1299 atomRep other = pprPanic "atomRep" (ppr (deAnnotate (undefined,other)))
1301 isPtrAtom :: AnnExpr' Id ann -> Bool
1302 isPtrAtom e = isFollowableRep (atomRep e)
1304 -- Let szsw be the sizes in words of some items pushed onto the stack,
1305 -- which has initial depth d'. Return the values which the stack environment
1306 -- should map these items to.
1307 mkStackOffsets :: Int -> [Int] -> [Int]
1308 mkStackOffsets original_depth szsw
1309 = map (subtract 1) (tail (scanl (+) original_depth szsw))
1311 -- -----------------------------------------------------------------------------
1312 -- The bytecode generator's monad
1316 nextlabel :: Int, -- for generating local labels
1317 malloced :: [Ptr ()] } -- ptrs malloced for current BCO
1318 -- Should be free()d when it is GCd
1320 newtype BcM r = BcM (BcM_State -> IO (BcM_State, r))
1322 ioToBc :: IO a -> BcM a
1323 ioToBc io = BcM $ \st -> do
1327 runBc :: BcM r -> IO (BcM_State, r)
1328 runBc (BcM m) = m (BcM_State 0 [])
1330 thenBc :: BcM a -> (a -> BcM b) -> BcM b
1331 thenBc (BcM expr) cont = BcM $ \st0 -> do
1332 (st1, q) <- expr st0
1337 thenBc_ :: BcM a -> BcM b -> BcM b
1338 thenBc_ (BcM expr) (BcM cont) = BcM $ \st0 -> do
1339 (st1, q) <- expr st0
1340 (st2, r) <- cont st1
1343 returnBc :: a -> BcM a
1344 returnBc result = BcM $ \st -> (return (st, result))
1346 instance Monad BcM where
1351 emitBc :: ([Ptr ()] -> ProtoBCO Name) -> BcM (ProtoBCO Name)
1353 = BcM $ \st -> return (st{malloced=[]}, bco (malloced st))
1355 recordMallocBc :: Ptr a -> BcM ()
1357 = BcM $ \st -> return (st{malloced = castPtr a : malloced st}, ())
1359 getLabelBc :: BcM Int
1361 = BcM $ \st -> return (st{nextlabel = 1 + nextlabel st}, nextlabel st)
1363 getLabelsBc :: Int -> BcM [Int]
1365 = BcM $ \st -> let ctr = nextlabel st
1366 in return (st{nextlabel = ctr+n}, [ctr .. ctr+n-1])