Commit Graph

562 Commits

Author SHA1 Message Date
Joe Groff
c98ce540d4 IRGen: Emit protocol conformance records.
When we emit a witness table, build a protocol conformance record for it, and emit the list of all conformance records into a "__swift1_proto" section of the data segment.

Swift SVN r22939
2014-10-25 01:50:49 +00:00
Joe Groff
df42e26d5f Runtime: Define a type for protocol conformance records.
Swift SVN r22918
2014-10-24 18:24:00 +00:00
Joe Groff
1a24b1b56b IRGen/Runtime: Expand the private data area in metadata caches.
And move the new size into an ABI constant so we can change it in one place in the future if need be.

Swift SVN r22899
2014-10-23 22:16:32 +00:00
Joe Groff
f761fc03e6 IRGen/Runtime: Put a reference to the metadata pattern in the nominal type descriptor.
We want to use the reserved space in the metadata pattern for protocol conformance caching, and this link lets us find the metadata pattern from an instance of the generic type.

Swift SVN r22898
2014-10-23 21:33:38 +00:00
Joe Groff
fc369ce734 Give Builtin.BridgeObject nil as an extra inhabitant.
Reserve the other low pointer values for potential internal use.

Swift SVN r22881
2014-10-23 00:49:14 +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
John McCall
89e60f31aa Add protocol witness tables to existential metatype
layouts.  Introduce new SIL instructions to initialize
and open existential metatype values.

Don't actually, y'know, lift any of the restriction on
existential metatypes; just pointlessly burn extra
memory storing them.

Swift SVN r22592
2014-10-08 01:20:13 +00:00
Dmitri Hrybenko
872a02f205 Runtime: don't use metadata for Builtin.UnknownObject when
!SWIFT_OBJC_INTEROP

Swift SVN r22510
2014-10-03 23:43:01 +00:00
Joe Groff
93cb4dd83e Runtime: Remove dead HeapArrayMetadata kind.
Swift SVN r22294
2014-09-26 03:12:21 +00:00
Joe Groff
7a08716de3 Runtime: Factor out a helper function on Metadata to say when metadata refers to a class type.
Rename the existing misleadingly-named "isClassType" to "isClassObject", and document that it refers to (swift or ObjC) class objects, the latter of which aren't always type metadata.

Swift SVN r22153
2014-09-21 00:18:16 +00:00
Joe Groff
7d1e60e253 IRGen/Runtime: Agree on which flag bit means "isNotBitwiseTakable" again.
Somehow this got out of sync, and IRGen's idea of which flag it was collided with "hasExtraInhabitants", causing hilarity to ensue whenever weak or protocol types were packed in optionals. Fixes rdar://problem/17814752.

Swift SVN r21803
2014-09-09 05:07:17 +00:00
John McCall
c1b03784df Instead of passing type metadata to swift_initClassMetadata,
pass the size and alignment of each field.  Take advantage
of this to pass a constant size and alignment when
possible.

This avoids the need to recursively find type metadata for
every field type, allowing generic recursively-structured
classes to be built.  There are a number of more complicated
cases that this approach isn't good enough for, but this
is good enough for now to fix rdar://18067671.

Also make an effort to properly support generic subclasses
of Objective-C classes.

Swift SVN r21506
2014-08-28 01:27:53 +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
61ee5f7529 Allow dynamic casts between foreign classes and other classes.
These casts always succeed, because we don't have any way to check
them (yet). Part of <rdar://problem/18088474>.

Swift SVN r21386
2014-08-21 22:54:10 +00:00
John McCall
80dd63b38d Don't allocate values out-of-line just because they're
not bitwise takable.

Finishes rdar://16755731

Swift SVN r20742
2014-07-30 08:24:18 +00:00
John McCall
584795e505 Replace the typeOf value witness with an
initializeBufferWithTakeOfBuffer value witness.

Attempt to use initializeBufferWithTakeOfBuffer in
some appropriate places.

There are some changes enabled by this which are
coming in a follow-up patch.

