2011-06-01 11:34:52 -07:00
|
|
|
// trans.rs: Translate the completed AST to the LLVM IR.
|
|
|
|
//
|
|
|
|
// Some functions here, such as trans_block and trans_expr, return a value --
|
|
|
|
// the result of the translation to LLVM -- while others, such as trans_fn,
|
|
|
|
// trans_obj, and trans_item, are called only for the side effect of adding a
|
|
|
|
// particular definition to the LLVM IR output we're producing.
|
2011-06-01 16:33:03 -07:00
|
|
|
//
|
|
|
|
// Hopefully useful general knowledge about trans:
|
2011-06-07 17:54:22 -07:00
|
|
|
//
|
2011-06-01 16:33:03 -07:00
|
|
|
// * There's no way to find out the ty::t type of a ValueRef. Doing so
|
|
|
|
// would be "trying to get the eggs out of an omelette" (credit:
|
|
|
|
// pcwalton). You can, instead, find out its TypeRef by calling val_ty,
|
|
|
|
// but many TypeRefs correspond to one ty::t; for instance, tup(int, int,
|
|
|
|
// int) and rec(x=int, y=int, z=int) will have the same TypeRef.
|
2011-05-17 20:41:41 +02:00
|
|
|
import std::int;
|
|
|
|
import std::str;
|
2011-08-23 20:25:23 -07:00
|
|
|
import std::istr;
|
2011-05-17 20:41:41 +02:00
|
|
|
import std::uint;
|
|
|
|
import std::str::rustrt::sbuf;
|
2011-05-12 17:24:54 +02:00
|
|
|
import std::map;
|
|
|
|
import std::map::hashmap;
|
|
|
|
import std::option;
|
|
|
|
import std::option::some;
|
|
|
|
import std::option::none;
|
2011-06-07 17:54:22 -07:00
|
|
|
import std::fs;
|
2011-07-19 11:56:46 -07:00
|
|
|
import std::time;
|
2011-08-17 19:11:01 -07:00
|
|
|
import std::vec;
|
2011-07-05 11:48:19 +02:00
|
|
|
import syntax::ast;
|
2011-08-21 21:44:41 -07:00
|
|
|
import syntax::ast_util;
|
2011-05-12 17:24:54 +02:00
|
|
|
import driver::session;
|
|
|
|
import middle::ty;
|
2011-07-21 14:49:58 -07:00
|
|
|
import middle::freevars::*;
|
2011-08-11 14:33:40 -07:00
|
|
|
import middle::gc;
|
2011-05-13 16:47:37 -04:00
|
|
|
import back::link;
|
2011-05-12 17:24:54 +02:00
|
|
|
import back::x86;
|
|
|
|
import back::abi;
|
|
|
|
import back::upcall;
|
2011-07-05 11:48:19 +02:00
|
|
|
import syntax::visit;
|
2011-06-10 17:24:20 +02:00
|
|
|
import visit::vt;
|
2011-05-12 17:24:54 +02:00
|
|
|
import util::common;
|
2011-07-13 17:26:06 -07:00
|
|
|
import util::common::*;
|
2011-07-06 16:46:17 +02:00
|
|
|
import std::map::new_int_hash;
|
|
|
|
import std::map::new_str_hash;
|
2011-07-05 11:48:19 +02:00
|
|
|
import syntax::codemap::span;
|
2011-05-12 17:24:54 +02:00
|
|
|
import lib::llvm::llvm;
|
|
|
|
import lib::llvm::target_data;
|
|
|
|
import lib::llvm::type_names;
|
|
|
|
import lib::llvm::mk_target_data;
|
|
|
|
import lib::llvm::mk_type_names;
|
|
|
|
import lib::llvm::llvm::ModuleRef;
|
|
|
|
import lib::llvm::llvm::ValueRef;
|
|
|
|
import lib::llvm::llvm::TypeRef;
|
|
|
|
import lib::llvm::llvm::TypeHandleRef;
|
|
|
|
import lib::llvm::llvm::BuilderRef;
|
|
|
|
import lib::llvm::llvm::BasicBlockRef;
|
|
|
|
import lib::llvm::False;
|
|
|
|
import lib::llvm::True;
|
|
|
|
import lib::llvm::Bool;
|
2011-06-07 17:54:22 -07:00
|
|
|
import link::mangle_internal_name_by_type_only;
|
|
|
|
import link::mangle_internal_name_by_seq;
|
|
|
|
import link::mangle_internal_name_by_path;
|
|
|
|
import link::mangle_internal_name_by_path_and_seq;
|
|
|
|
import link::mangle_exported_name;
|
2011-06-27 14:37:02 -07:00
|
|
|
import metadata::creader;
|
2011-07-07 22:18:38 -07:00
|
|
|
import metadata::csearch;
|
2011-07-07 18:13:24 -07:00
|
|
|
import metadata::cstore;
|
2011-07-05 11:48:19 +02:00
|
|
|
import util::ppaux::ty_to_str;
|
|
|
|
import util::ppaux::ty_to_short_str;
|
|
|
|
import syntax::print::pprust::expr_to_str;
|
|
|
|
import syntax::print::pprust::path_to_str;
|
2011-06-07 17:54:22 -07:00
|
|
|
|
2011-07-14 17:08:22 -07:00
|
|
|
import trans_common::*;
|
2011-08-30 09:59:30 +02:00
|
|
|
import trans_build::*;
|
2011-07-14 17:08:22 -07:00
|
|
|
|
2011-08-09 16:42:55 -07:00
|
|
|
import trans_objects::trans_anon_obj;
|
|
|
|
import trans_objects::trans_obj;
|
2011-08-22 11:48:00 -07:00
|
|
|
import ivec = trans_ivec;
|
2011-08-09 16:42:55 -07:00
|
|
|
|
2011-03-03 14:10:36 -08:00
|
|
|
// This function now fails if called on a type with dynamic size (as its
|
|
|
|
// return value was always meaningless in that case anyhow). Beware!
|
|
|
|
//
|
|
|
|
// TODO: Enforce via a predicate.
|
2011-08-22 12:45:18 +02:00
|
|
|
fn type_of(cx: &@crate_ctxt, sp: &span, t: ty::t) -> TypeRef {
|
2011-07-27 14:19:39 +02:00
|
|
|
if ty::type_has_dynamic_size(cx.tcx, t) {
|
2011-06-18 22:41:20 -07:00
|
|
|
cx.sess.span_fatal(sp,
|
2011-08-27 16:36:48 -07:00
|
|
|
~"type_of() called on a type with dynamic size: " +
|
|
|
|
ty_to_str(cx.tcx, t));
|
2011-03-03 14:10:36 -08:00
|
|
|
}
|
2011-05-18 15:35:16 -07:00
|
|
|
ret type_of_inner(cx, sp, t);
|
2010-11-20 22:04:34 -08:00
|
|
|
}
|
|
|
|
|
2011-08-04 16:20:09 -07:00
|
|
|
fn type_of_explicit_args(cx: &@crate_ctxt, sp: &span, inputs: &[ty::arg]) ->
|
|
|
|
[TypeRef] {
|
2011-08-19 15:16:48 -07:00
|
|
|
let atys: [TypeRef] = [];
|
2011-08-15 21:54:52 -07:00
|
|
|
for arg: ty::arg in inputs {
|
2011-08-03 13:11:02 -07:00
|
|
|
let t: TypeRef = type_of_inner(cx, sp, arg.ty);
|
2011-08-19 12:48:45 +02:00
|
|
|
t = alt arg.mode {
|
|
|
|
ty::mo_alias(_) { T_ptr(t) }
|
|
|
|
ty::mo_move. { T_ptr(t) }
|
|
|
|
_ {
|
|
|
|
if ty::type_is_structural(cx.tcx, arg.ty) { T_ptr(t) }
|
|
|
|
else { t }
|
|
|
|
}
|
|
|
|
};
|
2011-08-19 15:16:48 -07:00
|
|
|
atys += [t];
|
2011-02-16 16:16:11 -05:00
|
|
|
}
|
|
|
|
ret atys;
|
|
|
|
}
|
2011-02-08 11:47:53 -08:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-02-08 11:47:53 -08:00
|
|
|
// NB: must keep 4 fns in sync:
|
|
|
|
//
|
|
|
|
// - type_of_fn_full
|
|
|
|
// - create_llargs_for_fn_args.
|
|
|
|
// - new_fn_ctxt
|
|
|
|
// - trans_args
|
2011-07-27 14:19:39 +02:00
|
|
|
fn type_of_fn_full(cx: &@crate_ctxt, sp: &span, proto: ast::proto,
|
2011-08-22 12:45:18 +02:00
|
|
|
is_method: bool, inputs: &[ty::arg], output: ty::t,
|
2011-07-27 14:19:39 +02:00
|
|
|
ty_param_count: uint) -> TypeRef {
|
2011-08-19 15:16:48 -07:00
|
|
|
let atys: [TypeRef] = [];
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-28 18:54:05 -07:00
|
|
|
// Arg 0: Output pointer.
|
2011-08-19 15:16:48 -07:00
|
|
|
atys += [T_ptr(type_of_inner(cx, sp, output))];
|
2011-02-08 11:47:53 -08:00
|
|
|
|
2011-06-28 18:54:05 -07:00
|
|
|
// Arg 1: task pointer.
|
2011-08-19 15:16:48 -07:00
|
|
|
atys += [T_taskptr(*cx)];
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-28 18:54:05 -07:00
|
|
|
// Arg 2: Env (closure-bindings / self-obj)
|
2011-07-27 14:19:39 +02:00
|
|
|
if is_method {
|
2011-08-19 15:16:48 -07:00
|
|
|
atys += [T_ptr(cx.rust_object_type)];
|
|
|
|
} else { atys += [T_opaque_closure_ptr(*cx)]; }
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-28 18:54:05 -07:00
|
|
|
// Args >3: ty params, if not acquired via capture...
|
2011-07-27 14:19:39 +02:00
|
|
|
if !is_method {
|
|
|
|
let i = 0u;
|
2011-08-19 15:16:48 -07:00
|
|
|
while i < ty_param_count { atys += [T_ptr(cx.tydesc_type)]; i += 1u; }
|
2011-02-08 11:47:53 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
if proto == ast::proto_iter {
|
2011-02-17 12:20:55 -08:00
|
|
|
// If it's an iter, the 'output' type of the iter is actually the
|
|
|
|
// *input* type of the function we're given as our iter-block
|
|
|
|
// argument.
|
2011-08-19 15:16:48 -07:00
|
|
|
atys += [type_of_inner(cx, sp, ty::mk_iter_body_fn(cx.tcx, output))];
|
2011-02-17 12:20:55 -08:00
|
|
|
}
|
2011-06-28 18:54:05 -07:00
|
|
|
// ... then explicit args.
|
2011-06-15 11:19:50 -07:00
|
|
|
atys += type_of_explicit_args(cx, sp, inputs);
|
2011-05-12 17:24:54 +02:00
|
|
|
ret T_fn(atys, llvm::LLVMVoidType());
|
2010-12-16 10:23:47 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn type_of_fn(cx: &@crate_ctxt, sp: &span, proto: ast::proto,
|
2011-08-22 12:45:18 +02:00
|
|
|
inputs: &[ty::arg], output: ty::t, ty_param_count: uint) ->
|
2011-06-15 11:19:50 -07:00
|
|
|
TypeRef {
|
2011-07-27 14:19:39 +02:00
|
|
|
ret type_of_fn_full(cx, sp, proto, false, inputs, output, ty_param_count);
|
2010-12-23 17:31:16 -08:00
|
|
|
}
|
|
|
|
|
2011-07-26 17:59:43 -07:00
|
|
|
// Given a function type and a count of ty params, construct an llvm type
|
2011-08-22 12:45:18 +02:00
|
|
|
fn type_of_fn_from_ty(cx: &@crate_ctxt, sp: &span, fty: ty::t,
|
2011-08-19 15:16:48 -07:00
|
|
|
ty_param_count: uint) -> TypeRef {
|
|
|
|
ret type_of_fn(cx, sp, ty::ty_fn_proto(cx.tcx, fty),
|
|
|
|
ty::ty_fn_args(cx.tcx, fty), ty::ty_fn_ret(cx.tcx, fty),
|
2011-07-26 17:59:43 -07:00
|
|
|
ty_param_count);
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn type_of_native_fn(cx: &@crate_ctxt, sp: &span, abi: ast::native_abi,
|
2011-08-22 12:45:18 +02:00
|
|
|
inputs: &[ty::arg], output: ty::t, ty_param_count: uint)
|
2011-06-15 11:19:50 -07:00
|
|
|
-> TypeRef {
|
2011-08-19 15:16:48 -07:00
|
|
|
let atys: [TypeRef] = [];
|
2011-07-27 14:19:39 +02:00
|
|
|
if abi == ast::native_abi_rust {
|
2011-08-19 15:16:48 -07:00
|
|
|
atys += [T_taskptr(*cx)];
|
2011-07-27 14:19:39 +02:00
|
|
|
let i = 0u;
|
2011-08-19 15:16:48 -07:00
|
|
|
while i < ty_param_count { atys += [T_ptr(cx.tydesc_type)]; i += 1u; }
|
2011-02-28 10:37:49 -05:00
|
|
|
}
|
2011-05-18 15:35:16 -07:00
|
|
|
atys += type_of_explicit_args(cx, sp, inputs);
|
|
|
|
ret T_fn(atys, type_of_inner(cx, sp, output));
|
2011-02-16 16:16:11 -05:00
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn type_of_inner(cx: &@crate_ctxt, sp: &span, t: ty::t) -> TypeRef {
|
2011-04-19 16:40:46 -07:00
|
|
|
// Check the cache.
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
if cx.lltypes.contains_key(t) { ret cx.lltypes.get(t); }
|
|
|
|
let llty: TypeRef = 0 as TypeRef;
|
|
|
|
alt ty::struct(cx.tcx, t) {
|
|
|
|
ty::ty_native(_) { llty = T_ptr(T_i8()); }
|
|
|
|
ty::ty_nil. { llty = T_nil(); }
|
|
|
|
ty::ty_bot. {
|
|
|
|
llty = T_nil(); /* ...I guess? */
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_bool. { llty = T_bool(); }
|
|
|
|
ty::ty_int. { llty = T_int(); }
|
|
|
|
ty::ty_float. { llty = T_float(); }
|
|
|
|
ty::ty_uint. { llty = T_int(); }
|
|
|
|
ty::ty_machine(tm) {
|
|
|
|
alt tm {
|
|
|
|
ast::ty_i8. { llty = T_i8(); }
|
|
|
|
ast::ty_u8. { llty = T_i8(); }
|
|
|
|
ast::ty_i16. { llty = T_i16(); }
|
|
|
|
ast::ty_u16. { llty = T_i16(); }
|
|
|
|
ast::ty_i32. { llty = T_i32(); }
|
|
|
|
ast::ty_u32. { llty = T_i32(); }
|
|
|
|
ast::ty_i64. { llty = T_i64(); }
|
|
|
|
ast::ty_u64. { llty = T_i64(); }
|
|
|
|
ast::ty_f32. { llty = T_f32(); }
|
|
|
|
ast::ty_f64. { llty = T_f64(); }
|
2011-06-15 11:19:50 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_char. { llty = T_char(); }
|
|
|
|
ty::ty_str. { llty = T_ptr(T_str()); }
|
2011-08-25 10:18:02 +02:00
|
|
|
ty::ty_istr. { llty = T_ptr(T_ivec(T_i8())); }
|
2011-07-27 14:19:39 +02:00
|
|
|
ty::ty_tag(did, _) { llty = type_of_tag(cx, sp, did, t); }
|
|
|
|
ty::ty_box(mt) { llty = T_ptr(T_box(type_of_inner(cx, sp, mt.ty))); }
|
2011-08-10 17:23:46 -07:00
|
|
|
ty::ty_uniq(t) { llty = T_ptr(type_of_inner(cx, sp, t)); }
|
2011-08-18 14:11:06 -07:00
|
|
|
ty::ty_vec(mt) {
|
2011-07-27 14:19:39 +02:00
|
|
|
if ty::type_has_dynamic_size(cx.tcx, mt.ty) {
|
2011-08-25 10:18:02 +02:00
|
|
|
llty = T_ptr(T_opaque_ivec());
|
|
|
|
} else { llty = T_ptr(T_ivec(type_of_inner(cx, sp, mt.ty))); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_ptr(mt) { llty = T_ptr(type_of_inner(cx, sp, mt.ty)); }
|
|
|
|
ty::ty_rec(fields) {
|
2011-08-19 15:16:48 -07:00
|
|
|
let tys: [TypeRef] = [];
|
2011-08-15 21:54:52 -07:00
|
|
|
for f: ty::field in fields {
|
2011-08-19 15:16:48 -07:00
|
|
|
tys += [type_of_inner(cx, sp, f.mt.ty)];
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
llty = T_struct(tys);
|
|
|
|
}
|
2011-07-26 17:59:43 -07:00
|
|
|
ty::ty_fn(_, _, _, _, _) {
|
|
|
|
llty = T_fn_pair(*cx, type_of_fn_from_ty(cx, sp, t, 0u));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_native_fn(abi, args, out) {
|
|
|
|
let nft = native_fn_wrapper_type(cx, sp, 0u, t);
|
|
|
|
llty = T_fn_pair(*cx, nft);
|
|
|
|
}
|
|
|
|
ty::ty_obj(meths) { llty = cx.rust_object_type; }
|
|
|
|
ty::ty_res(_, sub, tps) {
|
|
|
|
let sub1 = ty::substitute_type_params(cx.tcx, tps, sub);
|
2011-08-19 15:16:48 -07:00
|
|
|
ret T_struct([T_i32(), type_of_inner(cx, sp, sub1)]);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_var(_) {
|
2011-08-27 16:36:48 -07:00
|
|
|
cx.tcx.sess.span_fatal(sp, ~"trans::type_of called on ty_var");
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-03 13:11:02 -07:00
|
|
|
ty::ty_param(_, _) { llty = T_typaram(cx.tn); }
|
2011-07-27 14:19:39 +02:00
|
|
|
ty::ty_type. { llty = T_ptr(cx.tydesc_type); }
|
2011-08-15 11:40:26 +02:00
|
|
|
ty::ty_tup(elts) {
|
2011-08-19 15:16:48 -07:00
|
|
|
let tys = [];
|
|
|
|
for elt in elts { tys += [type_of_inner(cx, sp, elt)]; }
|
2011-08-15 11:40:26 +02:00
|
|
|
llty = T_struct(tys);
|
|
|
|
}
|
2010-10-19 14:54:10 -07:00
|
|
|
}
|
2011-05-02 17:47:24 -07:00
|
|
|
assert (llty as int != 0);
|
2011-04-19 16:40:46 -07:00
|
|
|
cx.lltypes.insert(t, llty);
|
2011-02-25 16:24:19 -08:00
|
|
|
ret llty;
|
2010-10-19 14:54:10 -07:00
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn type_of_tag(cx: &@crate_ctxt, sp: &span, did: &ast::def_id, t: ty::t) ->
|
2011-07-27 14:19:39 +02:00
|
|
|
TypeRef {
|
2011-08-15 16:38:23 -07:00
|
|
|
let degen = std::vec::len(ty::tag_variants(cx.tcx, did)) == 1u;
|
2011-07-27 14:19:39 +02:00
|
|
|
if ty::type_has_dynamic_size(cx.tcx, t) {
|
|
|
|
if degen { ret T_i8(); } else { ret T_opaque_tag(cx.tn); }
|
2011-07-01 12:36:49 +02:00
|
|
|
} else {
|
2011-07-27 14:19:39 +02:00
|
|
|
let size = static_size_of_tag(cx, sp, t);
|
|
|
|
if !degen { ret T_tag(cx.tn, size); }
|
2011-07-01 12:36:49 +02:00
|
|
|
// LLVM does not like 0-size arrays, apparently
|
2011-07-27 14:19:39 +02:00
|
|
|
if size == 0u { size = 1u; }
|
2011-07-01 12:36:49 +02:00
|
|
|
ret T_array(T_i8(), size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 16:40:23 -07:00
|
|
|
fn type_of_ty_param_kinds_and_ty(lcx: @local_ctxt, sp: &span,
|
|
|
|
tpt: &ty::ty_param_kinds_and_ty) -> TypeRef {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt ty::struct(lcx.ccx.tcx, tpt.ty) {
|
2011-07-26 17:59:43 -07:00
|
|
|
ty::ty_fn(_, _, _, _, _) {
|
2011-08-19 15:16:48 -07:00
|
|
|
let llfnty =
|
|
|
|
type_of_fn_from_ty(lcx.ccx, sp, tpt.ty, std::vec::len(tpt.kinds));
|
2011-07-27 14:19:39 +02:00
|
|
|
ret T_fn_pair(*lcx.ccx, llfnty);
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
// fall through
|
|
|
|
}
|
2011-03-30 18:15:29 -07:00
|
|
|
}
|
2011-07-26 14:06:02 +02:00
|
|
|
ret type_of(lcx.ccx, sp, tpt.ty);
|
2011-03-30 18:15:29 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn type_of_or_i8(bcx: &@block_ctxt, typ: ty::t) -> TypeRef {
|
|
|
|
if ty::type_has_dynamic_size(bcx_tcx(bcx), typ) { ret T_i8(); }
|
2011-07-26 13:02:26 -07:00
|
|
|
ret type_of(bcx_ccx(bcx), bcx.sp, typ);
|
2011-06-10 19:35:59 -07:00
|
|
|
}
|
|
|
|
|
2011-03-30 18:15:29 -07:00
|
|
|
|
2010-12-10 15:02:23 -08:00
|
|
|
// Name sanitation. LLVM will happily accept identifiers with weird names, but
|
|
|
|
// gas doesn't!
|
2011-08-25 21:04:04 -07:00
|
|
|
fn sanitize(s: &istr) -> istr {
|
|
|
|
let result = ~"";
|
2011-08-15 21:54:52 -07:00
|
|
|
for c: u8 in s {
|
2011-07-27 14:19:39 +02:00
|
|
|
if c == '@' as u8 {
|
2011-08-25 21:04:04 -07:00
|
|
|
result += ~"boxed_";
|
2010-12-10 15:02:23 -08:00
|
|
|
} else {
|
2011-07-27 14:19:39 +02:00
|
|
|
if c == ',' as u8 {
|
2011-08-25 21:04:04 -07:00
|
|
|
result += ~"_";
|
2010-12-20 19:52:14 -08:00
|
|
|
} else {
|
2011-07-27 14:19:39 +02:00
|
|
|
if c == '{' as u8 || c == '(' as u8 {
|
2011-08-25 21:04:04 -07:00
|
|
|
result += ~"_of_";
|
2010-12-20 19:52:14 -08:00
|
|
|
} else {
|
2011-07-27 14:19:39 +02:00
|
|
|
if c != 10u8 && c != '}' as u8 && c != ')' as u8 &&
|
|
|
|
c != ' ' as u8 && c != '\t' as u8 && c != ';' as u8
|
|
|
|
{
|
2011-08-19 15:16:48 -07:00
|
|
|
let v = [c];
|
2011-08-25 21:04:04 -07:00
|
|
|
result += istr::unsafe_from_bytes(v);
|
2010-12-20 19:52:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-10 15:02:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2010-09-24 14:56:04 -07:00
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn log_fn_time(ccx: &@crate_ctxt, name: &istr, start: &time::timeval,
|
2011-07-27 14:19:39 +02:00
|
|
|
end: &time::timeval) {
|
|
|
|
let elapsed =
|
|
|
|
1000 * (end.sec - start.sec as int) +
|
|
|
|
((end.usec as int) - (start.usec as int)) / 1000;
|
2011-08-19 15:16:48 -07:00
|
|
|
*ccx.stats.fn_times += [{ident: name, time: elapsed}];
|
2011-07-19 11:56:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn decl_fn(llmod: ModuleRef, name: &istr, cc: uint, llty: TypeRef) ->
|
2011-07-27 14:19:39 +02:00
|
|
|
ValueRef {
|
2011-08-26 21:34:56 -07:00
|
|
|
let llfn: ValueRef = istr::as_buf(name, { |buf|
|
2011-08-26 15:36:18 -07:00
|
|
|
llvm::LLVMAddFunction(llmod, buf, llty)
|
|
|
|
});
|
2011-05-12 17:24:54 +02:00
|
|
|
llvm::LLVMSetFunctionCallConv(llfn, cc);
|
2010-09-23 17:16:34 -07:00
|
|
|
ret llfn;
|
|
|
|
}
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn decl_cdecl_fn(llmod: ModuleRef, name: &istr, llty: TypeRef) -> ValueRef {
|
2011-05-12 17:24:54 +02:00
|
|
|
ret decl_fn(llmod, name, lib::llvm::LLVMCCallConv, llty);
|
2010-11-14 12:28:07 -08:00
|
|
|
}
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn decl_fastcall_fn(llmod: ModuleRef, name: &istr,
|
|
|
|
llty: TypeRef) -> ValueRef {
|
2011-08-10 20:11:43 -07:00
|
|
|
let llfn = decl_fn(llmod, name, lib::llvm::LLVMFastCallConv, llty);
|
2011-08-26 15:36:18 -07:00
|
|
|
let _: () = istr::as_buf(~"rust", { |buf|
|
|
|
|
llvm::LLVMSetGC(llfn, buf)
|
|
|
|
});
|
2011-08-10 20:11:43 -07:00
|
|
|
ret llfn;
|
2010-11-14 12:28:07 -08:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-05-15 19:53:06 -04:00
|
|
|
// Only use this if you are going to actually define the function. It's
|
|
|
|
// not valid to simply declare a function as internal.
|
2011-08-26 21:34:56 -07:00
|
|
|
fn decl_internal_fastcall_fn(llmod: ModuleRef, name: &istr, llty: TypeRef) ->
|
2011-06-15 11:19:50 -07:00
|
|
|
ValueRef {
|
2011-07-27 14:19:39 +02:00
|
|
|
let llfn = decl_fn(llmod, name, lib::llvm::LLVMFastCallConv, llty);
|
2011-06-15 11:19:50 -07:00
|
|
|
llvm::LLVMSetLinkage(llfn,
|
|
|
|
lib::llvm::LLVMInternalLinkage as llvm::Linkage);
|
2011-08-26 15:36:18 -07:00
|
|
|
let _: () = istr::as_buf(~"rust", { |buf|
|
|
|
|
llvm::LLVMSetGC(llfn, buf)
|
|
|
|
});
|
2011-03-26 19:14:07 -07:00
|
|
|
ret llfn;
|
|
|
|
}
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn decl_glue(llmod: ModuleRef, cx: &crate_ctxt, s: &istr) -> ValueRef {
|
2011-08-19 15:16:48 -07:00
|
|
|
ret decl_cdecl_fn(llmod, s, T_fn([T_taskptr(cx)], T_void()));
|
2010-09-22 17:05:38 -07:00
|
|
|
}
|
|
|
|
|
2011-08-25 15:12:54 -07:00
|
|
|
fn get_extern_fn(externs: &hashmap<istr, ValueRef>, llmod: ModuleRef,
|
2011-08-26 21:34:56 -07:00
|
|
|
name: &istr, cc: uint, ty: TypeRef) -> ValueRef {
|
|
|
|
if externs.contains_key(name) {
|
|
|
|
ret externs.get(name);
|
2011-08-25 15:12:54 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let f = decl_fn(llmod, name, cc, ty);
|
2011-08-26 21:34:56 -07:00
|
|
|
externs.insert(name, f);
|
2010-09-23 18:38:37 -07:00
|
|
|
ret f;
|
|
|
|
}
|
|
|
|
|
2011-08-25 15:12:54 -07:00
|
|
|
fn get_extern_const(externs: &hashmap<istr, ValueRef>, llmod: ModuleRef,
|
2011-08-26 21:34:56 -07:00
|
|
|
name: &istr, ty: TypeRef) -> ValueRef {
|
|
|
|
if externs.contains_key(name) {
|
|
|
|
ret externs.get(name);
|
2011-08-25 15:12:54 -07:00
|
|
|
}
|
2011-08-26 21:34:56 -07:00
|
|
|
let c = istr::as_buf(name, { |buf|
|
2011-08-26 15:36:18 -07:00
|
|
|
llvm::LLVMAddGlobal(llmod, ty, buf)
|
|
|
|
});
|
2011-08-26 21:34:56 -07:00
|
|
|
externs.insert(name, c);
|
2011-03-25 17:59:45 -07:00
|
|
|
ret c;
|
|
|
|
}
|
|
|
|
|
2011-08-25 15:12:54 -07:00
|
|
|
fn get_simple_extern_fn(externs: &hashmap<istr, ValueRef>, llmod: ModuleRef,
|
2011-08-26 21:34:56 -07:00
|
|
|
name: &istr, n_args: int) -> ValueRef {
|
2011-08-13 00:09:25 -07:00
|
|
|
let inputs = std::vec::init_elt::<TypeRef>(T_int(), n_args as uint);
|
2011-07-27 14:19:39 +02:00
|
|
|
let output = T_int();
|
|
|
|
let t = T_fn(inputs, output);
|
2011-05-12 17:24:54 +02:00
|
|
|
ret get_extern_fn(externs, llmod, name, lib::llvm::LLVMCCallConv, t);
|
2011-03-25 17:59:45 -07:00
|
|
|
}
|
|
|
|
|
2011-08-25 15:12:54 -07:00
|
|
|
fn trans_native_call(cx: &@block_ctxt, externs: &hashmap<istr, ValueRef>,
|
2011-08-26 21:34:56 -07:00
|
|
|
llmod: ModuleRef, name: &istr, args: &[ValueRef]) ->
|
2011-08-19 15:16:48 -07:00
|
|
|
ValueRef {
|
2011-08-13 00:09:25 -07:00
|
|
|
let n: int = std::vec::len::<ValueRef>(args) as int;
|
2011-07-27 14:19:39 +02:00
|
|
|
let llnative: ValueRef = get_simple_extern_fn(externs, llmod, name, n);
|
2011-08-19 15:16:48 -07:00
|
|
|
let call_args: [ValueRef] = [];
|
2011-08-24 14:54:55 +02:00
|
|
|
for a: ValueRef in args {
|
2011-08-30 09:59:30 +02:00
|
|
|
call_args += [ZExtOrBitCast(cx, a, T_int())];
|
2011-08-24 14:54:55 +02:00
|
|
|
}
|
2011-08-30 09:59:30 +02:00
|
|
|
ret Call(cx, llnative, call_args);
|
2010-09-24 14:56:04 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_non_gc_free(cx: &@block_ctxt, v: ValueRef) -> result {
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(cx, bcx_ccx(cx).upcalls.free,
|
|
|
|
[cx.fcx.lltaskptr, PointerCast(cx, v, T_ptr(T_i8())),
|
2011-08-19 15:16:48 -07:00
|
|
|
C_int(0)]);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(cx, C_int(0));
|
2010-09-29 17:22:07 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_shared_free(cx: &@block_ctxt, v: ValueRef) -> result {
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(cx, bcx_ccx(cx).upcalls.shared_free,
|
|
|
|
[cx.fcx.lltaskptr, PointerCast(cx, v, T_ptr(T_i8()))]);
|
2011-07-05 22:55:41 -07:00
|
|
|
ret rslt(cx, C_int(0));
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn umax(cx: &@block_ctxt, a: ValueRef, b: ValueRef) -> ValueRef {
|
2011-08-30 09:59:30 +02:00
|
|
|
let cond = ICmp(cx, lib::llvm::LLVMIntULT, a, b);
|
|
|
|
ret Select(cx, cond, b, a);
|
2011-01-18 15:38:35 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn umin(cx: &@block_ctxt, a: ValueRef, b: ValueRef) -> ValueRef {
|
2011-08-30 09:59:30 +02:00
|
|
|
let cond = ICmp(cx, lib::llvm::LLVMIntULT, a, b);
|
|
|
|
ret Select(cx, cond, a, b);
|
2011-03-09 20:14:19 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn align_to(cx: &@block_ctxt, off: ValueRef, align: ValueRef) -> ValueRef {
|
2011-08-30 09:59:30 +02:00
|
|
|
let mask = Sub(cx, align, C_int(1));
|
|
|
|
let bumped = Add(cx, off, mask);
|
|
|
|
ret And(cx, bumped, Not(cx, mask));
|
2011-01-18 15:38:35 -08:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-03-04 15:08:33 -08:00
|
|
|
// Returns the real size of the given type for the current target.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn llsize_of_real(cx: &@crate_ctxt, t: TypeRef) -> uint {
|
2011-05-12 17:24:54 +02:00
|
|
|
ret llvm::LLVMStoreSizeOfType(cx.td.lltd, t);
|
2011-03-04 15:08:33 -08:00
|
|
|
}
|
|
|
|
|
2011-08-04 10:46:10 -07:00
|
|
|
// Returns the real alignment of the given type for the current target.
|
|
|
|
fn llalign_of_real(cx: &@crate_ctxt, t: TypeRef) -> uint {
|
|
|
|
ret llvm::LLVMPreferredAlignmentOfType(cx.td.lltd, t);
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn llsize_of(t: TypeRef) -> ValueRef {
|
2011-06-15 11:19:50 -07:00
|
|
|
ret llvm::LLVMConstIntCast(lib::llvm::llvm::LLVMSizeOf(t), T_int(),
|
|
|
|
False);
|
2010-12-20 12:54:50 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn llalign_of(t: TypeRef) -> ValueRef {
|
2011-06-15 11:19:50 -07:00
|
|
|
ret llvm::LLVMConstIntCast(lib::llvm::llvm::LLVMAlignOf(t), T_int(),
|
|
|
|
False);
|
2010-12-20 12:54:50 -08:00
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn size_of(cx: &@block_ctxt, t: ty::t) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
if !ty::type_has_dynamic_size(bcx_tcx(cx), t) {
|
2011-07-26 13:02:26 -07:00
|
|
|
ret rslt(cx, llsize_of(type_of(bcx_ccx(cx), cx.sp, t)));
|
2011-01-18 15:38:35 -08:00
|
|
|
}
|
|
|
|
ret dynamic_size_of(cx, t);
|
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn align_of(cx: &@block_ctxt, t: ty::t) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
if !ty::type_has_dynamic_size(bcx_tcx(cx), t) {
|
2011-07-26 13:02:26 -07:00
|
|
|
ret rslt(cx, llalign_of(type_of(bcx_ccx(cx), cx.sp, t)));
|
2011-01-18 15:38:35 -08:00
|
|
|
}
|
|
|
|
ret dynamic_align_of(cx, t);
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn alloca(cx: &@block_ctxt, t: TypeRef) -> ValueRef {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret Alloca(new_raw_block_ctxt(cx.fcx, cx.fcx.llstaticallocas), t);
|
2011-03-28 18:04:52 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn array_alloca(cx: &@block_ctxt, t: TypeRef, n: ValueRef) -> ValueRef {
|
2011-08-17 18:14:47 -07:00
|
|
|
let bcx = cx;
|
2011-08-24 14:54:55 +02:00
|
|
|
let dy_cx = new_raw_block_ctxt(cx.fcx, cx.fcx.lldynamicallocas);
|
2011-08-17 18:14:47 -07:00
|
|
|
let lltaskptr = bcx_fcx(bcx).lltaskptr;
|
2011-08-17 17:27:31 -07:00
|
|
|
alt bcx_fcx(cx).llobstacktoken {
|
2011-08-19 15:16:48 -07:00
|
|
|
none. {
|
|
|
|
bcx_fcx(cx).llobstacktoken =
|
2011-08-24 14:54:55 +02:00
|
|
|
some(mk_obstack_token(bcx_ccx(cx), cx.fcx, lltaskptr));
|
2011-08-19 15:16:48 -07:00
|
|
|
}
|
|
|
|
some(_) {/* no-op */ }
|
2011-08-17 17:27:31 -07:00
|
|
|
}
|
2011-08-17 18:14:47 -07:00
|
|
|
|
|
|
|
let dynastack_alloc = bcx_ccx(bcx).upcalls.dynastack_alloc;
|
2011-08-30 09:59:30 +02:00
|
|
|
let llsz = Mul(dy_cx, C_uint(llsize_of_real(bcx_ccx(bcx), t)), n);
|
|
|
|
let llresult = Call(dy_cx, dynastack_alloc, [lltaskptr, llsz]);
|
|
|
|
ret PointerCast(dy_cx, llresult, T_ptr(t));
|
2011-08-17 18:14:47 -07:00
|
|
|
}
|
|
|
|
|
2011-08-24 14:54:55 +02:00
|
|
|
fn mk_obstack_token(ccx: &@crate_ctxt, fcx: @fn_ctxt,
|
2011-08-17 18:14:47 -07:00
|
|
|
lltaskptr: ValueRef) -> ValueRef {
|
2011-08-24 14:54:55 +02:00
|
|
|
let cx = new_raw_block_ctxt(fcx, fcx.lldynamicallocas);
|
2011-08-30 09:59:30 +02:00
|
|
|
ret Call(cx, ccx.upcalls.dynastack_mark, [lltaskptr]);
|
2011-03-28 18:04:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-15 10:18:23 -07:00
|
|
|
// Creates a simpler, size-equivalent type. The resulting type is guaranteed
|
|
|
|
// to have (a) the same size as the type that was passed in; (b) to be non-
|
|
|
|
// recursive. This is done by replacing all boxes in a type with boxed unit
|
|
|
|
// types.
|
2011-08-22 12:45:18 +02:00
|
|
|
fn simplify_type(ccx: &@crate_ctxt, typ: ty::t) -> ty::t {
|
2011-07-27 14:19:39 +02:00
|
|
|
fn simplifier(ccx: @crate_ctxt, typ: ty::t) -> ty::t {
|
|
|
|
alt ty::struct(ccx.tcx, typ) {
|
|
|
|
ty::ty_box(_) { ret ty::mk_imm_box(ccx.tcx, ty::mk_nil(ccx.tcx)); }
|
2011-08-10 17:23:46 -07:00
|
|
|
ty::ty_uniq(_) { ret ty::mk_uniq(ccx.tcx, ty::mk_nil(ccx.tcx)); }
|
2011-07-27 14:19:39 +02:00
|
|
|
ty::ty_fn(_, _, _, _, _) {
|
2011-08-15 12:08:05 +02:00
|
|
|
ret ty::mk_tup(ccx.tcx,
|
2011-08-19 15:16:48 -07:00
|
|
|
[ty::mk_imm_box(ccx.tcx, ty::mk_nil(ccx.tcx)),
|
|
|
|
ty::mk_imm_box(ccx.tcx, ty::mk_nil(ccx.tcx))]);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_obj(_) {
|
2011-08-15 12:08:05 +02:00
|
|
|
ret ty::mk_tup(ccx.tcx,
|
2011-08-19 15:16:48 -07:00
|
|
|
[ty::mk_imm_box(ccx.tcx, ty::mk_nil(ccx.tcx)),
|
|
|
|
ty::mk_imm_box(ccx.tcx, ty::mk_nil(ccx.tcx))]);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_res(_, sub, tps) {
|
|
|
|
let sub1 = ty::substitute_type_params(ccx.tcx, tps, sub);
|
2011-08-15 12:08:05 +02:00
|
|
|
ret ty::mk_tup(ccx.tcx,
|
2011-08-19 15:16:48 -07:00
|
|
|
[ty::mk_int(ccx.tcx), simplify_type(ccx, sub1)]);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ { ret typ; }
|
2011-04-15 10:18:23 -07:00
|
|
|
}
|
|
|
|
}
|
2011-06-09 11:20:47 -07:00
|
|
|
ret ty::fold_ty(ccx.tcx, ty::fm_general(bind simplifier(ccx, _)), typ);
|
2011-04-15 10:18:23 -07:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-03-04 15:08:33 -08:00
|
|
|
// Computes the size of the data part of a non-dynamically-sized tag.
|
2011-08-22 12:45:18 +02:00
|
|
|
fn static_size_of_tag(cx: &@crate_ctxt, sp: &span, t: ty::t) -> uint {
|
2011-07-27 14:19:39 +02:00
|
|
|
if ty::type_has_dynamic_size(cx.tcx, t) {
|
2011-06-18 22:41:20 -07:00
|
|
|
cx.tcx.sess.span_fatal(sp,
|
2011-08-27 16:36:48 -07:00
|
|
|
~"dynamically sized type passed to \
|
2011-07-22 23:56:02 -07:00
|
|
|
static_size_of_tag()");
|
2011-03-04 15:08:33 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
if cx.tag_sizes.contains_key(t) { ret cx.tag_sizes.get(t); }
|
|
|
|
alt ty::struct(cx.tcx, t) {
|
|
|
|
ty::ty_tag(tid, subtys) {
|
|
|
|
// Compute max(variant sizes).
|
2011-07-01 12:46:14 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let max_size = 0u;
|
|
|
|
let variants = ty::tag_variants(cx.tcx, tid);
|
2011-08-15 21:54:52 -07:00
|
|
|
for variant: ty::variant_info in variants {
|
2011-08-19 15:16:48 -07:00
|
|
|
let tup_ty = simplify_type(cx, ty::mk_tup(cx.tcx, variant.args));
|
2011-07-27 14:19:39 +02:00
|
|
|
// Perform any type parameter substitutions.
|
2011-07-01 12:46:14 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
tup_ty = ty::substitute_type_params(cx.tcx, subtys, tup_ty);
|
|
|
|
// Here we possibly do a recursive call.
|
2011-07-01 12:46:14 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let this_size = llsize_of_real(cx, type_of(cx, sp, tup_ty));
|
|
|
|
if max_size < this_size { max_size = this_size; }
|
2011-03-04 15:08:33 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
cx.tag_sizes.insert(t, max_size);
|
|
|
|
ret max_size;
|
|
|
|
}
|
|
|
|
_ {
|
2011-08-27 16:36:48 -07:00
|
|
|
cx.tcx.sess.span_fatal(sp, ~"non-tag passed to static_size_of_tag()");
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-03-04 15:08:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn dynamic_size_of(cx: &@block_ctxt, t: ty::t) -> result {
|
2011-08-04 16:20:09 -07:00
|
|
|
fn align_elements(cx: &@block_ctxt, elts: &[ty::t]) -> result {
|
2011-03-01 16:01:33 -08:00
|
|
|
//
|
|
|
|
// C padding rules:
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// - Pad after each element so that next element is aligned.
|
|
|
|
// - Pad after final structure member so that whole structure
|
|
|
|
// is aligned to max alignment of interior.
|
|
|
|
//
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let off = C_int(0);
|
|
|
|
let max_align = C_int(1);
|
|
|
|
let bcx = cx;
|
2011-08-15 21:54:52 -07:00
|
|
|
for e: ty::t in elts {
|
2011-07-27 14:19:39 +02:00
|
|
|
let elt_align = align_of(bcx, e);
|
2011-03-01 16:01:33 -08:00
|
|
|
bcx = elt_align.bcx;
|
2011-07-27 14:19:39 +02:00
|
|
|
let elt_size = size_of(bcx, e);
|
2011-03-01 16:01:33 -08:00
|
|
|
bcx = elt_size.bcx;
|
2011-07-27 14:19:39 +02:00
|
|
|
let aligned_off = align_to(bcx, off, elt_align.val);
|
2011-08-30 09:59:30 +02:00
|
|
|
off = Add(bcx, aligned_off, elt_size.val);
|
2011-03-01 16:01:33 -08:00
|
|
|
max_align = umax(bcx, max_align, elt_align.val);
|
|
|
|
}
|
|
|
|
off = align_to(bcx, off, max_align);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(bcx, off);
|
2011-03-01 16:01:33 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
alt ty::struct(bcx_tcx(cx), t) {
|
2011-08-19 15:16:48 -07:00
|
|
|
ty::ty_param(p, _) {
|
2011-07-27 14:19:39 +02:00
|
|
|
let szptr = field_of_tydesc(cx, t, false, abi::tydesc_field_size);
|
2011-08-30 09:59:30 +02:00
|
|
|
ret rslt(szptr.bcx, Load(szptr.bcx, szptr.val));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_rec(flds) {
|
2011-08-19 15:16:48 -07:00
|
|
|
let tys: [ty::t] = [];
|
|
|
|
for f: ty::field in flds { tys += [f.mt.ty]; }
|
2011-07-27 14:19:39 +02:00
|
|
|
ret align_elements(cx, tys);
|
|
|
|
}
|
2011-08-15 11:40:26 +02:00
|
|
|
ty::ty_tup(elts) {
|
2011-08-19 15:16:48 -07:00
|
|
|
let tys = [];
|
|
|
|
for tp in elts { tys += [tp]; }
|
2011-08-15 11:40:26 +02:00
|
|
|
ret align_elements(cx, tys);
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ty::ty_tag(tid, tps) {
|
|
|
|
let bcx = cx;
|
|
|
|
// Compute max(variant sizes).
|
|
|
|
|
|
|
|
let max_size: ValueRef = alloca(bcx, T_int());
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(bcx, C_int(0), max_size);
|
2011-07-27 14:19:39 +02:00
|
|
|
let variants = ty::tag_variants(bcx_tcx(bcx), tid);
|
2011-08-15 21:54:52 -07:00
|
|
|
for variant: ty::variant_info in variants {
|
2011-07-27 14:19:39 +02:00
|
|
|
// Perform type substitution on the raw argument types.
|
|
|
|
|
2011-08-04 16:20:09 -07:00
|
|
|
let raw_tys: [ty::t] = variant.args;
|
2011-08-19 15:16:48 -07:00
|
|
|
let tys: [ty::t] = [];
|
2011-08-15 21:54:52 -07:00
|
|
|
for raw_ty: ty::t in raw_tys {
|
2011-07-27 14:19:39 +02:00
|
|
|
let t = ty::substitute_type_params(bcx_tcx(cx), tps, raw_ty);
|
2011-08-19 15:16:48 -07:00
|
|
|
tys += [t];
|
2011-03-01 16:37:17 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let rslt = align_elements(bcx, tys);
|
|
|
|
bcx = rslt.bcx;
|
|
|
|
let this_size = rslt.val;
|
2011-08-30 09:59:30 +02:00
|
|
|
let old_max_size = Load(bcx, max_size);
|
|
|
|
Store(bcx, umax(bcx, this_size, old_max_size), max_size);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-30 09:59:30 +02:00
|
|
|
let max_size_val = Load(bcx, max_size);
|
2011-07-27 14:19:39 +02:00
|
|
|
let total_size =
|
2011-08-15 16:38:23 -07:00
|
|
|
if std::vec::len(variants) != 1u {
|
2011-08-30 09:59:30 +02:00
|
|
|
Add(bcx, max_size_val, llsize_of(T_int()))
|
2011-07-01 12:36:49 +02:00
|
|
|
} else { max_size_val };
|
2011-07-27 14:19:39 +02:00
|
|
|
ret rslt(bcx, total_size);
|
|
|
|
}
|
2011-01-18 15:38:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn dynamic_align_of(cx: &@block_ctxt, t: ty::t) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt ty::struct(bcx_tcx(cx), t) {
|
2011-08-19 15:16:48 -07:00
|
|
|
ty::ty_param(p, _) {
|
2011-07-27 14:19:39 +02:00
|
|
|
let aptr = field_of_tydesc(cx, t, false, abi::tydesc_field_align);
|
2011-08-30 09:59:30 +02:00
|
|
|
ret rslt(aptr.bcx, Load(aptr.bcx, aptr.val));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_rec(flds) {
|
|
|
|
let a = C_int(1);
|
|
|
|
let bcx = cx;
|
2011-08-15 21:54:52 -07:00
|
|
|
for f: ty::field in flds {
|
2011-07-27 14:19:39 +02:00
|
|
|
let align = align_of(bcx, f.mt.ty);
|
|
|
|
bcx = align.bcx;
|
|
|
|
a = umax(bcx, a, align.val);
|
|
|
|
}
|
|
|
|
ret rslt(bcx, a);
|
|
|
|
}
|
|
|
|
ty::ty_tag(_, _) {
|
|
|
|
ret rslt(cx, C_int(1)); // FIXME: stub
|
|
|
|
}
|
2011-08-15 11:40:26 +02:00
|
|
|
ty::ty_tup(elts) {
|
|
|
|
let a = C_int(1);
|
|
|
|
let bcx = cx;
|
|
|
|
for e in elts {
|
2011-08-15 12:08:05 +02:00
|
|
|
let align = align_of(bcx, e);
|
2011-08-15 11:40:26 +02:00
|
|
|
bcx = align.bcx;
|
|
|
|
a = umax(bcx, a, align.val);
|
|
|
|
}
|
|
|
|
ret rslt(bcx, a);
|
|
|
|
}
|
2011-01-18 15:38:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-01 15:08:16 -07:00
|
|
|
// Simple wrapper around GEP that takes an array of ints and wraps them
|
|
|
|
// in C_int()
|
2011-08-04 16:20:09 -07:00
|
|
|
fn GEPi(cx: &@block_ctxt, base: ValueRef, ixs: &[int]) -> ValueRef {
|
2011-08-19 15:16:48 -07:00
|
|
|
let v: [ValueRef] = [];
|
|
|
|
for i: int in ixs { v += [C_int(i)]; }
|
2011-08-30 09:59:30 +02:00
|
|
|
ret InBoundsGEP(cx, base, v);
|
2011-08-01 15:08:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Increment a pointer by a given amount and then cast it to be a pointer
|
|
|
|
// to a given type.
|
2011-08-22 12:45:18 +02:00
|
|
|
fn bump_ptr(bcx: &@block_ctxt, t: ty::t, base: ValueRef, sz: ValueRef) ->
|
2011-08-19 15:16:48 -07:00
|
|
|
ValueRef {
|
2011-08-30 09:59:30 +02:00
|
|
|
let raw = PointerCast(bcx, base, T_ptr(T_i8()));
|
|
|
|
let bumped = GEP(bcx, raw, [sz]);
|
2011-08-19 15:16:48 -07:00
|
|
|
if ty::type_has_dynamic_size(bcx_tcx(bcx), t) { ret bumped; }
|
2011-08-01 15:08:16 -07:00
|
|
|
let typ = T_ptr(type_of(bcx_ccx(bcx), bcx.sp, t));
|
2011-08-30 09:59:30 +02:00
|
|
|
ret PointerCast(bcx, bumped, typ);
|
2011-08-01 15:08:16 -07:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-01-19 16:29:14 -08:00
|
|
|
// Replacement for the LLVM 'GEP' instruction when field-indexing into a
|
2011-03-04 15:08:33 -08:00
|
|
|
// tuple-like structure (tup, rec) with a static index. This one is driven off
|
2011-05-12 17:24:54 +02:00
|
|
|
// ty::struct and knows what to do when it runs into a ty_param stuck in the
|
2011-03-04 15:08:33 -08:00
|
|
|
// middle of the thing it's GEP'ing into. Much like size_of and align_of,
|
|
|
|
// above.
|
2011-08-22 12:45:18 +02:00
|
|
|
fn GEP_tup_like(cx: &@block_ctxt, t: ty::t, base: ValueRef, ixs: &[int]) ->
|
2011-07-27 14:19:39 +02:00
|
|
|
result {
|
2011-07-26 13:02:26 -07:00
|
|
|
assert (ty::type_is_tup_like(bcx_tcx(cx), t));
|
2011-01-19 16:29:14 -08:00
|
|
|
// It might be a static-known type. Handle this.
|
2011-07-27 14:19:39 +02:00
|
|
|
if !ty::type_has_dynamic_size(bcx_tcx(cx), t) {
|
2011-08-01 15:08:16 -07:00
|
|
|
ret rslt(cx, GEPi(cx, base, ixs));
|
2011-01-19 16:29:14 -08:00
|
|
|
}
|
|
|
|
// It is a dynamic-containing type that, if we convert directly to an LLVM
|
|
|
|
// TypeRef, will be all wrong; there's no proper LLVM type to represent
|
|
|
|
// it, and the lowering function will stick in i8* values for each
|
|
|
|
// ty_param, which is not right; the ty_params are all of some dynamic
|
|
|
|
// size.
|
|
|
|
//
|
|
|
|
// What we must do instead is sadder. We must look through the indices
|
|
|
|
// manually and split the input type into a prefix and a target. We then
|
|
|
|
// measure the prefix size, bump the input pointer by that amount, and
|
|
|
|
// cast to a pointer-to-target type.
|
|
|
|
|
|
|
|
// Given a type, an index vector and an element number N in that vector,
|
|
|
|
// calculate index X and the type that results by taking the first X-1
|
|
|
|
// elements of the type and splitting the Xth off. Return the prefix as
|
|
|
|
// well as the innermost Xth type.
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn split_type(ccx: &@crate_ctxt, t: ty::t, ixs: &[int], n: uint) ->
|
2011-08-04 16:20:09 -07:00
|
|
|
{prefix: [ty::t], target: ty::t} {
|
2011-08-13 00:09:25 -07:00
|
|
|
let len: uint = std::vec::len::<int>(ixs);
|
2011-05-12 17:24:54 +02:00
|
|
|
// We don't support 0-index or 1-index GEPs: The former is nonsense
|
2011-01-19 16:29:14 -08:00
|
|
|
// and the latter would only be meaningful if we supported non-0
|
|
|
|
// values for the 0th index (we don't).
|
|
|
|
|
2011-05-02 17:47:24 -07:00
|
|
|
assert (len > 1u);
|
2011-07-27 14:19:39 +02:00
|
|
|
if n == 0u {
|
2011-01-19 16:29:14 -08:00
|
|
|
// Since we're starting from a value that's a pointer to a
|
|
|
|
// *single* structure, the first index (in GEP-ese) should just be
|
|
|
|
// 0, to yield the pointee.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
assert (ixs[n] == 0);
|
2011-06-15 11:19:50 -07:00
|
|
|
ret split_type(ccx, t, ixs, n + 1u);
|
2011-01-19 16:29:14 -08:00
|
|
|
}
|
2011-05-02 17:47:24 -07:00
|
|
|
assert (n < len);
|
2011-08-19 15:16:48 -07:00
|
|
|
let ix: int = ixs[n];
|
|
|
|
let prefix: [ty::t] = [];
|
2011-07-27 14:19:39 +02:00
|
|
|
let i: int = 0;
|
|
|
|
while i < ix {
|
2011-08-19 15:16:48 -07:00
|
|
|
prefix += [ty::get_element_type(ccx.tcx, t, i as uint)];
|
2011-06-15 11:19:50 -07:00
|
|
|
i += 1;
|
2011-01-19 16:29:14 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let selected = ty::get_element_type(ccx.tcx, t, i as uint);
|
|
|
|
if n == len - 1u {
|
2011-01-19 16:29:14 -08:00
|
|
|
// We are at the innermost index.
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
ret {prefix: prefix, target: selected};
|
2011-01-19 16:29:14 -08:00
|
|
|
} else {
|
|
|
|
// Not the innermost index; call self recursively to dig deeper.
|
|
|
|
// Once we get an inner result, append it current prefix and
|
|
|
|
// return to caller.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let inner = split_type(ccx, selected, ixs, n + 1u);
|
2011-01-19 16:29:14 -08:00
|
|
|
prefix += inner.prefix;
|
2011-07-27 14:19:39 +02:00
|
|
|
ret {prefix: prefix with inner};
|
2011-01-19 16:29:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// We make a fake prefix tuple-type here; luckily for measuring sizes
|
|
|
|
// the tuple parens are associative so it doesn't matter that we've
|
|
|
|
// flattened the incoming structure.
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let s = split_type(bcx_ccx(cx), t, ixs, 0u);
|
2011-06-30 11:28:38 -07:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
let args = [];
|
|
|
|
for typ: ty::t in s.prefix { args += [typ]; }
|
2011-08-15 12:08:05 +02:00
|
|
|
let prefix_ty = ty::mk_tup(bcx_tcx(cx), args);
|
2011-06-30 11:28:38 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx = cx;
|
|
|
|
let sz = size_of(bcx, prefix_ty);
|
2011-08-01 15:08:16 -07:00
|
|
|
ret rslt(sz.bcx, bump_ptr(sz.bcx, s.target, base, sz.val));
|
2011-01-19 16:29:14 -08:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-03-04 15:08:33 -08:00
|
|
|
// Replacement for the LLVM 'GEP' instruction when field indexing into a tag.
|
|
|
|
// This function uses GEP_tup_like() above and automatically performs casts as
|
|
|
|
// appropriate. @llblobptr is the data part of a tag value; its actual type is
|
|
|
|
// meaningless, as it will be cast away.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn GEP_tag(cx: @block_ctxt, llblobptr: ValueRef, tag_id: &ast::def_id,
|
2011-08-04 16:20:09 -07:00
|
|
|
variant_id: &ast::def_id, ty_substs: &[ty::t], ix: int) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let variant = ty::tag_variant_with_id(bcx_tcx(cx), tag_id, variant_id);
|
2011-03-04 15:08:33 -08:00
|
|
|
// Synthesize a tuple type so that GEP_tup_like() can work its magic.
|
|
|
|
// Separately, store the type of the element we're interested in.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let arg_tys = variant.args;
|
|
|
|
let elem_ty = ty::mk_nil(bcx_tcx(cx)); // typestate infelicity
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let i = 0;
|
2011-08-19 15:16:48 -07:00
|
|
|
let true_arg_tys: [ty::t] = [];
|
2011-08-15 21:54:52 -07:00
|
|
|
for aty: ty::t in arg_tys {
|
2011-07-27 14:19:39 +02:00
|
|
|
let arg_ty = ty::substitute_type_params(bcx_tcx(cx), ty_substs, aty);
|
2011-08-19 15:16:48 -07:00
|
|
|
true_arg_tys += [arg_ty];
|
2011-07-27 14:19:39 +02:00
|
|
|
if i == ix { elem_ty = arg_ty; }
|
2011-03-04 15:08:33 -08:00
|
|
|
i += 1;
|
|
|
|
}
|
2011-08-15 12:08:05 +02:00
|
|
|
let tup_ty = ty::mk_tup(bcx_tcx(cx), true_arg_tys);
|
2011-03-04 15:08:33 -08:00
|
|
|
// Cast the blob pointer to the appropriate type, if we need to (i.e. if
|
|
|
|
// the blob pointer isn't dynamically sized).
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let llunionptr: ValueRef;
|
|
|
|
if !ty::type_has_dynamic_size(bcx_tcx(cx), tup_ty) {
|
|
|
|
let llty = type_of(bcx_ccx(cx), cx.sp, tup_ty);
|
2011-08-30 09:59:30 +02:00
|
|
|
llunionptr = TruncOrBitCast(cx, llblobptr, T_ptr(llty));
|
2011-06-15 11:19:50 -07:00
|
|
|
} else { llunionptr = llblobptr; }
|
2011-03-04 15:08:33 -08:00
|
|
|
// Do the GEP_tup_like().
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
let rs = GEP_tup_like(cx, tup_ty, llunionptr, [0, ix]);
|
2011-03-04 15:08:33 -08:00
|
|
|
// Cast the result to the appropriate type, if necessary.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let val;
|
|
|
|
if !ty::type_has_dynamic_size(bcx_tcx(cx), elem_ty) {
|
|
|
|
let llelemty = type_of(bcx_ccx(rs.bcx), cx.sp, elem_ty);
|
2011-08-30 09:59:30 +02:00
|
|
|
val = PointerCast(rs.bcx, rs.val, T_ptr(llelemty));
|
2011-06-24 19:04:08 +02:00
|
|
|
} else { val = rs.val; }
|
|
|
|
ret rslt(rs.bcx, val);
|
2011-03-04 15:08:33 -08:00
|
|
|
}
|
|
|
|
|
2011-06-03 17:16:28 -07:00
|
|
|
// trans_raw_malloc: expects a type indicating which pointer type we want and
|
|
|
|
// a size indicating how much space we want malloc'd.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_raw_malloc(cx: &@block_ctxt, llptr_ty: TypeRef, llsize: ValueRef) ->
|
2011-06-15 11:19:50 -07:00
|
|
|
result {
|
2010-12-02 17:43:05 -08:00
|
|
|
// FIXME: need a table to collect tydesc globals.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let tydesc = C_null(T_ptr(bcx_ccx(cx).tydesc_type));
|
|
|
|
let rval =
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(cx, bcx_ccx(cx).upcalls.malloc,
|
2011-08-19 15:16:48 -07:00
|
|
|
[cx.fcx.lltaskptr, llsize, tydesc]);
|
2011-08-30 09:59:30 +02:00
|
|
|
ret rslt(cx, PointerCast(cx, rval, llptr_ty));
|
2011-03-02 17:24:22 -08:00
|
|
|
}
|
|
|
|
|
2011-07-05 22:55:41 -07:00
|
|
|
// trans_shared_malloc: expects a type indicating which pointer type we want
|
|
|
|
// and a size indicating how much space we want malloc'd.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_shared_malloc(cx: &@block_ctxt, llptr_ty: TypeRef, llsize: ValueRef)
|
|
|
|
-> result {
|
2011-07-05 22:55:41 -07:00
|
|
|
// FIXME: need a table to collect tydesc globals.
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let tydesc = C_null(T_ptr(bcx_ccx(cx).tydesc_type));
|
|
|
|
let rval =
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(cx, bcx_ccx(cx).upcalls.shared_malloc,
|
2011-08-19 15:16:48 -07:00
|
|
|
[cx.fcx.lltaskptr, llsize, tydesc]);
|
2011-08-30 09:59:30 +02:00
|
|
|
ret rslt(cx, PointerCast(cx, rval, llptr_ty));
|
2011-07-05 22:55:41 -07:00
|
|
|
}
|
2011-06-03 17:16:28 -07:00
|
|
|
|
2011-07-28 15:44:51 -07:00
|
|
|
// trans_malloc_boxed_raw: expects an unboxed type and returns a pointer to
|
|
|
|
// enough space for something of that type, along with space for a reference
|
|
|
|
// count; in other words, it allocates a box for something of that type.
|
|
|
|
fn trans_malloc_boxed_raw(cx: &@block_ctxt, t: ty::t) -> result {
|
2011-03-07 14:05:16 -08:00
|
|
|
// Synthesize a fake box type structurally so we have something
|
|
|
|
// to measure the size of.
|
2011-06-03 17:16:28 -07:00
|
|
|
|
|
|
|
// We synthesize two types here because we want both the type of the
|
|
|
|
// pointer and the pointee. boxed_body is the type that we measure the
|
|
|
|
// size of; box_ptr is the type that's converted to a TypeRef and used as
|
|
|
|
// the pointer cast target in trans_raw_malloc.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:48:34 +02:00
|
|
|
// The mk_int here is the space being
|
|
|
|
// reserved for the refcount.
|
2011-08-19 15:16:48 -07:00
|
|
|
let boxed_body = ty::mk_tup(bcx_tcx(cx), [ty::mk_int(bcx_tcx(cx)), t]);
|
2011-07-27 14:19:39 +02:00
|
|
|
let box_ptr = ty::mk_imm_box(bcx_tcx(cx), t);
|
|
|
|
let sz = size_of(cx, boxed_body);
|
2011-07-28 15:44:51 -07:00
|
|
|
|
2011-06-03 17:16:28 -07:00
|
|
|
// Grab the TypeRef type of box_ptr, because that's what trans_raw_malloc
|
|
|
|
// wants.
|
2011-07-27 14:19:39 +02:00
|
|
|
let llty = type_of(bcx_ccx(cx), cx.sp, box_ptr);
|
2011-03-07 14:05:16 -08:00
|
|
|
ret trans_raw_malloc(sz.bcx, llty, sz.val);
|
2010-12-02 17:43:05 -08:00
|
|
|
}
|
|
|
|
|
2011-07-28 15:44:51 -07:00
|
|
|
// trans_malloc_boxed: usefully wraps trans_malloc_box_raw; allocates a box,
|
|
|
|
// initializes the reference count to 1, and pulls out the body and rc
|
|
|
|
fn trans_malloc_boxed(cx: &@block_ctxt, t: ty::t) ->
|
2011-08-19 15:16:48 -07:00
|
|
|
{bcx: @block_ctxt, box: ValueRef, body: ValueRef} {
|
2011-07-28 15:44:51 -07:00
|
|
|
let res = trans_malloc_boxed_raw(cx, t);
|
|
|
|
let box = res.val;
|
2011-08-19 15:16:48 -07:00
|
|
|
let rc = GEPi(res.bcx, box, [0, abi::box_rc_field_refcnt]);
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(res.bcx, C_int(1), rc);
|
2011-08-19 15:16:48 -07:00
|
|
|
let body = GEPi(res.bcx, box, [0, abi::box_rc_field_body]);
|
2011-08-02 16:24:38 -07:00
|
|
|
ret {bcx: res.bcx, box: res.val, body: body};
|
2011-07-28 15:44:51 -07:00
|
|
|
}
|
2010-12-02 17:43:05 -08:00
|
|
|
|
2010-12-20 10:23:37 -08:00
|
|
|
// Type descriptor and type glue stuff
|
|
|
|
|
|
|
|
// Given a type and a field index into its corresponding type descriptor,
|
|
|
|
// returns an LLVM ValueRef of that field from the tydesc, generating the
|
|
|
|
// tydesc if necessary.
|
2011-08-22 12:45:18 +02:00
|
|
|
fn field_of_tydesc(cx: &@block_ctxt, t: ty::t, escapes: bool, field: int) ->
|
2011-06-15 11:19:50 -07:00
|
|
|
result {
|
2011-08-13 00:09:25 -07:00
|
|
|
let ti = none::<@tydesc_info>;
|
2011-08-24 18:36:51 -07:00
|
|
|
let tydesc = get_tydesc(cx, t, escapes, tps_normal, ti).result;
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(tydesc.bcx,
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(tydesc.bcx, tydesc.val, [C_int(0), C_int(field)]));
|
2010-12-20 10:23:37 -08:00
|
|
|
}
|
2010-12-10 15:02:23 -08:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-01-27 17:07:52 -08:00
|
|
|
// Given a type containing ty params, build a vector containing a ValueRef for
|
2011-04-12 15:09:50 -07:00
|
|
|
// each of the ty params it uses (from the current frame) and a vector of the
|
|
|
|
// indices of the ty params present in the type. This is used solely for
|
2011-01-27 17:07:52 -08:00
|
|
|
// constructing derived tydescs.
|
2011-08-22 12:45:18 +02:00
|
|
|
fn linearize_ty_params(cx: &@block_ctxt, t: ty::t) ->
|
2011-08-04 16:20:09 -07:00
|
|
|
{params: [uint], descs: [ValueRef]} {
|
2011-08-19 15:16:48 -07:00
|
|
|
let param_vals: [ValueRef] = [];
|
|
|
|
let param_defs: [uint] = [];
|
2011-07-27 14:19:39 +02:00
|
|
|
type rr =
|
2011-08-04 16:20:09 -07:00
|
|
|
{cx: @block_ctxt, mutable vals: [ValueRef], mutable defs: [uint]};
|
2011-07-27 14:19:39 +02:00
|
|
|
|
|
|
|
fn linearizer(r: @rr, t: ty::t) {
|
|
|
|
alt ty::struct(bcx_tcx(r.cx), t) {
|
2011-08-19 15:16:48 -07:00
|
|
|
ty::ty_param(pid, _) {
|
2011-07-27 14:19:39 +02:00
|
|
|
let seen: bool = false;
|
2011-08-15 21:54:52 -07:00
|
|
|
for d: uint in r.defs { if d == pid { seen = true; } }
|
2011-08-19 15:16:48 -07:00
|
|
|
if !seen { r.vals += [r.cx.fcx.lltydescs[pid]]; r.defs += [pid]; }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ { }
|
2011-01-27 17:07:52 -08:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let x = @{cx: cx, mutable vals: param_vals, mutable defs: param_defs};
|
|
|
|
let f = bind linearizer(x, _);
|
2011-07-26 13:02:26 -07:00
|
|
|
ty::walk_ty(bcx_tcx(cx), f, t);
|
2011-07-27 14:19:39 +02:00
|
|
|
ret {params: x.defs, descs: x.vals};
|
2011-01-27 17:07:52 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_stack_local_derived_tydesc(cx: &@block_ctxt, llsz: ValueRef,
|
|
|
|
llalign: ValueRef, llroottydesc: ValueRef,
|
2011-08-24 18:36:51 -07:00
|
|
|
llfirstparam: ValueRef, n_params: uint,
|
|
|
|
obj_params: uint)
|
2011-08-19 15:16:48 -07:00
|
|
|
-> ValueRef {
|
2011-07-27 14:19:39 +02:00
|
|
|
let llmyroottydesc = alloca(cx, bcx_ccx(cx).tydesc_type);
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-08-24 18:36:51 -07:00
|
|
|
// By convention, desc 0 is the root descriptor.
|
2011-08-30 09:59:30 +02:00
|
|
|
llroottydesc = Load(cx, llroottydesc);
|
|
|
|
Store(cx, llroottydesc, llmyroottydesc);
|
2011-05-08 16:24:24 -07:00
|
|
|
|
2011-08-24 18:36:51 -07:00
|
|
|
// Store a pointer to the rest of the descriptors.
|
2011-08-04 11:25:09 -07:00
|
|
|
store_inbounds(cx, llfirstparam, llmyroottydesc,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::tydesc_field_first_param)]);
|
2011-08-04 11:25:09 -07:00
|
|
|
store_inbounds(cx, C_uint(n_params), llmyroottydesc,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::tydesc_field_n_params)]);
|
2011-08-04 11:25:09 -07:00
|
|
|
store_inbounds(cx, llsz, llmyroottydesc,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::tydesc_field_size)]);
|
2011-08-04 11:25:09 -07:00
|
|
|
store_inbounds(cx, llalign, llmyroottydesc,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::tydesc_field_align)]);
|
2011-08-24 18:36:51 -07:00
|
|
|
store_inbounds(cx, C_uint(obj_params), llmyroottydesc,
|
|
|
|
[C_int(0), C_int(abi::tydesc_field_obj_params)]);
|
2011-05-11 11:56:49 -07:00
|
|
|
ret llmyroottydesc;
|
2011-05-02 15:28:22 -07:00
|
|
|
}
|
|
|
|
|
2011-08-26 17:05:05 -07:00
|
|
|
// Objects and closures store their type parameters differently (in the object
|
|
|
|
// or closure itself rather than in the type descriptor).
|
2011-08-24 18:36:51 -07:00
|
|
|
tag ty_param_storage {
|
|
|
|
tps_normal;
|
|
|
|
tps_obj(uint);
|
2011-08-26 17:05:05 -07:00
|
|
|
tps_fn(uint);
|
2011-08-24 18:36:51 -07:00
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn get_derived_tydesc(cx: &@block_ctxt, t: ty::t, escapes: bool,
|
2011-08-24 18:36:51 -07:00
|
|
|
storage: ty_param_storage,
|
2011-08-12 07:15:18 -07:00
|
|
|
static_ti: &mutable option::t<@tydesc_info>) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt cx.fcx.derived_tydescs.find(t) {
|
|
|
|
some(info) {
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
|
|
|
|
// If the tydesc escapes in this context, the cached derived
|
|
|
|
// tydesc also has to be one that was marked as escaping.
|
2011-08-25 14:20:21 -07:00
|
|
|
if !(escapes && !info.escapes) && storage == tps_normal {
|
|
|
|
ret rslt(cx, info.lltydesc);
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
none. {/* fall through */ }
|
2011-05-11 11:56:49 -07:00
|
|
|
}
|
2011-08-25 14:20:21 -07:00
|
|
|
|
2011-07-26 13:02:26 -07:00
|
|
|
bcx_ccx(cx).stats.n_derived_tydescs += 1u;
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx = new_raw_block_ctxt(cx.fcx, cx.fcx.llderivedtydescs);
|
|
|
|
let tys = linearize_ty_params(bcx, t);
|
|
|
|
let root_ti = get_static_tydesc(bcx, t, tys.params);
|
2011-08-13 00:09:25 -07:00
|
|
|
static_ti = some::<@tydesc_info>(root_ti);
|
2011-05-12 15:42:12 -07:00
|
|
|
lazily_emit_all_tydesc_glue(cx, static_ti);
|
2011-07-27 14:19:39 +02:00
|
|
|
let root = root_ti.tydesc;
|
|
|
|
let sz = size_of(bcx, t);
|
2011-05-06 10:38:38 -07:00
|
|
|
bcx = sz.bcx;
|
2011-07-27 14:19:39 +02:00
|
|
|
let align = align_of(bcx, t);
|
2011-05-06 10:38:38 -07:00
|
|
|
bcx = align.bcx;
|
2011-08-24 18:36:51 -07:00
|
|
|
|
|
|
|
// Store the captured type descriptors in an alloca if the caller isn't
|
|
|
|
// promising to do so itself.
|
|
|
|
let n_params = ty::count_ty_params(bcx_tcx(bcx), t);
|
|
|
|
|
|
|
|
assert (n_params == std::vec::len::<uint>(tys.params));
|
|
|
|
assert (n_params == std::vec::len::<ValueRef>(tys.descs));
|
|
|
|
|
|
|
|
let llparamtydescs =
|
|
|
|
alloca(bcx, T_array(T_ptr(bcx_ccx(bcx).tydesc_type), n_params + 1u));
|
|
|
|
let i = 0;
|
|
|
|
|
|
|
|
// If the type descriptor escapes, we need to add in the root as
|
|
|
|
// the first parameter, because upcall_get_type_desc() expects it.
|
2011-07-27 14:19:39 +02:00
|
|
|
if escapes {
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(bcx, root, GEPi(bcx, llparamtydescs, [0, 0]));
|
2011-05-06 11:35:04 -07:00
|
|
|
i += 1;
|
2011-08-24 18:36:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for td: ValueRef in tys.descs {
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(bcx, td, GEPi(bcx, llparamtydescs, [0, i]));
|
2011-08-24 18:36:51 -07:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let llfirstparam =
|
2011-08-30 09:59:30 +02:00
|
|
|
PointerCast(bcx, llparamtydescs,
|
2011-08-24 18:36:51 -07:00
|
|
|
T_ptr(T_ptr(bcx_ccx(bcx).tydesc_type)));
|
|
|
|
|
2011-08-26 17:05:05 -07:00
|
|
|
// The top bit indicates whether this type descriptor describes an object
|
|
|
|
// (0) or a function (1).
|
2011-08-24 18:36:51 -07:00
|
|
|
let obj_params;
|
|
|
|
alt storage {
|
|
|
|
tps_normal. { obj_params = 0u; }
|
|
|
|
tps_obj(np) { obj_params = np; }
|
2011-08-26 17:05:05 -07:00
|
|
|
tps_fn(np) { obj_params = 0x80000000u | np; }
|
2011-08-24 18:36:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let v;
|
|
|
|
if escapes {
|
2011-07-27 14:19:39 +02:00
|
|
|
let td_val =
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(bcx, bcx_ccx(bcx).upcalls.get_type_desc,
|
2011-08-19 15:16:48 -07:00
|
|
|
[bcx.fcx.lltaskptr, C_null(T_ptr(T_nil())), sz.val,
|
2011-08-24 18:36:51 -07:00
|
|
|
align.val, C_uint(1u + n_params),
|
|
|
|
llfirstparam, C_uint(obj_params)]);
|
2011-05-11 11:56:49 -07:00
|
|
|
v = td_val;
|
2011-05-06 10:38:38 -07:00
|
|
|
} else {
|
2011-08-24 18:36:51 -07:00
|
|
|
v = trans_stack_local_derived_tydesc(bcx, sz.val, align.val, root,
|
|
|
|
llfirstparam, n_params,
|
|
|
|
obj_params);
|
2011-05-06 10:38:38 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
bcx.fcx.derived_tydescs.insert(t, {lltydesc: v, escapes: escapes});
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(cx, v);
|
2011-05-06 10:38:38 -07:00
|
|
|
}
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
type get_tydesc_result = {kind: tydesc_kind, result: result};
|
2011-08-17 19:11:01 -07:00
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn get_tydesc(cx: &@block_ctxt, orig_t: ty::t, escapes: bool,
|
2011-08-24 18:36:51 -07:00
|
|
|
storage: ty_param_storage,
|
2011-08-19 15:16:48 -07:00
|
|
|
static_ti: &mutable option::t<@tydesc_info>) ->
|
|
|
|
get_tydesc_result {
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let t = ty::strip_cname(bcx_tcx(cx), orig_t);
|
2011-07-20 19:04:45 -07:00
|
|
|
|
2011-06-29 17:29:24 -07:00
|
|
|
// Is the supplied type a type param? If so, return the passed-in tydesc.
|
2011-07-27 14:19:39 +02:00
|
|
|
alt ty::type_param(bcx_tcx(cx), t) {
|
2011-08-10 15:57:03 -07:00
|
|
|
some(id) {
|
2011-08-17 19:11:01 -07:00
|
|
|
if id < vec::len(cx.fcx.lltydescs) {
|
2011-08-19 15:16:48 -07:00
|
|
|
ret {kind: tk_param, result: rslt(cx, cx.fcx.lltydescs[id])};
|
|
|
|
} else {
|
2011-08-26 23:39:08 -07:00
|
|
|
bcx_tcx(cx).sess.span_bug(
|
|
|
|
cx.sp,
|
2011-08-27 16:36:48 -07:00
|
|
|
~"Unbound typaram in get_tydesc: " +
|
|
|
|
~"orig_t = " +
|
|
|
|
ty_to_str(bcx_tcx(cx), orig_t) +
|
|
|
|
~" ty_param = " +
|
|
|
|
std::uint::str(id));
|
2011-08-10 15:57:03 -07:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
none. {/* fall through */ }
|
2010-12-10 15:02:23 -08:00
|
|
|
}
|
2011-01-28 15:28:20 -08:00
|
|
|
|
2011-06-29 17:29:24 -07:00
|
|
|
// Does it contain a type param? If so, generate a derived tydesc.
|
2011-07-27 14:19:39 +02:00
|
|
|
if ty::type_contains_params(bcx_tcx(cx), t) {
|
2011-08-19 15:16:48 -07:00
|
|
|
ret {kind: tk_derived,
|
2011-08-24 18:36:51 -07:00
|
|
|
result: get_derived_tydesc(cx, t, escapes, storage, static_ti)};
|
2010-12-20 15:23:24 -08:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-29 17:29:24 -07:00
|
|
|
// Otherwise, generate a tydesc if necessary, and return it.
|
2011-08-19 15:16:48 -07:00
|
|
|
let info = get_static_tydesc(cx, t, []);
|
2011-08-13 00:09:25 -07:00
|
|
|
static_ti = some::<@tydesc_info>(info);
|
2011-08-19 15:16:48 -07:00
|
|
|
ret {kind: tk_static, result: rslt(cx, info.tydesc)};
|
2011-04-26 17:19:44 -07:00
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn get_static_tydesc(cx: &@block_ctxt, orig_t: ty::t, ty_params: &[uint]) ->
|
2011-07-27 14:19:39 +02:00
|
|
|
@tydesc_info {
|
|
|
|
let t = ty::strip_cname(bcx_tcx(cx), orig_t);
|
2011-07-20 19:04:45 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
|
|
|
|
alt bcx_ccx(cx).tydescs.find(t) {
|
|
|
|
some(info) { ret info; }
|
|
|
|
none. {
|
|
|
|
bcx_ccx(cx).stats.n_static_tydescs += 1u;
|
|
|
|
let info = declare_tydesc(cx.fcx.lcx, cx.sp, t, ty_params);
|
|
|
|
bcx_ccx(cx).tydescs.insert(t, info);
|
|
|
|
ret info;
|
|
|
|
}
|
2010-12-20 15:23:24 -08:00
|
|
|
}
|
2010-12-10 15:02:23 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn set_no_inline(f: ValueRef) {
|
2011-06-15 11:19:50 -07:00
|
|
|
llvm::LLVMAddFunctionAttr(f,
|
|
|
|
lib::llvm::LLVMNoInlineAttribute as
|
|
|
|
lib::llvm::llvm::Attribute);
|
2011-05-20 14:57:52 -07:00
|
|
|
}
|
|
|
|
|
2011-07-06 17:02:56 -07:00
|
|
|
// Tell LLVM to emit the information necessary to unwind the stack for the
|
|
|
|
// function f.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn set_uwtable(f: ValueRef) {
|
2011-06-15 11:19:50 -07:00
|
|
|
llvm::LLVMAddFunctionAttr(f,
|
|
|
|
lib::llvm::LLVMUWTableAttribute as
|
|
|
|
lib::llvm::llvm::Attribute);
|
2011-05-24 13:47:27 -04:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn set_always_inline(f: ValueRef) {
|
2011-06-15 11:19:50 -07:00
|
|
|
llvm::LLVMAddFunctionAttr(f,
|
|
|
|
lib::llvm::LLVMAlwaysInlineAttribute as
|
|
|
|
lib::llvm::llvm::Attribute);
|
2011-05-20 14:57:52 -07:00
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn set_glue_inlining(cx: &@local_ctxt, f: ValueRef, t: ty::t) {
|
2011-07-27 14:19:39 +02:00
|
|
|
if ty::type_is_structural(cx.ccx.tcx, t) {
|
2011-05-20 14:57:52 -07:00
|
|
|
set_no_inline(f);
|
2011-06-15 11:19:50 -07:00
|
|
|
} else { set_always_inline(f); }
|
2011-05-20 14:57:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-12 15:42:12 -07:00
|
|
|
// Generates the declaration for (but doesn't emit) a type descriptor.
|
2011-08-22 12:45:18 +02:00
|
|
|
fn declare_tydesc(cx: &@local_ctxt, sp: &span, t: ty::t, ty_params: &[uint])
|
2011-07-27 14:19:39 +02:00
|
|
|
-> @tydesc_info {
|
2011-08-26 23:39:08 -07:00
|
|
|
log ~"+++ declare_tydesc " + ty_to_str(cx.ccx.tcx, t);
|
2011-07-27 14:19:39 +02:00
|
|
|
let ccx = cx.ccx;
|
|
|
|
let llsize;
|
|
|
|
let llalign;
|
|
|
|
if !ty::type_has_dynamic_size(ccx.tcx, t) {
|
|
|
|
let llty = type_of(ccx, sp, t);
|
2011-03-02 16:23:14 -08:00
|
|
|
llsize = llsize_of(llty);
|
|
|
|
llalign = llalign_of(llty);
|
|
|
|
} else {
|
|
|
|
// These will be overwritten as the derived tydesc is generated, so
|
|
|
|
// we create placeholder values.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-03-02 16:23:14 -08:00
|
|
|
llsize = C_int(0);
|
|
|
|
llalign = C_int(0);
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let name;
|
|
|
|
if cx.ccx.sess.get_opts().debuginfo {
|
2011-08-25 21:04:04 -07:00
|
|
|
name = mangle_internal_name_by_type_only(cx.ccx, t, ~"tydesc");
|
2011-05-10 17:58:11 -07:00
|
|
|
name = sanitize(name);
|
2011-08-25 21:04:04 -07:00
|
|
|
} else { name = mangle_internal_name_by_seq(cx.ccx, ~"tydesc"); }
|
2011-08-26 15:36:18 -07:00
|
|
|
let gvar = istr::as_buf(name, { |buf|
|
|
|
|
llvm::LLVMAddGlobal(ccx.llmod, ccx.tydesc_type, buf)
|
|
|
|
});
|
2011-07-27 14:19:39 +02:00
|
|
|
let info =
|
|
|
|
@{ty: t,
|
|
|
|
tydesc: gvar,
|
|
|
|
size: llsize,
|
|
|
|
align: llalign,
|
2011-08-19 10:24:13 +02:00
|
|
|
mutable take_glue: none::<ValueRef>,
|
2011-08-13 00:09:25 -07:00
|
|
|
mutable drop_glue: none::<ValueRef>,
|
|
|
|
mutable free_glue: none::<ValueRef>,
|
|
|
|
mutable cmp_glue: none::<ValueRef>,
|
2011-08-24 20:30:20 +02:00
|
|
|
mutable copy_glue: none::<ValueRef>,
|
2011-07-27 14:19:39 +02:00
|
|
|
ty_params: ty_params};
|
2011-08-26 23:39:08 -07:00
|
|
|
log ~"--- declare_tydesc " + ty_to_str(cx.ccx.tcx, t);
|
2011-04-26 17:19:44 -07:00
|
|
|
ret info;
|
2010-12-10 15:02:23 -08:00
|
|
|
}
|
|
|
|
|
2011-08-22 11:41:49 +02:00
|
|
|
tag glue_helper {
|
2011-08-22 12:45:18 +02:00
|
|
|
default_helper(fn(&@block_ctxt, ValueRef, ty::t));
|
|
|
|
copy_helper(fn(&@block_ctxt, ValueRef, ValueRef, ty::t));
|
2011-08-22 11:41:49 +02:00
|
|
|
}
|
2011-04-18 10:56:52 -07:00
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn declare_generic_glue(cx: &@local_ctxt, t: ty::t, llfnty: TypeRef,
|
2011-08-26 21:34:56 -07:00
|
|
|
name: &istr) -> ValueRef {
|
|
|
|
let name = name;
|
2011-07-27 14:19:39 +02:00
|
|
|
let fn_nm;
|
|
|
|
if cx.ccx.sess.get_opts().debuginfo {
|
2011-08-25 21:04:04 -07:00
|
|
|
fn_nm = mangle_internal_name_by_type_only(cx.ccx, t, ~"glue_" + name);
|
2011-05-04 15:36:42 -07:00
|
|
|
fn_nm = sanitize(fn_nm);
|
2011-08-25 21:04:04 -07:00
|
|
|
} else { fn_nm = mangle_internal_name_by_seq(cx.ccx, ~"glue_" + name); }
|
2011-08-26 21:34:56 -07:00
|
|
|
let llfn = decl_cdecl_fn(cx.ccx.llmod, fn_nm, llfnty);
|
2011-05-20 14:57:52 -07:00
|
|
|
set_glue_inlining(cx, llfn, t);
|
2011-03-20 15:05:13 -07:00
|
|
|
ret llfn;
|
2011-03-04 17:22:43 -08:00
|
|
|
}
|
2010-12-10 15:02:23 -08:00
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn make_generic_glue_inner(cx: &@local_ctxt, sp: &span, t: ty::t,
|
2011-07-27 14:19:39 +02:00
|
|
|
llfn: ValueRef,
|
2011-08-22 11:41:49 +02:00
|
|
|
helper: &glue_helper,
|
2011-08-04 16:20:09 -07:00
|
|
|
ty_params: &[uint]) -> ValueRef {
|
2011-07-27 14:19:39 +02:00
|
|
|
let fcx = new_fn_ctxt(cx, sp, llfn);
|
2011-06-15 11:19:50 -07:00
|
|
|
llvm::LLVMSetLinkage(llfn,
|
|
|
|
lib::llvm::LLVMInternalLinkage as llvm::Linkage);
|
2011-05-12 15:42:12 -07:00
|
|
|
cx.ccx.stats.n_glues_created += 1u;
|
2011-04-19 11:25:40 -07:00
|
|
|
// Any nontrivial glue is with values passed *by alias*; this is a
|
|
|
|
// requirement since in many contexts glue is invoked indirectly and
|
|
|
|
// the caller has no idea if it's dealing with something that can be
|
|
|
|
// passed by value.
|
2011-04-12 12:06:20 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let llty;
|
|
|
|
if ty::type_has_dynamic_size(cx.ccx.tcx, t) {
|
2011-04-19 11:25:40 -07:00
|
|
|
llty = T_ptr(T_i8());
|
2011-06-15 11:19:50 -07:00
|
|
|
} else { llty = T_ptr(type_of(cx.ccx, sp, t)); }
|
2011-08-13 00:09:25 -07:00
|
|
|
let ty_param_count = std::vec::len::<uint>(ty_params);
|
2011-07-27 14:19:39 +02:00
|
|
|
let lltyparams = llvm::LLVMGetParam(llfn, 3u);
|
|
|
|
let copy_args_bcx = new_raw_block_ctxt(fcx, fcx.llcopyargs);
|
2011-08-19 15:16:48 -07:00
|
|
|
let lltydescs = [mutable];
|
2011-07-27 14:19:39 +02:00
|
|
|
let p = 0u;
|
|
|
|
while p < ty_param_count {
|
2011-08-30 09:59:30 +02:00
|
|
|
let llparam = GEP(copy_args_bcx, lltyparams, [C_int(p as int)]);
|
|
|
|
llparam = Load(copy_args_bcx, llparam);
|
2011-08-19 15:16:48 -07:00
|
|
|
std::vec::grow_set(lltydescs, ty_params[p], 0 as ValueRef, llparam);
|
2011-04-19 11:25:40 -07:00
|
|
|
p += 1u;
|
|
|
|
}
|
2011-07-07 18:30:35 -07:00
|
|
|
|
|
|
|
// TODO: Implement some kind of freeze operation in the standard library.
|
2011-08-19 15:16:48 -07:00
|
|
|
let lltydescs_frozen = [];
|
|
|
|
for lltydesc: ValueRef in lltydescs { lltydescs_frozen += [lltydesc]; }
|
2011-07-07 18:30:35 -07:00
|
|
|
fcx.lltydescs = lltydescs_frozen;
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx = new_top_block_ctxt(fcx);
|
|
|
|
let lltop = bcx.llbb;
|
|
|
|
let llrawptr0 = llvm::LLVMGetParam(llfn, 4u);
|
2011-08-30 09:59:30 +02:00
|
|
|
let llval0 = BitCast(bcx, llrawptr0, llty);
|
2011-08-22 11:41:49 +02:00
|
|
|
alt helper {
|
|
|
|
default_helper(helper) {
|
|
|
|
helper(bcx, llval0, t);
|
|
|
|
}
|
|
|
|
copy_helper(helper) {
|
2011-08-22 12:03:11 +02:00
|
|
|
let llrawptr1 = llvm::LLVMGetParam(llfn, 5u);
|
2011-08-30 09:59:30 +02:00
|
|
|
let llval1 = BitCast(bcx, llrawptr1, llty);
|
2011-08-22 11:41:49 +02:00
|
|
|
helper(bcx, llval0, llval1, t);
|
|
|
|
}
|
|
|
|
}
|
2011-05-11 11:56:49 -07:00
|
|
|
finish_fn(fcx, lltop);
|
2010-12-10 15:02:23 -08:00
|
|
|
ret llfn;
|
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn make_generic_glue(cx: &@local_ctxt, sp: &span, t: ty::t, llfn: ValueRef,
|
2011-08-22 11:41:49 +02:00
|
|
|
helper: &glue_helper, ty_params: &[uint],
|
2011-08-26 21:34:56 -07:00
|
|
|
name: &istr) -> ValueRef {
|
2011-07-19 11:56:46 -07:00
|
|
|
if !cx.ccx.sess.get_opts().stats {
|
|
|
|
ret make_generic_glue_inner(cx, sp, t, llfn, helper, ty_params);
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let start = time::get_time();
|
|
|
|
let llval = make_generic_glue_inner(cx, sp, t, llfn, helper, ty_params);
|
|
|
|
let end = time::get_time();
|
2011-08-26 21:34:56 -07:00
|
|
|
log_fn_time(cx.ccx, ~"glue " + name + ~" " +
|
|
|
|
ty_to_short_str(cx.ccx.tcx, t),
|
2011-07-19 11:56:46 -07:00
|
|
|
start, end);
|
|
|
|
ret llval;
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn emit_tydescs(ccx: &@crate_ctxt) {
|
2011-08-15 21:54:52 -07:00
|
|
|
for each pair: @{key: ty::t, val: @tydesc_info} in ccx.tydescs.items() {
|
2011-07-27 14:19:39 +02:00
|
|
|
let glue_fn_ty = T_ptr(T_glue_fn(*ccx));
|
|
|
|
let cmp_fn_ty = T_ptr(T_cmp_glue_fn(*ccx));
|
2011-08-24 20:30:20 +02:00
|
|
|
let copy_fn_ty = T_ptr(T_copy_glue_fn(*ccx));
|
2011-07-27 14:19:39 +02:00
|
|
|
let ti = pair.val;
|
2011-08-19 10:24:13 +02:00
|
|
|
let take_glue =
|
|
|
|
alt { ti.take_glue } {
|
2011-07-27 14:19:39 +02:00
|
|
|
none. { ccx.stats.n_null_glues += 1u; C_null(glue_fn_ty) }
|
|
|
|
some(v) { ccx.stats.n_real_glues += 1u; v }
|
2011-06-15 11:19:50 -07:00
|
|
|
};
|
2011-07-27 14:19:39 +02:00
|
|
|
let drop_glue =
|
|
|
|
alt { ti.drop_glue } {
|
|
|
|
none. { ccx.stats.n_null_glues += 1u; C_null(glue_fn_ty) }
|
|
|
|
some(v) { ccx.stats.n_real_glues += 1u; v }
|
2011-06-15 11:19:50 -07:00
|
|
|
};
|
2011-07-27 14:19:39 +02:00
|
|
|
let free_glue =
|
|
|
|
alt { ti.free_glue } {
|
|
|
|
none. { ccx.stats.n_null_glues += 1u; C_null(glue_fn_ty) }
|
|
|
|
some(v) { ccx.stats.n_real_glues += 1u; v }
|
2011-06-15 11:19:50 -07:00
|
|
|
};
|
2011-07-27 14:19:39 +02:00
|
|
|
let cmp_glue =
|
|
|
|
alt { ti.cmp_glue } {
|
|
|
|
none. { ccx.stats.n_null_glues += 1u; C_null(cmp_fn_ty) }
|
|
|
|
some(v) { ccx.stats.n_real_glues += 1u; v }
|
2011-06-15 11:19:50 -07:00
|
|
|
};
|
2011-08-24 20:30:20 +02:00
|
|
|
let copy_glue =
|
|
|
|
alt { ti.copy_glue } {
|
|
|
|
none. { ccx.stats.n_null_glues += 1u; C_null(copy_fn_ty) }
|
|
|
|
some(v) { ccx.stats.n_real_glues += 1u; v }
|
|
|
|
};
|
2011-08-04 11:25:09 -07:00
|
|
|
|
|
|
|
let shape = shape::shape_of(ccx, pair.key);
|
|
|
|
let shape_tables =
|
|
|
|
llvm::LLVMConstPointerCast(ccx.shape_cx.llshapetables,
|
|
|
|
T_ptr(T_i8()));
|
|
|
|
|
|
|
|
let tydesc =
|
2011-07-14 15:19:17 -04:00
|
|
|
C_named_struct(ccx.tydesc_type,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_null(T_ptr(T_ptr(ccx.tydesc_type))),
|
|
|
|
ti.size, // size
|
|
|
|
ti.align, // align
|
2011-08-19 10:24:13 +02:00
|
|
|
take_glue, // take_glue
|
2011-08-19 15:16:48 -07:00
|
|
|
drop_glue, // drop_glue
|
|
|
|
free_glue, // free_glue
|
2011-08-24 20:30:20 +02:00
|
|
|
copy_glue, // copy_glue
|
2011-08-19 15:16:48 -07:00
|
|
|
C_null(glue_fn_ty), // sever_glue
|
|
|
|
C_null(glue_fn_ty), // mark_glue
|
|
|
|
C_null(glue_fn_ty), // is_stateful
|
|
|
|
cmp_glue, // cmp_glue
|
|
|
|
C_shape(ccx, shape), // shape
|
|
|
|
shape_tables, // shape_tables
|
2011-08-24 18:36:51 -07:00
|
|
|
C_int(0), // n_params
|
|
|
|
C_int(0)]); // n_obj_params
|
2011-07-27 14:19:39 +02:00
|
|
|
|
|
|
|
let gvar = ti.tydesc;
|
2011-05-12 15:42:12 -07:00
|
|
|
llvm::LLVMSetInitializer(gvar, tydesc);
|
|
|
|
llvm::LLVMSetGlobalConstant(gvar, True);
|
2011-06-15 11:19:50 -07:00
|
|
|
llvm::LLVMSetLinkage(gvar,
|
|
|
|
lib::llvm::LLVMInternalLinkage as llvm::Linkage);
|
2011-05-12 15:42:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-24 20:30:20 +02:00
|
|
|
// NOTE this is currently just a complicated way to do memmove. I'm working on
|
|
|
|
// a representation of ivecs that will need pointers into itself, which must
|
|
|
|
// be adjusted when copying. Will flesh this out when the time comes.
|
|
|
|
fn make_copy_glue(cx: &@block_ctxt, src: ValueRef, dst: ValueRef, t: ty::t) {
|
|
|
|
let bcx = memmove_ty(cx, dst, src, t).bcx;
|
|
|
|
build_return(bcx);
|
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn make_take_glue(cx: &@block_ctxt, v: ValueRef, t: ty::t) {
|
2011-08-25 10:18:02 +02:00
|
|
|
let bcx = cx;
|
2011-04-12 12:06:20 -07:00
|
|
|
// NB: v is an *alias* of type t here, not a direct value.
|
2011-08-25 10:18:02 +02:00
|
|
|
if ty::type_is_boxed(bcx_tcx(bcx), t) {
|
2011-08-30 09:59:30 +02:00
|
|
|
bcx = incr_refcnt_of_boxed(bcx, Load(bcx, v)).bcx;
|
2011-08-25 10:18:02 +02:00
|
|
|
} else if ty::type_is_structural(bcx_tcx(bcx), t) {
|
2011-08-26 11:20:10 +02:00
|
|
|
bcx = iter_structural_ty(bcx, v, t, take_ty).bcx;
|
2011-08-25 10:18:02 +02:00
|
|
|
} else if ty::type_is_ivec(bcx_tcx(bcx), t) {
|
|
|
|
bcx = ivec::duplicate(bcx, v);
|
|
|
|
bcx = ivec::iter_ivec(bcx, v, t, take_ty).bcx;
|
|
|
|
}
|
2011-08-17 13:58:49 -07:00
|
|
|
|
2011-08-17 17:49:54 -07:00
|
|
|
build_return(bcx);
|
2010-12-10 16:13:52 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn incr_refcnt_of_boxed(cx: &@block_ctxt, box_ptr: ValueRef) -> result {
|
|
|
|
let rc_ptr =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(cx, box_ptr, [C_int(0), C_int(abi::box_rc_field_refcnt)]);
|
|
|
|
let rc = Load(cx, rc_ptr);
|
2011-08-26 21:34:56 -07:00
|
|
|
let rc_adj_cx = new_sub_block_ctxt(cx, ~"rc++");
|
|
|
|
let next_cx = new_sub_block_ctxt(cx, ~"next");
|
2011-07-27 14:19:39 +02:00
|
|
|
let const_test =
|
2011-08-30 09:59:30 +02:00
|
|
|
ICmp(cx, lib::llvm::LLVMIntEQ, C_int(abi::const_refcount as int),
|
2011-06-15 11:19:50 -07:00
|
|
|
rc);
|
2011-08-30 09:59:30 +02:00
|
|
|
CondBr(cx, const_test, next_cx.llbb, rc_adj_cx.llbb);
|
|
|
|
rc = Add(rc_adj_cx, rc, C_int(1));
|
|
|
|
Store(rc_adj_cx, rc, rc_ptr);
|
|
|
|
Br(rc_adj_cx, next_cx.llbb);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(next_cx, C_nil());
|
2010-12-10 16:13:52 -08:00
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn make_free_glue(cx: &@block_ctxt, v0: ValueRef, t: ty::t) {
|
2011-05-18 17:28:08 -07:00
|
|
|
// NB: v is an *alias* of type t here, not a direct value.
|
2011-07-27 14:48:34 +02:00
|
|
|
let rs =
|
2011-07-27 14:19:39 +02:00
|
|
|
alt ty::struct(bcx_tcx(cx), t) {
|
2011-08-10 22:47:18 -07:00
|
|
|
ty::ty_str. {
|
2011-08-30 09:59:30 +02:00
|
|
|
let v = Load(cx, v0);
|
2011-08-10 22:47:18 -07:00
|
|
|
if !bcx_ccx(cx).sess.get_opts().do_gc {
|
|
|
|
trans_non_gc_free(cx, v)
|
2011-08-19 15:16:48 -07:00
|
|
|
} else { rslt(cx, C_nil()) }
|
2011-08-10 22:47:18 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ty::ty_box(body_mt) {
|
2011-08-30 09:59:30 +02:00
|
|
|
let v = Load(cx, v0);
|
2011-07-27 14:19:39 +02:00
|
|
|
let body =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(cx, v, [C_int(0), C_int(abi::box_rc_field_body)]);
|
2011-07-27 14:19:39 +02:00
|
|
|
let body_ty = body_mt.ty;
|
2011-08-26 11:20:10 +02:00
|
|
|
let rs = drop_ty(cx, body, body_ty);
|
2011-08-10 22:47:18 -07:00
|
|
|
if !bcx_ccx(cx).sess.get_opts().do_gc {
|
|
|
|
trans_non_gc_free(rs.bcx, v)
|
2011-08-19 15:16:48 -07:00
|
|
|
} else { rslt(cx, C_nil()) }
|
2011-08-10 17:23:46 -07:00
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
ty::ty_uniq(_) { fail "free uniq unimplemented"; }
|
2011-07-27 14:19:39 +02:00
|
|
|
ty::ty_obj(_) {
|
2011-08-16 17:38:51 -07:00
|
|
|
// Call through the obj's own fields-drop glue first.
|
|
|
|
// Then free the body.
|
2011-07-27 14:19:39 +02:00
|
|
|
let box_cell =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(cx, v0, [C_int(0), C_int(abi::obj_field_box)]);
|
|
|
|
let b = Load(cx, box_cell);
|
2011-07-27 14:19:39 +02:00
|
|
|
let ccx = bcx_ccx(cx);
|
|
|
|
let llbox_ty = T_opaque_obj_ptr(*ccx);
|
2011-08-30 09:59:30 +02:00
|
|
|
b = PointerCast(cx, b, llbox_ty);
|
2011-07-27 14:19:39 +02:00
|
|
|
let body =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(cx, b, [C_int(0), C_int(abi::box_rc_field_body)]);
|
2011-07-27 14:19:39 +02:00
|
|
|
let tydescptr =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(cx, body,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::obj_body_elt_tydesc)]);
|
2011-08-30 09:59:30 +02:00
|
|
|
let tydesc = Load(cx, tydescptr);
|
2011-08-13 00:09:25 -07:00
|
|
|
let ti = none::<@tydesc_info>;
|
2011-07-29 13:36:57 +02:00
|
|
|
call_tydesc_glue_full(cx, body, tydesc,
|
2011-05-18 17:28:08 -07:00
|
|
|
abi::tydesc_field_drop_glue, ti);
|
2011-08-19 15:16:48 -07:00
|
|
|
if !bcx_ccx(cx).sess.get_opts().do_gc {
|
2011-08-10 22:47:18 -07:00
|
|
|
trans_non_gc_free(cx, b)
|
2011-08-19 15:16:48 -07:00
|
|
|
} else { rslt(cx, C_nil()) }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_fn(_, _, _, _, _) {
|
2011-08-16 17:38:51 -07:00
|
|
|
// Call through the closure's own fields-drop glue first.
|
|
|
|
// Then free the body.
|
2011-07-27 14:19:39 +02:00
|
|
|
let box_cell =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(cx, v0, [C_int(0), C_int(abi::fn_field_box)]);
|
|
|
|
let v = Load(cx, box_cell);
|
2011-07-27 14:19:39 +02:00
|
|
|
let body =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(cx, v, [C_int(0), C_int(abi::box_rc_field_body)]);
|
2011-07-27 14:19:39 +02:00
|
|
|
let bindings =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(cx, body,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::closure_elt_bindings)]);
|
2011-07-27 14:19:39 +02:00
|
|
|
let tydescptr =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(cx, body,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::closure_elt_tydesc)]);
|
2011-08-13 00:09:25 -07:00
|
|
|
let ti = none::<@tydesc_info>;
|
2011-08-30 09:59:30 +02:00
|
|
|
call_tydesc_glue_full(cx, bindings, Load(cx, tydescptr),
|
2011-05-18 17:28:08 -07:00
|
|
|
abi::tydesc_field_drop_glue, ti);
|
2011-08-19 15:16:48 -07:00
|
|
|
if !bcx_ccx(cx).sess.get_opts().do_gc {
|
2011-08-10 22:47:18 -07:00
|
|
|
trans_non_gc_free(cx, v)
|
2011-08-19 15:16:48 -07:00
|
|
|
} else { rslt(cx, C_nil()) }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ { rslt(cx, C_nil()) }
|
|
|
|
};
|
2011-08-17 13:58:49 -07:00
|
|
|
|
2011-08-17 17:49:54 -07:00
|
|
|
build_return(rs.bcx);
|
2011-05-18 17:28:08 -07:00
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn make_drop_glue(cx: &@block_ctxt, v0: ValueRef, t: ty::t) {
|
2011-05-18 17:28:08 -07:00
|
|
|
// NB: v0 is an *alias* of type t here, not a direct value.
|
2011-07-27 14:19:39 +02:00
|
|
|
let ccx = bcx_ccx(cx);
|
2011-07-27 14:48:34 +02:00
|
|
|
let rs =
|
2011-07-27 14:19:39 +02:00
|
|
|
alt ty::struct(ccx.tcx, t) {
|
|
|
|
ty::ty_str. { decr_refcnt_maybe_free(cx, v0, v0, t) }
|
2011-08-25 10:18:02 +02:00
|
|
|
ty::ty_vec(_) {
|
|
|
|
rslt(ivec::make_drop_glue(cx, v0, t), C_nil())
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-22 14:33:31 -07:00
|
|
|
ty::ty_istr. {
|
2011-08-25 10:18:02 +02:00
|
|
|
rslt(ivec::make_drop_glue(cx, v0, t), C_nil())
|
2011-08-22 14:33:31 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ty::ty_box(_) { decr_refcnt_maybe_free(cx, v0, v0, t) }
|
2011-08-30 09:59:30 +02:00
|
|
|
ty::ty_uniq(_) { trans_shared_free(cx, Load(cx, v0)) }
|
2011-07-27 14:19:39 +02:00
|
|
|
ty::ty_obj(_) {
|
|
|
|
let box_cell =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(cx, v0, [C_int(0), C_int(abi::obj_field_box)]);
|
2011-06-24 19:04:08 +02:00
|
|
|
decr_refcnt_maybe_free(cx, box_cell, v0, t)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_res(did, inner, tps) {
|
2011-06-30 14:46:17 +02:00
|
|
|
trans_res_drop(cx, v0, did, inner, tps)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_fn(_, _, _, _, _) {
|
|
|
|
let box_cell =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(cx, v0, [C_int(0), C_int(abi::fn_field_box)]);
|
2011-06-24 19:04:08 +02:00
|
|
|
decr_refcnt_maybe_free(cx, box_cell, v0, t)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ {
|
|
|
|
if ty::type_has_pointers(ccx.tcx, t) &&
|
2011-08-25 10:18:02 +02:00
|
|
|
ty::type_is_structural(ccx.tcx, t) {
|
2011-08-26 11:20:10 +02:00
|
|
|
iter_structural_ty(cx, v0, t, drop_ty)
|
2011-06-24 19:04:08 +02:00
|
|
|
} else { rslt(cx, C_nil()) }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
};
|
2011-08-17 13:58:49 -07:00
|
|
|
|
2011-08-17 17:49:54 -07:00
|
|
|
build_return(rs.bcx);
|
2010-12-10 15:02:23 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_res_drop(cx: @block_ctxt, rs: ValueRef, did: &ast::def_id,
|
2011-08-04 16:20:09 -07:00
|
|
|
inner_t: ty::t, tps: &[ty::t]) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let ccx = bcx_ccx(cx);
|
|
|
|
let inner_t_s = ty::substitute_type_params(ccx.tcx, tps, inner_t);
|
2011-08-19 15:16:48 -07:00
|
|
|
let tup_ty = ty::mk_tup(ccx.tcx, [ty::mk_int(ccx.tcx), inner_t_s]);
|
2011-08-26 21:34:56 -07:00
|
|
|
let drop_cx = new_sub_block_ctxt(cx, ~"drop res");
|
|
|
|
let next_cx = new_sub_block_ctxt(cx, ~"next");
|
2011-06-28 16:14:01 +02:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
let drop_flag = GEP_tup_like(cx, tup_ty, rs, [0, 0]);
|
2011-06-28 16:14:01 +02:00
|
|
|
cx = drop_flag.bcx;
|
2011-08-30 09:59:30 +02:00
|
|
|
let null_test = IsNull(cx, Load(cx, drop_flag.val));
|
|
|
|
CondBr(cx, null_test, next_cx.llbb, drop_cx.llbb);
|
2011-06-28 16:14:01 +02:00
|
|
|
cx = drop_cx;
|
2011-06-30 12:35:19 +02:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
let val = GEP_tup_like(cx, tup_ty, rs, [0, 1]);
|
2011-06-30 14:46:17 +02:00
|
|
|
cx = val.bcx;
|
2011-06-30 12:35:19 +02:00
|
|
|
// Find and call the actual destructor.
|
2011-08-04 10:46:10 -07:00
|
|
|
let dtor_pair = trans_common::get_res_dtor(ccx, cx.sp, did, inner_t);
|
2011-07-27 14:19:39 +02:00
|
|
|
let dtor_addr =
|
2011-08-30 09:59:30 +02:00
|
|
|
Load(cx, GEP(cx, dtor_pair,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::fn_field_code)]));
|
2011-07-27 14:19:39 +02:00
|
|
|
let dtor_env =
|
2011-08-30 09:59:30 +02:00
|
|
|
Load(cx, GEP(cx, dtor_pair,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::fn_field_box)]));
|
|
|
|
let args = [cx.fcx.llretptr, cx.fcx.lltaskptr, dtor_env];
|
|
|
|
for tp: ty::t in tps {
|
2011-08-12 07:15:18 -07:00
|
|
|
let ti: option::t<@tydesc_info> = none;
|
2011-08-24 18:36:51 -07:00
|
|
|
let td = get_tydesc(cx, tp, false, tps_normal, ti).result;
|
2011-08-19 15:16:48 -07:00
|
|
|
args += [td.val];
|
2011-06-30 14:46:17 +02:00
|
|
|
cx = td.bcx;
|
|
|
|
}
|
|
|
|
// Kludge to work around the fact that we know the precise type of the
|
|
|
|
// value here, but the dtor expects a type that still has opaque pointers
|
|
|
|
// for type variables.
|
2011-07-27 14:19:39 +02:00
|
|
|
let val_llty =
|
2011-08-19 15:16:48 -07:00
|
|
|
lib::llvm::fn_ty_param_tys(
|
|
|
|
llvm::LLVMGetElementType(
|
|
|
|
llvm::LLVMTypeOf(dtor_addr)))[std::vec::len(args)];
|
2011-08-30 09:59:30 +02:00
|
|
|
let val_cast = BitCast(cx, val.val, val_llty);
|
|
|
|
FastCall(cx, dtor_addr, args + [val_cast]);
|
2011-06-30 14:46:17 +02:00
|
|
|
|
2011-08-26 11:20:10 +02:00
|
|
|
cx = drop_ty(cx, val.val, inner_t_s).bcx;
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(cx, C_int(0), drop_flag.val);
|
|
|
|
Br(cx, next_cx.llbb);
|
2011-06-28 16:14:01 +02:00
|
|
|
ret rslt(next_cx, C_nil());
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn decr_refcnt_maybe_free(cx: &@block_ctxt, box_ptr_alias: ValueRef,
|
2011-08-22 12:45:18 +02:00
|
|
|
full_alias: ValueRef, t: ty::t) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let ccx = bcx_ccx(cx);
|
2011-08-26 21:34:56 -07:00
|
|
|
let load_rc_cx = new_sub_block_ctxt(cx, ~"load rc");
|
|
|
|
let rc_adj_cx = new_sub_block_ctxt(cx, ~"rc--");
|
|
|
|
let free_cx = new_sub_block_ctxt(cx, ~"free");
|
|
|
|
let next_cx = new_sub_block_ctxt(cx, ~"next");
|
2011-08-30 09:59:30 +02:00
|
|
|
let box_ptr = Load(cx, box_ptr_alias);
|
2011-07-27 14:19:39 +02:00
|
|
|
let llbox_ty = T_opaque_obj_ptr(*ccx);
|
2011-08-30 09:59:30 +02:00
|
|
|
box_ptr = PointerCast(cx, box_ptr, llbox_ty);
|
|
|
|
let null_test = IsNull(cx, box_ptr);
|
|
|
|
CondBr(cx, null_test, next_cx.llbb, load_rc_cx.llbb);
|
2011-07-27 14:19:39 +02:00
|
|
|
let rc_ptr =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(load_rc_cx, box_ptr,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::box_rc_field_refcnt)]);
|
2011-08-30 09:59:30 +02:00
|
|
|
let rc = Load(load_rc_cx, rc_ptr);
|
2011-07-27 14:19:39 +02:00
|
|
|
let const_test =
|
2011-08-30 09:59:30 +02:00
|
|
|
ICmp(load_rc_cx, lib::llvm::LLVMIntEQ,
|
2011-05-12 17:24:54 +02:00
|
|
|
C_int(abi::const_refcount as int), rc);
|
2011-08-30 09:59:30 +02:00
|
|
|
CondBr(load_rc_cx, const_test, next_cx.llbb, rc_adj_cx.llbb);
|
|
|
|
rc = Sub(rc_adj_cx, rc, C_int(1));
|
|
|
|
Store(rc_adj_cx, rc, rc_ptr);
|
|
|
|
let zero_test = ICmp(rc_adj_cx, lib::llvm::LLVMIntEQ, C_int(0), rc);
|
|
|
|
CondBr(rc_adj_cx, zero_test, free_cx.llbb, next_cx.llbb);
|
2011-07-27 14:19:39 +02:00
|
|
|
let free_res =
|
2011-08-26 11:20:10 +02:00
|
|
|
free_ty(free_cx, full_alias, t);
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(free_res.bcx, next_cx.llbb);
|
2011-07-27 14:19:39 +02:00
|
|
|
let t_else = T_nil();
|
|
|
|
let v_else = C_nil();
|
|
|
|
let phi =
|
2011-08-30 09:59:30 +02:00
|
|
|
Phi(next_cx, t_else, [v_else, v_else, v_else, free_res.val],
|
2011-08-19 15:16:48 -07:00
|
|
|
[cx.llbb, load_rc_cx.llbb, rc_adj_cx.llbb,
|
|
|
|
free_res.bcx.llbb]);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(next_cx, phi);
|
2010-10-01 18:25:42 -07:00
|
|
|
}
|
|
|
|
|
2011-04-19 15:22:57 -07:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
// Structural comparison: a rather involved form of glue.
|
2011-08-26 21:34:56 -07:00
|
|
|
fn maybe_name_value(cx: &@crate_ctxt, v: ValueRef, s: &istr) {
|
2011-07-27 14:19:39 +02:00
|
|
|
if cx.sess.get_opts().save_temps {
|
2011-08-26 21:34:56 -07:00
|
|
|
let _: () = istr::as_buf(s, { |buf|
|
2011-08-26 15:36:18 -07:00
|
|
|
llvm::LLVMSetValueName(v, buf)
|
|
|
|
});
|
2011-05-10 16:43:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-08-02 12:26:52 -07:00
|
|
|
// Used only for creating scalar comparison glue.
|
2011-07-11 16:37:21 -05:00
|
|
|
tag scalar_type { nil_type; signed_int; unsigned_int; floating_point; }
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-02 16:23:52 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn compare_scalar_types(cx: @block_ctxt, lhs: ValueRef, rhs: ValueRef,
|
2011-08-22 12:45:18 +02:00
|
|
|
t: ty::t, llop: ValueRef) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let f = bind compare_scalar_values(cx, lhs, rhs, _, llop);
|
|
|
|
|
|
|
|
alt ty::struct(bcx_tcx(cx), t) {
|
|
|
|
ty::ty_nil. { ret f(nil_type); }
|
2011-08-19 15:16:48 -07:00
|
|
|
ty::ty_bool. | ty::ty_uint. | ty::ty_ptr(_) | ty::ty_char. {
|
|
|
|
ret f(unsigned_int);
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ty::ty_int. { ret f(signed_int); }
|
|
|
|
ty::ty_float. { ret f(floating_point); }
|
|
|
|
ty::ty_machine(_) {
|
|
|
|
if ty::type_is_fp(bcx_tcx(cx), t) {
|
|
|
|
// Floating point machine types
|
|
|
|
ret f(floating_point);
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if ty::type_is_signed(bcx_tcx(cx), t) {
|
2011-07-27 14:19:39 +02:00
|
|
|
// Signed, integral machine types
|
|
|
|
ret f(signed_int);
|
|
|
|
} else {
|
|
|
|
// Unsigned, integral machine types
|
2011-06-28 11:23:53 -07:00
|
|
|
ret f(unsigned_int);
|
2011-06-16 17:05:59 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_type. {
|
2011-08-26 21:34:56 -07:00
|
|
|
trans_fail(cx, none, ~"attempt to compare values of type type");
|
2011-07-27 14:19:39 +02:00
|
|
|
|
|
|
|
// This is a bit lame, because we return a dummy block to the
|
|
|
|
// caller that's actually unreachable, but I don't think it
|
|
|
|
// matters.
|
2011-08-26 21:34:56 -07:00
|
|
|
ret rslt(new_sub_block_ctxt(cx, ~"after_fail_dummy"), C_bool(false));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_native(_) {
|
2011-08-13 00:09:25 -07:00
|
|
|
trans_fail(cx, none::<span>,
|
2011-08-26 21:34:56 -07:00
|
|
|
~"attempt to compare values of type native");
|
|
|
|
ret rslt(new_sub_block_ctxt(cx, ~"after_fail_dummy"), C_bool(false));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ {
|
|
|
|
// Should never get here, because t is scalar.
|
2011-08-27 16:36:48 -07:00
|
|
|
bcx_ccx(cx).sess.bug(~"non-scalar type passed to \
|
2011-07-22 23:56:02 -07:00
|
|
|
compare_scalar_types");
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-04-19 15:22:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-11 16:37:21 -05:00
|
|
|
// A helper function to do the actual comparison of scalar values.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn compare_scalar_values(cx: &@block_ctxt, lhs: ValueRef, rhs: ValueRef,
|
|
|
|
nt: scalar_type, llop: ValueRef) -> result {
|
|
|
|
let eq_cmp;
|
|
|
|
let lt_cmp;
|
|
|
|
let le_cmp;
|
|
|
|
alt nt {
|
|
|
|
nil_type. {
|
|
|
|
// We don't need to do actual comparisons for nil.
|
|
|
|
// () == () holds but () < () does not.
|
|
|
|
eq_cmp = 1u;
|
|
|
|
lt_cmp = 0u;
|
|
|
|
le_cmp = 1u;
|
|
|
|
}
|
|
|
|
floating_point. {
|
|
|
|
eq_cmp = lib::llvm::LLVMRealUEQ;
|
|
|
|
lt_cmp = lib::llvm::LLVMRealULT;
|
|
|
|
le_cmp = lib::llvm::LLVMRealULE;
|
|
|
|
}
|
|
|
|
signed_int. {
|
|
|
|
eq_cmp = lib::llvm::LLVMIntEQ;
|
|
|
|
lt_cmp = lib::llvm::LLVMIntSLT;
|
|
|
|
le_cmp = lib::llvm::LLVMIntSLE;
|
|
|
|
}
|
|
|
|
unsigned_int. {
|
|
|
|
eq_cmp = lib::llvm::LLVMIntEQ;
|
|
|
|
lt_cmp = lib::llvm::LLVMIntULT;
|
|
|
|
le_cmp = lib::llvm::LLVMIntULE;
|
|
|
|
}
|
2011-06-02 16:23:52 -07:00
|
|
|
}
|
|
|
|
// FIXME: This wouldn't be necessary if we could bind methods off of
|
|
|
|
// objects and therefore abstract over FCmp and ICmp (issue #435). Then
|
2011-08-30 09:59:30 +02:00
|
|
|
// we could just write, e.g., "cmp_fn = bind FCmp(cx, _, _, _);" in
|
2011-06-02 16:23:52 -07:00
|
|
|
// the above, and "auto eq_result = cmp_fn(eq_cmp, lhs, rhs);" in the
|
|
|
|
// below.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn generic_cmp(cx: &@block_ctxt, nt: scalar_type, op: uint, lhs: ValueRef,
|
|
|
|
rhs: ValueRef) -> ValueRef {
|
|
|
|
let r: ValueRef;
|
|
|
|
if nt == nil_type {
|
2011-07-11 16:37:21 -05:00
|
|
|
r = C_bool(op != 0u);
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if nt == floating_point {
|
2011-08-30 09:59:30 +02:00
|
|
|
r = FCmp(cx, op, lhs, rhs);
|
|
|
|
} else { r = ICmp(cx, op, lhs, rhs); }
|
2011-06-02 16:23:52 -07:00
|
|
|
ret r;
|
2011-04-19 15:22:57 -07:00
|
|
|
}
|
2011-08-26 21:34:56 -07:00
|
|
|
let last_cx = new_sub_block_ctxt(cx, ~"last");
|
|
|
|
let eq_cx = new_sub_block_ctxt(cx, ~"eq");
|
2011-07-27 14:19:39 +02:00
|
|
|
let eq_result = generic_cmp(eq_cx, nt, eq_cmp, lhs, rhs);
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(eq_cx, last_cx.llbb);
|
2011-08-26 21:34:56 -07:00
|
|
|
let lt_cx = new_sub_block_ctxt(cx, ~"lt");
|
2011-07-27 14:19:39 +02:00
|
|
|
let lt_result = generic_cmp(lt_cx, nt, lt_cmp, lhs, rhs);
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(lt_cx, last_cx.llbb);
|
2011-08-26 21:34:56 -07:00
|
|
|
let le_cx = new_sub_block_ctxt(cx, ~"le");
|
2011-07-27 14:19:39 +02:00
|
|
|
let le_result = generic_cmp(le_cx, nt, le_cmp, lhs, rhs);
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(le_cx, last_cx.llbb);
|
2011-08-26 21:34:56 -07:00
|
|
|
let unreach_cx = new_sub_block_ctxt(cx, ~"unreach");
|
2011-08-30 09:59:30 +02:00
|
|
|
Unreachable(unreach_cx);
|
|
|
|
let llswitch = Switch(cx, llop, unreach_cx.llbb, 3u);
|
2011-05-12 17:24:54 +02:00
|
|
|
llvm::LLVMAddCase(llswitch, C_u8(abi::cmp_glue_op_eq), eq_cx.llbb);
|
|
|
|
llvm::LLVMAddCase(llswitch, C_u8(abi::cmp_glue_op_lt), lt_cx.llbb);
|
|
|
|
llvm::LLVMAddCase(llswitch, C_u8(abi::cmp_glue_op_le), le_cx.llbb);
|
2011-07-27 14:19:39 +02:00
|
|
|
let last_result =
|
2011-08-30 09:59:30 +02:00
|
|
|
Phi(last_cx, T_i1(), [eq_result, lt_result, le_result],
|
2011-08-19 15:16:48 -07:00
|
|
|
[eq_cx.llbb, lt_cx.llbb, le_cx.llbb]);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(last_cx, last_result);
|
2011-04-19 15:22:57 -07:00
|
|
|
}
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
type val_pair_fn = fn(&@block_ctxt, ValueRef, ValueRef) -> result;
|
|
|
|
type val_fn = fn(&@block_ctxt, ValueRef) -> result;
|
|
|
|
type val_and_ty_fn = fn(&@block_ctxt, ValueRef, ty::t) -> result;
|
2010-11-24 16:55:45 -08:00
|
|
|
|
2011-02-28 17:49:26 -08:00
|
|
|
|
2010-12-20 16:23:49 -08:00
|
|
|
// Iterates through the elements of a structural type.
|
2011-08-22 12:45:18 +02:00
|
|
|
fn iter_structural_ty(cx: &@block_ctxt, v: ValueRef, t: ty::t,
|
2011-07-27 14:19:39 +02:00
|
|
|
f: val_and_ty_fn) -> result {
|
2011-08-19 15:16:48 -07:00
|
|
|
fn adaptor_fn(f: val_and_ty_fn, cx: &@block_ctxt, av: ValueRef, t: ty::t)
|
|
|
|
-> result {
|
2011-02-28 17:49:26 -08:00
|
|
|
ret f(cx, av, t);
|
|
|
|
}
|
2011-08-25 10:18:02 +02:00
|
|
|
let x = iter_structural_ty_full(cx, v, t, bind adaptor_fn(f, _, _, _));
|
|
|
|
ret x;
|
2011-02-28 17:49:26 -08:00
|
|
|
}
|
|
|
|
|
2011-08-04 16:20:09 -07:00
|
|
|
fn load_inbounds(cx: &@block_ctxt, p: ValueRef, idxs: &[ValueRef]) ->
|
2011-07-27 14:19:39 +02:00
|
|
|
ValueRef {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret Load(cx, InBoundsGEP(cx, p, idxs));
|
2011-07-05 14:19:19 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn store_inbounds(cx: &@block_ctxt, v: ValueRef, p: ValueRef,
|
2011-08-04 16:20:09 -07:00
|
|
|
idxs: &[ValueRef]) {
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(cx, v, InBoundsGEP(cx, p, idxs));
|
2011-07-05 14:19:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// This uses store and inboundsGEP, but it only doing so superficially; it's
|
|
|
|
// really storing an incremented pointer to another pointer.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn incr_ptr(cx: &@block_ctxt, p: ValueRef, incr: ValueRef, pp: ValueRef) {
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(cx, InBoundsGEP(cx, p, [incr]), pp);
|
2011-07-05 14:19:19 -07:00
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn iter_structural_ty_full(cx: &@block_ctxt, av: ValueRef, t: ty::t,
|
2011-08-09 18:28:46 -07:00
|
|
|
f: &val_and_ty_fn) -> result {
|
2011-08-19 15:16:48 -07:00
|
|
|
fn iter_boxpp(cx: @block_ctxt, box_cell: ValueRef, f: &val_and_ty_fn) ->
|
|
|
|
result {
|
2011-08-30 09:59:30 +02:00
|
|
|
let box_ptr = Load(cx, box_cell);
|
2011-07-27 14:19:39 +02:00
|
|
|
let tnil = ty::mk_nil(bcx_tcx(cx));
|
|
|
|
let tbox = ty::mk_imm_box(bcx_tcx(cx), tnil);
|
2011-08-26 21:34:56 -07:00
|
|
|
let inner_cx = new_sub_block_ctxt(cx, ~"iter box");
|
|
|
|
let next_cx = new_sub_block_ctxt(cx, ~"next");
|
2011-08-30 09:59:30 +02:00
|
|
|
let null_test = IsNull(cx, box_ptr);
|
|
|
|
CondBr(cx, null_test, next_cx.llbb, inner_cx.llbb);
|
2011-08-26 11:20:10 +02:00
|
|
|
let r = f(inner_cx, box_cell, tbox);
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(r.bcx, next_cx.llbb);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(next_cx, C_nil());
|
2010-12-20 17:28:07 -08:00
|
|
|
}
|
2011-07-01 12:36:49 +02:00
|
|
|
|
2011-08-09 18:28:46 -07:00
|
|
|
fn iter_variant(cx: @block_ctxt, a_tup: ValueRef,
|
2011-08-04 16:20:09 -07:00
|
|
|
variant: &ty::variant_info, tps: &[ty::t],
|
2011-08-09 18:28:46 -07:00
|
|
|
tid: &ast::def_id, f: &val_and_ty_fn) -> result {
|
2011-08-13 00:09:25 -07:00
|
|
|
if std::vec::len::<ty::t>(variant.args) == 0u {
|
2011-07-01 12:36:49 +02:00
|
|
|
ret rslt(cx, C_nil());
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let fn_ty = variant.ctor_ty;
|
|
|
|
let ccx = bcx_ccx(cx);
|
|
|
|
alt ty::struct(ccx.tcx, fn_ty) {
|
|
|
|
ty::ty_fn(_, args, _, _, _) {
|
|
|
|
let j = 0;
|
2011-08-15 21:54:52 -07:00
|
|
|
for a: ty::arg in args {
|
2011-07-27 14:19:39 +02:00
|
|
|
let rslt = GEP_tag(cx, a_tup, tid, variant.id, tps, j);
|
|
|
|
let llfldp_a = rslt.val;
|
|
|
|
cx = rslt.bcx;
|
|
|
|
let ty_subst = ty::substitute_type_params(ccx.tcx, tps, a.ty);
|
2011-08-26 11:20:10 +02:00
|
|
|
rslt = f(cx, llfldp_a, ty_subst);
|
2011-07-27 14:19:39 +02:00
|
|
|
cx = rslt.bcx;
|
|
|
|
j += 1;
|
2011-07-01 12:36:49 +02:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-07-01 12:36:49 +02:00
|
|
|
}
|
|
|
|
ret rslt(cx, C_nil());
|
|
|
|
}
|
2011-07-13 15:44:09 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let r: result = rslt(cx, C_nil());
|
|
|
|
alt ty::struct(bcx_tcx(cx), t) {
|
|
|
|
ty::ty_rec(fields) {
|
|
|
|
let i: int = 0;
|
2011-08-15 11:40:26 +02:00
|
|
|
for fld: ty::field in fields {
|
2011-08-19 15:16:48 -07:00
|
|
|
r = GEP_tup_like(r.bcx, t, av, [0, i]);
|
2011-07-27 14:19:39 +02:00
|
|
|
let llfld_a = r.val;
|
2011-08-26 11:20:10 +02:00
|
|
|
r = f(r.bcx, llfld_a, fld.mt.ty);
|
2011-07-27 14:19:39 +02:00
|
|
|
i += 1;
|
2011-06-10 19:35:59 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-15 11:40:26 +02:00
|
|
|
ty::ty_tup(args) {
|
|
|
|
let i = 0;
|
|
|
|
for arg in args {
|
2011-08-19 15:16:48 -07:00
|
|
|
r = GEP_tup_like(r.bcx, t, av, [0, i]);
|
2011-08-15 11:40:26 +02:00
|
|
|
let llfld_a = r.val;
|
2011-08-26 11:20:10 +02:00
|
|
|
r = f(r.bcx, llfld_a, arg);
|
2011-08-15 11:40:26 +02:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ty::ty_res(_, inner, tps) {
|
2011-07-29 11:44:29 +02:00
|
|
|
let tcx = bcx_tcx(cx);
|
|
|
|
let inner1 = ty::substitute_type_params(tcx, tps, inner);
|
|
|
|
let inner_t_s = ty::substitute_type_params(tcx, tps, inner);
|
2011-08-19 15:16:48 -07:00
|
|
|
let tup_t = ty::mk_tup(tcx, [ty::mk_int(tcx), inner_t_s]);
|
|
|
|
r = GEP_tup_like(r.bcx, tup_t, av, [0, 1]);
|
2011-07-27 14:19:39 +02:00
|
|
|
let llfld_a = r.val;
|
2011-08-26 11:20:10 +02:00
|
|
|
r = f(r.bcx, llfld_a, inner1);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_tag(tid, tps) {
|
|
|
|
let variants = ty::tag_variants(bcx_tcx(cx), tid);
|
2011-08-15 16:38:23 -07:00
|
|
|
let n_variants = std::vec::len(variants);
|
2011-07-27 14:19:39 +02:00
|
|
|
|
|
|
|
// Cast the tags to types we can GEP into.
|
|
|
|
if n_variants == 1u {
|
2011-08-19 15:16:48 -07:00
|
|
|
ret iter_variant(cx, av, variants[0], tps, tid, f);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let lltagty = T_opaque_tag_ptr(bcx_ccx(cx).tn);
|
2011-08-30 09:59:30 +02:00
|
|
|
let av_tag = PointerCast(cx, av, lltagty);
|
|
|
|
let lldiscrim_a_ptr = GEP(cx, av_tag, [C_int(0), C_int(0)]);
|
|
|
|
let llunion_a_ptr = GEP(cx, av_tag, [C_int(0), C_int(1)]);
|
|
|
|
let lldiscrim_a = Load(cx, lldiscrim_a_ptr);
|
2011-07-27 14:19:39 +02:00
|
|
|
|
|
|
|
// NB: we must hit the discriminant first so that structural
|
|
|
|
// comparison know not to proceed when the discriminants differ.
|
|
|
|
let bcx = cx;
|
2011-08-26 11:20:10 +02:00
|
|
|
bcx = f(bcx, lldiscrim_a_ptr, ty::mk_int(bcx_tcx(cx))).bcx;
|
2011-08-26 21:34:56 -07:00
|
|
|
let unr_cx = new_sub_block_ctxt(bcx, ~"tag-iter-unr");
|
2011-08-30 09:59:30 +02:00
|
|
|
Unreachable(unr_cx);
|
|
|
|
let llswitch = Switch(bcx, lldiscrim_a, unr_cx.llbb, n_variants);
|
2011-08-26 21:34:56 -07:00
|
|
|
let next_cx = new_sub_block_ctxt(bcx, ~"tag-iter-next");
|
2011-07-27 14:19:39 +02:00
|
|
|
let i = 0u;
|
2011-08-15 21:54:52 -07:00
|
|
|
for variant: ty::variant_info in variants {
|
2011-07-27 14:19:39 +02:00
|
|
|
let variant_cx =
|
|
|
|
new_sub_block_ctxt(bcx,
|
2011-08-26 21:34:56 -07:00
|
|
|
~"tag-iter-variant-" +
|
|
|
|
uint::to_str(i, 10u));
|
2011-07-27 14:19:39 +02:00
|
|
|
llvm::LLVMAddCase(llswitch, C_int(i as int), variant_cx.llbb);
|
2011-08-19 15:16:48 -07:00
|
|
|
variant_cx =
|
|
|
|
iter_variant(variant_cx, llunion_a_ptr, variant, tps, tid,
|
|
|
|
f).bcx;
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(variant_cx, next_cx.llbb);
|
2011-07-27 14:19:39 +02:00
|
|
|
i += 1u;
|
2010-12-01 17:08:46 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ret rslt(next_cx, C_nil());
|
|
|
|
}
|
|
|
|
ty::ty_fn(_, _, _, _, _) {
|
|
|
|
let box_cell_a =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(cx, av, [C_int(0), C_int(abi::fn_field_box)]);
|
2011-08-09 18:28:46 -07:00
|
|
|
ret iter_boxpp(cx, box_cell_a, f);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_obj(_) {
|
|
|
|
let box_cell_a =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(cx, av, [C_int(0), C_int(abi::obj_field_box)]);
|
2011-08-09 18:28:46 -07:00
|
|
|
ret iter_boxpp(cx, box_cell_a, f);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-27 16:36:48 -07:00
|
|
|
_ { bcx_ccx(cx).sess.unimpl(~"type in iter_structural_ty_full"); }
|
2010-11-09 17:49:20 -08:00
|
|
|
}
|
2010-11-24 16:55:45 -08:00
|
|
|
ret r;
|
2010-11-09 17:49:20 -08:00
|
|
|
}
|
|
|
|
|
2011-03-03 17:27:35 -08:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
// Iterates through a pointer range, until the src* hits the src_lim*.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn iter_sequence_raw(cx: @block_ctxt, dst: ValueRef,
|
2011-08-19 15:16:48 -07:00
|
|
|
src:
|
|
|
|
// elt*
|
|
|
|
ValueRef,
|
|
|
|
src_lim:
|
|
|
|
// elt*
|
|
|
|
ValueRef,
|
|
|
|
elt_sz:
|
|
|
|
// elt*
|
|
|
|
ValueRef, f: &val_pair_fn) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx = cx;
|
|
|
|
let dst_int: ValueRef = vp2i(bcx, dst);
|
|
|
|
let src_int: ValueRef = vp2i(bcx, src);
|
|
|
|
let src_lim_int: ValueRef = vp2i(bcx, src_lim);
|
2011-08-26 21:34:56 -07:00
|
|
|
let cond_cx = new_scope_block_ctxt(cx, ~"sequence-iter cond");
|
|
|
|
let body_cx = new_scope_block_ctxt(cx, ~"sequence-iter body");
|
|
|
|
let next_cx = new_sub_block_ctxt(cx, ~"next");
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(bcx, cond_cx.llbb);
|
2011-07-27 14:19:39 +02:00
|
|
|
let dst_curr: ValueRef =
|
2011-08-30 09:59:30 +02:00
|
|
|
Phi(cond_cx, T_int(), [dst_int], [bcx.llbb]);
|
2011-07-27 14:19:39 +02:00
|
|
|
let src_curr: ValueRef =
|
2011-08-30 09:59:30 +02:00
|
|
|
Phi(cond_cx, T_int(), [src_int], [bcx.llbb]);
|
2011-07-27 14:19:39 +02:00
|
|
|
let end_test =
|
2011-08-30 09:59:30 +02:00
|
|
|
ICmp(cond_cx, lib::llvm::LLVMIntULT, src_curr, src_lim_int);
|
|
|
|
CondBr(cond_cx, end_test, body_cx.llbb, next_cx.llbb);
|
2011-07-27 14:19:39 +02:00
|
|
|
let dst_curr_ptr = vi2p(body_cx, dst_curr, T_ptr(T_i8()));
|
|
|
|
let src_curr_ptr = vi2p(body_cx, src_curr, T_ptr(T_i8()));
|
|
|
|
let body_res = f(body_cx, dst_curr_ptr, src_curr_ptr);
|
2011-03-03 17:27:35 -08:00
|
|
|
body_cx = body_res.bcx;
|
2011-08-30 09:59:30 +02:00
|
|
|
let dst_next = Add(body_cx, dst_curr, elt_sz);
|
|
|
|
let src_next = Add(body_cx, src_curr, elt_sz);
|
|
|
|
Br(body_cx, cond_cx.llbb);
|
|
|
|
AddIncomingToPhi(dst_curr, [dst_next], [body_cx.llbb]);
|
|
|
|
AddIncomingToPhi(src_curr, [src_next], [body_cx.llbb]);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(next_cx, C_nil());
|
2011-03-03 17:27:35 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn iter_sequence_inner(cx: &@block_ctxt, src: ValueRef,
|
|
|
|
src_lim:
|
2011-06-16 16:55:46 -07:00
|
|
|
// elt*
|
2011-07-27 14:19:39 +02:00
|
|
|
ValueRef,
|
|
|
|
elt_ty: & // elt*
|
|
|
|
ty::t, f: &val_and_ty_fn) -> result {
|
|
|
|
fn adaptor_fn(f: val_and_ty_fn, elt_ty: ty::t, cx: &@block_ctxt,
|
2011-08-18 10:01:39 +02:00
|
|
|
_dst: ValueRef, src: ValueRef) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let llptrty;
|
|
|
|
if !ty::type_has_dynamic_size(bcx_tcx(cx), elt_ty) {
|
|
|
|
let llty = type_of(bcx_ccx(cx), cx.sp, elt_ty);
|
2011-03-18 16:04:16 -07:00
|
|
|
llptrty = T_ptr(llty);
|
2011-06-15 11:19:50 -07:00
|
|
|
} else { llptrty = T_ptr(T_ptr(T_i8())); }
|
2011-08-30 09:59:30 +02:00
|
|
|
let p = PointerCast(cx, src, llptrty);
|
2011-04-12 12:06:20 -07:00
|
|
|
ret f(cx, load_if_immediate(cx, p, elt_ty), elt_ty);
|
2011-03-03 17:27:35 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let elt_sz = size_of(cx, elt_ty);
|
2011-06-15 21:38:43 +02:00
|
|
|
ret iter_sequence_raw(elt_sz.bcx, src, src, src_lim, elt_sz.val,
|
|
|
|
bind adaptor_fn(f, elt_ty, _, _, _));
|
2011-03-03 17:27:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-24 16:55:45 -08:00
|
|
|
// Iterates through the elements of a vec or str.
|
2011-08-22 12:45:18 +02:00
|
|
|
fn iter_sequence(cx: @block_ctxt, v: ValueRef, t: ty::t, f: &val_and_ty_fn)
|
2011-07-27 14:19:39 +02:00
|
|
|
-> result {
|
2011-08-25 10:18:02 +02:00
|
|
|
fn iter_sequence_body(bcx: @block_ctxt, v: ValueRef, elt_ty: ty::t,
|
2011-07-27 14:19:39 +02:00
|
|
|
f: &val_and_ty_fn, trailing_null: bool,
|
|
|
|
interior: bool) -> result {
|
|
|
|
let p0;
|
|
|
|
let len;
|
2011-08-25 10:18:02 +02:00
|
|
|
let llunit_ty = type_of_or_i8(bcx, elt_ty);
|
2011-07-27 14:19:39 +02:00
|
|
|
if !interior {
|
2011-08-30 09:59:30 +02:00
|
|
|
p0 = GEP(bcx, v, [C_int(0), C_int(abi::vec_elt_data)]);
|
|
|
|
let lp = GEP(bcx, v, [C_int(0), C_int(abi::vec_elt_fill)]);
|
|
|
|
len = Load(bcx, lp);
|
2011-06-20 16:52:43 -07:00
|
|
|
} else {
|
2011-08-25 10:18:02 +02:00
|
|
|
len = ivec::get_fill(bcx, v);
|
|
|
|
p0 = ivec::get_dataptr(bcx, v, llunit_ty);
|
2011-06-20 16:52:43 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
if trailing_null {
|
|
|
|
let unit_sz = size_of(bcx, elt_ty);
|
2011-03-03 17:27:35 -08:00
|
|
|
bcx = unit_sz.bcx;
|
2011-08-30 09:59:30 +02:00
|
|
|
len = Sub(bcx, len, unit_sz.val);
|
2010-11-24 16:55:45 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let p1 =
|
2011-08-30 09:59:30 +02:00
|
|
|
vi2p(bcx, Add(bcx, vp2i(bcx, p0), len), T_ptr(llunit_ty));
|
2011-04-26 20:39:25 +00:00
|
|
|
ret iter_sequence_inner(bcx, p0, p1, elt_ty, f);
|
2010-11-24 16:55:45 -08:00
|
|
|
}
|
2011-06-20 16:52:43 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
|
|
|
|
alt ty::struct(bcx_tcx(cx), t) {
|
|
|
|
ty::ty_str. {
|
|
|
|
let et = ty::mk_mach(bcx_tcx(cx), ast::ty_u8);
|
|
|
|
ret iter_sequence_body(cx, v, et, f, true, false);
|
|
|
|
}
|
2011-08-18 14:11:06 -07:00
|
|
|
ty::ty_vec(elt) {
|
2011-07-27 14:19:39 +02:00
|
|
|
ret iter_sequence_body(cx, v, elt.ty, f, false, true);
|
|
|
|
}
|
|
|
|
ty::ty_istr. {
|
|
|
|
let et = ty::mk_mach(bcx_tcx(cx), ast::ty_u8);
|
|
|
|
ret iter_sequence_body(cx, v, et, f, true, true);
|
|
|
|
}
|
|
|
|
_ {
|
2011-08-26 23:39:08 -07:00
|
|
|
bcx_ccx(cx).sess.bug(
|
2011-08-27 16:36:48 -07:00
|
|
|
~"unexpected type in trans::iter_sequence: "
|
|
|
|
+ ty_to_str(cx.fcx.lcx.ccx.tcx, t));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2010-11-09 17:49:20 -08:00
|
|
|
}
|
2010-11-24 16:55:45 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn lazily_emit_all_tydesc_glue(cx: &@block_ctxt,
|
2011-08-12 07:15:18 -07:00
|
|
|
static_ti: &option::t<@tydesc_info>) {
|
2011-08-19 10:24:13 +02:00
|
|
|
lazily_emit_tydesc_glue(cx, abi::tydesc_field_take_glue, static_ti);
|
2011-05-12 15:42:12 -07:00
|
|
|
lazily_emit_tydesc_glue(cx, abi::tydesc_field_drop_glue, static_ti);
|
2011-05-18 17:28:08 -07:00
|
|
|
lazily_emit_tydesc_glue(cx, abi::tydesc_field_free_glue, static_ti);
|
2011-05-12 15:42:12 -07:00
|
|
|
lazily_emit_tydesc_glue(cx, abi::tydesc_field_cmp_glue, static_ti);
|
2011-08-24 20:30:20 +02:00
|
|
|
lazily_emit_tydesc_glue(cx, abi::tydesc_field_copy_glue, static_ti);
|
2011-05-12 15:42:12 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn lazily_emit_all_generic_info_tydesc_glues(cx: &@block_ctxt,
|
|
|
|
gi: &generic_info) {
|
2011-08-12 07:15:18 -07:00
|
|
|
for ti: option::t<@tydesc_info> in gi.static_tis {
|
2011-05-12 15:42:12 -07:00
|
|
|
lazily_emit_all_tydesc_glue(cx, ti);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn lazily_emit_tydesc_glue(cx: &@block_ctxt, field: int,
|
2011-08-12 07:15:18 -07:00
|
|
|
static_ti: &option::t<@tydesc_info>) {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt static_ti {
|
|
|
|
none. { }
|
|
|
|
some(ti) {
|
2011-08-19 10:24:13 +02:00
|
|
|
if field == abi::tydesc_field_take_glue {
|
|
|
|
alt { ti.take_glue } {
|
2011-07-27 14:19:39 +02:00
|
|
|
some(_) { }
|
|
|
|
none. {
|
2011-08-19 15:16:48 -07:00
|
|
|
log #fmt["+++ lazily_emit_tydesc_glue TAKE %s",
|
2011-08-26 23:39:08 -07:00
|
|
|
istr::to_estr(ty_to_str(bcx_tcx(cx), ti.ty))];
|
2011-07-27 14:19:39 +02:00
|
|
|
let lcx = cx.fcx.lcx;
|
|
|
|
let glue_fn =
|
|
|
|
declare_generic_glue(lcx, ti.ty, T_glue_fn(*lcx.ccx),
|
2011-08-26 21:34:56 -07:00
|
|
|
~"take");
|
2011-08-19 10:24:13 +02:00
|
|
|
ti.take_glue = some::<ValueRef>(glue_fn);
|
2011-08-22 11:41:49 +02:00
|
|
|
make_generic_glue(lcx, cx.sp, ti.ty, glue_fn,
|
|
|
|
default_helper(make_take_glue),
|
2011-08-26 21:34:56 -07:00
|
|
|
ti.ty_params, ~"take");
|
2011-08-19 15:16:48 -07:00
|
|
|
log #fmt["--- lazily_emit_tydesc_glue TAKE %s",
|
2011-08-26 23:39:08 -07:00
|
|
|
istr::to_estr(ty_to_str(bcx_tcx(cx), ti.ty))];
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if field == abi::tydesc_field_drop_glue {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt { ti.drop_glue } {
|
|
|
|
some(_) { }
|
|
|
|
none. {
|
2011-08-19 15:16:48 -07:00
|
|
|
log #fmt["+++ lazily_emit_tydesc_glue DROP %s",
|
2011-08-26 23:39:08 -07:00
|
|
|
istr::to_estr(ty_to_str(bcx_tcx(cx), ti.ty))];
|
2011-07-27 14:19:39 +02:00
|
|
|
let lcx = cx.fcx.lcx;
|
|
|
|
let glue_fn =
|
|
|
|
declare_generic_glue(lcx, ti.ty, T_glue_fn(*lcx.ccx),
|
2011-08-26 21:34:56 -07:00
|
|
|
~"drop");
|
2011-08-13 00:09:25 -07:00
|
|
|
ti.drop_glue = some::<ValueRef>(glue_fn);
|
2011-08-22 11:41:49 +02:00
|
|
|
make_generic_glue(lcx, cx.sp, ti.ty, glue_fn,
|
|
|
|
default_helper(make_drop_glue),
|
2011-08-26 21:34:56 -07:00
|
|
|
ti.ty_params, ~"drop");
|
2011-08-19 15:16:48 -07:00
|
|
|
log #fmt["--- lazily_emit_tydesc_glue DROP %s",
|
2011-08-26 23:39:08 -07:00
|
|
|
istr::to_estr(ty_to_str(bcx_tcx(cx), ti.ty))];
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-22 11:41:49 +02:00
|
|
|
} else if field == abi::tydesc_field_free_glue {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt { ti.free_glue } {
|
|
|
|
some(_) { }
|
|
|
|
none. {
|
2011-08-19 15:16:48 -07:00
|
|
|
log #fmt["+++ lazily_emit_tydesc_glue FREE %s",
|
2011-08-26 23:39:08 -07:00
|
|
|
istr::to_estr(ty_to_str(bcx_tcx(cx), ti.ty))];
|
2011-07-27 14:19:39 +02:00
|
|
|
let lcx = cx.fcx.lcx;
|
|
|
|
let glue_fn =
|
|
|
|
declare_generic_glue(lcx, ti.ty, T_glue_fn(*lcx.ccx),
|
2011-08-26 21:34:56 -07:00
|
|
|
~"free");
|
2011-08-13 00:09:25 -07:00
|
|
|
ti.free_glue = some::<ValueRef>(glue_fn);
|
2011-08-22 11:41:49 +02:00
|
|
|
make_generic_glue(lcx, cx.sp, ti.ty, glue_fn,
|
|
|
|
default_helper(make_free_glue),
|
2011-08-26 21:34:56 -07:00
|
|
|
ti.ty_params, ~"free");
|
2011-08-19 15:16:48 -07:00
|
|
|
log #fmt["--- lazily_emit_tydesc_glue FREE %s",
|
2011-08-26 23:39:08 -07:00
|
|
|
istr::to_estr(ty_to_str(bcx_tcx(cx), ti.ty))];
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if field == abi::tydesc_field_cmp_glue {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt { ti.cmp_glue } {
|
|
|
|
some(_) { }
|
|
|
|
none. {
|
2011-08-19 15:16:48 -07:00
|
|
|
log #fmt["+++ lazily_emit_tydesc_glue CMP %s",
|
2011-08-26 23:39:08 -07:00
|
|
|
istr::to_estr(ty_to_str(bcx_tcx(cx), ti.ty))];
|
2011-08-09 17:01:38 -07:00
|
|
|
ti.cmp_glue = some(bcx_ccx(cx).upcalls.cmp_type);
|
2011-08-19 15:16:48 -07:00
|
|
|
log #fmt["--- lazily_emit_tydesc_glue CMP %s",
|
2011-08-26 23:39:08 -07:00
|
|
|
istr::to_estr(ty_to_str(bcx_tcx(cx), ti.ty))];
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-05-12 15:42:12 -07:00
|
|
|
}
|
2011-08-24 20:30:20 +02:00
|
|
|
} else if field == abi::tydesc_field_copy_glue {
|
|
|
|
alt { ti.copy_glue } {
|
|
|
|
some(_) {}
|
|
|
|
none. {
|
|
|
|
let lcx = cx.fcx.lcx;
|
|
|
|
let glue_fn =
|
|
|
|
declare_generic_glue(lcx, ti.ty, T_copy_glue_fn(*lcx.ccx),
|
2011-08-26 21:34:56 -07:00
|
|
|
~"copy");
|
2011-08-24 20:30:20 +02:00
|
|
|
ti.copy_glue = some(glue_fn);
|
|
|
|
make_generic_glue(lcx, cx.sp, ti.ty, glue_fn,
|
|
|
|
copy_helper(make_copy_glue),
|
2011-08-26 21:34:56 -07:00
|
|
|
ti.ty_params, ~"copy");
|
2011-08-24 20:30:20 +02:00
|
|
|
}
|
|
|
|
}
|
2011-05-12 15:42:12 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-05-12 15:42:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn call_tydesc_glue_full(cx: &@block_ctxt, v: ValueRef, tydesc: ValueRef,
|
2011-08-12 07:15:18 -07:00
|
|
|
field: int, static_ti: &option::t<@tydesc_info>) {
|
2011-05-12 15:42:12 -07:00
|
|
|
lazily_emit_tydesc_glue(cx, field, static_ti);
|
2011-06-27 15:38:04 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let static_glue_fn = none;
|
|
|
|
alt static_ti {
|
|
|
|
none. {/* no-op */ }
|
|
|
|
some(sti) {
|
2011-08-19 10:24:13 +02:00
|
|
|
if field == abi::tydesc_field_take_glue {
|
|
|
|
static_glue_fn = sti.take_glue;
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if field == abi::tydesc_field_drop_glue {
|
2011-06-27 15:38:04 -07:00
|
|
|
static_glue_fn = sti.drop_glue;
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if field == abi::tydesc_field_free_glue {
|
2011-06-27 15:38:04 -07:00
|
|
|
static_glue_fn = sti.free_glue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-30 09:59:30 +02:00
|
|
|
let llrawptr = PointerCast(cx, v, T_ptr(T_i8()));
|
2011-07-27 14:19:39 +02:00
|
|
|
let lltydescs =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(cx, tydesc,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::tydesc_field_first_param)]);
|
2011-08-30 09:59:30 +02:00
|
|
|
lltydescs = Load(cx, lltydescs);
|
2011-06-27 15:38:04 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let llfn;
|
|
|
|
alt static_glue_fn {
|
|
|
|
none. {
|
2011-08-30 09:59:30 +02:00
|
|
|
let llfnptr = GEP(cx, tydesc, [C_int(0), C_int(field)]);
|
|
|
|
llfn = Load(cx, llfnptr);
|
2011-06-27 15:38:04 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
some(sgf) { llfn = sgf; }
|
2011-06-27 15:38:04 -07:00
|
|
|
}
|
|
|
|
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(cx, llfn,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_null(T_ptr(T_nil())), cx.fcx.lltaskptr,
|
|
|
|
C_null(T_ptr(T_nil())), lltydescs, llrawptr]);
|
2011-01-28 14:34:25 -08:00
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn call_tydesc_glue(cx: &@block_ctxt, v: ValueRef, t: ty::t, field: int) ->
|
2011-06-15 11:19:50 -07:00
|
|
|
result {
|
2011-08-13 00:09:25 -07:00
|
|
|
let ti: option::t<@tydesc_info> = none::<@tydesc_info>;
|
2011-08-24 18:36:51 -07:00
|
|
|
let td = get_tydesc(cx, t, false, tps_normal, ti).result;
|
2011-08-26 11:20:10 +02:00
|
|
|
call_tydesc_glue_full(td.bcx, v, td.val, field, ti);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(td.bcx, C_nil());
|
2011-01-28 14:34:25 -08:00
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn call_cmp_glue(cx: &@block_ctxt, lhs: ValueRef, rhs: ValueRef, t: ty::t,
|
2011-07-27 14:19:39 +02:00
|
|
|
llop: ValueRef) -> result {
|
2011-04-19 15:22:57 -07:00
|
|
|
// We can't use call_tydesc_glue_full() and friends here because compare
|
|
|
|
// glue has a special signature.
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let lllhs = spill_if_immediate(cx, lhs, t);
|
|
|
|
let llrhs = spill_if_immediate(cx, rhs, t);
|
2011-08-30 09:59:30 +02:00
|
|
|
let llrawlhsptr = BitCast(cx, lllhs, T_ptr(T_i8()));
|
|
|
|
let llrawrhsptr = BitCast(cx, llrhs, T_ptr(T_i8()));
|
2011-08-13 00:09:25 -07:00
|
|
|
let ti = none::<@tydesc_info>;
|
2011-08-24 18:36:51 -07:00
|
|
|
let r = get_tydesc(cx, t, false, tps_normal, ti).result;
|
2011-05-12 15:42:12 -07:00
|
|
|
lazily_emit_tydesc_glue(cx, abi::tydesc_field_cmp_glue, ti);
|
2011-08-04 11:25:09 -07:00
|
|
|
let lltydesc = r.val;
|
2011-07-27 14:19:39 +02:00
|
|
|
let lltydescs =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(r.bcx, lltydesc,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::tydesc_field_first_param)]);
|
2011-08-30 09:59:30 +02:00
|
|
|
lltydescs = Load(r.bcx, lltydescs);
|
2011-06-27 18:35:01 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let llfn;
|
|
|
|
alt ti {
|
|
|
|
none. {
|
|
|
|
let llfnptr =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(r.bcx, lltydesc,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::tydesc_field_cmp_glue)]);
|
2011-08-30 09:59:30 +02:00
|
|
|
llfn = Load(r.bcx, llfnptr);
|
2011-06-27 18:35:01 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
some(sti) { llfn = option::get(sti.cmp_glue); }
|
2011-06-27 18:35:01 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let llcmpresultptr = alloca(r.bcx, T_i1());
|
2011-08-04 16:20:09 -07:00
|
|
|
let llargs: [ValueRef] =
|
2011-08-19 15:16:48 -07:00
|
|
|
[llcmpresultptr, r.bcx.fcx.lltaskptr, lltydesc, lltydescs,
|
|
|
|
llrawlhsptr, llrawrhsptr, llop];
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(r.bcx, llfn, llargs);
|
|
|
|
ret rslt(r.bcx, Load(r.bcx, llcmpresultptr));
|
2011-04-19 15:22:57 -07:00
|
|
|
}
|
|
|
|
|
2011-08-24 20:30:20 +02:00
|
|
|
fn call_copy_glue(cx: &@block_ctxt, dst: ValueRef, src: ValueRef, t: ty::t,
|
|
|
|
take: bool) -> @block_ctxt {
|
|
|
|
// You can't call this on immediate types. Those are simply copied with
|
|
|
|
// Load/Store.
|
|
|
|
assert !type_is_immediate(bcx_ccx(cx), t);
|
2011-08-30 09:59:30 +02:00
|
|
|
let srcptr = BitCast(cx, src, T_ptr(T_i8()));
|
|
|
|
let dstptr = BitCast(cx, dst, T_ptr(T_i8()));
|
2011-08-24 20:30:20 +02:00
|
|
|
let ti = none;
|
2011-08-24 18:36:51 -07:00
|
|
|
let {bcx, val: lltydesc} =
|
|
|
|
get_tydesc(cx, t, false, tps_normal, ti).result;
|
2011-08-24 20:30:20 +02:00
|
|
|
lazily_emit_tydesc_glue(cx, abi::tydesc_field_copy_glue, ti);
|
2011-08-30 09:59:30 +02:00
|
|
|
let lltydescs = GEP
|
2011-08-24 20:30:20 +02:00
|
|
|
(bcx, lltydesc, [C_int(0), C_int(abi::tydesc_field_first_param)]);
|
2011-08-30 09:59:30 +02:00
|
|
|
lltydescs = Load(bcx, lltydescs);
|
2011-08-24 20:30:20 +02:00
|
|
|
|
|
|
|
let llfn = alt ti {
|
|
|
|
none. {
|
2011-08-30 09:59:30 +02:00
|
|
|
Load(bcx, GEP
|
2011-08-24 20:30:20 +02:00
|
|
|
(bcx, lltydesc, [C_int(0), C_int(abi::tydesc_field_copy_glue)]))
|
|
|
|
}
|
|
|
|
some(sti) { option::get(sti.copy_glue) }
|
|
|
|
};
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(bcx, llfn, [C_null(T_ptr(T_nil())), bcx.fcx.lltaskptr,
|
2011-08-24 20:30:20 +02:00
|
|
|
C_null(T_ptr(T_nil())), lltydescs, srcptr, dstptr]);
|
|
|
|
if take {
|
|
|
|
lazily_emit_tydesc_glue(cx, abi::tydesc_field_take_glue, ti);
|
|
|
|
llfn = alt ti {
|
|
|
|
none. {
|
2011-08-30 09:59:30 +02:00
|
|
|
Load(bcx, GEP(bcx, lltydesc,
|
2011-08-24 20:30:20 +02:00
|
|
|
[C_int(0),
|
|
|
|
C_int(abi::tydesc_field_take_glue)]))
|
|
|
|
}
|
|
|
|
some(sti) { option::get(sti.take_glue) }
|
|
|
|
};
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(bcx, llfn, [C_null(T_ptr(T_nil())), bcx.fcx.lltaskptr,
|
2011-08-24 20:30:20 +02:00
|
|
|
C_null(T_ptr(T_nil())), lltydescs, dstptr]);
|
|
|
|
}
|
|
|
|
ret bcx;
|
|
|
|
}
|
|
|
|
|
2011-08-22 12:03:11 +02:00
|
|
|
|
2011-06-28 11:23:53 -07:00
|
|
|
// Compares two values. Performs the simple scalar comparison if the types are
|
|
|
|
// scalar and calls to comparison glue otherwise.
|
2011-08-22 12:45:18 +02:00
|
|
|
fn compare(cx: &@block_ctxt, lhs: ValueRef, rhs: ValueRef, t: ty::t,
|
2011-07-27 14:19:39 +02:00
|
|
|
llop: ValueRef) -> result {
|
|
|
|
if ty::type_is_scalar(bcx_tcx(cx), t) {
|
2011-06-28 11:23:53 -07:00
|
|
|
ret compare_scalar_types(cx, lhs, rhs, t, llop);
|
|
|
|
}
|
|
|
|
ret call_cmp_glue(cx, lhs, rhs, t, llop);
|
|
|
|
}
|
|
|
|
|
2011-08-19 10:24:13 +02:00
|
|
|
fn take_ty(cx: &@block_ctxt, v: ValueRef, t: ty::t) -> result {
|
2011-08-25 10:18:02 +02:00
|
|
|
if ty::type_has_pointers(bcx_tcx(cx), t) {
|
2011-08-19 10:24:13 +02:00
|
|
|
ret call_tydesc_glue(cx, v, t, abi::tydesc_field_take_glue);
|
2010-12-17 18:20:10 -08:00
|
|
|
}
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(cx, C_nil());
|
2010-11-24 16:55:45 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn drop_ty(cx: &@block_ctxt, v: ValueRef, t: ty::t) -> result {
|
2011-07-29 12:53:58 +02:00
|
|
|
if ty::type_needs_drop(bcx_tcx(cx), t) {
|
2011-05-20 14:57:52 -07:00
|
|
|
ret call_tydesc_glue(cx, v, t, abi::tydesc_field_drop_glue);
|
2010-12-17 18:20:10 -08:00
|
|
|
}
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(cx, C_nil());
|
2010-11-24 16:55:45 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn free_ty(cx: &@block_ctxt, v: ValueRef, t: ty::t) -> result {
|
|
|
|
if ty::type_has_pointers(bcx_tcx(cx), t) {
|
2011-05-20 14:57:52 -07:00
|
|
|
ret call_tydesc_glue(cx, v, t, abi::tydesc_field_free_glue);
|
2011-05-18 17:28:08 -07:00
|
|
|
}
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(cx, C_nil());
|
2011-05-18 17:28:08 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn call_memmove(cx: &@block_ctxt, dst: ValueRef, src: ValueRef,
|
|
|
|
n_bytes: ValueRef) -> result {
|
2011-05-10 11:50:29 -07:00
|
|
|
// FIXME: switch to the 64-bit variant when on such a platform.
|
2011-06-18 16:59:44 -07:00
|
|
|
// TODO: Provide LLVM with better alignment information when the alignment
|
|
|
|
// is statically known (it must be nothing more than a constant int, or
|
|
|
|
// LLVM complains -- not even a constant element of a tydesc works).
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let i = bcx_ccx(cx).intrinsics;
|
2011-08-25 15:12:54 -07:00
|
|
|
assert (i.contains_key(~"llvm.memmove.p0i8.p0i8.i32"));
|
|
|
|
let memmove = i.get(~"llvm.memmove.p0i8.p0i8.i32");
|
2011-08-30 09:59:30 +02:00
|
|
|
let src_ptr = PointerCast(cx, src, T_ptr(T_i8()));
|
|
|
|
let dst_ptr = PointerCast(cx, dst, T_ptr(T_i8()));
|
|
|
|
let size = IntCast(cx, n_bytes, T_i32());
|
2011-08-04 16:13:25 -07:00
|
|
|
let align = C_int(1);
|
2011-07-27 14:19:39 +02:00
|
|
|
let volatile = C_bool(false);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(cx,
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(cx, memmove,
|
2011-08-19 15:16:48 -07:00
|
|
|
[dst_ptr, src_ptr, size, align, volatile]));
|
2010-11-24 16:55:45 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn call_bzero(cx: &@block_ctxt, dst: ValueRef, n_bytes: ValueRef,
|
|
|
|
align_bytes: ValueRef) -> result {
|
2011-05-10 11:50:29 -07:00
|
|
|
// FIXME: switch to the 64-bit variant when on such a platform.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let i = bcx_ccx(cx).intrinsics;
|
2011-08-25 15:12:54 -07:00
|
|
|
assert (i.contains_key(~"llvm.memset.p0i8.i32"));
|
|
|
|
let memset = i.get(~"llvm.memset.p0i8.i32");
|
2011-08-30 09:59:30 +02:00
|
|
|
let dst_ptr = PointerCast(cx, dst, T_ptr(T_i8()));
|
|
|
|
let size = IntCast(cx, n_bytes, T_i32());
|
2011-07-27 14:19:39 +02:00
|
|
|
let align =
|
|
|
|
if lib::llvm::llvm::LLVMIsConstant(align_bytes) == True {
|
2011-08-30 09:59:30 +02:00
|
|
|
IntCast(cx, align_bytes, T_i32())
|
|
|
|
} else { IntCast(cx, C_int(0), T_i32()) };
|
2011-07-27 14:19:39 +02:00
|
|
|
let volatile = C_bool(false);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(cx,
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(cx, memset,
|
2011-08-19 15:16:48 -07:00
|
|
|
[dst_ptr, C_u8(0u), size, align, volatile]));
|
2011-01-18 15:38:35 -08:00
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn memmove_ty(cx: &@block_ctxt, dst: ValueRef, src: ValueRef, t: ty::t) ->
|
2011-06-15 11:19:50 -07:00
|
|
|
result {
|
2011-07-27 14:19:39 +02:00
|
|
|
if ty::type_has_dynamic_size(bcx_tcx(cx), t) {
|
|
|
|
let llsz = size_of(cx, t);
|
2011-06-18 16:59:44 -07:00
|
|
|
ret call_memmove(llsz.bcx, dst, src, llsz.val);
|
2011-08-04 16:13:25 -07:00
|
|
|
} else if ty::type_is_structural(bcx_tcx(cx), t) {
|
|
|
|
let llsz = llsize_of(type_of(bcx_ccx(cx), cx.sp, t));
|
|
|
|
ret call_memmove(cx, dst, src, llsz);
|
2011-08-30 09:59:30 +02:00
|
|
|
} else { ret rslt(cx, Store(cx, Load(cx, src), dst)); }
|
2011-01-17 13:30:29 -08:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
tag copy_action { INIT; DROP_EXISTING; }
|
2011-06-10 13:39:13 -07:00
|
|
|
|
2011-08-22 12:38:58 +02:00
|
|
|
// These are the types that are passed by pointer.
|
|
|
|
fn type_is_structural_or_param(tcx: &ty::ctxt, t: ty::t) -> bool {
|
|
|
|
if ty::type_is_structural(tcx, t) { ret true; }
|
|
|
|
alt ty::struct(tcx, t) {
|
|
|
|
ty::ty_param(_, _) { ret true; }
|
|
|
|
_ { ret false; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn copy_val(cx: &@block_ctxt, action: copy_action, dst: ValueRef,
|
2011-08-22 12:45:18 +02:00
|
|
|
src: ValueRef, t: ty::t) -> @block_ctxt {
|
2011-08-22 12:38:58 +02:00
|
|
|
if type_is_structural_or_param(bcx_ccx(cx).tcx, t) &&
|
|
|
|
action == DROP_EXISTING {
|
2011-08-26 21:34:56 -07:00
|
|
|
let do_copy_cx = new_sub_block_ctxt(cx, ~"do_copy");
|
|
|
|
let next_cx = new_sub_block_ctxt(cx, ~"next");
|
2011-08-22 12:38:58 +02:00
|
|
|
let self_assigning =
|
2011-08-30 09:59:30 +02:00
|
|
|
ICmp(cx, lib::llvm::LLVMIntNE,
|
|
|
|
PointerCast(cx, dst, val_ty(src)), src);
|
|
|
|
CondBr(cx, self_assigning, do_copy_cx.llbb, next_cx.llbb);
|
2011-08-22 12:38:58 +02:00
|
|
|
do_copy_cx = copy_val_no_check(do_copy_cx, action, dst, src, t);
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(do_copy_cx, next_cx.llbb);
|
2011-08-22 12:38:58 +02:00
|
|
|
ret next_cx;
|
|
|
|
}
|
|
|
|
ret copy_val_no_check(cx, action, dst, src, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn copy_val_no_check(cx: &@block_ctxt, action: copy_action, dst: ValueRef,
|
2011-08-22 12:45:18 +02:00
|
|
|
src: ValueRef, t: ty::t) -> @block_ctxt {
|
2011-07-27 14:19:39 +02:00
|
|
|
let ccx = bcx_ccx(cx);
|
2011-06-25 12:22:50 +02:00
|
|
|
// FIXME this is just a clunky stopgap. we should do proper checking in an
|
|
|
|
// earlier pass.
|
2011-07-27 14:19:39 +02:00
|
|
|
if !ty::type_is_copyable(ccx.tcx, t) {
|
2011-08-27 16:36:48 -07:00
|
|
|
ccx.sess.span_fatal(cx.sp, ~"Copying a non-copyable type.");
|
2011-06-25 12:22:50 +02:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
if ty::type_is_scalar(ccx.tcx, t) || ty::type_is_native(ccx.tcx, t) {
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(cx, src, dst);
|
2011-08-22 12:38:58 +02:00
|
|
|
ret cx;
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if ty::type_is_nil(ccx.tcx, t) || ty::type_is_bot(ccx.tcx, t) {
|
2011-08-22 12:38:58 +02:00
|
|
|
ret cx;
|
2011-08-25 10:18:02 +02:00
|
|
|
} else if ty::type_is_boxed(ccx.tcx, t) ||
|
|
|
|
ty::type_is_ivec(ccx.tcx, t) {
|
2011-08-22 12:38:58 +02:00
|
|
|
let bcx = if action == DROP_EXISTING {
|
2011-08-26 11:20:10 +02:00
|
|
|
drop_ty(cx, dst, t).bcx
|
2011-08-22 12:38:58 +02:00
|
|
|
} else { cx };
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(bcx, src, dst);
|
2011-08-26 11:20:10 +02:00
|
|
|
bcx = take_ty(bcx, dst, t).bcx;
|
2011-08-22 12:38:58 +02:00
|
|
|
ret bcx;
|
|
|
|
} else if type_is_structural_or_param(ccx.tcx, t) {
|
|
|
|
let bcx = if action == DROP_EXISTING {
|
|
|
|
drop_ty(cx, dst, t).bcx
|
|
|
|
} else { cx };
|
2011-08-25 10:18:02 +02:00
|
|
|
bcx = memmove_ty(bcx, dst, src, t).bcx;
|
|
|
|
ret take_ty(bcx, dst, t).bcx;
|
2011-08-22 12:38:58 +02:00
|
|
|
}
|
2011-08-27 16:36:48 -07:00
|
|
|
ccx.sess.bug(~"unexpected type in trans::copy_val_no_check: " +
|
|
|
|
ty_to_str(ccx.tcx, t));
|
2010-11-09 17:49:20 -08:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-05-31 14:36:08 -07:00
|
|
|
// This works like copy_val, except that it deinitializes the source.
|
|
|
|
// Since it needs to zero out the source, src also needs to be an lval.
|
|
|
|
// FIXME: We always zero out the source. Ideally we would detect the
|
|
|
|
// case where a variable is always deinitialized by block exit and thus
|
|
|
|
// doesn't need to be dropped.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn move_val(cx: @block_ctxt, action: copy_action, dst: ValueRef,
|
2011-08-22 12:45:18 +02:00
|
|
|
src: &lval_result, t: ty::t) -> @block_ctxt {
|
2011-07-27 14:19:39 +02:00
|
|
|
let src_val = src.res.val;
|
2011-08-22 14:00:41 +02:00
|
|
|
let tcx = bcx_tcx(cx);
|
|
|
|
if ty::type_is_scalar(tcx, t) ||
|
|
|
|
ty::type_is_native(tcx, t) {
|
2011-08-30 09:59:30 +02:00
|
|
|
if src.is_mem { src_val = Load(cx, src_val); }
|
|
|
|
Store(cx, src_val, dst);
|
2011-08-22 12:38:58 +02:00
|
|
|
ret cx;
|
2011-08-22 14:00:41 +02:00
|
|
|
} else if ty::type_is_nil(tcx, t) || ty::type_is_bot(tcx, t) {
|
2011-08-22 12:38:58 +02:00
|
|
|
ret cx;
|
2011-08-22 14:00:41 +02:00
|
|
|
} else if ty::type_is_unique(tcx, t) ||
|
2011-08-25 10:18:02 +02:00
|
|
|
ty::type_is_boxed(tcx, t) {
|
2011-08-30 09:59:30 +02:00
|
|
|
if src.is_mem { src_val = Load(cx, src_val); }
|
2011-07-27 14:19:39 +02:00
|
|
|
if action == DROP_EXISTING {
|
2011-08-26 11:20:10 +02:00
|
|
|
cx = drop_ty(cx, dst, t).bcx;
|
2011-05-31 14:36:08 -07:00
|
|
|
}
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(cx, src_val, dst);
|
2011-08-22 12:38:58 +02:00
|
|
|
if src.is_mem { ret zero_alloca(cx, src.res.val, t).bcx; }
|
2011-08-16 12:38:42 -07:00
|
|
|
|
|
|
|
// If we're here, it must be a temporary.
|
|
|
|
revoke_clean(cx, src_val);
|
2011-08-22 12:38:58 +02:00
|
|
|
ret cx;
|
2011-08-22 14:00:41 +02:00
|
|
|
} else if type_is_structural_or_param(tcx, t) {
|
2011-07-27 14:19:39 +02:00
|
|
|
if action == DROP_EXISTING { cx = drop_ty(cx, dst, t).bcx; }
|
2011-08-25 10:18:02 +02:00
|
|
|
cx = memmove_ty(cx, dst, src_val, t).bcx;
|
2011-07-27 14:19:39 +02:00
|
|
|
if src.is_mem {
|
2011-08-22 12:38:58 +02:00
|
|
|
ret zero_alloca(cx, src_val, t).bcx;
|
Make moving of temporaries do the right thing, use it to optimize
This adds support for dropping cleanups for temporary values when they
are moved somewhere else. It then adds wraps most copy operations
(return, put in data structure, box, etc) in a way that will fall back
to a move when it is safe.
This saves a lot of taking/dropping, shaving over a megabyte off the
stage2/rustc binary size.
In some cases, most notably function returns, we could detect that the
returned value is a local variable, and can thus be safely moved even
though it is not a temporary. This will require putting some more
information in lvals.
I did not yet handle function arguments, since the logic for passing
them looked too convoluted to touch. I'll probably try that in the
near future, since it's bound to be a big win.
2011-07-07 13:36:12 +02:00
|
|
|
} else { // Temporary value
|
|
|
|
revoke_clean(cx, src_val);
|
2011-08-22 12:38:58 +02:00
|
|
|
ret cx;
|
Make moving of temporaries do the right thing, use it to optimize
This adds support for dropping cleanups for temporary values when they
are moved somewhere else. It then adds wraps most copy operations
(return, put in data structure, box, etc) in a way that will fall back
to a move when it is safe.
This saves a lot of taking/dropping, shaving over a megabyte off the
stage2/rustc binary size.
In some cases, most notably function returns, we could detect that the
returned value is a local variable, and can thus be safely moved even
though it is not a temporary. This will require putting some more
information in lvals.
I did not yet handle function arguments, since the logic for passing
them looked too convoluted to touch. I'll probably try that in the
near future, since it's bound to be a big win.
2011-07-07 13:36:12 +02:00
|
|
|
}
|
2011-05-31 14:36:08 -07:00
|
|
|
}
|
2011-08-27 16:36:48 -07:00
|
|
|
bcx_ccx(cx).sess.bug(~"unexpected type in trans::move_val: " +
|
|
|
|
ty_to_str(tcx, t));
|
Make moving of temporaries do the right thing, use it to optimize
This adds support for dropping cleanups for temporary values when they
are moved somewhere else. It then adds wraps most copy operations
(return, put in data structure, box, etc) in a way that will fall back
to a move when it is safe.
This saves a lot of taking/dropping, shaving over a megabyte off the
stage2/rustc binary size.
In some cases, most notably function returns, we could detect that the
returned value is a local variable, and can thus be safely moved even
though it is not a temporary. This will require putting some more
information in lvals.
I did not yet handle function arguments, since the logic for passing
them looked too convoluted to touch. I'll probably try that in the
near future, since it's bound to be a big win.
2011-07-07 13:36:12 +02:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn move_val_if_temp(cx: @block_ctxt, action: copy_action, dst: ValueRef,
|
2011-08-22 12:45:18 +02:00
|
|
|
src: &lval_result, t: ty::t) -> @block_ctxt {
|
2011-07-27 14:19:39 +02:00
|
|
|
|
Make moving of temporaries do the right thing, use it to optimize
This adds support for dropping cleanups for temporary values when they
are moved somewhere else. It then adds wraps most copy operations
(return, put in data structure, box, etc) in a way that will fall back
to a move when it is safe.
This saves a lot of taking/dropping, shaving over a megabyte off the
stage2/rustc binary size.
In some cases, most notably function returns, we could detect that the
returned value is a local variable, and can thus be safely moved even
though it is not a temporary. This will require putting some more
information in lvals.
I did not yet handle function arguments, since the logic for passing
them looked too convoluted to touch. I'll probably try that in the
near future, since it's bound to be a big win.
2011-07-07 13:36:12 +02:00
|
|
|
// Lvals in memory are not temporaries. Copy them.
|
2011-07-27 14:19:39 +02:00
|
|
|
if src.is_mem {
|
|
|
|
ret copy_val(cx, action, dst, load_if_immediate(cx, src.res.val, t),
|
|
|
|
t);
|
2011-08-16 12:38:42 -07:00
|
|
|
}
|
|
|
|
ret move_val(cx, action, dst, src, t);
|
2011-05-31 14:36:08 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_crate_lit(cx: &@crate_ctxt, lit: &ast::lit) -> ValueRef {
|
|
|
|
alt lit.node {
|
|
|
|
ast::lit_int(i) { ret C_int(i); }
|
|
|
|
ast::lit_uint(u) { ret C_int(u as int); }
|
|
|
|
ast::lit_mach_int(tm, i) {
|
|
|
|
// FIXME: the entire handling of mach types falls apart
|
|
|
|
// if target int width is larger than host, at the moment;
|
|
|
|
// re-do the mach-int types using 'big' when that works.
|
|
|
|
|
|
|
|
let t = T_int();
|
|
|
|
let s = True;
|
|
|
|
alt tm {
|
|
|
|
ast::ty_u8. { t = T_i8(); s = False; }
|
|
|
|
ast::ty_u16. { t = T_i16(); s = False; }
|
|
|
|
ast::ty_u32. { t = T_i32(); s = False; }
|
|
|
|
ast::ty_u64. { t = T_i64(); s = False; }
|
|
|
|
ast::ty_i8. { t = T_i8(); }
|
|
|
|
ast::ty_i16. { t = T_i16(); }
|
|
|
|
ast::ty_i32. { t = T_i32(); }
|
|
|
|
ast::ty_i64. { t = T_i64(); }
|
|
|
|
}
|
|
|
|
ret C_integral(t, i as uint, s);
|
|
|
|
}
|
2011-08-26 18:48:08 -07:00
|
|
|
ast::lit_float(fs) { ret C_float(fs); }
|
2011-07-27 14:19:39 +02:00
|
|
|
ast::lit_mach_float(tm, s) {
|
|
|
|
let t = T_float();
|
|
|
|
alt tm { ast::ty_f32. { t = T_f32(); } ast::ty_f64. { t = T_f64(); } }
|
2011-08-26 18:48:08 -07:00
|
|
|
ret C_floating(s, t);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::lit_char(c) { ret C_integral(T_char(), c as uint, False); }
|
|
|
|
ast::lit_bool(b) { ret C_bool(b); }
|
|
|
|
ast::lit_nil. { ret C_nil(); }
|
2011-08-26 18:48:08 -07:00
|
|
|
ast::lit_str(s, ast::sk_rc.) { ret C_str(cx, s); }
|
2011-07-27 14:19:39 +02:00
|
|
|
ast::lit_str(s, ast::sk_unique.) {
|
2011-08-27 16:36:48 -07:00
|
|
|
cx.sess.span_unimpl(lit.span, ~"unique string in this context");
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-07-14 11:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_lit(cx: &@block_ctxt, lit: &ast::lit) -> result {
|
|
|
|
alt lit.node {
|
2011-08-25 10:18:02 +02:00
|
|
|
ast::lit_str(s, ast::sk_unique.) { ret ivec::trans_istr(cx, s); }
|
2011-07-26 13:02:26 -07:00
|
|
|
_ { ret rslt(cx, trans_crate_lit(bcx_ccx(cx), lit)); }
|
2010-09-28 14:01:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-04-08 21:27:54 -07:00
|
|
|
// Converts an annotation to a type
|
2011-07-27 14:19:39 +02:00
|
|
|
fn node_id_type(cx: &@crate_ctxt, id: ast::node_id) -> ty::t {
|
2011-06-19 22:41:21 +02:00
|
|
|
ret ty::node_id_to_monotype(cx.tcx, id);
|
2010-11-05 18:31:02 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn node_type(cx: &@crate_ctxt, sp: &span, id: ast::node_id) -> TypeRef {
|
2011-06-19 22:41:21 +02:00
|
|
|
ret type_of(cx, sp, node_id_type(cx, id));
|
2010-11-22 16:27:00 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_unary(cx: &@block_ctxt, op: ast::unop, e: &@ast::expr,
|
|
|
|
id: ast::node_id) -> result {
|
|
|
|
let e_ty = ty::expr_ty(bcx_tcx(cx), e);
|
|
|
|
alt op {
|
|
|
|
ast::not. {
|
|
|
|
let sub = trans_expr(cx, e);
|
2011-08-30 09:59:30 +02:00
|
|
|
ret rslt(sub.bcx, Not(sub.bcx, sub.val));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::neg. {
|
|
|
|
let sub = trans_expr(cx, e);
|
|
|
|
if ty::struct(bcx_tcx(cx), e_ty) == ty::ty_float {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret rslt(sub.bcx, FNeg(sub.bcx, sub.val));
|
|
|
|
} else { ret rslt(sub.bcx, Neg(sub.bcx, sub.val)); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::box(_) {
|
|
|
|
let lv = trans_lval(cx, e);
|
|
|
|
let box_ty = node_id_type(bcx_ccx(lv.res.bcx), id);
|
|
|
|
let sub = trans_malloc_boxed(lv.res.bcx, e_ty);
|
2011-07-28 15:44:51 -07:00
|
|
|
let body = sub.body;
|
|
|
|
add_clean_temp(cx, sub.box, box_ty);
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
// Cast the body type to the type of the value. This is needed to
|
|
|
|
// make tags work, since tags have a different LLVM type depending
|
|
|
|
// on whether they're boxed or not.
|
|
|
|
if !ty::type_has_dynamic_size(bcx_tcx(cx), e_ty) {
|
|
|
|
let llety = T_ptr(type_of(bcx_ccx(sub.bcx), e.span, e_ty));
|
2011-08-30 09:59:30 +02:00
|
|
|
body = PointerCast(sub.bcx, body, llety);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-22 12:38:58 +02:00
|
|
|
let bcx = move_val_if_temp(sub.bcx, INIT, body, lv, e_ty);
|
|
|
|
ret rslt(bcx, sub.box);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::deref. {
|
2011-08-27 16:36:48 -07:00
|
|
|
bcx_ccx(cx).sess.bug(~"deref expressions should have been \
|
2011-07-22 23:56:02 -07:00
|
|
|
translated using trans_lval(), not \
|
|
|
|
trans_unary()");
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2010-09-28 14:01:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
fn trans_compare(cx: &@block_ctxt, op: ast::binop, lhs: ValueRef,
|
|
|
|
_lhs_t: ty::t, rhs: ValueRef, rhs_t: ty::t) -> result {
|
2011-04-19 15:22:57 -07:00
|
|
|
// Determine the operation we need.
|
2011-07-27 14:19:39 +02:00
|
|
|
let llop;
|
|
|
|
alt op {
|
2011-08-02 13:17:30 -07:00
|
|
|
ast::eq. | ast::ne. { llop = C_u8(abi::cmp_glue_op_eq); }
|
|
|
|
ast::lt. | ast::ge. { llop = C_u8(abi::cmp_glue_op_lt); }
|
|
|
|
ast::le. | ast::gt. { llop = C_u8(abi::cmp_glue_op_le); }
|
2011-02-28 16:36:08 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
|
2011-08-12 16:33:53 -07:00
|
|
|
let rs = compare(cx, lhs, rhs, rhs_t, llop);
|
2011-02-28 16:36:08 -08:00
|
|
|
|
2011-08-02 18:04:24 -07:00
|
|
|
// Invert the result if necessary.
|
|
|
|
alt op {
|
|
|
|
ast::eq. | ast::lt. | ast::le. { ret rslt(rs.bcx, rs.val); }
|
|
|
|
ast::ne. | ast::ge. | ast::gt. {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret rslt(rs.bcx, Not(rs.bcx, rs.val));
|
2011-08-02 18:04:24 -07:00
|
|
|
}
|
2011-02-10 19:40:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn trans_evec_append(cx: &@block_ctxt, t: ty::t, lhs: ValueRef,
|
2011-08-19 15:16:48 -07:00
|
|
|
rhs: ValueRef) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let elt_ty = ty::sequence_element_type(bcx_tcx(cx), t);
|
|
|
|
let skip_null = C_bool(false);
|
|
|
|
alt ty::struct(bcx_tcx(cx), t) {
|
|
|
|
ty::ty_str. { skip_null = C_bool(true); }
|
|
|
|
_ { }
|
|
|
|
}
|
|
|
|
let bcx = cx;
|
2011-08-13 00:09:25 -07:00
|
|
|
let ti = none::<@tydesc_info>;
|
2011-08-24 18:36:51 -07:00
|
|
|
let llvec_tydesc = get_tydesc(bcx, t, false, tps_normal, ti).result;
|
2011-03-03 18:18:51 -08:00
|
|
|
bcx = llvec_tydesc.bcx;
|
2011-08-13 00:09:25 -07:00
|
|
|
ti = none::<@tydesc_info>;
|
2011-08-24 18:36:51 -07:00
|
|
|
let llelt_tydesc = get_tydesc(bcx, elt_ty, false, tps_normal, ti).result;
|
2011-08-19 10:24:13 +02:00
|
|
|
lazily_emit_tydesc_glue(cx, abi::tydesc_field_take_glue, ti);
|
2011-05-12 15:42:12 -07:00
|
|
|
lazily_emit_tydesc_glue(cx, abi::tydesc_field_drop_glue, ti);
|
2011-05-18 17:28:08 -07:00
|
|
|
lazily_emit_tydesc_glue(cx, abi::tydesc_field_free_glue, ti);
|
2011-03-03 18:18:51 -08:00
|
|
|
bcx = llelt_tydesc.bcx;
|
2011-08-30 09:59:30 +02:00
|
|
|
let dst = PointerCast(bcx, lhs, T_ptr(T_opaque_vec_ptr()));
|
|
|
|
let src = PointerCast(bcx, rhs, T_opaque_vec_ptr());
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(bcx,
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(bcx, bcx_ccx(cx).upcalls.evec_append,
|
2011-08-19 15:16:48 -07:00
|
|
|
[cx.fcx.lltaskptr, llvec_tydesc.val,
|
|
|
|
llelt_tydesc.val, dst, src, skip_null]));
|
2011-03-02 16:42:09 -08:00
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn trans_evec_add(cx: &@block_ctxt, t: ty::t, lhs: ValueRef, rhs: ValueRef)
|
2011-08-19 15:16:48 -07:00
|
|
|
-> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let r = alloc_ty(cx, t);
|
|
|
|
let tmp = r.val;
|
2011-08-22 12:38:58 +02:00
|
|
|
let bcx = copy_val(r.bcx, INIT, tmp, lhs, t);
|
|
|
|
let bcx = trans_evec_append(bcx, t, tmp, rhs).bcx;
|
2011-04-12 12:06:20 -07:00
|
|
|
tmp = load_if_immediate(bcx, tmp, t);
|
Make moving of temporaries do the right thing, use it to optimize
This adds support for dropping cleanups for temporary values when they
are moved somewhere else. It then adds wraps most copy operations
(return, put in data structure, box, etc) in a way that will fall back
to a move when it is safe.
This saves a lot of taking/dropping, shaving over a megabyte off the
stage2/rustc binary size.
In some cases, most notably function returns, we could detect that the
returned value is a local variable, and can thus be safely moved even
though it is not a temporary. This will require putting some more
information in lvals.
I did not yet handle function arguments, since the logic for passing
them looked too convoluted to touch. I'll probably try that in the
near future, since it's bound to be a big win.
2011-07-07 13:36:12 +02:00
|
|
|
add_clean_temp(cx, tmp, t);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(bcx, tmp);
|
2011-03-02 16:42:09 -08:00
|
|
|
}
|
|
|
|
|
2011-08-02 17:21:28 -07:00
|
|
|
// Important to get types for both lhs and rhs, because one might be _|_
|
|
|
|
// and the other not.
|
|
|
|
fn trans_eager_binop(cx: &@block_ctxt, op: ast::binop, lhs: ValueRef,
|
|
|
|
lhs_t: ty::t, rhs: ValueRef, rhs_t: ty::t) -> result {
|
2011-08-02 18:04:24 -07:00
|
|
|
|
|
|
|
// If either is bottom, it diverges. So no need to do the
|
|
|
|
// operation.
|
2011-08-19 15:16:48 -07:00
|
|
|
if ty::type_is_bot(bcx_tcx(cx), lhs_t) ||
|
|
|
|
ty::type_is_bot(bcx_tcx(cx), rhs_t) {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret rslt(cx, Unreachable(cx));
|
2011-08-02 18:04:24 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let is_float = false;
|
2011-08-02 17:21:28 -07:00
|
|
|
let intype = lhs_t;
|
2011-08-19 15:16:48 -07:00
|
|
|
if ty::type_is_bot(bcx_tcx(cx), intype) { intype = rhs_t; }
|
2011-08-02 17:21:28 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
alt ty::struct(bcx_tcx(cx), intype) {
|
|
|
|
ty::ty_float. { is_float = true; }
|
|
|
|
_ { is_float = false; }
|
2011-03-21 16:21:54 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
alt op {
|
|
|
|
ast::add. {
|
|
|
|
if ty::type_is_sequence(bcx_tcx(cx), intype) {
|
|
|
|
if ty::sequence_is_interior(bcx_tcx(cx), intype) {
|
|
|
|
ret ivec::trans_add(cx, intype, lhs, rhs);
|
2011-03-02 16:42:09 -08:00
|
|
|
}
|
2011-08-18 12:22:13 -07:00
|
|
|
ret trans_evec_add(cx, intype, lhs, rhs);
|
2011-03-02 16:42:09 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
if is_float {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret rslt(cx, FAdd(cx, lhs, rhs));
|
|
|
|
} else { ret rslt(cx, Add(cx, lhs, rhs)); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::sub. {
|
|
|
|
if is_float {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret rslt(cx, FSub(cx, lhs, rhs));
|
|
|
|
} else { ret rslt(cx, Sub(cx, lhs, rhs)); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::mul. {
|
|
|
|
if is_float {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret rslt(cx, FMul(cx, lhs, rhs));
|
|
|
|
} else { ret rslt(cx, Mul(cx, lhs, rhs)); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::div. {
|
2011-08-30 09:59:30 +02:00
|
|
|
if is_float { ret rslt(cx, FDiv(cx, lhs, rhs)); }
|
2011-07-27 14:19:39 +02:00
|
|
|
if ty::type_is_signed(bcx_tcx(cx), intype) {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret rslt(cx, SDiv(cx, lhs, rhs));
|
|
|
|
} else { ret rslt(cx, UDiv(cx, lhs, rhs)); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::rem. {
|
2011-08-30 09:59:30 +02:00
|
|
|
if is_float { ret rslt(cx, FRem(cx, lhs, rhs)); }
|
2011-07-27 14:19:39 +02:00
|
|
|
if ty::type_is_signed(bcx_tcx(cx), intype) {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret rslt(cx, SRem(cx, lhs, rhs));
|
|
|
|
} else { ret rslt(cx, URem(cx, lhs, rhs)); }
|
|
|
|
}
|
|
|
|
ast::bitor. { ret rslt(cx, Or(cx, lhs, rhs)); }
|
|
|
|
ast::bitand. { ret rslt(cx, And(cx, lhs, rhs)); }
|
|
|
|
ast::bitxor. { ret rslt(cx, Xor(cx, lhs, rhs)); }
|
|
|
|
ast::lsl. { ret rslt(cx, Shl(cx, lhs, rhs)); }
|
|
|
|
ast::lsr. { ret rslt(cx, LShr(cx, lhs, rhs)); }
|
|
|
|
ast::asr. { ret rslt(cx, AShr(cx, lhs, rhs)); }
|
2011-08-02 17:21:28 -07:00
|
|
|
_ { ret trans_compare(cx, op, lhs, lhs_t, rhs, rhs_t); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn autoderef(cx: &@block_ctxt, v: ValueRef, t: ty::t) -> result_t {
|
2011-07-27 14:19:39 +02:00
|
|
|
let v1: ValueRef = v;
|
|
|
|
let t1: ty::t = t;
|
|
|
|
let ccx = bcx_ccx(cx);
|
|
|
|
while true {
|
|
|
|
alt ty::struct(ccx.tcx, t1) {
|
|
|
|
ty::ty_box(mt) {
|
|
|
|
let body =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(cx, v1, [C_int(0), C_int(abi::box_rc_field_body)]);
|
2011-07-27 14:19:39 +02:00
|
|
|
t1 = mt.ty;
|
|
|
|
|
|
|
|
// Since we're changing levels of box indirection, we may have
|
|
|
|
// to cast this pointer, since statically-sized tag types have
|
|
|
|
// different types depending on whether they're behind a box
|
|
|
|
// or not.
|
|
|
|
if !ty::type_has_dynamic_size(ccx.tcx, mt.ty) {
|
|
|
|
let llty = type_of(ccx, cx.sp, mt.ty);
|
2011-08-30 09:59:30 +02:00
|
|
|
v1 = PointerCast(cx, body, T_ptr(llty));
|
2011-07-27 14:19:39 +02:00
|
|
|
} else { v1 = body; }
|
|
|
|
}
|
2011-08-10 17:23:46 -07:00
|
|
|
ty::ty_uniq(t) { fail "autoderef uniq unimplemented"; }
|
2011-07-27 14:19:39 +02:00
|
|
|
ty::ty_res(did, inner, tps) {
|
|
|
|
t1 = ty::substitute_type_params(ccx.tcx, tps, inner);
|
2011-08-30 09:59:30 +02:00
|
|
|
v1 = GEP(cx, v1, [C_int(0), C_int(1)]);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_tag(did, tps) {
|
|
|
|
let variants = ty::tag_variants(ccx.tcx, did);
|
2011-08-15 16:38:23 -07:00
|
|
|
if std::vec::len(variants) != 1u ||
|
2011-08-19 15:16:48 -07:00
|
|
|
std::vec::len(variants[0].args) != 1u {
|
2011-07-27 14:19:39 +02:00
|
|
|
break;
|
2011-07-01 13:57:03 +02:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
t1 =
|
2011-08-19 15:16:48 -07:00
|
|
|
ty::substitute_type_params(ccx.tcx, tps, variants[0].args[0]);
|
2011-07-27 14:19:39 +02:00
|
|
|
if !ty::type_has_dynamic_size(ccx.tcx, t1) {
|
2011-08-30 09:59:30 +02:00
|
|
|
v1 = PointerCast(cx, v1, T_ptr(type_of(ccx, cx.sp, t1)));
|
2011-01-24 18:03:31 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ { break; }
|
2011-01-24 18:03:31 -08:00
|
|
|
}
|
2011-07-11 13:12:44 -05:00
|
|
|
v1 = load_if_immediate(cx, v1, t1);
|
2011-01-24 18:03:31 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ret {bcx: cx, val: v1, ty: t1};
|
2011-01-24 18:03:31 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_binary(cx: &@block_ctxt, op: ast::binop, a: &@ast::expr,
|
|
|
|
b: &@ast::expr) -> result {
|
2011-08-19 15:16:48 -07:00
|
|
|
|
2010-10-19 17:24:15 -07:00
|
|
|
// First couple cases are lazy:
|
2011-07-27 14:19:39 +02:00
|
|
|
alt op {
|
|
|
|
ast::and. {
|
|
|
|
// Lazy-eval and
|
2011-08-12 16:33:53 -07:00
|
|
|
let lhs_res = trans_expr(cx, a);
|
2011-08-26 21:34:56 -07:00
|
|
|
let rhs_cx = new_scope_block_ctxt(cx, ~"rhs");
|
2011-08-12 16:33:53 -07:00
|
|
|
let rhs_res = trans_expr(rhs_cx, b);
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
let lhs_false_cx = new_scope_block_ctxt(cx, ~"lhs false");
|
2011-07-27 14:19:39 +02:00
|
|
|
let lhs_false_res = rslt(lhs_false_cx, C_bool(false));
|
2011-08-12 16:32:07 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
// The following line ensures that any cleanups for rhs
|
|
|
|
// are done within the block for rhs. This is necessary
|
|
|
|
// because and/or are lazy. So the rhs may never execute,
|
|
|
|
// and the cleanups can't be pushed into later code.
|
|
|
|
let rhs_bcx = trans_block_cleanups(rhs_res.bcx, rhs_cx);
|
2011-08-30 09:59:30 +02:00
|
|
|
CondBr(lhs_res.bcx, lhs_res.val, rhs_cx.llbb, lhs_false_cx.llbb);
|
2011-07-27 14:19:39 +02:00
|
|
|
ret join_results(cx, T_bool(),
|
2011-08-19 15:16:48 -07:00
|
|
|
[lhs_false_res, {bcx: rhs_bcx, val: rhs_res.val}]);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::or. {
|
|
|
|
// Lazy-eval or
|
2011-08-12 16:33:53 -07:00
|
|
|
let lhs_res = trans_expr(cx, a);
|
2011-08-26 21:34:56 -07:00
|
|
|
let rhs_cx = new_scope_block_ctxt(cx, ~"rhs");
|
2011-08-12 16:33:53 -07:00
|
|
|
let rhs_res = trans_expr(rhs_cx, b);
|
2011-08-26 21:34:56 -07:00
|
|
|
let lhs_true_cx = new_scope_block_ctxt(cx, ~"lhs true");
|
2011-07-27 14:19:39 +02:00
|
|
|
let lhs_true_res = rslt(lhs_true_cx, C_bool(true));
|
|
|
|
|
2011-08-12 16:32:07 -07:00
|
|
|
// see the and case for an explanation
|
2011-07-27 14:19:39 +02:00
|
|
|
let rhs_bcx = trans_block_cleanups(rhs_res.bcx, rhs_cx);
|
2011-08-30 09:59:30 +02:00
|
|
|
CondBr(lhs_res.bcx, lhs_res.val, lhs_true_cx.llbb, rhs_cx.llbb);
|
2011-07-27 14:19:39 +02:00
|
|
|
ret join_results(cx, T_bool(),
|
2011-08-19 15:16:48 -07:00
|
|
|
[lhs_true_res, {bcx: rhs_bcx, val: rhs_res.val}]);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ {
|
|
|
|
// Remaining cases are eager:
|
2011-08-12 16:33:53 -07:00
|
|
|
let lhs = trans_expr(cx, a);
|
|
|
|
let rhs = trans_expr(lhs.bcx, b);
|
2011-07-27 14:19:39 +02:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
ret trans_eager_binop(rhs.bcx, op, lhs.val,
|
|
|
|
ty::expr_ty(bcx_tcx(cx), a), rhs.val,
|
|
|
|
ty::expr_ty(bcx_tcx(cx), b));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2010-09-28 14:01:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-04 16:20:09 -07:00
|
|
|
fn join_results(parent_cx: &@block_ctxt, t: TypeRef, ins: &[result]) ->
|
2011-07-27 14:19:39 +02:00
|
|
|
result {
|
2011-08-19 15:16:48 -07:00
|
|
|
let live: [result] = [];
|
|
|
|
let vals: [ValueRef] = [];
|
|
|
|
let bbs: [BasicBlockRef] = [];
|
2011-08-15 21:54:52 -07:00
|
|
|
for r: result in ins {
|
2011-07-27 14:19:39 +02:00
|
|
|
if !is_terminated(r.bcx) {
|
2011-08-19 15:16:48 -07:00
|
|
|
live += [r];
|
|
|
|
vals += [r.val];
|
|
|
|
bbs += [r.bcx.llbb];
|
2010-11-12 14:51:11 -08:00
|
|
|
}
|
|
|
|
}
|
2011-08-13 00:09:25 -07:00
|
|
|
alt std::vec::len::<result>(live) {
|
2011-07-27 14:19:39 +02:00
|
|
|
0u {
|
|
|
|
// No incoming edges are live, so we're in dead-code-land.
|
|
|
|
// Arbitrarily pick the first dead edge, since the caller
|
|
|
|
// is just going to propagate it outward.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-08-13 00:09:25 -07:00
|
|
|
assert (std::vec::len::<result>(ins) >= 1u);
|
2011-08-19 15:16:48 -07:00
|
|
|
ret ins[0];
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ {/* fall through */ }
|
2010-11-12 14:51:11 -08:00
|
|
|
}
|
|
|
|
// We have >1 incoming edges. Make a join block and br+phi them into it.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
let join_cx = new_sub_block_ctxt(parent_cx, ~"join");
|
2011-08-30 09:59:30 +02:00
|
|
|
for r: result in live { Br(r.bcx, join_cx.llbb); }
|
|
|
|
let phi = Phi(join_cx, t, vals, bbs);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(join_cx, phi);
|
2010-11-12 14:51:11 -08:00
|
|
|
}
|
|
|
|
|
2011-08-04 16:20:09 -07:00
|
|
|
fn join_branches(parent_cx: &@block_ctxt, ins: &[result]) -> @block_ctxt {
|
2011-08-26 21:34:56 -07:00
|
|
|
let out = new_sub_block_ctxt(parent_cx, ~"join");
|
2011-08-15 21:54:52 -07:00
|
|
|
for r: result in ins {
|
2011-08-30 09:59:30 +02:00
|
|
|
if !is_terminated(r.bcx) { Br(r.bcx, out.llbb); }
|
2011-05-31 14:24:04 +02:00
|
|
|
}
|
|
|
|
ret out;
|
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
tag out_method { return; save_in(ValueRef); }
|
2011-05-31 14:24:04 +02:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_if(cx: &@block_ctxt, cond: &@ast::expr, thn: &ast::blk,
|
2011-08-19 15:16:48 -07:00
|
|
|
els: &option::t<@ast::expr>, output: &out_method) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let cond_res = trans_expr(cx, cond);
|
2011-08-03 13:05:46 -07:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
if ty::type_is_bot(bcx_tcx(cx), ty::expr_ty(bcx_tcx(cx), cond)) {
|
|
|
|
|
2011-08-03 13:05:46 -07:00
|
|
|
// No need to generate code for comparison,
|
|
|
|
// since the cond diverges.
|
2011-08-24 14:54:55 +02:00
|
|
|
if !is_terminated(cx) {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret rslt(cx, Unreachable(cx));
|
2011-08-19 15:16:48 -07:00
|
|
|
} else { ret cond_res; }
|
2011-08-03 13:05:46 -07:00
|
|
|
}
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
let then_cx = new_scope_block_ctxt(cx, ~"then");
|
2011-07-27 14:19:39 +02:00
|
|
|
let then_res = trans_block(then_cx, thn, output);
|
2011-08-26 21:34:56 -07:00
|
|
|
let else_cx = new_scope_block_ctxt(cx, ~"else");
|
2011-07-27 14:48:34 +02:00
|
|
|
// Synthesize a block here to act as the else block
|
|
|
|
// containing an if expression. Needed in order for the
|
|
|
|
// else scope to behave like a normal block scope. A tad
|
|
|
|
// ugly.
|
|
|
|
// Calling trans_block directly instead of trans_expr
|
|
|
|
// because trans_expr will create another scope block
|
|
|
|
// context for the block, but we've already got the
|
|
|
|
// 'else' context
|
|
|
|
let else_res =
|
2011-07-27 14:19:39 +02:00
|
|
|
alt els {
|
|
|
|
some(elexpr) {
|
|
|
|
alt elexpr.node {
|
|
|
|
ast::expr_if(_, _, _) {
|
2011-08-21 21:44:41 -07:00
|
|
|
let elseif_blk = ast_util::block_from_expr(elexpr);
|
2011-07-27 14:19:39 +02:00
|
|
|
trans_block(else_cx, elseif_blk, output)
|
|
|
|
}
|
|
|
|
ast::expr_block(blk) { trans_block(else_cx, blk, output) }
|
2011-03-27 18:51:25 -04:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ { rslt(else_cx, C_nil()) }
|
|
|
|
};
|
2011-08-30 09:59:30 +02:00
|
|
|
CondBr(cond_res.bcx, cond_res.val, then_cx.llbb, else_cx.llbb);
|
2011-08-19 15:16:48 -07:00
|
|
|
ret rslt(join_branches(cx, [then_res, else_res]), C_nil());
|
2010-10-04 15:55:12 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_for(cx: &@block_ctxt, local: &@ast::local, seq: &@ast::expr,
|
|
|
|
body: &ast::blk) -> result {
|
2011-07-06 19:00:00 -07:00
|
|
|
// FIXME: We bind to an alias here to avoid a segfault... this is
|
|
|
|
// obviously a bug.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn inner(cx: &@block_ctxt, local: @ast::local, curr: ValueRef, t: ty::t,
|
|
|
|
body: &ast::blk, outer_next_cx: @block_ctxt) -> result {
|
2011-08-26 21:34:56 -07:00
|
|
|
let next_cx = new_sub_block_ctxt(cx, ~"next");
|
2011-07-27 14:19:39 +02:00
|
|
|
let scope_cx =
|
2011-08-13 00:09:25 -07:00
|
|
|
new_loop_scope_block_ctxt(cx,
|
|
|
|
option::some::<@block_ctxt>(next_cx),
|
2011-08-26 21:34:56 -07:00
|
|
|
outer_next_cx, ~"for loop scope");
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(cx, scope_cx.llbb);
|
2011-07-27 14:19:39 +02:00
|
|
|
let local_res = alloc_local(scope_cx, local);
|
2011-08-22 12:38:58 +02:00
|
|
|
let bcx = copy_val(local_res.bcx, INIT, local_res.val, curr, t);
|
Make moving of temporaries do the right thing, use it to optimize
This adds support for dropping cleanups for temporary values when they
are moved somewhere else. It then adds wraps most copy operations
(return, put in data structure, box, etc) in a way that will fall back
to a move when it is safe.
This saves a lot of taking/dropping, shaving over a megabyte off the
stage2/rustc binary size.
In some cases, most notably function returns, we could detect that the
returned value is a local variable, and can thus be safely moved even
though it is not a temporary. This will require putting some more
information in lvals.
I did not yet handle function arguments, since the logic for passing
them looked too convoluted to touch. I'll probably try that in the
near future, since it's bound to be a big win.
2011-07-07 13:36:12 +02:00
|
|
|
add_clean(scope_cx, local_res.val, t);
|
2011-08-19 15:16:48 -07:00
|
|
|
let bcx =
|
2011-08-22 12:38:58 +02:00
|
|
|
trans_alt::bind_irrefutable_pat(bcx, local.node.pat,
|
2011-08-19 15:16:48 -07:00
|
|
|
local_res.val, cx.fcx.lllocals,
|
|
|
|
false);
|
2011-05-31 14:24:04 +02:00
|
|
|
bcx = trans_block(bcx, body, return).bcx;
|
2011-08-24 14:54:55 +02:00
|
|
|
if !is_terminated(bcx) {
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(bcx, next_cx.llbb);
|
2011-07-04 22:48:08 -07:00
|
|
|
// otherwise, this code is unreachable
|
|
|
|
}
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(next_cx, C_nil());
|
2011-01-21 07:59:06 -08:00
|
|
|
}
|
2011-08-26 21:34:56 -07:00
|
|
|
let next_cx = new_sub_block_ctxt(cx, ~"next");
|
2011-07-27 14:19:39 +02:00
|
|
|
let seq_ty = ty::expr_ty(bcx_tcx(cx), seq);
|
|
|
|
let seq_res = trans_expr(cx, seq);
|
|
|
|
let it =
|
2011-06-15 11:19:50 -07:00
|
|
|
iter_sequence(seq_res.bcx, seq_res.val, seq_ty,
|
2011-06-16 15:58:25 -07:00
|
|
|
bind inner(_, local, _, _, body, next_cx));
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(it.bcx, next_cx.llbb);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(next_cx, it.val);
|
2011-01-21 07:59:06 -08:00
|
|
|
}
|
|
|
|
|
2011-03-10 16:49:00 -08:00
|
|
|
|
|
|
|
// Iterator translation
|
|
|
|
|
2011-08-02 16:24:38 -07:00
|
|
|
// Given a block context and a list of tydescs and values to bind
|
|
|
|
// construct a closure out of them. If copying is true, it is a
|
|
|
|
// heap allocated closure that copies the upvars into environment.
|
|
|
|
// Otherwise, it is stack allocated and copies pointers to the upvars.
|
2011-08-04 16:20:09 -07:00
|
|
|
fn build_environment(bcx: @block_ctxt, lltydescs: [ValueRef],
|
|
|
|
bound_tys: [ty::t], bound_vals: [lval_result],
|
2011-08-19 15:16:48 -07:00
|
|
|
copying: bool) ->
|
|
|
|
{ptr: ValueRef, ptrty: ty::t, bcx: @block_ctxt} {
|
2011-07-28 16:54:06 -07:00
|
|
|
// Synthesize a closure type.
|
|
|
|
|
|
|
|
// First, synthesize a tuple type containing the types of all the
|
|
|
|
// bound expressions.
|
2011-08-19 14:34:45 +02:00
|
|
|
// bindings_ty = [bound_ty1, bound_ty2, ...]
|
2011-08-15 12:08:05 +02:00
|
|
|
let bindings_ty: ty::t = ty::mk_tup(bcx_tcx(bcx), bound_tys);
|
2011-07-28 16:54:06 -07:00
|
|
|
|
|
|
|
// NB: keep this in sync with T_closure_ptr; we're making
|
|
|
|
// a ty::t structure that has the same "shape" as the LLVM type
|
|
|
|
// it constructs.
|
|
|
|
|
|
|
|
// Make a vector that contains ty_param_count copies of tydesc_ty.
|
|
|
|
// (We'll need room for that many tydescs in the closure.)
|
2011-08-15 16:38:23 -07:00
|
|
|
let ty_param_count = std::vec::len(lltydescs);
|
2011-07-28 16:54:06 -07:00
|
|
|
let tydesc_ty: ty::t = ty::mk_type(bcx_tcx(bcx));
|
2011-08-19 15:16:48 -07:00
|
|
|
let captured_tys: [ty::t] = std::vec::init_elt(tydesc_ty, ty_param_count);
|
2011-07-28 16:54:06 -07:00
|
|
|
|
|
|
|
// Get all the types we've got (some of which we synthesized
|
|
|
|
// ourselves) into a vector. The whole things ends up looking
|
|
|
|
// like:
|
|
|
|
|
2011-07-29 17:15:19 -07:00
|
|
|
// closure_tys = [tydesc_ty, [bound_ty1, bound_ty2, ...], [tydesc_ty,
|
|
|
|
// tydesc_ty, ...]]
|
2011-08-04 16:20:09 -07:00
|
|
|
let closure_tys: [ty::t] =
|
2011-08-19 15:16:48 -07:00
|
|
|
[tydesc_ty, bindings_ty, ty::mk_tup(bcx_tcx(bcx), captured_tys)];
|
2011-07-28 16:54:06 -07:00
|
|
|
|
|
|
|
// Finally, synthesize a type for that whole vector.
|
2011-08-15 12:08:05 +02:00
|
|
|
let closure_ty: ty::t = ty::mk_tup(bcx_tcx(bcx), closure_tys);
|
2011-07-28 16:54:06 -07:00
|
|
|
|
|
|
|
// Allocate a box that can hold something closure-sized.
|
2011-08-19 15:16:48 -07:00
|
|
|
let r =
|
|
|
|
if copying {
|
|
|
|
trans_malloc_boxed(bcx, closure_ty)
|
|
|
|
} else {
|
|
|
|
// We need to dummy up a box on the stack
|
|
|
|
let ty =
|
|
|
|
ty::mk_tup(bcx_tcx(bcx),
|
|
|
|
[ty::mk_int(bcx_tcx(bcx)), closure_ty]);
|
|
|
|
let r = alloc_ty(bcx, ty);
|
|
|
|
let body = GEPi(bcx, r.val, [0, abi::box_rc_field_body]);
|
|
|
|
{bcx: r.bcx, box: r.val, body: body}
|
|
|
|
};
|
2011-07-28 16:54:06 -07:00
|
|
|
bcx = r.bcx;
|
|
|
|
let closure = r.body;
|
|
|
|
|
|
|
|
// Store bindings tydesc.
|
2011-08-02 16:24:38 -07:00
|
|
|
if copying {
|
2011-08-19 15:16:48 -07:00
|
|
|
let bound_tydesc = GEPi(bcx, closure, [0, abi::closure_elt_tydesc]);
|
2011-08-02 16:24:38 -07:00
|
|
|
let ti = none;
|
2011-08-24 18:36:51 -07:00
|
|
|
let bindings_tydesc =
|
|
|
|
get_tydesc(bcx, bindings_ty, true, tps_normal, ti).result;
|
2011-08-02 16:24:38 -07:00
|
|
|
lazily_emit_tydesc_glue(bcx, abi::tydesc_field_drop_glue, ti);
|
|
|
|
lazily_emit_tydesc_glue(bcx, abi::tydesc_field_free_glue, ti);
|
|
|
|
bcx = bindings_tydesc.bcx;
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(bcx, bindings_tydesc.val, bound_tydesc);
|
2011-08-02 16:24:38 -07:00
|
|
|
}
|
2011-07-28 16:54:06 -07:00
|
|
|
|
|
|
|
// Copy expr values into boxed bindings.
|
|
|
|
let i = 0u;
|
|
|
|
let bindings =
|
2011-07-29 17:15:19 -07:00
|
|
|
GEP_tup_like(bcx, closure_ty, closure,
|
2011-08-19 15:16:48 -07:00
|
|
|
[0, abi::closure_elt_bindings]);
|
2011-07-29 17:15:19 -07:00
|
|
|
bcx = bindings.bcx;
|
2011-08-15 21:54:52 -07:00
|
|
|
for lv: lval_result in bound_vals {
|
2011-08-19 15:16:48 -07:00
|
|
|
let bound =
|
|
|
|
GEP_tup_like(bcx, bindings_ty, bindings.val, [0, i as int]);
|
2011-08-02 16:24:38 -07:00
|
|
|
bcx = bound.bcx;
|
|
|
|
if copying {
|
2011-08-22 12:38:58 +02:00
|
|
|
bcx = move_val_if_temp(bcx, INIT, bound.val, lv, bound_tys[i]);
|
2011-08-30 09:59:30 +02:00
|
|
|
} else { Store(bcx, lv.res.val, bound.val); }
|
2011-07-28 16:54:06 -07:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If necessary, copy tydescs describing type parameters into the
|
|
|
|
// appropriate slot in the closure.
|
|
|
|
let ty_params_slot =
|
2011-07-29 17:15:19 -07:00
|
|
|
GEP_tup_like(bcx, closure_ty, closure,
|
2011-08-19 15:16:48 -07:00
|
|
|
[0, abi::closure_elt_ty_params]);
|
2011-07-29 17:15:19 -07:00
|
|
|
bcx = ty_params_slot.bcx;
|
2011-07-28 16:54:06 -07:00
|
|
|
i = 0u;
|
2011-08-15 21:54:52 -07:00
|
|
|
for td: ValueRef in lltydescs {
|
2011-08-19 15:16:48 -07:00
|
|
|
let ty_param_slot = GEPi(bcx, ty_params_slot.val, [0, i as int]);
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(bcx, td, ty_param_slot);
|
2011-07-28 16:54:06 -07:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret {ptr: r.box, ptrty: closure_ty, bcx: bcx};
|
2011-06-23 15:06:04 -07:00
|
|
|
}
|
|
|
|
|
2011-08-02 16:24:38 -07:00
|
|
|
// Given a context and a list of upvars, build a closure. This just
|
|
|
|
// collects the upvars and packages them up for build_environment.
|
2011-08-19 15:16:48 -07:00
|
|
|
fn build_closure(cx: &@block_ctxt, upvars: &@[ast::node_id], copying: bool) ->
|
|
|
|
{ptr: ValueRef, ptrty: ty::t, bcx: @block_ctxt} {
|
|
|
|
let closure_vals: [lval_result] = [];
|
|
|
|
let closure_tys: [ty::t] = [];
|
|
|
|
// If we need to, package up the iterator body to call
|
|
|
|
if !copying && !option::is_none(cx.fcx.lliterbody) {
|
|
|
|
closure_vals += [lval_mem(cx, option::get(cx.fcx.lliterbody))];
|
|
|
|
closure_tys += [option::get(cx.fcx.iterbodyty)];
|
|
|
|
}
|
|
|
|
// Package up the upvars
|
|
|
|
for nid: ast::node_id in *upvars {
|
|
|
|
closure_vals += [trans_var(cx, cx.sp, nid)];
|
|
|
|
let ty = ty::node_id_to_monotype(bcx_tcx(cx), nid);
|
|
|
|
if !copying { ty = ty::mk_mut_ptr(bcx_tcx(cx), ty); }
|
|
|
|
closure_tys += [ty];
|
|
|
|
}
|
2011-03-10 16:49:00 -08:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
ret build_environment(cx, cx.fcx.lltydescs, closure_tys, closure_vals,
|
|
|
|
copying);
|
2011-06-20 19:40:01 -07:00
|
|
|
}
|
|
|
|
|
2011-08-01 15:17:34 -07:00
|
|
|
// Return a pointer to the stored typarams in a closure.
|
|
|
|
// This is awful. Since the size of the bindings stored in the closure might
|
|
|
|
// be dynamically sized, we can't skip past them to get to the tydescs until
|
|
|
|
// we have loaded the tydescs. Thus we use the stored size of the bindings
|
|
|
|
// in the tydesc for the closure to skip over them. Ugh.
|
2011-08-22 12:45:18 +02:00
|
|
|
fn find_environment_tydescs(bcx: &@block_ctxt, envty: ty::t,
|
2011-08-01 15:17:34 -07:00
|
|
|
closure: ValueRef) -> ValueRef {
|
|
|
|
ret if !ty::type_has_dynamic_size(bcx_tcx(bcx), envty) {
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
// If we can find the typarams statically, do it
|
|
|
|
GEPi(bcx, closure,
|
|
|
|
[0, abi::box_rc_field_body, abi::closure_elt_ty_params])
|
|
|
|
} else {
|
|
|
|
// Ugh. We need to load the size of the bindings out of the
|
|
|
|
// closure's tydesc and use that to skip over the bindings.
|
|
|
|
let descsty =
|
|
|
|
ty::get_element_type(bcx_tcx(bcx), envty,
|
|
|
|
abi::closure_elt_ty_params as uint);
|
|
|
|
let llenv = GEPi(bcx, closure, [0, abi::box_rc_field_body]);
|
|
|
|
// Load the tydesc and find the size of the body
|
|
|
|
let lldesc =
|
2011-08-30 09:59:30 +02:00
|
|
|
Load(bcx, GEPi(bcx, llenv,
|
2011-08-19 15:16:48 -07:00
|
|
|
[0, abi::closure_elt_tydesc]));
|
|
|
|
let llsz =
|
2011-08-30 09:59:30 +02:00
|
|
|
Load(bcx, GEPi(bcx, lldesc,
|
2011-08-19 15:16:48 -07:00
|
|
|
[0, abi::tydesc_field_size]));
|
|
|
|
|
|
|
|
// Get the bindings pointer and add the size to it
|
|
|
|
let llbinds = GEPi(bcx, llenv, [0, abi::closure_elt_bindings]);
|
|
|
|
bump_ptr(bcx, descsty, llbinds, llsz)
|
|
|
|
}
|
2011-08-01 15:17:34 -07:00
|
|
|
}
|
|
|
|
|
2011-08-02 16:24:38 -07:00
|
|
|
// Given an enclosing block context, a new function context, a closure type,
|
|
|
|
// and a list of upvars, generate code to load and populate the environment
|
|
|
|
// with the upvars and type descriptors.
|
2011-08-22 12:45:18 +02:00
|
|
|
fn load_environment(enclosing_cx: &@block_ctxt, fcx: &@fn_ctxt, envty: ty::t,
|
2011-08-19 15:16:48 -07:00
|
|
|
upvars: &@[ast::node_id], copying: bool) {
|
2011-07-29 14:56:43 -07:00
|
|
|
let bcx = new_raw_block_ctxt(fcx, fcx.llcopyargs);
|
|
|
|
|
2011-07-29 17:15:19 -07:00
|
|
|
let ty = ty::mk_imm_box(bcx_tcx(bcx), envty);
|
|
|
|
let llty = type_of(bcx_ccx(bcx), bcx.sp, ty);
|
2011-08-30 09:59:30 +02:00
|
|
|
let llclosure = PointerCast(bcx, fcx.llenv, llty);
|
2011-07-29 17:15:19 -07:00
|
|
|
|
2011-08-01 15:17:34 -07:00
|
|
|
// Populate the type parameters from the environment. We need to
|
|
|
|
// do this first because the tydescs are needed to index into
|
|
|
|
// the bindings if they are dynamically sized.
|
2011-08-15 16:38:23 -07:00
|
|
|
let tydesc_count = std::vec::len(enclosing_cx.fcx.lltydescs);
|
2011-08-01 15:17:34 -07:00
|
|
|
let lltydescs = find_environment_tydescs(bcx, envty, llclosure);
|
|
|
|
let i = 0u;
|
|
|
|
while i < tydesc_count {
|
2011-08-19 15:16:48 -07:00
|
|
|
let lltydescptr = GEPi(bcx, lltydescs, [0, i as int]);
|
2011-08-30 09:59:30 +02:00
|
|
|
fcx.lltydescs += [Load(bcx, lltydescptr)];
|
2011-08-01 15:17:34 -07:00
|
|
|
i += 1u;
|
|
|
|
}
|
2011-07-29 14:56:43 -07:00
|
|
|
|
|
|
|
// Populate the upvars from the environment.
|
2011-08-19 15:16:48 -07:00
|
|
|
let path = [0, abi::box_rc_field_body, abi::closure_elt_bindings];
|
2011-08-01 15:17:34 -07:00
|
|
|
i = 0u;
|
2011-08-02 16:24:38 -07:00
|
|
|
// If this is an aliasing closure/for-each body, we need to load
|
|
|
|
// the iterbody.
|
|
|
|
if !copying && !option::is_none(enclosing_cx.fcx.lliterbody) {
|
2011-08-19 15:16:48 -07:00
|
|
|
let iterbodyptr = GEP_tup_like(bcx, ty, llclosure, path + [0]);
|
2011-08-30 09:59:30 +02:00
|
|
|
fcx.lliterbody = some(Load(bcx, iterbodyptr.val));
|
2011-08-02 16:24:38 -07:00
|
|
|
bcx = iterbodyptr.bcx;
|
2011-07-29 14:56:43 -07:00
|
|
|
i += 1u;
|
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
|
2011-08-02 16:24:38 -07:00
|
|
|
// Load the acutal upvars.
|
2011-08-15 21:54:52 -07:00
|
|
|
for upvar_id: ast::node_id in *upvars {
|
2011-08-19 15:16:48 -07:00
|
|
|
let upvarptr = GEP_tup_like(bcx, ty, llclosure, path + [i as int]);
|
2011-08-02 16:24:38 -07:00
|
|
|
bcx = upvarptr.bcx;
|
|
|
|
let llupvarptr = upvarptr.val;
|
2011-08-30 09:59:30 +02:00
|
|
|
if !copying { llupvarptr = Load(bcx, llupvarptr); }
|
2011-08-21 21:44:41 -07:00
|
|
|
let def_id = ast_util::def_id_of_def(bcx_tcx(bcx).
|
|
|
|
def_map.get(upvar_id));
|
2011-07-28 18:38:27 -07:00
|
|
|
fcx.llupvars.insert(def_id.node, llupvarptr);
|
2011-06-20 19:42:57 -07:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_for_each(cx: &@block_ctxt, local: &@ast::local, seq: &@ast::expr,
|
|
|
|
body: &ast::blk) -> result {
|
2011-06-20 19:40:01 -07:00
|
|
|
/*
|
|
|
|
* The translation is a little .. complex here. Code like:
|
|
|
|
*
|
|
|
|
* let ty1 p = ...;
|
|
|
|
*
|
|
|
|
* let ty1 q = ...;
|
|
|
|
*
|
|
|
|
* foreach (ty v in foo(a,b)) { body(p,q,v) }
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Turns into a something like so (C/Rust mishmash):
|
|
|
|
*
|
|
|
|
* type env = { *ty1 p, *ty2 q, ... };
|
|
|
|
*
|
|
|
|
* let env e = { &p, &q, ... };
|
|
|
|
*
|
|
|
|
* fn foreach123_body(env* e, ty v) { body(*(e->p),*(e->q),v) }
|
|
|
|
*
|
|
|
|
* foo([foreach123_body, env*], a, b);
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-07-18 19:14:01 -07:00
|
|
|
// Step 1: Generate code to build an environment containing pointers
|
|
|
|
// to all of the upvars
|
2011-07-27 14:19:39 +02:00
|
|
|
let lcx = cx.fcx.lcx;
|
2011-06-20 19:40:01 -07:00
|
|
|
|
|
|
|
// FIXME: possibly support alias-mode here?
|
2011-07-27 14:19:39 +02:00
|
|
|
let decl_ty = node_id_type(lcx.ccx, local.node.id);
|
|
|
|
let upvars = get_freevars(lcx.ccx.tcx, body.node.id);
|
2011-06-20 19:40:01 -07:00
|
|
|
|
2011-08-02 16:24:38 -07:00
|
|
|
let llenv = build_closure(cx, upvars, false);
|
2011-06-20 19:40:01 -07:00
|
|
|
|
|
|
|
// Step 2: Declare foreach body function.
|
2011-08-25 21:04:04 -07:00
|
|
|
let s: istr =
|
|
|
|
mangle_internal_name_by_path_and_seq(lcx.ccx,
|
2011-08-26 18:25:46 -07:00
|
|
|
lcx.path,
|
2011-08-25 21:04:04 -07:00
|
|
|
~"foreach");
|
2011-06-20 19:40:01 -07:00
|
|
|
|
2011-02-17 12:20:55 -08:00
|
|
|
// The 'env' arg entering the body function is a fake env member (as in
|
|
|
|
// the env-part of the normal rust calling convention) that actually
|
|
|
|
// points to a stack allocated env in this frame. We bundle that env
|
|
|
|
// pointer along with the foreach-body-fn pointer into a 'normal' fn pair
|
|
|
|
// and pass it in as a first class fn-arg to the iterator.
|
2011-07-27 14:19:39 +02:00
|
|
|
let iter_body_llty =
|
2011-08-02 16:24:38 -07:00
|
|
|
type_of_fn_from_ty(lcx.ccx, cx.sp,
|
|
|
|
ty::mk_iter_body_fn(lcx.ccx.tcx, decl_ty), 0u);
|
2011-07-27 14:19:39 +02:00
|
|
|
let lliterbody: ValueRef =
|
2011-08-25 21:04:04 -07:00
|
|
|
decl_internal_fastcall_fn(lcx.ccx.llmod,
|
2011-08-26 21:34:56 -07:00
|
|
|
s, iter_body_llty);
|
2011-08-02 15:13:08 -07:00
|
|
|
let fcx = new_fn_ctxt_w_id(lcx, cx.sp, lliterbody, body.node.id);
|
2011-08-02 16:24:38 -07:00
|
|
|
fcx.iterbodyty = cx.fcx.iterbodyty;
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-20 19:42:57 -07:00
|
|
|
// Generate code to load the environment out of the
|
|
|
|
// environment pointer.
|
2011-08-02 16:24:38 -07:00
|
|
|
load_environment(cx, fcx, llenv.ptrty, upvars, false);
|
2011-03-11 12:02:51 -08:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx = new_top_block_ctxt(fcx);
|
2011-07-28 12:01:45 +02:00
|
|
|
// Add bindings for the loop variable alias.
|
2011-08-19 15:16:48 -07:00
|
|
|
bcx =
|
|
|
|
trans_alt::bind_irrefutable_pat(bcx, local.node.pat,
|
|
|
|
llvm::LLVMGetParam(fcx.llfn, 3u),
|
|
|
|
bcx.fcx.lllocals, false);
|
2011-07-27 14:19:39 +02:00
|
|
|
let lltop = bcx.llbb;
|
|
|
|
let r = trans_block(bcx, body, return);
|
2011-05-11 11:56:49 -07:00
|
|
|
finish_fn(fcx, lltop);
|
2011-07-05 15:59:04 -07:00
|
|
|
|
2011-08-24 14:54:55 +02:00
|
|
|
if !is_terminated(r.bcx) {
|
2011-07-05 15:59:04 -07:00
|
|
|
// if terminated is true, no need for the ret-fail
|
2011-08-17 17:49:54 -07:00
|
|
|
build_return(r.bcx);
|
2011-07-05 15:59:04 -07:00
|
|
|
}
|
2011-02-17 12:20:55 -08:00
|
|
|
|
2011-02-18 17:30:57 -08:00
|
|
|
// Step 3: Call iter passing [lliterbody, llenv], plus other args.
|
2011-07-27 14:19:39 +02:00
|
|
|
alt seq.node {
|
|
|
|
ast::expr_call(f, args) {
|
|
|
|
let pair =
|
|
|
|
create_real_fn_pair(cx, iter_body_llty, lliterbody, llenv.ptr);
|
2011-08-19 11:54:46 -07:00
|
|
|
r = trans_call(cx, f, some(pair), args, seq.id);
|
2011-07-27 14:19:39 +02:00
|
|
|
ret rslt(r.bcx, C_nil());
|
|
|
|
}
|
2011-02-17 12:20:55 -08:00
|
|
|
}
|
2011-02-14 18:17:31 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_while(cx: &@block_ctxt, cond: &@ast::expr, body: &ast::blk) ->
|
2011-06-15 11:19:50 -07:00
|
|
|
result {
|
2011-08-26 21:34:56 -07:00
|
|
|
let next_cx = new_sub_block_ctxt(cx, ~"while next");
|
2011-08-23 14:51:22 -07:00
|
|
|
let cond_cx = new_loop_scope_block_ctxt(cx, option::none::<@block_ctxt>,
|
2011-08-26 21:34:56 -07:00
|
|
|
next_cx, ~"while cond");
|
2011-07-27 14:19:39 +02:00
|
|
|
let body_cx =
|
2011-08-26 21:34:56 -07:00
|
|
|
new_scope_block_ctxt(cond_cx, ~"while loop body");
|
2011-07-27 14:19:39 +02:00
|
|
|
let body_res = trans_block(body_cx, body, return);
|
|
|
|
let cond_res = trans_expr(cond_cx, cond);
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(body_res.bcx, cond_cx.llbb);
|
2011-07-27 14:19:39 +02:00
|
|
|
let cond_bcx = trans_block_cleanups(cond_res.bcx, cond_cx);
|
2011-08-30 09:59:30 +02:00
|
|
|
CondBr(cond_bcx, cond_res.val, body_cx.llbb, next_cx.llbb);
|
|
|
|
Br(cx, cond_cx.llbb);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(next_cx, C_nil());
|
2010-11-03 11:05:15 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_do_while(cx: &@block_ctxt, body: &ast::blk, cond: &@ast::expr) ->
|
2011-06-15 11:19:50 -07:00
|
|
|
result {
|
2011-08-26 21:34:56 -07:00
|
|
|
let next_cx = new_sub_block_ctxt(cx, ~"next");
|
2011-07-27 14:19:39 +02:00
|
|
|
let body_cx =
|
2011-08-13 00:09:25 -07:00
|
|
|
new_loop_scope_block_ctxt(cx, option::none::<@block_ctxt>, next_cx,
|
2011-08-26 21:34:56 -07:00
|
|
|
~"do-while loop body");
|
2011-07-27 14:19:39 +02:00
|
|
|
let body_res = trans_block(body_cx, body, return);
|
2011-08-11 14:14:05 -07:00
|
|
|
if is_terminated(body_res.bcx) {
|
|
|
|
// This is kind of ridiculous, but no permutations
|
|
|
|
// involving body_res or body_cx.val worked.
|
2011-08-12 16:15:03 -07:00
|
|
|
let rs = trans_block(cx, body, return);
|
2011-08-30 09:59:30 +02:00
|
|
|
if !is_terminated(next_cx) { Unreachable(next_cx); }
|
|
|
|
if !is_terminated(body_cx) { Unreachable(body_cx); }
|
2011-08-12 16:15:03 -07:00
|
|
|
ret rs;
|
2011-08-11 14:14:05 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let cond_res = trans_expr(body_res.bcx, cond);
|
2011-08-30 09:59:30 +02:00
|
|
|
CondBr(cond_res.bcx, cond_res.val, body_cx.llbb, next_cx.llbb);
|
|
|
|
Br(cx, body_cx.llbb);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(next_cx, body_res.val);
|
2010-11-03 11:05:15 -07:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
type generic_info =
|
2011-07-27 14:19:39 +02:00
|
|
|
{item_type: ty::t,
|
2011-08-12 07:15:18 -07:00
|
|
|
static_tis: [option::t<@tydesc_info>],
|
2011-08-04 16:20:09 -07:00
|
|
|
tydescs: [ValueRef]};
|
2011-01-14 14:17:57 -08:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
type lval_result =
|
2011-07-27 14:19:39 +02:00
|
|
|
{res: result,
|
|
|
|
is_mem: bool,
|
2011-08-12 07:15:18 -07:00
|
|
|
generic: option::t<generic_info>,
|
|
|
|
llobj: option::t<ValueRef>,
|
|
|
|
method_ty: option::t<ty::t>};
|
2011-07-27 14:19:39 +02:00
|
|
|
|
|
|
|
fn lval_mem(cx: &@block_ctxt, val: ValueRef) -> lval_result {
|
|
|
|
ret {res: rslt(cx, val),
|
|
|
|
is_mem: true,
|
2011-08-13 00:09:25 -07:00
|
|
|
generic: none::<generic_info>,
|
|
|
|
llobj: none::<ValueRef>,
|
|
|
|
method_ty: none::<ty::t>};
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lval_val(cx: &@block_ctxt, val: ValueRef) -> lval_result {
|
|
|
|
ret {res: rslt(cx, val),
|
|
|
|
is_mem: false,
|
2011-08-13 00:09:25 -07:00
|
|
|
generic: none::<generic_info>,
|
|
|
|
llobj: none::<ValueRef>,
|
|
|
|
method_ty: none::<ty::t>};
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_external_path(cx: &@block_ctxt, did: &ast::def_id,
|
2011-07-29 16:40:23 -07:00
|
|
|
tpt: &ty::ty_param_kinds_and_ty) -> ValueRef {
|
2011-07-27 14:19:39 +02:00
|
|
|
let lcx = cx.fcx.lcx;
|
|
|
|
let name = csearch::get_symbol(lcx.ccx.sess.get_cstore(), did);
|
2011-08-26 21:34:56 -07:00
|
|
|
ret get_extern_const(lcx.ccx.externs, lcx.ccx.llmod,
|
2011-08-26 22:48:10 -07:00
|
|
|
name,
|
2011-07-29 16:40:23 -07:00
|
|
|
type_of_ty_param_kinds_and_ty(lcx, cx.sp, tpt));
|
2011-03-30 18:15:29 -07:00
|
|
|
}
|
|
|
|
|
2011-07-29 16:40:23 -07:00
|
|
|
fn lval_generic_fn(cx: &@block_ctxt, tpt: &ty::ty_param_kinds_and_ty,
|
2011-07-27 14:19:39 +02:00
|
|
|
fn_id: &ast::def_id, id: ast::node_id) -> lval_result {
|
|
|
|
let lv;
|
|
|
|
if fn_id.crate == ast::local_crate {
|
2011-03-30 18:15:29 -07:00
|
|
|
// Internal reference.
|
2011-07-26 13:02:26 -07:00
|
|
|
assert (bcx_ccx(cx).fn_pairs.contains_key(fn_id.node));
|
|
|
|
lv = lval_val(cx, bcx_ccx(cx).fn_pairs.get(fn_id.node));
|
2011-03-30 18:15:29 -07:00
|
|
|
} else {
|
|
|
|
// External reference.
|
2011-07-25 14:58:59 +02:00
|
|
|
lv = lval_val(cx, trans_external_path(cx, fn_id, tpt));
|
2011-03-30 18:15:29 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let tys = ty::node_id_to_type_params(bcx_tcx(cx), id);
|
2011-08-13 00:09:25 -07:00
|
|
|
if std::vec::len::<ty::t>(tys) != 0u {
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx = lv.res.bcx;
|
2011-08-19 15:16:48 -07:00
|
|
|
let tydescs: [ValueRef] = [];
|
|
|
|
let tis: [option::t<@tydesc_info>] = [];
|
2011-08-15 21:54:52 -07:00
|
|
|
for t: ty::t in tys {
|
2011-04-29 14:59:52 -07:00
|
|
|
// TODO: Doesn't always escape.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-08-13 00:09:25 -07:00
|
|
|
let ti = none::<@tydesc_info>;
|
2011-08-24 18:36:51 -07:00
|
|
|
let td = get_tydesc(bcx, t, true, tps_normal, ti).result;
|
2011-08-19 15:16:48 -07:00
|
|
|
tis += [ti];
|
2011-01-31 18:06:35 -08:00
|
|
|
bcx = td.bcx;
|
2011-08-19 15:16:48 -07:00
|
|
|
tydescs += [td.val];
|
2011-01-31 18:06:35 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let gen = {item_type: tpt.ty, static_tis: tis, tydescs: tydescs};
|
2011-07-26 17:59:43 -07:00
|
|
|
lv = {res: rslt(bcx, lv.res.val), generic: some(gen) with lv};
|
2011-01-31 18:06:35 -08:00
|
|
|
}
|
|
|
|
ret lv;
|
|
|
|
}
|
|
|
|
|
2011-08-18 10:01:39 +02:00
|
|
|
fn lookup_discriminant(lcx: &@local_ctxt, vid: &ast::def_id) -> ValueRef {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt lcx.ccx.discrims.find(vid.node) {
|
|
|
|
none. {
|
|
|
|
// It's an external discriminant that we haven't seen yet.
|
|
|
|
assert (vid.crate != ast::local_crate);
|
|
|
|
let sym = csearch::get_symbol(lcx.ccx.sess.get_cstore(), vid);
|
2011-08-26 22:48:10 -07:00
|
|
|
let gvar = istr::as_buf(sym, { |buf|
|
2011-08-26 15:36:18 -07:00
|
|
|
llvm::LLVMAddGlobal(lcx.ccx.llmod, T_int(), buf)
|
|
|
|
});
|
2011-07-27 14:19:39 +02:00
|
|
|
llvm::LLVMSetLinkage(gvar,
|
|
|
|
lib::llvm::LLVMExternalLinkage as llvm::Linkage);
|
|
|
|
llvm::LLVMSetGlobalConstant(gvar, True);
|
|
|
|
lcx.ccx.discrims.insert(vid.node, gvar);
|
|
|
|
ret gvar;
|
|
|
|
}
|
|
|
|
some(llval) { ret llval; }
|
2011-04-07 11:42:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
fn trans_var(cx: &@block_ctxt, sp: &span, id: ast::node_id) -> lval_result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let ccx = bcx_ccx(cx);
|
2011-08-02 15:13:08 -07:00
|
|
|
alt freevars::def_lookup(bcx_tcx(cx), cx.fcx.id, id) {
|
|
|
|
some(ast::def_upvar(did, _)) {
|
|
|
|
assert (cx.fcx.llupvars.contains_key(did.node));
|
|
|
|
ret lval_mem(cx, cx.fcx.llupvars.get(did.node));
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
some(ast::def_arg(did)) {
|
2011-08-02 15:13:08 -07:00
|
|
|
assert (cx.fcx.llargs.contains_key(did.node));
|
|
|
|
ret lval_mem(cx, cx.fcx.llargs.get(did.node));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
some(ast::def_local(did)) {
|
2011-08-02 15:13:08 -07:00
|
|
|
assert (cx.fcx.lllocals.contains_key(did.node));
|
|
|
|
ret lval_mem(cx, cx.fcx.lllocals.get(did.node));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
some(ast::def_binding(did)) {
|
2011-08-02 15:13:08 -07:00
|
|
|
assert (cx.fcx.lllocals.contains_key(did.node));
|
|
|
|
ret lval_mem(cx, cx.fcx.lllocals.get(did.node));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
some(ast::def_obj_field(did)) {
|
|
|
|
assert (cx.fcx.llobjfields.contains_key(did.node));
|
|
|
|
ret lval_mem(cx, cx.fcx.llobjfields.get(did.node));
|
|
|
|
}
|
|
|
|
some(ast::def_fn(did, _)) {
|
|
|
|
let tyt = ty::lookup_item_type(ccx.tcx, did);
|
|
|
|
ret lval_generic_fn(cx, tyt, did, id);
|
|
|
|
}
|
|
|
|
some(ast::def_variant(tid, vid)) {
|
|
|
|
let v_tyt = ty::lookup_item_type(ccx.tcx, vid);
|
|
|
|
alt ty::struct(ccx.tcx, v_tyt.ty) {
|
|
|
|
ty::ty_fn(_, _, _, _, _) {
|
|
|
|
// N-ary variant.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
ret lval_generic_fn(cx, v_tyt, vid, id);
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
// Nullary variant.
|
|
|
|
let tag_ty = node_id_type(ccx, id);
|
|
|
|
let alloc_result = alloc_ty(cx, tag_ty);
|
|
|
|
let lltagblob = alloc_result.val;
|
2011-07-28 18:38:27 -07:00
|
|
|
let lltagty = type_of_tag(ccx, sp, tid, tag_ty);
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx = alloc_result.bcx;
|
2011-08-30 09:59:30 +02:00
|
|
|
let lltagptr = PointerCast(bcx, lltagblob, T_ptr(lltagty));
|
2011-08-15 16:38:23 -07:00
|
|
|
if std::vec::len(ty::tag_variants(ccx.tcx, tid)) != 1u {
|
2011-08-18 10:01:39 +02:00
|
|
|
let lldiscrim_gv = lookup_discriminant(bcx.fcx.lcx, vid);
|
2011-08-30 09:59:30 +02:00
|
|
|
let lldiscrim = Load(bcx, lldiscrim_gv);
|
2011-07-27 14:19:39 +02:00
|
|
|
let lldiscrimptr =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(bcx, lltagptr, [C_int(0), C_int(0)]);
|
|
|
|
Store(bcx, lldiscrim, lldiscrimptr);
|
2010-10-19 16:33:11 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ret lval_val(bcx, lltagptr);
|
2011-07-25 14:58:59 +02:00
|
|
|
}
|
2011-05-12 13:25:18 +02:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
some(ast::def_const(did)) {
|
|
|
|
if did.crate == ast::local_crate {
|
|
|
|
assert (ccx.consts.contains_key(did.node));
|
|
|
|
ret lval_mem(cx, ccx.consts.get(did.node));
|
|
|
|
} else {
|
|
|
|
let tp = ty::node_id_to_monotype(ccx.tcx, id);
|
2011-08-19 15:16:48 -07:00
|
|
|
let k: [ast::kind] = [];
|
2011-07-27 14:19:39 +02:00
|
|
|
ret lval_val(cx,
|
|
|
|
load_if_immediate(cx,
|
|
|
|
trans_external_path(cx, did,
|
2011-07-29 16:40:23 -07:00
|
|
|
{kinds: k,
|
2011-07-27 14:19:39 +02:00
|
|
|
ty: tp}),
|
|
|
|
tp));
|
2010-11-26 15:54:04 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
some(ast::def_native_fn(did)) {
|
|
|
|
let tyt = ty::lookup_item_type(ccx.tcx, did);
|
|
|
|
ret lval_generic_fn(cx, tyt, did, id);
|
|
|
|
}
|
2011-08-27 16:36:48 -07:00
|
|
|
_ { ccx.sess.span_unimpl(cx.sp, ~"def variant in trans"); }
|
2010-11-26 15:54:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-28 18:38:27 -07:00
|
|
|
fn trans_path(cx: &@block_ctxt, p: &ast::path, id: ast::node_id) ->
|
|
|
|
lval_result {
|
|
|
|
ret trans_var(cx, p.span, id);
|
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn trans_field(cx: &@block_ctxt, sp: &span, v: ValueRef, t0: ty::t,
|
2011-08-18 10:01:39 +02:00
|
|
|
field: &ast::ident) -> lval_result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let r = autoderef(cx, v, t0);
|
|
|
|
let t = r.ty;
|
|
|
|
alt ty::struct(bcx_tcx(cx), t) {
|
|
|
|
ty::ty_rec(fields) {
|
|
|
|
let ix: uint = ty::field_idx(bcx_ccx(cx).sess, sp, field, fields);
|
2011-08-19 15:16:48 -07:00
|
|
|
let v = GEP_tup_like(r.bcx, t, r.val, [0, ix as int]);
|
2011-07-27 14:19:39 +02:00
|
|
|
ret lval_mem(v.bcx, v.val);
|
|
|
|
}
|
|
|
|
ty::ty_obj(methods) {
|
|
|
|
let ix: uint = ty::method_idx(bcx_ccx(cx).sess, sp, field, methods);
|
|
|
|
let vtbl =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(r.bcx, r.val, [C_int(0), C_int(abi::obj_field_vtbl)]);
|
|
|
|
vtbl = Load(r.bcx, vtbl);
|
2011-07-27 14:19:39 +02:00
|
|
|
|
2011-07-29 13:36:57 +02:00
|
|
|
let vtbl_type = T_ptr(T_array(T_ptr(T_nil()), ix + 1u));
|
2011-08-30 09:59:30 +02:00
|
|
|
vtbl = PointerCast(cx, vtbl, vtbl_type);
|
2011-07-27 14:19:39 +02:00
|
|
|
|
2011-08-30 09:59:30 +02:00
|
|
|
let v = GEP(r.bcx, vtbl, [C_int(0), C_int(ix as int)]);
|
2011-08-19 15:16:48 -07:00
|
|
|
let fn_ty: ty::t = ty::method_ty_to_fn_ty(bcx_tcx(cx), methods[ix]);
|
2011-07-27 14:19:39 +02:00
|
|
|
let tcx = bcx_tcx(cx);
|
|
|
|
let ll_fn_ty =
|
|
|
|
type_of_fn_full(bcx_ccx(cx), sp, ty::ty_fn_proto(tcx, fn_ty),
|
|
|
|
true, ty::ty_fn_args(tcx, fn_ty),
|
|
|
|
ty::ty_fn_ret(tcx, fn_ty), 0u);
|
2011-08-30 09:59:30 +02:00
|
|
|
v = PointerCast(r.bcx, v, T_ptr(T_ptr(ll_fn_ty)));
|
2011-07-27 14:19:39 +02:00
|
|
|
let lvo = lval_mem(r.bcx, v);
|
2011-08-13 00:09:25 -07:00
|
|
|
ret {llobj: some::<ValueRef>(r.val), method_ty: some::<ty::t>(fn_ty)
|
2011-07-27 14:19:39 +02:00
|
|
|
with lvo};
|
|
|
|
}
|
2011-08-27 16:36:48 -07:00
|
|
|
_ { bcx_ccx(cx).sess.unimpl(~"field variant in trans_field"); }
|
2010-11-26 15:54:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_index(cx: &@block_ctxt, sp: &span, base: &@ast::expr,
|
|
|
|
idx: &@ast::expr, id: ast::node_id) -> lval_result {
|
2011-06-11 23:15:16 -07:00
|
|
|
// Is this an interior vector?
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let base_ty = ty::expr_ty(bcx_tcx(cx), base);
|
|
|
|
let exp = trans_expr(cx, base);
|
|
|
|
let lv = autoderef(exp.bcx, exp.val, base_ty);
|
|
|
|
let base_ty_no_boxes = lv.ty;
|
|
|
|
let is_interior = ty::sequence_is_interior(bcx_tcx(cx), base_ty_no_boxes);
|
|
|
|
let ix = trans_expr(lv.bcx, idx);
|
|
|
|
let v = lv.val;
|
|
|
|
let bcx = ix.bcx;
|
2011-03-07 15:43:55 -08:00
|
|
|
// Cast to an LLVM integer. Rust is less strict than LLVM in this regard.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let ix_val;
|
|
|
|
let ix_size = llsize_of_real(bcx_ccx(cx), val_ty(ix.val));
|
|
|
|
let int_size = llsize_of_real(bcx_ccx(cx), T_int());
|
|
|
|
if ix_size < int_size {
|
2011-08-30 09:59:30 +02:00
|
|
|
ix_val = ZExt(bcx, ix.val, T_int());
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if ix_size > int_size {
|
2011-08-30 09:59:30 +02:00
|
|
|
ix_val = Trunc(bcx, ix.val, T_int());
|
2011-06-15 11:19:50 -07:00
|
|
|
} else { ix_val = ix.val; }
|
2011-07-27 14:19:39 +02:00
|
|
|
let unit_ty = node_id_type(bcx_ccx(cx), id);
|
|
|
|
let unit_sz = size_of(bcx, unit_ty);
|
2011-01-31 15:03:05 -08:00
|
|
|
bcx = unit_sz.bcx;
|
2011-08-26 21:34:56 -07:00
|
|
|
maybe_name_value(bcx_ccx(cx), unit_sz.val, ~"unit_sz");
|
2011-08-30 09:59:30 +02:00
|
|
|
let scaled_ix = Mul(bcx, ix_val, unit_sz.val);
|
2011-08-26 21:34:56 -07:00
|
|
|
maybe_name_value(bcx_ccx(cx), scaled_ix, ~"scaled_ix");
|
2011-07-27 14:19:39 +02:00
|
|
|
let interior_len_and_data;
|
|
|
|
if is_interior {
|
2011-08-25 10:18:02 +02:00
|
|
|
let len = ivec::get_fill(bcx, v);
|
|
|
|
let data = ivec::get_dataptr(bcx, v, type_of_or_i8(bcx, unit_ty));
|
|
|
|
interior_len_and_data = some({len: len, data: data});
|
2011-06-15 11:19:50 -07:00
|
|
|
} else { interior_len_and_data = none; }
|
2011-07-27 14:19:39 +02:00
|
|
|
let lim;
|
|
|
|
alt interior_len_and_data {
|
|
|
|
some(lad) { lim = lad.len; }
|
|
|
|
none. {
|
2011-08-30 09:59:30 +02:00
|
|
|
lim = GEP(bcx, v, [C_int(0), C_int(abi::vec_elt_fill)]);
|
|
|
|
lim = Load(bcx, lim);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-06-11 23:15:16 -07:00
|
|
|
}
|
2011-08-30 09:59:30 +02:00
|
|
|
let bounds_check = ICmp(bcx, lib::llvm::LLVMIntULT, scaled_ix, lim);
|
2011-08-26 21:34:56 -07:00
|
|
|
let fail_cx = new_sub_block_ctxt(bcx, ~"fail");
|
|
|
|
let next_cx = new_sub_block_ctxt(bcx, ~"next");
|
2011-08-30 09:59:30 +02:00
|
|
|
CondBr(bcx, bounds_check, next_cx.llbb, fail_cx.llbb);
|
2010-12-10 16:10:35 -08:00
|
|
|
// fail: bad bounds check.
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
trans_fail(fail_cx, some::<span>(sp), ~"bounds check");
|
2011-07-27 14:19:39 +02:00
|
|
|
let body;
|
|
|
|
alt interior_len_and_data {
|
|
|
|
some(lad) { body = lad.data; }
|
|
|
|
none. {
|
|
|
|
body =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(next_cx, v,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::vec_elt_data), C_int(0)]);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-06-11 23:15:16 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let elt;
|
|
|
|
if ty::type_has_dynamic_size(bcx_tcx(cx), unit_ty) {
|
2011-08-30 09:59:30 +02:00
|
|
|
body = PointerCast(next_cx, body, T_ptr(T_i8()));
|
|
|
|
elt = GEP(next_cx, body, [scaled_ix]);
|
2011-03-31 10:10:21 -07:00
|
|
|
} else {
|
2011-08-30 09:59:30 +02:00
|
|
|
elt = GEP(next_cx, body, [ix_val]);
|
2011-04-07 18:00:29 -07:00
|
|
|
// We're crossing a box boundary here, so we may need to pointer cast.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let llunitty = type_of(bcx_ccx(next_cx), sp, unit_ty);
|
2011-08-30 09:59:30 +02:00
|
|
|
elt = PointerCast(next_cx, elt, T_ptr(llunitty));
|
2011-03-31 10:10:21 -07:00
|
|
|
}
|
2010-12-23 17:31:16 -08:00
|
|
|
ret lval_mem(next_cx, elt);
|
2010-12-10 16:10:35 -08:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2010-12-07 11:57:19 -08:00
|
|
|
// The additional bool returned indicates whether it's mem (that is
|
|
|
|
// represented as an alloca or heap, hence needs a 'load' to be used as an
|
|
|
|
// immediate).
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_lval_gen(cx: &@block_ctxt, e: &@ast::expr) -> lval_result {
|
|
|
|
alt e.node {
|
|
|
|
ast::expr_path(p) { ret trans_path(cx, p, e.id); }
|
|
|
|
ast::expr_field(base, ident) {
|
|
|
|
let r = trans_expr(cx, base);
|
|
|
|
let t = ty::expr_ty(bcx_tcx(cx), base);
|
2011-08-18 10:01:39 +02:00
|
|
|
ret trans_field(r.bcx, e.span, r.val, t, ident);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::expr_index(base, idx) {
|
|
|
|
ret trans_index(cx, e.span, base, idx, e.id);
|
|
|
|
}
|
|
|
|
ast::expr_unary(ast::deref., base) {
|
|
|
|
let ccx = bcx_ccx(cx);
|
|
|
|
let sub = trans_expr(cx, base);
|
|
|
|
let t = ty::expr_ty(ccx.tcx, base);
|
|
|
|
let val =
|
|
|
|
alt ty::struct(ccx.tcx, t) {
|
|
|
|
ty::ty_box(_) {
|
2011-08-30 09:59:30 +02:00
|
|
|
InBoundsGEP(sub.bcx, sub.val,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0),
|
|
|
|
C_int(abi::box_rc_field_body)])
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-10 17:23:46 -07:00
|
|
|
ty::ty_uniq(_) { fail "uniq lval translation unimplemented" }
|
2011-07-27 14:19:39 +02:00
|
|
|
ty::ty_res(_, _, _) {
|
2011-08-30 09:59:30 +02:00
|
|
|
InBoundsGEP(sub.bcx, sub.val, [C_int(0), C_int(1)])
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_tag(_, _) {
|
|
|
|
let ety = ty::expr_ty(ccx.tcx, e);
|
|
|
|
let ellty;
|
|
|
|
if ty::type_has_dynamic_size(ccx.tcx, ety) {
|
|
|
|
ellty = T_typaram_ptr(ccx.tn);
|
|
|
|
} else { ellty = T_ptr(type_of(ccx, e.span, ety)); }
|
2011-08-30 09:59:30 +02:00
|
|
|
PointerCast(sub.bcx, sub.val, ellty)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty::ty_ptr(_) { sub.val }
|
2011-06-28 16:38:14 +02:00
|
|
|
};
|
2011-07-27 14:19:39 +02:00
|
|
|
ret lval_mem(sub.bcx, val);
|
|
|
|
}
|
2011-08-16 12:38:42 -07:00
|
|
|
ast::expr_uniq(contents) { ret trans_uniq(cx, contents); }
|
2011-07-27 14:19:39 +02:00
|
|
|
ast::expr_self_method(ident) {
|
|
|
|
alt { cx.fcx.llself } {
|
|
|
|
some(pair) {
|
|
|
|
let r = pair.v;
|
|
|
|
let t = pair.t;
|
2011-08-18 10:01:39 +02:00
|
|
|
ret trans_field(cx, e.span, r, t, ident);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ {
|
|
|
|
// Shouldn't happen.
|
2011-08-27 16:36:48 -07:00
|
|
|
bcx_ccx(cx).sess.bug(~"trans_lval called on \
|
2011-07-22 23:56:02 -07:00
|
|
|
expr_self_method in \
|
|
|
|
a context without llself");
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-07-08 14:28:46 +02:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ {
|
|
|
|
ret {res: trans_expr(cx, e),
|
|
|
|
is_mem: false,
|
|
|
|
generic: none,
|
|
|
|
llobj: none,
|
|
|
|
method_ty: none};
|
|
|
|
}
|
2011-07-08 14:28:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_lval(cx: &@block_ctxt, e: &@ast::expr) -> lval_result {
|
|
|
|
let lv = trans_lval_gen(cx, e);
|
|
|
|
alt lv.generic {
|
|
|
|
some(gi) {
|
|
|
|
let t = ty::expr_ty(bcx_tcx(cx), e);
|
2011-08-15 16:38:23 -07:00
|
|
|
let n_args = std::vec::len(ty::ty_fn_args(bcx_tcx(cx), t));
|
2011-08-13 00:09:25 -07:00
|
|
|
let args = std::vec::init_elt(none::<@ast::expr>, n_args);
|
2011-07-27 14:19:39 +02:00
|
|
|
let bound = trans_bind_1(lv.res.bcx, e, lv, args, e.id);
|
|
|
|
ret lval_val(bound.bcx, bound.val);
|
|
|
|
}
|
|
|
|
none. { ret lv; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn int_cast(bcx: &@block_ctxt, lldsttype: TypeRef, llsrctype: TypeRef,
|
|
|
|
llsrc: ValueRef, signed: bool) -> ValueRef {
|
|
|
|
let srcsz = llvm::LLVMGetIntTypeWidth(llsrctype);
|
|
|
|
let dstsz = llvm::LLVMGetIntTypeWidth(lldsttype);
|
|
|
|
ret if dstsz == srcsz {
|
2011-08-30 09:59:30 +02:00
|
|
|
BitCast(bcx, llsrc, lldsttype)
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if srcsz > dstsz {
|
2011-08-30 09:59:30 +02:00
|
|
|
TruncOrBitCast(bcx, llsrc, lldsttype)
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if signed {
|
2011-08-30 09:59:30 +02:00
|
|
|
SExtOrBitCast(bcx, llsrc, lldsttype)
|
|
|
|
} else { ZExtOrBitCast(bcx, llsrc, lldsttype) };
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn float_cast(bcx: &@block_ctxt, lldsttype: TypeRef, llsrctype: TypeRef,
|
|
|
|
llsrc: ValueRef) -> ValueRef {
|
|
|
|
let srcsz = lib::llvm::float_width(llsrctype);
|
|
|
|
let dstsz = lib::llvm::float_width(lldsttype);
|
|
|
|
ret if dstsz > srcsz {
|
2011-08-30 09:59:30 +02:00
|
|
|
FPExt(bcx, llsrc, lldsttype)
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if srcsz > dstsz {
|
2011-08-30 09:59:30 +02:00
|
|
|
FPTrunc(bcx, llsrc, lldsttype)
|
2011-07-27 14:19:39 +02:00
|
|
|
} else { llsrc };
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_cast(cx: &@block_ctxt, e: &@ast::expr, id: ast::node_id) -> result {
|
|
|
|
let ccx = bcx_ccx(cx);
|
|
|
|
let e_res = trans_expr(cx, e);
|
|
|
|
let ll_t_in = val_ty(e_res.val);
|
|
|
|
let t_in = ty::expr_ty(ccx.tcx, e);
|
|
|
|
let t_out = node_id_type(ccx, id);
|
|
|
|
let ll_t_out = type_of(ccx, e.span, t_out);
|
2011-07-22 13:10:59 +02:00
|
|
|
|
|
|
|
tag kind { native_; integral; float; other; }
|
2011-07-27 14:19:39 +02:00
|
|
|
fn t_kind(tcx: &ty::ctxt, t: ty::t) -> kind {
|
|
|
|
ret if ty::type_is_fp(tcx, t) {
|
|
|
|
float
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if ty::type_is_native(tcx, t) {
|
2011-07-27 14:19:39 +02:00
|
|
|
native_
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if ty::type_is_integral(tcx, t) {
|
2011-07-27 14:19:39 +02:00
|
|
|
integral
|
|
|
|
} else { other };
|
|
|
|
}
|
|
|
|
let k_in = t_kind(ccx.tcx, t_in);
|
|
|
|
let k_out = t_kind(ccx.tcx, t_out);
|
|
|
|
let s_in = k_in == integral && ty::type_is_signed(ccx.tcx, t_in);
|
|
|
|
|
|
|
|
let newval =
|
|
|
|
alt {in: k_in, out: k_out} {
|
|
|
|
{in: integral., out: integral.} {
|
|
|
|
int_cast(e_res.bcx, ll_t_out, ll_t_in, e_res.val, s_in)
|
|
|
|
}
|
|
|
|
{in: float., out: float.} {
|
|
|
|
float_cast(e_res.bcx, ll_t_out, ll_t_in, e_res.val)
|
|
|
|
}
|
|
|
|
{in: integral., out: float.} {
|
|
|
|
if s_in {
|
2011-08-30 09:59:30 +02:00
|
|
|
SIToFP(e_res.bcx, e_res.val, ll_t_out)
|
|
|
|
} else { UIToFP(e_res.bcx, e_res.val, ll_t_out) }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
{in: float., out: integral.} {
|
|
|
|
if ty::type_is_signed(ccx.tcx, t_out) {
|
2011-08-30 09:59:30 +02:00
|
|
|
FPToSI(e_res.bcx, e_res.val, ll_t_out)
|
|
|
|
} else { FPToUI(e_res.bcx, e_res.val, ll_t_out) }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
{in: integral., out: native_.} {
|
2011-08-30 09:59:30 +02:00
|
|
|
IntToPtr(e_res.bcx, e_res.val, ll_t_out)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
{in: native_., out: integral.} {
|
2011-08-30 09:59:30 +02:00
|
|
|
PtrToInt(e_res.bcx, e_res.val, ll_t_out)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
{in: native_., out: native_.} {
|
2011-08-30 09:59:30 +02:00
|
|
|
PointerCast(e_res.bcx, e_res.val, ll_t_out)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-27 16:36:48 -07:00
|
|
|
_ { ccx.sess.bug(~"Translating unsupported cast.") }
|
2011-07-27 14:19:39 +02:00
|
|
|
};
|
2011-07-22 13:10:59 +02:00
|
|
|
ret rslt(e_res.bcx, newval);
|
2010-11-24 10:32:14 -08:00
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn trans_bind_thunk(cx: &@local_ctxt, sp: &span, incoming_fty: ty::t,
|
|
|
|
outgoing_fty: ty::t, args: &[option::t<@ast::expr>],
|
|
|
|
env_ty: ty::t, ty_param_count: uint,
|
2011-08-12 07:15:18 -07:00
|
|
|
target_fn: &option::t<ValueRef>) ->
|
2011-08-19 15:16:48 -07:00
|
|
|
{val: ValueRef, ty: TypeRef} {
|
2011-01-07 15:12:23 -08:00
|
|
|
|
2011-06-28 18:54:05 -07:00
|
|
|
// Here we're not necessarily constructing a thunk in the sense of
|
|
|
|
// "function with no arguments". The result of compiling 'bind f(foo,
|
|
|
|
// bar, baz)' would be a thunk that, when called, applies f to those
|
|
|
|
// arguments and returns the result. But we're stretching the meaning of
|
|
|
|
// the word "thunk" here to also mean the result of compiling, say, 'bind
|
|
|
|
// f(foo, _, baz)', or any other bind expression that binds f and leaves
|
|
|
|
// some (or all) of the arguments unbound.
|
|
|
|
|
|
|
|
// Here, 'incoming_fty' is the type of the entire bind expression, while
|
|
|
|
// 'outgoing_fty' is the type of the function that is having some of its
|
|
|
|
// arguments bound. If f is a function that takes three arguments of type
|
|
|
|
// int and returns int, and we're translating, say, 'bind f(3, _, 5)',
|
|
|
|
// then outgoing_fty is the type of f, which is (int, int, int) -> int,
|
|
|
|
// and incoming_fty is the type of 'bind f(3, _, 5)', which is int -> int.
|
|
|
|
|
|
|
|
// Once translated, the entire bind expression will be the call f(foo,
|
|
|
|
// bar, baz) wrapped in a (so-called) thunk that takes 'bar' as its
|
|
|
|
// argument and that has bindings of 'foo' to 3 and 'baz' to 5 and a
|
|
|
|
// pointer to 'f' all saved in its environment. So, our job is to
|
|
|
|
// construct and return that thunk.
|
|
|
|
|
|
|
|
// Give the thunk a name, type, and value.
|
2011-08-25 21:04:04 -07:00
|
|
|
let s: istr =
|
|
|
|
mangle_internal_name_by_path_and_seq(cx.ccx,
|
2011-08-26 18:25:46 -07:00
|
|
|
cx.path,
|
2011-08-25 21:04:04 -07:00
|
|
|
~"thunk");
|
2011-07-27 14:19:39 +02:00
|
|
|
let llthunk_ty: TypeRef =
|
2011-06-15 11:19:50 -07:00
|
|
|
get_pair_fn_ty(type_of(cx.ccx, sp, incoming_fty));
|
2011-07-27 14:19:39 +02:00
|
|
|
let llthunk: ValueRef =
|
2011-08-25 21:04:04 -07:00
|
|
|
decl_internal_fastcall_fn(cx.ccx.llmod,
|
2011-08-26 21:34:56 -07:00
|
|
|
s, llthunk_ty);
|
2011-06-28 18:54:05 -07:00
|
|
|
|
|
|
|
// Create a new function context and block context for the thunk, and hold
|
|
|
|
// onto a pointer to the first block in the function for later use.
|
2011-07-27 14:19:39 +02:00
|
|
|
let fcx = new_fn_ctxt(cx, sp, llthunk);
|
|
|
|
let bcx = new_top_block_ctxt(fcx);
|
|
|
|
let lltop = bcx.llbb;
|
2011-07-08 14:11:21 -05:00
|
|
|
// Since we might need to construct derived tydescs that depend on
|
|
|
|
// our bound tydescs, we need to load tydescs out of the environment
|
|
|
|
// before derived tydescs are constructed. To do this, we load them
|
|
|
|
// in the copy_args block.
|
2011-07-27 14:19:39 +02:00
|
|
|
let copy_args_bcx = new_raw_block_ctxt(fcx, fcx.llcopyargs);
|
2011-06-28 19:06:44 -07:00
|
|
|
|
|
|
|
// The 'llenv' that will arrive in the thunk we're creating is an
|
|
|
|
// environment that will contain the values of its arguments and a pointer
|
|
|
|
// to the original function. So, let's create one of those:
|
|
|
|
|
|
|
|
// The llenv pointer needs to be the correct size. That size is
|
|
|
|
// 'closure_ty', which was determined by trans_bind.
|
2011-07-29 17:15:19 -07:00
|
|
|
let closure_ty = ty::mk_imm_box(cx.ccx.tcx, env_ty);
|
|
|
|
let llclosure_ptr_ty = type_of(cx.ccx, sp, closure_ty);
|
2011-07-27 14:19:39 +02:00
|
|
|
let llclosure =
|
2011-08-30 09:59:30 +02:00
|
|
|
PointerCast(copy_args_bcx, fcx.llenv, llclosure_ptr_ty);
|
2011-06-28 18:54:05 -07:00
|
|
|
|
|
|
|
// "target", in this context, means the function that's having some of its
|
|
|
|
// arguments bound and that will be called inside the thunk we're
|
|
|
|
// creating. (In our running example, target is the function f.) Pick
|
2011-07-27 14:14:04 -07:00
|
|
|
// out the pointer to the target function from the environment. The
|
|
|
|
// target function lives in the first binding spot.
|
2011-08-19 15:16:48 -07:00
|
|
|
let (lltarget, starting_idx) =
|
|
|
|
alt target_fn {
|
|
|
|
some(lltarget) { (lltarget, 0) }
|
|
|
|
none. {
|
|
|
|
let lltarget =
|
|
|
|
GEP_tup_like(bcx, closure_ty, llclosure,
|
|
|
|
[0, abi::box_rc_field_body,
|
|
|
|
abi::closure_elt_bindings, 0]);
|
|
|
|
bcx = lltarget.bcx;;
|
|
|
|
(lltarget.val, 1)
|
|
|
|
}
|
|
|
|
};
|
2011-06-28 18:54:05 -07:00
|
|
|
|
|
|
|
// And then, pick out the target function's own environment. That's what
|
|
|
|
// we'll use as the environment the thunk gets.
|
2011-07-27 14:19:39 +02:00
|
|
|
let lltargetclosure =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(bcx, lltarget, [C_int(0), C_int(abi::fn_field_box)]);
|
|
|
|
lltargetclosure = Load(bcx, lltargetclosure);
|
2011-06-28 18:54:05 -07:00
|
|
|
|
|
|
|
// Get f's return type, which will also be the return type of the entire
|
|
|
|
// bind expression.
|
2011-07-27 14:19:39 +02:00
|
|
|
let outgoing_ret_ty = ty::ty_fn_ret(cx.ccx.tcx, outgoing_fty);
|
2011-06-28 18:54:05 -07:00
|
|
|
|
|
|
|
// Get the types of the arguments to f.
|
2011-07-27 14:19:39 +02:00
|
|
|
let outgoing_args = ty::ty_fn_args(cx.ccx.tcx, outgoing_fty);
|
2011-06-28 18:54:05 -07:00
|
|
|
|
|
|
|
// The 'llretptr' that will arrive in the thunk we're creating also needs
|
2011-08-03 13:11:02 -07:00
|
|
|
// to be the correct type. Cast it to f's return type, if necessary.
|
2011-07-27 14:19:39 +02:00
|
|
|
let llretptr = fcx.llretptr;
|
2011-08-03 13:11:02 -07:00
|
|
|
if ty::type_contains_params(cx.ccx.tcx, outgoing_ret_ty) {
|
|
|
|
let llretty = type_of_inner(cx.ccx, sp, outgoing_ret_ty);
|
2011-08-30 09:59:30 +02:00
|
|
|
llretptr = PointerCast(bcx, llretptr, T_ptr(llretty));
|
2011-02-23 10:58:43 -08:00
|
|
|
}
|
2011-06-28 18:54:05 -07:00
|
|
|
|
|
|
|
// Set up the three implicit arguments to the thunk.
|
2011-08-19 15:16:48 -07:00
|
|
|
let llargs: [ValueRef] = [llretptr, fcx.lltaskptr, lltargetclosure];
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-28 18:54:05 -07:00
|
|
|
// Copy in the type parameters.
|
2011-07-27 14:19:39 +02:00
|
|
|
let i: uint = 0u;
|
|
|
|
while i < ty_param_count {
|
|
|
|
let lltyparam_ptr =
|
2011-07-08 14:11:21 -05:00
|
|
|
GEP_tup_like(copy_args_bcx, closure_ty, llclosure,
|
2011-08-19 15:16:48 -07:00
|
|
|
[0, abi::box_rc_field_body,
|
|
|
|
abi::closure_elt_ty_params, i as int]);
|
2011-07-08 14:11:21 -05:00
|
|
|
copy_args_bcx = lltyparam_ptr.bcx;
|
2011-08-30 09:59:30 +02:00
|
|
|
let td = Load(copy_args_bcx, lltyparam_ptr.val);
|
2011-08-19 15:16:48 -07:00
|
|
|
llargs += [td];
|
|
|
|
fcx.lltydescs += [td];
|
2011-02-23 10:58:43 -08:00
|
|
|
i += 1u;
|
|
|
|
}
|
2011-06-28 18:54:05 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let a: uint = 3u; // retptr, task ptr, env come first
|
2011-08-15 18:18:19 -07:00
|
|
|
let b: int = starting_idx;
|
2011-07-27 14:19:39 +02:00
|
|
|
let outgoing_arg_index: uint = 0u;
|
2011-08-04 16:20:09 -07:00
|
|
|
let llout_arg_tys: [TypeRef] =
|
2011-05-18 15:35:16 -07:00
|
|
|
type_of_explicit_args(cx.ccx, sp, outgoing_args);
|
2011-08-12 07:15:18 -07:00
|
|
|
for arg: option::t<@ast::expr> in args {
|
2011-08-19 15:16:48 -07:00
|
|
|
let out_arg = outgoing_args[outgoing_arg_index];
|
|
|
|
let llout_arg_ty = llout_arg_tys[outgoing_arg_index];
|
2011-07-27 15:43:56 -07:00
|
|
|
let is_val = out_arg.mode == ty::mo_val;
|
2011-07-27 14:19:39 +02:00
|
|
|
alt arg {
|
2011-08-19 15:16:48 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
// Arg provided at binding time; thunk copies it from
|
|
|
|
// closure.
|
|
|
|
some(e) {
|
|
|
|
let e_ty = ty::expr_ty(cx.ccx.tcx, e);
|
|
|
|
let bound_arg =
|
|
|
|
GEP_tup_like(bcx, closure_ty, llclosure,
|
2011-08-19 15:16:48 -07:00
|
|
|
[0, abi::box_rc_field_body,
|
|
|
|
abi::closure_elt_bindings, b]);
|
2011-07-27 14:19:39 +02:00
|
|
|
bcx = bound_arg.bcx;
|
|
|
|
let val = bound_arg.val;
|
2011-07-27 15:43:56 -07:00
|
|
|
// If the type is parameterized, then we need to cast the
|
|
|
|
// type we actually have to the parameterized out type.
|
|
|
|
if ty::type_contains_params(cx.ccx.tcx, out_arg.ty) {
|
2011-08-19 15:16:48 -07:00
|
|
|
let ty =
|
|
|
|
if is_val { T_ptr(llout_arg_ty) } else { llout_arg_ty };
|
2011-08-30 09:59:30 +02:00
|
|
|
val = PointerCast(bcx, val, ty);
|
2011-07-27 15:43:56 -07:00
|
|
|
}
|
2011-08-19 14:34:45 +02:00
|
|
|
if is_val && type_is_immediate(cx.ccx, e_ty) {
|
2011-08-30 09:59:30 +02:00
|
|
|
val = Load(bcx, val);
|
2011-01-07 15:12:23 -08:00
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
llargs += [val];
|
2011-07-27 14:19:39 +02:00
|
|
|
b += 1;
|
|
|
|
}
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
// Arg will be provided when the thunk is invoked.
|
|
|
|
none. {
|
2011-07-27 18:39:57 -07:00
|
|
|
let arg: ValueRef = llvm::LLVMGetParam(llthunk, a);
|
2011-07-27 14:19:39 +02:00
|
|
|
if ty::type_contains_params(cx.ccx.tcx, out_arg.ty) {
|
2011-08-30 09:59:30 +02:00
|
|
|
arg = PointerCast(bcx, arg, llout_arg_ty);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
llargs += [arg];
|
2011-07-27 14:19:39 +02:00
|
|
|
a += 1u;
|
|
|
|
}
|
2011-01-07 15:12:23 -08:00
|
|
|
}
|
2011-03-10 15:09:41 -08:00
|
|
|
outgoing_arg_index += 1u;
|
2011-01-07 15:12:23 -08:00
|
|
|
}
|
2011-08-15 17:40:08 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let lltargetfn =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(bcx, lltarget, [C_int(0), C_int(abi::fn_field_code)]);
|
2011-06-28 18:54:05 -07:00
|
|
|
|
2011-07-26 18:14:36 -07:00
|
|
|
// Cast the outgoing function to the appropriate type.
|
|
|
|
// This is necessary because the type of the function that we have
|
|
|
|
// in the closure does not know how many type descriptors the function
|
|
|
|
// needs to take.
|
2011-07-27 14:19:39 +02:00
|
|
|
let lltargetty =
|
2011-07-26 17:59:43 -07:00
|
|
|
type_of_fn_from_ty(bcx_ccx(bcx), sp, outgoing_fty, ty_param_count);
|
2011-08-30 09:59:30 +02:00
|
|
|
lltargetfn = PointerCast(bcx, lltargetfn, T_ptr(T_ptr(lltargetty)));
|
|
|
|
lltargetfn = Load(bcx, lltargetfn);
|
|
|
|
FastCall(bcx, lltargetfn, llargs);
|
2011-08-17 17:49:54 -07:00
|
|
|
build_return(bcx);
|
2011-05-11 11:56:49 -07:00
|
|
|
finish_fn(fcx, lltop);
|
2011-07-26 16:03:46 -07:00
|
|
|
ret {val: llthunk, ty: llthunk_ty};
|
2011-01-07 15:12:23 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_bind(cx: &@block_ctxt, f: &@ast::expr,
|
2011-08-12 07:15:18 -07:00
|
|
|
args: &[option::t<@ast::expr>], id: ast::node_id) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let f_res = trans_lval_gen(cx, f);
|
2011-07-08 14:28:46 +02:00
|
|
|
ret trans_bind_1(cx, f, f_res, args, id);
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_bind_1(cx: &@block_ctxt, f: &@ast::expr, f_res: &lval_result,
|
2011-08-19 15:16:48 -07:00
|
|
|
args: &[option::t<@ast::expr>], id: ast::node_id) -> result {
|
|
|
|
let bound: [@ast::expr] = [];
|
2011-08-12 07:15:18 -07:00
|
|
|
for argopt: option::t<@ast::expr> in args {
|
2011-08-19 15:16:48 -07:00
|
|
|
alt argopt { none. { } some(e) { bound += [e]; } }
|
2011-07-26 14:47:44 -07:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-26 14:47:44 -07:00
|
|
|
// Figure out which tydescs we need to pass, if any.
|
2011-07-27 14:14:04 -07:00
|
|
|
let outgoing_fty: ty::t = ty::expr_ty(bcx_tcx(cx), f);
|
|
|
|
let outgoing_fty_real; // the type with typarams still in it
|
2011-08-04 16:20:09 -07:00
|
|
|
let lltydescs: [ValueRef];
|
2011-07-27 14:19:39 +02:00
|
|
|
alt f_res.generic {
|
2011-08-19 15:16:48 -07:00
|
|
|
none. { outgoing_fty_real = outgoing_fty; lltydescs = []; }
|
2011-07-27 14:19:39 +02:00
|
|
|
some(ginfo) {
|
2011-07-26 14:47:44 -07:00
|
|
|
lazily_emit_all_generic_info_tydesc_glues(cx, ginfo);
|
2011-07-27 14:14:04 -07:00
|
|
|
outgoing_fty_real = ginfo.item_type;
|
2011-07-26 14:47:44 -07:00
|
|
|
lltydescs = ginfo.tydescs;
|
|
|
|
}
|
|
|
|
}
|
2011-06-28 18:54:05 -07:00
|
|
|
|
2011-08-15 16:38:23 -07:00
|
|
|
let ty_param_count = std::vec::len(lltydescs);
|
|
|
|
if std::vec::len(bound) == 0u && ty_param_count == 0u {
|
2011-07-26 14:47:44 -07:00
|
|
|
// Trivial 'binding': just return the static pair-ptr.
|
|
|
|
ret f_res.res;
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx = f_res.res.bcx;
|
2011-07-26 14:47:44 -07:00
|
|
|
|
2011-07-27 14:14:04 -07:00
|
|
|
|
2011-08-15 18:18:19 -07:00
|
|
|
// Arrange for the bound function to live in the first binding spot
|
|
|
|
// if the function is not statically known.
|
2011-08-19 15:16:48 -07:00
|
|
|
let (bound_tys, bound_vals, target_res) =
|
|
|
|
if f_res.is_mem {
|
|
|
|
// Cast the function we are binding to be the type that the
|
|
|
|
// closure will expect it to have. The type the closure knows
|
|
|
|
// about has the type parameters substituted with the real types.
|
|
|
|
let llclosurety =
|
|
|
|
T_ptr(type_of(bcx_ccx(cx), cx.sp, outgoing_fty));
|
2011-08-30 09:59:30 +02:00
|
|
|
let src_loc = PointerCast(bcx, f_res.res.val, llclosurety);
|
2011-08-19 15:16:48 -07:00
|
|
|
let bound_f = {res: {bcx: bcx, val: src_loc} with f_res};
|
|
|
|
([outgoing_fty], [bound_f], none)
|
|
|
|
} else { ([], [], some(f_res.res.val)) };
|
2011-08-15 18:18:19 -07:00
|
|
|
|
2011-07-26 14:47:44 -07:00
|
|
|
// Translate the bound expressions.
|
2011-08-15 21:54:52 -07:00
|
|
|
for e: @ast::expr in bound {
|
2011-07-27 14:19:39 +02:00
|
|
|
let lv = trans_lval(bcx, e);
|
2011-07-26 14:47:44 -07:00
|
|
|
bcx = lv.res.bcx;
|
2011-08-19 15:16:48 -07:00
|
|
|
bound_vals += [lv];
|
|
|
|
bound_tys += [ty::expr_ty(bcx_tcx(cx), e)];
|
2011-07-26 14:47:44 -07:00
|
|
|
}
|
2011-06-28 18:54:05 -07:00
|
|
|
|
2011-07-28 16:54:06 -07:00
|
|
|
// Actually construct the closure
|
2011-08-19 15:16:48 -07:00
|
|
|
let closure =
|
|
|
|
build_environment(bcx, lltydescs, bound_tys, bound_vals, true);
|
2011-07-28 16:54:06 -07:00
|
|
|
bcx = closure.bcx;
|
2011-07-26 14:47:44 -07:00
|
|
|
|
2011-07-26 16:03:46 -07:00
|
|
|
// Make thunk
|
2011-07-26 14:47:44 -07:00
|
|
|
// The type of the entire bind expression.
|
2011-07-26 16:03:46 -07:00
|
|
|
let pair_ty = node_id_type(bcx_ccx(cx), id);
|
|
|
|
let llthunk =
|
2011-08-19 15:16:48 -07:00
|
|
|
trans_bind_thunk(cx.fcx.lcx, cx.sp, pair_ty, outgoing_fty_real, args,
|
|
|
|
closure.ptrty, ty_param_count, target_res);
|
2011-07-26 14:47:44 -07:00
|
|
|
|
2011-07-26 16:03:46 -07:00
|
|
|
// Construct the function pair
|
2011-08-19 15:16:48 -07:00
|
|
|
let pair_v =
|
|
|
|
create_real_fn_pair(bcx, llthunk.ty, llthunk.val, closure.ptr);
|
2011-07-26 14:47:44 -07:00
|
|
|
add_clean_temp(cx, pair_v, pair_ty);
|
|
|
|
ret rslt(bcx, pair_v);
|
2011-01-03 22:39:43 -08:00
|
|
|
}
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
fn trans_arg_expr(cx: &@block_ctxt, arg: &ty::arg, lldestty0: TypeRef,
|
|
|
|
to_zero: &mutable [{v: ValueRef, t: ty::t}],
|
|
|
|
to_revoke: &mutable [ValueRef], e: &@ast::expr) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let ccx = bcx_ccx(cx);
|
|
|
|
let e_ty = ty::expr_ty(ccx.tcx, e);
|
|
|
|
let is_bot = ty::type_is_bot(ccx.tcx, e_ty);
|
|
|
|
let lv = trans_lval(cx, e);
|
|
|
|
let bcx = lv.res.bcx;
|
|
|
|
let val = lv.res.val;
|
|
|
|
if is_bot {
|
2011-06-02 17:50:37 -07:00
|
|
|
// For values of type _|_, we generate an
|
|
|
|
// "undef" value, as such a value should never
|
|
|
|
// be inspected. It's important for the value
|
|
|
|
// to have type lldestty0 (the callee's expected type).
|
|
|
|
val = llvm::LLVMGetUndef(lldestty0);
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if arg.mode == ty::mo_val {
|
2011-08-19 14:34:45 +02:00
|
|
|
// Eliding take/drop for appending of external vectors currently
|
|
|
|
// corrupts memory. I can't figure out why, and external vectors
|
|
|
|
// are on the way out anyway, so this simply turns off the
|
|
|
|
// optimization for that case.
|
|
|
|
let is_ext_vec_plus = alt e.node {
|
|
|
|
ast::expr_binary(_, _, _) {
|
|
|
|
ty::type_is_sequence(ccx.tcx, e_ty) &&
|
|
|
|
!ty::sequence_is_interior(ccx.tcx, e_ty)
|
|
|
|
}
|
|
|
|
_ { false }
|
|
|
|
};
|
|
|
|
if !lv.is_mem && !is_ext_vec_plus {
|
|
|
|
// Do nothing for temporaries, just give them to callee
|
2011-08-22 13:19:07 +02:00
|
|
|
} else if type_is_structural_or_param(ccx.tcx, e_ty) {
|
2011-07-27 14:19:39 +02:00
|
|
|
let dst = alloc_ty(bcx, e_ty);
|
2011-08-22 12:38:58 +02:00
|
|
|
bcx = copy_val(dst.bcx, INIT, dst.val, val, e_ty);
|
2011-07-07 16:14:00 +02:00
|
|
|
val = dst.val;
|
2011-08-19 14:34:45 +02:00
|
|
|
add_clean_temp(bcx, val, e_ty);
|
2011-07-08 11:02:26 +02:00
|
|
|
} else {
|
2011-08-25 10:18:02 +02:00
|
|
|
if ty::type_is_ivec(ccx.tcx, e_ty) {
|
2011-08-30 09:59:30 +02:00
|
|
|
let arg_copy = do_spill(bcx, Load(bcx, val));
|
2011-08-25 10:18:02 +02:00
|
|
|
bcx = take_ty(bcx, arg_copy, e_ty).bcx;
|
2011-08-30 09:59:30 +02:00
|
|
|
val = Load(bcx, arg_copy);
|
2011-08-25 10:18:02 +02:00
|
|
|
} else if lv.is_mem {
|
2011-08-26 11:20:10 +02:00
|
|
|
bcx = take_ty(bcx, val, e_ty).bcx;
|
2011-08-19 14:34:45 +02:00
|
|
|
val = load_if_immediate(bcx, val, e_ty);
|
2011-08-26 11:20:10 +02:00
|
|
|
} else if is_ext_vec_plus {
|
|
|
|
let spilled = do_spill(bcx, val);
|
|
|
|
bcx = take_ty(bcx, spilled, e_ty).bcx;
|
2011-08-19 14:34:45 +02:00
|
|
|
}
|
|
|
|
add_clean_temp(bcx, val, e_ty);
|
2011-07-07 16:14:00 +02:00
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if type_is_immediate(ccx, e_ty) && !lv.is_mem {
|
2011-07-07 16:14:00 +02:00
|
|
|
val = do_spill(bcx, val);
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
if !is_bot && ty::type_contains_params(ccx.tcx, arg.ty) {
|
|
|
|
let lldestty = lldestty0;
|
2011-08-30 09:59:30 +02:00
|
|
|
val = PointerCast(bcx, val, lldestty);
|
2011-04-23 14:17:44 -07:00
|
|
|
}
|
2011-08-09 17:56:26 -07:00
|
|
|
|
|
|
|
// Collect arg for later if it happens to be one we've moving out.
|
|
|
|
if arg.mode == ty::mo_move {
|
|
|
|
if lv.is_mem {
|
2011-08-19 15:16:48 -07:00
|
|
|
// Use actual ty, not declared ty -- anything else doesn't make
|
|
|
|
// sense if declared ty is a ty param
|
|
|
|
to_zero += [{v: lv.res.val, t: e_ty}];
|
|
|
|
} else { to_revoke += [lv.res.val]; }
|
2011-08-09 17:56:26 -07:00
|
|
|
}
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(bcx, val);
|
2011-04-23 14:17:44 -07:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-02-08 11:47:53 -08:00
|
|
|
// NB: must keep 4 fns in sync:
|
|
|
|
//
|
|
|
|
// - type_of_fn_full
|
|
|
|
// - create_llargs_for_fn_args.
|
|
|
|
// - new_fn_ctxt
|
|
|
|
// - trans_args
|
2011-08-04 11:30:56 -07:00
|
|
|
fn trans_args(cx: &@block_ctxt, llenv: ValueRef,
|
2011-08-12 07:15:18 -07:00
|
|
|
gen: &option::t<generic_info>, lliterbody: &option::t<ValueRef>,
|
2011-08-22 12:45:18 +02:00
|
|
|
es: &[@ast::expr], fn_ty: ty::t) ->
|
2011-08-09 17:56:26 -07:00
|
|
|
{bcx: @block_ctxt,
|
|
|
|
args: [ValueRef],
|
|
|
|
retslot: ValueRef,
|
2011-08-19 15:16:48 -07:00
|
|
|
to_zero: [{v: ValueRef, t: ty::t}],
|
|
|
|
to_revoke: [ValueRef]} {
|
2011-08-09 17:56:26 -07:00
|
|
|
|
2011-08-04 16:20:09 -07:00
|
|
|
let args: [ty::arg] = ty::ty_fn_args(bcx_tcx(cx), fn_ty);
|
2011-08-19 15:16:48 -07:00
|
|
|
let llargs: [ValueRef] = [];
|
|
|
|
let lltydescs: [ValueRef] = [];
|
|
|
|
let to_zero = [];
|
|
|
|
let to_revoke = [];
|
2011-08-09 17:56:26 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx: @block_ctxt = cx;
|
2011-02-08 11:47:53 -08:00
|
|
|
// Arg 0: Output pointer.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-03 10:39:07 -07:00
|
|
|
// FIXME: test case looks like
|
|
|
|
// f(1, fail, @42);
|
2011-08-24 14:54:55 +02:00
|
|
|
if is_terminated(bcx) {
|
2011-07-03 10:39:07 -07:00
|
|
|
// This means an earlier arg was divergent.
|
|
|
|
// So this arg can't be evaluated.
|
2011-08-19 15:16:48 -07:00
|
|
|
ret {bcx: bcx,
|
|
|
|
args: [],
|
|
|
|
retslot: C_nil(),
|
|
|
|
to_zero: to_zero,
|
|
|
|
to_revoke: to_revoke};
|
2011-07-03 10:39:07 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let retty = ty::ty_fn_ret(bcx_tcx(cx), fn_ty);
|
|
|
|
let llretslot_res = alloc_ty(bcx, retty);
|
2011-02-10 15:00:16 -08:00
|
|
|
bcx = llretslot_res.bcx;
|
2011-07-27 14:19:39 +02:00
|
|
|
let llretslot = llretslot_res.val;
|
|
|
|
alt gen {
|
|
|
|
some(g) {
|
|
|
|
lazily_emit_all_generic_info_tydesc_glues(cx, g);
|
|
|
|
lltydescs = g.tydescs;
|
|
|
|
args = ty::ty_fn_args(bcx_tcx(cx), g.item_type);
|
|
|
|
retty = ty::ty_fn_ret(bcx_tcx(cx), g.item_type);
|
|
|
|
}
|
|
|
|
_ { }
|
2011-02-08 11:47:53 -08:00
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
if ty::type_contains_params(bcx_tcx(cx), retty) {
|
2011-02-08 17:05:53 -08:00
|
|
|
// It's possible that the callee has some generic-ness somewhere in
|
|
|
|
// its return value -- say a method signature within an obj or a fn
|
|
|
|
// type deep in a structure -- which the caller has a concrete view
|
|
|
|
// of. If so, cast the caller's view of the restlot to the callee's
|
|
|
|
// view, for the sake of making a type-compatible call.
|
2011-08-03 13:11:02 -07:00
|
|
|
let llretty = T_ptr(type_of_inner(bcx_ccx(bcx), bcx.sp, retty));
|
2011-08-30 09:59:30 +02:00
|
|
|
llargs += [PointerCast(cx, llretslot, llretty)];
|
2011-08-19 15:16:48 -07:00
|
|
|
} else { llargs += [llretslot]; }
|
2011-02-08 11:47:53 -08:00
|
|
|
|
2011-08-03 13:11:02 -07:00
|
|
|
// Arg 1: task pointer.
|
2011-08-19 15:16:48 -07:00
|
|
|
llargs += [bcx.fcx.lltaskptr];
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-08-03 13:11:02 -07:00
|
|
|
// Arg 2: Env (closure-bindings / self-obj)
|
2011-08-19 15:16:48 -07:00
|
|
|
llargs += [llenv];
|
2011-02-08 11:47:53 -08:00
|
|
|
|
2011-08-03 13:11:02 -07:00
|
|
|
// Args >3: ty_params ...
|
2011-06-15 11:19:50 -07:00
|
|
|
llargs += lltydescs;
|
|
|
|
|
2011-08-03 13:11:02 -07:00
|
|
|
// ... then possibly an lliterbody argument.
|
2011-08-19 11:54:46 -07:00
|
|
|
alt lliterbody {
|
|
|
|
none. { }
|
|
|
|
some(lli) {
|
2011-08-19 15:16:48 -07:00
|
|
|
let lli =
|
|
|
|
if ty::type_contains_params(bcx_tcx(cx), retty) {
|
|
|
|
let body_ty = ty::mk_iter_body_fn(bcx_tcx(cx), retty);
|
|
|
|
let body_llty = type_of_inner(bcx_ccx(cx), cx.sp, body_ty);
|
2011-08-30 09:59:30 +02:00
|
|
|
PointerCast(bcx, lli, T_ptr(body_llty))
|
2011-08-19 15:16:48 -07:00
|
|
|
} else { lli };
|
2011-08-30 09:59:30 +02:00
|
|
|
llargs += [Load(cx, lli)];
|
2011-08-19 11:54:46 -07:00
|
|
|
}
|
|
|
|
}
|
2011-08-03 13:11:02 -07:00
|
|
|
|
2011-02-08 11:47:53 -08:00
|
|
|
// ... then explicit args.
|
2011-02-24 13:51:53 -08:00
|
|
|
|
|
|
|
// First we figure out the caller's view of the types of the arguments.
|
|
|
|
// This will be needed if this is a generic call, because the callee has
|
|
|
|
// to cast her view of the arguments to the caller's view.
|
2011-07-27 14:19:39 +02:00
|
|
|
let arg_tys = type_of_explicit_args(bcx_ccx(cx), cx.sp, args);
|
|
|
|
let i = 0u;
|
2011-08-15 21:54:52 -07:00
|
|
|
for e: @ast::expr in es {
|
2011-08-24 14:54:55 +02:00
|
|
|
if is_terminated(bcx) {
|
2011-07-03 10:39:07 -07:00
|
|
|
// This means an earlier arg was divergent.
|
|
|
|
// So this arg can't be evaluated.
|
|
|
|
break;
|
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
let r =
|
|
|
|
trans_arg_expr(bcx, args[i], arg_tys[i], to_zero, to_revoke, e);
|
2011-04-23 14:17:44 -07:00
|
|
|
bcx = r.bcx;
|
2011-08-19 15:16:48 -07:00
|
|
|
llargs += [r.val];
|
2011-02-08 11:47:53 -08:00
|
|
|
i += 1u;
|
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
ret {bcx: bcx,
|
|
|
|
args: llargs,
|
|
|
|
retslot: llretslot,
|
|
|
|
to_zero: to_zero,
|
|
|
|
to_revoke: to_revoke};
|
2011-02-08 11:47:53 -08:00
|
|
|
}
|
|
|
|
|
2011-08-19 14:34:45 +02:00
|
|
|
fn trans_call(in_cx: &@block_ctxt, f: &@ast::expr,
|
2011-08-12 07:15:18 -07:00
|
|
|
lliterbody: &option::t<ValueRef>, args: &[@ast::expr],
|
2011-07-27 14:19:39 +02:00
|
|
|
id: ast::node_id) -> result {
|
2011-04-05 16:17:47 -07:00
|
|
|
// NB: 'f' isn't necessarily a function; it might be an entire self-call
|
|
|
|
// expression because of the hack that allows us to process self-calls
|
|
|
|
// with trans_call.
|
2011-08-26 21:34:56 -07:00
|
|
|
let cx = new_scope_block_ctxt(in_cx, ~"call");
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(in_cx, cx.llbb);
|
2011-07-27 14:19:39 +02:00
|
|
|
let f_res = trans_lval_gen(cx, f);
|
|
|
|
let fn_ty: ty::t;
|
|
|
|
alt f_res.method_ty {
|
|
|
|
some(meth) {
|
|
|
|
// self-call
|
|
|
|
fn_ty = meth;
|
|
|
|
}
|
|
|
|
_ { fn_ty = ty::expr_ty(bcx_tcx(cx), f); }
|
2011-07-01 02:52:05 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx = f_res.res.bcx;
|
2011-07-01 02:52:05 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let faddr = f_res.res.val;
|
|
|
|
let llenv = C_null(T_opaque_closure_ptr(*bcx_ccx(cx)));
|
|
|
|
alt f_res.llobj {
|
2011-08-03 18:31:56 -07:00
|
|
|
some(ob) {
|
2011-07-27 14:19:39 +02:00
|
|
|
// It's a vtbl entry.
|
2011-08-30 09:59:30 +02:00
|
|
|
faddr = Load(bcx, faddr);
|
2011-08-03 18:31:56 -07:00
|
|
|
llenv = ob;
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
none. {
|
|
|
|
// It's a closure. We have to autoderef.
|
|
|
|
if f_res.is_mem { faddr = load_if_immediate(bcx, faddr, fn_ty); }
|
|
|
|
let res = autoderef(bcx, faddr, fn_ty);
|
|
|
|
bcx = res.bcx;
|
|
|
|
fn_ty = res.ty;
|
|
|
|
|
|
|
|
let pair = res.val;
|
2011-08-30 09:59:30 +02:00
|
|
|
faddr = GEP(bcx, pair, [C_int(0), C_int(abi::fn_field_code)]);
|
|
|
|
faddr = Load(bcx, faddr);
|
2011-07-27 14:19:39 +02:00
|
|
|
let llclosure =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(bcx, pair, [C_int(0), C_int(abi::fn_field_box)]);
|
|
|
|
llenv = Load(bcx, llclosure);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2010-12-20 16:48:28 -08:00
|
|
|
}
|
2011-07-01 02:52:05 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let ret_ty = ty::node_id_to_type(bcx_tcx(cx), id);
|
|
|
|
let args_res =
|
2011-08-04 11:30:56 -07:00
|
|
|
trans_args(bcx, llenv, f_res.generic, lliterbody, args, fn_ty);
|
2011-07-26 14:06:02 +02:00
|
|
|
bcx = args_res.bcx;
|
2011-07-27 14:19:39 +02:00
|
|
|
let llargs = args_res.args;
|
|
|
|
let llretslot = args_res.retslot;
|
2011-03-31 11:26:25 -07:00
|
|
|
/*
|
2011-07-26 13:02:26 -07:00
|
|
|
log "calling: " + val_str(bcx_ccx(cx).tn, faddr);
|
2011-07-27 14:48:34 +02:00
|
|
|
|
2011-08-10 15:57:03 -07:00
|
|
|
for arg: ValueRef in llargs {
|
2011-07-26 13:02:26 -07:00
|
|
|
log "arg: " + val_str(bcx_ccx(cx).tn, arg);
|
2011-04-01 16:04:22 -07:00
|
|
|
}
|
2011-03-31 11:26:25 -07:00
|
|
|
*/
|
|
|
|
|
2011-07-02 21:55:38 -07:00
|
|
|
/* If the block is terminated,
|
|
|
|
then one or more of the args has
|
|
|
|
type _|_. Since that means it diverges, the code
|
|
|
|
for the call itself is unreachable. */
|
2011-07-27 14:19:39 +02:00
|
|
|
let retval = C_nil();
|
2011-08-24 14:54:55 +02:00
|
|
|
if !is_terminated(bcx) {
|
2011-08-30 09:59:30 +02:00
|
|
|
FastCall(bcx, faddr, llargs);
|
2011-07-27 14:19:39 +02:00
|
|
|
alt lliterbody {
|
|
|
|
none. {
|
|
|
|
if !ty::type_is_nil(bcx_tcx(cx), ret_ty) {
|
|
|
|
retval = load_if_immediate(bcx, llretslot, ret_ty);
|
|
|
|
// Retval doesn't correspond to anything really tangible
|
|
|
|
// in the frame, but it's a ref all the same, so we put a
|
|
|
|
// note here to drop it when we're done in this scope.
|
2011-08-19 14:34:45 +02:00
|
|
|
add_clean_temp(in_cx, retval, ret_ty);
|
2011-04-23 14:17:44 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
some(_) {
|
|
|
|
// If there was an lliterbody, it means we were calling an
|
|
|
|
// iter, and we are *not* the party using its 'output' value,
|
|
|
|
// we should ignore llretslot.
|
|
|
|
}
|
2011-04-23 14:17:44 -07:00
|
|
|
}
|
2011-08-09 17:56:26 -07:00
|
|
|
|
|
|
|
// Forget about anything we moved out.
|
2011-08-19 15:16:48 -07:00
|
|
|
for {v: v, t: t}: {v: ValueRef, t: ty::t} in args_res.to_zero {
|
2011-08-09 17:56:26 -07:00
|
|
|
zero_alloca(bcx, v, t)
|
|
|
|
}
|
2011-08-19 14:34:45 +02:00
|
|
|
for v: ValueRef in args_res.to_revoke {
|
|
|
|
revoke_clean(bcx, v)
|
|
|
|
}
|
|
|
|
bcx = trans_block_cleanups(bcx, cx);
|
2011-08-26 21:34:56 -07:00
|
|
|
let next_cx = new_sub_block_ctxt(in_cx, ~"next");
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(bcx, next_cx.llbb);
|
2011-08-19 14:34:45 +02:00
|
|
|
bcx = next_cx;
|
2011-03-31 11:26:25 -07:00
|
|
|
}
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(bcx, retval);
|
2011-03-31 11:26:25 -07:00
|
|
|
}
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
fn trans_tup(cx: &@block_ctxt, elts: &[@ast::expr], id: ast::node_id) ->
|
|
|
|
result {
|
2011-08-15 11:40:26 +02:00
|
|
|
let bcx = cx;
|
|
|
|
let t = node_id_type(bcx.fcx.lcx.ccx, id);
|
|
|
|
let tup_res = alloc_ty(bcx, t);
|
|
|
|
let tup_val = tup_res.val;
|
|
|
|
bcx = tup_res.bcx;
|
|
|
|
add_clean_temp(cx, tup_val, t);
|
|
|
|
let i: int = 0;
|
|
|
|
for e in elts {
|
2011-08-15 12:08:05 +02:00
|
|
|
let e_ty = ty::expr_ty(cx.fcx.lcx.ccx.tcx, e);
|
|
|
|
let src = trans_lval(bcx, e);
|
2011-08-15 11:40:26 +02:00
|
|
|
bcx = src.res.bcx;
|
2011-08-19 15:16:48 -07:00
|
|
|
let dst_res = GEP_tup_like(bcx, t, tup_val, [0, i]);
|
2011-08-22 12:38:58 +02:00
|
|
|
bcx = move_val_if_temp(dst_res.bcx, INIT, dst_res.val, src, e_ty);
|
2011-08-15 11:40:26 +02:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
ret rslt(bcx, tup_val);
|
|
|
|
}
|
|
|
|
|
2011-08-04 16:20:09 -07:00
|
|
|
fn trans_rec(cx: &@block_ctxt, fields: &[ast::field],
|
2011-08-12 07:15:18 -07:00
|
|
|
base: &option::t<@ast::expr>, id: ast::node_id) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx = cx;
|
|
|
|
let t = node_id_type(bcx_ccx(bcx), id);
|
|
|
|
let rec_res = alloc_ty(bcx, t);
|
|
|
|
let rec_val = rec_res.val;
|
2011-02-10 15:00:16 -08:00
|
|
|
bcx = rec_res.bcx;
|
Make moving of temporaries do the right thing, use it to optimize
This adds support for dropping cleanups for temporary values when they
are moved somewhere else. It then adds wraps most copy operations
(return, put in data structure, box, etc) in a way that will fall back
to a move when it is safe.
This saves a lot of taking/dropping, shaving over a megabyte off the
stage2/rustc binary size.
In some cases, most notably function returns, we could detect that the
returned value is a local variable, and can thus be safely moved even
though it is not a temporary. This will require putting some more
information in lvals.
I did not yet handle function arguments, since the logic for passing
them looked too convoluted to touch. I'll probably try that in the
near future, since it's bound to be a big win.
2011-07-07 13:36:12 +02:00
|
|
|
add_clean_temp(cx, rec_val, t);
|
2011-07-27 14:19:39 +02:00
|
|
|
let i: int = 0;
|
|
|
|
let base_val = C_nil();
|
|
|
|
alt base {
|
|
|
|
none. { }
|
|
|
|
some(bexp) {
|
|
|
|
let base_res = trans_expr(bcx, bexp);
|
|
|
|
bcx = base_res.bcx;
|
|
|
|
base_val = base_res.val;
|
|
|
|
}
|
2011-02-15 18:16:13 -08:00
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
let ty_fields: [ty::field] = [];
|
2011-07-27 14:19:39 +02:00
|
|
|
alt ty::struct(bcx_tcx(cx), t) { ty::ty_rec(flds) { ty_fields = flds; } }
|
2011-08-15 21:54:52 -07:00
|
|
|
for tf: ty::field in ty_fields {
|
2011-07-27 14:19:39 +02:00
|
|
|
let e_ty = tf.mt.ty;
|
2011-08-19 15:16:48 -07:00
|
|
|
let dst_res = GEP_tup_like(bcx, t, rec_val, [0, i]);
|
2011-02-10 15:00:16 -08:00
|
|
|
bcx = dst_res.bcx;
|
2011-07-27 14:19:39 +02:00
|
|
|
let expr_provided = false;
|
2011-08-15 21:54:52 -07:00
|
|
|
for f: ast::field in fields {
|
2011-08-25 17:00:12 -07:00
|
|
|
if istr::eq(f.node.ident, tf.ident) {
|
2011-02-15 18:16:13 -08:00
|
|
|
expr_provided = true;
|
2011-07-27 14:19:39 +02:00
|
|
|
let lv = trans_lval(bcx, f.node.expr);
|
2011-08-22 12:38:58 +02:00
|
|
|
bcx = move_val_if_temp(lv.res.bcx, INIT, dst_res.val,
|
|
|
|
lv, e_ty);
|
Make moving of temporaries do the right thing, use it to optimize
This adds support for dropping cleanups for temporary values when they
are moved somewhere else. It then adds wraps most copy operations
(return, put in data structure, box, etc) in a way that will fall back
to a move when it is safe.
This saves a lot of taking/dropping, shaving over a megabyte off the
stage2/rustc binary size.
In some cases, most notably function returns, we could detect that the
returned value is a local variable, and can thus be safely moved even
though it is not a temporary. This will require putting some more
information in lvals.
I did not yet handle function arguments, since the logic for passing
them looked too convoluted to touch. I'll probably try that in the
near future, since it's bound to be a big win.
2011-07-07 13:36:12 +02:00
|
|
|
break;
|
2011-02-15 18:16:13 -08:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
if !expr_provided {
|
2011-08-19 15:16:48 -07:00
|
|
|
let src_res = GEP_tup_like(bcx, t, base_val, [0, i]);
|
2011-06-15 11:19:50 -07:00
|
|
|
src_res =
|
2011-06-24 19:04:08 +02:00
|
|
|
rslt(src_res.bcx, load_if_immediate(bcx, src_res.val, e_ty));
|
2011-08-22 12:38:58 +02:00
|
|
|
bcx = copy_val(src_res.bcx, INIT, dst_res.val, src_res.val, e_ty);
|
2011-02-15 18:16:13 -08:00
|
|
|
}
|
2010-11-30 10:39:35 -08:00
|
|
|
i += 1;
|
|
|
|
}
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(bcx, rec_val);
|
2010-11-30 10:39:35 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_expr(cx: &@block_ctxt, e: &@ast::expr) -> result {
|
2011-06-15 12:17:51 +02:00
|
|
|
ret trans_expr_out(cx, e, return);
|
2011-05-31 14:24:04 +02:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_expr_out(cx: &@block_ctxt, e: &@ast::expr, output: out_method) ->
|
2011-06-15 11:19:50 -07:00
|
|
|
result {
|
2011-08-25 10:18:02 +02:00
|
|
|
// Fixme Fill in cx.sp
|
2011-07-27 14:19:39 +02:00
|
|
|
alt e.node {
|
|
|
|
ast::expr_lit(lit) { ret trans_lit(cx, *lit); }
|
|
|
|
ast::expr_unary(op, x) {
|
|
|
|
if op != ast::deref { ret trans_unary(cx, op, x, e.id); }
|
|
|
|
}
|
|
|
|
ast::expr_binary(op, x, y) { ret trans_binary(cx, op, x, y); }
|
|
|
|
ast::expr_if(cond, thn, els) {
|
2011-08-19 15:16:48 -07:00
|
|
|
ret with_out_method(bind trans_if(cx, cond, thn, els, _), cx, e.id,
|
|
|
|
output);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::expr_if_check(cond, thn, els) {
|
2011-08-19 15:16:48 -07:00
|
|
|
ret with_out_method(bind trans_if(cx, cond, thn, els, _), cx, e.id,
|
|
|
|
output);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::expr_ternary(_, _, _) {
|
2011-08-21 21:44:41 -07:00
|
|
|
ret trans_expr_out(cx, ast_util::ternary_to_if(e), output);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::expr_for(decl, seq, body) { ret trans_for(cx, decl, seq, body); }
|
|
|
|
ast::expr_for_each(decl, seq, body) {
|
|
|
|
ret trans_for_each(cx, decl, seq, body);
|
|
|
|
}
|
|
|
|
ast::expr_while(cond, body) { ret trans_while(cx, cond, body); }
|
|
|
|
ast::expr_do_while(body, cond) { ret trans_do_while(cx, body, cond); }
|
|
|
|
ast::expr_alt(expr, arms) {
|
2011-08-19 15:16:48 -07:00
|
|
|
ret with_out_method(bind trans_alt::trans_alt(cx, expr, arms, _), cx,
|
|
|
|
e.id, output);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::expr_fn(f) {
|
|
|
|
let ccx = bcx_ccx(cx);
|
|
|
|
let llfnty: TypeRef =
|
2011-07-26 17:59:43 -07:00
|
|
|
type_of_fn_from_ty(ccx, e.span, node_id_type(ccx, e.id), 0u);
|
2011-08-26 16:54:25 -07:00
|
|
|
let sub_cx = extend_path(cx.fcx.lcx,
|
2011-08-26 18:25:46 -07:00
|
|
|
ccx.names.next(~"anon"));
|
2011-08-25 21:04:04 -07:00
|
|
|
let s = mangle_internal_name_by_path(ccx,
|
2011-08-26 18:25:46 -07:00
|
|
|
sub_cx.path);
|
2011-08-25 21:04:04 -07:00
|
|
|
let llfn = decl_internal_fastcall_fn(ccx.llmod,
|
2011-08-26 21:34:56 -07:00
|
|
|
s, llfnty);
|
2011-07-27 14:19:39 +02:00
|
|
|
|
|
|
|
let fn_res =
|
|
|
|
trans_closure(some(cx), some(llfnty), sub_cx, e.span, f, llfn,
|
2011-08-19 15:16:48 -07:00
|
|
|
none, [], e.id);
|
2011-07-27 14:19:39 +02:00
|
|
|
let fn_pair =
|
|
|
|
alt fn_res {
|
|
|
|
some(fn_pair) { fn_pair }
|
2011-08-19 15:16:48 -07:00
|
|
|
none. {
|
2011-08-26 21:34:56 -07:00
|
|
|
{fn_pair: create_fn_pair(ccx, s,
|
2011-08-25 21:04:04 -07:00
|
|
|
llfnty, llfn, false),
|
2011-08-19 15:16:48 -07:00
|
|
|
bcx: cx}
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
};
|
2011-08-01 12:09:59 -07:00
|
|
|
ret rslt(fn_pair.bcx, fn_pair.fn_pair);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::expr_block(blk) {
|
2011-08-26 21:34:56 -07:00
|
|
|
let sub_cx = new_scope_block_ctxt(cx, ~"block-expr body");
|
|
|
|
let next_cx = new_sub_block_ctxt(cx, ~"next");
|
2011-07-27 14:19:39 +02:00
|
|
|
let sub =
|
|
|
|
with_out_method(bind trans_block(sub_cx, blk, _), cx, e.id,
|
|
|
|
output);
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(cx, sub_cx.llbb);
|
|
|
|
if !is_terminated(sub.bcx) { Br(sub.bcx, next_cx.llbb); }
|
2011-07-27 14:19:39 +02:00
|
|
|
ret rslt(next_cx, sub.val);
|
|
|
|
}
|
2011-08-15 15:35:40 -07:00
|
|
|
ast::expr_copy(a) {
|
|
|
|
// FIXME: this has more-subtle semantics than just "fall through".
|
|
|
|
ret trans_expr_out(cx, a, output);
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ast::expr_move(dst, src) {
|
|
|
|
let lhs_res = trans_lval(cx, dst);
|
|
|
|
assert (lhs_res.is_mem);
|
|
|
|
// FIXME Fill in lhs_res.res.bcx.sp
|
|
|
|
|
|
|
|
let rhs_res = trans_lval(lhs_res.res.bcx, src);
|
|
|
|
let t = ty::expr_ty(bcx_tcx(cx), src);
|
|
|
|
// FIXME: calculate copy init-ness in typestate.
|
|
|
|
|
2011-08-22 12:38:58 +02:00
|
|
|
let bcx = move_val(rhs_res.res.bcx, DROP_EXISTING, lhs_res.res.val,
|
|
|
|
rhs_res, t);
|
|
|
|
ret rslt(bcx, C_nil());
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::expr_assign(dst, src) {
|
|
|
|
let lhs_res = trans_lval(cx, dst);
|
|
|
|
assert (lhs_res.is_mem);
|
|
|
|
// FIXME Fill in lhs_res.res.bcx.sp
|
|
|
|
let rhs = trans_lval(lhs_res.res.bcx, src);
|
|
|
|
let t = ty::expr_ty(bcx_tcx(cx), src);
|
|
|
|
// FIXME: calculate copy init-ness in typestate.
|
2011-08-22 12:38:58 +02:00
|
|
|
let bcx = move_val_if_temp(rhs.res.bcx, DROP_EXISTING,
|
|
|
|
lhs_res.res.val, rhs, t);
|
|
|
|
ret rslt(bcx, C_nil());
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::expr_swap(dst, src) {
|
|
|
|
let lhs_res = trans_lval(cx, dst);
|
|
|
|
assert (lhs_res.is_mem);
|
|
|
|
// FIXME Fill in lhs_res.res.bcx.sp
|
|
|
|
|
|
|
|
let rhs_res = trans_lval(lhs_res.res.bcx, src);
|
|
|
|
let t = ty::expr_ty(bcx_tcx(cx), src);
|
2011-08-22 14:00:41 +02:00
|
|
|
let {bcx, val: tmp_alloc} = alloc_ty(rhs_res.res.bcx, t);
|
2011-07-27 14:19:39 +02:00
|
|
|
// Swap through a temporary.
|
|
|
|
|
2011-08-22 14:00:41 +02:00
|
|
|
bcx = move_val(bcx, INIT, tmp_alloc, lhs_res, t);
|
|
|
|
bcx = move_val(bcx, INIT, lhs_res.res.val, rhs_res, t);
|
|
|
|
bcx = move_val(bcx, INIT, rhs_res.res.val,
|
|
|
|
lval_mem(bcx, tmp_alloc), t);
|
|
|
|
ret rslt(bcx, C_nil());
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::expr_assign_op(op, dst, src) {
|
2011-08-24 13:53:34 +02:00
|
|
|
let tcx = bcx_tcx(cx);
|
|
|
|
let t = ty::expr_ty(tcx, src);
|
2011-07-27 14:19:39 +02:00
|
|
|
let lhs_res = trans_lval(cx, dst);
|
|
|
|
assert (lhs_res.is_mem);
|
|
|
|
|
2011-08-24 13:53:34 +02:00
|
|
|
// Special case for `+= [x]`
|
|
|
|
alt ty::struct(tcx, t) {
|
|
|
|
ty::ty_vec(_) {
|
|
|
|
alt src.node {
|
|
|
|
ast::expr_vec(args, _) {
|
|
|
|
let bcx = ivec::trans_append_literal
|
|
|
|
(lhs_res.res.bcx, lhs_res.res.val, t, args);
|
|
|
|
ret rslt(bcx, C_nil());
|
|
|
|
}
|
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME Fill in lhs_res.res.bcx.sp
|
2011-07-27 14:19:39 +02:00
|
|
|
let rhs_res = trans_expr(lhs_res.res.bcx, src);
|
2011-08-24 13:53:34 +02:00
|
|
|
if ty::type_is_sequence(tcx, t) {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt op {
|
|
|
|
ast::add. {
|
2011-08-24 13:53:34 +02:00
|
|
|
if ty::sequence_is_interior(tcx, t) {
|
2011-07-27 14:19:39 +02:00
|
|
|
ret ivec::trans_append(rhs_res.bcx, t, lhs_res.res.val,
|
|
|
|
rhs_res.val);
|
2011-04-28 17:44:28 -07:00
|
|
|
}
|
2011-08-18 12:22:13 -07:00
|
|
|
ret trans_evec_append(rhs_res.bcx, t, lhs_res.res.val,
|
2011-08-19 15:16:48 -07:00
|
|
|
rhs_res.val);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ { }
|
2011-04-28 17:44:28 -07:00
|
|
|
}
|
2010-11-26 15:54:04 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let lhs_val = load_if_immediate(rhs_res.bcx, lhs_res.res.val, t);
|
2011-08-19 15:16:48 -07:00
|
|
|
let v =
|
|
|
|
trans_eager_binop(rhs_res.bcx, op, lhs_val, t, rhs_res.val, t);
|
2011-07-27 14:19:39 +02:00
|
|
|
// FIXME: calculate copy init-ness in typestate.
|
|
|
|
// This is always a temporary, so can always be safely moved
|
2011-08-22 12:38:58 +02:00
|
|
|
let bcx = move_val(v.bcx, DROP_EXISTING, lhs_res.res.val,
|
|
|
|
lval_val(v.bcx, v.val), t);
|
|
|
|
ret rslt(bcx, C_nil());
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::expr_bind(f, args) { ret trans_bind(cx, f, args, e.id); }
|
|
|
|
ast::expr_call(f, args) {
|
2011-08-13 00:09:25 -07:00
|
|
|
ret trans_call(cx, f, none::<ValueRef>, args, e.id);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::expr_cast(val, _) { ret trans_cast(cx, val, e.id); }
|
2011-08-22 11:23:30 -07:00
|
|
|
ast::expr_vec(args, _) { ret ivec::trans_ivec(cx, args, e.id); }
|
2011-07-27 14:19:39 +02:00
|
|
|
ast::expr_rec(args, base) { ret trans_rec(cx, args, base, e.id); }
|
2011-08-15 11:40:26 +02:00
|
|
|
ast::expr_tup(args) { ret trans_tup(cx, args, e.id); }
|
2011-08-27 16:36:48 -07:00
|
|
|
ast::expr_mac(_) { ret bcx_ccx(cx).sess.bug(~"unexpanded macro"); }
|
2011-07-27 14:19:39 +02:00
|
|
|
ast::expr_fail(expr) { ret trans_fail_expr(cx, some(e.span), expr); }
|
|
|
|
ast::expr_log(lvl, a) { ret trans_log(lvl, cx, a); }
|
2011-08-26 21:34:56 -07:00
|
|
|
ast::expr_assert(a) { ret trans_check_expr(cx, a, ~"Assertion"); }
|
2011-07-27 14:19:39 +02:00
|
|
|
ast::expr_check(ast::checked., a) {
|
2011-08-26 21:34:56 -07:00
|
|
|
ret trans_check_expr(cx, a, ~"Predicate");
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::expr_check(ast::unchecked., a) {
|
|
|
|
/* Claims are turned on and off by a global variable
|
|
|
|
that the RTS sets. This case generates code to
|
|
|
|
check the value of that variable, doing nothing
|
|
|
|
if it's set to false and acting like a check
|
|
|
|
otherwise. */
|
|
|
|
let c =
|
|
|
|
get_extern_const(bcx_ccx(cx).externs, bcx_ccx(cx).llmod,
|
2011-08-26 21:34:56 -07:00
|
|
|
~"check_claims", T_bool());
|
2011-08-30 09:59:30 +02:00
|
|
|
let cond = Load(cx, c);
|
2011-07-27 14:19:39 +02:00
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
let then_cx = new_scope_block_ctxt(cx, ~"claim_then");
|
|
|
|
let check_res = trans_check_expr(then_cx, a, ~"Claim");
|
|
|
|
let else_cx = new_scope_block_ctxt(cx, ~"else");
|
2011-07-27 14:19:39 +02:00
|
|
|
let els = rslt(else_cx, C_nil());
|
|
|
|
|
2011-08-30 09:59:30 +02:00
|
|
|
CondBr(cx, cond, then_cx.llbb, else_cx.llbb);
|
2011-08-19 15:16:48 -07:00
|
|
|
ret rslt(join_branches(cx, [check_res, els]), C_nil());
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::expr_break. { ret trans_break(e.span, cx); }
|
|
|
|
ast::expr_cont. { ret trans_cont(e.span, cx); }
|
|
|
|
ast::expr_ret(ex) { ret trans_ret(cx, ex); }
|
|
|
|
ast::expr_put(ex) { ret trans_put(cx, ex); }
|
2011-08-26 10:14:58 -07:00
|
|
|
ast::expr_be(ex) {
|
|
|
|
// Ideally, the expr_be tag would have a precondition
|
|
|
|
// that is_call_expr(ex) -- but we don't support that
|
|
|
|
// yet
|
|
|
|
// FIXME
|
|
|
|
check ast_util::is_call_expr(ex);
|
|
|
|
ret trans_be(cx, ex);
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ast::expr_anon_obj(anon_obj) {
|
|
|
|
ret trans_anon_obj(cx, e.span, anon_obj, e.id);
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
// The expression is an lvalue. Fall through.
|
2011-07-27 14:48:34 +02:00
|
|
|
assert (ty::is_lval(e));
|
|
|
|
// make sure it really is and that we
|
|
|
|
// didn't forget to add a case for a new expr!
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2010-09-28 12:23:40 -07:00
|
|
|
}
|
2011-04-07 12:50:37 -07:00
|
|
|
// lval cases fall through to trans_lval and then
|
|
|
|
// possibly load the result (if it's non-structural).
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let t = ty::expr_ty(bcx_tcx(cx), e);
|
|
|
|
let sub = trans_lval(cx, e);
|
|
|
|
let v = sub.res.val;
|
|
|
|
if sub.is_mem { v = load_if_immediate(sub.res.bcx, v, t); }
|
2011-07-08 14:28:46 +02:00
|
|
|
ret rslt(sub.res.bcx, v);
|
2010-09-28 12:23:40 -07:00
|
|
|
}
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
fn with_out_method(work: fn(&out_method) -> result, cx: @block_ctxt,
|
2011-07-27 14:19:39 +02:00
|
|
|
id: ast::node_id, outer_output: &out_method) -> result {
|
|
|
|
let ccx = bcx_ccx(cx);
|
|
|
|
if outer_output != return {
|
2011-05-31 14:24:04 +02:00
|
|
|
ret work(outer_output);
|
|
|
|
} else {
|
2011-07-27 14:19:39 +02:00
|
|
|
let tp = node_id_type(ccx, id);
|
|
|
|
if ty::type_is_nil(ccx.tcx, tp) { ret work(return); }
|
|
|
|
let res_alloca = alloc_ty(cx, tp);
|
2011-05-31 14:24:04 +02:00
|
|
|
cx = zero_alloca(res_alloca.bcx, res_alloca.val, tp).bcx;
|
2011-07-27 14:19:39 +02:00
|
|
|
let done = work(save_in(res_alloca.val));
|
|
|
|
let loaded = load_if_immediate(done.bcx, res_alloca.val, tp);
|
Make moving of temporaries do the right thing, use it to optimize
This adds support for dropping cleanups for temporary values when they
are moved somewhere else. It then adds wraps most copy operations
(return, put in data structure, box, etc) in a way that will fall back
to a move when it is safe.
This saves a lot of taking/dropping, shaving over a megabyte off the
stage2/rustc binary size.
In some cases, most notably function returns, we could detect that the
returned value is a local variable, and can thus be safely moved even
though it is not a temporary. This will require putting some more
information in lvals.
I did not yet handle function arguments, since the logic for passing
them looked too convoluted to touch. I'll probably try that in the
near future, since it's bound to be a big win.
2011-07-07 13:36:12 +02:00
|
|
|
add_clean_temp(cx, loaded, tp);
|
2011-07-27 14:19:39 +02:00
|
|
|
ret rslt(done.bcx, loaded);
|
2011-05-31 14:24:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2010-12-03 13:03:07 -08:00
|
|
|
// We pass structural values around the compiler "by pointer" and
|
2011-04-12 12:06:20 -07:00
|
|
|
// non-structural values (scalars, boxes, pointers) "by value". We call the
|
|
|
|
// latter group "immediates" and, in some circumstances when we know we have a
|
|
|
|
// pointer (or need one), perform load/store operations based on the
|
|
|
|
// immediate-ness of the type.
|
2011-08-22 12:45:18 +02:00
|
|
|
fn type_is_immediate(ccx: &@crate_ctxt, t: ty::t) -> bool {
|
2011-06-15 11:19:50 -07:00
|
|
|
ret ty::type_is_scalar(ccx.tcx, t) || ty::type_is_boxed(ccx.tcx, t) ||
|
2011-08-25 10:18:02 +02:00
|
|
|
ty::type_is_native(ccx.tcx, t) || ty::type_is_ivec(ccx.tcx, t);
|
2011-04-12 12:06:20 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn do_spill(cx: &@block_ctxt, v: ValueRef) -> ValueRef {
|
2011-04-12 12:06:20 -07:00
|
|
|
// We have a value but we have to spill it to pass by alias.
|
2011-07-27 14:19:39 +02:00
|
|
|
let llptr = alloca(cx, val_ty(v));
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(cx, v, llptr);
|
2011-04-12 12:06:20 -07:00
|
|
|
ret llptr;
|
|
|
|
}
|
2010-12-03 13:03:07 -08:00
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn spill_if_immediate(cx: &@block_ctxt, v: ValueRef, t: ty::t) -> ValueRef {
|
2011-07-27 14:19:39 +02:00
|
|
|
if type_is_immediate(bcx_ccx(cx), t) { ret do_spill(cx, v); }
|
2011-04-12 12:06:20 -07:00
|
|
|
ret v;
|
|
|
|
}
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn load_if_immediate(cx: &@block_ctxt, v: ValueRef, t: ty::t) -> ValueRef {
|
2011-08-30 09:59:30 +02:00
|
|
|
if type_is_immediate(bcx_ccx(cx), t) { ret Load(cx, v); }
|
2011-04-12 12:06:20 -07:00
|
|
|
ret v;
|
2010-12-03 13:03:07 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_log(lvl: int, cx: &@block_ctxt, e: &@ast::expr) -> result {
|
|
|
|
let lcx = cx.fcx.lcx;
|
2011-08-26 18:25:46 -07:00
|
|
|
let modname = istr::connect(lcx.module_path, ~"::");
|
2011-07-27 14:19:39 +02:00
|
|
|
let global;
|
2011-08-26 18:25:46 -07:00
|
|
|
if lcx.ccx.module_data.contains_key(modname) {
|
|
|
|
global = lcx.ccx.module_data.get(modname);
|
Make log the log level configurable per module
This overloads the meaning of RUST_LOG to also allow
'module.submodule' or 'module.somethingelse=2' forms. The first turn
on all logging for a module (loglevel 3), the second sets its loglevel
to 2. Log levels are:
0: Show only errors
1: Errors and warnings
2: Errors, warnings, and notes
3: Everything, including debug logging
Right now, since we only have one 'log' operation, everything happens
at level 1 (warning), so the only meaningful thing that can be done
with the new RUST_LOG support is disable logging (=0) for some
modules.
TODOS:
* Language support for logging at a specific level
* Also add a log level field to tasks, query the current task as well
as the current module before logging (log if one of them allows it)
* Revise the C logging API to conform to this set-up (globals for
per-module log level, query the task level before logging, stop
using a global mask)
Implementation notes:
Crates now contain two extra data structures. A 'module map' that
contains names and pointers to the module-log-level globals for each
module in the crate that logs, and a 'crate map' that points at the
crate's module map, as well as at the crate maps of all external
crates it depends on. These are walked by the runtime (in
rust_crate.cpp) to set the currect log levels based on RUST_LOG.
These module log globals are allocated as-needed whenever a log
expression is encountered, and their location is hard-coded into the
logging code, which compares the current level to the log statement's
level, and skips over all logging code when it is lower.
2011-04-17 16:29:18 +02:00
|
|
|
} else {
|
2011-07-27 14:19:39 +02:00
|
|
|
let s =
|
2011-08-25 21:04:04 -07:00
|
|
|
link::mangle_internal_name_by_path_and_seq(
|
|
|
|
lcx.ccx,
|
2011-08-26 18:25:46 -07:00
|
|
|
lcx.module_path,
|
2011-08-25 21:04:04 -07:00
|
|
|
~"loglevel");
|
2011-08-26 15:36:18 -07:00
|
|
|
global = istr::as_buf(s, { |buf|
|
|
|
|
llvm::LLVMAddGlobal(lcx.ccx.llmod, T_int(), buf)
|
|
|
|
});
|
2011-05-12 17:24:54 +02:00
|
|
|
llvm::LLVMSetGlobalConstant(global, False);
|
|
|
|
llvm::LLVMSetInitializer(global, C_null(T_int()));
|
2011-06-15 11:19:50 -07:00
|
|
|
llvm::LLVMSetLinkage(global,
|
|
|
|
lib::llvm::LLVMInternalLinkage as llvm::Linkage);
|
2011-08-26 18:25:46 -07:00
|
|
|
lcx.ccx.module_data.insert(modname, global);
|
Make log the log level configurable per module
This overloads the meaning of RUST_LOG to also allow
'module.submodule' or 'module.somethingelse=2' forms. The first turn
on all logging for a module (loglevel 3), the second sets its loglevel
to 2. Log levels are:
0: Show only errors
1: Errors and warnings
2: Errors, warnings, and notes
3: Everything, including debug logging
Right now, since we only have one 'log' operation, everything happens
at level 1 (warning), so the only meaningful thing that can be done
with the new RUST_LOG support is disable logging (=0) for some
modules.
TODOS:
* Language support for logging at a specific level
* Also add a log level field to tasks, query the current task as well
as the current module before logging (log if one of them allows it)
* Revise the C logging API to conform to this set-up (globals for
per-module log level, query the task level before logging, stop
using a global mask)
Implementation notes:
Crates now contain two extra data structures. A 'module map' that
contains names and pointers to the module-log-level globals for each
module in the crate that logs, and a 'crate map' that points at the
crate's module map, as well as at the crate maps of all external
crates it depends on. These are walked by the runtime (in
rust_crate.cpp) to set the currect log levels based on RUST_LOG.
These module log globals are allocated as-needed whenever a log
expression is encountered, and their location is hard-coded into the
logging code, which compares the current level to the log statement's
level, and skips over all logging code when it is lower.
2011-04-17 16:29:18 +02:00
|
|
|
}
|
2011-08-26 21:34:56 -07:00
|
|
|
let log_cx = new_scope_block_ctxt(cx, ~"log");
|
|
|
|
let after_cx = new_sub_block_ctxt(cx, ~"after");
|
2011-08-30 09:59:30 +02:00
|
|
|
let load = Load(cx, global);
|
|
|
|
let test = ICmp(cx, lib::llvm::LLVMIntSGE, load, C_int(lvl));
|
|
|
|
CondBr(cx, test, log_cx.llbb, after_cx.llbb);
|
2011-07-27 14:19:39 +02:00
|
|
|
let sub = trans_expr(log_cx, e);
|
|
|
|
let e_ty = ty::expr_ty(bcx_tcx(cx), e);
|
|
|
|
let log_bcx = sub.bcx;
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-08-13 00:09:25 -07:00
|
|
|
let ti = none::<@tydesc_info>;
|
2011-08-24 18:36:51 -07:00
|
|
|
let r = get_tydesc(log_bcx, e_ty, false, tps_normal, ti).result;
|
2011-08-10 12:55:29 -07:00
|
|
|
log_bcx = r.bcx;
|
|
|
|
|
|
|
|
// Call the polymorphic log function.
|
|
|
|
let llvalptr = spill_if_immediate(log_bcx, sub.val, e_ty);
|
2011-08-30 09:59:30 +02:00
|
|
|
let llval_i8 = PointerCast(log_bcx, llvalptr, T_ptr(T_i8()));
|
2011-08-10 12:55:29 -07:00
|
|
|
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(log_bcx, bcx_ccx(log_bcx).upcalls.log_type,
|
2011-08-19 15:16:48 -07:00
|
|
|
[log_bcx.fcx.lltaskptr, r.val, llval_i8, C_int(lvl)]);
|
2011-07-27 14:19:39 +02:00
|
|
|
|
2011-04-29 19:19:54 -07:00
|
|
|
log_bcx = trans_block_cleanups(log_bcx, log_cx);
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(log_bcx, after_cx.llbb);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(after_cx, C_nil());
|
2010-09-22 17:05:38 -07:00
|
|
|
}
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn trans_check_expr(cx: &@block_ctxt, e: &@ast::expr, s: &istr) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let cond_res = trans_expr(cx, e);
|
2011-08-27 01:16:40 -07:00
|
|
|
let expr_str = s + ~" " + expr_to_str(e) + ~" failed";
|
2011-08-26 21:34:56 -07:00
|
|
|
let fail_cx = new_sub_block_ctxt(cx, ~"fail");
|
2011-08-13 00:09:25 -07:00
|
|
|
trans_fail(fail_cx, some::<span>(e.span), expr_str);
|
2011-08-26 21:34:56 -07:00
|
|
|
let next_cx = new_sub_block_ctxt(cx, ~"next");
|
2011-08-30 09:59:30 +02:00
|
|
|
CondBr(cond_res.bcx, cond_res.val, next_cx.llbb, fail_cx.llbb);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(next_cx, C_nil());
|
2010-10-22 15:37:42 -07:00
|
|
|
}
|
|
|
|
|
2011-08-12 07:15:18 -07:00
|
|
|
fn trans_fail_expr(cx: &@block_ctxt, sp_opt: &option::t<span>,
|
|
|
|
fail_expr: &option::t<@ast::expr>) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx = cx;
|
|
|
|
alt fail_expr {
|
|
|
|
some(expr) {
|
|
|
|
let tcx = bcx_tcx(bcx);
|
|
|
|
let expr_res = trans_expr(bcx, expr);
|
|
|
|
let e_ty = ty::expr_ty(tcx, expr);
|
|
|
|
bcx = expr_res.bcx;
|
2011-07-01 14:33:15 -04:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
|
|
|
|
if ty::type_is_str(tcx, e_ty) {
|
|
|
|
let elt =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(bcx, expr_res.val,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::vec_elt_data)]);
|
2011-07-27 14:19:39 +02:00
|
|
|
ret trans_fail_value(bcx, sp_opt, elt);
|
|
|
|
} else {
|
|
|
|
bcx_ccx(cx).sess.span_bug(expr.span,
|
2011-08-27 16:36:48 -07:00
|
|
|
~"fail called with unsupported type "
|
|
|
|
+ ty_to_str(tcx, e_ty));
|
2011-07-01 14:33:15 -04:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-26 21:34:56 -07:00
|
|
|
_ { ret trans_fail(bcx, sp_opt, ~"explicit failure"); }
|
2011-07-01 14:33:15 -04:00
|
|
|
}
|
|
|
|
}
|
2011-07-13 15:44:09 -07:00
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn trans_fail(cx: &@block_ctxt, sp_opt: &option::t<span>, fail_str: &istr) ->
|
2011-07-27 14:19:39 +02:00
|
|
|
result {
|
2011-08-26 21:34:56 -07:00
|
|
|
let V_fail_str = C_cstr(bcx_ccx(cx), fail_str);
|
2011-07-01 14:33:15 -04:00
|
|
|
ret trans_fail_value(cx, sp_opt, V_fail_str);
|
|
|
|
}
|
|
|
|
|
2011-08-12 07:15:18 -07:00
|
|
|
fn trans_fail_value(cx: &@block_ctxt, sp_opt: &option::t<span>,
|
2011-07-27 14:19:39 +02:00
|
|
|
V_fail_str: &ValueRef) -> result {
|
|
|
|
let V_filename;
|
|
|
|
let V_line;
|
|
|
|
alt sp_opt {
|
|
|
|
some(sp) {
|
|
|
|
let loc = bcx_ccx(cx).sess.lookup_pos(sp.lo);
|
2011-08-27 00:43:22 -07:00
|
|
|
V_filename = C_cstr(bcx_ccx(cx), loc.filename);
|
2011-07-27 14:19:39 +02:00
|
|
|
V_line = loc.line as int;
|
|
|
|
}
|
2011-08-26 16:54:25 -07:00
|
|
|
none. { V_filename = C_cstr(bcx_ccx(cx), ~"<runtime>"); V_line = 0; }
|
2011-04-19 15:22:57 -07:00
|
|
|
}
|
2011-08-30 09:59:30 +02:00
|
|
|
let V_str = PointerCast(cx, V_fail_str, T_ptr(T_i8()));
|
|
|
|
V_filename = PointerCast(cx, V_filename, T_ptr(T_i8()));
|
2011-08-19 15:16:48 -07:00
|
|
|
let args = [cx.fcx.lltaskptr, V_str, V_filename, C_int(V_line)];
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(cx, bcx_ccx(cx).upcalls._fail, args);
|
|
|
|
Unreachable(cx);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(cx, C_nil());
|
2011-01-28 00:09:26 -05:00
|
|
|
}
|
|
|
|
|
2011-08-19 14:34:45 +02:00
|
|
|
fn trans_put(in_cx: &@block_ctxt, e: &option::t<@ast::expr>) -> result {
|
2011-08-26 21:34:56 -07:00
|
|
|
let cx = new_scope_block_ctxt(in_cx, ~"put");
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(in_cx, cx.llbb);
|
2011-07-27 14:19:39 +02:00
|
|
|
let llcallee = C_nil();
|
|
|
|
let llenv = C_nil();
|
|
|
|
alt { cx.fcx.lliterbody } {
|
|
|
|
some(lli) {
|
|
|
|
let slot = alloca(cx, val_ty(lli));
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(cx, lli, slot);
|
|
|
|
llcallee = GEP(cx, slot, [C_int(0), C_int(abi::fn_field_code)]);
|
|
|
|
llcallee = Load(cx, llcallee);
|
|
|
|
llenv = GEP(cx, slot, [C_int(0), C_int(abi::fn_field_box)]);
|
|
|
|
llenv = Load(cx, llenv);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-02-18 18:52:16 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx = cx;
|
|
|
|
let dummy_retslot = alloca(bcx, T_nil());
|
2011-08-19 15:16:48 -07:00
|
|
|
let llargs: [ValueRef] = [dummy_retslot, cx.fcx.lltaskptr, llenv];
|
2011-07-27 14:19:39 +02:00
|
|
|
alt e {
|
|
|
|
none. { }
|
|
|
|
some(x) {
|
|
|
|
let e_ty = ty::expr_ty(bcx_tcx(cx), x);
|
|
|
|
let arg = {mode: ty::mo_alias(false), ty: e_ty};
|
2011-08-19 15:16:48 -07:00
|
|
|
let arg_tys = type_of_explicit_args(bcx_ccx(cx), x.span, [arg]);
|
|
|
|
let z = [];
|
|
|
|
let k = [];
|
|
|
|
let r = trans_arg_expr(bcx, arg, arg_tys[0], z, k, x);
|
2011-07-27 14:19:39 +02:00
|
|
|
bcx = r.bcx;
|
2011-08-19 15:16:48 -07:00
|
|
|
llargs += [r.val];
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-02-18 18:52:16 -08:00
|
|
|
}
|
2011-08-30 09:59:30 +02:00
|
|
|
FastCall(bcx, llcallee, llargs);
|
2011-08-19 14:34:45 +02:00
|
|
|
bcx = trans_block_cleanups(bcx, cx);
|
2011-08-26 21:34:56 -07:00
|
|
|
let next_cx = new_sub_block_ctxt(in_cx, ~"next");
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(bcx, next_cx.llbb);
|
2011-08-19 14:34:45 +02:00
|
|
|
ret rslt(next_cx, C_nil());
|
2011-02-14 17:58:32 -08:00
|
|
|
}
|
|
|
|
|
2011-08-16 12:38:42 -07:00
|
|
|
fn trans_uniq(cx: &@block_ctxt, contents: &@ast::expr) -> lval_result {
|
|
|
|
let bcx = cx;
|
|
|
|
|
|
|
|
let contents_ty = ty::expr_ty(bcx_tcx(bcx), contents);
|
|
|
|
let r = size_of(bcx, contents_ty);
|
|
|
|
bcx = r.bcx;
|
|
|
|
let llsz = r.val;
|
|
|
|
|
|
|
|
let llptrty = T_ptr(type_of_or_i8(bcx, contents_ty));
|
|
|
|
|
|
|
|
r = trans_shared_malloc(bcx, llptrty, llsz);
|
|
|
|
bcx = r.bcx;
|
|
|
|
let llptrptr = r.val;
|
|
|
|
|
2011-08-30 09:59:30 +02:00
|
|
|
let llptr = Load(bcx, llptrptr);
|
2011-08-16 12:38:42 -07:00
|
|
|
r = trans_expr_out(bcx, contents, save_in(llptr));
|
|
|
|
ret lval_val(r.bcx, llptrptr);
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_break_cont(sp: &span, cx: &@block_ctxt, to_end: bool) -> result {
|
|
|
|
let bcx = cx;
|
2011-03-25 16:28:16 +01:00
|
|
|
// Locate closest loop block, outputting cleanup as we go.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let cleanup_cx = cx;
|
|
|
|
while true {
|
2011-03-25 16:28:16 +01:00
|
|
|
bcx = trans_block_cleanups(bcx, cleanup_cx);
|
2011-07-27 14:19:39 +02:00
|
|
|
alt { cleanup_cx.kind } {
|
|
|
|
LOOP_SCOPE_BLOCK(_cont, _break) {
|
|
|
|
if to_end {
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(bcx, _break.llbb);
|
2011-07-27 14:19:39 +02:00
|
|
|
} else {
|
|
|
|
alt _cont {
|
2011-08-30 09:59:30 +02:00
|
|
|
option::some(_cont) { Br(bcx, _cont.llbb); }
|
|
|
|
_ { Br(bcx, cleanup_cx.llbb); }
|
2011-03-25 16:28:16 +01:00
|
|
|
}
|
|
|
|
}
|
2011-08-26 21:34:56 -07:00
|
|
|
ret rslt(new_sub_block_ctxt(bcx, ~"break_cont.unreachable"),
|
2011-07-27 14:19:39 +02:00
|
|
|
C_nil());
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
alt { cleanup_cx.parent } {
|
|
|
|
parent_some(cx) { cleanup_cx = cx; }
|
|
|
|
parent_none. {
|
|
|
|
bcx_ccx(cx).sess.span_fatal(sp,
|
|
|
|
if to_end {
|
2011-08-27 16:36:48 -07:00
|
|
|
~"Break"
|
|
|
|
} else { ~"Cont" } +
|
|
|
|
~" outside a loop");
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-03-25 16:28:16 +01:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-03-25 16:28:16 +01:00
|
|
|
}
|
|
|
|
}
|
2011-06-04 18:13:00 -07:00
|
|
|
// If we get here without returning, it's a bug
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-08-27 16:36:48 -07:00
|
|
|
bcx_ccx(cx).sess.bug(~"in trans::trans_break_cont()");
|
2011-03-25 16:28:16 +01:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_break(sp: &span, cx: &@block_ctxt) -> result {
|
2011-06-10 14:34:01 -07:00
|
|
|
ret trans_break_cont(sp, cx, true);
|
2011-03-25 16:28:16 +01:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_cont(sp: &span, cx: &@block_ctxt) -> result {
|
2011-06-10 14:34:01 -07:00
|
|
|
ret trans_break_cont(sp, cx, false);
|
2011-03-25 16:28:16 +01:00
|
|
|
}
|
|
|
|
|
2011-08-12 07:15:18 -07:00
|
|
|
fn trans_ret(cx: &@block_ctxt, e: &option::t<@ast::expr>) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx = cx;
|
|
|
|
alt e {
|
|
|
|
some(x) {
|
|
|
|
let t = ty::expr_ty(bcx_tcx(cx), x);
|
|
|
|
let lv = trans_lval(cx, x);
|
|
|
|
bcx = lv.res.bcx;
|
2011-08-19 15:16:48 -07:00
|
|
|
let is_local =
|
|
|
|
alt x.node {
|
|
|
|
ast::expr_path(p) {
|
|
|
|
alt bcx_tcx(bcx).def_map.get(x.id) {
|
|
|
|
ast::def_local(_) { true }
|
|
|
|
_ { false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ { false }
|
|
|
|
};
|
2011-08-19 10:06:20 +02:00
|
|
|
if is_local {
|
2011-08-22 12:38:58 +02:00
|
|
|
bcx = move_val(bcx, INIT, cx.fcx.llretptr, lv, t);
|
2011-08-19 10:06:20 +02:00
|
|
|
} else {
|
2011-08-22 12:38:58 +02:00
|
|
|
bcx = move_val_if_temp(bcx, INIT, cx.fcx.llretptr, lv, t);
|
2011-08-19 10:06:20 +02:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ {
|
|
|
|
let t = llvm::LLVMGetElementType(val_ty(cx.fcx.llretptr));
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(bcx, C_null(t), cx.fcx.llretptr);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2010-10-22 19:23:10 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
// run all cleanups and back out.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let more_cleanups: bool = true;
|
|
|
|
let cleanup_cx = cx;
|
|
|
|
while more_cleanups {
|
2011-01-21 07:58:16 -08:00
|
|
|
bcx = trans_block_cleanups(bcx, cleanup_cx);
|
2011-07-27 14:19:39 +02:00
|
|
|
alt { cleanup_cx.parent } {
|
|
|
|
parent_some(b) { cleanup_cx = b; }
|
|
|
|
parent_none. { more_cleanups = false; }
|
2010-11-10 17:46:49 -08:00
|
|
|
}
|
|
|
|
}
|
2011-08-17 17:49:54 -07:00
|
|
|
build_return(bcx);
|
2011-08-26 21:34:56 -07:00
|
|
|
ret rslt(new_sub_block_ctxt(bcx, ~"ret.unreachable"), C_nil());
|
2010-10-22 19:23:10 -07:00
|
|
|
}
|
|
|
|
|
2011-08-30 09:59:30 +02:00
|
|
|
fn build_return(bcx: &@block_ctxt) { Br(bcx, bcx_fcx(bcx).llreturn); }
|
2011-08-17 17:49:54 -07:00
|
|
|
|
2011-08-26 10:14:58 -07:00
|
|
|
// fn trans_be(cx: &@block_ctxt, e: &@ast::expr) -> result {
|
|
|
|
fn trans_be(cx: &@block_ctxt, e: &@ast::expr)
|
|
|
|
: ast_util::is_call_expr(e) -> result {
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-02-09 22:36:37 -05:00
|
|
|
// FIXME: Turn this into a real tail call once
|
|
|
|
// calling convention issues are settled
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-01-30 17:18:19 -05:00
|
|
|
ret trans_ret(cx, some(e));
|
|
|
|
}
|
|
|
|
|
2011-07-28 12:01:45 +02:00
|
|
|
fn init_local(bcx: @block_ctxt, local: &@ast::local) -> result {
|
|
|
|
let ty = node_id_type(bcx_ccx(bcx), local.node.id);
|
|
|
|
let llptr = bcx.fcx.lllocals.get(local.node.id);
|
2011-01-21 07:58:16 -08:00
|
|
|
// Make a note to drop this slot on the way out.
|
2011-07-28 12:01:45 +02:00
|
|
|
add_clean(bcx, llptr, ty);
|
2011-08-23 15:45:24 -07:00
|
|
|
|
|
|
|
if (must_zero(local)) {
|
|
|
|
bcx = zero_alloca(bcx, llptr, ty).bcx;
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
alt local.node.init {
|
|
|
|
some(init) {
|
|
|
|
alt init.op {
|
|
|
|
ast::init_assign. {
|
|
|
|
// Use the type of the RHS because if it's _|_, the LHS
|
|
|
|
// type might be something else, but we don't want to copy
|
|
|
|
// the value.
|
2011-07-28 12:01:45 +02:00
|
|
|
ty = node_id_type(bcx_ccx(bcx), init.expr.id);
|
2011-07-27 14:19:39 +02:00
|
|
|
let sub = trans_lval(bcx, init.expr);
|
2011-08-22 12:38:58 +02:00
|
|
|
bcx = move_val_if_temp(sub.res.bcx, INIT, llptr, sub, ty);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::init_move. {
|
|
|
|
let sub = trans_lval(bcx, init.expr);
|
2011-08-22 12:38:58 +02:00
|
|
|
bcx = move_val(sub.res.bcx, INIT, llptr, sub, ty);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-01-21 07:58:16 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-23 15:45:24 -07:00
|
|
|
_ { }
|
2011-01-21 07:58:16 -08:00
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
bcx =
|
|
|
|
trans_alt::bind_irrefutable_pat(bcx, local.node.pat, llptr,
|
|
|
|
bcx.fcx.lllocals, false);
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(bcx, llptr);
|
2011-08-23 15:45:24 -07:00
|
|
|
|
|
|
|
fn must_zero(local: &@ast::local) -> bool {
|
|
|
|
alt local.node.init {
|
|
|
|
some(init) {
|
|
|
|
might_not_init(init.expr)
|
|
|
|
}
|
|
|
|
none. { true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn might_not_init(expr: &@ast::expr) -> bool {
|
|
|
|
type env = @mutable bool;
|
|
|
|
let e = @mutable false;
|
|
|
|
let visitor = visit::mk_vt(@{
|
|
|
|
visit_expr: fn(ex: &@ast::expr, e: &env, v: &vt<env>) {
|
|
|
|
// FIXME: Probably also need to account for expressions that
|
|
|
|
// fail but since we don't unwind yet, it doesn't seem to be a
|
|
|
|
// problem
|
|
|
|
let might_not_init = alt ex.node {
|
|
|
|
ast::expr_ret(_) { true }
|
|
|
|
ast::expr_break. { true }
|
|
|
|
ast::expr_cont. { true }
|
|
|
|
_ { false }
|
|
|
|
};
|
|
|
|
if might_not_init {
|
|
|
|
*e = true;
|
|
|
|
} else {
|
|
|
|
visit::visit_expr(ex, e, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
with *visit::default_visitor()
|
|
|
|
});
|
|
|
|
visitor.visit_expr(expr, e, visitor);
|
|
|
|
ret *e;
|
|
|
|
}
|
2011-01-21 07:58:16 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn zero_alloca(cx: &@block_ctxt, llptr: ValueRef, t: ty::t) -> result {
|
|
|
|
let bcx = cx;
|
|
|
|
if ty::type_has_dynamic_size(bcx_tcx(cx), t) {
|
|
|
|
let llsz = size_of(bcx, t);
|
2011-08-19 10:05:30 +02:00
|
|
|
// FIXME passing in the align here is correct, but causes issue #843
|
|
|
|
// let llalign = align_of(llsz.bcx, t);
|
|
|
|
bcx = call_bzero(llsz.bcx, llptr, llsz.val, C_int(0)).bcx;
|
2011-04-05 23:42:33 -04:00
|
|
|
} else {
|
2011-07-27 14:19:39 +02:00
|
|
|
let llty = type_of(bcx_ccx(bcx), cx.sp, t);
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(bcx, C_null(llty), llptr);
|
2011-04-05 23:42:33 -04:00
|
|
|
}
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(bcx, llptr);
|
2011-06-15 11:19:50 -07:00
|
|
|
}
|
2011-04-05 23:42:33 -04:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_stmt(cx: &@block_ctxt, s: &ast::stmt) -> result {
|
2011-06-15 12:17:51 +02:00
|
|
|
// FIXME Fill in cx.sp
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx = cx;
|
|
|
|
alt s.node {
|
|
|
|
ast::stmt_expr(e, _) { bcx = trans_expr(cx, e).bcx; }
|
|
|
|
ast::stmt_decl(d, _) {
|
|
|
|
alt d.node {
|
|
|
|
ast::decl_local(locals) {
|
2011-08-15 21:54:52 -07:00
|
|
|
for local: @ast::local in locals {
|
2011-07-27 14:19:39 +02:00
|
|
|
bcx = init_local(bcx, local).bcx;
|
2010-10-19 14:54:10 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::decl_item(i) { trans_item(cx.fcx.lcx, *i); }
|
2010-10-19 14:54:10 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-27 16:36:48 -07:00
|
|
|
_ { bcx_ccx(cx).sess.unimpl(~"stmt variant"); }
|
2010-09-23 13:15:51 -07:00
|
|
|
}
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(bcx, C_nil());
|
2010-10-01 18:25:42 -07:00
|
|
|
}
|
|
|
|
|
2010-10-04 15:55:12 -07:00
|
|
|
// You probably don't want to use this one. See the
|
|
|
|
// next three functions instead.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn new_block_ctxt(cx: &@fn_ctxt, parent: &block_parent, kind: block_kind,
|
2011-08-26 21:34:56 -07:00
|
|
|
name: &istr) -> @block_ctxt {
|
2011-08-26 16:54:25 -07:00
|
|
|
let s = ~"";
|
2011-07-27 14:19:39 +02:00
|
|
|
if cx.lcx.ccx.sess.get_opts().save_temps ||
|
|
|
|
cx.lcx.ccx.sess.get_opts().debuginfo {
|
2011-08-26 21:34:56 -07:00
|
|
|
s = cx.lcx.ccx.names.next(name);
|
2011-05-10 16:43:34 -07:00
|
|
|
}
|
2011-08-26 16:54:25 -07:00
|
|
|
let llbb: BasicBlockRef = istr::as_buf(s, { |buf|
|
2011-08-26 15:36:18 -07:00
|
|
|
llvm::LLVMAppendBasicBlock(cx.llfn, buf)
|
|
|
|
});
|
2011-07-27 14:19:39 +02:00
|
|
|
ret @{llbb: llbb,
|
2011-08-24 14:54:55 +02:00
|
|
|
mutable terminated: false,
|
2011-07-27 14:19:39 +02:00
|
|
|
parent: parent,
|
|
|
|
kind: kind,
|
2011-08-24 14:54:55 +02:00
|
|
|
mutable cleanups: [],
|
2011-07-27 14:19:39 +02:00
|
|
|
sp: cx.sp,
|
|
|
|
fcx: cx};
|
2010-09-27 15:38:34 -07:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2010-10-04 15:55:12 -07:00
|
|
|
// Use this when you're at the top block of a function or the like.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn new_top_block_ctxt(fcx: &@fn_ctxt) -> @block_ctxt {
|
2011-08-26 21:34:56 -07:00
|
|
|
ret new_block_ctxt(fcx, parent_none, SCOPE_BLOCK, ~"function top level");
|
2010-12-02 19:12:34 -08:00
|
|
|
}
|
2010-10-04 15:55:12 -07:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2010-12-02 19:12:34 -08:00
|
|
|
// Use this when you're at a curly-brace or similar lexical scope.
|
2011-08-26 21:34:56 -07:00
|
|
|
fn new_scope_block_ctxt(bcx: &@block_ctxt, n: &istr) -> @block_ctxt {
|
2011-01-24 15:26:10 -08:00
|
|
|
ret new_block_ctxt(bcx.fcx, parent_some(bcx), SCOPE_BLOCK, n);
|
2010-10-04 15:55:12 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn new_loop_scope_block_ctxt(bcx: &@block_ctxt,
|
2011-08-12 07:15:18 -07:00
|
|
|
_cont: &option::t<@block_ctxt>,
|
2011-08-26 21:34:56 -07:00
|
|
|
_break: &@block_ctxt, n: &istr) -> @block_ctxt {
|
2011-03-25 16:28:16 +01:00
|
|
|
ret new_block_ctxt(bcx.fcx, parent_some(bcx),
|
|
|
|
LOOP_SCOPE_BLOCK(_cont, _break), n);
|
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2010-12-02 19:12:34 -08:00
|
|
|
// Use this when you're making a general CFG BB within a scope.
|
2011-08-26 21:34:56 -07:00
|
|
|
fn new_sub_block_ctxt(bcx: &@block_ctxt, n: &istr) -> @block_ctxt {
|
2011-01-24 15:26:10 -08:00
|
|
|
ret new_block_ctxt(bcx.fcx, parent_some(bcx), NON_SCOPE_BLOCK, n);
|
2010-10-04 15:55:12 -07:00
|
|
|
}
|
2010-09-29 17:22:07 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn new_raw_block_ctxt(fcx: &@fn_ctxt, llbb: BasicBlockRef) -> @block_ctxt {
|
|
|
|
ret @{llbb: llbb,
|
2011-08-24 14:54:55 +02:00
|
|
|
mutable terminated: false,
|
2011-07-27 14:19:39 +02:00
|
|
|
parent: parent_none,
|
|
|
|
kind: NON_SCOPE_BLOCK,
|
2011-08-24 14:54:55 +02:00
|
|
|
mutable cleanups: [],
|
2011-07-27 14:19:39 +02:00
|
|
|
sp: fcx.sp,
|
|
|
|
fcx: fcx};
|
2011-05-11 11:56:49 -07:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-05-27 17:58:22 -07:00
|
|
|
// trans_block_cleanups: Go through all the cleanups attached to this
|
2011-07-13 15:44:09 -07:00
|
|
|
// block_ctxt and execute them.
|
2011-05-27 17:58:22 -07:00
|
|
|
//
|
|
|
|
// When translating a block that introdces new variables during its scope, we
|
|
|
|
// need to make sure those variables go out of scope when the block ends. We
|
|
|
|
// do that by running a 'cleanup' function for each variable.
|
|
|
|
// trans_block_cleanups runs all the cleanup functions for the block.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_block_cleanups(cx: &@block_ctxt, cleanup_cx: &@block_ctxt) ->
|
|
|
|
@block_ctxt {
|
|
|
|
let bcx = cx;
|
|
|
|
if cleanup_cx.kind == NON_SCOPE_BLOCK {
|
2011-08-13 00:09:25 -07:00
|
|
|
assert (std::vec::len::<cleanup>(cleanup_cx.cleanups) == 0u);
|
2010-12-02 19:12:34 -08:00
|
|
|
}
|
2011-08-13 00:09:25 -07:00
|
|
|
let i = std::vec::len::<cleanup>(cleanup_cx.cleanups);
|
2011-07-27 14:19:39 +02:00
|
|
|
while i > 0u {
|
2011-01-31 12:06:27 -08:00
|
|
|
i -= 1u;
|
2011-08-19 15:16:48 -07:00
|
|
|
let c = cleanup_cx.cleanups[i];
|
2011-07-27 14:19:39 +02:00
|
|
|
alt c {
|
|
|
|
clean(cfn) { bcx = cfn(bcx).bcx; }
|
|
|
|
clean_temp(_, cfn) { bcx = cfn(bcx).bcx; }
|
Make moving of temporaries do the right thing, use it to optimize
This adds support for dropping cleanups for temporary values when they
are moved somewhere else. It then adds wraps most copy operations
(return, put in data structure, box, etc) in a way that will fall back
to a move when it is safe.
This saves a lot of taking/dropping, shaving over a megabyte off the
stage2/rustc binary size.
In some cases, most notably function returns, we could detect that the
returned value is a local variable, and can thus be safely moved even
though it is not a temporary. This will require putting some more
information in lvals.
I did not yet handle function arguments, since the logic for passing
them looked too convoluted to touch. I'll probably try that in the
near future, since it's bound to be a big win.
2011-07-07 13:36:12 +02:00
|
|
|
}
|
2010-09-29 17:22:07 -07:00
|
|
|
}
|
2010-10-04 15:55:12 -07:00
|
|
|
ret bcx;
|
|
|
|
}
|
|
|
|
|
2011-08-24 14:54:55 +02:00
|
|
|
fn trans_fn_cleanups(fcx: &@fn_ctxt, cx: &@block_ctxt) {
|
2011-08-17 17:49:54 -07:00
|
|
|
alt fcx.llobstacktoken {
|
2011-08-19 15:16:48 -07:00
|
|
|
some(lltoken_) {
|
|
|
|
let lltoken = lltoken_; // satisfy alias checker
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(cx, fcx_ccx(fcx).upcalls.dynastack_free,
|
2011-08-24 14:54:55 +02:00
|
|
|
[fcx.lltaskptr, lltoken]);
|
2011-08-19 15:16:48 -07:00
|
|
|
}
|
|
|
|
none. {/* nothing to do */ }
|
2011-08-17 17:27:31 -07:00
|
|
|
}
|
2011-08-17 13:58:49 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
iter block_locals(b: &ast::blk) -> @ast::local {
|
2011-08-19 15:16:48 -07:00
|
|
|
|
2010-10-19 14:54:10 -07:00
|
|
|
// FIXME: putting from inside an iter block doesn't work, so we can't
|
|
|
|
// use the index here.
|
2011-08-15 21:54:52 -07:00
|
|
|
for s: @ast::stmt in b.node.stmts {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt s.node {
|
|
|
|
ast::stmt_decl(d, _) {
|
|
|
|
alt d.node {
|
|
|
|
ast::decl_local(locals) {
|
2011-07-28 12:01:45 +02:00
|
|
|
for local: @ast::local in locals { put local; }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ {/* fall through */ }
|
2010-10-19 14:54:10 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ {/* fall through */ }
|
2010-10-19 14:54:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn llstaticallocas_block_ctxt(fcx: &@fn_ctxt) -> @block_ctxt {
|
|
|
|
ret @{llbb: fcx.llstaticallocas,
|
2011-08-24 14:54:55 +02:00
|
|
|
mutable terminated: false,
|
2011-07-27 14:19:39 +02:00
|
|
|
parent: parent_none,
|
|
|
|
kind: SCOPE_BLOCK,
|
2011-08-24 14:54:55 +02:00
|
|
|
mutable cleanups: [],
|
2011-07-27 14:19:39 +02:00
|
|
|
sp: fcx.sp,
|
|
|
|
fcx: fcx};
|
2011-06-18 18:43:07 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn llderivedtydescs_block_ctxt(fcx: &@fn_ctxt) -> @block_ctxt {
|
|
|
|
ret @{llbb: fcx.llderivedtydescs,
|
2011-08-24 14:54:55 +02:00
|
|
|
mutable terminated: false,
|
2011-07-27 14:19:39 +02:00
|
|
|
parent: parent_none,
|
|
|
|
kind: SCOPE_BLOCK,
|
2011-08-24 14:54:55 +02:00
|
|
|
mutable cleanups: [],
|
2011-07-27 14:19:39 +02:00
|
|
|
sp: fcx.sp,
|
|
|
|
fcx: fcx};
|
2011-06-18 18:43:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-22 12:45:18 +02:00
|
|
|
fn alloc_ty(cx: &@block_ctxt, t: ty::t) -> result {
|
2011-08-11 14:33:40 -07:00
|
|
|
let bcx = cx;
|
2011-07-27 14:19:39 +02:00
|
|
|
let val = C_int(0);
|
2011-08-11 14:33:40 -07:00
|
|
|
if ty::type_has_dynamic_size(bcx_tcx(bcx), t) {
|
2011-03-28 18:04:52 -07:00
|
|
|
// NB: we have to run this particular 'size_of' in a
|
2011-06-18 18:43:07 -07:00
|
|
|
// block_ctxt built on the llderivedtydescs block for the fn,
|
2011-03-28 18:04:52 -07:00
|
|
|
// so that the size dominates the array_alloca that
|
|
|
|
// comes next.
|
|
|
|
|
2011-08-11 14:33:40 -07:00
|
|
|
let n = size_of(llderivedtydescs_block_ctxt(bcx.fcx), t);
|
|
|
|
bcx.fcx.llderivedtydescs = n.bcx.llbb;
|
|
|
|
val = array_alloca(bcx, T_i8(), n.val);
|
2011-08-19 15:16:48 -07:00
|
|
|
} else { val = alloca(bcx, type_of(bcx_ccx(cx), cx.sp, t)); }
|
2011-03-28 18:04:52 -07:00
|
|
|
// NB: since we've pushed all size calculations in this
|
|
|
|
// function up to the alloca block, we actually return the
|
|
|
|
// block passed into us unmodified; it doesn't really
|
|
|
|
// have to be passed-and-returned here, but it fits
|
|
|
|
// past caller conventions and may well make sense again,
|
|
|
|
// so we leave it as-is.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
if bcx_tcx(cx).sess.get_opts().do_gc {
|
2011-08-18 18:13:36 -07:00
|
|
|
bcx = gc::add_gc_root(bcx, val, t);
|
|
|
|
}
|
2011-08-11 14:33:40 -07:00
|
|
|
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(cx, val);
|
2011-01-21 07:58:16 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn alloc_local(cx: &@block_ctxt, local: &@ast::local) -> result {
|
|
|
|
let t = node_id_type(bcx_ccx(cx), local.node.id);
|
|
|
|
let r = alloc_ty(cx, t);
|
2011-07-28 12:01:45 +02:00
|
|
|
alt local.node.pat.node {
|
|
|
|
ast::pat_bind(ident) {
|
|
|
|
if bcx_ccx(cx).sess.get_opts().debuginfo {
|
2011-08-26 15:36:18 -07:00
|
|
|
let _: () = istr::as_buf(ident, { |buf|
|
|
|
|
llvm::LLVMSetValueName(r.val, buf)
|
|
|
|
});
|
2011-07-28 12:01:45 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
_ { }
|
2011-07-15 16:40:04 -07:00
|
|
|
}
|
2011-02-10 15:00:16 -08:00
|
|
|
ret r;
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_block(cx: &@block_ctxt, b: &ast::blk, output: &out_method) ->
|
|
|
|
result {
|
|
|
|
let bcx = cx;
|
2011-07-28 12:01:45 +02:00
|
|
|
for each local: @ast::local in block_locals(b) {
|
2011-06-15 12:17:51 +02:00
|
|
|
// FIXME Update bcx.sp
|
2011-07-28 12:01:45 +02:00
|
|
|
let r = alloc_local(bcx, local);
|
|
|
|
bcx = r.bcx;
|
|
|
|
bcx.fcx.lllocals.insert(local.node.id, r.val);
|
2010-10-19 14:54:10 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let r = rslt(bcx, C_nil());
|
2011-08-15 21:54:52 -07:00
|
|
|
for s: @ast::stmt in b.node.stmts {
|
2010-11-10 17:46:49 -08:00
|
|
|
r = trans_stmt(bcx, *s);
|
|
|
|
bcx = r.bcx;
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2010-11-12 10:15:40 -08:00
|
|
|
// If we hit a terminator, control won't go any further so
|
|
|
|
// we're in dead-code land. Stop here.
|
2011-07-27 14:19:39 +02:00
|
|
|
if is_terminated(bcx) { ret r; }
|
|
|
|
}
|
|
|
|
fn accept_out_method(expr: &@ast::expr) -> bool {
|
|
|
|
ret alt expr.node {
|
|
|
|
ast::expr_if(_, _, _) { true }
|
|
|
|
ast::expr_alt(_, _) { true }
|
|
|
|
ast::expr_block(_) { true }
|
|
|
|
_ { false }
|
2011-06-15 11:19:50 -07:00
|
|
|
};
|
2011-05-31 14:24:04 +02:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
alt b.node.expr {
|
|
|
|
some(e) {
|
|
|
|
let ccx = bcx_ccx(cx);
|
|
|
|
let r_ty = ty::expr_ty(ccx.tcx, e);
|
|
|
|
let pass = output != return && accept_out_method(e);
|
|
|
|
if pass {
|
|
|
|
r = trans_expr_out(bcx, e, output);
|
|
|
|
bcx = r.bcx;
|
|
|
|
if is_terminated(bcx) || ty::type_is_bot(ccx.tcx, r_ty) { ret r; }
|
|
|
|
} else {
|
|
|
|
let lv = trans_lval(bcx, e);
|
|
|
|
r = lv.res;
|
|
|
|
bcx = r.bcx;
|
|
|
|
if is_terminated(bcx) || ty::type_is_bot(ccx.tcx, r_ty) { ret r; }
|
|
|
|
alt output {
|
|
|
|
save_in(target) {
|
|
|
|
// The output method is to save the value at target,
|
|
|
|
// and we didn't pass it to the recursive trans_expr
|
|
|
|
// call.
|
2011-08-22 12:38:58 +02:00
|
|
|
bcx = move_val_if_temp(bcx, INIT, target, lv, r_ty);
|
2011-07-27 14:19:39 +02:00
|
|
|
r = rslt(bcx, C_nil());
|
|
|
|
}
|
|
|
|
return. { }
|
2010-11-29 17:11:03 -08:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
none. { r = rslt(bcx, C_nil()); }
|
2010-11-29 17:11:03 -08:00
|
|
|
}
|
2010-12-02 19:44:24 -08:00
|
|
|
bcx = trans_block_cleanups(bcx, find_scope_cx(bcx));
|
2011-06-24 19:04:08 +02:00
|
|
|
ret rslt(bcx, r.val);
|
2010-09-23 13:15:51 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn new_local_ctxt(ccx: &@crate_ctxt) -> @local_ctxt {
|
2011-08-26 18:25:46 -07:00
|
|
|
let pth: [istr] = [];
|
2011-07-27 14:19:39 +02:00
|
|
|
ret @{path: pth,
|
2011-08-26 18:25:46 -07:00
|
|
|
module_path: [ccx.link_meta.name],
|
2011-08-19 15:16:48 -07:00
|
|
|
obj_typarams: [],
|
|
|
|
obj_fields: [],
|
2011-07-27 14:19:39 +02:00
|
|
|
ccx: ccx};
|
2011-04-20 17:23:45 +02:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-18 18:43:07 -07:00
|
|
|
// Creates the standard quartet of basic blocks: static allocas, copy args,
|
|
|
|
// derived tydescs, and dynamic allocas.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn mk_standard_basic_blocks(llfn: ValueRef) ->
|
|
|
|
{sa: BasicBlockRef,
|
|
|
|
ca: BasicBlockRef,
|
|
|
|
dt: BasicBlockRef,
|
2011-08-17 17:49:54 -07:00
|
|
|
da: BasicBlockRef,
|
|
|
|
rt: BasicBlockRef} {
|
2011-08-26 15:36:18 -07:00
|
|
|
ret {sa: istr::as_buf(~"statuc_allocas", { |buf|
|
|
|
|
llvm::LLVMAppendBasicBlock(llfn, buf)
|
|
|
|
}),
|
|
|
|
ca: istr::as_buf(~"copy_args", { |buf|
|
|
|
|
llvm::LLVMAppendBasicBlock(llfn, buf)
|
|
|
|
}),
|
|
|
|
dt: istr::as_buf(~"derived_tydescs", { |buf|
|
|
|
|
llvm::LLVMAppendBasicBlock(llfn, buf)
|
|
|
|
}),
|
|
|
|
da: istr::as_buf(~"dynamic_allocas", { |buf|
|
|
|
|
llvm::LLVMAppendBasicBlock(llfn, buf)
|
|
|
|
}),
|
|
|
|
rt: istr::as_buf(~"return", { |buf|
|
|
|
|
llvm::LLVMAppendBasicBlock(llfn, buf)
|
|
|
|
})};
|
2011-05-11 11:56:49 -07:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-02-08 11:47:53 -08:00
|
|
|
// NB: must keep 4 fns in sync:
|
|
|
|
//
|
|
|
|
// - type_of_fn_full
|
|
|
|
// - create_llargs_for_fn_args.
|
|
|
|
// - new_fn_ctxt
|
|
|
|
// - trans_args
|
2011-08-02 15:13:08 -07:00
|
|
|
fn new_fn_ctxt_w_id(cx: @local_ctxt, sp: &span, llfndecl: ValueRef,
|
|
|
|
id: ast::node_id) -> @fn_ctxt {
|
2011-07-27 14:19:39 +02:00
|
|
|
let llretptr: ValueRef = llvm::LLVMGetParam(llfndecl, 0u);
|
|
|
|
let lltaskptr: ValueRef = llvm::LLVMGetParam(llfndecl, 1u);
|
|
|
|
let llenv: ValueRef = llvm::LLVMGetParam(llfndecl, 2u);
|
2011-08-13 00:09:25 -07:00
|
|
|
let llargs: hashmap<ast::node_id, ValueRef> = new_int_hash::<ValueRef>();
|
2011-08-12 07:15:18 -07:00
|
|
|
let llobjfields: hashmap<ast::node_id, ValueRef> =
|
2011-08-13 00:09:25 -07:00
|
|
|
new_int_hash::<ValueRef>();
|
|
|
|
let lllocals: hashmap<ast::node_id, ValueRef> =
|
|
|
|
new_int_hash::<ValueRef>();
|
|
|
|
let llupvars: hashmap<ast::node_id, ValueRef> =
|
|
|
|
new_int_hash::<ValueRef>();
|
2011-07-27 14:19:39 +02:00
|
|
|
let derived_tydescs =
|
2011-08-13 00:09:25 -07:00
|
|
|
map::mk_hashmap::<ty::t, derived_tydesc_info>(ty::hash_ty, ty::eq_ty);
|
2011-07-27 14:19:39 +02:00
|
|
|
let llbbs = mk_standard_basic_blocks(llfndecl);
|
|
|
|
ret @{llfn: llfndecl,
|
|
|
|
lltaskptr: lltaskptr,
|
|
|
|
llenv: llenv,
|
|
|
|
llretptr: llretptr,
|
|
|
|
mutable llstaticallocas: llbbs.sa,
|
|
|
|
mutable llcopyargs: llbbs.ca,
|
|
|
|
mutable llderivedtydescs_first: llbbs.dt,
|
|
|
|
mutable llderivedtydescs: llbbs.dt,
|
|
|
|
mutable lldynamicallocas: llbbs.da,
|
2011-08-17 17:49:54 -07:00
|
|
|
mutable llreturn: llbbs.rt,
|
2011-08-17 17:27:31 -07:00
|
|
|
mutable llobstacktoken: none::<ValueRef>,
|
2011-08-13 00:09:25 -07:00
|
|
|
mutable llself: none::<val_self_pair>,
|
|
|
|
mutable lliterbody: none::<ValueRef>,
|
|
|
|
mutable iterbodyty: none::<ty::t>,
|
2011-07-27 14:19:39 +02:00
|
|
|
llargs: llargs,
|
|
|
|
llobjfields: llobjfields,
|
|
|
|
lllocals: lllocals,
|
|
|
|
llupvars: llupvars,
|
2011-08-19 15:16:48 -07:00
|
|
|
mutable lltydescs: [],
|
2011-07-27 14:19:39 +02:00
|
|
|
derived_tydescs: derived_tydescs,
|
2011-08-02 15:13:08 -07:00
|
|
|
id: id,
|
2011-07-27 14:19:39 +02:00
|
|
|
sp: sp,
|
|
|
|
lcx: cx};
|
2010-09-27 15:38:34 -07:00
|
|
|
}
|
|
|
|
|
2011-08-02 15:13:08 -07:00
|
|
|
fn new_fn_ctxt(cx: @local_ctxt, sp: &span, llfndecl: ValueRef) -> @fn_ctxt {
|
|
|
|
be new_fn_ctxt_w_id(cx, sp, llfndecl, -1);
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-02-08 11:47:53 -08:00
|
|
|
// NB: must keep 4 fns in sync:
|
|
|
|
//
|
|
|
|
// - type_of_fn_full
|
|
|
|
// - create_llargs_for_fn_args.
|
|
|
|
// - new_fn_ctxt
|
|
|
|
// - trans_args
|
|
|
|
|
2011-06-01 11:34:52 -07:00
|
|
|
// create_llargs_for_fn_args: Creates a mapping from incoming arguments to
|
|
|
|
// allocas created for them.
|
|
|
|
//
|
|
|
|
// When we translate a function, we need to map its incoming arguments to the
|
|
|
|
// spaces that have been created for them (by code in the llallocas field of
|
|
|
|
// the function's fn_ctxt). create_llargs_for_fn_args populates the llargs
|
|
|
|
// field of the fn_ctxt with
|
2011-07-27 14:19:39 +02:00
|
|
|
fn create_llargs_for_fn_args(cx: &@fn_ctxt, proto: ast::proto,
|
2011-08-12 07:15:18 -07:00
|
|
|
ty_self: option::t<ty::t>, ret_ty: ty::t,
|
2011-08-04 16:20:09 -07:00
|
|
|
args: &[ast::arg], ty_params: &[ast::ty_param]) {
|
2011-06-01 11:34:52 -07:00
|
|
|
// Skip the implicit arguments 0, 1, and 2. TODO: Pull out 3u and define
|
|
|
|
// it as a constant, since we're using it in several places in trans this
|
|
|
|
// way.
|
2011-07-27 14:19:39 +02:00
|
|
|
let arg_n = 3u;
|
|
|
|
alt ty_self {
|
2011-08-13 00:09:25 -07:00
|
|
|
some(tt) { cx.llself = some::<val_self_pair>({v: cx.llenv, t: tt}); }
|
2011-07-27 14:19:39 +02:00
|
|
|
none. {
|
|
|
|
let i = 0u;
|
2011-08-15 21:54:52 -07:00
|
|
|
for tp: ast::ty_param in ty_params {
|
2011-07-27 14:19:39 +02:00
|
|
|
let llarg = llvm::LLVMGetParam(cx.llfn, arg_n);
|
|
|
|
assert (llarg as int != 0);
|
2011-08-19 15:16:48 -07:00
|
|
|
cx.lltydescs += [llarg];
|
2011-07-27 14:19:39 +02:00
|
|
|
arg_n += 1u;
|
|
|
|
i += 1u;
|
2011-02-09 09:54:58 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2010-12-20 14:33:11 -08:00
|
|
|
}
|
2011-08-02 16:24:38 -07:00
|
|
|
|
2011-06-01 11:34:52 -07:00
|
|
|
// If the function is actually an iter, populate the lliterbody field of
|
|
|
|
// the function context with the ValueRef that we get from
|
|
|
|
// llvm::LLVMGetParam for the iter's body.
|
2011-07-27 14:19:39 +02:00
|
|
|
if proto == ast::proto_iter {
|
2011-08-02 16:24:38 -07:00
|
|
|
cx.iterbodyty = some(ty::mk_iter_body_fn(fcx_tcx(cx), ret_ty));
|
2011-07-27 14:19:39 +02:00
|
|
|
let llarg = llvm::LLVMGetParam(cx.llfn, arg_n);
|
2011-05-02 17:47:24 -07:00
|
|
|
assert (llarg as int != 0);
|
2011-08-13 00:09:25 -07:00
|
|
|
cx.lliterbody = some::<ValueRef>(llarg);
|
2011-02-17 12:20:55 -08:00
|
|
|
arg_n += 1u;
|
|
|
|
}
|
2010-12-23 17:31:16 -08:00
|
|
|
|
2011-06-01 11:34:52 -07:00
|
|
|
// Populate the llargs field of the function context with the ValueRefs
|
|
|
|
// that we get from llvm::LLVMGetParam for each argument.
|
2011-08-15 21:54:52 -07:00
|
|
|
for arg: ast::arg in args {
|
2011-07-27 14:19:39 +02:00
|
|
|
let llarg = llvm::LLVMGetParam(cx.llfn, arg_n);
|
2011-05-02 17:47:24 -07:00
|
|
|
assert (llarg as int != 0);
|
2010-12-09 17:38:17 -08:00
|
|
|
cx.llargs.insert(arg.id, llarg);
|
|
|
|
arg_n += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-19 14:34:45 +02:00
|
|
|
fn copy_args_to_allocas(fcx: @fn_ctxt, scope: @block_ctxt,
|
|
|
|
args: &[ast::arg], arg_tys: &[ty::arg]) {
|
2011-07-27 14:19:39 +02:00
|
|
|
let bcx = new_raw_block_ctxt(fcx, fcx.llcopyargs);
|
|
|
|
let arg_n: uint = 0u;
|
2011-08-15 21:54:52 -07:00
|
|
|
for aarg: ast::arg in args {
|
2011-08-19 14:34:45 +02:00
|
|
|
let arg_ty = arg_tys[arg_n].ty;
|
|
|
|
alt aarg.mode {
|
|
|
|
ast::val. {
|
|
|
|
// Structural types are passed by pointer, and we use the
|
|
|
|
// pointed-to memory for the local.
|
2011-08-22 13:19:07 +02:00
|
|
|
if !type_is_structural_or_param(fcx_tcx(fcx), arg_ty) {
|
2011-08-19 14:34:45 +02:00
|
|
|
// Overwrite the llargs entry for this arg with its alloca.
|
|
|
|
let aval = bcx.fcx.llargs.get(aarg.id);
|
|
|
|
let addr = do_spill(bcx, aval);
|
|
|
|
bcx.fcx.llargs.insert(aarg.id, addr);
|
|
|
|
|
|
|
|
// Args that are locally assigned to need to do a local
|
|
|
|
// take/drop
|
|
|
|
if fcx.lcx.ccx.mut_map.contains_key(aarg.id) {
|
2011-08-26 11:20:10 +02:00
|
|
|
bcx = take_ty(bcx, addr, arg_ty).bcx;
|
2011-08-19 14:34:45 +02:00
|
|
|
add_clean(scope, addr, arg_ty);
|
|
|
|
}
|
2011-08-19 12:48:45 +02:00
|
|
|
}
|
2011-08-19 14:34:45 +02:00
|
|
|
}
|
|
|
|
ast::move. {
|
|
|
|
add_clean(scope, bcx.fcx.llargs.get(aarg.id), arg_ty);
|
|
|
|
}
|
|
|
|
_ {}
|
2010-12-07 12:34:10 -08:00
|
|
|
}
|
2010-11-26 17:47:27 -08:00
|
|
|
arg_n += 1u;
|
|
|
|
}
|
2011-08-19 12:48:45 +02:00
|
|
|
fcx.llcopyargs = bcx.llbb;
|
2010-11-26 17:47:27 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn is_terminated(cx: &@block_ctxt) -> bool {
|
|
|
|
let inst = llvm::LLVMGetLastInstruction(cx.llbb);
|
2011-05-12 17:24:54 +02:00
|
|
|
ret llvm::LLVMIsATerminatorInst(inst) as int != 0;
|
2010-11-10 17:46:49 -08:00
|
|
|
}
|
|
|
|
|
2011-08-04 16:20:09 -07:00
|
|
|
fn arg_tys_of_fn(ccx: &@crate_ctxt, id: ast::node_id) -> [ty::arg] {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt ty::struct(ccx.tcx, ty::node_id_to_type(ccx.tcx, id)) {
|
|
|
|
ty::ty_fn(_, arg_tys, _, _, _) { ret arg_tys; }
|
2010-12-06 11:22:08 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn populate_fn_ctxt_from_llself(fcx: @fn_ctxt, llself: val_self_pair) {
|
|
|
|
let bcx = llstaticallocas_block_ctxt(fcx);
|
2011-08-19 15:16:48 -07:00
|
|
|
let field_tys: [ty::t] = [];
|
2011-08-15 21:54:52 -07:00
|
|
|
for f: ast::obj_field in bcx.fcx.lcx.obj_fields {
|
2011-08-19 15:16:48 -07:00
|
|
|
field_tys += [node_id_type(bcx_ccx(bcx), f.id)];
|
2010-12-30 17:01:20 -08:00
|
|
|
}
|
2011-03-03 11:49:35 -08:00
|
|
|
// Synthesize a tuple type for the fields so that GEP_tup_like() can work
|
|
|
|
// its magic.
|
|
|
|
|
2011-08-15 12:08:05 +02:00
|
|
|
let fields_tup_ty = ty::mk_tup(fcx.lcx.ccx.tcx, field_tys);
|
2011-08-13 00:09:25 -07:00
|
|
|
let n_typarams = std::vec::len::<ast::ty_param>(bcx.fcx.lcx.obj_typarams);
|
2011-07-27 14:19:39 +02:00
|
|
|
let llobj_box_ty: TypeRef = T_obj_ptr(*bcx_ccx(bcx), n_typarams);
|
|
|
|
let box_cell =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(bcx, llself.v, [C_int(0), C_int(abi::obj_field_box)]);
|
|
|
|
let box_ptr = Load(bcx, box_cell);
|
|
|
|
box_ptr = PointerCast(bcx, box_ptr, llobj_box_ty);
|
2011-07-27 14:19:39 +02:00
|
|
|
let obj_typarams =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(bcx, box_ptr,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::box_rc_field_body),
|
|
|
|
C_int(abi::obj_body_elt_typarams)]);
|
2011-03-03 11:49:35 -08:00
|
|
|
// The object fields immediately follow the type parameters, so we skip
|
|
|
|
// over them to get the pointer.
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let et = llvm::LLVMGetElementType(val_ty(obj_typarams));
|
2011-08-30 09:59:30 +02:00
|
|
|
let obj_fields = Add(bcx, vp2i(bcx, obj_typarams), llsize_of(et));
|
2011-03-03 11:49:35 -08:00
|
|
|
// If we can (i.e. the type is statically sized), then cast the resulting
|
|
|
|
// fields pointer to the appropriate LLVM type. If not, just leave it as
|
|
|
|
// i8 *.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
if !ty::type_has_dynamic_size(fcx.lcx.ccx.tcx, fields_tup_ty) {
|
|
|
|
let llfields_ty = type_of(fcx.lcx.ccx, fcx.sp, fields_tup_ty);
|
2011-03-03 11:49:35 -08:00
|
|
|
obj_fields = vi2p(bcx, obj_fields, T_ptr(llfields_ty));
|
2011-06-15 11:19:50 -07:00
|
|
|
} else { obj_fields = vi2p(bcx, obj_fields, T_ptr(T_i8())); }
|
2011-07-27 14:19:39 +02:00
|
|
|
let i: int = 0;
|
2011-08-15 21:54:52 -07:00
|
|
|
for p: ast::ty_param in fcx.lcx.obj_typarams {
|
2011-07-27 14:19:39 +02:00
|
|
|
let lltyparam: ValueRef =
|
2011-08-30 09:59:30 +02:00
|
|
|
GEP(bcx, obj_typarams, [C_int(0), C_int(i)]);
|
|
|
|
lltyparam = Load(bcx, lltyparam);
|
2011-08-19 15:16:48 -07:00
|
|
|
fcx.lltydescs += [lltyparam];
|
2011-02-03 14:40:57 -08:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
i = 0;
|
2011-08-15 21:54:52 -07:00
|
|
|
for f: ast::obj_field in fcx.lcx.obj_fields {
|
2011-08-19 15:16:48 -07:00
|
|
|
let rslt = GEP_tup_like(bcx, fields_tup_ty, obj_fields, [0, i]);
|
2011-06-18 18:43:07 -07:00
|
|
|
bcx = llstaticallocas_block_ctxt(fcx);
|
2011-07-27 14:19:39 +02:00
|
|
|
let llfield = rslt.val;
|
2011-03-28 18:04:52 -07:00
|
|
|
fcx.llobjfields.insert(f.id, llfield);
|
2010-12-30 17:01:20 -08:00
|
|
|
i += 1;
|
|
|
|
}
|
2011-06-18 18:43:07 -07:00
|
|
|
fcx.llstaticallocas = bcx.llbb;
|
2010-12-30 17:01:20 -08:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-18 18:43:07 -07:00
|
|
|
// Ties up the llstaticallocas -> llcopyargs -> llderivedtydescs ->
|
2011-08-17 17:49:54 -07:00
|
|
|
// lldynamicallocas -> lltop edges, and builds the return block.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn finish_fn(fcx: &@fn_ctxt, lltop: BasicBlockRef) {
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(new_raw_block_ctxt(fcx, fcx.llstaticallocas), fcx.llcopyargs);
|
|
|
|
Br(new_raw_block_ctxt(fcx, fcx.llcopyargs),
|
2011-08-24 14:54:55 +02:00
|
|
|
fcx.llderivedtydescs_first);
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(new_raw_block_ctxt(fcx, fcx.llderivedtydescs),
|
2011-08-24 14:54:55 +02:00
|
|
|
fcx.lldynamicallocas);
|
2011-08-30 09:59:30 +02:00
|
|
|
Br(new_raw_block_ctxt(fcx, fcx.lldynamicallocas), lltop);
|
2011-08-17 17:49:54 -07:00
|
|
|
|
2011-08-24 14:54:55 +02:00
|
|
|
let ret_cx = new_raw_block_ctxt(fcx, fcx.llreturn);
|
|
|
|
trans_fn_cleanups(fcx, ret_cx);
|
2011-08-30 09:59:30 +02:00
|
|
|
RetVoid(ret_cx);
|
2011-05-11 11:56:49 -07:00
|
|
|
}
|
|
|
|
|
2011-06-29 19:50:50 -07:00
|
|
|
// trans_closure: Builds an LLVM function out of a source function.
|
|
|
|
// If the function closes over its environment a closure will be
|
|
|
|
// returned.
|
2011-08-12 07:15:18 -07:00
|
|
|
fn trans_closure(bcx_maybe: &option::t<@block_ctxt>,
|
|
|
|
llfnty: &option::t<TypeRef>, cx: @local_ctxt, sp: &span,
|
|
|
|
f: &ast::_fn, llfndecl: ValueRef, ty_self: option::t<ty::t>,
|
2011-08-19 15:16:48 -07:00
|
|
|
ty_params: &[ast::ty_param], id: ast::node_id) ->
|
|
|
|
option::t<{fn_pair: ValueRef, bcx: @block_ctxt}> {
|
2011-05-24 13:47:27 -04:00
|
|
|
set_uwtable(llfndecl);
|
2010-11-26 17:47:27 -08:00
|
|
|
|
2011-06-28 18:54:05 -07:00
|
|
|
// Set up arguments to the function.
|
2011-08-02 15:13:08 -07:00
|
|
|
let fcx = new_fn_ctxt_w_id(cx, sp, llfndecl, id);
|
2011-06-15 11:19:50 -07:00
|
|
|
create_llargs_for_fn_args(fcx, f.proto, ty_self,
|
2011-07-27 14:19:39 +02:00
|
|
|
ty::ret_ty_of_fn(cx.ccx.tcx, id), f.decl.inputs,
|
|
|
|
ty_params);
|
|
|
|
alt { fcx.llself } {
|
|
|
|
some(llself) { populate_fn_ctxt_from_llself(fcx, llself); }
|
|
|
|
_ { }
|
2010-12-30 17:01:20 -08:00
|
|
|
}
|
2011-08-19 14:34:45 +02:00
|
|
|
|
|
|
|
// Create the first basic block in the function and keep a handle on it to
|
|
|
|
// pass to finish_fn later.
|
|
|
|
let bcx = new_top_block_ctxt(fcx);
|
|
|
|
let lltop = bcx.llbb;
|
|
|
|
let block_ty = node_id_type(cx.ccx, f.body.node.id);
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let arg_tys = arg_tys_of_fn(fcx.lcx.ccx, id);
|
2011-08-19 14:34:45 +02:00
|
|
|
copy_args_to_allocas(fcx, bcx, f.decl.inputs, arg_tys);
|
2011-06-28 18:54:05 -07:00
|
|
|
|
2011-06-29 19:50:50 -07:00
|
|
|
// Figure out if we need to build a closure and act accordingly
|
2011-08-19 15:16:48 -07:00
|
|
|
let res =
|
|
|
|
alt f.proto {
|
|
|
|
ast::proto_block. | ast::proto_closure. {
|
|
|
|
let bcx = option::get(bcx_maybe);
|
|
|
|
let upvars = get_freevars(cx.ccx.tcx, id);
|
|
|
|
|
|
|
|
let copying = f.proto == ast::proto_closure;
|
|
|
|
let env = build_closure(bcx, upvars, copying);
|
|
|
|
load_environment(bcx, fcx, env.ptrty, upvars, copying);
|
|
|
|
|
|
|
|
let closure =
|
|
|
|
create_real_fn_pair(env.bcx, option::get(llfnty), llfndecl,
|
|
|
|
env.ptr);
|
|
|
|
if copying {
|
|
|
|
add_clean_temp(bcx, closure, node_id_type(cx.ccx, id))
|
|
|
|
}
|
|
|
|
some({fn_pair: closure, bcx: env.bcx})
|
|
|
|
}
|
|
|
|
_ { none }
|
|
|
|
};
|
2011-06-29 19:50:50 -07:00
|
|
|
|
2011-06-28 18:54:05 -07:00
|
|
|
|
2011-08-16 21:34:52 +02:00
|
|
|
// This call to trans_block is the place where we bridge between
|
|
|
|
// translation calls that don't have a return value (trans_crate,
|
|
|
|
// trans_mod, trans_item, trans_obj, et cetera) and those that do
|
|
|
|
// (trans_block, trans_expr, et cetera).
|
|
|
|
let rslt =
|
|
|
|
if !ty::type_is_nil(cx.ccx.tcx, block_ty) &&
|
2011-08-19 15:16:48 -07:00
|
|
|
!ty::type_is_bot(cx.ccx.tcx, block_ty) &&
|
|
|
|
f.proto != ast::proto_iter {
|
|
|
|
trans_block(bcx, f.body, save_in(fcx.llretptr))
|
|
|
|
} else { trans_block(bcx, f.body, return) };
|
2011-08-16 21:34:52 +02:00
|
|
|
bcx = rslt.bcx;
|
2011-07-15 11:38:16 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
if !is_terminated(bcx) {
|
2010-11-14 13:04:01 -08:00
|
|
|
// FIXME: until LLVM has a unit type, we are moving around
|
|
|
|
// C_nil values rather than their void type.
|
2011-08-17 17:49:54 -07:00
|
|
|
build_return(bcx);
|
2010-11-10 17:46:49 -08:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-29 17:29:24 -07:00
|
|
|
// Insert the mandatory first few basic blocks before lltop.
|
2011-05-11 11:56:49 -07:00
|
|
|
finish_fn(fcx, lltop);
|
2011-06-29 19:50:50 -07:00
|
|
|
|
2011-08-01 12:09:59 -07:00
|
|
|
ret res;
|
2011-06-29 19:50:50 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_fn_inner(cx: @local_ctxt, sp: &span, f: &ast::_fn,
|
2011-08-12 07:15:18 -07:00
|
|
|
llfndecl: ValueRef, ty_self: option::t<ty::t>,
|
2011-08-04 16:20:09 -07:00
|
|
|
ty_params: &[ast::ty_param], id: ast::node_id) {
|
2011-06-29 19:50:50 -07:00
|
|
|
trans_closure(none, none, cx, sp, f, llfndecl, ty_self, ty_params, id);
|
2010-09-22 17:05:38 -07:00
|
|
|
}
|
|
|
|
|
2011-07-19 11:56:46 -07:00
|
|
|
|
|
|
|
// trans_fn: creates an LLVM function corresponding to a source language
|
|
|
|
// function.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_fn(cx: @local_ctxt, sp: &span, f: &ast::_fn, llfndecl: ValueRef,
|
2011-08-12 07:15:18 -07:00
|
|
|
ty_self: option::t<ty::t>, ty_params: &[ast::ty_param],
|
2011-07-27 14:19:39 +02:00
|
|
|
id: ast::node_id) {
|
2011-07-19 11:56:46 -07:00
|
|
|
if !cx.ccx.sess.get_opts().stats {
|
|
|
|
trans_fn_inner(cx, sp, f, llfndecl, ty_self, ty_params, id);
|
2011-07-20 18:43:05 -07:00
|
|
|
ret;
|
2011-07-19 11:56:46 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let start = time::get_time();
|
2011-07-19 11:56:46 -07:00
|
|
|
trans_fn_inner(cx, sp, f, llfndecl, ty_self, ty_params, id);
|
2011-07-27 14:19:39 +02:00
|
|
|
let end = time::get_time();
|
2011-08-26 21:34:56 -07:00
|
|
|
log_fn_time(cx.ccx, istr::connect(cx.path, ~"::"),
|
2011-08-26 18:25:46 -07:00
|
|
|
start, end);
|
2011-07-19 11:56:46 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_res_ctor(cx: @local_ctxt, sp: &span, dtor: &ast::_fn,
|
2011-08-04 16:20:09 -07:00
|
|
|
ctor_id: ast::node_id, ty_params: &[ast::ty_param]) {
|
2011-06-30 12:35:19 +02:00
|
|
|
// Create a function for the constructor
|
2011-07-27 14:19:39 +02:00
|
|
|
let llctor_decl;
|
|
|
|
alt cx.ccx.item_ids.find(ctor_id) {
|
|
|
|
some(x) { llctor_decl = x; }
|
2011-08-27 16:36:48 -07:00
|
|
|
_ { cx.ccx.sess.span_fatal(sp, ~"unbound ctor_id in trans_res_ctor"); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
let fcx = new_fn_ctxt(cx, sp, llctor_decl);
|
|
|
|
let ret_t = ty::ret_ty_of_fn(cx.ccx.tcx, ctor_id);
|
2011-08-13 00:09:25 -07:00
|
|
|
create_llargs_for_fn_args(fcx, ast::proto_fn, none::<ty::t>, ret_t,
|
2011-07-27 14:19:39 +02:00
|
|
|
dtor.decl.inputs, ty_params);
|
|
|
|
let bcx = new_top_block_ctxt(fcx);
|
|
|
|
let lltop = bcx.llbb;
|
2011-08-19 15:16:48 -07:00
|
|
|
let arg_t = arg_tys_of_fn(cx.ccx, ctor_id)[0].ty;
|
|
|
|
let tup_t = ty::mk_tup(cx.ccx.tcx, [ty::mk_int(cx.ccx.tcx), arg_t]);
|
2011-07-27 14:19:39 +02:00
|
|
|
let arg;
|
2011-08-19 15:16:48 -07:00
|
|
|
alt fcx.llargs.find(dtor.decl.inputs[0].id) {
|
2011-07-27 14:19:39 +02:00
|
|
|
some(x) { arg = load_if_immediate(bcx, x, arg_t); }
|
2011-08-27 16:36:48 -07:00
|
|
|
_ { cx.ccx.sess.span_fatal(
|
|
|
|
sp, ~"unbound dtor decl in trans_res_ctor"); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
let llretptr = fcx.llretptr;
|
|
|
|
if ty::type_has_dynamic_size(cx.ccx.tcx, ret_t) {
|
2011-08-19 15:16:48 -07:00
|
|
|
let llret_t = T_ptr(T_struct([T_i32(), llvm::LLVMTypeOf(arg)]));
|
2011-08-30 09:59:30 +02:00
|
|
|
llretptr = BitCast(bcx, llretptr, llret_t);
|
2011-06-30 14:46:17 +02:00
|
|
|
}
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
let dst = GEP_tup_like(bcx, tup_t, llretptr, [0, 1]);
|
2011-06-28 16:14:01 +02:00
|
|
|
bcx = dst.bcx;
|
2011-08-22 12:38:58 +02:00
|
|
|
bcx = copy_val(bcx, INIT, dst.val, arg, arg_t);
|
2011-08-19 15:16:48 -07:00
|
|
|
let flag = GEP_tup_like(bcx, tup_t, llretptr, [0, 0]);
|
2011-06-28 16:14:01 +02:00
|
|
|
bcx = flag.bcx;
|
2011-08-30 09:59:30 +02:00
|
|
|
Store(bcx, C_int(1), flag.val);
|
2011-08-17 17:49:54 -07:00
|
|
|
build_return(bcx);
|
2011-06-24 18:10:40 +02:00
|
|
|
finish_fn(fcx, lltop);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_tag_variant(cx: @local_ctxt, tag_id: ast::node_id,
|
|
|
|
variant: &ast::variant, index: int, is_degen: bool,
|
2011-08-04 16:20:09 -07:00
|
|
|
ty_params: &[ast::ty_param]) {
|
2011-08-13 00:09:25 -07:00
|
|
|
if std::vec::len::<ast::variant_arg>(variant.node.args) == 0u {
|
2011-06-15 11:19:50 -07:00
|
|
|
ret; // nullary constructors are just constants
|
2010-12-01 19:03:47 -08:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
}
|
2010-12-06 16:50:24 -08:00
|
|
|
// Translate variant arguments to function arguments.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
let fn_args: [ast::arg] = [];
|
2011-07-27 14:19:39 +02:00
|
|
|
let i = 0u;
|
2011-08-15 21:54:52 -07:00
|
|
|
for varg: ast::variant_arg in variant.node.args {
|
2011-06-15 11:19:50 -07:00
|
|
|
fn_args +=
|
2011-08-19 15:16:48 -07:00
|
|
|
[{mode: ast::alias(false),
|
|
|
|
ty: varg.ty,
|
2011-08-25 17:00:12 -07:00
|
|
|
ident: ~"arg" + uint::to_str(i, 10u),
|
2011-08-19 15:16:48 -07:00
|
|
|
id: varg.id}];
|
2010-12-06 16:50:24 -08:00
|
|
|
}
|
2011-05-02 17:47:24 -07:00
|
|
|
assert (cx.ccx.item_ids.contains_key(variant.node.id));
|
2011-07-27 14:19:39 +02:00
|
|
|
let llfndecl: ValueRef;
|
|
|
|
alt cx.ccx.item_ids.find(variant.node.id) {
|
|
|
|
some(x) { llfndecl = x; }
|
|
|
|
_ {
|
|
|
|
cx.ccx.sess.span_fatal(variant.span,
|
2011-08-27 16:36:48 -07:00
|
|
|
~"unbound variant id in trans_tag_variant");
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-07-05 19:57:34 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let fcx = new_fn_ctxt(cx, variant.span, llfndecl);
|
2011-08-13 00:09:25 -07:00
|
|
|
create_llargs_for_fn_args(fcx, ast::proto_fn, none::<ty::t>,
|
2011-06-19 22:41:21 +02:00
|
|
|
ty::ret_ty_of_fn(cx.ccx.tcx, variant.node.id),
|
|
|
|
fn_args, ty_params);
|
2011-08-19 15:16:48 -07:00
|
|
|
let ty_param_substs: [ty::t] = [];
|
2011-04-12 15:09:50 -07:00
|
|
|
i = 0u;
|
2011-08-15 21:54:52 -07:00
|
|
|
for tp: ast::ty_param in ty_params {
|
2011-08-19 15:16:48 -07:00
|
|
|
ty_param_substs += [ty::mk_param(cx.ccx.tcx, i, tp.kind)];
|
2011-04-12 15:09:50 -07:00
|
|
|
i += 1u;
|
2011-03-09 17:48:07 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let arg_tys = arg_tys_of_fn(cx.ccx, variant.node.id);
|
|
|
|
let bcx = new_top_block_ctxt(fcx);
|
2011-08-19 14:34:45 +02:00
|
|
|
copy_args_to_allocas(fcx, bcx, fn_args, arg_tys);
|
2011-07-27 14:19:39 +02:00
|
|
|
let lltop = bcx.llbb;
|
2011-03-03 15:52:54 -08:00
|
|
|
|
2011-07-27 14:48:34 +02:00
|
|
|
// Cast the tag to a type we can GEP into.
|
|
|
|
let llblobptr =
|
2011-07-27 14:19:39 +02:00
|
|
|
if is_degen {
|
|
|
|
fcx.llretptr
|
|
|
|
} else {
|
|
|
|
let lltagptr =
|
2011-08-30 09:59:30 +02:00
|
|
|
PointerCast(bcx, fcx.llretptr,
|
2011-07-27 14:19:39 +02:00
|
|
|
T_opaque_tag_ptr(fcx.lcx.ccx.tn));
|
2011-08-30 09:59:30 +02:00
|
|
|
let lldiscrimptr = GEP(bcx, lltagptr, [C_int(0), C_int(0)]);
|
|
|
|
Store(bcx, C_int(index), lldiscrimptr);
|
|
|
|
GEP(bcx, lltagptr, [C_int(0), C_int(1)])
|
2011-07-27 14:19:39 +02:00
|
|
|
};
|
2010-12-06 16:50:24 -08:00
|
|
|
i = 0u;
|
2011-08-15 21:54:52 -07:00
|
|
|
for va: ast::variant_arg in variant.node.args {
|
2011-07-27 14:19:39 +02:00
|
|
|
let rslt =
|
2011-08-21 21:44:41 -07:00
|
|
|
GEP_tag(bcx, llblobptr, ast_util::local_def(tag_id),
|
|
|
|
ast_util::local_def(variant.node.id), ty_param_substs,
|
2011-06-15 11:19:50 -07:00
|
|
|
i as int);
|
2011-03-03 15:52:54 -08:00
|
|
|
bcx = rslt.bcx;
|
2011-07-27 14:19:39 +02:00
|
|
|
let lldestptr = rslt.val;
|
2011-03-04 15:08:33 -08:00
|
|
|
// If this argument to this function is a tag, it'll have come in to
|
|
|
|
// this function as an opaque blob due to the way that type_of()
|
|
|
|
// works. So we have to cast to the destination's view of the type.
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let llargptr;
|
|
|
|
alt fcx.llargs.find(va.id) {
|
2011-08-30 09:59:30 +02:00
|
|
|
some(x) { llargptr = PointerCast(bcx, x, val_ty(lldestptr)); }
|
2011-07-27 14:19:39 +02:00
|
|
|
none. {
|
2011-08-27 16:36:48 -07:00
|
|
|
bcx_ccx(bcx).sess.bug(~"unbound argptr in \
|
2011-07-22 23:56:02 -07:00
|
|
|
trans_tag_variant");
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-07-05 19:57:34 -07:00
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
let arg_ty = arg_tys[i].ty;
|
2011-07-27 14:19:39 +02:00
|
|
|
let llargval;
|
|
|
|
if ty::type_is_structural(cx.ccx.tcx, arg_ty) ||
|
|
|
|
ty::type_has_dynamic_size(cx.ccx.tcx, arg_ty) {
|
2011-03-04 18:05:48 -08:00
|
|
|
llargval = llargptr;
|
2011-08-30 09:59:30 +02:00
|
|
|
} else { llargval = Load(bcx, llargptr); }
|
2011-08-22 12:38:58 +02:00
|
|
|
bcx = copy_val(bcx, INIT, lldestptr, llargval, arg_ty);
|
2010-12-06 16:50:24 -08:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
bcx = trans_block_cleanups(bcx, find_scope_cx(bcx));
|
2011-08-17 17:49:54 -07:00
|
|
|
build_return(bcx);
|
2011-05-11 11:56:49 -07:00
|
|
|
finish_fn(fcx, lltop);
|
2010-12-01 19:03:47 -08:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-01-21 12:09:25 -08:00
|
|
|
// FIXME: this should do some structural hash-consing to avoid
|
|
|
|
// duplicate constants. I think. Maybe LLVM has a magical mode
|
|
|
|
// that does so later on?
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_const_expr(cx: &@crate_ctxt, e: @ast::expr) -> ValueRef {
|
|
|
|
alt e.node {
|
|
|
|
ast::expr_lit(lit) { ret trans_crate_lit(cx, *lit); }
|
2011-08-27 16:36:48 -07:00
|
|
|
_ { cx.sess.span_unimpl(e.span, ~"consts that's not a plain literal"); }
|
2011-01-21 12:09:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_const(cx: &@crate_ctxt, e: @ast::expr, id: ast::node_id) {
|
|
|
|
let v = trans_const_expr(cx, e);
|
|
|
|
|
2011-03-18 16:22:59 -07:00
|
|
|
// The scalars come back as 1st class LLVM vals
|
|
|
|
// which we have to stick into global constants.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
alt cx.consts.find(id) {
|
|
|
|
some(g) {
|
|
|
|
llvm::LLVMSetInitializer(g, v);
|
|
|
|
llvm::LLVMSetGlobalConstant(g, True);
|
|
|
|
}
|
2011-08-27 16:36:48 -07:00
|
|
|
_ { cx.sess.span_fatal(e.span, ~"Unbound const in trans_const"); }
|
2011-07-05 19:57:34 -07:00
|
|
|
}
|
2011-01-21 12:09:25 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_item(cx: @local_ctxt, item: &ast::item) {
|
|
|
|
alt item.node {
|
2011-08-05 11:38:06 -07:00
|
|
|
ast::item_fn(f, tps) {
|
2011-08-26 18:25:46 -07:00
|
|
|
let sub_cx = extend_path(cx, item.ident);
|
2011-07-27 14:19:39 +02:00
|
|
|
alt cx.ccx.item_ids.find(item.id) {
|
|
|
|
some(llfndecl) {
|
|
|
|
trans_fn(sub_cx, item.span, f, llfndecl, none, tps, item.id);
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
cx.ccx.sess.span_fatal(item.span,
|
2011-08-27 16:36:48 -07:00
|
|
|
~"unbound function item in trans_item");
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2010-09-22 17:05:38 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::item_obj(ob, tps, ctor_id) {
|
|
|
|
let sub_cx =
|
|
|
|
@{obj_typarams: tps, obj_fields: ob.fields
|
2011-08-26 18:25:46 -07:00
|
|
|
with *extend_path(cx, item.ident)};
|
2011-07-27 14:19:39 +02:00
|
|
|
trans_obj(sub_cx, item.span, ob, ctor_id, tps);
|
|
|
|
}
|
|
|
|
ast::item_res(dtor, dtor_id, tps, ctor_id) {
|
|
|
|
trans_res_ctor(cx, item.span, dtor, ctor_id, tps);
|
|
|
|
|
|
|
|
// Create a function for the destructor
|
|
|
|
alt cx.ccx.item_ids.find(item.id) {
|
|
|
|
some(lldtor_decl) {
|
|
|
|
trans_fn(cx, item.span, dtor, lldtor_decl, none, tps, dtor_id);
|
|
|
|
}
|
|
|
|
_ {
|
2011-08-27 16:36:48 -07:00
|
|
|
cx.ccx.sess.span_fatal(item.span, ~"unbound dtor in trans_item");
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2010-12-01 19:03:47 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::item_mod(m) {
|
|
|
|
let sub_cx =
|
2011-08-26 18:25:46 -07:00
|
|
|
@{path: cx.path + [item.ident],
|
2011-08-25 17:00:12 -07:00
|
|
|
module_path: cx.module_path
|
2011-08-26 18:25:46 -07:00
|
|
|
+ [item.ident] with *cx};
|
2011-07-27 14:19:39 +02:00
|
|
|
trans_mod(sub_cx, m);
|
|
|
|
}
|
|
|
|
ast::item_tag(variants, tps) {
|
2011-08-26 18:25:46 -07:00
|
|
|
let sub_cx = extend_path(cx, item.ident);
|
2011-08-15 16:38:23 -07:00
|
|
|
let degen = std::vec::len(variants) == 1u;
|
2011-07-27 14:19:39 +02:00
|
|
|
let i = 0;
|
2011-08-15 21:54:52 -07:00
|
|
|
for variant: ast::variant in variants {
|
2011-07-27 14:19:39 +02:00
|
|
|
trans_tag_variant(sub_cx, item.id, variant, i, degen, tps);
|
|
|
|
i += 1;
|
2011-01-21 12:09:25 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::item_const(_, expr) { trans_const(cx.ccx, expr, item.id); }
|
|
|
|
_ {/* fall through */ }
|
2010-09-22 17:05:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-16 16:55:46 -07:00
|
|
|
|
2011-06-15 12:18:02 -07:00
|
|
|
// Translate a module. Doing this amounts to translating the items in the
|
|
|
|
// module; there ends up being no artifact (aside from linkage names) of
|
|
|
|
// separate modules in the compiled program. That's because modules exist
|
|
|
|
// only as a convenience for humans working with the code, to organize names
|
|
|
|
// and control visibility.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_mod(cx: @local_ctxt, m: &ast::_mod) {
|
2011-08-15 21:54:52 -07:00
|
|
|
for item: @ast::item in m.items { trans_item(cx, *item); }
|
2010-09-22 17:05:38 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn get_pair_fn_ty(llpairty: TypeRef) -> TypeRef {
|
2011-01-07 15:12:23 -08:00
|
|
|
// Bit of a kludge: pick the fn typeref out of the pair.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-10 13:49:22 -07:00
|
|
|
ret struct_elt(llpairty, 0u);
|
2011-01-07 15:12:23 -08:00
|
|
|
}
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn decl_fn_and_pair(ccx: &@crate_ctxt, sp: &span, path: &[istr], flav: &istr,
|
2011-08-04 16:20:09 -07:00
|
|
|
ty_params: &[ast::ty_param], node_id: ast::node_id) {
|
2011-06-30 12:35:19 +02:00
|
|
|
decl_fn_and_pair_full(ccx, sp, path, flav, ty_params, node_id,
|
|
|
|
node_id_type(ccx, node_id));
|
|
|
|
}
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn decl_fn_and_pair_full(ccx: &@crate_ctxt, sp: &span, path: &[istr],
|
|
|
|
_flav: &istr, ty_params: &[ast::ty_param],
|
2011-07-27 14:19:39 +02:00
|
|
|
node_id: ast::node_id, node_type: ty::t) {
|
2011-08-26 21:34:56 -07:00
|
|
|
let path = path;
|
2011-08-19 15:16:48 -07:00
|
|
|
let llfty =
|
|
|
|
type_of_fn_from_ty(ccx, sp, node_type, std::vec::len(ty_params));
|
2011-07-27 14:19:39 +02:00
|
|
|
alt ty::struct(ccx.tcx, node_type) {
|
|
|
|
ty::ty_fn(proto, inputs, output, _, _) {
|
|
|
|
llfty =
|
|
|
|
type_of_fn(ccx, sp, proto, inputs, output,
|
2011-08-13 00:09:25 -07:00
|
|
|
std::vec::len::<ast::ty_param>(ty_params));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-27 16:36:48 -07:00
|
|
|
_ { ccx.sess.bug(
|
|
|
|
~"decl_fn_and_pair(): fn item doesn't have fn type!"); }
|
2011-03-08 16:51:23 -08:00
|
|
|
}
|
2011-08-25 21:04:04 -07:00
|
|
|
let s: istr = mangle_internal_name_by_path(ccx, path);
|
|
|
|
let llfn: ValueRef = decl_internal_fastcall_fn(ccx.llmod,
|
2011-08-26 21:34:56 -07:00
|
|
|
s, llfty);
|
2011-01-05 15:31:35 -08:00
|
|
|
// Declare the global constant pair that points to it.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-08-25 21:04:04 -07:00
|
|
|
let ps: istr = mangle_exported_name(ccx, path, node_type);
|
2011-08-26 21:34:56 -07:00
|
|
|
register_fn_pair(ccx, ps, llfty, llfn, node_id);
|
2011-08-12 18:43:44 -07:00
|
|
|
|
|
|
|
let is_main: bool = is_main_name(path) && !ccx.sess.get_opts().library;
|
2011-08-19 15:16:48 -07:00
|
|
|
if is_main { create_main_wrapper(ccx, sp, llfn, node_type); }
|
2011-08-12 18:43:44 -07:00
|
|
|
}
|
|
|
|
|
2011-08-17 20:31:55 -07:00
|
|
|
// Create a _rust_main(args: [str]) function which will be called from the
|
|
|
|
// runtime rust_start function
|
2011-08-19 15:16:48 -07:00
|
|
|
fn create_main_wrapper(ccx: &@crate_ctxt, sp: &span, main_llfn: ValueRef,
|
|
|
|
main_node_type: ty::t) {
|
2011-08-12 18:43:44 -07:00
|
|
|
|
2011-08-13 00:09:25 -07:00
|
|
|
if ccx.main_fn != none::<ValueRef> {
|
2011-08-27 16:36:48 -07:00
|
|
|
ccx.sess.span_fatal(sp, ~"multiple 'main' functions");
|
2011-08-12 18:43:44 -07:00
|
|
|
}
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
let main_takes_ivec =
|
|
|
|
alt ty::struct(ccx.tcx, main_node_type) {
|
|
|
|
ty::ty_fn(_, args, _, _, _) { std::vec::len(args) != 0u }
|
|
|
|
};
|
2011-08-12 18:43:44 -07:00
|
|
|
|
2011-08-17 16:50:49 -07:00
|
|
|
let llfn = create_main(ccx, sp, main_llfn, main_takes_ivec);
|
|
|
|
ccx.main_fn = some(llfn);
|
2011-08-12 18:43:44 -07:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
fn create_main(ccx: &@crate_ctxt, sp: &span, main_llfn: ValueRef,
|
2011-08-17 16:50:49 -07:00
|
|
|
takes_ivec: bool) -> ValueRef {
|
2011-08-19 15:16:48 -07:00
|
|
|
let ivecarg_ty: ty::arg =
|
|
|
|
{mode: ty::mo_val,
|
|
|
|
ty:
|
|
|
|
ty::mk_vec(ccx.tcx,
|
|
|
|
{ty: ty::mk_str(ccx.tcx), mut: ast::imm})};
|
|
|
|
let llfty =
|
|
|
|
type_of_fn(ccx, sp, ast::proto_fn, [ivecarg_ty],
|
|
|
|
ty::mk_nil(ccx.tcx), 0u);
|
2011-08-26 21:34:56 -07:00
|
|
|
let llfdecl = decl_fastcall_fn(ccx.llmod, ~"_rust_main", llfty);
|
2011-08-12 18:43:44 -07:00
|
|
|
|
|
|
|
let fcx = new_fn_ctxt(new_local_ctxt(ccx), sp, llfdecl);
|
2011-08-17 20:31:55 -07:00
|
|
|
|
2011-08-12 18:43:44 -07:00
|
|
|
let bcx = new_top_block_ctxt(fcx);
|
2011-08-17 20:31:55 -07:00
|
|
|
let lltop = bcx.llbb;
|
2011-08-12 18:43:44 -07:00
|
|
|
|
2011-08-19 12:48:45 +02:00
|
|
|
let lloutputarg = llvm::LLVMGetParam(llfdecl, 0u);
|
|
|
|
let lltaskarg = llvm::LLVMGetParam(llfdecl, 1u);
|
|
|
|
let llenvarg = llvm::LLVMGetParam(llfdecl, 2u);
|
|
|
|
let llargvarg = llvm::LLVMGetParam(llfdecl, 3u);
|
2011-08-19 14:34:45 +02:00
|
|
|
let args = [lloutputarg, lltaskarg, llenvarg];
|
|
|
|
if takes_ivec { args += [llargvarg]; }
|
2011-08-30 09:59:30 +02:00
|
|
|
FastCall(bcx, main_llfn, args);
|
2011-08-17 17:49:54 -07:00
|
|
|
build_return(bcx);
|
2011-08-12 18:43:44 -07:00
|
|
|
|
|
|
|
finish_fn(fcx, lltop);
|
|
|
|
|
|
|
|
ret llfdecl;
|
|
|
|
}
|
2011-02-28 17:33:46 -05:00
|
|
|
}
|
|
|
|
|
2011-08-12 18:43:44 -07:00
|
|
|
|
2011-06-16 17:33:31 -07:00
|
|
|
// Create a closure: a pair containing (1) a ValueRef, pointing to where the
|
|
|
|
// fn's definition is in the executable we're creating, and (2) a pointer to
|
|
|
|
// space for the function's environment.
|
2011-08-26 21:34:56 -07:00
|
|
|
fn create_fn_pair(cx: &@crate_ctxt, ps: &istr, llfnty: TypeRef,
|
|
|
|
llfn: ValueRef, external: bool) -> ValueRef {
|
|
|
|
let gvar = istr::as_buf(ps, { |buf|
|
2011-08-26 15:36:18 -07:00
|
|
|
llvm::LLVMAddGlobal(cx.llmod, T_fn_pair(*cx, llfnty), buf)
|
|
|
|
});
|
2011-08-19 15:16:48 -07:00
|
|
|
let pair = C_struct([llfn, C_null(T_opaque_closure_ptr(*cx))]);
|
2011-05-12 17:24:54 +02:00
|
|
|
llvm::LLVMSetInitializer(gvar, pair);
|
|
|
|
llvm::LLVMSetGlobalConstant(gvar, True);
|
2011-07-27 14:19:39 +02:00
|
|
|
if !external {
|
2011-06-15 11:19:50 -07:00
|
|
|
llvm::LLVMSetLinkage(gvar,
|
|
|
|
lib::llvm::LLVMInternalLinkage as llvm::Linkage);
|
2011-06-01 15:45:31 -04:00
|
|
|
}
|
2011-06-14 15:20:04 +02:00
|
|
|
ret gvar;
|
|
|
|
}
|
2011-01-05 15:31:35 -08:00
|
|
|
|
2011-06-29 18:44:38 -07:00
|
|
|
// Create a /real/ closure: this is like create_fn_pair, but creates a
|
|
|
|
// a fn value on the stack with a specified environment (which need not be
|
|
|
|
// on the stack).
|
2011-07-27 14:19:39 +02:00
|
|
|
fn create_real_fn_pair(cx: &@block_ctxt, llfnty: TypeRef, llfn: ValueRef,
|
|
|
|
llenvptr: ValueRef) -> ValueRef {
|
|
|
|
let lcx = cx.fcx.lcx;
|
2011-06-29 18:44:38 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let pair = alloca(cx, T_fn_pair(*lcx.ccx, llfnty));
|
2011-08-30 09:59:30 +02:00
|
|
|
let code_cell = GEP(cx, pair, [C_int(0), C_int(abi::fn_field_code)]);
|
|
|
|
Store(cx, llfn, code_cell);
|
|
|
|
let env_cell = GEP(cx, pair, [C_int(0), C_int(abi::fn_field_box)]);
|
2011-07-27 14:19:39 +02:00
|
|
|
let llenvblobptr =
|
2011-08-30 09:59:30 +02:00
|
|
|
PointerCast(cx, llenvptr, T_opaque_closure_ptr(*lcx.ccx));
|
|
|
|
Store(cx, llenvblobptr, env_cell);
|
2011-06-29 18:44:38 -07:00
|
|
|
ret pair;
|
|
|
|
}
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn register_fn_pair(cx: &@crate_ctxt, ps: &istr, llfnty: TypeRef,
|
2011-07-27 14:19:39 +02:00
|
|
|
llfn: ValueRef, id: ast::node_id) {
|
2011-06-14 15:20:04 +02:00
|
|
|
// FIXME: We should also hide the unexported pairs in crates.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let gvar =
|
2011-07-07 14:42:18 -04:00
|
|
|
create_fn_pair(cx, ps, llfnty, llfn, cx.sess.get_opts().library);
|
2011-01-05 15:31:35 -08:00
|
|
|
cx.item_ids.insert(id, llfn);
|
2011-08-26 21:34:56 -07:00
|
|
|
cx.item_symbols.insert(id, ps);
|
2011-01-05 15:31:35 -08:00
|
|
|
cx.fn_pairs.insert(id, gvar);
|
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-03-08 16:51:23 -08:00
|
|
|
// Returns the number of type parameters that the given native function has.
|
2011-07-27 14:19:39 +02:00
|
|
|
fn native_fn_ty_param_count(cx: &@crate_ctxt, id: ast::node_id) -> uint {
|
|
|
|
let count;
|
|
|
|
let native_item =
|
|
|
|
alt cx.ast_map.find(id) { some(ast_map::node_native_item(i)) { i } };
|
|
|
|
alt native_item.node {
|
|
|
|
ast::native_item_ty. {
|
2011-08-27 16:36:48 -07:00
|
|
|
cx.sess.bug(~"decl_native_fn_and_pair(): native fn isn't \
|
2011-07-22 23:56:02 -07:00
|
|
|
actually a fn");
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::native_item_fn(_, _, tps) {
|
2011-08-13 00:09:25 -07:00
|
|
|
count = std::vec::len::<ast::ty_param>(tps);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-03-08 16:51:23 -08:00
|
|
|
}
|
|
|
|
ret count;
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn native_fn_wrapper_type(cx: &@crate_ctxt, sp: &span, ty_param_count: uint,
|
|
|
|
x: ty::t) -> TypeRef {
|
|
|
|
alt ty::struct(cx.tcx, x) {
|
|
|
|
ty::ty_native_fn(abi, args, out) {
|
|
|
|
ret type_of_fn(cx, sp, ast::proto_fn, args, out, ty_param_count);
|
|
|
|
}
|
2011-02-28 17:33:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn decl_native_fn_and_pair(ccx: &@crate_ctxt, sp: &span, path: &[istr],
|
|
|
|
name: &istr, id: ast::node_id) {
|
|
|
|
let path = path;
|
2011-07-27 14:19:39 +02:00
|
|
|
let num_ty_param = native_fn_ty_param_count(ccx, id);
|
2011-02-28 17:33:46 -05:00
|
|
|
// Declare the wrapper.
|
2011-05-20 18:36:35 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let t = node_id_type(ccx, id);
|
|
|
|
let wrapper_type = native_fn_wrapper_type(ccx, sp, num_ty_param, t);
|
2011-08-25 21:04:04 -07:00
|
|
|
let s: istr = mangle_internal_name_by_path(ccx, path);
|
2011-07-27 14:19:39 +02:00
|
|
|
let wrapper_fn: ValueRef =
|
2011-08-25 21:04:04 -07:00
|
|
|
decl_internal_fastcall_fn(ccx.llmod,
|
2011-08-26 21:34:56 -07:00
|
|
|
s, wrapper_type);
|
2011-02-28 17:33:46 -05:00
|
|
|
// Declare the global constant pair that points to it.
|
2011-02-16 15:34:59 -05:00
|
|
|
|
2011-08-25 21:04:04 -07:00
|
|
|
let ps: istr = mangle_exported_name(ccx, path, node_id_type(ccx, id));
|
2011-08-26 21:34:56 -07:00
|
|
|
register_fn_pair(ccx, ps, wrapper_type, wrapper_fn, id);
|
2011-03-07 15:37:40 -05:00
|
|
|
// Build the wrapper.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let fcx = new_fn_ctxt(new_local_ctxt(ccx), sp, wrapper_fn);
|
|
|
|
let bcx = new_top_block_ctxt(fcx);
|
|
|
|
let lltop = bcx.llbb;
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-08-17 16:06:33 -07:00
|
|
|
// Declare the function itself.
|
2011-07-27 14:19:39 +02:00
|
|
|
let fn_type = node_id_type(ccx, id); // NB: has no type params
|
2011-03-18 15:01:45 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let abi = ty::ty_fn_abi(ccx.tcx, fn_type);
|
2011-04-26 18:48:34 -07:00
|
|
|
// FIXME: If the returned type is not nil, then we assume it's 32 bits
|
|
|
|
// wide. This is obviously wildly unsafe. We should have a better FFI
|
|
|
|
// that allows types of different sizes to be returned.
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let rty = ty::ty_fn_ret(ccx.tcx, fn_type);
|
|
|
|
let rty_is_nil = ty::type_is_nil(ccx.tcx, rty);
|
2011-07-09 21:13:25 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let pass_task;
|
|
|
|
let uses_retptr;
|
|
|
|
let cast_to_i32;
|
|
|
|
alt abi {
|
|
|
|
ast::native_abi_rust. {
|
2011-07-09 21:13:25 -07:00
|
|
|
pass_task = true;
|
|
|
|
uses_retptr = false;
|
|
|
|
cast_to_i32 = true;
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ast::native_abi_rust_intrinsic. {
|
2011-07-09 21:13:25 -07:00
|
|
|
pass_task = true;
|
|
|
|
uses_retptr = true;
|
|
|
|
cast_to_i32 = false;
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ast::native_abi_cdecl. {
|
2011-07-09 21:13:25 -07:00
|
|
|
pass_task = false;
|
|
|
|
uses_retptr = false;
|
|
|
|
cast_to_i32 = true;
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ast::native_abi_llvm. {
|
2011-07-09 21:13:25 -07:00
|
|
|
pass_task = false;
|
|
|
|
uses_retptr = false;
|
|
|
|
cast_to_i32 = false;
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ast::native_abi_x86stdcall. {
|
2011-07-18 13:14:33 -07:00
|
|
|
pass_task = false;
|
|
|
|
uses_retptr = false;
|
|
|
|
cast_to_i32 = true;
|
|
|
|
}
|
2011-05-05 17:47:30 -07:00
|
|
|
}
|
2011-07-09 21:13:25 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let lltaskptr;
|
|
|
|
if cast_to_i32 {
|
2011-05-05 17:47:30 -07:00
|
|
|
lltaskptr = vp2i(bcx, fcx.lltaskptr);
|
2011-06-15 11:19:50 -07:00
|
|
|
} else { lltaskptr = fcx.lltaskptr; }
|
2011-07-09 21:13:25 -07:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
let call_args: [ValueRef] = [];
|
|
|
|
if pass_task { call_args += [lltaskptr]; }
|
|
|
|
if uses_retptr { call_args += [bcx.fcx.llretptr]; }
|
2011-07-09 21:13:25 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let arg_n = 3u;
|
2011-08-15 21:54:52 -07:00
|
|
|
for each i: uint in uint::range(0u, num_ty_param) {
|
2011-07-27 14:19:39 +02:00
|
|
|
let llarg = llvm::LLVMGetParam(fcx.llfn, arg_n);
|
2011-08-19 15:16:48 -07:00
|
|
|
fcx.lltydescs += [llarg];
|
2011-05-05 17:47:30 -07:00
|
|
|
assert (llarg as int != 0);
|
2011-07-27 14:19:39 +02:00
|
|
|
if cast_to_i32 {
|
2011-08-19 15:16:48 -07:00
|
|
|
call_args += [vp2i(bcx, llarg)];
|
|
|
|
} else { call_args += [llarg]; }
|
2011-05-05 17:47:30 -07:00
|
|
|
arg_n += 1u;
|
2011-03-07 15:37:40 -05:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
fn convert_arg_to_i32(cx: &@block_ctxt, v: ValueRef, t: ty::t,
|
|
|
|
mode: ty::mode) -> ValueRef {
|
|
|
|
if mode == ty::mo_val {
|
|
|
|
if ty::type_is_integral(bcx_tcx(cx), t) {
|
|
|
|
let lldsttype = T_int();
|
|
|
|
let llsrctype = type_of(bcx_ccx(cx), cx.sp, t);
|
|
|
|
if llvm::LLVMGetIntTypeWidth(lldsttype) >
|
|
|
|
llvm::LLVMGetIntTypeWidth(llsrctype) {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret ZExtOrBitCast(cx, v, T_int());
|
2011-04-29 11:54:41 -07:00
|
|
|
}
|
2011-08-30 09:59:30 +02:00
|
|
|
ret TruncOrBitCast(cx, v, T_int());
|
2011-04-29 11:54:41 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
if ty::type_is_fp(bcx_tcx(cx), t) {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret FPToSI(cx, v, T_int());
|
2011-04-05 21:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-05 17:47:30 -07:00
|
|
|
ret vp2i(cx, v);
|
2011-04-05 21:21:32 +00:00
|
|
|
}
|
2011-07-09 21:13:25 -07:00
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn trans_simple_native_abi(bcx: &@block_ctxt, name: &istr,
|
2011-08-04 16:20:09 -07:00
|
|
|
call_args: &mutable [ValueRef], fn_type: ty::t,
|
2011-08-19 15:16:48 -07:00
|
|
|
uses_retptr: bool, cc: uint) ->
|
|
|
|
{val: ValueRef, rptr: ValueRef} {
|
|
|
|
let call_arg_tys: [TypeRef] = [];
|
|
|
|
for arg: ValueRef in call_args { call_arg_tys += [val_ty(arg)]; }
|
2011-07-09 21:13:25 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let llnativefnty;
|
|
|
|
if uses_retptr {
|
2011-07-09 21:13:25 -07:00
|
|
|
llnativefnty = T_fn(call_arg_tys, T_void());
|
|
|
|
} else {
|
|
|
|
llnativefnty =
|
|
|
|
T_fn(call_arg_tys,
|
2011-07-26 13:02:26 -07:00
|
|
|
type_of(bcx_ccx(bcx), bcx.sp,
|
|
|
|
ty::ty_fn_ret(bcx_tcx(bcx), fn_type)));
|
2011-07-09 21:13:25 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let llnativefn =
|
|
|
|
get_extern_fn(bcx_ccx(bcx).externs, bcx_ccx(bcx).llmod, name, cc,
|
|
|
|
llnativefnty);
|
|
|
|
let r =
|
|
|
|
if cc == lib::llvm::LLVMCCallConv {
|
2011-08-30 09:59:30 +02:00
|
|
|
Call(bcx, llnativefn, call_args)
|
|
|
|
} else { CallWithConv(bcx, llnativefn, call_args, cc) };
|
2011-07-27 14:19:39 +02:00
|
|
|
let rptr = bcx.fcx.llretptr;
|
|
|
|
ret {val: r, rptr: rptr};
|
2011-05-03 18:03:59 -07:00
|
|
|
}
|
2011-07-09 21:13:25 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let args = ty::ty_fn_args(ccx.tcx, fn_type);
|
2011-05-05 17:47:30 -07:00
|
|
|
// Build up the list of arguments.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let i = arg_n;
|
2011-08-15 21:54:52 -07:00
|
|
|
for arg: ty::arg in args {
|
2011-07-27 14:19:39 +02:00
|
|
|
let llarg = llvm::LLVMGetParam(fcx.llfn, i);
|
2011-05-05 17:47:30 -07:00
|
|
|
assert (llarg as int != 0);
|
2011-07-27 14:19:39 +02:00
|
|
|
if cast_to_i32 {
|
|
|
|
let llarg_i32 = convert_arg_to_i32(bcx, llarg, arg.ty, arg.mode);
|
2011-08-19 15:16:48 -07:00
|
|
|
call_args += [llarg_i32];
|
|
|
|
} else { call_args += [llarg]; }
|
2011-05-05 17:47:30 -07:00
|
|
|
i += 1u;
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let r;
|
|
|
|
let rptr;
|
|
|
|
alt abi {
|
|
|
|
ast::native_abi_llvm. {
|
|
|
|
let result =
|
2011-08-18 10:01:39 +02:00
|
|
|
trans_simple_native_abi(bcx, name, call_args, fn_type,
|
2011-07-27 14:19:39 +02:00
|
|
|
uses_retptr, lib::llvm::LLVMCCallConv);
|
|
|
|
r = result.val;
|
|
|
|
rptr = result.rptr;
|
|
|
|
}
|
|
|
|
ast::native_abi_rust_intrinsic. {
|
2011-08-26 21:34:56 -07:00
|
|
|
let external_name = ~"rust_intrinsic_" + name;
|
2011-07-27 14:19:39 +02:00
|
|
|
let result =
|
|
|
|
trans_simple_native_abi(bcx, external_name, call_args, fn_type,
|
2011-08-18 10:01:39 +02:00
|
|
|
uses_retptr, lib::llvm::LLVMCCallConv);
|
2011-07-27 14:19:39 +02:00
|
|
|
r = result.val;
|
|
|
|
rptr = result.rptr;
|
|
|
|
}
|
|
|
|
ast::native_abi_x86stdcall. {
|
|
|
|
let result =
|
2011-08-18 10:01:39 +02:00
|
|
|
trans_simple_native_abi(bcx, name, call_args, fn_type,
|
2011-07-27 14:19:39 +02:00
|
|
|
uses_retptr,
|
|
|
|
lib::llvm::LLVMX86StdcallCallConv);
|
|
|
|
r = result.val;
|
|
|
|
rptr = result.rptr;
|
|
|
|
}
|
|
|
|
_ {
|
2011-08-24 14:54:55 +02:00
|
|
|
r = trans_native_call(new_raw_block_ctxt(bcx.fcx, bcx.llbb),
|
|
|
|
ccx.externs, ccx.llmod, name, call_args);
|
2011-08-30 09:59:30 +02:00
|
|
|
rptr = BitCast(bcx, fcx.llretptr, T_ptr(T_i32()));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-03-07 15:37:40 -05:00
|
|
|
}
|
2011-04-26 18:48:34 -07:00
|
|
|
// We don't store the return value if it's nil, to avoid stomping on a nil
|
|
|
|
// pointer. This is the only concession made to non-i32 return values. See
|
|
|
|
// the FIXME above.
|
|
|
|
|
2011-08-30 09:59:30 +02:00
|
|
|
if !rty_is_nil && !uses_retptr { Store(bcx, r, rptr); }
|
2011-07-09 21:13:25 -07:00
|
|
|
|
2011-08-17 17:49:54 -07:00
|
|
|
build_return(bcx);
|
2011-05-11 11:56:49 -07:00
|
|
|
finish_fn(fcx, lltop);
|
2011-02-16 15:34:59 -05:00
|
|
|
}
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn item_path(item: &@ast::item) -> [istr] { ret [item.ident]; }
|
2011-04-20 17:23:45 +02:00
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn collect_native_item(ccx: @crate_ctxt, i: &@ast::native_item, pt: &[istr],
|
|
|
|
_v: &vt<[istr]>) {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt i.node {
|
|
|
|
ast::native_item_fn(_, _, _) {
|
|
|
|
if !ccx.obj_methods.contains_key(i.id) {
|
2011-08-25 17:00:12 -07:00
|
|
|
decl_native_fn_and_pair(ccx, i.span, pt,
|
2011-08-26 21:34:56 -07:00
|
|
|
i.ident, i.id);
|
2011-03-26 17:36:47 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ { }
|
2011-03-26 17:36:47 -07:00
|
|
|
}
|
|
|
|
}
|
2011-01-05 15:31:35 -08:00
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn collect_item_1(ccx: @crate_ctxt, i: &@ast::item, pt: &[istr],
|
|
|
|
v: &vt<[istr]>) {
|
2011-06-10 17:24:20 +02:00
|
|
|
visit::visit_item(i, pt + item_path(i), v);
|
2011-07-27 14:19:39 +02:00
|
|
|
alt i.node {
|
|
|
|
ast::item_const(_, _) {
|
|
|
|
let typ = node_id_type(ccx, i.id);
|
|
|
|
let s =
|
2011-08-26 21:34:56 -07:00
|
|
|
mangle_exported_name(ccx, pt + [i.ident],
|
2011-07-27 14:19:39 +02:00
|
|
|
node_id_type(ccx, i.id));
|
2011-08-26 15:36:18 -07:00
|
|
|
let g = istr::as_buf(s, { |buf|
|
|
|
|
llvm::LLVMAddGlobal(ccx.llmod, type_of(ccx, i.span, typ), buf)
|
|
|
|
});
|
2011-08-26 17:14:13 -07:00
|
|
|
ccx.item_symbols.insert(i.id, s);
|
2011-07-27 14:19:39 +02:00
|
|
|
ccx.consts.insert(i.id, g);
|
|
|
|
}
|
|
|
|
_ { }
|
2011-03-14 16:56:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn collect_item_2(ccx: &@crate_ctxt, i: &@ast::item, pt: &[istr],
|
|
|
|
v: &vt<[istr]>) {
|
2011-07-27 14:19:39 +02:00
|
|
|
let new_pt = pt + item_path(i);
|
2011-06-10 17:24:20 +02:00
|
|
|
visit::visit_item(i, new_pt, v);
|
2011-07-27 14:19:39 +02:00
|
|
|
alt i.node {
|
2011-08-05 11:38:06 -07:00
|
|
|
ast::item_fn(f, tps) {
|
2011-07-27 14:19:39 +02:00
|
|
|
if !ccx.obj_methods.contains_key(i.id) {
|
2011-08-26 21:34:56 -07:00
|
|
|
decl_fn_and_pair(ccx, i.span, new_pt, ~"fn", tps, i.id);
|
2010-10-22 19:31:33 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::item_obj(ob, tps, ctor_id) {
|
2011-08-26 21:34:56 -07:00
|
|
|
decl_fn_and_pair(ccx, i.span, new_pt, ~"obj_ctor", tps, ctor_id);
|
2011-08-15 21:54:52 -07:00
|
|
|
for m: @ast::method in ob.methods {
|
2011-07-27 14:19:39 +02:00
|
|
|
ccx.obj_methods.insert(m.node.id, ());
|
2011-06-24 18:10:40 +02:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::item_res(_, dtor_id, tps, ctor_id) {
|
2011-08-26 21:34:56 -07:00
|
|
|
decl_fn_and_pair(ccx, i.span, new_pt, ~"res_ctor", tps, ctor_id);
|
2011-07-27 14:19:39 +02:00
|
|
|
// Note that the destructor is associated with the item's id, not
|
|
|
|
// the dtor_id. This is a bit counter-intuitive, but simplifies
|
|
|
|
// ty_res, which would have to carry around two def_ids otherwise
|
|
|
|
// -- one to identify the type, and one to find the dtor symbol.
|
2011-08-26 21:34:56 -07:00
|
|
|
decl_fn_and_pair_full(ccx, i.span, new_pt, ~"res_dtor", tps, i.id,
|
2011-07-27 14:19:39 +02:00
|
|
|
node_id_type(ccx, dtor_id));
|
|
|
|
}
|
|
|
|
_ { }
|
2010-10-22 19:31:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn collect_items(ccx: &@crate_ctxt, crate: @ast::crate) {
|
|
|
|
let visitor0 = visit::default_visitor();
|
|
|
|
let visitor1 =
|
|
|
|
@{visit_native_item: bind collect_native_item(ccx, _, _, _),
|
|
|
|
visit_item: bind collect_item_1(ccx, _, _, _) with *visitor0};
|
|
|
|
let visitor2 =
|
|
|
|
@{visit_item: bind collect_item_2(ccx, _, _, _) with *visitor0};
|
2011-08-19 15:16:48 -07:00
|
|
|
visit::visit_crate(*crate, [], visit::mk_vt(visitor1));
|
|
|
|
visit::visit_crate(*crate, [], visit::mk_vt(visitor2));
|
2011-06-10 17:24:20 +02:00
|
|
|
}
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn collect_tag_ctor(ccx: @crate_ctxt, i: &@ast::item, pt: &[istr],
|
|
|
|
v: &vt<[istr]>) {
|
2011-07-27 14:19:39 +02:00
|
|
|
let new_pt = pt + item_path(i);
|
2011-06-10 17:24:20 +02:00
|
|
|
visit::visit_item(i, new_pt, v);
|
2011-07-27 14:19:39 +02:00
|
|
|
alt i.node {
|
|
|
|
ast::item_tag(variants, tps) {
|
2011-08-15 21:54:52 -07:00
|
|
|
for variant: ast::variant in variants {
|
2011-08-15 16:38:23 -07:00
|
|
|
if std::vec::len(variant.node.args) != 0u {
|
2011-08-25 17:00:12 -07:00
|
|
|
decl_fn_and_pair(ccx, i.span,
|
2011-08-26 21:34:56 -07:00
|
|
|
new_pt + [variant.node.name],
|
|
|
|
~"tag", tps, variant.node.id);
|
2011-01-05 15:31:35 -08:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ {/* fall through */ }
|
2011-01-05 15:31:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn collect_tag_ctors(ccx: &@crate_ctxt, crate: @ast::crate) {
|
|
|
|
let visitor =
|
|
|
|
@{visit_item: bind collect_tag_ctor(ccx, _, _, _)
|
|
|
|
with *visit::default_visitor()};
|
2011-08-19 15:16:48 -07:00
|
|
|
visit::visit_crate(*crate, [], visit::mk_vt(visitor));
|
2011-01-05 15:31:35 -08:00
|
|
|
}
|
|
|
|
|
2010-12-02 19:30:06 -08:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
// The constant translation pass.
|
2011-08-26 21:34:56 -07:00
|
|
|
fn trans_constant(ccx: @crate_ctxt, it: &@ast::item, pt: &[istr],
|
|
|
|
v: &vt<[istr]>) {
|
2011-07-27 14:19:39 +02:00
|
|
|
let new_pt = pt + item_path(it);
|
2011-06-10 17:24:20 +02:00
|
|
|
visit::visit_item(it, new_pt, v);
|
2011-07-27 14:19:39 +02:00
|
|
|
alt it.node {
|
|
|
|
ast::item_tag(variants, _) {
|
|
|
|
let i = 0u;
|
2011-08-13 00:09:25 -07:00
|
|
|
let n_variants = std::vec::len::<ast::variant>(variants);
|
2011-07-27 14:19:39 +02:00
|
|
|
while i < n_variants {
|
2011-08-19 15:16:48 -07:00
|
|
|
let variant = variants[i];
|
2011-08-26 21:34:56 -07:00
|
|
|
let p = new_pt + [it.ident,
|
|
|
|
variant.node.name,
|
|
|
|
~"discrim"];
|
2011-08-25 21:04:04 -07:00
|
|
|
let s = mangle_exported_name(ccx, p,
|
|
|
|
ty::mk_int(ccx.tcx));
|
2011-08-26 15:36:18 -07:00
|
|
|
let discrim_gvar = istr::as_buf(s, { |buf|
|
|
|
|
llvm::LLVMAddGlobal(ccx.llmod, T_int(), buf)
|
|
|
|
});
|
2011-07-27 14:19:39 +02:00
|
|
|
if n_variants != 1u {
|
|
|
|
llvm::LLVMSetInitializer(discrim_gvar, C_int(i as int));
|
|
|
|
llvm::LLVMSetGlobalConstant(discrim_gvar, True);
|
2010-12-02 19:30:06 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ccx.discrims.insert(variant.node.id, discrim_gvar);
|
2011-08-26 17:14:13 -07:00
|
|
|
ccx.discrim_symbols.insert(variant.node.id, s);
|
2011-07-27 14:19:39 +02:00
|
|
|
i += 1u;
|
2010-12-02 19:30:06 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ { }
|
2010-12-02 19:30:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_constants(ccx: &@crate_ctxt, crate: @ast::crate) {
|
|
|
|
let visitor =
|
|
|
|
@{visit_item: bind trans_constant(ccx, _, _, _)
|
|
|
|
with *visit::default_visitor()};
|
2011-08-19 15:16:48 -07:00
|
|
|
visit::visit_crate(*crate, [], visit::mk_vt(visitor));
|
2010-12-02 19:30:06 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn vp2i(cx: &@block_ctxt, v: ValueRef) -> ValueRef {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret PtrToInt(cx, v, T_int());
|
2011-03-02 16:42:09 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn vi2p(cx: &@block_ctxt, v: ValueRef, t: TypeRef) -> ValueRef {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret IntToPtr(cx, v, t);
|
2011-03-02 16:42:09 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn p2i(v: ValueRef) -> ValueRef { ret llvm::LLVMConstPtrToInt(v, T_int()); }
|
2010-09-27 13:43:53 -07:00
|
|
|
|
2011-08-25 15:12:54 -07:00
|
|
|
fn declare_intrinsics(llmod: ModuleRef) -> hashmap<istr, ValueRef> {
|
2011-08-04 16:20:09 -07:00
|
|
|
let T_memmove32_args: [TypeRef] =
|
2011-08-19 15:16:48 -07:00
|
|
|
[T_ptr(T_i8()), T_ptr(T_i8()), T_i32(), T_i32(), T_i1()];
|
2011-08-04 16:20:09 -07:00
|
|
|
let T_memmove64_args: [TypeRef] =
|
2011-08-19 15:16:48 -07:00
|
|
|
[T_ptr(T_i8()), T_ptr(T_i8()), T_i64(), T_i32(), T_i1()];
|
2011-08-04 16:20:09 -07:00
|
|
|
let T_memset32_args: [TypeRef] =
|
2011-08-19 15:16:48 -07:00
|
|
|
[T_ptr(T_i8()), T_i8(), T_i32(), T_i32(), T_i1()];
|
2011-08-04 16:20:09 -07:00
|
|
|
let T_memset64_args: [TypeRef] =
|
2011-08-19 15:16:48 -07:00
|
|
|
[T_ptr(T_i8()), T_i8(), T_i64(), T_i32(), T_i1()];
|
|
|
|
let T_trap_args: [TypeRef] = [];
|
2011-08-17 19:11:01 -07:00
|
|
|
let gcroot =
|
2011-08-26 21:34:56 -07:00
|
|
|
decl_cdecl_fn(llmod, ~"llvm.gcroot",
|
2011-08-19 15:16:48 -07:00
|
|
|
T_fn([T_ptr(T_ptr(T_i8())), T_ptr(T_i8())], T_void()));
|
2011-08-10 17:59:33 -07:00
|
|
|
let gcread =
|
2011-08-26 21:34:56 -07:00
|
|
|
decl_cdecl_fn(llmod, ~"llvm.gcread",
|
2011-08-19 15:16:48 -07:00
|
|
|
T_fn([T_ptr(T_i8()), T_ptr(T_ptr(T_i8()))], T_void()));
|
2011-07-27 14:19:39 +02:00
|
|
|
let memmove32 =
|
2011-08-26 21:34:56 -07:00
|
|
|
decl_cdecl_fn(llmod, ~"llvm.memmove.p0i8.p0i8.i32",
|
2011-06-15 11:19:50 -07:00
|
|
|
T_fn(T_memmove32_args, T_void()));
|
2011-07-27 14:19:39 +02:00
|
|
|
let memmove64 =
|
2011-08-26 21:34:56 -07:00
|
|
|
decl_cdecl_fn(llmod, ~"llvm.memmove.p0i8.p0i8.i64",
|
2011-06-15 11:19:50 -07:00
|
|
|
T_fn(T_memmove64_args, T_void()));
|
2011-07-27 14:19:39 +02:00
|
|
|
let memset32 =
|
2011-08-26 21:34:56 -07:00
|
|
|
decl_cdecl_fn(llmod, ~"llvm.memset.p0i8.i32",
|
2011-06-15 11:19:50 -07:00
|
|
|
T_fn(T_memset32_args, T_void()));
|
2011-07-27 14:19:39 +02:00
|
|
|
let memset64 =
|
2011-08-26 21:34:56 -07:00
|
|
|
decl_cdecl_fn(llmod, ~"llvm.memset.p0i8.i64",
|
2011-06-15 11:19:50 -07:00
|
|
|
T_fn(T_memset64_args, T_void()));
|
2011-08-26 21:34:56 -07:00
|
|
|
let trap = decl_cdecl_fn(llmod, ~"llvm.trap",
|
|
|
|
T_fn(T_trap_args, T_void()));
|
2011-08-13 00:09:25 -07:00
|
|
|
let intrinsics = new_str_hash::<ValueRef>();
|
2011-08-25 15:12:54 -07:00
|
|
|
intrinsics.insert(~"llvm.gcroot", gcroot);
|
|
|
|
intrinsics.insert(~"llvm.gcread", gcread);
|
|
|
|
intrinsics.insert(~"llvm.memmove.p0i8.p0i8.i32", memmove32);
|
|
|
|
intrinsics.insert(~"llvm.memmove.p0i8.p0i8.i64", memmove64);
|
|
|
|
intrinsics.insert(~"llvm.memset.p0i8.i32", memset32);
|
|
|
|
intrinsics.insert(~"llvm.memset.p0i8.i64", memset64);
|
|
|
|
intrinsics.insert(~"llvm.trap", trap);
|
2010-11-25 17:45:26 -08:00
|
|
|
ret intrinsics;
|
2010-11-14 11:21:49 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trap(bcx: &@block_ctxt) {
|
2011-08-19 15:16:48 -07:00
|
|
|
let v: [ValueRef] = [];
|
2011-08-25 15:12:54 -07:00
|
|
|
alt bcx_ccx(bcx).intrinsics.find(~"llvm.trap") {
|
2011-08-30 09:59:30 +02:00
|
|
|
some(x) { Call(bcx, x, v); }
|
2011-08-27 16:36:48 -07:00
|
|
|
_ { bcx_ccx(bcx).sess.bug(~"unbound llvm.trap in trap"); }
|
2011-07-05 19:57:34 -07:00
|
|
|
}
|
2011-02-02 15:23:49 -08:00
|
|
|
}
|
2010-12-06 17:17:49 -08:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn decl_no_op_type_glue(llmod: ModuleRef, taskptr_type: TypeRef) -> ValueRef {
|
2011-08-19 15:16:48 -07:00
|
|
|
let ty = T_fn([taskptr_type, T_ptr(T_i8())], T_void());
|
2011-08-25 21:08:32 -07:00
|
|
|
ret decl_fastcall_fn(llmod,
|
2011-08-26 21:34:56 -07:00
|
|
|
abi::no_op_type_glue_name(), ty);
|
2011-03-10 17:25:11 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn vec_fill(bcx: &@block_ctxt, v: ValueRef) -> ValueRef {
|
2011-08-30 09:59:30 +02:00
|
|
|
ret Load(bcx, GEP(bcx, v,
|
2011-08-19 15:16:48 -07:00
|
|
|
[C_int(0), C_int(abi::vec_elt_fill)]));
|
2011-03-09 20:14:19 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn vec_p0(bcx: &@block_ctxt, v: ValueRef) -> ValueRef {
|
2011-08-30 09:59:30 +02:00
|
|
|
let p = GEP(bcx, v, [C_int(0), C_int(abi::vec_elt_data)]);
|
|
|
|
ret PointerCast(bcx, p, T_ptr(T_i8()));
|
2011-03-09 20:14:19 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn make_glues(llmod: ModuleRef, taskptr_type: TypeRef) -> @glue_fns {
|
|
|
|
ret @{no_op_type_glue: decl_no_op_type_glue(llmod, taskptr_type)};
|
2010-12-17 18:40:04 -08:00
|
|
|
}
|
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
fn make_common_glue(sess: &session::session, output: &istr) {
|
2011-03-10 17:25:11 -08:00
|
|
|
// FIXME: part of this is repetitive and is probably a good idea
|
2011-05-10 14:22:14 -07:00
|
|
|
// to autogen it.
|
2011-07-27 14:19:39 +02:00
|
|
|
let task_type = T_task();
|
|
|
|
let taskptr_type = T_ptr(task_type);
|
2011-07-13 20:06:27 -04:00
|
|
|
|
2011-08-26 15:36:18 -07:00
|
|
|
let llmod = istr::as_buf(~"rust_out", { |buf|
|
|
|
|
llvm::LLVMModuleCreateWithNameInContext(buf,
|
|
|
|
llvm::LLVMGetGlobalContext())
|
|
|
|
});
|
|
|
|
let _: () = istr::as_buf(x86::get_data_layout(), { |buf|
|
|
|
|
llvm::LLVMSetDataLayout(llmod, buf)
|
|
|
|
});
|
|
|
|
let _: () = istr::as_buf(x86::get_target_triple(), { |buf|
|
|
|
|
llvm::LLVMSetTarget(llmod, buf)
|
|
|
|
});
|
2011-08-26 16:33:30 -07:00
|
|
|
mk_target_data(x86::get_data_layout());
|
2011-06-30 09:00:44 -07:00
|
|
|
declare_intrinsics(llmod);
|
2011-08-26 15:36:18 -07:00
|
|
|
let _: () = istr::as_buf(x86::get_module_asm(), { |buf|
|
|
|
|
llvm::LLVMSetModuleInlineAsm(llmod, buf)
|
|
|
|
});
|
2011-07-13 20:06:27 -04:00
|
|
|
make_glues(llmod, taskptr_type);
|
2011-08-26 21:34:56 -07:00
|
|
|
link::write::run_passes(sess, llmod, output);
|
2011-03-10 17:25:11 -08:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn create_module_map(ccx: &@crate_ctxt) -> ValueRef {
|
2011-08-19 15:16:48 -07:00
|
|
|
let elttype = T_struct([T_int(), T_int()]);
|
2011-07-27 14:19:39 +02:00
|
|
|
let maptype = T_array(elttype, ccx.module_data.size() + 1u);
|
2011-08-26 15:36:18 -07:00
|
|
|
let map = istr::as_buf(~"_rust_mod_map", { |buf|
|
|
|
|
llvm::LLVMAddGlobal(ccx.llmod, maptype, buf)
|
|
|
|
});
|
2011-07-08 15:03:48 -04:00
|
|
|
llvm::LLVMSetLinkage(map,
|
2011-07-27 14:19:39 +02:00
|
|
|
lib::llvm::LLVMInternalLinkage as llvm::Linkage);
|
2011-08-19 15:16:48 -07:00
|
|
|
let elts: [ValueRef] = [];
|
2011-08-25 15:12:54 -07:00
|
|
|
for each item: @{key: istr, val: ValueRef} in ccx.module_data.items() {
|
2011-08-26 16:54:25 -07:00
|
|
|
let elt = C_struct([p2i(C_cstr(ccx, item.key)),
|
2011-08-25 15:12:54 -07:00
|
|
|
p2i(item.val)]);
|
2011-08-19 15:16:48 -07:00
|
|
|
elts += [elt];
|
Make log the log level configurable per module
This overloads the meaning of RUST_LOG to also allow
'module.submodule' or 'module.somethingelse=2' forms. The first turn
on all logging for a module (loglevel 3), the second sets its loglevel
to 2. Log levels are:
0: Show only errors
1: Errors and warnings
2: Errors, warnings, and notes
3: Everything, including debug logging
Right now, since we only have one 'log' operation, everything happens
at level 1 (warning), so the only meaningful thing that can be done
with the new RUST_LOG support is disable logging (=0) for some
modules.
TODOS:
* Language support for logging at a specific level
* Also add a log level field to tasks, query the current task as well
as the current module before logging (log if one of them allows it)
* Revise the C logging API to conform to this set-up (globals for
per-module log level, query the task level before logging, stop
using a global mask)
Implementation notes:
Crates now contain two extra data structures. A 'module map' that
contains names and pointers to the module-log-level globals for each
module in the crate that logs, and a 'crate map' that points at the
crate's module map, as well as at the crate maps of all external
crates it depends on. These are walked by the runtime (in
rust_crate.cpp) to set the currect log levels based on RUST_LOG.
These module log globals are allocated as-needed whenever a log
expression is encountered, and their location is hard-coded into the
logging code, which compares the current level to the log statement's
level, and skips over all logging code when it is lower.
2011-04-17 16:29:18 +02:00
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
let term = C_struct([C_int(0), C_int(0)]);
|
|
|
|
elts += [term];
|
2011-05-12 17:24:54 +02:00
|
|
|
llvm::LLVMSetInitializer(map, C_array(elttype, elts));
|
Make log the log level configurable per module
This overloads the meaning of RUST_LOG to also allow
'module.submodule' or 'module.somethingelse=2' forms. The first turn
on all logging for a module (loglevel 3), the second sets its loglevel
to 2. Log levels are:
0: Show only errors
1: Errors and warnings
2: Errors, warnings, and notes
3: Everything, including debug logging
Right now, since we only have one 'log' operation, everything happens
at level 1 (warning), so the only meaningful thing that can be done
with the new RUST_LOG support is disable logging (=0) for some
modules.
TODOS:
* Language support for logging at a specific level
* Also add a log level field to tasks, query the current task as well
as the current module before logging (log if one of them allows it)
* Revise the C logging API to conform to this set-up (globals for
per-module log level, query the task level before logging, stop
using a global mask)
Implementation notes:
Crates now contain two extra data structures. A 'module map' that
contains names and pointers to the module-log-level globals for each
module in the crate that logs, and a 'crate map' that points at the
crate's module map, as well as at the crate maps of all external
crates it depends on. These are walked by the runtime (in
rust_crate.cpp) to set the currect log levels based on RUST_LOG.
These module log globals are allocated as-needed whenever a log
expression is encountered, and their location is hard-coded into the
logging code, which compares the current level to the log statement's
level, and skips over all logging code when it is lower.
2011-04-17 16:29:18 +02:00
|
|
|
ret map;
|
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
Make log the log level configurable per module
This overloads the meaning of RUST_LOG to also allow
'module.submodule' or 'module.somethingelse=2' forms. The first turn
on all logging for a module (loglevel 3), the second sets its loglevel
to 2. Log levels are:
0: Show only errors
1: Errors and warnings
2: Errors, warnings, and notes
3: Everything, including debug logging
Right now, since we only have one 'log' operation, everything happens
at level 1 (warning), so the only meaningful thing that can be done
with the new RUST_LOG support is disable logging (=0) for some
modules.
TODOS:
* Language support for logging at a specific level
* Also add a log level field to tasks, query the current task as well
as the current module before logging (log if one of them allows it)
* Revise the C logging API to conform to this set-up (globals for
per-module log level, query the task level before logging, stop
using a global mask)
Implementation notes:
Crates now contain two extra data structures. A 'module map' that
contains names and pointers to the module-log-level globals for each
module in the crate that logs, and a 'crate map' that points at the
crate's module map, as well as at the crate maps of all external
crates it depends on. These are walked by the runtime (in
rust_crate.cpp) to set the currect log levels based on RUST_LOG.
These module log globals are allocated as-needed whenever a log
expression is encountered, and their location is hard-coded into the
logging code, which compares the current level to the log statement's
level, and skips over all logging code when it is lower.
2011-04-17 16:29:18 +02:00
|
|
|
// FIXME use hashed metadata instead of crate names once we have that
|
2011-07-27 14:19:39 +02:00
|
|
|
fn create_crate_map(ccx: &@crate_ctxt) -> ValueRef {
|
2011-08-19 15:16:48 -07:00
|
|
|
let subcrates: [ValueRef] = [];
|
2011-07-27 14:19:39 +02:00
|
|
|
let i = 1;
|
|
|
|
let cstore = ccx.sess.get_cstore();
|
|
|
|
while cstore::have_crate_data(cstore, i) {
|
2011-08-26 22:48:10 -07:00
|
|
|
let nm = ~"_rust_crate_map_" +
|
|
|
|
cstore::get_crate_data(cstore, i).name;
|
|
|
|
let cr = istr::as_buf(nm, { |buf|
|
2011-08-26 15:36:18 -07:00
|
|
|
llvm::LLVMAddGlobal(ccx.llmod, T_int(), buf)
|
|
|
|
});
|
2011-08-19 15:16:48 -07:00
|
|
|
subcrates += [p2i(cr)];
|
Make log the log level configurable per module
This overloads the meaning of RUST_LOG to also allow
'module.submodule' or 'module.somethingelse=2' forms. The first turn
on all logging for a module (loglevel 3), the second sets its loglevel
to 2. Log levels are:
0: Show only errors
1: Errors and warnings
2: Errors, warnings, and notes
3: Everything, including debug logging
Right now, since we only have one 'log' operation, everything happens
at level 1 (warning), so the only meaningful thing that can be done
with the new RUST_LOG support is disable logging (=0) for some
modules.
TODOS:
* Language support for logging at a specific level
* Also add a log level field to tasks, query the current task as well
as the current module before logging (log if one of them allows it)
* Revise the C logging API to conform to this set-up (globals for
per-module log level, query the task level before logging, stop
using a global mask)
Implementation notes:
Crates now contain two extra data structures. A 'module map' that
contains names and pointers to the module-log-level globals for each
module in the crate that logs, and a 'crate map' that points at the
crate's module map, as well as at the crate maps of all external
crates it depends on. These are walked by the runtime (in
rust_crate.cpp) to set the currect log levels based on RUST_LOG.
These module log globals are allocated as-needed whenever a log
expression is encountered, and their location is hard-coded into the
logging code, which compares the current level to the log statement's
level, and skips over all logging code when it is lower.
2011-04-17 16:29:18 +02:00
|
|
|
i += 1;
|
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
subcrates += [C_int(0)];
|
2011-07-27 14:19:39 +02:00
|
|
|
let mapname;
|
|
|
|
if ccx.sess.get_opts().library {
|
2011-06-29 12:18:43 -07:00
|
|
|
mapname = ccx.link_meta.name;
|
2011-08-25 21:04:04 -07:00
|
|
|
} else { mapname = ~"toplevel"; }
|
2011-08-26 15:36:18 -07:00
|
|
|
let sym_name = ~"_rust_crate_map_" + mapname;
|
2011-08-13 00:09:25 -07:00
|
|
|
let arrtype = T_array(T_int(), std::vec::len::<ValueRef>(subcrates));
|
2011-08-19 15:16:48 -07:00
|
|
|
let maptype = T_struct([T_int(), arrtype]);
|
2011-08-26 15:36:18 -07:00
|
|
|
let map = istr::as_buf(sym_name, { |buf|
|
|
|
|
llvm::LLVMAddGlobal(ccx.llmod, maptype, buf)
|
|
|
|
});
|
2011-06-15 11:19:50 -07:00
|
|
|
llvm::LLVMSetLinkage(map,
|
|
|
|
lib::llvm::LLVMExternalLinkage as llvm::Linkage);
|
|
|
|
llvm::LLVMSetInitializer(map,
|
2011-08-19 15:16:48 -07:00
|
|
|
C_struct([p2i(create_module_map(ccx)),
|
|
|
|
C_array(T_int(), subcrates)]));
|
Make log the log level configurable per module
This overloads the meaning of RUST_LOG to also allow
'module.submodule' or 'module.somethingelse=2' forms. The first turn
on all logging for a module (loglevel 3), the second sets its loglevel
to 2. Log levels are:
0: Show only errors
1: Errors and warnings
2: Errors, warnings, and notes
3: Everything, including debug logging
Right now, since we only have one 'log' operation, everything happens
at level 1 (warning), so the only meaningful thing that can be done
with the new RUST_LOG support is disable logging (=0) for some
modules.
TODOS:
* Language support for logging at a specific level
* Also add a log level field to tasks, query the current task as well
as the current module before logging (log if one of them allows it)
* Revise the C logging API to conform to this set-up (globals for
per-module log level, query the task level before logging, stop
using a global mask)
Implementation notes:
Crates now contain two extra data structures. A 'module map' that
contains names and pointers to the module-log-level globals for each
module in the crate that logs, and a 'crate map' that points at the
crate's module map, as well as at the crate maps of all external
crates it depends on. These are walked by the runtime (in
rust_crate.cpp) to set the currect log levels based on RUST_LOG.
These module log globals are allocated as-needed whenever a log
expression is encountered, and their location is hard-coded into the
logging code, which compares the current level to the log statement's
level, and skips over all logging code when it is lower.
2011-04-17 16:29:18 +02:00
|
|
|
ret map;
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn write_metadata(cx: &@crate_ctxt, crate: &@ast::crate) {
|
|
|
|
if !cx.sess.get_opts().library { ret; }
|
2011-08-26 16:54:25 -07:00
|
|
|
let llmeta = C_postr(
|
2011-08-26 22:48:10 -07:00
|
|
|
metadata::encoder::encode_metadata(cx, crate));
|
2011-08-19 15:16:48 -07:00
|
|
|
let llconst = trans_common::C_struct([llmeta]);
|
2011-08-26 15:36:18 -07:00
|
|
|
let llglobal = istr::as_buf(~"rust_metadata", { |buf|
|
|
|
|
llvm::LLVMAddGlobal(cx.llmod, val_ty(llconst), buf)
|
|
|
|
});
|
2011-06-27 16:09:28 -07:00
|
|
|
llvm::LLVMSetInitializer(llglobal, llconst);
|
2011-08-26 15:36:18 -07:00
|
|
|
let _: () = istr::as_buf(x86::get_meta_sect_name(), { |buf|
|
|
|
|
llvm::LLVMSetSection(llglobal, buf)
|
|
|
|
});
|
2011-07-20 14:52:31 -04:00
|
|
|
llvm::LLVMSetLinkage(llglobal,
|
|
|
|
lib::llvm::LLVMInternalLinkage as llvm::Linkage);
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
let t_ptr_i8 = T_ptr(T_i8());
|
2011-07-20 14:52:31 -04:00
|
|
|
llglobal = llvm::LLVMConstBitCast(llglobal, t_ptr_i8);
|
2011-08-26 15:36:18 -07:00
|
|
|
let llvm_used = istr::as_buf(~"llvm.used", { |buf|
|
|
|
|
llvm::LLVMAddGlobal(cx.llmod, T_array(t_ptr_i8, 1u), buf)
|
|
|
|
});
|
2011-07-20 14:52:31 -04:00
|
|
|
llvm::LLVMSetLinkage(llvm_used,
|
|
|
|
lib::llvm::LLVMAppendingLinkage as llvm::Linkage);
|
2011-08-19 15:16:48 -07:00
|
|
|
llvm::LLVMSetInitializer(llvm_used, C_array(t_ptr_i8, [llglobal]));
|
2011-06-27 16:09:28 -07:00
|
|
|
}
|
|
|
|
|
2011-08-20 14:22:09 -07:00
|
|
|
// Writes the current ABI version into the crate.
|
|
|
|
fn write_abi_version(ccx: &@crate_ctxt) {
|
2011-08-26 22:09:06 -07:00
|
|
|
shape::mk_global(ccx, ~"rust_abi_version", C_uint(abi::abi_version),
|
2011-08-20 14:22:09 -07:00
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn trans_crate(sess: &session::session, crate: &@ast::crate, tcx: &ty::ctxt,
|
2011-08-26 21:34:56 -07:00
|
|
|
output: &istr, amap: &ast_map::map, mut_map: alias::mut_map)
|
2011-08-19 14:34:45 +02:00
|
|
|
-> ModuleRef {
|
2011-08-26 15:36:18 -07:00
|
|
|
let llmod = istr::as_buf(~"rust_out", { |buf|
|
|
|
|
llvm::LLVMModuleCreateWithNameInContext(buf,
|
|
|
|
llvm::LLVMGetGlobalContext())
|
|
|
|
});
|
|
|
|
let _: () = istr::as_buf(x86::get_data_layout(), { |buf|
|
|
|
|
llvm::LLVMSetDataLayout(llmod, buf)
|
|
|
|
});
|
|
|
|
let _: () = istr::as_buf(x86::get_target_triple(), { |buf|
|
|
|
|
llvm::LLVMSetTarget(llmod, buf)
|
|
|
|
});
|
2011-08-26 16:33:30 -07:00
|
|
|
let td = mk_target_data(x86::get_data_layout());
|
2011-07-27 14:19:39 +02:00
|
|
|
let tn = mk_type_names();
|
|
|
|
let intrinsics = declare_intrinsics(llmod);
|
|
|
|
let task_type = T_task();
|
|
|
|
let taskptr_type = T_ptr(task_type);
|
2011-08-26 16:33:30 -07:00
|
|
|
tn.associate(~"taskptr", taskptr_type);
|
2011-07-27 14:19:39 +02:00
|
|
|
let tydesc_type = T_tydesc(taskptr_type);
|
2011-08-26 16:33:30 -07:00
|
|
|
tn.associate(~"tydesc", tydesc_type);
|
2011-07-27 14:19:39 +02:00
|
|
|
let glues = make_glues(llmod, taskptr_type);
|
|
|
|
let hasher = ty::hash_ty;
|
|
|
|
let eqer = ty::eq_ty;
|
2011-08-13 00:09:25 -07:00
|
|
|
let tag_sizes = map::mk_hashmap::<ty::t, uint>(hasher, eqer);
|
|
|
|
let tydescs = map::mk_hashmap::<ty::t, @tydesc_info>(hasher, eqer);
|
|
|
|
let lltypes = map::mk_hashmap::<ty::t, TypeRef>(hasher, eqer);
|
2011-08-25 21:04:04 -07:00
|
|
|
let sha1s = map::mk_hashmap::<ty::t, istr>(hasher, eqer);
|
2011-08-26 17:14:13 -07:00
|
|
|
let short_names = map::mk_hashmap::<ty::t, istr>(hasher, eqer);
|
2011-07-27 14:19:39 +02:00
|
|
|
let sha = std::sha1::mk_sha1();
|
|
|
|
let ccx =
|
|
|
|
@{sess: sess,
|
|
|
|
llmod: llmod,
|
|
|
|
td: td,
|
|
|
|
tn: tn,
|
2011-08-13 00:09:25 -07:00
|
|
|
externs: new_str_hash::<ValueRef>(),
|
2011-07-27 14:19:39 +02:00
|
|
|
intrinsics: intrinsics,
|
2011-08-13 00:09:25 -07:00
|
|
|
item_ids: new_int_hash::<ValueRef>(),
|
2011-07-27 14:19:39 +02:00
|
|
|
ast_map: amap,
|
2011-08-26 17:14:13 -07:00
|
|
|
item_symbols: new_int_hash::<istr>(),
|
2011-08-13 00:09:25 -07:00
|
|
|
mutable main_fn: none::<ValueRef>,
|
2011-08-25 21:04:04 -07:00
|
|
|
link_meta: link::build_link_meta(sess, *crate,
|
2011-08-26 21:34:56 -07:00
|
|
|
output, sha),
|
2011-07-27 14:19:39 +02:00
|
|
|
tag_sizes: tag_sizes,
|
2011-08-13 00:09:25 -07:00
|
|
|
discrims: new_int_hash::<ValueRef>(),
|
2011-08-26 17:14:13 -07:00
|
|
|
discrim_symbols: new_int_hash::<istr>(),
|
2011-08-13 00:09:25 -07:00
|
|
|
fn_pairs: new_int_hash::<ValueRef>(),
|
|
|
|
consts: new_int_hash::<ValueRef>(),
|
|
|
|
obj_methods: new_int_hash::<()>(),
|
2011-07-27 14:19:39 +02:00
|
|
|
tydescs: tydescs,
|
2011-08-13 00:09:25 -07:00
|
|
|
module_data: new_str_hash::<ValueRef>(),
|
2011-07-27 14:19:39 +02:00
|
|
|
lltypes: lltypes,
|
|
|
|
glues: glues,
|
|
|
|
names: namegen(0),
|
|
|
|
sha: sha,
|
|
|
|
type_sha1s: sha1s,
|
|
|
|
type_short_names: short_names,
|
|
|
|
tcx: tcx,
|
2011-08-19 14:34:45 +02:00
|
|
|
mut_map: mut_map,
|
2011-07-27 14:19:39 +02:00
|
|
|
stats:
|
|
|
|
{mutable n_static_tydescs: 0u,
|
|
|
|
mutable n_derived_tydescs: 0u,
|
|
|
|
mutable n_glues_created: 0u,
|
|
|
|
mutable n_null_glues: 0u,
|
|
|
|
mutable n_real_glues: 0u,
|
2011-08-19 15:16:48 -07:00
|
|
|
fn_times: @mutable []},
|
2011-07-27 14:19:39 +02:00
|
|
|
upcalls:
|
|
|
|
upcall::declare_upcalls(tn, tydesc_type, taskptr_type, llmod),
|
|
|
|
rust_object_type: T_rust_object(),
|
|
|
|
tydesc_type: tydesc_type,
|
2011-08-04 10:46:10 -07:00
|
|
|
task_type: task_type,
|
2011-08-24 16:30:20 +02:00
|
|
|
builder: BuilderRef_res(llvm::LLVMCreateBuilder()),
|
2011-08-17 19:11:01 -07:00
|
|
|
shape_cx: shape::mk_ctxt(llmod),
|
2011-08-26 13:58:26 -07:00
|
|
|
gc_cx: gc::mk_ctxt()};
|
2011-07-27 14:19:39 +02:00
|
|
|
let cx = new_local_ctxt(ccx);
|
2011-04-20 17:23:45 +02:00
|
|
|
collect_items(ccx, crate);
|
2011-04-20 17:43:13 +02:00
|
|
|
collect_tag_ctors(ccx, crate);
|
|
|
|
trans_constants(ccx, crate);
|
2010-10-05 18:21:44 -07:00
|
|
|
trans_mod(cx, crate.node.module);
|
2011-06-30 09:00:44 -07:00
|
|
|
create_crate_map(ccx);
|
2011-05-12 15:42:12 -07:00
|
|
|
emit_tydescs(ccx);
|
2011-08-04 11:25:09 -07:00
|
|
|
shape::gen_shape_tables(ccx);
|
2011-08-20 14:22:09 -07:00
|
|
|
write_abi_version(ccx);
|
2011-03-11 15:35:20 -08:00
|
|
|
|
2011-08-04 11:25:09 -07:00
|
|
|
// Translate the metadata.
|
2011-06-27 16:09:28 -07:00
|
|
|
write_metadata(cx.ccx, crate);
|
2011-07-27 14:19:39 +02:00
|
|
|
if ccx.sess.get_opts().stats {
|
2011-05-12 15:42:12 -07:00
|
|
|
log_err "--- trans stats ---";
|
2011-08-19 15:16:48 -07:00
|
|
|
log_err #fmt["n_static_tydescs: %u", ccx.stats.n_static_tydescs];
|
|
|
|
log_err #fmt["n_derived_tydescs: %u", ccx.stats.n_derived_tydescs];
|
|
|
|
log_err #fmt["n_glues_created: %u", ccx.stats.n_glues_created];
|
|
|
|
log_err #fmt["n_null_glues: %u", ccx.stats.n_null_glues];
|
|
|
|
log_err #fmt["n_real_glues: %u", ccx.stats.n_real_glues];
|
2011-07-19 11:56:46 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
|
2011-08-26 21:34:56 -07:00
|
|
|
for timing: {ident: istr, time: int} in *ccx.stats.fn_times {
|
|
|
|
log_err #fmt["time: %s took %d ms",
|
|
|
|
istr::to_estr(timing.ident), timing.time];
|
2011-07-19 11:56:46 -07:00
|
|
|
}
|
2011-05-12 15:42:12 -07:00
|
|
|
}
|
2011-05-02 17:45:07 -07:00
|
|
|
ret llmod;
|
2010-09-22 15:21:06 -07:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-03-25 15:07:27 -07:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-09-22 15:21:06 -07:00
|
|
|
// End:
|
|
|
|
//
|