Open hmelder opened 5 days ago
Looking further into Swift's code gen for ObjC, I am getting a bit more pessimistic as there a dozen files where internal ObjC structures are accessed directly. A lot of abstraction leakage to the point were Swift classes are essentially a super set of objc_class without a dtable. Not sure if they even use the same sidetable for weak references.
Options are:
objc_class
into two structures,
provides facilities for weak ref interop, and initialisation in a preallocated region (+ all the changes mirrored to CGObjCGNU in clang and Swift Codegen) %struct._class_t = type { ptr, ptr, ptr, ptr, ptr }
%struct._class_ro_t = type { i32, i32, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
I am currently looking into the implementation details of the Objective-C interoperability in the Swift compiler and runtime, and it turns out that we have a bit of a problem with the layout of
objc_class
.objc4 splits a class into three structures:
objc_class
,class_ro_t
, andclass_rw_t
.objc_class
is the one embedded into the swift class metadata described below.Embedding the existing
libobjc2
class structure will be difficult as it will probably break the contract between compiler and runtime in Swift. However, changingobjc_class
breaks the existing ABI in libobjc2.@davidchisnall there is probably no other way then to have an
-fobjc-runtime=gnustep-3.0
, or we make use of the version number field (probably very ugly). This should still be a lot less effort than portingobjc4
due to their dependence on Mach-O.Common Metadata Layout
Assuming
sizeof(void *) == 8
:Class Metadata Layout
The class metadata is of kind 0 on non-apple platforms, and a ISA pointer to an ObjC metaclass otherwise. There are 5 words reserved for
objc_class
starting with offset 0, exactly the size of the newobjc_class
.From docs/ABI/TypeMetadata.rst in swiftlang/swift
From runtime/objc-runtime-new.h in objc4