2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Foreign]{Foreign calls}
9 Safety(..), playSafe, playThreadSafe,
11 CExportSpec(..), CLabelString, isCLabelString, pprCLabelString,
13 CCallTarget(..), isDynamicTarget,
14 CCallConv(..), defaultCCallConv, ccallConvToInt, ccallConvAttribute,
16 DNCallSpec(..), DNKind(..), DNType(..),
20 #include "HsVersions.h"
22 import FastString ( FastString, unpackFS )
23 import Char ( isAlphaNum )
29 %************************************************************************
31 \subsubsection{Data types}
33 %************************************************************************
39 deriving( Eq ) -- We compare them when seeing if an interface
40 -- has changed (for versioning purposes)
41 {-! derive: Binary !-}
43 -- We may need more clues to distinguish foreign calls
44 -- but this simple printer will do for now
45 instance Outputable ForeignCall where
46 ppr (CCall cc) = ppr cc
47 ppr (DNCall dn) = ppr dn
53 = PlaySafe -- Might invoke Haskell GC, or do a call back, or
54 -- switch threads, etc. So make sure things are
55 -- tidy before the call
56 Bool -- => True, external function is also re-entrant.
57 -- [if supported, RTS arranges for the external call
58 -- to be executed by a separate OS thread, i.e.,
59 -- _concurrently_ to the execution of other Haskell threads.]
61 | PlayRisky -- None of the above can happen; the call will return
62 -- without interacting with the runtime system at all
64 -- Show used just for Show Lex.Token, I think
65 {-! derive: Binary !-}
67 instance Outputable Safety where
68 ppr (PlaySafe False) = ptext SLIT("safe")
69 ppr (PlaySafe True) = ptext SLIT("threadsafe")
70 ppr PlayRisky = ptext SLIT("unsafe")
72 playSafe :: Safety -> Bool
73 playSafe PlaySafe{} = True
74 playSafe PlayRisky = False
76 playThreadSafe :: Safety -> Bool
77 playThreadSafe (PlaySafe x) = x
78 playThreadSafe _ = False
82 %************************************************************************
84 \subsubsection{Calling C}
86 %************************************************************************
90 = CExportStatic -- foreign export ccall foo :: ty
91 CLabelString -- C Name of exported function
93 {-! derive: Binary !-}
96 = CCallSpec CCallTarget -- What to call
97 CCallConv -- Calling convention to use.
100 {-! derive: Binary !-}
107 = StaticTarget CLabelString -- An "unboxed" ccall# to `fn'.
108 | DynamicTarget -- First argument (an Addr#) is the function pointer
110 {-! derive: Binary !-}
112 isDynamicTarget :: CCallTarget -> Bool
113 isDynamicTarget DynamicTarget = True
114 isDynamicTarget other = False
118 Stuff to do with calling convention:
120 ccall: Caller allocates parameters, *and* deallocates them.
122 stdcall: Caller allocates parameters, callee deallocates.
123 Function name has @N after it, where N is number of arg bytes
126 ToDo: The stdcall calling convention is x86 (win32) specific,
127 so perhaps we should emit a warning if it's being used on other
131 data CCallConv = CCallConv | StdCallConv
133 {-! derive: Binary !-}
135 instance Outputable CCallConv where
136 ppr StdCallConv = ptext SLIT("stdcall")
137 ppr CCallConv = ptext SLIT("ccall")
139 defaultCCallConv :: CCallConv
140 defaultCCallConv = CCallConv
142 ccallConvToInt :: CCallConv -> Int
143 ccallConvToInt StdCallConv = 0
144 ccallConvToInt CCallConv = 1
147 Generate the gcc attribute corresponding to the given
148 calling convention (used by PprAbsC):
151 ccallConvAttribute :: CCallConv -> String
152 ccallConvAttribute StdCallConv = "__attribute__((__stdcall__))"
153 ccallConvAttribute CCallConv = ""
157 type CLabelString = FastString -- A C label, completely unencoded
159 pprCLabelString :: CLabelString -> SDoc
160 pprCLabelString lbl = ftext lbl
162 isCLabelString :: CLabelString -> Bool -- Checks to see if this is a valid C label
164 = all ok (unpackFS lbl)
166 ok c = isAlphaNum c || c == '_' || c == '.'
167 -- The '.' appears in e.g. "foo.so" in the
168 -- module part of a ExtName. Maybe it should be separate
172 Printing into C files:
175 instance Outputable CExportSpec where
176 ppr (CExportStatic str _) = pprCLabelString str
178 instance Outputable CCallSpec where
179 ppr (CCallSpec fun cconv safety)
180 = hcat [ ifPprDebug callconv, ppr_fun fun ]
182 callconv = text "{-" <> ppr cconv <> text "-}"
184 gc_suf | playSafe safety = text "_GC"
187 ppr_fun DynamicTarget = text "__dyn_ccall" <> gc_suf <+> text "\"\""
188 ppr_fun (StaticTarget fn) = text "__ccall" <> gc_suf <+> pprCLabelString fn
192 %************************************************************************
194 \subsubsection{.NET interop}
196 %************************************************************************
200 DNCallSpec Bool -- True => static method/field
201 DNKind -- what type of access
203 String -- fully qualified method/field name.
204 [DNType] -- argument types.
205 DNType -- result type.
207 {-! derive: Binary !-}
214 {-! derive: Binary !-}
236 {-! derive: Binary !-}
238 withDNTypes :: DNCallSpec -> [DNType] -> DNType -> DNCallSpec
239 withDNTypes (DNCallSpec isStatic k assem nm _ _) argTys resTy
240 = DNCallSpec isStatic k assem nm argTys resTy
242 instance Outputable DNCallSpec where
243 ppr (DNCallSpec isStatic kind ass nm _ _ )
245 (if isStatic then text "static" else empty) <+>
246 (text (case kind of { DNMethod -> "method" ; DNField -> "field"; DNConstructor -> "ctor" })) <+>
247 (if null ass then char ' ' else char '[' <> text ass <> char ']') <>
254 %************************************************************************
258 %************************************************************************
261 {-* Generated by DrIFT-v1.0 : Look, but Don't Touch. *-}
262 instance Binary ForeignCall where
263 put_ bh (CCall aa) = do
266 put_ bh (DNCall ab) = do
277 instance Binary Safety where
278 put_ bh (PlaySafe aa) = do
281 put_ bh PlayRisky = do
288 _ -> do return PlayRisky
290 instance Binary CExportSpec where
291 put_ bh (CExportStatic aa ab) = do
297 return (CExportStatic aa ab)
299 instance Binary CCallSpec where
300 put_ bh (CCallSpec aa ab ac) = do
308 return (CCallSpec aa ab ac)
310 instance Binary CCallTarget where
311 put_ bh (StaticTarget aa) = do
314 put_ bh DynamicTarget = do
320 return (StaticTarget aa)
321 _ -> do return DynamicTarget
323 instance Binary CCallConv where
324 put_ bh CCallConv = do
326 put_ bh StdCallConv = do
331 0 -> do return CCallConv
332 _ -> do return StdCallConv
334 instance Binary DNCallSpec where
335 put_ bh (DNCallSpec isStatic kind ass nm _ _) = do
345 return (DNCallSpec isStatic kind ass nm [] undefined)
347 instance Binary DNKind where
348 put_ bh DNMethod = do
352 put_ bh DNConstructor = do
357 0 -> do return DNMethod
358 1 -> do return DNField
359 _ -> do return DNConstructor
361 instance Binary DNType where
368 put_ bh DNDouble = do
384 put_ bh DNWord16 = do
386 put_ bh DNWord32 = do
388 put_ bh DNWord64 = do
394 put_ bh DNObject = do
396 put_ bh DNString = do
413 11 -> return DNWord16
414 12 -> return DNWord32
415 13 -> return DNWord64
418 16 -> return DNObject
419 17 -> return DNString
421 -- Imported from other files :-