aboutsummaryrefslogtreecommitdiffstats
path: root/bytecode/src/data.rs
blob: 4774f2a697b005173d590c3a1c141e5f852df8fd (plain) (blame)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::fmt;

// The data representations of values of different types are given below.
// - 0 represents an undefined value. Any operation with 0 will cause an error.
// - Pointers are 64 bit unsigned (positive) ints which are
//   word-aligned, i.e. 0 mod 8. All types not listed below are
//   allocated on the heap behind a pointer.
// - Int:
//     < 63-bit signed int > 1
//   Ints are a 63 bit int with a 1 in the low bit.
// - Booleans and nil are 2 mod 8:
//   - False is 0x2
//   - True is 0xA
//   - Nil is 0x12
// - A symbol's low byte is 0x4, and the high 56 bytes are an unsigned integer constant
//   representing a pointer into the symbol table.
//
// n.b. 6 mod 8 is unused.

#[derive(PartialEq, Eq, Debug, Hash, Clone, Copy)]
pub struct Pointer(pub usize);

impl Pointer {
    pub fn offset(self, i: usize) -> Self {
        let Pointer(u) = self;
        Pointer(u + i)
    }
}

impl fmt::LowerHex for Pointer {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let val = self.0;

        fmt::LowerHex::fmt(&val, f)
    }
}

#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub struct Value(pub u64);

impl Value {
    pub fn is_pointer(self) -> bool {
        let Value(stack_representation) = self;
        return stack_representation != 0 && stack_representation & 0x7 == 0;
    }

    pub fn from_pointer(p: Pointer) -> Self {
        let Pointer(x) = p;
        return Value(u64::try_from(x).unwrap());
    }

    pub fn to_pointer(self) -> Result<Pointer, String> {
        let Value(stack_representation) = self;
        if !self.is_pointer() {
            return Err(format!(
                "value 0x{:x} is not a pointer",
                stack_representation
            ));
        }
        return Ok(Pointer(usize::try_from(stack_representation).unwrap()));
    }

    pub fn is_int(self) -> bool {
        let Value(stack_representation) = self;
        return stack_representation & 0x1 == 1;
    }

    pub fn from_int(i: i64) -> Self {
        return Value((i as u64) << 1 | 1);
    }

    pub fn to_int(self) -> Result<i64, String> {
        let Value(stack_representation) = self;
        if !self.is_int() {
            return Err(format!("value 0x{:x} is not an int", stack_representation));
        }
        return Ok(stack_representation as i64 >> 1);
    }

    pub fn from_bool(b: bool) -> Self {
        if b {
            return Value(0xa);
        }
        return Value(0x2);
    }

    pub const NIL: Self = Value(0x12);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn is_pointer() {
        assert_eq!(true, Value(0xf8).is_pointer());
    }

    #[test]
    fn is_not_pointer() {
        assert_eq!(false, Value(1).is_pointer());
    }

    #[test]
    fn from_pointer() {
        assert_eq!(Value(0xf8), Value::from_pointer(Pointer(0xf8)));
    }

    #[test]
    fn to_pointer() {
        assert_eq!(Ok(Pointer(0xf8)), Value(0xf8).to_pointer());
    }

    #[test]
    fn is_int() {
        assert_eq!(true, Value(1).is_int());
    }

    #[test]
    fn is_not_int() {
        assert_eq!(false, Value(0).is_int());
    }

    #[test]
    fn from_int() {
        assert_eq!(Value(0xb), Value::from_int(5));
    }

    #[test]
    fn to_int() {
        assert_eq!(Ok(5), Value(0xb).to_int());
    }

    #[test]
    fn true_from_bool() {
        assert_eq!(Value(0xa), Value::from_bool(true));
    }

    #[test]
    fn false_from_bool() {
        assert_eq!(Value(2), Value::from_bool(false));
    }
}