X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2FdeSugar%2FDsForeign.lhs;h=05dcb05221ee9649f71a56e643fb188c79b378f0;hb=550421384b8364cdaf3135f7859c9f7d7ee1fff1;hp=4074d0453ff56fa6af776a1630191de1fe4ed2a1;hpb=af13609607da81e7837a7c7c598de82452363ab5;p=ghc-hetmet.git diff --git a/ghc/compiler/deSugar/DsForeign.lhs b/ghc/compiler/deSugar/DsForeign.lhs index 4074d04..05dcb05 100644 --- a/ghc/compiler/deSugar/DsForeign.lhs +++ b/ghc/compiler/deSugar/DsForeign.lhs @@ -9,27 +9,28 @@ Expanding out @foreign import@ and @foreign export@ declarations. module DsForeign ( dsForeigns ) where #include "HsVersions.h" +import TcRnMonad -- temp import CoreSyn import DsCCall ( dsCCall, mkFCall, boxResult, unboxArg, resultWrapper ) import DsMonad -import HsSyn ( ForeignDecl(..), ForeignExport(..), +import HsSyn ( ForeignDecl(..), ForeignExport(..), LForeignDecl, ForeignImport(..), CImportSpec(..) ) -import TcHsSyn ( TypecheckedForeignDecl ) import CoreUtils ( exprType, mkInlineMe ) import Id ( Id, idType, idName, mkSysLocal, setInlinePragma ) import Literal ( Literal(..) ) import Module ( moduleString ) import Name ( getOccString, NamedThing(..) ) import OccName ( encodeFS ) -import Type ( repType, eqType ) +import Type ( repType, eqType, typePrimRep ) import TcType ( Type, mkFunTys, mkForAllTys, mkTyConApp, mkFunTy, tcSplitTyConApp_maybe, tcSplitForAllTys, tcSplitFunTys, tcTyConAppArgs, ) +import BasicTypes ( Boxity(..) ) import HscTypes ( ForeignStubs(..) ) import ForeignCall ( ForeignCall(..), CCallSpec(..), Safety(..), playSafe, @@ -38,10 +39,13 @@ import ForeignCall ( ForeignCall(..), CCallSpec(..), ccallConvAttribute ) import CStrings ( CLabelString ) -import TysWiredIn ( unitTy, stablePtrTyCon ) -import TysPrim ( addrPrimTy ) -import PrelNames ( hasKey, ioTyConKey, newStablePtrName, bindIOName ) +import TysWiredIn ( unitTy, tupleTyCon ) +import TysPrim ( addrPrimTy, mkStablePtrPrimTy, alphaTy ) +import PrimRep ( getPrimRepSizeInBytes ) +import PrelNames ( hasKey, ioTyConKey, stablePtrTyConName, newStablePtrName, bindIOName, + checkDotnetResName ) import BasicTypes ( Activation( NeverActive ) ) +import SrcLoc ( Located(..), unLoc ) import Outputable import Maybe ( fromJust ) import FastString @@ -64,30 +68,42 @@ so we reuse the desugaring code in @DsCCall@ to deal with these. type Binding = (Id, CoreExpr) -- No rec/nonrec structure; -- the occurrence analyser will sort it all out -dsForeigns :: [TypecheckedForeignDecl] +dsForeigns :: [LForeignDecl Id] -> DsM (ForeignStubs, [Binding]) +dsForeigns [] + = returnDs (NoStubs, []) dsForeigns fos = foldlDs combine (ForeignStubs empty empty [] [], []) fos where combine (ForeignStubs acc_h acc_c acc_hdrs acc_feb, acc_f) - (ForeignImport id _ spec depr loc) - = dsFImport id spec `thenDs` \(bs, h, c, hd) -> - warnDepr depr loc `thenDs` \_ -> - returnDs (ForeignStubs (h $$ acc_h) (c $$ acc_c) (hd ++ acc_hdrs) acc_feb, + (L loc (ForeignImport id _ spec depr)) + = traceIf (text "fi start" <+> ppr id) `thenDs` \ _ -> + dsFImport (unLoc id) spec `thenDs` \ (bs, h, c, mbhd) -> + warnDepr depr loc `thenDs` \ _ -> + traceIf (text "fi end" <+> ppr id) `thenDs` \ _ -> + returnDs (ForeignStubs (h $$ acc_h) + (c $$ acc_c) + (addH mbhd acc_hdrs) + acc_feb, bs ++ acc_f) combine (ForeignStubs acc_h acc_c acc_hdrs acc_feb, acc_f) - (ForeignExport id _ (CExport (CExportStatic ext_nm cconv)) depr loc) + (L loc (ForeignExport (L _ id) _ (CExport (CExportStatic ext_nm cconv)) depr)) = dsFExport id (idType id) - ext_nm cconv False `thenDs` \(h, c) -> + ext_nm cconv False `thenDs` \(h, c, _) -> warnDepr depr loc `thenDs` \_ -> returnDs (ForeignStubs (h $$ acc_h) (c $$ acc_c) acc_hdrs (id:acc_feb), acc_f) + addH Nothing ls = ls + addH (Just e) ls + | e `elem` ls = ls + | otherwise = e:ls + warnDepr False _ = returnDs () warnDepr True loc = dsWarn (loc, msg) - where - msg = ptext SLIT("foreign declaration uses deprecated non-standard syntax") + where + msg = ptext SLIT("foreign declaration uses deprecated non-standard syntax") \end{code} @@ -118,10 +134,10 @@ because it exposes the boxing to the call site. \begin{code} dsFImport :: Id -> ForeignImport - -> DsM ([Binding], SDoc, SDoc, [FastString]) + -> DsM ([Binding], SDoc, SDoc, Maybe FastString) dsFImport id (CImport cconv safety header lib spec) = dsCImport id spec cconv safety no_hdrs `thenDs` \(ids, h, c) -> - returnDs (ids, h, c, if no_hdrs then [] else [header]) + returnDs (ids, h, c, if no_hdrs then Nothing else Just header) where no_hdrs = nullFastString header @@ -130,7 +146,7 @@ dsFImport id (CImport cconv safety header lib spec) -- support such calls yet; if `nullFastString lib', the value was not given dsFImport id (DNImport spec) = dsFCall id (DNCall spec) True {- No headers -} `thenDs` \(ids, h, c) -> - returnDs (ids, h, c, []) + returnDs (ids, h, c, Nothing) dsCImport :: Id -> CImportSpec @@ -139,11 +155,10 @@ dsCImport :: Id -> Bool -- True <=> no headers in the f.i decl -> DsM ([Binding], SDoc, SDoc) dsCImport id (CLabel cid) _ _ no_hdrs - = ASSERT(fromJust resTy `eqType` addrPrimTy) -- typechecker ensures this - returnDs ([(setImpInline no_hdrs id, rhs)], empty, empty) - where - (resTy, foRhs) = resultWrapper (idType id) - rhs = foRhs (mkLit (MachLabel cid)) + = resultWrapper (idType id) `thenDs` \ (resTy, foRhs) -> + ASSERT(fromJust resTy `eqType` addrPrimTy) -- typechecker ensures this + let rhs = foRhs (mkLit (MachLabel cid Nothing)) in + returnDs ([(setImpInline no_hdrs id, rhs)], empty, empty) dsCImport id (CFunction target) cconv safety no_hdrs = dsFCall id (CCall (CCallSpec target cconv safety)) no_hdrs dsCImport id CWrapper cconv _ _ @@ -193,11 +208,37 @@ dsFCall fn_id fcall no_hdrs -- ForeignObj#s live across a 'safe' foreign import). maybe_arg_ids | unsafe_call fcall = work_arg_ids | otherwise = [] + + forDotnet = + case fcall of + DNCall{} -> True + _ -> False + + topConDs + | forDotnet = + dsLookupGlobalId checkDotnetResName `thenDs` \ check_id -> + return (Just check_id) + | otherwise = return Nothing + + augmentResultDs + | forDotnet = + newSysLocalDs addrPrimTy `thenDs` \ err_res -> + returnDs (\ (mb_res_ty, resWrap) -> + case mb_res_ty of + Nothing -> (Just (mkTyConApp (tupleTyCon Unboxed 1) + [ addrPrimTy ]), + resWrap) + Just x -> (Just (mkTyConApp (tupleTyCon Unboxed 2) + [ x, addrPrimTy ]), + resWrap)) + | otherwise = returnDs id in - boxResult maybe_arg_ids io_res_ty `thenDs` \ (ccall_result_ty, res_wrapper) -> + augmentResultDs `thenDs` \ augment -> + topConDs `thenDs` \ topCon -> + boxResult maybe_arg_ids augment topCon io_res_ty `thenDs` \ (ccall_result_ty, res_wrapper) -> - getUniqueDs `thenDs` \ ccall_uniq -> - getUniqueDs `thenDs` \ work_uniq -> + newUnique `thenDs` \ ccall_uniq -> + newUnique `thenDs` \ work_uniq -> let -- Build the worker worker_ty = mkForAllTys tvs (mkFunTys (map idType work_arg_ids) ccall_result_ty) @@ -246,12 +287,13 @@ dsFExport :: Id -- Either the exported Id, -- the first argument's stable pointer -> DsM ( SDoc -- contents of Module_stub.h , SDoc -- contents of Module_stub.c + , [Type] -- arguments expected by stub function. ) dsFExport fn_id ty ext_name cconv isDyn = let - (tvs,sans_foralls) = tcSplitForAllTys ty + (_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! @@ -275,13 +317,10 @@ dsFExport fn_id ty ext_name cconv isDyn ) `thenDs` \ (res_ty, -- t is_IO_res_ty) -> -- Bool - let - (h_stub, c_stub) - = mkFExportCBits ext_name - (if isDyn then Nothing else Just fn_id) - fe_arg_tys res_ty is_IO_res_ty cconv - in - returnDs (h_stub, c_stub) + returnDs $ + mkFExportCBits ext_name + (if isDyn then Nothing else Just fn_id) + fe_arg_tys res_ty is_IO_res_ty cconv \end{code} @foreign export dynamic@ lets you dress up Haskell IO actions @@ -317,23 +356,24 @@ dsFExportDynamic id cconv -- hack: need to get at the name of the C stub we're about to generate. fe_nm = mkFastString (moduleString mod_name ++ "_" ++ toCName fe_id) in - dsFExport id export_ty fe_nm cconv True `thenDs` \ (h_code, c_code) -> newSysLocalDs arg_ty `thenDs` \ cback -> - dsLookupGlobalId newStablePtrName `thenDs` \ newStablePtrId -> + dsLookupGlobalId newStablePtrName `thenDs` \ newStablePtrId -> + dsLookupTyCon stablePtrTyConName `thenDs` \ stable_ptr_tycon -> let - mk_stbl_ptr_app = mkApps (Var newStablePtrId) [ Type arg_ty, Var cback ] + mk_stbl_ptr_app = mkApps (Var newStablePtrId) [ Type arg_ty, Var cback ] + stable_ptr_ty = mkTyConApp stable_ptr_tycon [arg_ty] + export_ty = mkFunTy stable_ptr_ty arg_ty in - dsLookupGlobalId bindIOName `thenDs` \ bindIOId -> - newSysLocalDs (mkTyConApp stablePtrTyCon [arg_ty]) `thenDs` \ stbl_value -> + dsLookupGlobalId bindIOName `thenDs` \ bindIOId -> + newSysLocalDs stable_ptr_ty `thenDs` \ stbl_value -> + dsFExport id export_ty fe_nm cconv True `thenDs` \ (h_code, c_code, stub_args) -> let - stbl_app cont ret_ty - = mkApps (Var bindIOId) - [ Type (mkTyConApp stablePtrTyCon [arg_ty]) - , Type ret_ty - , mk_stbl_ptr_app - , cont - ] - + stbl_app cont ret_ty = mkApps (Var bindIOId) + [ Type stable_ptr_ty + , Type ret_ty + , mk_stbl_ptr_app + , cont + ] {- The arguments to the external function which will create a little bit of (template) code on the fly @@ -343,13 +383,18 @@ dsFExportDynamic id cconv -} adj_args = [ mkIntLitInt (ccallConvToInt cconv) , Var stbl_value - , mkLit (MachLabel fe_nm) + , mkLit (MachLabel fe_nm mb_sz_args) ] -- name of external entry point providing these services. -- (probably in the RTS.) - adjustor = FSLIT("createAdjustor") + adjustor = FSLIT("createAdjustor") + + sz_args = sum (map (getPrimRepSizeInBytes . typePrimRep) stub_args) + mb_sz_args = case cconv of + StdCallConv -> Just sz_args + _ -> Nothing in - dsCCall adjustor adj_args PlayRisky False io_res_ty `thenDs` \ ccall_adj -> + dsCCall adjustor adj_args PlayRisky io_res_ty `thenDs` \ ccall_adj -> -- PlayRisky: the adjustor doesn't allocate in the Haskell heap or do a callback let ccall_adj_ty = exprType ccall_adj ccall_io_adj = mkLams [stbl_value] $ @@ -370,7 +415,6 @@ dsFExportDynamic id cconv ([arg_ty], io_res_ty) = tcSplitFunTys sans_foralls [res_ty] = tcTyConAppArgs io_res_ty -- Must use tcSplit* to see the (IO t), which is a newtype - export_ty = mkFunTy (mkTyConApp stablePtrTyCon [arg_ty]) arg_ty toCName :: Id -> String toCName i = showSDoc (pprCode CStyle (ppr (idName i))) @@ -393,9 +437,9 @@ mkFExportCBits :: FastString -> Type -> Bool -- True <=> returns an IO type -> CCallConv - -> (SDoc, SDoc) + -> (SDoc, SDoc, [Type]) mkFExportCBits c_nm maybe_target arg_htys res_hty is_IO_res_ty cc - = (header_bits, c_bits) + = (header_bits, c_bits, all_arg_tys) where -- Create up types and names for the real args arg_cnames, arg_ctys :: [SDoc] @@ -404,18 +448,21 @@ mkFExportCBits c_nm maybe_target arg_htys res_hty is_IO_res_ty cc -- 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 + extra_cnames_and_tys = case maybe_target of - Nothing -> [(text "the_stableptr", text "StgStablePtr")] + Nothing -> [((text "the_stableptr", text "StgStablePtr"), mkStablePtrPrimTy alphaTy)] other -> [] ++ case (maybe_target, cc) of - (Nothing, CCallConv) -> [(text "original_return_addr", text "void*")] + (Nothing, CCallConv) -> [((text "original_return_addr", text "void*"), addrPrimTy)] other -> [] all_cnames_and_ctys :: [(SDoc, SDoc)] all_cnames_and_ctys - = extra_cnames_and_ctys ++ zip arg_cnames arg_ctys + = map fst extra_cnames_and_tys ++ zip arg_cnames arg_ctys + + all_arg_tys + = map snd extra_cnames_and_tys ++ arg_htys -- stuff to do with the return type of the C function res_hty_is_unit = res_hty `eqType` unitTy -- Look through any newtypes