-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathCStarToC11.ml
More file actions
1696 lines (1496 loc) · 61.3 KB
/
CStarToC11.ml
File metadata and controls
1696 lines (1496 loc) · 61.3 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
(* Copyright (c) INRIA and Microsoft Corporation. All rights reserved. *)
(* Licensed under the Apache 2.0 and MIT Licenses. *)
(** Converting from C* to C abstract syntax. *)
module C = C11
open C
open CStar
open KPrint
open Common
let is_primitive = Helpers.is_primitive
let zero = C.Constant (K.UInt8, "0")
let cmul x y =
match x, y with
| C.Constant (_, "1"), _ -> y
| _, C.Constant (_, "1") -> x
| _ -> C.Op2 (K.Mult, x, y)
let is_array = function Array _ -> true | _ -> false
let is_commutative : K.op -> bool = function
| Add | Mult
| BOr | BAnd | BXor -> true
| _ -> false
let is_compoundable : K.op -> bool = function
| Add | Sub | Mult | Div | Mod
| BOr | BAnd | BXor | BShiftL | BShiftR -> true
| _ -> false
(* returns true when expr is definitely pure *)
let rec is_pure : C11.expr -> bool = function
| Assign _ | CompoundAssign _ | Call _ | Stmt _
| Member _ | MemberP _ -> false (* Unsure what these are *)
| Name _ | Literal _ | Constant _ | Bool _
| Sizeof _ | CompoundLiteral _ | Type _
| CxxInitializerList _ -> true
| Op1 (_, e) -> is_pure e
| Op2 (_, e1, e2) -> is_pure e1 && is_pure e2
| Index (e1, e2) -> is_pure e1 && is_pure e2
| Deref e -> is_pure e
| Address e -> is_pure e
| Cast (_, e) -> is_pure e
| MemberAccess (e, _) -> is_pure e
| MemberAccessPointer (e, _) -> is_pure e
| InlineComment (_, e, _) -> is_pure e
| Ternary (e1, e2, e3) -> is_pure e1 && is_pure e2 && is_pure e3
(* Tries to make compound assignments when possible.
Assumptions:
- The result is always used in statement context (discarded value), so
PostIncr vs PreIncr does not matter.
C standard §6.5.16.2:
A compound assignment of the form E1 op = E2 is equivalent to the simple
assignment expression E1 = E1 op (E2), except that the lvalue E1 is
evaluated only once, and with respect to an indeterminately-sequenced
function call, the operation of a compound assignment is a single
evaluation *)
let mk_assign (lhs : C11.expr) (rhs : C11.expr) : C11.expr =
match rhs with
(* We don't really generate impure LHS of assignments,
but it doesn't hurt to be defensive. *)
| _ when not (is_pure lhs) -> Assign (lhs, rhs)
| Op2 (Add, e2, Cast (_, Constant (_, "1")))
| Op2 (Add, e2, Constant (_, "1")) when e2 = lhs ->
Op1 (PostIncr, lhs)
| Op2 (Add, Cast (_, Constant (_, "1")), e2)
| Op2 (Add, Constant (_, "1"), e2) when e2 = lhs ->
Op1 (PostIncr, lhs)
| Op2 (Sub, e2, Cast (_, Constant (_, "1")))
| Op2 (Sub, e2, Constant (_, "1")) when e2 = lhs ->
Op1 (PostDecr, lhs)
| Op2 (op, e2, e3) when is_compoundable op && e2 = lhs ->
CompoundAssign (lhs, op, e3)
| Op2 (op, e2, e3) when e3 = lhs && is_compoundable op && is_commutative op ->
CompoundAssign (lhs, op, e2)
| _ ->
Assign (lhs, rhs)
let is_var = function Var _ -> true | _ -> false
let is_call = function
| Call (Qualified (_, s), _) ->
not (KString.starts_with s "op_Bang_Star__")
| _ -> false
let fresh =
let r = ref (-1) in
fun () ->
incr r;
"_" ^ string_of_int !r
let escape_string s =
let b = Buffer.create 256 in
String.iter (fun c ->
match c with
| '\x27' -> Buffer.add_string b "\\'"
| '\x22' -> Buffer.add_string b "\\\""
| '\x3f' -> Buffer.add_string b "\\?"
| '\x5c' -> Buffer.add_string b "\\\\"
| '\x07' -> Buffer.add_string b "\\a"
| '\x08' -> Buffer.add_string b "\\b"
| '\x0c' -> Buffer.add_string b "\\f"
| '\x0a' -> Buffer.add_string b "\\n"
| '\x0d' -> Buffer.add_string b "\\r"
| '\x09' -> Buffer.add_string b "\\t"
| '\x0b' -> Buffer.add_string b "\\v"
| '\x20'..'\x7e' -> Buffer.add_char b c
| _ -> Printf.bprintf b "\\x%02x" (Char.code c)
) s;
Buffer.contents b
let to_c_name = GlobalNames.to_c_name
let assert_var m =
function
| Var v ->
v
| Qualified v ->
to_c_name m (v, Other)
| e -> Warn.fatal_error
"TODO: for (int i = 0, t tmp = e1; i < ...; ++i) tmp[i] = \n%s is not a var"
(show_expr e)
module S = Set.Make(String)
let rec vars_of m = function
| CStar.Var v ->
S.singleton v
| Qualified v
| Macro v ->
S.singleton (to_c_name m (v, Other))
| Constant _
| Bool _
| StringLiteral _
| Any
| EAbort _
| Type _
| BufNull
| Op _ ->
S.empty
| Cast (e, _)
| Field (e, _)
| AddrOf e
| InlineComment (_, e, _) ->
vars_of m e
| BufRead (e1, e2)
| BufSub (e1, e2)
| Comma (e1, e2)
| BufCreate (_, e1, e2) ->
S.union (vars_of m e1) (vars_of m e2)
| Call (e, es) ->
List.fold_left S.union (vars_of m e) (List.map (vars_of m) es)
| BufCreateL (_, es) ->
KList.reduce S.union (List.map (vars_of m) es)
| Struct (_, fieldexprs) ->
KList.reduce S.union (List.map (fun (_, e) -> vars_of m e) fieldexprs)
| Stmt stmts ->
vars_of_block m stmts
| Ternary (e1, e2, e3) ->
S.union (vars_of m e1) (S.union (vars_of m e2) (vars_of m e3))
| Sizeof _t ->
S.empty
and vars_of_block m stmts =
KList.reduce S.union (List.map (vars_of_stmt m) stmts)
and vars_of_stmt m = function
| CStar.Abort _
| Return _
| Break
| Continue
| Comment _ ->
S.empty
| Ignore e
| BufFree (_, e) ->
vars_of m e
| Block stmts ->
vars_of_block m stmts
| Decl ({ name; _ }, e) ->
S.remove name (vars_of m e)
| IfThenElse (_, e, b1, b2) ->
S.union (vars_of m e) (S.union (vars_of_block m b1) (vars_of_block m b2))
| While (e, b) ->
S.union (vars_of m e) (vars_of_block m b)
| For (i, e, s, b) ->
begin match i with
| `Decl ({ name; _ }, e') ->
S.remove name (
KList.reduce S.union [
vars_of m e;
vars_of m e';
vars_of_stmt m s;
vars_of_block m b
]
)
| `Skip ->
KList.reduce S.union [
vars_of m e;
vars_of_stmt m s;
vars_of_block m b
]
| `Stmt s' ->
KList.reduce S.union [
vars_of m e;
vars_of_stmt m s;
vars_of_stmt m s';
vars_of_block m b
]
end
| Assign (e, _, e') ->
S.union (vars_of m e) (vars_of m e')
| Switch (e, cs, b) ->
KList.reduce S.union ([
vars_of m e;
match b with Some b -> vars_of_block m b | None -> S.empty
] @ List.map (fun (_, b) -> vars_of_block m b) cs)
| BufWrite (e1, e2, e3) ->
KList.reduce S.union [
vars_of m e1;
vars_of m e2;
vars_of m e3;
]
| BufFill (_, e2, e3, e4) ->
KList.reduce S.union [
vars_of m e2;
vars_of m e3;
vars_of m e4;
]
| BufBlit (_, e2, e3, e4, e5, e6) ->
KList.reduce S.union [
vars_of m e2;
vars_of m e3;
vars_of m e4;
vars_of m e5;
vars_of m e6;
]
let c99_format w =
let open K in
"PRIx" ^ string_of_int (bytes_of_width w * 8)
let mk_debug name parameters =
if Options.debug "c-calls" then
let formats, args = List.split (List.map (fun (name, typ) ->
match typ with
| Int w ->
Printf.sprintf "%s=0x%%08\"%s\"" name (c99_format w), C.Name name
| Bool ->
Printf.sprintf "%s=%%d" name, C.Name name
(* | Pointer (Int w) -> *)
(* Some (Printf.sprintf "%s[0]=%%\"%s\"" name (c99_format w), C.Deref (C.Name name)) *)
| _ ->
Printf.sprintf "%s=%%s" name, C.Literal "unknown"
) parameters) in
[ C.Expr (C.Call (C.Name "KRML_HOST_PRINTF", [
C.Literal (String.concat " " (name :: formats @ [ "\\n" ]))
] @ args)) ]
else
[]
let mk_pretty_type = function
| "FStar_UInt128_uint128" when !Options.builtin_uint128 ->
"uint128_t"
| x ->
x
let bytes_in = function
(* SizeT does not have a statically known size *)
| Int SizeT -> None
| Int w -> Some (K.bytes_of_width w)
| Qualified ([ "FStar"; "UInt128" ], "uint128") -> Some (128 / 8)
| Qualified ([ "Lib"; "IntVector"; "Intrinsics" ], "vec128") -> Some (128 / 8)
| Qualified ([ "Lib"; "IntVector"; "Intrinsics" ], "vec256") -> Some (256 / 8)
| Qualified ([ "Lib"; "IntVector"; "Intrinsics" ], "vec32") -> Some (32 / 8)
| _ -> None
(* https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3088.pdf 6.7.10
The goal of this function is to generate an initializer list that is i) compact and ii) sensible.
For compactness, it appears that owing to §20 and §22 above, one can just omit remaining fields /
indices when their initial value is zero (since they are initialized as if they had static storage
duration).
For sensibility, we don't want to be *too* smart and e.g. omit the last field of a struct if it
statically known to be zero; we also want to emit { 0 } for subarrays, not {} which is C++/C23
syntax for default (i.e. zero) initialization.
*)
let is_zero_expr = function
| C11.Constant (_, "0") -> true
| C11.Cast (_, Constant (_, "0")) -> true
| _ -> false
let rec is_zero = function
| InitExpr e -> is_zero_expr e
| Designated (_, _i) -> false (* too smart: is_zero i *)
| Initializer is -> List.for_all is_zero is
let rec trim_trailing_zeros l =
match l with
| Initializer [] -> Initializer []
| InitExpr (CompoundLiteral (_, l)) -> Initializer (List.map trim_trailing_zeros (chop_zeros l))
| InitExpr e -> InitExpr e
| Designated (d, init) -> Designated (d, trim_trailing_zeros init)
| Initializer l -> Initializer (List.map trim_trailing_zeros (chop_zeros l))
and chop_zeros is =
let rec chop_zeros = function
| hd :: tl when is_zero hd -> chop_zeros tl
| [] -> [ InitExpr (C11.Constant (K.UInt32, "0")) ]
| l -> List.rev l
in
chop_zeros (List.rev is)
let workaround_gcc_bug53119_fml t init =
let rec array_nesting = function
| Array (t, _) -> 1 + array_nesting t
| Const t -> array_nesting t
| _ -> 0
in
match init with
| Initializer [ init ] when is_zero init && array_nesting t > 1 ->
let rec nest l =
if l = 0 then
Initializer [ init ]
else
Initializer [ nest (l - 1) ]
in
nest (array_nesting t - 1)
| _ ->
init
(* Turns the ML declaration inside-out to match the C reading of a type.
* See: en.cppreference.com/w/c/language/declarations.
* The continuation is key in the Function case. *)
let rec mk_spec_and_decl m name qs (t: typ) (k: C.declarator -> C.declarator):
C.qualifier list * C.type_spec * C.declarator
=
match t with
| Const t ->
(* Originally, this was added via a mechanism of attributes and made sense only in the pointer
case. Now... mostly used by Eurydice to mark constants as global. *)
mk_spec_and_decl m name [ C.Const ] t k
| Pointer t ->
mk_spec_and_decl m name [] t (fun d -> Pointer (qs, k d))
| Array (t, size) ->
(* F* guarantees that the initial size of arrays is always something
* reasonable (i.e. <4GB). *)
let size = match size with
| Some (Constant k) -> Some (C.Constant k)
| Some size -> Some (mk_expr m size)
| None -> None
in
mk_spec_and_decl m name qs t (fun d -> Array ([], k d, size))
| Function (cc, t, ts) ->
(* Function types are pointers to function types, except in the top-level
* declarator for a function, which gets special treatment via
* mk_spec_and_declarator_f. *)
mk_spec_and_decl m name [] t (fun d ->
Function (cc, Pointer (qs, k d), List.mapi (fun i t ->
mk_spec_and_decl m (KPrint.bsprintf "x%d" i) [] t (fun d -> d)) ts))
| Int w ->
qs, Int w, k (Ident name)
| Void ->
qs, Void, k (Ident name)
| Qualified l ->
qs, Named (mk_pretty_type (to_c_name m (l, Type))), k (Ident name)
| Enum tags ->
(* Important: the tags of an enum live in the value namespace, not the type namespace. *)
let tags = List.map (fun (lid, v) -> to_c_name m (lid, Other), v) tags in
qs, Enum (None, tags), k (Ident name)
| Bool ->
let bool = if !Options.microsoft then "BOOLEAN" else "bool" in
qs, Named bool, k (Ident name)
| Struct fields ->
qs, Struct (None, mk_fields m fields), k (Ident name)
| Union fields ->
qs, Union (None, mk_union_fields m fields), k (Ident name)
and mk_fields m fields =
Some (List.map (fun (name, typ) ->
let name = match name with Some name -> name | None -> "" in
let qs, spec, decl = mk_spec_and_declarator m name typ in
qs, spec, None, None, { maybe_unused = false; target = None }, [ decl, None, None ]
) fields)
and mk_union_fields m fields =
Some (List.map (fun (name, typ) ->
let qs, spec, decl = mk_spec_and_decl m name [] typ (fun d -> d) in
qs, spec, None, None, { maybe_unused = false; target = None }, [ decl, None, None ]
) fields)
(* Standard spec/declarator pair (e.g. int x). *)
and mk_spec_and_declarator m name t =
mk_spec_and_decl m name [] t (fun d -> d)
(* A variant dedicated to typedef's, where we need to name structs. *)
and mk_spec_and_declarator_t m name t =
match t with
| Struct fields ->
(* In C, there's a separate namespace for struct names; our type names are
* unique, therefore, post-fixing them with "_s" also generates a set of
* unique struct names. *)
[], C.Struct (Some (name ^ "_s"), mk_fields m fields), Ident name
| Union fields ->
[], C.Union (Some (name ^ "_s"), mk_union_fields m fields), Ident name
| _ ->
mk_spec_and_declarator m name t
(* A variant dedicated to functions that avoids the conversion of function type
* to pointer-to-function. *)
and mk_spec_and_declarator_f m cc name ret_t params =
mk_spec_and_decl m name [] ret_t (fun d ->
Function (cc, d, List.map (fun (n, t) -> mk_spec_and_declarator m n t) params))
(* Enforce the invariant that declarations are wrapped in compound statements
* and cannot appear "alone". *)
and mk_compound_if (stmts: C.stmt list) (under_else: bool): C.stmt =
match stmts with
| [ Decl _ ] ->
Compound stmts
| [ If _ | IfElse _ as stmt ] when under_else ->
(* Never wrap an if under else with braces, because it would defeat `else
* if` on the same line. *)
stmt
| [ stmt ] when not !Options.curly_braces ->
stmt
| _ ->
Compound stmts
and ensure_compound (stmts: C.stmt list): C.stmt =
match stmts with
| [ Compound _ as stmt ] ->
stmt
| _ ->
Compound stmts
(* Ideally, most of the for-loops should've been desugared C89-style if needed
* beforehand. *)
and mk_for_loop name qs t init test incr body =
if !Options.c89_scope then
Compound [
Decl (qs, t, None, None, { maybe_unused = false; target = None }, [ Ident name, None, None ]);
For (
`Expr (Op2 (K.Assign, Name name, init)),
test, incr, body)
]
else
For (
`Decl (qs, t, None, None, { maybe_unused = false; target = None }, [ Ident name, None, Some (InitExpr init)]),
test, incr, body)
(* Takes e_array of type (Buf t). e_size = size of the array, in number of
elements *)
and mk_initializer ~null_check t e_array e_size e_value: C.stmt =
(* KPrint.bprintf "element_size is: %s\n" (C11.show_expr e_size); *)
(* KPrint.bprintf "t is: %s\n" (C11.show_type_name t); *)
let if_not_null e: C.stmt =
if null_check then
if !Options.curly_braces then
If (Op2 (K.Neq, e_array, Name "NULL"), Compound [ e ])
else
If (Op2 (K.Neq, e_array, Name "NULL"), e)
else
e
in
match e_size with
| C.Constant (_, "1")
| C.Cast (_, C.Constant (_, "1")) ->
if_not_null @@
Expr (Op2 (K.Assign, Index (e_array, Constant (K.UInt32, "0")), e_value))
| _ ->
match e_value with
| C.Constant (_, s)
| C.Cast (_, C.Constant (_, s)) when int_of_string s = 0 ->
(* KPrint.bprintf "need memset 0\n"; *)
if_not_null @@
mk_memset t e_array e_size (C.Constant (K.UInt8, "0"))
| C.Name "Lib_IntVector_Intrinsics_vec128_zero"
| C.Name "Lib_IntVector_Intrinsics_vec256_zero"
| C.Name "Lib_IntVector_Intrinsics_vec512_zero" ->
(* Same as above. This is important to avoid generating avx2 instructions when merely
allocating simd state. Under the hood, the C memset will use suitable instructions to
go fast. *)
(* KPrint.bprintf "need memset 1\n"; *)
if_not_null @@
mk_memset t e_array e_size (C.Constant (K.UInt8, "0"))
| C.Constant (K.UInt8, _)
| C.Cast (_, C.Constant (K.UInt8, _)) ->
(* KPrint.bprintf "need memset 2\n"; *)
if_not_null @@
mk_memset t e_array e_size e_value
| _ ->
if_not_null @@
mk_for_loop "_i" [] (Int K.UInt32) zero
(Op2 (K.Lt, Name "_i", e_size))
(Op1 (K.PreIncr, Name "_i"))
(Expr (Op2 (K.Assign, Index (e_array, Name "_i"), e_value)))
and mk_memset t e_array e_size e_init =
let e_size =
(* Commenting out this optimization below, because:
* error: ‘memset’ used with length equal to number of elements without
* multiplication by element size [-Werror=memset-elt-size]
* is it not known that sizeof uint8_t = 1 ? *)
(* match e_init with
| C.Constant (K.UInt8, _) ->
e_size
| _ -> *)
Op2 (K.Mult, e_size, Sizeof (Type t))
in
Expr (Call (Name "memset", [ e_array; e_init; e_size]))
and mk_check_size m t n_elements: C.stmt list =
(* [init] is the default value for the elements of the array, and [n_elements] is
* hopefully a constant *)
let default = [ C.Expr (C.Call (C.Name "KRML_CHECK_SIZE", [ mk_sizeof m t; n_elements ])) ] in
match bytes_in t, n_elements with
| _, C.Cast (_, C.Constant (_, "1"))
| _, C.Constant (_, "1") ->
(* C compilers also don't seem to let the user define a type that would be
greater than size_t, so if the element size is 1, then we can get rid
of the check as well. *)
[]
| Some w, C.Cast (_, C.Constant (_, n_elements))
| Some w, C.Constant (_, n_elements) ->
(* Compute, if we can, the size statically *)
let size_bytes = Z.(of_int w * of_string n_elements) in
let ptr_size = Z.(one lsl 16) in
(* The C data model guarantees 16 bits wide for size_t, at least. *)
if Z.( lt size_bytes ptr_size )then
[]
else
default
| _ ->
(* Nothing much we can deduce statically, bail *)
default
and mk_sizeof m t =
C.Sizeof (C.Type (mk_type m t))
and mk_sizeof_mul m t s =
match s with
| C.Constant (_, "1")
| C.Cast (_, C.Constant (_, "1")) ->
mk_sizeof m t
| _ ->
C.Op2 (K.Mult, mk_sizeof m t, s)
and mk_alloc_cast m t e =
if !Options.cast_allocations then
C.Cast (mk_type m (Pointer t), e)
else
e
and mk_malloc m t s =
match t with
| Qualified lid when Helpers.is_aligned_type lid ->
let sz = Option.get (mk_alignment m t) in
mk_alloc_cast m t (C.Call (C.Name "KRML_ALIGNED_MALLOC", [ sz; mk_sizeof_mul m t s ]))
| _ ->
mk_alloc_cast m t (C.Call (C.Name "KRML_HOST_MALLOC", [ mk_sizeof_mul m t s ]))
and mk_calloc m t s =
mk_alloc_cast m t (C.Call (C.Name "KRML_HOST_CALLOC", [ s; mk_sizeof m t ]))
and mk_free t e =
match t with
| Qualified lid when Helpers.is_aligned_type lid ->
C.Call (C.Name "KRML_ALIGNED_FREE", [ e ])
| _ ->
C.Call (C.Name "KRML_HOST_FREE", [ e ])
and mk_ignore is_var e =
if is_var then
C.Call (C.Name "KRML_MAYBE_UNUSED_VAR", [ e ])
else
C.Call (C.Name "KRML_HOST_IGNORE", [ e ])
(* NOTE: this is only legal because we rule out the creation of zero-length
* heap-allocated buffers; if we were to allow that, then this begs the question
* of whether memset(malloc(0), 0, 0) is UB or not! The result of malloc(0) is
* implementation-defined, not undefined behavior. *)
and mk_eternal_bufcreate m buf (t: CStar.typ) init size =
let size = mk_expr m size in
let e, extra_stmt = match init with
| Constant (_, "0") ->
(* NOTE: we MUST NOT catch vector types here because there is no aligned_calloc! *)
mk_calloc m t size, []
| Any | Cast (Any, _) ->
mk_malloc m t size, []
| _ ->
mk_malloc m t size,
[ mk_initializer ~null_check:true (mk_type m t) (mk_expr m buf) size (mk_expr m init) ]
in
mk_check_size m t size, e, extra_stmt
and assert_pointer t =
match t with
| Array (t, _)
| Pointer t ->
t
| _ ->
Warn.fatal_error "let-bound bufcreate has type %s instead of Pointer" (show_typ t)
and ensure_array_l t inits =
assert (List.for_all (function BufCreate _ -> false | _ -> true) inits);
match t with
| Pointer t ->
Array (t, Some (Constant (K.uint32_of_int (List.length inits))))
| _ ->
t
(* Returns
- C* array type
- C* initializer
- C* num elems
- C11 (translated) size of elem
*)
and ensure_array m (t : CStar.typ) (expr : CStar.expr) : CStar.typ * CStar.expr * CStar.expr * C.expr =
match t, expr with
| Pointer t, BufCreate ((Stack | Eternal), init, len) ->
let _, init, len', size' = ensure_array m t init in
Array (t, Some len), init, len, cmul (mk_expr m len') size'
| Array (t, l), BufCreate ((Stack | Eternal), init, len) ->
assert (l = Some len);
let _, init, len', size' = ensure_array m t init in
Array (t, Some len), init, len, cmul (mk_expr m len') size'
| _ ->
t, expr, CStar.Constant (K.UInt8, "1"), C.Sizeof (C.Type (mk_type m t))
and decay_array t =
match t with
| Array (t, _) ->
Pointer t
| t ->
Warn.fatal_error "impossible: %s" (show_typ t)
and assert_array t =
match t with
| Array (t, _) ->
t
| t ->
Warn.fatal_error "impossible: not an array %s" (show_typ t)
and is_aligned_type = function
| Qualified lid ->
Helpers.is_aligned_type lid
| _ ->
false
and mk_alignment m t: C11.expr option =
if is_aligned_type t then
match t with
| Qualified (["Lib"; "IntVector"; "Intrinsics"], "vec128") ->
Some (Constant (CInt, "16"))
| Qualified (["Lib"; "IntVector"; "Intrinsics"], "vec256") ->
Some (Constant (CInt, "32"))
| Qualified (["Lib"; "IntVector"; "Intrinsics"], "vec512") ->
Some (Constant (CInt, "64"))
| _ ->
Some (Sizeof (Type (mk_type m t)))
else
None
and to_initializer m = function
| BufCreateL ((Stack | Eternal), inits) ->
Initializer (List.map (to_initializer m) inits)
| BufCreate ((Stack | Eternal), init, size) ->
begin match size with
| Constant (_, c) ->
Initializer (List.init (int_of_string c) (fun _ -> to_initializer m init))
| _ -> failwith (CStar.show_expr size ^ " is not a constant")
end
| e ->
InitExpr (mk_expr m e)
and mk_stmt m (stmt: stmt): C.stmt list =
match stmt with
| Comment s ->
[ Comment s ]
| Return e ->
let e = Option.map (fun e ->
let e = mk_expr m e in
if Options.debug "c-calls" then
C.Call (Name "KRML_DEBUG_RETURN", [ e ])
else
e
) e in
[ Return e ]
| Block stmts ->
[ Compound (mk_stmts m stmts) ]
| Break ->
[ Break ]
| Continue ->
[ Continue ]
(* Ignore injects `expr`s into `stmt`s by ignoring their return value. No need
to double-ignore, since C does it for us automatically, and C compilers
treat this as 100% normal UNLESS the programmer uses extensions like
`__attribute__((nodiscard))`. *)
| Ignore (Call (Qualified ([ "LowStar"; "Ignore" ], "ignore"), [ arg; _; _ ])) when is_call arg ->
[ Expr (mk_expr m arg) ]
| Ignore e ->
[ Expr (mk_expr m e) ]
| Decl (binder, BufCreate ((Eternal | Heap), init, size)) ->
let t = assert_pointer binder.typ in
let stmt_check, expr_alloc, stmt_extra =
mk_eternal_bufcreate m (Var binder.name) t init size
in
let qs, spec, decl = mk_spec_and_declarator m binder.name binder.typ in
let decl: C.stmt list = [ Decl (qs, spec, None, None, { maybe_unused = false; target = None }, [ decl, None, Some (InitExpr expr_alloc)]) ] in
stmt_check @ decl @ stmt_extra
| Decl (binder, (BufCreate (Stack, _, _) as rhs)) ->
(* In the case where this is a buffer creation in the C* meaning, then we
* declare a fixed-length array; this is an "upcast" from pointer type to
* array type, in the C sense. *)
let t, init, n_elements, e_size = ensure_array m binder.typ rhs in
let rebind_n_elements, cstar_n_elements, n_elements =
(* If the length expression is complex, then assign it to a local
variable and use that. Otherwise we would duplicate it, potentially
affecting the meaning. *)
let rec is_pure e =
match e with
| Constant _ | Var _ | Macro _ | Qualified _
| BufRead _ | BufSub _ | BufNull
| Op _ | Bool _ | Type _ | StringLiteral _
| Any | Sizeof _ ->
true
| Ternary (c, t, e) ->
is_pure c && is_pure t && is_pure e
(* Calls in general we take as impure, but operators
are pure. *)
| Call (Op _, args) -> List.for_all is_pure args
| Call _ | BufCreate _ | BufCreateL _ | Stmt _ | EAbort _ ->
false
(* just descend *)
| Cast (e, _)
| InlineComment (_, e, _)
| Field (e, _)
| AddrOf e ->
is_pure e
| Struct (_, fs) ->
List.for_all (fun (_, e) -> is_pure e) fs
| Comma (e1, e2) ->
is_pure e1 && is_pure e2
in
if is_pure n_elements then
[], n_elements, mk_expr m n_elements
else
let name_init = "_arraylen" ^ fresh () in
let no_extra = { maybe_unused = false; target = None } in
let stmt_init : C.stmt = C.Decl ([], Int SizeT, None, None, no_extra, [(Ident name_init, None, Some (InitExpr (mk_expr m n_elements)))]) in
[stmt_init], CStar.Var name_init, C.Name name_init
in
(* Having rebound n_elements, make sure that `t` uses this expression
for its size, instead of the original one. Otherwise the declaration
for the array will contain it. *)
let t =
match t with
| Array (et, Some _len) -> Array (et, Some cstar_n_elements)
| t -> t
in
(* KPrint.bprintf "n_elements is: %s\n" (C11.show_expr n_elements); *)
(* KPrint.bprintf "e_size is: %s\n" (C11.show_expr e_size); *)
let alignment = mk_alignment m (assert_array t) in
let is_constant = match n_elements with Constant _ -> true | _ -> false in
let use_alloca = not is_constant && !Options.alloca_if_vla in
let (maybe_init, needs_init): C.init option * _ = match init, n_elements with
| _, Constant (_, "0") (* zero-sized array... legal for malloc *)
| Cast (Any, _), _
| Any, _ ->
(* No initial value needed in the declarator; no further
* initialization needed either. *)
None, false
| (Constant (_, "0") |
Qualified (["Lib"; "IntVector"; "Intrinsics"],
("vec128_zero" | "vec256_zero" | "vec512_zero"))),
Constant _ when not use_alloca ->
(* The only case the we can initialize statically is a known, static
* size _and_ a zero initializer. If we're about to alloca, don't
* use a zero-initializer. *)
Some (Initializer [ InitExpr (C.Constant (K.UInt32, "0")) ]), false
| _ ->
None, true
in
let t, maybe_init =
(* If we're doing an alloca, override the initial value (it's now the
* call to alloca) and decay the array to a pointer type. *)
if use_alloca then
if alignment <> None then
Warn.fatal_error "In the following statement, the variable-length \
array on the stack (VLA) must be aligned, but -falloca mandates the \
use of alloca, which krml cannot yet align\n%s\n"
(show_stmt stmt)
else
let bytes = mk_alloc_cast m (assert_pointer t) (C.Call (C.Name "alloca", [ cmul n_elements e_size ])) in
assert (maybe_init = None);
decay_array t, Some (InitExpr bytes)
else
t, maybe_init
in
let init = mk_expr m init in
let qs, spec, decl = mk_spec_and_declarator m binder.name t in
let extra_stmt: C.stmt list =
if needs_init then
[ mk_initializer ~null_check:false (mk_type m (assert_pointer t)) (Name binder.name) n_elements init ]
else
[]
in
let decl: C.stmt list = [ Decl (qs, spec, None, None, { maybe_unused = false; target = None }, [ decl, alignment, maybe_init ]) ] in
rebind_n_elements @
mk_check_size m (assert_pointer binder.typ) n_elements @
decl @
extra_stmt
| Decl (_, BufCreateL ((Eternal | Heap), _)) as s ->
failwith ("TODO: the array below is either in the eternal or heap region, \
uses createL, but we don't have (yet) codegen for this:\n" ^
CStar.show_stmt s)
| Decl (binder, (BufCreateL (Stack, inits) as init_expr)) ->
(* Per the C standard, static initializers guarantee for missing fields
* that they're initialized as if they had static storage duration, i.e.
* with zero. *)
let t = ensure_array_l binder.typ inits in
let alignment = mk_alignment m (assert_array t) in
let qs, spec, decl = mk_spec_and_declarator m binder.name t in
let init_expr = trim_trailing_zeros (to_initializer m init_expr) in
let init_expr = workaround_gcc_bug53119_fml t init_expr in
[ Decl (qs, spec, None, None, { maybe_unused = false; target = None }, [ decl, alignment, Some init_expr])]
| Decl (binder, e) ->
let qs, spec, decl = mk_spec_and_declarator m binder.name binder.typ in
let init: init option = match e with Any -> None | _ -> Some (trim_trailing_zeros (struct_as_initializer m e)) in
[ Decl (qs, spec, None, None, { maybe_unused = false; target = None }, [ decl, None, init ]) ]
| IfThenElse (false, e, b1, b2) ->
if List.length b2 > 0 then
[ IfElse (mk_expr m e, mk_compound_if (mk_stmts m b1) false, mk_compound_if (mk_stmts m b2) true) ]
else
[ If (mk_expr m e, mk_compound_if (mk_stmts m b1) false) ]
| IfThenElse (true, e, b1, b2) ->
let rec find_elif acc = function
| [ IfThenElse (true, e, b1, b2) ] ->
let acc = (mk_expr m e, mk_stmts m b1) :: acc in
find_elif acc b2
| b ->
List.rev acc, mk_stmts m b
in
let elif_blocks, else_block = find_elif [] b2 in
[ IfDef (mk_expr m e, mk_stmts m b1, elif_blocks, else_block) ]
| Assign (BufRead _, _, (Any | Cast (Any, _))) ->
[]
| Assign (e1, t, BufCreate (Eternal, init, size)) ->
let v = assert_var m e1 in
(* Evil bug:
* x <- bufcreate 1 e[x]
* might become:
* x <- bufcreate 1 any
* x[0] <- e[x] <--- does NOT evaluate to the previous value of x
* Note: from Simplify.ml we know that BufCreate appears only under a
* [Decl] node or an [Assign] node. Name collisions are not possible with
* the Decl case since [AstToCStar] would've avoided the name conflict,
* and F* does not have recursive value definitions.
*)
let vs = vars_of m init in
if S.mem v vs then
let t_elt = assert_pointer t in
let name_init = "_init" ^ fresh () in
let size = mk_expr m size in
let stmt_init = mk_stmt m (Decl ({ name = name_init; typ = t_elt }, init)) in
let stmt_assign = [ Expr (Assign (mk_expr m e1, mk_malloc m t_elt size)) ] in
(* GC'd allocation, not null-checking this -- this is LEGACY *)
let stmt_fill = mk_initializer ~null_check:false (mk_type m t_elt) (mk_expr m e1) size (mk_expr m (Var name_init)) in
stmt_init @
stmt_assign @
[ stmt_fill ]
else
let stmt_check, expr_alloc, stmt_extra = mk_eternal_bufcreate m e1 (assert_pointer t) init size in
stmt_check @
[ Expr (Assign (mk_expr m e1, expr_alloc)) ] @
stmt_extra
| Assign (_, _, BufCreateL (Eternal, _)) ->
failwith "TODO"
| Assign (e1, _, e2) ->
[ Expr (mk_assign (mk_expr m e1) (mk_expr m e2)) ]
| BufWrite (_, _, (Any | Cast (Any, _))) ->
[]
| BufWrite (e1, e2, e3) ->
[ Expr (mk_assign (mk_index m e1 e2) (mk_expr m e3)) ]
| BufBlit (t, e1, e2, e3, e4, e5) ->
let dest = match e4 with
| Constant (_, "0") -> mk_expr m e3
| _ -> Op2 (K.Add, mk_expr m e3, mk_expr m e4)
in
let source = match e2 with
| Constant (_, "0") -> mk_expr m e1
| _ -> Op2 (K.Add, mk_expr m e1, mk_expr m e2)
in
[ Expr (Call (Name "memcpy", [
dest;
source;
Op2 (K.Mult, mk_expr m e5, mk_sizeof m t)])) ]
| BufFill (t, buf, v, size) ->
(* Again, assuming that these are non-effectful. *)
[ mk_initializer ~null_check:false (mk_type m t) (mk_expr m buf) (mk_expr m size) (mk_expr m v) ]
| BufFree (t, e) ->
[ Expr (mk_free t (mk_expr m e)) ]
| While (e1, e2) ->
[ While (mk_expr m e1, mk_compound_if (mk_stmts m e2) false) ]
| Switch (e, branches, default) ->
[ Switch (
mk_expr m e,
List.map (fun (ident, block) ->
(match ident with
| `Ident ident -> Name (to_c_name m (ident, Other))
| `Int k -> Constant k),
let block = mk_stmts m block in
if List.length block > 0 then
match KList.last block with
| Return _ -> Compound block
| _ -> Compound (block @ [ Break ])
else
Compound [ Break ]
) branches,
match default with
| Some block ->
Compound (mk_stmts m block)
| _ ->
let p = if !Options.c89_std then "KRML_HOST_PRINTF" else "KRML_HOST_EPRINTF" in
Compound [
Expr (Call (Name p, [
Literal "KaRaMeL incomplete match at %s:%d\\n"; Name "__FILE__"; Name "__LINE__" ]));
Expr (Call (Name "KRML_HOST_EXIT", [ Constant (K.UInt8, "253") ]))
]
)]
| Abort s ->
let p = if !Options.c89_std then "KRML_HOST_PRINTF" else "KRML_HOST_EPRINTF" in
[ Expr (Call (Name p, [
Literal "KaRaMeL abort at %s:%d\\n%s\\n"; Name "__FILE__"; Name "__LINE__"; Literal (escape_string s) ]));
Expr (Call (Name "KRML_HOST_EXIT", [ Constant (K.UInt8, "255") ])); ]
| For (`Decl (binder, e1), e2, e3, b) ->
let qs, spec, decl = mk_spec_and_declarator m binder.name binder.typ in
let name = match decl with Ident name -> name | _ -> failwith "not an ident" in
let init = match struct_as_initializer m e1 with InitExpr init -> init | _ -> failwith "not an initexpr" in
let e2 = mk_expr m e2 in
let e3 = match mk_stmt m e3 with [ Expr e3 ] -> e3 | _ -> assert false in
let b = mk_compound_if (mk_stmts m b) false in
[ mk_for_loop name qs spec init e2 e3 b ]
| For (e1, e2, e3, b) ->
let e1 = match e1 with
| `Skip -> `Skip
| `Stmt e1 -> `Expr (match mk_stmt m e1 with [ Expr e1 ] -> e1 | _ -> assert false)
| _ -> assert false
in
let e2 = mk_expr m e2 in
let e3 = match mk_stmt m e3 with [ Expr e3 ] -> e3 | _ -> assert false in