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

Bryan Chan bryan.chan at ca.ibm.com
Tue Mar 22 07:57:48 CDT 2016



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)

Where foo is:

  typedef struct {
    long a;
    long b;
    long c;
  } Tuple;

  extern "C" {
    Tuple foo(int i) {
      return Tuple{ i, i, i };
    }
  }

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)?

Thanks, Bryan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20160322/7570a3d4/attachment.html>


More information about the swift-dev mailing list