1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
// AluRE: AluVM runtime environment.
// This is rust implementation of AluVM (arithmetic logic unit virtual machine).
//
// Designed & written in 2021 by
//     Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// This software is licensed under the terms of MIT License.
// You should have received a copy of the MIT License along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

use core::convert::TryFrom;
use core::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr};

use super::{RegVal, Value};

impl Not for RegVal {
    type Output = RegVal;

    #[inline]
    fn not(self) -> Self::Output {
        self.map(Value::not).into()
    }
}

impl Not for Value {
    type Output = Value;

    #[inline]
    fn not(mut self) -> Self::Output {
        for i in 0..self.len {
            self[i] = !self[i];
        }
        self
    }
}

impl BitAnd for Value {
    type Output = Value;

    #[inline]
    fn bitand(self, rhs: Self) -> Self::Output {
        self.to_u1024().bitand(rhs.to_u1024()).into()
    }
}

impl BitOr for Value {
    type Output = Value;

    #[inline]
    fn bitor(self, rhs: Self) -> Self::Output {
        self.to_u1024().bitor(rhs.to_u1024()).into()
    }
}

impl BitXor for Value {
    type Output = Value;

    #[inline]
    fn bitxor(self, rhs: Self) -> Self::Output {
        self.to_u1024().bitxor(rhs.to_u1024()).into()
    }
}

impl Shl for Value {
    type Output = Value;

    #[inline]
    fn shl(self, rhs: Self) -> Self::Output {
        self.to_u1024()
            .shl(
                u16::try_from(rhs).expect("attempt to bitshift left for more than 2^16 bits")
                    as usize,
            )
            .into()
    }
}

impl Shr for Value {
    type Output = Value;

    #[inline]
    fn shr(self, rhs: Self) -> Self::Output {
        self.to_u1024()
            .shr(
                u16::try_from(rhs).expect("attempt to bitshift right for more than 2^16 bits")
                    as usize,
            )
            .into()
    }
}

impl Value {
    pub fn scl(src1: Value, src2: Value) -> Value {
        todo!()
    }

    pub fn scr(src1: Value, src2: Value) -> Value {
        todo!()
    }
}