[swift-dev] Returning more than two scalars from C code

Bryan Chan bryan.chan at ca.ibm.com
Tue Mar 22 13:30:23 CDT 2016


jgroff at apple.com wrote on 2016-03-22 12:05:06 PM:

> On Mar 22, 2016, at 5:57 AM, Bryan Chan via swift-dev
<swift-dev at swift.org
> wrote:
>
> > I tried to google for an answer but couldn't find anything. What is
> > the correct way to do the following on Linux?
> >
> > @_silgen_name("foo")
> > func foo(theInt: UInt)
> > -> (a: UInt, b: UInt, c: UInt)
> >
> > var (d, e, f) = foo(aNum)
> >
[snip]
> >
> > Currently, the call to foo crashes because foo wants to return the
> > tuple indirectly, but the Swift call expects the three scalar return
> > values in registers. This example is a generalization of runtime
> > functions such as swift_class_getInstanceExtents, which happens to
> > work because it only returns two scalars in RAX and RDX, which is
> > supported by Clang. But my tests show that three-scalar tuples/
> > structs will cause problems. On architectures where Clang only
> > supports one scalar return value, the problem is worse.
> >
> > Am I doing something wrong or is there already a way to deal with
> > this (e.g. annotation in Swift code to make it obey C ABI, or vice
versa)?
>
> Don't use @_silgen_name. If you want to use a C API from Swift,
> define a Clang module for its headers, and import the module. See
> http://clang.llvm.org/docs/Modules.html for documentation on Clang
> module maps.

Is this the solution for runtime functions like
swift_class_getInstanceExtents as well?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20160322/6b7f0f50/attachment.html>


More information about the swift-dev mailing list