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 )
27 import TysPrim ( byteArrayPrimTy, realWorldTy, realWorldStatePrimTy )
28 import TysWiredIn ( getStatePairingConInfo,
29 realWorldStateTy, stateDataCon,
32 import Util ( pprPanic, pprError, panic )
34 maybeBoxedPrimType = panic "DsCCall.maybeBoxedPrimType"
37 Desugaring of @ccall@s consists of adding some state manipulation,
38 unboxing any boxed primitive arguments and boxing the result if
41 The state stuff just consists of adding in
42 @\ s -> case s of { S# s# -> ... }@ in an appropriate place.
44 The unboxing is straightforward, as all information needed to unbox is
45 available from the type. For each boxed-primitive argument, we
48 _ccall_ foo [ r, t1, ... tm ] e1 ... em
52 case e1 of { T1# x1# ->
54 case em of { Tm# xm# -> xm#
55 ccall# foo [ r, t1#, ... tm# ] x1# ... xm#
59 The reboxing of a @_ccall_@ result is a bit tricker: the types don't
60 contain information about the state-pairing functions so we have to
61 keep a list of \tr{(type, s-p-function)} pairs. We transform as
64 ccall# foo [ r, t1#, ... tm# ] e1# ... em#
68 \ s# -> case (ccall# foo [ r, t1#, ... tm# ] s# e1# ... em#) of
69 (StateAnd<r># result# state#) -> (R# result#, realWorld#)
73 dsCCall :: FAST_STRING -- C routine to invoke
74 -> [CoreExpr] -- Arguments (desugared)
75 -> Bool -- True <=> might cause Haskell GC
76 -> Bool -- True <=> really a "_casm_"
77 -> Type -- Type of the result (a boxed-prim type)
80 dsCCall label args may_gc is_asm result_ty
81 = newSysLocalDs realWorldStateTy `thenDs` \ old_s ->
83 mapAndUnzipDs unboxArg (Var old_s : args) `thenDs` \ (final_args, arg_wrappers) ->
85 boxResult result_ty `thenDs` \ (final_result_ty, res_wrapper) ->
88 the_ccall_op = CCallOp label is_asm may_gc
89 (map coreExprType final_args)
93 [] -- ***NOTE*** no ty apps; the types are inside the_ccall_op.
94 final_args `thenDs` \ the_prim_app ->
96 the_body = foldr apply (res_wrapper the_prim_app) arg_wrappers
98 returnDs (Lam (ValBinder old_s) the_body)
104 unboxArg :: CoreExpr -- The supplied argument
105 -> DsM (CoreExpr, -- To pass as the actual argument
106 CoreExpr -> CoreExpr -- Wrapper to unbox the arg
111 -- ADR Question: can this ever be used? None of the PrimTypes are
112 -- instances of the CCallable class.
114 = returnDs (arg, \body -> body)
117 | arg_ty `eqTy` stringTy
118 -- ToDo (ADR): - allow synonyms of Strings too?
119 = newSysLocalDs byteArrayPrimTy `thenDs` \ prim_arg ->
120 mkAppDs (Var packStringForCId) [] [arg] `thenDs` \ pack_appn ->
121 returnDs (Var prim_arg,
122 \body -> Case pack_appn (PrimAlts []
123 (BindDefault prim_arg body))
127 -- oops: we can't see the data constructors!!!
128 = can't_see_datacons_error "argument" arg_ty
130 -- Byte-arrays, both mutable and otherwise
131 -- (HACKy method -- but we really don't want the TyCons wired-in...) [WDP 94/10]
133 length data_con_arg_tys == 2 &&
134 not (isPrimType data_con_arg_ty1) &&
135 isPrimType data_con_arg_ty2
136 -- and, of course, it is an instance of CCallable
137 -- ( tycon == byteArrayTyCon ||
138 -- tycon == mutableByteArrayTyCon )
139 = newSysLocalsDs data_con_arg_tys `thenDs` \ vars@[ixs_var, arr_cts_var] ->
140 returnDs (Var arr_cts_var,
141 \ body -> Case arg (AlgAlts [(the_data_con,vars,body)]
145 -- Data types with a single constructor, which has a single, primitive-typed arg
146 | maybeToBool maybe_boxed_prim_arg_ty
147 = newSysLocalDs the_prim_arg_ty `thenDs` \ prim_arg ->
148 returnDs (Var prim_arg,
149 \ body -> Case arg (AlgAlts [(box_data_con,[prim_arg],body)]
154 = pprPanic "unboxArg: " (ppr PprDebug arg_ty)
156 arg_ty = coreExprType arg
158 maybe_boxed_prim_arg_ty = maybeBoxedPrimType arg_ty
159 (Just (box_data_con, the_prim_arg_ty)) = maybe_boxed_prim_arg_ty
161 maybe_data_type = maybeAppDataTyConExpandingDicts arg_ty
162 is_data_type = maybeToBool maybe_data_type
163 (Just (tycon, tycon_arg_tys, data_cons)) = maybe_data_type
164 (the_data_con : other_data_cons) = data_cons
166 data_con_arg_tys = dataConArgTys the_data_con tycon_arg_tys
167 (data_con_arg_ty1 : data_con_arg_ty2 : _) = data_con_arg_tys
169 can't_see_datacons_error thing ty
170 = pprError "ERROR: Can't see the data constructor(s) for _ccall_/_casm_ "
171 (ppBesides [ppStr thing, ppStr "; type: ", ppr PprForUser ty])
176 tuple_con_2 = mkTupleCon 2 -- out here to avoid CAF (sigh)
177 covar_tuple_con_0 = Var (mkTupleCon 0) -- ditto
179 boxResult :: Type -- Type of desired result
180 -> DsM (Type, -- Type of the result of the ccall itself
181 CoreExpr -> CoreExpr) -- Wrapper for the ccall
185 -- oops! can't see the data constructors
186 = can't_see_datacons_error "result" result_ty
188 -- Data types with a single constructor, which has a single, primitive-typed arg
189 | (maybeToBool maybe_data_type) && -- Data type
190 (null other_data_cons) && -- Just one constr
191 not (null data_con_arg_tys) && null other_args_tys && -- Just one arg
192 isPrimType the_prim_result_ty -- of primitive type
194 newSysLocalDs realWorldStatePrimTy `thenDs` \ prim_state_id ->
195 newSysLocalDs the_prim_result_ty `thenDs` \ prim_result_id ->
197 mkConDs stateDataCon [realWorldTy] [Var prim_state_id] `thenDs` \ new_state ->
198 mkConDs the_data_con tycon_arg_tys [Var prim_result_id] `thenDs` \ the_result ->
201 [result_ty, realWorldStateTy]
202 [the_result, new_state] `thenDs` \ the_pair ->
204 the_alt = (state_and_prim_datacon, [prim_state_id, prim_result_id], the_pair)
206 returnDs (state_and_prim_ty,
207 \prim_app -> Case prim_app (AlgAlts [the_alt] NoDefault)
210 -- Data types with a single nullary constructor
211 | (maybeToBool maybe_data_type) && -- Data type
212 (null other_data_cons) && -- Just one constr
213 (null data_con_arg_tys)
215 newSysLocalDs realWorldStatePrimTy `thenDs` \ prim_state_id ->
217 mkConDs stateDataCon [realWorldTy] [Var prim_state_id] `thenDs` \ new_state ->
220 [result_ty, realWorldStateTy]
221 [covar_tuple_con_0, new_state] `thenDs` \ the_pair ->
224 the_alt = (stateDataCon, [prim_state_id], the_pair)
226 returnDs (realWorldStateTy,
227 \prim_app -> Case prim_app (AlgAlts [the_alt] NoDefault)
231 = pprPanic "boxResult: " (ppr PprDebug result_ty)
234 maybe_data_type = maybeAppDataTyConExpandingDicts result_ty
235 Just (tycon, tycon_arg_tys, data_cons) = maybe_data_type
236 (the_data_con : other_data_cons) = data_cons
238 data_con_arg_tys = dataConArgTys the_data_con tycon_arg_tys
239 (the_prim_result_ty : other_args_tys) = data_con_arg_tys
241 (state_and_prim_datacon, state_and_prim_ty) = getStatePairingConInfo the_prim_result_ty