diff --git a/src/librustc/back/upcall.rs b/src/librustc/back/upcall.rs index 4d2ea4eb4a642..a4b97bfccb783 100644 --- a/src/librustc/back/upcall.rs +++ b/src/librustc/back/upcall.rs @@ -16,8 +16,6 @@ use lib::llvm::{ModuleRef, ValueRef, TypeRef}; pub struct Upcalls { trace: ValueRef, - call_shim_on_c_stack: ValueRef, - call_shim_on_rust_stack: ValueRef, rust_personality: ValueRef, reset_stack_limit: ValueRef } @@ -45,14 +43,6 @@ pub fn declare_upcalls(targ_cfg: @session::config, trace: dv(~"trace", ~[T_ptr(T_i8()), T_ptr(T_i8()), int_t]), - call_shim_on_c_stack: - d(~"call_shim_on_c_stack", - // arguments: void *args, void *fn_ptr - ~[T_ptr(T_i8()), T_ptr(T_i8())], - int_t), - call_shim_on_rust_stack: - d(~"call_shim_on_rust_stack", - ~[T_ptr(T_i8()), T_ptr(T_i8())], int_t), rust_personality: nothrow(d(~"rust_personality", ~[], T_i32())), reset_stack_limit: diff --git a/src/librustc/middle/trans/base.rs b/src/librustc/middle/trans/base.rs index 90449e8a17aef..b54c3e023dd4b 100644 --- a/src/librustc/middle/trans/base.rs +++ b/src/librustc/middle/trans/base.rs @@ -169,21 +169,23 @@ pub fn decl_internal_cdecl_fn(llmod: ModuleRef, name: ~str, llty: TypeRef) -> pub fn get_extern_fn(externs: ExternMap, llmod: ModuleRef, - name: @str, + name: &str, cc: lib::llvm::CallConv, ty: TypeRef) -> ValueRef { - match externs.find(&name) { - Some(n) => return copy *n, + match externs.find_equiv(&name) { + Some(&n) => { + return n; + } None => () } let f = decl_fn(llmod, name, cc, ty); - externs.insert(name, f); + externs.insert(name.to_owned(), f); return f; } pub fn get_extern_const(externs: ExternMap, llmod: ModuleRef, - name: @str, ty: TypeRef) -> ValueRef { - match externs.find(&name) { + name: &str, ty: TypeRef) -> ValueRef { + match externs.find_equiv(&name) { Some(n) => return copy *n, None => () } @@ -191,34 +193,11 @@ pub fn get_extern_const(externs: ExternMap, llmod: ModuleRef, let c = str::as_c_str(name, |buf| { llvm::LLVMAddGlobal(llmod, ty, buf) }); - externs.insert(name, c); + externs.insert(name.to_owned(), c); return c; } } - fn get_simple_extern_fn(cx: block, - externs: ExternMap, - llmod: ModuleRef, - name: @str, - n_args: int) -> ValueRef { - let _icx = cx.insn_ctxt("get_simple_extern_fn"); - let ccx = cx.fcx.ccx; - let inputs = vec::from_elem(n_args as uint, ccx.int_type); - let output = ccx.int_type; - let t = T_fn(inputs, output); - return get_extern_fn(externs, llmod, name, lib::llvm::CCallConv, t); -} - -pub fn trans_foreign_call(cx: block, externs: ExternMap, - llmod: ModuleRef, name: @str, args: &[ValueRef]) -> - ValueRef { - let _icx = cx.insn_ctxt("trans_foreign_call"); - let n = args.len() as int; - let llforeign: ValueRef = - get_simple_extern_fn(cx, externs, llmod, name, n); - return Call(cx, llforeign, args); -} - pub fn umax(cx: block, a: ValueRef, b: ValueRef) -> ValueRef { let _icx = cx.insn_ctxt("umax"); let cond = ICmp(cx, lib::llvm::IntULT, a, b); @@ -517,7 +496,6 @@ pub fn get_res_dtor(ccx: @CrateContext, None, ty::lookup_item_type(tcx, parent_id).ty); let llty = type_of_dtor(ccx, class_ty); - let name = name.to_managed(); // :-( get_extern_fn(ccx.externs, ccx.llmod, name, @@ -805,13 +783,13 @@ pub fn fail_if_zero(cx: block, span: span, divrem: ast::binop, } } -pub fn null_env_ptr(bcx: block) -> ValueRef { - C_null(T_opaque_box_ptr(bcx.ccx())) +pub fn null_env_ptr(ccx: @CrateContext) -> ValueRef { + C_null(T_opaque_box_ptr(ccx)) } pub fn trans_external_path(ccx: @CrateContext, did: ast::def_id, t: ty::t) -> ValueRef { - let name = csearch::get_symbol(ccx.sess.cstore, did).to_managed(); // Sad + let name = csearch::get_symbol(ccx.sess.cstore, did); match ty::get(t).sty { ty::ty_bare_fn(_) | ty::ty_closure(_) => { let llty = type_of_fn_from_ty(ccx, t); @@ -1570,7 +1548,7 @@ pub fn mk_standard_basic_blocks(llfn: ValueRef) -> BasicBlocks { // slot where the return value of the function must go. pub fn make_return_pointer(fcx: fn_ctxt, output_type: ty::t) -> ValueRef { unsafe { - if !ty::type_is_immediate(output_type) { + if type_of::return_uses_outptr(output_type) { llvm::LLVMGetParam(fcx.llfn, 0) } else { let lloutputtype = type_of::type_of(*fcx.ccx, output_type); @@ -1611,7 +1589,7 @@ pub fn new_fn_ctxt_w_id(ccx: @CrateContext, ty::subst_tps(ccx.tcx, substs.tys, substs.self_ty, output_type) } }; - let is_immediate = ty::type_is_immediate(substd_output_type); + let uses_outptr = type_of::return_uses_outptr(substd_output_type); let fcx = @mut fn_ctxt_ { llfn: llfndecl, @@ -1623,7 +1601,7 @@ pub fn new_fn_ctxt_w_id(ccx: @CrateContext, llself: None, personality: None, loop_ret: None, - has_immediate_return_value: is_immediate, + caller_expects_out_pointer: uses_outptr, llargs: @mut HashMap::new(), lllocals: @mut HashMap::new(), llupvars: @mut HashMap::new(), @@ -1783,7 +1761,7 @@ pub fn build_return_block(fcx: fn_ctxt) { let ret_cx = raw_block(fcx, false, fcx.llreturn); // Return the value if this function immediate; otherwise, return void. - if fcx.has_immediate_return_value { + if !fcx.caller_expects_out_pointer { Ret(ret_cx, Load(ret_cx, fcx.llretptr.get())) } else { RetVoid(ret_cx) @@ -1871,9 +1849,7 @@ pub fn trans_closure(ccx: @CrateContext, // translation calls that don't have a return value (trans_crate, // trans_mod, trans_item, et cetera) and those that do // (trans_block, trans_expr, et cetera). - if body.node.expr.is_none() || ty::type_is_bot(block_ty) || - ty::type_is_nil(block_ty) - { + if body.node.expr.is_none() || ty::type_is_voidish(block_ty) { bcx = controlflow::trans_block(bcx, body, expr::Ignore); } else { let dest = expr::SaveIn(fcx.llretptr.get()); @@ -2114,13 +2090,14 @@ pub fn trans_item(ccx: @CrateContext, item: &ast::item) { ast::item_fn(ref decl, purity, _abis, ref generics, ref body) => { if purity == ast::extern_fn { let llfndecl = get_item_val(ccx, item.id); - foreign::trans_foreign_fn(ccx, - vec::append(/*bad*/copy *path, - [path_name(item.ident)]), - decl, - body, - llfndecl, - item.id); + foreign::trans_rust_fn_with_foreign_abi( + ccx, + &vec::append(/*bad*/copy *path, + [path_name(item.ident)]), + decl, + body, + llfndecl, + item.id); } else if !generics.is_type_parameterized() { let llfndecl = get_item_val(ccx, item.id); trans_fn(ccx, @@ -2180,7 +2157,7 @@ pub fn trans_item(ccx: @CrateContext, item: &ast::item) { } }, ast::item_foreign_mod(ref foreign_mod) => { - foreign::trans_foreign_mod(ccx, path, foreign_mod); + foreign::trans_foreign_mod(ccx, foreign_mod); } ast::item_struct(struct_def, ref generics) => { if !generics.is_type_parameterized() { @@ -2293,7 +2270,7 @@ pub fn create_entry_wrapper(ccx: @CrateContext, fn create_main(ccx: @CrateContext, main_llfn: ValueRef) -> ValueRef { let nt = ty::mk_nil(); - let llfty = type_of_fn(ccx, [], nt); + let llfty = type_of_rust_fn(ccx, [], nt); let llfdecl = decl_fn(ccx.llmod, "_rust_main", lib::llvm::CCallConv, llfty); @@ -2456,11 +2433,11 @@ pub fn get_item_val(ccx: @CrateContext, id: ast::node_id) -> ValueRef { let llfn = if purity != ast::extern_fn { register_fn(ccx, i.span, my_path, i.id, i.attrs) } else { - foreign::register_foreign_fn(ccx, - i.span, - my_path, - i.id, - i.attrs) + foreign::register_rust_fn_with_foreign_abi(ccx, + i.span, + my_path, + i.id, + i.attrs) }; set_inline_hint_if_appr(i.attrs, llfn); llfn @@ -2485,15 +2462,13 @@ pub fn get_item_val(ccx: @CrateContext, id: ast::node_id) -> ValueRef { exprt = true; register_method(ccx, id, pth, m) } - ast_map::node_foreign_item(ni, _, _, pth) => { + ast_map::node_foreign_item(ni, abis, _, pth) => { exprt = true; match ni.node { ast::foreign_item_fn(*) => { - register_fn(ccx, ni.span, - vec::append(/*bad*/copy *pth, - [path_name(ni.ident)]), - ni.id, - ni.attrs) + let path = vec::append(/*bad*/copy *pth, + [path_name(ni.ident)]); + foreign::register_foreign_item_fn(ccx, abis, &path, ni) } ast::foreign_item_const(*) => { let typ = ty::node_id_to_type(tcx, ni.id); diff --git a/src/librustc/middle/trans/cabi.rs b/src/librustc/middle/trans/cabi.rs index c6f4d23041973..0adba16d53493 100644 --- a/src/librustc/middle/trans/cabi.rs +++ b/src/librustc/middle/trans/cabi.rs @@ -8,14 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use lib::llvm::{llvm, TypeRef, ValueRef, Attribute, Void}; -use middle::trans::base::*; -use middle::trans::build::*; -use middle::trans::common::*; - -use core::libc::c_uint; +use lib::llvm::{TypeRef, Attribute}; use core::option; -use core::vec; pub trait ABIInfo { fn compute_info(&self, @@ -29,164 +23,26 @@ pub struct LLVMType { ty: TypeRef } +/// Metadata describing how the arguments to a native function +/// should be passed in order to respect the native ABI. +/// +/// I will do my best to describe this structure, but these +/// comments are reverse-engineered and may be inaccurate. -NDM pub struct FnType { + /// The LLVM types of each argument. If the cast flag is true, + /// then the argument should be cast, typically because the + /// official argument type will be an int and the rust type is i8 + /// or something like that. arg_tys: ~[LLVMType], - ret_ty: LLVMType, - attrs: ~[option::Option], - sret: bool -} - -pub impl FnType { - fn decl_fn(&self, decl: &fn(fnty: TypeRef) -> ValueRef) -> ValueRef { - let atys = vec::map(self.arg_tys, |t| t.ty); - let rty = self.ret_ty.ty; - let fnty = T_fn(atys, rty); - let llfn = decl(fnty); - - for vec::eachi(self.attrs) |i, a| { - match *a { - option::Some(attr) => { - unsafe { - let llarg = get_param(llfn, i); - llvm::LLVMAddAttribute(llarg, attr as c_uint); - } - } - _ => () - } - } - return llfn; - } - - fn build_shim_args(&self, bcx: block, - arg_tys: &[TypeRef], - llargbundle: ValueRef) -> ~[ValueRef] { - let mut atys = /*bad*/copy self.arg_tys; - let mut attrs = /*bad*/copy self.attrs; - - let mut llargvals = ~[]; - let mut i = 0u; - let n = arg_tys.len(); - if self.sret { - let llretptr = GEPi(bcx, llargbundle, [0u, n]); - let llretloc = Load(bcx, llretptr); - llargvals = ~[llretloc]; - atys = vec::to_owned(atys.tail()); - attrs = vec::to_owned(attrs.tail()); - } - - while i < n { - let llargval = if atys[i].cast { - let arg_ptr = GEPi(bcx, llargbundle, [0u, i]); - let arg_ptr = BitCast(bcx, arg_ptr, T_ptr(atys[i].ty)); - Load(bcx, arg_ptr) - } else if attrs[i].is_some() { - GEPi(bcx, llargbundle, [0u, i]) - } else { - load_inbounds(bcx, llargbundle, [0u, i]) - }; - llargvals.push(llargval); - i += 1u; - } - - return llargvals; - } - - fn build_shim_ret(&self, - bcx: block, - arg_tys: &[TypeRef], - ret_def: bool, - llargbundle: ValueRef, - llretval: ValueRef) { - for vec::eachi(self.attrs) |i, a| { - match *a { - option::Some(attr) => { - unsafe { - llvm::LLVMAddInstrAttribute(llretval, - (i + 1u) as c_uint, - attr as c_uint); - } - } - _ => () - } - } - if self.sret || !ret_def { - return; - } - let n = arg_tys.len(); - // R** llretptr = &args->r; - let llretptr = GEPi(bcx, llargbundle, [0u, n]); - // R* llretloc = *llretptr; /* (args->r) */ - let llretloc = Load(bcx, llretptr); - if self.ret_ty.cast { - let tmp_ptr = BitCast(bcx, llretloc, T_ptr(self.ret_ty.ty)); - // *args->r = r; - Store(bcx, llretval, tmp_ptr); - } else { - // *args->r = r; - Store(bcx, llretval, llretloc); - }; - } - - fn build_wrap_args(&self, - bcx: block, - ret_ty: TypeRef, - llwrapfn: ValueRef, - llargbundle: ValueRef) { - let mut atys = /*bad*/copy self.arg_tys; - let mut attrs = /*bad*/copy self.attrs; - let mut j = 0u; - let llretptr = if self.sret { - atys = vec::to_owned(atys.tail()); - attrs = vec::to_owned(attrs.tail()); - j = 1u; - get_param(llwrapfn, 0u) - } else if self.ret_ty.cast { - let retptr = alloca(bcx, self.ret_ty.ty); - BitCast(bcx, retptr, T_ptr(ret_ty)) - } else { - alloca(bcx, ret_ty) - }; - - let mut i = 0u; - let n = atys.len(); - while i < n { - let mut argval = get_param(llwrapfn, i + j); - if attrs[i].is_some() { - argval = Load(bcx, argval); - store_inbounds(bcx, argval, llargbundle, [0u, i]); - } else if atys[i].cast { - let argptr = GEPi(bcx, llargbundle, [0u, i]); - let argptr = BitCast(bcx, argptr, T_ptr(atys[i].ty)); - Store(bcx, argval, argptr); - } else { - store_inbounds(bcx, argval, llargbundle, [0u, i]); - } - i += 1u; - } - store_inbounds(bcx, llretptr, llargbundle, [0u, n]); - } + /// A list of attributes to be attached to each argument (parallel + /// the `arg_tys` array). If the attribute for a given is Some, + /// then the argument should be passed by reference. + attrs: ~[option::Option], - fn build_wrap_ret(&self, - bcx: block, - arg_tys: &[TypeRef], - llargbundle: ValueRef) { - unsafe { - if llvm::LLVMGetTypeKind(self.ret_ty.ty) == Void { - return; - } - } + /// LLVM return type. + ret_ty: LLVMType, - let llretval = load_inbounds(bcx, llargbundle, [ 0, arg_tys.len() ]); - let llretval = if self.ret_ty.cast { - let retptr = BitCast(bcx, llretval, T_ptr(self.ret_ty.ty)); - Load(bcx, retptr) - } else { - Load(bcx, llretval) - }; - let llretptr = BitCast(bcx, - bcx.fcx.llretptr.get(), - T_ptr(self.ret_ty.ty)); - Store(bcx, llretval, llretptr); - } + /// If true, then an implicit pointer should be added for the result. + sret: bool } diff --git a/src/librustc/middle/trans/cabi_x86.rs b/src/librustc/middle/trans/cabi_x86.rs index fbb605330245c..2f063fe22ec03 100644 --- a/src/librustc/middle/trans/cabi_x86.rs +++ b/src/librustc/middle/trans/cabi_x86.rs @@ -26,40 +26,75 @@ impl ABIInfo for X86_ABIInfo { atys: &[TypeRef], rty: TypeRef, ret_def: bool) -> FnType { - let mut arg_tys = do atys.map |a| { - LLVMType { cast: false, ty: *a } - }; - let mut ret_ty = LLVMType { - cast: false, - ty: rty - }; - let mut attrs = do atys.map |_| { - None - }; + let mut arg_tys = ~[]; + let mut attrs = ~[]; - // Rules for returning structs taken from - // http://www.angelcode.com/dev/callconv/callconv.html - // Clang's ABI handling is in lib/CodeGen/TargetInfo.cpp - let sret = { - let returning_a_struct = unsafe { LLVMGetTypeKind(rty) == Struct && ret_def }; - let big_struct = match self.ccx.sess.targ_cfg.os { - os_win32 | os_macos => llsize_of_alloc(self.ccx, rty) > 8, - _ => true + let ret_ty; + let sret; + if !ret_def { + ret_ty = LLVMType { + cast: false, + ty: T_void(), }; - returning_a_struct && big_struct - }; + sret = false; + } else if unsafe { LLVMGetTypeKind(rty) == Struct } { + // Returning a structure. Most often, this will use + // a hidden first argument. On some platforms, though, + // small structs are returned as integers. + // + // Some links: + // http://www.angelcode.com/dev/callconv/callconv.html + // Clang's ABI handling is in lib/CodeGen/TargetInfo.cpp - if sret { - let ret_ptr_ty = LLVMType { - cast: false, - ty: T_ptr(ret_ty.ty) + enum Strategy { RetValue(TypeRef), RetPointer } + let strategy = match self.ccx.sess.targ_cfg.os { + os_win32 | os_macos => { + match llsize_of_alloc(self.ccx, rty) { + 1 => RetValue(T_i8()), + 2 => RetValue(T_i16()), + 4 => RetValue(T_i32()), + 8 => RetValue(T_i64()), + _ => RetPointer + } + } + _ => { + RetPointer + } }; - arg_tys = ~[ret_ptr_ty] + arg_tys; - attrs = ~[Some(StructRetAttribute)] + attrs; + + match strategy { + RetValue(t) => { + ret_ty = LLVMType { + cast: true, + ty: t + }; + sret = false; + } + RetPointer => { + arg_tys.push(LLVMType { + cast: false, + ty: T_ptr(rty) + }); + attrs.push(Some(StructRetAttribute)); + + ret_ty = LLVMType { + cast: false, + ty: T_void(), + }; + sret = true; + } + } + } else { ret_ty = LLVMType { cast: false, - ty: T_void(), + ty: rty }; + sret = false; + } + + for atys.each |&a| { + arg_tys.push(LLVMType { cast: false, ty: a }); + attrs.push(None); } return FnType { diff --git a/src/librustc/middle/trans/callee.rs b/src/librustc/middle/trans/callee.rs index 3d4649bba4660..7758ffa82b29a 100644 --- a/src/librustc/middle/trans/callee.rs +++ b/src/librustc/middle/trans/callee.rs @@ -39,6 +39,7 @@ use middle::trans::inline; use middle::trans::meth; use middle::trans::monomorphize; use middle::trans::type_of; +use middle::trans::foreign; use middle::ty; use middle::typeck; use util::ppaux::Repr; @@ -46,6 +47,7 @@ use util::ppaux::Repr; use syntax::ast; use syntax::ast_map; use syntax::visit; +use syntax::abi::AbiSet; // Represents a (possibly monomorphized) top-level fn item or method // item. Note that this is just the fn-ptr and is not a Rust closure @@ -196,20 +198,20 @@ pub fn trans_fn_ref_with_vtables( type_params: &[ty::t], // values for fn's ty params vtables: Option) -> FnData { - //! - // - // Translates a reference to a fn/method item, monomorphizing and - // inlining as it goes. - // - // # Parameters - // - // - `bcx`: the current block where the reference to the fn occurs - // - `def_id`: def id of the fn or method item being referenced - // - `ref_id`: node id of the reference to the fn/method, if applicable. - // This parameter may be zero; but, if so, the resulting value may not - // have the right type, so it must be cast before being used. - // - `type_params`: values for each of the fn/method's type parameters - // - `vtables`: values for each bound on each of the type parameters + /*! + * Translates a reference to a fn/method item, monomorphizing and + * inlining as it goes. + * + * # Parameters + * + * - `bcx`: the current block where the reference to the fn occurs + * - `def_id`: def id of the fn or method item being referenced + * - `ref_id`: node id of the reference to the fn/method, if applicable. + * This parameter may be zero; but, if so, the resulting value may not + * have the right type, so it must be cast before being used. + * - `type_params`: values for each of the fn/method's type parameters + * - `vtables`: values for each bound on each of the type parameters + */ let _icx = bcx.insn_ctxt("trans_fn_ref_with_vtables"); let ccx = bcx.ccx(); @@ -291,7 +293,7 @@ pub fn trans_fn_ref_with_vtables( } // Find the actual function pointer. - let val = { + let mut val = { if def_id.crate == ast::local_crate { // Internal reference. get_item_val(ccx, def_id.node) @@ -301,6 +303,35 @@ pub fn trans_fn_ref_with_vtables( } }; + // This is subtle and surprising, but sometimes we have to bitcast + // the resulting fn pointer. The reason has to do with external + // functions. If you have two crates that both bind the same C + // library, they may not use precisely the same types: for + // example, they will probably each declare their own structs, + // which are distinct types from LLVM's point of view (nominal + // types). + // + // Now, if those two crates are linked into an application, and + // they contain inlined code, you can wind up with a situation + // where both of those functions wind up being loaded into this + // application simultaneously. In that case, the same function + // (from LLVM's point of view) requires two types. But of course + // LLVM won't allow one function to have two types. + // + // What we currently do, therefore, is declare the function with + // one of the two types (whichever happens to come first) and then + // bitcast as needed when the function is referenced to make sure + // it has the type we expect. + // + // This can occur on either a crate-local or crate-external + // reference. It also occurs when testing libcore and in some + // other weird situations. Annoying. + let llty = type_of::type_of_fn_from_ty(ccx, fn_tpt.ty); + let llptrty = T_ptr(llty); + if val_ty(val) != llptrty { + val = BitCast(bcx, val, llptrty); + } + return FnData {llfn: val}; } @@ -449,7 +480,7 @@ pub fn body_contains_ret(body: &ast::blk) -> bool { // See [Note-arg-mode] pub fn trans_call_inner(in_cx: block, call_info: Option, - fn_expr_ty: ty::t, + callee_ty: ty::t, ret_ty: ty::t, get_callee: &fn(block) -> Callee, args: CallArgs, @@ -506,79 +537,103 @@ pub fn trans_call_inner(in_cx: block, } }; - let llretslot = trans_ret_slot(bcx, fn_expr_ty, dest); - - let mut llargs = ~[]; + let abi = match ty::get(callee_ty).sty { + ty::ty_bare_fn(ref f) => f.abis, + _ => AbiSet::Rust() + }; + let is_rust_fn = + abi.is_rust() || + abi.is_intrinsic(); + + // Generate a location to store the result. If the user does + // not care about the result, just make a stack slot. + let llretslot = match dest { + expr::SaveIn(dst) => dst, + expr::Ignore => { + if ty::type_is_voidish(ret_ty) { + unsafe { + llvm::LLVMGetUndef(T_ptr(T_nil())) + } + } else { + alloc_ty(bcx, ret_ty) + } + } + }; - if ty::type_is_immediate(ret_ty) { - unsafe { - llargs.push(llvm::LLVMGetUndef(T_ptr(T_i8()))); + // The code below invokes the function, using either the Rust + // conventions (if it is a rust fn) or the native conventions + // (otherwise). The important part is that, when all is sad + // and done, the return value of the function will have been + // written in llretslot. + if is_rust_fn { + let mut llargs = ~[]; + // Push the out-pointer if we use an out-pointer for this + // return type, otherwise push "undef". + if type_of::return_uses_outptr(ret_ty) { + llargs.push(llretslot); + } else { + unsafe { + llargs.push(llvm::LLVMGetUndef(T_ptr(T_i8()))); + } } - } else { - llargs.push(llretslot); - } - llargs.push(llenv); - bcx = trans_args(bcx, args, fn_expr_ty, - ret_flag, autoref_arg, &mut llargs); + // Push the environment. + llargs.push(llenv); + // Push the arguments. + bcx = trans_args(bcx, args, callee_ty, + ret_flag, autoref_arg, &mut llargs); - // Now that the arguments have finished evaluating, we need to revoke - // the cleanup for the self argument, if it exists - match callee.data { - Method(d) if d.self_mode == ty::ByCopy => { - revoke_clean(bcx, d.llself); + // Now that the arguments have finished evaluating, we need to revoke + // the cleanup for the self argument, if it exists + match callee.data { + Method(d) if d.self_mode == ty::ByCopy => { + revoke_clean(bcx, d.llself); + } + _ => {} } - _ => {} - } - // Uncomment this to debug calls. - /* - io::println(fmt!("calling: %s", bcx.val_str(llfn))); - for llargs.each |llarg| { - io::println(fmt!("arg: %s", bcx.val_str(*llarg))); - } - io::println("---"); - */ + // Invoke the actual rust fn and update bcx/llresult. + let (llret, b) = base::invoke(bcx, llfn, llargs); + bcx = b; - // If the block is terminated, then one or more of the args - // has type _|_. Since that means it diverges, the code for - // the call itself is unreachable. - let (llresult, new_bcx) = base::invoke(bcx, llfn, llargs); - bcx = new_bcx; + // If the Rust convention for this type is return via + // the return value, copy it into llretslot. + if !type_of::return_uses_outptr(ret_ty) && !ty::type_is_voidish(ret_ty) { + Store(bcx, llret, llretslot); + } + } else { + let mut llargs = ~[]; + bcx = trans_args(bcx, args, callee_ty, + ret_flag, autoref_arg, &mut llargs); + bcx = foreign::trans_native_call(bcx, callee_ty, + llfn, llretslot, llargs); + } + // If the caller doesn't care about the result of this fn call, + // drop the temporary slot we made. match dest { expr::Ignore => { // drop the value if it is not being saved. unsafe { if llvm::LLVMIsUndef(llretslot) != lib::llvm::True { - if ty::type_is_nil(ret_ty) { + if ty::type_is_voidish(ret_ty) { // When implementing the for-loop sugar syntax, the // type of the for-loop is nil, but the function // it's invoking returns a bool. This is a special // case to ignore instead of invoking the Store // below into a scratch pointer of a mismatched // type. - } else if ty::type_is_immediate(ret_ty) { - let llscratchptr = alloc_ty(bcx, ret_ty); - Store(bcx, llresult, llscratchptr); - bcx = glue::drop_ty(bcx, llscratchptr, ret_ty); } else { bcx = glue::drop_ty(bcx, llretslot, ret_ty); } } } } - expr::SaveIn(lldest) => { - // If this is an immediate, store into the result location. - // (If this was not an immediate, the result will already be - // directly written into the output slot.) - if ty::type_is_immediate(ret_ty) { - Store(bcx, llresult, lldest); - } - } + expr::SaveIn(_) => { } } + // Do some other scary stuff that I don't want to read or comment. -nmatsakis if ty::type_is_bot(ret_ty) { Unreachable(bcx); } else if ret_in_loop { @@ -597,30 +652,11 @@ pub fn trans_call_inner(in_cx: block, } } - pub enum CallArgs<'self> { ArgExprs(&'self [@ast::expr]), ArgVals(&'self [ValueRef]) } -pub fn trans_ret_slot(bcx: block, fn_ty: ty::t, dest: expr::Dest) - -> ValueRef { - let retty = ty::ty_fn_ret(fn_ty); - - match dest { - expr::SaveIn(dst) => dst, - expr::Ignore => { - if ty::type_is_nil(retty) { - unsafe { - llvm::LLVMGetUndef(T_ptr(T_nil())) - } - } else { - alloc_ty(bcx, retty) - } - } - } -} - pub fn trans_args(cx: block, args: CallArgs, fn_ty: ty::t, @@ -787,7 +823,7 @@ pub fn trans_arg_expr(bcx: block, if formal_arg_ty != arg_datum.ty { // this could happen due to e.g. subtyping - let llformal_arg_ty = type_of::type_of_explicit_arg(ccx, &formal_arg_ty); + let llformal_arg_ty = type_of::type_of_explicit_arg(ccx, formal_arg_ty); let llformal_arg_ty = match self_mode { ty::ByRef => T_ptr(llformal_arg_ty), ty::ByCopy => llformal_arg_ty, diff --git a/src/librustc/middle/trans/common.rs b/src/librustc/middle/trans/common.rs index be074cfc57a30..c7e713eab02d2 100644 --- a/src/librustc/middle/trans/common.rs +++ b/src/librustc/middle/trans/common.rs @@ -149,7 +149,7 @@ pub fn BuilderRef_res(B: BuilderRef) -> BuilderRef_res { } } -pub type ExternMap = @mut HashMap<@str, ValueRef>; +pub type ExternMap = @mut HashMap<~str, ValueRef>; // Crate context. Every crate we compile has one of these. pub struct CrateContext { @@ -318,10 +318,10 @@ pub struct fn_ctxt_ { // for that (flagptr, retptr) loop_ret: Option<(ValueRef, ValueRef)>, - // True if this function has an immediate return value, false otherwise. - // If this is false, the llretptr will alias the first argument of the - // function. - has_immediate_return_value: bool, + // True if the caller expects this fn to use the out pointer to + // return. Either way, your code should write into llretptr, but if + // this value is false, llretptr will be a local alloca. + caller_expects_out_pointer: bool, // Maps arguments to allocas created for them in llallocas. llargs: @mut HashMap, diff --git a/src/librustc/middle/trans/datum.rs b/src/librustc/middle/trans/datum.rs index 6be5dbe1beb6c..b070c0bb7c191 100644 --- a/src/librustc/middle/trans/datum.rs +++ b/src/librustc/middle/trans/datum.rs @@ -183,14 +183,14 @@ pub fn immediate_rvalue_bcx(bcx: block, pub fn scratch_datum(bcx: block, ty: ty::t, zero: bool) -> Datum { /*! - * * Allocates temporary space on the stack using alloca() and * returns a by-ref Datum pointing to it. If `zero` is true, the * space will be zeroed when it is allocated; this is normally not * necessary, but in the case of automatic rooting in match * statements it is possible to have temporaries that may not get * initialized if a certain arm is not taken, so we must zero - * them. You must arrange any cleanups etc yourself! */ + * them. You must arrange any cleanups etc yourself! + */ let llty = type_of::type_of(bcx.ccx(), ty); let scratch = alloca_maybe_zeroed(bcx, llty, zero); @@ -199,12 +199,12 @@ pub fn scratch_datum(bcx: block, ty: ty::t, zero: bool) -> Datum { pub fn appropriate_mode(ty: ty::t) -> DatumMode { /*! - * - * Indicates the "appropriate" mode for this value, - * which is either by ref or by value, depending - * on whether type is immediate or not. */ + * Indicates the "appropriate" mode for this value, + * which is either by ref or by value, depending + * on whether type is immediate or not. + */ - if ty::type_is_nil(ty) || ty::type_is_bot(ty) { + if ty::type_is_voidish(ty) { ByValue } else if ty::type_is_immediate(ty) { ByValue @@ -272,7 +272,7 @@ pub impl Datum { let _icx = bcx.insn_ctxt("copy_to"); - if ty::type_is_nil(self.ty) || ty::type_is_bot(self.ty) { + if ty::type_is_voidish(self.ty) { return bcx; } @@ -342,7 +342,7 @@ pub impl Datum { debug!("move_to(self=%s, action=%?, dst=%s)", self.to_str(bcx.ccx()), action, bcx.val_str(dst)); - if ty::type_is_nil(self.ty) || ty::type_is_bot(self.ty) { + if ty::type_is_voidish(self.ty) { return bcx; } @@ -428,7 +428,7 @@ pub impl Datum { * * Yields the value itself. */ - if ty::type_is_nil(self.ty) || ty::type_is_bot(self.ty) { + if ty::type_is_voidish(self.ty) { C_nil() } else { match self.mode { @@ -465,7 +465,7 @@ pub impl Datum { match self.mode { ByRef => self.val, ByValue => { - if ty::type_is_nil(self.ty) || ty::type_is_bot(self.ty) { + if ty::type_is_voidish(self.ty) { C_null(T_ptr(type_of::type_of(bcx.ccx(), self.ty))) } else { let slot = alloc_ty(bcx, self.ty); diff --git a/src/librustc/middle/trans/expr.rs b/src/librustc/middle/trans/expr.rs index d581d8043f8e4..abb9727154e39 100644 --- a/src/librustc/middle/trans/expr.rs +++ b/src/librustc/middle/trans/expr.rs @@ -292,7 +292,7 @@ pub fn trans_to_datum(bcx: block, expr: @ast::expr) -> DatumBlock { assert_eq!(datum.appropriate_mode(), ByValue); Store(bcx, datum.to_appropriate_llval(bcx), llfn); let llenv = GEPi(bcx, scratch.val, [0u, abi::fn_field_box]); - Store(bcx, base::null_env_ptr(bcx), llenv); + Store(bcx, base::null_env_ptr(bcx.ccx()), llenv); DatumBlock {bcx: bcx, datum: scratch} } @@ -326,7 +326,7 @@ pub fn trans_into(bcx: block, expr: @ast::expr, dest: Dest) -> block { debuginfo::update_source_pos(bcx, expr.span); let dest = { - if ty::type_is_nil(ty) || ty::type_is_bot(ty) { + if ty::type_is_voidish(ty) { Ignore } else { dest @@ -416,7 +416,7 @@ fn trans_to_datum_unadjusted(bcx: block, expr: @ast::expr) -> DatumBlock { ty::RvalueDpsExpr => { let ty = expr_ty(bcx, expr); - if ty::type_is_nil(ty) || ty::type_is_bot(ty) { + if ty::type_is_voidish(ty) { bcx = trans_rvalue_dps_unadjusted(bcx, expr, Ignore); return nil(bcx, ty); } else { diff --git a/src/librustc/middle/trans/foreign.rs b/src/librustc/middle/trans/foreign.rs index b0560c40277b8..c8c6601afae07 100644 --- a/src/librustc/middle/trans/foreign.rs +++ b/src/librustc/middle/trans/foreign.rs @@ -10,281 +10,88 @@ use core::prelude::*; -use back::{link, abi}; -use lib::llvm::{SequentiallyConsistent, Acquire, Release, Xchg}; -use lib::llvm::{TypeRef, ValueRef}; +use back::{link}; +use core::libc::c_uint; +use lib::llvm::{TypeRef, ValueRef, Attribute}; +use lib::llvm::llvm; use lib; -use middle::trans::base::*; +use middle::trans::machine; +use middle::trans::base; +use middle::trans::base::get_insn_ctxt; use middle::trans::cabi; use middle::trans::cabi_x86; use middle::trans::cabi_x86_64; use middle::trans::cabi_arm; use middle::trans::cabi_mips; use middle::trans::build::*; -use middle::trans::callee::*; use middle::trans::common::*; -use middle::trans::datum::*; -use middle::trans::expr::Ignore; -use middle::trans::machine::llsize_of; -use middle::trans::glue; -use middle::trans::machine; use middle::trans::type_of::*; use middle::trans::type_of; use middle::ty; -use middle::ty::FnSig; -use util::ppaux::ty_to_str; - use syntax::codemap::span; -use syntax::{ast, ast_util}; +use syntax::{ast}; use syntax::{attr, ast_map}; -use syntax::opt_vec; use syntax::parse::token::special_idents; use syntax::abi::{X86, X86_64, Arm, Mips}; use syntax::abi::{RustIntrinsic, Rust, Stdcall, Fastcall, - Cdecl, Aapcs, C}; + Cdecl, Aapcs, C, AbiSet}; +use util::ppaux::{Repr}; -fn abi_info(ccx: @CrateContext) -> @cabi::ABIInfo { - return match ccx.sess.targ_cfg.arch { - X86 => cabi_x86::abi_info(ccx), - X86_64 => cabi_x86_64::abi_info(), - Arm => cabi_arm::abi_info(), - Mips => cabi_mips::abi_info(), - } -} +/////////////////////////////////////////////////////////////////////////// +// Type definitions -pub fn link_name(ccx: @CrateContext, i: @ast::foreign_item) -> @~str { - match attr::first_attr_value_str_by_name(i.attrs, "link_name") { - None => ccx.sess.str_of(i.ident), - Some(ln) => ln, - } -} - -struct ShimTypes { +struct ForeignTypes { + /// Rust signature of the function fn_sig: ty::FnSig, + /// Adapter object for handling native ABI rules (trust me, you + /// don't want to know) + fn_ty: cabi::FnType, + /// LLVM types that will appear on the foreign function llsig: LlvmSignature, /// True if there is a return value (not bottom, not unit) ret_def: bool, - - /// Type of the struct we will use to shuttle values back and forth. - /// This is always derived from the llsig. - bundle_ty: TypeRef, - - /// Type of the shim function itself. - shim_fn_ty: TypeRef, - - /// Adapter object for handling native ABI rules (trust me, you - /// don't want to know). - fn_ty: cabi::FnType } struct LlvmSignature { + // LLVM versions of the types of this function's arguments. llarg_tys: ~[TypeRef], - llret_ty: TypeRef, - sret: bool, -} -fn foreign_signature(ccx: @CrateContext, fn_sig: &ty::FnSig) - -> LlvmSignature { - /*! - * The ForeignSignature is the LLVM types of the arguments/return type - * of a function. Note that these LLVM types are not quite the same - * as the LLVM types would be for a native Rust function because foreign - * functions just plain ignore modes. They also don't pass aggregate - * values by pointer like we do. - */ - - let llarg_tys = fn_sig.inputs.map(|arg_ty| type_of(ccx, *arg_ty)); - let llret_ty = type_of::type_of(ccx, fn_sig.output); - LlvmSignature { - llarg_tys: llarg_tys, - llret_ty: llret_ty, - sret: !ty::type_is_immediate(fn_sig.output), - } -} + // LLVM version of the type that this function returns. Note that + // this *may not be* the declared return type of the foreign + // function, because the foreign function may opt to return via an + // out pointer. + llret_ty: TypeRef, -fn shim_types(ccx: @CrateContext, id: ast::node_id) -> ShimTypes { - let fn_sig = match ty::get(ty::node_id_to_type(ccx.tcx, id)).sty { - ty::ty_bare_fn(ref fn_ty) => copy fn_ty.sig, - _ => ccx.sess.bug("c_arg_and_ret_lltys called on non-function type") - }; - let llsig = foreign_signature(ccx, &fn_sig); - let bundle_ty = T_struct(vec::append_one(copy llsig.llarg_tys, - T_ptr(llsig.llret_ty)), - false); - let ret_def = !ty::type_is_bot(fn_sig.output) && - !ty::type_is_nil(fn_sig.output); - let fn_ty = abi_info(ccx).compute_info(llsig.llarg_tys, - llsig.llret_ty, - ret_def); - ShimTypes { - fn_sig: fn_sig, - llsig: llsig, - ret_def: ret_def, - bundle_ty: bundle_ty, - shim_fn_ty: T_fn([T_ptr(bundle_ty)], T_nil()), - fn_ty: fn_ty - } + // True if *Rust* would use an outpointer for this function. + sret: bool, } -type shim_arg_builder<'self> = - &'self fn(bcx: block, tys: &ShimTypes, - llargbundle: ValueRef) -> ~[ValueRef]; - -type shim_ret_builder<'self> = - &'self fn(bcx: block, tys: &ShimTypes, - llargbundle: ValueRef, - llretval: ValueRef); - -fn build_shim_fn_(ccx: @CrateContext, - shim_name: ~str, - llbasefn: ValueRef, - tys: &ShimTypes, - cc: lib::llvm::CallConv, - arg_builder: shim_arg_builder, - ret_builder: shim_ret_builder) - -> ValueRef { - let llshimfn = decl_internal_cdecl_fn( - ccx.llmod, shim_name, tys.shim_fn_ty); - - // Declare the body of the shim function: - let fcx = new_fn_ctxt(ccx, ~[], llshimfn, tys.fn_sig.output, None); - let bcx = top_scope_block(fcx, None); - let lltop = bcx.llbb; - let llargbundle = get_param(llshimfn, 0u); - let llargvals = arg_builder(bcx, tys, llargbundle); - - // Create the call itself and store the return value: - let llretval = CallWithConv(bcx, llbasefn, llargvals, cc); - - ret_builder(bcx, tys, llargbundle, llretval); - - // Don't finish up the function in the usual way, because this doesn't - // follow the normal Rust calling conventions. - tie_up_header_blocks(fcx, lltop); - - let ret_cx = raw_block(fcx, false, fcx.llreturn); - Ret(ret_cx, C_null(T_nil())); - - return llshimfn; -} -type wrap_arg_builder<'self> = &'self fn(bcx: block, - tys: &ShimTypes, - llwrapfn: ValueRef, - llargbundle: ValueRef); - -type wrap_ret_builder<'self> = &'self fn(bcx: block, - tys: &ShimTypes, - llargbundle: ValueRef); - -fn build_wrap_fn_(ccx: @CrateContext, - tys: &ShimTypes, - llshimfn: ValueRef, - llwrapfn: ValueRef, - shim_upcall: ValueRef, - needs_c_return: bool, - arg_builder: wrap_arg_builder, - ret_builder: wrap_ret_builder) { - let _icx = ccx.insn_ctxt("foreign::build_wrap_fn_"); - let fcx = new_fn_ctxt(ccx, ~[], llwrapfn, tys.fn_sig.output, None); - - // Patch up the return type if it's not immediate and we're returning via - // the C ABI. - if needs_c_return && !ty::type_is_immediate(tys.fn_sig.output) { - let lloutputtype = type_of::type_of(*fcx.ccx, tys.fn_sig.output); - fcx.llretptr = Some(alloca(raw_block(fcx, false, fcx.llstaticallocas), - lloutputtype)); - } +/////////////////////////////////////////////////////////////////////////// +// Calls to external functions - let bcx = top_scope_block(fcx, None); - let lltop = bcx.llbb; - - // Allocate the struct and write the arguments into it. - let llargbundle = alloca(bcx, tys.bundle_ty); - arg_builder(bcx, tys, llwrapfn, llargbundle); - - // Create call itself. - let llshimfnptr = PointerCast(bcx, llshimfn, T_ptr(T_i8())); - let llrawargbundle = PointerCast(bcx, llargbundle, T_ptr(T_i8())); - Call(bcx, shim_upcall, [llrawargbundle, llshimfnptr]); - ret_builder(bcx, tys, llargbundle); - - // Perform a custom version of `finish_fn`. First, tie up the header - // blocks. - tie_up_header_blocks(fcx, lltop); - - // Then return according to the C ABI. - unsafe { - let return_context = raw_block(fcx, false, fcx.llreturn); - - let llfunctiontype = val_ty(llwrapfn); - let llfunctiontype = - ::lib::llvm::llvm::LLVMGetElementType(llfunctiontype); - let llfunctionreturntype = - ::lib::llvm::llvm::LLVMGetReturnType(llfunctiontype); - if ::lib::llvm::llvm::LLVMGetTypeKind(llfunctionreturntype) == - ::lib::llvm::Void { - // XXX: This might be wrong if there are any functions for which - // the C ABI specifies a void output pointer and the Rust ABI - // does not. - RetVoid(return_context); - } else { - // Cast if we have to... - // XXX: This is ugly. - let llretptr = BitCast(return_context, - fcx.llretptr.get(), - T_ptr(llfunctionreturntype)); - Ret(return_context, Load(return_context, llretptr)); - } - } -} +pub fn register_foreign_item_fn(ccx: @CrateContext, + abis: AbiSet, + path: &ast_map::path, + foreign_item: @ast::foreign_item) -> ValueRef { + /*! + * Registers a foreign function found in a library. + * Just adds a LLVM global. + */ -// For each foreign function F, we generate a wrapper function W and a shim -// function S that all work together. The wrapper function W is the function -// that other rust code actually invokes. Its job is to marshall the -// arguments into a struct. It then uses a small bit of assembly to switch -// over to the C stack and invoke the shim function. The shim function S then -// unpacks the arguments from the struct and invokes the actual function F -// according to its specified calling convention. -// -// Example: Given a foreign c-stack function F(x: X, y: Y) -> Z, -// we generate a wrapper function W that looks like: -// -// void W(Z* dest, void *env, X x, Y y) { -// struct { X x; Y y; Z *z; } args = { x, y, z }; -// call_on_c_stack_shim(S, &args); -// } -// -// The shim function S then looks something like: -// -// void S(struct { X x; Y y; Z *z; } *args) { -// *args->z = F(args->x, args->y); -// } -// -// However, if the return type of F is dynamically sized or of aggregate type, -// the shim function looks like: -// -// void S(struct { X x; Y y; Z *z; } *args) { -// F(args->z, args->x, args->y); -// } -// -// Note: on i386, the layout of the args struct is generally the same -// as the desired layout of the arguments on the C stack. Therefore, -// we could use upcall_alloc_c_stack() to allocate the `args` -// structure and switch the stack pointer appropriately to avoid a -// round of copies. (In fact, the shim function itself is -// unnecessary). We used to do this, in fact, and will perhaps do so -// in the future. -pub fn trans_foreign_mod(ccx: @CrateContext, - path: &ast_map::path, - foreign_mod: &ast::foreign_mod) { - let _icx = ccx.insn_ctxt("foreign::trans_foreign_mod"); + debug!("register_foreign_item_fn(abis=%s, \ + path=%s, \ + foreign_item.id=%?)", + abis.repr(ccx.tcx), + path.repr(ccx.tcx), + foreign_item.id); + // Determine appropriate LLVM calling convention constant let arch = ccx.sess.targ_cfg.arch; - let abi = match foreign_mod.abis.for_arch(arch) { + let cc = match abis.for_arch(arch) { None => { ccx.sess.fatal( fmt!("No suitable ABI for target architecture \ @@ -293,1108 +100,622 @@ pub fn trans_foreign_mod(ccx: @CrateContext, ccx.sess.intr()))); } - Some(abi) => abi, - }; + Some(RustIntrinsic) => { + // Intrinsics are emitted by monomorphic fn + ccx.sess.bug( + fmt!("Asked to register intrinsic fn %s", + ast_map::path_to_str(*path, + ccx.sess.intr()))); + } - for foreign_mod.items.each |&foreign_item| { - match foreign_item.node { - ast::foreign_item_fn(*) => { - let id = foreign_item.id; - match abi { - RustIntrinsic => { - // Intrinsics are emitted by monomorphic fn - } - - Rust => { - // FIXME(#3678) Implement linking to foreign fns with Rust ABI - ccx.sess.unimpl( - fmt!("Foreign functions with Rust ABI")); - } - - Stdcall => { - build_foreign_fn(ccx, id, foreign_item, - lib::llvm::X86StdcallCallConv); - } - - Fastcall => { - build_foreign_fn(ccx, id, foreign_item, - lib::llvm::X86FastcallCallConv); - } - - Cdecl => { - // FIXME(#3678) should really be more specific - build_foreign_fn(ccx, id, foreign_item, - lib::llvm::CCallConv); - } - - Aapcs => { - // FIXME(#3678) should really be more specific - build_foreign_fn(ccx, id, foreign_item, - lib::llvm::CCallConv); - } - - C => { - build_foreign_fn(ccx, id, foreign_item, - lib::llvm::CCallConv); - } - } - } - ast::foreign_item_const(*) => { - let ident = ccx.sess.parse_sess.interner.get( - foreign_item.ident); - ccx.item_symbols.insert(foreign_item.id, copy *ident); - } + Some(Rust) => { + // FIXME(#3678) Implement linking to foreign fns with Rust ABI + ccx.sess.unimpl( + fmt!("Foreign functions with Rust ABI")); } - } - fn build_foreign_fn(ccx: @CrateContext, - id: ast::node_id, - foreign_item: @ast::foreign_item, - cc: lib::llvm::CallConv) { - let llwrapfn = get_item_val(ccx, id); - let tys = shim_types(ccx, id); - if attr::attrs_contains_name(foreign_item.attrs, "rust_stack") { - build_direct_fn(ccx, llwrapfn, foreign_item, - &tys, cc); - } else if attr::attrs_contains_name(foreign_item.attrs, "fast_ffi") { - build_fast_ffi_fn(ccx, llwrapfn, foreign_item, &tys, cc); + Some(Stdcall) => lib::llvm::X86StdcallCallConv, + Some(Fastcall) => lib::llvm::X86FastcallCallConv, + Some(C) => lib::llvm::CCallConv, + + // NOTE These API constants ought to be more specific + Some(Cdecl) => lib::llvm::CCallConv, + Some(Aapcs) => lib::llvm::CCallConv, + }; + + // Register the function as a C extern fn + let lname = link_name(ccx, foreign_item); + let tys = foreign_types_for_id(ccx, foreign_item.id); + + // Create the LLVM value for the C extern fn + let llfn_ty = lltype_for_fn_from_foreign_types(&tys); + let llfn = base::get_extern_fn(ccx.externs, ccx.llmod, + *lname, cc, llfn_ty); + add_argument_attributes(&tys, llfn); + + return llfn; +} + +pub fn trans_native_call(bcx: block, + callee_ty: ty::t, + llfn: ValueRef, + llretptr: ValueRef, + llargs_rust: &[ValueRef]) -> block { + /*! + * Prepares a call to a native function. This requires adapting + * from the Rust argument passing rules to the native rules. + * + * # Parameters + * + * - `callee_ty`: Rust type for the function we are calling + * - `llfn`: the function pointer we are calling + * - `llretptr`: where to store the return value of the function + * - `llargs_rust`: a list of the argument values, prepared + * as they would be if calling a Rust function + */ + + let ccx = bcx.ccx(); + let tcx = bcx.tcx(); + + debug!("trans_native_call(callee_ty=%s, \ + llfn=%s, \ + llretptr=%s)", + callee_ty.repr(tcx), + val_str(ccx.tn, llfn), + val_str(ccx.tn, llretptr)); + + // make sure that the native call will have sufficient stack + base::set_fixed_stack_segment(bcx.fcx.llfn); + + let abi_info = abi_info(ccx); + let fn_sig = match ty::get(callee_ty).sty { + ty::ty_bare_fn(ref fn_ty) => copy fn_ty.sig, + _ => ccx.sess.bug("trans_native_call called on non-function type") + }; + let llsig = foreign_signature(ccx, &fn_sig); + let ret_def = !ty::type_is_voidish(fn_sig.output); + let fn_type = abi_info.compute_info(llsig.llarg_tys, + llsig.llret_ty, + ret_def); + + let all_arg_tys: &[cabi::LLVMType] = fn_type.arg_tys; + let all_attributes: &[Option] = fn_type.attrs; + + let mut llargs_foreign = ~[]; + + // If the foreign ABI expects return value by pointer, supply the + // pointer that Rust gave us. Sometimes we have to bitcast + // because foreign fns return slightly different (but equivalent) + // views on the same type (e.g., i64 in place of {i32,i32}). + let (arg_tys, attributes) = { + if fn_type.sret { + if all_arg_tys[0].cast { + let llcastedretptr = + BitCast(bcx, llretptr, T_ptr(all_arg_tys[0].ty)); + llargs_foreign.push(llcastedretptr); + } else { + llargs_foreign.push(llretptr); + } + (all_arg_tys.tail(), all_attributes.tail()) } else { - let llshimfn = build_shim_fn(ccx, foreign_item, &tys, cc); - build_wrap_fn(ccx, &tys, llshimfn, llwrapfn); + (all_arg_tys, all_attributes) } - } + }; - fn build_shim_fn(ccx: @CrateContext, - foreign_item: @ast::foreign_item, - tys: &ShimTypes, - cc: lib::llvm::CallConv) - -> ValueRef { - /*! - * - * Build S, from comment above: - * - * void S(struct { X x; Y y; Z *z; } *args) { - * F(args->z, args->x, args->y); - * } - */ - - let _icx = ccx.insn_ctxt("foreign::build_shim_fn"); - - fn build_args(bcx: block, tys: &ShimTypes, llargbundle: ValueRef) - -> ~[ValueRef] { - let _icx = bcx.insn_ctxt("foreign::shim::build_args"); - tys.fn_ty.build_shim_args(bcx, tys.llsig.llarg_tys, llargbundle) - } + for llargs_rust.eachi |i, &llarg_rust| { + let mut llarg_rust = llarg_rust; - fn build_ret(bcx: block, - tys: &ShimTypes, - llargbundle: ValueRef, - llretval: ValueRef) { - let _icx = bcx.insn_ctxt("foreign::shim::build_ret"); - tys.fn_ty.build_shim_ret(bcx, - tys.llsig.llarg_tys, - tys.ret_def, - llargbundle, - llretval); - build_return(bcx); - } + // Does Rust pass this argument by pointer? + let rust_indirect = type_of::arg_is_indirect(ccx, fn_sig.inputs[i]); - let lname = link_name(ccx, foreign_item); - let llbasefn = base_fn(ccx, *lname, tys, cc); - // Name the shim function - let shim_name = *lname + "__c_stack_shim"; - build_shim_fn_(ccx, - shim_name, - llbasefn, - tys, - cc, - build_args, - build_ret) - } + debug!("argument %u, llarg_rust=%s, rust_indirect=%b, arg_ty=%s", + i, + val_str(ccx.tn, llarg_rust), + rust_indirect, + ty_str(ccx.tn, arg_tys[i].ty)); - fn base_fn(ccx: @CrateContext, - lname: &str, - tys: &ShimTypes, - cc: lib::llvm::CallConv) - -> ValueRef { - // Declare the "prototype" for the base function F: - do tys.fn_ty.decl_fn |fnty| { - decl_fn(ccx.llmod, lname, cc, fnty) + // Ensure that we always have the Rust value indirectly, + // because it makes bitcasting easier. + if !rust_indirect { + let scratch = base::alloca(bcx, arg_tys[i].ty); + Store(bcx, llarg_rust, scratch); + llarg_rust = scratch; } - } - // FIXME (#2535): this is very shaky and probably gets ABIs wrong all - // over the place - fn build_direct_fn(ccx: @CrateContext, - decl: ValueRef, - item: @ast::foreign_item, - tys: &ShimTypes, - cc: lib::llvm::CallConv) { - debug!("build_direct_fn(%s)", *link_name(ccx, item)); - - let fcx = new_fn_ctxt(ccx, ~[], decl, tys.fn_sig.output, None); - let bcx = top_scope_block(fcx, None), lltop = bcx.llbb; - let llbasefn = base_fn(ccx, *link_name(ccx, item), tys, cc); - let ty = ty::lookup_item_type(ccx.tcx, - ast_util::local_def(item.id)).ty; - let args = vec::from_fn(ty::ty_fn_args(ty).len(), |i| { - get_param(decl, i + first_real_arg) - }); - let retval = Call(bcx, llbasefn, args); - let ret_ty = ty::ty_fn_ret(ty); - if !ty::type_is_nil(ret_ty) && !ty::type_is_bot(ret_ty) { - Store(bcx, retval, fcx.llretptr.get()); - } - build_return(bcx); - finish_fn(fcx, lltop); - } + debug!("llarg_rust=%s (after indirection)", + val_str(ccx.tn, llarg_rust)); - // FIXME (#2535): this is very shaky and probably gets ABIs wrong all - // over the place - fn build_fast_ffi_fn(ccx: @CrateContext, - decl: ValueRef, - item: @ast::foreign_item, - tys: &ShimTypes, - cc: lib::llvm::CallConv) { - debug!("build_fast_ffi_fn(%s)", *link_name(ccx, item)); - - let fcx = new_fn_ctxt(ccx, ~[], decl, tys.fn_sig.output, None); - let bcx = top_scope_block(fcx, None), lltop = bcx.llbb; - let llbasefn = base_fn(ccx, *link_name(ccx, item), tys, cc); - set_no_inline(fcx.llfn); - set_fixed_stack_segment(fcx.llfn); - let ty = ty::lookup_item_type(ccx.tcx, - ast_util::local_def(item.id)).ty; - let args = vec::from_fn(ty::ty_fn_args(ty).len(), |i| { - get_param(decl, i + first_real_arg) - }); - let retval = Call(bcx, llbasefn, args); - let ret_ty = ty::ty_fn_ret(ty); - if !ty::type_is_nil(ret_ty) && !ty::type_is_bot(ret_ty) { - Store(bcx, retval, fcx.llretptr.get()); + // Check whether we need to do any casting + let foreignarg_ty = arg_tys[i].ty; + if arg_tys[i].cast { + llarg_rust = BitCast(bcx, llarg_rust, T_ptr(foreignarg_ty)); } - build_return(bcx); - finish_fn(fcx, lltop); + + debug!("llarg_rust=%s (after casting)", + val_str(ccx.tn, llarg_rust)); + + // Finally, load the value if needed for the foreign ABI + let foreign_indirect = attributes[i].is_some(); + let llarg_foreign = if foreign_indirect { + llarg_rust + } else { + Load(bcx, llarg_rust) + }; + + debug!("argument %u, llarg_foreign=%s", + i, val_str(ccx.tn, llarg_foreign)); + + llargs_foreign.push(llarg_foreign); } - fn build_wrap_fn(ccx: @CrateContext, - tys: &ShimTypes, - llshimfn: ValueRef, - llwrapfn: ValueRef) { - /*! - * - * Build W, from comment above: - * - * void W(Z* dest, void *env, X x, Y y) { - * struct { X x; Y y; Z *z; } args = { x, y, z }; - * call_on_c_stack_shim(S, &args); - * } - * - * One thing we have to be very careful of is to - * account for the Rust modes. - */ - - let _icx = ccx.insn_ctxt("foreign::build_wrap_fn"); - - build_wrap_fn_(ccx, - tys, - llshimfn, - llwrapfn, - ccx.upcalls.call_shim_on_c_stack, - false, - build_args, - build_ret); - - fn build_args(bcx: block, - tys: &ShimTypes, - llwrapfn: ValueRef, - llargbundle: ValueRef) { - let _icx = bcx.insn_ctxt("foreign::wrap::build_args"); - let ccx = bcx.ccx(); - let n = tys.llsig.llarg_tys.len(); - let implicit_args = first_real_arg; // return + env - for uint::range(0, n) |i| { - let mut llargval = get_param(llwrapfn, i + implicit_args); - - // In some cases, Rust will pass a pointer which the - // native C type doesn't have. In that case, just - // load the value from the pointer. - if type_of::arg_is_indirect(ccx, &tys.fn_sig.inputs[i]) { - llargval = Load(bcx, llargval); - } + let llforeign_retval = Call(bcx, llfn, llargs_foreign); - store_inbounds(bcx, llargval, llargbundle, [0u, i]); - } - let llretptr = bcx.fcx.llretptr.get(); - store_inbounds(bcx, llretptr, llargbundle, [0u, n]); - } + // If the function we just called does not use an outpointer, + // store the result into the rust outpointer. Cast the outpointer + // type to match because some ABIs will use a different type than + // the Rust type. e.g., a {u32,u32} struct could be returned as + // u64. + if ret_def && !fn_type.sret { + let llrust_ret_ty = llsig.llret_ty; + let llforeign_ret_ty = fn_type.ret_ty.ty; - fn build_ret(bcx: block, - shim_types: &ShimTypes, - llargbundle: ValueRef) { - let _icx = bcx.insn_ctxt("foreign::wrap::build_ret"); - let arg_count = shim_types.fn_sig.inputs.len(); - let llretptr = load_inbounds(bcx, llargbundle, [0, arg_count]); - Store(bcx, Load(bcx, llretptr), bcx.fcx.llretptr.get()); - build_return(bcx); + debug!("llretptr=%s", val_str(ccx.tn, llretptr)); + debug!("llforeign_retval=%s", val_str(ccx.tn, llforeign_retval)); + debug!("llrust_ret_ty=%s", ty_str(ccx.tn, llrust_ret_ty)); + debug!("llforeign_ret_ty=%s", ty_str(ccx.tn, llforeign_ret_ty)); + + if llrust_ret_ty == llforeign_ret_ty { + Store(bcx, llforeign_retval, llretptr); + } else { + // The actual return type is a struct, but the ABI + // adaptation code has cast it into some scalar type. The + // code that follows is the only reliable way I have + // found to do a transform like i64 -> {i32,i32}. + // Basically we dump the data onto the stack then memcpy it. + // + // Other approaches I tried: + // - Casting rust ret pointer to the foreign type and using Store + // is (a) unsafe if size of foreign type > size of rust type and + // (b) runs afoul of strict aliasing rules, yielding invalid + // assembly under -O (specifically, the store gets removed). + // - Truncating foreign type to correct integral type and then + // bitcasting to the struct type yields invalid cast errors. + let llscratch = base::alloca(bcx, llforeign_ret_ty); + Store(bcx, llforeign_retval, llscratch); + let llscratch_i8 = BitCast(bcx, llscratch, T_ptr(T_i8())); + let llretptr_i8 = BitCast(bcx, llretptr, T_ptr(T_i8())); + let llrust_size = machine::llsize_of_store(ccx, llrust_ret_ty); + let llforeign_align = machine::llalign_of_min(ccx, llforeign_ret_ty); + let llrust_align = machine::llalign_of_min(ccx, llrust_ret_ty); + let llalign = uint::min(llforeign_align, llrust_align); + debug!("llrust_size=%?", llrust_size); + base::call_memcpy(bcx, llretptr_i8, llscratch_i8, + C_uint(ccx, llrust_size), llalign as u32); } } + + return bcx; } -pub fn trans_intrinsic(ccx: @CrateContext, - decl: ValueRef, - item: @ast::foreign_item, - path: ast_map::path, - substs: @param_substs, - attributes: &[ast::attribute], - ref_id: Option) { - debug!("trans_intrinsic(item.ident=%s)", *ccx.sess.str_of(item.ident)); - - let output_type = ty::ty_fn_ret(ty::node_id_to_type(ccx.tcx, item.id)); - - let fcx = new_fn_ctxt_w_id(ccx, - path, - decl, - item.id, - output_type, - None, - Some(substs), - Some(item.span)); - - // Set the fixed stack segment flag if necessary. - if attr::attrs_contains_name(attributes, "fixed_stack_segment") { - set_fixed_stack_segment(fcx.llfn); +pub fn trans_foreign_mod(ccx: @CrateContext, + foreign_mod: &ast::foreign_mod) { + let _icx = ccx.insn_ctxt("foreign::trans_foreign_mod"); + for vec::each(foreign_mod.items) |&foreign_item| { + let lname = link_name(ccx, foreign_item); + ccx.item_symbols.insert(foreign_item.id, copy *lname); } +} - let mut bcx = top_scope_block(fcx, None); - let lltop = bcx.llbb; - match *ccx.sess.str_of(item.ident) { - ~"atomic_cxchg" => { - let old = AtomicCmpXchg(bcx, - get_param(decl, first_real_arg), - get_param(decl, first_real_arg + 1u), - get_param(decl, first_real_arg + 2u), - SequentiallyConsistent); - Store(bcx, old, fcx.llretptr.get()); - } - ~"atomic_cxchg_acq" => { - let old = AtomicCmpXchg(bcx, - get_param(decl, first_real_arg), - get_param(decl, first_real_arg + 1u), - get_param(decl, first_real_arg + 2u), - Acquire); - Store(bcx, old, fcx.llretptr.get()); - } - ~"atomic_cxchg_rel" => { - let old = AtomicCmpXchg(bcx, - get_param(decl, first_real_arg), - get_param(decl, first_real_arg + 1u), - get_param(decl, first_real_arg + 2u), - Release); - Store(bcx, old, fcx.llretptr.get()); - } - ~"atomic_load" => { - let old = AtomicLoad(bcx, - get_param(decl, first_real_arg), - SequentiallyConsistent); - Store(bcx, old, fcx.llretptr.get()); - } - ~"atomic_load_acq" => { - let old = AtomicLoad(bcx, - get_param(decl, first_real_arg), - Acquire); - Store(bcx, old, fcx.llretptr.get()); - } - ~"atomic_store" => { - AtomicStore(bcx, - get_param(decl, first_real_arg + 1u), - get_param(decl, first_real_arg), - SequentiallyConsistent); - } - ~"atomic_store_rel" => { - AtomicStore(bcx, - get_param(decl, first_real_arg + 1u), - get_param(decl, first_real_arg), - Release); - } - ~"atomic_xchg" => { - let old = AtomicRMW(bcx, Xchg, - get_param(decl, first_real_arg), - get_param(decl, first_real_arg + 1u), - SequentiallyConsistent); - Store(bcx, old, fcx.llretptr.get()); - } - ~"atomic_xchg_acq" => { - let old = AtomicRMW(bcx, Xchg, - get_param(decl, first_real_arg), - get_param(decl, first_real_arg + 1u), - Acquire); - Store(bcx, old, fcx.llretptr.get()); - } - ~"atomic_xchg_rel" => { - let old = AtomicRMW(bcx, Xchg, - get_param(decl, first_real_arg), - get_param(decl, first_real_arg + 1u), - Release); - Store(bcx, old, fcx.llretptr.get()); - } - ~"atomic_xadd" => { - let old = AtomicRMW(bcx, lib::llvm::Add, - get_param(decl, first_real_arg), - get_param(decl, first_real_arg + 1u), - SequentiallyConsistent); - Store(bcx, old, fcx.llretptr.get()); - } - ~"atomic_xadd_acq" => { - let old = AtomicRMW(bcx, lib::llvm::Add, - get_param(decl, first_real_arg), - get_param(decl, first_real_arg + 1u), - Acquire); - Store(bcx, old, fcx.llretptr.get()); - } - ~"atomic_xadd_rel" => { - let old = AtomicRMW(bcx, lib::llvm::Add, - get_param(decl, first_real_arg), - get_param(decl, first_real_arg + 1u), - Release); - Store(bcx, old, fcx.llretptr.get()); - } - ~"atomic_xsub" => { - let old = AtomicRMW(bcx, lib::llvm::Sub, - get_param(decl, first_real_arg), - get_param(decl, first_real_arg + 1u), - SequentiallyConsistent); - Store(bcx, old, fcx.llretptr.get()); - } - ~"atomic_xsub_acq" => { - let old = AtomicRMW(bcx, lib::llvm::Sub, - get_param(decl, first_real_arg), - get_param(decl, first_real_arg + 1u), - Acquire); - Store(bcx, old, fcx.llretptr.get()); - } - ~"atomic_xsub_rel" => { - let old = AtomicRMW(bcx, lib::llvm::Sub, - get_param(decl, first_real_arg), - get_param(decl, first_real_arg + 1u), - Release); - Store(bcx, old, fcx.llretptr.get()); - } - ~"size_of" => { - let tp_ty = substs.tys[0]; - let lltp_ty = type_of::type_of(ccx, tp_ty); - Store(bcx, C_uint(ccx, machine::llsize_of_real(ccx, lltp_ty)), - fcx.llretptr.get()); - } - ~"move_val" => { - // Create a datum reflecting the value being moved: - // - // - the datum will be by ref if the value is non-immediate; - // - // - the datum has a RevokeClean source because, that way, - // the `move_to()` method does not feel compelled to - // zero out the memory where the datum resides. Zeroing - // is not necessary since, for intrinsics, there is no - // cleanup to concern ourselves with. - let tp_ty = substs.tys[0]; - let mode = appropriate_mode(tp_ty); - let src = Datum {val: get_param(decl, first_real_arg + 1u), - ty: tp_ty, mode: mode, source: RevokeClean}; - bcx = src.move_to(bcx, DROP_EXISTING, - get_param(decl, first_real_arg)); - } - ~"move_val_init" => { - // See comments for `"move_val"`. - let tp_ty = substs.tys[0]; - let mode = appropriate_mode(tp_ty); - let src = Datum {val: get_param(decl, first_real_arg + 1u), - ty: tp_ty, mode: mode, source: RevokeClean}; - bcx = src.move_to(bcx, INIT, get_param(decl, first_real_arg)); - } - ~"min_align_of" => { - let tp_ty = substs.tys[0]; - let lltp_ty = type_of::type_of(ccx, tp_ty); - Store(bcx, C_uint(ccx, machine::llalign_of_min(ccx, lltp_ty)), - fcx.llretptr.get()); - } - ~"pref_align_of"=> { - let tp_ty = substs.tys[0]; - let lltp_ty = type_of::type_of(ccx, tp_ty); - Store(bcx, C_uint(ccx, machine::llalign_of_pref(ccx, lltp_ty)), - fcx.llretptr.get()); - } - ~"get_tydesc" => { - let tp_ty = substs.tys[0]; - let static_ti = get_tydesc(ccx, tp_ty); - glue::lazily_emit_all_tydesc_glue(ccx, static_ti); - - // FIXME (#3727): change this to T_ptr(ccx.tydesc_ty) when the - // core::sys copy of the get_tydesc interface dies off. - let td = PointerCast(bcx, static_ti.tydesc, T_ptr(T_nil())); - Store(bcx, td, fcx.llretptr.get()); - } - ~"init" => { - let tp_ty = substs.tys[0]; - let lltp_ty = type_of::type_of(ccx, tp_ty); - if !ty::type_is_nil(tp_ty) { - Store(bcx, C_null(lltp_ty), fcx.llretptr.get()); - } - } - ~"uninit" => { - // Do nothing, this is effectively a no-op - } - ~"forget" => {} - ~"transmute" => { - let (in_type, out_type) = (substs.tys[0], substs.tys[1]); - let llintype = type_of::type_of(ccx, in_type); - let llouttype = type_of::type_of(ccx, out_type); - - let in_type_size = machine::llbitsize_of_real(ccx, llintype); - let out_type_size = machine::llbitsize_of_real(ccx, llouttype); - if in_type_size != out_type_size { - let sp = match ccx.tcx.items.get_copy(&ref_id.get()) { - ast_map::node_expr(e) => e.span, - _ => fail!("transmute has non-expr arg"), - }; - let pluralize = |n| if 1u == n { "" } else { "s" }; - ccx.sess.span_fatal(sp, - fmt!("transmute called on types with \ - different sizes: %s (%u bit%s) to \ - %s (%u bit%s)", - ty_to_str(ccx.tcx, in_type), - in_type_size, - pluralize(in_type_size), - ty_to_str(ccx.tcx, out_type), - out_type_size, - pluralize(out_type_size))); - } +/////////////////////////////////////////////////////////////////////////// +// Rust functions with foreign ABIs +// +// These are normal Rust functions defined with foreign ABIs. For +// now, and perhaps forever, we translate these using a "layer of +// indirection". That is, given a Rust declaration like: +// +// extern "C" fn foo(i: u32) -> u32 { ... } +// +// we will generate a function like: +// +// S foo(T i) { +// S r; +// foo0(&r, NULL, i); +// return r; +// } +// +// #[inline_always] +// void foo0(uint32_t *r, void *env, uint32_t i) { ... } +// +// Here the (internal) `foo0` function follows the Rust ABI as normal, +// where the `foo` function follows the C ABI. We rely on LLVM to +// inline the one into the other. Of course we could just generate the +// correct code in the first place, but this is much simpler. + +pub fn register_rust_fn_with_foreign_abi(ccx: @CrateContext, + sp: span, + path: ast_map::path, + node_id: ast::node_id, + attrs: &[ast::attribute]) + -> ValueRef { + let _icx = ccx.insn_ctxt("foreign::register_foreign_fn"); - if !ty::type_is_nil(out_type) { - // NB: Do not use a Load and Store here. This causes massive - // code bloat when `transmute` is used on large structural - // types. - let lldestptr = fcx.llretptr.get(); - let lldestptr = PointerCast(bcx, lldestptr, T_ptr(T_i8())); - - let llsrcval = get_param(decl, first_real_arg); - let llsrcptr = if ty::type_is_immediate(in_type) { - let llsrcptr = alloca(bcx, llintype); - Store(bcx, llsrcval, llsrcptr); - llsrcptr - } else { - llsrcval - }; - let llsrcptr = PointerCast(bcx, llsrcptr, T_ptr(T_i8())); - - let llsize = llsize_of(ccx, llintype); - call_memcpy(bcx, lldestptr, llsrcptr, llsize, 1); - } - } - ~"needs_drop" => { - let tp_ty = substs.tys[0]; - Store(bcx, - C_bool(ty::type_needs_drop(ccx.tcx, tp_ty)), - fcx.llretptr.get()); - } - ~"visit_tydesc" => { - let td = get_param(decl, first_real_arg); - let visitor = get_param(decl, first_real_arg + 1u); - //let llvisitorptr = alloca(bcx, val_ty(visitor)); - //Store(bcx, visitor, llvisitorptr); - let td = PointerCast(bcx, td, T_ptr(ccx.tydesc_type)); - glue::call_tydesc_glue_full(bcx, - visitor, - td, - abi::tydesc_field_visit_glue, - None); - } - ~"frame_address" => { - let frameaddress = *ccx.intrinsics.get(&~"llvm.frameaddress"); - let frameaddress_val = Call(bcx, frameaddress, [C_i32(0i32)]); - let star_u8 = ty::mk_imm_ptr( - bcx.tcx(), - ty::mk_mach_uint(ast::ty_u8)); - let fty = ty::mk_closure(bcx.tcx(), ty::ClosureTy { - purity: ast::impure_fn, - sigil: ast::BorrowedSigil, - onceness: ast::Many, - region: ty::re_bound(ty::br_anon(0)), - bounds: ty::EmptyBuiltinBounds(), - sig: FnSig { - bound_lifetime_names: opt_vec::Empty, - inputs: ~[ star_u8 ], - output: ty::mk_nil() - } - }); - let datum = Datum {val: get_param(decl, first_real_arg), - mode: ByRef, ty: fty, source: ZeroMem}; - let arg_vals = ~[frameaddress_val]; - bcx = trans_call_inner( - bcx, None, fty, ty::mk_nil(), - |bcx| Callee {bcx: bcx, data: Closure(datum)}, - ArgVals(arg_vals), Ignore, DontAutorefArg); - } - ~"morestack_addr" => { - // XXX This is a hack to grab the address of this particular - // native function. There should be a general in-language - // way to do this - let llfty = type_of_fn(bcx.ccx(), [], ty::mk_nil()); - let morestack_addr = decl_cdecl_fn( - bcx.ccx().llmod, "__morestack", llfty); - let morestack_addr = PointerCast(bcx, morestack_addr, - T_ptr(T_nil())); - Store(bcx, morestack_addr, fcx.llretptr.get()); - } - ~"memcpy32" => { - let tp_ty = substs.tys[0]; - let lltp_ty = type_of::type_of(ccx, tp_ty); - let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32); - let size = C_i32(machine::llsize_of_real(ccx, lltp_ty) as i32); - - let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), T_ptr(T_i8())); - let src_ptr = PointerCast(bcx, get_param(decl, first_real_arg + 1), T_ptr(T_i8())); - let count = get_param(decl, first_real_arg + 2); - let volatile = C_i1(false); - let llfn = *bcx.ccx().intrinsics.get(&~"llvm.memcpy.p0i8.p0i8.i32"); - Call(bcx, llfn, [dst_ptr, src_ptr, Mul(bcx, size, count), align, volatile]); - } - ~"memcpy64" => { - let tp_ty = substs.tys[0]; - let lltp_ty = type_of::type_of(ccx, tp_ty); - let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32); - let size = C_i64(machine::llsize_of_real(ccx, lltp_ty) as i64); - - let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), T_ptr(T_i8())); - let src_ptr = PointerCast(bcx, get_param(decl, first_real_arg + 1), T_ptr(T_i8())); - let count = get_param(decl, first_real_arg + 2); - let volatile = C_i1(false); - let llfn = *bcx.ccx().intrinsics.get(&~"llvm.memcpy.p0i8.p0i8.i64"); - Call(bcx, llfn, [dst_ptr, src_ptr, Mul(bcx, size, count), align, volatile]); - } - ~"memmove32" => { - let tp_ty = substs.tys[0]; - let lltp_ty = type_of::type_of(ccx, tp_ty); - let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32); - let size = C_i32(machine::llsize_of_real(ccx, lltp_ty) as i32); - - let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), T_ptr(T_i8())); - let src_ptr = PointerCast(bcx, get_param(decl, first_real_arg + 1), T_ptr(T_i8())); - let count = get_param(decl, first_real_arg + 2); - let volatile = C_i1(false); - let llfn = *bcx.ccx().intrinsics.get(&~"llvm.memmove.p0i8.p0i8.i32"); - Call(bcx, llfn, [dst_ptr, src_ptr, Mul(bcx, size, count), align, volatile]); - } - ~"memmove64" => { - let tp_ty = substs.tys[0]; - let lltp_ty = type_of::type_of(ccx, tp_ty); - let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32); - let size = C_i64(machine::llsize_of_real(ccx, lltp_ty) as i64); - - let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), T_ptr(T_i8())); - let src_ptr = PointerCast(bcx, get_param(decl, first_real_arg + 1), T_ptr(T_i8())); - let count = get_param(decl, first_real_arg + 2); - let volatile = C_i1(false); - let llfn = *bcx.ccx().intrinsics.get(&~"llvm.memmove.p0i8.p0i8.i64"); - Call(bcx, llfn, [dst_ptr, src_ptr, Mul(bcx, size, count), align, volatile]); - } - ~"memset32" => { - let tp_ty = substs.tys[0]; - let lltp_ty = type_of::type_of(ccx, tp_ty); - let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32); - let size = C_i32(machine::llsize_of_real(ccx, lltp_ty) as i32); - - let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), T_ptr(T_i8())); - let val = get_param(decl, first_real_arg + 1); - let count = get_param(decl, first_real_arg + 2); - let volatile = C_i1(false); - let llfn = *bcx.ccx().intrinsics.get(&~"llvm.memset.p0i8.i32"); - Call(bcx, llfn, [dst_ptr, val, Mul(bcx, size, count), align, volatile]); - } - ~"memset64" => { - let tp_ty = substs.tys[0]; - let lltp_ty = type_of::type_of(ccx, tp_ty); - let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32); - let size = C_i64(machine::llsize_of_real(ccx, lltp_ty) as i64); - - let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), T_ptr(T_i8())); - let val = get_param(decl, first_real_arg + 1); - let count = get_param(decl, first_real_arg + 2); - let volatile = C_i1(false); - let llfn = *bcx.ccx().intrinsics.get(&~"llvm.memset.p0i8.i64"); - Call(bcx, llfn, [dst_ptr, val, Mul(bcx, size, count), align, volatile]); - } - ~"sqrtf32" => { - let x = get_param(decl, first_real_arg); - let sqrtf = *ccx.intrinsics.get(&~"llvm.sqrt.f32"); - Store(bcx, Call(bcx, sqrtf, [x]), fcx.llretptr.get()); - } - ~"sqrtf64" => { - let x = get_param(decl, first_real_arg); - let sqrtf = *ccx.intrinsics.get(&~"llvm.sqrt.f64"); - Store(bcx, Call(bcx, sqrtf, [x]), fcx.llretptr.get()); - } - ~"powif32" => { - let a = get_param(decl, first_real_arg); - let x = get_param(decl, first_real_arg + 1u); - let powif = *ccx.intrinsics.get(&~"llvm.powi.f32"); - Store(bcx, Call(bcx, powif, [a, x]), fcx.llretptr.get()); - } - ~"powif64" => { - let a = get_param(decl, first_real_arg); - let x = get_param(decl, first_real_arg + 1u); - let powif = *ccx.intrinsics.get(&~"llvm.powi.f64"); - Store(bcx, Call(bcx, powif, [a, x]), fcx.llretptr.get()); - } - ~"sinf32" => { - let x = get_param(decl, first_real_arg); - let sinf = *ccx.intrinsics.get(&~"llvm.sin.f32"); - Store(bcx, Call(bcx, sinf, [x]), fcx.llretptr.get()); - } - ~"sinf64" => { - let x = get_param(decl, first_real_arg); - let sinf = *ccx.intrinsics.get(&~"llvm.sin.f64"); - Store(bcx, Call(bcx, sinf, [x]), fcx.llretptr.get()); - } - ~"cosf32" => { - let x = get_param(decl, first_real_arg); - let cosf = *ccx.intrinsics.get(&~"llvm.cos.f32"); - Store(bcx, Call(bcx, cosf, [x]), fcx.llretptr.get()); - } - ~"cosf64" => { - let x = get_param(decl, first_real_arg); - let cosf = *ccx.intrinsics.get(&~"llvm.cos.f64"); - Store(bcx, Call(bcx, cosf, [x]), fcx.llretptr.get()); - } - ~"powf32" => { - let a = get_param(decl, first_real_arg); - let x = get_param(decl, first_real_arg + 1u); - let powf = *ccx.intrinsics.get(&~"llvm.pow.f32"); - Store(bcx, Call(bcx, powf, [a, x]), fcx.llretptr.get()); - } - ~"powf64" => { - let a = get_param(decl, first_real_arg); - let x = get_param(decl, first_real_arg + 1u); - let powf = *ccx.intrinsics.get(&~"llvm.pow.f64"); - Store(bcx, Call(bcx, powf, [a, x]), fcx.llretptr.get()); - } - ~"expf32" => { - let x = get_param(decl, first_real_arg); - let expf = *ccx.intrinsics.get(&~"llvm.exp.f32"); - Store(bcx, Call(bcx, expf, [x]), fcx.llretptr.get()); - } - ~"expf64" => { - let x = get_param(decl, first_real_arg); - let expf = *ccx.intrinsics.get(&~"llvm.exp.f64"); - Store(bcx, Call(bcx, expf, [x]), fcx.llretptr.get()); - } - ~"exp2f32" => { - let x = get_param(decl, first_real_arg); - let exp2f = *ccx.intrinsics.get(&~"llvm.exp2.f32"); - Store(bcx, Call(bcx, exp2f, [x]), fcx.llretptr.get()); - } - ~"exp2f64" => { - let x = get_param(decl, first_real_arg); - let exp2f = *ccx.intrinsics.get(&~"llvm.exp2.f64"); - Store(bcx, Call(bcx, exp2f, [x]), fcx.llretptr.get()); - } - ~"logf32" => { - let x = get_param(decl, first_real_arg); - let logf = *ccx.intrinsics.get(&~"llvm.log.f32"); - Store(bcx, Call(bcx, logf, [x]), fcx.llretptr.get()); - } - ~"logf64" => { - let x = get_param(decl, first_real_arg); - let logf = *ccx.intrinsics.get(&~"llvm.log.f64"); - Store(bcx, Call(bcx, logf, [x]), fcx.llretptr.get()); - } - ~"log10f32" => { - let x = get_param(decl, first_real_arg); - let log10f = *ccx.intrinsics.get(&~"llvm.log10.f32"); - Store(bcx, Call(bcx, log10f, [x]), fcx.llretptr.get()); - } - ~"log10f64" => { - let x = get_param(decl, first_real_arg); - let log10f = *ccx.intrinsics.get(&~"llvm.log10.f64"); - Store(bcx, Call(bcx, log10f, [x]), fcx.llretptr.get()); - } - ~"log2f32" => { - let x = get_param(decl, first_real_arg); - let log2f = *ccx.intrinsics.get(&~"llvm.log2.f32"); - Store(bcx, Call(bcx, log2f, [x]), fcx.llretptr.get()); - } - ~"log2f64" => { - let x = get_param(decl, first_real_arg); - let log2f = *ccx.intrinsics.get(&~"llvm.log2.f64"); - Store(bcx, Call(bcx, log2f, [x]), fcx.llretptr.get()); - } - ~"fmaf32" => { - let a = get_param(decl, first_real_arg); - let b = get_param(decl, first_real_arg + 1u); - let c = get_param(decl, first_real_arg + 2u); - let fmaf = *ccx.intrinsics.get(&~"llvm.fma.f32"); - Store(bcx, Call(bcx, fmaf, [a, b, c]), fcx.llretptr.get()); - } - ~"fmaf64" => { - let a = get_param(decl, first_real_arg); - let b = get_param(decl, first_real_arg + 1u); - let c = get_param(decl, first_real_arg + 2u); - let fmaf = *ccx.intrinsics.get(&~"llvm.fma.f64"); - Store(bcx, Call(bcx, fmaf, [a, b, c]), fcx.llretptr.get()); - } - ~"fabsf32" => { - let x = get_param(decl, first_real_arg); - let fabsf = *ccx.intrinsics.get(&~"llvm.fabs.f32"); - Store(bcx, Call(bcx, fabsf, [x]), fcx.llretptr.get()); - } - ~"fabsf64" => { - let x = get_param(decl, first_real_arg); - let fabsf = *ccx.intrinsics.get(&~"llvm.fabs.f64"); - Store(bcx, Call(bcx, fabsf, [x]), fcx.llretptr.get()); - } - ~"floorf32" => { - let x = get_param(decl, first_real_arg); - let floorf = *ccx.intrinsics.get(&~"llvm.floor.f32"); - Store(bcx, Call(bcx, floorf, [x]), fcx.llretptr.get()); - } - ~"floorf64" => { - let x = get_param(decl, first_real_arg); - let floorf = *ccx.intrinsics.get(&~"llvm.floor.f64"); - Store(bcx, Call(bcx, floorf, [x]), fcx.llretptr.get()); - } - ~"ceilf32" => { - let x = get_param(decl, first_real_arg); - let ceilf = *ccx.intrinsics.get(&~"llvm.ceil.f32"); - Store(bcx, Call(bcx, ceilf, [x]), fcx.llretptr.get()); - } - ~"ceilf64" => { - let x = get_param(decl, first_real_arg); - let ceilf = *ccx.intrinsics.get(&~"llvm.ceil.f64"); - Store(bcx, Call(bcx, ceilf, [x]), fcx.llretptr.get()); - } - ~"truncf32" => { - let x = get_param(decl, first_real_arg); - let truncf = *ccx.intrinsics.get(&~"llvm.trunc.f32"); - Store(bcx, Call(bcx, truncf, [x]), fcx.llretptr.get()); - } - ~"truncf64" => { - let x = get_param(decl, first_real_arg); - let truncf = *ccx.intrinsics.get(&~"llvm.trunc.f64"); - Store(bcx, Call(bcx, truncf, [x]), fcx.llretptr.get()); - } - ~"ctpop8" => { - let x = get_param(decl, first_real_arg); - let ctpop = *ccx.intrinsics.get(&~"llvm.ctpop.i8"); - Store(bcx, Call(bcx, ctpop, [x]), fcx.llretptr.get()) - } - ~"ctpop16" => { - let x = get_param(decl, first_real_arg); - let ctpop = *ccx.intrinsics.get(&~"llvm.ctpop.i16"); - Store(bcx, Call(bcx, ctpop, [x]), fcx.llretptr.get()) - } - ~"ctpop32" => { - let x = get_param(decl, first_real_arg); - let ctpop = *ccx.intrinsics.get(&~"llvm.ctpop.i32"); - Store(bcx, Call(bcx, ctpop, [x]), fcx.llretptr.get()) - } - ~"ctpop64" => { - let x = get_param(decl, first_real_arg); - let ctpop = *ccx.intrinsics.get(&~"llvm.ctpop.i64"); - Store(bcx, Call(bcx, ctpop, [x]), fcx.llretptr.get()) - } - ~"ctlz8" => { - let x = get_param(decl, first_real_arg); - let y = C_i1(false); - let ctlz = *ccx.intrinsics.get(&~"llvm.ctlz.i8"); - Store(bcx, Call(bcx, ctlz, [x, y]), fcx.llretptr.get()) - } - ~"ctlz16" => { - let x = get_param(decl, first_real_arg); - let y = C_i1(false); - let ctlz = *ccx.intrinsics.get(&~"llvm.ctlz.i16"); - Store(bcx, Call(bcx, ctlz, [x, y]), fcx.llretptr.get()) - } - ~"ctlz32" => { - let x = get_param(decl, first_real_arg); - let y = C_i1(false); - let ctlz = *ccx.intrinsics.get(&~"llvm.ctlz.i32"); - Store(bcx, Call(bcx, ctlz, [x, y]), fcx.llretptr.get()) - } - ~"ctlz64" => { - let x = get_param(decl, first_real_arg); - let y = C_i1(false); - let ctlz = *ccx.intrinsics.get(&~"llvm.ctlz.i64"); - Store(bcx, Call(bcx, ctlz, [x, y]), fcx.llretptr.get()) - } - ~"cttz8" => { - let x = get_param(decl, first_real_arg); - let y = C_i1(false); - let cttz = *ccx.intrinsics.get(&~"llvm.cttz.i8"); - Store(bcx, Call(bcx, cttz, [x, y]), fcx.llretptr.get()) - } - ~"cttz16" => { - let x = get_param(decl, first_real_arg); - let y = C_i1(false); - let cttz = *ccx.intrinsics.get(&~"llvm.cttz.i16"); - Store(bcx, Call(bcx, cttz, [x, y]), fcx.llretptr.get()) - } - ~"cttz32" => { - let x = get_param(decl, first_real_arg); - let y = C_i1(false); - let cttz = *ccx.intrinsics.get(&~"llvm.cttz.i32"); - Store(bcx, Call(bcx, cttz, [x, y]), fcx.llretptr.get()) - } - ~"cttz64" => { - let x = get_param(decl, first_real_arg); - let y = C_i1(false); - let cttz = *ccx.intrinsics.get(&~"llvm.cttz.i64"); - Store(bcx, Call(bcx, cttz, [x, y]), fcx.llretptr.get()) - } - ~"bswap16" => { - let x = get_param(decl, first_real_arg); - let cttz = *ccx.intrinsics.get(&~"llvm.bswap.i16"); - Store(bcx, Call(bcx, cttz, [x]), fcx.llretptr.get()) - } - ~"bswap32" => { - let x = get_param(decl, first_real_arg); - let cttz = *ccx.intrinsics.get(&~"llvm.bswap.i32"); - Store(bcx, Call(bcx, cttz, [x]), fcx.llretptr.get()) - } - ~"bswap64" => { - let x = get_param(decl, first_real_arg); - let cttz = *ccx.intrinsics.get(&~"llvm.bswap.i64"); - Store(bcx, Call(bcx, cttz, [x]), fcx.llretptr.get()) - } - _ => { - // Could we make this an enum rather than a string? does it get - // checked earlier? - ccx.sess.span_bug(item.span, "unknown intrinsic"); - } - } - build_return(bcx); - finish_fn(fcx, lltop); + let t = ty::node_id_to_type(ccx.tcx, node_id); + + let tys = foreign_types_for_id(ccx, node_id); + let llfn_ty = lltype_for_fn_from_foreign_types(&tys); + let llfn = base::register_fn_fuller(ccx, + sp, + /*bad*/copy path, + node_id, + attrs, + t, + lib::llvm::CCallConv, + llfn_ty); + add_argument_attributes(&tys, llfn); + debug!("register_rust_fn_with_foreign_abi(node_id=%?, llfn_ty=%s, llfn=%s)", + node_id, ty_str(ccx.tn, llfn_ty), val_str(ccx.tn, llfn)); + llfn } -/** - * Translates a "crust" fn, meaning a Rust fn that can be called - * from C code. In this case, we have to perform some adaptation - * to (1) switch back to the Rust stack and (2) adapt the C calling - * convention to our own. - * - * Example: Given a crust fn F(x: X, y: Y) -> Z, we generate a - * Rust function R as normal: - * - * void R(Z* dest, void *env, X x, Y y) {...} - * - * and then we generate a wrapper function W that looks like: - * - * Z W(X x, Y y) { - * struct { X x; Y y; Z *z; } args = { x, y, z }; - * call_on_c_stack_shim(S, &args); - * } - * - * Note that the wrapper follows the foreign (typically "C") ABI. - * The wrapper is the actual "value" of the foreign fn. Finally, - * we generate a shim function S that looks like: - * - * void S(struct { X x; Y y; Z *z; } *args) { - * R(args->z, NULL, args->x, args->y); - * } - */ -pub fn trans_foreign_fn(ccx: @CrateContext, - path: ast_map::path, - decl: &ast::fn_decl, - body: &ast::blk, - llwrapfn: ValueRef, - id: ast::node_id) { +pub fn trans_rust_fn_with_foreign_abi(ccx: @CrateContext, + path: &ast_map::path, + decl: &ast::fn_decl, + body: &ast::blk, + llwrapfn: ValueRef, + id: ast::node_id) { let _icx = ccx.insn_ctxt("foreign::build_foreign_fn"); + let tys = foreign_types_for_id(ccx, id); + + unsafe { // unsafe because we call LLVM operations + // Build up the Rust function (`foo0` above). + let llrustfn = build_rust_fn(ccx, path, decl, body, id); + + // Build up the foreign wrapper (`foo` above). + return build_wrap_fn(ccx, llrustfn, llwrapfn, &tys); + } fn build_rust_fn(ccx: @CrateContext, - path: ast_map::path, + path: &ast_map::path, decl: &ast::fn_decl, body: &ast::blk, id: ast::node_id) - -> ValueRef { + -> ValueRef { let _icx = ccx.insn_ctxt("foreign::foreign::build_rust_fn"); let t = ty::node_id_to_type(ccx.tcx, id); - // XXX: Bad copy. let ps = link::mangle_internal_name_by_path( - ccx, vec::append_one(copy path, ast_map::path_name( + ccx, vec::append_one(copy *path, ast_map::path_name( special_idents::clownshoe_abi ))); let llty = type_of_fn_from_ty(ccx, t); - let llfndecl = decl_internal_cdecl_fn(ccx.llmod, ps, llty); - trans_fn(ccx, - path, - decl, - body, - llfndecl, - no_self, - None, - id, - None, - []); + let llfndecl = base::decl_internal_cdecl_fn(ccx.llmod, ps, llty); + base::trans_fn(ccx, + copy *path, + decl, + body, + llfndecl, + base::no_self, + None, + id, + None, + []); return llfndecl; } - fn build_shim_fn(ccx: @CrateContext, - path: ast_map::path, - llrustfn: ValueRef, - tys: &ShimTypes) - -> ValueRef { - /*! - * - * Generate the shim S: - * - * void S(struct { X x; Y y; Z *z; } *args) { - * R(args->z, NULL, &args->x, args->y); - * } - * - * One complication is that we must adapt to the Rust - * calling convention, which introduces indirection - * in some cases. To demonstrate this, I wrote one of the - * entries above as `&args->x`, because presumably `X` is - * one of those types that is passed by pointer in Rust. - */ - - let _icx = ccx.insn_ctxt("foreign::foreign::build_shim_fn"); - - fn build_args(bcx: block, tys: &ShimTypes, llargbundle: ValueRef) - -> ~[ValueRef] { - let _icx = bcx.insn_ctxt("foreign::extern::shim::build_args"); - let ccx = bcx.ccx(); - let mut llargvals = ~[]; - let mut i = 0u; - let n = tys.fn_sig.inputs.len(); - - if !ty::type_is_immediate(tys.fn_sig.output) { - let llretptr = load_inbounds(bcx, llargbundle, [0u, n]); - llargvals.push(llretptr); - } else { - llargvals.push(C_null(T_ptr(T_i8()))); + unsafe fn build_wrap_fn(ccx: @CrateContext, + llrustfn: ValueRef, + llwrapfn: ValueRef, + tys: &ForeignTypes) { + let _icx = ccx.insn_ctxt( + "foreign::trans_rust_fn_with_foreign_abi::build_wrap_fn"); + + debug!("build_wrap_fn(llrustfn=%s, llwrapfn=%s)", + val_str(ccx.tn, llrustfn), + val_str(ccx.tn, llwrapfn)); + + // Avoid all the Rust generation stuff and just generate raw + // LLVM here. + // + // We want to generate code like this: + // + // S foo(T i) { + // S r; + // foo0(&r, NULL, i); + // return r; + // } + + let the_block = + str::as_c_str("the_block", + |s| llvm::LLVMAppendBasicBlock(llwrapfn, s)); + + let builder = ccx.builder.B; + llvm::LLVMPositionBuilderAtEnd(builder, the_block); + + // Array for the arguments we will pass to the rust function. + let mut llrust_args = ~[]; + let mut next_foreign_arg_counter: c_uint = 0; + let next_foreign_arg: &fn() -> c_uint = { + || { + next_foreign_arg_counter += 1; + next_foreign_arg_counter - 1 } + }; - let llenvptr = C_null(T_opaque_box_ptr(bcx.ccx())); - llargvals.push(llenvptr); - while i < n { - // Get a pointer to the argument: - let mut llargval = GEPi(bcx, llargbundle, [0u, i]); + // If there is an out pointer on the foreign function + let foreign_outptr = { + if tys.fn_ty.sret { + Some(llvm::LLVMGetParam(llwrapfn, next_foreign_arg())) + } else { + None + } + }; + + // Push Rust return pointer, using null if it will be unused. + let rust_uses_outptr = type_of::return_uses_outptr(tys.fn_sig.output); + let return_alloca: Option; + let llrust_ret_ty = tys.llsig.llret_ty; + let llrust_retptr_ty = T_ptr(llrust_ret_ty); + if rust_uses_outptr { + // Rust expects to use an outpointer. If the foreign fn + // also uses an outpointer, we can reuse it, but the types + // may vary, so cast first to the Rust type. If the + // foriegn fn does NOT use an outpointer, we will have to + // alloca some scratch space on the stack. + match foreign_outptr { + Some(llforeign_outptr) => { + debug!("out pointer, foreign=%s", + val_str(ccx.tn, llforeign_outptr)); + let llrust_retptr = + llvm::LLVMBuildBitCast(builder, + llforeign_outptr, + T_ptr(llrust_ret_ty), + noname()); + debug!("out pointer, foreign=%s (casted)", + val_str(ccx.tn, llrust_retptr)); + llrust_args.push(llrust_retptr); + return_alloca = None; + } - if !type_of::arg_is_indirect(ccx, &tys.fn_sig.inputs[i]) { - // If Rust would pass this by value, load the value. - llargval = Load(bcx, llargval); + None => { + let slot = { + str::as_c_str( + "return_alloca", + |s| llvm::LLVMBuildAlloca(builder, llrust_ret_ty, s)) + }; + debug!("out pointer, \ + allocad=%s, \ + llrust_ret_ty=%s, \ + return_ty=%s", + val_str(ccx.tn, slot), + ty_str(ccx.tn, llrust_ret_ty), + tys.fn_sig.output.repr(ccx.tcx)); + llrust_args.push(slot); + return_alloca = Some(slot); } + } + } else { + // Rust will not use the outpointer, so pass undefined. + let undef = llvm::LLVMGetUndef(T_ptr(T_i8())); + debug!("out pointer, undef=%s", val_str(ccx.tn, undef)); + llrust_args.push(undef); + return_alloca = None; + }; + + // Push an (null) env pointer + let env_pointer = base::null_env_ptr(ccx); + debug!("env pointer=%s", val_str(ccx.tn, env_pointer)); + llrust_args.push(env_pointer); + + // Build up the arguments to the call to the rust function. + // Careful to adapt for cases where the native convention uses + // a pointer and Rust does not or vice versa. + for uint::range(0, tys.fn_sig.inputs.len()) |i| { + let rust_ty = tys.fn_sig.inputs[i]; + let llrust_ty = tys.llsig.llarg_tys[i]; + let foreign_index = next_foreign_arg(); + let rust_indirect = type_of::arg_is_indirect(ccx, rust_ty); + let foreign_indirect = tys.fn_ty.attrs[foreign_index].is_some(); + let mut llforeign_arg = llvm::LLVMGetParam(llwrapfn, foreign_index); + + debug!("llforeign_arg #%u: %s", + i, val_str(ccx.tn, llforeign_arg)); + debug!("rust_indirect = %b, foreign_indirect = %b", + rust_indirect, foreign_indirect); + + // Ensure that the foreign argument is indirect (by + // pointer). It makes adapting types easier, since we can + // always just bitcast pointers. + if !foreign_indirect { + let lltemp = + llvm::LLVMBuildAlloca( + builder, val_ty(llforeign_arg), noname()); + llvm::LLVMBuildStore( + builder, llforeign_arg, lltemp); + llforeign_arg = lltemp; + } - llargvals.push(llargval); - i += 1u; + // If the types in the ABI and the Rust types don't match, + // bitcast the llforeign_arg pointer so it matches the types + // Rust expects. + if tys.fn_ty.arg_tys[foreign_index].cast { + assert!(!foreign_indirect); + llforeign_arg = llvm::LLVMBuildBitCast(builder, llforeign_arg, + T_ptr(llrust_ty), noname()); } - return llargvals; - } - fn build_ret(bcx: block, - shim_types: &ShimTypes, - llargbundle: ValueRef, - llretval: ValueRef) { - if ty::type_is_immediate(shim_types.fn_sig.output) { - // Write the value into the argument bundle. - let arg_count = shim_types.fn_sig.inputs.len(); - let llretptr = load_inbounds(bcx, - llargbundle, - [0, arg_count]); - Store(bcx, llretval, llretptr); + let llrust_arg = if rust_indirect { + llforeign_arg } else { - // NB: The return pointer in the Rust ABI function is wired - // directly into the return slot in the shim struct. + llvm::LLVMBuildLoad(builder, llforeign_arg, noname()) + }; + + debug!("llrust_arg #%u: %s", + i, val_str(ccx.tn, llrust_arg)); + llrust_args.push(llrust_arg); + } + + // Perform the call itself + let llrust_ret_val = do vec::as_imm_buf(llrust_args) |ptr, len| { + debug!("calling llrustfn = %s", val_str(ccx.tn, llrustfn)); + llvm::LLVMBuildCall(builder, llrustfn, ptr, + len as c_uint, noname()) + }; + + // Get the return value where the foreign fn expects it. + let llforeign_ret_ty = tys.fn_ty.ret_ty.ty; + match foreign_outptr { + None if !tys.ret_def => { + // Function returns `()` or `bot`, which in Rust is the LLVM + // type "{}" but in foreign ABIs is "Void". + llvm::LLVMBuildRetVoid(builder); + } + + None if rust_uses_outptr => { + // Rust uses an outpointer, but the foreign ABI does not. Load. + let llrust_outptr = return_alloca.get(); + let llforeign_outptr_casted = + llvm::LLVMBuildBitCast(builder, + llrust_outptr, + T_ptr(llforeign_ret_ty), + noname()); + let llforeign_retval = + llvm::LLVMBuildLoad(builder, llforeign_outptr_casted, noname()); + llvm::LLVMBuildRet(builder, llforeign_retval); + } + + None if llforeign_ret_ty != llrust_ret_ty => { + // Neither ABI uses an outpointer, but the types don't + // quite match. Must cast. Probably we should try and + // examine the types and use a concrete llvm cast, but + // right now we just use a temp memory location and + // bitcast the pointer, which is the same thing the + // old wrappers used to do. + let lltemp = + llvm::LLVMBuildAlloca( + builder, llforeign_ret_ty, noname()); + let lltemp_casted = + llvm::LLVMBuildBitCast(builder, + lltemp, + T_ptr(llrust_ret_ty), + noname()); + llvm::LLVMBuildStore( + builder, llrust_ret_val, lltemp_casted); + let llforeign_retval = + llvm::LLVMBuildLoad(builder, lltemp, noname()); + llvm::LLVMBuildRet(builder, llforeign_retval); + } + + None => { + // Neither ABI uses an outpointer, and the types + // match. Easy peasy. + llvm::LLVMBuildRet(builder, llrust_ret_val); + } + + Some(llforeign_outptr) if !rust_uses_outptr => { + // Foreign ABI requires an out pointer, but Rust doesn't. + // Store Rust return value. + let llforeign_outptr_casted = + llvm::LLVMBuildBitCast(builder, + llforeign_outptr, + llrust_retptr_ty, + noname()); + llvm::LLVMBuildStore( + builder, llrust_ret_val, llforeign_outptr_casted); + llvm::LLVMBuildRetVoid(builder); } - build_return(bcx); + Some(_) => { + // Both ABIs use outpointers. Easy peasy. + llvm::LLVMBuildRetVoid(builder); + } } + } +} - let shim_name = link::mangle_internal_name_by_path( - ccx, - vec::append_one(path, ast_map::path_name( - special_idents::clownshoe_stack_shim - ))); - build_shim_fn_(ccx, - shim_name, - llrustfn, - tys, - lib::llvm::CCallConv, - build_args, - build_ret) +/////////////////////////////////////////////////////////////////////////// +// General ABI Support +// +// This code is kind of a confused mess and needs to be reworked given +// the massive simplifications that have occurred. + +fn abi_info(ccx: @CrateContext) -> @cabi::ABIInfo { + return match ccx.sess.targ_cfg.arch { + X86 => cabi_x86::abi_info(ccx), + X86_64 => cabi_x86_64::abi_info(), + Arm => cabi_arm::abi_info(), + Mips => cabi_mips::abi_info(), } +} - fn build_wrap_fn(ccx: @CrateContext, - llshimfn: ValueRef, - llwrapfn: ValueRef, - tys: &ShimTypes) { - /*! - * - * Generate the wrapper W: - * - * Z W(X x, Y y) { - * struct { X x; Y y; Z *z; } args = { x, y, z }; - * call_on_c_stack_shim(S, &args); - * } - */ - - let _icx = ccx.insn_ctxt("foreign::foreign::build_wrap_fn"); - - build_wrap_fn_(ccx, - tys, - llshimfn, - llwrapfn, - ccx.upcalls.call_shim_on_rust_stack, - true, - build_args, - build_ret); - - fn build_args(bcx: block, - tys: &ShimTypes, - llwrapfn: ValueRef, - llargbundle: ValueRef) { - let _icx = bcx.insn_ctxt("foreign::foreign::wrap::build_args"); - tys.fn_ty.build_wrap_args(bcx, - tys.llsig.llret_ty, - llwrapfn, - llargbundle); - } +pub fn link_name(ccx: @CrateContext, i: @ast::foreign_item) -> @~str { + match attr::first_attr_value_str_by_name(i.attrs, "link_name") { + None => ccx.sess.str_of(i.ident), + Some(ln) => ln, + } +} - fn build_ret(bcx: block, tys: &ShimTypes, llargbundle: ValueRef) { - let _icx = bcx.insn_ctxt("foreign::foreign::wrap::build_ret"); - tys.fn_ty.build_wrap_ret(bcx, tys.llsig.llarg_tys, llargbundle); - build_return(bcx); - } +fn foreign_signature(ccx: @CrateContext, fn_sig: &ty::FnSig) + -> LlvmSignature { + /*! + * The ForeignSignature is the LLVM types of the arguments/return type + * of a function. Note that these LLVM types are not quite the same + * as the LLVM types would be for a native Rust function because foreign + * functions just plain ignore modes. They also don't pass aggregate + * values by pointer like we do. + */ + + let llarg_tys = fn_sig.inputs.map(|&arg| type_of(ccx, arg)); + let llret_ty = type_of::type_of(ccx, fn_sig.output); + LlvmSignature { + llarg_tys: llarg_tys, + llret_ty: llret_ty, + sret: type_of::return_uses_outptr(fn_sig.output), } +} - let tys = shim_types(ccx, id); - // The internal Rust ABI function - runs on the Rust stack - // XXX: Bad copy. - let llrustfn = build_rust_fn(ccx, copy path, decl, body, id); - // The internal shim function - runs on the Rust stack - let llshimfn = build_shim_fn(ccx, path, llrustfn, &tys); - // The foreign C function - runs on the C stack - build_wrap_fn(ccx, llshimfn, llwrapfn, &tys) +fn foreign_types_for_id(ccx: @CrateContext, id: ast::node_id) -> ForeignTypes { + foreign_types_for_fn_ty(ccx, ty::node_id_to_type(ccx.tcx, id)) } -pub fn register_foreign_fn(ccx: @CrateContext, - sp: span, - path: ast_map::path, - node_id: ast::node_id, - attrs: &[ast::attribute]) - -> ValueRef { - let _icx = ccx.insn_ctxt("foreign::register_foreign_fn"); +fn foreign_types_for_fn_ty(ccx: @CrateContext, ty: ty::t) -> ForeignTypes { + let fn_sig = match ty::get(ty).sty { + ty::ty_bare_fn(ref fn_ty) => copy fn_ty.sig, + _ => ccx.sess.bug("foreign_types_for_fn_ty called on non-function type") + }; + let llsig = foreign_signature(ccx, &fn_sig); + let ret_def = !ty::type_is_voidish(fn_sig.output); + let fn_ty = abi_info(ccx).compute_info(llsig.llarg_tys, + llsig.llret_ty, + ret_def); + ForeignTypes { + fn_sig: fn_sig, + llsig: llsig, + ret_def: ret_def, + fn_ty: fn_ty + } +} - let t = ty::node_id_to_type(ccx.tcx, node_id); +fn lltype_for_fn_from_foreign_types(tys: &ForeignTypes) -> TypeRef { + let llargument_tys = vec::map(tys.fn_ty.arg_tys, |t| t.ty); + let llreturn_ty = tys.fn_ty.ret_ty.ty; + T_fn(llargument_tys, llreturn_ty) +} - let tys = shim_types(ccx, node_id); - do tys.fn_ty.decl_fn |fnty| { - register_fn_fuller(ccx, - sp, - /*bad*/copy path, - node_id, - attrs, - t, - lib::llvm::CCallConv, - fnty) +pub fn lltype_for_foreign_fn(ccx: @CrateContext, ty: ty::t) -> TypeRef { + let fn_types = foreign_types_for_fn_ty(ccx, ty); + lltype_for_fn_from_foreign_types(&fn_types) +} + +fn add_argument_attributes(tys: &ForeignTypes, + llfn: ValueRef) { + for tys.fn_ty.attrs.eachi |i, a| { + match *a { + Some(attr) => { + let llarg = get_param(llfn, i); + unsafe { + llvm::LLVMAddAttribute(llarg, attr as c_uint); + } + } + None => () + } } } diff --git a/src/librustc/middle/trans/intrinsic.rs b/src/librustc/middle/trans/intrinsic.rs new file mode 100644 index 0000000000000..2d57e579bac4d --- /dev/null +++ b/src/librustc/middle/trans/intrinsic.rs @@ -0,0 +1,666 @@ +// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use core::prelude::*; +use back::{abi}; +use lib::llvm::{SequentiallyConsistent, Acquire, Release, Xchg}; +use lib::llvm::{ValueRef}; +use lib; +use middle::trans::base::*; +use middle::trans::build::*; +use middle::trans::callee::*; +use middle::trans::common::*; +use middle::trans::datum::*; +use middle::trans::type_of::*; +use middle::trans::type_of; +use middle::trans::expr::Ignore; +use middle::ty::FnSig; +use middle::ty; +use syntax::ast; +use syntax::ast_map; +use syntax::attr; +use syntax::opt_vec; +use middle::trans::machine; +use middle::trans::glue; +use util::ppaux::{ty_to_str}; +use middle::trans::machine::llsize_of; + +pub fn trans_intrinsic(ccx: @CrateContext, + decl: ValueRef, + item: @ast::foreign_item, + path: ast_map::path, + substs: @param_substs, + attributes: &[ast::attribute], + ref_id: Option) { + debug!("trans_intrinsic(item.ident=%s)", *ccx.sess.str_of(item.ident)); + + let output_type = ty::ty_fn_ret(ty::node_id_to_type(ccx.tcx, item.id)); + + let fcx = new_fn_ctxt_w_id(ccx, + path, + decl, + item.id, + output_type, + None, + Some(substs), + Some(item.span)); + + // Set the fixed stack segment flag if necessary. + if attr::attrs_contains_name(attributes, "fixed_stack_segment") { + set_fixed_stack_segment(fcx.llfn); + } + + let mut bcx = top_scope_block(fcx, None); + let lltop = bcx.llbb; + match *ccx.sess.str_of(item.ident) { + ~"atomic_cxchg" => { + let old = AtomicCmpXchg(bcx, + get_param(decl, first_real_arg), + get_param(decl, first_real_arg + 1u), + get_param(decl, first_real_arg + 2u), + SequentiallyConsistent); + Store(bcx, old, fcx.llretptr.get()); + } + ~"atomic_cxchg_acq" => { + let old = AtomicCmpXchg(bcx, + get_param(decl, first_real_arg), + get_param(decl, first_real_arg + 1u), + get_param(decl, first_real_arg + 2u), + Acquire); + Store(bcx, old, fcx.llretptr.get()); + } + ~"atomic_cxchg_rel" => { + let old = AtomicCmpXchg(bcx, + get_param(decl, first_real_arg), + get_param(decl, first_real_arg + 1u), + get_param(decl, first_real_arg + 2u), + Release); + Store(bcx, old, fcx.llretptr.get()); + } + ~"atomic_load" => { + let old = AtomicLoad(bcx, + get_param(decl, first_real_arg), + SequentiallyConsistent); + Store(bcx, old, fcx.llretptr.get()); + } + ~"atomic_load_acq" => { + let old = AtomicLoad(bcx, + get_param(decl, first_real_arg), + Acquire); + Store(bcx, old, fcx.llretptr.get()); + } + ~"atomic_store" => { + AtomicStore(bcx, + get_param(decl, first_real_arg + 1u), + get_param(decl, first_real_arg), + SequentiallyConsistent); + } + ~"atomic_store_rel" => { + AtomicStore(bcx, + get_param(decl, first_real_arg + 1u), + get_param(decl, first_real_arg), + Release); + } + ~"atomic_xchg" => { + let old = AtomicRMW(bcx, Xchg, + get_param(decl, first_real_arg), + get_param(decl, first_real_arg + 1u), + SequentiallyConsistent); + Store(bcx, old, fcx.llretptr.get()); + } + ~"atomic_xchg_acq" => { + let old = AtomicRMW(bcx, Xchg, + get_param(decl, first_real_arg), + get_param(decl, first_real_arg + 1u), + Acquire); + Store(bcx, old, fcx.llretptr.get()); + } + ~"atomic_xchg_rel" => { + let old = AtomicRMW(bcx, Xchg, + get_param(decl, first_real_arg), + get_param(decl, first_real_arg + 1u), + Release); + Store(bcx, old, fcx.llretptr.get()); + } + ~"atomic_xadd" => { + let old = AtomicRMW(bcx, lib::llvm::Add, + get_param(decl, first_real_arg), + get_param(decl, first_real_arg + 1u), + SequentiallyConsistent); + Store(bcx, old, fcx.llretptr.get()); + } + ~"atomic_xadd_acq" => { + let old = AtomicRMW(bcx, lib::llvm::Add, + get_param(decl, first_real_arg), + get_param(decl, first_real_arg + 1u), + Acquire); + Store(bcx, old, fcx.llretptr.get()); + } + ~"atomic_xadd_rel" => { + let old = AtomicRMW(bcx, lib::llvm::Add, + get_param(decl, first_real_arg), + get_param(decl, first_real_arg + 1u), + Release); + Store(bcx, old, fcx.llretptr.get()); + } + ~"atomic_xsub" => { + let old = AtomicRMW(bcx, lib::llvm::Sub, + get_param(decl, first_real_arg), + get_param(decl, first_real_arg + 1u), + SequentiallyConsistent); + Store(bcx, old, fcx.llretptr.get()); + } + ~"atomic_xsub_acq" => { + let old = AtomicRMW(bcx, lib::llvm::Sub, + get_param(decl, first_real_arg), + get_param(decl, first_real_arg + 1u), + Acquire); + Store(bcx, old, fcx.llretptr.get()); + } + ~"atomic_xsub_rel" => { + let old = AtomicRMW(bcx, lib::llvm::Sub, + get_param(decl, first_real_arg), + get_param(decl, first_real_arg + 1u), + Release); + Store(bcx, old, fcx.llretptr.get()); + } + ~"size_of" => { + let tp_ty = substs.tys[0]; + let lltp_ty = type_of::type_of(ccx, tp_ty); + Store(bcx, C_uint(ccx, machine::llsize_of_real(ccx, lltp_ty)), + fcx.llretptr.get()); + } + ~"move_val" => { + // Create a datum reflecting the value being moved: + // + // - the datum will be by ref if the value is non-immediate; + // + // - the datum has a RevokeClean source because, that way, + // the `move_to()` method does not feel compelled to + // zero out the memory where the datum resides. Zeroing + // is not necessary since, for intrinsics, there is no + // cleanup to concern ourselves with. + let tp_ty = substs.tys[0]; + let mode = appropriate_mode(tp_ty); + let src = Datum {val: get_param(decl, first_real_arg + 1u), + ty: tp_ty, mode: mode, source: RevokeClean}; + bcx = src.move_to(bcx, DROP_EXISTING, + get_param(decl, first_real_arg)); + } + ~"move_val_init" => { + // See comments for `"move_val"`. + let tp_ty = substs.tys[0]; + let mode = appropriate_mode(tp_ty); + let src = Datum {val: get_param(decl, first_real_arg + 1u), + ty: tp_ty, mode: mode, source: RevokeClean}; + bcx = src.move_to(bcx, INIT, get_param(decl, first_real_arg)); + } + ~"min_align_of" => { + let tp_ty = substs.tys[0]; + let lltp_ty = type_of::type_of(ccx, tp_ty); + Store(bcx, C_uint(ccx, machine::llalign_of_min(ccx, lltp_ty)), + fcx.llretptr.get()); + } + ~"pref_align_of"=> { + let tp_ty = substs.tys[0]; + let lltp_ty = type_of::type_of(ccx, tp_ty); + Store(bcx, C_uint(ccx, machine::llalign_of_pref(ccx, lltp_ty)), + fcx.llretptr.get()); + } + ~"get_tydesc" => { + let tp_ty = substs.tys[0]; + let static_ti = get_tydesc(ccx, tp_ty); + glue::lazily_emit_all_tydesc_glue(ccx, static_ti); + + // FIXME (#3727): change this to T_ptr(ccx.tydesc_ty) when the + // core::sys copy of the get_tydesc interface dies off. + let td = PointerCast(bcx, static_ti.tydesc, T_ptr(T_nil())); + Store(bcx, td, fcx.llretptr.get()); + } + ~"init" => { + let tp_ty = substs.tys[0]; + let lltp_ty = type_of::type_of(ccx, tp_ty); + if !ty::type_is_nil(tp_ty) { + Store(bcx, C_null(lltp_ty), fcx.llretptr.get()); + } + } + ~"uninit" => { + // Do nothing, this is effectively a no-op + } + ~"forget" => {} + ~"transmute" => { + let (in_type, out_type) = (substs.tys[0], substs.tys[1]); + let llintype = type_of::type_of(ccx, in_type); + let llouttype = type_of::type_of(ccx, out_type); + + let in_type_size = machine::llbitsize_of_real(ccx, llintype); + let out_type_size = machine::llbitsize_of_real(ccx, llouttype); + if in_type_size != out_type_size { + let sp = match ccx.tcx.items.get_copy(&ref_id.get()) { + ast_map::node_expr(e) => e.span, + _ => fail!("transmute has non-expr arg"), + }; + let pluralize = |n| if 1u == n { "" } else { "s" }; + ccx.sess.span_fatal(sp, + fmt!("transmute called on types with \ + different sizes: %s (%u bit%s) to \ + %s (%u bit%s)", + ty_to_str(ccx.tcx, in_type), + in_type_size, + pluralize(in_type_size), + ty_to_str(ccx.tcx, out_type), + out_type_size, + pluralize(out_type_size))); + } + + if !ty::type_is_nil(out_type) { + // NB: Do not use a Load and Store here. This causes massive + // code bloat when `transmute` is used on large structural + // types. + let lldestptr = fcx.llretptr.get(); + let lldestptr = PointerCast(bcx, lldestptr, T_ptr(T_i8())); + + let llsrcval = get_param(decl, first_real_arg); + let llsrcptr = if ty::type_is_immediate(in_type) { + let llsrcptr = alloca(bcx, llintype); + Store(bcx, llsrcval, llsrcptr); + llsrcptr + } else { + llsrcval + }; + let llsrcptr = PointerCast(bcx, llsrcptr, T_ptr(T_i8())); + + let llsize = llsize_of(ccx, llintype); + call_memcpy(bcx, lldestptr, llsrcptr, llsize, 1); + } + } + ~"needs_drop" => { + let tp_ty = substs.tys[0]; + Store(bcx, + C_bool(ty::type_needs_drop(ccx.tcx, tp_ty)), + fcx.llretptr.get()); + } + ~"visit_tydesc" => { + let td = get_param(decl, first_real_arg); + let visitor = get_param(decl, first_real_arg + 1u); + //let llvisitorptr = alloca(bcx, val_ty(visitor)); + //Store(bcx, visitor, llvisitorptr); + let td = PointerCast(bcx, td, T_ptr(ccx.tydesc_type)); + glue::call_tydesc_glue_full(bcx, + visitor, + td, + abi::tydesc_field_visit_glue, + None); + } + ~"frame_address" => { + let frameaddress = *ccx.intrinsics.get(&~"llvm.frameaddress"); + let frameaddress_val = Call(bcx, frameaddress, [C_i32(0i32)]); + let star_u8 = ty::mk_imm_ptr( + bcx.tcx(), + ty::mk_mach_uint(ast::ty_u8)); + let fty = ty::mk_closure(bcx.tcx(), ty::ClosureTy { + purity: ast::impure_fn, + sigil: ast::BorrowedSigil, + onceness: ast::Many, + region: ty::re_bound(ty::br_anon(0)), + bounds: ty::EmptyBuiltinBounds(), + sig: FnSig { + bound_lifetime_names: opt_vec::Empty, + inputs: ~[ star_u8 ], + output: ty::mk_nil() + } + }); + let datum = Datum {val: get_param(decl, first_real_arg), + mode: ByRef, ty: fty, source: ZeroMem}; + let arg_vals = ~[frameaddress_val]; + bcx = trans_call_inner( + bcx, None, fty, ty::mk_nil(), + |bcx| Callee {bcx: bcx, data: Closure(datum)}, + ArgVals(arg_vals), Ignore, DontAutorefArg); + } + ~"morestack_addr" => { + // XXX This is a hack to grab the address of this particular + // native function. There should be a general in-language + // way to do this + let llfty = type_of_rust_fn(bcx.ccx(), [], ty::mk_nil()); + let morestack_addr = decl_cdecl_fn( + bcx.ccx().llmod, "__morestack", llfty); + let morestack_addr = PointerCast(bcx, morestack_addr, + T_ptr(T_nil())); + Store(bcx, morestack_addr, fcx.llretptr.get()); + } + ~"memcpy32" => { + let tp_ty = substs.tys[0]; + let lltp_ty = type_of::type_of(ccx, tp_ty); + let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32); + let size = C_i32(machine::llsize_of_real(ccx, lltp_ty) as i32); + + let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), T_ptr(T_i8())); + let src_ptr = PointerCast(bcx, get_param(decl, first_real_arg + 1), T_ptr(T_i8())); + let count = get_param(decl, first_real_arg + 2); + let volatile = C_i1(false); + let llfn = *bcx.ccx().intrinsics.get(&~"llvm.memcpy.p0i8.p0i8.i32"); + Call(bcx, llfn, [dst_ptr, src_ptr, Mul(bcx, size, count), align, volatile]); + } + ~"memcpy64" => { + let tp_ty = substs.tys[0]; + let lltp_ty = type_of::type_of(ccx, tp_ty); + let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32); + let size = C_i64(machine::llsize_of_real(ccx, lltp_ty) as i64); + + let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), T_ptr(T_i8())); + let src_ptr = PointerCast(bcx, get_param(decl, first_real_arg + 1), T_ptr(T_i8())); + let count = get_param(decl, first_real_arg + 2); + let volatile = C_i1(false); + let llfn = *bcx.ccx().intrinsics.get(&~"llvm.memcpy.p0i8.p0i8.i64"); + Call(bcx, llfn, [dst_ptr, src_ptr, Mul(bcx, size, count), align, volatile]); + } + ~"memmove32" => { + let tp_ty = substs.tys[0]; + let lltp_ty = type_of::type_of(ccx, tp_ty); + let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32); + let size = C_i32(machine::llsize_of_real(ccx, lltp_ty) as i32); + + let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), T_ptr(T_i8())); + let src_ptr = PointerCast(bcx, get_param(decl, first_real_arg + 1), T_ptr(T_i8())); + let count = get_param(decl, first_real_arg + 2); + let volatile = C_i1(false); + let llfn = *bcx.ccx().intrinsics.get(&~"llvm.memmove.p0i8.p0i8.i32"); + Call(bcx, llfn, [dst_ptr, src_ptr, Mul(bcx, size, count), align, volatile]); + } + ~"memmove64" => { + let tp_ty = substs.tys[0]; + let lltp_ty = type_of::type_of(ccx, tp_ty); + let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32); + let size = C_i64(machine::llsize_of_real(ccx, lltp_ty) as i64); + + let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), T_ptr(T_i8())); + let src_ptr = PointerCast(bcx, get_param(decl, first_real_arg + 1), T_ptr(T_i8())); + let count = get_param(decl, first_real_arg + 2); + let volatile = C_i1(false); + let llfn = *bcx.ccx().intrinsics.get(&~"llvm.memmove.p0i8.p0i8.i64"); + Call(bcx, llfn, [dst_ptr, src_ptr, Mul(bcx, size, count), align, volatile]); + } + ~"memset32" => { + let tp_ty = substs.tys[0]; + let lltp_ty = type_of::type_of(ccx, tp_ty); + let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32); + let size = C_i32(machine::llsize_of_real(ccx, lltp_ty) as i32); + + let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), T_ptr(T_i8())); + let val = get_param(decl, first_real_arg + 1); + let count = get_param(decl, first_real_arg + 2); + let volatile = C_i1(false); + let llfn = *bcx.ccx().intrinsics.get(&~"llvm.memset.p0i8.i32"); + Call(bcx, llfn, [dst_ptr, val, Mul(bcx, size, count), align, volatile]); + } + ~"memset64" => { + let tp_ty = substs.tys[0]; + let lltp_ty = type_of::type_of(ccx, tp_ty); + let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32); + let size = C_i64(machine::llsize_of_real(ccx, lltp_ty) as i64); + + let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), T_ptr(T_i8())); + let val = get_param(decl, first_real_arg + 1); + let count = get_param(decl, first_real_arg + 2); + let volatile = C_i1(false); + let llfn = *bcx.ccx().intrinsics.get(&~"llvm.memset.p0i8.i64"); + Call(bcx, llfn, [dst_ptr, val, Mul(bcx, size, count), align, volatile]); + } + ~"sqrtf32" => { + let x = get_param(decl, first_real_arg); + let sqrtf = *ccx.intrinsics.get(&~"llvm.sqrt.f32"); + Store(bcx, Call(bcx, sqrtf, [x]), fcx.llretptr.get()); + } + ~"sqrtf64" => { + let x = get_param(decl, first_real_arg); + let sqrtf = *ccx.intrinsics.get(&~"llvm.sqrt.f64"); + Store(bcx, Call(bcx, sqrtf, [x]), fcx.llretptr.get()); + } + ~"powif32" => { + let a = get_param(decl, first_real_arg); + let x = get_param(decl, first_real_arg + 1u); + let powif = *ccx.intrinsics.get(&~"llvm.powi.f32"); + Store(bcx, Call(bcx, powif, [a, x]), fcx.llretptr.get()); + } + ~"powif64" => { + let a = get_param(decl, first_real_arg); + let x = get_param(decl, first_real_arg + 1u); + let powif = *ccx.intrinsics.get(&~"llvm.powi.f64"); + Store(bcx, Call(bcx, powif, [a, x]), fcx.llretptr.get()); + } + ~"sinf32" => { + let x = get_param(decl, first_real_arg); + let sinf = *ccx.intrinsics.get(&~"llvm.sin.f32"); + Store(bcx, Call(bcx, sinf, [x]), fcx.llretptr.get()); + } + ~"sinf64" => { + let x = get_param(decl, first_real_arg); + let sinf = *ccx.intrinsics.get(&~"llvm.sin.f64"); + Store(bcx, Call(bcx, sinf, [x]), fcx.llretptr.get()); + } + ~"cosf32" => { + let x = get_param(decl, first_real_arg); + let cosf = *ccx.intrinsics.get(&~"llvm.cos.f32"); + Store(bcx, Call(bcx, cosf, [x]), fcx.llretptr.get()); + } + ~"cosf64" => { + let x = get_param(decl, first_real_arg); + let cosf = *ccx.intrinsics.get(&~"llvm.cos.f64"); + Store(bcx, Call(bcx, cosf, [x]), fcx.llretptr.get()); + } + ~"powf32" => { + let a = get_param(decl, first_real_arg); + let x = get_param(decl, first_real_arg + 1u); + let powf = *ccx.intrinsics.get(&~"llvm.pow.f32"); + Store(bcx, Call(bcx, powf, [a, x]), fcx.llretptr.get()); + } + ~"powf64" => { + let a = get_param(decl, first_real_arg); + let x = get_param(decl, first_real_arg + 1u); + let powf = *ccx.intrinsics.get(&~"llvm.pow.f64"); + Store(bcx, Call(bcx, powf, [a, x]), fcx.llretptr.get()); + } + ~"expf32" => { + let x = get_param(decl, first_real_arg); + let expf = *ccx.intrinsics.get(&~"llvm.exp.f32"); + Store(bcx, Call(bcx, expf, [x]), fcx.llretptr.get()); + } + ~"expf64" => { + let x = get_param(decl, first_real_arg); + let expf = *ccx.intrinsics.get(&~"llvm.exp.f64"); + Store(bcx, Call(bcx, expf, [x]), fcx.llretptr.get()); + } + ~"exp2f32" => { + let x = get_param(decl, first_real_arg); + let exp2f = *ccx.intrinsics.get(&~"llvm.exp2.f32"); + Store(bcx, Call(bcx, exp2f, [x]), fcx.llretptr.get()); + } + ~"exp2f64" => { + let x = get_param(decl, first_real_arg); + let exp2f = *ccx.intrinsics.get(&~"llvm.exp2.f64"); + Store(bcx, Call(bcx, exp2f, [x]), fcx.llretptr.get()); + } + ~"logf32" => { + let x = get_param(decl, first_real_arg); + let logf = *ccx.intrinsics.get(&~"llvm.log.f32"); + Store(bcx, Call(bcx, logf, [x]), fcx.llretptr.get()); + } + ~"logf64" => { + let x = get_param(decl, first_real_arg); + let logf = *ccx.intrinsics.get(&~"llvm.log.f64"); + Store(bcx, Call(bcx, logf, [x]), fcx.llretptr.get()); + } + ~"log10f32" => { + let x = get_param(decl, first_real_arg); + let log10f = *ccx.intrinsics.get(&~"llvm.log10.f32"); + Store(bcx, Call(bcx, log10f, [x]), fcx.llretptr.get()); + } + ~"log10f64" => { + let x = get_param(decl, first_real_arg); + let log10f = *ccx.intrinsics.get(&~"llvm.log10.f64"); + Store(bcx, Call(bcx, log10f, [x]), fcx.llretptr.get()); + } + ~"log2f32" => { + let x = get_param(decl, first_real_arg); + let log2f = *ccx.intrinsics.get(&~"llvm.log2.f32"); + Store(bcx, Call(bcx, log2f, [x]), fcx.llretptr.get()); + } + ~"log2f64" => { + let x = get_param(decl, first_real_arg); + let log2f = *ccx.intrinsics.get(&~"llvm.log2.f64"); + Store(bcx, Call(bcx, log2f, [x]), fcx.llretptr.get()); + } + ~"fmaf32" => { + let a = get_param(decl, first_real_arg); + let b = get_param(decl, first_real_arg + 1u); + let c = get_param(decl, first_real_arg + 2u); + let fmaf = *ccx.intrinsics.get(&~"llvm.fma.f32"); + Store(bcx, Call(bcx, fmaf, [a, b, c]), fcx.llretptr.get()); + } + ~"fmaf64" => { + let a = get_param(decl, first_real_arg); + let b = get_param(decl, first_real_arg + 1u); + let c = get_param(decl, first_real_arg + 2u); + let fmaf = *ccx.intrinsics.get(&~"llvm.fma.f64"); + Store(bcx, Call(bcx, fmaf, [a, b, c]), fcx.llretptr.get()); + } + ~"fabsf32" => { + let x = get_param(decl, first_real_arg); + let fabsf = *ccx.intrinsics.get(&~"llvm.fabs.f32"); + Store(bcx, Call(bcx, fabsf, [x]), fcx.llretptr.get()); + } + ~"fabsf64" => { + let x = get_param(decl, first_real_arg); + let fabsf = *ccx.intrinsics.get(&~"llvm.fabs.f64"); + Store(bcx, Call(bcx, fabsf, [x]), fcx.llretptr.get()); + } + ~"floorf32" => { + let x = get_param(decl, first_real_arg); + let floorf = *ccx.intrinsics.get(&~"llvm.floor.f32"); + Store(bcx, Call(bcx, floorf, [x]), fcx.llretptr.get()); + } + ~"floorf64" => { + let x = get_param(decl, first_real_arg); + let floorf = *ccx.intrinsics.get(&~"llvm.floor.f64"); + Store(bcx, Call(bcx, floorf, [x]), fcx.llretptr.get()); + } + ~"ceilf32" => { + let x = get_param(decl, first_real_arg); + let ceilf = *ccx.intrinsics.get(&~"llvm.ceil.f32"); + Store(bcx, Call(bcx, ceilf, [x]), fcx.llretptr.get()); + } + ~"ceilf64" => { + let x = get_param(decl, first_real_arg); + let ceilf = *ccx.intrinsics.get(&~"llvm.ceil.f64"); + Store(bcx, Call(bcx, ceilf, [x]), fcx.llretptr.get()); + } + ~"truncf32" => { + let x = get_param(decl, first_real_arg); + let truncf = *ccx.intrinsics.get(&~"llvm.trunc.f32"); + Store(bcx, Call(bcx, truncf, [x]), fcx.llretptr.get()); + } + ~"truncf64" => { + let x = get_param(decl, first_real_arg); + let truncf = *ccx.intrinsics.get(&~"llvm.trunc.f64"); + Store(bcx, Call(bcx, truncf, [x]), fcx.llretptr.get()); + } + ~"ctpop8" => { + let x = get_param(decl, first_real_arg); + let ctpop = *ccx.intrinsics.get(&~"llvm.ctpop.i8"); + Store(bcx, Call(bcx, ctpop, [x]), fcx.llretptr.get()) + } + ~"ctpop16" => { + let x = get_param(decl, first_real_arg); + let ctpop = *ccx.intrinsics.get(&~"llvm.ctpop.i16"); + Store(bcx, Call(bcx, ctpop, [x]), fcx.llretptr.get()) + } + ~"ctpop32" => { + let x = get_param(decl, first_real_arg); + let ctpop = *ccx.intrinsics.get(&~"llvm.ctpop.i32"); + Store(bcx, Call(bcx, ctpop, [x]), fcx.llretptr.get()) + } + ~"ctpop64" => { + let x = get_param(decl, first_real_arg); + let ctpop = *ccx.intrinsics.get(&~"llvm.ctpop.i64"); + Store(bcx, Call(bcx, ctpop, [x]), fcx.llretptr.get()) + } + ~"ctlz8" => { + let x = get_param(decl, first_real_arg); + let y = C_i1(false); + let ctlz = *ccx.intrinsics.get(&~"llvm.ctlz.i8"); + Store(bcx, Call(bcx, ctlz, [x, y]), fcx.llretptr.get()) + } + ~"ctlz16" => { + let x = get_param(decl, first_real_arg); + let y = C_i1(false); + let ctlz = *ccx.intrinsics.get(&~"llvm.ctlz.i16"); + Store(bcx, Call(bcx, ctlz, [x, y]), fcx.llretptr.get()) + } + ~"ctlz32" => { + let x = get_param(decl, first_real_arg); + let y = C_i1(false); + let ctlz = *ccx.intrinsics.get(&~"llvm.ctlz.i32"); + Store(bcx, Call(bcx, ctlz, [x, y]), fcx.llretptr.get()) + } + ~"ctlz64" => { + let x = get_param(decl, first_real_arg); + let y = C_i1(false); + let ctlz = *ccx.intrinsics.get(&~"llvm.ctlz.i64"); + Store(bcx, Call(bcx, ctlz, [x, y]), fcx.llretptr.get()) + } + ~"cttz8" => { + let x = get_param(decl, first_real_arg); + let y = C_i1(false); + let cttz = *ccx.intrinsics.get(&~"llvm.cttz.i8"); + Store(bcx, Call(bcx, cttz, [x, y]), fcx.llretptr.get()) + } + ~"cttz16" => { + let x = get_param(decl, first_real_arg); + let y = C_i1(false); + let cttz = *ccx.intrinsics.get(&~"llvm.cttz.i16"); + Store(bcx, Call(bcx, cttz, [x, y]), fcx.llretptr.get()) + } + ~"cttz32" => { + let x = get_param(decl, first_real_arg); + let y = C_i1(false); + let cttz = *ccx.intrinsics.get(&~"llvm.cttz.i32"); + Store(bcx, Call(bcx, cttz, [x, y]), fcx.llretptr.get()) + } + ~"cttz64" => { + let x = get_param(decl, first_real_arg); + let y = C_i1(false); + let cttz = *ccx.intrinsics.get(&~"llvm.cttz.i64"); + Store(bcx, Call(bcx, cttz, [x, y]), fcx.llretptr.get()) + } + ~"bswap16" => { + let x = get_param(decl, first_real_arg); + let cttz = *ccx.intrinsics.get(&~"llvm.bswap.i16"); + Store(bcx, Call(bcx, cttz, [x]), fcx.llretptr.get()) + } + ~"bswap32" => { + let x = get_param(decl, first_real_arg); + let cttz = *ccx.intrinsics.get(&~"llvm.bswap.i32"); + Store(bcx, Call(bcx, cttz, [x]), fcx.llretptr.get()) + } + ~"bswap64" => { + let x = get_param(decl, first_real_arg); + let cttz = *ccx.intrinsics.get(&~"llvm.bswap.i64"); + Store(bcx, Call(bcx, cttz, [x]), fcx.llretptr.get()) + } + _ => { + // Could we make this an enum rather than a string? does it get + // checked earlier? + ccx.sess.span_bug(item.span, "unknown intrinsic"); + } + } + build_return(bcx); + finish_fn(fcx, lltop); +} diff --git a/src/librustc/middle/trans/monomorphize.rs b/src/librustc/middle/trans/monomorphize.rs index 1b3150f14b208..e993b80761203 100644 --- a/src/librustc/middle/trans/monomorphize.rs +++ b/src/librustc/middle/trans/monomorphize.rs @@ -21,12 +21,12 @@ use middle::trans::base::{get_item_val, no_self}; use middle::trans::base; use middle::trans::common::*; use middle::trans::datum; -use middle::trans::foreign; use middle::trans::machine; use middle::trans::meth; use middle::trans::type_of::type_of_fn_from_ty; use middle::trans::type_of; use middle::trans::type_use; +use middle::trans::intrinsic; use middle::ty; use middle::ty::{FnSig}; use middle::typeck; @@ -211,8 +211,8 @@ pub fn monomorphic_fn(ccx: @CrateContext, } ast_map::node_foreign_item(i, _, _, _) => { let d = mk_lldecl(); - foreign::trans_intrinsic(ccx, d, i, pt, psubsts.get(), i.attrs, - ref_id); + intrinsic::trans_intrinsic(ccx, d, i, pt, psubsts.get(), i.attrs, + ref_id); d } ast_map::node_variant(ref v, enum_item, _) => { @@ -384,7 +384,7 @@ pub fn make_mono_id(ccx: @CrateContext, // Special value for nil to prevent problems // with undef return pointers. - if size <= 8u && ty::type_is_nil(subst) { + if size <= 8u && ty::type_is_voidish(subst) { mono_repr(0u, 0u, data_class, mode) } else { mono_repr(size, align, data_class, mode) diff --git a/src/librustc/middle/trans/reflect.rs b/src/librustc/middle/trans/reflect.rs index b8d38cf7701bc..8aea60278c408 100644 --- a/src/librustc/middle/trans/reflect.rs +++ b/src/librustc/middle/trans/reflect.rs @@ -284,7 +284,7 @@ pub impl Reflector { sub_path, "get_disr"); - let llfty = type_of_fn(ccx, [opaqueptrty], ty::mk_int()); + let llfty = type_of_rust_fn(ccx, [opaqueptrty], ty::mk_int()); let llfdecl = decl_internal_cdecl_fn(ccx.llmod, sym, llfty); let arg = unsafe { llvm::LLVMGetParam(llfdecl, first_real_arg as c_uint) diff --git a/src/librustc/middle/trans/type_of.rs b/src/librustc/middle/trans/type_of.rs index bddcb9a1d7377..7e66a21b3acfe 100644 --- a/src/librustc/middle/trans/type_of.rs +++ b/src/librustc/middle/trans/type_of.rs @@ -16,35 +16,61 @@ use middle::trans::adt; use middle::trans::base; use middle::trans::common::*; use middle::trans::common; +use middle::trans::foreign; use middle::ty; use util::ppaux; use syntax::ast; -pub fn arg_is_indirect(_: @CrateContext, arg_ty: &ty::t) -> bool { - !ty::type_is_immediate(*arg_ty) +pub fn arg_is_indirect(_: @CrateContext, arg_ty: ty::t) -> bool { + !ty::type_is_immediate(arg_ty) } -pub fn type_of_explicit_arg(ccx: @CrateContext, arg_ty: &ty::t) -> TypeRef { - let llty = type_of(ccx, *arg_ty); +pub fn return_uses_outptr(ty: ty::t) -> bool { + !ty::type_is_immediate(ty) +} + +pub fn type_of_explicit_arg(ccx: @CrateContext, arg_ty: ty::t) -> TypeRef { + let llty = type_of(ccx, arg_ty); if arg_is_indirect(ccx, arg_ty) {T_ptr(llty)} else {llty} } pub fn type_of_explicit_args(ccx: @CrateContext, inputs: &[ty::t]) -> ~[TypeRef] { - inputs.map(|arg_ty| type_of_explicit_arg(ccx, arg_ty)) + inputs.map(|&arg_ty| type_of_explicit_arg(ccx, arg_ty)) +} + +// Given a function type and a count of ty params, construct an llvm type +pub fn type_of_fn_from_ty(cx: @CrateContext, fty: ty::t) -> TypeRef { + return match ty::get(fty).sty { + ty::ty_closure(ref f) => { + type_of_rust_fn(cx, f.sig.inputs, f.sig.output) + } + ty::ty_bare_fn(ref f) => { + if f.abis.is_rust() || f.abis.is_intrinsic() { + type_of_rust_fn(cx, f.sig.inputs, f.sig.output) + } else { + foreign::lltype_for_foreign_fn(cx, fty) + } + } + _ => { + cx.sess.bug("type_of_fn_from_ty given non-closure, non-bare-fn") + } + }; } -pub fn type_of_fn(cx: @CrateContext, inputs: &[ty::t], output: ty::t) - -> TypeRef { +pub fn type_of_rust_fn(cx: @CrateContext, + inputs: &[ty::t], + output: ty::t) -> TypeRef +{ unsafe { let mut atys: ~[TypeRef] = ~[]; // Arg 0: Output pointer. // (if the output type is non-immediate) - let output_is_immediate = ty::type_is_immediate(output); + let use_out_pointer = return_uses_outptr(output); let lloutputtype = type_of(cx, output); - if !output_is_immediate { + if use_out_pointer { atys.push(T_ptr(lloutputtype)); } else { // FIXME #6575: Eliminate this. @@ -58,7 +84,7 @@ pub fn type_of_fn(cx: @CrateContext, inputs: &[ty::t], output: ty::t) atys.push_all(type_of_explicit_args(cx, inputs)); // Use the output as the actual return value if it's immediate. - if output_is_immediate { + if !use_out_pointer { T_fn(atys, lloutputtype) } else { T_fn(atys, llvm::LLVMVoidType()) @@ -66,17 +92,6 @@ pub fn type_of_fn(cx: @CrateContext, inputs: &[ty::t], output: ty::t) } } -// Given a function type and a count of ty params, construct an llvm type -pub fn type_of_fn_from_ty(cx: @CrateContext, fty: ty::t) -> TypeRef { - match ty::get(fty).sty { - ty::ty_closure(ref f) => type_of_fn(cx, f.sig.inputs, f.sig.output), - ty::ty_bare_fn(ref f) => type_of_fn(cx, f.sig.inputs, f.sig.output), - _ => { - cx.sess.bug("type_of_fn_from_ty given non-closure, non-bare-fn") - } - } -} - pub fn type_of_non_gc_box(cx: @CrateContext, t: ty::t) -> TypeRef { assert!(!ty::type_needs_infer(t)); diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 6d1ae8ff0e6a8..213b7b2738c5a 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -1530,6 +1530,11 @@ pub fn subst(cx: ctxt, // Type utilities +pub fn type_is_voidish(ty: t) -> bool { + //! "nil" and "bot" are void types in that they represent 0 bits of information + type_is_nil(ty) || type_is_bot(ty) +} + pub fn type_is_nil(ty: t) -> bool { get(ty).sty == ty_nil } pub fn type_is_bot(ty: t) -> bool { diff --git a/src/librustc/rustc.rc b/src/librustc/rustc.rc index 056b4a9a49ef9..ae07f19030073 100644 --- a/src/librustc/rustc.rc +++ b/src/librustc/rustc.rc @@ -76,6 +76,7 @@ pub mod middle { pub mod cabi_arm; pub mod cabi_mips; pub mod foreign; + pub mod intrinsic; pub mod reflect; pub mod shape; pub mod debuginfo; diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index 9d74f6c7b0ed7..f98ffceb4a9be 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -801,3 +801,9 @@ impl UserString for ty::TraitRef { } } } + +impl Repr for AbiSet { + fn repr(&self, _tcx: ctxt) -> ~str { + self.to_str() + } +} diff --git a/src/rt/rust_log.cpp b/src/rt/rust_log.cpp index df24f569495b4..dd01a9ed9a0a4 100644 --- a/src/rt/rust_log.cpp +++ b/src/rt/rust_log.cpp @@ -131,12 +131,6 @@ rust_log::trace_ln(char *prefix, char *message) { void rust_log::trace_ln(rust_task *task, uint32_t level, char *message) { - - if (task) { - // There is not enough room to be logging on the rust stack - assert(!task->on_rust_stack() && "logging on rust stack"); - } - // FIXME (#2672): The scheduler and task names used to have meaning, // but they are always equal to 'main' currently #if 0 diff --git a/src/test/compile-fail/warn-foreign-int-types.rs b/src/test/compile-fail/warn-foreign-int-types.rs index e5c2603c56914..234040f721775 100644 --- a/src/test/compile-fail/warn-foreign-int-types.rs +++ b/src/test/compile-fail/warn-foreign-int-types.rs @@ -8,15 +8,15 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//error-pattern:libc::c_int or libc::c_long should be used +#[forbid(ctypes)]; + mod xx { pub extern { - pub fn strlen(str: *u8) -> uint; - pub fn foo(x: int, y: uint); + pub fn strlen(str: *u8) -> uint; //~ ERROR found rust type `uint` + pub fn foo(x: int, y: uint); //~ ERROR found rust type `int` + //~^ ERROR found rust type `uint` } } fn main() { - // let it fail to verify warning message - fail!() } diff --git a/src/test/run-pass/extern-return-TwoU16s.rs b/src/test/run-pass/extern-return-TwoU16s.rs index 3a345099bad0a..175fce97333d6 100644 --- a/src/test/run-pass/extern-return-TwoU16s.rs +++ b/src/test/run-pass/extern-return-TwoU16s.rs @@ -8,9 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// xfail-win32 #5745 -// xfail-macos Broken on mac i686 - struct TwoU16s { one: u16, two: u16 } diff --git a/src/test/run-pass/extern-return-TwoU8s.rs b/src/test/run-pass/extern-return-TwoU8s.rs index c1f897e79b00b..4216bc80dba44 100644 --- a/src/test/run-pass/extern-return-TwoU8s.rs +++ b/src/test/run-pass/extern-return-TwoU8s.rs @@ -8,9 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// xfail-win32 #5745 -// xfail-macos Broken on mac i686 - struct TwoU8s { one: u8, two: u8 } diff --git a/src/test/run-pass/morestack6.rs b/src/test/run-pass/morestack6.rs index 79c66ba72b0c7..c6ac42fa6ad4c 100644 --- a/src/test/run-pass/morestack6.rs +++ b/src/test/run-pass/morestack6.rs @@ -11,6 +11,8 @@ // This test attempts to force the dynamic linker to resolve // external symbols as close to the red zone as possible. +use std::libc; + mod rustrt { pub extern { pub fn debug_get_stk_seg() -> *u8;