23

Well, like the title says, what language is Swift (Apple's language, not the other Swift thing) written in? More specifically, what is the compiler written in. Is it written in C (like every other language) or some other magical, unknown-til-now Super Language? Or is it some strange recursive, boot-strapped version of Swift? The web is strangely quiet on this issue.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
noizetoys
  • 2,923
  • 5
  • 24
  • 15
  • 5
    probably like everything else, it started in C or C++, then eventually became self-hosting – Marc B Jul 09 '15 at 18:29
  • Apple says it is influenced by Obj-C but it is its own proprietary language at this point that shares compiler support with C/C++/Obj-C languages. – david tallon Jul 09 '15 at 18:34

3 Answers3

42

The source code has just been made available on Github, and it appears that Swift itself is primarily written in C++, and its standard library is written in Swift.

For more info, see this press release from Apple, and the new Swift.org web site.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
rspeed
  • 1,612
  • 17
  • 21
  • Any idea why they choose C++ instead of objc++? – Pedro Paulo Amorim Feb 11 '20 at 16:37
  • 2
    @PedroPauloAmorim I can think of two reasons. One: Swift is meant to supplant ObjC in Apple's ecosystem. It would be unwise to create a dependence on something which will eventually be going away. Two: Pure C++ is much more portable. Because of this, Swift can be built on nearly any platform. – rspeed Feb 19 '20 at 01:30
  • new link for press release is [here](https://www.apple.com/newsroom/2015/12/03Apple-Releases-Swift-as-Open-Source/) – Nicki Klein Oct 31 '20 at 21:14
  • @NickiKlein Can you edit my answer to update the link? – rspeed Nov 01 '20 at 01:26
  • negative, says edit queue is full @rspeed – Nicki Klein Apr 13 '21 at 01:34
8

Swift is implemented in C. You can see an overview of one person's analysis here: https://github.com/rodionovd/SWRoute/wiki/Function-hooking-in-Swift

With Swift going open-source, I imagine this question will be answered more completely at that point.

I'll include a portion below, but definitely read the whole analysis if you're interested:

func call_function(f : () -> Int) {
    let b = f()
}

func someFunction() -> Int {
    return 0
}

In Swift we just write call_function(someFunction). But rather than performing the call as call_function(&someFunction), Swift compiler produces the code:

struct swift_func_wrapper *wrapper =  ... /* configure wrapper for someFunction() */
struct swift_func_type_metadata *type_metadata = ... /* information about function's arguments and return type */
call_function(wrapper->trampoline, type_metadata);

A wrapper has the following structure:

struct swift_func_wrapper {
    uint64_t **trampoline_ptr_ptr; // = &trampoline_ptr
    uint64_t *trampoline_ptr;
    struct swift_func_object *object;
}

And what is the swift_func_object type? To create this object, Swift runtime uses a global constant named metadata[N] (which is unique for each function call that takes your func as an argument of a generic type , so for this code:

func callf(f: () -> ()) {
    f();
}
callf(someFunction);
callf(someFunction);

two constants metadata and metadata2 will be created).

A metadata[N]’s structure is kinda this:

struct metadata {
    uint64_t *destructor_func;
    uint64_t *unknown0;
    const char type:1; // I'm not sure about this and padding,
    char padding[7];   // maybe it's just a uint64_t too...
    uint64_t *self; 
}

Initially metadataN has only two fields set: destructor_func and type. The first is a pointer to a function that will be used to deallocate all the memory for an object created with swift_allocObject(). And the latter is the object's type identifer (0x40 or '@' for functions/methods), and is (somehow) used by swift_allocObject() to create a right object for our func:

swift_allocObject(&metadata2->type, 0x20, 0x7);

Once the func object is created it has the following structure:

struct swift_func_object {
    uint64_t *original_type_ptr;
    uint64_t *unknown0;
    uint64_t function_address;
    uint64_t *self;
}

The first field is a pointer to a corresponding metadata[N]->type value, the second one seems to be 0x4 | 1 << 24 (0x100000004) and that's indicates something maybe (dunno what). function_address is what we actually interested in for hooking, and self is (suddenly) a pointer to the self (if our object represents a plain function this field is NULL).

bryanm
  • 355
  • 1
  • 7
-12

It is built with the LLVM compiler framework included in Xcode 6, and uses the Objective-C runtime, allowing C, Objective-C, C++ and Swift code to run within a single program.

from swift wikipedia

TheHuman Wall
  • 207
  • 1
  • 8
  • I haven't seen so many down votes lately.... And yes, it does not answer the question... It 'kind of' answers a few non-asked questions though..... : ( – eharo2 Mar 13 '19 at 03:09