[project @ 2002-04-29 14:03:38 by simonmar]
[ghc-hetmet.git] / ghc / compiler / ghci / ByteCodeGen.lhs
index ca1326b..56f64fc 100644 (file)
@@ -14,56 +14,57 @@ module ByteCodeGen ( UnlinkedBCO, UnlinkedBCOExpr, ItblEnv, ClosureEnv, HValue,
 import Outputable
 import Name            ( Name, getName )
 import Id              ( Id, idType, isDataConId_maybe, isPrimOpId_maybe, isFCallId,
-                         idPrimRep, mkSysLocal, idName, isFCallId_maybe )
+                         idPrimRep, mkSysLocal, idName, isFCallId_maybe, isPrimOpId )
 import ForeignCall     ( ForeignCall(..), CCallTarget(..), CCallSpec(..) )
 import OrdList         ( OrdList, consOL, snocOL, appOL, unitOL, 
                          nilOL, toOL, concatOL, fromOL )
-import FiniteMap       ( FiniteMap, addListToFM, listToFM,
+import FiniteMap       ( FiniteMap, addListToFM, listToFM, elemFM,
                          addToFM, lookupFM, fmToList )
 import CoreSyn
 import PprCore         ( pprCoreExpr )
 import Literal         ( Literal(..), literalPrimRep )
 import PrimRep         ( PrimRep(..) )
 import PrimOp          ( PrimOp(..) )
-import CStrings                ( CLabelString )
 import CoreFVs         ( freeVars )
-import Type            ( typePrimRep, splitTyConApp_maybe, isTyVarTy, splitForAllTys )
+import Type            ( typePrimRep, splitTyConApp_maybe, isTyVarTy )
 import DataCon         ( dataConTag, fIRST_TAG, dataConTyCon, 
                           dataConWrapId, isUnboxedTupleCon )
 import TyCon           ( TyCon(..), tyConFamilySize, isDataTyCon, tyConDataCons,
                          isFunTyCon, isUnboxedTupleTyCon )
 import Class           ( Class, classTyCon )
-import Type            ( Type, repType, splitRepFunTys )
-import Util            ( zipEqual, zipWith4Equal, naturalMergeSortLe, nOfThem )
+import Type            ( Type, repType, splitFunTys, dropForAlls )
+import Util            ( zipEqual, zipWith4Equal, naturalMergeSortLe, nOfThem,
+                         isSingleton, lengthIs, notNull )
+import DataCon         ( dataConRepArity )
 import Var             ( isTyVar )
 import VarSet          ( VarSet, varSetElems )
-import PrimRep         ( getPrimRepSize, isFollowableRep )
+import PrimRep         ( isFollowableRep )
 import CmdLineOpts     ( DynFlags, DynFlag(..) )
 import ErrUtils                ( showPass, dumpIfSet_dyn )
 import Unique          ( mkPseudoUnique3 )
 import FastString      ( FastString(..) )
 import Panic           ( GhcException(..) )
-import SMRep           ( fixedHdrSize )
 import PprType         ( pprType )
+import SMRep           ( arrWordsHdrSize, arrPtrsHdrSize )
+import Constants       ( wORD_SIZE )
 import ByteCodeInstr   ( BCInstr(..), ProtoBCO(..), nameOfProtoBCO, bciStackUse )
 import ByteCodeItbls   ( ItblEnv, mkITbls )
 import ByteCodeLink    ( UnlinkedBCO, UnlinkedBCOExpr, assembleBCO,
-                         ClosureEnv, HValue, filterNameMap,
+                         ClosureEnv, HValue, filterNameMap, linkFail,
                          iNTERP_STACK_CHECK_THRESH )
-import ByteCodeFFI     ( taggedSizeW, untaggedSizeW, mkMarshalCode )
+import ByteCodeFFI     ( taggedSizeW, untaggedSizeW, mkMarshalCode, moan64 )
 import Linker          ( lookupSymbol )
 
 import List            ( intersperse, sortBy, zip4 )
-import Foreign         ( Ptr(..), mallocBytes )
-import Addr            ( Addr(..), nullAddr, addrToInt, writeCharOffAddr )
+import Foreign         ( Ptr(..), castPtr, mallocBytes, pokeByteOff, Word8 )
 import CTypes          ( CInt )
 import Exception       ( throwDyn )
 
-import PrelBase                ( Int(..) )
-import PrelGHC         ( ByteArray# )
-import IOExts          ( unsafePerformIO )
-import PrelIOBase      ( IO(..) )
+import GlaExts         ( Int(..), ByteArray# )
 
+import Monad           ( when )
+import Maybe           ( isJust )
+import Char            ( ord )
 \end{code}
 
 %************************************************************************
@@ -87,9 +88,14 @@ byteCodeGen dflags binds local_tycons local_classes
             getBind (NonRec bndr rhs) = [(bndr, freeVars rhs)]
             getBind (Rec binds)       = [(bndr, freeVars rhs) | (bndr,rhs) <- binds]
 
-        (BcM_State proto_bcos final_ctr, ())
-           <- runBc (BcM_State [] 0) 
-                    (mapBc (schemeR True) flatBinds `thenBc_` returnBc ())
+        (BcM_State proto_bcos final_ctr mallocd, ())
+           <- runBc (BcM_State [] 0 []) 
+                    (mapBc (schemeR True []) flatBinds `thenBc_` returnBc ())
+                       --               ^^
+                       -- better be no free vars in these top-level bindings
+
+        when (notNull mallocd)
+             (panic "ByteCodeGen.byteCodeGen: missing final emitBc?")
 
         dumpIfSet_dyn dflags Opt_D_dump_BCOs
            "Proto-bcos" (vcat (intersperse (char ' ') (map ppr proto_bcos)))
@@ -109,13 +115,20 @@ coreExprToBCOs dflags expr
 
       -- create a totally bogus name for the top-level BCO; this
       -- should be harmless, since it's never used for anything
-      let invented_id   = mkSysLocal SLIT("Expr-Top-Level") (mkPseudoUnique3 0) 
-                                    (panic "invented_id's type")
+      let invented_id   = mkSysLocal FSLIT("Expr-Top-Level") 
+                               (mkPseudoUnique3 0) 
+                               (panic "invented_id's type")
       let invented_name = idName invented_id
 
-      (BcM_State all_proto_bcos final_ctr, ()) 
-         <- runBc (BcM_State [] 0) 
-                  (schemeR True (invented_id, freeVars expr))
+         annexpr = freeVars expr
+         fvs = filter (not.isTyVar) (varSetElems (fst annexpr))
+
+      (BcM_State all_proto_bcos final_ctr mallocd, ()) 
+         <- runBc (BcM_State [] 0 []) 
+                  (schemeR True fvs (invented_id, annexpr))
+
+      when (notNull mallocd)
+           (panic "ByteCodeGen.coreExprToBCOs: missing final emitBc?")
 
       dumpIfSet_dyn dflags Opt_D_dump_BCOs
          "Proto-bcos" (vcat (intersperse (char ' ') (map ppr all_proto_bcos)))
@@ -159,8 +172,8 @@ ppBCEnv p
 
 -- Create a BCO and do a spot of peephole optimisation on the insns
 -- at the same time.
-mkProtoBCO nm instrs_ordlist origin
-   = ProtoBCO nm maybe_with_stack_check origin
+mkProtoBCO nm instrs_ordlist origin mallocd_blocks
+   = ProtoBCO nm maybe_with_stack_check origin mallocd_blocks
      where
         -- Overestimate the stack usage (in words) of this BCO,
         -- and if >= iNTERP_STACK_CHECK_THRESH, add an explicit
@@ -200,8 +213,8 @@ mkProtoBCO nm instrs_ordlist origin
 -- variable to which this value was bound, so as to give the
 -- resulting BCO a name.  Bool indicates top-levelness.
 
-schemeR :: Bool -> (Id, AnnExpr Id VarSet) -> BcM ()
-schemeR is_top (nm, rhs) 
+schemeR :: Bool -> [Id] -> (Id, AnnExpr Id VarSet) -> BcM ()
+schemeR is_top fvs (nm, rhs) 
 {-
    | trace (showSDoc (
               (char ' '
@@ -212,7 +225,7 @@ schemeR is_top (nm, rhs)
    = undefined
 -}
    | otherwise
-   = schemeR_wrk is_top rhs nm (collect [] rhs)
+   = schemeR_wrk is_top fvs rhs nm (collect [] rhs)
 
 
 collect xs (_, AnnNote note e)
@@ -222,7 +235,7 @@ collect xs (_, AnnLam x e)
 collect xs not_lambda
    = (reverse xs, not_lambda)
 
-schemeR_wrk is_top original_body nm (args, body)
+schemeR_wrk is_top fvs original_body nm (args, body)
    | Just dcon <- maybe_toplevel_null_con_rhs
    = --trace ("nullary constructor! " ++ showSDocDebug (ppr nm)) (
      emitBc (mkProtoBCO (getName nm) (toOL [PACK dcon 0, ENTER])
@@ -230,8 +243,7 @@ schemeR_wrk is_top original_body nm (args, body)
      --)
 
    | otherwise
-   = let fvs       = filter (not.isTyVar) (varSetElems (fst original_body))
-         all_args  = reverse args ++ fvs
+   = let all_args  = reverse args ++ fvs
          szsw_args = map taggedIdSizeW all_args
          szw_args  = sum szsw_args
          p_init    = listToFM (zip all_args (mkStackOffsets 0 szsw_args))
@@ -244,7 +256,7 @@ schemeR_wrk is_top original_body nm (args, body)
      where
         maybe_toplevel_null_con_rhs
            | is_top && null args
-           = case snd body of
+           = case nukeTyArgs (snd body) of
                 AnnVar v_wrk 
                    -> case isDataConId_maybe v_wrk of
                          Nothing -> Nothing
@@ -256,6 +268,10 @@ schemeR_wrk is_top original_body nm (args, body)
            | otherwise
            = Nothing
 
+        nukeTyArgs (AnnApp f (_, AnnType _)) = nukeTyArgs (snd f)
+        nukeTyArgs other                     = other
+
+
 -- Let szsw be the sizes in words of some items pushed onto the stack,
 -- which has initial depth d'.  Return the values which the stack environment
 -- should map these items to.
@@ -278,25 +294,122 @@ schemeE d s p e@(fvs, AnnVar v)
 
    | otherwise
    = -- returning an unboxed value.  Heave it on the stack, SLIDE, and RETURN.
-     let (push, szw) = pushAtom True d p (AnnVar v)
-     in  returnBc (push                        -- value onto stack
-                   `appOL`  mkSLIDE szw (d-s)  -- clear to sequel
-                   `snocOL` RETURN v_rep)      -- go
+     pushAtom True d p (AnnVar v)      `thenBc` \ (push, szw) ->
+     returnBc (push                    -- value onto stack
+               `appOL`  mkSLIDE szw (d-s)      -- clear to sequel
+               `snocOL` RETURN v_rep)  -- go
    where
       v_rep = typePrimRep (idType v)
 
 schemeE d s p (fvs, AnnLit literal)
-   = let (push, szw) = pushAtom True d p (AnnLit literal)
-         l_rep = literalPrimRep literal
+   = pushAtom True d p (AnnLit literal)        `thenBc` \ (push, szw) ->
+     let l_rep = literalPrimRep literal
      in  returnBc (push                        -- value onto stack
                    `appOL`  mkSLIDE szw (d-s)  -- clear to sequel
                    `snocOL` RETURN l_rep)      -- go
 
+
+{-
+   Deal specially with the cases
+      let x = fn atom1 .. atomn  in B
+   and
+      let x = Con atom1 .. atomn  in B
+              (Con must be saturated)
+
+   In these cases, generate code to allocate in-line.
+
+   This is optimisation of the general case for let, which follows
+   this one; this case can safely be omitted.  The reduction in
+   interpreter execution time seems to be around 5% for some programs,
+   with a similar drop in allocations.
+
+   This optimisation should be done more cleanly.  As-is, it is
+   inapplicable to RHSs in letrecs, and needlessly duplicates code in
+   schemeR and schemeT.  Some refactoring of the machinery would cure
+   both ills.  
+-}
+schemeE d s p ee@(fvs, AnnLet (AnnNonRec x rhs) b)
+   | ok_to_go
+   = let d_init = if is_con then d else d'
+     in
+     mkPushes d_init args_r_to_l_reordered     `thenBc` \ (d_final, push_code) ->
+     schemeE d' s p' b                         `thenBc` \ body_code ->
+     let size  = d_final - d_init
+         alloc = if is_con then nilOL else unitOL (ALLOC size)
+         pack  = unitOL (if is_con then PACK the_dcon size else MKAP size size)
+     in
+         returnBc (alloc `appOL` push_code `appOL` pack
+                   `appOL` body_code)
+     where
+        -- Decide whether we can do this or not
+        (ok_to_go, is_con, the_dcon, the_fn)
+            = case maybe_fn of
+                 Nothing        -> (False, bomb 1, bomb 2, bomb 3)
+                 Just (Left fn) -> (True,  False,  bomb 5, fn)
+                 Just (Right dcon)
+                    |  dataConRepArity dcon <= length args_r_to_l
+                    -> (True, True, dcon, bomb 6)
+                    |  otherwise
+                    -> (False, bomb 7, bomb 8, bomb 9)
+        bomb n = panic ("schemeE.is_con(hacky hack hack) " ++ show n)
+
+        -- Extract the args (R -> L) and fn
+        args_r_to_l_reordered
+           | not is_con
+           = args_r_to_l
+           | otherwise
+           = filter (not.isPtr.snd) args_r_to_l ++ filter (isPtr.snd) args_r_to_l
+             where isPtr = isFollowableRep . atomRep
+
+        args_r_to_l = filter (not.isTypeAtom.snd) args_r_to_l_raw
+        isTypeAtom (AnnType _) = True
+        isTypeAtom _           = False
+
+        (args_r_to_l_raw, maybe_fn) = chomp rhs
+        chomp expr
+           = case snd expr of
+                AnnVar v 
+                   |  isFCallId v || isPrimOpId v  
+                   -> ([], Nothing)
+                   |  otherwise
+                   -> case isDataConId_maybe v of
+                         Just dcon -> ([], Just (Right dcon))
+                         Nothing   -> ([], Just (Left v))
+                AnnApp f a  -> case chomp f of (az, f) -> (a:az, f)
+                AnnNote n e -> chomp e
+                other       -> ([], Nothing)
+
+        -- This is the env in which to translate the body
+        p' = addToFM p x d
+        d' = d + 1
+
+        -- Shove the args on the stack, including the fn in the non-dcon case
+        tag_when_push = not is_con
+
+        mkPushes :: Int{-curr depth-} -> [AnnExpr Id VarSet] 
+                 -> BcM (Int{-final depth-}, BCInstrList)
+        mkPushes dd []
+           | is_con
+           = returnBc (dd, nilOL)
+           | otherwise
+           = pushAtom False dd p' (AnnVar the_fn) `thenBc` \ (fn_push_code, fn_szw) ->
+             returnBc (dd+fn_szw, fn_push_code)
+        mkPushes dd (atom:atoms) 
+           = pushAtom tag_when_push dd p' (snd atom)   
+                                               `thenBc` \ (push1_code, push1_szw) ->
+             mkPushes (dd+push1_szw) atoms     `thenBc` \ (dd_final, push_rest) ->
+             returnBc (dd_final, push1_code `appOL` push_rest)
+
+
+-- General case for let.  Generates correct, if inefficient, code in
+-- all situations.
 schemeE d s p (fvs, AnnLet binds b)
    = let (xs,rhss) = case binds of AnnNonRec x rhs  -> ([x],[rhs])
                                    AnnRec xs_n_rhss -> unzip xs_n_rhss
          n     = length xs
-         fvss  = map (filter (not.isTyVar).varSetElems.fst) rhss
+
+        is_local id = not (isTyVar id) && elemFM id p'
+         fvss  = map (filter is_local . varSetElems . fst) rhss
 
          -- Sizes of tagged free vars, + 1 for the fn
          sizes = map (\rhs_fvs -> 1 + sum (map taggedIdSizeW rhs_fvs)) fvss
@@ -314,19 +427,27 @@ schemeE d s p (fvs, AnnLet binds b)
 
          -- ToDo: don't build thunks for things with no free variables
          buildThunk dd ([], size, id, off)
-            = PUSH_G (Left (getName id))
-              `consOL` unitOL (MKAP (off+size-1) size)
+            = returnBc (PUSH_G (Left (getName id))
+                        `consOL` unitOL (MKAP (off+size-1) size))
          buildThunk dd ((fv:fvs), size, id, off)
-            = case pushAtom True dd p' (AnnVar fv) of
-                 (push_code, pushed_szw)
-                    -> push_code `appOL`
-                       buildThunk (dd+pushed_szw) (fvs, size, id, off)
+            = pushAtom True dd p' (AnnVar fv) 
+                                       `thenBc` \ (push_code, pushed_szw) ->
+              buildThunk (dd+pushed_szw) (fvs, size, id, off)
+                                       `thenBc` \ more_push_code ->
+              returnBc (push_code `appOL` more_push_code)
+
+         genThunkCode = mapBc (buildThunk d') infos    `thenBc` \ tcodes ->
+                        returnBc (concatOL tcodes)
 
-         thunkCode = concatOL (map (buildThunk d') infos)
          allocCode = toOL (map ALLOC sizes)
+
+        schemeRs [] _ _ = returnBc ()
+        schemeRs (fvs:fvss) (x:xs) (rhs:rhss) = 
+               schemeR False fvs (x,rhs) `thenBc_` schemeRs fvss xs rhss
      in
      schemeE d' s p' b                                 `thenBc`  \ bodyCode ->
-     mapBc (schemeR False) (zip xs rhss)               `thenBc_`
+     schemeRs fvss xs rhss                             `thenBc_`
+     genThunkCode                                      `thenBc` \ thunkCode ->
      returnBc (allocCode `appOL` thunkCode `appOL` bodyCode)
 
 
@@ -359,7 +480,6 @@ schemeE d s p (fvs_case, AnnCase (fvs_scrut, scrut) bndr
      in  trace ("WARNING: ignoring polymorphic case in interpreted mode.\n" ++
                 "   Possibly due to strict polymorphic/functional constructor args.\n" ++
                 "   Your program may leak space unexpectedly.\n")
-                -- ++ showSDoc (char ' ' $$ pprCoreExpr (deAnnotate new_expr) $$ char ' '))
          (schemeE d s p new_expr)
 
 
@@ -471,7 +591,7 @@ schemeE d s p other
 -- and enter.  Four cases:
 --
 -- 0.  (Nasty hack).
---     An application "PrelGHC.tagToEnum# <type> unboxed-int".
+--     An application "GHC.Prim.tagToEnum# <type> unboxed-int".
 --     The int will be on the stack.  Generate a code sequence
 --     to convert it to the relevant constructor, SLIDE and ENTER.
 --
@@ -483,11 +603,13 @@ schemeE d s p other
 --     (the VoidRep takes up zero stack space).  Also, spot
 --     (# b #) and treat it as  b.
 --
--- 3.  Application of a non-nullary constructor, by defn saturated.
+-- 3.  The fn denotes a ccall.  Defer to generateCCall.
+--
+-- 4.  Application of a non-nullary constructor, by defn saturated.
 --     Split the args into ptrs and non-ptrs, and push the nonptrs, 
 --     then the ptrs, and then do PACK and RETURN.
 --
--- 4.  Otherwise, it must be a function call.  Push the args
+-- 5.  Otherwise, it must be a function call.  Push the args
 --     right to left, SLIDE and ENTER.
 
 schemeT :: Int                 -- Stack depth
@@ -504,42 +626,44 @@ schemeT d s p app
 --   | trace ("\nschemeT\n" ++ showSDoc (pprCoreExpr (deAnnotate app)) ++ "\n") False
 --   = error "?!?!" 
 
-   -- Handle case 0
+   -- Case 0
    | Just (arg, constr_names) <- maybe_is_tagToEnum_call
-   = pushAtom True d p arg             `bind` \ (push, arg_words) ->
+   = pushAtom True d p arg             `thenBc` \ (push, arg_words) ->
      implement_tagToId constr_names    `thenBc` \ tagToId_sequence ->
      returnBc (push `appOL`  tagToId_sequence            
                     `appOL`  mkSLIDE 1 (d+arg_words-s)
                     `snocOL` ENTER)
 
-   -- Handle case 1
+   -- Case 1
    | is_con_call && null args_r_to_l
    = returnBc (
         (PUSH_G (Left (getName con)) `consOL` mkSLIDE 1 (d-s))
         `snocOL` ENTER
      )
 
-   -- Handle case 2
-   | let isVoidRepAtom (_, AnnVar v)    = VoidRep == typePrimRep (idType v)
+   -- Case 2
+   | [arg1,arg2] <- args_r_to_l,
+     let 
+        isVoidRepAtom (_, AnnVar v)    = typePrimRep (idType v) == VoidRep
          isVoidRepAtom (_, AnnNote n e) = isVoidRepAtom e
-     in  is_con_call && isUnboxedTupleCon con 
-         && ( (length args_r_to_l == 2 && isVoidRepAtom (last (args_r_to_l)))
-              || (length args_r_to_l == 1)
-            )
-   = --trace (if length args_r_to_l == 1
+        isVoidRepAtom _ = False
+     in  
+        isVoidRepAtom arg2
+   = --trace (if isSingleton args_r_to_l
      --       then "schemeT: unboxed singleton"
      --       else "schemeT: unboxed pair with Void first component") (
-     schemeT d s p (head args_r_to_l)
+     schemeT d s p arg1
      --)
 
+   -- Case 3
    | Just (CCall ccall_spec) <- isFCallId_maybe fn
    = generateCCall d s p ccall_spec fn args_r_to_l
 
-   -- Cases 3 and 4
+   -- Cases 4 and 5
    | otherwise
    = if   is_con_call && isUnboxedTupleCon con
-     then returnBc unboxedTupleException
-     else code `seq` returnBc code
+     then unboxedTupleException
+     else do_pushery d (map snd args_final_r_to_l)
 
    where
       -- Detect and extract relevant info for the tagToEnum kludge.
@@ -548,7 +672,7 @@ schemeT d s p app
                   = case splitTyConApp_maybe (repType ty) of
                        (Just (tyc, [])) |  isDataTyCon tyc
                                         -> map getName (tyConDataCons tyc)
-                       other            -> panic "maybe_is_tagToEnum_call.extract_constr_Ids"
+                       other -> panic "maybe_is_tagToEnum_call.extract_constr_Ids"
            in 
            case app of
               (_, AnnApp (_, AnnApp (_, AnnVar v) (_, AnnType t)) arg)
@@ -558,23 +682,34 @@ schemeT d s p app
               other -> Nothing
 
       -- Extract the args (R->L) and fn
-      (args_r_to_l_raw, fn) = chomp app
+      (args_r_to_l, fn) = chomp app
       chomp expr
          = case snd expr of
               AnnVar v    -> ([], v)
-              AnnApp f a  -> case chomp f of (az, f) -> (a:az, f)
+              AnnApp f a
+                | isTypeAtom (snd a) -> chomp f
+                | otherwise          -> case chomp f of (az, f) -> (a:az, f)
               AnnNote n e -> chomp e
               other       -> pprPanic "schemeT" 
-                                (ppr (deAnnotate (panic "schemeT.chomp", other)))
-         
-      args_r_to_l = filter (not.isTypeAtom.snd) args_r_to_l_raw
+                               (ppr (deAnnotate (panic "schemeT.chomp", other)))
+
+      n_args = length args_r_to_l
+
       isTypeAtom (AnnType _) = True
       isTypeAtom _           = False
 
-      -- decide if this is a constructor call, and rearrange
-      -- args appropriately.
-      maybe_dcon  = isDataConId_maybe fn
-      is_con_call = case maybe_dcon of Nothing -> False; Just _ -> True
+      -- decide if this is a constructor application, because we need
+      -- to rearrange the arguments on the stack if so.  For building
+      -- a constructor, we put pointers before non-pointers and omit
+      -- the tags.
+      --
+      -- Also if the constructor is not saturated, we just arrange to
+      -- call the curried worker instead.
+
+      maybe_dcon  = case isDataConId_maybe fn of
+                       Just con | dataConRepArity con == n_args -> Just con
+                       _ -> Nothing
+      is_con_call = isJust maybe_dcon
       (Just con)  = maybe_dcon
 
       args_final_r_to_l
@@ -585,29 +720,30 @@ schemeT d s p app
            where isPtr = isFollowableRep . atomRep
 
       -- make code to push the args and then do the SLIDE-ENTER thing
-      code          = do_pushery d (map snd args_final_r_to_l)
       tag_when_push = not is_con_call
       narg_words    = sum (map (get_arg_szw . atomRep . snd) args_r_to_l)
       get_arg_szw   = if tag_when_push then taggedSizeW else untaggedSizeW
 
       do_pushery d (arg:args)
-         = let (push, arg_words) = pushAtom tag_when_push d p arg
-           in  push `appOL` do_pushery (d+arg_words) args
+         = pushAtom tag_when_push d p arg      `thenBc` \ (push, arg_words) ->
+           do_pushery (d+arg_words) args       `thenBc` \ more_push_code ->
+           returnBc (push `appOL` more_push_code)
       do_pushery d []
          | Just (CCall ccall_spec) <- isFCallId_maybe fn
          = panic "schemeT.do_pushery: unexpected ccall"
-
          | otherwise
          = case maybe_dcon of
-              Just con -> PACK con narg_words `consOL` (
-                          mkSLIDE 1 (d - narg_words - s) `snocOL` ENTER)
+              Just con -> returnBc (
+                             (PACK con narg_words `consOL`
+                              mkSLIDE 1 (d - narg_words - s)) `snocOL`
+                              ENTER
+                          )
               Nothing
-                 -> let (push, arg_words) = pushAtom True d p (AnnVar fn)
-                    in  push 
-                        `appOL` mkSLIDE (narg_words+arg_words) 
-                                        (d - s - narg_words)
-                        `snocOL` ENTER
-
+                 -> pushAtom True d p (AnnVar fn)      
+                                               `thenBc` \ (push, arg_words) ->
+                    returnBc (push `appOL` mkSLIDE (narg_words+arg_words) 
+                                                   (d - s - narg_words)
+                              `snocOL` ENTER)
 
 
 {- Deal with a CCall.  Taggedly push the args onto the stack R->L,
@@ -634,27 +770,59 @@ generateCCall d0 s p ccall_spec@(CCallSpec target cconv safety) fn args_r_to_l
          -- depth to the first word of the bits for that arg, and the
          -- PrimRep of what was actually pushed.
 
-         f d [] = []
-         f d ((_,a):az) 
+         pargs d [] = returnBc []
+         pargs d ((_,a):az) 
             = let rep_arg = atomRep a
               in case rep_arg of
                     -- Don't push the FO; instead push the Addr# it
                     -- contains.
                     ForeignObjRep
-                       -> let foro_szW = taggedSizeW ForeignObjRep
-                              push_fo  = fst (pushAtom False{-irrelevant-} d p a)
+                       -> pushAtom False{-irrelevant-} d p a
+                                                       `thenBc` \ (push_fo, _) ->
+                          let foro_szW = taggedSizeW ForeignObjRep
                               d_now    = d + addr_tsizeW
                               code     = push_fo `appOL` toOL [
                                             UPK_TAG addr_usizeW 0 0,
                                             SLIDE addr_tsizeW foro_szW
                                          ]
-                          in  (code, AddrRep) : f d_now az
+                          in  pargs d_now az           `thenBc` \ rest ->
+                              returnBc ((code, AddrRep) : rest)
+
+                    ArrayRep
+                       -> pargs (d + addr_tsizeW) az   `thenBc` \ rest ->
+                          parg_ArrayishRep arrPtrsHdrSize d p a
+                                                       `thenBc` \ code ->
+                          returnBc ((code,AddrRep):rest)
+
+                    ByteArrayRep
+                       -> pargs (d + addr_tsizeW) az   `thenBc` \ rest ->
+                          parg_ArrayishRep arrWordsHdrSize d p a
+                                                       `thenBc` \ code ->
+                          returnBc ((code,AddrRep):rest)
+
                     -- Default case: push taggedly, but otherwise intact.
                     other
-                       -> let (code_a, sz_a) = pushAtom True d p a
-                          in  (code_a, rep_arg) : f (d+sz_a) az
+                       -> pushAtom True d p a          `thenBc` \ (code_a, sz_a) ->
+                          pargs (d+sz_a) az            `thenBc` \ rest ->
+                          returnBc ((code_a, rep_arg) : rest)
+
+         -- Do magic for Ptr/Byte arrays.  Push a ptr to the array on
+         -- the stack but then advance it over the headers, so as to
+         -- point to the payload.
+         parg_ArrayishRep hdrSizeW d p a
+            = pushAtom False{-irrel-} d p a `thenBc` \ (push_fo, _) ->
+              -- The ptr points at the header.  Advance it over the
+              -- header and then pretend this is an Addr# (push a tag).
+              returnBc (push_fo `snocOL` 
+                        SWIZZLE 0 (hdrSizeW * untaggedSizeW PtrRep
+                                            * wORD_SIZE) 
+                        `snocOL`
+                        PUSH_TAG addr_usizeW)
 
-         (pushs_arg, a_reps_pushed_r_to_l) = unzip (f d0 args_r_to_l)
+     in
+         pargs d0 args_r_to_l                          `thenBc` \ code_n_reps ->
+     let
+         (pushs_arg, a_reps_pushed_r_to_l) = unzip code_n_reps
 
          push_args    = concatOL pushs_arg
          d_after_args = d0 + sum (map taggedSizeW a_reps_pushed_r_to_l)
@@ -715,16 +883,14 @@ generateCCall d0 s p ccall_spec@(CCallSpec target cconv safety) fn args_r_to_l
                  DynamicTarget
                     -> returnBc (False, panic "ByteCodeGen.generateCCall(dyn)")
                  StaticTarget target
-                    -> ioToBc (lookupSymbol (_UNPK_ target)) `thenBc` \res ->
+                    -> let sym_to_find = unpackFS target in
+                       ioToBc (lookupSymbol sym_to_find) `thenBc` \res ->
                        case res of
-                           Just aa -> case aa of Ptr a# -> returnBc (True, A# a#)
-                           Nothing -> returnBc invalid
+                           Just aa -> returnBc (True, aa)
+                           Nothing -> ioToBc (linkFail "ByteCodeGen.generateCCall" 
+                                                       sym_to_find)
                  CasmTarget _
-                    -> returnBc invalid
-                 where
-                    invalid = pprPanic ("ByteCodeGen.generateCCall: unfindable " 
-                                        ++ "symbol or otherwise invalid target")
-                                       (ppr ccall_spec)
+                    -> pprPanic "ByteCodeGen.generateCCall: casm" (ppr ccall_spec)
      in
          get_target_info       `thenBc` \ (is_static, static_target_addr) ->
      let
@@ -767,9 +933,10 @@ generateCCall d0 s p ccall_spec@(CCallSpec target cconv safety) fn args_r_to_l
          ioToBc (mkMarshalCode cconv
                     (r_offW, r_rep) addr_offW
                     (zip args_offW a_reps))    `thenBc` \ addr_of_marshaller ->
+         recordMallocBc addr_of_marshaller     `thenBc_`
      let
          -- do the call
-         do_call      = unitOL (CCALL addr_of_marshaller)
+         do_call      = unitOL (CCALL (castPtr addr_of_marshaller))
          -- slide and return
          wrapup       = mkSLIDE r_tsizeW (d_after_r - r_tsizeW - s)
                         `snocOL` RETURN r_rep
@@ -787,37 +954,39 @@ generateCCall d0 s p ccall_spec@(CCallSpec target cconv safety) fn args_r_to_l
 mkDummyLiteral :: PrimRep -> Literal
 mkDummyLiteral pr
    = case pr of
+        CharRep   -> MachChar 0
         IntRep    -> MachInt 0
+        WordRep   -> MachWord 0
         DoubleRep -> MachDouble 0
         FloatRep  -> MachFloat 0
         AddrRep   | taggedSizeW AddrRep == taggedSizeW WordRep -> MachWord 0
-        _         -> pprPanic "mkDummyLiteral" (ppr pr)
+        _         -> moan64 "mkDummyLiteral" (ppr pr)
 
 
 -- Convert (eg) 
---     PrelGHC.Char# -> PrelGHC.State# PrelGHC.RealWorld
---                   -> (# PrelGHC.State# PrelGHC.RealWorld, PrelGHC.Int# #)
+--     GHC.Prim.Char# -> GHC.Prim.State# GHC.Prim.RealWorld
+--                   -> (# GHC.Prim.State# GHC.Prim.RealWorld, GHC.Prim.Int# #)
 --
 -- to  Just IntRep
 -- and check that an unboxed pair is returned wherein the first arg is VoidRep'd.
 --
 -- Alternatively, for call-targets returning nothing, convert
 --
---     PrelGHC.Char# -> PrelGHC.State# PrelGHC.RealWorld
---                   -> (# PrelGHC.State# PrelGHC.RealWorld #)
+--     GHC.Prim.Char# -> GHC.Prim.State# GHC.Prim.RealWorld
+--                   -> (# GHC.Prim.State# GHC.Prim.RealWorld #)
 --
 -- to  Nothing
 
 maybe_getCCallReturnRep :: Type -> Maybe PrimRep
 maybe_getCCallReturnRep fn_ty
-   = let (a_tys, r_ty) = splitRepFunTys fn_ty
+   = let (a_tys, r_ty) = splitFunTys (dropForAlls fn_ty)
          maybe_r_rep_to_go  
-            = if length r_reps == 1 then Nothing else Just (r_reps !! 1)
+            = if isSingleton r_reps then Nothing else Just (r_reps !! 1)
          (r_tycon, r_reps) 
             = case splitTyConApp_maybe (repType r_ty) of
                       (Just (tyc, tys)) -> (tyc, map typePrimRep tys)
                       Nothing -> blargh
-         ok = ( (length r_reps == 2 && VoidRep == head r_reps)
+         ok = ( ( r_reps `lengthIs` 2 && VoidRep == head r_reps)
                 || r_reps == [VoidRep] )
               && isUnboxedTupleTyCon r_tycon
               && case maybe_r_rep_to_go of
@@ -846,7 +1015,7 @@ atomRep other = pprPanic "atomRep" (ppr (deAnnotate (undefined,other)))
 -- as a consequence.
 implement_tagToId :: [Name] -> BcM BCInstrList
 implement_tagToId names
-   = ASSERT(not (null names))
+   = ASSERT( notNull names )
      getLabelsBc (length names)                        `thenBc` \ labels ->
      getLabelBc                                        `thenBc` \ label_fail ->
      getLabelBc                                `thenBc` \ label_exit ->
@@ -925,10 +1094,11 @@ mkUnpackCode vars d p
         code_np = do_nptrs vreps_env_uszw ptrs_szw (reverse (map snd vreps_np))
         do_nptrs off_h off_s [] = nilOL
         do_nptrs off_h off_s (npr:nprs)
-           | npr `elem` [IntRep, WordRep, FloatRep, DoubleRep, CharRep, AddrRep]
+           | npr `elem` [IntRep, WordRep, FloatRep, DoubleRep, 
+                         CharRep, AddrRep, StablePtrRep]
            = approved
            | otherwise
-           = pprPanic "ByteCodeGen.mkUnpackCode" (ppr npr)
+           = moan64 "ByteCodeGen.mkUnpackCode" (ppr npr)
              where
                 approved = UPK_TAG usizeW (off_h-usizeW) off_s   `consOL` theRest
                 theRest  = do_nptrs (off_h-usizeW) (off_s + tsizeW) nprs
@@ -960,18 +1130,18 @@ mkUnpackCode vars d p
 -- 5 and not to 4.  Stack locations are numbered from zero, so a depth
 -- 6 stack has valid words 0 .. 5.
 
-pushAtom :: Bool -> Int -> BCEnv -> AnnExpr' Id VarSet -> (BCInstrList, Int)
+pushAtom :: Bool -> Int -> BCEnv -> AnnExpr' Id VarSet -> BcM (BCInstrList, Int)
 pushAtom tagged d p (AnnVar v)
 
    | idPrimRep v == VoidRep
-   = if tagged then (unitOL (PUSH_TAG 0), 1) 
+   = if tagged then returnBc (unitOL (PUSH_TAG 0), 1) 
                else panic "ByteCodeGen.pushAtom(VoidRep,untaggedly)"
 
    | isFCallId v
    = pprPanic "pushAtom: shouldn't get an FCallId here" (ppr v)
 
    | Just primop <- isPrimOpId_maybe v
-   = (unitOL (PUSH_G (Right primop)), 1)
+   = returnBc (unitOL (PUSH_G (Right primop)), 1)
 
    | otherwise
    = let  {-
@@ -998,14 +1168,15 @@ pushAtom tagged d p (AnnVar v)
          sz_u   = untaggedIdSizeW v
          nwords = if tagged then sz_t else sz_u
      in
-         result
+         returnBc result
 
 pushAtom True d p (AnnLit lit)
-   = let (ubx_code, ubx_size) = pushAtom False d p (AnnLit lit)
-     in  (ubx_code `snocOL` PUSH_TAG ubx_size, 1 + ubx_size)
+   = pushAtom False d p (AnnLit lit)           `thenBc` \ (ubx_code, ubx_size) ->
+     returnBc (ubx_code `snocOL` PUSH_TAG ubx_size, 1 + ubx_size)
 
 pushAtom False d p (AnnLit lit)
    = case lit of
+        MachLabel fs -> code CodePtrRep
         MachWord w   -> code WordRep
         MachInt i    -> code IntRep
         MachFloat r  -> code FloatRep
@@ -1015,12 +1186,13 @@ pushAtom False d p (AnnLit lit)
      where
         code rep
            = let size_host_words = untaggedSizeW rep
-             in (unitOL (PUSH_UBX (Left lit) size_host_words), size_host_words)
+             in  returnBc (unitOL (PUSH_UBX (Left lit) size_host_words), 
+                           size_host_words)
 
         pushStr s 
-           = let mallocvilleAddr
+           = let getMallocvilleAddr
                     = case s of
-                         CharStr s i -> A# s
+                         CharStr s i -> returnBc (Ptr s)
 
                          FastString _ l ba -> 
                             -- sigh, a string in the heap is no good to us.
@@ -1030,16 +1202,18 @@ pushAtom False d p (AnnLit lit)
                             -- at the same time.
                             let n = I# l
                             -- CAREFUL!  Chars are 32 bits in ghc 4.09+
-                            in  unsafePerformIO (
-                                   do (Ptr a#) <- mallocBytes (n+1)
-                                      strncpy (Ptr a#) ba (fromIntegral n)
-                                      writeCharOffAddr (A# a#) n '\0'
-                                      return (A# a#)
+                            in  ioToBc (mallocBytes (n+1)) `thenBc` \ ptr ->
+                                recordMallocBc ptr         `thenBc_`
+                                ioToBc (
+                                   do memcpy ptr ba (fromIntegral n)
+                                     pokeByteOff ptr n (fromIntegral (ord '\0') :: Word8)
+                                      return ptr
                                    )
-                         _ -> panic "StgInterp.lit2expr: unhandled string constant type"
+                         other -> panic "ByteCodeGen.pushAtom.pushStr"
              in
+                getMallocvilleAddr `thenBc` \ addr ->
                 -- Get the addr on the stack, untaggedly
-                (unitOL (PUSH_UBX (Right mallocvilleAddr) 1), 1)
+                   returnBc (unitOL (PUSH_UBX (Right addr) 1), 1)
 
 
 
@@ -1059,7 +1233,7 @@ pushAtom tagged d p other
    = pprPanic "ByteCodeGen.pushAtom" 
               (pprCoreExpr (deAnnotate (undefined, other)))
 
-foreign import "strncpy" strncpy :: Ptr a -> ByteArray# -> CInt -> IO ()
+foreign import "memcpy" memcpy :: Ptr a -> ByteArray# -> CInt -> IO ()
 
 
 -- Given a bunch of alts code and their discrs, do the donkey work
@@ -1234,8 +1408,9 @@ bind x f    = f x
 \begin{code}
 data BcM_State 
    = BcM_State { bcos      :: [ProtoBCO Name], -- accumulates completed BCOs
-                 nextlabel :: Int }            -- for generating local labels
-
+                 nextlabel :: Int,             -- for generating local labels
+                 malloced  :: [Ptr ()] }       -- ptrs malloced for current BCO
+                                                -- Should be free()d when it is GCd
 type BcM r = BcM_State -> IO (BcM_State, r)
 
 ioToBc :: IO a -> BcM a
@@ -1269,9 +1444,20 @@ mapBc f (x:xs)
     mapBc f xs   `thenBc` \ rs ->
     returnBc (r:rs)
 
-emitBc :: ProtoBCO Name -> BcM ()
+emitBc :: ([Ptr ()] -> ProtoBCO Name) -> BcM ()
 emitBc bco st
-   = return (st{bcos = bco : bcos st}, ())
+   = return (st{bcos = bco (malloced st) : bcos st, malloced=[]}, ())
+
+newbcoBc :: BcM ()
+newbcoBc st
+   | notNull (malloced st)
+   = panic "ByteCodeGen.newbcoBc: missed prior emitBc?"
+   | otherwise
+   = return (st, ())
+
+recordMallocBc :: Ptr a -> BcM ()
+recordMallocBc a st
+   = return (st{malloced = castPtr a : malloced st}, ())
 
 getLabelBc :: BcM Int
 getLabelBc st