Skip to content

Commit 48837d7

Browse files
committed
---
yaml --- r: 714 b: refs/heads/master c: 2db1f86 h: refs/heads/master v: v3
1 parent 96e523c commit 48837d7

File tree

6 files changed

+96
-23
lines changed

6 files changed

+96
-23
lines changed

[refs]

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: bba4cae1b8b6422a4d9569c7b84139c53834b4eb
2+
refs/heads/master: 2db1f864e23e38d26b6dfb1e0a00829122a6aefd

trunk/src/comp/back/abi.rs

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const int calltup_elt_iterator_args = 5;
3434

3535
const int worst_case_glue_call_args = 7;
3636

37+
const int n_upcall_glues = 7;
38+
3739
//
3840
// Local Variables:
3941
// mode: rust

trunk/src/comp/back/x86.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import lib.llvm.llvm;
22
import lib.llvm.llvm.ModuleRef;
33
import std._str;
4-
import std._int;
54
import std._vec;
5+
import util.common.istr;
66

77
const int wordsz = 4;
88

9-
fn istr(int i) -> str {
10-
ret _int.to_str(i, 10u);
11-
}
12-
139
fn wstr(int i) -> str {
1410
ret istr(i * wordsz);
1511
}
@@ -122,6 +118,13 @@ fn decl_glue(int align, str prefix, str name, vec[str] insns) -> str {
122118
}
123119

124120

121+
fn decl_upcall_glue(int align, str prefix, uint n) -> str {
122+
let int i = n as int;
123+
ret decl_glue(align, prefix,
124+
"rust_upcall_" + istr(i),
125+
upcall_glue(i));
126+
}
127+
125128
fn get_module_asm() -> str {
126129
auto align = 4;
127130
auto prefix = "";
@@ -133,20 +136,15 @@ fn get_module_asm() -> str {
133136

134137
decl_glue(align, prefix,
135138
"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-
}
139+
rust_yield_glue()))
140+
141+
+ _vec.init_fn[str](bind decl_upcall_glue(align, prefix, _),
142+
abi.n_upcall_glues as uint);
146143

147144
ret _str.connect(glues, "\n\n");
148145
}
149146

147+
150148
//
151149
// Local Variables:
152150
// mode: rust

trunk/src/comp/lib/llvm.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,19 @@ type LongLong = i64;
2525
type Long = i32;
2626
type Bool = int;
2727

28-
fn True() -> Bool { ret 1; }
29-
fn False() -> Bool { ret 0; }
28+
29+
const Bool True = 1;
30+
const Bool False = 0;
31+
32+
// Consts for the LLVM CallConv type, pre-cast to uint.
33+
// FIXME: figure out a way to merge these with the native
34+
// typedef and/or a tag type in the native module below.
35+
36+
const uint LLVMCCallConv = 0u;
37+
const uint LLVMFastCallConv = 8u;
38+
const uint LLVMColdCallConv = 9u;
39+
const uint LLVMX86StdcallCallConv = 64u;
40+
const uint LLVMX86FastcallCallConv = 65u;
3041

3142

