Skip to content

Commit f290f4d

Browse files
authored
record bitwidth in ConstValue::Int (#118)
fixes #117
1 parent d1a99a2 commit f290f4d

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/builder/tail_loop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ mod test {
9999
"main",
100100
Signature::new_df(type_row![BIT], type_row![NAT, BIT]),
101101
)?;
102-
let s1 = module_builder.constant(ConstValue::Int(1))?;
102+
let s1 = module_builder.constant(ConstValue::i64(1))?;
103103
let _fdef = {
104104
let mut fbuild = module_builder.define_function(&main)?;
105105
let [i1] = fbuild.input_wires_arr();
@@ -130,7 +130,7 @@ mod test {
130130
let main = module_builder
131131
.declare("main", Signature::new_df(type_row![BIT], type_row![NAT]))?;
132132

133-
let s2 = module_builder.constant(ConstValue::Int(2))?;
133+
let s2 = module_builder.constant(ConstValue::i64(2))?;
134134
let tru_const = module_builder.constant(ConstValue::true_val())?;
135135

136136
let _fdef = {

src/ops/module.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl ModuleOp {
115115
#[allow(missing_docs)]
116116
pub enum ConstValue {
117117
/// An arbitrary length integer constant.
118-
Int(i64),
118+
Int { value: i64, width: usize },
119119
/// A constant specifying a variant of a Sum type.
120120
Sum {
121121
tag: usize,
@@ -131,7 +131,16 @@ pub enum ConstValue {
131131
impl PartialEq for ConstValue {
132132
fn eq(&self, other: &Self) -> bool {
133133
match (self, other) {
134-
(Self::Int(l0), Self::Int(r0)) => l0 == r0,
134+
(
135+
Self::Int {
136+
value: l0,
137+
width: l_width,
138+
},
139+
Self::Int {
140+
value: r0,
141+
width: r_width,
142+
},
143+
) => l0 == r0 && l_width == r_width,
135144
(Self::Opaque(l0, l1), Self::Opaque(r0, r1)) => l0 == r0 && l1.eq(&**r1),
136145
(
137146
Self::Sum { tag, variants, val },
@@ -152,15 +161,18 @@ impl Eq for ConstValue {}
152161

153162
impl Default for ConstValue {
154163
fn default() -> Self {
155-
Self::Int(0)
164+
Self::Int {
165+
value: 0,
166+
width: 64,
167+
}
156168
}
157169
}
158170

159171
impl ConstValue {
160172
/// Returns the datatype of the constant.
161173
pub fn const_type(&self) -> ClassicType {
162174
match self {
163-
Self::Int(_) => ClassicType::i64(),
175+
Self::Int { value: _, width } => ClassicType::Int(*width),
164176
Self::Opaque(_, b) => (*b).const_type(),
165177
Self::Sum { variants, .. } => {
166178
ClassicType::Container(Container::Sum(Box::new(variants.clone())))
@@ -178,7 +190,7 @@ impl ConstValue {
178190
/// Unique name of the constant.
179191
pub fn name(&self) -> SmolStr {
180192
match self {
181-
Self::Int(v) => format!("const:int:{v}"),
193+
Self::Int { value, width } => format!("const:int<{width}>:{value}"),
182194
Self::Opaque(_, v) => format!("const:{}", v.name()),
183195
Self::Sum { tag, val, .. } => {
184196
format!("const:sum:{{tag:{tag}, val:{}}}", val.name())
@@ -235,6 +247,11 @@ impl ConstValue {
235247
pub fn simple_unary_predicate() -> Self {
236248
Self::simple_predicate(0, 1)
237249
}
250+
251+
/// New 64 bit integer constant
252+
pub fn i64(value: i64) -> Self {
253+
Self::Int { value, width: 64 }
254+
}
238255
}
239256

240257
impl<T: CustomConst> From<T> for ConstValue {

0 commit comments

Comments
 (0)