Commit Graph

262 Commits

Author SHA1 Message Date
Davide Italiano
4b5c2295d1 [RemoteAST] Support for extracting the type/valuea out of an existential.
This will be used in lldb.
<rdar://problem/41546568>
2018-07-30 17:00:23 -07:00
John McCall
db8f23df74 Update the ABI for uniquing foreign type metadata.
- `swift_getForeignTypeMetadata` is now a request/response function.

- The initialization function is now a completion function, and the
  pointer to it has moved into the type descriptor.

- The cache variable is no longer part of the ABI; it's an
  implementation detail of the access function.

- The two points above mean that there is no special header on foreign
  type metadata and therefore that they can be marked constant when
  there isn't something about them that needs to be initialized.

The only foreign-metadata initialization we actually do right now is
of the superclass field of a foreign class, and since that relationship
is a proper DAG, it's not actually possible to have recursive
initialization problems.  But this is the right long-term thing to do,
and it removes one of the last two clients of once-based initialization.
2018-07-29 03:16:35 -04:00
John McCall
d10239313f Reference runtime-only ObjC classes with bare strings.
As part of this, rename TypeMetadataRecordKind to TypeReferenceKind
and consistently give it three bits of storage.

The better modelling of these type references appears to have been
sufficient to make dynamic conformance checks succeed, which is good
but unexpected.
2018-07-27 22:55:22 -04:00
Davide Italiano
5dc41566fb [Reflection] Hoist computation of error existentials to the reader.
<rdar://problem/41546568>
2018-07-26 10:23:31 -07:00
Davide Italiano
055c789915 [Reflection] Hoist computation of metadata/value for opaque existential to the reader.
This will allow re-use of this code in RemoteAST (and, transitively,
in lldb).

<rdar://problem/41546568>
2018-07-25 16:02:46 -07:00
John McCall
dadb51e708 Support in-place value metadata initialization in the runtime. 2018-07-25 03:00:36 -04:00
Doug Gregor
e7998f1846 [ABI] Record unmangled names in Swift protocol descriptors.
Rather than storing a mangled name in a Swift protocol descriptor,
which encodes information that is redundant with the context of the
protocol, store an unmangled name as in nominal type descriptors. Update
the various places where this name is used to extract the demangle
tree from the context descriptors.
2018-07-24 04:25:13 -07:00
Doug Gregor
a54a6d8d7f [ABI] Rework protocol descriptor metadata.
Reimplement protocol descriptors for Swift protocols as a kind of
context descriptor, dropping the Objective-C protocol compatibility
layout. The new protocol descriptors have several advantages over the
current implementation:

* They drop all of the unused fields required for layout-compatibility
  with Objective-C protocols.
* They encode the full requirement signature of the protocol. This
  maintains more information about the protocol itself, including
  (e.g.) correctly encoding superclass requirements.
* They fit within the general scheme of context descriptors, rather than
  being their own thing, which allows us to share more code with
  nominal type descriptors.
* They only use relative pointers, so they’re smaller and can be placed
  in read-only memory

 Implements rdar://problem/38815359.
2018-07-23 22:12:42 -07:00
Doug Gregor
a777c5c6a9 [Metadata reader] Separate ObjC and Swift protocol descriptor reading.
When reading the protocol metadata from existential type metadata,
check the “isObjC” bit and handle the reading of the Objective-C
protocol name (using TargetObjCProtocolPrefix) separately from the reading the name of a Swift protocol (using TargetProtocolDescriptor).
More preparation for separating the layout of these two entities.
2018-07-23 17:08:59 -07:00
Doug Gregor
3464929638 [ABI] Rework existential type metadata to use ProtocolDescriptorRef.
Use ProtocolDescriptorRefs within the runtime representation of
existential type metadata (TargetExistentialTypeMetadata) instead of
bare protocol descriptor pointers. Start rolling out the use of
ProtocolDescriptorRef in a few places in the runtime that touch this
code. Note that we’re not yet establishing any strong invariants on
the TargetProtocolDescriptorRef instances.

