Skip to content

Commit 46e46d0

Browse files
committed
Translate a bunch of the material (lltrans, llasm, abi) from rustboot to rustc, and move files around.
1 parent 2c514f3 commit 46e46d0

File tree

9 files changed

+232
-12
lines changed

9 files changed

+232
-12
lines changed

src/comp/back/abi.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
const int rc_base_field_refcnt = 0;
3+
4+
const int task_field_refcnt = 0;
5+
const int task_field_stk = 2;
6+
const int task_field_runtime_sp = 3;
7+
const int task_field_rust_sp = 4;
8+
const int task_field_gc_alloc_chain = 5;
9+
const int task_field_dom = 6;
10+
const int n_visible_task_fields = 7;
11+
12+
const int dom_field_interrupt_flag = 1;
13+
14+
const int frame_glue_fns_field_mark = 0;
15+
const int frame_glue_fns_field_drop = 1;
16+
const int frame_glue_fns_field_reloc = 2;
17+
18+
const int box_rc_field_refcnt = 0;
19+
const int box_rc_field_body = 1;
20+
21+
const int general_code_alignment = 16;
22+
23+
const int vec_elt_rc = 0;
24+
const int vec_elt_alloc = 1;
25+
const int vec_elt_fill = 2;
26+
const int vec_elt_data = 3;
27+
28+
const int calltup_elt_out_ptr = 0;
29+
const int calltup_elt_task_ptr = 1;
30+
const int calltup_elt_indirect_args = 2;
31+
const int calltup_elt_ty_params = 3;
32+
const int calltup_elt_args = 4;
33+
const int calltup_elt_iterator_args = 5;
34+
35+
const int worst_case_glue_call_args = 7;
36+
37+
//
38+
// Local Variables:
39+
// mode: rust
40+
// fill-column: 78;
41+
// indent-tabs-mode: nil
42+
// c-basic-offset: 4
43+
// buffer-file-coding-system: utf-8-unix
44+
// compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
45+
// End:
46+
//