3243
native mod llvm = llvm_lib {

trunk/src/comp/middle/trans.rs

+62-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import std._vec.rustrt.vbuf;
66
import front.ast;
77
import driver.session;
88
import back.x86;
9+
import back.abi;
10+
11+
import util.common.istr;
912

1013
import lib.llvm.llvm;
1114
import lib.llvm.builder;
@@ -18,8 +21,13 @@ import lib.llvm.llvm.BasicBlockRef;
1821
import lib.llvm.False;
1922
import lib.llvm.True;
2023

24+
type glue_fns = rec(ValueRef activate_glue,
25+
ValueRef yield_glue,
26+
vec[ValueRef] upcall_glues);
27+
2128
type trans_ctxt = rec(session.session sess,
2229
ModuleRef llmod,
30+
@glue_fns glues,
2331
str path);
2432

2533
fn T_nil() -> TypeRef {
@@ -34,9 +42,53 @@ fn T_fn(vec[TypeRef] inputs, TypeRef output) -> TypeRef {
3442
ret llvm.LLVMFunctionType(output,
3543
_vec.buf[TypeRef](inputs),
3644
_vec.len[TypeRef](inputs),
37-
False());
45+
False);
46+
}
47+
48+
fn T_ptr(TypeRef t) -> TypeRef {
49+
ret llvm.LLVMPointerType(t, 0u);
50+
}
51+
52+
fn T_struct(vec[TypeRef] elts) -> TypeRef {
53+
ret llvm.LLVMStructType(_vec.buf[TypeRef](elts),
54+
_vec.len[TypeRef](elts),
55+
False);
56+
}
57+
58+
fn T_opaque() -> TypeRef {
59+
ret llvm.LLVMOpaqueType();
60+
}
61+
62+
fn T_task() -> TypeRef {
63+
ret T_struct(vec(T_int(), // Refcount
64+
T_opaque())); // Rest is opaque for now
65+
}
66+
67+
fn decl_cdecl_fn(ModuleRef llmod, str name,
68+
vec[TypeRef] inputs, TypeRef output) -> ValueRef {
69+
let TypeRef llty = T_fn(inputs, output);
70+
let ValueRef llfn =
71+
llvm.LLVMAddFunction(llmod, _str.buf(name), llty);
72+
llvm.LLVMSetFunctionCallConv(llfn, lib.llvm.LLVMCCallConv);
73+
ret llfn;
74+
}
75+
76+
fn decl_glue(ModuleRef llmod, str s) -> ValueRef {
77+
ret decl_cdecl_fn(llmod, s, vec(T_ptr(T_task())), T_nil());
3878
}
3979

80+
fn decl_upcall(ModuleRef llmod, uint _n) -> ValueRef {
81+
let int n = _n as int;
82+
let str s = "rust_upcall_" + istr(n);
83+
let vec[TypeRef] args =
84+
vec(T_ptr(T_task()), // taskptr
85+
T_int()) // callee
86+
+ _vec.init_elt[TypeRef](T_int(), n as uint);
87+
88+
ret decl_cdecl_fn(llmod, s, args, T_int());
89+
}
90+
91+
4092
type terminator = fn(&trans_ctxt cx, builder b);
4193

4294
fn trans_log(&trans_ctxt cx, builder b, &ast.atom a) {
@@ -71,10 +123,9 @@ fn trans_block(&trans_ctxt cx, ValueRef llfn, &ast.block b, terminator t) {
71123

72124
fn trans_fn(&trans_ctxt cx, &ast._fn f) {
73125
let vec[TypeRef] args = vec();
74-
let TypeRef llty = T_fn(args, T_nil());
75-
let ValueRef llfn =
76-
llvm.LLVMAddFunction(cx.llmod, _str.buf(cx.path), llty);
126+
let ValueRef llfn = decl_cdecl_fn(cx.llmod, cx.path, args, T_nil());
77127
auto term = default_terminate;
128+
78129
trans_block(cx, llfn, f.body, term);
79130
}
80131

@@ -103,7 +154,13 @@ fn trans_crate(session.session sess, ast.crate crate) {
103154

104155
llvm.LLVMSetModuleInlineAsm(llmod, _str.buf(x86.get_module_asm()));
105156

106-
auto cx = rec(sess=sess, llmod=llmod, path="");
157+
auto glues = @rec(activate_glue = decl_glue(llmod, "rust_activate_glue"),
158+
yield_glue = decl_glue(llmod, "rust_yield_glue"),
159+
upcall_glues =
160+
_vec.init_fn[ValueRef](bind decl_upcall(llmod, _),
161+
abi.n_upcall_glues as uint));
162+
163+
auto cx = rec(sess=sess, llmod=llmod, glues=glues, path="");
107164
trans_mod(cx, crate.module);
108165

109166
llvm.LLVMWriteBitcodeToFile(llmod, _str.buf("rust_out.bc"));

trunk/src/comp/util/common.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import std._uint;
2+
import std._int;
23

34
type pos = rec(uint line, uint col);
45
type span = rec(str filename, pos lo, pos hi);
@@ -41,6 +42,10 @@ fn new_str_hash[V]() -> std.map.hashmap[str,V] {
4142
ret std.map.mk_hashmap[str,V](hasher, eqer);
4243
}
4344

45+
fn istr(int i) -> str {
46+
ret _int.to_str(i, 10u);
47+
}
48+
4449
//
4550
// Local Variables:
4651
// mode: rust

0 commit comments

Comments
 (0)