- ppr_fn = case fn of
- CmmLit (CmmLabel lbl) -> pprCLabel lbl
- _ -> parens (cCast (pprCFunType cconv results args) fn)
- -- for a dynamic call, cast the expression to
- -- a function of the right type (we hope).
-
- -- we #undef a function before calling it: the FFI is supposed to be
- -- an interface specifically to C, not to C+CPP. For one thing, this
- -- makes the via-C route more compatible with the NCG. If macros
- -- are being used for optimisation, then inline functions are probably
- -- better anyway.
- pprUndef (CmmLit (CmmLabel lbl)) =
- ptext SLIT("#undef") <+> pprCLabel lbl
- pprUndef _ = empty
+ cast_fn = parens (cCast (pprCFunType (char '*') cconv results args) fn)
+
+ real_fun_proto lbl = char ';' <>
+ pprCFunType (pprCLabel lbl) cconv results args <>
+ noreturn_attr <> semi
+
+ data_proto lbl = ptext (sLit ";EI_(") <>
+ pprCLabel lbl <> char ')' <> semi
+
+ noreturn_attr = case ret of
+ CmmNeverReturns -> text "__attribute__ ((noreturn))"
+ CmmMayReturn -> empty
+
+ -- See wiki:Commentary/Compiler/Backends/PprC#Prototypes
+ (maybe_proto, ppr_fn) =
+ case fn of
+ CmmLit (CmmLabel lbl)
+ | StdCallConv <- cconv -> (real_fun_proto lbl, pprCLabel lbl)
+ -- stdcall functions must be declared with
+ -- a function type, otherwise the C compiler
+ -- doesn't add the @n suffix to the label. We
+ -- can't add the @n suffix ourselves, because
+ -- it isn't valid C.
+ | CmmNeverReturns <- ret -> (real_fun_proto lbl, pprCLabel lbl)
+ | not (isMathFun lbl) -> (data_proto lbl, cast_fn)
+ -- we declare all other called functions as
+ -- data labels, and then cast them to the
+ -- right type when calling. This is because
+ -- the label might already have a declaration
+ -- as a data label in the same file,
+ -- e.g. Foreign.Marshal.Alloc declares 'free'
+ -- as both a data label and a function label.
+ _ ->
+ (empty {- no proto -}, cast_fn)
+ -- for a dynamic call, no declaration is necessary.