|
| 1 | +// Translates individual functions in the completed AST to the LLVM IR, using |
| 2 | +// destination-passing style. |
| 3 | + |
| 4 | +import syntax::ast; |
| 5 | +import middle::trans; |
| 6 | +import middle::ty; |
| 7 | +import trans::block_ctxt; |
| 8 | +import trans::crate_ctxt; |
| 9 | +import trans::fn_ctxt; |
| 10 | +import trans::local_ctxt; |
| 11 | +import lib::llvm::llvm::TypeRef; |
| 12 | +import lib::llvm::llvm::ValueRef; |
| 13 | +import std::option::none; |
| 14 | +import std::option::some; |
| 15 | + |
| 16 | +import type_of_node = trans::node_id_type; |
| 17 | + |
| 18 | + |
| 19 | +// LLVM utilities |
| 20 | + |
| 21 | +fn llelement_type(TypeRef llty) -> TypeRef { |
| 22 | + lib::llvm::llvm::LLVMGetElementType(llty) |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +// Destination utilities |
| 27 | + |
| 28 | +tag dest_slot { |
| 29 | + dst_nil; |
| 30 | + dst_val(ValueRef); |
| 31 | +} |
| 32 | + |
| 33 | +type dest = rec(dest_slot slot, bool move); |
| 34 | + |
| 35 | +fn dest_slot_for_ptr(&ty::ctxt tcx, ValueRef llptr, ty::t t) -> dest_slot { |
| 36 | + if ty::type_is_nil(tcx, t) { dst_nil } else { dst_val(llptr) } |
| 37 | +} |
| 38 | + |
| 39 | +fn dest_copy(&ty::ctxt tcx, ValueRef llptr, ty::t t) -> dest { |
| 40 | + ret rec(slot=dest_slot_for_ptr(tcx, llptr, t), move=false); |
| 41 | +} |
| 42 | + |
| 43 | +fn dest_move(&ty::ctxt tcx, ValueRef llptr, ty::t t) -> dest { |
| 44 | + ret rec(slot=dest_slot_for_ptr(tcx, llptr, t), move=true); |
| 45 | +} |
| 46 | + |
| 47 | +fn dest_tmp(&@block_ctxt bcx, ty::t t) -> tup(@block_ctxt, dest) { |
| 48 | + if ty::type_is_nil(bcx_tcx(bcx), t) { |
| 49 | + ret tup(bcx, rec(slot=dst_nil, move=true)); |
| 50 | + } |
| 51 | + auto r = trans::alloc_ty(bcx, t); |
| 52 | + ret tup(r.bcx, dest_move(bcx_tcx(bcx), r.val, t)); |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +// Accessors |
| 57 | +// TODO: When we have overloading, simplify these names! |
| 58 | + |
| 59 | +fn bcx_tcx(&@block_ctxt bcx) -> ty::ctxt { ret bcx.fcx.lcx.ccx.tcx; } |
| 60 | +fn bcx_ccx(&@block_ctxt bcx) -> @crate_ctxt { ret bcx.fcx.lcx.ccx; } |
| 61 | +fn bcx_lcx(&@block_ctxt bcx) -> @local_ctxt { ret bcx.fcx.lcx; } |
| 62 | +fn bcx_fcx(&@block_ctxt bcx) -> @fn_ctxt { ret bcx.fcx; } |
| 63 | + |
| 64 | + |
| 65 | +// Common operations |
| 66 | + |
| 67 | +fn store(&@block_ctxt bcx, &dest dest, ValueRef llsrc) -> @block_ctxt { |
| 68 | + alt (dest.slot) { |
| 69 | + dst_nil { /* no-op */ } |
| 70 | + dst_val(?lldest) { bcx.build.Store(llsrc, lldest); } |
| 71 | + } |
| 72 | + ret bcx; |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +// AST substructure translation, with destinations |
| 77 | + |
| 78 | +fn trans_expr(&@block_ctxt bcx, &dest dest, &@ast::expr expr) -> @block_ctxt { |
| 79 | + ret bcx; // TODO |
| 80 | +} |
| 81 | + |
| 82 | +fn trans_recv(&@block_ctxt bcx, &dest dest, &@ast::expr expr) -> @block_ctxt { |
| 83 | + ret bcx; // TODO |
| 84 | +} |
| 85 | + |
| 86 | +fn trans_block(&@block_ctxt cx, &dest dest, &ast::block block) |
| 87 | + -> @block_ctxt { |
| 88 | + auto bcx = cx; |
| 89 | + for each (@ast::local local in trans::block_locals(block)) { |
| 90 | + bcx = trans::alloc_local(bcx, local).bcx; |
| 91 | + } |
| 92 | + |
| 93 | + for (@ast::stmt stmt in block.node.stmts) { |
| 94 | + bcx = trans_stmt(bcx, stmt); |
| 95 | + |
| 96 | + // If we hit a terminator, control won't go any further so |
| 97 | + // we're in dead-code land. Stop here. |
| 98 | + if trans::is_terminated(bcx) { ret bcx; } |
| 99 | + } |
| 100 | + |
| 101 | + alt (block.node.expr) { |
| 102 | + some(?e) { ret trans_expr(bcx, dest, e); } |
| 103 | + none { ret bcx; } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +// AST substructure translation, without destinations |
| 109 | + |
| 110 | +fn trans_init_local(&@block_ctxt bcx, &@ast::local local) -> @block_ctxt { |
| 111 | + auto llptr = bcx_fcx(bcx).lllocals.get(local.node.id); |
| 112 | + |
| 113 | + auto t = type_of_node(bcx_ccx(bcx), local.node.id); |
| 114 | + trans::add_clean(bcx, llptr, t); |
| 115 | + |
| 116 | + alt (local.node.init) { |
| 117 | + some(?init) { |
| 118 | + alt (init.op) { |
| 119 | + ast::init_assign { |
| 120 | + ret trans_expr(bcx, dest_copy(bcx_tcx(bcx), llptr, t), init.expr); |
| 121 | + } |
| 122 | + ast::init_move { |
| 123 | + ret trans_expr(bcx, dest_move(bcx_tcx(bcx), llptr, t), init.expr); |
| 124 | + } |
| 125 | + ast::init_recv { |
| 126 | + ret trans_recv(bcx, dest_copy(bcx_tcx(bcx), llptr, t), init.expr); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + none { |
| 131 | + ret store(bcx, dest_copy(bcx_tcx(bcx), llptr, t), |
| 132 | + trans_common::C_null(llelement_type(trans::val_ty(llptr)))); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +fn trans_stmt(&@block_ctxt cx, &@ast::stmt stmt) -> @block_ctxt { |
| 138 | + auto bcx = cx; |
| 139 | + alt (stmt.node) { |
| 140 | + ast::stmt_expr(?e, _) { |
| 141 | + auto tmp_r = dest_tmp(bcx, ty::expr_ty(bcx_tcx(bcx), e)); |
| 142 | + bcx = tmp_r._0; auto tmp = tmp_r._1; |
| 143 | + ret trans_expr(bcx, tmp, e); |
| 144 | + } |
| 145 | + ast::stmt_decl(?d, _) { |
| 146 | + alt (d.node) { |
| 147 | + ast::decl_local(?local) { ret trans_init_local(bcx, local); } |
| 148 | + ast::decl_item(?item) { |
| 149 | + trans::trans_item(bcx_lcx(bcx), *item); |
| 150 | + ret bcx; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
0 commit comments