Skip to content

Commit 3bd98a5

Browse files
committed
---
yaml --- r: 726 b: refs/heads/master c: 11e72fd h: refs/heads/master v: v3
1 parent feda419 commit 3bd98a5

File tree

3 files changed

+96
-21
lines changed

3 files changed

+96
-21
lines changed

[refs]

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

trunk/src/comp/front/parser.rs

+4
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ state fn parse_lit(parser p) -> @ast.lit {
150150
p.bump();
151151
ret @ast.lit_bool(b);
152152
}
153+
case (token.LIT_STR(?s)) {
154+
p.bump();
155+
ret @ast.lit_str(s);
156+
}
153157
}
154158
p.err("expected literal");
155159
fail;

trunk/src/comp/middle/trans.rs

+91-20
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,30 @@ import lib.llvm.llvm.BasicBlockRef;
2323
import lib.llvm.False;
2424
import lib.llvm.True;
2525

26+
state obj namegen(mutable int i) {
27+
fn next(str prefix) -> str {
28+
i += 1;
29+
ret prefix + istr(i);
30+
}
31+
}
32+
2633
type glue_fns = rec(ValueRef activate_glue,
2734
ValueRef yield_glue,
2835
ValueRef exit_task_glue,
2936
vec[ValueRef] upcall_glues);
3037

31-
type trans_ctxt = rec(session.session sess,
32-
ModuleRef llmod,
33-
hashmap[str,ValueRef] upcalls,
34-
hashmap[str,ValueRef] fns,
35-
@glue_fns glues,
36-
str path);
38+
state type trans_ctxt = rec(session.session sess,
39+
ModuleRef llmod,
40+
hashmap[str,ValueRef] upcalls,
41+
hashmap[str,ValueRef] fns,
42+
@glue_fns glues,
43+
namegen names,
44+
str path);
3745

38-
type fn_ctxt = rec(ValueRef llfn,
39-
ValueRef lloutptr,
40-
ValueRef lltaskptr,
41-
@trans_ctxt tcx);
46+
state type fn_ctxt = rec(ValueRef llfn,
47+
ValueRef lloutptr,
48+
ValueRef lltaskptr,
49+
@trans_ctxt tcx);
4250

4351
type terminator = fn(@fn_ctxt cx, builder build);
4452

@@ -54,10 +62,27 @@ fn T_nil() -> TypeRef {
5462
ret llvm.LLVMVoidType();
5563
}
5664

