-
Notifications
You must be signed in to change notification settings - Fork 0
/
petr4_to_hol4p4Script.sml
4175 lines (3846 loc) · 199 KB
/
petr4_to_hol4p4Script.sml
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
open HolKernel boolLib liteLib simpLib Parse bossLib;
val _ = new_theory "petr4_to_hol4p4";
open stringTheory listTheory ASCIInumbersTheory;
open parse_jsonTheory;
open p4Theory p4_auxTheory;
(* For EVAL *)
open ASCIInumbersLib;
(* Finds an element e obeying P in l, then returns a tuple of e and l with e removed *)
Definition FIND_EXTRACT_def:
FIND_EXTRACT P l =
INDEX_FIND 0 P l >>=
\ (j, e). SOME (e, (TAKE j l)++(DROP (j+1) l))
End
(* Like FIND_EXTRACT, but returns NONE if there's more than one element obeying P *)
Definition FIND_EXTRACT_ONE_def:
FIND_EXTRACT_ONE P l =
case INDEX_FIND 0 P l of
| SOME (j, e) =>
let l' = (TAKE j l)++(DROP (j+1) l) in
(case INDEX_FIND 0 P l' of
| SOME (k, e') => NONE
| NONE => SOME (e, l'))
| NONE => NONE
End
Definition opt_pair_def:
(opt_pair (SOME x) (SOME y) = SOME (x, y)) /\
(opt_pair _ _ = NONE)
End
(* For passing string lists to print in strings *)
Definition merge_string_list'_def:
(merge_string_list' [] = "]") /\
(merge_string_list' (h::(h'::t)) =
h++("; "++(merge_string_list' (h'::t)))) /\
(merge_string_list' (h::t) =
h++(merge_string_list' t))
End
Definition merge_string_list_def:
merge_string_list string_list = ("["++(merge_string_list' string_list))
End
(* Option datatype with error message for the NONE case *)
Datatype:
option_msg_t =
SOME_msg 'a
| NONE_msg string
End
(* Also with debug term *)
Datatype:
option_debug_msg_t =
SOME_dbg 'a
| NONE_dbg 'b string
End
Definition opt_add_msg_def:
opt_add_msg msg opt =
case opt of
| SOME x => SOME_msg x
| NONE => NONE_msg msg
End
Definition msg_add_dbg_def:
msg_add_dbg data opt =
case opt of
| SOME_msg x => SOME_dbg x
| NONE_msg msg => NONE_dbg data msg
End
Definition dbg_remove_def:
dbg_remove opt =
case opt of
| SOME_dbg x => SOME_msg x
| NONE_dbg data msg => NONE_msg msg
End
Definition opt_msg_bind_def:
(opt_msg_bind (NONE_msg msg) f f' = NONE_msg $ f' msg) /\
(opt_msg_bind (SOME_msg x) f f' = f x)
End
(******************)
(* PRE-PROCESSING *)
Definition json_to_string_wrap_def:
json_to_string_wrap json = (FOLDL (\ str acc. str++acc) []) (json_to_string json)
End
Definition get_error_msg_def:
get_error_msg msg json = NONE_msg (msg++(json_to_string_wrap json))
End
Definition json_is_null_def:
json_is_null json =
case json of
| Null => T
| _ => F
End
Definition json_dest_str_def:
json_dest_str json =
case json of
| String s => SOME s
| _ => NONE
End
Definition json_dest_obj_def:
json_dest_obj json =
case json of
| Object obj => SOME obj
| _ => NONE
End
Definition json_dest_arr_def:
json_dest_arr json =
case json of
| Array arr => SOME arr
| _ => NONE
End
Definition json_parse_obj'_def:
(json_parse_obj' [] [] = SOME []) /\
(json_parse_obj' [] _ = NONE) /\
(json_parse_obj' _ [] = NONE) /\
(json_parse_obj' (h2::t2) ((k, v)::t) =
if k = h2
then
json_parse_obj' t2 t >>=
\vl. SOME (v::vl)
else NONE)
End
(* Returns a list of the JSON elements that are the
* values of the members of json iff the keys match
* the provided list of strings *)
Definition json_parse_obj_def:
json_parse_obj fields json =
json_dest_obj json >>=
json_parse_obj' fields
End
Definition json_parse_arr'_def:
(json_parse_arr' _ _ [] = NONE) /\
(json_parse_arr' name f (h::[t]) =
json_dest_str h >>=
\s. if s = name then f t else NONE) /\
(json_parse_arr' _ _ _ = NONE)
End
(* Returns the list of JSONs that is the tail of the list in json iff
* the first element is a String that matches the provided string.
* Note that this is not the general format of a JSON array, but rather
* a specific pattern found in petr4 JSON exports. *)
Definition json_parse_arr_def:
json_parse_arr name f json =
json_dest_arr json >>=
json_parse_arr' name f
End
Definition json_parse_arr_list'_def:
(json_parse_arr_list' _ [] = NONE) /\
(json_parse_arr_list' name_f_l (h::t) =
json_dest_str h >>=
\s. INDEX_FIND 0 (\name_f. (FST name_f) = s) name_f_l >>=
\ (i, name_f).
(* TODO: can t be empty if we're dealing with an error? *)
(case t of
| [] =>
(SND name_f) Null
| [t'] =>
(SND name_f) t'
| _ => NONE))
End
(* Parses the JSON element using one of the options in name_f_l, depending
* on the String value, which has to be the first element of the list *)
Definition json_parse_arr_list_def:
json_parse_arr_list name_f_l json =
case json of
| Array arr_elems => json_parse_arr_list' name_f_l arr_elems
| _ => NONE
End
(* TODO: Pre-parse JSON into alternative JSON format that has enumerated type for fields instead of strings to avoid excessive string matching? *)
(************************************)
(* PROGRAM TRANSFORMATION CONSTANTS *)
val apply_result_placeholder = “"gen_apply_result"”;
val apply_result_placeholder_varn = “varn_name ^apply_result_placeholder”;
val reserved_globals = “[^apply_result_placeholder_varn]”;
(***********************)
(* HOL4 JSON TO HOL4P4 *)
Definition p4_from_json_preamble:
p4_from_json_preamble json_parse_result =
case json_parse_result of
| INR (json, [], []) =>
(* petr4: all output is a list with an array at top *)
(case json_dest_arr json of
| SOME json_list =>
(* petr4: first element in this list is the string "program" *)
(case json_list of
| (json'::(json_list'::[])) =>
if json' = String "program"
then
(case json_dest_arr json_list' of
| SOME json_list'' => SOME_msg json_list''
| _ => NONE_msg "petr4 format error: Top-level List did not have Array as second element")
else NONE_msg "petr4 format error: Top level List did not have String \"program\" as first element"
| _ => NONE_msg "petr4 format error: Empty program")
| NONE => NONE_msg "petr4 format error: JSON was not a singleton list containing an Array at top level")
| _ => NONE_msg "output of HOL4 JSON parser did not have expected format"
End
(* LHS of the initial architectural-level reduction step:
* actx: Contains (ab_list, pblock_map, ffblock_map, input_f, output_f, ty_map, ext_map, func_map)
* ab_list: Programmable block "calls" with arguments
* pblock_map: Can be built from petr4 output
* ffblock_map: Cannot be built from petr4 output
* input_f: Cannot be built from petr4 output
* output_f: Cannot be built from petr4 output
* ty_map: Can be built from petr4 output
* ext_map: Can be partially built from petr4 output (not extern implementations)
* func_map: Can be built from petr4 output
* aenv: Cannot be built from petr4 output
* g_scope_list: First element (with the global variables) can be built from petr4.
* arch_frame_list: Should always start as empty
* ctrl: Cannot be built from petr4 output
* status: Should always start as Running *)
(* Some types which are used throughout the .p4 parser which are not part of the HOL4P4 state or
* context *)
val _ = type_abbrev("tyenv", ``:(string, p_tau) alist``);
val _ = type_abbrev("enummap", ``:(string # (string, v) alist) list``);
val _ = type_abbrev("ftymap", ``:((funn, (p_tau list # p_tau)) alist)``);
(* pblock and tbl_map with list of actions stored *)
val _ = type_abbrev("tbl_map_extra", ``:((string, ((mk list) # x_list # (x # e_list))) alist)``);
val _ = type_abbrev("pblock_extra", ``:(pbl_type # ((string # d) list) # b_func_map # t_scope # pars_map # tbl_map_extra)``);
(* The tau signifies the type argument(s) *)
Datatype:
vss_pkg_t =
vss_pkg_VSS tau
End
Datatype:
ebpf_pkg_t =
ebpf_pkg_ebpfFilter tau
End
Datatype:
v1model_pkg_t =
v1model_pkg_V1Switch tau tau
End
(* The option type signifies whether the top-level package has been recognized yet *)
Datatype:
arch_t =
arch_vss (vss_pkg_t option)
| arch_ebpf (ebpf_pkg_t option)
| arch_v1model (v1model_pkg_t option)
End
(********************)
(* Type definitions *)
Definition petr4_parse_width_def:
petr4_parse_width json_list =
case json_list of
| (width::(null::[])) =>
json_dest_str width >>=
\width_res.
if json_is_null null
then
fromDecString width_res >>=
\w_num. SOME (p_tau_bit w_num)
else NONE
| _ => NONE
End
Definition petr4_deparse_name_def:
petr4_deparse_name name_str =
Object [("tags",Array []); ("string",String name_str)]
End
Definition petr4_parse_name_def:
petr4_parse_name name_obj =
case json_parse_obj ["tags"; "string"] name_obj of
| SOME [tags; name] =>
json_dest_str name
| _ => NONE
End
(* Different one-off format for switch cases *)
Definition petr4_parse_case_name_def:
petr4_parse_case_name case_name =
json_parse_arr "name" SOME case_name >>=
\name_obj.
(case json_parse_obj ["tags"; "name"] name_obj of
| SOME [tags'; name'] =>
(case json_parse_obj ["tags"; "string"] name' of
| SOME [tags''; name''] =>
json_dest_str name''
| _ => NONE)
| _ => NONE)
End
Definition petr4_parse_names_def:
(petr4_parse_names [] = SOME []) /\
(petr4_parse_names (h::t) =
petr4_parse_name h >>=
\res_hd. petr4_parse_names t >>=
\res_tl. SOME (res_hd::res_tl))
End
Definition petr4_parse_bare_name_def:
petr4_parse_bare_name bname =
json_parse_arr "BareName" SOME bname >>=
\bname_obj.
(case json_parse_obj ["tags"; "name"] bname_obj of
| SOME [tags; name] => petr4_parse_name name
| _ => NONE)
End
(* Parses a named type *)
(* TODO: This is currently used also to parse variable names... *)
Definition petr4_parse_tyname_def:
petr4_parse_tyname json =
case json_parse_obj ["tags"; "name"] json of
| SOME [tags; name] =>
petr4_parse_bare_name name
| _ => NONE
End
(* Parses a type to a string, should return NONE if type is not a named type *)
Definition petr4_parse_type_name_def:
petr4_parse_type_name json =
json_parse_arr_list
[("name", petr4_parse_tyname);
("specialized", \json'.
(case json_parse_obj ["tags"; "base"; "args"] json' of
| SOME [tags; base; args] =>
(json_parse_arr "name" petr4_parse_tyname base)
| _ => NONE));] json
End
Definition petr4_num_binop_lookup_def:
petr4_num_binop_lookup optype (op1, op2) =
ALOOKUP [("Plus", op1 + op2);
("Minus", op1 - op2);
("Mul", op1 * op2);
("Div", op1 DIV op2);
("Mod", op1 MOD op2)] optype
End
Triviality get_error_msg_distinct:
!str obj str'. get_error_msg str obj <> SOME_msg str'
Proof
fs[get_error_msg_def]
QED
Triviality json_parse_arr_size:
!str json json'.
json_parse_arr str SOME json = SOME json' ==>
json_size json' < json_size json
Proof
rpt strip_tac >>
fs[json_parse_arr_def, app_opt_def] >>
Cases_on ‘json’ >> (fs[json_dest_arr_def]) >>
rw[] >>
Cases_on ‘l’ >> (fs[json_parse_arr'_def]) >>
Cases_on ‘t’ >> (fs[json_parse_arr'_def]) >>
Cases_on ‘t'’ >> (fs[json_parse_arr'_def, json_size_def, app_opt_def])
QED
Triviality json_parse_obj_size:
!str_list json json_list.
json_parse_obj str_list json = SOME json_list ==>
json3_size json_list < json_size json
Proof
Induct_on ‘json_list’ >- (
rpt strip_tac >>
fs[json_parse_obj_def, json_dest_obj_def, app_opt_def] >>
Cases_on ‘json’ >> (fs[json_size_def])
) >>
rpt strip_tac >>
fs[json_parse_obj_def, app_opt_def] >>
Cases_on ‘json’ >> (fs[json_dest_obj_def]) >>
rw[] >>
Cases_on ‘str_list’ >> (fs[json_parse_obj'_def, json_size_def]) >>
(Cases_on ‘l’ >> (fs[json_parse_obj'_def, json_size_def])) >>
Cases_on ‘h''’ >> (fs[json_parse_obj'_def, json_size_def, app_opt_def]) >>
Cases_on ‘json_parse_obj' t t'’ >> (fs[json_parse_obj'_def, json_size_def]) >>
rw[] >>
subgoal ‘(case (Object t') of
Object obj => SOME obj
| Array v8 => NONE
| String v9 => NONE
| Number v10 v11 v12 => NONE
| Bool v13 => NONE
| Null => NONE) =
SOME t'’ >- (
fs[]
) >>
subgoal ‘json3_size json_list < json_size (Object t')’ >- (
metis_tac[]
) >>
fs[json_size_def]
QED
(* Parses compile-time known constants, e.g. in bitstring widths *)
(* TODO: Should the return type be option_msg? *)
Definition petr4_parse_compiletime_constantexp_def:
petr4_parse_compiletime_constantexp exp =
case json_dest_arr exp of
| SOME [exptype_str; x_obj] =>
json_dest_str exptype_str >>=
\exptype.
if exptype = "int"
then
(case json_parse_obj ["tags"; "x"] x_obj of
| SOME [tags; x] =>
(case json_parse_obj ["tags"; "value"; "width_signed"] x of
| SOME [tags'; value_str; width_signed] =>
json_dest_str value_str >>=
\value.
(case width_signed of
| Null => fromDecString value
(* TODO: Not sure if this can happen here... *)
| Array [Number (Positive, width) NONE NONE; Bool F] =>
fromDecString value
| _ => NONE)
| _ => NONE)
| _ => NONE)
else if exptype = "binary_op"
then (* Binary operation *)
(case json_parse_obj ["tags"; "op"; "args"] x_obj of
| SOME [tags; op; args] =>
(case json_dest_arr op of
| SOME [optype_str; op_tags] =>
json_dest_str optype_str >>=
\optype.
(case json_dest_arr args of
| SOME [op1; op2] =>
petr4_parse_compiletime_constantexp op1 >>=
\res_op1. petr4_parse_compiletime_constantexp op2 >>=
(* TODO: Treat comparisons, bit shift+concat and regular binops differently *)
\res_op2. petr4_num_binop_lookup optype (res_op1, res_op2)
| _ => NONE)
| _ => NONE)
| _ => NONE)
else NONE
| _ => NONE
Termination
WF_REL_TAC `measure json_size` >>
rpt strip_tac >> (
Cases_on ‘exp’ >> (fs[json_dest_arr_def, json_size_def]) >>
Cases_on ‘op’ >> (fs[]) >>
Cases_on ‘args’ >> (fs[]) >>
rw[] >>
imp_res_tac json_parse_obj_size >>
fs[json_size_def]
)
End
(* TODO: Expand as you encounter more types *)
(* If type specialisation is ignored via "ignore_tyspec", type-specialised types
* will be treated as their base type: this will result in p_tau_par.
* This is used e.g. when parsing package types, when only the base type of the
* parameter is relevant.
* Since type-parameterised extern types are currently not treated, all extern
* types will be plain. *)
Definition petr4_parse_ptype_def:
petr4_parse_ptype ignore_tyspec tyenv json =
json_parse_arr_list
[("bit", \json'.
(case json_parse_obj ["tags"; "expr"] json' of
| SOME [tags; expr] =>
petr4_parse_compiletime_constantexp expr >>=
\n. SOME (p_tau_bit n)
| _ => NONE));
("bool", \json'. SOME p_tau_bool);
("error", \json'. SOME (p_tau_bit 32));
("void", \json'. SOME p_tau_bot);
("name", \json'. petr4_parse_tyname json' >>=
(\name. case ALOOKUP tyenv name of
| SOME p_tau => SOME p_tau
| NONE => SOME (p_tau_par name)));
(* Note. It's OK to map the string type to p_tau_bot, since we never want to
* do type inference in expressions of this type. *)
("string", \json'. SOME p_tau_bot);
("specialized", \json'.
(case json_parse_obj ["tags"; "base"; "args"] json' of
| SOME [tags; base; args] =>
petr4_parse_type_name base >>=
\name.
(case ALOOKUP tyenv name of
| SOME (p_tau_ext ext_name) =>
SOME (p_tau_ext ext_name)
| _ =>
if ignore_tyspec
then SOME (p_tau_par name)
else NONE)
| _ => NONE))] json
End
(* Version for non-parameterized types *)
Definition petr4_parse_type_def:
petr4_parse_type tyenv json =
petr4_parse_ptype F tyenv json >>=
\p_tau. deparameterise_tau p_tau
End
(* TODO: Avoid explicit case matching on list possible? *)
Definition petr4_typedef_to_tyenvupdate_def:
petr4_typedef_to_tyenvupdate tyenv json =
case json_parse_obj ["tags"; "annotations"; "name"; "typ_or_decl"] json of
| SOME [tags; annot; ty_name; typ_or_decl] =>
opt_pair
(petr4_parse_name ty_name)
(json_parse_arr "Left" SOME typ_or_decl >>=
petr4_parse_ptype F tyenv)
| _ => NONE
End
Definition petr4_parse_typedef_def:
petr4_parse_typedef tyenv json =
case petr4_typedef_to_tyenvupdate tyenv json of
| SOME (ty_name, tau) =>
SOME_msg (AUPDATE tyenv (ty_name, tau))
| NONE => NONE_msg "Could not parse type definition"
End
(*********************)
(* Enumeration types *)
Definition COUNT_LIST_interval_def:
COUNT_LIST_interval n m =
DROP n (COUNT_LIST (n+m))
End
Definition get_32bitv_def:
get_32bitv n =
v_bit (fixwidth 32 (n2v n), 32)
End
(* Note that currently, if any error or match_kind enumeration members are
* re-declared, they get a new number *)
(* TODO: Fix code duplication *)
Definition petr4_enum_to_enummapupdates_def:
petr4_enum_to_enummapupdates enummap name members =
case petr4_parse_name name of
| SOME enum_name =>
if (enum_name = "error") \/ (enum_name = "match_kind")
then
(case petr4_parse_names members of
| SOME enum_members =>
(case ALOOKUP enummap enum_name of
| SOME enum_mem_map =>
SOME (AUPDATE enummap (enum_name,
AUPDATE_LIST enum_mem_map (ZIP (enum_members,
MAP get_32bitv (COUNT_LIST_interval (LENGTH enum_mem_map) (LENGTH enum_members))))))
| NONE =>
SOME (AUPDATE enummap (enum_name,
AUPDATE_LIST [] (ZIP (enum_members,
MAP get_32bitv (COUNT_LIST_interval 0 (LENGTH enum_members)))))))
| NONE => NONE)
else if (ALOOKUP enummap enum_name = NONE)
then
(case petr4_parse_names members of
| SOME enum_members =>
SOME (AUPDATE enummap (enum_name,
AUPDATE_LIST [] (ZIP (enum_members,
MAP get_32bitv (COUNT_LIST_interval 0 (LENGTH enum_members))))))
| NONE => NONE)
else NONE
| NONE => NONE
End
(* Note: enummap updated directly here *)
Definition petr4_parse_enum_def:
petr4_parse_enum enummap json =
case json_parse_obj ["tags"; "annotations"; "name"; "members"] json of
| SOME [tags; annot; name; Array members] =>
(case petr4_enum_to_enummapupdates enummap name members of
| SOME enummap' => SOME_msg enummap'
| NONE => get_error_msg "Could not parse enumeration type: " json)
| _ => get_error_msg "Unknown JSON format of enumeration type: " json
End
Definition petr4_parse_error_def:
petr4_parse_error enummap json =
case json_parse_obj ["tags"; "members"] json of
| SOME [tags; Array members] =>
(case petr4_enum_to_enummapupdates enummap (petr4_deparse_name "error") members of
| SOME enummap' =>
SOME_msg enummap'
| NONE => get_error_msg "Could not parse error type: " json)
| _ => get_error_msg "Unknown JSON format of error type: " json
End
Definition petr4_parse_match_kind_typedef_def:
petr4_parse_match_kind_typedef enummap json =
case json_parse_obj ["tags"; "members"] json of
| SOME [tags; Array members] =>
(case petr4_enum_to_enummapupdates enummap (petr4_deparse_name "match_kind") members of
| SOME enummap' =>
SOME_msg enummap'
| NONE => get_error_msg "Could not parse match kind type: " json)
| _ => get_error_msg "Unknown JSON format of match kind type: " json
End
(***********************)
(* Common: expressions *)
Definition width_of_tau_def:
width_of_tau tau =
case tau of
| (tau_bit w) => SOME w
| _ => NONE
End
Definition width_of_p_tau_def:
width_of_p_tau p_tau =
case p_tau of
| (p_tau_bit w) => SOME w
| _ => NONE
End
Definition n_of_e_bitv_def:
n_of_e_bitv e =
case e of
| (e_v v) =>
(case v of
| (v_bit (bl, n)) =>
SOME (v2n bl)
| _ => NONE)
| _ => NONE
End
(* TODO: More types of expressions *)
(* TODO: vtymap uses varn_name to later use varn_star for case of function call *)
Definition exp_to_p_tau_def:
exp_to_p_tau (vtymap, ftymap) exp =
case exp of
| (e_v $ v_bool b) => SOME p_tau_bool
| (e_v (v_bit (bl, n))) => SOME (p_tau_bit n)
| (e_acc struct fld) =>
(case exp_to_p_tau (vtymap, ftymap) struct of
| SOME (p_tau_xtl struct_ty f_t_list) =>
(FIND (\ (f, t). f = fld) f_t_list) >>=
\ (fld, res_tau). SOME res_tau
| _ => NONE)
| (e_cast cast e') =>
(case cast of
| cast_unsigned n => SOME (p_tau_bit n)
| cast_bool => SOME p_tau_bool)
| (e_var (varn_name varname)) => ALOOKUP vtymap (varn_name varname)
| (e_binop op1 binop op2) => exp_to_p_tau (vtymap, ftymap) op1
| (e_slice e hi lo) =>
n_of_e_bitv hi >>=
\n. n_of_e_bitv lo >>=
\n'. SOME (p_tau_bit ((n - n') + 1))
| (e_call funn e_l) =>
(* Lookahead exception: second arg is dummy type argument *)
if funn = (funn_ext "packet_in" "lookahead")
then
(case e_l of
| [e1;e2] => exp_to_p_tau (vtymap, ftymap) e2
| _ => NONE)
else
(ALOOKUP ftymap funn >>=
\ftys. SOME $ SND ftys)
| _ => NONE
End
(* TODO: Use list option monad *)
Definition exps_to_p_taus_def:
(exps_to_p_taus (vtymap, ftymap) [] = SOME []) /\
(exps_to_p_taus (vtymap, ftymap) (h::t) =
exp_to_p_tau (vtymap, ftymap) h >>=
\p_tau. exps_to_p_taus (vtymap, ftymap) t >>=
\res. SOME (p_tau::res))
End
Definition exp_to_funn_def:
exp_to_funn (vtymap, extfun_list) exp =
case exp of
(* Regular function call *)
| (e_var (varn_name name)) =>
if MEM name extfun_list
then SOME (SOME (funn_ext "" name), NONE)
else SOME (SOME (funn_name name), NONE)
(* Extern method call/validity manipulation *)
| (e_acc obj fun_name) =>
(* TODO: This is a hack, making exceptions for validity manipulations and "apply"...
* Need to check type of obj to see what methods exist *)
(if fun_name = "isValid" then
SOME (SOME (funn_ext "header" "isValid"), SOME obj)
else if fun_name = "setInvalid" then
SOME (SOME (funn_ext "header" "setInvalid"), SOME obj)
else if fun_name = "setValid" then
SOME (SOME (funn_ext "header" "setValid"), SOME obj)
else if fun_name = "apply" then
(* Apply is a statement, no function name needed *)
SOME (NONE, SOME obj)
else
(case obj of
(* TODO: Note that "ext_name" is here the name of the specific object, not the object type.
* In HOL4P4, this should be the extern object type and the object itself be the first
* argument to the function. *)
| (e_var (varn_name ext_name)) =>
(case ALOOKUP vtymap (varn_name ext_name) of
| SOME (p_tau_ext ext_tyname) =>
SOME (SOME (funn_ext ext_tyname fun_name), SOME obj)
| _ => NONE)
| _ => NONE))
| _ => NONE
End
Definition tyu_l_only_def:
(tyu_l_only [] = SOME []) /\
(tyu_l_only (h::t) =
case h of
| (INL a) =>
tyu_l_only t >>=
\l. SOME (a::l)
| _ => NONE)
End
Definition petr4_unop_lookup_def:
petr4_unop_lookup unop_str =
ALOOKUP [("Not", unop_neg);
("BitNot", unop_compl);
("UMinus", unop_neg_signed)] unop_str
End
(* TODO: Annoying special treatment of concat... *)
Definition petr4_binop_lookup_def:
petr4_binop_lookup binop_str =
if binop_str = "PlusPlus"
then SOME ( \op1 op2. (e_concat op1 op2))
else
case
(ALOOKUP [("Plus", binop_add);
("PlusSat", binop_sat_add);
("Minus", binop_sub);
("MinusSat", binop_sat_sub);
("Mul", binop_mul);
("Div", binop_div);
("Mod", binop_mod);
("Shl", binop_shl);
("Shr", binop_shr);
("Le", binop_le);
("Ge", binop_ge);
("Lt", binop_lt);
("Gt", binop_gt);
("Eq", binop_eq);
("NotEq", binop_neq);
("BitAnd", binop_and);
("BitXor", binop_xor);
("BitOr", binop_or);
("And", binop_bin_and);
("Or", binop_bin_or)] binop_str) of
| SOME binop => SOME ( \op1 op2. (e_binop op1 binop op2))
| NONE => NONE
End
(* Like ALOOKUP, but also requires the number of arguments to match
* Required due to overloaded function names, e.g. extract in packet_in *)
(* TODO: Should this really return a tuple and not only the args? Check where this is used... *)
Definition find_fty_match_args_def:
find_fty_match_args (ftymap:ftymap) funn numargs =
(case FIND (\ (funn', (tyargs', tyret')). if funn = funn' then (numargs = LENGTH tyargs') else F) ftymap of
| SOME fty => SOME (SND fty)
| NONE => NONE)
End
Triviality find_fty_match_args_LENGTH:
!ftymap funn numargs res.
find_fty_match_args ftymap funn numargs = SOME res ==>
LENGTH (FST res) = numargs
Proof
fs[find_fty_match_args_def] >>
rpt strip_tac >>
fs[FIND_def] >>
Cases_on ‘(INDEX_FIND 0
(\(funn',tyargs',tyret').
funn = funn' /\ numargs = LENGTH tyargs') ftymap)’ >> (fs[]) >- (
Cases_on ‘x’ >>
imp_res_tac index_find_first >>
PairCases_on ‘r’ >>
gvs[]
)
QED
Definition json_p_tau_opt_list_size_def:
json_p_tau_opt_list_size json_p_tau_opt_list =
let (json_list, p_tau_opt_list) = UNZIP json_p_tau_opt_list in
json3_size json_list
End
Datatype:
exp_res_t =
Number num
| Exp e
(* table.apply().action_run expression to be inlined *)
| InlineApp (string list) e
| SetExp s
End
Definition msg_opt_INL_def:
(msg_opt_INL (SOME_msg (e:e)) = (SOME_msg ((INL e):(e + num)))) /\
(msg_opt_INL (NONE_msg msg) = (NONE_msg msg))
End
(* This gathers all known extern functions which have type arguments that don't appear
* among their arguments. *)
(* TODO: This is architecture-dependent in general... *)
Definition incomplete_typeinf_def:
incomplete_typeinf funn =
case funn of
| (funn_ext ext_obj ext_fun) =>
(if ext_obj = "packet_in" /\ ext_fun = "lookahead"
then T
else F)
| _ => F
End
Definition get_typeinf_dummy_args_def:
get_typeinf_dummy_args tyenv tyargs =
case
FOLDL ( \ args_opt tyarg.
case args_opt of
| SOME args =>
petr4_parse_type tyenv tyarg >>=
\type. SOME (args++[(e_v $ arb_from_tau type)])
| NONE => NONE) (SOME []) tyargs of
| SOME dummy_args => SOME_msg dummy_args
| NONE => get_error_msg "could not transform extern function's type arguments to dummy arguments: " (Array tyargs)
End
Definition get_bitv_of_e_def:
get_bitv_of_e e =
case e of
| e_v v =>
(case v of
| v_bit bitv => SOME bitv
| _ => NONE)
| _ => NONE
End
(* TODO: Use OPTION_BIND, parse_arr and parse_obj *)
Definition petr4_parse_expression_gen_def:
(petr4_parse_expression_gen (tyenv, enummap, vtymap, ftymap, gscope, extfun_list) (exp, p_tau_opt) =
case exp of
(* TODO: Null can occur in case of return without value - works generally? *)
| Null => SOME_msg (Exp (e_v v_bot))
(* True and false *)
| Array [String "true";
Object [("tags", tags)]] =>
SOME_msg (Exp (e_v (v_bool T)))
| Array [String "false";
Object [("tags", tags)]] =>
SOME_msg (Exp (e_v (v_bool F)))
(* Struct member/field access *)
| Array [String "expression_member";
Object [("tags", tags);
("expr", nested_exp);
("name", name)]] =>
(case petr4_parse_expression_gen (tyenv, enummap, vtymap, ftymap, gscope, extfun_list) (nested_exp, NONE) of
| SOME_msg (Exp mem_nested_exp) =>
(case petr4_parse_name name of
| SOME mem_name =>
SOME_msg (Exp (e_acc mem_nested_exp mem_name))
| NONE => get_error_msg "could not parse name: " name)
(* Nested apply expression allowed for struct field access (action_run, et.c.)*)
| SOME_msg (InlineApp str_l e_app) =>
(case petr4_parse_name name of
| SOME mem_name =>
SOME_msg (InlineApp str_l (e_acc e_app mem_name))
| NONE => get_error_msg "could not parse name: " name)
| SOME_msg (Number n) => NONE_msg "cannot access field of constant"
| NONE_msg mem_msg => NONE_msg mem_msg)
(* Variable *)
| Array [String "name";
name] =>
(case petr4_parse_tyname name of
| SOME var_name =>
let varn = (varn_name var_name) in
(* If variable is multiply defined in vtymap, this occurrence is not referring
* to a global constant *)
if (LENGTH $ FILTER (\ (varn', ty). varn' = varn) vtymap) > 1
then SOME_msg (Exp (e_var varn))
else
(case ALOOKUP gscope varn of
| SOME (v, lval_opt) =>
SOME_msg (Exp (e_v v))
(* Not in gscope ==> not a global constant *)
| NONE => SOME_msg (Exp (e_var varn)))
| NONE => get_error_msg "could not parse name: " name)
(* Arbitrary-width integer literal + fixed-width (unsigned) integer *)
| Array [String "int";
Object [("tags", tags);
("x", x)]] =>
(case x of
| Object [("tags", tags2);
("value", String value_str);
("width_signed", width_signed)] =>
(case width_signed of
| Null =>
(case p_tau_opt of
| SOME p_tau =>
(case width_of_p_tau p_tau of
| SOME w =>
(case fromDecString value_str of
| SOME n => SOME_msg (Exp (e_v (v_bit (fixwidth w (n2v n), w))))
| NONE => NONE_msg ("could not parse string to integer: "++value_str))
| NONE =>
(case fromDecString value_str of
| SOME n => SOME_msg (Number n)
| NONE => NONE_msg ("could not parse string to integer after failing to obtain width of expression type: "++value_str)))
| NONE =>
(case fromDecString value_str of
| SOME n => SOME_msg (Number n)
| NONE => NONE_msg ("could not parse string to integer: "++value_str)))
| Array [Number (Positive, width) NONE NONE; Bool F] =>
(case fromDecString value_str of
| SOME n =>
(let bl = n2v n in
if LENGTH bl > width then NONE_msg ("integer overflows width: "++value_str)
else SOME_msg (Exp (e_v (v_bit (fixwidth width bl, width)))))
| NONE => NONE_msg ("could not parse string to integer: "++value_str))
| _ => get_error_msg "unsupported integer type: " width_signed)
| _ => get_error_msg "could not obtain value and width of integer literal: " x)
(* Cast *)
| Array [String "cast";
Object [("tags", tags);
("type", cast_type);
("expr", op)]] =>
(case petr4_parse_expression_gen (tyenv, enummap, vtymap, ftymap, gscope, extfun_list) (op, NONE) of
| SOME_msg res_op =>
(case petr4_parse_type tyenv cast_type of
| SOME (tau_bit n) =>
(case res_op of
| Exp op_exp =>
SOME_msg (Exp (e_cast (cast_unsigned n) op_exp))
| Number op_const =>
SOME_msg (Exp (e_v (v_bit (fixwidth n (n2v op_const), n))))
| InlineApp s_l app_exp =>
get_error_msg "apply expression unsupported for casts: " op
| SetExp set_exp =>
get_error_msg "apply expression unsupported for set expressions: " op)
| SOME tau_bool =>
(case res_op of
| Exp op_exp =>
SOME_msg (Exp (e_cast cast_bool op_exp))
| Number op_const =>
SOME_msg (Exp (e_v (v_bool (HD $ REVERSE (n2v op_const)))))
| InlineApp s_l app_exp =>
get_error_msg "apply expression unsupported for casts: " op
| SetExp set_exp =>
get_error_msg "apply expression unsupported for set expressions: " op)
| SOME _ => get_error_msg "unsupported cast type: " cast_type
| NONE => get_error_msg "unknown cast type: " cast_type)
| NONE_msg op_msg => NONE_msg op_msg)
(* Unary operation *)
| Array [String "unary_op";
Object [("tags", tags);
("op", Array [String optype; op_tags]);
("arg", op)]] =>
(* TODO: All unary operations preserve type? *)
(case petr4_parse_expression_gen (tyenv, enummap, vtymap, ftymap, gscope, extfun_list) (op, p_tau_opt) of
| SOME_msg res_op =>
(case petr4_unop_lookup optype of
| SOME unop =>
(case res_op of
| Exp op_exp =>
SOME_msg (Exp (e_unop unop op_exp))
| _ =>
(* TODO: Infer type directly from tau_opt *)
get_error_msg "type inference or apply expression unsupported for unops: " exp)
| NONE => NONE_msg ("unknown optype: "++optype))
| NONE_msg op_msg => NONE_msg op_msg)
(* Binary operation *)