2011-07-14 19:08:22 -05:00
|
|
|
/**
|
|
|
|
Code that is useful in various trans modules.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
import libc::c_uint;
|
2012-02-28 22:43:39 -06:00
|
|
|
import vec::unsafe::to_ptr;
|
2012-03-20 16:21:02 -05:00
|
|
|
import std::map::{hashmap,set};
|
2012-05-22 01:34:34 -05:00
|
|
|
import syntax::{ast, ast_map};
|
2011-07-14 19:08:22 -05:00
|
|
|
import driver::session;
|
2012-01-12 10:59:49 -06:00
|
|
|
import session::session;
|
2011-12-19 13:56:11 -06:00
|
|
|
import middle::{resolve, ty};
|
2011-11-10 10:41:42 -06:00
|
|
|
import back::{link, abi, upcall};
|
2011-07-14 19:08:22 -05:00
|
|
|
import syntax::codemap::span;
|
2012-01-13 02:32:05 -06:00
|
|
|
import lib::llvm::{llvm, target_data, type_names, associate_type,
|
|
|
|
name_has_type};
|
2012-02-01 04:04:56 -06:00
|
|
|
import lib::llvm::{ModuleRef, ValueRef, TypeRef, BasicBlockRef, BuilderRef};
|
2011-09-12 18:13:28 -05:00
|
|
|
import lib::llvm::{True, False, Bool};
|
2012-05-17 18:17:11 -05:00
|
|
|
import metadata::{csearch};
|
|
|
|
import metadata::common::link_meta;
|
2012-05-22 01:34:34 -05:00
|
|
|
import syntax::ast_map::path;
|
2012-05-14 22:32:29 -05:00
|
|
|
import util::ppaux::ty_to_str;
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
type namegen = fn@(~str) -> ~str;
|
2012-01-13 02:32:05 -06:00
|
|
|
fn new_namegen() -> namegen {
|
2012-03-26 20:35:18 -05:00
|
|
|
let i = @mut 0;
|
2012-07-14 00:57:48 -05:00
|
|
|
ret fn@(prefix: ~str) -> ~str { *i += 1; prefix + int::str(*i) };
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type tydesc_info =
|
2011-07-27 07:19:39 -05:00
|
|
|
{ty: ty::t,
|
|
|
|
tydesc: ValueRef,
|
|
|
|
size: ValueRef,
|
|
|
|
align: ValueRef,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut take_glue: option<ValueRef>,
|
|
|
|
mut drop_glue: option<ValueRef>,
|
2012-05-14 19:57:39 -05:00
|
|
|
mut free_glue: option<ValueRef>,
|
|
|
|
mut visit_glue: option<ValueRef>};
|
2011-07-21 19:27:34 -05:00
|
|
|
|
|
|
|
/*
|
2012-06-26 18:18:37 -05:00
|
|
|
* A note on nomenclature of linking: "extern", "foreign", and "upcall".
|
2011-07-21 19:27:34 -05:00
|
|
|
*
|
|
|
|
* An "extern" is an LLVM symbol we wind up emitting an undefined external
|
|
|
|
* reference to. This means "we don't have the thing in this compilation unit,
|
|
|
|
* please make sure you link it in at runtime". This could be a reference to
|
|
|
|
* C code found in a C library, or rust code found in a rust crate.
|
|
|
|
*
|
2012-06-26 18:18:37 -05:00
|
|
|
* Most "externs" are implicitly declared (automatically) as a result of a
|
|
|
|
* user declaring an extern _module_ dependency; this causes the rust driver
|
|
|
|
* to locate an extern crate, scan its compilation metadata, and emit extern
|
|
|
|
* declarations for any symbols used by the declaring crate.
|
2011-07-21 19:27:34 -05:00
|
|
|
*
|
2012-06-26 18:18:37 -05:00
|
|
|
* A "foreign" is an extern that references C (or other non-rust ABI) code.
|
|
|
|
* There is no metadata to scan for extern references so in these cases either
|
|
|
|
* a header-digester like bindgen, or manual function prototypes, have to
|
|
|
|
* serve as declarators. So these are usually given explicitly as prototype
|
|
|
|
* declarations, in rust code, with ABI attributes on them noting which ABI to
|
|
|
|
* link via.
|
|
|
|
*
|
|
|
|
* An "upcall" is a foreign call generated by the compiler (not corresponding
|
|
|
|
* to any user-written call in the code) into the runtime library, to perform
|
|
|
|
* some helper task such as bringing a task to life, allocating memory, etc.
|
2011-07-21 19:27:34 -05:00
|
|
|
*
|
|
|
|
*/
|
2012-03-22 15:44:20 -05:00
|
|
|
|
2011-07-21 19:27:34 -05:00
|
|
|
type stats =
|
2012-03-26 20:35:18 -05:00
|
|
|
{mut n_static_tydescs: uint,
|
|
|
|
mut n_glues_created: uint,
|
|
|
|
mut n_null_glues: uint,
|
|
|
|
mut n_real_glues: uint,
|
2012-07-14 00:57:48 -05:00
|
|
|
llvm_insn_ctxt: @mut ~[~str],
|
|
|
|
llvm_insns: hashmap<~str, uint>,
|
|
|
|
fn_times: @mut ~[{ident: ~str, time: int}]};
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2012-06-22 13:53:25 -05:00
|
|
|
class BuilderRef_res {
|
|
|
|
let B: BuilderRef;
|
|
|
|
new(B: BuilderRef) { self.B = B; }
|
|
|
|
drop { llvm::LLVMDisposeBuilder(self.B); }
|
|
|
|
}
|
2011-08-24 09:30:20 -05:00
|
|
|
|
2011-07-21 19:27:34 -05:00
|
|
|
// Crate context. Every crate we compile has one of these.
|
2012-03-14 19:31:16 -05:00
|
|
|
type crate_ctxt = {
|
2012-02-21 08:11:20 -06:00
|
|
|
sess: session::session,
|
2011-08-19 17:16:48 -05:00
|
|
|
llmod: ModuleRef,
|
|
|
|
td: target_data,
|
|
|
|
tn: type_names,
|
2012-07-14 00:57:48 -05:00
|
|
|
externs: hashmap<~str, ValueRef>,
|
|
|
|
intrinsics: hashmap<~str, ValueRef>,
|
2012-03-07 05:21:08 -06:00
|
|
|
item_vals: hashmap<ast::node_id, ValueRef>,
|
2011-12-19 13:56:11 -06:00
|
|
|
exp_map: resolve::exp_map,
|
2012-03-20 06:28:46 -05:00
|
|
|
reachable: reachable::map,
|
2012-07-14 00:57:48 -05:00
|
|
|
item_symbols: hashmap<ast::node_id, ~str>,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut main_fn: option<ValueRef>,
|
2012-05-17 18:17:11 -05:00
|
|
|
link_meta: link_meta,
|
2012-01-25 07:34:31 -06:00
|
|
|
enum_sizes: hashmap<ty::t, uint>,
|
2011-10-20 17:26:26 -05:00
|
|
|
discrims: hashmap<ast::def_id, ValueRef>,
|
2012-07-14 00:57:48 -05:00
|
|
|
discrim_symbols: hashmap<ast::node_id, ~str>,
|
2011-08-19 17:16:48 -05:00
|
|
|
tydescs: hashmap<ty::t, @tydesc_info>,
|
2012-03-06 04:33:25 -06:00
|
|
|
// Track mapping of external ids to local items imported for inlining
|
|
|
|
external: hashmap<ast::def_id, option<ast::node_id>>,
|
|
|
|
// Cache instances of monomorphized functions
|
2012-03-08 12:02:22 -06:00
|
|
|
monomorphized: hashmap<mono_id, ValueRef>,
|
2012-04-23 09:44:52 -05:00
|
|
|
monomorphizing: hashmap<ast::def_id, uint>,
|
2012-03-12 10:31:22 -05:00
|
|
|
// Cache computed type parameter uses (see type_use.rs)
|
2012-06-29 18:26:56 -05:00
|
|
|
type_use_cache: hashmap<ast::def_id, ~[type_use::type_uses]>,
|
2012-03-08 09:10:25 -06:00
|
|
|
// Cache generated vtables
|
|
|
|
vtables: hashmap<mono_id, ValueRef>,
|
2012-04-21 15:23:25 -05:00
|
|
|
// Cache of constant strings,
|
2012-07-14 00:57:48 -05:00
|
|
|
const_cstr_cache: hashmap<~str, ValueRef>,
|
|
|
|
module_data: hashmap<~str, ValueRef>,
|
2011-08-19 17:16:48 -05:00
|
|
|
lltypes: hashmap<ty::t, TypeRef>,
|
|
|
|
names: namegen,
|
|
|
|
sha: std::sha1::sha1,
|
2012-07-14 00:57:48 -05:00
|
|
|
type_sha1s: hashmap<ty::t, ~str>,
|
|
|
|
type_short_names: hashmap<ty::t, ~str>,
|
|
|
|
all_llvm_symbols: set<~str>,
|
2011-08-19 17:16:48 -05:00
|
|
|
tcx: ty::ctxt,
|
2012-05-14 19:46:45 -05:00
|
|
|
maps: astencode::maps,
|
2011-08-19 17:16:48 -05:00
|
|
|
stats: stats,
|
|
|
|
upcalls: @upcall::upcalls,
|
|
|
|
tydesc_type: TypeRef,
|
2011-10-14 19:00:17 -05:00
|
|
|
int_type: TypeRef,
|
|
|
|
float_type: TypeRef,
|
2011-08-19 17:16:48 -05:00
|
|
|
task_type: TypeRef,
|
2011-10-14 22:38:24 -05:00
|
|
|
opaque_vec_type: TypeRef,
|
2011-08-24 09:30:20 -05:00
|
|
|
builder: BuilderRef_res,
|
2011-08-19 17:16:48 -05:00
|
|
|
shape_cx: shape::ctxt,
|
2011-11-09 23:55:09 -06:00
|
|
|
crate_map: ValueRef,
|
2012-03-21 17:56:20 -05:00
|
|
|
dbg_cx: option<debuginfo::debug_ctxt>,
|
2012-03-23 21:49:01 -05:00
|
|
|
// Mapping from class constructors to parent class --
|
|
|
|
// used in base::trans_closure
|
2012-04-10 12:52:06 -05:00
|
|
|
// parent_class must be a def_id because ctors can be
|
|
|
|
// inlined, so the parent may be in a different crate
|
|
|
|
class_ctors: hashmap<ast::node_id, ast::def_id>,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut do_not_commit_warning_issued: bool};
|
2011-07-21 19:27:34 -05:00
|
|
|
|
|
|
|
// Types used for llself.
|
2011-07-27 07:19:39 -05:00
|
|
|
type val_self_pair = {v: ValueRef, t: ty::t};
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2012-01-19 19:56:05 -06:00
|
|
|
enum local_val { local_mem(ValueRef), local_imm(ValueRef), }
|
2011-10-07 04:20:51 -05:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
type param_substs = {tys: ~[ty::t],
|
2012-03-08 09:17:59 -06:00
|
|
|
vtables: option<typeck::vtable_res>,
|
2012-06-29 18:26:56 -05:00
|
|
|
bounds: @~[ty::param_bounds]};
|
2012-02-09 04:17:11 -06:00
|
|
|
|
2011-08-03 17:39:43 -05:00
|
|
|
// Function context. Every LLVM function we create will have one of
|
|
|
|
// these.
|
2012-02-21 08:11:20 -06:00
|
|
|
type fn_ctxt = @{
|
2011-07-27 07:19:39 -05:00
|
|
|
// The ValueRef returned from a call to llvm::LLVMAddFunction; the
|
2011-08-03 17:39:43 -05:00
|
|
|
// address of the first instruction in the sequence of
|
|
|
|
// instructions for this function that will go in the .text
|
|
|
|
// section of the executable we're generating.
|
2012-02-03 06:37:55 -06:00
|
|
|
llfn: ValueRef,
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2012-02-03 06:37:55 -06:00
|
|
|
// The two implicit arguments that arrive in the function we're creating.
|
|
|
|
// For instance, foo(int, int) is really foo(ret*, env*, int, int).
|
|
|
|
llenv: ValueRef,
|
|
|
|
llretptr: ValueRef,
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2012-02-03 06:37:55 -06:00
|
|
|
// These elements: "hoisted basic blocks" containing
|
2011-07-27 07:19:39 -05:00
|
|
|
// administrative activities that have to happen in only one place in
|
|
|
|
// the function, due to LLVM's quirks.
|
2011-08-03 17:39:43 -05:00
|
|
|
// A block for all the function's static allocas, so that LLVM
|
|
|
|
// will coalesce them into a single alloca call.
|
2012-03-26 20:35:18 -05:00
|
|
|
mut llstaticallocas: BasicBlockRef,
|
2011-07-27 07:19:39 -05:00
|
|
|
// A block containing code that copies incoming arguments to space
|
2011-08-03 17:39:43 -05:00
|
|
|
// already allocated by code in one of the llallocas blocks.
|
|
|
|
// (LLVM requires that arguments be copied to local allocas before
|
|
|
|
// allowing most any operation to be performed on them.)
|
2012-03-26 20:35:18 -05:00
|
|
|
mut llloadenv: BasicBlockRef,
|
|
|
|
mut llreturn: BasicBlockRef,
|
2012-01-13 03:58:31 -06:00
|
|
|
// The 'self' value currently in use in this function, if there
|
2011-08-03 17:39:43 -05:00
|
|
|
// is one.
|
2012-03-26 20:35:18 -05:00
|
|
|
mut llself: option<val_self_pair>,
|
2012-02-15 06:57:29 -06:00
|
|
|
// The a value alloca'd for calls to upcalls.rust_personality. Used when
|
|
|
|
// outputting the resume instruction.
|
2012-03-26 20:35:18 -05:00
|
|
|
mut personality: option<ValueRef>,
|
2012-03-27 05:33:13 -05:00
|
|
|
// If this is a for-loop body that returns, this holds the pointers needed
|
|
|
|
// for that
|
|
|
|
mut loop_ret: option<{flagptr: ValueRef, retptr: ValueRef}>,
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
// Maps arguments to allocas created for them in llallocas.
|
2012-02-03 06:37:55 -06:00
|
|
|
llargs: hashmap<ast::node_id, local_val>,
|
2011-07-27 07:19:39 -05:00
|
|
|
// Maps the def_ids for local variables to the allocas created for
|
|
|
|
// them in llallocas.
|
2012-02-03 06:37:55 -06:00
|
|
|
lllocals: hashmap<ast::node_id, local_val>,
|
|
|
|
// Same as above, but for closure upvars
|
|
|
|
llupvars: hashmap<ast::node_id, ValueRef>,
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-08-03 17:39:43 -05:00
|
|
|
// The node_id of the function, or -1 if it doesn't correspond to
|
|
|
|
// a user-defined function.
|
2012-02-03 06:37:55 -06:00
|
|
|
id: ast::node_id,
|
|
|
|
|
|
|
|
// If this function is being monomorphized, this contains the type
|
|
|
|
// substitutions used.
|
2012-02-09 04:17:11 -06:00
|
|
|
param_substs: option<param_substs>,
|
2012-02-03 06:37:55 -06:00
|
|
|
|
|
|
|
// The source span and nesting context where this function comes from, for
|
|
|
|
// error reporting and symbol generation.
|
|
|
|
span: option<span>,
|
|
|
|
path: path,
|
2011-08-02 17:13:08 -05:00
|
|
|
|
2012-02-03 06:37:55 -06:00
|
|
|
// This function's enclosing crate context.
|
2012-03-14 19:31:16 -05:00
|
|
|
ccx: @crate_ctxt
|
2012-02-03 06:37:55 -06:00
|
|
|
};
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn warn_not_to_commit(ccx: @crate_ctxt, msg: ~str) {
|
2012-02-01 20:52:08 -06:00
|
|
|
if !ccx.do_not_commit_warning_issued {
|
|
|
|
ccx.do_not_commit_warning_issued = true;
|
2012-07-14 00:57:48 -05:00
|
|
|
ccx.sess.warn(msg + ~" -- do not commit like this!");
|
2012-02-01 20:52:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-26 15:50:43 -05:00
|
|
|
// Heap selectors. Indicate which heap something should go on.
|
|
|
|
enum heap {
|
|
|
|
heap_shared,
|
|
|
|
heap_exchange,
|
|
|
|
}
|
|
|
|
|
2012-03-23 19:52:20 -05:00
|
|
|
enum cleantype {
|
|
|
|
normal_exit_only,
|
|
|
|
normal_exit_and_unwind
|
|
|
|
}
|
|
|
|
|
2012-01-19 16:24:03 -06:00
|
|
|
enum cleanup {
|
2012-03-23 19:52:20 -05:00
|
|
|
clean(fn@(block) -> block, cleantype),
|
|
|
|
clean_temp(ValueRef, fn@(block) -> block, cleantype),
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
|
2012-02-15 06:57:29 -06:00
|
|
|
// Used to remember and reuse existing cleanup paths
|
|
|
|
// target: none means the path ends in an resume instruction
|
|
|
|
type cleanup_path = {target: option<BasicBlockRef>,
|
|
|
|
dest: BasicBlockRef};
|
|
|
|
|
2012-02-17 04:18:14 -06:00
|
|
|
fn scope_clean_changed(info: scope_info) {
|
2012-06-29 18:26:56 -05:00
|
|
|
if info.cleanup_paths.len() > 0u { info.cleanup_paths = ~[]; }
|
2012-02-17 04:18:14 -06:00
|
|
|
info.landing_pad = none;
|
2012-02-15 06:57:29 -06:00
|
|
|
}
|
|
|
|
|
2012-03-23 19:52:20 -05:00
|
|
|
fn cleanup_type(cx: ty::ctxt, ty: ty::t) -> cleantype {
|
|
|
|
if ty::type_needs_unwind_cleanup(cx, ty) {
|
|
|
|
normal_exit_and_unwind
|
|
|
|
} else {
|
|
|
|
normal_exit_only
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn add_clean(cx: block, val: ValueRef, ty: ty::t) {
|
2012-02-21 07:20:18 -06:00
|
|
|
if !ty::type_needs_drop(cx.tcx(), ty) { ret; }
|
2012-05-14 22:32:29 -05:00
|
|
|
#debug["add_clean(%s, %s, %s)",
|
|
|
|
cx.to_str(), val_str(cx.ccx().tn, val),
|
|
|
|
ty_to_str(cx.ccx().tcx, ty)];
|
2012-03-23 19:52:20 -05:00
|
|
|
let cleanup_type = cleanup_type(cx.tcx(), ty);
|
2012-06-30 18:19:07 -05:00
|
|
|
do in_scope_cx(cx) |info| {
|
|
|
|
vec::push(info.cleanups, clean(|a| base::drop_ty(a, val, ty),
|
2012-06-26 02:39:18 -05:00
|
|
|
cleanup_type));
|
2012-02-17 04:18:14 -06:00
|
|
|
scope_clean_changed(info);
|
|
|
|
}
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
2012-02-17 06:17:40 -06:00
|
|
|
fn add_clean_temp(cx: block, val: ValueRef, ty: ty::t) {
|
2012-02-21 07:20:18 -06:00
|
|
|
if !ty::type_needs_drop(cx.tcx(), ty) { ret; }
|
2012-05-14 22:32:29 -05:00
|
|
|
#debug["add_clean_temp(%s, %s, %s)",
|
|
|
|
cx.to_str(), val_str(cx.ccx().tn, val),
|
|
|
|
ty_to_str(cx.ccx().tcx, ty)];
|
2012-03-23 19:52:20 -05:00
|
|
|
let cleanup_type = cleanup_type(cx.tcx(), ty);
|
2012-02-17 06:17:40 -06:00
|
|
|
fn do_drop(bcx: block, val: ValueRef, ty: ty::t) ->
|
|
|
|
block {
|
2012-02-03 08:15:28 -06:00
|
|
|
if ty::type_is_immediate(ty) {
|
2012-01-27 06:17:06 -06:00
|
|
|
ret base::drop_ty_immediate(bcx, val, ty);
|
2011-10-10 03:58:53 -05:00
|
|
|
} else {
|
2012-02-21 08:22:55 -06:00
|
|
|
ret base::drop_ty(bcx, val, ty);
|
2011-10-10 03:58:53 -05:00
|
|
|
}
|
2011-08-26 04:20:10 -05:00
|
|
|
}
|
2012-06-30 18:19:07 -05:00
|
|
|
do in_scope_cx(cx) |info| {
|
|
|
|
vec::push(info.cleanups, clean_temp(val, |a| do_drop(a, val, ty),
|
2012-06-26 02:39:18 -05:00
|
|
|
cleanup_type));
|
2012-02-17 04:18:14 -06:00
|
|
|
scope_clean_changed(info);
|
|
|
|
}
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
2012-02-17 06:17:40 -06:00
|
|
|
fn add_clean_temp_mem(cx: block, val: ValueRef, ty: ty::t) {
|
2012-02-21 07:20:18 -06:00
|
|
|
if !ty::type_needs_drop(cx.tcx(), ty) { ret; }
|
2012-05-14 22:32:29 -05:00
|
|
|
#debug["add_clean_temp_mem(%s, %s, %s)",
|
|
|
|
cx.to_str(), val_str(cx.ccx().tn, val),
|
|
|
|
ty_to_str(cx.ccx().tcx, ty)];
|
2012-03-23 19:52:20 -05:00
|
|
|
let cleanup_type = cleanup_type(cx.tcx(), ty);
|
2012-06-30 18:19:07 -05:00
|
|
|
do in_scope_cx(cx) |info| {
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(info.cleanups,
|
2012-06-30 18:19:07 -05:00
|
|
|
clean_temp(val, |a| base::drop_ty(a, val, ty),
|
2012-06-26 02:39:18 -05:00
|
|
|
cleanup_type));
|
2012-02-17 04:18:14 -06:00
|
|
|
scope_clean_changed(info);
|
|
|
|
}
|
2011-09-26 15:13:08 -05:00
|
|
|
}
|
2012-06-26 15:50:43 -05:00
|
|
|
fn add_clean_free(cx: block, ptr: ValueRef, heap: heap) {
|
|
|
|
let free_fn = alt heap {
|
2012-06-30 18:19:07 -05:00
|
|
|
heap_shared { |a| base::trans_free(a, ptr) }
|
|
|
|
heap_exchange { |a| base::trans_unique_free(a, ptr) }
|
2012-06-26 15:50:43 -05:00
|
|
|
};
|
2012-06-30 18:19:07 -05:00
|
|
|
do in_scope_cx(cx) |info| {
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(info.cleanups, clean_temp(ptr, free_fn,
|
|
|
|
normal_exit_and_unwind));
|
2012-02-17 04:18:14 -06:00
|
|
|
scope_clean_changed(info);
|
|
|
|
}
|
2011-09-27 06:19:55 -05:00
|
|
|
}
|
2011-07-21 19:27:34 -05:00
|
|
|
|
|
|
|
// Note that this only works for temporaries. We should, at some point, move
|
|
|
|
// to a system where we can also cancel the cleanup on local variables, but
|
|
|
|
// this will be more involved. For now, we simply zero out the local, and the
|
|
|
|
// drop glue checks whether it is zero.
|
2012-02-17 06:17:40 -06:00
|
|
|
fn revoke_clean(cx: block, val: ValueRef) {
|
2012-06-30 18:19:07 -05:00
|
|
|
do in_scope_cx(cx) |info| {
|
|
|
|
do option::iter(vec::position(info.cleanups, |cu| {
|
2012-04-06 13:01:43 -05:00
|
|
|
alt cu { clean_temp(v, _, _) if v == val { true } _ { false } }
|
2012-06-30 18:19:07 -05:00
|
|
|
})) |i| {
|
2012-04-06 13:01:43 -05:00
|
|
|
info.cleanups =
|
2012-06-28 15:52:13 -05:00
|
|
|
vec::append(vec::slice(info.cleanups, 0u, i),
|
2012-07-12 12:47:37 -05:00
|
|
|
// FIXME (#2880): use view here.
|
|
|
|
vec::slice(info.cleanups,
|
2012-06-28 15:52:13 -05:00
|
|
|
i + 1u,
|
|
|
|
info.cleanups.len()));
|
2012-04-06 13:01:43 -05:00
|
|
|
scope_clean_changed(info);
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-19 16:24:03 -06:00
|
|
|
enum block_kind {
|
2012-01-28 13:49:21 -06:00
|
|
|
// A scope at the end of which temporary values created inside of it are
|
|
|
|
// cleaned up. May correspond to an actual block in the language, but also
|
|
|
|
// to an implicit scope, for example, calls introduce an implicit scope in
|
|
|
|
// which the arguments are evaluated and cleaned up.
|
2012-02-17 06:17:40 -06:00
|
|
|
block_scope(scope_info),
|
2011-07-21 19:27:34 -05:00
|
|
|
// A non-scope block is a basic block created as a translation artifact
|
|
|
|
// from translating code that expresses conditional logic rather than by
|
|
|
|
// explicit { ... } block structure in the source language. It's called a
|
|
|
|
// non-scope block because it doesn't introduce a new variable scope.
|
2012-02-17 06:17:40 -06:00
|
|
|
block_non_scope,
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
|
2012-02-17 04:18:14 -06:00
|
|
|
type scope_info = {
|
2012-05-14 10:40:51 -05:00
|
|
|
loop_break: option<block>,
|
2012-02-17 04:18:14 -06:00
|
|
|
// A list of functions that must be run at when leaving this
|
|
|
|
// block, cleaning up any variables that were introduced in the
|
|
|
|
// block.
|
2012-06-29 18:26:56 -05:00
|
|
|
mut cleanups: ~[cleanup],
|
2012-02-17 04:18:14 -06:00
|
|
|
// Existing cleanup paths that may be reused, indexed by destination and
|
|
|
|
// cleared when the set of cleanups changes.
|
2012-06-29 18:26:56 -05:00
|
|
|
mut cleanup_paths: ~[cleanup_path],
|
2012-02-17 04:18:14 -06:00
|
|
|
// Unwinding landing pad. Also cleared when cleanups change.
|
2012-03-26 20:35:18 -05:00
|
|
|
mut landing_pad: option<BasicBlockRef>,
|
2012-02-17 04:18:14 -06:00
|
|
|
};
|
|
|
|
|
2012-05-14 16:24:16 -05:00
|
|
|
impl node_info for @ast::expr {
|
|
|
|
fn info() -> option<node_info> {
|
|
|
|
some({id: self.id, span: self.span})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl node_info for ast::blk {
|
|
|
|
fn info() -> option<node_info> {
|
|
|
|
some({id: self.node.id, span: self.span})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl node_info for option<@ast::expr> {
|
|
|
|
fn info() -> option<node_info> {
|
2012-06-30 18:19:07 -05:00
|
|
|
self.chain(|s| s.info())
|
2012-05-14 16:24:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type node_info = {
|
|
|
|
id: ast::node_id,
|
|
|
|
span: span
|
|
|
|
};
|
|
|
|
|
2011-07-21 19:27:34 -05:00
|
|
|
// Basic block context. We create a block context for each basic block
|
|
|
|
// (single-entry, single-exit sequence of instructions) we generate from Rust
|
|
|
|
// code. Each basic block we generate is attached to a function, typically
|
|
|
|
// with many basic blocks per function. All the basic blocks attached to a
|
|
|
|
// function are organized as a directed graph.
|
2012-06-12 16:55:44 -05:00
|
|
|
class block_ {
|
2011-07-27 07:19:39 -05:00
|
|
|
// The BasicBlockRef returned from a call to
|
2011-08-03 17:39:43 -05:00
|
|
|
// llvm::LLVMAppendBasicBlock(llfn, name), which adds a basic
|
|
|
|
// block to the function pointed to by llfn. We insert
|
|
|
|
// instructions into that block by way of this block context.
|
2011-09-02 17:34:58 -05:00
|
|
|
// The block pointing to this one in the function's digraph.
|
2012-06-12 16:55:44 -05:00
|
|
|
let llbb: BasicBlockRef;
|
|
|
|
let mut terminated: bool;
|
|
|
|
let mut unreachable: bool;
|
|
|
|
let parent: option<block>;
|
2011-09-02 17:34:58 -05:00
|
|
|
// The 'kind' of basic block this is.
|
2012-06-12 16:55:44 -05:00
|
|
|
let kind: block_kind;
|
2012-05-14 16:24:16 -05:00
|
|
|
// info about the AST node this block originated from, if any
|
2012-06-12 16:55:44 -05:00
|
|
|
let node_info: option<node_info>;
|
2011-09-02 17:34:58 -05:00
|
|
|
// The function context for the function to which this block is
|
|
|
|
// attached.
|
2012-06-12 16:55:44 -05:00
|
|
|
let fcx: fn_ctxt;
|
|
|
|
new(llbb: BasicBlockRef, parent: option<block>, -kind: block_kind,
|
|
|
|
node_info: option<node_info>, fcx: fn_ctxt) {
|
|
|
|
// sigh
|
|
|
|
self.llbb = llbb; self.terminated = false; self.unreachable = false;
|
|
|
|
self.parent = parent; self.kind = kind; self.node_info = node_info;
|
|
|
|
self.fcx = fcx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This must be enum and not type, or trans goes into an infinite loop (#2572)
|
|
|
|
*/
|
|
|
|
enum block = @block_;
|
|
|
|
|
|
|
|
fn mk_block(llbb: BasicBlockRef, parent: option<block>, -kind: block_kind,
|
|
|
|
node_info: option<node_info>, fcx: fn_ctxt) -> block {
|
|
|
|
block(@block_(llbb, parent, kind, node_info, fcx))
|
|
|
|
}
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2012-02-21 07:20:18 -06:00
|
|
|
// First two args are retptr, env
|
2012-03-09 03:47:40 -06:00
|
|
|
const first_real_arg: uint = 2u;
|
2012-02-21 07:20:18 -06:00
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
type result = {bcx: block, val: ValueRef};
|
|
|
|
type result_t = {bcx: block, val: ValueRef, ty: ty::t};
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn rslt(bcx: block, val: ValueRef) -> result {
|
2011-09-15 22:22:03 -05:00
|
|
|
{bcx: bcx, val: val}
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn ty_str(tn: type_names, t: TypeRef) -> ~str {
|
2011-08-26 18:54:25 -05:00
|
|
|
ret lib::llvm::type_to_str(tn, t);
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
|
2012-05-22 07:20:47 -05:00
|
|
|
fn val_ty(v: ValueRef) -> TypeRef { ret llvm::LLVMTypeOf(v); }
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn val_str(tn: type_names, v: ValueRef) -> ~str { ret ty_str(tn, val_ty(v)); }
|
2011-07-21 19:27:34 -05:00
|
|
|
|
|
|
|
// Returns the nth element of the given LLVM structure type.
|
2011-10-12 15:31:41 -05:00
|
|
|
fn struct_elt(llstructty: TypeRef, n: uint) -> TypeRef unsafe {
|
2012-01-16 04:21:01 -06:00
|
|
|
let elt_count = llvm::LLVMCountStructElementTypes(llstructty) as uint;
|
2011-10-12 15:31:41 -05:00
|
|
|
assert (n < elt_count);
|
2012-03-12 17:52:30 -05:00
|
|
|
let elt_tys = vec::from_elem(elt_count, T_nil());
|
2011-10-12 15:31:41 -05:00
|
|
|
llvm::LLVMGetStructElementTypes(llstructty, to_ptr(elt_tys));
|
|
|
|
ret llvm::LLVMGetElementType(elt_tys[n]);
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn in_scope_cx(cx: block, f: fn(scope_info)) {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut cur = cx;
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2012-02-17 04:18:14 -06:00
|
|
|
alt cur.kind {
|
2012-04-19 18:19:53 -05:00
|
|
|
block_scope(inf) { f(inf); ret; }
|
2012-02-17 04:18:14 -06:00
|
|
|
_ {}
|
|
|
|
}
|
2012-02-17 06:17:40 -06:00
|
|
|
cur = block_parent(cur);
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn block_parent(cx: block) -> block {
|
2012-06-12 16:55:44 -05:00
|
|
|
alt cx.parent {
|
|
|
|
some(b) { b }
|
|
|
|
none { cx.sess().bug(#fmt("block_parent called on root block %?",
|
|
|
|
cx)); }
|
|
|
|
}
|
2012-02-17 06:17:40 -06:00
|
|
|
}
|
|
|
|
|
2011-07-21 19:27:34 -05:00
|
|
|
// Accessors
|
|
|
|
|
2012-05-14 22:32:29 -05:00
|
|
|
impl bcx_cxs for block {
|
2012-06-08 08:03:06 -05:00
|
|
|
pure fn ccx() -> @crate_ctxt { self.fcx.ccx }
|
|
|
|
pure fn tcx() -> ty::ctxt { self.fcx.ccx.tcx }
|
|
|
|
pure fn sess() -> session { self.fcx.ccx.sess }
|
2012-05-14 22:32:29 -05:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn val_str(val: ValueRef) -> ~str {
|
2012-05-22 20:44:28 -05:00
|
|
|
val_str(self.ccx().tn, val)
|
|
|
|
}
|
2012-07-14 00:57:48 -05:00
|
|
|
fn ty_to_str(t: ty::t) -> ~str {
|
2012-05-22 20:44:28 -05:00
|
|
|
ty_to_str(self.tcx(), t)
|
|
|
|
}
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str {
|
2012-05-14 22:32:29 -05:00
|
|
|
alt self.node_info {
|
|
|
|
some(node_info) {
|
|
|
|
#fmt["[block %d]", node_info.id]
|
|
|
|
}
|
|
|
|
none {
|
|
|
|
#fmt["[block %x]", ptr::addr_of(*self) as uint]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-21 07:20:18 -06:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
|
|
|
// LLVM type constructors.
|
|
|
|
fn T_void() -> TypeRef {
|
|
|
|
// Note: For the time being llvm is kinda busted here, it has the notion
|
|
|
|
// of a 'void' type that can only occur as part of the signature of a
|
|
|
|
// function, but no general unit type of 0-sized value. This is, afaict,
|
|
|
|
// vestigial from its C heritage, and we'll be attempting to submit a
|
|
|
|
// patch upstream to fix it. In the mean time we only model function
|
|
|
|
// outputs (Rust functions and C functions) using T_void, and model the
|
|
|
|
// Rust general purpose nil type you can construct as 1-bit (always
|
|
|
|
// zero). This makes the result incorrect for now -- things like a tuple
|
|
|
|
// of 10 nil values will have 10-bit size -- but it doesn't seem like we
|
|
|
|
// have any other options until it's fixed upstream.
|
|
|
|
|
|
|
|
ret llvm::LLVMVoidType();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn T_nil() -> TypeRef {
|
|
|
|
// NB: See above in T_void().
|
|
|
|
|
|
|
|
ret llvm::LLVMInt1Type();
|
|
|
|
}
|
|
|
|
|
2011-11-15 20:11:22 -06:00
|
|
|
fn T_metadata() -> TypeRef { ret llvm::LLVMMetadataType(); }
|
|
|
|
|
2011-07-14 19:08:22 -05:00
|
|
|
fn T_i1() -> TypeRef { ret llvm::LLVMInt1Type(); }
|
|
|
|
|
|
|
|
fn T_i8() -> TypeRef { ret llvm::LLVMInt8Type(); }
|
|
|
|
|
|
|
|
fn T_i16() -> TypeRef { ret llvm::LLVMInt16Type(); }
|
|
|
|
|
|
|
|
fn T_i32() -> TypeRef { ret llvm::LLVMInt32Type(); }
|
|
|
|
|
|
|
|
fn T_i64() -> TypeRef { ret llvm::LLVMInt64Type(); }
|
|
|
|
|
|
|
|
fn T_f32() -> TypeRef { ret llvm::LLVMFloatType(); }
|
|
|
|
|
|
|
|
fn T_f64() -> TypeRef { ret llvm::LLVMDoubleType(); }
|
|
|
|
|
|
|
|
fn T_bool() -> TypeRef { ret T_i1(); }
|
|
|
|
|
2011-10-14 22:38:24 -05:00
|
|
|
fn T_int(targ_cfg: @session::config) -> TypeRef {
|
|
|
|
ret alt targ_cfg.arch {
|
2012-01-19 03:03:57 -06:00
|
|
|
session::arch_x86 { T_i32() }
|
|
|
|
session::arch_x86_64 { T_i64() }
|
|
|
|
session::arch_arm { T_i32() }
|
2011-10-14 18:45:25 -05:00
|
|
|
};
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_int_ty(cx: @crate_ctxt, t: ast::int_ty) -> TypeRef {
|
2011-12-07 14:06:12 -06:00
|
|
|
alt t {
|
2012-01-19 00:37:22 -06:00
|
|
|
ast::ty_i { cx.int_type }
|
|
|
|
ast::ty_char { T_char() }
|
|
|
|
ast::ty_i8 { T_i8() }
|
|
|
|
ast::ty_i16 { T_i16() }
|
|
|
|
ast::ty_i32 { T_i32() }
|
|
|
|
ast::ty_i64 { T_i64() }
|
2011-12-07 14:06:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_uint_ty(cx: @crate_ctxt, t: ast::uint_ty) -> TypeRef {
|
2011-12-07 14:06:12 -06:00
|
|
|
alt t {
|
2012-01-19 00:37:22 -06:00
|
|
|
ast::ty_u { cx.int_type }
|
|
|
|
ast::ty_u8 { T_i8() }
|
|
|
|
ast::ty_u16 { T_i16() }
|
|
|
|
ast::ty_u32 { T_i32() }
|
|
|
|
ast::ty_u64 { T_i64() }
|
2011-12-07 14:06:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_float_ty(cx: @crate_ctxt, t: ast::float_ty) -> TypeRef {
|
2011-12-07 14:06:12 -06:00
|
|
|
alt t {
|
2012-01-19 00:37:22 -06:00
|
|
|
ast::ty_f { cx.float_type }
|
|
|
|
ast::ty_f32 { T_f32() }
|
|
|
|
ast::ty_f64 { T_f64() }
|
2011-12-07 14:06:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 22:38:24 -05:00
|
|
|
fn T_float(targ_cfg: @session::config) -> TypeRef {
|
|
|
|
ret alt targ_cfg.arch {
|
2012-01-19 03:03:57 -06:00
|
|
|
session::arch_x86 { T_f64() }
|
|
|
|
session::arch_x86_64 { T_f64() }
|
|
|
|
session::arch_arm { T_f64() }
|
2011-10-14 18:45:25 -05:00
|
|
|
};
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn T_char() -> TypeRef { ret T_i32(); }
|
|
|
|
|
2011-10-14 22:38:24 -05:00
|
|
|
fn T_size_t(targ_cfg: @session::config) -> TypeRef {
|
|
|
|
ret T_int(targ_cfg);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn T_fn(inputs: ~[TypeRef], output: TypeRef) -> TypeRef unsafe {
|
2012-01-02 09:50:51 -06:00
|
|
|
ret llvm::LLVMFunctionType(output, to_ptr(inputs),
|
2012-03-12 22:04:27 -05:00
|
|
|
inputs.len() as c_uint,
|
2012-01-16 04:21:01 -06:00
|
|
|
False);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_fn_pair(cx: @crate_ctxt, tfn: TypeRef) -> TypeRef {
|
2012-06-29 18:26:56 -05:00
|
|
|
ret T_struct(~[T_ptr(tfn), T_opaque_cbox_ptr(cx)]);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-01-16 04:21:01 -06:00
|
|
|
fn T_ptr(t: TypeRef) -> TypeRef {
|
2012-03-12 22:04:27 -05:00
|
|
|
ret llvm::LLVMPointerType(t, 0u as c_uint);
|
2012-01-16 04:21:01 -06:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn T_struct(elts: ~[TypeRef]) -> TypeRef unsafe {
|
2012-03-12 22:04:27 -05:00
|
|
|
ret llvm::LLVMStructType(to_ptr(elts), elts.len() as c_uint, False);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn T_named_struct(name: ~str) -> TypeRef {
|
2011-07-27 07:19:39 -05:00
|
|
|
let c = llvm::LLVMGetGlobalContext();
|
2012-06-30 18:19:07 -05:00
|
|
|
ret str::as_c_str(name, |buf| llvm::LLVMStructCreateNamed(c, buf));
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn set_struct_body(t: TypeRef, elts: ~[TypeRef]) unsafe {
|
2012-01-16 04:21:01 -06:00
|
|
|
llvm::LLVMStructSetBody(t, to_ptr(elts),
|
2012-03-12 22:04:27 -05:00
|
|
|
elts.len() as c_uint, False);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn T_empty_struct() -> TypeRef { ret T_struct(~[]); }
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2012-03-08 09:17:59 -06:00
|
|
|
// A vtable is, in reality, a vtable pointer followed by zero or more pointers
|
2012-03-08 16:37:45 -06:00
|
|
|
// to tydescs and other vtables that it closes over. But the types and number
|
|
|
|
// of those are rarely known to the code that needs to manipulate them, so
|
|
|
|
// they are described by this opaque type.
|
2012-03-08 09:17:59 -06:00
|
|
|
fn T_vtable() -> TypeRef { T_array(T_ptr(T_i8()), 1u) }
|
2012-01-02 09:50:51 -06:00
|
|
|
|
2011-10-14 22:38:24 -05:00
|
|
|
fn T_task(targ_cfg: @session::config) -> TypeRef {
|
2012-07-14 00:57:48 -05:00
|
|
|
let t = T_named_struct(~"task");
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
// Refcount
|
|
|
|
// Delegate pointer
|
|
|
|
// Stack segment pointer
|
|
|
|
// Runtime SP
|
|
|
|
// Rust SP
|
|
|
|
// GC chain
|
2011-08-19 17:16:48 -05:00
|
|
|
|
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
// Domain pointer
|
|
|
|
// Crate cache pointer
|
2011-08-19 17:16:48 -05:00
|
|
|
|
2011-10-14 22:38:24 -05:00
|
|
|
let t_int = T_int(targ_cfg);
|
2011-08-19 17:16:48 -05:00
|
|
|
let elems =
|
2012-06-29 18:26:56 -05:00
|
|
|
~[t_int, t_int, t_int, t_int,
|
|
|
|
t_int, t_int, t_int, t_int];
|
2011-07-14 19:08:22 -05:00
|
|
|
set_struct_body(t, elems);
|
|
|
|
ret t;
|
|
|
|
}
|
|
|
|
|
2012-05-04 00:31:38 -05:00
|
|
|
fn T_tydesc_field(cx: @crate_ctxt, field: uint) -> TypeRef unsafe {
|
2011-07-14 19:08:22 -05:00
|
|
|
// Bit of a kludge: pick the fn typeref out of the tydesc..
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
let tydesc_elts: ~[TypeRef] =
|
2012-05-04 00:31:38 -05:00
|
|
|
vec::from_elem::<TypeRef>(abi::n_tydesc_fields,
|
2012-01-18 15:02:29 -06:00
|
|
|
T_nil());
|
2011-10-12 15:31:41 -05:00
|
|
|
llvm::LLVMGetStructElementTypes(cx.tydesc_type,
|
|
|
|
to_ptr::<TypeRef>(tydesc_elts));
|
|
|
|
let t = llvm::LLVMGetElementType(tydesc_elts[field]);
|
|
|
|
ret t;
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_glue_fn(cx: @crate_ctxt) -> TypeRef {
|
2012-07-14 00:57:48 -05:00
|
|
|
let s = ~"glue_fn";
|
2012-01-13 02:32:05 -06:00
|
|
|
alt name_has_type(cx.tn, s) { some(t) { ret t; } _ {} }
|
2011-07-27 07:19:39 -05:00
|
|
|
let t = T_tydesc_field(cx, abi::tydesc_field_drop_glue);
|
2012-01-13 02:32:05 -06:00
|
|
|
associate_type(cx.tn, s, t);
|
2011-07-14 19:08:22 -05:00
|
|
|
ret t;
|
|
|
|
}
|
|
|
|
|
2011-10-14 22:38:24 -05:00
|
|
|
fn T_tydesc(targ_cfg: @session::config) -> TypeRef {
|
2012-07-14 00:57:48 -05:00
|
|
|
let tydesc = T_named_struct(~"tydesc");
|
2011-07-27 07:19:39 -05:00
|
|
|
let tydescpp = T_ptr(T_ptr(tydesc));
|
|
|
|
let pvoid = T_ptr(T_i8());
|
|
|
|
let glue_fn_ty =
|
2012-06-29 18:26:56 -05:00
|
|
|
T_ptr(T_fn(~[T_ptr(T_nil()), T_ptr(T_nil()), tydescpp,
|
|
|
|
pvoid], T_void()));
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2011-10-14 22:38:24 -05:00
|
|
|
let int_type = T_int(targ_cfg);
|
2011-08-04 13:25:09 -05:00
|
|
|
let elems =
|
2012-07-09 16:56:11 -05:00
|
|
|
~[int_type, int_type,
|
|
|
|
glue_fn_ty, glue_fn_ty, glue_fn_ty, glue_fn_ty,
|
|
|
|
T_ptr(T_i8()), T_ptr(T_i8())];
|
2011-07-14 19:08:22 -05:00
|
|
|
set_struct_body(tydesc, elems);
|
|
|
|
ret tydesc;
|
|
|
|
}
|
|
|
|
|
2012-01-16 04:21:01 -06:00
|
|
|
fn T_array(t: TypeRef, n: uint) -> TypeRef {
|
2012-03-12 22:04:27 -05:00
|
|
|
ret llvm::LLVMArrayType(t, n as c_uint);
|
2012-01-16 04:21:01 -06:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
|
|
|
// Interior vector.
|
2011-10-14 22:38:24 -05:00
|
|
|
fn T_vec2(targ_cfg: @session::config, t: TypeRef) -> TypeRef {
|
2012-06-29 18:26:56 -05:00
|
|
|
ret T_struct(~[T_int(targ_cfg), // fill
|
2011-10-14 22:38:24 -05:00
|
|
|
T_int(targ_cfg), // alloc
|
2012-06-29 18:26:56 -05:00
|
|
|
T_array(t, 0u)]); // elements
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_vec(ccx: @crate_ctxt, t: TypeRef) -> TypeRef {
|
2012-01-12 10:59:49 -06:00
|
|
|
ret T_vec2(ccx.sess.targ_cfg, t);
|
2011-10-14 22:38:24 -05:00
|
|
|
}
|
|
|
|
|
2011-07-14 19:08:22 -05:00
|
|
|
// Note that the size of this one is in bytes.
|
2011-10-14 22:38:24 -05:00
|
|
|
fn T_opaque_vec(targ_cfg: @session::config) -> TypeRef {
|
|
|
|
ret T_vec2(targ_cfg, T_i8());
|
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2012-02-01 20:52:08 -06:00
|
|
|
// Let T be the content of a box @T. tuplify_box_ty(t) returns the
|
|
|
|
// representation of @T as a tuple (i.e., the ty::t version of what T_box()
|
|
|
|
// returns).
|
|
|
|
fn tuplify_box_ty(tcx: ty::ctxt, t: ty::t) -> ty::t {
|
2012-02-15 13:25:39 -06:00
|
|
|
let ptr = ty::mk_ptr(tcx, {ty: ty::mk_nil(tcx), mutbl: ast::m_imm});
|
2012-06-29 18:26:56 -05:00
|
|
|
ret ty::mk_tup(tcx, ~[ty::mk_uint(tcx), ty::mk_type(tcx),
|
2012-02-01 20:52:08 -06:00
|
|
|
ptr, ptr,
|
2012-06-29 18:26:56 -05:00
|
|
|
t]);
|
2012-02-01 20:52:08 -06:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn T_box_header_fields(cx: @crate_ctxt) -> ~[TypeRef] {
|
2012-02-01 20:52:08 -06:00
|
|
|
let ptr = T_ptr(T_i8());
|
2012-06-29 18:26:56 -05:00
|
|
|
ret ~[cx.int_type, T_ptr(cx.tydesc_type), ptr, ptr];
|
2012-02-01 20:52:08 -06:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_box_header(cx: @crate_ctxt) -> TypeRef {
|
2012-02-01 20:52:08 -06:00
|
|
|
ret T_struct(T_box_header_fields(cx));
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_box(cx: @crate_ctxt, t: TypeRef) -> TypeRef {
|
2012-06-29 18:26:56 -05:00
|
|
|
ret T_struct(vec::append(T_box_header_fields(cx), ~[t]));
|
2012-02-01 20:52:08 -06:00
|
|
|
}
|
|
|
|
|
2012-05-07 16:27:43 -05:00
|
|
|
fn T_box_ptr(t: TypeRef) -> TypeRef {
|
|
|
|
const box_addrspace: uint = 1u;
|
|
|
|
ret llvm::LLVMPointerType(t, box_addrspace as c_uint);
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_opaque_box(cx: @crate_ctxt) -> TypeRef {
|
2012-02-01 20:52:08 -06:00
|
|
|
ret T_box(cx, T_i8());
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_opaque_box_ptr(cx: @crate_ctxt) -> TypeRef {
|
2012-05-07 16:27:43 -05:00
|
|
|
ret T_box_ptr(T_opaque_box(cx));
|
2011-10-14 18:45:25 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2012-05-09 16:11:46 -05:00
|
|
|
fn T_unique(cx: @crate_ctxt, t: TypeRef) -> TypeRef {
|
2012-06-29 18:26:56 -05:00
|
|
|
ret T_struct(vec::append(T_box_header_fields(cx), ~[t]));
|
2012-05-09 16:11:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn T_unique_ptr(t: TypeRef) -> TypeRef {
|
2012-06-05 00:56:40 -05:00
|
|
|
const unique_addrspace: uint = 1u;
|
2012-05-09 16:11:46 -05:00
|
|
|
ret llvm::LLVMPointerType(t, unique_addrspace as c_uint);
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_port(cx: @crate_ctxt, _t: TypeRef) -> TypeRef {
|
2012-06-29 18:26:56 -05:00
|
|
|
ret T_struct(~[cx.int_type]); // Refcount
|
2011-07-14 19:08:22 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_chan(cx: @crate_ctxt, _t: TypeRef) -> TypeRef {
|
2012-06-29 18:26:56 -05:00
|
|
|
ret T_struct(~[cx.int_type]); // Refcount
|
2011-07-14 19:08:22 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_taskptr(cx: @crate_ctxt) -> TypeRef { ret T_ptr(cx.task_type); }
|
2011-07-14 19:08:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
// This type must never be used directly; it must always be cast away.
|
2011-09-12 04:27:30 -05:00
|
|
|
fn T_typaram(tn: type_names) -> TypeRef {
|
2012-07-14 00:57:48 -05:00
|
|
|
let s = ~"typaram";
|
2012-01-13 02:32:05 -06:00
|
|
|
alt name_has_type(tn, s) { some(t) { ret t; } _ {} }
|
2011-07-27 07:19:39 -05:00
|
|
|
let t = T_i8();
|
2012-01-13 02:32:05 -06:00
|
|
|
associate_type(tn, s, t);
|
2011-07-14 19:08:22 -05:00
|
|
|
ret t;
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn T_typaram_ptr(tn: type_names) -> TypeRef { ret T_ptr(T_typaram(tn)); }
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_opaque_cbox_ptr(cx: @crate_ctxt) -> TypeRef {
|
2012-02-01 20:52:08 -06:00
|
|
|
// closures look like boxes (even when they are fn~ or fn&)
|
|
|
|
// see trans_closure.rs
|
|
|
|
ret T_opaque_box_ptr(cx);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-04-25 19:06:01 -05:00
|
|
|
fn T_enum_discrim(cx: @crate_ctxt) -> TypeRef {
|
2011-11-23 14:01:15 -06:00
|
|
|
ret cx.int_type;
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_opaque_enum(cx: @crate_ctxt) -> TypeRef {
|
2012-07-14 00:57:48 -05:00
|
|
|
let s = ~"opaque_enum";
|
2012-01-13 02:32:05 -06:00
|
|
|
alt name_has_type(cx.tn, s) { some(t) { ret t; } _ {} }
|
2012-06-29 18:26:56 -05:00
|
|
|
let t = T_struct(~[T_enum_discrim(cx), T_i8()]);
|
2012-01-13 02:32:05 -06:00
|
|
|
associate_type(cx.tn, s, t);
|
2011-07-14 19:08:22 -05:00
|
|
|
ret t;
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_opaque_enum_ptr(cx: @crate_ctxt) -> TypeRef {
|
2012-01-25 07:34:31 -06:00
|
|
|
ret T_ptr(T_opaque_enum(cx));
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn T_captured_tydescs(cx: @crate_ctxt, n: uint) -> TypeRef {
|
2012-03-12 17:52:30 -05:00
|
|
|
ret T_struct(vec::from_elem::<TypeRef>(n, T_ptr(cx.tydesc_type)));
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-07-03 18:30:42 -05:00
|
|
|
fn T_opaque_trait(cx: @crate_ctxt) -> TypeRef {
|
2012-06-29 18:26:56 -05:00
|
|
|
T_struct(~[T_ptr(cx.tydesc_type), T_opaque_box_ptr(cx)])
|
2012-01-07 15:44:14 -06:00
|
|
|
}
|
|
|
|
|
2011-07-14 19:08:22 -05:00
|
|
|
fn T_opaque_port_ptr() -> TypeRef { ret T_ptr(T_i8()); }
|
|
|
|
|
|
|
|
fn T_opaque_chan_ptr() -> TypeRef { ret T_ptr(T_i8()); }
|
|
|
|
|
|
|
|
|
|
|
|
// LLVM constant constructors.
|
2011-07-27 07:19:39 -05:00
|
|
|
fn C_null(t: TypeRef) -> ValueRef { ret llvm::LLVMConstNull(t); }
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2011-11-13 23:34:58 -06:00
|
|
|
fn C_integral(t: TypeRef, u: u64, sign_extend: Bool) -> ValueRef {
|
2012-05-17 18:17:11 -05:00
|
|
|
ret llvm::LLVMConstInt(t, u, sign_extend);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn C_floating(s: ~str, t: TypeRef) -> ValueRef {
|
2012-06-30 18:19:07 -05:00
|
|
|
ret str::as_c_str(s, |buf| llvm::LLVMConstRealOfString(t, buf));
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn C_nil() -> ValueRef {
|
|
|
|
// NB: See comment above in T_void().
|
|
|
|
|
2011-11-13 23:34:58 -06:00
|
|
|
ret C_integral(T_i1(), 0u64, False);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn C_bool(b: bool) -> ValueRef {
|
2012-03-26 09:09:27 -05:00
|
|
|
C_integral(T_bool(), if b { 1u64 } else { 0u64 }, False)
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2011-10-26 00:23:28 -05:00
|
|
|
fn C_i32(i: i32) -> ValueRef {
|
2011-11-13 23:34:58 -06:00
|
|
|
ret C_integral(T_i32(), i as u64, True);
|
2011-10-26 00:23:28 -05:00
|
|
|
}
|
|
|
|
|
2011-11-15 20:11:22 -06:00
|
|
|
fn C_i64(i: i64) -> ValueRef {
|
2011-11-18 02:47:59 -06:00
|
|
|
ret C_integral(T_i64(), i as u64, True);
|
2011-11-15 20:11:22 -06:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn C_int(cx: @crate_ctxt, i: int) -> ValueRef {
|
2011-11-13 23:34:58 -06:00
|
|
|
ret C_integral(cx.int_type, i as u64, True);
|
2011-10-14 18:45:25 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn C_uint(cx: @crate_ctxt, i: uint) -> ValueRef {
|
2011-11-13 23:34:58 -06:00
|
|
|
ret C_integral(cx.int_type, i as u64, False);
|
2011-10-14 18:45:25 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2011-11-13 23:34:58 -06:00
|
|
|
fn C_u8(i: uint) -> ValueRef { ret C_integral(T_i8(), i as u64, False); }
|
2011-07-14 19:08:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
// This is a 'c-like' raw string, which differs from
|
|
|
|
// our boxed-and-length-annotated strings.
|
2012-07-14 00:57:48 -05:00
|
|
|
fn C_cstr(cx: @crate_ctxt, s: ~str) -> ValueRef {
|
2012-04-21 15:23:25 -05:00
|
|
|
alt cx.const_cstr_cache.find(s) {
|
|
|
|
some(llval) { ret llval; }
|
|
|
|
none { }
|
|
|
|
}
|
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
let sc = do str::as_c_str(s) |buf| {
|
2012-03-12 22:04:27 -05:00
|
|
|
llvm::LLVMConstString(buf, str::len(s) as c_uint, False)
|
2012-01-16 04:21:01 -06:00
|
|
|
};
|
2011-09-02 17:34:58 -05:00
|
|
|
let g =
|
2012-07-14 00:57:48 -05:00
|
|
|
str::as_c_str(cx.names(~"str"),
|
2012-06-30 18:19:07 -05:00
|
|
|
|buf| llvm::LLVMAddGlobal(cx.llmod, val_ty(sc), buf));
|
2011-07-14 19:08:22 -05:00
|
|
|
llvm::LLVMSetInitializer(g, sc);
|
|
|
|
llvm::LLVMSetGlobalConstant(g, True);
|
2012-02-01 04:04:56 -06:00
|
|
|
lib::llvm::SetLinkage(g, lib::llvm::InternalLinkage);
|
2012-04-21 15:23:25 -05:00
|
|
|
|
|
|
|
cx.const_cstr_cache.insert(s, g);
|
|
|
|
|
2011-07-14 19:08:22 -05:00
|
|
|
ret g;
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn C_estr_slice(cx: @crate_ctxt, s: ~str) -> ValueRef {
|
2012-06-08 15:26:06 -05:00
|
|
|
let cs = llvm::LLVMConstPointerCast(C_cstr(cx, s), T_ptr(T_i8()));
|
2012-06-29 18:26:56 -05:00
|
|
|
C_struct(~[cs, C_uint(cx, str::len(s) + 1u /* +1 for null */)])
|
2012-06-08 15:26:06 -05:00
|
|
|
}
|
|
|
|
|
2011-07-14 19:08:22 -05:00
|
|
|
// Returns a Plain Old LLVM String:
|
2012-07-14 00:57:48 -05:00
|
|
|
fn C_postr(s: ~str) -> ValueRef {
|
2012-06-30 18:19:07 -05:00
|
|
|
ret do str::as_c_str(s) |buf| {
|
2012-03-12 22:04:27 -05:00
|
|
|
llvm::LLVMConstString(buf, str::len(s) as c_uint, False)
|
2012-01-16 04:21:01 -06:00
|
|
|
};
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2011-10-12 15:31:41 -05:00
|
|
|
fn C_zero_byte_arr(size: uint) -> ValueRef unsafe {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut i = 0u;
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut elts: ~[ValueRef] = ~[];
|
2012-06-26 02:39:18 -05:00
|
|
|
while i < size { vec::push(elts, C_u8(0u)); i += 1u; }
|
2012-02-28 22:43:39 -06:00
|
|
|
ret llvm::LLVMConstArray(T_i8(), vec::unsafe::to_ptr(elts),
|
2012-03-12 22:04:27 -05:00
|
|
|
elts.len() as c_uint);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn C_struct(elts: ~[ValueRef]) -> ValueRef unsafe {
|
2012-02-28 22:43:39 -06:00
|
|
|
ret llvm::LLVMConstStruct(vec::unsafe::to_ptr(elts),
|
2012-03-12 22:04:27 -05:00
|
|
|
elts.len() as c_uint, False);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn C_named_struct(T: TypeRef, elts: ~[ValueRef]) -> ValueRef unsafe {
|
2012-02-28 22:43:39 -06:00
|
|
|
ret llvm::LLVMConstNamedStruct(T, vec::unsafe::to_ptr(elts),
|
2012-03-12 22:04:27 -05:00
|
|
|
elts.len() as c_uint);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn C_array(ty: TypeRef, elts: ~[ValueRef]) -> ValueRef unsafe {
|
2012-02-28 22:43:39 -06:00
|
|
|
ret llvm::LLVMConstArray(ty, vec::unsafe::to_ptr(elts),
|
2012-03-12 22:04:27 -05:00
|
|
|
elts.len() as c_uint);
|
2011-07-27 17:14:59 -05:00
|
|
|
}
|
2011-08-04 12:46:10 -05:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn C_bytes(bytes: ~[u8]) -> ValueRef unsafe {
|
2011-10-12 15:31:41 -05:00
|
|
|
ret llvm::LLVMConstString(
|
2012-02-28 22:43:39 -06:00
|
|
|
unsafe::reinterpret_cast(vec::unsafe::to_ptr(bytes)),
|
2012-03-12 22:04:27 -05:00
|
|
|
bytes.len() as c_uint, False);
|
2011-08-04 12:46:10 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn C_shape(ccx: @crate_ctxt, bytes: ~[u8]) -> ValueRef {
|
2011-08-04 13:25:09 -05:00
|
|
|
let llshape = C_bytes(bytes);
|
2012-07-14 00:57:48 -05:00
|
|
|
let llglobal = str::as_c_str(ccx.names(~"shape"), |buf| {
|
2012-01-13 02:32:05 -06:00
|
|
|
llvm::LLVMAddGlobal(ccx.llmod, val_ty(llshape), buf)
|
|
|
|
});
|
2011-08-04 13:25:09 -05:00
|
|
|
llvm::LLVMSetInitializer(llglobal, llshape);
|
|
|
|
llvm::LLVMSetGlobalConstant(llglobal, True);
|
2012-02-01 04:04:56 -06:00
|
|
|
lib::llvm::SetLinkage(llglobal, lib::llvm::InternalLinkage);
|
2011-08-04 13:25:09 -05:00
|
|
|
ret llvm::LLVMConstPointerCast(llglobal, T_ptr(T_i8()));
|
|
|
|
}
|
|
|
|
|
2012-03-21 09:42:20 -05:00
|
|
|
fn get_param(fndecl: ValueRef, param: uint) -> ValueRef {
|
|
|
|
llvm::LLVMGetParam(fndecl, param as c_uint)
|
|
|
|
}
|
|
|
|
|
2012-03-12 10:31:22 -05:00
|
|
|
// Used to identify cached monomorphized functions and vtables
|
|
|
|
enum mono_param_id {
|
2012-06-29 18:26:56 -05:00
|
|
|
mono_precise(ty::t, option<~[mono_id]>),
|
2012-03-12 10:31:22 -05:00
|
|
|
mono_any,
|
|
|
|
mono_repr(uint /* size */, uint /* align */),
|
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
type mono_id = @{def: ast::def_id, params: ~[mono_param_id]};
|
2012-02-02 05:37:17 -06:00
|
|
|
fn hash_mono_id(&&mi: mono_id) -> uint {
|
2012-05-23 02:45:18 -05:00
|
|
|
let mut h = syntax::ast_util::hash_def(mi.def);
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(mi.params) |param| {
|
2012-03-12 10:31:22 -05:00
|
|
|
h = h * alt param {
|
|
|
|
mono_precise(ty, vts) {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut h = ty::type_id(ty);
|
2012-06-30 18:19:07 -05:00
|
|
|
do option::iter(vts) |vts| {
|
|
|
|
for vec::each(vts) |vt| { h += hash_mono_id(vt); }
|
2012-03-12 10:31:22 -05:00
|
|
|
}
|
|
|
|
h
|
|
|
|
}
|
|
|
|
mono_any { 1u }
|
|
|
|
mono_repr(sz, align) { sz * (align + 2u) }
|
|
|
|
}
|
2012-03-08 09:10:25 -06:00
|
|
|
}
|
2012-02-02 05:37:17 -06:00
|
|
|
h
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn umax(cx: block, a: ValueRef, b: ValueRef) -> ValueRef {
|
2012-02-01 04:04:56 -06:00
|
|
|
let cond = build::ICmp(cx, lib::llvm::IntULT, a, b);
|
2012-01-27 06:17:06 -06:00
|
|
|
ret build::Select(cx, cond, b, a);
|
2012-01-19 12:21:42 -06:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn umin(cx: block, a: ValueRef, b: ValueRef) -> ValueRef {
|
2012-02-01 04:04:56 -06:00
|
|
|
let cond = build::ICmp(cx, lib::llvm::IntULT, a, b);
|
2012-01-27 06:17:06 -06:00
|
|
|
ret build::Select(cx, cond, a, b);
|
2012-01-19 12:21:42 -06:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn align_to(cx: block, off: ValueRef, align: ValueRef) -> ValueRef {
|
2012-02-21 07:20:18 -06:00
|
|
|
let mask = build::Sub(cx, align, C_int(cx.ccx(), 1));
|
2012-01-27 06:17:06 -06:00
|
|
|
let bumped = build::Add(cx, off, mask);
|
|
|
|
ret build::And(cx, bumped, build::Not(cx, mask));
|
2012-01-19 12:21:42 -06:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn path_str(p: path) -> ~str {
|
|
|
|
let mut r = ~"", first = true;
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(p) |e| {
|
2012-02-03 02:53:37 -06:00
|
|
|
alt e { ast_map::path_name(s) | ast_map::path_mod(s) {
|
|
|
|
if first { first = false; }
|
2012-07-14 00:57:48 -05:00
|
|
|
else { r += ~"::"; }
|
2012-06-10 02:49:59 -05:00
|
|
|
r += *s;
|
2012-02-03 02:53:37 -06:00
|
|
|
} }
|
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn node_id_type(bcx: block, id: ast::node_id) -> ty::t {
|
2012-02-21 07:20:18 -06:00
|
|
|
let tcx = bcx.tcx();
|
2012-02-02 05:37:17 -06:00
|
|
|
let t = ty::node_id_to_type(tcx, id);
|
|
|
|
alt bcx.fcx.param_substs {
|
2012-04-18 23:26:25 -05:00
|
|
|
some(substs) { ty::subst_tps(tcx, substs.tys, t) }
|
2012-03-12 04:05:15 -05:00
|
|
|
_ { assert !ty::type_has_params(t); t }
|
2012-02-02 05:37:17 -06:00
|
|
|
}
|
|
|
|
}
|
2012-02-17 06:17:40 -06:00
|
|
|
fn expr_ty(bcx: block, ex: @ast::expr) -> ty::t {
|
2012-02-02 05:37:17 -06:00
|
|
|
node_id_type(bcx, ex.id)
|
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
fn node_id_type_params(bcx: block, id: ast::node_id) -> ~[ty::t] {
|
2012-02-21 07:20:18 -06:00
|
|
|
let tcx = bcx.tcx();
|
2012-02-09 07:33:00 -06:00
|
|
|
let params = ty::node_id_to_type_params(tcx, id);
|
|
|
|
alt bcx.fcx.param_substs {
|
|
|
|
some(substs) {
|
2012-06-30 18:19:07 -05:00
|
|
|
vec::map(params, |t| ty::subst_tps(tcx, substs.tys, t))
|
2012-02-09 07:33:00 -06:00
|
|
|
}
|
|
|
|
_ { params }
|
|
|
|
}
|
|
|
|
}
|
2012-02-02 05:37:17 -06:00
|
|
|
|
2012-03-28 00:08:48 -05:00
|
|
|
fn field_idx_strict(cx: ty::ctxt, sp: span, ident: ast::ident,
|
2012-06-29 18:26:56 -05:00
|
|
|
fields: ~[ty::field])
|
2012-05-04 00:31:38 -05:00
|
|
|
-> uint {
|
2012-03-28 00:08:48 -05:00
|
|
|
alt ty::field_idx(ident, fields) {
|
2012-05-04 00:31:38 -05:00
|
|
|
none { cx.sess.span_bug(sp, #fmt("base expr doesn't appear to \
|
2012-06-10 02:49:59 -05:00
|
|
|
have a field named %s", *ident)); }
|
2012-05-04 00:31:38 -05:00
|
|
|
some(i) { i }
|
|
|
|
}
|
2012-03-28 00:08:48 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn dummy_substs(tps: ~[ty::t]) -> ty::substs {
|
2012-05-09 08:09:58 -05:00
|
|
|
{self_r: some(ty::re_bound(ty::br_self)),
|
|
|
|
self_ty: none,
|
|
|
|
tps: tps}
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
|
2011-08-22 04:41:49 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|