Swift SVN r20741
2014-07-30 08:24:12 +00:00
John McCall
934527a029 Eliminate all uses of the typeof value witness.
Swift SVN r20734
2014-07-30 04:40:58 +00:00
John McCall
a611637c5a Add a unit test to validate that the new runtime machinery
constructs generic class metadata properly when the
superclass has an unexpected prefix.

Also fix a bug in the constructor for ClassMetadata (which is
only actually constructed this way in unit tests).

Swift SVN r20586
2014-07-26 01:04:58 +00:00
John McCall
64aa0ea950 When instantiating a generic class, compensate for
unexpected forematter from the superclass.

This requires a pretty substantial shift in the
generic-metadata allocation/initialization dance
because (1) we can't allocate class metadata without
knowing what the superclass is and (2) the offset
from the metadata cache entry to the address point is
no longer determined solely by the metadata pattern.

While I'm making invasive changes to metadata, fix
two race conditions in metadata creation.  The first
is that we need to ensure that only one thread succeeds
at lazily creating a generic-metadata cache.  The second
is that we need to ensure that only one thread actually
attempts to create a particular metadata; any others
should block until the metadata is successfully built.

This commit finishes rdar://17776354.  LLDB will
need to adjust to the runtime-private metadata layout
changes.

Swift SVN r20537
2014-07-25 10:08:51 +00:00
John McCall
ae0dfd4676 Change the layout of the extended class-object header: add
a flags field, add an instance address point field, and reserve
some additional space.

This change must be coordinated with a corresponding change
to ObjC runtime bits in libarclite; without this, dynamic
subclassing features like KVO will break.

The actual contents of the new fields can change without
bothering the ObjC runtime.

Swift SVN r20183
2014-07-18 21:39:43 +00:00
Chris Lattner
02999cac51 Reinstate the @ on the @objc attribute. This is largely a revert of r19555 with a few tweaks.
Swift SVN r19706
2014-07-08 21:50:34 +00:00
Joe Groff
03c9f871d2 IRGen/Runtime: Add a "primary generic parameters" count to generic parameter descriptors.
lldb needs this to be able to tell how many generic parameters are actually needed to instantiate a generic type. Fixes <rdar://problem/17425286>.

Swift SVN r19573
2014-07-04 19:34:11 +00:00
Chris Lattner
7a56499d61 Start making @objc not start with an @ sign:
- Change the parser to accept "objc" without an @ sign as a contextual
   keyword, including the dance to handle the general parenthesized case.
 - Update all comments to refer to "objc" instead of "@objc".
 - Update all diagnostics accordingly.
 - Update all tests that fail due to the diagnostics change.
 - Switch the stdlib to use the new syntax.

This does not switch all tests to use the new syntax, nor does it warn about
the old syntax yet.  That will be forthcoming.  Also, this needs a bit of 
refactoring, which will be coming up.



Swift SVN r19555
2014-07-04 05:57:57 +00:00
Michael Gottesman
e269523e48 Reintroduce discriminator bit stripping in the runtime that was reverted
when I undid the array enum change in r19026.

Swift SVN r19028
2014-06-20 02:40:57 +00:00
Michael Gottesman
8de060ff3f Revert "[stdlib] mask unused bits in uniqueness checks"
Revert "[stdlib] Use an enum for ArrayBuffer storage"

This reverts commit r18996.
This reverts commit r18954.

The optimizer is not ready yet to handle this change especially given
the time until Beta 3. After speaking with DaveA, we agreed to revert
this and take such large changes onto private branches until we are sure
that the optimizer is ready to handle them rather than risking
performance regressions due to hitting the "optimization cliff".

Swift SVN r19026
2014-06-20 01:41:04 +00:00
Dave Abrahams
2b82e8988f [stdlib] mask unused bits in uniqueness checks
Also update ArrayBuffer to take advantage of it.

This change allows us to pass a word-sized enum with a native object
reference payload directly to the runtime, without switching on the enum
to unwrap the contents.  Even though that unwrapping was semantically
equivalent to bit masking, it was causing fits in the optimizer.

Swift SVN r18996
2014-06-19 02:32:07 +00:00
John McCall
9f5736880e Basic IR-generation support for checked_cast_addr
in terms of swift_dynamicCast.

