2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-02-25 13:11:21 -06:00
|
|
|
use lib::llvm::*;
|
2013-07-15 22:42:13 -05:00
|
|
|
use middle::lang_items::{FailFnLangItem, FailBoundsCheckFnLangItem};
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::base::*;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::build::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::callee;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::common::*;
|
2013-11-18 09:56:44 -06:00
|
|
|
use middle::trans::debuginfo;
|
2014-01-15 13:39:08 -06:00
|
|
|
use middle::trans::cleanup;
|
|
|
|
use middle::trans::cleanup::CleanupMethods;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::expr;
|
|
|
|
use middle::ty;
|
|
|
|
use util::ppaux;
|
2014-01-15 13:39:08 -06:00
|
|
|
use util::ppaux::Repr;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-06-16 05:52:44 -05:00
|
|
|
use middle::trans::type_::Type;
|
|
|
|
|
2013-02-25 13:11:21 -06:00
|
|
|
use syntax::ast;
|
2013-09-10 14:01:44 -05:00
|
|
|
use syntax::ast::Name;
|
2013-02-25 13:11:21 -06:00
|
|
|
use syntax::ast_util;
|
2013-08-31 11:13:04 -05:00
|
|
|
use syntax::codemap::Span;
|
2013-09-24 20:18:40 -05:00
|
|
|
use syntax::visit::Visitor;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn trans_stmt<'a>(cx: &'a Block<'a>,
|
|
|
|
s: &ast::Stmt)
|
|
|
|
-> &'a Block<'a> {
|
|
|
|
let _icx = push_ctxt("trans_stmt");
|
|
|
|
let fcx = cx.fcx;
|
|
|
|
debug!("trans_stmt({})", s.repr(cx.tcx()));
|
|
|
|
|
|
|
|
if cx.sess().asm_comments() {
|
|
|
|
add_span_comment(cx, s.span, s.repr(cx.tcx()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut bcx = cx;
|
|
|
|
|
|
|
|
let id = ast_util::stmt_id(s);
|
|
|
|
fcx.push_ast_cleanup_scope(id);
|
|
|
|
|
|
|
|
match s.node {
|
|
|
|
ast::StmtExpr(e, _) | ast::StmtSemi(e, _) => {
|
|
|
|
bcx = expr::trans_into(cx, e, expr::Ignore);
|
|
|
|
}
|
|
|
|
ast::StmtDecl(d, _) => {
|
|
|
|
match d.node {
|
|
|
|
ast::DeclLocal(ref local) => {
|
|
|
|
bcx = init_local(bcx, *local);
|
|
|
|
if cx.sess().opts.extra_debuginfo {
|
|
|
|
debuginfo::create_local_var_metadata(bcx, *local);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::DeclItem(i) => trans_item(cx.fcx.ccx, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::StmtMac(..) => cx.tcx().sess.bug("unexpanded macro")
|
|
|
|
}
|
|
|
|
|
|
|
|
bcx = fcx.pop_and_trans_ast_cleanup_scope(
|
|
|
|
bcx, ast_util::stmt_id(s));
|
|
|
|
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn trans_block<'a>(bcx: &'a Block<'a>,
|
|
|
|
b: &ast::Block,
|
|
|
|
dest: expr::Dest)
|
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_block");
|
2014-01-15 13:39:08 -06:00
|
|
|
let fcx = bcx.fcx;
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
2014-01-15 13:39:08 -06:00
|
|
|
|
|
|
|
fcx.push_ast_cleanup_scope(b.id);
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for s in b.stmts.iter() {
|
2013-04-17 11:15:37 -05:00
|
|
|
bcx = trans_stmt(bcx, *s);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-07-16 13:08:35 -05:00
|
|
|
match b.expr {
|
2012-08-28 17:54:45 -05:00
|
|
|
Some(e) => {
|
|
|
|
bcx = expr::trans_into(bcx, e, dest);
|
|
|
|
}
|
|
|
|
None => {
|
2013-12-18 16:54:42 -06:00
|
|
|
assert!(dest == expr::Ignore || bcx.unreachable.get());
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
|
|
|
|
bcx = fcx.pop_and_trans_ast_cleanup_scope(bcx, b.id);
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn trans_if<'a>(bcx: &'a Block<'a>,
|
|
|
|
if_id: ast::NodeId,
|
|
|
|
cond: &ast::Expr,
|
|
|
|
thn: ast::P<ast::Block>,
|
|
|
|
els: Option<@ast::Expr>,
|
|
|
|
dest: expr::Dest)
|
|
|
|
-> &'a Block<'a> {
|
|
|
|
debug!("trans_if(bcx={}, if_id={}, cond={}, thn={:?}, dest={})",
|
|
|
|
bcx.to_str(), if_id, bcx.expr_to_str(cond), thn.id,
|
2012-08-28 17:54:45 -05:00
|
|
|
dest.to_str(bcx.ccx()));
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_if");
|
2014-01-15 13:39:08 -06:00
|
|
|
let mut bcx = bcx;
|
2013-07-17 03:13:41 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
let cond_val = unpack_result!(bcx, expr::trans(bcx, cond).to_llbool());
|
2013-07-25 03:53:27 -05:00
|
|
|
|
|
|
|
// Drop branches that are known to be impossible
|
|
|
|
if is_const(cond_val) && !is_undef(cond_val) {
|
|
|
|
if const_to_uint(cond_val) == 1 {
|
2013-09-24 20:18:40 -05:00
|
|
|
match els {
|
|
|
|
Some(elexpr) => {
|
|
|
|
let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx };
|
|
|
|
trans.visit_expr(elexpr, ());
|
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
2013-07-25 03:53:27 -05:00
|
|
|
// if true { .. } [else { .. }]
|
2014-01-15 13:39:08 -06:00
|
|
|
bcx = trans_block(bcx, thn, dest);
|
|
|
|
debuginfo::clear_source_location(bcx.fcx);
|
2013-07-25 03:53:27 -05:00
|
|
|
} else {
|
2013-09-24 20:18:40 -05:00
|
|
|
let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx } ;
|
|
|
|
trans.visit_block(thn, ());
|
|
|
|
|
2013-07-25 03:53:27 -05:00
|
|
|
match els {
|
|
|
|
// if false { .. } else { .. }
|
|
|
|
Some(elexpr) => {
|
2014-01-15 13:39:08 -06:00
|
|
|
bcx = expr::trans_into(bcx, elexpr, dest);
|
|
|
|
debuginfo::clear_source_location(bcx.fcx);
|
2013-07-17 03:13:41 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2013-07-25 03:53:27 -05:00
|
|
|
// if false { .. }
|
2014-01-15 13:39:08 -06:00
|
|
|
None => { }
|
2013-07-17 03:13:41 -05:00
|
|
|
}
|
2013-07-25 03:53:27 -05:00
|
|
|
}
|
2013-07-17 03:13:41 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
return bcx;
|
|
|
|
}
|
2013-02-06 16:28:02 -06:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
let name = format!("then-block-{}-", thn.id);
|
|
|
|
let then_bcx_in = bcx.fcx.new_id_block(name, thn.id);
|
2012-08-28 17:54:45 -05:00
|
|
|
let then_bcx_out = trans_block(then_bcx_in, thn, dest);
|
2013-11-18 09:56:44 -06:00
|
|
|
debuginfo::clear_source_location(bcx.fcx);
|
2013-07-12 21:10:41 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
let next_bcx;
|
|
|
|
match els {
|
|
|
|
Some(elexpr) => {
|
|
|
|
let else_bcx_in = bcx.fcx.new_id_block("else-block", elexpr.id);
|
|
|
|
let else_bcx_out = expr::trans_into(else_bcx_in, elexpr, dest);
|
|
|
|
next_bcx = bcx.fcx.join_blocks(if_id,
|
|
|
|
[then_bcx_out, else_bcx_out]);
|
|
|
|
CondBr(bcx, cond_val, then_bcx_in.llbb, else_bcx_in.llbb);
|
2013-11-30 02:26:21 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
None => {
|
|
|
|
next_bcx = bcx.fcx.new_id_block("next-block", if_id);
|
|
|
|
Br(then_bcx_out, next_bcx.llbb);
|
|
|
|
CondBr(bcx, cond_val, then_bcx_in.llbb, next_bcx.llbb);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
|
|
|
|
// Clear the source location because it is still set to whatever has been translated
|
|
|
|
// right before.
|
|
|
|
debuginfo::clear_source_location(next_bcx.fcx);
|
|
|
|
|
|
|
|
next_bcx
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn trans_while<'a>(bcx: &'a Block<'a>,
|
|
|
|
loop_id: ast::NodeId,
|
|
|
|
cond: &ast::Expr,
|
|
|
|
body: &ast::Block)
|
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_while");
|
2014-01-15 13:39:08 -06:00
|
|
|
let fcx = bcx.fcx;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// bcx
|
|
|
|
// |
|
|
|
|
// cond_bcx_in <--------+
|
|
|
|
// | |
|
|
|
|
// cond_bcx_out |
|
|
|
|
// | | |
|
|
|
|
// | body_bcx_in |
|
2014-01-15 13:39:08 -06:00
|
|
|
// cleanup_blk | |
|
2012-08-28 17:54:45 -05:00
|
|
|
// | body_bcx_out --+
|
2014-01-15 13:39:08 -06:00
|
|
|
// next_bcx_in
|
|
|
|
|
|
|
|
let next_bcx_in = fcx.new_id_block("while_exit", loop_id);
|
|
|
|
let cond_bcx_in = fcx.new_id_block("while_cond", cond.id);
|
|
|
|
let body_bcx_in = fcx.new_id_block("while_body", body.id);
|
|
|
|
|
|
|
|
fcx.push_loop_cleanup_scope(loop_id, [next_bcx_in, cond_bcx_in]);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
Br(bcx, cond_bcx_in.llbb);
|
|
|
|
|
|
|
|
// compile the block where we will handle loop cleanups
|
|
|
|
let cleanup_llbb = fcx.normal_exit_block(loop_id, cleanup::EXIT_BREAK);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// compile the condition
|
|
|
|
let Result {bcx: cond_bcx_out, val: cond_val} =
|
2014-01-15 13:39:08 -06:00
|
|
|
expr::trans(cond_bcx_in, cond).to_llbool();
|
|
|
|
CondBr(cond_bcx_out, cond_val, body_bcx_in.llbb, cleanup_llbb);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// loop body:
|
|
|
|
let body_bcx_out = trans_block(body_bcx_in, body, expr::Ignore);
|
2014-01-15 13:39:08 -06:00
|
|
|
Br(body_bcx_out, cond_bcx_in.llbb);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
fcx.pop_loop_cleanup_scope(loop_id);
|
|
|
|
return next_bcx_in;
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn trans_loop<'a>(bcx:&'a Block<'a>,
|
|
|
|
loop_id: ast::NodeId,
|
|
|
|
body: &ast::Block)
|
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_loop");
|
2014-01-15 13:39:08 -06:00
|
|
|
let fcx = bcx.fcx;
|
|
|
|
|
|
|
|
// bcx
|
|
|
|
// |
|
|
|
|
// body_bcx_in
|
|
|
|
// |
|
|
|
|
// body_bcx_out
|
|
|
|
//
|
|
|
|
// next_bcx
|
|
|
|
//
|
|
|
|
// Links between body_bcx_in and next_bcx are created by
|
|
|
|
// break statements.
|
|
|
|
|
|
|
|
let next_bcx_in = bcx.fcx.new_id_block("loop_exit", loop_id);
|
|
|
|
let body_bcx_in = bcx.fcx.new_id_block("loop_body", body.id);
|
|
|
|
|
|
|
|
fcx.push_loop_cleanup_scope(loop_id, [next_bcx_in, body_bcx_in]);
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
Br(bcx, body_bcx_in.llbb);
|
|
|
|
let body_bcx_out = trans_block(body_bcx_in, body, expr::Ignore);
|
2014-01-15 13:39:08 -06:00
|
|
|
Br(body_bcx_out, body_bcx_in.llbb);
|
|
|
|
|
|
|
|
fcx.pop_loop_cleanup_scope(loop_id);
|
|
|
|
|
|
|
|
return next_bcx_in;
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn trans_break_cont<'a>(bcx: &'a Block<'a>,
|
|
|
|
expr_id: ast::NodeId,
|
|
|
|
opt_label: Option<Name>,
|
|
|
|
exit: uint)
|
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_break_cont");
|
2014-01-15 13:39:08 -06:00
|
|
|
let fcx = bcx.fcx;
|
|
|
|
|
|
|
|
if bcx.unreachable.get() {
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Locate loop that we will break to
|
|
|
|
let loop_id = match opt_label {
|
|
|
|
None => fcx.top_loop_scope(),
|
|
|
|
Some(_) => {
|
|
|
|
let def_map = bcx.tcx().def_map.borrow();
|
|
|
|
match def_map.get().find(&expr_id) {
|
|
|
|
Some(&ast::DefLabel(loop_id)) => loop_id,
|
|
|
|
ref r => {
|
|
|
|
bcx.tcx().sess.bug(format!("{:?} in def-map for label", r))
|
Implement scopes independent of LLVM basic blocks
Currently, scopes are tied to LLVM basic blocks. For each scope, there
are two new basic blocks, which means two extra jumps in the unoptimized
IR. These blocks aren't actually required, but only used to act as the
boundary for cleanups.
By keeping track of the current scope within a single basic block, we
can avoid those extra blocks and jumps, shrinking the pre-optimization
IR quite considerably. For example, the IR for trans_intrinsic goes
from ~22k lines to ~16k lines, almost 30% less.
The impact on the build times of optimized builds is rather small (about
1%), but unoptimized builds are about 11% faster. The testsuite for
unoptimized builds runs between 15% (CPU time) and 7.5% (wallclock time on
my i7) faster.
Also, in some situations this helps LLVM to generate better code by
inlining functions that it previously considered to be too large.
Likely because of the pointless blocks/jumps that were still present at
the time the inlining pass runs.
Refs #7462
2013-07-07 07:53:57 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Generate appropriate cleanup code and branch
|
|
|
|
let cleanup_llbb = fcx.normal_exit_block(loop_id, exit);
|
|
|
|
Br(bcx, cleanup_llbb);
|
|
|
|
Unreachable(bcx); // anything afterwards should be ignored
|
2012-08-28 17:54:45 -05:00
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn trans_break<'a>(bcx: &'a Block<'a>,
|
|
|
|
expr_id: ast::NodeId,
|
|
|
|
label_opt: Option<Name>)
|
|
|
|
-> &'a Block<'a> {
|
|
|
|
return trans_break_cont(bcx, expr_id, label_opt, cleanup::EXIT_BREAK);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn trans_cont<'a>(bcx: &'a Block<'a>,
|
|
|
|
expr_id: ast::NodeId,
|
|
|
|
label_opt: Option<Name>)
|
|
|
|
-> &'a Block<'a> {
|
|
|
|
return trans_break_cont(bcx, expr_id, label_opt, cleanup::EXIT_LOOP);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn trans_ret<'a>(bcx: &'a Block<'a>,
|
|
|
|
e: Option<@ast::Expr>)
|
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_ret");
|
2014-01-15 13:39:08 -06:00
|
|
|
let fcx = bcx.fcx;
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
2013-12-20 22:36:07 -06:00
|
|
|
let dest = match bcx.fcx.llretptr.get() {
|
2013-06-20 09:42:44 -05:00
|
|
|
None => expr::Ignore,
|
|
|
|
Some(retptr) => expr::SaveIn(retptr),
|
2012-08-28 17:54:45 -05:00
|
|
|
};
|
|
|
|
match e {
|
2013-08-11 20:12:57 -05:00
|
|
|
Some(x) => {
|
|
|
|
bcx = expr::trans_into(bcx, x, dest);
|
|
|
|
}
|
|
|
|
_ => ()
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
let cleanup_llbb = fcx.return_exit_block();
|
|
|
|
Br(bcx, cleanup_llbb);
|
2012-08-28 17:54:45 -05:00
|
|
|
Unreachable(bcx);
|
|
|
|
return bcx;
|
|
|
|
}
|
2013-01-29 19:57:02 -06:00
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
pub fn trans_fail_expr<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-08-31 11:13:04 -05:00
|
|
|
sp_opt: Option<Span>,
|
2013-09-01 20:45:37 -05:00
|
|
|
fail_expr: Option<@ast::Expr>)
|
2014-01-07 10:54:58 -06:00
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_fail_expr");
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
match fail_expr {
|
|
|
|
Some(arg_expr) => {
|
2013-06-04 23:43:41 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
let tcx = ccx.tcx;
|
2014-01-15 13:39:08 -06:00
|
|
|
let arg_datum =
|
|
|
|
unpack_datum!(bcx, expr::trans_to_lvalue(bcx, arg_expr, "fail"));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
if ty::type_is_str(arg_datum.ty) {
|
2013-05-03 15:26:43 -05:00
|
|
|
let (lldata, _) = arg_datum.get_vec_base_and_len_no_root(bcx);
|
2012-08-28 17:54:45 -05:00
|
|
|
return trans_fail_value(bcx, sp_opt, lldata);
|
2013-12-18 16:54:42 -06:00
|
|
|
} else if bcx.unreachable.get() || ty::type_is_bot(arg_datum.ty) {
|
2012-08-28 17:54:45 -05:00
|
|
|
return bcx;
|
|
|
|
} else {
|
|
|
|
bcx.sess().span_bug(
|
|
|
|
arg_expr.span, ~"fail called with unsupported type " +
|
|
|
|
ppaux::ty_to_str(tcx, arg_datum.ty));
|
|
|
|
}
|
|
|
|
}
|
2013-06-12 12:02:55 -05:00
|
|
|
_ => trans_fail(bcx, sp_opt, @"explicit failure")
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
pub fn trans_fail<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-08-31 11:13:04 -05:00
|
|
|
sp_opt: Option<Span>,
|
2013-06-12 12:02:55 -05:00
|
|
|
fail_str: @str)
|
2014-01-07 10:54:58 -06:00
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_fail");
|
2012-08-28 17:54:45 -05:00
|
|
|
let V_fail_str = C_cstr(bcx.ccx(), fail_str);
|
|
|
|
return trans_fail_value(bcx, sp_opt, V_fail_str);
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_fail_value<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-08-31 11:13:04 -05:00
|
|
|
sp_opt: Option<Span>,
|
2013-01-29 19:57:02 -06:00
|
|
|
V_fail_str: ValueRef)
|
2014-01-07 10:54:58 -06:00
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_fail_value");
|
2012-08-28 17:54:45 -05:00
|
|
|
let ccx = bcx.ccx();
|
2013-02-19 01:40:42 -06:00
|
|
|
let (V_filename, V_line) = match sp_opt {
|
2012-08-28 17:54:45 -05:00
|
|
|
Some(sp) => {
|
|
|
|
let sess = bcx.sess();
|
2012-11-12 20:24:56 -06:00
|
|
|
let loc = sess.parse_sess.cm.lookup_char_pos(sp.lo);
|
2013-06-12 12:02:55 -05:00
|
|
|
(C_cstr(bcx.ccx(), loc.file.name),
|
2013-02-20 18:41:21 -06:00
|
|
|
loc.line as int)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
None => {
|
2013-06-12 12:02:55 -05:00
|
|
|
(C_cstr(bcx.ccx(), @"<runtime>"), 0)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
};
|
2013-06-15 22:45:48 -05:00
|
|
|
let V_str = PointerCast(bcx, V_fail_str, Type::i8p());
|
|
|
|
let V_filename = PointerCast(bcx, V_filename, Type::i8p());
|
2012-08-28 17:54:45 -05:00
|
|
|
let args = ~[V_str, V_filename, C_int(ccx, V_line)];
|
2013-07-15 22:42:13 -05:00
|
|
|
let did = langcall(bcx, sp_opt, "", FailFnLangItem);
|
|
|
|
let bcx = callee::trans_lang_call(bcx, did, args, Some(expr::Ignore)).bcx;
|
2012-08-28 17:54:45 -05:00
|
|
|
Unreachable(bcx);
|
|
|
|
return bcx;
|
|
|
|
}
|
2012-09-29 06:34:11 -05:00
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
pub fn trans_fail_bounds_check<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
|
|
|
sp: Span,
|
|
|
|
index: ValueRef,
|
|
|
|
len: ValueRef)
|
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_fail_bounds_check");
|
2013-05-04 13:29:32 -05:00
|
|
|
let (filename, line) = filename_and_line_num_from_span(bcx, sp);
|
2012-09-29 06:34:11 -05:00
|
|
|
let args = ~[filename, line, index, len];
|
2013-07-15 22:42:13 -05:00
|
|
|
let did = langcall(bcx, Some(sp), "", FailBoundsCheckFnLangItem);
|
|
|
|
let bcx = callee::trans_lang_call(bcx, did, args, Some(expr::Ignore)).bcx;
|
2012-09-29 06:34:11 -05:00
|
|
|
Unreachable(bcx);
|
|
|
|
return bcx;
|
|
|
|
}
|