Commit Graph

641 Commits

Author SHA1 Message Date
Dmitri Hrybenko
f40ba621a8 Make integer literals 2048-bit. This covers the whole Float64 range.
Swift SVN r9308
2013-10-14 05:18:55 +00:00
Anna Zaks
1d646a8ba8 Implement overflow checking on integer literals (1 of 2)
- Added 2 new builtins strunc_with_overflow and utrunc_with_overflow
  that perform truncation and produce a compile time error when truncation
  overflows.

- Used these builtins instead of trunc to implement "_convertFromBuiltinIntegerLiteral".

- Currently, the builtins are converted to trunc in IRGen, but we should
  not be IRGenning code that uses them, since all uses of
  "_convertFromBuiltinIntegerLiteral" should be inlined and the arguments
  constant folded.

- I had to change a test and the implementation of operator '~' in the standard library
  because they assumed that '0xFF' is a valid signed Int8. It is questionable if we should
  allow this and if we should treat signed and unsigned integers differently depending on
  how they are spelled (decimal or hexadecimal).

* This patch will be further improved (Ex: will start finding overflows on Int64, better
  deal with '-128' after the negative integer literal patch is committed.)

Swift SVN r9226
2013-10-11 22:19:14 +00:00
Doug Gregor
a012f60633 Make protocol methods generic over <Self>.
Pull the implicit 'Self' associated type out of the protocol and into
an implicitly-declared generic parameter list for the protocol. This
makes all of the methods of a protocol polymorphic, e.g., given

  protocol P {
    typealias Assoc
    func getAssoc() -> Assoc
  }

the type of P.getAssoc is:

  <Self : P> (self : @inout P) -> () -> Self.Assoc

This directly expresses the notion that protocol methods are
polymorphic, even though 'Self' is always implicitly bound. It can be
used to simplify IRgen and some parts of the type checker, as well as
laying more of the groundwork for default definitions within
protocols as well as sundry other improvements to the generics
system.

