Skip to content

Commit 94ce73b

Browse files
committed
---
yaml --- r: 968 b: refs/heads/master c: 9f56b00 h: refs/heads/master v: v3
1 parent 55d2722 commit 94ce73b

File tree

2 files changed

+99
-89
lines changed

2 files changed

+99
-89
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: 1600974a3a1dc2c2cbdfef6efdc5887a1dc13218
2+
refs/heads/master: 9f56b0061ce058c0edd614cfdd5cbdb72efcfe08

trunk/src/comp/middle/trans.rs

Lines changed: 98 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,10 @@ fn copy_ty(@block_ctxt cx,
679679
if (! is_init) {
680680
r = drop_ty(r.bcx, dst, t);
681681
}
682-
auto llty = type_of(cx.fcx.ccx, t);
683-
r = build_memcpy(r.bcx, dst, src, llty);
684-
ret res(r.bcx, src);
682+
// In this one surprising case, we do a load/store on
683+
// structure types. This results in a memcpy. Usually
684+
// we talk about structures by pointers in this file.
685+
ret res(r.bcx, r.bcx.build.Store(r.bcx.build.Load(src), dst));
685686
}
686687

687688
cx.fcx.ccx.sess.bug("unexpected type in trans.copy_ty: " +
@@ -778,7 +779,7 @@ fn node_type(@crate_ctxt cx, &ast.ann a) -> TypeRef {
778779
}
779780

780781
impure fn trans_unary(@block_ctxt cx, ast.unop op,
781-
&ast.expr e, &ast.ann a) -> result {
782+
@ast.expr e, &ast.ann a) -> result {
782783

783784
auto sub = trans_expr(cx, e);
784785

@@ -811,7 +812,7 @@ impure fn trans_unary(@block_ctxt cx, ast.unop op,
811812
}
812813

813814
impure fn trans_binary(@block_ctxt cx, ast.binop op,
814-
&ast.expr a, &ast.expr b) -> result {
815+
@ast.expr a, @ast.expr b) -> result {
815816

816817
// First couple cases are lazy:
817818

@@ -997,7 +998,7 @@ fn join_results(@block_ctxt parent_cx,
997998
ret res(join_cx, phi);
998999
}
9991000

1000-
impure fn trans_if(@block_ctxt cx, &ast.expr cond,
1001+
impure fn trans_if(@block_ctxt cx, @ast.expr cond,
10011002
&ast.block thn, &option.t[ast.block] els) -> result {
10021003

10031004
auto cond_res = trans_expr(cx, cond);
@@ -1023,7 +1024,7 @@ impure fn trans_if(@block_ctxt cx, &ast.expr cond,
10231024
vec(then_res, else_res));
10241025
}
10251026

1026-
impure fn trans_while(@block_ctxt cx, &ast.expr cond,
1027+
impure fn trans_while(@block_ctxt cx, @ast.expr cond,
10271028
&ast.block body) -> result {
10281029

10291030
auto cond_cx = new_sub_block_ctxt(cx, "while cond");
@@ -1043,7 +1044,7 @@ impure fn trans_while(@block_ctxt cx, &ast.expr cond,
10431044
}
10441045

10451046
impure fn trans_do_while(@block_ctxt cx, &ast.block body,
1046-
&ast.expr cond) -> result {
1047+
@ast.expr cond) -> result {
10471048

10481049
auto body_cx = new_sub_block_ctxt(cx, "do-while loop body");
10491050
auto next_cx = new_sub_block_ctxt(cx, "next");
@@ -1058,42 +1059,66 @@ impure fn trans_do_while(@block_ctxt cx, &ast.block body,
10581059
ret res(next_cx, body_res.val);
10591060
}
10601061

1061-
// The additional bool returned indicates whether it's a local
1062-
// (that is represented as an alloca, hence needs a 'load' to be
1063-
// used as an rval).
1062+
// The additional bool returned indicates whether it's mem (that is
1063+
// represented as an alloca or heap, hence needs a 'load' to be used as an
1064+
// immediate).
10641065

1065-
fn trans_lval(@block_ctxt cx, &ast.expr e)
1066-
-> tup(result, bool, ast.def_id) {
1067-
alt (e.node) {
1068-
case (ast.expr_name(?n, ?dopt, _)) {
1069-
alt (dopt) {
1070-
case (some[ast.def](?def)) {
1071-
alt (def) {
1072-
case (ast.def_arg(?did)) {
1073-
check (cx.fcx.llargs.contains_key(did));
1074-
ret tup(res(cx, cx.fcx.llargs.get(did)),
1075-
false, did);
1076-
}
1077-
case (ast.def_local(?did)) {
1078-
check (cx.fcx.lllocals.contains_key(did));
1079-
ret tup(res(cx, cx.fcx.lllocals.get(did)),
1080-
true, did);
1081-
}
1082-
case (ast.def_fn(?did)) {
1083-
check (cx.fcx.ccx.fn_ids.contains_key(did));
1084-
ret tup(res(cx, cx.fcx.ccx.fn_ids.get(did)),
1085-
false, did);
1086-
}
1087-
case (_) {
1088-
cx.fcx.ccx.sess.unimpl("def variant in trans");
1089-
}
1090-
}
1066+
fn trans_name(@block_ctxt cx, &ast.name n, &option.t[ast.def] dopt)
1067+
-> tup(result, bool) {
1068+
alt (dopt) {
1069+
case (some[ast.def](?def)) {
1070+
alt (def) {
1071+
case (ast.def_arg(?did)) {
1072+
check (cx.fcx.llargs.contains_key(did));
1073+
ret tup(res(cx, cx.fcx.llargs.get(did)),
1074+
false);
1075+
}
1076+
case (ast.def_local(?did)) {
1077+
check (cx.fcx.lllocals.contains_key(did));
1078+
ret tup(res(cx, cx.fcx.lllocals.get(did)),
1079+
true);
10911080
}
1092-
case (none[ast.def]) {
1093-
cx.fcx.ccx.sess.err("unresolved expr_name in trans");
1081+
case (ast.def_fn(?did)) {
1082+
check (cx.fcx.ccx.fn_ids.contains_key(did));
1083+
ret tup(res(cx, cx.fcx.ccx.fn_ids.get(did)),
1084+
false);
1085+
}
1086+
case (_) {
1087+
cx.fcx.ccx.sess.unimpl("def variant in trans");
10941088
}
10951089
}
10961090
}
1091+
case (none[ast.def]) {
1092+
cx.fcx.ccx.sess.err("unresolved expr_name in trans");
1093+
}
1094+
}
1095+
fail;
1096+
}
1097+
1098+
fn trans_field(@block_ctxt cx, &ast.span sp, @ast.expr base,
1099+
&ast.ident field, &ast.ann ann) -> tup(result, bool) {
1100+
auto lv = trans_lval(cx, base);
1101+
auto r = lv._0;
1102+
auto ty = typeck.expr_ty(base);
1103+
alt (ty.struct) {
1104+
case (typeck.ty_tup(?fields)) {
1105+
let uint ix = typeck.field_num(cx.fcx.ccx.sess, sp, field);
1106+
auto v = r.bcx.build.GEP(r.val, vec(C_int(0), C_int(ix as int)));
1107+
ret tup(res(r.bcx, v), lv._1);
1108+
}
1109+
}
1110+
cx.fcx.ccx.sess.unimpl("field variant in trans_field");
1111+
fail;
1112+
}
1113+
1114+
fn trans_lval(@block_ctxt cx, @ast.expr e) -> tup(result, bool) {
1115+
alt (e.node) {
1116+
case (ast.expr_name(?n, ?dopt, _)) {
1117+
ret trans_name(cx, n, dopt);
1118+
}
1119+
case (ast.expr_field(?base, ?ident, ?ann)) {
1120+
ret trans_field(cx, e.span, base, ident, ann);
1121+
}
10971122
}
10981123
cx.fcx.ccx.sess.unimpl("expr variant in trans_lval");
10991124
fail;
@@ -1105,15 +1130,15 @@ impure fn trans_exprs(@block_ctxt cx, &vec[@ast.expr] es)
11051130
let @block_ctxt bcx = cx;
11061131

11071132
for (@ast.expr e in es) {
1108-
auto res = trans_expr(bcx, *e);
1133+
auto res = trans_expr(bcx, e);
11091134
vs += res.val;
11101135
bcx = res.bcx;
11111136
}
11121137

11131138
ret tup(bcx, vs);
11141139
}
11151140

1116-
impure fn trans_cast(@block_ctxt cx, &ast.expr e, &ast.ann ann) -> result {
1141+
impure fn trans_cast(@block_ctxt cx, @ast.expr e, &ast.ann ann) -> result {
11171142
auto e_res = trans_expr(cx, e);
11181143
auto llsrctype = val_ty(e_res.val);
11191144
auto t = node_ann_type(cx.fcx.ccx, ann);
@@ -1144,7 +1169,7 @@ impure fn trans_cast(@block_ctxt cx, &ast.expr e, &ast.ann ann) -> result {
11441169
ret e_res;
11451170
}
11461171

1147-
impure fn trans_call(@block_ctxt cx, &ast.expr f,
1172+
impure fn trans_call(@block_ctxt cx, @ast.expr f,
11481173
vec[@ast.expr] args) -> result {
11491174
auto f_res = trans_lval(cx, f);
11501175
check (! f_res._1);
@@ -1163,7 +1188,7 @@ impure fn trans_tup(@block_ctxt cx, vec[tup(bool, @ast.expr)] args,
11631188
auto r = res(cx, C_nil());
11641189
for (tup(bool, @ast.expr) arg in args) {
11651190
auto t = typeck.expr_ty(arg._1);
1166-
auto src_res = trans_expr(r.bcx, *arg._1);
1191+
auto src_res = trans_expr(r.bcx, arg._1);
11671192
auto dst_elt = r.bcx.build.GEP(tup_val, vec(C_int(0), C_int(i)));
11681193
// FIXME: calculate copy init-ness in typestate.
11691194
r = copy_ty(src_res.bcx, true, dst_elt, src_res.val, t);
@@ -1173,46 +1198,31 @@ impure fn trans_tup(@block_ctxt cx, vec[tup(bool, @ast.expr)] args,
11731198
}
11741199

11751200

1176-
impure fn trans_field(@block_ctxt cx, &ast.span sp, @ast.expr base,
1177-
&ast.ident field, &ast.ann ann) -> result {
1178-
auto r = trans_expr(cx, *base);
1179-
auto ty = typeck.expr_ty(base);
1180-
alt (ty.struct) {
1181-
case (typeck.ty_tup(?fields)) {
1182-
let uint ix = typeck.field_num(cx.fcx.ccx.sess, sp, field);
1183-
auto v = r.bcx.build.GEP(r.val, vec(C_int(ix as int)));
1184-
ret res(r.bcx, v);
1185-
}
1186-
}
1187-
cx.fcx.ccx.sess.unimpl("field variant in trans_field");
1188-
fail;
1189-
}
1190-
11911201

1192-
impure fn trans_expr(@block_ctxt cx, &ast.expr e) -> result {
1202+
impure fn trans_expr(@block_ctxt cx, @ast.expr e) -> result {
11931203
alt (e.node) {
11941204
case (ast.expr_lit(?lit, _)) {
11951205
ret trans_lit(cx, *lit);
11961206
}
11971207

11981208
case (ast.expr_unary(?op, ?x, ?ann)) {
1199-
ret trans_unary(cx, op, *x, ann);
1209+
ret trans_unary(cx, op, x, ann);
12001210
}
12011211

12021212
case (ast.expr_binary(?op, ?x, ?y, _)) {
1203-
ret trans_binary(cx, op, *x, *y);
1213+
ret trans_binary(cx, op, x, y);
12041214
}
12051215

12061216
case (ast.expr_if(?cond, ?thn, ?els, _)) {
1207-
ret trans_if(cx, *cond, thn, els);
1217+
ret trans_if(cx, cond, thn, els);
12081218
}
12091219

12101220
case (ast.expr_while(?cond, ?body, _)) {
1211-
ret trans_while(cx, *cond, body);
1221+
ret trans_while(cx, cond, body);
12121222
}
12131223

12141224
case (ast.expr_do_while(?body, ?cond, _)) {
1215-
ret trans_do_while(cx, body, *cond);
1225+
ret trans_do_while(cx, body, cond);
12161226
}
12171227

12181228
case (ast.expr_block(?blk, _)) {
@@ -1226,46 +1236,45 @@ impure fn trans_expr(@block_ctxt cx, &ast.expr e) -> result {
12261236
ret res(next_cx, sub.val);
12271237
}
12281238

1229-
case (ast.expr_name(_,_,_)) {
1230-
auto sub = trans_lval(cx, e);
1231-
if (sub._1) {
1232-
ret res(sub._0.bcx, cx.build.Load(sub._0.val));
1233-
} else {
1234-
ret sub._0;
1235-
}
1236-
}
1237-
12381239
case (ast.expr_assign(?dst, ?src, ?ann)) {
1239-
auto lhs_res = trans_lval(cx, *dst);
1240+
auto lhs_res = trans_lval(cx, dst);
12401241
check (lhs_res._1);
1241-
auto rhs_res = trans_expr(lhs_res._0.bcx, *src);
1242+
auto rhs_res = trans_expr(lhs_res._0.bcx, src);
12421243
auto t = node_ann_type(cx.fcx.ccx, ann);
12431244
// FIXME: calculate copy init-ness in typestate.
12441245
ret copy_ty(rhs_res.bcx, true, lhs_res._0.val, rhs_res.val, t);
12451246
}
12461247

12471248
case (ast.expr_call(?f, ?args, _)) {
1248-
ret trans_call(cx, *f, args);
1249+
ret trans_call(cx, f, args);
12491250
}
12501251

12511252
case (ast.expr_cast(?e, _, ?ann)) {
1252-
ret trans_cast(cx, *e, ann);
1253+
ret trans_cast(cx, e, ann);
12531254
}
12541255

12551256
case (ast.expr_tup(?args, ?ann)) {
12561257
ret trans_tup(cx, args, ann);
12571258
}
12581259

1259-
case (ast.expr_field(?base, ?ident, ?ann)) {
1260-
ret trans_field(cx, e.span, base, ident, ann);
1261-
}
1260+
// lval cases fall through to trans_lval and then
1261+
// possibly load the result (if it's non-structural).
12621262

1263+
case (_) {
1264+
auto t = typeck.expr_ty(e);
1265+
auto sub = trans_lval(cx, e);
1266+
if (sub._1 && ! typeck.type_is_structural(t)) {
1267+
ret res(sub._0.bcx, cx.build.Load(sub._0.val));
1268+
} else {
1269+
ret sub._0;
1270+
}
1271+
}
12631272
}
12641273
cx.fcx.ccx.sess.unimpl("expr variant in trans_expr");
12651274
fail;
12661275
}
12671276

1268-
impure fn trans_log(@block_ctxt cx, &ast.expr e) -> result {
1277+
impure fn trans_log(@block_ctxt cx, @ast.expr e) -> result {
12691278
alt (e.node) {
12701279
case (ast.expr_lit(?lit, _)) {
12711280
alt (lit.node) {
@@ -1293,7 +1302,7 @@ impure fn trans_log(@block_ctxt cx, &ast.expr e) -> result {
12931302
}
12941303
}
12951304

1296-
impure fn trans_check_expr(@block_ctxt cx, &ast.expr e) -> result {
1305+
impure fn trans_check_expr(@block_ctxt cx, @ast.expr e) -> result {
12971306
auto cond_res = trans_expr(cx, e);
12981307

12991308
// FIXME: need pretty-printer.
@@ -1317,7 +1326,7 @@ impure fn trans_ret(@block_ctxt cx, &option.t[@ast.expr] e) -> result {
13171326
auto r = res(cx, C_nil());
13181327
alt (e) {
13191328
case (some[@ast.expr](?x)) {
1320-
r = trans_expr(cx, *x);
1329+
r = trans_expr(cx, x);
13211330
}
13221331
}
13231332

@@ -1353,19 +1362,19 @@ impure fn trans_stmt(@block_ctxt cx, &ast.stmt s) -> result {
13531362
auto sub = res(cx, C_nil());
13541363
alt (s.node) {
13551364
case (ast.stmt_log(?a)) {
1356-
sub.bcx = trans_log(cx, *a).bcx;
1365+
sub.bcx = trans_log(cx, a).bcx;
13571366
}
13581367

13591368
case (ast.stmt_check_expr(?a)) {
1360-
sub.bcx = trans_check_expr(cx, *a).bcx;
1369+
sub.bcx = trans_check_expr(cx, a).bcx;
13611370
}
13621371

13631372
case (ast.stmt_ret(?e)) {
13641373
sub.bcx = trans_ret(cx, e).bcx;
13651374
}
13661375

13671376
case (ast.stmt_expr(?e)) {
1368-
sub.bcx = trans_expr(cx, *e).bcx;
1377+
sub.bcx = trans_expr(cx, e).bcx;
13691378
}
13701379

13711380
case (ast.stmt_decl(?d)) {
@@ -1375,8 +1384,9 @@ impure fn trans_stmt(@block_ctxt cx, &ast.stmt s) -> result {
13751384
case (some[@ast.expr](?e)) {
13761385
check (cx.fcx.lllocals.contains_key(local.id));
13771386
auto llptr = cx.fcx.lllocals.get(local.id);
1378-
sub = trans_expr(cx, *e);
1379-
sub.val = sub.bcx.build.Store(sub.val, llptr);
1387+
sub = trans_expr(cx, e);
1388+
copy_ty(sub.bcx, true, llptr, sub.val,
1389+
typeck.expr_ty(e));
13801390
}
13811391
}
13821392
}

0 commit comments

Comments
 (0)