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.data b/tests/correctness/float/float-const.data index 5a00caae4f..60689ad11b 100644 --- a/tests/correctness/float/float-const.data +++ b/tests/correctness/float/float-const.data @@ -1,14 +1,4 @@ { - "mem_read": { - "data": [ - 4.2 - ], - "format": { - "is_signed": true, - "numeric_type": "floating_point", - "width": 32 - } - }, "mem_write": { "data": [ 0.0 @@ -19,4 +9,4 @@ "width": 32 } } -} \ No newline at end of file +} diff --git a/tests/correctness/float/float-const.futil b/tests/correctness/float/float-const.futil index 4bbfc8cdd8..93f4908cdb 100644 --- a/tests/correctness/float/float-const.futil +++ b/tests/correctness/float/float-const.futil @@ -4,13 +4,11 @@ import "primitives/memories/comb.futil"; component main<"toplevel"=1,>(@clk clk: 1, @reset reset: 1, @go go: 1) -> (@done done: 1) { cells { reg0 = std_reg(32); - @external mem_read = comb_mem_d1(32, 1, 1); @external mem_write = comb_mem_d1(32, 1, 1); } wires { group read { - mem_read.addr0 = 1'b0; - reg0.in = mem_read.read_data; + reg0.in = ieee754_const(32'f4.2); reg0.write_en = 1'b1; read[done] = reg0.done; } @@ -28,4 +26,4 @@ component main<"toplevel"=1,>(@clk clk: 1, @reset reset: 1, @go go: 1) -> (@done write; } } -} \ No newline at end of file +}