Commit Graph

95 Commits

Author SHA1 Message Date
Doug Gregor
7d70b704e4 Merge commit '5e11e3f7287427d386636a169c4065c0373931a8' into swift-3-api-guidelines 2016-01-19 23:18:20 -08:00
Dmitri Gribenko
f0d294b714 Merge pull request #884 from practicalswift/apostrophes
[gardening] Replace left/right quotation marks
2016-01-11 20:41:24 -08:00
Slava Pestov
5e1d65c0d1 SILGen: Remove some Builtins that are no longer needed
These were only used by the Sema-synthesized materializeForSet.
2016-01-11 19:55:46 -08:00
practicalswift
4689a22f1b Replace left/right quotation marks with apostrophes in non-quote contexts
* LEFT SINGLE QUOTATION MARK (U+2018) → APOSTROPHE (U+0027)
* RIGHT SINGLE QUOTATION MARK (U+2019) → APOSTROPHE (U+0027)
2016-01-06 12:29:40 +01:00
Max Moiseev
f51e708a8f Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-01-04 12:25:25 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Max Moiseev
d610fa0d1c Merge remote-tracking branch 'origin' into swift-3-api-guidelines 2015-12-10 10:29:52 -08:00
Dmitri Gribenko
feacbc4433 Rename ErrorType to ErrorProtocol 2015-12-09 17:12:19 -08:00
Andrew Trick
a8a49afd9e Add Builtin.isOptional.
There was previously no way to detect a type that is nominally
Optional at runtime. The standard library, namely OutputStream, needs
to handle Optionals specially in order to cirumvent conversion to the
Optional's wrapped type. This should be done with conditional
conformance, but until that feature is available, Builtin.isOptional
will serve as a useful crutch.
2015-12-09 16:06:42 -08:00
Norman S
f7a1b6a546 Fix dynamic typo 2015-12-03 18:27:25 +01:00
Joe Groff
0bf05283d2 Add builtins for atomic load/store operations.
Swift SVN r32952
2015-10-28 23:04:28 +00:00
Andrew Trick
8007dc8cfa Add Builtin.castReference and internal API _unsafeCastReference.
_unsafeCastReference allows casting of any references types, regardless
of whether they are references to objects or class existentials. The
implementation is responsible for converting between representations.

_unsafeCastReference provides a dynamic check to ensure that the source
and dest are both actually references. If not, the implementation will
trap at runtime. Generally, the optimizer can prove that the source
and dest are references, and promote this cast to an
unchecked_ref_cast bitcast. There is no dynamic check that the
references types are compatible.

This differs from unsafeDownCast in two ways:

(1) The source and dest types are not statically typed
AnyObjects. Therefore, unsafeCastReference can be used when the
surrounding code dynamically handles both reference and nonreference
types.

(2) The source and dest also need not dynamically conform to AnyObject.
Either side of the cast may be a class existential. The primary
requirement is that the source and dest refer to the same reference
counted object.

Swift SVN r32588
2015-10-10 03:39:34 +00:00
Arnold Schwaighofer
cc4e41cb3e Add a ispod builtin and _isPOD function to the stdlib
The builtin returns true if the argument type is a POD type: needs no special
handling for copy and destruct operations.

Swift SVN r32242
2015-09-25 22:05:14 +00:00
Joe Groff
2dce54dad5 AST: unexpectedError and errorInMain aren't 'readnone'.
And the SIL optimizer shouldn't DCE them. Noticed while testing rdar://problem/20807523.

Swift SVN r29421
2015-06-17 01:29:49 +00:00
John McCall
b186be429b Catch errors in top-level code.
Swift SVN r28133
2015-05-04 22:15:28 +00:00
John McCall
5c171fd448 Parsing, type-checking, SILGen, and IRGen for try!.
Swift SVN r28085
2015-05-02 08:03:15 +00:00
Andrew Trick
a174aa4dfe Add AST and SILGen support for Builtin.isUnique.
Preparation to fix <rdar://problem/18151694> Add Builtin.checkUnique
to avoid lost Array copies.

This adds the following new builtins:

    isUnique : <T> (inout T[?]) -> Int1
    isUniqueOrPinned : <T> (inout T[?]) -> Int1

