2013-07-21 08:33:40 -05:00
|
|
|
// Copyright 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.
|
|
|
|
|
2014-04-01 09:28:08 -05:00
|
|
|
#![allow(dead_code)] // FFI wrappers
|
|
|
|
|
2014-07-07 19:58:01 -05:00
|
|
|
use llvm;
|
2014-07-25 18:06:44 -05:00
|
|
|
use llvm::{CallConv, AtomicBinOp, AtomicOrdering, AsmDialect, AttrBuilder};
|
2014-07-07 19:58:01 -05:00
|
|
|
use llvm::{Opcode, IntPredicate, RealPredicate, False};
|
|
|
|
use llvm::{ValueRef, BasicBlockRef, BuilderRef, ModuleRef};
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::base;
|
|
|
|
use trans::common::*;
|
|
|
|
use trans::machine::llalign_of_pref;
|
|
|
|
use trans::type_::Type;
|
2014-11-09 16:59:56 -06:00
|
|
|
use util::nodemap::FnvHashMap;
|
2014-10-14 15:36:11 -05:00
|
|
|
use libc::{c_uint, c_char};
|
2014-05-22 18:57:53 -05:00
|
|
|
use std::string::String;
|
2013-08-31 11:13:04 -05:00
|
|
|
use syntax::codemap::Span;
|
2013-07-21 08:33:40 -05:00
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
pub struct Builder<'a, 'tcx: 'a> {
|
2014-03-28 12:05:27 -05:00
|
|
|
pub llbuilder: BuilderRef,
|
2014-04-22 07:56:37 -05:00
|
|
|
pub ccx: &'a CrateContext<'a, 'tcx>,
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is a really awful way to get a zero-length c-string, but better (and a
|
|
|
|
// lot more efficient) than doing str::as_c_str("", ...) every time.
|
2014-06-25 14:47:34 -05:00
|
|
|
pub fn noname() -> *const c_char {
|
2014-09-12 20:55:37 -05:00
|
|
|
static CNULL: c_char = 0;
|
|
|
|
&CNULL as *const c_char
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|
|
|
pub fn new(ccx: &'a CrateContext<'a, 'tcx>) -> Builder<'a, 'tcx> {
|
2013-07-21 08:33:40 -05:00
|
|
|
Builder {
|
2014-09-05 11:18:53 -05:00
|
|
|
llbuilder: ccx.raw_builder(),
|
2013-07-21 08:33:40 -05:00
|
|
|
ccx: ccx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn count_insn(&self, category: &str) {
|
2014-03-05 08:36:01 -06:00
|
|
|
if self.ccx.sess().trans_stats() {
|
2014-09-05 11:18:53 -05:00
|
|
|
self.ccx.stats().n_llvm_insns.set(self.ccx
|
|
|
|
.stats()
|
2013-12-22 15:52:05 -06:00
|
|
|
.n_llvm_insns
|
|
|
|
.get() + 1);
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
2014-07-21 18:42:34 -05:00
|
|
|
self.ccx.count_llvm_insn();
|
2014-03-05 08:36:01 -06:00
|
|
|
if self.ccx.sess().count_llvm_insns() {
|
2013-11-21 17:42:55 -06:00
|
|
|
base::with_insn_ctxt(|v| {
|
2014-09-05 11:18:53 -05:00
|
|
|
let mut h = self.ccx.stats().llvm_insns.borrow_mut();
|
2013-07-21 08:33:40 -05:00
|
|
|
|
|
|
|
// Build version of path with cycles removed.
|
|
|
|
|
|
|
|
// Pass 1: scan table mapping str -> rightmost pos.
|
2014-11-09 16:59:56 -06:00
|
|
|
let mut mm = FnvHashMap::new();
|
2013-07-21 08:33:40 -05:00
|
|
|
let len = v.len();
|
|
|
|
let mut i = 0u;
|
|
|
|
while i < len {
|
|
|
|
mm.insert(v[i], i);
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass 2: concat strings for each elt, skipping
|
|
|
|
// forwards over any cycles by advancing to rightmost
|
|
|
|
// occurrence of each element in path.
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut s = String::from_str(".");
|
2013-07-21 08:33:40 -05:00
|
|
|
i = 0u;
|
|
|
|
while i < len {
|
2014-10-15 01:05:01 -05:00
|
|
|
i = mm[v[i]];
|
|
|
|
s.push('/');
|
2013-07-21 08:33:40 -05:00
|
|
|
s.push_str(v[i]);
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
2014-10-15 01:05:01 -05:00
|
|
|
s.push('/');
|
2013-07-21 08:33:40 -05:00
|
|
|
s.push_str(category);
|
|
|
|
|
2014-11-06 11:25:16 -06:00
|
|
|
let n = match h.get(&s) {
|
2013-07-21 08:33:40 -05:00
|
|
|
Some(&n) => n,
|
|
|
|
_ => 0u
|
|
|
|
};
|
2014-03-20 21:49:20 -05:00
|
|
|
h.insert(s, n+1u);
|
2013-11-21 17:42:55 -06:00
|
|
|
})
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn position_before(&self, insn: ValueRef) {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMPositionBuilderBefore(self.llbuilder, insn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn position_at_end(&self, llbb: BasicBlockRef) {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ret_void(&self) {
|
|
|
|
self.count_insn("retvoid");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildRetVoid(self.llbuilder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ret(&self, v: ValueRef) {
|
|
|
|
self.count_insn("ret");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildRet(self.llbuilder, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn aggregate_ret(&self, ret_vals: &[ValueRef]) {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildAggregateRet(self.llbuilder,
|
2013-12-15 06:35:12 -06:00
|
|
|
ret_vals.as_ptr(),
|
2013-07-21 08:33:40 -05:00
|
|
|
ret_vals.len() as c_uint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn br(&self, dest: BasicBlockRef) {
|
|
|
|
self.count_insn("br");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildBr(self.llbuilder, dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cond_br(&self, cond: ValueRef, then_llbb: BasicBlockRef, else_llbb: BasicBlockRef) {
|
|
|
|
self.count_insn("condbr");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn switch(&self, v: ValueRef, else_llbb: BasicBlockRef, num_cases: uint) -> ValueRef {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, num_cases as c_uint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn indirect_br(&self, addr: ValueRef, num_dests: uint) {
|
|
|
|
self.count_insn("indirectbr");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildIndirectBr(self.llbuilder, addr, num_dests as c_uint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn invoke(&self,
|
|
|
|
llfn: ValueRef,
|
|
|
|
args: &[ValueRef],
|
|
|
|
then: BasicBlockRef,
|
2013-09-16 17:30:59 -05:00
|
|
|
catch: BasicBlockRef,
|
2014-07-25 18:06:44 -05:00
|
|
|
attributes: Option<AttrBuilder>)
|
2013-07-21 08:33:40 -05:00
|
|
|
-> ValueRef {
|
|
|
|
self.count_insn("invoke");
|
2014-06-28 14:55:17 -05:00
|
|
|
|
|
|
|
debug!("Invoke {} with args ({})",
|
2014-09-05 11:18:53 -05:00
|
|
|
self.ccx.tn().val_to_string(llfn),
|
2014-06-28 14:55:17 -05:00
|
|
|
args.iter()
|
2014-09-05 11:18:53 -05:00
|
|
|
.map(|&v| self.ccx.tn().val_to_string(v))
|
2014-06-28 14:55:17 -05:00
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.connect(", "));
|
|
|
|
|
2013-07-21 08:33:40 -05:00
|
|
|
unsafe {
|
2013-09-16 17:30:59 -05:00
|
|
|
let v = llvm::LLVMBuildInvoke(self.llbuilder,
|
|
|
|
llfn,
|
2013-12-15 06:35:12 -06:00
|
|
|
args.as_ptr(),
|
2013-09-16 17:30:59 -05:00
|
|
|
args.len() as c_uint,
|
|
|
|
then,
|
|
|
|
catch,
|
|
|
|
noname());
|
2014-07-25 18:06:44 -05:00
|
|
|
match attributes {
|
|
|
|
Some(a) => a.apply_callsite(v),
|
|
|
|
None => {}
|
2013-09-16 17:30:59 -05:00
|
|
|
}
|
|
|
|
v
|
|
|
|
}
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unreachable(&self) {
|
|
|
|
self.count_insn("unreachable");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildUnreachable(self.llbuilder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Arithmetic */
|
|
|
|
pub fn add(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("add");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildAdd(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nswadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("nswadd");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildNSWAdd(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nuwadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("nuwadd");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildNUWAdd(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("fadd");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("sub");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildSub(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nswsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("nwsub");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildNSWSub(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nuwsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("nuwsub");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildNUWSub(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("sub");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("mul");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildMul(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nswmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("nswmul");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildNSWMul(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nuwmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("nuwmul");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildNUWMul(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("fmul");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn udiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("udiv");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildUDiv(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("sdiv");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildSDiv(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exactsdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("exactsdiv");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildExactSDiv(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("fdiv");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn urem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("urem");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildURem(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn srem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("srem");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildSRem(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn frem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("frem");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shl(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("shl");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildShl(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lshr(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("lshr");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildLShr(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ashr(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("ashr");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildAShr(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn and(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("and");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildAnd(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn or(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("or");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildOr(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn xor(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("xor");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildXor(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn binop(&self, op: Opcode, lhs: ValueRef, rhs: ValueRef)
|
|
|
|
-> ValueRef {
|
|
|
|
self.count_insn("binop");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildBinOp(self.llbuilder, op, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 15:15:03 -06:00
|
|
|
pub fn neg(&self, v: ValueRef) -> ValueRef {
|
2013-07-21 08:33:40 -05:00
|
|
|
self.count_insn("neg");
|
|
|
|
unsafe {
|
2014-02-15 15:15:03 -06:00
|
|
|
llvm::LLVMBuildNeg(self.llbuilder, v, noname())
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 15:15:03 -06:00
|
|
|
pub fn nswneg(&self, v: ValueRef) -> ValueRef {
|
2013-07-21 08:33:40 -05:00
|
|
|
self.count_insn("nswneg");
|
|
|
|
unsafe {
|
2014-02-15 15:15:03 -06:00
|
|
|
llvm::LLVMBuildNSWNeg(self.llbuilder, v, noname())
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 15:15:03 -06:00
|
|
|
pub fn nuwneg(&self, v: ValueRef) -> ValueRef {
|
2013-07-21 08:33:40 -05:00
|
|
|
self.count_insn("nuwneg");
|
|
|
|
unsafe {
|
2014-02-15 15:15:03 -06:00
|
|
|
llvm::LLVMBuildNUWNeg(self.llbuilder, v, noname())
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-15 15:15:03 -06:00
|
|
|
pub fn fneg(&self, v: ValueRef) -> ValueRef {
|
2013-07-21 08:33:40 -05:00
|
|
|
self.count_insn("fneg");
|
|
|
|
unsafe {
|
2014-02-15 15:15:03 -06:00
|
|
|
llvm::LLVMBuildFNeg(self.llbuilder, v, noname())
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 15:15:03 -06:00
|
|
|
pub fn not(&self, v: ValueRef) -> ValueRef {
|
2013-07-21 08:33:40 -05:00
|
|
|
self.count_insn("not");
|
|
|
|
unsafe {
|
2014-02-15 15:15:03 -06:00
|
|
|
llvm::LLVMBuildNot(self.llbuilder, v, noname())
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Memory */
|
|
|
|
pub fn malloc(&self, ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("malloc");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildMalloc(self.llbuilder, ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn array_malloc(&self, ty: Type, val: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("arraymalloc");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildArrayMalloc(self.llbuilder, ty.to_ref(), val, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn alloca(&self, ty: Type, name: &str) -> ValueRef {
|
|
|
|
self.count_insn("alloca");
|
|
|
|
unsafe {
|
|
|
|
if name.is_empty() {
|
|
|
|
llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), noname())
|
|
|
|
} else {
|
2013-11-21 17:42:55 -06:00
|
|
|
name.with_c_str(|c| {
|
2013-07-22 23:41:46 -05:00
|
|
|
llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), c)
|
2013-11-21 17:42:55 -06:00
|
|
|
})
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn array_alloca(&self, ty: Type, val: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("arrayalloca");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildArrayAlloca(self.llbuilder, ty.to_ref(), val, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn free(&self, ptr: ValueRef) {
|
|
|
|
self.count_insn("free");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildFree(self.llbuilder, ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load(&self, ptr: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("load");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildLoad(self.llbuilder, ptr, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-28 08:01:53 -06:00
|
|
|
pub fn volatile_load(&self, ptr: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("load.volatile");
|
|
|
|
unsafe {
|
|
|
|
let insn = llvm::LLVMBuildLoad(self.llbuilder, ptr, noname());
|
2014-07-07 19:58:01 -05:00
|
|
|
llvm::LLVMSetVolatile(insn, llvm::True);
|
2013-12-28 08:01:53 -06:00
|
|
|
insn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-21 08:33:40 -05:00
|
|
|
pub fn atomic_load(&self, ptr: ValueRef, order: AtomicOrdering) -> ValueRef {
|
|
|
|
self.count_insn("load.atomic");
|
|
|
|
unsafe {
|
2014-01-15 17:32:44 -06:00
|
|
|
let ty = Type::from_ref(llvm::LLVMTypeOf(ptr));
|
|
|
|
let align = llalign_of_pref(self.ccx, ty.element_type());
|
|
|
|
llvm::LLVMBuildAtomicLoad(self.llbuilder, ptr, noname(), order,
|
|
|
|
align as c_uint)
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-14 15:36:11 -05:00
|
|
|
pub fn load_range_assert(&self, ptr: ValueRef, lo: u64,
|
|
|
|
hi: u64, signed: llvm::Bool) -> ValueRef {
|
2013-07-21 08:33:40 -05:00
|
|
|
let value = self.load(ptr);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let t = llvm::LLVMGetElementType(llvm::LLVMTypeOf(ptr));
|
|
|
|
let min = llvm::LLVMConstInt(t, lo, signed);
|
|
|
|
let max = llvm::LLVMConstInt(t, hi, signed);
|
|
|
|
|
2013-12-17 08:49:31 -06:00
|
|
|
let v = [min, max];
|
|
|
|
|
2014-07-07 19:58:01 -05:00
|
|
|
llvm::LLVMSetMetadata(value, llvm::MD_range as c_uint,
|
2014-09-05 11:18:53 -05:00
|
|
|
llvm::LLVMMDNodeInContext(self.ccx.llcx(),
|
2014-10-14 15:36:11 -05:00
|
|
|
v.as_ptr(),
|
|
|
|
v.len() as c_uint));
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
value
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn store(&self, val: ValueRef, ptr: ValueRef) {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Store {} -> {}",
|
2014-09-05 11:18:53 -05:00
|
|
|
self.ccx.tn().val_to_string(val),
|
|
|
|
self.ccx.tn().val_to_string(ptr));
|
2014-02-10 15:50:42 -06:00
|
|
|
assert!(self.llbuilder.is_not_null());
|
2013-07-21 08:33:40 -05:00
|
|
|
self.count_insn("store");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildStore(self.llbuilder, val, ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-28 08:01:53 -06:00
|
|
|
pub fn volatile_store(&self, val: ValueRef, ptr: ValueRef) {
|
|
|
|
debug!("Store {} -> {}",
|
2014-09-05 11:18:53 -05:00
|
|
|
self.ccx.tn().val_to_string(val),
|
|
|
|
self.ccx.tn().val_to_string(ptr));
|
2014-02-10 15:50:42 -06:00
|
|
|
assert!(self.llbuilder.is_not_null());
|
2013-12-28 08:01:53 -06:00
|
|
|
self.count_insn("store.volatile");
|
|
|
|
unsafe {
|
|
|
|
let insn = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
|
2014-07-07 19:58:01 -05:00
|
|
|
llvm::LLVMSetVolatile(insn, llvm::True);
|
2013-12-28 08:01:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-21 08:33:40 -05:00
|
|
|
pub fn atomic_store(&self, val: ValueRef, ptr: ValueRef, order: AtomicOrdering) {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Store {} -> {}",
|
2014-09-05 11:18:53 -05:00
|
|
|
self.ccx.tn().val_to_string(val),
|
|
|
|
self.ccx.tn().val_to_string(ptr));
|
2013-07-21 08:33:40 -05:00
|
|
|
self.count_insn("store.atomic");
|
|
|
|
unsafe {
|
2014-01-15 17:32:44 -06:00
|
|
|
let ty = Type::from_ref(llvm::LLVMTypeOf(ptr));
|
|
|
|
let align = llalign_of_pref(self.ccx, ty.element_type());
|
2013-07-21 08:33:40 -05:00
|
|
|
llvm::LLVMBuildAtomicStore(self.llbuilder, val, ptr, order, align as c_uint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef {
|
|
|
|
self.count_insn("gep");
|
|
|
|
unsafe {
|
2013-12-15 06:35:12 -06:00
|
|
|
llvm::LLVMBuildGEP(self.llbuilder, ptr, indices.as_ptr(),
|
2013-07-21 08:33:40 -05:00
|
|
|
indices.len() as c_uint, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple wrapper around GEP that takes an array of ints and wraps them
|
|
|
|
// in C_i32()
|
|
|
|
#[inline]
|
|
|
|
pub fn gepi(&self, base: ValueRef, ixs: &[uint]) -> ValueRef {
|
|
|
|
// Small vector optimization. This should catch 100% of the cases that
|
|
|
|
// we care about.
|
|
|
|
if ixs.len() < 16 {
|
2014-03-15 15:29:34 -05:00
|
|
|
let mut small_vec = [ C_i32(self.ccx, 0), ..16 ];
|
2014-09-14 22:27:36 -05:00
|
|
|
for (small_vec_e, &ix) in small_vec.iter_mut().zip(ixs.iter()) {
|
2014-03-15 15:29:34 -05:00
|
|
|
*small_vec_e = C_i32(self.ccx, ix as i32);
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
2014-09-24 06:41:09 -05:00
|
|
|
self.inbounds_gep(base, small_vec[..ixs.len()])
|
2013-07-21 08:33:40 -05:00
|
|
|
} else {
|
2014-03-15 15:29:34 -05:00
|
|
|
let v = ixs.iter().map(|i| C_i32(self.ccx, *i as i32)).collect::<Vec<ValueRef>>();
|
2013-07-21 08:33:40 -05:00
|
|
|
self.count_insn("gepi");
|
2014-03-08 14:36:22 -06:00
|
|
|
self.inbounds_gep(base, v.as_slice())
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn inbounds_gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef {
|
|
|
|
self.count_insn("inboundsgep");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildInBoundsGEP(
|
2013-12-15 06:35:12 -06:00
|
|
|
self.llbuilder, ptr, indices.as_ptr(), indices.len() as c_uint, noname())
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn struct_gep(&self, ptr: ValueRef, idx: uint) -> ValueRef {
|
|
|
|
self.count_insn("structgep");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildStructGEP(self.llbuilder, ptr, idx as c_uint, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-25 14:47:34 -05:00
|
|
|
pub fn global_string(&self, _str: *const c_char) -> ValueRef {
|
2013-07-21 08:33:40 -05:00
|
|
|
self.count_insn("globalstring");
|
|
|
|
unsafe {
|
2014-02-15 15:15:03 -06:00
|
|
|
llvm::LLVMBuildGlobalString(self.llbuilder, _str, noname())
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-25 14:47:34 -05:00
|
|
|
pub fn global_string_ptr(&self, _str: *const c_char) -> ValueRef {
|
2013-07-21 08:33:40 -05:00
|
|
|
self.count_insn("globalstringptr");
|
|
|
|
unsafe {
|
2014-02-15 15:15:03 -06:00
|
|
|
llvm::LLVMBuildGlobalStringPtr(self.llbuilder, _str, noname())
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Casts */
|
|
|
|
pub fn trunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("trunc");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn zext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("zext");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("sext");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fptoui(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("fptoui");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fptosi(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("fptosi");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty.to_ref(),noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn uitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("uitofp");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("sitofp");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fptrunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("fptrunc");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fpext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("fpext");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ptrtoint(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("ptrtoint");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn inttoptr(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("inttoptr");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("bitcast");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn zext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("zextorbitcast");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildZExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("sextorbitcast");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildSExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn trunc_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("truncorbitcast");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildTruncOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cast(&self, op: Opcode, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("cast");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildCast(self.llbuilder, op, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pointercast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("pointercast");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn intcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("intcast");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildIntCast(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fpcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("fpcast");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildFPCast(self.llbuilder, val, dest_ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Comparisons */
|
|
|
|
pub fn icmp(&self, op: IntPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("icmp");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fcmp(&self, op: RealPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("fcmp");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Miscellaneous instructions */
|
|
|
|
pub fn empty_phi(&self, ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("emptyphi");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildPhi(self.llbuilder, ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn phi(&self, ty: Type, vals: &[ValueRef], bbs: &[BasicBlockRef]) -> ValueRef {
|
|
|
|
assert_eq!(vals.len(), bbs.len());
|
|
|
|
let phi = self.empty_phi(ty);
|
|
|
|
self.count_insn("addincoming");
|
|
|
|
unsafe {
|
2013-12-15 06:35:12 -06:00
|
|
|
llvm::LLVMAddIncoming(phi, vals.as_ptr(),
|
|
|
|
bbs.as_ptr(),
|
2013-07-21 08:33:40 -05:00
|
|
|
vals.len() as c_uint);
|
|
|
|
phi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-31 11:13:04 -05:00
|
|
|
pub fn add_span_comment(&self, sp: Span, text: &str) {
|
2014-03-05 08:36:01 -06:00
|
|
|
if self.ccx.sess().asm_comments() {
|
2014-05-16 12:45:16 -05:00
|
|
|
let s = format!("{} ({})",
|
|
|
|
text,
|
2014-06-21 05:39:03 -05:00
|
|
|
self.ccx.sess().codemap().span_to_string(sp));
|
2014-05-16 12:45:16 -05:00
|
|
|
debug!("{}", s.as_slice());
|
|
|
|
self.add_comment(s.as_slice());
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_comment(&self, text: &str) {
|
2014-03-05 08:36:01 -06:00
|
|
|
if self.ccx.sess().asm_comments() {
|
2013-07-21 08:33:40 -05:00
|
|
|
let sanitized = text.replace("$", "");
|
2014-05-28 11:24:28 -05:00
|
|
|
let comment_text = format!("{} {}", "#",
|
|
|
|
sanitized.replace("\n", "\n\t# "));
|
2013-07-21 08:33:40 -05:00
|
|
|
self.count_insn("inlineasm");
|
2014-11-27 13:10:25 -06:00
|
|
|
let asm = comment_text.with_c_str(|c| {
|
2013-07-21 08:33:40 -05:00
|
|
|
unsafe {
|
2014-11-17 02:39:01 -06:00
|
|
|
llvm::LLVMConstInlineAsm(Type::func(&[], &Type::void(self.ccx)).to_ref(),
|
2013-07-21 08:33:40 -05:00
|
|
|
c, noname(), False, False)
|
|
|
|
}
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2014-11-17 02:39:01 -06:00
|
|
|
self.call(asm, &[], None);
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-25 14:47:34 -05:00
|
|
|
pub fn inline_asm_call(&self, asm: *const c_char, cons: *const c_char,
|
2013-07-21 08:33:40 -05:00
|
|
|
inputs: &[ValueRef], output: Type,
|
|
|
|
volatile: bool, alignstack: bool,
|
|
|
|
dia: AsmDialect) -> ValueRef {
|
|
|
|
self.count_insn("inlineasm");
|
|
|
|
|
2014-07-07 19:58:01 -05:00
|
|
|
let volatile = if volatile { llvm::True }
|
|
|
|
else { llvm::False };
|
|
|
|
let alignstack = if alignstack { llvm::True }
|
|
|
|
else { llvm::False };
|
2013-07-21 08:33:40 -05:00
|
|
|
|
2014-03-28 14:42:34 -05:00
|
|
|
let argtys = inputs.iter().map(|v| {
|
2014-10-15 01:25:34 -05:00
|
|
|
debug!("Asm Input Type: {}", self.ccx.tn().val_to_string(*v));
|
2013-07-21 08:33:40 -05:00
|
|
|
val_ty(*v)
|
2014-03-28 14:42:34 -05:00
|
|
|
}).collect::<Vec<_>>();
|
2013-07-21 08:33:40 -05:00
|
|
|
|
2014-10-15 01:25:34 -05:00
|
|
|
debug!("Asm Output Type: {}", self.ccx.tn().type_to_string(output));
|
2014-03-28 14:42:34 -05:00
|
|
|
let fty = Type::func(argtys.as_slice(), &output);
|
2013-07-21 08:33:40 -05:00
|
|
|
unsafe {
|
|
|
|
let v = llvm::LLVMInlineAsm(
|
|
|
|
fty.to_ref(), asm, cons, volatile, alignstack, dia as c_uint);
|
2014-07-25 18:06:44 -05:00
|
|
|
self.call(v, inputs, None)
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-12 23:14:17 -05:00
|
|
|
pub fn call(&self, llfn: ValueRef, args: &[ValueRef],
|
2014-07-25 18:06:44 -05:00
|
|
|
attributes: Option<AttrBuilder>) -> ValueRef {
|
2013-07-21 08:33:40 -05:00
|
|
|
self.count_insn("call");
|
2014-01-27 06:18:36 -06:00
|
|
|
|
|
|
|
debug!("Call {} with args ({})",
|
2014-09-05 11:18:53 -05:00
|
|
|
self.ccx.tn().val_to_string(llfn),
|
2014-03-28 14:42:34 -05:00
|
|
|
args.iter()
|
2014-09-05 11:18:53 -05:00
|
|
|
.map(|&v| self.ccx.tn().val_to_string(v))
|
2014-05-22 18:57:53 -05:00
|
|
|
.collect::<Vec<String>>()
|
2014-03-28 14:42:34 -05:00
|
|
|
.connect(", "));
|
2014-01-27 06:18:36 -06:00
|
|
|
|
2013-07-21 08:33:40 -05:00
|
|
|
unsafe {
|
2013-12-15 06:35:12 -06:00
|
|
|
let v = llvm::LLVMBuildCall(self.llbuilder, llfn, args.as_ptr(),
|
2013-07-21 08:33:40 -05:00
|
|
|
args.len() as c_uint, noname());
|
2014-07-25 18:06:44 -05:00
|
|
|
match attributes {
|
|
|
|
Some(a) => a.apply_callsite(v),
|
|
|
|
None => {}
|
2013-09-12 22:23:44 -05:00
|
|
|
}
|
2013-07-21 08:33:40 -05:00
|
|
|
v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-12 23:14:17 -05:00
|
|
|
pub fn call_with_conv(&self, llfn: ValueRef, args: &[ValueRef],
|
2014-07-25 18:06:44 -05:00
|
|
|
conv: CallConv, attributes: Option<AttrBuilder>) -> ValueRef {
|
2013-09-12 23:14:17 -05:00
|
|
|
self.count_insn("callwithconv");
|
|
|
|
let v = self.call(llfn, args, attributes);
|
2014-07-07 19:58:01 -05:00
|
|
|
llvm::SetInstructionCallConv(v, conv);
|
2013-09-12 23:14:17 -05:00
|
|
|
v
|
|
|
|
}
|
|
|
|
|
2013-07-21 08:33:40 -05:00
|
|
|
pub fn select(&self, cond: ValueRef, then_val: ValueRef, else_val: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("select");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn va_arg(&self, list: ValueRef, ty: Type) -> ValueRef {
|
|
|
|
self.count_insn("vaarg");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildVAArg(self.llbuilder, list, ty.to_ref(), noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn extract_element(&self, vec: ValueRef, idx: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("extractelement");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert_element(&self, vec: ValueRef, elt: ValueRef, idx: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("insertelement");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shuffle_vector(&self, v1: ValueRef, v2: ValueRef, mask: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("shufflevector");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn vector_splat(&self, num_elts: uint, elt: ValueRef) -> ValueRef {
|
|
|
|
unsafe {
|
|
|
|
let elt_ty = val_ty(elt);
|
2014-02-15 15:15:03 -06:00
|
|
|
let undef = llvm::LLVMGetUndef(Type::vector(&elt_ty, num_elts as u64).to_ref());
|
2014-03-15 15:29:34 -05:00
|
|
|
let vec = self.insert_element(undef, elt, C_i32(self.ccx, 0));
|
|
|
|
let vec_i32_ty = Type::vector(&Type::i32(self.ccx), num_elts as u64);
|
|
|
|
self.shuffle_vector(vec, undef, C_null(vec_i32_ty))
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn extract_value(&self, agg_val: ValueRef, idx: uint) -> ValueRef {
|
|
|
|
self.count_insn("extractvalue");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert_value(&self, agg_val: ValueRef, elt: ValueRef,
|
2013-08-07 14:40:09 -05:00
|
|
|
idx: uint) -> ValueRef {
|
2013-07-21 08:33:40 -05:00
|
|
|
self.count_insn("insertvalue");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint,
|
2013-08-07 14:40:09 -05:00
|
|
|
noname())
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_null(&self, val: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("isnull");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildIsNull(self.llbuilder, val, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_not_null(&self, val: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("isnotnull");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildIsNotNull(self.llbuilder, val, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ptrdiff(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("ptrdiff");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildPtrDiff(self.llbuilder, lhs, rhs, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn trap(&self) {
|
|
|
|
unsafe {
|
2014-02-15 15:15:03 -06:00
|
|
|
let bb: BasicBlockRef = llvm::LLVMGetInsertBlock(self.llbuilder);
|
|
|
|
let fn_: ValueRef = llvm::LLVMGetBasicBlockParent(bb);
|
|
|
|
let m: ModuleRef = llvm::LLVMGetGlobalParent(fn_);
|
|
|
|
let t: ValueRef = "llvm.trap".with_c_str(|buf| {
|
|
|
|
llvm::LLVMGetNamedFunction(m, buf)
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2014-02-15 15:15:03 -06:00
|
|
|
assert!((t as int != 0));
|
2014-11-17 02:39:01 -06:00
|
|
|
let args: &[ValueRef] = &[];
|
2013-07-21 08:33:40 -05:00
|
|
|
self.count_insn("trap");
|
|
|
|
llvm::LLVMBuildCall(
|
2014-02-15 15:15:03 -06:00
|
|
|
self.llbuilder, t, args.as_ptr(), args.len() as c_uint, noname());
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn landing_pad(&self, ty: Type, pers_fn: ValueRef, num_clauses: uint) -> ValueRef {
|
|
|
|
self.count_insn("landingpad");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildLandingPad(
|
|
|
|
self.llbuilder, ty.to_ref(), pers_fn, num_clauses as c_uint, noname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_cleanup(&self, landing_pad: ValueRef) {
|
|
|
|
self.count_insn("setcleanup");
|
|
|
|
unsafe {
|
2014-07-07 19:58:01 -05:00
|
|
|
llvm::LLVMSetCleanup(landing_pad, llvm::True);
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resume(&self, exn: ValueRef) -> ValueRef {
|
|
|
|
self.count_insn("resume");
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildResume(self.llbuilder, exn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Atomic Operations
|
|
|
|
pub fn atomic_cmpxchg(&self, dst: ValueRef,
|
|
|
|
cmp: ValueRef, src: ValueRef,
|
2014-03-31 16:43:19 -05:00
|
|
|
order: AtomicOrdering,
|
|
|
|
failure_order: AtomicOrdering) -> ValueRef {
|
2013-07-21 08:33:40 -05:00
|
|
|
unsafe {
|
2014-03-31 16:43:19 -05:00
|
|
|
llvm::LLVMBuildAtomicCmpXchg(self.llbuilder, dst, cmp, src,
|
|
|
|
order, failure_order)
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn atomic_rmw(&self, op: AtomicBinOp,
|
|
|
|
dst: ValueRef, src: ValueRef,
|
|
|
|
order: AtomicOrdering) -> ValueRef {
|
|
|
|
unsafe {
|
2013-05-25 17:23:12 -05:00
|
|
|
llvm::LLVMBuildAtomicRMW(self.llbuilder, op, dst, src, order, False)
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
2013-07-28 02:48:16 -05:00
|
|
|
|
|
|
|
pub fn atomic_fence(&self, order: AtomicOrdering) {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMBuildAtomicFence(self.llbuilder, order);
|
|
|
|
}
|
|
|
|
}
|
2013-07-21 08:33:40 -05:00
|
|
|
}
|