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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std;
use num::{BigInt, BigRational};
use std::sync::Arc;
use value::*;
use ty::*;
pub type Const = Arc<ConstKind>;
impl Into<ValueRef> for Const {
fn into(self) -> ValueRef {
ValueRef::Const(self)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConstKind {
Int(ConstInt),
Time(ConstTime),
}
impl Value for ConstKind {
fn id(&self) -> ValueId {
INLINE_VALUE_ID
}
fn ty(&self) -> Type {
match *self {
ConstKind::Int(ref k) => int_ty(k.width()),
ConstKind::Time(_) => time_ty(),
}
}
fn name(&self) -> Option<&str> {
None
}
}
impl ConstKind {
fn desc(&self) -> &'static str {
match *self {
ConstKind::Int(_) => "ConstKind::Int",
ConstKind::Time(_) => "ConstKind::Time",
}
}
pub fn as_int(&self) -> &ConstInt {
match *self {
ConstKind::Int(ref k) => k,
_ => panic!("as_int called on {}", self.desc())
}
}
pub fn as_time(&self) -> &ConstTime {
match *self {
ConstKind::Time(ref k) => k,
_ => panic!("as_time called on {}", self.desc())
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConstInt {
width: usize,
value: BigInt,
}
impl ConstInt {
pub fn new(width: usize, value: BigInt) -> ConstInt {
ConstInt {
width: width,
value: value,
}
}
pub fn width(&self) -> usize {
self.width
}
pub fn value(&self) -> &BigInt {
&self.value
}
}
#[derive(Clone, PartialOrd, Ord, PartialEq, Eq)]
pub struct ConstTime {
time: BigRational,
delta: BigInt,
epsilon: BigInt,
}
impl ConstTime {
pub fn new(time: BigRational, delta: BigInt, epsilon: BigInt) -> ConstTime {
ConstTime {
time: time,
delta: delta,
epsilon: epsilon,
}
}
pub fn time(&self) -> &BigRational {
&self.time
}
pub fn delta(&self) -> &BigInt {
&self.delta
}
pub fn epsilon(&self) -> &BigInt {
&self.epsilon
}
pub fn is_zero(&self) -> bool {
use num::Zero;
self.time.is_zero() && self.delta.is_zero() && self.epsilon.is_zero()
}
}
impl std::fmt::Debug for ConstTime {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self)
}
}
impl std::fmt::Display for ConstTime {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
use num::Zero;
let mut any = false;
if !self.time.is_zero() || self.is_zero() {
write!(f, "{}s", self.time)?;
any = true;
}
if !self.delta.is_zero() {
if any { write!(f, " ")? };
write!(f, "{}d", self.delta)?;
any = true;
}
if !self.epsilon.is_zero() {
if any { write!(f, " ")? };
write!(f, "{}e", self.epsilon)?;
}
Ok(())
}
}
pub fn const_int(width: usize, value: BigInt) -> Const {
Const::new(ConstKind::Int(ConstInt::new(width, value)))
}
pub fn const_time(time: BigRational, delta: BigInt, epsilon: BigInt) -> Const {
Const::new(ConstKind::Time(ConstTime::new(time, delta, epsilon)))
}
pub fn const_zero(ty: &Type) -> Const {
use num::Zero;
match **ty {
IntType(sz) => const_int(sz, BigInt::zero()),
TimeType => const_time(BigRational::zero(), BigInt::zero(), BigInt::zero()),
ref x => panic!("no const zero value for type {}", x),
}
}