Interop with Swift; questions

hi,

I'm looking into inter-operating with Swift compiler generated code with the new stable abi nearing.

I'm getting a fairly good idea on how things work, but I do have some questions:

  • I see there are two constructors; 1 actual and 1 that also allocates, where the allocator one calls the real one. Can I presume this follows the Objc rules and always returns retained?
  • What about normal method calls, do they return retained by default?
  • When allocating there's a type metadata accessor that is used to well, allocate it. It takes an int64 parameter, is there any documentation on how this works and what the int64 means (will it be an int32 on 32bits targets?)?
  • How does virtual method dispatch kick in? I tried the example below but doesn't seem to do virtual dispatch.
  • Is there any doc on the format of mangling itself?

module fred:

open class Test {
  public init(a: Swift.Int, _ b: Swift.Int, aaa c: Swift.Int)
  public func getVal() -> Swift.Int
  open func DoIt()
  deinit
}
public func newit() -> fred.Test

User:

import fred
public func newit2() {
  var q = newit()
  q.DoIt()
}

IR:

define dllexport swiftcc void @"$s5test26newit2yyF"() #0 {
entry:
  %q = alloca %T4fred4TestC*, align 8
  %0 = bitcast %T4fred4TestC** %q to i8*
  call void @llvm.memset.p0i8.i64(i8* align 8 %0, i8 0, i64 8, i1 false)
  %1 = bitcast %T4fred4TestC** %q to i8*
  call void @llvm.lifetime.start.p0i8(i64 8, i8* %1)
  %2 = call swiftcc %T4fred4TestC* @"$s4fred5newitAA4TestCyF"()
  store %T4fred4TestC* %2, %T4fred4TestC** %q, align 8
  %3 = bitcast %T4fred4TestC* %2 to %swift.refcounted*
  %4 = call %swift.refcounted* @swift_retain(%swift.refcounted* returned %3) #3
  call swiftcc void @"$s4fred4TestC4DoItyyFTj"(%T4fred4TestC* swiftself %2) 
  call void bitcast (void (%swift.refcounted*)* @swift_release to void (%T4fred4TestC*)*)(%T4fred4TestC* %2) #3
  %toDestroy = load %T4fred4TestC*, %T4fred4TestC** %q, align 8
  call void bitcast (void (%swift.refcounted*)* @swift_release to void (%T4fred4TestC*)*)(%T4fred4TestC* %toDestroy) #3
  %5 = bitcast %T4fred4TestC** %q to i8*
  call void @llvm.lifetime.end.p0i8(i64 8, i8* %5)
  ret void
}

Note the call swiftcc void @"$s4fred4TestC4DoItyyFTj"(%T4fred4TestC* swiftself %2)

I somewhat expected this to be virtual..

(Do let me know if this is in the wrong place, or should be split up)

1 Like