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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::num::Wrapping;
use std::u64;
use quickcheck::{Arbitrary,Gen};
use {NameRef,Result,SegmentRef};
#[derive(Clone,Copy,PartialEq,Eq,PartialOrd,Ord,Debug,Hash)]
pub struct Variable {
pub name: NameRef,
pub bits: usize,
}
impl Variable {
pub fn new(name: NameRef, bits: usize) -> Result<Variable> {
if bits == 0 { return Err("Variable can't have size 0".into()); }
Ok(Variable{
name: name,
bits: bits
})
}
}
#[derive(Clone,Copy,PartialEq,Eq,PartialOrd,Ord,Debug,Hash)]
pub struct Constant {
pub value: u64,
pub bits: usize,
}
impl Constant {
pub fn new(value: u64, bits: usize) -> Result<Constant> {
if bits == 0 { return Err("Variable can't have size 0".into()); }
Ok(Constant{
value: value,
bits: bits
})
}
pub fn mask(&self) -> Wrapping<u64> {
Wrapping(if self.bits < 64 { (1u64 << self.bits) - 1 } else { u64::MAX })
}
}
impl Into<Wrapping<u64>> for Constant {
fn into(self) -> Wrapping<u64> {
Wrapping(self.value) & self.mask()
}
}
#[derive(Clone,Copy,PartialEq,Eq,PartialOrd,Ord,Debug,Hash)]
pub enum Value {
Undefined,
Variable(Variable),
Constant(Constant),
}
impl Value {
pub fn val(val: u64, bits: usize) -> Result<Value> {
Ok(Value::Constant(Constant::new(val,bits)?))
}
pub fn var(name: NameRef, bits: usize) -> Result<Value> {
Ok(Value::Variable(Variable::new(name,bits)?))
}
pub fn undef() -> Value {
Value::Undefined
}
pub fn bits(&self) -> Option<usize> {
match self {
&Value::Variable(Variable{ bits,.. }) => Some(bits),
&Value::Constant(Constant{ bits,.. }) => Some(bits),
&Value::Undefined => None,
}
}
}
impl From<Variable> for Value {
fn from(v: Variable) -> Value {
Value::Variable(v)
}
}
impl From<Constant> for Value {
fn from(v: Constant) -> Value {
Value::Constant(v)
}
}
#[derive(Clone,Copy,PartialEq,Eq,Debug,Hash)]
pub struct Segment {
pub name: SegmentRef,
}
impl Arbitrary for Variable {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
Variable {
name: NameRef::arbitrary(g),
bits: 1 << g.gen_range(0, 11),
}
}
}
impl Arbitrary for Constant {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
Constant{
value: g.gen(),
bits: 1 << g.gen_range(0, 11),
}
}
}
impl Arbitrary for Segment {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
Segment{ name: SegmentRef::new(g.gen_range(0,100)) }
}
}
impl Arbitrary for Value {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
match g.gen_range(0, 2) {
0 => Value::Undefined,
1 => Value::Variable(Variable::arbitrary(g)),
2 => Value::Constant(Constant::arbitrary(g)),
_ => unreachable!(),
}
}
}