-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEquivalence.lean
More file actions
2551 lines (2267 loc) · 119 KB
/
Equivalence.lean
File metadata and controls
2551 lines (2267 loc) · 119 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import OpenvmFv.Equivalence.Auipc
import OpenvmFv.Equivalence.BaseALU
import OpenvmFv.Equivalence.BranchEqual
import OpenvmFv.Equivalence.BranchLessThan
import OpenvmFv.Equivalence.DivRem
import OpenvmFv.Equivalence.JalLui
import OpenvmFv.Equivalence.JalR
import OpenvmFv.Equivalence.LoadSignExtend
import OpenvmFv.Equivalence.LoadStore
import OpenvmFv.Equivalence.Lt
import OpenvmFv.Equivalence.Mul
import OpenvmFv.Equivalence.Mulh
import OpenvmFv.Equivalence.Shift
set_option maxHeartbeats 2_000_000
set_option maxRecDepth 20_000
namespace RV32IM.Equivalence
section Auipc
open VmAirWrapper_auipc.constraints
open Equivalence.Auipc
attribute [local simp]
_executionBus_row
executionBus_row
_programBus_row
programBus_row
LeanRV32D.Functions.execute
LeanRV32D.Functions.execute_UTYPE
LeanRV32D.Functions.get_arch_pc
variable
[Field ExtF]
(air : Valid_VmAirWrapper_auipc FBB ExtF)
(row : ℕ)
(h_row : row ≤ air.last_row)
(h_constraints : allHold air row h_row)
(h_is_valid : air.core.is_valid row 0 = 1)
(h_bus_axioms : axiomsPerRow air row)
(h_bus_wellformedness : wf_propertiesToAssumePerRow air row)
(h_bus : (bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).1)
include h_constraints h_is_valid h_bus_axioms h_bus_wellformedness h_bus
theorem equiv_AUIPC
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let imm := (_programBus_row air row)[0]!.xc
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let imm := BitVec.ofNat 20 (imm.toNat >>> 4)
let instr : instruction := instruction.UTYPE (imm, rd, uop.AUIPC)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by
have h_spec := Auipc.ValidRows.spec_auipc ExtF air row h_row h_constraints h_is_valid h_bus_axioms h_bus_wellformedness
have h_nzd := rd_neq_0 air row h_row h_constraints h_is_valid h_bus_wellformedness
-- Apply the bus equivalence
rw [chip_bus_effect air row h_row h_constraints h_is_valid h_bus_wellformedness h_bus]
-- Get the hypotheses
have := chip_bus_hypotheses (state := state) air row h_row h_constraints h_is_valid h_bus_wellformedness
rw [this] at h_bus; clear this
obtain ⟨ h_pc, h_rd ⟩ := h_bus
extract_lets rd_ptr imm rd imm instr val reg_idx
subst rd_ptr imm rd imm instr val reg_idx
simp [h_pc, writeReg_state_success, -Fin.toNat_eq_val]; rw [Fin.toNat_eq_val]
rw [readReg_of_write_other_reg_state h_pc (by simp)]
simp
rw [wX_write_xreg_non_zero_equiv (rd := ⟨ (Transpiler.wrap_to_regidx (air.adapter.rd_ptr row 0)).val, by simp [Transpiler.wrap_to_regidx, get_instruction_fields_row] at h_nzd ⊢; omega ⟩) (h_rd := by simp)]
simp [write_xreg, Sail.writeReg, PreSail.writeReg, write_reg_state, cast]
rw [ExtDHashMap.insert_comm (h_neq := by have := @reg_of_fin_neq_nextPC; grind)]
congr; rw [← h_spec]
simp [← BitVec.toNat_inj, U32.toNat]
end Auipc
section BaseALU
open VmAirWrapper_alu.constraints
open Equivalence.BaseALU
attribute [local simp]
_programBus_row
programBus_row
execute_RTYPE'
execute_ITYPE'
execute_RTYPE_pure
execute_ITYPE_pure
ALU.ValidRows.rop_of_ALU_opcode
ALU.ValidRows.iop_of_ALU_opcode
variable
[Field ExtF]
(air : Valid_VmAirWrapper_alu FBB ExtF)
(row : ℕ)
(h_row : row ≤ air.last_row)
(h_constraints : allHold air row h_row)
(h_is_valid : air.core.is_valid row 0 = 1)
(h_bus_axioms : axiomsPerRow air row)
(h_bus_wellformedness : wf_propertiesToAssumePerRow air row)
(h_bus : (bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).1)
section ProofMacros
set_option hygiene false
macro "proof_start" : tactic => `(tactic| (
simp [h_opcode] at h_spec
-- Apply the bus equivalence
rw [chip_bus_effect air row h_row h_constraints h_is_valid h_bus_wellformedness h_bus]
-- Get the hypotheses
have := chip_bus_hypotheses (state := state) air row h_row h_constraints h_is_valid h_bus_wellformedness
rw [this] at h_bus; clear this
obtain ⟨ h_pc, h_rs1, h_rs2, h_rd ⟩ := h_bus
simp [h_imm] at h_rs2; clear h_rd
-- Get the fact that rd is non-zero
have h_nzd := rd_neq_0 air row h_row h_constraints h_is_valid h_bus_wellformedness
simp [get_instruction_fields_row] at h_nzd
extract_lets rs1_ptr rs2_ptr rd_ptr r2 r1 rd val reg_idx
subst rs1_ptr rs2_ptr rd_ptr r2 r1 rd val reg_idx
simp [
h_pc,
writeReg_state_success,
LeanRV32D.Functions.execute
]
rw [rX_bits_write_other_reg_state (h_neq := by apply reg_of_fin_neq_nextPC)]
rotate_left
rw [rX_read_xreg_equiv (rd := Transpiler.wrap_to_regidx (air.adapter.rs1_ptr row 0)) (h_rd := by simp)]
assumption
simp
))
macro "proof_non_imm_specific" : tactic => `(tactic| (
rw [rX_bits_write_other_reg_state (h_neq := by apply reg_of_fin_neq_nextPC)]
rotate_left
rw [rX_read_xreg_equiv (rd := Transpiler.wrap_to_regidx (air.adapter.rs2 row 0)) (h_rd := by simp)]
assumption
simp
))
macro "proof_end" : tactic => `(tactic| (
rw [wX_write_xreg_non_zero_equiv (rd := ⟨ (Transpiler.wrap_to_regidx (air.adapter.rd_ptr row 0)).val, by simp; omega ⟩) (h_rd := by simp)]
simp [write_xreg, Sail.writeReg, PreSail.writeReg, write_reg_state, cast]
rw [ExtDHashMap.insert_comm (h_neq := by have := @reg_of_fin_neq_nextPC; grind)]
congr 3; symm
assumption
))
macro "alu_non_imm_proof" : tactic => `(tactic| (
-- Get the spec
have h_spec := ALU.ValidRows.spec_base_ALU_non_imm ExtF air row (by omega) h_constraints h_is_valid h_bus_axioms h_bus_wellformedness h_imm
proof_start; proof_non_imm_specific; proof_end
))
macro "alu_imm_proof" : tactic => `(tactic| (
-- Get the spec
have h_spec := ALU.ValidRows.spec_base_ALU_imm ExtF air row (by omega) h_constraints h_is_valid h_bus_axioms h_bus_wellformedness h_imm
proof_start; proof_end
))
end ProofMacros
include h_constraints h_is_valid h_bus_axioms h_bus_wellformedness h_bus
theorem equiv_ADD
(h_opcode : (air.core.ctx row 0).instruction.opcode = 512)
(h_imm : air.adapter.rs2_as row 0 = 1)
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let rs1_ptr := (_programBus_row air row)[0]!.xb
let rs2_ptr := (_programBus_row air row)[0]!.xc
let r1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let r2 : regidx := ⟨ (Transpiler.wrap_to_regidx rs2_ptr).val, by simp ⟩
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let instr : instruction := instruction.RTYPE (r2, r1, rd, rop.ADD)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by alu_non_imm_proof
theorem equiv_ADDI
(h_opcode : (air.core.ctx row 0).instruction.opcode = 512)
(h_imm : air.adapter.rs2_as row 0 = 0)
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let rs1_ptr := (_programBus_row air row)[0]!.xb
let imm := (_programBus_row air row)[0]!.xc
let r1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let imm : BitVec 12 := BitVec.ofNat 12 imm.toNat
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let instr : instruction := instruction.ITYPE (imm, r1, rd, iop.ADDI)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by alu_imm_proof
theorem equiv_SUB
(h_opcode : (air.core.ctx row 0).instruction.opcode = 513)
(h_imm : air.adapter.rs2_as row 0 = 1)
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let rs1_ptr := (_programBus_row air row)[0]!.xb
let rs2_ptr := (_programBus_row air row)[0]!.xc
let r1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let r2 : regidx := ⟨ (Transpiler.wrap_to_regidx rs2_ptr).val, by simp ⟩
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let instr : instruction := instruction.RTYPE (r2, r1, rd, rop.SUB)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by alu_non_imm_proof
theorem equiv_XOR
(h_opcode : (air.core.ctx row 0).instruction.opcode = 514)
(h_imm : air.adapter.rs2_as row 0 = 1)
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let rs1_ptr := (_programBus_row air row)[0]!.xb
let rs2_ptr := (_programBus_row air row)[0]!.xc
let r1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let r2 : regidx := ⟨ (Transpiler.wrap_to_regidx rs2_ptr).val, by simp ⟩
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let instr : instruction := instruction.RTYPE (r2, r1, rd, rop.XOR)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by alu_non_imm_proof
theorem equiv_XORI
(h_opcode : (air.core.ctx row 0).instruction.opcode = 514)
(h_imm : air.adapter.rs2_as row 0 = 0)
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let rs1_ptr := (_programBus_row air row)[0]!.xb
let imm := (_programBus_row air row)[0]!.xc
let r1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let imm : BitVec 12 := BitVec.ofNat 12 imm.toNat
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let instr : instruction := instruction.ITYPE (imm, r1, rd, iop.XORI)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by alu_imm_proof
theorem equiv_OR
(h_opcode : (air.core.ctx row 0).instruction.opcode = 515)
(h_imm : air.adapter.rs2_as row 0 = 1)
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let rs1_ptr := (_programBus_row air row)[0]!.xb
let rs2_ptr := (_programBus_row air row)[0]!.xc
let r1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let r2 : regidx := ⟨ (Transpiler.wrap_to_regidx rs2_ptr).val, by simp ⟩
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let instr : instruction := instruction.RTYPE (r2, r1, rd, rop.OR)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by alu_non_imm_proof
theorem equiv_ORI
(h_opcode : (air.core.ctx row 0).instruction.opcode = 515)
(h_imm : air.adapter.rs2_as row 0 = 0)
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let rs1_ptr := (_programBus_row air row)[0]!.xb
let imm := (_programBus_row air row)[0]!.xc
let r1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let imm : BitVec 12 := BitVec.ofNat 12 imm.toNat
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let instr : instruction := instruction.ITYPE (imm, r1, rd, iop.ORI)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by alu_imm_proof
theorem equiv_AND
(h_opcode : (air.core.ctx row 0).instruction.opcode = 516)
(h_imm : air.adapter.rs2_as row 0 = 1)
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let rs1_ptr := (_programBus_row air row)[0]!.xb
let rs2_ptr := (_programBus_row air row)[0]!.xc
let r1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let r2 : regidx := ⟨ (Transpiler.wrap_to_regidx rs2_ptr).val, by simp ⟩
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let instr : instruction := instruction.RTYPE (r2, r1, rd, rop.AND)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by alu_non_imm_proof
theorem equiv_ANDI
(h_opcode : (air.core.ctx row 0).instruction.opcode = 516)
(h_imm : air.adapter.rs2_as row 0 = 0)
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let rs1_ptr := (_programBus_row air row)[0]!.xb
let imm := (_programBus_row air row)[0]!.xc
let r1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let imm : BitVec 12 := BitVec.ofNat 12 imm.toNat
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let instr : instruction := instruction.ITYPE (imm, r1, rd, iop.ANDI)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by alu_imm_proof
end BaseALU
section BranchEqual
open VmAirWrapper_branch_eq.constraints
open Equivalence.BranchEqual
attribute [local simp]
_programBus_row
programBus_row
LeanRV32D.Functions.execute
LeanRV32D.Functions.execute_BTYPE
variable
[Field ExtF]
(air : Valid_VmAirWrapper_branch_eq FBB ExtF)
(row : ℕ)
(h_row : row ≤ air.last_row)
(h_constraints : allHold air row h_row)
(h_is_valid : air.core.is_valid row 0 = 1)
(h_bus_axioms : axiomsPerRow air row)
(h_bus_wellformedness : wf_propertiesToAssumePerRow air row)
(h_bus : (bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).1)
(risc_v_assumptions : RISC_V_assumptions state mstatus pmaRegion misa mseccfg)
section ProofMacros
set_option hygiene false
macro "proof_common" : tactic => `(tactic| (
have ex_reg_misa : state.regs.get? Register.misa = .some misa
:= by simp [RISC_V_assumptions] at risc_v_assumptions; grind
have h_spec := BranchEqual.ValidRows.spec_BEQ_BNE_pc ExtF air row h_row h_constraints h_is_valid h_bus_axioms h_bus_wellformedness
simp [h_opcode] at h_spec
-- Apply the bus equivalence
rw [chip_bus_effect air row h_row h_constraints h_is_valid h_bus_wellformedness h_bus]
-- Get the hypotheses
have := chip_bus_hypotheses (state := state) air row h_row h_constraints h_is_valid h_bus_wellformedness
rw [this] at h_bus; clear this
obtain ⟨ h_pc, h_rs1, h_rs2 ⟩ := h_bus
extract_lets rs1_ptr rs2_ptr imm r2 r1 imm instr
subst rs1_ptr rs2_ptr imm r2 r1 imm instr
-- Get the axioms
simp [h_is_valid, VmAirWrapper_branch_eq_constraint_and_interaction_simplification, and_assoc] at h_bus_axioms
obtain ⟨ ub_pc, al_pc, ub_ts, ub_tpc, al_tpc, ub_nts ⟩ := h_bus_axioms
replace al_tpc : (BitVec.ofNat 32 (air.to_pc row 0)).toNat % 4 = 0
:= by simp; grind
simp [
h_pc,
writeReg_state_success,
]
rw [rX_bits_write_other_reg_state (h_neq := by apply reg_of_fin_neq_nextPC)]
rotate_left
rw [rX_read_xreg_equiv (rd := Transpiler.wrap_to_regidx (air.adapter.rs1_ptr row 0)) (h_rd := by simp)]
assumption
simp
rw [rX_bits_write_other_reg_state (h_neq := by apply reg_of_fin_neq_nextPC)]
rotate_left
rw [rX_read_xreg_equiv (rd := Transpiler.wrap_to_regidx (air.adapter.rs2_ptr row 0)) (h_rd := by simp)]
assumption
simp
))
end ProofMacros
include h_constraints h_is_valid h_bus_axioms h_bus_wellformedness h_bus risc_v_assumptions
theorem equiv_BEQ
(h_opcode : air.core.expected_opcode row 0 = 544)
:
let rs1_ptr := (_programBus_row air row)[0]!.xa
let rs2_ptr := (_programBus_row air row)[0]!.xb
let imm := (_programBus_row air row)[0]!.xc
let r1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let r2 : regidx := ⟨ (Transpiler.wrap_to_regidx rs2_ptr).val, by simp ⟩
let imm : BitVec 13 := BitVec.ofInt 13 (BabyBear.toInt imm)
let instr : instruction := instruction.BTYPE (imm, r2, r1, bop.BEQ)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by
proof_common
split_ifs at h_spec
. rw [if_pos (by assumption), h_spec]
rw [readReg_of_write_other_reg_state h_pc (by simp)]
simp
rw [if_neg,
writeReg_read_diff ex_reg_misa (by simp)]
. simp
rw [if_neg]
. simp [write_reg_state]
. rw [← h_spec]; clear *- ub_tpc al_tpc
simp [BitVec.ofBool, BitVec.getElem_eq_testBit_toNat]
rw [Nat.mod_eq_of_lt (by omega)]
grind
. rw [← h_spec]; clear *- ub_tpc al_tpc
simp [BitVec.ofBool, BitVec.getElem_eq_testBit_toNat]
grind
. rw [if_neg (by assumption), h_spec]
simp [write_reg_state]
theorem equiv_BNE
(h_opcode : air.core.expected_opcode row 0 = 545)
:
let rs1_ptr := (_programBus_row air row)[0]!.xa
let rs2_ptr := (_programBus_row air row)[0]!.xb
let imm := (_programBus_row air row)[0]!.xc
let r1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let r2 : regidx := ⟨ (Transpiler.wrap_to_regidx rs2_ptr).val, by simp ⟩
let imm : BitVec 13 := BitVec.ofInt 13 (BabyBear.toInt imm)
let instr : instruction := instruction.BTYPE (imm, r2, r1, bop.BNE)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by
proof_common
split_ifs at h_spec
. rw [if_pos (by assumption), h_spec]
simp [write_reg_state]
. rw [if_neg (by assumption), h_spec]
rw [readReg_of_write_other_reg_state h_pc (by simp)]
simp
rw [if_neg,
writeReg_read_diff ex_reg_misa (by simp)]
. simp
rw [if_neg]
. simp [write_reg_state]
. rw [← h_spec]; clear *- ub_tpc al_tpc
simp [BitVec.ofBool, BitVec.getElem_eq_testBit_toNat]
rw [Nat.mod_eq_of_lt (by omega)]
grind
. rw [← h_spec]; clear *- ub_tpc al_tpc
simp [BitVec.ofBool, BitVec.getElem_eq_testBit_toNat]
grind
end BranchEqual
section BranchLessThan
open VmAirWrapper_branch_lt.constraints
open Equivalence.BranchLessThan
attribute [local simp]
_programBus_row
programBus_row
LeanRV32D.Functions.execute
LeanRV32D.Functions.execute_BTYPE
variable
[Field ExtF]
(air : Valid_VmAirWrapper_branch_lt FBB ExtF)
(row : ℕ)
(h_row : row ≤ air.last_row)
(h_constraints : allHold air row h_row)
(h_is_valid : air.core.is_valid row 0 = 1)
(h_bus_axioms : axiomsPerRow air row)
(h_bus_wellformedness : wf_propertiesToAssumePerRow air row)
(h_bus : (bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).1)
(risc_v_assumptions : RISC_V_assumptions state mstatus pmaRegion misa mseccfg)
section ProofMacros
set_option hygiene false
macro "proof_common" : tactic => `(tactic| (
have ex_reg_misa : state.regs.get? Register.misa = .some misa
:= by simp [RISC_V_assumptions] at risc_v_assumptions; grind
have h_spec := BranchLessThan.ValidRows.spec_BLT_BLTU_BGE_BGEU_pc ExtF air row h_row h_constraints h_is_valid h_bus_axioms h_bus_wellformedness
simp [h_opcode] at h_spec
-- Apply the bus equivalence
rw [chip_bus_effect air row h_row h_constraints h_is_valid h_bus_wellformedness h_bus]
-- Get the hypotheses
have := chip_bus_hypotheses (state := state) air row h_row h_constraints h_is_valid h_bus_wellformedness
rw [this] at h_bus; clear this
obtain ⟨ h_pc, h_rs1, h_rs2 ⟩ := h_bus
extract_lets rs1_ptr rs2_ptr imm r1 r2 imm instr
subst rs1_ptr rs2_ptr imm r1 r2 imm instr
-- Get the axioms
simp [h_is_valid, VmAirWrapper_branch_lt_constraint_and_interaction_simplification, and_assoc] at h_bus_axioms
obtain ⟨ ub_pc, al_pc, ub_ts, ub_tpc, al_tpc, ub_nts ⟩ := h_bus_axioms
replace al_tpc : (BitVec.ofNat 32 (air.to_pc row 0)).toNat % 4 = 0
:= by simp; clear *- al_tpc; grind
simp [
h_pc,
writeReg_state_success,
]
rw [rX_bits_write_other_reg_state (h_neq := by apply reg_of_fin_neq_nextPC)]
rotate_left
rw [rX_read_xreg_equiv (rd := Transpiler.wrap_to_regidx (air.adapter.rs1_ptr row 0)) (h_rd := by simp)]
assumption
simp
rw [rX_bits_write_other_reg_state (h_neq := by apply reg_of_fin_neq_nextPC)]
rotate_left
rw [rX_read_xreg_equiv (rd := Transpiler.wrap_to_regidx (air.adapter.rs2_ptr row 0)) (h_rd := by simp)]
assumption
simp
split_ifs at h_spec
. rw [if_pos (by assumption), h_spec]
rw [readReg_of_write_other_reg_state h_pc (by simp)]
simp
rw [if_neg,
writeReg_read_diff ex_reg_misa (by simp)]
. simp
rw [if_neg]
. simp [write_reg_state]
. rw [← h_spec]; clear *- ub_tpc al_tpc
simp [BitVec.ofBool, BitVec.getElem_eq_testBit_toNat]
rw [Nat.mod_eq_of_lt (by omega)]
grind
. rw [← h_spec]; clear *- ub_tpc al_tpc
simp [BitVec.ofBool, BitVec.getElem_eq_testBit_toNat]
grind
. rw [if_neg (by assumption), h_spec]
simp [write_reg_state]
))
end ProofMacros
include h_constraints h_is_valid h_bus_axioms h_bus_wellformedness h_bus risc_v_assumptions
theorem equiv_BLT
(h_opcode : air.core.expected_opcode row 0 = 549)
:
let rs1_ptr := (_programBus_row air row)[0]!.xa
let rs2_ptr := (_programBus_row air row)[0]!.xb
let imm := (_programBus_row air row)[0]!.xc
let r1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let r2 : regidx := ⟨ (Transpiler.wrap_to_regidx rs2_ptr).val, by simp ⟩
let imm : BitVec 13 := BitVec.ofInt 13 (BabyBear.toInt imm)
let instr : instruction := instruction.BTYPE (imm, r2, r1, bop.BLT)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by proof_common
theorem equiv_BLTU
(h_opcode : air.core.expected_opcode row 0 = 550)
:
let rs1_ptr := (_programBus_row air row)[0]!.xa
let rs2_ptr := (_programBus_row air row)[0]!.xb
let imm := (_programBus_row air row)[0]!.xc
let r1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let r2 : regidx := ⟨ (Transpiler.wrap_to_regidx rs2_ptr).val, by simp ⟩
let imm : BitVec 13 := BitVec.ofInt 13 (BabyBear.toInt imm)
let instr : instruction := instruction.BTYPE (imm, r2, r1, bop.BLTU)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by proof_common
theorem equiv_BGE
(h_opcode : air.core.expected_opcode row 0 = 551)
:
let rs1_ptr := (_programBus_row air row)[0]!.xa
let rs2_ptr := (_programBus_row air row)[0]!.xb
let imm := (_programBus_row air row)[0]!.xc
let r1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let r2 : regidx := ⟨ (Transpiler.wrap_to_regidx rs2_ptr).val, by simp ⟩
let imm : BitVec 13 := BitVec.ofInt 13 (BabyBear.toInt imm)
let instr : instruction := instruction.BTYPE (imm, r2, r1, bop.BGE)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by proof_common
theorem equiv_BGEU
(h_opcode : air.core.expected_opcode row 0 = 552)
:
let rs1_ptr := (_programBus_row air row)[0]!.xa
let rs2_ptr := (_programBus_row air row)[0]!.xb
let imm := (_programBus_row air row)[0]!.xc
let r1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let r2 : regidx := ⟨ (Transpiler.wrap_to_regidx rs2_ptr).val, by simp ⟩
let imm : BitVec 13 := BitVec.ofInt 13 (BabyBear.toInt imm)
let instr : instruction := instruction.BTYPE (imm, r2, r1, bop.BGEU)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by proof_common
end BranchLessThan
@[simp]
lemma bv_ofFin_ofNat
(x : Fin 256)
: @BitVec.ofFin 8 x = BitVec.ofNat 8 x.val
:= by simp
@[simp]
lemma bv_ofNat_clearMod
(x : ℕ)
: BitVec.ofNat 8 (x % 256) = BitVec.ofNat 8 x
:= by simp [← BitVec.toNat_inj]
@[simp]
lemma bv_natCast_bv8
(x : FBB)
:
@Nat.cast (BitVec 8) BitVec.instNatCast x = BitVec.ofNat 8 x.val
:= by simp
section DivRem
open VmAirWrapper_divrem.constraints
open Equivalence.DivRem
attribute [local simp]
_executionBus_row
executionBus_row
_programBus_row
programBus_row
execute_DIV'
execute_REM'
execute_DIV_REM_pure
execute_DIV_REM_pure_int
attribute [-simp]
Int.tmod_eq_emod
variable
[Field ExtF]
(air : Valid_VmAirWrapper_divrem FBB ExtF)
(row : ℕ)
(h_row : row ≤ air.last_row)
(h_constraints : allHold air row h_row)
(h_is_valid : air.core.is_valid row 0 = 1)
(h_bus_axioms : axiomsPerRow air row)
(h_bus_wellformedness : wf_propertiesToAssumePerRow air row)
(h_bus : (bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).1)
section ProofMacros
set_option hygiene false
macro "proof_common" : tactic => `(tactic| (
have h_ess := DivRem.ValidRows.essentials ExtF air row h_row h_constraints h_is_valid h_bus_axioms h_bus_wellformedness
have h_spec := h_ess.2.2.2.2.2.2.2.2.2.2.2.2.2
obtain ⟨ ua0, ua1, ua2, ua3, ub0, ub1, ub2, ub3, uc0, uc1, uc2, uc3 ⟩ :
(air.core.a row 0 0).val % 256 = (air.core.a row 0 0).val ∧ (air.core.a row 0 1).val % 256 = (air.core.a row 0 1).val ∧ (air.core.a row 0 2).val % 256 = (air.core.a row 0 2).val ∧ (air.core.a row 0 3).val % 256 = (air.core.a row 0 3).val ∧
(air.core.b_0 row 0).val % 256 = (air.core.b_0 row 0).val ∧ (air.core.b_1 row 0).val % 256 = (air.core.b_1 row 0).val ∧ (air.core.b_2 row 0).val % 256 = (air.core.b_2 row 0).val ∧ (air.core.b_3 row 0).val % 256 = (air.core.b_3 row 0).val ∧
(air.core.c_0 row 0).val % 256 = (air.core.c_0 row 0).val ∧ (air.core.c_1 row 0).val % 256 = (air.core.c_1 row 0).val ∧ (air.core.c_2 row 0).val % 256 = (air.core.c_2 row 0).val ∧ (air.core.c_3 row 0).val % 256 = (air.core.c_3 row 0).val
:= by omega
simp [h_opcode] at h_spec
obtain ⟨ ha0, ha1, ha2, ha3 ⟩ := h_ess
-- Apply the bus equivalence
rw [chip_bus_effect air row h_row h_constraints h_is_valid h_bus_wellformedness h_bus]
-- Get the hypotheses
have := chip_bus_hypotheses (state := state) air row h_row h_constraints h_is_valid h_bus_wellformedness
rw [this] at h_bus; clear this
obtain ⟨ h_pc, h_rs1, h_rs2, h_rd ⟩ := h_bus; clear h_rd
-- Get the fact that rd is non-zero
have h_nzd := rd_neq_0 air row h_row h_constraints h_is_valid h_bus_wellformedness
simp [get_instruction_fields_row] at h_nzd
extract_lets rd_ptr rs1_ptr rs2_ptr rs1 rs2 rd instr val reg_idx
simp [ ub0, ub1, ub2, ub3, uc0, uc1, uc2, uc3 ] at h_rs1 h_rs2
subst rd_ptr rs1_ptr rs2_ptr rs1 rs2 rd instr val reg_idx
simp [
h_pc,
writeReg_state_success,
LeanRV32D.Functions.execute,
ua0, ua1, ua2, ua3
]
rw [rX_bits_write_other_reg_state (h_neq := by apply reg_of_fin_neq_nextPC)]
rotate_left
rw [rX_read_xreg_equiv (rd := Transpiler.wrap_to_regidx (air.adapter.rs1_ptr row 0)) (h_rd := by simp)]
assumption
simp
rw [rX_bits_write_other_reg_state (h_neq := by apply reg_of_fin_neq_nextPC)]
rotate_left
rw [rX_read_xreg_equiv (rd := Transpiler.wrap_to_regidx (air.adapter.rs2_ptr row 0)) (h_rd := by simp)]
assumption
simp [h_spec]
replace h_spec := DivRem.ValidRows.spec_DIVREM ExtF air row h_row h_constraints h_is_valid h_bus_wellformedness
have h_spec' := DivRem.ValidRows.spec_DIVUREMU ExtF air row h_row h_constraints h_is_valid h_bus_wellformedness
simp [h_opcode] at h_spec h_spec'
(try simp [← h_spec]); (try simp [← h_spec'])
rw [wX_write_xreg_non_zero_equiv (rd := ⟨ (Transpiler.wrap_to_regidx (air.adapter.rd_ptr row 0)).val, by simp [Transpiler.wrap_to_regidx] at h_nzd ⊢; omega ⟩) (h_rd := by simp)]
simp [write_xreg, Sail.writeReg, PreSail.writeReg, write_reg_state, cast]
rw [ExtDHashMap.insert_comm (h_neq := by have := @reg_of_fin_neq_nextPC; clear *- this; grind)]
))
end ProofMacros
include h_constraints h_is_valid h_bus_axioms h_bus_wellformedness h_bus
theorem equiv_DIV
(h_opcode : (air.core.ctx row 0).instruction.opcode = 596)
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let rs1_ptr := (_programBus_row air row)[0]!.xb
let rs2_ptr := (_programBus_row air row)[0]!.xc
let rs1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let rs2 : regidx := ⟨ (Transpiler.wrap_to_regidx rs2_ptr).val, by simp ⟩
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let instr : instruction := .DIV (rs2, rs1, rd, false)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by proof_common
theorem equiv_DIVU
(h_opcode : (air.core.ctx row 0).instruction.opcode = 597)
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let rs1_ptr := (_programBus_row air row)[0]!.xb
let rs2_ptr := (_programBus_row air row)[0]!.xc
let rs1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let rs2 : regidx := ⟨ (Transpiler.wrap_to_regidx rs2_ptr).val, by simp ⟩
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let instr : instruction := .DIV (rs2, rs1, rd, true)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by proof_common
theorem equiv_REM
(h_opcode : (air.core.ctx row 0).instruction.opcode = 598)
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let rs1_ptr := (_programBus_row air row)[0]!.xb
let rs2_ptr := (_programBus_row air row)[0]!.xc
let rs1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let rs2 : regidx := ⟨ (Transpiler.wrap_to_regidx rs2_ptr).val, by simp ⟩
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let instr : instruction := .REM (rs2, rs1, rd, false)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by proof_common
theorem equiv_REMU
(h_opcode : (air.core.ctx row 0).instruction.opcode = 599)
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let rs1_ptr := (_programBus_row air row)[0]!.xb
let rs2_ptr := (_programBus_row air row)[0]!.xc
let rs1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let rs2 : regidx := ⟨ (Transpiler.wrap_to_regidx rs2_ptr).val, by simp ⟩
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let instr : instruction := .REM (rs2, rs1, rd, true)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by proof_common
end DivRem
section JalR
open VmAirWrapper_jalr.constraints
open Equivalence.JalR
attribute [local simp]
_executionBus_row
executionBus_row
_programBus_row
programBus_row
LeanRV32D.Functions.execute
LeanRV32D.Functions.execute_JALR
LeanRV32D.Functions.update_elp_state
LeanRV32D.Functions.get_xLPE
variable
[Field ExtF]
(air : Valid_VmAirWrapper_jalr FBB ExtF)
(row : ℕ)
(h_row : row ≤ air.last_row)
(h_constraints : allHold air row h_row)
(h_is_valid : air.core.is_valid row 0 = 1)
(h_bus_axioms : axiomsPerRow air row)
(h_bus_wellformedness : wf_propertiesToAssumePerRow air row)
(h_bus : (bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).1)
(risc_v_assumptions : RISC_V_assumptions state mstatus pmaRegion misa mseccfg)
include h_constraints h_is_valid h_bus_axioms h_bus_wellformedness h_bus risc_v_assumptions
theorem equiv_JALR
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let rs1_ptr := (_programBus_row air row)[0]!.xb
let imm := (_programBus_row air row)[0]!.xc
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let rs1 : regidx := ⟨ (Transpiler.wrap_to_regidx rs1_ptr).val, by simp ⟩
let imm : BitVec 12 := BitVec.ofNat 12 imm
let instr : instruction := .JALR (imm, rs1, rd)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by
obtain ⟨ h_priv, h_mprv, h_pma_regions, h_pma_base, h_pma_size, h_pma_readable, h_pma_writable, h_pma_misaligned, h_htif, h_misa, h_mseccfg ⟩ := risc_v_assumptions
have h_nzd := rd_neq_0 air row h_row h_constraints h_is_valid h_bus_wellformedness
simp [get_instruction_fields_row] at h_nzd
obtain ⟨ h_spec_rd, h_spec_npc ⟩ := JalR.ValidRows.spec_jalr ExtF air row h_row h_constraints h_bus_axioms h_bus_wellformedness (by simp [h_is_valid])
-- Apply the bus equivalence
rw [chip_bus_effect air row h_row h_constraints h_is_valid h_bus_wellformedness h_bus]
-- Get the hypotheses
have := chip_bus_hypotheses (state := state) air row h_row h_constraints h_is_valid h_bus_wellformedness
rw [this] at h_bus; clear this
obtain ⟨ h_pc, h_rs1, h_rd ⟩ := h_bus
extract_lets rd_ptr rs1_ptr imm rd rs1 imm instr
subst rd_ptr rs1_ptr imm rd rs1 imm instr
simp at h_rs1 h_rd h_spec_rd h_spec_npc
-- Get the axioms
simp [h_is_valid, VmAirWrapper_branch_eq_constraint_and_interaction_simplification, and_assoc] at h_bus_axioms
obtain ⟨ ub_pc, al_pc, ub_ts, ub_tpc, al_tpc, ub_nts ⟩ := h_bus_axioms
replace al_tpc : (BitVec.ofNat 32 (air.core.to_pc row 0)).toNat % 4 = 0
:= by simp; grind
simp [
h_pc,
writeReg_state_success,
]
rw [readReg_of_write_other_reg_state h_priv (h_neq := by simp)]
simp
rw [readReg_of_write_other_reg_state h_mseccfg (h_neq := by simp)]
simp
rw [rX_bits_write_other_reg_state (h_neq := by apply reg_of_fin_neq_nextPC)]
rotate_left
rw [rX_read_xreg_equiv (rd := Transpiler.wrap_to_regidx (air.adapter.rs1_ptr row 0)) (h_rd := by simp)]
assumption
simp [Sail.BitVec.update]
rw [if_neg]
. rw [writeReg_read_diff h_misa (h_neq := by simp)]
simp
rw [wX_write_xreg_non_zero_equiv (rd := ⟨ (Transpiler.wrap_to_regidx (air.adapter.rd_ptr row 0)).val, by simp [Transpiler.wrap_to_regidx] at h_nzd ⊢; omega ⟩) (h_rd := by simp)]
simp [write_xreg, Sail.writeReg, PreSail.writeReg, write_reg_state, cast]
rw [ExtDHashMap.insert_comm (h_neq := by have := @reg_of_fin_neq_nextPC; grind)]
congr
. simp [h_spec_rd]
. simp [h_spec_npc]
. simp [h_spec_npc] at al_tpc
obtain ⟨ x, eq_x ⟩ : ∃ x, x = U32.toNat #v[BitVec.ofNat 8 (air.core.rs1_data_0 row 0).val, BitVec.ofNat 8 (air.core.rs1_data_1 row 0).val, BitVec.ofNat 8 (air.core.rs1_data_2 row 0).val, BitVec.ofNat 8 (air.core.rs1_data_3 row 0).val] +
(BitVec.signExtend 32 (BitVec.ofNat 12 (air.core.imm row 0).val)).toNat := by simp
simp [← BitVec.toNat_inj] at h_spec_npc
rw [Nat.mod_eq_of_lt (b := 4294967296) (by omega)] at h_spec_npc
simp [Fin.lt_def, h_spec_npc] at ub_tpc
rw [← eq_x] at al_tpc ub_tpc
simp [BitVec.ofBool, BitVec.getElem_eq_testBit_toNat, ← eq_x]
have : (x % 4294967296).testBit 1 = x.testBit 1
:= by simp [Nat.testBit_eq_decide_div_mod_eq]; omega
rw [this]; clear this
simp [Nat.testBit_eq_decide_div_mod_eq]
clear *- al_tpc
simp [Bool.cond_eq_ite]
suffices : (x % 4294967296) / 2 % 2 = 0
. omega
. obtain ⟨ x, eq_x, ub_x ⟩ : ∃ y, y = x % 4294967296 ∧ y < 4294967296
:= by use x % 4294967296; simp; omega
rw [← eq_x] at al_tpc ⊢; clear eq_x
suffices : (x % 4) / 2 % 2 = 0
. omega
. have := @Nat.and_mod_two_pow 4294967294 x 2
simp [this] at al_tpc; clear this ub_x
rw [Nat.and_comm] at al_tpc
have := @Nat.and_two_pow (i := 1) (x % 4)
simp [al_tpc] at this
simp [Nat.testBit_eq_decide_div_mod_eq] at this
assumption
end JalR
section JalLui
open VmAirWrapper_jallui.constraints
open Equivalence.JalLui
attribute [local simp]
_executionBus_row
executionBus_row
_programBus_row
programBus_row
LeanRV32D.Functions.execute
LeanRV32D.Functions.execute_JAL
LeanRV32D.Functions.execute_UTYPE
variable
[Field ExtF]
(air : Valid_VmAirWrapper_jallui FBB ExtF)
(row : ℕ)
(h_row : row ≤ air.last_row)
(h_constraints : allHold air row h_row)
(h_is_valid : air.core.is_valid row 0 = 1)
(h_bus_axioms : axiomsPerRow air row)
(h_bus_wellformedness : wf_propertiesToAssumePerRow air row)
(h_bus : (bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).1)
(risc_v_assumptions : RISC_V_assumptions state mstatus pmaRegion misa mseccfg)
include h_constraints h_is_valid h_bus_axioms h_bus_wellformedness h_bus risc_v_assumptions
theorem equiv_JAL
(h_opcode : air.core.expected_opcode row 0 = 560)
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let imm := (_programBus_row air row)[0]!.xc
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let imm : BitVec 21 := BitVec.ofInt 21 (BabyBear.toInt imm)
let instr : instruction := .JAL (imm, rd)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by
have ex_reg_misa : state.regs.get? Register.misa = .some misa
:= by simp [RISC_V_assumptions] at risc_v_assumptions; grind
have h_nzd := rd_neq_0 air row h_row h_constraints h_is_valid h_bus_wellformedness
simp [get_instruction_fields_row] at h_nzd
have h_spec := JalLui.ValidRows.spec_jal ExtF air row h_row h_constraints h_bus_axioms h_bus_wellformedness (by simp [h_is_valid])
simp [h_opcode] at h_spec
obtain ⟨ h_spec_rd, h_spec_pc ⟩ := h_spec
-- Apply the bus equivalence
rw [chip_bus_effect air row h_row h_constraints h_is_valid h_bus_wellformedness h_bus]
-- Get the hypotheses
have := chip_bus_hypotheses (state := state) air row h_row h_constraints h_is_valid h_bus_wellformedness
rw [this] at h_bus; clear this
obtain ⟨ h_pc, h_rd ⟩ := h_bus
extract_lets rd_ptr imm rd imm instr
subst rd_ptr imm rd imm instr
simp at h_rd h_spec_rd h_spec_pc
-- Get the axioms
simp [h_is_valid, VmAirWrapper_branch_eq_constraint_and_interaction_simplification, and_assoc] at h_bus_axioms
obtain ⟨ ub_pc, al_pc, ub_ts, ub_tpc, al_tpc, ub_nts ⟩ := h_bus_axioms
replace al_tpc : (BitVec.ofNat 32 (air.to_pc row 0)).toNat % 4 = 0
:= by simp; grind
simp [
h_pc,
writeReg_state_success,
]
rw [readReg_of_write_other_reg_state h_pc (h_neq := by simp)]
simp
rw [if_neg]
. rw [writeReg_read_diff ex_reg_misa (h_neq := by simp)]
simp
rw [if_neg]
. simp
rw [wX_write_xreg_non_zero_equiv (rd := ⟨ (Transpiler.wrap_to_regidx (air.adapter.inner.rd_ptr row 0)).val, by simp [Transpiler.wrap_to_regidx] at h_nzd ⊢; omega ⟩) (h_rd := by simp)]
simp [write_xreg, Sail.writeReg, PreSail.writeReg, write_reg_state, cast]
rw [ExtDHashMap.insert_comm (h_neq := by have := @reg_of_fin_neq_nextPC; grind)]
congr 3
. rw [h_spec_rd]
. rw [h_spec_pc]
. rw [← h_spec_pc]
simp [BitVec.ofBool, BitVec.getElem_eq_testBit_toNat]
rw [Nat.mod_eq_of_lt (by omega)]
grind
. rw [← h_spec_pc]
simp [BitVec.ofBool, BitVec.getElem_eq_testBit_toNat]
grind
theorem equiv_LUI
(h_opcode : air.core.expected_opcode row 0 = 561)
:
let rd_ptr := (_programBus_row air row)[0]!.xa
let imm := (_programBus_row air row)[0]!.xc
let rd : regidx := ⟨ (Transpiler.wrap_to_regidx rd_ptr).val, by simp ⟩
let imm : BitVec 21 := BitVec.ofNat 21 imm
let instr : instruction := .UTYPE (imm, rd, .LUI)
execute_instruction instr state =
(bus_effect (_executionBus_row air row) (_memoryBus_row air row) state).2
:= by
have h_nzd := rd_neq_0 air row h_row h_constraints h_is_valid h_bus_wellformedness
simp [get_instruction_fields_row] at h_nzd
have spec := JalLui.ValidRows.spec_lui ExtF air row h_row h_constraints h_bus_axioms h_bus_wellformedness (by simp [h_is_valid])
simp [h_opcode] at spec
obtain ⟨ h_spec_rd, h_spec_pc ⟩ := spec
-- Apply the bus equivalence
rw [chip_bus_effect air row h_row h_constraints h_is_valid h_bus_wellformedness h_bus]
-- Get the hypotheses
have := chip_bus_hypotheses (state := state) air row h_row h_constraints h_is_valid h_bus_wellformedness
rw [this] at h_bus; clear this
obtain ⟨ h_pc, h_rd ⟩ := h_bus
extract_lets rd_ptr imm rd imm instr
subst rd_ptr imm rd imm instr
simp at h_rd h_spec_rd h_spec_pc
-- Get the axioms
simp [h_is_valid, VmAirWrapper_branch_eq_constraint_and_interaction_simplification, and_assoc] at h_bus_axioms
obtain ⟨ ub_pc, al_pc, ub_ts, ub_tpc, al_tpc, ub_nts ⟩ := h_bus_axioms
replace al_tpc : (BitVec.ofNat 32 (air.to_pc row 0)).toNat % 4 = 0
:= by simp; grind
simp [
h_pc,
writeReg_state_success,
]
rw [wX_write_xreg_non_zero_equiv (rd := ⟨ (Transpiler.wrap_to_regidx (air.adapter.inner.rd_ptr row 0)).val, by simp [Transpiler.wrap_to_regidx] at h_nzd ⊢; omega ⟩) (h_rd := by simp)]
simp [write_xreg, Sail.writeReg, PreSail.writeReg, write_reg_state, cast]
rw [ExtDHashMap.insert_comm (h_neq := by have := @reg_of_fin_neq_nextPC; grind)]
rw [h_spec_rd, h_spec_pc]
end JalLui
section LoadSignExtend
open VmAirWrapper_load_sign_extend.constraints
attribute [local simp]
_executionBus_row
executionBus_row
_programBus_row
programBus_row
variable