Skip to content

Commit df2909b

Browse files
committed
---
yaml --- r: 1135 b: refs/heads/master c: ac270fc h: refs/heads/master i: 1133: c3fb894 1131: 1230ac6 1127: 7fa4a08 1119: ab483a6 v: v3
1 parent a372a4d commit df2909b

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
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: 0411132679b58ff3c177ccf24a22212881d6538d
2+
refs/heads/master: ac270fc8549b12040b6e71ad8e30634157ec6c96

trunk/src/comp/middle/trans.rs

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import back.abi;
1818
import middle.ty.pat_ty;
1919

2020
import util.common;
21+
import util.common.append;
2122
import util.common.istr;
2223
import util.common.new_def_hash;
2324
import util.common.new_str_hash;
@@ -566,15 +567,20 @@ fn align_of(TypeRef t) -> ValueRef {
566567
ret llvm.LLVMConstIntCast(lib.llvm.llvm.LLVMAlignOf(t), T_int(), False);
567568
}
568569

569-
fn trans_malloc(@block_ctxt cx, @ty.t t) -> result {
570-
auto scope_cx = find_scope_cx(cx);
571-
auto ptr_ty = type_of(cx.fcx.ccx, t);
572-
auto body_ty = lib.llvm.llvm.LLVMGetElementType(ptr_ty);
570+
fn trans_malloc_inner(@block_ctxt cx, TypeRef llptr_ty) -> result {
571+
auto llbody_ty = lib.llvm.llvm.LLVMGetElementType(llptr_ty);
573572
// FIXME: need a table to collect tydesc globals.
574573
auto tydesc = C_int(0);
575-
auto sz = size_of(body_ty);
574+
auto sz = size_of(llbody_ty);
576575
auto sub = trans_upcall(cx, "upcall_malloc", vec(sz, tydesc));
577-
sub.val = sub.bcx.build.IntToPtr(sub.val, ptr_ty);
576+
sub.val = sub.bcx.build.IntToPtr(sub.val, llptr_ty);
577+
ret sub;
578+
}
579+
580+
fn trans_malloc(@block_ctxt cx, @ty.t t) -> result {
581+
auto scope_cx = find_scope_cx(cx);
582+
auto llptr_ty = type_of(cx.fcx.ccx, t);
583+
auto sub = trans_malloc_inner(cx, llptr_ty);
578584
scope_cx.cleanups += clean(bind drop_ty(_, sub.val, t));
579585
ret sub;
580586
}
@@ -2442,7 +2448,7 @@ impure fn trans_obj(@crate_ctxt cx, &ast._obj ob, ast.def_id oid,
24422448
auto llctor_decl = cx.item_ids.get(oid);
24432449
cx.item_names.insert(cx.path, llctor_decl);
24442450

2445-
// Translate obj ctor fields to function arguments.
2451+
// Translate obj ctor args to function arguments.
24462452
let vec[ast.arg] fn_args = vec();
24472453
for (ast.obj_field f in ob.fields) {
24482454
fn_args += vec(rec(mode=ast.alias,
@@ -2456,7 +2462,8 @@ impure fn trans_obj(@crate_ctxt cx, &ast._obj ob, ast.def_id oid,
24562462

24572463
auto bcx = new_top_block_ctxt(fcx);
24582464

2459-
copy_args_to_allocas(bcx, fn_args, arg_tys_of_fn(ann));
2465+
let vec[ty.arg] arg_tys = arg_tys_of_fn(ann);
2466+
copy_args_to_allocas(bcx, fn_args, arg_tys);
24602467

24612468
auto pair = bcx.build.Alloca(type_of(cx, ret_ty_of_fn(ann)));
24622469
auto vtbl = trans_vtbl(cx, ob, ty_params);
@@ -2468,9 +2475,37 @@ impure fn trans_obj(@crate_ctxt cx, &ast._obj ob, ast.def_id oid,
24682475
C_int(abi.obj_field_box)));
24692476
bcx.build.Store(vtbl, pair_vtbl);
24702477

2471-
// FIXME: allocate the object body, copy the args in, etc.
2472-
bcx.build.Store(C_null(T_ptr(T_box(T_nil()))), pair_box);
2478+
let TypeRef llbox_ty = T_ptr(T_box(T_nil()));
2479+
if (_vec.len[ty.arg](arg_tys) == 0u) {
2480+
// Store null into pair, if no args.
2481+
bcx.build.Store(C_null(llbox_ty), pair_box);
2482+
} else {
2483+
// Malloc a box for the body and copy args in.
2484+
let vec[@ty.t] obj_fields = vec();
2485+
for (ty.arg a in arg_tys) {
2486+
append[@ty.t](obj_fields, a.ty);
2487+
}
2488+
// Synthesize an obj body:
2489+
let @ty.t fields_ty = ty.plain_ty(ty.ty_tup(obj_fields));
2490+
let TypeRef llfields_ty = type_of(bcx.fcx.ccx, fields_ty);
2491+
let TypeRef llobj_body_ty =
2492+
T_ptr(T_box(T_struct(vec(T_tydesc(),
2493+
llfields_ty))));
2494+
auto r = trans_malloc_inner(bcx, llobj_body_ty);
2495+
auto box = r.val;
2496+
auto rc = r.bcx.build.GEP(box,
2497+
vec(C_int(0),
2498+
C_int(abi.box_rc_field_refcnt)));
2499+
auto body = r.bcx.build.GEP(box,
2500+
vec(C_int(0),
2501+
C_int(abi.box_rc_field_body)));
2502+
r.bcx.build.Store(C_int(1), rc);
2503+
2504+
// FIXME: Copy args into body
24732505

2506+
auto p = r.bcx.build.PointerCast(box, llbox_ty);
2507+
r.bcx.build.Store(p, pair_box);
2508+
}
24742509
bcx.build.Ret(bcx.build.Load(pair));
24752510
}
24762511

0 commit comments

Comments
 (0)