2011-07-14 19:08:22 -05:00
|
|
|
/**
|
|
|
|
Code that is useful in various trans modules.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
import core::{int, vec, str, uint, option, unsafe};
|
|
|
|
import vec::to_ptr;
|
2011-07-14 19:08:22 -05:00
|
|
|
import std::map::hashmap;
|
2011-12-13 18:25:51 -06:00
|
|
|
import option::some;
|
2011-07-14 19:08:22 -05:00
|
|
|
import syntax::ast;
|
|
|
|
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 util::common::*;
|
|
|
|
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};
|
2011-11-10 10:41:42 -06:00
|
|
|
import lib::llvm::llvm::{ModuleRef, ValueRef, TypeRef, BasicBlockRef};
|
2011-09-12 18:13:28 -05:00
|
|
|
import lib::llvm::{True, False, Bool};
|
2011-11-10 10:41:42 -06:00
|
|
|
import metadata::{csearch};
|
2011-07-14 19:08:22 -05:00
|
|
|
|
|
|
|
// FIXME: These should probably be pulled in here too.
|
2011-09-14 07:34:50 -05:00
|
|
|
import trans::{type_of_fn, drop_ty};
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2012-01-13 02:32:05 -06:00
|
|
|
type namegen = fn@(str) -> str;
|
|
|
|
fn new_namegen() -> namegen {
|
|
|
|
let i = @mutable 0;
|
|
|
|
ret fn@(prefix: str) -> str { *i += 1; prefix + int::str(*i) };
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type derived_tydesc_info = {lltydesc: ValueRef, escapes: bool};
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2011-08-17 21:11:01 -05:00
|
|
|
tag tydesc_kind {
|
2011-08-19 17:16:48 -05:00
|
|
|
tk_static; // Static (monomorphic) type descriptor.
|
|
|
|
tk_param; // Type parameter.
|
|
|
|
tk_derived; // Derived from a typaram or another derived tydesc.
|
2011-08-17 21:11:01 -05:00
|
|
|
}
|
|
|
|
|
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,
|
2011-08-19 03:24:13 -05:00
|
|
|
mutable take_glue: option::t<ValueRef>,
|
2011-08-12 09:15:18 -05:00
|
|
|
mutable drop_glue: option::t<ValueRef>,
|
|
|
|
mutable free_glue: option::t<ValueRef>,
|
|
|
|
mutable cmp_glue: option::t<ValueRef>,
|
2012-01-13 03:58:31 -06:00
|
|
|
ty_params: [uint]};
|
2011-07-21 19:27:34 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A note on nomenclature of linking: "upcall", "extern" and "native".
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* A "native" is an extern that references C code. Called with cdecl.
|
|
|
|
*
|
|
|
|
* An upcall is a native call generated by the compiler (not corresponding to
|
|
|
|
* any user-written call in the code) into librustrt, to perform some helper
|
|
|
|
* task such as bringing a task to life, allocating memory, etc.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
type stats =
|
2011-07-27 07:19:39 -05:00
|
|
|
{mutable n_static_tydescs: uint,
|
|
|
|
mutable n_derived_tydescs: uint,
|
|
|
|
mutable n_glues_created: uint,
|
|
|
|
mutable n_null_glues: uint,
|
|
|
|
mutable n_real_glues: uint,
|
2011-09-02 17:34:58 -05:00
|
|
|
fn_times: @mutable [{ident: str, time: int}]};
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
resource BuilderRef_res(B: llvm::BuilderRef) { llvm::LLVMDisposeBuilder(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.
|
2011-08-19 17:16:48 -05:00
|
|
|
type crate_ctxt =
|
2011-07-27 07:19:39 -05:00
|
|
|
// A mapping from the def_id of each item in this crate to the address
|
|
|
|
// of the first instruction of the item's definition in the executable
|
|
|
|
// we're generating.
|
2011-08-12 09:15:18 -05:00
|
|
|
// TODO: hashmap<tup(tag_id,subtys), @tag_info>
|
2011-08-19 17:16:48 -05:00
|
|
|
{sess: session::session,
|
|
|
|
llmod: ModuleRef,
|
|
|
|
td: target_data,
|
|
|
|
tn: type_names,
|
2011-09-02 17:34:58 -05:00
|
|
|
externs: hashmap<str, ValueRef>,
|
|
|
|
intrinsics: hashmap<str, ValueRef>,
|
2011-08-19 17:16:48 -05:00
|
|
|
item_ids: hashmap<ast::node_id, ValueRef>,
|
|
|
|
ast_map: ast_map::map,
|
2011-12-19 13:56:11 -06:00
|
|
|
exp_map: resolve::exp_map,
|
2011-09-02 17:34:58 -05:00
|
|
|
item_symbols: hashmap<ast::node_id, str>,
|
2011-08-19 17:16:48 -05:00
|
|
|
mutable main_fn: option::t<ValueRef>,
|
|
|
|
link_meta: link::link_meta,
|
|
|
|
tag_sizes: hashmap<ty::t, uint>,
|
2011-10-20 17:26:26 -05:00
|
|
|
discrims: hashmap<ast::def_id, ValueRef>,
|
2011-09-02 17:34:58 -05:00
|
|
|
discrim_symbols: hashmap<ast::node_id, str>,
|
2011-08-19 17:16:48 -05:00
|
|
|
consts: hashmap<ast::node_id, ValueRef>,
|
|
|
|
tydescs: hashmap<ty::t, @tydesc_info>,
|
2012-01-06 07:22:31 -06:00
|
|
|
dicts: hashmap<dict_id, ValueRef>,
|
2011-09-02 17:34:58 -05:00
|
|
|
module_data: hashmap<str, ValueRef>,
|
2011-08-19 17:16:48 -05:00
|
|
|
lltypes: hashmap<ty::t, TypeRef>,
|
|
|
|
names: namegen,
|
|
|
|
sha: std::sha1::sha1,
|
2011-09-02 17:34:58 -05:00
|
|
|
type_sha1s: hashmap<ty::t, str>,
|
|
|
|
type_short_names: hashmap<ty::t, str>,
|
2011-08-19 17:16:48 -05:00
|
|
|
tcx: ty::ctxt,
|
2011-08-31 11:45:37 -05:00
|
|
|
mut_map: mut::mut_map,
|
2011-09-07 08:13:19 -05:00
|
|
|
copy_map: alias::copy_map,
|
2011-11-18 08:10:14 -06:00
|
|
|
last_uses: last_use::last_uses,
|
2011-12-14 07:38:25 -06:00
|
|
|
method_map: typeck::method_map,
|
2012-01-02 09:50:51 -06:00
|
|
|
dict_map: typeck::dict_map,
|
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-10-20 06:48:10 -05:00
|
|
|
gc_cx: gc::ctxt,
|
2011-11-09 23:55:09 -06:00
|
|
|
crate_map: ValueRef,
|
2011-12-09 10:32:23 -06:00
|
|
|
dbg_cx: option::t<@debuginfo::debug_ctxt>};
|
2011-07-21 19:27:34 -05:00
|
|
|
|
|
|
|
type local_ctxt =
|
2011-09-02 17:34:58 -05:00
|
|
|
{path: [str],
|
|
|
|
module_path: [str],
|
2011-07-27 07:19:39 -05:00
|
|
|
ccx: @crate_ctxt};
|
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
|
|
|
|
2011-10-07 04:20:51 -05:00
|
|
|
tag local_val { local_mem(ValueRef); local_imm(ValueRef); }
|
|
|
|
|
2012-01-02 09:50:51 -06:00
|
|
|
type fn_ty_param = {desc: ValueRef, dicts: option::t<[ValueRef]>};
|
|
|
|
|
2011-08-03 17:39:43 -05:00
|
|
|
// Function context. Every LLVM function we create will have one of
|
|
|
|
// these.
|
2011-08-19 17:16:48 -05: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.
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
// The three implicit arguments that arrive in the function we're
|
2011-08-03 17:39:43 -05:00
|
|
|
// creating. For instance, foo(int, int) is really foo(ret*,
|
|
|
|
// task*, env*, int, int). These are also available via
|
|
|
|
// llvm::LLVMGetParam(llfn, uint) where uint = 1, 2, 0
|
|
|
|
// respectively, but we unpack them into these fields for
|
|
|
|
// convenience.
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
// Points to the current task.
|
|
|
|
|
|
|
|
// Points to the current environment (bindings of variables to
|
2012-01-13 03:58:31 -06:00
|
|
|
// values), if this is a regular function
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-08-03 17:39:43 -05:00
|
|
|
// Points to where the return value of this function should end
|
|
|
|
// up.
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
// The next three elements: "hoisted basic blocks" containing
|
|
|
|
// 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.
|
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.)
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
// The first block containing derived tydescs received from the
|
|
|
|
// runtime. See description of derived_tydescs, below.
|
|
|
|
|
|
|
|
// The last block of the llderivedtydescs group.
|
|
|
|
|
|
|
|
// A block for all of the dynamically sized allocas. This must be
|
|
|
|
// after llderivedtydescs, because these sometimes depend on
|
|
|
|
// information computed from derived tydescs.
|
|
|
|
|
2011-08-17 15:15:04 -05:00
|
|
|
// The token used to clear the dynamic allocas at the end of this frame.
|
|
|
|
|
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.
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-08-03 17:39:43 -05:00
|
|
|
// If this function is actually a iter, a block containing the
|
|
|
|
// code called whenever the iter calls 'put'.
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
// The next four items: hash tables mapping from AST def_ids to
|
|
|
|
// LLVM-stuff-in-the-frame.
|
|
|
|
|
|
|
|
// Maps arguments to allocas created for them in llallocas.
|
|
|
|
|
|
|
|
// Maps the def_ids for local variables to the allocas created for
|
|
|
|
// them in llallocas.
|
|
|
|
|
2011-08-03 17:39:43 -05:00
|
|
|
// The same as above, but for variables accessed via the frame
|
|
|
|
// pointer we pass into an iter, for access to the static
|
|
|
|
// environment of the iter-calling frame.
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-08-03 17:39:43 -05:00
|
|
|
// For convenience, a vector of the incoming tydescs for each of
|
|
|
|
// this functions type parameters, fetched via llvm::LLVMGetParam.
|
2011-08-13 02:09:25 -05:00
|
|
|
// For example, for a function foo::<A, B, C>(), lltydescs contains
|
2011-08-03 17:39:43 -05:00
|
|
|
// the ValueRefs for the tydescs for A, B, and C.
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
// Derived tydescs are tydescs created at runtime, for types that
|
|
|
|
// involve type parameters inside type constructors. For example,
|
|
|
|
// suppose a function parameterized by T creates a vector of type
|
2011-08-03 17:39:43 -05:00
|
|
|
// [T]. The function doesn't know what T is until runtime, and
|
|
|
|
// the function's caller knows T but doesn't know that a vector is
|
2011-07-27 07:19:39 -05:00
|
|
|
// involved. So a tydesc for [T] can't be created until runtime,
|
2011-08-03 17:39:43 -05:00
|
|
|
// when information about both "[T]" and "T" are available. When
|
|
|
|
// such a tydesc is created, we cache it in the derived_tydescs
|
|
|
|
// table for the next time that such a tydesc is needed.
|
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.
|
2011-08-02 17:13:08 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
// The source span where this function comes from, for error
|
|
|
|
// reporting.
|
|
|
|
|
|
|
|
// This function's enclosing local context.
|
2011-08-19 17:16:48 -05:00
|
|
|
{llfn: ValueRef,
|
|
|
|
llenv: ValueRef,
|
|
|
|
llretptr: ValueRef,
|
|
|
|
mutable llstaticallocas: BasicBlockRef,
|
2011-09-23 04:01:09 -05:00
|
|
|
mutable llloadenv: BasicBlockRef,
|
2011-08-19 17:16:48 -05:00
|
|
|
mutable llderivedtydescs_first: BasicBlockRef,
|
|
|
|
mutable llderivedtydescs: BasicBlockRef,
|
|
|
|
mutable lldynamicallocas: BasicBlockRef,
|
|
|
|
mutable llreturn: BasicBlockRef,
|
|
|
|
mutable llobstacktoken: option::t<ValueRef>,
|
|
|
|
mutable llself: option::t<val_self_pair>,
|
2011-10-07 04:20:51 -05:00
|
|
|
llargs: hashmap<ast::node_id, local_val>,
|
|
|
|
lllocals: hashmap<ast::node_id, local_val>,
|
2011-08-19 17:16:48 -05:00
|
|
|
llupvars: hashmap<ast::node_id, ValueRef>,
|
2012-01-02 09:50:51 -06:00
|
|
|
mutable lltyparams: [fn_ty_param],
|
2011-08-19 17:16:48 -05:00
|
|
|
derived_tydescs: hashmap<ty::t, derived_tydesc_info>,
|
|
|
|
id: ast::node_id,
|
2011-09-14 06:48:51 -05:00
|
|
|
ret_style: ast::ret_style,
|
2011-08-19 17:16:48 -05:00
|
|
|
sp: span,
|
|
|
|
lcx: @local_ctxt};
|
2011-07-21 19:27:34 -05:00
|
|
|
|
|
|
|
tag cleanup {
|
2011-10-18 17:07:40 -05:00
|
|
|
clean(fn@(@block_ctxt) -> @block_ctxt);
|
|
|
|
clean_temp(ValueRef, fn@(@block_ctxt) -> @block_ctxt);
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn add_clean(cx: @block_ctxt, val: ValueRef, ty: ty::t) {
|
2011-10-07 07:46:23 -05:00
|
|
|
if !ty::type_needs_drop(bcx_tcx(cx), ty) { ret; }
|
2011-09-13 18:10:39 -05:00
|
|
|
let scope_cx = find_scope_cx(cx);
|
|
|
|
scope_cx.cleanups += [clean(bind drop_ty(_, val, ty))];
|
2011-09-16 16:37:58 -05:00
|
|
|
scope_cx.lpad_dirty = true;
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
2011-09-12 04:27:30 -05:00
|
|
|
fn add_clean_temp(cx: @block_ctxt, val: ValueRef, ty: ty::t) {
|
2011-10-07 07:46:23 -05:00
|
|
|
if !ty::type_needs_drop(bcx_tcx(cx), ty) { ret; }
|
2011-10-10 03:58:53 -05:00
|
|
|
fn do_drop(bcx: @block_ctxt, val: ValueRef, ty: ty::t) ->
|
2011-09-02 17:34:58 -05:00
|
|
|
@block_ctxt {
|
2011-10-10 03:58:53 -05:00
|
|
|
if ty::type_is_immediate(bcx_tcx(bcx), ty) {
|
|
|
|
ret trans::drop_ty_immediate(bcx, val, ty);
|
|
|
|
} else {
|
|
|
|
ret drop_ty(bcx, val, ty);
|
|
|
|
}
|
2011-08-26 04:20:10 -05:00
|
|
|
}
|
2011-09-13 18:10:39 -05:00
|
|
|
let scope_cx = find_scope_cx(cx);
|
|
|
|
scope_cx.cleanups +=
|
2011-10-10 03:58:53 -05:00
|
|
|
[clean_temp(val, bind do_drop(_, val, ty))];
|
2011-09-16 16:37:58 -05:00
|
|
|
scope_cx.lpad_dirty = true;
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
2011-09-26 15:13:08 -05:00
|
|
|
fn add_clean_temp_mem(cx: @block_ctxt, val: ValueRef, ty: ty::t) {
|
2011-10-07 07:46:23 -05:00
|
|
|
if !ty::type_needs_drop(bcx_tcx(cx), ty) { ret; }
|
2011-09-26 15:13:08 -05:00
|
|
|
let scope_cx = find_scope_cx(cx);
|
|
|
|
scope_cx.cleanups += [clean_temp(val, bind drop_ty(_, val, ty))];
|
|
|
|
scope_cx.lpad_dirty = true;
|
|
|
|
}
|
2011-09-27 06:19:55 -05:00
|
|
|
fn add_clean_free(cx: @block_ctxt, ptr: ValueRef, shared: bool) {
|
|
|
|
let scope_cx = find_scope_cx(cx);
|
|
|
|
let free_fn = if shared { bind trans::trans_shared_free(_, ptr) }
|
2011-12-14 09:31:12 -06:00
|
|
|
else { bind trans::trans_free_if_not_gc(_, ptr) };
|
2011-09-27 06:19:55 -05:00
|
|
|
scope_cx.cleanups += [clean_temp(ptr, free_fn)];
|
|
|
|
scope_cx.lpad_dirty = true;
|
|
|
|
}
|
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.
|
2011-11-18 08:10:14 -06:00
|
|
|
fn revoke_clean(cx: @block_ctxt, val: ValueRef) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let sc_cx = find_scope_cx(cx);
|
|
|
|
let found = -1;
|
|
|
|
let i = 0;
|
2011-08-15 23:54:52 -05:00
|
|
|
for c: cleanup in sc_cx.cleanups {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt c {
|
|
|
|
clean_temp(v, _) {
|
|
|
|
if v as uint == val as uint { found = i; break; }
|
|
|
|
}
|
|
|
|
_ { }
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
2011-11-23 03:32:24 -06:00
|
|
|
// The value does not have a cleanup associated with it.
|
|
|
|
if found == -1 { ret; }
|
2011-07-21 19:27:34 -05:00
|
|
|
// We found the cleanup and remove it
|
2011-07-27 07:19:39 -05:00
|
|
|
sc_cx.cleanups =
|
2011-12-13 18:25:51 -06:00
|
|
|
vec::slice(sc_cx.cleanups, 0u, found as uint) +
|
|
|
|
vec::slice(sc_cx.cleanups, (found as uint) + 1u,
|
|
|
|
vec::len(sc_cx.cleanups));
|
2011-09-16 16:37:58 -05:00
|
|
|
sc_cx.lpad_dirty = true;
|
2011-11-18 08:10:14 -06:00
|
|
|
ret;
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn get_res_dtor(ccx: @crate_ctxt, sp: span, did: ast::def_id, inner_t: ty::t)
|
|
|
|
-> ValueRef {
|
2011-08-04 12:46:10 -05:00
|
|
|
if did.crate == ast::local_crate {
|
2011-09-18 15:05:58 -05:00
|
|
|
alt ccx.item_ids.find(did.node) {
|
2011-08-19 17:16:48 -05:00
|
|
|
some(x) { ret x; }
|
2011-09-02 17:34:58 -05:00
|
|
|
_ { ccx.tcx.sess.bug("get_res_dtor: can't find resource dtor!"); }
|
2011-08-04 12:46:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-02 05:00:40 -06:00
|
|
|
let param_bounds = ty::lookup_item_type(ccx.tcx, did).bounds;
|
2011-09-15 22:22:03 -05:00
|
|
|
let nil_res = ty::mk_nil(ccx.tcx);
|
|
|
|
// FIXME: Silly check -- mk_nil should have a postcondition
|
|
|
|
check non_ty_var(ccx, nil_res);
|
2012-01-13 03:58:31 -06:00
|
|
|
let f_t = type_of_fn(ccx, sp,
|
2011-09-14 07:34:50 -05:00
|
|
|
[{mode: ast::by_ref, ty: inner_t}],
|
2012-01-02 05:09:26 -06:00
|
|
|
nil_res, *param_bounds);
|
2011-08-04 12:46:10 -05:00
|
|
|
ret trans::get_extern_const(ccx.externs, ccx.llmod,
|
2012-01-12 10:59:49 -06:00
|
|
|
csearch::get_symbol(ccx.sess.cstore,
|
2011-09-18 15:05:58 -05:00
|
|
|
did), f_t);
|
2011-08-04 12:46:10 -05:00
|
|
|
}
|
|
|
|
|
2011-07-21 19:27:34 -05:00
|
|
|
tag block_kind {
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-07-21 19:27:34 -05:00
|
|
|
// A scope block is a basic block created by translating a block { ... }
|
|
|
|
// the the source language. Since these blocks create variable scope, any
|
|
|
|
// variables created in them that are still live at the end of the block
|
|
|
|
// must be dropped and cleaned up when the block ends.
|
|
|
|
SCOPE_BLOCK;
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-07-21 19:27:34 -05:00
|
|
|
// A basic block created from the body of a loop. Contains pointers to
|
|
|
|
// which block to jump to in the case of "continue" or "break", with the
|
|
|
|
// "continue" block optional, because "while" and "do while" don't support
|
|
|
|
// "continue" (TODO: is this intentional?)
|
2011-08-12 09:15:18 -05:00
|
|
|
LOOP_SCOPE_BLOCK(option::t<@block_ctxt>, @block_ctxt);
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
|
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.
|
|
|
|
NON_SCOPE_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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.
|
2011-08-19 17:16:48 -05:00
|
|
|
type block_ctxt =
|
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.
|
|
|
|
// The 'kind' of basic block this is.
|
|
|
|
// A list of functions that run at the end of translating this
|
|
|
|
// block, cleaning up any variables that were introduced in the
|
|
|
|
// block and need to go out of scope at the end of it.
|
|
|
|
// The source span where this block comes from, for error
|
|
|
|
// reporting. FIXME this is not currently reliable
|
|
|
|
// The function context for the function to which this block is
|
|
|
|
// attached.
|
2011-08-19 17:16:48 -05:00
|
|
|
{llbb: BasicBlockRef,
|
2011-08-24 07:54:55 -05:00
|
|
|
mutable terminated: bool,
|
2011-09-21 05:40:27 -05:00
|
|
|
mutable unreachable: bool,
|
2011-08-19 17:16:48 -05:00
|
|
|
parent: block_parent,
|
|
|
|
kind: block_kind,
|
|
|
|
mutable cleanups: [cleanup],
|
2011-09-16 16:37:58 -05:00
|
|
|
mutable lpad_dirty: bool,
|
2011-09-13 18:10:39 -05:00
|
|
|
mutable lpad: option::t<BasicBlockRef>,
|
2011-08-19 17:16:48 -05:00
|
|
|
sp: span,
|
2011-12-18 22:32:38 -06:00
|
|
|
fcx: @fn_ctxt};
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2011-08-12 09:15:18 -05:00
|
|
|
// FIXME: we should be able to use option::t<@block_parent> here but
|
2011-07-21 19:27:34 -05:00
|
|
|
// the infinite-tag check in rustboot gets upset.
|
|
|
|
tag block_parent { parent_none; parent_some(@block_ctxt); }
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type result = {bcx: @block_ctxt, val: ValueRef};
|
|
|
|
type result_t = {bcx: @block_ctxt, val: ValueRef, ty: ty::t};
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn extend_path(cx: @local_ctxt, name: str) -> @local_ctxt {
|
2011-08-19 17:16:48 -05:00
|
|
|
ret @{path: cx.path + [name] with *cx};
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn rslt(bcx: @block_ctxt, val: ValueRef) -> result {
|
2011-09-15 22:22:03 -05:00
|
|
|
{bcx: bcx, val: val}
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
|
2011-09-02 17:34:58 -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-01-02 06:26:51 -06:00
|
|
|
fn val_ty(&&v: ValueRef) -> TypeRef { ret llvm::LLVMTypeOf(v); }
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2011-09-02 17:34:58 -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 {
|
|
|
|
let elt_count = llvm::LLVMCountStructElementTypes(llstructty);
|
|
|
|
assert (n < elt_count);
|
2011-12-13 18:25:51 -06:00
|
|
|
let elt_tys = vec::init_elt(T_nil(), elt_count);
|
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
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn find_scope_cx(cx: @block_ctxt) -> @block_ctxt {
|
2011-07-27 07:19:39 -05:00
|
|
|
if cx.kind != NON_SCOPE_BLOCK { ret cx; }
|
|
|
|
alt cx.parent {
|
|
|
|
parent_some(b) { ret find_scope_cx(b); }
|
|
|
|
parent_none. {
|
2011-09-02 17:34:58 -05:00
|
|
|
cx.fcx.lcx.ccx.sess.bug("trans::find_scope_cx() " +
|
|
|
|
"called on parentless block_ctxt");
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-07-21 19:27:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accessors
|
|
|
|
// TODO: When we have overloading, simplify these names!
|
|
|
|
|
2011-11-23 03:56:10 -06:00
|
|
|
pure fn bcx_tcx(bcx: @block_ctxt) -> ty::ctxt { ret bcx.fcx.lcx.ccx.tcx; }
|
|
|
|
pure fn bcx_ccx(bcx: @block_ctxt) -> @crate_ctxt { ret bcx.fcx.lcx.ccx; }
|
|
|
|
pure fn bcx_lcx(bcx: @block_ctxt) -> @local_ctxt { ret bcx.fcx.lcx; }
|
|
|
|
pure fn bcx_fcx(bcx: @block_ctxt) -> @fn_ctxt { ret bcx.fcx; }
|
|
|
|
pure fn fcx_ccx(fcx: @fn_ctxt) -> @crate_ctxt { ret fcx.lcx.ccx; }
|
|
|
|
pure fn fcx_tcx(fcx: @fn_ctxt) -> ty::ctxt { ret fcx.lcx.ccx.tcx; }
|
|
|
|
pure fn lcx_ccx(lcx: @local_ctxt) -> @crate_ctxt { ret lcx.ccx; }
|
|
|
|
pure fn ccx_tcx(ccx: @crate_ctxt) -> ty::ctxt { ret ccx.tcx; }
|
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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-12-07 14:06:12 -06:00
|
|
|
fn T_int_ty(cx: @crate_ctxt, t: ast::int_ty) -> TypeRef {
|
|
|
|
alt t {
|
|
|
|
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() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn T_uint_ty(cx: @crate_ctxt, t: ast::uint_ty) -> TypeRef {
|
|
|
|
alt t {
|
|
|
|
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() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn T_float_ty(cx: @crate_ctxt, t: ast::float_ty) -> TypeRef {
|
|
|
|
alt t {
|
|
|
|
ast::ty_f. { cx.float_type }
|
|
|
|
ast::ty_f32. { T_f32() }
|
|
|
|
ast::ty_f64. { T_f64() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 22:38:24 -05:00
|
|
|
fn T_float(targ_cfg: @session::config) -> TypeRef {
|
|
|
|
ret alt targ_cfg.arch {
|
|
|
|
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-01-02 09:50:51 -06:00
|
|
|
fn T_fn(inputs: [TypeRef], output: TypeRef) -> TypeRef unsafe {
|
|
|
|
ret llvm::LLVMFunctionType(output, to_ptr(inputs),
|
|
|
|
vec::len::<TypeRef>(inputs), False);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2011-10-14 18:45:25 -05:00
|
|
|
fn T_fn_pair(cx: @crate_ctxt, tfn: TypeRef) -> TypeRef {
|
2012-01-05 18:19:12 -06:00
|
|
|
ret T_struct([T_ptr(tfn), T_opaque_cbox_ptr(cx)]);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn T_ptr(t: TypeRef) -> TypeRef { ret llvm::LLVMPointerType(t, 0u); }
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2012-01-02 09:50:51 -06:00
|
|
|
fn T_struct(elts: [TypeRef]) -> TypeRef unsafe {
|
|
|
|
ret llvm::LLVMStructType(to_ptr(elts), vec::len(elts), False);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn T_named_struct(name: str) -> TypeRef {
|
2011-07-27 07:19:39 -05:00
|
|
|
let c = llvm::LLVMGetGlobalContext();
|
2011-09-02 17:34:58 -05:00
|
|
|
ret str::as_buf(name, {|buf| llvm::LLVMStructCreateNamed(c, buf) });
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2011-10-12 15:31:41 -05:00
|
|
|
fn set_struct_body(t: TypeRef, elts: [TypeRef]) unsafe {
|
2011-12-13 18:25:51 -06:00
|
|
|
llvm::LLVMStructSetBody(t, to_ptr(elts), vec::len(elts), False);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
fn T_empty_struct() -> TypeRef { ret T_struct([]); }
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2012-01-02 09:50:51 -06:00
|
|
|
// A dict is, in reality, a vtable pointer followed by zero or more pointers
|
|
|
|
// to tydescs and other dicts 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.
|
|
|
|
fn T_dict() -> TypeRef { T_array(T_ptr(T_i8()), 1u) }
|
|
|
|
|
2011-10-14 22:38:24 -05:00
|
|
|
fn T_task(targ_cfg: @session::config) -> TypeRef {
|
2011-09-02 17:34:58 -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 =
|
2011-10-14 19:00:17 -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;
|
|
|
|
}
|
|
|
|
|
2011-10-25 15:13:55 -05:00
|
|
|
fn T_tydesc_field(cx: @crate_ctxt, field: int) -> TypeRef unsafe {
|
2011-07-14 19:08:22 -05:00
|
|
|
// Bit of a kludge: pick the fn typeref out of the tydesc..
|
|
|
|
|
2011-10-12 15:31:41 -05:00
|
|
|
let tydesc_elts: [TypeRef] =
|
2011-12-13 18:25:51 -06:00
|
|
|
vec::init_elt::<TypeRef>(T_nil(),
|
2011-10-12 15:31:41 -05:00
|
|
|
abi::n_tydesc_fields as uint);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-10-14 18:45:25 -05:00
|
|
|
fn T_glue_fn(cx: @crate_ctxt) -> TypeRef {
|
2011-09-02 17:34:58 -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 18:45:25 -05:00
|
|
|
fn T_cmp_glue_fn(cx: @crate_ctxt) -> TypeRef {
|
2011-09-02 17:34:58 -05:00
|
|
|
let s = "cmp_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_cmp_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 {
|
2011-09-02 17:34:58 -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 =
|
2011-10-20 04:56:45 -05:00
|
|
|
T_ptr(T_fn([T_ptr(T_nil()), T_ptr(T_nil()), tydescpp,
|
2011-08-19 17:16:48 -05:00
|
|
|
pvoid], T_void()));
|
2011-07-27 07:19:39 -05:00
|
|
|
let cmp_glue_fn_ty =
|
2011-10-20 04:56:45 -05:00
|
|
|
T_ptr(T_fn([T_ptr(T_i1()), T_ptr(tydesc), tydescpp,
|
2011-08-19 17:16:48 -05:00
|
|
|
pvoid, pvoid, T_i8()], 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 =
|
2011-10-14 22:38:24 -05:00
|
|
|
[tydescpp, int_type, int_type,
|
2011-10-14 19:00:17 -05:00
|
|
|
glue_fn_ty, glue_fn_ty, glue_fn_ty,
|
2011-08-24 10:05:53 -05:00
|
|
|
T_ptr(T_i8()), glue_fn_ty, glue_fn_ty, glue_fn_ty, cmp_glue_fn_ty,
|
2011-10-14 22:38:24 -05:00
|
|
|
T_ptr(T_i8()), T_ptr(T_i8()), int_type, int_type];
|
2011-07-14 19:08:22 -05:00
|
|
|
set_struct_body(tydesc, elems);
|
|
|
|
ret tydesc;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn T_array(t: TypeRef, n: uint) -> TypeRef { ret llvm::LLVMArrayType(t, n); }
|
2011-07-14 19:08:22 -05:00
|
|
|
|
|
|
|
// Interior vector.
|
|
|
|
//
|
|
|
|
// TODO: Support user-defined vector sizes.
|
2011-10-14 22:38:24 -05:00
|
|
|
fn T_vec2(targ_cfg: @session::config, t: TypeRef) -> TypeRef {
|
|
|
|
ret T_struct([T_int(targ_cfg), // fill
|
|
|
|
T_int(targ_cfg), // alloc
|
2011-08-25 03:18:02 -05:00
|
|
|
T_array(t, 0u)]); // elements
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2011-10-14 22:38:24 -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
|
|
|
|
2011-10-14 18:45:25 -05:00
|
|
|
fn T_box(cx: @crate_ctxt, t: TypeRef) -> TypeRef {
|
2011-10-14 19:00:17 -05:00
|
|
|
ret T_struct([cx.int_type, t]);
|
2011-10-14 18:45:25 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2011-10-14 18:45:25 -05:00
|
|
|
fn T_port(cx: @crate_ctxt, _t: TypeRef) -> TypeRef {
|
2011-10-14 19:00:17 -05:00
|
|
|
ret T_struct([cx.int_type]); // Refcount
|
2011-07-14 19:08:22 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-10-14 18:45:25 -05:00
|
|
|
fn T_chan(cx: @crate_ctxt, _t: TypeRef) -> TypeRef {
|
2011-10-14 19:00:17 -05:00
|
|
|
ret T_struct([cx.int_type]); // Refcount
|
2011-07-14 19:08:22 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-10-14 18:45:25 -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 {
|
2011-09-02 17:34:58 -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-01-05 18:19:12 -06:00
|
|
|
fn T_opaque_cbox_ptr(cx: @crate_ctxt) -> TypeRef {
|
|
|
|
let s = "*cbox";
|
2012-01-13 02:32:05 -06:00
|
|
|
alt name_has_type(cx.tn, s) { some(t) { ret t; } _ {} }
|
2012-01-05 18:19:12 -06:00
|
|
|
let t = T_ptr(T_struct([cx.int_type,
|
|
|
|
T_ptr(cx.tydesc_type),
|
|
|
|
T_i8() /* represents closed over tydescs
|
|
|
|
and data go here; see trans_closure.rs*/
|
|
|
|
]));
|
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-11-23 14:01:15 -06:00
|
|
|
fn T_tag_variant(cx: @crate_ctxt) -> TypeRef {
|
|
|
|
ret cx.int_type;
|
|
|
|
}
|
|
|
|
|
2011-10-14 18:45:25 -05:00
|
|
|
fn T_tag(cx: @crate_ctxt, size: uint) -> TypeRef {
|
2011-09-02 17:34:58 -05:00
|
|
|
let s = "tag_" + uint::to_str(size, 10u);
|
2012-01-13 02:32:05 -06:00
|
|
|
alt name_has_type(cx.tn, s) { some(t) { ret t; } _ {} }
|
2011-09-12 04:27:30 -05:00
|
|
|
let t =
|
|
|
|
if size == 0u {
|
2011-11-23 14:01:15 -06:00
|
|
|
T_struct([T_tag_variant(cx)])
|
|
|
|
} else { T_struct([T_tag_variant(cx), T_array(T_i8(), size)]) };
|
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 18:45:25 -05:00
|
|
|
fn T_opaque_tag(cx: @crate_ctxt) -> TypeRef {
|
2011-09-02 17:34:58 -05:00
|
|
|
let s = "opaque_tag";
|
2012-01-13 02:32:05 -06:00
|
|
|
alt name_has_type(cx.tn, s) { some(t) { ret t; } _ {} }
|
2011-11-23 14:01:15 -06:00
|
|
|
let t = T_struct([T_tag_variant(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;
|
|
|
|
}
|
|
|
|
|
2011-10-14 18:45:25 -05:00
|
|
|
fn T_opaque_tag_ptr(cx: @crate_ctxt) -> TypeRef {
|
|
|
|
ret T_ptr(T_opaque_tag(cx));
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2011-10-14 18:45:25 -05:00
|
|
|
fn T_captured_tydescs(cx: @crate_ctxt, n: uint) -> TypeRef {
|
2011-12-13 18:25:51 -06:00
|
|
|
ret T_struct(vec::init_elt::<TypeRef>(T_ptr(cx.tydesc_type), n));
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2012-01-07 15:44:14 -06:00
|
|
|
fn T_opaque_iface_ptr(cx: @crate_ctxt) -> TypeRef {
|
|
|
|
let tdptr = T_ptr(cx.tydesc_type);
|
|
|
|
T_ptr(T_box(cx, T_struct([tdptr, tdptr, T_i8()])))
|
|
|
|
}
|
|
|
|
|
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 {
|
2011-11-16 14:15:54 -06:00
|
|
|
let u_hi = (u >> 32u64) as uint;
|
|
|
|
let u_lo = u as uint;
|
|
|
|
ret llvm::LLVMRustConstInt(t, u_hi, u_lo, sign_extend);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn C_floating(s: str, t: TypeRef) -> ValueRef {
|
2011-09-02 17:34:58 -05:00
|
|
|
ret str::as_buf(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 {
|
|
|
|
if b {
|
2011-11-13 23:34:58 -06:00
|
|
|
ret C_integral(T_bool(), 1u64, False);
|
|
|
|
} else { ret C_integral(T_bool(), 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
|
|
|
}
|
|
|
|
|
2011-10-14 18:45:25 -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
|
|
|
|
2011-10-14 18:45:25 -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.
|
2011-09-12 04:27:30 -05:00
|
|
|
fn C_cstr(cx: @crate_ctxt, s: str) -> ValueRef {
|
2011-09-02 17:34:58 -05:00
|
|
|
let sc =
|
|
|
|
str::as_buf(s,
|
|
|
|
{|buf|
|
|
|
|
llvm::LLVMConstString(buf, str::byte_len(s), False)
|
|
|
|
});
|
|
|
|
let g =
|
2012-01-13 02:32:05 -06:00
|
|
|
str::as_buf(cx.names("str"),
|
2011-09-02 17:34:58 -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);
|
|
|
|
llvm::LLVMSetLinkage(g, lib::llvm::LLVMInternalLinkage as llvm::Linkage);
|
|
|
|
ret g;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a Plain Old LLVM String:
|
2011-09-12 04:27:30 -05:00
|
|
|
fn C_postr(s: str) -> ValueRef {
|
2011-09-02 17:34:58 -05:00
|
|
|
ret str::as_buf(s,
|
|
|
|
{|buf|
|
|
|
|
llvm::LLVMConstString(buf, str::byte_len(s), False)
|
|
|
|
});
|
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 {
|
|
|
|
let i = 0u;
|
|
|
|
let elts: [ValueRef] = [];
|
|
|
|
while i < size { elts += [C_u8(0u)]; i += 1u; }
|
2011-12-13 18:25:51 -06:00
|
|
|
ret llvm::LLVMConstArray(T_i8(), vec::to_ptr(elts),
|
|
|
|
vec::len(elts));
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2011-10-12 15:31:41 -05:00
|
|
|
fn C_struct(elts: [ValueRef]) -> ValueRef unsafe {
|
2011-12-13 18:25:51 -06:00
|
|
|
ret llvm::LLVMConstStruct(vec::to_ptr(elts), vec::len(elts),
|
2011-10-12 15:31:41 -05:00
|
|
|
False);
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2011-10-12 15:31:41 -05:00
|
|
|
fn C_named_struct(T: TypeRef, elts: [ValueRef]) -> ValueRef unsafe {
|
2011-12-13 18:25:51 -06:00
|
|
|
ret llvm::LLVMConstNamedStruct(T, vec::to_ptr(elts),
|
|
|
|
vec::len(elts));
|
2011-07-14 19:08:22 -05:00
|
|
|
}
|
|
|
|
|
2011-10-12 15:31:41 -05:00
|
|
|
fn C_array(ty: TypeRef, elts: [ValueRef]) -> ValueRef unsafe {
|
2011-12-13 18:25:51 -06:00
|
|
|
ret llvm::LLVMConstArray(ty, vec::to_ptr(elts),
|
|
|
|
vec::len(elts));
|
2011-07-27 17:14:59 -05:00
|
|
|
}
|
2011-08-04 12:46:10 -05:00
|
|
|
|
2011-10-12 15:31:41 -05:00
|
|
|
fn C_bytes(bytes: [u8]) -> ValueRef unsafe {
|
|
|
|
ret llvm::LLVMConstString(
|
|
|
|
unsafe::reinterpret_cast(vec::to_ptr(bytes)),
|
|
|
|
vec::len(bytes), False);
|
2011-08-04 12:46:10 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -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-01-13 02:32:05 -06:00
|
|
|
let llglobal = str::as_buf(ccx.names("shape"), {|buf|
|
|
|
|
llvm::LLVMAddGlobal(ccx.llmod, val_ty(llshape), buf)
|
|
|
|
});
|
2011-08-04 13:25:09 -05:00
|
|
|
llvm::LLVMSetInitializer(llglobal, llshape);
|
|
|
|
llvm::LLVMSetGlobalConstant(llglobal, True);
|
|
|
|
llvm::LLVMSetLinkage(llglobal,
|
|
|
|
lib::llvm::LLVMInternalLinkage as llvm::Linkage);
|
|
|
|
ret llvm::LLVMConstPointerCast(llglobal, T_ptr(T_i8()));
|
|
|
|
}
|
|
|
|
|
2011-09-01 19:25:06 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
pure fn valid_variant_index(ix: uint, cx: @block_ctxt, tag_id: ast::def_id,
|
|
|
|
variant_id: ast::def_id) -> bool {
|
2011-09-02 17:34:58 -05:00
|
|
|
|
2011-09-01 19:25:06 -05:00
|
|
|
// Handwaving: it's ok to pretend this code is referentially
|
|
|
|
// transparent, because the relevant parts of the type context don't
|
|
|
|
// change. (We're not adding new variants during trans.)
|
2011-09-02 17:34:58 -05:00
|
|
|
unchecked{
|
|
|
|
let variant =
|
|
|
|
ty::tag_variant_with_id(bcx_tcx(cx), tag_id, variant_id);
|
|
|
|
ix < vec::len(variant.args)
|
2011-09-01 19:25:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
pure fn type_has_static_size(cx: @crate_ctxt, t: ty::t) -> bool {
|
2011-09-02 20:59:22 -05:00
|
|
|
!ty::type_has_dynamic_size(cx.tcx, t)
|
|
|
|
}
|
|
|
|
|
2011-09-15 22:22:03 -05:00
|
|
|
pure fn non_ty_var(cx: @crate_ctxt, t: ty::t) -> bool {
|
2011-09-24 18:33:26 -05:00
|
|
|
let st = ty::struct(cx.tcx, t);
|
2011-09-15 22:22:03 -05:00
|
|
|
alt st {
|
|
|
|
ty::ty_var(_) { false }
|
|
|
|
_ { true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-16 15:03:26 -05:00
|
|
|
pure fn returns_non_ty_var(cx: @crate_ctxt, t: ty::t) -> bool {
|
|
|
|
non_ty_var(cx, ty::ty_fn_ret(cx.tcx, t))
|
|
|
|
}
|
|
|
|
|
2011-09-17 12:17:40 -05:00
|
|
|
pure fn type_is_tup_like(cx: @block_ctxt, t: ty::t) -> bool {
|
|
|
|
let tcx = bcx_tcx(cx);
|
|
|
|
ty::type_is_tup_like(tcx, t)
|
|
|
|
}
|
|
|
|
|
2012-01-06 07:22:31 -06:00
|
|
|
// Used to identify cached dictionaries
|
|
|
|
tag dict_param {
|
|
|
|
dict_param_dict(dict_id);
|
|
|
|
dict_param_ty(ty::t);
|
|
|
|
}
|
2012-01-12 09:57:30 -06:00
|
|
|
type dict_id = @{def: ast::def_id, params: [dict_param]};
|
2012-01-06 07:22:31 -06:00
|
|
|
fn hash_dict_id(&&dp: dict_id) -> uint {
|
2012-01-12 09:57:30 -06:00
|
|
|
let h = syntax::ast_util::hash_def_id(dp.def);
|
2012-01-06 07:22:31 -06:00
|
|
|
for param in dp.params {
|
|
|
|
h = h << 2u;
|
|
|
|
alt param {
|
|
|
|
dict_param_dict(d) { h += hash_dict_id(d); }
|
|
|
|
dict_param_ty(t) { h += t; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
//
|