use std::fmt;
use il::*;
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct Constant {
value: u64,
bits: usize
}
impl Constant {
pub fn new(value: u64, bits: usize) -> Constant {
Constant { value: Constant::trim_value(value, bits), bits: bits }
}
fn trim_value(value: u64, bits: usize) -> u64 {
if bits == 64 {
value
}
else {
value & ((1 << bits) - 1)
}
}
pub fn value(&self) -> u64 {
self.value
}
pub fn bits(&self) -> usize {
self.bits
}
}
impl fmt::Display for Constant {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "0x{:X}:{}", self.value(), self.bits)
}
}
impl Into<Expression> for Constant {
fn into(self) -> Expression {
Expression::constant(self)
}
}