]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/foreign.rs
Fix bug in `match`ing struct patterns
[rust.git] / src / librustc / middle / trans / foreign.rs
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.
4 //
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.
10
11
12 use back::{link, abi};
13 use lib::llvm::{Pointer, ValueRef};
14 use lib;
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;
31 use middle::ty;
32 use middle::ty::FnSig;
33 use util::ppaux::ty_to_str;
34
35 use std::cell::Cell;
36 use std::vec;
37 use syntax::codemap::span;
38 use syntax::{ast, ast_util};
39 use syntax::{attr, ast_map};
40 use syntax::opt_vec;
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,
45                   Cdecl, Aapcs, C};
46 use middle::trans::type_::Type;
47
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(),
54     }
55 }
56
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),
60         Some(ln) => ln,
61     }
62 }
63
64 struct ShimTypes {
65     fn_sig: ty::FnSig,
66
67     /// LLVM types that will appear on the foreign function
68     llsig: LlvmSignature,
69
70     /// True if there is a return value (not bottom, not unit)
71     ret_def: bool,
72
73     /// Type of the struct we will use to shuttle values back and forth.
74     /// This is always derived from the llsig.
75     bundle_ty: Type,
76
77     /// Type of the shim function itself.
78     shim_fn_ty: Type,
79
80     /// Adapter object for handling native ABI rules (trust me, you
81     /// don't want to know).
82     fn_ty: cabi::FnType
83 }
84
85 struct LlvmSignature {
86     llarg_tys: ~[Type],
87     llret_ty: Type,
88     sret: bool,
89 }
90
91 fn foreign_signature(ccx: &mut CrateContext, fn_sig: &ty::FnSig)
92                      -> LlvmSignature {
93     /*!
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.
99      */
100
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);
103     LlvmSignature {
104         llarg_tys: llarg_tys,
105         llret_ty: llret_ty,
106         sret: !ty::type_is_immediate(ccx.tcx, fn_sig.output),
107     }
108 }
109
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")
114     };
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);
120     ShimTypes {
121         fn_sig: fn_sig,
122         llsig: llsig,
123         ret_def: ret_def,
124         bundle_ty: bundle_ty,
125         shim_fn_ty: Type::func([bundle_ty.ptr_to()], &Type::void()),
126         fn_ty: fn_ty
127     }
128 }
129
130 type shim_arg_builder<'self> =
131     &'self fn(bcx: @mut Block, tys: &ShimTypes,
132               llargbundle: ValueRef) -> ~[ValueRef];
133
134 type shim_ret_builder<'self> =
135     &'self fn(bcx: @mut Block, tys: &ShimTypes,
136               llargbundle: ValueRef,
137               llretval: ValueRef);
138
139 fn build_shim_fn_(ccx: @mut CrateContext,
140                   shim_name: &str,
141                   llbasefn: ValueRef,
142                   tys: &ShimTypes,
143                   cc: lib::llvm::CallConv,
144                   arg_builder: shim_arg_builder,
145                   ret_builder: shim_ret_builder)
146                -> ValueRef {
147     let llshimfn = decl_internal_cdecl_fn(
148         ccx.llmod, shim_name, tys.shim_fn_ty);
149
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();
153
154     let llargbundle = get_param(llshimfn, 0u);
155     let llargvals = arg_builder(bcx, tys, llargbundle);
156
157     // Create the call itself and store the return value:
158     let llretval = CallWithConv(bcx, llbasefn, llargvals, cc);
159
160     ret_builder(bcx, tys, llargbundle, llretval);
161
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),
166         None => bcx
167     };
168     RetVoid(ret_cx);
169     fcx.cleanup();
170
171     return llshimfn;
172 }
173
174 type wrap_arg_builder<'self> = &'self fn(bcx: @mut Block,
175                                          tys: &ShimTypes,
176                                          llwrapfn: ValueRef,
177                                          llargbundle: ValueRef);
178
179 type wrap_ret_builder<'self> = &'self fn(bcx: @mut Block,
180                                          tys: &ShimTypes,
181                                          llargbundle: ValueRef);
182
183 fn build_wrap_fn_(ccx: @mut CrateContext,
184                   tys: &ShimTypes,
185                   llshimfn: ValueRef,
186                   llwrapfn: ValueRef,
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();
194
195     // Patch up the return type if it's not immediate and we're returning via
196     // the C ABI.
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, ""));
200     }
201
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);
205
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);
211
212     // Then return according to the C ABI.
213     let return_context = match fcx.llreturn {
214         Some(llreturn) => raw_block(fcx, false, llreturn),
215         None => bcx
216     };
217
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
224         // does not.
225         RetVoid(return_context);
226     } else {
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));
231     }
232     fcx.cleanup();
233 }
234
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.
242 //
243 // Example: Given a foreign c-stack function F(x: X, y: Y) -> Z,
244 // we generate a wrapper function W that looks like:
245 //
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);
249 //    }
250 //
251 // The shim function S then looks something like:
252 //
253 //     void S(struct { X x; Y y; Z *z; } *args) {
254 //         *args->z = F(args->x, args->y);
255 //     }
256 //
257 // However, if the return type of F is dynamically sized or of aggregate type,
258 // the shim function looks like:
259 //
260 //     void S(struct { X x; Y y; Z *z; } *args) {
261 //         F(args->z, args->x, args->y);
262 //     }
263 //
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
270 // in the future.
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");
275
276     let arch = ccx.sess.targ_cfg.arch;
277     let abi = match foreign_mod.abis.for_arch(arch) {
278         None => {
279             ccx.sess.fatal(
280                 fmt!("No suitable ABI for target architecture \
281                       in module %s",
282                      ast_map::path_to_str(*path,
283                                           ccx.sess.intr())));
284         }
285
286         Some(abi) => abi,
287     };
288
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;
293                 match abi {
294                     RustIntrinsic => {
295                         // Intrinsics are emitted by monomorphic fn
296                     }
297
298                     Rust => {
299                         // FIXME(#3678) Implement linking to foreign fns with Rust ABI
300                         ccx.sess.unimpl(
301                             fmt!("Foreign functions with Rust ABI"));
302                     }
303
304                     Stdcall => {
305                         build_foreign_fn(ccx, id, foreign_item,
306                                          lib::llvm::X86StdcallCallConv);
307                     }
308
309                     Fastcall => {
310                         build_foreign_fn(ccx, id, foreign_item,
311                                          lib::llvm::X86FastcallCallConv);
312                     }
313
314                     Cdecl => {
315                         // FIXME(#3678) should really be more specific
316                         build_foreign_fn(ccx, id, foreign_item,
317                                          lib::llvm::CCallConv);
318                     }
319
320                     Aapcs => {
321                         // FIXME(#3678) should really be more specific
322                         build_foreign_fn(ccx, id, foreign_item,
323                                          lib::llvm::CCallConv);
324                     }
325
326                     C => {
327                         build_foreign_fn(ccx, id, foreign_item,
328                                          lib::llvm::CCallConv);
329                     }
330                 }
331             }
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());
335             }
336         }
337     }
338
339     fn build_foreign_fn(ccx: @mut CrateContext,
340                         id: ast::NodeId,
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,
347                             &tys, cc);
348         } else if attr::contains_name(foreign_item.attrs, "fast_ffi") {
349             build_fast_ffi_fn(ccx, llwrapfn, foreign_item, &tys, cc);
350         } else {
351             let llshimfn = build_shim_fn(ccx, foreign_item, &tys, cc);
352             build_wrap_fn(ccx, &tys, llshimfn, llwrapfn);
353         }
354     }
355
356     fn build_shim_fn(ccx: @mut CrateContext,
357                      foreign_item: &ast::foreign_item,
358                      tys: &ShimTypes,
359                      cc: lib::llvm::CallConv)
360                   -> ValueRef {
361         /*!
362          *
363          * Build S, from comment above:
364          *
365          *     void S(struct { X x; Y y; Z *z; } *args) {
366          *         F(args->z, args->x, args->y);
367          *     }
368          */
369
370         let _icx = push_ctxt("foreign::build_shim_fn");
371
372         fn build_args(bcx: @mut Block, tys: &ShimTypes, llargbundle: ValueRef)
373                    -> ~[ValueRef] {
374             let _icx = push_ctxt("foreign::shim::build_args");
375             tys.fn_ty.build_shim_args(bcx, tys.llsig.llarg_tys, llargbundle)
376         }
377
378         fn build_ret(bcx: @mut Block,
379                      tys: &ShimTypes,
380                      llargbundle: ValueRef,
381                      llretval: ValueRef) {
382             let _icx = push_ctxt("foreign::shim::build_ret");
383             tys.fn_ty.build_shim_ret(bcx,
384                                      tys.llsig.llarg_tys,
385                                      tys.ret_def,
386                                      llargbundle,
387                                      llretval);
388         }
389
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);
394         build_shim_fn_(ccx,
395                        shim_name,
396                        llbasefn,
397                        tys,
398                        cc,
399                        build_args,
400                        build_ret)
401     }
402
403     fn base_fn(ccx: &CrateContext,
404                lname: &str,
405                tys: &ShimTypes,
406                cc: lib::llvm::CallConv)
407                -> ValueRef {
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)
411         }
412     }
413
414     // FIXME (#2535): this is very shaky and probably gets ABIs wrong all
415     // over the place
416     fn build_direct_fn(ccx: @mut CrateContext,
417                        decl: ValueRef,
418                        item: &ast::foreign_item,
419                        tys: &ShimTypes,
420                        cc: lib::llvm::CallConv) {
421         debug!("build_direct_fn(%s)", link_name(ccx, item));
422
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))
431         });
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());
435         }
436         finish_fn(fcx, bcx);
437     }
438
439     // FIXME (#2535): this is very shaky and probably gets ABIs wrong all
440     // over the place
441     fn build_fast_ffi_fn(ccx: @mut CrateContext,
442                          decl: ValueRef,
443                          item: &ast::foreign_item,
444                          tys: &ShimTypes,
445                          cc: lib::llvm::CallConv) {
446         debug!("build_fast_ffi_fn(%s)", link_name(ccx, item));
447
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))
458         });
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());
462         }
463         finish_fn(fcx, bcx);
464     }
465
466     fn build_wrap_fn(ccx: @mut CrateContext,
467                      tys: &ShimTypes,
468                      llshimfn: ValueRef,
469                      llwrapfn: ValueRef) {
470         /*!
471          *
472          * Build W, from comment above:
473          *
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);
477          *     }
478          *
479          * One thing we have to be very careful of is to
480          * account for the Rust modes.
481          */
482
483         let _icx = push_ctxt("foreign::build_wrap_fn");
484
485         build_wrap_fn_(ccx,
486                        tys,
487                        llshimfn,
488                        llwrapfn,
489                        ccx.upcalls.call_shim_on_c_stack,
490                        false,
491                        build_args,
492                        build_ret);
493
494         fn build_args(bcx: @mut Block,
495                       tys: &ShimTypes,
496                       llwrapfn: ValueRef,
497                       llargbundle: ValueRef) {
498             let _icx = push_ctxt("foreign::wrap::build_args");
499             let ccx = bcx.ccx();
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);
504
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);
510                 }
511
512                 store_inbounds(bcx, llargval, llargbundle, [0u, i]);
513             }
514
515             for &retptr in bcx.fcx.llretptr.iter() {
516                 store_inbounds(bcx, retptr, llargbundle, [0u, n]);
517             }
518         }
519
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);
528             }
529         }
530     }
531 }
532
533 pub fn trans_intrinsic(ccx: @mut CrateContext,
534                        decl: ValueRef,
535                        item: &ast::foreign_item,
536                        path: ast_map::path,
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));
541
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);
548         }
549         let llfn = bcx.ccx().intrinsics.get_copy(&name);
550         Ret(bcx, Call(bcx, llfn, args.slice(0, num_args)));
551     }
552
553     fn memcpy_intrinsic(bcx: @mut Block, name: &'static str, tp_ty: ty::t, sizebits: u8) {
554         let ccx = bcx.ccx();
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")
561         };
562
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]);
571         RetVoid(bcx);
572     }
573
574     fn memset_intrinsic(bcx: @mut Block, name: &'static str, tp_ty: ty::t, sizebits: u8) {
575         let ccx = bcx.ccx();
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")
582         };
583
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]);
592         RetVoid(bcx);
593     }
594
595     fn count_zeros_intrinsic(bcx: @mut Block, name: &'static str) {
596         let x = get_param(bcx.fcx.llfn, bcx.fcx.arg_pos(0u));
597         let y = C_i1(false);
598         let llfn = bcx.ccx().intrinsics.get_copy(&name);
599         Ret(bcx, Call(bcx, llfn, [x, y]));
600     }
601
602     let output_type = ty::ty_fn_ret(ty::node_id_to_type(ccx.tcx, item.id));
603
604     let fcx = new_fn_ctxt_w_id(ccx,
605                                path,
606                                decl,
607                                item.id,
608                                output_type,
609                                true,
610                                Some(substs),
611                                None,
612                                Some(item.span));
613
614     set_always_inline(fcx.llfn);
615
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);
619     }
620
621     let mut bcx = fcx.entry_bcx.unwrap();
622     let first_real_arg = fcx.arg_pos(0u);
623
624     let nm = ccx.sess.str_of(item.ident);
625     let name = nm.as_slice();
626
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
634         } else {
635             match split[2] {
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")
641             }
642         };
643
644         match split[1] {
645             "cxchg" => {
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),
649                                         order);
650                 Ret(bcx, old);
651             }
652             "load" => {
653                 let old = AtomicLoad(bcx, get_param(decl, first_real_arg),
654                                      order);
655                 Ret(bcx, old);
656             }
657             "store" => {
658                 AtomicStore(bcx, get_param(decl, first_real_arg + 1u),
659                             get_param(decl, first_real_arg),
660                             order);
661                 RetVoid(bcx);
662             }
663             "fence" => {
664                 AtomicFence(bcx, order);
665                 RetVoid(bcx);
666             }
667             op => {
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")
682                 };
683
684                 let old = AtomicRMW(bcx, atom_op, get_param(decl, first_real_arg),
685                                     get_param(decl, first_real_arg + 1u),
686                                     order);
687                 Ret(bcx, old);
688             }
689         }
690
691         fcx.cleanup();
692         return;
693     }
694
695     match name {
696         "size_of" => {
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)));
700         }
701         "move_val" => {
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));
713             RetVoid(bcx);
714         }
715         "move_val_init" => {
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));
722             RetVoid(bcx);
723         }
724         "min_align_of" => {
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)));
728         }
729         "pref_align_of"=> {
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)));
733         }
734         "get_tydesc" => {
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);
738
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);
745             Ret(bcx, td);
746         }
747         "init" => {
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)),
754             }
755         }
756         "uninit" => {
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) {
760                 unsafe {
761                     Ret(bcx, lib::llvm::llvm::LLVMGetUndef(type_of(ccx, retty).to_ref()));
762                 }
763             } else {
764                 RetVoid(bcx)
765             }
766         }
767         "forget" => {
768             RetVoid(bcx);
769         }
770         "transmute" => {
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);
774
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"),
781                 };
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 \
786                                           %s (%u bit%s)",
787                                          ty_to_str(ccx.tcx, in_type),
788                                          in_type_size,
789                                          pluralize(in_type_size),
790                                          ty_to_str(ccx.tcx, out_type),
791                                          out_type_size,
792                                          pluralize(out_type_size)));
793             }
794
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) {
798                     match fcx.llretptr {
799                         Some(llretptr) => {
800                             Store(bcx, llsrcval, PointerCast(bcx, llretptr, llintype.ptr_to()));
801                             RetVoid(bcx);
802                         }
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));
808                             }
809                             _ => Ret(bcx, BitCast(bcx, llsrcval, llouttype))
810                         }
811                     }
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));
815                 } else {
816                     // NB: Do not use a Load and Store here. This causes massive
817                     // code bloat when `transmute` is used on large structural
818                     // types.
819                     let lldestptr = fcx.llretptr.unwrap();
820                     let lldestptr = PointerCast(bcx, lldestptr, Type::i8p());
821                     let llsrcptr = PointerCast(bcx, llsrcval, Type::i8p());
822
823                     let llsize = llsize_of(ccx, llintype);
824                     call_memcpy(bcx, lldestptr, llsrcptr, llsize, 1);
825                     RetVoid(bcx);
826                 };
827             } else {
828                 RetVoid(bcx);
829             }
830         }
831         "needs_drop" => {
832             let tp_ty = substs.tys[0];
833             Ret(bcx, C_bool(ty::type_needs_drop(ccx.tcx, tp_ty)));
834         }
835         "contains_managed" => {
836             let tp_ty = substs.tys[0];
837             Ret(bcx, C_bool(ty::type_contents(ccx.tcx, tp_ty).contains_managed()));
838         }
839         "visit_tydesc" => {
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);
847             RetVoid(bcx);
848         }
849         "frame_address" => {
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(
853                 bcx.tcx(),
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,
858                 onceness: ast::Many,
859                 region: ty::re_bound(ty::br_anon(0)),
860                 bounds: ty::EmptyBuiltinBounds(),
861                 sig: FnSig {
862                     bound_lifetime_names: opt_vec::Empty,
863                     inputs: ~[ star_u8 ],
864                     output: ty::mk_nil()
865                 }
866             });
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;
874             RetVoid(bcx);
875         }
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
879             // way to do this
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);
885         }
886         "offset" => {
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]));
890         }
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),
942         _ => {
943             // Could we make this an enum rather than a string? does it get
944             // checked earlier?
945             ccx.sess.span_bug(item.span, "unknown intrinsic");
946         }
947     }
948     fcx.cleanup();
949 }
950
951 /**
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.
956  *
957  * Example: Given a crust fn F(x: X, y: Y) -> Z, we generate a
958  * Rust function R as normal:
959  *
960  *    void R(Z* dest, void *env, X x, Y y) {...}
961  *
962  * and then we generate a wrapper function W that looks like:
963  *
964  *    Z W(X x, Y y) {
965  *        struct { X x; Y y; Z *z; } args = { x, y, z };
966  *        call_on_c_stack_shim(S, &args);
967  *    }
968  *
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:
972  *
973  *     void S(struct { X x; Y y; Z *z; } *args) {
974  *         R(args->z, NULL, args->x, args->y);
975  *     }
976  */
977 pub fn trans_foreign_fn(ccx: @mut CrateContext,
978                         path: ast_map::path,
979                         decl: &ast::fn_decl,
980                         body: &ast::Block,
981                         llwrapfn: ValueRef,
982                         id: ast::NodeId) {
983     let _icx = push_ctxt("foreign::build_foreign_fn");
984
985     fn build_rust_fn(ccx: @mut CrateContext,
986                      path: &ast_map::path,
987                      decl: &ast::fn_decl,
988                      body: &ast::Block,
989                      id: ast::NodeId)
990                   -> ValueRef {
991         let _icx = push_ctxt("foreign::foreign::build_rust_fn");
992         let t = ty::node_id_to_type(ccx.tcx, id);
993         // XXX: Bad copy.
994         let ps = link::mangle_internal_name_by_path(
995                             ccx,
996                             vec::append_one((*path).clone(),
997                                             ast_map::path_name(
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);
1001         trans_fn(ccx,
1002                  (*path).clone(),
1003                  decl,
1004                  body,
1005                  llfndecl,
1006                  no_self,
1007                  None,
1008                  id,
1009                  []);
1010         return llfndecl;
1011     }
1012
1013     fn build_shim_fn(ccx: @mut CrateContext,
1014                      path: ast_map::path,
1015                      llrustfn: ValueRef,
1016                      tys: &ShimTypes)
1017                      -> ValueRef {
1018         /*!
1019          *
1020          * Generate the shim S:
1021          *
1022          *     void S(struct { X x; Y y; Z *z; } *args) {
1023          *         R(args->z, NULL, &args->x, args->y);
1024          *     }
1025          *
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.
1031          */
1032
1033         let _icx = push_ctxt("foreign::foreign::build_shim_fn");
1034
1035         fn build_args(bcx: @mut Block, tys: &ShimTypes, llargbundle: ValueRef)
1036                       -> ~[ValueRef] {
1037             let _icx = push_ctxt("foreign::extern::shim::build_args");
1038             let ccx = bcx.ccx();
1039             let mut llargvals = ~[];
1040             let mut i = 0u;
1041             let n = tys.fn_sig.inputs.len();
1042
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);
1046             }
1047
1048             let llenvptr = C_null(Type::opaque_box(bcx.ccx()).ptr_to());
1049             llargvals.push(llenvptr);
1050             while i < n {
1051                 // Get a pointer to the argument:
1052                 let mut llargval = GEPi(bcx, llargbundle, [0u, i]);
1053
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);
1057                 }
1058
1059                 llargvals.push(llargval);
1060                 i += 1u;
1061             }
1062             return llargvals;
1063         }
1064
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,
1074                                              llargbundle,
1075                                              [0, arg_count]);
1076                 Store(bcx, llretval, llretptr);
1077             } else {
1078                 // NB: The return pointer in the Rust ABI function is wired
1079                 // directly into the return slot in the shim struct.
1080             }
1081         }
1082
1083         let shim_name = link::mangle_internal_name_by_path(
1084             ccx,
1085             vec::append_one(path, ast_map::path_name(
1086                 special_idents::clownshoe_stack_shim
1087             )));
1088         build_shim_fn_(ccx,
1089                        shim_name,
1090                        llrustfn,
1091                        tys,
1092                        lib::llvm::CCallConv,
1093                        build_args,
1094                        build_ret)
1095     }
1096
1097     fn build_wrap_fn(ccx: @mut CrateContext,
1098                      llshimfn: ValueRef,
1099                      llwrapfn: ValueRef,
1100                      tys: &ShimTypes) {
1101         /*!
1102          *
1103          * Generate the wrapper W:
1104          *
1105          *    Z W(X x, Y y) {
1106          *        struct { X x; Y y; Z *z; } args = { x, y, z };
1107          *        call_on_c_stack_shim(S, &args);
1108          *    }
1109          */
1110
1111         let _icx = push_ctxt("foreign::foreign::build_wrap_fn");
1112
1113         build_wrap_fn_(ccx,
1114                        tys,
1115                        llshimfn,
1116                        llwrapfn,
1117                        ccx.upcalls.call_shim_on_rust_stack,
1118                        true,
1119                        build_args,
1120                        build_ret);
1121
1122         fn build_args(bcx: @mut Block,
1123                       tys: &ShimTypes,
1124                       llwrapfn: ValueRef,
1125                       llargbundle: ValueRef) {
1126             let _icx = push_ctxt("foreign::foreign::wrap::build_args");
1127             tys.fn_ty.build_wrap_args(bcx,
1128                                       tys.llsig.llret_ty,
1129                                       llwrapfn,
1130                                       llargbundle);
1131         }
1132
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);
1136         }
1137     }
1138
1139     let tys = shim_types(ccx, id);
1140     // The internal Rust ABI function - runs on the Rust stack
1141     // XXX: Bad copy.
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)
1147 }
1148
1149 pub fn register_foreign_fn(ccx: @mut CrateContext,
1150                            sp: span,
1151                            sym: ~str,
1152                            node_id: ast::NodeId)
1153                            -> ValueRef {
1154     let _icx = push_ctxt("foreign::register_foreign_fn");
1155
1156     let sym = Cell::new(sym);
1157
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)
1161     }
1162 }