1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use back::{link, abi};
13 use lib::llvm::{Pointer, ValueRef};
15 use middle::trans::base::*;
16 use middle::trans::cabi;
17 use middle::trans::cabi_x86;
18 use middle::trans::cabi_x86_64;
19 use middle::trans::cabi_arm;
20 use middle::trans::cabi_mips;
21 use middle::trans::build::*;
22 use middle::trans::callee::*;
23 use middle::trans::common::*;
24 use middle::trans::datum::*;
25 use middle::trans::expr::Ignore;
26 use middle::trans::machine::llsize_of;
27 use middle::trans::glue;
28 use middle::trans::machine;
29 use middle::trans::type_of::*;
30 use middle::trans::type_of;
32 use middle::ty::FnSig;
33 use util::ppaux::ty_to_str;
37 use syntax::codemap::span;
38 use syntax::{ast, ast_util};
39 use syntax::{attr, ast_map};
41 use syntax::parse::token::special_idents;
42 use syntax::parse::token;
43 use syntax::abi::{X86, X86_64, Arm, Mips};
44 use syntax::abi::{RustIntrinsic, Rust, Stdcall, Fastcall,
46 use middle::trans::type_::Type;
48 fn abi_info(ccx: @mut CrateContext) -> @cabi::ABIInfo {
49 return match ccx.sess.targ_cfg.arch {
50 X86 => cabi_x86::abi_info(ccx),
51 X86_64 => cabi_x86_64::abi_info(),
52 Arm => cabi_arm::abi_info(),
53 Mips => cabi_mips::abi_info(),
57 pub fn link_name(ccx: &CrateContext, i: &ast::foreign_item) -> @str {
58 match attr::first_attr_value_str_by_name(i.attrs, "link_name") {
59 None => ccx.sess.str_of(i.ident),
67 /// LLVM types that will appear on the foreign function
70 /// True if there is a return value (not bottom, not unit)
73 /// Type of the struct we will use to shuttle values back and forth.
74 /// This is always derived from the llsig.
77 /// Type of the shim function itself.
80 /// Adapter object for handling native ABI rules (trust me, you
81 /// don't want to know).
85 struct LlvmSignature {
91 fn foreign_signature(ccx: &mut CrateContext, fn_sig: &ty::FnSig)
94 * The ForeignSignature is the LLVM types of the arguments/return type
95 * of a function. Note that these LLVM types are not quite the same
96 * as the LLVM types would be for a native Rust function because foreign
97 * functions just plain ignore modes. They also don't pass aggregate
98 * values by pointer like we do.
101 let llarg_tys = fn_sig.inputs.map(|arg_ty| type_of(ccx, *arg_ty));
102 let llret_ty = type_of::type_of(ccx, fn_sig.output);
104 llarg_tys: llarg_tys,
106 sret: !ty::type_is_immediate(ccx.tcx, fn_sig.output),
110 fn shim_types(ccx: @mut CrateContext, id: ast::NodeId) -> ShimTypes {
111 let fn_sig = match ty::get(ty::node_id_to_type(ccx.tcx, id)).sty {
112 ty::ty_bare_fn(ref fn_ty) => fn_ty.sig.clone(),
113 _ => ccx.sess.bug("c_arg_and_ret_lltys called on non-function type")
115 let llsig = foreign_signature(ccx, &fn_sig);
116 let bundle_ty = Type::struct_(llsig.llarg_tys + &[llsig.llret_ty.ptr_to()], false);
117 let ret_def = !ty::type_is_bot(fn_sig.output) &&
118 !ty::type_is_nil(fn_sig.output);
119 let fn_ty = abi_info(ccx).compute_info(llsig.llarg_tys, llsig.llret_ty, ret_def);
124 bundle_ty: bundle_ty,
125 shim_fn_ty: Type::func([bundle_ty.ptr_to()], &Type::void()),
130 type shim_arg_builder<'self> =
131 &'self fn(bcx: @mut Block, tys: &ShimTypes,
132 llargbundle: ValueRef) -> ~[ValueRef];
134 type shim_ret_builder<'self> =
135 &'self fn(bcx: @mut Block, tys: &ShimTypes,
136 llargbundle: ValueRef,
139 fn build_shim_fn_(ccx: @mut CrateContext,
143 cc: lib::llvm::CallConv,
144 arg_builder: shim_arg_builder,
145 ret_builder: shim_ret_builder)
147 let llshimfn = decl_internal_cdecl_fn(
148 ccx.llmod, shim_name, tys.shim_fn_ty);
150 // Declare the body of the shim function:
151 let fcx = new_fn_ctxt(ccx, ~[], llshimfn, tys.fn_sig.output, None);
152 let bcx = fcx.entry_bcx.unwrap();
154 let llargbundle = get_param(llshimfn, 0u);
155 let llargvals = arg_builder(bcx, tys, llargbundle);
157 // Create the call itself and store the return value:
158 let llretval = CallWithConv(bcx, llbasefn, llargvals, cc);
160 ret_builder(bcx, tys, llargbundle, llretval);
162 // Don't finish up the function in the usual way, because this doesn't
163 // follow the normal Rust calling conventions.
164 let ret_cx = match fcx.llreturn {
165 Some(llreturn) => raw_block(fcx, false, llreturn),
174 type wrap_arg_builder<'self> = &'self fn(bcx: @mut Block,
177 llargbundle: ValueRef);
179 type wrap_ret_builder<'self> = &'self fn(bcx: @mut Block,
181 llargbundle: ValueRef);
183 fn build_wrap_fn_(ccx: @mut CrateContext,
187 shim_upcall: ValueRef,
188 needs_c_return: bool,
189 arg_builder: wrap_arg_builder,
190 ret_builder: wrap_ret_builder) {
191 let _icx = push_ctxt("foreign::build_wrap_fn_");
192 let fcx = new_fn_ctxt(ccx, ~[], llwrapfn, tys.fn_sig.output, None);
193 let bcx = fcx.entry_bcx.unwrap();
195 // Patch up the return type if it's not immediate and we're returning via
197 if needs_c_return && !ty::type_is_immediate(ccx.tcx, tys.fn_sig.output) {
198 let lloutputtype = type_of::type_of(fcx.ccx, tys.fn_sig.output);
199 fcx.llretptr = Some(alloca(bcx, lloutputtype, ""));
202 // Allocate the struct and write the arguments into it.
203 let llargbundle = alloca(bcx, tys.bundle_ty, "__llargbundle");
204 arg_builder(bcx, tys, llwrapfn, llargbundle);
206 // Create call itself.
207 let llshimfnptr = PointerCast(bcx, llshimfn, Type::i8p());
208 let llrawargbundle = PointerCast(bcx, llargbundle, Type::i8p());
209 Call(bcx, shim_upcall, [llrawargbundle, llshimfnptr]);
210 ret_builder(bcx, tys, llargbundle);
212 // Then return according to the C ABI.
213 let return_context = match fcx.llreturn {
214 Some(llreturn) => raw_block(fcx, false, llreturn),
218 let llfunctiontype = val_ty(llwrapfn);
219 let llfunctiontype = llfunctiontype.element_type();
220 let return_type = llfunctiontype.return_type();
221 if return_type.kind() == ::lib::llvm::Void {
222 // XXX: This might be wrong if there are any functions for which
223 // the C ABI specifies a void output pointer and the Rust ABI
225 RetVoid(return_context);
227 // Cast if we have to...
228 // XXX: This is ugly.
229 let llretptr = BitCast(return_context, fcx.llretptr.unwrap(), return_type.ptr_to());
230 Ret(return_context, Load(return_context, llretptr));
235 // For each foreign function F, we generate a wrapper function W and a shim
236 // function S that all work together. The wrapper function W is the function
237 // that other rust code actually invokes. Its job is to marshall the
238 // arguments into a struct. It then uses a small bit of assembly to switch
239 // over to the C stack and invoke the shim function. The shim function S then
240 // unpacks the arguments from the struct and invokes the actual function F
241 // according to its specified calling convention.
243 // Example: Given a foreign c-stack function F(x: X, y: Y) -> Z,
244 // we generate a wrapper function W that looks like:
246 // void W(Z* dest, void *env, X x, Y y) {
247 // struct { X x; Y y; Z *z; } args = { x, y, z };
248 // call_on_c_stack_shim(S, &args);
251 // The shim function S then looks something like:
253 // void S(struct { X x; Y y; Z *z; } *args) {
254 // *args->z = F(args->x, args->y);
257 // However, if the return type of F is dynamically sized or of aggregate type,
258 // the shim function looks like:
260 // void S(struct { X x; Y y; Z *z; } *args) {
261 // F(args->z, args->x, args->y);
264 // Note: on i386, the layout of the args struct is generally the same
265 // as the desired layout of the arguments on the C stack. Therefore,
266 // we could use upcall_alloc_c_stack() to allocate the `args`
267 // structure and switch the stack pointer appropriately to avoid a
268 // round of copies. (In fact, the shim function itself is
269 // unnecessary). We used to do this, in fact, and will perhaps do so
271 pub fn trans_foreign_mod(ccx: @mut CrateContext,
272 path: &ast_map::path,
273 foreign_mod: &ast::foreign_mod) {
274 let _icx = push_ctxt("foreign::trans_foreign_mod");
276 let arch = ccx.sess.targ_cfg.arch;
277 let abi = match foreign_mod.abis.for_arch(arch) {
280 fmt!("No suitable ABI for target architecture \
282 ast_map::path_to_str(*path,
289 for &foreign_item in foreign_mod.items.iter() {
290 match foreign_item.node {
291 ast::foreign_item_fn(*) => {
292 let id = foreign_item.id;
295 // Intrinsics are emitted by monomorphic fn
299 // FIXME(#3678) Implement linking to foreign fns with Rust ABI
301 fmt!("Foreign functions with Rust ABI"));
305 build_foreign_fn(ccx, id, foreign_item,
306 lib::llvm::X86StdcallCallConv);
310 build_foreign_fn(ccx, id, foreign_item,
311 lib::llvm::X86FastcallCallConv);
315 // FIXME(#3678) should really be more specific
316 build_foreign_fn(ccx, id, foreign_item,
317 lib::llvm::CCallConv);
321 // FIXME(#3678) should really be more specific
322 build_foreign_fn(ccx, id, foreign_item,
323 lib::llvm::CCallConv);
327 build_foreign_fn(ccx, id, foreign_item,
328 lib::llvm::CCallConv);
332 ast::foreign_item_static(*) => {
333 let ident = token::ident_to_str(&foreign_item.ident);
334 ccx.item_symbols.insert(foreign_item.id, /* bad */ident.to_owned());
339 fn build_foreign_fn(ccx: @mut CrateContext,
341 foreign_item: @ast::foreign_item,
342 cc: lib::llvm::CallConv) {
343 let llwrapfn = get_item_val(ccx, id);
344 let tys = shim_types(ccx, id);
345 if attr::contains_name(foreign_item.attrs, "rust_stack") {
346 build_direct_fn(ccx, llwrapfn, foreign_item,
348 } else if attr::contains_name(foreign_item.attrs, "fast_ffi") {
349 build_fast_ffi_fn(ccx, llwrapfn, foreign_item, &tys, cc);
351 let llshimfn = build_shim_fn(ccx, foreign_item, &tys, cc);
352 build_wrap_fn(ccx, &tys, llshimfn, llwrapfn);
356 fn build_shim_fn(ccx: @mut CrateContext,
357 foreign_item: &ast::foreign_item,
359 cc: lib::llvm::CallConv)
363 * Build S, from comment above:
365 * void S(struct { X x; Y y; Z *z; } *args) {
366 * F(args->z, args->x, args->y);
370 let _icx = push_ctxt("foreign::build_shim_fn");
372 fn build_args(bcx: @mut Block, tys: &ShimTypes, llargbundle: ValueRef)
374 let _icx = push_ctxt("foreign::shim::build_args");
375 tys.fn_ty.build_shim_args(bcx, tys.llsig.llarg_tys, llargbundle)
378 fn build_ret(bcx: @mut Block,
380 llargbundle: ValueRef,
381 llretval: ValueRef) {
382 let _icx = push_ctxt("foreign::shim::build_ret");
383 tys.fn_ty.build_shim_ret(bcx,
390 let lname = link_name(ccx, foreign_item);
391 let llbasefn = base_fn(ccx, lname, tys, cc);
392 // Name the shim function
393 let shim_name = fmt!("%s__c_stack_shim", lname);
403 fn base_fn(ccx: &CrateContext,
406 cc: lib::llvm::CallConv)
408 // Declare the "prototype" for the base function F:
409 do tys.fn_ty.decl_fn |fnty| {
410 decl_fn(ccx.llmod, lname, cc, fnty)
414 // FIXME (#2535): this is very shaky and probably gets ABIs wrong all
416 fn build_direct_fn(ccx: @mut CrateContext,
418 item: &ast::foreign_item,
420 cc: lib::llvm::CallConv) {
421 debug!("build_direct_fn(%s)", link_name(ccx, item));
423 let fcx = new_fn_ctxt(ccx, ~[], decl, tys.fn_sig.output, None);
424 let bcx = fcx.entry_bcx.unwrap();
425 let llbasefn = base_fn(ccx, link_name(ccx, item), tys, cc);
426 let ty = ty::lookup_item_type(ccx.tcx,
427 ast_util::local_def(item.id)).ty;
428 let ret_ty = ty::ty_fn_ret(ty);
429 let args = vec::from_fn(ty::ty_fn_args(ty).len(), |i| {
430 get_param(decl, fcx.arg_pos(i))
432 let retval = Call(bcx, llbasefn, args);
433 if !ty::type_is_nil(ret_ty) && !ty::type_is_bot(ret_ty) {
434 Store(bcx, retval, fcx.llretptr.unwrap());
439 // FIXME (#2535): this is very shaky and probably gets ABIs wrong all
441 fn build_fast_ffi_fn(ccx: @mut CrateContext,
443 item: &ast::foreign_item,
445 cc: lib::llvm::CallConv) {
446 debug!("build_fast_ffi_fn(%s)", link_name(ccx, item));
448 let fcx = new_fn_ctxt(ccx, ~[], decl, tys.fn_sig.output, None);
449 let bcx = fcx.entry_bcx.unwrap();
450 let llbasefn = base_fn(ccx, link_name(ccx, item), tys, cc);
451 set_no_inline(fcx.llfn);
452 set_fixed_stack_segment(fcx.llfn);
453 let ty = ty::lookup_item_type(ccx.tcx,
454 ast_util::local_def(item.id)).ty;
455 let ret_ty = ty::ty_fn_ret(ty);
456 let args = vec::from_fn(ty::ty_fn_args(ty).len(), |i| {
457 get_param(decl, fcx.arg_pos(i))
459 let retval = Call(bcx, llbasefn, args);
460 if !ty::type_is_nil(ret_ty) && !ty::type_is_bot(ret_ty) {
461 Store(bcx, retval, fcx.llretptr.unwrap());
466 fn build_wrap_fn(ccx: @mut CrateContext,
469 llwrapfn: ValueRef) {
472 * Build W, from comment above:
474 * void W(Z* dest, void *env, X x, Y y) {
475 * struct { X x; Y y; Z *z; } args = { x, y, z };
476 * call_on_c_stack_shim(S, &args);
479 * One thing we have to be very careful of is to
480 * account for the Rust modes.
483 let _icx = push_ctxt("foreign::build_wrap_fn");
489 ccx.upcalls.call_shim_on_c_stack,
494 fn build_args(bcx: @mut Block,
497 llargbundle: ValueRef) {
498 let _icx = push_ctxt("foreign::wrap::build_args");
500 let n = tys.llsig.llarg_tys.len();
501 for i in range(0u, n) {
502 let arg_i = bcx.fcx.arg_pos(i);
503 let mut llargval = get_param(llwrapfn, arg_i);
505 // In some cases, Rust will pass a pointer which the
506 // native C type doesn't have. In that case, just
507 // load the value from the pointer.
508 if type_of::arg_is_indirect(ccx, &tys.fn_sig.inputs[i]) {
509 llargval = Load(bcx, llargval);
512 store_inbounds(bcx, llargval, llargbundle, [0u, i]);
515 for &retptr in bcx.fcx.llretptr.iter() {
516 store_inbounds(bcx, retptr, llargbundle, [0u, n]);
520 fn build_ret(bcx: @mut Block,
521 shim_types: &ShimTypes,
522 llargbundle: ValueRef) {
523 let _icx = push_ctxt("foreign::wrap::build_ret");
524 let arg_count = shim_types.fn_sig.inputs.len();
525 for &retptr in bcx.fcx.llretptr.iter() {
526 let llretptr = load_inbounds(bcx, llargbundle, [0, arg_count]);
527 Store(bcx, Load(bcx, llretptr), retptr);
533 pub fn trans_intrinsic(ccx: @mut CrateContext,
535 item: &ast::foreign_item,
537 substs: @param_substs,
538 attributes: &[ast::Attribute],
539 ref_id: Option<ast::NodeId>) {
540 debug!("trans_intrinsic(item.ident=%s)", ccx.sess.str_of(item.ident));
542 fn simple_llvm_intrinsic(bcx: @mut Block, name: &'static str, num_args: uint) {
543 assert!(num_args <= 4);
544 let mut args = [0 as ValueRef, ..4];
545 let first_real_arg = bcx.fcx.arg_pos(0u);
546 for i in range(0u, num_args) {
547 args[i] = get_param(bcx.fcx.llfn, first_real_arg + i);
549 let llfn = bcx.ccx().intrinsics.get_copy(&name);
550 Ret(bcx, Call(bcx, llfn, args.slice(0, num_args)));
553 fn memcpy_intrinsic(bcx: @mut Block, name: &'static str, tp_ty: ty::t, sizebits: u8) {
555 let lltp_ty = type_of::type_of(ccx, tp_ty);
556 let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32);
557 let size = match sizebits {
558 32 => C_i32(machine::llsize_of_real(ccx, lltp_ty) as i32),
559 64 => C_i64(machine::llsize_of_real(ccx, lltp_ty) as i64),
560 _ => ccx.sess.fatal("Invalid value for sizebits")
563 let decl = bcx.fcx.llfn;
564 let first_real_arg = bcx.fcx.arg_pos(0u);
565 let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), Type::i8p());
566 let src_ptr = PointerCast(bcx, get_param(decl, first_real_arg + 1), Type::i8p());
567 let count = get_param(decl, first_real_arg + 2);
568 let volatile = C_i1(false);
569 let llfn = bcx.ccx().intrinsics.get_copy(&name);
570 Call(bcx, llfn, [dst_ptr, src_ptr, Mul(bcx, size, count), align, volatile]);
574 fn memset_intrinsic(bcx: @mut Block, name: &'static str, tp_ty: ty::t, sizebits: u8) {
576 let lltp_ty = type_of::type_of(ccx, tp_ty);
577 let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32);
578 let size = match sizebits {
579 32 => C_i32(machine::llsize_of_real(ccx, lltp_ty) as i32),
580 64 => C_i64(machine::llsize_of_real(ccx, lltp_ty) as i64),
581 _ => ccx.sess.fatal("Invalid value for sizebits")
584 let decl = bcx.fcx.llfn;
585 let first_real_arg = bcx.fcx.arg_pos(0u);
586 let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), Type::i8p());
587 let val = get_param(decl, first_real_arg + 1);
588 let count = get_param(decl, first_real_arg + 2);
589 let volatile = C_i1(false);
590 let llfn = bcx.ccx().intrinsics.get_copy(&name);
591 Call(bcx, llfn, [dst_ptr, val, Mul(bcx, size, count), align, volatile]);
595 fn count_zeros_intrinsic(bcx: @mut Block, name: &'static str) {
596 let x = get_param(bcx.fcx.llfn, bcx.fcx.arg_pos(0u));
598 let llfn = bcx.ccx().intrinsics.get_copy(&name);
599 Ret(bcx, Call(bcx, llfn, [x, y]));
602 let output_type = ty::ty_fn_ret(ty::node_id_to_type(ccx.tcx, item.id));
604 let fcx = new_fn_ctxt_w_id(ccx,
614 set_always_inline(fcx.llfn);
616 // Set the fixed stack segment flag if necessary.
617 if attr::contains_name(attributes, "fixed_stack_segment") {
618 set_fixed_stack_segment(fcx.llfn);
621 let mut bcx = fcx.entry_bcx.unwrap();
622 let first_real_arg = fcx.arg_pos(0u);
624 let nm = ccx.sess.str_of(item.ident);
625 let name = nm.as_slice();
627 // This requires that atomic intrinsics follow a specific naming pattern:
628 // "atomic_<operation>[_<ordering>], and no ordering means SeqCst
629 if name.starts_with("atomic_") {
630 let split : ~[&str] = name.split_iter('_').collect();
631 assert!(split.len() >= 2, "Atomic intrinsic not correct format");
632 let order = if split.len() == 2 {
633 lib::llvm::SequentiallyConsistent
636 "relaxed" => lib::llvm::Monotonic,
637 "acq" => lib::llvm::Acquire,
638 "rel" => lib::llvm::Release,
639 "acqrel" => lib::llvm::AcquireRelease,
640 _ => ccx.sess.fatal("Unknown ordering in atomic intrinsic")
646 let old = AtomicCmpXchg(bcx, get_param(decl, first_real_arg),
647 get_param(decl, first_real_arg + 1u),
648 get_param(decl, first_real_arg + 2u),
653 let old = AtomicLoad(bcx, get_param(decl, first_real_arg),
658 AtomicStore(bcx, get_param(decl, first_real_arg + 1u),
659 get_param(decl, first_real_arg),
664 AtomicFence(bcx, order);
668 // These are all AtomicRMW ops
669 let atom_op = match op {
670 "xchg" => lib::llvm::Xchg,
671 "xadd" => lib::llvm::Add,
672 "xsub" => lib::llvm::Sub,
673 "and" => lib::llvm::And,
674 "nand" => lib::llvm::Nand,
675 "or" => lib::llvm::Or,
676 "xor" => lib::llvm::Xor,
677 "max" => lib::llvm::Max,
678 "min" => lib::llvm::Min,
679 "umax" => lib::llvm::UMax,
680 "umin" => lib::llvm::UMin,
681 _ => ccx.sess.fatal("Unknown atomic operation")
684 let old = AtomicRMW(bcx, atom_op, get_param(decl, first_real_arg),
685 get_param(decl, first_real_arg + 1u),
697 let tp_ty = substs.tys[0];
698 let lltp_ty = type_of::type_of(ccx, tp_ty);
699 Ret(bcx, C_uint(ccx, machine::llsize_of_real(ccx, lltp_ty)));
702 // Create a datum reflecting the value being moved.
703 // Use `appropriate_mode` so that the datum is by ref
704 // if the value is non-immediate. Note that, with
705 // intrinsics, there are no argument cleanups to
706 // concern ourselves with.
707 let tp_ty = substs.tys[0];
708 let mode = appropriate_mode(ccx.tcx, tp_ty);
709 let src = Datum {val: get_param(decl, first_real_arg + 1u),
710 ty: tp_ty, mode: mode};
711 bcx = src.move_to(bcx, DROP_EXISTING,
712 get_param(decl, first_real_arg));
716 // See comments for `"move_val"`.
717 let tp_ty = substs.tys[0];
718 let mode = appropriate_mode(ccx.tcx, tp_ty);
719 let src = Datum {val: get_param(decl, first_real_arg + 1u),
720 ty: tp_ty, mode: mode};
721 bcx = src.move_to(bcx, INIT, get_param(decl, first_real_arg));
725 let tp_ty = substs.tys[0];
726 let lltp_ty = type_of::type_of(ccx, tp_ty);
727 Ret(bcx, C_uint(ccx, machine::llalign_of_min(ccx, lltp_ty)));
730 let tp_ty = substs.tys[0];
731 let lltp_ty = type_of::type_of(ccx, tp_ty);
732 Ret(bcx, C_uint(ccx, machine::llalign_of_pref(ccx, lltp_ty)));
735 let tp_ty = substs.tys[0];
736 let static_ti = get_tydesc(ccx, tp_ty);
737 glue::lazily_emit_all_tydesc_glue(ccx, static_ti);
739 // FIXME (#3730): ideally this shouldn't need a cast,
740 // but there's a circularity between translating rust types to llvm
741 // types and having a tydesc type available. So I can't directly access
742 // the llvm type of intrinsic::TyDesc struct.
743 let userland_tydesc_ty = type_of::type_of(ccx, output_type);
744 let td = PointerCast(bcx, static_ti.tydesc, userland_tydesc_ty);
748 let tp_ty = substs.tys[0];
749 let lltp_ty = type_of::type_of(ccx, tp_ty);
750 match bcx.fcx.llretptr {
751 Some(ptr) => { Store(bcx, C_null(lltp_ty), ptr); RetVoid(bcx); }
752 None if ty::type_is_nil(tp_ty) => RetVoid(bcx),
753 None => Ret(bcx, C_null(lltp_ty)),
757 // Do nothing, this is effectively a no-op
758 let retty = substs.tys[0];
759 if ty::type_is_immediate(ccx.tcx, retty) && !ty::type_is_nil(retty) {
761 Ret(bcx, lib::llvm::llvm::LLVMGetUndef(type_of(ccx, retty).to_ref()));
771 let (in_type, out_type) = (substs.tys[0], substs.tys[1]);
772 let llintype = type_of::type_of(ccx, in_type);
773 let llouttype = type_of::type_of(ccx, out_type);
775 let in_type_size = machine::llbitsize_of_real(ccx, llintype);
776 let out_type_size = machine::llbitsize_of_real(ccx, llouttype);
777 if in_type_size != out_type_size {
778 let sp = match ccx.tcx.items.get_copy(&ref_id.unwrap()) {
779 ast_map::node_expr(e) => e.span,
780 _ => fail!("transmute has non-expr arg"),
782 let pluralize = |n| if 1u == n { "" } else { "s" };
783 ccx.sess.span_fatal(sp,
784 fmt!("transmute called on types with \
785 different sizes: %s (%u bit%s) to \
787 ty_to_str(ccx.tcx, in_type),
789 pluralize(in_type_size),
790 ty_to_str(ccx.tcx, out_type),
792 pluralize(out_type_size)));
795 if !ty::type_is_nil(out_type) {
796 let llsrcval = get_param(decl, first_real_arg);
797 if ty::type_is_immediate(ccx.tcx, in_type) {
800 Store(bcx, llsrcval, PointerCast(bcx, llretptr, llintype.ptr_to()));
803 None => match (llintype.kind(), llouttype.kind()) {
804 (Pointer, other) | (other, Pointer) if other != Pointer => {
805 let tmp = Alloca(bcx, llouttype, "");
806 Store(bcx, llsrcval, PointerCast(bcx, tmp, llintype.ptr_to()));
807 Ret(bcx, Load(bcx, tmp));
809 _ => Ret(bcx, BitCast(bcx, llsrcval, llouttype))
812 } else if ty::type_is_immediate(ccx.tcx, out_type) {
813 let llsrcptr = PointerCast(bcx, llsrcval, llouttype.ptr_to());
814 Ret(bcx, Load(bcx, llsrcptr));
816 // NB: Do not use a Load and Store here. This causes massive
817 // code bloat when `transmute` is used on large structural
819 let lldestptr = fcx.llretptr.unwrap();
820 let lldestptr = PointerCast(bcx, lldestptr, Type::i8p());
821 let llsrcptr = PointerCast(bcx, llsrcval, Type::i8p());
823 let llsize = llsize_of(ccx, llintype);
824 call_memcpy(bcx, lldestptr, llsrcptr, llsize, 1);
832 let tp_ty = substs.tys[0];
833 Ret(bcx, C_bool(ty::type_needs_drop(ccx.tcx, tp_ty)));
835 "contains_managed" => {
836 let tp_ty = substs.tys[0];
837 Ret(bcx, C_bool(ty::type_contents(ccx.tcx, tp_ty).contains_managed()));
840 let td = get_param(decl, first_real_arg);
841 let visitor = get_param(decl, first_real_arg + 1u);
842 //let llvisitorptr = alloca(bcx, val_ty(visitor));
843 //Store(bcx, visitor, llvisitorptr);
844 let td = PointerCast(bcx, td, ccx.tydesc_type.ptr_to());
845 glue::call_tydesc_glue_full(bcx, visitor, td,
846 abi::tydesc_field_visit_glue, None);
850 let frameaddress = ccx.intrinsics.get_copy(& &"llvm.frameaddress");
851 let frameaddress_val = Call(bcx, frameaddress, [C_i32(0i32)]);
852 let star_u8 = ty::mk_imm_ptr(
854 ty::mk_mach_uint(ast::ty_u8));
855 let fty = ty::mk_closure(bcx.tcx(), ty::ClosureTy {
856 purity: ast::impure_fn,
857 sigil: ast::BorrowedSigil,
859 region: ty::re_bound(ty::br_anon(0)),
860 bounds: ty::EmptyBuiltinBounds(),
862 bound_lifetime_names: opt_vec::Empty,
863 inputs: ~[ star_u8 ],
867 let datum = Datum {val: get_param(decl, first_real_arg),
868 mode: ByRef(ZeroMem), ty: fty};
869 let arg_vals = ~[frameaddress_val];
870 bcx = trans_call_inner(
871 bcx, None, fty, ty::mk_nil(),
872 |bcx| Callee {bcx: bcx, data: Closure(datum)},
873 ArgVals(arg_vals), Some(Ignore), DontAutorefArg).bcx;
876 "morestack_addr" => {
877 // XXX This is a hack to grab the address of this particular
878 // native function. There should be a general in-language
880 let llfty = type_of_fn(bcx.ccx(), [], ty::mk_nil());
881 let morestack_addr = decl_cdecl_fn(
882 bcx.ccx().llmod, "__morestack", llfty);
883 let morestack_addr = PointerCast(bcx, morestack_addr, Type::nil().ptr_to());
884 Ret(bcx, morestack_addr);
887 let ptr = get_param(decl, first_real_arg);
888 let offset = get_param(decl, first_real_arg + 1);
889 Ret(bcx, GEP(bcx, ptr, [offset]));
891 "memcpy32" => memcpy_intrinsic(bcx, "llvm.memcpy.p0i8.p0i8.i32", substs.tys[0], 32),
892 "memcpy64" => memcpy_intrinsic(bcx, "llvm.memcpy.p0i8.p0i8.i64", substs.tys[0], 64),
893 "memmove32" => memcpy_intrinsic(bcx, "llvm.memmove.p0i8.p0i8.i32", substs.tys[0], 32),
894 "memmove64" => memcpy_intrinsic(bcx, "llvm.memmove.p0i8.p0i8.i64", substs.tys[0], 64),
895 "memset32" => memset_intrinsic(bcx, "llvm.memset.p0i8.i32", substs.tys[0], 32),
896 "memset64" => memset_intrinsic(bcx, "llvm.memset.p0i8.i64", substs.tys[0], 64),
897 "sqrtf32" => simple_llvm_intrinsic(bcx, "llvm.sqrt.f32", 1),
898 "sqrtf64" => simple_llvm_intrinsic(bcx, "llvm.sqrt.f64", 1),
899 "powif32" => simple_llvm_intrinsic(bcx, "llvm.powi.f32", 2),
900 "powif64" => simple_llvm_intrinsic(bcx, "llvm.powi.f64", 2),
901 "sinf32" => simple_llvm_intrinsic(bcx, "llvm.sin.f32", 1),
902 "sinf64" => simple_llvm_intrinsic(bcx, "llvm.sin.f64", 1),
903 "cosf32" => simple_llvm_intrinsic(bcx, "llvm.cos.f32", 1),
904 "cosf64" => simple_llvm_intrinsic(bcx, "llvm.cos.f64", 1),
905 "powf32" => simple_llvm_intrinsic(bcx, "llvm.pow.f32", 2),
906 "powf64" => simple_llvm_intrinsic(bcx, "llvm.pow.f64", 2),
907 "expf32" => simple_llvm_intrinsic(bcx, "llvm.exp.f32", 1),
908 "expf64" => simple_llvm_intrinsic(bcx, "llvm.exp.f64", 1),
909 "exp2f32" => simple_llvm_intrinsic(bcx, "llvm.exp2.f32", 1),
910 "exp2f64" => simple_llvm_intrinsic(bcx, "llvm.exp2.f64", 1),
911 "logf32" => simple_llvm_intrinsic(bcx, "llvm.log.f32", 1),
912 "logf64" => simple_llvm_intrinsic(bcx, "llvm.log.f64", 1),
913 "log10f32" => simple_llvm_intrinsic(bcx, "llvm.log10.f32", 1),
914 "log10f64" => simple_llvm_intrinsic(bcx, "llvm.log10.f64", 1),
915 "log2f32" => simple_llvm_intrinsic(bcx, "llvm.log2.f32", 1),
916 "log2f64" => simple_llvm_intrinsic(bcx, "llvm.log2.f64", 1),
917 "fmaf32" => simple_llvm_intrinsic(bcx, "llvm.fma.f32", 3),
918 "fmaf64" => simple_llvm_intrinsic(bcx, "llvm.fma.f64", 3),
919 "fabsf32" => simple_llvm_intrinsic(bcx, "llvm.fabs.f32", 1),
920 "fabsf64" => simple_llvm_intrinsic(bcx, "llvm.fabs.f64", 1),
921 "floorf32" => simple_llvm_intrinsic(bcx, "llvm.floor.f32", 1),
922 "floorf64" => simple_llvm_intrinsic(bcx, "llvm.floor.f64", 1),
923 "ceilf32" => simple_llvm_intrinsic(bcx, "llvm.ceil.f32", 1),
924 "ceilf64" => simple_llvm_intrinsic(bcx, "llvm.ceil.f64", 1),
925 "truncf32" => simple_llvm_intrinsic(bcx, "llvm.trunc.f32", 1),
926 "truncf64" => simple_llvm_intrinsic(bcx, "llvm.trunc.f64", 1),
927 "ctpop8" => simple_llvm_intrinsic(bcx, "llvm.ctpop.i8", 1),
928 "ctpop16" => simple_llvm_intrinsic(bcx, "llvm.ctpop.i16", 1),
929 "ctpop32" => simple_llvm_intrinsic(bcx, "llvm.ctpop.i32", 1),
930 "ctpop64" => simple_llvm_intrinsic(bcx, "llvm.ctpop.i64", 1),
931 "ctlz8" => count_zeros_intrinsic(bcx, "llvm.ctlz.i8"),
932 "ctlz16" => count_zeros_intrinsic(bcx, "llvm.ctlz.i16"),
933 "ctlz32" => count_zeros_intrinsic(bcx, "llvm.ctlz.i32"),
934 "ctlz64" => count_zeros_intrinsic(bcx, "llvm.ctlz.i64"),
935 "cttz8" => count_zeros_intrinsic(bcx, "llvm.cttz.i8"),
936 "cttz16" => count_zeros_intrinsic(bcx, "llvm.cttz.i16"),
937 "cttz32" => count_zeros_intrinsic(bcx, "llvm.cttz.i32"),
938 "cttz64" => count_zeros_intrinsic(bcx, "llvm.cttz.i64"),
939 "bswap16" => simple_llvm_intrinsic(bcx, "llvm.bswap.i16", 1),
940 "bswap32" => simple_llvm_intrinsic(bcx, "llvm.bswap.i32", 1),
941 "bswap64" => simple_llvm_intrinsic(bcx, "llvm.bswap.i64", 1),
943 // Could we make this an enum rather than a string? does it get
945 ccx.sess.span_bug(item.span, "unknown intrinsic");
952 * Translates a "crust" fn, meaning a Rust fn that can be called
953 * from C code. In this case, we have to perform some adaptation
954 * to (1) switch back to the Rust stack and (2) adapt the C calling
955 * convention to our own.
957 * Example: Given a crust fn F(x: X, y: Y) -> Z, we generate a
958 * Rust function R as normal:
960 * void R(Z* dest, void *env, X x, Y y) {...}
962 * and then we generate a wrapper function W that looks like:
965 * struct { X x; Y y; Z *z; } args = { x, y, z };
966 * call_on_c_stack_shim(S, &args);
969 * Note that the wrapper follows the foreign (typically "C") ABI.
970 * The wrapper is the actual "value" of the foreign fn. Finally,
971 * we generate a shim function S that looks like:
973 * void S(struct { X x; Y y; Z *z; } *args) {
974 * R(args->z, NULL, args->x, args->y);
977 pub fn trans_foreign_fn(ccx: @mut CrateContext,
983 let _icx = push_ctxt("foreign::build_foreign_fn");
985 fn build_rust_fn(ccx: @mut CrateContext,
986 path: &ast_map::path,
991 let _icx = push_ctxt("foreign::foreign::build_rust_fn");
992 let t = ty::node_id_to_type(ccx.tcx, id);
994 let ps = link::mangle_internal_name_by_path(
996 vec::append_one((*path).clone(),
998 special_idents::clownshoe_abi)));
999 let llty = type_of_fn_from_ty(ccx, t);
1000 let llfndecl = decl_internal_cdecl_fn(ccx.llmod, ps, llty);
1013 fn build_shim_fn(ccx: @mut CrateContext,
1014 path: ast_map::path,
1020 * Generate the shim S:
1022 * void S(struct { X x; Y y; Z *z; } *args) {
1023 * R(args->z, NULL, &args->x, args->y);
1026 * One complication is that we must adapt to the Rust
1027 * calling convention, which introduces indirection
1028 * in some cases. To demonstrate this, I wrote one of the
1029 * entries above as `&args->x`, because presumably `X` is
1030 * one of those types that is passed by pointer in Rust.
1033 let _icx = push_ctxt("foreign::foreign::build_shim_fn");
1035 fn build_args(bcx: @mut Block, tys: &ShimTypes, llargbundle: ValueRef)
1037 let _icx = push_ctxt("foreign::extern::shim::build_args");
1038 let ccx = bcx.ccx();
1039 let mut llargvals = ~[];
1041 let n = tys.fn_sig.inputs.len();
1043 if !ty::type_is_immediate(bcx.tcx(), tys.fn_sig.output) {
1044 let llretptr = load_inbounds(bcx, llargbundle, [0u, n]);
1045 llargvals.push(llretptr);
1048 let llenvptr = C_null(Type::opaque_box(bcx.ccx()).ptr_to());
1049 llargvals.push(llenvptr);
1051 // Get a pointer to the argument:
1052 let mut llargval = GEPi(bcx, llargbundle, [0u, i]);
1054 if !type_of::arg_is_indirect(ccx, &tys.fn_sig.inputs[i]) {
1055 // If Rust would pass this by value, load the value.
1056 llargval = Load(bcx, llargval);
1059 llargvals.push(llargval);
1065 fn build_ret(bcx: @mut Block,
1066 shim_types: &ShimTypes,
1067 llargbundle: ValueRef,
1068 llretval: ValueRef) {
1069 if bcx.fcx.llretptr.is_some() &&
1070 ty::type_is_immediate(bcx.tcx(), shim_types.fn_sig.output) {
1071 // Write the value into the argument bundle.
1072 let arg_count = shim_types.fn_sig.inputs.len();
1073 let llretptr = load_inbounds(bcx,
1076 Store(bcx, llretval, llretptr);
1078 // NB: The return pointer in the Rust ABI function is wired
1079 // directly into the return slot in the shim struct.
1083 let shim_name = link::mangle_internal_name_by_path(
1085 vec::append_one(path, ast_map::path_name(
1086 special_idents::clownshoe_stack_shim
1092 lib::llvm::CCallConv,
1097 fn build_wrap_fn(ccx: @mut CrateContext,
1103 * Generate the wrapper W:
1106 * struct { X x; Y y; Z *z; } args = { x, y, z };
1107 * call_on_c_stack_shim(S, &args);
1111 let _icx = push_ctxt("foreign::foreign::build_wrap_fn");
1117 ccx.upcalls.call_shim_on_rust_stack,
1122 fn build_args(bcx: @mut Block,
1125 llargbundle: ValueRef) {
1126 let _icx = push_ctxt("foreign::foreign::wrap::build_args");
1127 tys.fn_ty.build_wrap_args(bcx,
1133 fn build_ret(bcx: @mut Block, tys: &ShimTypes, llargbundle: ValueRef) {
1134 let _icx = push_ctxt("foreign::foreign::wrap::build_ret");
1135 tys.fn_ty.build_wrap_ret(bcx, tys.llsig.llarg_tys, llargbundle);
1139 let tys = shim_types(ccx, id);
1140 // The internal Rust ABI function - runs on the Rust stack
1142 let llrustfn = build_rust_fn(ccx, &path, decl, body, id);
1143 // The internal shim function - runs on the Rust stack
1144 let llshimfn = build_shim_fn(ccx, path, llrustfn, &tys);
1145 // The foreign C function - runs on the C stack
1146 build_wrap_fn(ccx, llshimfn, llwrapfn, &tys)
1149 pub fn register_foreign_fn(ccx: @mut CrateContext,
1152 node_id: ast::NodeId)
1154 let _icx = push_ctxt("foreign::register_foreign_fn");
1156 let sym = Cell::new(sym);
1158 let tys = shim_types(ccx, node_id);
1159 do tys.fn_ty.decl_fn |fnty| {
1160 register_fn_llvmty(ccx, sp, sym.take(), node_id, lib::llvm::CCallConv, fnty)