As a follow up to
Is there any info on how to layout my classes if I were to emit classes that descent from or implement protocols defined in Swift?
As a follow up to
Is there any info on how to layout my classes if I were to emit classes that descent from or implement protocols defined in Swift?
What is it you're trying to do?
Trying to make my compiler be able to fully interact with swift modules like it already can for objc.
To elaborate, we work on https://www.elementscompiler.com, a multi-language multi-platform compiler that, among other platforms, supports building apps for iOS, currently on the Objective-C Runtime ABI level, using C#, Java, Oxygene, Go (and also Swift).
Now that Apple Swift has ABI Stability and (soon) Module Stability, we want to extend the compiler to also be able to participate at the Swift ABI level; more specifically, we'd like to be able to (a) compile/link against and use types from Swift-created libraries and frameworks and (b) generate libraries and frameworks (and executables) that define and expose Swift API-compatible types and can then in theory be referenced from Swift as if they were created by "native" Apple Swift.
We don't have detailed documentation for many aspects of the ABI yet, sorry. If you'd like that work on that, that would be great, and we'd be happy to provide guidance.
Generating Swift libraries should be straightforward with the right documentation and once we have stable (textual) module interface descriptions.
That'd be great and any pointers would be much appreciated, yes. thanx!
Okay. I would probably start with struct layout.
Any chance of some hints on where to look (for this + the stuff in the original thread)?
The Swift 5 struct layout algorithm is to lay out the stored properties using naive, in-order C layout. Class layout is the same, except the first field is either the base class (recursively) or a fixed header (a pointer plus 64 unaligned bits). The only significant difference from C, besides the lack of bit-fields, is that we distinguish size vs. stride and so don't round a struct (or base class's) size up to the maximum alignment of any stored property. But you can also look at lib/IRGen/StructLayout.cpp
in the source code.
The ABI rule for tuples always uses the Swift 5 layout algorithm, and that is a permanent guarantee. For structs, officially the layout algorithm is determined by an algorithm that's somehow parameterized by the struct and the build settings; however, right now that algorithm is always "use the Swift 5 layout algorithm".
Yet that's simple enough. My research shows that when targetting Darwin:
When targetting Objc the allocation goes a bit the same except:
My questions are:
(It could be I missed some steps, it's quite hard to read this code)
There's a TypeMetadata.rst
which explains the basics of that. You're confusing type descriptors and type metadata, which are different.
that algorithm is always "use the Swift 5 layout algorithm".
Is the document https://github.com/apple/swift/blob/master/docs/ABI/TypeLayout.rst up to date vis-a-vis this "Swift 5 layout algorithm"?
In enum layout, spare bits are considered in other places besides just smaller-than-allocated integers. Otherwise I think it's accurate.
Great, thank you!