2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Foreign]{Foreign calls}
11 CCallSpec(..), ccallIsCasm,
12 CCallTarget(..), dynamicTarget, isDynamicTarget,
13 CCallConv(..), defaultCCallConv, ccallConvToInt, ccallConvAttribute,
18 #include "HsVersions.h"
20 import CStrings ( CLabelString, pprCLabelString )
25 %************************************************************************
27 \subsubsection{Data types}
29 %************************************************************************
34 | DotNetCall DotNetCallSpec
35 deriving( Eq ) -- We compare them when seeing if an interface
36 -- has changed (for versioning purposes)
38 -- We may need more clues to distinguish foreign calls
39 -- but this simple printer will do for now
40 instance Outputable ForeignCall where
41 ppr (CCall cc) = ppr cc
42 ppr (DotNetCall dn) = ppr dn
48 = PlaySafe -- Might invoke Haskell GC, or do a call back, or
49 -- switch threads, etc. So make sure things are
50 -- tidy before the call
52 | PlayRisky -- None of the above can happen; the call will return
53 -- without interacting with the runtime system at all
55 -- Show used just for Show Lex.Token, I think
57 instance Outputable Safety where
59 ppr PlayRisky = ptext SLIT("unsafe")
61 playSafe PlaySafe = True
62 playSafe PlayRisky = False
66 %************************************************************************
68 \subsubsection{Calling C}
70 %************************************************************************
74 = CCallSpec CCallTarget -- What to call
75 CCallConv -- Calling convention to use.
77 Bool -- True <=> really a "casm"
81 ccallIsCasm :: CCallSpec -> Bool
82 ccallIsCasm (CCallSpec _ _ _ c_asm) = c_asm
89 = StaticTarget CLabelString -- An "unboxed" ccall# to `fn'.
90 | DynamicTarget -- First argument (an Addr#) is the function pointer
93 isDynamicTarget DynamicTarget = True
94 isDynamicTarget (StaticTarget _) = False
96 dynamicTarget :: CCallTarget
97 dynamicTarget = DynamicTarget
101 Stuff to do with calling convention
104 data CCallConv = CCallConv | StdCallConv
107 instance Outputable CCallConv where
108 ppr StdCallConv = ptext SLIT("__stdcall")
109 ppr CCallConv = ptext SLIT("_ccall")
111 defaultCCallConv :: CCallConv
112 defaultCCallConv = CCallConv
114 ccallConvToInt :: CCallConv -> Int
115 ccallConvToInt StdCallConv = 0
116 ccallConvToInt CCallConv = 1
119 Generate the gcc attribute corresponding to the given
120 calling convention (used by PprAbsC):
122 ToDo: The stdcall calling convention is x86 (win32) specific,
123 so perhaps we should emit a warning if it's being used on other
127 ccallConvAttribute :: CCallConv -> String
128 ccallConvAttribute StdCallConv = "__stdcall"
129 ccallConvAttribute CCallConv = ""
132 Printing into C files:
135 instance Outputable CCallSpec where
136 ppr (CCallSpec fun cconv safety is_casm)
137 = hcat [ ifPprDebug callconv
139 , text before , ppr_fun , after]
141 callconv = text "{-" <> ppr cconv <> text "-}"
142 play_safe = playSafe safety
145 | is_casm && play_safe = "casm_GC ``"
146 | is_casm = "casm ``"
147 | play_safe = "ccall_GC "
148 | otherwise = "ccall "
151 | is_casm = text "''"
154 ppr_dyn = case fun of
155 DynamicTarget -> text "dyn_"
158 ppr_fun = case fun of
159 DynamicTarget -> text "\"\""
160 StaticTarget fn -> pprCLabelString fn
164 %************************************************************************
166 \subsubsection{.NET stuff}
168 %************************************************************************
171 data DotNetCallSpec = DotNetCallSpec
174 instance Outputable DotNetCallSpec where
175 ppr DotNetCallSpec = text "DotNet!"