1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use driver::config::FullDebugInfo;
14 use middle::lang_items::{FailFnLangItem, FailBoundsCheckFnLangItem};
15 use middle::trans::base::*;
16 use middle::trans::build::*;
17 use middle::trans::callee;
18 use middle::trans::cleanup::CleanupMethods;
19 use middle::trans::cleanup;
20 use middle::trans::common::*;
21 use middle::trans::debuginfo;
22 use middle::trans::expr;
23 use middle::trans::type_of;
25 use util::ppaux::Repr;
28 use syntax::ast::Ident;
30 use syntax::codemap::Span;
31 use syntax::parse::token::InternedString;
32 use syntax::parse::token;
33 use syntax::visit::Visitor;
35 pub fn trans_stmt<'a>(cx: &'a Block<'a>,
38 let _icx = push_ctxt("trans_stmt");
40 debug!("trans_stmt({})", s.repr(cx.tcx()));
42 if cx.sess().asm_comments() {
43 add_span_comment(cx, s.span, s.repr(cx.tcx()).as_slice());
48 let id = ast_util::stmt_id(s);
49 fcx.push_ast_cleanup_scope(id);
52 ast::StmtExpr(e, _) | ast::StmtSemi(e, _) => {
53 bcx = trans_stmt_semi(bcx, e);
55 ast::StmtDecl(d, _) => {
57 ast::DeclLocal(ref local) => {
58 bcx = init_local(bcx, *local);
59 if cx.sess().opts.debuginfo == FullDebugInfo {
60 debuginfo::create_local_var_metadata(bcx, *local);
63 ast::DeclItem(i) => trans_item(cx.fcx.ccx, i)
66 ast::StmtMac(..) => cx.tcx().sess.bug("unexpanded macro")
69 bcx = fcx.pop_and_trans_ast_cleanup_scope(
70 bcx, ast_util::stmt_id(s));
75 pub fn trans_stmt_semi<'a>(cx: &'a Block<'a>, e: &ast::Expr) -> &'a Block<'a> {
76 let _icx = push_ctxt("trans_stmt_semi");
77 let ty = expr_ty(cx, e);
78 if ty::type_needs_drop(cx.tcx(), ty) {
79 expr::trans_to_lvalue(cx, e, "stmt").bcx
81 expr::trans_into(cx, e, expr::Ignore)
85 pub fn trans_block<'a>(bcx: &'a Block<'a>,
89 let _icx = push_ctxt("trans_block");
93 fcx.push_ast_cleanup_scope(b.id);
95 for s in b.stmts.iter() {
96 bcx = trans_stmt(bcx, *s);
99 if dest != expr::Ignore {
100 let block_ty = node_id_type(bcx, b.id);
101 if b.expr.is_none() || type_is_zero_size(bcx.ccx(), block_ty) {
108 bcx = expr::trans_into(bcx, e, dest);
111 assert!(dest == expr::Ignore || bcx.unreachable.get());
115 bcx = fcx.pop_and_trans_ast_cleanup_scope(bcx, b.id);
120 pub fn trans_if<'a>(bcx: &'a Block<'a>,
123 thn: ast::P<ast::Block>,
124 els: Option<@ast::Expr>,
127 debug!("trans_if(bcx={}, if_id={}, cond={}, thn={:?}, dest={})",
128 bcx.to_str(), if_id, bcx.expr_to_str(cond), thn.id,
129 dest.to_str(bcx.ccx()));
130 let _icx = push_ctxt("trans_if");
133 let cond_val = unpack_result!(bcx, expr::trans(bcx, cond).to_llbool());
135 // Drop branches that are known to be impossible
136 if is_const(cond_val) && !is_undef(cond_val) {
137 if const_to_uint(cond_val) == 1 {
140 let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx };
141 trans.visit_expr(elexpr, ());
145 // if true { .. } [else { .. }]
146 bcx = trans_block(bcx, thn, dest);
147 debuginfo::clear_source_location(bcx.fcx);
149 let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx } ;
150 trans.visit_block(thn, ());
153 // if false { .. } else { .. }
155 bcx = expr::trans_into(bcx, elexpr, dest);
156 debuginfo::clear_source_location(bcx.fcx);
167 let name = format!("then-block-{}-", thn.id);
168 let then_bcx_in = bcx.fcx.new_id_block(name.as_slice(), thn.id);
169 let then_bcx_out = trans_block(then_bcx_in, thn, dest);
170 debuginfo::clear_source_location(bcx.fcx);
175 let else_bcx_in = bcx.fcx.new_id_block("else-block", elexpr.id);
176 let else_bcx_out = expr::trans_into(else_bcx_in, elexpr, dest);
177 next_bcx = bcx.fcx.join_blocks(if_id,
178 [then_bcx_out, else_bcx_out]);
179 CondBr(bcx, cond_val, then_bcx_in.llbb, else_bcx_in.llbb);
183 next_bcx = bcx.fcx.new_id_block("next-block", if_id);
184 Br(then_bcx_out, next_bcx.llbb);
185 CondBr(bcx, cond_val, then_bcx_in.llbb, next_bcx.llbb);
189 // Clear the source location because it is still set to whatever has been translated
191 debuginfo::clear_source_location(next_bcx.fcx);
196 pub fn trans_while<'a>(bcx: &'a Block<'a>,
197 loop_id: ast::NodeId,
201 let _icx = push_ctxt("trans_while");
206 // cond_bcx_in <--------+
212 // | body_bcx_out --+
215 let next_bcx_in = fcx.new_id_block("while_exit", loop_id);
216 let cond_bcx_in = fcx.new_id_block("while_cond", cond.id);
217 let body_bcx_in = fcx.new_id_block("while_body", body.id);
219 fcx.push_loop_cleanup_scope(loop_id, [next_bcx_in, cond_bcx_in]);
221 Br(bcx, cond_bcx_in.llbb);
223 // compile the block where we will handle loop cleanups
224 let cleanup_llbb = fcx.normal_exit_block(loop_id, cleanup::EXIT_BREAK);
226 // compile the condition
227 let Result {bcx: cond_bcx_out, val: cond_val} =
228 expr::trans(cond_bcx_in, cond).to_llbool();
229 CondBr(cond_bcx_out, cond_val, body_bcx_in.llbb, cleanup_llbb);
232 let body_bcx_out = trans_block(body_bcx_in, body, expr::Ignore);
233 Br(body_bcx_out, cond_bcx_in.llbb);
235 fcx.pop_loop_cleanup_scope(loop_id);
239 pub fn trans_loop<'a>(bcx:&'a Block<'a>,
240 loop_id: ast::NodeId,
243 let _icx = push_ctxt("trans_loop");
254 // Links between body_bcx_in and next_bcx are created by
257 let next_bcx_in = bcx.fcx.new_id_block("loop_exit", loop_id);
258 let body_bcx_in = bcx.fcx.new_id_block("loop_body", body.id);
260 fcx.push_loop_cleanup_scope(loop_id, [next_bcx_in, body_bcx_in]);
262 Br(bcx, body_bcx_in.llbb);
263 let body_bcx_out = trans_block(body_bcx_in, body, expr::Ignore);
264 Br(body_bcx_out, body_bcx_in.llbb);
266 fcx.pop_loop_cleanup_scope(loop_id);
268 if ty::type_is_bot(node_id_type(bcx, loop_id)) {
269 Unreachable(next_bcx_in);
275 pub fn trans_break_cont<'a>(bcx: &'a Block<'a>,
276 expr_id: ast::NodeId,
277 opt_label: Option<Ident>,
280 let _icx = push_ctxt("trans_break_cont");
283 if bcx.unreachable.get() {
287 // Locate loop that we will break to
288 let loop_id = match opt_label {
289 None => fcx.top_loop_scope(),
291 match bcx.tcx().def_map.borrow().find(&expr_id) {
292 Some(&def::DefLabel(loop_id)) => loop_id,
294 bcx.tcx().sess.bug(format!("{:?} in def-map for label",
301 // Generate appropriate cleanup code and branch
302 let cleanup_llbb = fcx.normal_exit_block(loop_id, exit);
303 Br(bcx, cleanup_llbb);
304 Unreachable(bcx); // anything afterwards should be ignored
308 pub fn trans_break<'a>(bcx: &'a Block<'a>,
309 expr_id: ast::NodeId,
310 label_opt: Option<Ident>)
312 return trans_break_cont(bcx, expr_id, label_opt, cleanup::EXIT_BREAK);
315 pub fn trans_cont<'a>(bcx: &'a Block<'a>,
316 expr_id: ast::NodeId,
317 label_opt: Option<Ident>)
319 return trans_break_cont(bcx, expr_id, label_opt, cleanup::EXIT_LOOP);
322 pub fn trans_ret<'a>(bcx: &'a Block<'a>,
323 e: Option<@ast::Expr>)
325 let _icx = push_ctxt("trans_ret");
328 let dest = match bcx.fcx.llretptr.get() {
329 None => expr::Ignore,
330 Some(retptr) => expr::SaveIn(retptr),
334 bcx = expr::trans_into(bcx, x, dest);
338 let cleanup_llbb = fcx.return_exit_block();
339 Br(bcx, cleanup_llbb);
344 fn str_slice_arg<'a>(bcx: &'a Block<'a>, s: InternedString) -> ValueRef {
346 let t = ty::mk_str_slice(bcx.tcx(), ty::ReStatic, ast::MutImmutable);
347 let s = C_str_slice(ccx, s);
348 let slot = alloca(bcx, val_ty(s), "__temp");
351 // The type of C_str_slice is { i8*, i64 }, but the type of the &str is
352 // %str_slice, so we do a bitcast here to the right type.
353 BitCast(bcx, slot, type_of::type_of(ccx, t).ptr_to())
356 pub fn trans_fail<'a>(
359 fail_str: InternedString)
362 let _icx = push_ctxt("trans_fail_value");
364 let v_str = str_slice_arg(bcx, fail_str);
365 let loc = bcx.sess().codemap().lookup_char_pos(sp.lo);
366 let filename = token::intern_and_get_ident(loc.file.name.as_slice());
367 let v_filename = str_slice_arg(bcx, filename);
368 let v_line = loc.line as int;
369 let args = vec!(v_str, v_filename, C_int(ccx, v_line));
370 let did = langcall(bcx, Some(sp), "", FailFnLangItem);
371 let bcx = callee::trans_lang_call(bcx,
374 Some(expr::Ignore)).bcx;
379 pub fn trans_fail_bounds_check<'a>(
385 let _icx = push_ctxt("trans_fail_bounds_check");
387 // Extract the file/line from the span
388 let loc = bcx.sess().codemap().lookup_char_pos(sp.lo);
389 let filename = token::intern_and_get_ident(loc.file.name.as_slice());
391 // Invoke the lang item
392 let filename = str_slice_arg(bcx, filename);
393 let line = C_int(bcx.ccx(), loc.line as int);
394 let args = vec!(filename, line, index, len);
395 let did = langcall(bcx, Some(sp), "", FailBoundsCheckFnLangItem);
396 let bcx = callee::trans_lang_call(bcx,
399 Some(expr::Ignore)).bcx;