ForeignImport(..), CImportSpec(..) )
import TcHsSyn ( TypecheckedForeignDecl )
import CoreUtils ( exprType, mkInlineMe )
-import Id ( Id, idType, idName, mkVanillaGlobal, mkSysLocal,
- setInlinePragma )
-import IdInfo ( vanillaIdInfo )
+import Id ( Id, idType, idName, mkSysLocal, setInlinePragma )
import Literal ( Literal(..) )
import Module ( Module, moduleUserString )
-import Name ( mkGlobalName, nameModule, nameOccName, getOccString,
- mkForeignExportOcc, isLocalName,
- NamedThing(..),
- )
+import Name ( getOccString, NamedThing(..) )
+import OccName ( encodeFS )
import Type ( repType, eqType )
import TcType ( Type, mkFunTys, mkForAllTys, mkTyConApp,
- mkFunTy, applyTy,
+ mkFunTy, tcSplitTyConApp_maybe,
tcSplitForAllTys, tcSplitFunTys, tcTyConAppArgs,
- tcSplitTyConApp_maybe, tcSplitAppTy,
- tcFunResultTy
)
import ForeignCall ( ForeignCall(..), CCallSpec(..),
Safety(..), playSafe,
CExportSpec(..),
- CCallConv(..), ccallConvToInt
+ CCallConv(..), ccallConvToInt,
+ ccallConvAttribute
)
import CStrings ( CLabelString )
-import TysWiredIn ( addrTy, unitTy, stablePtrTyCon )
+import TysWiredIn ( unitTy, stablePtrTyCon )
import TysPrim ( addrPrimTy )
-import PrelNames ( hasKey, ioTyConKey, deRefStablePtrName, newStablePtrName,
- bindIOName, returnIOName
- )
+import PrelNames ( hasKey, ioTyConKey, newStablePtrName, bindIOName )
import BasicTypes ( Activation( NeverActive ) )
import ErrUtils ( addShortWarnLocLine )
import Outputable
combine (acc_feb, acc_f, acc_h, acc_c, acc_header)
(ForeignExport id _ (CExport (CExportStatic ext_nm cconv)) depr loc)
= dsFExport mod_name id (idType id)
- ext_nm cconv False `thenDs` \(feb, b, h, c) ->
+ ext_nm cconv False `thenDs` \(h, c) ->
warnDepr depr loc `thenDs` \_ ->
- returnDs (feb:acc_feb, b : acc_f, h $$ acc_h, c $$ acc_c, acc_header)
+ returnDs (acc_feb, acc_f, h $$ acc_h, c $$ acc_c, acc_header)
warnDepr False _ = returnDs ()
warnDepr True loc = dsWarn (addShortWarnLocLine loc msg)
worker_ty = mkForAllTys tvs (mkFunTys (map idType work_arg_ids) ccall_result_ty)
the_ccall_app = mkFCall ccall_uniq fcall val_args ccall_result_ty
work_rhs = mkLams tvs (mkLams work_arg_ids the_ccall_app)
- work_id = mkSysLocal SLIT("$wccall") work_uniq worker_ty
+ work_id = mkSysLocal (encodeFS SLIT("$wccall")) work_uniq worker_ty
-- Build the wrapper
work_app = mkApps (mkVarApps (Var work_id) tvs) val_args
-> Bool -- True => foreign export dynamic
-- so invoke IO action that's hanging off
-- the first argument's stable pointer
- -> DsM ( Id -- The foreign-exported Id
- , Binding
- , SDoc
- , SDoc
+ -> DsM ( SDoc -- contents of Module_stub.h
+ , SDoc -- contents of Module_stub.c
)
+
dsFExport mod_name fn_id ty ext_name cconv isDyn
- = -- BUILD THE returnIO WRAPPER, if necessary
+ =
+ let
+ (tvs,sans_foralls) = tcSplitForAllTys ty
+ (fe_arg_tys', orig_res_ty) = tcSplitFunTys sans_foralls
+ -- We must use tcSplits here, because we want to see
+ -- the (IO t) in the corner of the type!
+ fe_arg_tys | isDyn = tail fe_arg_tys'
+ | otherwise = fe_arg_tys'
+ in
-- Look at the result type of the exported function, orig_res_ty
- -- If it's IO t, return (\x.x, IO t, t)
- -- If it's plain t, return (\x.returnIO x, IO t, t)
+ -- If it's IO t, return (t, True)
+ -- If it's plain t, return (t, False)
(case tcSplitTyConApp_maybe orig_res_ty of
-- We must use tcSplit here so that we see the (IO t) in
-- the type. [IO t is transparent to plain splitTyConApp.]
Just (ioTyCon, [res_ty])
-> ASSERT( ioTyCon `hasKey` ioTyConKey )
- -- The function already returns IO t
- returnDs (\body -> body, orig_res_ty, res_ty)
-
- other -> -- The function returns t, so wrap the call in returnIO
- dsLookupGlobalValue returnIOName `thenDs` \ retIOId ->
- returnDs (\body -> mkApps (Var retIOId) [Type orig_res_ty, body],
- tcFunResultTy (applyTy (idType retIOId) orig_res_ty),
- -- We don't have ioTyCon conveniently to hand
- orig_res_ty)
-
- ) `thenDs` \ (return_io_wrapper, -- Either identity or returnIO
- io_res_ty, -- IO t
- res_ty) -> -- t
-
-
- -- BUILD THE deRefStablePtr WRAPPER, if necessary
- (if isDyn then
- newSysLocalDs stbl_ptr_ty `thenDs` \ stbl_ptr ->
- newSysLocalDs stbl_ptr_to_ty `thenDs` \ stbl_value ->
- dsLookupGlobalValue deRefStablePtrName `thenDs` \ deRefStablePtrId ->
- dsLookupGlobalValue bindIOName `thenDs` \ bindIOId ->
- let
- the_deref_app = mkApps (Var deRefStablePtrId)
- [ Type stbl_ptr_to_ty, Var stbl_ptr ]
-
- stbl_app cont = mkApps (Var bindIOId)
- [ Type stbl_ptr_to_ty
- , Type res_ty
- , the_deref_app
- , mkLams [stbl_value] cont]
- in
- returnDs (stbl_value, stbl_app, stbl_ptr)
- else
- returnDs (fn_id,
- \ body -> body,
- panic "stbl_ptr" -- should never be touched.
- )) `thenDs` \ (i, getFun_wrapper, stbl_ptr) ->
-
-
- -- BUILD THE HELPER
- getModuleDs `thenDs` \ mod ->
- getUniqueDs `thenDs` \ uniq ->
- getSrcLocDs `thenDs` \ src_loc ->
- newSysLocalsDs fe_arg_tys `thenDs` \ fe_args ->
+ -- The function already returns IO t
+ returnDs (res_ty, True)
+
+ other -> -- The function returns t
+ returnDs (orig_res_ty, False)
+ )
+ `thenDs` \ (res_ty, -- t
+ is_IO_res_ty) -> -- Bool
+ getModuleDs
+ `thenDs` \ mod ->
let
- wrapper_args | isDyn = stbl_ptr:fe_args
- | otherwise = fe_args
-
- wrapper_arg_tys | isDyn = stbl_ptr_ty:fe_arg_tys
- | otherwise = fe_arg_tys
-
- helper_ty = mkForAllTys tvs $
- mkFunTys wrapper_arg_tys io_res_ty
-
- f_helper_glob = mkVanillaGlobal helper_name helper_ty vanillaIdInfo
- where
- name = idName fn_id
- mod
- | isLocalName name = mod_name
- | otherwise = nameModule name
-
- occ = mkForeignExportOcc (nameOccName name)
- helper_name = mkGlobalName uniq mod occ src_loc
-
- the_app = getFun_wrapper (return_io_wrapper (mkVarApps (Var i) (tvs ++ fe_args)))
- the_body = mkLams (tvs ++ wrapper_args) the_app
-
- (h_stub, c_stub) = fexportEntry (moduleUserString mod)
- ext_name f_helper_glob
- wrapper_arg_tys res_ty cconv isDyn
+ (h_stub, c_stub)
+ = mkFExportCBits (moduleUserString mod) ext_name
+ (if isDyn then Nothing else Just fn_id)
+ fe_arg_tys res_ty is_IO_res_ty cconv
in
- returnDs (f_helper_glob, (f_helper_glob, the_body), h_stub, c_stub)
-
- where
- (tvs,sans_foralls) = tcSplitForAllTys ty
- (fe_arg_tys', orig_res_ty) = tcSplitFunTys sans_foralls
- -- We must use tcSplits here, because we want to see
- -- the (IO t) in the corner of the type!
-
- fe_arg_tys | isDyn = tail fe_arg_tys'
- | otherwise = fe_arg_tys'
-
- stbl_ptr_ty | isDyn = head fe_arg_tys'
- | otherwise = error "stbl_ptr_ty"
-
- (_, stbl_ptr_ty') = tcSplitForAllTys stbl_ptr_ty
- (_, stbl_ptr_to_ty) = tcSplitAppTy stbl_ptr_ty'
- -- Again, stable pointers are just newtypes,
- -- so we must see them! Hence tcSplit*
+ returnDs (h_stub, c_stub)
\end{code}
@foreign export dynamic@ lets you dress up Haskell IO actions
-- hack: need to get at the name of the C stub we're about to generate.
fe_nm = _PK_ (moduleUserString mod_name ++ "_" ++ toCName fe_id)
in
- dsFExport mod_name id export_ty fe_nm cconv True `thenDs` \ (feb, fe, h_code, c_code) ->
+ dsFExport mod_name id export_ty fe_nm cconv True `thenDs` \ (h_code, c_code) ->
newSysLocalDs arg_ty `thenDs` \ cback ->
dsLookupGlobalValue newStablePtrName `thenDs` \ newStablePtrId ->
let
-- Never inline the f.e.d. function, because the litlit
-- might not be in scope in other modules.
in
- returnDs ([fed, fe], h_code, c_code)
+ returnDs ([fed], h_code, c_code)
where
ty = idType id
using the hugs/ghc rts invocation API.
\begin{code}
-fexportEntry :: String
- -> FAST_STRING
- -> Id
- -> [Type]
- -> Type
- -> CCallConv
- -> Bool
- -> (SDoc, SDoc)
-fexportEntry mod_nm c_nm helper args res_ty cc isDyn = (header_bits, c_bits)
+mkFExportCBits :: String
+ -> FAST_STRING
+ -> Maybe Id -- Just==static, Nothing==dynamic
+ -> [Type]
+ -> Type
+ -> Bool -- True <=> returns an IO type
+ -> CCallConv
+ -> (SDoc, SDoc)
+mkFExportCBits mod_nm c_nm maybe_target arg_htys res_hty is_IO_res_ty cc
+ = (header_bits, c_bits)
where
- -- name of the (Haskell) helper function generated by the desugarer.
- h_nm = ppr helper <> text "_closure"
- -- prototype for the exported function.
+ -- Create up types and names for the real args
+ arg_cnames, arg_ctys :: [SDoc]
+ arg_cnames = mkCArgNames 1 arg_htys
+ arg_ctys = map showStgType arg_htys
+
+ -- and also for auxiliary ones; the stable ptr in the dynamic case, and
+ -- a slot for the dummy return address in the dynamic + ccall case
+ extra_cnames_and_ctys
+ = case maybe_target of
+ Nothing -> [(text "the_stableptr", text "StgStablePtr")]
+ other -> []
+ ++
+ case (maybe_target, cc) of
+ (Nothing, CCallConv) -> [(text "original_return_addr", text "void*")]
+ other -> []
+
+ all_cnames_and_ctys :: [(SDoc, SDoc)]
+ all_cnames_and_ctys
+ = extra_cnames_and_ctys ++ zip arg_cnames arg_ctys
+
+ -- stuff to do with the return type of the C function
+ res_hty_is_unit = res_hty `eqType` unitTy -- Look through any newtypes
+
+ cResType | res_hty_is_unit = text "void"
+ | otherwise = showStgType res_hty
+
+ -- Now we can cook up the prototype for the exported function.
+ pprCconv = case cc of
+ CCallConv -> empty
+ StdCallConv -> text (ccallConvAttribute cc)
+
header_bits = ptext SLIT("extern") <+> fun_proto <> semi
fun_proto = cResType <+> pprCconv <+> ptext c_nm <>
- parens (hsep (punctuate comma (zipWith (<+>) cParamTypes proto_args)))
-
+ parens (hsep (punctuate comma (map (\(nm,ty) -> ty <+> nm)
+ all_cnames_and_ctys)))
+
+ -- the target which will form the root of what we ask rts_evalIO to run
+ the_cfun
+ = case maybe_target of
+ Nothing -> text "(StgClosure*)deRefStablePtr(the_stableptr)"
+ Just hs_fn -> char '&' <> ppr hs_fn <> text "_closure"
+
+ -- the expression we give to rts_evalIO
+ expr_to_run
+ = foldl appArg the_cfun (zip arg_cnames arg_htys)
+ where
+ appArg acc (arg_cname, arg_hty)
+ = text "rts_apply"
+ <> parens (acc <> comma <> mkHObj arg_hty <> parens arg_cname)
+
+ -- various other bits for inside the fn
+ declareResult = text "HaskellObj ret;"
+
+ return_what | res_hty_is_unit = empty
+ | otherwise = parens (unpackHObj res_hty <> parens (text "ret"))
+
+ -- an extern decl for the fn being called
+ extern_decl
+ = case maybe_target of
+ Nothing -> empty
+ Just hs_fn -> text "extern StgClosure " <> ppr hs_fn <> text "_closure" <> semi
+
+ -- finally, the whole darn thing
c_bits =
- externDecl $$
+ space $$
+ extern_decl $$
fun_proto $$
vcat
[ lbrace
, text "SchedulerStatus rc;"
, declareResult
-- create the application + perform it.
- , text "rc=rts_evalIO" <>
- parens (foldl appArg (text "(StgClosure*)&" <> h_nm) (zip args c_args) <> comma <> text "&ret") <> semi
+ , text (if is_IO_res_ty then "rc=rts_evalIO" else "rc=rts_eval")
+ <> parens (expr_to_run <+> comma <> text "&ret")
+ <> semi
, text "rts_checkSchedStatus" <> parens (doubleQuotes (ptext c_nm)
<> comma <> text "rc") <> semi
, text "return" <> return_what <> semi
, rbrace
]
- appArg acc (a,c_a) =
- text "rts_apply" <> parens (acc <> comma <> mkHObj a <> parens c_a)
-
- cParamTypes = map showStgType real_args
-
- res_ty_is_unit = res_ty `eqType` unitTy -- Look through any newtypes
-
- cResType | res_ty_is_unit = text "void"
- | otherwise = showStgType res_ty
-
- pprCconv = case cc of
- CCallConv -> empty
- StdCallConv -> ppr cc
-
- declareResult = text "HaskellObj ret;"
-
- externDecl = mkExtern (text "HaskellObj") h_nm
-
- mkExtern ty nm = text "extern" <+> ty <+> nm <> semi
-
- return_what | res_ty_is_unit = empty
- | otherwise = parens (unpackHObj res_ty <> parens (text "ret"))
-
- c_args = mkCArgNames 0 args
-
- {-
- If we're generating an entry point for a 'foreign export ccall dynamic',
- then we receive the return address of the C function that wants to
- invoke a Haskell function as any other C function, as second arg.
- This arg is unused within the body of the generated C stub, but
- needed by the Adjustor.c code to get the stack cleanup right.
- -}
- (proto_args, real_args)
- = case cc of
- CCallConv | isDyn -> ( text "a0" : text "a_" : mkCArgNames 1 (tail args)
- , head args : addrTy : tail args)
- other -> (mkCArgNames 0 args, args)
mkCArgNames :: Int -> [a] -> [SDoc]
mkCArgNames n as = zipWith (\ _ n -> text ('a':show n)) as [n..]