Commit Graph

254 Commits

Author SHA1 Message Date
John McCall
92cf4a6d39 Teach MetadataReader how to handle ObjCClassWrapper metadata. 2016-04-26 13:04:02 -07:00
John McCall
812557ca24 Reorganization within RemoteAST. Basic error propagation.
Initial stabs towards reading foreign class metadata, although
these do not yet successfully resolve the declaration.
2016-04-25 16:57:19 -07:00
David Farler
3a3cafc6bd MetadataReader infrastructure for reading capture descriptors
This starts wiring up capture descriptors to the metadata reader
so we can discern the layout of closures at runtime.

rdar://problem/24989531
2016-04-24 23:47:41 -07:00
Slava Pestov
e3502e9ca9 Runtime: Factor TargetFunctionTypeFlags a bit so that we can use it without a Runtime 2016-04-22 19:03:14 -07:00
John McCall
014bfa10f7 Handle MetadataKind::Class in readMetadata. This can only
come up as the exact value of the metadata kind for classes
where ObjC interop is disabled.
2016-04-21 23:23:47 -07:00
Jordan Rose
bc83940301 Make pointer nullability explicit using Optional.
Implements SE-0055: https://github.com/apple/swift-evolution/blob/master/proposals/0055-optional-unsafe-pointers.md

- Add NULL as an extra inhabitant of Builtin.RawPointer (currently
  hardcoded to 0 rather than being target-dependent).
- Import non-object pointers as Optional/IUO when nullable/null_unspecified
  (like everything else).
- Change the type checker's *-to-pointer conversions to handle a layer of
  optional.
- Use 'AutoreleasingUnsafeMutablePointer<NSError?>?' as the type of error
  parameters exported to Objective-C.
- Drop NilLiteralConvertible conformance for all pointer types.
- Update the standard library and then all the tests.

I've decided to leave this commit only updating existing tests; any new
tests will come in the following commits. (That may mean some additional
implementation work to follow.)

The other major piece that's missing here is migration. I'm hoping we get
a lot of that with Swift 1.1's work for optional object references, but
I still need to investigate.
2016-04-11 20:06:38 -07:00
David Farler
2520d62515 Temporarily suppress -Winvalid-offsetof in Metadata.h
Temporarily turning off this warning while I make the types
standard layout.

https://bugs.swift.org/browse/SR-1155
2016-04-04 15:04:42 -07:00
John McCall
0ffb7278bc Only use metadata patterns for generic types; perform other
initialization in-place on demand.  Initialize parent metadata
references correctly on struct and enum metadata.

Also includes several minor improvements related to relative
pointers that I was using before deciding to simply switch the
parent reference to an absolute reference to get better access
patterns.

Includes a fix since the earlier commit to make enum metadata
writable if they have an unfilled payload size.  This didn't show
up on Darwin because "constant" is currently unenforced there in
global data containing relocations.

This patch requires an associated LLDB change which is being
submitted in parallel.
2016-03-24 15:10:31 -07:00
John McCall
abba7f0c8b Revert "Only use metadata patterns for generic types; perform other"
This reverts commit 41efb3d4d3.
LLDB has too many tendrils into our metadata.
2016-03-23 20:26:43 -07:00
John McCall
41efb3d4d3 Only use metadata patterns for generic types; perform other
initialization in-place on demand.  Initialize parent metadata
references correctly on struct and enum metadata.

Also includes several minor improvements related to relative
pointers that I was using before deciding to simply switch the
parent reference to an absolute reference to get better access
patterns.
2016-03-23 17:04:04 -07:00
David Farler
579ce977f0 swift-reflection-test: Replace Mach IPC with pipes
This is more amenable to cross-platform remote reflection tests.

Also add a new callback to the memory reader: getSymbolAddress,
which will be used for getting the addresses of nominal type
descriptors for concrete and fixed generic types.
2016-03-17 19:02:01 -07:00
David Farler
55efcb35c7 Track parent typerefs for nominal and bound generic types
- Read the Parent pointer out of Class/Value metadata and create
  typerefs for them.
