Skip to content

Commit 7736be0

Browse files
committed
---
yaml --- r: 809 b: refs/heads/master c: b8cca09 h: refs/heads/master i: 807: d218154 v: v3
1 parent 3cbfa46 commit 7736be0

File tree

7 files changed

+175
-19
lines changed

7 files changed

+175
-19
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 10d628dbd06ec71956c7849c24822dde8710c2bd
2+
refs/heads/master: b8cca0971fc2dcd5f4bf4ac2ce82c1c635da0279

trunk/src/comp/front/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type local = rec(option[@ty] ty,
7878

7979
type decl = spanned[decl_];
8080
tag decl_ {
81-
decl_local(local);
81+
decl_local(@local);
8282
decl_item(@item);
8383
}
8484

trunk/src/comp/front/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ io fn parse_let(parser p) -> @ast.decl {
629629
init = init,
630630
id = p.next_def_id());
631631

632-
ret @spanned(lo, hi, ast.decl_local(local));
632+
ret @spanned(lo, hi, ast.decl_local(@local));
633633
}
634634

635635
io fn parse_auto(parser p) -> @ast.decl {
@@ -648,7 +648,7 @@ io fn parse_auto(parser p) -> @ast.decl {
648648
init = init,
649649
id = p.next_def_id());
650650

651-
ret @spanned(lo, hi, ast.decl_local(local));
651+
ret @spanned(lo, hi, ast.decl_local(@local));
652652
}
653653

654654
io fn parse_stmt(parser p) -> @ast.stmt {

trunk/src/comp/middle/fold.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ type ast_fold[ENV] =
103103

104104
// Decl folds.
105105
(fn(&ENV e, &span sp,
106-
&ast.local local) -> @decl) fold_decl_local,
106+
@ast.local local) -> @decl) fold_decl_local,
107107

108108
(fn(&ENV e, &span sp,
109109
@item item) -> @decl) fold_decl_item,
@@ -238,7 +238,7 @@ fn fold_decl[ENV](&ENV env, ast_fold[ENV] fld, @decl d) -> @decl {
238238
init_ = some[@ast.expr](fold_expr(env, fld, e));
239239
}
240240
}
241-
let ast.local local_ = rec(ty=ty_, init=init_ with local);
241+
let @ast.local local_ = @rec(ty=ty_, init=init_ with *local);
242242
ret fld.fold_decl_local(env_, d.span, local_);
243243
}
244244

@@ -356,7 +356,7 @@ fn fold_expr[ENV](&ENV env, ast_fold[ENV] fld, &@expr e) -> @expr {
356356

357357
case (ast.expr_name(?n, ?r, ?t)) {
358358
auto n_ = fold_name(env_, fld, n);
359-
ret fld.fold_expr_name(env_, e.span, n, r, t);
359+
ret fld.fold_expr_name(env_, e.span, n_, r, t);
360360
}
361361
}
362362