There are a number of moving parts that needed to be updated in tandem
for this. In no particular order:
  - Protocols always get an implicit generic parameter list, with a
  single generic parameter 'Self' that conforms to the protocol itself.
  - The 'Self' archetype type now knows which protocol it is
  associated with (since we can no longer point it at the Self
  associated type declaration).
  - Protocol methods now get interface types (i.e., canonicalizable
  dependent function types).
  - The "all archetypes" list for a polymorphic function type does not
  include the Self archetype nor its nested types, because they are
  handled implicitly. This avoids the need to rework IRGen's handling
  of archetypes for now.
  - When (de-)serializing a XREF for a function type that has an
  interface type, use the canonicalized interface type, which can be
  meaningfully compared during deserialization (unlike the
  PolymorphicFunctionType we'd otherwise be dealing with).
  - Added a SIL-specific type attribute @sil_self, which extracts the
  'Self' archetype of a protocol, because we can no longer refer to
  the associated type "P.Self". 




Swift SVN r9066
2013-10-09 17:27:58 +00:00
John McCall
a79cee2c54 Revert "Reimplement integer arithmetic overflow checking to use special, error on overflow builtins"
This was causing massive failures at run-time.

This reverts commit 80081db973ccb7100741fea19ce8e8c116fc410f.

Conflicts:
	lib/SILPasses/ConstantPropagation.cpp
	test/SILPasses/constant_propagation.swift
	test/SILPasses/constant_propagation2.sil

Swift SVN r9050
2013-10-09 01:20:39 +00:00
Anna Zaks
ccc1dae7fd Reimplement integer arithmetic overflow checking to use special, error on overflow builtins
After talking to John, Joe, and Dave Z, we've decided that it's best to
specialize each arithmetic builtin that could overflow, instead of calling
a separate generic "staticReport" builtin and passing it enough info to
produce the message. The main advantage of this approach is that it
would be possible for the compiler to customize the message and better
link it to the builtin that overflows. For example, the constants that
participated in the computation could be printed. In addition, less code
will be generated and the compiler could, in the future, automatically
emit the overflow diagnostics/trap at runtime.

This patch introduces new versions of op_with_overflow swift builtins.
Which are lowered to llvm.op_with_overflow builtins in IRGen after the
static diagnostics. If the last argument to the builtins evaluates to true,
the overflow is unintentional. CCP uses the builtins to diagnose the overflow
detectable at compile time. FixedPoint is changed to rely on these in
implementation of primitive arithmetic operations.

Swift SVN r9034
2013-10-08 23:07:56 +00:00
Anna Zaks
a19d7569cc Introduce Builtin.staticReport, which allows compiler diagnostic reporting.
- Introduces the Builtin
- If the first parameter evaluates to '1', the dataflow diagnostics pass produces a diagnostic.
- The Builtin gets cleaned up before IRGen, but not before SIL serialization.

This patch also removes the current, overflow warning and XFAILs one of the tests. The other test is switched to use Builtin.staticReport.

TODO:
 - Utilize the other parameters to the builtin - the Message and IsError flag.
 - Use this Builtin within the stdlib.

Swift SVN r8939
2013-10-05 00:12:23 +00:00
Joe Groff
3d4c1251f1 Rename 'byref' attribute to 'inout'.
Swift SVN r8661
2013-09-25 20:56:52 +00:00
Dmitri Hrybenko
536ed954ad Remove FuncExpr. Add CaptureInfo to FuncDecl. Introduce AnyFunctionRef.
AnyFunctionRef is a universal function reference that can wrap all AST nodes
that represent functions and exposes a common interface to them.  Use it in two
places in SIL where CapturingExpr was used previously.

AnyFunctionRef allows further simplifications in other places, but these will
be done separately.


Swift SVN r8239
2013-09-14 02:15:48 +00:00
Dmitri Hrybenko
c69c79084a Move result typeloc and body result type from FuncExpr to FuncDecl
Swift SVN r8153
2013-09-12 18:40:57 +00:00
Dmitri Hrybenko
3cc01cf7d6 Introduce AbstractFunctionDecl -- a base class for ConstructorDecl,
DestructorDecl, FuncDecl -- and move some of the common concepts and logic
into it

No functionality change.


Swift SVN r8090
2013-09-11 04:04:01 +00:00
Dmitri Hrybenko
cb008946f4 Refactor the way builtin functions are created
Specifically, don't call the FuncDecl constructor everywhere directly.  We only
ever pass a few non-trivial parameters.


Swift SVN r8079
2013-09-10 20:56:48 +00:00
Doug Gregor
330f5fe1e7 For nested archetypes, track the corresponding associated type.
Each nested archetype X.Y corresponds to an associated type named 'Y'
within one of the protocols to which X conforms. Record the associated
type within the archetype itself. When performing type substitutions,
use that associated type to extract the corresponding type witness
rather than looking for the type itself. This is technically more
correct (since we used the type witness for type checking), and a step
toward pulling type substitutions into the AST.



Swift SVN r7624
2013-08-27 17:05:11 +00:00
Doug Gregor
7c84fd5926 Start detangling archetypes from the interface of generic functions.
This breaks the type-canonicalization link between a generic parameter
type and the archetype to which it maps. Generic type parameter types
are now separate entities (that can eventually be canonicalized) from
archetypes (rather than just being sugar).

Most of the front end still traffics in archetypes. As a step away
from this, allow us to type-check the generic parameter list's types
prior to wiring the generic type parameter declarations to archetypes,
using the new "dependent" member type to describe assocaited
types. The archetype builder understands dependent member types and
uses them to map down to associated types when building archetypes.

Once we have assigned archetypes, we revert the dependent identifier
types within the generic parameter list to an un-type-checked state
and do the type checking again in the presence of archetypes, so that
nothing beyond the generic-parameter-list checking code has to deal
with dependent types. We'll creep support out to other dependent types
elsewhere over time.



Swift SVN r7462
2013-08-22 18:07:40 +00:00
Anna Zaks
fe2d8d594d [SIL] Get access to LLVM intrinsic attributes.
And use them to decide if an llvm intrinsic apply instruction can be considered dead.

(This is a hack because it uses LLVM Global context. However, we already use
this approach elsewhere.)

Swift SVN r7404
2013-08-21 17:44:51 +00:00
Anna Zaks
21ce68188d [SIL] Add attributes to swift builtins, specifically, the readnone attribute.
Use the attribute when deciding if a call to a builtin can be eliminated as dead.

Swift SVN r7391
2013-08-21 00:02:25 +00:00
Doug Gregor
e4eaf3fb56 Record depth/index of generic parameters.
Another baby step toward a proper canonical form for polymorphic
function types: generic parameters will eventually be uniquable by
their depth and index.


Swift SVN r7380
2013-08-20 22:44:00 +00:00
Anna Zaks
fae4359d72 [SIL CCP] Fold trunc, sext, and zext builtins.
Swift SVN r7348
2013-08-19 23:57:03 +00:00
Doug Gregor
1ddb34fb71 Factor generic parameters and associated types into their own decl nodes.
Previously, TypeAliasDecl was used for typealiases, generic
parameters, and assocaited types, which is hideous and the source of
much confusion. Factor the latter two out into their own decl nodes,
with a common abstract base for "type parameters", and push these
nodes throughout the frontend.

No real functionality change, but this is a step toward uniquing
polymorphic types, among other things.


Swift SVN r7345
2013-08-19 23:36:58 +00:00
Joe Groff
4316239f5d Kill Builtin.OpaquePointer.
It's not needed by SIL anymore.

Swift SVN r6873
2013-08-03 01:56:06 +00:00
Joe Groff
aec49519c1 Carry noreturn-ness of LLVM intrinsics to Swift builtins.
If an LLVM intrinsic has a noreturn attribute, reflect that into the Swift builtin's function type.

Swift SVN r6787
2013-07-31 23:39:45 +00:00
Doug Gregor
9a5c96a8c1 Introduce basic support for LLVM vectors as builtins.
This adds builtin types Builtin.VecNxT, where N is a natural number
and T is a builtin type, which map down to the LLVM type <N x T>. 

Update varous builtins to support vector arguments, e.g., binary
operations, comparisons, negation. Add InsertElement and
ExtractElement builtins for vectors.

On top of these builtins, add Vec4f and Vec4b structs to the standard
library, which provide 4xFloat and 4xBool vectors, respectively, with
basic support for arithmetic. These are mostly straw men, to be burned
down at our leisure.

Some issues as yet unresolved:
  - Comparisons of Vec4f'ss are producing bogus Vec4b's, which I
  haven't tracked down yet.
  - We still don't support the shuffle builtin, although it should be
  easy
  - More testing!



Swift SVN r5820
2013-06-26 21:16:36 +00:00
Chris Lattner
a206b326ef add support for Builtin.bitcast converting between fp and integer builtin types.
This implements rdar://14193818


Swift SVN r5668
2013-06-19 00:42:57 +00:00
Joe Groff
6133119f8d Add a Builtin.typeof function.
This performs the operation 'x.metatype' performs now. SILGen lowers it using the same logic as for MetatypeExpr--emit a static metatype for value types, or look up a dynamic metatype for class, archetype, and existential types.

Swift SVN r5007
2013-05-01 02:47:56 +00:00
Joe Groff
b761bad088 SILGen: Lower some builtins to SIL.
The value semantics primitives load/move/assign/init/destroy lower trivially to SIL value semantics operators, and the bridge casting operations introduce r/r semantics that should be visible to the ARC optimizer, so move the lowering for these builtins up to SILGen. Add a BUILTIN_SIL_OPERATION metaprogramming macro to Builtins.def, and add a facility similar to IRGen's former SpecializedCallEmission so we can handle builtin call emissions as special cases.

This also sets up the framework for eventually reintroducing special-case handling of known functions like &&, ||, Bool.getLogicValue, Int.convertFromIntegerLiteral, etc. in SILGen.

Swift SVN r4862
2013-04-22 23:05:18 +00:00
Joe Groff
18655c76a0 stdlib: Fix unary - for floats.
+0.0 - ±0.0 == ±0.0, so the correct definition of '-x' is '-0.0 - x'. However, this would be infinitely recursive, so I added an 'fneg' builtin that lowers directly to 'fsub -0.0, %x', and redefined the unary - operators for floats in terms of it.

Swift SVN r4634
2013-04-08 16:38:04 +00:00
Joe Groff
ef1395dcc6 IRGen: Add an 'addressof' builtin.
Returns the address of an lvalue as a RawPointer. The address is only valid at statement scope for logical lvalues.

Swift SVN r3928
2013-02-01 21:13:57 +00:00
Joe Groff
0422897a47 Expose %swift.opaque as a builtin type.
Archetypes and projected existentials have the type %swift.opaque* and not i8*, so I need a corresponding SIL type to be able to model the ProjectExistential operation. We might also end up needing the builtin type for other low-level things down the line.

Swift SVN r3793
2013-01-18 02:24:23 +00:00
Joe Groff
c39e922092 Quell warning about the new 'Half' IITDescriptor.
Swift SVN r3760
2013-01-14 02:57:15 +00:00
Chris Lattner
b58a87c7c4 do an inttoptr/ptrtoint dance to enable atomics on Builtin.RawPointer,
implementing: rdar://12939803 - ER: support atomic cmpxchg/xchg with pointers


Swift SVN r3702
2013-01-07 19:14:01 +00:00
John McCall
9b2a906669 Include Intrinsics.gen at its actual location.
Swift SVN r3683
2013-01-04 22:35:22 +00:00
Chris Lattner
47dbc4051c update for mainline LLVM header file moves.
Swift SVN r3634
2013-01-02 14:38:42 +00:00
Chris Lattner
bb389185c1 move the LLVM IR intrinsics namespace from Builtin.x to Builtin.int_x
(e.g. Builtin.int_trap instead of Builtin.trap) to avoid conflicts between
IR instructions and intrinsics (e.g. trunc and llvm.trunc).


Swift SVN r3621
2012-12-30 10:31:04 +00:00
Chris Lattner
854880b04d simplify some code by using the new implicit TupleTypeElt ctor.
Swift SVN r3620
2012-12-30 10:24:57 +00:00
Chris Lattner
db63db1c6b expose the llvm atomicrmw instruction through the builtin module to swift code,
wrapping up rdar://12939282.


Swift SVN r3619
2012-12-30 09:53:41 +00:00
Chris Lattner
53baaafffb implement support for generating the fence instruction through
Builtin.fence_*, part of rdar://12939282


Swift SVN r3618
2012-12-30 09:36:08 +00:00
Chris Lattner
fd2a4490fa expose the llvm cmpxchg instruction featureset through to the swift Builtin module,
first part of rdar://12939282.


Swift SVN r3617
2012-12-30 09:21:26 +00:00
Chris Lattner
1fd5d9c1da clean up Builtins.def:
- Don't require "OverloadedBuiltinKind::" to be in the invocation
   of a bunch of builtins for their specification of overload info.
   Eliminating this makes the file fit in 80 columns.
 - Eliminate a bunch of classifications that only classify one thing,
   in favor of a "BUILTIN_MISC_OPERATION" dumping ground for all the
   custom stuff.  This eliminates a bunch of boilerplate.

No functionality change.


Swift SVN r3615
2012-12-30 08:03:14 +00:00
Doug Gregor
75c8591487 When lookup finds a generic parameter, treat it like a local declaration, not a member declaration.
Swift SVN r3534
2012-12-18 22:50:49 +00:00
Doug Gregor
be915b9c04 Implement semantic analysis for inherits-from constraints.
This change enables inheritance constraints such as "T : NSObject",
which specifies that the type parameter T must inherit (directly or
indirectly) from NSObject. One can then implicit convert from T to
NSObject and perform (checked) downcasts from an NSObject to a T. With
this, we can type-

IR generation still needs to be updated to handle these implicit
conversions and downcasts. New AST nodes may follow.


Swift SVN r3459
2012-12-12 23:28:19 +00:00
Eli Friedman
56cc6953ee Add a hack to make swift work with llvm trunk.
Swift SVN r3197
2012-11-15 22:28:41 +00:00
John McCall
e7b9ae47fa Defer to the value witness when moving an archetype. This
is really a deficiency in TypeInfo::initializeWithTake, which
is now virtual and not implemented in TypeInfo anymore.  This
fixes rdar://problem/12153619.

While I'm at it, fix an inefficiency in how we were handling
ignored results of generic calls, and add 4 new builtins:
  Builtin.strideof is like sizeof, but guarantees that it
  returns a multiple of the alignment (i.e., like C sizeof,
  it is the appropriate allocation size for members of an
  array).
  Builtin.destroy destroys something "in place";  previously
  this was being simulated by moving and ignoring the result.
  Builtin.allocRaw allocates raw, uninitialized memory, given
  a size and alignment.
  Builtin.deallocRaw deallocates a pointer allocated with
  Builtin.allocRaw;  it must be given the allocated size.

Swift SVN r2720
2012-08-23 05:21:31 +00:00
Eli Friedman
9e06964c8a Make the TypeLoc constructor which takes just a type private, and expose a
static method to call it, to make it more explicit what is happening.  Avoid
using TypeLoc::withoutLoc for function definitions; instead, just use an empty
TypeLoc.



Swift SVN r2606
2012-08-10 02:09:00 +00:00
Eli Friedman
95cb2d6af2 Add builtins to convert between arbitrary retainable pointers,
Builtin.ObjectPointer, and Builtin.RawPointer.  I don't really like the way
these builtins are defined (specifically, the part where messing up
gives a mysterious IRGen error), but it's workable. <rdar://problem/11976323>.



Swift SVN r2585
2012-08-08 00:40:07 +00:00
Doug Gregor
9e8633ac41 Encode and pass all of the archetypes, including derived archetypes,
in SpecializeExpr, so that we have complete substitution and
protocol-conformance information. On the IR generation side, pass
witness tables for all of the archetypes (again, including derived
archetypes) into generic functions, so that we have witness tables for
all of the associated types.

There are at least two major issues:

  (1) This is a terribly inefficient way to pass witness tables for
  associated types. The witness tables for associated types should be
  accessible via the witness tables of their parent. However, we need
  more information in the ASTs here, because there may be additional
  witness tables that will need to be passed for requirements that are
  placed on the associated type by the generic function itself.

  (2) Something about my test triggers a void/non-void verification failure
  in the witness build for an instance function whose abstracted form
  returns an associated type archetype and whose concrete form returns
  an empty struct. See the FIXME in the test.



Swift SVN r2464
2012-07-26 17:52:04 +00:00
Eli Friedman
688138482d Complete the implementation of a basic Vector<T>.
Swift SVN r2457
2012-07-25 22:51:46 +00:00
Doug Gregor
ce1c30f531 Switch the representation of archetype types so that it encodes when
an archetype is a nested type of another archetype. No functionality
change (yet).


Swift SVN r2416
2012-07-23 21:54:31 +00:00
Eli Friedman
d6a4ba90dd Move TypeLocs to a design where a TypeLoc is a struct containing a type plus
location info for that type.  Propagate TypeLocs a bit more through the AST.



Swift SVN r2383
2012-07-20 21:00:30 +00:00
Eli Friedman
361f931a16 Start propgating TypeLocs from the parser into the AST.
Swift SVN r2372
2012-07-19 02:54:28 +00:00
Chris Lattner
79906830d1 fix the build with whatever version of Xc4.4 I'm using.
Swift SVN r2354
2012-07-13 05:19:10 +00:00
Doug Gregor
d684a74ada Implement builtin 'sizeof' and 'alignof' for everthing except archetypes.
Swift SVN r2330
2012-07-10 18:35:48 +00:00