Skip to content

Commit

Permalink
Lean: Fix bitvector printing
Browse files Browse the repository at this point in the history
  • Loading branch information
Alasdair committed Feb 17, 2025
1 parent be5a801 commit 73678dc
Show file tree
Hide file tree
Showing 27 changed files with 31 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/string.sail
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ val hex_str = pure "hex_str" : int -> string
val hex_str_upper = pure "hex_str_upper" : int -> string

val bits_str = pure {
lean: "BitVec.string_of_bits",
lean: "BitVec.toFormatted",
_: "string_of_bits"} : forall 'n. bitvector('n, dec) -> string

val concat_str_bits : forall 'n. (string, bitvector('n, dec)) -> string
Expand Down
9 changes: 6 additions & 3 deletions src/sail_lean_backend/Sail/Sail.lean
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ def append' (x : BitVec n) (y : BitVec m) {mn}
(hmn : mn = n + m := by (conv => rhs; dsimp); try rfl) : BitVec mn :=
hmn ▸ x.append y

def string_of_bits {w : Nat} (x : BitVec w) : String :=
def toBin {w : Nat} (x : BitVec w) : String :=
List.asString (List.map (fun c => if c then '1' else '0') (List.ofFn (BitVec.getMsb' x)))

def toFormatted {w : Nat} (x : BitVec w) : String :=
if (length x % 4) == 0 then
s!"0x{String.toUpper (BitVec.toHex x)}"
else
s!"0b{toString x}"
s!"0b{BitVec.toBin x}"

instance : Coe (BitVec (1 * n)) (BitVec n) where
coe x := x.cast (by simp)
Expand Down Expand Up @@ -309,7 +312,7 @@ def print_int_effect (str : String) (n : Int) : PreSailM RegisterType c ue Unit
print_effect s!"{str}{n}\n"

def print_bits_effect {w : Nat} (str : String) (x : BitVec w) : PreSailM RegisterType c ue Unit :=
print_effect s!"{str}{BitVec.string_of_bits x}\n"
print_effect s!"{str}{BitVec.toFormatted x}\n"

def print_endline_effect (str : String) : PreSailM RegisterType c ue Unit :=
print_effect s!"{str}\n"
Expand Down
2 changes: 1 addition & 1 deletion test/lean/SailTinyArm.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/append.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/bitfield.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/bitvec_operation.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/enum.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/errors.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/extern.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/extern_bitvec.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/implicit.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/ite.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/let.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/loop.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/mapping.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/match.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/match_bv.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/option.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/range.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/register_vector.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/registers.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/riscv_duopod.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
4 changes: 0 additions & 4 deletions test/lean/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@
'pow2_var',
'large_bitvector',
'exn_hello_world',
'struct',
'poly_union_rev',
'match_bind',
'rev_bits_in_byte',
'foreach_none',
'all_even_vector_length',
'outcome_impl',
'lib_valid_hex_bits',
'string_of_bits2',
'tuple_conversion',
'pow2',
'primop',
Expand All @@ -63,7 +61,6 @@
'rv_format2',
'try_return',
'bitvector_update2',
'poly_mapping2',
'real',
'inc_tests',
'poly_outcome',
Expand Down Expand Up @@ -98,7 +95,6 @@
'nexp_simp_euclidian',
'config_register_ones',
'toplevel_tyvar',
'pattern_concat_nest',
'ediv',
'ediv_from_tdiv',
'int_struct',
Expand Down
2 changes: 1 addition & 1 deletion test/lean/struct.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/struct_of_enum.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/typedef.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down
2 changes: 1 addition & 1 deletion test/lean/typquant.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def is_some (opt : (Option k_a)) : Bool :=

/-- Type quantifiers: k_n : Int -/
def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
(HAppend.hAppend str (BitVec.string_of_bits x))
(HAppend.hAppend str (BitVec.toFormatted x))

/-- Type quantifiers: x : Int -/
def concat_str_dec (str : String) (x : Int) : String :=
Expand Down

0 comments on commit 73678dc

Please sign in to comment.