Skip to content

Commit 3b1c8d5

Browse files
committed
---
yaml --- r: 998 b: refs/heads/master c: 0f41f5a h: refs/heads/master v: v3
1 parent 58c114f commit 3b1c8d5

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
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: e233978891d5b93641e1365e7448d20cdc8b0a12
2+
refs/heads/master: 0f41f5a8f9492cc4d27ae5b36415a8303b59d937

trunk/src/comp/middle/trans.rs

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -374,16 +374,16 @@ fn C_struct(vec[ValueRef] elts) -> ValueRef {
374374
}
375375

376376
fn C_tydesc(TypeRef t) -> ValueRef {
377-
ret C_struct(vec(C_null(T_opaque()), // first_param
378-
llvm.LLVMSizeOf(t), // size
379-
llvm.LLVMAlignOf(t), // align
380-
C_null(T_opaque()), // copy_glue_off
381-
C_null(T_opaque()), // drop_glue_off
382-
C_null(T_opaque()), // free_glue_off
383-
C_null(T_opaque()), // sever_glue_off
384-
C_null(T_opaque()), // mark_glue_off
385-
C_null(T_opaque()), // obj_drop_glue_off
386-
C_null(T_opaque()))); // is_stateful
377+
ret C_struct(vec(C_null(T_ptr(T_opaque())), // first_param
378+
llvm.LLVMSizeOf(t), // size
379+
llvm.LLVMAlignOf(t), // align
380+
C_null(T_ptr(T_opaque())), // copy_glue_off
381+
C_null(T_ptr(T_opaque())), // drop_glue_off
382+
C_null(T_ptr(T_opaque())), // free_glue_off
383+
C_null(T_ptr(T_opaque())), // sever_glue_off
384+
C_null(T_ptr(T_opaque())), // mark_glue_off
385+
C_null(T_ptr(T_opaque())), // obj_drop_glue_off
386+
C_null(T_ptr(T_opaque())))); // is_stateful
387387
}
388388

389389
fn decl_fn(ModuleRef llmod, str name, uint cc, TypeRef llty) -> ValueRef {
@@ -450,6 +450,19 @@ fn trans_non_gc_free(@block_ctxt cx, ValueRef v) -> result {
450450
C_int(0)));
451451
}
452452

453+
fn trans_malloc(@block_ctxt cx, @typeck.ty t) -> result {
454+
auto ptr_ty = type_of(cx.fcx.ccx, t);
455+
auto body_ty = lib.llvm.llvm.LLVMGetElementType(ptr_ty);
456+
// FIXME: need a table to collect tydesc globals.
457+
auto tydesc = C_int(0);
458+
auto sz = cx.build.IntCast(lib.llvm.llvm.LLVMSizeOf(body_ty), T_int());
459+
auto sub = trans_upcall(cx, "upcall_malloc", vec(sz, tydesc));
460+
sub.val = sub.bcx.build.IntToPtr(sub.val, ptr_ty);
461+
sub.bcx.cleanups += clean(bind drop_ty(_, sub.val, t));
462+
ret sub;
463+
}
464+
465+
453466
fn incr_refcnt(@block_ctxt cx, ValueRef box_ptr) -> result {
454467
auto rc_ptr = cx.build.GEP(box_ptr, vec(C_int(0),
455468
C_int(abi.box_rc_field_refcnt)));
@@ -636,16 +649,21 @@ fn drop_ty(@block_ctxt cx,
636649
T_int(), C_int(0));
637650
}
638651

639-
case (typeck.ty_box(_)) {
652+
case (typeck.ty_box(?body_ty)) {
640653
fn hit_zero(@block_ctxt cx, ValueRef v,
641-
@typeck.ty elt_ty) -> result {
642-
auto res = drop_ty(cx,
643-
cx.build.GEP(v, vec(C_int(0))),
644-
elt_ty);
654+
@typeck.ty body_ty) -> result {
655+
auto body = cx.build.GEP(v,
656+
vec(C_int(0),
657+
C_int(abi.box_rc_field_body)));
658+
659+
auto res = drop_ty(cx, body, body_ty);
645660
// FIXME: switch gc/non-gc on stratum of the type.
646661
ret trans_non_gc_free(res.bcx, v);
647662
}
648-
ret incr_refcnt(cx, v);
663+
ret decr_refcnt_and_if_zero(cx, v,
664+
bind hit_zero(_, v, body_ty),
665+
"free box",
666+
T_int(), C_int(0));
649667
}
650668

651669
case (_) {
@@ -778,7 +796,7 @@ impure fn trans_lit(@block_ctxt cx, &ast.lit lit) -> result {
778796
C_int(len)));
779797
sub.val = sub.bcx.build.IntToPtr(sub.val,
780798
T_ptr(T_str()));
781-
cx.cleanups += vec(clean(bind trans_drop_str(_, sub.val)));
799+
cx.cleanups += clean(bind trans_drop_str(_, sub.val));
782800
ret sub;
783801
}
784802
}
@@ -835,13 +853,19 @@ impure fn trans_unary(@block_ctxt cx, ast.unop op,
835853
ret sub;
836854
}
837855
case (ast.box) {
838-
auto e_ty = node_type(cx.fcx.ccx, a);
839-
auto box_ty = T_box(e_ty);
840-
sub.val = cx.build.Malloc(box_ty);
841-
auto rc = sub.bcx.build.GEP(sub.val,
856+
auto e_ty = typeck.expr_ty(e);
857+
auto e_val = sub.val;
858+
sub = trans_malloc(sub.bcx, node_ann_type(sub.bcx.fcx.ccx, a));
859+
auto box = sub.val;
860+
auto rc = sub.bcx.build.GEP(box,
842861
vec(C_int(0),
843862
C_int(abi.box_rc_field_refcnt)));
844-
ret res(sub.bcx, cx.build.Store(C_int(1), rc));
863+
auto body = sub.bcx.build.GEP(box,
864+
vec(C_int(0),
865+
C_int(abi.box_rc_field_body)));
866+
sub.bcx.build.Store(C_int(1), rc);
867+
sub = copy_ty(sub.bcx, true, body, e_val, e_ty);
868+
ret res(sub.bcx, box);
845869
}
846870
case (_) {
847871
cx.fcx.ccx.sess.unimpl("expr variant in trans_unary");

0 commit comments

Comments
 (0)