use raw::*;
use function::UncompiledFunction;
use types::*;
use util::from_ptr;
use std::marker::PhantomData;
use std::fmt;
use std::ops::*;
pub struct Val(PhantomData<[()]>);
native_ref!(&Val = jit_value_t);
impl fmt::Debug for Val {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "v({:?})", self.get_type())
}
}
impl Val {
#[inline(always)]
pub fn new<'a>(func:&UncompiledFunction<'a>, value_type:&Ty) -> &'a Val {
unsafe {
from_ptr(jit_value_create(func.into(), value_type.into()))
}
}
pub fn get_type(&self) -> &Ty {
unsafe {
from_ptr(jit_value_get_type(self.into()))
}
}
pub fn get_function(&self) -> UncompiledFunction {
unsafe {
from_ptr(jit_value_get_function(self.into()))
}
}
#[inline]
pub fn is_temp(&self) -> bool {
unsafe {
jit_value_is_temporary(self.into()) != 0
}
}
#[inline]
pub fn is_addressable(&self) -> bool {
unsafe {
jit_value_is_addressable(self.into()) != 0
}
}
#[inline]
pub fn set_addressable(&self) -> () {
unsafe {
jit_value_set_addressable(self.into())
}
}
}
macro_rules! bin_op {
($trait_ty:ident, $trait_func:ident, $func:ident) => (
impl<'a> $trait_ty<&'a Val> for &'a Val {
type Output = &'a Val;
fn $trait_func(self, other: &'a Val) -> &'a Val {
self.get_function().$func(self, other)
}
}
)
}
macro_rules! un_op {
($trait_ty:ident, $trait_func:ident, $func:ident) => (
impl<'a> $trait_ty for &'a Val {
type Output = &'a Val;
fn $trait_func(self) -> &'a Val {
self.get_function().$func(self)
}
}
)
}
bin_op!{Add, add, insn_add}
bin_op!{BitAnd, bitand, insn_and}
bin_op!{BitOr, bitor, insn_or}
bin_op!{BitXor, bitxor, insn_xor}
bin_op!{Div, div, insn_div}
bin_op!{Mul, mul, insn_mul}
bin_op!{Rem, rem, insn_rem}
bin_op!{Shl, shl, insn_shl}
bin_op!{Shr, shr, insn_shr}
bin_op!{Sub, sub, insn_sub}
un_op!{Neg, neg, insn_neg}
un_op!{Not, not, insn_not}