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 TyCon ( TyCon(..), tyConFamilySize, isDataTyCon, tyConDataCons,
isFunTyCon, isUnboxedTupleTyCon )
import Class ( Class, classTyCon )
-import Type ( Type, repType, splitRepFunTys )
+import Type ( Type, repType, splitFunTys, dropForAlls )
import Util ( zipEqual, zipWith4Equal, naturalMergeSortLe, nOfThem,
- isSingleton, lengthIs )
+ isSingleton, lengthIs, notNull )
+import DataCon ( dataConRepArity )
import Var ( isTyVar )
import VarSet ( VarSet, varSetElems )
import PrimRep ( isFollowableRep )
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 Monad ( when )
+import GlaExts ( Int(..), ByteArray# )
+import Monad ( when )
+import Maybe ( isJust )
+import Char ( ord )
\end{code}
%************************************************************************
(BcM_State proto_bcos final_ctr mallocd, ())
<- runBc (BcM_State [] 0 [])
- (mapBc (schemeR True) flatBinds `thenBc_` returnBc ())
+ (mapBc (schemeR True []) flatBinds `thenBc_` returnBc ())
+ -- ^^
+ -- better be no free vars in these top-level bindings
- when (not (null mallocd))
+ when (notNull mallocd)
(panic "ByteCodeGen.byteCodeGen: missing final emitBc?")
dumpIfSet_dyn dflags Opt_D_dump_BCOs
-- 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
+ annexpr = freeVars expr
+ fvs = filter (not.isTyVar) (varSetElems (fst annexpr))
+
(BcM_State all_proto_bcos final_ctr mallocd, ())
<- runBc (BcM_State [] 0 [])
- (schemeR True (invented_id, freeVars expr))
+ (schemeR True fvs (invented_id, annexpr))
- when (not (null mallocd))
+ when (notNull mallocd)
(panic "ByteCodeGen.coreExprToBCOs: missing final emitBc?")
dumpIfSet_dyn dflags Opt_D_dump_BCOs
-- 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))
`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)
-- 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.
--
)
-- Case 2
- | let isVoidRepAtom (_, AnnVar v) = VoidRep == typePrimRep (idType v)
+ | [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
- && ( (args_r_to_l `lengthIs` 2 && isVoidRepAtom (last (args_r_to_l)))
- || (isSingleton args_r_to_l)
- )
+ 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
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
DynamicTarget
-> returnBc (False, panic "ByteCodeGen.generateCCall(dyn)")
StaticTarget target
- -> let sym_to_find = _UNPK_ target in
+ -> 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#)
+ Just aa -> returnBc (True, aa)
Nothing -> ioToBc (linkFail "ByteCodeGen.generateCCall"
sym_to_find)
CasmTarget _
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
-- 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 isSingleton r_reps then Nothing else Just (r_reps !! 1)
(r_tycon, r_reps)
-- 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 ->
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
= moan64 "ByteCodeGen.mkUnpackCode" (ppr npr)
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
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 (mallocBytes (n+1)) `thenBc` \ (Ptr a#) ->
- recordMallocBc (A# a#) `thenBc_`
+ in ioToBc (mallocBytes (n+1)) `thenBc` \ ptr ->
+ recordMallocBc ptr `thenBc_`
ioToBc (
- do strncpy (Ptr a#) ba (fromIntegral n)
- writeCharOffAddr (A# a#) n '\0'
- return (A# a#)
+ 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
data BcM_State
= BcM_State { bcos :: [ProtoBCO Name], -- accumulates completed BCOs
nextlabel :: Int, -- for generating local labels
- malloced :: [Addr] } -- ptrs malloced for current BCO
+ malloced :: [Ptr ()] } -- ptrs malloced for current BCO
-- Should be free()d when it is GCd
type BcM r = BcM_State -> IO (BcM_State, r)
mapBc f xs `thenBc` \ rs ->
returnBc (r:rs)
-emitBc :: ([Addr] -> ProtoBCO Name) -> BcM ()
+emitBc :: ([Ptr ()] -> ProtoBCO Name) -> BcM ()
emitBc bco st
= return (st{bcos = bco (malloced st) : bcos st, malloced=[]}, ())
newbcoBc :: BcM ()
newbcoBc st
- | not (null (malloced st))
+ | notNull (malloced st)
= panic "ByteCodeGen.newbcoBc: missed prior emitBc?"
| otherwise
= return (st, ())
-recordMallocBc :: Addr -> BcM ()
+recordMallocBc :: Ptr a -> BcM ()
recordMallocBc a st
- = return (st{malloced = a : malloced st}, ())
+ = return (st{malloced = castPtr a : malloced st}, ())
getLabelBc :: BcM Int
getLabelBc st