Commit Graph

1544 Commits

Author SHA1 Message Date
Joe Groff
8ec59e5c11 Runtime: Implementation for generic typed boxes.
Provide new swift_{alloc,dealloc,project}Box2 entry points that allocate, project, and deallocate typed boxes using runtime-instantiated metadata. Give these a new metadata kind, so that external tools recognize the difference and can interpret the metadata appropriately.

Swift SVN r29714
2015-06-26 00:06:17 +00:00
Joe Groff
4a3f39761c Runtime: Suppress warnings about extern "C" symbols with C-incompatible types.
These are a necessary evil until we get __attribute__((swiftcall)) or something like it in Clang. (Or rewrite the runtime in Swift.)

Swift SVN r29713
2015-06-25 23:56:52 +00:00
Joe Groff
9a33c50f04 Runtime: Look through artificial subclasses when generating metatype names.
Fixes rdar://problem/20307995.

Swift SVN r29325
2015-06-05 23:37:33 +00:00
Greg Parker
b9bf94307a [stdlib] Fix some reflection bugs on armv7k.
armv7k can return 16-byte structs in registers, but the Swift side of 
the reflection implementation assumes these structs return in memory.
Adding an intermediating struct for return values with an empty user 
destructor guarantees that they come back in memory.

rdar://21215339


Swift SVN r29274
2015-06-03 06:25:37 +00:00
Ted Kremenek
fa1ba12f22 Revert "Revert "[stdlib] Finish Underscoring the legacy Reflectable protocol""
Swift SVN r29084
2015-05-27 21:49:37 +00:00
Ted Kremenek
1c27893a9f Revert "[stdlib] Finish Underscoring the legacy Reflectable protocol"
Revert this to fix the bots.

Swift SVN r29083
2015-05-27 21:48:28 +00:00
Dave Abrahams
a326cae7d6 [stdlib] Finish Underscoring the legacy Reflectable protocol
Somehow I didn't get an adequate local rebuild and broke the bots!
This fixes it.

Swift SVN r29080
2015-05-27 21:28:07 +00:00
Slava Pestov
d2a5fbd94a Implement reflection on instances of empty and single-payload enums
This change attempts to introduce the functionality without being too
disruptive. After we branch, I want to consolidate some of the runtime
functions and implement this functionality for multi-payload enums
as well, which requires adding new runtime metadata.

Example:

(swift) enum Color { case Red, Green, Blue(Int) }
(swift) print(Color.Red)
REPL.Color.Red
(swift) print(Color.Blue(5))
REPL.Color.Blue(5)

Implements <rdar://problem/18334936>.

Swift SVN r28430
2015-05-11 18:21:39 +00:00
Andrew Trick
23b1186467 Removed _swift_isUniquelyReferenced shims. Don't call these from swift.
Swift SVN r28270
2015-05-07 16:39:28 +00:00
Joe Groff
9b4dc24721 Runtime: (Almost) cleanse of global constructors and destructors.
Configure the runtime to build with -Wglobal-constructors, and Lazy-fy almost everything that gets flagged. (I gave "swift_isaMask" a pass since that's almost definitely hot enough to warrant a static initialization.) Make some improvements to the Lazy wrapper, using aligned_storage to ensure that it's trivially constructed and destructed.

Swift SVN r28199
2015-05-06 04:59:56 +00:00
Joe Groff
7b2a6e5e8d IRGen: Emit the "done" check for Builtin.once inline.
This matches how dispatch_once works in C, dramatically cutting the cost of a global accessor by avoiding the runtime call in the hot path and giving the global a unique branch for the CPU to predict away. For now, only do this for Darwin; non-ObjC platforms don't necessarily expose their "done" value as ABI like ours do.

While we're here, change "once" to take a thin function pointer. We don't ever emit global initializers with context dependencies, and this simplifies the runtime glue between swift_once and dispatch_once/std::call_once a bit.

Swift SVN r28166
2015-05-05 15:35:57 +00:00
Joe Groff
597578b0c0 Fix comments for enum runtime functions.
Thanks Dmitri.

Swift SVN r27812
2015-04-27 19:37:12 +00:00
Joe Groff
4afbe78d59 Runtime: Entry points for dynamic multi-payload enums.
Swift SVN r27791
2015-04-27 00:35:10 +00:00
Joe Groff
0b339b9a46 IRGen: Populate nominal type descriptor metadata for enums.
Store the number of payload and no-payload cases, the case names, and a lazy case type accessor function for enums, like we do for stored properties of structs and classes. This will be useful for multi-payload runtime support, and should also be enough info to hack together a reflection implementation for enums.

For dynamic multi-payload enums to not be ridiculously inefficient, we'll need to track the size of the payload area in the enum, like we do the field offsets of generic structs and classes, so hack off a byte in the payload case count to track the offset of that field in metadata records. 16 million payloads ought to be enough for anyone, right? (and 256 words between the enum metadata's address point and the payload size offset)