These builtins take an inout object reference and return a
boolean. Passing the reference inout forces the optimizer to preserve
a retain distinct from what’s required to maintain lifetime for any of
the reference's source-level copies, because the called function is
allowed to replace the reference, thereby releasing the referent.

Before this change, the API entry points for uniqueness checking
already took an inout reference. However, after full inlining, it was
possible for two source-level variables that reference the same object
to appear to be the same variable from the optimizer's perspective
because an address to the variable was longer taken at the point of
checking uniqueness. Consequently the optimizer could remove
"redundant" copies which were actually needed to implement
copy-on-write semantics. With a builtin, the variable whose reference
is being checked for uniqueness appears mutable at the level of an
individual SIL instruction.

The kind of reference count checking that Builtin.isUnique performs
depends on the argument type:

    - Native object types are directly checked by reading the
      strong reference count:
      (Builtin.NativeObject, known native class reference)

    - Objective-C object types require an additional check that the
      dynamic object type uses native swift reference counting:
      (Builtin.UnknownObject, unknown class reference, class existential)

    - Bridged object types allow the dymanic object type check to be
      bypassed based on the pointer encoding:
      (Builtin.BridgeObject)

Any of the above types may also be wrapped in an optional.  If the
static argument type is optional, then a null check is also performed.

Thus, isUnique only returns true for non-null, native swift object
references with a strong reference count of one.

isUniqueOrPinned has the same semantics as isUnique except that it
also returns true if the object is marked pinned regardless of the
reference count. This allows for simultaneous non-structural
modification of multiple subobjects.

In some cases, the standard library can dynamically determine that it
has a native reference even though the static type is a bridge or
unknown object. Unsafe variants of the builtin are available to allow
the additional pointer bit mask and dynamic class lookup to be
bypassed in these cases:

    isUnique_native : <T> (inout T[?]) -> Int1
    isUniqueOrPinned_native : <T> (inout T[?]) -> Int1

These builtins perform an implicit cast to NativeObject before
checking uniqueness. There’s no way at SIL level to cast the address
of a reference, so we need to encapsulate this operation as part of
the builtin.

Swift SVN r27887
2015-04-28 22:54:24 +00:00
Nadav Rotem
3e8c871c6a Emit calls to swift_willThrow as part of SILGen using a builtin.
Calls to willThrow are marked as read-none so that the optimizer can remove
them. The willThrow builtin is still generated for all throw/rethrow sites,
but I plan to look at this next.

rdar://20356658

Swift SVN r27877
2015-04-28 18:36:22 +00:00
John McCall
513a55544f Add builtins for converting refs <-> Builtin.UnknownObject.
Swift SVN r24419
2015-01-14 19:14:12 +00:00
John McCall
f19c31fbec Add builtins for allocating, projecting, and deallocating
Builtin.UnsafeValueBuffer.

Swift SVN r24043
2014-12-19 22:41:13 +00:00
John McCall
24f41462db Change the materializeForSet callback to take the
value buffer inout, just for better typing.

Swift SVN r24041
2014-12-19 22:41:11 +00:00
John McCall
b6eb086af8 Add a rather hacky builtin function for constructing
a materializeForSet callback from a closure.

Swift SVN r23993
2014-12-17 22:23:26 +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
Joe Groff
e6d97e8de7 Clang importer: Add a zeroing default initializer to imported structs.
If an imported C struct has no __nonnull pointer fields, then we can give a default initializer that zeroes all of its fields. This becomes a requirement when working with partially-imported types like NSDecimal. NSDecimal has bitfields Swift can't see yet, so it's impossible to DI, but the Foundation functions that work with NSDecimal all emit their result by out parameter, and without access to its fields it is impossible to initialize an NSDecimal for use with one of these functions. Implement the initializer using a builtin that gets lowered by IRGen; this is also made necessary by the fact that Swift has only a partial view of the struct, so we can't form a complete zero initializer until we have the definitive type layout from Clang.

