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 )
isFunTyCon, isUnboxedTupleTyCon )
import Class ( Class, classTyCon )
import Type ( Type, repType, splitRepFunTys )
-import Util ( zipEqual, zipWith4Equal, naturalMergeSortLe, nOfThem )
+import Util ( zipEqual, zipWith4Equal, naturalMergeSortLe, nOfThem,
+ isSingleton, lengthIs )
+import DataCon ( dataConRepArity )
import Var ( isTyVar )
import VarSet ( VarSet, varSetElems )
import PrimRep ( isFollowableRep )
import FastString ( FastString(..) )
import Panic ( GhcException(..) )
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(..), writeCharOffAddr )
+import Foreign ( Ptr(..), castPtr, mallocBytes, pokeByteOff, Word8 )
import CTypes ( CInt )
import Exception ( throwDyn )
-import PrelBase ( Int(..) )
-import PrelGHC ( ByteArray# )
-import PrelIOBase ( IO(..) )
+import GlaExts ( Int(..), ByteArray# )
+import Monad ( when )
+import Maybe ( isJust )
+import Char ( ord )
\end{code}
%************************************************************************
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 (not (null mallocd))
+ (panic "ByteCodeGen.byteCodeGen: missing final emitBc?")
dumpIfSet_dyn dflags Opt_D_dump_BCOs
"Proto-bcos" (vcat (intersperse (char ' ') (map ppr proto_bcos)))
(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 (not (null mallocd))
+ (panic "ByteCodeGen.coreExprToBCOs: missing final emitBc?")
dumpIfSet_dyn dflags Opt_D_dump_BCOs
"Proto-bcos" (vcat (intersperse (char ' ') (map ppr all_proto_bcos)))
-- 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
-- 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 ' '
= undefined
-}
| otherwise
- = schemeR_wrk is_top rhs nm (collect [] rhs)
+ = schemeR_wrk is_top fvs rhs nm (collect [] rhs)
collect xs (_, AnnNote note 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])
--)
| 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))
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
| 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.
`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
returnBc (concatOL tcodes)
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)
-- (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
-- | 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 `thenBc` \ (push, arg_words) ->
implement_tagToId constr_names `thenBc` \ 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
+ -- Case 2
| let isVoidRepAtom (_, AnnVar v) = VoidRep == typePrimRep (idType v)
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)
+ && ( (args_r_to_l `lengthIs` 2 && isVoidRepAtom (last (args_r_to_l)))
+ || (isSingleton args_r_to_l)
)
- = --trace (if length args_r_to_l == 1
+ = --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)
--)
+ -- 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 unboxedTupleException
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
`snocOL` ENTER)
-
{- Deal with a CCall. Taggedly push the args onto the stack R->L,
deferencing ForeignObj#s and (ToDo: adjusting addrs to point to
payloads in Ptr/Byte arrays). Then, generate the marshalling
]
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
-> 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)
+
in
pargs d0 args_r_to_l `thenBc` \ code_n_reps ->
let
DynamicTarget
-> returnBc (False, panic "ByteCodeGen.generateCCall(dyn)")
StaticTarget target
- -> ioToBc (lookupSymbol (_UNPK_ target)) `thenBc` \res ->
+ -> let sym_to_find = _UNPK_ 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
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
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)
maybe_getCCallReturnRep fn_ty
= let (a_tys, r_ty) = splitRepFunTys 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
| npr `elem` [IntRep, WordRep, FloatRep, DoubleRep, CharRep, AddrRep]
= 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
pushStr s
= let getMallocvilleAddr
= case s of
- CharStr s i -> returnBc (A# s)
+ CharStr s i -> returnBc (Ptr s)
FastString _ l ba ->
-- sigh, a string in the heap is no good to us.
-- at the same time.
let n = I# l
-- CAREFUL! Chars are 32 bits in ghc 4.09+
- in ioToBc (
- 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
)
other -> panic "ByteCodeGen.pushAtom.pushStr"
in
= 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
\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
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
+ | not (null (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