/
foreign.rs
1340 lines (1248 loc) · 52.4 KB
/
foreign.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use back::{link, abi};
use lib::llvm::{SequentiallyConsistent, Acquire, Release, Xchg};
use lib::llvm::{TypeRef, ValueRef};
use lib;
use middle::trans::base::*;
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, arg};
use util::ppaux::ty_to_str;
use syntax::codemap::span;
use syntax::{ast, ast_util};
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};
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(),
}
}
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 {
fn_sig: ty::FnSig,
/// 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 {
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| 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),
}
}
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
}
}
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));
}
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));
}
}
}
// 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");
let arch = ccx.sess.targ_cfg.arch;
let abi = match foreign_mod.abis.for_arch(arch) {
None => {
ccx.sess.fatal(
fmt!("No suitable ABI for target architecture \
in module %s",
ast_map::path_to_str(*path,
ccx.sess.intr())));
}
Some(abi) => abi,
};
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);
}
}
}
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);
} else {
let llshimfn = build_shim_fn(ccx, foreign_item, &tys, cc);
build_wrap_fn(ccx, &tys, llshimfn, llwrapfn);
}
}
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)
}
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);
}
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)
}
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)
}
}
// 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);
}
// 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());
}
build_return(bcx);
finish_fn(fcx, lltop);
}
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 = vec::len(tys.llsig.llarg_tys);
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);
}
store_inbounds(bcx, llargval, llargbundle, ~[0u, i]);
}
let llretptr = bcx.fcx.llretptr.get();
store_inbounds(bcx, llretptr, llargbundle, ~[0u, n]);
}
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);
}
}
}
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<ast::node_id>) {
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));
// XXX: Bad copy.
let fcx = new_fn_ctxt_w_id(ccx,
path,
decl,
item.id,
output_type,
None,
Some(copy 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);
}
}
~"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)),
sig: FnSig {
bound_lifetime_names: opt_vec::Empty,
inputs: ~[ arg { ty: 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());
}
~"memmove32" => {
let dst_ptr = get_param(decl, first_real_arg);
let src_ptr = get_param(decl, first_real_arg + 1);
let size = get_param(decl, first_real_arg + 2);
let align = C_i32(1);
let volatile = C_i1(false);
let llfn = *bcx.ccx().intrinsics.get(
&~"llvm.memmove.p0i8.p0i8.i32");
Call(bcx, llfn, ~[dst_ptr, src_ptr, size, align, volatile]);
}
~"memmove64" => {
let dst_ptr = get_param(decl, first_real_arg);
let src_ptr = get_param(decl, first_real_arg + 1);
let size = get_param(decl, first_real_arg + 2);
let align = C_i32(1);
let volatile = C_i1(false);
let llfn = *bcx.ccx().intrinsics.get(
&~"llvm.memmove.p0i8.p0i8.i64");
Call(bcx, llfn, ~[dst_ptr, src_ptr, size, 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);