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.
就像题目说的,Swift是用什么语言写的?更具体地说,编译器用什么来编写。它是用C语言写的(像其他语言一样)还是其他一些神奇的、未知的、现在的超级语言?或者它是某种奇怪的递归的、启动绑定的Swift版本?奇怪的是,网络在这个问题上保持沉默。
3 个解决方案
#1
32
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.
该源代码刚刚在Github上公开,看来Swift本身主要是用c++编写的,其标准库也是用Swift编写的。
For more info, see this press release from Apple, and the new Swift.org web site.
有关更多信息,请参见苹果的新闻稿和新的Swift.org网站。
#2
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
Swift是在c中实现的。您可以在这里看到一个人分析的概述: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.
随着Swift开源,我想这个问题将在那时得到更完整的答案。
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:
在Swift中,我们只写call_function(someFunction)。但是,Swift编译器不是将调用执行为call_function(&someFunction),而是生成代码:
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:
swift_func_object类型是什么?为了创建这个对象,Swift运行时使用一个名为metadata[N]的全局常量(对于每个函数调用来说,这是惟一的,它将您的func作为泛型类型的参数,所以对于这段代码:
func callf(f: () -> ()) {
f();
}
callf(someFunction);
callf(someFunction);
two constants metadata and metadata2 will be created).
将创建两个常量元数据和元数据)。
A metadata[N]’s structure is kinda this:
元数据[N]的结构是这样的:
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:
最初,metadataN只设置了两个字段:析构函数和类型。第一个是指向一个函数的指针,该函数用于释放使用swift_allocObject()创建的对象的所有内存。后者是对象的类型标识符(用于函数/方法的0x40或“@”),它(以某种方式)被swift_allocObject()用于为我们的func创建一个正确的对象:
swift_allocObject(&metadata2->type, 0x20, 0x7);
Once the func object is created it has the following structure:
一旦创建了func对象,它具有以下结构:
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).
第一个字段是指向相应元数据的指针[N]->类型值,第二个字段似乎是0x4 | 1 < 24 (0x100000004),这表明可能有什么(不知道什么)。function_address是我们真正感兴趣的连接,而self(突然)是指向self的指针(如果我们的对象表示一个普通函数,那么这个字段为NULL)。
#3
-9
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.
它使用Xcode 6中包含的LLVM编译器框架构建,并使用Objective-C运行时,允许C、Objective-C、c++和Swift代码在单个程序中运行。
from swift wikipedia
迅速从*
#1
32
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.
该源代码刚刚在Github上公开,看来Swift本身主要是用c++编写的,其标准库也是用Swift编写的。
For more info, see this press release from Apple, and the new Swift.org web site.
有关更多信息,请参见苹果的新闻稿和新的Swift.org网站。
#2
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
Swift是在c中实现的。您可以在这里看到一个人分析的概述: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.
随着Swift开源,我想这个问题将在那时得到更完整的答案。
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:
在Swift中,我们只写call_function(someFunction)。但是,Swift编译器不是将调用执行为call_function(&someFunction),而是生成代码:
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:
swift_func_object类型是什么?为了创建这个对象,Swift运行时使用一个名为metadata[N]的全局常量(对于每个函数调用来说,这是惟一的,它将您的func作为泛型类型的参数,所以对于这段代码:
func callf(f: () -> ()) {
f();
}
callf(someFunction);
callf(someFunction);
two constants metadata and metadata2 will be created).
将创建两个常量元数据和元数据)。
A metadata[N]’s structure is kinda this:
元数据[N]的结构是这样的:
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:
最初,metadataN只设置了两个字段:析构函数和类型。第一个是指向一个函数的指针,该函数用于释放使用swift_allocObject()创建的对象的所有内存。后者是对象的类型标识符(用于函数/方法的0x40或“@”),它(以某种方式)被swift_allocObject()用于为我们的func创建一个正确的对象:
swift_allocObject(&metadata2->type, 0x20, 0x7);
Once the func object is created it has the following structure:
一旦创建了func对象,它具有以下结构:
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).
第一个字段是指向相应元数据的指针[N]->类型值,第二个字段似乎是0x4 | 1 < 24 (0x100000004),这表明可能有什么(不知道什么)。function_address是我们真正感兴趣的连接,而self(突然)是指向self的指针(如果我们的对象表示一个普通函数,那么这个字段为NULL)。
#3
-9
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.
它使用Xcode 6中包含的LLVM编译器框架构建,并使用Objective-C运行时,允许C、Objective-C、c++和Swift代码在单个程序中运行。
from swift wikipedia
迅速从*