Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What language is Swift written in?

Tags:

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.

like image 747
noizetoys Avatar asked Jul 09 '15 18:07

noizetoys


People also ask

Is Swift based on Python?

According to Apple, the primary features of Swift is that it is fast, modern and interactive. Swift's creator Chris Lattner his creation was a result of ideas inspired by different languages such as C#, Ruby, and especially by Python. That's why we can easily find a couple of similarities between Swift and Python.

Is Swift C based language?

Swift is a successor to both the C and Objective-C languages. It includes low-level primitives such as types, flow control, and operators. It also provides object-oriented features such as classes, protocols, and generics, giving Cocoa and Cocoa Touch developers the performance and power they demand.

Is Swift a script language?

Swift is friendly to new programmers. It's an industrial-quality programming language that's as expressive and enjoyable as a scripting language. Writing Swift code in a playground lets you experiment with code and see the results immediately, without the overhead of building and running an app.

Is Swift easier than Python?

Not only does its simple syntax and hand-holding help you develop faster, but it also lives up to its name: As stated on apple.com, Swift is up to 2.6x faster than Objective-C and 8.4x faster than Python.


2 Answers

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).

like image 37
bryanm Avatar answered Sep 28 '22 08:09

bryanm


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.

like image 88
rspeed Avatar answered Sep 28 '22 07:09

rspeed