Swift SVN r27789
2015-04-27 00:35:04 +00:00
Michael Gottesman
e43570f1f0 Add MetadataKind.def, a header for metaprogramming with MetadataKinds
and use it to update LeaksChecker in a robust way to handle new metadata
kinds.

I also fixed a small typo where the native ErrorType was not included in
the range of non-ObjC isa metadata kinds.

Swift SVN r27718
2015-04-25 19:04:55 +00:00
Joe Groff
00f7520888 IRGen/Runtime: Combine the separate per-convention function metadata caches into one.
We have enough flag bits on function types now to warrant stashing an extra word in the metadata key alongside the arguments and results, so add one, and pack the number of arguments, function convention, and 'throws' bit in there. This lets us merge the separate metadata caches for thick/thin/block/C functions into one, saving a bit of runtime memory, and simplifying a bunch of repetitive code in the runtime and IRGen.

This also fixes a subtle bug we had where the runtime getFunctionTypeMetadata function expected the result argument to be passed in the arguments array, but IRGen was passing it as a separate argument, which would have caused function type metadata to fail to be uniqued by result type.

Swift SVN r27651
2015-04-23 22:58:11 +00:00
Joe Groff
c4ec47ddea IRGen/Runtime: Metadata for @convention(c) function types.
For now, C&P a new metadata cache for C function pointers, like we do for blocks and thin types.

Swift SVN r27595
2015-04-22 18:06:23 +00:00
Joe Groff
fcf535d682 Runtime: Implement ObjC-free metatype dynamic casts.
Swift SVN r27414
2015-04-17 05:30:35 +00:00
Joe Groff
225a0ebd70 Runtime: Non-ObjC-interoperable version of ErrorObject implementation.
This gets the runtime building again with ObjC interop disabled, though the standard library still has issues.

Swift SVN r27326
2015-04-15 20:27:24 +00:00
Joe Groff
c0a2994564 AST: Start printing function types with @convention instead of old attributes.
And update tests to match.

Swift SVN r27262
2015-04-13 22:51:34 +00:00
Joe Groff
41f73d43ae Runtime: Try to make lazy ErrorType-to-NSError bridging thread-safe.
Atomically initialize and load the NSError bridging fields within an ErrorType box so that we do the right thing when two threads concurrently coerce the box to NSError.

Swift SVN r26996
2015-04-05 00:17:34 +00:00
Joe Pamer
980bd819a2 Propagate 'throws' information into function type metadata.
Swift SVN r26956
2015-04-03 21:45:33 +00:00
Chris Lattner
79ed57f9f2 standardize naming of tuples and tuple patterns on "elements".
Previously some parts of the compiler referred to them as "fields",
and most referred to them as "elements".  Use the more generic 'elements'
nomenclature because that's what we refer to other things in the compiler
(e.g. the elements of a bracestmt).

At the same time, make the API better by providing "getElement" consistently
and using it, instead of getElements()[i].

NFC.



Swift SVN r26894
2015-04-02 20:23:49 +00:00
Joe Groff
ff5f87f56b Runtime: Add ExistentialTypeMetadata helpers for taking values.
Add a 'mayTakeValue' function, which returns true if we're allowed to take the value from an existential container (assuming we own the existential container value). This is true for class and opaque existential containers, but not for boxed existentials, where multiple owners may share references to the boxed value. Also add a 'deinitExistentialContainer' function to do any cleanup that must happen after the value has been consumed out of an existential container. Fix up an off-by-one indirection level in the SwiftError implementations of the existing existential container methods.

Swift SVN r26561
2015-03-25 23:02:56 +00:00
Joe Groff
668928cc83 Runtime: Handle ErrorType's layout in ExistentialTypeMetadata accessors.
Teach ExistentialTypeMetadata how to project out the value pointer, type metadata, and protocol witness table from ErrorType containers. NFC yet; the dynamic casting and reflection machinery needs further work to correctly handle ErrorType boxes.