src/comp/back/x86.rs

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import lib.llvm.llvm;
2+
import lib.llvm.llvm.ModuleRef;
3+
import std._str;
4+
import std._int;
5+
import std._vec;
6+
7+
const int wordsz = 4;
8+
9+
fn istr(int i) -> str {
10+
ret _int.to_str(i, 10u);
11+
}
12+
13+
fn wstr(int i) -> str {
14+
ret istr(i * wordsz);
15+
}
16+
17+
fn save_callee_saves() -> vec[str] {
18+
ret vec("pushl %ebp",
19+
"pushl %edi",
20+
"pushl %esi",
21+
"pushl %ebx");
22+
}
23+
24+
fn restore_callee_saves() -> vec[str] {
25+
ret vec("popl %ebx",
26+
"popl %esi",
27+
"popl %edi",
28+
"popl %ebp");
29+
}
30+
31+
fn load_esp_from_rust_sp() -> vec[str] {
32+
ret vec("movl " + wstr(abi.task_field_rust_sp) + "(%edx), %esp");
33+
}
34+
35+
fn load_esp_from_runtime_sp() -> vec[str] {
36+
ret vec("movl " + wstr(abi.task_field_runtime_sp) + "(%edx), %esp");
37+
}
38+
39+
fn store_esp_to_rust_sp() -> vec[str] {
40+
ret vec("movl %esp, " + wstr(abi.task_field_rust_sp) + "(%edx)");
41+
}
42+
43+
fn store_esp_to_runtime_sp() -> vec[str] {
44+
ret vec("movl %esp, " + wstr(abi.task_field_runtime_sp) + "(%edx)");
45+
}
46+
47+
fn rust_activate_glue() -> vec[str] {
48+
ret vec("movl 4(%esp), %edx # edx = rust_task")
49+
+ save_callee_saves()
50+
+ store_esp_to_runtime_sp()
51+
+ load_esp_from_rust_sp()
52+
53+
// This 'add' instruction is a bit surprising.
54+
// See lengthy comment in boot/be/x86.ml activate_glue.
55+
+ vec("addl $20, " + wstr(abi.task_field_rust_sp) + "(%edx)")
56+
57+
+ restore_callee_saves()
58+
+ vec("ret");
59+
}
60+
61+
fn rust_yield_glue() -> vec[str] {
62+
ret vec("movl 0(%esp), %edx # edx = rust_task")
63+
+ load_esp_from_rust_sp()
64+
+ save_callee_saves()
65+
+ store_esp_to_rust_sp()
66+
+ load_esp_from_runtime_sp()
67+
+ restore_callee_saves()
68+
+ vec("ret");
69+
}
70+
71+
fn upcall_glue(int n_args) -> vec[str] {
72+
73+
/*
74+
* 0, 4, 8, 12 are callee-saves
75+
* 16 is retpc
76+
* 20 is taskptr
77+
* 24 is callee
78+
* 28 .. (7+i) * 4 are args
79+
*/
80+
81+
fn copy_arg(uint i) -> str {
82+
auto off = wstr(7 + (i as int));
83+
auto m = vec("movl " + off + "(%ebp),%edx",
84+
"movl %edx," + off + "(%esp)");
85+
ret _str.connect(m, "\n\t");
86+
}
87+
88+
auto carg = copy_arg;
89+
90+
ret
91+
save_callee_saves()
92+
93+
+ vec("movl %esp, %ebp # ebp = rust_sp",
94+
"movl 20(%esp), %edx # edx = rust_task")
95+
96+
+ store_esp_to_rust_sp()
97+
+ load_esp_from_runtime_sp()
98+
99+
+ vec("subl $" + wstr(n_args + 1) + ", %esp # esp -= args",
100+
"andl $~0xf, %esp # align esp down",
101+
"movl %edx, (%esp) # arg[0] = rust_task ")
102+
103+
+ _vec.init_fn[str](carg, n_args as uint)
104+
105+
+ vec("movl 24(%ebp), %edx # edx = callee",
106+
"call *%edx # call *%edx",
107+
"movl 20(%ebp), %edx # edx = rust_task")
108+
109+
+ load_esp_from_rust_sp()
110+
+ restore_callee_saves()
111+
+ vec("ret");
112+
113+
}
114+
115+
116+
fn decl_glue(int align, str prefix, str name, vec[str] insns) -> str {
117+
auto sym = prefix + name;
118+
ret "\t.globl " + sym + "\n" +
119+
"\t.balign " + istr(align) + "\n" +
120+
sym + ":\n" +
121+
"\t" + _str.connect(insns, "\n\t");
122+
}
123+
124+
125+
fn get_module_asm() -> str {
126+
auto align = 4;
127+
auto prefix = "";
128+
129+
auto glues =
130+
vec(decl_glue(align, prefix,
131+
"rust_activate_glue",
132+
rust_activate_glue()),
133+
134+
decl_glue(align, prefix,
135+
"rust_yield_glue",
136+
rust_yield_glue()));
137+
138+
let int i = 0;
139+
let int n_upcall_glues = 7;
140+
while (i < n_upcall_glues) {
141+
glues += decl_glue(align, prefix,
142+
"rust_upcall_" + istr(i),
143+
upcall_glue(i));
144+
i += 1;
145+
}
146+
147+
ret _str.connect(glues, "\n\n");
148+
}
149+
150+
//
151+
// Local Variables:
152+
// mode: rust
153+
// fill-column: 78;
154+
// indent-tabs-mode: nil
155+
// c-basic-offset: 4
156+
// buffer-file-coding-system: utf-8-unix
157+
// compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
158+
// End:
159+
//

src/comp/driver/rustc.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// -*- rust -*-
22

3-
import fe.parser;
4-
import fe.token;
5-
import me.trans;
3+
import front.parser;
4+
import front.token;
5+
import middle.trans;
66

