Skip to content

Commit 649a7bf

Browse files
committed
Comments and cleanup.
1 parent 8410ec7 commit 649a7bf

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

src/comp/middle/trans.rs

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
// trans.rs: Translate the completed AST to the LLVM IR.
2+
//
3+
// Some functions here, such as trans_block and trans_expr, return a value --
4+
// the result of the translation to LLVM -- while others, such as trans_fn,
5+
// trans_obj, and trans_item, are called only for the side effect of adding a
6+
// particular definition to the LLVM IR output we're producing.
7+
18
import std::int;
29
import std::str;
310
import std::uint;
@@ -130,8 +137,9 @@ type local_ctxt = rec(vec[str] path,
130137
@crate_ctxt ccx);
131138

132139

133-
// The type used for llself.
134-
type self_vt = rec(ValueRef v, ty::t t);
140+
// Types used for llself.
141+
type val_self_pair = rec(ValueRef v, ty::t t);
142+
type ty_self_pair = tup(TypeRef, ty::t);
135143

136144
// Function context. Every LLVM function we create will have one of these.
137145
state type fn_ctxt = rec(
@@ -155,7 +163,7 @@ state type fn_ctxt = rec(
155163
// function, due to LLVM's quirks.
156164

157165
// A block for all the function's allocas, so that LLVM will coalesce them
158-
// into a single alloca.
166+
// into a single alloca call.
159167
mutable BasicBlockRef llallocas,
160168

161169
// A block containing code that copies incoming arguments to space already
@@ -174,7 +182,7 @@ state type fn_ctxt = rec(
174182
// llallocas?
175183

176184
// The 'self' object currently in use in this function, if there is one.
177-
mutable option::t[self_vt] llself,
185+
mutable option::t[val_self_pair] llself,
178186

179187
// If this function is actually a iter, a block containing the code called
180188
// whenever the iter calls 'put'.
@@ -4668,9 +4676,9 @@ fn trans_lval(&@block_ctxt cx, &@ast::expr e) -> lval_result {
46684676
}
46694677
case (ast::expr_self_method(?ident, ?ann)) {
46704678
alt (cx.fcx.llself) {
4671-
case (some(?s_vt)) {
4672-
auto r = s_vt.v;
4673-
auto t = s_vt.t;
4679+
case (some(?pair)) {
4680+
auto r = pair.v;
4681+
auto t = pair.t;
46744682
ret trans_field(cx, e.span, r, t, ident, ann);
46754683
}
46764684
case (_) {
@@ -6694,7 +6702,7 @@ fn new_fn_ctxt(@local_ctxt cx, &span sp,
66946702
mutable llallocas=llbbs._0,
66956703
mutable llcopyargs=llbbs._1,
66966704
mutable llderivedtydescs=llbbs._2,
6697-
mutable llself=none[self_vt],
6705+
mutable llself=none[val_self_pair],
66986706
mutable lliterbody=none[ValueRef],
66996707
llargs=llargs,
67006708
llobjfields=llobjfields,
@@ -6713,18 +6721,28 @@ fn new_fn_ctxt(@local_ctxt cx, &span sp,
67136721
// - new_fn_ctxt
67146722
// - trans_args
67156723

6724+
// create_llargs_for_fn_args: Creates a mapping from incoming arguments to
6725+
// allocas created for them.
6726+
//
6727+
// When we translate a function, we need to map its incoming arguments to the
6728+
// spaces that have been created for them (by code in the llallocas field of
6729+
// the function's fn_ctxt). create_llargs_for_fn_args populates the llargs
6730+
// field of the fn_ctxt with
67166731
fn create_llargs_for_fn_args(&@fn_ctxt cx,
67176732
ast::proto proto,
6718-
option::t[tup(TypeRef, ty::t)] ty_self,
6733+
option::t[ty_self_pair] ty_self,
67196734
ty::t ret_ty,
67206735
&vec[ast::arg] args,
67216736
&vec[ast::ty_param] ty_params) {
67226737

6738+
// Skip the implicit arguments 0, 1, and 2. TODO: Pull out 3u and define
6739+
// it as a constant, since we're using it in several places in trans this
6740+
// way.
67236741
auto arg_n = 3u;
67246742

67256743
alt (ty_self) {
67266744
case (some(?tt)) {
6727-
cx.llself = some[self_vt](rec(v = cx.llenv, t = tt._1));
6745+
cx.llself = some[val_self_pair](rec(v = cx.llenv, t = tt._1));
67286746
}
67296747
case (none) {
67306748
auto i = 0u;
@@ -6738,13 +6756,18 @@ fn create_llargs_for_fn_args(&@fn_ctxt cx,
67386756
}
67396757
}
67406758

6759+
// If the function is actually an iter, populate the lliterbody field of
6760+
// the function context with the ValueRef that we get from
6761+
// llvm::LLVMGetParam for the iter's body.
67416762
if (proto == ast::proto_iter) {
67426763
auto llarg = llvm::LLVMGetParam(cx.llfn, arg_n);
67436764
assert (llarg as int != 0);
67446765
cx.lliterbody = some[ValueRef](llarg);
67456766
arg_n += 1u;
67466767
}
67476768

6769+
// Populate the llargs field of the function context with the ValueRefs
6770+
// that we get from llvm::LLVMGetParam for each argument.
67486771
for (ast::arg arg in args) {
67496772
auto llarg = llvm::LLVMGetParam(cx.llfn, arg_n);
67506773
assert (llarg as int != 0);
@@ -6758,17 +6781,17 @@ fn create_llargs_for_fn_args(&@fn_ctxt cx,
67586781
// were passed and whatnot. Apparently mem2reg will mop up.
67596782

67606783
fn copy_any_self_to_alloca(@fn_ctxt fcx,
6761-
option::t[tup(TypeRef, ty::t)] ty_self) {
6784+
option::t[ty_self_pair] ty_self) {
67626785

67636786
auto bcx = llallocas_block_ctxt(fcx);
67646787

67656788
alt (fcx.llself) {
6766-
case (some(?s_vt)) {
6789+
case (some(?pair)) {
67676790
alt (ty_self) {
6768-
case (some[tup(TypeRef, ty::t)](?tt)) {
6791+
case (some[ty_self_pair](?tt)) {
67696792
auto a = alloca(bcx, tt._0);
6770-
bcx.build.Store(s_vt.v, a);
6771-
fcx.llself = some[self_vt](rec(v = a, t = s_vt.t));
6793+
bcx.build.Store(pair.v, a);
6794+
fcx.llself = some[val_self_pair](rec(v = a, t = pair.t));
67726795
}
67736796
}
67746797
}
@@ -6842,7 +6865,7 @@ fn ret_ty_of_fn(&@crate_ctxt ccx, ast::ann ann) -> ty::t {
68426865
ret ret_ty_of_fn_ty(ccx, ty::ann_to_type(ccx.tcx.node_types, ann));
68436866
}
68446867

6845-
fn populate_fn_ctxt_from_llself(@fn_ctxt fcx, self_vt llself) {
6868+
fn populate_fn_ctxt_from_llself(@fn_ctxt fcx, val_self_pair llself) {
68466869
auto bcx = llallocas_block_ctxt(fcx);
68476870

68486871
let vec[ty::t] field_tys = [];
@@ -6921,7 +6944,7 @@ fn finish_fn(&@fn_ctxt fcx, BasicBlockRef lltop) {
69216944
// trans_fn: creates an LLVM function corresponding to a source language
69226945
// function.
69236946
fn trans_fn(@local_ctxt cx, &span sp, &ast::_fn f, ast::def_id fid,
6924-
option::t[tup(TypeRef, ty::t)] ty_self,
6947+
option::t[ty_self_pair] ty_self,
69256948
&vec[ast::ty_param] ty_params, &ast::ann ann) {
69266949
auto llfndecl = cx.ccx.item_ids.get(fid);
69276950

@@ -6949,6 +6972,10 @@ fn trans_fn(@local_ctxt cx, &span sp, &ast::_fn f, ast::def_id fid,
69496972
auto lltop = bcx.llbb;
69506973

69516974
auto block_ty = node_ann_type(cx.ccx, f.body.node.a);
6975+
// This call to trans_block is the place where we bridge between
6976+
// translation calls that don't have a return value (trans_crate,
6977+
// trans_mod, trans_item, trans_obj, et cetera) and those that do
6978+
// (trans_block, trans_expr, et cetera).
69526979
auto res = if (!ty::type_is_nil(cx.ccx.tcx, block_ty)
69536980
&& !ty::type_is_bot(cx.ccx.tcx, block_ty)) {
69546981
trans_block(bcx, f.body, save_in(fcx.llretptr))
@@ -7007,7 +7034,7 @@ fn create_vtbl(@local_ctxt cx,
70077034
cx.ccx.item_symbols.insert(m.node.id, s);
70087035

70097036
trans_fn(mcx, m.span, m.node.meth, m.node.id,
7010-
some[tup(TypeRef, ty::t)](tup(llself_ty, self_ty)),
7037+
some[ty_self_pair](tup(llself_ty, self_ty)),
70117038
ty_params, m.node.ann);
70127039
methods += [llfn];
70137040
}
@@ -7036,7 +7063,7 @@ fn trans_dtor(@local_ctxt cx,
70367063
cx.ccx.item_symbols.insert(dtor.node.id, s);
70377064

70387065
trans_fn(dcx, dtor.span, dtor.node.meth, dtor.node.id,
7039-
some[tup(TypeRef, ty::t)](tup(llself_ty, self_ty)),
7066+
some[ty_self_pair](tup(llself_ty, self_ty)),
70407067
ty_params, dtor.node.ann);
70417068

70427069
ret llfn;
@@ -7065,7 +7092,7 @@ fn trans_obj(@local_ctxt cx, &span sp, &ast::_obj ob, ast::def_id oid,
70657092

70667093
auto fcx = new_fn_ctxt(cx, sp, llctor_decl);
70677094
create_llargs_for_fn_args(fcx, ast::proto_fn,
7068-
none[tup(TypeRef, ty::t)],
7095+
none[ty_self_pair],
70697096
ret_ty_of_fn(ccx, ann),
70707097
fn_args, ty_params);
70717098

@@ -7232,7 +7259,7 @@ fn trans_tag_variant(@local_ctxt cx, ast::def_id tag_id,
72327259
auto fcx = new_fn_ctxt(cx, variant.span, llfndecl);
72337260

72347261
create_llargs_for_fn_args(fcx, ast::proto_fn,
7235-
none[tup(TypeRef, ty::t)],
7262+
none[ty_self_pair],
72367263
ret_ty_of_fn(cx.ccx, variant.node.ann),
72377264
fn_args, ty_params);
72387265

@@ -7325,7 +7352,7 @@ fn trans_item(@local_ctxt cx, &ast::item item) {
73257352
alt (item.node) {
73267353
case (ast::item_fn(?name, ?f, ?tps, ?fid, ?ann)) {
73277354
auto sub_cx = extend_path(cx, name);
7328-
trans_fn(sub_cx, item.span, f, fid, none[tup(TypeRef, ty::t)],
7355+
trans_fn(sub_cx, item.span, f, fid, none[ty_self_pair],
73297356
tps, ann);
73307357
}
73317358
case (ast::item_obj(?name, ?ob, ?tps, ?oid, ?ann)) {

0 commit comments

Comments
 (0)