57-
fn T_int() -> TypeRef {
65+
fn T_i8() -> TypeRef {
66+
ret llvm.LLVMInt8Type();
67+
}
68+
69+
fn T_i16() -> TypeRef {
70+
ret llvm.LLVMInt16Type();
71+
}
72+
73+
fn T_i32() -> TypeRef {
5874
ret llvm.LLVMInt32Type();
5975
}
6076

77+
fn T_i64() -> TypeRef {
78+
ret llvm.LLVMInt64Type();
79+
}
80+
81+
fn T_int() -> TypeRef {
82+
// FIXME: switch on target type.
83+
ret T_i32();
84+
}
85+
6186
fn T_fn(vec[TypeRef] inputs, TypeRef output) -> TypeRef {
6287
ret llvm.LLVMFunctionType(output,
6388
_vec.buf[TypeRef](inputs),
@@ -124,19 +149,26 @@ fn C_null(TypeRef t) -> ValueRef {
124149
ret llvm.LLVMConstNull(t);
125150
}
126151

127-
fn C_int(int i) -> ValueRef {
152+
fn C_integral(int i, TypeRef t) -> ValueRef {
128153
// FIXME. We can't use LLVM.ULongLong with our existing minimal native
129154
// API, which only knows word-sized args. Lucky for us LLVM has a "take a
130155
// string encoding" version. Hilarious. Please fix to handle:
131156
//
132157
// ret llvm.LLVMConstInt(T_int(), t as LLVM.ULongLong, False);
133158
//
134-
ret llvm.LLVMConstIntOfString(T_int(),
135-
_str.buf(istr(i)), 10);
159+
ret llvm.LLVMConstIntOfString(t, _str.buf(istr(i)), 10);
160+
}
161+
162+
fn C_int(int i) -> ValueRef {
163+
ret C_integral(i, T_int());
136164
}
137165

138-
fn C_str(str s) -> ValueRef {
139-
ret llvm.LLVMConstString(_str.buf(s), _str.byte_len(s), False);
166+
fn C_str(@trans_ctxt cx, str s) -> ValueRef {
167+
auto sc = llvm.LLVMConstString(_str.buf(s), _str.byte_len(s), False);
168+
auto g = llvm.LLVMAddGlobal(cx.llmod, llvm.LLVMTypeOf(sc),
169+
_str.buf(cx.names.next("str")));
170+
llvm.LLVMSetInitializer(g, sc);
171+
ret g;
140172
}
141173

142174
fn C_struct(vec[ValueRef] elts) -> ValueRef {
@@ -192,7 +224,10 @@ fn trans_upcall(@block_ctxt cx, str name, vec[ValueRef] args) -> ValueRef {
192224
llupcall = llvm.LLVMConstPointerCast(llupcall, T_int());
193225

194226
let ValueRef llglue = cx.fcx.tcx.glues.upcall_glues.(n);
195-
let vec[ValueRef] call_args = vec(cx.fcx.lltaskptr, llupcall) + args;
227+
let vec[ValueRef] call_args = vec(cx.fcx.lltaskptr, llupcall);
228+
for (ValueRef a in args) {
229+
call_args += cx.build.ZExtOrBitCast(a, T_int());
230+
}
196231
log "emitting indirect-upcall via " + abi.upcall_glue_name(n);
197232
for (ValueRef v in call_args) {
198233
log "arg: " + lib.llvm.type_to_str(llvm.LLVMTypeOf(v));
@@ -202,15 +237,50 @@ fn trans_upcall(@block_ctxt cx, str name, vec[ValueRef] args) -> ValueRef {
202237
ret cx.build.Call(llglue, call_args);
203238
}
204239

205-
fn trans_log(@block_ctxt cx, &ast.expr e) {
240+
fn trans_expr(@block_ctxt cx, &ast.expr e) -> ValueRef {
206241
alt (e) {
207242
case (ast.expr_lit(?lit)) {
208243
alt (*lit) {
209244
case (ast.lit_int(?i)) {
210-
trans_upcall(cx, "upcall_log_int", vec(C_int(i)));
245+
ret C_int(i);
246+
}
247+
case (ast.lit_uint(?u)) {
248+
ret C_int(u as int);
249+
}
250+
case (ast.lit_char(?c)) {
251+
ret C_integral(c as int, T_i32());
252+
}
253+
case (ast.lit_bool(?b)) {
254+
if (b) {
255+
ret C_integral(1, T_i8());
256+
} else {
257+
ret C_integral(0, T_i8());
258+
}
259+
}
260+
case (ast.lit_str(?s)) {
261+
auto len = (_str.byte_len(s) as int) + 1;
262+
ret trans_upcall(cx, "upcall_new_str",
263+
vec(p2i(C_str(cx.fcx.tcx, s)),
264+
C_int(len)));
265+
}
266+
}
267+
}
268+
}
269+
cx.fcx.tcx.sess.unimpl("expr variant in trans_expr");
270+
fail;
271+
}
272+
273+
fn trans_log(@block_ctxt cx, &ast.expr e) {
274+
alt (e) {
275+
case (ast.expr_lit(?lit)) {
276+
alt (*lit) {
277+
case (ast.lit_str(_)) {
278+
auto v = trans_expr(cx, e);
279+
trans_upcall(cx, "upcall_log_str", vec(v));
211280
}
212281
case (_) {
213-
cx.fcx.tcx.sess.unimpl("literal variant in trans_log");
282+
auto v = trans_expr(cx, e);
283+
trans_upcall(cx, "upcall_log_int", vec(v));
214284
}
215285
}
216286
}
@@ -432,6 +502,7 @@ fn trans_crate(session.session sess, ast.crate crate) {
432502
upcalls = new_str_hash[ValueRef](),
433503
fns = new_str_hash[ValueRef](),
434504
glues = glues,
505+
names = namegen(0),
435506
path = "_rust");
436507

437508
trans_mod(cx, crate.module);

0 commit comments

Comments
 (0)