2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Foreign]{Foreign calls}
13 CCallTarget(..), isDynamicTarget, isCasmTarget,
14 CCallConv(..), defaultCCallConv, ccallConvToInt, ccallConvAttribute,
21 #include "HsVersions.h"
23 import CStrings ( CLabelString, pprCLabelString )
24 import FastString ( FastString )
29 %************************************************************************
31 \subsubsection{Data types}
33 %************************************************************************
39 deriving( Eq ) -- We compare them when seeing if an interface
40 -- has changed (for versioning purposes)
42 -- We may need more clues to distinguish foreign calls
43 -- but this simple printer will do for now
44 instance Outputable ForeignCall where
45 ppr (CCall cc) = ppr cc
46 ppr (DNCall dn) = ppr dn
52 = PlaySafe -- Might invoke Haskell GC, or do a call back, or
53 -- switch threads, etc. So make sure things are
54 -- tidy before the call
56 | PlayRisky -- None of the above can happen; the call will return
57 -- without interacting with the runtime system at all
59 -- Show used just for Show Lex.Token, I think
61 instance Outputable Safety where
63 ppr PlayRisky = ptext SLIT("unsafe")
65 playSafe PlaySafe = True
66 playSafe PlayRisky = False
70 %************************************************************************
72 \subsubsection{Calling C}
74 %************************************************************************
78 = CExportStatic -- foreign export ccall foo :: ty
79 CLabelString -- C Name of exported function
83 = CCallSpec CCallTarget -- What to call
84 CCallConv -- Calling convention to use.
93 = StaticTarget CLabelString -- An "unboxed" ccall# to `fn'.
94 | DynamicTarget -- First argument (an Addr#) is the function pointer
95 | CasmTarget CLabelString -- Inline C code (now seriously deprecated)
98 isDynamicTarget, isCasmTarget :: CCallTarget -> Bool
99 isDynamicTarget DynamicTarget = True
100 isDynamicTarget other = False
102 isCasmTarget (CasmTarget _) = True
103 isCasmTarget other = False
107 Stuff to do with calling convention
110 data CCallConv = CCallConv | StdCallConv
113 instance Outputable CCallConv where
114 ppr StdCallConv = ptext SLIT("__stdcall")
115 ppr CCallConv = ptext SLIT("_ccall")
117 defaultCCallConv :: CCallConv
118 defaultCCallConv = CCallConv
120 ccallConvToInt :: CCallConv -> Int
121 ccallConvToInt StdCallConv = 0
122 ccallConvToInt CCallConv = 1
125 Generate the gcc attribute corresponding to the given
126 calling convention (used by PprAbsC):
128 ToDo: The stdcall calling convention is x86 (win32) specific,
129 so perhaps we should emit a warning if it's being used on other
133 ccallConvAttribute :: CCallConv -> String
134 ccallConvAttribute StdCallConv = "__stdcall"
135 ccallConvAttribute CCallConv = ""
138 Printing into C files:
141 instance Outputable CExportSpec where
142 ppr (CExportStatic str _) = pprCLabelString str
144 instance Outputable CCallSpec where
145 ppr (CCallSpec fun cconv safety)
146 = hcat [ ifPprDebug callconv, ppr_fun fun ]
148 callconv = text "{-" <> ppr cconv <> text "-}"
150 gc_suf | playSafe safety = text "_GC"
153 ppr_fun DynamicTarget = text "__dyn_ccall" <> gc_suf <+> text "\"\""
154 ppr_fun (StaticTarget fn) = text "__ccall" <> gc_suf <+> pprCLabelString fn
155 ppr_fun (CasmTarget fn) = text "__casm" <> gc_suf <+> text "``" <> pprCLabelString fn <> text "''"
159 %************************************************************************
161 \subsubsection{.NET stuff}
163 %************************************************************************
166 data DNCallSpec = DNCallSpec FastString
169 instance Outputable DNCallSpec where
170 ppr (DNCallSpec s) = text "DotNet" <+> ptext s
175 %************************************************************************
179 %************************************************************************
182 okToExposeFCall :: ForeignCall -> Bool
183 -- OK to unfold a Foreign Call in an interface file
184 -- Yes, unless it's a _casm_
185 okToExposeFCall (CCall (CCallSpec target _ _)) = not (isCasmTarget target)
186 okToExposeFCall other = True