- Add Parent fields to NominalTypeRef and BoundGenericTypeRef.
- Add TypeRef::getSubstMap(), which creates a new generic argument
  map after substitution has taken place on it. This is used to
  continue to burrow into nested value types, where generic type
  parameters may have a different index.
- Use a DenseMap as that generic argument map.
- Unconditionally key the generic argument map with (Depth, Index)
- Clean up ordering and presentation of Index and Depth. In the rest
  of the compiler, Depth comes before Index.
2016-03-17 19:02:01 -07:00
Max Moiseev
1fae0d1325 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-08 12:48:48 -08:00
David Farler
759cd23a86 Use int32_t for external relative direct pointers 2016-03-07 17:43:13 -08:00
David Farler
6f0a399e5c Use offsetof to calculate offset to Descriptor offset 2016-03-07 17:43:12 -08:00
David Farler
ff75058188 Gardening: indentation 2016-03-07 17:43:12 -08:00
Max Moiseev
7fe6916bf6 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-07 12:10:47 -08:00
David Farler
bd6d657411 Get typerefs for existential type metadata 2016-03-04 17:10:40 -08:00
David Farler
5f169b466c Get typerefs for function type metadata 2016-03-04 17:10:40 -08:00
David Farler
04444c44a2 Get typerefs for tuple type metadata, including their elements 2016-03-04 17:10:40 -08:00
David Farler
5196f5b89c Get typerefs for nominal type metadata
and read their generic arguments, if there are any, too.
2016-03-04 17:10:39 -08:00
practicalswift
5b2800225d [gardening] Fix recently introduced typos
Fixed:
* perfomance → performance
* requirments → requirements
* satisified → satisfied
* template template → template
2016-03-04 11:10:50 +01:00
Max Moiseev
cf4bafe9e3 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-03 13:22:03 -08:00
David Farler
502c10fe12 Fix doc comment for GenericParameterDescriptor::Offset
This was a little confusing because it is actually the offset to
the first generic argument and this isn't well documented
anywhere else.
2016-03-03 11:52:31 -08:00
Slava Pestov
b3103ac7b7 Runtime: Fix alignment issue in default witness tables
The size of a protocol's metadata was not a multiple of 8 bytes, so
on 64-bit platforms, the runtime would copy default witnesses from
the wrong address, because IRGen metadata does not add alignment padding,
whereas the in-memory structure does.

Fix this by adding a 32-bit padding field at the end of the protocol
descriptor. Technically this is not necessary on 32-bit, but this keeps
things simpler for now.

The test case for this is a library evolution test exercising resilient
default protocol requirements, but it is not quite ready to go in yet.
2016-03-03 07:36:59 -08:00
David Farler
0ab31065ff Flesh out remote memory reader
Adds a rough sketch of what will be a test harness, currently only supported
on OS X:
- Launch a child process: an executable written in Swift
- Receive the child process's Mach port
- Receive reflection section addresses and the address of a heap instance
  of interest
- Perform field type lookup on the instance remotely (TODO)
2016-03-02 21:25:04 -08:00
David Farler
cd65a8e0b0 Template metadata structures
- Add RuntimeTarget template This will allow for converting between
  metadata structures for native host and remote target architectures.

- Create InProcess and External templates for stored pointers

Add a few more types to abstract pointer access in the runtime
structures but keep native in-process pointer access the same as that
with a plain old pointer type.

There is now a notion of a "stored pointer", which is just the raw value
of the pointer, and the actual pointer type, which is used for loads.
Decoupling these allows us to fork the behavior when looking at metadata
in an external process, but keep things the same for the in-process
case.

There are two basic "runtime targets" that you can use to work with
metadata:

InProcess: Defines the pointer to be trivially a T* and stored as a
uintptr_t. A Metadata * is exactly as it was before, but defined via
AbstractMetadata<InProcess>.

