2 % (c) The AQUA Project, Glasgow University, 1994-1996
4 \section[DsCCall]{Desugaring \tr{_ccall_}s and \tr{_casm_}s}
7 #include "HsVersions.h"
9 module DsCCall ( dsCCall ) where
18 import CoreUtils ( coreExprType )
19 import Id ( dataConArgTys, mkTupleCon )
20 import Maybes ( maybeToBool )
21 import PprStyle ( PprStyle(..) )
22 import PprType ( GenType{-instances-} )
24 import PrelVals ( packStringForCId )
25 import PrimOp ( PrimOp(..) )
26 import Type ( isPrimType, maybeAppDataTyConExpandingDicts, eqTy, maybeBoxedPrimType )
27 import TysPrim ( byteArrayPrimTy, realWorldTy, realWorldStatePrimTy )
28 import TysWiredIn ( getStatePairingConInfo,
29 realWorldStateTy, stateDataCon,
32 import Util ( pprPanic, pprError, panic )
35 Desugaring of @ccall@s consists of adding some state manipulation,
36 unboxing any boxed primitive arguments and boxing the result if
39 The state stuff just consists of adding in
40 @PrimIO (\ s -> case s of { S# s# -> ... })@ in an appropriate place.
42 The unboxing is straightforward, as all information needed to unbox is
43 available from the type. For each boxed-primitive argument, we
46 _ccall_ foo [ r, t1, ... tm ] e1 ... em
50 case e1 of { T1# x1# ->
52 case em of { Tm# xm# -> xm#
53 ccall# foo [ r, t1#, ... tm# ] x1# ... xm#
57 The reboxing of a @_ccall_@ result is a bit tricker: the types don't
58 contain information about the state-pairing functions so we have to
59 keep a list of \tr{(type, s-p-function)} pairs. We transform as
62 ccall# foo [ r, t1#, ... tm# ] e1# ... em#
66 \ s# -> case (ccall# foo [ r, t1#, ... tm# ] s# e1# ... em#) of
67 (StateAnd<r># result# state#) -> (R# result#, realWorld#)
71 dsCCall :: FAST_STRING -- C routine to invoke
72 -> [CoreExpr] -- Arguments (desugared)
73 -> Bool -- True <=> might cause Haskell GC
74 -> Bool -- True <=> really a "_casm_"
75 -> Type -- Type of the result (a boxed-prim type)
78 dsCCall label args may_gc is_asm result_ty
79 = newSysLocalDs realWorldStateTy `thenDs` \ old_s ->
81 mapAndUnzipDs unboxArg (Var old_s : args) `thenDs` \ (final_args, arg_wrappers) ->
83 boxResult result_ty `thenDs` \ (final_result_ty, res_wrapper) ->
86 the_ccall_op = CCallOp label is_asm may_gc
87 (map coreExprType final_args)
90 mkPrimDs the_ccall_op (map VarArg final_args) `thenDs` \ the_prim_app ->
92 the_body = foldr ($) (res_wrapper the_prim_app) arg_wrappers
94 returnDs (Lam (ValBinder old_s) the_body)
98 unboxArg :: CoreExpr -- The supplied argument
99 -> DsM (CoreExpr, -- To pass as the actual argument
100 CoreExpr -> CoreExpr -- Wrapper to unbox the arg
105 -- ADR Question: can this ever be used? None of the PrimTypes are
106 -- instances of the CCallable class.
108 = returnDs (arg, \body -> body)
111 | arg_ty `eqTy` stringTy
112 -- ToDo (ADR): - allow synonyms of Strings too?
113 = newSysLocalDs byteArrayPrimTy `thenDs` \ prim_arg ->
114 mkAppDs (Var packStringForCId) [VarArg arg] `thenDs` \ pack_appn ->
115 returnDs (Var prim_arg,
116 \body -> Case pack_appn (PrimAlts []
117 (BindDefault prim_arg body))
121 -- oops: we can't see the data constructors!!!
122 = can't_see_datacons_error "argument" arg_ty
124 -- Byte-arrays, both mutable and otherwise
125 -- (HACKy method -- but we really don't want the TyCons wired-in...) [WDP 94/10]
127 length data_con_arg_tys == 2 &&
128 not (isPrimType data_con_arg_ty1) &&
129 isPrimType data_con_arg_ty2
130 -- and, of course, it is an instance of CCallable
131 -- ( tycon == byteArrayTyCon ||
132 -- tycon == mutableByteArrayTyCon )
133 = newSysLocalsDs data_con_arg_tys `thenDs` \ vars@[ixs_var, arr_cts_var] ->
134 returnDs (Var arr_cts_var,
135 \ body -> Case arg (AlgAlts [(the_data_con,vars,body)]
139 -- Data types with a single constructor, which has a single, primitive-typed arg
140 | maybeToBool maybe_boxed_prim_arg_ty
141 = newSysLocalDs the_prim_arg_ty `thenDs` \ prim_arg ->
142 returnDs (Var prim_arg,
143 \ body -> Case arg (AlgAlts [(box_data_con,[prim_arg],body)]
148 = pprPanic "unboxArg: " (ppr PprDebug arg_ty)
150 arg_ty = coreExprType arg
152 maybe_boxed_prim_arg_ty = maybeBoxedPrimType arg_ty
153 (Just (box_data_con, the_prim_arg_ty)) = maybe_boxed_prim_arg_ty
155 maybe_data_type = maybeAppDataTyConExpandingDicts arg_ty
156 is_data_type = maybeToBool maybe_data_type
157 (Just (tycon, tycon_arg_tys, data_cons)) = maybe_data_type
158 (the_data_con : other_data_cons) = data_cons
160 data_con_arg_tys = dataConArgTys the_data_con tycon_arg_tys
161 (data_con_arg_ty1 : data_con_arg_ty2 : _) = data_con_arg_tys
163 can't_see_datacons_error thing ty
164 = pprError "ERROR: Can't see the data constructor(s) for _ccall_/_casm_ "
165 (ppBesides [ppStr thing, ppStr "; type: ", ppr PprForUser ty])
170 tuple_con_2 = mkTupleCon 2 -- out here to avoid CAF (sigh)
171 covar_tuple_con_0 = Var (mkTupleCon 0) -- ditto
173 boxResult :: Type -- Type of desired result
174 -> DsM (Type, -- Type of the result of the ccall itself
175 CoreExpr -> CoreExpr) -- Wrapper for the ccall
179 -- oops! can't see the data constructors
180 = can't_see_datacons_error "result" result_ty
182 -- Data types with a single constructor, which has a single, primitive-typed arg
183 | (maybeToBool maybe_data_type) && -- Data type
184 (null other_data_cons) && -- Just one constr
185 not (null data_con_arg_tys) && null other_args_tys && -- Just one arg
186 isPrimType the_prim_result_ty -- of primitive type
188 newSysLocalDs realWorldStatePrimTy `thenDs` \ prim_state_id ->
189 newSysLocalDs the_prim_result_ty `thenDs` \ prim_result_id ->
191 mkConDs stateDataCon [TyArg realWorldTy, VarArg (Var prim_state_id)] `thenDs` \ new_state ->
192 mkConDs the_data_con (map TyArg tycon_arg_tys ++ [VarArg (Var prim_result_id)]) `thenDs` \ the_result ->
195 [TyArg result_ty, TyArg realWorldStateTy, VarArg the_result, VarArg new_state]
196 `thenDs` \ the_pair ->
198 the_alt = (state_and_prim_datacon, [prim_state_id, prim_result_id], the_pair)
200 returnDs (state_and_prim_ty,
201 \prim_app -> Case prim_app (AlgAlts [the_alt] NoDefault)
204 -- Data types with a single nullary constructor
205 | (maybeToBool maybe_data_type) && -- Data type
206 (null other_data_cons) && -- Just one constr
207 (null data_con_arg_tys)
209 newSysLocalDs realWorldStatePrimTy `thenDs` \ prim_state_id ->
211 mkConDs stateDataCon [TyArg realWorldTy, VarArg (Var prim_state_id)]
212 `thenDs` \ new_state ->
214 [TyArg result_ty, TyArg realWorldStateTy, VarArg covar_tuple_con_0, VarArg new_state]
215 `thenDs` \ the_pair ->
218 the_alt = (stateDataCon, [prim_state_id], the_pair)
220 returnDs (realWorldStateTy,
221 \prim_app -> Case prim_app (AlgAlts [the_alt] NoDefault)
225 = pprPanic "boxResult: " (ppr PprDebug result_ty)
228 maybe_data_type = maybeAppDataTyConExpandingDicts result_ty
229 Just (tycon, tycon_arg_tys, data_cons) = maybe_data_type
230 (the_data_con : other_data_cons) = data_cons
232 data_con_arg_tys = dataConArgTys the_data_con tycon_arg_tys
233 (the_prim_result_ty : other_args_tys) = data_con_arg_tys
235 (state_and_prim_datacon, state_and_prim_ty) = getStatePairingConInfo the_prim_result_ty