1 // Copyright 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.
11 #![allow(dead_code)] // FFI wrappers
14 use llvm::{CallConv, AtomicBinOp, AtomicOrdering, AsmDialect, AttrBuilder};
15 use llvm::{Opcode, IntPredicate, RealPredicate, False};
16 use llvm::{ValueRef, BasicBlockRef, BuilderRef, ModuleRef};
19 use trans::machine::llalign_of_pref;
20 use trans::type_::Type;
21 use util::nodemap::FnvHashMap;
22 use libc::{c_uint, c_char};
23 use std::c_str::ToCStr;
24 use syntax::codemap::Span;
26 pub struct Builder<'a, 'tcx: 'a> {
27 pub llbuilder: BuilderRef,
28 pub ccx: &'a CrateContext<'a, 'tcx>,
31 // This is a really awful way to get a zero-length c-string, but better (and a
32 // lot more efficient) than doing str::as_c_str("", ...) every time.
33 pub fn noname() -> *const c_char {
34 static CNULL: c_char = 0;
35 &CNULL as *const c_char
38 impl<'a, 'tcx> Builder<'a, 'tcx> {
39 pub fn new(ccx: &'a CrateContext<'a, 'tcx>) -> Builder<'a, 'tcx> {
41 llbuilder: ccx.raw_builder(),
46 pub fn count_insn(&self, category: &str) {
47 if self.ccx.sess().trans_stats() {
48 self.ccx.stats().n_llvm_insns.set(self.ccx
53 self.ccx.count_llvm_insn();
54 if self.ccx.sess().count_llvm_insns() {
55 base::with_insn_ctxt(|v| {
56 let mut h = self.ccx.stats().llvm_insns.borrow_mut();
58 // Build version of path with cycles removed.
60 // Pass 1: scan table mapping str -> rightmost pos.
61 let mut mm = FnvHashMap::new();
69 // Pass 2: concat strings for each elt, skipping
70 // forwards over any cycles by advancing to rightmost
71 // occurrence of each element in path.
72 let mut s = String::from_str(".");
84 let n = match h.get(&s) {
93 pub fn position_before(&self, insn: ValueRef) {
95 llvm::LLVMPositionBuilderBefore(self.llbuilder, insn);
99 pub fn position_at_end(&self, llbb: BasicBlockRef) {
101 llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
105 pub fn ret_void(&self) {
106 self.count_insn("retvoid");
108 llvm::LLVMBuildRetVoid(self.llbuilder);
112 pub fn ret(&self, v: ValueRef) {
113 self.count_insn("ret");
115 llvm::LLVMBuildRet(self.llbuilder, v);
119 pub fn aggregate_ret(&self, ret_vals: &[ValueRef]) {
121 llvm::LLVMBuildAggregateRet(self.llbuilder,
123 ret_vals.len() as c_uint);
127 pub fn br(&self, dest: BasicBlockRef) {
128 self.count_insn("br");
130 llvm::LLVMBuildBr(self.llbuilder, dest);
134 pub fn cond_br(&self, cond: ValueRef, then_llbb: BasicBlockRef, else_llbb: BasicBlockRef) {
135 self.count_insn("condbr");
137 llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
141 pub fn switch(&self, v: ValueRef, else_llbb: BasicBlockRef, num_cases: uint) -> ValueRef {
143 llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, num_cases as c_uint)
147 pub fn indirect_br(&self, addr: ValueRef, num_dests: uint) {
148 self.count_insn("indirectbr");
150 llvm::LLVMBuildIndirectBr(self.llbuilder, addr, num_dests as c_uint);
158 catch: BasicBlockRef,
159 attributes: Option<AttrBuilder>)
161 self.count_insn("invoke");
163 debug!("Invoke {} with args ({})",
164 self.ccx.tn().val_to_string(llfn),
166 .map(|&v| self.ccx.tn().val_to_string(v))
167 .collect::<Vec<String>>()
171 let v = llvm::LLVMBuildInvoke(self.llbuilder,
174 args.len() as c_uint,
179 Some(a) => a.apply_callsite(v),
186 pub fn unreachable(&self) {
187 self.count_insn("unreachable");
189 llvm::LLVMBuildUnreachable(self.llbuilder);
194 pub fn add(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
195 self.count_insn("add");
197 llvm::LLVMBuildAdd(self.llbuilder, lhs, rhs, noname())
201 pub fn nswadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
202 self.count_insn("nswadd");
204 llvm::LLVMBuildNSWAdd(self.llbuilder, lhs, rhs, noname())
208 pub fn nuwadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
209 self.count_insn("nuwadd");
211 llvm::LLVMBuildNUWAdd(self.llbuilder, lhs, rhs, noname())
215 pub fn fadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
216 self.count_insn("fadd");
218 llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname())
222 pub fn sub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
223 self.count_insn("sub");
225 llvm::LLVMBuildSub(self.llbuilder, lhs, rhs, noname())
229 pub fn nswsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
230 self.count_insn("nwsub");
232 llvm::LLVMBuildNSWSub(self.llbuilder, lhs, rhs, noname())
236 pub fn nuwsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
237 self.count_insn("nuwsub");
239 llvm::LLVMBuildNUWSub(self.llbuilder, lhs, rhs, noname())
243 pub fn fsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
244 self.count_insn("sub");
246 llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname())
250 pub fn mul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
251 self.count_insn("mul");
253 llvm::LLVMBuildMul(self.llbuilder, lhs, rhs, noname())
257 pub fn nswmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
258 self.count_insn("nswmul");
260 llvm::LLVMBuildNSWMul(self.llbuilder, lhs, rhs, noname())
264 pub fn nuwmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
265 self.count_insn("nuwmul");
267 llvm::LLVMBuildNUWMul(self.llbuilder, lhs, rhs, noname())
271 pub fn fmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
272 self.count_insn("fmul");
274 llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname())
278 pub fn udiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
279 self.count_insn("udiv");
281 llvm::LLVMBuildUDiv(self.llbuilder, lhs, rhs, noname())
285 pub fn sdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
286 self.count_insn("sdiv");
288 llvm::LLVMBuildSDiv(self.llbuilder, lhs, rhs, noname())
292 pub fn exactsdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
293 self.count_insn("exactsdiv");
295 llvm::LLVMBuildExactSDiv(self.llbuilder, lhs, rhs, noname())
299 pub fn fdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
300 self.count_insn("fdiv");
302 llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname())
306 pub fn urem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
307 self.count_insn("urem");
309 llvm::LLVMBuildURem(self.llbuilder, lhs, rhs, noname())
313 pub fn srem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
314 self.count_insn("srem");
316 llvm::LLVMBuildSRem(self.llbuilder, lhs, rhs, noname())
320 pub fn frem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
321 self.count_insn("frem");
323 llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname())
327 pub fn shl(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
328 self.count_insn("shl");
330 llvm::LLVMBuildShl(self.llbuilder, lhs, rhs, noname())
334 pub fn lshr(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
335 self.count_insn("lshr");
337 llvm::LLVMBuildLShr(self.llbuilder, lhs, rhs, noname())
341 pub fn ashr(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
342 self.count_insn("ashr");
344 llvm::LLVMBuildAShr(self.llbuilder, lhs, rhs, noname())
348 pub fn and(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
349 self.count_insn("and");
351 llvm::LLVMBuildAnd(self.llbuilder, lhs, rhs, noname())
355 pub fn or(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
356 self.count_insn("or");
358 llvm::LLVMBuildOr(self.llbuilder, lhs, rhs, noname())
362 pub fn xor(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
363 self.count_insn("xor");
365 llvm::LLVMBuildXor(self.llbuilder, lhs, rhs, noname())
369 pub fn binop(&self, op: Opcode, lhs: ValueRef, rhs: ValueRef)
371 self.count_insn("binop");
373 llvm::LLVMBuildBinOp(self.llbuilder, op, lhs, rhs, noname())
377 pub fn neg(&self, v: ValueRef) -> ValueRef {
378 self.count_insn("neg");
380 llvm::LLVMBuildNeg(self.llbuilder, v, noname())
384 pub fn nswneg(&self, v: ValueRef) -> ValueRef {
385 self.count_insn("nswneg");
387 llvm::LLVMBuildNSWNeg(self.llbuilder, v, noname())
391 pub fn nuwneg(&self, v: ValueRef) -> ValueRef {
392 self.count_insn("nuwneg");
394 llvm::LLVMBuildNUWNeg(self.llbuilder, v, noname())
397 pub fn fneg(&self, v: ValueRef) -> ValueRef {
398 self.count_insn("fneg");
400 llvm::LLVMBuildFNeg(self.llbuilder, v, noname())
404 pub fn not(&self, v: ValueRef) -> ValueRef {
405 self.count_insn("not");
407 llvm::LLVMBuildNot(self.llbuilder, v, noname())
412 pub fn malloc(&self, ty: Type) -> ValueRef {
413 self.count_insn("malloc");
415 llvm::LLVMBuildMalloc(self.llbuilder, ty.to_ref(), noname())
419 pub fn array_malloc(&self, ty: Type, val: ValueRef) -> ValueRef {
420 self.count_insn("arraymalloc");
422 llvm::LLVMBuildArrayMalloc(self.llbuilder, ty.to_ref(), val, noname())
426 pub fn alloca(&self, ty: Type, name: &str) -> ValueRef {
427 self.count_insn("alloca");
430 llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), noname())
432 name.with_c_str(|c| {
433 llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), c)
439 pub fn array_alloca(&self, ty: Type, val: ValueRef) -> ValueRef {
440 self.count_insn("arrayalloca");
442 llvm::LLVMBuildArrayAlloca(self.llbuilder, ty.to_ref(), val, noname())
446 pub fn free(&self, ptr: ValueRef) {
447 self.count_insn("free");
449 llvm::LLVMBuildFree(self.llbuilder, ptr);
453 pub fn load(&self, ptr: ValueRef) -> ValueRef {
454 self.count_insn("load");
456 llvm::LLVMBuildLoad(self.llbuilder, ptr, noname())
460 pub fn volatile_load(&self, ptr: ValueRef) -> ValueRef {
461 self.count_insn("load.volatile");
463 let insn = llvm::LLVMBuildLoad(self.llbuilder, ptr, noname());
464 llvm::LLVMSetVolatile(insn, llvm::True);
469 pub fn atomic_load(&self, ptr: ValueRef, order: AtomicOrdering) -> ValueRef {
470 self.count_insn("load.atomic");
472 let ty = Type::from_ref(llvm::LLVMTypeOf(ptr));
473 let align = llalign_of_pref(self.ccx, ty.element_type());
474 llvm::LLVMBuildAtomicLoad(self.llbuilder, ptr, noname(), order,
480 pub fn load_range_assert(&self, ptr: ValueRef, lo: u64,
481 hi: u64, signed: llvm::Bool) -> ValueRef {
482 let value = self.load(ptr);
485 let t = llvm::LLVMGetElementType(llvm::LLVMTypeOf(ptr));
486 let min = llvm::LLVMConstInt(t, lo, signed);
487 let max = llvm::LLVMConstInt(t, hi, signed);
491 llvm::LLVMSetMetadata(value, llvm::MD_range as c_uint,
492 llvm::LLVMMDNodeInContext(self.ccx.llcx(),
500 pub fn store(&self, val: ValueRef, ptr: ValueRef) {
501 debug!("Store {} -> {}",
502 self.ccx.tn().val_to_string(val),
503 self.ccx.tn().val_to_string(ptr));
504 assert!(!self.llbuilder.is_null());
505 self.count_insn("store");
507 llvm::LLVMBuildStore(self.llbuilder, val, ptr);
511 pub fn volatile_store(&self, val: ValueRef, ptr: ValueRef) {
512 debug!("Store {} -> {}",
513 self.ccx.tn().val_to_string(val),
514 self.ccx.tn().val_to_string(ptr));
515 assert!(!self.llbuilder.is_null());
516 self.count_insn("store.volatile");
518 let insn = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
519 llvm::LLVMSetVolatile(insn, llvm::True);
523 pub fn atomic_store(&self, val: ValueRef, ptr: ValueRef, order: AtomicOrdering) {
524 debug!("Store {} -> {}",
525 self.ccx.tn().val_to_string(val),
526 self.ccx.tn().val_to_string(ptr));
527 self.count_insn("store.atomic");
529 let ty = Type::from_ref(llvm::LLVMTypeOf(ptr));
530 let align = llalign_of_pref(self.ccx, ty.element_type());
531 llvm::LLVMBuildAtomicStore(self.llbuilder, val, ptr, order, align as c_uint);
535 pub fn gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef {
536 self.count_insn("gep");
538 llvm::LLVMBuildGEP(self.llbuilder, ptr, indices.as_ptr(),
539 indices.len() as c_uint, noname())
543 // Simple wrapper around GEP that takes an array of ints and wraps them
546 pub fn gepi(&self, base: ValueRef, ixs: &[uint]) -> ValueRef {
547 // Small vector optimization. This should catch 100% of the cases that
550 let mut small_vec = [ C_i32(self.ccx, 0); 16 ];
551 for (small_vec_e, &ix) in small_vec.iter_mut().zip(ixs.iter()) {
552 *small_vec_e = C_i32(self.ccx, ix as i32);
554 self.inbounds_gep(base, small_vec[..ixs.len()])
556 let v = ixs.iter().map(|i| C_i32(self.ccx, *i as i32)).collect::<Vec<ValueRef>>();
557 self.count_insn("gepi");
558 self.inbounds_gep(base, v[])
562 pub fn inbounds_gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef {
563 self.count_insn("inboundsgep");
565 llvm::LLVMBuildInBoundsGEP(
566 self.llbuilder, ptr, indices.as_ptr(), indices.len() as c_uint, noname())
570 pub fn struct_gep(&self, ptr: ValueRef, idx: uint) -> ValueRef {
571 self.count_insn("structgep");
573 llvm::LLVMBuildStructGEP(self.llbuilder, ptr, idx as c_uint, noname())
577 pub fn global_string(&self, _str: *const c_char) -> ValueRef {
578 self.count_insn("globalstring");
580 llvm::LLVMBuildGlobalString(self.llbuilder, _str, noname())
584 pub fn global_string_ptr(&self, _str: *const c_char) -> ValueRef {
585 self.count_insn("globalstringptr");
587 llvm::LLVMBuildGlobalStringPtr(self.llbuilder, _str, noname())
592 pub fn trunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
593 self.count_insn("trunc");
595 llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty.to_ref(), noname())
599 pub fn zext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
600 self.count_insn("zext");
602 llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty.to_ref(), noname())
606 pub fn sext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
607 self.count_insn("sext");
609 llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty.to_ref(), noname())
613 pub fn fptoui(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
614 self.count_insn("fptoui");
616 llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty.to_ref(), noname())
620 pub fn fptosi(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
621 self.count_insn("fptosi");
623 llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty.to_ref(),noname())
627 pub fn uitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
628 self.count_insn("uitofp");
630 llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty.to_ref(), noname())
634 pub fn sitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
635 self.count_insn("sitofp");
637 llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty.to_ref(), noname())
641 pub fn fptrunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
642 self.count_insn("fptrunc");
644 llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty.to_ref(), noname())
648 pub fn fpext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
649 self.count_insn("fpext");
651 llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty.to_ref(), noname())
655 pub fn ptrtoint(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
656 self.count_insn("ptrtoint");
658 llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty.to_ref(), noname())
662 pub fn inttoptr(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
663 self.count_insn("inttoptr");
665 llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty.to_ref(), noname())
669 pub fn bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
670 self.count_insn("bitcast");
672 llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
676 pub fn zext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
677 self.count_insn("zextorbitcast");
679 llvm::LLVMBuildZExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
683 pub fn sext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
684 self.count_insn("sextorbitcast");
686 llvm::LLVMBuildSExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
690 pub fn trunc_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
691 self.count_insn("truncorbitcast");
693 llvm::LLVMBuildTruncOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
697 pub fn cast(&self, op: Opcode, val: ValueRef, dest_ty: Type) -> ValueRef {
698 self.count_insn("cast");
700 llvm::LLVMBuildCast(self.llbuilder, op, val, dest_ty.to_ref(), noname())
704 pub fn pointercast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
705 self.count_insn("pointercast");
707 llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty.to_ref(), noname())
711 pub fn intcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
712 self.count_insn("intcast");
714 llvm::LLVMBuildIntCast(self.llbuilder, val, dest_ty.to_ref(), noname())
718 pub fn fpcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
719 self.count_insn("fpcast");
721 llvm::LLVMBuildFPCast(self.llbuilder, val, dest_ty.to_ref(), noname())
727 pub fn icmp(&self, op: IntPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
728 self.count_insn("icmp");
730 llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
734 pub fn fcmp(&self, op: RealPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
735 self.count_insn("fcmp");
737 llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
741 /* Miscellaneous instructions */
742 pub fn empty_phi(&self, ty: Type) -> ValueRef {
743 self.count_insn("emptyphi");
745 llvm::LLVMBuildPhi(self.llbuilder, ty.to_ref(), noname())
749 pub fn phi(&self, ty: Type, vals: &[ValueRef], bbs: &[BasicBlockRef]) -> ValueRef {
750 assert_eq!(vals.len(), bbs.len());
751 let phi = self.empty_phi(ty);
752 self.count_insn("addincoming");
754 llvm::LLVMAddIncoming(phi, vals.as_ptr(),
756 vals.len() as c_uint);
761 pub fn add_span_comment(&self, sp: Span, text: &str) {
762 if self.ccx.sess().asm_comments() {
763 let s = format!("{} ({})",
765 self.ccx.sess().codemap().span_to_string(sp));
767 self.add_comment(s[]);
771 pub fn add_comment(&self, text: &str) {
772 if self.ccx.sess().asm_comments() {
773 let sanitized = text.replace("$", "");
774 let comment_text = format!("{} {}", "#",
775 sanitized.replace("\n", "\n\t# "));
776 self.count_insn("inlineasm");
777 let asm = comment_text.with_c_str(|c| {
779 llvm::LLVMConstInlineAsm(Type::func(&[], &Type::void(self.ccx)).to_ref(),
780 c, noname(), False, False)
783 self.call(asm, &[], None);
787 pub fn inline_asm_call(&self, asm: *const c_char, cons: *const c_char,
788 inputs: &[ValueRef], output: Type,
789 volatile: bool, alignstack: bool,
790 dia: AsmDialect) -> ValueRef {
791 self.count_insn("inlineasm");
793 let volatile = if volatile { llvm::True }
794 else { llvm::False };
795 let alignstack = if alignstack { llvm::True }
796 else { llvm::False };
798 let argtys = inputs.iter().map(|v| {
799 debug!("Asm Input Type: {}", self.ccx.tn().val_to_string(*v));
801 }).collect::<Vec<_>>();
803 debug!("Asm Output Type: {}", self.ccx.tn().type_to_string(output));
804 let fty = Type::func(argtys[], &output);
806 let v = llvm::LLVMInlineAsm(
807 fty.to_ref(), asm, cons, volatile, alignstack, dia as c_uint);
808 self.call(v, inputs, None)
812 pub fn call(&self, llfn: ValueRef, args: &[ValueRef],
813 attributes: Option<AttrBuilder>) -> ValueRef {
814 self.count_insn("call");
816 debug!("Call {} with args ({})",
817 self.ccx.tn().val_to_string(llfn),
819 .map(|&v| self.ccx.tn().val_to_string(v))
820 .collect::<Vec<String>>()
824 let v = llvm::LLVMBuildCall(self.llbuilder, llfn, args.as_ptr(),
825 args.len() as c_uint, noname());
827 Some(a) => a.apply_callsite(v),
834 pub fn call_with_conv(&self, llfn: ValueRef, args: &[ValueRef],
835 conv: CallConv, attributes: Option<AttrBuilder>) -> ValueRef {
836 self.count_insn("callwithconv");
837 let v = self.call(llfn, args, attributes);
838 llvm::SetInstructionCallConv(v, conv);
842 pub fn select(&self, cond: ValueRef, then_val: ValueRef, else_val: ValueRef) -> ValueRef {
843 self.count_insn("select");
845 llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, noname())
849 pub fn va_arg(&self, list: ValueRef, ty: Type) -> ValueRef {
850 self.count_insn("vaarg");
852 llvm::LLVMBuildVAArg(self.llbuilder, list, ty.to_ref(), noname())
856 pub fn extract_element(&self, vec: ValueRef, idx: ValueRef) -> ValueRef {
857 self.count_insn("extractelement");
859 llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, noname())
863 pub fn insert_element(&self, vec: ValueRef, elt: ValueRef, idx: ValueRef) -> ValueRef {
864 self.count_insn("insertelement");
866 llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, noname())
870 pub fn shuffle_vector(&self, v1: ValueRef, v2: ValueRef, mask: ValueRef) -> ValueRef {
871 self.count_insn("shufflevector");
873 llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, noname())
877 pub fn vector_splat(&self, num_elts: uint, elt: ValueRef) -> ValueRef {
879 let elt_ty = val_ty(elt);
880 let undef = llvm::LLVMGetUndef(Type::vector(&elt_ty, num_elts as u64).to_ref());
881 let vec = self.insert_element(undef, elt, C_i32(self.ccx, 0));
882 let vec_i32_ty = Type::vector(&Type::i32(self.ccx), num_elts as u64);
883 self.shuffle_vector(vec, undef, C_null(vec_i32_ty))
887 pub fn extract_value(&self, agg_val: ValueRef, idx: uint) -> ValueRef {
888 self.count_insn("extractvalue");
890 llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, noname())
894 pub fn insert_value(&self, agg_val: ValueRef, elt: ValueRef,
895 idx: uint) -> ValueRef {
896 self.count_insn("insertvalue");
898 llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint,
903 pub fn is_null(&self, val: ValueRef) -> ValueRef {
904 self.count_insn("isnull");
906 llvm::LLVMBuildIsNull(self.llbuilder, val, noname())
910 pub fn is_not_null(&self, val: ValueRef) -> ValueRef {
911 self.count_insn("isnotnull");
913 llvm::LLVMBuildIsNotNull(self.llbuilder, val, noname())
917 pub fn ptrdiff(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
918 self.count_insn("ptrdiff");
920 llvm::LLVMBuildPtrDiff(self.llbuilder, lhs, rhs, noname())
926 let bb: BasicBlockRef = llvm::LLVMGetInsertBlock(self.llbuilder);
927 let fn_: ValueRef = llvm::LLVMGetBasicBlockParent(bb);
928 let m: ModuleRef = llvm::LLVMGetGlobalParent(fn_);
929 let t: ValueRef = "llvm.trap".with_c_str(|buf| {
930 llvm::LLVMGetNamedFunction(m, buf)
932 assert!((t as int != 0));
933 let args: &[ValueRef] = &[];
934 self.count_insn("trap");
936 self.llbuilder, t, args.as_ptr(), args.len() as c_uint, noname());
940 pub fn landing_pad(&self, ty: Type, pers_fn: ValueRef, num_clauses: uint) -> ValueRef {
941 self.count_insn("landingpad");
943 llvm::LLVMBuildLandingPad(
944 self.llbuilder, ty.to_ref(), pers_fn, num_clauses as c_uint, noname())
948 pub fn set_cleanup(&self, landing_pad: ValueRef) {
949 self.count_insn("setcleanup");
951 llvm::LLVMSetCleanup(landing_pad, llvm::True);
955 pub fn resume(&self, exn: ValueRef) -> ValueRef {
956 self.count_insn("resume");
958 llvm::LLVMBuildResume(self.llbuilder, exn)
963 pub fn atomic_cmpxchg(&self, dst: ValueRef,
964 cmp: ValueRef, src: ValueRef,
965 order: AtomicOrdering,
966 failure_order: AtomicOrdering) -> ValueRef {
968 llvm::LLVMBuildAtomicCmpXchg(self.llbuilder, dst, cmp, src,
969 order, failure_order)
972 pub fn atomic_rmw(&self, op: AtomicBinOp,
973 dst: ValueRef, src: ValueRef,
974 order: AtomicOrdering) -> ValueRef {
976 llvm::LLVMBuildAtomicRMW(self.llbuilder, op, dst, src, order, False)
980 pub fn atomic_fence(&self, order: AtomicOrdering) {
982 llvm::LLVMBuildAtomicFence(self.llbuilder, order);