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
12 #include "HsVersions.h"
15 import Name ( Name, getName )
16 import Id ( Id, idType, isDataConId_maybe, isPrimOpId_maybe, isFCallId,
17 idPrimRep, mkSysLocal, idName, isFCallId_maybe )
18 import ForeignCall ( ForeignCall(..), CCallTarget(..), CCallSpec(..) )
19 import OrdList ( OrdList, consOL, snocOL, appOL, unitOL,
20 nilOL, toOL, concatOL, fromOL )
21 import FiniteMap ( FiniteMap, addListToFM, listToFM,
22 addToFM, lookupFM, fmToList )
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, splitTyConApp_maybe, isTyVarTy )
30 import DataCon ( dataConTag, fIRST_TAG, dataConTyCon,
31 dataConWrapId, isUnboxedTupleCon )
32 import TyCon ( TyCon(..), tyConFamilySize, isDataTyCon, tyConDataCons,
33 isFunTyCon, isUnboxedTupleTyCon )
34 import Class ( Class, classTyCon )
35 import Type ( Type, repType, splitRepFunTys )
36 import Util ( zipEqual, zipWith4Equal, naturalMergeSortLe, nOfThem )
37 import Var ( isTyVar )
38 import VarSet ( VarSet, varSetElems )
39 import PrimRep ( isFollowableRep )
40 import CmdLineOpts ( DynFlags, DynFlag(..) )
41 import ErrUtils ( showPass, dumpIfSet_dyn )
42 import Unique ( mkPseudoUnique3 )
43 import FastString ( FastString(..) )
44 import Panic ( GhcException(..) )
45 import PprType ( pprType )
46 import ByteCodeInstr ( BCInstr(..), ProtoBCO(..), nameOfProtoBCO, bciStackUse )
47 import ByteCodeItbls ( ItblEnv, mkITbls )
48 import ByteCodeLink ( UnlinkedBCO, UnlinkedBCOExpr, assembleBCO,
49 ClosureEnv, HValue, filterNameMap,
50 iNTERP_STACK_CHECK_THRESH )
51 import ByteCodeFFI ( taggedSizeW, untaggedSizeW, mkMarshalCode )
52 import Linker ( lookupSymbol )
54 import List ( intersperse, sortBy, zip4 )
55 import Foreign ( Ptr(..), mallocBytes )
56 import Addr ( Addr(..), writeCharOffAddr )
57 import CTypes ( CInt )
58 import Exception ( throwDyn )
60 import PrelBase ( Int(..) )
61 import PrelGHC ( ByteArray# )
62 import PrelIOBase ( IO(..) )
66 %************************************************************************
68 \subsection{Functions visible from outside this module.}
70 %************************************************************************
74 byteCodeGen :: DynFlags
77 -> IO ([UnlinkedBCO], ItblEnv)
78 byteCodeGen dflags binds local_tycons local_classes
79 = do showPass dflags "ByteCodeGen"
80 let tycs = local_tycons ++ map classTyCon local_classes
81 itblenv <- mkITbls tycs
83 let flatBinds = concatMap getBind binds
84 getBind (NonRec bndr rhs) = [(bndr, freeVars rhs)]
85 getBind (Rec binds) = [(bndr, freeVars rhs) | (bndr,rhs) <- binds]
87 (BcM_State proto_bcos final_ctr, ())
88 <- runBc (BcM_State [] 0)
89 (mapBc (schemeR True) flatBinds `thenBc_` returnBc ())
91 dumpIfSet_dyn dflags Opt_D_dump_BCOs
92 "Proto-bcos" (vcat (intersperse (char ' ') (map ppr proto_bcos)))
94 bcos <- mapM assembleBCO proto_bcos
96 return (bcos, itblenv)
99 -- Returns: (the root BCO for this expression,
100 -- a list of auxilary BCOs resulting from compiling closures)
101 coreExprToBCOs :: DynFlags
103 -> IO UnlinkedBCOExpr
104 coreExprToBCOs dflags expr
105 = do showPass dflags "ByteCodeGen"
107 -- create a totally bogus name for the top-level BCO; this
108 -- should be harmless, since it's never used for anything
109 let invented_id = mkSysLocal SLIT("Expr-Top-Level") (mkPseudoUnique3 0)
110 (panic "invented_id's type")
111 let invented_name = idName invented_id
113 (BcM_State all_proto_bcos final_ctr, ())
114 <- runBc (BcM_State [] 0)
115 (schemeR True (invented_id, freeVars expr))
117 dumpIfSet_dyn dflags Opt_D_dump_BCOs
118 "Proto-bcos" (vcat (intersperse (char ' ') (map ppr all_proto_bcos)))
121 = case filter ((== invented_name).nameOfProtoBCO) all_proto_bcos of
122 [root_bco] -> root_bco
124 = filter ((/= invented_name).nameOfProtoBCO) all_proto_bcos
126 auxiliary_bcos <- mapM assembleBCO auxiliary_proto_bcos
127 root_bco <- assembleBCO root_proto_bco
129 return (root_bco, auxiliary_bcos)
132 %************************************************************************
134 \subsection{Compilation schema for the bytecode generator.}
136 %************************************************************************
140 type BCInstrList = OrdList BCInstr
142 type Sequel = Int -- back off to this depth before ENTER
144 -- Maps Ids to the offset from the stack _base_ so we don't have
145 -- to mess with it after each push/pop.
146 type BCEnv = FiniteMap Id Int -- To find vars on the stack
148 ppBCEnv :: BCEnv -> SDoc
151 $$ nest 4 (vcat (map pp_one (sortBy cmp_snd (fmToList p))))
154 pp_one (var, offset) = int offset <> colon <+> ppr var
155 cmp_snd x y = compare (snd x) (snd y)
157 -- Create a BCO and do a spot of peephole optimisation on the insns
159 mkProtoBCO nm instrs_ordlist origin
160 = ProtoBCO nm maybe_with_stack_check origin
162 -- Overestimate the stack usage (in words) of this BCO,
163 -- and if >= iNTERP_STACK_CHECK_THRESH, add an explicit
164 -- stack check. (The interpreter always does a stack check
165 -- for iNTERP_STACK_CHECK_THRESH words at the start of each
166 -- BCO anyway, so we only need to add an explicit on in the
167 -- (hopefully rare) cases when the (overestimated) stack use
168 -- exceeds iNTERP_STACK_CHECK_THRESH.
169 maybe_with_stack_check
170 | stack_overest >= 65535
171 = pprPanic "mkProtoBCO: stack use won't fit in 16 bits"
173 | stack_overest >= iNTERP_STACK_CHECK_THRESH
174 = (STKCHECK stack_overest) : peep_d
176 = peep_d -- the supposedly common case
178 stack_overest = sum (map bciStackUse peep_d)
179 + 10 {- just to be really really sure -}
182 -- Merge local pushes
183 peep_d = peep (fromOL instrs_ordlist)
185 peep (PUSH_L off1 : PUSH_L off2 : PUSH_L off3 : rest)
186 = PUSH_LLL off1 (off2-1) (off3-2) : peep rest
187 peep (PUSH_L off1 : PUSH_L off2 : rest)
188 = PUSH_LL off1 (off2-1) : peep rest
195 -- Compile code for the right hand side of a let binding.
196 -- Park the resulting BCO in the monad. Also requires the
197 -- variable to which this value was bound, so as to give the
198 -- resulting BCO a name. Bool indicates top-levelness.
200 schemeR :: Bool -> (Id, AnnExpr Id VarSet) -> BcM ()
201 schemeR is_top (nm, rhs)
205 $$ (ppr.filter (not.isTyVar).varSetElems.fst) rhs
206 $$ pprCoreExpr (deAnnotate rhs)
212 = schemeR_wrk is_top rhs nm (collect [] rhs)
215 collect xs (_, AnnNote note e)
217 collect xs (_, AnnLam x e)
218 = collect (if isTyVar x then xs else (x:xs)) e
219 collect xs not_lambda
220 = (reverse xs, not_lambda)
222 schemeR_wrk is_top original_body nm (args, body)
223 | Just dcon <- maybe_toplevel_null_con_rhs
224 = --trace ("nullary constructor! " ++ showSDocDebug (ppr nm)) (
225 emitBc (mkProtoBCO (getName nm) (toOL [PACK dcon 0, ENTER])
226 (Right original_body))
230 = let fvs = filter (not.isTyVar) (varSetElems (fst original_body))
231 all_args = reverse args ++ fvs
232 szsw_args = map taggedIdSizeW all_args
233 szw_args = sum szsw_args
234 p_init = listToFM (zip all_args (mkStackOffsets 0 szsw_args))
235 argcheck = unitOL (ARGCHECK szw_args)
237 schemeE szw_args 0 p_init body `thenBc` \ body_code ->
238 emitBc (mkProtoBCO (getName nm) (appOL argcheck body_code)
239 (Right original_body))
242 maybe_toplevel_null_con_rhs
243 | is_top && null args
246 -> case isDataConId_maybe v_wrk of
248 Just dc_wrk | nm == dataConWrapId dc_wrk
256 -- Let szsw be the sizes in words of some items pushed onto the stack,
257 -- which has initial depth d'. Return the values which the stack environment
258 -- should map these items to.
259 mkStackOffsets :: Int -> [Int] -> [Int]
260 mkStackOffsets original_depth szsw
261 = map (subtract 1) (tail (scanl (+) original_depth szsw))
263 -- Compile code to apply the given expression to the remaining args
264 -- on the stack, returning a HNF.
265 schemeE :: Int -> Sequel -> BCEnv -> AnnExpr Id VarSet -> BcM BCInstrList
267 -- Delegate tail-calls to schemeT.
268 schemeE d s p e@(fvs, AnnApp f a)
269 = schemeT d s p (fvs, AnnApp f a)
271 schemeE d s p e@(fvs, AnnVar v)
272 | isFollowableRep v_rep
273 = -- Ptr-ish thing; push it in the normal way
274 schemeT d s p (fvs, AnnVar v)
277 = -- returning an unboxed value. Heave it on the stack, SLIDE, and RETURN.
278 pushAtom True d p (AnnVar v) `thenBc` \ (push, szw) ->
279 returnBc (push -- value onto stack
280 `appOL` mkSLIDE szw (d-s) -- clear to sequel
281 `snocOL` RETURN v_rep) -- go
283 v_rep = typePrimRep (idType v)
285 schemeE d s p (fvs, AnnLit literal)
286 = pushAtom True d p (AnnLit literal) `thenBc` \ (push, szw) ->
287 let l_rep = literalPrimRep literal
288 in returnBc (push -- value onto stack
289 `appOL` mkSLIDE szw (d-s) -- clear to sequel
290 `snocOL` RETURN l_rep) -- go
292 schemeE d s p (fvs, AnnLet binds b)
293 = let (xs,rhss) = case binds of AnnNonRec x rhs -> ([x],[rhs])
294 AnnRec xs_n_rhss -> unzip xs_n_rhss
296 fvss = map (filter (not.isTyVar).varSetElems.fst) rhss
298 -- Sizes of tagged free vars, + 1 for the fn
299 sizes = map (\rhs_fvs -> 1 + sum (map taggedIdSizeW rhs_fvs)) fvss
301 -- This p', d' defn is safe because all the items being pushed
302 -- are ptrs, so all have size 1. d' and p' reflect the stack
303 -- after the closures have been allocated in the heap (but not
304 -- filled in), and pointers to them parked on the stack.
305 p' = addListToFM p (zipE xs (mkStackOffsets d (nOfThem n 1)))
308 infos = zipE4 fvss sizes xs [n, n-1 .. 1]
309 zipE = zipEqual "schemeE"
310 zipE4 = zipWith4Equal "schemeE" (\a b c d -> (a,b,c,d))
312 -- ToDo: don't build thunks for things with no free variables
313 buildThunk dd ([], size, id, off)
314 = returnBc (PUSH_G (Left (getName id))
315 `consOL` unitOL (MKAP (off+size-1) size))
316 buildThunk dd ((fv:fvs), size, id, off)
317 = pushAtom True dd p' (AnnVar fv)
318 `thenBc` \ (push_code, pushed_szw) ->
319 buildThunk (dd+pushed_szw) (fvs, size, id, off)
320 `thenBc` \ more_push_code ->
321 returnBc (push_code `appOL` more_push_code)
323 genThunkCode = mapBc (buildThunk d') infos `thenBc` \ tcodes ->
324 returnBc (concatOL tcodes)
326 allocCode = toOL (map ALLOC sizes)
328 schemeE d' s p' b `thenBc` \ bodyCode ->
329 mapBc (schemeR False) (zip xs rhss) `thenBc_`
330 genThunkCode `thenBc` \ thunkCode ->
331 returnBc (allocCode `appOL` thunkCode `appOL` bodyCode)
337 schemeE d s p (fvs_case, AnnCase (fvs_scrut, scrut) bndr
338 [(DEFAULT, [], (fvs_rhs, rhs))])
340 | let isFunType var_type
341 = case splitTyConApp_maybe var_type of
342 Just (tycon,_) | isFunTyCon tycon -> True
344 ty_bndr = repType (idType bndr)
345 in isFunType ty_bndr || isTyVarTy ty_bndr
348 -- case scrut::suspect of bndr { DEFAULT -> rhs }
350 -- let bndr = scrut in rhs
351 -- when suspect is polymorphic or arrowtyped
352 -- So the required strictness properties are not observed.
353 -- At some point, must fix this properly.
357 (AnnNonRec bndr (fvs_scrut, scrut)) (fvs_rhs, rhs)
360 in trace ("WARNING: ignoring polymorphic case in interpreted mode.\n" ++
361 " Possibly due to strict polymorphic/functional constructor args.\n" ++
362 " Your program may leak space unexpectedly.\n")
363 (schemeE d s p new_expr)
367 {- Convert case .... of (# VoidRep'd-thing, a #) -> ...
369 case .... of a -> ...
370 Use a as the name of the binder too.
372 Also case .... of (# a #) -> ...
374 case .... of a -> ...
376 schemeE d s p (fvs, AnnCase scrut bndr [(DataAlt dc, [bind1, bind2], rhs)])
377 | isUnboxedTupleCon dc && VoidRep == typePrimRep (idType bind1)
378 = --trace "automagic mashing of case alts (# VoidRep, a #)" (
379 schemeE d s p (fvs, AnnCase scrut bind2 [(DEFAULT, [bind2], rhs)])
382 schemeE d s p (fvs, AnnCase scrut bndr [(DataAlt dc, [bind1], rhs)])
383 | isUnboxedTupleCon dc
384 = --trace "automagic mashing of case alts (# a #)" (
385 schemeE d s p (fvs, AnnCase scrut bind1 [(DEFAULT, [bind1], rhs)])
388 schemeE d s p (fvs, AnnCase scrut bndr alts)
390 -- Top of stack is the return itbl, as usual.
391 -- underneath it is the pointer to the alt_code BCO.
392 -- When an alt is entered, it assumes the returned value is
393 -- on top of the itbl.
396 -- Env and depth in which to compile the alts, not including
397 -- any vars bound by the alts themselves
398 d' = d + ret_frame_sizeW + taggedIdSizeW bndr
399 p' = addToFM p bndr (d' - 1)
401 scrut_primrep = typePrimRep (idType bndr)
403 | scrut_primrep == PtrRep
405 | scrut_primrep `elem`
406 [CharRep, AddrRep, WordRep, IntRep, FloatRep, DoubleRep,
407 VoidRep, Int8Rep, Int16Rep, Int32Rep, Int64Rep,
408 Word8Rep, Word16Rep, Word32Rep, Word64Rep]
411 = pprPanic "ByteCodeGen.schemeE" (ppr scrut_primrep)
413 -- given an alt, return a discr and code for it.
414 codeAlt alt@(discr, binds_f, rhs)
416 = let (unpack_code, d_after_unpack, p_after_unpack)
417 = mkUnpackCode (filter (not.isTyVar) binds_f) d' p'
418 in schemeE d_after_unpack s p_after_unpack rhs
419 `thenBc` \ rhs_code ->
420 returnBc (my_discr alt, unpack_code `appOL` rhs_code)
422 = ASSERT(null binds_f)
423 schemeE d' s p' rhs `thenBc` \ rhs_code ->
424 returnBc (my_discr alt, rhs_code)
426 my_discr (DEFAULT, binds, rhs) = NoDiscr
427 my_discr (DataAlt dc, binds, rhs)
428 | isUnboxedTupleCon dc
429 = unboxedTupleException
431 = DiscrP (dataConTag dc - fIRST_TAG)
432 my_discr (LitAlt l, binds, rhs)
433 = case l of MachInt i -> DiscrI (fromInteger i)
434 MachFloat r -> DiscrF (fromRational r)
435 MachDouble r -> DiscrD (fromRational r)
436 MachChar i -> DiscrI i
437 _ -> pprPanic "schemeE(AnnCase).my_discr" (ppr l)
440 | not isAlgCase = Nothing
442 = case [dc | (DataAlt dc, _, _) <- alts] of
444 (dc:_) -> Just (tyConFamilySize (dataConTyCon dc))
447 mapBc codeAlt alts `thenBc` \ alt_stuff ->
448 mkMultiBranch maybe_ncons alt_stuff `thenBc` \ alt_final ->
450 alt_final_ac = ARGCHECK (taggedIdSizeW bndr) `consOL` alt_final
451 alt_bco_name = getName bndr
452 alt_bco = mkProtoBCO alt_bco_name alt_final_ac (Left alts)
454 schemeE (d + ret_frame_sizeW)
455 (d + ret_frame_sizeW) p scrut `thenBc` \ scrut_code ->
457 emitBc alt_bco `thenBc_`
458 returnBc (PUSH_AS alt_bco_name scrut_primrep `consOL` scrut_code)
461 schemeE d s p (fvs, AnnNote note body)
465 = pprPanic "ByteCodeGen.schemeE: unhandled case"
466 (pprCoreExpr (deAnnotate other))
469 -- Compile code to do a tail call. Specifically, push the fn,
470 -- slide the on-stack app back down to the sequel depth,
471 -- and enter. Four cases:
474 -- An application "PrelGHC.tagToEnum# <type> unboxed-int".
475 -- The int will be on the stack. Generate a code sequence
476 -- to convert it to the relevant constructor, SLIDE and ENTER.
478 -- 1. A nullary constructor. Push its closure on the stack
479 -- and SLIDE and RETURN.
481 -- 2. (Another nasty hack). Spot (# a::VoidRep, b #) and treat
482 -- it simply as b -- since the representations are identical
483 -- (the VoidRep takes up zero stack space). Also, spot
484 -- (# b #) and treat it as b.
486 -- 3. Application of a non-nullary constructor, by defn saturated.
487 -- Split the args into ptrs and non-ptrs, and push the nonptrs,
488 -- then the ptrs, and then do PACK and RETURN.
490 -- 4. Otherwise, it must be a function call. Push the args
491 -- right to left, SLIDE and ENTER.
493 schemeT :: Int -- Stack depth
494 -> Sequel -- Sequel depth
495 -> BCEnv -- stack env
501 -- | trace ("schemeT: env in = \n" ++ showSDocDebug (ppBCEnv p)) False
502 -- = panic "schemeT ?!?!"
504 -- | trace ("\nschemeT\n" ++ showSDoc (pprCoreExpr (deAnnotate app)) ++ "\n") False
508 | Just (arg, constr_names) <- maybe_is_tagToEnum_call
509 = pushAtom True d p arg `thenBc` \ (push, arg_words) ->
510 implement_tagToId constr_names `thenBc` \ tagToId_sequence ->
511 returnBc (push `appOL` tagToId_sequence
512 `appOL` mkSLIDE 1 (d+arg_words-s)
516 | is_con_call && null args_r_to_l
518 (PUSH_G (Left (getName con)) `consOL` mkSLIDE 1 (d-s))
523 | let isVoidRepAtom (_, AnnVar v) = VoidRep == typePrimRep (idType v)
524 isVoidRepAtom (_, AnnNote n e) = isVoidRepAtom e
525 in is_con_call && isUnboxedTupleCon con
526 && ( (length args_r_to_l == 2 && isVoidRepAtom (last (args_r_to_l)))
527 || (length args_r_to_l == 1)
529 = --trace (if length args_r_to_l == 1
530 -- then "schemeT: unboxed singleton"
531 -- else "schemeT: unboxed pair with Void first component") (
532 schemeT d s p (head args_r_to_l)
535 | Just (CCall ccall_spec) <- isFCallId_maybe fn
536 = generateCCall d s p ccall_spec fn args_r_to_l
540 = if is_con_call && isUnboxedTupleCon con
541 then unboxedTupleException
542 else do_pushery d (map snd args_final_r_to_l)
545 -- Detect and extract relevant info for the tagToEnum kludge.
546 maybe_is_tagToEnum_call
547 = let extract_constr_Names ty
548 = case splitTyConApp_maybe (repType ty) of
549 (Just (tyc, [])) | isDataTyCon tyc
550 -> map getName (tyConDataCons tyc)
551 other -> panic "maybe_is_tagToEnum_call.extract_constr_Ids"
554 (_, AnnApp (_, AnnApp (_, AnnVar v) (_, AnnType t)) arg)
555 -> case isPrimOpId_maybe v of
556 Just TagToEnumOp -> Just (snd arg, extract_constr_Names t)
560 -- Extract the args (R->L) and fn
561 (args_r_to_l_raw, fn) = chomp app
565 AnnApp f a -> case chomp f of (az, f) -> (a:az, f)
566 AnnNote n e -> chomp e
567 other -> pprPanic "schemeT"
568 (ppr (deAnnotate (panic "schemeT.chomp", other)))
570 args_r_to_l = filter (not.isTypeAtom.snd) args_r_to_l_raw
571 isTypeAtom (AnnType _) = True
574 -- decide if this is a constructor call, and rearrange
575 -- args appropriately.
576 maybe_dcon = isDataConId_maybe fn
577 is_con_call = case maybe_dcon of Nothing -> False; Just _ -> True
578 (Just con) = maybe_dcon
584 = filter (not.isPtr.snd) args_r_to_l ++ filter (isPtr.snd) args_r_to_l
585 where isPtr = isFollowableRep . atomRep
587 -- make code to push the args and then do the SLIDE-ENTER thing
588 tag_when_push = not is_con_call
589 narg_words = sum (map (get_arg_szw . atomRep . snd) args_r_to_l)
590 get_arg_szw = if tag_when_push then taggedSizeW else untaggedSizeW
592 do_pushery d (arg:args)
593 = pushAtom tag_when_push d p arg `thenBc` \ (push, arg_words) ->
594 do_pushery (d+arg_words) args `thenBc` \ more_push_code ->
595 returnBc (push `appOL` more_push_code)
597 | Just (CCall ccall_spec) <- isFCallId_maybe fn
598 = panic "schemeT.do_pushery: unexpected ccall"
601 Just con -> returnBc (
602 (PACK con narg_words `consOL`
603 mkSLIDE 1 (d - narg_words - s)) `snocOL`
607 -> pushAtom True d p (AnnVar fn)
608 `thenBc` \ (push, arg_words) ->
609 returnBc (push `appOL` mkSLIDE (narg_words+arg_words)
615 {- Deal with a CCall. Taggedly push the args onto the stack R->L,
616 deferencing ForeignObj#s and (ToDo: adjusting addrs to point to
617 payloads in Ptr/Byte arrays). Then, generate the marshalling
618 (machine) code for the ccall, and create bytecodes to call that and
619 then return in the right way.
621 generateCCall :: Int -> Sequel -- stack and sequel depths
623 -> CCallSpec -- where to call
624 -> Id -- of target, for type info
625 -> [AnnExpr Id VarSet] -- args (atoms)
628 generateCCall d0 s p ccall_spec@(CCallSpec target cconv safety) fn args_r_to_l
631 addr_usizeW = untaggedSizeW AddrRep
632 addr_tsizeW = taggedSizeW AddrRep
634 -- Get the args on the stack, with tags and suitably
635 -- dereferenced for the CCall. For each arg, return the
636 -- depth to the first word of the bits for that arg, and the
637 -- PrimRep of what was actually pushed.
639 pargs d [] = returnBc []
641 = let rep_arg = atomRep a
643 -- Don't push the FO; instead push the Addr# it
646 -> pushAtom False{-irrelevant-} d p a
647 `thenBc` \ (push_fo, _) ->
648 let foro_szW = taggedSizeW ForeignObjRep
649 d_now = d + addr_tsizeW
650 code = push_fo `appOL` toOL [
651 UPK_TAG addr_usizeW 0 0,
652 SLIDE addr_tsizeW foro_szW
654 in pargs d_now az `thenBc` \ rest ->
655 returnBc ((code, AddrRep) : rest)
656 -- Default case: push taggedly, but otherwise intact.
658 -> pushAtom True d p a `thenBc` \ (code_a, sz_a) ->
659 pargs (d+sz_a) az `thenBc` \ rest ->
660 returnBc ((code_a, rep_arg) : rest)
662 pargs d0 args_r_to_l `thenBc` \ code_n_reps ->
664 (pushs_arg, a_reps_pushed_r_to_l) = unzip code_n_reps
666 push_args = concatOL pushs_arg
667 d_after_args = d0 + sum (map taggedSizeW a_reps_pushed_r_to_l)
669 | null a_reps_pushed_r_to_l || head a_reps_pushed_r_to_l /= VoidRep
670 = panic "ByteCodeGen.generateCCall: missing or invalid World token?"
672 = reverse (tail a_reps_pushed_r_to_l)
674 -- Now: a_reps_pushed_RAW are the reps which are actually on the stack.
675 -- push_args is the code to do that.
676 -- d_after_args is the stack depth once the args are on.
678 -- Get the result rep.
679 (returns_void, r_rep)
680 = case maybe_getCCallReturnRep (idType fn) of
681 Nothing -> (True, VoidRep)
682 Just rr -> (False, rr)
684 Because the Haskell stack grows down, the a_reps refer to
685 lowest to highest addresses in that order. The args for the call
686 are on the stack. Now push an unboxed, tagged Addr# indicating
687 the C function to call. Then push a dummy placeholder for the
688 result. Finally, emit a CCALL insn with an offset pointing to the
689 Addr# just pushed, and a literal field holding the mallocville
690 address of the piece of marshalling code we generate.
691 So, just prior to the CCALL insn, the stack looks like this
692 (growing down, as usual):
697 Addr# address_of_C_fn
698 <placeholder-for-result#> (must be an unboxed type)
700 The interpreter then calls the marshall code mentioned
701 in the CCALL insn, passing it (& <placeholder-for-result#>),
702 that is, the addr of the topmost word in the stack.
703 When this returns, the placeholder will have been
704 filled in. The placeholder is slid down to the sequel
705 depth, and we RETURN.
707 This arrangement makes it simple to do f-i-dynamic since the Addr#
708 value is the first arg anyway. It also has the virtue that the
709 stack is GC-understandable at all times.
711 The marshalling code is generated specifically for this
712 call site, and so knows exactly the (Haskell) stack
713 offsets of the args, fn address and placeholder. It
714 copies the args to the C stack, calls the stacked addr,
715 and parks the result back in the placeholder. The interpreter
716 calls it as a normal C call, assuming it has a signature
717 void marshall_code ( StgWord* ptr_to_top_of_stack )
719 -- resolve static address
723 -> returnBc (False, panic "ByteCodeGen.generateCCall(dyn)")
725 -> ioToBc (lookupSymbol (_UNPK_ target)) `thenBc` \res ->
727 Just aa -> case aa of Ptr a# -> returnBc (True, A# a#)
728 Nothing -> returnBc invalid
732 invalid = pprPanic ("ByteCodeGen.generateCCall: unfindable "
733 ++ "symbol or otherwise invalid target")
736 get_target_info `thenBc` \ (is_static, static_target_addr) ->
739 -- Get the arg reps, zapping the leading Addr# in the dynamic case
740 a_reps -- | trace (showSDoc (ppr a_reps_pushed_RAW)) False = error "???"
741 | is_static = a_reps_pushed_RAW
742 | otherwise = if null a_reps_pushed_RAW
743 then panic "ByteCodeGen.generateCCall: dyn with no args"
744 else tail a_reps_pushed_RAW
747 (push_Addr, d_after_Addr)
749 = (toOL [PUSH_UBX (Right static_target_addr) addr_usizeW,
750 PUSH_TAG addr_usizeW],
751 d_after_args + addr_tsizeW)
752 | otherwise -- is already on the stack
753 = (nilOL, d_after_args)
755 -- Push the return placeholder. For a call returning nothing,
756 -- this is a VoidRep (tag).
757 r_usizeW = untaggedSizeW r_rep
758 r_tsizeW = taggedSizeW r_rep
759 d_after_r = d_after_Addr + r_tsizeW
760 r_lit = mkDummyLiteral r_rep
761 push_r = (if returns_void
763 else unitOL (PUSH_UBX (Left r_lit) r_usizeW))
765 unitOL (PUSH_TAG r_usizeW)
767 -- generate the marshalling code we're going to call
770 arg1_offW = r_tsizeW + addr_tsizeW
771 args_offW = map (arg1_offW +)
772 (init (scanl (+) 0 (map taggedSizeW a_reps)))
774 ioToBc (mkMarshalCode cconv
775 (r_offW, r_rep) addr_offW
776 (zip args_offW a_reps)) `thenBc` \ addr_of_marshaller ->
779 do_call = unitOL (CCALL addr_of_marshaller)
781 wrapup = mkSLIDE r_tsizeW (d_after_r - r_tsizeW - s)
782 `snocOL` RETURN r_rep
784 --trace (show (arg1_offW, args_offW , (map taggedSizeW a_reps) )) (
787 push_Addr `appOL` push_r `appOL` do_call `appOL` wrapup
792 -- Make a dummy literal, to be used as a placeholder for FFI return
793 -- values on the stack.
794 mkDummyLiteral :: PrimRep -> Literal
798 DoubleRep -> MachDouble 0
799 FloatRep -> MachFloat 0
800 AddrRep | taggedSizeW AddrRep == taggedSizeW WordRep -> MachWord 0
801 _ -> pprPanic "mkDummyLiteral" (ppr pr)
805 -- PrelGHC.Char# -> PrelGHC.State# PrelGHC.RealWorld
806 -- -> (# PrelGHC.State# PrelGHC.RealWorld, PrelGHC.Int# #)
809 -- and check that an unboxed pair is returned wherein the first arg is VoidRep'd.
811 -- Alternatively, for call-targets returning nothing, convert
813 -- PrelGHC.Char# -> PrelGHC.State# PrelGHC.RealWorld
814 -- -> (# PrelGHC.State# PrelGHC.RealWorld #)
818 maybe_getCCallReturnRep :: Type -> Maybe PrimRep
819 maybe_getCCallReturnRep fn_ty
820 = let (a_tys, r_ty) = splitRepFunTys fn_ty
822 = if length r_reps == 1 then Nothing else Just (r_reps !! 1)
824 = case splitTyConApp_maybe (repType r_ty) of
825 (Just (tyc, tys)) -> (tyc, map typePrimRep tys)
827 ok = ( (length r_reps == 2 && VoidRep == head r_reps)
828 || r_reps == [VoidRep] )
829 && isUnboxedTupleTyCon r_tycon
830 && case maybe_r_rep_to_go of
832 Just r_rep -> r_rep /= PtrRep
833 -- if it was, it would be impossible
834 -- to create a valid return value
835 -- placeholder on the stack
836 blargh = pprPanic "maybe_getCCallReturn: can't handle:"
839 --trace (showSDoc (ppr (a_reps, r_reps))) (
840 if ok then maybe_r_rep_to_go else blargh
843 atomRep (AnnVar v) = typePrimRep (idType v)
844 atomRep (AnnLit l) = literalPrimRep l
845 atomRep (AnnNote n b) = atomRep (snd b)
846 atomRep (AnnApp f (_, AnnType _)) = atomRep (snd f)
847 atomRep (AnnLam x e) | isTyVar x = atomRep (snd e)
848 atomRep other = pprPanic "atomRep" (ppr (deAnnotate (undefined,other)))
851 -- Compile code which expects an unboxed Int on the top of stack,
852 -- (call it i), and pushes the i'th closure in the supplied list
854 implement_tagToId :: [Name] -> BcM BCInstrList
855 implement_tagToId names
856 = ASSERT(not (null names))
857 getLabelsBc (length names) `thenBc` \ labels ->
858 getLabelBc `thenBc` \ label_fail ->
859 getLabelBc `thenBc` \ label_exit ->
860 zip4 labels (tail labels ++ [label_fail])
861 [0 ..] names `bind` \ infos ->
862 map (mkStep label_exit) infos `bind` \ steps ->
863 returnBc (concatOL steps
865 toOL [LABEL label_fail, CASEFAIL, LABEL label_exit])
867 mkStep l_exit (my_label, next_label, n, name_for_n)
868 = toOL [LABEL my_label,
869 TESTEQ_I n next_label,
870 PUSH_G (Left name_for_n),
874 -- Make code to unpack the top-of-stack constructor onto the stack,
875 -- adding tags for the unboxed bits. Takes the PrimReps of the
876 -- constructor's arguments. off_h and off_s are travelling offsets
877 -- along the constructor and the stack.
879 -- Supposing a constructor in the heap has layout
881 -- Itbl p_1 ... p_i np_1 ... np_j
883 -- then we add to the stack, shown growing down, the following:
895 -- so that in the common case (ptrs only) a single UNPACK instr can
896 -- copy all the payload of the constr onto the stack with no further ado.
898 mkUnpackCode :: [Id] -- constr args
899 -> Int -- depth before unpack
900 -> BCEnv -- env before unpack
901 -> (BCInstrList, Int, BCEnv)
902 mkUnpackCode vars d p
903 = --trace ("mkUnpackCode: " ++ showSDocDebug (ppr vars)
904 -- ++ " --> " ++ show d' ++ "\n" ++ showSDocDebug (ppBCEnv p')
906 (code_p `appOL` code_np, d', p')
910 vreps = [(var, typePrimRep (idType var)) | var <- vars]
912 -- ptrs and nonptrs, forward
913 vreps_p = filter (isFollowableRep.snd) vreps
914 vreps_np = filter (not.isFollowableRep.snd) vreps
916 -- the order in which we will augment the environment
917 vreps_env = reverse vreps_p ++ reverse vreps_np
920 vreps_env_tszsw = map (taggedSizeW.snd) vreps_env
921 p' = addListToFM p (zip (map fst vreps_env)
922 (mkStackOffsets d vreps_env_tszsw))
923 d' = d + sum vreps_env_tszsw
925 -- code to unpack the ptrs
926 ptrs_szw = sum (map (untaggedSizeW.snd) vreps_p)
927 code_p | null vreps_p = nilOL
928 | otherwise = unitOL (UNPACK ptrs_szw)
930 -- code to unpack the nonptrs
931 vreps_env_uszw = sum (map (untaggedSizeW.snd) vreps_env)
932 code_np = do_nptrs vreps_env_uszw ptrs_szw (reverse (map snd vreps_np))
933 do_nptrs off_h off_s [] = nilOL
934 do_nptrs off_h off_s (npr:nprs)
935 | npr `elem` [IntRep, WordRep, FloatRep, DoubleRep, CharRep, AddrRep]
938 = pprPanic "ByteCodeGen.mkUnpackCode" (ppr npr)
940 approved = UPK_TAG usizeW (off_h-usizeW) off_s `consOL` theRest
941 theRest = do_nptrs (off_h-usizeW) (off_s + tsizeW) nprs
942 usizeW = untaggedSizeW npr
943 tsizeW = taggedSizeW npr
946 -- Push an atom onto the stack, returning suitable code & number of
947 -- stack words used. Pushes it either tagged or untagged, since
948 -- pushAtom is used to set up the stack prior to copying into the
949 -- heap for both APs (requiring tags) and constructors (which don't).
951 -- NB this means NO GC between pushing atoms for a constructor and
952 -- copying them into the heap. It probably also means that
953 -- tail calls MUST be of the form atom{atom ... atom} since if the
954 -- expression head was allowed to be arbitrary, there could be GC
955 -- in between pushing the arg atoms and completing the head.
956 -- (not sure; perhaps the allocate/doYouWantToGC interface means this
957 -- isn't a problem; but only if arbitrary graph construction for the
958 -- head doesn't leave this BCO, since GC might happen at the start of
959 -- each BCO (we consult doYouWantToGC there).
961 -- Blargh. JRS 001206
963 -- NB (further) that the env p must map each variable to the highest-
964 -- numbered stack slot for it. For example, if the stack has depth 4
965 -- and we tagged-ly push (v :: Int#) on it, the value will be in stack[4],
966 -- the tag in stack[5], the stack will have depth 6, and p must map v to
967 -- 5 and not to 4. Stack locations are numbered from zero, so a depth
968 -- 6 stack has valid words 0 .. 5.
970 pushAtom :: Bool -> Int -> BCEnv -> AnnExpr' Id VarSet -> BcM (BCInstrList, Int)
971 pushAtom tagged d p (AnnVar v)
973 | idPrimRep v == VoidRep
974 = if tagged then returnBc (unitOL (PUSH_TAG 0), 1)
975 else panic "ByteCodeGen.pushAtom(VoidRep,untaggedly)"
978 = pprPanic "pushAtom: shouldn't get an FCallId here" (ppr v)
980 | Just primop <- isPrimOpId_maybe v
981 = returnBc (unitOL (PUSH_G (Right primop)), 1)
985 str = "\npushAtom " ++ showSDocDebug (ppr v)
986 ++ " :: " ++ showSDocDebug (pprType (idType v))
987 ++ ", depth = " ++ show d
988 ++ ", tagged = " ++ show tagged ++ ", env =\n" ++
989 showSDocDebug (ppBCEnv p)
990 ++ " --> words: " ++ show (snd result) ++ "\n" ++
991 showSDoc (nest 4 (vcat (map ppr (fromOL (fst result)))))
992 ++ "\nendPushAtom " ++ showSDocDebug (ppr v)
996 = case lookupBCEnv_maybe p v of
997 Just d_v -> (toOL (nOfThem nwords (PUSH_L (d-d_v+sz_t-2))), nwords)
998 Nothing -> ASSERT(sz_t == 1) (unitOL (PUSH_G (Left nm)), nwords)
1000 nm = case isDataConId_maybe v of
1002 Nothing -> getName v
1004 sz_t = taggedIdSizeW v
1005 sz_u = untaggedIdSizeW v
1006 nwords = if tagged then sz_t else sz_u
1010 pushAtom True d p (AnnLit lit)
1011 = pushAtom False d p (AnnLit lit) `thenBc` \ (ubx_code, ubx_size) ->
1012 returnBc (ubx_code `snocOL` PUSH_TAG ubx_size, 1 + ubx_size)
1014 pushAtom False d p (AnnLit lit)
1016 MachWord w -> code WordRep
1017 MachInt i -> code IntRep
1018 MachFloat r -> code FloatRep
1019 MachDouble r -> code DoubleRep
1020 MachChar c -> code CharRep
1021 MachStr s -> pushStr s
1024 = let size_host_words = untaggedSizeW rep
1025 in returnBc (unitOL (PUSH_UBX (Left lit) size_host_words),
1029 = let getMallocvilleAddr
1031 CharStr s i -> returnBc (A# s)
1033 FastString _ l ba ->
1034 -- sigh, a string in the heap is no good to us.
1035 -- We need a static C pointer, since the type of
1036 -- a string literal is Addr#. So, copy the string
1037 -- into C land and introduce a memory leak
1038 -- at the same time.
1040 -- CAREFUL! Chars are 32 bits in ghc 4.09+
1042 do (Ptr a#) <- mallocBytes (n+1)
1043 strncpy (Ptr a#) ba (fromIntegral n)
1044 writeCharOffAddr (A# a#) n '\0'
1047 other -> panic "ByteCodeGen.pushAtom.pushStr"
1049 getMallocvilleAddr `thenBc` \ addr ->
1050 -- Get the addr on the stack, untaggedly
1051 returnBc (unitOL (PUSH_UBX (Right addr) 1), 1)
1057 pushAtom tagged d p (AnnApp f (_, AnnType _))
1058 = pushAtom tagged d p (snd f)
1060 pushAtom tagged d p (AnnNote note e)
1061 = pushAtom tagged d p (snd e)
1063 pushAtom tagged d p (AnnLam x e)
1065 = pushAtom tagged d p (snd e)
1067 pushAtom tagged d p other
1068 = pprPanic "ByteCodeGen.pushAtom"
1069 (pprCoreExpr (deAnnotate (undefined, other)))
1071 foreign import "strncpy" strncpy :: Ptr a -> ByteArray# -> CInt -> IO ()
1074 -- Given a bunch of alts code and their discrs, do the donkey work
1075 -- of making a multiway branch using a switch tree.
1076 -- What a load of hassle!
1077 mkMultiBranch :: Maybe Int -- # datacons in tycon, if alg alt
1078 -- a hint; generates better code
1079 -- Nothing is always safe
1080 -> [(Discr, BCInstrList)]
1082 mkMultiBranch maybe_ncons raw_ways
1083 = let d_way = filter (isNoDiscr.fst) raw_ways
1084 notd_ways = naturalMergeSortLe
1085 (\w1 w2 -> leAlt (fst w1) (fst w2))
1086 (filter (not.isNoDiscr.fst) raw_ways)
1088 mkTree :: [(Discr, BCInstrList)] -> Discr -> Discr -> BcM BCInstrList
1089 mkTree [] range_lo range_hi = returnBc the_default
1091 mkTree [val] range_lo range_hi
1092 | range_lo `eqAlt` range_hi
1093 = returnBc (snd val)
1095 = getLabelBc `thenBc` \ label_neq ->
1096 returnBc (mkTestEQ (fst val) label_neq
1098 `appOL` unitOL (LABEL label_neq)
1099 `appOL` the_default))
1101 mkTree vals range_lo range_hi
1102 = let n = length vals `div` 2
1103 vals_lo = take n vals
1104 vals_hi = drop n vals
1105 v_mid = fst (head vals_hi)
1107 getLabelBc `thenBc` \ label_geq ->
1108 mkTree vals_lo range_lo (dec v_mid) `thenBc` \ code_lo ->
1109 mkTree vals_hi v_mid range_hi `thenBc` \ code_hi ->
1110 returnBc (mkTestLT v_mid label_geq
1112 `appOL` unitOL (LABEL label_geq)
1116 = case d_way of [] -> unitOL CASEFAIL
1119 -- None of these will be needed if there are no non-default alts
1120 (mkTestLT, mkTestEQ, init_lo, init_hi)
1122 = panic "mkMultiBranch: awesome foursome"
1124 = case fst (head notd_ways) of {
1125 DiscrI _ -> ( \(DiscrI i) fail_label -> TESTLT_I i fail_label,
1126 \(DiscrI i) fail_label -> TESTEQ_I i fail_label,
1129 DiscrF _ -> ( \(DiscrF f) fail_label -> TESTLT_F f fail_label,
1130 \(DiscrF f) fail_label -> TESTEQ_F f fail_label,
1133 DiscrD _ -> ( \(DiscrD d) fail_label -> TESTLT_D d fail_label,
1134 \(DiscrD d) fail_label -> TESTEQ_D d fail_label,
1137 DiscrP _ -> ( \(DiscrP i) fail_label -> TESTLT_P i fail_label,
1138 \(DiscrP i) fail_label -> TESTEQ_P i fail_label,
1140 DiscrP algMaxBound )
1143 (algMinBound, algMaxBound)
1144 = case maybe_ncons of
1145 Just n -> (0, n - 1)
1146 Nothing -> (minBound, maxBound)
1148 (DiscrI i1) `eqAlt` (DiscrI i2) = i1 == i2
1149 (DiscrF f1) `eqAlt` (DiscrF f2) = f1 == f2
1150 (DiscrD d1) `eqAlt` (DiscrD d2) = d1 == d2
1151 (DiscrP i1) `eqAlt` (DiscrP i2) = i1 == i2
1152 NoDiscr `eqAlt` NoDiscr = True
1155 (DiscrI i1) `leAlt` (DiscrI i2) = i1 <= i2
1156 (DiscrF f1) `leAlt` (DiscrF f2) = f1 <= f2
1157 (DiscrD d1) `leAlt` (DiscrD d2) = d1 <= d2
1158 (DiscrP i1) `leAlt` (DiscrP i2) = i1 <= i2
1159 NoDiscr `leAlt` NoDiscr = True
1162 isNoDiscr NoDiscr = True
1165 dec (DiscrI i) = DiscrI (i-1)
1166 dec (DiscrP i) = DiscrP (i-1)
1167 dec other = other -- not really right, but if you
1168 -- do cases on floating values, you'll get what you deserve
1170 -- same snotty comment applies to the following
1172 minD, maxD :: Double
1178 mkTree notd_ways init_lo init_hi
1182 %************************************************************************
1184 \subsection{Supporting junk for the compilation schemes}
1186 %************************************************************************
1190 -- Describes case alts
1198 instance Outputable Discr where
1199 ppr (DiscrI i) = int i
1200 ppr (DiscrF f) = text (show f)
1201 ppr (DiscrD d) = text (show d)
1202 ppr (DiscrP i) = int i
1203 ppr NoDiscr = text "DEF"
1206 -- Find things in the BCEnv (the what's-on-the-stack-env)
1207 -- See comment preceding pushAtom for precise meaning of env contents
1208 --lookupBCEnv :: BCEnv -> Id -> Int
1209 --lookupBCEnv env nm
1210 -- = case lookupFM env nm of
1211 -- Nothing -> pprPanic "lookupBCEnv"
1212 -- (ppr nm $$ char ' ' $$ vcat (map ppr (fmToList env)))
1215 lookupBCEnv_maybe :: BCEnv -> Id -> Maybe Int
1216 lookupBCEnv_maybe = lookupFM
1219 taggedIdSizeW, untaggedIdSizeW :: Id -> Int
1220 taggedIdSizeW = taggedSizeW . typePrimRep . idType
1221 untaggedIdSizeW = untaggedSizeW . typePrimRep . idType
1223 unboxedTupleException :: a
1224 unboxedTupleException
1227 ("Bytecode generator can't handle unboxed tuples. Possibly due\n" ++
1228 "\tto foreign import/export decls in source. Workaround:\n" ++
1229 "\tcompile this module to a .o file, then restart session."))
1232 mkSLIDE n d = if d == 0 then nilOL else unitOL (SLIDE n d)
1237 %************************************************************************
1239 \subsection{The bytecode generator's monad}
1241 %************************************************************************
1245 = BcM_State { bcos :: [ProtoBCO Name], -- accumulates completed BCOs
1246 nextlabel :: Int } -- for generating local labels
1248 type BcM r = BcM_State -> IO (BcM_State, r)
1250 ioToBc :: IO a -> BcM a
1251 ioToBc io st = do x <- io
1254 runBc :: BcM_State -> BcM r -> IO (BcM_State, r)
1255 runBc st0 m = do (st1, res) <- m st0
1258 thenBc :: BcM a -> (a -> BcM b) -> BcM b
1259 thenBc expr cont st0
1260 = do (st1, q) <- expr st0
1261 (st2, r) <- cont q st1
1264 thenBc_ :: BcM a -> BcM b -> BcM b
1265 thenBc_ expr cont st0
1266 = do (st1, q) <- expr st0
1267 (st2, r) <- cont st1
1270 returnBc :: a -> BcM a
1271 returnBc result st = return (st, result)
1274 mapBc :: (a -> BcM b) -> [a] -> BcM [b]
1275 mapBc f [] = returnBc []
1277 = f x `thenBc` \ r ->
1278 mapBc f xs `thenBc` \ rs ->
1281 emitBc :: ProtoBCO Name -> BcM ()
1283 = return (st{bcos = bco : bcos st}, ())
1285 getLabelBc :: BcM Int
1287 = return (st{nextlabel = 1 + nextlabel st}, nextlabel st)
1289 getLabelsBc :: Int -> BcM [Int]
1291 = let ctr = nextlabel st
1292 in return (st{nextlabel = ctr+n}, [ctr .. ctr+n-1])