Commit Graph

65 Commits

Author SHA1 Message Date
Joe Groff
7191b87759 Runtime: Don't touch potentially ObjC unowned-referenced objects to determine their species.
ObjC unowned references are backed by an ObjC weak reference which will eagerly deallocate the object when it's strongly released, so in an unknown-refcount situation, we can't safely dereference the object pointer to determine its Swiftness. We can, however, look at the side table of weak references; if there's an entry for this object, then it's reliably an ObjC object (or it's some other object that got allocated in the reclaimed space for the dead object, but that's a race we fundamentally can't win with this broken design). Fixes rdar://problem/18091547 (modulo the aforementioned reallocation race).

Swift SVN r24825
2015-01-29 22:49:45 +00:00
Greg Parker
6d72340259 [runtime] Add -[SwiftObject isNSString__] et al. for Foundation's use.
rdar://19503750


Swift SVN r24668
2015-01-23 01:43:42 +00:00
Graham Batty
057c27f009 Disable existential metatype casting on non-objc.
Also fixes getting the size of an instance of a class to
work without it when objective-c interop is turned off.

Swift SVN r24477
2015-01-16 20:27:54 +00:00
John McCall
a027f3bc46 Flesh out the pinning API to cover all the cases
we need for arrays.

Swift SVN r24421
2015-01-14 19:14:24 +00:00
John McCall
6a91f7a172 Various improvements to the function-type ABI.
Teach IRGen and the runtime about the extra inhabitants
of function pointers, and take advantage of that in
thin and thick function types.

Also add runtime entrypoints for thin function type
metadata.

Swift SVN r24346
2015-01-10 01:45:37 +00:00
Dmitri Hrybenko
f996883fd6 SwiftObject.mm: explicitly include some headers that are used on Apple
ObjC platofrms

Swift SVN r24175
2015-01-05 07:18:11 +00:00
Joe Groff
343f35a81c Runtime: Make sure the superclass of a class is realized before we instantiate it.
Fixes a crash when some programs were run in JIT mode; they instantiated a JITed class before the ObjC subclass was realized. Send a message to super to realize the class before we try to readClassPair it. Fixes rdar://problem/19170232.

Swift SVN r23825
2014-12-10 01:12:29 +00:00
John McCall
3f46b30ca4 Add runtime functions to "pin" a native Swift object.
Pinning an object prevents it from being deallocated,
just like retaining it, but only one client can own the
pin at once.  Sensible "sharing" of the pin can occur
if attempts are perfectly nested.  It is efficient to
simultaneously query the pin state of an object in
conjunction with its strong reference count.

This combination of traits makes pinning suitable for
use in tracking whether a data structure backed by
an object is undergoing a non-structural modification:

- A structural change would require unique ownership
  of the object, but two non-structural changes (to
  different parts of the object) can occur at once
  without harm.  So a non-structural change can check
  for either uniqueness or a pin and then, if necessary,
  assert the pin for the duration of the change.
  Meanwhile, this act of asserting the pin prevents
  simultaneous structural changes.

- A very simple code-generation discipline leads to
  changes being perfectly nested as long as they're
  all performed by a single thread (or synchronously).
  Asynchrony can introduce imperfect nesting, but it's
  easy to write that off as a race condition and hence
  undefined behavior.

See Accessors.rst for more on both of these points.

Swift SVN r23761
2014-12-06 09:46:01 +00:00
John McCall
147fb8790a Comment some of the isUniquelyReferenced entrypoints
and remove _swift_isUniquelyReferenced_native_spareBits,
which was killed in favor of BridgeObject.

Swift SVN r23760
2014-12-06 09:45:59 +00:00
Joe Groff
b60a30c84b stdlib: Make isUniquelyReferenced shims properly return bool.
rdar://problem/18573806 is fixed.

Swift SVN r23547
2014-11-22 05:36:38 +00:00
Dmitri Hrybenko
441a84a68d Native runtime: no need to account for ObjC pointers in uniqueness
checking

Addresses review comment by Dave Abrahams.

Swift SVN r23520
2014-11-21 18:28:28 +00:00
Graham Batty
4e479f548c non-objc: Guard use of isNonNative_unTagged_bridgeObject.
Swift SVN r23477
2014-11-20 18:23:31 +00:00
Dave Abrahams
c6711afd77 [runtime] Fix BridgeObject refCounting/masking
Previously we weren't accounting for the fact that
heap_object_abi::SwiftSpareBitsMask includes all the tagged pointer
demarcation bits in heap_object_abi::ObjCReservedBitsMask.

Swift SVN r23459
2014-11-20 00:44:32 +00:00
Dave Abrahams
68fbea2988 BridgeObject: native object with no spare bits set
Naturally, the fast path for things like Array<Int> has to be that no
masking is required to get at the buffer.  Therefore, the state with no
spare bits set needs to be reserved for native objects.  After the
change, an ObjC non-tagged pointer is stored with all spare bits set, and
native objects are stored with 0..<N bits set, where N is the number of
spare bits.  ObjC tagged pointers are still stored verbatim.

Swift SVN r23430
2014-11-19 06:07:19 +00:00
Dave Abrahams
b297c1f51b Make better use of Builtin.BridgeObject bits
We used to reserve a specific spare bit to say "this is a native
object."  Now, we're going to say, "if *any* spare bit is set, this is a
native object."  At the cost of having no spare bits to work with in the
non-native case, this allows us to store a number in 1..<4 (actually
0..<4, at some cost in speed for the 0 case) along with any native
object on all platforms.

This half bit advantage is important on 32-bit platforms, we have only
spare 2 bits to work with.

Given that on the 64-bit platforms there are *no* spare bits in the case
where the object is a non-native tagged pointer, we have no guarantee of
being able to store extra information along with an arbitrary non-native
object.  Giving up the ability to store bits for *all* non-native
objects (even non-tagged ones) is therefore not much of a sacrifice.

Fixes <rdar://problem/18920415> More useful spare bits in Builtin.BridgeObject

Swift SVN r23345
2014-11-15 00:53:39 +00:00
Graham Batty
04f8efbdf5 Correctly calculate non-objc instance size and rename to reflect.
Renames swift_class_getInstanceSize to
swift_class_getInstancePositiveExtentSize to reflect that it is
the size of the object after the instance address point.

Swift SVN r23333
2014-11-14 22:09:20 +00:00
Graham Batty
7b71be3fc9 Fix non-objc implementation of getRootSuperclass and isObjCTaggedPointer.
Swift SVN r23329
2014-11-14 21:18:25 +00:00
Joe Groff
4c0e64f1da Handle class-constrained generic class-to-objc-existential casts.
Fixes rdar://problem/18638625.

Swift SVN r23324
2014-11-14 19:51:20 +00:00
Joe Groff
b5b860ad39 IRGen: Properly create ObjC protocols using the runtime in JIT mode.
Just injecting a new protocol descriptor into an already-running ObjC runtime isn't a good idea, since the runtime might have already canonized the protocol somewhere else, and it won't recognize that classes conform to protocols it doesn't know about.

Swift SVN r23313
2014-11-14 01:01:02 +00:00
Dave Abrahams
9fa1cc7a3a [stdlib] BridgeObject uniqueness checking
Also, more complete testing overall

Swift SVN r23268
2014-11-12 15:00:57 +00:00
Dmitri Hrybenko
0d42339173 Runtime: fix the build for !SWIFT_OBJC_INTEROP
Swift SVN r23265
2014-11-12 08:02:28 +00:00
Joe Groff
b50293b26a Runtime: Work around rdar://problem/18950072 to avoid paying for .cxx_destruct on Swift objects.
Clang overzealously infects SwiftObject with the HasCXXStructors bit because it contained a struct (with trivial constructor). Manually explode the struct to avoid this.

Swift SVN r23259
2014-11-12 02:30:31 +00:00
Joe Groff
7ce4ea860c IRGen/Runtime: Handle non-class-metatype to ObjC existential casts.
These always fail, and it doesn't make sense to inline this check into the cast site, so provide additional runtime functions for metatype-to-objc-existential casts.

Swift SVN r23237
2014-11-11 18:51:09 +00:00
Dmitri Hrybenko
af9515d754 Break a circular dependency between SwiftShims and the Darwin module
stdint.h and stddef.h are shipped with CLang, but they not included in
Clang's module.map, which causes Clang to import libc versions instead
(and Clang's stdint.h is dispatching to libc).  This was causing
hard-to-debug transient failures during incremental rebuilds, like this:

error: module file was created by an older version of the compiler: .../Darwin.swiftmodule

Swift SVN r23230
2014-11-11 02:26:06 +00:00
Dave Abrahams
3970bed9c7 [stdlib] Fix a comment
Swift SVN r23189
2014-11-09 04:53:32 +00:00
Greg Parker
e70ca2762d Runtime: Improve diagnostics for failed casts to ObjC class and protocol types.
Swift SVN r23146
2014-11-07 00:53:05 +00:00
Graham Batty
d15c24e25b Changes to runtime library to support non-objc targets
Swift SVN r23122
2014-11-05 23:17:07 +00:00
Joe Groff
dfac9181dd Runtime: Add ObjC weak referencing support to SwiftObject.
We were missing -_tryRetain, -_isDeallocating, -allowsWeakReference and -retainWeakReference implementations on SwiftObject, so forming an ObjC weak reference to a pure Swift object always failed and produced a nil reference. Fixes rdar://problem/18637774.

This can be reapplied now that we properly call objc_destructInstance on deallocation.

Swift SVN r23070
2014-11-02 21:03:21 +00:00
Joe Groff
01554448fc Runtime: Walk up superclass chains to find class conformances.
Swift SVN r23065
2014-11-02 15:41:00 +00:00
Joe Groff
26eae4743a Runtime: Conditionalize the ObjC-bridging parts of bridgeObjectRetain/Release.
If there's no ObjC runtime to interop with, then there's no need to handle tagged pointers or the native refcounting bit.

Swift SVN r23019
2014-10-30 21:18:53 +00:00
Joe Groff
f6813e1440 Typo in comment
Swift SVN r22900
2014-10-23 22:39:07 +00:00
Greg Parker
c6a88262cf [stdlib] Scrap bit-rotted FastEntryPoints.s. Rewrite refcounts using __atomic.
Generated code on x86_64 for swift_retain and swift_release and 
swift_allocObject are unchanged. arm64 is improved by using weaker 
memory barriers, fixing rdar://17423624.


Swift SVN r22887
2014-10-23 08:22:48 +00:00
Joe Groff
5a2f48e3be Add a Builtin.BridgeObject type.
This is a type that has ownership of a reference while allowing access to the
spare bits inside the pointer, but which can also safely hold an ObjC tagged pointer
reference (with no spare bits of course). It additionally blesses one
Foundation-coordinated bit with the meaning of "has swift refcounting" in order
to get a faster short-circuit to native refcounting. It supports the following
builtin operations:

- Builtin.castToBridgeObject<T>(ref: T, bits: Builtin.Word) ->
  Builtin.BridgeObject

  Creates a BridgeObject that contains the bitwise-OR of the bit patterns of
  "ref" and "bits". It is the user's responsibility to ensure "bits" doesn't
  interfere with the reference identity of the resulting value. In other words,
  it is undefined behavior unless:

    castReferenceFromBridgeObject(castToBridgeObject(ref, bits)) === ref

  This means "bits" must be zero if "ref" is a tagged pointer. If "ref" is a real
  object pointer, "bits" must not have any non-spare bits set (unless they're
  already set in the pointer value). The native discriminator bit may only be set
  if the object is Swift-refcounted.

- Builtin.castReferenceFromBridgeObject<T>(bo: Builtin.BridgeObject) -> T

  Extracts the reference from a BridgeObject.

- Builtin.castBitPatternFromBridgeObject(bo: Builtin.BridgeObject) -> Builtin.Word

  Presents the bit pattern of a BridgeObject as a Word.

BridgeObject's bits are set up as follows on the various platforms:

i386, armv7:

  No ObjC tagged pointers
  Swift native refcounting flag bit: 0x0000_0001
  Other available spare bits:        0x0000_0002

x86_64:

  Reserved for ObjC tagged pointers: 0x8000_0000_0000_0001
  Swift native refcounting flag bit: 0x0000_0000_0000_0002
  Other available spare bits:        0x7F00_0000_0000_0004

arm64:

  Reserved for ObjC tagged pointers: 0x8000_0000_0000_0000
  Swift native refcounting flag bit: 0x4000_0000_0000_0000
  Other available spare bits:        0x3F00_0000_0000_0007

TODO: BridgeObject doesn't present any extra inhabitants. It ought to at least provide null as an extra inhabitant for Optional.

Swift SVN r22880
2014-10-23 00:09:23 +00:00
Dave Abrahams
56c4cb2d42 [stdlib] Kill off _isUniquelyReferenced
It was doing an unsafeBitCast and possibly not managing lifetimes;
replace it with more-typesafe and memory-safe calls where possible.

Swift SVN r22779
2014-10-15 22:25:12 +00:00
Dave Abrahams
506a1b07a6 [stdlib] Modernize ArrayBuffer uniqueness check
Swift SVN r22773
2014-10-15 21:17:31 +00:00
Greg Parker
330752b749 Revert r22710 and r22711 implementing ObjC weak ref methods on SwiftObject.
This is still incomplete and needs more work. rdar://18637774.


Swift SVN r22717
2014-10-14 01:33:29 +00:00
Greg Parker
cb8480359b Fix -[SwiftObject allowsWeakReference].
Swift SVN r22711
2014-10-13 22:20:56 +00:00
Joe Groff
82f6260c64 Runtime: Add ObjC weak referencing support to SwiftObject.
We were missing -_tryRetain, -_isDeallocating, -allowsWeakReference and -retainWeakReference implementations on SwiftObject, so forming an ObjC weak reference to a pure Swift object always failed and produced a nil reference. Fixes rdar://problem/18637774.

Swift SVN r22710
2014-10-13 22:12:39 +00:00
Joe Groff
b7b226d437 Runtime: Provide ARC methods for the root metaclass.
In ObjC, RootClass's metaclass isa RootClass, which is bad for SwiftObject, because its applying instance retain/release operations ends up clobbering internal state of the class. Add class method versions of 'retain' and 'release' that are no-ops, like +[NSObject retain/release] are. Fixes rdar://problem/18625294.

Swift SVN r22703
2014-10-13 16:59:18 +00:00
Dmitri Hrybenko
fea5de2fc8 stdlib and runtime: coding style fixes
Swift SVN r22685
2014-10-11 01:40:57 +00:00
Dave Abrahams
5c33278e63 [stdlib] Add ManagedBufferPointer
A revamped version of HeapBuffer that doesn't allow null buffer
references.  Something like this is needed for killing the null array
state.

Swift SVN r22683
2014-10-11 01:10:38 +00:00
Dave Abrahams
74e27aaab1 [stdlib] uniqueness checking for users
Make unique reference checking available to users, making ManagedBuffer
a complete facility for building COW value types.  Also rationalize the
way we name and organize the runtime primitives we ultimately call.

Swift SVN r22594
2014-10-08 04:48:52 +00:00
Dave Abrahams
512da79b7a [stdlib] Switch some shims to use id
Since ObjC functions take their arguments at +0, we don't need to erase
"object-ness" in order to avoid disturbing reference counts.  This
interacts much better with lifetime optimizations, since the caller must
ensure the argument isn't released until these functions return.

Swift SVN r22580
2014-10-07 22:15:11 +00:00
Dave Abrahams
cc9bc0c274 [stdlib] Kill _swift_isUniquelyReferenced @asmname
Swift SVN r22576
2014-10-07 21:40:48 +00:00
Dmitri Hrybenko
6d7a95246a Runtime: move swift_getObjectType() to a C++ file
This function is not ObjC-specific, and it is used when no ObjC runtime
is present.

Swift SVN r22524
2014-10-05 00:55:25 +00:00
Jordan Rose
61b2d1565c [runtime] Remove unnecessary imports of shims.h
...which is not C++-compatible at the moment.

Swift SVN r21651
2014-09-02 20:19:47 +00:00
John McCall
590c25479d Use isa-masking to read the class object pointer from
instances of Swift subclasses of ObjC classes.

We were already doing this in the runtime.  This patch
unhides the runtime's mask word (swift_isaMask) and makes
IR-gen take advantage of it when it can.

Swift SVN r21592
2014-08-29 21:36:53 +00:00
Dave Abrahams
0085b94509 [stdlib] AnyObject wrapper for bridging purposes
As part of the evolution toward a one-word array layout, create a type
that can be used to efficiently store Cocoa or Native class instances
and discriminate between them.

Swift SVN r21469
2014-08-27 00:04:25 +00:00
Doug Gregor
e00aaadf48 Allow dynamic casting of foreign metatypes.
Another part of CF casting support, <rdar://problem/18088474>.


Swift SVN r21402
2014-08-22 04:47:11 +00:00
Doug Gregor
dde9485f08 Fix embarrassing bug in -_cfTypeID. Thanks, Joe
Swift SVN r21369
2014-08-21 18:13:48 +00:00