Swift SVN r23727
2014-12-05 05:31:22 +00:00
John McCall
b376264442 Bind mark_dependence as Builtin.markDependence.
Swift SVN r23707
2014-12-05 00:38:03 +00:00
Erik Eckstein
54e1071da0 Add a builtin assumeNonNegative.
It returns the argument and specifies that the value is not negative.
It has only an effect if the argument is a load or call.

The effect of this builtin is that for the load/call argument a positive range metadata is created in llvm ir.

I also added a public function _assumeNonNegative(x: Int) -> Int in the stdlib.
To be on the save side, I prefixed it with an underscore. But maybe it makes sense to make it available for all users.



Swift SVN r23582
2014-11-26 09:53:14 +00:00
David Farler
23a86eda49 CmpXChg Builtin: allow 'weak', return {iNN, i1}
CmpXChg builtins now return (T, Bool) to match the LLVM return value.
Turn the tests back on and check extractvalue / inttoptr instructions.

<rdar://problem/17309776> Update modeling of cmpxchg builtin to handle weak-ness and separate success bit

Swift SVN r23104
2014-11-04 21:49:55 +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
Joe Groff
f518ff5003 Remove the dead Builtin.typeof function.
We have syntax for this now.

Swift SVN r22201
2014-09-23 02:56:41 +00:00
Arnold Schwaighofer
16e41ada77 Make Builtin.canBeClass return a tri-state
Replace the true/maybe state that Builtin.canBeClass was returning by a
tri-state (yes, no, maybe) allowing the optimizer to use the definite no
answer.  This removes the need of the sizeof check that we had in
isClassOrObjCExistential. It also removes the need to CSE this function since
in most cases we will be able to instantiate canBeClass to yes or no (vs maybe)
at compile time.

benchmark``````````````,``baserun0``,``optrun2``,``delta,``speedup
ClassArrayGetter```````,``988.00````,``337.00```,``644.00``,````````191.7%
DeltaBlue``````````````,``2429.00```,``1927.00``,``460.00``,````````23.9%
Dictionary`````````````,``1374.00```,``1231.00``,``129.00``,````````10.9%
Havlak`````````````````,``1079.00```,``911.00```,``124.00``,````````13.7%
Rectangles`````````````,``924.00````,``541.00```,``379.00``,````````70.1%

radar://16823238

Swift SVN r21331
2014-08-21 00:55:40 +00:00
Pete Cooper
ce68523178 Add strideof_nonzero to the compiler.
This builtin function generates max(strideof, 1) in IRGen.

Swift SVN r20508
2014-07-24 18:44:42 +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
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
Joe Groff
3fc9f8497f AST: Add Builtin.reinterpretCast.
SILGen lowers this to unchecked_trivial_bit_cast or unchecked_ref_bit_cast based on the semantics of the input and output types, raising an unsupported error if one of the types are address-only.

Swift SVN r19058
2014-06-21 00:23:46 +00:00
John McCall
059ed4cd10 Propagate alignment arguments around through the
slow allocation/deallocation APIs.

Swift SVN r18160
2014-05-16 01:40:36 +00:00
Joe Groff
3b4a9d2ba5 Add a 'conditionallyUnreachable' builtin.
This builtin only becomes unreachable when assert_configuration calls have been folded, allowing library-level checks to become unreachable based on the assert level.

Swift SVN r17322
2014-05-03 19:41:40 +00:00
Joe Groff
1757b709b9 Update comments for 'canBeClass' change.
Swift SVN r17320
2014-05-03 18:37:35 +00:00
Joe Groff
4229342571 Rename 'canBeObjCClass' to 'canBeClass' and have it return true for all classes.
This will enable the D[] <-> B[] conversions to work with all class types.

Swift SVN r17281
2014-05-02 22:04:43 +00:00
Joe Groff
caff7b62d9 IRGen: Expose the array value witnesses as builtins.
Add Builtin.destroyArray, .copyArray, .takeArrayFrontToBack, and .takeArrayBackToFront, which perform bulk destroy/copy/take operations using memcpy/memmove, a loop, or a generic value witness.

Swift SVN r17009
2014-04-29 03:46:58 +00:00
John McCall
6a9ab031c6 Add Builtin.retain and Builtin.release, and
make Builtin.autorelease an unbalanced operation.