Swift SVN r18825
2014-06-12 08:08:20 +00:00
John McCall
9890d8bca0 Add swift_dynamicCast.
This is the most general dynamic cast operation, permitting
arbitary source and destination types and handling arbitrary
changes in representation.  A value of the destination type
is constructed in an address provided by the caller; flags
control the behavior w.r.t. the source value.

Not yet used; probably buggy in various particulars.

Swift SVN r18815
2014-06-12 00:13:13 +00:00
John McCall
a581511069 Permit LLVM's cast operators to be used on Metadata
instances, and add ExistentialTypeMetadata::isClassBounded().

Swift SVN r18814
2014-06-12 00:13:08 +00:00
Joe Groff
ac903bf943 IRGen: Make our use of spare bits in heap objects tagged-pointer-safe.
Don't use spare bits on platforms that use ObjC tagged pointers when an enum payload involves a class-constrained existential, archetype, or ObjC-defined class type. If a payload is of a Swift-defined class type, we can still assume it's a real pointer and use its spare bits. Add an @unsafe_no_objc_tagged_pointer attribute that can be applied to protocols to denote that existentials bounded by that protocol can use spare bits; this is necessary to preserve the layout of bridged Array and Dictionary types, which should not be bound to tagged pointer types in practice (fingers crossed). Fixes <rdar://problem/16270219>.

Swift SVN r18781
2014-06-10 17:07:47 +00:00
John McCall
5e1c15df93 Rationalize the class-instance dynamicCast runtime functions.
dynamicCastClass assumes that the destination type is a
Swift class type.

dynamicCastObjCClass assumes that the destination type is
an ObjC class type (represented as ObjC metadata, not type
metadata).

dynamicCastUnknownClass assumes only that the destination
type is some kind of class.

Swift SVN r18776
2014-06-10 02:11:24 +00:00
John McCall
1f5fa04724 Move the rest of the dynamic-cast code over to Casting.cpp.
Swift SVN r18759
2014-06-09 21:42:30 +00:00
Joe Groff
8c60c4af1f IRGen/Runtime: Emit distinct type metadata for block types.
Blocks need their own type metadata with value witnesses appropriate to the block representation. Fixes <rdar://problem/16918740> and <rdar://problem/16981126>.

Swift SVN r18508
2014-05-21 20:24:11 +00:00
Greg Parker
e2ca13c993 <rdar://16529903> Make UIViewController's nib search find Swift class names.
UIViewController is able to find nib files that have names derived from the 
UIViewController subclass's name. This change modifies UIViewController's 
lookup so it is able to find nib files for Swift class names.


Swift SVN r18470
2014-05-20 23:14:52 +00:00
Doug Gregor
bf8fbfc32e Teach isBridgedToObjectiveC and bridgeToObjectiveC to unwrap implicitly unwrapped optionals.
This allows us to, for example, convert an array of unchecked
optionals of some bridged type T to NSArray. It's a runtime failure if
the unchecked optional contains nil.

This is most of <rdar://problem/16899681>, but I want to improve the
testing and look into the various FIXMEs.


Swift SVN r18106
2014-05-15 16:16:35 +00:00
Dave Zarzycki
f618a98ec4 Runtime: Remove swift_getClassMetadata()
Greg's arclite hack is much better. See also: <rdar://problem/16735599>

Swift SVN r17945
2014-05-12 18:24:24 +00:00
Joe Groff
1dce36edd2 Make 'T.self is U.Type' work.
Fix up all of type-checking, SILGen, IRGen, and the runtime to support checked casts of metatypes. <rdar://problem/16847453>

Swift SVN r17719
2014-05-08 22:55:14 +00:00
Dave Abrahams
1e66774acd [stdlib] swift_isClassOrObjCExistentialImpl fix
If we're going to claim to check for ObjC existential-ness, we should
actually do so.

Swift SVN r17712
2014-05-08 20:31:07 +00:00
John McCall
bafeb84a56 Generate unique type metadata for foreign classes.
Swift SVN r17430
2014-05-05 06:45:42 +00:00
John McCall
c57dac63ae Use the first element of structs and tuples as a source
for extra inhabitants.

