Skip to content

Commit ba20979

Browse files
committed
Add flag for match bv
1 parent 5e72122 commit ba20979

27 files changed

+240
-129
lines changed

src/sail_lean_backend/sail_plugin_lean.ml

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ let opt_lean_real_numbers : bool ref = ref false
8181

8282
let opt_single_file : bool ref = ref false
8383

84+
(* We keep two flags to use the [If_flag] in the list of rewrites. They should never be equal. *)
85+
let opt_enable_matchbv : bool ref = ref false
86+
let opt_disable_matchbv : bool ref = ref true
87+
8488
let lean_version : string = "lean4:nightly-2025-04-07"
8589
let mathlib_version : string = "nightly-testing-2025-04-07"
8690

@@ -98,6 +102,14 @@ let lean_options =
98102
Arg.Unit (fun () -> opt_single_file := true),
99103
"puts the entire output in a single .lean file"
100104
);
105+
( Flag.create ~prefix:["lean"] "matchbv",
106+
Arg.Unit
107+
(fun () ->
108+
opt_disable_matchbv := false;
109+
opt_enable_matchbv := true
110+
),
111+
"use matchbv in the Lean output"
112+
);
101113
( Flag.create ~prefix:["lean"] "noncomputable",
102114
Arg.Unit (fun () -> opt_lean_noncomputable := true),
103115
"add a 'noncomputable section' at the beginning of the output"
@@ -141,11 +153,12 @@ let lean_rewrites =
141153
("tuple_assignments", []);
142154
("vector_concat_assignments", []);
143155
("simple_assignments", []);
144-
(* ("remove_vector_concat", []); *)
145-
(* ("remove_bitvector_pats", []); *)
156+
("remove_vector_concat", [If_flag opt_disable_matchbv]);
157+
("remove_bitvector_pats", [If_flag opt_disable_matchbv]);
146158
(* ("remove_numeral_pats", []); *)
147159
(* ("pattern_literals", [Literal_arg "lem"]); *)
148-
("fun_guarded_pats", []);
160+
("fun_guarded_pats", [If_flag opt_enable_matchbv]);
161+
("guarded_pats", [If_flag opt_disable_matchbv]);
149162
(* ("register_ref_writes", rewrite_register_ref_writes); *)
150163
("nexp_ids", []);
151164
("split", [String_arg "execute"]);
@@ -164,7 +177,7 @@ let lean_rewrites =
164177
(* We need to do the exhaustiveness check before merging, because it may
165178
introduce new wildcard clauses *)
166179
("recheck_defs", []);
167-
(* ("make_cases_exhaustive", []); *)
180+
("make_cases_exhaustive", [If_flag opt_disable_matchbv]);
168181
(* merge funcls before adding the measure argument so that it doesn't
169182
disappear into an internal pattern match *)
170183
("merge_function_clauses", []);

test/lean/SailTinyArm.expected.lean

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ open CacheOp
508508
open Barrier
509509
open AccessType
510510

511-
/-- Type quantifiers: k_ex5891# : Bool, k_ex5890# : Bool -/
511+
/-- Type quantifiers: k_ex6347# : Bool, k_ex6346# : Bool -/
512512
def neq_bool (x : Bool) (y : Bool) : Bool :=
513513
(Bool.not (BEq.beq x y))
514514

@@ -1936,14 +1936,42 @@ def execute (merge_var : ast) : SailM Unit := do
19361936
| .DataMemoryBarrier arg0 => (execute_DataMemoryBarrier arg0)
19371937
| .CompareAndBranch (t, offset) => (execute_CompareAndBranch t offset)
19381938

1939-
def decode (merge_var : (BitVec 32)) : (Option ast) :=
1940-
match_bv merge_var with
1941-
| [11,111,0,00,opc:2,1,Rm:5,option_v:3,S,10,Rn:5,Rt:5] =>
1942-
(decodeLoadStoreRegister opc Rm option_v S Rn Rt)
1943-
| [sf,10,01010,shift:2,N,Rm:5,imm6:6,Rn:5,Rd:5] => (decodeExclusiveOr sf shift N Rm imm6 Rn Rd)
1944-
| [1101010100,0,00,011,0011,CRm:4,1,01,11111] => (decodeDataMemoryBarrier CRm)
1945-
| [1,011010,0,imm19:19,Rt:5] => (decodeCompareAndBranch imm19 Rt)
1946-
| _ => none
1939+
def decode (v__0 : (BitVec 32)) : (Option ast) :=
1940+
bif (Bool.and (BEq.beq (Sail.BitVec.extractLsb v__0 31 24) (0xF8 : (BitVec 8)))
1941+
(Bool.and (BEq.beq (Sail.BitVec.extractLsb v__0 21 21) (0b1 : (BitVec 1)))
1942+
(BEq.beq (Sail.BitVec.extractLsb v__0 11 10) (0b10 : (BitVec 2)))))
1943+
then
1944+
(let S := (BitVec.access v__0 12)
1945+
let option_v : (BitVec 3) := (Sail.BitVec.extractLsb v__0 15 13)
1946+
let opc : (BitVec 2) := (Sail.BitVec.extractLsb v__0 23 22)
1947+
let Rt : (BitVec 5) := (Sail.BitVec.extractLsb v__0 4 0)
1948+
let Rn : (BitVec 5) := (Sail.BitVec.extractLsb v__0 9 5)
1949+
let Rm : (BitVec 5) := (Sail.BitVec.extractLsb v__0 20 16)
1950+
(decodeLoadStoreRegister opc Rm option_v S Rn Rt))
1951+
else
1952+
(bif (BEq.beq (Sail.BitVec.extractLsb v__0 30 24) (0b1001010 : (BitVec 7)))
1953+
then
1954+
(let sf := (BitVec.access v__0 31)
1955+
let N := (BitVec.access v__0 21)
1956+
let shift : (BitVec 2) := (Sail.BitVec.extractLsb v__0 23 22)
1957+
let imm6 : (BitVec 6) := (Sail.BitVec.extractLsb v__0 15 10)
1958+
let Rn : (BitVec 5) := (Sail.BitVec.extractLsb v__0 9 5)
1959+
let Rm : (BitVec 5) := (Sail.BitVec.extractLsb v__0 20 16)
1960+
let Rd : (BitVec 5) := (Sail.BitVec.extractLsb v__0 4 0)
1961+
(decodeExclusiveOr sf shift N Rm imm6 Rn Rd))
1962+
else
1963+
(bif (Bool.and (BEq.beq (Sail.BitVec.extractLsb v__0 31 12) (0xD5033 : (BitVec 20)))
1964+
(BEq.beq (Sail.BitVec.extractLsb v__0 7 0) (0xBF : (BitVec 8))))
1965+
then
1966+
(let CRm : (BitVec 4) := (Sail.BitVec.extractLsb v__0 11 8)
1967+
(decodeDataMemoryBarrier CRm))
1968+
else
1969+
(bif (BEq.beq (Sail.BitVec.extractLsb v__0 31 24) (0xB4 : (BitVec 8)))
1970+
then
1971+
(let imm19 : (BitVec 19) := (Sail.BitVec.extractLsb v__0 23 5)
1972+
let Rt : (BitVec 5) := (Sail.BitVec.extractLsb v__0 4 0)
1973+
(decodeCompareAndBranch imm19 Rt))
1974+
else none)))
19471975

19481976
def iFetch (addr : (BitVec 64)) : SailM (BitVec 32) := do
19491977
let req : (Mem_read_request 4 64 (BitVec 56) (Option TranslationInfo) arm_acc_type) :=

test/lean/append.expected.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace Out.Functions
4545

4646
open option
4747

48-
/-- Type quantifiers: k_ex688# : Bool, k_ex687# : Bool -/
48+
/-- Type quantifiers: k_ex699# : Bool, k_ex698# : Bool -/
4949
def neq_bool (x : Bool) (y : Bool) : Bool :=
5050
(Bool.not (BEq.beq x y))
5151

test/lean/bitfield.expected.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace Out.Functions
5555
open option
5656
open Register
5757

58-
/-- Type quantifiers: k_ex689# : Bool, k_ex688# : Bool -/
58+
/-- Type quantifiers: k_ex700# : Bool, k_ex699# : Bool -/
5959
def neq_bool (x : Bool) (y : Bool) : Bool :=
6060
(Bool.not (BEq.beq x y))
6161

test/lean/bitvec_operation.expected.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace Out.Functions
4545

4646
open option
4747

48-
/-- Type quantifiers: k_ex807# : Bool, k_ex806# : Bool -/
48+
/-- Type quantifiers: k_ex820# : Bool, k_ex819# : Bool -/
4949
def neq_bool (x : Bool) (y : Bool) : Bool :=
5050
(Bool.not (BEq.beq x y))
5151

test/lean/early_return.expected.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ open option
6565
open Register
6666
open E
6767

68-
/-- Type quantifiers: k_ex2312# : Bool, k_ex2311# : Bool -/
68+
/-- Type quantifiers: k_ex2343# : Bool, k_ex2342# : Bool -/
6969
def neq_bool (x : Bool) (y : Bool) : Bool :=
7070
(Bool.not (BEq.beq x y))
7171

@@ -418,7 +418,7 @@ def match_early_return_loop (x : E) : SailM E := SailME.run do
418418
| C => writeReg r_C A
419419
readReg r_B
420420

421-
/-- Type quantifiers: k_ex2624# : Bool -/
421+
/-- Type quantifiers: k_ex2655# : Bool -/
422422
def ite_early_return (x : Bool) : SailM E := SailME.run do
423423
writeReg r_A (← readReg r_C)
424424
let y ← (( do
@@ -429,7 +429,7 @@ def ite_early_return (x : Bool) : SailM E := SailME.run do
429429
else readReg r_B ) : SailME _ E )
430430
readReg r_B
431431

432-
/-- Type quantifiers: k_ex2626# : Bool -/
432+
/-- Type quantifiers: k_ex2657# : Bool -/
433433
def ite_early_return_inloop (x : Bool) : SailM E := SailME.run do
434434
let loop_i_lower := 0
435435
let loop_i_upper := 10
@@ -448,7 +448,7 @@ def ite_early_return_inloop (x : Bool) : SailM E := SailME.run do
448448
(pure loop_vars)
449449
readReg r_B
450450

451-
/-- Type quantifiers: k_ex2630# : Bool -/
451+
/-- Type quantifiers: k_ex2661# : Bool -/
452452
def ite_early_return_loop (x : Bool) : SailM E := SailME.run do
453453
bif x
454454
then
@@ -468,7 +468,7 @@ def ite_early_return_loop (x : Bool) : SailM E := SailME.run do
468468
def unit_type (x : E) : SailM Unit := do
469469
writeReg r_A x
470470

471-
/-- Type quantifiers: k_ex2634# : Bool -/
471+
/-- Type quantifiers: k_ex2665# : Bool -/
472472
def ite_early_return_seq (x : Bool) : SailM E := SailME.run do
473473
writeReg r_A (← readReg r_C)
474474
let y ← (( do

test/lean/enum.expected.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace Out.Functions
4949
open option
5050
open E
5151

52-
/-- Type quantifiers: k_ex706# : Bool, k_ex705# : Bool -/
52+
/-- Type quantifiers: k_ex717# : Bool, k_ex716# : Bool -/
5353
def neq_bool (x : Bool) (y : Bool) : Bool :=
5454
(Bool.not (BEq.beq x y))
5555

test/lean/errors.expected.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace Out.Functions
5353
open option
5454
open Register
5555

56-
/-- Type quantifiers: k_ex716# : Bool, k_ex715# : Bool -/
56+
/-- Type quantifiers: k_ex731# : Bool, k_ex730# : Bool -/
5757
def neq_bool (x : Bool) (y : Bool) : Bool :=
5858
(Bool.not (BEq.beq x y))
5959

@@ -124,13 +124,13 @@ def concat_str_bits (str : String) (x : (BitVec k_n)) : String :=
124124
def concat_str_dec (str : String) (x : Int) : String :=
125125
(HAppend.hAppend str (Int.repr x))
126126

127-
/-- Type quantifiers: k_ex809# : Bool -/
127+
/-- Type quantifiers: k_ex824# : Bool -/
128128
def test_exit (b : Bool) : SailM Unit := do
129129
bif b
130130
then throw Error.Exit
131131
else (pure ())
132132

133-
/-- Type quantifiers: k_ex811# : Bool -/
133+
/-- Type quantifiers: k_ex826# : Bool -/
134134
def test_assert (b : Bool) : SailM (BitVec 1) := do
135135
assert b "b is false"
136136
(pure 1#1)

test/lean/extern.expected.lean

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace Out.Functions
4545

4646
open option
4747

48-
/-- Type quantifiers: k_ex1150# : Bool, k_ex1149# : Bool -/
48+
/-- Type quantifiers: k_ex1162# : Bool, k_ex1161# : Bool -/
4949
def neq_bool (x : Bool) (y : Bool) : Bool :=
5050
(Bool.not (BEq.beq x y))
5151

@@ -166,12 +166,10 @@ def sep_backwards (arg_ : String) : SailM Unit := do
166166
def sep_forwards_matches (arg_ : Unit) : Bool :=
167167
match arg_ with
168168
| () => true
169-
| _ => false
170169

171170
def sep_backwards_matches (arg_ : String) : SailM Bool := do
172171
match arg_ with
173172
| _ => throw Error.Exit
174-
| _ => (pure false)
175173

176174
def extern_add (_ : Unit) : Int :=
177175
(5 +i 4)

test/lean/extern_bitvec.expected.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace Out.Functions
4545

4646
open option
4747

48-
/-- Type quantifiers: k_ex718# : Bool, k_ex717# : Bool -/
48+
/-- Type quantifiers: k_ex729# : Bool, k_ex728# : Bool -/
4949
def neq_bool (x : Bool) (y : Bool) : Bool :=
5050
(Bool.not (BEq.beq x y))
5151

0 commit comments

Comments
 (0)