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, SynchronizationScope, 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};
24 use std::ffi::CString;
26 use syntax::codemap::Span;
28 pub struct Builder<'a, 'tcx: 'a> {
29 pub llbuilder: BuilderRef,
30 pub ccx: &'a CrateContext<'a, 'tcx>,
33 // This is a really awful way to get a zero-length c-string, but better (and a
34 // lot more efficient) than doing str::as_c_str("", ...) every time.
35 pub fn noname() -> *const c_char {
36 static CNULL: c_char = 0;
40 impl<'a, 'tcx> Builder<'a, 'tcx> {
41 pub fn new(ccx: &'a CrateContext<'a, 'tcx>) -> Builder<'a, 'tcx> {
43 llbuilder: ccx.raw_builder(),
48 pub fn count_insn(&self, category: &str) {
49 if self.ccx.sess().trans_stats() {
50 self.ccx.stats().n_llvm_insns.set(self.ccx
55 self.ccx.count_llvm_insn();
56 if self.ccx.sess().count_llvm_insns() {
57 base::with_insn_ctxt(|v| {
58 let mut h = self.ccx.stats().llvm_insns.borrow_mut();
60 // Build version of path with cycles removed.
62 // Pass 1: scan table mapping str -> rightmost pos.
63 let mut mm = FnvHashMap();
71 // Pass 2: concat strings for each elt, skipping
72 // forwards over any cycles by advancing to rightmost
73 // occurrence of each element in path.
74 let mut s = String::from_str(".");
86 let n = match h.get(&s) {
95 pub fn position_before(&self, insn: ValueRef) {
97 llvm::LLVMPositionBuilderBefore(self.llbuilder, insn);
101 pub fn position_at_end(&self, llbb: BasicBlockRef) {
103 llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
107 pub fn ret_void(&self) {
108 self.count_insn("retvoid");
110 llvm::LLVMBuildRetVoid(self.llbuilder);
114 pub fn ret(&self, v: ValueRef) {
115 self.count_insn("ret");
117 llvm::LLVMBuildRet(self.llbuilder, v);
121 pub fn aggregate_ret(&self, ret_vals: &[ValueRef]) {
123 llvm::LLVMBuildAggregateRet(self.llbuilder,
125 ret_vals.len() as c_uint);
129 pub fn br(&self, dest: BasicBlockRef) {
130 self.count_insn("br");
132 llvm::LLVMBuildBr(self.llbuilder, dest);
136 pub fn cond_br(&self, cond: ValueRef, then_llbb: BasicBlockRef, else_llbb: BasicBlockRef) {
137 self.count_insn("condbr");
139 llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
143 pub fn switch(&self, v: ValueRef, else_llbb: BasicBlockRef, num_cases: usize) -> ValueRef {
145 llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, num_cases as c_uint)
149 pub fn indirect_br(&self, addr: ValueRef, num_dests: usize) {
150 self.count_insn("indirectbr");
152 llvm::LLVMBuildIndirectBr(self.llbuilder, addr, num_dests as c_uint);
160 catch: BasicBlockRef,
161 attributes: Option<AttrBuilder>)
163 self.count_insn("invoke");
165 debug!("Invoke {} with args ({})",
166 self.ccx.tn().val_to_string(llfn),
168 .map(|&v| self.ccx.tn().val_to_string(v))
169 .collect::<Vec<String>>()
173 let v = llvm::LLVMBuildInvoke(self.llbuilder,
176 args.len() as c_uint,
181 Some(a) => a.apply_callsite(v),
188 pub fn unreachable(&self) {
189 self.count_insn("unreachable");
191 llvm::LLVMBuildUnreachable(self.llbuilder);
196 pub fn add(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
197 self.count_insn("add");
199 llvm::LLVMBuildAdd(self.llbuilder, lhs, rhs, noname())
203 pub fn nswadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
204 self.count_insn("nswadd");
206 llvm::LLVMBuildNSWAdd(self.llbuilder, lhs, rhs, noname())
210 pub fn nuwadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
211 self.count_insn("nuwadd");
213 llvm::LLVMBuildNUWAdd(self.llbuilder, lhs, rhs, noname())
217 pub fn fadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
218 self.count_insn("fadd");
220 llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname())
224 pub fn sub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
225 self.count_insn("sub");
227 llvm::LLVMBuildSub(self.llbuilder, lhs, rhs, noname())
231 pub fn nswsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
232 self.count_insn("nwsub");
234 llvm::LLVMBuildNSWSub(self.llbuilder, lhs, rhs, noname())
238 pub fn nuwsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
239 self.count_insn("nuwsub");
241 llvm::LLVMBuildNUWSub(self.llbuilder, lhs, rhs, noname())
245 pub fn fsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
246 self.count_insn("sub");
248 llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname())
252 pub fn mul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
253 self.count_insn("mul");
255 llvm::LLVMBuildMul(self.llbuilder, lhs, rhs, noname())
259 pub fn nswmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
260 self.count_insn("nswmul");
262 llvm::LLVMBuildNSWMul(self.llbuilder, lhs, rhs, noname())
266 pub fn nuwmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
267 self.count_insn("nuwmul");
269 llvm::LLVMBuildNUWMul(self.llbuilder, lhs, rhs, noname())
273 pub fn fmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
274 self.count_insn("fmul");
276 llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname())
280 pub fn udiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
281 self.count_insn("udiv");
283 llvm::LLVMBuildUDiv(self.llbuilder, lhs, rhs, noname())
287 pub fn sdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
288 self.count_insn("sdiv");
290 llvm::LLVMBuildSDiv(self.llbuilder, lhs, rhs, noname())
294 pub fn exactsdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
295 self.count_insn("exactsdiv");
297 llvm::LLVMBuildExactSDiv(self.llbuilder, lhs, rhs, noname())
301 pub fn fdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
302 self.count_insn("fdiv");
304 llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname())
308 pub fn urem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
309 self.count_insn("urem");
311 llvm::LLVMBuildURem(self.llbuilder, lhs, rhs, noname())
315 pub fn srem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
316 self.count_insn("srem");
318 llvm::LLVMBuildSRem(self.llbuilder, lhs, rhs, noname())
322 pub fn frem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
323 self.count_insn("frem");
325 llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname())
329 pub fn shl(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
330 self.count_insn("shl");
332 llvm::LLVMBuildShl(self.llbuilder, lhs, rhs, noname())
336 pub fn lshr(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
337 self.count_insn("lshr");
339 llvm::LLVMBuildLShr(self.llbuilder, lhs, rhs, noname())
343 pub fn ashr(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
344 self.count_insn("ashr");
346 llvm::LLVMBuildAShr(self.llbuilder, lhs, rhs, noname())
350 pub fn and(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
351 self.count_insn("and");
353 llvm::LLVMBuildAnd(self.llbuilder, lhs, rhs, noname())
357 pub fn or(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
358 self.count_insn("or");
360 llvm::LLVMBuildOr(self.llbuilder, lhs, rhs, noname())
364 pub fn xor(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
365 self.count_insn("xor");
367 llvm::LLVMBuildXor(self.llbuilder, lhs, rhs, noname())
371 pub fn binop(&self, op: Opcode, lhs: ValueRef, rhs: ValueRef)
373 self.count_insn("binop");
375 llvm::LLVMBuildBinOp(self.llbuilder, op, lhs, rhs, noname())
379 pub fn neg(&self, v: ValueRef) -> ValueRef {
380 self.count_insn("neg");
382 llvm::LLVMBuildNeg(self.llbuilder, v, noname())
386 pub fn nswneg(&self, v: ValueRef) -> ValueRef {
387 self.count_insn("nswneg");
389 llvm::LLVMBuildNSWNeg(self.llbuilder, v, noname())
393 pub fn nuwneg(&self, v: ValueRef) -> ValueRef {
394 self.count_insn("nuwneg");
396 llvm::LLVMBuildNUWNeg(self.llbuilder, v, noname())
399 pub fn fneg(&self, v: ValueRef) -> ValueRef {
400 self.count_insn("fneg");
402 llvm::LLVMBuildFNeg(self.llbuilder, v, noname())
406 pub fn not(&self, v: ValueRef) -> ValueRef {
407 self.count_insn("not");
409 llvm::LLVMBuildNot(self.llbuilder, v, noname())
414 pub fn malloc(&self, ty: Type) -> ValueRef {
415 self.count_insn("malloc");
417 llvm::LLVMBuildMalloc(self.llbuilder, ty.to_ref(), noname())
421 pub fn array_malloc(&self, ty: Type, val: ValueRef) -> ValueRef {
422 self.count_insn("arraymalloc");
424 llvm::LLVMBuildArrayMalloc(self.llbuilder, ty.to_ref(), val, noname())
428 pub fn alloca(&self, ty: Type, name: &str) -> ValueRef {
429 self.count_insn("alloca");
432 llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), noname())
434 let name = CString::new(name).unwrap();
435 llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(),
441 pub fn array_alloca(&self, ty: Type, val: ValueRef) -> ValueRef {
442 self.count_insn("arrayalloca");
444 llvm::LLVMBuildArrayAlloca(self.llbuilder, ty.to_ref(), val, noname())
448 pub fn free(&self, ptr: ValueRef) {
449 self.count_insn("free");
451 llvm::LLVMBuildFree(self.llbuilder, ptr);
455 pub fn load(&self, ptr: ValueRef) -> ValueRef {
456 self.count_insn("load");
458 llvm::LLVMBuildLoad(self.llbuilder, ptr, noname())
462 pub fn volatile_load(&self, ptr: ValueRef) -> ValueRef {
463 self.count_insn("load.volatile");
465 let insn = llvm::LLVMBuildLoad(self.llbuilder, ptr, noname());
466 llvm::LLVMSetVolatile(insn, llvm::True);
471 pub fn atomic_load(&self, ptr: ValueRef, order: AtomicOrdering) -> ValueRef {
472 self.count_insn("load.atomic");
474 let ty = Type::from_ref(llvm::LLVMTypeOf(ptr));
475 let align = llalign_of_pref(self.ccx, ty.element_type());
476 llvm::LLVMBuildAtomicLoad(self.llbuilder, ptr, noname(), order,
482 pub fn load_range_assert(&self, ptr: ValueRef, lo: u64,
483 hi: u64, signed: llvm::Bool) -> ValueRef {
484 let value = self.load(ptr);
487 let t = llvm::LLVMGetElementType(llvm::LLVMTypeOf(ptr));
488 let min = llvm::LLVMConstInt(t, lo, signed);
489 let max = llvm::LLVMConstInt(t, hi, signed);
493 llvm::LLVMSetMetadata(value, llvm::MD_range as c_uint,
494 llvm::LLVMMDNodeInContext(self.ccx.llcx(),
502 pub fn load_nonnull(&self, ptr: ValueRef) -> ValueRef {
503 let value = self.load(ptr);
505 llvm::LLVMSetMetadata(value, llvm::MD_nonnull as c_uint,
506 llvm::LLVMMDNodeInContext(self.ccx.llcx(), ptr::null(), 0));
512 pub fn store(&self, val: ValueRef, ptr: ValueRef) -> ValueRef {
513 debug!("Store {} -> {}",
514 self.ccx.tn().val_to_string(val),
515 self.ccx.tn().val_to_string(ptr));
516 assert!(!self.llbuilder.is_null());
517 self.count_insn("store");
519 llvm::LLVMBuildStore(self.llbuilder, val, ptr)
523 pub fn volatile_store(&self, val: ValueRef, ptr: ValueRef) -> ValueRef {
524 debug!("Store {} -> {}",
525 self.ccx.tn().val_to_string(val),
526 self.ccx.tn().val_to_string(ptr));
527 assert!(!self.llbuilder.is_null());
528 self.count_insn("store.volatile");
530 let insn = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
531 llvm::LLVMSetVolatile(insn, llvm::True);
536 pub fn atomic_store(&self, val: ValueRef, ptr: ValueRef, order: AtomicOrdering) {
537 debug!("Store {} -> {}",
538 self.ccx.tn().val_to_string(val),
539 self.ccx.tn().val_to_string(ptr));
540 self.count_insn("store.atomic");
542 let ty = Type::from_ref(llvm::LLVMTypeOf(ptr));
543 let align = llalign_of_pref(self.ccx, ty.element_type());
544 llvm::LLVMBuildAtomicStore(self.llbuilder, val, ptr, order, align as c_uint);
548 pub fn gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef {
549 self.count_insn("gep");
551 llvm::LLVMBuildGEP(self.llbuilder, ptr, indices.as_ptr(),
552 indices.len() as c_uint, noname())
556 // Simple wrapper around GEP that takes an array of ints and wraps them
559 pub fn gepi(&self, base: ValueRef, ixs: &[usize]) -> ValueRef {
560 // Small vector optimization. This should catch 100% of the cases that
563 let mut small_vec = [ C_i32(self.ccx, 0); 16 ];
564 for (small_vec_e, &ix) in small_vec.iter_mut().zip(ixs.iter()) {
565 *small_vec_e = C_i32(self.ccx, ix as i32);
567 self.inbounds_gep(base, &small_vec[..ixs.len()])
569 let v = ixs.iter().map(|i| C_i32(self.ccx, *i as i32)).collect::<Vec<ValueRef>>();
570 self.count_insn("gepi");
571 self.inbounds_gep(base, &v[..])
575 pub fn inbounds_gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef {
576 self.count_insn("inboundsgep");
578 llvm::LLVMBuildInBoundsGEP(
579 self.llbuilder, ptr, indices.as_ptr(), indices.len() as c_uint, noname())
583 pub fn struct_gep(&self, ptr: ValueRef, idx: usize) -> ValueRef {
584 self.count_insn("structgep");
586 llvm::LLVMBuildStructGEP(self.llbuilder, ptr, idx as c_uint, noname())
590 pub fn global_string(&self, _str: *const c_char) -> ValueRef {
591 self.count_insn("globalstring");
593 llvm::LLVMBuildGlobalString(self.llbuilder, _str, noname())
597 pub fn global_string_ptr(&self, _str: *const c_char) -> ValueRef {
598 self.count_insn("globalstringptr");
600 llvm::LLVMBuildGlobalStringPtr(self.llbuilder, _str, noname())
605 pub fn trunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
606 self.count_insn("trunc");
608 llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty.to_ref(), noname())
612 pub fn zext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
613 self.count_insn("zext");
615 llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty.to_ref(), noname())
619 pub fn sext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
620 self.count_insn("sext");
622 llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty.to_ref(), noname())
626 pub fn fptoui(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
627 self.count_insn("fptoui");
629 llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty.to_ref(), noname())
633 pub fn fptosi(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
634 self.count_insn("fptosi");
636 llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty.to_ref(),noname())
640 pub fn uitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
641 self.count_insn("uitofp");
643 llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty.to_ref(), noname())
647 pub fn sitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
648 self.count_insn("sitofp");
650 llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty.to_ref(), noname())
654 pub fn fptrunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
655 self.count_insn("fptrunc");
657 llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty.to_ref(), noname())
661 pub fn fpext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
662 self.count_insn("fpext");
664 llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty.to_ref(), noname())
668 pub fn ptrtoint(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
669 self.count_insn("ptrtoint");
671 llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty.to_ref(), noname())
675 pub fn inttoptr(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
676 self.count_insn("inttoptr");
678 llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty.to_ref(), noname())
682 pub fn bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
683 self.count_insn("bitcast");
685 llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
689 pub fn zext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
690 self.count_insn("zextorbitcast");
692 llvm::LLVMBuildZExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
696 pub fn sext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
697 self.count_insn("sextorbitcast");
699 llvm::LLVMBuildSExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
703 pub fn trunc_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
704 self.count_insn("truncorbitcast");
706 llvm::LLVMBuildTruncOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
710 pub fn cast(&self, op: Opcode, val: ValueRef, dest_ty: Type) -> ValueRef {
711 self.count_insn("cast");
713 llvm::LLVMBuildCast(self.llbuilder, op, val, dest_ty.to_ref(), noname())
717 pub fn pointercast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
718 self.count_insn("pointercast");
720 llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty.to_ref(), noname())
724 pub fn intcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
725 self.count_insn("intcast");
727 llvm::LLVMBuildIntCast(self.llbuilder, val, dest_ty.to_ref(), noname())
731 pub fn fpcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
732 self.count_insn("fpcast");
734 llvm::LLVMBuildFPCast(self.llbuilder, val, dest_ty.to_ref(), noname())
740 pub fn icmp(&self, op: IntPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
741 self.count_insn("icmp");
743 llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
747 pub fn fcmp(&self, op: RealPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
748 self.count_insn("fcmp");
750 llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
754 /* Miscellaneous instructions */
755 pub fn empty_phi(&self, ty: Type) -> ValueRef {
756 self.count_insn("emptyphi");
758 llvm::LLVMBuildPhi(self.llbuilder, ty.to_ref(), noname())
762 pub fn phi(&self, ty: Type, vals: &[ValueRef], bbs: &[BasicBlockRef]) -> ValueRef {
763 assert_eq!(vals.len(), bbs.len());
764 let phi = self.empty_phi(ty);
765 self.count_insn("addincoming");
767 llvm::LLVMAddIncoming(phi, vals.as_ptr(),
769 vals.len() as c_uint);
774 pub fn add_span_comment(&self, sp: Span, text: &str) {
775 if self.ccx.sess().asm_comments() {
776 let s = format!("{} ({})",
778 self.ccx.sess().codemap().span_to_string(sp));
779 debug!("{}", &s[..]);
780 self.add_comment(&s[..]);
784 pub fn add_comment(&self, text: &str) {
785 if self.ccx.sess().asm_comments() {
786 let sanitized = text.replace("$", "");
787 let comment_text = format!("{} {}", "#",
788 sanitized.replace("\n", "\n\t# "));
789 self.count_insn("inlineasm");
790 let comment_text = CString::new(comment_text).unwrap();
792 llvm::LLVMConstInlineAsm(Type::func(&[], &Type::void(self.ccx)).to_ref(),
793 comment_text.as_ptr(), noname(), False,
796 self.call(asm, &[], None);
800 pub fn inline_asm_call(&self, asm: *const c_char, cons: *const c_char,
801 inputs: &[ValueRef], output: Type,
802 volatile: bool, alignstack: bool,
803 dia: AsmDialect) -> ValueRef {
804 self.count_insn("inlineasm");
806 let volatile = if volatile { llvm::True }
807 else { llvm::False };
808 let alignstack = if alignstack { llvm::True }
809 else { llvm::False };
811 let argtys = inputs.iter().map(|v| {
812 debug!("Asm Input Type: {}", self.ccx.tn().val_to_string(*v));
814 }).collect::<Vec<_>>();
816 debug!("Asm Output Type: {}", self.ccx.tn().type_to_string(output));
817 let fty = Type::func(&argtys[..], &output);
819 let v = llvm::LLVMInlineAsm(
820 fty.to_ref(), asm, cons, volatile, alignstack, dia as c_uint);
821 self.call(v, inputs, None)
825 pub fn call(&self, llfn: ValueRef, args: &[ValueRef],
826 attributes: Option<AttrBuilder>) -> ValueRef {
827 self.count_insn("call");
829 debug!("Call {} with args ({})",
830 self.ccx.tn().val_to_string(llfn),
832 .map(|&v| self.ccx.tn().val_to_string(v))
833 .collect::<Vec<String>>()
837 let v = llvm::LLVMBuildCall(self.llbuilder, llfn, args.as_ptr(),
838 args.len() as c_uint, noname());
840 Some(a) => a.apply_callsite(v),
847 pub fn call_with_conv(&self, llfn: ValueRef, args: &[ValueRef],
848 conv: CallConv, attributes: Option<AttrBuilder>) -> ValueRef {
849 self.count_insn("callwithconv");
850 let v = self.call(llfn, args, attributes);
851 llvm::SetInstructionCallConv(v, conv);
855 pub fn select(&self, cond: ValueRef, then_val: ValueRef, else_val: ValueRef) -> ValueRef {
856 self.count_insn("select");
858 llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, noname())
862 pub fn va_arg(&self, list: ValueRef, ty: Type) -> ValueRef {
863 self.count_insn("vaarg");
865 llvm::LLVMBuildVAArg(self.llbuilder, list, ty.to_ref(), noname())
869 pub fn extract_element(&self, vec: ValueRef, idx: ValueRef) -> ValueRef {
870 self.count_insn("extractelement");
872 llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, noname())
876 pub fn insert_element(&self, vec: ValueRef, elt: ValueRef, idx: ValueRef) -> ValueRef {
877 self.count_insn("insertelement");
879 llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, noname())
883 pub fn shuffle_vector(&self, v1: ValueRef, v2: ValueRef, mask: ValueRef) -> ValueRef {
884 self.count_insn("shufflevector");
886 llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, noname())
890 pub fn vector_splat(&self, num_elts: usize, elt: ValueRef) -> ValueRef {
892 let elt_ty = val_ty(elt);
893 let undef = llvm::LLVMGetUndef(Type::vector(&elt_ty, num_elts as u64).to_ref());
894 let vec = self.insert_element(undef, elt, C_i32(self.ccx, 0));
895 let vec_i32_ty = Type::vector(&Type::i32(self.ccx), num_elts as u64);
896 self.shuffle_vector(vec, undef, C_null(vec_i32_ty))
900 pub fn extract_value(&self, agg_val: ValueRef, idx: usize) -> ValueRef {
901 self.count_insn("extractvalue");
903 llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, noname())
907 pub fn insert_value(&self, agg_val: ValueRef, elt: ValueRef,
908 idx: usize) -> ValueRef {
909 self.count_insn("insertvalue");
911 llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint,
916 pub fn is_null(&self, val: ValueRef) -> ValueRef {
917 self.count_insn("isnull");
919 llvm::LLVMBuildIsNull(self.llbuilder, val, noname())
923 pub fn is_not_null(&self, val: ValueRef) -> ValueRef {
924 self.count_insn("isnotnull");
926 llvm::LLVMBuildIsNotNull(self.llbuilder, val, noname())
930 pub fn ptrdiff(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
931 self.count_insn("ptrdiff");
933 llvm::LLVMBuildPtrDiff(self.llbuilder, lhs, rhs, noname())
939 let bb: BasicBlockRef = llvm::LLVMGetInsertBlock(self.llbuilder);
940 let fn_: ValueRef = llvm::LLVMGetBasicBlockParent(bb);
941 let m: ModuleRef = llvm::LLVMGetGlobalParent(fn_);
942 let p = "llvm.trap\0".as_ptr();
943 let t: ValueRef = llvm::LLVMGetNamedFunction(m, p as *const _);
944 assert!((t as isize != 0));
945 let args: &[ValueRef] = &[];
946 self.count_insn("trap");
948 self.llbuilder, t, args.as_ptr(), args.len() as c_uint, noname());
952 pub fn landing_pad(&self, ty: Type, pers_fn: ValueRef, num_clauses: usize) -> ValueRef {
953 self.count_insn("landingpad");
955 llvm::LLVMBuildLandingPad(
956 self.llbuilder, ty.to_ref(), pers_fn, num_clauses as c_uint, noname())
960 pub fn set_cleanup(&self, landing_pad: ValueRef) {
961 self.count_insn("setcleanup");
963 llvm::LLVMSetCleanup(landing_pad, llvm::True);
967 pub fn resume(&self, exn: ValueRef) -> ValueRef {
968 self.count_insn("resume");
970 llvm::LLVMBuildResume(self.llbuilder, exn)
975 pub fn atomic_cmpxchg(&self, dst: ValueRef,
976 cmp: ValueRef, src: ValueRef,
977 order: AtomicOrdering,
978 failure_order: AtomicOrdering) -> ValueRef {
980 llvm::LLVMBuildAtomicCmpXchg(self.llbuilder, dst, cmp, src,
981 order, failure_order)
984 pub fn atomic_rmw(&self, op: AtomicBinOp,
985 dst: ValueRef, src: ValueRef,
986 order: AtomicOrdering) -> ValueRef {
988 llvm::LLVMBuildAtomicRMW(self.llbuilder, op, dst, src, order, False)
992 pub fn atomic_fence(&self, order: AtomicOrdering, scope: SynchronizationScope) {
994 llvm::LLVMBuildAtomicFence(self.llbuilder, order, scope);