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};
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: uint) -> ValueRef {
145 llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, num_cases as c_uint)
149 pub fn indirect_br(&self, addr: ValueRef, num_dests: uint) {
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) {
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) {
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);
535 pub fn atomic_store(&self, val: ValueRef, ptr: ValueRef, order: AtomicOrdering) {
536 debug!("Store {} -> {}",
537 self.ccx.tn().val_to_string(val),
538 self.ccx.tn().val_to_string(ptr));
539 self.count_insn("store.atomic");
541 let ty = Type::from_ref(llvm::LLVMTypeOf(ptr));
542 let align = llalign_of_pref(self.ccx, ty.element_type());
543 llvm::LLVMBuildAtomicStore(self.llbuilder, val, ptr, order, align as c_uint);
547 pub fn gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef {
548 self.count_insn("gep");
550 llvm::LLVMBuildGEP(self.llbuilder, ptr, indices.as_ptr(),
551 indices.len() as c_uint, noname())
555 // Simple wrapper around GEP that takes an array of ints and wraps them
558 pub fn gepi(&self, base: ValueRef, ixs: &[uint]) -> ValueRef {
559 // Small vector optimization. This should catch 100% of the cases that
562 let mut small_vec = [ C_i32(self.ccx, 0); 16 ];
563 for (small_vec_e, &ix) in small_vec.iter_mut().zip(ixs.iter()) {
564 *small_vec_e = C_i32(self.ccx, ix as i32);
566 self.inbounds_gep(base, &small_vec[..ixs.len()])
568 let v = ixs.iter().map(|i| C_i32(self.ccx, *i as i32)).collect::<Vec<ValueRef>>();
569 self.count_insn("gepi");
570 self.inbounds_gep(base, &v[..])
574 pub fn inbounds_gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef {
575 self.count_insn("inboundsgep");
577 llvm::LLVMBuildInBoundsGEP(
578 self.llbuilder, ptr, indices.as_ptr(), indices.len() as c_uint, noname())
582 pub fn struct_gep(&self, ptr: ValueRef, idx: uint) -> ValueRef {
583 self.count_insn("structgep");
585 llvm::LLVMBuildStructGEP(self.llbuilder, ptr, idx as c_uint, noname())
589 pub fn global_string(&self, _str: *const c_char) -> ValueRef {
590 self.count_insn("globalstring");
592 llvm::LLVMBuildGlobalString(self.llbuilder, _str, noname())
596 pub fn global_string_ptr(&self, _str: *const c_char) -> ValueRef {
597 self.count_insn("globalstringptr");
599 llvm::LLVMBuildGlobalStringPtr(self.llbuilder, _str, noname())
604 pub fn trunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
605 self.count_insn("trunc");
607 llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty.to_ref(), noname())
611 pub fn zext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
612 self.count_insn("zext");
614 llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty.to_ref(), noname())
618 pub fn sext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
619 self.count_insn("sext");
621 llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty.to_ref(), noname())
625 pub fn fptoui(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
626 self.count_insn("fptoui");
628 llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty.to_ref(), noname())
632 pub fn fptosi(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
633 self.count_insn("fptosi");
635 llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty.to_ref(),noname())
639 pub fn uitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
640 self.count_insn("uitofp");
642 llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty.to_ref(), noname())
646 pub fn sitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
647 self.count_insn("sitofp");
649 llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty.to_ref(), noname())
653 pub fn fptrunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
654 self.count_insn("fptrunc");
656 llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty.to_ref(), noname())
660 pub fn fpext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
661 self.count_insn("fpext");
663 llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty.to_ref(), noname())
667 pub fn ptrtoint(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
668 self.count_insn("ptrtoint");
670 llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty.to_ref(), noname())
674 pub fn inttoptr(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
675 self.count_insn("inttoptr");
677 llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty.to_ref(), noname())
681 pub fn bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
682 self.count_insn("bitcast");
684 llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
688 pub fn zext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
689 self.count_insn("zextorbitcast");
691 llvm::LLVMBuildZExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
695 pub fn sext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
696 self.count_insn("sextorbitcast");
698 llvm::LLVMBuildSExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
702 pub fn trunc_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
703 self.count_insn("truncorbitcast");
705 llvm::LLVMBuildTruncOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
709 pub fn cast(&self, op: Opcode, val: ValueRef, dest_ty: Type) -> ValueRef {
710 self.count_insn("cast");
712 llvm::LLVMBuildCast(self.llbuilder, op, val, dest_ty.to_ref(), noname())
716 pub fn pointercast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
717 self.count_insn("pointercast");
719 llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty.to_ref(), noname())
723 pub fn intcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
724 self.count_insn("intcast");
726 llvm::LLVMBuildIntCast(self.llbuilder, val, dest_ty.to_ref(), noname())
730 pub fn fpcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
731 self.count_insn("fpcast");
733 llvm::LLVMBuildFPCast(self.llbuilder, val, dest_ty.to_ref(), noname())
739 pub fn icmp(&self, op: IntPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
740 self.count_insn("icmp");
742 llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
746 pub fn fcmp(&self, op: RealPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
747 self.count_insn("fcmp");
749 llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
753 /* Miscellaneous instructions */
754 pub fn empty_phi(&self, ty: Type) -> ValueRef {
755 self.count_insn("emptyphi");
757 llvm::LLVMBuildPhi(self.llbuilder, ty.to_ref(), noname())
761 pub fn phi(&self, ty: Type, vals: &[ValueRef], bbs: &[BasicBlockRef]) -> ValueRef {
762 assert_eq!(vals.len(), bbs.len());
763 let phi = self.empty_phi(ty);
764 self.count_insn("addincoming");
766 llvm::LLVMAddIncoming(phi, vals.as_ptr(),
768 vals.len() as c_uint);
773 pub fn add_span_comment(&self, sp: Span, text: &str) {
774 if self.ccx.sess().asm_comments() {
775 let s = format!("{} ({})",
777 self.ccx.sess().codemap().span_to_string(sp));
778 debug!("{}", &s[..]);
779 self.add_comment(&s[..]);
783 pub fn add_comment(&self, text: &str) {
784 if self.ccx.sess().asm_comments() {
785 let sanitized = text.replace("$", "");
786 let comment_text = format!("{} {}", "#",
787 sanitized.replace("\n", "\n\t# "));
788 self.count_insn("inlineasm");
789 let comment_text = CString::new(comment_text).unwrap();
791 llvm::LLVMConstInlineAsm(Type::func(&[], &Type::void(self.ccx)).to_ref(),
792 comment_text.as_ptr(), noname(), False,
795 self.call(asm, &[], None);
799 pub fn inline_asm_call(&self, asm: *const c_char, cons: *const c_char,
800 inputs: &[ValueRef], output: Type,
801 volatile: bool, alignstack: bool,
802 dia: AsmDialect) -> ValueRef {
803 self.count_insn("inlineasm");
805 let volatile = if volatile { llvm::True }
806 else { llvm::False };
807 let alignstack = if alignstack { llvm::True }
808 else { llvm::False };
810 let argtys = inputs.iter().map(|v| {
811 debug!("Asm Input Type: {}", self.ccx.tn().val_to_string(*v));
813 }).collect::<Vec<_>>();
815 debug!("Asm Output Type: {}", self.ccx.tn().type_to_string(output));
816 let fty = Type::func(&argtys[..], &output);
818 let v = llvm::LLVMInlineAsm(
819 fty.to_ref(), asm, cons, volatile, alignstack, dia as c_uint);
820 self.call(v, inputs, None)
824 pub fn call(&self, llfn: ValueRef, args: &[ValueRef],
825 attributes: Option<AttrBuilder>) -> ValueRef {
826 self.count_insn("call");
828 debug!("Call {} with args ({})",
829 self.ccx.tn().val_to_string(llfn),
831 .map(|&v| self.ccx.tn().val_to_string(v))
832 .collect::<Vec<String>>()
836 let v = llvm::LLVMBuildCall(self.llbuilder, llfn, args.as_ptr(),
837 args.len() as c_uint, noname());
839 Some(a) => a.apply_callsite(v),
846 pub fn call_with_conv(&self, llfn: ValueRef, args: &[ValueRef],
847 conv: CallConv, attributes: Option<AttrBuilder>) -> ValueRef {
848 self.count_insn("callwithconv");
849 let v = self.call(llfn, args, attributes);
850 llvm::SetInstructionCallConv(v, conv);
854 pub fn select(&self, cond: ValueRef, then_val: ValueRef, else_val: ValueRef) -> ValueRef {
855 self.count_insn("select");
857 llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, noname())
861 pub fn va_arg(&self, list: ValueRef, ty: Type) -> ValueRef {
862 self.count_insn("vaarg");
864 llvm::LLVMBuildVAArg(self.llbuilder, list, ty.to_ref(), noname())
868 pub fn extract_element(&self, vec: ValueRef, idx: ValueRef) -> ValueRef {
869 self.count_insn("extractelement");
871 llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, noname())
875 pub fn insert_element(&self, vec: ValueRef, elt: ValueRef, idx: ValueRef) -> ValueRef {
876 self.count_insn("insertelement");
878 llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, noname())
882 pub fn shuffle_vector(&self, v1: ValueRef, v2: ValueRef, mask: ValueRef) -> ValueRef {
883 self.count_insn("shufflevector");
885 llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, noname())
889 pub fn vector_splat(&self, num_elts: uint, elt: ValueRef) -> ValueRef {
891 let elt_ty = val_ty(elt);
892 let undef = llvm::LLVMGetUndef(Type::vector(&elt_ty, num_elts as u64).to_ref());
893 let vec = self.insert_element(undef, elt, C_i32(self.ccx, 0));
894 let vec_i32_ty = Type::vector(&Type::i32(self.ccx), num_elts as u64);
895 self.shuffle_vector(vec, undef, C_null(vec_i32_ty))
899 pub fn extract_value(&self, agg_val: ValueRef, idx: uint) -> ValueRef {
900 self.count_insn("extractvalue");
902 llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, noname())
906 pub fn insert_value(&self, agg_val: ValueRef, elt: ValueRef,
907 idx: uint) -> ValueRef {
908 self.count_insn("insertvalue");
910 llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint,
915 pub fn is_null(&self, val: ValueRef) -> ValueRef {
916 self.count_insn("isnull");
918 llvm::LLVMBuildIsNull(self.llbuilder, val, noname())
922 pub fn is_not_null(&self, val: ValueRef) -> ValueRef {
923 self.count_insn("isnotnull");
925 llvm::LLVMBuildIsNotNull(self.llbuilder, val, noname())
929 pub fn ptrdiff(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
930 self.count_insn("ptrdiff");
932 llvm::LLVMBuildPtrDiff(self.llbuilder, lhs, rhs, noname())
938 let bb: BasicBlockRef = llvm::LLVMGetInsertBlock(self.llbuilder);
939 let fn_: ValueRef = llvm::LLVMGetBasicBlockParent(bb);
940 let m: ModuleRef = llvm::LLVMGetGlobalParent(fn_);
941 let p = "llvm.trap\0".as_ptr();
942 let t: ValueRef = llvm::LLVMGetNamedFunction(m, p as *const _);
943 assert!((t as int != 0));
944 let args: &[ValueRef] = &[];
945 self.count_insn("trap");
947 self.llbuilder, t, args.as_ptr(), args.len() as c_uint, noname());
951 pub fn landing_pad(&self, ty: Type, pers_fn: ValueRef, num_clauses: uint) -> ValueRef {
952 self.count_insn("landingpad");
954 llvm::LLVMBuildLandingPad(
955 self.llbuilder, ty.to_ref(), pers_fn, num_clauses as c_uint, noname())
959 pub fn set_cleanup(&self, landing_pad: ValueRef) {
960 self.count_insn("setcleanup");
962 llvm::LLVMSetCleanup(landing_pad, llvm::True);
966 pub fn resume(&self, exn: ValueRef) -> ValueRef {
967 self.count_insn("resume");
969 llvm::LLVMBuildResume(self.llbuilder, exn)
974 pub fn atomic_cmpxchg(&self, dst: ValueRef,
975 cmp: ValueRef, src: ValueRef,
976 order: AtomicOrdering,
977 failure_order: AtomicOrdering) -> ValueRef {
979 llvm::LLVMBuildAtomicCmpXchg(self.llbuilder, dst, cmp, src,
980 order, failure_order)
983 pub fn atomic_rmw(&self, op: AtomicBinOp,
984 dst: ValueRef, src: ValueRef,
985 order: AtomicOrdering) -> ValueRef {
987 llvm::LLVMBuildAtomicRMW(self.llbuilder, op, dst, src, order, False)
991 pub fn atomic_fence(&self, order: AtomicOrdering) {
993 llvm::LLVMBuildAtomicFence(self.llbuilder, order);