Swift SVN r26509
2015-03-25 00:56:16 +00:00
Joe Groff
c0712e8cb0 Runtime: Reserve some protocol descriptor bits to identify special protocols.
We need to be able to easily recognize ErrorType in the runtime (and AnyObject, which we recognize by strcmp'ing in a few places currently, which is terrible). Carve out a byte in the protocol descriptor and existential type metadata flags where we can store an enumerator to identify interesting protocols and their existential types. While we're here, move the ProtocolDescriptorFlags and ExistentialTypeFlags up to swift/ABI/MetadataValues.h so their layout can be shared easily from IRGen and the runtime. NFC yet (except you're now arbitrarily limited to 16M protocols in a composition instead of 2G, but hopefully nobody will notice).

Swift SVN r26361
2015-03-20 21:06:18 +00:00
Dmitri Hrybenko
350248dae5 Reorganize the directory structure under 'stdlib'
The standard library has grown significantly, and we need a new
directory structure that clearly reflects the role of the APIs, and
allows future growth.

See stdlib/{public,internal,private}/README.txt for more information.

Swift SVN r25876
2015-03-09 05:26:05 +00:00
John McCall
b6bafa5d74 When instantiating a generic class, set up the
correct preconditions for ObjC layout, and write the
computed offsets back to global ivar offset variables
when present.

Swift will use the global variables for accesses to
ivars when it can show that their offsets are
non-dependent.

Fixes a major problem with generic subclasses of ObjC
classes whose dynamic layout does not match the layout
in their @interface.

rdar://19583881

Swift SVN r25536
2015-02-25 23:35:25 +00:00
Nadav Rotem
d214e367d0 Remove the custom memory allocator. It was not effective in accelerating the metadata lookups and Greg and Dave have suggested other solutions for accelerating this call.
Swift SVN r24869
2015-01-31 00:44:17 +00:00
Nadav Rotem
44506fff22 Rename the method. NFC.
Swift SVN r24867
2015-01-31 00:44:15 +00:00
Nadav Rotem
278bc16f43 Rename the method. NFC.
Swift SVN r24866
2015-01-31 00:44:15 +00:00
Nadav Rotem
26261cd1fe Allow the allocator to free memory.
Swift SVN r24865
2015-01-31 00:44:14 +00:00
Nadav Rotem
f2a6e11932 Add a clear() method to delete all links.
Swift SVN r24864
2015-01-31 00:44:13 +00:00
Nadav Rotem
2236ff8153 Use std::distance and add the missing typedefs needed for stl
Swift SVN r24863
2015-01-31 00:44:10 +00:00
Nadav Rotem
0b9a79b38b Add a concurrent bump-ptr memory allocator.
Swift SVN r24702
2015-01-24 01:08:04 +00:00
Nadav Rotem
1e02da9465 Add a concurrent memory bank
Swift SVN r24701
2015-01-24 01:08:04 +00:00
Nadav Rotem
b1ca4bfab5 Move the concurrent utilities to the /include directory
Swift SVN r24699
2015-01-24 01:08:03 +00:00
John McCall
f3dc58667d Improve the typing of materializeForSet callbacks to
use a thin function type.

We still need thin-function-to-RawPointer conversions
for generic code, but that's fixable with some sort of
partial_apply_thin_recoverable instruction.

Swift SVN r24364
2015-01-11 21:13:35 +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
John McCall
3b4e0d307e Intrinsic support for pinning.
Using the intrinsics is obnoxious because I needed them
to return Builtin.NativeObject?, but there's no reasonable
way to safely generate optional types from Builtins.cpp.
Ugh.

Dave and I also decided that there's no need for
swift_tryPin to allow a null object.

Swift SVN r23824
2014-12-10 00:52:48 +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
Joe Groff
27f49cd19d Runtime: Optional-like enums with single-refcounted payloads can share the refcounted value witnesses.
swift_retain and objc_retain both handle nil already, so we can bypass the generic Optional value witnesses when instantiating Optional on a class type, which happens kind of a lot.

Swift SVN r23613
2014-12-02 17:34:36 +00:00
Joe Groff
d752636387 Runtime: Drop in POD value witnesses when generic structs or enums are instantiated.
If, after layout, we recognize that a generic struct or enum instance is POD, we can replace the generic value witnesses in the image with POD ones.

Swift SVN r23609
2014-12-02 03:33:40 +00:00
David Farler
3c4428dd78 Emit unique metadata for functions with inout
References to functions that take inout parameters crash the compiler
because InOutType isn't a "real" type in itself and has no special type
metadata to emit. It merely further qualifies the function's input
types.

For example, we would like to have a unique entry in the cache for:

var f: (T, T) -> ()

and

var f2: (inout T, T) -> ()

For each argument type metadata pointer in the function's input, take
advantage of pointer alignment and mark the lowest bit if it is inout.
Since the metadata cache uses pointers to create the key, this creates a
unique entry while still being able to extract the actual pointer.

This fixes <rdar://problem/17655125>, and a couple of other similar
crashes.

Swift SVN r23557
2014-11-22 22:01:23 +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
Joe Groff
412b1332f9 Runtime: Fix crash in cast to existential metatype.
Handle a null conformances pointer in _conformsToProtocol, and when we want to overwrite a metatype, pass a correct conformances pointer for the existential metatype value.

Swift SVN r23304
2014-11-13 17:36:07 +00:00
Joe Groff
08fe138808 Runtime: Generate a human-understandable name for Any.Type.
Expose this in the stdlib as _typeName(Any.Type) -> String, pending API review.

Swift SVN r23254
2014-11-11 23:38:24 +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
Joe Groff
2475bd1e4f Clean up platform conditionals.
Thanks Jordan!

Swift SVN r23235
2014-11-11 17:05:56 +00:00
Joe Groff
7ee99eb9d3 Runtime: Workaround for rdar://problem/18889711.
LLVM treats 'consume' as 'acquire' on ARM64, emitting an unnecessary barrier.

Swift SVN r23180
2014-11-08 17:17:49 +00:00