Skip to content

Commit

Permalink
trying to parse ieee754 const
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahanxie353 committed Aug 12, 2024
1 parent e938308 commit 206ea71
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
32 changes: 17 additions & 15 deletions calyx-frontend/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,22 @@ impl CalyxParser {
Ok(float_val)
}

fn ieee754_const(input: Node) -> ParseResult<BitNum> {
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<BitNum> {
let span = Self::get_span(&input);
let num = match_nodes!(
Expand Down Expand Up @@ -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,

);

Expand Down
9 changes: 6 additions & 3 deletions calyx-frontend/src/syntax.pest
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
12 changes: 1 addition & 11 deletions tests/correctness/float/float-const.data
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
{
"mem_read": {
"data": [
4.2
],
"format": {
"is_signed": true,
"numeric_type": "floating_point",
"width": 32
}
},
"mem_write": {
"data": [
0.0
Expand All @@ -19,4 +9,4 @@
"width": 32
}
}
}
}
6 changes: 2 additions & 4 deletions tests/correctness/float/float-const.futil
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -28,4 +26,4 @@ component main<"toplevel"=1,>(@clk clk: 1, @reset reset: 1, @go go: 1) -> (@done
write;
}
}
}
}

0 comments on commit 206ea71

Please sign in to comment.