@@ -375,7 +375,7 @@ fn fold_stmt[ENV](&ENV env, ast_fold[ENV] fld, &@stmt s) -> @stmt {
375375
alt (s.node) {
376376
case (ast.stmt_decl(?d)) {
377377
auto dd = fold_decl(env_, fld, d);
378-
ret fld.fold_stmt_decl(env_, s.span, d);
378+
ret fld.fold_stmt_decl(env_, s.span, dd);
379379
}
380380

381381
case (ast.stmt_ret(?oe)) {
@@ -390,12 +390,12 @@ fn fold_stmt[ENV](&ENV env, ast_fold[ENV] fld, &@stmt s) -> @stmt {
390390

391391
case (ast.stmt_log(?e)) {
392392
auto ee = fold_expr(env_, fld, e);
393-
ret fld.fold_stmt_log(env_, s.span, e);
393+
ret fld.fold_stmt_log(env_, s.span, ee);
394394
}
395395

396396
case (ast.stmt_expr(?e)) {
397397
auto ee = fold_expr(env_, fld, e);
398-
ret fld.fold_stmt_expr(env_, s.span, e);
398+
ret fld.fold_stmt_expr(env_, s.span, ee);
399399
}
400400
}
401401
ret s;
@@ -621,7 +621,7 @@ fn identity_fold_expr_name[ENV](&ENV env, &span sp,
621621
// Decl identities.
622622

623623
fn identity_fold_decl_local[ENV](&ENV e, &span sp,
624-
&ast.local local) -> @decl {
624+
@ast.local local) -> @decl {
625625
ret @respan(sp, ast.decl_local(local));
626626
}
627627

trunk/src/comp/middle/resolve.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ tag scope {
1818
scope_block(ast.block);
1919
}
2020

21-
type env = list[scope];
21+
type env = rec(list[scope] scopes,
22+
session.session sess);
2223

2324
fn lookup_name(&env e, ast.ident i) -> option[def] {
2425

@@ -97,7 +98,7 @@ fn lookup_name(&env e, ast.ident i) -> option[def] {
9798
ret none[def];
9899
}
99100

100-
ret std.list.find[scope,def](e, bind in_scope(i, _));
101+
ret std.list.find[scope,def](e.scopes, bind in_scope(i, _));
101102
}
102103

103104
fn fold_expr_name(&env e, &span sp, &ast.name n,
@@ -110,33 +111,39 @@ fn fold_expr_name(&env e, &span sp, &ast.name n,
110111
log "resolved name " + n.node.ident;
111112
}
112113
case (none[def]) {
113-
log "unresolved name " + n.node.ident;
114+
e.sess.err("unresolved name: " + n.node.ident);
114115
}
115116
}
116117

117118
ret @fold.respan[ast.expr_](sp, ast.expr_name(n, d_, t));
118119
}
119120

120121
fn update_env_for_crate(&env e, @ast.crate c) -> env {
121-
ret cons[scope](scope_crate(c), @e);
122+
ret rec(scopes = cons[scope](scope_crate(c), @e.scopes) with e);
122123
}
123124

124125
fn update_env_for_item(&env e, @ast.item i) -> env {
125-
ret cons[scope](scope_item(i), @e);
126+
ret rec(scopes = cons[scope](scope_item(i), @e.scopes) with e);
126127
}
127128

128129
fn update_env_for_block(&env e, &ast.block b) -> env {
129-
ret cons[scope](scope_block(b), @e);
130+
ret rec(scopes = cons[scope](scope_block(b), @e.scopes) with e);
130131
}
131132

132133
fn resolve_crate(session.session sess, @ast.crate crate) -> @ast.crate {
134+
133135
let fold.ast_fold[env] fld = fold.new_identity_fold[env]();
136+
134137
fld = @rec( fold_expr_name = bind fold_expr_name(_,_,_,_,_),
135138
update_env_for_crate = bind update_env_for_crate(_,_),
136139
update_env_for_item = bind update_env_for_item(_,_),
137140
update_env_for_block = bind update_env_for_block(_,_)
138141
with *fld );
139-
ret fold.fold_crate[env](nil[scope], fld, crate);
142+
143+
auto e = rec(scopes = nil[scope],
144+
sess = sess);
145+
146+
ret fold.fold_crate[env](e, fld, crate);
140147
}
141148

142149
// Local Variables:

trunk/src/comp/middle/trans.rs

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import driver.session;
1212
import back.x86;
1313
import back.abi;
1414

15+
import util.common;
1516
import util.common.istr;
17+
import util.common.new_def_hash;
1618
import util.common.new_str_hash;
1719

1820
import lib.llvm.llvm;
@@ -49,6 +51,7 @@ state type trans_ctxt = rec(session.session sess,
4951
state type fn_ctxt = rec(ValueRef llfn,
5052
ValueRef lloutptr,
5153
ValueRef lltaskptr,
54+
hashmap[ast.def_id, ValueRef] lllocals,
5255
@trans_ctxt tcx);
5356

5457
type terminator = fn(@fn_ctxt cx, builder build);
@@ -126,11 +129,23 @@ fn T_i64() -> TypeRef {
126129
ret llvm.LLVMInt64Type();
127130
}
128131

132+
fn T_f32() -> TypeRef {
133+
ret llvm.LLVMFloatType();
134+
}
135+
136+
fn T_f64() -> TypeRef {
137+
ret llvm.LLVMDoubleType();
138+
}
139+
129140
fn T_int() -> TypeRef {
130141
// FIXME: switch on target type.
131142
ret T_i32();
132143
}
133144

145+
fn T_char() -> TypeRef {
146+
ret T_i32();
147+
}
148+
134149
fn T_fn(vec[TypeRef] inputs, TypeRef output) -> TypeRef {
135150
ret llvm.LLVMFunctionType(output,
136151
_vec.buf[TypeRef](inputs),
@@ -180,6 +195,10 @@ fn T_str(uint n) -> TypeRef {
180195
ret T_vec(T_i8(), n);
181196
}
182197

198+
fn T_box(TypeRef t) -> TypeRef {
199+
ret T_struct(vec(T_int(), t));
200+
}
201+
183202
fn T_crate() -> TypeRef {
184203
ret T_struct(vec(T_int(), // ptrdiff_t image_base_off
185204
T_int(), // uintptr_t self_addr
@@ -206,6 +225,49 @@ fn T_taskptr() -> TypeRef {
206225
ret T_ptr(T_task());
207226
}
208227

228+
fn type_of(@trans_ctxt cx, @ast.ty t) -> TypeRef {
229+
alt (t.node) {
230+
case (ast.ty_nil) { ret T_nil(); }
231+
case (ast.ty_bool) { ret T_i1(); }
232+
case (ast.ty_int) { ret T_int(); }
233+
case (ast.ty_uint) { ret T_int(); }
234+
case (ast.ty_machine(?tm)) {
235+
alt (tm) {
236+
case (common.ty_i8) { ret T_i8(); }
237+
case (common.ty_u8) { ret T_i8(); }
238+
case (common.ty_i16) { ret T_i16(); }
239+
case (common.ty_u16) { ret T_i16(); }
240+
case (common.ty_i32) { ret T_i32(); }
241+
case (common.ty_u32) { ret T_i32(); }
242+
case (common.ty_i64) { ret T_i64(); }
243+
case (common.ty_u64) { ret T_i64(); }
244+
case (common.ty_f32) { ret T_f32(); }
245+
case (common.ty_f64) { ret T_f64(); }
246+
}
247+
}
248+
case (ast.ty_char) { ret T_char(); }
249+
case (ast.ty_str) { ret T_str(0u); }
250+
case (ast.ty_box(?t)) {
251+
ret T_ptr(T_box(type_of(cx, t)));
252+
}
253+
case (ast.ty_vec(?t)) {
254+
ret T_ptr(T_vec(type_of(cx, t), 0u));
255+
}
256+
case (ast.ty_tup(?elts)) {
257+
let vec[TypeRef] tys = vec();
258+
for (tup(bool, @ast.ty) elt in elts) {
259+
tys += type_of(cx, elt._1);
260+
}
261+
ret T_struct(tys);
262+
}
263+
case (ast.ty_path(?pth, ?def)) {
264+
// FIXME: implement.
265+
cx.sess.unimpl("ty_path in trans.type_of");
266+
}
267+
}
268+
fail;
269+
}
270+
209271
// LLVM constant constructors.
210272

211273
fn C_null(TypeRef t) -> ValueRef {
@@ -356,7 +418,7 @@ fn trans_lit(@block_ctxt cx, &ast.lit lit) -> result {
356418
ret res(cx, C_int(u as int));
357419
}
358420
case (ast.lit_char(?c)) {
359-
ret res(cx, C_integral(c as int, T_i32()));
421+
ret res(cx, C_integral(c as int, T_char()));
360422
}
361423
case (ast.lit_bool(?b)) {
362424
ret res(cx, C_bool(b));
@@ -572,6 +634,25 @@ fn trans_expr(@block_ctxt cx, &ast.expr e) -> result {
572634

573635
ret res(next_cx, sub.val);
574636
}
637+
638+
case (ast.expr_name(?n, ?dopt, _)) {
639+
alt (dopt) {
640+
case (some[ast.def](?def)) {
641+
alt (def) {
642+
case (ast.def_local(?did)) {
643+
auto llptr = cx.fcx.lllocals.get(did);
644+
ret res(cx, cx.build.Load(llptr));
645+
}
646+
case (_) {
647+
cx.fcx.tcx.sess.unimpl("def variant in trans");
648+
}
649+
}
650+
}
651+
case (none[ast.def]) {
652+
cx.fcx.tcx.sess.err("unresolved expr_name in trans");
653+
}
654+
}
655+
}
575656
}
576657
cx.fcx.tcx.sess.unimpl("expr variant in trans_expr");
577658
fail;
@@ -616,6 +697,20 @@ fn trans_stmt(@block_ctxt cx, &ast.stmt s) -> result {
616697
sub.bcx = trans_expr(cx, *e).bcx;
617698
}
618699

700+
case (ast.stmt_decl(?d)) {
701+
alt (d.node) {
702+
case (ast.decl_local(?local)) {
703+
alt (local.init) {
704+
case (some[@ast.expr](?e)) {
705+
log "storing init of local " + local.ident;
706+
auto llptr = cx.fcx.lllocals.get(local.id);
707+
sub = trans_expr(cx, *e);
708+
sub.val = sub.bcx.build.Store(sub.val, llptr);
709+
}
710+
}
711+
}
712+
}
713+
}
619714
case (_) {
620715
cx.fcx.tcx.sess.unimpl("stmt variant");
621716
}
@@ -685,9 +780,41 @@ fn trans_block_cleanups(@block_ctxt cx) -> @block_ctxt {
685780
ret bcx;
686781
}
687782

783+
iter block_locals(&ast.block b) -> @ast.local {
784+
// FIXME: putting from inside an iter block doesn't work, so we can't
785+
// use the index here.
786+
for (@ast.stmt s in b.node.stmts) {
787+
alt (s.node) {
788+
case (ast.stmt_decl(?d)) {
789+
alt (d.node) {
790+
case (ast.decl_local(?local)) {
791+
put local;
792+
}
793+
}
794+
}
795+
}
796+
}
797+
}
798+
688799
fn trans_block(@block_ctxt cx, &ast.block b) -> result {
689800
auto bcx = cx;
690801

802+
for each (@ast.local local in block_locals(b)) {
803+
log "declaring local " + local.ident;
804+
auto ty = T_nil();
805+
alt (local.ty) {
806+
case (some[@ast.ty](?t)) {
807+
ty = type_of(cx.fcx.tcx, t);
808+
}
809+
case (none[@ast.ty]) {
810+
cx.fcx.tcx.sess.err("missing type for local " + local.ident);
811+
}
812+
}
813+
auto val = bcx.build.Alloca(ty);
814+
log "built alloca: " + val_str(val);
815+
cx.fcx.lllocals.insert(local.id, val);
816+
}
817+
691818
for (@ast.stmt s in b.node.stmts) {
692819
bcx = trans_stmt(bcx, *s).bcx;
693820
}
@@ -709,9 +836,11 @@ fn new_fn_ctxt(@trans_ctxt cx,
709836
cx.fns.insert(cx.path, llfn);
710837
let ValueRef lloutptr = llvm.LLVMGetParam(llfn, 0u);
711838
let ValueRef lltaskptr = llvm.LLVMGetParam(llfn, 1u);
839+
let hashmap[ast.def_id, ValueRef] lllocals = new_def_hash[ValueRef]();
712840
ret @rec(llfn=llfn,
713841
lloutptr=lloutptr,
714842
lltaskptr=lltaskptr,
843+
lllocals=lllocals,
715844
tcx=cx);
716845
}
717846

@@ -758,6 +887,7 @@ fn trans_exit_task_glue(@trans_ctxt cx) {
758887
auto fcx = @rec(llfn=llfn,
759888
lloutptr=lloutptr,
760889
lltaskptr=lltaskptr,
890+
lllocals=new_def_hash[ValueRef](),
761891
tcx=cx);
762892

763893
auto bcx = new_top_block_ctxt(fcx);

0 commit comments

Comments
 (0)