77
fn main(vec[str] args) {
88

@@ -16,7 +16,6 @@ fn main(vec[str] args) {
1616
for (str filename in args) {
1717
if (i > 0) {
1818
auto p = parser.new_parser(sess, filename);
19-
log "opened file: " + filename;
2019
auto crate = parser.parse_crate(p);
2120
trans.trans_crate(sess, crate);
2221
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/comp/me/trans.rs renamed to src/comp/middle/trans.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import std._vec;
33
import std._str.rustrt.sbuf;
44
import std._vec.rustrt.vbuf;
55

6-
import fe.ast;
6+
import front.ast;
77
import driver.session;
8+
import back.x86;
89

910
import lib.llvm.llvm;
1011
import lib.llvm.builder;
@@ -36,10 +37,12 @@ fn T_fn(vec[TypeRef] inputs, TypeRef output) -> TypeRef {
3637
False());
3738
}
3839

40+
type terminator = fn(&trans_ctxt cx, builder b);
41+
3942
fn trans_log(&trans_ctxt cx, builder b, &ast.atom a) {
4043
}
4144

42-
fn trans_stmt(&trans_ctxt cx, builder b, &ast.stmt s) {
45+
fn trans_stmt(&trans_ctxt cx, builder b, &ast.stmt s, terminator t) {
4346
alt (s) {
4447
case (ast.stmt_log(?a)) {
4548
trans_log(cx, b, *a);
@@ -50,23 +53,29 @@ fn trans_stmt(&trans_ctxt cx, builder b, &ast.stmt s) {
5053
}
5154
}
5255

53-
fn trans_block(&trans_ctxt cx, ValueRef llfn, &ast.block b) {
56+
fn default_terminate(&trans_ctxt cx, builder b) {
57+
b.RetVoid();
58+
}
59+
60+
fn trans_block(&trans_ctxt cx, ValueRef llfn, &ast.block b, terminator t) {
5461
let BasicBlockRef llbb =
5562
llvm.LLVMAppendBasicBlock(llfn, _str.buf(""));
5663
let BuilderRef llbuild = llvm.LLVMCreateBuilder();
5764
llvm.LLVMPositionBuilderAtEnd(llbuild, llbb);
5865
auto bld = builder(llbuild);
5966
for (@ast.stmt s in b) {
60-
trans_stmt(cx, bld, *s);
67+
trans_stmt(cx, bld, *s, t);
6168
}
69+
t(cx, bld);
6270
}
6371

6472
fn trans_fn(&trans_ctxt cx, &ast._fn f) {
6573
let vec[TypeRef] args = vec();
6674
let TypeRef llty = T_fn(args, T_nil());
6775
let ValueRef llfn =
6876
llvm.LLVMAddFunction(cx.llmod, _str.buf(cx.path), llty);
69-
trans_block(cx, llfn, f.body);
77+
auto term = default_terminate;
78+
trans_block(cx, llfn, f.body, term);
7079
}
7180

7281
fn trans_item(&trans_ctxt cx, &str name, &ast.item item) {
@@ -92,6 +101,8 @@ fn trans_crate(session.session sess, ast.crate crate) {
92101
llvm.LLVMModuleCreateWithNameInContext(_str.buf("rust_out"),
93102
llvm.LLVMGetGlobalContext());
94103

104+
llvm.LLVMSetModuleInlineAsm(llmod, _str.buf(x86.get_module_asm()));
105+
95106
auto cx = rec(sess=sess, llmod=llmod, path="");
96107
trans_mod(cx, crate.module);
97108

src/comp/rustc.rc

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@
33

44
use std;
55

6-
mod fe {
6+
mod front {
77
mod ast;
88
mod lexer;
99
mod parser;
1010
mod token;
1111
}
1212

13-
mod me {
13+
mod middle {
1414
mod trans;
1515
}
1616

17+
mod back {
18+
mod abi;
19+
mod x86;
20+
}
21+
1722
mod driver {
1823
mod rustc;
1924
mod session;
@@ -24,7 +29,7 @@ mod util {
2429
}
2530

2631
auth driver.rustc.main = state;
27-
auth me.trans = unsafe;
32+
auth middle.trans = unsafe;
2833

2934

3035
mod lib {

0 commit comments

Comments
 (0)