X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2FdeSugar%2FDsCCall.lhs;h=35722fae20d7407dfe710a2f2575addfdd970e9e;hb=783e505e2d884f94d30ec8074e590507f2561c49;hp=b54e111991732992fbcb906ac546318fb1b39fdf;hpb=7d61cb61daa5e433a0cb85b34b7f0c58b2f961ff;p=ghc-hetmet.git diff --git a/ghc/compiler/deSugar/DsCCall.lhs b/ghc/compiler/deSugar/DsCCall.lhs index b54e111..35722fa 100644 --- a/ghc/compiler/deSugar/DsCCall.lhs +++ b/ghc/compiler/deSugar/DsCCall.lhs @@ -1,35 +1,47 @@ % -% (c) The AQUA Project, Glasgow University, 1994-1996 +% (c) The AQUA Project, Glasgow University, 1994-1998 % \section[DsCCall]{Desugaring \tr{_ccall_}s and \tr{_casm_}s} \begin{code} -#include "HsVersions.h" - -module DsCCall ( dsCCall ) where +module DsCCall + ( dsCCall + , mkCCall + , unboxArg + , boxResult + , wrapUnboxedValue + , can'tSeeDataConsPanic + + ) where -import Ubiq +#include "HsVersions.h" import CoreSyn import DsMonad import DsUtils -import CoreUtils ( coreExprType ) -import Id ( getInstantiatedDataConSig, mkTupleCon ) +import TcHsSyn ( maybeBoxedPrimType ) +import CoreUtils ( exprType ) +import Id ( Id, mkWildId ) +import MkId ( mkCCallOpId ) import Maybes ( maybeToBool ) -import PprStyle ( PprStyle(..) ) -import PprType ( GenType{-instances-} ) -import PrelInfo ( byteArrayPrimTy, getStatePairingConInfo, - packStringForCId, realWorldStatePrimTy, - realWorldStateTy, realWorldTy, stateDataCon, - stringTy ) -import Pretty -import PrimOp ( PrimOp(..) ) -import Type ( isPrimType, maybeAppDataTyCon, eqTy ) -import Util ( pprPanic, pprError, panic ) - -maybeBoxedPrimType = panic "DsCCall.maybeBoxedPrimType" +import PrelInfo ( packStringForCId ) +import PrimOp ( PrimOp(..), CCall(..), CCallTarget(..) ) +import DataCon ( DataCon, splitProductType_maybe ) +import CallConv +import Type ( isUnLiftedType, splitAlgTyConApp_maybe, mkFunTys, + splitTyConApp_maybe, tyVarsOfType, mkForAllTys, Type + ) +import TysPrim ( byteArrayPrimTy, realWorldStatePrimTy, + byteArrayPrimTyCon, mutableByteArrayPrimTyCon ) +import TysWiredIn ( unitDataConId, stringTy, + unboxedPairDataCon, + mkUnboxedTupleTy, unboxedTupleCon + ) +import Unique ( Unique ) +import VarSet ( varSetElems ) +import Outputable \end{code} Desugaring of @ccall@s consists of adding some state manipulation, @@ -37,7 +49,7 @@ unboxing any boxed primitive arguments and boxing the result if desired. The state stuff just consists of adding in -@\ s -> case s of { S# s# -> ... }@ in an appropriate place. +@PrimIO (\ s -> case s of { S# s# -> ... })@ in an appropriate place. The unboxing is straightforward, as all information needed to unbox is available from the type. For each boxed-primitive argument, we @@ -68,233 +80,209 @@ follows: \end{verbatim} \begin{code} -dsCCall :: FAST_STRING -- C routine to invoke +dsCCall :: FAST_STRING -- C routine to invoke -> [CoreExpr] -- Arguments (desugared) - -> Bool -- True <=> might cause Haskell GC - -> Bool -- True <=> really a "_casm_" - -> Type -- Type of the result (a boxed-prim type) + -> Bool -- True <=> might cause Haskell GC + -> Bool -- True <=> really a "_casm_" + -> Type -- Type of the result (a boxed-prim IO type) -> DsM CoreExpr -dsCCall label args may_gc is_asm result_ty - = newSysLocalDs realWorldStateTy `thenDs` \ old_s -> - - mapAndUnzipDs unboxArg (Var old_s : args) `thenDs` \ (final_args, arg_wrappers) -> +dsCCall lbl args may_gc is_asm result_ty + = newSysLocalDs realWorldStatePrimTy `thenDs` \ old_s -> - boxResult result_ty `thenDs` \ (final_result_ty, res_wrapper) -> - - let - the_ccall_op = CCallOp label is_asm may_gc - (map coreExprType final_args) - final_result_ty - in - mkPrimDs the_ccall_op - [] -- ***NOTE*** no ty apps; the types are inside the_ccall_op. - final_args `thenDs` \ the_prim_app -> + mapAndUnzipDs unboxArg args `thenDs` \ (unboxed_args, arg_wrappers) -> + boxResult result_ty `thenDs` \ (final_result_ty, res_wrapper) -> + getUniqueDs `thenDs` \ uniq -> let - the_body = foldr apply (res_wrapper the_prim_app) arg_wrappers + val_args = Var old_s : unboxed_args + the_ccall = CCall (StaticTarget lbl) is_asm may_gc cCallConv + the_prim_app = mkCCall uniq the_ccall val_args final_result_ty + the_body = foldr ($) (res_wrapper the_prim_app) arg_wrappers in - returnDs (Lam (ValBinder old_s) the_body) + returnDs (Lam old_s the_body) + +mkCCall :: Unique -> CCall + -> [CoreExpr] -- Args + -> Type -- Result type + -> CoreExpr +-- Construct the ccall. The only tricky bit is that the ccall Id should have +-- no free vars, so if any of the arg tys do we must give it a polymorphic type. +-- [I forget *why* it should have no free vars!] +-- For example: +-- mkCCall ... [s::StablePtr (a->b), x::Addr, c::Char] +-- +-- Here we build a ccall thus +-- (ccallid::(forall a b. StablePtr (a -> b) -> Addr -> Char -> IO Addr)) +-- a b s x c +mkCCall uniq the_ccall val_args res_ty + = mkApps (mkVarApps (Var the_ccall_id) tyvars) val_args where - apply f x = f x + arg_tys = map exprType val_args + body_ty = (mkFunTys arg_tys res_ty) + tyvars = varSetElems (tyVarsOfType body_ty) + ty = mkForAllTys tyvars body_ty + the_ccall_id = mkCCallOpId uniq the_ccall ty \end{code} \begin{code} unboxArg :: CoreExpr -- The supplied argument - -> DsM (CoreExpr, -- To pass as the actual argument + -> DsM (CoreExpr, -- To pass as the actual argument CoreExpr -> CoreExpr -- Wrapper to unbox the arg ) unboxArg arg -- Primitive types -- ADR Question: can this ever be used? None of the PrimTypes are - -- instances of the _CCallable class. - | isPrimType arg_ty + -- instances of the CCallable class. + -- + -- SOF response: + -- Oh yes they are, I've just added them :-) Having _ccall_ and _casm_ + -- that accept unboxed arguments is a Good Thing if you have a stub generator + -- which generates the boiler-plate box-unbox code for you, i.e., it may help + -- us nuke this very module :-) + -- + | isUnLiftedType arg_ty = returnDs (arg, \body -> body) -- Strings - | arg_ty `eqTy` stringTy + | arg_ty == stringTy -- ToDo (ADR): - allow synonyms of Strings too? = newSysLocalDs byteArrayPrimTy `thenDs` \ prim_arg -> - mkAppDs (Var packStringForCId) [] [arg] `thenDs` \ pack_appn -> returnDs (Var prim_arg, - \body -> Case pack_appn (PrimAlts [] - (BindDefault prim_arg body)) - ) - - | null data_cons - -- oops: we can't see the data constructors!!! - = can't_see_datacons_error "argument" arg_ty - - -- Byte-arrays, both mutable and otherwise - -- (HACKy method -- but we really don't want the TyCons wired-in...) [WDP 94/10] - | is_data_type && - length data_con_arg_tys == 2 && - not (isPrimType data_con_arg_ty1) && - isPrimType data_con_arg_ty2 - -- and, of course, it is an instance of _CCallable --- ( tycon == byteArrayTyCon || --- tycon == mutableByteArrayTyCon ) - = newSysLocalsDs data_con_arg_tys `thenDs` \ vars@[ixs_var, arr_cts_var] -> + \body -> Case (App (Var packStringForCId) arg) + prim_arg [(DEFAULT,[],body)]) + + -- Byte-arrays, both mutable and otherwise; hack warning + | is_product_type && + length data_con_arg_tys == 3 && + maybeToBool maybe_arg3_tycon && + (arg3_tycon == byteArrayPrimTyCon || + arg3_tycon == mutableByteArrayPrimTyCon) + -- and, of course, it is an instance of CCallable + = newSysLocalDs arg_ty `thenDs` \ case_bndr -> + newSysLocalsDs data_con_arg_tys `thenDs` \ vars@[l_var, r_var, arr_cts_var] -> returnDs (Var arr_cts_var, - \ body -> Case arg (AlgAlts [(the_data_con,vars,body)] - NoDefault) + \ body -> Case arg case_bndr [(DataAlt data_con,vars,body)] ) -- Data types with a single constructor, which has a single, primitive-typed arg | maybeToBool maybe_boxed_prim_arg_ty - = newSysLocalDs the_prim_arg_ty `thenDs` \ prim_arg -> - returnDs (Var prim_arg, - \ body -> Case arg (AlgAlts [(box_data_con,[prim_arg],body)] - NoDefault) - ) - -- ... continued below .... -\end{code} - -As an experiment, I'm going to unpack any "acceptably small" -enumeration. This code will never get used in the main version -because enumerations would have triggered type errors but I've -disabled type-checking in my version. ADR - -To Will: It might be worth leaving this in (but commented out) until -we decide what's happening with enumerations. ADR - -\begin{code} -#if 0 - -- MAYBE LATER: - -- Data types with a nullary constructors (enumeration) - | isEnumerationType arg_ty && -- enumeration - (length data_cons) <= 5 -- "acceptably short" - = newSysLocalDs the_prim_arg_ty `thenDs` \ prim_arg -> - - let - alts = [ (con, [], mkMachInt i) | (con,i) <- data_cons `zip` [0..] ] - arg_tag = Case arg (AlgAlts alts) NoDefault - in - + = newSysLocalDs arg_ty `thenDs` \ case_bndr -> + newSysLocalDs the_prim_arg_ty `thenDs` \ prim_arg -> returnDs (Var prim_arg, - \ body -> Case arg_tag (PrimAlts [(prim_arg, body)] NoDefault) + \ body -> Case arg case_bndr [(DataAlt box_data_con,[prim_arg],body)] ) -#endif -\end{code} -\begin{code} - -- ... continued from above .... | otherwise - = pprPanic "unboxArg: " (ppr PprDebug arg_ty) + = getSrcLocDs `thenDs` \ l -> + pprPanic "unboxArg: " (ppr l <+> ppr arg_ty) where - arg_ty = coreExprType arg + arg_ty = exprType arg maybe_boxed_prim_arg_ty = maybeBoxedPrimType arg_ty (Just (box_data_con, the_prim_arg_ty)) = maybe_boxed_prim_arg_ty - maybe_data_type = maybeAppDataTyCon arg_ty - is_data_type = maybeToBool maybe_data_type - (Just (tycon, tycon_arg_tys, data_cons)) = maybe_data_type - (the_data_con : other_data_cons) = data_cons + maybe_product_type = splitProductType_maybe arg_ty + is_product_type = maybeToBool maybe_product_type + Just (tycon, _, data_con, data_con_arg_tys) = maybe_product_type + (data_con_arg_ty1 : data_con_arg_ty2 : data_con_arg_ty3 :_) + = data_con_arg_tys - (_, data_con_arg_tys, _) = getInstantiatedDataConSig the_data_con tycon_arg_tys - (data_con_arg_ty1 : data_con_arg_ty2 : _) = data_con_arg_tys + maybe_arg3_tycon = splitTyConApp_maybe data_con_arg_ty3 + Just (arg3_tycon,_) = maybe_arg3_tycon -can't_see_datacons_error thing ty - = pprError "ERROR: Can't see the data constructor(s) for _ccall_/_casm_ " - (ppBesides [ppStr thing, ppStr "; type: ", ppr PprForUser ty]) +can'tSeeDataConsPanic thing ty + = pprPanic + "ERROR: Can't see the data constructor(s) for _ccall_/_casm_/foreign declaration" + (hcat [ text thing, text "; type: ", ppr ty + , text "(try compiling with -fno-prune-tydecls ..)\n"]) \end{code} \begin{code} -tuple_con_2 = mkTupleCon 2 -- out here to avoid CAF (sigh) -covar_tuple_con_0 = Var (mkTupleCon 0) -- ditto - -boxResult :: Type -- Type of desired result +boxResult :: Type -- Type of desired result -> DsM (Type, -- Type of the result of the ccall itself CoreExpr -> CoreExpr) -- Wrapper for the ccall - -- to box the result + -- to box the result boxResult result_ty - | null data_cons - -- oops! can't see the data constructors - = can't_see_datacons_error "result" result_ty - - -- Data types with a single constructor, which has a single, primitive-typed arg - | (maybeToBool maybe_data_type) && -- Data type - (null other_data_cons) && -- Just one constr - not (null data_con_arg_tys) && null other_args_tys && -- Just one arg - isPrimType the_prim_result_ty -- of primitive type + -- Data types with a single nullary constructor + | (maybeToBool maybe_product_type) && -- Data type + (null data_con_arg_tys) = - newSysLocalDs realWorldStatePrimTy `thenDs` \ prim_state_id -> - newSysLocalDs the_prim_result_ty `thenDs` \ prim_result_id -> - - mkConDs stateDataCon [realWorldTy] [Var prim_state_id] `thenDs` \ new_state -> - mkConDs the_data_con tycon_arg_tys [Var prim_result_id] `thenDs` \ the_result -> - - mkConDs tuple_con_2 - [result_ty, realWorldStateTy] - [the_result, new_state] `thenDs` \ the_pair -> + newSysLocalDs realWorldStatePrimTy `thenDs` \ prim_state_id -> +{- + wrapUnboxedValue result_ty `thenDs` \ (state_and_prim_datacon, + state_and_prim_ty, prim_result_id, the_result) -> + mkConDs ioOkDataCon + [TyArg result_ty, VarArg (Var prim_state_id), VarArg the_result] + `thenDs` \ the_pair -> +-} let - the_alt = (state_and_prim_datacon, [prim_state_id, prim_result_id], the_pair) + the_pair = mkConApp unboxedPairDataCon + [Type realWorldStatePrimTy, Type result_ty, + Var prim_state_id, + Var unitDataConId] + the_alt = (DataAlt (unboxedTupleCon 1), [prim_state_id], the_pair) + scrut_ty = mkUnboxedTupleTy 1 [realWorldStatePrimTy] in - returnDs (state_and_prim_ty, - \prim_app -> Case prim_app (AlgAlts [the_alt] NoDefault) + returnDs (scrut_ty, \prim_app -> Case prim_app (mkWildId scrut_ty) [the_alt] ) - -- Data types with a single nullary constructor - | (maybeToBool maybe_data_type) && -- Data type - (null other_data_cons) && -- Just one constr - (null data_con_arg_tys) + -- Data types with a single constructor, which has a single, primitive-typed arg + | (maybeToBool maybe_product_type) && -- Data type + not (null data_con_arg_tys) && null other_args_tys && -- Just one arg + isUnLiftedType the_prim_result_ty -- of primitive type = - newSysLocalDs realWorldStatePrimTy `thenDs` \ prim_state_id -> - - mkConDs stateDataCon [realWorldTy] [Var prim_state_id] `thenDs` \ new_state -> - - mkConDs tuple_con_2 - [result_ty, realWorldStateTy] - [covar_tuple_con_0, new_state] `thenDs` \ the_pair -> + newSysLocalDs realWorldStatePrimTy `thenDs` \ prim_state_id -> + newSysLocalDs the_prim_result_ty `thenDs` \ prim_result_id -> + newSysLocalDs ccall_res_type `thenDs` \ case_bndr -> let - the_alt = (stateDataCon, [prim_state_id], the_pair) + the_result = mkConApp data_con (map Type tycon_arg_tys ++ [Var prim_result_id]) + the_pair = mkConApp unboxedPairDataCon + [Type realWorldStatePrimTy, Type result_ty, + Var prim_state_id, the_result] + the_alt = (DataAlt unboxedPairDataCon, [prim_state_id, prim_result_id], the_pair) in - returnDs (realWorldStateTy, - \prim_app -> Case prim_app (AlgAlts [the_alt] NoDefault) + returnDs (ccall_res_type, \prim_app -> Case prim_app case_bndr [the_alt] ) -#if 0 - -- MAYBE LATER??? - - -- Data types with several nullary constructors (Enumerated types) - | isEnumerationType result_ty && -- Enumeration - (length data_cons) <= 5 -- fairly short - = - newSysLocalDs realWorldStatePrimTy `thenDs` \ prim_state_id -> - newSysLocalDs intPrimTy `thenDs` \ prim_result_id -> + | otherwise + = pprPanic "boxResult: " (ppr result_ty) + where + maybe_product_type = splitProductType_maybe result_ty + Just (tycon, tycon_arg_tys, data_con, data_con_arg_tys) = maybe_product_type + (the_prim_result_ty : other_args_tys) = data_con_arg_tys - mkConDs stateDataCon [realWorldTy] [Var prim_state_id] `thenDs` \ new_state -> + ccall_res_type = mkUnboxedTupleTy 2 [realWorldStatePrimTy, the_prim_result_ty] +-- wrap up an unboxed value. +wrapUnboxedValue :: Type -> DsM (Type, Id, CoreExpr) +wrapUnboxedValue ty + | (maybeToBool maybe_product_type) && -- Data type + not (null data_con_arg_tys) && null other_args_tys && -- Just one arg + isUnLiftedType the_prim_result_ty -- of primitive type + = + newSysLocalDs the_prim_result_ty `thenDs` \ prim_result_id -> let - alts = [ (mkMachInt i, con) | (i, con) <- [0..] `zip` data_cons ] - the_result = Case prim_result_id (PrimAlts alts) NoDefault + the_result = mkConApp data_con (map Type tycon_arg_tys ++ [Var prim_result_id]) in + returnDs (ccall_res_type, prim_result_id, the_result) - mkConDs (mkTupleCon 2) - [result_ty, realWorldStateTy] - [the_result, new_state] `thenDs` \ the_pair -> - let - the_alt = (state_and_prim_datacon, [prim_state_id, prim_result_id], the_pair) + -- Data types with a single nullary constructor + | (maybeToBool maybe_product_type) && -- Data type + (null data_con_arg_tys) + = + let + scrut_ty = mkUnboxedTupleTy 1 [realWorldStatePrimTy] in - returnDs (state_and_prim_ty, - \prim_app -> Case prim_app (AlgAlts [the_alt] NoDefault) - ) -#endif + returnDs (scrut_ty, unitDataConId, Var unitDataConId) | otherwise - = pprPanic "boxResult: " (ppr PprDebug result_ty) - - where - maybe_data_type = maybeAppDataTyCon result_ty - Just (tycon, tycon_arg_tys, data_cons) = maybe_data_type - (the_data_con : other_data_cons) = data_cons - - (_, data_con_arg_tys, _) = getInstantiatedDataConSig the_data_con tycon_arg_tys - (the_prim_result_ty : other_args_tys) = data_con_arg_tys - - (state_and_prim_datacon, state_and_prim_ty) = getStatePairingConInfo the_prim_result_ty + = pprPanic "boxResult: " (ppr ty) + where + maybe_product_type = splitProductType_maybe ty + Just (tycon, tycon_arg_tys, data_con, data_con_arg_tys) = maybe_product_type + (the_prim_result_ty : other_args_tys) = data_con_arg_tys + ccall_res_type = mkUnboxedTupleTy 2 [realWorldStatePrimTy, the_prim_result_ty] \end{code} -