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

Joe Groff jgroff at apple.com
Tue Mar 22 13:37:47 CDT 2016


> On Mar 22, 2016, at 11:30 AM, Bryan Chan <bryan.chan at ca.ibm.com> wrote:
> 
> 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 <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?
The runtime functions are hacked in various unsavory ways to give them Swift-like calling conventions. Many of them also have important semantics that the compiler needs to be aware of, so if they aren't exported as standard library API in some way, you really shouldn't interact with them directly. (swift_class_getInstanceExtents happens to be OK, but what are you trying to do with it?)

-Joe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20160322/066a4e80/attachment.html>


More information about the swift-dev mailing list