From 269ec2ca8fbeb3676fdeea772684a0a919d7a500 Mon Sep 17 00:00:00 2001 From: Jiahan Xie Date: Wed, 7 Aug 2024 23:27:01 -0400 Subject: [PATCH] trying to parse ieee754 const --- calyx-frontend/src/parser.rs | 32 ++++++++++++----------- calyx-frontend/src/syntax.pest | 9 ++++--- tests/correctness/float/float-const.futil | 2 +- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/calyx-frontend/src/parser.rs b/calyx-frontend/src/parser.rs index ac4541f334..2a48c2540e 100644 --- a/calyx-frontend/src/parser.rs +++ b/calyx-frontend/src/parser.rs @@ -302,6 +302,22 @@ impl CalyxParser { Ok(float_val) } + fn ieee754_const(input: Node) -> ParseResult { + println!("parsing iee754"); + let span = Self::get_span(&input); + let val = match_nodes!( + input.clone().into_children(); + [float(val)] => val + ); + let bit_pattern = val.to_bits(); + Ok(BitNum { + width: 64, + num_type: NumType::Hex, + val: bit_pattern, + span, + }) + } + fn num_lit(input: Node) -> ParseResult { let span = Self::get_span(&input); let num = match_nodes!( @@ -330,21 +346,7 @@ impl CalyxParser { val, span }, - [bitwidth(width), float(val)] => { - let bit_pattern = if width == 32 { - (val as f32).to_bits() as u64 - } else if width == 64 { - val.to_bits() - } else { - return Err(input.error("Unsupported bitwidth for floating-point number")); - }; - BitNum { - width, - num_type: NumType::Hex, - val: bit_pattern, - span - } - }, + [ieee754_const(val)] => val, ); diff --git a/calyx-frontend/src/syntax.pest b/calyx-frontend/src/syntax.pest index d01f1a136c..6dc5b80f93 100644 --- a/calyx-frontend/src/syntax.pest +++ b/calyx-frontend/src/syntax.pest @@ -18,17 +18,20 @@ octal = @{ ASCII_HEX_DIGIT+ } hex = @{ ASCII_HEX_DIGIT+ } float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ } +ieee754_const = { "ieee754_const(" ~ float ~ ")" } + // `$` creates a compound rule which ignores whitespace while allowing for // inner rules (`@` makes inner rules silent). // See: https://pest.rs/book/print.html#atomic num_lit = ${ - bitwidth + (bitwidth ~ "'" ~ ( "d" ~ decimal | "b" ~ binary | "x" ~ hex - | "o" ~ octal - | "f" ~ float) + | "o" ~ octal) + ) + | ieee754_const } char = { !"\"" ~ ANY } diff --git a/tests/correctness/float/float-const.futil b/tests/correctness/float/float-const.futil index 4bbfc8cdd8..969494e51c 100644 --- a/tests/correctness/float/float-const.futil +++ b/tests/correctness/float/float-const.futil @@ -10,7 +10,7 @@ component main<"toplevel"=1,>(@clk clk: 1, @reset reset: 1, @go go: 1) -> (@done wires { group read { mem_read.addr0 = 1'b0; - reg0.in = mem_read.read_data; + reg0.in = ieee754_const(1.00); reg0.write_en = 1'b1; read[done] = reg0.done; }