While here, replace TargetExistentialTypeMetadata’s hand-rolled pointer 
arithmetic with swift::ABI::TrailingObjects and centralize knowledge of
its layout better.
2018-07-20 20:54:49 -07:00
Joe Groff
e2dd2ff11f Runtime: Handle synthesized decl "related entity" tags.
Clang-importer-synthesized declarations get an extra tag character included in their mangling, which was not being preserved in type context descriptors. This caused runtime lookup for these synthesized types to fail. Fix this by adding the tag information to type context descriptors and teaching the runtime to match it up when fetching metadata by mangled name. Fixes rdar://problem/40878715.
2018-06-28 12:11:12 -07:00
Joe Groff
681a96b45c Runtime: Tolerate unknown metadata kinds.
We want to be able to potentially introduce new metadata kinds in future Swift compilers, so a runtime ought to be able to degrade gracefully in the face of metadata kinds it doesn't know about. Remove attempts to exhaustively switch over metadata kinds and instead treat unknown metadata kinds as opaque.
2018-05-17 15:35:06 -07:00
Sho Ikeda
e71a065e97 [gardening][Reflection][Remote] Replace typedef with using 2018-04-02 13:43:39 +09:00
Joe Groff
57d6772d48 Runtime: Use type context descriptors to unique foreign metadata.
Now that every foreign type has a type context descriptor, we can use that for a uniquing key instead of a dedicated mangled string, saving some code size especially in code that makes heavy use of imported types. rdar://problem/37537241
2018-03-10 10:15:38 -08:00
John McCall
a7c5c80799 Compute class metadata bounds solely from class-descriptor chain information.
Change the "metadata base offset" variable into a "class metadata bounds"
variable that contains the base offset and the +/- bounds on the class.
Link this variable from the class descriptor when the class has a resilient
superclass; otherwise, store the +/- bounds there.  Use this variable to
compute the immediate-members offset for various runtime queries.  Teach the
runtime to fill it in lazily and remove the code to compute it from the
generated code for instantiation.  Identify generic arguments with the start
of the immediate class metadata members / end of the {struct,enum} metadata
header and remove the generic-arguments offset from generic type descriptors.
2018-03-04 02:14:32 -05:00
John McCall
f2bb319bdb Change the pattern of generic class metadata instantiation.
Minimize the generic class metadata template by removing the
class header and base-class members.  Add back the set of
information that's really required for instantiation.
Teach swift_allocateGenericClass how to allocate classes without
superclass metadata.  Reorder generic initialization to establish
a stronger phase-ordering between allocation (the part that doesn't
really care about the generic arguments) and initialization (the
part that really does care about the generic arguments and therefore
might need to be delayed to handle metadata cycles).

A similar thing needs to happen for resilient class relocation.
2018-03-04 00:01:56 -05:00
John McCall
8d93cee012 Add a superclass reference to class descriptors. 2018-02-28 00:38:00 -05:00
John McCall
a53f82449c Clarify which type-descriptor-specific flags apply to which types; NFC. 2018-02-27 16:00:21 -05:00
John McCall
dd99536d31 Move the metadata-pattern header into the type context descriptor.
This is yet another waypoint on the path towards the final
generic-metadata design.  The immediate goal is to make the
pattern a private implementation detail and to give the runtime
more visibility into the allocation and caching of generic types.
2018-02-26 12:10:24 -05:00
John McCall
23fa44e56c Render TypeContextDescriptor into a proper hierarchy; NFC.
The purpose here is to make it easier to add type-specific fields to the
descriptor.
2018-02-20 15:20:32 -05:00
Joe Groff
953dddd5d3 IRGen/Runtime: Allow mangled type refs to embed "symbolic references" to type context descriptors.
This makes resolving mangled names to nominal types in the same module more efficient, and for eventual secrecy improvements, also allows types in the same module to be referenced from mangled typerefs without encoding any source-level name information about them.
2018-02-10 10:43:47 -08:00
Arnold Schwaighofer
d981bb1d96 Mangling: noescape functions will be trivial and no longer compatible with escape function types.
Mangle escapeness as part of the type.

Part of:
SR-5441
rdar://36116691
2018-02-06 08:51:43 -08:00
Davide Italiano
4ab738e13b [MetadataReader] Initialize fields to prevent UB.
hasVtable is not initialized on all paths, so when we read it,
we end up reading a random value. On debug builds, this value
almost always happen to be zero, so we do the right thing. This
isn't necessarily true when the optimizations kick in.

