-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSyntax.v
2482 lines (2158 loc) · 90.4 KB
/
Syntax.v
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
Require Export Bool Ascii String Fin List FunctionalExtensionality Psatz PeanoNat.
Require Export Kami.Lib.VectorFacts Kami.Lib.EclecticLib.
Require Export Kami.Lib.Word Kami.Lib.WordProperties.
Export ListNotations.
Require Import Permutation.
Require Import ZArith.
Global Set Implicit Arguments.
Global Set Asymmetric Patterns.
Global Open Scope word_scope.
Global Open Scope nat_scope.
Global Open Scope string_scope.
Global Open Scope vector_scope.
Global Open Scope list_scope.
Inductive Kind :=
| Bool : Kind
| Bit : nat -> Kind
| Struct : forall n, (Fin.t n -> Kind) -> (Fin.t n -> string) -> Kind
| Array : nat -> Kind -> Kind.
Inductive FullKind: Type :=
| SyntaxKind: Kind -> FullKind
| NativeKind (t: Type) (c : t) : FullKind.
Inductive ConstT: Kind -> Type :=
| ConstBool: bool -> ConstT Bool
| ConstBit n: word n -> ConstT (Bit n)
| ConstStruct n fk fs (fv: forall i, ConstT (fk i)): ConstT (@Struct n fk fs)
| ConstArray n k (fk: Fin.t n -> ConstT k): ConstT (Array n k).
Inductive ConstFullT: FullKind -> Type :=
| SyntaxConst k: ConstT k -> ConstFullT (SyntaxKind k)
| NativeConst t (c' : t) : ConstFullT (NativeKind c').
Coercion ConstBool : bool >-> ConstT.
Coercion ConstBit : word >-> ConstT.
Fixpoint getDefaultConst (k: Kind): ConstT k :=
match k with
| Bool => ConstBool false
| Bit n => ConstBit (wzero n)
| Struct n fk fs =>
ConstStruct fk fs (fun i => getDefaultConst (fk i))
| Array n k => ConstArray (fun _ => getDefaultConst k)
end.
Notation Default := (getDefaultConst _).
Fixpoint getDefaultConstFullKind (k : FullKind) : ConstFullT k :=
match k with
| SyntaxKind k' => SyntaxConst (getDefaultConst k')
| NativeKind t c' => NativeConst c'
end.
Inductive UniBoolOp: Set :=
| Neg: UniBoolOp.
Inductive CABoolOp: Set :=
| And: CABoolOp
(* | Or: CABoolOp *)
| Xor: CABoolOp.
Inductive UniBitOp: nat -> nat -> Set :=
| Inv n: UniBitOp n n
| TruncLsb lsb msb: UniBitOp (lsb + msb) lsb
| TruncMsb lsb msb: UniBitOp (lsb + msb) msb
| UAnd n: UniBitOp n 1
| UOr n: UniBitOp n 1
| UXor n: UniBitOp n 1.
Inductive BinSign := SignSS | SignSU | SignUU.
Inductive BinBitOp: nat -> nat -> nat -> Set :=
| Sub n: BinBitOp n n n
| Div n: BinBitOp n n n
| Rem n: BinBitOp n n n
| Sll n m: BinBitOp n m n
| Srl n m: BinBitOp n m n
| Sra n m: BinBitOp n m n
| Concat msb lsb: BinBitOp msb lsb (lsb + msb) (* MSB : n1, LSB : n2 *).
Inductive CABitOp: Set :=
| Add: CABitOp
| Mul: CABitOp
| Band: CABitOp
(* | Bor: CABitOp *)
| Bxor: CABitOp.
Inductive BinBitBoolOp: nat -> nat -> Set :=
| LessThan n: BinBitBoolOp n n.
Fixpoint type (k: Kind): Type :=
match k with
| Bool => bool
| Bit n => word n
| Struct n fk fs => forall i, type (fk i)
| Array n k' => Fin.t n -> type k'
end.
Fixpoint evalConstT k (e: ConstT k): type k :=
match e in ConstT k return type k with
| ConstBool b => b
| ConstBit n w => w
| ConstStruct n fk fs fv => fun i => evalConstT (fv i)
| ConstArray n k' fv => fun i => evalConstT (fv i)
end.
Section Phoas.
Variable ty: Kind -> Type.
Definition fullType k := match k with
| SyntaxKind k' => ty k'
| NativeKind k' c' => k'
end.
Inductive Expr: FullKind -> Type :=
| Var k: fullType k -> Expr k
| Const k: ConstT k -> Expr (SyntaxKind k)
| UniBool: UniBoolOp -> Expr (SyntaxKind Bool) -> Expr (SyntaxKind Bool)
| CABool: CABoolOp -> list (Expr (SyntaxKind Bool)) -> Expr (SyntaxKind Bool)
| UniBit n1 n2: UniBitOp n1 n2 -> Expr (SyntaxKind (Bit n1)) -> Expr (SyntaxKind (Bit n2))
| CABit n: CABitOp -> list (Expr (SyntaxKind (Bit n))) -> Expr (SyntaxKind (Bit n))
| BinBit n1 n2 n3: BinBitOp n1 n2 n3 ->
Expr (SyntaxKind (Bit n1)) -> Expr (SyntaxKind (Bit n2)) ->
Expr (SyntaxKind (Bit n3))
| BinBitBool n1 n2: BinBitBoolOp n1 n2 ->
Expr (SyntaxKind (Bit n1)) -> Expr (SyntaxKind (Bit n2)) ->
Expr (SyntaxKind Bool)
| ITE k: Expr (SyntaxKind Bool) -> Expr k -> Expr k -> Expr k
| Eq k: Expr (SyntaxKind k) -> Expr (SyntaxKind k) -> Expr (SyntaxKind Bool)
| ReadStruct n (fk: Fin.t n -> Kind) (fs: Fin.t n -> string)
(e: Expr (SyntaxKind (Struct fk fs))) i:
Expr (SyntaxKind (fk i))
| BuildStruct n (fk: Fin.t n -> Kind) (fs: Fin.t n -> string)
(fv: forall i, Expr (SyntaxKind (fk i))):
Expr (SyntaxKind (Struct fk fs))
| ReadArray n m k: Expr (SyntaxKind (Array n k)) ->
Expr (SyntaxKind (Bit m)) ->
Expr (SyntaxKind k)
| ReadArrayConst n k: Expr (SyntaxKind (Array n k)) ->
Fin.t n ->
Expr (SyntaxKind k)
| BuildArray n k: (Fin.t n -> Expr (SyntaxKind k)) -> Expr (SyntaxKind (Array n k))
| Kor k: list (Expr (SyntaxKind k)) -> Expr (SyntaxKind k)
| ToNative k:
Expr (SyntaxKind k) -> Expr (@NativeKind (type k) (evalConstT (getDefaultConst k)))
| FromNative k: Expr (@NativeKind (type k) (evalConstT (getDefaultConst k))) ->
Expr (SyntaxKind k).
Definition UpdateArray n m k (e: Expr (SyntaxKind (Array n k)))
(i: Expr (SyntaxKind (Bit m)))
(v: Expr (SyntaxKind k)) :=
BuildArray (fun i' : Fin.t n =>
ITE (Eq i (Const (natToWord _ (proj1_sig (Fin.to_nat i'))))) v
(ReadArrayConst e i')).
Definition UpdateArrayConst n k (e: Expr (SyntaxKind (Array n k)))
(i: Fin.t n)
(v: Expr (SyntaxKind k)) :=
BuildArray (fun i' : Fin.t n =>
match Fin.eq_dec i i' with
| left _ => v
| right _ => ReadArrayConst e i'
end).
Definition UpdateStruct n (fk: Fin.t n -> Kind) (fs: Fin.t n -> string)
(e: Expr (SyntaxKind (Struct fk fs))) i (v: Expr (SyntaxKind (fk i))) :=
BuildStruct fk fs (fun i' => match Fin_eq_dec i i' with
| left pf =>
match pf in _ = Y return
Expr (SyntaxKind (fk Y)) with
| eq_refl => v
end
| right _ => ReadStruct e i'
end).
Section BitOps.
Definition castBits ni no (pf: ni = no) (e: Expr (SyntaxKind (Bit ni))) :=
nat_cast (fun n => Expr (SyntaxKind (Bit n))) pf e.
Definition Slt n (e1 e2: Expr (SyntaxKind (Bit (n + 1)))) :=
Eq (Eq (UniBit (TruncMsb n 1) e1) (UniBit (TruncMsb n 1) e2)) (BinBitBool (LessThan _) e1 e2).
Definition ConstExtract lsb n msb (e: Expr (SyntaxKind (Bit (lsb + n + msb)))): Expr (SyntaxKind (Bit n)) :=
UniBit (TruncMsb lsb n) (UniBit (TruncLsb (lsb + n) msb) e).
Definition OneExtend msb lsb (e: Expr (SyntaxKind (Bit lsb))): Expr (SyntaxKind (Bit (lsb + msb))) :=
(BinBit (Concat msb lsb) (Const (wones msb))) e.
Definition ZeroExtend msb lsb (e: Expr (SyntaxKind (Bit lsb))): Expr (SyntaxKind (Bit (lsb + msb))) :=
(BinBit (Concat msb lsb) (Const (wzero msb))) e.
Definition SignExtend lsb msb: Expr (SyntaxKind (Bit lsb)) -> Expr (SyntaxKind (Bit (lsb + msb))).
refine
match lsb return Expr (SyntaxKind (Bit lsb)) -> Expr (SyntaxKind (Bit (lsb + msb))) with
| 0 => fun _ => Const (wzero msb)
| S m => fun e => BinBit (Concat msb (S m)) (ITE (Eq (UniBit (TruncMsb m 1)
(castBits _ e))
(Const (WO~0)%word))
(Const (wzero msb))
(Const (wones msb))) e
end; abstract lia.
Defined.
Fixpoint replicate sz (e: Expr (SyntaxKind (Bit sz))) n : Expr (SyntaxKind (Bit (n * sz))) :=
match n with
| 0 => Const WO
| S m => BinBit (Concat (m * sz) sz) (replicate e m) e
end.
Definition OneExtendTruncLsb ni no (e: Expr (SyntaxKind (Bit ni))):
Expr (SyntaxKind (Bit no)).
refine
match Compare_dec.lt_dec ni no with
| left isLt => castBits _ (@OneExtend (no - ni) ni e)
| right isGe => UniBit (TruncLsb no (ni - no)) (castBits _ e)
end; abstract lia.
Defined.
Definition ZeroExtendTruncLsb ni no (e: Expr (SyntaxKind (Bit ni))):
Expr (SyntaxKind (Bit no)).
refine
match Compare_dec.lt_dec ni no with
| left isLt => castBits _ (@ZeroExtend (no - ni) ni e)
| right isGe => UniBit (TruncLsb no (ni - no)) (castBits _ e)
end; abstract lia.
Defined.
Definition SignExtendTruncLsb ni no (e: Expr (SyntaxKind (Bit ni))):
Expr (SyntaxKind (Bit no)).
refine
match Compare_dec.lt_dec ni no with
| left isLt => castBits _ (@SignExtend ni (no - ni) e)
| right isGe => UniBit (TruncLsb no (ni - no)) (castBits _ e)
end; abstract Omega.omega.
Defined.
Definition ZeroExtendTruncMsb ni no (e: Expr (SyntaxKind (Bit ni))):
Expr (SyntaxKind (Bit no)).
refine
match Compare_dec.lt_dec ni no with
| left isLt => castBits _ (@ZeroExtend (no - ni) ni e)
| right isGe => UniBit (TruncMsb (ni - no) no) (castBits _ e)
end; abstract lia.
Defined.
Definition SignExtendTruncMsb ni no (e: Expr (SyntaxKind (Bit ni))):
Expr (SyntaxKind (Bit no)).
refine
match Compare_dec.lt_dec ni no with
| left isLt => castBits _ (@SignExtend ni (no - ni) e)
| right isGe => UniBit (TruncMsb (ni - no) no) (castBits _ e)
end; abstract Omega.omega.
Defined.
Fixpoint countLeadingZeros ni no: Expr (SyntaxKind (Bit ni)) -> Expr (SyntaxKind (Bit no)).
refine
match ni return Expr (SyntaxKind (Bit ni)) -> Expr (SyntaxKind (Bit no)) with
| 0 => fun _ => Const (wzero _)
| S m => fun e =>
ITE (Eq (UniBit (TruncMsb m 1) (castBits (eq_sym (Nat.add_1_r m)) e)) (Const WO~0))
(CABit Add [Const (natToWord _ 1);
countLeadingZeros m _ (UniBit (TruncLsb m 1) (castBits (eq_sym (Nat.add_1_r m)) e))])
(Const (wzero _))
end.
Defined.
Fixpoint sumSizes n: (Fin.t n -> nat) -> nat :=
match n return (Fin.t n -> nat) -> nat with
| 0 => fun _ => 0
| S m => fun sizes => sumSizes (fun x => sizes (Fin.FS x)) + sizes Fin.F1
end.
Fixpoint size (k: Kind) {struct k} :=
match k with
| Bool => 1
| Bit n => n
| Struct n fk fs =>
sumSizes (fun i => size (fk i))
| Array n k => n * size k
end.
(* ConstExtract: LSB, MIDDLE, MSB *)
(* Concat: MSB, LSB *)
Fixpoint concatStructExpr n {struct n}:
forall (sizes: Fin.t n -> nat)
(f: forall i, Expr (SyntaxKind (Bit (sizes i)))),
Expr (SyntaxKind (Bit (sumSizes sizes))) :=
match n return forall
(sizes: Fin.t n -> nat)
(f: forall i, Expr (SyntaxKind (Bit (sizes i)))),
Expr (SyntaxKind (Bit (sumSizes sizes))) with
| 0 => fun _ _ => Const WO
| S m => fun sizes f =>
BinBit
(Concat _ _) (f Fin.F1)
(@concatStructExpr m (fun x => (sizes (Fin.FS x))) (fun x => f (Fin.FS x)))
end.
Fixpoint pack (k: Kind): Expr (SyntaxKind k) -> Expr (SyntaxKind (Bit (size k))).
refine
match k return Expr (SyntaxKind k) -> Expr (SyntaxKind (Bit (size k))) with
| Bool => fun e => (ITE e (Const (WO~1)%word) (Const (WO~0)%word))
| Bit n => fun e => e
| Struct n fk fs =>
fun e =>
concatStructExpr (fun i => size (fk i))
(fun i => @pack (fk i) (ReadStruct e i))
| Array n k =>
fun e =>
(fix help i :=
match i return Expr (SyntaxKind (Bit (i * size k))) with
| 0 => Const WO
| S m =>
castBits _ (BinBit
(Concat (size k) (m * size k))
(@pack k (ReadArray e (Const (natToWord (Nat.log2_up n) m))))
(help m))
end) n
end; abstract lia.
Defined.
Fixpoint sumSizesMsbs n (i: Fin.t n) {struct i}: (Fin.t n -> nat) -> nat :=
match i in Fin.t n return (Fin.t n -> nat) -> nat with
| Fin.F1 _ => fun _ => 0
| Fin.FS m f => fun sizes => sumSizesMsbs f (fun j => sizes (Fin.FS j)) + sizes Fin.F1
end.
Lemma helper_sumSizes n (i: Fin.t n):
forall (sizes: Fin.t n -> nat), sumSizes sizes = (sumSizes sizes - (sumSizesMsbs i sizes + sizes i)) + sizes i + sumSizesMsbs i sizes.
Proof.
induction i; simpl; intros; auto.
- lia.
- specialize (IHi (fun x => sizes (Fin.FS x))).
lia.
Qed.
Lemma helper_array n (i: Fin.t n):
forall size_k,
n * size_k = (proj1_sig (Fin.to_nat i) * size_k) + size_k + (n * size_k - ((proj1_sig (Fin.to_nat i) * size_k) + size_k)) .
Proof.
induction i; simpl; intros; auto.
- lia.
- case_eq (Fin.to_nat i); simpl; intros.
rewrite H in *; simpl in *.
rewrite IHi at 1.
lia.
Qed.
Fixpoint unpack (k: Kind): Expr (SyntaxKind (Bit (size k))) -> Expr (SyntaxKind k) :=
match k return Expr (SyntaxKind (Bit (size k))) -> Expr (SyntaxKind k) with
| Bool => fun e => Eq e (Const (WO~1)%word)
| Bit _ => fun e => e
| Struct n fk fs =>
fun e => BuildStruct
_ _
(fun i =>
unpack
_
(ConstExtract
_ _ (sumSizesMsbs i (fun j => size (fk j)))
(@castBits _ _ (helper_sumSizes i (fun j => size (fk j))) e)))
| Array n k =>
fun e =>
BuildArray
(fun i => unpack _ (ConstExtract (proj1_sig (Fin.to_nat i) * size k) _ _
(@castBits _ _ (helper_array _ _) e)))
end.
End BitOps.
Inductive BitFormat :=
| Binary
| Decimal
| Hex.
Definition FullBitFormat := (nat * BitFormat)%type.
Inductive FullFormat: Kind -> Type :=
| FBool: nat -> BitFormat -> FullFormat Bool
| FBit n: nat -> BitFormat -> FullFormat (Bit n)
| FStruct n fk fs: (forall i, FullFormat (fk i)) -> FullFormat (@Struct n fk fs)
| FArray n k: FullFormat k -> FullFormat (@Array n k).
Fixpoint fullFormatHex k : FullFormat k :=
match k return FullFormat k with
| Bool => FBool 1 Hex
| Bit n => FBit n ((n+3)/4) Hex
| Struct n fk fs => FStruct fk fs (fun i => fullFormatHex (fk i))
| Array n k => FArray n (fullFormatHex k)
end.
Fixpoint fullFormatBinary k : FullFormat k :=
match k return FullFormat k with
| Bool => FBool 1 Binary
| Bit n => FBit n n Binary
| Struct n fk fs => FStruct fk fs (fun i => fullFormatBinary (fk i))
| Array n k => FArray n (fullFormatBinary k)
end.
Fixpoint fullFormatDecimal k : FullFormat k :=
match k return FullFormat k with
| Bool => FBool 1 Decimal
| Bit n => FBit n 0 Decimal
| Struct n fk fs => FStruct fk fs (fun i => fullFormatDecimal (fk i))
| Array n k => FArray n (fullFormatDecimal k)
end.
Inductive SysT: Type :=
| DispString (s: string): SysT
| DispExpr k (e: Expr (SyntaxKind k)) (ff: FullFormat k): SysT
| Finish: SysT.
Definition DispHex k (e: Expr (SyntaxKind k)) :=
DispExpr e (fullFormatHex k).
Definition DispBinary k (e: Expr (SyntaxKind k)) :=
DispExpr e (fullFormatBinary k).
Definition DispDecimal k (e: Expr (SyntaxKind k)) :=
DispExpr e (fullFormatDecimal k).
Inductive LetExprSyntax k :=
| NormExpr (e: Expr (SyntaxKind k)): LetExprSyntax k
| SysE (ls: list SysT) (e: LetExprSyntax k): LetExprSyntax k
| LetE k' (e: LetExprSyntax k') (cont: ty k' -> LetExprSyntax k): LetExprSyntax k
| IfElseE (pred: Expr (SyntaxKind Bool)) k' (t f: LetExprSyntax k') (cont: ty k' -> LetExprSyntax k):
LetExprSyntax k.
Inductive ActionT (lretT: Kind) : Type :=
| MCall (meth: string) s:
Expr (SyntaxKind (fst s)) ->
(ty (snd s) -> ActionT lretT) ->
ActionT lretT
| LetExpr k: Expr k -> (fullType k -> ActionT lretT) -> ActionT lretT
| LetAction k: ActionT k -> (ty k -> ActionT lretT) -> ActionT lretT
| ReadNondet k: (fullType k -> ActionT lretT) -> ActionT lretT
| ReadReg (r: string) k: (fullType k -> ActionT lretT) -> ActionT lretT
| WriteReg (r: string) k:
Expr k -> ActionT lretT -> ActionT lretT
| IfElse: Expr (SyntaxKind Bool) -> forall k,
ActionT k ->
ActionT k ->
(ty k -> ActionT lretT) ->
ActionT lretT
| Sys: list SysT -> ActionT lretT -> ActionT lretT
| Return: Expr (SyntaxKind lretT) -> ActionT lretT.
Fixpoint convertLetExprSyntax_ActionT k (e: LetExprSyntax k) :=
match e in LetExprSyntax _ return ActionT k with
| NormExpr e' => Return e'
| LetE _ e' cont => LetAction (convertLetExprSyntax_ActionT e') (fun v => convertLetExprSyntax_ActionT (cont v))
| SysE ls cont => Sys ls (convertLetExprSyntax_ActionT cont)
| IfElseE pred k' t f cont => IfElse pred (convertLetExprSyntax_ActionT t)
(convertLetExprSyntax_ActionT f)
(fun v => convertLetExprSyntax_ActionT (cont v))
end.
End Phoas.
Definition Action (retTy : Kind) := forall ty, ActionT ty retTy.
Definition Signature := (Kind * Kind)%type.
Definition MethodT (sig : Signature) := forall ty, ty (fst sig) -> ActionT ty (snd sig).
Notation Void := (Bit 0).
Notation Attribute A := (string * A)%type (only parsing).
Section RegInitValT.
Variable x: FullKind.
Definition RegInitValT := option (ConstFullT x).
End RegInitValT.
Definition RegInitT := Attribute (sigT RegInitValT).
Definition DefMethT := Attribute (sigT MethodT).
Definition RuleT := Attribute (Action Void).
Inductive RegFileInitT (IdxNum: nat) (Data: Kind) :=
| RFNonFile (init: option (ConstT Data))
| RFFile (isAscii: bool) (isArg: bool) (file: string) (offset size: nat) (init: Fin.t IdxNum -> ConstT Data).
Record SyncRead := { readReqName : string ;
readResName : string ;
readRegName : string }.
Inductive RegFileReaders :=
| Async (reads: list string)
| Sync (isAddr: bool) (reads: list SyncRead).
Record RegFileBase := { rfIsWrMask : bool ;
rfNum: nat ;
rfDataArray: string ;
rfRead: RegFileReaders ;
rfWrite: string ;
rfIdxNum: nat ;
rfData: Kind ;
rfInit: RegFileInitT rfIdxNum rfData }.
Inductive BaseModule: Type :=
| BaseRegFile (rf: RegFileBase)
| BaseMod (regs: list RegInitT) (rules: list RuleT) (dms: list DefMethT).
Inductive Mod: Type :=
| Base (m: BaseModule): Mod
| HideMeth (m: Mod) (meth: string): Mod
| ConcatMod (m1 m2: Mod): Mod.
Coercion Base: BaseModule >-> Mod.
Notation getKindAttr ls := (map (fun x => (fst x, projT1 (snd x))) ls).
Definition getRegFileRegisters m :=
match m with
| @Build_RegFileBase isWrMask num dataArray readers write IdxNum Data init =>
(dataArray, existT RegInitValT (SyntaxKind (Array IdxNum Data))
match init with
| RFNonFile x => match x with
| None => None
| Some init' => Some (SyntaxConst (ConstArray (fun _ => init')))
end
| RFFile isAscii isArg file offset size init => Some (SyntaxConst (ConstArray init))
end) :: match readers with
| Async _ => nil
| Sync isAddr read =>
if isAddr
then map (fun x => (readRegName x, existT RegInitValT (SyntaxKind (Bit (Nat.log2_up IdxNum)))
None)) read
else map (fun x => (readRegName x, existT RegInitValT (SyntaxKind (Array num Data)) None)) read
end
end.
Definition getRegisters m :=
match m with
| BaseRegFile rf => getRegFileRegisters rf
| BaseMod regs rules dms => regs
end.
Fixpoint getRules m :=
match m with
| BaseRegFile rf => nil
| BaseMod regs rules dms => rules
end.
Definition getStruct ls :=
(Struct (fun i => snd (nth_Fin ls i)) (fun j => fst (nth_Fin ls j))).
Arguments getStruct : simpl never.
Definition getStructVal ty ls :=
(BuildStruct (fun i => snd (nth_Fin (map (@projT1 _ _) ls) i))
(fun j => fst (nth_Fin (map (@projT1 _ _) ls) j))
(fun k => nth_Fin_map2 (@projT1 _ _) (fun x => Expr ty (SyntaxKind (snd x)))
ls k (projT2 (nth_Fin ls (Fin.cast k (map_length_red (@projT1 _ _) ls)))))).
Arguments getStructVal : simpl never.
Definition getStructConst ls :=
(ConstStruct (fun i => snd (nth_Fin (map (@projT1 _ _) ls) i))
(fun j => fst (nth_Fin (map (@projT1 _ _) ls) j))
(fun k => nth_Fin_map2 (@projT1 _ _) (fun x => ConstT (snd x))
ls k (projT2 (nth_Fin ls (Fin.cast k (map_length_red (@projT1 _ _) ls)))))).
Arguments getStructConst : simpl never.
Definition WriteRq lgIdxNum Data := (getStruct (cons ("addr", Bit lgIdxNum)
(cons ("data", Data) nil))).
(* STRUCT_TYPE { "addr" :: Bit lgIdxNum ; *)
(* "data" :: Data }. *)
Definition WriteRqMask lgIdxNum num Data := (getStruct (cons ("addr", Bit lgIdxNum)
(cons ("data", Array num Data)
(cons ("mask", Array num Bool)
nil)))).
(* Definition WriteRqMask lgIdxNum num Data := STRUCT_TYPE { "addr" :: Bit lgIdxNum ; *)
(* "data" :: Array num Data ; *)
(* "mask" :: Array num Bool }. *)
Definition buildNumDataArray num dataArray IdxNum Data ty (idx: ty (Bit (Nat.log2_up IdxNum))) :=
ReadReg dataArray (SyntaxKind (Array IdxNum Data))
(fun val =>
Return (BuildArray (fun i: Fin.t num =>
ReadArray
(Var ty _ val)
(CABit Add (Var ty (SyntaxKind _) idx ::
Const ty (natToWord _ (proj1_sig (Fin.to_nat i))) :: nil))))).
Definition updateNumDataArray num dataArray IdxNum Data ty (idxData: ty (WriteRq (Nat.log2_up IdxNum)
(Array num Data))):
ActionT ty Void :=
ReadReg dataArray (SyntaxKind (Array IdxNum Data))
(fun val =>
WriteReg dataArray
(fold_left (fun newArr i =>
(UpdateArray newArr
(CABit Add (ReadStruct (Var ty (SyntaxKind _) idxData)
Fin.F1 ::
Const ty (natToWord _ (proj1_sig (Fin.to_nat i))) ::
nil))
(ReadArrayConst (ReadStruct (Var ty (SyntaxKind _) idxData)
(Fin.FS Fin.F1)) i))) (getFins num)
(Var ty (SyntaxKind (Array IdxNum Data)) val))
(Return (Const _ WO))).
Definition updateNumDataArrayMask num dataArray IdxNum Data ty (idxData: ty (WriteRqMask
(Nat.log2_up IdxNum) num Data)):
ActionT ty Void :=
ReadReg dataArray (SyntaxKind (Array IdxNum Data))
(fun val =>
WriteReg dataArray
(fold_left (fun newArr i =>
ITE
(ReadArrayConst (ReadStruct (Var ty (SyntaxKind _) idxData) (Fin.FS (Fin.FS Fin.F1))) i)
(UpdateArray newArr
(CABit Add (ReadStruct
(Var ty (SyntaxKind _) idxData)
Fin.F1 :: Const ty (natToWord _ (proj1_sig (Fin.to_nat i))) ::
nil))
(ReadArrayConst (ReadStruct (Var ty (SyntaxKind _) idxData)
(Fin.FS Fin.F1)) i))
newArr
) (getFins num)
(Var ty (SyntaxKind (Array IdxNum Data)) val))
(Return (Const _ WO))).
Definition readRegFile num dataArray (read: list string) IdxNum Data :=
(map (fun x => (x, existT MethodT (Bit (Nat.log2_up IdxNum), Array num Data)
(buildNumDataArray num dataArray IdxNum Data))) read).
Definition writeRegFileFn (isWrMask: bool) num dataArray (write: string) IdxNum Data :=
(write,
if isWrMask
then existT MethodT (WriteRqMask (Nat.log2_up IdxNum) num Data, Void)
(updateNumDataArrayMask num dataArray IdxNum Data)
else existT MethodT (WriteRq (Nat.log2_up IdxNum) (Array num Data), Void)
(updateNumDataArray num dataArray IdxNum Data)).
Definition readSyncRegFile (isAddr: bool) num dataArray (read: list SyncRead) IdxNum Data :=
if isAddr
then
((map (fun r =>
(readReqName r,
existT MethodT (Bit (Nat.log2_up IdxNum), Void)
(fun ty idx =>
WriteReg (readRegName r) (Var ty (SyntaxKind _) idx)
(Return (Const _ WO)))))) read)
++
(map (fun r =>
(readResName r,
existT MethodT (Void, Array num Data)
(fun ty _ =>
ReadReg (readRegName r) (SyntaxKind (Bit (Nat.log2_up IdxNum)))
(buildNumDataArray num dataArray IdxNum Data ty))))
read)
else
((map (fun r =>
(readReqName r,
existT MethodT (Bit (Nat.log2_up IdxNum), Void)
(fun ty idx =>
LetAction (buildNumDataArray num dataArray IdxNum Data ty idx)
(fun vals => WriteReg (readRegName r) (Var ty (SyntaxKind _) vals)
(Return (Const _ WO)))))) read)
++
(map (fun r =>
(readResName r,
existT MethodT (Void, Array num Data)
(fun ty x =>
ReadReg (readRegName r) (SyntaxKind (Array num Data))
(fun data =>
Return (Var ty (SyntaxKind (Array num Data)) data)))))
read)).
Definition getRegFileMethods m :=
match m with
| @Build_RegFileBase isWrMask num dataArray readers write IdxNum Data init =>
writeRegFileFn isWrMask num dataArray write IdxNum Data ::
match readers with
| Async read =>
readRegFile num dataArray read IdxNum Data
| Sync isAddr read =>
readSyncRegFile isAddr num dataArray read IdxNum Data
end
end.
Fixpoint getMethods m :=
match m with
| BaseRegFile rf => getRegFileMethods rf
| BaseMod regs rules dms => dms
end.
Fixpoint getAllRegisters m :=
match m with
| Base m' => getRegisters m'
| HideMeth m' s => getAllRegisters m'
| ConcatMod m1 m2 => getAllRegisters m1 ++ getAllRegisters m2
end.
Fixpoint getAllRules m :=
match m with
| Base m' => getRules m'
| HideMeth m' s => getAllRules m'
| ConcatMod m1 m2 => getAllRules m1 ++ getAllRules m2
end.
Fixpoint getAllMethods m :=
match m with
| Base m' => getMethods m'
| HideMeth m' s => getAllMethods m'
| ConcatMod m1 m2 => getAllMethods m1 ++ getAllMethods m2
end.
Fixpoint getHidden m :=
match m with
| Base _ => []
| ConcatMod m1 m2 => getHidden m1 ++ getHidden m2
| HideMeth m' s => s :: getHidden m'
end.
Section WfBaseMod.
Variable ty : Kind -> Type.
Section WfActionT.
Variable regs : list (string * {x : FullKind & RegInitValT x}).
Inductive WfActionT: forall lretT, ActionT ty lretT -> Prop :=
| WfMCall meth s e lretT c: (forall v, WfActionT (c v)) -> @WfActionT lretT (MCall meth s e c)
| WfLetExpr k (e: Expr ty k) lretT c: (forall v, WfActionT (c v)) -> @WfActionT lretT (LetExpr e c)
| WfLetAction k (a: ActionT ty k) lretT c: WfActionT a -> (forall v, WfActionT (c v)) -> @WfActionT lretT (LetAction a c)
| WfReadNondet k lretT c: (forall v, WfActionT (c v)) -> @WfActionT lretT (ReadNondet k c)
| WfReadReg r k lretT c: (forall v, WfActionT (c v)) -> In (r, k) (getKindAttr regs) ->
@WfActionT lretT (ReadReg r k c)
| WfWriteReg r k (e: Expr ty k) lretT c: WfActionT c -> In (r, k) (getKindAttr regs) ->
@WfActionT lretT (WriteReg r e c)
| WfIfElse p k (atrue: ActionT ty k) afalse lretT c: (forall v, WfActionT (c v)) -> WfActionT atrue ->
WfActionT afalse -> @WfActionT lretT (IfElse p atrue afalse c)
| WfSys ls lretT c: WfActionT c -> @WfActionT lretT (Sys ls c)
| WfReturn lretT e: @WfActionT lretT (Return e).
Definition lookup{K X} : (K -> K -> bool) -> K -> list (K * X) -> option X :=
fun eqbk key pairs => match List.find (fun p => eqbk key (fst p)) pairs with
| Some p => Some (snd p)
| None => None
end.
Lemma lookup_cons : forall K V (eqb : K -> K -> bool) k k' v (ps : list (K*V)), lookup eqb k ((k',v)::ps) =
if eqb k k' then Some v else lookup eqb k ps.
Proof.
intros.
unfold lookup.
unfold find.
simpl.
destruct (eqb k k'); auto.
Qed.
Fixpoint WfActionT_new{k}(a : ActionT ty k) : Prop :=
match a with
| MCall meth s e cont => forall x, WfActionT_new (cont x)
| LetExpr k e cont => forall x, WfActionT_new (cont x)
| LetAction k a cont => (WfActionT_new a /\ forall x, WfActionT_new (cont x))
| ReadNondet k cont => forall x, WfActionT_new (cont x)
| ReadReg r k' cont => match lookup String.eqb r regs with
| None => False
| Some (existT k'' _) => k' = k'' /\ forall x, WfActionT_new (cont x)
end
| WriteReg r k' e a => match lookup String.eqb r regs with
| None => False
| Some (existT k'' _) => k' = k'' /\ WfActionT_new a
end
| IfElse e k1 a1 a2 cont => (WfActionT_new a1 /\ WfActionT_new a2 /\ forall x, WfActionT_new (cont x))
| Sys _ a => WfActionT_new a
| Return _ => True
end.
Fixpoint WfRules(rules : list RuleT) :=
match rules with
| [] => True
| r::rs => WfActionT_new (snd r ty) /\ WfRules rs
end.
Fixpoint WfMeths(meths : list (string * {x : Signature & MethodT x})) :=
match meths with
| [] => True
| m::ms => (forall v, WfActionT_new (projT2 (snd m) ty v)) /\ WfMeths ms
end.
End WfActionT.
Definition WfBaseModule (m : BaseModule) :=
(forall rule, In rule (getRules m) -> WfActionT (getRegisters m) (snd rule ty)) /\
(forall meth, In meth (getMethods m) -> forall v, WfActionT (getRegisters m) (projT2 (snd meth) ty v)) /\
NoDup (map fst (getMethods m)) /\ NoDup (map fst (getRegisters m)) /\ NoDup (map fst (getRules m)).
Definition WfBaseModule_new(m : BaseModule) :=
(WfRules (getRegisters m) (getRules m)) /\
(WfMeths (getRegisters m) (getMethods m)) /\
(NoDup (map fst (getMethods m))) /\
(NoDup (map fst (getRegisters m))) /\
(NoDup (map fst (getRules m))).
Section WfActionT'.
Variable m : BaseModule.
Inductive WfActionT': forall lretT, ActionT type lretT -> Prop :=
| WfMCall' meth s e lretT c v: (WfActionT' (c v)) -> @WfActionT' lretT (MCall meth s e c)
| WfLetExpr' k (e: Expr type k) lretT c v: (WfActionT' (c v)) -> @WfActionT' lretT (LetExpr e c)
| WfLetAction' k (a: ActionT type k) lretT c v: WfActionT' a -> (WfActionT' (c v)) -> @WfActionT' lretT (LetAction a c)
| WfReadNondet' k lretT c v: (WfActionT' (c v)) -> @WfActionT' lretT (ReadNondet k c)
| WfReadReg' r k lretT c v: (WfActionT' (c v)) -> In (r, k) (getKindAttr (getRegisters m)) ->
@WfActionT' lretT (ReadReg r k c)
| WfWriteReg' r k (e: Expr type k) lretT c: WfActionT' c -> In (r, k) (getKindAttr (getRegisters m)) ->
@WfActionT' lretT (WriteReg r e c)
| WfIfElse' p k (atrue: ActionT type k) afalse lretT c v: (WfActionT' (c v)) -> WfActionT' atrue ->
WfActionT' afalse -> @WfActionT' lretT (IfElse p atrue afalse c)
| WfSys' ls lretT c: WfActionT' c -> @WfActionT' lretT (Sys ls c)
| WfReturn' lretT e: @WfActionT' lretT (Return e).
End WfActionT'.
End WfBaseMod.
Lemma WfLetExprSyntax k m (e: LetExprSyntax type k): WfActionT (getRegisters m) (convertLetExprSyntax_ActionT e).
Proof.
induction e; constructor; auto.
Qed.
Lemma WfLetExprSyntax_new k m (e: LetExprSyntax type k): WfActionT_new (getRegisters m) (convertLetExprSyntax_ActionT e).
Proof.
induction e; simpl; repeat split; auto.
Qed.
Section WfBaseModProofs.
Lemma In_getKindAttr : forall r k (regs : list (string * {x : FullKind & RegInitValT x})), In (r,k) (getKindAttr regs) -> In r (map fst regs).
Proof.
intros.
rewrite in_map_iff in H.
dest.
inv H.
apply in_map; auto.
Qed.
Lemma In_lookup : forall r k (regs : list (string * {x : FullKind & RegInitValT x})), NoDup (map fst regs) -> In (r,k) (getKindAttr regs) -> exists k' v, k = k' /\ lookup String.eqb r regs = Some (existT _ k' v).
Proof.
induction regs; intros.
- destruct H0.
- destruct H0.
+ destruct a.
destruct s0.
destruct r0.
* inversion H0.
exists x; eexists.
split.
** auto.
** unfold lookup; simpl.
rewrite String.eqb_refl.
reflexivity.
* inversion H0.
exists k; eexists.
split.
** auto.
** unfold lookup; simpl.
rewrite String.eqb_refl.
simpl.
reflexivity.
+ assert (NoDup (map fst regs)).
inversion H; auto.
destruct (IHregs H1 H0) as [k' [v [Hk' Hv]]].
exists k', v.
split.
* auto.
* destruct a.
destruct s0.
destruct r0.
** rewrite lookup_cons.
destruct (r =? s) eqn:G.
*** rewrite String.eqb_eq in G.
rewrite <- G in H.
inversion H.
elim H4.
eapply In_getKindAttr.
exact H0.
*** auto.
** rewrite lookup_cons.
destruct (r =? s) eqn:G.
*** rewrite String.eqb_eq in G.
rewrite <- G in H.
inversion H.
elim H4.
eapply In_getKindAttr.
exact H0.
*** auto.
Qed.
Lemma lookup_In : forall r k v regs, lookup String.eqb r (regs) = Some (existT RegInitValT k v) -> In (r,k) (getKindAttr regs).
Proof.
induction regs; intros.
- discriminate H.
- destruct a.
destruct s0.
rewrite lookup_cons in H.
+ destruct (r =? s) eqn:G.
* rewrite String.eqb_eq in G.
inversion H.
left; simpl; congruence.
* right.
apply IHregs.
auto.
Qed.
Lemma WfActionT_WfActionT_new{ty lret} : forall regs (a : ActionT ty lret), NoDup (map fst regs) -> WfActionT regs a -> WfActionT_new regs a.
Proof.
intros.
induction a; simpl; intros.
- apply H1.
inversion H0.
EqDep_subst.
apply H4.
- apply H1.
inversion H0.
EqDep_subst.
apply H4.
- inversion H0.
split.
+ apply IHa.
EqDep_subst.
auto.
+ EqDep_subst.
intro.
auto.
- inversion H0.
apply H1.
EqDep_subst.
auto.
- inversion H0.
unfold getRegisters in H7.
destruct (In_lookup _ _ _ H H7) as [k' [v [Hk Hv]]].
rewrite Hv.
split.
+ auto.
+ intro.
apply H1.
EqDep_subst.
apply H5.
- inversion H0.
unfold getRegisters in H7.
destruct (In_lookup _ _ _ H H7) as [k' [v [Hk Hv]]].
rewrite Hv.
split.
+ auto.
+ apply IHa.
EqDep_subst; auto.
- inversion H0.
repeat split.
+ apply IHa1.
EqDep_subst.
auto.
+ apply IHa2.
EqDep_subst.
auto.
+ intro; apply H1.
EqDep_subst.
apply H6.
- apply IHa.
inversion H0.
EqDep_subst.
auto.
- auto.
Qed.
Lemma wf_rules_In : forall ty regs rules, NoDup (map fst regs) -> (forall rule : RuleT, In rule rules -> WfActionT regs (snd rule ty)) -> WfRules ty regs rules.
Proof.
induction rules; intros.
- simpl; auto.
- simpl.
split.
+ eapply WfActionT_WfActionT_new.
* auto.
* apply H0; left; auto.
+ eapply IHrules.
* auto.
* intros.
apply H0.
right; auto.
Qed.
Lemma wf_meths_In : forall ty regs dms, NoDup (map fst regs) -> (forall (meth : string * {x : Signature & MethodT x}),
In meth dms -> forall v : ty (fst (projT1 (snd meth))), WfActionT regs (projT2 (snd meth) ty v)) -> WfMeths ty regs dms.
Proof.
induction dms; intros.
- simpl; auto.
- simpl.
split.
+ intro; eapply WfActionT_WfActionT_new; auto.
apply H0.
left; auto.
+ eapply IHdms.
* auto.
* intros.