module DsCCall ( dsCCall ) where
-import Ubiq
+IMP_Ubiq()
+import CmdLineOpts (opt_PprUserLength)
import CoreSyn
import DsMonad
import DsUtils
import CoreUtils ( coreExprType )
-import Id ( dataConArgTys, mkTupleCon )
+import Id ( dataConArgTys, dataConTyCon, idType )
import Maybes ( maybeToBool )
-import PprStyle ( PprStyle(..) )
+import Outputable ( PprStyle(..), Outputable(..) )
import PprType ( GenType{-instances-} )
-import PrelInfo ( byteArrayPrimTy, getStatePairingConInfo,
- packStringForCId, realWorldStatePrimTy,
- realWorldStateTy, realWorldTy, stateDataCon,
- stringTy )
import Pretty
+import PrelVals ( packStringForCId )
import PrimOp ( PrimOp(..) )
-import Type ( isPrimType, maybeAppDataTyConExpandingDicts, eqTy )
+import Type ( isPrimType, maybeAppDataTyConExpandingDicts, maybeAppTyCon,
+ eqTy, maybeBoxedPrimType, SYN_IE(Type), GenType(..),
+ splitFunTy, splitForAllTy, splitAppTys )
+import TyCon ( tyConDataCons )
+import TysPrim ( byteArrayPrimTy, realWorldTy, realWorldStatePrimTy,
+ byteArrayPrimTyCon, mutableByteArrayPrimTyCon )
+import TysWiredIn ( getStatePairingConInfo,
+ unitDataCon, stringTy,
+ realWorldStateTy, stateDataCon
+ )
import Util ( pprPanic, pprError, panic )
-maybeBoxedPrimType = panic "DsCCall.maybeBoxedPrimType"
\end{code}
Desugaring of @ccall@s consists of adding some state manipulation,
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
\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 ->
+dsCCall label args may_gc is_asm io_result_ty
+ = newSysLocalDs realWorldStatePrimTy `thenDs` \ old_s ->
- mapAndUnzipDs unboxArg (Var old_s : args) `thenDs` \ (final_args, arg_wrappers) ->
+ mapAndUnzipDs unboxArg args `thenDs` \ (unboxed_args, arg_wrappers) ->
+ let
+ final_args = Var old_s : unboxed_args
+ (ioOkDataCon, result_ty) = getIoOkDataCon io_result_ty
+ in
- boxResult result_ty `thenDs` \ (final_result_ty, res_wrapper) ->
+ boxResult ioOkDataCon 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 ->
+ mkPrimDs the_ccall_op (map VarArg final_args) `thenDs` \ the_prim_app ->
let
- the_body = foldr apply (res_wrapper the_prim_app) arg_wrappers
+ the_body = foldr ($) (res_wrapper the_prim_app) arg_wrappers
in
returnDs (Lam (ValBinder old_s) the_body)
- where
- apply f x = f x
\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.
+ --
+ -- 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 :-)
+ --
| isPrimType arg_ty
= returnDs (arg, \body -> body)
| arg_ty `eqTy` stringTy
-- ToDo (ADR): - allow synonyms of Strings too?
= newSysLocalDs byteArrayPrimTy `thenDs` \ prim_arg ->
- mkAppDs (Var packStringForCId) [] [arg] `thenDs` \ pack_appn ->
+ mkAppDs (Var packStringForCId) [VarArg arg] `thenDs` \ pack_appn ->
returnDs (Var prim_arg,
\body -> Case pack_appn (PrimAlts []
(BindDefault prim_arg body))
-- 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]
+ -- Byte-arrays, both mutable and otherwise; hack warning
| is_data_type &&
length data_con_arg_tys == 2 &&
- not (isPrimType data_con_arg_ty1) &&
- isPrimType data_con_arg_ty2
+ maybeToBool maybe_arg2_tycon &&
+ (arg2_tycon == byteArrayPrimTyCon ||
+ arg2_tycon == mutableByteArrayPrimTyCon)
-- 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] ->
returnDs (Var arr_cts_var,
\ body -> Case arg (AlgAlts [(the_data_con,vars,body)]
\ 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
-
- returnDs (Var prim_arg,
- \ body -> Case arg_tag (PrimAlts [(prim_arg, body)] NoDefault)
- )
-#endif
-\end{code}
-\begin{code}
- -- ... continued from above ....
| otherwise
= pprPanic "unboxArg: " (ppr PprDebug arg_ty)
where
data_con_arg_tys = dataConArgTys the_data_con tycon_arg_tys
(data_con_arg_ty1 : data_con_arg_ty2 : _) = data_con_arg_tys
+ maybe_arg2_tycon = maybeAppTyCon data_con_arg_ty2
+ Just (arg2_tycon,_) = maybe_arg2_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])
+ (hcat [text thing, text "; type: ", ppr (PprForUser opt_PprUserLength) 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 :: Id -- IOok constructor
+ -> 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
-boxResult result_ty
+ -- to box the result
+boxResult ioOkDataCon 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 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
=
- newSysLocalDs realWorldStatePrimTy `thenDs` \ prim_state_id ->
- newSysLocalDs the_prim_result_ty `thenDs` \ prim_result_id ->
+ 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 the_data_con (map TyArg tycon_arg_tys ++ [VarArg (Var prim_result_id)]) `thenDs` \ the_result ->
- mkConDs tuple_con_2
- [result_ty, realWorldStateTy]
- [the_result, new_state] `thenDs` \ the_pair ->
+ 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)
in
(null other_data_cons) && -- Just one constr
(null data_con_arg_tys)
=
- newSysLocalDs realWorldStatePrimTy `thenDs` \ prim_state_id ->
-
- mkConDs stateDataCon [realWorldTy] [Var prim_state_id] `thenDs` \ new_state ->
+ newSysLocalDs realWorldStatePrimTy `thenDs` \ prim_state_id ->
- mkConDs tuple_con_2
- [result_ty, realWorldStateTy]
- [covar_tuple_con_0, new_state] `thenDs` \ the_pair ->
+ mkConDs ioOkDataCon
+ [TyArg result_ty, VarArg (Var prim_state_id), VarArg (Var unitDataCon)]
+ `thenDs` \ the_pair ->
let
the_alt = (stateDataCon, [prim_state_id], the_pair)
\prim_app -> Case prim_app (AlgAlts [the_alt] NoDefault)
)
-#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 ->
-
- mkConDs stateDataCon [realWorldTy] [Var prim_state_id] `thenDs` \ new_state ->
-
- let
- alts = [ (mkMachInt i, con) | (i, con) <- [0..] `zip` data_cons ]
- the_result = Case prim_result_id (PrimAlts alts) NoDefault
- in
-
- 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)
- in
- returnDs (state_and_prim_ty,
- \prim_app -> Case prim_app (AlgAlts [the_alt] NoDefault)
- )
-#endif
-
| otherwise
= pprPanic "boxResult: " (ppr PprDebug result_ty)
(state_and_prim_datacon, state_and_prim_ty) = getStatePairingConInfo the_prim_result_ty
\end{code}
+This grimy bit of code is for digging out the IOok constructor from an
+application of the the IO type. The constructor is needed for
+wrapping the result of a _ccall_. The alternative is to wire-in IO,
+which brings a whole heap of junk with it.
+
+If the representation of IO changes, this will probably have to be
+brought in line with the new definition.
+
+newtype IO a = IO (State# RealWorld -> IOResult a)
+
+the constructor IO has type (State# RealWorld -> IOResult a) -> IO a
+
+\begin{code}
+getIoOkDataCon :: Type -> (Id,Type)
+getIoOkDataCon io_result_ty =
+ let
+ AppTy (TyConTy ioTyCon _) result_ty = io_result_ty
+ [ioDataCon] = tyConDataCons ioTyCon
+ ioDataConTy = idType ioDataCon
+ (_,ioDataConTy') = splitForAllTy ioDataConTy
+ ([arg],_) = splitFunTy ioDataConTy'
+ (_,AppTy (TyConTy ioResultTyCon _) _) = splitFunTy arg
+ [ioOkDataCon,ioFailDataCon] = tyConDataCons ioResultTyCon
+ in
+ (ioOkDataCon, result_ty)
+
+\end{code}
+
+Another way to do it, more sensitive:
+
+ case ioDataConTy of
+ ForAll _ (FunTy (FunTy _ (AppTy (TyConTy ioResultTyCon _) _)) _) ->
+ let [ioOkDataCon,ioFailDataCon] = tyConDataCons ioResultTyCon
+ in
+ (ioOkDataCon, result_ty)
+ _ -> pprPanic "getIoOkDataCon: " (ppr PprDebug ioDataConTy)