lldb uses metadata reder to resolve types and as result of this
undefined behaviour sometimes gets wrong results back, causing
a bunch of tests to fail in release builds.
2018-02-03 15:19:58 -08:00
Joe Groff
f35cb9d35b Don't use SmallVector in MetadataReader.
Not all of its clients link in the necessary LLVM support libraries.
2018-01-30 12:30:52 -08:00
Joe Groff
a7a3b17597 Replace nominal type descriptors with a hierarchy of context descriptors.
This new format more efficiently represents existing information, while
more accurately encoding important information about nested generic
contexts with same-type and layout constraints that need to be evaluated
at runtime. It's also designed with an eye to forward- and
backward-compatible expansion for ABI stability with future Swift
versions.
2018-01-29 16:19:25 -08:00
Doug Gregor
d0b73b85d0 [Type decoder] Generalize the canonicalization of nominal type decl references.
Various TypeDecoder clients will depend on having the "bare" nominal
type declaration demangled node for looking up nominal type descriptors,
so move the generic argument-stripping code into TypeDecoder.
2018-01-12 15:35:12 -08:00
Doug Gregor
61884f7702 [Type decoder] Rework the builder contract for protocols.
TypeDecoder's interface with its builders already treated protocols as
a type (due to their being mangled as "protocol composition containing
one type"), and intermixed protocols with superclasses when forming
compositions. This makes for some awkwardness when working with
protocol descriptors, which are very much a distinct entity from a
type.

Separate out the notion of a "protocol declaration" (now represented
by the builder-provided BuiltProtocolDecl type) from "a protocol
composition containing a single type", similarly to the way we handle
nominal type declarations. Teach remote mirrors and remote AST to
handle the new contract.
2018-01-09 10:46:31 -08:00
Doug Gregor
e28e856595 [Runtime] Use bare protocol mangling for protocol descriptors.
The mangled name of protocol descriptors was the “protocol composition”
type consisting of a single protocol, which is a little odd. Instead,
use a bare protocol reference (e.g., “6Module5ProtoP”) with the “$S”
prefer to be more in line with nominal type descriptor names while still
making it clear that this is a Swift (not an Objective-C) protocol.
2018-01-09 10:21:55 -08:00
Pavel Yaskevich
fc64e34c8e Merge pull request #13701 from xedin/rdar-36278686
[Runtime/ABI] Optimize number of function metadata ABI endpoints
2018-01-05 21:47:13 -08:00
Doug Gregor
132075f870 [NFC] Move TypeDecoder into the Demangling library.
The remote reflection library has a fantastic utility class, TypeDecoder,
to take a mangled type and form an abstract type from it. Move this facility
into the Demangling library so other clients can use it.
2018-01-05 15:42:45 -08:00
Pavel Yaskevich
0f071848f3 [Runtime/ABI] NFC: Refactor function type metadata generation 2018-01-05 13:38:20 -08:00
Slava Pestov
f18ef548d7 Runtime: Code cleanups based on code review feedback 2018-01-03 19:30:30 -08:00
Slava Pestov
fea92a0214 Runtime: Add support for resilient superclasses
If the nominal type descriptor's resilient superclass flag
is set, the generic parameter offset, vtable start offset
and field offset start offset are all relative to the
start of the class's immedaite members, and not the start
of the class metadata.

Support this by loading the size of the superclass and
adding it to these offsets if the flag is set.
2018-01-02 22:56:49 -08:00
Joe Groff
05fc210be6 Runtime: Compact the uniquing header for foreign metadata records.
We can reduce the uniquing header from 3–4 pointer-sized words down to 1–2 32-bit words + one pointer:

- The initialization function (when present) and name are always emitted into the same binary image, so we can use relative references to shrink these down to 32-bit fields.
- We don't ever simultaneously need the initialization flags and the initialized uniqued pointer. (Keeping the "initialization function" flag bit theoretically lets us turn a "consume" load into a "relaxed" load, but that makes no practical difference on most contemporary architectures.) 12 flag bits Ought To Be Enough For Anyone and lets us reliably tell a valid pointer from a flag set, so overlap the initialization flags with the eventual invasive cache value.

The invasive cache is left inline, since we've decided we're not going to make the rest of type metadata records ever be true-const, so they'll already be sitting on a dirty page. A dynamic linker that was sufficiently Swift-optimized to precalculate the other load-time-initialized entries in metadata could likely precompute the invasive cache value as well.

rdar://problem/22527141
2017-12-20 14:06:01 -08:00
Pavel Yaskevich
cf0c757b1f [RemoteAST] Fix metadata reader to properly read function metadata 2017-11-07 17:56:11 -08:00
Pavel Yaskevich
be3d1af525 [ABI/Runtime] NFC: Rename function metadata arguments to parameters 2017-11-07 12:45:32 -08:00
Pavel Yaskevich
622cc1c64a [ABI/IRGen] Add custom function parameter flags representation for metadata use 2017-11-07 12:45:32 -08:00
Pavel Yaskevich
e9d4a5304e [IRGen] Fix function metadata endpoints to use separate parameter/flags arguments
Switch most general endpoint to be `flags, parameters, parameterFlags, result`,
instead of opaque `void **`, more specialized ones to use follow argument scheme:
`flags, param0, [flags0], ..., paramN, [flagsN], result` and store parameter/flags
information separately in `FunctionCacheEntry::{Key, Data}` as well.
2017-11-07 12:45:32 -08:00
Pavel Yaskevich
9b3739b91d [IRGen] Add parameter flags to function type metadata
Currently only single 'inout' flag has been encoded into function
metadata, these changes extend function metadata to support up to
32 flags per parameter.
2017-11-07 12:45:32 -08:00
Pavel Yaskevich
659c1bc2e0 Revert "[IRGen] Add parameter flags to function type metadata"
This reverts commit b33a6c7cdf.
2017-11-07 00:24:24 -08:00
Pavel Yaskevich
72bf180d0f Revert "[IRGen] Fix function metadata endpoints to use separate parameter/flags arguments"
This reverts commit 728d2a4c2f.
2017-11-07 00:24:22 -08:00
Pavel Yaskevich
aa89c4f4a8 Revert "[ABI/IRGen] Add custom function parameter flags representation for metadata use"
This reverts commit f6b0d2d2cf.
2017-11-07 00:24:21 -08:00
Pavel Yaskevich
4f354a7f09 Revert "[ABI/Runtime] NFC: Rename function metadata arguments to parameters"
This reverts commit 801b35e28c.
2017-11-07 00:24:20 -08:00
Pavel Yaskevich
dedbc0d434 Revert "[RemoteAST] Fix metadata reader to properly read function metadata"
This reverts commit e80455e2d6.
2017-11-07 00:24:18 -08:00
Pavel Yaskevich
e80455e2d6 [RemoteAST] Fix metadata reader to properly read function metadata 2017-11-06 11:16:46 -08:00
Pavel Yaskevich
801b35e28c [ABI/Runtime] NFC: Rename function metadata arguments to parameters 2017-11-06 11:16:46 -08:00
Pavel Yaskevich
f6b0d2d2cf [ABI/IRGen] Add custom function parameter flags representation for metadata use 2017-11-06 11:16:46 -08:00
Pavel Yaskevich
728d2a4c2f [IRGen] Fix function metadata endpoints to use separate parameter/flags arguments
Switch most general endpoint to be `flags, parameters, parameterFlags, result`,
instead of opaque `void **`, more specialized ones to use follow argument scheme:
`flags, param0, [flags0], ..., paramN, [flagsN], result` and store parameter/flags
information separately in `FunctionCacheEntry::{Key, Data}` as well.
2017-11-06 11:16:46 -08:00
Pavel Yaskevich
b33a6c7cdf [IRGen] Add parameter flags to function type metadata
Currently only single 'inout' flag has been encoded into function
metadata, these changes extend function metadata to support up to
32 flags per parameter.
2017-11-06 11:16:46 -08:00
Pavel Yaskevich
13f0abf54f [RemoteAST] Fix metadata reader to properly read tuple type metadata
Because `readMetadata` correctly reads tuple type metadata bounds,
there is no need to call for remote read of the memory allocated
for element types, since they are going to be laid out right after
tuple metadata itself and could be accessed using accessors in
`TargetTupleTypeMetadata`.
2017-11-04 17:52:32 -07:00