For structs in particular, this eliminates a major source
of abstraction penatlies.  For example, an optional struct
containing an object pointer is now represented the same
way as an optional object pointer, which is critical for
correctly importing CF types as Unmanaged<T>!.

In time, we should generalize this to consider all elements
as sources for extra inhabitants, as well as exploiting
spare bits in the representation, but getting the
single-element case right really provides the bulk of the
benefit.

This commit restores r17242 and r17243 with a fix to use
value witnesses that actually forward the right type metadata
down.  We were already generating these value witnesses in
the dependent struct VWT pattern, but I was being too clever
and trying to use the underlying value witness directly.

Swift SVN r17267
2014-05-02 20:17:14 +00:00
Joe Groff
0e6ec12b57 Revert "Use the first element of a struct as a source for"
This reverts commit r17243. We can't just forward the extra inhabitant payloads
from a field, because they will end up receiving metadata for the incorrect
type and crashing.

Swift SVN r17251
2014-05-02 16:22:41 +00:00
John McCall
c0e4242bec Use the first element of a struct as a source for
extra inhabitants.

Obviously this should eventually be generalized to
take from any element, but this is good enough to
give us zero-cost abstraction via single-field structs.

Contains some bugfixes for the tuple-extra-inhabitant
changes as well, because test coverage for optional
structs is obviously quite a bit richer than for
optional tuples.

All of this is leading towards unblocking IRGen for
importing CFStringRef as Unmanaged<CFString>!.

Swift SVN r17243
2014-05-02 10:29:55 +00:00
Joe Groff
71678b4bdf IRGen/Runtime: Expose the 'isBitwiseTakable' bit in the value witness flags.
Set a bit for types that are non-bitwise-takable, and calculate it as part of runtime struct and enum layout. Include 'bitwise takable' as part of the runtime 'is inline' calculation to be consistent with the compile-time policy change in r17008.

Swift SVN r17036
2014-04-29 15:23:14 +00:00
Greg Parker
cdc3953a55 [runtime] Note that libobjc knows about some of ClassMetadata's fields.
Swift SVN r17026
2014-04-29 07:30:47 +00:00
Dave Zarzycki
fae27e3357 Runtime: provide inline friendly isa mask/shift variables for 1.0
<rdar://problem/16663586> provide a global isa mask so v-table dispatch doesn't have to call object_getClass

Swift SVN r16980
2014-04-28 15:33:41 +00:00
Doug Gregor
18c4ef0cf0 Revert r16960, which is causing build failures due to missing _objc_debug_isa_class_mask.
Swift SVN r16978
2014-04-28 13:41:12 +00:00
Dave Zarzycki
e5f7cddded Runtime: provide inline friendly isa mask/shift variables for 1.0
<rdar://problem/16663586> provide a global isa mask so v-table dispatch doesn't have to call object_getClass

Swift SVN r16960
2014-04-28 05:47:55 +00:00
Dave Zarzycki
6da5c57926 Runtime: part 1 of work to enable CoreData
Until we lock down the Swift ABI and ship with the OS, we need to be resilient
in the face of ObjC dynamic subclassing and OS changes. In practice, this means
that we need to have a swift runtime ABI to read the isa out of objects. I've
added it as of r. See: swift_getClassMetadata()

We can and will optimize swift_getClassMetadata into a single instruction once
we lockdown our ABI and ship with the OS.

See also: <rdar://problem/16735599>

Swift SVN r16889
2014-04-26 20:00:08 +00:00
John McCall
63072df530 Add class size and address point fields to class metadata.
I've put these fields on the class object for now, just
so we can at least theoretically update them.  A superclass
that grew left rather than right could maybe even be made
to work with this schema, but probably not.

rdar://16705821

Swift SVN r16880
2014-04-26 10:57:50 +00:00
John McCall
f3d4513721 Turn some 64-bit metadata fields into 32-bit fields.
We really don't need to support individual objects
this large, much less more than 4 billion fields in
a single type.

Also rearrange the fields to bring the instance
size/alignment fields closer to the class header,
just for a minor locality win.

Swift SVN r16879
2014-04-26 09:43:39 +00:00