Swift SVN r16794
2014-04-25 02:16:02 +00:00
Joe Groff
79bbf17f42 Add Builtin.unreachable().
This will allow stdlib code to explicitly mark branches it knows to be unreachable. Make this work with SIL diagnostics by lowering the builtin to a normal builtin_function_ref/apply in SILGen, and special-case handling the builtin in DCE by removing the apply along with the following dead instructions when we recognize an unreachable block.

Swift SVN r16745
2014-04-24 05:00:00 +00:00
Joe Groff
a65a693ac9 Add an 'autorelease' builtin instead of @asmname-ing objc_autorelease.
Swift SVN r16643
2014-04-22 04:29:33 +00:00
John McCall
8681963bcb A couple of long-overdue renames.
Builtin.ObjectPointer -> Builtin.NativeObject
Builtin.ObjCPointer -> Builtin.UnknownObject

Swift SVN r16634
2014-04-22 00:17:08 +00:00
Arnold Schwaighofer
989d554a45 Add support for an assert_configuration builtin function
This patch adds support for a builtin function assert_configuration that is
replaced by constant progpagation by an appropriate value dependent on a compile
time setting. This replacement can also be disabled when serializing sil for a
library.

Using this mechanism we implement assertions that can  be disabled (or whose
behavior changes) depending on compile time build settings (Debug, Release,
DisableReplacement).

In the standard library we can now write one assert function that uses this
builtin function to provide different compile time selectable runtime behavior.

Example

Assert.swift:

@transparent
func assert<T : LogicValue>(
  condition: @auto_closure () -> T, message: StaticString = StaticString(),

  // Do not supply these parameters explicitly; they will be filled in
  // by the compiler and aren't even present when asserts are disabled
  file: StaticString = __FILE__, line: UWord = __LINE__
) {
  // Only in debug mode.
  if _isDebug() {
    assert(condition().getLogicValue(), message, file, line)
  }
}

AssertCommon.swift:

@transparent
func _isDebug() -> Bool {
  return Int32(Builtin.assert_configuration()) == 0;
}

rdar://16458612

Swift SVN r16472
2014-04-17 22:05:42 +00:00
Chris Lattner
2b7f6b82b4 rename Builtin.move to Builtin.take. This matches the SIL naming better,
and avoids confusion with the C++'11 notion of "move".  'take' destroys 
source, where std::move leaves it destroyable.


Swift SVN r16316
2014-04-14 14:33:30 +00:00
Joe Groff
71404091d4 Add Builtin.fixLifetime as a way for library code to fix object lifetimes.
Lowers directly to the SIL fix_lifetime instruction.

Swift SVN r15637
2014-03-29 22:59:24 +00:00
Joe Groff
98d6fecc99 '.metatype' -> '.Type'
Also, reserve 'type' as a keyword for ensuing syntax changes.

Swift SVN r14375
2014-02-26 04:23:21 +00:00
Joe Groff
a0874ad8d0 Add a Builtin.canBeObjCClass "type trait" builtin.
This builtin returns true for types that might be ObjC class types. We want to use this builtin to optimize away NSArray handling for non-object Array types, so it needs to persist in SIL long enough for specialization to do its thing, but we never actually want to pay a runtime cost for this check, so always lower it to a constant value at IRGen time. Handle this by having canBeObjCClass return a tri-state "yes/maybe/no" result. In SILGen, we only fold away obviously "yes" or "no" cases, and in IRGen, we fold away "maybe" cases as "yes".

The optimizer will need to learn about this builtin too, but that part isn't done yet.

Swift SVN r13980
2014-02-17 07:25:52 +00:00
Greg Parker
95c2fc43f8 Clena up some 32/64 mismatches. Remove some dead posix stubs.
Swift SVN r13144
2014-01-30 09:34:26 +00:00
Joe Groff
c7a8b4f1a5 IRGen: Emit unchecked operations for checked builtins with -disable-all-runtime-checks.
Emit checked arithmetic and conversion instructions as unchecked LLVM IR when runtime checks are disabled.

Swift SVN r12592
2014-01-20 18:01:02 +00:00