External: A template that requires a target to specify its pointer size.

ExternalPointer: An opaque pointer in another address space that can't
(and shouldn't) be indirected with operator* or operator->.  The memory
reader will fetch the data explicitly.
2016-03-02 21:25:04 -08:00
swiftix
2573782c8b Merge pull request #1357 from swiftix/wip-runtime-calling-convention
Prepare the ground for using a new calling convention for functions from the runtime library
2016-03-01 16:22:37 -08:00
Max Moiseev
488b464f10 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-25 12:21:56 -08:00
John McCall
3ce1ba3e65 Only store the minimal requirements in generic metadata, where
"minimal" is defined as the set of requirements that would be
passed to a function with the type's generic signature that
takes the thick metadata of the parent type as its only argument.
2016-02-25 10:33:33 -08:00
Roman Levenstein
99fd8b6080 Rename some macros based on the PR review comments.
- use  the SWIFT prefix for all macros
- make names of some macros shorter
2016-02-25 05:31:00 -08:00
Roman Levenstein
de3b850ce8 Use more descriptive names for calling conventions.
Rename RuntimeCC into DefaultCC
Rename RuntimeCC1 into RegisterPreservingCC
Remove RuntimeCC0 because it was identical to DefaultCC.
2016-02-25 05:31:00 -08:00
Roman Levenstein
68b6181642 Annotate runtime functions using the newly introduced annotations from runtime/Config.h.
This makes sure that runtime functions use proper calling conventions, get the required visibility, etc.

We annotate the most popular runtime functions in terms of how often they are invoked from Swift code.
- Almost all variants of retain/release functions are annotated to use the new calling convention.
- Some popular non-reference counting functions like swift_getGenericMetadata or swift_dynamicCast are annotated as well.

The set of runtime functions annotated to use the new calling convention should exactly match the definitions in RuntimeFunctions.def!
2016-02-25 05:30:59 -08:00
Max Moiseev
3a3984877a Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-15 15:43:34 -08:00
Joe Groff
32872cb74a IRGen/Runtime: Relative-reference the nominal type descriptor and parent type from metadata.
Save a couple relocations per concrete value type, leaving only the value witness table as an absolute symbol.
2016-02-09 15:17:03 -08:00
Joe Groff
f7291b21ec Runtime: Build with -fvisibility=hidden.
...and explicitly mark symbols we export, either for use by executables or for runtime-stdlib interaction. Until the stdlib supports resilience we have to allow programs to link to these SPI symbols.
2016-02-08 08:06:02 -08:00
David Farler
086000a198 Start the swiftReflection library
- Nearly done: TypeRefs and the mangled name decoder.

- Add the swift-reflection-test tool.
  The field reflection pipeline is roughly:
  - Decode type references
  - Substitute generic parameters
  - Calculate sizes and offsets

  There is currently only one action in the tool, which will test the
  *Decode* part of the pipeline: `dump-reflection-section`. This reads
  the *swift3_reflect section from an object file and dumps the decoded
  type references for all of the stored properties and enum cases in the
  file.
  - TODO: Write tests with various type arrangements to exercise the
    decoder - there are likely some holes in the decoder still since the
    AST mangler is quite rich in its kinds.

  TODO: The next test mode, `dump-field-types`, will do the following:
  1. Launch a swift executable with a canned stopping point
  2. Get the address of a heap object instance of interest
  3. Dump the fully substituted typerefs of all of the stored properties
  or enum case payloads.

  That test mode will be more involved since it will attach to another
  process and need to read from its address space but will test the
  entire out-of-process reflection pipeline in a controlled environment.
  We can maybe take this test a step further, with an option or a new
  test mode, that prints the entire heap reference graph rooted at that
  object of interest, in order to test the ability to detect reference
  cycles, for example.
2016-02-04 18:10:49 -08:00
Slava Pestov
3624b1fc6b Runtime: Support for resiliently adding protocol requirements with default implementations
This is the first patch in a series that will allow new protocol
requirements to be added resiliently, with the runtime filling in
default implementations in witness tables.

First, this adds a new flag to the protocol descriptor indicating
that the protocol is resilient. In this case, there are two
additional fields, MinimumWitnessTableSizeInWords and
DefaultWitnessTableSizeInWords, followed by tail-allocated
default witnesses.

The swift_getGenericWitnessTable() entry point now fills in the
default witnesses from the protocol if the given witness table
template is smaller than the expected witness table size.

This also changes the layout of instantiated witness tables to move
the address point to the end of private data. Previously the private
data came after the requirements, but this meant that adding new
requirements would require sliding the private data at runtime and
accessing it indirectly. It is much simpler to access it from
negative offsets instead.

I updated IRGen to emit the new metadata, but currently all protocols
are flagged as not resilient, and default witnesses are not emitted;
this will come in a subsequent patch once some more plumbing is
in place.

To avoid generating GOT entries for references to protocols defined
in the current module, I had to add some hacks to the existing hack
for this. I'll hopefully clean this up in a principled manner later.
2016-02-04 17:34:55 -08:00
Max Moiseev
61c837209b Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-04 16:13:39 -08:00
David Farler
a6a5ece206 IRGen: Emit type references for remote reflection
- Implement emission of type references for nominal type field
  reflection, using a small custom encoder resulting in packed
  structs, not strings. This will let us embed 7-bit encoded
  32-bit relative offsets directly in the structure (not yet
  hooked in).
- Use the AST Mangler for encoding type references
  Archetypes and internal references were complicating this before, so we
  can take the opportunity to reuse this machinery and avoid unique code
  and new ABI.

Next up: Tests for reading the reflection sections and converting the
demangle tree into a tree of type references.

Todo: For concrete types, serialize the types for associated types of
their conformances to bootstrap the typeref substitution process.

rdar://problem/15617914
2016-02-03 13:52:26 -08:00
Slava Pestov
d887d823ef Re-apply "Protocol conformances can now reference resilient value types"
This comes with a fix for a null pointer dereference in _typeByName()
that would pop with foreign classes that do not have a
NominalTypeDescriptor.

Also, I decided to back out part of the change for now, where the
NominalTypeDescriptor references an accessor function instead of a
pattern, since this broke LLDB, which reaches into the pattern to
get the generic cache.

Soon we will split off the generic cache from the pattern, and at
that time we can change the NominalTypeDescriptor to point at the
cache. But for now, let's avoid needless churn in LLDB by keeping
that part of the setup unchanged.
2016-01-29 00:49:00 -08:00
Slava Pestov
4fd1387b3a Revert "Protocol conformances can now reference resilient value types"
This apparently broke Foundation and LLDB tests. I need to investigate further.

This reverts commit 65dd0e7b93.
2016-01-28 01:03:39 -08:00
Slava Pestov
65dd0e7b93 Protocol conformances can now reference resilient value types
Change conformance records to reference NominalTypeDescriptors instead of
metadata patterns for resilient or generic types.

For a resilient type, we don't know if the metadata is constant or not,
so we can't directly reference either constant metadata or the metadata
template.

Also, whereas previously NominalTypeDescriptors would point to the
metadata pattern, they now point to the metadata accessor function.
This allows the recently-added logic for instantiating concrete types
by name to continue working.

In turn, swift_initClassMetadata_UniversalStrategy() would reach into
the NominalTypeDescriptor to get the pattern out, so that its bump
allocator could be used to allocate ivar tables. Since the pattern is
no longer available this way, we have to pass it in as a parameter.

In the future, we will split off the read-write metadata cache entry
from the pattern; then swift_initClassMetadata_UniversalStrategy() can
just take a pointer to that, since it doesn't actually need anything
else from the pattern.

Since Clang doesn't guarantee alignment for function pointers, I had
to kill the cute trick that packed the NominalTypeKind into the low
bits of the relative pointer to the pattern; instead the kind is now
stored out of line. We could fix this by packing it with some other
field, or keep it this way in case we add new flags later.

Now that generic metadata is instantiated by calling accessor functions,
this change removes the last remaining place that metadata patterns were
referenced from outside the module they were defined in. Now, the layout
of the metadata pattern and the behavior of swift_getGenericMetadata()
is purely an implementation detail of generic metadata accessors.

This patch allows two previously-XFAIL'd tests to pass.
2016-01-28 00:33:10 -08:00
Slava Pestov
19fe31fde9 IRGen: Emit and use accessors for generic type metadata
Instead of directly emitting calls to swift_getGenericMetadata*() and
referencing metadata templates, call a metadata accessor function
corresponding to the UnboundGenericType of the NominalTypeDecl.

The body of this accessor forwards arguments to a runtime metadata
instantiation function, together with the template.

Also, move some code around, so that metadata accesses which are
only done as part of the body of a metadata accessor function are
handled separately in emitTypeMetadataAccessFunction().

Apart from protocol conformances, this means metadata templates are
no longer referenced from outside the module where they were defined.
2016-01-26 16:00:54 -08:00
David Farler
640cefd5b5 IRGen FieldRecordFlags: Use already-defined Ownership type 2016-01-26 09:33:54 -08:00
Slava Pestov
34a4075116 IRGen: Implement resilient enum case numbering
Recent changes added support for resiliently-sized enums, and
enums resilient to changes in implementation strategy.

This patch adds resilient case numbering, fixing the problem
where adding new payload cases would break existing code by
changing the numbering of no-payload cases.

The problem is that internally, enum cases are numbered with payload
cases coming first, followed by no-payload cases. While each list
is itself in declaration order, with new additions coming at the
end, we need to partition it to give us a fast runtime test for
"is this a payload or no-payload case index."

The resilient numbering strategy used here is that the getEnumTag
and destructiveInjectEnumTag value witness functions now take a
tag index in the range [-ElementsWithPayload..ElementsWithNoPayload-1].

Payload elements are numbered in *reverse* declaration order, so
adding new payload cases yields decreasing tag indices, and adding
new no-payload cases yields increasing tag indices, allowing use
sites to be resilient.

This adds the adjustment between 'fragile' and 'resilient' tag
indices in a somewhat unsatisfying manner, because the calculation
could be pushed down further into EnumImplStrategy, simplifying
both the IRGen code and the generated IR. I'll clean this up later.

In the meantime, clean up some other stuff in GenEnum.cpp, mostly
abstracting code that walks cases.
2016-01-21 12:10:57 -08:00
David Farler
0b8a74800f Define the FieldRecord LLVM type
An individual field record for a nominal type consists of:

- 32-bit general purpose flags,
- 32-bit relative offset to the encoded type reference string, or
  32-bit relative offset to the mangled name of the type defined
  in another image, and
- 32-bit relative offset to the field name string.
2016-01-21 11:24:28 -08:00
Joe Groff
638e4b0984 IRGen/Runtime: Use relative addresses in nominal type descriptors.
Decrease the size of nominal type descriptors and make them true-const by relative-addressing the other metadata they need to reference, which should all be included in the same image as the descriptor itself. Relative-referencing string constants exposes a bug in the Apple linker, which crashes when resolving relative relocations to coalesceable symbols (rdar://problem/22674524); work around this for now by revoking the `unnamed_addr`-ness of string constants that we take relative references to. (I haven't tested whether GNU ld or gold also have this problem on Linux; it may be possible to conditionalize the workaround to only apply to Darwin targets for now.)
2016-01-20 14:41:53 -08:00
Doug Gregor
7d70b704e4 Merge commit '5e11e3f7287427d386636a169c4065c0373931a8' into swift-3-api-guidelines 2016-01-19 23:18:20 -08:00
Dmitri Gribenko
5a1f8b25a5 Merge pull request #979 from antonblanchard/powerpc64_merge
Add powerpc64le Linux support
2016-01-15 18:14:45 -08:00