Skip to content

Commit 8a94498

Browse files
committed
---
yaml --- r: 727 b: refs/heads/master c: 5a4cb3e h: refs/heads/master i: 725: feda419 723: 810c1a9 719: 00181f8 v: v3
1 parent 3bd98a5 commit 8a94498

File tree

4 files changed

+177
-38
lines changed

4 files changed

+177
-38
lines changed

[refs]

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

trunk/src/comp/front/parser.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,13 @@ state fn parse_prefix_expr(parser p) -> @ast.expr {
250250
alt (p.peek()) {
251251

252252
case (token.NOT) {
253+
p.bump();
253254
auto e = parse_prefix_expr(p);
254255
ret @ast.expr_unary(ast.not, e);
255256
}
256257

257258
case (token.TILDE) {
259+
p.bump();
258260
auto e = parse_prefix_expr(p);
259261
ret @ast.expr_unary(ast.bitnot, e);
260262
}
@@ -263,11 +265,13 @@ state fn parse_prefix_expr(parser p) -> @ast.expr {
263265
alt (b) {
264266

265267
case (token.MINUS) {
268+
p.bump();
266269
auto e = parse_prefix_expr(p);
267270
ret @ast.expr_unary(ast.neg, e);
268271
}
269272

270273
case (token.STAR) {
274+
p.bump();
271275
auto e = parse_prefix_expr(p);
272276
ret @ast.expr_unary(ast.deref, e);
273277
}
@@ -298,19 +302,16 @@ state fn parse_binops(parser p,
298302
auto more = true;
299303
while (more) {
300304
more = false;
301-
auto t = p.peek();
302-
alt (t) {
303-
case (token.BINOP(?op)) {
304-
for (tup(token.binop, ast.binop) pair in ops) {
305+
for (tup(token.binop, ast.binop) pair in ops) {
306+
alt (p.peek()) {
307+
case (token.BINOP(?op)) {
305308
if (pair._0 == op) {
309+
p.bump();
306310
e = @ast.expr_binary(pair._1, e, sub(p));
307311
more = true;
308-
t = p.peek();
309312
}
310313
}
311314
}
312-
case (_) {
313-
}
314315
}
315316
}
316317
ret e;
@@ -324,12 +325,11 @@ state fn parse_binary_exprs(parser p,
324325
auto more = true;
325326
while (more) {
326327
more = false;
327-
auto t = p.peek();
328328
for (tup(token.token, ast.binop) pair in ops) {
329-
if (pair._0 == t) {
329+
if (pair._0 == p.peek()) {
330+
p.bump();
330331
e = @ast.expr_binary(pair._1, e, sub(p));
331332
more = true;
332-
t = p.peek();
333333
}
334334
}
335335
}

trunk/src/comp/lib/llvm.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ const uint LLVMColdCallConv = 9u;
4141
const uint LLVMX86StdcallCallConv = 64u;
4242
const uint LLVMX86FastcallCallConv = 65u;
4343

44+
// Consts for the LLVM IntPredicate type, pre-cast to uint.
45+
// FIXME: as above.
46+
47+
const uint LLVMIntEQ = 32;
48+
const uint LLVMIntNE = 33;
49+
const uint LLVMIntUGT = 34;
50+
const uint LLVMIntUGE = 35;
51+
const uint LLVMIntULT = 36;
52+
const uint LLVMIntULE = 37;
53+
const uint LLVMIntSGT = 38;
54+
const uint LLVMIntSGE = 39;
55+
const uint LLVMIntSLT = 40;
56+
const uint LLVMIntSLE = 41;
57+
58+
4459
native mod llvm = llvm_lib {
4560

4661
type ModuleRef;
@@ -626,7 +641,7 @@ native mod llvm = llvm_lib {
626641
TypeRef DestTy, sbuf Name) -> ValueRef;
627642

628643
/* Comparisons */
629-
fn LLVMBuildICmp(BuilderRef B, IntPredicate Op,
644+
fn LLVMBuildICmp(BuilderRef B, uint Op,
630645
ValueRef LHS, ValueRef RHS,
631646
sbuf Name) -> ValueRef;
632647
fn LLVMBuildFCmp(BuilderRef B, RealPredicate Op,
@@ -981,7 +996,7 @@ obj builder(BuilderRef B) {
981996

982997

983998
/* Comparisons */
984-
fn ICmp(IntPredicate Op, ValueRef LHS, ValueRef RHS) -> ValueRef {
999+
fn ICmp(uint Op, ValueRef LHS, ValueRef RHS) -> ValueRef {
9851000
ret llvm.LLVMBuildICmp(B, Op, LHS, RHS, _str.buf(""));
9861001
}
9871002

trunk/src/comp/middle/trans.rs

+149-25
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ fn C_integral(int i, TypeRef t) -> ValueRef {
159159
ret llvm.LLVMConstIntOfString(t, _str.buf(istr(i)), 10);
160160
}
161161

162+
fn C_bool(bool b) -> ValueRef {
163+
if (b) {
164+
ret C_integral(1, T_i8());
165+
} else {
166+
ret C_integral(0, T_i8());
167+
}
168+
}
169+
162170
fn C_int(int i) -> ValueRef {
163171
ret C_integral(i, T_int());
164172
}
@@ -237,33 +245,148 @@ fn trans_upcall(@block_ctxt cx, str name, vec[ValueRef] args) -> ValueRef {
237245
ret cx.build.Call(llglue, call_args);
238246
}
239247

248+
fn trans_lit(@block_ctxt cx, &ast.lit lit) -> ValueRef {
249+
alt (lit) {
250+
case (ast.lit_int(?i)) {
251+
ret C_int(i);
252+
}
253+
case (ast.lit_uint(?u)) {
254+
ret C_int(u as int);
255+
}
256+
case (ast.lit_char(?c)) {
257+
ret C_integral(c as int, T_i32());
258+
}
259+
case (ast.lit_bool(?b)) {
260+
ret C_bool(b);
261+
}
262+
case (ast.lit_str(?s)) {
263+
auto len = (_str.byte_len(s) as int) + 1;
264+
ret trans_upcall(cx, "upcall_new_str",
265+
vec(p2i(C_str(cx.fcx.tcx, s)),
266+
C_int(len)));
267+
}
268+
}
269+
}
270+
271+
fn trans_unary(@block_ctxt cx, ast.unop op, &ast.expr e) -> ValueRef {
272+
alt (op) {
273+
case (ast.bitnot) {
274+
ret cx.build.Not(trans_expr(cx, e));
275+
}
276+
case (ast.not) {
277+
ret cx.build.And(C_bool(true),
278+
cx.build.Not(trans_expr(cx, e)));
279+
}
280+
case (ast.neg) {
281+
// FIXME: switch by signedness.
282+
ret cx.build.Neg(trans_expr(cx, e));
283+
}
284+
285+
}
286+
cx.fcx.tcx.sess.unimpl("expr variant in trans_unary");
287+
fail;
288+
}
289+
290+
fn trans_binary(@block_ctxt cx, ast.binop op,
291+
&ast.expr a, &ast.expr b) -> ValueRef {
292+
alt (op) {
293+
case (ast.add) {
294+
ret cx.build.Add(trans_expr(cx, a), trans_expr(cx, b));
295+
}
296+
297+
case (ast.sub) {
298+
ret cx.build.Sub(trans_expr(cx, a), trans_expr(cx, b));
299+
}
300+
301+
case (ast.mul) {
302+
// FIXME: switch by signedness.
303+
ret cx.build.Mul(trans_expr(cx, a), trans_expr(cx, b));
304+
}
305+
306+
case (ast.div) {
307+
// FIXME: switch by signedness.
308+
ret cx.build.SDiv(trans_expr(cx, a), trans_expr(cx, b));
309+
}
310+
311+
case (ast.rem) {
312+
// FIXME: switch by signedness.
313+
ret cx.build.SRem(trans_expr(cx, a), trans_expr(cx, b));
314+
}
315+
316+
case (ast.bitor) {
317+
ret cx.build.Or(trans_expr(cx, a), trans_expr(cx, b));
318+
}
319+
320+
case (ast.bitand) {
321+
ret cx.build.And(trans_expr(cx, a), trans_expr(cx, b));
322+
}
323+
324+
case (ast.bitxor) {
325+
ret cx.build.Xor(trans_expr(cx, a), trans_expr(cx, b));
326+
}
327+
328+
case (ast.lsl) {
329+
ret cx.build.Shl(trans_expr(cx, a), trans_expr(cx, b));
330+
}
331+
332+
case (ast.lsr) {
333+
ret cx.build.LShr(trans_expr(cx, a), trans_expr(cx, b));
334+
}
335+
336+
case (ast.asr) {
337+
ret cx.build.AShr(trans_expr(cx, a), trans_expr(cx, b));
338+
}
339+
340+
case (ast.eq) {
341+
ret cx.build.ICmp(lib.llvm.LLVMIntEQ,
342+
trans_expr(cx, a), trans_expr(cx, b));
343+
}
344+
345+
case (ast.ne) {
346+
ret cx.build.ICmp(lib.llvm.LLVMIntNE,
347+
trans_expr(cx, a), trans_expr(cx, b));
348+
}
349+
350+
case (ast.lt) {
351+
// FIXME: switch by signedness.
352+
ret cx.build.ICmp(lib.llvm.LLVMIntSLT,
353+
trans_expr(cx, a), trans_expr(cx, b));
354+
}
355+
356+
case (ast.le) {
357+
// FIXME: switch by signedness.
358+
ret cx.build.ICmp(lib.llvm.LLVMIntSLE,
359+
trans_expr(cx, a), trans_expr(cx, b));
360+
}
361+
362+
case (ast.ge) {
363+
// FIXME: switch by signedness.
364+
ret cx.build.ICmp(lib.llvm.LLVMIntSGE,
365+
trans_expr(cx, a), trans_expr(cx, b));
366+
}
367+
368+
case (ast.gt) {
369+
// FIXME: switch by signedness.
370+
ret cx.build.ICmp(lib.llvm.LLVMIntSGT,
371+
trans_expr(cx, a), trans_expr(cx, b));
372+
}
373+
}
374+
cx.fcx.tcx.sess.unimpl("expr variant in trans_binary");
375+
fail;
376+
}
377+
240378
fn trans_expr(@block_ctxt cx, &ast.expr e) -> ValueRef {
241379
alt (e) {
242380
case (ast.expr_lit(?lit)) {
243-
alt (*lit) {
244-
case (ast.lit_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-
}
381+
ret trans_lit(cx, *lit);
382+
}
383+
384+
case (ast.expr_unary(?op, ?x)) {
385+
ret trans_unary(cx, op, *x);
386+
}
387+
388+
case (ast.expr_binary(?op, ?x, ?y)) {
389+
ret trans_binary(cx, op, *x, *y);
267390
}
268391
}
269392
cx.fcx.tcx.sess.unimpl("expr variant in trans_expr");
@@ -285,7 +408,8 @@ fn trans_log(@block_ctxt cx, &ast.expr e) {
285408
}
286409
}
287410
case (_) {
288-
cx.fcx.tcx.sess.unimpl("expr variant in trans_log");
411+
auto v = trans_expr(cx, e);
412+
trans_upcall(cx, "upcall_log_int", vec(v));
289413
}
290414
}
291415
}

0 commit comments

Comments
 (0)